blob: 7a01c94496f14eac3c1ed13b37a9811cc1b6f853 [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.
38 * */
39#define EC_PROTO_VERSION_UNKNOWN 0
40#define EC_MAX_REQUEST_OVERHEAD 1
41#define EC_MAX_RESPONSE_OVERHEAD 2
42
43/*
Simon Glass4ab61742013-02-25 14:08:37 -080044 * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
45 */
46enum {
47 EC_MSG_TX_HEADER_BYTES = 3,
48 EC_MSG_TX_TRAILER_BYTES = 1,
49 EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES +
50 EC_MSG_TX_TRAILER_BYTES,
51 EC_MSG_RX_PROTO_BYTES = 3,
52
Gwendal Grignou5d749d02016-03-08 09:13:52 -080053 /* Max length of messages for proto 2*/
54 EC_PROTO2_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
Bill Richardson5271db22014-04-30 10:44:08 -070055 EC_MSG_TX_PROTO_BYTES,
Gwendal Grignou5d749d02016-03-08 09:13:52 -080056
57 EC_MAX_MSG_BYTES = 64 * 1024,
Simon Glass4ab61742013-02-25 14:08:37 -080058};
59
Bill Richardson5d4773e2014-06-18 11:14:02 -070060/*
Simon Glass4ab61742013-02-25 14:08:37 -080061 * @version: Command version number (often 0)
Bill Richardson5d4773e2014-06-18 11:14:02 -070062 * @command: Command to send (EC_CMD_...)
Bill Richardson5d4773e2014-06-18 11:14:02 -070063 * @outsize: Outgoing length in bytes
Bill Richardson12ebc8a2014-06-18 11:14:06 -070064 * @insize: Max number of bytes to accept from EC
Bill Richardson5d4773e2014-06-18 11:14:02 -070065 * @result: EC's response to the command (separate from communication failure)
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020066 * @data: Where to put the incoming data from EC and outgoing data to EC
Simon Glass4ab61742013-02-25 14:08:37 -080067 */
Bill Richardson5d4773e2014-06-18 11:14:02 -070068struct cros_ec_command {
69 uint32_t version;
70 uint32_t command;
Bill Richardson5d4773e2014-06-18 11:14:02 -070071 uint32_t outsize;
Bill Richardson5d4773e2014-06-18 11:14:02 -070072 uint32_t insize;
73 uint32_t result;
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020074 uint8_t data[0];
Simon Glass4ab61742013-02-25 14:08:37 -080075};
76
77/**
78 * struct cros_ec_device - Information about a ChromeOS EC device
79 *
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070080 * @phys_name: name of physical comms layer (e.g. 'i2c-4')
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +010081 * @dev: Device pointer for physical comms device
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070082 * @was_wake_device: true if this device was set to wake the system from
83 * sleep at the last suspend
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +010084 * @cmd_readmem: direct read of the EC memory-mapped region, if supported
85 * @offset is within EC_LPC_ADDR_MEMMAP region.
86 * @bytes: number of bytes to read. zero means "read a string" (including
87 * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be read.
88 * Caller must ensure that the buffer is large enough for the result when
89 * reading a string.
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070090 *
Simon Glass4ab61742013-02-25 14:08:37 -080091 * @priv: Private data
92 * @irq: Interrupt to use
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020093 * @id: Device id
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070094 * @din: input buffer (for data from EC)
95 * @dout: output buffer (for data to EC)
Simon Glass4ab61742013-02-25 14:08:37 -080096 * \note
97 * These two buffers will always be dword-aligned and include enough
98 * space for up to 7 word-alignment bytes also, so we can ensure that
99 * the body of the message is always dword-aligned (64-bit).
Simon Glass4ab61742013-02-25 14:08:37 -0800100 * We use this alignment to keep ARM and x86 happy. Probably word
101 * alignment would be OK, there might be a small performance advantage
102 * to using dword.
Bill Richardson2ce701a2014-06-18 11:13:59 -0700103 * @din_size: size of din buffer to allocate (zero to use static din)
104 * @dout_size: size of dout buffer to allocate (zero to use static dout)
Simon Glass4ab61742013-02-25 14:08:37 -0800105 * @wake_enabled: true if this device can wake the system from sleep
Joseph Loa9eb1862016-12-16 18:57:36 +0100106 * @suspended: true if this device had been suspended
Andrew Brestickera6551a72014-09-18 17:18:56 +0200107 * @cmd_xfer: send command to EC and get response
108 * Returns the number of bytes received if the communication succeeded, but
109 * that doesn't mean the EC was happy with the command. The caller
110 * should check msg.result for the EC's result code.
Stephen Barber2c7589a2015-06-09 13:04:45 +0200111 * @pkt_xfer: send packet to EC and get response
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700112 * @lock: one transaction at a time
Vic Yang6f1d9122016-08-10 19:05:24 +0200113 * @mkbp_event_supported: true if this EC supports the MKBP event protocol.
114 * @event_notifier: interrupt event notifier for transport devices.
115 * @event_data: raw payload transferred with the MKBP event.
116 * @event_size: size in bytes of the event data.
Simon Glass4ab61742013-02-25 14:08:37 -0800117 */
118struct cros_ec_device {
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700119
120 /* These are used by other drivers that want to talk to the EC */
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700121 const char *phys_name;
122 struct device *dev;
123 bool was_wake_device;
124 struct class *cros_class;
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +0100125 int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
126 unsigned int bytes, void *dest);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700127
128 /* These are used to implement the platform-specific interface */
Stephen Barber2c7589a2015-06-09 13:04:45 +0200129 u16 max_request;
130 u16 max_response;
131 u16 max_passthru;
132 u16 proto_version;
Simon Glass4ab61742013-02-25 14:08:37 -0800133 void *priv;
134 int irq;
Stephen Barber2c7589a2015-06-09 13:04:45 +0200135 u8 *din;
136 u8 *dout;
Simon Glass4ab61742013-02-25 14:08:37 -0800137 int din_size;
138 int dout_size;
Simon Glass4ab61742013-02-25 14:08:37 -0800139 bool wake_enabled;
Joseph Loa9eb1862016-12-16 18:57:36 +0100140 bool suspended;
Andrew Brestickera6551a72014-09-18 17:18:56 +0200141 int (*cmd_xfer)(struct cros_ec_device *ec,
142 struct cros_ec_command *msg);
Stephen Barber2c7589a2015-06-09 13:04:45 +0200143 int (*pkt_xfer)(struct cros_ec_device *ec,
144 struct cros_ec_command *msg);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700145 struct mutex lock;
Vic Yang6f1d9122016-08-10 19:05:24 +0200146 bool mkbp_event_supported;
147 struct blocking_notifier_head event_notifier;
148
149 struct ec_response_get_next_event event_data;
150 int event_size;
Simon Glass4ab61742013-02-25 14:08:37 -0800151};
152
Enric Balletbo i Serra974e6f02016-08-01 11:54:35 +0200153/**
154 * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information
155 *
156 * @sensor_num: Id of the sensor, as reported by the EC.
157 */
158struct cros_ec_sensor_platform {
159 u8 sensor_num;
160};
161
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200162/* struct cros_ec_platform - ChromeOS EC platform information
163 *
164 * @ec_name: name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
165 * used in /dev/ and sysfs.
166 * @cmd_offset: offset to apply for each command. Set when
167 * registering a devicde behind another one.
168 */
169struct cros_ec_platform {
170 const char *ec_name;
171 u16 cmd_offset;
172};
173
174/*
175 * struct cros_ec_dev - ChromeOS EC device entry point
176 *
177 * @class_dev: Device structure used in sysfs
178 * @cdev: Character device structure in /dev
179 * @ec_dev: cros_ec_device structure to talk to the physical device
180 * @dev: pointer to the platform device
181 * @cmd_offset: offset to apply for each command.
182 */
183struct cros_ec_dev {
184 struct device class_dev;
185 struct cdev cdev;
186 struct cros_ec_device *ec_dev;
187 struct device *dev;
188 u16 cmd_offset;
Vincent Palatine4244eb2016-08-01 11:54:37 +0200189 u32 features[2];
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200190};
191
Simon Glass4ab61742013-02-25 14:08:37 -0800192/**
193 * cros_ec_suspend - Handle a suspend operation for the ChromeOS EC device
194 *
195 * This can be called by drivers to handle a suspend event.
196 *
197 * ec_dev: Device to suspend
198 * @return 0 if ok, -ve on error
199 */
200int cros_ec_suspend(struct cros_ec_device *ec_dev);
201
202/**
203 * cros_ec_resume - Handle a resume operation for the ChromeOS EC device
204 *
205 * This can be called by drivers to handle a resume event.
206 *
207 * @ec_dev: Device to resume
208 * @return 0 if ok, -ve on error
209 */
210int cros_ec_resume(struct cros_ec_device *ec_dev);
211
212/**
213 * cros_ec_prepare_tx - Prepare an outgoing message in the output buffer
214 *
215 * This is intended to be used by all ChromeOS EC drivers, but at present
216 * only SPI uses it. Once LPC uses the same protocol it can start using it.
217 * I2C could use it now, with a refactor of the existing code.
218 *
219 * @ec_dev: Device to register
220 * @msg: Message to write
221 */
222int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
Bill Richardson5d4773e2014-06-18 11:14:02 -0700223 struct cros_ec_command *msg);
Simon Glass4ab61742013-02-25 14:08:37 -0800224
225/**
Bill Richardson6db07b62014-06-18 11:14:05 -0700226 * cros_ec_check_result - Check ec_msg->result
227 *
228 * This is used by ChromeOS EC drivers to check the ec_msg->result for
229 * errors and to warn about them.
230 *
231 * @ec_dev: EC device
232 * @msg: Message to check
233 */
234int cros_ec_check_result(struct cros_ec_device *ec_dev,
235 struct cros_ec_command *msg);
236
237/**
Andrew Brestickera6551a72014-09-18 17:18:56 +0200238 * cros_ec_cmd_xfer - Send a command to the ChromeOS EC
239 *
240 * Call this to send a command to the ChromeOS EC. This should be used
241 * instead of calling the EC's cmd_xfer() callback directly.
242 *
243 * @ec_dev: EC device
244 * @msg: Message to write
245 */
246int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
247 struct cros_ec_command *msg);
248
249/**
Tomeu Vizoso9798ac62016-07-15 16:28:41 -0700250 * cros_ec_cmd_xfer_status - Send a command to the ChromeOS EC
251 *
252 * This function is identical to cros_ec_cmd_xfer, except it returns success
253 * status only if both the command was transmitted successfully and the EC
254 * replied with success status. It's not necessary to check msg->result when
255 * using this function.
256 *
257 * @ec_dev: EC device
258 * @msg: Message to write
259 * @return: Num. of bytes transferred on success, <0 on failure
260 */
261int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
262 struct cros_ec_command *msg);
263
264/**
Simon Glass4ab61742013-02-25 14:08:37 -0800265 * cros_ec_remove - Remove a ChromeOS EC
266 *
Bill Richardsonee986622014-06-18 11:13:58 -0700267 * Call this to deregister a ChromeOS EC, then clean up any private data.
Simon Glass4ab61742013-02-25 14:08:37 -0800268 *
269 * @ec_dev: Device to register
270 * @return 0 if ok, -ve on error
271 */
272int cros_ec_remove(struct cros_ec_device *ec_dev);
273
274/**
275 * cros_ec_register - Register a new ChromeOS EC, using the provided info
276 *
277 * Before calling this, allocate a pointer to a new device and then fill
278 * in all the fields up to the --private-- marker.
279 *
280 * @ec_dev: Device to register
281 * @return 0 if ok, -ve on error
282 */
283int cros_ec_register(struct cros_ec_device *ec_dev);
284
Stephen Barber2c7589a2015-06-09 13:04:45 +0200285/**
Tomeu Vizosof4bcf5a2016-02-05 14:32:56 +0100286 * cros_ec_query_all - Query the protocol version supported by the ChromeOS EC
Stephen Barber2c7589a2015-06-09 13:04:45 +0200287 *
288 * @ec_dev: Device to register
289 * @return 0 if ok, -ve on error
290 */
291int cros_ec_query_all(struct cros_ec_device *ec_dev);
292
Vic Yang6f1d9122016-08-10 19:05:24 +0200293/**
294 * cros_ec_get_next_event - Fetch next event from the ChromeOS EC
295 *
296 * @ec_dev: Device to fetch event from
297 *
298 * Returns: 0 on success, Linux error number on failure
299 */
300int cros_ec_get_next_event(struct cros_ec_device *ec_dev);
301
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200302/* sysfs stuff */
303extern struct attribute_group cros_ec_attr_group;
304extern struct attribute_group cros_ec_lightbar_attr_group;
Emilio López18800fc2015-09-21 10:38:22 -0300305extern struct attribute_group cros_ec_vbc_attr_group;
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200306
Simon Glass4ab61742013-02-25 14:08:37 -0800307#endif /* __LINUX_MFD_CROS_EC_H */