Messages in this thread Patch in this message | | | From | Naresh Solanki <> | Subject | [PATCH v4 4/4] regulator: output-supply: Add Notification support | Date | Thu, 7 Jul 2022 10:18:26 +0200 |
| |
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
| |