kunimitsu: battery led implementation

Implement LED function for battery charging behavior

BUG=none
BRANCH=none
TEST=checked and verified LED behavior while charging and remaining battery
capacity under 3%/10%
Signed-off-by: Mike Hsieh <mike.m.hsieh@intel.com>

Change-Id: Ic340d4ea428f7726611b2ae24a4d18563c63ee80
Reviewed-on: https://chromium-review.googlesource.com/295476
Commit-Ready: Wenkai Du <wenkai.du@intel.com>
Tested-by: Wenkai Du <wenkai.du@intel.com>
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
This commit is contained in:
Mike M Hsieh
2015-08-27 11:44:14 +08:00
committed by chrome-bot
parent 5bc9b32807
commit 6ced01bb86
4 changed files with 172 additions and 1 deletions

View File

@@ -37,6 +37,7 @@
#undef CONFIG_KEYBOARD_KSO_BASE
#define CONFIG_KEYBOARD_KSO_BASE 0 /* KSO starts from KSO04 */
#define CONFIG_KEYBOARD_PROTOCOL_8042
#define CONFIG_LED_COMMON
#define CONFIG_LID_SWITCH
#define CONFIG_PORT80_TASK_EN
#define CONFIG_POWER_BUTTON
@@ -76,6 +77,10 @@
#define CONFIG_SYSTEM_UNLOCKED
#define CONFIG_WATCHDOG_HELP
/* LED signals */
#define GPIO_BAT_LED_BLUE GPIO_CHARGE_LED1
#define GPIO_BAT_LED_AMBER GPIO_CHARGE_LED2
/* I2C ports */
#define I2C_PORT_BATTERY MEC1322_I2C0_0
#define I2C_PORT_CHARGER MEC1322_I2C0_0

View File

@@ -10,6 +10,6 @@
CHIP:=mec1322
CHIP_SPI_SIZE_KB:=512
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

165
board/kunimitsu/led.c Normal file
View File

@@ -0,0 +1,165 @@
/* 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 Glados.
*/
#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 1
#define BAT_LED_OFF 0
#define CRITICAL_LOW_BATTERY_PERCENTAGE 3
#define LOW_BATTERY_PERCENTAGE 10
#define LED_TOTAL_4SECS_TICKS 16
#define LED_TOTAL_2SECS_TICKS 8
#define LED_ON_1SEC_TICKS 4
#define LED_ON_2SECS_TICKS 8
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_BLUE,
LED_PURPLE,
LED_AMBER,
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_BLUE, BAT_LED_OFF);
gpio_set_level(GPIO_BAT_LED_AMBER, BAT_LED_OFF);
break;
case LED_BLUE:
gpio_set_level(GPIO_BAT_LED_BLUE, BAT_LED_ON);
gpio_set_level(GPIO_BAT_LED_AMBER, BAT_LED_OFF);
break;
case LED_PURPLE:
gpio_set_level(GPIO_BAT_LED_BLUE, BAT_LED_ON);
gpio_set_level(GPIO_BAT_LED_AMBER, BAT_LED_ON);
break;
case LED_AMBER:
gpio_set_level(GPIO_BAT_LED_BLUE, BAT_LED_OFF);
gpio_set_level(GPIO_BAT_LED_AMBER, 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_BLUE] = 1;
brightness_range[EC_LED_COLOR_AMBER] = 1;
}
static int kunimitsu_led_set_color_battery(enum led_color color)
{
return bat_led_set_color(color);
}
static int kunimitsu_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 = kunimitsu_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_BLUE] != 0 &&
brightness[EC_LED_COLOR_AMBER] != 0)
kunimitsu_led_set_color(led_id, LED_PURPLE);
else if (brightness[EC_LED_COLOR_BLUE] != 0)
kunimitsu_led_set_color(led_id, LED_BLUE);
else if (brightness[EC_LED_COLOR_AMBER] != 0)
kunimitsu_led_set_color(led_id, LED_AMBER);
else
kunimitsu_led_set_color(led_id, LED_OFF);
return EC_SUCCESS;
}
static void kunimitsu_led_set_battery(void)
{
static int battery_ticks;
uint32_t chflags = charge_get_flags();
battery_ticks++;
/* BAT LED behavior:
* Same as the chromeos spec
* Green/Amber for CHARGE_FLAG_FORCE_IDLE
*/
switch (charge_get_state()) {
case PWR_STATE_CHARGE:
kunimitsu_led_set_color_battery(LED_PURPLE);
break;
case PWR_STATE_DISCHARGE:
/* Less than 3%, blink one second every two second */
if (charge_get_percent() < CRITICAL_LOW_BATTERY_PERCENTAGE)
kunimitsu_led_set_color_battery(
(battery_ticks % LED_TOTAL_2SECS_TICKS <
LED_ON_1SEC_TICKS) ? LED_PURPLE : LED_OFF);
/* Less than 10%, blink one second every four seconds */
else if (charge_get_percent() < LOW_BATTERY_PERCENTAGE)
kunimitsu_led_set_color_battery(
(battery_ticks % LED_TOTAL_4SECS_TICKS <
LED_ON_1SEC_TICKS) ? LED_PURPLE : LED_OFF);
else
kunimitsu_led_set_color_battery(LED_OFF);
break;
case PWR_STATE_ERROR:
kunimitsu_led_set_color_battery(
(battery_ticks % LED_TOTAL_2SECS_TICKS <
LED_ON_1SEC_TICKS) ? LED_AMBER : LED_OFF);
break;
case PWR_STATE_CHARGE_NEAR_FULL:
kunimitsu_led_set_color_battery(LED_BLUE);
break;
case PWR_STATE_IDLE: /* External power connected in IDLE */
if (chflags & CHARGE_FLAG_FORCE_IDLE)
kunimitsu_led_set_color_battery(
(battery_ticks % LED_TOTAL_4SECS_TICKS <
LED_ON_2SECS_TICKS) ? LED_AMBER : LED_BLUE);
else
kunimitsu_led_set_color_battery(LED_BLUE);
break;
default:
/* Other states don't alter LED behavior */
break;
}
}
/** * Called by hook task every 1 sec */
static void led_second(void)
{
if (led_auto_control_is_enabled(EC_LED_ID_BATTERY_LED))
kunimitsu_led_set_battery();
}
DECLARE_HOOK(HOOK_SECOND, led_second, HOOK_PRIO_DEFAULT);

View File

@@ -1532,6 +1532,7 @@ enum ec_led_colors {
EC_LED_COLOR_BLUE,
EC_LED_COLOR_YELLOW,
EC_LED_COLOR_WHITE,
EC_LED_COLOR_AMBER,
EC_LED_COLOR_COUNT
};