2 * ChromeOS EC multi-function device
4 * Copyright (C) 2012 Google, Inc
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.
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.
16 #ifndef __LINUX_MFD_CROS_EC_H
17 #define __LINUX_MFD_CROS_EC_H
19 #include <linux/cdev.h>
20 #include <linux/device.h>
21 #include <linux/notifier.h>
22 #include <linux/mfd/cros_ec_commands.h>
23 #include <linux/mutex.h>
25 #define CROS_EC_DEV_NAME "cros_ec"
26 #define CROS_EC_DEV_PD_NAME "cros_pd"
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.
32 #define EC_REBOOT_DELAY_MS 50
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 * SPI requires up to 32 additional bytes for responses.
40 #define EC_PROTO_VERSION_UNKNOWN 0
41 #define EC_MAX_REQUEST_OVERHEAD 1
42 #define EC_MAX_RESPONSE_OVERHEAD 32
45 * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
48 EC_MSG_TX_HEADER_BYTES = 3,
49 EC_MSG_TX_TRAILER_BYTES = 1,
50 EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES +
51 EC_MSG_TX_TRAILER_BYTES,
52 EC_MSG_RX_PROTO_BYTES = 3,
54 /* Max length of messages for proto 2*/
55 EC_PROTO2_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
56 EC_MSG_TX_PROTO_BYTES,
58 EC_MAX_MSG_BYTES = 64 * 1024,
62 * struct cros_ec_command - Information about a ChromeOS EC command.
63 * @version: Command version number (often 0).
64 * @command: Command to send (EC_CMD_...).
65 * @outsize: Outgoing length in bytes.
66 * @insize: Max number of bytes to accept from the EC.
67 * @result: EC's response to the command (separate from communication failure).
68 * @data: Where to put the incoming data from EC and outgoing data to EC.
70 struct cros_ec_command {
80 * struct cros_ec_device - Information about a ChromeOS EC device.
81 * @phys_name: Name of physical comms layer (e.g. 'i2c-4').
82 * @dev: Device pointer for physical comms device
83 * @was_wake_device: True if this device was set to wake the system from
84 * sleep at the last suspend.
85 * @cros_class: The class structure for this device.
86 * @cmd_readmem: Direct read of the EC memory-mapped region, if supported.
87 * @offset: Is within EC_LPC_ADDR_MEMMAP region.
88 * @bytes: Number of bytes to read. zero means "read a string" (including
89 * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be
90 * read. Caller must ensure that the buffer is large enough for the
91 * result when reading a string.
92 * @max_request: Max size of message requested.
93 * @max_response: Max size of message response.
94 * @max_passthru: Max sice of passthru message.
95 * @proto_version: The protocol version used for this device.
96 * @priv: Private data.
97 * @irq: Interrupt to use.
99 * @din: Input buffer (for data from EC). This buffer will always be
100 * dword-aligned and include enough space for up to 7 word-alignment
101 * bytes also, so we can ensure that the body of the message is always
102 * dword-aligned (64-bit). We use this alignment to keep ARM and x86
103 * happy. Probably word alignment would be OK, there might be a small
104 * performance advantage to using dword.
105 * @dout: Output buffer (for data to EC). This buffer will always be
106 * dword-aligned and include enough space for up to 7 word-alignment
107 * bytes also, so we can ensure that the body of the message is always
108 * dword-aligned (64-bit). We use this alignment to keep ARM and x86
109 * happy. Probably word alignment would be OK, there might be a small
110 * performance advantage to using dword.
111 * @din_size: Size of din buffer to allocate (zero to use static din).
112 * @dout_size: Size of dout buffer to allocate (zero to use static dout).
113 * @wake_enabled: True if this device can wake the system from sleep.
114 * @suspended: True if this device had been suspended.
115 * @cmd_xfer: Send command to EC and get response.
116 * Returns the number of bytes received if the communication
117 * succeeded, but that doesn't mean the EC was happy with the
118 * command. The caller should check msg.result for the EC's result
120 * @pkt_xfer: Send packet to EC and get response.
121 * @lock: One transaction at a time.
122 * @mkbp_event_supported: True if this EC supports the MKBP event protocol.
123 * @event_notifier: Interrupt event notifier for transport devices.
124 * @event_data: Raw payload transferred with the MKBP event.
125 * @event_size: Size in bytes of the event data.
126 * @host_event_wake_mask: Mask of host events that cause wake from suspend.
128 struct cros_ec_device {
129 /* These are used by other drivers that want to talk to the EC */
130 const char *phys_name;
132 bool was_wake_device;
133 struct class *cros_class;
134 int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
135 unsigned int bytes, void *dest);
137 /* These are used to implement the platform-specific interface */
150 int (*cmd_xfer)(struct cros_ec_device *ec,
151 struct cros_ec_command *msg);
152 int (*pkt_xfer)(struct cros_ec_device *ec,
153 struct cros_ec_command *msg);
155 bool mkbp_event_supported;
156 struct blocking_notifier_head event_notifier;
158 struct ec_response_get_next_event_v1 event_data;
160 u32 host_event_wake_mask;
164 * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
165 * @sensor_num: Id of the sensor, as reported by the EC.
167 struct cros_ec_sensor_platform {
172 * struct cros_ec_platform - ChromeOS EC platform information.
173 * @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
174 * used in /dev/ and sysfs.
175 * @cmd_offset: Offset to apply for each command. Set when
176 * registering a device behind another one.
178 struct cros_ec_platform {
183 struct cros_ec_debugfs;
186 * struct cros_ec_dev - ChromeOS EC device entry point.
187 * @class_dev: Device structure used in sysfs.
188 * @cdev: Character device structure in /dev.
189 * @ec_dev: cros_ec_device structure to talk to the physical device.
190 * @dev: Pointer to the platform device.
191 * @debug_info: cros_ec_debugfs structure for debugging information.
192 * @has_kb_wake_angle: True if at least 2 accelerometer are connected to the EC.
193 * @cmd_offset: Offset to apply for each command.
194 * @features: Features supported by the EC.
197 struct device class_dev;
199 struct cros_ec_device *ec_dev;
201 struct cros_ec_debugfs *debug_info;
202 bool has_kb_wake_angle;
207 #define to_cros_ec_dev(dev) container_of(dev, struct cros_ec_dev, class_dev)
210 * cros_ec_suspend() - Handle a suspend operation for the ChromeOS EC device.
211 * @ec_dev: Device to suspend.
213 * This can be called by drivers to handle a suspend event.
215 * Return: 0 on success or negative error code.
217 int cros_ec_suspend(struct cros_ec_device *ec_dev);
220 * cros_ec_resume() - Handle a resume operation for the ChromeOS EC device.
221 * @ec_dev: Device to resume.
223 * This can be called by drivers to handle a resume event.
225 * Return: 0 on success or negative error code.
227 int cros_ec_resume(struct cros_ec_device *ec_dev);
230 * cros_ec_prepare_tx() - Prepare an outgoing message in the output buffer.
231 * @ec_dev: Device to register.
232 * @msg: Message to write.
234 * This is intended to be used by all ChromeOS EC drivers, but at present
235 * only SPI uses it. Once LPC uses the same protocol it can start using it.
236 * I2C could use it now, with a refactor of the existing code.
238 * Return: 0 on success or negative error code.
240 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
241 struct cros_ec_command *msg);
244 * cros_ec_check_result() - Check ec_msg->result.
245 * @ec_dev: EC device.
246 * @msg: Message to check.
248 * This is used by ChromeOS EC drivers to check the ec_msg->result for
249 * errors and to warn about them.
251 * Return: 0 on success or negative error code.
253 int cros_ec_check_result(struct cros_ec_device *ec_dev,
254 struct cros_ec_command *msg);
257 * cros_ec_cmd_xfer() - Send a command to the ChromeOS EC.
258 * @ec_dev: EC device.
259 * @msg: Message to write.
261 * Call this to send a command to the ChromeOS EC. This should be used
262 * instead of calling the EC's cmd_xfer() callback directly.
264 * Return: 0 on success or negative error code.
266 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
267 struct cros_ec_command *msg);
270 * cros_ec_cmd_xfer_status() - Send a command to the ChromeOS EC.
271 * @ec_dev: EC device.
272 * @msg: Message to write.
274 * This function is identical to cros_ec_cmd_xfer, except it returns success
275 * status only if both the command was transmitted successfully and the EC
276 * replied with success status. It's not necessary to check msg->result when
277 * using this function.
279 * Return: The number of bytes transferred on success or negative error code.
281 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
282 struct cros_ec_command *msg);
285 * cros_ec_remove() - Remove a ChromeOS EC.
286 * @ec_dev: Device to register.
288 * Call this to deregister a ChromeOS EC, then clean up any private data.
290 * Return: 0 on success or negative error code.
292 int cros_ec_remove(struct cros_ec_device *ec_dev);
295 * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
296 * @ec_dev: Device to register.
298 * Before calling this, allocate a pointer to a new device and then fill
299 * in all the fields up to the --private-- marker.
301 * Return: 0 on success or negative error code.
303 int cros_ec_register(struct cros_ec_device *ec_dev);
306 * cros_ec_query_all() - Query the protocol version supported by the
308 * @ec_dev: Device to register.
310 * Return: 0 on success or negative error code.
312 int cros_ec_query_all(struct cros_ec_device *ec_dev);
315 * cros_ec_get_next_event() - Fetch next event from the ChromeOS EC.
316 * @ec_dev: Device to fetch event from.
317 * @wake_event: Pointer to a bool set to true upon return if the event might be
318 * treated as a wake event. Ignored if null.
320 * Return: negative error code on errors; 0 for no data; or else number of
321 * bytes received (i.e., an event was retrieved successfully). Event types are
322 * written out to @ec_dev->event_data.event_type on success.
324 int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event);
327 * cros_ec_get_host_event() - Return a mask of event set by the ChromeOS EC.
328 * @ec_dev: Device to fetch event from.
330 * When MKBP is supported, when the EC raises an interrupt, we collect the
331 * events raised and call the functions in the ec notifier. This function
332 * is a helper to know which events are raised.
334 * Return: 0 on error or non-zero bitmask of one or more EC_HOST_EVENT_*.
336 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
339 extern struct attribute_group cros_ec_attr_group;
340 extern struct attribute_group cros_ec_lightbar_attr_group;
341 extern struct attribute_group cros_ec_vbc_attr_group;
344 int cros_ec_debugfs_init(struct cros_ec_dev *ec);
345 void cros_ec_debugfs_remove(struct cros_ec_dev *ec);
346 void cros_ec_debugfs_suspend(struct cros_ec_dev *ec);
347 void cros_ec_debugfs_resume(struct cros_ec_dev *ec);
349 #endif /* __LINUX_MFD_CROS_EC_H */