mfd: cros_ec: Use a zero-length array for command data
[cascardo/linux.git] / include / linux / mfd / cros_ec.h
1 /*
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
19 #include <linux/cdev.h>
20 #include <linux/notifier.h>
21 #include <linux/mfd/cros_ec_commands.h>
22 #include <linux/mutex.h>
23
24 /*
25  * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
26  */
27 enum {
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 */
35         EC_MSG_BYTES            = EC_PROTO2_MAX_PARAM_SIZE +
36                                         EC_MSG_TX_PROTO_BYTES,
37 };
38
39 /*
40  * @version: Command version number (often 0)
41  * @command: Command to send (EC_CMD_...)
42  * @outsize: Outgoing length in bytes
43  * @insize: Max number of bytes to accept from EC
44  * @result: EC's response to the command (separate from communication failure)
45  * @data: Where to put the incoming data from EC and outgoing data to EC
46  */
47 struct cros_ec_command {
48         uint32_t version;
49         uint32_t command;
50         uint32_t outsize;
51         uint32_t insize;
52         uint32_t result;
53         uint8_t data[0];
54 };
55
56 /**
57  * struct cros_ec_device - Information about a ChromeOS EC device
58  *
59  * @ec_name: name of EC device (e.g. 'chromeos-ec')
60  * @phys_name: name of physical comms layer (e.g. 'i2c-4')
61  * @dev: Device pointer for physical comms device
62  * @vdev: Device pointer for virtual comms device
63  * @cdev: Character device structure for virtual comms device
64  * @was_wake_device: true if this device was set to wake the system from
65  * sleep at the last suspend
66  * @cmd_readmem: direct read of the EC memory-mapped region, if supported
67  *     @offset is within EC_LPC_ADDR_MEMMAP region.
68  *     @bytes: number of bytes to read. zero means "read a string" (including
69  *     the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be read.
70  *     Caller must ensure that the buffer is large enough for the result when
71  *     reading a string.
72  *
73  * @priv: Private data
74  * @irq: Interrupt to use
75  * @din: input buffer (for data from EC)
76  * @dout: output buffer (for data to EC)
77  * \note
78  * These two buffers will always be dword-aligned and include enough
79  * space for up to 7 word-alignment bytes also, so we can ensure that
80  * the body of the message is always dword-aligned (64-bit).
81  * We use this alignment to keep ARM and x86 happy. Probably word
82  * alignment would be OK, there might be a small performance advantage
83  * to using dword.
84  * @din_size: size of din buffer to allocate (zero to use static din)
85  * @dout_size: size of dout buffer to allocate (zero to use static dout)
86  * @wake_enabled: true if this device can wake the system from sleep
87  * @cmd_xfer: send command to EC and get response
88  *     Returns the number of bytes received if the communication succeeded, but
89  *     that doesn't mean the EC was happy with the command. The caller
90  *     should check msg.result for the EC's result code.
91  * @lock: one transaction at a time
92  */
93 struct cros_ec_device {
94
95         /* These are used by other drivers that want to talk to the EC */
96         const char *ec_name;
97         const char *phys_name;
98         struct device *dev;
99         struct device *vdev;
100         struct cdev cdev;
101         bool was_wake_device;
102         struct class *cros_class;
103         int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
104                            unsigned int bytes, void *dest);
105
106         /* These are used to implement the platform-specific interface */
107         void *priv;
108         int irq;
109         uint8_t *din;
110         uint8_t *dout;
111         int din_size;
112         int dout_size;
113         bool wake_enabled;
114         int (*cmd_xfer)(struct cros_ec_device *ec,
115                         struct cros_ec_command *msg);
116         struct mutex lock;
117 };
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  */
127 int 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  */
137 int 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  */
149 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
150                        struct cros_ec_command *msg);
151
152 /**
153  * cros_ec_check_result - Check ec_msg->result
154  *
155  * This is used by ChromeOS EC drivers to check the ec_msg->result for
156  * errors and to warn about them.
157  *
158  * @ec_dev: EC device
159  * @msg: Message to check
160  */
161 int cros_ec_check_result(struct cros_ec_device *ec_dev,
162                          struct cros_ec_command *msg);
163
164 /**
165  * cros_ec_cmd_xfer - Send a command to the ChromeOS EC
166  *
167  * Call this to send a command to the ChromeOS EC.  This should be used
168  * instead of calling the EC's cmd_xfer() callback directly.
169  *
170  * @ec_dev: EC device
171  * @msg: Message to write
172  */
173 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
174                      struct cros_ec_command *msg);
175
176 /**
177  * cros_ec_remove - Remove a ChromeOS EC
178  *
179  * Call this to deregister a ChromeOS EC, then clean up any private data.
180  *
181  * @ec_dev: Device to register
182  * @return 0 if ok, -ve on error
183  */
184 int cros_ec_remove(struct cros_ec_device *ec_dev);
185
186 /**
187  * cros_ec_register - Register a new ChromeOS EC, using the provided info
188  *
189  * Before calling this, allocate a pointer to a new device and then fill
190  * in all the fields up to the --private-- marker.
191  *
192  * @ec_dev: Device to register
193  * @return 0 if ok, -ve on error
194  */
195 int cros_ec_register(struct cros_ec_device *ec_dev);
196
197 #endif /* __LINUX_MFD_CROS_EC_H */