rmi_bus.c 9.7 KB

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