1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011-2016 Synaptics Incorporated
4 * Copyright (c) 2011 Unixphere
7 #include <linux/kernel.h>
8 #include <linux/device.h>
10 #include <linux/irqdomain.h>
11 #include <linux/list.h>
13 #include <linux/rmi.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
18 #include "rmi_driver.h"
20 static int debug_flags;
21 module_param(debug_flags, int, 0644);
22 MODULE_PARM_DESC(debug_flags, "control debugging information");
24 void rmi_dbg(int flags, struct device *dev, const char *fmt, ...)
29 if (flags & debug_flags) {
35 dev_printk(KERN_DEBUG, dev, "%pV", &vaf);
40 EXPORT_SYMBOL_GPL(rmi_dbg);
43 * RMI Physical devices
45 * Physical RMI device consists of several functions serving particular
46 * purpose. For example F11 is a 2D touch sensor while F01 is a generic
47 * function present in every RMI device.
50 static void rmi_release_device(struct device *dev)
52 struct rmi_device *rmi_dev = to_rmi_device(dev);
57 static const struct device_type rmi_device_type = {
58 .name = "rmi4_sensor",
59 .release = rmi_release_device,
62 bool rmi_is_physical_device(struct device *dev)
64 return dev->type == &rmi_device_type;
68 * rmi_register_transport_device - register a transport device connection
69 * on the RMI bus. Transport drivers provide communication from the devices
70 * on a bus (such as SPI, I2C, and so on) to the RMI4 sensor.
72 * @xport: the transport device to register
74 int rmi_register_transport_device(struct rmi_transport_dev *xport)
76 static atomic_t transport_device_count = ATOMIC_INIT(0);
77 struct rmi_device *rmi_dev;
80 rmi_dev = kzalloc(sizeof(struct rmi_device), GFP_KERNEL);
84 device_initialize(&rmi_dev->dev);
86 rmi_dev->xport = xport;
87 rmi_dev->number = atomic_inc_return(&transport_device_count) - 1;
89 dev_set_name(&rmi_dev->dev, "rmi4-%02d", rmi_dev->number);
91 rmi_dev->dev.bus = &rmi_bus_type;
92 rmi_dev->dev.type = &rmi_device_type;
94 xport->rmi_dev = rmi_dev;
96 error = device_add(&rmi_dev->dev);
100 rmi_dbg(RMI_DEBUG_CORE, xport->dev,
101 "%s: Registered %s as %s.\n", __func__,
102 dev_name(rmi_dev->xport->dev), dev_name(&rmi_dev->dev));
107 put_device(&rmi_dev->dev);
110 EXPORT_SYMBOL_GPL(rmi_register_transport_device);
113 * rmi_unregister_transport_device - unregister a transport device connection
114 * @xport: the transport driver to unregister
117 void rmi_unregister_transport_device(struct rmi_transport_dev *xport)
119 struct rmi_device *rmi_dev = xport->rmi_dev;
121 device_del(&rmi_dev->dev);
122 put_device(&rmi_dev->dev);
124 EXPORT_SYMBOL(rmi_unregister_transport_device);
127 /* Function specific stuff */
129 static void rmi_release_function(struct device *dev)
131 struct rmi_function *fn = to_rmi_function(dev);
136 static const struct device_type rmi_function_type = {
137 .name = "rmi4_function",
138 .release = rmi_release_function,
141 bool rmi_is_function_device(struct device *dev)
143 return dev->type == &rmi_function_type;
146 static int rmi_function_match(struct device *dev, struct device_driver *drv)
148 struct rmi_function_handler *handler = to_rmi_function_handler(drv);
149 struct rmi_function *fn = to_rmi_function(dev);
151 return fn->fd.function_number == handler->func;
155 static void rmi_function_of_probe(struct rmi_function *fn)
158 struct device_node *node = fn->rmi_dev->xport->dev->of_node;
160 snprintf(of_name, sizeof(of_name), "rmi4-f%02x",
161 fn->fd.function_number);
162 fn->dev.of_node = of_get_child_by_name(node, of_name);
165 static inline void rmi_function_of_probe(struct rmi_function *fn)
169 static struct irq_chip rmi_irq_chip = {
173 static int rmi_create_function_irq(struct rmi_function *fn,
174 struct rmi_function_handler *handler)
176 struct rmi_driver_data *drvdata = dev_get_drvdata(&fn->rmi_dev->dev);
179 for (i = 0; i < fn->num_of_irqs; i++) {
180 set_bit(fn->irq_pos + i, fn->irq_mask);
182 fn->irq[i] = irq_create_mapping(drvdata->irqdomain,
185 irq_set_chip_data(fn->irq[i], fn);
186 irq_set_chip_and_handler(fn->irq[i], &rmi_irq_chip,
188 irq_set_nested_thread(fn->irq[i], 1);
190 error = devm_request_threaded_irq(&fn->dev, fn->irq[i], NULL,
191 handler->attention, IRQF_ONESHOT,
192 dev_name(&fn->dev), fn);
194 dev_err(&fn->dev, "Error %d registering IRQ\n", error);
202 static int rmi_function_probe(struct device *dev)
204 struct rmi_function *fn = to_rmi_function(dev);
205 struct rmi_function_handler *handler =
206 to_rmi_function_handler(dev->driver);
209 rmi_function_of_probe(fn);
211 if (handler->probe) {
212 error = handler->probe(fn);
217 if (fn->num_of_irqs && handler->attention) {
218 error = rmi_create_function_irq(fn, handler);
226 static int rmi_function_remove(struct device *dev)
228 struct rmi_function *fn = to_rmi_function(dev);
229 struct rmi_function_handler *handler =
230 to_rmi_function_handler(dev->driver);
238 int rmi_register_function(struct rmi_function *fn)
240 struct rmi_device *rmi_dev = fn->rmi_dev;
243 device_initialize(&fn->dev);
245 dev_set_name(&fn->dev, "%s.fn%02x",
246 dev_name(&rmi_dev->dev), fn->fd.function_number);
248 fn->dev.parent = &rmi_dev->dev;
249 fn->dev.type = &rmi_function_type;
250 fn->dev.bus = &rmi_bus_type;
252 error = device_add(&fn->dev);
254 dev_err(&rmi_dev->dev,
255 "Failed device_register function device %s\n",
260 rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Registered F%02X.\n",
261 fn->fd.function_number);
266 put_device(&fn->dev);
270 void rmi_unregister_function(struct rmi_function *fn)
274 rmi_dbg(RMI_DEBUG_CORE, &fn->dev, "Unregistering F%02X.\n",
275 fn->fd.function_number);
277 device_del(&fn->dev);
278 of_node_put(fn->dev.of_node);
280 for (i = 0; i < fn->num_of_irqs; i++)
281 irq_dispose_mapping(fn->irq[i]);
283 put_device(&fn->dev);
287 * rmi_register_function_handler - register a handler for an RMI function
288 * @handler: RMI handler that should be registered.
289 * @module: pointer to module that implements the handler
290 * @mod_name: name of the module implementing the handler
292 * This function performs additional setup of RMI function handler and
293 * registers it with the RMI core so that it can be bound to
294 * RMI function devices.
296 int __rmi_register_function_handler(struct rmi_function_handler *handler,
297 struct module *owner,
298 const char *mod_name)
300 struct device_driver *driver = &handler->driver;
303 driver->bus = &rmi_bus_type;
304 driver->owner = owner;
305 driver->mod_name = mod_name;
306 driver->probe = rmi_function_probe;
307 driver->remove = rmi_function_remove;
309 error = driver_register(driver);
311 pr_err("driver_register() failed for %s, error: %d\n",
312 driver->name, error);
318 EXPORT_SYMBOL_GPL(__rmi_register_function_handler);
321 * rmi_unregister_function_handler - unregister given RMI function handler
322 * @handler: RMI handler that should be unregistered.
324 * This function unregisters given function handler from RMI core which
325 * causes it to be unbound from the function devices.
327 void rmi_unregister_function_handler(struct rmi_function_handler *handler)
329 driver_unregister(&handler->driver);
331 EXPORT_SYMBOL_GPL(rmi_unregister_function_handler);
333 /* Bus specific stuff */
335 static int rmi_bus_match(struct device *dev, struct device_driver *drv)
337 bool physical = rmi_is_physical_device(dev);
339 /* First see if types are not compatible */
340 if (physical != rmi_is_physical_driver(drv))
343 return physical || rmi_function_match(dev, drv);
346 struct bus_type rmi_bus_type = {
347 .match = rmi_bus_match,
351 static struct rmi_function_handler *fn_handlers[] = {
353 #ifdef CONFIG_RMI4_F03
356 #ifdef CONFIG_RMI4_F11
359 #ifdef CONFIG_RMI4_F12
362 #ifdef CONFIG_RMI4_F30
365 #ifdef CONFIG_RMI4_F34
368 #ifdef CONFIG_RMI4_F3A
371 #ifdef CONFIG_RMI4_F54
374 #ifdef CONFIG_RMI4_F55
379 static void __rmi_unregister_function_handlers(int start_idx)
383 for (i = start_idx; i >= 0; i--)
384 rmi_unregister_function_handler(fn_handlers[i]);
387 static void rmi_unregister_function_handlers(void)
389 __rmi_unregister_function_handlers(ARRAY_SIZE(fn_handlers) - 1);
392 static int rmi_register_function_handlers(void)
397 for (i = 0; i < ARRAY_SIZE(fn_handlers); i++) {
398 ret = rmi_register_function_handler(fn_handlers[i]);
400 pr_err("%s: error registering the RMI F%02x handler: %d\n",
401 __func__, fn_handlers[i]->func, ret);
402 goto err_unregister_function_handlers;
408 err_unregister_function_handlers:
409 __rmi_unregister_function_handlers(i - 1);
413 int rmi_of_property_read_u32(struct device *dev, u32 *result,
414 const char *prop, bool optional)
419 retval = of_property_read_u32(dev->of_node, prop, &val);
420 if (retval && (!optional && retval == -EINVAL)) {
421 dev_err(dev, "Failed to get %s value: %d\n",
429 EXPORT_SYMBOL_GPL(rmi_of_property_read_u32);
431 static int __init rmi_bus_init(void)
435 error = bus_register(&rmi_bus_type);
437 pr_err("%s: error registering the RMI bus: %d\n",
442 error = rmi_register_function_handlers();
444 goto err_unregister_bus;
446 error = rmi_register_physical_driver();
448 pr_err("%s: error registering the RMI physical driver: %d\n",
450 goto err_unregister_bus;
456 bus_unregister(&rmi_bus_type);
459 module_init(rmi_bus_init);
461 static void __exit rmi_bus_exit(void)
464 * We should only ever get here if all drivers are unloaded, so
465 * all we have to do at this point is unregister ourselves.
468 rmi_unregister_physical_driver();
469 rmi_unregister_function_handlers();
470 bus_unregister(&rmi_bus_type);
472 module_exit(rmi_bus_exit);
474 MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com");
475 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com");
476 MODULE_DESCRIPTION("RMI bus");
477 MODULE_LICENSE("GPL");