From 8a7a8e2c300da9d317bc12cad53e4f495978b476 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ond=C5=99ej=20Jirman?= <megi@xff.cz>
Date: Mon, 8 Jun 2020 00:15:04 +0200
Subject: regulator: axp20x: Add support for vin-supply for drivevbus

When drivevbus is used to control some regulator via the N_VBUSEN
pin of the PMIC, this regulator may have a source supply. Allow to
specify the supply using the vin-supply property.

Signed-off-by: Ondrej Jirman <megi@xff.cz>
---
 drivers/regulator/axp20x-regulator.c | 30 +++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/drivers/regulator/axp20x-regulator.c b/drivers/regulator/axp20x-regulator.c
index dca99cfb7cbb..0f2480316d5a 100644
--- a/drivers/regulator/axp20x-regulator.c
+++ b/drivers/regulator/axp20x-regulator.c
@@ -1734,12 +1734,36 @@ static int axp20x_regulator_probe(struct platform_device *pdev)
 	}
 
 	if (drivevbus) {
+		struct regulator_desc *new_desc;
+		bool drivevbus_vin = false;
+		struct device_node *nr, *nd;
+
+		nr = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
+		if (nr) {
+			nd = of_get_child_by_name(nr, "drivevbus");
+			if (nd) {
+				drivevbus_vin = !!of_find_property(nd, "vin-supply", NULL);
+				of_node_put(nd);
+			}
+
+			of_node_put(nr);
+		}
+
+		new_desc = devm_kzalloc(&pdev->dev, sizeof(*new_desc), GFP_KERNEL);
+		if (!new_desc)
+			return -ENOMEM;
+
+		*new_desc = axp22x_drivevbus_regulator;
+
+		if (drivevbus_vin) {
+			new_desc->supply_name = "vin";
+			dev_info(&pdev->dev, "drivevbus has vin\n");
+		}
+
 		/* Change N_VBUSEN sense pin to DRIVEVBUS output pin */
 		regmap_update_bits(axp20x->regmap, AXP20X_OVER_TMP,
 				   AXP22X_MISC_N_VBUSEN_FUNC, 0);
-		rdev = devm_regulator_register(&pdev->dev,
-					       &axp22x_drivevbus_regulator,
-					       &config);
+		rdev = devm_regulator_register(&pdev->dev, new_desc, &config);
 		if (IS_ERR(rdev)) {
 			dev_err(&pdev->dev, "Failed to register drivevbus\n");
 			return PTR_ERR(rdev);
-- 
2.35.3

