cros_ec.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 for proto 2*/
  47. EC_PROTO2_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
  48. EC_MSG_TX_PROTO_BYTES,
  49. EC_MAX_MSG_BYTES = 64 * 1024,
  50. };
  51. /*
  52. * @version: Command version number (often 0)
  53. * @command: Command to send (EC_CMD_...)
  54. * @outsize: Outgoing length in bytes
  55. * @insize: Max number of bytes to accept from EC
  56. * @result: EC's response to the command (separate from communication failure)
  57. * @data: Where to put the incoming data from EC and outgoing data to EC
  58. */
  59. struct cros_ec_command {
  60. uint32_t version;
  61. uint32_t command;
  62. uint32_t outsize;
  63. uint32_t insize;
  64. uint32_t result;
  65. uint8_t data[0];
  66. };
  67. /**
  68. * struct cros_ec_device - Information about a ChromeOS EC device
  69. *
  70. * @phys_name: name of physical comms layer (e.g. 'i2c-4')
  71. * @dev: Device pointer for physical comms device
  72. * @was_wake_device: true if this device was set to wake the system from
  73. * sleep at the last suspend
  74. * @cmd_readmem: direct read of the EC memory-mapped region, if supported
  75. * @offset is within EC_LPC_ADDR_MEMMAP region.
  76. * @bytes: number of bytes to read. zero means "read a string" (including
  77. * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be read.
  78. * Caller must ensure that the buffer is large enough for the result when
  79. * reading a string.
  80. *
  81. * @priv: Private data
  82. * @irq: Interrupt to use
  83. * @id: Device id
  84. * @din: input buffer (for data from EC)
  85. * @dout: output buffer (for data to EC)
  86. * \note
  87. * These two buffers will always be dword-aligned and include enough
  88. * space for up to 7 word-alignment bytes also, so we can ensure that
  89. * the body of the message is always dword-aligned (64-bit).
  90. * We use this alignment to keep ARM and x86 happy. Probably word
  91. * alignment would be OK, there might be a small performance advantage
  92. * to using dword.
  93. * @din_size: size of din buffer to allocate (zero to use static din)
  94. * @dout_size: size of dout buffer to allocate (zero to use static dout)
  95. * @wake_enabled: true if this device can wake the system from sleep
  96. * @cmd_xfer: send command to EC and get response
  97. * Returns the number of bytes received if the communication succeeded, but
  98. * that doesn't mean the EC was happy with the command. The caller
  99. * should check msg.result for the EC's result code.
  100. * @pkt_xfer: send packet to EC and get response
  101. * @lock: one transaction at a time
  102. * @mkbp_event_supported: true if this EC supports the MKBP event protocol.
  103. * @event_notifier: interrupt event notifier for transport devices.
  104. * @event_data: raw payload transferred with the MKBP event.
  105. * @event_size: size in bytes of the event data.
  106. */
  107. struct cros_ec_device {
  108. /* These are used by other drivers that want to talk to the EC */
  109. const char *phys_name;
  110. struct device *dev;
  111. bool was_wake_device;
  112. struct class *cros_class;
  113. int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
  114. unsigned int bytes, void *dest);
  115. /* These are used to implement the platform-specific interface */
  116. u16 max_request;
  117. u16 max_response;
  118. u16 max_passthru;
  119. u16 proto_version;
  120. void *priv;
  121. int irq;
  122. u8 *din;
  123. u8 *dout;
  124. int din_size;
  125. int dout_size;
  126. bool wake_enabled;
  127. int (*cmd_xfer)(struct cros_ec_device *ec,
  128. struct cros_ec_command *msg);
  129. int (*pkt_xfer)(struct cros_ec_device *ec,
  130. struct cros_ec_command *msg);
  131. struct mutex lock;
  132. bool mkbp_event_supported;
  133. struct blocking_notifier_head event_notifier;
  134. struct ec_response_get_next_event event_data;
  135. int event_size;
  136. };
  137. /* struct cros_ec_platform - ChromeOS EC platform information
  138. *
  139. * @ec_name: name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
  140. * used in /dev/ and sysfs.
  141. * @cmd_offset: offset to apply for each command. Set when
  142. * registering a devicde behind another one.
  143. */
  144. struct cros_ec_platform {
  145. const char *ec_name;
  146. u16 cmd_offset;
  147. };
  148. /*
  149. * struct cros_ec_dev - ChromeOS EC device entry point
  150. *
  151. * @class_dev: Device structure used in sysfs
  152. * @cdev: Character device structure in /dev
  153. * @ec_dev: cros_ec_device structure to talk to the physical device
  154. * @dev: pointer to the platform device
  155. * @cmd_offset: offset to apply for each command.
  156. */
  157. struct cros_ec_dev {
  158. struct device class_dev;
  159. struct cdev cdev;
  160. struct cros_ec_device *ec_dev;
  161. struct device *dev;
  162. u16 cmd_offset;
  163. };
  164. /**
  165. * cros_ec_suspend - Handle a suspend operation for the ChromeOS EC device
  166. *
  167. * This can be called by drivers to handle a suspend event.
  168. *
  169. * ec_dev: Device to suspend
  170. * @return 0 if ok, -ve on error
  171. */
  172. int cros_ec_suspend(struct cros_ec_device *ec_dev);
  173. /**
  174. * cros_ec_resume - Handle a resume operation for the ChromeOS EC device
  175. *
  176. * This can be called by drivers to handle a resume event.
  177. *
  178. * @ec_dev: Device to resume
  179. * @return 0 if ok, -ve on error
  180. */
  181. int cros_ec_resume(struct cros_ec_device *ec_dev);
  182. /**
  183. * cros_ec_prepare_tx - Prepare an outgoing message in the output buffer
  184. *
  185. * This is intended to be used by all ChromeOS EC drivers, but at present
  186. * only SPI uses it. Once LPC uses the same protocol it can start using it.
  187. * I2C could use it now, with a refactor of the existing code.
  188. *
  189. * @ec_dev: Device to register
  190. * @msg: Message to write
  191. */
  192. int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
  193. struct cros_ec_command *msg);
  194. /**
  195. * cros_ec_check_result - Check ec_msg->result
  196. *
  197. * This is used by ChromeOS EC drivers to check the ec_msg->result for
  198. * errors and to warn about them.
  199. *
  200. * @ec_dev: EC device
  201. * @msg: Message to check
  202. */
  203. int cros_ec_check_result(struct cros_ec_device *ec_dev,
  204. struct cros_ec_command *msg);
  205. /**
  206. * cros_ec_cmd_xfer - Send a command to the ChromeOS EC
  207. *
  208. * Call this to send a command to the ChromeOS EC. This should be used
  209. * instead of calling the EC's cmd_xfer() callback directly.
  210. *
  211. * @ec_dev: EC device
  212. * @msg: Message to write
  213. */
  214. int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
  215. struct cros_ec_command *msg);
  216. /**
  217. * cros_ec_cmd_xfer_status - Send a command to the ChromeOS EC
  218. *
  219. * This function is identical to cros_ec_cmd_xfer, except it returns success
  220. * status only if both the command was transmitted successfully and the EC
  221. * replied with success status. It's not necessary to check msg->result when
  222. * using this function.
  223. *
  224. * @ec_dev: EC device
  225. * @msg: Message to write
  226. * @return: Num. of bytes transferred on success, <0 on failure
  227. */
  228. int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
  229. struct cros_ec_command *msg);
  230. /**
  231. * cros_ec_remove - Remove a ChromeOS EC
  232. *
  233. * Call this to deregister a ChromeOS EC, then clean up any private data.
  234. *
  235. * @ec_dev: Device to register
  236. * @return 0 if ok, -ve on error
  237. */
  238. int cros_ec_remove(struct cros_ec_device *ec_dev);
  239. /**
  240. * cros_ec_register - Register a new ChromeOS EC, using the provided info
  241. *
  242. * Before calling this, allocate a pointer to a new device and then fill
  243. * in all the fields up to the --private-- marker.
  244. *
  245. * @ec_dev: Device to register
  246. * @return 0 if ok, -ve on error
  247. */
  248. int cros_ec_register(struct cros_ec_device *ec_dev);
  249. /**
  250. * cros_ec_query_all - Query the protocol version supported by the ChromeOS EC
  251. *
  252. * @ec_dev: Device to register
  253. * @return 0 if ok, -ve on error
  254. */
  255. int cros_ec_query_all(struct cros_ec_device *ec_dev);
  256. /**
  257. * cros_ec_get_next_event - Fetch next event from the ChromeOS EC
  258. *
  259. * @ec_dev: Device to fetch event from
  260. *
  261. * Returns: 0 on success, Linux error number on failure
  262. */
  263. int cros_ec_get_next_event(struct cros_ec_device *ec_dev);
  264. /* sysfs stuff */
  265. extern struct attribute_group cros_ec_attr_group;
  266. extern struct attribute_group cros_ec_lightbar_attr_group;
  267. extern struct attribute_group cros_ec_vbc_attr_group;
  268. #endif /* __LINUX_MFD_CROS_EC_H */