Skip to content

Commit 539b9c9

Browse files
t-8chjwrdegoede
authored andcommitted
power: supply: add helpers for charge_behaviour sysfs
These helper functions can be used by drivers to implement their own sysfs-attributes. This is useful for ACPI-drivers extending the default ACPI-battery with their own charge_behaviour attributes. Signed-off-by: Thomas Weißschuh <linux@weissschuh.net> Acked-by: Sebastian Reichel <sebastian.reichel@collabora.com> Link: https://lore.kernel.org/r/20211123232704.25394-3-linux@weissschuh.net Signed-off-by: Hans de Goede <hdegoede@redhat.com>
1 parent 1b0b6cc commit 539b9c9

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

drivers/power/supply/power_supply_sysfs.c

+55
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ static const char * const POWER_SUPPLY_SCOPE_TEXT[] = {
133133
[POWER_SUPPLY_SCOPE_DEVICE] = "Device",
134134
};
135135

136+
static const char * const POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[] = {
137+
[POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO] = "auto",
138+
[POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE] = "inhibit-charge",
139+
[POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE] = "force-discharge",
140+
};
141+
136142
static struct power_supply_attr power_supply_attrs[] = {
137143
/* Properties of type `int' */
138144
POWER_SUPPLY_ENUM_ATTR(STATUS),
@@ -484,3 +490,52 @@ int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
484490

485491
return ret;
486492
}
493+
494+
ssize_t power_supply_charge_behaviour_show(struct device *dev,
495+
unsigned int available_behaviours,
496+
enum power_supply_charge_behaviour current_behaviour,
497+
char *buf)
498+
{
499+
bool match = false, available, active;
500+
ssize_t count = 0;
501+
int i;
502+
503+
for (i = 0; i < ARRAY_SIZE(POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT); i++) {
504+
available = available_behaviours & BIT(i);
505+
active = i == current_behaviour;
506+
507+
if (available && active) {
508+
count += sysfs_emit_at(buf, count, "[%s] ",
509+
POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[i]);
510+
match = true;
511+
} else if (available) {
512+
count += sysfs_emit_at(buf, count, "%s ",
513+
POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[i]);
514+
}
515+
}
516+
517+
if (!match) {
518+
dev_warn(dev, "driver reporting unsupported charge behaviour\n");
519+
return -EINVAL;
520+
}
521+
522+
if (count)
523+
buf[count - 1] = '\n';
524+
525+
return count;
526+
}
527+
EXPORT_SYMBOL_GPL(power_supply_charge_behaviour_show);
528+
529+
int power_supply_charge_behaviour_parse(unsigned int available_behaviours, const char *buf)
530+
{
531+
int i = sysfs_match_string(POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT, buf);
532+
533+
if (i < 0)
534+
return i;
535+
536+
if (available_behaviours & BIT(i))
537+
return i;
538+
539+
return -EINVAL;
540+
}
541+
EXPORT_SYMBOL_GPL(power_supply_charge_behaviour_parse);

include/linux/power_supply.h

+9
Original file line numberDiff line numberDiff line change
@@ -546,4 +546,13 @@ static inline
546546
void power_supply_remove_hwmon_sysfs(struct power_supply *psy) {}
547547
#endif
548548

549+
#ifdef CONFIG_SYSFS
550+
ssize_t power_supply_charge_behaviour_show(struct device *dev,
551+
unsigned int available_behaviours,
552+
enum power_supply_charge_behaviour behaviour,
553+
char *buf);
554+
555+
int power_supply_charge_behaviour_parse(unsigned int available_behaviours, const char *buf);
556+
#endif
557+
549558
#endif /* __LINUX_POWER_SUPPLY_H__ */

0 commit comments

Comments
 (0)