power: supply: core: Add battery internal resistance temperature table support

Since the battery internal resistance can be changed with the temperature
changes, thus add a resistance temperature table support to look up
the accurate battery internal resistance in a certain temperature.

Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
Signed-off-by: Baolin Wang <baolin.wang7@gmail.com>
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
This commit is contained in:
Baolin Wang 2019-12-09 11:56:22 +08:00 committed by Sebastian Reichel
parent 9a056a8791
commit 65dbad713d
2 changed files with 76 additions and 1 deletions

View File

@ -565,9 +565,11 @@ EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
int power_supply_get_battery_info(struct power_supply *psy, int power_supply_get_battery_info(struct power_supply *psy,
struct power_supply_battery_info *info) struct power_supply_battery_info *info)
{ {
struct power_supply_resistance_temp_table *resist_table;
struct device_node *battery_np; struct device_node *battery_np;
const char *value; const char *value;
int err, len, index; int err, len, index;
const __be32 *list;
info->energy_full_design_uwh = -EINVAL; info->energy_full_design_uwh = -EINVAL;
info->charge_full_design_uah = -EINVAL; info->charge_full_design_uah = -EINVAL;
@ -578,6 +580,7 @@ int power_supply_get_battery_info(struct power_supply *psy,
info->constant_charge_current_max_ua = -EINVAL; info->constant_charge_current_max_ua = -EINVAL;
info->constant_charge_voltage_max_uv = -EINVAL; info->constant_charge_voltage_max_uv = -EINVAL;
info->factory_internal_resistance_uohm = -EINVAL; info->factory_internal_resistance_uohm = -EINVAL;
info->resist_table = NULL;
for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) { for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) {
info->ocv_table[index] = NULL; info->ocv_table[index] = NULL;
@ -644,7 +647,6 @@ int power_supply_get_battery_info(struct power_supply *psy,
for (index = 0; index < len; index++) { for (index = 0; index < len; index++) {
struct power_supply_battery_ocv_table *table; struct power_supply_battery_ocv_table *table;
char *propname; char *propname;
const __be32 *list;
int i, tab_len, size; int i, tab_len, size;
propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index); propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index);
@ -677,6 +679,26 @@ int power_supply_get_battery_info(struct power_supply *psy,
} }
} }
list = of_get_property(battery_np, "resistance-temp-table", &len);
if (!list || !len)
goto out_put_node;
info->resist_table_size = len / (2 * sizeof(__be32));
resist_table = info->resist_table = devm_kcalloc(&psy->dev,
info->resist_table_size,
sizeof(*resist_table),
GFP_KERNEL);
if (!info->resist_table) {
power_supply_put_battery_info(psy, info);
err = -ENOMEM;
goto out_put_node;
}
for (index = 0; index < info->resist_table_size; index++) {
resist_table[index].temp = be32_to_cpu(*list++);
resist_table[index].resistance = be32_to_cpu(*list++);
}
out_put_node: out_put_node:
of_node_put(battery_np); of_node_put(battery_np);
return err; return err;
@ -692,9 +714,52 @@ void power_supply_put_battery_info(struct power_supply *psy,
if (info->ocv_table[i]) if (info->ocv_table[i])
devm_kfree(&psy->dev, info->ocv_table[i]); devm_kfree(&psy->dev, info->ocv_table[i]);
} }
if (info->resist_table)
devm_kfree(&psy->dev, info->resist_table);
} }
EXPORT_SYMBOL_GPL(power_supply_put_battery_info); EXPORT_SYMBOL_GPL(power_supply_put_battery_info);
/**
* power_supply_temp2resist_simple() - find the battery internal resistance
* percent
* @table: Pointer to battery resistance temperature table
* @table_len: The table length
* @ocv: Current temperature
*
* This helper function is used to look up battery internal resistance percent
* according to current temperature value from the resistance temperature table,
* and the table must be ordered descending. Then the actual battery internal
* resistance = the ideal battery internal resistance * percent / 100.
*
* Return: the battery internal resistance percent
*/
int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table,
int table_len, int temp)
{
int i, resist;
for (i = 0; i < table_len; i++)
if (temp > table[i].temp)
break;
if (i > 0 && i < table_len) {
int tmp;
tmp = (table[i - 1].resistance - table[i].resistance) *
(temp - table[i].temp);
tmp /= table[i - 1].temp - table[i].temp;
resist = tmp + table[i].resistance;
} else if (i == 0) {
resist = table[0].resistance;
} else {
resist = table[table_len - 1].resistance;
}
return resist;
}
EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple);
/** /**
* power_supply_ocv2cap_simple() - find the battery capacity * power_supply_ocv2cap_simple() - find the battery capacity
* @table: Pointer to battery OCV lookup table * @table: Pointer to battery OCV lookup table

View File

@ -325,6 +325,11 @@ struct power_supply_battery_ocv_table {
int capacity; /* percent */ int capacity; /* percent */
}; };
struct power_supply_resistance_temp_table {
int temp; /* celsius */
int resistance; /* internal resistance percent */
};
#define POWER_SUPPLY_OCV_TEMP_MAX 20 #define POWER_SUPPLY_OCV_TEMP_MAX 20
/* /*
@ -349,6 +354,8 @@ struct power_supply_battery_info {
int ocv_temp[POWER_SUPPLY_OCV_TEMP_MAX];/* celsius */ int ocv_temp[POWER_SUPPLY_OCV_TEMP_MAX];/* celsius */
struct power_supply_battery_ocv_table *ocv_table[POWER_SUPPLY_OCV_TEMP_MAX]; struct power_supply_battery_ocv_table *ocv_table[POWER_SUPPLY_OCV_TEMP_MAX];
int ocv_table_size[POWER_SUPPLY_OCV_TEMP_MAX]; int ocv_table_size[POWER_SUPPLY_OCV_TEMP_MAX];
struct power_supply_resistance_temp_table *resist_table;
int resist_table_size;
}; };
extern struct atomic_notifier_head power_supply_notifier; extern struct atomic_notifier_head power_supply_notifier;
@ -381,6 +388,9 @@ power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
int temp, int *table_len); int temp, int *table_len);
extern int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info, extern int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
int ocv, int temp); int ocv, int temp);
extern int
power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table,
int table_len, int temp);
extern void power_supply_changed(struct power_supply *psy); extern void power_supply_changed(struct power_supply *psy);
extern int power_supply_am_i_supplied(struct power_supply *psy); extern int power_supply_am_i_supplied(struct power_supply *psy);
extern int power_supply_set_input_current_limit_from_supplier( extern int power_supply_set_input_current_limit_from_supplier(