blob: 59d909434efd01c67f818caa2aa0f18e13a20df5 [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 Barber2c7589a2015-06-09 13:04:45 +020025 * Max bus-specific overhead incurred by request/responses.
26 * I2C requires 1 additional byte for requests.
27 * I2C requires 2 additional bytes for responses.
28 * */
29#define EC_PROTO_VERSION_UNKNOWN 0
30#define EC_MAX_REQUEST_OVERHEAD 1
31#define EC_MAX_RESPONSE_OVERHEAD 2
32
33/*
Simon Glass4ab61742013-02-25 14:08:37 -080034 * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
35 */
36enum {
37 EC_MSG_TX_HEADER_BYTES = 3,
38 EC_MSG_TX_TRAILER_BYTES = 1,
39 EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES +
40 EC_MSG_TX_TRAILER_BYTES,
41 EC_MSG_RX_PROTO_BYTES = 3,
42
43 /* Max length of messages */
Bill Richardson5271db22014-04-30 10:44:08 -070044 EC_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
45 EC_MSG_TX_PROTO_BYTES,
Simon Glass4ab61742013-02-25 14:08:37 -080046};
47
Bill Richardson5d4773e2014-06-18 11:14:02 -070048/*
Simon Glass4ab61742013-02-25 14:08:37 -080049 * @version: Command version number (often 0)
Bill Richardson5d4773e2014-06-18 11:14:02 -070050 * @command: Command to send (EC_CMD_...)
Bill Richardson5d4773e2014-06-18 11:14:02 -070051 * @outsize: Outgoing length in bytes
Bill Richardson12ebc8a2014-06-18 11:14:06 -070052 * @insize: Max number of bytes to accept from EC
Bill Richardson5d4773e2014-06-18 11:14:02 -070053 * @result: EC's response to the command (separate from communication failure)
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020054 * @data: Where to put the incoming data from EC and outgoing data to EC
Simon Glass4ab61742013-02-25 14:08:37 -080055 */
Bill Richardson5d4773e2014-06-18 11:14:02 -070056struct cros_ec_command {
57 uint32_t version;
58 uint32_t command;
Bill Richardson5d4773e2014-06-18 11:14:02 -070059 uint32_t outsize;
Bill Richardson5d4773e2014-06-18 11:14:02 -070060 uint32_t insize;
61 uint32_t result;
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020062 uint8_t data[0];
Simon Glass4ab61742013-02-25 14:08:37 -080063};
64
65/**
66 * struct cros_ec_device - Information about a ChromeOS EC device
67 *
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070068 * @ec_name: name of EC device (e.g. 'chromeos-ec')
69 * @phys_name: name of physical comms layer (e.g. 'i2c-4')
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +010070 * @dev: Device pointer for physical comms device
71 * @vdev: Device pointer for virtual comms device
72 * @cdev: Character device structure for virtual comms device
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070073 * @was_wake_device: true if this device was set to wake the system from
74 * sleep at the last suspend
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +010075 * @cmd_readmem: direct read of the EC memory-mapped region, if supported
76 * @offset is within EC_LPC_ADDR_MEMMAP region.
77 * @bytes: number of bytes to read. zero means "read a string" (including
78 * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be read.
79 * Caller must ensure that the buffer is large enough for the result when
80 * reading a string.
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070081 *
Simon Glass4ab61742013-02-25 14:08:37 -080082 * @priv: Private data
83 * @irq: Interrupt to use
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070084 * @din: input buffer (for data from EC)
85 * @dout: output buffer (for data to EC)
Simon Glass4ab61742013-02-25 14:08:37 -080086 * \note
87 * These two buffers will always be dword-aligned and include enough
88 * space for up to 7 word-alignment bytes also, so we can ensure that
89 * the body of the message is always dword-aligned (64-bit).
Simon Glass4ab61742013-02-25 14:08:37 -080090 * We use this alignment to keep ARM and x86 happy. Probably word
91 * alignment would be OK, there might be a small performance advantage
92 * to using dword.
Bill Richardson2ce701a2014-06-18 11:13:59 -070093 * @din_size: size of din buffer to allocate (zero to use static din)
94 * @dout_size: size of dout buffer to allocate (zero to use static dout)
Simon Glass4ab61742013-02-25 14:08:37 -080095 * @wake_enabled: true if this device can wake the system from sleep
Andrew Brestickera6551a72014-09-18 17:18:56 +020096 * @cmd_xfer: send command to EC and get response
97 * Returns the number of bytes received if the communication succeeded, but
98 * that doesn't mean the EC was happy with the command. The caller
99 * should check msg.result for the EC's result code.
Stephen Barber2c7589a2015-06-09 13:04:45 +0200100 * @pkt_xfer: send packet to EC and get response
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700101 * @lock: one transaction at a time
Simon Glass4ab61742013-02-25 14:08:37 -0800102 */
103struct cros_ec_device {
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700104
105 /* These are used by other drivers that want to talk to the EC */
106 const char *ec_name;
107 const char *phys_name;
108 struct device *dev;
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +0100109 struct device *vdev;
110 struct cdev cdev;
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700111 bool was_wake_device;
112 struct class *cros_class;
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +0100113 int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
114 unsigned int bytes, void *dest);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700115
116 /* These are used to implement the platform-specific interface */
Stephen Barber2c7589a2015-06-09 13:04:45 +0200117 u16 max_request;
118 u16 max_response;
119 u16 max_passthru;
120 u16 proto_version;
Simon Glass4ab61742013-02-25 14:08:37 -0800121 void *priv;
122 int irq;
Stephen Barber2c7589a2015-06-09 13:04:45 +0200123 u8 *din;
124 u8 *dout;
Simon Glass4ab61742013-02-25 14:08:37 -0800125 int din_size;
126 int dout_size;
Simon Glass4ab61742013-02-25 14:08:37 -0800127 bool wake_enabled;
Andrew Brestickera6551a72014-09-18 17:18:56 +0200128 int (*cmd_xfer)(struct cros_ec_device *ec,
129 struct cros_ec_command *msg);
Stephen Barber2c7589a2015-06-09 13:04:45 +0200130 int (*pkt_xfer)(struct cros_ec_device *ec,
131 struct cros_ec_command *msg);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700132 struct mutex lock;
Simon Glass4ab61742013-02-25 14:08:37 -0800133};
134
135/**
136 * cros_ec_suspend - Handle a suspend operation for the ChromeOS EC device
137 *
138 * This can be called by drivers to handle a suspend event.
139 *
140 * ec_dev: Device to suspend
141 * @return 0 if ok, -ve on error
142 */
143int cros_ec_suspend(struct cros_ec_device *ec_dev);
144
145/**
146 * cros_ec_resume - Handle a resume operation for the ChromeOS EC device
147 *
148 * This can be called by drivers to handle a resume event.
149 *
150 * @ec_dev: Device to resume
151 * @return 0 if ok, -ve on error
152 */
153int cros_ec_resume(struct cros_ec_device *ec_dev);
154
155/**
156 * cros_ec_prepare_tx - Prepare an outgoing message in the output buffer
157 *
158 * This is intended to be used by all ChromeOS EC drivers, but at present
159 * only SPI uses it. Once LPC uses the same protocol it can start using it.
160 * I2C could use it now, with a refactor of the existing code.
161 *
162 * @ec_dev: Device to register
163 * @msg: Message to write
164 */
165int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
Bill Richardson5d4773e2014-06-18 11:14:02 -0700166 struct cros_ec_command *msg);
Simon Glass4ab61742013-02-25 14:08:37 -0800167
168/**
Bill Richardson6db07b62014-06-18 11:14:05 -0700169 * cros_ec_check_result - Check ec_msg->result
170 *
171 * This is used by ChromeOS EC drivers to check the ec_msg->result for
172 * errors and to warn about them.
173 *
174 * @ec_dev: EC device
175 * @msg: Message to check
176 */
177int cros_ec_check_result(struct cros_ec_device *ec_dev,
178 struct cros_ec_command *msg);
179
180/**
Andrew Brestickera6551a72014-09-18 17:18:56 +0200181 * cros_ec_cmd_xfer - Send a command to the ChromeOS EC
182 *
183 * Call this to send a command to the ChromeOS EC. This should be used
184 * instead of calling the EC's cmd_xfer() callback directly.
185 *
186 * @ec_dev: EC device
187 * @msg: Message to write
188 */
189int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
190 struct cros_ec_command *msg);
191
192/**
Simon Glass4ab61742013-02-25 14:08:37 -0800193 * cros_ec_remove - Remove a ChromeOS EC
194 *
Bill Richardsonee986622014-06-18 11:13:58 -0700195 * Call this to deregister a ChromeOS EC, then clean up any private data.
Simon Glass4ab61742013-02-25 14:08:37 -0800196 *
197 * @ec_dev: Device to register
198 * @return 0 if ok, -ve on error
199 */
200int cros_ec_remove(struct cros_ec_device *ec_dev);
201
202/**
203 * cros_ec_register - Register a new ChromeOS EC, using the provided info
204 *
205 * Before calling this, allocate a pointer to a new device and then fill
206 * in all the fields up to the --private-- marker.
207 *
208 * @ec_dev: Device to register
209 * @return 0 if ok, -ve on error
210 */
211int cros_ec_register(struct cros_ec_device *ec_dev);
212
Stephen Barber2c7589a2015-06-09 13:04:45 +0200213/**
214 * cros_ec_register - Query the protocol version supported by the ChromeOS EC
215 *
216 * @ec_dev: Device to register
217 * @return 0 if ok, -ve on error
218 */
219int cros_ec_query_all(struct cros_ec_device *ec_dev);
220
Simon Glass4ab61742013-02-25 14:08:37 -0800221#endif /* __LINUX_MFD_CROS_EC_H */