cros_ec.h 10 KB

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