public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Naresh Solanki <naresh.solanki@9elements.com>
To: linux-kernel@vger.kernel.org, Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>
Cc: Patrick Rudolph <patrick.rudolph@9elements.com>,
	Naresh Solanki <Naresh.Solanki@9elements.com>
Subject: [PATCH v4 4/4] regulator: output-supply: Add Notification support
Date: Thu,  7 Jul 2022 10:18:26 +0200	[thread overview]
Message-ID: <20220707081826.953449-5-Naresh.Solanki@9elements.com> (raw)
In-Reply-To: <20220707081826.953449-1-Naresh.Solanki@9elements.com>

From: Patrick Rudolph <patrick.rudolph@9elements.com>

Add notification support to output-supply driver to receive regulator
events.

During runtime, regulator may encounter events like over/under voltage,
over current etc. & its useful to report to userspace to take necessary
corrective action.

If sysfs notification is desired then that can be enabled using
regulator-notify-enable property.
This is very useful in situation wherein immediate responsive action
needs to be taken upon receiving events & delayed response is not
acceptable depending on criticality of the received event.

Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
Signed-off-by: Naresh Solanki <Naresh.Solanki@9elements.com>
---
 drivers/regulator/userspace-consumer.c | 50 ++++++++++++++++++++++++--
 1 file changed, 48 insertions(+), 2 deletions(-)

diff --git a/drivers/regulator/userspace-consumer.c b/drivers/regulator/userspace-consumer.c
index b424a0ddf582..0cf2724b5c37 100644
--- a/drivers/regulator/userspace-consumer.c
+++ b/drivers/regulator/userspace-consumer.c
@@ -22,14 +22,33 @@
 
 struct userspace_consumer_data {
 	const char *name;
-
+	bool notify_enable;
+	struct notifier_block nb;
 	struct mutex lock;
 	bool enabled;
+	unsigned long events;
+	struct kobject *kobj;
 
 	int num_supplies;
 	struct regulator_bulk_data *supplies;
 };
 
+static DEFINE_MUTEX(events_lock);
+
+static ssize_t events_show(struct device *dev,
+			  struct device_attribute *attr, char *buf)
+{
+	struct userspace_consumer_data *data = dev_get_drvdata(dev);
+	unsigned long e;
+
+	mutex_lock(&events_lock);
+	e = data->events;
+	data->events = 0;
+	mutex_unlock(&events_lock);
+
+	return sprintf(buf, "0x%lx\n", e);
+}
+
 static ssize_t name_show(struct device *dev,
 			 struct device_attribute *attr, char *buf)
 {
@@ -88,12 +107,14 @@ static ssize_t state_store(struct device *dev, struct device_attribute *attr,
 	return count;
 }
 
+static DEVICE_ATTR_RO(events);
 static DEVICE_ATTR_RO(name);
 static DEVICE_ATTR_RW(state);
 
 static struct attribute *attributes[] = {
 	&dev_attr_name.attr,
 	&dev_attr_state.attr,
+	&dev_attr_events.attr,
 	NULL,
 };
 
@@ -101,6 +122,22 @@ static const struct attribute_group attr_group = {
 	.attrs	= attributes,
 };
 
+static int regulator_userspace_notify(struct notifier_block *nb,
+				      unsigned long event,
+				      void *ignored)
+{
+	struct userspace_consumer_data *data =
+		container_of(nb, struct userspace_consumer_data, nb);
+
+	mutex_lock(&events_lock);
+	data->events |= event;
+	mutex_unlock(&events_lock);
+
+	sysfs_notify(data->kobj, NULL, dev_attr_events.attr.name);
+
+	return NOTIFY_OK;
+}
+
 static struct regulator_userspace_consumer_data *get_pdata_from_dt_node(
 		struct platform_device *pdev)
 {
@@ -139,7 +176,7 @@ static int regulator_userspace_consumer_probe(struct platform_device *pdev)
 {
 	struct regulator_userspace_consumer_data *pdata;
 	struct userspace_consumer_data *drvdata;
-	int ret;
+	int ret, i;
 
 	pdata = dev_get_platdata(&pdev->dev);
 	if (!pdata && pdev->dev.of_node) {
@@ -159,6 +196,7 @@ static int regulator_userspace_consumer_probe(struct platform_device *pdev)
 	drvdata->name = pdata->name;
 	drvdata->num_supplies = pdata->num_supplies;
 	drvdata->supplies = pdata->supplies;
+	drvdata->kobj = &pdev->dev.kobj;
 
 	mutex_init(&drvdata->lock);
 
@@ -183,7 +221,15 @@ static int regulator_userspace_consumer_probe(struct platform_device *pdev)
 		}
 	}
 
+	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;
+	}
+
 	drvdata->enabled = pdata->init_on;
+
 	platform_set_drvdata(pdev, drvdata);
 
 	return 0;
-- 
2.35.3


  parent reply	other threads:[~2022-07-07  8:21 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-07  8:18 [PATCH v4 0/4] regulator: output-supply DT support Naresh Solanki
2022-07-07  8:18 ` [PATCH v4 1/4] dt-bindings: vendor-prefixes: add 9elements Naresh Solanki
2022-07-11 22:19   ` Rob Herring
2022-07-07  8:18 ` [PATCH v4 2/4] dt-bindings: regulator: add bindings for output-supply Naresh Solanki
2022-07-08 18:33   ` Mark Brown
2022-07-14 14:02   ` Rob Herring
2022-07-14 14:10   ` Rob Herring
2022-07-14 14:14     ` Mark Brown
2022-07-14 14:23       ` Rob Herring
2022-07-14 14:42         ` Mark Brown
2022-07-14 15:07           ` Rob Herring
2022-07-14 15:54             ` Mark Brown
2022-07-14 16:59               ` Rob Herring
2022-07-14 17:50                 ` Mark Brown
2022-07-21  8:33                 ` Zev Weiss
2022-07-21 11:05                   ` Mark Brown
2022-07-07  8:18 ` [PATCH v4 3/4] regulator: output-supply: Add devicetree support Naresh Solanki
2022-07-07  8:18 ` Naresh Solanki [this message]
2022-07-11 19:11 ` [PATCH v4 0/4] regulator: output-supply DT support Mark Brown
2022-07-14 14:17   ` Rob Herring
2022-07-14 18:24     ` 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=20220707081826.953449-5-Naresh.Solanki@9elements.com \
    --to=naresh.solanki@9elements.com \
    --cc=broonie@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=patrick.rudolph@9elements.com \
    /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