mmc: rtsx_pci: Enable MMC_CAP_ERASE to allow erase/discard/trim requests
[linux/fpc-iii.git] / drivers / input / rmi4 / rmi_f01.c
blobeb362bc71a4c1508ec837722b82253a91a4d8888
1 /*
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.
8 */
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>
15 #include <linux/of.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 {
50 u8 manufacturer_id;
51 bool has_lts;
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];
56 u16 productinfo;
57 u32 firmware_id;
60 /* F01 device status bits */
62 /* Most recent device status event */
63 #define RMI_F01_STATUS_CODE(status) ((status) & 0x0f)
64 /* The device has lost its configuration for some reason. */
65 #define RMI_F01_STATUS_UNCONFIGURED(status) (!!((status) & 0x80))
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_CRTL0_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_CRTL0_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
95 * touch sensor.
97 #define RMI_F01_CRTL0_REPORTRATE_BIT BIT(6)
100 * Written by the host as an indicator that the device has been
101 * successfully configured.
103 #define RMI_F01_CRTL0_CONFIGURED_BIT BIT(7)
106 * @ctrl0 - see the bit definitions above.
107 * @doze_interval - controls the interval between checks for finger presence
108 * when the touch sensor is in doze mode, in units of 10ms.
109 * @wakeup_threshold - controls the capacitance threshold at which the touch
110 * sensor will decide to wake up from that low power state.
111 * @doze_holdoff - controls how long the touch sensor waits after the last
112 * finger lifts before entering the doze state, in units of 100ms.
114 struct f01_device_control {
115 u8 ctrl0;
116 u8 doze_interval;
117 u8 wakeup_threshold;
118 u8 doze_holdoff;
121 struct f01_data {
122 struct f01_basic_properties properties;
123 struct f01_device_control device_control;
125 u16 doze_interval_addr;
126 u16 wakeup_threshold_addr;
127 u16 doze_holdoff_addr;
129 bool suspended;
130 bool old_nosleep;
132 unsigned int num_of_irq_regs;
135 static int rmi_f01_read_properties(struct rmi_device *rmi_dev,
136 u16 query_base_addr,
137 struct f01_basic_properties *props)
139 u8 queries[RMI_F01_BASIC_QUERY_LEN];
140 int ret;
141 int query_offset = query_base_addr;
142 bool has_ds4_queries = false;
143 bool has_query42 = false;
144 bool has_sensor_id = false;
145 bool has_package_id_query = false;
146 bool has_build_id_query = false;
147 u16 prod_info_addr;
148 u8 ds4_query_len;
150 ret = rmi_read_block(rmi_dev, query_offset,
151 queries, RMI_F01_BASIC_QUERY_LEN);
152 if (ret) {
153 dev_err(&rmi_dev->dev,
154 "Failed to read device query registers: %d\n", ret);
155 return ret;
158 prod_info_addr = query_offset + 17;
159 query_offset += RMI_F01_BASIC_QUERY_LEN;
161 /* Now parse what we got */
162 props->manufacturer_id = queries[0];
164 props->has_lts = queries[1] & RMI_F01_QRY1_HAS_LTS;
165 props->has_adjustable_doze =
166 queries[1] & RMI_F01_QRY1_HAS_ADJ_DOZE;
167 props->has_adjustable_doze_holdoff =
168 queries[1] & RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF;
169 has_query42 = queries[1] & RMI_F01_QRY1_HAS_QUERY42;
170 has_sensor_id = queries[1] & RMI_F01_QRY1_HAS_SENSOR_ID;
172 snprintf(props->dom, sizeof(props->dom), "20%02d/%02d/%02d",
173 queries[5] & RMI_F01_QRY5_YEAR_MASK,
174 queries[6] & RMI_F01_QRY6_MONTH_MASK,
175 queries[7] & RMI_F01_QRY7_DAY_MASK);
177 memcpy(props->product_id, &queries[11],
178 RMI_PRODUCT_ID_LENGTH);
179 props->product_id[RMI_PRODUCT_ID_LENGTH] = '\0';
181 props->productinfo =
182 ((queries[2] & RMI_F01_QRY2_PRODINFO_MASK) << 7) |
183 (queries[3] & RMI_F01_QRY2_PRODINFO_MASK);
185 if (has_sensor_id)
186 query_offset++;
188 if (has_query42) {
189 ret = rmi_read(rmi_dev, query_offset, queries);
190 if (ret) {
191 dev_err(&rmi_dev->dev,
192 "Failed to read query 42 register: %d\n", ret);
193 return ret;
196 has_ds4_queries = !!(queries[0] & BIT(0));
197 query_offset++;
200 if (has_ds4_queries) {
201 ret = rmi_read(rmi_dev, query_offset, &ds4_query_len);
202 if (ret) {
203 dev_err(&rmi_dev->dev,
204 "Failed to read DS4 queries length: %d\n", ret);
205 return ret;
207 query_offset++;
209 if (ds4_query_len > 0) {
210 ret = rmi_read(rmi_dev, query_offset, queries);
211 if (ret) {
212 dev_err(&rmi_dev->dev,
213 "Failed to read DS4 queries: %d\n",
214 ret);
215 return ret;
218 has_package_id_query = !!(queries[0] & BIT(0));
219 has_build_id_query = !!(queries[0] & BIT(1));
222 if (has_package_id_query)
223 prod_info_addr++;
225 if (has_build_id_query) {
226 ret = rmi_read_block(rmi_dev, prod_info_addr, queries,
228 if (ret) {
229 dev_err(&rmi_dev->dev,
230 "Failed to read product info: %d\n",
231 ret);
232 return ret;
235 props->firmware_id = queries[1] << 8 | queries[0];
236 props->firmware_id += queries[2] * 65536;
240 return 0;
243 char *rmi_f01_get_product_ID(struct rmi_function *fn)
245 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
247 return f01->properties.product_id;
250 #ifdef CONFIG_OF
251 static int rmi_f01_of_probe(struct device *dev,
252 struct rmi_device_platform_data *pdata)
254 int retval;
255 u32 val;
257 retval = rmi_of_property_read_u32(dev,
258 (u32 *)&pdata->power_management.nosleep,
259 "syna,nosleep-mode", 1);
260 if (retval)
261 return retval;
263 retval = rmi_of_property_read_u32(dev, &val,
264 "syna,wakeup-threshold", 1);
265 if (retval)
266 return retval;
268 pdata->power_management.wakeup_threshold = val;
270 retval = rmi_of_property_read_u32(dev, &val,
271 "syna,doze-holdoff-ms", 1);
272 if (retval)
273 return retval;
275 pdata->power_management.doze_holdoff = val * 100;
277 retval = rmi_of_property_read_u32(dev, &val,
278 "syna,doze-interval-ms", 1);
279 if (retval)
280 return retval;
282 pdata->power_management.doze_interval = val / 10;
284 return 0;
286 #else
287 static inline int rmi_f01_of_probe(struct device *dev,
288 struct rmi_device_platform_data *pdata)
290 return -ENODEV;
292 #endif
294 static int rmi_f01_probe(struct rmi_function *fn)
296 struct rmi_device *rmi_dev = fn->rmi_dev;
297 struct rmi_driver_data *driver_data = dev_get_drvdata(&rmi_dev->dev);
298 struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
299 struct f01_data *f01;
300 int error;
301 u16 ctrl_base_addr = fn->fd.control_base_addr;
302 u8 device_status;
303 u8 temp;
305 if (fn->dev.of_node) {
306 error = rmi_f01_of_probe(&fn->dev, pdata);
307 if (error)
308 return error;
311 f01 = devm_kzalloc(&fn->dev, sizeof(struct f01_data), GFP_KERNEL);
312 if (!f01)
313 return -ENOMEM;
315 f01->num_of_irq_regs = driver_data->num_of_irq_regs;
318 * Set the configured bit and (optionally) other important stuff
319 * in the device control register.
322 error = rmi_read(rmi_dev, fn->fd.control_base_addr,
323 &f01->device_control.ctrl0);
324 if (error) {
325 dev_err(&fn->dev, "Failed to read F01 control: %d\n", error);
326 return error;
329 switch (pdata->power_management.nosleep) {
330 case RMI_F01_NOSLEEP_DEFAULT:
331 break;
332 case RMI_F01_NOSLEEP_OFF:
333 f01->device_control.ctrl0 &= ~RMI_F01_CRTL0_NOSLEEP_BIT;
334 break;
335 case RMI_F01_NOSLEEP_ON:
336 f01->device_control.ctrl0 |= RMI_F01_CRTL0_NOSLEEP_BIT;
337 break;
341 * Sleep mode might be set as a hangover from a system crash or
342 * reboot without power cycle. If so, clear it so the sensor
343 * is certain to function.
345 if ((f01->device_control.ctrl0 & RMI_F01_CTRL0_SLEEP_MODE_MASK) !=
346 RMI_SLEEP_MODE_NORMAL) {
347 dev_warn(&fn->dev,
348 "WARNING: Non-zero sleep mode found. Clearing...\n");
349 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
352 f01->device_control.ctrl0 |= RMI_F01_CRTL0_CONFIGURED_BIT;
354 error = rmi_write(rmi_dev, fn->fd.control_base_addr,
355 f01->device_control.ctrl0);
356 if (error) {
357 dev_err(&fn->dev, "Failed to write F01 control: %d\n", error);
358 return error;
361 /* Dummy read in order to clear irqs */
362 error = rmi_read(rmi_dev, fn->fd.data_base_addr + 1, &temp);
363 if (error < 0) {
364 dev_err(&fn->dev, "Failed to read Interrupt Status.\n");
365 return error;
368 error = rmi_f01_read_properties(rmi_dev, fn->fd.query_base_addr,
369 &f01->properties);
370 if (error < 0) {
371 dev_err(&fn->dev, "Failed to read F01 properties.\n");
372 return error;
375 dev_info(&fn->dev, "found RMI device, manufacturer: %s, product: %s, fw id: %d\n",
376 f01->properties.manufacturer_id == 1 ? "Synaptics" : "unknown",
377 f01->properties.product_id, f01->properties.firmware_id);
379 /* Advance to interrupt control registers, then skip over them. */
380 ctrl_base_addr++;
381 ctrl_base_addr += f01->num_of_irq_regs;
383 /* read control register */
384 if (f01->properties.has_adjustable_doze) {
385 f01->doze_interval_addr = ctrl_base_addr;
386 ctrl_base_addr++;
388 if (pdata->power_management.doze_interval) {
389 f01->device_control.doze_interval =
390 pdata->power_management.doze_interval;
391 error = rmi_write(rmi_dev, f01->doze_interval_addr,
392 f01->device_control.doze_interval);
393 if (error) {
394 dev_err(&fn->dev,
395 "Failed to configure F01 doze interval register: %d\n",
396 error);
397 return error;
399 } else {
400 error = rmi_read(rmi_dev, f01->doze_interval_addr,
401 &f01->device_control.doze_interval);
402 if (error) {
403 dev_err(&fn->dev,
404 "Failed to read F01 doze interval register: %d\n",
405 error);
406 return error;
410 f01->wakeup_threshold_addr = ctrl_base_addr;
411 ctrl_base_addr++;
413 if (pdata->power_management.wakeup_threshold) {
414 f01->device_control.wakeup_threshold =
415 pdata->power_management.wakeup_threshold;
416 error = rmi_write(rmi_dev, f01->wakeup_threshold_addr,
417 f01->device_control.wakeup_threshold);
418 if (error) {
419 dev_err(&fn->dev,
420 "Failed to configure F01 wakeup threshold register: %d\n",
421 error);
422 return error;
424 } else {
425 error = rmi_read(rmi_dev, f01->wakeup_threshold_addr,
426 &f01->device_control.wakeup_threshold);
427 if (error < 0) {
428 dev_err(&fn->dev,
429 "Failed to read F01 wakeup threshold register: %d\n",
430 error);
431 return error;
436 if (f01->properties.has_lts)
437 ctrl_base_addr++;
439 if (f01->properties.has_adjustable_doze_holdoff) {
440 f01->doze_holdoff_addr = ctrl_base_addr;
441 ctrl_base_addr++;
443 if (pdata->power_management.doze_holdoff) {
444 f01->device_control.doze_holdoff =
445 pdata->power_management.doze_holdoff;
446 error = rmi_write(rmi_dev, f01->doze_holdoff_addr,
447 f01->device_control.doze_holdoff);
448 if (error) {
449 dev_err(&fn->dev,
450 "Failed to configure F01 doze holdoff register: %d\n",
451 error);
452 return error;
454 } else {
455 error = rmi_read(rmi_dev, f01->doze_holdoff_addr,
456 &f01->device_control.doze_holdoff);
457 if (error) {
458 dev_err(&fn->dev,
459 "Failed to read F01 doze holdoff register: %d\n",
460 error);
461 return error;
466 error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
467 if (error < 0) {
468 dev_err(&fn->dev,
469 "Failed to read device status: %d\n", error);
470 return error;
473 if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
474 dev_err(&fn->dev,
475 "Device was reset during configuration process, status: %#02x!\n",
476 RMI_F01_STATUS_CODE(device_status));
477 return -EINVAL;
480 dev_set_drvdata(&fn->dev, f01);
482 return 0;
485 static int rmi_f01_config(struct rmi_function *fn)
487 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
488 int error;
490 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
491 f01->device_control.ctrl0);
492 if (error) {
493 dev_err(&fn->dev,
494 "Failed to write device_control register: %d\n", error);
495 return error;
498 if (f01->properties.has_adjustable_doze) {
499 error = rmi_write(fn->rmi_dev, f01->doze_interval_addr,
500 f01->device_control.doze_interval);
501 if (error) {
502 dev_err(&fn->dev,
503 "Failed to write doze interval: %d\n", error);
504 return error;
507 error = rmi_write_block(fn->rmi_dev,
508 f01->wakeup_threshold_addr,
509 &f01->device_control.wakeup_threshold,
510 sizeof(u8));
511 if (error) {
512 dev_err(&fn->dev,
513 "Failed to write wakeup threshold: %d\n",
514 error);
515 return error;
519 if (f01->properties.has_adjustable_doze_holdoff) {
520 error = rmi_write(fn->rmi_dev, f01->doze_holdoff_addr,
521 f01->device_control.doze_holdoff);
522 if (error) {
523 dev_err(&fn->dev,
524 "Failed to write doze holdoff: %d\n", error);
525 return error;
529 return 0;
532 static int rmi_f01_suspend(struct rmi_function *fn)
534 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
535 int error;
537 f01->old_nosleep =
538 f01->device_control.ctrl0 & RMI_F01_CRTL0_NOSLEEP_BIT;
539 f01->device_control.ctrl0 &= ~RMI_F01_CRTL0_NOSLEEP_BIT;
541 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
542 if (device_may_wakeup(fn->rmi_dev->xport->dev))
543 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_RESERVED1;
544 else
545 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_SENSOR_SLEEP;
547 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
548 f01->device_control.ctrl0);
549 if (error) {
550 dev_err(&fn->dev, "Failed to write sleep mode: %d.\n", error);
551 if (f01->old_nosleep)
552 f01->device_control.ctrl0 |= RMI_F01_CRTL0_NOSLEEP_BIT;
553 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
554 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
555 return error;
558 return 0;
561 static int rmi_f01_resume(struct rmi_function *fn)
563 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
564 int error;
566 if (f01->old_nosleep)
567 f01->device_control.ctrl0 |= RMI_F01_CRTL0_NOSLEEP_BIT;
569 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
570 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
572 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
573 f01->device_control.ctrl0);
574 if (error) {
575 dev_err(&fn->dev,
576 "Failed to restore normal operation: %d.\n", error);
577 return error;
580 return 0;
583 static int rmi_f01_attention(struct rmi_function *fn,
584 unsigned long *irq_bits)
586 struct rmi_device *rmi_dev = fn->rmi_dev;
587 int error;
588 u8 device_status;
590 error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
591 if (error) {
592 dev_err(&fn->dev,
593 "Failed to read device status: %d.\n", error);
594 return error;
597 if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
598 dev_warn(&fn->dev, "Device reset detected.\n");
599 error = rmi_dev->driver->reset_handler(rmi_dev);
600 if (error) {
601 dev_err(&fn->dev, "Device reset failed: %d\n", error);
602 return error;
606 return 0;
609 struct rmi_function_handler rmi_f01_handler = {
610 .driver = {
611 .name = "rmi4_f01",
613 * Do not allow user unbinding F01 as it is critical
614 * function.
616 .suppress_bind_attrs = true,
618 .func = 0x01,
619 .probe = rmi_f01_probe,
620 .config = rmi_f01_config,
621 .attention = rmi_f01_attention,
622 .suspend = rmi_f01_suspend,
623 .resume = rmi_f01_resume,