core.c 14 KB

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