spmi.c 14 KB

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