core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
  3. *
  4. * Based on drivers/spmi/spmi.c:
  5. * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 and
  9. * only version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/errno.h>
  17. #include <linux/idr.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/serdev.h>
  23. #include <linux/slab.h>
  24. static bool is_registered;
  25. static DEFINE_IDA(ctrl_ida);
  26. static void serdev_device_release(struct device *dev)
  27. {
  28. struct serdev_device *serdev = to_serdev_device(dev);
  29. kfree(serdev);
  30. }
  31. static const struct device_type serdev_device_type = {
  32. .release = serdev_device_release,
  33. };
  34. static void serdev_ctrl_release(struct device *dev)
  35. {
  36. struct serdev_controller *ctrl = to_serdev_controller(dev);
  37. ida_simple_remove(&ctrl_ida, ctrl->nr);
  38. kfree(ctrl);
  39. }
  40. static const struct device_type serdev_ctrl_type = {
  41. .release = serdev_ctrl_release,
  42. };
  43. static int serdev_device_match(struct device *dev, struct device_driver *drv)
  44. {
  45. /* TODO: ACPI and platform matching */
  46. return of_driver_match_device(dev, drv);
  47. }
  48. static int serdev_uevent(struct device *dev, struct kobj_uevent_env *env)
  49. {
  50. /* TODO: ACPI and platform modalias */
  51. return of_device_uevent_modalias(dev, env);
  52. }
  53. /**
  54. * serdev_device_add() - add a device previously constructed via serdev_device_alloc()
  55. * @serdev: serdev_device to be added
  56. */
  57. int serdev_device_add(struct serdev_device *serdev)
  58. {
  59. struct device *parent = serdev->dev.parent;
  60. int err;
  61. dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr);
  62. err = device_add(&serdev->dev);
  63. if (err < 0) {
  64. dev_err(&serdev->dev, "Can't add %s, status %d\n",
  65. dev_name(&serdev->dev), err);
  66. goto err_device_add;
  67. }
  68. dev_dbg(&serdev->dev, "device %s registered\n", dev_name(&serdev->dev));
  69. err_device_add:
  70. return err;
  71. }
  72. EXPORT_SYMBOL_GPL(serdev_device_add);
  73. /**
  74. * serdev_device_remove(): remove an serdev device
  75. * @serdev: serdev_device to be removed
  76. */
  77. void serdev_device_remove(struct serdev_device *serdev)
  78. {
  79. device_unregister(&serdev->dev);
  80. }
  81. EXPORT_SYMBOL_GPL(serdev_device_remove);
  82. int serdev_device_open(struct serdev_device *serdev)
  83. {
  84. struct serdev_controller *ctrl = serdev->ctrl;
  85. if (!ctrl || !ctrl->ops->open)
  86. return -EINVAL;
  87. return ctrl->ops->open(ctrl);
  88. }
  89. EXPORT_SYMBOL_GPL(serdev_device_open);
  90. void serdev_device_close(struct serdev_device *serdev)
  91. {
  92. struct serdev_controller *ctrl = serdev->ctrl;
  93. if (!ctrl || !ctrl->ops->close)
  94. return;
  95. ctrl->ops->close(ctrl);
  96. }
  97. EXPORT_SYMBOL_GPL(serdev_device_close);
  98. void serdev_device_write_wakeup(struct serdev_device *serdev)
  99. {
  100. complete(&serdev->write_comp);
  101. }
  102. EXPORT_SYMBOL_GPL(serdev_device_write_wakeup);
  103. int serdev_device_write_buf(struct serdev_device *serdev,
  104. const unsigned char *buf, size_t count)
  105. {
  106. struct serdev_controller *ctrl = serdev->ctrl;
  107. if (!ctrl || !ctrl->ops->write_buf)
  108. return -EINVAL;
  109. return ctrl->ops->write_buf(ctrl, buf, count);
  110. }
  111. EXPORT_SYMBOL_GPL(serdev_device_write_buf);
  112. int serdev_device_write(struct serdev_device *serdev,
  113. const unsigned char *buf, size_t count,
  114. unsigned long timeout)
  115. {
  116. struct serdev_controller *ctrl = serdev->ctrl;
  117. int ret;
  118. if (!ctrl || !ctrl->ops->write_buf ||
  119. (timeout && !serdev->ops->write_wakeup))
  120. return -EINVAL;
  121. mutex_lock(&serdev->write_lock);
  122. do {
  123. reinit_completion(&serdev->write_comp);
  124. ret = ctrl->ops->write_buf(ctrl, buf, count);
  125. if (ret < 0)
  126. break;
  127. buf += ret;
  128. count -= ret;
  129. } while (count &&
  130. (timeout = wait_for_completion_timeout(&serdev->write_comp,
  131. timeout)));
  132. mutex_unlock(&serdev->write_lock);
  133. return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
  134. }
  135. EXPORT_SYMBOL_GPL(serdev_device_write);
  136. void serdev_device_write_flush(struct serdev_device *serdev)
  137. {
  138. struct serdev_controller *ctrl = serdev->ctrl;
  139. if (!ctrl || !ctrl->ops->write_flush)
  140. return;
  141. ctrl->ops->write_flush(ctrl);
  142. }
  143. EXPORT_SYMBOL_GPL(serdev_device_write_flush);
  144. int serdev_device_write_room(struct serdev_device *serdev)
  145. {
  146. struct serdev_controller *ctrl = serdev->ctrl;
  147. if (!ctrl || !ctrl->ops->write_room)
  148. return 0;
  149. return serdev->ctrl->ops->write_room(ctrl);
  150. }
  151. EXPORT_SYMBOL_GPL(serdev_device_write_room);
  152. unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed)
  153. {
  154. struct serdev_controller *ctrl = serdev->ctrl;
  155. if (!ctrl || !ctrl->ops->set_baudrate)
  156. return 0;
  157. return ctrl->ops->set_baudrate(ctrl, speed);
  158. }
  159. EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
  160. void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable)
  161. {
  162. struct serdev_controller *ctrl = serdev->ctrl;
  163. if (!ctrl || !ctrl->ops->set_flow_control)
  164. return;
  165. ctrl->ops->set_flow_control(ctrl, enable);
  166. }
  167. EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);
  168. void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout)
  169. {
  170. struct serdev_controller *ctrl = serdev->ctrl;
  171. if (!ctrl || !ctrl->ops->wait_until_sent)
  172. return;
  173. ctrl->ops->wait_until_sent(ctrl, timeout);
  174. }
  175. EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent);
  176. int serdev_device_get_tiocm(struct serdev_device *serdev)
  177. {
  178. struct serdev_controller *ctrl = serdev->ctrl;
  179. if (!ctrl || !ctrl->ops->get_tiocm)
  180. return -ENOTSUPP;
  181. return ctrl->ops->get_tiocm(ctrl);
  182. }
  183. EXPORT_SYMBOL_GPL(serdev_device_get_tiocm);
  184. int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
  185. {
  186. struct serdev_controller *ctrl = serdev->ctrl;
  187. if (!ctrl || !ctrl->ops->set_tiocm)
  188. return -ENOTSUPP;
  189. return ctrl->ops->set_tiocm(ctrl, set, clear);
  190. }
  191. EXPORT_SYMBOL_GPL(serdev_device_set_tiocm);
  192. static int serdev_drv_probe(struct device *dev)
  193. {
  194. const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
  195. return sdrv->probe(to_serdev_device(dev));
  196. }
  197. static int serdev_drv_remove(struct device *dev)
  198. {
  199. const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
  200. sdrv->remove(to_serdev_device(dev));
  201. return 0;
  202. }
  203. static ssize_t modalias_show(struct device *dev,
  204. struct device_attribute *attr, char *buf)
  205. {
  206. return of_device_modalias(dev, buf, PAGE_SIZE);
  207. }
  208. DEVICE_ATTR_RO(modalias);
  209. static struct attribute *serdev_device_attrs[] = {
  210. &dev_attr_modalias.attr,
  211. NULL,
  212. };
  213. ATTRIBUTE_GROUPS(serdev_device);
  214. static struct bus_type serdev_bus_type = {
  215. .name = "serial",
  216. .match = serdev_device_match,
  217. .probe = serdev_drv_probe,
  218. .remove = serdev_drv_remove,
  219. .uevent = serdev_uevent,
  220. .dev_groups = serdev_device_groups,
  221. };
  222. /**
  223. * serdev_controller_alloc() - Allocate a new serdev device
  224. * @ctrl: associated controller
  225. *
  226. * Caller is responsible for either calling serdev_device_add() to add the
  227. * newly allocated controller, or calling serdev_device_put() to discard it.
  228. */
  229. struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
  230. {
  231. struct serdev_device *serdev;
  232. serdev = kzalloc(sizeof(*serdev), GFP_KERNEL);
  233. if (!serdev)
  234. return NULL;
  235. serdev->ctrl = ctrl;
  236. ctrl->serdev = serdev;
  237. device_initialize(&serdev->dev);
  238. serdev->dev.parent = &ctrl->dev;
  239. serdev->dev.bus = &serdev_bus_type;
  240. serdev->dev.type = &serdev_device_type;
  241. init_completion(&serdev->write_comp);
  242. mutex_init(&serdev->write_lock);
  243. return serdev;
  244. }
  245. EXPORT_SYMBOL_GPL(serdev_device_alloc);
  246. /**
  247. * serdev_controller_alloc() - Allocate a new serdev controller
  248. * @parent: parent device
  249. * @size: size of private data
  250. *
  251. * Caller is responsible for either calling serdev_controller_add() to add the
  252. * newly allocated controller, or calling serdev_controller_put() to discard it.
  253. * The allocated private data region may be accessed via
  254. * serdev_controller_get_drvdata()
  255. */
  256. struct serdev_controller *serdev_controller_alloc(struct device *parent,
  257. size_t size)
  258. {
  259. struct serdev_controller *ctrl;
  260. int id;
  261. if (WARN_ON(!parent))
  262. return NULL;
  263. ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
  264. if (!ctrl)
  265. return NULL;
  266. device_initialize(&ctrl->dev);
  267. ctrl->dev.type = &serdev_ctrl_type;
  268. ctrl->dev.bus = &serdev_bus_type;
  269. ctrl->dev.parent = parent;
  270. ctrl->dev.of_node = parent->of_node;
  271. serdev_controller_set_drvdata(ctrl, &ctrl[1]);
  272. id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
  273. if (id < 0) {
  274. dev_err(parent,
  275. "unable to allocate serdev controller identifier.\n");
  276. serdev_controller_put(ctrl);
  277. return NULL;
  278. }
  279. ctrl->nr = id;
  280. dev_set_name(&ctrl->dev, "serial%d", id);
  281. dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
  282. return ctrl;
  283. }
  284. EXPORT_SYMBOL_GPL(serdev_controller_alloc);
  285. static int of_serdev_register_devices(struct serdev_controller *ctrl)
  286. {
  287. struct device_node *node;
  288. struct serdev_device *serdev = NULL;
  289. int err;
  290. bool found = false;
  291. for_each_available_child_of_node(ctrl->dev.of_node, node) {
  292. if (!of_get_property(node, "compatible", NULL))
  293. continue;
  294. dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
  295. serdev = serdev_device_alloc(ctrl);
  296. if (!serdev)
  297. continue;
  298. serdev->dev.of_node = node;
  299. err = serdev_device_add(serdev);
  300. if (err) {
  301. dev_err(&serdev->dev,
  302. "failure adding device. status %d\n", err);
  303. serdev_device_put(serdev);
  304. } else
  305. found = true;
  306. }
  307. if (!found)
  308. return -ENODEV;
  309. return 0;
  310. }
  311. /**
  312. * serdev_controller_add() - Add an serdev controller
  313. * @ctrl: controller to be registered.
  314. *
  315. * Register a controller previously allocated via serdev_controller_alloc() with
  316. * the serdev core.
  317. */
  318. int serdev_controller_add(struct serdev_controller *ctrl)
  319. {
  320. int ret;
  321. /* Can't register until after driver model init */
  322. if (WARN_ON(!is_registered))
  323. return -EAGAIN;
  324. ret = device_add(&ctrl->dev);
  325. if (ret)
  326. return ret;
  327. ret = of_serdev_register_devices(ctrl);
  328. if (ret)
  329. goto out_dev_del;
  330. dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
  331. ctrl->nr, &ctrl->dev);
  332. return 0;
  333. out_dev_del:
  334. device_del(&ctrl->dev);
  335. return ret;
  336. };
  337. EXPORT_SYMBOL_GPL(serdev_controller_add);
  338. /* Remove a device associated with a controller */
  339. static int serdev_remove_device(struct device *dev, void *data)
  340. {
  341. struct serdev_device *serdev = to_serdev_device(dev);
  342. if (dev->type == &serdev_device_type)
  343. serdev_device_remove(serdev);
  344. return 0;
  345. }
  346. /**
  347. * serdev_controller_remove(): remove an serdev controller
  348. * @ctrl: controller to remove
  349. *
  350. * Remove a serdev controller. Caller is responsible for calling
  351. * serdev_controller_put() to discard the allocated controller.
  352. */
  353. void serdev_controller_remove(struct serdev_controller *ctrl)
  354. {
  355. int dummy;
  356. if (!ctrl)
  357. return;
  358. dummy = device_for_each_child(&ctrl->dev, NULL,
  359. serdev_remove_device);
  360. device_del(&ctrl->dev);
  361. }
  362. EXPORT_SYMBOL_GPL(serdev_controller_remove);
  363. /**
  364. * serdev_driver_register() - Register client driver with serdev core
  365. * @sdrv: client driver to be associated with client-device.
  366. *
  367. * This API will register the client driver with the serdev framework.
  368. * It is typically called from the driver's module-init function.
  369. */
  370. int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
  371. {
  372. sdrv->driver.bus = &serdev_bus_type;
  373. sdrv->driver.owner = owner;
  374. /* force drivers to async probe so I/O is possible in probe */
  375. sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
  376. return driver_register(&sdrv->driver);
  377. }
  378. EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
  379. static void __exit serdev_exit(void)
  380. {
  381. bus_unregister(&serdev_bus_type);
  382. }
  383. module_exit(serdev_exit);
  384. static int __init serdev_init(void)
  385. {
  386. int ret;
  387. ret = bus_register(&serdev_bus_type);
  388. if (ret)
  389. return ret;
  390. is_registered = true;
  391. return 0;
  392. }
  393. /* Must be before serial drivers register */
  394. postcore_initcall(serdev_init);
  395. MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
  396. MODULE_LICENSE("GPL v2");
  397. MODULE_DESCRIPTION("Serial attached device bus");