Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2011-2016 Synaptics Incorporated
0004  * Copyright (c) 2011 Unixphere
0005  */
0006 
0007 #include <linux/kernel.h>
0008 #include <linux/rmi.h>
0009 #include <linux/slab.h>
0010 #include <linux/uaccess.h>
0011 #include <linux/of.h>
0012 #include <asm/unaligned.h>
0013 #include "rmi_driver.h"
0014 
0015 #define RMI_PRODUCT_ID_LENGTH    10
0016 #define RMI_PRODUCT_INFO_LENGTH   2
0017 
0018 #define RMI_DATE_CODE_LENGTH      3
0019 
0020 #define PRODUCT_ID_OFFSET 0x10
0021 #define PRODUCT_INFO_OFFSET 0x1E
0022 
0023 
0024 /* Force a firmware reset of the sensor */
0025 #define RMI_F01_CMD_DEVICE_RESET    1
0026 
0027 /* Various F01_RMI_QueryX bits */
0028 
0029 #define RMI_F01_QRY1_CUSTOM_MAP     BIT(0)
0030 #define RMI_F01_QRY1_NON_COMPLIANT  BIT(1)
0031 #define RMI_F01_QRY1_HAS_LTS        BIT(2)
0032 #define RMI_F01_QRY1_HAS_SENSOR_ID  BIT(3)
0033 #define RMI_F01_QRY1_HAS_CHARGER_INP    BIT(4)
0034 #define RMI_F01_QRY1_HAS_ADJ_DOZE   BIT(5)
0035 #define RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF  BIT(6)
0036 #define RMI_F01_QRY1_HAS_QUERY42    BIT(7)
0037 
0038 #define RMI_F01_QRY5_YEAR_MASK      0x1f
0039 #define RMI_F01_QRY6_MONTH_MASK     0x0f
0040 #define RMI_F01_QRY7_DAY_MASK       0x1f
0041 
0042 #define RMI_F01_QRY2_PRODINFO_MASK  0x7f
0043 
0044 #define RMI_F01_BASIC_QUERY_LEN     21 /* From Query 00 through 20 */
0045 
0046 struct f01_basic_properties {
0047     u8 manufacturer_id;
0048     bool has_lts;
0049     bool has_adjustable_doze;
0050     bool has_adjustable_doze_holdoff;
0051     char dom[11]; /* YYYY/MM/DD + '\0' */
0052     u8 product_id[RMI_PRODUCT_ID_LENGTH + 1];
0053     u16 productinfo;
0054     u32 firmware_id;
0055     u32 package_id;
0056 };
0057 
0058 /* F01 device status bits */
0059 
0060 /* Most recent device status event */
0061 #define RMI_F01_STATUS_CODE(status)     ((status) & 0x0f)
0062 /* The device has lost its configuration for some reason. */
0063 #define RMI_F01_STATUS_UNCONFIGURED(status) (!!((status) & 0x80))
0064 /* The device is in bootloader mode */
0065 #define RMI_F01_STATUS_BOOTLOADER(status)   ((status) & 0x40)
0066 
0067 /* Control register bits */
0068 
0069 /*
0070  * Sleep mode controls power management on the device and affects all
0071  * functions of the device.
0072  */
0073 #define RMI_F01_CTRL0_SLEEP_MODE_MASK   0x03
0074 
0075 #define RMI_SLEEP_MODE_NORMAL       0x00
0076 #define RMI_SLEEP_MODE_SENSOR_SLEEP 0x01
0077 #define RMI_SLEEP_MODE_RESERVED0    0x02
0078 #define RMI_SLEEP_MODE_RESERVED1    0x03
0079 
0080 /*
0081  * This bit disables whatever sleep mode may be selected by the sleep_mode
0082  * field and forces the device to run at full power without sleeping.
0083  */
0084 #define RMI_F01_CTRL0_NOSLEEP_BIT   BIT(2)
0085 
0086 /*
0087  * When this bit is set, the touch controller employs a noise-filtering
0088  * algorithm designed for use with a connected battery charger.
0089  */
0090 #define RMI_F01_CTRL0_CHARGER_BIT   BIT(5)
0091 
0092 /*
0093  * Sets the report rate for the device. The effect of this setting is
0094  * highly product dependent. Check the spec sheet for your particular
0095  * touch sensor.
0096  */
0097 #define RMI_F01_CTRL0_REPORTRATE_BIT    BIT(6)
0098 
0099 /*
0100  * Written by the host as an indicator that the device has been
0101  * successfully configured.
0102  */
0103 #define RMI_F01_CTRL0_CONFIGURED_BIT    BIT(7)
0104 
0105 /**
0106  * struct f01_device_control - controls basic sensor functions
0107  *
0108  * @ctrl0: see the bit definitions above.
0109  * @doze_interval: controls the interval between checks for finger presence
0110  *  when the touch sensor is in doze mode, in units of 10ms.
0111  * @wakeup_threshold: controls the capacitance threshold at which the touch
0112  *  sensor will decide to wake up from that low power state.
0113  * @doze_holdoff: controls how long the touch sensor waits after the last
0114  *  finger lifts before entering the doze state, in units of 100ms.
0115  */
0116 struct f01_device_control {
0117     u8 ctrl0;
0118     u8 doze_interval;
0119     u8 wakeup_threshold;
0120     u8 doze_holdoff;
0121 };
0122 
0123 struct f01_data {
0124     struct f01_basic_properties properties;
0125     struct f01_device_control device_control;
0126 
0127     u16 doze_interval_addr;
0128     u16 wakeup_threshold_addr;
0129     u16 doze_holdoff_addr;
0130 
0131     bool suspended;
0132     bool old_nosleep;
0133 
0134     unsigned int num_of_irq_regs;
0135 };
0136 
0137 static int rmi_f01_read_properties(struct rmi_device *rmi_dev,
0138                    u16 query_base_addr,
0139                    struct f01_basic_properties *props)
0140 {
0141     u8 queries[RMI_F01_BASIC_QUERY_LEN];
0142     int ret;
0143     int query_offset = query_base_addr;
0144     bool has_ds4_queries = false;
0145     bool has_query42 = false;
0146     bool has_sensor_id = false;
0147     bool has_package_id_query = false;
0148     bool has_build_id_query = false;
0149     u16 prod_info_addr;
0150     u8 ds4_query_len;
0151 
0152     ret = rmi_read_block(rmi_dev, query_offset,
0153                    queries, RMI_F01_BASIC_QUERY_LEN);
0154     if (ret) {
0155         dev_err(&rmi_dev->dev,
0156             "Failed to read device query registers: %d\n", ret);
0157         return ret;
0158     }
0159 
0160     prod_info_addr = query_offset + 17;
0161     query_offset += RMI_F01_BASIC_QUERY_LEN;
0162 
0163     /* Now parse what we got */
0164     props->manufacturer_id = queries[0];
0165 
0166     props->has_lts = queries[1] & RMI_F01_QRY1_HAS_LTS;
0167     props->has_adjustable_doze =
0168             queries[1] & RMI_F01_QRY1_HAS_ADJ_DOZE;
0169     props->has_adjustable_doze_holdoff =
0170             queries[1] & RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF;
0171     has_query42 = queries[1] & RMI_F01_QRY1_HAS_QUERY42;
0172     has_sensor_id = queries[1] & RMI_F01_QRY1_HAS_SENSOR_ID;
0173 
0174     snprintf(props->dom, sizeof(props->dom), "20%02d/%02d/%02d",
0175          queries[5] & RMI_F01_QRY5_YEAR_MASK,
0176          queries[6] & RMI_F01_QRY6_MONTH_MASK,
0177          queries[7] & RMI_F01_QRY7_DAY_MASK);
0178 
0179     memcpy(props->product_id, &queries[11],
0180         RMI_PRODUCT_ID_LENGTH);
0181     props->product_id[RMI_PRODUCT_ID_LENGTH] = '\0';
0182 
0183     props->productinfo =
0184             ((queries[2] & RMI_F01_QRY2_PRODINFO_MASK) << 7) |
0185             (queries[3] & RMI_F01_QRY2_PRODINFO_MASK);
0186 
0187     if (has_sensor_id)
0188         query_offset++;
0189 
0190     if (has_query42) {
0191         ret = rmi_read(rmi_dev, query_offset, queries);
0192         if (ret) {
0193             dev_err(&rmi_dev->dev,
0194                 "Failed to read query 42 register: %d\n", ret);
0195             return ret;
0196         }
0197 
0198         has_ds4_queries = !!(queries[0] & BIT(0));
0199         query_offset++;
0200     }
0201 
0202     if (has_ds4_queries) {
0203         ret = rmi_read(rmi_dev, query_offset, &ds4_query_len);
0204         if (ret) {
0205             dev_err(&rmi_dev->dev,
0206                 "Failed to read DS4 queries length: %d\n", ret);
0207             return ret;
0208         }
0209         query_offset++;
0210 
0211         if (ds4_query_len > 0) {
0212             ret = rmi_read(rmi_dev, query_offset, queries);
0213             if (ret) {
0214                 dev_err(&rmi_dev->dev,
0215                     "Failed to read DS4 queries: %d\n",
0216                     ret);
0217                 return ret;
0218             }
0219 
0220             has_package_id_query = !!(queries[0] & BIT(0));
0221             has_build_id_query = !!(queries[0] & BIT(1));
0222         }
0223 
0224         if (has_package_id_query) {
0225             ret = rmi_read_block(rmi_dev, prod_info_addr,
0226                          queries, sizeof(__le64));
0227             if (ret) {
0228                 dev_err(&rmi_dev->dev,
0229                     "Failed to read package info: %d\n",
0230                     ret);
0231                 return ret;
0232             }
0233 
0234             props->package_id = get_unaligned_le64(queries);
0235             prod_info_addr++;
0236         }
0237 
0238         if (has_build_id_query) {
0239             ret = rmi_read_block(rmi_dev, prod_info_addr, queries,
0240                         3);
0241             if (ret) {
0242                 dev_err(&rmi_dev->dev,
0243                     "Failed to read product info: %d\n",
0244                     ret);
0245                 return ret;
0246             }
0247 
0248             props->firmware_id = queries[1] << 8 | queries[0];
0249             props->firmware_id += queries[2] * 65536;
0250         }
0251     }
0252 
0253     return 0;
0254 }
0255 
0256 const char *rmi_f01_get_product_ID(struct rmi_function *fn)
0257 {
0258     struct f01_data *f01 = dev_get_drvdata(&fn->dev);
0259 
0260     return f01->properties.product_id;
0261 }
0262 
0263 static ssize_t rmi_driver_manufacturer_id_show(struct device *dev,
0264                            struct device_attribute *dattr,
0265                            char *buf)
0266 {
0267     struct rmi_driver_data *data = dev_get_drvdata(dev);
0268     struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
0269 
0270     return scnprintf(buf, PAGE_SIZE, "%d\n",
0271              f01->properties.manufacturer_id);
0272 }
0273 
0274 static DEVICE_ATTR(manufacturer_id, 0444,
0275            rmi_driver_manufacturer_id_show, NULL);
0276 
0277 static ssize_t rmi_driver_dom_show(struct device *dev,
0278                    struct device_attribute *dattr, char *buf)
0279 {
0280     struct rmi_driver_data *data = dev_get_drvdata(dev);
0281     struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
0282 
0283     return scnprintf(buf, PAGE_SIZE, "%s\n", f01->properties.dom);
0284 }
0285 
0286 static DEVICE_ATTR(date_of_manufacture, 0444, rmi_driver_dom_show, NULL);
0287 
0288 static ssize_t rmi_driver_product_id_show(struct device *dev,
0289                       struct device_attribute *dattr,
0290                       char *buf)
0291 {
0292     struct rmi_driver_data *data = dev_get_drvdata(dev);
0293     struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
0294 
0295     return scnprintf(buf, PAGE_SIZE, "%s\n", f01->properties.product_id);
0296 }
0297 
0298 static DEVICE_ATTR(product_id, 0444, rmi_driver_product_id_show, NULL);
0299 
0300 static ssize_t rmi_driver_firmware_id_show(struct device *dev,
0301                        struct device_attribute *dattr,
0302                        char *buf)
0303 {
0304     struct rmi_driver_data *data = dev_get_drvdata(dev);
0305     struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
0306 
0307     return scnprintf(buf, PAGE_SIZE, "%d\n", f01->properties.firmware_id);
0308 }
0309 
0310 static DEVICE_ATTR(firmware_id, 0444, rmi_driver_firmware_id_show, NULL);
0311 
0312 static ssize_t rmi_driver_package_id_show(struct device *dev,
0313                       struct device_attribute *dattr,
0314                       char *buf)
0315 {
0316     struct rmi_driver_data *data = dev_get_drvdata(dev);
0317     struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
0318 
0319     u32 package_id = f01->properties.package_id;
0320 
0321     return scnprintf(buf, PAGE_SIZE, "%04x.%04x\n",
0322              package_id & 0xffff, (package_id >> 16) & 0xffff);
0323 }
0324 
0325 static DEVICE_ATTR(package_id, 0444, rmi_driver_package_id_show, NULL);
0326 
0327 static struct attribute *rmi_f01_attrs[] = {
0328     &dev_attr_manufacturer_id.attr,
0329     &dev_attr_date_of_manufacture.attr,
0330     &dev_attr_product_id.attr,
0331     &dev_attr_firmware_id.attr,
0332     &dev_attr_package_id.attr,
0333     NULL
0334 };
0335 
0336 static const struct attribute_group rmi_f01_attr_group = {
0337     .attrs = rmi_f01_attrs,
0338 };
0339 
0340 #ifdef CONFIG_OF
0341 static int rmi_f01_of_probe(struct device *dev,
0342                 struct rmi_device_platform_data *pdata)
0343 {
0344     int retval;
0345     u32 val;
0346 
0347     retval = rmi_of_property_read_u32(dev,
0348             (u32 *)&pdata->power_management.nosleep,
0349             "syna,nosleep-mode", 1);
0350     if (retval)
0351         return retval;
0352 
0353     retval = rmi_of_property_read_u32(dev, &val,
0354             "syna,wakeup-threshold", 1);
0355     if (retval)
0356         return retval;
0357 
0358     pdata->power_management.wakeup_threshold = val;
0359 
0360     retval = rmi_of_property_read_u32(dev, &val,
0361             "syna,doze-holdoff-ms", 1);
0362     if (retval)
0363         return retval;
0364 
0365     pdata->power_management.doze_holdoff = val * 100;
0366 
0367     retval = rmi_of_property_read_u32(dev, &val,
0368             "syna,doze-interval-ms", 1);
0369     if (retval)
0370         return retval;
0371 
0372     pdata->power_management.doze_interval = val / 10;
0373 
0374     return 0;
0375 }
0376 #else
0377 static inline int rmi_f01_of_probe(struct device *dev,
0378                     struct rmi_device_platform_data *pdata)
0379 {
0380     return -ENODEV;
0381 }
0382 #endif
0383 
0384 static int rmi_f01_probe(struct rmi_function *fn)
0385 {
0386     struct rmi_device *rmi_dev = fn->rmi_dev;
0387     struct rmi_driver_data *driver_data = dev_get_drvdata(&rmi_dev->dev);
0388     struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
0389     struct f01_data *f01;
0390     int error;
0391     u16 ctrl_base_addr = fn->fd.control_base_addr;
0392     u8 device_status;
0393     u8 temp;
0394 
0395     if (fn->dev.of_node) {
0396         error = rmi_f01_of_probe(&fn->dev, pdata);
0397         if (error)
0398             return error;
0399     }
0400 
0401     f01 = devm_kzalloc(&fn->dev, sizeof(struct f01_data), GFP_KERNEL);
0402     if (!f01)
0403         return -ENOMEM;
0404 
0405     f01->num_of_irq_regs = driver_data->num_of_irq_regs;
0406 
0407     /*
0408      * Set the configured bit and (optionally) other important stuff
0409      * in the device control register.
0410      */
0411 
0412     error = rmi_read(rmi_dev, fn->fd.control_base_addr,
0413              &f01->device_control.ctrl0);
0414     if (error) {
0415         dev_err(&fn->dev, "Failed to read F01 control: %d\n", error);
0416         return error;
0417     }
0418 
0419     switch (pdata->power_management.nosleep) {
0420     case RMI_REG_STATE_DEFAULT:
0421         break;
0422     case RMI_REG_STATE_OFF:
0423         f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_NOSLEEP_BIT;
0424         break;
0425     case RMI_REG_STATE_ON:
0426         f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
0427         break;
0428     }
0429 
0430     /*
0431      * Sleep mode might be set as a hangover from a system crash or
0432      * reboot without power cycle.  If so, clear it so the sensor
0433      * is certain to function.
0434      */
0435     if ((f01->device_control.ctrl0 & RMI_F01_CTRL0_SLEEP_MODE_MASK) !=
0436             RMI_SLEEP_MODE_NORMAL) {
0437         dev_warn(&fn->dev,
0438              "WARNING: Non-zero sleep mode found. Clearing...\n");
0439         f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
0440     }
0441 
0442     f01->device_control.ctrl0 |= RMI_F01_CTRL0_CONFIGURED_BIT;
0443 
0444     error = rmi_write(rmi_dev, fn->fd.control_base_addr,
0445               f01->device_control.ctrl0);
0446     if (error) {
0447         dev_err(&fn->dev, "Failed to write F01 control: %d\n", error);
0448         return error;
0449     }
0450 
0451     /* Dummy read in order to clear irqs */
0452     error = rmi_read(rmi_dev, fn->fd.data_base_addr + 1, &temp);
0453     if (error < 0) {
0454         dev_err(&fn->dev, "Failed to read Interrupt Status.\n");
0455         return error;
0456     }
0457 
0458     error = rmi_f01_read_properties(rmi_dev, fn->fd.query_base_addr,
0459                     &f01->properties);
0460     if (error < 0) {
0461         dev_err(&fn->dev, "Failed to read F01 properties.\n");
0462         return error;
0463     }
0464 
0465     dev_info(&fn->dev, "found RMI device, manufacturer: %s, product: %s, fw id: %d\n",
0466          f01->properties.manufacturer_id == 1 ? "Synaptics" : "unknown",
0467          f01->properties.product_id, f01->properties.firmware_id);
0468 
0469     /* Advance to interrupt control registers, then skip over them. */
0470     ctrl_base_addr++;
0471     ctrl_base_addr += f01->num_of_irq_regs;
0472 
0473     /* read control register */
0474     if (f01->properties.has_adjustable_doze) {
0475         f01->doze_interval_addr = ctrl_base_addr;
0476         ctrl_base_addr++;
0477 
0478         if (pdata->power_management.doze_interval) {
0479             f01->device_control.doze_interval =
0480                 pdata->power_management.doze_interval;
0481             error = rmi_write(rmi_dev, f01->doze_interval_addr,
0482                       f01->device_control.doze_interval);
0483             if (error) {
0484                 dev_err(&fn->dev,
0485                     "Failed to configure F01 doze interval register: %d\n",
0486                     error);
0487                 return error;
0488             }
0489         } else {
0490             error = rmi_read(rmi_dev, f01->doze_interval_addr,
0491                      &f01->device_control.doze_interval);
0492             if (error) {
0493                 dev_err(&fn->dev,
0494                     "Failed to read F01 doze interval register: %d\n",
0495                     error);
0496                 return error;
0497             }
0498         }
0499 
0500         f01->wakeup_threshold_addr = ctrl_base_addr;
0501         ctrl_base_addr++;
0502 
0503         if (pdata->power_management.wakeup_threshold) {
0504             f01->device_control.wakeup_threshold =
0505                 pdata->power_management.wakeup_threshold;
0506             error = rmi_write(rmi_dev, f01->wakeup_threshold_addr,
0507                       f01->device_control.wakeup_threshold);
0508             if (error) {
0509                 dev_err(&fn->dev,
0510                     "Failed to configure F01 wakeup threshold register: %d\n",
0511                     error);
0512                 return error;
0513             }
0514         } else {
0515             error = rmi_read(rmi_dev, f01->wakeup_threshold_addr,
0516                      &f01->device_control.wakeup_threshold);
0517             if (error < 0) {
0518                 dev_err(&fn->dev,
0519                     "Failed to read F01 wakeup threshold register: %d\n",
0520                     error);
0521                 return error;
0522             }
0523         }
0524     }
0525 
0526     if (f01->properties.has_lts)
0527         ctrl_base_addr++;
0528 
0529     if (f01->properties.has_adjustable_doze_holdoff) {
0530         f01->doze_holdoff_addr = ctrl_base_addr;
0531         ctrl_base_addr++;
0532 
0533         if (pdata->power_management.doze_holdoff) {
0534             f01->device_control.doze_holdoff =
0535                 pdata->power_management.doze_holdoff;
0536             error = rmi_write(rmi_dev, f01->doze_holdoff_addr,
0537                       f01->device_control.doze_holdoff);
0538             if (error) {
0539                 dev_err(&fn->dev,
0540                     "Failed to configure F01 doze holdoff register: %d\n",
0541                     error);
0542                 return error;
0543             }
0544         } else {
0545             error = rmi_read(rmi_dev, f01->doze_holdoff_addr,
0546                      &f01->device_control.doze_holdoff);
0547             if (error) {
0548                 dev_err(&fn->dev,
0549                     "Failed to read F01 doze holdoff register: %d\n",
0550                     error);
0551                 return error;
0552             }
0553         }
0554     }
0555 
0556     error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
0557     if (error < 0) {
0558         dev_err(&fn->dev,
0559             "Failed to read device status: %d\n", error);
0560         return error;
0561     }
0562 
0563     if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
0564         dev_err(&fn->dev,
0565             "Device was reset during configuration process, status: %#02x!\n",
0566             RMI_F01_STATUS_CODE(device_status));
0567         return -EINVAL;
0568     }
0569 
0570     dev_set_drvdata(&fn->dev, f01);
0571 
0572     error = sysfs_create_group(&fn->rmi_dev->dev.kobj, &rmi_f01_attr_group);
0573     if (error)
0574         dev_warn(&fn->dev, "Failed to create sysfs group: %d\n", error);
0575 
0576     return 0;
0577 }
0578 
0579 static void rmi_f01_remove(struct rmi_function *fn)
0580 {
0581     /* Note that the bus device is used, not the F01 device */
0582     sysfs_remove_group(&fn->rmi_dev->dev.kobj, &rmi_f01_attr_group);
0583 }
0584 
0585 static int rmi_f01_config(struct rmi_function *fn)
0586 {
0587     struct f01_data *f01 = dev_get_drvdata(&fn->dev);
0588     int error;
0589 
0590     error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
0591               f01->device_control.ctrl0);
0592     if (error) {
0593         dev_err(&fn->dev,
0594             "Failed to write device_control register: %d\n", error);
0595         return error;
0596     }
0597 
0598     if (f01->properties.has_adjustable_doze) {
0599         error = rmi_write(fn->rmi_dev, f01->doze_interval_addr,
0600                   f01->device_control.doze_interval);
0601         if (error) {
0602             dev_err(&fn->dev,
0603                 "Failed to write doze interval: %d\n", error);
0604             return error;
0605         }
0606 
0607         error = rmi_write_block(fn->rmi_dev,
0608                      f01->wakeup_threshold_addr,
0609                      &f01->device_control.wakeup_threshold,
0610                      sizeof(u8));
0611         if (error) {
0612             dev_err(&fn->dev,
0613                 "Failed to write wakeup threshold: %d\n",
0614                 error);
0615             return error;
0616         }
0617     }
0618 
0619     if (f01->properties.has_adjustable_doze_holdoff) {
0620         error = rmi_write(fn->rmi_dev, f01->doze_holdoff_addr,
0621                   f01->device_control.doze_holdoff);
0622         if (error) {
0623             dev_err(&fn->dev,
0624                 "Failed to write doze holdoff: %d\n", error);
0625             return error;
0626         }
0627     }
0628 
0629     return 0;
0630 }
0631 
0632 static int rmi_f01_suspend(struct rmi_function *fn)
0633 {
0634     struct f01_data *f01 = dev_get_drvdata(&fn->dev);
0635     int error;
0636 
0637     f01->old_nosleep =
0638         f01->device_control.ctrl0 & RMI_F01_CTRL0_NOSLEEP_BIT;
0639     f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_NOSLEEP_BIT;
0640 
0641     f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
0642     if (device_may_wakeup(fn->rmi_dev->xport->dev))
0643         f01->device_control.ctrl0 |= RMI_SLEEP_MODE_RESERVED1;
0644     else
0645         f01->device_control.ctrl0 |= RMI_SLEEP_MODE_SENSOR_SLEEP;
0646 
0647     error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
0648               f01->device_control.ctrl0);
0649     if (error) {
0650         dev_err(&fn->dev, "Failed to write sleep mode: %d.\n", error);
0651         if (f01->old_nosleep)
0652             f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
0653         f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
0654         f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
0655         return error;
0656     }
0657 
0658     return 0;
0659 }
0660 
0661 static int rmi_f01_resume(struct rmi_function *fn)
0662 {
0663     struct f01_data *f01 = dev_get_drvdata(&fn->dev);
0664     int error;
0665 
0666     if (f01->old_nosleep)
0667         f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
0668 
0669     f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
0670     f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
0671 
0672     error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
0673               f01->device_control.ctrl0);
0674     if (error) {
0675         dev_err(&fn->dev,
0676             "Failed to restore normal operation: %d.\n", error);
0677         return error;
0678     }
0679 
0680     return 0;
0681 }
0682 
0683 static irqreturn_t rmi_f01_attention(int irq, void *ctx)
0684 {
0685     struct rmi_function *fn = ctx;
0686     struct rmi_device *rmi_dev = fn->rmi_dev;
0687     int error;
0688     u8 device_status;
0689 
0690     error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
0691     if (error) {
0692         dev_err(&fn->dev,
0693             "Failed to read device status: %d.\n", error);
0694         return IRQ_RETVAL(error);
0695     }
0696 
0697     if (RMI_F01_STATUS_BOOTLOADER(device_status))
0698         dev_warn(&fn->dev,
0699              "Device in bootloader mode, please update firmware\n");
0700 
0701     if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
0702         dev_warn(&fn->dev, "Device reset detected.\n");
0703         error = rmi_dev->driver->reset_handler(rmi_dev);
0704         if (error) {
0705             dev_err(&fn->dev, "Device reset failed: %d\n", error);
0706             return IRQ_RETVAL(error);
0707         }
0708     }
0709 
0710     return IRQ_HANDLED;
0711 }
0712 
0713 struct rmi_function_handler rmi_f01_handler = {
0714     .driver = {
0715         .name   = "rmi4_f01",
0716         /*
0717          * Do not allow user unbinding F01 as it is critical
0718          * function.
0719          */
0720         .suppress_bind_attrs = true,
0721     },
0722     .func       = 0x01,
0723     .probe      = rmi_f01_probe,
0724     .remove     = rmi_f01_remove,
0725     .config     = rmi_f01_config,
0726     .attention  = rmi_f01_attention,
0727     .suspend    = rmi_f01_suspend,
0728     .resume     = rmi_f01_resume,
0729 };