summaryrefslogtreecommitdiff
path: root/board/beadrix/led.c
diff options
context:
space:
mode:
Diffstat (limited to 'board/beadrix/led.c')
-rw-r--r--board/beadrix/led.c182
1 files changed, 127 insertions, 55 deletions
diff --git a/board/beadrix/led.c b/board/beadrix/led.c
index 6fcff58280..ab011d49d4 100644
--- a/board/beadrix/led.c
+++ b/board/beadrix/led.c
@@ -3,80 +3,152 @@
* found in the LICENSE file.
*/
-/* Waddledee specific PWM LED settings. */
+/* Beadrix specific LED settings. */
+#include "battery.h"
+#include "charge_manager.h"
+#include "charge_state.h"
+#include "chipset.h"
#include "common.h"
#include "ec_commands.h"
-#include "led_pwm.h"
-#include "pwm.h"
+#include "gpio.h"
+#include "hooks.h"
+#include "host_command.h"
+#include "led_common.h"
+#include "system.h"
#include "util.h"
+#define LED_ONE_SEC (1000 / HOOK_TICK_INTERVAL_MS)
+#define BAT_LED_ON 0
+#define BAT_LED_OFF 1
+
const enum ec_led_id supported_led_ids[] = {
- EC_LED_ID_POWER_LED,
+ EC_LED_ID_BATTERY_LED
};
const int supported_led_ids_count = ARRAY_SIZE(supported_led_ids);
-/*
- * Board has one physical LED with red, green, and blue
- */
-struct pwm_led_color_map led_color_map[EC_LED_COLOR_COUNT] = {
- /* Red, Green, Blue */
- [EC_LED_COLOR_RED] = { 100, 0, 0 },
- [EC_LED_COLOR_GREEN] = { 0, 100, 0 },
- [EC_LED_COLOR_BLUE] = { 0, 0, 100 },
- [EC_LED_COLOR_YELLOW] = { 50, 50, 0 },
- [EC_LED_COLOR_WHITE] = { 50, 50, 50 },
- [EC_LED_COLOR_AMBER] = { 70, 30, 0 },
+enum led_color {
+ LED_OFF = 0,
+ LED_RED,
+ LED_BLUE,
+ LED_COLOR_COUNT /* Number of colors, not a color itself */
};
-/* One logical LED with red, green, and blue channels. */
-struct pwm_led pwm_leds[CONFIG_LED_PWM_COUNT] = {
- {
- .ch0 = PWM_CH_LED_RED,
- .ch1 = PWM_CH_LED_GREEN,
- .ch2 = PWM_CH_LED_BLUE,
- .enable = &pwm_enable,
- .set_duty = &pwm_set_duty,
- },
-};
+static void led_set_color(enum led_color color)
+{
+ gpio_set_level(GPIO_EC_LED_R_ODL,
+ (color == LED_RED) ? BAT_LED_ON : BAT_LED_OFF);
+ gpio_set_level(GPIO_EC_LED_B_ODL,
+ (color == LED_BLUE) ? BAT_LED_ON : BAT_LED_OFF);
+}
void led_get_brightness_range(enum ec_led_id led_id, uint8_t *brightness_range)
{
- memset(brightness_range, '\0',
- sizeof(*brightness_range) * EC_LED_COLOR_COUNT);
- brightness_range[EC_LED_COLOR_RED] = 100;
- brightness_range[EC_LED_COLOR_GREEN] = 100;
- brightness_range[EC_LED_COLOR_BLUE] = 100;
- brightness_range[EC_LED_COLOR_YELLOW] = 100;
- brightness_range[EC_LED_COLOR_WHITE] = 100;
- brightness_range[EC_LED_COLOR_AMBER] = 100;
+ brightness_range[EC_LED_COLOR_RED] = 1;
+ brightness_range[EC_LED_COLOR_BLUE] = 1;
}
int led_set_brightness(enum ec_led_id led_id, const uint8_t *brightness)
{
- enum pwm_led_id pwm_id;
-
- /* Convert ec_led_id to pwm_led_id. */
- if (led_id == EC_LED_ID_POWER_LED)
- pwm_id = PWM_LED0;
+ if (brightness[EC_LED_COLOR_BLUE] != 0)
+ led_set_color(LED_BLUE);
+ else if (brightness[EC_LED_COLOR_RED] != 0)
+ led_set_color(LED_RED);
else
- return EC_ERROR_UNKNOWN;
-
- if (brightness[EC_LED_COLOR_RED])
- set_pwm_led_color(pwm_id, EC_LED_COLOR_RED);
- else if (brightness[EC_LED_COLOR_GREEN])
- set_pwm_led_color(pwm_id, EC_LED_COLOR_GREEN);
- else if (brightness[EC_LED_COLOR_BLUE])
- set_pwm_led_color(pwm_id, EC_LED_COLOR_BLUE);
- else if (brightness[EC_LED_COLOR_YELLOW])
- set_pwm_led_color(pwm_id, EC_LED_COLOR_YELLOW);
- else if (brightness[EC_LED_COLOR_WHITE])
- set_pwm_led_color(pwm_id, EC_LED_COLOR_WHITE);
- else if (brightness[EC_LED_COLOR_AMBER])
- set_pwm_led_color(pwm_id, EC_LED_COLOR_AMBER);
- else
- /* Otherwise, the "color" is "off". */
- set_pwm_led_color(pwm_id, -1);
+ led_set_color(LED_OFF);
return EC_SUCCESS;
}
+
+static void board_led_set_battery(void)
+{
+ static int battery_ticks;
+ enum led_color color = LED_OFF;
+ int period = 0;
+ uint32_t chflags = charge_get_flags();
+
+ battery_ticks++;
+
+ switch (charge_get_state()) {
+ case PWR_STATE_CHARGE:
+ /* Always indicate amber on when charging. */
+ color = LED_RED;
+ break;
+ case PWR_STATE_DISCHARGE:
+ if (chipset_in_state(CHIPSET_STATE_ANY_SUSPEND)) {
+ /* Discharging in S3: Red 1 sec, off 3 sec */
+ period = (1 + 3) * LED_ONE_SEC;
+ battery_ticks = battery_ticks % period;
+ if (battery_ticks < 1 * LED_ONE_SEC)
+ color = LED_RED;
+ else
+ color = LED_OFF;
+ } else if (chipset_in_state(CHIPSET_STATE_ANY_OFF)) {
+ /* Discharging in S5: off */
+ color = LED_OFF;
+ } else if (chipset_in_state(CHIPSET_STATE_ON)) {
+ /* Discharging in S0: Blue on */
+ color = LED_BLUE;
+ }
+ break;
+ case PWR_STATE_ERROR:
+ /* Battery error: Red 1 sec, off 1 sec */
+ period = (1 + 1) * LED_ONE_SEC;
+ battery_ticks = battery_ticks % period;
+ if (battery_ticks < 1 * LED_ONE_SEC)
+ color = LED_RED;
+ else
+ color = LED_OFF;
+ break;
+ case PWR_STATE_CHARGE_NEAR_FULL:
+ /* Full Charged: Blue on */
+ color = LED_BLUE;
+ break;
+ case PWR_STATE_IDLE: /* External power connected in IDLE */
+ if (chflags & CHARGE_FLAG_FORCE_IDLE) {
+ /* Factory mode: Blue 2 sec, Red 2 sec */
+ period = (2 + 2) * LED_ONE_SEC;
+ battery_ticks = battery_ticks % period;
+ if (battery_ticks < 2 * LED_ONE_SEC)
+ color = LED_BLUE;
+ else
+ color = LED_RED;
+ } else
+ color = LED_BLUE;
+ break;
+ default:
+ /* Other states don't alter LED behavior */
+ break;
+ }
+
+ led_set_color(color);
+}
+
+/* Called by hook task every TICK */
+static void led_tick(void)
+{
+ if (led_auto_control_is_enabled(EC_LED_ID_BATTERY_LED))
+ board_led_set_battery();
+}
+DECLARE_HOOK(HOOK_TICK, led_tick, HOOK_PRIO_DEFAULT);
+
+void led_control(enum ec_led_id led_id, enum ec_led_state state)
+{
+ enum led_color color;
+
+ if ((led_id != EC_LED_ID_RECOVERY_HW_REINIT_LED) &&
+ (led_id != EC_LED_ID_SYSRQ_DEBUG_LED))
+ return;
+
+ if (state == LED_STATE_RESET) {
+ led_auto_control(EC_LED_ID_BATTERY_LED, 1);
+ board_led_set_battery();
+ return;
+ }
+
+ color = state ? LED_BLUE : LED_OFF;
+
+ led_auto_control(EC_LED_ID_BATTERY_LED, 0);
+
+ led_set_color(color);
+}