[PATCH v3 05/12] opp: Add dev_pm_opp_set_voltage()

Dmitry Osipenko
2021-01-18T00:59:36Z

Add dev_pm_opp_set_voltage() which allows OPP table users to set voltage
in accordance to a given OPP. In particular this is needed for driving
voltage of a generic power domain which uses OPPs and doesn't have a
clock.

Tested-by: Peter Geis <pgwipeout@gmail.com>
Tested-by: Nicolas Chauvet <kwizart@gmail.com>
Tested-by: Matt Merhar <mattmerhar@protonmail.com>
Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
---
drivers/opp/core.c | 55 ++++++++++++++++++++++++++++++++++++++++++
include/linux/pm_opp.h | 6 +++++
2 files changed, 61 insertions(+)

diff --git a/drivers/opp/core.c b/drivers/opp/core.c
index 99d18befc209..341484d58e6c 100644
--- a/drivers/opp/core.c
+++ b/drivers/opp/core.c
@@ -2731,3 +2731,58 @@ int dev_pm_opp_sync_regulators(struct device *dev)
return ret;
}
EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);
+
+/**
+ * dev_pm_opp_set_voltage() - Change voltage of regulators
+ * @dev: device for which we do this operation
+ * @opp: opp based on which the voltages are to be configured
+ *
+ * Change voltage of the OPP table regulators.
+ *
+ * Return: 0 on success or a negative error value.
+ */
+int dev_pm_opp_set_voltage(struct device *dev, struct dev_pm_opp *opp)
+{
+ struct opp_table *opp_table;
+ struct regulator *reg;
+ int ret = 0;
+
+ /* Device may not have OPP table */
+ opp_table = _find_opp_table(dev);
+ if (IS_ERR(opp_table))
+ return 0;
+
+ /* Regulator may not be required for the device */
+ if (!opp_table->regulators)
+ goto put_table;
+
+ /* This function only supports single regulator per device */
+ if (WARN_ON(opp_table->regulator_count > 1)) {
+ dev_err(dev, "multiple regulators are not supported\n");
+ ret = -EINVAL;
+ goto put_table;
+ }
+
+ mutex_lock(&opp_table->lock);
+
+ reg = opp_table->regulators[0];
+ ret = _set_opp_voltage(dev, reg, opp->supplies);
+
+ if (!opp_table->enabled) {
+ ret = regulator_enable(reg);
+ if (ret < 0) {
+ dev_warn(dev, "Failed to enable regulator: %d", ret);
+ goto unlock;
+ }
+
+ opp_table->enabled = true;
+ }
+unlock:
+ mutex_unlock(&opp_table->lock);
+put_table:
+ /* Drop reference taken by _find_opp_table() */
+ dev_pm_opp_put_opp_table(opp_table);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(dev_pm_opp_set_voltage);
diff --git a/include/linux/pm_opp.h b/include/linux/pm_opp.h
index 1c3a09cc8dcd..f344be844bde 100644
--- a/include/linux/pm_opp.h
+++ b/include/linux/pm_opp.h
@@ -163,6 +163,7 @@ int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
void dev_pm_opp_remove_table(struct device *dev);
void dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask);
int dev_pm_opp_sync_regulators(struct device *dev);
+int dev_pm_opp_set_voltage(struct device *dev, struct dev_pm_opp *opp);
#else
static inline struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
{
@@ -404,6 +405,11 @@ static inline int dev_pm_opp_sync_regulators(struct device *dev)
return -ENOTSUPP;
}

+static inline int dev_pm_opp_set_voltage(struct device *dev, struct dev_pm_opp *opp)
+{
+ return -ENOTSUPP;
+}
+
#endif /* CONFIG_PM_OPP */

#if defined(CONFIG_PM_OPP) && defined(CONFIG_OF)
--
2.29.2