spmi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 int spmi_drv_uevent(struct device *dev, struct kobj_uevent_env *env)
  319. {
  320. int ret;
  321. ret = of_device_uevent_modalias(dev, env);
  322. if (ret != -ENODEV)
  323. return ret;
  324. return 0;
  325. }
  326. static struct bus_type spmi_bus_type = {
  327. .name = "spmi",
  328. .match = spmi_device_match,
  329. .probe = spmi_drv_probe,
  330. .remove = spmi_drv_remove,
  331. .uevent = spmi_drv_uevent,
  332. };
  333. /**
  334. * spmi_controller_alloc() - Allocate a new SPMI device
  335. * @ctrl: associated controller
  336. *
  337. * Caller is responsible for either calling spmi_device_add() to add the
  338. * newly allocated controller, or calling spmi_device_put() to discard it.
  339. */
  340. struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl)
  341. {
  342. struct spmi_device *sdev;
  343. sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  344. if (!sdev)
  345. return NULL;
  346. sdev->ctrl = ctrl;
  347. device_initialize(&sdev->dev);
  348. sdev->dev.parent = &ctrl->dev;
  349. sdev->dev.bus = &spmi_bus_type;
  350. sdev->dev.type = &spmi_dev_type;
  351. return sdev;
  352. }
  353. EXPORT_SYMBOL_GPL(spmi_device_alloc);
  354. /**
  355. * spmi_controller_alloc() - Allocate a new SPMI controller
  356. * @parent: parent device
  357. * @size: size of private data
  358. *
  359. * Caller is responsible for either calling spmi_controller_add() to add the
  360. * newly allocated controller, or calling spmi_controller_put() to discard it.
  361. * The allocated private data region may be accessed via
  362. * spmi_controller_get_drvdata()
  363. */
  364. struct spmi_controller *spmi_controller_alloc(struct device *parent,
  365. size_t size)
  366. {
  367. struct spmi_controller *ctrl;
  368. int id;
  369. if (WARN_ON(!parent))
  370. return NULL;
  371. ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
  372. if (!ctrl)
  373. return NULL;
  374. device_initialize(&ctrl->dev);
  375. ctrl->dev.type = &spmi_ctrl_type;
  376. ctrl->dev.bus = &spmi_bus_type;
  377. ctrl->dev.parent = parent;
  378. ctrl->dev.of_node = parent->of_node;
  379. spmi_controller_set_drvdata(ctrl, &ctrl[1]);
  380. id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
  381. if (id < 0) {
  382. dev_err(parent,
  383. "unable to allocate SPMI controller identifier.\n");
  384. spmi_controller_put(ctrl);
  385. return NULL;
  386. }
  387. ctrl->nr = id;
  388. dev_set_name(&ctrl->dev, "spmi-%d", id);
  389. dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
  390. return ctrl;
  391. }
  392. EXPORT_SYMBOL_GPL(spmi_controller_alloc);
  393. static void of_spmi_register_devices(struct spmi_controller *ctrl)
  394. {
  395. struct device_node *node;
  396. int err;
  397. if (!ctrl->dev.of_node)
  398. return;
  399. for_each_available_child_of_node(ctrl->dev.of_node, node) {
  400. struct spmi_device *sdev;
  401. u32 reg[2];
  402. dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
  403. err = of_property_read_u32_array(node, "reg", reg, 2);
  404. if (err) {
  405. dev_err(&ctrl->dev,
  406. "node %pOF err (%d) does not have 'reg' property\n",
  407. node, err);
  408. continue;
  409. }
  410. if (reg[1] != SPMI_USID) {
  411. dev_err(&ctrl->dev,
  412. "node %pOF contains unsupported 'reg' entry\n",
  413. node);
  414. continue;
  415. }
  416. if (reg[0] >= SPMI_MAX_SLAVE_ID) {
  417. dev_info(&ctrl->dev, "invalid usid on %pOF\n", node);
  418. continue;
  419. }
  420. dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
  421. sdev = spmi_device_alloc(ctrl);
  422. if (!sdev)
  423. continue;
  424. sdev->dev.of_node = node;
  425. sdev->usid = (u8) reg[0];
  426. err = spmi_device_add(sdev);
  427. if (err) {
  428. dev_err(&sdev->dev,
  429. "failure adding device. status %d\n", err);
  430. spmi_device_put(sdev);
  431. }
  432. }
  433. }
  434. /**
  435. * spmi_controller_add() - Add an SPMI controller
  436. * @ctrl: controller to be registered.
  437. *
  438. * Register a controller previously allocated via spmi_controller_alloc() with
  439. * the SPMI core.
  440. */
  441. int spmi_controller_add(struct spmi_controller *ctrl)
  442. {
  443. int ret;
  444. /* Can't register until after driver model init */
  445. if (WARN_ON(!is_registered))
  446. return -EAGAIN;
  447. ret = device_add(&ctrl->dev);
  448. if (ret)
  449. return ret;
  450. if (IS_ENABLED(CONFIG_OF))
  451. of_spmi_register_devices(ctrl);
  452. dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n",
  453. ctrl->nr, &ctrl->dev);
  454. return 0;
  455. };
  456. EXPORT_SYMBOL_GPL(spmi_controller_add);
  457. /* Remove a device associated with a controller */
  458. static int spmi_ctrl_remove_device(struct device *dev, void *data)
  459. {
  460. struct spmi_device *spmidev = to_spmi_device(dev);
  461. if (dev->type == &spmi_dev_type)
  462. spmi_device_remove(spmidev);
  463. return 0;
  464. }
  465. /**
  466. * spmi_controller_remove(): remove an SPMI controller
  467. * @ctrl: controller to remove
  468. *
  469. * Remove a SPMI controller. Caller is responsible for calling
  470. * spmi_controller_put() to discard the allocated controller.
  471. */
  472. void spmi_controller_remove(struct spmi_controller *ctrl)
  473. {
  474. int dummy;
  475. if (!ctrl)
  476. return;
  477. dummy = device_for_each_child(&ctrl->dev, NULL,
  478. spmi_ctrl_remove_device);
  479. device_del(&ctrl->dev);
  480. }
  481. EXPORT_SYMBOL_GPL(spmi_controller_remove);
  482. /**
  483. * spmi_driver_register() - Register client driver with SPMI core
  484. * @sdrv: client driver to be associated with client-device.
  485. *
  486. * This API will register the client driver with the SPMI framework.
  487. * It is typically called from the driver's module-init function.
  488. */
  489. int __spmi_driver_register(struct spmi_driver *sdrv, struct module *owner)
  490. {
  491. sdrv->driver.bus = &spmi_bus_type;
  492. sdrv->driver.owner = owner;
  493. return driver_register(&sdrv->driver);
  494. }
  495. EXPORT_SYMBOL_GPL(__spmi_driver_register);
  496. static void __exit spmi_exit(void)
  497. {
  498. bus_unregister(&spmi_bus_type);
  499. }
  500. module_exit(spmi_exit);
  501. static int __init spmi_init(void)
  502. {
  503. int ret;
  504. ret = bus_register(&spmi_bus_type);
  505. if (ret)
  506. return ret;
  507. is_registered = true;
  508. return 0;
  509. }
  510. postcore_initcall(spmi_init);
  511. MODULE_LICENSE("GPL v2");
  512. MODULE_DESCRIPTION("SPMI module");
  513. MODULE_ALIAS("platform:spmi");