spmi.c 15 KB

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