Skip to content

Commit a6456d4

Browse files
jwrdegoedesre
authored andcommitted
power: supply: sysfs: Add power_supply_show_enum_with_available() helper
Turn power_supply_charge_behaviour_show() into a generic function for showing enum values with their available (for writing) values shown and the current value shown surrounded by sqaure-brackets like the show() output for "usb_type" and "charge_behaviour". This is a preparation patch for refactoring the "usb_type" property handling to use a bitmask indicating available usb-types + this new generic function. 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 03ec41c commit a6456d4

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

drivers/power/supply/power_supply_sysfs.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -518,31 +518,28 @@ int power_supply_uevent(const struct device *dev, struct kobj_uevent_env *env)
518518
return ret;
519519
}
520520

521-
ssize_t power_supply_charge_behaviour_show(struct device *dev,
522-
unsigned int available_behaviours,
523-
enum power_supply_charge_behaviour current_behaviour,
524-
char *buf)
521+
static ssize_t power_supply_show_enum_with_available(
522+
struct device *dev, const char * const labels[], int label_count,
523+
unsigned int available_values, int value, char *buf)
525524
{
526525
bool match = false, available, active;
527526
ssize_t count = 0;
528527
int i;
529528

530-
for (i = 0; i < ARRAY_SIZE(POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT); i++) {
531-
available = available_behaviours & BIT(i);
532-
active = i == current_behaviour;
529+
for (i = 0; i < label_count; i++) {
530+
available = available_values & BIT(i);
531+
active = i == value;
533532

534533
if (available && active) {
535-
count += sysfs_emit_at(buf, count, "[%s] ",
536-
POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[i]);
534+
count += sysfs_emit_at(buf, count, "[%s] ", labels[i]);
537535
match = true;
538536
} else if (available) {
539-
count += sysfs_emit_at(buf, count, "%s ",
540-
POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[i]);
537+
count += sysfs_emit_at(buf, count, "%s ", labels[i]);
541538
}
542539
}
543540

544541
if (!match) {
545-
dev_warn(dev, "driver reporting unsupported charge behaviour\n");
542+
dev_warn(dev, "driver reporting unavailable enum value %d\n", value);
546543
return -EINVAL;
547544
}
548545

@@ -551,6 +548,17 @@ ssize_t power_supply_charge_behaviour_show(struct device *dev,
551548

552549
return count;
553550
}
551+
552+
ssize_t power_supply_charge_behaviour_show(struct device *dev,
553+
unsigned int available_behaviours,
554+
enum power_supply_charge_behaviour current_behaviour,
555+
char *buf)
556+
{
557+
return power_supply_show_enum_with_available(
558+
dev, POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT,
559+
ARRAY_SIZE(POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT),
560+
available_behaviours, current_behaviour, buf);
561+
}
554562
EXPORT_SYMBOL_GPL(power_supply_charge_behaviour_show);
555563

556564
int power_supply_charge_behaviour_parse(unsigned int available_behaviours, const char *buf)

0 commit comments

Comments
 (0)