2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
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.
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>
16 #include <asm/unaligned.h>
17 #include "rmi_driver.h"
19 #define RMI_PRODUCT_ID_LENGTH 10
20 #define RMI_PRODUCT_INFO_LENGTH 2
22 #define RMI_DATE_CODE_LENGTH 3
24 #define PRODUCT_ID_OFFSET 0x10
25 #define PRODUCT_INFO_OFFSET 0x1E
28 /* Force a firmware reset of the sensor */
29 #define RMI_F01_CMD_DEVICE_RESET 1
31 /* Various F01_RMI_QueryX bits */
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)
42 #define RMI_F01_QRY5_YEAR_MASK 0x1f
43 #define RMI_F01_QRY6_MONTH_MASK 0x0f
44 #define RMI_F01_QRY7_DAY_MASK 0x1f
46 #define RMI_F01_QRY2_PRODINFO_MASK 0x7f
48 #define RMI_F01_BASIC_QUERY_LEN 21 /* From Query 00 through 20 */
50 struct f01_basic_properties {
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];
62 /* F01 device status bits */
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)
71 /* Control register bits */
74 * Sleep mode controls power management on the device and affects all
75 * functions of the device.
77 #define RMI_F01_CTRL0_SLEEP_MODE_MASK 0x03
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
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.
88 #define RMI_F01_CTRL0_NOSLEEP_BIT BIT(2)
91 * When this bit is set, the touch controller employs a noise-filtering
92 * algorithm designed for use with a connected battery charger.
94 #define RMI_F01_CTRL0_CHARGER_BIT BIT(5)
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
101 #define RMI_F01_CTRL0_REPORTRATE_BIT BIT(6)
104 * Written by the host as an indicator that the device has been
105 * successfully configured.
107 #define RMI_F01_CTRL0_CONFIGURED_BIT BIT(7)
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.
118 struct f01_device_control {
126 struct f01_basic_properties properties;
127 struct f01_device_control device_control;
129 u16 doze_interval_addr;
130 u16 wakeup_threshold_addr;
131 u16 doze_holdoff_addr;
136 unsigned int num_of_irq_regs;
139 static int rmi_f01_read_properties(struct rmi_device *rmi_dev,
141 struct f01_basic_properties *props)
143 u8 queries[RMI_F01_BASIC_QUERY_LEN];
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;
154 ret = rmi_read_block(rmi_dev, query_offset,
155 queries, RMI_F01_BASIC_QUERY_LEN);
157 dev_err(&rmi_dev->dev,
158 "Failed to read device query registers: %d\n", ret);
162 prod_info_addr = query_offset + 17;
163 query_offset += RMI_F01_BASIC_QUERY_LEN;
165 /* Now parse what we got */
166 props->manufacturer_id = queries[0];
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;
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);
181 memcpy(props->product_id, &queries[11],
182 RMI_PRODUCT_ID_LENGTH);
183 props->product_id[RMI_PRODUCT_ID_LENGTH] = '\0';
186 ((queries[2] & RMI_F01_QRY2_PRODINFO_MASK) << 7) |
187 (queries[3] & RMI_F01_QRY2_PRODINFO_MASK);
193 ret = rmi_read(rmi_dev, query_offset, queries);
195 dev_err(&rmi_dev->dev,
196 "Failed to read query 42 register: %d\n", ret);
200 has_ds4_queries = !!(queries[0] & BIT(0));
204 if (has_ds4_queries) {
205 ret = rmi_read(rmi_dev, query_offset, &ds4_query_len);
207 dev_err(&rmi_dev->dev,
208 "Failed to read DS4 queries length: %d\n", ret);
213 if (ds4_query_len > 0) {
214 ret = rmi_read(rmi_dev, query_offset, queries);
216 dev_err(&rmi_dev->dev,
217 "Failed to read DS4 queries: %d\n",
222 has_package_id_query = !!(queries[0] & BIT(0));
223 has_build_id_query = !!(queries[0] & BIT(1));
226 if (has_package_id_query) {
227 ret = rmi_read_block(rmi_dev, prod_info_addr,
228 queries, sizeof(__le64));
230 dev_err(&rmi_dev->dev,
231 "Failed to read package info: %d\n",
236 props->package_id = get_unaligned_le64(queries);
240 if (has_build_id_query) {
241 ret = rmi_read_block(rmi_dev, prod_info_addr, queries,
244 dev_err(&rmi_dev->dev,
245 "Failed to read product info: %d\n",
250 props->firmware_id = queries[1] << 8 | queries[0];
251 props->firmware_id += queries[2] * 65536;
258 const char *rmi_f01_get_product_ID(struct rmi_function *fn)
260 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
262 return f01->properties.product_id;
265 static ssize_t rmi_driver_manufacturer_id_show(struct device *dev,
266 struct device_attribute *dattr,
269 struct rmi_driver_data *data = dev_get_drvdata(dev);
270 struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
272 return scnprintf(buf, PAGE_SIZE, "%d\n",
273 f01->properties.manufacturer_id);
276 static DEVICE_ATTR(manufacturer_id, 0444,
277 rmi_driver_manufacturer_id_show, NULL);
279 static ssize_t rmi_driver_dom_show(struct device *dev,
280 struct device_attribute *dattr, char *buf)
282 struct rmi_driver_data *data = dev_get_drvdata(dev);
283 struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
285 return scnprintf(buf, PAGE_SIZE, "%s\n", f01->properties.dom);
288 static DEVICE_ATTR(date_of_manufacture, 0444, rmi_driver_dom_show, NULL);
290 static ssize_t rmi_driver_product_id_show(struct device *dev,
291 struct device_attribute *dattr,
294 struct rmi_driver_data *data = dev_get_drvdata(dev);
295 struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
297 return scnprintf(buf, PAGE_SIZE, "%s\n", f01->properties.product_id);
300 static DEVICE_ATTR(product_id, 0444, rmi_driver_product_id_show, NULL);
302 static ssize_t rmi_driver_firmware_id_show(struct device *dev,
303 struct device_attribute *dattr,
306 struct rmi_driver_data *data = dev_get_drvdata(dev);
307 struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
309 return scnprintf(buf, PAGE_SIZE, "%d\n", f01->properties.firmware_id);
312 static DEVICE_ATTR(firmware_id, 0444, rmi_driver_firmware_id_show, NULL);
314 static ssize_t rmi_driver_package_id_show(struct device *dev,
315 struct device_attribute *dattr,
318 struct rmi_driver_data *data = dev_get_drvdata(dev);
319 struct f01_data *f01 = dev_get_drvdata(&data->f01_container->dev);
321 u32 package_id = f01->properties.package_id;
323 return scnprintf(buf, PAGE_SIZE, "%04x.%04x\n",
324 package_id & 0xffff, (package_id >> 16) & 0xffff);
327 static DEVICE_ATTR(package_id, 0444, rmi_driver_package_id_show, NULL);
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,
338 static struct attribute_group rmi_f01_attr_group = {
339 .attrs = rmi_f01_attrs,
343 static int rmi_f01_of_probe(struct device *dev,
344 struct rmi_device_platform_data *pdata)
349 retval = rmi_of_property_read_u32(dev,
350 (u32 *)&pdata->power_management.nosleep,
351 "syna,nosleep-mode", 1);
355 retval = rmi_of_property_read_u32(dev, &val,
356 "syna,wakeup-threshold", 1);
360 pdata->power_management.wakeup_threshold = val;
362 retval = rmi_of_property_read_u32(dev, &val,
363 "syna,doze-holdoff-ms", 1);
367 pdata->power_management.doze_holdoff = val * 100;
369 retval = rmi_of_property_read_u32(dev, &val,
370 "syna,doze-interval-ms", 1);
374 pdata->power_management.doze_interval = val / 10;
379 static inline int rmi_f01_of_probe(struct device *dev,
380 struct rmi_device_platform_data *pdata)
386 static int rmi_f01_probe(struct rmi_function *fn)
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;
393 u16 ctrl_base_addr = fn->fd.control_base_addr;
397 if (fn->dev.of_node) {
398 error = rmi_f01_of_probe(&fn->dev, pdata);
403 f01 = devm_kzalloc(&fn->dev, sizeof(struct f01_data), GFP_KERNEL);
407 f01->num_of_irq_regs = driver_data->num_of_irq_regs;
410 * Set the configured bit and (optionally) other important stuff
411 * in the device control register.
414 error = rmi_read(rmi_dev, fn->fd.control_base_addr,
415 &f01->device_control.ctrl0);
417 dev_err(&fn->dev, "Failed to read F01 control: %d\n", error);
421 switch (pdata->power_management.nosleep) {
422 case RMI_REG_STATE_DEFAULT:
424 case RMI_REG_STATE_OFF:
425 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_NOSLEEP_BIT;
427 case RMI_REG_STATE_ON:
428 f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
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.
437 if ((f01->device_control.ctrl0 & RMI_F01_CTRL0_SLEEP_MODE_MASK) !=
438 RMI_SLEEP_MODE_NORMAL) {
440 "WARNING: Non-zero sleep mode found. Clearing...\n");
441 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
444 f01->device_control.ctrl0 |= RMI_F01_CTRL0_CONFIGURED_BIT;
446 error = rmi_write(rmi_dev, fn->fd.control_base_addr,
447 f01->device_control.ctrl0);
449 dev_err(&fn->dev, "Failed to write F01 control: %d\n", error);
453 /* Dummy read in order to clear irqs */
454 error = rmi_read(rmi_dev, fn->fd.data_base_addr + 1, &temp);
456 dev_err(&fn->dev, "Failed to read Interrupt Status.\n");
460 error = rmi_f01_read_properties(rmi_dev, fn->fd.query_base_addr,
463 dev_err(&fn->dev, "Failed to read F01 properties.\n");
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);
471 /* Advance to interrupt control registers, then skip over them. */
473 ctrl_base_addr += f01->num_of_irq_regs;
475 /* read control register */
476 if (f01->properties.has_adjustable_doze) {
477 f01->doze_interval_addr = ctrl_base_addr;
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);
487 "Failed to configure F01 doze interval register: %d\n",
492 error = rmi_read(rmi_dev, f01->doze_interval_addr,
493 &f01->device_control.doze_interval);
496 "Failed to read F01 doze interval register: %d\n",
502 f01->wakeup_threshold_addr = ctrl_base_addr;
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);
512 "Failed to configure F01 wakeup threshold register: %d\n",
517 error = rmi_read(rmi_dev, f01->wakeup_threshold_addr,
518 &f01->device_control.wakeup_threshold);
521 "Failed to read F01 wakeup threshold register: %d\n",
528 if (f01->properties.has_lts)
531 if (f01->properties.has_adjustable_doze_holdoff) {
532 f01->doze_holdoff_addr = ctrl_base_addr;
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);
542 "Failed to configure F01 doze holdoff register: %d\n",
547 error = rmi_read(rmi_dev, f01->doze_holdoff_addr,
548 &f01->device_control.doze_holdoff);
551 "Failed to read F01 doze holdoff register: %d\n",
558 error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
561 "Failed to read device status: %d\n", error);
565 if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
567 "Device was reset during configuration process, status: %#02x!\n",
568 RMI_F01_STATUS_CODE(device_status));
572 dev_set_drvdata(&fn->dev, f01);
574 error = sysfs_create_group(&fn->rmi_dev->dev.kobj, &rmi_f01_attr_group);
576 dev_warn(&fn->dev, "Failed to create sysfs group: %d\n", error);
581 static void rmi_f01_remove(struct rmi_function *fn)
583 sysfs_remove_group(&fn->rmi_dev->dev.kobj, &rmi_f01_attr_group);
586 static int rmi_f01_config(struct rmi_function *fn)
588 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
591 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
592 f01->device_control.ctrl0);
595 "Failed to write device_control register: %d\n", error);
599 if (f01->properties.has_adjustable_doze) {
600 error = rmi_write(fn->rmi_dev, f01->doze_interval_addr,
601 f01->device_control.doze_interval);
604 "Failed to write doze interval: %d\n", error);
608 error = rmi_write_block(fn->rmi_dev,
609 f01->wakeup_threshold_addr,
610 &f01->device_control.wakeup_threshold,
614 "Failed to write wakeup threshold: %d\n",
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);
625 "Failed to write doze holdoff: %d\n", error);
633 static int rmi_f01_suspend(struct rmi_function *fn)
635 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
639 f01->device_control.ctrl0 & RMI_F01_CTRL0_NOSLEEP_BIT;
640 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_NOSLEEP_BIT;
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;
646 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_SENSOR_SLEEP;
648 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
649 f01->device_control.ctrl0);
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;
662 static int rmi_f01_resume(struct rmi_function *fn)
664 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
667 if (f01->old_nosleep)
668 f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
670 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
671 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
673 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
674 f01->device_control.ctrl0);
677 "Failed to restore normal operation: %d.\n", error);
684 static int rmi_f01_attention(struct rmi_function *fn,
685 unsigned long *irq_bits)
687 struct rmi_device *rmi_dev = fn->rmi_dev;
691 error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
694 "Failed to read device status: %d.\n", error);
698 if (RMI_F01_STATUS_BOOTLOADER(device_status))
700 "Device in bootloader mode, please update firmware\n");
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);
706 dev_err(&fn->dev, "Device reset failed: %d\n", error);
714 struct rmi_function_handler rmi_f01_handler = {
718 * Do not allow user unbinding F01 as it is critical
721 .suppress_bind_attrs = true,
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,