devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v7 4/6] regulator: core: Resolve coupled regulators
@ 2018-04-23 14:33 Maciej Purski
  2018-05-17 16:41 ` Applied "regulator: core: Resolve coupled regulators" to the regulator tree Mark Brown
  0 siblings, 1 reply; 15+ messages in thread
From: Maciej Purski @ 2018-04-23 14:33 UTC (permalink / raw)
  To: linux-kernel, devicetree
  Cc: Mark Brown, Fabio Estevam, Tony Lindgren, Liam Girdwood,
	Rob Herring, Mark Rutland, Marek Szyprowski, Doug Anderson,
	Bartlomiej Zolnierkiewicz, Maciej Purski

On Odroid XU3/4 and other Exynos5422 based boards there is a case, that
different devices on the board are supplied by different regulators
with non-fixed voltages. If one of these devices temporarily requires
higher voltage, there might occur a situation that the spread between
two devices' voltages is so high, that there is a risk of changing
'high' and 'low' states on the interconnection between devices powered
by those regulators.

Fill coupling descriptor with data obtained from DTS using previously
defined of_functions. Fail to register a regulator, if some data
inconsistency occurs. If some coupled regulators are not yet registered,
don't fail to register, but try to resolve them in late init call.

Signed-off-by: Maciej Purski <m.purski@samsung.com>
---
 drivers/regulator/core.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 100 insertions(+)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index acca5de..82b002e 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -4117,6 +4117,96 @@ static int regulator_register_resolve_supply(struct device *dev, void *data)
 	return 0;
 }
 
+static int regulator_fill_coupling_array(struct regulator_dev *rdev)
+{
+	struct coupling_desc *c_desc = &rdev->coupling_desc;
+	int n_coupled = c_desc->n_coupled;
+	struct regulator_dev *c_rdev;
+	int i;
+
+	for (i = 1; i < n_coupled; i++) {
+		/* already resolved */
+		if (c_desc->coupled_rdevs[i])
+			continue;
+
+		c_rdev = of_parse_coupled_regulator(rdev, i - 1);
+
+		if (c_rdev) {
+			c_desc->coupled_rdevs[i] = c_rdev;
+			c_desc->n_resolved++;
+		}
+	}
+
+	if (rdev->coupling_desc.n_resolved < n_coupled)
+		return -1;
+	else
+		return 0;
+}
+
+static int regulator_register_fill_coupling_array(struct device *dev,
+						  void *data)
+{
+	struct regulator_dev *rdev = dev_to_rdev(dev);
+
+	if (!IS_ENABLED(CONFIG_OF))
+		return 0;
+
+	if (regulator_fill_coupling_array(rdev))
+		rdev_dbg(rdev, "unable to resolve coupling\n");
+
+	return 0;
+}
+
+static int regulator_resolve_coupling(struct regulator_dev *rdev)
+{
+	int n_phandles;
+
+	if (!IS_ENABLED(CONFIG_OF))
+		n_phandles = 0;
+	else
+		n_phandles = of_get_n_coupled(rdev);
+
+	if (n_phandles + 1 > MAX_COUPLED) {
+		rdev_err(rdev, "too many regulators coupled\n");
+		return -EPERM;
+	}
+
+	/*
+	 * Every regulator should always have coupling descriptor filled with
+	 * at least pointer to itself.
+	 */
+	rdev->coupling_desc.coupled_rdevs[0] = rdev;
+	rdev->coupling_desc.n_coupled = n_phandles + 1;
+	rdev->coupling_desc.n_resolved++;
+
+	/* regulator isn't coupled */
+	if (n_phandles == 0)
+		return 0;
+
+	/* regulator, which can't change its voltage, can't be coupled */
+	if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
+		rdev_err(rdev, "voltage operation not allowed\n");
+		return -EPERM;
+	}
+
+	if (rdev->constraints->max_spread <= 0) {
+		rdev_err(rdev, "wrong max_spread value\n");
+		return -EPERM;
+	}
+
+	if (!of_check_coupling_data(rdev))
+		return -EPERM;
+
+	/*
+	 * After everything has been checked, try to fill rdevs array
+	 * with pointers to regulators parsed from device tree. If some
+	 * regulators are not registered yet, retry in late init call
+	 */
+	regulator_fill_coupling_array(rdev);
+
+	return 0;
+}
+
 /**
  * regulator_register - register regulator
  * @regulator_desc: regulator to register
@@ -4250,6 +4340,13 @@ regulator_register(const struct regulator_desc *regulator_desc,
 	if (ret < 0)
 		goto wash;
 
+	mutex_lock(&regulator_list_mutex);
+	ret = regulator_resolve_coupling(rdev);
+	mutex_unlock(&regulator_list_mutex);
+
+	if (ret != 0)
+		goto wash;
+
 	/* add consumers devices */
 	if (init_data) {
 		mutex_lock(&regulator_list_mutex);
@@ -4744,6 +4841,9 @@ static int __init regulator_init_complete(void)
 	class_for_each_device(&regulator_class, NULL, NULL,
 			      regulator_late_cleanup);
 
+	class_for_each_device(&regulator_class, NULL, NULL,
+			      regulator_register_fill_coupling_array);
+
 	return 0;
 }
 late_initcall_sync(regulator_init_complete);
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2018-05-17 16:41 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20180122143019eucas1p213b852c98cde3fb8a77b96ab0d372ee6@eucas1p2.samsung.com>
2018-01-22 14:30 ` [PATCH v4 0/7] Add coupled regulators mechanism Maciej Purski
     [not found]   ` <CGME20180122143029eucas1p284202b6e60a86b428b3aa1e6eef62bda@eucas1p2.samsung.com>
2018-01-22 14:30     ` [PATCH v4 1/7] regulator: core: Move of_find_regulator_by_node() to of_regulator.c Maciej Purski
     [not found]   ` <CGME20180122143030eucas1p2eb1054dd14902eb0be83e5c9efb11d98@eucas1p2.samsung.com>
2018-01-22 14:30     ` [PATCH v4 2/7] regulator: core: Refactor regulator_list_voltage() Maciej Purski
2018-01-26 17:35       ` Applied "regulator: core: Refactor regulator_list_voltage()" to the regulator tree Mark Brown
     [not found]   ` <CGME20180122143031eucas1p205647b3821760ad7e7a3bef5f8873d14@eucas1p2.samsung.com>
     [not found]     ` <1516631412-17542-1-git-send-email-m.purski-Sze3O3UU22JBDgjK7y7TUQ@public.gmane.org>
2018-01-22 14:30       ` [PATCH v4 3/7] regulator: bindings: Add properties for coupled regulators Maciej Purski
2018-01-22 14:30       ` [PATCH v4 6/7] regulator: core: Add voltage balancing mechanism Maciej Purski
2018-03-02 12:55         ` Applied "regulator: core: Add voltage balancing mechanism" to the regulator tree Mark Brown
2018-05-17 16:41         ` Mark Brown
2018-01-22 14:30       ` [PATCH v4 7/7] regulator: core: Change voltage setting path Maciej Purski
     [not found]   ` <CGME20180122143032eucas1p229e2ec741318df3e716e736796bbb9c1@eucas1p2.samsung.com>
2018-01-22 14:30     ` [PATCH v4 4/7] regulator: core: Parse coupled regulators properties Maciej Purski
     [not found]   ` <CGME20180122143033eucas1p2adb3b39af238b2449486389d214a3a68@eucas1p2.samsung.com>
2018-01-22 14:30     ` [PATCH v4 5/7] regulator: core: Resolve coupled regulators Maciej Purski
2018-03-02 12:55       ` Applied "regulator: core: Resolve coupled regulators" to the regulator tree Mark Brown
2018-01-26 17:31   ` [PATCH v4 0/7] Add coupled regulators mechanism Mark Brown
2018-03-01 19:38   ` Mark Brown
2018-04-23 14:33 [PATCH v7 4/6] regulator: core: Resolve coupled regulators Maciej Purski
2018-05-17 16:41 ` Applied "regulator: core: Resolve coupled regulators" to the regulator tree Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).