spmi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/errno.h>
  7. #include <linux/idr.h>
  8. #include <linux/slab.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/of_device.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/spmi.h>
  14. #include <linux/pm_runtime.h>
  15. #include <dt-bindings/spmi/spmi.h>
  16. #define CREATE_TRACE_POINTS
  17. #include <trace/events/spmi.h>
  18. static bool is_registered;
  19. static DEFINE_IDA(ctrl_ida);
  20. static void spmi_dev_release(struct device *dev)
  21. {
  22. struct spmi_device *sdev = to_spmi_device(dev);
  23. kfree(sdev);
  24. }
  25. static const struct device_type spmi_dev_type = {
  26. .release = spmi_dev_release,
  27. };
  28. static void spmi_ctrl_release(struct device *dev)
  29. {
  30. struct spmi_controller *ctrl = to_spmi_controller(dev);
  31. ida_free(&ctrl_ida, ctrl->nr);
  32. kfree(ctrl);
  33. }
  34. static const struct device_type spmi_ctrl_type = {
  35. .release = spmi_ctrl_release,
  36. };
  37. static int spmi_device_match(struct device *dev, struct device_driver *drv)
  38. {
  39. if (of_driver_match_device(dev, drv))
  40. return 1;
  41. if (drv->name)
  42. return strncmp(dev_name(dev), drv->name,
  43. SPMI_NAME_SIZE) == 0;
  44. return 0;
  45. }
  46. /**
  47. * spmi_device_add() - add a device previously constructed via spmi_device_alloc()
  48. * @sdev: spmi_device to be added
  49. */
  50. int spmi_device_add(struct spmi_device *sdev)
  51. {
  52. struct spmi_controller *ctrl = sdev->ctrl;
  53. int err;
  54. dev_set_name(&sdev->dev, "%d-%02x", ctrl->nr, sdev->usid);
  55. err = device_add(&sdev->dev);
  56. if (err < 0) {
  57. dev_err(&sdev->dev, "Can't add %s, status %d\n",
  58. dev_name(&sdev->dev), err);
  59. goto err_device_add;
  60. }
  61. dev_dbg(&sdev->dev, "device %s registered\n", dev_name(&sdev->dev));
  62. err_device_add:
  63. return err;
  64. }
  65. EXPORT_SYMBOL_GPL(spmi_device_add);
  66. /**
  67. * spmi_device_remove(): remove an SPMI device
  68. * @sdev: spmi_device to be removed
  69. */
  70. void spmi_device_remove(struct spmi_device *sdev)
  71. {
  72. device_unregister(&sdev->dev);
  73. }
  74. EXPORT_SYMBOL_GPL(spmi_device_remove);
  75. static inline int
  76. spmi_cmd(struct spmi_controller *ctrl, u8 opcode, u8 sid)
  77. {
  78. int ret;
  79. if (!ctrl || !ctrl->cmd || ctrl->dev.type != &spmi_ctrl_type)
  80. return -EINVAL;
  81. ret = ctrl->cmd(ctrl, opcode, sid);
  82. trace_spmi_cmd(opcode, sid, ret);
  83. return ret;
  84. }
  85. static inline int spmi_read_cmd(struct spmi_controller *ctrl, u8 opcode,
  86. u8 sid, u16 addr, u8 *buf, size_t len)
  87. {
  88. int ret;
  89. if (!ctrl || !ctrl->read_cmd || ctrl->dev.type != &spmi_ctrl_type)
  90. return -EINVAL;
  91. trace_spmi_read_begin(opcode, sid, addr);
  92. ret = ctrl->read_cmd(ctrl, opcode, sid, addr, buf, len);
  93. trace_spmi_read_end(opcode, sid, addr, ret, len, buf);
  94. return ret;
  95. }
  96. static inline int spmi_write_cmd(struct spmi_controller *ctrl, u8 opcode,
  97. u8 sid, u16 addr, const u8 *buf, size_t len)
  98. {
  99. int ret;
  100. if (!ctrl || !ctrl->write_cmd || ctrl->dev.type != &spmi_ctrl_type)
  101. return -EINVAL;
  102. trace_spmi_write_begin(opcode, sid, addr, len, buf);
  103. ret = ctrl->write_cmd(ctrl, opcode, sid, addr, buf, len);
  104. trace_spmi_write_end(opcode, sid, addr, ret);
  105. return ret;
  106. }
  107. /**
  108. * spmi_register_read() - register read
  109. * @sdev: SPMI device.
  110. * @addr: slave register address (5-bit address).
  111. * @buf: buffer to be populated with data from the Slave.
  112. *
  113. * Reads 1 byte of data from a Slave device register.
  114. */
  115. int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf)
  116. {
  117. /* 5-bit register address */
  118. if (addr > 0x1F)
  119. return -EINVAL;
  120. return spmi_read_cmd(sdev->ctrl, SPMI_CMD_READ, sdev->usid, addr,
  121. buf, 1);
  122. }
  123. EXPORT_SYMBOL_GPL(spmi_register_read);
  124. /**
  125. * spmi_ext_register_read() - extended register read
  126. * @sdev: SPMI device.
  127. * @addr: slave register address (8-bit address).
  128. * @buf: buffer to be populated with data from the Slave.
  129. * @len: the request number of bytes to read (up to 16 bytes).
  130. *
  131. * Reads up to 16 bytes of data from the extended register space on a
  132. * Slave device.
  133. */
  134. int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
  135. size_t len)
  136. {
  137. /* 8-bit register address, up to 16 bytes */
  138. if (len == 0 || len > 16)
  139. return -EINVAL;
  140. return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READ, sdev->usid, addr,
  141. buf, len);
  142. }
  143. EXPORT_SYMBOL_GPL(spmi_ext_register_read);
  144. /**
  145. * spmi_ext_register_readl() - extended register read long
  146. * @sdev: SPMI device.
  147. * @addr: slave register address (16-bit address).
  148. * @buf: buffer to be populated with data from the Slave.
  149. * @len: the request number of bytes to read (up to 8 bytes).
  150. *
  151. * Reads up to 8 bytes of data from the extended register space on a
  152. * Slave device using 16-bit address.
  153. */
  154. int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
  155. size_t len)
  156. {
  157. /* 16-bit register address, up to 8 bytes */
  158. if (len == 0 || len > 8)
  159. return -EINVAL;
  160. return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READL, sdev->usid, addr,
  161. buf, len);
  162. }
  163. EXPORT_SYMBOL_GPL(spmi_ext_register_readl);
  164. /**
  165. * spmi_register_write() - register write
  166. * @sdev: SPMI device
  167. * @addr: slave register address (5-bit address).
  168. * @data: buffer containing the data to be transferred to the Slave.
  169. *
  170. * Writes 1 byte of data to a Slave device register.
  171. */
  172. int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data)
  173. {
  174. /* 5-bit register address */
  175. if (addr > 0x1F)
  176. return -EINVAL;
  177. return spmi_write_cmd(sdev->ctrl, SPMI_CMD_WRITE, sdev->usid, addr,
  178. &data, 1);
  179. }
  180. EXPORT_SYMBOL_GPL(spmi_register_write);
  181. /**
  182. * spmi_register_zero_write() - register zero write
  183. * @sdev: SPMI device.
  184. * @data: the data to be written to register 0 (7-bits).
  185. *
  186. * Writes data to register 0 of the Slave device.
  187. */
  188. int spmi_register_zero_write(struct spmi_device *sdev, u8 data)
  189. {
  190. return spmi_write_cmd(sdev->ctrl, SPMI_CMD_ZERO_WRITE, sdev->usid, 0,
  191. &data, 1);
  192. }
  193. EXPORT_SYMBOL_GPL(spmi_register_zero_write);
  194. /**
  195. * spmi_ext_register_write() - extended register write
  196. * @sdev: SPMI device.
  197. * @addr: slave register address (8-bit address).
  198. * @buf: buffer containing the data to be transferred to the Slave.
  199. * @len: the request number of bytes to read (up to 16 bytes).
  200. *
  201. * Writes up to 16 bytes of data to the extended register space of a
  202. * Slave device.
  203. */
  204. int spmi_ext_register_write(struct spmi_device *sdev, u8 addr, const u8 *buf,
  205. size_t len)
  206. {
  207. /* 8-bit register address, up to 16 bytes */
  208. if (len == 0 || len > 16)
  209. return -EINVAL;
  210. return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITE, sdev->usid, addr,
  211. buf, len);
  212. }
  213. EXPORT_SYMBOL_GPL(spmi_ext_register_write);
  214. /**
  215. * spmi_ext_register_writel() - extended register write long
  216. * @sdev: SPMI device.
  217. * @addr: slave register address (16-bit address).
  218. * @buf: buffer containing the data to be transferred to the Slave.
  219. * @len: the request number of bytes to read (up to 8 bytes).
  220. *
  221. * Writes up to 8 bytes of data to the extended register space of a
  222. * Slave device using 16-bit address.
  223. */
  224. int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr, const u8 *buf,
  225. size_t len)
  226. {
  227. /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
  228. if (len == 0 || len > 8)
  229. return -EINVAL;
  230. return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITEL, sdev->usid,
  231. addr, buf, len);
  232. }
  233. EXPORT_SYMBOL_GPL(spmi_ext_register_writel);
  234. /**
  235. * spmi_command_reset() - sends RESET command to the specified slave
  236. * @sdev: SPMI device.
  237. *
  238. * The Reset command initializes the Slave and forces all registers to
  239. * their reset values. The Slave shall enter the STARTUP state after
  240. * receiving a Reset command.
  241. */
  242. int spmi_command_reset(struct spmi_device *sdev)
  243. {
  244. return spmi_cmd(sdev->ctrl, SPMI_CMD_RESET, sdev->usid);
  245. }
  246. EXPORT_SYMBOL_GPL(spmi_command_reset);
  247. /**
  248. * spmi_command_sleep() - sends SLEEP command to the specified SPMI device
  249. * @sdev: SPMI device.
  250. *
  251. * The Sleep command causes the Slave to enter the user defined SLEEP state.
  252. */
  253. int spmi_command_sleep(struct spmi_device *sdev)
  254. {
  255. return spmi_cmd(sdev->ctrl, SPMI_CMD_SLEEP, sdev->usid);
  256. }
  257. EXPORT_SYMBOL_GPL(spmi_command_sleep);
  258. /**
  259. * spmi_command_wakeup() - sends WAKEUP command to the specified SPMI device
  260. * @sdev: SPMI device.
  261. *
  262. * The Wakeup command causes the Slave to move from the SLEEP state to
  263. * the ACTIVE state.
  264. */
  265. int spmi_command_wakeup(struct spmi_device *sdev)
  266. {
  267. return spmi_cmd(sdev->ctrl, SPMI_CMD_WAKEUP, sdev->usid);
  268. }
  269. EXPORT_SYMBOL_GPL(spmi_command_wakeup);
  270. /**
  271. * spmi_command_shutdown() - sends SHUTDOWN command to the specified SPMI device
  272. * @sdev: SPMI device.
  273. *
  274. * The Shutdown command causes the Slave to enter the SHUTDOWN state.
  275. */
  276. int spmi_command_shutdown(struct spmi_device *sdev)
  277. {
  278. return spmi_cmd(sdev->ctrl, SPMI_CMD_SHUTDOWN, sdev->usid);
  279. }
  280. EXPORT_SYMBOL_GPL(spmi_command_shutdown);
  281. static int spmi_drv_probe(struct device *dev)
  282. {
  283. const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
  284. struct spmi_device *sdev = to_spmi_device(dev);
  285. int err;
  286. pm_runtime_get_noresume(dev);
  287. pm_runtime_set_active(dev);
  288. pm_runtime_enable(dev);
  289. err = sdrv->probe(sdev);
  290. if (err)
  291. goto fail_probe;
  292. return 0;
  293. fail_probe:
  294. pm_runtime_disable(dev);
  295. pm_runtime_set_suspended(dev);
  296. pm_runtime_put_noidle(dev);
  297. return err;
  298. }
  299. static void spmi_drv_remove(struct device *dev)
  300. {
  301. const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
  302. pm_runtime_get_sync(dev);
  303. if (sdrv->remove)
  304. sdrv->remove(to_spmi_device(dev));
  305. pm_runtime_put_noidle(dev);
  306. pm_runtime_disable(dev);
  307. pm_runtime_set_suspended(dev);
  308. pm_runtime_put_noidle(dev);
  309. }
  310. static void spmi_drv_shutdown(struct device *dev)
  311. {
  312. const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
  313. if (sdrv && sdrv->shutdown)
  314. sdrv->shutdown(to_spmi_device(dev));
  315. }
  316. static int spmi_drv_uevent(struct device *dev, struct kobj_uevent_env *env)
  317. {
  318. int ret;
  319. ret = of_device_uevent_modalias(dev, env);
  320. if (ret != -ENODEV)
  321. return ret;
  322. return 0;
  323. }
  324. static struct bus_type spmi_bus_type = {
  325. .name = "spmi",
  326. .match = spmi_device_match,
  327. .probe = spmi_drv_probe,
  328. .remove = spmi_drv_remove,
  329. .shutdown = spmi_drv_shutdown,
  330. .uevent = spmi_drv_uevent,
  331. };
  332. /**
  333. * spmi_device_from_of() - get the associated SPMI device from a device node
  334. *
  335. * @np: device node
  336. *
  337. * Returns the struct spmi_device associated with a device node or NULL.
  338. */
  339. struct spmi_device *spmi_device_from_of(struct device_node *np)
  340. {
  341. struct device *dev = bus_find_device_by_of_node(&spmi_bus_type, np);
  342. if (dev)
  343. return to_spmi_device(dev);
  344. return NULL;
  345. }
  346. EXPORT_SYMBOL_GPL(spmi_device_from_of);
  347. /**
  348. * spmi_controller_alloc() - Allocate a new SPMI device
  349. * @ctrl: associated controller
  350. *
  351. * Caller is responsible for either calling spmi_device_add() to add the
  352. * newly allocated controller, or calling spmi_device_put() to discard it.
  353. */
  354. struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl)
  355. {
  356. struct spmi_device *sdev;
  357. sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  358. if (!sdev)
  359. return NULL;
  360. sdev->ctrl = ctrl;
  361. device_initialize(&sdev->dev);
  362. sdev->dev.parent = &ctrl->dev;
  363. sdev->dev.bus = &spmi_bus_type;
  364. sdev->dev.type = &spmi_dev_type;
  365. return sdev;
  366. }
  367. EXPORT_SYMBOL_GPL(spmi_device_alloc);
  368. /**
  369. * spmi_controller_alloc() - Allocate a new SPMI controller
  370. * @parent: parent device
  371. * @size: size of private data
  372. *
  373. * Caller is responsible for either calling spmi_controller_add() to add the
  374. * newly allocated controller, or calling spmi_controller_put() to discard it.
  375. * The allocated private data region may be accessed via
  376. * spmi_controller_get_drvdata()
  377. */
  378. struct spmi_controller *spmi_controller_alloc(struct device *parent,
  379. size_t size)
  380. {
  381. struct spmi_controller *ctrl;
  382. int id;
  383. if (WARN_ON(!parent))
  384. return NULL;
  385. ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
  386. if (!ctrl)
  387. return NULL;
  388. device_initialize(&ctrl->dev);
  389. ctrl->dev.type = &spmi_ctrl_type;
  390. ctrl->dev.bus = &spmi_bus_type;
  391. ctrl->dev.parent = parent;
  392. ctrl->dev.of_node = parent->of_node;
  393. spmi_controller_set_drvdata(ctrl, &ctrl[1]);
  394. id = ida_alloc(&ctrl_ida, GFP_KERNEL);
  395. if (id < 0) {
  396. dev_err(parent,
  397. "unable to allocate SPMI controller identifier.\n");
  398. spmi_controller_put(ctrl);
  399. return NULL;
  400. }
  401. ctrl->nr = id;
  402. dev_set_name(&ctrl->dev, "spmi-%d", id);
  403. dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
  404. return ctrl;
  405. }
  406. EXPORT_SYMBOL_GPL(spmi_controller_alloc);
  407. static void of_spmi_register_devices(struct spmi_controller *ctrl)
  408. {
  409. struct device_node *node;
  410. int err;
  411. if (!ctrl->dev.of_node)
  412. return;
  413. for_each_available_child_of_node(ctrl->dev.of_node, node) {
  414. struct spmi_device *sdev;
  415. u32 reg[2];
  416. dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
  417. err = of_property_read_u32_array(node, "reg", reg, 2);
  418. if (err) {
  419. dev_err(&ctrl->dev,
  420. "node %pOF err (%d) does not have 'reg' property\n",
  421. node, err);
  422. continue;
  423. }
  424. if (reg[1] != SPMI_USID) {
  425. dev_err(&ctrl->dev,
  426. "node %pOF contains unsupported 'reg' entry\n",
  427. node);
  428. continue;
  429. }
  430. if (reg[0] >= SPMI_MAX_SLAVE_ID) {
  431. dev_err(&ctrl->dev, "invalid usid on node %pOF\n", node);
  432. continue;
  433. }
  434. dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
  435. sdev = spmi_device_alloc(ctrl);
  436. if (!sdev)
  437. continue;
  438. sdev->dev.of_node = node;
  439. sdev->usid = (u8)reg[0];
  440. err = spmi_device_add(sdev);
  441. if (err) {
  442. dev_err(&sdev->dev,
  443. "failure adding device. status %d\n", err);
  444. spmi_device_put(sdev);
  445. }
  446. }
  447. }
  448. /**
  449. * spmi_controller_add() - Add an SPMI controller
  450. * @ctrl: controller to be registered.
  451. *
  452. * Register a controller previously allocated via spmi_controller_alloc() with
  453. * the SPMI core.
  454. */
  455. int spmi_controller_add(struct spmi_controller *ctrl)
  456. {
  457. int ret;
  458. /* Can't register until after driver model init */
  459. if (WARN_ON(!is_registered))
  460. return -EAGAIN;
  461. ret = device_add(&ctrl->dev);
  462. if (ret)
  463. return ret;
  464. if (IS_ENABLED(CONFIG_OF))
  465. of_spmi_register_devices(ctrl);
  466. dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n",
  467. ctrl->nr, &ctrl->dev);
  468. return 0;
  469. };
  470. EXPORT_SYMBOL_GPL(spmi_controller_add);
  471. /* Remove a device associated with a controller */
  472. static int spmi_ctrl_remove_device(struct device *dev, void *data)
  473. {
  474. struct spmi_device *spmidev = to_spmi_device(dev);
  475. if (dev->type == &spmi_dev_type)
  476. spmi_device_remove(spmidev);
  477. return 0;
  478. }
  479. /**
  480. * spmi_controller_remove(): remove an SPMI controller
  481. * @ctrl: controller to remove
  482. *
  483. * Remove a SPMI controller. Caller is responsible for calling
  484. * spmi_controller_put() to discard the allocated controller.
  485. */
  486. void spmi_controller_remove(struct spmi_controller *ctrl)
  487. {
  488. if (!ctrl)
  489. return;
  490. device_for_each_child(&ctrl->dev, NULL, spmi_ctrl_remove_device);
  491. device_del(&ctrl->dev);
  492. }
  493. EXPORT_SYMBOL_GPL(spmi_controller_remove);
  494. /**
  495. * spmi_driver_register() - Register client driver with SPMI core
  496. * @sdrv: client driver to be associated with client-device.
  497. *
  498. * This API will register the client driver with the SPMI framework.
  499. * It is typically called from the driver's module-init function.
  500. */
  501. int __spmi_driver_register(struct spmi_driver *sdrv, struct module *owner)
  502. {
  503. sdrv->driver.bus = &spmi_bus_type;
  504. sdrv->driver.owner = owner;
  505. return driver_register(&sdrv->driver);
  506. }
  507. EXPORT_SYMBOL_GPL(__spmi_driver_register);
  508. static void __exit spmi_exit(void)
  509. {
  510. bus_unregister(&spmi_bus_type);
  511. }
  512. module_exit(spmi_exit);
  513. static int __init spmi_init(void)
  514. {
  515. int ret;
  516. ret = bus_register(&spmi_bus_type);
  517. if (ret)
  518. return ret;
  519. is_registered = true;
  520. return 0;
  521. }
  522. postcore_initcall(spmi_init);
  523. MODULE_LICENSE("GPL v2");
  524. MODULE_DESCRIPTION("SPMI module");
  525. MODULE_ALIAS("platform:spmi");