Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 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 | |
Javier Martinez Canillas | 05c11ac | 2015-02-02 12:26:23 +0100 | [diff] [blame] | 19 | #include <linux/cdev.h> |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 20 | #include <linux/device.h> |
Bill Richardson | 7e6cb5b | 2014-06-18 11:14:00 -0700 | [diff] [blame] | 21 | #include <linux/notifier.h> |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 22 | #include <linux/mfd/cros_ec_commands.h> |
Bill Richardson | 7e6cb5b | 2014-06-18 11:14:00 -0700 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 24 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 25 | #define CROS_EC_DEV_NAME "cros_ec" |
| 26 | #define CROS_EC_DEV_PD_NAME "cros_pd" |
| 27 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 28 | /* |
Stephen Barber | d365407 | 2015-06-09 13:04:46 +0200 | [diff] [blame] | 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 | /* |
Stephen Barber | 2c7589a | 2015-06-09 13:04:45 +0200 | [diff] [blame] | 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 | * */ |
| 39 | #define EC_PROTO_VERSION_UNKNOWN 0 |
| 40 | #define EC_MAX_REQUEST_OVERHEAD 1 |
| 41 | #define EC_MAX_RESPONSE_OVERHEAD 2 |
| 42 | |
| 43 | /* |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 44 | * Command interface between EC and AP, for LPC, I2C and SPI interfaces. |
| 45 | */ |
| 46 | enum { |
| 47 | EC_MSG_TX_HEADER_BYTES = 3, |
| 48 | EC_MSG_TX_TRAILER_BYTES = 1, |
| 49 | EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES + |
| 50 | EC_MSG_TX_TRAILER_BYTES, |
| 51 | EC_MSG_RX_PROTO_BYTES = 3, |
| 52 | |
Gwendal Grignou | 5d749d0 | 2016-03-08 09:13:52 -0800 | [diff] [blame] | 53 | /* Max length of messages for proto 2*/ |
| 54 | EC_PROTO2_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE + |
Bill Richardson | 5271db2 | 2014-04-30 10:44:08 -0700 | [diff] [blame] | 55 | EC_MSG_TX_PROTO_BYTES, |
Gwendal Grignou | 5d749d0 | 2016-03-08 09:13:52 -0800 | [diff] [blame] | 56 | |
| 57 | EC_MAX_MSG_BYTES = 64 * 1024, |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 58 | }; |
| 59 | |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 60 | /* |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 61 | * @version: Command version number (often 0) |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 62 | * @command: Command to send (EC_CMD_...) |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 63 | * @outsize: Outgoing length in bytes |
Bill Richardson | 12ebc8a | 2014-06-18 11:14:06 -0700 | [diff] [blame] | 64 | * @insize: Max number of bytes to accept from EC |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 65 | * @result: EC's response to the command (separate from communication failure) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 66 | * @data: Where to put the incoming data from EC and outgoing data to EC |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 67 | */ |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 68 | struct cros_ec_command { |
| 69 | uint32_t version; |
| 70 | uint32_t command; |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 71 | uint32_t outsize; |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 72 | uint32_t insize; |
| 73 | uint32_t result; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 74 | uint8_t data[0]; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * struct cros_ec_device - Information about a ChromeOS EC device |
| 79 | * |
Bill Richardson | 7e6cb5b | 2014-06-18 11:14:00 -0700 | [diff] [blame] | 80 | * @phys_name: name of physical comms layer (e.g. 'i2c-4') |
Javier Martinez Canillas | 05c11ac | 2015-02-02 12:26:23 +0100 | [diff] [blame] | 81 | * @dev: Device pointer for physical comms device |
Bill Richardson | 7e6cb5b | 2014-06-18 11:14:00 -0700 | [diff] [blame] | 82 | * @was_wake_device: true if this device was set to wake the system from |
| 83 | * sleep at the last suspend |
Javier Martinez Canillas | 05c11ac | 2015-02-02 12:26:23 +0100 | [diff] [blame] | 84 | * @cmd_readmem: direct read of the EC memory-mapped region, if supported |
| 85 | * @offset is within EC_LPC_ADDR_MEMMAP region. |
| 86 | * @bytes: number of bytes to read. zero means "read a string" (including |
| 87 | * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be read. |
| 88 | * Caller must ensure that the buffer is large enough for the result when |
| 89 | * reading a string. |
Bill Richardson | 7e6cb5b | 2014-06-18 11:14:00 -0700 | [diff] [blame] | 90 | * |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 91 | * @priv: Private data |
| 92 | * @irq: Interrupt to use |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 93 | * @id: Device id |
Bill Richardson | 7e6cb5b | 2014-06-18 11:14:00 -0700 | [diff] [blame] | 94 | * @din: input buffer (for data from EC) |
| 95 | * @dout: output buffer (for data to EC) |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 96 | * \note |
| 97 | * These two buffers will always be dword-aligned and include enough |
| 98 | * space for up to 7 word-alignment bytes also, so we can ensure that |
| 99 | * the body of the message is always dword-aligned (64-bit). |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 100 | * We use this alignment to keep ARM and x86 happy. Probably word |
| 101 | * alignment would be OK, there might be a small performance advantage |
| 102 | * to using dword. |
Bill Richardson | 2ce701a | 2014-06-18 11:13:59 -0700 | [diff] [blame] | 103 | * @din_size: size of din buffer to allocate (zero to use static din) |
| 104 | * @dout_size: size of dout buffer to allocate (zero to use static dout) |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 105 | * @wake_enabled: true if this device can wake the system from sleep |
Andrew Bresticker | a6551a7 | 2014-09-18 17:18:56 +0200 | [diff] [blame] | 106 | * @cmd_xfer: send command to EC and get response |
| 107 | * Returns the number of bytes received if the communication succeeded, but |
| 108 | * that doesn't mean the EC was happy with the command. The caller |
| 109 | * should check msg.result for the EC's result code. |
Stephen Barber | 2c7589a | 2015-06-09 13:04:45 +0200 | [diff] [blame] | 110 | * @pkt_xfer: send packet to EC and get response |
Bill Richardson | 7e6cb5b | 2014-06-18 11:14:00 -0700 | [diff] [blame] | 111 | * @lock: one transaction at a time |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 112 | */ |
| 113 | struct cros_ec_device { |
Bill Richardson | 7e6cb5b | 2014-06-18 11:14:00 -0700 | [diff] [blame] | 114 | |
| 115 | /* These are used by other drivers that want to talk to the EC */ |
Bill Richardson | 7e6cb5b | 2014-06-18 11:14:00 -0700 | [diff] [blame] | 116 | const char *phys_name; |
| 117 | struct device *dev; |
| 118 | bool was_wake_device; |
| 119 | struct class *cros_class; |
Javier Martinez Canillas | 05c11ac | 2015-02-02 12:26:23 +0100 | [diff] [blame] | 120 | int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset, |
| 121 | unsigned int bytes, void *dest); |
Bill Richardson | 7e6cb5b | 2014-06-18 11:14:00 -0700 | [diff] [blame] | 122 | |
| 123 | /* These are used to implement the platform-specific interface */ |
Stephen Barber | 2c7589a | 2015-06-09 13:04:45 +0200 | [diff] [blame] | 124 | u16 max_request; |
| 125 | u16 max_response; |
| 126 | u16 max_passthru; |
| 127 | u16 proto_version; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 128 | void *priv; |
| 129 | int irq; |
Stephen Barber | 2c7589a | 2015-06-09 13:04:45 +0200 | [diff] [blame] | 130 | u8 *din; |
| 131 | u8 *dout; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 132 | int din_size; |
| 133 | int dout_size; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 134 | bool wake_enabled; |
Andrew Bresticker | a6551a7 | 2014-09-18 17:18:56 +0200 | [diff] [blame] | 135 | int (*cmd_xfer)(struct cros_ec_device *ec, |
| 136 | struct cros_ec_command *msg); |
Stephen Barber | 2c7589a | 2015-06-09 13:04:45 +0200 | [diff] [blame] | 137 | int (*pkt_xfer)(struct cros_ec_device *ec, |
| 138 | struct cros_ec_command *msg); |
Bill Richardson | 7e6cb5b | 2014-06-18 11:14:00 -0700 | [diff] [blame] | 139 | struct mutex lock; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 142 | /* struct cros_ec_platform - ChromeOS EC platform information |
| 143 | * |
| 144 | * @ec_name: name of EC device (e.g. 'cros-ec', 'cros-pd', ...) |
| 145 | * used in /dev/ and sysfs. |
| 146 | * @cmd_offset: offset to apply for each command. Set when |
| 147 | * registering a devicde behind another one. |
| 148 | */ |
| 149 | struct cros_ec_platform { |
| 150 | const char *ec_name; |
| 151 | u16 cmd_offset; |
| 152 | }; |
| 153 | |
| 154 | /* |
| 155 | * struct cros_ec_dev - ChromeOS EC device entry point |
| 156 | * |
| 157 | * @class_dev: Device structure used in sysfs |
| 158 | * @cdev: Character device structure in /dev |
| 159 | * @ec_dev: cros_ec_device structure to talk to the physical device |
| 160 | * @dev: pointer to the platform device |
| 161 | * @cmd_offset: offset to apply for each command. |
| 162 | */ |
| 163 | struct cros_ec_dev { |
| 164 | struct device class_dev; |
| 165 | struct cdev cdev; |
| 166 | struct cros_ec_device *ec_dev; |
| 167 | struct device *dev; |
| 168 | u16 cmd_offset; |
| 169 | }; |
| 170 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 171 | /** |
| 172 | * cros_ec_suspend - Handle a suspend operation for the ChromeOS EC device |
| 173 | * |
| 174 | * This can be called by drivers to handle a suspend event. |
| 175 | * |
| 176 | * ec_dev: Device to suspend |
| 177 | * @return 0 if ok, -ve on error |
| 178 | */ |
| 179 | int cros_ec_suspend(struct cros_ec_device *ec_dev); |
| 180 | |
| 181 | /** |
| 182 | * cros_ec_resume - Handle a resume operation for the ChromeOS EC device |
| 183 | * |
| 184 | * This can be called by drivers to handle a resume event. |
| 185 | * |
| 186 | * @ec_dev: Device to resume |
| 187 | * @return 0 if ok, -ve on error |
| 188 | */ |
| 189 | int cros_ec_resume(struct cros_ec_device *ec_dev); |
| 190 | |
| 191 | /** |
| 192 | * cros_ec_prepare_tx - Prepare an outgoing message in the output buffer |
| 193 | * |
| 194 | * This is intended to be used by all ChromeOS EC drivers, but at present |
| 195 | * only SPI uses it. Once LPC uses the same protocol it can start using it. |
| 196 | * I2C could use it now, with a refactor of the existing code. |
| 197 | * |
| 198 | * @ec_dev: Device to register |
| 199 | * @msg: Message to write |
| 200 | */ |
| 201 | int cros_ec_prepare_tx(struct cros_ec_device *ec_dev, |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 202 | struct cros_ec_command *msg); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 203 | |
| 204 | /** |
Bill Richardson | 6db07b6 | 2014-06-18 11:14:05 -0700 | [diff] [blame] | 205 | * cros_ec_check_result - Check ec_msg->result |
| 206 | * |
| 207 | * This is used by ChromeOS EC drivers to check the ec_msg->result for |
| 208 | * errors and to warn about them. |
| 209 | * |
| 210 | * @ec_dev: EC device |
| 211 | * @msg: Message to check |
| 212 | */ |
| 213 | int cros_ec_check_result(struct cros_ec_device *ec_dev, |
| 214 | struct cros_ec_command *msg); |
| 215 | |
| 216 | /** |
Andrew Bresticker | a6551a7 | 2014-09-18 17:18:56 +0200 | [diff] [blame] | 217 | * cros_ec_cmd_xfer - Send a command to the ChromeOS EC |
| 218 | * |
| 219 | * Call this to send a command to the ChromeOS EC. This should be used |
| 220 | * instead of calling the EC's cmd_xfer() callback directly. |
| 221 | * |
| 222 | * @ec_dev: EC device |
| 223 | * @msg: Message to write |
| 224 | */ |
| 225 | int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev, |
| 226 | struct cros_ec_command *msg); |
| 227 | |
| 228 | /** |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 229 | * cros_ec_remove - Remove a ChromeOS EC |
| 230 | * |
Bill Richardson | ee98662 | 2014-06-18 11:13:58 -0700 | [diff] [blame] | 231 | * Call this to deregister a ChromeOS EC, then clean up any private data. |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 232 | * |
| 233 | * @ec_dev: Device to register |
| 234 | * @return 0 if ok, -ve on error |
| 235 | */ |
| 236 | int cros_ec_remove(struct cros_ec_device *ec_dev); |
| 237 | |
| 238 | /** |
| 239 | * cros_ec_register - Register a new ChromeOS EC, using the provided info |
| 240 | * |
| 241 | * Before calling this, allocate a pointer to a new device and then fill |
| 242 | * in all the fields up to the --private-- marker. |
| 243 | * |
| 244 | * @ec_dev: Device to register |
| 245 | * @return 0 if ok, -ve on error |
| 246 | */ |
| 247 | int cros_ec_register(struct cros_ec_device *ec_dev); |
| 248 | |
Stephen Barber | 2c7589a | 2015-06-09 13:04:45 +0200 | [diff] [blame] | 249 | /** |
Tomeu Vizoso | f4bcf5a | 2016-02-05 14:32:56 +0100 | [diff] [blame] | 250 | * cros_ec_query_all - Query the protocol version supported by the ChromeOS EC |
Stephen Barber | 2c7589a | 2015-06-09 13:04:45 +0200 | [diff] [blame] | 251 | * |
| 252 | * @ec_dev: Device to register |
| 253 | * @return 0 if ok, -ve on error |
| 254 | */ |
| 255 | int cros_ec_query_all(struct cros_ec_device *ec_dev); |
| 256 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 257 | /* sysfs stuff */ |
| 258 | extern struct attribute_group cros_ec_attr_group; |
| 259 | extern struct attribute_group cros_ec_lightbar_attr_group; |
Emilio López | 18800fc | 2015-09-21 10:38:22 -0300 | [diff] [blame] | 260 | extern struct attribute_group cros_ec_vbc_attr_group; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 261 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 262 | #endif /* __LINUX_MFD_CROS_EC_H */ |