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))
65 /* The device is in bootloader mode */
66 #define RMI_F01_STATUS_BOOTLOADER(status) ((status) & 0x40)
68 /* Control register bits */
71 * Sleep mode controls power management on the device and affects all
72 * functions of the device.
74 #define RMI_F01_CTRL0_SLEEP_MODE_MASK 0x03
76 #define RMI_SLEEP_MODE_NORMAL 0x00
77 #define RMI_SLEEP_MODE_SENSOR_SLEEP 0x01
78 #define RMI_SLEEP_MODE_RESERVED0 0x02
79 #define RMI_SLEEP_MODE_RESERVED1 0x03
82 * This bit disables whatever sleep mode may be selected by the sleep_mode
83 * field and forces the device to run at full power without sleeping.
85 #define RMI_F01_CTRL0_NOSLEEP_BIT BIT(2)
88 * When this bit is set, the touch controller employs a noise-filtering
89 * algorithm designed for use with a connected battery charger.
91 #define RMI_F01_CTRL0_CHARGER_BIT BIT(5)
94 * Sets the report rate for the device. The effect of this setting is
95 * highly product dependent. Check the spec sheet for your particular
98 #define RMI_F01_CTRL0_REPORTRATE_BIT BIT(6)
101 * Written by the host as an indicator that the device has been
102 * successfully configured.
104 #define RMI_F01_CTRL0_CONFIGURED_BIT BIT(7)
107 * @ctrl0 - see the bit definitions above.
108 * @doze_interval - controls the interval between checks for finger presence
109 * when the touch sensor is in doze mode, in units of 10ms.
110 * @wakeup_threshold - controls the capacitance threshold at which the touch
111 * sensor will decide to wake up from that low power state.
112 * @doze_holdoff - controls how long the touch sensor waits after the last
113 * finger lifts before entering the doze state, in units of 100ms.
115 struct f01_device_control {
123 struct f01_basic_properties properties;
124 struct f01_device_control device_control;
126 u16 doze_interval_addr;
127 u16 wakeup_threshold_addr;
128 u16 doze_holdoff_addr;
133 unsigned int num_of_irq_regs;
136 static int rmi_f01_read_properties(struct rmi_device *rmi_dev,
138 struct f01_basic_properties *props)
140 u8 queries[RMI_F01_BASIC_QUERY_LEN];
142 int query_offset = query_base_addr;
143 bool has_ds4_queries = false;
144 bool has_query42 = false;
145 bool has_sensor_id = false;
146 bool has_package_id_query = false;
147 bool has_build_id_query = false;
151 ret = rmi_read_block(rmi_dev, query_offset,
152 queries, RMI_F01_BASIC_QUERY_LEN);
154 dev_err(&rmi_dev->dev,
155 "Failed to read device query registers: %d\n", ret);
159 prod_info_addr = query_offset + 17;
160 query_offset += RMI_F01_BASIC_QUERY_LEN;
162 /* Now parse what we got */
163 props->manufacturer_id = queries[0];
165 props->has_lts = queries[1] & RMI_F01_QRY1_HAS_LTS;
166 props->has_adjustable_doze =
167 queries[1] & RMI_F01_QRY1_HAS_ADJ_DOZE;
168 props->has_adjustable_doze_holdoff =
169 queries[1] & RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF;
170 has_query42 = queries[1] & RMI_F01_QRY1_HAS_QUERY42;
171 has_sensor_id = queries[1] & RMI_F01_QRY1_HAS_SENSOR_ID;
173 snprintf(props->dom, sizeof(props->dom), "20%02d/%02d/%02d",
174 queries[5] & RMI_F01_QRY5_YEAR_MASK,
175 queries[6] & RMI_F01_QRY6_MONTH_MASK,
176 queries[7] & RMI_F01_QRY7_DAY_MASK);
178 memcpy(props->product_id, &queries[11],
179 RMI_PRODUCT_ID_LENGTH);
180 props->product_id[RMI_PRODUCT_ID_LENGTH] = '\0';
183 ((queries[2] & RMI_F01_QRY2_PRODINFO_MASK) << 7) |
184 (queries[3] & RMI_F01_QRY2_PRODINFO_MASK);
190 ret = rmi_read(rmi_dev, query_offset, queries);
192 dev_err(&rmi_dev->dev,
193 "Failed to read query 42 register: %d\n", ret);
197 has_ds4_queries = !!(queries[0] & BIT(0));
201 if (has_ds4_queries) {
202 ret = rmi_read(rmi_dev, query_offset, &ds4_query_len);
204 dev_err(&rmi_dev->dev,
205 "Failed to read DS4 queries length: %d\n", ret);
210 if (ds4_query_len > 0) {
211 ret = rmi_read(rmi_dev, query_offset, queries);
213 dev_err(&rmi_dev->dev,
214 "Failed to read DS4 queries: %d\n",
219 has_package_id_query = !!(queries[0] & BIT(0));
220 has_build_id_query = !!(queries[0] & BIT(1));
223 if (has_package_id_query)
226 if (has_build_id_query) {
227 ret = rmi_read_block(rmi_dev, prod_info_addr, queries,
230 dev_err(&rmi_dev->dev,
231 "Failed to read product info: %d\n",
236 props->firmware_id = queries[1] << 8 | queries[0];
237 props->firmware_id += queries[2] * 65536;
244 char *rmi_f01_get_product_ID(struct rmi_function *fn)
246 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
248 return f01->properties.product_id;
252 static int rmi_f01_of_probe(struct device *dev,
253 struct rmi_device_platform_data *pdata)
258 retval = rmi_of_property_read_u32(dev,
259 (u32 *)&pdata->power_management.nosleep,
260 "syna,nosleep-mode", 1);
264 retval = rmi_of_property_read_u32(dev, &val,
265 "syna,wakeup-threshold", 1);
269 pdata->power_management.wakeup_threshold = val;
271 retval = rmi_of_property_read_u32(dev, &val,
272 "syna,doze-holdoff-ms", 1);
276 pdata->power_management.doze_holdoff = val * 100;
278 retval = rmi_of_property_read_u32(dev, &val,
279 "syna,doze-interval-ms", 1);
283 pdata->power_management.doze_interval = val / 10;
288 static inline int rmi_f01_of_probe(struct device *dev,
289 struct rmi_device_platform_data *pdata)
295 static int rmi_f01_probe(struct rmi_function *fn)
297 struct rmi_device *rmi_dev = fn->rmi_dev;
298 struct rmi_driver_data *driver_data = dev_get_drvdata(&rmi_dev->dev);
299 struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
300 struct f01_data *f01;
302 u16 ctrl_base_addr = fn->fd.control_base_addr;
306 if (fn->dev.of_node) {
307 error = rmi_f01_of_probe(&fn->dev, pdata);
312 f01 = devm_kzalloc(&fn->dev, sizeof(struct f01_data), GFP_KERNEL);
316 f01->num_of_irq_regs = driver_data->num_of_irq_regs;
319 * Set the configured bit and (optionally) other important stuff
320 * in the device control register.
323 error = rmi_read(rmi_dev, fn->fd.control_base_addr,
324 &f01->device_control.ctrl0);
326 dev_err(&fn->dev, "Failed to read F01 control: %d\n", error);
330 switch (pdata->power_management.nosleep) {
331 case RMI_REG_STATE_DEFAULT:
333 case RMI_REG_STATE_OFF:
334 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_NOSLEEP_BIT;
336 case RMI_REG_STATE_ON:
337 f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
342 * Sleep mode might be set as a hangover from a system crash or
343 * reboot without power cycle. If so, clear it so the sensor
344 * is certain to function.
346 if ((f01->device_control.ctrl0 & RMI_F01_CTRL0_SLEEP_MODE_MASK) !=
347 RMI_SLEEP_MODE_NORMAL) {
349 "WARNING: Non-zero sleep mode found. Clearing...\n");
350 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
353 f01->device_control.ctrl0 |= RMI_F01_CTRL0_CONFIGURED_BIT;
355 error = rmi_write(rmi_dev, fn->fd.control_base_addr,
356 f01->device_control.ctrl0);
358 dev_err(&fn->dev, "Failed to write F01 control: %d\n", error);
362 /* Dummy read in order to clear irqs */
363 error = rmi_read(rmi_dev, fn->fd.data_base_addr + 1, &temp);
365 dev_err(&fn->dev, "Failed to read Interrupt Status.\n");
369 error = rmi_f01_read_properties(rmi_dev, fn->fd.query_base_addr,
372 dev_err(&fn->dev, "Failed to read F01 properties.\n");
376 dev_info(&fn->dev, "found RMI device, manufacturer: %s, product: %s, fw id: %d\n",
377 f01->properties.manufacturer_id == 1 ? "Synaptics" : "unknown",
378 f01->properties.product_id, f01->properties.firmware_id);
380 /* Advance to interrupt control registers, then skip over them. */
382 ctrl_base_addr += f01->num_of_irq_regs;
384 /* read control register */
385 if (f01->properties.has_adjustable_doze) {
386 f01->doze_interval_addr = ctrl_base_addr;
389 if (pdata->power_management.doze_interval) {
390 f01->device_control.doze_interval =
391 pdata->power_management.doze_interval;
392 error = rmi_write(rmi_dev, f01->doze_interval_addr,
393 f01->device_control.doze_interval);
396 "Failed to configure F01 doze interval register: %d\n",
401 error = rmi_read(rmi_dev, f01->doze_interval_addr,
402 &f01->device_control.doze_interval);
405 "Failed to read F01 doze interval register: %d\n",
411 f01->wakeup_threshold_addr = ctrl_base_addr;
414 if (pdata->power_management.wakeup_threshold) {
415 f01->device_control.wakeup_threshold =
416 pdata->power_management.wakeup_threshold;
417 error = rmi_write(rmi_dev, f01->wakeup_threshold_addr,
418 f01->device_control.wakeup_threshold);
421 "Failed to configure F01 wakeup threshold register: %d\n",
426 error = rmi_read(rmi_dev, f01->wakeup_threshold_addr,
427 &f01->device_control.wakeup_threshold);
430 "Failed to read F01 wakeup threshold register: %d\n",
437 if (f01->properties.has_lts)
440 if (f01->properties.has_adjustable_doze_holdoff) {
441 f01->doze_holdoff_addr = ctrl_base_addr;
444 if (pdata->power_management.doze_holdoff) {
445 f01->device_control.doze_holdoff =
446 pdata->power_management.doze_holdoff;
447 error = rmi_write(rmi_dev, f01->doze_holdoff_addr,
448 f01->device_control.doze_holdoff);
451 "Failed to configure F01 doze holdoff register: %d\n",
456 error = rmi_read(rmi_dev, f01->doze_holdoff_addr,
457 &f01->device_control.doze_holdoff);
460 "Failed to read F01 doze holdoff register: %d\n",
467 error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
470 "Failed to read device status: %d\n", error);
474 if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
476 "Device was reset during configuration process, status: %#02x!\n",
477 RMI_F01_STATUS_CODE(device_status));
481 dev_set_drvdata(&fn->dev, f01);
486 static int rmi_f01_config(struct rmi_function *fn)
488 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
491 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
492 f01->device_control.ctrl0);
495 "Failed to write device_control register: %d\n", error);
499 if (f01->properties.has_adjustable_doze) {
500 error = rmi_write(fn->rmi_dev, f01->doze_interval_addr,
501 f01->device_control.doze_interval);
504 "Failed to write doze interval: %d\n", error);
508 error = rmi_write_block(fn->rmi_dev,
509 f01->wakeup_threshold_addr,
510 &f01->device_control.wakeup_threshold,
514 "Failed to write wakeup threshold: %d\n",
520 if (f01->properties.has_adjustable_doze_holdoff) {
521 error = rmi_write(fn->rmi_dev, f01->doze_holdoff_addr,
522 f01->device_control.doze_holdoff);
525 "Failed to write doze holdoff: %d\n", error);
533 static int rmi_f01_suspend(struct rmi_function *fn)
535 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
539 f01->device_control.ctrl0 & RMI_F01_CTRL0_NOSLEEP_BIT;
540 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_NOSLEEP_BIT;
542 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
543 if (device_may_wakeup(fn->rmi_dev->xport->dev))
544 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_RESERVED1;
546 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_SENSOR_SLEEP;
548 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
549 f01->device_control.ctrl0);
551 dev_err(&fn->dev, "Failed to write sleep mode: %d.\n", error);
552 if (f01->old_nosleep)
553 f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
554 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
555 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
562 static int rmi_f01_resume(struct rmi_function *fn)
564 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
567 if (f01->old_nosleep)
568 f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
570 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
571 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
573 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
574 f01->device_control.ctrl0);
577 "Failed to restore normal operation: %d.\n", error);
584 static int rmi_f01_attention(struct rmi_function *fn,
585 unsigned long *irq_bits)
587 struct rmi_device *rmi_dev = fn->rmi_dev;
591 error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
594 "Failed to read device status: %d.\n", error);
598 if (RMI_F01_STATUS_BOOTLOADER(device_status))
600 "Device in bootloader mode, please update firmware\n");
602 if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
603 dev_warn(&fn->dev, "Device reset detected.\n");
604 error = rmi_dev->driver->reset_handler(rmi_dev);
606 dev_err(&fn->dev, "Device reset failed: %d\n", error);
614 struct rmi_function_handler rmi_f01_handler = {
618 * Do not allow user unbinding F01 as it is critical
621 .suppress_bind_attrs = true,
624 .probe = rmi_f01_probe,
625 .config = rmi_f01_config,
626 .attention = rmi_f01_attention,
627 .suspend = rmi_f01_suspend,
628 .resume = rmi_f01_resume,