summaryrefslogtreecommitdiff
path: root/baseboard/octopus
diff options
context:
space:
mode:
authorDiana Z <dzigterman@chromium.org>2018-06-14 08:32:08 -0600
committerchrome-bot <chrome-bot@chromium.org>2018-06-15 15:26:29 -0700
commit81be36bfe7f242c029f08c1f053c0736a472648d (patch)
tree4aa5c1b8f71e352cde35fca134c44f984beb67ae /baseboard/octopus
parentaa88b43aa684a0eeda6d94dba1dd58fbb5811e69 (diff)
downloadchrome-ec-81be36bfe7f242c029f08c1f053c0736a472648d.tar.gz
octopus: move LED battery state functions to common code
Currently on other platforms, each board creates its own LED charging and power state functions (unless they use the std LEDs). In order to reduce the amount of repeated code between the different octopus boards, the LED power states and corresponding logic have been put into a common file which all boards can use for their LED tasks. Individual boards will still need to implement the LED common functions and their tables to indicate what their LED behavior is. This is implemented for yorp's LED only currently. BRANCH=none BUG=none TEST=make buildall, loaded EC code onto yorp to ensure LEDs still flash as they did before Change-Id: I05b704363e213306717d9728111477d6d7743a92 Signed-off-by: Diana Z <dzigterman@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1101167 Reviewed-by: Jett Rink <jettrink@chromium.org>
Diffstat (limited to 'baseboard/octopus')
-rw-r--r--baseboard/octopus/build.mk1
-rw-r--r--baseboard/octopus/led_states.c117
-rw-r--r--baseboard/octopus/led_states.h57
3 files changed, 175 insertions, 0 deletions
diff --git a/baseboard/octopus/build.mk b/baseboard/octopus/build.mk
index 3189d7cb07..bbe8af8e1f 100644
--- a/baseboard/octopus/build.mk
+++ b/baseboard/octopus/build.mk
@@ -7,6 +7,7 @@
#
baseboard-y=baseboard.o
+baseboard-$(CONFIG_LED_COMMON)+=led_states.o
baseboard-$(CONFIG_BATTERY_SMART)+=battery.o
baseboard-$(CONFIG_USB_POWER_DELIVERY)+=usb_pd_policy.o
baseboard-$(VARIANT_OCTOPUS_EC_NPCX796FB)+=variant_ec_npcx796fb.o
diff --git a/baseboard/octopus/led_states.c b/baseboard/octopus/led_states.c
new file mode 100644
index 0000000000..40d2ebefc8
--- /dev/null
+++ b/baseboard/octopus/led_states.c
@@ -0,0 +1,117 @@
+/* Copyright 2018 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 state control for octopus boards
+ */
+
+#include "battery.h"
+#include "charge_state.h"
+#include "chipset.h"
+#include "ec_commands.h"
+#include "extpower.h"
+#include "hooks.h"
+#include "led_common.h"
+#include "led_states.h"
+
+static enum led_states led_get_state(void)
+{
+ enum led_states new_state = LED_NUM_STATES;
+
+ switch (charge_get_state()) {
+ case PWR_STATE_CHARGE:
+ new_state = STATE_CHARGING;
+ /* TODO(b/110086152): additional charging states for phasor */
+ break;
+ case PWR_STATE_DISCHARGE_FULL:
+ if (extpower_is_present()) {
+ new_state = STATE_CHARGING_FULL_CHARGE;
+ break;
+ }
+ /* Intentional fall-through */
+ case PWR_STATE_DISCHARGE /* and PWR_STATE_DISCHARGE_FULL */:
+ if (chipset_in_state(CHIPSET_STATE_ON))
+ new_state = STATE_DISCHARGE_S0;
+ else if (chipset_in_state(CHIPSET_STATE_ANY_SUSPEND))
+ new_state = STATE_DISCHARGE_S3;
+ else
+ new_state = STATE_DISCHARGE_S5;
+ break;
+ case PWR_STATE_ERROR:
+ new_state = STATE_BATTERY_ERROR;
+ break;
+ case PWR_STATE_CHARGE_NEAR_FULL:
+ new_state = STATE_CHARGING_FULL_CHARGE;
+ break;
+ case PWR_STATE_IDLE: /* External power connected in IDLE */
+ new_state = STATE_DISCHARGE_S0;
+ break;
+ default:
+ /* Other states don't alter LED behavior */
+ break;
+ }
+
+ return new_state;
+}
+
+static void led_update_battery(void)
+{
+ static uint8_t ticks, period;
+ static int led_state = STATE_DEFAULT;
+ int phase;
+ enum led_states desired_state = led_get_state();
+
+ if (desired_state == led_state && period == LED_INDEFINITE)
+ /*
+ * No change needed if we're on the same state and
+ * it's configured to be solid
+ */
+ return;
+
+ /*
+ * If we're in a new valid state, update our ticks and period info.
+ * If our new state isn't defined, continue using the previous one
+ */
+ if (desired_state != led_state && desired_state < LED_NUM_STATES) {
+ /* State is changing */
+ led_state = desired_state;
+ /* Reset ticks and period when state changes */
+ ticks = 0;
+
+ period = led_bat_state_table[led_state][LED_PHASE_0].time +
+ led_bat_state_table[led_state][LED_PHASE_1].time;
+
+ }
+
+ /*
+ * Determine which phase of the state table to use. The phase is
+ * determined if it falls within first phase time duration.
+ */
+ phase = ticks < led_bat_state_table[led_state][LED_PHASE_0].time ?
+ 0 : 1;
+ ticks = (ticks + 1) % period;
+
+ /* Set the color for the given state and phase */
+ led_set_color_battery(led_bat_state_table[led_state][phase].color);
+}
+
+static void led_init(void)
+{
+ /* If battery LED is enabled, set it to "off" to start with */
+ if (led_auto_control_is_enabled(EC_LED_ID_BATTERY_LED))
+ led_set_color_battery(LED_OFF);
+}
+DECLARE_HOOK(HOOK_INIT, led_init, HOOK_PRIO_DEFAULT);
+
+/* Called by hook task every hook tick (200 msec) */
+static void led_update(void)
+{
+ /*
+ * If battery LED is enabled, set its state based on our power and
+ * charge
+ */
+ if (led_auto_control_is_enabled(EC_LED_ID_BATTERY_LED))
+ led_update_battery();
+ /* TODO(b/110084784): add power LED support for phasor */
+}
+DECLARE_HOOK(HOOK_TICK, led_update, HOOK_PRIO_DEFAULT);
diff --git a/baseboard/octopus/led_states.h b/baseboard/octopus/led_states.h
new file mode 100644
index 0000000000..f66c5adc70
--- /dev/null
+++ b/baseboard/octopus/led_states.h
@@ -0,0 +1,57 @@
+/* Copyright 2018 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.
+ *
+ * Common functions for stateful LEDs (charger and power)
+ */
+
+#ifndef __CROS_EC_BASEBOARD_LED_H
+#define __CROS_EC_BASEBOARD_LED_H
+
+#include "ec_commands.h"
+
+#define LED_INDEFINITE UINT8_MAX
+#define LED_ONE_SEC (1000 / HOOK_TICK_INTERVAL_MS)
+#define STATE_DEFAULT LED_NUM_STATES
+#define LED_OFF EC_LED_COLOR_COUNT
+
+/*
+ * All LED states should have one phase defined,
+ * and an additional phase can be defined for blinking
+ */
+enum led_phase {
+ LED_PHASE_0,
+ LED_PHASE_1,
+ LED_NUM_PHASES
+};
+
+enum led_states {
+ STATE_CHARGING,
+ /* TODO(b/110086152): more charging states for phasor */
+ STATE_CHARGING_FULL_CHARGE,
+ STATE_DISCHARGE_S0,
+ STATE_DISCHARGE_S3,
+ STATE_DISCHARGE_S5,
+ STATE_BATTERY_ERROR,
+ LED_NUM_STATES
+};
+
+struct led_descriptor {
+ enum ec_led_colors color;
+ uint8_t time;
+};
+
+
+/* Charging LED state table - defined in board's led.c */
+extern const struct led_descriptor
+ led_bat_state_table[LED_NUM_STATES][LED_NUM_PHASES];
+
+/**
+ * Set battery LED color - defined in board's led.c
+ *
+ * @param color Color to set on battery LED
+ *
+ */
+void led_set_color_battery(enum ec_led_colors color);
+
+#endif /* __CROS_EC_BASEBOARD_LED_H */