blob: c094d5d20fd746f5adaacc3b57b3572b6c97a2ab [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Nishanth Menone1f60b22010-10-13 00:13:10 +02002/*
3 * Generic OPP Interface
4 *
5 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
6 * Nishanth Menon
7 * Romit Dasgupta
8 * Kevin Hilman
Nishanth Menone1f60b22010-10-13 00:13:10 +02009 */
10
Viresh Kumard6d2a522015-10-17 09:45:18 +053011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Viresh Kumard54974c2016-02-09 10:30:38 +053013#include <linux/clk.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020014#include <linux/errno.h>
15#include <linux/err.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020016#include <linux/slab.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050017#include <linux/device.h>
Liam Girdwood80126ce2012-10-23 01:27:44 +020018#include <linux/export.h>
Viresh Kumar009acd12017-10-11 12:54:14 +053019#include <linux/pm_domain.h>
Viresh Kumar9f8ea962016-02-09 10:30:33 +053020#include <linux/regulator/consumer.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020021
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053022#include "opp.h"
Nishanth Menone1f60b22010-10-13 00:13:10 +020023
24/*
Viresh Kumar2c2709d2016-02-16 14:17:53 +053025 * The root of the list of all opp-tables. All opp_table structures branch off
26 * from here, with each opp_table containing the list of opps it supports in
Nishanth Menone1f60b22010-10-13 00:13:10 +020027 * various states of availability.
28 */
Viresh Kumarf47b72a2016-05-05 16:20:33 +053029LIST_HEAD(opp_tables);
Nishanth Menone1f60b22010-10-13 00:13:10 +020030/* Lock to allow exclusive modification to the device and opp lists */
Viresh Kumar2c2709d2016-02-16 14:17:53 +053031DEFINE_MUTEX(opp_table_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +020032
Viresh Kumar2c2709d2016-02-16 14:17:53 +053033static struct opp_device *_find_opp_dev(const struct device *dev,
34 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +053035{
Viresh Kumar2c2709d2016-02-16 14:17:53 +053036 struct opp_device *opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +053037
Viresh Kumar2c2709d2016-02-16 14:17:53 +053038 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
39 if (opp_dev->dev == dev)
40 return opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +053041
42 return NULL;
43}
44
Wei Yongjun6ac42392017-02-06 14:29:55 +000045static struct opp_table *_find_opp_table_unlocked(struct device *dev)
Viresh Kumar5b650b32017-01-23 10:11:48 +053046{
47 struct opp_table *opp_table;
Viresh Kumar3d255692018-08-03 07:05:21 +053048 bool found;
Viresh Kumar5b650b32017-01-23 10:11:48 +053049
50 list_for_each_entry(opp_table, &opp_tables, node) {
Viresh Kumar3d255692018-08-03 07:05:21 +053051 mutex_lock(&opp_table->lock);
52 found = !!_find_opp_dev(dev, opp_table);
53 mutex_unlock(&opp_table->lock);
54
55 if (found) {
Viresh Kumar5b650b32017-01-23 10:11:48 +053056 _get_opp_table_kref(opp_table);
57
58 return opp_table;
59 }
60 }
61
62 return ERR_PTR(-ENODEV);
63}
64
Nishanth Menone1f60b22010-10-13 00:13:10 +020065/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +053066 * _find_opp_table() - find opp_table struct using device pointer
67 * @dev: device pointer used to lookup OPP table
Nishanth Menone1f60b22010-10-13 00:13:10 +020068 *
Viresh Kumar052c6f12017-01-23 10:11:49 +053069 * Search OPP table for one containing matching device.
Nishanth Menone1f60b22010-10-13 00:13:10 +020070 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +053071 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
Nishanth Menone1f60b22010-10-13 00:13:10 +020072 * -EINVAL based on type of error.
73 *
Viresh Kumar5b650b32017-01-23 10:11:48 +053074 * The callers must call dev_pm_opp_put_opp_table() after the table is used.
Nishanth Menone1f60b22010-10-13 00:13:10 +020075 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +053076struct opp_table *_find_opp_table(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +020077{
Viresh Kumar2c2709d2016-02-16 14:17:53 +053078 struct opp_table *opp_table;
Nishanth Menone1f60b22010-10-13 00:13:10 +020079
Viresh Kumar50a3cb02015-08-12 15:59:39 +053080 if (IS_ERR_OR_NULL(dev)) {
Nishanth Menone1f60b22010-10-13 00:13:10 +020081 pr_err("%s: Invalid parameters\n", __func__);
82 return ERR_PTR(-EINVAL);
83 }
84
Viresh Kumar5b650b32017-01-23 10:11:48 +053085 mutex_lock(&opp_table_lock);
86 opp_table = _find_opp_table_unlocked(dev);
87 mutex_unlock(&opp_table_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +020088
Viresh Kumar5b650b32017-01-23 10:11:48 +053089 return opp_table;
Nishanth Menone1f60b22010-10-13 00:13:10 +020090}
91
92/**
Linus Torvaldsbaf51c42015-11-11 09:03:01 -080093 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +020094 * @opp: opp for which voltage has to be returned for
95 *
Nishanth Menon984f16c2014-12-24 11:22:57 -060096 * Return: voltage in micro volt corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +020097 * return 0
98 *
Viresh Kumardfbe4672016-12-01 16:28:19 +053099 * This is useful only for devices with single power supply.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200100 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500101unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200102{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530103 if (IS_ERR_OR_NULL(opp)) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200104 pr_err("%s: Invalid parameters\n", __func__);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530105 return 0;
106 }
Nishanth Menone1f60b22010-10-13 00:13:10 +0200107
Viresh Kumar052c6f12017-01-23 10:11:49 +0530108 return opp->supplies[0].u_volt;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200109}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500110EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200111
112/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500113 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200114 * @opp: opp for which frequency has to be returned for
115 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600116 * Return: frequency in hertz corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +0200117 * return 0
Nishanth Menone1f60b22010-10-13 00:13:10 +0200118 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500119unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200120{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530121 if (IS_ERR_OR_NULL(opp) || !opp->available) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200122 pr_err("%s: Invalid parameters\n", __func__);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530123 return 0;
124 }
Nishanth Menone1f60b22010-10-13 00:13:10 +0200125
Viresh Kumar052c6f12017-01-23 10:11:49 +0530126 return opp->rate;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200127}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500128EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200129
130/**
Rajendra Nayak5b93ac52019-01-10 09:32:02 +0530131 * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
132 * @opp: opp for which level value has to be returned for
133 *
134 * Return: level read from device tree corresponding to the opp, else
135 * return 0.
136 */
137unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
138{
139 if (IS_ERR_OR_NULL(opp) || !opp->available) {
140 pr_err("%s: Invalid parameters\n", __func__);
141 return 0;
142 }
143
144 return opp->level;
145}
146EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
147
148/**
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200149 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
150 * @opp: opp for which turbo mode is being verified
151 *
152 * Turbo OPPs are not for normal use, and can be enabled (under certain
153 * conditions) for short duration of times to finish high throughput work
154 * quickly. Running on them for longer times may overheat the chip.
155 *
156 * Return: true if opp is turbo opp, else false.
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200157 */
158bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
159{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530160 if (IS_ERR_OR_NULL(opp) || !opp->available) {
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200161 pr_err("%s: Invalid parameters\n", __func__);
162 return false;
163 }
164
Viresh Kumar052c6f12017-01-23 10:11:49 +0530165 return opp->turbo;
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200166}
167EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
168
169/**
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530170 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
171 * @dev: device for which we do this operation
172 *
173 * Return: This function returns the max clock latency in nanoseconds.
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530174 */
175unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
176{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530177 struct opp_table *opp_table;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530178 unsigned long clock_latency_ns;
179
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530180 opp_table = _find_opp_table(dev);
181 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +0530182 return 0;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530183
Viresh Kumar5b650b32017-01-23 10:11:48 +0530184 clock_latency_ns = opp_table->clock_latency_ns_max;
185
186 dev_pm_opp_put_opp_table(opp_table);
187
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530188 return clock_latency_ns;
189}
190EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
191
192/**
Viresh Kumar655c9df2016-02-09 10:30:35 +0530193 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
194 * @dev: device for which we do this operation
195 *
196 * Return: This function returns the max voltage latency in nanoseconds.
Viresh Kumar655c9df2016-02-09 10:30:35 +0530197 */
198unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
199{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530200 struct opp_table *opp_table;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530201 struct dev_pm_opp *opp;
Viresh Kumar478256b2017-05-23 09:32:11 +0530202 struct regulator *reg;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530203 unsigned long latency_ns = 0;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530204 int ret, i, count;
205 struct {
206 unsigned long min;
207 unsigned long max;
208 } *uV;
209
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530210 opp_table = _find_opp_table(dev);
211 if (IS_ERR(opp_table))
212 return 0;
213
Viresh Kumardfbe4672016-12-01 16:28:19 +0530214 /* Regulator may not be required for the device */
Viresh Kumar90e35772018-12-11 16:32:47 +0530215 if (!opp_table->regulators)
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530216 goto put_opp_table;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530217
Viresh Kumar90e35772018-12-11 16:32:47 +0530218 count = opp_table->regulator_count;
219
Viresh Kumardfbe4672016-12-01 16:28:19 +0530220 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
221 if (!uV)
Viresh Kumar478256b2017-05-23 09:32:11 +0530222 goto put_opp_table;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530223
Viresh Kumar052c6f12017-01-23 10:11:49 +0530224 mutex_lock(&opp_table->lock);
225
Viresh Kumardfbe4672016-12-01 16:28:19 +0530226 for (i = 0; i < count; i++) {
227 uV[i].min = ~0;
228 uV[i].max = 0;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530229
Viresh Kumar052c6f12017-01-23 10:11:49 +0530230 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumardfbe4672016-12-01 16:28:19 +0530231 if (!opp->available)
232 continue;
233
234 if (opp->supplies[i].u_volt_min < uV[i].min)
235 uV[i].min = opp->supplies[i].u_volt_min;
236 if (opp->supplies[i].u_volt_max > uV[i].max)
237 uV[i].max = opp->supplies[i].u_volt_max;
238 }
Viresh Kumar655c9df2016-02-09 10:30:35 +0530239 }
240
Viresh Kumar052c6f12017-01-23 10:11:49 +0530241 mutex_unlock(&opp_table->lock);
Viresh Kumar655c9df2016-02-09 10:30:35 +0530242
243 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530244 * The caller needs to ensure that opp_table (and hence the regulator)
Viresh Kumar655c9df2016-02-09 10:30:35 +0530245 * isn't freed, while we are executing this routine.
246 */
Andrzej Hajda8cc31112017-02-20 19:57:57 +0100247 for (i = 0; i < count; i++) {
Viresh Kumar478256b2017-05-23 09:32:11 +0530248 reg = opp_table->regulators[i];
Viresh Kumardfbe4672016-12-01 16:28:19 +0530249 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
250 if (ret > 0)
251 latency_ns += ret * 1000;
252 }
253
Viresh Kumardfbe4672016-12-01 16:28:19 +0530254 kfree(uV);
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530255put_opp_table:
256 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar655c9df2016-02-09 10:30:35 +0530257
258 return latency_ns;
259}
260EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
261
262/**
Viresh Kumar21743442016-02-09 10:30:36 +0530263 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
264 * nanoseconds
265 * @dev: device for which we do this operation
266 *
267 * Return: This function returns the max transition latency, in nanoseconds, to
268 * switch from one OPP to other.
Viresh Kumar21743442016-02-09 10:30:36 +0530269 */
270unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
271{
272 return dev_pm_opp_get_max_volt_latency(dev) +
273 dev_pm_opp_get_max_clock_latency(dev);
274}
275EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
276
277/**
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530278 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200279 * @dev: device for which we do this operation
280 *
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530281 * Return: This function returns the frequency of the OPP marked as suspend_opp
282 * if one is available, else returns 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200283 */
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530284unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200285{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530286 struct opp_table *opp_table;
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530287 unsigned long freq = 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200288
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530289 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530290 if (IS_ERR(opp_table))
291 return 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200292
Viresh Kumar5b650b32017-01-23 10:11:48 +0530293 if (opp_table->suspend_opp && opp_table->suspend_opp->available)
294 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530295
Viresh Kumar5b650b32017-01-23 10:11:48 +0530296 dev_pm_opp_put_opp_table(opp_table);
297
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530298 return freq;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200299}
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530300EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200301
Viresh Kumara1e8c132018-04-06 14:35:45 +0530302int _get_opp_count(struct opp_table *opp_table)
303{
304 struct dev_pm_opp *opp;
305 int count = 0;
306
307 mutex_lock(&opp_table->lock);
308
309 list_for_each_entry(opp, &opp_table->opp_list, node) {
310 if (opp->available)
311 count++;
312 }
313
314 mutex_unlock(&opp_table->lock);
315
316 return count;
317}
318
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200319/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530320 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
Nishanth Menone1f60b22010-10-13 00:13:10 +0200321 * @dev: device for which we do this operation
322 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600323 * Return: This function returns the number of available opps if there are any,
Nishanth Menone1f60b22010-10-13 00:13:10 +0200324 * else returns 0 if none or the corresponding error value.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200325 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500326int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200327{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530328 struct opp_table *opp_table;
Viresh Kumara1e8c132018-04-06 14:35:45 +0530329 int count;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200330
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530331 opp_table = _find_opp_table(dev);
332 if (IS_ERR(opp_table)) {
333 count = PTR_ERR(opp_table);
Fabio Estevam035ed072017-09-29 14:39:49 -0300334 dev_dbg(dev, "%s: OPP table not found (%d)\n",
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800335 __func__, count);
Viresh Kumar09f662f2018-10-03 15:22:03 +0530336 return count;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200337 }
338
Viresh Kumara1e8c132018-04-06 14:35:45 +0530339 count = _get_opp_count(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530340 dev_pm_opp_put_opp_table(opp_table);
341
Nishanth Menone1f60b22010-10-13 00:13:10 +0200342 return count;
343}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500344EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200345
346/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500347 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200348 * @dev: device for which we do this operation
349 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100350 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200351 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530352 * Return: Searches for exact match in the opp table and returns pointer to the
Nishanth Menon984f16c2014-12-24 11:22:57 -0600353 * matching opp if found, else returns ERR_PTR in case of error and should
354 * be handled using IS_ERR. Error return values can be:
Nishanth Menon07797262012-10-24 22:00:12 +0200355 * EINVAL: for bad pointer
356 * ERANGE: no match found for search
357 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200358 *
359 * Note: available is a modifier for the search. if available=true, then the
360 * match is for exact matching frequency and is available in the stored OPP
361 * table. if false, the match is for exact frequency which is not available.
362 *
363 * This provides a mechanism to enable an opp which is not available currently
364 * or the opposite as well.
365 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530366 * The callers are required to call dev_pm_opp_put() for the returned OPP after
367 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200368 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500369struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
370 unsigned long freq,
371 bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200372{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530373 struct opp_table *opp_table;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500374 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200375
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530376 opp_table = _find_opp_table(dev);
377 if (IS_ERR(opp_table)) {
378 int r = PTR_ERR(opp_table);
379
380 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200381 return ERR_PTR(r);
382 }
383
Viresh Kumar052c6f12017-01-23 10:11:49 +0530384 mutex_lock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530385
Viresh Kumar052c6f12017-01-23 10:11:49 +0530386 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200387 if (temp_opp->available == available &&
388 temp_opp->rate == freq) {
389 opp = temp_opp;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530390
391 /* Increment the reference count of OPP */
392 dev_pm_opp_get(opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200393 break;
394 }
395 }
396
Viresh Kumar052c6f12017-01-23 10:11:49 +0530397 mutex_unlock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530398 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530399
Nishanth Menone1f60b22010-10-13 00:13:10 +0200400 return opp;
401}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500402EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200403
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800404static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
405 unsigned long *freq)
406{
407 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
408
Viresh Kumar052c6f12017-01-23 10:11:49 +0530409 mutex_lock(&opp_table->lock);
410
411 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800412 if (temp_opp->available && temp_opp->rate >= *freq) {
413 opp = temp_opp;
414 *freq = opp->rate;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530415
416 /* Increment the reference count of OPP */
417 dev_pm_opp_get(opp);
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800418 break;
419 }
420 }
421
Viresh Kumar052c6f12017-01-23 10:11:49 +0530422 mutex_unlock(&opp_table->lock);
423
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800424 return opp;
425}
426
Nishanth Menone1f60b22010-10-13 00:13:10 +0200427/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500428 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200429 * @dev: device for which we do this operation
430 * @freq: Start frequency
431 *
432 * Search for the matching ceil *available* OPP from a starting freq
433 * for a device.
434 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600435 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200436 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
437 * values can be:
438 * EINVAL: for bad pointer
439 * ERANGE: no match found for search
440 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200441 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530442 * The callers are required to call dev_pm_opp_put() for the returned OPP after
443 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200444 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500445struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
446 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200447{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530448 struct opp_table *opp_table;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530449 struct dev_pm_opp *opp;
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800450
Nishanth Menone1f60b22010-10-13 00:13:10 +0200451 if (!dev || !freq) {
452 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
453 return ERR_PTR(-EINVAL);
454 }
455
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530456 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530457 if (IS_ERR(opp_table))
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530458 return ERR_CAST(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530459
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530460 opp = _find_freq_ceil(opp_table, freq);
461
Viresh Kumar5b650b32017-01-23 10:11:48 +0530462 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530463
464 return opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200465}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500466EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200467
468/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500469 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200470 * @dev: device for which we do this operation
471 * @freq: Start frequency
472 *
473 * Search for the matching floor *available* OPP from a starting freq
474 * for a device.
475 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600476 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200477 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
478 * values can be:
479 * EINVAL: for bad pointer
480 * ERANGE: no match found for search
481 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200482 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530483 * The callers are required to call dev_pm_opp_put() for the returned OPP after
484 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200485 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500486struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
487 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200488{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530489 struct opp_table *opp_table;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500490 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200491
492 if (!dev || !freq) {
493 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
494 return ERR_PTR(-EINVAL);
495 }
496
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530497 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530498 if (IS_ERR(opp_table))
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530499 return ERR_CAST(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530500
Viresh Kumar052c6f12017-01-23 10:11:49 +0530501 mutex_lock(&opp_table->lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200502
Viresh Kumar052c6f12017-01-23 10:11:49 +0530503 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200504 if (temp_opp->available) {
505 /* go to the next node, before choosing prev */
506 if (temp_opp->rate > *freq)
507 break;
508 else
509 opp = temp_opp;
510 }
511 }
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530512
513 /* Increment the reference count of OPP */
514 if (!IS_ERR(opp))
515 dev_pm_opp_get(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530516 mutex_unlock(&opp_table->lock);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530517 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530518
Nishanth Menone1f60b22010-10-13 00:13:10 +0200519 if (!IS_ERR(opp))
520 *freq = opp->rate;
521
522 return opp;
523}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500524EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200525
Andrew-sh.Cheng2f36bde2019-03-29 14:46:10 +0800526/**
527 * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for
528 * target voltage.
529 * @dev: Device for which we do this operation.
530 * @u_volt: Target voltage.
531 *
532 * Search for OPP with highest (ceil) frequency and has voltage <= u_volt.
533 *
534 * Return: matching *opp, else returns ERR_PTR in case of error which should be
535 * handled using IS_ERR.
536 *
537 * Error return values can be:
538 * EINVAL: bad parameters
539 *
540 * The callers are required to call dev_pm_opp_put() for the returned OPP after
541 * use.
542 */
543struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev,
544 unsigned long u_volt)
545{
546 struct opp_table *opp_table;
547 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
548
549 if (!dev || !u_volt) {
550 dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__,
551 u_volt);
552 return ERR_PTR(-EINVAL);
553 }
554
555 opp_table = _find_opp_table(dev);
556 if (IS_ERR(opp_table))
557 return ERR_CAST(opp_table);
558
559 mutex_lock(&opp_table->lock);
560
561 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
562 if (temp_opp->available) {
563 if (temp_opp->supplies[0].u_volt > u_volt)
564 break;
565 opp = temp_opp;
566 }
567 }
568
569 /* Increment the reference count of OPP */
570 if (!IS_ERR(opp))
571 dev_pm_opp_get(opp);
572
573 mutex_unlock(&opp_table->lock);
574 dev_pm_opp_put_opp_table(opp_table);
575
576 return opp;
577}
578EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt);
579
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530580static int _set_opp_voltage(struct device *dev, struct regulator *reg,
Viresh Kumarce317812016-12-01 16:28:18 +0530581 struct dev_pm_opp_supply *supply)
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530582{
583 int ret;
584
585 /* Regulator not available for device */
586 if (IS_ERR(reg)) {
587 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
588 PTR_ERR(reg));
589 return 0;
590 }
591
Viresh Kumarce317812016-12-01 16:28:18 +0530592 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
593 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530594
Viresh Kumarce317812016-12-01 16:28:18 +0530595 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
596 supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530597 if (ret)
598 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
Viresh Kumarce317812016-12-01 16:28:18 +0530599 __func__, supply->u_volt_min, supply->u_volt,
600 supply->u_volt_max, ret);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530601
602 return ret;
603}
604
Viresh Kumar285881b2019-01-31 13:53:25 +0530605static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
606 unsigned long freq)
Viresh Kumar94735582016-12-01 16:28:20 +0530607{
608 int ret;
609
610 ret = clk_set_rate(clk, freq);
611 if (ret) {
612 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
613 ret);
614 }
615
616 return ret;
617}
618
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530619static int _generic_set_opp_regulator(const struct opp_table *opp_table,
620 struct device *dev,
621 unsigned long old_freq,
622 unsigned long freq,
623 struct dev_pm_opp_supply *old_supply,
624 struct dev_pm_opp_supply *new_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530625{
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530626 struct regulator *reg = opp_table->regulators[0];
Viresh Kumar94735582016-12-01 16:28:20 +0530627 int ret;
628
629 /* This function only supports single regulator per device */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530630 if (WARN_ON(opp_table->regulator_count > 1)) {
Viresh Kumar94735582016-12-01 16:28:20 +0530631 dev_err(dev, "multiple regulators are not supported\n");
632 return -EINVAL;
633 }
634
635 /* Scaling up? Scale voltage before frequency */
Waldemar Rymarkiewiczc5c2a972018-06-14 15:56:08 +0200636 if (freq >= old_freq) {
Viresh Kumar94735582016-12-01 16:28:20 +0530637 ret = _set_opp_voltage(dev, reg, new_supply);
638 if (ret)
639 goto restore_voltage;
640 }
641
642 /* Change frequency */
Viresh Kumar285881b2019-01-31 13:53:25 +0530643 ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
Viresh Kumar94735582016-12-01 16:28:20 +0530644 if (ret)
645 goto restore_voltage;
646
647 /* Scaling down? Scale voltage after frequency */
648 if (freq < old_freq) {
649 ret = _set_opp_voltage(dev, reg, new_supply);
650 if (ret)
651 goto restore_freq;
652 }
653
654 return 0;
655
656restore_freq:
Viresh Kumar285881b2019-01-31 13:53:25 +0530657 if (_generic_set_opp_clk_only(dev, opp_table->clk, old_freq))
Viresh Kumar94735582016-12-01 16:28:20 +0530658 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
659 __func__, old_freq);
660restore_voltage:
661 /* This shouldn't harm even if the voltages weren't updated earlier */
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530662 if (old_supply)
Viresh Kumar94735582016-12-01 16:28:20 +0530663 _set_opp_voltage(dev, reg, old_supply);
664
665 return ret;
666}
667
Viresh Kumar7e5359932018-06-12 14:47:03 +0530668static int _set_opp_custom(const struct opp_table *opp_table,
669 struct device *dev, unsigned long old_freq,
670 unsigned long freq,
671 struct dev_pm_opp_supply *old_supply,
672 struct dev_pm_opp_supply *new_supply)
673{
674 struct dev_pm_set_opp_data *data;
675 int size;
676
677 data = opp_table->set_opp_data;
678 data->regulators = opp_table->regulators;
679 data->regulator_count = opp_table->regulator_count;
680 data->clk = opp_table->clk;
681 data->dev = dev;
682
683 data->old_opp.rate = old_freq;
684 size = sizeof(*old_supply) * opp_table->regulator_count;
Dmitry Osipenko560d1bc2019-06-23 20:50:53 +0300685 if (!old_supply)
Viresh Kumar7e5359932018-06-12 14:47:03 +0530686 memset(data->old_opp.supplies, 0, size);
687 else
688 memcpy(data->old_opp.supplies, old_supply, size);
689
690 data->new_opp.rate = freq;
691 memcpy(data->new_opp.supplies, new_supply, size);
692
693 return opp_table->set_opp(data);
694}
695
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530696/* This is only called for PM domain for now */
697static int _set_required_opps(struct device *dev,
698 struct opp_table *opp_table,
699 struct dev_pm_opp *opp)
700{
701 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
702 struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
703 unsigned int pstate;
704 int i, ret = 0;
705
706 if (!required_opp_tables)
707 return 0;
708
709 /* Single genpd case */
710 if (!genpd_virt_devs) {
Rajendra Nayakcd7ea582019-03-20 15:19:09 +0530711 pstate = likely(opp) ? opp->required_opps[0]->pstate : 0;
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530712 ret = dev_pm_genpd_set_performance_state(dev, pstate);
713 if (ret) {
714 dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
715 dev_name(dev), pstate, ret);
716 }
717 return ret;
718 }
719
720 /* Multiple genpd case */
721
722 /*
723 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
724 * after it is freed from another thread.
725 */
726 mutex_lock(&opp_table->genpd_virt_dev_lock);
727
728 for (i = 0; i < opp_table->required_opp_count; i++) {
Rajendra Nayakcd7ea582019-03-20 15:19:09 +0530729 pstate = likely(opp) ? opp->required_opps[i]->pstate : 0;
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530730
731 if (!genpd_virt_devs[i])
732 continue;
733
734 ret = dev_pm_genpd_set_performance_state(genpd_virt_devs[i], pstate);
735 if (ret) {
736 dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
737 dev_name(genpd_virt_devs[i]), pstate, ret);
738 break;
739 }
740 }
741 mutex_unlock(&opp_table->genpd_virt_dev_lock);
742
743 return ret;
744}
745
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530746/**
747 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
748 * @dev: device for which we do this operation
749 * @target_freq: frequency to achieve
750 *
Stephen Boydb3e37592019-03-20 15:19:08 +0530751 * This configures the power-supplies to the levels specified by the OPP
752 * corresponding to the target_freq, and programs the clock to a value <=
753 * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
754 * provided by the opp, should have already rounded to the target OPP's
755 * frequency.
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530756 */
757int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
758{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530759 struct opp_table *opp_table;
Stephen Boydb3e37592019-03-20 15:19:08 +0530760 unsigned long freq, old_freq, temp_freq;
Viresh Kumar94735582016-12-01 16:28:20 +0530761 struct dev_pm_opp *old_opp, *opp;
Viresh Kumar94735582016-12-01 16:28:20 +0530762 struct clk *clk;
Viresh Kumar7e5359932018-06-12 14:47:03 +0530763 int ret;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530764
Viresh Kumar052c6f12017-01-23 10:11:49 +0530765 opp_table = _find_opp_table(dev);
766 if (IS_ERR(opp_table)) {
767 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
768 return PTR_ERR(opp_table);
769 }
770
Rajendra Nayakcd7ea582019-03-20 15:19:09 +0530771 if (unlikely(!target_freq)) {
772 if (opp_table->required_opp_tables) {
773 ret = _set_required_opps(dev, opp_table, NULL);
774 } else {
775 dev_err(dev, "target frequency can't be 0\n");
776 ret = -EINVAL;
777 }
778
779 goto put_opp_table;
780 }
781
Viresh Kumar052c6f12017-01-23 10:11:49 +0530782 clk = opp_table->clk;
783 if (IS_ERR(clk)) {
784 dev_err(dev, "%s: No clock available for the device\n",
785 __func__);
786 ret = PTR_ERR(clk);
787 goto put_opp_table;
788 }
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530789
790 freq = clk_round_rate(clk, target_freq);
791 if ((long)freq <= 0)
792 freq = target_freq;
793
794 old_freq = clk_get_rate(clk);
795
796 /* Return early if nothing to do */
797 if (old_freq == freq) {
798 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
799 __func__, freq);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530800 ret = 0;
801 goto put_opp_table;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530802 }
803
Stephen Boydb3e37592019-03-20 15:19:08 +0530804 temp_freq = old_freq;
805 old_opp = _find_freq_ceil(opp_table, &temp_freq);
Arnd Bergmann4df27c92016-09-15 17:38:38 +0200806 if (IS_ERR(old_opp)) {
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530807 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
808 __func__, old_freq, PTR_ERR(old_opp));
809 }
810
Stephen Boydb3e37592019-03-20 15:19:08 +0530811 temp_freq = freq;
812 opp = _find_freq_ceil(opp_table, &temp_freq);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530813 if (IS_ERR(opp)) {
814 ret = PTR_ERR(opp);
815 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
816 __func__, freq, ret);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530817 goto put_old_opp;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530818 }
819
Viresh Kumar94735582016-12-01 16:28:20 +0530820 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
821 old_freq, freq);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530822
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530823 /* Scaling up? Configure required OPPs before frequency */
Viresh Kumarfaef0802019-03-12 10:27:18 +0530824 if (freq >= old_freq) {
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530825 ret = _set_required_opps(dev, opp_table, opp);
826 if (ret)
827 goto put_opp;
828 }
829
Viresh Kumar7e5359932018-06-12 14:47:03 +0530830 if (opp_table->set_opp) {
831 ret = _set_opp_custom(opp_table, dev, old_freq, freq,
832 IS_ERR(old_opp) ? NULL : old_opp->supplies,
833 opp->supplies);
834 } else if (opp_table->regulators) {
Viresh Kumarc74b32f2017-05-23 09:32:10 +0530835 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
836 IS_ERR(old_opp) ? NULL : old_opp->supplies,
837 opp->supplies);
838 } else {
Viresh Kumar7e5359932018-06-12 14:47:03 +0530839 /* Only frequency scaling */
Viresh Kumar285881b2019-01-31 13:53:25 +0530840 ret = _generic_set_opp_clk_only(dev, clk, freq);
Viresh Kumardfbe4672016-12-01 16:28:19 +0530841 }
842
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530843 /* Scaling down? Configure required OPPs after frequency */
844 if (!ret && freq < old_freq) {
845 ret = _set_required_opps(dev, opp_table, opp);
846 if (ret)
847 dev_err(dev, "Failed to set required opps: %d\n", ret);
848 }
849
850put_opp:
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530851 dev_pm_opp_put(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530852put_old_opp:
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530853 if (!IS_ERR(old_opp))
854 dev_pm_opp_put(old_opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530855put_opp_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +0530856 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530857 return ret;
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530858}
859EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
860
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530861/* OPP-dev Helpers */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530862static void _remove_opp_dev(struct opp_device *opp_dev,
863 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530864{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530865 opp_debug_unregister(opp_dev, opp_table);
866 list_del(&opp_dev->node);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530867 kfree(opp_dev);
Viresh Kumar06441652015-07-29 16:23:04 +0530868}
869
Viresh Kumar283d55e2018-09-07 09:01:54 +0530870static struct opp_device *_add_opp_dev_unlocked(const struct device *dev,
871 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +0530872{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530873 struct opp_device *opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530874
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530875 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
876 if (!opp_dev)
Viresh Kumar06441652015-07-29 16:23:04 +0530877 return NULL;
878
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530879 /* Initialize opp-dev */
880 opp_dev->dev = dev;
Viresh Kumar3d255692018-08-03 07:05:21 +0530881
Viresh Kumar052c6f12017-01-23 10:11:49 +0530882 list_add(&opp_dev->node, &opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530883
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530884 /* Create debugfs entries for the opp_table */
Greg Kroah-Hartmana2dea4c2019-01-22 16:21:17 +0100885 opp_debug_register(opp_dev, opp_table);
Viresh Kumar283d55e2018-09-07 09:01:54 +0530886
887 return opp_dev;
888}
889
890struct opp_device *_add_opp_dev(const struct device *dev,
891 struct opp_table *opp_table)
892{
893 struct opp_device *opp_dev;
894
895 mutex_lock(&opp_table->lock);
896 opp_dev = _add_opp_dev_unlocked(dev, opp_table);
Viresh Kumar3d255692018-08-03 07:05:21 +0530897 mutex_unlock(&opp_table->lock);
Viresh Kumardeaa5142015-11-11 07:59:01 +0530898
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530899 return opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +0530900}
901
Viresh Kumareb7c87432018-09-05 16:17:14 +0530902static struct opp_table *_allocate_opp_table(struct device *dev, int index)
Viresh Kumar07cce742014-12-10 09:45:34 +0530903{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530904 struct opp_table *opp_table;
905 struct opp_device *opp_dev;
Viresh Kumard54974c2016-02-09 10:30:38 +0530906 int ret;
Viresh Kumar07cce742014-12-10 09:45:34 +0530907
908 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530909 * Allocate a new OPP table. In the infrequent case where a new
Viresh Kumar07cce742014-12-10 09:45:34 +0530910 * device is needed to be added, we pay this penalty.
911 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530912 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
913 if (!opp_table)
Viresh Kumar07cce742014-12-10 09:45:34 +0530914 return NULL;
915
Viresh Kumar3d255692018-08-03 07:05:21 +0530916 mutex_init(&opp_table->lock);
Viresh Kumar4f018bc2018-06-26 16:29:34 +0530917 mutex_init(&opp_table->genpd_virt_dev_lock);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530918 INIT_LIST_HEAD(&opp_table->dev_list);
Viresh Kumar06441652015-07-29 16:23:04 +0530919
Viresh Kumar46f48ac2018-12-11 16:39:36 +0530920 /* Mark regulator count uninitialized */
921 opp_table->regulator_count = -1;
922
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530923 opp_dev = _add_opp_dev(dev, opp_table);
924 if (!opp_dev) {
925 kfree(opp_table);
Viresh Kumar06441652015-07-29 16:23:04 +0530926 return NULL;
927 }
928
Viresh Kumareb7c87432018-09-05 16:17:14 +0530929 _of_init_opp_table(opp_table, dev, index);
Viresh Kumar50f8cfb2016-02-09 10:30:37 +0530930
Viresh Kumard54974c2016-02-09 10:30:38 +0530931 /* Find clk for the device */
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530932 opp_table->clk = clk_get(dev, NULL);
933 if (IS_ERR(opp_table->clk)) {
934 ret = PTR_ERR(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +0530935 if (ret != -EPROBE_DEFER)
936 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
937 ret);
938 }
939
Viresh Kumar052c6f12017-01-23 10:11:49 +0530940 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530941 INIT_LIST_HEAD(&opp_table->opp_list);
Viresh Kumarf067a982017-01-23 10:11:42 +0530942 kref_init(&opp_table->kref);
Viresh Kumar07cce742014-12-10 09:45:34 +0530943
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530944 /* Secure the device table modification */
Viresh Kumar052c6f12017-01-23 10:11:49 +0530945 list_add(&opp_table->node, &opp_tables);
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530946 return opp_table;
Viresh Kumar07cce742014-12-10 09:45:34 +0530947}
948
Viresh Kumarf067a982017-01-23 10:11:42 +0530949void _get_opp_table_kref(struct opp_table *opp_table)
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530950{
Viresh Kumarf067a982017-01-23 10:11:42 +0530951 kref_get(&opp_table->kref);
952}
953
Viresh Kumareb7c87432018-09-05 16:17:14 +0530954static struct opp_table *_opp_get_opp_table(struct device *dev, int index)
Viresh Kumarf067a982017-01-23 10:11:42 +0530955{
956 struct opp_table *opp_table;
957
958 /* Hold our table modification lock here */
959 mutex_lock(&opp_table_lock);
960
Viresh Kumar5b650b32017-01-23 10:11:48 +0530961 opp_table = _find_opp_table_unlocked(dev);
962 if (!IS_ERR(opp_table))
Viresh Kumarf067a982017-01-23 10:11:42 +0530963 goto unlock;
Viresh Kumarf067a982017-01-23 10:11:42 +0530964
Viresh Kumar283d55e2018-09-07 09:01:54 +0530965 opp_table = _managed_opp(dev, index);
966 if (opp_table) {
967 if (!_add_opp_dev_unlocked(dev, opp_table)) {
968 dev_pm_opp_put_opp_table(opp_table);
969 opp_table = NULL;
970 }
971 goto unlock;
972 }
973
Viresh Kumareb7c87432018-09-05 16:17:14 +0530974 opp_table = _allocate_opp_table(dev, index);
Viresh Kumarf067a982017-01-23 10:11:42 +0530975
976unlock:
977 mutex_unlock(&opp_table_lock);
978
979 return opp_table;
980}
Viresh Kumareb7c87432018-09-05 16:17:14 +0530981
982struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
983{
984 return _opp_get_opp_table(dev, 0);
985}
Viresh Kumarf067a982017-01-23 10:11:42 +0530986EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
987
Viresh Kumareb7c87432018-09-05 16:17:14 +0530988struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev,
989 int index)
990{
991 return _opp_get_opp_table(dev, index);
992}
993
Viresh Kumarb83c1892017-01-23 10:11:45 +0530994static void _opp_table_kref_release(struct kref *kref)
Viresh Kumarf067a982017-01-23 10:11:42 +0530995{
996 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
Viresh Kumarcdd6ed92018-09-12 12:35:19 +0530997 struct opp_device *opp_dev, *temp;
Viresh Kumar06441652015-07-29 16:23:04 +0530998
Viresh Kumar5d6d1062018-06-07 14:50:43 +0530999 _of_clear_opp_table(opp_table);
1000
Viresh Kumard54974c2016-02-09 10:30:38 +05301001 /* Release clk */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301002 if (!IS_ERR(opp_table->clk))
1003 clk_put(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +05301004
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05301005 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar06441652015-07-29 16:23:04 +05301006
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05301007 list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
1008 /*
1009 * The OPP table is getting removed, drop the performance state
1010 * constraints.
1011 */
1012 if (opp_table->genpd_performance_state)
1013 dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
Viresh Kumar06441652015-07-29 16:23:04 +05301014
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05301015 _remove_opp_dev(opp_dev, opp_table);
1016 }
Viresh Kumar06441652015-07-29 16:23:04 +05301017
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301018 mutex_destroy(&opp_table->genpd_virt_dev_lock);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301019 mutex_destroy(&opp_table->lock);
Viresh Kumar052c6f12017-01-23 10:11:49 +05301020 list_del(&opp_table->node);
1021 kfree(opp_table);
Viresh Kumar3bac42c2015-07-29 16:22:59 +05301022
Viresh Kumarf067a982017-01-23 10:11:42 +05301023 mutex_unlock(&opp_table_lock);
1024}
1025
Viresh Kumard0e8ae62018-09-11 11:14:11 +05301026void _opp_remove_all_static(struct opp_table *opp_table)
1027{
1028 struct dev_pm_opp *opp, *tmp;
1029
1030 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1031 if (!opp->dynamic)
1032 dev_pm_opp_put(opp);
1033 }
1034
1035 opp_table->parsed_static_opps = false;
1036}
1037
1038static void _opp_table_list_kref_release(struct kref *kref)
1039{
1040 struct opp_table *opp_table = container_of(kref, struct opp_table,
1041 list_kref);
1042
1043 _opp_remove_all_static(opp_table);
1044 mutex_unlock(&opp_table_lock);
1045}
1046
1047void _put_opp_list_kref(struct opp_table *opp_table)
1048{
1049 kref_put_mutex(&opp_table->list_kref, _opp_table_list_kref_release,
1050 &opp_table_lock);
1051}
1052
Viresh Kumarf067a982017-01-23 10:11:42 +05301053void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
1054{
1055 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
1056 &opp_table_lock);
1057}
1058EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
1059
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301060void _opp_free(struct dev_pm_opp *opp)
Viresh Kumar969fceb2017-01-02 14:40:59 +05301061{
1062 kfree(opp);
Viresh Kumar969fceb2017-01-02 14:40:59 +05301063}
1064
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301065static void _opp_kref_release(struct dev_pm_opp *opp,
1066 struct opp_table *opp_table)
Viresh Kumar129eec52014-11-27 08:54:06 +05301067{
1068 /*
1069 * Notify the changes in the availability of the operable
1070 * frequency/voltage list.
1071 */
Viresh Kumar052c6f12017-01-23 10:11:49 +05301072 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
Viresh Kumarda544b62018-06-07 14:50:43 +05301073 _of_opp_free_required_opps(opp_table, opp);
Viresh Kumardeaa5142015-11-11 07:59:01 +05301074 opp_debug_remove_one(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +05301075 list_del(&opp->node);
1076 kfree(opp);
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301077}
Viresh Kumar129eec52014-11-27 08:54:06 +05301078
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301079static void _opp_kref_release_unlocked(struct kref *kref)
1080{
1081 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1082 struct opp_table *opp_table = opp->opp_table;
1083
1084 _opp_kref_release(opp, opp_table);
1085}
1086
1087static void _opp_kref_release_locked(struct kref *kref)
1088{
1089 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1090 struct opp_table *opp_table = opp->opp_table;
1091
1092 _opp_kref_release(opp, opp_table);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301093 mutex_unlock(&opp_table->lock);
Viresh Kumar129eec52014-11-27 08:54:06 +05301094}
1095
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301096void dev_pm_opp_get(struct dev_pm_opp *opp)
Viresh Kumar8a31d9d92017-01-23 10:11:47 +05301097{
1098 kref_get(&opp->kref);
1099}
1100
Viresh Kumar70347642017-01-23 10:11:46 +05301101void dev_pm_opp_put(struct dev_pm_opp *opp)
1102{
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301103 kref_put_mutex(&opp->kref, _opp_kref_release_locked,
1104 &opp->opp_table->lock);
Viresh Kumar70347642017-01-23 10:11:46 +05301105}
1106EXPORT_SYMBOL_GPL(dev_pm_opp_put);
1107
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301108static void dev_pm_opp_put_unlocked(struct dev_pm_opp *opp)
1109{
1110 kref_put(&opp->kref, _opp_kref_release_unlocked);
1111}
1112
Viresh Kumar129eec52014-11-27 08:54:06 +05301113/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301114 * dev_pm_opp_remove() - Remove an OPP from OPP table
Viresh Kumar129eec52014-11-27 08:54:06 +05301115 * @dev: device for which we do this operation
1116 * @freq: OPP to remove with matching 'freq'
1117 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301118 * This function removes an opp from the opp table.
Viresh Kumar129eec52014-11-27 08:54:06 +05301119 */
1120void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1121{
1122 struct dev_pm_opp *opp;
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301123 struct opp_table *opp_table;
Viresh Kumar129eec52014-11-27 08:54:06 +05301124 bool found = false;
1125
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301126 opp_table = _find_opp_table(dev);
1127 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +05301128 return;
Viresh Kumar129eec52014-11-27 08:54:06 +05301129
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301130 mutex_lock(&opp_table->lock);
1131
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301132 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumar129eec52014-11-27 08:54:06 +05301133 if (opp->rate == freq) {
1134 found = true;
1135 break;
1136 }
1137 }
1138
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301139 mutex_unlock(&opp_table->lock);
1140
Viresh Kumar5b650b32017-01-23 10:11:48 +05301141 if (found) {
1142 dev_pm_opp_put(opp);
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301143
1144 /* Drop the reference taken by dev_pm_opp_add() */
1145 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301146 } else {
Viresh Kumar129eec52014-11-27 08:54:06 +05301147 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1148 __func__, freq);
Viresh Kumar129eec52014-11-27 08:54:06 +05301149 }
1150
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301151 /* Drop the reference taken by _find_opp_table() */
Viresh Kumar5b650b32017-01-23 10:11:48 +05301152 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar129eec52014-11-27 08:54:06 +05301153}
1154EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1155
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301156/**
1157 * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
1158 * @dev: device for which we do this operation
1159 *
1160 * This function removes all dynamically created OPPs from the opp table.
1161 */
1162void dev_pm_opp_remove_all_dynamic(struct device *dev)
1163{
1164 struct opp_table *opp_table;
1165 struct dev_pm_opp *opp, *temp;
1166 int count = 0;
1167
1168 opp_table = _find_opp_table(dev);
1169 if (IS_ERR(opp_table))
1170 return;
1171
1172 mutex_lock(&opp_table->lock);
1173 list_for_each_entry_safe(opp, temp, &opp_table->opp_list, node) {
1174 if (opp->dynamic) {
1175 dev_pm_opp_put_unlocked(opp);
1176 count++;
1177 }
1178 }
1179 mutex_unlock(&opp_table->lock);
1180
1181 /* Drop the references taken by dev_pm_opp_add() */
1182 while (count--)
1183 dev_pm_opp_put_opp_table(opp_table);
1184
1185 /* Drop the reference taken by _find_opp_table() */
1186 dev_pm_opp_put_opp_table(opp_table);
1187}
1188EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
1189
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301190struct dev_pm_opp *_opp_allocate(struct opp_table *table)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301191{
1192 struct dev_pm_opp *opp;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301193 int count, supply_size;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301194
Viresh Kumardfbe4672016-12-01 16:28:19 +05301195 /* Allocate space for at least one supply */
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301196 count = table->regulator_count > 0 ? table->regulator_count : 1;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301197 supply_size = sizeof(*opp->supplies) * count;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301198
Viresh Kumardfbe4672016-12-01 16:28:19 +05301199 /* allocate new OPP node and supplies structures */
1200 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301201 if (!opp)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301202 return NULL;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301203
Viresh Kumardfbe4672016-12-01 16:28:19 +05301204 /* Put the supplies at the end of the OPP structure as an empty array */
1205 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1206 INIT_LIST_HEAD(&opp->node);
1207
Viresh Kumar23dacf62015-07-29 16:23:01 +05301208 return opp;
1209}
1210
Viresh Kumar7d34d562016-02-09 10:30:34 +05301211static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301212 struct opp_table *opp_table)
Viresh Kumar7d34d562016-02-09 10:30:34 +05301213{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301214 struct regulator *reg;
1215 int i;
Viresh Kumar7d34d562016-02-09 10:30:34 +05301216
Viresh Kumar90e35772018-12-11 16:32:47 +05301217 if (!opp_table->regulators)
1218 return true;
1219
Viresh Kumardfbe4672016-12-01 16:28:19 +05301220 for (i = 0; i < opp_table->regulator_count; i++) {
1221 reg = opp_table->regulators[i];
1222
1223 if (!regulator_is_supported_voltage(reg,
1224 opp->supplies[i].u_volt_min,
1225 opp->supplies[i].u_volt_max)) {
1226 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1227 __func__, opp->supplies[i].u_volt_min,
1228 opp->supplies[i].u_volt_max);
1229 return false;
1230 }
Viresh Kumar7d34d562016-02-09 10:30:34 +05301231 }
1232
1233 return true;
1234}
1235
Viresh Kumara1e8c132018-04-06 14:35:45 +05301236static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1237 struct opp_table *opp_table,
1238 struct list_head **head)
1239{
1240 struct dev_pm_opp *opp;
1241
1242 /*
1243 * Insert new OPP in order of increasing frequency and discard if
1244 * already present.
1245 *
1246 * Need to use &opp_table->opp_list in the condition part of the 'for'
1247 * loop, don't replace it with head otherwise it will become an infinite
1248 * loop.
1249 */
1250 list_for_each_entry(opp, &opp_table->opp_list, node) {
1251 if (new_opp->rate > opp->rate) {
1252 *head = &opp->node;
1253 continue;
1254 }
1255
1256 if (new_opp->rate < opp->rate)
1257 return 0;
1258
1259 /* Duplicate OPPs */
1260 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1261 __func__, opp->rate, opp->supplies[0].u_volt,
1262 opp->available, new_opp->rate,
1263 new_opp->supplies[0].u_volt, new_opp->available);
1264
1265 /* Should we compare voltages for all regulators here ? */
1266 return opp->available &&
1267 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1268 }
1269
1270 return 0;
1271}
1272
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301273/*
1274 * Returns:
1275 * 0: On success. And appropriate error message for duplicate OPPs.
1276 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1277 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1278 * sure we don't print error messages unnecessarily if different parts of
1279 * kernel try to initialize the OPP table.
1280 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1281 * should be considered an error by the callers of _opp_add().
1282 */
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301283int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
Viresh Kumara1e8c132018-04-06 14:35:45 +05301284 struct opp_table *opp_table, bool rate_not_available)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301285{
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301286 struct list_head *head;
Viresh Kumardeaa5142015-11-11 07:59:01 +05301287 int ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301288
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301289 mutex_lock(&opp_table->lock);
1290 head = &opp_table->opp_list;
1291
Viresh Kumara1e8c132018-04-06 14:35:45 +05301292 if (likely(!rate_not_available)) {
1293 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1294 if (ret) {
1295 mutex_unlock(&opp_table->lock);
1296 return ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301297 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301298 }
1299
Viresh Kumar052c6f12017-01-23 10:11:49 +05301300 list_add(&new_opp->node, head);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301301 mutex_unlock(&opp_table->lock);
1302
1303 new_opp->opp_table = opp_table;
Viresh Kumar70347642017-01-23 10:11:46 +05301304 kref_init(&new_opp->kref);
Viresh Kumar23dacf62015-07-29 16:23:01 +05301305
Greg Kroah-Hartmana2dea4c2019-01-22 16:21:17 +01001306 opp_debug_create_one(new_opp, opp_table);
Viresh Kumardeaa5142015-11-11 07:59:01 +05301307
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301308 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
Viresh Kumar7d34d562016-02-09 10:30:34 +05301309 new_opp->available = false;
1310 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1311 __func__, new_opp->rate);
1312 }
1313
Viresh Kumar23dacf62015-07-29 16:23:01 +05301314 return 0;
1315}
1316
Viresh Kumar737002b2015-07-29 16:22:58 +05301317/**
Viresh Kumarb64b9c3f2015-10-15 21:42:44 +05301318 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301319 * @opp_table: OPP table
Nishanth Menone1f60b22010-10-13 00:13:10 +02001320 * @dev: device for which we do this operation
1321 * @freq: Frequency in Hz for this OPP
1322 * @u_volt: Voltage in uVolts for this OPP
1323 * @dynamic: Dynamically added OPPs.
1324 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301325 * This function adds an opp definition to the opp table and returns status.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001326 * The opp is made available by default and it can be controlled using
1327 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1328 *
Viresh Kumar8f8d37b2015-09-04 13:47:24 +05301329 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1330 * and freed by dev_pm_opp_of_remove_table.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001331 *
Nishanth Menone1f60b22010-10-13 00:13:10 +02001332 * Return:
1333 * 0 On success OR
1334 * Duplicate OPPs (both freq and volt are same) and opp->available
1335 * -EEXIST Freq are same and volt are different OR
1336 * Duplicate OPPs (both freq and volt are same) and !opp->available
1337 * -ENOMEM Memory allocation failure
1338 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301339int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1340 unsigned long freq, long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001341{
Viresh Kumar23dacf62015-07-29 16:23:01 +05301342 struct dev_pm_opp *new_opp;
Viresh Kumar50f8cfb2016-02-09 10:30:37 +05301343 unsigned long tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001344 int ret;
1345
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301346 new_opp = _opp_allocate(opp_table);
1347 if (!new_opp)
1348 return -ENOMEM;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301349
Nishanth Menone1f60b22010-10-13 00:13:10 +02001350 /* populate the opp table */
1351 new_opp->rate = freq;
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301352 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301353 new_opp->supplies[0].u_volt = u_volt;
1354 new_opp->supplies[0].u_volt_min = u_volt - tol;
1355 new_opp->supplies[0].u_volt_max = u_volt + tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001356 new_opp->available = true;
Viresh Kumaraa5f2f82015-07-29 16:23:00 +05301357 new_opp->dynamic = dynamic;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301358
Viresh Kumara1e8c132018-04-06 14:35:45 +05301359 ret = _opp_add(dev, new_opp, opp_table, false);
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301360 if (ret) {
1361 /* Don't return error for duplicate OPPs */
1362 if (ret == -EBUSY)
1363 ret = 0;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301364 goto free_opp;
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301365 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301366
Nishanth Menone1f60b22010-10-13 00:13:10 +02001367 /*
1368 * Notify the changes in the availability of the operable
1369 * frequency/voltage list.
1370 */
Viresh Kumar052c6f12017-01-23 10:11:49 +05301371 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001372 return 0;
1373
1374free_opp:
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301375 _opp_free(new_opp);
1376
Nishanth Menone1f60b22010-10-13 00:13:10 +02001377 return ret;
1378}
Viresh Kumar383934092014-11-25 16:04:18 +05301379
Viresh Kumar27465902015-07-29 16:23:02 +05301380/**
Viresh Kumar7de36b02015-12-09 08:01:46 +05301381 * dev_pm_opp_set_supported_hw() - Set supported platforms
1382 * @dev: Device for which supported-hw has to be set.
1383 * @versions: Array of hierarchy of versions to match.
1384 * @count: Number of elements in the array.
1385 *
1386 * This is required only for the V2 bindings, and it enables a platform to
1387 * specify the hierarchy of versions it supports. OPP layer will then enable
1388 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1389 * property.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301390 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301391struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1392 const u32 *versions, unsigned int count)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301393{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301394 struct opp_table *opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301395
Viresh Kumarfa301842017-01-23 10:11:43 +05301396 opp_table = dev_pm_opp_get_opp_table(dev);
1397 if (!opp_table)
1398 return ERR_PTR(-ENOMEM);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301399
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301400 /* Make sure there are no concurrent readers while updating opp_table */
1401 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301402
Viresh Kumar25419de2018-05-22 16:38:08 +05301403 /* Another CPU that shares the OPP table has set the property ? */
1404 if (opp_table->supported_hw)
1405 return opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301406
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301407 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
Viresh Kumar7de36b02015-12-09 08:01:46 +05301408 GFP_KERNEL);
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301409 if (!opp_table->supported_hw) {
Viresh Kumar25419de2018-05-22 16:38:08 +05301410 dev_pm_opp_put_opp_table(opp_table);
1411 return ERR_PTR(-ENOMEM);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301412 }
1413
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301414 opp_table->supported_hw_count = count;
Viresh Kumarfa301842017-01-23 10:11:43 +05301415
1416 return opp_table;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301417}
1418EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1419
1420/**
1421 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
Viresh Kumarfa301842017-01-23 10:11:43 +05301422 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
Viresh Kumar7de36b02015-12-09 08:01:46 +05301423 *
1424 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301425 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
Viresh Kumar7de36b02015-12-09 08:01:46 +05301426 * will not be freed.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301427 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301428void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301429{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301430 /* Make sure there are no concurrent readers while updating opp_table */
1431 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar7de36b02015-12-09 08:01:46 +05301432
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301433 kfree(opp_table->supported_hw);
1434 opp_table->supported_hw = NULL;
1435 opp_table->supported_hw_count = 0;
Viresh Kumar7de36b02015-12-09 08:01:46 +05301436
Viresh Kumarfa301842017-01-23 10:11:43 +05301437 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar7de36b02015-12-09 08:01:46 +05301438}
1439EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1440
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301441/**
1442 * dev_pm_opp_set_prop_name() - Set prop-extn name
Viresh Kumara5da6442016-02-16 14:17:52 +05301443 * @dev: Device for which the prop-name has to be set.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301444 * @name: name to postfix to properties.
1445 *
1446 * This is required only for the V2 bindings, and it enables a platform to
1447 * specify the extn to be used for certain property names. The properties to
1448 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1449 * should postfix the property name with -<name> while looking for them.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301450 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301451struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301452{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301453 struct opp_table *opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301454
Viresh Kumarfa301842017-01-23 10:11:43 +05301455 opp_table = dev_pm_opp_get_opp_table(dev);
1456 if (!opp_table)
1457 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301458
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301459 /* Make sure there are no concurrent readers while updating opp_table */
1460 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301461
Viresh Kumar878ec1a2018-05-22 16:38:08 +05301462 /* Another CPU that shares the OPP table has set the property ? */
1463 if (opp_table->prop_name)
1464 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301465
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301466 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1467 if (!opp_table->prop_name) {
Viresh Kumar878ec1a2018-05-22 16:38:08 +05301468 dev_pm_opp_put_opp_table(opp_table);
1469 return ERR_PTR(-ENOMEM);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301470 }
1471
Viresh Kumarfa301842017-01-23 10:11:43 +05301472 return opp_table;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301473}
1474EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1475
1476/**
1477 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
Viresh Kumarfa301842017-01-23 10:11:43 +05301478 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301479 *
1480 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301481 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301482 * will not be freed.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301483 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301484void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301485{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301486 /* Make sure there are no concurrent readers while updating opp_table */
1487 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301488
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301489 kfree(opp_table->prop_name);
1490 opp_table->prop_name = NULL;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301491
Viresh Kumarfa301842017-01-23 10:11:43 +05301492 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar01fb4d32015-12-09 08:01:47 +05301493}
1494EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1495
Viresh Kumar94735582016-12-01 16:28:20 +05301496static int _allocate_set_opp_data(struct opp_table *opp_table)
1497{
1498 struct dev_pm_set_opp_data *data;
1499 int len, count = opp_table->regulator_count;
1500
Viresh Kumar90e35772018-12-11 16:32:47 +05301501 if (WARN_ON(!opp_table->regulators))
Viresh Kumar94735582016-12-01 16:28:20 +05301502 return -EINVAL;
1503
1504 /* space for set_opp_data */
1505 len = sizeof(*data);
1506
1507 /* space for old_opp.supplies and new_opp.supplies */
1508 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1509
1510 data = kzalloc(len, GFP_KERNEL);
1511 if (!data)
1512 return -ENOMEM;
1513
1514 data->old_opp.supplies = (void *)(data + 1);
1515 data->new_opp.supplies = data->old_opp.supplies + count;
1516
1517 opp_table->set_opp_data = data;
1518
1519 return 0;
1520}
1521
1522static void _free_set_opp_data(struct opp_table *opp_table)
1523{
1524 kfree(opp_table->set_opp_data);
1525 opp_table->set_opp_data = NULL;
1526}
1527
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301528/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301529 * dev_pm_opp_set_regulators() - Set regulator names for the device
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301530 * @dev: Device for which regulator name is being set.
Viresh Kumardfbe4672016-12-01 16:28:19 +05301531 * @names: Array of pointers to the names of the regulator.
1532 * @count: Number of regulators.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301533 *
1534 * In order to support OPP switching, OPP layer needs to know the name of the
Viresh Kumardfbe4672016-12-01 16:28:19 +05301535 * device's regulators, as the core would be required to switch voltages as
1536 * well.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301537 *
1538 * This must be called before any OPPs are initialized for the device.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301539 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301540struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1541 const char * const names[],
1542 unsigned int count)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301543{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301544 struct opp_table *opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301545 struct regulator *reg;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301546 int ret, i;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301547
Viresh Kumarfa301842017-01-23 10:11:43 +05301548 opp_table = dev_pm_opp_get_opp_table(dev);
1549 if (!opp_table)
1550 return ERR_PTR(-ENOMEM);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301551
1552 /* This should be called before OPPs are initialized */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301553 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301554 ret = -EBUSY;
1555 goto err;
1556 }
1557
Viresh Kumar779b7832018-05-22 16:38:08 +05301558 /* Another CPU that shares the OPP table has set the regulators ? */
1559 if (opp_table->regulators)
1560 return opp_table;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301561
1562 opp_table->regulators = kmalloc_array(count,
1563 sizeof(*opp_table->regulators),
1564 GFP_KERNEL);
1565 if (!opp_table->regulators) {
1566 ret = -ENOMEM;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301567 goto err;
1568 }
1569
Viresh Kumardfbe4672016-12-01 16:28:19 +05301570 for (i = 0; i < count; i++) {
1571 reg = regulator_get_optional(dev, names[i]);
1572 if (IS_ERR(reg)) {
1573 ret = PTR_ERR(reg);
1574 if (ret != -EPROBE_DEFER)
1575 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1576 __func__, names[i], ret);
1577 goto free_regulators;
1578 }
1579
1580 opp_table->regulators[i] = reg;
1581 }
1582
1583 opp_table->regulator_count = count;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301584
Viresh Kumar94735582016-12-01 16:28:20 +05301585 /* Allocate block only once to pass to set_opp() routines */
1586 ret = _allocate_set_opp_data(opp_table);
1587 if (ret)
1588 goto free_regulators;
1589
Stephen Boyd91291d92016-11-30 16:21:25 +05301590 return opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301591
Viresh Kumardfbe4672016-12-01 16:28:19 +05301592free_regulators:
1593 while (i != 0)
1594 regulator_put(opp_table->regulators[--i]);
1595
1596 kfree(opp_table->regulators);
1597 opp_table->regulators = NULL;
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301598 opp_table->regulator_count = -1;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301599err:
Viresh Kumarfa301842017-01-23 10:11:43 +05301600 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301601
Stephen Boyd91291d92016-11-30 16:21:25 +05301602 return ERR_PTR(ret);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301603}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301604EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301605
1606/**
Viresh Kumardfbe4672016-12-01 16:28:19 +05301607 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1608 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301609 */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301610void dev_pm_opp_put_regulators(struct opp_table *opp_table)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301611{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301612 int i;
1613
Viresh Kumar779b7832018-05-22 16:38:08 +05301614 if (!opp_table->regulators)
1615 goto put_opp_table;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301616
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301617 /* Make sure there are no concurrent readers while updating opp_table */
1618 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301619
Viresh Kumardfbe4672016-12-01 16:28:19 +05301620 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1621 regulator_put(opp_table->regulators[i]);
1622
Viresh Kumar94735582016-12-01 16:28:20 +05301623 _free_set_opp_data(opp_table);
1624
Viresh Kumardfbe4672016-12-01 16:28:19 +05301625 kfree(opp_table->regulators);
1626 opp_table->regulators = NULL;
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301627 opp_table->regulator_count = -1;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301628
Viresh Kumar779b7832018-05-22 16:38:08 +05301629put_opp_table:
Viresh Kumarfa301842017-01-23 10:11:43 +05301630 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301631}
Viresh Kumardfbe4672016-12-01 16:28:19 +05301632EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
Viresh Kumar9f8ea962016-02-09 10:30:33 +05301633
Viresh Kumar383934092014-11-25 16:04:18 +05301634/**
Viresh Kumar829a4e82017-06-21 10:29:13 +05301635 * dev_pm_opp_set_clkname() - Set clk name for the device
1636 * @dev: Device for which clk name is being set.
1637 * @name: Clk name.
1638 *
1639 * In order to support OPP switching, OPP layer needs to get pointer to the
1640 * clock for the device. Simple cases work fine without using this routine (i.e.
1641 * by passing connection-id as NULL), but for a device with multiple clocks
1642 * available, the OPP core needs to know the exact name of the clk to use.
1643 *
1644 * This must be called before any OPPs are initialized for the device.
1645 */
1646struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
1647{
1648 struct opp_table *opp_table;
1649 int ret;
1650
1651 opp_table = dev_pm_opp_get_opp_table(dev);
1652 if (!opp_table)
1653 return ERR_PTR(-ENOMEM);
1654
1655 /* This should be called before OPPs are initialized */
1656 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1657 ret = -EBUSY;
1658 goto err;
1659 }
1660
1661 /* Already have default clk set, free it */
1662 if (!IS_ERR(opp_table->clk))
1663 clk_put(opp_table->clk);
1664
1665 /* Find clk for the device */
1666 opp_table->clk = clk_get(dev, name);
1667 if (IS_ERR(opp_table->clk)) {
1668 ret = PTR_ERR(opp_table->clk);
1669 if (ret != -EPROBE_DEFER) {
1670 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
1671 ret);
1672 }
1673 goto err;
1674 }
1675
1676 return opp_table;
1677
1678err:
1679 dev_pm_opp_put_opp_table(opp_table);
1680
1681 return ERR_PTR(ret);
1682}
1683EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
1684
1685/**
1686 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1687 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1688 */
1689void dev_pm_opp_put_clkname(struct opp_table *opp_table)
1690{
1691 /* Make sure there are no concurrent readers while updating opp_table */
1692 WARN_ON(!list_empty(&opp_table->opp_list));
1693
1694 clk_put(opp_table->clk);
1695 opp_table->clk = ERR_PTR(-EINVAL);
1696
1697 dev_pm_opp_put_opp_table(opp_table);
1698}
1699EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
1700
1701/**
Viresh Kumar4dab1602016-12-01 16:28:21 +05301702 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1703 * @dev: Device for which the helper is getting registered.
1704 * @set_opp: Custom set OPP helper.
1705 *
1706 * This is useful to support complex platforms (like platforms with multiple
1707 * regulators per device), instead of the generic OPP set rate helper.
1708 *
1709 * This must be called before any OPPs are initialized for the device.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301710 */
Viresh Kumarfa301842017-01-23 10:11:43 +05301711struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
Viresh Kumar4dab1602016-12-01 16:28:21 +05301712 int (*set_opp)(struct dev_pm_set_opp_data *data))
1713{
1714 struct opp_table *opp_table;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301715
1716 if (!set_opp)
Viresh Kumarfa301842017-01-23 10:11:43 +05301717 return ERR_PTR(-EINVAL);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301718
Viresh Kumarfa301842017-01-23 10:11:43 +05301719 opp_table = dev_pm_opp_get_opp_table(dev);
1720 if (!opp_table)
1721 return ERR_PTR(-ENOMEM);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301722
1723 /* This should be called before OPPs are initialized */
1724 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
Viresh Kumar5019acc2018-05-22 16:38:08 +05301725 dev_pm_opp_put_opp_table(opp_table);
1726 return ERR_PTR(-EBUSY);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301727 }
1728
Viresh Kumar5019acc2018-05-22 16:38:08 +05301729 /* Another CPU that shares the OPP table has set the helper ? */
1730 if (!opp_table->set_opp)
1731 opp_table->set_opp = set_opp;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301732
Viresh Kumarfa301842017-01-23 10:11:43 +05301733 return opp_table;
Viresh Kumar4dab1602016-12-01 16:28:21 +05301734}
1735EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1736
1737/**
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301738 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
Viresh Kumar4dab1602016-12-01 16:28:21 +05301739 * set_opp helper
Viresh Kumarfa301842017-01-23 10:11:43 +05301740 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
Viresh Kumar4dab1602016-12-01 16:28:21 +05301741 *
Viresh Kumarfa301842017-01-23 10:11:43 +05301742 * Release resources blocked for platform specific set_opp helper.
Viresh Kumar4dab1602016-12-01 16:28:21 +05301743 */
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301744void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
Viresh Kumar4dab1602016-12-01 16:28:21 +05301745{
Viresh Kumar4dab1602016-12-01 16:28:21 +05301746 /* Make sure there are no concurrent readers while updating opp_table */
1747 WARN_ON(!list_empty(&opp_table->opp_list));
1748
1749 opp_table->set_opp = NULL;
Viresh Kumarfa301842017-01-23 10:11:43 +05301750 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301751}
Viresh Kumar604a7ae2017-10-05 17:26:21 +05301752EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
Viresh Kumar4dab1602016-12-01 16:28:21 +05301753
Viresh Kumar6319aee2019-05-08 15:19:13 +05301754static void _opp_detach_genpd(struct opp_table *opp_table)
1755{
1756 int index;
1757
1758 for (index = 0; index < opp_table->required_opp_count; index++) {
1759 if (!opp_table->genpd_virt_devs[index])
1760 continue;
1761
1762 dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
1763 opp_table->genpd_virt_devs[index] = NULL;
1764 }
Viresh Kumarc0ab9e02019-05-08 15:43:36 +05301765
1766 kfree(opp_table->genpd_virt_devs);
1767 opp_table->genpd_virt_devs = NULL;
Viresh Kumar6319aee2019-05-08 15:19:13 +05301768}
1769
Viresh Kumar4dab1602016-12-01 16:28:21 +05301770/**
Viresh Kumar6319aee2019-05-08 15:19:13 +05301771 * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
1772 * @dev: Consumer device for which the genpd is getting attached.
1773 * @names: Null terminated array of pointers containing names of genpd to attach.
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301774 *
1775 * Multiple generic power domains for a device are supported with the help of
1776 * virtual genpd devices, which are created for each consumer device - genpd
1777 * pair. These are the device structures which are attached to the power domain
1778 * and are required by the OPP core to set the performance state of the genpd.
Viresh Kumar6319aee2019-05-08 15:19:13 +05301779 * The same API also works for the case where single genpd is available and so
1780 * we don't need to support that separately.
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301781 *
1782 * This helper will normally be called by the consumer driver of the device
Viresh Kumar6319aee2019-05-08 15:19:13 +05301783 * "dev", as only that has details of the genpd names.
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301784 *
Viresh Kumar6319aee2019-05-08 15:19:13 +05301785 * This helper needs to be called once with a list of all genpd to attach.
1786 * Otherwise the original device structure will be used instead by the OPP core.
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301787 */
Viresh Kumar6319aee2019-05-08 15:19:13 +05301788struct opp_table *dev_pm_opp_attach_genpd(struct device *dev, const char **names)
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301789{
1790 struct opp_table *opp_table;
Viresh Kumar6319aee2019-05-08 15:19:13 +05301791 struct device *virt_dev;
1792 int index, ret = -EINVAL;
1793 const char **name = names;
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301794
1795 opp_table = dev_pm_opp_get_opp_table(dev);
1796 if (!opp_table)
1797 return ERR_PTR(-ENOMEM);
1798
Viresh Kumar6319aee2019-05-08 15:19:13 +05301799 /*
1800 * If the genpd's OPP table isn't already initialized, parsing of the
1801 * required-opps fail for dev. We should retry this after genpd's OPP
1802 * table is added.
1803 */
1804 if (!opp_table->required_opp_count) {
1805 ret = -EPROBE_DEFER;
1806 goto put_table;
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301807 }
1808
Viresh Kumar6319aee2019-05-08 15:19:13 +05301809 mutex_lock(&opp_table->genpd_virt_dev_lock);
1810
Viresh Kumarc0ab9e02019-05-08 15:43:36 +05301811 opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
1812 sizeof(*opp_table->genpd_virt_devs),
1813 GFP_KERNEL);
1814 if (!opp_table->genpd_virt_devs)
1815 goto unlock;
1816
Viresh Kumar6319aee2019-05-08 15:19:13 +05301817 while (*name) {
1818 index = of_property_match_string(dev->of_node,
1819 "power-domain-names", *name);
1820 if (index < 0) {
1821 dev_err(dev, "Failed to find power domain: %s (%d)\n",
1822 *name, index);
1823 goto err;
1824 }
1825
1826 if (index >= opp_table->required_opp_count) {
1827 dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
1828 *name, opp_table->required_opp_count, index);
1829 goto err;
1830 }
1831
1832 if (opp_table->genpd_virt_devs[index]) {
1833 dev_err(dev, "Genpd virtual device already set %s\n",
1834 *name);
1835 goto err;
1836 }
1837
1838 virt_dev = dev_pm_domain_attach_by_name(dev, *name);
1839 if (IS_ERR(virt_dev)) {
1840 ret = PTR_ERR(virt_dev);
1841 dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
1842 goto err;
1843 }
1844
1845 opp_table->genpd_virt_devs[index] = virt_dev;
1846 name++;
1847 }
1848
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301849 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1850
1851 return opp_table;
Viresh Kumar6319aee2019-05-08 15:19:13 +05301852
1853err:
1854 _opp_detach_genpd(opp_table);
Viresh Kumarc0ab9e02019-05-08 15:43:36 +05301855unlock:
Viresh Kumar6319aee2019-05-08 15:19:13 +05301856 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1857
1858put_table:
1859 dev_pm_opp_put_opp_table(opp_table);
1860
1861 return ERR_PTR(ret);
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301862}
Viresh Kumar6319aee2019-05-08 15:19:13 +05301863EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd);
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301864
1865/**
Viresh Kumar6319aee2019-05-08 15:19:13 +05301866 * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device.
1867 * @opp_table: OPP table returned by dev_pm_opp_attach_genpd().
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301868 *
Viresh Kumar6319aee2019-05-08 15:19:13 +05301869 * This detaches the genpd(s), resets the virtual device pointers, and puts the
1870 * OPP table.
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301871 */
Viresh Kumar6319aee2019-05-08 15:19:13 +05301872void dev_pm_opp_detach_genpd(struct opp_table *opp_table)
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301873{
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301874 /*
1875 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
1876 * used in parallel.
1877 */
1878 mutex_lock(&opp_table->genpd_virt_dev_lock);
Viresh Kumar6319aee2019-05-08 15:19:13 +05301879 _opp_detach_genpd(opp_table);
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301880 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1881
Viresh Kumar6319aee2019-05-08 15:19:13 +05301882 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301883}
Viresh Kumar6319aee2019-05-08 15:19:13 +05301884EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd);
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301885
1886/**
Viresh Kumarc8a59102018-11-02 14:36:42 +05301887 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
1888 * @src_table: OPP table which has dst_table as one of its required OPP table.
1889 * @dst_table: Required OPP table of the src_table.
1890 * @pstate: Current performance state of the src_table.
1891 *
1892 * This Returns pstate of the OPP (present in @dst_table) pointed out by the
1893 * "required-opps" property of the OPP (present in @src_table) which has
1894 * performance state set to @pstate.
1895 *
1896 * Return: Zero or positive performance state on success, otherwise negative
1897 * value on errors.
1898 */
1899int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
1900 struct opp_table *dst_table,
1901 unsigned int pstate)
1902{
1903 struct dev_pm_opp *opp;
1904 int dest_pstate = -EINVAL;
1905 int i;
1906
1907 if (!pstate)
1908 return 0;
1909
1910 /*
1911 * Normally the src_table will have the "required_opps" property set to
1912 * point to one of the OPPs in the dst_table, but in some cases the
1913 * genpd and its master have one to one mapping of performance states
1914 * and so none of them have the "required-opps" property set. Return the
1915 * pstate of the src_table as it is in such cases.
1916 */
1917 if (!src_table->required_opp_count)
1918 return pstate;
1919
1920 for (i = 0; i < src_table->required_opp_count; i++) {
1921 if (src_table->required_opp_tables[i]->np == dst_table->np)
1922 break;
1923 }
1924
1925 if (unlikely(i == src_table->required_opp_count)) {
1926 pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
1927 __func__, src_table, dst_table);
1928 return -EINVAL;
1929 }
1930
1931 mutex_lock(&src_table->lock);
1932
1933 list_for_each_entry(opp, &src_table->opp_list, node) {
1934 if (opp->pstate == pstate) {
1935 dest_pstate = opp->required_opps[i]->pstate;
1936 goto unlock;
1937 }
1938 }
1939
1940 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
1941 dst_table);
1942
1943unlock:
1944 mutex_unlock(&src_table->lock);
1945
1946 return dest_pstate;
1947}
1948
1949/**
Nishanth Menone1f60b22010-10-13 00:13:10 +02001950 * dev_pm_opp_add() - Add an OPP table from a table definitions
1951 * @dev: device for which we do this operation
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001952 * @freq: Frequency in Hz for this OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02001953 * @u_volt: Voltage in uVolts for this OPP
1954 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301955 * This function adds an opp definition to the opp table and returns status.
Nishanth Menon47d43ba2013-09-19 16:03:51 -05001956 * The opp is made available by default and it can be controlled using
Nishanth Menone1f60b22010-10-13 00:13:10 +02001957 * dev_pm_opp_enable/disable functions.
1958 *
Viresh Kumara7470db2014-11-25 16:04:17 +05301959 * Return:
1960 * 0 On success OR
1961 * Duplicate OPPs (both freq and volt are same) and opp->available
1962 * -EEXIST Freq are same and volt are different OR
1963 * Duplicate OPPs (both freq and volt are same) and !opp->available
Viresh Kumar383934092014-11-25 16:04:18 +05301964 * -ENOMEM Memory allocation failure
Viresh Kumara7470db2014-11-25 16:04:17 +05301965 */
Nishanth Menone1f60b22010-10-13 00:13:10 +02001966int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1967{
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301968 struct opp_table *opp_table;
1969 int ret;
1970
Viresh Kumarb83c1892017-01-23 10:11:45 +05301971 opp_table = dev_pm_opp_get_opp_table(dev);
1972 if (!opp_table)
1973 return -ENOMEM;
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301974
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301975 /* Fix regulator count for dynamic OPPs */
1976 opp_table->regulator_count = 1;
1977
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301978 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301979 if (ret)
1980 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301981
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301982 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001983}
1984EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1985
Nishanth Menone1f60b22010-10-13 00:13:10 +02001986/**
Nishanth Menon327854c2014-12-24 11:22:56 -06001987 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +02001988 * @dev: device for which we do this operation
1989 * @freq: OPP frequency to modify availability
1990 * @availability_req: availability status requested for this opp
1991 *
Viresh Kumar052c6f12017-01-23 10:11:49 +05301992 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1993 * which is isolated here.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001994 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06001995 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07001996 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menone1f60b22010-10-13 00:13:10 +02001997 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001998 */
Nishanth Menon327854c2014-12-24 11:22:56 -06001999static int _opp_set_availability(struct device *dev, unsigned long freq,
2000 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +02002001{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302002 struct opp_table *opp_table;
Viresh Kumara7f39872017-01-23 10:11:50 +05302003 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002004 int r = 0;
2005
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302006 /* Find the opp_table */
2007 opp_table = _find_opp_table(dev);
2008 if (IS_ERR(opp_table)) {
2009 r = PTR_ERR(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002010 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
Viresh Kumara7f39872017-01-23 10:11:50 +05302011 return r;
Nishanth Menone1f60b22010-10-13 00:13:10 +02002012 }
2013
Viresh Kumar37a73ec2017-01-23 10:11:41 +05302014 mutex_lock(&opp_table->lock);
2015
Nishanth Menone1f60b22010-10-13 00:13:10 +02002016 /* Do we have the frequency? */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302017 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
Nishanth Menone1f60b22010-10-13 00:13:10 +02002018 if (tmp_opp->rate == freq) {
2019 opp = tmp_opp;
2020 break;
2021 }
2022 }
Viresh Kumar37a73ec2017-01-23 10:11:41 +05302023
Nishanth Menone1f60b22010-10-13 00:13:10 +02002024 if (IS_ERR(opp)) {
2025 r = PTR_ERR(opp);
2026 goto unlock;
2027 }
2028
2029 /* Is update really needed? */
2030 if (opp->available == availability_req)
2031 goto unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +02002032
Viresh Kumara7f39872017-01-23 10:11:50 +05302033 opp->available = availability_req;
Nishanth Menone1f60b22010-10-13 00:13:10 +02002034
Viresh Kumare4d8ae02017-09-21 10:44:36 -07002035 dev_pm_opp_get(opp);
2036 mutex_unlock(&opp_table->lock);
2037
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002038 /* Notify the change of the OPP availability */
2039 if (availability_req)
Viresh Kumar052c6f12017-01-23 10:11:49 +05302040 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
Viresh Kumara7f39872017-01-23 10:11:50 +05302041 opp);
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002042 else
Viresh Kumar052c6f12017-01-23 10:11:49 +05302043 blocking_notifier_call_chain(&opp_table->head,
Viresh Kumara7f39872017-01-23 10:11:50 +05302044 OPP_EVENT_DISABLE, opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002045
Viresh Kumare4d8ae02017-09-21 10:44:36 -07002046 dev_pm_opp_put(opp);
2047 goto put_table;
2048
Nishanth Menone1f60b22010-10-13 00:13:10 +02002049unlock:
Viresh Kumar5b650b32017-01-23 10:11:48 +05302050 mutex_unlock(&opp_table->lock);
Viresh Kumare4d8ae02017-09-21 10:44:36 -07002051put_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +05302052 dev_pm_opp_put_opp_table(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002053 return r;
2054}
2055
2056/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002057 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02002058 * @dev: device for which we do this operation
2059 * @freq: OPP frequency to enable
2060 *
2061 * Enables a provided opp. If the operation is valid, this returns 0, else the
2062 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002063 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +02002064 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06002065 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07002066 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c2014-12-24 11:22:57 -06002067 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02002068 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002069int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02002070{
Nishanth Menon327854c2014-12-24 11:22:56 -06002071 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002072}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002073EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002074
2075/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002076 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02002077 * @dev: device for which we do this operation
2078 * @freq: OPP frequency to disable
2079 *
2080 * Disables a provided opp. If the operation is valid, this returns
2081 * 0, else the corresponding error value. It is meant to be a temporary
2082 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002083 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +02002084 *
Nishanth Menon984f16c2014-12-24 11:22:57 -06002085 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07002086 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c2014-12-24 11:22:57 -06002087 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02002088 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002089int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02002090{
Nishanth Menon327854c2014-12-24 11:22:56 -06002091 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002092}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002093EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002094
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002095/**
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302096 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
2097 * @dev: Device for which notifier needs to be registered
2098 * @nb: Notifier block to be registered
Nishanth Menon984f16c2014-12-24 11:22:57 -06002099 *
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302100 * Return: 0 on success or a negative error value.
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002101 */
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302102int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002103{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302104 struct opp_table *opp_table;
2105 int ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002106
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302107 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05302108 if (IS_ERR(opp_table))
2109 return PTR_ERR(opp_table);
2110
Viresh Kumar052c6f12017-01-23 10:11:49 +05302111 ret = blocking_notifier_chain_register(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302112
Viresh Kumar5b650b32017-01-23 10:11:48 +05302113 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302114
2115 return ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002116}
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302117EXPORT_SYMBOL(dev_pm_opp_register_notifier);
2118
2119/**
2120 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
2121 * @dev: Device for which notifier needs to be unregistered
2122 * @nb: Notifier block to be unregistered
2123 *
2124 * Return: 0 on success or a negative error value.
2125 */
2126int dev_pm_opp_unregister_notifier(struct device *dev,
2127 struct notifier_block *nb)
2128{
2129 struct opp_table *opp_table;
2130 int ret;
2131
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302132 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05302133 if (IS_ERR(opp_table))
2134 return PTR_ERR(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302135
Viresh Kumar052c6f12017-01-23 10:11:49 +05302136 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302137
Viresh Kumar5b650b32017-01-23 10:11:48 +05302138 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302139
2140 return ret;
2141}
2142EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +02002143
Viresh Kumar2a4eb732018-09-13 13:14:36 +05302144void _dev_pm_opp_find_and_remove_table(struct device *dev)
Viresh Kumar737002b2015-07-29 16:22:58 +05302145{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302146 struct opp_table *opp_table;
Viresh Kumar737002b2015-07-29 16:22:58 +05302147
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302148 /* Check for existing table for 'dev' */
2149 opp_table = _find_opp_table(dev);
2150 if (IS_ERR(opp_table)) {
2151 int error = PTR_ERR(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05302152
2153 if (error != -ENODEV)
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302154 WARN(1, "%s: opp_table: %d\n",
Viresh Kumar737002b2015-07-29 16:22:58 +05302155 IS_ERR_OR_NULL(dev) ?
2156 "Invalid device" : dev_name(dev),
2157 error);
Viresh Kumar5b650b32017-01-23 10:11:48 +05302158 return;
Viresh Kumar737002b2015-07-29 16:22:58 +05302159 }
2160
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05302161 _put_opp_list_kref(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05302162
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05302163 /* Drop reference taken by _find_opp_table() */
2164 dev_pm_opp_put_opp_table(opp_table);
2165
2166 /* Drop reference taken while the OPP table was added */
Viresh Kumar5b650b32017-01-23 10:11:48 +05302167 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05302168}
Viresh Kumar129eec52014-11-27 08:54:06 +05302169
2170/**
Sudeep Holla411466c2016-05-03 15:05:04 +01002171 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302172 * @dev: device pointer used to lookup OPP table.
Viresh Kumar129eec52014-11-27 08:54:06 +05302173 *
Sudeep Holla411466c2016-05-03 15:05:04 +01002174 * Free both OPPs created using static entries present in DT and the
2175 * dynamically added entries.
Viresh Kumar129eec52014-11-27 08:54:06 +05302176 */
Sudeep Holla411466c2016-05-03 15:05:04 +01002177void dev_pm_opp_remove_table(struct device *dev)
Viresh Kumar129eec52014-11-27 08:54:06 +05302178{
Viresh Kumar2a4eb732018-09-13 13:14:36 +05302179 _dev_pm_opp_find_and_remove_table(dev);
Viresh Kumar8d4d4e92015-06-12 17:10:38 +05302180}
Sudeep Holla411466c2016-05-03 15:05:04 +01002181EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);