spmi.h 5.6 KB

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