[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240831142039.28830-5-hdegoede@redhat.com>
Date: Sat, 31 Aug 2024 16:20:37 +0200
From: Hans de Goede <hdegoede@...hat.com>
To: Sebastian Reichel <sre@...nel.org>,
Thomas Weißschuh <linux@...ssschuh.net>,
MyungJoo Ham <myungjoo.ham@...sung.com>,
Chanwoo Choi <cw00.choi@...sung.com>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Heikki Krogerus <heikki.krogerus@...ux.intel.com>,
Vinod Koul <vkoul@...nel.org>,
Kishon Vijay Abraham I <kishon@...nel.org>
Cc: Hans de Goede <hdegoede@...hat.com>,
Enric Balletbo Serra <enric.balletbo@...labora.com>,
Andrey Smirnov <andrew.smirnov@...il.com>,
linux-pm@...r.kernel.org,
linux-kernel@...r.kernel.org,
linux-usb@...r.kernel.org,
linux-phy@...ts.infradead.org
Subject: [PATCH 4/6] 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 <hdegoede@...hat.com>
---
drivers/power/supply/power_supply_sysfs.c | 32 ++++++++++++++---------
1 file changed, 20 insertions(+), 12 deletions(-)
diff --git a/drivers/power/supply/power_supply_sysfs.c b/drivers/power/supply/power_supply_sysfs.c
index ff7e423edd57..9f21b0b54caf 100644
--- a/drivers/power/supply/power_supply_sysfs.c
+++ b/drivers/power/supply/power_supply_sysfs.c
@@ -518,31 +518,28 @@ int power_supply_uevent(const struct device *dev, struct kobj_uevent_env *env)
return ret;
}
-ssize_t power_supply_charge_behaviour_show(struct device *dev,
- unsigned int available_behaviours,
- enum power_supply_charge_behaviour current_behaviour,
- char *buf)
+static ssize_t power_supply_show_enum_with_available(
+ struct device *dev, const char * const labels[], int label_count,
+ unsigned int available_values, int value, char *buf)
{
bool match = false, available, active;
ssize_t count = 0;
int i;
- for (i = 0; i < ARRAY_SIZE(POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT); i++) {
- available = available_behaviours & BIT(i);
- active = i == current_behaviour;
+ for (i = 0; i < label_count; i++) {
+ available = available_values & BIT(i);
+ active = i == value;
if (available && active) {
- count += sysfs_emit_at(buf, count, "[%s] ",
- POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[i]);
+ count += sysfs_emit_at(buf, count, "[%s] ", labels[i]);
match = true;
} else if (available) {
- count += sysfs_emit_at(buf, count, "%s ",
- POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[i]);
+ count += sysfs_emit_at(buf, count, "%s ", labels[i]);
}
}
if (!match) {
- dev_warn(dev, "driver reporting unsupported charge behaviour\n");
+ dev_warn(dev, "driver reporting unavailable enum value %d\n", value);
return -EINVAL;
}
@@ -551,6 +548,17 @@ ssize_t power_supply_charge_behaviour_show(struct device *dev,
return count;
}
+
+ssize_t power_supply_charge_behaviour_show(struct device *dev,
+ unsigned int available_behaviours,
+ enum power_supply_charge_behaviour current_behaviour,
+ char *buf)
+{
+ return power_supply_show_enum_with_available(
+ dev, POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT,
+ ARRAY_SIZE(POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT),
+ available_behaviours, current_behaviour, buf);
+}
EXPORT_SYMBOL_GPL(power_supply_charge_behaviour_show);
int power_supply_charge_behaviour_parse(unsigned int available_behaviours, const char *buf)
--
2.46.0
Powered by blists - more mailing lists