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