2022-09-25 22:12:53

by Zev Weiss

[permalink] [raw]
Subject: [PATCH 3/3] regulator: userspace-consumer: Handle regulator-output DT nodes

In addition to adding some fairly simple OF support code, we make some
slight adjustments to the userspace-consumer driver to properly
support use with regulator-output hardware:

- We now do an exclusive get of the supply regulators so as to
prevent regulator_init_complete_work from automatically disabling
them.

- Instead of assuming that the supply is initially disabled, we now
query its state to determine the initial value of drvdata->enabled.

Signed-off-by: Zev Weiss <[email protected]>
---
drivers/regulator/userspace-consumer.c | 43 +++++++++++++++++---
include/linux/regulator/userspace-consumer.h | 1 +
2 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/drivers/regulator/userspace-consumer.c b/drivers/regulator/userspace-consumer.c
index 8ca28664776e..81752458f75e 100644
--- a/drivers/regulator/userspace-consumer.c
+++ b/drivers/regulator/userspace-consumer.c
@@ -14,6 +14,7 @@
#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/module.h>
+#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/regulator/consumer.h>
#include <linux/regulator/userspace-consumer.h>
@@ -24,6 +25,7 @@ struct userspace_consumer_data {

struct mutex lock;
bool enabled;
+ bool leave_on;

int num_supplies;
struct regulator_bulk_data *supplies;
@@ -102,13 +104,32 @@ static const struct attribute_group attr_group = {

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;

pdata = dev_get_platdata(&pdev->dev);
- if (!pdata)
+ if (!pdata) {
+ if (!pdev->dev.of_node)
+ return -EINVAL;
+
+ pdata = &tmpdata;
+ memset(pdata, 0, sizeof(*pdata));
+
+ pdata->num_supplies = 1;
+ pdata->supplies = devm_kzalloc(&pdev->dev, sizeof(*pdata->supplies), GFP_KERNEL);
+ if (!pdata->supplies)
+ return -ENOMEM;
+ pdata->supplies[0].supply = "vout";
+
+ pdata->leave_on = of_property_read_bool(pdev->dev.of_node, "regulator-leave-on");
+ }
+
+ if (pdata->num_supplies < 1) {
+ dev_err(&pdev->dev, "At least one supply required\n");
return -EINVAL;
+ }

drvdata = devm_kzalloc(&pdev->dev,
sizeof(struct userspace_consumer_data),
@@ -119,11 +140,12 @@ 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->leave_on = pdata->leave_on;

mutex_init(&drvdata->lock);

- ret = devm_regulator_bulk_get(&pdev->dev, drvdata->num_supplies,
- drvdata->supplies);
+ ret = devm_regulator_bulk_get_exclusive(&pdev->dev, drvdata->num_supplies,
+ drvdata->supplies);
if (ret) {
dev_err(&pdev->dev, "Failed to get supplies: %d\n", ret);
return ret;
@@ -143,7 +165,12 @@ static int regulator_userspace_consumer_probe(struct platform_device *pdev)
}
}

- drvdata->enabled = pdata->init_on;
+ ret = regulator_is_enabled(pdata->supplies[0].consumer);
+ if (ret < 0) {
+ dev_err(&pdev->dev, "Failed to get regulator status\n");
+ goto err_enable;
+ }
+ drvdata->enabled = !!ret;
platform_set_drvdata(pdev, drvdata);

return 0;
@@ -160,17 +187,23 @@ static int regulator_userspace_consumer_remove(struct platform_device *pdev)

sysfs_remove_group(&pdev->dev.kobj, &attr_group);

- if (data->enabled)
+ if (data->enabled && !data->leave_on)
regulator_bulk_disable(data->num_supplies, data->supplies);

return 0;
}

+static const struct of_device_id regulator_userspace_consumer_of_match[] = {
+ { .compatible = "regulator-output", },
+ {},
+};
+
static struct platform_driver regulator_userspace_consumer_driver = {
.probe = regulator_userspace_consumer_probe,
.remove = regulator_userspace_consumer_remove,
.driver = {
.name = "reg-userspace-consumer",
+ .of_match_table = regulator_userspace_consumer_of_match,
},
};

diff --git a/include/linux/regulator/userspace-consumer.h b/include/linux/regulator/userspace-consumer.h
index b5dba0628951..7eac57ede8ad 100644
--- a/include/linux/regulator/userspace-consumer.h
+++ b/include/linux/regulator/userspace-consumer.h
@@ -21,6 +21,7 @@ struct regulator_userspace_consumer_data {
struct regulator_bulk_data *supplies;

bool init_on;
+ bool leave_on;
};

#endif /* __REGULATOR_PLATFORM_CONSUMER_H_ */
--
2.37.3