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 | |
| 19 | #include <linux/mfd/cros_ec_commands.h> |
| 20 | |
| 21 | /* |
| 22 | * Command interface between EC and AP, for LPC, I2C and SPI interfaces. |
| 23 | */ |
| 24 | enum { |
| 25 | EC_MSG_TX_HEADER_BYTES = 3, |
| 26 | EC_MSG_TX_TRAILER_BYTES = 1, |
| 27 | EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES + |
| 28 | EC_MSG_TX_TRAILER_BYTES, |
| 29 | EC_MSG_RX_PROTO_BYTES = 3, |
| 30 | |
| 31 | /* Max length of messages */ |
| 32 | EC_MSG_BYTES = EC_HOST_PARAM_SIZE + EC_MSG_TX_PROTO_BYTES, |
| 33 | |
| 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * struct cros_ec_msg - A message sent to the EC, and its reply |
| 38 | * |
| 39 | * @version: Command version number (often 0) |
| 40 | * @cmd: Command to send (EC_CMD_...) |
| 41 | * @out_buf: Outgoing payload (to EC) |
| 42 | * @outlen: Outgoing length |
| 43 | * @in_buf: Incoming payload (from EC) |
| 44 | * @in_len: Incoming length |
| 45 | */ |
| 46 | struct cros_ec_msg { |
| 47 | u8 version; |
| 48 | u8 cmd; |
| 49 | uint8_t *out_buf; |
| 50 | int out_len; |
| 51 | uint8_t *in_buf; |
| 52 | int in_len; |
| 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * struct cros_ec_device - Information about a ChromeOS EC device |
| 57 | * |
| 58 | * @name: Name of this EC interface |
| 59 | * @priv: Private data |
| 60 | * @irq: Interrupt to use |
| 61 | * @din: input buffer (from EC) |
| 62 | * @dout: output buffer (to EC) |
| 63 | * \note |
| 64 | * These two buffers will always be dword-aligned and include enough |
| 65 | * space for up to 7 word-alignment bytes also, so we can ensure that |
| 66 | * the body of the message is always dword-aligned (64-bit). |
| 67 | * |
| 68 | * We use this alignment to keep ARM and x86 happy. Probably word |
| 69 | * alignment would be OK, there might be a small performance advantage |
| 70 | * to using dword. |
| 71 | * @din_size: size of din buffer |
| 72 | * @dout_size: size of dout buffer |
| 73 | * @command_send: send a command |
| 74 | * @command_recv: receive a command |
| 75 | * @ec_name: name of EC device (e.g. 'chromeos-ec') |
| 76 | * @phys_name: name of physical comms layer (e.g. 'i2c-4') |
| 77 | * @parent: pointer to parent device (e.g. i2c or spi device) |
| 78 | * @dev: Device pointer |
| 79 | * dev_lock: Lock to prevent concurrent access |
| 80 | * @wake_enabled: true if this device can wake the system from sleep |
| 81 | * @was_wake_device: true if this device was set to wake the system from |
| 82 | * sleep at the last suspend |
| 83 | * @event_notifier: interrupt event notifier for transport devices |
| 84 | */ |
| 85 | struct cros_ec_device { |
| 86 | const char *name; |
| 87 | void *priv; |
| 88 | int irq; |
| 89 | uint8_t *din; |
| 90 | uint8_t *dout; |
| 91 | int din_size; |
| 92 | int dout_size; |
| 93 | int (*command_send)(struct cros_ec_device *ec, |
| 94 | uint16_t cmd, void *out_buf, int out_len); |
| 95 | int (*command_recv)(struct cros_ec_device *ec, |
| 96 | uint16_t cmd, void *in_buf, int in_len); |
| 97 | int (*command_sendrecv)(struct cros_ec_device *ec, |
| 98 | uint16_t cmd, void *out_buf, int out_len, |
| 99 | void *in_buf, int in_len); |
| 100 | int (*command_xfer)(struct cros_ec_device *ec, |
| 101 | struct cros_ec_msg *msg); |
| 102 | |
| 103 | const char *ec_name; |
| 104 | const char *phys_name; |
| 105 | struct device *parent; |
| 106 | |
| 107 | /* These are --private-- fields - do not assign */ |
| 108 | struct device *dev; |
| 109 | struct mutex dev_lock; |
| 110 | bool wake_enabled; |
| 111 | bool was_wake_device; |
| 112 | struct blocking_notifier_head event_notifier; |
| 113 | }; |
| 114 | |
| 115 | /** |
| 116 | * cros_ec_suspend - Handle a suspend operation for the ChromeOS EC device |
| 117 | * |
| 118 | * This can be called by drivers to handle a suspend event. |
| 119 | * |
| 120 | * ec_dev: Device to suspend |
| 121 | * @return 0 if ok, -ve on error |
| 122 | */ |
| 123 | int cros_ec_suspend(struct cros_ec_device *ec_dev); |
| 124 | |
| 125 | /** |
| 126 | * cros_ec_resume - Handle a resume operation for the ChromeOS EC device |
| 127 | * |
| 128 | * This can be called by drivers to handle a resume event. |
| 129 | * |
| 130 | * @ec_dev: Device to resume |
| 131 | * @return 0 if ok, -ve on error |
| 132 | */ |
| 133 | int cros_ec_resume(struct cros_ec_device *ec_dev); |
| 134 | |
| 135 | /** |
| 136 | * cros_ec_prepare_tx - Prepare an outgoing message in the output buffer |
| 137 | * |
| 138 | * This is intended to be used by all ChromeOS EC drivers, but at present |
| 139 | * only SPI uses it. Once LPC uses the same protocol it can start using it. |
| 140 | * I2C could use it now, with a refactor of the existing code. |
| 141 | * |
| 142 | * @ec_dev: Device to register |
| 143 | * @msg: Message to write |
| 144 | */ |
| 145 | int cros_ec_prepare_tx(struct cros_ec_device *ec_dev, |
| 146 | struct cros_ec_msg *msg); |
| 147 | |
| 148 | /** |
| 149 | * cros_ec_remove - Remove a ChromeOS EC |
| 150 | * |
| 151 | * Call this to deregister a ChromeOS EC. After this you should call |
| 152 | * cros_ec_free(). |
| 153 | * |
| 154 | * @ec_dev: Device to register |
| 155 | * @return 0 if ok, -ve on error |
| 156 | */ |
| 157 | int cros_ec_remove(struct cros_ec_device *ec_dev); |
| 158 | |
| 159 | /** |
| 160 | * cros_ec_register - Register a new ChromeOS EC, using the provided info |
| 161 | * |
| 162 | * Before calling this, allocate a pointer to a new device and then fill |
| 163 | * in all the fields up to the --private-- marker. |
| 164 | * |
| 165 | * @ec_dev: Device to register |
| 166 | * @return 0 if ok, -ve on error |
| 167 | */ |
| 168 | int cros_ec_register(struct cros_ec_device *ec_dev); |
| 169 | |
| 170 | #endif /* __LINUX_MFD_CROS_EC_H */ |