blob: 2b0c5982dbc1531622b919395e05680a0b9827ef [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
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070019#include <linux/notifier.h>
Simon Glass4ab61742013-02-25 14:08:37 -080020#include <linux/mfd/cros_ec_commands.h>
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070021#include <linux/mutex.h>
Simon Glass4ab61742013-02-25 14:08:37 -080022
23/*
24 * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
25 */
26enum {
27 EC_MSG_TX_HEADER_BYTES = 3,
28 EC_MSG_TX_TRAILER_BYTES = 1,
29 EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES +
30 EC_MSG_TX_TRAILER_BYTES,
31 EC_MSG_RX_PROTO_BYTES = 3,
32
33 /* Max length of messages */
Bill Richardson5271db22014-04-30 10:44:08 -070034 EC_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
35 EC_MSG_TX_PROTO_BYTES,
Simon Glass4ab61742013-02-25 14:08:37 -080036};
37
Bill Richardson5d4773e2014-06-18 11:14:02 -070038/*
Simon Glass4ab61742013-02-25 14:08:37 -080039 * @version: Command version number (often 0)
Bill Richardson5d4773e2014-06-18 11:14:02 -070040 * @command: Command to send (EC_CMD_...)
41 * @outdata: Outgoing data to EC
42 * @outsize: Outgoing length in bytes
43 * @indata: Where to put the incoming data from EC
44 * @insize: Incoming length in bytes (filled in by EC)
45 * @result: EC's response to the command (separate from communication failure)
Simon Glass4ab61742013-02-25 14:08:37 -080046 */
Bill Richardson5d4773e2014-06-18 11:14:02 -070047struct cros_ec_command {
48 uint32_t version;
49 uint32_t command;
50 uint8_t *outdata;
51 uint32_t outsize;
52 uint8_t *indata;
53 uint32_t insize;
54 uint32_t result;
Simon Glass4ab61742013-02-25 14:08:37 -080055};
56
57/**
58 * struct cros_ec_device - Information about a ChromeOS EC device
59 *
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070060 * @ec_name: name of EC device (e.g. 'chromeos-ec')
61 * @phys_name: name of physical comms layer (e.g. 'i2c-4')
62 * @dev: Device pointer
63 * @was_wake_device: true if this device was set to wake the system from
64 * sleep at the last suspend
65 * @event_notifier: interrupt event notifier for transport devices
66 * @command_send: send a command
67 * @command_recv: receive a response
68 * @command_sendrecv: send a command and receive a response
69 *
Simon Glass4ab61742013-02-25 14:08:37 -080070 * @priv: Private data
71 * @irq: Interrupt to use
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070072 * @din: input buffer (for data from EC)
73 * @dout: output buffer (for data to EC)
Simon Glass4ab61742013-02-25 14:08:37 -080074 * \note
75 * These two buffers will always be dword-aligned and include enough
76 * space for up to 7 word-alignment bytes also, so we can ensure that
77 * the body of the message is always dword-aligned (64-bit).
Simon Glass4ab61742013-02-25 14:08:37 -080078 * We use this alignment to keep ARM and x86 happy. Probably word
79 * alignment would be OK, there might be a small performance advantage
80 * to using dword.
Bill Richardson2ce701a2014-06-18 11:13:59 -070081 * @din_size: size of din buffer to allocate (zero to use static din)
82 * @dout_size: size of dout buffer to allocate (zero to use static dout)
Simon Glass4ab61742013-02-25 14:08:37 -080083 * @parent: pointer to parent device (e.g. i2c or spi device)
Simon Glass4ab61742013-02-25 14:08:37 -080084 * @wake_enabled: true if this device can wake the system from sleep
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070085 * @lock: one transaction at a time
86 * @cmd_xfer: low-level channel to the EC
Simon Glass4ab61742013-02-25 14:08:37 -080087 */
88struct cros_ec_device {
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070089
90 /* These are used by other drivers that want to talk to the EC */
91 const char *ec_name;
92 const char *phys_name;
93 struct device *dev;
94 bool was_wake_device;
95 struct class *cros_class;
96 struct blocking_notifier_head event_notifier;
97 int (*command_send)(struct cros_ec_device *ec,
98 uint16_t cmd, void *out_buf, int out_len);
99 int (*command_recv)(struct cros_ec_device *ec,
100 uint16_t cmd, void *in_buf, int in_len);
101 int (*command_sendrecv)(struct cros_ec_device *ec,
102 uint16_t cmd, void *out_buf, int out_len,
103 void *in_buf, int in_len);
104
105 /* These are used to implement the platform-specific interface */
Simon Glass4ab61742013-02-25 14:08:37 -0800106 void *priv;
107 int irq;
108 uint8_t *din;
109 uint8_t *dout;
110 int din_size;
111 int dout_size;
Simon Glass4ab61742013-02-25 14:08:37 -0800112 struct device *parent;
Simon Glass4ab61742013-02-25 14:08:37 -0800113 bool wake_enabled;
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700114 struct mutex lock;
Bill Richardson5d4773e2014-06-18 11:14:02 -0700115 int (*cmd_xfer)(struct cros_ec_device *ec,
116 struct cros_ec_command *msg);
Simon Glass4ab61742013-02-25 14:08:37 -0800117};
118
119/**
120 * cros_ec_suspend - Handle a suspend operation for the ChromeOS EC device
121 *
122 * This can be called by drivers to handle a suspend event.
123 *
124 * ec_dev: Device to suspend
125 * @return 0 if ok, -ve on error
126 */
127int cros_ec_suspend(struct cros_ec_device *ec_dev);
128
129/**
130 * cros_ec_resume - Handle a resume operation for the ChromeOS EC device
131 *
132 * This can be called by drivers to handle a resume event.
133 *
134 * @ec_dev: Device to resume
135 * @return 0 if ok, -ve on error
136 */
137int cros_ec_resume(struct cros_ec_device *ec_dev);
138
139/**
140 * cros_ec_prepare_tx - Prepare an outgoing message in the output buffer
141 *
142 * This is intended to be used by all ChromeOS EC drivers, but at present
143 * only SPI uses it. Once LPC uses the same protocol it can start using it.
144 * I2C could use it now, with a refactor of the existing code.
145 *
146 * @ec_dev: Device to register
147 * @msg: Message to write
148 */
149int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
Bill Richardson5d4773e2014-06-18 11:14:02 -0700150 struct cros_ec_command *msg);
Simon Glass4ab61742013-02-25 14:08:37 -0800151
152/**
153 * cros_ec_remove - Remove a ChromeOS EC
154 *
Bill Richardsonee986622014-06-18 11:13:58 -0700155 * Call this to deregister a ChromeOS EC, then clean up any private data.
Simon Glass4ab61742013-02-25 14:08:37 -0800156 *
157 * @ec_dev: Device to register
158 * @return 0 if ok, -ve on error
159 */
160int cros_ec_remove(struct cros_ec_device *ec_dev);
161
162/**
163 * cros_ec_register - Register a new ChromeOS EC, using the provided info
164 *
165 * Before calling this, allocate a pointer to a new device and then fill
166 * in all the fields up to the --private-- marker.
167 *
168 * @ec_dev: Device to register
169 * @return 0 if ok, -ve on error
170 */
171int cros_ec_register(struct cros_ec_device *ec_dev);
172
173#endif /* __LINUX_MFD_CROS_EC_H */