blob: 27607969950494b2fbddc561478c5782a5419250 [file] [log] [blame]
Simon Glass4ab61742013-02-25 14:08:37 -08001/*
2 * ChromeOS EC multi-function device
3 *
4 * Copyright (C) 2012 Google, Inc
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#ifndef __LINUX_MFD_CROS_EC_H
17#define __LINUX_MFD_CROS_EC_H
18
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +010019#include <linux/cdev.h>
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020020#include <linux/device.h>
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070021#include <linux/notifier.h>
Simon Glass4ab61742013-02-25 14:08:37 -080022#include <linux/mfd/cros_ec_commands.h>
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070023#include <linux/mutex.h>
Simon Glass4ab61742013-02-25 14:08:37 -080024
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020025#define CROS_EC_DEV_NAME "cros_ec"
Enric Balletbo i Serra90486af2019-05-08 11:19:55 +020026#define CROS_EC_DEV_FP_NAME "cros_fp"
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020027#define CROS_EC_DEV_PD_NAME "cros_pd"
Rushikesh S Kadamd4cee952019-03-01 13:50:54 +053028#define CROS_EC_DEV_ISH_NAME "cros_ish"
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020029
Simon Glass4ab61742013-02-25 14:08:37 -080030/*
Stephen Barberd3654072015-06-09 13:04:46 +020031 * The EC is unresponsive for a time after a reboot command. Add a
32 * simple delay to make sure that the bus stays locked.
33 */
34#define EC_REBOOT_DELAY_MS 50
35
36/*
Stephen Barber2c7589a2015-06-09 13:04:45 +020037 * Max bus-specific overhead incurred by request/responses.
38 * I2C requires 1 additional byte for requests.
39 * I2C requires 2 additional bytes for responses.
Vic Yangb2376402017-03-24 18:44:01 +010040 * SPI requires up to 32 additional bytes for responses.
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +020041 */
Stephen Barber2c7589a2015-06-09 13:04:45 +020042#define EC_PROTO_VERSION_UNKNOWN 0
43#define EC_MAX_REQUEST_OVERHEAD 1
Vic Yangb2376402017-03-24 18:44:01 +010044#define EC_MAX_RESPONSE_OVERHEAD 32
Stephen Barber2c7589a2015-06-09 13:04:45 +020045
46/*
Simon Glass4ab61742013-02-25 14:08:37 -080047 * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
48 */
49enum {
50 EC_MSG_TX_HEADER_BYTES = 3,
51 EC_MSG_TX_TRAILER_BYTES = 1,
52 EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES +
53 EC_MSG_TX_TRAILER_BYTES,
54 EC_MSG_RX_PROTO_BYTES = 3,
55
Gwendal Grignou5d749d02016-03-08 09:13:52 -080056 /* Max length of messages for proto 2*/
57 EC_PROTO2_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
Bill Richardson5271db22014-04-30 10:44:08 -070058 EC_MSG_TX_PROTO_BYTES,
Gwendal Grignou5d749d02016-03-08 09:13:52 -080059
60 EC_MAX_MSG_BYTES = 64 * 1024,
Simon Glass4ab61742013-02-25 14:08:37 -080061};
62
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +020063/**
64 * struct cros_ec_command - Information about a ChromeOS EC command.
65 * @version: Command version number (often 0).
66 * @command: Command to send (EC_CMD_...).
67 * @outsize: Outgoing length in bytes.
68 * @insize: Max number of bytes to accept from the EC.
69 * @result: EC's response to the command (separate from communication failure).
70 * @data: Where to put the incoming data from EC and outgoing data to EC.
Simon Glass4ab61742013-02-25 14:08:37 -080071 */
Bill Richardson5d4773e2014-06-18 11:14:02 -070072struct cros_ec_command {
73 uint32_t version;
74 uint32_t command;
Bill Richardson5d4773e2014-06-18 11:14:02 -070075 uint32_t outsize;
Bill Richardson5d4773e2014-06-18 11:14:02 -070076 uint32_t insize;
77 uint32_t result;
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020078 uint8_t data[0];
Simon Glass4ab61742013-02-25 14:08:37 -080079};
80
81/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +020082 * struct cros_ec_device - Information about a ChromeOS EC device.
83 * @phys_name: Name of physical comms layer (e.g. 'i2c-4').
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +010084 * @dev: Device pointer for physical comms device
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +020085 * @was_wake_device: True if this device was set to wake the system from
86 * sleep at the last suspend.
87 * @cros_class: The class structure for this device.
88 * @cmd_readmem: Direct read of the EC memory-mapped region, if supported.
89 * @offset: Is within EC_LPC_ADDR_MEMMAP region.
90 * @bytes: Number of bytes to read. zero means "read a string" (including
91 * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be
92 * read. Caller must ensure that the buffer is large enough for the
93 * result when reading a string.
94 * @max_request: Max size of message requested.
95 * @max_response: Max size of message response.
96 * @max_passthru: Max sice of passthru message.
97 * @proto_version: The protocol version used for this device.
98 * @priv: Private data.
99 * @irq: Interrupt to use.
100 * @id: Device id.
101 * @din: Input buffer (for data from EC). This buffer will always be
102 * dword-aligned and include enough space for up to 7 word-alignment
103 * bytes also, so we can ensure that the body of the message is always
104 * dword-aligned (64-bit). We use this alignment to keep ARM and x86
105 * happy. Probably word alignment would be OK, there might be a small
106 * performance advantage to using dword.
107 * @dout: Output buffer (for data to EC). This buffer will always be
108 * dword-aligned and include enough space for up to 7 word-alignment
109 * bytes also, so we can ensure that the body of the message is always
110 * dword-aligned (64-bit). We use this alignment to keep ARM and x86
111 * happy. Probably word alignment would be OK, there might be a small
112 * performance advantage to using dword.
113 * @din_size: Size of din buffer to allocate (zero to use static din).
114 * @dout_size: Size of dout buffer to allocate (zero to use static dout).
115 * @wake_enabled: True if this device can wake the system from sleep.
116 * @suspended: True if this device had been suspended.
117 * @cmd_xfer: Send command to EC and get response.
118 * Returns the number of bytes received if the communication
119 * succeeded, but that doesn't mean the EC was happy with the
120 * command. The caller should check msg.result for the EC's result
121 * code.
122 * @pkt_xfer: Send packet to EC and get response.
123 * @lock: One transaction at a time.
124 * @mkbp_event_supported: True if this EC supports the MKBP event protocol.
Evan Green72355602019-04-03 14:34:28 -0700125 * @host_sleep_v1: True if this EC supports the sleep v1 command.
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200126 * @event_notifier: Interrupt event notifier for transport devices.
127 * @event_data: Raw payload transferred with the MKBP event.
128 * @event_size: Size in bytes of the event data.
129 * @host_event_wake_mask: Mask of host events that cause wake from suspend.
Simon Glass4ab61742013-02-25 14:08:37 -0800130 */
131struct cros_ec_device {
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700132 /* These are used by other drivers that want to talk to the EC */
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700133 const char *phys_name;
134 struct device *dev;
135 bool was_wake_device;
136 struct class *cros_class;
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +0100137 int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
138 unsigned int bytes, void *dest);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700139
140 /* These are used to implement the platform-specific interface */
Stephen Barber2c7589a2015-06-09 13:04:45 +0200141 u16 max_request;
142 u16 max_response;
143 u16 max_passthru;
144 u16 proto_version;
Simon Glass4ab61742013-02-25 14:08:37 -0800145 void *priv;
146 int irq;
Stephen Barber2c7589a2015-06-09 13:04:45 +0200147 u8 *din;
148 u8 *dout;
Simon Glass4ab61742013-02-25 14:08:37 -0800149 int din_size;
150 int dout_size;
Simon Glass4ab61742013-02-25 14:08:37 -0800151 bool wake_enabled;
Joseph Loa9eb1862016-12-16 18:57:36 +0100152 bool suspended;
Andrew Brestickera6551a72014-09-18 17:18:56 +0200153 int (*cmd_xfer)(struct cros_ec_device *ec,
154 struct cros_ec_command *msg);
Stephen Barber2c7589a2015-06-09 13:04:45 +0200155 int (*pkt_xfer)(struct cros_ec_device *ec,
156 struct cros_ec_command *msg);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700157 struct mutex lock;
Vic Yang6f1d9122016-08-10 19:05:24 +0200158 bool mkbp_event_supported;
Evan Green72355602019-04-03 14:34:28 -0700159 bool host_sleep_v1;
Vic Yang6f1d9122016-08-10 19:05:24 +0200160 struct blocking_notifier_head event_notifier;
161
Neil Armstrong57e94c82018-07-04 17:08:18 +0200162 struct ec_response_get_next_event_v1 event_data;
Vic Yang6f1d9122016-08-10 19:05:24 +0200163 int event_size;
Shawn Nematbakhsh29d99b92017-02-14 20:58:02 +0100164 u32 host_event_wake_mask;
Simon Glass4ab61742013-02-25 14:08:37 -0800165};
166
Enric Balletbo i Serra974e6f02016-08-01 11:54:35 +0200167/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200168 * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
Enric Balletbo i Serra974e6f02016-08-01 11:54:35 +0200169 * @sensor_num: Id of the sensor, as reported by the EC.
170 */
171struct cros_ec_sensor_platform {
172 u8 sensor_num;
173};
174
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200175/**
176 * struct cros_ec_platform - ChromeOS EC platform information.
177 * @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
178 * used in /dev/ and sysfs.
179 * @cmd_offset: Offset to apply for each command. Set when
180 * registering a device behind another one.
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200181 */
182struct cros_ec_platform {
183 const char *ec_name;
184 u16 cmd_offset;
185};
186
Eric Carusoe8626452017-05-16 17:46:48 +0200187struct cros_ec_debugfs;
188
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200189/**
190 * struct cros_ec_dev - ChromeOS EC device entry point.
191 * @class_dev: Device structure used in sysfs.
192 * @cdev: Character device structure in /dev.
193 * @ec_dev: cros_ec_device structure to talk to the physical device.
194 * @dev: Pointer to the platform device.
195 * @debug_info: cros_ec_debugfs structure for debugging information.
196 * @has_kb_wake_angle: True if at least 2 accelerometer are connected to the EC.
197 * @cmd_offset: Offset to apply for each command.
198 * @features: Features supported by the EC.
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200199 */
200struct cros_ec_dev {
201 struct device class_dev;
202 struct cdev cdev;
203 struct cros_ec_device *ec_dev;
204 struct device *dev;
Eric Carusoe8626452017-05-16 17:46:48 +0200205 struct cros_ec_debugfs *debug_info;
Gwendal Grignouc1d1e91a2018-03-23 18:42:47 +0100206 bool has_kb_wake_angle;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200207 u16 cmd_offset;
Vincent Palatine4244eb2016-08-01 11:54:37 +0200208 u32 features[2];
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200209};
210
Gwendal Grignou79a3d602018-05-30 09:04:13 -0700211#define to_cros_ec_dev(dev) container_of(dev, struct cros_ec_dev, class_dev)
212
Simon Glass4ab61742013-02-25 14:08:37 -0800213/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200214 * cros_ec_suspend() - Handle a suspend operation for the ChromeOS EC device.
215 * @ec_dev: Device to suspend.
Simon Glass4ab61742013-02-25 14:08:37 -0800216 *
217 * This can be called by drivers to handle a suspend event.
218 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200219 * Return: 0 on success or negative error code.
Simon Glass4ab61742013-02-25 14:08:37 -0800220 */
221int cros_ec_suspend(struct cros_ec_device *ec_dev);
222
223/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200224 * cros_ec_resume() - Handle a resume operation for the ChromeOS EC device.
225 * @ec_dev: Device to resume.
Simon Glass4ab61742013-02-25 14:08:37 -0800226 *
227 * This can be called by drivers to handle a resume event.
228 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200229 * Return: 0 on success or negative error code.
Simon Glass4ab61742013-02-25 14:08:37 -0800230 */
231int cros_ec_resume(struct cros_ec_device *ec_dev);
232
233/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200234 * cros_ec_prepare_tx() - Prepare an outgoing message in the output buffer.
235 * @ec_dev: Device to register.
236 * @msg: Message to write.
Simon Glass4ab61742013-02-25 14:08:37 -0800237 *
238 * This is intended to be used by all ChromeOS EC drivers, but at present
239 * only SPI uses it. Once LPC uses the same protocol it can start using it.
240 * I2C could use it now, with a refactor of the existing code.
241 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200242 * Return: 0 on success or negative error code.
Simon Glass4ab61742013-02-25 14:08:37 -0800243 */
244int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
Bill Richardson5d4773e2014-06-18 11:14:02 -0700245 struct cros_ec_command *msg);
Simon Glass4ab61742013-02-25 14:08:37 -0800246
247/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200248 * cros_ec_check_result() - Check ec_msg->result.
249 * @ec_dev: EC device.
250 * @msg: Message to check.
Bill Richardson6db07b62014-06-18 11:14:05 -0700251 *
252 * This is used by ChromeOS EC drivers to check the ec_msg->result for
253 * errors and to warn about them.
254 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200255 * Return: 0 on success or negative error code.
Bill Richardson6db07b62014-06-18 11:14:05 -0700256 */
257int cros_ec_check_result(struct cros_ec_device *ec_dev,
258 struct cros_ec_command *msg);
259
260/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200261 * cros_ec_cmd_xfer() - Send a command to the ChromeOS EC.
262 * @ec_dev: EC device.
263 * @msg: Message to write.
Andrew Brestickera6551a72014-09-18 17:18:56 +0200264 *
265 * Call this to send a command to the ChromeOS EC. This should be used
266 * instead of calling the EC's cmd_xfer() callback directly.
267 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200268 * Return: 0 on success or negative error code.
Andrew Brestickera6551a72014-09-18 17:18:56 +0200269 */
270int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
271 struct cros_ec_command *msg);
272
273/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200274 * cros_ec_cmd_xfer_status() - Send a command to the ChromeOS EC.
275 * @ec_dev: EC device.
276 * @msg: Message to write.
Tomeu Vizoso9798ac62016-07-15 16:28:41 -0700277 *
278 * This function is identical to cros_ec_cmd_xfer, except it returns success
279 * status only if both the command was transmitted successfully and the EC
280 * replied with success status. It's not necessary to check msg->result when
281 * using this function.
282 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200283 * Return: The number of bytes transferred on success or negative error code.
Tomeu Vizoso9798ac62016-07-15 16:28:41 -0700284 */
285int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
286 struct cros_ec_command *msg);
287
288/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200289 * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
290 * @ec_dev: Device to register.
Simon Glass4ab61742013-02-25 14:08:37 -0800291 *
292 * Before calling this, allocate a pointer to a new device and then fill
293 * in all the fields up to the --private-- marker.
294 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200295 * Return: 0 on success or negative error code.
Simon Glass4ab61742013-02-25 14:08:37 -0800296 */
297int cros_ec_register(struct cros_ec_device *ec_dev);
298
Stephen Barber2c7589a2015-06-09 13:04:45 +0200299/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200300 * cros_ec_query_all() - Query the protocol version supported by the
301 * ChromeOS EC.
302 * @ec_dev: Device to register.
Stephen Barber2c7589a2015-06-09 13:04:45 +0200303 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200304 * Return: 0 on success or negative error code.
Stephen Barber2c7589a2015-06-09 13:04:45 +0200305 */
306int cros_ec_query_all(struct cros_ec_device *ec_dev);
307
Vic Yang6f1d9122016-08-10 19:05:24 +0200308/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200309 * cros_ec_get_next_event() - Fetch next event from the ChromeOS EC.
310 * @ec_dev: Device to fetch event from.
Shawn Nematbakhsh29d99b92017-02-14 20:58:02 +0100311 * @wake_event: Pointer to a bool set to true upon return if the event might be
312 * treated as a wake event. Ignored if null.
Vic Yang6f1d9122016-08-10 19:05:24 +0200313 *
Brian Norris475b0872018-11-07 18:49:38 -0800314 * Return: negative error code on errors; 0 for no data; or else number of
315 * bytes received (i.e., an event was retrieved successfully). Event types are
316 * written out to @ec_dev->event_data.event_type on success.
Vic Yang6f1d9122016-08-10 19:05:24 +0200317 */
Shawn Nematbakhsh29d99b92017-02-14 20:58:02 +0100318int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event);
Vic Yang6f1d9122016-08-10 19:05:24 +0200319
Gwendal Grignou68c35ea2017-05-16 17:46:48 +0200320/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200321 * cros_ec_get_host_event() - Return a mask of event set by the ChromeOS EC.
322 * @ec_dev: Device to fetch event from.
Gwendal Grignou68c35ea2017-05-16 17:46:48 +0200323 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200324 * When MKBP is supported, when the EC raises an interrupt, we collect the
325 * events raised and call the functions in the ec notifier. This function
326 * is a helper to know which events are raised.
Gwendal Grignou68c35ea2017-05-16 17:46:48 +0200327 *
Brian Norris475b0872018-11-07 18:49:38 -0800328 * Return: 0 on error or non-zero bitmask of one or more EC_HOST_EVENT_*.
Gwendal Grignou68c35ea2017-05-16 17:46:48 +0200329 */
330u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
331
Simon Glass4ab61742013-02-25 14:08:37 -0800332#endif /* __LINUX_MFD_CROS_EC_H */