Merge tag 'gcc-plugins-v4.9-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / drivers / input / rmi4 / rmi_f01.c
1 /*
2  * Copyright (c) 2011-2016 Synaptics Incorporated
3  * Copyright (c) 2011 Unixphere
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/rmi.h>
12 #include <linux/slab.h>
13 #include <linux/uaccess.h>
14 #include <linux/of.h>
15 #include "rmi_driver.h"
16
17 #define RMI_PRODUCT_ID_LENGTH    10
18 #define RMI_PRODUCT_INFO_LENGTH   2
19
20 #define RMI_DATE_CODE_LENGTH      3
21
22 #define PRODUCT_ID_OFFSET 0x10
23 #define PRODUCT_INFO_OFFSET 0x1E
24
25
26 /* Force a firmware reset of the sensor */
27 #define RMI_F01_CMD_DEVICE_RESET        1
28
29 /* Various F01_RMI_QueryX bits */
30
31 #define RMI_F01_QRY1_CUSTOM_MAP         BIT(0)
32 #define RMI_F01_QRY1_NON_COMPLIANT      BIT(1)
33 #define RMI_F01_QRY1_HAS_LTS            BIT(2)
34 #define RMI_F01_QRY1_HAS_SENSOR_ID      BIT(3)
35 #define RMI_F01_QRY1_HAS_CHARGER_INP    BIT(4)
36 #define RMI_F01_QRY1_HAS_ADJ_DOZE       BIT(5)
37 #define RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF  BIT(6)
38 #define RMI_F01_QRY1_HAS_QUERY42        BIT(7)
39
40 #define RMI_F01_QRY5_YEAR_MASK          0x1f
41 #define RMI_F01_QRY6_MONTH_MASK         0x0f
42 #define RMI_F01_QRY7_DAY_MASK           0x1f
43
44 #define RMI_F01_QRY2_PRODINFO_MASK      0x7f
45
46 #define RMI_F01_BASIC_QUERY_LEN         21 /* From Query 00 through 20 */
47
48 struct f01_basic_properties {
49         u8 manufacturer_id;
50         bool has_lts;
51         bool has_adjustable_doze;
52         bool has_adjustable_doze_holdoff;
53         char dom[11]; /* YYYY/MM/DD + '\0' */
54         u8 product_id[RMI_PRODUCT_ID_LENGTH + 1];
55         u16 productinfo;
56         u32 firmware_id;
57 };
58
59 /* F01 device status bits */
60
61 /* Most recent device status event */
62 #define RMI_F01_STATUS_CODE(status)             ((status) & 0x0f)
63 /* The device has lost its configuration for some reason. */
64 #define RMI_F01_STATUS_UNCONFIGURED(status)     (!!((status) & 0x80))
65
66 /* Control register bits */
67
68 /*
69  * Sleep mode controls power management on the device and affects all
70  * functions of the device.
71  */
72 #define RMI_F01_CTRL0_SLEEP_MODE_MASK   0x03
73
74 #define RMI_SLEEP_MODE_NORMAL           0x00
75 #define RMI_SLEEP_MODE_SENSOR_SLEEP     0x01
76 #define RMI_SLEEP_MODE_RESERVED0        0x02
77 #define RMI_SLEEP_MODE_RESERVED1        0x03
78
79 /*
80  * This bit disables whatever sleep mode may be selected by the sleep_mode
81  * field and forces the device to run at full power without sleeping.
82  */
83 #define RMI_F01_CTRL0_NOSLEEP_BIT       BIT(2)
84
85 /*
86  * When this bit is set, the touch controller employs a noise-filtering
87  * algorithm designed for use with a connected battery charger.
88  */
89 #define RMI_F01_CTRL0_CHARGER_BIT       BIT(5)
90
91 /*
92  * Sets the report rate for the device. The effect of this setting is
93  * highly product dependent. Check the spec sheet for your particular
94  * touch sensor.
95  */
96 #define RMI_F01_CTRL0_REPORTRATE_BIT    BIT(6)
97
98 /*
99  * Written by the host as an indicator that the device has been
100  * successfully configured.
101  */
102 #define RMI_F01_CTRL0_CONFIGURED_BIT    BIT(7)
103
104 /**
105  * @ctrl0 - see the bit definitions above.
106  * @doze_interval - controls the interval between checks for finger presence
107  * when the touch sensor is in doze mode, in units of 10ms.
108  * @wakeup_threshold - controls the capacitance threshold at which the touch
109  * sensor will decide to wake up from that low power state.
110  * @doze_holdoff - controls how long the touch sensor waits after the last
111  * finger lifts before entering the doze state, in units of 100ms.
112  */
113 struct f01_device_control {
114         u8 ctrl0;
115         u8 doze_interval;
116         u8 wakeup_threshold;
117         u8 doze_holdoff;
118 };
119
120 struct f01_data {
121         struct f01_basic_properties properties;
122         struct f01_device_control device_control;
123
124         u16 doze_interval_addr;
125         u16 wakeup_threshold_addr;
126         u16 doze_holdoff_addr;
127
128         bool suspended;
129         bool old_nosleep;
130
131         unsigned int num_of_irq_regs;
132 };
133
134 static int rmi_f01_read_properties(struct rmi_device *rmi_dev,
135                                    u16 query_base_addr,
136                                    struct f01_basic_properties *props)
137 {
138         u8 queries[RMI_F01_BASIC_QUERY_LEN];
139         int ret;
140         int query_offset = query_base_addr;
141         bool has_ds4_queries = false;
142         bool has_query42 = false;
143         bool has_sensor_id = false;
144         bool has_package_id_query = false;
145         bool has_build_id_query = false;
146         u16 prod_info_addr;
147         u8 ds4_query_len;
148
149         ret = rmi_read_block(rmi_dev, query_offset,
150                                queries, RMI_F01_BASIC_QUERY_LEN);
151         if (ret) {
152                 dev_err(&rmi_dev->dev,
153                         "Failed to read device query registers: %d\n", ret);
154                 return ret;
155         }
156
157         prod_info_addr = query_offset + 17;
158         query_offset += RMI_F01_BASIC_QUERY_LEN;
159
160         /* Now parse what we got */
161         props->manufacturer_id = queries[0];
162
163         props->has_lts = queries[1] & RMI_F01_QRY1_HAS_LTS;
164         props->has_adjustable_doze =
165                         queries[1] & RMI_F01_QRY1_HAS_ADJ_DOZE;
166         props->has_adjustable_doze_holdoff =
167                         queries[1] & RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF;
168         has_query42 = queries[1] & RMI_F01_QRY1_HAS_QUERY42;
169         has_sensor_id = queries[1] & RMI_F01_QRY1_HAS_SENSOR_ID;
170
171         snprintf(props->dom, sizeof(props->dom), "20%02d/%02d/%02d",
172                  queries[5] & RMI_F01_QRY5_YEAR_MASK,
173                  queries[6] & RMI_F01_QRY6_MONTH_MASK,
174                  queries[7] & RMI_F01_QRY7_DAY_MASK);
175
176         memcpy(props->product_id, &queries[11],
177                 RMI_PRODUCT_ID_LENGTH);
178         props->product_id[RMI_PRODUCT_ID_LENGTH] = '\0';
179
180         props->productinfo =
181                         ((queries[2] & RMI_F01_QRY2_PRODINFO_MASK) << 7) |
182                         (queries[3] & RMI_F01_QRY2_PRODINFO_MASK);
183
184         if (has_sensor_id)
185                 query_offset++;
186
187         if (has_query42) {
188                 ret = rmi_read(rmi_dev, query_offset, queries);
189                 if (ret) {
190                         dev_err(&rmi_dev->dev,
191                                 "Failed to read query 42 register: %d\n", ret);
192                         return ret;
193                 }
194
195                 has_ds4_queries = !!(queries[0] & BIT(0));
196                 query_offset++;
197         }
198
199         if (has_ds4_queries) {
200                 ret = rmi_read(rmi_dev, query_offset, &ds4_query_len);
201                 if (ret) {
202                         dev_err(&rmi_dev->dev,
203                                 "Failed to read DS4 queries length: %d\n", ret);
204                         return ret;
205                 }
206                 query_offset++;
207
208                 if (ds4_query_len > 0) {
209                         ret = rmi_read(rmi_dev, query_offset, queries);
210                         if (ret) {
211                                 dev_err(&rmi_dev->dev,
212                                         "Failed to read DS4 queries: %d\n",
213                                         ret);
214                                 return ret;
215                         }
216
217                         has_package_id_query = !!(queries[0] & BIT(0));
218                         has_build_id_query = !!(queries[0] & BIT(1));
219                 }
220
221                 if (has_package_id_query)
222                         prod_info_addr++;
223
224                 if (has_build_id_query) {
225                         ret = rmi_read_block(rmi_dev, prod_info_addr, queries,
226                                             3);
227                         if (ret) {
228                                 dev_err(&rmi_dev->dev,
229                                         "Failed to read product info: %d\n",
230                                         ret);
231                                 return ret;
232                         }
233
234                         props->firmware_id = queries[1] << 8 | queries[0];
235                         props->firmware_id += queries[2] * 65536;
236                 }
237         }
238
239         return 0;
240 }
241
242 char *rmi_f01_get_product_ID(struct rmi_function *fn)
243 {
244         struct f01_data *f01 = dev_get_drvdata(&fn->dev);
245
246         return f01->properties.product_id;
247 }
248
249 #ifdef CONFIG_OF
250 static int rmi_f01_of_probe(struct device *dev,
251                                 struct rmi_device_platform_data *pdata)
252 {
253         int retval;
254         u32 val;
255
256         retval = rmi_of_property_read_u32(dev,
257                         (u32 *)&pdata->power_management.nosleep,
258                         "syna,nosleep-mode", 1);
259         if (retval)
260                 return retval;
261
262         retval = rmi_of_property_read_u32(dev, &val,
263                         "syna,wakeup-threshold", 1);
264         if (retval)
265                 return retval;
266
267         pdata->power_management.wakeup_threshold = val;
268
269         retval = rmi_of_property_read_u32(dev, &val,
270                         "syna,doze-holdoff-ms", 1);
271         if (retval)
272                 return retval;
273
274         pdata->power_management.doze_holdoff = val * 100;
275
276         retval = rmi_of_property_read_u32(dev, &val,
277                         "syna,doze-interval-ms", 1);
278         if (retval)
279                 return retval;
280
281         pdata->power_management.doze_interval = val / 10;
282
283         return 0;
284 }
285 #else
286 static inline int rmi_f01_of_probe(struct device *dev,
287                                         struct rmi_device_platform_data *pdata)
288 {
289         return -ENODEV;
290 }
291 #endif
292
293 static int rmi_f01_probe(struct rmi_function *fn)
294 {
295         struct rmi_device *rmi_dev = fn->rmi_dev;
296         struct rmi_driver_data *driver_data = dev_get_drvdata(&rmi_dev->dev);
297         struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
298         struct f01_data *f01;
299         int error;
300         u16 ctrl_base_addr = fn->fd.control_base_addr;
301         u8 device_status;
302         u8 temp;
303
304         if (fn->dev.of_node) {
305                 error = rmi_f01_of_probe(&fn->dev, pdata);
306                 if (error)
307                         return error;
308         }
309
310         f01 = devm_kzalloc(&fn->dev, sizeof(struct f01_data), GFP_KERNEL);
311         if (!f01)
312                 return -ENOMEM;
313
314         f01->num_of_irq_regs = driver_data->num_of_irq_regs;
315
316         /*
317          * Set the configured bit and (optionally) other important stuff
318          * in the device control register.
319          */
320
321         error = rmi_read(rmi_dev, fn->fd.control_base_addr,
322                          &f01->device_control.ctrl0);
323         if (error) {
324                 dev_err(&fn->dev, "Failed to read F01 control: %d\n", error);
325                 return error;
326         }
327
328         switch (pdata->power_management.nosleep) {
329         case RMI_F01_NOSLEEP_DEFAULT:
330                 break;
331         case RMI_F01_NOSLEEP_OFF:
332                 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_NOSLEEP_BIT;
333                 break;
334         case RMI_F01_NOSLEEP_ON:
335                 f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
336                 break;
337         }
338
339         /*
340          * Sleep mode might be set as a hangover from a system crash or
341          * reboot without power cycle.  If so, clear it so the sensor
342          * is certain to function.
343          */
344         if ((f01->device_control.ctrl0 & RMI_F01_CTRL0_SLEEP_MODE_MASK) !=
345                         RMI_SLEEP_MODE_NORMAL) {
346                 dev_warn(&fn->dev,
347                          "WARNING: Non-zero sleep mode found. Clearing...\n");
348                 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
349         }
350
351         f01->device_control.ctrl0 |= RMI_F01_CTRL0_CONFIGURED_BIT;
352
353         error = rmi_write(rmi_dev, fn->fd.control_base_addr,
354                           f01->device_control.ctrl0);
355         if (error) {
356                 dev_err(&fn->dev, "Failed to write F01 control: %d\n", error);
357                 return error;
358         }
359
360         /* Dummy read in order to clear irqs */
361         error = rmi_read(rmi_dev, fn->fd.data_base_addr + 1, &temp);
362         if (error < 0) {
363                 dev_err(&fn->dev, "Failed to read Interrupt Status.\n");
364                 return error;
365         }
366
367         error = rmi_f01_read_properties(rmi_dev, fn->fd.query_base_addr,
368                                         &f01->properties);
369         if (error < 0) {
370                 dev_err(&fn->dev, "Failed to read F01 properties.\n");
371                 return error;
372         }
373
374         dev_info(&fn->dev, "found RMI device, manufacturer: %s, product: %s, fw id: %d\n",
375                  f01->properties.manufacturer_id == 1 ? "Synaptics" : "unknown",
376                  f01->properties.product_id, f01->properties.firmware_id);
377
378         /* Advance to interrupt control registers, then skip over them. */
379         ctrl_base_addr++;
380         ctrl_base_addr += f01->num_of_irq_regs;
381
382         /* read control register */
383         if (f01->properties.has_adjustable_doze) {
384                 f01->doze_interval_addr = ctrl_base_addr;
385                 ctrl_base_addr++;
386
387                 if (pdata->power_management.doze_interval) {
388                         f01->device_control.doze_interval =
389                                 pdata->power_management.doze_interval;
390                         error = rmi_write(rmi_dev, f01->doze_interval_addr,
391                                           f01->device_control.doze_interval);
392                         if (error) {
393                                 dev_err(&fn->dev,
394                                         "Failed to configure F01 doze interval register: %d\n",
395                                         error);
396                                 return error;
397                         }
398                 } else {
399                         error = rmi_read(rmi_dev, f01->doze_interval_addr,
400                                          &f01->device_control.doze_interval);
401                         if (error) {
402                                 dev_err(&fn->dev,
403                                         "Failed to read F01 doze interval register: %d\n",
404                                         error);
405                                 return error;
406                         }
407                 }
408
409                 f01->wakeup_threshold_addr = ctrl_base_addr;
410                 ctrl_base_addr++;
411
412                 if (pdata->power_management.wakeup_threshold) {
413                         f01->device_control.wakeup_threshold =
414                                 pdata->power_management.wakeup_threshold;
415                         error = rmi_write(rmi_dev, f01->wakeup_threshold_addr,
416                                           f01->device_control.wakeup_threshold);
417                         if (error) {
418                                 dev_err(&fn->dev,
419                                         "Failed to configure F01 wakeup threshold register: %d\n",
420                                         error);
421                                 return error;
422                         }
423                 } else {
424                         error = rmi_read(rmi_dev, f01->wakeup_threshold_addr,
425                                          &f01->device_control.wakeup_threshold);
426                         if (error < 0) {
427                                 dev_err(&fn->dev,
428                                         "Failed to read F01 wakeup threshold register: %d\n",
429                                         error);
430                                 return error;
431                         }
432                 }
433         }
434
435         if (f01->properties.has_lts)
436                 ctrl_base_addr++;
437
438         if (f01->properties.has_adjustable_doze_holdoff) {
439                 f01->doze_holdoff_addr = ctrl_base_addr;
440                 ctrl_base_addr++;
441
442                 if (pdata->power_management.doze_holdoff) {
443                         f01->device_control.doze_holdoff =
444                                 pdata->power_management.doze_holdoff;
445                         error = rmi_write(rmi_dev, f01->doze_holdoff_addr,
446                                           f01->device_control.doze_holdoff);
447                         if (error) {
448                                 dev_err(&fn->dev,
449                                         "Failed to configure F01 doze holdoff register: %d\n",
450                                         error);
451                                 return error;
452                         }
453                 } else {
454                         error = rmi_read(rmi_dev, f01->doze_holdoff_addr,
455                                          &f01->device_control.doze_holdoff);
456                         if (error) {
457                                 dev_err(&fn->dev,
458                                         "Failed to read F01 doze holdoff register: %d\n",
459                                         error);
460                                 return error;
461                         }
462                 }
463         }
464
465         error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
466         if (error < 0) {
467                 dev_err(&fn->dev,
468                         "Failed to read device status: %d\n", error);
469                 return error;
470         }
471
472         if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
473                 dev_err(&fn->dev,
474                         "Device was reset during configuration process, status: %#02x!\n",
475                         RMI_F01_STATUS_CODE(device_status));
476                 return -EINVAL;
477         }
478
479         dev_set_drvdata(&fn->dev, f01);
480
481         return 0;
482 }
483
484 static int rmi_f01_config(struct rmi_function *fn)
485 {
486         struct f01_data *f01 = dev_get_drvdata(&fn->dev);
487         int error;
488
489         error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
490                           f01->device_control.ctrl0);
491         if (error) {
492                 dev_err(&fn->dev,
493                         "Failed to write device_control register: %d\n", error);
494                 return error;
495         }
496
497         if (f01->properties.has_adjustable_doze) {
498                 error = rmi_write(fn->rmi_dev, f01->doze_interval_addr,
499                                   f01->device_control.doze_interval);
500                 if (error) {
501                         dev_err(&fn->dev,
502                                 "Failed to write doze interval: %d\n", error);
503                         return error;
504                 }
505
506                 error = rmi_write_block(fn->rmi_dev,
507                                          f01->wakeup_threshold_addr,
508                                          &f01->device_control.wakeup_threshold,
509                                          sizeof(u8));
510                 if (error) {
511                         dev_err(&fn->dev,
512                                 "Failed to write wakeup threshold: %d\n",
513                                 error);
514                         return error;
515                 }
516         }
517
518         if (f01->properties.has_adjustable_doze_holdoff) {
519                 error = rmi_write(fn->rmi_dev, f01->doze_holdoff_addr,
520                                   f01->device_control.doze_holdoff);
521                 if (error) {
522                         dev_err(&fn->dev,
523                                 "Failed to write doze holdoff: %d\n", error);
524                         return error;
525                 }
526         }
527
528         return 0;
529 }
530
531 static int rmi_f01_suspend(struct rmi_function *fn)
532 {
533         struct f01_data *f01 = dev_get_drvdata(&fn->dev);
534         int error;
535
536         f01->old_nosleep =
537                 f01->device_control.ctrl0 & RMI_F01_CTRL0_NOSLEEP_BIT;
538         f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_NOSLEEP_BIT;
539
540         f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
541         if (device_may_wakeup(fn->rmi_dev->xport->dev))
542                 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_RESERVED1;
543         else
544                 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_SENSOR_SLEEP;
545
546         error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
547                           f01->device_control.ctrl0);
548         if (error) {
549                 dev_err(&fn->dev, "Failed to write sleep mode: %d.\n", error);
550                 if (f01->old_nosleep)
551                         f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
552                 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
553                 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
554                 return error;
555         }
556
557         return 0;
558 }
559
560 static int rmi_f01_resume(struct rmi_function *fn)
561 {
562         struct f01_data *f01 = dev_get_drvdata(&fn->dev);
563         int error;
564
565         if (f01->old_nosleep)
566                 f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
567
568         f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
569         f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
570
571         error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
572                           f01->device_control.ctrl0);
573         if (error) {
574                 dev_err(&fn->dev,
575                         "Failed to restore normal operation: %d.\n", error);
576                 return error;
577         }
578
579         return 0;
580 }
581
582 static int rmi_f01_attention(struct rmi_function *fn,
583                              unsigned long *irq_bits)
584 {
585         struct rmi_device *rmi_dev = fn->rmi_dev;
586         int error;
587         u8 device_status;
588
589         error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
590         if (error) {
591                 dev_err(&fn->dev,
592                         "Failed to read device status: %d.\n", error);
593                 return error;
594         }
595
596         if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
597                 dev_warn(&fn->dev, "Device reset detected.\n");
598                 error = rmi_dev->driver->reset_handler(rmi_dev);
599                 if (error) {
600                         dev_err(&fn->dev, "Device reset failed: %d\n", error);
601                         return error;
602                 }
603         }
604
605         return 0;
606 }
607
608 struct rmi_function_handler rmi_f01_handler = {
609         .driver = {
610                 .name   = "rmi4_f01",
611                 /*
612                  * Do not allow user unbinding F01 as it is critical
613                  * function.
614                  */
615                 .suppress_bind_attrs = true,
616         },
617         .func           = 0x01,
618         .probe          = rmi_f01_probe,
619         .config         = rmi_f01_config,
620         .attention      = rmi_f01_attention,
621         .suspend        = rmi_f01_suspend,
622         .resume         = rmi_f01_resume,
623 };