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