Skip to content

Commit 2986e5b

Browse files
Hans de Goedesre
authored andcommitted
power: supply: ug3105_battery: Switch to power_supply_batinfo_ocv2cap()
Replace the hardcoded ocv -> capacity table and the ug3105_get_capacity() helper with using the generic power_supply_batinfo_ocv2cap() function. Note this relies on the battery fwnode providing at least 1 "ocv-capacity-table", if that is missing probe() will now fail with EINVAL. Signed-off-by: Hans de Goede <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sebastian Reichel <[email protected]>
1 parent 8842bd0 commit 2986e5b

File tree

1 file changed

+10
-58
lines changed

1 file changed

+10
-58
lines changed

drivers/power/supply/ug3105_battery.c

Lines changed: 10 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
#define UG3105_LOW_BAT_UV 3700000
6767
#define UG3105_FULL_BAT_HYST_UV 38000
6868

69+
#define AMBIENT_TEMP_CELCIUS 25
70+
6971
struct ug3105_chip {
7072
struct i2c_client *client;
7173
struct power_supply *psy;
@@ -117,62 +119,6 @@ static int ug3105_get_status(struct ug3105_chip *chip)
117119
return POWER_SUPPLY_STATUS_NOT_CHARGING;
118120
}
119121

120-
static int ug3105_get_capacity(struct ug3105_chip *chip)
121-
{
122-
/*
123-
* OCV voltages in uV for 0-110% in 5% increments, the 100-110% is
124-
* for LiPo HV (High-Voltage) bateries which can go up to 4.35V
125-
* instead of the usual 4.2V.
126-
*/
127-
static const int ocv_capacity_tbl[23] = {
128-
3350000,
129-
3610000,
130-
3690000,
131-
3710000,
132-
3730000,
133-
3750000,
134-
3770000,
135-
3786667,
136-
3803333,
137-
3820000,
138-
3836667,
139-
3853333,
140-
3870000,
141-
3907500,
142-
3945000,
143-
3982500,
144-
4020000,
145-
4075000,
146-
4110000,
147-
4150000,
148-
4200000,
149-
4250000,
150-
4300000,
151-
};
152-
int i, ocv_diff, ocv_step;
153-
154-
if (chip->ocv_avg < ocv_capacity_tbl[0])
155-
return 0;
156-
157-
if (chip->status == POWER_SUPPLY_STATUS_FULL)
158-
return 100;
159-
160-
for (i = 1; i < ARRAY_SIZE(ocv_capacity_tbl); i++) {
161-
if (chip->ocv_avg > ocv_capacity_tbl[i])
162-
continue;
163-
164-
ocv_diff = ocv_capacity_tbl[i] - chip->ocv_avg;
165-
ocv_step = ocv_capacity_tbl[i] - ocv_capacity_tbl[i - 1];
166-
/* scale 0-110% down to 0-100% for LiPo HV */
167-
if (chip->psy->battery_info->constant_charge_voltage_max_uv >= 4300000)
168-
return (i * 500 - ocv_diff * 500 / ocv_step) / 110;
169-
else
170-
return i * 5 - ocv_diff * 5 / ocv_step;
171-
}
172-
173-
return 100;
174-
}
175-
176122
static void ug3105_work(struct work_struct *work)
177123
{
178124
struct ug3105_chip *chip = container_of(work, struct ug3105_chip,
@@ -231,7 +177,12 @@ static void ug3105_work(struct work_struct *work)
231177

232178
chip->supplied = power_supply_am_i_supplied(psy);
233179
chip->status = ug3105_get_status(chip);
234-
chip->capacity = ug3105_get_capacity(chip);
180+
if (chip->status == POWER_SUPPLY_STATUS_FULL)
181+
chip->capacity = 100;
182+
else
183+
chip->capacity = power_supply_batinfo_ocv2cap(chip->psy->battery_info,
184+
chip->ocv_avg,
185+
AMBIENT_TEMP_CELCIUS);
235186

236187
/*
237188
* Skip internal resistance calc on charger [un]plug and
@@ -403,7 +354,8 @@ static int ug3105_probe(struct i2c_client *client)
403354

404355
if (!psy->battery_info ||
405356
psy->battery_info->factory_internal_resistance_uohm == -EINVAL ||
406-
psy->battery_info->constant_charge_voltage_max_uv == -EINVAL) {
357+
psy->battery_info->constant_charge_voltage_max_uv == -EINVAL ||
358+
!psy->battery_info->ocv_table[0]) {
407359
dev_err(dev, "error required properties are missing\n");
408360
return -ENODEV;
409361
}

0 commit comments

Comments
 (0)