From: "Thomas Weißschuh" <linux@weissschuh.net>
To: linux-pm@vger.kernel.org, Sebastian Reichel <sre@kernel.org>,
ibm-acpi-devel@lists.sourceforge.net,
platform-driver-x86@vger.kernel.org,
Mark Gross <markgross@kernel.org>,
Hans de Goede <hdegoede@redhat.com>,
Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Cc: "Thomas Weißschuh" <linux@weissschuh.net>,
linux-kernel@vger.kernel.org, linrunner@gmx.net,
bberg@redhat.com, hadess@hadess.net, markpearson@lenovo.com,
nicolopiazzalunga@gmail.com, njoshi1@lenovo.com,
smclt30p@gmail.com
Subject: [PATCH v2 2/4] power: supply: add helpers for charge_behaviour sysfs
Date: Wed, 24 Nov 2021 00:27:02 +0100 [thread overview]
Message-ID: <20211123232704.25394-3-linux@weissschuh.net> (raw)
In-Reply-To: <20211123232704.25394-1-linux@weissschuh.net>
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>
---
drivers/power/supply/power_supply_sysfs.c | 55 +++++++++++++++++++++++
include/linux/power_supply.h | 9 ++++
2 files changed, 64 insertions(+)
diff --git a/drivers/power/supply/power_supply_sysfs.c b/drivers/power/supply/power_supply_sysfs.c
index c3d7cbcd4fad..5e3b8c15ddbe 100644
--- a/drivers/power/supply/power_supply_sysfs.c
+++ b/drivers/power/supply/power_supply_sysfs.c
@@ -133,6 +133,12 @@ static const char * const POWER_SUPPLY_SCOPE_TEXT[] = {
[POWER_SUPPLY_SCOPE_DEVICE] = "Device",
};
+static const char * const POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[] = {
+ [POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO] = "auto",
+ [POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE] = "inhibit-charge",
+ [POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE] = "force-discharge",
+};
+
static struct power_supply_attr power_supply_attrs[] = {
/* Properties of type `int' */
POWER_SUPPLY_ENUM_ATTR(STATUS),
@@ -484,3 +490,52 @@ int power_supply_uevent(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)
+{
+ 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;
+
+ if (available && active) {
+ count += sysfs_emit_at(buf, count, "[%s] ",
+ POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[i]);
+ match = true;
+ } else if (available) {
+ count += sysfs_emit_at(buf, count, "%s ",
+ POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[i]);
+ }
+ }
+
+ if (!match) {
+ dev_warn(dev, "driver reporting unsupported charge behaviour\n");
+ return -EINVAL;
+ }
+
+ if (count)
+ buf[count - 1] = '\n';
+
+ return count;
+}
+EXPORT_SYMBOL_GPL(power_supply_charge_behaviour_show);
+
+int power_supply_charge_behaviour_parse(unsigned int available_behaviours, const char *buf)
+{
+ int i = sysfs_match_string(POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT, buf);
+
+ if (i < 0)
+ return i;
+
+ if (available_behaviours & BIT(i))
+ return i;
+
+ return -EINVAL;
+}
+EXPORT_SYMBOL_GPL(power_supply_charge_behaviour_parse);
diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index 70c333e86293..71f0379c2af8 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -546,4 +546,13 @@ static inline
void power_supply_remove_hwmon_sysfs(struct power_supply *psy) {}
#endif
+#ifdef CONFIG_SYSFS
+ssize_t power_supply_charge_behaviour_show(struct device *dev,
+ unsigned int available_behaviours,
+ enum power_supply_charge_behaviour behaviour,
+ char *buf);
+
+int power_supply_charge_behaviour_parse(unsigned int available_behaviours, const char *buf);
+#endif
+
#endif /* __LINUX_POWER_SUPPLY_H__ */
--
2.34.0
next prev parent reply other threads:[~2021-11-23 23:27 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-23 23:27 [PATCH 0/4] power: supply: add charge_behaviour property (force-discharge, inhibit-charge) Thomas Weißschuh
2021-11-23 23:27 ` [PATCH v2 1/4] power: supply: add charge_behaviour attributes Thomas Weißschuh
2021-11-23 23:27 ` Thomas Weißschuh [this message]
2021-11-23 23:27 ` [PATCH v2 3/4] platform/x86: thinkpad_acpi: support force-discharge Thomas Weißschuh
2021-11-23 23:27 ` [PATCH v2 4/4] platform/x86: thinkpad_acpi: support inhibit-charge Thomas Weißschuh
2021-11-25 18:04 ` [ibm-acpi-devel] [PATCH 0/4] power: supply: add charge_behaviour property (force-discharge, inhibit-charge) Kevin Locke
2021-12-03 21:33 ` Sebastian Reichel
2021-12-04 13:56 ` Hans de Goede
2021-12-16 15:41 ` Hans de Goede
2021-12-21 16:06 ` Hans de Goede
2021-12-04 16:04 ` Thomas Koch
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211123232704.25394-3-linux@weissschuh.net \
--to=linux@weissschuh.net \
--cc=bberg@redhat.com \
--cc=hadess@hadess.net \
--cc=hdegoede@redhat.com \
--cc=hmh@hmh.eng.br \
--cc=ibm-acpi-devel@lists.sourceforge.net \
--cc=linrunner@gmx.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=markgross@kernel.org \
--cc=markpearson@lenovo.com \
--cc=nicolopiazzalunga@gmail.com \
--cc=njoshi1@lenovo.com \
--cc=platform-driver-x86@vger.kernel.org \
--cc=smclt30p@gmail.com \
--cc=sre@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).