]> asedeno.scripts.mit.edu Git - linux.git/blob - include/linux/mfd/cros_ec_commands.h
mfd: cros_ec: Zero BUILD_ macro
[linux.git] / include / linux / mfd / cros_ec_commands.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Host communication command constants for ChromeOS EC
4  *
5  * Copyright (C) 2012 Google, Inc
6  *
7  * NOTE: This file is auto-generated from ChromeOS EC Open Source code from
8  * https://chromium.googlesource.com/chromiumos/platform/ec/+/master/include/ec_commands.h
9  */
10
11 /* Host communication command constants for Chrome EC */
12
13 #ifndef __CROS_EC_COMMANDS_H
14 #define __CROS_EC_COMMANDS_H
15
16
17
18
19 #define BUILD_ASSERT(_cond)
20
21 /*
22  * Current version of this protocol
23  *
24  * TODO(crosbug.com/p/11223): This is effectively useless; protocol is
25  * determined in other ways.  Remove this once the kernel code no longer
26  * depends on it.
27  */
28 #define EC_PROTO_VERSION          0x00000002
29
30 /* Command version mask */
31 #define EC_VER_MASK(version) (1UL << (version))
32
33 /* I/O addresses for ACPI commands */
34 #define EC_LPC_ADDR_ACPI_DATA  0x62
35 #define EC_LPC_ADDR_ACPI_CMD   0x66
36
37 /* I/O addresses for host command */
38 #define EC_LPC_ADDR_HOST_DATA  0x200
39 #define EC_LPC_ADDR_HOST_CMD   0x204
40
41 /* I/O addresses for host command args and params */
42 /* Protocol version 2 */
43 #define EC_LPC_ADDR_HOST_ARGS    0x800  /* And 0x801, 0x802, 0x803 */
44 #define EC_LPC_ADDR_HOST_PARAM   0x804  /* For version 2 params; size is
45                                          * EC_PROTO2_MAX_PARAM_SIZE */
46 /* Protocol version 3 */
47 #define EC_LPC_ADDR_HOST_PACKET  0x800  /* Offset of version 3 packet */
48 #define EC_LPC_HOST_PACKET_SIZE  0x100  /* Max size of version 3 packet */
49
50 /* The actual block is 0x800-0x8ff, but some BIOSes think it's 0x880-0x8ff
51  * and they tell the kernel that so we have to think of it as two parts. */
52 #define EC_HOST_CMD_REGION0    0x800
53 #define EC_HOST_CMD_REGION1    0x880
54 #define EC_HOST_CMD_REGION_SIZE 0x80
55
56 /* EC command register bit functions */
57 #define EC_LPC_CMDR_DATA        (1 << 0)  /* Data ready for host to read */
58 #define EC_LPC_CMDR_PENDING     (1 << 1)  /* Write pending to EC */
59 #define EC_LPC_CMDR_BUSY        (1 << 2)  /* EC is busy processing a command */
60 #define EC_LPC_CMDR_CMD         (1 << 3)  /* Last host write was a command */
61 #define EC_LPC_CMDR_ACPI_BRST   (1 << 4)  /* Burst mode (not used) */
62 #define EC_LPC_CMDR_SCI         (1 << 5)  /* SCI event is pending */
63 #define EC_LPC_CMDR_SMI         (1 << 6)  /* SMI event is pending */
64
65 #define EC_LPC_ADDR_MEMMAP       0x900
66 #define EC_MEMMAP_SIZE         255 /* ACPI IO buffer max is 255 bytes */
67 #define EC_MEMMAP_TEXT_MAX     8   /* Size of a string in the memory map */
68
69 /* The offset address of each type of data in mapped memory. */
70 #define EC_MEMMAP_TEMP_SENSOR      0x00 /* Temp sensors 0x00 - 0x0f */
71 #define EC_MEMMAP_FAN              0x10 /* Fan speeds 0x10 - 0x17 */
72 #define EC_MEMMAP_TEMP_SENSOR_B    0x18 /* More temp sensors 0x18 - 0x1f */
73 #define EC_MEMMAP_ID               0x20 /* 0x20 == 'E', 0x21 == 'C' */
74 #define EC_MEMMAP_ID_VERSION       0x22 /* Version of data in 0x20 - 0x2f */
75 #define EC_MEMMAP_THERMAL_VERSION  0x23 /* Version of data in 0x00 - 0x1f */
76 #define EC_MEMMAP_BATTERY_VERSION  0x24 /* Version of data in 0x40 - 0x7f */
77 #define EC_MEMMAP_SWITCHES_VERSION 0x25 /* Version of data in 0x30 - 0x33 */
78 #define EC_MEMMAP_EVENTS_VERSION   0x26 /* Version of data in 0x34 - 0x3f */
79 #define EC_MEMMAP_HOST_CMD_FLAGS   0x27 /* Host cmd interface flags (8 bits) */
80 /* Unused 0x28 - 0x2f */
81 #define EC_MEMMAP_SWITCHES         0x30 /* 8 bits */
82 /* Unused 0x31 - 0x33 */
83 #define EC_MEMMAP_HOST_EVENTS      0x34 /* 32 bits */
84 /* Reserve 0x38 - 0x3f for additional host event-related stuff */
85 /* Battery values are all 32 bits */
86 #define EC_MEMMAP_BATT_VOLT        0x40 /* Battery Present Voltage */
87 #define EC_MEMMAP_BATT_RATE        0x44 /* Battery Present Rate */
88 #define EC_MEMMAP_BATT_CAP         0x48 /* Battery Remaining Capacity */
89 #define EC_MEMMAP_BATT_FLAG        0x4c /* Battery State, defined below */
90 #define EC_MEMMAP_BATT_DCAP        0x50 /* Battery Design Capacity */
91 #define EC_MEMMAP_BATT_DVLT        0x54 /* Battery Design Voltage */
92 #define EC_MEMMAP_BATT_LFCC        0x58 /* Battery Last Full Charge Capacity */
93 #define EC_MEMMAP_BATT_CCNT        0x5c /* Battery Cycle Count */
94 /* Strings are all 8 bytes (EC_MEMMAP_TEXT_MAX) */
95 #define EC_MEMMAP_BATT_MFGR        0x60 /* Battery Manufacturer String */
96 #define EC_MEMMAP_BATT_MODEL       0x68 /* Battery Model Number String */
97 #define EC_MEMMAP_BATT_SERIAL      0x70 /* Battery Serial Number String */
98 #define EC_MEMMAP_BATT_TYPE        0x78 /* Battery Type String */
99 #define EC_MEMMAP_ALS              0x80 /* ALS readings in lux (2 X 16 bits) */
100 /* Unused 0x84 - 0x8f */
101 #define EC_MEMMAP_ACC_STATUS       0x90 /* Accelerometer status (8 bits )*/
102 /* Unused 0x91 */
103 #define EC_MEMMAP_ACC_DATA         0x92 /* Accelerometer data 0x92 - 0x9f */
104 #define EC_MEMMAP_GYRO_DATA        0xa0 /* Gyroscope data 0xa0 - 0xa5 */
105 /* Unused 0xa6 - 0xfe (remember, 0xff is NOT part of the memmap region) */
106
107
108 /* Define the format of the accelerometer mapped memory status byte. */
109 #define EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK  0x0f
110 #define EC_MEMMAP_ACC_STATUS_BUSY_BIT        (1 << 4)
111 #define EC_MEMMAP_ACC_STATUS_PRESENCE_BIT    (1 << 7)
112
113 /* Number of temp sensors at EC_MEMMAP_TEMP_SENSOR */
114 #define EC_TEMP_SENSOR_ENTRIES     16
115 /*
116  * Number of temp sensors at EC_MEMMAP_TEMP_SENSOR_B.
117  *
118  * Valid only if EC_MEMMAP_THERMAL_VERSION returns >= 2.
119  */
120 #define EC_TEMP_SENSOR_B_ENTRIES      8
121
122 /* Special values for mapped temperature sensors */
123 #define EC_TEMP_SENSOR_NOT_PRESENT    0xff
124 #define EC_TEMP_SENSOR_ERROR          0xfe
125 #define EC_TEMP_SENSOR_NOT_POWERED    0xfd
126 #define EC_TEMP_SENSOR_NOT_CALIBRATED 0xfc
127 /*
128  * The offset of temperature value stored in mapped memory.  This allows
129  * reporting a temperature range of 200K to 454K = -73C to 181C.
130  */
131 #define EC_TEMP_SENSOR_OFFSET      200
132
133 /*
134  * Number of ALS readings at EC_MEMMAP_ALS
135  */
136 #define EC_ALS_ENTRIES             2
137
138 /*
139  * The default value a temperature sensor will return when it is present but
140  * has not been read this boot.  This is a reasonable number to avoid
141  * triggering alarms on the host.
142  */
143 #define EC_TEMP_SENSOR_DEFAULT     (296 - EC_TEMP_SENSOR_OFFSET)
144
145 #define EC_FAN_SPEED_ENTRIES       4       /* Number of fans at EC_MEMMAP_FAN */
146 #define EC_FAN_SPEED_NOT_PRESENT   0xffff  /* Entry not present */
147 #define EC_FAN_SPEED_STALLED       0xfffe  /* Fan stalled */
148
149 /* Battery bit flags at EC_MEMMAP_BATT_FLAG. */
150 #define EC_BATT_FLAG_AC_PRESENT   0x01
151 #define EC_BATT_FLAG_BATT_PRESENT 0x02
152 #define EC_BATT_FLAG_DISCHARGING  0x04
153 #define EC_BATT_FLAG_CHARGING     0x08
154 #define EC_BATT_FLAG_LEVEL_CRITICAL 0x10
155
156 /* Switch flags at EC_MEMMAP_SWITCHES */
157 #define EC_SWITCH_LID_OPEN               0x01
158 #define EC_SWITCH_POWER_BUTTON_PRESSED   0x02
159 #define EC_SWITCH_WRITE_PROTECT_DISABLED 0x04
160 /* Was recovery requested via keyboard; now unused. */
161 #define EC_SWITCH_IGNORE1                0x08
162 /* Recovery requested via dedicated signal (from servo board) */
163 #define EC_SWITCH_DEDICATED_RECOVERY     0x10
164 /* Was fake developer mode switch; now unused.  Remove in next refactor. */
165 #define EC_SWITCH_IGNORE0                0x20
166
167 /* Host command interface flags */
168 /* Host command interface supports LPC args (LPC interface only) */
169 #define EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED  0x01
170 /* Host command interface supports version 3 protocol */
171 #define EC_HOST_CMD_FLAG_VERSION_3   0x02
172
173 /* Wireless switch flags */
174 #define EC_WIRELESS_SWITCH_ALL       ~0x00  /* All flags */
175 #define EC_WIRELESS_SWITCH_WLAN       0x01  /* WLAN radio */
176 #define EC_WIRELESS_SWITCH_BLUETOOTH  0x02  /* Bluetooth radio */
177 #define EC_WIRELESS_SWITCH_WWAN       0x04  /* WWAN power */
178 #define EC_WIRELESS_SWITCH_WLAN_POWER 0x08  /* WLAN power */
179
180 /*
181  * This header file is used in coreboot both in C and ACPI code.  The ACPI code
182  * is pre-processed to handle constants but the ASL compiler is unable to
183  * handle actual C code so keep it separate.
184  */
185 #ifndef __ACPI__
186
187 /*
188  * Define __packed if someone hasn't beat us to it.  Linux kernel style
189  * checking prefers __packed over __attribute__((packed)).
190  */
191 #ifndef __packed
192 #define __packed __attribute__((packed))
193 #endif
194
195 /* LPC command status byte masks */
196 /* EC has written a byte in the data register and host hasn't read it yet */
197 #define EC_LPC_STATUS_TO_HOST     0x01
198 /* Host has written a command/data byte and the EC hasn't read it yet */
199 #define EC_LPC_STATUS_FROM_HOST   0x02
200 /* EC is processing a command */
201 #define EC_LPC_STATUS_PROCESSING  0x04
202 /* Last write to EC was a command, not data */
203 #define EC_LPC_STATUS_LAST_CMD    0x08
204 /* EC is in burst mode.  Unsupported by Chrome EC, so this bit is never set */
205 #define EC_LPC_STATUS_BURST_MODE  0x10
206 /* SCI event is pending (requesting SCI query) */
207 #define EC_LPC_STATUS_SCI_PENDING 0x20
208 /* SMI event is pending (requesting SMI query) */
209 #define EC_LPC_STATUS_SMI_PENDING 0x40
210 /* (reserved) */
211 #define EC_LPC_STATUS_RESERVED    0x80
212
213 /*
214  * EC is busy.  This covers both the EC processing a command, and the host has
215  * written a new command but the EC hasn't picked it up yet.
216  */
217 #define EC_LPC_STATUS_BUSY_MASK \
218         (EC_LPC_STATUS_FROM_HOST | EC_LPC_STATUS_PROCESSING)
219
220 /* Host command response codes */
221 enum ec_status {
222         EC_RES_SUCCESS = 0,
223         EC_RES_INVALID_COMMAND = 1,
224         EC_RES_ERROR = 2,
225         EC_RES_INVALID_PARAM = 3,
226         EC_RES_ACCESS_DENIED = 4,
227         EC_RES_INVALID_RESPONSE = 5,
228         EC_RES_INVALID_VERSION = 6,
229         EC_RES_INVALID_CHECKSUM = 7,
230         EC_RES_IN_PROGRESS = 8,         /* Accepted, command in progress */
231         EC_RES_UNAVAILABLE = 9,         /* No response available */
232         EC_RES_TIMEOUT = 10,            /* We got a timeout */
233         EC_RES_OVERFLOW = 11,           /* Table / data overflow */
234         EC_RES_INVALID_HEADER = 12,     /* Header contains invalid data */
235         EC_RES_REQUEST_TRUNCATED = 13,  /* Didn't get the entire request */
236         EC_RES_RESPONSE_TOO_BIG = 14    /* Response was too big to handle */
237 };
238
239 /*
240  * Host event codes.  Note these are 1-based, not 0-based, because ACPI query
241  * EC command uses code 0 to mean "no event pending".  We explicitly specify
242  * each value in the enum listing so they won't change if we delete/insert an
243  * item or rearrange the list (it needs to be stable across platforms, not
244  * just within a single compiled instance).
245  */
246 enum host_event_code {
247         EC_HOST_EVENT_LID_CLOSED = 1,
248         EC_HOST_EVENT_LID_OPEN = 2,
249         EC_HOST_EVENT_POWER_BUTTON = 3,
250         EC_HOST_EVENT_AC_CONNECTED = 4,
251         EC_HOST_EVENT_AC_DISCONNECTED = 5,
252         EC_HOST_EVENT_BATTERY_LOW = 6,
253         EC_HOST_EVENT_BATTERY_CRITICAL = 7,
254         EC_HOST_EVENT_BATTERY = 8,
255         EC_HOST_EVENT_THERMAL_THRESHOLD = 9,
256         EC_HOST_EVENT_THERMAL_OVERLOAD = 10,
257         EC_HOST_EVENT_THERMAL = 11,
258         EC_HOST_EVENT_USB_CHARGER = 12,
259         EC_HOST_EVENT_KEY_PRESSED = 13,
260         /*
261          * EC has finished initializing the host interface.  The host can check
262          * for this event following sending a EC_CMD_REBOOT_EC command to
263          * determine when the EC is ready to accept subsequent commands.
264          */
265         EC_HOST_EVENT_INTERFACE_READY = 14,
266         /* Keyboard recovery combo has been pressed */
267         EC_HOST_EVENT_KEYBOARD_RECOVERY = 15,
268
269         /* Shutdown due to thermal overload */
270         EC_HOST_EVENT_THERMAL_SHUTDOWN = 16,
271         /* Shutdown due to battery level too low */
272         EC_HOST_EVENT_BATTERY_SHUTDOWN = 17,
273
274         /* Suggest that the AP throttle itself */
275         EC_HOST_EVENT_THROTTLE_START = 18,
276         /* Suggest that the AP resume normal speed */
277         EC_HOST_EVENT_THROTTLE_STOP = 19,
278
279         /* Hang detect logic detected a hang and host event timeout expired */
280         EC_HOST_EVENT_HANG_DETECT = 20,
281         /* Hang detect logic detected a hang and warm rebooted the AP */
282         EC_HOST_EVENT_HANG_REBOOT = 21,
283         /* PD MCU triggering host event */
284         EC_HOST_EVENT_PD_MCU = 22,
285
286         /* EC desires to change state of host-controlled USB mux */
287         EC_HOST_EVENT_USB_MUX = 28,
288
289         /* EC RTC event occurred */
290         EC_HOST_EVENT_RTC = 26,
291
292         /*
293          * The high bit of the event mask is not used as a host event code.  If
294          * it reads back as set, then the entire event mask should be
295          * considered invalid by the host.  This can happen when reading the
296          * raw event status via EC_MEMMAP_HOST_EVENTS but the LPC interface is
297          * not initialized on the EC, or improperly configured on the host.
298          */
299         EC_HOST_EVENT_INVALID = 32
300 };
301 /* Host event mask */
302 #define EC_HOST_EVENT_MASK(event_code) (1UL << ((event_code) - 1))
303
304 /**
305  * struct ec_lpc_host_args - Arguments at EC_LPC_ADDR_HOST_ARGS
306  * @flags: The host argument flags.
307  * @command_version: Command version.
308  * @data_size: The length of data.
309  * @checksum: Checksum; sum of command + flags + command_version + data_size +
310  *            all params/response data bytes.
311  */
312 struct ec_lpc_host_args {
313         uint8_t flags;
314         uint8_t command_version;
315         uint8_t data_size;
316         uint8_t checksum;
317 } __packed;
318
319 /* Flags for ec_lpc_host_args.flags */
320 /*
321  * Args are from host.  Data area at EC_LPC_ADDR_HOST_PARAM contains command
322  * params.
323  *
324  * If EC gets a command and this flag is not set, this is an old-style command.
325  * Command version is 0 and params from host are at EC_LPC_ADDR_OLD_PARAM with
326  * unknown length.  EC must respond with an old-style response (that is,
327  * withouth setting EC_HOST_ARGS_FLAG_TO_HOST).
328  */
329 #define EC_HOST_ARGS_FLAG_FROM_HOST 0x01
330 /*
331  * Args are from EC.  Data area at EC_LPC_ADDR_HOST_PARAM contains response.
332  *
333  * If EC responds to a command and this flag is not set, this is an old-style
334  * response.  Command version is 0 and response data from EC is at
335  * EC_LPC_ADDR_OLD_PARAM with unknown length.
336  */
337 #define EC_HOST_ARGS_FLAG_TO_HOST   0x02
338
339 /*****************************************************************************/
340 /*
341  * Byte codes returned by EC over SPI interface.
342  *
343  * These can be used by the AP to debug the EC interface, and to determine
344  * when the EC is not in a state where it will ever get around to responding
345  * to the AP.
346  *
347  * Example of sequence of bytes read from EC for a current good transfer:
348  *   1. -                  - AP asserts chip select (CS#)
349  *   2. EC_SPI_OLD_READY   - AP sends first byte(s) of request
350  *   3. -                  - EC starts handling CS# interrupt
351  *   4. EC_SPI_RECEIVING   - AP sends remaining byte(s) of request
352  *   5. EC_SPI_PROCESSING  - EC starts processing request; AP is clocking in
353  *                           bytes looking for EC_SPI_FRAME_START
354  *   6. -                  - EC finishes processing and sets up response
355  *   7. EC_SPI_FRAME_START - AP reads frame byte
356  *   8. (response packet)  - AP reads response packet
357  *   9. EC_SPI_PAST_END    - Any additional bytes read by AP
358  *   10 -                  - AP deasserts chip select
359  *   11 -                  - EC processes CS# interrupt and sets up DMA for
360  *                           next request
361  *
362  * If the AP is waiting for EC_SPI_FRAME_START and sees any value other than
363  * the following byte values:
364  *   EC_SPI_OLD_READY
365  *   EC_SPI_RX_READY
366  *   EC_SPI_RECEIVING
367  *   EC_SPI_PROCESSING
368  *
369  * Then the EC found an error in the request, or was not ready for the request
370  * and lost data.  The AP should give up waiting for EC_SPI_FRAME_START,
371  * because the EC is unable to tell when the AP is done sending its request.
372  */
373
374 /*
375  * Framing byte which precedes a response packet from the EC.  After sending a
376  * request, the AP will clock in bytes until it sees the framing byte, then
377  * clock in the response packet.
378  */
379 #define EC_SPI_FRAME_START    0xec
380
381 /*
382  * Padding bytes which are clocked out after the end of a response packet.
383  */
384 #define EC_SPI_PAST_END       0xed
385
386 /*
387  * EC is ready to receive, and has ignored the byte sent by the AP.  EC expects
388  * that the AP will send a valid packet header (starting with
389  * EC_COMMAND_PROTOCOL_3) in the next 32 bytes.
390  */
391 #define EC_SPI_RX_READY       0xf8
392
393 /*
394  * EC has started receiving the request from the AP, but hasn't started
395  * processing it yet.
396  */
397 #define EC_SPI_RECEIVING      0xf9
398
399 /* EC has received the entire request from the AP and is processing it. */
400 #define EC_SPI_PROCESSING     0xfa
401
402 /*
403  * EC received bad data from the AP, such as a packet header with an invalid
404  * length.  EC will ignore all data until chip select deasserts.
405  */
406 #define EC_SPI_RX_BAD_DATA    0xfb
407
408 /*
409  * EC received data from the AP before it was ready.  That is, the AP asserted
410  * chip select and started clocking data before the EC was ready to receive it.
411  * EC will ignore all data until chip select deasserts.
412  */
413 #define EC_SPI_NOT_READY      0xfc
414
415 /*
416  * EC was ready to receive a request from the AP.  EC has treated the byte sent
417  * by the AP as part of a request packet, or (for old-style ECs) is processing
418  * a fully received packet but is not ready to respond yet.
419  */
420 #define EC_SPI_OLD_READY      0xfd
421
422 /*****************************************************************************/
423
424 /*
425  * Protocol version 2 for I2C and SPI send a request this way:
426  *
427  *      0       EC_CMD_VERSION0 + (command version)
428  *      1       Command number
429  *      2       Length of params = N
430  *      3..N+2  Params, if any
431  *      N+3     8-bit checksum of bytes 0..N+2
432  *
433  * The corresponding response is:
434  *
435  *      0       Result code (EC_RES_*)
436  *      1       Length of params = M
437  *      2..M+1  Params, if any
438  *      M+2     8-bit checksum of bytes 0..M+1
439  */
440 #define EC_PROTO2_REQUEST_HEADER_BYTES 3
441 #define EC_PROTO2_REQUEST_TRAILER_BYTES 1
442 #define EC_PROTO2_REQUEST_OVERHEAD (EC_PROTO2_REQUEST_HEADER_BYTES +    \
443                                     EC_PROTO2_REQUEST_TRAILER_BYTES)
444
445 #define EC_PROTO2_RESPONSE_HEADER_BYTES 2
446 #define EC_PROTO2_RESPONSE_TRAILER_BYTES 1
447 #define EC_PROTO2_RESPONSE_OVERHEAD (EC_PROTO2_RESPONSE_HEADER_BYTES +  \
448                                      EC_PROTO2_RESPONSE_TRAILER_BYTES)
449
450 /* Parameter length was limited by the LPC interface */
451 #define EC_PROTO2_MAX_PARAM_SIZE 0xfc
452
453 /* Maximum request and response packet sizes for protocol version 2 */
454 #define EC_PROTO2_MAX_REQUEST_SIZE (EC_PROTO2_REQUEST_OVERHEAD +        \
455                                     EC_PROTO2_MAX_PARAM_SIZE)
456 #define EC_PROTO2_MAX_RESPONSE_SIZE (EC_PROTO2_RESPONSE_OVERHEAD +      \
457                                      EC_PROTO2_MAX_PARAM_SIZE)
458
459 /*****************************************************************************/
460
461 /*
462  * Value written to legacy command port / prefix byte to indicate protocol
463  * 3+ structs are being used.  Usage is bus-dependent.
464  */
465 #define EC_COMMAND_PROTOCOL_3 0xda
466
467 #define EC_HOST_REQUEST_VERSION 3
468
469 /**
470  * struct ec_host_request - Version 3 request from host.
471  * @struct_version: Should be 3. The EC will return EC_RES_INVALID_HEADER if it
472  *                  receives a header with a version it doesn't know how to
473  *                  parse.
474  * @checksum: Checksum of request and data; sum of all bytes including checksum
475  *            should total to 0.
476  * @command: Command to send (EC_CMD_...)
477  * @command_version: Command version.
478  * @reserved: Unused byte in current protocol version; set to 0.
479  * @data_len: Length of data which follows this header.
480  */
481 struct ec_host_request {
482         uint8_t struct_version;
483         uint8_t checksum;
484         uint16_t command;
485         uint8_t command_version;
486         uint8_t reserved;
487         uint16_t data_len;
488 } __packed;
489
490 #define EC_HOST_RESPONSE_VERSION 3
491
492 /**
493  * struct ec_host_response - Version 3 response from EC.
494  * @struct_version: Struct version (=3).
495  * @checksum: Checksum of response and data; sum of all bytes including
496  *            checksum should total to 0.
497  * @result: EC's response to the command (separate from communication failure)
498  * @data_len: Length of data which follows this header.
499  * @reserved: Unused bytes in current protocol version; set to 0.
500  */
501 struct ec_host_response {
502         uint8_t struct_version;
503         uint8_t checksum;
504         uint16_t result;
505         uint16_t data_len;
506         uint16_t reserved;
507 } __packed;
508
509 /*****************************************************************************/
510 /*
511  * Notes on commands:
512  *
513  * Each command is an 16-bit command value.  Commands which take params or
514  * return response data specify structs for that data.  If no struct is
515  * specified, the command does not input or output data, respectively.
516  * Parameter/response length is implicit in the structs.  Some underlying
517  * communication protocols (I2C, SPI) may add length or checksum headers, but
518  * those are implementation-dependent and not defined here.
519  */
520
521 /*****************************************************************************/
522 /* General / test commands */
523
524 /*
525  * Get protocol version, used to deal with non-backward compatible protocol
526  * changes.
527  */
528 #define EC_CMD_PROTO_VERSION 0x00
529
530 /**
531  * struct ec_response_proto_version - Response to the proto version command.
532  * @version: The protocol version.
533  */
534 struct ec_response_proto_version {
535         uint32_t version;
536 } __packed;
537
538 /*
539  * Hello.  This is a simple command to test the EC is responsive to
540  * commands.
541  */
542 #define EC_CMD_HELLO 0x01
543
544 /**
545  * struct ec_params_hello - Parameters to the hello command.
546  * @in_data: Pass anything here.
547  */
548 struct ec_params_hello {
549         uint32_t in_data;
550 } __packed;
551
552 /**
553  * struct ec_response_hello - Response to the hello command.
554  * @out_data: Output will be in_data + 0x01020304.
555  */
556 struct ec_response_hello {
557         uint32_t out_data;
558 } __packed;
559
560 /* Get version number */
561 #define EC_CMD_GET_VERSION 0x02
562
563 enum ec_current_image {
564         EC_IMAGE_UNKNOWN = 0,
565         EC_IMAGE_RO,
566         EC_IMAGE_RW
567 };
568
569 /**
570  * struct ec_response_get_version - Response to the get version command.
571  * @version_string_ro: Null-terminated RO firmware version string.
572  * @version_string_rw: Null-terminated RW firmware version string.
573  * @reserved: Unused bytes; was previously RW-B firmware version string.
574  * @current_image: One of ec_current_image.
575  */
576 struct ec_response_get_version {
577         char version_string_ro[32];
578         char version_string_rw[32];
579         char reserved[32];
580         uint32_t current_image;
581 } __packed;
582
583 /* Read test */
584 #define EC_CMD_READ_TEST 0x03
585
586 /**
587  * struct ec_params_read_test - Parameters for the read test command.
588  * @offset: Starting value for read buffer.
589  * @size: Size to read in bytes.
590  */
591 struct ec_params_read_test {
592         uint32_t offset;
593         uint32_t size;
594 } __packed;
595
596 /**
597  * struct ec_response_read_test - Response to the read test command.
598  * @data: Data returned by the read test command.
599  */
600 struct ec_response_read_test {
601         uint32_t data[32];
602 } __packed;
603
604 /*
605  * Get build information
606  *
607  * Response is null-terminated string.
608  */
609 #define EC_CMD_GET_BUILD_INFO 0x04
610
611 /* Get chip info */
612 #define EC_CMD_GET_CHIP_INFO 0x05
613
614 /**
615  * struct ec_response_get_chip_info - Response to the get chip info command.
616  * @vendor: Null-terminated string for chip vendor.
617  * @name: Null-terminated string for chip name.
618  * @revision: Null-terminated string for chip mask version.
619  */
620 struct ec_response_get_chip_info {
621         char vendor[32];
622         char name[32];
623         char revision[32];
624 } __packed;
625
626 /* Get board HW version */
627 #define EC_CMD_GET_BOARD_VERSION 0x06
628
629 /**
630  * struct ec_response_board_version - Response to the board version command.
631  * @board_version: A monotonously incrementing number.
632  */
633 struct ec_response_board_version {
634         uint16_t board_version;
635 } __packed;
636
637 /*
638  * Read memory-mapped data.
639  *
640  * This is an alternate interface to memory-mapped data for bus protocols
641  * which don't support direct-mapped memory - I2C, SPI, etc.
642  *
643  * Response is params.size bytes of data.
644  */
645 #define EC_CMD_READ_MEMMAP 0x07
646
647 /**
648  * struct ec_params_read_memmap - Parameters for the read memory map command.
649  * @offset: Offset in memmap (EC_MEMMAP_*).
650  * @size: Size to read in bytes.
651  */
652 struct ec_params_read_memmap {
653         uint8_t offset;
654         uint8_t size;
655 } __packed;
656
657 /* Read versions supported for a command */
658 #define EC_CMD_GET_CMD_VERSIONS 0x08
659
660 /**
661  * struct ec_params_get_cmd_versions - Parameters for the get command versions.
662  * @cmd: Command to check.
663  */
664 struct ec_params_get_cmd_versions {
665         uint8_t cmd;
666 } __packed;
667
668 /**
669  * struct ec_params_get_cmd_versions_v1 - Parameters for the get command
670  *         versions (v1)
671  * @cmd: Command to check.
672  */
673 struct ec_params_get_cmd_versions_v1 {
674         uint16_t cmd;
675 } __packed;
676
677 /**
678  * struct ec_response_get_cmd_version - Response to the get command versions.
679  * @version_mask: Mask of supported versions; use EC_VER_MASK() to compare with
680  *                a desired version.
681  */
682 struct ec_response_get_cmd_versions {
683         uint32_t version_mask;
684 } __packed;
685
686 /*
687  * Check EC communcations status (busy). This is needed on i2c/spi but not
688  * on lpc since it has its own out-of-band busy indicator.
689  *
690  * lpc must read the status from the command register. Attempting this on
691  * lpc will overwrite the args/parameter space and corrupt its data.
692  */
693 #define EC_CMD_GET_COMMS_STATUS         0x09
694
695 /* Avoid using ec_status which is for return values */
696 enum ec_comms_status {
697         EC_COMMS_STATUS_PROCESSING      = 1 << 0,       /* Processing cmd */
698 };
699
700 /**
701  * struct ec_response_get_comms_status - Response to the get comms status
702  *         command.
703  * @flags: Mask of enum ec_comms_status.
704  */
705 struct ec_response_get_comms_status {
706         uint32_t flags;         /* Mask of enum ec_comms_status */
707 } __packed;
708
709 /* Fake a variety of responses, purely for testing purposes. */
710 #define EC_CMD_TEST_PROTOCOL            0x0a
711
712 /* Tell the EC what to send back to us. */
713 struct ec_params_test_protocol {
714         uint32_t ec_result;
715         uint32_t ret_len;
716         uint8_t buf[32];
717 } __packed;
718
719 /* Here it comes... */
720 struct ec_response_test_protocol {
721         uint8_t buf[32];
722 } __packed;
723
724 /* Get prococol information */
725 #define EC_CMD_GET_PROTOCOL_INFO        0x0b
726
727 /* Flags for ec_response_get_protocol_info.flags */
728 /* EC_RES_IN_PROGRESS may be returned if a command is slow */
729 #define EC_PROTOCOL_INFO_IN_PROGRESS_SUPPORTED (1 << 0)
730
731 /**
732  * struct ec_response_get_protocol_info - Response to the get protocol info.
733  * @protocol_versions: Bitmask of protocol versions supported (1 << n means
734  *                     version n).
735  * @max_request_packet_size: Maximum request packet size in bytes.
736  * @max_response_packet_size: Maximum response packet size in bytes.
737  * @flags: see EC_PROTOCOL_INFO_*
738  */
739 struct ec_response_get_protocol_info {
740         /* Fields which exist if at least protocol version 3 supported */
741         uint32_t protocol_versions;
742         uint16_t max_request_packet_size;
743         uint16_t max_response_packet_size;
744         uint32_t flags;
745 } __packed;
746
747
748 /*****************************************************************************/
749 /* Get/Set miscellaneous values */
750
751 /* The upper byte of .flags tells what to do (nothing means "get") */
752 #define EC_GSV_SET        0x80000000
753
754 /*
755  * The lower three bytes of .flags identifies the parameter, if that has
756  * meaning for an individual command.
757  */
758 #define EC_GSV_PARAM_MASK 0x00ffffff
759
760 struct ec_params_get_set_value {
761         uint32_t flags;
762         uint32_t value;
763 } __packed;
764
765 struct ec_response_get_set_value {
766         uint32_t flags;
767         uint32_t value;
768 } __packed;
769
770 /* More than one command can use these structs to get/set paramters. */
771 #define EC_CMD_GSV_PAUSE_IN_S5  0x0c
772
773 /*****************************************************************************/
774 /* List the features supported by the firmware */
775 #define EC_CMD_GET_FEATURES  0x0d
776
777 /* Supported features */
778 enum ec_feature_code {
779         /*
780          * This image contains a limited set of features. Another image
781          * in RW partition may support more features.
782          */
783         EC_FEATURE_LIMITED = 0,
784         /*
785          * Commands for probing/reading/writing/erasing the flash in the
786          * EC are present.
787          */
788         EC_FEATURE_FLASH = 1,
789         /*
790          * Can control the fan speed directly.
791          */
792         EC_FEATURE_PWM_FAN = 2,
793         /*
794          * Can control the intensity of the keyboard backlight.
795          */
796         EC_FEATURE_PWM_KEYB = 3,
797         /*
798          * Support Google lightbar, introduced on Pixel.
799          */
800         EC_FEATURE_LIGHTBAR = 4,
801         /* Control of LEDs  */
802         EC_FEATURE_LED = 5,
803         /* Exposes an interface to control gyro and sensors.
804          * The host goes through the EC to access these sensors.
805          * In addition, the EC may provide composite sensors, like lid angle.
806          */
807         EC_FEATURE_MOTION_SENSE = 6,
808         /* The keyboard is controlled by the EC */
809         EC_FEATURE_KEYB = 7,
810         /* The AP can use part of the EC flash as persistent storage. */
811         EC_FEATURE_PSTORE = 8,
812         /* The EC monitors BIOS port 80h, and can return POST codes. */
813         EC_FEATURE_PORT80 = 9,
814         /*
815          * Thermal management: include TMP specific commands.
816          * Higher level than direct fan control.
817          */
818         EC_FEATURE_THERMAL = 10,
819         /* Can switch the screen backlight on/off */
820         EC_FEATURE_BKLIGHT_SWITCH = 11,
821         /* Can switch the wifi module on/off */
822         EC_FEATURE_WIFI_SWITCH = 12,
823         /* Monitor host events, through for example SMI or SCI */
824         EC_FEATURE_HOST_EVENTS = 13,
825         /* The EC exposes GPIO commands to control/monitor connected devices. */
826         EC_FEATURE_GPIO = 14,
827         /* The EC can send i2c messages to downstream devices. */
828         EC_FEATURE_I2C = 15,
829         /* Command to control charger are included */
830         EC_FEATURE_CHARGER = 16,
831         /* Simple battery support. */
832         EC_FEATURE_BATTERY = 17,
833         /*
834          * Support Smart battery protocol
835          * (Common Smart Battery System Interface Specification)
836          */
837         EC_FEATURE_SMART_BATTERY = 18,
838         /* EC can detect when the host hangs. */
839         EC_FEATURE_HANG_DETECT = 19,
840         /* Report power information, for pit only */
841         EC_FEATURE_PMU = 20,
842         /* Another Cros EC device is present downstream of this one */
843         EC_FEATURE_SUB_MCU = 21,
844         /* Support USB Power delivery (PD) commands */
845         EC_FEATURE_USB_PD = 22,
846         /* Control USB multiplexer, for audio through USB port for instance. */
847         EC_FEATURE_USB_MUX = 23,
848         /* Motion Sensor code has an internal software FIFO */
849         EC_FEATURE_MOTION_SENSE_FIFO = 24,
850         /* Support temporary secure vstore */
851         EC_FEATURE_VSTORE = 25,
852         /* EC decides on USB-C SS mux state, muxes configured by host */
853         EC_FEATURE_USBC_SS_MUX_VIRTUAL = 26,
854         /* EC has RTC feature that can be controlled by host commands */
855         EC_FEATURE_RTC = 27,
856         /* The MCU exposes a Fingerprint sensor */
857         EC_FEATURE_FINGERPRINT = 28,
858         /* The MCU exposes a Touchpad */
859         EC_FEATURE_TOUCHPAD = 29,
860         /* The MCU has RWSIG task enabled */
861         EC_FEATURE_RWSIG = 30,
862         /* EC has device events support */
863         EC_FEATURE_DEVICE_EVENT = 31,
864         /* EC supports the unified wake masks for LPC/eSPI systems */
865         EC_FEATURE_UNIFIED_WAKE_MASKS = 32,
866         /* EC supports 64-bit host events */
867         EC_FEATURE_HOST_EVENT64 = 33,
868         /* EC runs code in RAM (not in place, a.k.a. XIP) */
869         EC_FEATURE_EXEC_IN_RAM = 34,
870         /* EC supports CEC commands */
871         EC_FEATURE_CEC = 35,
872         /* EC supports tight sensor timestamping. */
873         EC_FEATURE_MOTION_SENSE_TIGHT_TIMESTAMPS = 36,
874         /*
875          * EC supports tablet mode detection aligned to Chrome and allows
876          * setting of threshold by host command using
877          * MOTIONSENSE_CMD_TABLET_MODE_LID_ANGLE.
878          */
879         EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS = 37,
880         /* EC supports audio codec. */
881         EC_FEATURE_AUDIO_CODEC = 38,
882         /* EC Supports SCP. */
883         EC_FEATURE_SCP = 39,
884         /* The MCU is an Integrated Sensor Hub */
885         EC_FEATURE_ISH = 40,
886 };
887
888 #define EC_FEATURE_MASK_0(event_code) (1UL << (event_code % 32))
889 #define EC_FEATURE_MASK_1(event_code) (1UL << (event_code - 32))
890
891 struct ec_response_get_features {
892         uint32_t flags[2];
893 } __packed;
894
895 /*****************************************************************************/
896 /* Flash commands */
897
898 /* Get flash info */
899 #define EC_CMD_FLASH_INFO 0x10
900
901 /**
902  * struct ec_response_flash_info - Response to the flash info command.
903  * @flash_size: Usable flash size in bytes.
904  * @write_block_size: Write block size. Write offset and size must be a
905  *                    multiple of this.
906  * @erase_block_size: Erase block size. Erase offset and size must be a
907  *                    multiple of this.
908  * @protect_block_size: Protection block size. Protection offset and size
909  *                      must be a multiple of this.
910  *
911  * Version 0 returns these fields.
912  */
913 struct ec_response_flash_info {
914         uint32_t flash_size;
915         uint32_t write_block_size;
916         uint32_t erase_block_size;
917         uint32_t protect_block_size;
918 } __packed;
919
920 /* Flags for version 1+ flash info command */
921 /* EC flash erases bits to 0 instead of 1 */
922 #define EC_FLASH_INFO_ERASE_TO_0 (1 << 0)
923
924 /**
925  * struct ec_response_flash_info_1 - Response to the flash info v1 command.
926  * @flash_size: Usable flash size in bytes.
927  * @write_block_size: Write block size. Write offset and size must be a
928  *                    multiple of this.
929  * @erase_block_size: Erase block size. Erase offset and size must be a
930  *                    multiple of this.
931  * @protect_block_size: Protection block size. Protection offset and size
932  *                      must be a multiple of this.
933  * @write_ideal_size: Ideal write size in bytes.  Writes will be fastest if
934  *                    size is exactly this and offset is a multiple of this.
935  *                    For example, an EC may have a write buffer which can do
936  *                    half-page operations if data is aligned, and a slower
937  *                    word-at-a-time write mode.
938  * @flags: Flags; see EC_FLASH_INFO_*
939  *
940  * Version 1 returns the same initial fields as version 0, with additional
941  * fields following.
942  *
943  * gcc anonymous structs don't seem to get along with the __packed directive;
944  * if they did we'd define the version 0 struct as a sub-struct of this one.
945  */
946 struct ec_response_flash_info_1 {
947         /* Version 0 fields; see above for description */
948         uint32_t flash_size;
949         uint32_t write_block_size;
950         uint32_t erase_block_size;
951         uint32_t protect_block_size;
952
953         /* Version 1 adds these fields: */
954         uint32_t write_ideal_size;
955         uint32_t flags;
956 } __packed;
957
958 /*
959  * Read flash
960  *
961  * Response is params.size bytes of data.
962  */
963 #define EC_CMD_FLASH_READ 0x11
964
965 /**
966  * struct ec_params_flash_read - Parameters for the flash read command.
967  * @offset: Byte offset to read.
968  * @size: Size to read in bytes.
969  */
970 struct ec_params_flash_read {
971         uint32_t offset;
972         uint32_t size;
973 } __packed;
974
975 /* Write flash */
976 #define EC_CMD_FLASH_WRITE 0x12
977 #define EC_VER_FLASH_WRITE 1
978
979 /* Version 0 of the flash command supported only 64 bytes of data */
980 #define EC_FLASH_WRITE_VER0_SIZE 64
981
982 /**
983  * struct ec_params_flash_write - Parameters for the flash write command.
984  * @offset: Byte offset to write.
985  * @size: Size to write in bytes.
986  */
987 struct ec_params_flash_write {
988         uint32_t offset;
989         uint32_t size;
990         /* Followed by data to write */
991 } __packed;
992
993 /* Erase flash */
994 #define EC_CMD_FLASH_ERASE 0x13
995
996 /**
997  * struct ec_params_flash_erase - Parameters for the flash erase command.
998  * @offset: Byte offset to erase.
999  * @size: Size to erase in bytes.
1000  */
1001 struct ec_params_flash_erase {
1002         uint32_t offset;
1003         uint32_t size;
1004 } __packed;
1005
1006 /*
1007  * Get/set flash protection.
1008  *
1009  * If mask!=0, sets/clear the requested bits of flags.  Depending on the
1010  * firmware write protect GPIO, not all flags will take effect immediately;
1011  * some flags require a subsequent hard reset to take effect.  Check the
1012  * returned flags bits to see what actually happened.
1013  *
1014  * If mask=0, simply returns the current flags state.
1015  */
1016 #define EC_CMD_FLASH_PROTECT 0x15
1017 #define EC_VER_FLASH_PROTECT 1  /* Command version 1 */
1018
1019 /* Flags for flash protection */
1020 /* RO flash code protected when the EC boots */
1021 #define EC_FLASH_PROTECT_RO_AT_BOOT         (1 << 0)
1022 /*
1023  * RO flash code protected now.  If this bit is set, at-boot status cannot
1024  * be changed.
1025  */
1026 #define EC_FLASH_PROTECT_RO_NOW             (1 << 1)
1027 /* Entire flash code protected now, until reboot. */
1028 #define EC_FLASH_PROTECT_ALL_NOW            (1 << 2)
1029 /* Flash write protect GPIO is asserted now */
1030 #define EC_FLASH_PROTECT_GPIO_ASSERTED      (1 << 3)
1031 /* Error - at least one bank of flash is stuck locked, and cannot be unlocked */
1032 #define EC_FLASH_PROTECT_ERROR_STUCK        (1 << 4)
1033 /*
1034  * Error - flash protection is in inconsistent state.  At least one bank of
1035  * flash which should be protected is not protected.  Usually fixed by
1036  * re-requesting the desired flags, or by a hard reset if that fails.
1037  */
1038 #define EC_FLASH_PROTECT_ERROR_INCONSISTENT (1 << 5)
1039 /* Entile flash code protected when the EC boots */
1040 #define EC_FLASH_PROTECT_ALL_AT_BOOT        (1 << 6)
1041
1042 /**
1043  * struct ec_params_flash_protect - Parameters for the flash protect command.
1044  * @mask: Bits in flags to apply.
1045  * @flags: New flags to apply.
1046  */
1047 struct ec_params_flash_protect {
1048         uint32_t mask;
1049         uint32_t flags;
1050 } __packed;
1051
1052 /**
1053  * struct ec_response_flash_protect - Response to the flash protect command.
1054  * @flags: Current value of flash protect flags.
1055  * @valid_flags: Flags which are valid on this platform. This allows the
1056  *               caller to distinguish between flags which aren't set vs. flags
1057  *               which can't be set on this platform.
1058  * @writable_flags: Flags which can be changed given the current protection
1059  *                  state.
1060  */
1061 struct ec_response_flash_protect {
1062         uint32_t flags;
1063         uint32_t valid_flags;
1064         uint32_t writable_flags;
1065 } __packed;
1066
1067 /*
1068  * Note: commands 0x14 - 0x19 version 0 were old commands to get/set flash
1069  * write protect.  These commands may be reused with version > 0.
1070  */
1071
1072 /* Get the region offset/size */
1073 #define EC_CMD_FLASH_REGION_INFO 0x16
1074 #define EC_VER_FLASH_REGION_INFO 1
1075
1076 enum ec_flash_region {
1077         /* Region which holds read-only EC image */
1078         EC_FLASH_REGION_RO = 0,
1079         /* Region which holds rewritable EC image */
1080         EC_FLASH_REGION_RW,
1081         /*
1082          * Region which should be write-protected in the factory (a superset of
1083          * EC_FLASH_REGION_RO)
1084          */
1085         EC_FLASH_REGION_WP_RO,
1086         /* Number of regions */
1087         EC_FLASH_REGION_COUNT,
1088 };
1089
1090 /**
1091  * struct ec_params_flash_region_info - Parameters for the flash region info
1092  *         command.
1093  * @region: Flash region; see EC_FLASH_REGION_*
1094  */
1095 struct ec_params_flash_region_info {
1096         uint32_t region;
1097 } __packed;
1098
1099 struct ec_response_flash_region_info {
1100         uint32_t offset;
1101         uint32_t size;
1102 } __packed;
1103
1104 /* Read/write VbNvContext */
1105 #define EC_CMD_VBNV_CONTEXT 0x17
1106 #define EC_VER_VBNV_CONTEXT 1
1107 #define EC_VBNV_BLOCK_SIZE 16
1108
1109 enum ec_vbnvcontext_op {
1110         EC_VBNV_CONTEXT_OP_READ,
1111         EC_VBNV_CONTEXT_OP_WRITE,
1112 };
1113
1114 struct ec_params_vbnvcontext {
1115         uint32_t op;
1116         uint8_t block[EC_VBNV_BLOCK_SIZE];
1117 } __packed;
1118
1119 struct ec_response_vbnvcontext {
1120         uint8_t block[EC_VBNV_BLOCK_SIZE];
1121 } __packed;
1122
1123 /*****************************************************************************/
1124 /* PWM commands */
1125
1126 /* Get fan target RPM */
1127 #define EC_CMD_PWM_GET_FAN_TARGET_RPM 0x20
1128
1129 struct ec_response_pwm_get_fan_rpm {
1130         uint32_t rpm;
1131 } __packed;
1132
1133 /* Set target fan RPM */
1134 #define EC_CMD_PWM_SET_FAN_TARGET_RPM 0x21
1135
1136 struct ec_params_pwm_set_fan_target_rpm {
1137         uint32_t rpm;
1138 } __packed;
1139
1140 /* Get keyboard backlight */
1141 #define EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT 0x22
1142
1143 struct ec_response_pwm_get_keyboard_backlight {
1144         uint8_t percent;
1145         uint8_t enabled;
1146 } __packed;
1147
1148 /* Set keyboard backlight */
1149 #define EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT 0x23
1150
1151 struct ec_params_pwm_set_keyboard_backlight {
1152         uint8_t percent;
1153 } __packed;
1154
1155 /* Set target fan PWM duty cycle */
1156 #define EC_CMD_PWM_SET_FAN_DUTY 0x24
1157
1158 struct ec_params_pwm_set_fan_duty {
1159         uint32_t percent;
1160 } __packed;
1161
1162 #define EC_CMD_PWM_SET_DUTY 0x25
1163 /* 16 bit duty cycle, 0xffff = 100% */
1164 #define EC_PWM_MAX_DUTY 0xffff
1165
1166 enum ec_pwm_type {
1167         /* All types, indexed by board-specific enum pwm_channel */
1168         EC_PWM_TYPE_GENERIC = 0,
1169         /* Keyboard backlight */
1170         EC_PWM_TYPE_KB_LIGHT,
1171         /* Display backlight */
1172         EC_PWM_TYPE_DISPLAY_LIGHT,
1173         EC_PWM_TYPE_COUNT,
1174 };
1175
1176 struct ec_params_pwm_set_duty {
1177         uint16_t duty;     /* Duty cycle, EC_PWM_MAX_DUTY = 100% */
1178         uint8_t pwm_type;  /* ec_pwm_type */
1179         uint8_t index;     /* Type-specific index, or 0 if unique */
1180 } __packed;
1181
1182 #define EC_CMD_PWM_GET_DUTY 0x26
1183
1184 struct ec_params_pwm_get_duty {
1185         uint8_t pwm_type;  /* ec_pwm_type */
1186         uint8_t index;     /* Type-specific index, or 0 if unique */
1187 } __packed;
1188
1189 struct ec_response_pwm_get_duty {
1190         uint16_t duty;     /* Duty cycle, EC_PWM_MAX_DUTY = 100% */
1191 } __packed;
1192
1193 /*****************************************************************************/
1194 /*
1195  * Lightbar commands. This looks worse than it is. Since we only use one HOST
1196  * command to say "talk to the lightbar", we put the "and tell it to do X" part
1197  * into a subcommand. We'll make separate structs for subcommands with
1198  * different input args, so that we know how much to expect.
1199  */
1200 #define EC_CMD_LIGHTBAR_CMD 0x28
1201
1202 struct rgb_s {
1203         uint8_t r, g, b;
1204 };
1205
1206 #define LB_BATTERY_LEVELS 4
1207
1208 /*
1209  * List of tweakable parameters. NOTE: It's __packed so it can be sent in a
1210  * host command, but the alignment is the same regardless. Keep it that way.
1211  */
1212 struct lightbar_params_v0 {
1213         /* Timing */
1214         int32_t google_ramp_up;
1215         int32_t google_ramp_down;
1216         int32_t s3s0_ramp_up;
1217         int32_t s0_tick_delay[2];               /* AC=0/1 */
1218         int32_t s0a_tick_delay[2];              /* AC=0/1 */
1219         int32_t s0s3_ramp_down;
1220         int32_t s3_sleep_for;
1221         int32_t s3_ramp_up;
1222         int32_t s3_ramp_down;
1223
1224         /* Oscillation */
1225         uint8_t new_s0;
1226         uint8_t osc_min[2];                     /* AC=0/1 */
1227         uint8_t osc_max[2];                     /* AC=0/1 */
1228         uint8_t w_ofs[2];                       /* AC=0/1 */
1229
1230         /* Brightness limits based on the backlight and AC. */
1231         uint8_t bright_bl_off_fixed[2];         /* AC=0/1 */
1232         uint8_t bright_bl_on_min[2];            /* AC=0/1 */
1233         uint8_t bright_bl_on_max[2];            /* AC=0/1 */
1234
1235         /* Battery level thresholds */
1236         uint8_t battery_threshold[LB_BATTERY_LEVELS - 1];
1237
1238         /* Map [AC][battery_level] to color index */
1239         uint8_t s0_idx[2][LB_BATTERY_LEVELS];   /* AP is running */
1240         uint8_t s3_idx[2][LB_BATTERY_LEVELS];   /* AP is sleeping */
1241
1242         /* Color palette */
1243         struct rgb_s color[8];                  /* 0-3 are Google colors */
1244 } __packed;
1245
1246 struct lightbar_params_v1 {
1247         /* Timing */
1248         int32_t google_ramp_up;
1249         int32_t google_ramp_down;
1250         int32_t s3s0_ramp_up;
1251         int32_t s0_tick_delay[2];               /* AC=0/1 */
1252         int32_t s0a_tick_delay[2];              /* AC=0/1 */
1253         int32_t s0s3_ramp_down;
1254         int32_t s3_sleep_for;
1255         int32_t s3_ramp_up;
1256         int32_t s3_ramp_down;
1257         int32_t tap_tick_delay;
1258         int32_t tap_display_time;
1259
1260         /* Tap-for-battery params */
1261         uint8_t tap_pct_red;
1262         uint8_t tap_pct_green;
1263         uint8_t tap_seg_min_on;
1264         uint8_t tap_seg_max_on;
1265         uint8_t tap_seg_osc;
1266         uint8_t tap_idx[3];
1267
1268         /* Oscillation */
1269         uint8_t osc_min[2];                     /* AC=0/1 */
1270         uint8_t osc_max[2];                     /* AC=0/1 */
1271         uint8_t w_ofs[2];                       /* AC=0/1 */
1272
1273         /* Brightness limits based on the backlight and AC. */
1274         uint8_t bright_bl_off_fixed[2];         /* AC=0/1 */
1275         uint8_t bright_bl_on_min[2];            /* AC=0/1 */
1276         uint8_t bright_bl_on_max[2];            /* AC=0/1 */
1277
1278         /* Battery level thresholds */
1279         uint8_t battery_threshold[LB_BATTERY_LEVELS - 1];
1280
1281         /* Map [AC][battery_level] to color index */
1282         uint8_t s0_idx[2][LB_BATTERY_LEVELS];   /* AP is running */
1283         uint8_t s3_idx[2][LB_BATTERY_LEVELS];   /* AP is sleeping */
1284
1285         /* Color palette */
1286         struct rgb_s color[8];                  /* 0-3 are Google colors */
1287 } __packed;
1288
1289 /* Lightbar program */
1290 #define EC_LB_PROG_LEN 192
1291 struct lightbar_program {
1292         uint8_t size;
1293         uint8_t data[EC_LB_PROG_LEN];
1294 };
1295
1296 struct ec_params_lightbar {
1297         uint8_t cmd;                  /* Command (see enum lightbar_command) */
1298         union {
1299                 struct {
1300                         /* no args */
1301                 } dump, off, on, init, get_seq, get_params_v0, get_params_v1,
1302                         version, get_brightness, get_demo, suspend, resume;
1303
1304                 struct {
1305                         uint8_t num;
1306                 } set_brightness, seq, demo;
1307
1308                 struct {
1309                         uint8_t ctrl, reg, value;
1310                 } reg;
1311
1312                 struct {
1313                         uint8_t led, red, green, blue;
1314                 } set_rgb;
1315
1316                 struct {
1317                         uint8_t led;
1318                 } get_rgb;
1319
1320                 struct {
1321                         uint8_t enable;
1322                 } manual_suspend_ctrl;
1323
1324                 struct lightbar_params_v0 set_params_v0;
1325                 struct lightbar_params_v1 set_params_v1;
1326                 struct lightbar_program set_program;
1327         };
1328 } __packed;
1329
1330 struct ec_response_lightbar {
1331         union {
1332                 struct {
1333                         struct {
1334                                 uint8_t reg;
1335                                 uint8_t ic0;
1336                                 uint8_t ic1;
1337                         } vals[23];
1338                 } dump;
1339
1340                 struct  {
1341                         uint8_t num;
1342                 } get_seq, get_brightness, get_demo;
1343
1344                 struct lightbar_params_v0 get_params_v0;
1345                 struct lightbar_params_v1 get_params_v1;
1346
1347                 struct {
1348                         uint32_t num;
1349                         uint32_t flags;
1350                 } version;
1351
1352                 struct {
1353                         uint8_t red, green, blue;
1354                 } get_rgb;
1355
1356                 struct {
1357                         /* no return params */
1358                 } off, on, init, set_brightness, seq, reg, set_rgb,
1359                         demo, set_params_v0, set_params_v1,
1360                         set_program, manual_suspend_ctrl, suspend, resume;
1361         };
1362 } __packed;
1363
1364 /* Lightbar commands */
1365 enum lightbar_command {
1366         LIGHTBAR_CMD_DUMP = 0,
1367         LIGHTBAR_CMD_OFF = 1,
1368         LIGHTBAR_CMD_ON = 2,
1369         LIGHTBAR_CMD_INIT = 3,
1370         LIGHTBAR_CMD_SET_BRIGHTNESS = 4,
1371         LIGHTBAR_CMD_SEQ = 5,
1372         LIGHTBAR_CMD_REG = 6,
1373         LIGHTBAR_CMD_SET_RGB = 7,
1374         LIGHTBAR_CMD_GET_SEQ = 8,
1375         LIGHTBAR_CMD_DEMO = 9,
1376         LIGHTBAR_CMD_GET_PARAMS_V0 = 10,
1377         LIGHTBAR_CMD_SET_PARAMS_V0 = 11,
1378         LIGHTBAR_CMD_VERSION = 12,
1379         LIGHTBAR_CMD_GET_BRIGHTNESS = 13,
1380         LIGHTBAR_CMD_GET_RGB = 14,
1381         LIGHTBAR_CMD_GET_DEMO = 15,
1382         LIGHTBAR_CMD_GET_PARAMS_V1 = 16,
1383         LIGHTBAR_CMD_SET_PARAMS_V1 = 17,
1384         LIGHTBAR_CMD_SET_PROGRAM = 18,
1385         LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL = 19,
1386         LIGHTBAR_CMD_SUSPEND = 20,
1387         LIGHTBAR_CMD_RESUME = 21,
1388         LIGHTBAR_NUM_CMDS
1389 };
1390
1391 /*****************************************************************************/
1392 /* LED control commands */
1393
1394 #define EC_CMD_LED_CONTROL 0x29
1395
1396 enum ec_led_id {
1397         /* LED to indicate battery state of charge */
1398         EC_LED_ID_BATTERY_LED = 0,
1399         /*
1400          * LED to indicate system power state (on or in suspend).
1401          * May be on power button or on C-panel.
1402          */
1403         EC_LED_ID_POWER_LED,
1404         /* LED on power adapter or its plug */
1405         EC_LED_ID_ADAPTER_LED,
1406
1407         EC_LED_ID_COUNT
1408 };
1409
1410 /* LED control flags */
1411 #define EC_LED_FLAGS_QUERY (1 << 0) /* Query LED capability only */
1412 #define EC_LED_FLAGS_AUTO  (1 << 1) /* Switch LED back to automatic control */
1413
1414 enum ec_led_colors {
1415         EC_LED_COLOR_RED = 0,
1416         EC_LED_COLOR_GREEN,
1417         EC_LED_COLOR_BLUE,
1418         EC_LED_COLOR_YELLOW,
1419         EC_LED_COLOR_WHITE,
1420
1421         EC_LED_COLOR_COUNT
1422 };
1423
1424 struct ec_params_led_control {
1425         uint8_t led_id;     /* Which LED to control */
1426         uint8_t flags;      /* Control flags */
1427
1428         uint8_t brightness[EC_LED_COLOR_COUNT];
1429 } __packed;
1430
1431 struct ec_response_led_control {
1432         /*
1433          * Available brightness value range.
1434          *
1435          * Range 0 means color channel not present.
1436          * Range 1 means on/off control.
1437          * Other values means the LED is control by PWM.
1438          */
1439         uint8_t brightness_range[EC_LED_COLOR_COUNT];
1440 } __packed;
1441
1442 /*****************************************************************************/
1443 /* Verified boot commands */
1444
1445 /*
1446  * Note: command code 0x29 version 0 was VBOOT_CMD in Link EVT; it may be
1447  * reused for other purposes with version > 0.
1448  */
1449
1450 /* Verified boot hash command */
1451 #define EC_CMD_VBOOT_HASH 0x2A
1452
1453 struct ec_params_vboot_hash {
1454         uint8_t cmd;             /* enum ec_vboot_hash_cmd */
1455         uint8_t hash_type;       /* enum ec_vboot_hash_type */
1456         uint8_t nonce_size;      /* Nonce size; may be 0 */
1457         uint8_t reserved0;       /* Reserved; set 0 */
1458         uint32_t offset;         /* Offset in flash to hash */
1459         uint32_t size;           /* Number of bytes to hash */
1460         uint8_t nonce_data[64];  /* Nonce data; ignored if nonce_size=0 */
1461 } __packed;
1462
1463 struct ec_response_vboot_hash {
1464         uint8_t status;          /* enum ec_vboot_hash_status */
1465         uint8_t hash_type;       /* enum ec_vboot_hash_type */
1466         uint8_t digest_size;     /* Size of hash digest in bytes */
1467         uint8_t reserved0;       /* Ignore; will be 0 */
1468         uint32_t offset;         /* Offset in flash which was hashed */
1469         uint32_t size;           /* Number of bytes hashed */
1470         uint8_t hash_digest[64]; /* Hash digest data */
1471 } __packed;
1472
1473 enum ec_vboot_hash_cmd {
1474         EC_VBOOT_HASH_GET = 0,       /* Get current hash status */
1475         EC_VBOOT_HASH_ABORT = 1,     /* Abort calculating current hash */
1476         EC_VBOOT_HASH_START = 2,     /* Start computing a new hash */
1477         EC_VBOOT_HASH_RECALC = 3,    /* Synchronously compute a new hash */
1478 };
1479
1480 enum ec_vboot_hash_type {
1481         EC_VBOOT_HASH_TYPE_SHA256 = 0, /* SHA-256 */
1482 };
1483
1484 enum ec_vboot_hash_status {
1485         EC_VBOOT_HASH_STATUS_NONE = 0, /* No hash (not started, or aborted) */
1486         EC_VBOOT_HASH_STATUS_DONE = 1, /* Finished computing a hash */
1487         EC_VBOOT_HASH_STATUS_BUSY = 2, /* Busy computing a hash */
1488 };
1489
1490 /*
1491  * Special values for offset for EC_VBOOT_HASH_START and EC_VBOOT_HASH_RECALC.
1492  * If one of these is specified, the EC will automatically update offset and
1493  * size to the correct values for the specified image (RO or RW).
1494  */
1495 #define EC_VBOOT_HASH_OFFSET_RO 0xfffffffe
1496 #define EC_VBOOT_HASH_OFFSET_RW 0xfffffffd
1497
1498 /*****************************************************************************/
1499 /*
1500  * Motion sense commands. We'll make separate structs for sub-commands with
1501  * different input args, so that we know how much to expect.
1502  */
1503 #define EC_CMD_MOTION_SENSE_CMD 0x2B
1504
1505 /* Motion sense commands */
1506 enum motionsense_command {
1507         /*
1508          * Dump command returns all motion sensor data including motion sense
1509          * module flags and individual sensor flags.
1510          */
1511         MOTIONSENSE_CMD_DUMP = 0,
1512
1513         /*
1514          * Info command returns data describing the details of a given sensor,
1515          * including enum motionsensor_type, enum motionsensor_location, and
1516          * enum motionsensor_chip.
1517          */
1518         MOTIONSENSE_CMD_INFO = 1,
1519
1520         /*
1521          * EC Rate command is a setter/getter command for the EC sampling rate
1522          * of all motion sensors in milliseconds.
1523          */
1524         MOTIONSENSE_CMD_EC_RATE = 2,
1525
1526         /*
1527          * Sensor ODR command is a setter/getter command for the output data
1528          * rate of a specific motion sensor in millihertz.
1529          */
1530         MOTIONSENSE_CMD_SENSOR_ODR = 3,
1531
1532         /*
1533          * Sensor range command is a setter/getter command for the range of
1534          * a specified motion sensor in +/-G's or +/- deg/s.
1535          */
1536         MOTIONSENSE_CMD_SENSOR_RANGE = 4,
1537
1538         /*
1539          * Setter/getter command for the keyboard wake angle. When the lid
1540          * angle is greater than this value, keyboard wake is disabled in S3,
1541          * and when the lid angle goes less than this value, keyboard wake is
1542          * enabled. Note, the lid angle measurement is an approximate,
1543          * un-calibrated value, hence the wake angle isn't exact.
1544          */
1545         MOTIONSENSE_CMD_KB_WAKE_ANGLE = 5,
1546
1547         /*
1548          * Returns a single sensor data.
1549          */
1550         MOTIONSENSE_CMD_DATA = 6,
1551
1552         /*
1553          * Perform low level calibration.. On sensors that support it, ask to
1554          * do offset calibration.
1555          */
1556         MOTIONSENSE_CMD_PERFORM_CALIB = 10,
1557
1558         /*
1559          * Sensor Offset command is a setter/getter command for the offset used
1560          * for calibration. The offsets can be calculated by the host, or via
1561          * PERFORM_CALIB command.
1562          */
1563         MOTIONSENSE_CMD_SENSOR_OFFSET = 11,
1564
1565         /* Number of motionsense sub-commands. */
1566         MOTIONSENSE_NUM_CMDS
1567 };
1568
1569 enum motionsensor_id {
1570         EC_MOTION_SENSOR_ACCEL_BASE = 0,
1571         EC_MOTION_SENSOR_ACCEL_LID = 1,
1572         EC_MOTION_SENSOR_GYRO = 2,
1573
1574         /*
1575          * Note, if more sensors are added and this count changes, the padding
1576          * in ec_response_motion_sense dump command must be modified.
1577          */
1578         EC_MOTION_SENSOR_COUNT = 3
1579 };
1580
1581 /* List of motion sensor types. */
1582 enum motionsensor_type {
1583         MOTIONSENSE_TYPE_ACCEL = 0,
1584         MOTIONSENSE_TYPE_GYRO = 1,
1585         MOTIONSENSE_TYPE_MAG = 2,
1586         MOTIONSENSE_TYPE_PROX = 3,
1587         MOTIONSENSE_TYPE_LIGHT = 4,
1588         MOTIONSENSE_TYPE_ACTIVITY = 5,
1589         MOTIONSENSE_TYPE_BARO = 6,
1590         MOTIONSENSE_TYPE_MAX,
1591 };
1592
1593 /* List of motion sensor locations. */
1594 enum motionsensor_location {
1595         MOTIONSENSE_LOC_BASE = 0,
1596         MOTIONSENSE_LOC_LID = 1,
1597         MOTIONSENSE_LOC_MAX,
1598 };
1599
1600 /* List of motion sensor chips. */
1601 enum motionsensor_chip {
1602         MOTIONSENSE_CHIP_KXCJ9 = 0,
1603 };
1604
1605 /* Module flag masks used for the dump sub-command. */
1606 #define MOTIONSENSE_MODULE_FLAG_ACTIVE (1<<0)
1607
1608 /* Sensor flag masks used for the dump sub-command. */
1609 #define MOTIONSENSE_SENSOR_FLAG_PRESENT (1<<0)
1610
1611 /*
1612  * Send this value for the data element to only perform a read. If you
1613  * send any other value, the EC will interpret it as data to set and will
1614  * return the actual value set.
1615  */
1616 #define EC_MOTION_SENSE_NO_VALUE -1
1617
1618 #define EC_MOTION_SENSE_INVALID_CALIB_TEMP 0x8000
1619
1620 /* Set Calibration information */
1621 #define MOTION_SENSE_SET_OFFSET 1
1622
1623 struct ec_response_motion_sensor_data {
1624         /* Flags for each sensor. */
1625         uint8_t flags;
1626         /* Sensor number the data comes from */
1627         uint8_t sensor_num;
1628         /* Each sensor is up to 3-axis. */
1629         union {
1630                 int16_t             data[3];
1631                 struct {
1632                         uint16_t    rsvd;
1633                         uint32_t    timestamp;
1634                 } __packed;
1635                 struct {
1636                         uint8_t     activity; /* motionsensor_activity */
1637                         uint8_t     state;
1638                         int16_t     add_info[2];
1639                 };
1640         };
1641 } __packed;
1642
1643 struct ec_params_motion_sense {
1644         uint8_t cmd;
1645         union {
1646                 /* Used for MOTIONSENSE_CMD_DUMP. */
1647                 struct {
1648                         /* no args */
1649                 } dump;
1650
1651                 /*
1652                  * Used for MOTIONSENSE_CMD_EC_RATE and
1653                  * MOTIONSENSE_CMD_KB_WAKE_ANGLE.
1654                  */
1655                 struct {
1656                         /* Data to set or EC_MOTION_SENSE_NO_VALUE to read. */
1657                         int16_t data;
1658                 } ec_rate, kb_wake_angle;
1659
1660                 /* Used for MOTIONSENSE_CMD_SENSOR_OFFSET */
1661                 struct {
1662                         uint8_t sensor_num;
1663
1664                         /*
1665                          * bit 0: If set (MOTION_SENSE_SET_OFFSET), set
1666                          * the calibration information in the EC.
1667                          * If unset, just retrieve calibration information.
1668                          */
1669                         uint16_t flags;
1670
1671                         /*
1672                          * Temperature at calibration, in units of 0.01 C
1673                          * 0x8000: invalid / unknown.
1674                          * 0x0: 0C
1675                          * 0x7fff: +327.67C
1676                          */
1677                         int16_t temp;
1678
1679                         /*
1680                          * Offset for calibration.
1681                          * Unit:
1682                          * Accelerometer: 1/1024 g
1683                          * Gyro:          1/1024 deg/s
1684                          * Compass:       1/16 uT
1685                          */
1686                         int16_t offset[3];
1687                 } __packed sensor_offset;
1688
1689                 /* Used for MOTIONSENSE_CMD_INFO. */
1690                 struct {
1691                         uint8_t sensor_num;
1692                 } info;
1693
1694                 /*
1695                  * Used for MOTIONSENSE_CMD_SENSOR_ODR and
1696                  * MOTIONSENSE_CMD_SENSOR_RANGE.
1697                  */
1698                 struct {
1699                         /* Should be element of enum motionsensor_id. */
1700                         uint8_t sensor_num;
1701
1702                         /* Rounding flag, true for round-up, false for down. */
1703                         uint8_t roundup;
1704
1705                         uint16_t reserved;
1706
1707                         /* Data to set or EC_MOTION_SENSE_NO_VALUE to read. */
1708                         int32_t data;
1709                 } sensor_odr, sensor_range;
1710         };
1711 } __packed;
1712
1713 struct ec_response_motion_sense {
1714         union {
1715                 /* Used for MOTIONSENSE_CMD_DUMP. */
1716                 struct {
1717                         /* Flags representing the motion sensor module. */
1718                         uint8_t module_flags;
1719
1720                         /* Number of sensors managed directly by the EC. */
1721                         uint8_t sensor_count;
1722
1723                         /*
1724                          * Sensor data is truncated if response_max is too small
1725                          * for holding all the data.
1726                          */
1727                         struct ec_response_motion_sensor_data sensor[0];
1728                 } dump;
1729
1730                 /* Used for MOTIONSENSE_CMD_INFO. */
1731                 struct {
1732                         /* Should be element of enum motionsensor_type. */
1733                         uint8_t type;
1734
1735                         /* Should be element of enum motionsensor_location. */
1736                         uint8_t location;
1737
1738                         /* Should be element of enum motionsensor_chip. */
1739                         uint8_t chip;
1740                 } info;
1741
1742                 /* Used for MOTIONSENSE_CMD_DATA */
1743                 struct ec_response_motion_sensor_data data;
1744
1745                 /*
1746                  * Used for MOTIONSENSE_CMD_EC_RATE, MOTIONSENSE_CMD_SENSOR_ODR,
1747                  * MOTIONSENSE_CMD_SENSOR_RANGE, and
1748                  * MOTIONSENSE_CMD_KB_WAKE_ANGLE.
1749                  */
1750                 struct {
1751                         /* Current value of the parameter queried. */
1752                         int32_t ret;
1753                 } ec_rate, sensor_odr, sensor_range, kb_wake_angle;
1754
1755                 /* Used for MOTIONSENSE_CMD_SENSOR_OFFSET */
1756                 struct {
1757                         int16_t temp;
1758                         int16_t offset[3];
1759                 } sensor_offset, perform_calib;
1760         };
1761 } __packed;
1762
1763 /*****************************************************************************/
1764 /* USB charging control commands */
1765
1766 /* Set USB port charging mode */
1767 #define EC_CMD_USB_CHARGE_SET_MODE 0x30
1768
1769 struct ec_params_usb_charge_set_mode {
1770         uint8_t usb_port_id;
1771         uint8_t mode;
1772 } __packed;
1773
1774 /*****************************************************************************/
1775 /* Persistent storage for host */
1776
1777 /* Maximum bytes that can be read/written in a single command */
1778 #define EC_PSTORE_SIZE_MAX 64
1779
1780 /* Get persistent storage info */
1781 #define EC_CMD_PSTORE_INFO 0x40
1782
1783 struct ec_response_pstore_info {
1784         /* Persistent storage size, in bytes */
1785         uint32_t pstore_size;
1786         /* Access size; read/write offset and size must be a multiple of this */
1787         uint32_t access_size;
1788 } __packed;
1789
1790 /*
1791  * Read persistent storage
1792  *
1793  * Response is params.size bytes of data.
1794  */
1795 #define EC_CMD_PSTORE_READ 0x41
1796
1797 struct ec_params_pstore_read {
1798         uint32_t offset;   /* Byte offset to read */
1799         uint32_t size;     /* Size to read in bytes */
1800 } __packed;
1801
1802 /* Write persistent storage */
1803 #define EC_CMD_PSTORE_WRITE 0x42
1804
1805 struct ec_params_pstore_write {
1806         uint32_t offset;   /* Byte offset to write */
1807         uint32_t size;     /* Size to write in bytes */
1808         uint8_t data[EC_PSTORE_SIZE_MAX];
1809 } __packed;
1810
1811 /*****************************************************************************/
1812 /* Real-time clock */
1813
1814 /* RTC params and response structures */
1815 struct ec_params_rtc {
1816         uint32_t time;
1817 } __packed;
1818
1819 struct ec_response_rtc {
1820         uint32_t time;
1821 } __packed;
1822
1823 /* These use ec_response_rtc */
1824 #define EC_CMD_RTC_GET_VALUE 0x44
1825 #define EC_CMD_RTC_GET_ALARM 0x45
1826
1827 /* These all use ec_params_rtc */
1828 #define EC_CMD_RTC_SET_VALUE 0x46
1829 #define EC_CMD_RTC_SET_ALARM 0x47
1830
1831 /* Pass as param to SET_ALARM to clear the current alarm */
1832 #define EC_RTC_ALARM_CLEAR 0
1833
1834 /*****************************************************************************/
1835 /* Port80 log access */
1836
1837 /* Maximum entries that can be read/written in a single command */
1838 #define EC_PORT80_SIZE_MAX 32
1839
1840 /* Get last port80 code from previous boot */
1841 #define EC_CMD_PORT80_LAST_BOOT 0x48
1842 #define EC_CMD_PORT80_READ 0x48
1843
1844 enum ec_port80_subcmd {
1845         EC_PORT80_GET_INFO = 0,
1846         EC_PORT80_READ_BUFFER,
1847 };
1848
1849 struct ec_params_port80_read {
1850         uint16_t subcmd;
1851         union {
1852                 struct {
1853                         uint32_t offset;
1854                         uint32_t num_entries;
1855                 } read_buffer;
1856         };
1857 } __packed;
1858
1859 struct ec_response_port80_read {
1860         union {
1861                 struct {
1862                         uint32_t writes;
1863                         uint32_t history_size;
1864                         uint32_t last_boot;
1865                 } get_info;
1866                 struct {
1867                         uint16_t codes[EC_PORT80_SIZE_MAX];
1868                 } data;
1869         };
1870 } __packed;
1871
1872 struct ec_response_port80_last_boot {
1873         uint16_t code;
1874 } __packed;
1875
1876 /*****************************************************************************/
1877 /* Thermal engine commands. Note that there are two implementations. We'll
1878  * reuse the command number, but the data and behavior is incompatible.
1879  * Version 0 is what originally shipped on Link.
1880  * Version 1 separates the CPU thermal limits from the fan control.
1881  */
1882
1883 #define EC_CMD_THERMAL_SET_THRESHOLD 0x50
1884 #define EC_CMD_THERMAL_GET_THRESHOLD 0x51
1885
1886 /* The version 0 structs are opaque. You have to know what they are for
1887  * the get/set commands to make any sense.
1888  */
1889
1890 /* Version 0 - set */
1891 struct ec_params_thermal_set_threshold {
1892         uint8_t sensor_type;
1893         uint8_t threshold_id;
1894         uint16_t value;
1895 } __packed;
1896
1897 /* Version 0 - get */
1898 struct ec_params_thermal_get_threshold {
1899         uint8_t sensor_type;
1900         uint8_t threshold_id;
1901 } __packed;
1902
1903 struct ec_response_thermal_get_threshold {
1904         uint16_t value;
1905 } __packed;
1906
1907
1908 /* The version 1 structs are visible. */
1909 enum ec_temp_thresholds {
1910         EC_TEMP_THRESH_WARN = 0,
1911         EC_TEMP_THRESH_HIGH,
1912         EC_TEMP_THRESH_HALT,
1913
1914         EC_TEMP_THRESH_COUNT
1915 };
1916
1917 /* Thermal configuration for one temperature sensor. Temps are in degrees K.
1918  * Zero values will be silently ignored by the thermal task.
1919  */
1920 struct ec_thermal_config {
1921         uint32_t temp_host[EC_TEMP_THRESH_COUNT]; /* levels of hotness */
1922         uint32_t temp_fan_off;          /* no active cooling needed */
1923         uint32_t temp_fan_max;          /* max active cooling needed */
1924 } __packed;
1925
1926 /* Version 1 - get config for one sensor. */
1927 struct ec_params_thermal_get_threshold_v1 {
1928         uint32_t sensor_num;
1929 } __packed;
1930 /* This returns a struct ec_thermal_config */
1931
1932 /* Version 1 - set config for one sensor.
1933  * Use read-modify-write for best results! */
1934 struct ec_params_thermal_set_threshold_v1 {
1935         uint32_t sensor_num;
1936         struct ec_thermal_config cfg;
1937 } __packed;
1938 /* This returns no data */
1939
1940 /****************************************************************************/
1941
1942 /* Toggle automatic fan control */
1943 #define EC_CMD_THERMAL_AUTO_FAN_CTRL 0x52
1944
1945 /* Get TMP006 calibration data */
1946 #define EC_CMD_TMP006_GET_CALIBRATION 0x53
1947
1948 struct ec_params_tmp006_get_calibration {
1949         uint8_t index;
1950 } __packed;
1951
1952 struct ec_response_tmp006_get_calibration {
1953         float s0;
1954         float b0;
1955         float b1;
1956         float b2;
1957 } __packed;
1958
1959 /* Set TMP006 calibration data */
1960 #define EC_CMD_TMP006_SET_CALIBRATION 0x54
1961
1962 struct ec_params_tmp006_set_calibration {
1963         uint8_t index;
1964         uint8_t reserved[3];  /* Reserved; set 0 */
1965         float s0;
1966         float b0;
1967         float b1;
1968         float b2;
1969 } __packed;
1970
1971 /* Read raw TMP006 data */
1972 #define EC_CMD_TMP006_GET_RAW 0x55
1973
1974 struct ec_params_tmp006_get_raw {
1975         uint8_t index;
1976 } __packed;
1977
1978 struct ec_response_tmp006_get_raw {
1979         int32_t t;  /* In 1/100 K */
1980         int32_t v;  /* In nV */
1981 };
1982
1983 /*****************************************************************************/
1984 /* MKBP - Matrix KeyBoard Protocol */
1985
1986 /*
1987  * Read key state
1988  *
1989  * Returns raw data for keyboard cols; see ec_response_mkbp_info.cols for
1990  * expected response size.
1991  *
1992  * NOTE: This has been superseded by EC_CMD_MKBP_GET_NEXT_EVENT.  If you wish
1993  * to obtain the instantaneous state, use EC_CMD_MKBP_INFO with the type
1994  * EC_MKBP_INFO_CURRENT and event EC_MKBP_EVENT_KEY_MATRIX.
1995  */
1996 #define EC_CMD_MKBP_STATE 0x60
1997
1998 /*
1999  * Provide information about various MKBP things.  See enum ec_mkbp_info_type.
2000  */
2001 #define EC_CMD_MKBP_INFO 0x61
2002
2003 struct ec_response_mkbp_info {
2004         uint32_t rows;
2005         uint32_t cols;
2006         /* Formerly "switches", which was 0. */
2007         uint8_t reserved;
2008 } __packed;
2009
2010 struct ec_params_mkbp_info {
2011         uint8_t info_type;
2012         uint8_t event_type;
2013 } __packed;
2014
2015 enum ec_mkbp_info_type {
2016         /*
2017          * Info about the keyboard matrix: number of rows and columns.
2018          *
2019          * Returns struct ec_response_mkbp_info.
2020          */
2021         EC_MKBP_INFO_KBD = 0,
2022
2023         /*
2024          * For buttons and switches, info about which specifically are
2025          * supported.  event_type must be set to one of the values in enum
2026          * ec_mkbp_event.
2027          *
2028          * For EC_MKBP_EVENT_BUTTON and EC_MKBP_EVENT_SWITCH, returns a 4 byte
2029          * bitmask indicating which buttons or switches are present.  See the
2030          * bit inidices below.
2031          */
2032         EC_MKBP_INFO_SUPPORTED = 1,
2033
2034         /*
2035          * Instantaneous state of buttons and switches.
2036          *
2037          * event_type must be set to one of the values in enum ec_mkbp_event.
2038          *
2039          * For EC_MKBP_EVENT_KEY_MATRIX, returns uint8_t key_matrix[13]
2040          * indicating the current state of the keyboard matrix.
2041          *
2042          * For EC_MKBP_EVENT_HOST_EVENT, return uint32_t host_event, the raw
2043          * event state.
2044          *
2045          * For EC_MKBP_EVENT_BUTTON, returns uint32_t buttons, indicating the
2046          * state of supported buttons.
2047          *
2048          * For EC_MKBP_EVENT_SWITCH, returns uint32_t switches, indicating the
2049          * state of supported switches.
2050          */
2051         EC_MKBP_INFO_CURRENT = 2,
2052 };
2053
2054 /* Simulate key press */
2055 #define EC_CMD_MKBP_SIMULATE_KEY 0x62
2056
2057 struct ec_params_mkbp_simulate_key {
2058         uint8_t col;
2059         uint8_t row;
2060         uint8_t pressed;
2061 } __packed;
2062
2063 /* Configure keyboard scanning */
2064 #define EC_CMD_MKBP_SET_CONFIG 0x64
2065 #define EC_CMD_MKBP_GET_CONFIG 0x65
2066
2067 /* flags */
2068 enum mkbp_config_flags {
2069         EC_MKBP_FLAGS_ENABLE = 1,       /* Enable keyboard scanning */
2070 };
2071
2072 enum mkbp_config_valid {
2073         EC_MKBP_VALID_SCAN_PERIOD               = 1 << 0,
2074         EC_MKBP_VALID_POLL_TIMEOUT              = 1 << 1,
2075         EC_MKBP_VALID_MIN_POST_SCAN_DELAY       = 1 << 3,
2076         EC_MKBP_VALID_OUTPUT_SETTLE             = 1 << 4,
2077         EC_MKBP_VALID_DEBOUNCE_DOWN             = 1 << 5,
2078         EC_MKBP_VALID_DEBOUNCE_UP               = 1 << 6,
2079         EC_MKBP_VALID_FIFO_MAX_DEPTH            = 1 << 7,
2080 };
2081
2082 /* Configuration for our key scanning algorithm */
2083 struct ec_mkbp_config {
2084         uint32_t valid_mask;            /* valid fields */
2085         uint8_t flags;          /* some flags (enum mkbp_config_flags) */
2086         uint8_t valid_flags;            /* which flags are valid */
2087         uint16_t scan_period_us;        /* period between start of scans */
2088         /* revert to interrupt mode after no activity for this long */
2089         uint32_t poll_timeout_us;
2090         /*
2091          * minimum post-scan relax time. Once we finish a scan we check
2092          * the time until we are due to start the next one. If this time is
2093          * shorter this field, we use this instead.
2094          */
2095         uint16_t min_post_scan_delay_us;
2096         /* delay between setting up output and waiting for it to settle */
2097         uint16_t output_settle_us;
2098         uint16_t debounce_down_us;      /* time for debounce on key down */
2099         uint16_t debounce_up_us;        /* time for debounce on key up */
2100         /* maximum depth to allow for fifo (0 = no keyscan output) */
2101         uint8_t fifo_max_depth;
2102 } __packed;
2103
2104 struct ec_params_mkbp_set_config {
2105         struct ec_mkbp_config config;
2106 } __packed;
2107
2108 struct ec_response_mkbp_get_config {
2109         struct ec_mkbp_config config;
2110 } __packed;
2111
2112 /* Run the key scan emulation */
2113 #define EC_CMD_KEYSCAN_SEQ_CTRL 0x66
2114
2115 enum ec_keyscan_seq_cmd {
2116         EC_KEYSCAN_SEQ_STATUS = 0,      /* Get status information */
2117         EC_KEYSCAN_SEQ_CLEAR = 1,       /* Clear sequence */
2118         EC_KEYSCAN_SEQ_ADD = 2,         /* Add item to sequence */
2119         EC_KEYSCAN_SEQ_START = 3,       /* Start running sequence */
2120         EC_KEYSCAN_SEQ_COLLECT = 4,     /* Collect sequence summary data */
2121 };
2122
2123 enum ec_collect_flags {
2124         /*
2125          * Indicates this scan was processed by the EC. Due to timing, some
2126          * scans may be skipped.
2127          */
2128         EC_KEYSCAN_SEQ_FLAG_DONE        = 1 << 0,
2129 };
2130
2131 struct ec_collect_item {
2132         uint8_t flags;          /* some flags (enum ec_collect_flags) */
2133 };
2134
2135 struct ec_params_keyscan_seq_ctrl {
2136         uint8_t cmd;    /* Command to send (enum ec_keyscan_seq_cmd) */
2137         union {
2138                 struct {
2139                         uint8_t active;         /* still active */
2140                         uint8_t num_items;      /* number of items */
2141                         /* Current item being presented */
2142                         uint8_t cur_item;
2143                 } status;
2144                 struct {
2145                         /*
2146                          * Absolute time for this scan, measured from the
2147                          * start of the sequence.
2148                          */
2149                         uint32_t time_us;
2150                         uint8_t scan[0];        /* keyscan data */
2151                 } add;
2152                 struct {
2153                         uint8_t start_item;     /* First item to return */
2154                         uint8_t num_items;      /* Number of items to return */
2155                 } collect;
2156         };
2157 } __packed;
2158
2159 struct ec_result_keyscan_seq_ctrl {
2160         union {
2161                 struct {
2162                         uint8_t num_items;      /* Number of items */
2163                         /* Data for each item */
2164                         struct ec_collect_item item[0];
2165                 } collect;
2166         };
2167 } __packed;
2168
2169 /*
2170  * Command for retrieving the next pending MKBP event from the EC device
2171  *
2172  * The device replies with UNAVAILABLE if there aren't any pending events.
2173  */
2174 #define EC_CMD_GET_NEXT_EVENT 0x67
2175
2176 enum ec_mkbp_event {
2177         /* Keyboard matrix changed. The event data is the new matrix state. */
2178         EC_MKBP_EVENT_KEY_MATRIX = 0,
2179
2180         /* New host event. The event data is 4 bytes of host event flags. */
2181         EC_MKBP_EVENT_HOST_EVENT = 1,
2182
2183         /* New Sensor FIFO data. The event data is fifo_info structure. */
2184         EC_MKBP_EVENT_SENSOR_FIFO = 2,
2185
2186         /* The state of the non-matrixed buttons have changed. */
2187         EC_MKBP_EVENT_BUTTON = 3,
2188
2189         /* The state of the switches have changed. */
2190         EC_MKBP_EVENT_SWITCH = 4,
2191
2192         /* EC sent a sysrq command */
2193         EC_MKBP_EVENT_SYSRQ = 6,
2194
2195         /* Notify the AP that something happened on CEC */
2196         EC_MKBP_EVENT_CEC_EVENT = 8,
2197
2198         /* Send an incoming CEC message to the AP */
2199         EC_MKBP_EVENT_CEC_MESSAGE = 9,
2200
2201         /* Number of MKBP events */
2202         EC_MKBP_EVENT_COUNT,
2203 };
2204
2205 union ec_response_get_next_data {
2206         uint8_t   key_matrix[13];
2207
2208         /* Unaligned */
2209         uint32_t  host_event;
2210
2211         uint32_t   buttons;
2212         uint32_t   switches;
2213         uint32_t   sysrq;
2214 } __packed;
2215
2216 union ec_response_get_next_data_v1 {
2217         uint8_t key_matrix[16];
2218         uint32_t host_event;
2219         uint32_t buttons;
2220         uint32_t switches;
2221         uint32_t sysrq;
2222         uint32_t cec_events;
2223         uint8_t cec_message[16];
2224 } __packed;
2225
2226 struct ec_response_get_next_event {
2227         uint8_t event_type;
2228         /* Followed by event data if any */
2229         union ec_response_get_next_data data;
2230 } __packed;
2231
2232 struct ec_response_get_next_event_v1 {
2233         uint8_t event_type;
2234         /* Followed by event data if any */
2235         union ec_response_get_next_data_v1 data;
2236 } __packed;
2237
2238 /* Bit indices for buttons and switches.*/
2239 /* Buttons */
2240 #define EC_MKBP_POWER_BUTTON    0
2241 #define EC_MKBP_VOL_UP          1
2242 #define EC_MKBP_VOL_DOWN        2
2243
2244 /* Switches */
2245 #define EC_MKBP_LID_OPEN        0
2246 #define EC_MKBP_TABLET_MODE     1
2247 #define EC_MKBP_BASE_ATTACHED   2
2248
2249 /*****************************************************************************/
2250 /* Temperature sensor commands */
2251
2252 /* Read temperature sensor info */
2253 #define EC_CMD_TEMP_SENSOR_GET_INFO 0x70
2254
2255 struct ec_params_temp_sensor_get_info {
2256         uint8_t id;
2257 } __packed;
2258
2259 struct ec_response_temp_sensor_get_info {
2260         char sensor_name[32];
2261         uint8_t sensor_type;
2262 } __packed;
2263
2264 /*****************************************************************************/
2265
2266 /*
2267  * Note: host commands 0x80 - 0x87 are reserved to avoid conflict with ACPI
2268  * commands accidentally sent to the wrong interface.  See the ACPI section
2269  * below.
2270  */
2271
2272 /*****************************************************************************/
2273 /* Host event commands */
2274
2275 /*
2276  * Host event mask params and response structures, shared by all of the host
2277  * event commands below.
2278  */
2279 struct ec_params_host_event_mask {
2280         uint32_t mask;
2281 } __packed;
2282
2283 struct ec_response_host_event_mask {
2284         uint32_t mask;
2285 } __packed;
2286
2287 /* These all use ec_response_host_event_mask */
2288 #define EC_CMD_HOST_EVENT_GET_B         0x87
2289 #define EC_CMD_HOST_EVENT_GET_SMI_MASK  0x88
2290 #define EC_CMD_HOST_EVENT_GET_SCI_MASK  0x89
2291 #define EC_CMD_HOST_EVENT_GET_WAKE_MASK 0x8d
2292
2293 /* These all use ec_params_host_event_mask */
2294 #define EC_CMD_HOST_EVENT_SET_SMI_MASK  0x8a
2295 #define EC_CMD_HOST_EVENT_SET_SCI_MASK  0x8b
2296 #define EC_CMD_HOST_EVENT_CLEAR         0x8c
2297 #define EC_CMD_HOST_EVENT_SET_WAKE_MASK 0x8e
2298 #define EC_CMD_HOST_EVENT_CLEAR_B       0x8f
2299
2300 /*****************************************************************************/
2301 /* Switch commands */
2302
2303 /* Enable/disable LCD backlight */
2304 #define EC_CMD_SWITCH_ENABLE_BKLIGHT 0x90
2305
2306 struct ec_params_switch_enable_backlight {
2307         uint8_t enabled;
2308 } __packed;
2309
2310 /* Enable/disable WLAN/Bluetooth */
2311 #define EC_CMD_SWITCH_ENABLE_WIRELESS 0x91
2312 #define EC_VER_SWITCH_ENABLE_WIRELESS 1
2313
2314 /* Version 0 params; no response */
2315 struct ec_params_switch_enable_wireless_v0 {
2316         uint8_t enabled;
2317 } __packed;
2318
2319 /* Version 1 params */
2320 struct ec_params_switch_enable_wireless_v1 {
2321         /* Flags to enable now */
2322         uint8_t now_flags;
2323
2324         /* Which flags to copy from now_flags */
2325         uint8_t now_mask;
2326
2327         /*
2328          * Flags to leave enabled in S3, if they're on at the S0->S3
2329          * transition.  (Other flags will be disabled by the S0->S3
2330          * transition.)
2331          */
2332         uint8_t suspend_flags;
2333
2334         /* Which flags to copy from suspend_flags */
2335         uint8_t suspend_mask;
2336 } __packed;
2337
2338 /* Version 1 response */
2339 struct ec_response_switch_enable_wireless_v1 {
2340         /* Flags to enable now */
2341         uint8_t now_flags;
2342
2343         /* Flags to leave enabled in S3 */
2344         uint8_t suspend_flags;
2345 } __packed;
2346
2347 /*****************************************************************************/
2348 /* GPIO commands. Only available on EC if write protect has been disabled. */
2349
2350 /* Set GPIO output value */
2351 #define EC_CMD_GPIO_SET 0x92
2352
2353 struct ec_params_gpio_set {
2354         char name[32];
2355         uint8_t val;
2356 } __packed;
2357
2358 /* Get GPIO value */
2359 #define EC_CMD_GPIO_GET 0x93
2360
2361 /* Version 0 of input params and response */
2362 struct ec_params_gpio_get {
2363         char name[32];
2364 } __packed;
2365 struct ec_response_gpio_get {
2366         uint8_t val;
2367 } __packed;
2368
2369 /* Version 1 of input params and response */
2370 struct ec_params_gpio_get_v1 {
2371         uint8_t subcmd;
2372         union {
2373                 struct {
2374                         char name[32];
2375                 } get_value_by_name;
2376                 struct {
2377                         uint8_t index;
2378                 } get_info;
2379         };
2380 } __packed;
2381
2382 struct ec_response_gpio_get_v1 {
2383         union {
2384                 struct {
2385                         uint8_t val;
2386                 } get_value_by_name, get_count;
2387                 struct {
2388                         uint8_t val;
2389                         char name[32];
2390                         uint32_t flags;
2391                 } get_info;
2392         };
2393 } __packed;
2394
2395 enum gpio_get_subcmd {
2396         EC_GPIO_GET_BY_NAME = 0,
2397         EC_GPIO_GET_COUNT = 1,
2398         EC_GPIO_GET_INFO = 2,
2399 };
2400
2401 /*****************************************************************************/
2402 /* I2C commands. Only available when flash write protect is unlocked. */
2403
2404 /*
2405  * TODO(crosbug.com/p/23570): These commands are deprecated, and will be
2406  * removed soon.  Use EC_CMD_I2C_XFER instead.
2407  */
2408
2409 /* Read I2C bus */
2410 #define EC_CMD_I2C_READ 0x94
2411
2412 struct ec_params_i2c_read {
2413         uint16_t addr; /* 8-bit address (7-bit shifted << 1) */
2414         uint8_t read_size; /* Either 8 or 16. */
2415         uint8_t port;
2416         uint8_t offset;
2417 } __packed;
2418 struct ec_response_i2c_read {
2419         uint16_t data;
2420 } __packed;
2421
2422 /* Write I2C bus */
2423 #define EC_CMD_I2C_WRITE 0x95
2424
2425 struct ec_params_i2c_write {
2426         uint16_t data;
2427         uint16_t addr; /* 8-bit address (7-bit shifted << 1) */
2428         uint8_t write_size; /* Either 8 or 16. */
2429         uint8_t port;
2430         uint8_t offset;
2431 } __packed;
2432
2433 /*****************************************************************************/
2434 /* Charge state commands. Only available when flash write protect unlocked. */
2435
2436 /* Force charge state machine to stop charging the battery or force it to
2437  * discharge the battery.
2438  */
2439 #define EC_CMD_CHARGE_CONTROL 0x96
2440 #define EC_VER_CHARGE_CONTROL 1
2441
2442 enum ec_charge_control_mode {
2443         CHARGE_CONTROL_NORMAL = 0,
2444         CHARGE_CONTROL_IDLE,
2445         CHARGE_CONTROL_DISCHARGE,
2446 };
2447
2448 struct ec_params_charge_control {
2449         uint32_t mode;  /* enum charge_control_mode */
2450 } __packed;
2451
2452 /*****************************************************************************/
2453 /* Console commands. Only available when flash write protect is unlocked. */
2454
2455 /* Snapshot console output buffer for use by EC_CMD_CONSOLE_READ. */
2456 #define EC_CMD_CONSOLE_SNAPSHOT 0x97
2457
2458 /*
2459  * Read data from the saved snapshot. If the subcmd parameter is
2460  * CONSOLE_READ_NEXT, this will return data starting from the beginning of
2461  * the latest snapshot. If it is CONSOLE_READ_RECENT, it will start from the
2462  * end of the previous snapshot.
2463  *
2464  * The params are only looked at in version >= 1 of this command. Prior
2465  * versions will just default to CONSOLE_READ_NEXT behavior.
2466  *
2467  * Response is null-terminated string.  Empty string, if there is no more
2468  * remaining output.
2469  */
2470 #define EC_CMD_CONSOLE_READ 0x98
2471
2472 enum ec_console_read_subcmd {
2473         CONSOLE_READ_NEXT = 0,
2474         CONSOLE_READ_RECENT
2475 };
2476
2477 struct ec_params_console_read_v1 {
2478         uint8_t subcmd; /* enum ec_console_read_subcmd */
2479 } __packed;
2480
2481 /*****************************************************************************/
2482
2483 /*
2484  * Cut off battery power immediately or after the host has shut down.
2485  *
2486  * return EC_RES_INVALID_COMMAND if unsupported by a board/battery.
2487  *        EC_RES_SUCCESS if the command was successful.
2488  *        EC_RES_ERROR if the cut off command failed.
2489  */
2490
2491 #define EC_CMD_BATTERY_CUT_OFF 0x99
2492
2493 #define EC_BATTERY_CUTOFF_FLAG_AT_SHUTDOWN      (1 << 0)
2494
2495 struct ec_params_battery_cutoff {
2496         uint8_t flags;
2497 } __packed;
2498
2499 /*****************************************************************************/
2500 /* USB port mux control. */
2501
2502 /*
2503  * Switch USB mux or return to automatic switching.
2504  */
2505 #define EC_CMD_USB_MUX 0x9a
2506
2507 struct ec_params_usb_mux {
2508         uint8_t mux;
2509 } __packed;
2510
2511 /*****************************************************************************/
2512 /* LDOs / FETs control. */
2513
2514 enum ec_ldo_state {
2515         EC_LDO_STATE_OFF = 0,   /* the LDO / FET is shut down */
2516         EC_LDO_STATE_ON = 1,    /* the LDO / FET is ON / providing power */
2517 };
2518
2519 /*
2520  * Switch on/off a LDO.
2521  */
2522 #define EC_CMD_LDO_SET 0x9b
2523
2524 struct ec_params_ldo_set {
2525         uint8_t index;
2526         uint8_t state;
2527 } __packed;
2528
2529 /*
2530  * Get LDO state.
2531  */
2532 #define EC_CMD_LDO_GET 0x9c
2533
2534 struct ec_params_ldo_get {
2535         uint8_t index;
2536 } __packed;
2537
2538 struct ec_response_ldo_get {
2539         uint8_t state;
2540 } __packed;
2541
2542 /*****************************************************************************/
2543 /* Power info. */
2544
2545 /*
2546  * Get power info.
2547  */
2548 #define EC_CMD_POWER_INFO 0x9d
2549
2550 struct ec_response_power_info {
2551         uint32_t usb_dev_type;
2552         uint16_t voltage_ac;
2553         uint16_t voltage_system;
2554         uint16_t current_system;
2555         uint16_t usb_current_limit;
2556 } __packed;
2557
2558 /*****************************************************************************/
2559 /* I2C passthru command */
2560
2561 #define EC_CMD_I2C_PASSTHRU 0x9e
2562
2563 /* Read data; if not present, message is a write */
2564 #define EC_I2C_FLAG_READ        (1 << 15)
2565
2566 /* Mask for address */
2567 #define EC_I2C_ADDR_MASK        0x3ff
2568
2569 #define EC_I2C_STATUS_NAK       (1 << 0) /* Transfer was not acknowledged */
2570 #define EC_I2C_STATUS_TIMEOUT   (1 << 1) /* Timeout during transfer */
2571
2572 /* Any error */
2573 #define EC_I2C_STATUS_ERROR     (EC_I2C_STATUS_NAK | EC_I2C_STATUS_TIMEOUT)
2574
2575 struct ec_params_i2c_passthru_msg {
2576         uint16_t addr_flags;    /* I2C slave address (7 or 10 bits) and flags */
2577         uint16_t len;           /* Number of bytes to read or write */
2578 } __packed;
2579
2580 struct ec_params_i2c_passthru {
2581         uint8_t port;           /* I2C port number */
2582         uint8_t num_msgs;       /* Number of messages */
2583         struct ec_params_i2c_passthru_msg msg[];
2584         /* Data to write for all messages is concatenated here */
2585 } __packed;
2586
2587 struct ec_response_i2c_passthru {
2588         uint8_t i2c_status;     /* Status flags (EC_I2C_STATUS_...) */
2589         uint8_t num_msgs;       /* Number of messages processed */
2590         uint8_t data[];         /* Data read by messages concatenated here */
2591 } __packed;
2592
2593 /*****************************************************************************/
2594 /* Power button hang detect */
2595
2596 #define EC_CMD_HANG_DETECT 0x9f
2597
2598 /* Reasons to start hang detection timer */
2599 /* Power button pressed */
2600 #define EC_HANG_START_ON_POWER_PRESS  (1 << 0)
2601
2602 /* Lid closed */
2603 #define EC_HANG_START_ON_LID_CLOSE    (1 << 1)
2604
2605  /* Lid opened */
2606 #define EC_HANG_START_ON_LID_OPEN     (1 << 2)
2607
2608 /* Start of AP S3->S0 transition (booting or resuming from suspend) */
2609 #define EC_HANG_START_ON_RESUME       (1 << 3)
2610
2611 /* Reasons to cancel hang detection */
2612
2613 /* Power button released */
2614 #define EC_HANG_STOP_ON_POWER_RELEASE (1 << 8)
2615
2616 /* Any host command from AP received */
2617 #define EC_HANG_STOP_ON_HOST_COMMAND  (1 << 9)
2618
2619 /* Stop on end of AP S0->S3 transition (suspending or shutting down) */
2620 #define EC_HANG_STOP_ON_SUSPEND       (1 << 10)
2621
2622 /*
2623  * If this flag is set, all the other fields are ignored, and the hang detect
2624  * timer is started.  This provides the AP a way to start the hang timer
2625  * without reconfiguring any of the other hang detect settings.  Note that
2626  * you must previously have configured the timeouts.
2627  */
2628 #define EC_HANG_START_NOW             (1 << 30)
2629
2630 /*
2631  * If this flag is set, all the other fields are ignored (including
2632  * EC_HANG_START_NOW).  This provides the AP a way to stop the hang timer
2633  * without reconfiguring any of the other hang detect settings.
2634  */
2635 #define EC_HANG_STOP_NOW              (1 << 31)
2636
2637 struct ec_params_hang_detect {
2638         /* Flags; see EC_HANG_* */
2639         uint32_t flags;
2640
2641         /* Timeout in msec before generating host event, if enabled */
2642         uint16_t host_event_timeout_msec;
2643
2644         /* Timeout in msec before generating warm reboot, if enabled */
2645         uint16_t warm_reboot_timeout_msec;
2646 } __packed;
2647
2648 /*****************************************************************************/
2649 /* Commands for battery charging */
2650
2651 /*
2652  * This is the single catch-all host command to exchange data regarding the
2653  * charge state machine (v2 and up).
2654  */
2655 #define EC_CMD_CHARGE_STATE 0xa0
2656
2657 /* Subcommands for this host command */
2658 enum charge_state_command {
2659         CHARGE_STATE_CMD_GET_STATE,
2660         CHARGE_STATE_CMD_GET_PARAM,
2661         CHARGE_STATE_CMD_SET_PARAM,
2662         CHARGE_STATE_NUM_CMDS
2663 };
2664
2665 /*
2666  * Known param numbers are defined here. Ranges are reserved for board-specific
2667  * params, which are handled by the particular implementations.
2668  */
2669 enum charge_state_params {
2670         CS_PARAM_CHG_VOLTAGE,         /* charger voltage limit */
2671         CS_PARAM_CHG_CURRENT,         /* charger current limit */
2672         CS_PARAM_CHG_INPUT_CURRENT,   /* charger input current limit */
2673         CS_PARAM_CHG_STATUS,          /* charger-specific status */
2674         CS_PARAM_CHG_OPTION,          /* charger-specific options */
2675         /* How many so far? */
2676         CS_NUM_BASE_PARAMS,
2677
2678         /* Range for CONFIG_CHARGER_PROFILE_OVERRIDE params */
2679         CS_PARAM_CUSTOM_PROFILE_MIN = 0x10000,
2680         CS_PARAM_CUSTOM_PROFILE_MAX = 0x1ffff,
2681
2682         /* Other custom param ranges go here... */
2683 };
2684
2685 struct ec_params_charge_state {
2686         uint8_t cmd;                            /* enum charge_state_command */
2687         union {
2688                 struct {
2689                         /* no args */
2690                 } get_state;
2691
2692                 struct {
2693                         uint32_t param;         /* enum charge_state_param */
2694                 } get_param;
2695
2696                 struct {
2697                         uint32_t param;         /* param to set */
2698                         uint32_t value;         /* value to set */
2699                 } set_param;
2700         };
2701 } __packed;
2702
2703 struct ec_response_charge_state {
2704         union {
2705                 struct {
2706                         int ac;
2707                         int chg_voltage;
2708                         int chg_current;
2709                         int chg_input_current;
2710                         int batt_state_of_charge;
2711                 } get_state;
2712
2713                 struct {
2714                         uint32_t value;
2715                 } get_param;
2716                 struct {
2717                         /* no return values */
2718                 } set_param;
2719         };
2720 } __packed;
2721
2722
2723 /*
2724  * Set maximum battery charging current.
2725  */
2726 #define EC_CMD_CHARGE_CURRENT_LIMIT 0xa1
2727
2728 struct ec_params_current_limit {
2729         uint32_t limit; /* in mA */
2730 } __packed;
2731
2732 /*
2733  * Set maximum external voltage / current.
2734  */
2735 #define EC_CMD_EXTERNAL_POWER_LIMIT 0x00A2
2736
2737 /* Command v0 is used only on Spring and is obsolete + unsupported */
2738 struct ec_params_external_power_limit_v1 {
2739         uint16_t current_lim; /* in mA, or EC_POWER_LIMIT_NONE to clear limit */
2740         uint16_t voltage_lim; /* in mV, or EC_POWER_LIMIT_NONE to clear limit */
2741 } __packed;
2742
2743 #define EC_POWER_LIMIT_NONE 0xffff
2744
2745 /* Inform the EC when entering a sleep state */
2746 #define EC_CMD_HOST_SLEEP_EVENT 0xa9
2747
2748 enum host_sleep_event {
2749         HOST_SLEEP_EVENT_S3_SUSPEND   = 1,
2750         HOST_SLEEP_EVENT_S3_RESUME    = 2,
2751         HOST_SLEEP_EVENT_S0IX_SUSPEND = 3,
2752         HOST_SLEEP_EVENT_S0IX_RESUME  = 4
2753 };
2754
2755 struct ec_params_host_sleep_event {
2756         uint8_t sleep_event;
2757 } __packed;
2758
2759 /*
2760  * Use a default timeout value (CONFIG_SLEEP_TIMEOUT_MS) for detecting sleep
2761  * transition failures
2762  */
2763 #define EC_HOST_SLEEP_TIMEOUT_DEFAULT 0
2764
2765 /* Disable timeout detection for this sleep transition */
2766 #define EC_HOST_SLEEP_TIMEOUT_INFINITE 0xFFFF
2767
2768 struct ec_params_host_sleep_event_v1 {
2769         /* The type of sleep being entered or exited. */
2770         uint8_t sleep_event;
2771
2772         /* Padding */
2773         uint8_t reserved;
2774         union {
2775                 /* Parameters that apply for suspend messages. */
2776                 struct {
2777                         /*
2778                          * The timeout in milliseconds between when this message
2779                          * is received and when the EC will declare sleep
2780                          * transition failure if the sleep signal is not
2781                          * asserted.
2782                          */
2783                         uint16_t sleep_timeout_ms;
2784                 } suspend_params;
2785
2786                 /* No parameters for non-suspend messages. */
2787         };
2788 } __packed;
2789
2790 /* A timeout occurred when this bit is set */
2791 #define EC_HOST_RESUME_SLEEP_TIMEOUT 0x80000000
2792
2793 /*
2794  * The mask defining which bits correspond to the number of sleep transitions,
2795  * as well as the maximum number of suspend line transitions that will be
2796  * reported back to the host.
2797  */
2798 #define EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK 0x7FFFFFFF
2799
2800 struct ec_response_host_sleep_event_v1 {
2801         union {
2802                 /* Response fields that apply for resume messages. */
2803                 struct {
2804                         /*
2805                          * The number of sleep power signal transitions that
2806                          * occurred since the suspend message. The high bit
2807                          * indicates a timeout occurred.
2808                          */
2809                         uint32_t sleep_transitions;
2810                 } resume_response;
2811
2812                 /* No response fields for non-resume messages. */
2813         };
2814 } __packed;
2815
2816 /*****************************************************************************/
2817 /* Smart battery pass-through */
2818
2819 /* Get / Set 16-bit smart battery registers */
2820 #define EC_CMD_SB_READ_WORD   0xb0
2821 #define EC_CMD_SB_WRITE_WORD  0xb1
2822
2823 /* Get / Set string smart battery parameters
2824  * formatted as SMBUS "block".
2825  */
2826 #define EC_CMD_SB_READ_BLOCK  0xb2
2827 #define EC_CMD_SB_WRITE_BLOCK 0xb3
2828
2829 struct ec_params_sb_rd {
2830         uint8_t reg;
2831 } __packed;
2832
2833 struct ec_response_sb_rd_word {
2834         uint16_t value;
2835 } __packed;
2836
2837 struct ec_params_sb_wr_word {
2838         uint8_t reg;
2839         uint16_t value;
2840 } __packed;
2841
2842 struct ec_response_sb_rd_block {
2843         uint8_t data[32];
2844 } __packed;
2845
2846 struct ec_params_sb_wr_block {
2847         uint8_t reg;
2848         uint16_t data[32];
2849 } __packed;
2850
2851 /*****************************************************************************/
2852 /* Battery vendor parameters
2853  *
2854  * Get or set vendor-specific parameters in the battery. Implementations may
2855  * differ between boards or batteries. On a set operation, the response
2856  * contains the actual value set, which may be rounded or clipped from the
2857  * requested value.
2858  */
2859
2860 #define EC_CMD_BATTERY_VENDOR_PARAM 0xb4
2861
2862 enum ec_battery_vendor_param_mode {
2863         BATTERY_VENDOR_PARAM_MODE_GET = 0,
2864         BATTERY_VENDOR_PARAM_MODE_SET,
2865 };
2866
2867 struct ec_params_battery_vendor_param {
2868         uint32_t param;
2869         uint32_t value;
2870         uint8_t mode;
2871 } __packed;
2872
2873 struct ec_response_battery_vendor_param {
2874         uint32_t value;
2875 } __packed;
2876
2877 /*****************************************************************************/
2878 /* Commands for I2S recording on audio codec. */
2879
2880 #define EC_CMD_CODEC_I2S 0x00BC
2881
2882 enum ec_codec_i2s_subcmd {
2883         EC_CODEC_SET_SAMPLE_DEPTH = 0x0,
2884         EC_CODEC_SET_GAIN = 0x1,
2885         EC_CODEC_GET_GAIN = 0x2,
2886         EC_CODEC_I2S_ENABLE = 0x3,
2887         EC_CODEC_I2S_SET_CONFIG = 0x4,
2888         EC_CODEC_I2S_SET_TDM_CONFIG = 0x5,
2889         EC_CODEC_I2S_SET_BCLK = 0x6,
2890 };
2891
2892 enum ec_sample_depth_value {
2893         EC_CODEC_SAMPLE_DEPTH_16 = 0,
2894         EC_CODEC_SAMPLE_DEPTH_24 = 1,
2895 };
2896
2897 enum ec_i2s_config {
2898         EC_DAI_FMT_I2S = 0,
2899         EC_DAI_FMT_RIGHT_J = 1,
2900         EC_DAI_FMT_LEFT_J = 2,
2901         EC_DAI_FMT_PCM_A = 3,
2902         EC_DAI_FMT_PCM_B = 4,
2903         EC_DAI_FMT_PCM_TDM = 5,
2904 };
2905
2906 struct ec_param_codec_i2s {
2907         /*
2908          * enum ec_codec_i2s_subcmd
2909          */
2910         uint8_t cmd;
2911         union {
2912                 /*
2913                  * EC_CODEC_SET_SAMPLE_DEPTH
2914                  * Value should be one of ec_sample_depth_value.
2915                  */
2916                 uint8_t depth;
2917
2918                 /*
2919                  * EC_CODEC_SET_GAIN
2920                  * Value should be 0~43 for both channels.
2921                  */
2922                 struct ec_param_codec_i2s_set_gain {
2923                         uint8_t left;
2924                         uint8_t right;
2925                 } __packed gain;
2926
2927                 /*
2928                  * EC_CODEC_I2S_ENABLE
2929                  * 1 to enable, 0 to disable.
2930                  */
2931                 uint8_t i2s_enable;
2932
2933                 /*
2934                  * EC_CODEC_I2S_SET_COFNIG
2935                  * Value should be one of ec_i2s_config.
2936                  */
2937                 uint8_t i2s_config;
2938
2939                 /*
2940                  * EC_CODEC_I2S_SET_TDM_CONFIG
2941                  * Value should be one of ec_i2s_config.
2942                  */
2943                 struct ec_param_codec_i2s_tdm {
2944                         /*
2945                          * 0 to 496
2946                          */
2947                         int16_t ch0_delay;
2948                         /*
2949                          * -1 to 496
2950                          */
2951                         int16_t ch1_delay;
2952                         uint8_t adjacent_to_ch0;
2953                         uint8_t adjacent_to_ch1;
2954                 } __packed tdm_param;
2955
2956                 /*
2957                  * EC_CODEC_I2S_SET_BCLK
2958                  */
2959                 uint32_t bclk;
2960         };
2961 } __packed;
2962
2963 /*
2964  * For subcommand EC_CODEC_GET_GAIN.
2965  */
2966 struct ec_response_codec_gain {
2967         uint8_t left;
2968         uint8_t right;
2969 } __packed;
2970
2971 /*****************************************************************************/
2972 /* System commands */
2973
2974 /*
2975  * TODO(crosbug.com/p/23747): This is a confusing name, since it doesn't
2976  * necessarily reboot the EC.  Rename to "image" or something similar?
2977  */
2978 #define EC_CMD_REBOOT_EC 0xd2
2979
2980 /* Command */
2981 enum ec_reboot_cmd {
2982         EC_REBOOT_CANCEL = 0,        /* Cancel a pending reboot */
2983         EC_REBOOT_JUMP_RO = 1,       /* Jump to RO without rebooting */
2984         EC_REBOOT_JUMP_RW = 2,       /* Jump to RW without rebooting */
2985         /* (command 3 was jump to RW-B) */
2986         EC_REBOOT_COLD = 4,          /* Cold-reboot */
2987         EC_REBOOT_DISABLE_JUMP = 5,  /* Disable jump until next reboot */
2988         EC_REBOOT_HIBERNATE = 6      /* Hibernate EC */
2989 };
2990
2991 /* Flags for ec_params_reboot_ec.reboot_flags */
2992 #define EC_REBOOT_FLAG_RESERVED0      (1 << 0)  /* Was recovery request */
2993 #define EC_REBOOT_FLAG_ON_AP_SHUTDOWN (1 << 1)  /* Reboot after AP shutdown */
2994
2995 struct ec_params_reboot_ec {
2996         uint8_t cmd;           /* enum ec_reboot_cmd */
2997         uint8_t flags;         /* See EC_REBOOT_FLAG_* */
2998 } __packed;
2999
3000 /*
3001  * Get information on last EC panic.
3002  *
3003  * Returns variable-length platform-dependent panic information.  See panic.h
3004  * for details.
3005  */
3006 #define EC_CMD_GET_PANIC_INFO 0xd3
3007
3008 /*****************************************************************************/
3009 /*
3010  * ACPI commands
3011  *
3012  * These are valid ONLY on the ACPI command/data port.
3013  */
3014
3015 /*
3016  * ACPI Read Embedded Controller
3017  *
3018  * This reads from ACPI memory space on the EC (EC_ACPI_MEM_*).
3019  *
3020  * Use the following sequence:
3021  *
3022  *    - Write EC_CMD_ACPI_READ to EC_LPC_ADDR_ACPI_CMD
3023  *    - Wait for EC_LPC_CMDR_PENDING bit to clear
3024  *    - Write address to EC_LPC_ADDR_ACPI_DATA
3025  *    - Wait for EC_LPC_CMDR_DATA bit to set
3026  *    - Read value from EC_LPC_ADDR_ACPI_DATA
3027  */
3028 #define EC_CMD_ACPI_READ 0x80
3029
3030 /*
3031  * ACPI Write Embedded Controller
3032  *
3033  * This reads from ACPI memory space on the EC (EC_ACPI_MEM_*).
3034  *
3035  * Use the following sequence:
3036  *
3037  *    - Write EC_CMD_ACPI_WRITE to EC_LPC_ADDR_ACPI_CMD
3038  *    - Wait for EC_LPC_CMDR_PENDING bit to clear
3039  *    - Write address to EC_LPC_ADDR_ACPI_DATA
3040  *    - Wait for EC_LPC_CMDR_PENDING bit to clear
3041  *    - Write value to EC_LPC_ADDR_ACPI_DATA
3042  */
3043 #define EC_CMD_ACPI_WRITE 0x81
3044
3045 /*
3046  * ACPI Query Embedded Controller
3047  *
3048  * This clears the lowest-order bit in the currently pending host events, and
3049  * sets the result code to the 1-based index of the bit (event 0x00000001 = 1,
3050  * event 0x80000000 = 32), or 0 if no event was pending.
3051  */
3052 #define EC_CMD_ACPI_QUERY_EVENT 0x84
3053
3054 /* Valid addresses in ACPI memory space, for read/write commands */
3055
3056 /* Memory space version; set to EC_ACPI_MEM_VERSION_CURRENT */
3057 #define EC_ACPI_MEM_VERSION            0x00
3058 /*
3059  * Test location; writing value here updates test compliment byte to (0xff -
3060  * value).
3061  */
3062 #define EC_ACPI_MEM_TEST               0x01
3063 /* Test compliment; writes here are ignored. */
3064 #define EC_ACPI_MEM_TEST_COMPLIMENT    0x02
3065
3066 /* Keyboard backlight brightness percent (0 - 100) */
3067 #define EC_ACPI_MEM_KEYBOARD_BACKLIGHT 0x03
3068 /* DPTF Target Fan Duty (0-100, 0xff for auto/none) */
3069 #define EC_ACPI_MEM_FAN_DUTY           0x04
3070
3071 /*
3072  * DPTF temp thresholds. Any of the EC's temp sensors can have up to two
3073  * independent thresholds attached to them. The current value of the ID
3074  * register determines which sensor is affected by the THRESHOLD and COMMIT
3075  * registers. The THRESHOLD register uses the same EC_TEMP_SENSOR_OFFSET scheme
3076  * as the memory-mapped sensors. The COMMIT register applies those settings.
3077  *
3078  * The spec does not mandate any way to read back the threshold settings
3079  * themselves, but when a threshold is crossed the AP needs a way to determine
3080  * which sensor(s) are responsible. Each reading of the ID register clears and
3081  * returns one sensor ID that has crossed one of its threshold (in either
3082  * direction) since the last read. A value of 0xFF means "no new thresholds
3083  * have tripped". Setting or enabling the thresholds for a sensor will clear
3084  * the unread event count for that sensor.
3085  */
3086 #define EC_ACPI_MEM_TEMP_ID            0x05
3087 #define EC_ACPI_MEM_TEMP_THRESHOLD     0x06
3088 #define EC_ACPI_MEM_TEMP_COMMIT        0x07
3089 /*
3090  * Here are the bits for the COMMIT register:
3091  *   bit 0 selects the threshold index for the chosen sensor (0/1)
3092  *   bit 1 enables/disables the selected threshold (0 = off, 1 = on)
3093  * Each write to the commit register affects one threshold.
3094  */
3095 #define EC_ACPI_MEM_TEMP_COMMIT_SELECT_MASK (1 << 0)
3096 #define EC_ACPI_MEM_TEMP_COMMIT_ENABLE_MASK (1 << 1)
3097 /*
3098  * Example:
3099  *
3100  * Set the thresholds for sensor 2 to 50 C and 60 C:
3101  *   write 2 to [0x05]      --  select temp sensor 2
3102  *   write 0x7b to [0x06]   --  C_TO_K(50) - EC_TEMP_SENSOR_OFFSET
3103  *   write 0x2 to [0x07]    --  enable threshold 0 with this value
3104  *   write 0x85 to [0x06]   --  C_TO_K(60) - EC_TEMP_SENSOR_OFFSET
3105  *   write 0x3 to [0x07]    --  enable threshold 1 with this value
3106  *
3107  * Disable the 60 C threshold, leaving the 50 C threshold unchanged:
3108  *   write 2 to [0x05]      --  select temp sensor 2
3109  *   write 0x1 to [0x07]    --  disable threshold 1
3110  */
3111
3112 /* DPTF battery charging current limit */
3113 #define EC_ACPI_MEM_CHARGING_LIMIT     0x08
3114
3115 /* Charging limit is specified in 64 mA steps */
3116 #define EC_ACPI_MEM_CHARGING_LIMIT_STEP_MA   64
3117 /* Value to disable DPTF battery charging limit */
3118 #define EC_ACPI_MEM_CHARGING_LIMIT_DISABLED  0xff
3119
3120 /* Current version of ACPI memory address space */
3121 #define EC_ACPI_MEM_VERSION_CURRENT 1
3122
3123
3124 /*****************************************************************************/
3125 /*
3126  * HDMI CEC commands
3127  *
3128  * These commands are for sending and receiving message via HDMI CEC
3129  */
3130 #define EC_MAX_CEC_MSG_LEN 16
3131
3132 /* CEC message from the AP to be written on the CEC bus */
3133 #define EC_CMD_CEC_WRITE_MSG 0x00B8
3134
3135 /**
3136  * struct ec_params_cec_write - Message to write to the CEC bus
3137  * @msg: message content to write to the CEC bus
3138  */
3139 struct ec_params_cec_write {
3140         uint8_t msg[EC_MAX_CEC_MSG_LEN];
3141 } __packed;
3142
3143 /* Set various CEC parameters */
3144 #define EC_CMD_CEC_SET 0x00BA
3145
3146 /**
3147  * struct ec_params_cec_set - CEC parameters set
3148  * @cmd: parameter type, can be CEC_CMD_ENABLE or CEC_CMD_LOGICAL_ADDRESS
3149  * @val: in case cmd is CEC_CMD_ENABLE, this field can be 0 to disable CEC
3150  *      or 1 to enable CEC functionality, in case cmd is CEC_CMD_LOGICAL_ADDRESS,
3151  *      this field encodes the requested logical address between 0 and 15
3152  *      or 0xff to unregister
3153  */
3154 struct ec_params_cec_set {
3155         uint8_t cmd; /* enum cec_command */
3156         uint8_t val;
3157 } __packed;
3158
3159 /* Read various CEC parameters */
3160 #define EC_CMD_CEC_GET 0x00BB
3161
3162 /**
3163  * struct ec_params_cec_get - CEC parameters get
3164  * @cmd: parameter type, can be CEC_CMD_ENABLE or CEC_CMD_LOGICAL_ADDRESS
3165  */
3166 struct ec_params_cec_get {
3167         uint8_t cmd; /* enum cec_command */
3168 } __packed;
3169
3170 /**
3171  * struct ec_response_cec_get - CEC parameters get response
3172  * @val: in case cmd was CEC_CMD_ENABLE, this field will 0 if CEC is
3173  *      disabled or 1 if CEC functionality is enabled,
3174  *      in case cmd was CEC_CMD_LOGICAL_ADDRESS, this will encode the
3175  *      configured logical address between 0 and 15 or 0xff if unregistered
3176  */
3177 struct ec_response_cec_get {
3178         uint8_t val;
3179 } __packed;
3180
3181 /* CEC parameters command */
3182 enum ec_cec_command {
3183         /* CEC reading, writing and events enable */
3184         CEC_CMD_ENABLE,
3185         /* CEC logical address  */
3186         CEC_CMD_LOGICAL_ADDRESS,
3187 };
3188
3189 /* Events from CEC to AP */
3190 enum mkbp_cec_event {
3191         /* Outgoing message was acknowledged by a follower */
3192         EC_MKBP_CEC_SEND_OK                     = BIT(0),
3193         /* Outgoing message was not acknowledged */
3194         EC_MKBP_CEC_SEND_FAILED                 = BIT(1),
3195 };
3196
3197 /*****************************************************************************/
3198 /*
3199  * Special commands
3200  *
3201  * These do not follow the normal rules for commands.  See each command for
3202  * details.
3203  */
3204
3205 /*
3206  * Reboot NOW
3207  *
3208  * This command will work even when the EC LPC interface is busy, because the
3209  * reboot command is processed at interrupt level.  Note that when the EC
3210  * reboots, the host will reboot too, so there is no response to this command.
3211  *
3212  * Use EC_CMD_REBOOT_EC to reboot the EC more politely.
3213  */
3214 #define EC_CMD_REBOOT 0xd1  /* Think "die" */
3215
3216 /*
3217  * Resend last response (not supported on LPC).
3218  *
3219  * Returns EC_RES_UNAVAILABLE if there is no response available - for example,
3220  * there was no previous command, or the previous command's response was too
3221  * big to save.
3222  */
3223 #define EC_CMD_RESEND_RESPONSE 0xdb
3224
3225 /*
3226  * This header byte on a command indicate version 0. Any header byte less
3227  * than this means that we are talking to an old EC which doesn't support
3228  * versioning. In that case, we assume version 0.
3229  *
3230  * Header bytes greater than this indicate a later version. For example,
3231  * EC_CMD_VERSION0 + 1 means we are using version 1.
3232  *
3233  * The old EC interface must not use commands 0xdc or higher.
3234  */
3235 #define EC_CMD_VERSION0 0xdc
3236
3237 #endif  /* !__ACPI__ */
3238
3239 /*****************************************************************************/
3240 /*
3241  * PD commands
3242  *
3243  * These commands are for PD MCU communication.
3244  */
3245
3246 /* EC to PD MCU exchange status command */
3247 #define EC_CMD_PD_EXCHANGE_STATUS 0x100
3248
3249 /* Status of EC being sent to PD */
3250 struct ec_params_pd_status {
3251         int8_t batt_soc; /* battery state of charge */
3252 } __packed;
3253
3254 /* Status of PD being sent back to EC */
3255 struct ec_response_pd_status {
3256         int8_t status;        /* PD MCU status */
3257         uint32_t curr_lim_ma; /* input current limit */
3258 } __packed;
3259
3260 /* Set USB type-C port role and muxes */
3261 #define EC_CMD_USB_PD_CONTROL 0x101
3262
3263 enum usb_pd_control_role {
3264         USB_PD_CTRL_ROLE_NO_CHANGE = 0,
3265         USB_PD_CTRL_ROLE_TOGGLE_ON = 1, /* == AUTO */
3266         USB_PD_CTRL_ROLE_TOGGLE_OFF = 2,
3267         USB_PD_CTRL_ROLE_FORCE_SINK = 3,
3268         USB_PD_CTRL_ROLE_FORCE_SOURCE = 4,
3269 };
3270
3271 enum usb_pd_control_mux {
3272         USB_PD_CTRL_MUX_NO_CHANGE = 0,
3273         USB_PD_CTRL_MUX_NONE = 1,
3274         USB_PD_CTRL_MUX_USB = 2,
3275         USB_PD_CTRL_MUX_DP = 3,
3276         USB_PD_CTRL_MUX_DOCK = 4,
3277         USB_PD_CTRL_MUX_AUTO = 5,
3278 };
3279
3280 enum usb_pd_control_swap {
3281         USB_PD_CTRL_SWAP_NONE = 0,
3282         USB_PD_CTRL_SWAP_DATA = 1,
3283         USB_PD_CTRL_SWAP_POWER = 2,
3284         USB_PD_CTRL_SWAP_VCONN = 3,
3285         USB_PD_CTRL_SWAP_COUNT
3286 };
3287
3288 struct ec_params_usb_pd_control {
3289         uint8_t port;
3290         uint8_t role;
3291         uint8_t mux;
3292         uint8_t swap;
3293 } __packed;
3294
3295 #define PD_CTRL_RESP_ENABLED_COMMS      (1 << 0) /* Communication enabled */
3296 #define PD_CTRL_RESP_ENABLED_CONNECTED  (1 << 1) /* Device connected */
3297 #define PD_CTRL_RESP_ENABLED_PD_CAPABLE (1 << 2) /* Partner is PD capable */
3298
3299 #define PD_CTRL_RESP_ROLE_POWER         BIT(0) /* 0=SNK/1=SRC */
3300 #define PD_CTRL_RESP_ROLE_DATA          BIT(1) /* 0=UFP/1=DFP */
3301 #define PD_CTRL_RESP_ROLE_VCONN         BIT(2) /* Vconn status */
3302 #define PD_CTRL_RESP_ROLE_DR_POWER      BIT(3) /* Partner is dualrole power */
3303 #define PD_CTRL_RESP_ROLE_DR_DATA       BIT(4) /* Partner is dualrole data */
3304 #define PD_CTRL_RESP_ROLE_USB_COMM      BIT(5) /* Partner USB comm capable */
3305 #define PD_CTRL_RESP_ROLE_EXT_POWERED   BIT(6) /* Partner externally powerd */
3306
3307 struct ec_response_usb_pd_control_v1 {
3308         uint8_t enabled;
3309         uint8_t role;
3310         uint8_t polarity;
3311         char state[32];
3312 } __packed;
3313
3314 #define EC_CMD_USB_PD_PORTS 0x102
3315
3316 /* Maximum number of PD ports on a device, num_ports will be <= this */
3317 #define EC_USB_PD_MAX_PORTS 8
3318
3319 struct ec_response_usb_pd_ports {
3320         uint8_t num_ports;
3321 } __packed;
3322
3323 #define EC_CMD_USB_PD_POWER_INFO 0x103
3324
3325 #define PD_POWER_CHARGING_PORT 0xff
3326 struct ec_params_usb_pd_power_info {
3327         uint8_t port;
3328 } __packed;
3329
3330 enum usb_chg_type {
3331         USB_CHG_TYPE_NONE,
3332         USB_CHG_TYPE_PD,
3333         USB_CHG_TYPE_C,
3334         USB_CHG_TYPE_PROPRIETARY,
3335         USB_CHG_TYPE_BC12_DCP,
3336         USB_CHG_TYPE_BC12_CDP,
3337         USB_CHG_TYPE_BC12_SDP,
3338         USB_CHG_TYPE_OTHER,
3339         USB_CHG_TYPE_VBUS,
3340         USB_CHG_TYPE_UNKNOWN,
3341 };
3342 enum usb_power_roles {
3343         USB_PD_PORT_POWER_DISCONNECTED,
3344         USB_PD_PORT_POWER_SOURCE,
3345         USB_PD_PORT_POWER_SINK,
3346         USB_PD_PORT_POWER_SINK_NOT_CHARGING,
3347 };
3348
3349 struct usb_chg_measures {
3350         uint16_t voltage_max;
3351         uint16_t voltage_now;
3352         uint16_t current_max;
3353         uint16_t current_lim;
3354 } __packed;
3355
3356 struct ec_response_usb_pd_power_info {
3357         uint8_t role;
3358         uint8_t type;
3359         uint8_t dualrole;
3360         uint8_t reserved1;
3361         struct usb_chg_measures meas;
3362         uint32_t max_power;
3363 } __packed;
3364
3365 struct ec_params_usb_pd_info_request {
3366         uint8_t port;
3367 } __packed;
3368
3369 /*
3370  * This command will return the number of USB PD charge port + the number
3371  * of dedicated port present.
3372  * EC_CMD_USB_PD_PORTS does NOT include the dedicated ports
3373  */
3374 #define EC_CMD_CHARGE_PORT_COUNT 0x0105
3375 struct ec_response_charge_port_count {
3376         uint8_t port_count;
3377 } __packed;
3378
3379 /* Read USB-PD Device discovery info */
3380 #define EC_CMD_USB_PD_DISCOVERY 0x0113
3381 struct ec_params_usb_pd_discovery_entry {
3382         uint16_t vid;  /* USB-IF VID */
3383         uint16_t pid;  /* USB-IF PID */
3384         uint8_t ptype; /* product type (hub,periph,cable,ama) */
3385 } __packed;
3386
3387 /* Override default charge behavior */
3388 #define EC_CMD_PD_CHARGE_PORT_OVERRIDE 0x0114
3389
3390 /* Negative port parameters have special meaning */
3391 enum usb_pd_override_ports {
3392         OVERRIDE_DONT_CHARGE = -2,
3393         OVERRIDE_OFF = -1,
3394         /* [0, CONFIG_USB_PD_PORT_COUNT): Port# */
3395 };
3396
3397 struct ec_params_charge_port_override {
3398         int16_t override_port; /* Override port# */
3399 } __packed;
3400
3401 /* Read (and delete) one entry of PD event log */
3402 #define EC_CMD_PD_GET_LOG_ENTRY 0x0115
3403
3404 struct ec_response_pd_log {
3405         uint32_t timestamp; /* relative timestamp in milliseconds */
3406         uint8_t type;       /* event type : see PD_EVENT_xx below */
3407         uint8_t size_port;  /* [7:5] port number [4:0] payload size in bytes */
3408         uint16_t data;      /* type-defined data payload */
3409         uint8_t payload[0]; /* optional additional data payload: 0..16 bytes */
3410 } __packed;
3411
3412 /* The timestamp is the microsecond counter shifted to get about a ms. */
3413 #define PD_LOG_TIMESTAMP_SHIFT 10 /* 1 LSB = 1024us */
3414
3415 #define PD_LOG_SIZE_MASK  0x1f
3416 #define PD_LOG_PORT_MASK  0xe0
3417 #define PD_LOG_PORT_SHIFT    5
3418 #define PD_LOG_PORT_SIZE(port, size) (((port) << PD_LOG_PORT_SHIFT) | \
3419                                       ((size) & PD_LOG_SIZE_MASK))
3420 #define PD_LOG_PORT(size_port) ((size_port) >> PD_LOG_PORT_SHIFT)
3421 #define PD_LOG_SIZE(size_port) ((size_port) & PD_LOG_SIZE_MASK)
3422
3423 /* PD event log : entry types */
3424 /* PD MCU events */
3425 #define PD_EVENT_MCU_BASE       0x00
3426 #define PD_EVENT_MCU_CHARGE             (PD_EVENT_MCU_BASE+0)
3427 #define PD_EVENT_MCU_CONNECT            (PD_EVENT_MCU_BASE+1)
3428 /* Reserved for custom board event */
3429 #define PD_EVENT_MCU_BOARD_CUSTOM       (PD_EVENT_MCU_BASE+2)
3430 /* PD generic accessory events */
3431 #define PD_EVENT_ACC_BASE       0x20
3432 #define PD_EVENT_ACC_RW_FAIL   (PD_EVENT_ACC_BASE+0)
3433 #define PD_EVENT_ACC_RW_ERASE  (PD_EVENT_ACC_BASE+1)
3434 /* PD power supply events */
3435 #define PD_EVENT_PS_BASE        0x40
3436 #define PD_EVENT_PS_FAULT      (PD_EVENT_PS_BASE+0)
3437 /* PD video dongles events */
3438 #define PD_EVENT_VIDEO_BASE     0x60
3439 #define PD_EVENT_VIDEO_DP_MODE (PD_EVENT_VIDEO_BASE+0)
3440 #define PD_EVENT_VIDEO_CODEC   (PD_EVENT_VIDEO_BASE+1)
3441 /* Returned in the "type" field, when there is no entry available */
3442 #define PD_EVENT_NO_ENTRY       0xff
3443
3444 /*
3445  * PD_EVENT_MCU_CHARGE event definition :
3446  * the payload is "struct usb_chg_measures"
3447  * the data field contains the port state flags as defined below :
3448  */
3449 /* Port partner is a dual role device */
3450 #define CHARGE_FLAGS_DUAL_ROLE         BIT(15)
3451 /* Port is the pending override port */
3452 #define CHARGE_FLAGS_DELAYED_OVERRIDE  BIT(14)
3453 /* Port is the override port */
3454 #define CHARGE_FLAGS_OVERRIDE          BIT(13)
3455 /* Charger type */
3456 #define CHARGE_FLAGS_TYPE_SHIFT               3
3457 #define CHARGE_FLAGS_TYPE_MASK       (0xf << CHARGE_FLAGS_TYPE_SHIFT)
3458 /* Power delivery role */
3459 #define CHARGE_FLAGS_ROLE_MASK         (7 <<  0)
3460
3461 /*
3462  * PD_EVENT_PS_FAULT data field flags definition :
3463  */
3464 #define PS_FAULT_OCP                          1
3465 #define PS_FAULT_FAST_OCP                     2
3466 #define PS_FAULT_OVP                          3
3467 #define PS_FAULT_DISCH                        4
3468
3469 /*
3470  * PD_EVENT_VIDEO_CODEC payload is "struct mcdp_info".
3471  */
3472 struct mcdp_version {
3473         uint8_t major;
3474         uint8_t minor;
3475         uint16_t build;
3476 } __packed;
3477
3478 struct mcdp_info {
3479         uint8_t family[2];
3480         uint8_t chipid[2];
3481         struct mcdp_version irom;
3482         struct mcdp_version fw;
3483 } __packed;
3484
3485 /* struct mcdp_info field decoding */
3486 #define MCDP_CHIPID(chipid) ((chipid[0] << 8) | chipid[1])
3487 #define MCDP_FAMILY(family) ((family[0] << 8) | family[1])
3488
3489 /* Get info about USB-C SS muxes */
3490 #define EC_CMD_USB_PD_MUX_INFO 0x11a
3491
3492 struct ec_params_usb_pd_mux_info {
3493         uint8_t port; /* USB-C port number */
3494 } __packed;
3495
3496 /* Flags representing mux state */
3497 #define USB_PD_MUX_USB_ENABLED       (1 << 0)
3498 #define USB_PD_MUX_DP_ENABLED        (1 << 1)
3499 #define USB_PD_MUX_POLARITY_INVERTED (1 << 2)
3500 #define USB_PD_MUX_HPD_IRQ           (1 << 3)
3501
3502 struct ec_response_usb_pd_mux_info {
3503         uint8_t flags; /* USB_PD_MUX_*-encoded USB mux state */
3504 } __packed;
3505
3506 /*****************************************************************************/
3507 /*
3508  * Passthru commands
3509  *
3510  * Some platforms have sub-processors chained to each other.  For example.
3511  *
3512  *     AP <--> EC <--> PD MCU
3513  *
3514  * The top 2 bits of the command number are used to indicate which device the
3515  * command is intended for.  Device 0 is always the device receiving the
3516  * command; other device mapping is board-specific.
3517  *
3518  * When a device receives a command to be passed to a sub-processor, it passes
3519  * it on with the device number set back to 0.  This allows the sub-processor
3520  * to remain blissfully unaware of whether the command originated on the next
3521  * device up the chain, or was passed through from the AP.
3522  *
3523  * In the above example, if the AP wants to send command 0x0002 to the PD MCU,
3524  *     AP sends command 0x4002 to the EC
3525  *     EC sends command 0x0002 to the PD MCU
3526  *     EC forwards PD MCU response back to the AP
3527  */
3528
3529 /* Offset and max command number for sub-device n */
3530 #define EC_CMD_PASSTHRU_OFFSET(n) (0x4000 * (n))
3531 #define EC_CMD_PASSTHRU_MAX(n) (EC_CMD_PASSTHRU_OFFSET(n) + 0x3fff)
3532
3533 /*****************************************************************************/
3534 /*
3535  * Deprecated constants. These constants have been renamed for clarity. The
3536  * meaning and size has not changed. Programs that use the old names should
3537  * switch to the new names soon, as the old names may not be carried forward
3538  * forever.
3539  */
3540 #define EC_HOST_PARAM_SIZE      EC_PROTO2_MAX_PARAM_SIZE
3541 #define EC_LPC_ADDR_OLD_PARAM   EC_HOST_CMD_REGION1
3542 #define EC_OLD_PARAM_SIZE       EC_HOST_CMD_REGION_SIZE
3543
3544 #endif  /* __CROS_EC_COMMANDS_H */