blob: da72671a42fa6ed14d81a875c0fb8e6a2e66284f [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
53 /* Max length of messages */
Bill Richardson5271db22014-04-30 10:44:08 -070054 EC_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
55 EC_MSG_TX_PROTO_BYTES,
Simon Glass4ab61742013-02-25 14:08:37 -080056};
57
Bill Richardson5d4773e2014-06-18 11:14:02 -070058/*
Simon Glass4ab61742013-02-25 14:08:37 -080059 * @version: Command version number (often 0)
Bill Richardson5d4773e2014-06-18 11:14:02 -070060 * @command: Command to send (EC_CMD_...)
Bill Richardson5d4773e2014-06-18 11:14:02 -070061 * @outsize: Outgoing length in bytes
Bill Richardson12ebc8a2014-06-18 11:14:06 -070062 * @insize: Max number of bytes to accept from EC
Bill Richardson5d4773e2014-06-18 11:14:02 -070063 * @result: EC's response to the command (separate from communication failure)
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020064 * @data: Where to put the incoming data from EC and outgoing data to EC
Simon Glass4ab61742013-02-25 14:08:37 -080065 */
Bill Richardson5d4773e2014-06-18 11:14:02 -070066struct cros_ec_command {
67 uint32_t version;
68 uint32_t command;
Bill Richardson5d4773e2014-06-18 11:14:02 -070069 uint32_t outsize;
Bill Richardson5d4773e2014-06-18 11:14:02 -070070 uint32_t insize;
71 uint32_t result;
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020072 uint8_t data[0];
Simon Glass4ab61742013-02-25 14:08:37 -080073};
74
75/**
76 * struct cros_ec_device - Information about a ChromeOS EC device
77 *
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070078 * @phys_name: name of physical comms layer (e.g. 'i2c-4')
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +010079 * @dev: Device pointer for physical comms device
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070080 * @was_wake_device: true if this device was set to wake the system from
81 * sleep at the last suspend
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +010082 * @cmd_readmem: direct read of the EC memory-mapped region, if supported
83 * @offset is within EC_LPC_ADDR_MEMMAP region.
84 * @bytes: number of bytes to read. zero means "read a string" (including
85 * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be read.
86 * Caller must ensure that the buffer is large enough for the result when
87 * reading a string.
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070088 *
Simon Glass4ab61742013-02-25 14:08:37 -080089 * @priv: Private data
90 * @irq: Interrupt to use
Gwendal Grignou57b33ff2015-06-09 13:04:47 +020091 * @id: Device id
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070092 * @din: input buffer (for data from EC)
93 * @dout: output buffer (for data to EC)
Simon Glass4ab61742013-02-25 14:08:37 -080094 * \note
95 * These two buffers will always be dword-aligned and include enough
96 * space for up to 7 word-alignment bytes also, so we can ensure that
97 * the body of the message is always dword-aligned (64-bit).
Simon Glass4ab61742013-02-25 14:08:37 -080098 * We use this alignment to keep ARM and x86 happy. Probably word
99 * alignment would be OK, there might be a small performance advantage
100 * to using dword.
Bill Richardson2ce701a2014-06-18 11:13:59 -0700101 * @din_size: size of din buffer to allocate (zero to use static din)
102 * @dout_size: size of dout buffer to allocate (zero to use static dout)
Simon Glass4ab61742013-02-25 14:08:37 -0800103 * @wake_enabled: true if this device can wake the system from sleep
Andrew Brestickera6551a72014-09-18 17:18:56 +0200104 * @cmd_xfer: send command to EC and get response
105 * Returns the number of bytes received if the communication succeeded, but
106 * that doesn't mean the EC was happy with the command. The caller
107 * should check msg.result for the EC's result code.
Stephen Barber2c7589a2015-06-09 13:04:45 +0200108 * @pkt_xfer: send packet to EC and get response
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700109 * @lock: one transaction at a time
Simon Glass4ab61742013-02-25 14:08:37 -0800110 */
111struct cros_ec_device {
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700112
113 /* These are used by other drivers that want to talk to the EC */
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700114 const char *phys_name;
115 struct device *dev;
116 bool was_wake_device;
117 struct class *cros_class;
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +0100118 int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
119 unsigned int bytes, void *dest);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700120
121 /* These are used to implement the platform-specific interface */
Stephen Barber2c7589a2015-06-09 13:04:45 +0200122 u16 max_request;
123 u16 max_response;
124 u16 max_passthru;
125 u16 proto_version;
Simon Glass4ab61742013-02-25 14:08:37 -0800126 void *priv;
127 int irq;
Stephen Barber2c7589a2015-06-09 13:04:45 +0200128 u8 *din;
129 u8 *dout;
Simon Glass4ab61742013-02-25 14:08:37 -0800130 int din_size;
131 int dout_size;
Simon Glass4ab61742013-02-25 14:08:37 -0800132 bool wake_enabled;
Andrew Brestickera6551a72014-09-18 17:18:56 +0200133 int (*cmd_xfer)(struct cros_ec_device *ec,
134 struct cros_ec_command *msg);
Stephen Barber2c7589a2015-06-09 13:04:45 +0200135 int (*pkt_xfer)(struct cros_ec_device *ec,
136 struct cros_ec_command *msg);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700137 struct mutex lock;
Simon Glass4ab61742013-02-25 14:08:37 -0800138};
139
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200140/* struct cros_ec_platform - ChromeOS EC platform information
141 *
142 * @ec_name: name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
143 * used in /dev/ and sysfs.
144 * @cmd_offset: offset to apply for each command. Set when
145 * registering a devicde behind another one.
146 */
147struct cros_ec_platform {
148 const char *ec_name;
149 u16 cmd_offset;
150};
151
152/*
153 * struct cros_ec_dev - ChromeOS EC device entry point
154 *
155 * @class_dev: Device structure used in sysfs
156 * @cdev: Character device structure in /dev
157 * @ec_dev: cros_ec_device structure to talk to the physical device
158 * @dev: pointer to the platform device
159 * @cmd_offset: offset to apply for each command.
160 */
161struct cros_ec_dev {
162 struct device class_dev;
163 struct cdev cdev;
164 struct cros_ec_device *ec_dev;
165 struct device *dev;
166 u16 cmd_offset;
167};
168
Simon Glass4ab61742013-02-25 14:08:37 -0800169/**
170 * cros_ec_suspend - Handle a suspend operation for the ChromeOS EC device
171 *
172 * This can be called by drivers to handle a suspend event.
173 *
174 * ec_dev: Device to suspend
175 * @return 0 if ok, -ve on error
176 */
177int cros_ec_suspend(struct cros_ec_device *ec_dev);
178
179/**
180 * cros_ec_resume - Handle a resume operation for the ChromeOS EC device
181 *
182 * This can be called by drivers to handle a resume event.
183 *
184 * @ec_dev: Device to resume
185 * @return 0 if ok, -ve on error
186 */
187int cros_ec_resume(struct cros_ec_device *ec_dev);
188
189/**
190 * cros_ec_prepare_tx - Prepare an outgoing message in the output buffer
191 *
192 * This is intended to be used by all ChromeOS EC drivers, but at present
193 * only SPI uses it. Once LPC uses the same protocol it can start using it.
194 * I2C could use it now, with a refactor of the existing code.
195 *
196 * @ec_dev: Device to register
197 * @msg: Message to write
198 */
199int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
Bill Richardson5d4773e2014-06-18 11:14:02 -0700200 struct cros_ec_command *msg);
Simon Glass4ab61742013-02-25 14:08:37 -0800201
202/**
Bill Richardson6db07b62014-06-18 11:14:05 -0700203 * cros_ec_check_result - Check ec_msg->result
204 *
205 * This is used by ChromeOS EC drivers to check the ec_msg->result for
206 * errors and to warn about them.
207 *
208 * @ec_dev: EC device
209 * @msg: Message to check
210 */
211int cros_ec_check_result(struct cros_ec_device *ec_dev,
212 struct cros_ec_command *msg);
213
214/**
Andrew Brestickera6551a72014-09-18 17:18:56 +0200215 * cros_ec_cmd_xfer - Send a command to the ChromeOS EC
216 *
217 * Call this to send a command to the ChromeOS EC. This should be used
218 * instead of calling the EC's cmd_xfer() callback directly.
219 *
220 * @ec_dev: EC device
221 * @msg: Message to write
222 */
223int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
224 struct cros_ec_command *msg);
225
226/**
Simon Glass4ab61742013-02-25 14:08:37 -0800227 * cros_ec_remove - Remove a ChromeOS EC
228 *
Bill Richardsonee986622014-06-18 11:13:58 -0700229 * Call this to deregister a ChromeOS EC, then clean up any private data.
Simon Glass4ab61742013-02-25 14:08:37 -0800230 *
231 * @ec_dev: Device to register
232 * @return 0 if ok, -ve on error
233 */
234int cros_ec_remove(struct cros_ec_device *ec_dev);
235
236/**
237 * cros_ec_register - Register a new ChromeOS EC, using the provided info
238 *
239 * Before calling this, allocate a pointer to a new device and then fill
240 * in all the fields up to the --private-- marker.
241 *
242 * @ec_dev: Device to register
243 * @return 0 if ok, -ve on error
244 */
245int cros_ec_register(struct cros_ec_device *ec_dev);
246
Stephen Barber2c7589a2015-06-09 13:04:45 +0200247/**
248 * cros_ec_register - Query the protocol version supported by the ChromeOS EC
249 *
250 * @ec_dev: Device to register
251 * @return 0 if ok, -ve on error
252 */
253int cros_ec_query_all(struct cros_ec_device *ec_dev);
254
Gwendal Grignou57b33ff2015-06-09 13:04:47 +0200255/* sysfs stuff */
256extern struct attribute_group cros_ec_attr_group;
257extern struct attribute_group cros_ec_lightbar_attr_group;
258
Simon Glass4ab61742013-02-25 14:08:37 -0800259#endif /* __LINUX_MFD_CROS_EC_H */