1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
4 #include <device/mmio.h>
5 #include <ec/acpi/ec.h>
14 #define ec_cmd send_ec_command
15 #define ec_dat send_ec_data
17 static void ec_fcmd(uint8_t fcmd
)
19 write8p(ECRAM
+ FCMD
, fcmd
);
22 /* EC sets FCMD = 0x00 on completion (FCMD = 0xfa on some commands) */
23 int time
= wait_us(50000, read8p(ECRAM
+ FCMD
) == 0x00 || read8p(ECRAM
+ FCMD
) == 0xfa);
25 printk(BIOS_DEBUG
, "EC: FCMD 0x%02x completed after %d us\n", fcmd
, time
);
27 printk(BIOS_ERR
, "EC: FCMD 0x%02x timed out\n", fcmd
);
30 static void ec_recv_str(char *buf
, size_t size
)
33 *buf
= recv_ec_data();
34 if (*buf
== '$') { /* end mark */
41 /* Truncate and discard the rest */
43 do {} while (recv_ec_data() != '$');
44 printk(BIOS_ERR
, "EC: Received string longer than buffer. Data truncated.\n");
47 char *ec_read_model(void)
49 static char model
[10];
51 ec_cmd(ECCMD_READ_MODEL
);
52 ec_recv_str(model
, sizeof(model
));
57 char *ec_read_fw_version(void)
59 static char version
[10] = "1.";
61 ec_cmd(ECCMD_READ_FW_VER
);
62 ec_recv_str(version
+ 2, sizeof(version
) - 2);
67 void ec_set_acpi_mode(bool state
)
69 ec_cmd(state
? ECCMD_ENABLE_ACPI_MODE
: ECCMD_DISABLE_ACPI_MODE
);
71 ec_cmd(ECCMD_ENABLE_HOTKEYS
);
74 void ec_set_enter_g3_in_s4s5(bool state
)
76 clrsetbits8p(ECRAM
+ 0x1e6, 1 << G3FG
, state
<< G3FG
);
79 void ec_set_aprd(void)
81 setbits8p(ECRAM
+ 0x1eb, 1 << APRD
);
84 /* To be called by a graphics driver, when detecting a dGPU */
85 void ec_set_dgpu_present(bool state
)
87 clrsetbits8p(ECRAM
+ 0x1eb, 1 << DGPT
, state
<< DGPT
);
90 void ec_set_fn_win_swap(bool state
)
92 clrsetbits8p(ECRAM
+ ECKS
, 1 << SWFN
, state
<< SWFN
);
95 void ec_set_ac_fan_always_on(bool state
)
97 clrsetbits8p(ECRAM
+ 0x1e6, 1 << FOAC
, state
<< FOAC
);
100 void ec_set_kbled_timeout(uint16_t timeout
)
102 printk(BIOS_DEBUG
, "EC: set keyboard backlight timeout to %us\n", timeout
);
104 write8p(ECRAM
+ FDAT
, timeout
? 0xff : 0x00);
105 write16p(ECRAM
+ FBUF
, swab16(timeout
));
106 ec_fcmd(FCMD_SET_KBLED_TIMEOUT
);
109 void ec_set_flexicharger(bool state
, uint8_t start
, uint8_t stop
)
111 printk(BIOS_DEBUG
, "EC: set flexicharger: enabled=%d, start=%u%%, stop=%u%%\n",
118 } else if (start
> 100 || stop
> 100) {
119 printk(BIOS_ERR
, "EC: invalid flexicharger settings: start/stop > 100%%\n");
122 } else if (start
>= stop
) {
123 printk(BIOS_ERR
, "EC: invalid flexicharger settings: start >= stop\n");
127 write8p(ECRAM
+ FBF1
, state
<< 1);
128 write8p(ECRAM
+ FBUF
, start
);
129 write8p(ECRAM
+ FDAT
, stop
);
130 ec_fcmd(FCMD_FLEXICHARGER
);
133 void ec_set_camera_boot_state(enum camera_state state
)
135 if (state
> CAMERA_STATE_KEEP
) {
137 "EC: invalid camera boot state %u. Keeping previous state.\n", state
);
138 state
= CAMERA_STATE_KEEP
;
141 if (state
== CAMERA_STATE_KEEP
) {
143 * The EC maintains the camera's state in RAM. However, it doesn't sync the GPIO
144 * on a concurrent boot. Thus, read the previous state from the EC and set the
145 * state and the GPIO by sending the state command even in the keep-case.
147 ec_cmd(ECCMD_GET_DEVICES_STATE
);
148 state
= recv_ec_data() & 1;
151 printk(BIOS_DEBUG
, "EC: set camera: enabled=%u\n", state
);
153 ec_dat(DEVICE_CAMERA
| DEVICE_STATE(state
));
154 ec_cmd(ECCMD_SET_INV_DEVICE_STATE
);
157 void ec_set_tp_toggle_mode(uint8_t mode
)
160 case 0: /* CtrlAltF9 */
161 setbits8p(ECRAM
+ RINF
, TP_TOGGLE_CTRLALTF9
);
163 case 1: /* KeycodeF7F8*/
164 clrbits8p(ECRAM
+ RINF
, TP_TOGGLE_CTRLALTF9
);