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