cros_ec.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * ChromeOS EC multi-function device
  3. *
  4. * Copyright (C) 2012 Google, Inc
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #ifndef __LINUX_MFD_CROS_EC_H
  16. #define __LINUX_MFD_CROS_EC_H
  17. #include <linux/cdev.h>
  18. #include <linux/device.h>
  19. #include <linux/notifier.h>
  20. #include <linux/mfd/cros_ec_commands.h>
  21. #include <linux/mutex.h>
  22. #define CROS_EC_DEV_NAME "cros_ec"
  23. #define CROS_EC_DEV_PD_NAME "cros_pd"
  24. /*
  25. * The EC is unresponsive for a time after a reboot command. Add a
  26. * simple delay to make sure that the bus stays locked.
  27. */
  28. #define EC_REBOOT_DELAY_MS 50
  29. /*
  30. * Max bus-specific overhead incurred by request/responses.
  31. * I2C requires 1 additional byte for requests.
  32. * I2C requires 2 additional bytes for responses.
  33. * */
  34. #define EC_PROTO_VERSION_UNKNOWN 0
  35. #define EC_MAX_REQUEST_OVERHEAD 1
  36. #define EC_MAX_RESPONSE_OVERHEAD 2
  37. /*
  38. * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
  39. */
  40. enum {
  41. EC_MSG_TX_HEADER_BYTES = 3,
  42. EC_MSG_TX_TRAILER_BYTES = 1,
  43. EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES +
  44. EC_MSG_TX_TRAILER_BYTES,
  45. EC_MSG_RX_PROTO_BYTES = 3,
  46. /* Max length of messages */
  47. EC_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
  48. EC_MSG_TX_PROTO_BYTES,
  49. };
  50. /*
  51. * @version: Command version number (often 0)
  52. * @command: Command to send (EC_CMD_...)
  53. * @outsize: Outgoing length in bytes
  54. * @insize: Max number of bytes to accept from EC
  55. * @result: EC's response to the command (separate from communication failure)
  56. * @data: Where to put the incoming data from EC and outgoing data to EC
  57. */
  58. struct cros_ec_command {
  59. uint32_t version;
  60. uint32_t command;
  61. uint32_t outsize;
  62. uint32_t insize;
  63. uint32_t result;
  64. uint8_t data[0];
  65. };
  66. /**
  67. * struct cros_ec_device - Information about a ChromeOS EC device
  68. *
  69. * @phys_name: name of physical comms layer (e.g. 'i2c-4')
  70. * @dev: Device pointer for physical comms device
  71. * @was_wake_device: true if this device was set to wake the system from
  72. * sleep at the last suspend
  73. * @cmd_readmem: direct read of the EC memory-mapped region, if supported
  74. * @offset is within EC_LPC_ADDR_MEMMAP region.
  75. * @bytes: number of bytes to read. zero means "read a string" (including
  76. * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be read.
  77. * Caller must ensure that the buffer is large enough for the result when
  78. * reading a string.
  79. *
  80. * @priv: Private data
  81. * @irq: Interrupt to use
  82. * @id: Device id
  83. * @din: input buffer (for data from EC)
  84. * @dout: output buffer (for data to EC)
  85. * \note
  86. * These two buffers will always be dword-aligned and include enough
  87. * space for up to 7 word-alignment bytes also, so we can ensure that
  88. * the body of the message is always dword-aligned (64-bit).
  89. * We use this alignment to keep ARM and x86 happy. Probably word
  90. * alignment would be OK, there might be a small performance advantage
  91. * to using dword.
  92. * @din_size: size of din buffer to allocate (zero to use static din)
  93. * @dout_size: size of dout buffer to allocate (zero to use static dout)
  94. * @wake_enabled: true if this device can wake the system from sleep
  95. * @cmd_xfer: send command to EC and get response
  96. * Returns the number of bytes received if the communication succeeded, but
  97. * that doesn't mean the EC was happy with the command. The caller
  98. * should check msg.result for the EC's result code.
  99. * @pkt_xfer: send packet to EC and get response
  100. * @lock: one transaction at a time
  101. */
  102. struct cros_ec_device {
  103. /* These are used by other drivers that want to talk to the EC */
  104. const char *phys_name;
  105. struct device *dev;
  106. bool was_wake_device;
  107. struct class *cros_class;
  108. int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
  109. unsigned int bytes, void *dest);
  110. /* These are used to implement the platform-specific interface */
  111. u16 max_request;
  112. u16 max_response;
  113. u16 max_passthru;
  114. u16 proto_version;
  115. void *priv;
  116. int irq;
  117. u8 *din;
  118. u8 *dout;
  119. int din_size;
  120. int dout_size;
  121. bool wake_enabled;
  122. int (*cmd_xfer)(struct cros_ec_device *ec,
  123. struct cros_ec_command *msg);
  124. int (*pkt_xfer)(struct cros_ec_device *ec,
  125. struct cros_ec_command *msg);
  126. struct mutex lock;
  127. };
  128. /* struct cros_ec_platform - ChromeOS EC platform information
  129. *
  130. * @ec_name: name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
  131. * used in /dev/ and sysfs.
  132. * @cmd_offset: offset to apply for each command. Set when
  133. * registering a devicde behind another one.
  134. */
  135. struct cros_ec_platform {
  136. const char *ec_name;
  137. u16 cmd_offset;
  138. };
  139. /*
  140. * struct cros_ec_dev - ChromeOS EC device entry point
  141. *
  142. * @class_dev: Device structure used in sysfs
  143. * @cdev: Character device structure in /dev
  144. * @ec_dev: cros_ec_device structure to talk to the physical device
  145. * @dev: pointer to the platform device
  146. * @cmd_offset: offset to apply for each command.
  147. */
  148. struct cros_ec_dev {
  149. struct device class_dev;
  150. struct cdev cdev;
  151. struct cros_ec_device *ec_dev;
  152. struct device *dev;
  153. u16 cmd_offset;
  154. };
  155. /**
  156. * cros_ec_suspend - Handle a suspend operation for the ChromeOS EC device
  157. *
  158. * This can be called by drivers to handle a suspend event.
  159. *
  160. * ec_dev: Device to suspend
  161. * @return 0 if ok, -ve on error
  162. */
  163. int cros_ec_suspend(struct cros_ec_device *ec_dev);
  164. /**
  165. * cros_ec_resume - Handle a resume operation for the ChromeOS EC device
  166. *
  167. * This can be called by drivers to handle a resume event.
  168. *
  169. * @ec_dev: Device to resume
  170. * @return 0 if ok, -ve on error
  171. */
  172. int cros_ec_resume(struct cros_ec_device *ec_dev);
  173. /**
  174. * cros_ec_prepare_tx - Prepare an outgoing message in the output buffer
  175. *
  176. * This is intended to be used by all ChromeOS EC drivers, but at present
  177. * only SPI uses it. Once LPC uses the same protocol it can start using it.
  178. * I2C could use it now, with a refactor of the existing code.
  179. *
  180. * @ec_dev: Device to register
  181. * @msg: Message to write
  182. */
  183. int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
  184. struct cros_ec_command *msg);
  185. /**
  186. * cros_ec_check_result - Check ec_msg->result
  187. *
  188. * This is used by ChromeOS EC drivers to check the ec_msg->result for
  189. * errors and to warn about them.
  190. *
  191. * @ec_dev: EC device
  192. * @msg: Message to check
  193. */
  194. int cros_ec_check_result(struct cros_ec_device *ec_dev,
  195. struct cros_ec_command *msg);
  196. /**
  197. * cros_ec_cmd_xfer - Send a command to the ChromeOS EC
  198. *
  199. * Call this to send a command to the ChromeOS EC. This should be used
  200. * instead of calling the EC's cmd_xfer() callback directly.
  201. *
  202. * @ec_dev: EC device
  203. * @msg: Message to write
  204. */
  205. int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
  206. struct cros_ec_command *msg);
  207. /**
  208. * cros_ec_cmd_xfer_status - Send a command to the ChromeOS EC
  209. *
  210. * This function is identical to cros_ec_cmd_xfer, except it returns success
  211. * status only if both the command was transmitted successfully and the EC
  212. * replied with success status. It's not necessary to check msg->result when
  213. * using this function.
  214. *
  215. * @ec_dev: EC device
  216. * @msg: Message to write
  217. * @return: Num. of bytes transferred on success, <0 on failure
  218. */
  219. int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
  220. struct cros_ec_command *msg);
  221. /**
  222. * cros_ec_remove - Remove a ChromeOS EC
  223. *
  224. * Call this to deregister a ChromeOS EC, then clean up any private data.
  225. *
  226. * @ec_dev: Device to register
  227. * @return 0 if ok, -ve on error
  228. */
  229. int cros_ec_remove(struct cros_ec_device *ec_dev);
  230. /**
  231. * cros_ec_register - Register a new ChromeOS EC, using the provided info
  232. *
  233. * Before calling this, allocate a pointer to a new device and then fill
  234. * in all the fields up to the --private-- marker.
  235. *
  236. * @ec_dev: Device to register
  237. * @return 0 if ok, -ve on error
  238. */
  239. int cros_ec_register(struct cros_ec_device *ec_dev);
  240. /**
  241. * cros_ec_register - Query the protocol version supported by the ChromeOS EC
  242. *
  243. * @ec_dev: Device to register
  244. * @return 0 if ok, -ve on error
  245. */
  246. int cros_ec_query_all(struct cros_ec_device *ec_dev);
  247. /* sysfs stuff */
  248. extern struct attribute_group cros_ec_attr_group;
  249. extern struct attribute_group cros_ec_lightbar_attr_group;
  250. extern struct attribute_group cros_ec_vbc_attr_group;
  251. #endif /* __LINUX_MFD_CROS_EC_H */