core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2011-2017, The Linux Foundation
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/errno.h>
  7. #include <linux/slab.h>
  8. #include <linux/init.h>
  9. #include <linux/idr.h>
  10. #include <linux/of.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/slimbus.h>
  13. #include "slimbus.h"
  14. static DEFINE_IDA(ctrl_ida);
  15. static const struct slim_device_id *slim_match(const struct slim_device_id *id,
  16. const struct slim_device *sbdev)
  17. {
  18. while (id->manf_id != 0 || id->prod_code != 0) {
  19. if (id->manf_id == sbdev->e_addr.manf_id &&
  20. id->prod_code == sbdev->e_addr.prod_code)
  21. return id;
  22. id++;
  23. }
  24. return NULL;
  25. }
  26. static int slim_device_match(struct device *dev, struct device_driver *drv)
  27. {
  28. struct slim_device *sbdev = to_slim_device(dev);
  29. struct slim_driver *sbdrv = to_slim_driver(drv);
  30. return !!slim_match(sbdrv->id_table, sbdev);
  31. }
  32. static int slim_device_probe(struct device *dev)
  33. {
  34. struct slim_device *sbdev = to_slim_device(dev);
  35. struct slim_driver *sbdrv = to_slim_driver(dev->driver);
  36. return sbdrv->probe(sbdev);
  37. }
  38. static int slim_device_remove(struct device *dev)
  39. {
  40. struct slim_device *sbdev = to_slim_device(dev);
  41. struct slim_driver *sbdrv;
  42. if (dev->driver) {
  43. sbdrv = to_slim_driver(dev->driver);
  44. if (sbdrv->remove)
  45. sbdrv->remove(sbdev);
  46. }
  47. return 0;
  48. }
  49. struct bus_type slimbus_bus = {
  50. .name = "slimbus",
  51. .match = slim_device_match,
  52. .probe = slim_device_probe,
  53. .remove = slim_device_remove,
  54. };
  55. EXPORT_SYMBOL_GPL(slimbus_bus);
  56. /*
  57. * __slim_driver_register() - Client driver registration with SLIMbus
  58. *
  59. * @drv:Client driver to be associated with client-device.
  60. * @owner: owning module/driver
  61. *
  62. * This API will register the client driver with the SLIMbus
  63. * It is called from the driver's module-init function.
  64. */
  65. int __slim_driver_register(struct slim_driver *drv, struct module *owner)
  66. {
  67. /* ID table and probe are mandatory */
  68. if (!drv->id_table || !drv->probe)
  69. return -EINVAL;
  70. drv->driver.bus = &slimbus_bus;
  71. drv->driver.owner = owner;
  72. return driver_register(&drv->driver);
  73. }
  74. EXPORT_SYMBOL_GPL(__slim_driver_register);
  75. /*
  76. * slim_driver_unregister() - Undo effect of slim_driver_register
  77. *
  78. * @drv: Client driver to be unregistered
  79. */
  80. void slim_driver_unregister(struct slim_driver *drv)
  81. {
  82. driver_unregister(&drv->driver);
  83. }
  84. EXPORT_SYMBOL_GPL(slim_driver_unregister);
  85. static void slim_dev_release(struct device *dev)
  86. {
  87. struct slim_device *sbdev = to_slim_device(dev);
  88. kfree(sbdev);
  89. }
  90. static int slim_add_device(struct slim_controller *ctrl,
  91. struct slim_device *sbdev,
  92. struct device_node *node)
  93. {
  94. sbdev->dev.bus = &slimbus_bus;
  95. sbdev->dev.parent = ctrl->dev;
  96. sbdev->dev.release = slim_dev_release;
  97. sbdev->dev.driver = NULL;
  98. sbdev->ctrl = ctrl;
  99. INIT_LIST_HEAD(&sbdev->stream_list);
  100. spin_lock_init(&sbdev->stream_list_lock);
  101. if (node)
  102. sbdev->dev.of_node = of_node_get(node);
  103. dev_set_name(&sbdev->dev, "%x:%x:%x:%x",
  104. sbdev->e_addr.manf_id,
  105. sbdev->e_addr.prod_code,
  106. sbdev->e_addr.dev_index,
  107. sbdev->e_addr.instance);
  108. return device_register(&sbdev->dev);
  109. }
  110. static struct slim_device *slim_alloc_device(struct slim_controller *ctrl,
  111. struct slim_eaddr *eaddr,
  112. struct device_node *node)
  113. {
  114. struct slim_device *sbdev;
  115. int ret;
  116. sbdev = kzalloc(sizeof(*sbdev), GFP_KERNEL);
  117. if (!sbdev)
  118. return NULL;
  119. sbdev->e_addr = *eaddr;
  120. ret = slim_add_device(ctrl, sbdev, node);
  121. if (ret) {
  122. put_device(&sbdev->dev);
  123. return NULL;
  124. }
  125. return sbdev;
  126. }
  127. static void of_register_slim_devices(struct slim_controller *ctrl)
  128. {
  129. struct device *dev = ctrl->dev;
  130. struct device_node *node;
  131. if (!ctrl->dev->of_node)
  132. return;
  133. for_each_child_of_node(ctrl->dev->of_node, node) {
  134. struct slim_device *sbdev;
  135. struct slim_eaddr e_addr;
  136. const char *compat = NULL;
  137. int reg[2], ret;
  138. int manf_id, prod_code;
  139. compat = of_get_property(node, "compatible", NULL);
  140. if (!compat)
  141. continue;
  142. ret = sscanf(compat, "slim%x,%x", &manf_id, &prod_code);
  143. if (ret != 2) {
  144. dev_err(dev, "Manf ID & Product code not found %s\n",
  145. compat);
  146. continue;
  147. }
  148. ret = of_property_read_u32_array(node, "reg", reg, 2);
  149. if (ret) {
  150. dev_err(dev, "Device and Instance id not found:%d\n",
  151. ret);
  152. continue;
  153. }
  154. e_addr.dev_index = reg[0];
  155. e_addr.instance = reg[1];
  156. e_addr.manf_id = manf_id;
  157. e_addr.prod_code = prod_code;
  158. sbdev = slim_alloc_device(ctrl, &e_addr, node);
  159. if (!sbdev)
  160. continue;
  161. }
  162. }
  163. /*
  164. * slim_register_controller() - Controller bring-up and registration.
  165. *
  166. * @ctrl: Controller to be registered.
  167. *
  168. * A controller is registered with the framework using this API.
  169. * If devices on a controller were registered before controller,
  170. * this will make sure that they get probed when controller is up
  171. */
  172. int slim_register_controller(struct slim_controller *ctrl)
  173. {
  174. int id;
  175. id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
  176. if (id < 0)
  177. return id;
  178. ctrl->id = id;
  179. if (!ctrl->min_cg)
  180. ctrl->min_cg = SLIM_MIN_CLK_GEAR;
  181. if (!ctrl->max_cg)
  182. ctrl->max_cg = SLIM_MAX_CLK_GEAR;
  183. ida_init(&ctrl->laddr_ida);
  184. idr_init(&ctrl->tid_idr);
  185. mutex_init(&ctrl->lock);
  186. mutex_init(&ctrl->sched.m_reconf);
  187. init_completion(&ctrl->sched.pause_comp);
  188. dev_dbg(ctrl->dev, "Bus [%s] registered:dev:%p\n",
  189. ctrl->name, ctrl->dev);
  190. of_register_slim_devices(ctrl);
  191. return 0;
  192. }
  193. EXPORT_SYMBOL_GPL(slim_register_controller);
  194. /* slim_remove_device: Remove the effect of slim_add_device() */
  195. static void slim_remove_device(struct slim_device *sbdev)
  196. {
  197. device_unregister(&sbdev->dev);
  198. }
  199. static int slim_ctrl_remove_device(struct device *dev, void *null)
  200. {
  201. slim_remove_device(to_slim_device(dev));
  202. return 0;
  203. }
  204. /**
  205. * slim_unregister_controller() - Controller tear-down.
  206. *
  207. * @ctrl: Controller to tear-down.
  208. */
  209. int slim_unregister_controller(struct slim_controller *ctrl)
  210. {
  211. /* Remove all clients */
  212. device_for_each_child(ctrl->dev, NULL, slim_ctrl_remove_device);
  213. /* Enter Clock Pause */
  214. slim_ctrl_clk_pause(ctrl, false, 0);
  215. ida_simple_remove(&ctrl_ida, ctrl->id);
  216. return 0;
  217. }
  218. EXPORT_SYMBOL_GPL(slim_unregister_controller);
  219. static void slim_device_update_status(struct slim_device *sbdev,
  220. enum slim_device_status status)
  221. {
  222. struct slim_driver *sbdrv;
  223. if (sbdev->status == status)
  224. return;
  225. sbdev->status = status;
  226. if (!sbdev->dev.driver)
  227. return;
  228. sbdrv = to_slim_driver(sbdev->dev.driver);
  229. if (sbdrv->device_status)
  230. sbdrv->device_status(sbdev, sbdev->status);
  231. }
  232. /**
  233. * slim_report_absent() - Controller calls this function when a device
  234. * reports absent, OR when the device cannot be communicated with
  235. *
  236. * @sbdev: Device that cannot be reached, or sent report absent
  237. */
  238. void slim_report_absent(struct slim_device *sbdev)
  239. {
  240. struct slim_controller *ctrl = sbdev->ctrl;
  241. if (!ctrl)
  242. return;
  243. /* invalidate logical addresses */
  244. mutex_lock(&ctrl->lock);
  245. sbdev->is_laddr_valid = false;
  246. mutex_unlock(&ctrl->lock);
  247. ida_simple_remove(&ctrl->laddr_ida, sbdev->laddr);
  248. slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_DOWN);
  249. }
  250. EXPORT_SYMBOL_GPL(slim_report_absent);
  251. static bool slim_eaddr_equal(struct slim_eaddr *a, struct slim_eaddr *b)
  252. {
  253. return (a->manf_id == b->manf_id &&
  254. a->prod_code == b->prod_code &&
  255. a->dev_index == b->dev_index &&
  256. a->instance == b->instance);
  257. }
  258. static int slim_match_dev(struct device *dev, void *data)
  259. {
  260. struct slim_eaddr *e_addr = data;
  261. struct slim_device *sbdev = to_slim_device(dev);
  262. return slim_eaddr_equal(&sbdev->e_addr, e_addr);
  263. }
  264. static struct slim_device *find_slim_device(struct slim_controller *ctrl,
  265. struct slim_eaddr *eaddr)
  266. {
  267. struct slim_device *sbdev;
  268. struct device *dev;
  269. dev = device_find_child(ctrl->dev, eaddr, slim_match_dev);
  270. if (dev) {
  271. sbdev = to_slim_device(dev);
  272. return sbdev;
  273. }
  274. return NULL;
  275. }
  276. /**
  277. * slim_get_device() - get handle to a device.
  278. *
  279. * @ctrl: Controller on which this device will be added/queried
  280. * @e_addr: Enumeration address of the device to be queried
  281. *
  282. * Return: pointer to a device if it has already reported. Creates a new
  283. * device and returns pointer to it if the device has not yet enumerated.
  284. */
  285. struct slim_device *slim_get_device(struct slim_controller *ctrl,
  286. struct slim_eaddr *e_addr)
  287. {
  288. struct slim_device *sbdev;
  289. sbdev = find_slim_device(ctrl, e_addr);
  290. if (!sbdev) {
  291. sbdev = slim_alloc_device(ctrl, e_addr, NULL);
  292. if (!sbdev)
  293. return ERR_PTR(-ENOMEM);
  294. }
  295. return sbdev;
  296. }
  297. EXPORT_SYMBOL_GPL(slim_get_device);
  298. static int of_slim_match_dev(struct device *dev, void *data)
  299. {
  300. struct device_node *np = data;
  301. struct slim_device *sbdev = to_slim_device(dev);
  302. return (sbdev->dev.of_node == np);
  303. }
  304. static struct slim_device *of_find_slim_device(struct slim_controller *ctrl,
  305. struct device_node *np)
  306. {
  307. struct slim_device *sbdev;
  308. struct device *dev;
  309. dev = device_find_child(ctrl->dev, np, of_slim_match_dev);
  310. if (dev) {
  311. sbdev = to_slim_device(dev);
  312. return sbdev;
  313. }
  314. return NULL;
  315. }
  316. /**
  317. * of_slim_get_device() - get handle to a device using dt node.
  318. *
  319. * @ctrl: Controller on which this device will be added/queried
  320. * @np: node pointer to device
  321. *
  322. * Return: pointer to a device if it has already reported. Creates a new
  323. * device and returns pointer to it if the device has not yet enumerated.
  324. */
  325. struct slim_device *of_slim_get_device(struct slim_controller *ctrl,
  326. struct device_node *np)
  327. {
  328. return of_find_slim_device(ctrl, np);
  329. }
  330. EXPORT_SYMBOL_GPL(of_slim_get_device);
  331. static int slim_device_alloc_laddr(struct slim_device *sbdev,
  332. bool report_present)
  333. {
  334. struct slim_controller *ctrl = sbdev->ctrl;
  335. u8 laddr;
  336. int ret;
  337. mutex_lock(&ctrl->lock);
  338. if (ctrl->get_laddr) {
  339. ret = ctrl->get_laddr(ctrl, &sbdev->e_addr, &laddr);
  340. if (ret < 0)
  341. goto err;
  342. } else if (report_present) {
  343. ret = ida_simple_get(&ctrl->laddr_ida,
  344. 0, SLIM_LA_MANAGER - 1, GFP_KERNEL);
  345. if (ret < 0)
  346. goto err;
  347. laddr = ret;
  348. } else {
  349. ret = -EINVAL;
  350. goto err;
  351. }
  352. if (ctrl->set_laddr) {
  353. ret = ctrl->set_laddr(ctrl, &sbdev->e_addr, laddr);
  354. if (ret) {
  355. ret = -EINVAL;
  356. goto err;
  357. }
  358. }
  359. sbdev->laddr = laddr;
  360. sbdev->is_laddr_valid = true;
  361. slim_device_update_status(sbdev, SLIM_DEVICE_STATUS_UP);
  362. dev_dbg(ctrl->dev, "setting slimbus l-addr:%x, ea:%x,%x,%x,%x\n",
  363. laddr, sbdev->e_addr.manf_id, sbdev->e_addr.prod_code,
  364. sbdev->e_addr.dev_index, sbdev->e_addr.instance);
  365. err:
  366. mutex_unlock(&ctrl->lock);
  367. return ret;
  368. }
  369. /**
  370. * slim_device_report_present() - Report enumerated device.
  371. *
  372. * @ctrl: Controller with which device is enumerated.
  373. * @e_addr: Enumeration address of the device.
  374. * @laddr: Return logical address (if valid flag is false)
  375. *
  376. * Called by controller in response to REPORT_PRESENT. Framework will assign
  377. * a logical address to this enumeration address.
  378. * Function returns -EXFULL to indicate that all logical addresses are already
  379. * taken.
  380. */
  381. int slim_device_report_present(struct slim_controller *ctrl,
  382. struct slim_eaddr *e_addr, u8 *laddr)
  383. {
  384. struct slim_device *sbdev;
  385. int ret;
  386. ret = pm_runtime_get_sync(ctrl->dev);
  387. if (ctrl->sched.clk_state != SLIM_CLK_ACTIVE) {
  388. dev_err(ctrl->dev, "slim ctrl not active,state:%d, ret:%d\n",
  389. ctrl->sched.clk_state, ret);
  390. goto slimbus_not_active;
  391. }
  392. sbdev = slim_get_device(ctrl, e_addr);
  393. if (IS_ERR(sbdev))
  394. return -ENODEV;
  395. if (sbdev->is_laddr_valid) {
  396. *laddr = sbdev->laddr;
  397. return 0;
  398. }
  399. ret = slim_device_alloc_laddr(sbdev, true);
  400. slimbus_not_active:
  401. pm_runtime_mark_last_busy(ctrl->dev);
  402. pm_runtime_put_autosuspend(ctrl->dev);
  403. return ret;
  404. }
  405. EXPORT_SYMBOL_GPL(slim_device_report_present);
  406. /**
  407. * slim_get_logical_addr() - get/allocate logical address of a SLIMbus device.
  408. *
  409. * @sbdev: client handle requesting the address.
  410. *
  411. * Return: zero if a logical address is valid or a new logical address
  412. * has been assigned. error code in case of error.
  413. */
  414. int slim_get_logical_addr(struct slim_device *sbdev)
  415. {
  416. if (!sbdev->is_laddr_valid)
  417. return slim_device_alloc_laddr(sbdev, false);
  418. return 0;
  419. }
  420. EXPORT_SYMBOL_GPL(slim_get_logical_addr);
  421. static void __exit slimbus_exit(void)
  422. {
  423. bus_unregister(&slimbus_bus);
  424. }
  425. module_exit(slimbus_exit);
  426. static int __init slimbus_init(void)
  427. {
  428. return bus_register(&slimbus_bus);
  429. }
  430. postcore_initcall(slimbus_init);
  431. MODULE_LICENSE("GPL v2");
  432. MODULE_DESCRIPTION("SLIMbus core");