blob: 2d4e23c9ea0af54e19b40dab0942b1c610489dd7 [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.
Stephen Barber2c7589a2015-06-09 13:04:45 +020039 * */
40#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
Bill Richardson5d4773e2014-06-18 11:14:02 -070061/*
Simon Glass4ab61742013-02-25 14:08:37 -080062 * @version: Command version number (often 0)
Bill Richardson5d4773e2014-06-18 11:14:02 -070063 * @command: Command to send (EC_CMD_...)
Bill Richardson5d4773e2014-06-18 11:14:02 -070064 * @outsize: Outgoing length in bytes
Bill Richardson12ebc8a2014-06-18 11:14:06 -070065 * @insize: Max number of bytes to accept from EC
Bill Richardson5d4773e2014-06-18 11:14:02 -070066 * @result: EC's response to the command (separate from communication failure)
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020067 * @data: Where to put the incoming data from EC and outgoing data to EC
Simon Glass4ab61742013-02-25 14:08:37 -080068 */
Bill Richardson5d4773e2014-06-18 11:14:02 -070069struct cros_ec_command {
70 uint32_t version;
71 uint32_t command;
Bill Richardson5d4773e2014-06-18 11:14:02 -070072 uint32_t outsize;
Bill Richardson5d4773e2014-06-18 11:14:02 -070073 uint32_t insize;
74 uint32_t result;
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020075 uint8_t data[0];
Simon Glass4ab61742013-02-25 14:08:37 -080076};
77
78/**
79 * struct cros_ec_device - Information about a ChromeOS EC device
80 *
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070081 * @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
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070083 * @was_wake_device: true if this device was set to wake the system from
84 * sleep at the last suspend
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +010085 * @cmd_readmem: direct read of the EC memory-mapped region, if supported
86 * @offset is within EC_LPC_ADDR_MEMMAP region.
87 * @bytes: number of bytes to read. zero means "read a string" (including
88 * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be read.
89 * Caller must ensure that the buffer is large enough for the result when
90 * reading a string.
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070091 *
Simon Glass4ab61742013-02-25 14:08:37 -080092 * @priv: Private data
93 * @irq: Interrupt to use
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020094 * @id: Device id
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070095 * @din: input buffer (for data from EC)
96 * @dout: output buffer (for data to EC)
Simon Glass4ab61742013-02-25 14:08:37 -080097 * \note
98 * These two buffers will always be dword-aligned and include enough
99 * space for up to 7 word-alignment bytes also, so we can ensure that
100 * the body of the message is always dword-aligned (64-bit).
Simon Glass4ab61742013-02-25 14:08:37 -0800101 * We use this alignment to keep ARM and x86 happy. Probably word
102 * alignment would be OK, there might be a small performance advantage
103 * to using dword.
Bill Richardson2ce701a2014-06-18 11:13:59 -0700104 * @din_size: size of din buffer to allocate (zero to use static din)
105 * @dout_size: size of dout buffer to allocate (zero to use static dout)
Simon Glass4ab61742013-02-25 14:08:37 -0800106 * @wake_enabled: true if this device can wake the system from sleep
Joseph Loa9eb1862016-12-16 18:57:36 +0100107 * @suspended: true if this device had been suspended
Andrew Brestickera6551a72014-09-18 17:18:56 +0200108 * @cmd_xfer: send command to EC and get response
109 * Returns the number of bytes received if the communication succeeded, but
110 * that doesn't mean the EC was happy with the command. The caller
111 * should check msg.result for the EC's result code.
Stephen Barber2c7589a2015-06-09 13:04:45 +0200112 * @pkt_xfer: send packet to EC and get response
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700113 * @lock: one transaction at a time
Vic Yang6f1d9122016-08-10 19:05:24 +0200114 * @mkbp_event_supported: true if this EC supports the MKBP event protocol.
115 * @event_notifier: interrupt event notifier for transport devices.
116 * @event_data: raw payload transferred with the MKBP event.
117 * @event_size: size in bytes of the event data.
Simon Glass4ab61742013-02-25 14:08:37 -0800118 */
119struct cros_ec_device {
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700120
121 /* These are used by other drivers that want to talk to the EC */
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700122 const char *phys_name;
123 struct device *dev;
124 bool was_wake_device;
125 struct class *cros_class;
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +0100126 int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
127 unsigned int bytes, void *dest);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700128
129 /* These are used to implement the platform-specific interface */
Stephen Barber2c7589a2015-06-09 13:04:45 +0200130 u16 max_request;
131 u16 max_response;
132 u16 max_passthru;
133 u16 proto_version;
Simon Glass4ab61742013-02-25 14:08:37 -0800134 void *priv;
135 int irq;
Stephen Barber2c7589a2015-06-09 13:04:45 +0200136 u8 *din;
137 u8 *dout;
Simon Glass4ab61742013-02-25 14:08:37 -0800138 int din_size;
139 int dout_size;
Simon Glass4ab61742013-02-25 14:08:37 -0800140 bool wake_enabled;
Joseph Loa9eb1862016-12-16 18:57:36 +0100141 bool suspended;
Andrew Brestickera6551a72014-09-18 17:18:56 +0200142 int (*cmd_xfer)(struct cros_ec_device *ec,
143 struct cros_ec_command *msg);
Stephen Barber2c7589a2015-06-09 13:04:45 +0200144 int (*pkt_xfer)(struct cros_ec_device *ec,
145 struct cros_ec_command *msg);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700146 struct mutex lock;
Vic Yang6f1d9122016-08-10 19:05:24 +0200147 bool mkbp_event_supported;
148 struct blocking_notifier_head event_notifier;
149
150 struct ec_response_get_next_event event_data;
151 int event_size;
Shawn Nematbakhsh29d99b92017-02-14 20:58:02 +0100152 u32 host_event_wake_mask;
Simon Glass4ab61742013-02-25 14:08:37 -0800153};
154
Enric Balletbo i Serra974e6f02016-08-01 11:54:35 +0200155/**
156 * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information
157 *
158 * @sensor_num: Id of the sensor, as reported by the EC.
159 */
160struct cros_ec_sensor_platform {
161 u8 sensor_num;
162};
163
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200164/* struct cros_ec_platform - ChromeOS EC platform information
165 *
166 * @ec_name: name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
167 * used in /dev/ and sysfs.
168 * @cmd_offset: offset to apply for each command. Set when
169 * registering a devicde behind another one.
170 */
171struct cros_ec_platform {
172 const char *ec_name;
173 u16 cmd_offset;
174};
175
Eric Carusoe8626452017-05-16 17:46:48 +0200176struct cros_ec_debugfs;
177
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200178/*
179 * struct cros_ec_dev - ChromeOS EC device entry point
180 *
181 * @class_dev: Device structure used in sysfs
182 * @cdev: Character device structure in /dev
183 * @ec_dev: cros_ec_device structure to talk to the physical device
184 * @dev: pointer to the platform device
Eric Carusoe8626452017-05-16 17:46:48 +0200185 * @debug_info: cros_ec_debugfs structure for debugging information
Gwendal Grignouc1d1e91a2018-03-23 18:42:47 +0100186 * @has_kb_wake_angle: true if at least 2 accelerometer are connected to the EC.
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200187 * @cmd_offset: offset to apply for each command.
188 */
189struct cros_ec_dev {
190 struct device class_dev;
191 struct cdev cdev;
192 struct cros_ec_device *ec_dev;
193 struct device *dev;
Eric Carusoe8626452017-05-16 17:46:48 +0200194 struct cros_ec_debugfs *debug_info;
Gwendal Grignouc1d1e91a2018-03-23 18:42:47 +0100195 bool has_kb_wake_angle;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200196 u16 cmd_offset;
Vincent Palatine4244eb2016-08-01 11:54:37 +0200197 u32 features[2];
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200198};
199
Simon Glass4ab61742013-02-25 14:08:37 -0800200/**
201 * cros_ec_suspend - Handle a suspend operation for the ChromeOS EC device
202 *
203 * This can be called by drivers to handle a suspend event.
204 *
205 * ec_dev: Device to suspend
206 * @return 0 if ok, -ve on error
207 */
208int cros_ec_suspend(struct cros_ec_device *ec_dev);
209
210/**
211 * cros_ec_resume - Handle a resume operation for the ChromeOS EC device
212 *
213 * This can be called by drivers to handle a resume event.
214 *
215 * @ec_dev: Device to resume
216 * @return 0 if ok, -ve on error
217 */
218int cros_ec_resume(struct cros_ec_device *ec_dev);
219
220/**
221 * cros_ec_prepare_tx - Prepare an outgoing message in the output buffer
222 *
223 * This is intended to be used by all ChromeOS EC drivers, but at present
224 * only SPI uses it. Once LPC uses the same protocol it can start using it.
225 * I2C could use it now, with a refactor of the existing code.
226 *
227 * @ec_dev: Device to register
228 * @msg: Message to write
229 */
230int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
Bill Richardson5d4773e2014-06-18 11:14:02 -0700231 struct cros_ec_command *msg);
Simon Glass4ab61742013-02-25 14:08:37 -0800232
233/**
Bill Richardson6db07b62014-06-18 11:14:05 -0700234 * cros_ec_check_result - Check ec_msg->result
235 *
236 * This is used by ChromeOS EC drivers to check the ec_msg->result for
237 * errors and to warn about them.
238 *
239 * @ec_dev: EC device
240 * @msg: Message to check
241 */
242int cros_ec_check_result(struct cros_ec_device *ec_dev,
243 struct cros_ec_command *msg);
244
245/**
Andrew Brestickera6551a72014-09-18 17:18:56 +0200246 * cros_ec_cmd_xfer - Send a command to the ChromeOS EC
247 *
248 * Call this to send a command to the ChromeOS EC. This should be used
249 * instead of calling the EC's cmd_xfer() callback directly.
250 *
251 * @ec_dev: EC device
252 * @msg: Message to write
253 */
254int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
255 struct cros_ec_command *msg);
256
257/**
Tomeu Vizoso9798ac62016-07-15 16:28:41 -0700258 * cros_ec_cmd_xfer_status - Send a command to the ChromeOS EC
259 *
260 * This function is identical to cros_ec_cmd_xfer, except it returns success
261 * status only if both the command was transmitted successfully and the EC
262 * replied with success status. It's not necessary to check msg->result when
263 * using this function.
264 *
265 * @ec_dev: EC device
266 * @msg: Message to write
267 * @return: Num. of bytes transferred on success, <0 on failure
268 */
269int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
270 struct cros_ec_command *msg);
271
272/**
Simon Glass4ab61742013-02-25 14:08:37 -0800273 * cros_ec_remove - Remove a ChromeOS EC
274 *
Bill Richardsonee986622014-06-18 11:13:58 -0700275 * Call this to deregister a ChromeOS EC, then clean up any private data.
Simon Glass4ab61742013-02-25 14:08:37 -0800276 *
277 * @ec_dev: Device to register
278 * @return 0 if ok, -ve on error
279 */
280int cros_ec_remove(struct cros_ec_device *ec_dev);
281
282/**
283 * cros_ec_register - Register a new ChromeOS EC, using the provided info
284 *
285 * Before calling this, allocate a pointer to a new device and then fill
286 * in all the fields up to the --private-- marker.
287 *
288 * @ec_dev: Device to register
289 * @return 0 if ok, -ve on error
290 */
291int cros_ec_register(struct cros_ec_device *ec_dev);
292
Stephen Barber2c7589a2015-06-09 13:04:45 +0200293/**
Tomeu Vizosof4bcf5a2016-02-05 14:32:56 +0100294 * cros_ec_query_all - Query the protocol version supported by the ChromeOS EC
Stephen Barber2c7589a2015-06-09 13:04:45 +0200295 *
296 * @ec_dev: Device to register
297 * @return 0 if ok, -ve on error
298 */
299int cros_ec_query_all(struct cros_ec_device *ec_dev);
300
Vic Yang6f1d9122016-08-10 19:05:24 +0200301/**
302 * cros_ec_get_next_event - Fetch next event from the ChromeOS EC
303 *
304 * @ec_dev: Device to fetch event from
Shawn Nematbakhsh29d99b92017-02-14 20:58:02 +0100305 * @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.
Vic Yang6f1d9122016-08-10 19:05:24 +0200307 *
308 * Returns: 0 on success, Linux error number on failure
309 */
Shawn Nematbakhsh29d99b92017-02-14 20:58:02 +0100310int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event);
Vic Yang6f1d9122016-08-10 19:05:24 +0200311
Gwendal Grignou68c35ea2017-05-16 17:46:48 +0200312/**
313 * cros_ec_get_host_event - Return a mask of event set by the EC.
314 *
315 * When MKBP is supported, when the EC raises an interrupt,
316 * We collect the events raised and call the functions in the ec notifier.
317 *
318 * This function is a helper to know which events are raised.
319 */
320u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
321
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200322/* sysfs stuff */
323extern struct attribute_group cros_ec_attr_group;
324extern struct attribute_group cros_ec_lightbar_attr_group;
Emilio López18800fc2015-09-21 10:38:22 -0300325extern struct attribute_group cros_ec_vbc_attr_group;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200326
Thierry Escande5e011552017-11-20 17:15:26 +0100327/* debugfs stuff */
328int cros_ec_debugfs_init(struct cros_ec_dev *ec);
329void cros_ec_debugfs_remove(struct cros_ec_dev *ec);
330
Archana Patnie04653a2017-02-01 17:22:03 +0100331/* ACPI GPE handler */
332#ifdef CONFIG_ACPI
333
334int cros_ec_acpi_install_gpe_handler(struct device *dev);
335void cros_ec_acpi_remove_gpe_handler(void);
336void cros_ec_acpi_clear_gpe(void);
337
338#else /* CONFIG_ACPI */
339
340static inline int cros_ec_acpi_install_gpe_handler(struct device *dev)
341{
342 return -ENODEV;
343}
344static inline void cros_ec_acpi_remove_gpe_handler(void) {}
345static inline void cros_ec_acpi_clear_gpe(void) {}
346
347#endif /* CONFIG_ACPI */
348
Simon Glass4ab61742013-02-25 14:08:37 -0800349#endif /* __LINUX_MFD_CROS_EC_H */