diff options
Diffstat (limited to 'driver/battery/max17055.c')
-rw-r--r-- | driver/battery/max17055.c | 72 |
1 files changed, 37 insertions, 35 deletions
diff --git a/driver/battery/max17055.c b/driver/battery/max17055.c index bb0b941937..4610be64b6 100644 --- a/driver/battery/max17055.c +++ b/driver/battery/max17055.c @@ -1,4 +1,4 @@ -/* Copyright 2017 The Chromium OS Authors. All rights reserved. +/* Copyright 2017 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. * @@ -6,6 +6,7 @@ */ #include "battery.h" +#include "builtin/assert.h" #include "console.h" #include "extpower.h" #include "hooks.h" @@ -16,13 +17,13 @@ #include "util.h" /* Console output macros */ -#define CPRINTS(format, args...) cprints(CC_CHARGER, format, ## args) +#define CPRINTS(format, args...) cprints(CC_CHARGER, format, ##args) /* * For max17055 to finish battery presence detection, this is the minimal time * we have to wait since the last POR. LSB = 175ms. */ -#define RELIABLE_BATT_DETECT_TIME 0x10 +#define RELIABLE_BATT_DETECT_TIME 0x10 /* * Convert the register values to the units that match @@ -30,50 +31,48 @@ */ /* Voltage reg value to mV */ -#define VOLTAGE_CONV(REG) ((REG * 5) >> 6) +#define VOLTAGE_CONV(REG) ((REG * 5) >> 6) /* Current reg value to mA */ -#define CURRENT_CONV(REG) (((REG * 25) >> 4) / BATTERY_MAX17055_RSENSE) +#define CURRENT_CONV(REG) (((REG * 25) >> 4) / BATTERY_MAX17055_RSENSE) /* Capacity reg value to mAh */ -#define CAPACITY_CONV(REG) (REG * 5 / BATTERY_MAX17055_RSENSE) +#define CAPACITY_CONV(REG) (REG * 5 / BATTERY_MAX17055_RSENSE) /* Time reg value to minute */ -#define TIME_CONV(REG) ((REG * 3) >> 5) +#define TIME_CONV(REG) ((REG * 3) >> 5) /* Temperature reg value to 0.1K */ -#define TEMPERATURE_CONV(REG) (((REG * 10) >> 8) + 2731) +#define TEMPERATURE_CONV(REG) (((REG * 10) >> 8) + 2731) /* Percentage reg value to 1% */ -#define PERCENTAGE_CONV(REG) (REG >> 8) +#define PERCENTAGE_CONV(REG) (REG >> 8) /* Cycle count reg value (LSB = 1%) to absolute count (100%) */ -#define CYCLE_COUNT_CONV(REG) ((REG * 5) >> 9) +#define CYCLE_COUNT_CONV(REG) ((REG * 5) >> 9) /* Useful macros */ -#define MAX17055_READ_DEBUG(offset, ptr_reg) \ - do { \ - if (max17055_read(offset, ptr_reg)) { \ - CPRINTS("%s: failed to read reg %02x", \ - __func__, offset); \ - return; \ - } \ +#define MAX17055_READ_DEBUG(offset, ptr_reg) \ + do { \ + if (max17055_read(offset, ptr_reg)) { \ + CPRINTS("%s: failed to read reg %02x", __func__, \ + offset); \ + return; \ + } \ } while (0) -#define MAX17055_WRITE_DEBUG(offset, reg) \ - do { \ - if (max17055_write(offset, reg)) { \ - CPRINTS("%s: failed to read reg %02x", \ - __func__, offset); \ - return; \ - } \ +#define MAX17055_WRITE_DEBUG(offset, reg) \ + do { \ + if (max17055_write(offset, reg)) { \ + CPRINTS("%s: failed to read reg %02x", __func__, \ + offset); \ + return; \ + } \ } while (0) static int fake_state_of_charge = -1; static int max17055_read(int offset, int *data) { - return i2c_read16(I2C_PORT_BATTERY, MAX17055_ADDR_FLAGS, - offset, data); + return i2c_read16(I2C_PORT_BATTERY, MAX17055_ADDR_FLAGS, offset, data); } static int max17055_write(int offset, int data) { - return i2c_write16(I2C_PORT_BATTERY, MAX17055_ADDR_FLAGS, - offset, data); + return i2c_write16(I2C_PORT_BATTERY, MAX17055_ADDR_FLAGS, offset, data); } /* Return 1 if the device id is correct. */ @@ -94,10 +93,13 @@ int battery_device_name(char *device_name, int buf_size) int rv; rv = max17055_read(REG_DEVICE_NAME, &dev_id); - if (!rv) - snprintf(device_name, buf_size, "0x%04x", dev_id); + if (rv != EC_SUCCESS) + return rv; - return rv; + if (snprintf(device_name, buf_size, "0x%04x", dev_id) <= 0) + return EC_ERROR_UNKNOWN; + + return EC_SUCCESS; } int battery_state_of_charge_abs(int *percent) @@ -269,7 +271,7 @@ enum battery_present battery_is_present(void) void battery_get_params(struct batt_params *batt) { int reg = 0; - struct batt_params batt_new = {0}; + struct batt_params batt_new = { 0 }; /* * Assuming the battery is responsive as long as @@ -293,7 +295,8 @@ void battery_get_params(struct batt_params *batt) batt_new.flags |= BATT_FLAG_BAD_STATE_OF_CHARGE; batt_new.state_of_charge = fake_state_of_charge >= 0 ? - fake_state_of_charge : PERCENTAGE_CONV(reg); + fake_state_of_charge : + PERCENTAGE_CONV(reg); if (max17055_read(REG_VOLTAGE, ®)) batt_new.flags |= BATT_FLAG_BAD_VOLTAGE; @@ -319,8 +322,7 @@ void battery_get_params(struct batt_params *batt) * and battery isn't full (and we read them all correctly). */ if (!(batt_new.flags & BATT_FLAG_BAD_STATE_OF_CHARGE) && - batt_new.desired_voltage && - batt_new.desired_current && + batt_new.desired_voltage && batt_new.desired_current && batt_new.state_of_charge < BATTERY_LEVEL_FULL) batt_new.flags |= BATT_FLAG_WANT_CHARGE; |