spmi.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /* Copyright (c) 2012-2014, 2016 The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #ifndef _LINUX_SPMI_H
  13. #define _LINUX_SPMI_H
  14. #include <linux/types.h>
  15. #include <linux/device.h>
  16. #include <linux/mod_devicetable.h>
  17. /* Maximum slave identifier */
  18. #define SPMI_MAX_SLAVE_ID 16
  19. /* SPMI Commands */
  20. enum spmi_commands {
  21. SPMI_CMD_EXT_WRITE = 0x00,
  22. SPMI_CMD_RESET = 0x10,
  23. SPMI_CMD_SLEEP = 0x11,
  24. SPMI_CMD_SHUTDOWN = 0x12,
  25. SPMI_CMD_WAKEUP = 0x13,
  26. SPMI_CMD_AUTHENTICATE = 0x14,
  27. SPMI_CMD_MSTR_READ = 0x15,
  28. SPMI_CMD_MSTR_WRITE = 0x16,
  29. SPMI_CMD_TRANSFER_BUS_OWNERSHIP = 0x1A,
  30. SPMI_CMD_DDB_MASTER_READ = 0x1B,
  31. SPMI_CMD_DDB_SLAVE_READ = 0x1C,
  32. SPMI_CMD_EXT_READ = 0x20,
  33. SPMI_CMD_EXT_WRITEL = 0x30,
  34. SPMI_CMD_EXT_READL = 0x38,
  35. SPMI_CMD_WRITE = 0x40,
  36. SPMI_CMD_READ = 0x60,
  37. SPMI_CMD_ZERO_WRITE = 0x80,
  38. };
  39. struct spmi_device;
  40. /**
  41. * struct spmi_controller: interface to the SPMI master controller
  42. * @nr: board-specific number identifier for this controller/bus
  43. * @name: name for this controller
  44. * @cmd: sends a non-data command sequence on the SPMI bus.
  45. * @read_cmd: sends a register read command sequence on the SPMI bus.
  46. * @write_cmd: sends a register write command sequence on the SPMI bus.
  47. */
  48. struct spmi_controller {
  49. struct device dev;
  50. unsigned int nr;
  51. struct completion dev_released;
  52. int (*cmd)(struct spmi_controller *, u8 opcode, u8 sid);
  53. int (*read_cmd)(struct spmi_controller *,
  54. u8 opcode, u8 sid, u16 addr, u8 bc, u8 *buf);
  55. int (*write_cmd)(struct spmi_controller *,
  56. u8 opcode, u8 sid, u16 addr, u8 bc, u8 *buf);
  57. };
  58. #define to_spmi_controller(d) container_of(d, struct spmi_controller, dev)
  59. /**
  60. * struct spmi_driver: Manage SPMI generic/slave device driver
  61. * @probe: binds this driver to a SPMI device.
  62. * @remove: unbinds this driver from the SPMI device.
  63. * @shutdown: standard shutdown callback used during powerdown/halt.
  64. * @suspend: standard suspend callback used during system suspend
  65. * @resume: standard resume callback used during system resume
  66. * @driver: SPMI device drivers should initialize name and owner field of
  67. * this structure
  68. * @id_table: list of SPMI devices supported by this driver
  69. */
  70. struct spmi_driver {
  71. int (*probe)(struct spmi_device *dev);
  72. int (*remove)(struct spmi_device *dev);
  73. void (*shutdown)(struct spmi_device *dev);
  74. int (*suspend)(struct spmi_device *dev,
  75. pm_message_t pmesg);
  76. int (*resume)(struct spmi_device *dev);
  77. struct device_driver driver;
  78. const struct spmi_device_id *id_table;
  79. };
  80. #define to_spmi_driver(d) container_of(d, struct spmi_driver, driver)
  81. /**
  82. * struct spmi_resource: spmi_resource for one device_node
  83. * @num_resources: number of resources for this device node
  84. * @resources: array of resources for this device_node
  85. * @of_node: device_node of the resource in question
  86. * @label: name used to reference the device from the driver
  87. *
  88. * Note that we explicitly add a 'label' pointer here since per
  89. * the ePAPR 2.2.2, the device_node->name should be generic and not
  90. * reflect precise programming model. Thus label enables a
  91. * platform specific name to be assigned with the 'label' binding to
  92. * allow for unique query names.
  93. */
  94. struct spmi_resource {
  95. struct resource *resource;
  96. u32 num_resources;
  97. struct device_node *of_node;
  98. const char *label;
  99. };
  100. /**
  101. * Client/device handle (struct spmi_device):
  102. * ------------------------------------------
  103. * This is the client/device handle returned when a SPMI device
  104. * is registered with a controller.
  105. * Pointer to this structure is used by client-driver as a handle.
  106. * @dev: Driver model representation of the device.
  107. * @name: Name of driver to use with this device.
  108. * @ctrl: SPMI controller managing the bus hosting this device.
  109. * @res: SPMI resource for the primary node
  110. * @dev_node: array of SPMI resources when used with spmi-dev-container.
  111. * @num_dev_node: number of device_node structures.
  112. * @sid: Slave Identifier.
  113. * @id: Unique identifier to differentiate from other spmi devices with
  114. * possibly same name.
  115. *
  116. */
  117. struct spmi_device {
  118. struct device dev;
  119. const char *name;
  120. struct spmi_controller *ctrl;
  121. struct spmi_resource res;
  122. struct spmi_resource *dev_node;
  123. u32 num_dev_node;
  124. u8 sid;
  125. int id;
  126. };
  127. #define to_spmi_device(d) container_of(d, struct spmi_device, dev)
  128. /**
  129. * struct spmi_boardinfo: Declare board info for SPMI device bringup.
  130. * @name: Name of driver to use with this device.
  131. * @slave_id: slave identifier.
  132. * @spmi_device: device to be registered with the SPMI framework.
  133. * @of_node: pointer to the OpenFirmware device node.
  134. * @res: SPMI resource for the primary node
  135. * @dev_node: array of SPMI resources when used with spmi-dev-container.
  136. * @num_dev_node: number of device_node structures.
  137. * @platform_data: goes to spmi_device.dev.platform_data
  138. */
  139. struct spmi_boardinfo {
  140. char name[SPMI_NAME_SIZE];
  141. uint8_t slave_id;
  142. struct device_node *of_node;
  143. struct spmi_resource res;
  144. struct spmi_resource *dev_node;
  145. u32 num_dev_node;
  146. const void *platform_data;
  147. };
  148. /**
  149. * spmi_driver_register: Client driver registration with SPMI framework.
  150. * @drv: client driver to be associated with client-device.
  151. *
  152. * This API will register the client driver with the SPMI framework.
  153. * It is called from the driver's module-init function.
  154. */
  155. extern int spmi_driver_register(struct spmi_driver *drv);
  156. /**
  157. * spmi_driver_unregister - reverse effect of spmi_driver_register
  158. * @sdrv: the driver to unregister
  159. * Context: can sleep
  160. */
  161. static inline void spmi_driver_unregister(struct spmi_driver *sdrv)
  162. {
  163. if (sdrv)
  164. driver_unregister(&sdrv->driver);
  165. }
  166. /**
  167. * spmi_add_controller: Controller bring-up.
  168. * @ctrl: controller to be registered.
  169. *
  170. * A controller is registered with the framework using this API. ctrl->nr is the
  171. * desired number with which SPMI framework registers the controller.
  172. * Function will return -EBUSY if the number is in use.
  173. */
  174. extern int spmi_add_controller(struct spmi_controller *ctrl);
  175. /**
  176. * spmi_del_controller: Controller tear-down.
  177. * Controller added with the above API is teared down using this API.
  178. */
  179. extern int spmi_del_controller(struct spmi_controller *ctrl);
  180. /**
  181. * spmi_busnum_to_ctrl: Map bus number to controller
  182. * @busnum: bus number
  183. *
  184. * Returns controller device representing this bus number
  185. */
  186. extern struct spmi_controller *spmi_busnum_to_ctrl(u32 bus_num);
  187. /**
  188. * spmi_alloc_device: Allocate a new SPMI devices.
  189. * @ctrl: controller to which this device is to be added to.
  190. * Context: can sleep
  191. *
  192. * Allows a driver to allocate and initialize a SPMI device without
  193. * registering it immediately. This allows a driver to directly fill
  194. * the spmi_device structure before calling spmi_add_device().
  195. *
  196. * Caller is responsible to call spmi_add_device() on the returned
  197. * spmi_device. If the caller needs to discard the spmi_device without
  198. * adding it, then spmi_dev_put() should be called.
  199. */
  200. extern struct spmi_device *spmi_alloc_device(struct spmi_controller *ctrl);
  201. /**
  202. * spmi_add_device: Add spmi_device allocated with spmi_alloc_device().
  203. * @spmi_dev: spmi_device to be added (registered).
  204. */
  205. extern int spmi_add_device(struct spmi_device *spmi_dev);
  206. /**
  207. * spmi_new_device: Instantiates a new SPMI device
  208. * @ctrl: controller to which this device is to be added to.
  209. * @info: board information for this device.
  210. *
  211. * Returns the new device or NULL.
  212. */
  213. extern struct spmi_device *spmi_new_device(struct spmi_controller *ctrl,
  214. struct spmi_boardinfo const *info);
  215. /* spmi_remove_device: Remove the effect of spmi_add_device() */
  216. extern void spmi_remove_device(struct spmi_device *spmi_dev);
  217. #ifdef CONFIG_SPMI
  218. /**
  219. * spmi_register_board_info: Board-initialization routine.
  220. * @bus_num: controller number (bus) on which this device will sit.
  221. * @info: list of all devices on all controllers present on the board.
  222. * @n: number of entries.
  223. *
  224. * API enumerates respective devices on corresponding controller.
  225. * Called from board-init function.
  226. */
  227. extern int spmi_register_board_info(int busnum,
  228. struct spmi_boardinfo const *info, unsigned n);
  229. #else
  230. static inline int spmi_register_board_info(int busnum,
  231. struct spmi_boardinfo const *info, unsigned n)
  232. {
  233. return 0;
  234. }
  235. #endif
  236. static inline void *spmi_get_ctrldata(const struct spmi_controller *ctrl)
  237. {
  238. return dev_get_drvdata(&ctrl->dev);
  239. }
  240. static inline void spmi_set_ctrldata(struct spmi_controller *ctrl, void *data)
  241. {
  242. dev_set_drvdata(&ctrl->dev, data);
  243. }
  244. static inline void *spmi_get_devicedata(const struct spmi_device *dev)
  245. {
  246. return dev_get_drvdata(&dev->dev);
  247. }
  248. static inline void spmi_set_devicedata(struct spmi_device *dev, void *data)
  249. {
  250. dev_set_drvdata(&dev->dev, data);
  251. }
  252. static inline void spmi_dev_put(struct spmi_device *spmidev)
  253. {
  254. if (spmidev)
  255. put_device(&spmidev->dev);
  256. }
  257. /**
  258. * spmi_register_read() - register read
  259. * @ctrl: SPMI controller.
  260. * @sid: slave identifier.
  261. * @ad: slave register address (5-bit address).
  262. * @buf: buffer to be populated with data from the Slave.
  263. *
  264. * Reads 1 byte of data from a Slave device register.
  265. */
  266. extern int spmi_register_read(struct spmi_controller *ctrl,
  267. u8 sid, u8 ad, u8 *buf);
  268. /**
  269. * spmi_ext_register_read() - extended register read
  270. * @ctrl: SPMI controller.
  271. * @sid: slave identifier.
  272. * @ad: slave register address (8-bit address).
  273. * @len: the request number of bytes to read (up to 16 bytes).
  274. * @buf: buffer to be populated with data from the Slave.
  275. *
  276. * Reads up to 16 bytes of data from the extended register space on a
  277. * Slave device.
  278. */
  279. extern int spmi_ext_register_read(struct spmi_controller *ctrl,
  280. u8 sid, u8 ad, u8 *buf, int len);
  281. /**
  282. * spmi_ext_register_readl() - extended register read long
  283. * @ctrl: SPMI controller.
  284. * @sid: slave identifier.
  285. * @ad: slave register address (16-bit address).
  286. * @len: the request number of bytes to read (up to 8 bytes).
  287. * @buf: buffer to be populated with data from the Slave.
  288. *
  289. * Reads up to 8 bytes of data from the extended register space on a
  290. * Slave device using 16-bit address.
  291. */
  292. extern int spmi_ext_register_readl(struct spmi_controller *ctrl,
  293. u8 sid, u16 ad, u8 *buf, int len);
  294. /**
  295. * spmi_register_write() - register write
  296. * @ctrl: SPMI controller.
  297. * @sid: slave identifier.
  298. * @ad: slave register address (5-bit address).
  299. * @buf: buffer containing the data to be transferred to the Slave.
  300. *
  301. * Writes 1 byte of data to a Slave device register.
  302. */
  303. extern int spmi_register_write(struct spmi_controller *ctrl,
  304. u8 sid, u8 ad, u8 *buf);
  305. /**
  306. * spmi_register_zero_write() - register zero write
  307. * @ctrl: SPMI controller.
  308. * @sid: slave identifier.
  309. * @data: the data to be written to register 0 (7-bits).
  310. *
  311. * Writes data to register 0 of the Slave device.
  312. */
  313. extern int spmi_register_zero_write(struct spmi_controller *ctrl,
  314. u8 sid, u8 data);
  315. /**
  316. * spmi_ext_register_write() - extended register write
  317. * @ctrl: SPMI controller.
  318. * @sid: slave identifier.
  319. * @ad: slave register address (8-bit address).
  320. * @buf: buffer containing the data to be transferred to the Slave.
  321. * @len: the request number of bytes to read (up to 16 bytes).
  322. *
  323. * Writes up to 16 bytes of data to the extended register space of a
  324. * Slave device.
  325. */
  326. extern int spmi_ext_register_write(struct spmi_controller *ctrl,
  327. u8 sid, u8 ad, u8 *buf, int len);
  328. /**
  329. * spmi_ext_register_writel() - extended register write long
  330. * @ctrl: SPMI controller.
  331. * @sid: slave identifier.
  332. * @ad: slave register address (16-bit address).
  333. * @buf: buffer containing the data to be transferred to the Slave.
  334. * @len: the request number of bytes to read (up to 8 bytes).
  335. *
  336. * Writes up to 8 bytes of data to the extended register space of a
  337. * Slave device using 16-bit address.
  338. */
  339. extern int spmi_ext_register_writel(struct spmi_controller *ctrl,
  340. u8 sid, u16 ad, u8 *buf, int len);
  341. /**
  342. * spmi_command_reset() - sends RESET command to the specified slave
  343. * @ctrl: SPMI controller.
  344. * @sid: slave identifier.
  345. *
  346. * The Reset command initializes the Slave and forces all registers to
  347. * their reset values. The Slave shall enter the STARTUP state after
  348. * receiving a Reset command.
  349. *
  350. * Returns
  351. * -EINVAL for invalid slave identifier.
  352. * -EPERM if the SPMI transaction is denied due to permission issues.
  353. * -EIO if the SPMI transaction fails (parity errors, etc).
  354. * -ETIMEDOUT if the SPMI transaction times out.
  355. * -EAGAIN if the SPMI transaction is temporarily unavailable
  356. */
  357. extern int spmi_command_reset(struct spmi_controller *ctrl, u8 sid);
  358. /**
  359. * spmi_command_sleep() - sends SLEEP command to the specified slave
  360. * @ctrl: SPMI controller.
  361. * @sid: slave identifier.
  362. *
  363. * The Sleep command causes the Slave to enter the user defined SLEEP state.
  364. *
  365. * Returns
  366. * -EINVAL for invalid slave identifier.
  367. * -EPERM if the SPMI transaction is denied due to permission issues.
  368. * -EIO if the SPMI transaction fails (parity errors, etc).
  369. * -ETIMEDOUT if the SPMI transaction times out.
  370. * -EAGAIN if the SPMI transaction is temporarily unavailable
  371. */
  372. extern int spmi_command_sleep(struct spmi_controller *ctrl, u8 sid);
  373. /**
  374. * spmi_command_wakeup() - sends WAKEUP command to the specified slave
  375. * @ctrl: SPMI controller.
  376. * @sid: slave identifier.
  377. *
  378. * The Wakeup command causes the Slave to move from the SLEEP state to
  379. * the ACTIVE state.
  380. *
  381. * Returns
  382. * -EINVAL for invalid slave identifier.
  383. * -EPERM if the SPMI transaction is denied due to permission issues.
  384. * -EIO if the SPMI transaction fails (parity errors, etc).
  385. * -ETIMEDOUT if the SPMI transaction times out.
  386. * -EAGAIN if the SPMI transaction is temporarily unavailable
  387. */
  388. extern int spmi_command_wakeup(struct spmi_controller *ctrl, u8 sid);
  389. /**
  390. * spmi_command_shutdown() - sends SHUTDOWN command to the specified slave
  391. * @ctrl: SPMI controller.
  392. * @sid: slave identifier.
  393. *
  394. * The Shutdown command causes the Slave to enter the SHUTDOWN state.
  395. *
  396. * Returns
  397. * -EINVAL for invalid slave identifier.
  398. * -EPERM if the SPMI transaction is denied due to permission issues.
  399. * -EIO if the SPMI transaction fails (parity errors, etc).
  400. * -ETIMEDOUT if the SPMI transaction times out.
  401. * -EAGAIN if the SPMI transaction is temporarily unavailable
  402. */
  403. extern int spmi_command_shutdown(struct spmi_controller *ctrl, u8 sid);
  404. /**
  405. * spmi_for_each_container_dev - iterate over the array of devnode resources.
  406. * @res: spmi_resource pointer used as the array cursor
  407. * @spmi_dev: spmi_device to iterate
  408. *
  409. * Only useable in spmi-dev-container configurations.
  410. */
  411. #define spmi_for_each_container_dev(res, spmi_dev) \
  412. for (res = ((spmi_dev)->dev_node ? &(spmi_dev)->dev_node[0] : NULL); \
  413. (res - (spmi_dev)->dev_node) < (spmi_dev)->num_dev_node; res++)
  414. extern struct resource *spmi_get_resource(struct spmi_device *dev,
  415. struct spmi_resource *node,
  416. unsigned int type, unsigned int res_num);
  417. struct resource *spmi_get_resource_byname(struct spmi_device *dev,
  418. struct spmi_resource *node,
  419. unsigned int type,
  420. const char *name);
  421. extern int spmi_get_irq(struct spmi_device *dev, struct spmi_resource *node,
  422. unsigned int res_num);
  423. extern int spmi_get_irq_byname(struct spmi_device *dev,
  424. struct spmi_resource *node, const char *name);
  425. /**
  426. * spmi_get_node_name - return device name for spmi node
  427. * @dev: spmi device handle
  428. *
  429. * Get the primary node name of a spmi_device coresponding with
  430. * with the 'label' binding.
  431. *
  432. * Returns NULL if no primary dev name has been assigned to this spmi_device.
  433. */
  434. static inline const char *spmi_get_primary_dev_name(struct spmi_device *dev)
  435. {
  436. if (dev->res.label)
  437. return dev->res.label;
  438. return NULL;
  439. }
  440. struct spmi_resource *spmi_get_dev_container_byname(struct spmi_device *dev,
  441. const char *label);
  442. #endif