summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorParth Malkan <parthmalkan@google.com>2021-09-24 14:25:35 -0700
committerCommit Bot <commit-bot@chromium.org>2021-09-28 00:43:10 +0000
commit3932100f6df604496d3d19e0247df6c52359f0a6 (patch)
tree8519c5416f39da3709188b7219c9cf37e4dda6b1
parentf8380bc0b05e7d64ea3972a9b46517d8f4b4af2b (diff)
downloadchrome-ec-3932100f6df604496d3d19e0247df6c52359f0a6.tar.gz
zephyr: create led.c file local to Lazor project
Add led.c file locally to zephyr lazor project and unlink from CrOs led.c file BRANCH=None BUG=b:201007290 TEST=builds Signed-off-by: Parth Malkan <parthmalkan@google.com> Change-Id: Ibf3223da8889233cbc7cb3b21e78c6ea0773201a Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3183184 Reviewed-by: Wai-Hong Tam <waihong@google.com>
-rw-r--r--zephyr/projects/trogdor/lazor/CMakeLists.txt6
-rw-r--r--zephyr/projects/trogdor/lazor/src/led.c155
2 files changed, 158 insertions, 3 deletions
diff --git a/zephyr/projects/trogdor/lazor/CMakeLists.txt b/zephyr/projects/trogdor/lazor/CMakeLists.txt
index 2344e25666..7ab5d6a92e 100644
--- a/zephyr/projects/trogdor/lazor/CMakeLists.txt
+++ b/zephyr/projects/trogdor/lazor/CMakeLists.txt
@@ -20,9 +20,6 @@ zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_USBC
"${PLATFORM_EC_BASEBOARD}/usbc_config.c"
"${PLATFORM_EC_BASEBOARD}/usb_pd_policy.c")
-zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_LED_COMMON
- "${PLATFORM_EC_BOARD}/led.c")
-
zephyr_library_sources(
"${PLATFORM_EC_BOARD}/hibernate.c"
"${PLATFORM_EC_BOARD}/sku.c"
@@ -32,3 +29,6 @@ zephyr_library_sources(
# Board specific implementation
zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_I2C
"src/i2c.c")
+
+zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_LED_COMMON
+ "src/led.c")
diff --git a/zephyr/projects/trogdor/lazor/src/led.c b/zephyr/projects/trogdor/lazor/src/led.c
new file mode 100644
index 0000000000..e4b34576c8
--- /dev/null
+++ b/zephyr/projects/trogdor/lazor/src/led.c
@@ -0,0 +1,155 @@
+/* Copyright 2021 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * Power and battery LED control.
+ */
+
+#include "battery.h"
+#include "charge_manager.h"
+#include "charge_state.h"
+#include "chipset.h"
+#include "ec_commands.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 1
+#define BAT_LED_OFF 0
+
+const enum ec_led_id supported_led_ids[] = {
+ EC_LED_ID_BATTERY_LED,
+};
+
+const int supported_led_ids_count = ARRAY_SIZE(supported_led_ids);
+
+enum led_color {
+ LED_OFF = 0,
+ LED_AMBER,
+ LED_BLUE,
+ LED_COLOR_COUNT /* Number of colors, not a color itself */
+};
+
+static void led_set_color(enum led_color color)
+{
+ gpio_set_level(GPIO_EC_CHG_LED_Y_C1,
+ (color == LED_AMBER) ? BAT_LED_ON : BAT_LED_OFF);
+ gpio_set_level(GPIO_EC_CHG_LED_B_C1,
+ (color == LED_BLUE) ? BAT_LED_ON : BAT_LED_OFF);
+}
+
+void led_get_brightness_range(enum ec_led_id led_id, uint8_t *brightness_range)
+{
+ brightness_range[EC_LED_COLOR_AMBER] = 1;
+ brightness_range[EC_LED_COLOR_BLUE] = 1;
+}
+
+int led_set_brightness(enum ec_led_id led_id, const uint8_t *brightness)
+{
+ if (brightness[EC_LED_COLOR_BLUE] != 0)
+ led_set_color(LED_BLUE);
+ else if (brightness[EC_LED_COLOR_AMBER] != 0)
+ led_set_color(LED_AMBER);
+ else
+ led_set_color(LED_OFF);
+
+ return EC_SUCCESS;
+}
+
+static void board_led_set_battery(void)
+{
+ static int battery_ticks;
+ int 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_AMBER;
+ break;
+ case PWR_STATE_DISCHARGE:
+ if (chipset_in_state(CHIPSET_STATE_ANY_SUSPEND)) {
+ /* Discharging in S3: Amber 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_AMBER;
+ 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: Amber 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_AMBER;
+ 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, Amber 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_AMBER;
+ } 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);
+}