1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * ChromeOS Embedded Controller protocol interface. 4 * 5 * Copyright (C) 2012 Google, Inc 6 */ 7 8 #ifndef __LINUX_CROS_EC_PROTO_H 9 #define __LINUX_CROS_EC_PROTO_H 10 11 #include <linux/device.h> 12 #include <linux/mutex.h> 13 #include <linux/notifier.h> 14 15 #include <linux/mfd/cros_ec.h> 16 #include <linux/platform_data/cros_ec_commands.h> 17 18 #define CROS_EC_DEV_NAME "cros_ec" 19 #define CROS_EC_DEV_FP_NAME "cros_fp" 20 #define CROS_EC_DEV_ISH_NAME "cros_ish" 21 #define CROS_EC_DEV_PD_NAME "cros_pd" 22 #define CROS_EC_DEV_SCP_NAME "cros_scp" 23 #define CROS_EC_DEV_TP_NAME "cros_tp" 24 25 /* 26 * The EC is unresponsive for a time after a reboot command. Add a 27 * simple delay to make sure that the bus stays locked. 28 */ 29 #define EC_REBOOT_DELAY_MS 50 30 31 /* 32 * Max bus-specific overhead incurred by request/responses. 33 * I2C requires 1 additional byte for requests. 34 * I2C requires 2 additional bytes for responses. 35 * SPI requires up to 32 additional bytes for responses. 36 */ 37 #define EC_PROTO_VERSION_UNKNOWN 0 38 #define EC_MAX_REQUEST_OVERHEAD 1 39 #define EC_MAX_RESPONSE_OVERHEAD 32 40 41 /* 42 * Command interface between EC and AP, for LPC, I2C and SPI interfaces. 43 */ 44 enum { 45 EC_MSG_TX_HEADER_BYTES = 3, 46 EC_MSG_TX_TRAILER_BYTES = 1, 47 EC_MSG_TX_PROTO_BYTES = EC_MSG_TX_HEADER_BYTES + 48 EC_MSG_TX_TRAILER_BYTES, 49 EC_MSG_RX_PROTO_BYTES = 3, 50 51 /* Max length of messages for proto 2*/ 52 EC_PROTO2_MSG_BYTES = EC_PROTO2_MAX_PARAM_SIZE + 53 EC_MSG_TX_PROTO_BYTES, 54 55 EC_MAX_MSG_BYTES = 64 * 1024, 56 }; 57 58 /** 59 * struct cros_ec_command - Information about a ChromeOS EC command. 60 * @version: Command version number (often 0). 61 * @command: Command to send (EC_CMD_...). 62 * @outsize: Outgoing length in bytes. 63 * @insize: Max number of bytes to accept from the EC. 64 * @result: EC's response to the command (separate from communication failure). 65 * @data: Where to put the incoming data from EC and outgoing data to EC. 66 */ 67 struct cros_ec_command { 68 uint32_t version; 69 uint32_t command; 70 uint32_t outsize; 71 uint32_t insize; 72 uint32_t result; 73 uint8_t data[0]; 74 }; 75 76 /** 77 * struct cros_ec_device - Information about a ChromeOS EC device. 78 * @phys_name: Name of physical comms layer (e.g. 'i2c-4'). 79 * @dev: Device pointer for physical comms device 80 * @was_wake_device: True if this device was set to wake the system from 81 * sleep at the last suspend. 82 * @cros_class: The class structure for this device. 83 * @cmd_readmem: Direct read of the EC memory-mapped region, if supported. 84 * @offset: Is within EC_LPC_ADDR_MEMMAP region. 85 * @bytes: Number of bytes to read. zero means "read a string" (including 86 * the trailing '\0'). At most only EC_MEMMAP_SIZE bytes can be 87 * read. Caller must ensure that the buffer is large enough for the 88 * result when reading a string. 89 * @max_request: Max size of message requested. 90 * @max_response: Max size of message response. 91 * @max_passthru: Max sice of passthru message. 92 * @proto_version: The protocol version used for this device. 93 * @priv: Private data. 94 * @irq: Interrupt to use. 95 * @id: Device id. 96 * @din: Input buffer (for data from EC). This buffer will always be 97 * dword-aligned and include enough space for up to 7 word-alignment 98 * bytes also, so we can ensure that the body of the message is always 99 * dword-aligned (64-bit). We use this alignment to keep ARM and x86 100 * happy. Probably word alignment would be OK, there might be a small 101 * performance advantage to using dword. 102 * @dout: Output buffer (for data to EC). This buffer will always be 103 * dword-aligned and include enough space for up to 7 word-alignment 104 * bytes also, so we can ensure that the body of the message is always 105 * dword-aligned (64-bit). We use this alignment to keep ARM and x86 106 * happy. Probably word alignment would be OK, there might be a small 107 * performance advantage to using dword. 108 * @din_size: Size of din buffer to allocate (zero to use static din). 109 * @dout_size: Size of dout buffer to allocate (zero to use static dout). 110 * @wake_enabled: True if this device can wake the system from sleep. 111 * @suspended: True if this device had been suspended. 112 * @cmd_xfer: Send command to EC and get response. 113 * Returns the number of bytes received if the communication 114 * succeeded, but that doesn't mean the EC was happy with the 115 * command. The caller should check msg.result for the EC's result 116 * code. 117 * @pkt_xfer: Send packet to EC and get response. 118 * @lock: One transaction at a time. 119 * @mkbp_event_supported: 0 if MKBP not supported. Otherwise its value is 120 * the maximum supported version of the MKBP host event 121 * command + 1. 122 * @host_sleep_v1: True if this EC supports the sleep v1 command. 123 * @event_notifier: Interrupt event notifier for transport devices. 124 * @event_data: Raw payload transferred with the MKBP event. 125 * @event_size: Size in bytes of the event data. 126 * @host_event_wake_mask: Mask of host events that cause wake from suspend. 127 * @last_event_time: exact time from the hard irq when we got notified of 128 * a new event. 129 * @ec: The platform_device used by the mfd driver to interface with the 130 * main EC. 131 * @pd: The platform_device used by the mfd driver to interface with the 132 * PD behind an EC. 133 */ 134 struct cros_ec_device { 135 /* These are used by other drivers that want to talk to the EC */ 136 const char *phys_name; 137 struct device *dev; 138 bool was_wake_device; 139 struct class *cros_class; 140 int (*cmd_readmem)(struct cros_ec_device *ec, unsigned int offset, 141 unsigned int bytes, void *dest); 142 143 /* These are used to implement the platform-specific interface */ 144 u16 max_request; 145 u16 max_response; 146 u16 max_passthru; 147 u16 proto_version; 148 void *priv; 149 int irq; 150 u8 *din; 151 u8 *dout; 152 int din_size; 153 int dout_size; 154 bool wake_enabled; 155 bool suspended; 156 int (*cmd_xfer)(struct cros_ec_device *ec, 157 struct cros_ec_command *msg); 158 int (*pkt_xfer)(struct cros_ec_device *ec, 159 struct cros_ec_command *msg); 160 struct mutex lock; 161 u8 mkbp_event_supported; 162 bool host_sleep_v1; 163 struct blocking_notifier_head event_notifier; 164 165 struct ec_response_get_next_event_v1 event_data; 166 int event_size; 167 u32 host_event_wake_mask; 168 u32 last_resume_result; 169 ktime_t last_event_time; 170 171 /* The platform devices used by the mfd driver */ 172 struct platform_device *ec; 173 struct platform_device *pd; 174 }; 175 176 /** 177 * struct cros_ec_platform - ChromeOS EC platform information. 178 * @ec_name: Name of EC device (e.g. 'cros-ec', 'cros-pd', ...) 179 * used in /dev/ and sysfs. 180 * @cmd_offset: Offset to apply for each command. Set when 181 * registering a device behind another one. 182 */ 183 struct cros_ec_platform { 184 const char *ec_name; 185 u16 cmd_offset; 186 }; 187 188 int cros_ec_suspend(struct cros_ec_device *ec_dev); 189 190 int cros_ec_resume(struct cros_ec_device *ec_dev); 191 192 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev, 193 struct cros_ec_command *msg); 194 195 int cros_ec_check_result(struct cros_ec_device *ec_dev, 196 struct cros_ec_command *msg); 197 198 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev, 199 struct cros_ec_command *msg); 200 201 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev, 202 struct cros_ec_command *msg); 203 204 int cros_ec_register(struct cros_ec_device *ec_dev); 205 206 int cros_ec_unregister(struct cros_ec_device *ec_dev); 207 208 int cros_ec_query_all(struct cros_ec_device *ec_dev); 209 210 int cros_ec_get_next_event(struct cros_ec_device *ec_dev, 211 bool *wake_event, 212 bool *has_more_events); 213 214 u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev); 215 216 int cros_ec_check_features(struct cros_ec_dev *ec, int feature); 217 218 int cros_ec_get_sensor_count(struct cros_ec_dev *ec); 219 220 bool cros_ec_handle_event(struct cros_ec_device *ec_dev); 221 222 /** 223 * cros_ec_get_time_ns() - Return time in ns. 224 * 225 * This is the function used to record the time for last_event_time in struct 226 * cros_ec_device during the hard irq. 227 * 228 * Return: ktime_t format since boot. 229 */ 230 static inline ktime_t cros_ec_get_time_ns(void) 231 { 232 return ktime_get_boottime_ns(); 233 } 234 235 #endif /* __LINUX_CROS_EC_PROTO_H */ 236