summaryrefslogtreecommitdiff
path: root/common/battery_fuel_gauge.c
diff options
context:
space:
mode:
authorpoornima tom <poornima.tom@intel.com>2021-10-12 02:24:58 +0530
committerCommit Bot <commit-bot@chromium.org>2022-01-07 20:26:38 +0000
commit8e2b0d8b64a590426c1035baf319674b6351ca64 (patch)
treee733b379ea4fb962c9a92985b34b75431e056710 /common/battery_fuel_gauge.c
parent5735ad28476fe9f8c36149094afc0c57c8c4a6be (diff)
downloadchrome-ec-8e2b0d8b64a590426c1035baf319674b6351ca64.tar.gz
config: Add CONFIG_BATTERY_NO_AUTO_DETECT option
There are some battery types which cannot be identified by normal manufacturing name or device name. For devices, which uses this type of batteries can be configured with CONFIG_BATTERY_NO_AUTO_DETECT to indicate the same. Based on this config, the idea is to set the correct battery type at runtime and then use the manufacturing or device name to identify the battery later. The motivation of this change is to have a single binary for multiple platforms that have different battery configuration. BUG=b:194163586 BRANCH=none TEST=make buildall -j Signed-off-by: poornima tom <poornima.tom@intel.com> Change-Id: I488ca3d72250311630724381a8cfeb52d8dad657 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3216893 Reviewed-by: Keith Short <keithshort@chromium.org>
Diffstat (limited to 'common/battery_fuel_gauge.c')
-rw-r--r--common/battery_fuel_gauge.c110
1 files changed, 82 insertions, 28 deletions
diff --git a/common/battery_fuel_gauge.c b/common/battery_fuel_gauge.c
index 528713d68f..5555255188 100644
--- a/common/battery_fuel_gauge.c
+++ b/common/battery_fuel_gauge.c
@@ -14,11 +14,83 @@
#define CPRINTS(format, args...) cprints(CC_CHARGER, format, ## args)
+/*
+ * Authenticate the battery connected.
+ *
+ * Compare the manufacturer name read from the fuel gauge to the
+ * manufacturer names defined in the board_battery_info table. If
+ * a device name has been specified in the board_battery_info table,
+ * then both the manufacturer and device name must match.
+ */
+static bool authenticate_battery_type(int index, char *manuf_name)
+{
+ char device_name[32];
+
+ const struct fuel_gauge_info * const fuel_gauge =
+ &board_battery_info[index].fuel_gauge;
+ int len = 0;
+
+ /* check for valid index */
+ if (index >= BATTERY_TYPE_COUNT)
+ return false;
+
+ /* manufacturer name mismatch */
+ if (strcasecmp(manuf_name, fuel_gauge->manuf_name))
+ return false;
+
+ /* device name is specified in table */
+ if (fuel_gauge->device_name != NULL) {
+
+ /* Get the device name */
+ if (battery_device_name(device_name,
+ sizeof(device_name)))
+ return false;
+
+ len = strlen(fuel_gauge->device_name);
+
+ /* device name mismatch */
+ if (strncasecmp(device_name, fuel_gauge->device_name,
+ len))
+ return false;
+ }
+
+ CPRINTS("found batt:%s", fuel_gauge->manuf_name);
+ return true;
+}
+
+#ifdef CONFIG_BATTERY_TYPE_NO_AUTO_DETECT
+
+/* Variable to decide the battery type */
+static int fixed_battery_type = BATTERY_TYPE_UNINITIALIZED;
+
+/*
+ * Function to get the fixed battery type.
+ */
+static int battery_get_fixed_battery_type(void)
+{
+ if (fixed_battery_type == BATTERY_TYPE_UNINITIALIZED) {
+ CPRINTS("Warning: Battery type is not Initialized! "
+ "Setting to default battery type.\n");
+ fixed_battery_type = DEFAULT_BATTERY_TYPE;
+ }
+
+ return fixed_battery_type;
+}
+
+/*
+ * Function to set the battery type, when auto-detection cannot be used.
+ */
+void battery_set_fixed_battery_type(int type)
+{
+ if (type < BATTERY_TYPE_COUNT)
+ fixed_battery_type = type;
+}
+#endif /* CONFIG_BATTERY_TYPE_NO_AUTO_DETECT */
/* Get type of the battery connected on the board */
static int get_battery_type(void)
{
- char manuf_name[32], device_name[32];
+ char manuf_name[32];
int i;
static enum battery_type battery_type = BATTERY_TYPE_COUNT;
@@ -33,36 +105,18 @@ static int get_battery_type(void)
if (battery_manufacturer_name(manuf_name, sizeof(manuf_name)))
return battery_type;
- /*
- * Compare the manufacturer name read from the fuel gauge to the
- * manufacturer names defined in the board_battery_info table. If
- * a device name has been specified in the board_battery_info table,
- * then both the manufacturer and device name must match.
- */
+#if defined(CONFIG_BATTERY_TYPE_NO_AUTO_DETECT)
+ i = battery_get_fixed_battery_type();
+ if (authenticate_battery_type(i, manuf_name))
+ battery_type = i;
+#else
for (i = 0; i < BATTERY_TYPE_COUNT; i++) {
- const struct fuel_gauge_info * const fuel_gauge =
- &board_battery_info[i].fuel_gauge;
- int len = 0;
-
- if (strcasecmp(manuf_name, fuel_gauge->manuf_name))
- continue;
-
- if (fuel_gauge->device_name != NULL) {
-
- if (battery_device_name(device_name,
- sizeof(device_name)))
- continue;
-
- len = strlen(fuel_gauge->device_name);
- if (strncasecmp(device_name, fuel_gauge->device_name,
- len))
- continue;
+ if (authenticate_battery_type(i, manuf_name)) {
+ battery_type = i;
+ break;
}
-
- CPRINTS("found batt:%s", fuel_gauge->manuf_name);
- battery_type = i;
- break;
}
+#endif
return battery_type;
}