summaryrefslogtreecommitdiff
path: root/common/battery.c
diff options
context:
space:
mode:
authorVic Yang <victoryang@chromium.org>2013-08-20 16:36:38 +0800
committerchrome-internal-fetch <chrome-internal-fetch@google.com>2013-09-05 07:19:59 +0000
commit93cb494a7e44864912843e63c04ae72912a1a2e4 (patch)
tree97399ed492eda3d72923bc1c6ceac977a2bf4061 /common/battery.c
parent3f2eba22c5d3e771904f6451a4b63a41cc6964cb (diff)
downloadchrome-ec-93cb494a7e44864912843e63c04ae72912a1a2e4.tar.gz
Add BQ27541 battery driver
BQ27541 is not a smart battery IC, and thus we cannot use existing smart battery driver. Let's add a driver that implements a smart-battery-like interface. The 'battery' console command is also moved to battery.c so that it can be reused by different battery driver. BUG=chrome-os-partner:22048 TEST=Type 'battery' and check the reported values are sane. TEST=Check 'battery' command works fine on Spring. BRANCH=None Change-Id: I5d1eaeb3f801478f3b9473fd43c1f2a2eda75859 Signed-off-by: Vic Yang <victoryang@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/66340
Diffstat (limited to 'common/battery.c')
-rw-r--r--common/battery.c182
1 files changed, 182 insertions, 0 deletions
diff --git a/common/battery.c b/common/battery.c
new file mode 100644
index 0000000000..f92f538727
--- /dev/null
+++ b/common/battery.c
@@ -0,0 +1,182 @@
+/* Copyright (c) 2012 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 battery command.
+ */
+
+#include "battery.h"
+#include "console.h"
+#include "timer.h"
+#include "util.h"
+
+static const char *get_error_text(int rv)
+{
+ if (rv == EC_ERROR_UNIMPLEMENTED)
+ return "(unsupported)";
+ else
+ return "(error)";
+}
+
+static void print_item_name(const char *name)
+{
+ ccprintf(" %-11s", name);
+}
+
+static int check_print_error(int rv)
+{
+ if (rv != EC_SUCCESS)
+ ccprintf("%s\n", get_error_text(rv));
+ return rv == EC_SUCCESS;
+}
+
+static int print_battery_info(void)
+{
+ int value;
+ int hour, minute;
+ char text[32];
+ const char *unit;
+
+ print_item_name("Temp:");
+ if (check_print_error(battery_temperature(&value)))
+ ccprintf("0x%04x = %.1d K (%.1d C)\n",
+ value, value, value - 2731);
+
+ print_item_name("Manuf:");
+ if (check_print_error(battery_manufacturer_name(text, sizeof(text))))
+ ccprintf("%s\n", text);
+
+ print_item_name("Device:");
+ if (check_print_error(battery_device_name(text, sizeof(text))))
+ ccprintf("%s\n", text);
+
+ print_item_name("Chem:");
+ if (check_print_error(battery_device_chemistry(text, sizeof(text))))
+ ccprintf("%s\n", text);
+
+ print_item_name("Serial:");
+ if (check_print_error(battery_serial_number(&value)))
+ ccprintf("0x%04x\n", value);
+
+ print_item_name("V:");
+ if (check_print_error(battery_voltage(&value)))
+ ccprintf("0x%04x = %d mV\n", value, value);
+
+ print_item_name("V-desired:");
+ if (check_print_error(battery_desired_voltage(&value)))
+ ccprintf("0x%04x = %d mV\n", value, value);
+
+ print_item_name("V-deisgn:");
+ if (check_print_error(battery_design_voltage(&value)))
+ ccprintf("0x%04x = %d mV\n", value, value);
+
+ print_item_name("I:");
+ if (check_print_error(battery_current(&value))) {
+ ccprintf("0x%04x = %d mA", value & 0xffff, value);
+ if (value > 0)
+ ccputs("(CHG)");
+ else if (value < 0)
+ ccputs("(DISCHG)");
+ ccputs("\n");
+ }
+
+ print_item_name("I-desired:");
+ if (check_print_error(battery_desired_current(&value)))
+ ccprintf("0x%04x = %d mA\n", value, value);
+
+ print_item_name("Mode:");
+ if (check_print_error(battery_get_battery_mode(&value)))
+ ccprintf("0x%04x\n", value);
+
+ unit = battery_is_in_10mw_mode() ? "0 mW" : " mAh";
+
+ print_item_name("Charge:");
+ if (check_print_error(battery_state_of_charge(&value)))
+ ccprintf("%d %%\n", value);
+
+ print_item_name("Abs:");
+ if (check_print_error(battery_state_of_charge_abs(&value)))
+ ccprintf("%d %%\n", value);
+
+ print_item_name("Remaining:");
+ if (check_print_error(battery_remaining_capacity(&value)))
+ ccprintf("%d%s\n", value, unit);
+
+ print_item_name("Cap-full:");
+ if (check_print_error(battery_full_charge_capacity(&value)))
+ ccprintf("%d%s\n", value, unit);
+
+ print_item_name(" Design:");
+ if (check_print_error(battery_design_capacity(&value)))
+ ccprintf("%d%s\n", value, unit);
+
+ print_item_name("Time-full:");
+ if (check_print_error(battery_time_to_full(&value))) {
+ if (value == 65535) {
+ hour = 0;
+ minute = 0;
+ } else {
+ hour = value / 60;
+ minute = value % 60;
+ }
+ ccprintf("%dh:%d\n", hour, minute);
+ }
+
+ print_item_name(" Empty:");
+ if (check_print_error(battery_time_to_empty(&value))) {
+ if (value == 65535) {
+ hour = 0;
+ minute = 0;
+ } else {
+ hour = value / 60;
+ minute = value % 60;
+ }
+ ccprintf("%dh:%d\n", hour, minute);
+ }
+
+ return 0;
+}
+
+static int command_battery(int argc, char **argv)
+{
+ int repeat = 1;
+ int rv = 0;
+ int loop;
+ int sleep_ms = 0;
+ char *e;
+
+ if (argc > 1) {
+ repeat = strtoi(argv[1], &e, 0);
+ if (*e) {
+ ccputs("Invalid repeat count\n");
+ return EC_ERROR_INVAL;
+ }
+ }
+
+ if (argc > 2) {
+ sleep_ms = strtoi(argv[2], &e, 0);
+ if (*e) {
+ ccputs("Invalid sleep ms\n");
+ return EC_ERROR_INVAL;
+ }
+ }
+
+ for (loop = 0; loop < repeat; loop++) {
+ rv = print_battery_info();
+
+ if (sleep_ms)
+ msleep(sleep_ms);
+
+ if (rv)
+ break;
+ }
+
+ if (rv)
+ ccprintf("Failed - error %d\n", rv);
+
+ return rv ? EC_ERROR_UNKNOWN : EC_SUCCESS;
+}
+DECLARE_CONSOLE_COMMAND(battery, command_battery,
+ "<repeat_count> <sleep_ms>",
+ "Print battery info",
+ NULL);