blob: cfa78bb4990f7547470e9ebcfd0183ad13295e77 [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"
Enric Balletbo i Serra4f8f2bb2019-05-08 11:19:56 +020028#define CROS_EC_DEV_TP_NAME "cros_tp"
Rushikesh S Kadamd4cee952019-03-01 13:50:54 +053029#define CROS_EC_DEV_ISH_NAME "cros_ish"
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020030
Simon Glass4ab61742013-02-25 14:08:37 -080031/*
Stephen Barberd3654072015-06-09 13:04:46 +020032 * The EC is unresponsive for a time after a reboot command. Add a
33 * simple delay to make sure that the bus stays locked.
34 */
35#define EC_REBOOT_DELAY_MS 50
36
37/*
Stephen Barber2c7589a2015-06-09 13:04:45 +020038 * Max bus-specific overhead incurred by request/responses.
39 * I2C requires 1 additional byte for requests.
40 * I2C requires 2 additional bytes for responses.
Vic Yangb2376402017-03-24 18:44:01 +010041 * SPI requires up to 32 additional bytes for responses.
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +020042 */
Stephen Barber2c7589a2015-06-09 13:04:45 +020043#define EC_PROTO_VERSION_UNKNOWN 0
44#define EC_MAX_REQUEST_OVERHEAD 1
Vic Yangb2376402017-03-24 18:44:01 +010045#define EC_MAX_RESPONSE_OVERHEAD 32
Stephen Barber2c7589a2015-06-09 13:04:45 +020046
47/*
Simon Glass4ab61742013-02-25 14:08:37 -080048 * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
49 */
50enum {
51 EC_MSG_TX_HEADER_BYTES = 3,
52 EC_MSG_TX_TRAILER_BYTES = 1,
53 EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES +
54 EC_MSG_TX_TRAILER_BYTES,
55 EC_MSG_RX_PROTO_BYTES = 3,
56
Gwendal Grignou5d749d02016-03-08 09:13:52 -080057 /* Max length of messages for proto 2*/
58 EC_PROTO2_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
Bill Richardson5271db22014-04-30 10:44:08 -070059 EC_MSG_TX_PROTO_BYTES,
Gwendal Grignou5d749d02016-03-08 09:13:52 -080060
61 EC_MAX_MSG_BYTES = 64 * 1024,
Simon Glass4ab61742013-02-25 14:08:37 -080062};
63
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +020064/**
65 * struct cros_ec_command - Information about a ChromeOS EC command.
66 * @version: Command version number (often 0).
67 * @command: Command to send (EC_CMD_...).
68 * @outsize: Outgoing length in bytes.
69 * @insize: Max number of bytes to accept from the EC.
70 * @result: EC's response to the command (separate from communication failure).
71 * @data: Where to put the incoming data from EC and outgoing data to EC.
Simon Glass4ab61742013-02-25 14:08:37 -080072 */
Bill Richardson5d4773e2014-06-18 11:14:02 -070073struct cros_ec_command {
74 uint32_t version;
75 uint32_t command;
Bill Richardson5d4773e2014-06-18 11:14:02 -070076 uint32_t outsize;
Bill Richardson5d4773e2014-06-18 11:14:02 -070077 uint32_t insize;
78 uint32_t result;
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020079 uint8_t data[0];
Simon Glass4ab61742013-02-25 14:08:37 -080080};
81
82/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +020083 * struct cros_ec_device - Information about a ChromeOS EC device.
84 * @phys_name: Name of physical comms layer (e.g. 'i2c-4').
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +010085 * @dev: Device pointer for physical comms device
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +020086 * @was_wake_device: True if this device was set to wake the system from
87 * sleep at the last suspend.
88 * @cros_class: The class structure for this device.
89 * @cmd_readmem: Direct read of the EC memory-mapped region, if supported.
90 * @offset: Is within EC_LPC_ADDR_MEMMAP region.
91 * @bytes: Number of bytes to read. zero means "read a string" (including
92 * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be
93 * read. Caller must ensure that the buffer is large enough for the
94 * result when reading a string.
95 * @max_request: Max size of message requested.
96 * @max_response: Max size of message response.
97 * @max_passthru: Max sice of passthru message.
98 * @proto_version: The protocol version used for this device.
99 * @priv: Private data.
100 * @irq: Interrupt to use.
101 * @id: Device id.
102 * @din: Input buffer (for data from EC). This buffer will always be
103 * dword-aligned and include enough space for up to 7 word-alignment
104 * bytes also, so we can ensure that the body of the message is always
105 * dword-aligned (64-bit). We use this alignment to keep ARM and x86
106 * happy. Probably word alignment would be OK, there might be a small
107 * performance advantage to using dword.
108 * @dout: Output buffer (for data to EC). This buffer will always be
109 * dword-aligned and include enough space for up to 7 word-alignment
110 * bytes also, so we can ensure that the body of the message is always
111 * dword-aligned (64-bit). We use this alignment to keep ARM and x86
112 * happy. Probably word alignment would be OK, there might be a small
113 * performance advantage to using dword.
114 * @din_size: Size of din buffer to allocate (zero to use static din).
115 * @dout_size: Size of dout buffer to allocate (zero to use static dout).
116 * @wake_enabled: True if this device can wake the system from sleep.
117 * @suspended: True if this device had been suspended.
118 * @cmd_xfer: Send command to EC and get response.
119 * Returns the number of bytes received if the communication
120 * succeeded, but that doesn't mean the EC was happy with the
121 * command. The caller should check msg.result for the EC's result
122 * code.
123 * @pkt_xfer: Send packet to EC and get response.
124 * @lock: One transaction at a time.
125 * @mkbp_event_supported: True if this EC supports the MKBP event protocol.
Evan Green72355602019-04-03 14:34:28 -0700126 * @host_sleep_v1: True if this EC supports the sleep v1 command.
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200127 * @event_notifier: Interrupt event notifier for transport devices.
128 * @event_data: Raw payload transferred with the MKBP event.
129 * @event_size: Size in bytes of the event data.
130 * @host_event_wake_mask: Mask of host events that cause wake from suspend.
Simon Glass4ab61742013-02-25 14:08:37 -0800131 */
132struct cros_ec_device {
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700133 /* These are used by other drivers that want to talk to the EC */
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700134 const char *phys_name;
135 struct device *dev;
136 bool was_wake_device;
137 struct class *cros_class;
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +0100138 int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
139 unsigned int bytes, void *dest);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700140
141 /* These are used to implement the platform-specific interface */
Stephen Barber2c7589a2015-06-09 13:04:45 +0200142 u16 max_request;
143 u16 max_response;
144 u16 max_passthru;
145 u16 proto_version;
Simon Glass4ab61742013-02-25 14:08:37 -0800146 void *priv;
147 int irq;
Stephen Barber2c7589a2015-06-09 13:04:45 +0200148 u8 *din;
149 u8 *dout;
Simon Glass4ab61742013-02-25 14:08:37 -0800150 int din_size;
151 int dout_size;
Simon Glass4ab61742013-02-25 14:08:37 -0800152 bool wake_enabled;
Joseph Loa9eb1862016-12-16 18:57:36 +0100153 bool suspended;
Andrew Brestickera6551a72014-09-18 17:18:56 +0200154 int (*cmd_xfer)(struct cros_ec_device *ec,
155 struct cros_ec_command *msg);
Stephen Barber2c7589a2015-06-09 13:04:45 +0200156 int (*pkt_xfer)(struct cros_ec_device *ec,
157 struct cros_ec_command *msg);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700158 struct mutex lock;
Vic Yang6f1d9122016-08-10 19:05:24 +0200159 bool mkbp_event_supported;
Evan Green72355602019-04-03 14:34:28 -0700160 bool host_sleep_v1;
Vic Yang6f1d9122016-08-10 19:05:24 +0200161 struct blocking_notifier_head event_notifier;
162
Neil Armstrong57e94c82018-07-04 17:08:18 +0200163 struct ec_response_get_next_event_v1 event_data;
Vic Yang6f1d9122016-08-10 19:05:24 +0200164 int event_size;
Shawn Nematbakhsh29d99b92017-02-14 20:58:02 +0100165 u32 host_event_wake_mask;
Simon Glass4ab61742013-02-25 14:08:37 -0800166};
167
Enric Balletbo i Serra974e6f02016-08-01 11:54:35 +0200168/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200169 * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
Enric Balletbo i Serra974e6f02016-08-01 11:54:35 +0200170 * @sensor_num: Id of the sensor, as reported by the EC.
171 */
172struct cros_ec_sensor_platform {
173 u8 sensor_num;
174};
175
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200176/**
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.
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200182 */
183struct cros_ec_platform {
184 const char *ec_name;
185 u16 cmd_offset;
186};
187
Eric Carusoe8626452017-05-16 17:46:48 +0200188struct cros_ec_debugfs;
189
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200190/**
191 * struct cros_ec_dev - ChromeOS EC device entry point.
192 * @class_dev: Device structure used in sysfs.
193 * @cdev: Character device structure in /dev.
194 * @ec_dev: cros_ec_device structure to talk to the physical device.
195 * @dev: Pointer to the platform device.
196 * @debug_info: cros_ec_debugfs structure for debugging information.
197 * @has_kb_wake_angle: True if at least 2 accelerometer are connected to the EC.
198 * @cmd_offset: Offset to apply for each command.
199 * @features: Features supported by the EC.
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200200 */
201struct cros_ec_dev {
202 struct device class_dev;
203 struct cdev cdev;
204 struct cros_ec_device *ec_dev;
205 struct device *dev;
Eric Carusoe8626452017-05-16 17:46:48 +0200206 struct cros_ec_debugfs *debug_info;
Gwendal Grignouc1d1e91a2018-03-23 18:42:47 +0100207 bool has_kb_wake_angle;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200208 u16 cmd_offset;
Vincent Palatine4244eb2016-08-01 11:54:37 +0200209 u32 features[2];
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200210};
211
Gwendal Grignou79a3d602018-05-30 09:04:13 -0700212#define to_cros_ec_dev(dev) container_of(dev, struct cros_ec_dev, class_dev)
213
Simon Glass4ab61742013-02-25 14:08:37 -0800214/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200215 * cros_ec_suspend() - Handle a suspend operation for the ChromeOS EC device.
216 * @ec_dev: Device to suspend.
Simon Glass4ab61742013-02-25 14:08:37 -0800217 *
218 * This can be called by drivers to handle a suspend event.
219 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200220 * Return: 0 on success or negative error code.
Simon Glass4ab61742013-02-25 14:08:37 -0800221 */
222int cros_ec_suspend(struct cros_ec_device *ec_dev);
223
224/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200225 * cros_ec_resume() - Handle a resume operation for the ChromeOS EC device.
226 * @ec_dev: Device to resume.
Simon Glass4ab61742013-02-25 14:08:37 -0800227 *
228 * This can be called by drivers to handle a resume event.
229 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200230 * Return: 0 on success or negative error code.
Simon Glass4ab61742013-02-25 14:08:37 -0800231 */
232int cros_ec_resume(struct cros_ec_device *ec_dev);
233
234/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200235 * cros_ec_prepare_tx() - Prepare an outgoing message in the output buffer.
236 * @ec_dev: Device to register.
237 * @msg: Message to write.
Simon Glass4ab61742013-02-25 14:08:37 -0800238 *
239 * This is intended to be used by all ChromeOS EC drivers, but at present
240 * only SPI uses it. Once LPC uses the same protocol it can start using it.
241 * I2C could use it now, with a refactor of the existing code.
242 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200243 * Return: 0 on success or negative error code.
Simon Glass4ab61742013-02-25 14:08:37 -0800244 */
245int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
Bill Richardson5d4773e2014-06-18 11:14:02 -0700246 struct cros_ec_command *msg);
Simon Glass4ab61742013-02-25 14:08:37 -0800247
248/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200249 * cros_ec_check_result() - Check ec_msg->result.
250 * @ec_dev: EC device.
251 * @msg: Message to check.
Bill Richardson6db07b62014-06-18 11:14:05 -0700252 *
253 * This is used by ChromeOS EC drivers to check the ec_msg->result for
254 * errors and to warn about them.
255 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200256 * Return: 0 on success or negative error code.
Bill Richardson6db07b62014-06-18 11:14:05 -0700257 */
258int cros_ec_check_result(struct cros_ec_device *ec_dev,
259 struct cros_ec_command *msg);
260
261/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200262 * cros_ec_cmd_xfer() - Send a command to the ChromeOS EC.
263 * @ec_dev: EC device.
264 * @msg: Message to write.
Andrew Brestickera6551a72014-09-18 17:18:56 +0200265 *
266 * Call this to send a command to the ChromeOS EC. This should be used
267 * instead of calling the EC's cmd_xfer() callback directly.
268 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200269 * Return: 0 on success or negative error code.
Andrew Brestickera6551a72014-09-18 17:18:56 +0200270 */
271int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
272 struct cros_ec_command *msg);
273
274/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200275 * cros_ec_cmd_xfer_status() - Send a command to the ChromeOS EC.
276 * @ec_dev: EC device.
277 * @msg: Message to write.
Tomeu Vizoso9798ac62016-07-15 16:28:41 -0700278 *
279 * This function is identical to cros_ec_cmd_xfer, except it returns success
280 * status only if both the command was transmitted successfully and the EC
281 * replied with success status. It's not necessary to check msg->result when
282 * using this function.
283 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200284 * Return: The number of bytes transferred on success or negative error code.
Tomeu Vizoso9798ac62016-07-15 16:28:41 -0700285 */
286int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
287 struct cros_ec_command *msg);
288
289/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200290 * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
291 * @ec_dev: Device to register.
Simon Glass4ab61742013-02-25 14:08:37 -0800292 *
293 * Before calling this, allocate a pointer to a new device and then fill
294 * in all the fields up to the --private-- marker.
295 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200296 * Return: 0 on success or negative error code.
Simon Glass4ab61742013-02-25 14:08:37 -0800297 */
298int cros_ec_register(struct cros_ec_device *ec_dev);
299
Stephen Barber2c7589a2015-06-09 13:04:45 +0200300/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200301 * cros_ec_query_all() - Query the protocol version supported by the
302 * ChromeOS EC.
303 * @ec_dev: Device to register.
Stephen Barber2c7589a2015-06-09 13:04:45 +0200304 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200305 * Return: 0 on success or negative error code.
Stephen Barber2c7589a2015-06-09 13:04:45 +0200306 */
307int cros_ec_query_all(struct cros_ec_device *ec_dev);
308
Vic Yang6f1d9122016-08-10 19:05:24 +0200309/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200310 * cros_ec_get_next_event() - Fetch next event from the ChromeOS EC.
311 * @ec_dev: Device to fetch event from.
Shawn Nematbakhsh29d99b92017-02-14 20:58:02 +0100312 * @wake_event: Pointer to a bool set to true upon return if the event might be
313 * treated as a wake event. Ignored if null.
Vic Yang6f1d9122016-08-10 19:05:24 +0200314 *
Brian Norris475b0872018-11-07 18:49:38 -0800315 * Return: negative error code on errors; 0 for no data; or else number of
316 * bytes received (i.e., an event was retrieved successfully). Event types are
317 * written out to @ec_dev->event_data.event_type on success.
Vic Yang6f1d9122016-08-10 19:05:24 +0200318 */
Shawn Nematbakhsh29d99b92017-02-14 20:58:02 +0100319int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event);
Vic Yang6f1d9122016-08-10 19:05:24 +0200320
Gwendal Grignou68c35ea2017-05-16 17:46:48 +0200321/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200322 * cros_ec_get_host_event() - Return a mask of event set by the ChromeOS EC.
323 * @ec_dev: Device to fetch event from.
Gwendal Grignou68c35ea2017-05-16 17:46:48 +0200324 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200325 * When MKBP is supported, when the EC raises an interrupt, we collect the
326 * events raised and call the functions in the ec notifier. This function
327 * is a helper to know which events are raised.
Gwendal Grignou68c35ea2017-05-16 17:46:48 +0200328 *
Brian Norris475b0872018-11-07 18:49:38 -0800329 * Return: 0 on error or non-zero bitmask of one or more EC_HOST_EVENT_*.
Gwendal Grignou68c35ea2017-05-16 17:46:48 +0200330 */
331u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
332
Simon Glass4ab61742013-02-25 14:08:37 -0800333#endif /* __LINUX_MFD_CROS_EC_H */