cros_ec.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/notifier.h>
  18. #include <linux/mfd/cros_ec_commands.h>
  19. #include <linux/mutex.h>
  20. /*
  21. * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
  22. */
  23. enum {
  24. EC_MSG_TX_HEADER_BYTES = 3,
  25. EC_MSG_TX_TRAILER_BYTES = 1,
  26. EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES +
  27. EC_MSG_TX_TRAILER_BYTES,
  28. EC_MSG_RX_PROTO_BYTES = 3,
  29. /* Max length of messages */
  30. EC_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
  31. EC_MSG_TX_PROTO_BYTES,
  32. };
  33. /*
  34. * @version: Command version number (often 0)
  35. * @command: Command to send (EC_CMD_...)
  36. * @outdata: Outgoing data to EC
  37. * @outsize: Outgoing length in bytes
  38. * @indata: Where to put the incoming data from EC
  39. * @insize: Max number of bytes to accept from EC
  40. * @result: EC's response to the command (separate from communication failure)
  41. */
  42. struct cros_ec_command {
  43. uint32_t version;
  44. uint32_t command;
  45. uint8_t *outdata;
  46. uint32_t outsize;
  47. uint8_t *indata;
  48. uint32_t insize;
  49. uint32_t result;
  50. };
  51. /**
  52. * struct cros_ec_device - Information about a ChromeOS EC device
  53. *
  54. * @ec_name: name of EC device (e.g. 'chromeos-ec')
  55. * @phys_name: name of physical comms layer (e.g. 'i2c-4')
  56. * @dev: Device pointer
  57. * @was_wake_device: true if this device was set to wake the system from
  58. * sleep at the last suspend
  59. *
  60. * @priv: Private data
  61. * @irq: Interrupt to use
  62. * @din: input buffer (for data from EC)
  63. * @dout: output buffer (for data to EC)
  64. * \note
  65. * These two buffers will always be dword-aligned and include enough
  66. * space for up to 7 word-alignment bytes also, so we can ensure that
  67. * the body of the message is always dword-aligned (64-bit).
  68. * We use this alignment to keep ARM and x86 happy. Probably word
  69. * alignment would be OK, there might be a small performance advantage
  70. * to using dword.
  71. * @din_size: size of din buffer to allocate (zero to use static din)
  72. * @dout_size: size of dout buffer to allocate (zero to use static dout)
  73. * @parent: pointer to parent device (e.g. i2c or spi device)
  74. * @wake_enabled: true if this device can wake the system from sleep
  75. * @cmd_xfer: send command to EC and get response
  76. * Returns the number of bytes received if the communication succeeded, but
  77. * that doesn't mean the EC was happy with the command. The caller
  78. * should check msg.result for the EC's result code.
  79. * @lock: one transaction at a time
  80. */
  81. struct cros_ec_device {
  82. /* These are used by other drivers that want to talk to the EC */
  83. const char *ec_name;
  84. const char *phys_name;
  85. struct device *dev;
  86. bool was_wake_device;
  87. struct class *cros_class;
  88. /* These are used to implement the platform-specific interface */
  89. void *priv;
  90. int irq;
  91. uint8_t *din;
  92. uint8_t *dout;
  93. int din_size;
  94. int dout_size;
  95. struct device *parent;
  96. bool wake_enabled;
  97. int (*cmd_xfer)(struct cros_ec_device *ec,
  98. struct cros_ec_command *msg);
  99. struct mutex lock;
  100. };
  101. /**
  102. * cros_ec_suspend - Handle a suspend operation for the ChromeOS EC device
  103. *
  104. * This can be called by drivers to handle a suspend event.
  105. *
  106. * ec_dev: Device to suspend
  107. * @return 0 if ok, -ve on error
  108. */
  109. int cros_ec_suspend(struct cros_ec_device *ec_dev);
  110. /**
  111. * cros_ec_resume - Handle a resume operation for the ChromeOS EC device
  112. *
  113. * This can be called by drivers to handle a resume event.
  114. *
  115. * @ec_dev: Device to resume
  116. * @return 0 if ok, -ve on error
  117. */
  118. int cros_ec_resume(struct cros_ec_device *ec_dev);
  119. /**
  120. * cros_ec_prepare_tx - Prepare an outgoing message in the output buffer
  121. *
  122. * This is intended to be used by all ChromeOS EC drivers, but at present
  123. * only SPI uses it. Once LPC uses the same protocol it can start using it.
  124. * I2C could use it now, with a refactor of the existing code.
  125. *
  126. * @ec_dev: Device to register
  127. * @msg: Message to write
  128. */
  129. int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
  130. struct cros_ec_command *msg);
  131. /**
  132. * cros_ec_check_result - Check ec_msg->result
  133. *
  134. * This is used by ChromeOS EC drivers to check the ec_msg->result for
  135. * errors and to warn about them.
  136. *
  137. * @ec_dev: EC device
  138. * @msg: Message to check
  139. */
  140. int cros_ec_check_result(struct cros_ec_device *ec_dev,
  141. struct cros_ec_command *msg);
  142. /**
  143. * cros_ec_cmd_xfer - Send a command to the ChromeOS EC
  144. *
  145. * Call this to send a command to the ChromeOS EC. This should be used
  146. * instead of calling the EC's cmd_xfer() callback directly.
  147. *
  148. * @ec_dev: EC device
  149. * @msg: Message to write
  150. */
  151. int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
  152. struct cros_ec_command *msg);
  153. /**
  154. * cros_ec_remove - Remove a ChromeOS EC
  155. *
  156. * Call this to deregister a ChromeOS EC, then clean up any private data.
  157. *
  158. * @ec_dev: Device to register
  159. * @return 0 if ok, -ve on error
  160. */
  161. int cros_ec_remove(struct cros_ec_device *ec_dev);
  162. /**
  163. * cros_ec_register - Register a new ChromeOS EC, using the provided info
  164. *
  165. * Before calling this, allocate a pointer to a new device and then fill
  166. * in all the fields up to the --private-- marker.
  167. *
  168. * @ec_dev: Device to register
  169. * @return 0 if ok, -ve on error
  170. */
  171. int cros_ec_register(struct cros_ec_device *ec_dev);
  172. #endif /* __LINUX_MFD_CROS_EC_H */