spmi.c 15 KB

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