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 "rmi_driver.h"
17 #define RMI_PRODUCT_ID_LENGTH 10
18 #define RMI_PRODUCT_INFO_LENGTH 2
20 #define RMI_DATE_CODE_LENGTH 3
22 #define PRODUCT_ID_OFFSET 0x10
23 #define PRODUCT_INFO_OFFSET 0x1E
26 /* Force a firmware reset of the sensor */
27 #define RMI_F01_CMD_DEVICE_RESET 1
29 /* Various F01_RMI_QueryX bits */
31 #define RMI_F01_QRY1_CUSTOM_MAP BIT(0)
32 #define RMI_F01_QRY1_NON_COMPLIANT BIT(1)
33 #define RMI_F01_QRY1_HAS_LTS BIT(2)
34 #define RMI_F01_QRY1_HAS_SENSOR_ID BIT(3)
35 #define RMI_F01_QRY1_HAS_CHARGER_INP BIT(4)
36 #define RMI_F01_QRY1_HAS_ADJ_DOZE BIT(5)
37 #define RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF BIT(6)
38 #define RMI_F01_QRY1_HAS_QUERY42 BIT(7)
40 #define RMI_F01_QRY5_YEAR_MASK 0x1f
41 #define RMI_F01_QRY6_MONTH_MASK 0x0f
42 #define RMI_F01_QRY7_DAY_MASK 0x1f
44 #define RMI_F01_QRY2_PRODINFO_MASK 0x7f
46 #define RMI_F01_BASIC_QUERY_LEN 21 /* From Query 00 through 20 */
48 struct f01_basic_properties
{
51 bool has_adjustable_doze
;
52 bool has_adjustable_doze_holdoff
;
53 char dom
[11]; /* YYYY/MM/DD + '\0' */
54 u8 product_id
[RMI_PRODUCT_ID_LENGTH
+ 1];
59 /* F01 device status bits */
61 /* Most recent device status event */
62 #define RMI_F01_STATUS_CODE(status) ((status) & 0x0f)
63 /* The device has lost its configuration for some reason. */
64 #define RMI_F01_STATUS_UNCONFIGURED(status) (!!((status) & 0x80))
66 /* Control register bits */
69 * Sleep mode controls power management on the device and affects all
70 * functions of the device.
72 #define RMI_F01_CTRL0_SLEEP_MODE_MASK 0x03
74 #define RMI_SLEEP_MODE_NORMAL 0x00
75 #define RMI_SLEEP_MODE_SENSOR_SLEEP 0x01
76 #define RMI_SLEEP_MODE_RESERVED0 0x02
77 #define RMI_SLEEP_MODE_RESERVED1 0x03
80 * This bit disables whatever sleep mode may be selected by the sleep_mode
81 * field and forces the device to run at full power without sleeping.
83 #define RMI_F01_CTRL0_NOSLEEP_BIT BIT(2)
86 * When this bit is set, the touch controller employs a noise-filtering
87 * algorithm designed for use with a connected battery charger.
89 #define RMI_F01_CTRL0_CHARGER_BIT BIT(5)
92 * Sets the report rate for the device. The effect of this setting is
93 * highly product dependent. Check the spec sheet for your particular
96 #define RMI_F01_CTRL0_REPORTRATE_BIT BIT(6)
99 * Written by the host as an indicator that the device has been
100 * successfully configured.
102 #define RMI_F01_CTRL0_CONFIGURED_BIT BIT(7)
105 * @ctrl0 - see the bit definitions above.
106 * @doze_interval - controls the interval between checks for finger presence
107 * when the touch sensor is in doze mode, in units of 10ms.
108 * @wakeup_threshold - controls the capacitance threshold at which the touch
109 * sensor will decide to wake up from that low power state.
110 * @doze_holdoff - controls how long the touch sensor waits after the last
111 * finger lifts before entering the doze state, in units of 100ms.
113 struct f01_device_control
{
121 struct f01_basic_properties properties
;
122 struct f01_device_control device_control
;
124 u16 doze_interval_addr
;
125 u16 wakeup_threshold_addr
;
126 u16 doze_holdoff_addr
;
131 unsigned int num_of_irq_regs
;
134 static int rmi_f01_read_properties(struct rmi_device
*rmi_dev
,
136 struct f01_basic_properties
*props
)
138 u8 queries
[RMI_F01_BASIC_QUERY_LEN
];
140 int query_offset
= query_base_addr
;
141 bool has_ds4_queries
= false;
142 bool has_query42
= false;
143 bool has_sensor_id
= false;
144 bool has_package_id_query
= false;
145 bool has_build_id_query
= false;
149 ret
= rmi_read_block(rmi_dev
, query_offset
,
150 queries
, RMI_F01_BASIC_QUERY_LEN
);
152 dev_err(&rmi_dev
->dev
,
153 "Failed to read device query registers: %d\n", ret
);
157 prod_info_addr
= query_offset
+ 17;
158 query_offset
+= RMI_F01_BASIC_QUERY_LEN
;
160 /* Now parse what we got */
161 props
->manufacturer_id
= queries
[0];
163 props
->has_lts
= queries
[1] & RMI_F01_QRY1_HAS_LTS
;
164 props
->has_adjustable_doze
=
165 queries
[1] & RMI_F01_QRY1_HAS_ADJ_DOZE
;
166 props
->has_adjustable_doze_holdoff
=
167 queries
[1] & RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF
;
168 has_query42
= queries
[1] & RMI_F01_QRY1_HAS_QUERY42
;
169 has_sensor_id
= queries
[1] & RMI_F01_QRY1_HAS_SENSOR_ID
;
171 snprintf(props
->dom
, sizeof(props
->dom
), "20%02d/%02d/%02d",
172 queries
[5] & RMI_F01_QRY5_YEAR_MASK
,
173 queries
[6] & RMI_F01_QRY6_MONTH_MASK
,
174 queries
[7] & RMI_F01_QRY7_DAY_MASK
);
176 memcpy(props
->product_id
, &queries
[11],
177 RMI_PRODUCT_ID_LENGTH
);
178 props
->product_id
[RMI_PRODUCT_ID_LENGTH
] = '\0';
181 ((queries
[2] & RMI_F01_QRY2_PRODINFO_MASK
) << 7) |
182 (queries
[3] & RMI_F01_QRY2_PRODINFO_MASK
);
188 ret
= rmi_read(rmi_dev
, query_offset
, queries
);
190 dev_err(&rmi_dev
->dev
,
191 "Failed to read query 42 register: %d\n", ret
);
195 has_ds4_queries
= !!(queries
[0] & BIT(0));
199 if (has_ds4_queries
) {
200 ret
= rmi_read(rmi_dev
, query_offset
, &ds4_query_len
);
202 dev_err(&rmi_dev
->dev
,
203 "Failed to read DS4 queries length: %d\n", ret
);
208 if (ds4_query_len
> 0) {
209 ret
= rmi_read(rmi_dev
, query_offset
, queries
);
211 dev_err(&rmi_dev
->dev
,
212 "Failed to read DS4 queries: %d\n",
217 has_package_id_query
= !!(queries
[0] & BIT(0));
218 has_build_id_query
= !!(queries
[0] & BIT(1));
221 if (has_package_id_query
)
224 if (has_build_id_query
) {
225 ret
= rmi_read_block(rmi_dev
, prod_info_addr
, queries
,
228 dev_err(&rmi_dev
->dev
,
229 "Failed to read product info: %d\n",
234 props
->firmware_id
= queries
[1] << 8 | queries
[0];
235 props
->firmware_id
+= queries
[2] * 65536;
242 char *rmi_f01_get_product_ID(struct rmi_function
*fn
)
244 struct f01_data
*f01
= dev_get_drvdata(&fn
->dev
);
246 return f01
->properties
.product_id
;
250 static int rmi_f01_of_probe(struct device
*dev
,
251 struct rmi_device_platform_data
*pdata
)
256 retval
= rmi_of_property_read_u32(dev
,
257 (u32
*)&pdata
->power_management
.nosleep
,
258 "syna,nosleep-mode", 1);
262 retval
= rmi_of_property_read_u32(dev
, &val
,
263 "syna,wakeup-threshold", 1);
267 pdata
->power_management
.wakeup_threshold
= val
;
269 retval
= rmi_of_property_read_u32(dev
, &val
,
270 "syna,doze-holdoff-ms", 1);
274 pdata
->power_management
.doze_holdoff
= val
* 100;
276 retval
= rmi_of_property_read_u32(dev
, &val
,
277 "syna,doze-interval-ms", 1);
281 pdata
->power_management
.doze_interval
= val
/ 10;
286 static inline int rmi_f01_of_probe(struct device
*dev
,
287 struct rmi_device_platform_data
*pdata
)
293 static int rmi_f01_probe(struct rmi_function
*fn
)
295 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
296 struct rmi_driver_data
*driver_data
= dev_get_drvdata(&rmi_dev
->dev
);
297 struct rmi_device_platform_data
*pdata
= rmi_get_platform_data(rmi_dev
);
298 struct f01_data
*f01
;
300 u16 ctrl_base_addr
= fn
->fd
.control_base_addr
;
304 if (fn
->dev
.of_node
) {
305 error
= rmi_f01_of_probe(&fn
->dev
, pdata
);
310 f01
= devm_kzalloc(&fn
->dev
, sizeof(struct f01_data
), GFP_KERNEL
);
314 f01
->num_of_irq_regs
= driver_data
->num_of_irq_regs
;
317 * Set the configured bit and (optionally) other important stuff
318 * in the device control register.
321 error
= rmi_read(rmi_dev
, fn
->fd
.control_base_addr
,
322 &f01
->device_control
.ctrl0
);
324 dev_err(&fn
->dev
, "Failed to read F01 control: %d\n", error
);
328 switch (pdata
->power_management
.nosleep
) {
329 case RMI_F01_NOSLEEP_DEFAULT
:
331 case RMI_F01_NOSLEEP_OFF
:
332 f01
->device_control
.ctrl0
&= ~RMI_F01_CTRL0_NOSLEEP_BIT
;
334 case RMI_F01_NOSLEEP_ON
:
335 f01
->device_control
.ctrl0
|= RMI_F01_CTRL0_NOSLEEP_BIT
;
340 * Sleep mode might be set as a hangover from a system crash or
341 * reboot without power cycle. If so, clear it so the sensor
342 * is certain to function.
344 if ((f01
->device_control
.ctrl0
& RMI_F01_CTRL0_SLEEP_MODE_MASK
) !=
345 RMI_SLEEP_MODE_NORMAL
) {
347 "WARNING: Non-zero sleep mode found. Clearing...\n");
348 f01
->device_control
.ctrl0
&= ~RMI_F01_CTRL0_SLEEP_MODE_MASK
;
351 f01
->device_control
.ctrl0
|= RMI_F01_CTRL0_CONFIGURED_BIT
;
353 error
= rmi_write(rmi_dev
, fn
->fd
.control_base_addr
,
354 f01
->device_control
.ctrl0
);
356 dev_err(&fn
->dev
, "Failed to write F01 control: %d\n", error
);
360 /* Dummy read in order to clear irqs */
361 error
= rmi_read(rmi_dev
, fn
->fd
.data_base_addr
+ 1, &temp
);
363 dev_err(&fn
->dev
, "Failed to read Interrupt Status.\n");
367 error
= rmi_f01_read_properties(rmi_dev
, fn
->fd
.query_base_addr
,
370 dev_err(&fn
->dev
, "Failed to read F01 properties.\n");
374 dev_info(&fn
->dev
, "found RMI device, manufacturer: %s, product: %s, fw id: %d\n",
375 f01
->properties
.manufacturer_id
== 1 ? "Synaptics" : "unknown",
376 f01
->properties
.product_id
, f01
->properties
.firmware_id
);
378 /* Advance to interrupt control registers, then skip over them. */
380 ctrl_base_addr
+= f01
->num_of_irq_regs
;
382 /* read control register */
383 if (f01
->properties
.has_adjustable_doze
) {
384 f01
->doze_interval_addr
= ctrl_base_addr
;
387 if (pdata
->power_management
.doze_interval
) {
388 f01
->device_control
.doze_interval
=
389 pdata
->power_management
.doze_interval
;
390 error
= rmi_write(rmi_dev
, f01
->doze_interval_addr
,
391 f01
->device_control
.doze_interval
);
394 "Failed to configure F01 doze interval register: %d\n",
399 error
= rmi_read(rmi_dev
, f01
->doze_interval_addr
,
400 &f01
->device_control
.doze_interval
);
403 "Failed to read F01 doze interval register: %d\n",
409 f01
->wakeup_threshold_addr
= ctrl_base_addr
;
412 if (pdata
->power_management
.wakeup_threshold
) {
413 f01
->device_control
.wakeup_threshold
=
414 pdata
->power_management
.wakeup_threshold
;
415 error
= rmi_write(rmi_dev
, f01
->wakeup_threshold_addr
,
416 f01
->device_control
.wakeup_threshold
);
419 "Failed to configure F01 wakeup threshold register: %d\n",
424 error
= rmi_read(rmi_dev
, f01
->wakeup_threshold_addr
,
425 &f01
->device_control
.wakeup_threshold
);
428 "Failed to read F01 wakeup threshold register: %d\n",
435 if (f01
->properties
.has_lts
)
438 if (f01
->properties
.has_adjustable_doze_holdoff
) {
439 f01
->doze_holdoff_addr
= ctrl_base_addr
;
442 if (pdata
->power_management
.doze_holdoff
) {
443 f01
->device_control
.doze_holdoff
=
444 pdata
->power_management
.doze_holdoff
;
445 error
= rmi_write(rmi_dev
, f01
->doze_holdoff_addr
,
446 f01
->device_control
.doze_holdoff
);
449 "Failed to configure F01 doze holdoff register: %d\n",
454 error
= rmi_read(rmi_dev
, f01
->doze_holdoff_addr
,
455 &f01
->device_control
.doze_holdoff
);
458 "Failed to read F01 doze holdoff register: %d\n",
465 error
= rmi_read(rmi_dev
, fn
->fd
.data_base_addr
, &device_status
);
468 "Failed to read device status: %d\n", error
);
472 if (RMI_F01_STATUS_UNCONFIGURED(device_status
)) {
474 "Device was reset during configuration process, status: %#02x!\n",
475 RMI_F01_STATUS_CODE(device_status
));
479 dev_set_drvdata(&fn
->dev
, f01
);
484 static int rmi_f01_config(struct rmi_function
*fn
)
486 struct f01_data
*f01
= dev_get_drvdata(&fn
->dev
);
489 error
= rmi_write(fn
->rmi_dev
, fn
->fd
.control_base_addr
,
490 f01
->device_control
.ctrl0
);
493 "Failed to write device_control register: %d\n", error
);
497 if (f01
->properties
.has_adjustable_doze
) {
498 error
= rmi_write(fn
->rmi_dev
, f01
->doze_interval_addr
,
499 f01
->device_control
.doze_interval
);
502 "Failed to write doze interval: %d\n", error
);
506 error
= rmi_write_block(fn
->rmi_dev
,
507 f01
->wakeup_threshold_addr
,
508 &f01
->device_control
.wakeup_threshold
,
512 "Failed to write wakeup threshold: %d\n",
518 if (f01
->properties
.has_adjustable_doze_holdoff
) {
519 error
= rmi_write(fn
->rmi_dev
, f01
->doze_holdoff_addr
,
520 f01
->device_control
.doze_holdoff
);
523 "Failed to write doze holdoff: %d\n", error
);
531 static int rmi_f01_suspend(struct rmi_function
*fn
)
533 struct f01_data
*f01
= dev_get_drvdata(&fn
->dev
);
537 f01
->device_control
.ctrl0
& RMI_F01_CTRL0_NOSLEEP_BIT
;
538 f01
->device_control
.ctrl0
&= ~RMI_F01_CTRL0_NOSLEEP_BIT
;
540 f01
->device_control
.ctrl0
&= ~RMI_F01_CTRL0_SLEEP_MODE_MASK
;
541 if (device_may_wakeup(fn
->rmi_dev
->xport
->dev
))
542 f01
->device_control
.ctrl0
|= RMI_SLEEP_MODE_RESERVED1
;
544 f01
->device_control
.ctrl0
|= RMI_SLEEP_MODE_SENSOR_SLEEP
;
546 error
= rmi_write(fn
->rmi_dev
, fn
->fd
.control_base_addr
,
547 f01
->device_control
.ctrl0
);
549 dev_err(&fn
->dev
, "Failed to write sleep mode: %d.\n", error
);
550 if (f01
->old_nosleep
)
551 f01
->device_control
.ctrl0
|= RMI_F01_CTRL0_NOSLEEP_BIT
;
552 f01
->device_control
.ctrl0
&= ~RMI_F01_CTRL0_SLEEP_MODE_MASK
;
553 f01
->device_control
.ctrl0
|= RMI_SLEEP_MODE_NORMAL
;
560 static int rmi_f01_resume(struct rmi_function
*fn
)
562 struct f01_data
*f01
= dev_get_drvdata(&fn
->dev
);
565 if (f01
->old_nosleep
)
566 f01
->device_control
.ctrl0
|= RMI_F01_CTRL0_NOSLEEP_BIT
;
568 f01
->device_control
.ctrl0
&= ~RMI_F01_CTRL0_SLEEP_MODE_MASK
;
569 f01
->device_control
.ctrl0
|= RMI_SLEEP_MODE_NORMAL
;
571 error
= rmi_write(fn
->rmi_dev
, fn
->fd
.control_base_addr
,
572 f01
->device_control
.ctrl0
);
575 "Failed to restore normal operation: %d.\n", error
);
582 static int rmi_f01_attention(struct rmi_function
*fn
,
583 unsigned long *irq_bits
)
585 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
589 error
= rmi_read(rmi_dev
, fn
->fd
.data_base_addr
, &device_status
);
592 "Failed to read device status: %d.\n", error
);
596 if (RMI_F01_STATUS_UNCONFIGURED(device_status
)) {
597 dev_warn(&fn
->dev
, "Device reset detected.\n");
598 error
= rmi_dev
->driver
->reset_handler(rmi_dev
);
600 dev_err(&fn
->dev
, "Device reset failed: %d\n", error
);
608 struct rmi_function_handler rmi_f01_handler
= {
612 * Do not allow user unbinding F01 as it is critical
615 .suppress_bind_attrs
= true,
618 .probe
= rmi_f01_probe
,
619 .config
= rmi_f01_config
,
620 .attention
= rmi_f01_attention
,
621 .suspend
= rmi_f01_suspend
,
622 .resume
= rmi_f01_resume
,