spmi.c 14 KB

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