Skip to content

Commit bd52ca5

Browse files
Daniel Mackenomsg
authored andcommitted
ds2760_battery: Make charge_now and charge_full writeable
For userspace tools and daemons, it might be necessary to adjust the charge_now and charge_full properties of the ds2760 battery monitor, for example for unavoidable corrections due to aging batteries. Signed-off-by: Daniel Mack <[email protected]> Cc: Matt Reimer <[email protected]> Cc: Evgeniy Polyakov <[email protected]> Cc: Tejun Heo <[email protected]> Cc: David Woodhouse <[email protected]> Cc: Alexey Starikovskiy <[email protected]> Cc: Len Brown <[email protected]> Cc: Mark Brown <[email protected]> Signed-off-by: Anton Vorontsov <[email protected]>
1 parent 0011d2d commit bd52ca5

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

drivers/power/ds2760_battery.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,28 @@ static void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di,
304304
w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
305305
}
306306

307+
static void ds2760_battery_write_active_full(struct ds2760_device_info *di,
308+
int active_full)
309+
{
310+
unsigned char tmp[2] = {
311+
active_full >> 8,
312+
active_full & 0xff
313+
};
314+
315+
if (tmp[0] == di->raw[DS2760_ACTIVE_FULL] &&
316+
tmp[1] == di->raw[DS2760_ACTIVE_FULL + 1])
317+
return;
318+
319+
w1_ds2760_write(di->w1_dev, tmp, DS2760_ACTIVE_FULL, sizeof(tmp));
320+
w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK0);
321+
w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK0);
322+
323+
/* Write to the di->raw[] buffer directly - the DS2760_ACTIVE_FULL
324+
* values won't be read back by ds2760_battery_read_status() */
325+
di->raw[DS2760_ACTIVE_FULL] = tmp[0];
326+
di->raw[DS2760_ACTIVE_FULL + 1] = tmp[1];
327+
}
328+
307329
static void ds2760_battery_work(struct work_struct *work)
308330
{
309331
struct ds2760_device_info *di = container_of(work,
@@ -426,6 +448,45 @@ static int ds2760_battery_get_property(struct power_supply *psy,
426448
return 0;
427449
}
428450

451+
static int ds2760_battery_set_property(struct power_supply *psy,
452+
enum power_supply_property psp,
453+
const union power_supply_propval *val)
454+
{
455+
struct ds2760_device_info *di = to_ds2760_device_info(psy);
456+
457+
switch (psp) {
458+
case POWER_SUPPLY_PROP_CHARGE_FULL:
459+
/* the interface counts in uAh, convert the value */
460+
ds2760_battery_write_active_full(di, val->intval / 1000L);
461+
break;
462+
463+
case POWER_SUPPLY_PROP_CHARGE_NOW:
464+
/* ds2760_battery_set_current_accum() does the conversion */
465+
ds2760_battery_set_current_accum(di, val->intval);
466+
break;
467+
468+
default:
469+
return -EPERM;
470+
}
471+
472+
return 0;
473+
}
474+
475+
static int ds2760_battery_property_is_writeable(struct power_supply *psy,
476+
enum power_supply_property psp)
477+
{
478+
switch (psp) {
479+
case POWER_SUPPLY_PROP_CHARGE_FULL:
480+
case POWER_SUPPLY_PROP_CHARGE_NOW:
481+
return 1;
482+
483+
default:
484+
break;
485+
}
486+
487+
return 0;
488+
}
489+
429490
static enum power_supply_property ds2760_battery_props[] = {
430491
POWER_SUPPLY_PROP_STATUS,
431492
POWER_SUPPLY_PROP_VOLTAGE_NOW,
@@ -460,6 +521,9 @@ static int ds2760_battery_probe(struct platform_device *pdev)
460521
di->bat.properties = ds2760_battery_props;
461522
di->bat.num_properties = ARRAY_SIZE(ds2760_battery_props);
462523
di->bat.get_property = ds2760_battery_get_property;
524+
di->bat.set_property = ds2760_battery_set_property;
525+
di->bat.property_is_writeable =
526+
ds2760_battery_property_is_writeable;
463527
di->bat.set_charged = ds2760_battery_set_charged;
464528
di->bat.external_power_changed =
465529
ds2760_battery_external_power_changed;

0 commit comments

Comments
 (0)