linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
To: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
Cc: Lee Jones <lee.jones@linaro.org>,
	Rob Herring <robh+dt@kernel.org>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>,
	Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>,
	devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-power@fi.rohmeurope.com
Subject: [PATCH v5 13/19] regulator: Add regmap helper for ramp-delay setting
Date: Mon, 29 Mar 2021 15:59:04 +0300	[thread overview]
Message-ID: <f101f1db564cf32cb58719c77af0b00d7236bb89.1617020713.git.matti.vaittinen@fi.rohmeurope.com> (raw)
In-Reply-To: <cover.1617020713.git.matti.vaittinen@fi.rohmeurope.com>

Quite a few regulator ICs do support setting ramp-delay by writing a value
matching the delay to a ramp-delay register.

Provide a simple helper for table-based delay setting.

Signed-off-by: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
---
Changes since v4:
 - new patch
 drivers/regulator/helpers.c      | 65 ++++++++++++++++++++++++++++++++
 include/linux/regulator/driver.h |  5 +++
 2 files changed, 70 insertions(+)

diff --git a/drivers/regulator/helpers.c b/drivers/regulator/helpers.c
index 3e19ecbf7267..0e16e31c968f 100644
--- a/drivers/regulator/helpers.c
+++ b/drivers/regulator/helpers.c
@@ -901,3 +901,68 @@ bool regulator_is_equal(struct regulator *reg1, struct regulator *reg2)
 	return reg1->rdev == reg2->rdev;
 }
 EXPORT_SYMBOL_GPL(regulator_is_equal);
+
+static int find_closest_bigger(unsigned int target, const unsigned int *table,
+			       unsigned int num_sel, unsigned int *sel)
+{
+	unsigned int s, tmp, max, maxsel = 0;
+	bool found = false;
+
+	max = table[0];
+
+	for (s = 0; s < num_sel; s++) {
+		if (table[s] > max) {
+			max = table[s];
+			maxsel = s;
+		}
+		if (table[s] >= target) {
+			if (!found || table[s] - target < tmp - target) {
+				tmp = table[s];
+				*sel = s;
+				found = true;
+				if (tmp == target)
+					break;
+			}
+		}
+	}
+
+	if (!found) {
+		*sel = maxsel;
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+/**
+ * regulator_set_ramp_delay_regmap - set_ramp_delay() helper
+ *
+ * @rdev: regulator to operate on
+ *
+ * Regulators that use regmap for their register I/O can set the ramp_reg
+ * and ramp_mask fields in their descriptor and then use this as their
+ * set_ramp_delay operation, saving some code.
+ */
+int regulator_set_ramp_delay_regmap(struct regulator_dev *rdev, int ramp_delay)
+{
+	int ret;
+	unsigned int sel;
+
+	if (!rdev->desc->n_ramp_values)
+		return -EINVAL;
+
+	ret = find_closest_bigger(ramp_delay, rdev->desc->ramp_delay_table,
+				  rdev->desc->n_ramp_values, &sel);
+
+	if (ret) {
+		dev_warn(rdev_get_dev(rdev),
+			 "Can't set ramp-delay %u, setting %u\n", ramp_delay,
+			 rdev->desc->ramp_delay_table[sel]);
+	}
+
+	sel <<= ffs(rdev->desc->ramp_mask) - 1;
+
+	return regmap_update_bits(rdev->regmap, rdev->desc->ramp_reg,
+				  rdev->desc->ramp_mask, sel);
+}
+EXPORT_SYMBOL_GPL(regulator_set_ramp_delay_regmap);
diff --git a/include/linux/regulator/driver.h b/include/linux/regulator/driver.h
index 39a540111645..597ed117086f 100644
--- a/include/linux/regulator/driver.h
+++ b/include/linux/regulator/driver.h
@@ -373,6 +373,10 @@ struct regulator_desc {
 	unsigned int pull_down_reg;
 	unsigned int pull_down_mask;
 	unsigned int pull_down_val_on;
+	unsigned int ramp_reg;
+	unsigned int ramp_mask;
+	const unsigned int *ramp_delay_table;
+	unsigned int n_ramp_values;
 
 	unsigned int enable_time;
 
@@ -535,6 +539,7 @@ int regulator_set_current_limit_regmap(struct regulator_dev *rdev,
 				       int min_uA, int max_uA);
 int regulator_get_current_limit_regmap(struct regulator_dev *rdev);
 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data);
+int regulator_set_ramp_delay_regmap(struct regulator_dev *rdev, int ramp_delay);
 
 /*
  * Helper functions intended to be used by regulator drivers prior registering
-- 
2.25.4


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 

  parent reply	other threads:[~2021-03-29 13:00 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-29 12:52 [PATCH v5 00/19] Support ROHM BD71815 PMIC Matti Vaittinen
2021-03-29 12:53 ` [PATCH v5 01/19] rtc: bd70528: Do not require parent data Matti Vaittinen
2021-03-29 12:53 ` [PATCH v5 02/19] mfd: bd718x7: simplify by cleaning unnecessary device data Matti Vaittinen
2021-03-29 12:53 ` [PATCH v5 03/19] dt_bindings: bd71828: Add clock output mode Matti Vaittinen
2021-03-29 12:54 ` [PATCH v5 04/19] dt_bindings: regulator: Add ROHM BD71815 PMIC regulators Matti Vaittinen
2021-03-29 12:54 ` [PATCH v5 05/19] dt_bindings: mfd: Add ROHM BD71815 PMIC Matti Vaittinen
2021-03-29 12:54 ` [PATCH v5 06/19] mfd: Add ROHM BD71815 ID Matti Vaittinen
2021-03-29 12:55 ` [PATCH v5 07/19] mfd: Sort ROHM chip ID list for better readability Matti Vaittinen
2021-03-29 12:55 ` [PATCH v5 08/19] mfd: Support for ROHM BD71815 PMIC core Matti Vaittinen
2021-03-29 12:56 ` [PATCH v5 09/19] gpio: support ROHM BD71815 GPOs Matti Vaittinen
2021-03-30 10:11   ` Andy Shevchenko
2021-03-30 10:43     ` Matti Vaittinen
2021-03-30 10:54       ` Andy Shevchenko
2021-03-30 11:02         ` Vaittinen, Matti
2021-03-30 12:06     ` Vaittinen, Matti
2021-03-30 12:10       ` Andy Shevchenko
2021-03-29 12:57 ` [PATCH v5 10/19] regulator: helpers: Export helper voltage listing Matti Vaittinen
2021-03-29 12:58 ` [PATCH v5 11/19] regulator: rohm-regulator: linear voltage support Matti Vaittinen
2021-04-02 17:31   ` Mark Brown
2021-03-29 12:58 ` [PATCH v5 12/19] regulator: rohm-regulator: Support SNVS HW state Matti Vaittinen
2021-04-02 17:31   ` Mark Brown
2021-03-29 12:59 ` Matti Vaittinen [this message]
2021-03-29 12:59 ` [PATCH v5 14/19] regulator: bd718x7, bd71828: Use ramp-delay helper Matti Vaittinen
2021-04-02 17:33   ` Mark Brown
2021-03-29 12:59 ` [PATCH v5 15/19] regulator: Support ROHM BD71815 regulators Matti Vaittinen
2021-04-02 17:42   ` Mark Brown
2021-04-04 16:21     ` Vaittinen, Matti
2021-04-05  5:14       ` Vaittinen, Matti
2021-03-29 13:00 ` [PATCH v5 16/19] regulator: bd71815: use ramp-delay helper Matti Vaittinen
2021-04-02 19:02   ` Mark Brown
2021-04-04 16:19     ` Matti Vaittinen
2021-03-29 13:00 ` [PATCH v5 17/19] clk: bd718x7: Add support for clk gate on ROHM BD71815 PMIC Matti Vaittinen
2021-03-29 13:00 ` [PATCH v5 18/19] rtc: bd70528: Support RTC on ROHM BD71815 Matti Vaittinen
2021-03-29 13:01 ` [PATCH v5 19/19] MAINTAINERS: Add ROHM BD71815AGW Matti Vaittinen
2021-03-30 11:06 ` [PATCH v5 00/19] Support ROHM BD71815 PMIC Vaittinen, Matti
2021-04-02 19:19   ` Mark Brown
2021-04-05  5:23     ` Vaittinen, Matti
2021-04-06 11:04       ` Mark Brown
2021-04-02 19:33 ` (subset) " Mark Brown

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=f101f1db564cf32cb58719c77af0b00d7236bb89.1617020713.git.matti.vaittinen@fi.rohmeurope.com \
    --to=matti.vaittinen@fi.rohmeurope.com \
    --cc=broonie@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=lee.jones@linaro.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-power@fi.rohmeurope.com \
    --cc=robh+dt@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).