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