/include/linux/spmi.h

https://github.com/airy09/android_kernel_sony_apq8064 · C Header · 478 lines · 159 code · 44 blank · 275 comment · 4 complexity · beee532f0416d7221c3fc2046e470561 MD5 · raw file

  1. /* Copyright (c) 2012, 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 list_head list;
  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. */
  114. struct spmi_device {
  115. struct device dev;
  116. const char *name;
  117. struct spmi_controller *ctrl;
  118. struct spmi_resource res;
  119. struct spmi_resource *dev_node;
  120. u32 num_dev_node;
  121. u8 sid;
  122. };
  123. #define to_spmi_device(d) container_of(d, struct spmi_device, dev)
  124. /**
  125. * struct spmi_boardinfo: Declare board info for SPMI device bringup.
  126. * @name: Name of driver to use with this device.
  127. * @slave_id: slave identifier.
  128. * @spmi_device: device to be registered with the SPMI framework.
  129. * @of_node: pointer to the OpenFirmware device node.
  130. * @res: SPMI resource for the primary node
  131. * @dev_node: array of SPMI resources when used with spmi-dev-container.
  132. * @num_dev_node: number of device_node structures.
  133. * @platform_data: goes to spmi_device.dev.platform_data
  134. */
  135. struct spmi_boardinfo {
  136. char name[SPMI_NAME_SIZE];
  137. uint8_t slave_id;
  138. struct device_node *of_node;
  139. struct spmi_resource res;
  140. struct spmi_resource *dev_node;
  141. u32 num_dev_node;
  142. const void *platform_data;
  143. };
  144. /**
  145. * spmi_driver_register: Client driver registration with SPMI framework.
  146. * @drv: client driver to be associated with client-device.
  147. *
  148. * This API will register the client driver with the SPMI framework.
  149. * It is called from the driver's module-init function.
  150. */
  151. extern int spmi_driver_register(struct spmi_driver *drv);
  152. /**
  153. * spmi_driver_unregister - reverse effect of spmi_driver_register
  154. * @sdrv: the driver to unregister
  155. * Context: can sleep
  156. */
  157. static inline void spmi_driver_unregister(struct spmi_driver *sdrv)
  158. {
  159. if (sdrv)
  160. driver_unregister(&sdrv->driver);
  161. }
  162. /**
  163. * spmi_add_controller: Controller bring-up.
  164. * @ctrl: controller to be registered.
  165. *
  166. * A controller is registered with the framework using this API. ctrl->nr is the
  167. * desired number with which SPMI framework registers the controller.
  168. * Function will return -EBUSY if the number is in use.
  169. */
  170. extern int spmi_add_controller(struct spmi_controller *ctrl);
  171. /**
  172. * spmi_del_controller: Controller tear-down.
  173. * Controller added with the above API is teared down using this API.
  174. */
  175. extern int spmi_del_controller(struct spmi_controller *ctrl);
  176. /**
  177. * spmi_busnum_to_ctrl: Map bus number to controller
  178. * @busnum: bus number
  179. *
  180. * Returns controller device representing this bus number
  181. */
  182. extern struct spmi_controller *spmi_busnum_to_ctrl(u32 bus_num);
  183. /**
  184. * spmi_alloc_device: Allocate a new SPMI devices.
  185. * @ctrl: controller to which this device is to be added to.
  186. * Context: can sleep
  187. *
  188. * Allows a driver to allocate and initialize a SPMI device without
  189. * registering it immediately. This allows a driver to directly fill
  190. * the spmi_device structure before calling spmi_add_device().
  191. *
  192. * Caller is responsible to call spmi_add_device() on the returned
  193. * spmi_device. If the caller needs to discard the spmi_device without
  194. * adding it, then spmi_dev_put() should be called.
  195. */
  196. extern struct spmi_device *spmi_alloc_device(struct spmi_controller *ctrl);
  197. /**
  198. * spmi_add_device: Add spmi_device allocated with spmi_alloc_device().
  199. * @spmi_dev: spmi_device to be added (registered).
  200. */
  201. extern int spmi_add_device(struct spmi_device *spmi_dev);
  202. /**
  203. * spmi_new_device: Instantiates a new SPMI device
  204. * @ctrl: controller to which this device is to be added to.
  205. * @info: board information for this device.
  206. *
  207. * Returns the new device or NULL.
  208. */
  209. extern struct spmi_device *spmi_new_device(struct spmi_controller *ctrl,
  210. struct spmi_boardinfo const *info);
  211. /* spmi_remove_device: Remove the effect of spmi_add_device() */
  212. extern void spmi_remove_device(struct spmi_device *spmi_dev);
  213. #ifdef CONFIG_SPMI
  214. /**
  215. * spmi_register_board_info: Board-initialization routine.
  216. * @bus_num: controller number (bus) on which this device will sit.
  217. * @info: list of all devices on all controllers present on the board.
  218. * @n: number of entries.
  219. *
  220. * API enumerates respective devices on corresponding controller.
  221. * Called from board-init function.
  222. */
  223. extern int spmi_register_board_info(int busnum,
  224. struct spmi_boardinfo const *info, unsigned n);
  225. #else
  226. static inline int spmi_register_board_info(int busnum,
  227. struct spmi_boardinfo const *info, unsigned n)
  228. {
  229. return 0;
  230. }
  231. #endif
  232. static inline void *spmi_get_ctrldata(const struct spmi_controller *ctrl)
  233. {
  234. return dev_get_drvdata(&ctrl->dev);
  235. }
  236. static inline void spmi_set_ctrldata(struct spmi_controller *ctrl, void *data)
  237. {
  238. dev_set_drvdata(&ctrl->dev, data);
  239. }
  240. static inline void *spmi_get_devicedata(const struct spmi_device *dev)
  241. {
  242. return dev_get_drvdata(&dev->dev);
  243. }
  244. static inline void spmi_set_devicedata(struct spmi_device *dev, void *data)
  245. {
  246. dev_set_drvdata(&dev->dev, data);
  247. }
  248. static inline void spmi_dev_put(struct spmi_device *spmidev)
  249. {
  250. if (spmidev)
  251. put_device(&spmidev->dev);
  252. }
  253. /**
  254. * spmi_register_read() - register read
  255. * @ctrl: SPMI controller.
  256. * @sid: slave identifier.
  257. * @ad: slave register address (5-bit address).
  258. * @buf: buffer to be populated with data from the Slave.
  259. *
  260. * Reads 1 byte of data from a Slave device register.
  261. */
  262. extern int spmi_register_read(struct spmi_controller *ctrl,
  263. u8 sid, u8 ad, u8 *buf);
  264. /**
  265. * spmi_ext_register_read() - extended register read
  266. * @ctrl: SPMI controller.
  267. * @sid: slave identifier.
  268. * @ad: slave register address (8-bit address).
  269. * @len: the request number of bytes to read (up to 16 bytes).
  270. * @buf: buffer to be populated with data from the Slave.
  271. *
  272. * Reads up to 16 bytes of data from the extended register space on a
  273. * Slave device.
  274. */
  275. extern int spmi_ext_register_read(struct spmi_controller *ctrl,
  276. u8 sid, u8 ad, u8 *buf, int len);
  277. /**
  278. * spmi_ext_register_readl() - extended register read long
  279. * @ctrl: SPMI controller.
  280. * @sid: slave identifier.
  281. * @ad: slave register address (16-bit address).
  282. * @len: the request number of bytes to read (up to 8 bytes).
  283. * @buf: buffer to be populated with data from the Slave.
  284. *
  285. * Reads up to 8 bytes of data from the extended register space on a
  286. * Slave device using 16-bit address.
  287. */
  288. extern int spmi_ext_register_readl(struct spmi_controller *ctrl,
  289. u8 sid, u16 ad, u8 *buf, int len);
  290. /**
  291. * spmi_register_write() - register write
  292. * @ctrl: SPMI controller.
  293. * @sid: slave identifier.
  294. * @ad: slave register address (5-bit address).
  295. * @buf: buffer containing the data to be transferred to the Slave.
  296. *
  297. * Writes 1 byte of data to a Slave device register.
  298. */
  299. extern int spmi_register_write(struct spmi_controller *ctrl,
  300. u8 sid, u8 ad, u8 *buf);
  301. /**
  302. * spmi_register_zero_write() - register zero write
  303. * @ctrl: SPMI controller.
  304. * @sid: slave identifier.
  305. * @data: the data to be written to register 0 (7-bits).
  306. *
  307. * Writes data to register 0 of the Slave device.
  308. */
  309. extern int spmi_register_zero_write(struct spmi_controller *ctrl,
  310. u8 sid, u8 data);
  311. /**
  312. * spmi_ext_register_write() - extended register write
  313. * @ctrl: SPMI controller.
  314. * @sid: slave identifier.
  315. * @ad: slave register address (8-bit address).
  316. * @buf: buffer containing the data to be transferred to the Slave.
  317. * @len: the request number of bytes to read (up to 16 bytes).
  318. *
  319. * Writes up to 16 bytes of data to the extended register space of a
  320. * Slave device.
  321. */
  322. extern int spmi_ext_register_write(struct spmi_controller *ctrl,
  323. u8 sid, u8 ad, u8 *buf, int len);
  324. /**
  325. * spmi_ext_register_writel() - extended register write long
  326. * @ctrl: SPMI controller.
  327. * @sid: slave identifier.
  328. * @ad: slave register address (16-bit address).
  329. * @buf: buffer containing the data to be transferred to the Slave.
  330. * @len: the request number of bytes to read (up to 8 bytes).
  331. *
  332. * Writes up to 8 bytes of data to the extended register space of a
  333. * Slave device using 16-bit address.
  334. */
  335. extern int spmi_ext_register_writel(struct spmi_controller *ctrl,
  336. u8 sid, u16 ad, u8 *buf, int len);
  337. /**
  338. * spmi_command_reset() - sends RESET command to the specified slave
  339. * @ctrl: SPMI controller.
  340. * @sid: slave identifier.
  341. *
  342. * The Reset command initializes the Slave and forces all registers to
  343. * their reset values. The Slave shall enter the STARTUP state after
  344. * receiving a Reset command.
  345. *
  346. * Returns
  347. * -EINVAL for invalid slave identifier.
  348. * -EPERM if the SPMI transaction is denied due to permission issues.
  349. * -EIO if the SPMI transaction fails (parity errors, etc).
  350. * -ETIMEDOUT if the SPMI transaction times out.
  351. */
  352. extern int spmi_command_reset(struct spmi_controller *ctrl, u8 sid);
  353. /**
  354. * spmi_command_sleep() - sends SLEEP command to the specified slave
  355. * @ctrl: SPMI controller.
  356. * @sid: slave identifier.
  357. *
  358. * The Sleep command causes the Slave to enter the user defined SLEEP state.
  359. *
  360. * Returns
  361. * -EINVAL for invalid slave identifier.
  362. * -EPERM if the SPMI transaction is denied due to permission issues.
  363. * -EIO if the SPMI transaction fails (parity errors, etc).
  364. * -ETIMEDOUT if the SPMI transaction times out.
  365. */
  366. extern int spmi_command_sleep(struct spmi_controller *ctrl, u8 sid);
  367. /**
  368. * spmi_command_wakeup() - sends WAKEUP command to the specified slave
  369. * @ctrl: SPMI controller.
  370. * @sid: slave identifier.
  371. *
  372. * The Wakeup command causes the Slave to move from the SLEEP state to
  373. * the ACTIVE state.
  374. *
  375. * Returns
  376. * -EINVAL for invalid slave identifier.
  377. * -EPERM if the SPMI transaction is denied due to permission issues.
  378. * -EIO if the SPMI transaction fails (parity errors, etc).
  379. * -ETIMEDOUT if the SPMI transaction times out.
  380. */
  381. extern int spmi_command_wakeup(struct spmi_controller *ctrl, u8 sid);
  382. /**
  383. * spmi_command_shutdown() - sends SHUTDOWN command to the specified slave
  384. * @ctrl: SPMI controller.
  385. * @sid: slave identifier.
  386. *
  387. * The Shutdown command causes the Slave to enter the SHUTDOWN state.
  388. *
  389. * Returns
  390. * -EINVAL for invalid slave identifier.
  391. * -EPERM if the SPMI transaction is denied due to permission issues.
  392. * -EIO if the SPMI transaction fails (parity errors, etc).
  393. * -ETIMEDOUT if the SPMI transaction times out.
  394. */
  395. extern int spmi_command_shutdown(struct spmi_controller *ctrl, u8 sid);
  396. /**
  397. * spmi_for_each_container_dev - iterate over the array of devnode resources.
  398. * @res: spmi_resource pointer used as the array cursor
  399. * @spmi_dev: spmi_device to iterate
  400. *
  401. * Only useable in spmi-dev-container configurations.
  402. */
  403. #define spmi_for_each_container_dev(res, spmi_dev) \
  404. for (res = ((spmi_dev)->dev_node ? &(spmi_dev)->dev_node[0] : NULL); \
  405. (res - (spmi_dev)->dev_node) < (spmi_dev)->num_dev_node; res++)
  406. extern struct resource *spmi_get_resource(struct spmi_device *dev,
  407. struct spmi_resource *node,
  408. unsigned int type, unsigned int res_num);
  409. struct resource *spmi_get_resource_byname(struct spmi_device *dev,
  410. struct spmi_resource *node,
  411. unsigned int type,
  412. const char *name);
  413. extern int spmi_get_irq(struct spmi_device *dev, struct spmi_resource *node,
  414. unsigned int res_num);
  415. extern int spmi_get_irq_byname(struct spmi_device *dev,
  416. struct spmi_resource *node, const char *name);
  417. /**
  418. * spmi_get_node_name - return device name for spmi node
  419. * @dev: spmi device handle
  420. *
  421. * Get the primary node name of a spmi_device coresponding with
  422. * with the 'label' binding.
  423. *
  424. * Returns NULL if no primary dev name has been assigned to this spmi_device.
  425. */
  426. static inline const char *spmi_get_primary_dev_name(struct spmi_device *dev)
  427. {
  428. if (dev->res.label)
  429. return dev->res.label;
  430. return NULL;
  431. }
  432. struct spmi_resource *spmi_get_dev_container_byname(struct spmi_device *dev,
  433. const char *label);
  434. #endif