wilco-ec.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * ChromeOS Wilco Embedded Controller
  4. *
  5. * Copyright 2018 Google LLC
  6. */
  7. #ifndef WILCO_EC_H
  8. #define WILCO_EC_H
  9. #include <linux/mutex.h>
  10. #include <linux/types.h>
  11. /* Message flags for using the mailbox() interface */
  12. #define WILCO_EC_FLAG_NO_RESPONSE BIT(0) /* EC does not respond */
  13. /* Normal commands have a maximum 32 bytes of data */
  14. #define EC_MAILBOX_DATA_SIZE 32
  15. struct device;
  16. struct resource;
  17. struct platform_device;
  18. /**
  19. * struct wilco_ec_device - Wilco Embedded Controller handle.
  20. * @dev: Device handle.
  21. * @mailbox_lock: Mutex to ensure one mailbox command at a time.
  22. * @io_command: I/O port for mailbox command. Provided by ACPI.
  23. * @io_data: I/O port for mailbox data. Provided by ACPI.
  24. * @io_packet: I/O port for mailbox packet data. Provided by ACPI.
  25. * @data_buffer: Buffer used for EC communication. The same buffer
  26. * is used to hold the request and the response.
  27. * @data_size: Size of the data buffer used for EC communication.
  28. * @debugfs_pdev: The child platform_device used by the debugfs sub-driver.
  29. * @rtc_pdev: The child platform_device used by the RTC sub-driver.
  30. * @charger_pdev: Child platform_device used by the charger config sub-driver.
  31. * @telem_pdev: The child platform_device used by the telemetry sub-driver.
  32. */
  33. struct wilco_ec_device {
  34. struct device *dev;
  35. struct mutex mailbox_lock;
  36. struct resource *io_command;
  37. struct resource *io_data;
  38. struct resource *io_packet;
  39. void *data_buffer;
  40. size_t data_size;
  41. struct platform_device *debugfs_pdev;
  42. struct platform_device *rtc_pdev;
  43. struct platform_device *charger_pdev;
  44. struct platform_device *telem_pdev;
  45. };
  46. /**
  47. * struct wilco_ec_request - Mailbox request message format.
  48. * @struct_version: Should be %EC_MAILBOX_PROTO_VERSION
  49. * @checksum: Sum of all bytes must be 0.
  50. * @mailbox_id: Mailbox identifier, specifies the command set.
  51. * @mailbox_version: Mailbox interface version %EC_MAILBOX_VERSION
  52. * @reserved: Set to zero.
  53. * @data_size: Length of following data.
  54. */
  55. struct wilco_ec_request {
  56. u8 struct_version;
  57. u8 checksum;
  58. u16 mailbox_id;
  59. u8 mailbox_version;
  60. u8 reserved;
  61. u16 data_size;
  62. } __packed;
  63. /**
  64. * struct wilco_ec_response - Mailbox response message format.
  65. * @struct_version: Should be %EC_MAILBOX_PROTO_VERSION
  66. * @checksum: Sum of all bytes must be 0.
  67. * @result: Result code from the EC. Non-zero indicates an error.
  68. * @data_size: Length of the response data buffer.
  69. * @reserved: Set to zero.
  70. * @data: Response data buffer. Max size is %EC_MAILBOX_DATA_SIZE_EXTENDED.
  71. */
  72. struct wilco_ec_response {
  73. u8 struct_version;
  74. u8 checksum;
  75. u16 result;
  76. u16 data_size;
  77. u8 reserved[2];
  78. u8 data[];
  79. } __packed;
  80. /**
  81. * enum wilco_ec_msg_type - Message type to select a set of command codes.
  82. * @WILCO_EC_MSG_LEGACY: Legacy EC messages for standard EC behavior.
  83. * @WILCO_EC_MSG_PROPERTY: Get/Set/Sync EC controlled NVRAM property.
  84. * @WILCO_EC_MSG_TELEMETRY: Request telemetry data from the EC.
  85. */
  86. enum wilco_ec_msg_type {
  87. WILCO_EC_MSG_LEGACY = 0x00f0,
  88. WILCO_EC_MSG_PROPERTY = 0x00f2,
  89. WILCO_EC_MSG_TELEMETRY = 0x00f5,
  90. };
  91. /**
  92. * struct wilco_ec_message - Request and response message.
  93. * @type: Mailbox message type.
  94. * @flags: Message flags, e.g. %WILCO_EC_FLAG_NO_RESPONSE.
  95. * @request_size: Number of bytes to send to the EC.
  96. * @request_data: Buffer containing the request data.
  97. * @response_size: Number of bytes to read from EC.
  98. * @response_data: Buffer containing the response data, should be
  99. * response_size bytes and allocated by caller.
  100. */
  101. struct wilco_ec_message {
  102. enum wilco_ec_msg_type type;
  103. u8 flags;
  104. size_t request_size;
  105. void *request_data;
  106. size_t response_size;
  107. void *response_data;
  108. };
  109. /**
  110. * wilco_ec_mailbox() - Send request to the EC and receive the response.
  111. * @ec: Wilco EC device.
  112. * @msg: Wilco EC message.
  113. *
  114. * Return: Number of bytes received or negative error code on failure.
  115. */
  116. int wilco_ec_mailbox(struct wilco_ec_device *ec, struct wilco_ec_message *msg);
  117. /**
  118. * wilco_keyboard_leds_init() - Set up the keyboard backlight LEDs.
  119. * @ec: EC device to query.
  120. *
  121. * After this call, the keyboard backlight will be exposed through a an LED
  122. * device at /sys/class/leds.
  123. *
  124. * This may sleep because it uses wilco_ec_mailbox().
  125. *
  126. * Return: 0 on success, negative error code on failure.
  127. */
  128. int wilco_keyboard_leds_init(struct wilco_ec_device *ec);
  129. /*
  130. * A Property is typically a data item that is stored to NVRAM
  131. * by the EC. Each of these data items has an index associated
  132. * with it, known as the Property ID (PID). Properties may have
  133. * variable lengths, up to a max of WILCO_EC_PROPERTY_MAX_SIZE
  134. * bytes. Properties can be simple integers, or they may be more
  135. * complex binary data.
  136. */
  137. #define WILCO_EC_PROPERTY_MAX_SIZE 4
  138. /**
  139. * struct ec_property_set_msg - Message to get or set a property.
  140. * @property_id: Which property to get or set.
  141. * @length: Number of bytes of |data| that are used.
  142. * @data: Actual property data.
  143. */
  144. struct wilco_ec_property_msg {
  145. u32 property_id;
  146. int length;
  147. u8 data[WILCO_EC_PROPERTY_MAX_SIZE];
  148. };
  149. /**
  150. * wilco_ec_get_property() - Retrieve a property from the EC.
  151. * @ec: Embedded Controller device.
  152. * @prop_msg: Message for request and response.
  153. *
  154. * The property_id field of |prop_msg| should be filled before calling this
  155. * function. The result will be stored in the data and length fields.
  156. *
  157. * Return: 0 on success, negative error code on failure.
  158. */
  159. int wilco_ec_get_property(struct wilco_ec_device *ec,
  160. struct wilco_ec_property_msg *prop_msg);
  161. /**
  162. * wilco_ec_set_property() - Store a property on the EC.
  163. * @ec: Embedded Controller device.
  164. * @prop_msg: Message for request and response.
  165. *
  166. * The property_id, length, and data fields of |prop_msg| should be
  167. * filled before calling this function.
  168. *
  169. * Return: 0 on success, negative error code on failure.
  170. */
  171. int wilco_ec_set_property(struct wilco_ec_device *ec,
  172. struct wilco_ec_property_msg *prop_msg);
  173. /**
  174. * wilco_ec_get_byte_property() - Retrieve a byte-size property from the EC.
  175. * @ec: Embedded Controller device.
  176. * @property_id: Which property to retrieve.
  177. * @val: The result value, will be filled by this function.
  178. *
  179. * Return: 0 on success, negative error code on failure.
  180. */
  181. int wilco_ec_get_byte_property(struct wilco_ec_device *ec, u32 property_id,
  182. u8 *val);
  183. /**
  184. * wilco_ec_get_byte_property() - Store a byte-size property on the EC.
  185. * @ec: Embedded Controller device.
  186. * @property_id: Which property to store.
  187. * @val: Value to store.
  188. *
  189. * Return: 0 on success, negative error code on failure.
  190. */
  191. int wilco_ec_set_byte_property(struct wilco_ec_device *ec, u32 property_id,
  192. u8 val);
  193. /**
  194. * wilco_ec_add_sysfs() - Create sysfs entries
  195. * @ec: Wilco EC device
  196. *
  197. * wilco_ec_remove_sysfs() needs to be called afterwards
  198. * to perform the necessary cleanup.
  199. *
  200. * Return: 0 on success or negative error code on failure.
  201. */
  202. int wilco_ec_add_sysfs(struct wilco_ec_device *ec);
  203. void wilco_ec_remove_sysfs(struct wilco_ec_device *ec);
  204. #endif /* WILCO_EC_H */