core.c 14 KB

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