Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec...
[linux-2.6/btrfs-unstable.git] / include / linux / mfd / cros_ec.h
blob4e887ba22635ecf9c04adc1b0d1b17d106c37022
1 /*
2 * ChromeOS EC multi-function device
4 * Copyright (C) 2012 Google, Inc
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.
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.
16 #ifndef __LINUX_MFD_CROS_EC_H
17 #define __LINUX_MFD_CROS_EC_H
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>
25 #define CROS_EC_DEV_NAME "cros_ec"
26 #define CROS_EC_DEV_PD_NAME "cros_pd"
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.
32 #define EC_REBOOT_DELAY_MS 50
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
45 * Command interface between EC and AP, for LPC, I2C and SPI interfaces.
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,
54 /* Max length of messages for proto 2*/
55 EC_PROTO2_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE +
56 EC_MSG_TX_PROTO_BYTES,
58 EC_MAX_MSG_BYTES = 64 * 1024,
62 * @version: Command version number (often 0)
63 * @command: Command to send (EC_CMD_...)
64 * @outsize: Outgoing length in bytes
65 * @insize: Max number of bytes to accept from EC
66 * @result: EC's response to the command (separate from communication failure)
67 * @data: Where to put the incoming data from EC and outgoing data to EC
69 struct cros_ec_command {
70 uint32_t version;
71 uint32_t command;
72 uint32_t outsize;
73 uint32_t insize;
74 uint32_t result;
75 uint8_t data[0];
78 /**
79 * 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 * @cmd_readmem: direct read of the EC memory-mapped region, if supported
86 * @offset is within EC_LPC_ADDR_MEMMAP region.
87 * @bytes: number of bytes to read. zero means "read a string" (including
88 * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be read.
89 * Caller must ensure that the buffer is large enough for the result when
90 * reading a string.
92 * @priv: Private data
93 * @irq: Interrupt to use
94 * @id: Device id
95 * @din: input buffer (for data from EC)
96 * @dout: output buffer (for data to EC)
97 * \note
98 * These two buffers will always be dword-aligned and include enough
99 * space for up to 7 word-alignment bytes also, so we can ensure that
100 * the body of the message is always dword-aligned (64-bit).
101 * We use this alignment to keep ARM and x86 happy. Probably word
102 * alignment would be OK, there might be a small performance advantage
103 * to using dword.
104 * @din_size: size of din buffer to allocate (zero to use static din)
105 * @dout_size: size of dout buffer to allocate (zero to use static dout)
106 * @wake_enabled: true if this device can wake the system from sleep
107 * @suspended: true if this device had been suspended
108 * @cmd_xfer: send command to EC and get response
109 * Returns the number of bytes received if the communication succeeded, but
110 * that doesn't mean the EC was happy with the command. The caller
111 * should check msg.result for the EC's result code.
112 * @pkt_xfer: send packet to EC and get response
113 * @lock: one transaction at a time
114 * @mkbp_event_supported: true if this EC supports the MKBP event protocol.
115 * @event_notifier: interrupt event notifier for transport devices.
116 * @event_data: raw payload transferred with the MKBP event.
117 * @event_size: size in bytes of the event data.
119 struct cros_ec_device {
121 /* These are used by other drivers that want to talk to the EC */
122 const char *phys_name;
123 struct device *dev;
124 bool was_wake_device;
125 struct class *cros_class;
126 int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset,
127 unsigned int bytes, void *dest);
129 /* These are used to implement the platform-specific interface */
130 u16 max_request;
131 u16 max_response;
132 u16 max_passthru;
133 u16 proto_version;
134 void *priv;
135 int irq;
136 u8 *din;
137 u8 *dout;
138 int din_size;
139 int dout_size;
140 bool wake_enabled;
141 bool suspended;
142 int (*cmd_xfer)(struct cros_ec_device *ec,
143 struct cros_ec_command *msg);
144 int (*pkt_xfer)(struct cros_ec_device *ec,
145 struct cros_ec_command *msg);
146 struct mutex lock;
147 bool mkbp_event_supported;
148 struct blocking_notifier_head event_notifier;
150 struct ec_response_get_next_event event_data;
151 int event_size;
152 u32 host_event_wake_mask;
156 * struct cros_ec_sensor_platform - ChromeOS EC sensor platform information
158 * @sensor_num: Id of the sensor, as reported by the EC.
160 struct cros_ec_sensor_platform {
161 u8 sensor_num;
164 /* struct cros_ec_platform - ChromeOS EC platform information
166 * @ec_name: name of EC device (e.g. 'cros-ec', 'cros-pd', ...)
167 * used in /dev/ and sysfs.
168 * @cmd_offset: offset to apply for each command. Set when
169 * registering a devicde behind another one.
171 struct cros_ec_platform {
172 const char *ec_name;
173 u16 cmd_offset;
176 struct cros_ec_debugfs;
179 * struct cros_ec_dev - ChromeOS EC device entry point
181 * @class_dev: Device structure used in sysfs
182 * @cdev: Character device structure in /dev
183 * @ec_dev: cros_ec_device structure to talk to the physical device
184 * @dev: pointer to the platform device
185 * @debug_info: cros_ec_debugfs structure for debugging information
186 * @cmd_offset: offset to apply for each command.
188 struct cros_ec_dev {
189 struct device class_dev;
190 struct cdev cdev;
191 struct cros_ec_device *ec_dev;
192 struct device *dev;
193 struct cros_ec_debugfs *debug_info;
194 u16 cmd_offset;
195 u32 features[2];
199 * cros_ec_suspend - Handle a suspend operation for the ChromeOS EC device
201 * This can be called by drivers to handle a suspend event.
203 * ec_dev: Device to suspend
204 * @return 0 if ok, -ve on error
206 int cros_ec_suspend(struct cros_ec_device *ec_dev);
209 * cros_ec_resume - Handle a resume operation for the ChromeOS EC device
211 * This can be called by drivers to handle a resume event.
213 * @ec_dev: Device to resume
214 * @return 0 if ok, -ve on error
216 int cros_ec_resume(struct cros_ec_device *ec_dev);
219 * cros_ec_prepare_tx - Prepare an outgoing message in the output buffer
221 * This is intended to be used by all ChromeOS EC drivers, but at present
222 * only SPI uses it. Once LPC uses the same protocol it can start using it.
223 * I2C could use it now, with a refactor of the existing code.
225 * @ec_dev: Device to register
226 * @msg: Message to write
228 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
229 struct cros_ec_command *msg);
232 * cros_ec_check_result - Check ec_msg->result
234 * This is used by ChromeOS EC drivers to check the ec_msg->result for
235 * errors and to warn about them.
237 * @ec_dev: EC device
238 * @msg: Message to check
240 int cros_ec_check_result(struct cros_ec_device *ec_dev,
241 struct cros_ec_command *msg);
244 * cros_ec_cmd_xfer - Send a command to the ChromeOS EC
246 * Call this to send a command to the ChromeOS EC. This should be used
247 * instead of calling the EC's cmd_xfer() callback directly.
249 * @ec_dev: EC device
250 * @msg: Message to write
252 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
253 struct cros_ec_command *msg);
256 * cros_ec_cmd_xfer_status - Send a command to the ChromeOS EC
258 * This function is identical to cros_ec_cmd_xfer, except it returns success
259 * status only if both the command was transmitted successfully and the EC
260 * replied with success status. It's not necessary to check msg->result when
261 * using this function.
263 * @ec_dev: EC device
264 * @msg: Message to write
265 * @return: Num. of bytes transferred on success, <0 on failure
267 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
268 struct cros_ec_command *msg);
271 * cros_ec_remove - Remove a ChromeOS EC
273 * Call this to deregister a ChromeOS EC, then clean up any private data.
275 * @ec_dev: Device to register
276 * @return 0 if ok, -ve on error
278 int cros_ec_remove(struct cros_ec_device *ec_dev);
281 * cros_ec_register - Register a new ChromeOS EC, using the provided info
283 * Before calling this, allocate a pointer to a new device and then fill
284 * in all the fields up to the --private-- marker.
286 * @ec_dev: Device to register
287 * @return 0 if ok, -ve on error
289 int cros_ec_register(struct cros_ec_device *ec_dev);
292 * cros_ec_query_all - Query the protocol version supported by the ChromeOS EC
294 * @ec_dev: Device to register
295 * @return 0 if ok, -ve on error
297 int cros_ec_query_all(struct cros_ec_device *ec_dev);
300 * cros_ec_get_next_event - Fetch next event from the ChromeOS EC
302 * @ec_dev: Device to fetch event from
303 * @wake_event: Pointer to a bool set to true upon return if the event might be
304 * treated as a wake event. Ignored if null.
306 * Returns: 0 on success, Linux error number on failure
308 int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event);
311 * cros_ec_get_host_event - Return a mask of event set by the EC.
313 * When MKBP is supported, when the EC raises an interrupt,
314 * We collect the events raised and call the functions in the ec notifier.
316 * This function is a helper to know which events are raised.
318 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev);
320 /* sysfs stuff */
321 extern struct attribute_group cros_ec_attr_group;
322 extern struct attribute_group cros_ec_lightbar_attr_group;
323 extern struct attribute_group cros_ec_vbc_attr_group;
325 /* ACPI GPE handler */
326 #ifdef CONFIG_ACPI
328 int cros_ec_acpi_install_gpe_handler(struct device *dev);
329 void cros_ec_acpi_remove_gpe_handler(void);
330 void cros_ec_acpi_clear_gpe(void);
332 #else /* CONFIG_ACPI */
334 static inline int cros_ec_acpi_install_gpe_handler(struct device *dev)
336 return -ENODEV;
338 static inline void cros_ec_acpi_remove_gpe_handler(void) {}
339 static inline void cros_ec_acpi_clear_gpe(void) {}
341 #endif /* CONFIG_ACPI */
343 #endif /* __LINUX_MFD_CROS_EC_H */