blob: 3f46e499d615f2852622885aa7c40ff3faccb1db [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>
Paul Gortmaker51990e82012-01-22 11:23:42 -050016#include <linux/device.h>
Liam Girdwood80126ce2012-10-23 01:27:44 +020017#include <linux/export.h>
Viresh Kumar009acd12017-10-11 12:54:14 +053018#include <linux/pm_domain.h>
Viresh Kumar9f8ea962016-02-09 10:30:33 +053019#include <linux/regulator/consumer.h>
Viresh Kumar11b9b662022-05-25 15:23:16 +053020#include <linux/slab.h>
21#include <linux/xarray.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020022
Viresh Kumarf59d3ee2015-09-04 13:47:26 +053023#include "opp.h"
Nishanth Menone1f60b22010-10-13 00:13:10 +020024
25/*
Viresh Kumar2c2709d2016-02-16 14:17:53 +053026 * The root of the list of all opp-tables. All opp_table structures branch off
27 * from here, with each opp_table containing the list of opps it supports in
Nishanth Menone1f60b22010-10-13 00:13:10 +020028 * various states of availability.
29 */
Viresh Kumarf47b72a2016-05-05 16:20:33 +053030LIST_HEAD(opp_tables);
Viresh Kumar7eba0c72019-11-25 13:57:58 +053031
Nishanth Menone1f60b22010-10-13 00:13:10 +020032/* Lock to allow exclusive modification to the device and opp lists */
Viresh Kumar2c2709d2016-02-16 14:17:53 +053033DEFINE_MUTEX(opp_table_lock);
Viresh Kumar27c09482020-10-27 16:53:21 +053034/* Flag indicating that opp_tables list is being updated at the moment */
35static bool opp_tables_busy;
Nishanth Menone1f60b22010-10-13 00:13:10 +020036
Viresh Kumar11b9b662022-05-25 15:23:16 +053037/* OPP ID allocator */
38static DEFINE_XARRAY_ALLOC1(opp_configs);
39
Viresh Kumar9e62eda2020-10-27 11:49:03 +053040static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +053041{
Viresh Kumar2c2709d2016-02-16 14:17:53 +053042 struct opp_device *opp_dev;
Viresh Kumar9e62eda2020-10-27 11:49:03 +053043 bool found = false;
Viresh Kumar06441652015-07-29 16:23:04 +053044
Viresh Kumar9e62eda2020-10-27 11:49:03 +053045 mutex_lock(&opp_table->lock);
Viresh Kumar2c2709d2016-02-16 14:17:53 +053046 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
Viresh Kumar9e62eda2020-10-27 11:49:03 +053047 if (opp_dev->dev == dev) {
48 found = true;
49 break;
50 }
Viresh Kumar06441652015-07-29 16:23:04 +053051
Viresh Kumar9e62eda2020-10-27 11:49:03 +053052 mutex_unlock(&opp_table->lock);
53 return found;
Viresh Kumar06441652015-07-29 16:23:04 +053054}
55
Wei Yongjun6ac42392017-02-06 14:29:55 +000056static struct opp_table *_find_opp_table_unlocked(struct device *dev)
Viresh Kumar5b650b32017-01-23 10:11:48 +053057{
58 struct opp_table *opp_table;
59
60 list_for_each_entry(opp_table, &opp_tables, node) {
Viresh Kumar9e62eda2020-10-27 11:49:03 +053061 if (_find_opp_dev(dev, opp_table)) {
Viresh Kumar5b650b32017-01-23 10:11:48 +053062 _get_opp_table_kref(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +053063 return opp_table;
64 }
65 }
66
67 return ERR_PTR(-ENODEV);
68}
69
Nishanth Menone1f60b22010-10-13 00:13:10 +020070/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +053071 * _find_opp_table() - find opp_table struct using device pointer
72 * @dev: device pointer used to lookup OPP table
Nishanth Menone1f60b22010-10-13 00:13:10 +020073 *
Viresh Kumar052c6f12017-01-23 10:11:49 +053074 * Search OPP table for one containing matching device.
Nishanth Menone1f60b22010-10-13 00:13:10 +020075 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +053076 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
Nishanth Menone1f60b22010-10-13 00:13:10 +020077 * -EINVAL based on type of error.
78 *
Viresh Kumar5b650b32017-01-23 10:11:48 +053079 * The callers must call dev_pm_opp_put_opp_table() after the table is used.
Nishanth Menone1f60b22010-10-13 00:13:10 +020080 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +053081struct opp_table *_find_opp_table(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +020082{
Viresh Kumar2c2709d2016-02-16 14:17:53 +053083 struct opp_table *opp_table;
Nishanth Menone1f60b22010-10-13 00:13:10 +020084
Viresh Kumar50a3cb02015-08-12 15:59:39 +053085 if (IS_ERR_OR_NULL(dev)) {
Nishanth Menone1f60b22010-10-13 00:13:10 +020086 pr_err("%s: Invalid parameters\n", __func__);
87 return ERR_PTR(-EINVAL);
88 }
89
Viresh Kumar5b650b32017-01-23 10:11:48 +053090 mutex_lock(&opp_table_lock);
91 opp_table = _find_opp_table_unlocked(dev);
92 mutex_unlock(&opp_table_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +020093
Viresh Kumar5b650b32017-01-23 10:11:48 +053094 return opp_table;
Nishanth Menone1f60b22010-10-13 00:13:10 +020095}
96
Viresh Kumarf123ea72022-06-10 10:27:51 +053097/*
98 * Returns true if multiple clocks aren't there, else returns false with WARN.
99 *
100 * We don't force clk_count == 1 here as there are users who don't have a clock
101 * representation in the OPP table and manage the clock configuration themselves
102 * in an platform specific way.
103 */
104static bool assert_single_clk(struct opp_table *opp_table)
105{
106 return !WARN_ON(opp_table->clk_count > 1);
107}
108
Nishanth Menone1f60b22010-10-13 00:13:10 +0200109/**
Linus Torvaldsbaf51c42015-11-11 09:03:01 -0800110 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200111 * @opp: opp for which voltage has to be returned for
112 *
Nishanth Menon984f16c82014-12-24 11:22:57 -0600113 * Return: voltage in micro volt corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +0200114 * return 0
115 *
Viresh Kumardfbe4672016-12-01 16:28:19 +0530116 * This is useful only for devices with single power supply.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200117 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500118unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200119{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530120 if (IS_ERR_OR_NULL(opp)) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200121 pr_err("%s: Invalid parameters\n", __func__);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530122 return 0;
123 }
Nishanth Menone1f60b22010-10-13 00:13:10 +0200124
Viresh Kumar052c6f12017-01-23 10:11:49 +0530125 return opp->supplies[0].u_volt;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200126}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500127EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200128
129/**
Viresh Kumar69b1af12022-05-31 13:24:21 +0530130 * dev_pm_opp_get_supplies() - Gets the supply information corresponding to an opp
131 * @opp: opp for which voltage has to be returned for
132 * @supplies: Placeholder for copying the supply information.
133 *
134 * Return: negative error number on failure, 0 otherwise on success after
135 * setting @supplies.
136 *
137 * This can be used for devices with any number of power supplies. The caller
138 * must ensure the @supplies array must contain space for each regulator.
139 */
140int dev_pm_opp_get_supplies(struct dev_pm_opp *opp,
141 struct dev_pm_opp_supply *supplies)
142{
143 if (IS_ERR_OR_NULL(opp) || !supplies) {
144 pr_err("%s: Invalid parameters\n", __func__);
145 return -EINVAL;
146 }
147
148 memcpy(supplies, opp->supplies,
149 sizeof(*supplies) * opp->opp_table->regulator_count);
150 return 0;
151}
152EXPORT_SYMBOL_GPL(dev_pm_opp_get_supplies);
153
154/**
Lukasz Luba4f9a7a12022-03-02 11:29:14 +0000155 * dev_pm_opp_get_power() - Gets the power corresponding to an opp
156 * @opp: opp for which power has to be returned for
157 *
158 * Return: power in micro watt corresponding to the opp, else
159 * return 0
160 *
161 * This is useful only for devices with single power supply.
162 */
163unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp)
164{
165 unsigned long opp_power = 0;
166 int i;
167
168 if (IS_ERR_OR_NULL(opp)) {
169 pr_err("%s: Invalid parameters\n", __func__);
170 return 0;
171 }
172 for (i = 0; i < opp->opp_table->regulator_count; i++)
173 opp_power += opp->supplies[i].u_watt;
174
175 return opp_power;
176}
177EXPORT_SYMBOL_GPL(dev_pm_opp_get_power);
178
179/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500180 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200181 * @opp: opp for which frequency has to be returned for
182 *
Nishanth Menon984f16c82014-12-24 11:22:57 -0600183 * Return: frequency in hertz corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +0200184 * return 0
Nishanth Menone1f60b22010-10-13 00:13:10 +0200185 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500186unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200187{
Andrew-sh.Cheng06a8a052020-07-20 16:55:26 +0800188 if (IS_ERR_OR_NULL(opp)) {
Nishanth Menone1f60b22010-10-13 00:13:10 +0200189 pr_err("%s: Invalid parameters\n", __func__);
Viresh Kumar052c6f12017-01-23 10:11:49 +0530190 return 0;
191 }
Nishanth Menone1f60b22010-10-13 00:13:10 +0200192
Viresh Kumarf123ea72022-06-10 10:27:51 +0530193 if (!assert_single_clk(opp->opp_table))
194 return 0;
195
Viresh Kumar2083da22022-06-10 12:02:04 +0530196 return opp->rates[0];
Nishanth Menone1f60b22010-10-13 00:13:10 +0200197}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500198EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200199
200/**
Rajendra Nayak5b93ac52019-01-10 09:32:02 +0530201 * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
202 * @opp: opp for which level value has to be returned for
203 *
204 * Return: level read from device tree corresponding to the opp, else
205 * return 0.
206 */
207unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
208{
209 if (IS_ERR_OR_NULL(opp) || !opp->available) {
210 pr_err("%s: Invalid parameters\n", __func__);
211 return 0;
212 }
213
214 return opp->level;
215}
216EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
217
218/**
Dmitry Osipenko597ff542021-01-18 03:55:19 +0300219 * dev_pm_opp_get_required_pstate() - Gets the required performance state
220 * corresponding to an available opp
221 * @opp: opp for which performance state has to be returned for
222 * @index: index of the required opp
223 *
224 * Return: performance state read from device tree corresponding to the
225 * required opp, else return 0.
226 */
227unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
228 unsigned int index)
229{
Viresh Kumar84cb7ff2023-06-14 15:57:43 +0530230 struct opp_table *opp_table = opp->opp_table;
231
Dmitry Osipenko597ff542021-01-18 03:55:19 +0300232 if (IS_ERR_OR_NULL(opp) || !opp->available ||
Viresh Kumar84cb7ff2023-06-14 15:57:43 +0530233 index >= opp_table->required_opp_count) {
Dmitry Osipenko597ff542021-01-18 03:55:19 +0300234 pr_err("%s: Invalid parameters\n", __func__);
235 return 0;
236 }
237
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530238 /* required-opps not fully initialized yet */
Viresh Kumar84cb7ff2023-06-14 15:57:43 +0530239 if (lazy_linking_pending(opp_table))
Viresh Kumar7eba0c72019-11-25 13:57:58 +0530240 return 0;
241
Viresh Kumar84cb7ff2023-06-14 15:57:43 +0530242 /* The required OPP table must belong to a genpd */
243 if (unlikely(!opp_table->required_opp_tables[index]->is_genpd)) {
244 pr_err("%s: Performance state is only valid for genpds.\n", __func__);
245 return 0;
246 }
247
Viresh Kumar7c41cdc2023-06-14 15:29:32 +0530248 return opp->required_opps[index]->level;
Dmitry Osipenko597ff542021-01-18 03:55:19 +0300249}
250EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate);
251
252/**
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200253 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
254 * @opp: opp for which turbo mode is being verified
255 *
256 * Turbo OPPs are not for normal use, and can be enabled (under certain
257 * conditions) for short duration of times to finish high throughput work
258 * quickly. Running on them for longer times may overheat the chip.
259 *
260 * Return: true if opp is turbo opp, else false.
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200261 */
262bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
263{
Viresh Kumar052c6f12017-01-23 10:11:49 +0530264 if (IS_ERR_OR_NULL(opp) || !opp->available) {
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200265 pr_err("%s: Invalid parameters\n", __func__);
266 return false;
267 }
268
Viresh Kumar052c6f12017-01-23 10:11:49 +0530269 return opp->turbo;
Bartlomiej Zolnierkiewicz19445b22015-07-09 17:43:35 +0200270}
271EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
272
273/**
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530274 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
275 * @dev: device for which we do this operation
276 *
277 * Return: This function returns the max clock latency in nanoseconds.
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530278 */
279unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
280{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530281 struct opp_table *opp_table;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530282 unsigned long clock_latency_ns;
283
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530284 opp_table = _find_opp_table(dev);
285 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +0530286 return 0;
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530287
Viresh Kumar5b650b32017-01-23 10:11:48 +0530288 clock_latency_ns = opp_table->clock_latency_ns_max;
289
290 dev_pm_opp_put_opp_table(opp_table);
291
Viresh Kumar3ca9bb32015-07-29 16:23:03 +0530292 return clock_latency_ns;
293}
294EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
295
296/**
Viresh Kumar655c9df2016-02-09 10:30:35 +0530297 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
298 * @dev: device for which we do this operation
299 *
300 * Return: This function returns the max voltage latency in nanoseconds.
Viresh Kumar655c9df2016-02-09 10:30:35 +0530301 */
302unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
303{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530304 struct opp_table *opp_table;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530305 struct dev_pm_opp *opp;
Viresh Kumar478256b2017-05-23 09:32:11 +0530306 struct regulator *reg;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530307 unsigned long latency_ns = 0;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530308 int ret, i, count;
309 struct {
310 unsigned long min;
311 unsigned long max;
312 } *uV;
313
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530314 opp_table = _find_opp_table(dev);
315 if (IS_ERR(opp_table))
316 return 0;
317
Viresh Kumardfbe4672016-12-01 16:28:19 +0530318 /* Regulator may not be required for the device */
Viresh Kumar90e35772018-12-11 16:32:47 +0530319 if (!opp_table->regulators)
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530320 goto put_opp_table;
Viresh Kumardfbe4672016-12-01 16:28:19 +0530321
Viresh Kumar90e35772018-12-11 16:32:47 +0530322 count = opp_table->regulator_count;
323
Viresh Kumardfbe4672016-12-01 16:28:19 +0530324 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
325 if (!uV)
Viresh Kumar478256b2017-05-23 09:32:11 +0530326 goto put_opp_table;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530327
Viresh Kumar052c6f12017-01-23 10:11:49 +0530328 mutex_lock(&opp_table->lock);
329
Viresh Kumardfbe4672016-12-01 16:28:19 +0530330 for (i = 0; i < count; i++) {
331 uV[i].min = ~0;
332 uV[i].max = 0;
Viresh Kumar655c9df2016-02-09 10:30:35 +0530333
Viresh Kumar052c6f12017-01-23 10:11:49 +0530334 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumardfbe4672016-12-01 16:28:19 +0530335 if (!opp->available)
336 continue;
337
338 if (opp->supplies[i].u_volt_min < uV[i].min)
339 uV[i].min = opp->supplies[i].u_volt_min;
340 if (opp->supplies[i].u_volt_max > uV[i].max)
341 uV[i].max = opp->supplies[i].u_volt_max;
342 }
Viresh Kumar655c9df2016-02-09 10:30:35 +0530343 }
344
Viresh Kumar052c6f12017-01-23 10:11:49 +0530345 mutex_unlock(&opp_table->lock);
Viresh Kumar655c9df2016-02-09 10:30:35 +0530346
347 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530348 * The caller needs to ensure that opp_table (and hence the regulator)
Viresh Kumar655c9df2016-02-09 10:30:35 +0530349 * isn't freed, while we are executing this routine.
350 */
Andrzej Hajda8cc311162017-02-20 19:57:57 +0100351 for (i = 0; i < count; i++) {
Viresh Kumar478256b2017-05-23 09:32:11 +0530352 reg = opp_table->regulators[i];
Viresh Kumardfbe4672016-12-01 16:28:19 +0530353 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
354 if (ret > 0)
355 latency_ns += ret * 1000;
356 }
357
Viresh Kumardfbe4672016-12-01 16:28:19 +0530358 kfree(uV);
Viresh Kumarcdd3e612017-01-23 10:11:51 +0530359put_opp_table:
360 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar655c9df2016-02-09 10:30:35 +0530361
362 return latency_ns;
363}
364EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
365
366/**
Viresh Kumar21743442016-02-09 10:30:36 +0530367 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
368 * nanoseconds
369 * @dev: device for which we do this operation
370 *
371 * Return: This function returns the max transition latency, in nanoseconds, to
372 * switch from one OPP to other.
Viresh Kumar21743442016-02-09 10:30:36 +0530373 */
374unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
375{
376 return dev_pm_opp_get_max_volt_latency(dev) +
377 dev_pm_opp_get_max_clock_latency(dev);
378}
379EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
380
381/**
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530382 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200383 * @dev: device for which we do this operation
384 *
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530385 * Return: This function returns the frequency of the OPP marked as suspend_opp
386 * if one is available, else returns 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200387 */
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530388unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200389{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530390 struct opp_table *opp_table;
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530391 unsigned long freq = 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200392
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530393 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530394 if (IS_ERR(opp_table))
395 return 0;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200396
Viresh Kumar5b650b32017-01-23 10:11:48 +0530397 if (opp_table->suspend_opp && opp_table->suspend_opp->available)
398 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530399
Viresh Kumar5b650b32017-01-23 10:11:48 +0530400 dev_pm_opp_put_opp_table(opp_table);
401
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530402 return freq;
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200403}
Viresh Kumar3aa26a32017-01-02 14:41:02 +0530404EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200405
Viresh Kumara1e8c132018-04-06 14:35:45 +0530406int _get_opp_count(struct opp_table *opp_table)
407{
408 struct dev_pm_opp *opp;
409 int count = 0;
410
411 mutex_lock(&opp_table->lock);
412
413 list_for_each_entry(opp, &opp_table->opp_list, node) {
414 if (opp->available)
415 count++;
416 }
417
418 mutex_unlock(&opp_table->lock);
419
420 return count;
421}
422
Bartlomiej Zolnierkiewicz4eafbd12015-09-08 18:41:01 +0200423/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530424 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
Nishanth Menone1f60b22010-10-13 00:13:10 +0200425 * @dev: device for which we do this operation
426 *
Nishanth Menon984f16c82014-12-24 11:22:57 -0600427 * Return: This function returns the number of available opps if there are any,
Nishanth Menone1f60b22010-10-13 00:13:10 +0200428 * else returns 0 if none or the corresponding error value.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200429 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500430int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200431{
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530432 struct opp_table *opp_table;
Viresh Kumara1e8c132018-04-06 14:35:45 +0530433 int count;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200434
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530435 opp_table = _find_opp_table(dev);
436 if (IS_ERR(opp_table)) {
437 count = PTR_ERR(opp_table);
Fabio Estevam035ed072017-09-29 14:39:49 -0300438 dev_dbg(dev, "%s: OPP table not found (%d)\n",
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800439 __func__, count);
Viresh Kumar09f662f2018-10-03 15:22:03 +0530440 return count;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200441 }
442
Viresh Kumara1e8c132018-04-06 14:35:45 +0530443 count = _get_opp_count(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +0530444 dev_pm_opp_put_opp_table(opp_table);
445
Nishanth Menone1f60b22010-10-13 00:13:10 +0200446 return count;
447}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500448EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200449
Viresh Kumaraab8ced2022-06-02 13:03:05 +0530450/* Helpers to read keys */
451static unsigned long _read_freq(struct dev_pm_opp *opp, int index)
452{
Viresh Kumar2083da22022-06-10 12:02:04 +0530453 return opp->rates[0];
Viresh Kumaraab8ced2022-06-02 13:03:05 +0530454}
455
Viresh Kumarc2ab2cb2022-06-02 13:05:45 +0530456static unsigned long _read_level(struct dev_pm_opp *opp, int index)
457{
458 return opp->level;
459}
460
Viresh Kumaradd1dc02022-06-02 13:05:45 +0530461static unsigned long _read_bw(struct dev_pm_opp *opp, int index)
462{
463 return opp->bandwidth[index].peak;
464}
465
Viresh Kumaraab8ced2022-06-02 13:03:05 +0530466/* Generic comparison helpers */
467static bool _compare_exact(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
468 unsigned long opp_key, unsigned long key)
469{
470 if (opp_key == key) {
471 *opp = temp_opp;
472 return true;
473 }
474
475 return false;
476}
477
478static bool _compare_ceil(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
479 unsigned long opp_key, unsigned long key)
480{
481 if (opp_key >= key) {
482 *opp = temp_opp;
483 return true;
484 }
485
486 return false;
487}
488
489static bool _compare_floor(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
490 unsigned long opp_key, unsigned long key)
491{
492 if (opp_key > key)
493 return true;
494
495 *opp = temp_opp;
496 return false;
497}
498
499/* Generic key finding helpers */
500static struct dev_pm_opp *_opp_table_find_key(struct opp_table *opp_table,
501 unsigned long *key, int index, bool available,
502 unsigned long (*read)(struct dev_pm_opp *opp, int index),
503 bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
Viresh Kumare10a4642022-06-10 09:40:30 +0530504 unsigned long opp_key, unsigned long key),
505 bool (*assert)(struct opp_table *opp_table))
Viresh Kumaraab8ced2022-06-02 13:03:05 +0530506{
507 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
508
Viresh Kumare10a4642022-06-10 09:40:30 +0530509 /* Assert that the requirement is met */
510 if (assert && !assert(opp_table))
511 return ERR_PTR(-EINVAL);
512
Viresh Kumaraab8ced2022-06-02 13:03:05 +0530513 mutex_lock(&opp_table->lock);
514
515 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
516 if (temp_opp->available == available) {
517 if (compare(&opp, temp_opp, read(temp_opp, index), *key))
518 break;
519 }
520 }
521
522 /* Increment the reference count of OPP */
523 if (!IS_ERR(opp)) {
524 *key = read(opp, index);
525 dev_pm_opp_get(opp);
526 }
527
528 mutex_unlock(&opp_table->lock);
529
530 return opp;
531}
532
533static struct dev_pm_opp *
534_find_key(struct device *dev, unsigned long *key, int index, bool available,
535 unsigned long (*read)(struct dev_pm_opp *opp, int index),
536 bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
Viresh Kumare10a4642022-06-10 09:40:30 +0530537 unsigned long opp_key, unsigned long key),
538 bool (*assert)(struct opp_table *opp_table))
Viresh Kumaraab8ced2022-06-02 13:03:05 +0530539{
540 struct opp_table *opp_table;
541 struct dev_pm_opp *opp;
542
543 opp_table = _find_opp_table(dev);
544 if (IS_ERR(opp_table)) {
545 dev_err(dev, "%s: OPP table not found (%ld)\n", __func__,
546 PTR_ERR(opp_table));
547 return ERR_CAST(opp_table);
548 }
549
550 opp = _opp_table_find_key(opp_table, key, index, available, read,
Viresh Kumare10a4642022-06-10 09:40:30 +0530551 compare, assert);
Viresh Kumaraab8ced2022-06-02 13:03:05 +0530552
553 dev_pm_opp_put_opp_table(opp_table);
554
555 return opp;
556}
557
558static struct dev_pm_opp *_find_key_exact(struct device *dev,
559 unsigned long key, int index, bool available,
Viresh Kumare10a4642022-06-10 09:40:30 +0530560 unsigned long (*read)(struct dev_pm_opp *opp, int index),
561 bool (*assert)(struct opp_table *opp_table))
Viresh Kumaraab8ced2022-06-02 13:03:05 +0530562{
563 /*
564 * The value of key will be updated here, but will be ignored as the
565 * caller doesn't need it.
566 */
Viresh Kumare10a4642022-06-10 09:40:30 +0530567 return _find_key(dev, &key, index, available, read, _compare_exact,
568 assert);
Viresh Kumaraab8ced2022-06-02 13:03:05 +0530569}
570
571static struct dev_pm_opp *_opp_table_find_key_ceil(struct opp_table *opp_table,
572 unsigned long *key, int index, bool available,
Viresh Kumare10a4642022-06-10 09:40:30 +0530573 unsigned long (*read)(struct dev_pm_opp *opp, int index),
574 bool (*assert)(struct opp_table *opp_table))
Viresh Kumaraab8ced2022-06-02 13:03:05 +0530575{
576 return _opp_table_find_key(opp_table, key, index, available, read,
Viresh Kumare10a4642022-06-10 09:40:30 +0530577 _compare_ceil, assert);
Viresh Kumaraab8ced2022-06-02 13:03:05 +0530578}
579
580static struct dev_pm_opp *_find_key_ceil(struct device *dev, unsigned long *key,
581 int index, bool available,
Viresh Kumare10a4642022-06-10 09:40:30 +0530582 unsigned long (*read)(struct dev_pm_opp *opp, int index),
583 bool (*assert)(struct opp_table *opp_table))
Viresh Kumaraab8ced2022-06-02 13:03:05 +0530584{
Viresh Kumare10a4642022-06-10 09:40:30 +0530585 return _find_key(dev, key, index, available, read, _compare_ceil,
586 assert);
Viresh Kumaraab8ced2022-06-02 13:03:05 +0530587}
588
589static struct dev_pm_opp *_find_key_floor(struct device *dev,
590 unsigned long *key, int index, bool available,
Viresh Kumare10a4642022-06-10 09:40:30 +0530591 unsigned long (*read)(struct dev_pm_opp *opp, int index),
592 bool (*assert)(struct opp_table *opp_table))
Viresh Kumaraab8ced2022-06-02 13:03:05 +0530593{
Viresh Kumare10a4642022-06-10 09:40:30 +0530594 return _find_key(dev, key, index, available, read, _compare_floor,
595 assert);
Viresh Kumaraab8ced2022-06-02 13:03:05 +0530596}
597
Nishanth Menone1f60b22010-10-13 00:13:10 +0200598/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500599 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200600 * @dev: device for which we do this operation
601 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100602 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200603 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +0530604 * Return: Searches for exact match in the opp table and returns pointer to the
Nishanth Menon984f16c82014-12-24 11:22:57 -0600605 * matching opp if found, else returns ERR_PTR in case of error and should
606 * be handled using IS_ERR. Error return values can be:
Nishanth Menon07797262012-10-24 22:00:12 +0200607 * EINVAL: for bad pointer
608 * ERANGE: no match found for search
609 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200610 *
611 * Note: available is a modifier for the search. if available=true, then the
612 * match is for exact matching frequency and is available in the stored OPP
613 * table. if false, the match is for exact frequency which is not available.
614 *
615 * This provides a mechanism to enable an opp which is not available currently
616 * or the opposite as well.
617 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530618 * The callers are required to call dev_pm_opp_put() for the returned OPP after
619 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200620 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500621struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
Viresh Kumaraab8ced2022-06-02 13:03:05 +0530622 unsigned long freq, bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200623{
Viresh Kumarf123ea72022-06-10 10:27:51 +0530624 return _find_key_exact(dev, freq, 0, available, _read_freq,
625 assert_single_clk);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200626}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500627EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200628
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800629static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
630 unsigned long *freq)
631{
Viresh Kumare10a4642022-06-10 09:40:30 +0530632 return _opp_table_find_key_ceil(opp_table, freq, 0, true, _read_freq,
Viresh Kumarf123ea72022-06-10 10:27:51 +0530633 assert_single_clk);
Jisheng Zhang067b7ce2016-07-25 14:11:16 +0800634}
635
Nishanth Menone1f60b22010-10-13 00:13:10 +0200636/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500637 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200638 * @dev: device for which we do this operation
639 * @freq: Start frequency
640 *
641 * Search for the matching ceil *available* OPP from a starting freq
642 * for a device.
643 *
Nishanth Menon984f16c82014-12-24 11:22:57 -0600644 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200645 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
646 * values can be:
647 * EINVAL: for bad pointer
648 * ERANGE: no match found for search
649 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200650 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530651 * The callers are required to call dev_pm_opp_put() for the returned OPP after
652 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200653 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500654struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
655 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200656{
Viresh Kumarf123ea72022-06-10 10:27:51 +0530657 return _find_key_ceil(dev, freq, 0, true, _read_freq, assert_single_clk);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200658}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500659EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200660
661/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500662 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200663 * @dev: device for which we do this operation
664 * @freq: Start frequency
665 *
666 * Search for the matching floor *available* OPP from a starting freq
667 * for a device.
668 *
Nishanth Menon984f16c82014-12-24 11:22:57 -0600669 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200670 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
671 * values can be:
672 * EINVAL: for bad pointer
673 * ERANGE: no match found for search
674 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200675 *
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530676 * The callers are required to call dev_pm_opp_put() for the returned OPP after
677 * use.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200678 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500679struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
680 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200681{
Viresh Kumarf123ea72022-06-10 10:27:51 +0530682 return _find_key_floor(dev, freq, 0, true, _read_freq, assert_single_clk);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200683}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500684EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200685
Andrew-sh.Cheng2f36bde2019-03-29 14:46:10 +0800686/**
Viresh Kumar22079af2022-05-04 15:40:22 +0530687 * dev_pm_opp_find_level_exact() - search for an exact level
688 * @dev: device for which we do this operation
689 * @level: level to search for
690 *
691 * Return: Searches for exact match in the opp table and returns pointer to the
692 * matching opp if found, else returns ERR_PTR in case of error and should
693 * be handled using IS_ERR. Error return values can be:
694 * EINVAL: for bad pointer
695 * ERANGE: no match found for search
696 * ENODEV: if device not found in list of registered devices
697 *
698 * The callers are required to call dev_pm_opp_put() for the returned OPP after
699 * use.
700 */
701struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
702 unsigned int level)
703{
Viresh Kumare10a4642022-06-10 09:40:30 +0530704 return _find_key_exact(dev, level, 0, true, _read_level, NULL);
Viresh Kumar22079af2022-05-04 15:40:22 +0530705}
706EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
707
708/**
709 * dev_pm_opp_find_level_ceil() - search for an rounded up level
710 * @dev: device for which we do this operation
711 * @level: level to search for
712 *
713 * Return: Searches for rounded up match in the opp table and returns pointer
714 * to the matching opp if found, else returns ERR_PTR in case of error and
715 * should be handled using IS_ERR. Error return values can be:
716 * EINVAL: for bad pointer
717 * ERANGE: no match found for search
718 * ENODEV: if device not found in list of registered devices
719 *
720 * The callers are required to call dev_pm_opp_put() for the returned OPP after
721 * use.
722 */
723struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
724 unsigned int *level)
725{
Viresh Kumarc2ab2cb2022-06-02 13:05:45 +0530726 unsigned long temp = *level;
727 struct dev_pm_opp *opp;
Viresh Kumar22079af2022-05-04 15:40:22 +0530728
Viresh Kumare10a4642022-06-10 09:40:30 +0530729 opp = _find_key_ceil(dev, &temp, 0, true, _read_level, NULL);
Viresh Kumarc2ab2cb2022-06-02 13:05:45 +0530730 *level = temp;
Viresh Kumar22079af2022-05-04 15:40:22 +0530731 return opp;
732}
733EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
734
735/**
Krzysztof Kozlowski00ce3872022-05-04 10:17:32 +0200736 * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth
737 * @dev: device for which we do this operation
Yang Li617df302022-05-26 21:20:35 +0800738 * @bw: start bandwidth
Krzysztof Kozlowski00ce3872022-05-04 10:17:32 +0200739 * @index: which bandwidth to compare, in case of OPPs with several values
740 *
741 * Search for the matching floor *available* OPP from a starting bandwidth
742 * for a device.
743 *
744 * Return: matching *opp and refreshes *bw accordingly, else returns
745 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
746 * values can be:
747 * EINVAL: for bad pointer
748 * ERANGE: no match found for search
749 * ENODEV: if device not found in list of registered devices
750 *
751 * The callers are required to call dev_pm_opp_put() for the returned OPP after
752 * use.
753 */
Viresh Kumaradd1dc02022-06-02 13:05:45 +0530754struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev, unsigned int *bw,
755 int index)
Krzysztof Kozlowski00ce3872022-05-04 10:17:32 +0200756{
Viresh Kumaradd1dc02022-06-02 13:05:45 +0530757 unsigned long temp = *bw;
758 struct dev_pm_opp *opp;
Krzysztof Kozlowski00ce3872022-05-04 10:17:32 +0200759
Viresh Kumare10a4642022-06-10 09:40:30 +0530760 opp = _find_key_ceil(dev, &temp, index, true, _read_bw, NULL);
Viresh Kumaradd1dc02022-06-02 13:05:45 +0530761 *bw = temp;
Krzysztof Kozlowski00ce3872022-05-04 10:17:32 +0200762 return opp;
763}
764EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_ceil);
765
766/**
767 * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth
768 * @dev: device for which we do this operation
Yang Li617df302022-05-26 21:20:35 +0800769 * @bw: start bandwidth
Krzysztof Kozlowski00ce3872022-05-04 10:17:32 +0200770 * @index: which bandwidth to compare, in case of OPPs with several values
771 *
772 * Search for the matching floor *available* OPP from a starting bandwidth
773 * for a device.
774 *
775 * Return: matching *opp and refreshes *bw accordingly, else returns
776 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
777 * values can be:
778 * EINVAL: for bad pointer
779 * ERANGE: no match found for search
780 * ENODEV: if device not found in list of registered devices
781 *
782 * The callers are required to call dev_pm_opp_put() for the returned OPP after
783 * use.
784 */
785struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev,
786 unsigned int *bw, int index)
787{
Viresh Kumaradd1dc02022-06-02 13:05:45 +0530788 unsigned long temp = *bw;
789 struct dev_pm_opp *opp;
Krzysztof Kozlowski00ce3872022-05-04 10:17:32 +0200790
Viresh Kumare10a4642022-06-10 09:40:30 +0530791 opp = _find_key_floor(dev, &temp, index, true, _read_bw, NULL);
Viresh Kumaradd1dc02022-06-02 13:05:45 +0530792 *bw = temp;
Krzysztof Kozlowski00ce3872022-05-04 10:17:32 +0200793 return opp;
794}
795EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_floor);
796
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530797static int _set_opp_voltage(struct device *dev, struct regulator *reg,
Viresh Kumarce317812016-12-01 16:28:18 +0530798 struct dev_pm_opp_supply *supply)
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530799{
800 int ret;
801
802 /* Regulator not available for device */
803 if (IS_ERR(reg)) {
804 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
805 PTR_ERR(reg));
806 return 0;
807 }
808
Viresh Kumarce317812016-12-01 16:28:18 +0530809 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
810 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530811
Viresh Kumarce317812016-12-01 16:28:18 +0530812 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
813 supply->u_volt, supply->u_volt_max);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530814 if (ret)
815 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
Viresh Kumarce317812016-12-01 16:28:18 +0530816 __func__, supply->u_volt_min, supply->u_volt,
817 supply->u_volt_max, ret);
Viresh Kumar6a0712f2016-02-09 10:30:39 +0530818
819 return ret;
820}
821
Viresh Kumar2083da22022-06-10 12:02:04 +0530822static int
823_opp_config_clk_single(struct device *dev, struct opp_table *opp_table,
824 struct dev_pm_opp *opp, void *data, bool scaling_down)
Viresh Kumar94735582016-12-01 16:28:20 +0530825{
Viresh Kumar1efae8d2022-06-03 15:01:30 +0530826 unsigned long *target = data;
827 unsigned long freq;
Viresh Kumar94735582016-12-01 16:28:20 +0530828 int ret;
829
Viresh Kumar1efae8d2022-06-03 15:01:30 +0530830 /* One of target and opp must be available */
831 if (target) {
832 freq = *target;
833 } else if (opp) {
Viresh Kumar2083da22022-06-10 12:02:04 +0530834 freq = opp->rates[0];
Viresh Kumar1efae8d2022-06-03 15:01:30 +0530835 } else {
836 WARN_ON(1);
837 return -EINVAL;
838 }
839
840 ret = clk_set_rate(opp_table->clk, freq);
Viresh Kumar94735582016-12-01 16:28:20 +0530841 if (ret) {
842 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
843 ret);
Viresh Kumar1efae8d2022-06-03 15:01:30 +0530844 } else {
845 opp_table->rate_clk_single = freq;
Viresh Kumar94735582016-12-01 16:28:20 +0530846 }
847
848 return ret;
849}
850
Viresh Kumar8174a3a2022-06-10 12:37:25 +0530851/*
852 * Simple implementation for configuring multiple clocks. Configure clocks in
853 * the order in which they are present in the array while scaling up.
854 */
855int dev_pm_opp_config_clks_simple(struct device *dev,
856 struct opp_table *opp_table, struct dev_pm_opp *opp, void *data,
857 bool scaling_down)
858{
859 int ret, i;
860
861 if (scaling_down) {
862 for (i = opp_table->clk_count - 1; i >= 0; i--) {
863 ret = clk_set_rate(opp_table->clks[i], opp->rates[i]);
864 if (ret) {
865 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
866 ret);
867 return ret;
868 }
869 }
870 } else {
871 for (i = 0; i < opp_table->clk_count; i++) {
872 ret = clk_set_rate(opp_table->clks[i], opp->rates[i]);
873 if (ret) {
874 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
875 ret);
876 return ret;
877 }
878 }
879 }
880
Christophe JAILLETd36cb842022-08-15 14:44:37 +0200881 return 0;
Viresh Kumar8174a3a2022-06-10 12:37:25 +0530882}
883EXPORT_SYMBOL_GPL(dev_pm_opp_config_clks_simple);
884
Viresh Kumarc522ce82022-05-31 12:49:19 +0530885static int _opp_config_regulator_single(struct device *dev,
886 struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp,
887 struct regulator **regulators, unsigned int count)
Viresh Kumar94735582016-12-01 16:28:20 +0530888{
Viresh Kumarc522ce82022-05-31 12:49:19 +0530889 struct regulator *reg = regulators[0];
Viresh Kumar94735582016-12-01 16:28:20 +0530890 int ret;
891
892 /* This function only supports single regulator per device */
Viresh Kumarc522ce82022-05-31 12:49:19 +0530893 if (WARN_ON(count > 1)) {
Viresh Kumar94735582016-12-01 16:28:20 +0530894 dev_err(dev, "multiple regulators are not supported\n");
895 return -EINVAL;
896 }
897
Viresh Kumarc522ce82022-05-31 12:49:19 +0530898 ret = _set_opp_voltage(dev, reg, new_opp->supplies);
Viresh Kumar94735582016-12-01 16:28:20 +0530899 if (ret)
Viresh Kumarc522ce82022-05-31 12:49:19 +0530900 return ret;
Viresh Kumar94735582016-12-01 16:28:20 +0530901
Kamil Konieczny8d457192019-07-19 17:05:32 +0200902 /*
903 * Enable the regulator after setting its voltages, otherwise it breaks
904 * some boot-enabled regulators.
905 */
Viresh Kumarc522ce82022-05-31 12:49:19 +0530906 if (unlikely(!new_opp->opp_table->enabled)) {
Kamil Konieczny8d457192019-07-19 17:05:32 +0200907 ret = regulator_enable(reg);
908 if (ret < 0)
909 dev_warn(dev, "Failed to enable regulator: %d", ret);
Kamil Konieczny8d457192019-07-19 17:05:32 +0200910 }
911
Viresh Kumar94735582016-12-01 16:28:20 +0530912 return 0;
Viresh Kumar94735582016-12-01 16:28:20 +0530913}
914
Viresh Kumarb00e6672020-05-27 09:33:44 +0530915static int _set_opp_bw(const struct opp_table *opp_table,
Viresh Kumar240ae502021-01-21 15:27:55 +0530916 struct dev_pm_opp *opp, struct device *dev)
Viresh Kumarb00e6672020-05-27 09:33:44 +0530917{
918 u32 avg, peak;
919 int i, ret;
920
921 if (!opp_table->paths)
922 return 0;
923
924 for (i = 0; i < opp_table->path_count; i++) {
Viresh Kumar240ae502021-01-21 15:27:55 +0530925 if (!opp) {
Viresh Kumarb00e6672020-05-27 09:33:44 +0530926 avg = 0;
927 peak = 0;
928 } else {
929 avg = opp->bandwidth[i].avg;
930 peak = opp->bandwidth[i].peak;
931 }
932 ret = icc_set_bw(opp_table->paths[i], avg, peak);
933 if (ret) {
934 dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
Viresh Kumar240ae502021-01-21 15:27:55 +0530935 opp ? "set" : "remove", i, ret);
Viresh Kumarb00e6672020-05-27 09:33:44 +0530936 return ret;
937 }
938 }
939
940 return 0;
941}
942
Viresh Kumar528f2d82023-02-22 16:36:38 +0530943static int _set_performance_state(struct device *dev, struct device *pd_dev,
944 struct dev_pm_opp *opp, int i)
Stephan Gerhold60cdeae2020-07-30 10:01:44 +0200945{
Viresh Kumar7c41cdc2023-06-14 15:29:32 +0530946 unsigned int pstate = likely(opp) ? opp->required_opps[i]->level: 0;
Stephan Gerhold60cdeae2020-07-30 10:01:44 +0200947 int ret;
948
949 if (!pd_dev)
950 return 0;
951
952 ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
953 if (ret) {
Viresh Kumar9bfb1ff2022-06-03 15:27:55 +0530954 dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
Stephan Gerhold60cdeae2020-07-30 10:01:44 +0200955 dev_name(pd_dev), pstate, ret);
956 }
957
958 return ret;
959}
960
Viresh Kumar528f2d82023-02-22 16:36:38 +0530961static int _opp_set_required_opps_generic(struct device *dev,
962 struct opp_table *opp_table, struct dev_pm_opp *opp, bool scaling_down)
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530963{
Viresh Kumar528f2d82023-02-22 16:36:38 +0530964 dev_err(dev, "setting required-opps isn't supported for non-genpd devices\n");
965 return -ENOENT;
966}
967
968static int _opp_set_required_opps_genpd(struct device *dev,
969 struct opp_table *opp_table, struct dev_pm_opp *opp, bool scaling_down)
970{
Viresh Kumar29b1a922023-02-22 16:36:37 +0530971 struct device **genpd_virt_devs =
972 opp_table->genpd_virt_devs ? opp_table->genpd_virt_devs : &dev;
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530973 int i, ret = 0;
974
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530975 /*
976 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
977 * after it is freed from another thread.
978 */
979 mutex_lock(&opp_table->genpd_virt_dev_lock);
980
Stephan Gerhold2c591382020-07-30 10:01:45 +0200981 /* Scaling up? Set required OPPs in normal order, else reverse */
Viresh Kumar528f2d82023-02-22 16:36:38 +0530982 if (!scaling_down) {
Stephan Gerhold2c591382020-07-30 10:01:45 +0200983 for (i = 0; i < opp_table->required_opp_count; i++) {
Viresh Kumar528f2d82023-02-22 16:36:38 +0530984 ret = _set_performance_state(dev, genpd_virt_devs[i], opp, i);
Stephan Gerhold2c591382020-07-30 10:01:45 +0200985 if (ret)
986 break;
987 }
988 } else {
989 for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
Viresh Kumar528f2d82023-02-22 16:36:38 +0530990 ret = _set_performance_state(dev, genpd_virt_devs[i], opp, i);
Stephan Gerhold2c591382020-07-30 10:01:45 +0200991 if (ret)
992 break;
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530993 }
994 }
Stephan Gerhold2c591382020-07-30 10:01:45 +0200995
Viresh Kumarca1b5d72018-06-14 10:03:26 +0530996 mutex_unlock(&opp_table->genpd_virt_dev_lock);
997
998 return ret;
999}
1000
Viresh Kumar528f2d82023-02-22 16:36:38 +05301001/* This is only called for PM domain for now */
1002static int _set_required_opps(struct device *dev, struct opp_table *opp_table,
1003 struct dev_pm_opp *opp, bool up)
1004{
1005 /* required-opps not fully initialized yet */
1006 if (lazy_linking_pending(opp_table))
1007 return -EBUSY;
1008
1009 if (opp_table->set_required_opps)
1010 return opp_table->set_required_opps(dev, opp_table, opp, up);
1011
1012 return 0;
1013}
1014
1015/* Update set_required_opps handler */
1016void _update_set_required_opps(struct opp_table *opp_table)
1017{
1018 /* Already set */
1019 if (opp_table->set_required_opps)
1020 return;
1021
1022 /* All required OPPs will belong to genpd or none */
1023 if (opp_table->required_opp_tables[0]->is_genpd)
1024 opp_table->set_required_opps = _opp_set_required_opps_genpd;
1025 else
1026 opp_table->set_required_opps = _opp_set_required_opps_generic;
1027}
1028
Viresh Kumar81c4d8a2021-01-20 16:16:48 +05301029static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
1030{
1031 struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
1032 unsigned long freq;
1033
1034 if (!IS_ERR(opp_table->clk)) {
1035 freq = clk_get_rate(opp_table->clk);
1036 opp = _find_freq_ceil(opp_table, &freq);
1037 }
1038
1039 /*
1040 * Unable to find the current OPP ? Pick the first from the list since
1041 * it is in ascending order, otherwise rest of the code will need to
1042 * make special checks to validate current_opp.
1043 */
1044 if (IS_ERR(opp)) {
1045 mutex_lock(&opp_table->lock);
1046 opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
1047 dev_pm_opp_get(opp);
1048 mutex_unlock(&opp_table->lock);
1049 }
1050
1051 opp_table->current_opp = opp;
1052}
1053
Viresh Kumar5ad58bb2021-01-21 12:08:45 +05301054static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
Viresh Kumarf3364e12020-08-13 09:48:04 +05301055{
1056 int ret;
1057
1058 if (!opp_table->enabled)
1059 return 0;
1060
1061 /*
1062 * Some drivers need to support cases where some platforms may
1063 * have OPP table for the device, while others don't and
1064 * opp_set_rate() just needs to behave like clk_set_rate().
1065 */
1066 if (!_get_opp_count(opp_table))
1067 return 0;
1068
Viresh Kumar240ae502021-01-21 15:27:55 +05301069 ret = _set_opp_bw(opp_table, NULL, dev);
Viresh Kumarf3364e12020-08-13 09:48:04 +05301070 if (ret)
1071 return ret;
1072
1073 if (opp_table->regulators)
1074 regulator_disable(opp_table->regulators[0]);
1075
Stephan Gerhold2c591382020-07-30 10:01:45 +02001076 ret = _set_required_opps(dev, opp_table, NULL, false);
Viresh Kumarf3364e12020-08-13 09:48:04 +05301077
1078 opp_table->enabled = false;
1079 return ret;
1080}
1081
Viresh Kumar386ba852021-01-21 12:12:09 +05301082static int _set_opp(struct device *dev, struct opp_table *opp_table,
Viresh Kumar1efae8d2022-06-03 15:01:30 +05301083 struct dev_pm_opp *opp, void *clk_data, bool forced)
Viresh Kumar6a0712f2016-02-09 10:30:39 +05301084{
Viresh Kumar386ba852021-01-21 12:12:09 +05301085 struct dev_pm_opp *old_opp;
Viresh Kumarf0b88fa42021-01-21 16:00:12 +05301086 int scaling_down, ret;
Viresh Kumar6a0712f2016-02-09 10:30:39 +05301087
Viresh Kumar386ba852021-01-21 12:12:09 +05301088 if (unlikely(!opp))
1089 return _disable_opp_table(dev, opp_table);
Rajendra Nayakaca48b62020-04-08 19:16:27 +05301090
Viresh Kumar81c4d8a2021-01-20 16:16:48 +05301091 /* Find the currently set OPP if we don't know already */
1092 if (unlikely(!opp_table->current_opp))
1093 _find_current_opp(dev, opp_table);
Viresh Kumar6a0712f2016-02-09 10:30:39 +05301094
Viresh Kumar81c4d8a2021-01-20 16:16:48 +05301095 old_opp = opp_table->current_opp;
Viresh Kumar81c4d8a2021-01-20 16:16:48 +05301096
1097 /* Return early if nothing to do */
Viresh Kumar1efae8d2022-06-03 15:01:30 +05301098 if (!forced && old_opp == opp && opp_table->enabled) {
Adrián Larumbe9e28f7a2023-05-20 18:00:35 +01001099 dev_dbg_ratelimited(dev, "%s: OPPs are same, nothing to do\n", __func__);
Viresh Kumar386ba852021-01-21 12:12:09 +05301100 return 0;
Viresh Kumar6a0712f2016-02-09 10:30:39 +05301101 }
1102
Viresh Kumarf0b88fa42021-01-21 16:00:12 +05301103 dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
Viresh Kumar2083da22022-06-10 12:02:04 +05301104 __func__, old_opp->rates[0], opp->rates[0], old_opp->level,
1105 opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
Viresh Kumarf0b88fa42021-01-21 16:00:12 +05301106 opp->bandwidth ? opp->bandwidth[0].peak : 0);
1107
Viresh Kumar2083da22022-06-10 12:02:04 +05301108 scaling_down = _opp_compare_key(opp_table, old_opp, opp);
Viresh Kumarf0b88fa42021-01-21 16:00:12 +05301109 if (scaling_down == -1)
1110 scaling_down = 0;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301111
Viresh Kumarca1b5d72018-06-14 10:03:26 +05301112 /* Scaling up? Configure required OPPs before frequency */
Viresh Kumarf0b88fa42021-01-21 16:00:12 +05301113 if (!scaling_down) {
Stephan Gerhold2c591382020-07-30 10:01:45 +02001114 ret = _set_required_opps(dev, opp_table, opp, true);
Viresh Kumar870d5d92021-01-28 15:30:00 +05301115 if (ret) {
1116 dev_err(dev, "Failed to set required opps: %d\n", ret);
Viresh Kumar386ba852021-01-21 12:12:09 +05301117 return ret;
Viresh Kumar870d5d92021-01-28 15:30:00 +05301118 }
1119
1120 ret = _set_opp_bw(opp_table, opp, dev);
1121 if (ret) {
1122 dev_err(dev, "Failed to set bw: %d\n", ret);
1123 return ret;
1124 }
Viresh Kumaraee33522022-07-04 13:45:08 +05301125
1126 if (opp_table->config_regulators) {
1127 ret = opp_table->config_regulators(dev, old_opp, opp,
1128 opp_table->regulators,
1129 opp_table->regulator_count);
1130 if (ret) {
1131 dev_err(dev, "Failed to set regulator voltages: %d\n",
1132 ret);
1133 return ret;
1134 }
1135 }
Viresh Kumarca1b5d72018-06-14 10:03:26 +05301136 }
1137
Viresh Kumar2083da22022-06-10 12:02:04 +05301138 if (opp_table->config_clks) {
1139 ret = opp_table->config_clks(dev, opp_table, opp, clk_data, scaling_down);
1140 if (ret)
1141 return ret;
1142 }
Viresh Kumar870d5d92021-01-28 15:30:00 +05301143
Viresh Kumarca1b5d72018-06-14 10:03:26 +05301144 /* Scaling down? Configure required OPPs after frequency */
Viresh Kumar870d5d92021-01-28 15:30:00 +05301145 if (scaling_down) {
Viresh Kumaraee33522022-07-04 13:45:08 +05301146 if (opp_table->config_regulators) {
1147 ret = opp_table->config_regulators(dev, old_opp, opp,
1148 opp_table->regulators,
1149 opp_table->regulator_count);
1150 if (ret) {
1151 dev_err(dev, "Failed to set regulator voltages: %d\n",
1152 ret);
1153 return ret;
1154 }
1155 }
1156
Viresh Kumar240ae502021-01-21 15:27:55 +05301157 ret = _set_opp_bw(opp_table, opp, dev);
Viresh Kumar870d5d92021-01-28 15:30:00 +05301158 if (ret) {
1159 dev_err(dev, "Failed to set bw: %d\n", ret);
1160 return ret;
1161 }
Viresh Kumar81c4d8a2021-01-20 16:16:48 +05301162
Viresh Kumar870d5d92021-01-28 15:30:00 +05301163 ret = _set_required_opps(dev, opp_table, opp, false);
1164 if (ret) {
1165 dev_err(dev, "Failed to set required opps: %d\n", ret);
1166 return ret;
Viresh Kumar81c4d8a2021-01-20 16:16:48 +05301167 }
Viresh Kumar72f80ce2020-08-13 08:38:37 +05301168 }
Georgi Djakovfe2af402020-05-12 15:53:23 +03001169
Viresh Kumar870d5d92021-01-28 15:30:00 +05301170 opp_table->enabled = true;
1171 dev_pm_opp_put(old_opp);
1172
1173 /* Make sure current_opp doesn't get freed */
1174 dev_pm_opp_get(opp);
1175 opp_table->current_opp = opp;
1176
Viresh Kumar386ba852021-01-21 12:12:09 +05301177 return ret;
1178}
1179
1180/**
1181 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1182 * @dev: device for which we do this operation
1183 * @target_freq: frequency to achieve
1184 *
1185 * This configures the power-supplies to the levels specified by the OPP
1186 * corresponding to the target_freq, and programs the clock to a value <=
1187 * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1188 * provided by the opp, should have already rounded to the target OPP's
1189 * frequency.
1190 */
1191int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1192{
1193 struct opp_table *opp_table;
1194 unsigned long freq = 0, temp_freq;
1195 struct dev_pm_opp *opp = NULL;
Viresh Kumar1efae8d2022-06-03 15:01:30 +05301196 bool forced = false;
Viresh Kumar386ba852021-01-21 12:12:09 +05301197 int ret;
1198
1199 opp_table = _find_opp_table(dev);
1200 if (IS_ERR(opp_table)) {
1201 dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1202 return PTR_ERR(opp_table);
1203 }
1204
1205 if (target_freq) {
1206 /*
1207 * For IO devices which require an OPP on some platforms/SoCs
1208 * while just needing to scale the clock on some others
1209 * we look for empty OPP tables with just a clock handle and
1210 * scale only the clk. This makes dev_pm_opp_set_rate()
1211 * equivalent to a clk_set_rate()
1212 */
1213 if (!_get_opp_count(opp_table)) {
Viresh Kumar2083da22022-06-10 12:02:04 +05301214 ret = opp_table->config_clks(dev, opp_table, NULL,
1215 &target_freq, false);
Viresh Kumar386ba852021-01-21 12:12:09 +05301216 goto put_opp_table;
1217 }
1218
1219 freq = clk_round_rate(opp_table->clk, target_freq);
1220 if ((long)freq <= 0)
1221 freq = target_freq;
1222
1223 /*
1224 * The clock driver may support finer resolution of the
1225 * frequencies than the OPP table, don't update the frequency we
1226 * pass to clk_set_rate() here.
1227 */
1228 temp_freq = freq;
1229 opp = _find_freq_ceil(opp_table, &temp_freq);
1230 if (IS_ERR(opp)) {
1231 ret = PTR_ERR(opp);
1232 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1233 __func__, freq, ret);
1234 goto put_opp_table;
1235 }
Viresh Kumar1efae8d2022-06-03 15:01:30 +05301236
1237 /*
1238 * An OPP entry specifies the highest frequency at which other
1239 * properties of the OPP entry apply. Even if the new OPP is
1240 * same as the old one, we may still reach here for a different
1241 * value of the frequency. In such a case, do not abort but
1242 * configure the hardware to the desired frequency forcefully.
1243 */
1244 forced = opp_table->rate_clk_single != target_freq;
Viresh Kumar386ba852021-01-21 12:12:09 +05301245 }
1246
Viresh Kumar1efae8d2022-06-03 15:01:30 +05301247 ret = _set_opp(dev, opp_table, opp, &target_freq, forced);
Viresh Kumar386ba852021-01-21 12:12:09 +05301248
1249 if (target_freq)
1250 dev_pm_opp_put(opp);
Viresh Kumar1efae8d2022-06-03 15:01:30 +05301251
Viresh Kumar052c6f12017-01-23 10:11:49 +05301252put_opp_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +05301253 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar052c6f12017-01-23 10:11:49 +05301254 return ret;
Viresh Kumar6a0712f2016-02-09 10:30:39 +05301255}
1256EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
1257
Viresh Kumarabbe3482021-01-21 12:15:36 +05301258/**
1259 * dev_pm_opp_set_opp() - Configure device for OPP
1260 * @dev: device for which we do this operation
1261 * @opp: OPP to set to
1262 *
1263 * This configures the device based on the properties of the OPP passed to this
1264 * routine.
1265 *
1266 * Return: 0 on success, a negative error number otherwise.
1267 */
1268int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
1269{
1270 struct opp_table *opp_table;
1271 int ret;
1272
1273 opp_table = _find_opp_table(dev);
1274 if (IS_ERR(opp_table)) {
1275 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
1276 return PTR_ERR(opp_table);
1277 }
1278
Viresh Kumar1efae8d2022-06-03 15:01:30 +05301279 ret = _set_opp(dev, opp_table, opp, NULL, false);
Viresh Kumarabbe3482021-01-21 12:15:36 +05301280 dev_pm_opp_put_opp_table(opp_table);
1281
1282 return ret;
1283}
1284EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp);
1285
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301286/* OPP-dev Helpers */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301287static void _remove_opp_dev(struct opp_device *opp_dev,
1288 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +05301289{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301290 opp_debug_unregister(opp_dev, opp_table);
1291 list_del(&opp_dev->node);
Viresh Kumar052c6f12017-01-23 10:11:49 +05301292 kfree(opp_dev);
Viresh Kumar06441652015-07-29 16:23:04 +05301293}
1294
Viresh Kumaref43f012020-10-22 12:12:27 +05301295struct opp_device *_add_opp_dev(const struct device *dev,
1296 struct opp_table *opp_table)
Viresh Kumar06441652015-07-29 16:23:04 +05301297{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301298 struct opp_device *opp_dev;
Viresh Kumar06441652015-07-29 16:23:04 +05301299
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301300 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
1301 if (!opp_dev)
Viresh Kumar06441652015-07-29 16:23:04 +05301302 return NULL;
1303
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301304 /* Initialize opp-dev */
1305 opp_dev->dev = dev;
Viresh Kumar3d255692018-08-03 07:05:21 +05301306
Viresh Kumaref43f012020-10-22 12:12:27 +05301307 mutex_lock(&opp_table->lock);
Viresh Kumar052c6f12017-01-23 10:11:49 +05301308 list_add(&opp_dev->node, &opp_table->dev_list);
Viresh Kumaref43f012020-10-22 12:12:27 +05301309 mutex_unlock(&opp_table->lock);
Viresh Kumar06441652015-07-29 16:23:04 +05301310
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301311 /* Create debugfs entries for the opp_table */
Greg Kroah-Hartmana2dea4c2019-01-22 16:21:17 +01001312 opp_debug_register(opp_dev, opp_table);
Viresh Kumar283d55e2018-09-07 09:01:54 +05301313
1314 return opp_dev;
1315}
1316
Viresh Kumareb7c87432018-09-05 16:17:14 +05301317static struct opp_table *_allocate_opp_table(struct device *dev, int index)
Viresh Kumar07cce742014-12-10 09:45:34 +05301318{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301319 struct opp_table *opp_table;
1320 struct opp_device *opp_dev;
Viresh Kumard54974c2016-02-09 10:30:38 +05301321 int ret;
Viresh Kumar07cce742014-12-10 09:45:34 +05301322
1323 /*
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301324 * Allocate a new OPP table. In the infrequent case where a new
Viresh Kumar07cce742014-12-10 09:45:34 +05301325 * device is needed to be added, we pay this penalty.
1326 */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301327 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
1328 if (!opp_table)
Stephan Gerholddd461cd2020-07-27 11:30:46 +02001329 return ERR_PTR(-ENOMEM);
Viresh Kumar07cce742014-12-10 09:45:34 +05301330
Viresh Kumar3d255692018-08-03 07:05:21 +05301331 mutex_init(&opp_table->lock);
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301332 mutex_init(&opp_table->genpd_virt_dev_lock);
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301333 INIT_LIST_HEAD(&opp_table->dev_list);
Viresh Kumar7eba0c72019-11-25 13:57:58 +05301334 INIT_LIST_HEAD(&opp_table->lazy);
Viresh Kumar06441652015-07-29 16:23:04 +05301335
Viresh Kumar2083da22022-06-10 12:02:04 +05301336 opp_table->clk = ERR_PTR(-ENODEV);
1337
Viresh Kumar46f48ac2018-12-11 16:39:36 +05301338 /* Mark regulator count uninitialized */
1339 opp_table->regulator_count = -1;
1340
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301341 opp_dev = _add_opp_dev(dev, opp_table);
1342 if (!opp_dev) {
Stephan Gerholddd461cd2020-07-27 11:30:46 +02001343 ret = -ENOMEM;
1344 goto err;
Viresh Kumar06441652015-07-29 16:23:04 +05301345 }
1346
Viresh Kumareb7c87432018-09-05 16:17:14 +05301347 _of_init_opp_table(opp_table, dev, index);
Viresh Kumar50f8cfb2016-02-09 10:30:37 +05301348
Georgi Djakov6d3f9222020-05-12 15:53:21 +03001349 /* Find interconnect path(s) for the device */
1350 ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
Stephan Gerholddd461cd2020-07-27 11:30:46 +02001351 if (ret) {
1352 if (ret == -EPROBE_DEFER)
Viresh Kumar32439ac2021-01-28 12:05:22 +05301353 goto remove_opp_dev;
Stephan Gerholddd461cd2020-07-27 11:30:46 +02001354
Georgi Djakov6d3f9222020-05-12 15:53:21 +03001355 dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
1356 __func__, ret);
Stephan Gerholddd461cd2020-07-27 11:30:46 +02001357 }
Georgi Djakov6d3f9222020-05-12 15:53:21 +03001358
Viresh Kumar052c6f12017-01-23 10:11:49 +05301359 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301360 INIT_LIST_HEAD(&opp_table->opp_list);
Viresh Kumarf067a982017-01-23 10:11:42 +05301361 kref_init(&opp_table->kref);
Viresh Kumar07cce742014-12-10 09:45:34 +05301362
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301363 return opp_table;
Stephan Gerholddd461cd2020-07-27 11:30:46 +02001364
Quanyang Wang976509b2020-12-24 18:49:27 +08001365remove_opp_dev:
Stephan Gerholdb2a2ab02023-05-30 17:54:46 +02001366 _of_clear_opp_table(opp_table);
Quanyang Wang976509b2020-12-24 18:49:27 +08001367 _remove_opp_dev(opp_dev, opp_table);
Stephan Gerholdb2a2ab02023-05-30 17:54:46 +02001368 mutex_destroy(&opp_table->genpd_virt_dev_lock);
1369 mutex_destroy(&opp_table->lock);
Stephan Gerholddd461cd2020-07-27 11:30:46 +02001370err:
1371 kfree(opp_table);
1372 return ERR_PTR(ret);
Viresh Kumar07cce742014-12-10 09:45:34 +05301373}
1374
Viresh Kumarf067a982017-01-23 10:11:42 +05301375void _get_opp_table_kref(struct opp_table *opp_table)
Viresh Kumar3bac42c2015-07-29 16:22:59 +05301376{
Viresh Kumarf067a982017-01-23 10:11:42 +05301377 kref_get(&opp_table->kref);
1378}
1379
Viresh Kumar32439ac2021-01-28 12:05:22 +05301380static struct opp_table *_update_opp_table_clk(struct device *dev,
1381 struct opp_table *opp_table,
1382 bool getclk)
1383{
Viresh Kumard4a4c7a2021-01-29 16:12:04 +05301384 int ret;
1385
Viresh Kumar32439ac2021-01-28 12:05:22 +05301386 /*
Viresh Kumar2083da22022-06-10 12:02:04 +05301387 * Return early if we don't need to get clk or we have already done it
Viresh Kumar32439ac2021-01-28 12:05:22 +05301388 * earlier.
1389 */
Viresh Kumar2083da22022-06-10 12:02:04 +05301390 if (!getclk || IS_ERR(opp_table) || !IS_ERR(opp_table->clk) ||
1391 opp_table->clks)
Viresh Kumar32439ac2021-01-28 12:05:22 +05301392 return opp_table;
1393
1394 /* Find clk for the device */
1395 opp_table->clk = clk_get(dev, NULL);
Viresh Kumar32439ac2021-01-28 12:05:22 +05301396
Viresh Kumard4a4c7a2021-01-29 16:12:04 +05301397 ret = PTR_ERR_OR_ZERO(opp_table->clk);
Viresh Kumar2083da22022-06-10 12:02:04 +05301398 if (!ret) {
1399 opp_table->config_clks = _opp_config_clk_single;
1400 opp_table->clk_count = 1;
Viresh Kumard4a4c7a2021-01-29 16:12:04 +05301401 return opp_table;
Viresh Kumar2083da22022-06-10 12:02:04 +05301402 }
Viresh Kumar32439ac2021-01-28 12:05:22 +05301403
Viresh Kumard4a4c7a2021-01-29 16:12:04 +05301404 if (ret == -ENOENT) {
Viresh Kumar2083da22022-06-10 12:02:04 +05301405 /*
1406 * There are few platforms which don't want the OPP core to
1407 * manage device's clock settings. In such cases neither the
1408 * platform provides the clks explicitly to us, nor the DT
1409 * contains a valid clk entry. The OPP nodes in DT may still
1410 * contain "opp-hz" property though, which we need to parse and
1411 * allow the platform to find an OPP based on freq later on.
1412 *
1413 * This is a simple solution to take care of such corner cases,
1414 * i.e. make the clk_count 1, which lets us allocate space for
1415 * frequency in opp->rates and also parse the entries in DT.
1416 */
1417 opp_table->clk_count = 1;
1418
Viresh Kumar32439ac2021-01-28 12:05:22 +05301419 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
Viresh Kumard4a4c7a2021-01-29 16:12:04 +05301420 return opp_table;
Viresh Kumar32439ac2021-01-28 12:05:22 +05301421 }
1422
Viresh Kumard4a4c7a2021-01-29 16:12:04 +05301423 dev_pm_opp_put_opp_table(opp_table);
1424 dev_err_probe(dev, ret, "Couldn't find clock\n");
1425
1426 return ERR_PTR(ret);
Viresh Kumar32439ac2021-01-28 12:05:22 +05301427}
1428
Viresh Kumar27c09482020-10-27 16:53:21 +05301429/*
1430 * We need to make sure that the OPP table for a device doesn't get added twice,
1431 * if this routine gets called in parallel with the same device pointer.
1432 *
1433 * The simplest way to enforce that is to perform everything (find existing
1434 * table and if not found, create a new one) under the opp_table_lock, so only
1435 * one creator gets access to the same. But that expands the critical section
1436 * under the lock and may end up causing circular dependencies with frameworks
1437 * like debugfs, interconnect or clock framework as they may be direct or
1438 * indirect users of OPP core.
1439 *
1440 * And for that reason we have to go for a bit tricky implementation here, which
1441 * uses the opp_tables_busy flag to indicate if another creator is in the middle
1442 * of adding an OPP table and others should wait for it to finish.
1443 */
Viresh Kumar32439ac2021-01-28 12:05:22 +05301444struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
1445 bool getclk)
Viresh Kumarf067a982017-01-23 10:11:42 +05301446{
1447 struct opp_table *opp_table;
1448
Viresh Kumar27c09482020-10-27 16:53:21 +05301449again:
Viresh Kumarf067a982017-01-23 10:11:42 +05301450 mutex_lock(&opp_table_lock);
1451
Viresh Kumar5b650b32017-01-23 10:11:48 +05301452 opp_table = _find_opp_table_unlocked(dev);
1453 if (!IS_ERR(opp_table))
Viresh Kumarf067a982017-01-23 10:11:42 +05301454 goto unlock;
Viresh Kumarf067a982017-01-23 10:11:42 +05301455
Viresh Kumar27c09482020-10-27 16:53:21 +05301456 /*
1457 * The opp_tables list or an OPP table's dev_list is getting updated by
1458 * another user, wait for it to finish.
1459 */
1460 if (unlikely(opp_tables_busy)) {
1461 mutex_unlock(&opp_table_lock);
1462 cpu_relax();
1463 goto again;
1464 }
1465
1466 opp_tables_busy = true;
Viresh Kumar283d55e2018-09-07 09:01:54 +05301467 opp_table = _managed_opp(dev, index);
Viresh Kumar27c09482020-10-27 16:53:21 +05301468
1469 /* Drop the lock to reduce the size of critical section */
1470 mutex_unlock(&opp_table_lock);
1471
Viresh Kumar283d55e2018-09-07 09:01:54 +05301472 if (opp_table) {
Viresh Kumaref43f012020-10-22 12:12:27 +05301473 if (!_add_opp_dev(dev, opp_table)) {
Viresh Kumar283d55e2018-09-07 09:01:54 +05301474 dev_pm_opp_put_opp_table(opp_table);
Stephan Gerholddd461cd2020-07-27 11:30:46 +02001475 opp_table = ERR_PTR(-ENOMEM);
Viresh Kumar283d55e2018-09-07 09:01:54 +05301476 }
Viresh Kumar27c09482020-10-27 16:53:21 +05301477
1478 mutex_lock(&opp_table_lock);
1479 } else {
1480 opp_table = _allocate_opp_table(dev, index);
1481
1482 mutex_lock(&opp_table_lock);
1483 if (!IS_ERR(opp_table))
1484 list_add(&opp_table->node, &opp_tables);
Viresh Kumar283d55e2018-09-07 09:01:54 +05301485 }
1486
Viresh Kumar27c09482020-10-27 16:53:21 +05301487 opp_tables_busy = false;
Viresh Kumarf067a982017-01-23 10:11:42 +05301488
1489unlock:
1490 mutex_unlock(&opp_table_lock);
1491
Viresh Kumar32439ac2021-01-28 12:05:22 +05301492 return _update_opp_table_clk(dev, opp_table, getclk);
Viresh Kumarf067a982017-01-23 10:11:42 +05301493}
Viresh Kumareb7c87432018-09-05 16:17:14 +05301494
Viresh Kumar32439ac2021-01-28 12:05:22 +05301495static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
Viresh Kumare77dcb02020-11-06 10:37:16 +05301496{
Viresh Kumar32439ac2021-01-28 12:05:22 +05301497 return _add_opp_table_indexed(dev, 0, getclk);
Viresh Kumare77dcb02020-11-06 10:37:16 +05301498}
1499
Viresh Kumareb7c87432018-09-05 16:17:14 +05301500struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1501{
Viresh Kumare77dcb02020-11-06 10:37:16 +05301502 return _find_opp_table(dev);
Viresh Kumareb7c87432018-09-05 16:17:14 +05301503}
Viresh Kumarf067a982017-01-23 10:11:42 +05301504EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
1505
Viresh Kumarb83c1892017-01-23 10:11:45 +05301506static void _opp_table_kref_release(struct kref *kref)
Viresh Kumarf067a982017-01-23 10:11:42 +05301507{
1508 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05301509 struct opp_device *opp_dev, *temp;
Georgi Djakov6d3f9222020-05-12 15:53:21 +03001510 int i;
Viresh Kumar06441652015-07-29 16:23:04 +05301511
Viresh Kumare0df59d2020-10-22 12:26:08 +05301512 /* Drop the lock as soon as we can */
1513 list_del(&opp_table->node);
1514 mutex_unlock(&opp_table_lock);
1515
Viresh Kumar81c4d8a2021-01-20 16:16:48 +05301516 if (opp_table->current_opp)
1517 dev_pm_opp_put(opp_table->current_opp);
1518
Viresh Kumar5d6d1062018-06-07 14:50:43 +05301519 _of_clear_opp_table(opp_table);
1520
Viresh Kumar2083da22022-06-10 12:02:04 +05301521 /* Release automatically acquired single clk */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301522 if (!IS_ERR(opp_table->clk))
1523 clk_put(opp_table->clk);
Viresh Kumard54974c2016-02-09 10:30:38 +05301524
Georgi Djakov6d3f9222020-05-12 15:53:21 +03001525 if (opp_table->paths) {
1526 for (i = 0; i < opp_table->path_count; i++)
1527 icc_put(opp_table->paths[i]);
1528 kfree(opp_table->paths);
1529 }
1530
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05301531 WARN_ON(!list_empty(&opp_table->opp_list));
Viresh Kumar06441652015-07-29 16:23:04 +05301532
Viresh Kumar04bd2ea2023-06-14 12:50:20 +05301533 list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node)
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05301534 _remove_opp_dev(opp_dev, opp_table);
Viresh Kumar06441652015-07-29 16:23:04 +05301535
Viresh Kumar4f018bc2018-06-26 16:29:34 +05301536 mutex_destroy(&opp_table->genpd_virt_dev_lock);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301537 mutex_destroy(&opp_table->lock);
Viresh Kumar052c6f12017-01-23 10:11:49 +05301538 kfree(opp_table);
Viresh Kumarf067a982017-01-23 10:11:42 +05301539}
1540
1541void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
1542{
1543 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
1544 &opp_table_lock);
1545}
1546EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
1547
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301548void _opp_free(struct dev_pm_opp *opp)
Viresh Kumar969fceb2017-01-02 14:40:59 +05301549{
1550 kfree(opp);
Viresh Kumar969fceb2017-01-02 14:40:59 +05301551}
1552
Viresh Kumarcf1fac92020-11-19 11:24:32 +05301553static void _opp_kref_release(struct kref *kref)
Viresh Kumar129eec52014-11-27 08:54:06 +05301554{
Viresh Kumarcf1fac92020-11-19 11:24:32 +05301555 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1556 struct opp_table *opp_table = opp->opp_table;
1557
1558 list_del(&opp->node);
1559 mutex_unlock(&opp_table->lock);
1560
Viresh Kumar129eec52014-11-27 08:54:06 +05301561 /*
1562 * Notify the changes in the availability of the operable
1563 * frequency/voltage list.
1564 */
Viresh Kumar052c6f12017-01-23 10:11:49 +05301565 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
Liang He3466ea22022-07-18 21:36:32 +08001566 _of_clear_opp(opp_table, opp);
Viresh Kumardeaa5142015-11-11 07:59:01 +05301567 opp_debug_remove_one(opp);
Viresh Kumar052c6f12017-01-23 10:11:49 +05301568 kfree(opp);
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301569}
Viresh Kumar129eec52014-11-27 08:54:06 +05301570
Viresh Kumara88bd2a2017-11-29 15:18:36 +05301571void dev_pm_opp_get(struct dev_pm_opp *opp)
Viresh Kumar8a31d9d92017-01-23 10:11:47 +05301572{
1573 kref_get(&opp->kref);
1574}
1575
Viresh Kumar70347642017-01-23 10:11:46 +05301576void dev_pm_opp_put(struct dev_pm_opp *opp)
1577{
Viresh Kumarcf1fac92020-11-19 11:24:32 +05301578 kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
Viresh Kumar70347642017-01-23 10:11:46 +05301579}
1580EXPORT_SYMBOL_GPL(dev_pm_opp_put);
1581
Viresh Kumar129eec52014-11-27 08:54:06 +05301582/**
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301583 * dev_pm_opp_remove() - Remove an OPP from OPP table
Viresh Kumar129eec52014-11-27 08:54:06 +05301584 * @dev: device for which we do this operation
1585 * @freq: OPP to remove with matching 'freq'
1586 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301587 * This function removes an opp from the opp table.
Viresh Kumar129eec52014-11-27 08:54:06 +05301588 */
1589void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1590{
Jakob Koschel95073b72022-03-24 08:18:15 +01001591 struct dev_pm_opp *opp = NULL, *iter;
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301592 struct opp_table *opp_table;
Viresh Kumar129eec52014-11-27 08:54:06 +05301593
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301594 opp_table = _find_opp_table(dev);
1595 if (IS_ERR(opp_table))
Viresh Kumar5b650b32017-01-23 10:11:48 +05301596 return;
Viresh Kumar129eec52014-11-27 08:54:06 +05301597
Viresh Kumarf123ea72022-06-10 10:27:51 +05301598 if (!assert_single_clk(opp_table))
1599 goto put_table;
1600
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301601 mutex_lock(&opp_table->lock);
1602
Jakob Koschel95073b72022-03-24 08:18:15 +01001603 list_for_each_entry(iter, &opp_table->opp_list, node) {
Viresh Kumar2083da22022-06-10 12:02:04 +05301604 if (iter->rates[0] == freq) {
Jakob Koschel95073b72022-03-24 08:18:15 +01001605 opp = iter;
Viresh Kumar129eec52014-11-27 08:54:06 +05301606 break;
1607 }
1608 }
1609
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301610 mutex_unlock(&opp_table->lock);
1611
Jakob Koschel95073b72022-03-24 08:18:15 +01001612 if (opp) {
Viresh Kumar5b650b32017-01-23 10:11:48 +05301613 dev_pm_opp_put(opp);
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301614
1615 /* Drop the reference taken by dev_pm_opp_add() */
1616 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar5b650b32017-01-23 10:11:48 +05301617 } else {
Viresh Kumar129eec52014-11-27 08:54:06 +05301618 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1619 __func__, freq);
Viresh Kumar129eec52014-11-27 08:54:06 +05301620 }
1621
Viresh Kumarf123ea72022-06-10 10:27:51 +05301622put_table:
Viresh Kumar0ad8c622018-08-02 14:23:09 +05301623 /* Drop the reference taken by _find_opp_table() */
Viresh Kumar5b650b32017-01-23 10:11:48 +05301624 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar129eec52014-11-27 08:54:06 +05301625}
1626EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1627
Viresh Kumarcf1fac92020-11-19 11:24:32 +05301628static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1629 bool dynamic)
1630{
1631 struct dev_pm_opp *opp = NULL, *temp;
1632
1633 mutex_lock(&opp_table->lock);
1634 list_for_each_entry(temp, &opp_table->opp_list, node) {
Beata Michalska606a5d422021-03-04 15:07:34 +00001635 /*
1636 * Refcount must be dropped only once for each OPP by OPP core,
1637 * do that with help of "removed" flag.
1638 */
1639 if (!temp->removed && dynamic == temp->dynamic) {
Viresh Kumarcf1fac92020-11-19 11:24:32 +05301640 opp = temp;
1641 break;
1642 }
1643 }
1644
1645 mutex_unlock(&opp_table->lock);
1646 return opp;
1647}
1648
Beata Michalska606a5d422021-03-04 15:07:34 +00001649/*
1650 * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to
1651 * happen lock less to avoid circular dependency issues. This routine must be
1652 * called without the opp_table->lock held.
1653 */
1654static void _opp_remove_all(struct opp_table *opp_table, bool dynamic)
Viresh Kumar03758d62019-11-11 16:35:03 +05301655{
Viresh Kumarcf1fac92020-11-19 11:24:32 +05301656 struct dev_pm_opp *opp;
Viresh Kumar03758d62019-11-11 16:35:03 +05301657
Beata Michalska606a5d422021-03-04 15:07:34 +00001658 while ((opp = _opp_get_next(opp_table, dynamic))) {
1659 opp->removed = true;
1660 dev_pm_opp_put(opp);
1661
1662 /* Drop the references taken by dev_pm_opp_add() */
1663 if (dynamic)
1664 dev_pm_opp_put_opp_table(opp_table);
1665 }
1666}
1667
1668bool _opp_remove_all_static(struct opp_table *opp_table)
1669{
Viresh Kumar03758d62019-11-11 16:35:03 +05301670 mutex_lock(&opp_table->lock);
1671
Viresh Kumar922ff072020-08-31 13:03:06 +05301672 if (!opp_table->parsed_static_opps) {
Viresh Kumarcf1fac92020-11-19 11:24:32 +05301673 mutex_unlock(&opp_table->lock);
1674 return false;
Viresh Kumar922ff072020-08-31 13:03:06 +05301675 }
1676
Viresh Kumarcf1fac92020-11-19 11:24:32 +05301677 if (--opp_table->parsed_static_opps) {
1678 mutex_unlock(&opp_table->lock);
1679 return true;
Viresh Kumar03758d62019-11-11 16:35:03 +05301680 }
1681
Viresh Kumar03758d62019-11-11 16:35:03 +05301682 mutex_unlock(&opp_table->lock);
Viresh Kumar922ff072020-08-31 13:03:06 +05301683
Beata Michalska606a5d422021-03-04 15:07:34 +00001684 _opp_remove_all(opp_table, false);
Viresh Kumarcf1fac92020-11-19 11:24:32 +05301685 return true;
Viresh Kumar03758d62019-11-11 16:35:03 +05301686}
1687
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301688/**
1689 * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
1690 * @dev: device for which we do this operation
1691 *
1692 * This function removes all dynamically created OPPs from the opp table.
1693 */
1694void dev_pm_opp_remove_all_dynamic(struct device *dev)
1695{
1696 struct opp_table *opp_table;
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301697
1698 opp_table = _find_opp_table(dev);
1699 if (IS_ERR(opp_table))
1700 return;
1701
Beata Michalska606a5d422021-03-04 15:07:34 +00001702 _opp_remove_all(opp_table, true);
Viresh Kumar1690d8b2019-01-04 15:14:33 +05301703
1704 /* Drop the reference taken by _find_opp_table() */
1705 dev_pm_opp_put_opp_table(opp_table);
1706}
1707EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
1708
Viresh Kumard6134582022-06-09 15:35:36 +05301709struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301710{
1711 struct dev_pm_opp *opp;
Viresh Kumar2083da22022-06-10 12:02:04 +05301712 int supply_count, supply_size, icc_size, clk_size;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301713
Viresh Kumardfbe4672016-12-01 16:28:19 +05301714 /* Allocate space for at least one supply */
Viresh Kumard6134582022-06-09 15:35:36 +05301715 supply_count = opp_table->regulator_count > 0 ?
1716 opp_table->regulator_count : 1;
Georgi Djakov6d3f9222020-05-12 15:53:21 +03001717 supply_size = sizeof(*opp->supplies) * supply_count;
Viresh Kumar2083da22022-06-10 12:02:04 +05301718 clk_size = sizeof(*opp->rates) * opp_table->clk_count;
Viresh Kumard6134582022-06-09 15:35:36 +05301719 icc_size = sizeof(*opp->bandwidth) * opp_table->path_count;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301720
Viresh Kumardfbe4672016-12-01 16:28:19 +05301721 /* allocate new OPP node and supplies structures */
Viresh Kumar2083da22022-06-10 12:02:04 +05301722 opp = kzalloc(sizeof(*opp) + supply_size + clk_size + icc_size, GFP_KERNEL);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301723 if (!opp)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301724 return NULL;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301725
Viresh Kumar2083da22022-06-10 12:02:04 +05301726 /* Put the supplies, bw and clock at the end of the OPP structure */
Viresh Kumardfbe4672016-12-01 16:28:19 +05301727 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
Viresh Kumar2083da22022-06-10 12:02:04 +05301728
1729 opp->rates = (unsigned long *)(opp->supplies + supply_count);
1730
Georgi Djakov6d3f9222020-05-12 15:53:21 +03001731 if (icc_size)
Viresh Kumar2083da22022-06-10 12:02:04 +05301732 opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->rates + opp_table->clk_count);
1733
Viresh Kumardfbe4672016-12-01 16:28:19 +05301734 INIT_LIST_HEAD(&opp->node);
1735
Viresh Kumar23dacf62015-07-29 16:23:01 +05301736 return opp;
1737}
1738
Viresh Kumar7d34d562016-02-09 10:30:34 +05301739static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301740 struct opp_table *opp_table)
Viresh Kumar7d34d562016-02-09 10:30:34 +05301741{
Viresh Kumardfbe4672016-12-01 16:28:19 +05301742 struct regulator *reg;
1743 int i;
Viresh Kumar7d34d562016-02-09 10:30:34 +05301744
Viresh Kumar90e35772018-12-11 16:32:47 +05301745 if (!opp_table->regulators)
1746 return true;
1747
Viresh Kumardfbe4672016-12-01 16:28:19 +05301748 for (i = 0; i < opp_table->regulator_count; i++) {
1749 reg = opp_table->regulators[i];
1750
1751 if (!regulator_is_supported_voltage(reg,
1752 opp->supplies[i].u_volt_min,
1753 opp->supplies[i].u_volt_max)) {
1754 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1755 __func__, opp->supplies[i].u_volt_min,
1756 opp->supplies[i].u_volt_max);
1757 return false;
1758 }
Viresh Kumar7d34d562016-02-09 10:30:34 +05301759 }
1760
1761 return true;
1762}
1763
Viresh Kumar2083da22022-06-10 12:02:04 +05301764static int _opp_compare_rate(struct opp_table *opp_table,
1765 struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
1766{
1767 int i;
1768
1769 for (i = 0; i < opp_table->clk_count; i++) {
1770 if (opp1->rates[i] != opp2->rates[i])
1771 return opp1->rates[i] < opp2->rates[i] ? -1 : 1;
1772 }
1773
1774 /* Same rates for both OPPs */
1775 return 0;
1776}
1777
Viresh Kumar274c3e82022-07-05 11:50:47 +05301778static int _opp_compare_bw(struct opp_table *opp_table, struct dev_pm_opp *opp1,
1779 struct dev_pm_opp *opp2)
1780{
1781 int i;
1782
1783 for (i = 0; i < opp_table->path_count; i++) {
1784 if (opp1->bandwidth[i].peak != opp2->bandwidth[i].peak)
1785 return opp1->bandwidth[i].peak < opp2->bandwidth[i].peak ? -1 : 1;
1786 }
1787
1788 /* Same bw for both OPPs */
1789 return 0;
1790}
1791
Viresh Kumar8bdac142022-06-10 10:32:50 +05301792/*
1793 * Returns
1794 * 0: opp1 == opp2
1795 * 1: opp1 > opp2
1796 * -1: opp1 < opp2
1797 */
Viresh Kumar2083da22022-06-10 12:02:04 +05301798int _opp_compare_key(struct opp_table *opp_table, struct dev_pm_opp *opp1,
1799 struct dev_pm_opp *opp2)
Saravana Kannan6c591ee2020-05-12 15:53:19 +03001800{
Viresh Kumar2083da22022-06-10 12:02:04 +05301801 int ret;
1802
1803 ret = _opp_compare_rate(opp_table, opp1, opp2);
1804 if (ret)
1805 return ret;
1806
Viresh Kumar274c3e82022-07-05 11:50:47 +05301807 ret = _opp_compare_bw(opp_table, opp1, opp2);
1808 if (ret)
1809 return ret;
Viresh Kumar2083da22022-06-10 12:02:04 +05301810
Saravana Kannan6c591ee2020-05-12 15:53:19 +03001811 if (opp1->level != opp2->level)
1812 return opp1->level < opp2->level ? -1 : 1;
Viresh Kumar2083da22022-06-10 12:02:04 +05301813
1814 /* Duplicate OPPs */
Saravana Kannan6c591ee2020-05-12 15:53:19 +03001815 return 0;
1816}
1817
Viresh Kumara1e8c132018-04-06 14:35:45 +05301818static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1819 struct opp_table *opp_table,
1820 struct list_head **head)
1821{
1822 struct dev_pm_opp *opp;
Saravana Kannan6c591ee2020-05-12 15:53:19 +03001823 int opp_cmp;
Viresh Kumara1e8c132018-04-06 14:35:45 +05301824
1825 /*
1826 * Insert new OPP in order of increasing frequency and discard if
1827 * already present.
1828 *
1829 * Need to use &opp_table->opp_list in the condition part of the 'for'
1830 * loop, don't replace it with head otherwise it will become an infinite
1831 * loop.
1832 */
1833 list_for_each_entry(opp, &opp_table->opp_list, node) {
Viresh Kumar2083da22022-06-10 12:02:04 +05301834 opp_cmp = _opp_compare_key(opp_table, new_opp, opp);
Saravana Kannan6c591ee2020-05-12 15:53:19 +03001835 if (opp_cmp > 0) {
Viresh Kumara1e8c132018-04-06 14:35:45 +05301836 *head = &opp->node;
1837 continue;
1838 }
1839
Saravana Kannan6c591ee2020-05-12 15:53:19 +03001840 if (opp_cmp < 0)
Viresh Kumara1e8c132018-04-06 14:35:45 +05301841 return 0;
1842
1843 /* Duplicate OPPs */
1844 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
Viresh Kumar2083da22022-06-10 12:02:04 +05301845 __func__, opp->rates[0], opp->supplies[0].u_volt,
1846 opp->available, new_opp->rates[0],
Viresh Kumara1e8c132018-04-06 14:35:45 +05301847 new_opp->supplies[0].u_volt, new_opp->available);
1848
1849 /* Should we compare voltages for all regulators here ? */
1850 return opp->available &&
1851 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1852 }
1853
1854 return 0;
1855}
1856
Viresh Kumar7eba0c72019-11-25 13:57:58 +05301857void _required_opps_available(struct dev_pm_opp *opp, int count)
1858{
1859 int i;
1860
1861 for (i = 0; i < count; i++) {
1862 if (opp->required_opps[i]->available)
1863 continue;
1864
1865 opp->available = false;
1866 pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n",
Viresh Kumar2083da22022-06-10 12:02:04 +05301867 __func__, opp->required_opps[i]->np, opp->rates[0]);
Viresh Kumar7eba0c72019-11-25 13:57:58 +05301868 return;
1869 }
1870}
1871
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301872/*
1873 * Returns:
1874 * 0: On success. And appropriate error message for duplicate OPPs.
1875 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1876 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1877 * sure we don't print error messages unnecessarily if different parts of
1878 * kernel try to initialize the OPP table.
1879 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1880 * should be considered an error by the callers of _opp_add().
1881 */
Viresh Kumarf47b72a2016-05-05 16:20:33 +05301882int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
Viresh Kumar47689142022-06-08 11:57:24 +05301883 struct opp_table *opp_table)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301884{
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301885 struct list_head *head;
Viresh Kumardeaa5142015-11-11 07:59:01 +05301886 int ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301887
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301888 mutex_lock(&opp_table->lock);
1889 head = &opp_table->opp_list;
1890
Dmitry Osipenko32715be2021-01-18 03:55:13 +03001891 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1892 if (ret) {
1893 mutex_unlock(&opp_table->lock);
1894 return ret;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301895 }
1896
Viresh Kumar052c6f12017-01-23 10:11:49 +05301897 list_add(&new_opp->node, head);
Viresh Kumar37a73ec2017-01-23 10:11:41 +05301898 mutex_unlock(&opp_table->lock);
1899
1900 new_opp->opp_table = opp_table;
Viresh Kumar70347642017-01-23 10:11:46 +05301901 kref_init(&new_opp->kref);
Viresh Kumar23dacf62015-07-29 16:23:01 +05301902
Greg Kroah-Hartmana2dea4c2019-01-22 16:21:17 +01001903 opp_debug_create_one(new_opp, opp_table);
Viresh Kumardeaa5142015-11-11 07:59:01 +05301904
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301905 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
Viresh Kumar7d34d562016-02-09 10:30:34 +05301906 new_opp->available = false;
1907 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
Viresh Kumar2083da22022-06-10 12:02:04 +05301908 __func__, new_opp->rates[0]);
Viresh Kumar7d34d562016-02-09 10:30:34 +05301909 }
1910
Viresh Kumar7eba0c72019-11-25 13:57:58 +05301911 /* required-opps not fully initialized yet */
1912 if (lazy_linking_pending(opp_table))
1913 return 0;
Dmitry Osipenkocf659482021-01-18 03:55:14 +03001914
Viresh Kumar7eba0c72019-11-25 13:57:58 +05301915 _required_opps_available(new_opp, opp_table->required_opp_count);
Dmitry Osipenkocf659482021-01-18 03:55:14 +03001916
Viresh Kumar23dacf62015-07-29 16:23:01 +05301917 return 0;
1918}
1919
Viresh Kumar737002b2015-07-29 16:22:58 +05301920/**
Viresh Kumarb64b9c3f2015-10-15 21:42:44 +05301921 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301922 * @opp_table: OPP table
Nishanth Menone1f60b22010-10-13 00:13:10 +02001923 * @dev: device for which we do this operation
1924 * @freq: Frequency in Hz for this OPP
1925 * @u_volt: Voltage in uVolts for this OPP
1926 * @dynamic: Dynamically added OPPs.
1927 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301928 * This function adds an opp definition to the opp table and returns status.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001929 * The opp is made available by default and it can be controlled using
1930 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1931 *
Viresh Kumar8f8d37b2015-09-04 13:47:24 +05301932 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1933 * and freed by dev_pm_opp_of_remove_table.
Nishanth Menone1f60b22010-10-13 00:13:10 +02001934 *
Nishanth Menone1f60b22010-10-13 00:13:10 +02001935 * Return:
1936 * 0 On success OR
1937 * Duplicate OPPs (both freq and volt are same) and opp->available
1938 * -EEXIST Freq are same and volt are different OR
1939 * Duplicate OPPs (both freq and volt are same) and !opp->available
1940 * -ENOMEM Memory allocation failure
1941 */
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301942int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1943 unsigned long freq, long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +02001944{
Viresh Kumar23dacf62015-07-29 16:23:01 +05301945 struct dev_pm_opp *new_opp;
Viresh Kumar50f8cfb2016-02-09 10:30:37 +05301946 unsigned long tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001947 int ret;
1948
Viresh Kumarf123ea72022-06-10 10:27:51 +05301949 if (!assert_single_clk(opp_table))
1950 return -EINVAL;
1951
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301952 new_opp = _opp_allocate(opp_table);
1953 if (!new_opp)
1954 return -ENOMEM;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301955
Nishanth Menone1f60b22010-10-13 00:13:10 +02001956 /* populate the opp table */
Viresh Kumar2083da22022-06-10 12:02:04 +05301957 new_opp->rates[0] = freq;
Viresh Kumar2c2709d2016-02-16 14:17:53 +05301958 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
Viresh Kumardfbe4672016-12-01 16:28:19 +05301959 new_opp->supplies[0].u_volt = u_volt;
1960 new_opp->supplies[0].u_volt_min = u_volt - tol;
1961 new_opp->supplies[0].u_volt_max = u_volt + tol;
Nishanth Menone1f60b22010-10-13 00:13:10 +02001962 new_opp->available = true;
Viresh Kumaraa5f2f82015-07-29 16:23:00 +05301963 new_opp->dynamic = dynamic;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301964
Viresh Kumar47689142022-06-08 11:57:24 +05301965 ret = _opp_add(dev, new_opp, opp_table);
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301966 if (ret) {
1967 /* Don't return error for duplicate OPPs */
1968 if (ret == -EBUSY)
1969 ret = 0;
Viresh Kumar23dacf62015-07-29 16:23:01 +05301970 goto free_opp;
Viresh Kumar7f8538e2017-01-02 14:40:55 +05301971 }
Viresh Kumar23dacf62015-07-29 16:23:01 +05301972
Nishanth Menone1f60b22010-10-13 00:13:10 +02001973 /*
1974 * Notify the changes in the availability of the operable
1975 * frequency/voltage list.
1976 */
Viresh Kumar052c6f12017-01-23 10:11:49 +05301977 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02001978 return 0;
1979
1980free_opp:
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05301981 _opp_free(new_opp);
1982
Nishanth Menone1f60b22010-10-13 00:13:10 +02001983 return ret;
1984}
Viresh Kumar383934092014-11-25 16:04:18 +05301985
Viresh Kumar27465902015-07-29 16:23:02 +05301986/**
Viresh Kumar89f03982022-05-26 09:36:27 +05301987 * _opp_set_supported_hw() - Set supported platforms
Viresh Kumar7de36b02015-12-09 08:01:46 +05301988 * @dev: Device for which supported-hw has to be set.
1989 * @versions: Array of hierarchy of versions to match.
1990 * @count: Number of elements in the array.
1991 *
1992 * This is required only for the V2 bindings, and it enables a platform to
1993 * specify the hierarchy of versions it supports. OPP layer will then enable
1994 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1995 * property.
Viresh Kumar7de36b02015-12-09 08:01:46 +05301996 */
Viresh Kumar89f03982022-05-26 09:36:27 +05301997static int _opp_set_supported_hw(struct opp_table *opp_table,
1998 const u32 *versions, unsigned int count)
Viresh Kumar7de36b02015-12-09 08:01:46 +05301999{
Viresh Kumar25419de2018-05-22 16:38:08 +05302000 /* Another CPU that shares the OPP table has set the property ? */
2001 if (opp_table->supported_hw)
Viresh Kumar89f03982022-05-26 09:36:27 +05302002 return 0;
Viresh Kumar7de36b02015-12-09 08:01:46 +05302003
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302004 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
Viresh Kumar7de36b02015-12-09 08:01:46 +05302005 GFP_KERNEL);
Viresh Kumar89f03982022-05-26 09:36:27 +05302006 if (!opp_table->supported_hw)
2007 return -ENOMEM;
Viresh Kumar7de36b02015-12-09 08:01:46 +05302008
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302009 opp_table->supported_hw_count = count;
Viresh Kumarfa301842017-01-23 10:11:43 +05302010
Viresh Kumar89f03982022-05-26 09:36:27 +05302011 return 0;
Viresh Kumar7de36b02015-12-09 08:01:46 +05302012}
Viresh Kumar7de36b02015-12-09 08:01:46 +05302013
2014/**
Viresh Kumar89f03982022-05-26 09:36:27 +05302015 * _opp_put_supported_hw() - Releases resources blocked for supported hw
2016 * @opp_table: OPP table returned by _opp_set_supported_hw().
Viresh Kumar7de36b02015-12-09 08:01:46 +05302017 *
2018 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar89f03982022-05-26 09:36:27 +05302019 * _opp_set_supported_hw(). Until this is called, the opp_table structure
Viresh Kumar7de36b02015-12-09 08:01:46 +05302020 * will not be freed.
Viresh Kumar7de36b02015-12-09 08:01:46 +05302021 */
Viresh Kumar89f03982022-05-26 09:36:27 +05302022static void _opp_put_supported_hw(struct opp_table *opp_table)
Viresh Kumar7de36b02015-12-09 08:01:46 +05302023{
Viresh Kumar89f03982022-05-26 09:36:27 +05302024 if (opp_table->supported_hw) {
2025 kfree(opp_table->supported_hw);
2026 opp_table->supported_hw = NULL;
2027 opp_table->supported_hw_count = 0;
2028 }
Viresh Kumar7de36b02015-12-09 08:01:46 +05302029}
Yangtao Li9c4f2202021-03-14 19:33:56 +03002030
Viresh Kumar01fb4d32015-12-09 08:01:47 +05302031/**
Viresh Kumar298098e2022-05-26 09:36:27 +05302032 * _opp_set_prop_name() - Set prop-extn name
Viresh Kumara5da6442016-02-16 14:17:52 +05302033 * @dev: Device for which the prop-name has to be set.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05302034 * @name: name to postfix to properties.
2035 *
2036 * This is required only for the V2 bindings, and it enables a platform to
2037 * specify the extn to be used for certain property names. The properties to
2038 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
2039 * should postfix the property name with -<name> while looking for them.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05302040 */
Viresh Kumar298098e2022-05-26 09:36:27 +05302041static int _opp_set_prop_name(struct opp_table *opp_table, const char *name)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05302042{
Viresh Kumar878ec1a2018-05-22 16:38:08 +05302043 /* Another CPU that shares the OPP table has set the property ? */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302044 if (!opp_table->prop_name) {
Viresh Kumar298098e2022-05-26 09:36:27 +05302045 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
2046 if (!opp_table->prop_name)
2047 return -ENOMEM;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05302048 }
2049
Viresh Kumar298098e2022-05-26 09:36:27 +05302050 return 0;
Viresh Kumar01fb4d32015-12-09 08:01:47 +05302051}
Viresh Kumar01fb4d32015-12-09 08:01:47 +05302052
2053/**
Viresh Kumar298098e2022-05-26 09:36:27 +05302054 * _opp_put_prop_name() - Releases resources blocked for prop-name
2055 * @opp_table: OPP table returned by _opp_set_prop_name().
Viresh Kumar01fb4d32015-12-09 08:01:47 +05302056 *
2057 * This is required only for the V2 bindings, and is called for a matching
Viresh Kumar298098e2022-05-26 09:36:27 +05302058 * _opp_set_prop_name(). Until this is called, the opp_table structure
Viresh Kumar01fb4d32015-12-09 08:01:47 +05302059 * will not be freed.
Viresh Kumar01fb4d32015-12-09 08:01:47 +05302060 */
Viresh Kumar298098e2022-05-26 09:36:27 +05302061static void _opp_put_prop_name(struct opp_table *opp_table)
Viresh Kumar01fb4d32015-12-09 08:01:47 +05302062{
Viresh Kumar298098e2022-05-26 09:36:27 +05302063 if (opp_table->prop_name) {
2064 kfree(opp_table->prop_name);
2065 opp_table->prop_name = NULL;
2066 }
Viresh Kumar01fb4d32015-12-09 08:01:47 +05302067}
Viresh Kumar01fb4d32015-12-09 08:01:47 +05302068
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302069/**
Viresh Kumarb0ec0942022-07-04 16:36:26 +05302070 * _opp_set_regulators() - Set regulator names for the device
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302071 * @dev: Device for which regulator name is being set.
Viresh Kumardfbe4672016-12-01 16:28:19 +05302072 * @names: Array of pointers to the names of the regulator.
2073 * @count: Number of regulators.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302074 *
2075 * In order to support OPP switching, OPP layer needs to know the name of the
Viresh Kumardfbe4672016-12-01 16:28:19 +05302076 * device's regulators, as the core would be required to switch voltages as
2077 * well.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302078 *
2079 * This must be called before any OPPs are initialized for the device.
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302080 */
Viresh Kumarb0ec0942022-07-04 16:36:26 +05302081static int _opp_set_regulators(struct opp_table *opp_table, struct device *dev,
2082 const char * const names[])
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302083{
Viresh Kumar87686cc2022-07-04 16:10:39 +05302084 const char * const *temp = names;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302085 struct regulator *reg;
Viresh Kumar87686cc2022-07-04 16:10:39 +05302086 int count = 0, ret, i;
2087
2088 /* Count number of regulators */
2089 while (*temp++)
2090 count++;
2091
2092 if (!count)
Viresh Kumarb0ec0942022-07-04 16:36:26 +05302093 return -EINVAL;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302094
Viresh Kumar779b7832018-05-22 16:38:08 +05302095 /* Another CPU that shares the OPP table has set the regulators ? */
2096 if (opp_table->regulators)
Viresh Kumarb0ec0942022-07-04 16:36:26 +05302097 return 0;
Viresh Kumardfbe4672016-12-01 16:28:19 +05302098
2099 opp_table->regulators = kmalloc_array(count,
2100 sizeof(*opp_table->regulators),
2101 GFP_KERNEL);
Viresh Kumarb0ec0942022-07-04 16:36:26 +05302102 if (!opp_table->regulators)
2103 return -ENOMEM;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302104
Viresh Kumardfbe4672016-12-01 16:28:19 +05302105 for (i = 0; i < count; i++) {
2106 reg = regulator_get_optional(dev, names[i]);
2107 if (IS_ERR(reg)) {
Krzysztof Kozlowski543256d2022-04-08 13:10:52 +02002108 ret = dev_err_probe(dev, PTR_ERR(reg),
2109 "%s: no regulator (%s) found\n",
2110 __func__, names[i]);
Viresh Kumardfbe4672016-12-01 16:28:19 +05302111 goto free_regulators;
2112 }
2113
2114 opp_table->regulators[i] = reg;
2115 }
2116
2117 opp_table->regulator_count = count;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302118
Viresh Kumarc522ce82022-05-31 12:49:19 +05302119 /* Set generic config_regulators() for single regulators here */
2120 if (count == 1)
2121 opp_table->config_regulators = _opp_config_regulator_single;
2122
Viresh Kumarb0ec0942022-07-04 16:36:26 +05302123 return 0;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302124
Viresh Kumardfbe4672016-12-01 16:28:19 +05302125free_regulators:
Marek Szyprowski24957db2019-10-17 12:27:58 +02002126 while (i != 0)
2127 regulator_put(opp_table->regulators[--i]);
Viresh Kumardfbe4672016-12-01 16:28:19 +05302128
2129 kfree(opp_table->regulators);
2130 opp_table->regulators = NULL;
Viresh Kumar46f48ac2018-12-11 16:39:36 +05302131 opp_table->regulator_count = -1;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302132
Viresh Kumarb0ec0942022-07-04 16:36:26 +05302133 return ret;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302134}
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302135
2136/**
Viresh Kumarb0ec0942022-07-04 16:36:26 +05302137 * _opp_put_regulators() - Releases resources blocked for regulator
2138 * @opp_table: OPP table returned from _opp_set_regulators().
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302139 */
Viresh Kumarb0ec0942022-07-04 16:36:26 +05302140static void _opp_put_regulators(struct opp_table *opp_table)
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302141{
Viresh Kumardfbe4672016-12-01 16:28:19 +05302142 int i;
2143
Viresh Kumar779b7832018-05-22 16:38:08 +05302144 if (!opp_table->regulators)
Viresh Kumarb0ec0942022-07-04 16:36:26 +05302145 return;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302146
Viresh Kumar72f80ce2020-08-13 08:38:37 +05302147 if (opp_table->enabled) {
Kamil Konieczny8d457192019-07-19 17:05:32 +02002148 for (i = opp_table->regulator_count - 1; i >= 0; i--)
2149 regulator_disable(opp_table->regulators[i]);
Kamil Konieczny8d457192019-07-19 17:05:32 +02002150 }
2151
Marek Szyprowski24957db2019-10-17 12:27:58 +02002152 for (i = opp_table->regulator_count - 1; i >= 0; i--)
Viresh Kumardfbe4672016-12-01 16:28:19 +05302153 regulator_put(opp_table->regulators[i]);
2154
2155 kfree(opp_table->regulators);
2156 opp_table->regulators = NULL;
Viresh Kumar46f48ac2018-12-11 16:39:36 +05302157 opp_table->regulator_count = -1;
Viresh Kumar9f8ea962016-02-09 10:30:33 +05302158}
Yangtao Li32aee782021-03-14 19:33:55 +03002159
Viresh Kumar2083da22022-06-10 12:02:04 +05302160static void _put_clks(struct opp_table *opp_table, int count)
2161{
2162 int i;
2163
2164 for (i = count - 1; i >= 0; i--)
2165 clk_put(opp_table->clks[i]);
2166
2167 kfree(opp_table->clks);
2168 opp_table->clks = NULL;
2169}
2170
Viresh Kumar383934092014-11-25 16:04:18 +05302171/**
Viresh Kumar2368f572022-05-26 09:36:27 +05302172 * _opp_set_clknames() - Set clk names for the device
2173 * @dev: Device for which clk names is being set.
2174 * @names: Clk names.
Viresh Kumar829a4e82017-06-21 10:29:13 +05302175 *
Viresh Kumar2368f572022-05-26 09:36:27 +05302176 * In order to support OPP switching, OPP layer needs to get pointers to the
2177 * clocks for the device. Simple cases work fine without using this routine
2178 * (i.e. by passing connection-id as NULL), but for a device with multiple
2179 * clocks available, the OPP core needs to know the exact names of the clks to
2180 * use.
Viresh Kumar829a4e82017-06-21 10:29:13 +05302181 *
2182 * This must be called before any OPPs are initialized for the device.
2183 */
Viresh Kumar2368f572022-05-26 09:36:27 +05302184static int _opp_set_clknames(struct opp_table *opp_table, struct device *dev,
Viresh Kumar2083da22022-06-10 12:02:04 +05302185 const char * const names[],
2186 config_clks_t config_clks)
Viresh Kumar829a4e82017-06-21 10:29:13 +05302187{
Viresh Kumar2368f572022-05-26 09:36:27 +05302188 const char * const *temp = names;
Viresh Kumar2083da22022-06-10 12:02:04 +05302189 int count = 0, ret, i;
2190 struct clk *clk;
Viresh Kumar829a4e82017-06-21 10:29:13 +05302191
Viresh Kumar2368f572022-05-26 09:36:27 +05302192 /* Count number of clks */
2193 while (*temp++)
2194 count++;
Viresh Kumar829a4e82017-06-21 10:29:13 +05302195
Viresh Kumar2368f572022-05-26 09:36:27 +05302196 /*
2197 * This is a special case where we have a single clock, whose connection
2198 * id name is NULL, i.e. first two entries are NULL in the array.
2199 */
2200 if (!count && !names[1])
2201 count = 1;
2202
Viresh Kumar2083da22022-06-10 12:02:04 +05302203 /* Fail early for invalid configurations */
Viresh Kumar2f71ae12022-07-04 21:07:28 +05302204 if (!count || (!config_clks && count > 1))
Viresh Kumar2368f572022-05-26 09:36:27 +05302205 return -EINVAL;
Viresh Kumar829a4e82017-06-21 10:29:13 +05302206
Viresh Kumar0a434522022-05-25 14:47:59 +05302207 /* Another CPU that shares the OPP table has set the clkname ? */
Viresh Kumar2083da22022-06-10 12:02:04 +05302208 if (opp_table->clks)
Viresh Kumar2368f572022-05-26 09:36:27 +05302209 return 0;
Viresh Kumar0a434522022-05-25 14:47:59 +05302210
Viresh Kumar2083da22022-06-10 12:02:04 +05302211 opp_table->clks = kmalloc_array(count, sizeof(*opp_table->clks),
2212 GFP_KERNEL);
2213 if (!opp_table->clks)
2214 return -ENOMEM;
Viresh Kumar829a4e82017-06-21 10:29:13 +05302215
Viresh Kumar2083da22022-06-10 12:02:04 +05302216 /* Find clks for the device */
2217 for (i = 0; i < count; i++) {
2218 clk = clk_get(dev, names[i]);
2219 if (IS_ERR(clk)) {
2220 ret = dev_err_probe(dev, PTR_ERR(clk),
2221 "%s: Couldn't find clock with name: %s\n",
2222 __func__, names[i]);
2223 goto free_clks;
2224 }
2225
2226 opp_table->clks[i] = clk;
Viresh Kumar829a4e82017-06-21 10:29:13 +05302227 }
2228
Viresh Kumar2083da22022-06-10 12:02:04 +05302229 opp_table->clk_count = count;
Viresh Kumar2f71ae12022-07-04 21:07:28 +05302230 opp_table->config_clks = config_clks;
Viresh Kumar2083da22022-06-10 12:02:04 +05302231
2232 /* Set generic single clk set here */
2233 if (count == 1) {
Viresh Kumar2f71ae12022-07-04 21:07:28 +05302234 if (!opp_table->config_clks)
2235 opp_table->config_clks = _opp_config_clk_single;
Viresh Kumar2083da22022-06-10 12:02:04 +05302236
2237 /*
2238 * We could have just dropped the "clk" field and used "clks"
2239 * everywhere. Instead we kept the "clk" field around for
2240 * following reasons:
2241 *
2242 * - avoiding clks[0] everywhere else.
2243 * - not running single clk helpers for multiple clk usecase by
2244 * mistake.
2245 *
2246 * Since this is single-clk case, just update the clk pointer
2247 * too.
2248 */
2249 opp_table->clk = opp_table->clks[0];
Viresh Kumar2083da22022-06-10 12:02:04 +05302250 }
Viresh Kumar0a434522022-05-25 14:47:59 +05302251
Viresh Kumar2368f572022-05-26 09:36:27 +05302252 return 0;
Viresh Kumar2083da22022-06-10 12:02:04 +05302253
2254free_clks:
2255 _put_clks(opp_table, i);
2256 return ret;
Yangtao Lia74f6812021-03-14 19:33:54 +03002257}
2258
2259/**
Viresh Kumar2368f572022-05-26 09:36:27 +05302260 * _opp_put_clknames() - Releases resources blocked for clks.
2261 * @opp_table: OPP table returned from _opp_set_clknames().
Yangtao Lia74f6812021-03-14 19:33:54 +03002262 */
Viresh Kumar2368f572022-05-26 09:36:27 +05302263static void _opp_put_clknames(struct opp_table *opp_table)
Yangtao Lia74f6812021-03-14 19:33:54 +03002264{
Viresh Kumar2083da22022-06-10 12:02:04 +05302265 if (!opp_table->clks)
2266 return;
2267
2268 opp_table->config_clks = NULL;
2269 opp_table->clk = ERR_PTR(-ENODEV);
2270
2271 _put_clks(opp_table, opp_table->clk_count);
Yangtao Lia74f6812021-03-14 19:33:54 +03002272}
Yangtao Lia74f6812021-03-14 19:33:54 +03002273
Viresh Kumar829a4e82017-06-21 10:29:13 +05302274/**
Viresh Kumaraee33522022-07-04 13:45:08 +05302275 * _opp_set_config_regulators_helper() - Register custom set regulator helper.
2276 * @dev: Device for which the helper is getting registered.
2277 * @config_regulators: Custom set regulator helper.
2278 *
2279 * This is useful to support platforms with multiple regulators per device.
2280 *
2281 * This must be called before any OPPs are initialized for the device.
2282 */
2283static int _opp_set_config_regulators_helper(struct opp_table *opp_table,
2284 struct device *dev, config_regulators_t config_regulators)
2285{
2286 /* Another CPU that shares the OPP table has set the helper ? */
2287 if (!opp_table->config_regulators)
2288 opp_table->config_regulators = config_regulators;
2289
2290 return 0;
2291}
2292
2293/**
2294 * _opp_put_config_regulators_helper() - Releases resources blocked for
2295 * config_regulators helper.
2296 * @opp_table: OPP table returned from _opp_set_config_regulators_helper().
2297 *
2298 * Release resources blocked for platform specific config_regulators helper.
2299 */
2300static void _opp_put_config_regulators_helper(struct opp_table *opp_table)
2301{
2302 if (opp_table->config_regulators)
2303 opp_table->config_regulators = NULL;
2304}
2305
Viresh Kumar442e7a12022-05-26 09:36:27 +05302306static void _detach_genpd(struct opp_table *opp_table)
Viresh Kumar6319aee2019-05-08 15:19:13 +05302307{
2308 int index;
2309
Viresh Kumarcb60e962020-08-31 11:22:37 +05302310 if (!opp_table->genpd_virt_devs)
2311 return;
2312
Viresh Kumar6319aee2019-05-08 15:19:13 +05302313 for (index = 0; index < opp_table->required_opp_count; index++) {
2314 if (!opp_table->genpd_virt_devs[index])
2315 continue;
2316
2317 dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
2318 opp_table->genpd_virt_devs[index] = NULL;
2319 }
Viresh Kumarc0ab9e02019-05-08 15:43:36 +05302320
2321 kfree(opp_table->genpd_virt_devs);
2322 opp_table->genpd_virt_devs = NULL;
Viresh Kumar6319aee2019-05-08 15:19:13 +05302323}
2324
Viresh Kumar4dab1602016-12-01 16:28:21 +05302325/**
Viresh Kumar442e7a12022-05-26 09:36:27 +05302326 * _opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
Viresh Kumar6319aee2019-05-08 15:19:13 +05302327 * @dev: Consumer device for which the genpd is getting attached.
2328 * @names: Null terminated array of pointers containing names of genpd to attach.
Viresh Kumar17a8f862019-07-08 11:24:56 +05302329 * @virt_devs: Pointer to return the array of virtual devices.
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302330 *
2331 * Multiple generic power domains for a device are supported with the help of
2332 * virtual genpd devices, which are created for each consumer device - genpd
2333 * pair. These are the device structures which are attached to the power domain
2334 * and are required by the OPP core to set the performance state of the genpd.
Viresh Kumar6319aee2019-05-08 15:19:13 +05302335 * The same API also works for the case where single genpd is available and so
2336 * we don't need to support that separately.
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302337 *
2338 * This helper will normally be called by the consumer driver of the device
Viresh Kumar6319aee2019-05-08 15:19:13 +05302339 * "dev", as only that has details of the genpd names.
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302340 *
Viresh Kumar6319aee2019-05-08 15:19:13 +05302341 * This helper needs to be called once with a list of all genpd to attach.
2342 * Otherwise the original device structure will be used instead by the OPP core.
Viresh Kumarbaea35e2019-07-17 11:20:17 +05302343 *
2344 * The order of entries in the names array must match the order in which
2345 * "required-opps" are added in DT.
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302346 */
Viresh Kumar442e7a12022-05-26 09:36:27 +05302347static int _opp_attach_genpd(struct opp_table *opp_table, struct device *dev,
2348 const char * const *names, struct device ***virt_devs)
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302349{
Viresh Kumar6319aee2019-05-08 15:19:13 +05302350 struct device *virt_dev;
Viresh Kumarbaea35e2019-07-17 11:20:17 +05302351 int index = 0, ret = -EINVAL;
Dmitry Osipenko3734b9f2021-09-27 01:40:24 +03002352 const char * const *name = names;
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302353
Viresh Kumarcb60e962020-08-31 11:22:37 +05302354 if (opp_table->genpd_virt_devs)
Viresh Kumar442e7a12022-05-26 09:36:27 +05302355 return 0;
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302356
Viresh Kumar6319aee2019-05-08 15:19:13 +05302357 /*
2358 * If the genpd's OPP table isn't already initialized, parsing of the
2359 * required-opps fail for dev. We should retry this after genpd's OPP
2360 * table is added.
2361 */
Viresh Kumar442e7a12022-05-26 09:36:27 +05302362 if (!opp_table->required_opp_count)
2363 return -EPROBE_DEFER;
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302364
Viresh Kumar6319aee2019-05-08 15:19:13 +05302365 mutex_lock(&opp_table->genpd_virt_dev_lock);
2366
Viresh Kumarc0ab9e02019-05-08 15:43:36 +05302367 opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2368 sizeof(*opp_table->genpd_virt_devs),
2369 GFP_KERNEL);
2370 if (!opp_table->genpd_virt_devs)
2371 goto unlock;
2372
Viresh Kumar6319aee2019-05-08 15:19:13 +05302373 while (*name) {
Viresh Kumar6319aee2019-05-08 15:19:13 +05302374 if (index >= opp_table->required_opp_count) {
2375 dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
2376 *name, opp_table->required_opp_count, index);
2377 goto err;
2378 }
2379
Viresh Kumar6319aee2019-05-08 15:19:13 +05302380 virt_dev = dev_pm_domain_attach_by_name(dev, *name);
Tang Bin4ea94962022-05-24 20:31:51 +08002381 if (IS_ERR_OR_NULL(virt_dev)) {
2382 ret = PTR_ERR(virt_dev) ? : -ENODEV;
Viresh Kumar6319aee2019-05-08 15:19:13 +05302383 dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
2384 goto err;
2385 }
2386
2387 opp_table->genpd_virt_devs[index] = virt_dev;
Viresh Kumarbaea35e2019-07-17 11:20:17 +05302388 index++;
Viresh Kumar6319aee2019-05-08 15:19:13 +05302389 name++;
2390 }
2391
Viresh Kumar17a8f862019-07-08 11:24:56 +05302392 if (virt_devs)
2393 *virt_devs = opp_table->genpd_virt_devs;
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302394 mutex_unlock(&opp_table->genpd_virt_dev_lock);
2395
Viresh Kumar442e7a12022-05-26 09:36:27 +05302396 return 0;
Viresh Kumar6319aee2019-05-08 15:19:13 +05302397
2398err:
Viresh Kumar442e7a12022-05-26 09:36:27 +05302399 _detach_genpd(opp_table);
Viresh Kumarc0ab9e02019-05-08 15:43:36 +05302400unlock:
Viresh Kumar6319aee2019-05-08 15:19:13 +05302401 mutex_unlock(&opp_table->genpd_virt_dev_lock);
Viresh Kumar442e7a12022-05-26 09:36:27 +05302402 return ret;
Viresh Kumar6319aee2019-05-08 15:19:13 +05302403
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302404}
2405
2406/**
Viresh Kumar442e7a12022-05-26 09:36:27 +05302407 * _opp_detach_genpd() - Detach genpd(s) from the device.
2408 * @opp_table: OPP table returned by _opp_attach_genpd().
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302409 *
Viresh Kumar6319aee2019-05-08 15:19:13 +05302410 * This detaches the genpd(s), resets the virtual device pointers, and puts the
2411 * OPP table.
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302412 */
Viresh Kumar442e7a12022-05-26 09:36:27 +05302413static void _opp_detach_genpd(struct opp_table *opp_table)
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302414{
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302415 /*
2416 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
2417 * used in parallel.
2418 */
2419 mutex_lock(&opp_table->genpd_virt_dev_lock);
Viresh Kumar442e7a12022-05-26 09:36:27 +05302420 _detach_genpd(opp_table);
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302421 mutex_unlock(&opp_table->genpd_virt_dev_lock);
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302422}
Dmitry Osipenkob4b9e222021-01-18 03:55:21 +03002423
Viresh Kumar11b9b662022-05-25 15:23:16 +05302424static void _opp_clear_config(struct opp_config_data *data)
2425{
2426 if (data->flags & OPP_CONFIG_GENPD)
Viresh Kumar442e7a12022-05-26 09:36:27 +05302427 _opp_detach_genpd(data->opp_table);
Viresh Kumar11b9b662022-05-25 15:23:16 +05302428 if (data->flags & OPP_CONFIG_REGULATOR)
Viresh Kumarb0ec0942022-07-04 16:36:26 +05302429 _opp_put_regulators(data->opp_table);
Viresh Kumar11b9b662022-05-25 15:23:16 +05302430 if (data->flags & OPP_CONFIG_SUPPORTED_HW)
Viresh Kumar89f03982022-05-26 09:36:27 +05302431 _opp_put_supported_hw(data->opp_table);
Viresh Kumar1f378c62022-07-04 14:57:06 +05302432 if (data->flags & OPP_CONFIG_REGULATOR_HELPER)
Viresh Kumaraee33522022-07-04 13:45:08 +05302433 _opp_put_config_regulators_helper(data->opp_table);
Viresh Kumar11b9b662022-05-25 15:23:16 +05302434 if (data->flags & OPP_CONFIG_PROP_NAME)
Viresh Kumar298098e2022-05-26 09:36:27 +05302435 _opp_put_prop_name(data->opp_table);
Viresh Kumar11b9b662022-05-25 15:23:16 +05302436 if (data->flags & OPP_CONFIG_CLK)
Viresh Kumar2368f572022-05-26 09:36:27 +05302437 _opp_put_clknames(data->opp_table);
Viresh Kumar11b9b662022-05-25 15:23:16 +05302438
2439 dev_pm_opp_put_opp_table(data->opp_table);
2440 kfree(data);
2441}
2442
2443/**
2444 * dev_pm_opp_set_config() - Set OPP configuration for the device.
2445 * @dev: Device for which configuration is being set.
2446 * @config: OPP configuration.
2447 *
2448 * This allows all device OPP configurations to be performed at once.
2449 *
2450 * This must be called before any OPPs are initialized for the device. This may
2451 * be called multiple times for the same OPP table, for example once for each
2452 * CPU that share the same table. This must be balanced by the same number of
2453 * calls to dev_pm_opp_clear_config() in order to free the OPP table properly.
2454 *
2455 * This returns a token to the caller, which must be passed to
2456 * dev_pm_opp_clear_config() to free the resources later. The value of the
2457 * returned token will be >= 1 for success and negative for errors. The minimum
2458 * value of 1 is chosen here to make it easy for callers to manage the resource.
2459 */
2460int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
2461{
Viresh Kumar298098e2022-05-26 09:36:27 +05302462 struct opp_table *opp_table;
Viresh Kumar11b9b662022-05-25 15:23:16 +05302463 struct opp_config_data *data;
2464 unsigned int id;
2465 int ret;
2466
2467 data = kmalloc(sizeof(*data), GFP_KERNEL);
2468 if (!data)
2469 return -ENOMEM;
2470
2471 opp_table = _add_opp_table(dev, false);
2472 if (IS_ERR(opp_table)) {
2473 kfree(data);
2474 return PTR_ERR(opp_table);
2475 }
2476
2477 data->opp_table = opp_table;
2478 data->flags = 0;
2479
2480 /* This should be called before OPPs are initialized */
2481 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
2482 ret = -EBUSY;
2483 goto err;
2484 }
2485
2486 /* Configure clocks */
2487 if (config->clk_names) {
Viresh Kumar2083da22022-06-10 12:02:04 +05302488 ret = _opp_set_clknames(opp_table, dev, config->clk_names,
2489 config->config_clks);
Viresh Kumar2368f572022-05-26 09:36:27 +05302490 if (ret)
Viresh Kumar11b9b662022-05-25 15:23:16 +05302491 goto err;
Viresh Kumar11b9b662022-05-25 15:23:16 +05302492
2493 data->flags |= OPP_CONFIG_CLK;
Viresh Kumar2083da22022-06-10 12:02:04 +05302494 } else if (config->config_clks) {
2495 /* Don't allow config callback without clocks */
2496 ret = -EINVAL;
2497 goto err;
Viresh Kumar11b9b662022-05-25 15:23:16 +05302498 }
2499
2500 /* Configure property names */
2501 if (config->prop_name) {
Viresh Kumar298098e2022-05-26 09:36:27 +05302502 ret = _opp_set_prop_name(opp_table, config->prop_name);
2503 if (ret)
Viresh Kumar11b9b662022-05-25 15:23:16 +05302504 goto err;
Viresh Kumar11b9b662022-05-25 15:23:16 +05302505
2506 data->flags |= OPP_CONFIG_PROP_NAME;
2507 }
2508
Viresh Kumaraee33522022-07-04 13:45:08 +05302509 /* Configure config_regulators helper */
2510 if (config->config_regulators) {
2511 ret = _opp_set_config_regulators_helper(opp_table, dev,
2512 config->config_regulators);
2513 if (ret)
2514 goto err;
2515
2516 data->flags |= OPP_CONFIG_REGULATOR_HELPER;
2517 }
2518
Viresh Kumar11b9b662022-05-25 15:23:16 +05302519 /* Configure supported hardware */
2520 if (config->supported_hw) {
Viresh Kumar89f03982022-05-26 09:36:27 +05302521 ret = _opp_set_supported_hw(opp_table, config->supported_hw,
2522 config->supported_hw_count);
2523 if (ret)
Viresh Kumar11b9b662022-05-25 15:23:16 +05302524 goto err;
Viresh Kumar11b9b662022-05-25 15:23:16 +05302525
2526 data->flags |= OPP_CONFIG_SUPPORTED_HW;
2527 }
2528
2529 /* Configure supplies */
2530 if (config->regulator_names) {
Viresh Kumarb0ec0942022-07-04 16:36:26 +05302531 ret = _opp_set_regulators(opp_table, dev,
2532 config->regulator_names);
2533 if (ret)
Viresh Kumar11b9b662022-05-25 15:23:16 +05302534 goto err;
Viresh Kumar11b9b662022-05-25 15:23:16 +05302535
2536 data->flags |= OPP_CONFIG_REGULATOR;
2537 }
2538
2539 /* Attach genpds */
2540 if (config->genpd_names) {
Viresh Kumar442e7a12022-05-26 09:36:27 +05302541 ret = _opp_attach_genpd(opp_table, dev, config->genpd_names,
2542 config->virt_devs);
2543 if (ret)
Viresh Kumar11b9b662022-05-25 15:23:16 +05302544 goto err;
Viresh Kumar11b9b662022-05-25 15:23:16 +05302545
2546 data->flags |= OPP_CONFIG_GENPD;
2547 }
2548
2549 ret = xa_alloc(&opp_configs, &id, data, XA_LIMIT(1, INT_MAX),
2550 GFP_KERNEL);
2551 if (ret)
2552 goto err;
2553
2554 return id;
2555
2556err:
2557 _opp_clear_config(data);
2558 return ret;
2559}
2560EXPORT_SYMBOL_GPL(dev_pm_opp_set_config);
2561
2562/**
2563 * dev_pm_opp_clear_config() - Releases resources blocked for OPP configuration.
2564 * @opp_table: OPP table returned from dev_pm_opp_set_config().
2565 *
2566 * This allows all device OPP configurations to be cleared at once. This must be
2567 * called once for each call made to dev_pm_opp_set_config(), in order to free
2568 * the OPPs properly.
2569 *
2570 * Currently the first call itself ends up freeing all the OPP configurations,
2571 * while the later ones only drop the OPP table reference. This works well for
2572 * now as we would never want to use an half initialized OPP table and want to
2573 * remove the configurations together.
2574 */
2575void dev_pm_opp_clear_config(int token)
2576{
2577 struct opp_config_data *data;
2578
2579 /*
2580 * This lets the callers call this unconditionally and keep their code
2581 * simple.
2582 */
2583 if (unlikely(token <= 0))
2584 return;
2585
2586 data = xa_erase(&opp_configs, token);
2587 if (WARN_ON(!data))
2588 return;
2589
2590 _opp_clear_config(data);
2591}
2592EXPORT_SYMBOL_GPL(dev_pm_opp_clear_config);
2593
2594static void devm_pm_opp_config_release(void *token)
2595{
2596 dev_pm_opp_clear_config((unsigned long)token);
2597}
2598
2599/**
2600 * devm_pm_opp_set_config() - Set OPP configuration for the device.
2601 * @dev: Device for which configuration is being set.
2602 * @config: OPP configuration.
2603 *
2604 * This allows all device OPP configurations to be performed at once.
2605 * This is a resource-managed variant of dev_pm_opp_set_config().
2606 *
2607 * Return: 0 on success and errorno otherwise.
2608 */
2609int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
2610{
2611 int token = dev_pm_opp_set_config(dev, config);
2612
2613 if (token < 0)
2614 return token;
2615
2616 return devm_add_action_or_reset(dev, devm_pm_opp_config_release,
2617 (void *) ((unsigned long) token));
2618}
2619EXPORT_SYMBOL_GPL(devm_pm_opp_set_config);
2620
Viresh Kumar4f018bc2018-06-26 16:29:34 +05302621/**
Saravana Kannan7d8658ef2021-02-04 16:14:22 +08002622 * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
2623 * @src_table: OPP table which has @dst_table as one of its required OPP table.
2624 * @dst_table: Required OPP table of the @src_table.
2625 * @src_opp: OPP from the @src_table.
2626 *
2627 * This function returns the OPP (present in @dst_table) pointed out by the
2628 * "required-opps" property of the @src_opp (present in @src_table).
2629 *
2630 * The callers are required to call dev_pm_opp_put() for the returned OPP after
2631 * use.
2632 *
2633 * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise.
2634 */
2635struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
2636 struct opp_table *dst_table,
2637 struct dev_pm_opp *src_opp)
2638{
2639 struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
2640 int i;
2641
2642 if (!src_table || !dst_table || !src_opp ||
2643 !src_table->required_opp_tables)
2644 return ERR_PTR(-EINVAL);
2645
2646 /* required-opps not fully initialized yet */
2647 if (lazy_linking_pending(src_table))
2648 return ERR_PTR(-EBUSY);
2649
2650 for (i = 0; i < src_table->required_opp_count; i++) {
2651 if (src_table->required_opp_tables[i] == dst_table) {
2652 mutex_lock(&src_table->lock);
2653
2654 list_for_each_entry(opp, &src_table->opp_list, node) {
2655 if (opp == src_opp) {
2656 dest_opp = opp->required_opps[i];
2657 dev_pm_opp_get(dest_opp);
2658 break;
2659 }
2660 }
2661
2662 mutex_unlock(&src_table->lock);
2663 break;
2664 }
2665 }
2666
2667 if (IS_ERR(dest_opp)) {
2668 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__,
2669 src_table, dst_table);
2670 }
2671
2672 return dest_opp;
2673}
2674EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp);
2675
2676/**
Viresh Kumarc8a59102018-11-02 14:36:42 +05302677 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2678 * @src_table: OPP table which has dst_table as one of its required OPP table.
2679 * @dst_table: Required OPP table of the src_table.
2680 * @pstate: Current performance state of the src_table.
2681 *
2682 * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2683 * "required-opps" property of the OPP (present in @src_table) which has
2684 * performance state set to @pstate.
2685 *
2686 * Return: Zero or positive performance state on success, otherwise negative
2687 * value on errors.
2688 */
2689int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2690 struct opp_table *dst_table,
2691 unsigned int pstate)
2692{
2693 struct dev_pm_opp *opp;
2694 int dest_pstate = -EINVAL;
2695 int i;
2696
Viresh Kumarc8a59102018-11-02 14:36:42 +05302697 /*
2698 * Normally the src_table will have the "required_opps" property set to
2699 * point to one of the OPPs in the dst_table, but in some cases the
2700 * genpd and its master have one to one mapping of performance states
2701 * and so none of them have the "required-opps" property set. Return the
2702 * pstate of the src_table as it is in such cases.
2703 */
Dmitry Osipenkof2f4d2b2021-01-18 03:55:23 +03002704 if (!src_table || !src_table->required_opp_count)
Viresh Kumarc8a59102018-11-02 14:36:42 +05302705 return pstate;
2706
Viresh Kumar84cb7ff2023-06-14 15:57:43 +05302707 /* Both OPP tables must belong to genpds */
2708 if (unlikely(!src_table->is_genpd || !dst_table->is_genpd)) {
2709 pr_err("%s: Performance state is only valid for genpds.\n", __func__);
2710 return -EINVAL;
2711 }
2712
Viresh Kumar7eba0c72019-11-25 13:57:58 +05302713 /* required-opps not fully initialized yet */
2714 if (lazy_linking_pending(src_table))
2715 return -EBUSY;
2716
Viresh Kumarc8a59102018-11-02 14:36:42 +05302717 for (i = 0; i < src_table->required_opp_count; i++) {
2718 if (src_table->required_opp_tables[i]->np == dst_table->np)
2719 break;
2720 }
2721
2722 if (unlikely(i == src_table->required_opp_count)) {
2723 pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2724 __func__, src_table, dst_table);
2725 return -EINVAL;
2726 }
2727
2728 mutex_lock(&src_table->lock);
2729
2730 list_for_each_entry(opp, &src_table->opp_list, node) {
Viresh Kumar7c41cdc2023-06-14 15:29:32 +05302731 if (opp->level == pstate) {
2732 dest_pstate = opp->required_opps[i]->level;
Viresh Kumarc8a59102018-11-02 14:36:42 +05302733 goto unlock;
2734 }
2735 }
2736
2737 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2738 dst_table);
2739
2740unlock:
2741 mutex_unlock(&src_table->lock);
2742
2743 return dest_pstate;
2744}
2745
2746/**
Nishanth Menone1f60b22010-10-13 00:13:10 +02002747 * dev_pm_opp_add() - Add an OPP table from a table definitions
2748 * @dev: device for which we do this operation
Nishanth Menon47d43ba2013-09-19 16:03:51 -05002749 * @freq: Frequency in Hz for this OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02002750 * @u_volt: Voltage in uVolts for this OPP
2751 *
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302752 * This function adds an opp definition to the opp table and returns status.
Nishanth Menon47d43ba2013-09-19 16:03:51 -05002753 * The opp is made available by default and it can be controlled using
Nishanth Menone1f60b22010-10-13 00:13:10 +02002754 * dev_pm_opp_enable/disable functions.
2755 *
Viresh Kumara7470db2014-11-25 16:04:17 +05302756 * Return:
2757 * 0 On success OR
2758 * Duplicate OPPs (both freq and volt are same) and opp->available
2759 * -EEXIST Freq are same and volt are different OR
2760 * Duplicate OPPs (both freq and volt are same) and !opp->available
Viresh Kumar383934092014-11-25 16:04:18 +05302761 * -ENOMEM Memory allocation failure
Viresh Kumara7470db2014-11-25 16:04:17 +05302762 */
Nishanth Menone1f60b22010-10-13 00:13:10 +02002763int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
2764{
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05302765 struct opp_table *opp_table;
2766 int ret;
2767
Viresh Kumar32439ac2021-01-28 12:05:22 +05302768 opp_table = _add_opp_table(dev, true);
Stephan Gerholddd461cd2020-07-27 11:30:46 +02002769 if (IS_ERR(opp_table))
2770 return PTR_ERR(opp_table);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05302771
Viresh Kumar46f48ac2018-12-11 16:39:36 +05302772 /* Fix regulator count for dynamic OPPs */
2773 opp_table->regulator_count = 1;
2774
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05302775 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
Viresh Kumar0ad8c622018-08-02 14:23:09 +05302776 if (ret)
2777 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05302778
Viresh Kumar8cd2f6e2017-01-02 14:41:01 +05302779 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +02002780}
2781EXPORT_SYMBOL_GPL(dev_pm_opp_add);
2782
Nishanth Menone1f60b22010-10-13 00:13:10 +02002783/**
Nishanth Menon327854c2014-12-24 11:22:56 -06002784 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +02002785 * @dev: device for which we do this operation
2786 * @freq: OPP frequency to modify availability
2787 * @availability_req: availability status requested for this opp
2788 *
Viresh Kumar052c6f12017-01-23 10:11:49 +05302789 * Set the availability of an OPP, opp_{enable,disable} share a common logic
2790 * which is isolated here.
Nishanth Menone1f60b22010-10-13 00:13:10 +02002791 *
Nishanth Menon984f16c82014-12-24 11:22:57 -06002792 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07002793 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menone1f60b22010-10-13 00:13:10 +02002794 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02002795 */
Nishanth Menon327854c2014-12-24 11:22:56 -06002796static int _opp_set_availability(struct device *dev, unsigned long freq,
2797 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +02002798{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302799 struct opp_table *opp_table;
Viresh Kumara7f39872017-01-23 10:11:50 +05302800 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002801 int r = 0;
2802
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302803 /* Find the opp_table */
2804 opp_table = _find_opp_table(dev);
2805 if (IS_ERR(opp_table)) {
2806 r = PTR_ERR(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002807 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
Viresh Kumara7f39872017-01-23 10:11:50 +05302808 return r;
Nishanth Menone1f60b22010-10-13 00:13:10 +02002809 }
2810
Viresh Kumarf123ea72022-06-10 10:27:51 +05302811 if (!assert_single_clk(opp_table)) {
2812 r = -EINVAL;
2813 goto put_table;
2814 }
2815
Viresh Kumar37a73ec2017-01-23 10:11:41 +05302816 mutex_lock(&opp_table->lock);
2817
Nishanth Menone1f60b22010-10-13 00:13:10 +02002818 /* Do we have the frequency? */
Viresh Kumar2c2709d2016-02-16 14:17:53 +05302819 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
Viresh Kumar2083da22022-06-10 12:02:04 +05302820 if (tmp_opp->rates[0] == freq) {
Nishanth Menone1f60b22010-10-13 00:13:10 +02002821 opp = tmp_opp;
2822 break;
2823 }
2824 }
Viresh Kumar37a73ec2017-01-23 10:11:41 +05302825
Nishanth Menone1f60b22010-10-13 00:13:10 +02002826 if (IS_ERR(opp)) {
2827 r = PTR_ERR(opp);
2828 goto unlock;
2829 }
2830
2831 /* Is update really needed? */
2832 if (opp->available == availability_req)
2833 goto unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +02002834
Viresh Kumara7f39872017-01-23 10:11:50 +05302835 opp->available = availability_req;
Nishanth Menone1f60b22010-10-13 00:13:10 +02002836
Viresh Kumare4d8ae02017-09-21 10:44:36 -07002837 dev_pm_opp_get(opp);
2838 mutex_unlock(&opp_table->lock);
2839
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002840 /* Notify the change of the OPP availability */
2841 if (availability_req)
Viresh Kumar052c6f12017-01-23 10:11:49 +05302842 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
Viresh Kumara7f39872017-01-23 10:11:50 +05302843 opp);
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002844 else
Viresh Kumar052c6f12017-01-23 10:11:49 +05302845 blocking_notifier_call_chain(&opp_table->head,
Viresh Kumara7f39872017-01-23 10:11:50 +05302846 OPP_EVENT_DISABLE, opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002847
Viresh Kumare4d8ae02017-09-21 10:44:36 -07002848 dev_pm_opp_put(opp);
2849 goto put_table;
2850
Nishanth Menone1f60b22010-10-13 00:13:10 +02002851unlock:
Viresh Kumar5b650b32017-01-23 10:11:48 +05302852 mutex_unlock(&opp_table->lock);
Viresh Kumare4d8ae02017-09-21 10:44:36 -07002853put_table:
Viresh Kumar5b650b32017-01-23 10:11:48 +05302854 dev_pm_opp_put_opp_table(opp_table);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002855 return r;
2856}
2857
2858/**
Stephen Boyd25cb20a2019-10-16 16:57:53 +02002859 * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
2860 * @dev: device for which we do this operation
2861 * @freq: OPP frequency to adjust voltage of
2862 * @u_volt: new OPP target voltage
2863 * @u_volt_min: new OPP min voltage
2864 * @u_volt_max: new OPP max voltage
2865 *
2866 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2867 * copy operation, returns 0 if no modifcation was done OR modification was
2868 * successful.
2869 */
2870int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
2871 unsigned long u_volt, unsigned long u_volt_min,
2872 unsigned long u_volt_max)
2873
2874{
2875 struct opp_table *opp_table;
2876 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
2877 int r = 0;
2878
2879 /* Find the opp_table */
2880 opp_table = _find_opp_table(dev);
2881 if (IS_ERR(opp_table)) {
2882 r = PTR_ERR(opp_table);
2883 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
2884 return r;
2885 }
2886
Viresh Kumarf123ea72022-06-10 10:27:51 +05302887 if (!assert_single_clk(opp_table)) {
2888 r = -EINVAL;
2889 goto put_table;
2890 }
2891
Stephen Boyd25cb20a2019-10-16 16:57:53 +02002892 mutex_lock(&opp_table->lock);
2893
2894 /* Do we have the frequency? */
2895 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
Viresh Kumar2083da22022-06-10 12:02:04 +05302896 if (tmp_opp->rates[0] == freq) {
Stephen Boyd25cb20a2019-10-16 16:57:53 +02002897 opp = tmp_opp;
2898 break;
2899 }
2900 }
2901
2902 if (IS_ERR(opp)) {
2903 r = PTR_ERR(opp);
2904 goto adjust_unlock;
2905 }
2906
2907 /* Is update really needed? */
2908 if (opp->supplies->u_volt == u_volt)
2909 goto adjust_unlock;
2910
2911 opp->supplies->u_volt = u_volt;
2912 opp->supplies->u_volt_min = u_volt_min;
2913 opp->supplies->u_volt_max = u_volt_max;
2914
2915 dev_pm_opp_get(opp);
2916 mutex_unlock(&opp_table->lock);
2917
2918 /* Notify the voltage change of the OPP */
2919 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
2920 opp);
2921
2922 dev_pm_opp_put(opp);
Viresh Kumarf123ea72022-06-10 10:27:51 +05302923 goto put_table;
Stephen Boyd25cb20a2019-10-16 16:57:53 +02002924
2925adjust_unlock:
2926 mutex_unlock(&opp_table->lock);
Viresh Kumarf123ea72022-06-10 10:27:51 +05302927put_table:
Stephen Boyd25cb20a2019-10-16 16:57:53 +02002928 dev_pm_opp_put_opp_table(opp_table);
2929 return r;
2930}
Valdis Klētnieks03649152020-06-20 13:03:22 -04002931EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
Stephen Boyd25cb20a2019-10-16 16:57:53 +02002932
2933/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002934 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02002935 * @dev: device for which we do this operation
2936 * @freq: OPP frequency to enable
2937 *
2938 * Enables a provided opp. If the operation is valid, this returns 0, else the
2939 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002940 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +02002941 *
Nishanth Menon984f16c82014-12-24 11:22:57 -06002942 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07002943 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c82014-12-24 11:22:57 -06002944 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02002945 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002946int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02002947{
Nishanth Menon327854c2014-12-24 11:22:56 -06002948 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002949}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002950EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002951
2952/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002953 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +02002954 * @dev: device for which we do this operation
2955 * @freq: OPP frequency to disable
2956 *
2957 * Disables a provided opp. If the operation is valid, this returns
2958 * 0, else the corresponding error value. It is meant to be a temporary
2959 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002960 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +02002961 *
Nishanth Menon984f16c82014-12-24 11:22:57 -06002962 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Stephen Boyde1a2d492015-09-24 12:28:44 -07002963 * copy operation, returns 0 if no modification was done OR modification was
Nishanth Menon984f16c82014-12-24 11:22:57 -06002964 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +02002965 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002966int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +02002967{
Nishanth Menon327854c2014-12-24 11:22:56 -06002968 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002969}
Nishanth Menon5d4879c2013-09-19 16:03:50 -05002970EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +02002971
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002972/**
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302973 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
2974 * @dev: Device for which notifier needs to be registered
2975 * @nb: Notifier block to be registered
Nishanth Menon984f16c82014-12-24 11:22:57 -06002976 *
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302977 * Return: 0 on success or a negative error value.
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002978 */
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302979int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002980{
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302981 struct opp_table *opp_table;
2982 int ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002983
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302984 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05302985 if (IS_ERR(opp_table))
2986 return PTR_ERR(opp_table);
2987
Viresh Kumar052c6f12017-01-23 10:11:49 +05302988 ret = blocking_notifier_chain_register(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302989
Viresh Kumar5b650b32017-01-23 10:11:48 +05302990 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302991
2992 return ret;
MyungJoo Ham03ca3702011-09-30 22:35:12 +02002993}
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05302994EXPORT_SYMBOL(dev_pm_opp_register_notifier);
2995
2996/**
2997 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
2998 * @dev: Device for which notifier needs to be unregistered
2999 * @nb: Notifier block to be unregistered
3000 *
3001 * Return: 0 on success or a negative error value.
3002 */
3003int dev_pm_opp_unregister_notifier(struct device *dev,
3004 struct notifier_block *nb)
3005{
3006 struct opp_table *opp_table;
3007 int ret;
3008
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05303009 opp_table = _find_opp_table(dev);
Viresh Kumar5b650b32017-01-23 10:11:48 +05303010 if (IS_ERR(opp_table))
3011 return PTR_ERR(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05303012
Viresh Kumar052c6f12017-01-23 10:11:49 +05303013 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05303014
Viresh Kumar5b650b32017-01-23 10:11:48 +05303015 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumardc2c9ad2017-01-02 14:41:03 +05303016
3017 return ret;
3018}
3019EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +02003020
Viresh Kumar8aaf6262020-08-20 13:18:23 +05303021/**
3022 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
3023 * @dev: device pointer used to lookup OPP table.
3024 *
3025 * Free both OPPs created using static entries present in DT and the
3026 * dynamically added entries.
3027 */
3028void dev_pm_opp_remove_table(struct device *dev)
Viresh Kumar737002b2015-07-29 16:22:58 +05303029{
Viresh Kumar2c2709d2016-02-16 14:17:53 +05303030 struct opp_table *opp_table;
Viresh Kumar737002b2015-07-29 16:22:58 +05303031
Viresh Kumar2c2709d2016-02-16 14:17:53 +05303032 /* Check for existing table for 'dev' */
3033 opp_table = _find_opp_table(dev);
3034 if (IS_ERR(opp_table)) {
3035 int error = PTR_ERR(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05303036
3037 if (error != -ENODEV)
Viresh Kumar2c2709d2016-02-16 14:17:53 +05303038 WARN(1, "%s: opp_table: %d\n",
Viresh Kumar737002b2015-07-29 16:22:58 +05303039 IS_ERR_OR_NULL(dev) ?
3040 "Invalid device" : dev_name(dev),
3041 error);
Viresh Kumar5b650b32017-01-23 10:11:48 +05303042 return;
Viresh Kumar737002b2015-07-29 16:22:58 +05303043 }
3044
Viresh Kumar922ff072020-08-31 13:03:06 +05303045 /*
3046 * Drop the extra reference only if the OPP table was successfully added
3047 * with dev_pm_opp_of_add_table() earlier.
3048 **/
3049 if (_opp_remove_all_static(opp_table))
3050 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05303051
Viresh Kumarcdd6ed92018-09-12 12:35:19 +05303052 /* Drop reference taken by _find_opp_table() */
3053 dev_pm_opp_put_opp_table(opp_table);
Viresh Kumar737002b2015-07-29 16:22:58 +05303054}
Sudeep Holla411466c2016-05-03 15:05:04 +01003055EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
Dmitry Osipenkoce8073d2021-01-21 01:26:47 +03003056
3057/**
3058 * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
3059 * @dev: device for which we do this operation
3060 *
3061 * Sync voltage state of the OPP table regulators.
3062 *
3063 * Return: 0 on success or a negative error value.
3064 */
3065int dev_pm_opp_sync_regulators(struct device *dev)
3066{
3067 struct opp_table *opp_table;
3068 struct regulator *reg;
3069 int i, ret = 0;
3070
3071 /* Device may not have OPP table */
3072 opp_table = _find_opp_table(dev);
3073 if (IS_ERR(opp_table))
3074 return 0;
3075
3076 /* Regulator may not be required for the device */
3077 if (unlikely(!opp_table->regulators))
3078 goto put_table;
3079
3080 /* Nothing to sync if voltage wasn't changed */
3081 if (!opp_table->enabled)
3082 goto put_table;
3083
3084 for (i = 0; i < opp_table->regulator_count; i++) {
3085 reg = opp_table->regulators[i];
3086 ret = regulator_sync_voltage(reg);
3087 if (ret)
3088 break;
3089 }
3090put_table:
3091 /* Drop reference taken by _find_opp_table() */
3092 dev_pm_opp_put_opp_table(opp_table);
3093
3094 return ret;
3095}
3096EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);