1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * ChromeOS EC multi-function device
5 * Copyright (C) 2012 Google, Inc
8 #ifndef __LINUX_MFD_CROS_EC_H
9 #define __LINUX_MFD_CROS_EC_H
11 #include <linux/cdev.h>
12 #include <linux/device.h>
13 #include <linux/notifier.h>
14 #include <linux/mfd/cros_ec_commands.h>
15 #include <linux/mutex.h>
17 #define CROS_EC_DEV_NAME "cros_ec"
18 #define CROS_EC_DEV_FP_NAME "cros_fp"
19 #define CROS_EC_DEV_PD_NAME "cros_pd"
20 #define CROS_EC_DEV_TP_NAME "cros_tp"
21 #define CROS_EC_DEV_ISH_NAME "cros_ish"
24 * The EC is unresponsive for a time after a reboot command. Add a
25 * simple delay to make sure that the bus stays locked.
27 #define EC_REBOOT_DELAY_MS 50
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.
35 #define EC_PROTO_VERSION_UNKNOWN 0
36 #define EC_MAX_REQUEST_OVERHEAD 1
37 #define EC_MAX_RESPONSE_OVERHEAD 32
40 * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
43 EC_MSG_TX_HEADER_BYTES = 3,
44 EC_MSG_TX_TRAILER_BYTES = 1,
45 EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES +
46 EC_MSG_TX_TRAILER_BYTES,
47 EC_MSG_RX_PROTO_BYTES = 3,
49 /* Max length of messages for proto 2*/
50 EC_PROTO2_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
51 EC_MSG_TX_PROTO_BYTES,
53 EC_MAX_MSG_BYTES = 64 * 1024,
57 * struct cros_ec_command - Information about a ChromeOS EC command.
58 * @version: Command version number (often 0).
59 * @command: Command to send (EC_CMD_...).
60 * @outsize: Outgoing length in bytes.
61 * @insize: Max number of bytes to accept from the EC.
62 * @result: EC's response to the command (separate from communication failure).
63 * @data: Where to put the incoming data from EC and outgoing data to EC.
65 struct cros_ec_command {
75 * struct cros_ec_device - Information about a ChromeOS EC device.
76 * @phys_name: Name of physical comms layer (e.g. 'i2c-4').
77 * @dev: Device pointer for physical comms device
78 * @was_wake_device: True if this device was set to wake the system from
79 * sleep at the last suspend.
80 * @cros_class: The class structure for this device.
81 * @cmd_readmem: Direct read of the EC memory-mapped region, if supported.
82 * @offset: Is within EC_LPC_ADDR_MEMMAP region.
83 * @bytes: Number of bytes to read. zero means "read a string" (including
84 * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be
85 * read. Caller must ensure that the buffer is large enough for the
86 * result when reading a string.
87 * @max_request: Max size of message requested.
88 * @max_response: Max size of message response.
89 * @max_passthru: Max sice of passthru message.
90 * @proto_version: The protocol version used for this device.
91 * @priv: Private data.
92 * @irq: Interrupt to use.
94 * @din: Input buffer (for data from EC). This buffer will always be
95 * dword-aligned and include enough space for up to 7 word-alignment
96 * bytes also, so we can ensure that the body of the message is always
97 * dword-aligned (64-bit). We use this alignment to keep ARM and x86
98 * happy. Probably word alignment would be OK, there might be a small
99 * performance advantage to using dword.
100 * @dout: Output buffer (for data to EC). This buffer will always be
101 * dword-aligned and include enough space for up to 7 word-alignment
102 * bytes also, so we can ensure that the body of the message is always
103 * dword-aligned (64-bit). We use this alignment to keep ARM and x86
104 * happy. Probably word alignment would be OK, there might be a small
105 * performance advantage to using dword.
106 * @din_size: Size of din buffer to allocate (zero to use static din).
107 * @dout_size: Size of dout buffer to allocate (zero to use static dout).
108 * @wake_enabled: True if this device can wake the system from sleep.
109 * @suspended: True if this device had been suspended.
110 * @cmd_xfer: Send command to EC and get response.
111 * Returns the number of bytes received if the communication
112 * succeeded, but that doesn't mean the EC was happy with the
113 * command. The caller should check msg.result for the EC's result
115 * @pkt_xfer: Send packet to EC and get response.
116 * @lock: One transaction at a time.
117 * @mkbp_event_supported: True if this EC supports the MKBP event protocol.
118 * @host_sleep_v1: True if this EC supports the sleep v1 command.
119 * @event_notifier: Interrupt event notifier for transport devices.
120 * @event_data: Raw payload transferred with the MKBP event.
121 * @event_size: Size in bytes of the event data.
122 * @host_event_wake_mask: Mask of host events that cause wake from suspend.
124 struct cros_ec_device {
125 /* These are used by other drivers that want to talk to the EC */
126 const char *phys_name;
128 bool was_wake_device;
129 struct class *cros_class;
130 int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
131 unsigned int bytes, void *dest);
133 /* These are used to implement the platform-specific interface */
146 int (*cmd_xfer)(struct cros_ec_device *ec,
147 struct cros_ec_command *msg);
148 int (*pkt_xfer)(struct cros_ec_device *ec,
149 struct cros_ec_command *msg);
151 bool mkbp_event_supported;
153 struct blocking_notifier_head event_notifier;
155 struct ec_response_get_next_event_v1 event_data;
157 u32 host_event_wake_mask;
158 u32 last_resume_result;
162 * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
163 * @sensor_num: Id of the sensor, as reported by the EC.
165 struct cros_ec_sensor_platform {
170 * struct cros_ec_platform - ChromeOS EC platform information.
171 * @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
172 * used in /dev/ and sysfs.
173 * @cmd_offset: Offset to apply for each command. Set when
174 * registering a device behind another one.
176 struct cros_ec_platform {
181 struct cros_ec_debugfs;
184 * struct cros_ec_dev - ChromeOS EC device entry point.
185 * @class_dev: Device structure used in sysfs.
186 * @cdev: Character device structure in /dev.
187 * @ec_dev: cros_ec_device structure to talk to the physical device.
188 * @dev: Pointer to the platform device.
189 * @debug_info: cros_ec_debugfs structure for debugging information.
190 * @has_kb_wake_angle: True if at least 2 accelerometer are connected to the EC.
191 * @cmd_offset: Offset to apply for each command.
192 * @features: Features supported by the EC.
195 struct device class_dev;
197 struct cros_ec_device *ec_dev;
199 struct cros_ec_debugfs *debug_info;
200 bool has_kb_wake_angle;
205 #define to_cros_ec_dev(dev) container_of(dev, struct cros_ec_dev, class_dev)
208 * cros_ec_suspend() - Handle a suspend operation for the ChromeOS EC device.
209 * @ec_dev: Device to suspend.
211 * This can be called by drivers to handle a suspend event.
213 * Return: 0 on success or negative error code.
215 int cros_ec_suspend(struct cros_ec_device *ec_dev);
218 * cros_ec_resume() - Handle a resume operation for the ChromeOS EC device.
219 * @ec_dev: Device to resume.
221 * This can be called by drivers to handle a resume event.
223 * Return: 0 on success or negative error code.
225 int cros_ec_resume(struct cros_ec_device *ec_dev);
228 * cros_ec_prepare_tx() - Prepare an outgoing message in the output buffer.
229 * @ec_dev: Device to register.
230 * @msg: Message to write.
232 * This is intended to be used by all ChromeOS EC drivers, but at present
233 * only SPI uses it. Once LPC uses the same protocol it can start using it.
234 * I2C could use it now, with a refactor of the existing code.
236 * Return: 0 on success or negative error code.
238 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
239 struct cros_ec_command *msg);
242 * cros_ec_check_result() - Check ec_msg->result.
243 * @ec_dev: EC device.
244 * @msg: Message to check.
246 * This is used by ChromeOS EC drivers to check the ec_msg->result for
247 * errors and to warn about them.
249 * Return: 0 on success or negative error code.
251 int cros_ec_check_result(struct cros_ec_device *ec_dev,
252 struct cros_ec_command *msg);
255 * cros_ec_cmd_xfer() - Send a command to the ChromeOS EC.
256 * @ec_dev: EC device.
257 * @msg: Message to write.
259 * Call this to send a command to the ChromeOS EC. This should be used
260 * instead of calling the EC's cmd_xfer() callback directly.
262 * Return: 0 on success or negative error code.
264 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
265 struct cros_ec_command *msg);
268 * cros_ec_cmd_xfer_status() - Send a command to the ChromeOS EC.
269 * @ec_dev: EC device.
270 * @msg: Message to write.
272 * This function is identical to cros_ec_cmd_xfer, except it returns success
273 * status only if both the command was transmitted successfully and the EC
274 * replied with success status. It's not necessary to check msg->result when
275 * using this function.
277 * Return: The number of bytes transferred on success or negative error code.
279 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
280 struct cros_ec_command *msg);
283 * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
284 * @ec_dev: Device to register.
286 * Before calling this, allocate a pointer to a new device and then fill
287 * in all the fields up to the --private-- marker.
289 * Return: 0 on success or negative error code.
291 int cros_ec_register(struct cros_ec_device *ec_dev);
294 * cros_ec_query_all() - Query the protocol version supported by the
296 * @ec_dev: Device to register.
298 * Return: 0 on success or negative error code.
300 int cros_ec_query_all(struct cros_ec_device *ec_dev);
303 * cros_ec_get_next_event() - Fetch next event from the ChromeOS EC.
304 * @ec_dev: Device to fetch event from.
305 * @wake_event: Pointer to a bool set to true upon return if the event might be
306 * treated as a wake event. Ignored if null.
308 * Return: negative error code on errors; 0 for no data; or else number of
309 * bytes received (i.e., an event was retrieved successfully). Event types are
310 * written out to @ec_dev->event_data.event_type on success.
312 int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event);
315 * cros_ec_get_host_event() - Return a mask of event set by the ChromeOS EC.
316 * @ec_dev: Device to fetch event from.
318 * When MKBP is supported, when the EC raises an interrupt, we collect the
319 * events raised and call the functions in the ec notifier. This function
320 * is a helper to know which events are raised.
322 * Return: 0 on error or non-zero bitmask of one or more EC_HOST_EVENT_*.
324 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
326 #endif /* __LINUX_MFD_CROS_EC_H */