linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Bartosz Golaszewski <brgl@bgdev.pl>
To: Liam Girdwood <lgirdwood@gmail.com>, Mark Brown <broonie@kernel.org>
Cc: linux-kernel@vger.kernel.org,
	Bartosz Golaszewski <bgolaszewski@baylibre.com>
Subject: [PATCH 2/2] regulator: provide regulator_set_voltage_sel_regmap_step() helper
Date: Mon, 10 Dec 2018 16:10:24 +0100	[thread overview]
Message-ID: <20181210151024.3906-3-brgl@bgdev.pl> (raw)
In-Reply-To: <20181210151024.3906-1-brgl@bgdev.pl>

From: Bartosz Golaszewski <bgolaszewski@baylibre.com>

On some devices we need to manually ramp the regulators to desired
voltage one step at a time. This patch adds a helper routine for
regmap users that checks if the regulator is enabled and, if so,
iterates over all selectors between the current one and the one
representing the targeted voltage setting.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
 drivers/regulator/helpers.c      | 48 ++++++++++++++++++++++++++++++++
 include/linux/regulator/driver.h |  2 ++
 2 files changed, 50 insertions(+)

diff --git a/drivers/regulator/helpers.c b/drivers/regulator/helpers.c
index 5686a1335bd3..6eb0e147bbc9 100644
--- a/drivers/regulator/helpers.c
+++ b/drivers/regulator/helpers.c
@@ -19,6 +19,8 @@
 #include <linux/regulator/driver.h>
 #include <linux/module.h>
 
+#include "internal.h"
+
 /**
  * regulator_is_enabled_regmap - standard is_enabled() for regmap users
  *
@@ -279,6 +281,52 @@ int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
 }
 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
 
+/**
+ * regulator_set_voltage_sel_regmap_step - stepping set_voltage_sel for regmap
+ *                                         users
+ *
+ * @rdev: regulator to operate on
+ * @sel: Selector to set
+ *
+ * Regulators that use regmap for their register I/O but want to ramp up/down
+ * to the selected voltage one step at a time can use this routine as their
+ * set_voltage_sel operation.
+ */
+int regulator_set_voltage_sel_regmap_step(struct regulator_dev *rdev,
+					  unsigned int sel)
+{
+	int ret, curr, diff, i, end;
+	bool asc;
+
+	/*
+	 * If the regulator is disabled, we can program the desired
+	 * voltage right away.
+	 */
+	if (!_regulator_is_enabled(rdev))
+		return regulator_set_voltage_sel_regmap(rdev, sel);
+
+	curr = regulator_get_voltage_sel_regmap(rdev);
+	if (curr < 0)
+		return curr;
+
+	diff = curr - sel;
+	if (diff == 0)
+		return 0; /* Already there. */
+
+	asc = diff > 0 ? false : true;
+	end = asc ? sel + 1 : sel - 1;
+	asc ? curr++ : curr--;
+
+	for (i = curr; i != end; asc ? i++ : i--) {
+		ret = regulator_set_voltage_sel_regmap(rdev, i);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap_step);
+
 /**
  * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
  *
diff --git a/include/linux/regulator/driver.h b/include/linux/regulator/driver.h
index a9c030192147..eedd8b495900 100644
--- a/include/linux/regulator/driver.h
+++ b/include/linux/regulator/driver.h
@@ -530,6 +530,8 @@ int regulator_set_voltage_sel_pickable_regmap(struct regulator_dev *rdev,
 						unsigned int sel);
 int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev);
 int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel);
+int regulator_set_voltage_sel_regmap_step(struct regulator_dev *rdev,
+					  unsigned int sel);
 int regulator_is_enabled_regmap(struct regulator_dev *rdev);
 int regulator_enable_regmap(struct regulator_dev *rdev);
 int regulator_disable_regmap(struct regulator_dev *rdev);
-- 
2.19.1


  parent reply	other threads:[~2018-12-10 15:10 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-10 15:10 [PATCH 0/2] regulator: stepping set voltage for regmap users Bartosz Golaszewski
2018-12-10 15:10 ` [PATCH 1/2] regulator: make _regulator_is_enabled() available in internal.h Bartosz Golaszewski
2018-12-10 15:10 ` Bartosz Golaszewski [this message]
2018-12-10 15:41   ` [PATCH 2/2] regulator: provide regulator_set_voltage_sel_regmap_step() helper Mark Brown
2018-12-11 10:55     ` Bartosz Golaszewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181210151024.3906-3-brgl@bgdev.pl \
    --to=brgl@bgdev.pl \
    --cc=bgolaszewski@baylibre.com \
    --cc=broonie@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).