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.
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/kconfig.h>
13 #include <linux/list.h>
15 #include <linux/rmi.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
20 #include "rmi_driver.h"
22 static int debug_flags
;
23 module_param(debug_flags
, int, 0644);
24 MODULE_PARM_DESC(debug_flags
, "control debugging information");
26 void rmi_dbg(int flags
, struct device
*dev
, const char *fmt
, ...)
31 if (flags
& debug_flags
) {
37 dev_printk(KERN_DEBUG
, dev
, "%pV", &vaf
);
42 EXPORT_SYMBOL_GPL(rmi_dbg
);
45 * RMI Physical devices
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.
52 static void rmi_release_device(struct device
*dev
)
54 struct rmi_device
*rmi_dev
= to_rmi_device(dev
);
59 static struct device_type rmi_device_type
= {
60 .name
= "rmi4_sensor",
61 .release
= rmi_release_device
,
64 bool rmi_is_physical_device(struct device
*dev
)
66 return dev
->type
== &rmi_device_type
;
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.
74 * @xport: the transport device to register
76 int rmi_register_transport_device(struct rmi_transport_dev
*xport
)
78 static atomic_t transport_device_count
= ATOMIC_INIT(0);
79 struct rmi_device
*rmi_dev
;
82 rmi_dev
= kzalloc(sizeof(struct rmi_device
), GFP_KERNEL
);
86 device_initialize(&rmi_dev
->dev
);
88 rmi_dev
->xport
= xport
;
89 rmi_dev
->number
= atomic_inc_return(&transport_device_count
) - 1;
91 dev_set_name(&rmi_dev
->dev
, "rmi4-%02d", rmi_dev
->number
);
93 rmi_dev
->dev
.bus
= &rmi_bus_type
;
94 rmi_dev
->dev
.type
= &rmi_device_type
;
96 xport
->rmi_dev
= rmi_dev
;
98 error
= device_add(&rmi_dev
->dev
);
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
));
109 put_device(&rmi_dev
->dev
);
112 EXPORT_SYMBOL_GPL(rmi_register_transport_device
);
115 * rmi_unregister_transport_device - unregister a transport device connection
116 * @xport: the transport driver to unregister
119 void rmi_unregister_transport_device(struct rmi_transport_dev
*xport
)
121 struct rmi_device
*rmi_dev
= xport
->rmi_dev
;
123 device_del(&rmi_dev
->dev
);
124 put_device(&rmi_dev
->dev
);
126 EXPORT_SYMBOL(rmi_unregister_transport_device
);
129 /* Function specific stuff */
131 static void rmi_release_function(struct device
*dev
)
133 struct rmi_function
*fn
= to_rmi_function(dev
);
138 static struct device_type rmi_function_type
= {
139 .name
= "rmi4_function",
140 .release
= rmi_release_function
,
143 bool rmi_is_function_device(struct device
*dev
)
145 return dev
->type
== &rmi_function_type
;
148 static int rmi_function_match(struct device
*dev
, struct device_driver
*drv
)
150 struct rmi_function_handler
*handler
= to_rmi_function_handler(drv
);
151 struct rmi_function
*fn
= to_rmi_function(dev
);
153 return fn
->fd
.function_number
== handler
->func
;
157 static void rmi_function_of_probe(struct rmi_function
*fn
)
160 struct device_node
*node
= fn
->rmi_dev
->xport
->dev
->of_node
;
162 snprintf(of_name
, sizeof(of_name
), "rmi4-f%02x",
163 fn
->fd
.function_number
);
164 fn
->dev
.of_node
= of_get_child_by_name(node
, of_name
);
167 static inline void rmi_function_of_probe(struct rmi_function
*fn
)
171 static int rmi_function_probe(struct device
*dev
)
173 struct rmi_function
*fn
= to_rmi_function(dev
);
174 struct rmi_function_handler
*handler
=
175 to_rmi_function_handler(dev
->driver
);
178 rmi_function_of_probe(fn
);
180 if (handler
->probe
) {
181 error
= handler
->probe(fn
);
188 static int rmi_function_remove(struct device
*dev
)
190 struct rmi_function
*fn
= to_rmi_function(dev
);
191 struct rmi_function_handler
*handler
=
192 to_rmi_function_handler(dev
->driver
);
200 int rmi_register_function(struct rmi_function
*fn
)
202 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
205 device_initialize(&fn
->dev
);
207 dev_set_name(&fn
->dev
, "%s.fn%02x",
208 dev_name(&rmi_dev
->dev
), fn
->fd
.function_number
);
210 fn
->dev
.parent
= &rmi_dev
->dev
;
211 fn
->dev
.type
= &rmi_function_type
;
212 fn
->dev
.bus
= &rmi_bus_type
;
214 error
= device_add(&fn
->dev
);
216 dev_err(&rmi_dev
->dev
,
217 "Failed device_register function device %s\n",
222 rmi_dbg(RMI_DEBUG_CORE
, &rmi_dev
->dev
, "Registered F%02X.\n",
223 fn
->fd
.function_number
);
228 put_device(&fn
->dev
);
232 void rmi_unregister_function(struct rmi_function
*fn
)
234 device_del(&fn
->dev
);
235 of_node_put(fn
->dev
.of_node
);
236 put_device(&fn
->dev
);
240 * rmi_register_function_handler - register a handler for an RMI function
241 * @handler: RMI handler that should be registered.
242 * @module: pointer to module that implements the handler
243 * @mod_name: name of the module implementing the handler
245 * This function performs additional setup of RMI function handler and
246 * registers it with the RMI core so that it can be bound to
247 * RMI function devices.
249 int __rmi_register_function_handler(struct rmi_function_handler
*handler
,
250 struct module
*owner
,
251 const char *mod_name
)
253 struct device_driver
*driver
= &handler
->driver
;
256 driver
->bus
= &rmi_bus_type
;
257 driver
->owner
= owner
;
258 driver
->mod_name
= mod_name
;
259 driver
->probe
= rmi_function_probe
;
260 driver
->remove
= rmi_function_remove
;
262 error
= driver_register(&handler
->driver
);
264 pr_err("driver_register() failed for %s, error: %d\n",
265 handler
->driver
.name
, error
);
271 EXPORT_SYMBOL_GPL(__rmi_register_function_handler
);
274 * rmi_unregister_function_handler - unregister given RMI function handler
275 * @handler: RMI handler that should be unregistered.
277 * This function unregisters given function handler from RMI core which
278 * causes it to be unbound from the function devices.
280 void rmi_unregister_function_handler(struct rmi_function_handler
*handler
)
282 driver_unregister(&handler
->driver
);
284 EXPORT_SYMBOL_GPL(rmi_unregister_function_handler
);
286 /* Bus specific stuff */
288 static int rmi_bus_match(struct device
*dev
, struct device_driver
*drv
)
290 bool physical
= rmi_is_physical_device(dev
);
292 /* First see if types are not compatible */
293 if (physical
!= rmi_is_physical_driver(drv
))
296 return physical
|| rmi_function_match(dev
, drv
);
299 struct bus_type rmi_bus_type
= {
300 .match
= rmi_bus_match
,
304 static struct rmi_function_handler
*fn_handlers
[] = {
306 #ifdef CONFIG_RMI4_F11
309 #ifdef CONFIG_RMI4_F12
312 #ifdef CONFIG_RMI4_F30
317 static void __rmi_unregister_function_handlers(int start_idx
)
321 for (i
= start_idx
; i
>= 0; i
--)
322 rmi_unregister_function_handler(fn_handlers
[i
]);
325 static void rmi_unregister_function_handlers(void)
327 __rmi_unregister_function_handlers(ARRAY_SIZE(fn_handlers
) - 1);
330 static int rmi_register_function_handlers(void)
335 for (i
= 0; i
< ARRAY_SIZE(fn_handlers
); i
++) {
336 ret
= rmi_register_function_handler(fn_handlers
[i
]);
338 pr_err("%s: error registering the RMI F%02x handler: %d\n",
339 __func__
, fn_handlers
[i
]->func
, ret
);
340 goto err_unregister_function_handlers
;
346 err_unregister_function_handlers
:
347 __rmi_unregister_function_handlers(i
- 1);
351 int rmi_of_property_read_u32(struct device
*dev
, u32
*result
,
352 const char *prop
, bool optional
)
357 retval
= of_property_read_u32(dev
->of_node
, prop
, &val
);
358 if (retval
&& (!optional
&& retval
== -EINVAL
)) {
359 dev_err(dev
, "Failed to get %s value: %d\n",
367 EXPORT_SYMBOL_GPL(rmi_of_property_read_u32
);
369 static int __init
rmi_bus_init(void)
373 error
= bus_register(&rmi_bus_type
);
375 pr_err("%s: error registering the RMI bus: %d\n",
380 error
= rmi_register_function_handlers();
382 goto err_unregister_bus
;
384 error
= rmi_register_physical_driver();
386 pr_err("%s: error registering the RMI physical driver: %d\n",
388 goto err_unregister_bus
;
394 bus_unregister(&rmi_bus_type
);
397 module_init(rmi_bus_init
);
399 static void __exit
rmi_bus_exit(void)
402 * We should only ever get here if all drivers are unloaded, so
403 * all we have to do at this point is unregister ourselves.
406 rmi_unregister_physical_driver();
407 rmi_unregister_function_handlers();
408 bus_unregister(&rmi_bus_type
);
410 module_exit(rmi_bus_exit
);
412 MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com");
413 MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com");
414 MODULE_DESCRIPTION("RMI bus");
415 MODULE_LICENSE("GPL");
416 MODULE_VERSION(RMI_DRIVER_VERSION
);