blob: de8b588c8776da5526b78f66a107e871a0dd952c [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"
26#define CROS_EC_DEV_PD_NAME "cros_pd"
27
Simon Glass4ab61742013-02-25 14:08:37 -080028/*
Stephen Barberd3654072015-06-09 13:04:46 +020029 * 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/*
Stephen Barber2c7589a2015-06-09 13:04:45 +020035 * Max bus-specific overhead incurred by request/responses.
36 * I2C requires 1 additional byte for requests.
37 * I2C requires 2 additional bytes for responses.
Vic Yangb2376402017-03-24 18:44:01 +010038 * SPI requires up to 32 additional bytes for responses.
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +020039 */
Stephen Barber2c7589a2015-06-09 13:04:45 +020040#define EC_PROTO_VERSION_UNKNOWN 0
41#define EC_MAX_REQUEST_OVERHEAD 1
Vic Yangb2376402017-03-24 18:44:01 +010042#define EC_MAX_RESPONSE_OVERHEAD 32
Stephen Barber2c7589a2015-06-09 13:04:45 +020043
44/*
Simon Glass4ab61742013-02-25 14:08:37 -080045 * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
46 */
47enum {
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
Gwendal Grignou5d749d02016-03-08 09:13:52 -080054 /* Max length of messages for proto 2*/
55 EC_PROTO2_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
Bill Richardson5271db22014-04-30 10:44:08 -070056 EC_MSG_TX_PROTO_BYTES,
Gwendal Grignou5d749d02016-03-08 09:13:52 -080057
58 EC_MAX_MSG_BYTES = 64 * 1024,
Simon Glass4ab61742013-02-25 14:08:37 -080059};
60
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +020061/**
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.
Simon Glass4ab61742013-02-25 14:08:37 -080069 */
Bill Richardson5d4773e2014-06-18 11:14:02 -070070struct cros_ec_command {
71 uint32_t version;
72 uint32_t command;
Bill Richardson5d4773e2014-06-18 11:14:02 -070073 uint32_t outsize;
Bill Richardson5d4773e2014-06-18 11:14:02 -070074 uint32_t insize;
75 uint32_t result;
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020076 uint8_t data[0];
Simon Glass4ab61742013-02-25 14:08:37 -080077};
78
79/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +020080 * struct cros_ec_device - Information about a ChromeOS EC device.
81 * @phys_name: Name of physical comms layer (e.g. 'i2c-4').
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +010082 * @dev: Device pointer for physical comms device
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +020083 * @was_wake_device: True if this device was set to wake the system from
84 * sleep at the last suspend.
85 * @cros_class: The class structure for this device.
86 * @cmd_readmem: Direct read of the EC memory-mapped region, if supported.
87 * @offset: Is within EC_LPC_ADDR_MEMMAP region.
88 * @bytes: Number of bytes to read. zero means "read a string" (including
89 * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be
90 * read. Caller must ensure that the buffer is large enough for the
91 * result when reading a string.
92 * @max_request: Max size of message requested.
93 * @max_response: Max size of message response.
94 * @max_passthru: Max sice of passthru message.
95 * @proto_version: The protocol version used for this device.
96 * @priv: Private data.
97 * @irq: Interrupt to use.
98 * @id: Device id.
99 * @din: Input buffer (for data from 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 * @dout: Output buffer (for data to EC). This buffer will always be
106 * dword-aligned and include enough space for up to 7 word-alignment
107 * bytes also, so we can ensure that the body of the message is always
108 * dword-aligned (64-bit). We use this alignment to keep ARM and x86
109 * happy. Probably word alignment would be OK, there might be a small
110 * performance advantage to using dword.
111 * @din_size: Size of din buffer to allocate (zero to use static din).
112 * @dout_size: Size of dout buffer to allocate (zero to use static dout).
113 * @wake_enabled: True if this device can wake the system from sleep.
114 * @suspended: True if this device had been suspended.
115 * @cmd_xfer: Send command to EC and get response.
116 * Returns the number of bytes received if the communication
117 * succeeded, but that doesn't mean the EC was happy with the
118 * command. The caller should check msg.result for the EC's result
119 * code.
120 * @pkt_xfer: Send packet to EC and get response.
121 * @lock: One transaction at a time.
122 * @mkbp_event_supported: True if this EC supports the MKBP event protocol.
123 * @event_notifier: Interrupt event notifier for transport devices.
124 * @event_data: Raw payload transferred with the MKBP event.
125 * @event_size: Size in bytes of the event data.
126 * @host_event_wake_mask: Mask of host events that cause wake from suspend.
Simon Glass4ab61742013-02-25 14:08:37 -0800127 */
128struct cros_ec_device {
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700129 /* These are used by other drivers that want to talk to the EC */
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700130 const char *phys_name;
131 struct device *dev;
132 bool was_wake_device;
133 struct class *cros_class;
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +0100134 int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
135 unsigned int bytes, void *dest);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700136
137 /* These are used to implement the platform-specific interface */
Stephen Barber2c7589a2015-06-09 13:04:45 +0200138 u16 max_request;
139 u16 max_response;
140 u16 max_passthru;
141 u16 proto_version;
Simon Glass4ab61742013-02-25 14:08:37 -0800142 void *priv;
143 int irq;
Stephen Barber2c7589a2015-06-09 13:04:45 +0200144 u8 *din;
145 u8 *dout;
Simon Glass4ab61742013-02-25 14:08:37 -0800146 int din_size;
147 int dout_size;
Simon Glass4ab61742013-02-25 14:08:37 -0800148 bool wake_enabled;
Joseph Loa9eb1862016-12-16 18:57:36 +0100149 bool suspended;
Andrew Brestickera6551a72014-09-18 17:18:56 +0200150 int (*cmd_xfer)(struct cros_ec_device *ec,
151 struct cros_ec_command *msg);
Stephen Barber2c7589a2015-06-09 13:04:45 +0200152 int (*pkt_xfer)(struct cros_ec_device *ec,
153 struct cros_ec_command *msg);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700154 struct mutex lock;
Vic Yang6f1d9122016-08-10 19:05:24 +0200155 bool mkbp_event_supported;
156 struct blocking_notifier_head event_notifier;
157
Neil Armstrong57e94c82018-07-04 17:08:18 +0200158 struct ec_response_get_next_event_v1 event_data;
Vic Yang6f1d9122016-08-10 19:05:24 +0200159 int event_size;
Shawn Nematbakhsh29d99b92017-02-14 20:58:02 +0100160 u32 host_event_wake_mask;
Simon Glass4ab61742013-02-25 14:08:37 -0800161};
162
Enric Balletbo i Serra974e6f02016-08-01 11:54:35 +0200163/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200164 * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
Enric Balletbo i Serra974e6f02016-08-01 11:54:35 +0200165 * @sensor_num: Id of the sensor, as reported by the EC.
166 */
167struct cros_ec_sensor_platform {
168 u8 sensor_num;
169};
170
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200171/**
172 * struct cros_ec_platform - ChromeOS EC platform information.
173 * @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
174 * used in /dev/ and sysfs.
175 * @cmd_offset: Offset to apply for each command. Set when
176 * registering a device behind another one.
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200177 */
178struct cros_ec_platform {
179 const char *ec_name;
180 u16 cmd_offset;
181};
182
Eric Carusoe8626452017-05-16 17:46:48 +0200183struct cros_ec_debugfs;
184
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200185/**
186 * struct cros_ec_dev - ChromeOS EC device entry point.
187 * @class_dev: Device structure used in sysfs.
188 * @cdev: Character device structure in /dev.
189 * @ec_dev: cros_ec_device structure to talk to the physical device.
190 * @dev: Pointer to the platform device.
191 * @debug_info: cros_ec_debugfs structure for debugging information.
192 * @has_kb_wake_angle: True if at least 2 accelerometer are connected to the EC.
193 * @cmd_offset: Offset to apply for each command.
194 * @features: Features supported by the EC.
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200195 */
196struct cros_ec_dev {
197 struct device class_dev;
198 struct cdev cdev;
199 struct cros_ec_device *ec_dev;
200 struct device *dev;
Eric Carusoe8626452017-05-16 17:46:48 +0200201 struct cros_ec_debugfs *debug_info;
Gwendal Grignouc1d1e91a2018-03-23 18:42:47 +0100202 bool has_kb_wake_angle;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200203 u16 cmd_offset;
Vincent Palatine4244eb2016-08-01 11:54:37 +0200204 u32 features[2];
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200205};
206
Gwendal Grignou79a3d602018-05-30 09:04:13 -0700207#define to_cros_ec_dev(dev) container_of(dev, struct cros_ec_dev, class_dev)
208
Simon Glass4ab61742013-02-25 14:08:37 -0800209/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200210 * cros_ec_suspend() - Handle a suspend operation for the ChromeOS EC device.
211 * @ec_dev: Device to suspend.
Simon Glass4ab61742013-02-25 14:08:37 -0800212 *
213 * This can be called by drivers to handle a suspend event.
214 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200215 * Return: 0 on success or negative error code.
Simon Glass4ab61742013-02-25 14:08:37 -0800216 */
217int cros_ec_suspend(struct cros_ec_device *ec_dev);
218
219/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200220 * cros_ec_resume() - Handle a resume operation for the ChromeOS EC device.
221 * @ec_dev: Device to resume.
Simon Glass4ab61742013-02-25 14:08:37 -0800222 *
223 * This can be called by drivers to handle a resume event.
224 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200225 * Return: 0 on success or negative error code.
Simon Glass4ab61742013-02-25 14:08:37 -0800226 */
227int cros_ec_resume(struct cros_ec_device *ec_dev);
228
229/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200230 * cros_ec_prepare_tx() - Prepare an outgoing message in the output buffer.
231 * @ec_dev: Device to register.
232 * @msg: Message to write.
Simon Glass4ab61742013-02-25 14:08:37 -0800233 *
234 * This is intended to be used by all ChromeOS EC drivers, but at present
235 * only SPI uses it. Once LPC uses the same protocol it can start using it.
236 * I2C could use it now, with a refactor of the existing code.
237 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200238 * Return: 0 on success or negative error code.
Simon Glass4ab61742013-02-25 14:08:37 -0800239 */
240int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
Bill Richardson5d4773e2014-06-18 11:14:02 -0700241 struct cros_ec_command *msg);
Simon Glass4ab61742013-02-25 14:08:37 -0800242
243/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200244 * cros_ec_check_result() - Check ec_msg->result.
245 * @ec_dev: EC device.
246 * @msg: Message to check.
Bill Richardson6db07b62014-06-18 11:14:05 -0700247 *
248 * This is used by ChromeOS EC drivers to check the ec_msg->result for
249 * errors and to warn about them.
250 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200251 * Return: 0 on success or negative error code.
Bill Richardson6db07b62014-06-18 11:14:05 -0700252 */
253int cros_ec_check_result(struct cros_ec_device *ec_dev,
254 struct cros_ec_command *msg);
255
256/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200257 * cros_ec_cmd_xfer() - Send a command to the ChromeOS EC.
258 * @ec_dev: EC device.
259 * @msg: Message to write.
Andrew Brestickera6551a72014-09-18 17:18:56 +0200260 *
261 * Call this to send a command to the ChromeOS EC. This should be used
262 * instead of calling the EC's cmd_xfer() callback directly.
263 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200264 * Return: 0 on success or negative error code.
Andrew Brestickera6551a72014-09-18 17:18:56 +0200265 */
266int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
267 struct cros_ec_command *msg);
268
269/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200270 * cros_ec_cmd_xfer_status() - Send a command to the ChromeOS EC.
271 * @ec_dev: EC device.
272 * @msg: Message to write.
Tomeu Vizoso9798ac62016-07-15 16:28:41 -0700273 *
274 * This function is identical to cros_ec_cmd_xfer, except it returns success
275 * status only if both the command was transmitted successfully and the EC
276 * replied with success status. It's not necessary to check msg->result when
277 * using this function.
278 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200279 * Return: The number of bytes transferred on success or negative error code.
Tomeu Vizoso9798ac62016-07-15 16:28:41 -0700280 */
281int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
282 struct cros_ec_command *msg);
283
284/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200285 * cros_ec_remove() - Remove a ChromeOS EC.
286 * @ec_dev: Device to register.
Simon Glass4ab61742013-02-25 14:08:37 -0800287 *
Bill Richardsonee986622014-06-18 11:13:58 -0700288 * Call this to deregister a ChromeOS EC, then clean up any private data.
Simon Glass4ab61742013-02-25 14:08:37 -0800289 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200290 * Return: 0 on success or negative error code.
Simon Glass4ab61742013-02-25 14:08:37 -0800291 */
292int cros_ec_remove(struct cros_ec_device *ec_dev);
293
294/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200295 * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
296 * @ec_dev: Device to register.
Simon Glass4ab61742013-02-25 14:08:37 -0800297 *
298 * Before calling this, allocate a pointer to a new device and then fill
299 * in all the fields up to the --private-- marker.
300 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200301 * Return: 0 on success or negative error code.
Simon Glass4ab61742013-02-25 14:08:37 -0800302 */
303int cros_ec_register(struct cros_ec_device *ec_dev);
304
Stephen Barber2c7589a2015-06-09 13:04:45 +0200305/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200306 * cros_ec_query_all() - Query the protocol version supported by the
307 * ChromeOS EC.
308 * @ec_dev: Device to register.
Stephen Barber2c7589a2015-06-09 13:04:45 +0200309 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200310 * Return: 0 on success or negative error code.
Stephen Barber2c7589a2015-06-09 13:04:45 +0200311 */
312int cros_ec_query_all(struct cros_ec_device *ec_dev);
313
Vic Yang6f1d9122016-08-10 19:05:24 +0200314/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200315 * cros_ec_get_next_event() - Fetch next event from the ChromeOS EC.
316 * @ec_dev: Device to fetch event from.
Shawn Nematbakhsh29d99b92017-02-14 20:58:02 +0100317 * @wake_event: Pointer to a bool set to true upon return if the event might be
318 * treated as a wake event. Ignored if null.
Vic Yang6f1d9122016-08-10 19:05:24 +0200319 *
Brian Norris475b0872018-11-07 18:49:38 -0800320 * Return: negative error code on errors; 0 for no data; or else number of
321 * bytes received (i.e., an event was retrieved successfully). Event types are
322 * written out to @ec_dev->event_data.event_type on success.
Vic Yang6f1d9122016-08-10 19:05:24 +0200323 */
Shawn Nematbakhsh29d99b92017-02-14 20:58:02 +0100324int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event);
Vic Yang6f1d9122016-08-10 19:05:24 +0200325
Gwendal Grignou68c35ea2017-05-16 17:46:48 +0200326/**
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200327 * cros_ec_get_host_event() - Return a mask of event set by the ChromeOS EC.
328 * @ec_dev: Device to fetch event from.
Gwendal Grignou68c35ea2017-05-16 17:46:48 +0200329 *
Enric Balletbo i Serrae2bbf912018-07-18 18:09:56 +0200330 * When MKBP is supported, when the EC raises an interrupt, we collect the
331 * events raised and call the functions in the ec notifier. This function
332 * is a helper to know which events are raised.
Gwendal Grignou68c35ea2017-05-16 17:46:48 +0200333 *
Brian Norris475b0872018-11-07 18:49:38 -0800334 * Return: 0 on error or non-zero bitmask of one or more EC_HOST_EVENT_*.
Gwendal Grignou68c35ea2017-05-16 17:46:48 +0200335 */
336u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
337
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200338/* sysfs stuff */
339extern struct attribute_group cros_ec_attr_group;
340extern struct attribute_group cros_ec_lightbar_attr_group;
Emilio López18800fc2015-09-21 10:38:22 -0300341extern struct attribute_group cros_ec_vbc_attr_group;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200342
Thierry Escande5e011552017-11-20 17:15:26 +0100343/* debugfs stuff */
344int cros_ec_debugfs_init(struct cros_ec_dev *ec);
345void cros_ec_debugfs_remove(struct cros_ec_dev *ec);
Douglas Anderson44d99d72018-04-18 12:24:00 +0200346void cros_ec_debugfs_suspend(struct cros_ec_dev *ec);
347void cros_ec_debugfs_resume(struct cros_ec_dev *ec);
Archana Patnie04653a2017-02-01 17:22:03 +0100348
Simon Glass4ab61742013-02-25 14:08:37 -0800349#endif /* __LINUX_MFD_CROS_EC_H */