spmi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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_simple_remove(&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 int 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. sdrv->remove(to_spmi_device(dev));
  304. pm_runtime_put_noidle(dev);
  305. pm_runtime_disable(dev);
  306. pm_runtime_set_suspended(dev);
  307. pm_runtime_put_noidle(dev);
  308. return 0;
  309. }
  310. static int spmi_drv_uevent(struct device *dev, struct kobj_uevent_env *env)
  311. {
  312. int ret;
  313. ret = of_device_uevent_modalias(dev, env);
  314. if (ret != -ENODEV)
  315. return ret;
  316. return 0;
  317. }
  318. static struct bus_type spmi_bus_type = {
  319. .name = "spmi",
  320. .match = spmi_device_match,
  321. .probe = spmi_drv_probe,
  322. .remove = spmi_drv_remove,
  323. .uevent = spmi_drv_uevent,
  324. };
  325. /**
  326. * spmi_controller_alloc() - Allocate a new SPMI device
  327. * @ctrl: associated controller
  328. *
  329. * Caller is responsible for either calling spmi_device_add() to add the
  330. * newly allocated controller, or calling spmi_device_put() to discard it.
  331. */
  332. struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl)
  333. {
  334. struct spmi_device *sdev;
  335. sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  336. if (!sdev)
  337. return NULL;
  338. sdev->ctrl = ctrl;
  339. device_initialize(&sdev->dev);
  340. sdev->dev.parent = &ctrl->dev;
  341. sdev->dev.bus = &spmi_bus_type;
  342. sdev->dev.type = &spmi_dev_type;
  343. return sdev;
  344. }
  345. EXPORT_SYMBOL_GPL(spmi_device_alloc);
  346. /**
  347. * spmi_controller_alloc() - Allocate a new SPMI controller
  348. * @parent: parent device
  349. * @size: size of private data
  350. *
  351. * Caller is responsible for either calling spmi_controller_add() to add the
  352. * newly allocated controller, or calling spmi_controller_put() to discard it.
  353. * The allocated private data region may be accessed via
  354. * spmi_controller_get_drvdata()
  355. */
  356. struct spmi_controller *spmi_controller_alloc(struct device *parent,
  357. size_t size)
  358. {
  359. struct spmi_controller *ctrl;
  360. int id;
  361. if (WARN_ON(!parent))
  362. return NULL;
  363. ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
  364. if (!ctrl)
  365. return NULL;
  366. device_initialize(&ctrl->dev);
  367. ctrl->dev.type = &spmi_ctrl_type;
  368. ctrl->dev.bus = &spmi_bus_type;
  369. ctrl->dev.parent = parent;
  370. ctrl->dev.of_node = parent->of_node;
  371. spmi_controller_set_drvdata(ctrl, &ctrl[1]);
  372. id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
  373. if (id < 0) {
  374. dev_err(parent,
  375. "unable to allocate SPMI controller identifier.\n");
  376. spmi_controller_put(ctrl);
  377. return NULL;
  378. }
  379. ctrl->nr = id;
  380. dev_set_name(&ctrl->dev, "spmi-%d", id);
  381. dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
  382. return ctrl;
  383. }
  384. EXPORT_SYMBOL_GPL(spmi_controller_alloc);
  385. static void of_spmi_register_devices(struct spmi_controller *ctrl)
  386. {
  387. struct device_node *node;
  388. int err;
  389. if (!ctrl->dev.of_node)
  390. return;
  391. for_each_available_child_of_node(ctrl->dev.of_node, node) {
  392. struct spmi_device *sdev;
  393. u32 reg[2];
  394. dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
  395. err = of_property_read_u32_array(node, "reg", reg, 2);
  396. if (err) {
  397. dev_err(&ctrl->dev,
  398. "node %pOF err (%d) does not have 'reg' property\n",
  399. node, err);
  400. continue;
  401. }
  402. if (reg[1] != SPMI_USID) {
  403. dev_err(&ctrl->dev,
  404. "node %pOF contains unsupported 'reg' entry\n",
  405. node);
  406. continue;
  407. }
  408. if (reg[0] >= SPMI_MAX_SLAVE_ID) {
  409. dev_err(&ctrl->dev, "invalid usid on node %pOF\n", node);
  410. continue;
  411. }
  412. dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
  413. sdev = spmi_device_alloc(ctrl);
  414. if (!sdev)
  415. continue;
  416. sdev->dev.of_node = node;
  417. sdev->usid = (u8) reg[0];
  418. err = spmi_device_add(sdev);
  419. if (err) {
  420. dev_err(&sdev->dev,
  421. "failure adding device. status %d\n", err);
  422. spmi_device_put(sdev);
  423. }
  424. }
  425. }
  426. /**
  427. * spmi_controller_add() - Add an SPMI controller
  428. * @ctrl: controller to be registered.
  429. *
  430. * Register a controller previously allocated via spmi_controller_alloc() with
  431. * the SPMI core.
  432. */
  433. int spmi_controller_add(struct spmi_controller *ctrl)
  434. {
  435. int ret;
  436. /* Can't register until after driver model init */
  437. if (WARN_ON(!is_registered))
  438. return -EAGAIN;
  439. ret = device_add(&ctrl->dev);
  440. if (ret)
  441. return ret;
  442. if (IS_ENABLED(CONFIG_OF))
  443. of_spmi_register_devices(ctrl);
  444. dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n",
  445. ctrl->nr, &ctrl->dev);
  446. return 0;
  447. };
  448. EXPORT_SYMBOL_GPL(spmi_controller_add);
  449. /* Remove a device associated with a controller */
  450. static int spmi_ctrl_remove_device(struct device *dev, void *data)
  451. {
  452. struct spmi_device *spmidev = to_spmi_device(dev);
  453. if (dev->type == &spmi_dev_type)
  454. spmi_device_remove(spmidev);
  455. return 0;
  456. }
  457. /**
  458. * spmi_controller_remove(): remove an SPMI controller
  459. * @ctrl: controller to remove
  460. *
  461. * Remove a SPMI controller. Caller is responsible for calling
  462. * spmi_controller_put() to discard the allocated controller.
  463. */
  464. void spmi_controller_remove(struct spmi_controller *ctrl)
  465. {
  466. int dummy;
  467. if (!ctrl)
  468. return;
  469. dummy = device_for_each_child(&ctrl->dev, NULL,
  470. spmi_ctrl_remove_device);
  471. device_del(&ctrl->dev);
  472. }
  473. EXPORT_SYMBOL_GPL(spmi_controller_remove);
  474. /**
  475. * spmi_driver_register() - Register client driver with SPMI core
  476. * @sdrv: client driver to be associated with client-device.
  477. *
  478. * This API will register the client driver with the SPMI framework.
  479. * It is typically called from the driver's module-init function.
  480. */
  481. int __spmi_driver_register(struct spmi_driver *sdrv, struct module *owner)
  482. {
  483. sdrv->driver.bus = &spmi_bus_type;
  484. sdrv->driver.owner = owner;
  485. return driver_register(&sdrv->driver);
  486. }
  487. EXPORT_SYMBOL_GPL(__spmi_driver_register);
  488. static void __exit spmi_exit(void)
  489. {
  490. bus_unregister(&spmi_bus_type);
  491. }
  492. module_exit(spmi_exit);
  493. static int __init spmi_init(void)
  494. {
  495. int ret;
  496. ret = bus_register(&spmi_bus_type);
  497. if (ret)
  498. return ret;
  499. is_registered = true;
  500. return 0;
  501. }
  502. postcore_initcall(spmi_init);
  503. MODULE_LICENSE("GPL v2");
  504. MODULE_DESCRIPTION("SPMI module");
  505. MODULE_ALIAS("platform:spmi");