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