rmi_bus.c 11 KB

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