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/mutex.h>
13 #include <linux/notifier.h>
14
15 #include <linux/platform_data/cros_ec_commands.h>
16
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_SCP_C1_NAME "cros_scp_c1"
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 * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
46 */
47 enum {
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,
53
54 /* Max length of messages for proto 2*/
55 EC_PROTO2_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
56 EC_MSG_TX_PROTO_BYTES,
57
58 EC_MAX_MSG_BYTES = 64 * 1024,
59 };
60
61 /**
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.
69 */
70 struct cros_ec_command {
71 uint32_t version;
72 uint32_t command;
73 uint32_t outsize;
74 uint32_t insize;
75 uint32_t result;
76 uint8_t data[];
77 };
78
79 /**
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 * @cros_class: The class structure for this device.
84 * @cmd_readmem: Direct read of the EC memory-mapped region, if supported.
85 * @offset: Is within EC_LPC_ADDR_MEMMAP region.
86 * @bytes: Number of bytes to read. zero means "read a string" (including
87 * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be
88 * read. Caller must ensure that the buffer is large enough for the
89 * result when reading a string.
90 * @max_request: Max size of message requested.
91 * @max_response: Max size of message response.
92 * @max_passthru: Max sice of passthru message.
93 * @proto_version: The protocol version used for this device.
94 * @priv: Private data.
95 * @irq: Interrupt to use.
96 * @id: Device id.
97 * @din: Input buffer (for data from EC). This buffer will always be
98 * dword-aligned and include enough space for up to 7 word-alignment
99 * bytes also, so we can ensure that the body of the message is always
100 * dword-aligned (64-bit). We use this alignment to keep ARM and x86
101 * happy. Probably word alignment would be OK, there might be a small
102 * performance advantage to using dword.
103 * @dout: Output buffer (for data to EC). This buffer will always be
104 * dword-aligned and include enough space for up to 7 word-alignment
105 * bytes also, so we can ensure that the body of the message is always
106 * dword-aligned (64-bit). We use this alignment to keep ARM and x86
107 * happy. Probably word alignment would be OK, there might be a small
108 * performance advantage to using dword.
109 * @din_size: Size of din buffer to allocate (zero to use static din).
110 * @dout_size: Size of dout buffer to allocate (zero to use static dout).
111 * @wake_enabled: True if this device can wake the system from sleep.
112 * @suspended: True if this device had been suspended.
113 * @cmd_xfer: Send command to EC and get response.
114 * Returns the number of bytes received if the communication
115 * succeeded, but that doesn't mean the EC was happy with the
116 * command. The caller should check msg.result for the EC's result
117 * code.
118 * @pkt_xfer: Send packet to EC and get response.
119 * @lock: One transaction at a time.
120 * @mkbp_event_supported: 0 if MKBP not supported. Otherwise its value is
121 * the maximum supported version of the MKBP host event
122 * command + 1.
123 * @host_sleep_v1: True if this EC supports the sleep v1 command.
124 * @event_notifier: Interrupt event notifier for transport devices.
125 * @event_data: Raw payload transferred with the MKBP event.
126 * @event_size: Size in bytes of the event data.
127 * @host_event_wake_mask: Mask of host events that cause wake from suspend.
128 * @last_event_time: exact time from the hard irq when we got notified of
129 * a new event.
130 * @notifier_ready: The notifier_block to let the kernel re-query EC
131 * communication protocol when the EC sends
132 * EC_HOST_EVENT_INTERFACE_READY.
133 * @ec: The platform_device used by the mfd driver to interface with the
134 * main EC.
135 * @pd: The platform_device used by the mfd driver to interface with the
136 * PD behind an EC.
137 */
138 struct cros_ec_device {
139 /* These are used by other drivers that want to talk to the EC */
140 const char *phys_name;
141 struct device *dev;
142 struct class *cros_class;
143 int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
144 unsigned int bytes, void *dest);
145
146 /* These are used to implement the platform-specific interface */
147 u16 max_request;
148 u16 max_response;
149 u16 max_passthru;
150 u16 proto_version;
151 void *priv;
152 int irq;
153 u8 *din;
154 u8 *dout;
155 int din_size;
156 int dout_size;
157 bool wake_enabled;
158 bool suspended;
159 int (*cmd_xfer)(struct cros_ec_device *ec,
160 struct cros_ec_command *msg);
161 int (*pkt_xfer)(struct cros_ec_device *ec,
162 struct cros_ec_command *msg);
163 struct mutex lock;
164 u8 mkbp_event_supported;
165 bool host_sleep_v1;
166 struct blocking_notifier_head event_notifier;
167
168 struct ec_response_get_next_event_v1 event_data;
169 int event_size;
170 u32 host_event_wake_mask;
171 u32 last_resume_result;
172 u16 suspend_timeout_ms;
173 ktime_t last_event_time;
174 struct notifier_block notifier_ready;
175
176 /* The platform devices used by the mfd driver */
177 struct platform_device *ec;
178 struct platform_device *pd;
179 };
180
181 /**
182 * struct cros_ec_platform - ChromeOS EC platform information.
183 * @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
184 * used in /dev/ and sysfs.
185 * @cmd_offset: Offset to apply for each command. Set when
186 * registering a device behind another one.
187 */
188 struct cros_ec_platform {
189 const char *ec_name;
190 u16 cmd_offset;
191 };
192
193 /**
194 * struct cros_ec_dev - ChromeOS EC device entry point.
195 * @class_dev: Device structure used in sysfs.
196 * @ec_dev: cros_ec_device structure to talk to the physical device.
197 * @dev: Pointer to the platform device.
198 * @debug_info: cros_ec_debugfs structure for debugging information.
199 * @has_kb_wake_angle: True if at least 2 accelerometer are connected to the EC.
200 * @cmd_offset: Offset to apply for each command.
201 * @features: Features supported by the EC.
202 */
203 struct cros_ec_dev {
204 struct device class_dev;
205 struct cros_ec_device *ec_dev;
206 struct device *dev;
207 struct cros_ec_debugfs *debug_info;
208 bool has_kb_wake_angle;
209 u16 cmd_offset;
210 struct ec_response_get_features features;
211 };
212
213 #define to_cros_ec_dev(dev) container_of(dev, struct cros_ec_dev, class_dev)
214
215 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
216 struct cros_ec_command *msg);
217
218 int cros_ec_check_result(struct cros_ec_device *ec_dev,
219 struct cros_ec_command *msg);
220
221 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
222 struct cros_ec_command *msg);
223
224 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
225 struct cros_ec_command *msg);
226
227 int cros_ec_query_all(struct cros_ec_device *ec_dev);
228
229 int cros_ec_get_next_event(struct cros_ec_device *ec_dev,
230 bool *wake_event,
231 bool *has_more_events);
232
233 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
234
235 bool cros_ec_check_features(struct cros_ec_dev *ec, int feature);
236
237 int cros_ec_get_sensor_count(struct cros_ec_dev *ec);
238
239 int cros_ec_cmd(struct cros_ec_device *ec_dev, unsigned int version, int command, void *outdata,
240 size_t outsize, void *indata, size_t insize);
241
242 /**
243 * cros_ec_get_time_ns() - Return time in ns.
244 *
245 * This is the function used to record the time for last_event_time in struct
246 * cros_ec_device during the hard irq.
247 *
248 * Return: ktime_t format since boot.
249 */
cros_ec_get_time_ns(void)250 static inline ktime_t cros_ec_get_time_ns(void)
251 {
252 return ktime_get_boottime_ns();
253 }
254
255 #endif /* __LINUX_CROS_EC_PROTO_H */
256