blob: 79a35857cc9e1914b0a2a5d196ddb976760a950e [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
38/**
39 * struct cros_ec_msg - A message sent to the EC, and its reply
40 *
41 * @version: Command version number (often 0)
42 * @cmd: Command to send (EC_CMD_...)
43 * @out_buf: Outgoing payload (to EC)
44 * @outlen: Outgoing length
45 * @in_buf: Incoming payload (from EC)
46 * @in_len: Incoming length
47 */
48struct cros_ec_msg {
49 u8 version;
50 u8 cmd;
51 uint8_t *out_buf;
52 int out_len;
53 uint8_t *in_buf;
54 int in_len;
55};
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 * @name: Name of this EC interface
71 * @priv: Private data
72 * @irq: Interrupt to use
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070073 * @din: input buffer (for data from EC)
74 * @dout: output buffer (for data to EC)
Simon Glass4ab61742013-02-25 14:08:37 -080075 * \note
76 * These two buffers will always be dword-aligned and include enough
77 * space for up to 7 word-alignment bytes also, so we can ensure that
78 * the body of the message is always dword-aligned (64-bit).
Simon Glass4ab61742013-02-25 14:08:37 -080079 * We use this alignment to keep ARM and x86 happy. Probably word
80 * alignment would be OK, there might be a small performance advantage
81 * to using dword.
Bill Richardson2ce701a2014-06-18 11:13:59 -070082 * @din_size: size of din buffer to allocate (zero to use static din)
83 * @dout_size: size of dout buffer to allocate (zero to use static dout)
Simon Glass4ab61742013-02-25 14:08:37 -080084 * @parent: pointer to parent device (e.g. i2c or spi device)
Simon Glass4ab61742013-02-25 14:08:37 -080085 * @wake_enabled: true if this device can wake the system from sleep
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070086 * @lock: one transaction at a time
87 * @cmd_xfer: low-level channel to the EC
Simon Glass4ab61742013-02-25 14:08:37 -080088 */
89struct cros_ec_device {
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070090
91 /* These are used by other drivers that want to talk to the EC */
92 const char *ec_name;
93 const char *phys_name;
94 struct device *dev;
95 bool was_wake_device;
96 struct class *cros_class;
97 struct blocking_notifier_head event_notifier;
98 int (*command_send)(struct cros_ec_device *ec,
99 uint16_t cmd, void *out_buf, int out_len);
100 int (*command_recv)(struct cros_ec_device *ec,
101 uint16_t cmd, void *in_buf, int in_len);
102 int (*command_sendrecv)(struct cros_ec_device *ec,
103 uint16_t cmd, void *out_buf, int out_len,
104 void *in_buf, int in_len);
105
106 /* These are used to implement the platform-specific interface */
Simon Glass4ab61742013-02-25 14:08:37 -0800107 const char *name;
108 void *priv;
109 int irq;
110 uint8_t *din;
111 uint8_t *dout;
112 int din_size;
113 int dout_size;
Simon Glass4ab61742013-02-25 14:08:37 -0800114 struct device *parent;
Simon Glass4ab61742013-02-25 14:08:37 -0800115 bool wake_enabled;
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700116 struct mutex lock;
117 int (*cmd_xfer)(struct cros_ec_device *ec, struct cros_ec_msg *msg);
Simon Glass4ab61742013-02-25 14:08:37 -0800118};
119
120/**
121 * cros_ec_suspend - Handle a suspend operation for the ChromeOS EC device
122 *
123 * This can be called by drivers to handle a suspend event.
124 *
125 * ec_dev: Device to suspend
126 * @return 0 if ok, -ve on error
127 */
128int cros_ec_suspend(struct cros_ec_device *ec_dev);
129
130/**
131 * cros_ec_resume - Handle a resume operation for the ChromeOS EC device
132 *
133 * This can be called by drivers to handle a resume event.
134 *
135 * @ec_dev: Device to resume
136 * @return 0 if ok, -ve on error
137 */
138int cros_ec_resume(struct cros_ec_device *ec_dev);
139
140/**
141 * cros_ec_prepare_tx - Prepare an outgoing message in the output buffer
142 *
143 * This is intended to be used by all ChromeOS EC drivers, but at present
144 * only SPI uses it. Once LPC uses the same protocol it can start using it.
145 * I2C could use it now, with a refactor of the existing code.
146 *
147 * @ec_dev: Device to register
148 * @msg: Message to write
149 */
150int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
151 struct cros_ec_msg *msg);
152
153/**
154 * cros_ec_remove - Remove a ChromeOS EC
155 *
Bill Richardsonee986622014-06-18 11:13:58 -0700156 * Call this to deregister a ChromeOS EC, then clean up any private data.
Simon Glass4ab61742013-02-25 14:08:37 -0800157 *
158 * @ec_dev: Device to register
159 * @return 0 if ok, -ve on error
160 */
161int cros_ec_remove(struct cros_ec_device *ec_dev);
162
163/**
164 * cros_ec_register - Register a new ChromeOS EC, using the provided info
165 *
166 * Before calling this, allocate a pointer to a new device and then fill
167 * in all the fields up to the --private-- marker.
168 *
169 * @ec_dev: Device to register
170 * @return 0 if ok, -ve on error
171 */
172int cros_ec_register(struct cros_ec_device *ec_dev);
173
174#endif /* __LINUX_MFD_CROS_EC_H */