rmi_bus.c 11 KB

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