Commit | Line | Data |
---|---|---|
4ab6174e SG |
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 | ||
16 | #ifndef __LINUX_MFD_CROS_EC_H | |
17 | #define __LINUX_MFD_CROS_EC_H | |
18 | ||
05c11ac4 | 19 | #include <linux/cdev.h> |
57b33ff0 | 20 | #include <linux/device.h> |
7e6cb5b4 | 21 | #include <linux/notifier.h> |
4ab6174e | 22 | #include <linux/mfd/cros_ec_commands.h> |
7e6cb5b4 | 23 | #include <linux/mutex.h> |
4ab6174e | 24 | |
57b33ff0 GG |
25 | #define CROS_EC_DEV_NAME "cros_ec" |
26 | #define CROS_EC_DEV_PD_NAME "cros_pd" | |
27 | ||
d3654070 SB |
28 | /* |
29 | * The EC is unresponsive for a time after a reboot command. Add a | |
30 | * simple delay to make sure that the bus stays locked. | |
31 | */ | |
32 | #define EC_REBOOT_DELAY_MS 50 | |
33 | ||
2c7589af SB |
34 | /* |
35 | * Max bus-specific overhead incurred by request/responses. | |
36 | * I2C requires 1 additional byte for requests. | |
37 | * I2C requires 2 additional bytes for responses. | |
38 | * */ | |
39 | #define EC_PROTO_VERSION_UNKNOWN 0 | |
40 | #define EC_MAX_REQUEST_OVERHEAD 1 | |
41 | #define EC_MAX_RESPONSE_OVERHEAD 2 | |
42 | ||
4ab6174e SG |
43 | /* |
44 | * Command interface between EC and AP, for LPC, I2C and SPI interfaces. | |
45 | */ | |
46 | enum { | |
47 | EC_MSG_TX_HEADER_BYTES = 3, | |
48 | EC_MSG_TX_TRAILER_BYTES = 1, | |
49 | EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES + | |
50 | EC_MSG_TX_TRAILER_BYTES, | |
51 | EC_MSG_RX_PROTO_BYTES = 3, | |
52 | ||
5d749d0b GG |
53 | /* Max length of messages for proto 2*/ |
54 | EC_PROTO2_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE + | |
5271db29 | 55 | EC_MSG_TX_PROTO_BYTES, |
5d749d0b GG |
56 | |
57 | EC_MAX_MSG_BYTES = 64 * 1024, | |
4ab6174e SG |
58 | }; |
59 | ||
5d4773e2 | 60 | /* |
4ab6174e | 61 | * @version: Command version number (often 0) |
5d4773e2 | 62 | * @command: Command to send (EC_CMD_...) |
5d4773e2 | 63 | * @outsize: Outgoing length in bytes |
12ebc8a5 | 64 | * @insize: Max number of bytes to accept from EC |
5d4773e2 | 65 | * @result: EC's response to the command (separate from communication failure) |
a8411784 | 66 | * @data: Where to put the incoming data from EC and outgoing data to EC |
4ab6174e | 67 | */ |
5d4773e2 BR |
68 | struct cros_ec_command { |
69 | uint32_t version; | |
70 | uint32_t command; | |
5d4773e2 | 71 | uint32_t outsize; |
5d4773e2 BR |
72 | uint32_t insize; |
73 | uint32_t result; | |
a8411784 | 74 | uint8_t data[0]; |
4ab6174e SG |
75 | }; |
76 | ||
77 | /** | |
78 | * struct cros_ec_device - Information about a ChromeOS EC device | |
79 | * | |
7e6cb5b4 | 80 | * @phys_name: name of physical comms layer (e.g. 'i2c-4') |
05c11ac4 | 81 | * @dev: Device pointer for physical comms device |
7e6cb5b4 BR |
82 | * @was_wake_device: true if this device was set to wake the system from |
83 | * sleep at the last suspend | |
05c11ac4 JMC |
84 | * @cmd_readmem: direct read of the EC memory-mapped region, if supported |
85 | * @offset is within EC_LPC_ADDR_MEMMAP region. | |
86 | * @bytes: number of bytes to read. zero means "read a string" (including | |
87 | * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be read. | |
88 | * Caller must ensure that the buffer is large enough for the result when | |
89 | * reading a string. | |
7e6cb5b4 | 90 | * |
4ab6174e SG |
91 | * @priv: Private data |
92 | * @irq: Interrupt to use | |
57b33ff0 | 93 | * @id: Device id |
7e6cb5b4 BR |
94 | * @din: input buffer (for data from EC) |
95 | * @dout: output buffer (for data to EC) | |
4ab6174e SG |
96 | * \note |
97 | * These two buffers will always be dword-aligned and include enough | |
98 | * space for up to 7 word-alignment bytes also, so we can ensure that | |
99 | * the body of the message is always dword-aligned (64-bit). | |
4ab6174e SG |
100 | * We use this alignment to keep ARM and x86 happy. Probably word |
101 | * alignment would be OK, there might be a small performance advantage | |
102 | * to using dword. | |
2ce701ae BR |
103 | * @din_size: size of din buffer to allocate (zero to use static din) |
104 | * @dout_size: size of dout buffer to allocate (zero to use static dout) | |
4ab6174e | 105 | * @wake_enabled: true if this device can wake the system from sleep |
a6551a76 AB |
106 | * @cmd_xfer: send command to EC and get response |
107 | * Returns the number of bytes received if the communication succeeded, but | |
108 | * that doesn't mean the EC was happy with the command. The caller | |
109 | * should check msg.result for the EC's result code. | |
2c7589af | 110 | * @pkt_xfer: send packet to EC and get response |
7e6cb5b4 | 111 | * @lock: one transaction at a time |
6f1d912b VY |
112 | * @mkbp_event_supported: true if this EC supports the MKBP event protocol. |
113 | * @event_notifier: interrupt event notifier for transport devices. | |
114 | * @event_data: raw payload transferred with the MKBP event. | |
115 | * @event_size: size in bytes of the event data. | |
4ab6174e SG |
116 | */ |
117 | struct cros_ec_device { | |
7e6cb5b4 BR |
118 | |
119 | /* These are used by other drivers that want to talk to the EC */ | |
7e6cb5b4 BR |
120 | const char *phys_name; |
121 | struct device *dev; | |
122 | bool was_wake_device; | |
123 | struct class *cros_class; | |
05c11ac4 JMC |
124 | int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset, |
125 | unsigned int bytes, void *dest); | |
7e6cb5b4 BR |
126 | |
127 | /* These are used to implement the platform-specific interface */ | |
2c7589af SB |
128 | u16 max_request; |
129 | u16 max_response; | |
130 | u16 max_passthru; | |
131 | u16 proto_version; | |
4ab6174e SG |
132 | void *priv; |
133 | int irq; | |
2c7589af SB |
134 | u8 *din; |
135 | u8 *dout; | |
4ab6174e SG |
136 | int din_size; |
137 | int dout_size; | |
4ab6174e | 138 | bool wake_enabled; |
a6551a76 AB |
139 | int (*cmd_xfer)(struct cros_ec_device *ec, |
140 | struct cros_ec_command *msg); | |
2c7589af SB |
141 | int (*pkt_xfer)(struct cros_ec_device *ec, |
142 | struct cros_ec_command *msg); | |
7e6cb5b4 | 143 | struct mutex lock; |
6f1d912b VY |
144 | bool mkbp_event_supported; |
145 | struct blocking_notifier_head event_notifier; | |
146 | ||
147 | struct ec_response_get_next_event event_data; | |
148 | int event_size; | |
4ab6174e SG |
149 | }; |
150 | ||
57b33ff0 GG |
151 | /* struct cros_ec_platform - ChromeOS EC platform information |
152 | * | |
153 | * @ec_name: name of EC device (e.g. 'cros-ec', 'cros-pd', ...) | |
154 | * used in /dev/ and sysfs. | |
155 | * @cmd_offset: offset to apply for each command. Set when | |
156 | * registering a devicde behind another one. | |
157 | */ | |
158 | struct cros_ec_platform { | |
159 | const char *ec_name; | |
160 | u16 cmd_offset; | |
161 | }; | |
162 | ||
163 | /* | |
164 | * struct cros_ec_dev - ChromeOS EC device entry point | |
165 | * | |
166 | * @class_dev: Device structure used in sysfs | |
167 | * @cdev: Character device structure in /dev | |
168 | * @ec_dev: cros_ec_device structure to talk to the physical device | |
169 | * @dev: pointer to the platform device | |
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 | u16 cmd_offset; | |
178 | }; | |
179 | ||
4ab6174e SG |
180 | /** |
181 | * cros_ec_suspend - Handle a suspend operation for the ChromeOS EC device | |
182 | * | |
183 | * This can be called by drivers to handle a suspend event. | |
184 | * | |
185 | * ec_dev: Device to suspend | |
186 | * @return 0 if ok, -ve on error | |
187 | */ | |
188 | int cros_ec_suspend(struct cros_ec_device *ec_dev); | |
189 | ||
190 | /** | |
191 | * cros_ec_resume - Handle a resume operation for the ChromeOS EC device | |
192 | * | |
193 | * This can be called by drivers to handle a resume event. | |
194 | * | |
195 | * @ec_dev: Device to resume | |
196 | * @return 0 if ok, -ve on error | |
197 | */ | |
198 | int cros_ec_resume(struct cros_ec_device *ec_dev); | |
199 | ||
200 | /** | |
201 | * cros_ec_prepare_tx - Prepare an outgoing message in the output buffer | |
202 | * | |
203 | * This is intended to be used by all ChromeOS EC drivers, but at present | |
204 | * only SPI uses it. Once LPC uses the same protocol it can start using it. | |
205 | * I2C could use it now, with a refactor of the existing code. | |
206 | * | |
207 | * @ec_dev: Device to register | |
208 | * @msg: Message to write | |
209 | */ | |
210 | int cros_ec_prepare_tx(struct cros_ec_device *ec_dev, | |
5d4773e2 | 211 | struct cros_ec_command *msg); |
4ab6174e | 212 | |
6db07b63 BR |
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 | ||
a6551a76 AB |
225 | /** |
226 | * cros_ec_cmd_xfer - Send a command to the ChromeOS EC | |
227 | * | |
228 | * Call this to send a command to the ChromeOS EC. This should be used | |
229 | * instead of calling the EC's cmd_xfer() callback directly. | |
230 | * | |
231 | * @ec_dev: EC device | |
232 | * @msg: Message to write | |
233 | */ | |
234 | int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev, | |
235 | struct cros_ec_command *msg); | |
9798ac6d TV |
236 | |
237 | /** | |
238 | * cros_ec_cmd_xfer_status - Send a command to the ChromeOS EC | |
239 | * | |
240 | * This function is identical to cros_ec_cmd_xfer, except it returns success | |
241 | * status only if both the command was transmitted successfully and the EC | |
242 | * replied with success status. It's not necessary to check msg->result when | |
243 | * using this function. | |
244 | * | |
245 | * @ec_dev: EC device | |
246 | * @msg: Message to write | |
247 | * @return: Num. of bytes transferred on success, <0 on failure | |
248 | */ | |
249 | int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev, | |
250 | struct cros_ec_command *msg); | |
a6551a76 | 251 | |
4ab6174e SG |
252 | /** |
253 | * cros_ec_remove - Remove a ChromeOS EC | |
254 | * | |
ee98662e | 255 | * Call this to deregister a ChromeOS EC, then clean up any private data. |
4ab6174e SG |
256 | * |
257 | * @ec_dev: Device to register | |
258 | * @return 0 if ok, -ve on error | |
259 | */ | |
260 | int cros_ec_remove(struct cros_ec_device *ec_dev); | |
261 | ||
262 | /** | |
263 | * cros_ec_register - Register a new ChromeOS EC, using the provided info | |
264 | * | |
265 | * Before calling this, allocate a pointer to a new device and then fill | |
266 | * in all the fields up to the --private-- marker. | |
267 | * | |
268 | * @ec_dev: Device to register | |
269 | * @return 0 if ok, -ve on error | |
270 | */ | |
271 | int cros_ec_register(struct cros_ec_device *ec_dev); | |
272 | ||
2c7589af | 273 | /** |
f4bcf5a2 | 274 | * cros_ec_query_all - Query the protocol version supported by the ChromeOS EC |
2c7589af SB |
275 | * |
276 | * @ec_dev: Device to register | |
277 | * @return 0 if ok, -ve on error | |
278 | */ | |
279 | int cros_ec_query_all(struct cros_ec_device *ec_dev); | |
280 | ||
6f1d912b VY |
281 | /** |
282 | * cros_ec_get_next_event - Fetch next event from the ChromeOS EC | |
283 | * | |
284 | * @ec_dev: Device to fetch event from | |
285 | * | |
286 | * Returns: 0 on success, Linux error number on failure | |
287 | */ | |
288 | int cros_ec_get_next_event(struct cros_ec_device *ec_dev); | |
289 | ||
57b33ff0 GG |
290 | /* sysfs stuff */ |
291 | extern struct attribute_group cros_ec_attr_group; | |
292 | extern struct attribute_group cros_ec_lightbar_attr_group; | |
18800fc7 | 293 | extern struct attribute_group cros_ec_vbc_attr_group; |
57b33ff0 | 294 | |
4ab6174e | 295 | #endif /* __LINUX_MFD_CROS_EC_H */ |