Merge branch 'i2c/for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa...
[sfrench/cifs-2.6.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/device.h>
21 #include <linux/notifier.h>
22 #include <linux/mfd/cros_ec_commands.h>
23 #include <linux/mutex.h>
24
25 #define CROS_EC_DEV_NAME "cros_ec"
26 #define CROS_EC_DEV_PD_NAME "cros_pd"
27
28 /*
29  * The EC is unresponsive for a time after a reboot command.  Add a
30  * simple delay to make sure that the bus stays locked.
31  */
32 #define EC_REBOOT_DELAY_MS             50
33
34 /*
35  * Max bus-specific overhead incurred by request/responses.
36  * I2C requires 1 additional byte for requests.
37  * I2C requires 2 additional bytes for responses.
38  * SPI requires up to 32 additional bytes for responses.
39  */
40 #define EC_PROTO_VERSION_UNKNOWN        0
41 #define EC_MAX_REQUEST_OVERHEAD         1
42 #define EC_MAX_RESPONSE_OVERHEAD        32
43
44 /*
45  * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
46  */
47 enum {
48         EC_MSG_TX_HEADER_BYTES  = 3,
49         EC_MSG_TX_TRAILER_BYTES = 1,
50         EC_MSG_TX_PROTO_BYTES   = EC_MSG_TX_HEADER_BYTES +
51                                         EC_MSG_TX_TRAILER_BYTES,
52         EC_MSG_RX_PROTO_BYTES   = 3,
53
54         /* Max length of messages for proto 2*/
55         EC_PROTO2_MSG_BYTES             = EC_PROTO2_MAX_PARAM_SIZE +
56                                         EC_MSG_TX_PROTO_BYTES,
57
58         EC_MAX_MSG_BYTES                = 64 * 1024,
59 };
60
61 /**
62  * struct cros_ec_command - Information about a ChromeOS EC command.
63  * @version: Command version number (often 0).
64  * @command: Command to send (EC_CMD_...).
65  * @outsize: Outgoing length in bytes.
66  * @insize: Max number of bytes to accept from the EC.
67  * @result: EC's response to the command (separate from communication failure).
68  * @data: Where to put the incoming data from EC and outgoing data to EC.
69  */
70 struct cros_ec_command {
71         uint32_t version;
72         uint32_t command;
73         uint32_t outsize;
74         uint32_t insize;
75         uint32_t result;
76         uint8_t data[0];
77 };
78
79 /**
80  * struct cros_ec_device - Information about a ChromeOS EC device.
81  * @phys_name: Name of physical comms layer (e.g. 'i2c-4').
82  * @dev: Device pointer for physical comms device
83  * @was_wake_device: True if this device was set to wake the system from
84  *                   sleep at the last suspend.
85  * @cros_class: The class structure for this device.
86  * @cmd_readmem: Direct read of the EC memory-mapped region, if supported.
87  *     @offset: Is within EC_LPC_ADDR_MEMMAP region.
88  *     @bytes: Number of bytes to read. zero means "read a string" (including
89  *             the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be
90  *             read. Caller must ensure that the buffer is large enough for the
91  *             result when reading a string.
92  * @max_request: Max size of message requested.
93  * @max_response: Max size of message response.
94  * @max_passthru: Max sice of passthru message.
95  * @proto_version: The protocol version used for this device.
96  * @priv: Private data.
97  * @irq: Interrupt to use.
98  * @id: Device id.
99  * @din: Input buffer (for data from EC). This buffer will always be
100  *       dword-aligned and include enough space for up to 7 word-alignment
101  *       bytes also, so we can ensure that the body of the message is always
102  *       dword-aligned (64-bit). We use this alignment to keep ARM and x86
103  *       happy. Probably word alignment would be OK, there might be a small
104  *       performance advantage to using dword.
105  * @dout: Output buffer (for data to EC). This buffer will always be
106  *        dword-aligned and include enough space for up to 7 word-alignment
107  *        bytes also, so we can ensure that the body of the message is always
108  *        dword-aligned (64-bit). We use this alignment to keep ARM and x86
109  *        happy. Probably word alignment would be OK, there might be a small
110  *        performance advantage to using dword.
111  * @din_size: Size of din buffer to allocate (zero to use static din).
112  * @dout_size: Size of dout buffer to allocate (zero to use static dout).
113  * @wake_enabled: True if this device can wake the system from sleep.
114  * @suspended: True if this device had been suspended.
115  * @cmd_xfer: Send command to EC and get response.
116  *            Returns the number of bytes received if the communication
117  *            succeeded, but that doesn't mean the EC was happy with the
118  *            command. The caller should check msg.result for the EC's result
119  *            code.
120  * @pkt_xfer: Send packet to EC and get response.
121  * @lock: One transaction at a time.
122  * @mkbp_event_supported: True if this EC supports the MKBP event protocol.
123  * @event_notifier: Interrupt event notifier for transport devices.
124  * @event_data: Raw payload transferred with the MKBP event.
125  * @event_size: Size in bytes of the event data.
126  * @host_event_wake_mask: Mask of host events that cause wake from suspend.
127  */
128 struct cros_ec_device {
129         /* These are used by other drivers that want to talk to the EC */
130         const char *phys_name;
131         struct device *dev;
132         bool was_wake_device;
133         struct class *cros_class;
134         int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
135                            unsigned int bytes, void *dest);
136
137         /* These are used to implement the platform-specific interface */
138         u16 max_request;
139         u16 max_response;
140         u16 max_passthru;
141         u16 proto_version;
142         void *priv;
143         int irq;
144         u8 *din;
145         u8 *dout;
146         int din_size;
147         int dout_size;
148         bool wake_enabled;
149         bool suspended;
150         int (*cmd_xfer)(struct cros_ec_device *ec,
151                         struct cros_ec_command *msg);
152         int (*pkt_xfer)(struct cros_ec_device *ec,
153                         struct cros_ec_command *msg);
154         struct mutex lock;
155         bool mkbp_event_supported;
156         struct blocking_notifier_head event_notifier;
157
158         struct ec_response_get_next_event_v1 event_data;
159         int event_size;
160         u32 host_event_wake_mask;
161 };
162
163 /**
164  * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information.
165  * @sensor_num: Id of the sensor, as reported by the EC.
166  */
167 struct cros_ec_sensor_platform {
168         u8 sensor_num;
169 };
170
171 /**
172  * struct cros_ec_platform - ChromeOS EC platform information.
173  * @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
174  *           used in /dev/ and sysfs.
175  * @cmd_offset: Offset to apply for each command. Set when
176  *              registering a device behind another one.
177  */
178 struct cros_ec_platform {
179         const char *ec_name;
180         u16 cmd_offset;
181 };
182
183 struct cros_ec_debugfs;
184
185 /**
186  * struct cros_ec_dev - ChromeOS EC device entry point.
187  * @class_dev: Device structure used in sysfs.
188  * @cdev: Character device structure in /dev.
189  * @ec_dev: cros_ec_device structure to talk to the physical device.
190  * @dev: Pointer to the platform device.
191  * @debug_info: cros_ec_debugfs structure for debugging information.
192  * @has_kb_wake_angle: True if at least 2 accelerometer are connected to the EC.
193  * @cmd_offset: Offset to apply for each command.
194  * @features: Features supported by the EC.
195  */
196 struct cros_ec_dev {
197         struct device class_dev;
198         struct cdev cdev;
199         struct cros_ec_device *ec_dev;
200         struct device *dev;
201         struct cros_ec_debugfs *debug_info;
202         bool has_kb_wake_angle;
203         u16 cmd_offset;
204         u32 features[2];
205 };
206
207 #define to_cros_ec_dev(dev)  container_of(dev, struct cros_ec_dev, class_dev)
208
209 /**
210  * cros_ec_suspend() - Handle a suspend operation for the ChromeOS EC device.
211  * @ec_dev: Device to suspend.
212  *
213  * This can be called by drivers to handle a suspend event.
214  *
215  * Return: 0 on success or negative error code.
216  */
217 int cros_ec_suspend(struct cros_ec_device *ec_dev);
218
219 /**
220  * cros_ec_resume() - Handle a resume operation for the ChromeOS EC device.
221  * @ec_dev: Device to resume.
222  *
223  * This can be called by drivers to handle a resume event.
224  *
225  * Return: 0 on success or negative error code.
226  */
227 int cros_ec_resume(struct cros_ec_device *ec_dev);
228
229 /**
230  * cros_ec_prepare_tx() - Prepare an outgoing message in the output buffer.
231  * @ec_dev: Device to register.
232  * @msg: Message to write.
233  *
234  * This is intended to be used by all ChromeOS EC drivers, but at present
235  * only SPI uses it. Once LPC uses the same protocol it can start using it.
236  * I2C could use it now, with a refactor of the existing code.
237  *
238  * Return: 0 on success or negative error code.
239  */
240 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
241                        struct cros_ec_command *msg);
242
243 /**
244  * cros_ec_check_result() - Check ec_msg->result.
245  * @ec_dev: EC device.
246  * @msg: Message to check.
247  *
248  * This is used by ChromeOS EC drivers to check the ec_msg->result for
249  * errors and to warn about them.
250  *
251  * Return: 0 on success or negative error code.
252  */
253 int cros_ec_check_result(struct cros_ec_device *ec_dev,
254                          struct cros_ec_command *msg);
255
256 /**
257  * cros_ec_cmd_xfer() - Send a command to the ChromeOS EC.
258  * @ec_dev: EC device.
259  * @msg: Message to write.
260  *
261  * Call this to send a command to the ChromeOS EC.  This should be used
262  * instead of calling the EC's cmd_xfer() callback directly.
263  *
264  * Return: 0 on success or negative error code.
265  */
266 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
267                      struct cros_ec_command *msg);
268
269 /**
270  * cros_ec_cmd_xfer_status() - Send a command to the ChromeOS EC.
271  * @ec_dev: EC device.
272  * @msg: Message to write.
273  *
274  * This function is identical to cros_ec_cmd_xfer, except it returns success
275  * status only if both the command was transmitted successfully and the EC
276  * replied with success status. It's not necessary to check msg->result when
277  * using this function.
278  *
279  * Return: The number of bytes transferred on success or negative error code.
280  */
281 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
282                             struct cros_ec_command *msg);
283
284 /**
285  * cros_ec_remove() - Remove a ChromeOS EC.
286  * @ec_dev: Device to register.
287  *
288  * Call this to deregister a ChromeOS EC, then clean up any private data.
289  *
290  * Return: 0 on success or negative error code.
291  */
292 int cros_ec_remove(struct cros_ec_device *ec_dev);
293
294 /**
295  * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
296  * @ec_dev: Device to register.
297  *
298  * Before calling this, allocate a pointer to a new device and then fill
299  * in all the fields up to the --private-- marker.
300  *
301  * Return: 0 on success or negative error code.
302  */
303 int cros_ec_register(struct cros_ec_device *ec_dev);
304
305 /**
306  * cros_ec_query_all() -  Query the protocol version supported by the
307  *         ChromeOS EC.
308  * @ec_dev: Device to register.
309  *
310  * Return: 0 on success or negative error code.
311  */
312 int cros_ec_query_all(struct cros_ec_device *ec_dev);
313
314 /**
315  * cros_ec_get_next_event() - Fetch next event from the ChromeOS EC.
316  * @ec_dev: Device to fetch event from.
317  * @wake_event: Pointer to a bool set to true upon return if the event might be
318  *              treated as a wake event. Ignored if null.
319  *
320  * Return: 0 on success or negative error code.
321  */
322 int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event);
323
324 /**
325  * cros_ec_get_host_event() - Return a mask of event set by the ChromeOS EC.
326  * @ec_dev: Device to fetch event from.
327  *
328  * When MKBP is supported, when the EC raises an interrupt, we collect the
329  * events raised and call the functions in the ec notifier. This function
330  * is a helper to know which events are raised.
331  *
332  * Return: 0 on success or negative error code.
333  */
334 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
335
336 /* sysfs stuff */
337 extern struct attribute_group cros_ec_attr_group;
338 extern struct attribute_group cros_ec_lightbar_attr_group;
339 extern struct attribute_group cros_ec_vbc_attr_group;
340
341 /* debugfs stuff */
342 int cros_ec_debugfs_init(struct cros_ec_dev *ec);
343 void cros_ec_debugfs_remove(struct cros_ec_dev *ec);
344 void cros_ec_debugfs_suspend(struct cros_ec_dev *ec);
345 void cros_ec_debugfs_resume(struct cros_ec_dev *ec);
346
347 #endif /* __LINUX_MFD_CROS_EC_H */