summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMary Ruthven <mruthven@chromium.org>2015-12-18 13:49:54 -0800
committerchrome-bot <chrome-bot@chromium.org>2015-12-22 21:07:25 -0800
commit8b3090c1bdbb5515578b655e753574ebd9dfc304 (patch)
tree03ac326c3fa4811df4b7b6678761b33df5e3ce18
parent68a4b3a4b2bdf5b46b643805d641a01f6a8e8bc2 (diff)
downloadchrome-ec-8b3090c1bdbb5515578b655e753574ebd9dfc304.tar.gz
lucid: add LED support
Implement LED control for lucid with red, blue, and green LEDs. BUG=chrome-os-partner:48661 BRANCH=none TEST=make sure lucid builds Change-Id: I97ed56daa8fdb40daf8ab06e53913dcff2e41dea Signed-off-by: Mary Ruthven <mruthven@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/319224 Commit-Ready: Alec Berg <alecaberg@chromium.org> Tested-by: Alec Berg <alecaberg@chromium.org> Reviewed-by: Alec Berg <alecaberg@chromium.org>
-rw-r--r--board/lucid/board.c9
-rw-r--r--board/lucid/board.h1
-rw-r--r--board/lucid/build.mk2
-rw-r--r--board/lucid/gpio.inc4
-rw-r--r--board/lucid/led.c192
-rw-r--r--include/led_common.h7
6 files changed, 214 insertions, 1 deletions
diff --git a/board/lucid/board.c b/board/lucid/board.c
index 2cff3dc48c..12bf407a36 100644
--- a/board/lucid/board.c
+++ b/board/lucid/board.c
@@ -14,6 +14,7 @@
#include "gpio.h"
#include "hooks.h"
#include "i2c.h"
+#include "led_common.h"
#include "registers.h"
#include "task.h"
#include "usb_charge.h"
@@ -63,6 +64,11 @@ void vbus_evt(enum gpio_signal signal)
extpower_interrupt(signal);
}
+void charge_state_interrupt(enum gpio_signal signal)
+{
+ led_enable(gpio_get_level(signal));
+}
+
#include "gpio_list.h"
/* ADC channels */
@@ -116,6 +122,9 @@ static void board_init(void)
&charge_none);
}
+ /* Enable charge status interrupt */
+ gpio_enable_interrupt(GPIO_CHARGE_STATUS);
+
/* Initialize VBUS supplier based on whether or not VBUS is present */
update_vbus_supplier(gpio_get_level(GPIO_AC_PRESENT));
}
diff --git a/board/lucid/board.h b/board/lucid/board.h
index e193f276f3..84d3678be8 100644
--- a/board/lucid/board.h
+++ b/board/lucid/board.h
@@ -44,6 +44,7 @@
#define CONFIG_I2C
#define CONFIG_I2C_MASTER
#define CONFIG_I2C_SLAVE
+#define CONFIG_LED_COMMON
#undef CONFIG_LID_SWITCH
#define CONFIG_LOW_POWER_IDLE
#define CONFIG_STM_HWTIMER32
diff --git a/board/lucid/build.mk b/board/lucid/build.mk
index 634d0b8c34..c6b43f3d2a 100644
--- a/board/lucid/build.mk
+++ b/board/lucid/build.mk
@@ -10,6 +10,6 @@ CHIP:=stm32
CHIP_FAMILY:=stm32f0
CHIP_VARIANT:=stm32f07x
-board-y=board.o
+board-y=board.o led.o
board-$(CONFIG_BATTERY_SMART)+=battery.o
board-$(CONFIG_USB_POWER_DELIVERY)+=usb_pd_policy.o
diff --git a/board/lucid/gpio.inc b/board/lucid/gpio.inc
index aa6f010d02..72fb4acfa8 100644
--- a/board/lucid/gpio.inc
+++ b/board/lucid/gpio.inc
@@ -7,6 +7,7 @@
/* Interrupts */
GPIO_INT(AC_PRESENT, PIN(C, 13), GPIO_INT_BOTH, vbus_evt) /* AC power present */
+GPIO_INT(CHARGE_STATUS, PIN(F, 1), GPIO_INT_BOTH, charge_state_interrupt)
/* PD RX/TX */
GPIO(USB_C0_CC1_PD, PIN(A, 1), GPIO_ANALOG)
@@ -29,6 +30,9 @@ GPIO(USB_C0_P, PIN(A, 12), GPIO_INPUT)
/* Other outputs */
GPIO(AP_INT_L, PIN(A, 13), GPIO_ODR_HIGH)
GPIO(USB_C_CC_EN, PIN(A, 15), GPIO_OUT_HIGH)
+GPIO(BAT_LED_GREEN, PIN(C, 14), GPIO_ODR_HIGH)
+GPIO(BAT_LED_RED, PIN(C, 15), GPIO_ODR_HIGH)
+GPIO(BAT_LED_BLUE, PIN(F, 0), GPIO_ODR_HIGH)
UNIMPLEMENTED(ENTERING_RW)
diff --git a/board/lucid/led.c b/board/lucid/led.c
new file mode 100644
index 0000000000..7b722b7b51
--- /dev/null
+++ b/board/lucid/led.c
@@ -0,0 +1,192 @@
+/* Copyright 2015 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 for Lucid.
+ */
+
+#include "battery.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 "util.h"
+
+#define BAT_LED_ON 0
+#define BAT_LED_OFF 1
+
+#define CRITICAL_LOW_BATTERY_PERCENTAGE 5
+#define LOW_BATTERY_PERCENTAGE 20
+#define HIGH_BATTERY_PERCENTAGE 95
+
+#define LED_TOTAL_2SECS_TICKS 2
+#define LED_ON_1SEC_TICKS 1
+#define LED_ON_2SECS_TICKS 2
+
+const enum ec_led_id supported_led_ids[] = {
+ EC_LED_ID_BATTERY_LED};
+
+const int supported_led_ids_count = ARRAY_SIZE(supported_led_ids);
+
+static int led_enabled, battery_ticks;
+
+enum led_color {
+ LED_OFF = 0,
+ LED_RED,
+ LED_AMBER,
+ LED_GREEN,
+ LED_BLUE,
+ LED_COLOR_COUNT /* Number of colors, not a color itself */
+};
+
+static int bat_led_set_color(enum led_color color)
+{
+ switch (color) {
+ case LED_OFF:
+ gpio_set_level(GPIO_BAT_LED_RED, BAT_LED_OFF);
+ gpio_set_level(GPIO_BAT_LED_GREEN, BAT_LED_OFF);
+ gpio_set_level(GPIO_BAT_LED_BLUE, BAT_LED_OFF);
+ break;
+ case LED_RED:
+ gpio_set_level(GPIO_BAT_LED_RED, BAT_LED_ON);
+ gpio_set_level(GPIO_BAT_LED_GREEN, BAT_LED_OFF);
+ gpio_set_level(GPIO_BAT_LED_BLUE, BAT_LED_OFF);
+ break;
+ case LED_AMBER:
+ gpio_set_level(GPIO_BAT_LED_RED, BAT_LED_ON);
+ gpio_set_level(GPIO_BAT_LED_GREEN, BAT_LED_ON);
+ gpio_set_level(GPIO_BAT_LED_BLUE, BAT_LED_OFF);
+ break;
+ case LED_GREEN:
+ gpio_set_level(GPIO_BAT_LED_RED, BAT_LED_OFF);
+ gpio_set_level(GPIO_BAT_LED_GREEN, BAT_LED_ON);
+ gpio_set_level(GPIO_BAT_LED_BLUE, BAT_LED_OFF);
+ break;
+ case LED_BLUE:
+ gpio_set_level(GPIO_BAT_LED_RED, BAT_LED_OFF);
+ gpio_set_level(GPIO_BAT_LED_GREEN, BAT_LED_OFF);
+ gpio_set_level(GPIO_BAT_LED_BLUE, BAT_LED_ON);
+ break;
+ default:
+ return EC_ERROR_UNKNOWN;
+ }
+ return EC_SUCCESS;
+}
+
+void led_get_brightness_range(enum ec_led_id led_id, uint8_t *brightness_range)
+{
+ brightness_range[EC_LED_COLOR_RED] = 1;
+ brightness_range[EC_LED_COLOR_GREEN] = 1;
+ brightness_range[EC_LED_COLOR_BLUE] = 1;
+}
+
+static int lucid_led_set_color_battery(enum led_color color)
+{
+ return bat_led_set_color(color);
+}
+
+static int lucid_led_set_color(enum ec_led_id led_id, enum led_color color)
+{
+ int rv;
+
+ led_auto_control(led_id, 0);
+ switch (led_id) {
+ case EC_LED_ID_BATTERY_LED:
+ rv = lucid_led_set_color_battery(color);
+ break;
+ default:
+ return EC_ERROR_UNKNOWN;
+ }
+ return rv;
+}
+
+int led_set_brightness(enum ec_led_id led_id, const uint8_t *brightness)
+{
+ if (brightness[EC_LED_COLOR_RED] != 0 &&
+ brightness[EC_LED_COLOR_GREEN] != 0)
+ lucid_led_set_color(led_id, LED_AMBER);
+ else if (brightness[EC_LED_COLOR_RED] != 0)
+ lucid_led_set_color(led_id, LED_RED);
+ else if (brightness[EC_LED_COLOR_GREEN] != 0)
+ lucid_led_set_color(led_id, LED_GREEN);
+ else if (brightness[EC_LED_COLOR_BLUE] != 0)
+ lucid_led_set_color(led_id, LED_BLUE);
+ else
+ lucid_led_set_color(led_id, LED_OFF);
+ return EC_SUCCESS;
+}
+
+static void lucid_update_charge_display(void)
+{
+ uint32_t chflags = charge_get_flags();
+
+ /* BAT LED behavior:
+ * Fully charged: Green
+ * Force Idle (for factory): 2 secs of Blue 2 secs of Amber
+ * Battery low (20%): Red
+ * Battery critical low (5%): Red blink 1 second every 2 seconds
+ * Using battery or not connected to AC power: OFF
+ */
+ switch (charge_get_state()) {
+ case PWR_STATE_CHARGE:
+ lucid_led_set_color_battery(
+ (battery_ticks % LED_TOTAL_2SECS_TICKS <
+ LED_ON_1SEC_TICKS) ? LED_AMBER : LED_OFF);
+ break;
+ case PWR_STATE_DISCHARGE:
+ if (charge_get_percent() < CRITICAL_LOW_BATTERY_PERCENTAGE)
+ /* Blink once every two seconds */
+ lucid_led_set_color_battery(
+ (battery_ticks % LED_TOTAL_2SECS_TICKS <
+ LED_ON_1SEC_TICKS) ? LED_RED : LED_OFF);
+ else if (!led_enabled)
+ lucid_led_set_color_battery(LED_OFF);
+ else if (charge_get_percent() < LOW_BATTERY_PERCENTAGE)
+ lucid_led_set_color_battery(LED_RED);
+ else
+ lucid_led_set_color_battery(LED_AMBER);
+ break;
+ case PWR_STATE_ERROR:
+ lucid_led_set_color_battery(
+ (battery_ticks % LED_TOTAL_2SECS_TICKS <
+ LED_ON_1SEC_TICKS) ? LED_RED : LED_OFF);
+ break;
+ case PWR_STATE_CHARGE_NEAR_FULL:
+ lucid_led_set_color_battery(LED_GREEN);
+ break;
+ case PWR_STATE_IDLE: /* External power connected in IDLE. */
+ if (chflags & CHARGE_FLAG_FORCE_IDLE)
+ lucid_led_set_color_battery(
+ (battery_ticks % LED_TOTAL_2SECS_TICKS <
+ LED_ON_1SEC_TICKS) ? LED_BLUE : LED_AMBER);
+ else
+ lucid_led_set_color_battery(LED_BLUE);
+ break;
+ default:
+ /* Other states don't alter LED behavior */
+ break;
+ }
+}
+
+static void lucid_led_set_battery(void)
+{
+ battery_ticks++;
+ lucid_update_charge_display();
+}
+
+/** * Called by hook task every 1 sec */
+static void led_second(void)
+{
+ if (led_auto_control_is_enabled(EC_LED_ID_BATTERY_LED))
+ lucid_led_set_battery();
+}
+DECLARE_HOOK(HOOK_SECOND, led_second, HOOK_PRIO_DEFAULT);
+
+void led_enable(int enable)
+{
+ led_enabled = enable;
+ lucid_update_charge_display();
+}
diff --git a/include/led_common.h b/include/led_common.h
index eb541743d8..51230fbeb3 100644
--- a/include/led_common.h
+++ b/include/led_common.h
@@ -60,4 +60,11 @@ void led_get_brightness_range(enum ec_led_id, uint8_t *brightness_range);
*/
int led_set_brightness(enum ec_led_id led_id, const uint8_t *brightness);
+/**
+ * Enable LED.
+ *
+ * @param enable 1 to enable LED. 0 to disable.
+ *
+ */
+void led_enable(int enable);
#endif /* __CROS_EC_LED_COMMON_H */