Message ID | 20cfdc60b148646a0473640a8efdb056b207c56e.1637061794.git.matti.vaittinen@fi.rohmeurope.com (mailing list archive) |
---|---|
State | Not Applicable, archived |
Headers | show |
Series | power: supply: Add some fuel-gauge logic | expand |
On Tue, Nov 16, 2021 at 1:25 PM Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com> wrote: > The power-supply core supports concept of OCV (Open Circuit Voltage) => > SOC (State Of Charge) conversion tables. Usually these tables are used > to estimate SOC based on OCV. Some systems use so called "Zero Adjust" > where at the near end-of-battery condition the SOC from coulomb counter > is used to retrieve the OCV - and OCV and VSYS difference is used to > re-estimate the battery capacity. > > Add helper to do look-up the other-way around and also get the OCV > based on SOC > > Signed-off-by: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com> It seems you will need this for your chargers indeed. > +int power_supply_dcap2ocv_simple(struct power_supply_battery_ocv_table *table, > + int table_len, int dcap) > +{ > + int i, ocv, tmp; > + > + for (i = 0; i < table_len; i++) > + if (dcap > table[i].capacity * 10) > + break; > + > + if (i > 0 && i < table_len) { > + tmp = (table[i - 1].ocv - table[i].ocv) * > + (dcap - table[i].capacity * 10); > + > + tmp /= (table[i - 1].capacity - table[i].capacity) * 10; > + ocv = tmp + table[i].ocv; > + } else if (i == 0) { > + ocv = table[0].ocv; > + } else { > + ocv = table[table_len - 1].ocv; > + } > + > + return ocv; > +} > +EXPORT_SYMBOL_GPL(power_supply_dcap2ocv_simple); Rewrite this using the library fixpoint interpolation function but just copypasting from my patch: https://lore.kernel.org/linux-pm/20211116230233.2167104-1-linus.walleij@linaro.org/ Other than that it looks good to me! Yours, Linus Walleij
On 11/18/21 04:02, Linus Walleij wrote: > On Tue, Nov 16, 2021 at 1:25 PM Matti Vaittinen > <matti.vaittinen@fi.rohmeurope.com> wrote: > >> The power-supply core supports concept of OCV (Open Circuit Voltage) => >> SOC (State Of Charge) conversion tables. Usually these tables are used >> to estimate SOC based on OCV. Some systems use so called "Zero Adjust" >> where at the near end-of-battery condition the SOC from coulomb counter >> is used to retrieve the OCV - and OCV and VSYS difference is used to >> re-estimate the battery capacity. >> >> Add helper to do look-up the other-way around and also get the OCV >> based on SOC >> >> Signed-off-by: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com> > > It seems you will need this for your chargers indeed. > >> +int power_supply_dcap2ocv_simple(struct power_supply_battery_ocv_table *table, >> + int table_len, int dcap) >> +{ >> + int i, ocv, tmp; >> + >> + for (i = 0; i < table_len; i++) >> + if (dcap > table[i].capacity * 10) >> + break; >> + >> + if (i > 0 && i < table_len) { >> + tmp = (table[i - 1].ocv - table[i].ocv) * >> + (dcap - table[i].capacity * 10); >> + >> + tmp /= (table[i - 1].capacity - table[i].capacity) * 10; >> + ocv = tmp + table[i].ocv; >> + } else if (i == 0) { >> + ocv = table[0].ocv; >> + } else { >> + ocv = table[table_len - 1].ocv; >> + } >> + >> + return ocv; >> +} >> +EXPORT_SYMBOL_GPL(power_supply_dcap2ocv_simple); > > Rewrite this using the library fixpoint interpolation function but just > copypasting from my patch: > https://lore.kernel.org/linux-pm/20211116230233.2167104-1-linus.walleij@linaro.org/ Thanks :) I actually saw this (yesterday?) but didn't revise my patches. Thanks for the head's up - it's always good to have this kind of helpers :) > > Other than that it looks good to me! Thanks! Best Regards --Matti
diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index fc12a4f407f4..295672165836 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -806,6 +806,48 @@ int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *t } EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple); +/** + * power_supply_dcap2ocv_simple() - find the battery OCV by capacity + * @table: Pointer to battery OCV/CAP lookup table + * @table_len: OCV/CAP table length + * @cap: Current cap value in units of 0.1% + * + * OCV (Open Circuit Voltage) is often used to estimate the battery SOC (State + * Of Charge). Usually conversion tables are used to store the corresponding + * OCV and SOC. Systems which use so called "Zero Adjust" where at the near + * end-of-battery condition the SOC from coulomb counter is used to retrieve + * the OCV - and OCV and VSYS difference is used to re-estimate the battery + * capacity. This helper function can be used to look up battery OCV according + * to current capacity value from one OCV table, and the OCV table must be + * ordered descending. + * + * Return: the battery OCV in uV. + */ +int power_supply_dcap2ocv_simple(struct power_supply_battery_ocv_table *table, + int table_len, int dcap) +{ + int i, ocv, tmp; + + for (i = 0; i < table_len; i++) + if (dcap > table[i].capacity * 10) + break; + + if (i > 0 && i < table_len) { + tmp = (table[i - 1].ocv - table[i].ocv) * + (dcap - table[i].capacity * 10); + + tmp /= (table[i - 1].capacity - table[i].capacity) * 10; + ocv = tmp + table[i].ocv; + } else if (i == 0) { + ocv = table[0].ocv; + } else { + ocv = table[table_len - 1].ocv; + } + + return ocv; +} +EXPORT_SYMBOL_GPL(power_supply_dcap2ocv_simple); + /** * power_supply_ocv2cap_simple() - find the battery capacity * @table: Pointer to battery OCV lookup table @@ -866,6 +908,33 @@ power_supply_find_ocv2cap_table(struct power_supply_battery_info *info, } EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table); +/** + * power_supply_batinfo_dcap2ocv() - Compute OCV based on SOC + * @info: Pointer to battery information. + * @dcao: Battery capacity in units of 0.1% + * @temp: Temperatur in Celsius + * + * Compute the open circuit voltage at given temperature matching given + * capacity for a battery described by given battery info. Computation is + * done based on tables of known capacity - open circuit voltage value pairs. + * Requires the OCV tables being populated in the battery info. + * + * Return: The battery OCV in uV or -EINVAL if OCV table is not available. + */ +int power_supply_batinfo_dcap2ocv(struct power_supply_battery_info *info, + int dcap, int temp) +{ + struct power_supply_battery_ocv_table *table; + int table_len; + + table = power_supply_find_ocv2cap_table(info, temp, &table_len); + if (!table) + return -EINVAL; + + return power_supply_dcap2ocv_simple(table, table_len, dcap); +} +EXPORT_SYMBOL_GPL(power_supply_batinfo_dcap2ocv); + int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info, int ocv, int temp) { diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index 9ca1f120a211..fa8cf434f7e3 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -410,11 +410,16 @@ extern void power_supply_put_battery_info(struct power_supply *psy, struct power_supply_battery_info *info); extern int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table, int table_len, int ocv); +int power_supply_dcap2ocv_simple(struct power_supply_battery_ocv_table *table, + int table_len, int dcap); + extern struct power_supply_battery_ocv_table * power_supply_find_ocv2cap_table(struct power_supply_battery_info *info, int temp, int *table_len); extern int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info, int ocv, int temp); +int power_supply_batinfo_dcap2ocv(struct power_supply_battery_info *info, + int dcap, int temp); extern int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table, int table_len, int temp);
The power-supply core supports concept of OCV (Open Circuit Voltage) => SOC (State Of Charge) conversion tables. Usually these tables are used to estimate SOC based on OCV. Some systems use so called "Zero Adjust" where at the near end-of-battery condition the SOC from coulomb counter is used to retrieve the OCV - and OCV and VSYS difference is used to re-estimate the battery capacity. Add helper to do look-up the other-way around and also get the OCV based on SOC Signed-off-by: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com> --- RFC v3: - Kerneldoc fixes --- drivers/power/supply/power_supply_core.c | 69 ++++++++++++++++++++++++ include/linux/power_supply.h | 5 ++ 2 files changed, 74 insertions(+)