blob: 92e13aaa450c5bf6dcd8a526a4fe59933a73ee45 [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>
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070020#include <linux/notifier.h>
Simon Glass4ab61742013-02-25 14:08:37 -080021#include <linux/mfd/cros_ec_commands.h>
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070022#include <linux/mutex.h>
Simon Glass4ab61742013-02-25 14:08:37 -080023
24/*
Stephen Barberd3654072015-06-09 13:04:46 +020025 * The EC is unresponsive for a time after a reboot command. Add a
26 * simple delay to make sure that the bus stays locked.
27 */
28#define EC_REBOOT_DELAY_MS 50
29
30/*
Stephen Barber2c7589a2015-06-09 13:04:45 +020031 * Max bus-specific overhead incurred by request/responses.
32 * I2C requires 1 additional byte for requests.
33 * I2C requires 2 additional bytes for responses.
34 * */
35#define EC_PROTO_VERSION_UNKNOWN 0
36#define EC_MAX_REQUEST_OVERHEAD 1
37#define EC_MAX_RESPONSE_OVERHEAD 2
38
39/*
Simon Glass4ab61742013-02-25 14:08:37 -080040 * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
41 */
42enum {
43 EC_MSG_TX_HEADER_BYTES = 3,
44 EC_MSG_TX_TRAILER_BYTES = 1,
45 EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES +
46 EC_MSG_TX_TRAILER_BYTES,
47 EC_MSG_RX_PROTO_BYTES = 3,
48
49 /* Max length of messages */
Bill Richardson5271db22014-04-30 10:44:08 -070050 EC_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
51 EC_MSG_TX_PROTO_BYTES,
Simon Glass4ab61742013-02-25 14:08:37 -080052};
53
Bill Richardson5d4773e2014-06-18 11:14:02 -070054/*
Simon Glass4ab61742013-02-25 14:08:37 -080055 * @version: Command version number (often 0)
Bill Richardson5d4773e2014-06-18 11:14:02 -070056 * @command: Command to send (EC_CMD_...)
Bill Richardson5d4773e2014-06-18 11:14:02 -070057 * @outsize: Outgoing length in bytes
Bill Richardson12ebc8a2014-06-18 11:14:06 -070058 * @insize: Max number of bytes to accept from EC
Bill Richardson5d4773e2014-06-18 11:14:02 -070059 * @result: EC's response to the command (separate from communication failure)
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020060 * @data: Where to put the incoming data from EC and outgoing data to EC
Simon Glass4ab61742013-02-25 14:08:37 -080061 */
Bill Richardson5d4773e2014-06-18 11:14:02 -070062struct cros_ec_command {
63 uint32_t version;
64 uint32_t command;
Bill Richardson5d4773e2014-06-18 11:14:02 -070065 uint32_t outsize;
Bill Richardson5d4773e2014-06-18 11:14:02 -070066 uint32_t insize;
67 uint32_t result;
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020068 uint8_t data[0];
Simon Glass4ab61742013-02-25 14:08:37 -080069};
70
71/**
72 * struct cros_ec_device - Information about a ChromeOS EC device
73 *
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070074 * @ec_name: name of EC device (e.g. 'chromeos-ec')
75 * @phys_name: name of physical comms layer (e.g. 'i2c-4')
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +010076 * @dev: Device pointer for physical comms device
77 * @vdev: Device pointer for virtual comms device
78 * @cdev: Character device structure for virtual comms device
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070079 * @was_wake_device: true if this device was set to wake the system from
80 * sleep at the last suspend
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +010081 * @cmd_readmem: direct read of the EC memory-mapped region, if supported
82 * @offset is within EC_LPC_ADDR_MEMMAP region.
83 * @bytes: number of bytes to read. zero means "read a string" (including
84 * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be read.
85 * Caller must ensure that the buffer is large enough for the result when
86 * reading a string.
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070087 *
Simon Glass4ab61742013-02-25 14:08:37 -080088 * @priv: Private data
89 * @irq: Interrupt to use
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070090 * @din: input buffer (for data from EC)
91 * @dout: output buffer (for data to EC)
Simon Glass4ab61742013-02-25 14:08:37 -080092 * \note
93 * These two buffers will always be dword-aligned and include enough
94 * space for up to 7 word-alignment bytes also, so we can ensure that
95 * the body of the message is always dword-aligned (64-bit).
Simon Glass4ab61742013-02-25 14:08:37 -080096 * We use this alignment to keep ARM and x86 happy. Probably word
97 * alignment would be OK, there might be a small performance advantage
98 * to using dword.
Bill Richardson2ce701a2014-06-18 11:13:59 -070099 * @din_size: size of din buffer to allocate (zero to use static din)
100 * @dout_size: size of dout buffer to allocate (zero to use static dout)
Simon Glass4ab61742013-02-25 14:08:37 -0800101 * @wake_enabled: true if this device can wake the system from sleep
Andrew Brestickera6551a72014-09-18 17:18:56 +0200102 * @cmd_xfer: send command to EC and get response
103 * Returns the number of bytes received if the communication succeeded, but
104 * that doesn't mean the EC was happy with the command. The caller
105 * should check msg.result for the EC's result code.
Stephen Barber2c7589a2015-06-09 13:04:45 +0200106 * @pkt_xfer: send packet to EC and get response
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700107 * @lock: one transaction at a time
Simon Glass4ab61742013-02-25 14:08:37 -0800108 */
109struct cros_ec_device {
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700110
111 /* These are used by other drivers that want to talk to the EC */
112 const char *ec_name;
113 const char *phys_name;
114 struct device *dev;
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +0100115 struct device *vdev;
116 struct cdev cdev;
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700117 bool was_wake_device;
118 struct class *cros_class;
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +0100119 int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
120 unsigned int bytes, void *dest);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700121
122 /* These are used to implement the platform-specific interface */
Stephen Barber2c7589a2015-06-09 13:04:45 +0200123 u16 max_request;
124 u16 max_response;
125 u16 max_passthru;
126 u16 proto_version;
Simon Glass4ab61742013-02-25 14:08:37 -0800127 void *priv;
128 int irq;
Stephen Barber2c7589a2015-06-09 13:04:45 +0200129 u8 *din;
130 u8 *dout;
Simon Glass4ab61742013-02-25 14:08:37 -0800131 int din_size;
132 int dout_size;
Simon Glass4ab61742013-02-25 14:08:37 -0800133 bool wake_enabled;
Andrew Brestickera6551a72014-09-18 17:18:56 +0200134 int (*cmd_xfer)(struct cros_ec_device *ec,
135 struct cros_ec_command *msg);
Stephen Barber2c7589a2015-06-09 13:04:45 +0200136 int (*pkt_xfer)(struct cros_ec_device *ec,
137 struct cros_ec_command *msg);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700138 struct mutex lock;
Simon Glass4ab61742013-02-25 14:08:37 -0800139};
140
141/**
142 * cros_ec_suspend - Handle a suspend operation for the ChromeOS EC device
143 *
144 * This can be called by drivers to handle a suspend event.
145 *
146 * ec_dev: Device to suspend
147 * @return 0 if ok, -ve on error
148 */
149int cros_ec_suspend(struct cros_ec_device *ec_dev);
150
151/**
152 * cros_ec_resume - Handle a resume operation for the ChromeOS EC device
153 *
154 * This can be called by drivers to handle a resume event.
155 *
156 * @ec_dev: Device to resume
157 * @return 0 if ok, -ve on error
158 */
159int cros_ec_resume(struct cros_ec_device *ec_dev);
160
161/**
162 * cros_ec_prepare_tx - Prepare an outgoing message in the output buffer
163 *
164 * This is intended to be used by all ChromeOS EC drivers, but at present
165 * only SPI uses it. Once LPC uses the same protocol it can start using it.
166 * I2C could use it now, with a refactor of the existing code.
167 *
168 * @ec_dev: Device to register
169 * @msg: Message to write
170 */
171int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
Bill Richardson5d4773e2014-06-18 11:14:02 -0700172 struct cros_ec_command *msg);
Simon Glass4ab61742013-02-25 14:08:37 -0800173
174/**
Bill Richardson6db07b62014-06-18 11:14:05 -0700175 * cros_ec_check_result - Check ec_msg->result
176 *
177 * This is used by ChromeOS EC drivers to check the ec_msg->result for
178 * errors and to warn about them.
179 *
180 * @ec_dev: EC device
181 * @msg: Message to check
182 */
183int cros_ec_check_result(struct cros_ec_device *ec_dev,
184 struct cros_ec_command *msg);
185
186/**
Andrew Brestickera6551a72014-09-18 17:18:56 +0200187 * cros_ec_cmd_xfer - Send a command to the ChromeOS EC
188 *
189 * Call this to send a command to the ChromeOS EC. This should be used
190 * instead of calling the EC's cmd_xfer() callback directly.
191 *
192 * @ec_dev: EC device
193 * @msg: Message to write
194 */
195int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
196 struct cros_ec_command *msg);
197
198/**
Simon Glass4ab61742013-02-25 14:08:37 -0800199 * cros_ec_remove - Remove a ChromeOS EC
200 *
Bill Richardsonee986622014-06-18 11:13:58 -0700201 * Call this to deregister a ChromeOS EC, then clean up any private data.
Simon Glass4ab61742013-02-25 14:08:37 -0800202 *
203 * @ec_dev: Device to register
204 * @return 0 if ok, -ve on error
205 */
206int cros_ec_remove(struct cros_ec_device *ec_dev);
207
208/**
209 * cros_ec_register - Register a new ChromeOS EC, using the provided info
210 *
211 * Before calling this, allocate a pointer to a new device and then fill
212 * in all the fields up to the --private-- marker.
213 *
214 * @ec_dev: Device to register
215 * @return 0 if ok, -ve on error
216 */
217int cros_ec_register(struct cros_ec_device *ec_dev);
218
Stephen Barber2c7589a2015-06-09 13:04:45 +0200219/**
220 * cros_ec_register - Query the protocol version supported by the ChromeOS EC
221 *
222 * @ec_dev: Device to register
223 * @return 0 if ok, -ve on error
224 */
225int cros_ec_query_all(struct cros_ec_device *ec_dev);
226
Simon Glass4ab61742013-02-25 14:08:37 -0800227#endif /* __LINUX_MFD_CROS_EC_H */