rmi_bus.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. device_del(&fn->dev);
  183. of_node_put(fn->dev.of_node);
  184. put_device(&fn->dev);
  185. }
  186. /**
  187. * rmi_register_function_handler - register a handler for an RMI function
  188. * @handler: RMI handler that should be registered.
  189. * @module: pointer to module that implements the handler
  190. * @mod_name: name of the module implementing the handler
  191. *
  192. * This function performs additional setup of RMI function handler and
  193. * registers it with the RMI core so that it can be bound to
  194. * RMI function devices.
  195. */
  196. int __rmi_register_function_handler(struct rmi_function_handler *handler,
  197. struct module *owner,
  198. const char *mod_name)
  199. {
  200. struct device_driver *driver = &handler->driver;
  201. int error;
  202. driver->bus = &rmi_bus_type;
  203. driver->owner = owner;
  204. driver->mod_name = mod_name;
  205. driver->probe = rmi_function_probe;
  206. driver->remove = rmi_function_remove;
  207. error = driver_register(&handler->driver);
  208. if (error) {
  209. pr_err("driver_register() failed for %s, error: %d\n",
  210. handler->driver.name, error);
  211. return error;
  212. }
  213. return 0;
  214. }
  215. EXPORT_SYMBOL_GPL(__rmi_register_function_handler);
  216. /**
  217. * rmi_unregister_function_handler - unregister given RMI function handler
  218. * @handler: RMI handler that should be unregistered.
  219. *
  220. * This function unregisters given function handler from RMI core which
  221. * causes it to be unbound from the function devices.
  222. */
  223. void rmi_unregister_function_handler(struct rmi_function_handler *handler)
  224. {
  225. driver_unregister(&handler->driver);
  226. }
  227. EXPORT_SYMBOL_GPL(rmi_unregister_function_handler);
  228. /* Bus specific stuff */
  229. static int rmi_bus_match(struct device *dev, struct device_driver *drv)
  230. {
  231. bool physical = rmi_is_physical_device(dev);
  232. /* First see if types are not compatible */
  233. if (physical != rmi_is_physical_driver(drv))
  234. return 0;
  235. return physical || rmi_function_match(dev, drv);
  236. }
  237. struct bus_type rmi_bus_type = {
  238. .match = rmi_bus_match,
  239. .name = "rmi4",
  240. };
  241. static struct rmi_function_handler *fn_handlers[] = {
  242. &rmi_f01_handler,
  243. #ifdef CONFIG_RMI4_F11
  244. &rmi_f11_handler,
  245. #endif
  246. #ifdef CONFIG_RMI4_F12
  247. &rmi_f12_handler,
  248. #endif
  249. #ifdef CONFIG_RMI4_F30
  250. &rmi_f30_handler,
  251. #endif
  252. #ifdef CONFIG_RMI4_F54
  253. &rmi_f54_handler,
  254. #endif
  255. };
  256. static void __rmi_unregister_function_handlers(int start_idx)
  257. {
  258. int i;
  259. for (i = start_idx; i >= 0; i--)
  260. rmi_unregister_function_handler(fn_handlers[i]);
  261. }
  262. static void rmi_unregister_function_handlers(void)
  263. {
  264. __rmi_unregister_function_handlers(ARRAY_SIZE(fn_handlers) - 1);
  265. }
  266. static int rmi_register_function_handlers(void)
  267. {
  268. int ret;
  269. int i;
  270. for (i = 0; i < ARRAY_SIZE(fn_handlers); i++) {
  271. ret = rmi_register_function_handler(fn_handlers[i]);
  272. if (ret) {
  273. pr_err("%s: error registering the RMI F%02x handler: %d\n",
  274. __func__, fn_handlers[i]->func, ret);
  275. goto err_unregister_function_handlers;
  276. }
  277. }
  278. return 0;
  279. err_unregister_function_handlers:
  280. __rmi_unregister_function_handlers(i - 1);
  281. return ret;
  282. }
  283. int rmi_of_property_read_u32(struct device *dev, u32 *result,
  284. const char *prop, bool optional)
  285. {
  286. int retval;
  287. u32 val = 0;
  288. retval = of_property_read_u32(dev->of_node, prop, &val);
  289. if (retval && (!optional && retval == -EINVAL)) {
  290. dev_err(dev, "Failed to get %s value: %d\n",
  291. prop, retval);
  292. return retval;
  293. }
  294. *result = val;
  295. return 0;
  296. }
  297. EXPORT_SYMBOL_GPL(rmi_of_property_read_u32);
  298. static int __init rmi_bus_init(void)
  299. {
  300. int error;
  301. error = bus_register(&rmi_bus_type);
  302. if (error) {
  303. pr_err("%s: error registering the RMI bus: %d\n",
  304. __func__, error);
  305. return error;
  306. }
  307. error = rmi_register_function_handlers();
  308. if (error)
  309. goto err_unregister_bus;
  310. error = rmi_register_physical_driver();
  311. if (error) {
  312. pr_err("%s: error registering the RMI physical driver: %d\n",
  313. __func__, error);
  314. goto err_unregister_bus;
  315. }
  316. return 0;
  317. err_unregister_bus:
  318. bus_unregister(&rmi_bus_type);
  319. return error;
  320. }
  321. module_init(rmi_bus_init);
  322. static void __exit rmi_bus_exit(void)
  323. {
  324. /*
  325. * We should only ever get here if all drivers are unloaded, so
  326. * all we have to do at this point is unregister ourselves.
  327. */
  328. rmi_unregister_physical_driver();
  329. rmi_unregister_function_handlers();
  330. bus_unregister(&rmi_bus_type);
  331. }
  332. module_exit(rmi_bus_exit);
  333. MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com");
  334. MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com");
  335. MODULE_DESCRIPTION("RMI bus");
  336. MODULE_LICENSE("GPL");
  337. MODULE_VERSION(RMI_DRIVER_VERSION);