summaryrefslogtreecommitdiff
path: root/zephyr/projects/corsola/src/krabby
diff options
context:
space:
mode:
Diffstat (limited to 'zephyr/projects/corsola/src/krabby')
-rw-r--r--zephyr/projects/corsola/src/krabby/charger_workaround.c93
-rw-r--r--zephyr/projects/corsola/src/krabby/hooks.c90
-rw-r--r--zephyr/projects/corsola/src/krabby/i2c.c19
-rw-r--r--zephyr/projects/corsola/src/krabby/keyboard_magikarp.c29
-rw-r--r--zephyr/projects/corsola/src/krabby/ppc_krabby.c31
-rw-r--r--zephyr/projects/corsola/src/krabby/ppc_magikarp.c44
-rw-r--r--zephyr/projects/corsola/src/krabby/ppc_tentacruel.c89
-rw-r--r--zephyr/projects/corsola/src/krabby/sensor_magikarp.c41
-rw-r--r--zephyr/projects/corsola/src/krabby/sensor_tentacruel.c41
-rw-r--r--zephyr/projects/corsola/src/krabby/temp_tentacruel.c129
-rw-r--r--zephyr/projects/corsola/src/krabby/usb_pd_policy.c88
-rw-r--r--zephyr/projects/corsola/src/krabby/usbc_config.c141
12 files changed, 835 insertions, 0 deletions
diff --git a/zephyr/projects/corsola/src/krabby/charger_workaround.c b/zephyr/projects/corsola/src/krabby/charger_workaround.c
new file mode 100644
index 0000000000..d7fd05cc00
--- /dev/null
+++ b/zephyr/projects/corsola/src/krabby/charger_workaround.c
@@ -0,0 +1,93 @@
+/* Copyright 2022 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <zephyr/sys/util.h>
+
+#include "charger.h"
+#include "driver/charger/rt9490.h"
+#include "hooks.h"
+#include "i2c.h"
+#include "system.h"
+
+/*
+ * This workaround and the board id checks only apply to krabby and early
+ * tentacruel devices.
+ * Newer project should have all of these fixed.
+ */
+BUILD_ASSERT(IS_ENABLED(CONFIG_BOARD_KRABBY) ||
+ IS_ENABLED(CONFIG_BOARD_TENTACRUEL) || IS_ENABLED(CONFIG_TEST));
+
+/* b/194967754#comment5: work around for IBUS ADC unstable issue */
+static void ibus_adc_workaround(void)
+{
+ if (system_get_board_version() != 0) {
+ return;
+ }
+
+ i2c_update8(chg_chips[CHARGER_SOLO].i2c_port,
+ chg_chips[CHARGER_SOLO].i2c_addr_flags,
+ RT9490_REG_ADC_CHANNEL0, RT9490_VSYS_ADC_DIS, MASK_SET);
+
+ rt9490_enable_hidden_mode(CHARGER_SOLO, true);
+ /* undocumented registers... */
+ i2c_write8(chg_chips[CHARGER_SOLO].i2c_port,
+ chg_chips[CHARGER_SOLO].i2c_addr_flags, 0x52, 0xC4);
+
+ i2c_update8(chg_chips[CHARGER_SOLO].i2c_port,
+ chg_chips[CHARGER_SOLO].i2c_addr_flags,
+ RT9490_REG_ADC_CHANNEL0, RT9490_VSYS_ADC_DIS, MASK_CLR);
+ rt9490_enable_hidden_mode(CHARGER_SOLO, false);
+}
+
+/* b/214880220#comment44: lock i2c at 400khz */
+static void i2c_speed_workaround(void)
+{
+ if (system_get_board_version() >= 3) {
+ return;
+ }
+
+ rt9490_enable_hidden_mode(CHARGER_SOLO, true);
+ /* Set to Auto mode, default run at 400kHz */
+ i2c_write8(chg_chips[CHARGER_SOLO].i2c_port,
+ chg_chips[CHARGER_SOLO].i2c_addr_flags, 0x71, 0x22);
+ /* Manually select for 400kHz, valid only when 0x71[7] == 1 */
+ i2c_write8(chg_chips[CHARGER_SOLO].i2c_port,
+ chg_chips[CHARGER_SOLO].i2c_addr_flags, 0xF7, 0x14);
+ rt9490_enable_hidden_mode(CHARGER_SOLO, false);
+}
+
+static void eoc_deglitch_workaround(void)
+{
+ if (system_get_board_version() != 1) {
+ return;
+ }
+
+ /* set end-of-charge deglitch time to 2ms */
+ i2c_update8(chg_chips[CHARGER_SOLO].i2c_port,
+ chg_chips[CHARGER_SOLO].i2c_addr_flags,
+ RT9490_REG_ADD_CTRL0, RT9490_TD_EOC, MASK_CLR);
+}
+
+static void disable_safety_timer(void)
+{
+ if (system_get_board_version() >= 2) {
+ return;
+ }
+ /* Disable charge timer */
+ i2c_write8(chg_chips[CHARGER_SOLO].i2c_port,
+ chg_chips[CHARGER_SOLO].i2c_addr_flags,
+ RT9490_REG_SAFETY_TMR_CTRL,
+ RT9490_EN_TRICHG_TMR | RT9490_EN_PRECHG_TMR |
+ RT9490_EN_FASTCHG_TMR);
+}
+
+static void board_rt9490_workaround(void)
+{
+ ibus_adc_workaround();
+ i2c_speed_workaround();
+ eoc_deglitch_workaround();
+ disable_safety_timer();
+}
+DECLARE_HOOK(HOOK_INIT, board_rt9490_workaround, HOOK_PRIO_DEFAULT);
diff --git a/zephyr/projects/corsola/src/krabby/hooks.c b/zephyr/projects/corsola/src/krabby/hooks.c
new file mode 100644
index 0000000000..1eb4f600f2
--- /dev/null
+++ b/zephyr/projects/corsola/src/krabby/hooks.c
@@ -0,0 +1,90 @@
+/* Copyright 2021 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <zephyr/init.h>
+#include <zephyr/drivers/gpio.h>
+#include <zephyr/drivers/pinctrl.h>
+
+#include <ap_power/ap_power.h>
+#include "charger.h"
+#include "driver/charger/rt9490.h"
+#include "extpower.h"
+#include "gpio.h"
+#include "hooks.h"
+
+#define I2C3_NODE DT_NODELABEL(i2c3)
+PINCTRL_DT_DEFINE(I2C3_NODE);
+
+static void board_i2c3_ctrl(bool enable)
+{
+ if (DEVICE_DT_GET(
+ DT_GPIO_CTLR_BY_IDX(DT_NODELABEL(i2c3), scl_gpios, 0)) ==
+ DEVICE_DT_GET(DT_NODELABEL(gpiof))) {
+ const struct pinctrl_dev_config *pcfg =
+ PINCTRL_DT_DEV_CONFIG_GET(I2C3_NODE);
+
+ if (enable) {
+ pinctrl_apply_state(pcfg, PINCTRL_STATE_DEFAULT);
+ } else {
+ pinctrl_apply_state(pcfg, PINCTRL_STATE_SLEEP);
+ }
+ }
+}
+
+static void board_enable_i2c3(void)
+{
+ board_i2c3_ctrl(1);
+}
+DECLARE_HOOK(HOOK_CHIPSET_PRE_INIT, board_enable_i2c3, HOOK_PRIO_FIRST);
+
+static void board_disable_i2c3(void)
+{
+ board_i2c3_ctrl(0);
+}
+DECLARE_HOOK(HOOK_CHIPSET_HARD_OFF, board_disable_i2c3, HOOK_PRIO_LAST);
+
+static void board_suspend_handler(struct ap_power_ev_callback *cb,
+ struct ap_power_ev_data data)
+{
+ int value;
+
+ switch (data.event) {
+ default:
+ return;
+
+ case AP_POWER_RESUME:
+ value = 1;
+ break;
+
+ case AP_POWER_SUSPEND:
+ value = 0;
+ break;
+ }
+ gpio_pin_set_dt(GPIO_DT_FROM_NODELABEL(gpio_en_5v_usm), value);
+}
+
+static int install_suspend_handler(const struct device *unused)
+{
+ static struct ap_power_ev_callback cb;
+
+ /*
+ * Add a callback for suspend/resume.
+ */
+ ap_power_ev_init_callback(&cb, board_suspend_handler,
+ AP_POWER_RESUME | AP_POWER_SUSPEND);
+ ap_power_ev_add_callback(&cb);
+ return 0;
+}
+
+SYS_INIT(install_suspend_handler, APPLICATION, 1);
+
+static void board_hook_ac_change(void)
+{
+ if (system_get_board_version() >= 1) {
+ rt9490_enable_adc(CHARGER_SOLO, extpower_is_present());
+ }
+}
+DECLARE_HOOK(HOOK_AC_CHANGE, board_hook_ac_change, HOOK_PRIO_DEFAULT);
+DECLARE_HOOK(HOOK_INIT, board_hook_ac_change, HOOK_PRIO_LAST);
diff --git a/zephyr/projects/corsola/src/krabby/i2c.c b/zephyr/projects/corsola/src/krabby/i2c.c
new file mode 100644
index 0000000000..a83af77dbd
--- /dev/null
+++ b/zephyr/projects/corsola/src/krabby/i2c.c
@@ -0,0 +1,19 @@
+/* Copyright 2021 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "i2c/i2c.h"
+#include "i2c.h"
+
+/* Krabby board specific i2c implementation */
+
+#ifdef CONFIG_PLATFORM_EC_I2C_PASSTHRU_RESTRICTED
+int board_allow_i2c_passthru(const struct i2c_cmd_desc_t *cmd_desc)
+{
+ return (i2c_get_device_for_port(cmd_desc->port) ==
+ i2c_get_device_for_port(I2C_PORT_VIRTUAL_BATTERY) ||
+ i2c_get_device_for_port(cmd_desc->port) ==
+ i2c_get_device_for_port(I2C_PORT_EEPROM));
+}
+#endif
diff --git a/zephyr/projects/corsola/src/krabby/keyboard_magikarp.c b/zephyr/projects/corsola/src/krabby/keyboard_magikarp.c
new file mode 100644
index 0000000000..bcb706bba3
--- /dev/null
+++ b/zephyr/projects/corsola/src/krabby/keyboard_magikarp.c
@@ -0,0 +1,29 @@
+/* Copyright 2022 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "ec_commands.h"
+
+static const struct ec_response_keybd_config magikarp_kb_legacy = {
+ .num_top_row_keys = 10,
+ .action_keys = {
+ TK_BACK, /* T1 */
+ TK_REFRESH, /* T2 */
+ TK_FULLSCREEN, /* T3 */
+ TK_OVERVIEW, /* T4 */
+ TK_SNAPSHOT, /* T5 */
+ TK_BRIGHTNESS_DOWN, /* T6 */
+ TK_BRIGHTNESS_UP, /* T7 */
+ TK_VOL_MUTE, /* T8 */
+ TK_VOL_DOWN, /* T9 */
+ TK_VOL_UP, /* T10 */
+ },
+ .capabilities = KEYBD_CAP_SCRNLOCK_KEY,
+};
+
+__override const struct ec_response_keybd_config *
+board_vivaldi_keybd_config(void)
+{
+ return &magikarp_kb_legacy;
+}
diff --git a/zephyr/projects/corsola/src/krabby/ppc_krabby.c b/zephyr/projects/corsola/src/krabby/ppc_krabby.c
new file mode 100644
index 0000000000..d4f574a725
--- /dev/null
+++ b/zephyr/projects/corsola/src/krabby/ppc_krabby.c
@@ -0,0 +1,31 @@
+/* Copyright 2022 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/* Krabby PPC/BC12 (RT1739) configuration */
+
+#include "baseboard_usbc_config.h"
+#include "gpio/gpio_int.h"
+#include "driver/ppc/rt1739.h"
+#include "driver/ppc/syv682x.h"
+#include "hooks.h"
+#include "variant_db_detection.h"
+
+void c0_bc12_interrupt(enum gpio_signal signal)
+{
+ rt1739_interrupt(0);
+}
+
+static void board_usbc_init(void)
+{
+ gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_usb_c0_ppc_bc12));
+}
+DECLARE_HOOK(HOOK_INIT, board_usbc_init, HOOK_PRIO_POST_DEFAULT);
+
+void ppc_interrupt(enum gpio_signal signal)
+{
+ if (signal == GPIO_SIGNAL(DT_ALIAS(gpio_usb_c1_ppc_int_odl))) {
+ syv682x_interrupt(1);
+ }
+}
diff --git a/zephyr/projects/corsola/src/krabby/ppc_magikarp.c b/zephyr/projects/corsola/src/krabby/ppc_magikarp.c
new file mode 100644
index 0000000000..41cce3f73d
--- /dev/null
+++ b/zephyr/projects/corsola/src/krabby/ppc_magikarp.c
@@ -0,0 +1,44 @@
+/* Copyright 2022 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/* Tentacruel PPC/BC12 (mixed RT1739 or PI3USB9201+SYV682X) configuration */
+
+#include "baseboard_usbc_config.h"
+#include "console.h"
+#include "cros_board_info.h"
+#include "gpio/gpio_int.h"
+#include "hooks.h"
+#include "usbc/ppc.h"
+#include "variant_db_detection.h"
+
+#include <zephyr/logging/log.h>
+
+#define CPRINTSUSB(format, args...) cprints(CC_USBCHARGE, format, ##args)
+#define CPRINTS(format, args...) cprints(CC_SYSTEM, format, ##args)
+#define CPRINTF(format, args...) cprintf(CC_SYSTEM, format, ##args)
+
+void bc12_interrupt(enum gpio_signal signal)
+{
+ usb_charger_task_set_event(0, USB_CHG_EVENT_BC12);
+}
+
+static void board_usbc_init(void)
+{
+ /* Enable PPC interrupts. */
+ gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_usb_c0_ppc));
+
+ /* Enable BC1.2 interrupts. */
+ gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_usb_c0_bc12));
+}
+DECLARE_HOOK(HOOK_INIT, board_usbc_init, HOOK_PRIO_POST_DEFAULT);
+
+void ppc_interrupt(enum gpio_signal signal)
+{
+ if (signal == GPIO_SIGNAL(DT_NODELABEL(usb_c0_ppc_int_odl))) {
+ syv682x_interrupt(0);
+ } else if (signal == GPIO_SIGNAL(DT_ALIAS(gpio_usb_c1_ppc_int_odl))) {
+ syv682x_interrupt(1);
+ }
+}
diff --git a/zephyr/projects/corsola/src/krabby/ppc_tentacruel.c b/zephyr/projects/corsola/src/krabby/ppc_tentacruel.c
new file mode 100644
index 0000000000..877b9940b4
--- /dev/null
+++ b/zephyr/projects/corsola/src/krabby/ppc_tentacruel.c
@@ -0,0 +1,89 @@
+/* Copyright 2022 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/* Tentacruel PPC/BC12 (mixed RT1739 or PI3USB9201+SYV682X) configuration */
+
+#include "baseboard_usbc_config.h"
+#include "console.h"
+#include "cros_board_info.h"
+#include "driver/usb_mux/ps8743.h"
+#include "gpio/gpio_int.h"
+#include "hooks.h"
+#include "usb_mux.h"
+#include "usbc/ppc.h"
+#include "variant_db_detection.h"
+
+#include <zephyr/logging/log.h>
+
+#define CPRINTSUSB(format, args...) cprints(CC_USBCHARGE, format, ##args)
+#define CPRINTS(format, args...) cprints(CC_SYSTEM, format, ##args)
+#define CPRINTF(format, args...) cprintf(CC_SYSTEM, format, ##args)
+
+LOG_MODULE_REGISTER(alt_dev_replacement);
+
+#define BOARD_VERSION_UNKNOWN 0xffffffff
+
+/* Check board version to decide which ppc/bc12 is used. */
+static bool board_has_syv_ppc(void)
+{
+ static uint32_t board_version = BOARD_VERSION_UNKNOWN;
+
+ if (board_version == BOARD_VERSION_UNKNOWN) {
+ if (cbi_get_board_version(&board_version) != EC_SUCCESS) {
+ LOG_ERR("Failed to get board version.");
+ board_version = 0;
+ }
+ }
+
+ return (board_version >= 3);
+}
+
+static void check_alternate_devices(void)
+{
+ /* Configure the PPC driver */
+ if (board_has_syv_ppc())
+ /* Arg is the USB port number */
+ PPC_ENABLE_ALTERNATE(0);
+}
+DECLARE_HOOK(HOOK_INIT, check_alternate_devices, HOOK_PRIO_DEFAULT);
+
+void bc12_interrupt(enum gpio_signal signal)
+{
+ usb_charger_task_set_event(0, USB_CHG_EVENT_BC12);
+}
+
+/* USB Mux C1 : board_init of PS8743 */
+int ps8743_eq_c1_setting(void)
+{
+ ps8743_write(usb_muxes[1].mux, PS8743_REG_USB_EQ_RX, 0x90);
+ return EC_SUCCESS;
+}
+
+static void board_usbc_init(void)
+{
+ if (board_has_syv_ppc()) {
+ /* Enable PPC interrupts. */
+ gpio_enable_dt_interrupt(
+ GPIO_INT_FROM_NODELABEL(int_usb_c0_ppc));
+
+ /* Enable BC1.2 interrupts. */
+ gpio_enable_dt_interrupt(
+ GPIO_INT_FROM_NODELABEL(int_usb_c0_bc12));
+ } else {
+ gpio_enable_dt_interrupt(
+ GPIO_INT_FROM_NODELABEL(int_usb_c0_ppc));
+ }
+}
+DECLARE_HOOK(HOOK_INIT, board_usbc_init, HOOK_PRIO_POST_DEFAULT);
+
+void ppc_interrupt(enum gpio_signal signal)
+{
+ if (signal == GPIO_SIGNAL(DT_NODELABEL(usb_c0_ppc_int_odl))) {
+ ppc_chips[0].drv->interrupt(0);
+ }
+ if (signal == GPIO_SIGNAL(DT_ALIAS(gpio_usb_c1_ppc_int_odl))) {
+ ppc_chips[1].drv->interrupt(1);
+ }
+}
diff --git a/zephyr/projects/corsola/src/krabby/sensor_magikarp.c b/zephyr/projects/corsola/src/krabby/sensor_magikarp.c
new file mode 100644
index 0000000000..269bc26fae
--- /dev/null
+++ b/zephyr/projects/corsola/src/krabby/sensor_magikarp.c
@@ -0,0 +1,41 @@
+/* Copyright 2022 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "common.h"
+#include "accelgyro.h"
+#include "cros_cbi.h"
+#include "driver/accelgyro_bmi323.h"
+#include "driver/accelgyro_icm42607.h"
+#include "hooks.h"
+#include "motionsense_sensors.h"
+
+void motion_interrupt(enum gpio_signal signal)
+{
+ uint32_t val;
+
+ cros_cbi_get_fw_config(FW_BASE_GYRO, &val);
+ if (val == FW_BASE_ICM42607) {
+ icm42607_interrupt(signal);
+ } else if (val == FW_BASE_BMI323) {
+ bmi3xx_interrupt(signal);
+ }
+}
+
+static void motionsense_init(void)
+{
+ uint32_t val;
+
+ cros_cbi_get_fw_config(FW_BASE_GYRO, &val);
+ if (val == FW_BASE_ICM42607) {
+ ccprints("BASE ACCEL is ICM42607");
+ } else if (val == FW_BASE_BMI323) {
+ MOTIONSENSE_ENABLE_ALTERNATE(alt_base_accel);
+ MOTIONSENSE_ENABLE_ALTERNATE(alt_base_gyro);
+ ccprints("BASE ACCEL IS BMI323");
+ } else {
+ ccprints("no motionsense");
+ }
+}
+DECLARE_HOOK(HOOK_INIT, motionsense_init, HOOK_PRIO_DEFAULT);
diff --git a/zephyr/projects/corsola/src/krabby/sensor_tentacruel.c b/zephyr/projects/corsola/src/krabby/sensor_tentacruel.c
new file mode 100644
index 0000000000..269bc26fae
--- /dev/null
+++ b/zephyr/projects/corsola/src/krabby/sensor_tentacruel.c
@@ -0,0 +1,41 @@
+/* Copyright 2022 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "common.h"
+#include "accelgyro.h"
+#include "cros_cbi.h"
+#include "driver/accelgyro_bmi323.h"
+#include "driver/accelgyro_icm42607.h"
+#include "hooks.h"
+#include "motionsense_sensors.h"
+
+void motion_interrupt(enum gpio_signal signal)
+{
+ uint32_t val;
+
+ cros_cbi_get_fw_config(FW_BASE_GYRO, &val);
+ if (val == FW_BASE_ICM42607) {
+ icm42607_interrupt(signal);
+ } else if (val == FW_BASE_BMI323) {
+ bmi3xx_interrupt(signal);
+ }
+}
+
+static void motionsense_init(void)
+{
+ uint32_t val;
+
+ cros_cbi_get_fw_config(FW_BASE_GYRO, &val);
+ if (val == FW_BASE_ICM42607) {
+ ccprints("BASE ACCEL is ICM42607");
+ } else if (val == FW_BASE_BMI323) {
+ MOTIONSENSE_ENABLE_ALTERNATE(alt_base_accel);
+ MOTIONSENSE_ENABLE_ALTERNATE(alt_base_gyro);
+ ccprints("BASE ACCEL IS BMI323");
+ } else {
+ ccprints("no motionsense");
+ }
+}
+DECLARE_HOOK(HOOK_INIT, motionsense_init, HOOK_PRIO_DEFAULT);
diff --git a/zephyr/projects/corsola/src/krabby/temp_tentacruel.c b/zephyr/projects/corsola/src/krabby/temp_tentacruel.c
new file mode 100644
index 0000000000..59c5a989aa
--- /dev/null
+++ b/zephyr/projects/corsola/src/krabby/temp_tentacruel.c
@@ -0,0 +1,129 @@
+/* Copyright 2022 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "charger.h"
+#include "charge_state.h"
+#include "common.h"
+#include "config.h"
+#include "console.h"
+#include "driver/charger/rt9490.h"
+#include "hooks.h"
+#include "temp_sensor/temp_sensor.h"
+#include "thermal.h"
+#include "util.h"
+
+#define NUM_CURRENT_LEVELS ARRAY_SIZE(current_table)
+#define TEMP_THRESHOLD 55
+#define TEMP_BUFF_SIZE 60
+#define KEEP_TIME 5
+
+BUILD_ASSERT(IS_ENABLED(CONFIG_BOARD_TENTACRUEL) || IS_ENABLED(CONFIG_TEST));
+/* calculate current average temperature */
+static int average_tempature(void)
+{
+ static int temp_history_buffer[TEMP_BUFF_SIZE];
+ static int buff_ptr;
+ static int temp_sum;
+ static int past_temp;
+ static int avg_temp;
+ int cur_temp, t;
+
+ temp_sensor_read(TEMP_SENSOR_ID(DT_NODELABEL(temp_charger)), &t);
+ cur_temp = K_TO_C(t);
+ past_temp = temp_history_buffer[buff_ptr];
+ temp_history_buffer[buff_ptr] = cur_temp;
+ temp_sum = temp_sum + temp_history_buffer[buff_ptr] - past_temp;
+ buff_ptr++;
+ if (buff_ptr >= TEMP_BUFF_SIZE) {
+ buff_ptr = 0;
+ }
+ /* Calculate per minute temperature.
+ * It's expected low temperature when the first 60 seconds.
+ */
+ avg_temp = temp_sum / TEMP_BUFF_SIZE;
+ return avg_temp;
+}
+
+static int current_level;
+
+/* Limit charging current table : 3600/3000/2400/1800
+ * note this should be in descending order.
+ */
+static uint16_t current_table[] = {
+ 3600,
+ 3000,
+ 2400,
+ 1800,
+};
+
+/* Called by hook task every hook second (1 sec) */
+static void current_update(void)
+{
+ int temp;
+ static uint8_t uptime;
+ static uint8_t dntime;
+
+ temp = average_tempature();
+#ifndef CONFIG_TEST
+ if (charge_get_state() == PWR_STATE_DISCHARGE) {
+ current_level = 0;
+ uptime = 0;
+ dntime = 0;
+ return;
+ }
+#endif
+ if (temp >= TEMP_THRESHOLD) {
+ dntime = 0;
+ if (uptime < KEEP_TIME) {
+ uptime++;
+ } else {
+ uptime = 0;
+ current_level++;
+ }
+ } else if (current_level != 0 && temp < TEMP_THRESHOLD) {
+ uptime = 0;
+ if (dntime < KEEP_TIME) {
+ dntime++;
+ } else {
+ dntime = 0;
+ current_level--;
+ }
+ } else {
+ uptime = 0;
+ dntime = 0;
+ }
+ if (current_level > NUM_CURRENT_LEVELS) {
+ current_level = NUM_CURRENT_LEVELS;
+ }
+}
+DECLARE_HOOK(HOOK_SECOND, current_update, HOOK_PRIO_DEFAULT);
+
+int charger_profile_override(struct charge_state_data *curr)
+{
+ /*
+ * Precharge must be executed when communication is failed on
+ * dead battery.
+ */
+ if (!(curr->batt.flags & BATT_FLAG_RESPONSIVE))
+ return 0;
+ if (current_level != 0) {
+ if (curr->requested_current > current_table[current_level - 1])
+ curr->requested_current =
+ current_table[current_level - 1];
+ }
+ return 0;
+}
+
+enum ec_status charger_profile_override_get_param(uint32_t param,
+ uint32_t *value)
+{
+ return EC_RES_INVALID_PARAM;
+}
+
+enum ec_status charger_profile_override_set_param(uint32_t param,
+ uint32_t value)
+{
+ return EC_RES_INVALID_PARAM;
+}
diff --git a/zephyr/projects/corsola/src/krabby/usb_pd_policy.c b/zephyr/projects/corsola/src/krabby/usb_pd_policy.c
new file mode 100644
index 0000000000..8f2a2c3515
--- /dev/null
+++ b/zephyr/projects/corsola/src/krabby/usb_pd_policy.c
@@ -0,0 +1,88 @@
+/* Copyright 2021 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "adc.h"
+#include "charge_manager.h"
+#include "chipset.h"
+#include "usb_charge.h"
+#include "usb_pd.h"
+#include "usbc_ppc.h"
+
+int pd_snk_is_vbus_provided(int port)
+{
+ static atomic_t vbus_prev[CONFIG_USB_PD_PORT_MAX_COUNT];
+ int vbus;
+
+ /*
+ * (b:181203590#comment20) TODO(yllin): use
+ * PD_VSINK_DISCONNECT_PD for non-5V case.
+ */
+ vbus = adc_read_channel(board_get_vbus_adc(port)) >=
+ PD_V_SINK_DISCONNECT_MAX;
+
+#ifdef CONFIG_USB_CHARGER
+ /*
+ * There's no PPC to inform VBUS change for usb_charger, so inform
+ * the usb_charger now.
+ */
+ if (!!(vbus_prev[port] != vbus)) {
+ usb_charger_vbus_change(port, vbus);
+ }
+
+ if (vbus) {
+ atomic_or(&vbus_prev[port], 1);
+ } else {
+ atomic_clear(&vbus_prev[port]);
+ }
+#endif
+ return vbus;
+}
+
+void pd_power_supply_reset(int port)
+{
+ int prev_en;
+
+ prev_en = ppc_is_sourcing_vbus(port);
+
+ /* Disable VBUS. */
+ ppc_vbus_source_enable(port, 0);
+
+ /* Enable discharge if we were previously sourcing 5V */
+ if (prev_en) {
+ pd_set_vbus_discharge(port, 1);
+ }
+
+ /* Notify host of power info change. */
+ pd_send_host_event(PD_EVENT_POWER_CHANGE);
+}
+
+int pd_set_power_supply_ready(int port)
+{
+ int rv;
+
+ /* Disable charging. */
+ rv = ppc_vbus_sink_enable(port, 0);
+ if (rv) {
+ return rv;
+ }
+
+ pd_set_vbus_discharge(port, 0);
+
+ /* Provide Vbus. */
+ rv = ppc_vbus_source_enable(port, 1);
+ if (rv) {
+ return rv;
+ }
+
+ /* Notify host of power info change. */
+ pd_send_host_event(PD_EVENT_POWER_CHANGE);
+
+ return EC_SUCCESS;
+}
+
+int board_vbus_source_enabled(int port)
+{
+ return ppc_is_sourcing_vbus(port);
+}
diff --git a/zephyr/projects/corsola/src/krabby/usbc_config.c b/zephyr/projects/corsola/src/krabby/usbc_config.c
new file mode 100644
index 0000000000..7a7f710804
--- /dev/null
+++ b/zephyr/projects/corsola/src/krabby/usbc_config.c
@@ -0,0 +1,141 @@
+/* Copyright 2021 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/* Krabby board-specific USB-C configuration */
+
+#include "adc.h"
+#include "baseboard_usbc_config.h"
+#include "charge_manager.h"
+#include "console.h"
+#include "driver/tcpm/it83xx_pd.h"
+#include "driver/usb_mux/tusb1064.h"
+#include "i2c.h"
+#include "usb_pd.h"
+#include "usbc_ppc.h"
+
+#define CPRINTSUSB(format, args...) cprints(CC_USBCHARGE, format, ##args)
+#define CPRINTS(format, args...) cprints(CC_SYSTEM, format, ##args)
+#define CPRINTF(format, args...) cprintf(CC_SYSTEM, format, ##args)
+
+int tusb1064_mux_1_board_init(const struct usb_mux *me)
+{
+ int rv;
+
+ rv = i2c_write8(me->i2c_port, me->i2c_addr_flags,
+ TUSB1064_REG_DP1DP3EQ_SEL,
+ TUSB1064_DP1EQ(TUSB1064_DP_EQ_RX_8_9_DB) |
+ TUSB1064_DP3EQ(TUSB1064_DP_EQ_RX_5_4_DB));
+ if (rv)
+ return rv;
+
+ /* Enable EQ_OVERRIDE so the gain registers are used */
+ return i2c_update8(me->i2c_port, me->i2c_addr_flags,
+ TUSB1064_REG_GENERAL, REG_GENERAL_EQ_OVERRIDE,
+ MASK_SET);
+}
+
+#ifdef CONFIG_USB_PD_TCPM_ITE_ON_CHIP
+const struct cc_para_t *board_get_cc_tuning_parameter(enum usbpd_port port)
+{
+ const static struct cc_para_t
+ cc_parameter[CONFIG_USB_PD_ITE_ACTIVE_PORT_COUNT] = {
+ {
+ .rising_time =
+ IT83XX_TX_PRE_DRIVING_TIME_1_UNIT,
+ .falling_time =
+ IT83XX_TX_PRE_DRIVING_TIME_2_UNIT,
+ },
+ {
+ .rising_time =
+ IT83XX_TX_PRE_DRIVING_TIME_1_UNIT,
+ .falling_time =
+ IT83XX_TX_PRE_DRIVING_TIME_2_UNIT,
+ },
+ };
+
+ return &cc_parameter[port];
+}
+#endif
+
+void board_reset_pd_mcu(void)
+{
+ /*
+ * C0 & C1: TCPC is embedded in the EC and processes interrupts in the
+ * chip code (it83xx/intc.c)
+ */
+}
+
+#ifndef CONFIG_TEST
+int board_set_active_charge_port(int port)
+{
+ int i;
+ int is_valid_port = (port >= 0 && port < board_get_usb_pd_port_count());
+
+ if (!is_valid_port && port != CHARGE_PORT_NONE) {
+ return EC_ERROR_INVAL;
+ }
+
+ if (port == CHARGE_PORT_NONE) {
+ CPRINTS("Disabling all charger ports");
+
+ /* Disable all ports. */
+ for (i = 0; i < ppc_cnt; i++) {
+ /*
+ * Do not return early if one fails otherwise we can
+ * get into a boot loop assertion failure.
+ */
+ if (ppc_vbus_sink_enable(i, 0)) {
+ CPRINTS("Disabling C%d as sink failed.", i);
+ }
+ }
+
+ return EC_SUCCESS;
+ }
+
+ /* Check if the port is sourcing VBUS. */
+ if (ppc_is_sourcing_vbus(port)) {
+ CPRINTS("Skip enable C%d", port);
+ return EC_ERROR_INVAL;
+ }
+
+ CPRINTS("New charge port: C%d", port);
+
+ /*
+ * Turn off the other ports' sink path FETs, before enabling the
+ * requested charge port.
+ */
+ for (i = 0; i < ppc_cnt; i++) {
+ if (i == port) {
+ continue;
+ }
+
+ if (ppc_vbus_sink_enable(i, 0)) {
+ CPRINTS("C%d: sink path disable failed.", i);
+ }
+ }
+
+ /* Enable requested charge port. */
+ if (ppc_vbus_sink_enable(port, 1)) {
+ CPRINTS("C%d: sink path enable failed.", port);
+ return EC_ERROR_UNKNOWN;
+ }
+
+ return EC_SUCCESS;
+}
+#endif
+
+#ifdef CONFIG_USB_PD_VBUS_MEASURE_ADC_EACH_PORT
+enum adc_channel board_get_vbus_adc(int port)
+{
+ if (port == 0) {
+ return ADC_VBUS_C0;
+ }
+ if (port == 1) {
+ return ADC_VBUS_C1;
+ }
+ CPRINTSUSB("Unknown vbus adc port id: %d", port);
+ return ADC_VBUS_C0;
+}
+#endif /* CONFIG_USB_PD_VBUS_MEASURE_ADC_EACH_PORT */