diff options
author | Randall Spangler <rspangler@chromium.org> | 2013-03-19 16:07:20 -0700 |
---|---|---|
committer | ChromeBot <chrome-bot@google.com> | 2013-03-20 17:42:04 -0700 |
commit | 22ff9df91367d6b74ab08521a6fc0b7293c8eaf9 (patch) | |
tree | 5e50b5a1e15a30c9c8f9ccee4650cd410537fe0f | |
parent | f8393fab2c63e4c85646a56bfcd44d42db89e13c (diff) | |
download | chrome-ec-22ff9df91367d6b74ab08521a6fc0b7293c8eaf9.tar.gz |
Move battery LED code on spring to common
It's not board-specific, so move it out of board.c.
No functional changes; this is just moving code and renaming a few functions.
BUG=chrome-os-partner:18256
BRANCH=none
TEST=build spring
Change-Id: Ib004066aed93745cb0c96d3cb45d70b728545492
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/45918
-rw-r--r-- | board/spring/board.c | 126 | ||||
-rw-r--r-- | board/spring/usb_charging.c | 1 | ||||
-rw-r--r-- | common/build.mk | 2 | ||||
-rw-r--r-- | common/lp5562.c | 2 | ||||
-rw-r--r-- | common/lp5562_battery_led.c | 136 | ||||
-rw-r--r-- | include/lp5562.h | 2 |
6 files changed, 139 insertions, 130 deletions
diff --git a/board/spring/board.c b/board/spring/board.c index 82be07121f..cdb7abe3d7 100644 --- a/board/spring/board.c +++ b/board/spring/board.c @@ -16,10 +16,8 @@ #include "host_command.h" #include "i2c.h" #include "keyboard_scan.h" -#include "lp5562.h" #include "pmu_tpschrome.h" #include "registers.h" -#include "smart_battery.h" #include "stm32_adc.h" #include "timer.h" #include "util.h" @@ -32,21 +30,6 @@ #define HARD_RESET_TIMEOUT_MS 5 -/* We use yellow LED instead of blue LED. Re-map colors here. */ -#define LED_COLOR_NONE LP5562_COLOR_NONE -#define LED_COLOR_GREEN LP5562_COLOR_GREEN -#define LED_COLOR_YELLOW LP5562_COLOR_BLUE -#define LED_COLOR_RED LP5562_COLOR_RED - -/* LED breathing program */ -uint8_t breathing_prog[] = {0x41, 0xff, /* 0x80 -> 0x0 */ - 0x41, 0x7f, /* 0x0 -> 0x80 */ - 0x7f, 0x00, /* Wait ~4s */ - 0x7f, 0x00, - 0x7f, 0x00, - 0x7f, 0x00, - 0x00, 0x00}; /* Repeat */ - void usb_charge_interrupt(enum gpio_signal signal); /* GPIO signal list. Must match order from enum gpio_signal. */ @@ -311,115 +294,6 @@ int extpower_is_present(void) return vbus_good; } - -int board_led_breathing(int enabled) -{ - int ret = 0; - - if (enabled) { - ret |= lp5562_engine_load(LP5562_ENG_SEL_1, - breathing_prog, - sizeof(breathing_prog)); - ret |= lp5562_engine_control(LP5562_ENG_RUN, - LP5562_ENG_HOLD, - LP5562_ENG_HOLD); - ret |= lp5562_set_engine(LP5562_ENG_SEL_NONE, - LP5562_ENG_SEL_NONE, - LP5562_ENG_SEL_1); - } else { - ret |= lp5562_engine_control(LP5562_ENG_HOLD, - LP5562_ENG_HOLD, - LP5562_ENG_HOLD); - ret |= lp5562_set_engine(LP5562_ENG_SEL_NONE, - LP5562_ENG_SEL_NONE, - LP5562_ENG_SEL_NONE); - } - - return ret; -} - -static void board_battery_led_update(void) -{ - int current; - int desired_current; - - /* Current states and next states */ - static uint32_t color = LED_COLOR_RED; - static int breathing; - static int led_power; - int new_color = LED_COLOR_RED; - int new_breathing = 0; - int new_led_power; - - /* Determine LED power */ - new_led_power = extpower_is_present(); - if (new_led_power != led_power) { - led_power = new_led_power; - if (new_led_power) { - lp5562_poweron(); - } else { - color = LED_COLOR_NONE; - if (breathing) { - board_led_breathing(0); - breathing = 0; - } - lp5562_poweroff(); - } - } - if (!new_led_power) - return; - - /* - * LED power is controlled by accessory detection. We only - * set color here. - */ - switch (charge_get_state()) { - case ST_IDLE: - new_color = LED_COLOR_GREEN; - break; - case ST_DISCHARGING: - /* Discharging with AC, must be battery assist */ - new_color = LED_COLOR_YELLOW; - new_breathing = 1; - break; - case ST_PRE_CHARGING: - new_color = LED_COLOR_YELLOW; - break; - case ST_CHARGING: - if (battery_current(¤t) || - battery_desired_current(&desired_current)) { - /* Cannot talk to the battery. Set LED to red. */ - new_color = LED_COLOR_RED; - break; - } - - if (current < 0 && desired_current > 0) { /* Battery assist */ - new_breathing = 1; - new_color = LED_COLOR_YELLOW; - break; - } - - if (current && desired_current) - new_color = LED_COLOR_YELLOW; - else - new_color = LED_COLOR_GREEN; - break; - case ST_CHARGING_ERROR: - new_color = LED_COLOR_RED; - break; - } - - if (new_color != color) { - lp5562_set_color(new_color); - color = new_color; - } - if (new_breathing != breathing) { - board_led_breathing(new_breathing); - breathing = new_breathing; - } -} -DECLARE_HOOK(HOOK_SECOND, board_battery_led_update, HOOK_PRIO_DEFAULT); - /*****************************************************************************/ /* Host commands */ diff --git a/board/spring/usb_charging.c b/board/spring/usb_charging.c index 1818b4a489..7d9d5e168f 100644 --- a/board/spring/usb_charging.c +++ b/board/spring/usb_charging.c @@ -10,7 +10,6 @@ #include "console.h" #include "hooks.h" #include "gpio.h" -#include "lp5562.h" #include "keyboard_scan.h" #include "pmu_tpschrome.h" #include "registers.h" diff --git a/common/build.mk b/common/build.mk index a490cf9479..99865dcfc4 100644 --- a/common/build.mk +++ b/common/build.mk @@ -22,7 +22,7 @@ common-$(CONFIG_I2C)+=i2c_commands.o common-$(CONFIG_I2C_ARBITRATION)+=i2c_arbitration.o common-$(CONFIG_IR357x)+=ir357x.o common-$(CONFIG_KEYBOARD_TEST)+=keyboard_test.o -common-$(CONFIG_LP5562)+=lp5562.o +common-$(CONFIG_LP5562)+=lp5562.o lp5562_battery_led.o common-$(CONFIG_LPC)+=port80.o common-$(CONFIG_ONEWIRE_LED)+=onewire_led.o common-$(CONFIG_PSTORE)+=pstore_commands.o diff --git a/common/lp5562.c b/common/lp5562.c index 09ee0df2c5..a16c2e837a 100644 --- a/common/lp5562.c +++ b/common/lp5562.c @@ -42,7 +42,7 @@ int lp5562_set_engine(uint8_t r, uint8_t g, uint8_t b) return lp5562_write(LP5562_REG_LED_MAP, (r << 4) | (g << 2) | b); } -int lp5562_engine_load(int engine, uint8_t *program, int size) +int lp5562_engine_load(int engine, const uint8_t *program, int size) { int prog_addr = LP5562_REG_ENG_PROG(engine); int i, ret, val; diff --git a/common/lp5562_battery_led.c b/common/lp5562_battery_led.c new file mode 100644 index 0000000000..eb1f1f9cd1 --- /dev/null +++ b/common/lp5562_battery_led.c @@ -0,0 +1,136 @@ +/* Copyright (c) 2013 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. + * + * Battery LED state machine to drive RGB LED on LP5562 + */ + +#include "common.h" +#include "extpower.h" +#include "hooks.h" +#include "lp5562.h" +#include "pmu_tpschrome.h" +#include "smart_battery.h" + +/* We use yellow LED instead of blue LED. Re-map colors here. */ +#define LED_COLOR_NONE LP5562_COLOR_NONE +#define LED_COLOR_GREEN LP5562_COLOR_GREEN +#define LED_COLOR_YELLOW LP5562_COLOR_BLUE +#define LED_COLOR_RED LP5562_COLOR_RED + +/* LED breathing program */ +static const uint8_t breathing_prog[] = {0x41, 0xff, /* 0x80 -> 0x0 */ + 0x41, 0x7f, /* 0x0 -> 0x80 */ + 0x7f, 0x00, /* Wait ~4s */ + 0x7f, 0x00, + 0x7f, 0x00, + 0x7f, 0x00, + 0x00, 0x00}; /* Repeat */ + +static int led_breathing(int enabled) +{ + int ret = 0; + + if (enabled) { + ret |= lp5562_engine_load(LP5562_ENG_SEL_1, + breathing_prog, + sizeof(breathing_prog)); + ret |= lp5562_engine_control(LP5562_ENG_RUN, + LP5562_ENG_HOLD, + LP5562_ENG_HOLD); + ret |= lp5562_set_engine(LP5562_ENG_SEL_NONE, + LP5562_ENG_SEL_NONE, + LP5562_ENG_SEL_1); + } else { + ret |= lp5562_engine_control(LP5562_ENG_HOLD, + LP5562_ENG_HOLD, + LP5562_ENG_HOLD); + ret |= lp5562_set_engine(LP5562_ENG_SEL_NONE, + LP5562_ENG_SEL_NONE, + LP5562_ENG_SEL_NONE); + } + + return ret; +} + +static void battery_led_update(void) +{ + int current; + int desired_current; + + /* Current states and next states */ + static uint32_t color = LED_COLOR_RED; + static int breathing; + static int led_power; + int new_color = LED_COLOR_RED; + int new_breathing = 0; + int new_led_power; + + /* Determine LED power */ + new_led_power = extpower_is_present(); + if (new_led_power != led_power) { + led_power = new_led_power; + if (new_led_power) { + lp5562_poweron(); + } else { + color = LED_COLOR_NONE; + if (breathing) { + led_breathing(0); + breathing = 0; + } + lp5562_poweroff(); + } + } + if (!new_led_power) + return; + + /* + * LED power is controlled by accessory detection. We only + * set color here. + */ + switch (charge_get_state()) { + case ST_IDLE: + new_color = LED_COLOR_GREEN; + break; + case ST_DISCHARGING: + /* Discharging with AC, must be battery assist */ + new_color = LED_COLOR_YELLOW; + new_breathing = 1; + break; + case ST_PRE_CHARGING: + new_color = LED_COLOR_YELLOW; + break; + case ST_CHARGING: + if (battery_current(¤t) || + battery_desired_current(&desired_current)) { + /* Cannot talk to the battery. Set LED to red. */ + new_color = LED_COLOR_RED; + break; + } + + if (current < 0 && desired_current > 0) { /* Battery assist */ + new_breathing = 1; + new_color = LED_COLOR_YELLOW; + break; + } + + if (current && desired_current) + new_color = LED_COLOR_YELLOW; + else + new_color = LED_COLOR_GREEN; + break; + case ST_CHARGING_ERROR: + new_color = LED_COLOR_RED; + break; + } + + if (new_color != color) { + lp5562_set_color(new_color); + color = new_color; + } + if (new_breathing != breathing) { + led_breathing(new_breathing); + breathing = new_breathing; + } +} +DECLARE_HOOK(HOOK_SECOND, battery_led_update, HOOK_PRIO_DEFAULT); diff --git a/include/lp5562.h b/include/lp5562.h index b993825c7a..f1a5af092c 100644 --- a/include/lp5562.h +++ b/include/lp5562.h @@ -57,7 +57,7 @@ int lp5562_set_color(uint32_t rgb); int lp5562_set_engine(uint8_t r, uint8_t g, uint8_t b); /* Load lighting engine program */ -int lp5562_engine_load(int engine, uint8_t *program, int size); +int lp5562_engine_load(int engine, const uint8_t *program, int size); /* Control lighting engine execution state */ int lp5562_engine_control(int eng1, int eng2, int eng3); |