0055-spmi-Linux-driver-framework-for-SPMI.patch 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. From 1b0018dfd6295cbcc87738601b84bf49f3004419 Mon Sep 17 00:00:00 2001
  2. From: Kenneth Heitke <[email protected]>
  3. Date: Wed, 12 Feb 2014 13:44:22 -0600
  4. Subject: [PATCH 055/182] spmi: Linux driver framework for SPMI
  5. System Power Management Interface (SPMI) is a specification
  6. developed by the MIPI (Mobile Industry Process Interface) Alliance
  7. optimized for the real time control of Power Management ICs (PMIC).
  8. SPMI is a two-wire serial interface that supports up to 4 master
  9. devices and up to 16 logical slaves.
  10. The framework supports message APIs, multiple busses (1 controller
  11. per bus) and multiple clients/slave devices per controller.
  12. Signed-off-by: Kenneth Heitke <[email protected]>
  13. Signed-off-by: Michael Bohan <[email protected]>
  14. Signed-off-by: Josh Cartwright <[email protected]>
  15. Signed-off-by: Greg Kroah-Hartman <[email protected]>
  16. ---
  17. drivers/Kconfig | 2 +
  18. drivers/Makefile | 1 +
  19. drivers/spmi/Kconfig | 9 +
  20. drivers/spmi/Makefile | 4 +
  21. drivers/spmi/spmi.c | 609 +++++++++++++++++++++++++++++++++++++++
  22. include/dt-bindings/spmi/spmi.h | 18 ++
  23. include/linux/mod_devicetable.h | 8 +
  24. include/linux/spmi.h | 191 ++++++++++++
  25. 8 files changed, 842 insertions(+)
  26. create mode 100644 drivers/spmi/Kconfig
  27. create mode 100644 drivers/spmi/Makefile
  28. create mode 100644 drivers/spmi/spmi.c
  29. create mode 100644 include/dt-bindings/spmi/spmi.h
  30. create mode 100644 include/linux/spmi.h
  31. --- a/drivers/Kconfig
  32. +++ b/drivers/Kconfig
  33. @@ -52,6 +52,8 @@ source "drivers/i2c/Kconfig"
  34. source "drivers/spi/Kconfig"
  35. +source "drivers/spmi/Kconfig"
  36. +
  37. source "drivers/hsi/Kconfig"
  38. source "drivers/pps/Kconfig"
  39. --- a/drivers/Makefile
  40. +++ b/drivers/Makefile
  41. @@ -66,6 +66,7 @@ obj-$(CONFIG_ATA) += ata/
  42. obj-$(CONFIG_TARGET_CORE) += target/
  43. obj-$(CONFIG_MTD) += mtd/
  44. obj-$(CONFIG_SPI) += spi/
  45. +obj-$(CONFIG_SPMI) += spmi/
  46. obj-y += hsi/
  47. obj-y += net/
  48. obj-$(CONFIG_ATM) += atm/
  49. --- /dev/null
  50. +++ b/drivers/spmi/Kconfig
  51. @@ -0,0 +1,9 @@
  52. +#
  53. +# SPMI driver configuration
  54. +#
  55. +menuconfig SPMI
  56. + tristate "SPMI support"
  57. + help
  58. + SPMI (System Power Management Interface) is a two-wire
  59. + serial interface between baseband and application processors
  60. + and Power Management Integrated Circuits (PMIC).
  61. --- /dev/null
  62. +++ b/drivers/spmi/Makefile
  63. @@ -0,0 +1,4 @@
  64. +#
  65. +# Makefile for kernel SPMI framework.
  66. +#
  67. +obj-$(CONFIG_SPMI) += spmi.o
  68. --- /dev/null
  69. +++ b/drivers/spmi/spmi.c
  70. @@ -0,0 +1,609 @@
  71. +/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  72. + *
  73. + * This program is free software; you can redistribute it and/or modify
  74. + * it under the terms of the GNU General Public License version 2 and
  75. + * only version 2 as published by the Free Software Foundation.
  76. + *
  77. + * This program is distributed in the hope that it will be useful,
  78. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  79. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  80. + * GNU General Public License for more details.
  81. + */
  82. +#include <linux/kernel.h>
  83. +#include <linux/errno.h>
  84. +#include <linux/idr.h>
  85. +#include <linux/slab.h>
  86. +#include <linux/module.h>
  87. +#include <linux/of.h>
  88. +#include <linux/of_device.h>
  89. +#include <linux/platform_device.h>
  90. +#include <linux/spmi.h>
  91. +#include <linux/module.h>
  92. +#include <linux/pm_runtime.h>
  93. +
  94. +#include <dt-bindings/spmi/spmi.h>
  95. +
  96. +static DEFINE_IDA(ctrl_ida);
  97. +
  98. +static void spmi_dev_release(struct device *dev)
  99. +{
  100. + struct spmi_device *sdev = to_spmi_device(dev);
  101. + kfree(sdev);
  102. +}
  103. +
  104. +static const struct device_type spmi_dev_type = {
  105. + .release = spmi_dev_release,
  106. +};
  107. +
  108. +static void spmi_ctrl_release(struct device *dev)
  109. +{
  110. + struct spmi_controller *ctrl = to_spmi_controller(dev);
  111. + ida_simple_remove(&ctrl_ida, ctrl->nr);
  112. + kfree(ctrl);
  113. +}
  114. +
  115. +static const struct device_type spmi_ctrl_type = {
  116. + .release = spmi_ctrl_release,
  117. +};
  118. +
  119. +#ifdef CONFIG_PM_RUNTIME
  120. +static int spmi_runtime_suspend(struct device *dev)
  121. +{
  122. + struct spmi_device *sdev = to_spmi_device(dev);
  123. + int err;
  124. +
  125. + err = pm_generic_runtime_suspend(dev);
  126. + if (err)
  127. + return err;
  128. +
  129. + return spmi_command_sleep(sdev);
  130. +}
  131. +
  132. +static int spmi_runtime_resume(struct device *dev)
  133. +{
  134. + struct spmi_device *sdev = to_spmi_device(dev);
  135. + int err;
  136. +
  137. + err = spmi_command_wakeup(sdev);
  138. + if (err)
  139. + return err;
  140. +
  141. + return pm_generic_runtime_resume(dev);
  142. +}
  143. +#endif
  144. +
  145. +static const struct dev_pm_ops spmi_pm_ops = {
  146. + SET_RUNTIME_PM_OPS(
  147. + spmi_runtime_suspend,
  148. + spmi_runtime_resume,
  149. + NULL
  150. + )
  151. +};
  152. +
  153. +static int spmi_device_match(struct device *dev, struct device_driver *drv)
  154. +{
  155. + if (of_driver_match_device(dev, drv))
  156. + return 1;
  157. +
  158. + if (drv->name)
  159. + return strncmp(dev_name(dev), drv->name,
  160. + SPMI_NAME_SIZE) == 0;
  161. +
  162. + return 0;
  163. +}
  164. +
  165. +/**
  166. + * spmi_device_add() - add a device previously constructed via spmi_device_alloc()
  167. + * @sdev: spmi_device to be added
  168. + */
  169. +int spmi_device_add(struct spmi_device *sdev)
  170. +{
  171. + struct spmi_controller *ctrl = sdev->ctrl;
  172. + int err;
  173. +
  174. + dev_set_name(&sdev->dev, "%d-%02x", ctrl->nr, sdev->usid);
  175. +
  176. + err = device_add(&sdev->dev);
  177. + if (err < 0) {
  178. + dev_err(&sdev->dev, "Can't add %s, status %d\n",
  179. + dev_name(&sdev->dev), err);
  180. + goto err_device_add;
  181. + }
  182. +
  183. + dev_dbg(&sdev->dev, "device %s registered\n", dev_name(&sdev->dev));
  184. +
  185. +err_device_add:
  186. + return err;
  187. +}
  188. +EXPORT_SYMBOL_GPL(spmi_device_add);
  189. +
  190. +/**
  191. + * spmi_device_remove(): remove an SPMI device
  192. + * @sdev: spmi_device to be removed
  193. + */
  194. +void spmi_device_remove(struct spmi_device *sdev)
  195. +{
  196. + device_unregister(&sdev->dev);
  197. +}
  198. +EXPORT_SYMBOL_GPL(spmi_device_remove);
  199. +
  200. +static inline int
  201. +spmi_cmd(struct spmi_controller *ctrl, u8 opcode, u8 sid)
  202. +{
  203. + if (!ctrl || !ctrl->cmd || ctrl->dev.type != &spmi_ctrl_type)
  204. + return -EINVAL;
  205. +
  206. + return ctrl->cmd(ctrl, opcode, sid);
  207. +}
  208. +
  209. +static inline int spmi_read_cmd(struct spmi_controller *ctrl, u8 opcode,
  210. + u8 sid, u16 addr, u8 *buf, size_t len)
  211. +{
  212. + if (!ctrl || !ctrl->read_cmd || ctrl->dev.type != &spmi_ctrl_type)
  213. + return -EINVAL;
  214. +
  215. + return ctrl->read_cmd(ctrl, opcode, sid, addr, buf, len);
  216. +}
  217. +
  218. +static inline int spmi_write_cmd(struct spmi_controller *ctrl, u8 opcode,
  219. + u8 sid, u16 addr, const u8 *buf, size_t len)
  220. +{
  221. + if (!ctrl || !ctrl->write_cmd || ctrl->dev.type != &spmi_ctrl_type)
  222. + return -EINVAL;
  223. +
  224. + return ctrl->write_cmd(ctrl, opcode, sid, addr, buf, len);
  225. +}
  226. +
  227. +/**
  228. + * spmi_register_read() - register read
  229. + * @sdev: SPMI device.
  230. + * @addr: slave register address (5-bit address).
  231. + * @buf: buffer to be populated with data from the Slave.
  232. + *
  233. + * Reads 1 byte of data from a Slave device register.
  234. + */
  235. +int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf)
  236. +{
  237. + /* 5-bit register address */
  238. + if (addr > 0x1F)
  239. + return -EINVAL;
  240. +
  241. + return spmi_read_cmd(sdev->ctrl, SPMI_CMD_READ, sdev->usid, addr,
  242. + buf, 1);
  243. +}
  244. +EXPORT_SYMBOL_GPL(spmi_register_read);
  245. +
  246. +/**
  247. + * spmi_ext_register_read() - extended register read
  248. + * @sdev: SPMI device.
  249. + * @addr: slave register address (8-bit address).
  250. + * @buf: buffer to be populated with data from the Slave.
  251. + * @len: the request number of bytes to read (up to 16 bytes).
  252. + *
  253. + * Reads up to 16 bytes of data from the extended register space on a
  254. + * Slave device.
  255. + */
  256. +int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
  257. + size_t len)
  258. +{
  259. + /* 8-bit register address, up to 16 bytes */
  260. + if (len == 0 || len > 16)
  261. + return -EINVAL;
  262. +
  263. + return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READ, sdev->usid, addr,
  264. + buf, len);
  265. +}
  266. +EXPORT_SYMBOL_GPL(spmi_ext_register_read);
  267. +
  268. +/**
  269. + * spmi_ext_register_readl() - extended register read long
  270. + * @sdev: SPMI device.
  271. + * @addr: slave register address (16-bit address).
  272. + * @buf: buffer to be populated with data from the Slave.
  273. + * @len: the request number of bytes to read (up to 8 bytes).
  274. + *
  275. + * Reads up to 8 bytes of data from the extended register space on a
  276. + * Slave device using 16-bit address.
  277. + */
  278. +int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
  279. + size_t len)
  280. +{
  281. + /* 16-bit register address, up to 8 bytes */
  282. + if (len == 0 || len > 8)
  283. + return -EINVAL;
  284. +
  285. + return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READL, sdev->usid, addr,
  286. + buf, len);
  287. +}
  288. +EXPORT_SYMBOL_GPL(spmi_ext_register_readl);
  289. +
  290. +/**
  291. + * spmi_register_write() - register write
  292. + * @sdev: SPMI device
  293. + * @addr: slave register address (5-bit address).
  294. + * @data: buffer containing the data to be transferred to the Slave.
  295. + *
  296. + * Writes 1 byte of data to a Slave device register.
  297. + */
  298. +int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data)
  299. +{
  300. + /* 5-bit register address */
  301. + if (addr > 0x1F)
  302. + return -EINVAL;
  303. +
  304. + return spmi_write_cmd(sdev->ctrl, SPMI_CMD_WRITE, sdev->usid, addr,
  305. + &data, 1);
  306. +}
  307. +EXPORT_SYMBOL_GPL(spmi_register_write);
  308. +
  309. +/**
  310. + * spmi_register_zero_write() - register zero write
  311. + * @sdev: SPMI device.
  312. + * @data: the data to be written to register 0 (7-bits).
  313. + *
  314. + * Writes data to register 0 of the Slave device.
  315. + */
  316. +int spmi_register_zero_write(struct spmi_device *sdev, u8 data)
  317. +{
  318. + return spmi_write_cmd(sdev->ctrl, SPMI_CMD_ZERO_WRITE, sdev->usid, 0,
  319. + &data, 1);
  320. +}
  321. +EXPORT_SYMBOL_GPL(spmi_register_zero_write);
  322. +
  323. +/**
  324. + * spmi_ext_register_write() - extended register write
  325. + * @sdev: SPMI device.
  326. + * @addr: slave register address (8-bit address).
  327. + * @buf: buffer containing the data to be transferred to the Slave.
  328. + * @len: the request number of bytes to read (up to 16 bytes).
  329. + *
  330. + * Writes up to 16 bytes of data to the extended register space of a
  331. + * Slave device.
  332. + */
  333. +int spmi_ext_register_write(struct spmi_device *sdev, u8 addr, const u8 *buf,
  334. + size_t len)
  335. +{
  336. + /* 8-bit register address, up to 16 bytes */
  337. + if (len == 0 || len > 16)
  338. + return -EINVAL;
  339. +
  340. + return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITE, sdev->usid, addr,
  341. + buf, len);
  342. +}
  343. +EXPORT_SYMBOL_GPL(spmi_ext_register_write);
  344. +
  345. +/**
  346. + * spmi_ext_register_writel() - extended register write long
  347. + * @sdev: SPMI device.
  348. + * @addr: slave register address (16-bit address).
  349. + * @buf: buffer containing the data to be transferred to the Slave.
  350. + * @len: the request number of bytes to read (up to 8 bytes).
  351. + *
  352. + * Writes up to 8 bytes of data to the extended register space of a
  353. + * Slave device using 16-bit address.
  354. + */
  355. +int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr, const u8 *buf,
  356. + size_t len)
  357. +{
  358. + /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
  359. + if (len == 0 || len > 8)
  360. + return -EINVAL;
  361. +
  362. + return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITEL, sdev->usid,
  363. + addr, buf, len);
  364. +}
  365. +EXPORT_SYMBOL_GPL(spmi_ext_register_writel);
  366. +
  367. +/**
  368. + * spmi_command_reset() - sends RESET command to the specified slave
  369. + * @sdev: SPMI device.
  370. + *
  371. + * The Reset command initializes the Slave and forces all registers to
  372. + * their reset values. The Slave shall enter the STARTUP state after
  373. + * receiving a Reset command.
  374. + */
  375. +int spmi_command_reset(struct spmi_device *sdev)
  376. +{
  377. + return spmi_cmd(sdev->ctrl, SPMI_CMD_RESET, sdev->usid);
  378. +}
  379. +EXPORT_SYMBOL_GPL(spmi_command_reset);
  380. +
  381. +/**
  382. + * spmi_command_sleep() - sends SLEEP command to the specified SPMI device
  383. + * @sdev: SPMI device.
  384. + *
  385. + * The Sleep command causes the Slave to enter the user defined SLEEP state.
  386. + */
  387. +int spmi_command_sleep(struct spmi_device *sdev)
  388. +{
  389. + return spmi_cmd(sdev->ctrl, SPMI_CMD_SLEEP, sdev->usid);
  390. +}
  391. +EXPORT_SYMBOL_GPL(spmi_command_sleep);
  392. +
  393. +/**
  394. + * spmi_command_wakeup() - sends WAKEUP command to the specified SPMI device
  395. + * @sdev: SPMI device.
  396. + *
  397. + * The Wakeup command causes the Slave to move from the SLEEP state to
  398. + * the ACTIVE state.
  399. + */
  400. +int spmi_command_wakeup(struct spmi_device *sdev)
  401. +{
  402. + return spmi_cmd(sdev->ctrl, SPMI_CMD_WAKEUP, sdev->usid);
  403. +}
  404. +EXPORT_SYMBOL_GPL(spmi_command_wakeup);
  405. +
  406. +/**
  407. + * spmi_command_shutdown() - sends SHUTDOWN command to the specified SPMI device
  408. + * @sdev: SPMI device.
  409. + *
  410. + * The Shutdown command causes the Slave to enter the SHUTDOWN state.
  411. + */
  412. +int spmi_command_shutdown(struct spmi_device *sdev)
  413. +{
  414. + return spmi_cmd(sdev->ctrl, SPMI_CMD_SHUTDOWN, sdev->usid);
  415. +}
  416. +EXPORT_SYMBOL_GPL(spmi_command_shutdown);
  417. +
  418. +static int spmi_drv_probe(struct device *dev)
  419. +{
  420. + const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
  421. + struct spmi_device *sdev = to_spmi_device(dev);
  422. + int err;
  423. +
  424. + /* Ensure the slave is in ACTIVE state */
  425. + err = spmi_command_wakeup(sdev);
  426. + if (err)
  427. + goto fail_wakeup;
  428. +
  429. + pm_runtime_get_noresume(dev);
  430. + pm_runtime_set_active(dev);
  431. + pm_runtime_enable(dev);
  432. +
  433. + err = sdrv->probe(sdev);
  434. + if (err)
  435. + goto fail_probe;
  436. +
  437. + return 0;
  438. +
  439. +fail_probe:
  440. + pm_runtime_disable(dev);
  441. + pm_runtime_set_suspended(dev);
  442. + pm_runtime_put_noidle(dev);
  443. +fail_wakeup:
  444. + return err;
  445. +}
  446. +
  447. +static int spmi_drv_remove(struct device *dev)
  448. +{
  449. + const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
  450. +
  451. + pm_runtime_get_sync(dev);
  452. + sdrv->remove(to_spmi_device(dev));
  453. + pm_runtime_put_noidle(dev);
  454. +
  455. + pm_runtime_disable(dev);
  456. + pm_runtime_set_suspended(dev);
  457. + pm_runtime_put_noidle(dev);
  458. + return 0;
  459. +}
  460. +
  461. +static struct bus_type spmi_bus_type = {
  462. + .name = "spmi",
  463. + .match = spmi_device_match,
  464. + .pm = &spmi_pm_ops,
  465. + .probe = spmi_drv_probe,
  466. + .remove = spmi_drv_remove,
  467. +};
  468. +
  469. +/**
  470. + * spmi_controller_alloc() - Allocate a new SPMI device
  471. + * @ctrl: associated controller
  472. + *
  473. + * Caller is responsible for either calling spmi_device_add() to add the
  474. + * newly allocated controller, or calling spmi_device_put() to discard it.
  475. + */
  476. +struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl)
  477. +{
  478. + struct spmi_device *sdev;
  479. +
  480. + sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  481. + if (!sdev)
  482. + return NULL;
  483. +
  484. + sdev->ctrl = ctrl;
  485. + device_initialize(&sdev->dev);
  486. + sdev->dev.parent = &ctrl->dev;
  487. + sdev->dev.bus = &spmi_bus_type;
  488. + sdev->dev.type = &spmi_dev_type;
  489. + return sdev;
  490. +}
  491. +EXPORT_SYMBOL_GPL(spmi_device_alloc);
  492. +
  493. +/**
  494. + * spmi_controller_alloc() - Allocate a new SPMI controller
  495. + * @parent: parent device
  496. + * @size: size of private data
  497. + *
  498. + * Caller is responsible for either calling spmi_controller_add() to add the
  499. + * newly allocated controller, or calling spmi_controller_put() to discard it.
  500. + * The allocated private data region may be accessed via
  501. + * spmi_controller_get_drvdata()
  502. + */
  503. +struct spmi_controller *spmi_controller_alloc(struct device *parent,
  504. + size_t size)
  505. +{
  506. + struct spmi_controller *ctrl;
  507. + int id;
  508. +
  509. + if (WARN_ON(!parent))
  510. + return NULL;
  511. +
  512. + ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
  513. + if (!ctrl)
  514. + return NULL;
  515. +
  516. + device_initialize(&ctrl->dev);
  517. + ctrl->dev.type = &spmi_ctrl_type;
  518. + ctrl->dev.bus = &spmi_bus_type;
  519. + ctrl->dev.parent = parent;
  520. + ctrl->dev.of_node = parent->of_node;
  521. + spmi_controller_set_drvdata(ctrl, &ctrl[1]);
  522. +
  523. + id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
  524. + if (id < 0) {
  525. + dev_err(parent,
  526. + "unable to allocate SPMI controller identifier.\n");
  527. + spmi_controller_put(ctrl);
  528. + return NULL;
  529. + }
  530. +
  531. + ctrl->nr = id;
  532. + dev_set_name(&ctrl->dev, "spmi-%d", id);
  533. +
  534. + dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
  535. + return ctrl;
  536. +}
  537. +EXPORT_SYMBOL_GPL(spmi_controller_alloc);
  538. +
  539. +static void of_spmi_register_devices(struct spmi_controller *ctrl)
  540. +{
  541. + struct device_node *node;
  542. + int err;
  543. +
  544. + if (!ctrl->dev.of_node)
  545. + return;
  546. +
  547. + for_each_available_child_of_node(ctrl->dev.of_node, node) {
  548. + struct spmi_device *sdev;
  549. + u32 reg[2];
  550. +
  551. + dev_dbg(&ctrl->dev, "adding child %s\n", node->full_name);
  552. +
  553. + err = of_property_read_u32_array(node, "reg", reg, 2);
  554. + if (err) {
  555. + dev_err(&ctrl->dev,
  556. + "node %s err (%d) does not have 'reg' property\n",
  557. + node->full_name, err);
  558. + continue;
  559. + }
  560. +
  561. + if (reg[1] != SPMI_USID) {
  562. + dev_err(&ctrl->dev,
  563. + "node %s contains unsupported 'reg' entry\n",
  564. + node->full_name);
  565. + continue;
  566. + }
  567. +
  568. + if (reg[0] >= SPMI_MAX_SLAVE_ID) {
  569. + dev_err(&ctrl->dev,
  570. + "invalid usid on node %s\n",
  571. + node->full_name);
  572. + continue;
  573. + }
  574. +
  575. + dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
  576. +
  577. + sdev = spmi_device_alloc(ctrl);
  578. + if (!sdev)
  579. + continue;
  580. +
  581. + sdev->dev.of_node = node;
  582. + sdev->usid = (u8) reg[0];
  583. +
  584. + err = spmi_device_add(sdev);
  585. + if (err) {
  586. + dev_err(&sdev->dev,
  587. + "failure adding device. status %d\n", err);
  588. + spmi_device_put(sdev);
  589. + }
  590. + }
  591. +}
  592. +
  593. +/**
  594. + * spmi_controller_add() - Add an SPMI controller
  595. + * @ctrl: controller to be registered.
  596. + *
  597. + * Register a controller previously allocated via spmi_controller_alloc() with
  598. + * the SPMI core.
  599. + */
  600. +int spmi_controller_add(struct spmi_controller *ctrl)
  601. +{
  602. + int ret;
  603. +
  604. + /* Can't register until after driver model init */
  605. + if (WARN_ON(!spmi_bus_type.p))
  606. + return -EAGAIN;
  607. +
  608. + ret = device_add(&ctrl->dev);
  609. + if (ret)
  610. + return ret;
  611. +
  612. + if (IS_ENABLED(CONFIG_OF))
  613. + of_spmi_register_devices(ctrl);
  614. +
  615. + dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n",
  616. + ctrl->nr, &ctrl->dev);
  617. +
  618. + return 0;
  619. +};
  620. +EXPORT_SYMBOL_GPL(spmi_controller_add);
  621. +
  622. +/* Remove a device associated with a controller */
  623. +static int spmi_ctrl_remove_device(struct device *dev, void *data)
  624. +{
  625. + struct spmi_device *spmidev = to_spmi_device(dev);
  626. + if (dev->type == &spmi_dev_type)
  627. + spmi_device_remove(spmidev);
  628. + return 0;
  629. +}
  630. +
  631. +/**
  632. + * spmi_controller_remove(): remove an SPMI controller
  633. + * @ctrl: controller to remove
  634. + *
  635. + * Remove a SPMI controller. Caller is responsible for calling
  636. + * spmi_controller_put() to discard the allocated controller.
  637. + */
  638. +void spmi_controller_remove(struct spmi_controller *ctrl)
  639. +{
  640. + int dummy;
  641. +
  642. + if (!ctrl)
  643. + return;
  644. +
  645. + dummy = device_for_each_child(&ctrl->dev, NULL,
  646. + spmi_ctrl_remove_device);
  647. + device_del(&ctrl->dev);
  648. +}
  649. +EXPORT_SYMBOL_GPL(spmi_controller_remove);
  650. +
  651. +/**
  652. + * spmi_driver_register() - Register client driver with SPMI core
  653. + * @sdrv: client driver to be associated with client-device.
  654. + *
  655. + * This API will register the client driver with the SPMI framework.
  656. + * It is typically called from the driver's module-init function.
  657. + */
  658. +int spmi_driver_register(struct spmi_driver *sdrv)
  659. +{
  660. + sdrv->driver.bus = &spmi_bus_type;
  661. + return driver_register(&sdrv->driver);
  662. +}
  663. +EXPORT_SYMBOL_GPL(spmi_driver_register);
  664. +
  665. +static void __exit spmi_exit(void)
  666. +{
  667. + bus_unregister(&spmi_bus_type);
  668. +}
  669. +module_exit(spmi_exit);
  670. +
  671. +static int __init spmi_init(void)
  672. +{
  673. + return bus_register(&spmi_bus_type);
  674. +}
  675. +postcore_initcall(spmi_init);
  676. +
  677. +MODULE_LICENSE("GPL v2");
  678. +MODULE_DESCRIPTION("SPMI module");
  679. +MODULE_ALIAS("platform:spmi");
  680. --- /dev/null
  681. +++ b/include/dt-bindings/spmi/spmi.h
  682. @@ -0,0 +1,18 @@
  683. +/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
  684. + *
  685. + * This program is free software; you can redistribute it and/or modify
  686. + * it under the terms of the GNU General Public License version 2 and
  687. + * only version 2 as published by the Free Software Foundation.
  688. + *
  689. + * This program is distributed in the hope that it will be useful,
  690. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  691. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  692. + * GNU General Public License for more details.
  693. + */
  694. +#ifndef __DT_BINDINGS_SPMI_H
  695. +#define __DT_BINDINGS_SPMI_H
  696. +
  697. +#define SPMI_USID 0
  698. +#define SPMI_GSID 1
  699. +
  700. +#endif
  701. --- a/include/linux/mod_devicetable.h
  702. +++ b/include/linux/mod_devicetable.h
  703. @@ -432,6 +432,14 @@ struct spi_device_id {
  704. kernel_ulong_t driver_data; /* Data private to the driver */
  705. };
  706. +#define SPMI_NAME_SIZE 32
  707. +#define SPMI_MODULE_PREFIX "spmi:"
  708. +
  709. +struct spmi_device_id {
  710. + char name[SPMI_NAME_SIZE];
  711. + kernel_ulong_t driver_data; /* Data private to the driver */
  712. +};
  713. +
  714. /* dmi */
  715. enum dmi_field {
  716. DMI_NONE,
  717. --- /dev/null
  718. +++ b/include/linux/spmi.h
  719. @@ -0,0 +1,191 @@
  720. +/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  721. + *
  722. + * This program is free software; you can redistribute it and/or modify
  723. + * it under the terms of the GNU General Public License version 2 and
  724. + * only version 2 as published by the Free Software Foundation.
  725. + *
  726. + * This program is distributed in the hope that it will be useful,
  727. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  728. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  729. + * GNU General Public License for more details.
  730. + */
  731. +#ifndef _LINUX_SPMI_H
  732. +#define _LINUX_SPMI_H
  733. +
  734. +#include <linux/types.h>
  735. +#include <linux/device.h>
  736. +#include <linux/mod_devicetable.h>
  737. +
  738. +/* Maximum slave identifier */
  739. +#define SPMI_MAX_SLAVE_ID 16
  740. +
  741. +/* SPMI Commands */
  742. +#define SPMI_CMD_EXT_WRITE 0x00
  743. +#define SPMI_CMD_RESET 0x10
  744. +#define SPMI_CMD_SLEEP 0x11
  745. +#define SPMI_CMD_SHUTDOWN 0x12
  746. +#define SPMI_CMD_WAKEUP 0x13
  747. +#define SPMI_CMD_AUTHENTICATE 0x14
  748. +#define SPMI_CMD_MSTR_READ 0x15
  749. +#define SPMI_CMD_MSTR_WRITE 0x16
  750. +#define SPMI_CMD_TRANSFER_BUS_OWNERSHIP 0x1A
  751. +#define SPMI_CMD_DDB_MASTER_READ 0x1B
  752. +#define SPMI_CMD_DDB_SLAVE_READ 0x1C
  753. +#define SPMI_CMD_EXT_READ 0x20
  754. +#define SPMI_CMD_EXT_WRITEL 0x30
  755. +#define SPMI_CMD_EXT_READL 0x38
  756. +#define SPMI_CMD_WRITE 0x40
  757. +#define SPMI_CMD_READ 0x60
  758. +#define SPMI_CMD_ZERO_WRITE 0x80
  759. +
  760. +/**
  761. + * struct spmi_device - Basic representation of an SPMI device
  762. + * @dev: Driver model representation of the device.
  763. + * @ctrl: SPMI controller managing the bus hosting this device.
  764. + * @usid: This devices' Unique Slave IDentifier.
  765. + */
  766. +struct spmi_device {
  767. + struct device dev;
  768. + struct spmi_controller *ctrl;
  769. + u8 usid;
  770. +};
  771. +
  772. +static inline struct spmi_device *to_spmi_device(struct device *d)
  773. +{
  774. + return container_of(d, struct spmi_device, dev);
  775. +}
  776. +
  777. +static inline void *spmi_device_get_drvdata(const struct spmi_device *sdev)
  778. +{
  779. + return dev_get_drvdata(&sdev->dev);
  780. +}
  781. +
  782. +static inline void spmi_device_set_drvdata(struct spmi_device *sdev, void *data)
  783. +{
  784. + dev_set_drvdata(&sdev->dev, data);
  785. +}
  786. +
  787. +struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl);
  788. +
  789. +static inline void spmi_device_put(struct spmi_device *sdev)
  790. +{
  791. + if (sdev)
  792. + put_device(&sdev->dev);
  793. +}
  794. +
  795. +int spmi_device_add(struct spmi_device *sdev);
  796. +
  797. +void spmi_device_remove(struct spmi_device *sdev);
  798. +
  799. +/**
  800. + * struct spmi_controller - interface to the SPMI master controller
  801. + * @dev: Driver model representation of the device.
  802. + * @nr: board-specific number identifier for this controller/bus
  803. + * @cmd: sends a non-data command sequence on the SPMI bus.
  804. + * @read_cmd: sends a register read command sequence on the SPMI bus.
  805. + * @write_cmd: sends a register write command sequence on the SPMI bus.
  806. + */
  807. +struct spmi_controller {
  808. + struct device dev;
  809. + unsigned int nr;
  810. + int (*cmd)(struct spmi_controller *ctrl, u8 opcode, u8 sid);
  811. + int (*read_cmd)(struct spmi_controller *ctrl, u8 opcode,
  812. + u8 sid, u16 addr, u8 *buf, size_t len);
  813. + int (*write_cmd)(struct spmi_controller *ctrl, u8 opcode,
  814. + u8 sid, u16 addr, const u8 *buf, size_t len);
  815. +};
  816. +
  817. +static inline struct spmi_controller *to_spmi_controller(struct device *d)
  818. +{
  819. + return container_of(d, struct spmi_controller, dev);
  820. +}
  821. +
  822. +static inline
  823. +void *spmi_controller_get_drvdata(const struct spmi_controller *ctrl)
  824. +{
  825. + return dev_get_drvdata(&ctrl->dev);
  826. +}
  827. +
  828. +static inline void spmi_controller_set_drvdata(struct spmi_controller *ctrl,
  829. + void *data)
  830. +{
  831. + dev_set_drvdata(&ctrl->dev, data);
  832. +}
  833. +
  834. +struct spmi_controller *spmi_controller_alloc(struct device *parent,
  835. + size_t size);
  836. +
  837. +/**
  838. + * spmi_controller_put() - decrement controller refcount
  839. + * @ctrl SPMI controller.
  840. + */
  841. +static inline void spmi_controller_put(struct spmi_controller *ctrl)
  842. +{
  843. + if (ctrl)
  844. + put_device(&ctrl->dev);
  845. +}
  846. +
  847. +int spmi_controller_add(struct spmi_controller *ctrl);
  848. +void spmi_controller_remove(struct spmi_controller *ctrl);
  849. +
  850. +/**
  851. + * struct spmi_driver - SPMI slave device driver
  852. + * @driver: SPMI device drivers should initialize name and owner field of
  853. + * this structure.
  854. + * @probe: binds this driver to a SPMI device.
  855. + * @remove: unbinds this driver from the SPMI device.
  856. + * @shutdown: standard shutdown callback used during powerdown/halt.
  857. + * @suspend: standard suspend callback used during system suspend.
  858. + * @resume: standard resume callback used during system resume.
  859. + *
  860. + * If PM runtime support is desired for a slave, a device driver can call
  861. + * pm_runtime_put() from their probe() routine (and a balancing
  862. + * pm_runtime_get() in remove()). PM runtime support for a slave is
  863. + * implemented by issuing a SLEEP command to the slave on runtime_suspend(),
  864. + * transitioning the slave into the SLEEP state. On runtime_resume(), a WAKEUP
  865. + * command is sent to the slave to bring it back to ACTIVE.
  866. + */
  867. +struct spmi_driver {
  868. + struct device_driver driver;
  869. + int (*probe)(struct spmi_device *sdev);
  870. + void (*remove)(struct spmi_device *sdev);
  871. +};
  872. +
  873. +static inline struct spmi_driver *to_spmi_driver(struct device_driver *d)
  874. +{
  875. + return container_of(d, struct spmi_driver, driver);
  876. +}
  877. +
  878. +int spmi_driver_register(struct spmi_driver *sdrv);
  879. +
  880. +/**
  881. + * spmi_driver_unregister() - unregister an SPMI client driver
  882. + * @sdrv: the driver to unregister
  883. + */
  884. +static inline void spmi_driver_unregister(struct spmi_driver *sdrv)
  885. +{
  886. + if (sdrv)
  887. + driver_unregister(&sdrv->driver);
  888. +}
  889. +
  890. +#define module_spmi_driver(__spmi_driver) \
  891. + module_driver(__spmi_driver, spmi_driver_register, \
  892. + spmi_driver_unregister)
  893. +
  894. +int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf);
  895. +int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
  896. + size_t len);
  897. +int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
  898. + size_t len);
  899. +int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data);
  900. +int spmi_register_zero_write(struct spmi_device *sdev, u8 data);
  901. +int spmi_ext_register_write(struct spmi_device *sdev, u8 addr,
  902. + const u8 *buf, size_t len);
  903. +int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr,
  904. + const u8 *buf, size_t len);
  905. +int spmi_command_reset(struct spmi_device *sdev);
  906. +int spmi_command_sleep(struct spmi_device *sdev);
  907. +int spmi_command_wakeup(struct spmi_device *sdev);
  908. +int spmi_command_shutdown(struct spmi_device *sdev);
  909. +
  910. +#endif