From c6c396ce2a2551ff8184ef0fc6785637801e5710 Mon Sep 17 00:00:00 2001 From: CTCaer Date: Mon, 11 Jan 2021 21:30:59 +0200 Subject: [PATCH] reg5V: Manage battery source based on charger status --- bdk/input/joycon.c | 8 ++++---- bdk/power/regulator_5v.c | 17 ++++++++++++++--- bdk/power/regulator_5v.h | 7 ++++--- bdk/soc/hw_init.c | 2 +- bdk/thermal/fan.c | 4 ++-- bdk/utils/btn.c | 2 +- nyx/nyx_gui/frontend/gui.c | 7 +++++++ 7 files changed, 33 insertions(+), 14 deletions(-) diff --git a/bdk/input/joycon.c b/bdk/input/joycon.c index f2f03ea..9da1071 100644 --- a/bdk/input/joycon.c +++ b/bdk/input/joycon.c @@ -808,10 +808,10 @@ void jc_power_supply(u8 uart, bool enable) { if (enable) { - if (regulator_get_5v_dev_enabled(1 << uart)) + if (regulator_5v_get_dev_enabled(1 << uart)) return; - regulator_enable_5v(1 << uart); + regulator_5v_enable(1 << uart); if (jc_init_done) { @@ -841,10 +841,10 @@ void jc_power_supply(u8 uart, bool enable) } else { - if (!regulator_get_5v_dev_enabled(1 << uart)) + if (!regulator_5v_get_dev_enabled(1 << uart)) return; - regulator_disable_5v(1 << uart); + regulator_5v_disable(1 << uart); if (uart == UART_C) gpio_write(GPIO_PORT_CC, GPIO_PIN_3, GPIO_LOW); diff --git a/bdk/power/regulator_5v.c b/bdk/power/regulator_5v.c index c61db64..31d32f6 100644 --- a/bdk/power/regulator_5v.c +++ b/bdk/power/regulator_5v.c @@ -21,8 +21,9 @@ #include static u8 reg_5v_dev = 0; +static bool batt_src = false; -void regulator_enable_5v(u8 dev) +void regulator_5v_enable(u8 dev) { // The power supply selection from battery or USB is automatic. if (!reg_5v_dev) @@ -32,6 +33,7 @@ void regulator_enable_5v(u8 dev) gpio_config(GPIO_PORT_A, GPIO_PIN_5, GPIO_MODE_GPIO); gpio_output_enable(GPIO_PORT_A, GPIO_PIN_5, GPIO_OUTPUT_ENABLE); gpio_write(GPIO_PORT_A, GPIO_PIN_5, GPIO_HIGH); + batt_src = true; // Fan and Rail power from USB 5V VDD. PINMUX_AUX(PINMUX_AUX_USB_VBUS_EN0) = PINMUX_LPDR | 1; @@ -47,7 +49,7 @@ void regulator_enable_5v(u8 dev) reg_5v_dev |= dev; } -void regulator_disable_5v(u8 dev) +void regulator_5v_disable(u8 dev) { reg_5v_dev &= ~dev; @@ -58,6 +60,7 @@ void regulator_disable_5v(u8 dev) gpio_output_enable(GPIO_PORT_A, GPIO_PIN_5, GPIO_OUTPUT_DISABLE); gpio_config(GPIO_PORT_A, GPIO_PIN_5, GPIO_MODE_SPIO); PINMUX_AUX(PINMUX_AUX_SATA_LED_ACTIVE) = PINMUX_PARKED | PINMUX_INPUT_ENABLE; + batt_src = false; // Rail power from USB 5V VDD. gpio_write(GPIO_PORT_CC, GPIO_PIN_4, GPIO_LOW); @@ -70,7 +73,15 @@ void regulator_disable_5v(u8 dev) } } -bool regulator_get_5v_dev_enabled(u8 dev) +bool regulator_5v_get_dev_enabled(u8 dev) { return (reg_5v_dev & dev); } + +void regulator_5v_batt_src_enable(bool enable) +{ + if (enable && !batt_src) + gpio_write(GPIO_PORT_A, GPIO_PIN_5, GPIO_HIGH); + else if (!enable && batt_src) + gpio_write(GPIO_PORT_A, GPIO_PIN_5, GPIO_LOW); +} diff --git a/bdk/power/regulator_5v.h b/bdk/power/regulator_5v.h index 6bb837a..b7d7490 100644 --- a/bdk/power/regulator_5v.h +++ b/bdk/power/regulator_5v.h @@ -27,8 +27,9 @@ enum REGULATOR_5V_ALL = 0xFF }; -void regulator_enable_5v(u8 dev); -void regulator_disable_5v(u8 dev); -bool regulator_get_5v_dev_enabled(u8 dev); +void regulator_5v_enable(u8 dev); +void regulator_5v_disable(u8 dev); +bool regulator_5v_get_dev_enabled(u8 dev); +void regulator_5v_batt_src_enable(bool enable); #endif \ No newline at end of file diff --git a/bdk/soc/hw_init.c b/bdk/soc/hw_init.c index 269a882..5c81b1a 100644 --- a/bdk/soc/hw_init.c +++ b/bdk/soc/hw_init.c @@ -429,7 +429,7 @@ void hw_reinit_workaround(bool coreboot, u32 magic) touch_power_off(); set_fan_duty(0); jc_deinit(); - regulator_disable_5v(REGULATOR_5V_ALL); + regulator_5v_disable(REGULATOR_5V_ALL); clock_disable_uart(UART_B); clock_disable_uart(UART_C); #endif diff --git a/bdk/thermal/fan.c b/bdk/thermal/fan.c index f39b082..d149b36 100644 --- a/bdk/thermal/fan.c +++ b/bdk/thermal/fan.c @@ -56,7 +56,7 @@ void set_fan_duty(u32 duty) if (inv_duty == 236) { PWM(PWM_CONTROLLER_PWM_CSR_1) = PWM_CSR_EN | (0x100 << 16); // Bit 24 is absolute 0%. - regulator_disable_5v(REGULATOR_5V_FAN); + regulator_5v_disable(REGULATOR_5V_FAN); // Disable fan. PINMUX_AUX(PINMUX_AUX_LCD_GPIO2) = @@ -65,7 +65,7 @@ void set_fan_duty(u32 duty) else // Set PWM duty. { // Fan power supply. - regulator_enable_5v(REGULATOR_5V_FAN); + regulator_5v_enable(REGULATOR_5V_FAN); PWM(PWM_CONTROLLER_PWM_CSR_1) = PWM_CSR_EN | (inv_duty << 16); // Enable fan. diff --git a/bdk/utils/btn.c b/bdk/utils/btn.c index b683003..cc36573 100644 --- a/bdk/utils/btn.c +++ b/bdk/utils/btn.c @@ -29,7 +29,7 @@ u8 btn_read() res |= BTN_VOL_DOWN; if (!gpio_read(GPIO_PORT_X, GPIO_PIN_6)) res |= BTN_VOL_UP; - if (i2c_recv_byte(4, MAX77620_I2C_ADDR, MAX77620_REG_ONOFFSTAT) & MAX77620_ONOFFSTAT_EN0) + if (i2c_recv_byte(I2C_5, MAX77620_I2C_ADDR, MAX77620_REG_ONOFFSTAT) & MAX77620_ONOFFSTAT_EN0) res |= BTN_POWER; return res; } diff --git a/nyx/nyx_gui/frontend/gui.c b/nyx/nyx_gui/frontend/gui.c index 11b1a4f..5c78a29 100644 --- a/nyx/nyx_gui/frontend/gui.c +++ b/nyx/nyx_gui/frontend/gui.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -1263,8 +1264,14 @@ static void _update_status_bar(void *params) else strcat(label, "#FF3C28 "SYMBOL_BATTERY_EMPTY"#"); + // Set charging symbol and regulator 5V source based on USB state. if (charge_status) + { strcat(label, " #FFDD00 "SYMBOL_CHARGE"#"); + regulator_5v_batt_src_enable(false); + } + else + regulator_5v_batt_src_enable(true); lv_label_set_text(status_bar.battery, label); lv_obj_realign(status_bar.battery);