1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011-2016 Synaptics Incorporated
4 * Copyright (c) 2011 Unixphere
7 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/uaccess.h>
12 #include <asm/unaligned.h>
13 #include "rmi_driver.h"
15 #define RMI_PRODUCT_ID_LENGTH 10
16 #define RMI_PRODUCT_INFO_LENGTH 2
18 #define RMI_DATE_CODE_LENGTH 3
20 #define PRODUCT_ID_OFFSET 0x10
21 #define PRODUCT_INFO_OFFSET 0x1E
24 /* Force a firmware reset of the sensor */
25 #define RMI_F01_CMD_DEVICE_RESET 1
27 /* Various F01_RMI_QueryX bits */
29 #define RMI_F01_QRY1_CUSTOM_MAP BIT(0)
30 #define RMI_F01_QRY1_NON_COMPLIANT BIT(1)
31 #define RMI_F01_QRY1_HAS_LTS BIT(2)
32 #define RMI_F01_QRY1_HAS_SENSOR_ID BIT(3)
33 #define RMI_F01_QRY1_HAS_CHARGER_INP BIT(4)
34 #define RMI_F01_QRY1_HAS_ADJ_DOZE BIT(5)
35 #define RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF BIT(6)
36 #define RMI_F01_QRY1_HAS_QUERY42 BIT(7)
38 #define RMI_F01_QRY5_YEAR_MASK 0x1f
39 #define RMI_F01_QRY6_MONTH_MASK 0x0f
40 #define RMI_F01_QRY7_DAY_MASK 0x1f
42 #define RMI_F01_QRY2_PRODINFO_MASK 0x7f
44 #define RMI_F01_BASIC_QUERY_LEN 21 /* From Query 00 through 20 */
46 struct f01_basic_properties
{
49 bool has_adjustable_doze
;
50 bool has_adjustable_doze_holdoff
;
51 char dom
[11]; /* YYYY/MM/DD + '\0' */
52 u8 product_id
[RMI_PRODUCT_ID_LENGTH
+ 1];
58 /* F01 device status bits */
60 /* Most recent device status event */
61 #define RMI_F01_STATUS_CODE(status) ((status) & 0x0f)
62 /* The device has lost its configuration for some reason. */
63 #define RMI_F01_STATUS_UNCONFIGURED(status) (!!((status) & 0x80))
64 /* The device is in bootloader mode */
65 #define RMI_F01_STATUS_BOOTLOADER(status) ((status) & 0x40)
67 /* Control register bits */
70 * Sleep mode controls power management on the device and affects all
71 * functions of the device.
73 #define RMI_F01_CTRL0_SLEEP_MODE_MASK 0x03
75 #define RMI_SLEEP_MODE_NORMAL 0x00
76 #define RMI_SLEEP_MODE_SENSOR_SLEEP 0x01
77 #define RMI_SLEEP_MODE_RESERVED0 0x02
78 #define RMI_SLEEP_MODE_RESERVED1 0x03
81 * This bit disables whatever sleep mode may be selected by the sleep_mode
82 * field and forces the device to run at full power without sleeping.
84 #define RMI_F01_CTRL0_NOSLEEP_BIT BIT(2)
87 * When this bit is set, the touch controller employs a noise-filtering
88 * algorithm designed for use with a connected battery charger.
90 #define RMI_F01_CTRL0_CHARGER_BIT BIT(5)
93 * Sets the report rate for the device. The effect of this setting is
94 * highly product dependent. Check the spec sheet for your particular
97 #define RMI_F01_CTRL0_REPORTRATE_BIT BIT(6)
100 * Written by the host as an indicator that the device has been
101 * successfully configured.
103 #define RMI_F01_CTRL0_CONFIGURED_BIT BIT(7)
106 * struct f01_device_control - controls basic sensor functions
108 * @ctrl0: see the bit definitions above.
109 * @doze_interval: controls the interval between checks for finger presence
110 * when the touch sensor is in doze mode, in units of 10ms.
111 * @wakeup_threshold: controls the capacitance threshold at which the touch
112 * sensor will decide to wake up from that low power state.
113 * @doze_holdoff: controls how long the touch sensor waits after the last
114 * finger lifts before entering the doze state, in units of 100ms.
116 struct f01_device_control
{
124 struct f01_basic_properties properties
;
125 struct f01_device_control device_control
;
127 u16 doze_interval_addr
;
128 u16 wakeup_threshold_addr
;
129 u16 doze_holdoff_addr
;
134 unsigned int num_of_irq_regs
;
137 static int rmi_f01_read_properties(struct rmi_device
*rmi_dev
,
139 struct f01_basic_properties
*props
)
141 u8 queries
[RMI_F01_BASIC_QUERY_LEN
];
143 int query_offset
= query_base_addr
;
144 bool has_ds4_queries
= false;
145 bool has_query42
= false;
146 bool has_sensor_id
= false;
147 bool has_package_id_query
= false;
148 bool has_build_id_query
= false;
152 ret
= rmi_read_block(rmi_dev
, query_offset
,
153 queries
, RMI_F01_BASIC_QUERY_LEN
);
155 dev_err(&rmi_dev
->dev
,
156 "Failed to read device query registers: %d\n", ret
);
160 prod_info_addr
= query_offset
+ 17;
161 query_offset
+= RMI_F01_BASIC_QUERY_LEN
;
163 /* Now parse what we got */
164 props
->manufacturer_id
= queries
[0];
166 props
->has_lts
= queries
[1] & RMI_F01_QRY1_HAS_LTS
;
167 props
->has_adjustable_doze
=
168 queries
[1] & RMI_F01_QRY1_HAS_ADJ_DOZE
;
169 props
->has_adjustable_doze_holdoff
=
170 queries
[1] & RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF
;
171 has_query42
= queries
[1] & RMI_F01_QRY1_HAS_QUERY42
;
172 has_sensor_id
= queries
[1] & RMI_F01_QRY1_HAS_SENSOR_ID
;
174 snprintf(props
->dom
, sizeof(props
->dom
), "20%02d/%02d/%02d",
175 queries
[5] & RMI_F01_QRY5_YEAR_MASK
,
176 queries
[6] & RMI_F01_QRY6_MONTH_MASK
,
177 queries
[7] & RMI_F01_QRY7_DAY_MASK
);
179 memcpy(props
->product_id
, &queries
[11],
180 RMI_PRODUCT_ID_LENGTH
);
181 props
->product_id
[RMI_PRODUCT_ID_LENGTH
] = '\0';
184 ((queries
[2] & RMI_F01_QRY2_PRODINFO_MASK
) << 7) |
185 (queries
[3] & RMI_F01_QRY2_PRODINFO_MASK
);
191 ret
= rmi_read(rmi_dev
, query_offset
, queries
);
193 dev_err(&rmi_dev
->dev
,
194 "Failed to read query 42 register: %d\n", ret
);
198 has_ds4_queries
= !!(queries
[0] & BIT(0));
202 if (has_ds4_queries
) {
203 ret
= rmi_read(rmi_dev
, query_offset
, &ds4_query_len
);
205 dev_err(&rmi_dev
->dev
,
206 "Failed to read DS4 queries length: %d\n", ret
);
211 if (ds4_query_len
> 0) {
212 ret
= rmi_read(rmi_dev
, query_offset
, queries
);
214 dev_err(&rmi_dev
->dev
,
215 "Failed to read DS4 queries: %d\n",
220 has_package_id_query
= !!(queries
[0] & BIT(0));
221 has_build_id_query
= !!(queries
[0] & BIT(1));
224 if (has_package_id_query
) {
225 ret
= rmi_read_block(rmi_dev
, prod_info_addr
,
226 queries
, sizeof(__le64
));
228 dev_err(&rmi_dev
->dev
,
229 "Failed to read package info: %d\n",
234 props
->package_id
= get_unaligned_le64(queries
);
238 if (has_build_id_query
) {
239 ret
= rmi_read_block(rmi_dev
, prod_info_addr
, queries
,
242 dev_err(&rmi_dev
->dev
,
243 "Failed to read product info: %d\n",
248 props
->firmware_id
= queries
[1] << 8 | queries
[0];
249 props
->firmware_id
+= queries
[2] * 65536;
256 const char *rmi_f01_get_product_ID(struct rmi_function
*fn
)
258 struct f01_data
*f01
= dev_get_drvdata(&fn
->dev
);
260 return f01
->properties
.product_id
;
263 static ssize_t
rmi_driver_manufacturer_id_show(struct device
*dev
,
264 struct device_attribute
*dattr
,
267 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
268 struct f01_data
*f01
= dev_get_drvdata(&data
->f01_container
->dev
);
270 return scnprintf(buf
, PAGE_SIZE
, "%d\n",
271 f01
->properties
.manufacturer_id
);
274 static DEVICE_ATTR(manufacturer_id
, 0444,
275 rmi_driver_manufacturer_id_show
, NULL
);
277 static ssize_t
rmi_driver_dom_show(struct device
*dev
,
278 struct device_attribute
*dattr
, char *buf
)
280 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
281 struct f01_data
*f01
= dev_get_drvdata(&data
->f01_container
->dev
);
283 return scnprintf(buf
, PAGE_SIZE
, "%s\n", f01
->properties
.dom
);
286 static DEVICE_ATTR(date_of_manufacture
, 0444, rmi_driver_dom_show
, NULL
);
288 static ssize_t
rmi_driver_product_id_show(struct device
*dev
,
289 struct device_attribute
*dattr
,
292 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
293 struct f01_data
*f01
= dev_get_drvdata(&data
->f01_container
->dev
);
295 return scnprintf(buf
, PAGE_SIZE
, "%s\n", f01
->properties
.product_id
);
298 static DEVICE_ATTR(product_id
, 0444, rmi_driver_product_id_show
, NULL
);
300 static ssize_t
rmi_driver_firmware_id_show(struct device
*dev
,
301 struct device_attribute
*dattr
,
304 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
305 struct f01_data
*f01
= dev_get_drvdata(&data
->f01_container
->dev
);
307 return scnprintf(buf
, PAGE_SIZE
, "%d\n", f01
->properties
.firmware_id
);
310 static DEVICE_ATTR(firmware_id
, 0444, rmi_driver_firmware_id_show
, NULL
);
312 static ssize_t
rmi_driver_package_id_show(struct device
*dev
,
313 struct device_attribute
*dattr
,
316 struct rmi_driver_data
*data
= dev_get_drvdata(dev
);
317 struct f01_data
*f01
= dev_get_drvdata(&data
->f01_container
->dev
);
319 u32 package_id
= f01
->properties
.package_id
;
321 return scnprintf(buf
, PAGE_SIZE
, "%04x.%04x\n",
322 package_id
& 0xffff, (package_id
>> 16) & 0xffff);
325 static DEVICE_ATTR(package_id
, 0444, rmi_driver_package_id_show
, NULL
);
327 static struct attribute
*rmi_f01_attrs
[] = {
328 &dev_attr_manufacturer_id
.attr
,
329 &dev_attr_date_of_manufacture
.attr
,
330 &dev_attr_product_id
.attr
,
331 &dev_attr_firmware_id
.attr
,
332 &dev_attr_package_id
.attr
,
336 static const struct attribute_group rmi_f01_attr_group
= {
337 .attrs
= rmi_f01_attrs
,
341 static int rmi_f01_of_probe(struct device
*dev
,
342 struct rmi_device_platform_data
*pdata
)
347 retval
= rmi_of_property_read_u32(dev
,
348 (u32
*)&pdata
->power_management
.nosleep
,
349 "syna,nosleep-mode", 1);
353 retval
= rmi_of_property_read_u32(dev
, &val
,
354 "syna,wakeup-threshold", 1);
358 pdata
->power_management
.wakeup_threshold
= val
;
360 retval
= rmi_of_property_read_u32(dev
, &val
,
361 "syna,doze-holdoff-ms", 1);
365 pdata
->power_management
.doze_holdoff
= val
* 100;
367 retval
= rmi_of_property_read_u32(dev
, &val
,
368 "syna,doze-interval-ms", 1);
372 pdata
->power_management
.doze_interval
= val
/ 10;
377 static inline int rmi_f01_of_probe(struct device
*dev
,
378 struct rmi_device_platform_data
*pdata
)
384 static int rmi_f01_probe(struct rmi_function
*fn
)
386 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
387 struct rmi_driver_data
*driver_data
= dev_get_drvdata(&rmi_dev
->dev
);
388 struct rmi_device_platform_data
*pdata
= rmi_get_platform_data(rmi_dev
);
389 struct f01_data
*f01
;
391 u16 ctrl_base_addr
= fn
->fd
.control_base_addr
;
395 if (fn
->dev
.of_node
) {
396 error
= rmi_f01_of_probe(&fn
->dev
, pdata
);
401 f01
= devm_kzalloc(&fn
->dev
, sizeof(struct f01_data
), GFP_KERNEL
);
405 f01
->num_of_irq_regs
= driver_data
->num_of_irq_regs
;
408 * Set the configured bit and (optionally) other important stuff
409 * in the device control register.
412 error
= rmi_read(rmi_dev
, fn
->fd
.control_base_addr
,
413 &f01
->device_control
.ctrl0
);
415 dev_err(&fn
->dev
, "Failed to read F01 control: %d\n", error
);
419 switch (pdata
->power_management
.nosleep
) {
420 case RMI_REG_STATE_DEFAULT
:
422 case RMI_REG_STATE_OFF
:
423 f01
->device_control
.ctrl0
&= ~RMI_F01_CTRL0_NOSLEEP_BIT
;
425 case RMI_REG_STATE_ON
:
426 f01
->device_control
.ctrl0
|= RMI_F01_CTRL0_NOSLEEP_BIT
;
431 * Sleep mode might be set as a hangover from a system crash or
432 * reboot without power cycle. If so, clear it so the sensor
433 * is certain to function.
435 if ((f01
->device_control
.ctrl0
& RMI_F01_CTRL0_SLEEP_MODE_MASK
) !=
436 RMI_SLEEP_MODE_NORMAL
) {
438 "WARNING: Non-zero sleep mode found. Clearing...\n");
439 f01
->device_control
.ctrl0
&= ~RMI_F01_CTRL0_SLEEP_MODE_MASK
;
442 f01
->device_control
.ctrl0
|= RMI_F01_CTRL0_CONFIGURED_BIT
;
444 error
= rmi_write(rmi_dev
, fn
->fd
.control_base_addr
,
445 f01
->device_control
.ctrl0
);
447 dev_err(&fn
->dev
, "Failed to write F01 control: %d\n", error
);
451 /* Dummy read in order to clear irqs */
452 error
= rmi_read(rmi_dev
, fn
->fd
.data_base_addr
+ 1, &temp
);
454 dev_err(&fn
->dev
, "Failed to read Interrupt Status.\n");
458 error
= rmi_f01_read_properties(rmi_dev
, fn
->fd
.query_base_addr
,
461 dev_err(&fn
->dev
, "Failed to read F01 properties.\n");
465 dev_info(&fn
->dev
, "found RMI device, manufacturer: %s, product: %s, fw id: %d\n",
466 f01
->properties
.manufacturer_id
== 1 ? "Synaptics" : "unknown",
467 f01
->properties
.product_id
, f01
->properties
.firmware_id
);
469 /* Advance to interrupt control registers, then skip over them. */
471 ctrl_base_addr
+= f01
->num_of_irq_regs
;
473 /* read control register */
474 if (f01
->properties
.has_adjustable_doze
) {
475 f01
->doze_interval_addr
= ctrl_base_addr
;
478 if (pdata
->power_management
.doze_interval
) {
479 f01
->device_control
.doze_interval
=
480 pdata
->power_management
.doze_interval
;
481 error
= rmi_write(rmi_dev
, f01
->doze_interval_addr
,
482 f01
->device_control
.doze_interval
);
485 "Failed to configure F01 doze interval register: %d\n",
490 error
= rmi_read(rmi_dev
, f01
->doze_interval_addr
,
491 &f01
->device_control
.doze_interval
);
494 "Failed to read F01 doze interval register: %d\n",
500 f01
->wakeup_threshold_addr
= ctrl_base_addr
;
503 if (pdata
->power_management
.wakeup_threshold
) {
504 f01
->device_control
.wakeup_threshold
=
505 pdata
->power_management
.wakeup_threshold
;
506 error
= rmi_write(rmi_dev
, f01
->wakeup_threshold_addr
,
507 f01
->device_control
.wakeup_threshold
);
510 "Failed to configure F01 wakeup threshold register: %d\n",
515 error
= rmi_read(rmi_dev
, f01
->wakeup_threshold_addr
,
516 &f01
->device_control
.wakeup_threshold
);
519 "Failed to read F01 wakeup threshold register: %d\n",
526 if (f01
->properties
.has_lts
)
529 if (f01
->properties
.has_adjustable_doze_holdoff
) {
530 f01
->doze_holdoff_addr
= ctrl_base_addr
;
533 if (pdata
->power_management
.doze_holdoff
) {
534 f01
->device_control
.doze_holdoff
=
535 pdata
->power_management
.doze_holdoff
;
536 error
= rmi_write(rmi_dev
, f01
->doze_holdoff_addr
,
537 f01
->device_control
.doze_holdoff
);
540 "Failed to configure F01 doze holdoff register: %d\n",
545 error
= rmi_read(rmi_dev
, f01
->doze_holdoff_addr
,
546 &f01
->device_control
.doze_holdoff
);
549 "Failed to read F01 doze holdoff register: %d\n",
556 error
= rmi_read(rmi_dev
, fn
->fd
.data_base_addr
, &device_status
);
559 "Failed to read device status: %d\n", error
);
563 if (RMI_F01_STATUS_UNCONFIGURED(device_status
)) {
565 "Device was reset during configuration process, status: %#02x!\n",
566 RMI_F01_STATUS_CODE(device_status
));
570 dev_set_drvdata(&fn
->dev
, f01
);
572 error
= sysfs_create_group(&fn
->rmi_dev
->dev
.kobj
, &rmi_f01_attr_group
);
574 dev_warn(&fn
->dev
, "Failed to create sysfs group: %d\n", error
);
579 static void rmi_f01_remove(struct rmi_function
*fn
)
581 /* Note that the bus device is used, not the F01 device */
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 irqreturn_t
rmi_f01_attention(int irq
, void *ctx
)
685 struct rmi_function
*fn
= ctx
;
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
);
694 return IRQ_RETVAL(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
);
706 return IRQ_RETVAL(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
,