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/rmi.h>
12 #include <linux/slab.h>
13 #include <linux/uaccess.h>
15 #include <asm/unaligned.h>
16 #include "rmi_driver.h"
18 #define RMI_PRODUCT_ID_LENGTH 10
19 #define RMI_PRODUCT_INFO_LENGTH 2
21 #define RMI_DATE_CODE_LENGTH 3
23 #define PRODUCT_ID_OFFSET 0x10
24 #define PRODUCT_INFO_OFFSET 0x1E
27 /* Force a firmware reset of the sensor */
28 #define RMI_F01_CMD_DEVICE_RESET 1
30 /* Various F01_RMI_QueryX bits */
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)
41 #define RMI_F01_QRY5_YEAR_MASK 0x1f
42 #define RMI_F01_QRY6_MONTH_MASK 0x0f
43 #define RMI_F01_QRY7_DAY_MASK 0x1f
45 #define RMI_F01_QRY2_PRODINFO_MASK 0x7f
47 #define RMI_F01_BASIC_QUERY_LEN 21 /* From Query 00 through 20 */
49 struct f01_basic_properties
{
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];
61 /* F01 device status bits */
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)
70 /* Control register bits */
73 * Sleep mode controls power management on the device and affects all
74 * functions of the device.
76 #define RMI_F01_CTRL0_SLEEP_MODE_MASK 0x03
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
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.
87 #define RMI_F01_CTRL0_NOSLEEP_BIT BIT(2)
90 * When this bit is set, the touch controller employs a noise-filtering
91 * algorithm designed for use with a connected battery charger.
93 #define RMI_F01_CTRL0_CHARGER_BIT BIT(5)
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
100 #define RMI_F01_CTRL0_REPORTRATE_BIT BIT(6)
103 * Written by the host as an indicator that the device has been
104 * successfully configured.
106 #define RMI_F01_CTRL0_CONFIGURED_BIT BIT(7)
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.
117 struct f01_device_control
{
125 struct f01_basic_properties properties
;
126 struct f01_device_control device_control
;
128 u16 doze_interval_addr
;
129 u16 wakeup_threshold_addr
;
130 u16 doze_holdoff_addr
;
135 unsigned int num_of_irq_regs
;
138 static int rmi_f01_read_properties(struct rmi_device
*rmi_dev
,
140 struct f01_basic_properties
*props
)
142 u8 queries
[RMI_F01_BASIC_QUERY_LEN
];
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;
153 ret
= rmi_read_block(rmi_dev
, query_offset
,
154 queries
, RMI_F01_BASIC_QUERY_LEN
);
156 dev_err(&rmi_dev
->dev
,
157 "Failed to read device query registers: %d\n", ret
);
161 prod_info_addr
= query_offset
+ 17;
162 query_offset
+= RMI_F01_BASIC_QUERY_LEN
;
164 /* Now parse what we got */
165 props
->manufacturer_id
= queries
[0];
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
;
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
);
180 memcpy(props
->product_id
, &queries
[11],
181 RMI_PRODUCT_ID_LENGTH
);
182 props
->product_id
[RMI_PRODUCT_ID_LENGTH
] = '\0';
185 ((queries
[2] & RMI_F01_QRY2_PRODINFO_MASK
) << 7) |
186 (queries
[3] & RMI_F01_QRY2_PRODINFO_MASK
);
192 ret
= rmi_read(rmi_dev
, query_offset
, queries
);
194 dev_err(&rmi_dev
->dev
,
195 "Failed to read query 42 register: %d\n", ret
);
199 has_ds4_queries
= !!(queries
[0] & BIT(0));
203 if (has_ds4_queries
) {
204 ret
= rmi_read(rmi_dev
, query_offset
, &ds4_query_len
);
206 dev_err(&rmi_dev
->dev
,
207 "Failed to read DS4 queries length: %d\n", ret
);
212 if (ds4_query_len
> 0) {
213 ret
= rmi_read(rmi_dev
, query_offset
, queries
);
215 dev_err(&rmi_dev
->dev
,
216 "Failed to read DS4 queries: %d\n",
221 has_package_id_query
= !!(queries
[0] & BIT(0));
222 has_build_id_query
= !!(queries
[0] & BIT(1));
225 if (has_package_id_query
) {
226 ret
= rmi_read_block(rmi_dev
, prod_info_addr
,
227 queries
, sizeof(__le64
));
229 dev_err(&rmi_dev
->dev
,
230 "Failed to read package info: %d\n",
235 props
->package_id
= get_unaligned_le64(queries
);
239 if (has_build_id_query
) {
240 ret
= rmi_read_block(rmi_dev
, prod_info_addr
, queries
,
243 dev_err(&rmi_dev
->dev
,
244 "Failed to read product info: %d\n",
249 props
->firmware_id
= queries
[1] << 8 | queries
[0];
250 props
->firmware_id
+= queries
[2] * 65536;
257 const char *rmi_f01_get_product_ID(struct rmi_function
*fn
)
259 struct f01_data
*f01
= dev_get_drvdata(&fn
->dev
);
261 return f01
->properties
.product_id
;
264 static ssize_t
rmi_driver_manufacturer_id_show(struct device
*dev
,
265 struct device_attribute
*dattr
,
268 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
269 struct f01_data
*f01
= dev_get_drvdata(&data
->f01_container
->dev
);
271 return scnprintf(buf
, PAGE_SIZE
, "%d\n",
272 f01
->properties
.manufacturer_id
);
275 static DEVICE_ATTR(manufacturer_id
, 0444,
276 rmi_driver_manufacturer_id_show
, NULL
);
278 static ssize_t
rmi_driver_dom_show(struct device
*dev
,
279 struct device_attribute
*dattr
, char *buf
)
281 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
282 struct f01_data
*f01
= dev_get_drvdata(&data
->f01_container
->dev
);
284 return scnprintf(buf
, PAGE_SIZE
, "%s\n", f01
->properties
.dom
);
287 static DEVICE_ATTR(date_of_manufacture
, 0444, rmi_driver_dom_show
, NULL
);
289 static ssize_t
rmi_driver_product_id_show(struct device
*dev
,
290 struct device_attribute
*dattr
,
293 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
294 struct f01_data
*f01
= dev_get_drvdata(&data
->f01_container
->dev
);
296 return scnprintf(buf
, PAGE_SIZE
, "%s\n", f01
->properties
.product_id
);
299 static DEVICE_ATTR(product_id
, 0444, rmi_driver_product_id_show
, NULL
);
301 static ssize_t
rmi_driver_firmware_id_show(struct device
*dev
,
302 struct device_attribute
*dattr
,
305 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
306 struct f01_data
*f01
= dev_get_drvdata(&data
->f01_container
->dev
);
308 return scnprintf(buf
, PAGE_SIZE
, "%d\n", f01
->properties
.firmware_id
);
311 static DEVICE_ATTR(firmware_id
, 0444, rmi_driver_firmware_id_show
, NULL
);
313 static ssize_t
rmi_driver_package_id_show(struct device
*dev
,
314 struct device_attribute
*dattr
,
317 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
318 struct f01_data
*f01
= dev_get_drvdata(&data
->f01_container
->dev
);
320 u32 package_id
= f01
->properties
.package_id
;
322 return scnprintf(buf
, PAGE_SIZE
, "%04x.%04x\n",
323 package_id
& 0xffff, (package_id
>> 16) & 0xffff);
326 static DEVICE_ATTR(package_id
, 0444, rmi_driver_package_id_show
, NULL
);
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
,
337 static struct attribute_group rmi_f01_attr_group
= {
338 .attrs
= rmi_f01_attrs
,
342 static int rmi_f01_of_probe(struct device
*dev
,
343 struct rmi_device_platform_data
*pdata
)
348 retval
= rmi_of_property_read_u32(dev
,
349 (u32
*)&pdata
->power_management
.nosleep
,
350 "syna,nosleep-mode", 1);
354 retval
= rmi_of_property_read_u32(dev
, &val
,
355 "syna,wakeup-threshold", 1);
359 pdata
->power_management
.wakeup_threshold
= val
;
361 retval
= rmi_of_property_read_u32(dev
, &val
,
362 "syna,doze-holdoff-ms", 1);
366 pdata
->power_management
.doze_holdoff
= val
* 100;
368 retval
= rmi_of_property_read_u32(dev
, &val
,
369 "syna,doze-interval-ms", 1);
373 pdata
->power_management
.doze_interval
= val
/ 10;
378 static inline int rmi_f01_of_probe(struct device
*dev
,
379 struct rmi_device_platform_data
*pdata
)
385 static int rmi_f01_probe(struct rmi_function
*fn
)
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
;
392 u16 ctrl_base_addr
= fn
->fd
.control_base_addr
;
396 if (fn
->dev
.of_node
) {
397 error
= rmi_f01_of_probe(&fn
->dev
, pdata
);
402 f01
= devm_kzalloc(&fn
->dev
, sizeof(struct f01_data
), GFP_KERNEL
);
406 f01
->num_of_irq_regs
= driver_data
->num_of_irq_regs
;
409 * Set the configured bit and (optionally) other important stuff
410 * in the device control register.
413 error
= rmi_read(rmi_dev
, fn
->fd
.control_base_addr
,
414 &f01
->device_control
.ctrl0
);
416 dev_err(&fn
->dev
, "Failed to read F01 control: %d\n", error
);
420 switch (pdata
->power_management
.nosleep
) {
421 case RMI_REG_STATE_DEFAULT
:
423 case RMI_REG_STATE_OFF
:
424 f01
->device_control
.ctrl0
&= ~RMI_F01_CTRL0_NOSLEEP_BIT
;
426 case RMI_REG_STATE_ON
:
427 f01
->device_control
.ctrl0
|= RMI_F01_CTRL0_NOSLEEP_BIT
;
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.
436 if ((f01
->device_control
.ctrl0
& RMI_F01_CTRL0_SLEEP_MODE_MASK
) !=
437 RMI_SLEEP_MODE_NORMAL
) {
439 "WARNING: Non-zero sleep mode found. Clearing...\n");
440 f01
->device_control
.ctrl0
&= ~RMI_F01_CTRL0_SLEEP_MODE_MASK
;
443 f01
->device_control
.ctrl0
|= RMI_F01_CTRL0_CONFIGURED_BIT
;
445 error
= rmi_write(rmi_dev
, fn
->fd
.control_base_addr
,
446 f01
->device_control
.ctrl0
);
448 dev_err(&fn
->dev
, "Failed to write F01 control: %d\n", error
);
452 /* Dummy read in order to clear irqs */
453 error
= rmi_read(rmi_dev
, fn
->fd
.data_base_addr
+ 1, &temp
);
455 dev_err(&fn
->dev
, "Failed to read Interrupt Status.\n");
459 error
= rmi_f01_read_properties(rmi_dev
, fn
->fd
.query_base_addr
,
462 dev_err(&fn
->dev
, "Failed to read F01 properties.\n");
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
);
470 /* Advance to interrupt control registers, then skip over them. */
472 ctrl_base_addr
+= f01
->num_of_irq_regs
;
474 /* read control register */
475 if (f01
->properties
.has_adjustable_doze
) {
476 f01
->doze_interval_addr
= ctrl_base_addr
;
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
);
486 "Failed to configure F01 doze interval register: %d\n",
491 error
= rmi_read(rmi_dev
, f01
->doze_interval_addr
,
492 &f01
->device_control
.doze_interval
);
495 "Failed to read F01 doze interval register: %d\n",
501 f01
->wakeup_threshold_addr
= ctrl_base_addr
;
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
);
511 "Failed to configure F01 wakeup threshold register: %d\n",
516 error
= rmi_read(rmi_dev
, f01
->wakeup_threshold_addr
,
517 &f01
->device_control
.wakeup_threshold
);
520 "Failed to read F01 wakeup threshold register: %d\n",
527 if (f01
->properties
.has_lts
)
530 if (f01
->properties
.has_adjustable_doze_holdoff
) {
531 f01
->doze_holdoff_addr
= ctrl_base_addr
;
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
);
541 "Failed to configure F01 doze holdoff register: %d\n",
546 error
= rmi_read(rmi_dev
, f01
->doze_holdoff_addr
,
547 &f01
->device_control
.doze_holdoff
);
550 "Failed to read F01 doze holdoff register: %d\n",
557 error
= rmi_read(rmi_dev
, fn
->fd
.data_base_addr
, &device_status
);
560 "Failed to read device status: %d\n", error
);
564 if (RMI_F01_STATUS_UNCONFIGURED(device_status
)) {
566 "Device was reset during configuration process, status: %#02x!\n",
567 RMI_F01_STATUS_CODE(device_status
));
571 dev_set_drvdata(&fn
->dev
, f01
);
573 error
= sysfs_create_group(&fn
->rmi_dev
->dev
.kobj
, &rmi_f01_attr_group
);
575 dev_warn(&fn
->dev
, "Failed to create sysfs group: %d\n", error
);
580 static void rmi_f01_remove(struct rmi_function
*fn
)
582 sysfs_remove_group(&fn
->rmi_dev
->dev
.kobj
, &rmi_f01_attr_group
);
585 static int rmi_f01_config(struct rmi_function
*fn
)
587 struct f01_data
*f01
= dev_get_drvdata(&fn
->dev
);
590 error
= rmi_write(fn
->rmi_dev
, fn
->fd
.control_base_addr
,
591 f01
->device_control
.ctrl0
);
594 "Failed to write device_control register: %d\n", error
);
598 if (f01
->properties
.has_adjustable_doze
) {
599 error
= rmi_write(fn
->rmi_dev
, f01
->doze_interval_addr
,
600 f01
->device_control
.doze_interval
);
603 "Failed to write doze interval: %d\n", error
);
607 error
= rmi_write_block(fn
->rmi_dev
,
608 f01
->wakeup_threshold_addr
,
609 &f01
->device_control
.wakeup_threshold
,
613 "Failed to write wakeup threshold: %d\n",
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
);
624 "Failed to write doze holdoff: %d\n", error
);
632 static int rmi_f01_suspend(struct rmi_function
*fn
)
634 struct f01_data
*f01
= dev_get_drvdata(&fn
->dev
);
638 f01
->device_control
.ctrl0
& RMI_F01_CTRL0_NOSLEEP_BIT
;
639 f01
->device_control
.ctrl0
&= ~RMI_F01_CTRL0_NOSLEEP_BIT
;
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
;
645 f01
->device_control
.ctrl0
|= RMI_SLEEP_MODE_SENSOR_SLEEP
;
647 error
= rmi_write(fn
->rmi_dev
, fn
->fd
.control_base_addr
,
648 f01
->device_control
.ctrl0
);
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
;
661 static int rmi_f01_resume(struct rmi_function
*fn
)
663 struct f01_data
*f01
= dev_get_drvdata(&fn
->dev
);
666 if (f01
->old_nosleep
)
667 f01
->device_control
.ctrl0
|= RMI_F01_CTRL0_NOSLEEP_BIT
;
669 f01
->device_control
.ctrl0
&= ~RMI_F01_CTRL0_SLEEP_MODE_MASK
;
670 f01
->device_control
.ctrl0
|= RMI_SLEEP_MODE_NORMAL
;
672 error
= rmi_write(fn
->rmi_dev
, fn
->fd
.control_base_addr
,
673 f01
->device_control
.ctrl0
);
676 "Failed to restore normal operation: %d.\n", error
);
683 static int rmi_f01_attention(struct rmi_function
*fn
,
684 unsigned long *irq_bits
)
686 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
690 error
= rmi_read(rmi_dev
, fn
->fd
.data_base_addr
, &device_status
);
693 "Failed to read device status: %d.\n", error
);
697 if (RMI_F01_STATUS_BOOTLOADER(device_status
))
699 "Device in bootloader mode, please update firmware\n");
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
);
705 dev_err(&fn
->dev
, "Device reset failed: %d\n", error
);
713 struct rmi_function_handler rmi_f01_handler
= {
717 * Do not allow user unbinding F01 as it is critical
720 .suppress_bind_attrs
= true,
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
,