blob: 14cf522123ddb1678794d5e34d1df2a93ae7c63b [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/*
25 * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
26 */
27enum {
28 EC_MSG_TX_HEADER_BYTES = 3,
29 EC_MSG_TX_TRAILER_BYTES = 1,
30 EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES +
31 EC_MSG_TX_TRAILER_BYTES,
32 EC_MSG_RX_PROTO_BYTES = 3,
33
34 /* Max length of messages */
Bill Richardson5271db22014-04-30 10:44:08 -070035 EC_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
36 EC_MSG_TX_PROTO_BYTES,
Simon Glass4ab61742013-02-25 14:08:37 -080037};
38
Bill Richardson5d4773e2014-06-18 11:14:02 -070039/*
Simon Glass4ab61742013-02-25 14:08:37 -080040 * @version: Command version number (often 0)
Bill Richardson5d4773e2014-06-18 11:14:02 -070041 * @command: Command to send (EC_CMD_...)
Bill Richardson5d4773e2014-06-18 11:14:02 -070042 * @outsize: Outgoing length in bytes
Bill Richardson12ebc8a2014-06-18 11:14:06 -070043 * @insize: Max number of bytes to accept from EC
Bill Richardson5d4773e2014-06-18 11:14:02 -070044 * @result: EC's response to the command (separate from communication failure)
Javier Martinez Canillas1b84f2a2015-02-02 12:26:22 +010045 * @outdata: Outgoing data to EC
46 * @indata: Where to put the incoming data from EC
Simon Glass4ab61742013-02-25 14:08:37 -080047 */
Bill Richardson5d4773e2014-06-18 11:14:02 -070048struct cros_ec_command {
49 uint32_t version;
50 uint32_t command;
Bill Richardson5d4773e2014-06-18 11:14:02 -070051 uint32_t outsize;
Bill Richardson5d4773e2014-06-18 11:14:02 -070052 uint32_t insize;
53 uint32_t result;
Javier Martinez Canillas1b84f2a2015-02-02 12:26:22 +010054 uint8_t outdata[EC_PROTO2_MAX_PARAM_SIZE];
55 uint8_t indata[EC_PROTO2_MAX_PARAM_SIZE];
Simon Glass4ab61742013-02-25 14:08:37 -080056};
57
58/**
59 * struct cros_ec_device - Information about a ChromeOS EC device
60 *
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070061 * @ec_name: name of EC device (e.g. 'chromeos-ec')
62 * @phys_name: name of physical comms layer (e.g. 'i2c-4')
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +010063 * @dev: Device pointer for physical comms device
64 * @vdev: Device pointer for virtual comms device
65 * @cdev: Character device structure for virtual comms device
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070066 * @was_wake_device: true if this device was set to wake the system from
67 * sleep at the last suspend
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +010068 * @cmd_readmem: direct read of the EC memory-mapped region, if supported
69 * @offset is within EC_LPC_ADDR_MEMMAP region.
70 * @bytes: number of bytes to read. zero means "read a string" (including
71 * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be read.
72 * Caller must ensure that the buffer is large enough for the result when
73 * reading a string.
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070074 *
Simon Glass4ab61742013-02-25 14:08:37 -080075 * @priv: Private data
76 * @irq: Interrupt to use
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070077 * @din: input buffer (for data from EC)
78 * @dout: output buffer (for data to EC)
Simon Glass4ab61742013-02-25 14:08:37 -080079 * \note
80 * These two buffers will always be dword-aligned and include enough
81 * space for up to 7 word-alignment bytes also, so we can ensure that
82 * the body of the message is always dword-aligned (64-bit).
Simon Glass4ab61742013-02-25 14:08:37 -080083 * We use this alignment to keep ARM and x86 happy. Probably word
84 * alignment would be OK, there might be a small performance advantage
85 * to using dword.
Bill Richardson2ce701a2014-06-18 11:13:59 -070086 * @din_size: size of din buffer to allocate (zero to use static din)
87 * @dout_size: size of dout buffer to allocate (zero to use static dout)
Simon Glass4ab61742013-02-25 14:08:37 -080088 * @wake_enabled: true if this device can wake the system from sleep
Andrew Brestickera6551a72014-09-18 17:18:56 +020089 * @cmd_xfer: send command to EC and get response
90 * Returns the number of bytes received if the communication succeeded, but
91 * that doesn't mean the EC was happy with the command. The caller
92 * should check msg.result for the EC's result code.
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070093 * @lock: one transaction at a time
Simon Glass4ab61742013-02-25 14:08:37 -080094 */
95struct cros_ec_device {
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070096
97 /* These are used by other drivers that want to talk to the EC */
98 const char *ec_name;
99 const char *phys_name;
100 struct device *dev;
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +0100101 struct device *vdev;
102 struct cdev cdev;
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700103 bool was_wake_device;
104 struct class *cros_class;
Javier Martinez Canillas05c11ac2015-02-02 12:26:23 +0100105 int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
106 unsigned int bytes, void *dest);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700107
108 /* These are used to implement the platform-specific interface */
Simon Glass4ab61742013-02-25 14:08:37 -0800109 void *priv;
110 int irq;
111 uint8_t *din;
112 uint8_t *dout;
113 int din_size;
114 int dout_size;
Simon Glass4ab61742013-02-25 14:08:37 -0800115 bool wake_enabled;
Andrew Brestickera6551a72014-09-18 17:18:56 +0200116 int (*cmd_xfer)(struct cros_ec_device *ec,
117 struct cros_ec_command *msg);
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700118 struct mutex lock;
Simon Glass4ab61742013-02-25 14:08:37 -0800119};
120
121/**
122 * cros_ec_suspend - Handle a suspend operation for the ChromeOS EC device
123 *
124 * This can be called by drivers to handle a suspend event.
125 *
126 * ec_dev: Device to suspend
127 * @return 0 if ok, -ve on error
128 */
129int cros_ec_suspend(struct cros_ec_device *ec_dev);
130
131/**
132 * cros_ec_resume - Handle a resume operation for the ChromeOS EC device
133 *
134 * This can be called by drivers to handle a resume event.
135 *
136 * @ec_dev: Device to resume
137 * @return 0 if ok, -ve on error
138 */
139int cros_ec_resume(struct cros_ec_device *ec_dev);
140
141/**
142 * cros_ec_prepare_tx - Prepare an outgoing message in the output buffer
143 *
144 * This is intended to be used by all ChromeOS EC drivers, but at present
145 * only SPI uses it. Once LPC uses the same protocol it can start using it.
146 * I2C could use it now, with a refactor of the existing code.
147 *
148 * @ec_dev: Device to register
149 * @msg: Message to write
150 */
151int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
Bill Richardson5d4773e2014-06-18 11:14:02 -0700152 struct cros_ec_command *msg);
Simon Glass4ab61742013-02-25 14:08:37 -0800153
154/**
Bill Richardson6db07b62014-06-18 11:14:05 -0700155 * cros_ec_check_result - Check ec_msg->result
156 *
157 * This is used by ChromeOS EC drivers to check the ec_msg->result for
158 * errors and to warn about them.
159 *
160 * @ec_dev: EC device
161 * @msg: Message to check
162 */
163int cros_ec_check_result(struct cros_ec_device *ec_dev,
164 struct cros_ec_command *msg);
165
166/**
Andrew Brestickera6551a72014-09-18 17:18:56 +0200167 * cros_ec_cmd_xfer - Send a command to the ChromeOS EC
168 *
169 * Call this to send a command to the ChromeOS EC. This should be used
170 * instead of calling the EC's cmd_xfer() callback directly.
171 *
172 * @ec_dev: EC device
173 * @msg: Message to write
174 */
175int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
176 struct cros_ec_command *msg);
177
178/**
Simon Glass4ab61742013-02-25 14:08:37 -0800179 * cros_ec_remove - Remove a ChromeOS EC
180 *
Bill Richardsonee986622014-06-18 11:13:58 -0700181 * Call this to deregister a ChromeOS EC, then clean up any private data.
Simon Glass4ab61742013-02-25 14:08:37 -0800182 *
183 * @ec_dev: Device to register
184 * @return 0 if ok, -ve on error
185 */
186int cros_ec_remove(struct cros_ec_device *ec_dev);
187
188/**
189 * cros_ec_register - Register a new ChromeOS EC, using the provided info
190 *
191 * Before calling this, allocate a pointer to a new device and then fill
192 * in all the fields up to the --private-- marker.
193 *
194 * @ec_dev: Device to register
195 * @return 0 if ok, -ve on error
196 */
197int cros_ec_register(struct cros_ec_device *ec_dev);
198
199#endif /* __LINUX_MFD_CROS_EC_H */