core.c 13 KB

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