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