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