Add support for TI's Smart Battery Charger BQ24707A

This is very similar to the BQ24725. There are just enough differences to
require a separate file.

BUG=chrome-os-partner:19976
BRANCH=none
TEST=none

Nothing to test until it's enabled.

Change-Id: I3247fcfde93ac75f5f9790acadc7feca28038608
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/57811
Reviewed-by: Randall Spangler <rspangler@chromium.org>
This commit is contained in:
Bill Richardson
2013-06-06 10:03:58 -07:00
committed by ChromeBot
parent 0fdfcda963
commit 124b2f1492
6 changed files with 351 additions and 95 deletions

View File

@@ -12,8 +12,9 @@ common-y+=gpio_common.o version.o printf.o queue.o
common-$(CONFIG_BATTERY_BQ20Z453)+=battery_bq20z453.o
common-$(CONFIG_BATTERY_LINK)+=battery_link.o
common-$(CONFIG_BATTERY_SPRING)+=battery_spring.o
common-$(CONFIG_CHARGER)+=charge_state.o battery_precharge.o
common-$(CONFIG_CHARGER)+=charge_state.o battery_precharge.o charger_common.o
common-$(CONFIG_CHARGER_BQ24725)+=charger_bq24725.o
common-$(CONFIG_CHARGER_BQ24707A)+=charger_bq24707a.o
common-$(CONFIG_CHARGER_TPS65090)+=pmu_tps65090_charger.o
common-$(CONFIG_CHIPSET_GAIA)+=gaia_power.o
common-$(CONFIG_CHIPSET_X86_IVYBRIDGE)+=x86_power_ivybridge.o

187
common/charger_bq24707a.c Normal file
View File

@@ -0,0 +1,187 @@
/* 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 bq24707A battery charger driver.
*/
#include "charger.h"
#include "charger_bq24707a.h"
#include "console.h"
#include "common.h"
#include "i2c.h"
#include "smart_battery.h"
#include "util.h"
#include "printf.h"
/* Sense resistor configurations and macros */
#define DEFAULT_SENSE_RESISTOR 10
#define R_SNS CONFIG_BQ24707A_R_SNS
#define R_AC CONFIG_BQ24707A_R_AC
#define REG_TO_CURRENT(REG, RS) ((REG) * DEFAULT_SENSE_RESISTOR / (RS))
#define CURRENT_TO_REG(CUR, RS) ((CUR) * (RS) / DEFAULT_SENSE_RESISTOR)
/*
* charge voltage bitmask: 0111 1111 1111 0000
* charge current bitmask: 0001 1111 1100 0000
* input current bitmask : 0001 1111 1000 0000
*/
static const struct charger_info bq24707a_charger_info = {
.name = "bq24707A",
.voltage_max = 19200,
.voltage_min = 1024,
.voltage_step = 16,
.current_max = REG_TO_CURRENT(0x1fc0, R_SNS),
.current_min = REG_TO_CURRENT(0x40, R_SNS),
.current_step = REG_TO_CURRENT(0x40, R_SNS),
.input_current_max = REG_TO_CURRENT(0x1F80, R_AC),
.input_current_min = REG_TO_CURRENT(0x80, R_AC),
.input_current_step = REG_TO_CURRENT(0x80, R_AC),
};
/* bq24707a specific interfaces */
int charger_set_input_current(int input_current)
{
return sbc_write(BQ24707_INPUT_CURRENT,
CURRENT_TO_REG(input_current, R_AC));
}
int charger_get_input_current(int *input_current)
{
int rv;
int reg;
rv = sbc_read(BQ24707_INPUT_CURRENT, &reg);
if (rv)
return rv;
*input_current = REG_TO_CURRENT(reg, R_AC);
return EC_SUCCESS;
}
int charger_manufacturer_id(int *id)
{
return sbc_read(BQ24707_MANUFACTURE_ID, id);
}
int charger_device_id(int *id)
{
return sbc_read(BQ24707_DEVICE_ID, id);
}
int charger_get_option(int *option)
{
return sbc_read(BQ24707_CHARGE_OPTION, option);
}
int charger_set_option(int option)
{
return sbc_write(BQ24707_CHARGE_OPTION, option);
}
/* Charger interfaces */
const struct charger_info *charger_get_info(void)
{
return &bq24707a_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 = sbc_read(SB_CHARGING_CURRENT, &reg);
if (rv)
return rv;
*current = REG_TO_CURRENT(reg, R_SNS);
return EC_SUCCESS;
}
int charger_closest_current(int current)
{
const struct charger_info * const info = charger_get_info();
/*
* If the requested current is non-zero but below our minimum,
* return the minimum. See crosbug.com/p/8662.
*/
if (current > 0 && current < info->current_min)
return info->current_min;
/* Clip to max */
if (current > info->current_max)
return info->current_max;
/* Otherwise round down to nearest current step */
return current - (current % info->current_step);
}
int charger_set_current(int current)
{
current = charger_closest_current(current);
return sbc_write(SB_CHARGING_CURRENT, CURRENT_TO_REG(current, R_SNS));
}
int charger_get_voltage(int *voltage)
{
return sbc_read(SB_CHARGING_VOLTAGE, voltage);
}
int charger_set_voltage(int voltage)
{
return sbc_write(SB_CHARGING_VOLTAGE, voltage);
}
/* Charging power state initialization */
int charger_post_init(void)
{
/*
* Note: bq24725 power on reset state is:
* watch dog timer = 175 sec
* input current limit = ~1/2 maximum setting
* charging voltage = 0 mV
* charging current = 0 mA
*/
/* Set charger input current limit */
return charger_set_input_current(CONFIG_CHARGER_INPUT_CURRENT);
}

View File

@@ -40,13 +40,13 @@ static const struct charger_info bq24725_charger_info = {
/* bq24725 specific interfaces */
static int charger_set_input_current(int input_current)
int charger_set_input_current(int input_current)
{
return sbc_write(BQ24725_INPUT_CURRENT,
CURRENT_TO_REG(input_current, R_AC));
}
static int charger_get_input_current(int *input_current)
int charger_get_input_current(int *input_current)
{
int rv;
int reg;
@@ -60,22 +60,22 @@ static int charger_get_input_current(int *input_current)
return EC_SUCCESS;
}
static int charger_manufacturer_id(int *id)
int charger_manufacturer_id(int *id)
{
return sbc_read(BQ24725_MANUFACTURE_ID, id);
}
static int charger_device_id(int *id)
int charger_device_id(int *id)
{
return sbc_read(BQ24725_DEVICE_ID, id);
}
static int charger_get_option(int *option)
int charger_get_option(int *option)
{
return sbc_read(BQ24725_CHARGE_OPTION, option);
}
static int charger_set_option(int option)
int charger_set_option(int option)
{
return sbc_write(BQ24725_CHARGE_OPTION, option);
}
@@ -184,91 +184,3 @@ int charger_post_init(void)
/* Set charger input current limit */
return charger_set_input_current(CONFIG_CHARGER_INPUT_CURRENT);
}
/*****************************************************************************/
/* Console commands */
static int print_info(void)
{
int rv;
int d;
const struct charger_info *info;
/* info */
info = charger_get_info();
ccprintf("Name : %s\n", info->name);
/* option */
rv = charger_get_option(&d);
if (rv)
return rv;
ccprintf("Option: %016b (0x%04x)\n", d, d);
/* manufacturer id */
rv = charger_manufacturer_id(&d);
if (rv)
return rv;
ccprintf("Man id: 0x%04x\n", d);
/* device id */
rv = charger_device_id(&d);
if (rv)
return rv;
ccprintf("Dev id: 0x%04x\n", d);
/* charge voltage limit */
rv = charger_get_voltage(&d);
if (rv)
return rv;
ccprintf("V_batt: %5d (%4d - %5d, %3d)\n", d,
info->voltage_min, info->voltage_max, info->voltage_step);
/* charge current limit */
rv = charger_get_current(&d);
if (rv)
return rv;
ccprintf("I_batt: %5d (%4d - %5d, %3d)\n", d,
info->current_min, info->current_max, info->current_step);
/* input current limit */
rv = charger_get_input_current(&d);
if (rv)
return rv;
ccprintf("I_in : %5d (%4d - %5d, %3d)\n", d,
info->input_current_min, info->input_current_max,
info->input_current_step);
return EC_SUCCESS;
}
static int command_charger(int argc, char **argv)
{
int d;
char *e;
if (argc != 3)
return print_info();
if (strcasecmp(argv[1], "input") == 0) {
d = strtoi(argv[2], &e, 0);
if (*e)
return EC_ERROR_PARAM2;
return charger_set_input_current(d);
} else if (strcasecmp(argv[1], "current") == 0) {
d = strtoi(argv[2], &e, 0);
if (*e)
return EC_ERROR_PARAM2;
return charger_set_current(d);
} else if (strcasecmp(argv[1], "voltage") == 0) {
d = strtoi(argv[2], &e, 0);
if (*e)
return EC_ERROR_PARAM2;
return charger_set_voltage(d);
} else
return EC_ERROR_PARAM1;
}
DECLARE_CONSOLE_COMMAND(charger, command_charger,
"[input | current | voltage] [newval]",
"Get or set charger param(s)",
NULL);

104
common/charger_common.c Normal file
View File

@@ -0,0 +1,104 @@
/* Copyright (c) 2013 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 functions for battery charging.
*/
#include "charger.h"
#include "common.h"
#include "console.h"
#include "host_command.h"
#include "printf.h"
#include "smart_battery.h"
#include "util.h"
/* Console output macros */
#define CPUTS(outstr) cputs(CC_CHARGER, outstr)
#define CPRINTF(format, args...) cprintf(CC_CHARGER, format, ## args)
static int print_info(void)
{
int rv;
int d;
const struct charger_info *info;
/* info */
info = charger_get_info();
ccprintf("Name : %s\n", info->name);
/* option */
rv = charger_get_option(&d);
if (rv)
return rv;
ccprintf("Option: %016b (0x%04x)\n", d, d);
/* manufacturer id */
rv = charger_manufacturer_id(&d);
if (rv)
return rv;
ccprintf("Man id: 0x%04x\n", d);
/* device id */
rv = charger_device_id(&d);
if (rv)
return rv;
ccprintf("Dev id: 0x%04x\n", d);
/* charge voltage limit */
rv = charger_get_voltage(&d);
if (rv)
return rv;
ccprintf("V_batt: %5d (%4d - %5d, %3d)\n", d,
info->voltage_min, info->voltage_max, info->voltage_step);
/* charge current limit */
rv = charger_get_current(&d);
if (rv)
return rv;
ccprintf("I_batt: %5d (%4d - %5d, %3d)\n", d,
info->current_min, info->current_max, info->current_step);
/* input current limit */
rv = charger_get_input_current(&d);
if (rv)
return rv;
ccprintf("I_in : %5d (%4d - %5d, %3d)\n", d,
info->input_current_min, info->input_current_max,
info->input_current_step);
return EC_SUCCESS;
}
static int command_charger(int argc, char **argv)
{
int d;
char *e;
if (argc != 3)
return print_info();
if (strcasecmp(argv[1], "input") == 0) {
d = strtoi(argv[2], &e, 0);
if (*e)
return EC_ERROR_PARAM2;
return charger_set_input_current(d);
} else if (strcasecmp(argv[1], "current") == 0) {
d = strtoi(argv[2], &e, 0);
if (*e)
return EC_ERROR_PARAM2;
return charger_set_current(d);
} else if (strcasecmp(argv[1], "voltage") == 0) {
d = strtoi(argv[2], &e, 0);
if (*e)
return EC_ERROR_PARAM2;
return charger_set_voltage(d);
} else
return EC_ERROR_PARAM1;
}
DECLARE_CONSOLE_COMMAND(charger, command_charger,
"[input | current | voltage] [newval]",
"Get or set charger param(s)",
NULL);

View File

@@ -61,5 +61,13 @@ int charger_set_current(int current);
int charger_get_voltage(int *voltage);
int charger_set_voltage(int voltage);
/* Other parameters that may be charger-specific, but are common so far. */
int charger_set_input_current(int input_current);
int charger_get_input_current(int *input_current);
int charger_manufacturer_id(int *id);
int charger_device_id(int *id);
int charger_get_option(int *option);
int charger_set_option(int option);
#endif /* __CROS_EC_CHARGER_H */

View File

@@ -0,0 +1,44 @@
/* 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 bq24707A battery charger driver.
*/
#ifndef __CROS_EC_CHARGER_BQ24707A_H
#define __CROS_EC_CHARGER_BQ24707A_H
/* Chip specific commands */
#define BQ24707_CHARGE_OPTION 0x12
#define BQ24707_INPUT_CURRENT 0x3f
#define BQ24707_MANUFACTURE_ID 0xfe
#define BQ24707_DEVICE_ID 0xff
/* ChargeOption 0x12 */
#define OPTION_CHARGE_INHIBIT (1 << 0)
#define OPTION_ACOC_THRESHOLD (3 << 1)
#define OPTION_COMPARATOR_THRESHOLD (1 << 4)
#define OPTION_IOUT_SELECTION (1 << 5)
#define OPTION_IFAULT_HI_THRESHOLD (3 << 7)
#define OPTION_EMI_FREQ_ENABLE (1 << 9)
#define OPTION_EMI_FREQ_ADJ (1 << 10)
#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_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_BQ24707A_H */