blob: 0a2bd5a0f2b7961e4b4fb97b3e560b26c901da27 [file] [log] [blame]
Andrew Duggan2b6a3212016-03-10 15:35:49 -08001/*
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);