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
;
93 rmi_dev
->dev
.parent
= xport
->dev
;
95 xport
->rmi_dev
= rmi_dev
;
97 error
= device_add(&rmi_dev
->dev
);
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
));
108 put_device(&rmi_dev
->dev
);
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
);
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
, const struct device_driver
*drv
)
149 const 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
;
156 static void rmi_function_of_probe(struct rmi_function
*fn
)
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
);
166 static inline void rmi_function_of_probe(struct rmi_function
*fn
)
170 static struct irq_chip rmi_irq_chip
= {
174 static int rmi_create_function_irq(struct rmi_function
*fn
,
175 struct rmi_function_handler
*handler
)
177 struct rmi_driver_data
*drvdata
= dev_get_drvdata(&fn
->rmi_dev
->dev
);
180 for (i
= 0; i
< fn
->num_of_irqs
; i
++) {
181 set_bit(fn
->irq_pos
+ i
, fn
->irq_mask
);
183 fn
->irq
[i
] = irq_create_mapping(drvdata
->irqdomain
,
186 irq_set_chip_data(fn
->irq
[i
], fn
);
187 irq_set_chip_and_handler(fn
->irq
[i
], &rmi_irq_chip
,
189 irq_set_nested_thread(fn
->irq
[i
], 1);
191 error
= devm_request_threaded_irq(&fn
->dev
, fn
->irq
[i
], NULL
,
192 handler
->attention
, IRQF_ONESHOT
,
193 dev_name(&fn
->dev
), fn
);
195 dev_err(&fn
->dev
, "Error %d registering IRQ\n", error
);
203 static int rmi_function_probe(struct device
*dev
)
205 struct rmi_function
*fn
= to_rmi_function(dev
);
206 struct rmi_function_handler
*handler
=
207 to_rmi_function_handler(dev
->driver
);
210 rmi_function_of_probe(fn
);
212 if (handler
->probe
) {
213 error
= handler
->probe(fn
);
218 if (fn
->num_of_irqs
&& handler
->attention
) {
219 error
= rmi_create_function_irq(fn
, handler
);
227 static int rmi_function_remove(struct device
*dev
)
229 struct rmi_function
*fn
= to_rmi_function(dev
);
230 struct rmi_function_handler
*handler
=
231 to_rmi_function_handler(dev
->driver
);
239 int rmi_register_function(struct rmi_function
*fn
)
241 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
244 device_initialize(&fn
->dev
);
246 dev_set_name(&fn
->dev
, "%s.fn%02x",
247 dev_name(&rmi_dev
->dev
), fn
->fd
.function_number
);
249 fn
->dev
.parent
= &rmi_dev
->dev
;
250 fn
->dev
.type
= &rmi_function_type
;
251 fn
->dev
.bus
= &rmi_bus_type
;
253 error
= device_add(&fn
->dev
);
255 dev_err(&rmi_dev
->dev
,
256 "Failed device_register function device %s\n",
261 rmi_dbg(RMI_DEBUG_CORE
, &rmi_dev
->dev
, "Registered F%02X.\n",
262 fn
->fd
.function_number
);
267 put_device(&fn
->dev
);
271 void rmi_unregister_function(struct rmi_function
*fn
)
275 rmi_dbg(RMI_DEBUG_CORE
, &fn
->dev
, "Unregistering F%02X.\n",
276 fn
->fd
.function_number
);
278 device_del(&fn
->dev
);
279 of_node_put(fn
->dev
.of_node
);
281 for (i
= 0; i
< fn
->num_of_irqs
; i
++)
282 irq_dispose_mapping(fn
->irq
[i
]);
284 put_device(&fn
->dev
);
288 * __rmi_register_function_handler - register a handler for an RMI function
289 * @handler: RMI handler that should be registered.
290 * @owner: pointer to module that implements the handler
291 * @mod_name: name of the module implementing the handler
293 * This function performs additional setup of RMI function handler and
294 * registers it with the RMI core so that it can be bound to
295 * RMI function devices.
297 int __rmi_register_function_handler(struct rmi_function_handler
*handler
,
298 struct module
*owner
,
299 const char *mod_name
)
301 struct device_driver
*driver
= &handler
->driver
;
304 driver
->bus
= &rmi_bus_type
;
305 driver
->owner
= owner
;
306 driver
->mod_name
= mod_name
;
307 driver
->probe
= rmi_function_probe
;
308 driver
->remove
= rmi_function_remove
;
310 error
= driver_register(driver
);
312 pr_err("driver_register() failed for %s, error: %d\n",
313 driver
->name
, error
);
319 EXPORT_SYMBOL_GPL(__rmi_register_function_handler
);
322 * rmi_unregister_function_handler - unregister given RMI function handler
323 * @handler: RMI handler that should be unregistered.
325 * This function unregisters given function handler from RMI core which
326 * causes it to be unbound from the function devices.
328 void rmi_unregister_function_handler(struct rmi_function_handler
*handler
)
330 driver_unregister(&handler
->driver
);
332 EXPORT_SYMBOL_GPL(rmi_unregister_function_handler
);
334 /* Bus specific stuff */
336 static int rmi_bus_match(struct device
*dev
, const struct device_driver
*drv
)
338 bool physical
= rmi_is_physical_device(dev
);
340 /* First see if types are not compatible */
341 if (physical
!= rmi_is_physical_driver(drv
))
344 return physical
|| rmi_function_match(dev
, drv
);
347 const struct bus_type rmi_bus_type
= {
348 .match
= rmi_bus_match
,
352 static struct rmi_function_handler
*fn_handlers
[] = {
354 #ifdef CONFIG_RMI4_F03
357 #ifdef CONFIG_RMI4_F11
360 #ifdef CONFIG_RMI4_F12
363 #ifdef CONFIG_RMI4_F30
366 #ifdef CONFIG_RMI4_F34
369 #ifdef CONFIG_RMI4_F3A
372 #ifdef CONFIG_RMI4_F54
375 #ifdef CONFIG_RMI4_F55
380 static void __rmi_unregister_function_handlers(int start_idx
)
384 for (i
= start_idx
; i
>= 0; i
--)
385 rmi_unregister_function_handler(fn_handlers
[i
]);
388 static void rmi_unregister_function_handlers(void)
390 __rmi_unregister_function_handlers(ARRAY_SIZE(fn_handlers
) - 1);
393 static int rmi_register_function_handlers(void)
398 for (i
= 0; i
< ARRAY_SIZE(fn_handlers
); i
++) {
399 ret
= rmi_register_function_handler(fn_handlers
[i
]);
401 pr_err("%s: error registering the RMI F%02x handler: %d\n",
402 __func__
, fn_handlers
[i
]->func
, ret
);
403 goto err_unregister_function_handlers
;
409 err_unregister_function_handlers
:
410 __rmi_unregister_function_handlers(i
- 1);
414 int rmi_of_property_read_u32(struct device
*dev
, u32
*result
,
415 const char *prop
, bool optional
)
420 retval
= of_property_read_u32(dev
->of_node
, prop
, &val
);
421 if (retval
&& (!optional
&& retval
== -EINVAL
)) {
422 dev_err(dev
, "Failed to get %s value: %d\n",
430 EXPORT_SYMBOL_GPL(rmi_of_property_read_u32
);
432 static int __init
rmi_bus_init(void)
436 error
= bus_register(&rmi_bus_type
);
438 pr_err("%s: error registering the RMI bus: %d\n",
443 error
= rmi_register_function_handlers();
445 goto err_unregister_bus
;
447 error
= rmi_register_physical_driver();
449 pr_err("%s: error registering the RMI physical driver: %d\n",
451 goto err_unregister_bus
;
457 bus_unregister(&rmi_bus_type
);
460 module_init(rmi_bus_init
);
462 static void __exit
rmi_bus_exit(void)
465 * We should only ever get here if all drivers are unloaded, so
466 * all we have to do at this point is unregister ourselves.
469 rmi_unregister_physical_driver();
470 rmi_unregister_function_handlers();
471 bus_unregister(&rmi_bus_type
);
473 module_exit(rmi_bus_exit
);
475 MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com");
476 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com");
477 MODULE_DESCRIPTION("RMI bus");
478 MODULE_LICENSE("GPL");