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