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