rmi_bus.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Copyright (c) 2011-2016 Synaptics Incorporated
  3. * Copyright (c) 2011 Unixphere
  4. *
  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. */
  9. #include <linux/kernel.h>
  10. #include <linux/device.h>
  11. #include <linux/kconfig.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"
  20. static int debug_flags;
  21. module_param(debug_flags, int, 0644);
  22. MODULE_PARM_DESC(debug_flags, "control debugging information");
  23. void rmi_dbg(int flags, struct device *dev, const char *fmt, ...)
  24. {
  25. struct va_format vaf;
  26. va_list args;
  27. if (flags & debug_flags) {
  28. va_start(args, fmt);
  29. vaf.fmt = fmt;
  30. vaf.va = &args;
  31. dev_printk(KERN_DEBUG, dev, "%pV", &vaf);
  32. va_end(args);
  33. }
  34. }
  35. EXPORT_SYMBOL_GPL(rmi_dbg);
  36. /*
  37. * RMI Physical devices
  38. *
  39. * Physical RMI device consists of several functions serving particular
  40. * purpose. For example F11 is a 2D touch sensor while F01 is a generic
  41. * function present in every RMI device.
  42. */
  43. static void rmi_release_device(struct device *dev)
  44. {
  45. struct rmi_device *rmi_dev = to_rmi_device(dev);
  46. kfree(rmi_dev);
  47. }
  48. static struct device_type rmi_device_type = {
  49. .name = "rmi4_sensor",
  50. .release = rmi_release_device,
  51. };
  52. bool rmi_is_physical_device(struct device *dev)
  53. {
  54. return dev->type == &rmi_device_type;
  55. }
  56. /**
  57. * rmi_register_transport_device - register a transport device connection
  58. * on the RMI bus. Transport drivers provide communication from the devices
  59. * on a bus (such as SPI, I2C, and so on) to the RMI4 sensor.
  60. *
  61. * @xport: the transport device to register
  62. */
  63. int rmi_register_transport_device(struct rmi_transport_dev *xport)
  64. {
  65. static atomic_t transport_device_count = ATOMIC_INIT(0);
  66. struct rmi_device *rmi_dev;
  67. int error;
  68. rmi_dev = kzalloc(sizeof(struct rmi_device), GFP_KERNEL);
  69. if (!rmi_dev)
  70. return -ENOMEM;
  71. device_initialize(&rmi_dev->dev);
  72. rmi_dev->xport = xport;
  73. rmi_dev->number = atomic_inc_return(&transport_device_count) - 1;
  74. dev_set_name(&rmi_dev->dev, "rmi4-%02d", rmi_dev->number);
  75. rmi_dev->dev.bus = &rmi_bus_type;
  76. rmi_dev->dev.type = &rmi_device_type;
  77. xport->rmi_dev = rmi_dev;
  78. error = device_add(&rmi_dev->dev);
  79. if (error)
  80. goto err_put_device;
  81. rmi_dbg(RMI_DEBUG_CORE, xport->dev,
  82. "%s: Registered %s as %s.\n", __func__,
  83. dev_name(rmi_dev->xport->dev), dev_name(&rmi_dev->dev));
  84. return 0;
  85. err_put_device:
  86. put_device(&rmi_dev->dev);
  87. return error;
  88. }
  89. EXPORT_SYMBOL_GPL(rmi_register_transport_device);
  90. /**
  91. * rmi_unregister_transport_device - unregister a transport device connection
  92. * @xport: the transport driver to unregister
  93. *
  94. */
  95. void rmi_unregister_transport_device(struct rmi_transport_dev *xport)
  96. {
  97. struct rmi_device *rmi_dev = xport->rmi_dev;
  98. device_del(&rmi_dev->dev);
  99. put_device(&rmi_dev->dev);
  100. }
  101. EXPORT_SYMBOL(rmi_unregister_transport_device);
  102. /* Function specific stuff */
  103. static void rmi_release_function(struct device *dev)
  104. {
  105. struct rmi_function *fn = to_rmi_function(dev);
  106. kfree(fn);
  107. }
  108. static struct device_type rmi_function_type = {
  109. .name = "rmi4_function",
  110. .release = rmi_release_function,
  111. };
  112. bool rmi_is_function_device(struct device *dev)
  113. {
  114. return dev->type == &rmi_function_type;
  115. }
  116. static int rmi_function_match(struct device *dev, struct device_driver *drv)
  117. {
  118. struct rmi_function_handler *handler = to_rmi_function_handler(drv);
  119. struct rmi_function *fn = to_rmi_function(dev);
  120. return fn->fd.function_number == handler->func;
  121. }
  122. #ifdef CONFIG_OF
  123. static void rmi_function_of_probe(struct rmi_function *fn)
  124. {
  125. char of_name[9];
  126. struct device_node *node = fn->rmi_dev->xport->dev->of_node;
  127. snprintf(of_name, sizeof(of_name), "rmi4-f%02x",
  128. fn->fd.function_number);
  129. fn->dev.of_node = of_get_child_by_name(node, of_name);
  130. }
  131. #else
  132. static inline void rmi_function_of_probe(struct rmi_function *fn)
  133. {}
  134. #endif
  135. static int rmi_function_probe(struct device *dev)
  136. {
  137. struct rmi_function *fn = to_rmi_function(dev);
  138. struct rmi_function_handler *handler =
  139. to_rmi_function_handler(dev->driver);
  140. int error;
  141. rmi_function_of_probe(fn);
  142. if (handler->probe) {
  143. error = handler->probe(fn);
  144. return error;
  145. }
  146. return 0;
  147. }
  148. static int rmi_function_remove(struct device *dev)
  149. {
  150. struct rmi_function *fn = to_rmi_function(dev);
  151. struct rmi_function_handler *handler =
  152. to_rmi_function_handler(dev->driver);
  153. if (handler->remove)
  154. handler->remove(fn);
  155. return 0;
  156. }
  157. int rmi_register_function(struct rmi_function *fn)
  158. {
  159. struct rmi_device *rmi_dev = fn->rmi_dev;
  160. int error;
  161. device_initialize(&fn->dev);
  162. dev_set_name(&fn->dev, "%s.fn%02x",
  163. dev_name(&rmi_dev->dev), fn->fd.function_number);
  164. fn->dev.parent = &rmi_dev->dev;
  165. fn->dev.type = &rmi_function_type;
  166. fn->dev.bus = &rmi_bus_type;
  167. error = device_add(&fn->dev);
  168. if (error) {
  169. dev_err(&rmi_dev->dev,
  170. "Failed device_register function device %s\n",
  171. dev_name(&fn->dev));
  172. goto err_put_device;
  173. }
  174. rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Registered F%02X.\n",
  175. fn->fd.function_number);
  176. return 0;
  177. err_put_device:
  178. put_device(&fn->dev);
  179. return error;
  180. }
  181. void rmi_unregister_function(struct rmi_function *fn)
  182. {
  183. device_del(&fn->dev);
  184. of_node_put(fn->dev.of_node);
  185. put_device(&fn->dev);
  186. }
  187. /**
  188. * rmi_register_function_handler - register a handler for an RMI function
  189. * @handler: RMI handler that should be registered.
  190. * @module: pointer to module that implements the handler
  191. * @mod_name: name of the module implementing the handler
  192. *
  193. * This function performs additional setup of RMI function handler and
  194. * registers it with the RMI core so that it can be bound to
  195. * RMI function devices.
  196. */
  197. int __rmi_register_function_handler(struct rmi_function_handler *handler,
  198. struct module *owner,
  199. const char *mod_name)
  200. {
  201. struct device_driver *driver = &handler->driver;
  202. int error;
  203. driver->bus = &rmi_bus_type;
  204. driver->owner = owner;
  205. driver->mod_name = mod_name;
  206. driver->probe = rmi_function_probe;
  207. driver->remove = rmi_function_remove;
  208. error = driver_register(&handler->driver);
  209. if (error) {
  210. pr_err("driver_register() failed for %s, error: %d\n",
  211. handler->driver.name, error);
  212. return error;
  213. }
  214. return 0;
  215. }
  216. EXPORT_SYMBOL_GPL(__rmi_register_function_handler);
  217. /**
  218. * rmi_unregister_function_handler - unregister given RMI function handler
  219. * @handler: RMI handler that should be unregistered.
  220. *
  221. * This function unregisters given function handler from RMI core which
  222. * causes it to be unbound from the function devices.
  223. */
  224. void rmi_unregister_function_handler(struct rmi_function_handler *handler)
  225. {
  226. driver_unregister(&handler->driver);
  227. }
  228. EXPORT_SYMBOL_GPL(rmi_unregister_function_handler);
  229. /* Bus specific stuff */
  230. static int rmi_bus_match(struct device *dev, struct device_driver *drv)
  231. {
  232. bool physical = rmi_is_physical_device(dev);
  233. /* First see if types are not compatible */
  234. if (physical != rmi_is_physical_driver(drv))
  235. return 0;
  236. return physical || rmi_function_match(dev, drv);
  237. }
  238. struct bus_type rmi_bus_type = {
  239. .match = rmi_bus_match,
  240. .name = "rmi4",
  241. };
  242. static struct rmi_function_handler *fn_handlers[] = {
  243. &rmi_f01_handler,
  244. #ifdef CONFIG_RMI4_F11
  245. &rmi_f11_handler,
  246. #endif
  247. #ifdef CONFIG_RMI4_F12
  248. &rmi_f12_handler,
  249. #endif
  250. #ifdef CONFIG_RMI4_F30
  251. &rmi_f30_handler,
  252. #endif
  253. };
  254. static void __rmi_unregister_function_handlers(int start_idx)
  255. {
  256. int i;
  257. for (i = start_idx; i >= 0; i--)
  258. rmi_unregister_function_handler(fn_handlers[i]);
  259. }
  260. static void rmi_unregister_function_handlers(void)
  261. {
  262. __rmi_unregister_function_handlers(ARRAY_SIZE(fn_handlers) - 1);
  263. }
  264. static int rmi_register_function_handlers(void)
  265. {
  266. int ret;
  267. int i;
  268. for (i = 0; i < ARRAY_SIZE(fn_handlers); i++) {
  269. ret = rmi_register_function_handler(fn_handlers[i]);
  270. if (ret) {
  271. pr_err("%s: error registering the RMI F%02x handler: %d\n",
  272. __func__, fn_handlers[i]->func, ret);
  273. goto err_unregister_function_handlers;
  274. }
  275. }
  276. return 0;
  277. err_unregister_function_handlers:
  278. __rmi_unregister_function_handlers(i - 1);
  279. return ret;
  280. }
  281. int rmi_of_property_read_u32(struct device *dev, u32 *result,
  282. const char *prop, bool optional)
  283. {
  284. int retval;
  285. u32 val = 0;
  286. retval = of_property_read_u32(dev->of_node, prop, &val);
  287. if (retval && (!optional && retval == -EINVAL)) {
  288. dev_err(dev, "Failed to get %s value: %d\n",
  289. prop, retval);
  290. return retval;
  291. }
  292. *result = val;
  293. return 0;
  294. }
  295. EXPORT_SYMBOL_GPL(rmi_of_property_read_u32);
  296. static int __init rmi_bus_init(void)
  297. {
  298. int error;
  299. error = bus_register(&rmi_bus_type);
  300. if (error) {
  301. pr_err("%s: error registering the RMI bus: %d\n",
  302. __func__, error);
  303. return error;
  304. }
  305. error = rmi_register_function_handlers();
  306. if (error)
  307. goto err_unregister_bus;
  308. error = rmi_register_physical_driver();
  309. if (error) {
  310. pr_err("%s: error registering the RMI physical driver: %d\n",
  311. __func__, error);
  312. goto err_unregister_bus;
  313. }
  314. return 0;
  315. err_unregister_bus:
  316. bus_unregister(&rmi_bus_type);
  317. return error;
  318. }
  319. module_init(rmi_bus_init);
  320. static void __exit rmi_bus_exit(void)
  321. {
  322. /*
  323. * We should only ever get here if all drivers are unloaded, so
  324. * all we have to do at this point is unregister ourselves.
  325. */
  326. rmi_unregister_physical_driver();
  327. rmi_unregister_function_handlers();
  328. bus_unregister(&rmi_bus_type);
  329. }
  330. module_exit(rmi_bus_exit);
  331. MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com");
  332. MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com");
  333. MODULE_DESCRIPTION("RMI bus");
  334. MODULE_LICENSE("GPL");
  335. MODULE_VERSION(RMI_DRIVER_VERSION);