summaryrefslogtreecommitdiff
path: root/src/ec/clevo/it5570e/commands.c
diff options
context:
space:
mode:
authorMichael Niewöhner <foss@mniewoehner.de>2021-12-01 19:09:13 +0100
committerMichael Niewöhner <foss@mniewoehner.de>2022-11-11 22:42:46 +0000
commite1e65cb0f1cad6117d48503f2f78d115caecc55b (patch)
treeb6d42b3968fd2e6d57062031a48ac65123bb03ca /src/ec/clevo/it5570e/commands.c
parent064c6ced40a0acd7c4127414ee04c90b3a458751 (diff)
ec/clevo/it5570e: add driver for EC used on various Clevo laptops
This adds a driver for the ITE IT5570E EC in combination with Clevo vendor EC firmware. The interface is mostly identical on various laptop models. Thus, we have implemented one common driver to support them all. The following features were implemented: - Basics like battery, ac, etc. - Suspend/hibernate support: S0ix, S3*, S4/S5 - Save/restore of keyboard backlight level during S0ix without the need for Clevo vendor software (ControlCenter) - Flexicharger - Fn keys (backlight, volume, airplane etc.) - Various configuration options via Kconfig / CMOS options * Note: S3 support works at least on L140CU (Cometlake), but it's not enabled for this board because S0ix is used. Not implemented, yet: - Type-C UCSI: the EC firmware seems to be buggy (with vendor fw, too) - dGPU support is WIP An example of how this driver can be hooked up by a board can be seen in in change CB:59850, where support for the L140MU is added. Known issues: - Touchpad toggle: The touchpad toggle (Fn-F1) has two modes, Ctrl-Alt-F9 mode and keycodes 0xf7/0xf8 mode. Ctrl-Alt-F9 is the native touchpad toggle shortcut on Windows. On Linux this would switch to virtual console 9, if enabled. Thus, one should use the keycodes mode and add udev rules as specified in [1]. If VT9 is disabled, Ctrl-Alt-F9 mode could be used to set up a keyboard shortcut command toggling the touchpad. - Multi-fan systems The Clevo NV41MZ (w/o dGPU) has two fans that should be in-sync. However, the second fan does not spin. This needs further investigation. [1] https://docs.dasharo.com/variants/clevo_nv41/post_install/ Testing the various functionalities of this EC driver was done in the changes hooking up this driver for the boards. Change-Id: Ic8c0bee9002ad9edcd10c83b775fc723744caaa0 Co-authored-by: Michał Kopeć <michal.kopec@3mdeb.com> Co-authored-by: Michał Żygowski <michal.zygowski@3mdeb.com> Co-authored-by: Michael Niewöhner <foss@mniewoehner.de> Signed-off-by: Michał Kopeć <michal.kopec@3mdeb.com> Signed-off-by: Michał Żygowski <michal.zygowski@3mdeb.com> Signed-off-by: Michael Niewöhner <foss@mniewoehner.de> Reviewed-on: https://review.coreboot.org/c/coreboot/+/68791 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Diffstat (limited to 'src/ec/clevo/it5570e/commands.c')
-rw-r--r--src/ec/clevo/it5570e/commands.c168
1 files changed, 168 insertions, 0 deletions
diff --git a/src/ec/clevo/it5570e/commands.c b/src/ec/clevo/it5570e/commands.c
new file mode 100644
index 0000000000..7a85b8f769
--- /dev/null
+++ b/src/ec/clevo/it5570e/commands.c
@@ -0,0 +1,168 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <console/console.h>
+#include <delay.h>
+#include <device/mmio.h>
+#include <ec/acpi/ec.h>
+#include <swab.h>
+#include <timer.h>
+#include <types.h>
+
+#include "chip.h"
+#include "commands.h"
+#include "ec.h"
+
+#define ec_cmd send_ec_command
+#define ec_dat send_ec_data
+
+static void ec_fcmd(uint8_t fcmd)
+{
+ write8p(ECRAM + FCMD, fcmd);
+ ec_cmd(ECCMD_NOP);
+
+ /* EC sets FCMD = 0x00 on completion (FCMD = 0xfa on some commands) */
+ int time = wait_us(50000, read8p(ECRAM + FCMD) == 0x00 || read8p(ECRAM + FCMD) == 0xfa);
+ if (time)
+ printk(BIOS_DEBUG, "EC: FCMD 0x%02x completed after %d us\n", fcmd, time);
+ else
+ printk(BIOS_ERR, "EC: FCMD 0x%02x timed out\n", fcmd);
+}
+
+static void ec_recv_str(char *buf, size_t size)
+{
+ while (size--) {
+ *buf = recv_ec_data();
+ if (*buf == '$') { /* end mark */
+ *buf = '\0';
+ return;
+ }
+ buf++;
+ }
+
+ /* Truncate and discard the rest */
+ *--buf = '\0';
+ do {} while (recv_ec_data() != '$');
+ printk(BIOS_ERR, "EC: Received string longer than buffer. Data truncated.\n");
+}
+
+char *ec_read_model(void)
+{
+ static char model[10];
+
+ ec_cmd(ECCMD_READ_MODEL);
+ ec_recv_str(model, sizeof(model));
+
+ return model;
+}
+
+char *ec_read_fw_version(void)
+{
+ static char version[10] = "1.";
+
+ ec_cmd(ECCMD_READ_FW_VER);
+ ec_recv_str(version + 2, sizeof(version) - 2);
+
+ return version;
+}
+
+void ec_set_acpi_mode(bool state)
+{
+ ec_cmd(state ? ECCMD_ENABLE_ACPI_MODE : ECCMD_DISABLE_ACPI_MODE);
+ if (state)
+ ec_cmd(ECCMD_ENABLE_HOTKEYS);
+}
+
+void ec_set_enter_g3_in_s4s5(bool state)
+{
+ clrsetbits8p(ECRAM + 0x1e6, 1 << G3FG, state << G3FG);
+}
+
+void ec_set_aprd(void)
+{
+ setbits8p(ECRAM + 0x1eb, 1 << APRD);
+}
+
+/* To be called by a graphics driver, when detecting a dGPU */
+void ec_set_dgpu_present(bool state)
+{
+ clrsetbits8p(ECRAM + 0x1eb, 1 << DGPT, state << DGPT);
+}
+
+void ec_set_fn_win_swap(bool state)
+{
+ clrsetbits8p(ECRAM + ECKS, 1 << SWFN, state << SWFN);
+}
+
+void ec_set_ac_fan_always_on(bool state)
+{
+ clrsetbits8p(ECRAM + 0x1e6, 1 << FOAC, state << FOAC);
+}
+
+void ec_set_kbled_timeout(uint16_t timeout)
+{
+ printk(BIOS_DEBUG, "EC: set keyboard backlight timeout to %us\n", timeout);
+
+ write8p(ECRAM + FDAT, timeout ? 0xff : 0x00);
+ write16p(ECRAM + FBUF, swab16(timeout));
+ ec_fcmd(FCMD_SET_KBLED_TIMEOUT);
+}
+
+void ec_set_flexicharger(bool state, uint8_t start, uint8_t stop)
+{
+ printk(BIOS_DEBUG, "EC: set flexicharger: enabled=%d, start=%u%%, stop=%u%%\n",
+ state, start, stop);
+
+ if (!state) {
+ start = 0xff;
+ stop = 0xff;
+
+ } else if (start > 100 || stop > 100) {
+ printk(BIOS_ERR, "EC: invalid flexicharger settings: start/stop > 100%%\n");
+ return;
+
+ } else if (start >= stop) {
+ printk(BIOS_ERR, "EC: invalid flexicharger settings: start >= stop\n");
+ return;
+ }
+
+ write8p(ECRAM + FBF1, state << 1);
+ write8p(ECRAM + FBUF, start);
+ write8p(ECRAM + FDAT, stop);
+ ec_fcmd(FCMD_FLEXICHARGER);
+}
+
+void ec_set_camera_boot_state(enum camera_state state)
+{
+ if (state > CAMERA_STATE_KEEP) {
+ printk(BIOS_ERR,
+ "EC: invalid camera boot state %u. Keeping previous state.\n", state);
+ state = CAMERA_STATE_KEEP;
+ }
+
+ if (state == CAMERA_STATE_KEEP) {
+ /*
+ * The EC maintains the camera's state in RAM. However, it doesn't sync the GPIO
+ * on a concurrent boot. Thus, read the previous state from the EC and set the
+ * state and the GPIO by sending the state command even in the keep-case.
+ */
+ ec_cmd(ECCMD_GET_DEVICES_STATE);
+ state = recv_ec_data() & 1;
+ }
+
+ printk(BIOS_DEBUG, "EC: set camera: enabled=%u\n", state);
+
+ ec_dat(DEVICE_CAMERA | DEVICE_STATE(state));
+ ec_cmd(ECCMD_SET_INV_DEVICE_STATE);
+}
+
+void ec_set_tp_toggle_mode(uint8_t mode)
+{
+ switch (mode) {
+ case 0: /* CtrlAltF9 */
+ setbits8p(ECRAM + RINF, TP_TOGGLE_CTRLALTF9);
+ break;
+ case 1: /* KeycodeF7F8*/
+ clrbits8p(ECRAM + RINF, TP_TOGGLE_CTRLALTF9);
+ break;
+ }
+}