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_TP_NAME	"cros_tp"
23 
24 /*
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.
27  */
28 #define EC_REBOOT_DELAY_MS		50
29 
30 /*
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.
35  */
36 #define EC_PROTO_VERSION_UNKNOWN	0
37 #define EC_MAX_REQUEST_OVERHEAD		1
38 #define EC_MAX_RESPONSE_OVERHEAD	32
39 
40 /*
41  * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
42  */
43 enum {
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,
49 
50 	/* Max length of messages for proto 2*/
51 	EC_PROTO2_MSG_BYTES	= EC_PROTO2_MAX_PARAM_SIZE +
52 				  EC_MSG_TX_PROTO_BYTES,
53 
54 	EC_MAX_MSG_BYTES	= 64 * 1024,
55 };
56 
57 /**
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.
65  */
66 struct cros_ec_command {
67 	uint32_t version;
68 	uint32_t command;
69 	uint32_t outsize;
70 	uint32_t insize;
71 	uint32_t result;
72 	uint8_t data[];
73 };
74 
75 /**
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.
92  * @id: Device id.
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
113  *            code.
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
118  *                        command + 1.
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
125  *     a new event.
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
130  *      main EC.
131  * @pd: The platform_device used by the mfd driver to interface with the
132  *      PD behind an EC.
133  */
134 struct cros_ec_device {
135 	/* These are used by other drivers that want to talk to the EC */
136 	const char *phys_name;
137 	struct device *dev;
138 	struct class *cros_class;
139 	int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
140 			   unsigned int bytes, void *dest);
141 
142 	/* These are used to implement the platform-specific interface */
143 	u16 max_request;
144 	u16 max_response;
145 	u16 max_passthru;
146 	u16 proto_version;
147 	void *priv;
148 	int irq;
149 	u8 *din;
150 	u8 *dout;
151 	int din_size;
152 	int dout_size;
153 	bool wake_enabled;
154 	bool suspended;
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);
159 	struct mutex lock;
160 	u8 mkbp_event_supported;
161 	bool host_sleep_v1;
162 	struct blocking_notifier_head event_notifier;
163 
164 	struct ec_response_get_next_event_v1 event_data;
165 	int event_size;
166 	u32 host_event_wake_mask;
167 	u32 last_resume_result;
168 	ktime_t last_event_time;
169 	struct notifier_block notifier_ready;
170 
171 	/* The platform devices used by the mfd driver */
172 	struct platform_device *ec;
173 	struct platform_device *pd;
174 };
175 
176 /**
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.
182  */
183 struct cros_ec_platform {
184 	const char *ec_name;
185 	u16 cmd_offset;
186 };
187 
188 /**
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.
197  */
198 struct cros_ec_dev {
199 	struct device class_dev;
200 	struct cros_ec_device *ec_dev;
201 	struct device *dev;
202 	struct cros_ec_debugfs *debug_info;
203 	bool has_kb_wake_angle;
204 	u16 cmd_offset;
205 	struct ec_response_get_features features;
206 };
207 
208 #define to_cros_ec_dev(dev)  container_of(dev, struct cros_ec_dev, class_dev)
209 
210 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
211 		       struct cros_ec_command *msg);
212 
213 int cros_ec_check_result(struct cros_ec_device *ec_dev,
214 			 struct cros_ec_command *msg);
215 
216 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
217 		     struct cros_ec_command *msg);
218 
219 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
220 			    struct cros_ec_command *msg);
221 
222 int cros_ec_query_all(struct cros_ec_device *ec_dev);
223 
224 int cros_ec_get_next_event(struct cros_ec_device *ec_dev,
225 			   bool *wake_event,
226 			   bool *has_more_events);
227 
228 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
229 
230 bool cros_ec_check_features(struct cros_ec_dev *ec, int feature);
231 
232 int cros_ec_get_sensor_count(struct cros_ec_dev *ec);
233 
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);
236 
237 /**
238  * cros_ec_get_time_ns() - Return time in ns.
239  *
240  * This is the function used to record the time for last_event_time in struct
241  * cros_ec_device during the hard irq.
242  *
243  * Return: ktime_t format since boot.
244  */
cros_ec_get_time_ns(void)245 static inline ktime_t cros_ec_get_time_ns(void)
246 {
247 	return ktime_get_boottime_ns();
248 }
249 
250 #endif /* __LINUX_CROS_EC_PROTO_H */
251