2 * Copyright (c) 2011 Synaptics Incorporated
3 * Copyright (c) 2011 Unixphere
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/kernel.h>
21 #include <linux/device.h>
23 #include <linux/slab.h>
24 #include <linux/list.h>
25 #include <linux/rmi.h>
26 #include <linux/types.h>
27 #ifdef CONFIG_RMI4_DEBUG
28 #include <linux/debugfs.h>
30 #include "rmi_driver.h"
31 DEFINE_MUTEX(rmi_bus_mutex);
33 static struct rmi_function_list {
34 struct list_head list;
35 struct rmi_function_handler *fh;
36 } rmi_supported_functions;
38 static struct rmi_character_driver_list {
39 struct list_head list;
40 struct rmi_char_driver *cd;
41 } rmi_character_drivers;
43 static atomic_t physical_device_count;
45 #ifdef CONFIG_RMI4_DEBUG
46 static struct dentry *rmi_debugfs_root;
49 static int rmi_bus_match(struct device *dev, struct device_driver *driver)
51 struct rmi_driver *rmi_driver;
52 struct rmi_device *rmi_dev;
53 struct rmi_device_platform_data *pdata;
55 rmi_driver = to_rmi_driver(driver);
56 rmi_dev = to_rmi_device(dev);
57 pdata = to_rmi_platform_data(rmi_dev);
58 dev_dbg(dev, "%s: Matching %s.\n", __func__, pdata->sensor_name);
60 if (!strcmp(pdata->driver_name, rmi_driver->driver.name)) {
61 rmi_dev->driver = rmi_driver;
62 dev_dbg(dev, "%s: Match %s to %s succeeded.\n", __func__,
63 pdata->driver_name, rmi_driver->driver.name);
67 dev_vdbg(dev, "%s: Match %s to %s failed.\n", __func__,
68 pdata->driver_name, rmi_driver->driver.name);
73 static int rmi_bus_suspend(struct device *dev)
75 #ifdef GENERIC_SUBSYS_PM_OPS
76 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
78 if (pm && pm->suspend)
79 return pm->suspend(dev);
85 static int rmi_bus_resume(struct device *dev)
87 #ifdef GENERIC_SUBSYS_PM_OPS
88 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
91 return pm->resume(dev);
92 else if (dev->driver && dev->driver->resume)
93 return dev->driver->resume(dev);
95 if (dev->driver && dev->driver->resume)
96 return dev->driver->resume(dev);
103 static int rmi_bus_probe(struct device *dev)
105 struct rmi_driver *driver;
106 struct rmi_device *rmi_dev = to_rmi_device(dev);
108 driver = rmi_dev->driver;
109 if (driver && driver->probe)
110 return driver->probe(rmi_dev);
115 static int rmi_bus_remove(struct device *dev)
117 struct rmi_driver *driver;
118 struct rmi_device *rmi_dev = to_rmi_device(dev);
120 driver = rmi_dev->driver;
121 if (driver && driver->remove)
122 return driver->remove(rmi_dev);
127 static void rmi_bus_shutdown(struct device *dev)
129 struct rmi_driver *driver;
130 struct rmi_device *rmi_dev = to_rmi_device(dev);
132 driver = rmi_dev->driver;
133 if (driver && driver->shutdown)
134 driver->shutdown(rmi_dev);
137 static SIMPLE_DEV_PM_OPS(rmi_bus_pm_ops,
138 rmi_bus_suspend, rmi_bus_resume);
140 struct bus_type rmi_bus_type = {
142 .match = rmi_bus_match,
143 .probe = rmi_bus_probe,
144 .remove = rmi_bus_remove,
145 .shutdown = rmi_bus_shutdown,
146 .pm = &rmi_bus_pm_ops
149 static void release_rmidev_device(struct device *dev) {
150 device_unregister(dev);
153 int rmi_register_phys_device(struct rmi_phys_device *phys)
155 struct rmi_device_platform_data *pdata = phys->dev->platform_data;
156 struct rmi_device *rmi_dev;
159 dev_err(phys->dev, "no platform data!\n");
163 rmi_dev = kzalloc(sizeof(struct rmi_device), GFP_KERNEL);
167 rmi_dev->phys = phys;
168 rmi_dev->dev.bus = &rmi_bus_type;
170 rmi_dev->number = atomic_inc_return(&physical_device_count) - 1;
171 rmi_dev->dev.release = release_rmidev_device;
173 dev_set_name(&rmi_dev->dev, "sensor%02d", rmi_dev->number);
174 dev_dbg(phys->dev, "%s: Registered %s as %s.\n", __func__,
175 pdata->sensor_name, dev_name(&rmi_dev->dev));
177 #ifdef CONFIG_RMI4_DEBUG
178 if (rmi_debugfs_root) {
179 rmi_dev->debugfs_root = debugfs_create_dir(
180 dev_name(&rmi_dev->dev), rmi_debugfs_root);
181 if (!rmi_dev->debugfs_root)
182 dev_err(&rmi_dev->dev, "Failed to create debugfs root.\n");
185 phys->rmi_dev = rmi_dev;
186 return device_register(&rmi_dev->dev);
188 EXPORT_SYMBOL(rmi_register_phys_device);
190 void rmi_unregister_phys_device(struct rmi_phys_device *phys)
192 struct rmi_device *rmi_dev = phys->rmi_dev;
194 #ifdef CONFIG_RMI4_DEBUG
195 if (rmi_dev->debugfs_root)
196 debugfs_remove(rmi_dev->debugfs_root);
201 EXPORT_SYMBOL(rmi_unregister_phys_device);
203 int rmi_register_driver(struct rmi_driver *driver)
205 driver->driver.bus = &rmi_bus_type;
206 return driver_register(&driver->driver);
208 EXPORT_SYMBOL(rmi_register_driver);
210 static int __rmi_driver_remove(struct device *dev, void *data)
212 struct rmi_driver *driver = data;
213 struct rmi_device *rmi_dev = to_rmi_device(dev);
215 if (rmi_dev->driver == driver)
216 rmi_dev->driver = NULL;
221 void rmi_unregister_driver(struct rmi_driver *driver)
223 bus_for_each_dev(&rmi_bus_type, NULL, driver, __rmi_driver_remove);
224 driver_unregister(&driver->driver);
226 EXPORT_SYMBOL(rmi_unregister_driver);
228 static int __rmi_bus_fh_add(struct device *dev, void *data)
230 struct rmi_driver *driver;
231 struct rmi_device *rmi_dev = to_rmi_device(dev);
233 driver = rmi_dev->driver;
234 if (driver && driver->fh_add)
235 driver->fh_add(rmi_dev, data);
240 int rmi_register_function_driver(struct rmi_function_handler *fh)
242 struct rmi_function_list *entry;
243 struct rmi_function_handler *fh_dup;
245 fh_dup = rmi_get_function_handler(fh->func);
247 pr_err("%s: function f%.2x already registered!\n", __func__,
252 entry = kzalloc(sizeof(struct rmi_function_list), GFP_KERNEL);
257 INIT_LIST_HEAD(&entry->list);
258 list_add_tail(&entry->list, &rmi_supported_functions.list);
260 /* notify devices of the new function handler */
261 bus_for_each_dev(&rmi_bus_type, NULL, fh, __rmi_bus_fh_add);
265 EXPORT_SYMBOL(rmi_register_function_driver);
267 static int __rmi_bus_fh_remove(struct device *dev, void *data)
269 struct rmi_driver *driver;
270 struct rmi_device *rmi_dev = to_rmi_device(dev);
272 driver = rmi_dev->driver;
273 if (driver && driver->fh_remove)
274 driver->fh_remove(rmi_dev, data);
279 void rmi_unregister_function_driver(struct rmi_function_handler *fh)
281 struct rmi_function_list *entry, *n;
283 /* notify devices of the removal of the function handler */
284 bus_for_each_dev(&rmi_bus_type, NULL, fh, __rmi_bus_fh_remove);
286 if (list_empty(&rmi_supported_functions.list))
289 list_for_each_entry_safe(entry, n, &rmi_supported_functions.list,
291 if (entry->fh->func == fh->func) {
292 list_del(&entry->list);
298 EXPORT_SYMBOL(rmi_unregister_function_driver);
300 struct rmi_function_handler *rmi_get_function_handler(int id)
302 struct rmi_function_list *entry;
304 if (list_empty(&rmi_supported_functions.list))
307 list_for_each_entry(entry, &rmi_supported_functions.list, list)
308 if (entry->fh->func == id)
313 EXPORT_SYMBOL(rmi_get_function_handler);
315 static void rmi_release_character_device(struct device *dev)
317 dev_dbg(dev, "%s: Called.\n", __func__);
321 static int rmi_register_character_device(struct device *dev, void *data)
323 struct rmi_device *rmi_dev;
324 struct rmi_char_driver *char_driver = data;
325 struct rmi_char_device *char_dev;
328 dev_dbg(dev, "Attaching character device.\n");
329 rmi_dev = to_rmi_device(dev);
330 if (char_driver->match && !char_driver->match(rmi_dev))
333 if (!char_driver->init) {
334 dev_err(dev, "ERROR: No init() function in %s.\n", __func__);
338 char_dev = kzalloc(sizeof(struct rmi_char_device), GFP_KERNEL);
342 char_dev->rmi_dev = rmi_dev;
343 char_dev->driver = char_driver;
345 char_dev->dev.parent = dev;
346 char_dev->dev.release = rmi_release_character_device;
347 char_dev->dev.driver = &char_driver->driver;
348 retval = device_register(&char_dev->dev);
350 dev_err(dev, "Failed to register character device.\n");
354 retval = char_driver->init(char_dev);
356 dev_err(dev, "Failed to initialize character device.\n");
360 mutex_lock(&rmi_bus_mutex);
361 list_add_tail(&char_dev->list, &char_driver->devices);
362 mutex_unlock(&rmi_bus_mutex);
363 dev_info(&char_dev->dev, "Registered a device.\n");
371 int rmi_register_character_driver(struct rmi_char_driver *char_driver)
373 struct rmi_character_driver_list *entry;
376 pr_debug("%s: Registering character driver %s.\n", __func__,
377 char_driver->driver.name);
379 char_driver->driver.bus = &rmi_bus_type;
380 INIT_LIST_HEAD(&char_driver->devices);
381 retval = driver_register(&char_driver->driver);
383 pr_err("%s: Failed to register %s, code: %d.\n", __func__,
384 char_driver->driver.name, retval);
388 entry = kzalloc(sizeof(struct rmi_character_driver_list), GFP_KERNEL);
391 entry->cd = char_driver;
393 mutex_lock(&rmi_bus_mutex);
394 list_add_tail(&entry->list, &rmi_character_drivers.list);
395 mutex_unlock(&rmi_bus_mutex);
397 /* notify devices of the removal of the function handler */
398 bus_for_each_dev(&rmi_bus_type, NULL, char_driver,
399 rmi_register_character_device);
403 EXPORT_SYMBOL(rmi_register_character_driver);
406 int rmi_unregister_character_driver(struct rmi_char_driver *char_driver)
408 struct rmi_character_driver_list *entry, *n;
409 struct rmi_char_device *char_dev, *m;
410 pr_debug("%s: Unregistering character driver %s.\n", __func__,
411 char_driver->driver.name);
413 mutex_lock(&rmi_bus_mutex);
414 list_for_each_entry_safe(char_dev, m, &char_driver->devices,
416 list_del(&char_dev->list);
417 char_dev->driver->remove(char_dev);
419 list_for_each_entry_safe(entry, n, &rmi_character_drivers.list,
421 if (entry->cd == char_driver) {
422 list_del(&entry->list);
426 mutex_unlock(&rmi_bus_mutex);
428 driver_unregister(&char_driver->driver);
432 EXPORT_SYMBOL(rmi_unregister_character_driver);
434 static int __init rmi_bus_init(void)
438 mutex_init(&rmi_bus_mutex);
439 INIT_LIST_HEAD(&rmi_supported_functions.list);
440 INIT_LIST_HEAD(&rmi_character_drivers.list);
442 #ifdef CONFIG_RMI4_DEBUG
443 rmi_debugfs_root = debugfs_create_dir(rmi_bus_type.name, NULL);
444 if (!rmi_debugfs_root)
445 pr_err("%s: Failed to create debugfs root.\n", __func__);
446 else if (IS_ERR(rmi_debugfs_root)) {
447 pr_err("%s: Kernel may not contain debugfs support, code=%ld\n",
448 __func__, PTR_ERR(rmi_debugfs_root));
449 rmi_debugfs_root = NULL;
453 error = bus_register(&rmi_bus_type);
455 pr_err("%s: error registering the RMI bus: %d\n", __func__,
459 pr_debug("%s: successfully registered RMI bus.\n", __func__);
464 static void __exit rmi_bus_exit(void)
466 /* We should only ever get here if all drivers are unloaded, so
467 * all we have to do at this point is unregister ourselves.
469 #ifdef CONFIG_RMI4_DEBUG
470 if (rmi_debugfs_root)
471 debugfs_remove(rmi_debugfs_root);
473 bus_unregister(&rmi_bus_type);
476 module_init(rmi_bus_init);
477 module_exit(rmi_bus_exit);
479 MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com");
480 MODULE_DESCRIPTION("RMI bus");
481 MODULE_LICENSE("GPL");
482 MODULE_VERSION(RMI_DRIVER_VERSION);