1 /* SPDX-License-Identifier: GPL-2.0 */
3 * ChromeOS Embedded Controller protocol interface.
5 * Copyright (C) 2012 Google, Inc
8 #ifndef __LINUX_CROS_EC_PROTO_H
9 #define __LINUX_CROS_EC_PROTO_H
11 #include <linux/device.h>
12 #include <linux/mutex.h>
13 #include <linux/notifier.h>
15 #include <linux/platform_data/cros_ec_commands.h>
17 #define CROS_EC_DEV_NAME "cros_ec"
18 #define CROS_EC_DEV_FP_NAME "cros_fp"
19 #define CROS_EC_DEV_ISH_NAME "cros_ish"
20 #define CROS_EC_DEV_PD_NAME "cros_pd"
21 #define CROS_EC_DEV_SCP_NAME "cros_scp"
22 #define CROS_EC_DEV_TP_NAME "cros_tp"
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.
28 #define EC_REBOOT_DELAY_MS 50
31 * Max bus-specific overhead incurred by request/responses.
32 * I2C requires 1 additional byte for requests.
33 * I2C requires 2 additional bytes for responses.
34 * SPI requires up to 32 additional bytes for responses.
36 #define EC_PROTO_VERSION_UNKNOWN 0
37 #define EC_MAX_REQUEST_OVERHEAD 1
38 #define EC_MAX_RESPONSE_OVERHEAD 32
41 * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
44 EC_MSG_TX_HEADER_BYTES = 3,
45 EC_MSG_TX_TRAILER_BYTES = 1,
46 EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES +
47 EC_MSG_TX_TRAILER_BYTES,
48 EC_MSG_RX_PROTO_BYTES = 3,
50 /* Max length of messages for proto 2*/
51 EC_PROTO2_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
52 EC_MSG_TX_PROTO_BYTES,
54 EC_MAX_MSG_BYTES = 64 * 1024,
58 * struct cros_ec_command - Information about a ChromeOS EC command.
59 * @version: Command version number (often 0).
60 * @command: Command to send (EC_CMD_...).
61 * @outsize: Outgoing length in bytes.
62 * @insize: Max number of bytes to accept from the EC.
63 * @result: EC's response to the command (separate from communication failure).
64 * @data: Where to put the incoming data from EC and outgoing data to EC.
66 struct cros_ec_command {
76 * struct cros_ec_device - Information about a ChromeOS EC device.
77 * @phys_name: Name of physical comms layer (e.g. 'i2c-4').
78 * @dev: Device pointer for physical comms device
79 * @cros_class: The class structure for this device.
80 * @cmd_readmem: Direct read of the EC memory-mapped region, if supported.
81 * @offset: Is within EC_LPC_ADDR_MEMMAP region.
82 * @bytes: Number of bytes to read. zero means "read a string" (including
83 * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be
84 * read. Caller must ensure that the buffer is large enough for the
85 * result when reading a string.
86 * @max_request: Max size of message requested.
87 * @max_response: Max size of message response.
88 * @max_passthru: Max sice of passthru message.
89 * @proto_version: The protocol version used for this device.
90 * @priv: Private data.
91 * @irq: Interrupt to use.
93 * @din: Input buffer (for data from EC). This buffer will always be
94 * dword-aligned and include enough space for up to 7 word-alignment
95 * bytes also, so we can ensure that the body of the message is always
96 * dword-aligned (64-bit). We use this alignment to keep ARM and x86
97 * happy. Probably word alignment would be OK, there might be a small
98 * performance advantage to using dword.
99 * @dout: Output buffer (for data to 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 * @din_size: Size of din buffer to allocate (zero to use static din).
106 * @dout_size: Size of dout buffer to allocate (zero to use static dout).
107 * @wake_enabled: True if this device can wake the system from sleep.
108 * @suspended: True if this device had been suspended.
109 * @cmd_xfer: Send command to EC and get response.
110 * Returns the number of bytes received if the communication
111 * succeeded, but that doesn't mean the EC was happy with the
112 * command. The caller should check msg.result for the EC's result
114 * @pkt_xfer: Send packet to EC and get response.
115 * @lock: One transaction at a time.
116 * @mkbp_event_supported: 0 if MKBP not supported. Otherwise its value is
117 * the maximum supported version of the MKBP host event
119 * @host_sleep_v1: True if this EC supports the sleep v1 command.
120 * @event_notifier: Interrupt event notifier for transport devices.
121 * @event_data: Raw payload transferred with the MKBP event.
122 * @event_size: Size in bytes of the event data.
123 * @host_event_wake_mask: Mask of host events that cause wake from suspend.
124 * @last_event_time: exact time from the hard irq when we got notified of
126 * @notifier_ready: The notifier_block to let the kernel re-query EC
127 * communication protocol when the EC sends
128 * EC_HOST_EVENT_INTERFACE_READY.
129 * @ec: The platform_device used by the mfd driver to interface with the
131 * @pd: The platform_device used by the mfd driver to interface with the
134 struct cros_ec_device {
135 /* These are used by other drivers that want to talk to the EC */
136 const char *phys_name;
138 struct class *cros_class;
139 int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
140 unsigned int bytes, void *dest);
142 /* These are used to implement the platform-specific interface */
155 int (*cmd_xfer)(struct cros_ec_device *ec,
156 struct cros_ec_command *msg);
157 int (*pkt_xfer)(struct cros_ec_device *ec,
158 struct cros_ec_command *msg);
160 u8 mkbp_event_supported;
162 struct blocking_notifier_head event_notifier;
164 struct ec_response_get_next_event_v1 event_data;
166 u32 host_event_wake_mask;
167 u32 last_resume_result;
168 ktime_t last_event_time;
169 struct notifier_block notifier_ready;
171 /* The platform devices used by the mfd driver */
172 struct platform_device *ec;
173 struct platform_device *pd;
177 * struct cros_ec_platform - ChromeOS EC platform information.
178 * @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
179 * used in /dev/ and sysfs.
180 * @cmd_offset: Offset to apply for each command. Set when
181 * registering a device behind another one.
183 struct cros_ec_platform {
189 * struct cros_ec_dev - ChromeOS EC device entry point.
190 * @class_dev: Device structure used in sysfs.
191 * @ec_dev: cros_ec_device structure to talk to the physical device.
192 * @dev: Pointer to the platform device.
193 * @debug_info: cros_ec_debugfs structure for debugging information.
194 * @has_kb_wake_angle: True if at least 2 accelerometer are connected to the EC.
195 * @cmd_offset: Offset to apply for each command.
196 * @features: Features supported by the EC.
199 struct device class_dev;
200 struct cros_ec_device *ec_dev;
202 struct cros_ec_debugfs *debug_info;
203 bool has_kb_wake_angle;
205 struct ec_response_get_features features;
208 #define to_cros_ec_dev(dev) container_of(dev, struct cros_ec_dev, class_dev)
210 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
211 struct cros_ec_command *msg);
213 int cros_ec_check_result(struct cros_ec_device *ec_dev,
214 struct cros_ec_command *msg);
216 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
217 struct cros_ec_command *msg);
219 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
220 struct cros_ec_command *msg);
222 int cros_ec_query_all(struct cros_ec_device *ec_dev);
224 int cros_ec_get_next_event(struct cros_ec_device *ec_dev,
226 bool *has_more_events);
228 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
230 bool cros_ec_check_features(struct cros_ec_dev *ec, int feature);
232 int cros_ec_get_sensor_count(struct cros_ec_dev *ec);
234 int cros_ec_command(struct cros_ec_device *ec_dev, unsigned int version, int command, void *outdata,
235 int outsize, void *indata, int insize);
238 * cros_ec_get_time_ns() - Return time in ns.
240 * This is the function used to record the time for last_event_time in struct
241 * cros_ec_device during the hard irq.
243 * Return: ktime_t format since boot.
245 static inline ktime_t cros_ec_get_time_ns(void)
247 return ktime_get_boottime_ns();
250 #endif /* __LINUX_CROS_EC_PROTO_H */