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