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