]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/opp/core.c
devfreq: tegra30: Migrate to dev_pm_opp_set_opp()
[mirror_ubuntu-jammy-kernel.git] / drivers / opp / core.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
e1f60b29
NM
2/*
3 * Generic OPP Interface
4 *
5 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
6 * Nishanth Menon
7 * Romit Dasgupta
8 * Kevin Hilman
e1f60b29
NM
9 */
10
d6d2a528
VK
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
d54974c2 13#include <linux/clk.h>
e1f60b29
NM
14#include <linux/errno.h>
15#include <linux/err.h>
e1f60b29 16#include <linux/slab.h>
51990e82 17#include <linux/device.h>
80126ce7 18#include <linux/export.h>
009acd19 19#include <linux/pm_domain.h>
9f8ea969 20#include <linux/regulator/consumer.h>
e1f60b29 21
f59d3ee8 22#include "opp.h"
e1f60b29
NM
23
24/*
2c2709dc
VK
25 * The root of the list of all opp-tables. All opp_table structures branch off
26 * from here, with each opp_table containing the list of opps it supports in
e1f60b29
NM
27 * various states of availability.
28 */
f47b72a1 29LIST_HEAD(opp_tables);
e1f60b29 30/* Lock to allow exclusive modification to the device and opp lists */
2c2709dc 31DEFINE_MUTEX(opp_table_lock);
27c09484
VK
32/* Flag indicating that opp_tables list is being updated at the moment */
33static bool opp_tables_busy;
e1f60b29 34
9e62edac 35static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table)
06441658 36{
2c2709dc 37 struct opp_device *opp_dev;
9e62edac 38 bool found = false;
06441658 39
9e62edac 40 mutex_lock(&opp_table->lock);
2c2709dc 41 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
9e62edac
VK
42 if (opp_dev->dev == dev) {
43 found = true;
44 break;
45 }
06441658 46
9e62edac
VK
47 mutex_unlock(&opp_table->lock);
48 return found;
06441658
VK
49}
50
6ac42397 51static struct opp_table *_find_opp_table_unlocked(struct device *dev)
5b650b38
VK
52{
53 struct opp_table *opp_table;
54
55 list_for_each_entry(opp_table, &opp_tables, node) {
9e62edac 56 if (_find_opp_dev(dev, opp_table)) {
5b650b38 57 _get_opp_table_kref(opp_table);
5b650b38
VK
58 return opp_table;
59 }
60 }
61
62 return ERR_PTR(-ENODEV);
63}
64
e1f60b29 65/**
2c2709dc
VK
66 * _find_opp_table() - find opp_table struct using device pointer
67 * @dev: device pointer used to lookup OPP table
e1f60b29 68 *
052c6f19 69 * Search OPP table for one containing matching device.
e1f60b29 70 *
2c2709dc 71 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
e1f60b29
NM
72 * -EINVAL based on type of error.
73 *
5b650b38 74 * The callers must call dev_pm_opp_put_opp_table() after the table is used.
e1f60b29 75 */
2c2709dc 76struct opp_table *_find_opp_table(struct device *dev)
e1f60b29 77{
2c2709dc 78 struct opp_table *opp_table;
e1f60b29 79
50a3cb04 80 if (IS_ERR_OR_NULL(dev)) {
e1f60b29
NM
81 pr_err("%s: Invalid parameters\n", __func__);
82 return ERR_PTR(-EINVAL);
83 }
84
5b650b38
VK
85 mutex_lock(&opp_table_lock);
86 opp_table = _find_opp_table_unlocked(dev);
87 mutex_unlock(&opp_table_lock);
e1f60b29 88
5b650b38 89 return opp_table;
e1f60b29
NM
90}
91
92/**
d6d00742 93 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
e1f60b29
NM
94 * @opp: opp for which voltage has to be returned for
95 *
984f16c8 96 * Return: voltage in micro volt corresponding to the opp, else
e1f60b29
NM
97 * return 0
98 *
dfbe4678 99 * This is useful only for devices with single power supply.
e1f60b29 100 */
47d43ba7 101unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
e1f60b29 102{
052c6f19 103 if (IS_ERR_OR_NULL(opp)) {
e1f60b29 104 pr_err("%s: Invalid parameters\n", __func__);
052c6f19
VK
105 return 0;
106 }
e1f60b29 107
052c6f19 108 return opp->supplies[0].u_volt;
e1f60b29 109}
5d4879cd 110EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
e1f60b29
NM
111
112/**
5d4879cd 113 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
e1f60b29
NM
114 * @opp: opp for which frequency has to be returned for
115 *
984f16c8 116 * Return: frequency in hertz corresponding to the opp, else
e1f60b29 117 * return 0
e1f60b29 118 */
47d43ba7 119unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
e1f60b29 120{
06a8a059 121 if (IS_ERR_OR_NULL(opp)) {
e1f60b29 122 pr_err("%s: Invalid parameters\n", __func__);
052c6f19
VK
123 return 0;
124 }
e1f60b29 125
052c6f19 126 return opp->rate;
e1f60b29 127}
5d4879cd 128EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
e1f60b29 129
5b93ac54
RN
130/**
131 * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
132 * @opp: opp for which level value has to be returned for
133 *
134 * Return: level read from device tree corresponding to the opp, else
135 * return 0.
136 */
137unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
138{
139 if (IS_ERR_OR_NULL(opp) || !opp->available) {
140 pr_err("%s: Invalid parameters\n", __func__);
141 return 0;
142 }
143
144 return opp->level;
145}
146EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
147
597ff543
DO
148/**
149 * dev_pm_opp_get_required_pstate() - Gets the required performance state
150 * corresponding to an available opp
151 * @opp: opp for which performance state has to be returned for
152 * @index: index of the required opp
153 *
154 * Return: performance state read from device tree corresponding to the
155 * required opp, else return 0.
156 */
157unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
158 unsigned int index)
159{
160 if (IS_ERR_OR_NULL(opp) || !opp->available ||
161 index >= opp->opp_table->required_opp_count) {
162 pr_err("%s: Invalid parameters\n", __func__);
163 return 0;
164 }
165
166 return opp->required_opps[index]->pstate;
167}
168EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate);
169
19445b25
BZ
170/**
171 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
172 * @opp: opp for which turbo mode is being verified
173 *
174 * Turbo OPPs are not for normal use, and can be enabled (under certain
175 * conditions) for short duration of times to finish high throughput work
176 * quickly. Running on them for longer times may overheat the chip.
177 *
178 * Return: true if opp is turbo opp, else false.
19445b25
BZ
179 */
180bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
181{
052c6f19 182 if (IS_ERR_OR_NULL(opp) || !opp->available) {
19445b25
BZ
183 pr_err("%s: Invalid parameters\n", __func__);
184 return false;
185 }
186
052c6f19 187 return opp->turbo;
19445b25
BZ
188}
189EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
190
3ca9bb33
VK
191/**
192 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
193 * @dev: device for which we do this operation
194 *
195 * Return: This function returns the max clock latency in nanoseconds.
3ca9bb33
VK
196 */
197unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
198{
2c2709dc 199 struct opp_table *opp_table;
3ca9bb33
VK
200 unsigned long clock_latency_ns;
201
2c2709dc
VK
202 opp_table = _find_opp_table(dev);
203 if (IS_ERR(opp_table))
5b650b38
VK
204 return 0;
205
206 clock_latency_ns = opp_table->clock_latency_ns_max;
207
208 dev_pm_opp_put_opp_table(opp_table);
3ca9bb33 209
3ca9bb33
VK
210 return clock_latency_ns;
211}
212EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
213
655c9df9
VK
214/**
215 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
216 * @dev: device for which we do this operation
217 *
218 * Return: This function returns the max voltage latency in nanoseconds.
655c9df9
VK
219 */
220unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
221{
2c2709dc 222 struct opp_table *opp_table;
655c9df9 223 struct dev_pm_opp *opp;
478256bd 224 struct regulator *reg;
655c9df9 225 unsigned long latency_ns = 0;
dfbe4678
VK
226 int ret, i, count;
227 struct {
228 unsigned long min;
229 unsigned long max;
230 } *uV;
231
cdd3e614
VK
232 opp_table = _find_opp_table(dev);
233 if (IS_ERR(opp_table))
234 return 0;
235
dfbe4678 236 /* Regulator may not be required for the device */
90e3577b 237 if (!opp_table->regulators)
cdd3e614 238 goto put_opp_table;
dfbe4678 239
90e3577b
VK
240 count = opp_table->regulator_count;
241
dfbe4678
VK
242 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
243 if (!uV)
478256bd 244 goto put_opp_table;
655c9df9 245
052c6f19
VK
246 mutex_lock(&opp_table->lock);
247
dfbe4678
VK
248 for (i = 0; i < count; i++) {
249 uV[i].min = ~0;
250 uV[i].max = 0;
655c9df9 251
052c6f19 252 list_for_each_entry(opp, &opp_table->opp_list, node) {
dfbe4678
VK
253 if (!opp->available)
254 continue;
255
256 if (opp->supplies[i].u_volt_min < uV[i].min)
257 uV[i].min = opp->supplies[i].u_volt_min;
258 if (opp->supplies[i].u_volt_max > uV[i].max)
259 uV[i].max = opp->supplies[i].u_volt_max;
260 }
655c9df9
VK
261 }
262
052c6f19 263 mutex_unlock(&opp_table->lock);
655c9df9
VK
264
265 /*
2c2709dc 266 * The caller needs to ensure that opp_table (and hence the regulator)
655c9df9
VK
267 * isn't freed, while we are executing this routine.
268 */
8cc31116 269 for (i = 0; i < count; i++) {
478256bd 270 reg = opp_table->regulators[i];
dfbe4678
VK
271 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
272 if (ret > 0)
273 latency_ns += ret * 1000;
274 }
275
dfbe4678 276 kfree(uV);
cdd3e614
VK
277put_opp_table:
278 dev_pm_opp_put_opp_table(opp_table);
655c9df9
VK
279
280 return latency_ns;
281}
282EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
283
21743447
VK
284/**
285 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
286 * nanoseconds
287 * @dev: device for which we do this operation
288 *
289 * Return: This function returns the max transition latency, in nanoseconds, to
290 * switch from one OPP to other.
21743447
VK
291 */
292unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
293{
294 return dev_pm_opp_get_max_volt_latency(dev) +
295 dev_pm_opp_get_max_clock_latency(dev);
296}
297EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
298
4eafbd15 299/**
3aa26a3b 300 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
4eafbd15
BZ
301 * @dev: device for which we do this operation
302 *
3aa26a3b
VK
303 * Return: This function returns the frequency of the OPP marked as suspend_opp
304 * if one is available, else returns 0;
4eafbd15 305 */
3aa26a3b 306unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
4eafbd15 307{
2c2709dc 308 struct opp_table *opp_table;
3aa26a3b 309 unsigned long freq = 0;
4eafbd15 310
2c2709dc 311 opp_table = _find_opp_table(dev);
5b650b38
VK
312 if (IS_ERR(opp_table))
313 return 0;
3aa26a3b 314
5b650b38
VK
315 if (opp_table->suspend_opp && opp_table->suspend_opp->available)
316 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
317
318 dev_pm_opp_put_opp_table(opp_table);
4eafbd15 319
3aa26a3b 320 return freq;
4eafbd15 321}
3aa26a3b 322EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
4eafbd15 323
a1e8c136
VK
324int _get_opp_count(struct opp_table *opp_table)
325{
326 struct dev_pm_opp *opp;
327 int count = 0;
328
329 mutex_lock(&opp_table->lock);
330
331 list_for_each_entry(opp, &opp_table->opp_list, node) {
332 if (opp->available)
333 count++;
334 }
335
336 mutex_unlock(&opp_table->lock);
337
338 return count;
339}
340
e1f60b29 341/**
2c2709dc 342 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
e1f60b29
NM
343 * @dev: device for which we do this operation
344 *
984f16c8 345 * Return: This function returns the number of available opps if there are any,
e1f60b29 346 * else returns 0 if none or the corresponding error value.
e1f60b29 347 */
5d4879cd 348int dev_pm_opp_get_opp_count(struct device *dev)
e1f60b29 349{
2c2709dc 350 struct opp_table *opp_table;
a1e8c136 351 int count;
e1f60b29 352
2c2709dc
VK
353 opp_table = _find_opp_table(dev);
354 if (IS_ERR(opp_table)) {
355 count = PTR_ERR(opp_table);
035ed072 356 dev_dbg(dev, "%s: OPP table not found (%d)\n",
b4718c02 357 __func__, count);
09f662f9 358 return count;
e1f60b29
NM
359 }
360
a1e8c136 361 count = _get_opp_count(opp_table);
5b650b38
VK
362 dev_pm_opp_put_opp_table(opp_table);
363
e1f60b29
NM
364 return count;
365}
5d4879cd 366EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
e1f60b29
NM
367
368/**
5d4879cd 369 * dev_pm_opp_find_freq_exact() - search for an exact frequency
e1f60b29
NM
370 * @dev: device for which we do this operation
371 * @freq: frequency to search for
7ae49618 372 * @available: true/false - match for available opp
e1f60b29 373 *
2c2709dc 374 * Return: Searches for exact match in the opp table and returns pointer to the
984f16c8
NM
375 * matching opp if found, else returns ERR_PTR in case of error and should
376 * be handled using IS_ERR. Error return values can be:
0779726c
NM
377 * EINVAL: for bad pointer
378 * ERANGE: no match found for search
379 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
380 *
381 * Note: available is a modifier for the search. if available=true, then the
382 * match is for exact matching frequency and is available in the stored OPP
383 * table. if false, the match is for exact frequency which is not available.
384 *
385 * This provides a mechanism to enable an opp which is not available currently
386 * or the opposite as well.
387 *
8a31d9d9
VK
388 * The callers are required to call dev_pm_opp_put() for the returned OPP after
389 * use.
e1f60b29 390 */
47d43ba7
NM
391struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
392 unsigned long freq,
393 bool available)
e1f60b29 394{
2c2709dc 395 struct opp_table *opp_table;
47d43ba7 396 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 397
2c2709dc
VK
398 opp_table = _find_opp_table(dev);
399 if (IS_ERR(opp_table)) {
400 int r = PTR_ERR(opp_table);
401
402 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
e1f60b29
NM
403 return ERR_PTR(r);
404 }
405
052c6f19 406 mutex_lock(&opp_table->lock);
5b650b38 407
052c6f19 408 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
409 if (temp_opp->available == available &&
410 temp_opp->rate == freq) {
411 opp = temp_opp;
8a31d9d9
VK
412
413 /* Increment the reference count of OPP */
414 dev_pm_opp_get(opp);
e1f60b29
NM
415 break;
416 }
417 }
418
052c6f19 419 mutex_unlock(&opp_table->lock);
5b650b38 420 dev_pm_opp_put_opp_table(opp_table);
8a31d9d9 421
e1f60b29
NM
422 return opp;
423}
5d4879cd 424EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
e1f60b29 425
71419d84
NC
426/**
427 * dev_pm_opp_find_level_exact() - search for an exact level
428 * @dev: device for which we do this operation
429 * @level: level to search for
430 *
431 * Return: Searches for exact match in the opp table and returns pointer to the
432 * matching opp if found, else returns ERR_PTR in case of error and should
433 * be handled using IS_ERR. Error return values can be:
434 * EINVAL: for bad pointer
435 * ERANGE: no match found for search
436 * ENODEV: if device not found in list of registered devices
437 *
438 * The callers are required to call dev_pm_opp_put() for the returned OPP after
439 * use.
440 */
441struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
442 unsigned int level)
443{
444 struct opp_table *opp_table;
445 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
446
447 opp_table = _find_opp_table(dev);
448 if (IS_ERR(opp_table)) {
449 int r = PTR_ERR(opp_table);
450
451 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
452 return ERR_PTR(r);
453 }
454
455 mutex_lock(&opp_table->lock);
456
457 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
458 if (temp_opp->level == level) {
459 opp = temp_opp;
460
461 /* Increment the reference count of OPP */
462 dev_pm_opp_get(opp);
463 break;
464 }
465 }
466
467 mutex_unlock(&opp_table->lock);
468 dev_pm_opp_put_opp_table(opp_table);
469
470 return opp;
471}
472EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
473
8dd5cada
DO
474/**
475 * dev_pm_opp_find_level_ceil() - search for an rounded up level
476 * @dev: device for which we do this operation
477 * @level: level to search for
478 *
479 * Return: Searches for rounded up match in the opp table and returns pointer
480 * to the matching opp if found, else returns ERR_PTR in case of error and
481 * should be handled using IS_ERR. Error return values can be:
482 * EINVAL: for bad pointer
483 * ERANGE: no match found for search
484 * ENODEV: if device not found in list of registered devices
485 *
486 * The callers are required to call dev_pm_opp_put() for the returned OPP after
487 * use.
488 */
489struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
490 unsigned int *level)
491{
492 struct opp_table *opp_table;
493 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
494
495 opp_table = _find_opp_table(dev);
496 if (IS_ERR(opp_table)) {
497 int r = PTR_ERR(opp_table);
498
499 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
500 return ERR_PTR(r);
501 }
502
503 mutex_lock(&opp_table->lock);
504
505 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
506 if (temp_opp->available && temp_opp->level >= *level) {
507 opp = temp_opp;
508 *level = opp->level;
509
510 /* Increment the reference count of OPP */
511 dev_pm_opp_get(opp);
512 break;
513 }
514 }
515
516 mutex_unlock(&opp_table->lock);
517 dev_pm_opp_put_opp_table(opp_table);
518
519 return opp;
520}
521EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
522
067b7ce0
JZ
523static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
524 unsigned long *freq)
525{
526 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
527
052c6f19
VK
528 mutex_lock(&opp_table->lock);
529
530 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
067b7ce0
JZ
531 if (temp_opp->available && temp_opp->rate >= *freq) {
532 opp = temp_opp;
533 *freq = opp->rate;
8a31d9d9
VK
534
535 /* Increment the reference count of OPP */
536 dev_pm_opp_get(opp);
067b7ce0
JZ
537 break;
538 }
539 }
540
052c6f19
VK
541 mutex_unlock(&opp_table->lock);
542
067b7ce0
JZ
543 return opp;
544}
545
e1f60b29 546/**
5d4879cd 547 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
e1f60b29
NM
548 * @dev: device for which we do this operation
549 * @freq: Start frequency
550 *
551 * Search for the matching ceil *available* OPP from a starting freq
552 * for a device.
553 *
984f16c8 554 * Return: matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
555 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
556 * values can be:
557 * EINVAL: for bad pointer
558 * ERANGE: no match found for search
559 * ENODEV: if device not found in list of registered devices
e1f60b29 560 *
8a31d9d9
VK
561 * The callers are required to call dev_pm_opp_put() for the returned OPP after
562 * use.
e1f60b29 563 */
47d43ba7
NM
564struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
565 unsigned long *freq)
e1f60b29 566{
2c2709dc 567 struct opp_table *opp_table;
8a31d9d9 568 struct dev_pm_opp *opp;
b02ded24 569
e1f60b29
NM
570 if (!dev || !freq) {
571 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
572 return ERR_PTR(-EINVAL);
573 }
574
2c2709dc 575 opp_table = _find_opp_table(dev);
5b650b38 576 if (IS_ERR(opp_table))
2c2709dc 577 return ERR_CAST(opp_table);
5b650b38 578
8a31d9d9 579 opp = _find_freq_ceil(opp_table, freq);
e1f60b29 580
5b650b38 581 dev_pm_opp_put_opp_table(opp_table);
8a31d9d9
VK
582
583 return opp;
e1f60b29 584}
5d4879cd 585EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
e1f60b29
NM
586
587/**
5d4879cd 588 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
e1f60b29
NM
589 * @dev: device for which we do this operation
590 * @freq: Start frequency
591 *
592 * Search for the matching floor *available* OPP from a starting freq
593 * for a device.
594 *
984f16c8 595 * Return: matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
596 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
597 * values can be:
598 * EINVAL: for bad pointer
599 * ERANGE: no match found for search
600 * ENODEV: if device not found in list of registered devices
e1f60b29 601 *
8a31d9d9
VK
602 * The callers are required to call dev_pm_opp_put() for the returned OPP after
603 * use.
e1f60b29 604 */
47d43ba7
NM
605struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
606 unsigned long *freq)
e1f60b29 607{
2c2709dc 608 struct opp_table *opp_table;
47d43ba7 609 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29
NM
610
611 if (!dev || !freq) {
612 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
613 return ERR_PTR(-EINVAL);
614 }
615
2c2709dc 616 opp_table = _find_opp_table(dev);
5b650b38 617 if (IS_ERR(opp_table))
2c2709dc 618 return ERR_CAST(opp_table);
5b650b38 619
052c6f19 620 mutex_lock(&opp_table->lock);
e1f60b29 621
052c6f19 622 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
623 if (temp_opp->available) {
624 /* go to the next node, before choosing prev */
625 if (temp_opp->rate > *freq)
626 break;
627 else
628 opp = temp_opp;
629 }
630 }
8a31d9d9
VK
631
632 /* Increment the reference count of OPP */
633 if (!IS_ERR(opp))
634 dev_pm_opp_get(opp);
052c6f19 635 mutex_unlock(&opp_table->lock);
5b650b38 636 dev_pm_opp_put_opp_table(opp_table);
8a31d9d9 637
e1f60b29
NM
638 if (!IS_ERR(opp))
639 *freq = opp->rate;
640
641 return opp;
642}
5d4879cd 643EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
e1f60b29 644
2f36bde0
AC
645/**
646 * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for
647 * target voltage.
648 * @dev: Device for which we do this operation.
649 * @u_volt: Target voltage.
650 *
651 * Search for OPP with highest (ceil) frequency and has voltage <= u_volt.
652 *
653 * Return: matching *opp, else returns ERR_PTR in case of error which should be
654 * handled using IS_ERR.
655 *
656 * Error return values can be:
657 * EINVAL: bad parameters
658 *
659 * The callers are required to call dev_pm_opp_put() for the returned OPP after
660 * use.
661 */
662struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev,
663 unsigned long u_volt)
664{
665 struct opp_table *opp_table;
666 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
667
668 if (!dev || !u_volt) {
669 dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__,
670 u_volt);
671 return ERR_PTR(-EINVAL);
672 }
673
674 opp_table = _find_opp_table(dev);
675 if (IS_ERR(opp_table))
676 return ERR_CAST(opp_table);
677
678 mutex_lock(&opp_table->lock);
679
680 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
681 if (temp_opp->available) {
682 if (temp_opp->supplies[0].u_volt > u_volt)
683 break;
684 opp = temp_opp;
685 }
686 }
687
688 /* Increment the reference count of OPP */
689 if (!IS_ERR(opp))
690 dev_pm_opp_get(opp);
691
692 mutex_unlock(&opp_table->lock);
693 dev_pm_opp_put_opp_table(opp_table);
694
695 return opp;
696}
697EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt);
698
6a0712f6 699static int _set_opp_voltage(struct device *dev, struct regulator *reg,
ce31781a 700 struct dev_pm_opp_supply *supply)
6a0712f6
VK
701{
702 int ret;
703
704 /* Regulator not available for device */
705 if (IS_ERR(reg)) {
706 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
707 PTR_ERR(reg));
708 return 0;
709 }
710
ce31781a
VK
711 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
712 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
6a0712f6 713
ce31781a
VK
714 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
715 supply->u_volt, supply->u_volt_max);
6a0712f6
VK
716 if (ret)
717 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
ce31781a
VK
718 __func__, supply->u_volt_min, supply->u_volt,
719 supply->u_volt_max, ret);
6a0712f6
VK
720
721 return ret;
722}
723
285881b5
VK
724static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
725 unsigned long freq)
94735585
VK
726{
727 int ret;
728
35e74b2e
VK
729 /* We may reach here for devices which don't change frequency */
730 if (IS_ERR(clk))
731 return 0;
732
94735585
VK
733 ret = clk_set_rate(clk, freq);
734 if (ret) {
735 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
736 ret);
737 }
738
739 return ret;
740}
741
8d45719c 742static int _generic_set_opp_regulator(struct opp_table *opp_table,
c74b32fa 743 struct device *dev,
3f62670f 744 struct dev_pm_opp *opp,
c74b32fa 745 unsigned long freq,
3f62670f 746 int scaling_down)
94735585 747{
c74b32fa 748 struct regulator *reg = opp_table->regulators[0];
3f62670f 749 struct dev_pm_opp *old_opp = opp_table->current_opp;
94735585
VK
750 int ret;
751
752 /* This function only supports single regulator per device */
c74b32fa 753 if (WARN_ON(opp_table->regulator_count > 1)) {
94735585
VK
754 dev_err(dev, "multiple regulators are not supported\n");
755 return -EINVAL;
756 }
757
758 /* Scaling up? Scale voltage before frequency */
3f62670f
VK
759 if (!scaling_down) {
760 ret = _set_opp_voltage(dev, reg, opp->supplies);
94735585
VK
761 if (ret)
762 goto restore_voltage;
763 }
764
765 /* Change frequency */
285881b5 766 ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
94735585
VK
767 if (ret)
768 goto restore_voltage;
769
770 /* Scaling down? Scale voltage after frequency */
3f62670f
VK
771 if (scaling_down) {
772 ret = _set_opp_voltage(dev, reg, opp->supplies);
94735585
VK
773 if (ret)
774 goto restore_freq;
775 }
776
8d45719c
KK
777 /*
778 * Enable the regulator after setting its voltages, otherwise it breaks
779 * some boot-enabled regulators.
780 */
72f80ce4 781 if (unlikely(!opp_table->enabled)) {
8d45719c
KK
782 ret = regulator_enable(reg);
783 if (ret < 0)
784 dev_warn(dev, "Failed to enable regulator: %d", ret);
8d45719c
KK
785 }
786
94735585
VK
787 return 0;
788
789restore_freq:
3f62670f 790 if (_generic_set_opp_clk_only(dev, opp_table->clk, old_opp->rate))
94735585 791 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
3f62670f 792 __func__, old_opp->rate);
94735585
VK
793restore_voltage:
794 /* This shouldn't harm even if the voltages weren't updated earlier */
3f62670f 795 _set_opp_voltage(dev, reg, old_opp->supplies);
94735585
VK
796
797 return ret;
798}
799
b00e667a
VK
800static int _set_opp_bw(const struct opp_table *opp_table,
801 struct dev_pm_opp *opp, struct device *dev, bool remove)
802{
803 u32 avg, peak;
804 int i, ret;
805
806 if (!opp_table->paths)
807 return 0;
808
809 for (i = 0; i < opp_table->path_count; i++) {
810 if (remove) {
811 avg = 0;
812 peak = 0;
813 } else {
814 avg = opp->bandwidth[i].avg;
815 peak = opp->bandwidth[i].peak;
816 }
817 ret = icc_set_bw(opp_table->paths[i], avg, peak);
818 if (ret) {
819 dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
820 remove ? "remove" : "set", i, ret);
821 return ret;
822 }
823 }
824
825 return 0;
826}
827
7e535993 828static int _set_opp_custom(const struct opp_table *opp_table,
509e4777
VK
829 struct device *dev, struct dev_pm_opp *opp,
830 unsigned long freq)
7e535993 831{
04b447df 832 struct dev_pm_set_opp_data *data = opp_table->set_opp_data;
509e4777 833 struct dev_pm_opp *old_opp = opp_table->current_opp;
7e535993
VK
834 int size;
835
04b447df
DO
836 /*
837 * We support this only if dev_pm_opp_set_regulators() was called
838 * earlier.
839 */
840 if (opp_table->sod_supplies) {
509e4777
VK
841 size = sizeof(*old_opp->supplies) * opp_table->regulator_count;
842 memcpy(data->old_opp.supplies, old_opp->supplies, size);
843 memcpy(data->new_opp.supplies, opp->supplies, size);
04b447df
DO
844 data->regulator_count = opp_table->regulator_count;
845 } else {
846 data->regulator_count = 0;
847 }
848
7e535993 849 data->regulators = opp_table->regulators;
7e535993
VK
850 data->clk = opp_table->clk;
851 data->dev = dev;
509e4777 852 data->old_opp.rate = old_opp->rate;
7e535993 853 data->new_opp.rate = freq;
7e535993
VK
854
855 return opp_table->set_opp(data);
856}
857
60cdeae0
SG
858static int _set_required_opp(struct device *dev, struct device *pd_dev,
859 struct dev_pm_opp *opp, int i)
860{
861 unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0;
862 int ret;
863
864 if (!pd_dev)
865 return 0;
866
867 ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
868 if (ret) {
869 dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
870 dev_name(pd_dev), pstate, ret);
871 }
872
873 return ret;
874}
875
ca1b5d77
VK
876/* This is only called for PM domain for now */
877static int _set_required_opps(struct device *dev,
878 struct opp_table *opp_table,
2c59138c 879 struct dev_pm_opp *opp, bool up)
ca1b5d77
VK
880{
881 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
882 struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
ca1b5d77
VK
883 int i, ret = 0;
884
885 if (!required_opp_tables)
886 return 0;
887
888 /* Single genpd case */
60cdeae0
SG
889 if (!genpd_virt_devs)
890 return _set_required_opp(dev, dev, opp, 0);
ca1b5d77
VK
891
892 /* Multiple genpd case */
893
894 /*
895 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
896 * after it is freed from another thread.
897 */
898 mutex_lock(&opp_table->genpd_virt_dev_lock);
899
2c59138c
SG
900 /* Scaling up? Set required OPPs in normal order, else reverse */
901 if (up) {
902 for (i = 0; i < opp_table->required_opp_count; i++) {
903 ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
904 if (ret)
905 break;
906 }
907 } else {
908 for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
909 ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
910 if (ret)
911 break;
ca1b5d77
VK
912 }
913 }
2c59138c 914
ca1b5d77
VK
915 mutex_unlock(&opp_table->genpd_virt_dev_lock);
916
917 return ret;
918}
919
3ae1f39a
SS
920/**
921 * dev_pm_opp_set_bw() - sets bandwidth levels corresponding to an opp
922 * @dev: device for which we do this operation
923 * @opp: opp based on which the bandwidth levels are to be configured
924 *
925 * This configures the bandwidth to the levels specified by the OPP. However
926 * if the OPP specified is NULL the bandwidth levels are cleared out.
927 *
928 * Return: 0 on success or a negative error value.
929 */
930int dev_pm_opp_set_bw(struct device *dev, struct dev_pm_opp *opp)
931{
932 struct opp_table *opp_table;
933 int ret;
934
935 opp_table = _find_opp_table(dev);
936 if (IS_ERR(opp_table)) {
937 dev_err(dev, "%s: device opp table doesn't exist\n", __func__);
938 return PTR_ERR(opp_table);
939 }
940
941 if (opp)
942 ret = _set_opp_bw(opp_table, opp, dev, false);
943 else
944 ret = _set_opp_bw(opp_table, NULL, dev, true);
945
946 dev_pm_opp_put_opp_table(opp_table);
947 return ret;
948}
949EXPORT_SYMBOL_GPL(dev_pm_opp_set_bw);
950
81c4d8a3
VK
951static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
952{
953 struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
954 unsigned long freq;
955
956 if (!IS_ERR(opp_table->clk)) {
957 freq = clk_get_rate(opp_table->clk);
958 opp = _find_freq_ceil(opp_table, &freq);
959 }
960
961 /*
962 * Unable to find the current OPP ? Pick the first from the list since
963 * it is in ascending order, otherwise rest of the code will need to
964 * make special checks to validate current_opp.
965 */
966 if (IS_ERR(opp)) {
967 mutex_lock(&opp_table->lock);
968 opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
969 dev_pm_opp_get(opp);
970 mutex_unlock(&opp_table->lock);
971 }
972
973 opp_table->current_opp = opp;
974}
975
5ad58bba 976static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
f3364e17
VK
977{
978 int ret;
979
980 if (!opp_table->enabled)
981 return 0;
982
983 /*
984 * Some drivers need to support cases where some platforms may
985 * have OPP table for the device, while others don't and
986 * opp_set_rate() just needs to behave like clk_set_rate().
987 */
988 if (!_get_opp_count(opp_table))
989 return 0;
990
991 ret = _set_opp_bw(opp_table, NULL, dev, true);
992 if (ret)
993 return ret;
994
995 if (opp_table->regulators)
996 regulator_disable(opp_table->regulators[0]);
997
2c59138c 998 ret = _set_required_opps(dev, opp_table, NULL, false);
f3364e17
VK
999
1000 opp_table->enabled = false;
1001 return ret;
1002}
1003
386ba854
VK
1004static int _set_opp(struct device *dev, struct opp_table *opp_table,
1005 struct dev_pm_opp *opp, unsigned long freq)
6a0712f6 1006{
386ba854 1007 struct dev_pm_opp *old_opp;
f0b88fa4 1008 int scaling_down, ret;
6a0712f6 1009
386ba854
VK
1010 if (unlikely(!opp))
1011 return _disable_opp_table(dev, opp_table);
aca48b61 1012
81c4d8a3
VK
1013 /* Find the currently set OPP if we don't know already */
1014 if (unlikely(!opp_table->current_opp))
1015 _find_current_opp(dev, opp_table);
6a0712f6 1016
81c4d8a3 1017 old_opp = opp_table->current_opp;
81c4d8a3
VK
1018
1019 /* Return early if nothing to do */
1020 if (opp_table->enabled && old_opp == opp) {
1021 dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__);
386ba854 1022 return 0;
6a0712f6
VK
1023 }
1024
f0b88fa4 1025 dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
509e4777 1026 __func__, old_opp->rate, freq, old_opp->level, opp->level,
f0b88fa4
VK
1027 old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
1028 opp->bandwidth ? opp->bandwidth[0].peak : 0);
1029
1030 scaling_down = _opp_compare_key(old_opp, opp);
1031 if (scaling_down == -1)
1032 scaling_down = 0;
dfbe4678 1033
ca1b5d77 1034 /* Scaling up? Configure required OPPs before frequency */
f0b88fa4 1035 if (!scaling_down) {
2c59138c 1036 ret = _set_required_opps(dev, opp_table, opp, true);
ca1b5d77 1037 if (ret)
386ba854 1038 return ret;
ca1b5d77
VK
1039 }
1040
7e535993 1041 if (opp_table->set_opp) {
509e4777 1042 ret = _set_opp_custom(opp_table, dev, opp, freq);
7e535993 1043 } else if (opp_table->regulators) {
3f62670f
VK
1044 ret = _generic_set_opp_regulator(opp_table, dev, opp, freq,
1045 scaling_down);
c74b32fa 1046 } else {
7e535993 1047 /* Only frequency scaling */
1d3c42ca 1048 ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
ca1b5d77 1049 }
c74b32fa 1050
ca1b5d77 1051 /* Scaling down? Configure required OPPs after frequency */
f0b88fa4 1052 if (!ret && scaling_down) {
2c59138c 1053 ret = _set_required_opps(dev, opp_table, opp, false);
ca1b5d77
VK
1054 if (ret)
1055 dev_err(dev, "Failed to set required opps: %d\n", ret);
dfbe4678
VK
1056 }
1057
72f80ce4 1058 if (!ret) {
b00e667a 1059 ret = _set_opp_bw(opp_table, opp, dev, false);
81c4d8a3 1060 if (!ret) {
72f80ce4 1061 opp_table->enabled = true;
81c4d8a3
VK
1062 dev_pm_opp_put(old_opp);
1063
1064 /* Make sure current_opp doesn't get freed */
1065 dev_pm_opp_get(opp);
1066 opp_table->current_opp = opp;
1067 }
72f80ce4 1068 }
fe2af402 1069
386ba854
VK
1070 return ret;
1071}
1072
1073/**
1074 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1075 * @dev: device for which we do this operation
1076 * @target_freq: frequency to achieve
1077 *
1078 * This configures the power-supplies to the levels specified by the OPP
1079 * corresponding to the target_freq, and programs the clock to a value <=
1080 * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1081 * provided by the opp, should have already rounded to the target OPP's
1082 * frequency.
1083 */
1084int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1085{
1086 struct opp_table *opp_table;
1087 unsigned long freq = 0, temp_freq;
1088 struct dev_pm_opp *opp = NULL;
1089 int ret;
1090
1091 opp_table = _find_opp_table(dev);
1092 if (IS_ERR(opp_table)) {
1093 dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1094 return PTR_ERR(opp_table);
1095 }
1096
1097 if (target_freq) {
1098 /*
1099 * For IO devices which require an OPP on some platforms/SoCs
1100 * while just needing to scale the clock on some others
1101 * we look for empty OPP tables with just a clock handle and
1102 * scale only the clk. This makes dev_pm_opp_set_rate()
1103 * equivalent to a clk_set_rate()
1104 */
1105 if (!_get_opp_count(opp_table)) {
1106 ret = _generic_set_opp_clk_only(dev, opp_table->clk, target_freq);
1107 goto put_opp_table;
1108 }
1109
1110 freq = clk_round_rate(opp_table->clk, target_freq);
1111 if ((long)freq <= 0)
1112 freq = target_freq;
1113
1114 /*
1115 * The clock driver may support finer resolution of the
1116 * frequencies than the OPP table, don't update the frequency we
1117 * pass to clk_set_rate() here.
1118 */
1119 temp_freq = freq;
1120 opp = _find_freq_ceil(opp_table, &temp_freq);
1121 if (IS_ERR(opp)) {
1122 ret = PTR_ERR(opp);
1123 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1124 __func__, freq, ret);
1125 goto put_opp_table;
1126 }
1127 }
1128
1129 ret = _set_opp(dev, opp_table, opp, freq);
1130
1131 if (target_freq)
1132 dev_pm_opp_put(opp);
052c6f19 1133put_opp_table:
5b650b38 1134 dev_pm_opp_put_opp_table(opp_table);
052c6f19 1135 return ret;
6a0712f6
VK
1136}
1137EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
1138
abbe3483
VK
1139/**
1140 * dev_pm_opp_set_opp() - Configure device for OPP
1141 * @dev: device for which we do this operation
1142 * @opp: OPP to set to
1143 *
1144 * This configures the device based on the properties of the OPP passed to this
1145 * routine.
1146 *
1147 * Return: 0 on success, a negative error number otherwise.
1148 */
1149int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
1150{
1151 struct opp_table *opp_table;
1152 int ret;
1153
1154 opp_table = _find_opp_table(dev);
1155 if (IS_ERR(opp_table)) {
1156 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
1157 return PTR_ERR(opp_table);
1158 }
1159
1160 ret = _set_opp(dev, opp_table, opp, opp ? opp->rate : 0);
1161 dev_pm_opp_put_opp_table(opp_table);
1162
1163 return ret;
1164}
1165EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp);
1166
2c2709dc 1167/* OPP-dev Helpers */
2c2709dc
VK
1168static void _remove_opp_dev(struct opp_device *opp_dev,
1169 struct opp_table *opp_table)
06441658 1170{
2c2709dc
VK
1171 opp_debug_unregister(opp_dev, opp_table);
1172 list_del(&opp_dev->node);
052c6f19 1173 kfree(opp_dev);
06441658
VK
1174}
1175
ef43f01a
VK
1176struct opp_device *_add_opp_dev(const struct device *dev,
1177 struct opp_table *opp_table)
06441658 1178{
2c2709dc 1179 struct opp_device *opp_dev;
06441658 1180
2c2709dc
VK
1181 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
1182 if (!opp_dev)
06441658
VK
1183 return NULL;
1184
2c2709dc
VK
1185 /* Initialize opp-dev */
1186 opp_dev->dev = dev;
3d255699 1187
ef43f01a 1188 mutex_lock(&opp_table->lock);
052c6f19 1189 list_add(&opp_dev->node, &opp_table->dev_list);
ef43f01a 1190 mutex_unlock(&opp_table->lock);
06441658 1191
2c2709dc 1192 /* Create debugfs entries for the opp_table */
a2dea4cb 1193 opp_debug_register(opp_dev, opp_table);
283d55e6
VK
1194
1195 return opp_dev;
1196}
1197
eb7c8743 1198static struct opp_table *_allocate_opp_table(struct device *dev, int index)
07cce74a 1199{
2c2709dc
VK
1200 struct opp_table *opp_table;
1201 struct opp_device *opp_dev;
d54974c2 1202 int ret;
07cce74a
VK
1203
1204 /*
2c2709dc 1205 * Allocate a new OPP table. In the infrequent case where a new
07cce74a
VK
1206 * device is needed to be added, we pay this penalty.
1207 */
2c2709dc
VK
1208 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
1209 if (!opp_table)
dd461cd9 1210 return ERR_PTR(-ENOMEM);
07cce74a 1211
3d255699 1212 mutex_init(&opp_table->lock);
4f018bc0 1213 mutex_init(&opp_table->genpd_virt_dev_lock);
2c2709dc 1214 INIT_LIST_HEAD(&opp_table->dev_list);
06441658 1215
46f48aca
VK
1216 /* Mark regulator count uninitialized */
1217 opp_table->regulator_count = -1;
1218
2c2709dc
VK
1219 opp_dev = _add_opp_dev(dev, opp_table);
1220 if (!opp_dev) {
dd461cd9
SG
1221 ret = -ENOMEM;
1222 goto err;
06441658
VK
1223 }
1224
eb7c8743 1225 _of_init_opp_table(opp_table, dev, index);
50f8cfbd 1226
6d3f922c
GD
1227 /* Find interconnect path(s) for the device */
1228 ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
dd461cd9
SG
1229 if (ret) {
1230 if (ret == -EPROBE_DEFER)
32439ac7 1231 goto remove_opp_dev;
dd461cd9 1232
6d3f922c
GD
1233 dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
1234 __func__, ret);
dd461cd9 1235 }
6d3f922c 1236
052c6f19 1237 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
2c2709dc 1238 INIT_LIST_HEAD(&opp_table->opp_list);
f067a982 1239 kref_init(&opp_table->kref);
07cce74a 1240
2c2709dc 1241 return opp_table;
dd461cd9 1242
976509bb
QW
1243remove_opp_dev:
1244 _remove_opp_dev(opp_dev, opp_table);
dd461cd9
SG
1245err:
1246 kfree(opp_table);
1247 return ERR_PTR(ret);
07cce74a
VK
1248}
1249
f067a982 1250void _get_opp_table_kref(struct opp_table *opp_table)
b6160e26 1251{
f067a982
VK
1252 kref_get(&opp_table->kref);
1253}
1254
32439ac7
VK
1255static struct opp_table *_update_opp_table_clk(struct device *dev,
1256 struct opp_table *opp_table,
1257 bool getclk)
1258{
1259 /*
1260 * Return early if we don't need to get clk or we have already tried it
1261 * earlier.
1262 */
1263 if (!getclk || IS_ERR(opp_table) || opp_table->clk)
1264 return opp_table;
1265
1266 /* Find clk for the device */
1267 opp_table->clk = clk_get(dev, NULL);
1268 if (IS_ERR(opp_table->clk)) {
1269 int ret = PTR_ERR(opp_table->clk);
1270
1271 if (ret == -EPROBE_DEFER) {
1272 dev_pm_opp_put_opp_table(opp_table);
1273 return ERR_PTR(ret);
1274 }
1275
1276 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
1277 }
1278
1279 return opp_table;
1280}
1281
27c09484
VK
1282/*
1283 * We need to make sure that the OPP table for a device doesn't get added twice,
1284 * if this routine gets called in parallel with the same device pointer.
1285 *
1286 * The simplest way to enforce that is to perform everything (find existing
1287 * table and if not found, create a new one) under the opp_table_lock, so only
1288 * one creator gets access to the same. But that expands the critical section
1289 * under the lock and may end up causing circular dependencies with frameworks
1290 * like debugfs, interconnect or clock framework as they may be direct or
1291 * indirect users of OPP core.
1292 *
1293 * And for that reason we have to go for a bit tricky implementation here, which
1294 * uses the opp_tables_busy flag to indicate if another creator is in the middle
1295 * of adding an OPP table and others should wait for it to finish.
1296 */
32439ac7
VK
1297struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
1298 bool getclk)
f067a982
VK
1299{
1300 struct opp_table *opp_table;
1301
27c09484 1302again:
f067a982
VK
1303 mutex_lock(&opp_table_lock);
1304
5b650b38
VK
1305 opp_table = _find_opp_table_unlocked(dev);
1306 if (!IS_ERR(opp_table))
f067a982 1307 goto unlock;
f067a982 1308
27c09484
VK
1309 /*
1310 * The opp_tables list or an OPP table's dev_list is getting updated by
1311 * another user, wait for it to finish.
1312 */
1313 if (unlikely(opp_tables_busy)) {
1314 mutex_unlock(&opp_table_lock);
1315 cpu_relax();
1316 goto again;
1317 }
1318
1319 opp_tables_busy = true;
283d55e6 1320 opp_table = _managed_opp(dev, index);
27c09484
VK
1321
1322 /* Drop the lock to reduce the size of critical section */
1323 mutex_unlock(&opp_table_lock);
1324
283d55e6 1325 if (opp_table) {
ef43f01a 1326 if (!_add_opp_dev(dev, opp_table)) {
283d55e6 1327 dev_pm_opp_put_opp_table(opp_table);
dd461cd9 1328 opp_table = ERR_PTR(-ENOMEM);
283d55e6 1329 }
27c09484
VK
1330
1331 mutex_lock(&opp_table_lock);
1332 } else {
1333 opp_table = _allocate_opp_table(dev, index);
1334
1335 mutex_lock(&opp_table_lock);
1336 if (!IS_ERR(opp_table))
1337 list_add(&opp_table->node, &opp_tables);
283d55e6
VK
1338 }
1339
27c09484 1340 opp_tables_busy = false;
f067a982
VK
1341
1342unlock:
1343 mutex_unlock(&opp_table_lock);
1344
32439ac7 1345 return _update_opp_table_clk(dev, opp_table, getclk);
f067a982 1346}
eb7c8743 1347
32439ac7 1348static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
eb7c8743 1349{
32439ac7 1350 return _add_opp_table_indexed(dev, 0, getclk);
eb7c8743 1351}
f067a982 1352
e77dcb0b 1353struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
eb7c8743 1354{
e77dcb0b 1355 return _find_opp_table(dev);
eb7c8743 1356}
e77dcb0b 1357EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
eb7c8743 1358
b83c1899 1359static void _opp_table_kref_release(struct kref *kref)
f067a982
VK
1360{
1361 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
cdd6ed90 1362 struct opp_device *opp_dev, *temp;
6d3f922c 1363 int i;
b6160e26 1364
e0df59de
VK
1365 /* Drop the lock as soon as we can */
1366 list_del(&opp_table->node);
1367 mutex_unlock(&opp_table_lock);
1368
81c4d8a3
VK
1369 if (opp_table->current_opp)
1370 dev_pm_opp_put(opp_table->current_opp);
1371
5d6d106f
VK
1372 _of_clear_opp_table(opp_table);
1373
b6160e26
VK
1374 /* Release clk */
1375 if (!IS_ERR(opp_table->clk))
1376 clk_put(opp_table->clk);
1377
6d3f922c
GD
1378 if (opp_table->paths) {
1379 for (i = 0; i < opp_table->path_count; i++)
1380 icc_put(opp_table->paths[i]);
1381 kfree(opp_table->paths);
1382 }
1383
cdd6ed90 1384 WARN_ON(!list_empty(&opp_table->opp_list));
b6160e26 1385
cdd6ed90
VK
1386 list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
1387 /*
1388 * The OPP table is getting removed, drop the performance state
1389 * constraints.
1390 */
1391 if (opp_table->genpd_performance_state)
1392 dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
b6160e26 1393
cdd6ed90
VK
1394 _remove_opp_dev(opp_dev, opp_table);
1395 }
b6160e26 1396
4f018bc0 1397 mutex_destroy(&opp_table->genpd_virt_dev_lock);
37a73ec0 1398 mutex_destroy(&opp_table->lock);
052c6f19 1399 kfree(opp_table);
f067a982
VK
1400}
1401
1402void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
1403{
1404 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
1405 &opp_table_lock);
1406}
1407EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
1408
8cd2f6e8 1409void _opp_free(struct dev_pm_opp *opp)
969fceb3
VK
1410{
1411 kfree(opp);
969fceb3
VK
1412}
1413
cf1fac94 1414static void _opp_kref_release(struct kref *kref)
129eec55 1415{
cf1fac94
VK
1416 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1417 struct opp_table *opp_table = opp->opp_table;
1418
1419 list_del(&opp->node);
1420 mutex_unlock(&opp_table->lock);
1421
129eec55
VK
1422 /*
1423 * Notify the changes in the availability of the operable
1424 * frequency/voltage list.
1425 */
052c6f19 1426 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
da544b61 1427 _of_opp_free_required_opps(opp_table, opp);
deaa5146 1428 opp_debug_remove_one(opp);
052c6f19 1429 kfree(opp);
1690d8bb 1430}
129eec55 1431
a88bd2a5 1432void dev_pm_opp_get(struct dev_pm_opp *opp)
8a31d9d9
VK
1433{
1434 kref_get(&opp->kref);
1435}
1436
7034764a
VK
1437void dev_pm_opp_put(struct dev_pm_opp *opp)
1438{
cf1fac94 1439 kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
7034764a
VK
1440}
1441EXPORT_SYMBOL_GPL(dev_pm_opp_put);
1442
129eec55 1443/**
2c2709dc 1444 * dev_pm_opp_remove() - Remove an OPP from OPP table
129eec55
VK
1445 * @dev: device for which we do this operation
1446 * @freq: OPP to remove with matching 'freq'
1447 *
2c2709dc 1448 * This function removes an opp from the opp table.
129eec55
VK
1449 */
1450void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1451{
1452 struct dev_pm_opp *opp;
2c2709dc 1453 struct opp_table *opp_table;
129eec55
VK
1454 bool found = false;
1455
2c2709dc
VK
1456 opp_table = _find_opp_table(dev);
1457 if (IS_ERR(opp_table))
5b650b38 1458 return;
129eec55 1459
37a73ec0
VK
1460 mutex_lock(&opp_table->lock);
1461
2c2709dc 1462 list_for_each_entry(opp, &opp_table->opp_list, node) {
129eec55
VK
1463 if (opp->rate == freq) {
1464 found = true;
1465 break;
1466 }
1467 }
1468
37a73ec0
VK
1469 mutex_unlock(&opp_table->lock);
1470
5b650b38
VK
1471 if (found) {
1472 dev_pm_opp_put(opp);
0ad8c623
VK
1473
1474 /* Drop the reference taken by dev_pm_opp_add() */
1475 dev_pm_opp_put_opp_table(opp_table);
5b650b38 1476 } else {
129eec55
VK
1477 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1478 __func__, freq);
129eec55
VK
1479 }
1480
0ad8c623 1481 /* Drop the reference taken by _find_opp_table() */
5b650b38 1482 dev_pm_opp_put_opp_table(opp_table);
129eec55
VK
1483}
1484EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1485
cf1fac94
VK
1486static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1487 bool dynamic)
1488{
1489 struct dev_pm_opp *opp = NULL, *temp;
1490
1491 mutex_lock(&opp_table->lock);
1492 list_for_each_entry(temp, &opp_table->opp_list, node) {
1493 if (dynamic == temp->dynamic) {
1494 opp = temp;
1495 break;
1496 }
1497 }
1498
1499 mutex_unlock(&opp_table->lock);
1500 return opp;
1501}
1502
922ff075 1503bool _opp_remove_all_static(struct opp_table *opp_table)
03758d60 1504{
cf1fac94 1505 struct dev_pm_opp *opp;
03758d60
VK
1506
1507 mutex_lock(&opp_table->lock);
1508
922ff075 1509 if (!opp_table->parsed_static_opps) {
cf1fac94
VK
1510 mutex_unlock(&opp_table->lock);
1511 return false;
922ff075
VK
1512 }
1513
cf1fac94
VK
1514 if (--opp_table->parsed_static_opps) {
1515 mutex_unlock(&opp_table->lock);
1516 return true;
03758d60
VK
1517 }
1518
03758d60 1519 mutex_unlock(&opp_table->lock);
922ff075 1520
cf1fac94
VK
1521 /*
1522 * Can't remove the OPP from under the lock, debugfs removal needs to
1523 * happen lock less to avoid circular dependency issues.
1524 */
1525 while ((opp = _opp_get_next(opp_table, false)))
1526 dev_pm_opp_put(opp);
1527
1528 return true;
03758d60
VK
1529}
1530
1690d8bb
VK
1531/**
1532 * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
1533 * @dev: device for which we do this operation
1534 *
1535 * This function removes all dynamically created OPPs from the opp table.
1536 */
1537void dev_pm_opp_remove_all_dynamic(struct device *dev)
1538{
1539 struct opp_table *opp_table;
cf1fac94 1540 struct dev_pm_opp *opp;
1690d8bb
VK
1541 int count = 0;
1542
1543 opp_table = _find_opp_table(dev);
1544 if (IS_ERR(opp_table))
1545 return;
1546
cf1fac94
VK
1547 /*
1548 * Can't remove the OPP from under the lock, debugfs removal needs to
1549 * happen lock less to avoid circular dependency issues.
1550 */
1551 while ((opp = _opp_get_next(opp_table, true))) {
1552 dev_pm_opp_put(opp);
1553 count++;
1690d8bb 1554 }
1690d8bb
VK
1555
1556 /* Drop the references taken by dev_pm_opp_add() */
1557 while (count--)
1558 dev_pm_opp_put_opp_table(opp_table);
1559
1560 /* Drop the reference taken by _find_opp_table() */
1561 dev_pm_opp_put_opp_table(opp_table);
1562}
1563EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
1564
8cd2f6e8 1565struct dev_pm_opp *_opp_allocate(struct opp_table *table)
e1f60b29 1566{
23dacf6d 1567 struct dev_pm_opp *opp;
6d3f922c 1568 int supply_count, supply_size, icc_size;
e1f60b29 1569
dfbe4678 1570 /* Allocate space for at least one supply */
6d3f922c
GD
1571 supply_count = table->regulator_count > 0 ? table->regulator_count : 1;
1572 supply_size = sizeof(*opp->supplies) * supply_count;
1573 icc_size = sizeof(*opp->bandwidth) * table->path_count;
e1f60b29 1574
dfbe4678 1575 /* allocate new OPP node and supplies structures */
6d3f922c
GD
1576 opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL);
1577
8cd2f6e8 1578 if (!opp)
23dacf6d 1579 return NULL;
23dacf6d 1580
dfbe4678
VK
1581 /* Put the supplies at the end of the OPP structure as an empty array */
1582 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
6d3f922c
GD
1583 if (icc_size)
1584 opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count);
dfbe4678
VK
1585 INIT_LIST_HEAD(&opp->node);
1586
23dacf6d
VK
1587 return opp;
1588}
1589
7d34d56e 1590static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
2c2709dc 1591 struct opp_table *opp_table)
7d34d56e 1592{
dfbe4678
VK
1593 struct regulator *reg;
1594 int i;
1595
90e3577b
VK
1596 if (!opp_table->regulators)
1597 return true;
1598
dfbe4678
VK
1599 for (i = 0; i < opp_table->regulator_count; i++) {
1600 reg = opp_table->regulators[i];
1601
1602 if (!regulator_is_supported_voltage(reg,
1603 opp->supplies[i].u_volt_min,
1604 opp->supplies[i].u_volt_max)) {
1605 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1606 __func__, opp->supplies[i].u_volt_min,
1607 opp->supplies[i].u_volt_max);
1608 return false;
1609 }
7d34d56e
VK
1610 }
1611
1612 return true;
1613}
1614
6c591eec
SK
1615int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
1616{
1617 if (opp1->rate != opp2->rate)
1618 return opp1->rate < opp2->rate ? -1 : 1;
6d3f922c
GD
1619 if (opp1->bandwidth && opp2->bandwidth &&
1620 opp1->bandwidth[0].peak != opp2->bandwidth[0].peak)
1621 return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1;
6c591eec
SK
1622 if (opp1->level != opp2->level)
1623 return opp1->level < opp2->level ? -1 : 1;
1624 return 0;
1625}
1626
a1e8c136
VK
1627static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1628 struct opp_table *opp_table,
1629 struct list_head **head)
23dacf6d
VK
1630{
1631 struct dev_pm_opp *opp;
6c591eec 1632 int opp_cmp;
23dacf6d
VK
1633
1634 /*
1635 * Insert new OPP in order of increasing frequency and discard if
1636 * already present.
1637 *
2c2709dc 1638 * Need to use &opp_table->opp_list in the condition part of the 'for'
23dacf6d
VK
1639 * loop, don't replace it with head otherwise it will become an infinite
1640 * loop.
1641 */
052c6f19 1642 list_for_each_entry(opp, &opp_table->opp_list, node) {
6c591eec
SK
1643 opp_cmp = _opp_compare_key(new_opp, opp);
1644 if (opp_cmp > 0) {
a1e8c136 1645 *head = &opp->node;
23dacf6d
VK
1646 continue;
1647 }
1648
6c591eec 1649 if (opp_cmp < 0)
a1e8c136 1650 return 0;
23dacf6d
VK
1651
1652 /* Duplicate OPPs */
06441658 1653 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
dfbe4678
VK
1654 __func__, opp->rate, opp->supplies[0].u_volt,
1655 opp->available, new_opp->rate,
1656 new_opp->supplies[0].u_volt, new_opp->available);
23dacf6d 1657
dfbe4678 1658 /* Should we compare voltages for all regulators here ? */
a1e8c136
VK
1659 return opp->available &&
1660 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1661 }
1662
1663 return 0;
1664}
1665
1666/*
1667 * Returns:
1668 * 0: On success. And appropriate error message for duplicate OPPs.
1669 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1670 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1671 * sure we don't print error messages unnecessarily if different parts of
1672 * kernel try to initialize the OPP table.
1673 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1674 * should be considered an error by the callers of _opp_add().
1675 */
1676int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1677 struct opp_table *opp_table, bool rate_not_available)
1678{
1679 struct list_head *head;
cf65948d 1680 unsigned int i;
a1e8c136
VK
1681 int ret;
1682
1683 mutex_lock(&opp_table->lock);
1684 head = &opp_table->opp_list;
37a73ec0 1685
32715be4
DO
1686 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1687 if (ret) {
1688 mutex_unlock(&opp_table->lock);
1689 return ret;
23dacf6d
VK
1690 }
1691
052c6f19 1692 list_add(&new_opp->node, head);
37a73ec0
VK
1693 mutex_unlock(&opp_table->lock);
1694
1695 new_opp->opp_table = opp_table;
7034764a 1696 kref_init(&new_opp->kref);
23dacf6d 1697
a2dea4cb 1698 opp_debug_create_one(new_opp, opp_table);
deaa5146 1699
2c2709dc 1700 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
7d34d56e
VK
1701 new_opp->available = false;
1702 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1703 __func__, new_opp->rate);
1704 }
1705
cf65948d
DO
1706 for (i = 0; i < opp_table->required_opp_count; i++) {
1707 if (new_opp->required_opps[i]->available)
1708 continue;
1709
1710 new_opp->available = false;
1711 dev_warn(dev, "%s: OPP not supported by required OPP %pOF (%lu)\n",
1712 __func__, new_opp->required_opps[i]->np, new_opp->rate);
1713 break;
1714 }
1715
23dacf6d
VK
1716 return 0;
1717}
1718
984f16c8 1719/**
b64b9c3f 1720 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
8cd2f6e8 1721 * @opp_table: OPP table
984f16c8
NM
1722 * @dev: device for which we do this operation
1723 * @freq: Frequency in Hz for this OPP
1724 * @u_volt: Voltage in uVolts for this OPP
1725 * @dynamic: Dynamically added OPPs.
1726 *
2c2709dc 1727 * This function adds an opp definition to the opp table and returns status.
984f16c8
NM
1728 * The opp is made available by default and it can be controlled using
1729 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1730 *
8f8d37b2
VK
1731 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1732 * and freed by dev_pm_opp_of_remove_table.
984f16c8 1733 *
984f16c8
NM
1734 * Return:
1735 * 0 On success OR
1736 * Duplicate OPPs (both freq and volt are same) and opp->available
1737 * -EEXIST Freq are same and volt are different OR
1738 * Duplicate OPPs (both freq and volt are same) and !opp->available
1739 * -ENOMEM Memory allocation failure
1740 */
8cd2f6e8
VK
1741int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1742 unsigned long freq, long u_volt, bool dynamic)
e1f60b29 1743{
23dacf6d 1744 struct dev_pm_opp *new_opp;
50f8cfbd 1745 unsigned long tol;
6ce4184d 1746 int ret;
e1f60b29 1747
8cd2f6e8
VK
1748 new_opp = _opp_allocate(opp_table);
1749 if (!new_opp)
1750 return -ENOMEM;
23dacf6d 1751
a7470db6 1752 /* populate the opp table */
a7470db6 1753 new_opp->rate = freq;
2c2709dc 1754 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
dfbe4678
VK
1755 new_opp->supplies[0].u_volt = u_volt;
1756 new_opp->supplies[0].u_volt_min = u_volt - tol;
1757 new_opp->supplies[0].u_volt_max = u_volt + tol;
a7470db6 1758 new_opp->available = true;
23dacf6d 1759 new_opp->dynamic = dynamic;
a7470db6 1760
a1e8c136 1761 ret = _opp_add(dev, new_opp, opp_table, false);
7f8538eb
VK
1762 if (ret) {
1763 /* Don't return error for duplicate OPPs */
1764 if (ret == -EBUSY)
1765 ret = 0;
6ce4184d 1766 goto free_opp;
7f8538eb 1767 }
64ce8545 1768
03ca370f
MH
1769 /*
1770 * Notify the changes in the availability of the operable
1771 * frequency/voltage list.
1772 */
052c6f19 1773 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
e1f60b29 1774 return 0;
6ce4184d
VK
1775
1776free_opp:
8cd2f6e8
VK
1777 _opp_free(new_opp);
1778
6ce4184d 1779 return ret;
e1f60b29 1780}
38393409 1781
7de36b0a
VK
1782/**
1783 * dev_pm_opp_set_supported_hw() - Set supported platforms
1784 * @dev: Device for which supported-hw has to be set.
1785 * @versions: Array of hierarchy of versions to match.
1786 * @count: Number of elements in the array.
1787 *
1788 * This is required only for the V2 bindings, and it enables a platform to
1789 * specify the hierarchy of versions it supports. OPP layer will then enable
1790 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1791 * property.
7de36b0a 1792 */
fa30184d
VK
1793struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1794 const u32 *versions, unsigned int count)
7de36b0a 1795{
2c2709dc 1796 struct opp_table *opp_table;
7de36b0a 1797
32439ac7 1798 opp_table = _add_opp_table(dev, false);
dd461cd9
SG
1799 if (IS_ERR(opp_table))
1800 return opp_table;
7de36b0a 1801
2c2709dc
VK
1802 /* Make sure there are no concurrent readers while updating opp_table */
1803 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1804
25419de1
VK
1805 /* Another CPU that shares the OPP table has set the property ? */
1806 if (opp_table->supported_hw)
1807 return opp_table;
7de36b0a 1808
2c2709dc 1809 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
7de36b0a 1810 GFP_KERNEL);
2c2709dc 1811 if (!opp_table->supported_hw) {
25419de1
VK
1812 dev_pm_opp_put_opp_table(opp_table);
1813 return ERR_PTR(-ENOMEM);
7de36b0a
VK
1814 }
1815
2c2709dc 1816 opp_table->supported_hw_count = count;
fa30184d
VK
1817
1818 return opp_table;
7de36b0a
VK
1819}
1820EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1821
1822/**
1823 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
fa30184d 1824 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
7de36b0a
VK
1825 *
1826 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1827 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
7de36b0a 1828 * will not be freed.
7de36b0a 1829 */
fa30184d 1830void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
7de36b0a 1831{
c7bf8758
VK
1832 if (unlikely(!opp_table))
1833 return;
1834
2c2709dc
VK
1835 /* Make sure there are no concurrent readers while updating opp_table */
1836 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1837
2c2709dc
VK
1838 kfree(opp_table->supported_hw);
1839 opp_table->supported_hw = NULL;
1840 opp_table->supported_hw_count = 0;
7de36b0a 1841
fa30184d 1842 dev_pm_opp_put_opp_table(opp_table);
7de36b0a
VK
1843}
1844EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1845
01fb4d3c
VK
1846/**
1847 * dev_pm_opp_set_prop_name() - Set prop-extn name
a5da6447 1848 * @dev: Device for which the prop-name has to be set.
01fb4d3c
VK
1849 * @name: name to postfix to properties.
1850 *
1851 * This is required only for the V2 bindings, and it enables a platform to
1852 * specify the extn to be used for certain property names. The properties to
1853 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1854 * should postfix the property name with -<name> while looking for them.
01fb4d3c 1855 */
fa30184d 1856struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
01fb4d3c 1857{
2c2709dc 1858 struct opp_table *opp_table;
01fb4d3c 1859
32439ac7 1860 opp_table = _add_opp_table(dev, false);
dd461cd9
SG
1861 if (IS_ERR(opp_table))
1862 return opp_table;
01fb4d3c 1863
2c2709dc
VK
1864 /* Make sure there are no concurrent readers while updating opp_table */
1865 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1866
878ec1a9
VK
1867 /* Another CPU that shares the OPP table has set the property ? */
1868 if (opp_table->prop_name)
1869 return opp_table;
01fb4d3c 1870
2c2709dc
VK
1871 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1872 if (!opp_table->prop_name) {
878ec1a9
VK
1873 dev_pm_opp_put_opp_table(opp_table);
1874 return ERR_PTR(-ENOMEM);
01fb4d3c
VK
1875 }
1876
fa30184d 1877 return opp_table;
01fb4d3c
VK
1878}
1879EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1880
1881/**
1882 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
fa30184d 1883 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
01fb4d3c
VK
1884 *
1885 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1886 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
01fb4d3c 1887 * will not be freed.
01fb4d3c 1888 */
fa30184d 1889void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
01fb4d3c 1890{
c7bf8758
VK
1891 if (unlikely(!opp_table))
1892 return;
1893
2c2709dc
VK
1894 /* Make sure there are no concurrent readers while updating opp_table */
1895 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1896
2c2709dc
VK
1897 kfree(opp_table->prop_name);
1898 opp_table->prop_name = NULL;
01fb4d3c 1899
fa30184d 1900 dev_pm_opp_put_opp_table(opp_table);
01fb4d3c
VK
1901}
1902EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1903
9f8ea969 1904/**
dfbe4678 1905 * dev_pm_opp_set_regulators() - Set regulator names for the device
9f8ea969 1906 * @dev: Device for which regulator name is being set.
dfbe4678
VK
1907 * @names: Array of pointers to the names of the regulator.
1908 * @count: Number of regulators.
9f8ea969
VK
1909 *
1910 * In order to support OPP switching, OPP layer needs to know the name of the
dfbe4678
VK
1911 * device's regulators, as the core would be required to switch voltages as
1912 * well.
9f8ea969
VK
1913 *
1914 * This must be called before any OPPs are initialized for the device.
9f8ea969 1915 */
dfbe4678
VK
1916struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1917 const char * const names[],
1918 unsigned int count)
9f8ea969 1919{
38bb3439 1920 struct dev_pm_opp_supply *supplies;
2c2709dc 1921 struct opp_table *opp_table;
9f8ea969 1922 struct regulator *reg;
dfbe4678 1923 int ret, i;
9f8ea969 1924
32439ac7 1925 opp_table = _add_opp_table(dev, false);
dd461cd9
SG
1926 if (IS_ERR(opp_table))
1927 return opp_table;
9f8ea969
VK
1928
1929 /* This should be called before OPPs are initialized */
2c2709dc 1930 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
9f8ea969
VK
1931 ret = -EBUSY;
1932 goto err;
1933 }
1934
779b783c
VK
1935 /* Another CPU that shares the OPP table has set the regulators ? */
1936 if (opp_table->regulators)
1937 return opp_table;
dfbe4678
VK
1938
1939 opp_table->regulators = kmalloc_array(count,
1940 sizeof(*opp_table->regulators),
1941 GFP_KERNEL);
1942 if (!opp_table->regulators) {
1943 ret = -ENOMEM;
9f8ea969
VK
1944 goto err;
1945 }
1946
dfbe4678
VK
1947 for (i = 0; i < count; i++) {
1948 reg = regulator_get_optional(dev, names[i]);
1949 if (IS_ERR(reg)) {
1950 ret = PTR_ERR(reg);
1951 if (ret != -EPROBE_DEFER)
1952 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1953 __func__, names[i], ret);
1954 goto free_regulators;
1955 }
1956
1957 opp_table->regulators[i] = reg;
1958 }
1959
1960 opp_table->regulator_count = count;
9f8ea969 1961
38bb3439
VK
1962 supplies = kmalloc_array(count * 2, sizeof(*supplies), GFP_KERNEL);
1963 if (!supplies) {
1964 ret = -ENOMEM;
94735585 1965 goto free_regulators;
38bb3439
VK
1966 }
1967
1968 mutex_lock(&opp_table->lock);
1969 opp_table->sod_supplies = supplies;
1970 if (opp_table->set_opp_data) {
1971 opp_table->set_opp_data->old_opp.supplies = supplies;
1972 opp_table->set_opp_data->new_opp.supplies = supplies + count;
1973 }
1974 mutex_unlock(&opp_table->lock);
94735585 1975
91291d9a 1976 return opp_table;
9f8ea969 1977
dfbe4678 1978free_regulators:
24957db1
MS
1979 while (i != 0)
1980 regulator_put(opp_table->regulators[--i]);
dfbe4678
VK
1981
1982 kfree(opp_table->regulators);
1983 opp_table->regulators = NULL;
46f48aca 1984 opp_table->regulator_count = -1;
9f8ea969 1985err:
fa30184d 1986 dev_pm_opp_put_opp_table(opp_table);
9f8ea969 1987
91291d9a 1988 return ERR_PTR(ret);
9f8ea969 1989}
dfbe4678 1990EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
9f8ea969
VK
1991
1992/**
dfbe4678
VK
1993 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1994 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
9f8ea969 1995 */
dfbe4678 1996void dev_pm_opp_put_regulators(struct opp_table *opp_table)
9f8ea969 1997{
dfbe4678
VK
1998 int i;
1999
c7bf8758
VK
2000 if (unlikely(!opp_table))
2001 return;
2002
779b783c
VK
2003 if (!opp_table->regulators)
2004 goto put_opp_table;
9f8ea969 2005
2c2709dc
VK
2006 /* Make sure there are no concurrent readers while updating opp_table */
2007 WARN_ON(!list_empty(&opp_table->opp_list));
9f8ea969 2008
72f80ce4 2009 if (opp_table->enabled) {
8d45719c
KK
2010 for (i = opp_table->regulator_count - 1; i >= 0; i--)
2011 regulator_disable(opp_table->regulators[i]);
8d45719c
KK
2012 }
2013
24957db1 2014 for (i = opp_table->regulator_count - 1; i >= 0; i--)
dfbe4678
VK
2015 regulator_put(opp_table->regulators[i]);
2016
38bb3439
VK
2017 mutex_lock(&opp_table->lock);
2018 if (opp_table->set_opp_data) {
2019 opp_table->set_opp_data->old_opp.supplies = NULL;
2020 opp_table->set_opp_data->new_opp.supplies = NULL;
2021 }
2022
2023 kfree(opp_table->sod_supplies);
2024 opp_table->sod_supplies = NULL;
2025 mutex_unlock(&opp_table->lock);
94735585 2026
dfbe4678
VK
2027 kfree(opp_table->regulators);
2028 opp_table->regulators = NULL;
46f48aca 2029 opp_table->regulator_count = -1;
9f8ea969 2030
779b783c 2031put_opp_table:
fa30184d 2032 dev_pm_opp_put_opp_table(opp_table);
9f8ea969 2033}
dfbe4678 2034EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
9f8ea969 2035
829a4e8c
VK
2036/**
2037 * dev_pm_opp_set_clkname() - Set clk name for the device
2038 * @dev: Device for which clk name is being set.
2039 * @name: Clk name.
2040 *
2041 * In order to support OPP switching, OPP layer needs to get pointer to the
2042 * clock for the device. Simple cases work fine without using this routine (i.e.
2043 * by passing connection-id as NULL), but for a device with multiple clocks
2044 * available, the OPP core needs to know the exact name of the clk to use.
2045 *
2046 * This must be called before any OPPs are initialized for the device.
2047 */
2048struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
2049{
2050 struct opp_table *opp_table;
2051 int ret;
2052
32439ac7 2053 opp_table = _add_opp_table(dev, false);
dd461cd9
SG
2054 if (IS_ERR(opp_table))
2055 return opp_table;
829a4e8c
VK
2056
2057 /* This should be called before OPPs are initialized */
2058 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
2059 ret = -EBUSY;
2060 goto err;
2061 }
2062
32439ac7
VK
2063 /* clk shouldn't be initialized at this point */
2064 if (WARN_ON(opp_table->clk)) {
2065 ret = -EBUSY;
2066 goto err;
2067 }
829a4e8c
VK
2068
2069 /* Find clk for the device */
2070 opp_table->clk = clk_get(dev, name);
2071 if (IS_ERR(opp_table->clk)) {
2072 ret = PTR_ERR(opp_table->clk);
2073 if (ret != -EPROBE_DEFER) {
2074 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
2075 ret);
2076 }
2077 goto err;
2078 }
2079
2080 return opp_table;
2081
2082err:
2083 dev_pm_opp_put_opp_table(opp_table);
2084
2085 return ERR_PTR(ret);
2086}
2087EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
2088
2089/**
2090 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
2091 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
2092 */
2093void dev_pm_opp_put_clkname(struct opp_table *opp_table)
2094{
c7bf8758
VK
2095 if (unlikely(!opp_table))
2096 return;
2097
829a4e8c
VK
2098 /* Make sure there are no concurrent readers while updating opp_table */
2099 WARN_ON(!list_empty(&opp_table->opp_list));
2100
2101 clk_put(opp_table->clk);
2102 opp_table->clk = ERR_PTR(-EINVAL);
2103
2104 dev_pm_opp_put_opp_table(opp_table);
2105}
2106EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
2107
4dab160e
VK
2108/**
2109 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
2110 * @dev: Device for which the helper is getting registered.
2111 * @set_opp: Custom set OPP helper.
2112 *
2113 * This is useful to support complex platforms (like platforms with multiple
2114 * regulators per device), instead of the generic OPP set rate helper.
2115 *
2116 * This must be called before any OPPs are initialized for the device.
4dab160e 2117 */
fa30184d 2118struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
4dab160e
VK
2119 int (*set_opp)(struct dev_pm_set_opp_data *data))
2120{
38bb3439 2121 struct dev_pm_set_opp_data *data;
4dab160e 2122 struct opp_table *opp_table;
4dab160e
VK
2123
2124 if (!set_opp)
fa30184d 2125 return ERR_PTR(-EINVAL);
4dab160e 2126
32439ac7 2127 opp_table = _add_opp_table(dev, false);
47efcbcb 2128 if (IS_ERR(opp_table))
dd461cd9 2129 return opp_table;
4dab160e
VK
2130
2131 /* This should be called before OPPs are initialized */
2132 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
5019acc6
VK
2133 dev_pm_opp_put_opp_table(opp_table);
2134 return ERR_PTR(-EBUSY);
4dab160e
VK
2135 }
2136
5019acc6 2137 /* Another CPU that shares the OPP table has set the helper ? */
38bb3439
VK
2138 if (opp_table->set_opp)
2139 return opp_table;
2140
2141 data = kzalloc(sizeof(*data), GFP_KERNEL);
2142 if (!data)
2143 return ERR_PTR(-ENOMEM);
2144
2145 mutex_lock(&opp_table->lock);
2146 opp_table->set_opp_data = data;
2147 if (opp_table->sod_supplies) {
2148 data->old_opp.supplies = opp_table->sod_supplies;
2149 data->new_opp.supplies = opp_table->sod_supplies +
2150 opp_table->regulator_count;
2151 }
2152 mutex_unlock(&opp_table->lock);
2153
2154 opp_table->set_opp = set_opp;
4dab160e 2155
fa30184d 2156 return opp_table;
4dab160e
VK
2157}
2158EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
2159
2160/**
604a7aeb 2161 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
4dab160e 2162 * set_opp helper
fa30184d 2163 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
4dab160e 2164 *
fa30184d 2165 * Release resources blocked for platform specific set_opp helper.
4dab160e 2166 */
604a7aeb 2167void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
4dab160e 2168{
c7bf8758
VK
2169 if (unlikely(!opp_table))
2170 return;
2171
4dab160e
VK
2172 /* Make sure there are no concurrent readers while updating opp_table */
2173 WARN_ON(!list_empty(&opp_table->opp_list));
2174
2175 opp_table->set_opp = NULL;
38bb3439
VK
2176
2177 mutex_lock(&opp_table->lock);
2178 kfree(opp_table->set_opp_data);
2179 opp_table->set_opp_data = NULL;
2180 mutex_unlock(&opp_table->lock);
2181
fa30184d 2182 dev_pm_opp_put_opp_table(opp_table);
4dab160e 2183}
604a7aeb 2184EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
4dab160e 2185
a3c47af6
DO
2186static void devm_pm_opp_unregister_set_opp_helper(void *data)
2187{
2188 dev_pm_opp_unregister_set_opp_helper(data);
2189}
2190
2191/**
2192 * devm_pm_opp_register_set_opp_helper() - Register custom set OPP helper
2193 * @dev: Device for which the helper is getting registered.
2194 * @set_opp: Custom set OPP helper.
2195 *
2196 * This is a resource-managed version of dev_pm_opp_register_set_opp_helper().
2197 *
2198 * Return: pointer to 'struct opp_table' on success and errorno otherwise.
2199 */
2200struct opp_table *
2201devm_pm_opp_register_set_opp_helper(struct device *dev,
2202 int (*set_opp)(struct dev_pm_set_opp_data *data))
2203{
2204 struct opp_table *opp_table;
2205 int err;
2206
2207 opp_table = dev_pm_opp_register_set_opp_helper(dev, set_opp);
2208 if (IS_ERR(opp_table))
2209 return opp_table;
2210
2211 err = devm_add_action_or_reset(dev, devm_pm_opp_unregister_set_opp_helper,
2212 opp_table);
2213 if (err)
2214 return ERR_PTR(err);
2215
2216 return opp_table;
2217}
2218EXPORT_SYMBOL_GPL(devm_pm_opp_register_set_opp_helper);
2219
6319aee1
VK
2220static void _opp_detach_genpd(struct opp_table *opp_table)
2221{
2222 int index;
2223
cb60e960
VK
2224 if (!opp_table->genpd_virt_devs)
2225 return;
2226
6319aee1
VK
2227 for (index = 0; index < opp_table->required_opp_count; index++) {
2228 if (!opp_table->genpd_virt_devs[index])
2229 continue;
2230
2231 dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
2232 opp_table->genpd_virt_devs[index] = NULL;
2233 }
c0ab9e08
VK
2234
2235 kfree(opp_table->genpd_virt_devs);
2236 opp_table->genpd_virt_devs = NULL;
6319aee1
VK
2237}
2238
4f018bc0 2239/**
6319aee1
VK
2240 * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
2241 * @dev: Consumer device for which the genpd is getting attached.
2242 * @names: Null terminated array of pointers containing names of genpd to attach.
17a8f868 2243 * @virt_devs: Pointer to return the array of virtual devices.
4f018bc0
VK
2244 *
2245 * Multiple generic power domains for a device are supported with the help of
2246 * virtual genpd devices, which are created for each consumer device - genpd
2247 * pair. These are the device structures which are attached to the power domain
2248 * and are required by the OPP core to set the performance state of the genpd.
6319aee1
VK
2249 * The same API also works for the case where single genpd is available and so
2250 * we don't need to support that separately.
4f018bc0
VK
2251 *
2252 * This helper will normally be called by the consumer driver of the device
6319aee1 2253 * "dev", as only that has details of the genpd names.
4f018bc0 2254 *
6319aee1
VK
2255 * This helper needs to be called once with a list of all genpd to attach.
2256 * Otherwise the original device structure will be used instead by the OPP core.
baea35e4
VK
2257 *
2258 * The order of entries in the names array must match the order in which
2259 * "required-opps" are added in DT.
4f018bc0 2260 */
17a8f868
VK
2261struct opp_table *dev_pm_opp_attach_genpd(struct device *dev,
2262 const char **names, struct device ***virt_devs)
4f018bc0
VK
2263{
2264 struct opp_table *opp_table;
6319aee1 2265 struct device *virt_dev;
baea35e4 2266 int index = 0, ret = -EINVAL;
6319aee1 2267 const char **name = names;
4f018bc0 2268
32439ac7 2269 opp_table = _add_opp_table(dev, false);
dd461cd9
SG
2270 if (IS_ERR(opp_table))
2271 return opp_table;
4f018bc0 2272
cb60e960
VK
2273 if (opp_table->genpd_virt_devs)
2274 return opp_table;
4f018bc0 2275
6319aee1
VK
2276 /*
2277 * If the genpd's OPP table isn't already initialized, parsing of the
2278 * required-opps fail for dev. We should retry this after genpd's OPP
2279 * table is added.
2280 */
2281 if (!opp_table->required_opp_count) {
2282 ret = -EPROBE_DEFER;
2283 goto put_table;
2284 }
2285
4f018bc0
VK
2286 mutex_lock(&opp_table->genpd_virt_dev_lock);
2287
c0ab9e08
VK
2288 opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2289 sizeof(*opp_table->genpd_virt_devs),
2290 GFP_KERNEL);
2291 if (!opp_table->genpd_virt_devs)
2292 goto unlock;
4f018bc0 2293
6319aee1 2294 while (*name) {
6319aee1
VK
2295 if (index >= opp_table->required_opp_count) {
2296 dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
2297 *name, opp_table->required_opp_count, index);
2298 goto err;
2299 }
4f018bc0 2300
6319aee1
VK
2301 virt_dev = dev_pm_domain_attach_by_name(dev, *name);
2302 if (IS_ERR(virt_dev)) {
2303 ret = PTR_ERR(virt_dev);
2304 dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
2305 goto err;
2306 }
2307
2308 opp_table->genpd_virt_devs[index] = virt_dev;
baea35e4 2309 index++;
6319aee1 2310 name++;
4f018bc0
VK
2311 }
2312
17a8f868
VK
2313 if (virt_devs)
2314 *virt_devs = opp_table->genpd_virt_devs;
4f018bc0
VK
2315 mutex_unlock(&opp_table->genpd_virt_dev_lock);
2316
2317 return opp_table;
6319aee1
VK
2318
2319err:
2320 _opp_detach_genpd(opp_table);
c0ab9e08 2321unlock:
6319aee1
VK
2322 mutex_unlock(&opp_table->genpd_virt_dev_lock);
2323
2324put_table:
2325 dev_pm_opp_put_opp_table(opp_table);
2326
2327 return ERR_PTR(ret);
4f018bc0 2328}
6319aee1 2329EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd);
4f018bc0
VK
2330
2331/**
6319aee1
VK
2332 * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device.
2333 * @opp_table: OPP table returned by dev_pm_opp_attach_genpd().
4f018bc0 2334 *
6319aee1
VK
2335 * This detaches the genpd(s), resets the virtual device pointers, and puts the
2336 * OPP table.
4f018bc0 2337 */
6319aee1 2338void dev_pm_opp_detach_genpd(struct opp_table *opp_table)
4f018bc0 2339{
c7bf8758
VK
2340 if (unlikely(!opp_table))
2341 return;
2342
4f018bc0
VK
2343 /*
2344 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
2345 * used in parallel.
2346 */
2347 mutex_lock(&opp_table->genpd_virt_dev_lock);
6319aee1 2348 _opp_detach_genpd(opp_table);
4f018bc0
VK
2349 mutex_unlock(&opp_table->genpd_virt_dev_lock);
2350
6319aee1 2351 dev_pm_opp_put_opp_table(opp_table);
4f018bc0 2352}
6319aee1 2353EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd);
4f018bc0 2354
b4b9e223
DO
2355static void devm_pm_opp_detach_genpd(void *data)
2356{
2357 dev_pm_opp_detach_genpd(data);
2358}
2359
2360/**
2361 * devm_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual
2362 * device pointer
2363 * @dev: Consumer device for which the genpd is getting attached.
2364 * @names: Null terminated array of pointers containing names of genpd to attach.
2365 * @virt_devs: Pointer to return the array of virtual devices.
2366 *
2367 * This is a resource-managed version of dev_pm_opp_attach_genpd().
2368 *
2369 * Return: pointer to 'struct opp_table' on success and errorno otherwise.
2370 */
2371struct opp_table *
2372devm_pm_opp_attach_genpd(struct device *dev, const char **names,
2373 struct device ***virt_devs)
2374{
2375 struct opp_table *opp_table;
2376 int err;
2377
2378 opp_table = dev_pm_opp_attach_genpd(dev, names, virt_devs);
2379 if (IS_ERR(opp_table))
2380 return opp_table;
2381
2382 err = devm_add_action_or_reset(dev, devm_pm_opp_detach_genpd,
2383 opp_table);
2384 if (err)
2385 return ERR_PTR(err);
2386
2387 return opp_table;
2388}
2389EXPORT_SYMBOL_GPL(devm_pm_opp_attach_genpd);
2390
c8a59103
VK
2391/**
2392 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2393 * @src_table: OPP table which has dst_table as one of its required OPP table.
2394 * @dst_table: Required OPP table of the src_table.
2395 * @pstate: Current performance state of the src_table.
2396 *
2397 * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2398 * "required-opps" property of the OPP (present in @src_table) which has
2399 * performance state set to @pstate.
2400 *
2401 * Return: Zero or positive performance state on success, otherwise negative
2402 * value on errors.
2403 */
2404int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2405 struct opp_table *dst_table,
2406 unsigned int pstate)
2407{
2408 struct dev_pm_opp *opp;
2409 int dest_pstate = -EINVAL;
2410 int i;
2411
c8a59103
VK
2412 /*
2413 * Normally the src_table will have the "required_opps" property set to
2414 * point to one of the OPPs in the dst_table, but in some cases the
2415 * genpd and its master have one to one mapping of performance states
2416 * and so none of them have the "required-opps" property set. Return the
2417 * pstate of the src_table as it is in such cases.
2418 */
f2f4d2b8 2419 if (!src_table || !src_table->required_opp_count)
c8a59103
VK
2420 return pstate;
2421
2422 for (i = 0; i < src_table->required_opp_count; i++) {
2423 if (src_table->required_opp_tables[i]->np == dst_table->np)
2424 break;
2425 }
2426
2427 if (unlikely(i == src_table->required_opp_count)) {
2428 pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2429 __func__, src_table, dst_table);
2430 return -EINVAL;
2431 }
2432
2433 mutex_lock(&src_table->lock);
2434
2435 list_for_each_entry(opp, &src_table->opp_list, node) {
2436 if (opp->pstate == pstate) {
2437 dest_pstate = opp->required_opps[i]->pstate;
2438 goto unlock;
2439 }
2440 }
2441
2442 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2443 dst_table);
2444
2445unlock:
2446 mutex_unlock(&src_table->lock);
2447
2448 return dest_pstate;
2449}
2450
38393409
VK
2451/**
2452 * dev_pm_opp_add() - Add an OPP table from a table definitions
2453 * @dev: device for which we do this operation
2454 * @freq: Frequency in Hz for this OPP
2455 * @u_volt: Voltage in uVolts for this OPP
2456 *
2c2709dc 2457 * This function adds an opp definition to the opp table and returns status.
38393409
VK
2458 * The opp is made available by default and it can be controlled using
2459 * dev_pm_opp_enable/disable functions.
2460 *
38393409 2461 * Return:
984f16c8 2462 * 0 On success OR
38393409 2463 * Duplicate OPPs (both freq and volt are same) and opp->available
984f16c8 2464 * -EEXIST Freq are same and volt are different OR
38393409 2465 * Duplicate OPPs (both freq and volt are same) and !opp->available
984f16c8 2466 * -ENOMEM Memory allocation failure
38393409
VK
2467 */
2468int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
2469{
8cd2f6e8
VK
2470 struct opp_table *opp_table;
2471 int ret;
2472
32439ac7 2473 opp_table = _add_opp_table(dev, true);
dd461cd9
SG
2474 if (IS_ERR(opp_table))
2475 return PTR_ERR(opp_table);
8cd2f6e8 2476
46f48aca
VK
2477 /* Fix regulator count for dynamic OPPs */
2478 opp_table->regulator_count = 1;
2479
8cd2f6e8 2480 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
0ad8c623
VK
2481 if (ret)
2482 dev_pm_opp_put_opp_table(opp_table);
8cd2f6e8 2483
8cd2f6e8 2484 return ret;
38393409 2485}
5d4879cd 2486EXPORT_SYMBOL_GPL(dev_pm_opp_add);
e1f60b29
NM
2487
2488/**
327854c8 2489 * _opp_set_availability() - helper to set the availability of an opp
e1f60b29
NM
2490 * @dev: device for which we do this operation
2491 * @freq: OPP frequency to modify availability
2492 * @availability_req: availability status requested for this opp
2493 *
052c6f19
VK
2494 * Set the availability of an OPP, opp_{enable,disable} share a common logic
2495 * which is isolated here.
e1f60b29 2496 *
984f16c8 2497 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 2498 * copy operation, returns 0 if no modification was done OR modification was
e1f60b29 2499 * successful.
e1f60b29 2500 */
327854c8
NM
2501static int _opp_set_availability(struct device *dev, unsigned long freq,
2502 bool availability_req)
e1f60b29 2503{
2c2709dc 2504 struct opp_table *opp_table;
a7f3987e 2505 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
e1f60b29
NM
2506 int r = 0;
2507
2c2709dc
VK
2508 /* Find the opp_table */
2509 opp_table = _find_opp_table(dev);
2510 if (IS_ERR(opp_table)) {
2511 r = PTR_ERR(opp_table);
e1f60b29 2512 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
a7f3987e 2513 return r;
e1f60b29
NM
2514 }
2515
37a73ec0
VK
2516 mutex_lock(&opp_table->lock);
2517
e1f60b29 2518 /* Do we have the frequency? */
2c2709dc 2519 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
2520 if (tmp_opp->rate == freq) {
2521 opp = tmp_opp;
2522 break;
2523 }
2524 }
37a73ec0 2525
e1f60b29
NM
2526 if (IS_ERR(opp)) {
2527 r = PTR_ERR(opp);
2528 goto unlock;
2529 }
2530
2531 /* Is update really needed? */
2532 if (opp->available == availability_req)
2533 goto unlock;
e1f60b29 2534
a7f3987e 2535 opp->available = availability_req;
e1f60b29 2536
e4d8ae00
VK
2537 dev_pm_opp_get(opp);
2538 mutex_unlock(&opp_table->lock);
2539
03ca370f
MH
2540 /* Notify the change of the OPP availability */
2541 if (availability_req)
052c6f19 2542 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
a7f3987e 2543 opp);
03ca370f 2544 else
052c6f19 2545 blocking_notifier_call_chain(&opp_table->head,
a7f3987e 2546 OPP_EVENT_DISABLE, opp);
e1f60b29 2547
e4d8ae00
VK
2548 dev_pm_opp_put(opp);
2549 goto put_table;
2550
e1f60b29 2551unlock:
5b650b38 2552 mutex_unlock(&opp_table->lock);
e4d8ae00 2553put_table:
5b650b38 2554 dev_pm_opp_put_opp_table(opp_table);
e1f60b29
NM
2555 return r;
2556}
2557
25cb20a2
SB
2558/**
2559 * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
2560 * @dev: device for which we do this operation
2561 * @freq: OPP frequency to adjust voltage of
2562 * @u_volt: new OPP target voltage
2563 * @u_volt_min: new OPP min voltage
2564 * @u_volt_max: new OPP max voltage
2565 *
2566 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2567 * copy operation, returns 0 if no modifcation was done OR modification was
2568 * successful.
2569 */
2570int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
2571 unsigned long u_volt, unsigned long u_volt_min,
2572 unsigned long u_volt_max)
2573
2574{
2575 struct opp_table *opp_table;
2576 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
2577 int r = 0;
2578
2579 /* Find the opp_table */
2580 opp_table = _find_opp_table(dev);
2581 if (IS_ERR(opp_table)) {
2582 r = PTR_ERR(opp_table);
2583 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
2584 return r;
2585 }
2586
2587 mutex_lock(&opp_table->lock);
2588
2589 /* Do we have the frequency? */
2590 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
2591 if (tmp_opp->rate == freq) {
2592 opp = tmp_opp;
2593 break;
2594 }
2595 }
2596
2597 if (IS_ERR(opp)) {
2598 r = PTR_ERR(opp);
2599 goto adjust_unlock;
2600 }
2601
2602 /* Is update really needed? */
2603 if (opp->supplies->u_volt == u_volt)
2604 goto adjust_unlock;
2605
2606 opp->supplies->u_volt = u_volt;
2607 opp->supplies->u_volt_min = u_volt_min;
2608 opp->supplies->u_volt_max = u_volt_max;
2609
2610 dev_pm_opp_get(opp);
2611 mutex_unlock(&opp_table->lock);
2612
2613 /* Notify the voltage change of the OPP */
2614 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
2615 opp);
2616
2617 dev_pm_opp_put(opp);
2618 goto adjust_put_table;
2619
2620adjust_unlock:
2621 mutex_unlock(&opp_table->lock);
2622adjust_put_table:
2623 dev_pm_opp_put_opp_table(opp_table);
2624 return r;
2625}
03649154 2626EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
25cb20a2 2627
e1f60b29 2628/**
5d4879cd 2629 * dev_pm_opp_enable() - Enable a specific OPP
e1f60b29
NM
2630 * @dev: device for which we do this operation
2631 * @freq: OPP frequency to enable
2632 *
2633 * Enables a provided opp. If the operation is valid, this returns 0, else the
2634 * corresponding error value. It is meant to be used for users an OPP available
5d4879cd 2635 * after being temporarily made unavailable with dev_pm_opp_disable.
e1f60b29 2636 *
984f16c8 2637 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 2638 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 2639 * successful.
e1f60b29 2640 */
5d4879cd 2641int dev_pm_opp_enable(struct device *dev, unsigned long freq)
e1f60b29 2642{
327854c8 2643 return _opp_set_availability(dev, freq, true);
e1f60b29 2644}
5d4879cd 2645EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
e1f60b29
NM
2646
2647/**
5d4879cd 2648 * dev_pm_opp_disable() - Disable a specific OPP
e1f60b29
NM
2649 * @dev: device for which we do this operation
2650 * @freq: OPP frequency to disable
2651 *
2652 * Disables a provided opp. If the operation is valid, this returns
2653 * 0, else the corresponding error value. It is meant to be a temporary
2654 * control by users to make this OPP not available until the circumstances are
5d4879cd 2655 * right to make it available again (with a call to dev_pm_opp_enable).
e1f60b29 2656 *
984f16c8 2657 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 2658 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 2659 * successful.
e1f60b29 2660 */
5d4879cd 2661int dev_pm_opp_disable(struct device *dev, unsigned long freq)
e1f60b29 2662{
327854c8 2663 return _opp_set_availability(dev, freq, false);
e1f60b29 2664}
5d4879cd 2665EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
e1f60b29 2666
03ca370f 2667/**
dc2c9ad5
VK
2668 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
2669 * @dev: Device for which notifier needs to be registered
2670 * @nb: Notifier block to be registered
984f16c8 2671 *
dc2c9ad5
VK
2672 * Return: 0 on success or a negative error value.
2673 */
2674int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
2675{
2676 struct opp_table *opp_table;
2677 int ret;
2678
dc2c9ad5 2679 opp_table = _find_opp_table(dev);
5b650b38
VK
2680 if (IS_ERR(opp_table))
2681 return PTR_ERR(opp_table);
2682
052c6f19 2683 ret = blocking_notifier_chain_register(&opp_table->head, nb);
dc2c9ad5 2684
5b650b38 2685 dev_pm_opp_put_opp_table(opp_table);
dc2c9ad5
VK
2686
2687 return ret;
2688}
2689EXPORT_SYMBOL(dev_pm_opp_register_notifier);
2690
2691/**
2692 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
2693 * @dev: Device for which notifier needs to be unregistered
2694 * @nb: Notifier block to be unregistered
984f16c8 2695 *
dc2c9ad5 2696 * Return: 0 on success or a negative error value.
03ca370f 2697 */
dc2c9ad5
VK
2698int dev_pm_opp_unregister_notifier(struct device *dev,
2699 struct notifier_block *nb)
03ca370f 2700{
dc2c9ad5
VK
2701 struct opp_table *opp_table;
2702 int ret;
03ca370f 2703
dc2c9ad5 2704 opp_table = _find_opp_table(dev);
5b650b38
VK
2705 if (IS_ERR(opp_table))
2706 return PTR_ERR(opp_table);
dc2c9ad5 2707
052c6f19 2708 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
03ca370f 2709
5b650b38 2710 dev_pm_opp_put_opp_table(opp_table);
dc2c9ad5
VK
2711
2712 return ret;
03ca370f 2713}
dc2c9ad5 2714EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
b496dfbc 2715
8aaf6264
VK
2716/**
2717 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
2718 * @dev: device pointer used to lookup OPP table.
2719 *
2720 * Free both OPPs created using static entries present in DT and the
2721 * dynamically added entries.
2722 */
2723void dev_pm_opp_remove_table(struct device *dev)
9274c892
VK
2724{
2725 struct opp_table *opp_table;
2726
2c2709dc
VK
2727 /* Check for existing table for 'dev' */
2728 opp_table = _find_opp_table(dev);
2729 if (IS_ERR(opp_table)) {
2730 int error = PTR_ERR(opp_table);
737002b5
VK
2731
2732 if (error != -ENODEV)
2c2709dc 2733 WARN(1, "%s: opp_table: %d\n",
737002b5
VK
2734 IS_ERR_OR_NULL(dev) ?
2735 "Invalid device" : dev_name(dev),
2736 error);
5b650b38 2737 return;
737002b5
VK
2738 }
2739
922ff075
VK
2740 /*
2741 * Drop the extra reference only if the OPP table was successfully added
2742 * with dev_pm_opp_of_add_table() earlier.
2743 **/
2744 if (_opp_remove_all_static(opp_table))
2745 dev_pm_opp_put_opp_table(opp_table);
cdd6ed90
VK
2746
2747 /* Drop reference taken by _find_opp_table() */
2748 dev_pm_opp_put_opp_table(opp_table);
737002b5 2749}
411466c5 2750EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
ce8073d8
DO
2751
2752/**
2753 * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
2754 * @dev: device for which we do this operation
2755 *
2756 * Sync voltage state of the OPP table regulators.
2757 *
2758 * Return: 0 on success or a negative error value.
2759 */
2760int dev_pm_opp_sync_regulators(struct device *dev)
2761{
2762 struct opp_table *opp_table;
2763 struct regulator *reg;
2764 int i, ret = 0;
2765
2766 /* Device may not have OPP table */
2767 opp_table = _find_opp_table(dev);
2768 if (IS_ERR(opp_table))
2769 return 0;
2770
2771 /* Regulator may not be required for the device */
2772 if (unlikely(!opp_table->regulators))
2773 goto put_table;
2774
2775 /* Nothing to sync if voltage wasn't changed */
2776 if (!opp_table->enabled)
2777 goto put_table;
2778
2779 for (i = 0; i < opp_table->regulator_count; i++) {
2780 reg = opp_table->regulators[i];
2781 ret = regulator_sync_voltage(reg);
2782 if (ret)
2783 break;
2784 }
2785put_table:
2786 /* Drop reference taken by _find_opp_table() */
2787 dev_pm_opp_put_opp_table(opp_table);
2788
2789 return ret;
2790}
2791EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);