spmi.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* Copyright (c) 2012-2013, 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. #define SPMI_CMD_EXT_WRITE 0x00
  21. #define SPMI_CMD_RESET 0x10
  22. #define SPMI_CMD_SLEEP 0x11
  23. #define SPMI_CMD_SHUTDOWN 0x12
  24. #define SPMI_CMD_WAKEUP 0x13
  25. #define SPMI_CMD_AUTHENTICATE 0x14
  26. #define SPMI_CMD_MSTR_READ 0x15
  27. #define SPMI_CMD_MSTR_WRITE 0x16
  28. #define SPMI_CMD_TRANSFER_BUS_OWNERSHIP 0x1A
  29. #define SPMI_CMD_DDB_MASTER_READ 0x1B
  30. #define SPMI_CMD_DDB_SLAVE_READ 0x1C
  31. #define SPMI_CMD_EXT_READ 0x20
  32. #define SPMI_CMD_EXT_WRITEL 0x30
  33. #define SPMI_CMD_EXT_READL 0x38
  34. #define SPMI_CMD_WRITE 0x40
  35. #define SPMI_CMD_READ 0x60
  36. #define SPMI_CMD_ZERO_WRITE 0x80
  37. /**
  38. * struct spmi_device - Basic representation of an SPMI device
  39. * @dev: Driver model representation of the device.
  40. * @ctrl: SPMI controller managing the bus hosting this device.
  41. * @usid: This devices' Unique Slave IDentifier.
  42. */
  43. struct spmi_device {
  44. struct device dev;
  45. struct spmi_controller *ctrl;
  46. u8 usid;
  47. };
  48. static inline struct spmi_device *to_spmi_device(struct device *d)
  49. {
  50. return container_of(d, struct spmi_device, dev);
  51. }
  52. static inline void *spmi_device_get_drvdata(const struct spmi_device *sdev)
  53. {
  54. return dev_get_drvdata(&sdev->dev);
  55. }
  56. static inline void spmi_device_set_drvdata(struct spmi_device *sdev, void *data)
  57. {
  58. dev_set_drvdata(&sdev->dev, data);
  59. }
  60. struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl);
  61. static inline void spmi_device_put(struct spmi_device *sdev)
  62. {
  63. if (sdev)
  64. put_device(&sdev->dev);
  65. }
  66. int spmi_device_add(struct spmi_device *sdev);
  67. void spmi_device_remove(struct spmi_device *sdev);
  68. /**
  69. * struct spmi_controller - interface to the SPMI master controller
  70. * @dev: Driver model representation of the device.
  71. * @nr: board-specific number identifier for this controller/bus
  72. * @cmd: sends a non-data command sequence on the SPMI bus.
  73. * @read_cmd: sends a register read command sequence on the SPMI bus.
  74. * @write_cmd: sends a register write command sequence on the SPMI bus.
  75. */
  76. struct spmi_controller {
  77. struct device dev;
  78. unsigned int nr;
  79. int (*cmd)(struct spmi_controller *ctrl, u8 opcode, u8 sid);
  80. int (*read_cmd)(struct spmi_controller *ctrl, u8 opcode,
  81. u8 sid, u16 addr, u8 *buf, size_t len);
  82. int (*write_cmd)(struct spmi_controller *ctrl, u8 opcode,
  83. u8 sid, u16 addr, const u8 *buf, size_t len);
  84. };
  85. static inline struct spmi_controller *to_spmi_controller(struct device *d)
  86. {
  87. return container_of(d, struct spmi_controller, dev);
  88. }
  89. static inline
  90. void *spmi_controller_get_drvdata(const struct spmi_controller *ctrl)
  91. {
  92. return dev_get_drvdata(&ctrl->dev);
  93. }
  94. static inline void spmi_controller_set_drvdata(struct spmi_controller *ctrl,
  95. void *data)
  96. {
  97. dev_set_drvdata(&ctrl->dev, data);
  98. }
  99. struct spmi_controller *spmi_controller_alloc(struct device *parent,
  100. size_t size);
  101. /**
  102. * spmi_controller_put() - decrement controller refcount
  103. * @ctrl SPMI controller.
  104. */
  105. static inline void spmi_controller_put(struct spmi_controller *ctrl)
  106. {
  107. if (ctrl)
  108. put_device(&ctrl->dev);
  109. }
  110. int spmi_controller_add(struct spmi_controller *ctrl);
  111. void spmi_controller_remove(struct spmi_controller *ctrl);
  112. /**
  113. * struct spmi_driver - SPMI slave device driver
  114. * @driver: SPMI device drivers should initialize name and owner field of
  115. * this structure.
  116. * @probe: binds this driver to a SPMI device.
  117. * @remove: unbinds this driver from the SPMI device.
  118. *
  119. * If PM runtime support is desired for a slave, a device driver can call
  120. * pm_runtime_put() from their probe() routine (and a balancing
  121. * pm_runtime_get() in remove()). PM runtime support for a slave is
  122. * implemented by issuing a SLEEP command to the slave on runtime_suspend(),
  123. * transitioning the slave into the SLEEP state. On runtime_resume(), a WAKEUP
  124. * command is sent to the slave to bring it back to ACTIVE.
  125. */
  126. struct spmi_driver {
  127. struct device_driver driver;
  128. int (*probe)(struct spmi_device *sdev);
  129. void (*remove)(struct spmi_device *sdev);
  130. };
  131. static inline struct spmi_driver *to_spmi_driver(struct device_driver *d)
  132. {
  133. return container_of(d, struct spmi_driver, driver);
  134. }
  135. #define spmi_driver_register(sdrv) \
  136. __spmi_driver_register(sdrv, THIS_MODULE)
  137. int __spmi_driver_register(struct spmi_driver *sdrv, struct module *owner);
  138. /**
  139. * spmi_driver_unregister() - unregister an SPMI client driver
  140. * @sdrv: the driver to unregister
  141. */
  142. static inline void spmi_driver_unregister(struct spmi_driver *sdrv)
  143. {
  144. if (sdrv)
  145. driver_unregister(&sdrv->driver);
  146. }
  147. #define module_spmi_driver(__spmi_driver) \
  148. module_driver(__spmi_driver, spmi_driver_register, \
  149. spmi_driver_unregister)
  150. int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf);
  151. int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
  152. size_t len);
  153. int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
  154. size_t len);
  155. int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data);
  156. int spmi_register_zero_write(struct spmi_device *sdev, u8 data);
  157. int spmi_ext_register_write(struct spmi_device *sdev, u8 addr,
  158. const u8 *buf, size_t len);
  159. int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr,
  160. const u8 *buf, size_t len);
  161. int spmi_command_reset(struct spmi_device *sdev);
  162. int spmi_command_sleep(struct spmi_device *sdev);
  163. int spmi_command_wakeup(struct spmi_device *sdev);
  164. int spmi_command_shutdown(struct spmi_device *sdev);
  165. #endif