xref: /linux/include/linux/platform_data/cros_ec_proto.h (revision ec2e0fb07d789976c601bec19ecced7a501c3705)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * ChromeOS Embedded Controller protocol interface.
4  *
5  * Copyright (C) 2012 Google, Inc
6  */
7 
8 #ifndef __LINUX_CROS_EC_PROTO_H
9 #define __LINUX_CROS_EC_PROTO_H
10 
11 #include <linux/device.h>
12 #include <linux/lockdep_types.h>
13 #include <linux/mutex.h>
14 #include <linux/notifier.h>
15 
16 #include <linux/platform_data/cros_ec_commands.h>
17 
18 #define CROS_EC_DEV_NAME	"cros_ec"
19 #define CROS_EC_DEV_FP_NAME	"cros_fp"
20 #define CROS_EC_DEV_ISH_NAME	"cros_ish"
21 #define CROS_EC_DEV_PD_NAME	"cros_pd"
22 #define CROS_EC_DEV_SCP_NAME	"cros_scp"
23 #define CROS_EC_DEV_TP_NAME	"cros_tp"
24 
25 #define CROS_EC_DEV_EC_INDEX 0
26 #define CROS_EC_DEV_PD_INDEX 1
27 
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 
34 /*
35  * Max bus-specific overhead incurred by request/responses.
36  *
37  * Request:
38  * - I2C requires 1 byte (see struct ec_host_request_i2c).
39  * - ISHTP requires 4 bytes (see struct cros_ish_out_msg).
40  *
41  * Response:
42  * - I2C requires 2 bytes (see struct ec_host_response_i2c).
43  * - ISHTP requires 4 bytes (see struct cros_ish_in_msg).
44  * - SPI requires 32 bytes (see EC_MSG_PREAMBLE_COUNT).
45  */
46 #define EC_PROTO_VERSION_UNKNOWN	0
47 #define EC_MAX_REQUEST_OVERHEAD		4
48 #define EC_MAX_RESPONSE_OVERHEAD	32
49 
50 /*
51  * ACPI notify value for MKBP host event.
52  */
53 #define ACPI_NOTIFY_CROS_EC_MKBP 0x80
54 
55 /*
56  * EC panic is not covered by the standard (0-F) ACPI notify values.
57  * Arbitrarily choosing B0 to notify ec panic, which is in the 84-BF
58  * device specific ACPI notify range.
59  */
60 #define ACPI_NOTIFY_CROS_EC_PANIC 0xB0
61 
62 /*
63  * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
64  */
65 enum {
66 	EC_MSG_TX_HEADER_BYTES	= 3,
67 	EC_MSG_TX_TRAILER_BYTES	= 1,
68 	EC_MSG_TX_PROTO_BYTES	= EC_MSG_TX_HEADER_BYTES +
69 				  EC_MSG_TX_TRAILER_BYTES,
70 	EC_MSG_RX_PROTO_BYTES	= 3,
71 
72 	/* Max length of messages for proto 2*/
73 	EC_PROTO2_MSG_BYTES	= EC_PROTO2_MAX_PARAM_SIZE +
74 				  EC_MSG_TX_PROTO_BYTES,
75 
76 	EC_MAX_MSG_BYTES	= 64 * 1024,
77 };
78 
79 /**
80  * struct cros_ec_command - Information about a ChromeOS EC command.
81  * @version: Command version number (often 0).
82  * @command: Command to send (EC_CMD_...).
83  * @outsize: Outgoing length in bytes.
84  * @insize: Max number of bytes to accept from the EC.
85  * @result: EC's response to the command (separate from communication failure).
86  * @data: Where to put the incoming data from EC and outgoing data to EC.
87  */
88 struct cros_ec_command {
89 	uint32_t version;
90 	uint32_t command;
91 	uint32_t outsize;
92 	uint32_t insize;
93 	uint32_t result;
94 	uint8_t data[];
95 };
96 
97 /**
98  * struct cros_ec_device - Information about a ChromeOS EC device.
99  * @phys_name: Name of physical comms layer (e.g. 'i2c-4').
100  * @dev: Device pointer for physical comms device
101  * @cros_class: The class structure for this device.
102  * @cmd_readmem: Direct read of the EC memory-mapped region, if supported.
103  *     @offset: Is within EC_LPC_ADDR_MEMMAP region.
104  *     @bytes: Number of bytes to read. zero means "read a string" (including
105  *             the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be
106  *             read. Caller must ensure that the buffer is large enough for the
107  *             result when reading a string.
108  * @max_request: Max size of message requested.
109  * @max_response: Max size of message response.
110  * @max_passthru: Max sice of passthru message.
111  * @proto_version: The protocol version used for this device.
112  * @priv: Private data.
113  * @irq: Interrupt to use.
114  * @id: Device id.
115  * @din: Input buffer (for data from EC). This buffer will always be
116  *       dword-aligned and include enough space for up to 7 word-alignment
117  *       bytes also, so we can ensure that the body of the message is always
118  *       dword-aligned (64-bit). We use this alignment to keep ARM and x86
119  *       happy. Probably word alignment would be OK, there might be a small
120  *       performance advantage to using dword.
121  * @dout: Output buffer (for data to EC). This buffer will always be
122  *        dword-aligned and include enough space for up to 7 word-alignment
123  *        bytes also, so we can ensure that the body of the message is always
124  *        dword-aligned (64-bit). We use this alignment to keep ARM and x86
125  *        happy. Probably word alignment would be OK, there might be a small
126  *        performance advantage to using dword.
127  * @din_size: Size of din buffer to allocate (zero to use static din).
128  * @dout_size: Size of dout buffer to allocate (zero to use static dout).
129  * @wake_enabled: True if this device can wake the system from sleep.
130  * @suspended: True if this device had been suspended.
131  * @registered: True if this device had been registered.
132  * @cmd_xfer: Send command to EC and get response.
133  *            Returns the number of bytes received if the communication
134  *            succeeded, but that doesn't mean the EC was happy with the
135  *            command. The caller should check msg.result for the EC's result
136  *            code.
137  * @pkt_xfer: Send packet to EC and get response.
138  * @lockdep_key: Lockdep class for each instance. Unused if CONFIG_LOCKDEP is
139  *		 not enabled.
140  * @lock: One transaction at a time.
141  * @mkbp_event_supported: 0 if MKBP not supported. Otherwise its value is
142  *                        the maximum supported version of the MKBP host event
143  *                        command + 1.
144  * @host_sleep_v1: True if this EC supports the sleep v1 command.
145  * @event_notifier: Interrupt event notifier for transport devices.
146  * @event_data: Raw payload transferred with the MKBP event.
147  * @event_size: Size in bytes of the event data.
148  * @host_event_wake_mask: Mask of host events that cause wake from suspend.
149  * @suspend_timeout_ms: The timeout in milliseconds between when sleep event
150  *                      is received and when the EC will declare sleep
151  *                      transition failure if the sleep signal is not
152  *                      asserted.  See also struct
153  *                      ec_params_host_sleep_event_v1 in cros_ec_commands.h.
154  * @last_resume_result: The number of sleep power signal transitions that
155  *                      occurred since the suspend message. The high bit
156  *                      indicates a timeout occurred.  See also struct
157  *                      ec_response_host_sleep_event_v1 in cros_ec_commands.h.
158  * @last_event_time: exact time from the hard irq when we got notified of
159  *     a new event.
160  * @notifier_ready: The notifier_block to let the kernel re-query EC
161  *		    communication protocol when the EC sends
162  *		    EC_HOST_EVENT_INTERFACE_READY.
163  * @ec: The platform_device used by the mfd driver to interface with the
164  *      main EC.
165  * @pd: The platform_device used by the mfd driver to interface with the
166  *      PD behind an EC.
167  * @panic_notifier: EC panic notifier.
168  */
169 struct cros_ec_device {
170 	/* These are used by other drivers that want to talk to the EC */
171 	const char *phys_name;
172 	struct device *dev;
173 	struct class *cros_class;
174 	int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
175 			   unsigned int bytes, void *dest);
176 
177 	/* These are used to implement the platform-specific interface */
178 	u16 max_request;
179 	u16 max_response;
180 	u16 max_passthru;
181 	u16 proto_version;
182 	void *priv;
183 	int irq;
184 	u8 *din;
185 	u8 *dout;
186 	int din_size;
187 	int dout_size;
188 	bool wake_enabled;
189 	bool suspended;
190 	bool registered;
191 	int (*cmd_xfer)(struct cros_ec_device *ec,
192 			struct cros_ec_command *msg);
193 	int (*pkt_xfer)(struct cros_ec_device *ec,
194 			struct cros_ec_command *msg);
195 	struct lock_class_key lockdep_key;
196 	struct mutex lock;
197 	u8 mkbp_event_supported;
198 	bool host_sleep_v1;
199 	struct blocking_notifier_head event_notifier;
200 
201 	struct ec_response_get_next_event_v3 event_data;
202 	int event_size;
203 	u32 host_event_wake_mask;
204 	u32 last_resume_result;
205 	u16 suspend_timeout_ms;
206 	ktime_t last_event_time;
207 	struct notifier_block notifier_ready;
208 
209 	/* The platform devices used by the mfd driver */
210 	struct platform_device *ec;
211 	struct platform_device *pd;
212 
213 	struct blocking_notifier_head panic_notifier;
214 };
215 
216 /**
217  * struct cros_ec_platform - ChromeOS EC platform information.
218  * @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
219  *           used in /dev/ and sysfs.
220  * @cmd_offset: Offset to apply for each command. Set when
221  *              registering a device behind another one.
222  */
223 struct cros_ec_platform {
224 	const char *ec_name;
225 	u16 cmd_offset;
226 };
227 
228 /**
229  * struct cros_ec_dev - ChromeOS EC device entry point.
230  * @class_dev: Device structure used in sysfs.
231  * @ec_dev: cros_ec_device structure to talk to the physical device.
232  * @dev: Pointer to the platform device.
233  * @debug_info: cros_ec_debugfs structure for debugging information.
234  * @has_kb_wake_angle: True if at least 2 accelerometer are connected to the EC.
235  * @cmd_offset: Offset to apply for each command.
236  * @features: Features supported by the EC.
237  */
238 struct cros_ec_dev {
239 	struct device class_dev;
240 	struct cros_ec_device *ec_dev;
241 	struct device *dev;
242 	struct cros_ec_debugfs *debug_info;
243 	bool has_kb_wake_angle;
244 	u16 cmd_offset;
245 	struct ec_response_get_features features;
246 };
247 
248 #define to_cros_ec_dev(dev)  container_of(dev, struct cros_ec_dev, class_dev)
249 
250 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
251 		       struct cros_ec_command *msg);
252 
253 int cros_ec_check_result(struct cros_ec_device *ec_dev,
254 			 struct cros_ec_command *msg);
255 
256 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
257 		     struct cros_ec_command *msg);
258 
259 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
260 			    struct cros_ec_command *msg);
261 
262 int cros_ec_rwsig_continue(struct cros_ec_device *ec_dev);
263 
264 int cros_ec_query_all(struct cros_ec_device *ec_dev);
265 
266 int cros_ec_get_next_event(struct cros_ec_device *ec_dev,
267 			   bool *wake_event,
268 			   bool *has_more_events);
269 
270 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
271 
272 bool cros_ec_check_features(struct cros_ec_dev *ec, int feature);
273 
274 int cros_ec_get_sensor_count(struct cros_ec_dev *ec);
275 
276 int cros_ec_cmd(struct cros_ec_device *ec_dev, unsigned int version, int command, const void *outdata,
277 		    size_t outsize, void *indata, size_t insize);
278 
279 int cros_ec_cmd_readmem(struct cros_ec_device *ec_dev, u8 offset, u8 size, void *dest);
280 
281 int cros_ec_get_cmd_versions(struct cros_ec_device *ec_dev, u16 cmd);
282 
283 bool cros_ec_device_registered(struct cros_ec_device *ec_dev);
284 
285 /**
286  * cros_ec_get_time_ns() - Return time in ns.
287  *
288  * This is the function used to record the time for last_event_time in struct
289  * cros_ec_device during the hard irq.
290  *
291  * Return: ktime_t format since boot.
292  */
cros_ec_get_time_ns(void)293 static inline ktime_t cros_ec_get_time_ns(void)
294 {
295 	return ktime_get_boottime_ns();
296 }
297 
298 #endif /* __LINUX_CROS_EC_PROTO_H */
299