Merge tag 'tag-chrome-platform-for-v5.3' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / include / linux / mfd / cros_ec.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * ChromeOS EC multi-function device
4  *
5  * Copyright (C) 2012 Google, Inc
6  */
7
8 #ifndef __LINUX_MFD_CROS_EC_H
9 #define __LINUX_MFD_CROS_EC_H
10
11 #include <linux/cdev.h>
12 #include <linux/device.h>
13 #include <linux/notifier.h>
14 #include <linux/mfd/cros_ec_commands.h>
15 #include <linux/mutex.h>
16
17 #define CROS_EC_DEV_NAME "cros_ec"
18 #define CROS_EC_DEV_FP_NAME "cros_fp"
19 #define CROS_EC_DEV_PD_NAME "cros_pd"
20 #define CROS_EC_DEV_TP_NAME "cros_tp"
21 #define CROS_EC_DEV_ISH_NAME "cros_ish"
22
23 /*
24  * The EC is unresponsive for a time after a reboot command.  Add a
25  * simple delay to make sure that the bus stays locked.
26  */
27 #define EC_REBOOT_DELAY_MS             50
28
29 /*
30  * Max bus-specific overhead incurred by request/responses.
31  * I2C requires 1 additional byte for requests.
32  * I2C requires 2 additional bytes for responses.
33  * SPI requires up to 32 additional bytes for responses.
34  */
35 #define EC_PROTO_VERSION_UNKNOWN        0
36 #define EC_MAX_REQUEST_OVERHEAD         1
37 #define EC_MAX_RESPONSE_OVERHEAD        32
38
39 /*
40  * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
41  */
42 enum {
43         EC_MSG_TX_HEADER_BYTES  = 3,
44         EC_MSG_TX_TRAILER_BYTES = 1,
45         EC_MSG_TX_PROTO_BYTES   = EC_MSG_TX_HEADER_BYTES +
46                                         EC_MSG_TX_TRAILER_BYTES,
47         EC_MSG_RX_PROTO_BYTES   = 3,
48
49         /* Max length of messages for proto 2*/
50         EC_PROTO2_MSG_BYTES             = EC_PROTO2_MAX_PARAM_SIZE +
51                                         EC_MSG_TX_PROTO_BYTES,
52
53         EC_MAX_MSG_BYTES                = 64 * 1024,
54 };
55
56 /**
57  * struct cros_ec_command - Information about a ChromeOS EC command.
58  * @version: Command version number (often 0).
59  * @command: Command to send (EC_CMD_...).
60  * @outsize: Outgoing length in bytes.
61  * @insize: Max number of bytes to accept from the EC.
62  * @result: EC's response to the command (separate from communication failure).
63  * @data: Where to put the incoming data from EC and outgoing data to EC.
64  */
65 struct cros_ec_command {
66         uint32_t version;
67         uint32_t command;
68         uint32_t outsize;
69         uint32_t insize;
70         uint32_t result;
71         uint8_t data[0];
72 };
73
74 /**
75  * struct cros_ec_device - Information about a ChromeOS EC device.
76  * @phys_name: Name of physical comms layer (e.g. 'i2c-4').
77  * @dev: Device pointer for physical comms device
78  * @was_wake_device: True if this device was set to wake the system from
79  *                   sleep at the last suspend.
80  * @cros_class: The class structure for this device.
81  * @cmd_readmem: Direct read of the EC memory-mapped region, if supported.
82  *     @offset: Is within EC_LPC_ADDR_MEMMAP region.
83  *     @bytes: Number of bytes to read. zero means "read a string" (including
84  *             the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be
85  *             read. Caller must ensure that the buffer is large enough for the
86  *             result when reading a string.
87  * @max_request: Max size of message requested.
88  * @max_response: Max size of message response.
89  * @max_passthru: Max sice of passthru message.
90  * @proto_version: The protocol version used for this device.
91  * @priv: Private data.
92  * @irq: Interrupt to use.
93  * @id: Device id.
94  * @din: Input buffer (for data from EC). This buffer will always be
95  *       dword-aligned and include enough space for up to 7 word-alignment
96  *       bytes also, so we can ensure that the body of the message is always
97  *       dword-aligned (64-bit). We use this alignment to keep ARM and x86
98  *       happy. Probably word alignment would be OK, there might be a small
99  *       performance advantage to using dword.
100  * @dout: Output buffer (for data to EC). This buffer will always be
101  *        dword-aligned and include enough space for up to 7 word-alignment
102  *        bytes also, so we can ensure that the body of the message is always
103  *        dword-aligned (64-bit). We use this alignment to keep ARM and x86
104  *        happy. Probably word alignment would be OK, there might be a small
105  *        performance advantage to using dword.
106  * @din_size: Size of din buffer to allocate (zero to use static din).
107  * @dout_size: Size of dout buffer to allocate (zero to use static dout).
108  * @wake_enabled: True if this device can wake the system from sleep.
109  * @suspended: True if this device had been suspended.
110  * @cmd_xfer: Send command to EC and get response.
111  *            Returns the number of bytes received if the communication
112  *            succeeded, but that doesn't mean the EC was happy with the
113  *            command. The caller should check msg.result for the EC's result
114  *            code.
115  * @pkt_xfer: Send packet to EC and get response.
116  * @lock: One transaction at a time.
117  * @mkbp_event_supported: True if this EC supports the MKBP event protocol.
118  * @host_sleep_v1: True if this EC supports the sleep v1 command.
119  * @event_notifier: Interrupt event notifier for transport devices.
120  * @event_data: Raw payload transferred with the MKBP event.
121  * @event_size: Size in bytes of the event data.
122  * @host_event_wake_mask: Mask of host events that cause wake from suspend.
123  */
124 struct cros_ec_device {
125         /* These are used by other drivers that want to talk to the EC */
126         const char *phys_name;
127         struct device *dev;
128         bool was_wake_device;
129         struct class *cros_class;
130         int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
131                            unsigned int bytes, void *dest);
132
133         /* These are used to implement the platform-specific interface */
134         u16 max_request;
135         u16 max_response;
136         u16 max_passthru;
137         u16 proto_version;
138         void *priv;
139         int irq;
140         u8 *din;
141         u8 *dout;
142         int din_size;
143         int dout_size;
144         bool wake_enabled;
145         bool suspended;
146         int (*cmd_xfer)(struct cros_ec_device *ec,
147                         struct cros_ec_command *msg);
148         int (*pkt_xfer)(struct cros_ec_device *ec,
149                         struct cros_ec_command *msg);
150         struct mutex lock;
151         bool mkbp_event_supported;
152         bool host_sleep_v1;
153         struct blocking_notifier_head event_notifier;
154
155         struct ec_response_get_next_event_v1 event_data;
156         int event_size;
157         u32 host_event_wake_mask;
158         u32 last_resume_result;
159 };
160
161 /**
162  * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
163  * @sensor_num: Id of the sensor, as reported by the EC.
164  */
165 struct cros_ec_sensor_platform {
166         u8 sensor_num;
167 };
168
169 /**
170  * struct cros_ec_platform - ChromeOS EC platform information.
171  * @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
172  *           used in /dev/ and sysfs.
173  * @cmd_offset: Offset to apply for each command. Set when
174  *              registering a device behind another one.
175  */
176 struct cros_ec_platform {
177         const char *ec_name;
178         u16 cmd_offset;
179 };
180
181 struct cros_ec_debugfs;
182
183 /**
184  * struct cros_ec_dev - ChromeOS EC device entry point.
185  * @class_dev: Device structure used in sysfs.
186  * @cdev: Character device structure in /dev.
187  * @ec_dev: cros_ec_device structure to talk to the physical device.
188  * @dev: Pointer to the platform device.
189  * @debug_info: cros_ec_debugfs structure for debugging information.
190  * @has_kb_wake_angle: True if at least 2 accelerometer are connected to the EC.
191  * @cmd_offset: Offset to apply for each command.
192  * @features: Features supported by the EC.
193  */
194 struct cros_ec_dev {
195         struct device class_dev;
196         struct cdev cdev;
197         struct cros_ec_device *ec_dev;
198         struct device *dev;
199         struct cros_ec_debugfs *debug_info;
200         bool has_kb_wake_angle;
201         u16 cmd_offset;
202         u32 features[2];
203 };
204
205 #define to_cros_ec_dev(dev)  container_of(dev, struct cros_ec_dev, class_dev)
206
207 /**
208  * cros_ec_suspend() - Handle a suspend operation for the ChromeOS EC device.
209  * @ec_dev: Device to suspend.
210  *
211  * This can be called by drivers to handle a suspend event.
212  *
213  * Return: 0 on success or negative error code.
214  */
215 int cros_ec_suspend(struct cros_ec_device *ec_dev);
216
217 /**
218  * cros_ec_resume() - Handle a resume operation for the ChromeOS EC device.
219  * @ec_dev: Device to resume.
220  *
221  * This can be called by drivers to handle a resume event.
222  *
223  * Return: 0 on success or negative error code.
224  */
225 int cros_ec_resume(struct cros_ec_device *ec_dev);
226
227 /**
228  * cros_ec_prepare_tx() - Prepare an outgoing message in the output buffer.
229  * @ec_dev: Device to register.
230  * @msg: Message to write.
231  *
232  * This is intended to be used by all ChromeOS EC drivers, but at present
233  * only SPI uses it. Once LPC uses the same protocol it can start using it.
234  * I2C could use it now, with a refactor of the existing code.
235  *
236  * Return: 0 on success or negative error code.
237  */
238 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
239                        struct cros_ec_command *msg);
240
241 /**
242  * cros_ec_check_result() - Check ec_msg->result.
243  * @ec_dev: EC device.
244  * @msg: Message to check.
245  *
246  * This is used by ChromeOS EC drivers to check the ec_msg->result for
247  * errors and to warn about them.
248  *
249  * Return: 0 on success or negative error code.
250  */
251 int cros_ec_check_result(struct cros_ec_device *ec_dev,
252                          struct cros_ec_command *msg);
253
254 /**
255  * cros_ec_cmd_xfer() - Send a command to the ChromeOS EC.
256  * @ec_dev: EC device.
257  * @msg: Message to write.
258  *
259  * Call this to send a command to the ChromeOS EC.  This should be used
260  * instead of calling the EC's cmd_xfer() callback directly.
261  *
262  * Return: 0 on success or negative error code.
263  */
264 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
265                      struct cros_ec_command *msg);
266
267 /**
268  * cros_ec_cmd_xfer_status() - Send a command to the ChromeOS EC.
269  * @ec_dev: EC device.
270  * @msg: Message to write.
271  *
272  * This function is identical to cros_ec_cmd_xfer, except it returns success
273  * status only if both the command was transmitted successfully and the EC
274  * replied with success status. It's not necessary to check msg->result when
275  * using this function.
276  *
277  * Return: The number of bytes transferred on success or negative error code.
278  */
279 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
280                             struct cros_ec_command *msg);
281
282 /**
283  * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
284  * @ec_dev: Device to register.
285  *
286  * Before calling this, allocate a pointer to a new device and then fill
287  * in all the fields up to the --private-- marker.
288  *
289  * Return: 0 on success or negative error code.
290  */
291 int cros_ec_register(struct cros_ec_device *ec_dev);
292
293 /**
294  * cros_ec_query_all() -  Query the protocol version supported by the
295  *         ChromeOS EC.
296  * @ec_dev: Device to register.
297  *
298  * Return: 0 on success or negative error code.
299  */
300 int cros_ec_query_all(struct cros_ec_device *ec_dev);
301
302 /**
303  * cros_ec_get_next_event() - Fetch next event from the ChromeOS EC.
304  * @ec_dev: Device to fetch event from.
305  * @wake_event: Pointer to a bool set to true upon return if the event might be
306  *              treated as a wake event. Ignored if null.
307  *
308  * Return: negative error code on errors; 0 for no data; or else number of
309  * bytes received (i.e., an event was retrieved successfully). Event types are
310  * written out to @ec_dev->event_data.event_type on success.
311  */
312 int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event);
313
314 /**
315  * cros_ec_get_host_event() - Return a mask of event set by the ChromeOS EC.
316  * @ec_dev: Device to fetch event from.
317  *
318  * When MKBP is supported, when the EC raises an interrupt, we collect the
319  * events raised and call the functions in the ec notifier. This function
320  * is a helper to know which events are raised.
321  *
322  * Return: 0 on error or non-zero bitmask of one or more EC_HOST_EVENT_*.
323  */
324 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
325
326 #endif /* __LINUX_MFD_CROS_EC_H */