summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorParth Malkan <parthmalkan@google.com>2022-05-25 18:02:06 -0700
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-06-03 01:29:41 +0000
commit8f2fa112010f6f52413975c72e7a22a3ecdbfdbe (patch)
tree18f5bbd56259d6968e5043e20ec185a158e1aade
parent18fc3aab5262dc0e66f0b84f4feaebbacfd818a1 (diff)
downloadchrome-ec-8f2fa112010f6f52413975c72e7a22a3ecdbfdbe.tar.gz
zephyr: LED: Define battery-level property as int range
Currently battery-level property is defined using enums. This patch changes this to use int range instead. BRANCH=None BUG=b:233953617 TEST=Manual test on Lazor by modifying nodes to include battery-level range, use battfake to set different levels and observe LED behavior. Cq-Depend: chromium:3669089, chromium:3669090 Signed-off-by: Parth Malkan <parthmalkan@google.com> Change-Id: Idfe516addfe20d5141e5106afd338b49f10a6349 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3669088 Reviewed-by: Wai-Hong Tam <waihong@google.com>
-rw-r--r--include/battery.h24
-rw-r--r--zephyr/dts/bindings/leds/cros-ec,led-colors.yaml11
-rw-r--r--zephyr/include/dt-bindings/battery.h20
-rw-r--r--zephyr/shim/src/led_driver/led.c31
4 files changed, 62 insertions, 24 deletions
diff --git a/include/battery.h b/include/battery.h
index 7817794e98..9daf1ce981 100644
--- a/include/battery.h
+++ b/include/battery.h
@@ -12,12 +12,15 @@
#include "compiler.h"
#include "host_command.h"
-/* Battery index, only used with CONFIG_BATTERY_V2. */
-enum battery_index {
- BATT_IDX_INVALID = -1,
- BATT_IDX_MAIN = 0,
- BATT_IDX_BASE = 1,
-};
+/*
+ * If compiling with Zephyr, include the BATTERY_LEVEL_ definitions that are
+ * shared with device tree
+ */
+#ifdef CONFIG_ZEPHYR
+
+#include "dt-bindings/battery.h"
+
+#else /* !CONFIG_ZEPHYR */
/* Stop charge when charging and battery level >= this percentage */
#define BATTERY_LEVEL_FULL 100
@@ -47,9 +50,18 @@ enum battery_index {
*/
#define BATTERY_LEVEL_SHUTDOWN 3
+#endif /* CONFIG_ZEPHYR */
+
/* Full-capacity change reqd for host event */
#define LFCC_EVENT_THRESH 5
+/* Battery index, only used with CONFIG_BATTERY_V2. */
+enum battery_index {
+ BATT_IDX_INVALID = -1,
+ BATT_IDX_MAIN = 0,
+ BATT_IDX_BASE = 1,
+};
+
/*
* Sometimes we have hardware to detect battery present, sometimes we have to
* wait until we've been able to talk to the battery.
diff --git a/zephyr/dts/bindings/leds/cros-ec,led-colors.yaml b/zephyr/dts/bindings/leds/cros-ec,led-colors.yaml
index 76b6ac527c..cd10ca9a60 100644
--- a/zephyr/dts/bindings/leds/cros-ec,led-colors.yaml
+++ b/zephyr/dts/bindings/leds/cros-ec,led-colors.yaml
@@ -47,6 +47,15 @@ child-binding:
- POWER_S3
- POWER_S5
+ batt-lvl:
+ description: If the LED color depends on current battery level, this property
+ is used to describe the batt_lvl range using closed interval [x,y].
+ Use the macros defined in dt-bindings/battery.h.
+ e.g. <BATTERY_LEVEL_EMPTY BATTERY_LEVEL_LOW> describes battery level
+ range of BATTERY_LEVEL_EMPTY to BATTERY_LEVEL_LOW inclusive.
+ type: array
+ required: false
+
extra-flag:
description: If the LED color depends on additional factors
type: string
@@ -55,8 +64,6 @@ child-binding:
- NONE
- LED_CHFLAG_FORCE_IDLE
- LED_CHFLAG_DEFAULT
- - LED_BATT_BELOW_10_PCT
- - LED_BATT_ABOVE_10_PCT
child-binding:
description: Color enum
diff --git a/zephyr/include/dt-bindings/battery.h b/zephyr/include/dt-bindings/battery.h
new file mode 100644
index 0000000000..c87de79b45
--- /dev/null
+++ b/zephyr/include/dt-bindings/battery.h
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2022 The ChromiumOS Authors.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#ifndef DT_BINDINGS_BATTERY_H_
+#define DT_BINDINGS_BATTERY_H_
+
+/*
+ * Macros used by LED devicetree files (led.dts) to define battery-level
+ * range.
+ */
+#define BATTERY_LEVEL_EMPTY 0
+#define BATTERY_LEVEL_SHUTDOWN 3
+#define BATTERY_LEVEL_CRITICAL 5
+#define BATTERY_LEVEL_LOW 10
+#define BATTERY_LEVEL_NEAR_FULL 97
+#define BATTERY_LEVEL_FULL 100
+
+#endif /* DT_BINDINGS_BATTERY_H_ */
diff --git a/zephyr/shim/src/led_driver/led.c b/zephyr/shim/src/led_driver/led.c
index 5b130b709a..127c786ddf 100644
--- a/zephyr/shim/src/led_driver/led.c
+++ b/zephyr/shim/src/led_driver/led.c
@@ -35,8 +35,6 @@ enum led_extra_flag_t {
NONE = 0,
LED_CHFLAG_FORCE_IDLE,
LED_CHFLAG_DEFAULT,
- LED_BATT_BELOW_10_PCT,
- LED_BATT_ABOVE_10_PCT,
};
#define DECLARE_PINS_NODE(id) \
@@ -67,7 +65,8 @@ struct node_prop_t {
enum charge_state pwr_state;
enum power_state chipset_state;
enum led_extra_flag_t led_extra_flag;
- int charge_port;
+ int8_t batt_lvl[2];
+ int8_t charge_port;
struct led_color_node_t led_colors[MAX_COLOR];
};
@@ -111,10 +110,13 @@ struct node_prop_t {
{ \
.pwr_state = GET_PROP(state_id, charge_state), \
.chipset_state = GET_PROP(state_id, chipset_state), \
+ .led_extra_flag = GET_PROP(state_id, extra_flag), \
+ .batt_lvl = COND_CODE_1( \
+ DT_NODE_HAS_PROP(state_id, batt_lvl), \
+ (DT_PROP(state_id, batt_lvl)), ({-1, -1})), \
.charge_port = COND_CODE_1( \
DT_NODE_HAS_PROP(state_id, charge_port), \
(DT_PROP(state_id, charge_port)), (-1)), \
- .led_extra_flag = GET_PROP(state_id, extra_flag), \
.led_colors = {LED_COLOR_INIT(0, 1, state_id), \
LED_COLOR_INIT(1, 2, state_id), \
LED_COLOR_INIT(2, 3, state_id), \
@@ -164,18 +166,6 @@ static bool find_node_with_extra_flag(int i)
found_node = true;
}
break;
- case LED_BATT_BELOW_10_PCT:
- case LED_BATT_ABOVE_10_PCT:
- if (charge_get_percent() < 10) {
- if (node_array[i].led_extra_flag ==
- LED_BATT_BELOW_10_PCT)
- found_node = true;
- } else {
- if (node_array[i].led_extra_flag ==
- LED_BATT_ABOVE_10_PCT)
- found_node = true;
- }
- break;
default:
LOG_ERR("Invalid led extra flag %d",
node_array[i].led_extra_flag);
@@ -259,6 +249,15 @@ static int match_node(int node_idx)
return -1;
}
+ /* Check if this node depends on battery level */
+ if (node_array[node_idx].batt_lvl[0] != -1) {
+ int curr_batt_lvl = charge_get_percent();
+
+ if ((curr_batt_lvl < node_array[node_idx].batt_lvl[0]) ||
+ (curr_batt_lvl > node_array[node_idx].batt_lvl[1]))
+ return -1;
+ }
+
/* Check if the node depends on any special flags */
if (node_array[node_idx].led_extra_flag != NONE)
if (!find_node_with_extra_flag(node_idx))