spmi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/idr.h>
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/spmi.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. pm_runtime_get_noresume(dev);
  281. pm_runtime_set_active(dev);
  282. pm_runtime_enable(dev);
  283. err = sdrv->probe(sdev);
  284. if (err)
  285. goto fail_probe;
  286. return 0;
  287. fail_probe:
  288. pm_runtime_disable(dev);
  289. pm_runtime_set_suspended(dev);
  290. pm_runtime_put_noidle(dev);
  291. return err;
  292. }
  293. static int spmi_drv_remove(struct device *dev)
  294. {
  295. const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
  296. pm_runtime_get_sync(dev);
  297. sdrv->remove(to_spmi_device(dev));
  298. pm_runtime_put_noidle(dev);
  299. pm_runtime_disable(dev);
  300. pm_runtime_set_suspended(dev);
  301. pm_runtime_put_noidle(dev);
  302. return 0;
  303. }
  304. static struct bus_type spmi_bus_type = {
  305. .name = "spmi",
  306. .match = spmi_device_match,
  307. .probe = spmi_drv_probe,
  308. .remove = spmi_drv_remove,
  309. };
  310. /**
  311. * spmi_controller_alloc() - Allocate a new SPMI device
  312. * @ctrl: associated controller
  313. *
  314. * Caller is responsible for either calling spmi_device_add() to add the
  315. * newly allocated controller, or calling spmi_device_put() to discard it.
  316. */
  317. struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl)
  318. {
  319. struct spmi_device *sdev;
  320. sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  321. if (!sdev)
  322. return NULL;
  323. sdev->ctrl = ctrl;
  324. device_initialize(&sdev->dev);
  325. sdev->dev.parent = &ctrl->dev;
  326. sdev->dev.bus = &spmi_bus_type;
  327. sdev->dev.type = &spmi_dev_type;
  328. return sdev;
  329. }
  330. EXPORT_SYMBOL_GPL(spmi_device_alloc);
  331. /**
  332. * spmi_controller_alloc() - Allocate a new SPMI controller
  333. * @parent: parent device
  334. * @size: size of private data
  335. *
  336. * Caller is responsible for either calling spmi_controller_add() to add the
  337. * newly allocated controller, or calling spmi_controller_put() to discard it.
  338. * The allocated private data region may be accessed via
  339. * spmi_controller_get_drvdata()
  340. */
  341. struct spmi_controller *spmi_controller_alloc(struct device *parent,
  342. size_t size)
  343. {
  344. struct spmi_controller *ctrl;
  345. int id;
  346. if (WARN_ON(!parent))
  347. return NULL;
  348. ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
  349. if (!ctrl)
  350. return NULL;
  351. device_initialize(&ctrl->dev);
  352. ctrl->dev.type = &spmi_ctrl_type;
  353. ctrl->dev.bus = &spmi_bus_type;
  354. ctrl->dev.parent = parent;
  355. ctrl->dev.of_node = parent->of_node;
  356. spmi_controller_set_drvdata(ctrl, &ctrl[1]);
  357. id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
  358. if (id < 0) {
  359. dev_err(parent,
  360. "unable to allocate SPMI controller identifier.\n");
  361. spmi_controller_put(ctrl);
  362. return NULL;
  363. }
  364. ctrl->nr = id;
  365. dev_set_name(&ctrl->dev, "spmi-%d", id);
  366. dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
  367. return ctrl;
  368. }
  369. EXPORT_SYMBOL_GPL(spmi_controller_alloc);
  370. static void of_spmi_register_devices(struct spmi_controller *ctrl)
  371. {
  372. struct device_node *node;
  373. int err;
  374. if (!ctrl->dev.of_node)
  375. return;
  376. for_each_available_child_of_node(ctrl->dev.of_node, node) {
  377. struct spmi_device *sdev;
  378. u32 reg[2];
  379. dev_dbg(&ctrl->dev, "adding child %s\n", node->full_name);
  380. err = of_property_read_u32_array(node, "reg", reg, 2);
  381. if (err) {
  382. dev_err(&ctrl->dev,
  383. "node %s err (%d) does not have 'reg' property\n",
  384. node->full_name, err);
  385. continue;
  386. }
  387. if (reg[1] != SPMI_USID) {
  388. dev_err(&ctrl->dev,
  389. "node %s contains unsupported 'reg' entry\n",
  390. node->full_name);
  391. continue;
  392. }
  393. if (reg[0] >= SPMI_MAX_SLAVE_ID) {
  394. dev_err(&ctrl->dev,
  395. "invalid usid on node %s\n",
  396. node->full_name);
  397. continue;
  398. }
  399. dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
  400. sdev = spmi_device_alloc(ctrl);
  401. if (!sdev)
  402. continue;
  403. sdev->dev.of_node = node;
  404. sdev->usid = (u8) reg[0];
  405. err = spmi_device_add(sdev);
  406. if (err) {
  407. dev_err(&sdev->dev,
  408. "failure adding device. status %d\n", err);
  409. spmi_device_put(sdev);
  410. }
  411. }
  412. }
  413. /**
  414. * spmi_controller_add() - Add an SPMI controller
  415. * @ctrl: controller to be registered.
  416. *
  417. * Register a controller previously allocated via spmi_controller_alloc() with
  418. * the SPMI core.
  419. */
  420. int spmi_controller_add(struct spmi_controller *ctrl)
  421. {
  422. int ret;
  423. /* Can't register until after driver model init */
  424. if (WARN_ON(!spmi_bus_type.p))
  425. return -EAGAIN;
  426. ret = device_add(&ctrl->dev);
  427. if (ret)
  428. return ret;
  429. if (IS_ENABLED(CONFIG_OF))
  430. of_spmi_register_devices(ctrl);
  431. dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n",
  432. ctrl->nr, &ctrl->dev);
  433. return 0;
  434. };
  435. EXPORT_SYMBOL_GPL(spmi_controller_add);
  436. /* Remove a device associated with a controller */
  437. static int spmi_ctrl_remove_device(struct device *dev, void *data)
  438. {
  439. struct spmi_device *spmidev = to_spmi_device(dev);
  440. if (dev->type == &spmi_dev_type)
  441. spmi_device_remove(spmidev);
  442. return 0;
  443. }
  444. /**
  445. * spmi_controller_remove(): remove an SPMI controller
  446. * @ctrl: controller to remove
  447. *
  448. * Remove a SPMI controller. Caller is responsible for calling
  449. * spmi_controller_put() to discard the allocated controller.
  450. */
  451. void spmi_controller_remove(struct spmi_controller *ctrl)
  452. {
  453. int dummy;
  454. if (!ctrl)
  455. return;
  456. dummy = device_for_each_child(&ctrl->dev, NULL,
  457. spmi_ctrl_remove_device);
  458. device_del(&ctrl->dev);
  459. }
  460. EXPORT_SYMBOL_GPL(spmi_controller_remove);
  461. /**
  462. * spmi_driver_register() - Register client driver with SPMI core
  463. * @sdrv: client driver to be associated with client-device.
  464. *
  465. * This API will register the client driver with the SPMI framework.
  466. * It is typically called from the driver's module-init function.
  467. */
  468. int spmi_driver_register(struct spmi_driver *sdrv)
  469. {
  470. sdrv->driver.bus = &spmi_bus_type;
  471. return driver_register(&sdrv->driver);
  472. }
  473. EXPORT_SYMBOL_GPL(spmi_driver_register);
  474. static void __exit spmi_exit(void)
  475. {
  476. bus_unregister(&spmi_bus_type);
  477. }
  478. module_exit(spmi_exit);
  479. static int __init spmi_init(void)
  480. {
  481. return bus_register(&spmi_bus_type);
  482. }
  483. postcore_initcall(spmi_init);
  484. MODULE_LICENSE("GPL v2");
  485. MODULE_DESCRIPTION("SPMI module");
  486. MODULE_ALIAS("platform:spmi");