mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Johann Neuhauser <jneuhauser@dh-electronics.com>
To: <linux-kernel@vger.kernel.org>, <linux-doc@vger.kernel.org>
Cc: Johann Neuhauser <jneuhauser@dh-electronics.com>,
	Jonathan Corbet <corbet@lwn.net>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>
Subject: [PATCH 1/3] regulator: userspace-consumer: Add uevent reporting for regulator events
Date: Fri, 4 Apr 2025 15:40:07 +0200	[thread overview]
Message-ID: <20250404134009.2610460-2-jneuhauser@dh-electronics.com> (raw)
In-Reply-To: <20250404134009.2610460-1-jneuhauser@dh-electronics.com>

Generate uevents when regulator events such as voltage changes, overcurrent,
or enable/disable transitions occur. A separate uevent is emitted for each
individual regulator event bit, allowing precise event handling in userspace
via udev rules.

The emitted uevent key `EVENT=` corresponds directly to the event types defined
in include/uapi/regulator/regulator.h.

This provides a flexible, user-friendly mechanism to monitor and handle
regulator events from userspace.

Signed-off-by: Johann Neuhauser <jneuhauser@dh-electronics.com>
---
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Liam Girdwood <lgirdwood@gmail.com>
Cc: linux-doc@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: Mark Brown <broonie@kernel.org>
---
 drivers/regulator/userspace-consumer.c | 74 +++++++++++++++++++++++++-
 1 file changed, 73 insertions(+), 1 deletion(-)

diff --git a/drivers/regulator/userspace-consumer.c b/drivers/regulator/userspace-consumer.c
index 72bb5ffb49a8..01cf07d42682 100644
--- a/drivers/regulator/userspace-consumer.c
+++ b/drivers/regulator/userspace-consumer.c
@@ -11,6 +11,7 @@
  *   Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  */
 
+#include <linux/bitops.h>
 #include <linux/err.h>
 #include <linux/mutex.h>
 #include <linux/module.h>
@@ -29,6 +30,9 @@ struct userspace_consumer_data {
 
 	int num_supplies;
 	struct regulator_bulk_data *supplies;
+
+	struct kobject *kobj;
+	struct notifier_block nb;
 };
 
 static ssize_t name_show(struct device *dev,
@@ -115,12 +119,68 @@ static const struct attribute_group attr_group = {
 	.is_visible =  attr_visible,
 };
 
+/*
+ * This should probably be placed elsewhere in the regulator framework...
+ */
+static const char *regulator_event_str(unsigned long event)
+{
+	switch (event) {
+	case REGULATOR_EVENT_ABORT_DISABLE:
+		return "ABORT_DISABLE";
+	case REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE:
+		return "ABORT_VOLTAGE_CHANGE";
+	case REGULATOR_EVENT_DISABLE:
+		return "DISABLE";
+	case REGULATOR_EVENT_ENABLE:
+		return "ENABLE";
+	case REGULATOR_EVENT_FAIL:
+		return "FAIL";
+	case REGULATOR_EVENT_FORCE_DISABLE:
+		return "FORCE_DISABLE";
+	case REGULATOR_EVENT_OVER_CURRENT:
+		return "OVER_CURRENT";
+	case REGULATOR_EVENT_OVER_TEMP:
+		return "OVER_TEMP";
+	case REGULATOR_EVENT_PRE_DISABLE:
+		return "PRE_DISABLE";
+	case REGULATOR_EVENT_PRE_VOLTAGE_CHANGE:
+		return "PRE_VOLTAGE_CHANGE";
+	case REGULATOR_EVENT_REGULATION_OUT:
+		return "REGULATION_OUT";
+	case REGULATOR_EVENT_UNDER_VOLTAGE:
+		return "UNDER_VOLTAGE";
+	case REGULATOR_EVENT_VOLTAGE_CHANGE:
+		return "VOLTAGE_CHANGE";
+	default:
+		return NULL;
+	}
+}
+
+static int regulator_userspace_notify(struct notifier_block *nb, unsigned long event, void *unused)
+{
+	struct userspace_consumer_data *drvdata = container_of(nb, struct userspace_consumer_data, nb);
+	char env_buf[128];
+	char *envp[] = { "NAME=event", env_buf, NULL };
+	unsigned int bit;
+
+	for_each_set_bit(bit, &event, BITS_PER_TYPE(event)) {
+		const char *event_str = regulator_event_str(BIT(bit));
+
+		if (event_str && event_str[0] != '\0') {
+			scnprintf(env_buf, sizeof(env_buf), "EVENT=%s", event_str);
+			kobject_uevent_env(drvdata->kobj, KOBJ_CHANGE, envp);
+		}
+	}
+
+	return NOTIFY_OK;
+}
+
 static int regulator_userspace_consumer_probe(struct platform_device *pdev)
 {
 	struct regulator_userspace_consumer_data tmpdata;
 	struct regulator_userspace_consumer_data *pdata;
 	struct userspace_consumer_data *drvdata;
-	int ret;
+	int i, ret;
 
 	pdata = dev_get_platdata(&pdev->dev);
 	if (!pdata) {
@@ -153,6 +213,7 @@ static int regulator_userspace_consumer_probe(struct platform_device *pdev)
 	drvdata->num_supplies = pdata->num_supplies;
 	drvdata->supplies = pdata->supplies;
 	drvdata->no_autoswitch = pdata->no_autoswitch;
+	drvdata->kobj = &pdev->dev.kobj;
 
 	mutex_init(&drvdata->lock);
 
@@ -184,6 +245,13 @@ static int regulator_userspace_consumer_probe(struct platform_device *pdev)
 	}
 	drvdata->enabled = !!ret;
 
+	drvdata->nb.notifier_call = regulator_userspace_notify;
+	for (i = 0; i < drvdata->num_supplies; i++) {
+		ret = devm_regulator_register_notifier(drvdata->supplies[i].consumer, &drvdata->nb);
+		if (ret)
+			goto err_enable;
+	}
+
 	return 0;
 
 err_enable:
@@ -195,6 +263,10 @@ static int regulator_userspace_consumer_probe(struct platform_device *pdev)
 static void regulator_userspace_consumer_remove(struct platform_device *pdev)
 {
 	struct userspace_consumer_data *data = platform_get_drvdata(pdev);
+	int i;
+
+	for (i = 0; i < data->num_supplies; i++)
+		devm_regulator_unregister_notifier(data->supplies[i].consumer, &data->nb);
 
 	sysfs_remove_group(&pdev->dev.kobj, &attr_group);
 
-- 
2.39.5


  reply	other threads:[~2025-04-04 13:42 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-04 13:40 [PATCH 0/3] regulator: userspace-consumer: Add regulator event uevents Johann Neuhauser
2025-04-04 13:40 ` Johann Neuhauser [this message]
2025-04-04 13:40 ` [PATCH 2/3] ABI: sysfs-platform: Document uevent ABI for reg-userspace-consumer Johann Neuhauser
2025-04-04 13:40 ` [PATCH 3/3] docs: regulator: userspace-consumer: Add uevent-based regulator event reporting Johann Neuhauser
2025-04-04 17:02 ` [PATCH 0/3] regulator: userspace-consumer: Add regulator event uevents Mark Brown
2025-04-07 14:17   ` Johann Neuhauser
2025-04-07 15:57     ` Mark Brown

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=20250404134009.2610460-2-jneuhauser@dh-electronics.com \
    --to=jneuhauser@dh-electronics.com \
    --cc=broonie@kernel.org \
    --cc=corbet@lwn.net \
    --cc=lgirdwood@gmail.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.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

all inboxes | Powered by JetHome®