aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Duggan <aduggan@synaptics.com>2016-03-10 18:35:49 -0500
committerDmitry Torokhov <dmitry.torokhov@gmail.com>2016-03-10 19:02:39 -0500
commit2b6a321da9a2d8725a1d3dbb0b2e96a7618ebe56 (patch)
tree6b08b78455038ea08b3c4cd9473d3f580a2fa56b
parentafd2ff9b7e1b367172f18ba7f693dfb62bdcb2dc (diff)
Input: synaptics-rmi4 - add support for Synaptics RMI4 devices
Synaptics uses the Register Mapped Interface (RMI) protocol as a communications interface for their devices. This driver adds the core functionality needed to interface with RMI4 devices. RMI devices can be connected to the host via several transport protocols and can supports a wide variety of functionality defined by RMI functions. Support for transport protocols and RMI functions are implemented in individual drivers. The RMI4 core driver uses a bus architecture to facilitate the various combinations of transport and function drivers needed by a particular device. Signed-off-by: Andrew Duggan <aduggan@synaptics.com> Signed-off-by: Christopher Heiny <cheiny@synaptics.com> Tested-by: Benjamin Tissoires <benjamin.tissoires@redhat.com> Tested-by: Linus Walleij <linus.walleij@linaro.org> Tested-by: Bjorn Andersson <bjorn.andersson@linaro.org> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
-rw-r--r--drivers/input/Kconfig2
-rw-r--r--drivers/input/Makefile2
-rw-r--r--drivers/input/rmi4/Kconfig10
-rw-r--r--drivers/input/rmi4/Makefile2
-rw-r--r--drivers/input/rmi4/rmi_bus.c375
-rw-r--r--drivers/input/rmi4/rmi_bus.h186
-rw-r--r--drivers/input/rmi4/rmi_driver.c1027
-rw-r--r--drivers/input/rmi4/rmi_driver.h103
-rw-r--r--drivers/input/rmi4/rmi_f01.c574
-rw-r--r--include/linux/rmi.h209
-rw-r--r--include/uapi/linux/input.h1
11 files changed, 2491 insertions, 0 deletions
diff --git a/drivers/input/Kconfig b/drivers/input/Kconfig
index a35532ec00e4..6261874c07c9 100644
--- a/drivers/input/Kconfig
+++ b/drivers/input/Kconfig
@@ -201,6 +201,8 @@ source "drivers/input/touchscreen/Kconfig"
201 201
202source "drivers/input/misc/Kconfig" 202source "drivers/input/misc/Kconfig"
203 203
204source "drivers/input/rmi4/Kconfig"
205
204endif 206endif
205 207
206menu "Hardware I/O ports" 208menu "Hardware I/O ports"
diff --git a/drivers/input/Makefile b/drivers/input/Makefile
index 0c9302ca9954..595820bbabe9 100644
--- a/drivers/input/Makefile
+++ b/drivers/input/Makefile
@@ -26,3 +26,5 @@ obj-$(CONFIG_INPUT_TOUCHSCREEN) += touchscreen/
26obj-$(CONFIG_INPUT_MISC) += misc/ 26obj-$(CONFIG_INPUT_MISC) += misc/
27 27
28obj-$(CONFIG_INPUT_APMPOWER) += apm-power.o 28obj-$(CONFIG_INPUT_APMPOWER) += apm-power.o
29
30obj-$(CONFIG_RMI4_CORE) += rmi4/
diff --git a/drivers/input/rmi4/Kconfig b/drivers/input/rmi4/Kconfig
new file mode 100644
index 000000000000..5ea60e338093
--- /dev/null
+++ b/drivers/input/rmi4/Kconfig
@@ -0,0 +1,10 @@
1#
2# RMI4 configuration
3#
4config RMI4_CORE
5 tristate "Synaptics RMI4 bus support"
6 help
7 Say Y here if you want to support the Synaptics RMI4 bus. This is
8 required for all RMI4 device support.
9
10 If unsure, say Y.
diff --git a/drivers/input/rmi4/Makefile b/drivers/input/rmi4/Makefile
new file mode 100644
index 000000000000..12f219779b74
--- /dev/null
+++ b/drivers/input/rmi4/Makefile
@@ -0,0 +1,2 @@
1obj-$(CONFIG_RMI4_CORE) += rmi_core.o
2rmi_core-y := rmi_bus.o rmi_driver.o rmi_f01.o
diff --git a/drivers/input/rmi4/rmi_bus.c b/drivers/input/rmi4/rmi_bus.c
new file mode 100644
index 000000000000..0a2bd5a0f2b7
--- /dev/null
+++ b/drivers/input/rmi4/rmi_bus.c
@@ -0,0 +1,375 @@
1/*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
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 */
9
10#include <linux/kernel.h>
11#include <linux/device.h>
12#include <linux/kconfig.h>
13#include <linux/list.h>
14#include <linux/pm.h>
15#include <linux/rmi.h>
16#include <linux/slab.h>
17#include <linux/types.h>
18#include <linux/of.h>
19#include "rmi_bus.h"
20#include "rmi_driver.h"
21
22static int debug_flags;
23module_param(debug_flags, int, 0644);
24MODULE_PARM_DESC(debug_flags, "control debugging information");
25
26void rmi_dbg(int flags, struct device *dev, const char *fmt, ...)
27{
28 struct va_format vaf;
29 va_list args;
30
31 if (flags & debug_flags) {
32 va_start(args, fmt);
33
34 vaf.fmt = fmt;
35 vaf.va = &args;
36
37 dev_printk(KERN_DEBUG, dev, "%pV", &vaf);
38
39 va_end(args);
40 }
41}
42EXPORT_SYMBOL_GPL(rmi_dbg);
43
44/*
45 * RMI Physical devices
46 *
47 * Physical RMI device consists of several functions serving particular
48 * purpose. For example F11 is a 2D touch sensor while F01 is a generic
49 * function present in every RMI device.
50 */
51
52static void rmi_release_device(struct device *dev)
53{
54 struct rmi_device *rmi_dev = to_rmi_device(dev);
55
56 kfree(rmi_dev);
57}
58
59static struct device_type rmi_device_type = {
60 .name = "rmi4_sensor",
61 .release = rmi_release_device,
62};
63
64bool rmi_is_physical_device(struct device *dev)
65{
66 return dev->type == &rmi_device_type;
67}
68
69/**
70 * rmi_register_transport_device - register a transport device connection
71 * on the RMI bus. Transport drivers provide communication from the devices
72 * on a bus (such as SPI, I2C, and so on) to the RMI4 sensor.
73 *
74 * @xport: the transport device to register
75 */
76int rmi_register_transport_device(struct rmi_transport_dev *xport)
77{
78 static atomic_t transport_device_count = ATOMIC_INIT(0);
79 struct rmi_device *rmi_dev;
80 int error;
81
82 rmi_dev = kzalloc(sizeof(struct rmi_device), GFP_KERNEL);
83 if (!rmi_dev)
84 return -ENOMEM;
85
86 device_initialize(&rmi_dev->dev);
87
88 rmi_dev->xport = xport;
89 rmi_dev->number = atomic_inc_return(&transport_device_count) - 1;
90
91 dev_set_name(&rmi_dev->dev, "rmi4-%02d", rmi_dev->number);
92
93 rmi_dev->dev.bus = &rmi_bus_type;
94 rmi_dev->dev.type = &rmi_device_type;
95
96 xport->rmi_dev = rmi_dev;
97
98 error = device_add(&rmi_dev->dev);
99 if (error)
100 goto err_put_device;
101
102 rmi_dbg(RMI_DEBUG_CORE, xport->dev,
103 "%s: Registered %s as %s.\n", __func__,
104 dev_name(rmi_dev->xport->dev), dev_name(&rmi_dev->dev));
105
106 return 0;
107
108err_put_device:
109 put_device(&rmi_dev->dev);
110 return error;
111}
112EXPORT_SYMBOL_GPL(rmi_register_transport_device);
113
114/**
115 * rmi_unregister_transport_device - unregister a transport device connection
116 * @xport: the transport driver to unregister
117 *
118 */
119void rmi_unregister_transport_device(struct rmi_transport_dev *xport)
120{
121 struct rmi_device *rmi_dev = xport->rmi_dev;
122
123 device_del(&rmi_dev->dev);
124 put_device(&rmi_dev->dev);
125}
126EXPORT_SYMBOL(rmi_unregister_transport_device);
127
128
129/* Function specific stuff */
130
131static void rmi_release_function(struct device *dev)
132{
133 struct rmi_function *fn = to_rmi_function(dev);
134
135 kfree(fn);
136}
137
138static struct device_type rmi_function_type = {
139 .name = "rmi4_function",
140 .release = rmi_release_function,
141};
142
143bool rmi_is_function_device(struct device *dev)
144{
145 return dev->type == &rmi_function_type;
146}
147
148static int rmi_function_match(struct device *dev, struct device_driver *drv)
149{
150 struct rmi_function_handler *handler = to_rmi_function_handler(drv);
151 struct rmi_function *fn = to_rmi_function(dev);
152
153 return fn->fd.function_number == handler->func;
154}
155
156static int rmi_function_probe(struct device *dev)
157{
158 struct rmi_function *fn = to_rmi_function(dev);
159 struct rmi_function_handler *handler =
160 to_rmi_function_handler(dev->driver);
161 int error;
162
163 if (handler->probe) {
164 error = handler->probe(fn);
165 return error;
166 }
167
168 return 0;
169}
170
171static int rmi_function_remove(struct device *dev)
172{
173 struct rmi_function *fn = to_rmi_function(dev);
174 struct rmi_function_handler *handler =
175 to_rmi_function_handler(dev->driver);
176
177 if (handler->remove)
178 handler->remove(fn);
179
180 return 0;
181}
182
183int rmi_register_function(struct rmi_function *fn)
184{
185 struct rmi_device *rmi_dev = fn->rmi_dev;
186 int error;
187
188 device_initialize(&fn->dev);
189
190 dev_set_name(&fn->dev, "%s.fn%02x",
191 dev_name(&rmi_dev->dev), fn->fd.function_number);
192
193 fn->dev.parent = &rmi_dev->dev;
194 fn->dev.type = &rmi_function_type;
195 fn->dev.bus = &rmi_bus_type;
196
197 error = device_add(&fn->dev);
198 if (error) {
199 dev_err(&rmi_dev->dev,
200 "Failed device_register function device %s\n",
201 dev_name(&fn->dev));
202 goto err_put_device;
203 }
204
205 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Registered F%02X.\n",
206 fn->fd.function_number);
207
208 return 0;
209
210err_put_device:
211 put_device(&fn->dev);
212 return error;
213}
214
215void rmi_unregister_function(struct rmi_function *fn)
216{
217 device_del(&fn->dev);
218
219 if (fn->dev.of_node)
220 of_node_put(fn->dev.of_node);
221
222 put_device(&fn->dev);
223}
224
225/**
226 * rmi_register_function_handler - register a handler for an RMI function
227 * @handler: RMI handler that should be registered.
228 * @module: pointer to module that implements the handler
229 * @mod_name: name of the module implementing the handler
230 *
231 * This function performs additional setup of RMI function handler and
232 * registers it with the RMI core so that it can be bound to
233 * RMI function devices.
234 */
235int __rmi_register_function_handler(struct rmi_function_handler *handler,
236 struct module *owner,
237 const char *mod_name)
238{
239 struct device_driver *driver = &handler->driver;
240 int error;
241
242 driver->bus = &rmi_bus_type;
243 driver->owner = owner;
244 driver->mod_name = mod_name;
245 driver->probe = rmi_function_probe;
246 driver->remove = rmi_function_remove;
247
248 error = driver_register(&handler->driver);
249 if (error) {
250 pr_err("driver_register() failed for %s, error: %d\n",
251 handler->driver.name, error);
252 return error;
253 }
254
255 return 0;
256}
257EXPORT_SYMBOL_GPL(__rmi_register_function_handler);
258
259/**
260 * rmi_unregister_function_handler - unregister given RMI function handler
261 * @handler: RMI handler that should be unregistered.
262 *
263 * This function unregisters given function handler from RMI core which
264 * causes it to be unbound from the function devices.
265 */
266void rmi_unregister_function_handler(struct rmi_function_handler *handler)
267{
268 driver_unregister(&handler->driver);
269}
270EXPORT_SYMBOL_GPL(rmi_unregister_function_handler);
271
272/* Bus specific stuff */
273
274static int rmi_bus_match(struct device *dev, struct device_driver *drv)
275{
276 bool physical = rmi_is_physical_device(dev);
277
278 /* First see if types are not compatible */
279 if (physical != rmi_is_physical_driver(drv))
280 return 0;
281
282 return physical || rmi_function_match(dev, drv);
283}
284
285struct bus_type rmi_bus_type = {
286 .match = rmi_bus_match,
287 .name = "rmi4",
288};
289
290static struct rmi_function_handler *fn_handlers[] = {
291 &rmi_f01_handler,
292};
293
294static void __rmi_unregister_function_handlers(int start_idx)
295{
296 int i;
297
298 for (i = start_idx; i >= 0; i--)
299 rmi_unregister_function_handler(fn_handlers[i]);
300}
301
302static void rmi_unregister_function_handlers(void)
303{
304 __rmi_unregister_function_handlers(ARRAY_SIZE(fn_handlers) - 1);
305}
306
307static int rmi_register_function_handlers(void)
308{
309 int ret;
310 int i;
311
312 for (i = 0; i < ARRAY_SIZE(fn_handlers); i++) {
313 ret = rmi_register_function_handler(fn_handlers[i]);
314 if (ret) {
315 pr_err("%s: error registering the RMI F%02x handler: %d\n",
316 __func__, fn_handlers[i]->func, ret);
317 goto err_unregister_function_handlers;
318 }
319 }
320
321 return 0;
322
323err_unregister_function_handlers:
324 __rmi_unregister_function_handlers(i - 1);
325 return ret;
326}
327
328static int __init rmi_bus_init(void)
329{
330 int error;
331
332 error = bus_register(&rmi_bus_type);
333 if (error) {
334 pr_err("%s: error registering the RMI bus: %d\n",
335 __func__, error);
336 return error;
337 }
338
339 error = rmi_register_function_handlers();
340 if (error)
341 goto err_unregister_bus;
342
343 error = rmi_register_physical_driver();
344 if (error) {
345 pr_err("%s: error registering the RMI physical driver: %d\n",
346 __func__, error);
347 goto err_unregister_bus;
348 }
349
350 return 0;
351
352err_unregister_bus:
353 bus_unregister(&rmi_bus_type);
354 return error;
355}
356module_init(rmi_bus_init);
357
358static void __exit rmi_bus_exit(void)
359{
360 /*
361 * We should only ever get here if all drivers are unloaded, so
362 * all we have to do at this point is unregister ourselves.
363 */
364
365 rmi_unregister_physical_driver();
366 rmi_unregister_function_handlers();
367 bus_unregister(&rmi_bus_type);
368}
369module_exit(rmi_bus_exit);
370
371MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com");
372MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com");
373MODULE_DESCRIPTION("RMI bus");
374MODULE_LICENSE("GPL");
375MODULE_VERSION(RMI_DRIVER_VERSION);
diff --git a/drivers/input/rmi4/rmi_bus.h b/drivers/input/rmi4/rmi_bus.h
new file mode 100644
index 000000000000..13b148d44b37
--- /dev/null
+++ b/drivers/input/rmi4/rmi_bus.h
@@ -0,0 +1,186 @@
1/*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
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 */
9
10#ifndef _RMI_BUS_H
11#define _RMI_BUS_H
12
13#include <linux/rmi.h>
14
15struct rmi_device;
16
17/**
18 * struct rmi_function - represents the implementation of an RMI4
19 * function for a particular device (basically, a driver for that RMI4 function)
20 *
21 * @fd: The function descriptor of the RMI function
22 * @rmi_dev: Pointer to the RMI device associated with this function container
23 * @dev: The device associated with this particular function.
24 *
25 * @num_of_irqs: The number of irqs needed by this function
26 * @irq_pos: The position in the irq bitfield this function holds
27 * @irq_mask: For convenience, can be used to mask IRQ bits off during ATTN
28 * interrupt handling.
29 *
30 * @node: entry in device's list of functions
31 */
32struct rmi_function {
33 struct rmi_function_descriptor fd;
34 struct rmi_device *rmi_dev;
35 struct device dev;
36 struct list_head node;
37
38 unsigned int num_of_irqs;
39 unsigned int irq_pos;
40 unsigned long irq_mask[];
41};
42
43#define to_rmi_function(d) container_of(d, struct rmi_function, dev)
44
45bool rmi_is_function_device(struct device *dev);
46
47int __must_check rmi_register_function(struct rmi_function *);
48void rmi_unregister_function(struct rmi_function *);
49
50/**
51 * struct rmi_function_handler - driver routines for a particular RMI function.
52 *
53 * @func: The RMI function number
54 * @reset: Called when a reset of the touch sensor is detected. The routine
55 * should perform any out-of-the-ordinary reset handling that might be
56 * necessary. Restoring of touch sensor configuration registers should be
57 * handled in the config() callback, below.
58 * @config: Called when the function container is first initialized, and
59 * after a reset is detected. This routine should write any necessary
60 * configuration settings to the device.
61 * @attention: Called when the IRQ(s) for the function are set by the touch
62 * sensor.
63 * @suspend: Should perform any required operations to suspend the particular
64 * function.
65 * @resume: Should perform any required operations to resume the particular
66 * function.
67 *
68 * All callbacks are expected to return 0 on success, error code on failure.
69 */
70struct rmi_function_handler {
71 struct device_driver driver;
72
73 u8 func;
74
75 int (*probe)(struct rmi_function *fn);
76 void (*remove)(struct rmi_function *fn);
77 int (*config)(struct rmi_function *fn);
78 int (*reset)(struct rmi_function *fn);
79 int (*attention)(struct rmi_function *fn, unsigned long *irq_bits);
80 int (*suspend)(struct rmi_function *fn);
81 int (*resume)(struct rmi_function *fn);
82};
83
84#define to_rmi_function_handler(d) \
85 container_of(d, struct rmi_function_handler, driver)
86
87int __must_check __rmi_register_function_handler(struct rmi_function_handler *,
88 struct module *, const char *);
89#define rmi_register_function_handler(handler) \
90 __rmi_register_function_handler(handler, THIS_MODULE, KBUILD_MODNAME)
91
92void rmi_unregister_function_handler(struct rmi_function_handler *);
93
94#define to_rmi_driver(d) \
95 container_of(d, struct rmi_driver, driver)
96
97#define to_rmi_device(d) container_of(d, struct rmi_device, dev)
98
99static inline struct rmi_device_platform_data *
100rmi_get_platform_data(struct rmi_device *d)
101{
102 return &d->xport->pdata;
103}
104
105bool rmi_is_physical_device(struct device *dev);
106
107/**
108 * rmi_read - read a single byte
109 * @d: Pointer to an RMI device
110 * @addr: The address to read from
111 * @buf: The read buffer
112 *
113 * Reads a single byte of data using the underlying transport protocol
114 * into memory pointed by @buf. It returns 0 on success or a negative
115 * error code.
116 */
117static inline int rmi_read(struct rmi_device *d, u16 addr, u8 *buf)
118{
119 return d->xport->ops->read_block(d->xport, addr, buf, 1);
120}
121
122/**
123 * rmi_read_block - read a block of bytes
124 * @d: Pointer to an RMI device
125 * @addr: The start address to read from
126 * @buf: The read buffer
127 * @len: Length of the read buffer
128 *
129 * Reads a block of byte data using the underlying transport protocol
130 * into memory pointed by @buf. It returns 0 on success or a negative
131 * error code.
132 */
133static inline int rmi_read_block(struct rmi_device *d, u16 addr,
134 void *buf, size_t len)
135{
136 return d->xport->ops->read_block(d->xport, addr, buf, len);
137}
138
139/**
140 * rmi_write - write a single byte
141 * @d: Pointer to an RMI device
142 * @addr: The address to write to
143 * @data: The data to write
144 *
145 * Writes a single byte using the underlying transport protocol. It
146 * returns zero on success or a negative error code.
147 */
148static inline int rmi_write(struct rmi_device *d, u16 addr, u8 data)
149{
150 return d->xport->ops->write_block(d->xport, addr, &data, 1);
151}
152
153/**
154 * rmi_write_block - write a block of bytes
155 * @d: Pointer to an RMI device
156 * @addr: The start address to write to
157 * @buf: The write buffer
158 * @len: Length of the write buffer
159 *
160 * Writes a block of byte data from buf using the underlaying transport
161 * protocol. It returns the amount of bytes written or a negative error code.
162 */
163static inline int rmi_write_block(struct rmi_device *d, u16 addr,
164 const void *buf, size_t len)
165{
166 return d->xport->ops->write_block(d->xport, addr, buf, len);
167}
168
169int rmi_for_each_dev(void *data, int (*func)(struct device *dev, void *data));
170
171extern struct bus_type rmi_bus_type;
172
173int rmi_of_property_read_u32(struct device *dev, u32 *result,
174 const char *prop, bool optional);
175int rmi_of_property_read_u16(struct device *dev, u16 *result,
176 const char *prop, bool optional);
177int rmi_of_property_read_u8(struct device *dev, u8 *result,
178 const char *prop, bool optional);
179
180#define RMI_DEBUG_CORE BIT(0)
181#define RMI_DEBUG_XPORT BIT(1)
182#define RMI_DEBUG_FN BIT(2)
183#define RMI_DEBUG_2D_SENSOR BIT(3)
184
185void rmi_dbg(int flags, struct device *dev, const char *fmt, ...);
186#endif
diff --git a/drivers/input/rmi4/rmi_driver.c b/drivers/input/rmi4/rmi_driver.c
new file mode 100644
index 000000000000..b0f34b57a126
--- /dev/null
+++ b/drivers/input/rmi4/rmi_driver.c
@@ -0,0 +1,1027 @@
1/*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
5 * This driver provides the core support for a single RMI4-based device.
6 *
7 * The RMI4 specification can be found here (URL split for line length):
8 *
9 * http://www.synaptics.com/sites/default/files/
10 * 511-000136-01-Rev-E-RMI4-Interfacing-Guide.pdf
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published by
14 * the Free Software Foundation.
15 */
16
17#include <linux/bitmap.h>
18#include <linux/delay.h>
19#include <linux/fs.h>
20#include <linux/kconfig.h>
21#include <linux/pm.h>
22#include <linux/slab.h>
23#include <uapi/linux/input.h>
24#include <linux/rmi.h>
25#include "rmi_bus.h"
26#include "rmi_driver.h"
27
28#define HAS_NONSTANDARD_PDT_MASK 0x40
29#define RMI4_MAX_PAGE 0xff
30#define RMI4_PAGE_SIZE 0x100
31#define RMI4_PAGE_MASK 0xFF00
32
33#define RMI_DEVICE_RESET_CMD 0x01
34#define DEFAULT_RESET_DELAY_MS 100
35
36static void rmi_free_function_list(struct rmi_device *rmi_dev)
37{
38 struct rmi_function *fn, *tmp;
39 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
40
41 data->f01_container = NULL;
42
43 /* Doing it in the reverse order so F01 will be removed last */
44 list_for_each_entry_safe_reverse(fn, tmp,
45 &data->function_list, node) {
46 list_del(&fn->node);
47 rmi_unregister_function(fn);
48 }
49}
50
51static int reset_one_function(struct rmi_function *fn)
52{
53 struct rmi_function_handler *fh;
54 int retval = 0;
55
56 if (!fn || !fn->dev.driver)
57 return 0;
58
59 fh = to_rmi_function_handler(fn->dev.driver);
60 if (fh->reset) {
61 retval = fh->reset(fn);
62 if (retval < 0)
63 dev_err(&fn->dev, "Reset failed with code %d.\n",
64 retval);
65 }
66
67 return retval;
68}
69
70static int configure_one_function(struct rmi_function *fn)
71{
72 struct rmi_function_handler *fh;
73 int retval = 0;
74
75 if (!fn || !fn->dev.driver)
76 return 0;
77
78 fh = to_rmi_function_handler(fn->dev.driver);
79 if (fh->config) {
80 retval = fh->config(fn);
81 if (retval < 0)
82 dev_err(&fn->dev, "Config failed with code %d.\n",
83 retval);
84 }
85
86 return retval;
87}
88
89static int rmi_driver_process_reset_requests(struct rmi_device *rmi_dev)
90{
91 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
92 struct rmi_function *entry;
93 int retval;
94
95 list_for_each_entry(entry, &data->function_list, node) {
96 retval = reset_one_function(entry);
97 if (retval < 0)
98 return retval;
99 }
100
101 return 0;
102}
103
104static int rmi_driver_process_config_requests(struct rmi_device *rmi_dev)
105{
106 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
107 struct rmi_function *entry;
108 int retval;
109
110 list_for_each_entry(entry, &data->function_list, node) {
111 retval = configure_one_function(entry);
112 if (retval < 0)
113 return retval;
114 }
115
116 return 0;
117}
118
119static void process_one_interrupt(struct rmi_driver_data *data,
120 struct rmi_function *fn)
121{
122 struct rmi_function_handler *fh;
123
124 if (!fn || !fn->dev.driver)
125 return;
126
127 fh = to_rmi_function_handler(fn->dev.driver);
128 if (fn->irq_mask && fh->attention) {
129 bitmap_and(data->fn_irq_bits, data->irq_status, fn->irq_mask,
130 data->irq_count);
131 if (!bitmap_empty(data->fn_irq_bits, data->irq_count))
132 fh->attention(fn, data->fn_irq_bits);
133 }
134}
135
136int rmi_process_interrupt_requests(struct rmi_device *rmi_dev)
137{
138 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
139 struct device *dev = &rmi_dev->dev;
140 struct rmi_function *entry;
141 int error;
142
143 if (!data)
144 return 0;
145
146 if (!rmi_dev->xport->attn_data) {
147 error = rmi_read_block(rmi_dev,
148 data->f01_container->fd.data_base_addr + 1,
149 data->irq_status, data->num_of_irq_regs);
150 if (error < 0) {
151 dev_err(dev, "Failed to read irqs, code=%d\n", error);
152 return error;
153 }
154 }
155
156 mutex_lock(&data->irq_mutex);
157 bitmap_and(data->irq_status, data->irq_status, data->current_irq_mask,
158 data->irq_count);
159 /*
160 * At this point, irq_status has all bits that are set in the
161 * interrupt status register and are enabled.
162 */
163 mutex_unlock(&data->irq_mutex);
164
165 /*
166 * It would be nice to be able to use irq_chip to handle these
167 * nested IRQs. Unfortunately, most of the current customers for
168 * this driver are using older kernels (3.0.x) that don't support
169 * the features required for that. Once they've shifted to more
170 * recent kernels (say, 3.3 and higher), this should be switched to
171 * use irq_chip.
172 */
173 list_for_each_entry(entry, &data->function_list, node)
174 if (entry->irq_mask)
175 process_one_interrupt(data, entry);
176
177 if (data->input)
178 input_sync(data->input);
179
180 return 0;
181}
182EXPORT_SYMBOL_GPL(rmi_process_interrupt_requests);
183
184static int suspend_one_function(struct rmi_function *fn)
185{
186 struct rmi_function_handler *fh;
187 int retval = 0;
188
189 if (!fn || !fn->dev.driver)
190 return 0;
191
192 fh = to_rmi_function_handler(fn->dev.driver);
193 if (fh->suspend) {
194 retval = fh->suspend(fn);
195 if (retval < 0)
196 dev_err(&fn->dev, "Suspend failed with code %d.\n",
197 retval);
198 }
199
200 return retval;
201}
202
203static int rmi_suspend_functions(struct rmi_device *rmi_dev)
204{
205 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
206 struct rmi_function *entry;
207 int retval;
208
209 list_for_each_entry(entry, &data->function_list, node) {
210 retval = suspend_one_function(entry);
211 if (retval < 0)
212 return retval;
213 }
214
215 return 0;
216}
217
218static int resume_one_function(struct rmi_function *fn)
219{
220 struct rmi_function_handler *fh;
221 int retval = 0;
222
223 if (!fn || !fn->dev.driver)
224 return 0;
225
226 fh = to_rmi_function_handler(fn->dev.driver);
227 if (fh->resume) {
228 retval = fh->resume(fn);
229 if (retval < 0)
230 dev_err(&fn->dev, "Resume failed with code %d.\n",
231 retval);
232 }
233
234 return retval;
235}
236
237static int rmi_resume_functions(struct rmi_device *rmi_dev)
238{
239 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
240 struct rmi_function *entry;
241 int retval;
242
243 list_for_each_entry(entry, &data->function_list, node) {
244 retval = resume_one_function(entry);
245 if (retval < 0)
246 return retval;
247 }
248
249 return 0;
250}
251
252static int enable_sensor(struct rmi_device *rmi_dev)
253{
254 int retval = 0;
255
256 retval = rmi_driver_process_config_requests(rmi_dev);
257 if (retval < 0)
258 return retval;
259
260 return rmi_process_interrupt_requests(rmi_dev);
261}
262
263/**
264 * rmi_driver_set_input_params - set input device id and other data.
265 *
266 * @rmi_dev: Pointer to an RMI device
267 * @input: Pointer to input device
268 *
269 */
270static int rmi_driver_set_input_params(struct rmi_device *rmi_dev,
271 struct input_dev *input)
272{
273 input->name = SYNAPTICS_INPUT_DEVICE_NAME;
274 input->id.vendor = SYNAPTICS_VENDOR_ID;
275 input->id.bustype = BUS_RMI;
276 return 0;
277}
278
279static void rmi_driver_set_input_name(struct rmi_device *rmi_dev,
280 struct input_dev *input)
281{
282 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
283 char *device_name = rmi_f01_get_product_ID(data->f01_container);
284 char *name;
285
286 name = devm_kasprintf(&rmi_dev->dev, GFP_KERNEL,
287 "Synaptics %s", device_name);
288 if (!name)
289 return;
290
291 input->name = name;
292}
293
294static int rmi_driver_set_irq_bits(struct rmi_device *rmi_dev,
295 unsigned long *mask)
296{
297 int error = 0;
298 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
299 struct device *dev = &rmi_dev->dev;
300
301 mutex_lock(&data->irq_mutex);
302 bitmap_or(data->new_irq_mask,
303 data->current_irq_mask, mask, data->irq_count);
304
305 error = rmi_write_block(rmi_dev,
306 data->f01_container->fd.control_base_addr + 1,
307 data->new_irq_mask, data->num_of_irq_regs);
308 if (error < 0) {
309 dev_err(dev, "%s: Failed to change enabled interrupts!",
310 __func__);
311 goto error_unlock;
312 }
313 bitmap_copy(data->current_irq_mask, data->new_irq_mask,
314 data->num_of_irq_regs);
315
316error_unlock:
317 mutex_unlock(&data->irq_mutex);
318 return error;
319}
320
321static int rmi_driver_clear_irq_bits(struct rmi_device *rmi_dev,
322 unsigned long *mask)
323{
324 int error = 0;
325 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
326 struct device *dev = &rmi_dev->dev;
327
328 mutex_lock(&data->irq_mutex);
329 bitmap_andnot(data->new_irq_mask,
330 data->current_irq_mask, mask, data->irq_count);
331
332 error = rmi_write_block(rmi_dev,
333 data->f01_container->fd.control_base_addr + 1,
334 data->new_irq_mask, data->num_of_irq_regs);
335 if (error < 0) {
336 dev_err(dev, "%s: Failed to change enabled interrupts!",
337 __func__);
338 goto error_unlock;
339 }
340 bitmap_copy(data->current_irq_mask, data->new_irq_mask,
341 data->num_of_irq_regs);
342
343error_unlock:
344 mutex_unlock(&data->irq_mutex);
345 return error;
346}
347
348static int rmi_driver_reset_handler(struct rmi_device *rmi_dev)
349{
350 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
351 int error;
352
353 /*
354 * Can get called before the driver is fully ready to deal with
355 * this situation.
356 */
357 if (!data || !data->f01_container) {
358 dev_warn(&rmi_dev->dev,
359 "Not ready to handle reset yet!\n");
360 return 0;
361 }
362
363 error = rmi_read_block(rmi_dev,
364 data->f01_container->fd.control_base_addr + 1,
365 data->current_irq_mask, data->num_of_irq_regs);
366 if (error < 0) {
367 dev_err(&rmi_dev->dev, "%s: Failed to read current IRQ mask.\n",
368 __func__);
369 return error;
370 }
371
372 error = rmi_driver_process_reset_requests(rmi_dev);
373 if (error < 0)
374 return error;
375
376 error = rmi_driver_process_config_requests(rmi_dev);
377 if (error < 0)
378 return error;
379
380 return 0;
381}
382
383int rmi_read_pdt_entry(struct rmi_device *rmi_dev, struct pdt_entry *entry,
384 u16 pdt_address)
385{
386 u8 buf[RMI_PDT_ENTRY_SIZE];
387 int error;
388
389 error = rmi_read_block(rmi_dev, pdt_address, buf, RMI_PDT_ENTRY_SIZE);
390 if (error) {
391 dev_err(&rmi_dev->dev, "Read PDT entry at %#06x failed, code: %d.\n",
392 pdt_address, error);
393 return error;
394 }
395
396 entry->page_start = pdt_address & RMI4_PAGE_MASK;
397 entry->query_base_addr = buf[0];
398 entry->command_base_addr = buf[1];
399 entry->control_base_addr = buf[2];
400 entry->data_base_addr = buf[3];
401 entry->interrupt_source_count = buf[4] & RMI_PDT_INT_SOURCE_COUNT_MASK;
402 entry->function_version = (buf[4] & RMI_PDT_FUNCTION_VERSION_MASK) >> 5;
403 entry->function_number = buf[5];
404
405 return 0;
406}
407EXPORT_SYMBOL_GPL(rmi_read_pdt_entry);
408
409static void rmi_driver_copy_pdt_to_fd(const struct pdt_entry *pdt,
410 struct rmi_function_descriptor *fd)
411{
412 fd->query_base_addr = pdt->query_base_addr + pdt->page_start;
413 fd->command_base_addr = pdt->command_base_addr + pdt->page_start;
414 fd->control_base_addr = pdt->control_base_addr + pdt->page_start;
415 fd->data_base_addr = pdt->data_base_addr + pdt->page_start;
416 fd->function_number = pdt->function_number;
417 fd->interrupt_source_count = pdt->interrupt_source_count;
418 fd->function_version = pdt->function_version;
419}
420
421#define RMI_SCAN_CONTINUE 0
422#define RMI_SCAN_DONE 1
423
424static int rmi_scan_pdt_page(struct rmi_device *rmi_dev,
425 int page,
426 void *ctx,
427 int (*callback)(struct rmi_device *rmi_dev,
428 void *ctx,
429 const struct pdt_entry *entry))
430{
431 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
432 struct pdt_entry pdt_entry;
433 u16 page_start = RMI4_PAGE_SIZE * page;
434 u16 pdt_start = page_start + PDT_START_SCAN_LOCATION;
435 u16 pdt_end = page_start + PDT_END_SCAN_LOCATION;
436 u16 addr;
437 int error;
438 int retval;
439
440 for (addr = pdt_start; addr >= pdt_end; addr -= RMI_PDT_ENTRY_SIZE) {
441 error = rmi_read_pdt_entry(rmi_dev, &pdt_entry, addr);
442 if (error)
443 return error;
444
445 if (RMI4_END_OF_PDT(pdt_entry.function_number))
446 break;
447
448 retval = callback(rmi_dev, ctx, &pdt_entry);
449 if (retval != RMI_SCAN_CONTINUE)
450 return retval;
451 }
452
453 return (data->f01_bootloader_mode || addr == pdt_start) ?
454 RMI_SCAN_DONE : RMI_SCAN_CONTINUE;
455}
456
457static int rmi_scan_pdt(struct rmi_device *rmi_dev, void *ctx,
458 int (*callback)(struct rmi_device *rmi_dev,
459 void *ctx,
460 const struct pdt_entry *entry))
461{
462 int page;
463 int retval = RMI_SCAN_DONE;
464
465 for (page = 0; page <= RMI4_MAX_PAGE; page++) {
466 retval = rmi_scan_pdt_page(rmi_dev, page, ctx, callback);
467 if (retval != RMI_SCAN_CONTINUE)
468 break;
469 }
470
471 return retval < 0 ? retval : 0;
472}
473
474int rmi_read_register_desc(struct rmi_device *d, u16 addr,
475 struct rmi_register_descriptor *rdesc)
476{
477 int ret;
478 u8 size_presence_reg;
479 u8 buf[35];
480 int presense_offset = 1;
481 u8 *struct_buf;
482 int reg;
483 int offset = 0;
484 int map_offset = 0;
485 int i;
486 int b;
487
488 /*
489 * The first register of the register descriptor is the size of
490 * the register descriptor's presense register.
491 */
492 ret = rmi_read(d, addr, &size_presence_reg);
493 if (ret)
494 return ret;
495 ++addr;
496
497 if (size_presence_reg < 0 || size_presence_reg > 35)
498 return -EIO;
499
500 memset(buf, 0, sizeof(buf));
501
502 /*
503 * The presence register contains the size of the register structure
504 * and a bitmap which identified which packet registers are present
505 * for this particular register type (ie query, control, or data).
506 */
507 ret = rmi_read_block(d, addr, buf, size_presence_reg);
508 if (ret)
509 return ret;
510 ++addr;
511
512 if (buf[0] == 0) {
513 presense_offset = 3;
514 rdesc->struct_size = buf[1] | (buf[2] << 8);
515 } else {
516 rdesc->struct_size = buf[0];
517 }
518
519 for (i = presense_offset; i < size_presence_reg; i++) {
520 for (b = 0; b < 8; b++) {
521 if (buf[i] & (0x1 << b))
522 bitmap_set(rdesc->presense_map, map_offset, 1);
523 ++map_offset;
524 }
525 }
526
527 rdesc->num_registers = bitmap_weight(rdesc->presense_map,
528 RMI_REG_DESC_PRESENSE_BITS);
529
530 rdesc->registers = devm_kzalloc(&d->dev, rdesc->num_registers *
531 sizeof(struct rmi_register_desc_item),
532 GFP_KERNEL);
533 if (!rdesc->registers)
534 return -ENOMEM;
535
536 /*
537 * Allocate a temporary buffer to hold the register structure.
538 * I'm not using devm_kzalloc here since it will not be retained
539 * after exiting this function
540 */
541 struct_buf = kzalloc(rdesc->struct_size, GFP_KERNEL);
542 if (!struct_buf)
543 return -ENOMEM;
544
545 /*
546 * The register structure contains information about every packet
547 * register of this type. This includes the size of the packet
548 * register and a bitmap of all subpackets contained in the packet
549 * register.
550 */
551 ret = rmi_read_block(d, addr, struct_buf, rdesc->struct_size);
552 if (ret)
553 goto free_struct_buff;
554
555 reg = find_first_bit(rdesc->presense_map, RMI_REG_DESC_PRESENSE_BITS);
556 map_offset = 0;
557 for (i = 0; i < rdesc->num_registers; i++) {
558 struct rmi_register_desc_item *item = &rdesc->registers[i];
559 int reg_size = struct_buf[offset];
560
561 ++offset;
562 if (reg_size == 0) {
563 reg_size = struct_buf[offset] |
564 (struct_buf[offset + 1] << 8);
565 offset += 2;
566 }
567
568 if (reg_size == 0) {
569 reg_size = struct_buf[offset] |
570 (struct_buf[offset + 1] << 8) |
571 (struct_buf[offset + 2] << 16) |
572 (struct_buf[offset + 3] << 24);
573 offset += 4;
574 }
575
576 item->reg = reg;
577 item->reg_size = reg_size;
578
579 do {
580 for (b = 0; b < 7; b++) {
581 if (struct_buf[offset] & (0x1 << b))
582 bitmap_set(item->subpacket_map,
583 map_offset, 1);
584 ++map_offset;
585 }
586 } while (struct_buf[offset++] & 0x80);
587
588 item->num_subpackets = bitmap_weight(item->subpacket_map,
589 RMI_REG_DESC_SUBPACKET_BITS);
590
591 rmi_dbg(RMI_DEBUG_CORE, &d->dev,
592 "%s: reg: %d reg size: %ld subpackets: %d\n", __func__,
593 item->reg, item->reg_size, item->num_subpackets);
594
595 reg = find_next_bit(rdesc->presense_map,
596 RMI_REG_DESC_PRESENSE_BITS, reg + 1);
597 }
598
599free_struct_buff:
600 kfree(struct_buf);
601 return ret;
602}
603EXPORT_SYMBOL_GPL(rmi_read_register_desc);
604
605const struct rmi_register_desc_item *rmi_get_register_desc_item(
606 struct rmi_register_descriptor *rdesc, u16 reg)
607{
608 const struct rmi_register_desc_item *item;
609 int i;
610
611 for (i = 0; i < rdesc->num_registers; i++) {
612 item = &rdesc->registers[i];
613 if (item->reg == reg)
614 return item;
615 }
616
617 return NULL;
618}
619EXPORT_SYMBOL_GPL(rmi_get_register_desc_item);
620
621size_t rmi_register_desc_calc_size(struct rmi_register_descriptor *rdesc)
622{
623 const struct rmi_register_desc_item *item;
624 int i;
625 size_t size = 0;
626
627 for (i = 0; i < rdesc->num_registers; i++) {
628 item = &rdesc->registers[i];
629 size += item->reg_size;
630 }
631 return size;
632}
633EXPORT_SYMBOL_GPL(rmi_register_desc_calc_size);
634
635/* Compute the register offset relative to the base address */
636int rmi_register_desc_calc_reg_offset(
637 struct rmi_register_descriptor *rdesc, u16 reg)
638{
639 const struct rmi_register_desc_item *item;
640 int offset = 0;
641 int i;
642
643 for (i = 0; i < rdesc->num_registers; i++) {
644 item = &rdesc->registers[i];
645 if (item->reg == reg)
646 return offset;
647 ++offset;
648 }
649 return -1;
650}
651EXPORT_SYMBOL_GPL(rmi_register_desc_calc_reg_offset);
652
653bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item *item,
654 u8 subpacket)
655{
656 return find_next_bit(item->subpacket_map, RMI_REG_DESC_PRESENSE_BITS,
657 subpacket) == subpacket;
658}
659
660/* Indicates that flash programming is enabled (bootloader mode). */
661#define RMI_F01_STATUS_BOOTLOADER(status) (!!((status) & 0x40))
662
663/*
664 * Given the PDT entry for F01, read the device status register to determine
665 * if we're stuck in bootloader mode or not.
666 *
667 */
668static int rmi_check_bootloader_mode(struct rmi_device *rmi_dev,
669 const struct pdt_entry *pdt)
670{
671 int error;
672 u8 device_status;
673
674 error = rmi_read(rmi_dev, pdt->data_base_addr + pdt->page_start,
675 &device_status);
676 if (error) {
677 dev_err(&rmi_dev->dev,
678 "Failed to read device status: %d.\n", error);
679 return error;
680 }
681
682 return RMI_F01_STATUS_BOOTLOADER(device_status);
683}
684
685static int rmi_count_irqs(struct rmi_device *rmi_dev,
686 void *ctx, const struct pdt_entry *pdt)
687{
688 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
689 int *irq_count = ctx;
690
691 *irq_count += pdt->interrupt_source_count;
692 if (pdt->function_number == 0x01) {
693 data->f01_bootloader_mode =
694 rmi_check_bootloader_mode(rmi_dev, pdt);
695 if (data->f01_bootloader_mode)
696 dev_warn(&rmi_dev->dev,
697 "WARNING: RMI4 device is in bootloader mode!\n");
698 }
699
700 return RMI_SCAN_CONTINUE;
701}
702
703static int rmi_initial_reset(struct rmi_device *rmi_dev,
704 void *ctx, const struct pdt_entry *pdt)
705{
706 int error;
707
708 if (pdt->function_number == 0x01) {
709 u16 cmd_addr = pdt->page_start + pdt->command_base_addr;
710 u8 cmd_buf = RMI_DEVICE_RESET_CMD;
711 const struct rmi_device_platform_data *pdata =
712 rmi_get_platform_data(rmi_dev);
713
714 if (rmi_dev->xport->ops->reset) {
715 error = rmi_dev->xport->ops->reset(rmi_dev->xport,
716 cmd_addr);
717 if (error)
718 return error;
719
720 return RMI_SCAN_DONE;
721 }
722
723 error = rmi_write_block(rmi_dev, cmd_addr, &cmd_buf, 1);
724 if (error) {
725 dev_err(&rmi_dev->dev,
726 "Initial reset failed. Code = %d.\n", error);
727 return error;
728 }
729
730 mdelay(pdata->reset_delay_ms ?: DEFAULT_RESET_DELAY_MS);
731
732 return RMI_SCAN_DONE;
733 }
734
735 /* F01 should always be on page 0. If we don't find it there, fail. */
736 return pdt->page_start == 0 ? RMI_SCAN_CONTINUE : -ENODEV;
737}
738
739static int rmi_create_function(struct rmi_device *rmi_dev,
740 void *ctx, const struct pdt_entry *pdt)
741{
742 struct device *dev = &rmi_dev->dev;
743 struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
744 int *current_irq_count = ctx;
745 struct rmi_function *fn;
746 int i;
747 int error;
748
749 rmi_dbg(RMI_DEBUG_CORE, dev, "Initializing F%02X.\n",
750 pdt->function_number);
751
752 fn = kzalloc(sizeof(struct rmi_function) +
753 BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long),
754 GFP_KERNEL);
755 if (!fn) {
756 dev_err(dev, "Failed to allocate memory for F%02X\n",
757 pdt->function_number);
758 return -ENOMEM;
759 }
760
761 INIT_LIST_HEAD(&fn->node);
762 rmi_driver_copy_pdt_to_fd(pdt, &fn->fd);
763
764 fn->rmi_dev = rmi_dev;
765
766 fn->num_of_irqs = pdt->interrupt_source_count;
767 fn->irq_pos = *current_irq_count;
768 *current_irq_count += fn->num_of_irqs;
769
770 for (i = 0; i < fn->num_of_irqs; i++)
771 set_bit(fn->irq_pos + i, fn->irq_mask);
772
773 error = rmi_register_function(fn);
774 if (error)
775 goto err_put_fn;
776
777 if (pdt->function_number == 0x01)
778 data->f01_container = fn;
779
780 list_add_tail(&fn->node, &data->function_list);
781
782 return RMI_SCAN_CONTINUE;
783
784err_put_fn:
785 put_device(&fn->dev);
786 return error;
787}
788
789int rmi_driver_suspend(struct rmi_device *rmi_dev)
790{
791 int retval = 0;
792
793 retval = rmi_suspend_functions(rmi_dev);
794 if (retval)
795 dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
796 retval);
797
798 return retval;
799}
800EXPORT_SYMBOL_GPL(rmi_driver_suspend);
801
802int rmi_driver_resume(struct rmi_device *rmi_dev)
803{
804 int retval;
805
806 retval = rmi_resume_functions(rmi_dev);
807 if (retval)
808 dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
809 retval);
810
811 return retval;
812}
813EXPORT_SYMBOL_GPL(rmi_driver_resume);
814
815static int rmi_driver_remove(struct device *dev)
816{
817 struct rmi_device *rmi_dev = to_rmi_device(dev);
818
819 rmi_free_function_list(rmi_dev);
820
821 return 0;
822}
823
824static int rmi_driver_probe(struct device *dev)
825{
826 struct rmi_driver *rmi_driver;
827 struct rmi_driver_data *data;
828 struct rmi_device_platform_data *pdata;
829 struct rmi_device *rmi_dev;
830 size_t size;
831 void *irq_memory;
832 int irq_count;
833 int retval;
834
835 rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Starting probe.\n",
836 __func__);
837
838 if (!rmi_is_physical_device(dev)) {
839 rmi_dbg(RMI_DEBUG_CORE, dev, "Not a physical device.\n");
840 return -ENODEV;
841 }
842
843 rmi_dev = to_rmi_device(dev);
844 rmi_driver = to_rmi_driver(dev->driver);
845 rmi_dev->driver = rmi_driver;
846
847 pdata = rmi_get_platform_data(rmi_dev);
848
849 data = devm_kzalloc(dev, sizeof(struct rmi_driver_data), GFP_KERNEL);
850 if (!data)
851 return -ENOMEM;
852
853 INIT_LIST_HEAD(&data->function_list);
854 data->rmi_dev = rmi_dev;
855 dev_set_drvdata(&rmi_dev->dev, data);
856
857 /*
858 * Right before a warm boot, the sensor might be in some unusual state,
859 * such as F54 diagnostics, or F34 bootloader mode after a firmware
860 * or configuration update. In order to clear the sensor to a known
861 * state and/or apply any updates, we issue a initial reset to clear any
862 * previous settings and force it into normal operation.
863 *
864 * We have to do this before actually building the PDT because
865 * the reflash updates (if any) might cause various registers to move
866 * around.
867 *
868 * For a number of reasons, this initial reset may fail to return
869 * within the specified time, but we'll still be able to bring up the
870 * driver normally after that failure. This occurs most commonly in
871 * a cold boot situation (where then firmware takes longer to come up
872 * than from a warm boot) and the reset_delay_ms in the platform data
873 * has been set too short to accommodate that. Since the sensor will
874 * eventually come up and be usable, we don't want to just fail here
875 * and leave the customer's device unusable. So we warn them, and
876 * continue processing.
877 */
878 retval = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
879 if (retval < 0)
880 dev_warn(dev, "RMI initial reset failed! Continuing in spite of this.\n");
881
882 retval = rmi_read(rmi_dev, PDT_PROPERTIES_LOCATION, &data->pdt_props);
883 if (retval < 0) {
884 /*
885 * we'll print out a warning and continue since
886 * failure to get the PDT properties is not a cause to fail
887 */
888 dev_warn(dev, "Could not read PDT properties from %#06x (code %d). Assuming 0x00.\n",
889 PDT_PROPERTIES_LOCATION, retval);
890 }
891
892 /*
893 * We need to count the IRQs and allocate their storage before scanning
894 * the PDT and creating the function entries, because adding a new
895 * function can trigger events that result in the IRQ related storage
896 * being accessed.
897 */
898 rmi_dbg(RMI_DEBUG_CORE, dev, "Counting IRQs.\n");
899 irq_count = 0;
900 retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_count_irqs);
901 if (retval < 0) {
902 dev_err(dev, "IRQ counting failed with code %d.\n", retval);
903 goto err;
904 }
905 data->irq_count = irq_count;
906 data->num_of_irq_regs = (data->irq_count + 7) / 8;
907
908 mutex_init(&data->irq_mutex);
909
910 size = BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long);
911 irq_memory = devm_kzalloc(dev, size * 4, GFP_KERNEL);
912 if (!irq_memory) {
913 dev_err(dev, "Failed to allocate memory for irq masks.\n");
914 goto err;
915 }
916
917 data->irq_status = irq_memory + size * 0;
918 data->fn_irq_bits = irq_memory + size * 1;
919 data->current_irq_mask = irq_memory + size * 2;
920 data->new_irq_mask = irq_memory + size * 3;
921
922 if (rmi_dev->xport->input) {
923 /*
924 * The transport driver already has an input device.
925 * In some cases it is preferable to reuse the transport
926 * devices input device instead of creating a new one here.
927 * One example is some HID touchpads report "pass-through"
928 * button events are not reported by rmi registers.
929 */
930 data->input = rmi_dev->xport->input;
931 } else {
932 data->input = devm_input_allocate_device(dev);
933 if (!data->input) {
934 dev_err(dev, "%s: Failed to allocate input device.\n",
935 __func__);
936 retval = -ENOMEM;
937 goto err_destroy_functions;
938 }
939 rmi_driver_set_input_params(rmi_dev, data->input);
940 data->input->phys = devm_kasprintf(dev, GFP_KERNEL,
941 "%s/input0", dev_name(dev));
942 }
943
944 irq_count = 0;
945 rmi_dbg(RMI_DEBUG_CORE, dev, "Creating functions.");
946 retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_create_function);
947 if (retval < 0) {
948 dev_err(dev, "Function creation failed with code %d.\n",
949 retval);
950 goto err_destroy_functions;
951 }
952
953 if (!data->f01_container) {
954 dev_err(dev, "Missing F01 container!\n");
955 retval = -EINVAL;
956 goto err_destroy_functions;
957 }
958
959 retval = rmi_read_block(rmi_dev,
960 data->f01_container->fd.control_base_addr + 1,
961 data->current_irq_mask, data->num_of_irq_regs);
962 if (retval < 0) {
963 dev_err(dev, "%s: Failed to read current IRQ mask.\n",
964 __func__);
965 goto err_destroy_functions;
966 }
967
968 if (data->input) {
969 rmi_driver_set_input_name(rmi_dev, data->input);
970 if (!rmi_dev->xport->input) {
971 if (input_register_device(data->input)) {
972 dev_err(dev, "%s: Failed to register input device.\n",
973 __func__);
974 goto err_destroy_functions;
975 }
976 }
977 }
978
979 if (data->f01_container->dev.driver)
980 /* Driver already bound, so enable ATTN now. */
981 return enable_sensor(rmi_dev);
982
983 return 0;
984
985err_destroy_functions:
986 rmi_free_function_list(rmi_dev);
987err:
988 return retval < 0 ? retval : 0;
989}
990
991static struct rmi_driver rmi_physical_driver = {
992 .driver = {
993 .owner = THIS_MODULE,
994 .name = "rmi4_physical",
995 .bus = &rmi_bus_type,
996 .probe = rmi_driver_probe,
997 .remove = rmi_driver_remove,
998 },
999 .reset_handler = rmi_driver_reset_handler,
1000 .clear_irq_bits = rmi_driver_clear_irq_bits,
1001 .set_irq_bits = rmi_driver_set_irq_bits,
1002 .set_input_params = rmi_driver_set_input_params,
1003};
1004
1005bool rmi_is_physical_driver(struct device_driver *drv)
1006{
1007 return drv == &rmi_physical_driver.driver;
1008}
1009
1010int __init rmi_register_physical_driver(void)
1011{
1012 int error;
1013
1014 error = driver_register(&rmi_physical_driver.driver);
1015 if (error) {
1016 pr_err("%s: driver register failed, code=%d.\n", __func__,
1017 error);
1018 return error;
1019 }
1020
1021 return 0;
1022}
1023
1024void __exit rmi_unregister_physical_driver(void)
1025{
1026 driver_unregister(&rmi_physical_driver.driver);
1027}
diff --git a/drivers/input/rmi4/rmi_driver.h b/drivers/input/rmi4/rmi_driver.h
new file mode 100644
index 000000000000..3ab3f1a8078f
--- /dev/null
+++ b/drivers/input/rmi4/rmi_driver.h
@@ -0,0 +1,103 @@
1/*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
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 */
9
10#ifndef _RMI_DRIVER_H
11#define _RMI_DRIVER_H
12
13#include <linux/ctype.h>
14#include <linux/hrtimer.h>
15#include <linux/ktime.h>
16#include <linux/input.h>
17#include "rmi_bus.h"
18
19#define RMI_DRIVER_VERSION "2.0"
20
21#define SYNAPTICS_INPUT_DEVICE_NAME "Synaptics RMI4 Touch Sensor"
22#define SYNAPTICS_VENDOR_ID 0x06cb
23
24#define GROUP(_attrs) { \
25 .attrs = _attrs, \
26}
27
28#define PDT_PROPERTIES_LOCATION 0x00EF
29#define BSR_LOCATION 0x00FE
30
31#define RMI_PDT_PROPS_HAS_BSR 0x02
32
33#define NAME_BUFFER_SIZE 256
34
35#define RMI_PDT_ENTRY_SIZE 6
36#define RMI_PDT_FUNCTION_VERSION_MASK 0x60
37#define RMI_PDT_INT_SOURCE_COUNT_MASK 0x07
38
39#define PDT_START_SCAN_LOCATION 0x00e9
40#define PDT_END_SCAN_LOCATION 0x0005
41#define RMI4_END_OF_PDT(id) ((id) == 0x00 || (id) == 0xff)
42
43struct pdt_entry {
44 u16 page_start;
45 u8 query_base_addr;
46 u8 command_base_addr;
47 u8 control_base_addr;
48 u8 data_base_addr;
49 u8 interrupt_source_count;
50 u8 function_version;
51 u8 function_number;
52};
53
54int rmi_read_pdt_entry(struct rmi_device *rmi_dev, struct pdt_entry *entry,
55 u16 pdt_address);
56
57#define RMI_REG_DESC_PRESENSE_BITS (32 * BITS_PER_BYTE)
58#define RMI_REG_DESC_SUBPACKET_BITS (37 * BITS_PER_BYTE)
59
60/* describes a single packet register */
61struct rmi_register_desc_item {
62 u16 reg;
63 unsigned long reg_size;
64 u8 num_subpackets;
65 unsigned long subpacket_map[BITS_TO_LONGS(
66 RMI_REG_DESC_SUBPACKET_BITS)];
67};
68
69/*
70 * describes the packet registers for a particular type
71 * (ie query, control, data)
72 */
73struct rmi_register_descriptor {
74 unsigned long struct_size;
75 unsigned long presense_map[BITS_TO_LONGS(RMI_REG_DESC_PRESENSE_BITS)];
76 u8 num_registers;
77 struct rmi_register_desc_item *registers;
78};
79
80int rmi_read_register_desc(struct rmi_device *d, u16 addr,
81 struct rmi_register_descriptor *rdesc);
82const struct rmi_register_desc_item *rmi_get_register_desc_item(
83 struct rmi_register_descriptor *rdesc, u16 reg);
84
85/*
86 * Calculate the total size of all of the registers described in the
87 * descriptor.
88 */
89size_t rmi_register_desc_calc_size(struct rmi_register_descriptor *rdesc);
90int rmi_register_desc_calc_reg_offset(
91 struct rmi_register_descriptor *rdesc, u16 reg);
92bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item *item,
93 u8 subpacket);
94
95bool rmi_is_physical_driver(struct device_driver *);
96int rmi_register_physical_driver(void);
97void rmi_unregister_physical_driver(void);
98
99char *rmi_f01_get_product_ID(struct rmi_function *fn);
100
101extern struct rmi_function_handler rmi_f01_handler;
102
103#endif
diff --git a/drivers/input/rmi4/rmi_f01.c b/drivers/input/rmi4/rmi_f01.c
new file mode 100644
index 000000000000..e50ecc6699e7
--- /dev/null
+++ b/drivers/input/rmi4/rmi_f01.c
@@ -0,0 +1,574 @@
1/*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
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 */
9
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"
17
18#define RMI_PRODUCT_ID_LENGTH 10
19#define RMI_PRODUCT_INFO_LENGTH 2
20
21#define RMI_DATE_CODE_LENGTH 3
22
23#define PRODUCT_ID_OFFSET 0x10
24#define PRODUCT_INFO_OFFSET 0x1E
25
26
27/* Force a firmware reset of the sensor */
28#define RMI_F01_CMD_DEVICE_RESET 1
29
30/* Various F01_RMI_QueryX bits */
31
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)
40
41#define RMI_F01_QRY5_YEAR_MASK 0x1f
42#define RMI_F01_QRY6_MONTH_MASK 0x0f
43#define RMI_F01_QRY7_DAY_MASK 0x1f
44
45#define RMI_F01_QRY2_PRODINFO_MASK 0x7f
46
47#define RMI_F01_BASIC_QUERY_LEN 21 /* From Query 00 through 20 */
48
49struct 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;
58};
59
60/* F01 device status bits */
61
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))
66
67/* Control register bits */
68
69/*
70 * Sleep mode controls power management on the device and affects all
71 * functions of the device.
72 */
73#define RMI_F01_CTRL0_SLEEP_MODE_MASK 0x03
74
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
79
80/*
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.
83 */
84#define RMI_F01_CRTL0_NOSLEEP_BIT BIT(2)
85
86/*
87 * When this bit is set, the touch controller employs a noise-filtering
88 * algorithm designed for use with a connected battery charger.
89 */
90#define RMI_F01_CRTL0_CHARGER_BIT BIT(5)
91
92/*
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.
96 */
97#define RMI_F01_CRTL0_REPORTRATE_BIT BIT(6)
98
99/*
100 * Written by the host as an indicator that the device has been
101 * successfully configured.
102 */
103#define RMI_F01_CRTL0_CONFIGURED_BIT BIT(7)
104
105/**
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.
113 */
114struct f01_device_control {
115 u8 ctrl0;
116 u8 doze_interval;
117 u8 wakeup_threshold;
118 u8 doze_holdoff;
119};
120
121struct f01_data {
122 struct f01_basic_properties properties;
123 struct f01_device_control device_control;
124
125 u16 doze_interval_addr;
126 u16 wakeup_threshold_addr;
127 u16 doze_holdoff_addr;
128
129 bool suspended;
130 bool old_nosleep;
131
132 unsigned int num_of_irq_regs;
133};
134
135static int rmi_f01_read_properties(struct rmi_device *rmi_dev,
136 u16 query_base_addr,
137 struct f01_basic_properties *props)
138{
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;
149
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;
156 }
157
158 prod_info_addr = query_offset + 17;
159 query_offset += RMI_F01_BASIC_QUERY_LEN;
160
161 /* Now parse what we got */
162 props->manufacturer_id = queries[0];
163
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;
171
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);
176
177 memcpy(props->product_id, &queries[11],
178 RMI_PRODUCT_ID_LENGTH);
179 props->product_id[RMI_PRODUCT_ID_LENGTH] = '\0';
180
181 props->productinfo =
182 ((queries[2] & RMI_F01_QRY2_PRODINFO_MASK) << 7) |
183 (queries[3] & RMI_F01_QRY2_PRODINFO_MASK);
184
185 if (has_sensor_id)
186 query_offset++;
187
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;
194 }
195
196 has_ds4_queries = !!(queries[0] & BIT(0));
197 query_offset++;
198 }
199
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;
206 }
207 query_offset++;
208
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;
216 }
217
218 has_package_id_query = !!(queries[0] & BIT(0));
219 has_build_id_query = !!(queries[0] & BIT(1));
220 }
221
222 if (has_package_id_query)
223 prod_info_addr++;
224
225 if (has_build_id_query) {
226 ret = rmi_read_block(rmi_dev, prod_info_addr, queries,
227 3);
228 if (ret) {
229 dev_err(&rmi_dev->dev,
230 "Failed to read product info: %d\n",
231 ret);
232 return ret;
233 }
234
235 props->firmware_id = queries[1] << 8 | queries[0];
236 props->firmware_id += queries[2] * 65536;
237 }
238 }
239
240 return 0;
241}
242
243char *rmi_f01_get_product_ID(struct rmi_function *fn)
244{
245 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
246
247 return f01->properties.product_id;
248}
249
250static int rmi_f01_probe(struct rmi_function *fn)
251{
252 struct rmi_device *rmi_dev = fn->rmi_dev;
253 struct rmi_driver_data *driver_data = dev_get_drvdata(&rmi_dev->dev);
254 struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
255 struct f01_data *f01;
256 int error;
257 u16 ctrl_base_addr = fn->fd.control_base_addr;
258 u8 device_status;
259 u8 temp;
260
261 f01 = devm_kzalloc(&fn->dev, sizeof(struct f01_data), GFP_KERNEL);
262 if (!f01)
263 return -ENOMEM;
264
265 f01->num_of_irq_regs = driver_data->num_of_irq_regs;
266
267 /*
268 * Set the configured bit and (optionally) other important stuff
269 * in the device control register.
270 */
271
272 error = rmi_read(rmi_dev, fn->fd.control_base_addr,
273 &f01->device_control.ctrl0);
274 if (error) {
275 dev_err(&fn->dev, "Failed to read F01 control: %d\n", error);
276 return error;
277 }
278
279 switch (pdata->power_management.nosleep) {
280 case RMI_F01_NOSLEEP_DEFAULT:
281 break;
282 case RMI_F01_NOSLEEP_OFF:
283 f01->device_control.ctrl0 &= ~RMI_F01_CRTL0_NOSLEEP_BIT;
284 break;
285 case RMI_F01_NOSLEEP_ON:
286 f01->device_control.ctrl0 |= RMI_F01_CRTL0_NOSLEEP_BIT;
287 break;
288 }
289
290 /*
291 * Sleep mode might be set as a hangover from a system crash or
292 * reboot without power cycle. If so, clear it so the sensor
293 * is certain to function.
294 */
295 if ((f01->device_control.ctrl0 & RMI_F01_CTRL0_SLEEP_MODE_MASK) !=
296 RMI_SLEEP_MODE_NORMAL) {
297 dev_warn(&fn->dev,
298 "WARNING: Non-zero sleep mode found. Clearing...\n");
299 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
300 }
301
302 f01->device_control.ctrl0 |= RMI_F01_CRTL0_CONFIGURED_BIT;
303
304 error = rmi_write(rmi_dev, fn->fd.control_base_addr,
305 f01->device_control.ctrl0);
306 if (error) {
307 dev_err(&fn->dev, "Failed to write F01 control: %d\n", error);
308 return error;
309 }
310
311 /* Dummy read in order to clear irqs */
312 error = rmi_read(rmi_dev, fn->fd.data_base_addr + 1, &temp);
313 if (error < 0) {
314 dev_err(&fn->dev, "Failed to read Interrupt Status.\n");
315 return error;
316 }
317
318 error = rmi_f01_read_properties(rmi_dev, fn->fd.query_base_addr,
319 &f01->properties);
320 if (error < 0) {
321 dev_err(&fn->dev, "Failed to read F01 properties.\n");
322 return error;
323 }
324
325 dev_info(&fn->dev, "found RMI device, manufacturer: %s, product: %s, fw id: %d\n",
326 f01->properties.manufacturer_id == 1 ? "Synaptics" : "unknown",
327 f01->properties.product_id, f01->properties.firmware_id);
328
329 /* Advance to interrupt control registers, then skip over them. */
330 ctrl_base_addr++;
331 ctrl_base_addr += f01->num_of_irq_regs;
332
333 /* read control register */
334 if (f01->properties.has_adjustable_doze) {
335 f01->doze_interval_addr = ctrl_base_addr;
336 ctrl_base_addr++;
337
338 if (pdata->power_management.doze_interval) {
339 f01->device_control.doze_interval =
340 pdata->power_management.doze_interval;
341 error = rmi_write(rmi_dev, f01->doze_interval_addr,
342 f01->device_control.doze_interval);
343 if (error) {
344 dev_err(&fn->dev,
345 "Failed to configure F01 doze interval register: %d\n",
346 error);
347 return error;
348 }
349 } else {
350 error = rmi_read(rmi_dev, f01->doze_interval_addr,
351 &f01->device_control.doze_interval);
352 if (error) {
353 dev_err(&fn->dev,
354 "Failed to read F01 doze interval register: %d\n",
355 error);
356 return error;
357 }
358 }
359
360 f01->wakeup_threshold_addr = ctrl_base_addr;
361 ctrl_base_addr++;
362
363 if (pdata->power_management.wakeup_threshold) {
364 f01->device_control.wakeup_threshold =
365 pdata->power_management.wakeup_threshold;
366 error = rmi_write(rmi_dev, f01->wakeup_threshold_addr,
367 f01->device_control.wakeup_threshold);
368 if (error) {
369 dev_err(&fn->dev,
370 "Failed to configure F01 wakeup threshold register: %d\n",
371 error);
372 return error;
373 }
374 } else {
375 error = rmi_read(rmi_dev, f01->wakeup_threshold_addr,
376 &f01->device_control.wakeup_threshold);
377 if (error < 0) {
378 dev_err(&fn->dev,
379 "Failed to read F01 wakeup threshold register: %d\n",
380 error);
381 return error;
382 }
383 }
384 }
385
386 if (f01->properties.has_lts)
387 ctrl_base_addr++;
388
389 if (f01->properties.has_adjustable_doze_holdoff) {
390 f01->doze_holdoff_addr = ctrl_base_addr;
391 ctrl_base_addr++;
392
393 if (pdata->power_management.doze_holdoff) {
394 f01->device_control.doze_holdoff =
395 pdata->power_management.doze_holdoff;
396 error = rmi_write(rmi_dev, f01->doze_holdoff_addr,
397 f01->device_control.doze_holdoff);
398 if (error) {
399 dev_err(&fn->dev,
400 "Failed to configure F01 doze holdoff register: %d\n",
401 error);
402 return error;
403 }
404 } else {
405 error = rmi_read(rmi_dev, f01->doze_holdoff_addr,
406 &f01->device_control.doze_holdoff);
407 if (error) {
408 dev_err(&fn->dev,
409 "Failed to read F01 doze holdoff register: %d\n",
410 error);
411 return error;
412 }
413 }
414 }
415
416 error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
417 if (error < 0) {
418 dev_err(&fn->dev,
419 "Failed to read device status: %d\n", error);
420 return error;
421 }
422
423 if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
424 dev_err(&fn->dev,
425 "Device was reset during configuration process, status: %#02x!\n",
426 RMI_F01_STATUS_CODE(device_status));
427 return -EINVAL;
428 }
429
430 dev_set_drvdata(&fn->dev, f01);
431
432 return 0;
433}
434
435static int rmi_f01_config(struct rmi_function *fn)
436{
437 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
438 int error;
439
440 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
441 f01->device_control.ctrl0);
442 if (error) {
443 dev_err(&fn->dev,
444 "Failed to write device_control register: %d\n", error);
445 return error;
446 }
447
448 if (f01->properties.has_adjustable_doze) {
449 error = rmi_write(fn->rmi_dev, f01->doze_interval_addr,
450 f01->device_control.doze_interval);
451 if (error) {
452 dev_err(&fn->dev,
453 "Failed to write doze interval: %d\n", error);
454 return error;
455 }
456
457 error = rmi_write_block(fn->rmi_dev,
458 f01->wakeup_threshold_addr,
459 &f01->device_control.wakeup_threshold,
460 sizeof(u8));
461 if (error) {
462 dev_err(&fn->dev,
463 "Failed to write wakeup threshold: %d\n",
464 error);
465 return error;
466 }
467 }
468
469 if (f01->properties.has_adjustable_doze_holdoff) {
470 error = rmi_write(fn->rmi_dev, f01->doze_holdoff_addr,
471 f01->device_control.doze_holdoff);
472 if (error) {
473 dev_err(&fn->dev,
474 "Failed to write doze holdoff: %d\n", error);
475 return error;
476 }
477 }
478
479 return 0;
480}
481
482static int rmi_f01_suspend(struct rmi_function *fn)
483{
484 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
485 int error;
486
487 f01->old_nosleep =
488 f01->device_control.ctrl0 & RMI_F01_CRTL0_NOSLEEP_BIT;
489 f01->device_control.ctrl0 &= ~RMI_F01_CRTL0_NOSLEEP_BIT;
490
491 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
492 if (device_may_wakeup(fn->rmi_dev->xport->dev))
493 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_RESERVED1;
494 else
495 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_SENSOR_SLEEP;
496
497 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
498 f01->device_control.ctrl0);
499 if (error) {
500 dev_err(&fn->dev, "Failed to write sleep mode: %d.\n", error);
501 if (f01->old_nosleep)
502 f01->device_control.ctrl0 |= RMI_F01_CRTL0_NOSLEEP_BIT;
503 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
504 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
505 return error;
506 }
507
508 return 0;
509}
510
511static int rmi_f01_resume(struct rmi_function *fn)
512{
513 struct f01_data *f01 = dev_get_drvdata(&fn->dev);
514 int error;
515
516 if (f01->old_nosleep)
517 f01->device_control.ctrl0 |= RMI_F01_CRTL0_NOSLEEP_BIT;
518
519 f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
520 f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
521
522 error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
523 f01->device_control.ctrl0);
524 if (error) {
525 dev_err(&fn->dev,
526 "Failed to restore normal operation: %d.\n", error);
527 return error;
528 }
529
530 return 0;
531}
532
533static int rmi_f01_attention(struct rmi_function *fn,
534 unsigned long *irq_bits)
535{
536 struct rmi_device *rmi_dev = fn->rmi_dev;
537 int error;
538 u8 device_status;
539
540 error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
541 if (error) {
542 dev_err(&fn->dev,
543 "Failed to read device status: %d.\n", error);
544 return error;
545 }
546
547 if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
548 dev_warn(&fn->dev, "Device reset detected.\n");
549 error = rmi_dev->driver->reset_handler(rmi_dev);
550 if (error) {
551 dev_err(&fn->dev, "Device reset failed: %d\n", error);
552 return error;
553 }
554 }
555
556 return 0;
557}
558
559struct rmi_function_handler rmi_f01_handler = {
560 .driver = {
561 .name = "rmi4_f01",
562 /*
563 * Do not allow user unbinding F01 as it is critical
564 * function.
565 */
566 .suppress_bind_attrs = true,
567 },
568 .func = 0x01,
569 .probe = rmi_f01_probe,
570 .config = rmi_f01_config,
571 .attention = rmi_f01_attention,
572 .suspend = rmi_f01_suspend,
573 .resume = rmi_f01_resume,
574};
diff --git a/include/linux/rmi.h b/include/linux/rmi.h
new file mode 100644
index 000000000000..c748fa302067
--- /dev/null
+++ b/include/linux/rmi.h
@@ -0,0 +1,209 @@
1/*
2 * Copyright (c) 2011-2016 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
4 *
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 */
9
10#ifndef _RMI_H
11#define _RMI_H
12#include <linux/kernel.h>
13#include <linux/device.h>
14#include <linux/interrupt.h>
15#include <linux/input.h>
16#include <linux/list.h>
17#include <linux/module.h>
18#include <linux/types.h>
19
20#define NAME_BUFFER_SIZE 256
21
22/**
23 * struct rmi_f01_power - override default power management settings.
24 *
25 */
26enum rmi_f01_nosleep {
27 RMI_F01_NOSLEEP_DEFAULT = 0,
28 RMI_F01_NOSLEEP_OFF = 1,
29 RMI_F01_NOSLEEP_ON = 2
30};
31
32/**
33 * struct rmi_f01_power_management -When non-zero, these values will be written
34 * to the touch sensor to override the default firmware settigns. For a
35 * detailed explanation of what each field does, see the corresponding
36 * documention in the RMI4 specification.
37 *
38 * @nosleep - specifies whether the device is permitted to sleep or doze (that
39 * is, enter a temporary low power state) when no fingers are touching the
40 * sensor.
41 * @wakeup_threshold - controls the capacitance threshold at which the touch
42 * sensor will decide to wake up from that low power state.
43 * @doze_holdoff - controls how long the touch sensor waits after the last
44 * finger lifts before entering the doze state, in units of 100ms.
45 * @doze_interval - controls the interval between checks for finger presence
46 * when the touch sensor is in doze mode, in units of 10ms.
47 */
48struct rmi_f01_power_management {
49 enum rmi_f01_nosleep nosleep;
50 u8 wakeup_threshold;
51 u8 doze_holdoff;
52 u8 doze_interval;
53};
54
55/**
56 * struct rmi_device_platform_data - system specific configuration info.
57 *
58 * @reset_delay_ms - after issuing a reset command to the touch sensor, the
59 * driver waits a few milliseconds to give the firmware a chance to
60 * to re-initialize. You can override the default wait period here.
61 */
62struct rmi_device_platform_data {
63 int reset_delay_ms;
64
65 /* function handler pdata */
66 struct rmi_f01_power_management power_management;
67};
68
69/**
70 * struct rmi_function_descriptor - RMI function base addresses
71 *
72 * @query_base_addr: The RMI Query base address
73 * @command_base_addr: The RMI Command base address
74 * @control_base_addr: The RMI Control base address
75 * @data_base_addr: The RMI Data base address
76 * @interrupt_source_count: The number of irqs this RMI function needs
77 * @function_number: The RMI function number
78 *
79 * This struct is used when iterating the Page Description Table. The addresses
80 * are 16-bit values to include the current page address.
81 *
82 */
83struct rmi_function_descriptor {
84 u16 query_base_addr;
85 u16 command_base_addr;
86 u16 control_base_addr;
87 u16 data_base_addr;
88 u8 interrupt_source_count;
89 u8 function_number;
90 u8 function_version;
91};
92
93struct rmi_device;
94
95/**
96 * struct rmi_transport_dev - represent an RMI transport device
97 *
98 * @dev: Pointer to the communication device, e.g. i2c or spi
99 * @rmi_dev: Pointer to the RMI device
100 * @proto_name: name of the transport protocol (SPI, i2c, etc)
101 * @ops: pointer to transport operations implementation
102 *
103 * The RMI transport device implements the glue between different communication
104 * buses such as I2C and SPI.
105 *
106 */
107struct rmi_transport_dev {
108 struct device *dev;
109 struct rmi_device *rmi_dev;
110
111 const char *proto_name;
112 const struct rmi_transport_ops *ops;
113
114 struct rmi_device_platform_data pdata;
115
116 struct input_dev *input;
117
118 void *attn_data;
119 int attn_size;
120};
121
122/**
123 * struct rmi_transport_ops - defines transport protocol operations.
124 *
125 * @write_block: Writing a block of data to the specified address
126 * @read_block: Read a block of data from the specified address.
127 */
128struct rmi_transport_ops {
129 int (*write_block)(struct rmi_transport_dev *xport, u16 addr,
130 const void *buf, size_t len);
131 int (*read_block)(struct rmi_transport_dev *xport, u16 addr,
132 void *buf, size_t len);
133 int (*reset)(struct rmi_transport_dev *xport, u16 reset_addr);
134};
135
136/**
137 * struct rmi_driver - driver for an RMI4 sensor on the RMI bus.
138 *
139 * @driver: Device driver model driver
140 * @reset_handler: Called when a reset is detected.
141 * @clear_irq_bits: Clear the specified bits in the current interrupt mask.
142 * @set_irq_bist: Set the specified bits in the current interrupt mask.
143 * @store_productid: Callback for cache product id from function 01
144 * @data: Private data pointer
145 *
146 */
147struct rmi_driver {
148 struct device_driver driver;
149
150 int (*reset_handler)(struct rmi_device *rmi_dev);
151 int (*clear_irq_bits)(struct rmi_device *rmi_dev, unsigned long *mask);
152 int (*set_irq_bits)(struct rmi_device *rmi_dev, unsigned long *mask);
153 int (*store_productid)(struct rmi_device *rmi_dev);
154 int (*set_input_params)(struct rmi_device *rmi_dev,
155 struct input_dev *input);
156 void *data;
157};
158
159/**
160 * struct rmi_device - represents an RMI4 sensor device on the RMI bus.
161 *
162 * @dev: The device created for the RMI bus
163 * @number: Unique number for the device on the bus.
164 * @driver: Pointer to associated driver
165 * @xport: Pointer to the transport interface
166 *
167 */
168struct rmi_device {
169 struct device dev;
170 int number;
171
172 struct rmi_driver *driver;
173 struct rmi_transport_dev *xport;
174
175};
176
177struct rmi_driver_data {
178 struct list_head function_list;
179
180 struct rmi_device *rmi_dev;
181
182 struct rmi_function *f01_container;
183 bool f01_bootloader_mode;
184
185 u32 attn_count;
186 int num_of_irq_regs;
187 int irq_count;
188 unsigned long *irq_status;
189 unsigned long *fn_irq_bits;
190 unsigned long *current_irq_mask;
191 unsigned long *new_irq_mask;
192 struct mutex irq_mutex;
193 struct input_dev *input;
194
195 u8 pdt_props;
196 u8 bsr;
197
198 bool enabled;
199
200 void *data;
201};
202
203int rmi_register_transport_device(struct rmi_transport_dev *xport);
204void rmi_unregister_transport_device(struct rmi_transport_dev *xport);
205int rmi_process_interrupt_requests(struct rmi_device *rmi_dev);
206
207int rmi_driver_suspend(struct rmi_device *rmi_dev);
208int rmi_driver_resume(struct rmi_device *rmi_dev);
209#endif
diff --git a/include/uapi/linux/input.h b/include/uapi/linux/input.h
index 2758687300b4..01113841190d 100644
--- a/include/uapi/linux/input.h
+++ b/include/uapi/linux/input.h
@@ -246,6 +246,7 @@ struct input_mask {
246#define BUS_GSC 0x1A 246#define BUS_GSC 0x1A
247#define BUS_ATARI 0x1B 247#define BUS_ATARI 0x1B
248#define BUS_SPI 0x1C 248#define BUS_SPI 0x1C
249#define BUS_RMI 0x1D
249 250
250/* 251/*
251 * MT_TOOL types 252 * MT_TOOL types