From 812b3f8cb692ae5f9c616f570e5973e9c66eddcb Mon Sep 17 00:00:00 2001 From: Rong Chang Date: Mon, 6 Feb 2012 20:51:48 -0800 Subject: [PATCH] Initial bq24725 charger driver import Battery charging state machine contains many file changes. This is the 1st part of the break down. Refactor original test code into board dummy driver. Normalize charger API. And import link's charger IC driver. Signed-off-by: Rong Chang BUG=chrome-os-partner:7855 TEST=build without warning and error BOARD=bds make BOARD=link make BOARD=discovery make Change-Id: I34b6e9862a45331378916bc77653d4adb22ca548 --- board/bds/build.mk | 5 + .../charger.c => board/bds/dummy_charger.c | 22 +- board/link/board.h | 2 + chip/lm4/build.mk | 1 - common/build.mk | 3 + common/charger_bq24725.c | 226 ++++++++++++++++++ common/charger_bq24725.h | 53 ++++ common/smart_battery.h | 58 +++++ include/charger.h | 44 ++++ 9 files changed, 398 insertions(+), 16 deletions(-) rename chip/lm4/charger.c => board/bds/dummy_charger.c (89%) create mode 100644 common/charger_bq24725.c create mode 100644 common/charger_bq24725.h create mode 100644 common/smart_battery.h diff --git a/board/bds/build.mk b/board/bds/build.mk index c3fae48c4b..73f193751f 100644 --- a/board/bds/build.mk +++ b/board/bds/build.mk @@ -1,8 +1,13 @@ +# 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. # # Board specific files build +# # the IC is TI Stellaris LM4 CHIP:=lm4 board-y=board.o +board-$(CONFIG_CHARGER)+=dummy_charger.o board-$(CONFIG_TEMP_SENSOR)+=board_temp_sensor.o diff --git a/chip/lm4/charger.c b/board/bds/dummy_charger.c similarity index 89% rename from chip/lm4/charger.c rename to board/bds/dummy_charger.c index e603e63453..c4ee308e1c 100644 --- a/chip/lm4/charger.c +++ b/board/bds/dummy_charger.c @@ -3,16 +3,10 @@ * found in the LICENSE file. */ -/* Charger/battery debug command module for Chrome EC */ - -/* TODO: remove this or merge into charger/battery modules - * once charger and battery modules are ready. - */ - -#include "charger.h" #include "board.h" -#include "i2c.h" +#include "charger.h" #include "console.h" +#include "i2c.h" #include "uart.h" #include "util.h" @@ -22,6 +16,11 @@ /* Address of battery */ #define BATTERY_ADDR 0x16 +int charger_init(void) +{ + return EC_SUCCESS; +} + /*****************************************************************************/ /* Console commands */ @@ -87,10 +86,3 @@ static int command_battery(int argc, char **argv) } DECLARE_CONSOLE_COMMAND(battery, command_battery); -/*****************************************************************************/ -/* Initialization */ - -int charger_init(void) -{ - return EC_SUCCESS; -} diff --git a/board/link/board.h b/board/link/board.h index 33491ed032..37334a591b 100644 --- a/board/link/board.h +++ b/board/link/board.h @@ -58,6 +58,8 @@ enum adc_channel /* Charger module */ #define CONFIG_CHARGER_BQ24725 +#define CONFIG_BQ24725_R_SNS 10 /* 10 mOhm charge sense resistor */ +#define CONFIG_BQ24725_R_AC 20 /* 20 mOhm input current sense resistor */ /* I2C ports */ #define I2C_PORT_BATTERY 0 diff --git a/chip/lm4/build.mk b/chip/lm4/build.mk index 7a2053b2f9..028eeecc3f 100644 --- a/chip/lm4/build.mk +++ b/chip/lm4/build.mk @@ -16,4 +16,3 @@ chip-$(CONFIG_LPC)+=lpc.o chip-$(CONFIG_PWM)+=pwm.o chip-$(CONFIG_TEMP_SENSOR)+=chip_temp_sensor.o chip-$(CONFIG_TASK_KEYSCAN)+=keyboard_scan.o -chip-$(CONFIG_CHARGER)+=charger.o diff --git a/common/build.mk b/common/build.mk index 9c25397569..564cfe80dd 100644 --- a/common/build.mk +++ b/common/build.mk @@ -15,3 +15,6 @@ common-$(CONFIG_TASK_X86POWER)+=x86_power.o common-$(CONFIG_FLASH)+=flash_commands.o common-$(CONFIG_PWM)+=pwm_commands.o common-$(CONFIG_TEMP_SENSOR)+=temp_sensor.o temp_sensor_commands.o + +# Board driver modules +common-$(CONFIG_CHARGER_BQ24725)+=charger_bq24725.o diff --git a/common/charger_bq24725.c b/common/charger_bq24725.c new file mode 100644 index 0000000000..e47d3829f7 --- /dev/null +++ b/common/charger_bq24725.c @@ -0,0 +1,226 @@ +/* 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. + * + * TI bq24725 battery charger driver. + */ + +#include "board.h" +#include "charger.h" +#include "charger_bq24725.h" +#include "console.h" +#include "common.h" +#include "i2c.h" +#include "smart_battery.h" +#include "uart.h" +#include "util.h" + +/* Sense resistor configurations and macros */ +#define DEFAULT_SENSE_RESISTOR 10 +#define R_SNS CONFIG_BQ24725_R_SNS +#define R_AC CONFIG_BQ24725_R_AC +#define REG_TO_CURRENT(REG, RS) ((REG) * (RS) / DEFAULT_SENSE_RESISTOR) +#define CURRENT_TO_REG(CUR, RS) ((CUR) * DEFAULT_SENSE_RESISTOR / (RS)) + +/* Charger infomation + * charge voltage bitmask: 0111 1111 1111 0000 + * charge current bitmask: 0001 1111 1000 0000 + * input current bitmask : 0000 0000 1000 0000 + */ +static const struct charger_info bq24725_charger_info = { + .name = "bq24725", + .voltage_max = 19200, + .voltage_min = 1024, + .voltage_step = 16, + .current_max = REG_TO_CURRENT(8128, R_SNS), + .current_min = REG_TO_CURRENT(128, R_SNS), + .current_step = REG_TO_CURRENT(128, R_SNS), + .input_current_max = REG_TO_CURRENT(8064, R_AC), + .input_current_min = REG_TO_CURRENT(128, R_AC), + .input_current_step = REG_TO_CURRENT(128, R_AC), +}; + +/* bq24725 specific interfaces */ + +static int charger_set_input_current(int input_current) +{ + return i2c_write16(I2C_PORT_CHARGER, CHARGER_ADDR, + BQ24725_INPUT_CURRENT, CURRENT_TO_REG(input_current, R_AC)); +} + +static int charger_get_input_current(int *input_current) +{ + int rv; + int reg; + + rv = i2c_read16(I2C_PORT_CHARGER, CHARGER_ADDR, + BQ24725_INPUT_CURRENT, ®); + if (rv) + return rv; + + *input_current = REG_TO_CURRENT(reg, R_AC); + + return EC_SUCCESS; +} + +static int charger_manufacturer_id(int *id) +{ + return i2c_read16(I2C_PORT_CHARGER, CHARGER_ADDR, + BQ24725_MANUFACTURE_ID, id); +} + +static int charger_device_id(int *id) +{ + return i2c_read16(I2C_PORT_CHARGER, CHARGER_ADDR, + BQ24725_DEVICE_ID, id); +} + +static int charger_get_option(int *option) +{ + return i2c_read16(I2C_PORT_CHARGER, CHARGER_ADDR, + BQ24725_CHARGE_OPTION, option); +} + +static int charger_set_option(int option) +{ + return i2c_write16(I2C_PORT_CHARGER, CHARGER_ADDR, + BQ24725_CHARGE_OPTION, option); +} + +/* charger interfaces */ +const struct charger_info *charger_get_info(void) +{ + return &bq24725_charger_info; +} + +int charger_get_status(int *status) +{ + int rv; + int option; + + rv = charger_get_option(&option); + if (rv) + return rv; + + /* Default status */ + *status = CHARGER_LEVEL_2; + + if (option & OPTION_CHARGE_INHIBIT) + *status |= CHARGER_CHARGE_INHIBITED; + + return EC_SUCCESS; +} + +int charger_set_mode(int mode) +{ + int rv; + int option; + + rv = charger_get_option(&option); + if (rv) + return rv; + + if (mode & CHARGE_FLAG_INHIBIT_CHARGE) + option |= OPTION_CHARGE_INHIBIT; + else + option &= ~OPTION_CHARGE_INHIBIT; + return charger_set_option(option); +} + +int charger_get_current(int *current) +{ + int rv; + int reg; + + rv = i2c_read16(I2C_PORT_CHARGER, CHARGER_ADDR, + SB_CHARGING_CURRENT, ®); + if (rv) + return rv; + + *current = REG_TO_CURRENT(reg, R_SNS); + return EC_SUCCESS; +} + +int charger_set_current(int current) +{ + return i2c_write16(I2C_PORT_CHARGER, CHARGER_ADDR, + SB_CHARGING_CURRENT, CURRENT_TO_REG(current, R_SNS)); +} + +int charger_get_voltage(int *voltage) +{ + return i2c_read16(I2C_PORT_CHARGER, CHARGER_ADDR, + SB_CHARGING_VOLTAGE, voltage); +} + +int charger_set_voltage(int voltage) +{ + return i2c_write16(I2C_PORT_CHARGER, CHARGER_ADDR, + SB_CHARGING_VOLTAGE, voltage); +} + +/* Initialization */ +int charger_init(void) +{ + /* bq24725 power on reset state: + * charger watch dog timer = 175sec + * charger input current limit = 4096 * 10 / RS_AC + */ + + return charger_set_input_current(4096); +} + + +/* Console commands */ + +static int command_charger(int argc, char **argv) +{ + int rv; + int d; + const struct charger_info *info; + + uart_puts("Reading battery properties : now (max, min, step)\n"); + + /* info */ + info = charger_get_info(); + uart_printf(" chip name : %s\n", info->name); + + /* manufacturer id */ + rv = charger_manufacturer_id(&d); + if (rv) + return rv; + uart_printf(" manufacturer id: 0x%04X\n"); + + /* device id */ + rv = charger_device_id(&d); + if (rv) + return rv; + uart_printf(" device id : 0x%04X\n"); + + /* charge voltage limit */ + rv = charger_get_voltage(&d); + if (rv) + return rv; + uart_printf(" voltage : %d (%d, %d, %d)\n", d, + info->voltage_max, info->voltage_min, info->voltage_step); + + /* charge current limit */ + rv = charger_get_current(&d); + if (rv) + return rv; + uart_printf(" current : %d (%d, %d, %d)\n", d, + info->current_max, info->current_min, info->current_step); + + /* input current limit */ + rv = charger_get_input_current(&d); + if (rv) + return rv; + + uart_printf(" input : %d (%d, %d, %d)\n", d, + info->input_current_max, info->input_current_min, + info->input_current_step); + + return EC_SUCCESS; +} +DECLARE_CONSOLE_COMMAND(charger, command_charger); + diff --git a/common/charger_bq24725.h b/common/charger_bq24725.h new file mode 100644 index 0000000000..ab0a4324b3 --- /dev/null +++ b/common/charger_bq24725.h @@ -0,0 +1,53 @@ +/* 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. + * + * TI bq24725 battery charger driver. + */ + +#ifndef __CROS_EC_CHARGER_BQ24725_H +#define __CROS_EC_CHARGER_BQ24725_H + +/* I2C address */ +#define CHARGER_ADDR 0x12 + +/* Chip specific commands */ +#define BQ24725_CHARGE_OPTION 0x12 +#define BQ24725_INPUT_CURRENT 0x3f +#define BQ24725_MANUFACTURE_ID 0xfe +#define BQ24725_DEVICE_ID 0xff + +/* ChargeOption 0x12 */ +#define OPTION_CHARGE_INHIBIT (1 << 0) +#define OPTION_ACOC_THRESHOLD (3 << 1) +#define OPTION_IOUT_SELECTION (1 << 5) +#define OPTION_LEARN_ENABLE (1 << 6) +#define OPTION_IFAULT_HI_THRESHOLD (3 << 7) +#define OPTION_EMI_FREQ_ENABLE (1 << 9) +#define OPTION_EMI_FREQ_ADJ (1 << 10) +#define OPTION_BAT_DEPLETION_THRESHOLD (3 << 11) +#define OPTION_WATCHDOG_TIMER (3 << 13) +#define OPTION_AOC_DELITCH_TIME (1 << 15) +/* OPTION_ACOC_THRESHOLD */ +#define ACOC_THRESHOLD_DISABLE (0 << 1) +#define ACOC_THRESHOLD_133X (1 << 1) +#define ACOC_THRESHOLD_166X_DEFAULT (2 << 1) +#define ACOC_THRESHOLD_222X (3 << 1) +/* OPTION_IFAULT_HI_THRESHOLD */ +#define IFAULT_THRESHOLD_300MV (0 << 7) +#define IFAULT_THRESHOLD_500MV (1 << 7) +#define IFAULT_THRESHOLD_700MV_DEFAULT (2 << 7) +#define IFAULT_THRESHOLD_900MV (3 << 7) +/* OPTION_BAT_DEPLETION_THRESHOLD */ +#define FALLING_THRESHOLD_5919 (0 << 11) +#define FALLING_THRESHOLD_6265 (1 << 11) +#define FALLING_THRESHOLD_6655 (2 << 11) +#define FALLING_THRESHOLD_7097_DEFAULT (3 << 11) +/* OPTION_WATCHDOG_TIMER */ +#define CHARGE_WATCHDOG_DISABLE (0 << 13) +#define CHARGE_WATCHDOG_44SEC (1 << 13) +#define CHARGE_WATCHDOG_88SEC (2 << 13) +#define CHARGE_WATCHDOG_175SEC_DEFAULT (3 << 13) + +#endif /* __CROS_EC_CHARGER_BQ24725_H */ + diff --git a/common/smart_battery.h b/common/smart_battery.h new file mode 100644 index 0000000000..0aa91f6bff --- /dev/null +++ b/common/smart_battery.h @@ -0,0 +1,58 @@ +/* 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. + * + * Smart battery charger 1.1 + */ +#ifndef __CROS_EC_SMART_BATTERY_H +#define __CROS_EC_SMART_BATTERY_H + +/* Smart battery charger functions */ +#define SB_CHARGER_SPEC_INFO 0x11 +#define SB_CHARGE_MODE 0x12 +#define SB_CHARGER_STATUS 0x13 +#define SB_CHARGING_CURRENT 0x14 +#define SB_CHARGING_VOLTAGE 0x15 +#define SB_ALARM_WARNING 0x16 + +/* SB_ALARM_WARNING */ +#define ALARM_OVER_CHARGE 0x8000 +#define ALARM_TERMINATE_CHARG 0x4000 +#define ALARM_RESERVED_2000 0x2000 +#define ALARM_OVER_TEMP 0x1000 +#define ALARM_TERMINATE_DISCHARGE 0x0800 +#define ALARM_RESERVED_0400 0x0400 +#define ALARM_REMAINING_CAPACITY 0x0200 +#define ALARM_REMAINING_TIME 0x0100 +#define ALARM_STATUS_INITIALIZE 0x0080 +#define ALARM_STATUS_DISCHARGING 0x0040 +#define ALARM_STATUS_FULLY_CHARGED 0x0020 +#define ALARM_STATUS_FULLY_DISCHARG 0x0010 +/* SB_CHARGE_MODE */ +#define CHARGE_FLAG_INHIBIT_CHARGE (1 << 0) +#define CHARGE_FLAG_ENABLE_POLLING (1 << 1) +#define CHARGE_FLAG_POR_RESET (1 << 2) +#define CHARGE_FLAG_RESET_TO_ZERO (1 << 3) +/* SB_CHARGER_STATUS */ +#define CHARGER_CHARGE_INHIBITED (1 << 0) +#define CHARGER_POLLING_ENABLED (1 << 1) +#define CHARGER_VOLTAGE_NOTREG (1 << 2) +#define CHARGER_CURRENT_NOTREG (1 << 3) +#define CHARGER_LEVEL_2 (1 << 4) +#define CHARGER_LEVEL_3 (1 << 5) +#define CHARGER_CURRENT_OR (1 << 6) +#define CHARGER_VOLTAGE_OR (1 << 7) +#define CHARGER_RES_OR (1 << 8) +#define CHARGER_RES_COLD (1 << 9) +#define CHARGER_RES_HOT (1 << 10) +#define CHARGER_RES_UR (1 << 11) +#define CHARGER_ALARM_INHIBITED (1 << 12) +#define CHARGER_POWER_FAIL (1 << 13) +#define CHARGER_BATTERY_PRESENT (1 << 14) +#define CHARGER_AC_PRESENT (1 << 15) +/* SB_CHARGER_SPEC_INFO */ +#define INFO_CHARGER_SPEC(INFO) ((INFO) & 0xf) +#define INFO_SELECTOR_SUPPORT(INFO) (((INFO) >> 4) & 1) + +#endif /* __CROS_EC_SMART_BATTERY_H */ + diff --git a/include/charger.h b/include/charger.h index 5e40c37f75..19184c5389 100644 --- a/include/charger.h +++ b/include/charger.h @@ -8,6 +8,50 @@ #ifndef __CROS_EC_CHARGER_H #define __CROS_EC_CHARGER_H +#include "common.h" + +/* Charger infomation + * voltage unit: mV + * current unit: mA + */ +struct charger_info { + const char *name; + uint16_t voltage_max; + uint16_t voltage_min; + uint16_t voltage_step; + uint16_t current_max; + uint16_t current_min; + uint16_t current_step; + uint16_t input_current_max; + uint16_t input_current_min; + uint16_t input_current_step; +}; + +/* Initializes the charger, with AC input on and battery + * charging off. */ int charger_init(void); +/* Get charger information. */ +const struct charger_info *charger_get_info(void); + +/* Get smart battery charger status. Supported flags: + * CHARGER_CHARGE_INHIBITED + * CHARGER_LEVEL_2 + */ +int charger_get_status(int *status); + +/* Set smart battery charger mode. Supported mode(s): + * CHARGER_FLAG_INHIBIT_CHARGE + */ +int charger_set_mode(int mode); + +/* Get/set charge current limit in mA */ +int charger_get_current(int *current); +int charger_set_current(int current); + +/* Get/set charge voltage limit in mV */ +int charger_get_voltage(int *voltage); +int charger_set_voltage(int voltage); + #endif /* __CROS_EC_CHARGER_H */ +