Merge tag 'ceph-for-4.13-rc8' of git://github.com/ceph/ceph-client
[linux/fpc-iii.git] / drivers / input / rmi4 / rmi_bus.c
blobae1bffe45c7511fdf065809b26267710dc5bfaac
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/device.h>
12 #include <linux/list.h>
13 #include <linux/pm.h>
14 #include <linux/rmi.h>
15 #include <linux/slab.h>
16 #include <linux/types.h>
17 #include <linux/of.h>
18 #include "rmi_bus.h"
19 #include "rmi_driver.h"
21 static int debug_flags;
22 module_param(debug_flags, int, 0644);
23 MODULE_PARM_DESC(debug_flags, "control debugging information");
25 void rmi_dbg(int flags, struct device *dev, const char *fmt, ...)
27 struct va_format vaf;
28 va_list args;
30 if (flags & debug_flags) {
31 va_start(args, fmt);
33 vaf.fmt = fmt;
34 vaf.va = &args;
36 dev_printk(KERN_DEBUG, dev, "%pV", &vaf);
38 va_end(args);
41 EXPORT_SYMBOL_GPL(rmi_dbg);
44 * RMI Physical devices
46 * Physical RMI device consists of several functions serving particular
47 * purpose. For example F11 is a 2D touch sensor while F01 is a generic
48 * function present in every RMI device.
51 static void rmi_release_device(struct device *dev)
53 struct rmi_device *rmi_dev = to_rmi_device(dev);
55 kfree(rmi_dev);
58 static const struct device_type rmi_device_type = {
59 .name = "rmi4_sensor",
60 .release = rmi_release_device,
63 bool rmi_is_physical_device(struct device *dev)
65 return dev->type == &rmi_device_type;
68 /**
69 * rmi_register_transport_device - register a transport device connection
70 * on the RMI bus. Transport drivers provide communication from the devices
71 * on a bus (such as SPI, I2C, and so on) to the RMI4 sensor.
73 * @xport: the transport device to register
75 int rmi_register_transport_device(struct rmi_transport_dev *xport)
77 static atomic_t transport_device_count = ATOMIC_INIT(0);
78 struct rmi_device *rmi_dev;
79 int error;
81 rmi_dev = kzalloc(sizeof(struct rmi_device), GFP_KERNEL);
82 if (!rmi_dev)
83 return -ENOMEM;
85 device_initialize(&rmi_dev->dev);
87 rmi_dev->xport = xport;
88 rmi_dev->number = atomic_inc_return(&transport_device_count) - 1;
90 dev_set_name(&rmi_dev->dev, "rmi4-%02d", rmi_dev->number);
92 rmi_dev->dev.bus = &rmi_bus_type;
93 rmi_dev->dev.type = &rmi_device_type;
95 xport->rmi_dev = rmi_dev;
97 error = device_add(&rmi_dev->dev);
98 if (error)
99 goto err_put_device;
101 rmi_dbg(RMI_DEBUG_CORE, xport->dev,
102 "%s: Registered %s as %s.\n", __func__,
103 dev_name(rmi_dev->xport->dev), dev_name(&rmi_dev->dev));
105 return 0;
107 err_put_device:
108 put_device(&rmi_dev->dev);
109 return error;
111 EXPORT_SYMBOL_GPL(rmi_register_transport_device);
114 * rmi_unregister_transport_device - unregister a transport device connection
115 * @xport: the transport driver to unregister
118 void rmi_unregister_transport_device(struct rmi_transport_dev *xport)
120 struct rmi_device *rmi_dev = xport->rmi_dev;
122 device_del(&rmi_dev->dev);
123 put_device(&rmi_dev->dev);
125 EXPORT_SYMBOL(rmi_unregister_transport_device);
128 /* Function specific stuff */
130 static void rmi_release_function(struct device *dev)
132 struct rmi_function *fn = to_rmi_function(dev);
134 kfree(fn);
137 static const struct device_type rmi_function_type = {
138 .name = "rmi4_function",
139 .release = rmi_release_function,
142 bool rmi_is_function_device(struct device *dev)
144 return dev->type == &rmi_function_type;
147 static int rmi_function_match(struct device *dev, struct device_driver *drv)
149 struct rmi_function_handler *handler = to_rmi_function_handler(drv);
150 struct rmi_function *fn = to_rmi_function(dev);
152 return fn->fd.function_number == handler->func;
155 #ifdef CONFIG_OF
156 static void rmi_function_of_probe(struct rmi_function *fn)
158 char of_name[9];
159 struct device_node *node = fn->rmi_dev->xport->dev->of_node;
161 snprintf(of_name, sizeof(of_name), "rmi4-f%02x",
162 fn->fd.function_number);
163 fn->dev.of_node = of_get_child_by_name(node, of_name);
165 #else
166 static inline void rmi_function_of_probe(struct rmi_function *fn)
168 #endif
170 static int rmi_function_probe(struct device *dev)
172 struct rmi_function *fn = to_rmi_function(dev);
173 struct rmi_function_handler *handler =
174 to_rmi_function_handler(dev->driver);
175 int error;
177 rmi_function_of_probe(fn);
179 if (handler->probe) {
180 error = handler->probe(fn);
181 return error;
184 return 0;
187 static int rmi_function_remove(struct device *dev)
189 struct rmi_function *fn = to_rmi_function(dev);
190 struct rmi_function_handler *handler =
191 to_rmi_function_handler(dev->driver);
193 if (handler->remove)
194 handler->remove(fn);
196 return 0;
199 int rmi_register_function(struct rmi_function *fn)
201 struct rmi_device *rmi_dev = fn->rmi_dev;
202 int error;
204 device_initialize(&fn->dev);
206 dev_set_name(&fn->dev, "%s.fn%02x",
207 dev_name(&rmi_dev->dev), fn->fd.function_number);
209 fn->dev.parent = &rmi_dev->dev;
210 fn->dev.type = &rmi_function_type;
211 fn->dev.bus = &rmi_bus_type;
213 error = device_add(&fn->dev);
214 if (error) {
215 dev_err(&rmi_dev->dev,
216 "Failed device_register function device %s\n",
217 dev_name(&fn->dev));
218 goto err_put_device;
221 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Registered F%02X.\n",
222 fn->fd.function_number);
224 return 0;
226 err_put_device:
227 put_device(&fn->dev);
228 return error;
231 void rmi_unregister_function(struct rmi_function *fn)
233 rmi_dbg(RMI_DEBUG_CORE, &fn->dev, "Unregistering F%02X.\n",
234 fn->fd.function_number);
236 device_del(&fn->dev);
237 of_node_put(fn->dev.of_node);
238 put_device(&fn->dev);
242 * rmi_register_function_handler - register a handler for an RMI function
243 * @handler: RMI handler that should be registered.
244 * @module: pointer to module that implements the handler
245 * @mod_name: name of the module implementing the handler
247 * This function performs additional setup of RMI function handler and
248 * registers it with the RMI core so that it can be bound to
249 * RMI function devices.
251 int __rmi_register_function_handler(struct rmi_function_handler *handler,
252 struct module *owner,
253 const char *mod_name)
255 struct device_driver *driver = &handler->driver;
256 int error;
258 driver->bus = &rmi_bus_type;
259 driver->owner = owner;
260 driver->mod_name = mod_name;
261 driver->probe = rmi_function_probe;
262 driver->remove = rmi_function_remove;
264 error = driver_register(driver);
265 if (error) {
266 pr_err("driver_register() failed for %s, error: %d\n",
267 driver->name, error);
268 return error;
271 return 0;
273 EXPORT_SYMBOL_GPL(__rmi_register_function_handler);
276 * rmi_unregister_function_handler - unregister given RMI function handler
277 * @handler: RMI handler that should be unregistered.
279 * This function unregisters given function handler from RMI core which
280 * causes it to be unbound from the function devices.
282 void rmi_unregister_function_handler(struct rmi_function_handler *handler)
284 driver_unregister(&handler->driver);
286 EXPORT_SYMBOL_GPL(rmi_unregister_function_handler);
288 /* Bus specific stuff */
290 static int rmi_bus_match(struct device *dev, struct device_driver *drv)
292 bool physical = rmi_is_physical_device(dev);
294 /* First see if types are not compatible */
295 if (physical != rmi_is_physical_driver(drv))
296 return 0;
298 return physical || rmi_function_match(dev, drv);
301 struct bus_type rmi_bus_type = {
302 .match = rmi_bus_match,
303 .name = "rmi4",
306 static struct rmi_function_handler *fn_handlers[] = {
307 &rmi_f01_handler,
308 #ifdef CONFIG_RMI4_F03
309 &rmi_f03_handler,
310 #endif
311 #ifdef CONFIG_RMI4_F11
312 &rmi_f11_handler,
313 #endif
314 #ifdef CONFIG_RMI4_F12
315 &rmi_f12_handler,
316 #endif
317 #ifdef CONFIG_RMI4_F30
318 &rmi_f30_handler,
319 #endif
320 #ifdef CONFIG_RMI4_F34
321 &rmi_f34_handler,
322 #endif
323 #ifdef CONFIG_RMI4_F54
324 &rmi_f54_handler,
325 #endif
326 #ifdef CONFIG_RMI4_F55
327 &rmi_f55_handler,
328 #endif
331 static void __rmi_unregister_function_handlers(int start_idx)
333 int i;
335 for (i = start_idx; i >= 0; i--)
336 rmi_unregister_function_handler(fn_handlers[i]);
339 static void rmi_unregister_function_handlers(void)
341 __rmi_unregister_function_handlers(ARRAY_SIZE(fn_handlers) - 1);
344 static int rmi_register_function_handlers(void)
346 int ret;
347 int i;
349 for (i = 0; i < ARRAY_SIZE(fn_handlers); i++) {
350 ret = rmi_register_function_handler(fn_handlers[i]);
351 if (ret) {
352 pr_err("%s: error registering the RMI F%02x handler: %d\n",
353 __func__, fn_handlers[i]->func, ret);
354 goto err_unregister_function_handlers;
358 return 0;
360 err_unregister_function_handlers:
361 __rmi_unregister_function_handlers(i - 1);
362 return ret;
365 int rmi_of_property_read_u32(struct device *dev, u32 *result,
366 const char *prop, bool optional)
368 int retval;
369 u32 val = 0;
371 retval = of_property_read_u32(dev->of_node, prop, &val);
372 if (retval && (!optional && retval == -EINVAL)) {
373 dev_err(dev, "Failed to get %s value: %d\n",
374 prop, retval);
375 return retval;
377 *result = val;
379 return 0;
381 EXPORT_SYMBOL_GPL(rmi_of_property_read_u32);
383 static int __init rmi_bus_init(void)
385 int error;
387 error = bus_register(&rmi_bus_type);
388 if (error) {
389 pr_err("%s: error registering the RMI bus: %d\n",
390 __func__, error);
391 return error;
394 error = rmi_register_function_handlers();
395 if (error)
396 goto err_unregister_bus;
398 error = rmi_register_physical_driver();
399 if (error) {
400 pr_err("%s: error registering the RMI physical driver: %d\n",
401 __func__, error);
402 goto err_unregister_bus;
405 return 0;
407 err_unregister_bus:
408 bus_unregister(&rmi_bus_type);
409 return error;
411 module_init(rmi_bus_init);
413 static void __exit rmi_bus_exit(void)
416 * We should only ever get here if all drivers are unloaded, so
417 * all we have to do at this point is unregister ourselves.
420 rmi_unregister_physical_driver();
421 rmi_unregister_function_handlers();
422 bus_unregister(&rmi_bus_type);
424 module_exit(rmi_bus_exit);
426 MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com");
427 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com");
428 MODULE_DESCRIPTION("RMI bus");
429 MODULE_LICENSE("GPL");
430 MODULE_VERSION(RMI_DRIVER_VERSION);