[PATCH v2 2/2] regulator: userspace-consumer: Support multiple supplies in DT

From: Naresh Solanki
Date: Thu Apr 20 2023 - 15:25:04 EST


Add support for optional DT property 'regulator-supplies' to handle
connectors with multiple supplies.

If this property is present, it will determine all regulator supplies.
Otherwise, the 'vout' supply will be used as a fallback.

This change improves support for some connector output like PCIe
connectors on mainboards that can be powered by 12V and 3.3V supplies.

Signed-off-by: Naresh Solanki <Naresh.Solanki@xxxxxxxxxxxxx>
...
Change in V2:
- Update commit message
- Code improved to add fallback if the property isn't used.
---
drivers/regulator/userspace-consumer.c | 33 +++++++++++++++++++++-----
1 file changed, 27 insertions(+), 6 deletions(-)

diff --git a/drivers/regulator/userspace-consumer.c b/drivers/regulator/userspace-consumer.c
index 97f075ed68c9..aaa0189d6dab 100644
--- a/drivers/regulator/userspace-consumer.c
+++ b/drivers/regulator/userspace-consumer.c
@@ -120,7 +120,10 @@ 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;
+ struct device_node *np = pdev->dev.of_node;
+ struct property *prop;
+ const char *supply;
+ int ret, count = 0;

pdata = dev_get_platdata(&pdev->dev);
if (!pdata) {
@@ -131,11 +134,29 @@ static int regulator_userspace_consumer_probe(struct platform_device *pdev)
memset(pdata, 0, sizeof(*pdata));

pdata->no_autoswitch = true;
- 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";
+
+ if (of_find_property(np, "regulator-supplies", NULL)) {
+ pdata->num_supplies = of_property_count_strings(np, "regulator-supplies");
+ if (pdata->num_supplies < 1) {
+ dev_err(&pdev->dev,
+ "could not parse property regulator-supplies\n");
+ return -EINVAL;
+ }
+ pdata->supplies = devm_kzalloc(&pdev->dev, sizeof(*pdata->supplies) *
+ pdata->num_supplies, GFP_KERNEL);
+ if (!pdata->supplies)
+ return -ENOMEM;
+ of_property_for_each_string(np, "regulator-supplies", prop, supply) {
+ pdata->supplies[count++].supply = supply;
+ }
+ } else {
+ 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";
+ }
}

if (pdata->num_supplies < 1) {
--
2.39.1