power: supply: core: Use library interpolation

The power supply core appears to contain two open coded
linear interpolations. Use the kernel fixpoint arithmetic
interpolation library function instead.

Cc: Chunyan Zhang <chunyan.zhang@unisoc.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
This commit is contained in:
Linus Walleij
2021-11-17 00:02:33 +01:00
committed by Sebastian Reichel
parent 715ecbc10d
commit a4585ba205

View File

@ -21,6 +21,7 @@
#include <linux/power_supply.h> #include <linux/power_supply.h>
#include <linux/property.h> #include <linux/property.h>
#include <linux/thermal.h> #include <linux/thermal.h>
#include <linux/fixp-arith.h>
#include "power_supply.h" #include "power_supply.h"
/* exported for the APM Power driver, APM emulation */ /* exported for the APM Power driver, APM emulation */
@ -783,26 +784,25 @@ EXPORT_SYMBOL_GPL(power_supply_put_battery_info);
int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table, int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table,
int table_len, int temp) int table_len, int temp)
{ {
int i, resist; int i, high, low;
for (i = 0; i < table_len; i++) /* Break loop at table_len - 1 because that is the highest index */
for (i = 0; i < table_len - 1; i++)
if (temp > table[i].temp) if (temp > table[i].temp)
break; break;
if (i > 0 && i < table_len) { /* The library function will deal with high == low */
int tmp; if ((i == 0) || (i == (table_len - 1)))
high = i;
else
high = i - 1;
low = i;
tmp = (table[i - 1].resistance - table[i].resistance) * return fixp_linear_interpolate(table[low].temp,
(temp - table[i].temp); table[low].resistance,
tmp /= table[i - 1].temp - table[i].temp; table[high].temp,
resist = tmp + table[i].resistance; table[high].resistance,
} else if (i == 0) { temp);
resist = table[0].resistance;
} else {
resist = table[table_len - 1].resistance;
}
return resist;
} }
EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple); EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple);
@ -821,24 +821,25 @@ EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple);
int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table, int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table,
int table_len, int ocv) int table_len, int ocv)
{ {
int i, cap, tmp; int i, high, low;
for (i = 0; i < table_len; i++) /* Break loop at table_len - 1 because that is the highest index */
for (i = 0; i < table_len - 1; i++)
if (ocv > table[i].ocv) if (ocv > table[i].ocv)
break; break;
if (i > 0 && i < table_len) { /* The library function will deal with high == low */
tmp = (table[i - 1].capacity - table[i].capacity) * if ((i == 0) || (i == (table_len - 1)))
(ocv - table[i].ocv); high = i - 1;
tmp /= table[i - 1].ocv - table[i].ocv; else
cap = tmp + table[i].capacity; high = i; /* i.e. i == 0 */
} else if (i == 0) { low = i;
cap = table[0].capacity;
} else {
cap = table[table_len - 1].capacity;
}
return cap; return fixp_linear_interpolate(table[low].ocv,
table[low].capacity,
table[high].ocv,
table[high].capacity,
ocv);
} }
EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple); EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple);