]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/opp/core.c
opp: Split _set_opp() out of dev_pm_opp_set_rate()
[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
729 ret = clk_set_rate(clk, freq);
730 if (ret) {
731 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
732 ret);
733 }
734
735 return ret;
736}
737
8d45719c 738static int _generic_set_opp_regulator(struct opp_table *opp_table,
c74b32fa
VK
739 struct device *dev,
740 unsigned long old_freq,
741 unsigned long freq,
742 struct dev_pm_opp_supply *old_supply,
743 struct dev_pm_opp_supply *new_supply)
94735585 744{
c74b32fa 745 struct regulator *reg = opp_table->regulators[0];
94735585
VK
746 int ret;
747
748 /* This function only supports single regulator per device */
c74b32fa 749 if (WARN_ON(opp_table->regulator_count > 1)) {
94735585
VK
750 dev_err(dev, "multiple regulators are not supported\n");
751 return -EINVAL;
752 }
753
754 /* Scaling up? Scale voltage before frequency */
c5c2a97b 755 if (freq >= old_freq) {
94735585
VK
756 ret = _set_opp_voltage(dev, reg, new_supply);
757 if (ret)
758 goto restore_voltage;
759 }
760
761 /* Change frequency */
285881b5 762 ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
94735585
VK
763 if (ret)
764 goto restore_voltage;
765
766 /* Scaling down? Scale voltage after frequency */
767 if (freq < old_freq) {
768 ret = _set_opp_voltage(dev, reg, new_supply);
769 if (ret)
770 goto restore_freq;
771 }
772
8d45719c
KK
773 /*
774 * Enable the regulator after setting its voltages, otherwise it breaks
775 * some boot-enabled regulators.
776 */
72f80ce4 777 if (unlikely(!opp_table->enabled)) {
8d45719c
KK
778 ret = regulator_enable(reg);
779 if (ret < 0)
780 dev_warn(dev, "Failed to enable regulator: %d", ret);
8d45719c
KK
781 }
782
94735585
VK
783 return 0;
784
785restore_freq:
285881b5 786 if (_generic_set_opp_clk_only(dev, opp_table->clk, old_freq))
94735585
VK
787 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
788 __func__, old_freq);
789restore_voltage:
790 /* This shouldn't harm even if the voltages weren't updated earlier */
81c4d8a3 791 _set_opp_voltage(dev, reg, old_supply);
94735585
VK
792
793 return ret;
794}
795
b00e667a
VK
796static int _set_opp_bw(const struct opp_table *opp_table,
797 struct dev_pm_opp *opp, struct device *dev, bool remove)
798{
799 u32 avg, peak;
800 int i, ret;
801
802 if (!opp_table->paths)
803 return 0;
804
805 for (i = 0; i < opp_table->path_count; i++) {
806 if (remove) {
807 avg = 0;
808 peak = 0;
809 } else {
810 avg = opp->bandwidth[i].avg;
811 peak = opp->bandwidth[i].peak;
812 }
813 ret = icc_set_bw(opp_table->paths[i], avg, peak);
814 if (ret) {
815 dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
816 remove ? "remove" : "set", i, ret);
817 return ret;
818 }
819 }
820
821 return 0;
822}
823
7e535993
VK
824static int _set_opp_custom(const struct opp_table *opp_table,
825 struct device *dev, unsigned long old_freq,
826 unsigned long freq,
827 struct dev_pm_opp_supply *old_supply,
828 struct dev_pm_opp_supply *new_supply)
829{
04b447df 830 struct dev_pm_set_opp_data *data = opp_table->set_opp_data;
7e535993
VK
831 int size;
832
04b447df
DO
833 /*
834 * We support this only if dev_pm_opp_set_regulators() was called
835 * earlier.
836 */
837 if (opp_table->sod_supplies) {
838 size = sizeof(*old_supply) * opp_table->regulator_count;
81c4d8a3 839 memcpy(data->old_opp.supplies, old_supply, size);
04b447df
DO
840 memcpy(data->new_opp.supplies, new_supply, size);
841 data->regulator_count = opp_table->regulator_count;
842 } else {
843 data->regulator_count = 0;
844 }
845
7e535993 846 data->regulators = opp_table->regulators;
7e535993
VK
847 data->clk = opp_table->clk;
848 data->dev = dev;
7e535993 849 data->old_opp.rate = old_freq;
7e535993 850 data->new_opp.rate = freq;
7e535993
VK
851
852 return opp_table->set_opp(data);
853}
854
60cdeae0
SG
855static int _set_required_opp(struct device *dev, struct device *pd_dev,
856 struct dev_pm_opp *opp, int i)
857{
858 unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0;
859 int ret;
860
861 if (!pd_dev)
862 return 0;
863
864 ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
865 if (ret) {
866 dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
867 dev_name(pd_dev), pstate, ret);
868 }
869
870 return ret;
871}
872
ca1b5d77
VK
873/* This is only called for PM domain for now */
874static int _set_required_opps(struct device *dev,
875 struct opp_table *opp_table,
2c59138c 876 struct dev_pm_opp *opp, bool up)
ca1b5d77
VK
877{
878 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
879 struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
ca1b5d77
VK
880 int i, ret = 0;
881
882 if (!required_opp_tables)
883 return 0;
884
885 /* Single genpd case */
60cdeae0
SG
886 if (!genpd_virt_devs)
887 return _set_required_opp(dev, dev, opp, 0);
ca1b5d77
VK
888
889 /* Multiple genpd case */
890
891 /*
892 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
893 * after it is freed from another thread.
894 */
895 mutex_lock(&opp_table->genpd_virt_dev_lock);
896
2c59138c
SG
897 /* Scaling up? Set required OPPs in normal order, else reverse */
898 if (up) {
899 for (i = 0; i < opp_table->required_opp_count; i++) {
900 ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
901 if (ret)
902 break;
903 }
904 } else {
905 for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
906 ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
907 if (ret)
908 break;
ca1b5d77
VK
909 }
910 }
2c59138c 911
ca1b5d77
VK
912 mutex_unlock(&opp_table->genpd_virt_dev_lock);
913
914 return ret;
915}
916
3ae1f39a
SS
917/**
918 * dev_pm_opp_set_bw() - sets bandwidth levels corresponding to an opp
919 * @dev: device for which we do this operation
920 * @opp: opp based on which the bandwidth levels are to be configured
921 *
922 * This configures the bandwidth to the levels specified by the OPP. However
923 * if the OPP specified is NULL the bandwidth levels are cleared out.
924 *
925 * Return: 0 on success or a negative error value.
926 */
927int dev_pm_opp_set_bw(struct device *dev, struct dev_pm_opp *opp)
928{
929 struct opp_table *opp_table;
930 int ret;
931
932 opp_table = _find_opp_table(dev);
933 if (IS_ERR(opp_table)) {
934 dev_err(dev, "%s: device opp table doesn't exist\n", __func__);
935 return PTR_ERR(opp_table);
936 }
937
938 if (opp)
939 ret = _set_opp_bw(opp_table, opp, dev, false);
940 else
941 ret = _set_opp_bw(opp_table, NULL, dev, true);
942
943 dev_pm_opp_put_opp_table(opp_table);
944 return ret;
945}
946EXPORT_SYMBOL_GPL(dev_pm_opp_set_bw);
947
81c4d8a3
VK
948static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
949{
950 struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
951 unsigned long freq;
952
953 if (!IS_ERR(opp_table->clk)) {
954 freq = clk_get_rate(opp_table->clk);
955 opp = _find_freq_ceil(opp_table, &freq);
956 }
957
958 /*
959 * Unable to find the current OPP ? Pick the first from the list since
960 * it is in ascending order, otherwise rest of the code will need to
961 * make special checks to validate current_opp.
962 */
963 if (IS_ERR(opp)) {
964 mutex_lock(&opp_table->lock);
965 opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
966 dev_pm_opp_get(opp);
967 mutex_unlock(&opp_table->lock);
968 }
969
970 opp_table->current_opp = opp;
971}
972
5ad58bba 973static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
f3364e17
VK
974{
975 int ret;
976
977 if (!opp_table->enabled)
978 return 0;
979
980 /*
981 * Some drivers need to support cases where some platforms may
982 * have OPP table for the device, while others don't and
983 * opp_set_rate() just needs to behave like clk_set_rate().
984 */
985 if (!_get_opp_count(opp_table))
986 return 0;
987
988 ret = _set_opp_bw(opp_table, NULL, dev, true);
989 if (ret)
990 return ret;
991
992 if (opp_table->regulators)
993 regulator_disable(opp_table->regulators[0]);
994
2c59138c 995 ret = _set_required_opps(dev, opp_table, NULL, false);
f3364e17
VK
996
997 opp_table->enabled = false;
998 return ret;
999}
1000
386ba854
VK
1001static int _set_opp(struct device *dev, struct opp_table *opp_table,
1002 struct dev_pm_opp *opp, unsigned long freq)
6a0712f6 1003{
386ba854
VK
1004 struct dev_pm_opp *old_opp;
1005 unsigned long old_freq;
b00e667a 1006 int ret;
6a0712f6 1007
386ba854
VK
1008 if (unlikely(!opp))
1009 return _disable_opp_table(dev, opp_table);
aca48b61 1010
81c4d8a3
VK
1011 /* Find the currently set OPP if we don't know already */
1012 if (unlikely(!opp_table->current_opp))
1013 _find_current_opp(dev, opp_table);
6a0712f6 1014
81c4d8a3
VK
1015 old_opp = opp_table->current_opp;
1016 old_freq = old_opp->rate;
1017
1018 /* Return early if nothing to do */
1019 if (opp_table->enabled && old_opp == opp) {
1020 dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__);
386ba854 1021 return 0;
6a0712f6
VK
1022 }
1023
94735585
VK
1024 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
1025 old_freq, freq);
dfbe4678 1026
ca1b5d77 1027 /* Scaling up? Configure required OPPs before frequency */
faef080f 1028 if (freq >= old_freq) {
2c59138c 1029 ret = _set_required_opps(dev, opp_table, opp, true);
ca1b5d77 1030 if (ret)
386ba854 1031 return ret;
ca1b5d77
VK
1032 }
1033
7e535993
VK
1034 if (opp_table->set_opp) {
1035 ret = _set_opp_custom(opp_table, dev, old_freq, freq,
81c4d8a3 1036 old_opp->supplies, opp->supplies);
7e535993 1037 } else if (opp_table->regulators) {
c74b32fa 1038 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
81c4d8a3 1039 old_opp->supplies,
c74b32fa
VK
1040 opp->supplies);
1041 } else {
7e535993 1042 /* Only frequency scaling */
1d3c42ca 1043 ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
ca1b5d77 1044 }
c74b32fa 1045
ca1b5d77
VK
1046 /* Scaling down? Configure required OPPs after frequency */
1047 if (!ret && freq < old_freq) {
2c59138c 1048 ret = _set_required_opps(dev, opp_table, opp, false);
ca1b5d77
VK
1049 if (ret)
1050 dev_err(dev, "Failed to set required opps: %d\n", ret);
dfbe4678
VK
1051 }
1052
72f80ce4 1053 if (!ret) {
b00e667a 1054 ret = _set_opp_bw(opp_table, opp, dev, false);
81c4d8a3 1055 if (!ret) {
72f80ce4 1056 opp_table->enabled = true;
81c4d8a3
VK
1057 dev_pm_opp_put(old_opp);
1058
1059 /* Make sure current_opp doesn't get freed */
1060 dev_pm_opp_get(opp);
1061 opp_table->current_opp = opp;
1062 }
72f80ce4 1063 }
fe2af402 1064
386ba854
VK
1065 return ret;
1066}
1067
1068/**
1069 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1070 * @dev: device for which we do this operation
1071 * @target_freq: frequency to achieve
1072 *
1073 * This configures the power-supplies to the levels specified by the OPP
1074 * corresponding to the target_freq, and programs the clock to a value <=
1075 * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1076 * provided by the opp, should have already rounded to the target OPP's
1077 * frequency.
1078 */
1079int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1080{
1081 struct opp_table *opp_table;
1082 unsigned long freq = 0, temp_freq;
1083 struct dev_pm_opp *opp = NULL;
1084 int ret;
1085
1086 opp_table = _find_opp_table(dev);
1087 if (IS_ERR(opp_table)) {
1088 dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1089 return PTR_ERR(opp_table);
1090 }
1091
1092 if (target_freq) {
1093 /*
1094 * For IO devices which require an OPP on some platforms/SoCs
1095 * while just needing to scale the clock on some others
1096 * we look for empty OPP tables with just a clock handle and
1097 * scale only the clk. This makes dev_pm_opp_set_rate()
1098 * equivalent to a clk_set_rate()
1099 */
1100 if (!_get_opp_count(opp_table)) {
1101 ret = _generic_set_opp_clk_only(dev, opp_table->clk, target_freq);
1102 goto put_opp_table;
1103 }
1104
1105 freq = clk_round_rate(opp_table->clk, target_freq);
1106 if ((long)freq <= 0)
1107 freq = target_freq;
1108
1109 /*
1110 * The clock driver may support finer resolution of the
1111 * frequencies than the OPP table, don't update the frequency we
1112 * pass to clk_set_rate() here.
1113 */
1114 temp_freq = freq;
1115 opp = _find_freq_ceil(opp_table, &temp_freq);
1116 if (IS_ERR(opp)) {
1117 ret = PTR_ERR(opp);
1118 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1119 __func__, freq, ret);
1120 goto put_opp_table;
1121 }
1122 }
1123
1124 ret = _set_opp(dev, opp_table, opp, freq);
1125
1126 if (target_freq)
1127 dev_pm_opp_put(opp);
052c6f19 1128put_opp_table:
5b650b38 1129 dev_pm_opp_put_opp_table(opp_table);
052c6f19 1130 return ret;
6a0712f6
VK
1131}
1132EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
1133
2c2709dc 1134/* OPP-dev Helpers */
2c2709dc
VK
1135static void _remove_opp_dev(struct opp_device *opp_dev,
1136 struct opp_table *opp_table)
06441658 1137{
2c2709dc
VK
1138 opp_debug_unregister(opp_dev, opp_table);
1139 list_del(&opp_dev->node);
052c6f19 1140 kfree(opp_dev);
06441658
VK
1141}
1142
ef43f01a
VK
1143struct opp_device *_add_opp_dev(const struct device *dev,
1144 struct opp_table *opp_table)
06441658 1145{
2c2709dc 1146 struct opp_device *opp_dev;
06441658 1147
2c2709dc
VK
1148 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
1149 if (!opp_dev)
06441658
VK
1150 return NULL;
1151
2c2709dc
VK
1152 /* Initialize opp-dev */
1153 opp_dev->dev = dev;
3d255699 1154
ef43f01a 1155 mutex_lock(&opp_table->lock);
052c6f19 1156 list_add(&opp_dev->node, &opp_table->dev_list);
ef43f01a 1157 mutex_unlock(&opp_table->lock);
06441658 1158
2c2709dc 1159 /* Create debugfs entries for the opp_table */
a2dea4cb 1160 opp_debug_register(opp_dev, opp_table);
283d55e6
VK
1161
1162 return opp_dev;
1163}
1164
eb7c8743 1165static struct opp_table *_allocate_opp_table(struct device *dev, int index)
07cce74a 1166{
2c2709dc
VK
1167 struct opp_table *opp_table;
1168 struct opp_device *opp_dev;
d54974c2 1169 int ret;
07cce74a
VK
1170
1171 /*
2c2709dc 1172 * Allocate a new OPP table. In the infrequent case where a new
07cce74a
VK
1173 * device is needed to be added, we pay this penalty.
1174 */
2c2709dc
VK
1175 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
1176 if (!opp_table)
dd461cd9 1177 return ERR_PTR(-ENOMEM);
07cce74a 1178
3d255699 1179 mutex_init(&opp_table->lock);
4f018bc0 1180 mutex_init(&opp_table->genpd_virt_dev_lock);
2c2709dc 1181 INIT_LIST_HEAD(&opp_table->dev_list);
06441658 1182
46f48aca
VK
1183 /* Mark regulator count uninitialized */
1184 opp_table->regulator_count = -1;
1185
2c2709dc
VK
1186 opp_dev = _add_opp_dev(dev, opp_table);
1187 if (!opp_dev) {
dd461cd9
SG
1188 ret = -ENOMEM;
1189 goto err;
06441658
VK
1190 }
1191
eb7c8743 1192 _of_init_opp_table(opp_table, dev, index);
50f8cfbd 1193
6d3f922c
GD
1194 /* Find interconnect path(s) for the device */
1195 ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
dd461cd9
SG
1196 if (ret) {
1197 if (ret == -EPROBE_DEFER)
32439ac7 1198 goto remove_opp_dev;
dd461cd9 1199
6d3f922c
GD
1200 dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
1201 __func__, ret);
dd461cd9 1202 }
6d3f922c 1203
052c6f19 1204 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
2c2709dc 1205 INIT_LIST_HEAD(&opp_table->opp_list);
f067a982 1206 kref_init(&opp_table->kref);
07cce74a 1207
2c2709dc 1208 return opp_table;
dd461cd9 1209
976509bb
QW
1210remove_opp_dev:
1211 _remove_opp_dev(opp_dev, opp_table);
dd461cd9
SG
1212err:
1213 kfree(opp_table);
1214 return ERR_PTR(ret);
07cce74a
VK
1215}
1216
f067a982 1217void _get_opp_table_kref(struct opp_table *opp_table)
b6160e26 1218{
f067a982
VK
1219 kref_get(&opp_table->kref);
1220}
1221
32439ac7
VK
1222static struct opp_table *_update_opp_table_clk(struct device *dev,
1223 struct opp_table *opp_table,
1224 bool getclk)
1225{
1226 /*
1227 * Return early if we don't need to get clk or we have already tried it
1228 * earlier.
1229 */
1230 if (!getclk || IS_ERR(opp_table) || opp_table->clk)
1231 return opp_table;
1232
1233 /* Find clk for the device */
1234 opp_table->clk = clk_get(dev, NULL);
1235 if (IS_ERR(opp_table->clk)) {
1236 int ret = PTR_ERR(opp_table->clk);
1237
1238 if (ret == -EPROBE_DEFER) {
1239 dev_pm_opp_put_opp_table(opp_table);
1240 return ERR_PTR(ret);
1241 }
1242
1243 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
1244 }
1245
1246 return opp_table;
1247}
1248
27c09484
VK
1249/*
1250 * We need to make sure that the OPP table for a device doesn't get added twice,
1251 * if this routine gets called in parallel with the same device pointer.
1252 *
1253 * The simplest way to enforce that is to perform everything (find existing
1254 * table and if not found, create a new one) under the opp_table_lock, so only
1255 * one creator gets access to the same. But that expands the critical section
1256 * under the lock and may end up causing circular dependencies with frameworks
1257 * like debugfs, interconnect or clock framework as they may be direct or
1258 * indirect users of OPP core.
1259 *
1260 * And for that reason we have to go for a bit tricky implementation here, which
1261 * uses the opp_tables_busy flag to indicate if another creator is in the middle
1262 * of adding an OPP table and others should wait for it to finish.
1263 */
32439ac7
VK
1264struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
1265 bool getclk)
f067a982
VK
1266{
1267 struct opp_table *opp_table;
1268
27c09484 1269again:
f067a982
VK
1270 mutex_lock(&opp_table_lock);
1271
5b650b38
VK
1272 opp_table = _find_opp_table_unlocked(dev);
1273 if (!IS_ERR(opp_table))
f067a982 1274 goto unlock;
f067a982 1275
27c09484
VK
1276 /*
1277 * The opp_tables list or an OPP table's dev_list is getting updated by
1278 * another user, wait for it to finish.
1279 */
1280 if (unlikely(opp_tables_busy)) {
1281 mutex_unlock(&opp_table_lock);
1282 cpu_relax();
1283 goto again;
1284 }
1285
1286 opp_tables_busy = true;
283d55e6 1287 opp_table = _managed_opp(dev, index);
27c09484
VK
1288
1289 /* Drop the lock to reduce the size of critical section */
1290 mutex_unlock(&opp_table_lock);
1291
283d55e6 1292 if (opp_table) {
ef43f01a 1293 if (!_add_opp_dev(dev, opp_table)) {
283d55e6 1294 dev_pm_opp_put_opp_table(opp_table);
dd461cd9 1295 opp_table = ERR_PTR(-ENOMEM);
283d55e6 1296 }
27c09484
VK
1297
1298 mutex_lock(&opp_table_lock);
1299 } else {
1300 opp_table = _allocate_opp_table(dev, index);
1301
1302 mutex_lock(&opp_table_lock);
1303 if (!IS_ERR(opp_table))
1304 list_add(&opp_table->node, &opp_tables);
283d55e6
VK
1305 }
1306
27c09484 1307 opp_tables_busy = false;
f067a982
VK
1308
1309unlock:
1310 mutex_unlock(&opp_table_lock);
1311
32439ac7 1312 return _update_opp_table_clk(dev, opp_table, getclk);
f067a982 1313}
eb7c8743 1314
32439ac7 1315static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
eb7c8743 1316{
32439ac7 1317 return _add_opp_table_indexed(dev, 0, getclk);
eb7c8743 1318}
f067a982 1319
e77dcb0b 1320struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
eb7c8743 1321{
e77dcb0b 1322 return _find_opp_table(dev);
eb7c8743 1323}
e77dcb0b 1324EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
eb7c8743 1325
b83c1899 1326static void _opp_table_kref_release(struct kref *kref)
f067a982
VK
1327{
1328 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
cdd6ed90 1329 struct opp_device *opp_dev, *temp;
6d3f922c 1330 int i;
b6160e26 1331
e0df59de
VK
1332 /* Drop the lock as soon as we can */
1333 list_del(&opp_table->node);
1334 mutex_unlock(&opp_table_lock);
1335
81c4d8a3
VK
1336 if (opp_table->current_opp)
1337 dev_pm_opp_put(opp_table->current_opp);
1338
5d6d106f
VK
1339 _of_clear_opp_table(opp_table);
1340
b6160e26
VK
1341 /* Release clk */
1342 if (!IS_ERR(opp_table->clk))
1343 clk_put(opp_table->clk);
1344
6d3f922c
GD
1345 if (opp_table->paths) {
1346 for (i = 0; i < opp_table->path_count; i++)
1347 icc_put(opp_table->paths[i]);
1348 kfree(opp_table->paths);
1349 }
1350
cdd6ed90 1351 WARN_ON(!list_empty(&opp_table->opp_list));
b6160e26 1352
cdd6ed90
VK
1353 list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
1354 /*
1355 * The OPP table is getting removed, drop the performance state
1356 * constraints.
1357 */
1358 if (opp_table->genpd_performance_state)
1359 dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
b6160e26 1360
cdd6ed90
VK
1361 _remove_opp_dev(opp_dev, opp_table);
1362 }
b6160e26 1363
4f018bc0 1364 mutex_destroy(&opp_table->genpd_virt_dev_lock);
37a73ec0 1365 mutex_destroy(&opp_table->lock);
052c6f19 1366 kfree(opp_table);
f067a982
VK
1367}
1368
1369void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
1370{
1371 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
1372 &opp_table_lock);
1373}
1374EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
1375
8cd2f6e8 1376void _opp_free(struct dev_pm_opp *opp)
969fceb3
VK
1377{
1378 kfree(opp);
969fceb3
VK
1379}
1380
cf1fac94 1381static void _opp_kref_release(struct kref *kref)
129eec55 1382{
cf1fac94
VK
1383 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1384 struct opp_table *opp_table = opp->opp_table;
1385
1386 list_del(&opp->node);
1387 mutex_unlock(&opp_table->lock);
1388
129eec55
VK
1389 /*
1390 * Notify the changes in the availability of the operable
1391 * frequency/voltage list.
1392 */
052c6f19 1393 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
da544b61 1394 _of_opp_free_required_opps(opp_table, opp);
deaa5146 1395 opp_debug_remove_one(opp);
052c6f19 1396 kfree(opp);
1690d8bb 1397}
129eec55 1398
a88bd2a5 1399void dev_pm_opp_get(struct dev_pm_opp *opp)
8a31d9d9
VK
1400{
1401 kref_get(&opp->kref);
1402}
1403
7034764a
VK
1404void dev_pm_opp_put(struct dev_pm_opp *opp)
1405{
cf1fac94 1406 kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
7034764a
VK
1407}
1408EXPORT_SYMBOL_GPL(dev_pm_opp_put);
1409
129eec55 1410/**
2c2709dc 1411 * dev_pm_opp_remove() - Remove an OPP from OPP table
129eec55
VK
1412 * @dev: device for which we do this operation
1413 * @freq: OPP to remove with matching 'freq'
1414 *
2c2709dc 1415 * This function removes an opp from the opp table.
129eec55
VK
1416 */
1417void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1418{
1419 struct dev_pm_opp *opp;
2c2709dc 1420 struct opp_table *opp_table;
129eec55
VK
1421 bool found = false;
1422
2c2709dc
VK
1423 opp_table = _find_opp_table(dev);
1424 if (IS_ERR(opp_table))
5b650b38 1425 return;
129eec55 1426
37a73ec0
VK
1427 mutex_lock(&opp_table->lock);
1428
2c2709dc 1429 list_for_each_entry(opp, &opp_table->opp_list, node) {
129eec55
VK
1430 if (opp->rate == freq) {
1431 found = true;
1432 break;
1433 }
1434 }
1435
37a73ec0
VK
1436 mutex_unlock(&opp_table->lock);
1437
5b650b38
VK
1438 if (found) {
1439 dev_pm_opp_put(opp);
0ad8c623
VK
1440
1441 /* Drop the reference taken by dev_pm_opp_add() */
1442 dev_pm_opp_put_opp_table(opp_table);
5b650b38 1443 } else {
129eec55
VK
1444 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1445 __func__, freq);
129eec55
VK
1446 }
1447
0ad8c623 1448 /* Drop the reference taken by _find_opp_table() */
5b650b38 1449 dev_pm_opp_put_opp_table(opp_table);
129eec55
VK
1450}
1451EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1452
cf1fac94
VK
1453static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1454 bool dynamic)
1455{
1456 struct dev_pm_opp *opp = NULL, *temp;
1457
1458 mutex_lock(&opp_table->lock);
1459 list_for_each_entry(temp, &opp_table->opp_list, node) {
1460 if (dynamic == temp->dynamic) {
1461 opp = temp;
1462 break;
1463 }
1464 }
1465
1466 mutex_unlock(&opp_table->lock);
1467 return opp;
1468}
1469
922ff075 1470bool _opp_remove_all_static(struct opp_table *opp_table)
03758d60 1471{
cf1fac94 1472 struct dev_pm_opp *opp;
03758d60
VK
1473
1474 mutex_lock(&opp_table->lock);
1475
922ff075 1476 if (!opp_table->parsed_static_opps) {
cf1fac94
VK
1477 mutex_unlock(&opp_table->lock);
1478 return false;
922ff075
VK
1479 }
1480
cf1fac94
VK
1481 if (--opp_table->parsed_static_opps) {
1482 mutex_unlock(&opp_table->lock);
1483 return true;
03758d60
VK
1484 }
1485
03758d60 1486 mutex_unlock(&opp_table->lock);
922ff075 1487
cf1fac94
VK
1488 /*
1489 * Can't remove the OPP from under the lock, debugfs removal needs to
1490 * happen lock less to avoid circular dependency issues.
1491 */
1492 while ((opp = _opp_get_next(opp_table, false)))
1493 dev_pm_opp_put(opp);
1494
1495 return true;
03758d60
VK
1496}
1497
1690d8bb
VK
1498/**
1499 * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
1500 * @dev: device for which we do this operation
1501 *
1502 * This function removes all dynamically created OPPs from the opp table.
1503 */
1504void dev_pm_opp_remove_all_dynamic(struct device *dev)
1505{
1506 struct opp_table *opp_table;
cf1fac94 1507 struct dev_pm_opp *opp;
1690d8bb
VK
1508 int count = 0;
1509
1510 opp_table = _find_opp_table(dev);
1511 if (IS_ERR(opp_table))
1512 return;
1513
cf1fac94
VK
1514 /*
1515 * Can't remove the OPP from under the lock, debugfs removal needs to
1516 * happen lock less to avoid circular dependency issues.
1517 */
1518 while ((opp = _opp_get_next(opp_table, true))) {
1519 dev_pm_opp_put(opp);
1520 count++;
1690d8bb 1521 }
1690d8bb
VK
1522
1523 /* Drop the references taken by dev_pm_opp_add() */
1524 while (count--)
1525 dev_pm_opp_put_opp_table(opp_table);
1526
1527 /* Drop the reference taken by _find_opp_table() */
1528 dev_pm_opp_put_opp_table(opp_table);
1529}
1530EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
1531
8cd2f6e8 1532struct dev_pm_opp *_opp_allocate(struct opp_table *table)
e1f60b29 1533{
23dacf6d 1534 struct dev_pm_opp *opp;
6d3f922c 1535 int supply_count, supply_size, icc_size;
e1f60b29 1536
dfbe4678 1537 /* Allocate space for at least one supply */
6d3f922c
GD
1538 supply_count = table->regulator_count > 0 ? table->regulator_count : 1;
1539 supply_size = sizeof(*opp->supplies) * supply_count;
1540 icc_size = sizeof(*opp->bandwidth) * table->path_count;
e1f60b29 1541
dfbe4678 1542 /* allocate new OPP node and supplies structures */
6d3f922c
GD
1543 opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL);
1544
8cd2f6e8 1545 if (!opp)
23dacf6d 1546 return NULL;
23dacf6d 1547
dfbe4678
VK
1548 /* Put the supplies at the end of the OPP structure as an empty array */
1549 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
6d3f922c
GD
1550 if (icc_size)
1551 opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count);
dfbe4678
VK
1552 INIT_LIST_HEAD(&opp->node);
1553
23dacf6d
VK
1554 return opp;
1555}
1556
7d34d56e 1557static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
2c2709dc 1558 struct opp_table *opp_table)
7d34d56e 1559{
dfbe4678
VK
1560 struct regulator *reg;
1561 int i;
1562
90e3577b
VK
1563 if (!opp_table->regulators)
1564 return true;
1565
dfbe4678
VK
1566 for (i = 0; i < opp_table->regulator_count; i++) {
1567 reg = opp_table->regulators[i];
1568
1569 if (!regulator_is_supported_voltage(reg,
1570 opp->supplies[i].u_volt_min,
1571 opp->supplies[i].u_volt_max)) {
1572 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1573 __func__, opp->supplies[i].u_volt_min,
1574 opp->supplies[i].u_volt_max);
1575 return false;
1576 }
7d34d56e
VK
1577 }
1578
1579 return true;
1580}
1581
6c591eec
SK
1582int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
1583{
1584 if (opp1->rate != opp2->rate)
1585 return opp1->rate < opp2->rate ? -1 : 1;
6d3f922c
GD
1586 if (opp1->bandwidth && opp2->bandwidth &&
1587 opp1->bandwidth[0].peak != opp2->bandwidth[0].peak)
1588 return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1;
6c591eec
SK
1589 if (opp1->level != opp2->level)
1590 return opp1->level < opp2->level ? -1 : 1;
1591 return 0;
1592}
1593
a1e8c136
VK
1594static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1595 struct opp_table *opp_table,
1596 struct list_head **head)
23dacf6d
VK
1597{
1598 struct dev_pm_opp *opp;
6c591eec 1599 int opp_cmp;
23dacf6d
VK
1600
1601 /*
1602 * Insert new OPP in order of increasing frequency and discard if
1603 * already present.
1604 *
2c2709dc 1605 * Need to use &opp_table->opp_list in the condition part of the 'for'
23dacf6d
VK
1606 * loop, don't replace it with head otherwise it will become an infinite
1607 * loop.
1608 */
052c6f19 1609 list_for_each_entry(opp, &opp_table->opp_list, node) {
6c591eec
SK
1610 opp_cmp = _opp_compare_key(new_opp, opp);
1611 if (opp_cmp > 0) {
a1e8c136 1612 *head = &opp->node;
23dacf6d
VK
1613 continue;
1614 }
1615
6c591eec 1616 if (opp_cmp < 0)
a1e8c136 1617 return 0;
23dacf6d
VK
1618
1619 /* Duplicate OPPs */
06441658 1620 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
dfbe4678
VK
1621 __func__, opp->rate, opp->supplies[0].u_volt,
1622 opp->available, new_opp->rate,
1623 new_opp->supplies[0].u_volt, new_opp->available);
23dacf6d 1624
dfbe4678 1625 /* Should we compare voltages for all regulators here ? */
a1e8c136
VK
1626 return opp->available &&
1627 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1628 }
1629
1630 return 0;
1631}
1632
1633/*
1634 * Returns:
1635 * 0: On success. And appropriate error message for duplicate OPPs.
1636 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1637 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1638 * sure we don't print error messages unnecessarily if different parts of
1639 * kernel try to initialize the OPP table.
1640 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1641 * should be considered an error by the callers of _opp_add().
1642 */
1643int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1644 struct opp_table *opp_table, bool rate_not_available)
1645{
1646 struct list_head *head;
cf65948d 1647 unsigned int i;
a1e8c136
VK
1648 int ret;
1649
1650 mutex_lock(&opp_table->lock);
1651 head = &opp_table->opp_list;
37a73ec0 1652
32715be4
DO
1653 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1654 if (ret) {
1655 mutex_unlock(&opp_table->lock);
1656 return ret;
23dacf6d
VK
1657 }
1658
052c6f19 1659 list_add(&new_opp->node, head);
37a73ec0
VK
1660 mutex_unlock(&opp_table->lock);
1661
1662 new_opp->opp_table = opp_table;
7034764a 1663 kref_init(&new_opp->kref);
23dacf6d 1664
a2dea4cb 1665 opp_debug_create_one(new_opp, opp_table);
deaa5146 1666
2c2709dc 1667 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
7d34d56e
VK
1668 new_opp->available = false;
1669 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1670 __func__, new_opp->rate);
1671 }
1672
cf65948d
DO
1673 for (i = 0; i < opp_table->required_opp_count; i++) {
1674 if (new_opp->required_opps[i]->available)
1675 continue;
1676
1677 new_opp->available = false;
1678 dev_warn(dev, "%s: OPP not supported by required OPP %pOF (%lu)\n",
1679 __func__, new_opp->required_opps[i]->np, new_opp->rate);
1680 break;
1681 }
1682
23dacf6d
VK
1683 return 0;
1684}
1685
984f16c8 1686/**
b64b9c3f 1687 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
8cd2f6e8 1688 * @opp_table: OPP table
984f16c8
NM
1689 * @dev: device for which we do this operation
1690 * @freq: Frequency in Hz for this OPP
1691 * @u_volt: Voltage in uVolts for this OPP
1692 * @dynamic: Dynamically added OPPs.
1693 *
2c2709dc 1694 * This function adds an opp definition to the opp table and returns status.
984f16c8
NM
1695 * The opp is made available by default and it can be controlled using
1696 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1697 *
8f8d37b2
VK
1698 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1699 * and freed by dev_pm_opp_of_remove_table.
984f16c8 1700 *
984f16c8
NM
1701 * Return:
1702 * 0 On success OR
1703 * Duplicate OPPs (both freq and volt are same) and opp->available
1704 * -EEXIST Freq are same and volt are different OR
1705 * Duplicate OPPs (both freq and volt are same) and !opp->available
1706 * -ENOMEM Memory allocation failure
1707 */
8cd2f6e8
VK
1708int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1709 unsigned long freq, long u_volt, bool dynamic)
e1f60b29 1710{
23dacf6d 1711 struct dev_pm_opp *new_opp;
50f8cfbd 1712 unsigned long tol;
6ce4184d 1713 int ret;
e1f60b29 1714
8cd2f6e8
VK
1715 new_opp = _opp_allocate(opp_table);
1716 if (!new_opp)
1717 return -ENOMEM;
23dacf6d 1718
a7470db6 1719 /* populate the opp table */
a7470db6 1720 new_opp->rate = freq;
2c2709dc 1721 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
dfbe4678
VK
1722 new_opp->supplies[0].u_volt = u_volt;
1723 new_opp->supplies[0].u_volt_min = u_volt - tol;
1724 new_opp->supplies[0].u_volt_max = u_volt + tol;
a7470db6 1725 new_opp->available = true;
23dacf6d 1726 new_opp->dynamic = dynamic;
a7470db6 1727
a1e8c136 1728 ret = _opp_add(dev, new_opp, opp_table, false);
7f8538eb
VK
1729 if (ret) {
1730 /* Don't return error for duplicate OPPs */
1731 if (ret == -EBUSY)
1732 ret = 0;
6ce4184d 1733 goto free_opp;
7f8538eb 1734 }
64ce8545 1735
03ca370f
MH
1736 /*
1737 * Notify the changes in the availability of the operable
1738 * frequency/voltage list.
1739 */
052c6f19 1740 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
e1f60b29 1741 return 0;
6ce4184d
VK
1742
1743free_opp:
8cd2f6e8
VK
1744 _opp_free(new_opp);
1745
6ce4184d 1746 return ret;
e1f60b29 1747}
38393409 1748
7de36b0a
VK
1749/**
1750 * dev_pm_opp_set_supported_hw() - Set supported platforms
1751 * @dev: Device for which supported-hw has to be set.
1752 * @versions: Array of hierarchy of versions to match.
1753 * @count: Number of elements in the array.
1754 *
1755 * This is required only for the V2 bindings, and it enables a platform to
1756 * specify the hierarchy of versions it supports. OPP layer will then enable
1757 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1758 * property.
7de36b0a 1759 */
fa30184d
VK
1760struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1761 const u32 *versions, unsigned int count)
7de36b0a 1762{
2c2709dc 1763 struct opp_table *opp_table;
7de36b0a 1764
32439ac7 1765 opp_table = _add_opp_table(dev, false);
dd461cd9
SG
1766 if (IS_ERR(opp_table))
1767 return opp_table;
7de36b0a 1768
2c2709dc
VK
1769 /* Make sure there are no concurrent readers while updating opp_table */
1770 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1771
25419de1
VK
1772 /* Another CPU that shares the OPP table has set the property ? */
1773 if (opp_table->supported_hw)
1774 return opp_table;
7de36b0a 1775
2c2709dc 1776 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
7de36b0a 1777 GFP_KERNEL);
2c2709dc 1778 if (!opp_table->supported_hw) {
25419de1
VK
1779 dev_pm_opp_put_opp_table(opp_table);
1780 return ERR_PTR(-ENOMEM);
7de36b0a
VK
1781 }
1782
2c2709dc 1783 opp_table->supported_hw_count = count;
fa30184d
VK
1784
1785 return opp_table;
7de36b0a
VK
1786}
1787EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1788
1789/**
1790 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
fa30184d 1791 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
7de36b0a
VK
1792 *
1793 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1794 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
7de36b0a 1795 * will not be freed.
7de36b0a 1796 */
fa30184d 1797void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
7de36b0a 1798{
c7bf8758
VK
1799 if (unlikely(!opp_table))
1800 return;
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
2c2709dc
VK
1805 kfree(opp_table->supported_hw);
1806 opp_table->supported_hw = NULL;
1807 opp_table->supported_hw_count = 0;
7de36b0a 1808
fa30184d 1809 dev_pm_opp_put_opp_table(opp_table);
7de36b0a
VK
1810}
1811EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1812
01fb4d3c
VK
1813/**
1814 * dev_pm_opp_set_prop_name() - Set prop-extn name
a5da6447 1815 * @dev: Device for which the prop-name has to be set.
01fb4d3c
VK
1816 * @name: name to postfix to properties.
1817 *
1818 * This is required only for the V2 bindings, and it enables a platform to
1819 * specify the extn to be used for certain property names. The properties to
1820 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1821 * should postfix the property name with -<name> while looking for them.
01fb4d3c 1822 */
fa30184d 1823struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
01fb4d3c 1824{
2c2709dc 1825 struct opp_table *opp_table;
01fb4d3c 1826
32439ac7 1827 opp_table = _add_opp_table(dev, false);
dd461cd9
SG
1828 if (IS_ERR(opp_table))
1829 return opp_table;
01fb4d3c 1830
2c2709dc
VK
1831 /* Make sure there are no concurrent readers while updating opp_table */
1832 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1833
878ec1a9
VK
1834 /* Another CPU that shares the OPP table has set the property ? */
1835 if (opp_table->prop_name)
1836 return opp_table;
01fb4d3c 1837
2c2709dc
VK
1838 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1839 if (!opp_table->prop_name) {
878ec1a9
VK
1840 dev_pm_opp_put_opp_table(opp_table);
1841 return ERR_PTR(-ENOMEM);
01fb4d3c
VK
1842 }
1843
fa30184d 1844 return opp_table;
01fb4d3c
VK
1845}
1846EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1847
1848/**
1849 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
fa30184d 1850 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
01fb4d3c
VK
1851 *
1852 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1853 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
01fb4d3c 1854 * will not be freed.
01fb4d3c 1855 */
fa30184d 1856void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
01fb4d3c 1857{
c7bf8758
VK
1858 if (unlikely(!opp_table))
1859 return;
1860
2c2709dc
VK
1861 /* Make sure there are no concurrent readers while updating opp_table */
1862 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1863
2c2709dc
VK
1864 kfree(opp_table->prop_name);
1865 opp_table->prop_name = NULL;
01fb4d3c 1866
fa30184d 1867 dev_pm_opp_put_opp_table(opp_table);
01fb4d3c
VK
1868}
1869EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1870
9f8ea969 1871/**
dfbe4678 1872 * dev_pm_opp_set_regulators() - Set regulator names for the device
9f8ea969 1873 * @dev: Device for which regulator name is being set.
dfbe4678
VK
1874 * @names: Array of pointers to the names of the regulator.
1875 * @count: Number of regulators.
9f8ea969
VK
1876 *
1877 * In order to support OPP switching, OPP layer needs to know the name of the
dfbe4678
VK
1878 * device's regulators, as the core would be required to switch voltages as
1879 * well.
9f8ea969
VK
1880 *
1881 * This must be called before any OPPs are initialized for the device.
9f8ea969 1882 */
dfbe4678
VK
1883struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1884 const char * const names[],
1885 unsigned int count)
9f8ea969 1886{
38bb3439 1887 struct dev_pm_opp_supply *supplies;
2c2709dc 1888 struct opp_table *opp_table;
9f8ea969 1889 struct regulator *reg;
dfbe4678 1890 int ret, i;
9f8ea969 1891
32439ac7 1892 opp_table = _add_opp_table(dev, false);
dd461cd9
SG
1893 if (IS_ERR(opp_table))
1894 return opp_table;
9f8ea969
VK
1895
1896 /* This should be called before OPPs are initialized */
2c2709dc 1897 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
9f8ea969
VK
1898 ret = -EBUSY;
1899 goto err;
1900 }
1901
779b783c
VK
1902 /* Another CPU that shares the OPP table has set the regulators ? */
1903 if (opp_table->regulators)
1904 return opp_table;
dfbe4678
VK
1905
1906 opp_table->regulators = kmalloc_array(count,
1907 sizeof(*opp_table->regulators),
1908 GFP_KERNEL);
1909 if (!opp_table->regulators) {
1910 ret = -ENOMEM;
9f8ea969
VK
1911 goto err;
1912 }
1913
dfbe4678
VK
1914 for (i = 0; i < count; i++) {
1915 reg = regulator_get_optional(dev, names[i]);
1916 if (IS_ERR(reg)) {
1917 ret = PTR_ERR(reg);
1918 if (ret != -EPROBE_DEFER)
1919 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1920 __func__, names[i], ret);
1921 goto free_regulators;
1922 }
1923
1924 opp_table->regulators[i] = reg;
1925 }
1926
1927 opp_table->regulator_count = count;
9f8ea969 1928
38bb3439
VK
1929 supplies = kmalloc_array(count * 2, sizeof(*supplies), GFP_KERNEL);
1930 if (!supplies) {
1931 ret = -ENOMEM;
94735585 1932 goto free_regulators;
38bb3439
VK
1933 }
1934
1935 mutex_lock(&opp_table->lock);
1936 opp_table->sod_supplies = supplies;
1937 if (opp_table->set_opp_data) {
1938 opp_table->set_opp_data->old_opp.supplies = supplies;
1939 opp_table->set_opp_data->new_opp.supplies = supplies + count;
1940 }
1941 mutex_unlock(&opp_table->lock);
94735585 1942
91291d9a 1943 return opp_table;
9f8ea969 1944
dfbe4678 1945free_regulators:
24957db1
MS
1946 while (i != 0)
1947 regulator_put(opp_table->regulators[--i]);
dfbe4678
VK
1948
1949 kfree(opp_table->regulators);
1950 opp_table->regulators = NULL;
46f48aca 1951 opp_table->regulator_count = -1;
9f8ea969 1952err:
fa30184d 1953 dev_pm_opp_put_opp_table(opp_table);
9f8ea969 1954
91291d9a 1955 return ERR_PTR(ret);
9f8ea969 1956}
dfbe4678 1957EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
9f8ea969
VK
1958
1959/**
dfbe4678
VK
1960 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1961 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
9f8ea969 1962 */
dfbe4678 1963void dev_pm_opp_put_regulators(struct opp_table *opp_table)
9f8ea969 1964{
dfbe4678
VK
1965 int i;
1966
c7bf8758
VK
1967 if (unlikely(!opp_table))
1968 return;
1969
779b783c
VK
1970 if (!opp_table->regulators)
1971 goto put_opp_table;
9f8ea969 1972
2c2709dc
VK
1973 /* Make sure there are no concurrent readers while updating opp_table */
1974 WARN_ON(!list_empty(&opp_table->opp_list));
9f8ea969 1975
72f80ce4 1976 if (opp_table->enabled) {
8d45719c
KK
1977 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1978 regulator_disable(opp_table->regulators[i]);
8d45719c
KK
1979 }
1980
24957db1 1981 for (i = opp_table->regulator_count - 1; i >= 0; i--)
dfbe4678
VK
1982 regulator_put(opp_table->regulators[i]);
1983
38bb3439
VK
1984 mutex_lock(&opp_table->lock);
1985 if (opp_table->set_opp_data) {
1986 opp_table->set_opp_data->old_opp.supplies = NULL;
1987 opp_table->set_opp_data->new_opp.supplies = NULL;
1988 }
1989
1990 kfree(opp_table->sod_supplies);
1991 opp_table->sod_supplies = NULL;
1992 mutex_unlock(&opp_table->lock);
94735585 1993
dfbe4678
VK
1994 kfree(opp_table->regulators);
1995 opp_table->regulators = NULL;
46f48aca 1996 opp_table->regulator_count = -1;
9f8ea969 1997
779b783c 1998put_opp_table:
fa30184d 1999 dev_pm_opp_put_opp_table(opp_table);
9f8ea969 2000}
dfbe4678 2001EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
9f8ea969 2002
829a4e8c
VK
2003/**
2004 * dev_pm_opp_set_clkname() - Set clk name for the device
2005 * @dev: Device for which clk name is being set.
2006 * @name: Clk name.
2007 *
2008 * In order to support OPP switching, OPP layer needs to get pointer to the
2009 * clock for the device. Simple cases work fine without using this routine (i.e.
2010 * by passing connection-id as NULL), but for a device with multiple clocks
2011 * available, the OPP core needs to know the exact name of the clk to use.
2012 *
2013 * This must be called before any OPPs are initialized for the device.
2014 */
2015struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
2016{
2017 struct opp_table *opp_table;
2018 int ret;
2019
32439ac7 2020 opp_table = _add_opp_table(dev, false);
dd461cd9
SG
2021 if (IS_ERR(opp_table))
2022 return opp_table;
829a4e8c
VK
2023
2024 /* This should be called before OPPs are initialized */
2025 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
2026 ret = -EBUSY;
2027 goto err;
2028 }
2029
32439ac7
VK
2030 /* clk shouldn't be initialized at this point */
2031 if (WARN_ON(opp_table->clk)) {
2032 ret = -EBUSY;
2033 goto err;
2034 }
829a4e8c
VK
2035
2036 /* Find clk for the device */
2037 opp_table->clk = clk_get(dev, name);
2038 if (IS_ERR(opp_table->clk)) {
2039 ret = PTR_ERR(opp_table->clk);
2040 if (ret != -EPROBE_DEFER) {
2041 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
2042 ret);
2043 }
2044 goto err;
2045 }
2046
2047 return opp_table;
2048
2049err:
2050 dev_pm_opp_put_opp_table(opp_table);
2051
2052 return ERR_PTR(ret);
2053}
2054EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
2055
2056/**
2057 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
2058 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
2059 */
2060void dev_pm_opp_put_clkname(struct opp_table *opp_table)
2061{
c7bf8758
VK
2062 if (unlikely(!opp_table))
2063 return;
2064
829a4e8c
VK
2065 /* Make sure there are no concurrent readers while updating opp_table */
2066 WARN_ON(!list_empty(&opp_table->opp_list));
2067
2068 clk_put(opp_table->clk);
2069 opp_table->clk = ERR_PTR(-EINVAL);
2070
2071 dev_pm_opp_put_opp_table(opp_table);
2072}
2073EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
2074
4dab160e
VK
2075/**
2076 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
2077 * @dev: Device for which the helper is getting registered.
2078 * @set_opp: Custom set OPP helper.
2079 *
2080 * This is useful to support complex platforms (like platforms with multiple
2081 * regulators per device), instead of the generic OPP set rate helper.
2082 *
2083 * This must be called before any OPPs are initialized for the device.
4dab160e 2084 */
fa30184d 2085struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
4dab160e
VK
2086 int (*set_opp)(struct dev_pm_set_opp_data *data))
2087{
38bb3439 2088 struct dev_pm_set_opp_data *data;
4dab160e 2089 struct opp_table *opp_table;
4dab160e
VK
2090
2091 if (!set_opp)
fa30184d 2092 return ERR_PTR(-EINVAL);
4dab160e 2093
32439ac7 2094 opp_table = _add_opp_table(dev, false);
47efcbcb 2095 if (IS_ERR(opp_table))
dd461cd9 2096 return opp_table;
4dab160e
VK
2097
2098 /* This should be called before OPPs are initialized */
2099 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
5019acc6
VK
2100 dev_pm_opp_put_opp_table(opp_table);
2101 return ERR_PTR(-EBUSY);
4dab160e
VK
2102 }
2103
5019acc6 2104 /* Another CPU that shares the OPP table has set the helper ? */
38bb3439
VK
2105 if (opp_table->set_opp)
2106 return opp_table;
2107
2108 data = kzalloc(sizeof(*data), GFP_KERNEL);
2109 if (!data)
2110 return ERR_PTR(-ENOMEM);
2111
2112 mutex_lock(&opp_table->lock);
2113 opp_table->set_opp_data = data;
2114 if (opp_table->sod_supplies) {
2115 data->old_opp.supplies = opp_table->sod_supplies;
2116 data->new_opp.supplies = opp_table->sod_supplies +
2117 opp_table->regulator_count;
2118 }
2119 mutex_unlock(&opp_table->lock);
2120
2121 opp_table->set_opp = set_opp;
4dab160e 2122
fa30184d 2123 return opp_table;
4dab160e
VK
2124}
2125EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
2126
2127/**
604a7aeb 2128 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
4dab160e 2129 * set_opp helper
fa30184d 2130 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
4dab160e 2131 *
fa30184d 2132 * Release resources blocked for platform specific set_opp helper.
4dab160e 2133 */
604a7aeb 2134void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
4dab160e 2135{
c7bf8758
VK
2136 if (unlikely(!opp_table))
2137 return;
2138
4dab160e
VK
2139 /* Make sure there are no concurrent readers while updating opp_table */
2140 WARN_ON(!list_empty(&opp_table->opp_list));
2141
2142 opp_table->set_opp = NULL;
38bb3439
VK
2143
2144 mutex_lock(&opp_table->lock);
2145 kfree(opp_table->set_opp_data);
2146 opp_table->set_opp_data = NULL;
2147 mutex_unlock(&opp_table->lock);
2148
fa30184d 2149 dev_pm_opp_put_opp_table(opp_table);
4dab160e 2150}
604a7aeb 2151EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
4dab160e 2152
a3c47af6
DO
2153static void devm_pm_opp_unregister_set_opp_helper(void *data)
2154{
2155 dev_pm_opp_unregister_set_opp_helper(data);
2156}
2157
2158/**
2159 * devm_pm_opp_register_set_opp_helper() - Register custom set OPP helper
2160 * @dev: Device for which the helper is getting registered.
2161 * @set_opp: Custom set OPP helper.
2162 *
2163 * This is a resource-managed version of dev_pm_opp_register_set_opp_helper().
2164 *
2165 * Return: pointer to 'struct opp_table' on success and errorno otherwise.
2166 */
2167struct opp_table *
2168devm_pm_opp_register_set_opp_helper(struct device *dev,
2169 int (*set_opp)(struct dev_pm_set_opp_data *data))
2170{
2171 struct opp_table *opp_table;
2172 int err;
2173
2174 opp_table = dev_pm_opp_register_set_opp_helper(dev, set_opp);
2175 if (IS_ERR(opp_table))
2176 return opp_table;
2177
2178 err = devm_add_action_or_reset(dev, devm_pm_opp_unregister_set_opp_helper,
2179 opp_table);
2180 if (err)
2181 return ERR_PTR(err);
2182
2183 return opp_table;
2184}
2185EXPORT_SYMBOL_GPL(devm_pm_opp_register_set_opp_helper);
2186
6319aee1
VK
2187static void _opp_detach_genpd(struct opp_table *opp_table)
2188{
2189 int index;
2190
cb60e960
VK
2191 if (!opp_table->genpd_virt_devs)
2192 return;
2193
6319aee1
VK
2194 for (index = 0; index < opp_table->required_opp_count; index++) {
2195 if (!opp_table->genpd_virt_devs[index])
2196 continue;
2197
2198 dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
2199 opp_table->genpd_virt_devs[index] = NULL;
2200 }
c0ab9e08
VK
2201
2202 kfree(opp_table->genpd_virt_devs);
2203 opp_table->genpd_virt_devs = NULL;
6319aee1
VK
2204}
2205
4f018bc0 2206/**
6319aee1
VK
2207 * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
2208 * @dev: Consumer device for which the genpd is getting attached.
2209 * @names: Null terminated array of pointers containing names of genpd to attach.
17a8f868 2210 * @virt_devs: Pointer to return the array of virtual devices.
4f018bc0
VK
2211 *
2212 * Multiple generic power domains for a device are supported with the help of
2213 * virtual genpd devices, which are created for each consumer device - genpd
2214 * pair. These are the device structures which are attached to the power domain
2215 * and are required by the OPP core to set the performance state of the genpd.
6319aee1
VK
2216 * The same API also works for the case where single genpd is available and so
2217 * we don't need to support that separately.
4f018bc0
VK
2218 *
2219 * This helper will normally be called by the consumer driver of the device
6319aee1 2220 * "dev", as only that has details of the genpd names.
4f018bc0 2221 *
6319aee1
VK
2222 * This helper needs to be called once with a list of all genpd to attach.
2223 * Otherwise the original device structure will be used instead by the OPP core.
baea35e4
VK
2224 *
2225 * The order of entries in the names array must match the order in which
2226 * "required-opps" are added in DT.
4f018bc0 2227 */
17a8f868
VK
2228struct opp_table *dev_pm_opp_attach_genpd(struct device *dev,
2229 const char **names, struct device ***virt_devs)
4f018bc0
VK
2230{
2231 struct opp_table *opp_table;
6319aee1 2232 struct device *virt_dev;
baea35e4 2233 int index = 0, ret = -EINVAL;
6319aee1 2234 const char **name = names;
4f018bc0 2235
32439ac7 2236 opp_table = _add_opp_table(dev, false);
dd461cd9
SG
2237 if (IS_ERR(opp_table))
2238 return opp_table;
4f018bc0 2239
cb60e960
VK
2240 if (opp_table->genpd_virt_devs)
2241 return opp_table;
4f018bc0 2242
6319aee1
VK
2243 /*
2244 * If the genpd's OPP table isn't already initialized, parsing of the
2245 * required-opps fail for dev. We should retry this after genpd's OPP
2246 * table is added.
2247 */
2248 if (!opp_table->required_opp_count) {
2249 ret = -EPROBE_DEFER;
2250 goto put_table;
2251 }
2252
4f018bc0
VK
2253 mutex_lock(&opp_table->genpd_virt_dev_lock);
2254
c0ab9e08
VK
2255 opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2256 sizeof(*opp_table->genpd_virt_devs),
2257 GFP_KERNEL);
2258 if (!opp_table->genpd_virt_devs)
2259 goto unlock;
4f018bc0 2260
6319aee1 2261 while (*name) {
6319aee1
VK
2262 if (index >= opp_table->required_opp_count) {
2263 dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
2264 *name, opp_table->required_opp_count, index);
2265 goto err;
2266 }
4f018bc0 2267
6319aee1
VK
2268 virt_dev = dev_pm_domain_attach_by_name(dev, *name);
2269 if (IS_ERR(virt_dev)) {
2270 ret = PTR_ERR(virt_dev);
2271 dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
2272 goto err;
2273 }
2274
2275 opp_table->genpd_virt_devs[index] = virt_dev;
baea35e4 2276 index++;
6319aee1 2277 name++;
4f018bc0
VK
2278 }
2279
17a8f868
VK
2280 if (virt_devs)
2281 *virt_devs = opp_table->genpd_virt_devs;
4f018bc0
VK
2282 mutex_unlock(&opp_table->genpd_virt_dev_lock);
2283
2284 return opp_table;
6319aee1
VK
2285
2286err:
2287 _opp_detach_genpd(opp_table);
c0ab9e08 2288unlock:
6319aee1
VK
2289 mutex_unlock(&opp_table->genpd_virt_dev_lock);
2290
2291put_table:
2292 dev_pm_opp_put_opp_table(opp_table);
2293
2294 return ERR_PTR(ret);
4f018bc0 2295}
6319aee1 2296EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd);
4f018bc0
VK
2297
2298/**
6319aee1
VK
2299 * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device.
2300 * @opp_table: OPP table returned by dev_pm_opp_attach_genpd().
4f018bc0 2301 *
6319aee1
VK
2302 * This detaches the genpd(s), resets the virtual device pointers, and puts the
2303 * OPP table.
4f018bc0 2304 */
6319aee1 2305void dev_pm_opp_detach_genpd(struct opp_table *opp_table)
4f018bc0 2306{
c7bf8758
VK
2307 if (unlikely(!opp_table))
2308 return;
2309
4f018bc0
VK
2310 /*
2311 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
2312 * used in parallel.
2313 */
2314 mutex_lock(&opp_table->genpd_virt_dev_lock);
6319aee1 2315 _opp_detach_genpd(opp_table);
4f018bc0
VK
2316 mutex_unlock(&opp_table->genpd_virt_dev_lock);
2317
6319aee1 2318 dev_pm_opp_put_opp_table(opp_table);
4f018bc0 2319}
6319aee1 2320EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd);
4f018bc0 2321
b4b9e223
DO
2322static void devm_pm_opp_detach_genpd(void *data)
2323{
2324 dev_pm_opp_detach_genpd(data);
2325}
2326
2327/**
2328 * devm_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual
2329 * device pointer
2330 * @dev: Consumer device for which the genpd is getting attached.
2331 * @names: Null terminated array of pointers containing names of genpd to attach.
2332 * @virt_devs: Pointer to return the array of virtual devices.
2333 *
2334 * This is a resource-managed version of dev_pm_opp_attach_genpd().
2335 *
2336 * Return: pointer to 'struct opp_table' on success and errorno otherwise.
2337 */
2338struct opp_table *
2339devm_pm_opp_attach_genpd(struct device *dev, const char **names,
2340 struct device ***virt_devs)
2341{
2342 struct opp_table *opp_table;
2343 int err;
2344
2345 opp_table = dev_pm_opp_attach_genpd(dev, names, virt_devs);
2346 if (IS_ERR(opp_table))
2347 return opp_table;
2348
2349 err = devm_add_action_or_reset(dev, devm_pm_opp_detach_genpd,
2350 opp_table);
2351 if (err)
2352 return ERR_PTR(err);
2353
2354 return opp_table;
2355}
2356EXPORT_SYMBOL_GPL(devm_pm_opp_attach_genpd);
2357
c8a59103
VK
2358/**
2359 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2360 * @src_table: OPP table which has dst_table as one of its required OPP table.
2361 * @dst_table: Required OPP table of the src_table.
2362 * @pstate: Current performance state of the src_table.
2363 *
2364 * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2365 * "required-opps" property of the OPP (present in @src_table) which has
2366 * performance state set to @pstate.
2367 *
2368 * Return: Zero or positive performance state on success, otherwise negative
2369 * value on errors.
2370 */
2371int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2372 struct opp_table *dst_table,
2373 unsigned int pstate)
2374{
2375 struct dev_pm_opp *opp;
2376 int dest_pstate = -EINVAL;
2377 int i;
2378
c8a59103
VK
2379 /*
2380 * Normally the src_table will have the "required_opps" property set to
2381 * point to one of the OPPs in the dst_table, but in some cases the
2382 * genpd and its master have one to one mapping of performance states
2383 * and so none of them have the "required-opps" property set. Return the
2384 * pstate of the src_table as it is in such cases.
2385 */
f2f4d2b8 2386 if (!src_table || !src_table->required_opp_count)
c8a59103
VK
2387 return pstate;
2388
2389 for (i = 0; i < src_table->required_opp_count; i++) {
2390 if (src_table->required_opp_tables[i]->np == dst_table->np)
2391 break;
2392 }
2393
2394 if (unlikely(i == src_table->required_opp_count)) {
2395 pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2396 __func__, src_table, dst_table);
2397 return -EINVAL;
2398 }
2399
2400 mutex_lock(&src_table->lock);
2401
2402 list_for_each_entry(opp, &src_table->opp_list, node) {
2403 if (opp->pstate == pstate) {
2404 dest_pstate = opp->required_opps[i]->pstate;
2405 goto unlock;
2406 }
2407 }
2408
2409 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2410 dst_table);
2411
2412unlock:
2413 mutex_unlock(&src_table->lock);
2414
2415 return dest_pstate;
2416}
2417
38393409
VK
2418/**
2419 * dev_pm_opp_add() - Add an OPP table from a table definitions
2420 * @dev: device for which we do this operation
2421 * @freq: Frequency in Hz for this OPP
2422 * @u_volt: Voltage in uVolts for this OPP
2423 *
2c2709dc 2424 * This function adds an opp definition to the opp table and returns status.
38393409
VK
2425 * The opp is made available by default and it can be controlled using
2426 * dev_pm_opp_enable/disable functions.
2427 *
38393409 2428 * Return:
984f16c8 2429 * 0 On success OR
38393409 2430 * Duplicate OPPs (both freq and volt are same) and opp->available
984f16c8 2431 * -EEXIST Freq are same and volt are different OR
38393409 2432 * Duplicate OPPs (both freq and volt are same) and !opp->available
984f16c8 2433 * -ENOMEM Memory allocation failure
38393409
VK
2434 */
2435int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
2436{
8cd2f6e8
VK
2437 struct opp_table *opp_table;
2438 int ret;
2439
32439ac7 2440 opp_table = _add_opp_table(dev, true);
dd461cd9
SG
2441 if (IS_ERR(opp_table))
2442 return PTR_ERR(opp_table);
8cd2f6e8 2443
46f48aca
VK
2444 /* Fix regulator count for dynamic OPPs */
2445 opp_table->regulator_count = 1;
2446
8cd2f6e8 2447 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
0ad8c623
VK
2448 if (ret)
2449 dev_pm_opp_put_opp_table(opp_table);
8cd2f6e8 2450
8cd2f6e8 2451 return ret;
38393409 2452}
5d4879cd 2453EXPORT_SYMBOL_GPL(dev_pm_opp_add);
e1f60b29
NM
2454
2455/**
327854c8 2456 * _opp_set_availability() - helper to set the availability of an opp
e1f60b29
NM
2457 * @dev: device for which we do this operation
2458 * @freq: OPP frequency to modify availability
2459 * @availability_req: availability status requested for this opp
2460 *
052c6f19
VK
2461 * Set the availability of an OPP, opp_{enable,disable} share a common logic
2462 * which is isolated here.
e1f60b29 2463 *
984f16c8 2464 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 2465 * copy operation, returns 0 if no modification was done OR modification was
e1f60b29 2466 * successful.
e1f60b29 2467 */
327854c8
NM
2468static int _opp_set_availability(struct device *dev, unsigned long freq,
2469 bool availability_req)
e1f60b29 2470{
2c2709dc 2471 struct opp_table *opp_table;
a7f3987e 2472 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
e1f60b29
NM
2473 int r = 0;
2474
2c2709dc
VK
2475 /* Find the opp_table */
2476 opp_table = _find_opp_table(dev);
2477 if (IS_ERR(opp_table)) {
2478 r = PTR_ERR(opp_table);
e1f60b29 2479 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
a7f3987e 2480 return r;
e1f60b29
NM
2481 }
2482
37a73ec0
VK
2483 mutex_lock(&opp_table->lock);
2484
e1f60b29 2485 /* Do we have the frequency? */
2c2709dc 2486 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
2487 if (tmp_opp->rate == freq) {
2488 opp = tmp_opp;
2489 break;
2490 }
2491 }
37a73ec0 2492
e1f60b29
NM
2493 if (IS_ERR(opp)) {
2494 r = PTR_ERR(opp);
2495 goto unlock;
2496 }
2497
2498 /* Is update really needed? */
2499 if (opp->available == availability_req)
2500 goto unlock;
e1f60b29 2501
a7f3987e 2502 opp->available = availability_req;
e1f60b29 2503
e4d8ae00
VK
2504 dev_pm_opp_get(opp);
2505 mutex_unlock(&opp_table->lock);
2506
03ca370f
MH
2507 /* Notify the change of the OPP availability */
2508 if (availability_req)
052c6f19 2509 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
a7f3987e 2510 opp);
03ca370f 2511 else
052c6f19 2512 blocking_notifier_call_chain(&opp_table->head,
a7f3987e 2513 OPP_EVENT_DISABLE, opp);
e1f60b29 2514
e4d8ae00
VK
2515 dev_pm_opp_put(opp);
2516 goto put_table;
2517
e1f60b29 2518unlock:
5b650b38 2519 mutex_unlock(&opp_table->lock);
e4d8ae00 2520put_table:
5b650b38 2521 dev_pm_opp_put_opp_table(opp_table);
e1f60b29
NM
2522 return r;
2523}
2524
25cb20a2
SB
2525/**
2526 * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
2527 * @dev: device for which we do this operation
2528 * @freq: OPP frequency to adjust voltage of
2529 * @u_volt: new OPP target voltage
2530 * @u_volt_min: new OPP min voltage
2531 * @u_volt_max: new OPP max voltage
2532 *
2533 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2534 * copy operation, returns 0 if no modifcation was done OR modification was
2535 * successful.
2536 */
2537int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
2538 unsigned long u_volt, unsigned long u_volt_min,
2539 unsigned long u_volt_max)
2540
2541{
2542 struct opp_table *opp_table;
2543 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
2544 int r = 0;
2545
2546 /* Find the opp_table */
2547 opp_table = _find_opp_table(dev);
2548 if (IS_ERR(opp_table)) {
2549 r = PTR_ERR(opp_table);
2550 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
2551 return r;
2552 }
2553
2554 mutex_lock(&opp_table->lock);
2555
2556 /* Do we have the frequency? */
2557 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
2558 if (tmp_opp->rate == freq) {
2559 opp = tmp_opp;
2560 break;
2561 }
2562 }
2563
2564 if (IS_ERR(opp)) {
2565 r = PTR_ERR(opp);
2566 goto adjust_unlock;
2567 }
2568
2569 /* Is update really needed? */
2570 if (opp->supplies->u_volt == u_volt)
2571 goto adjust_unlock;
2572
2573 opp->supplies->u_volt = u_volt;
2574 opp->supplies->u_volt_min = u_volt_min;
2575 opp->supplies->u_volt_max = u_volt_max;
2576
2577 dev_pm_opp_get(opp);
2578 mutex_unlock(&opp_table->lock);
2579
2580 /* Notify the voltage change of the OPP */
2581 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
2582 opp);
2583
2584 dev_pm_opp_put(opp);
2585 goto adjust_put_table;
2586
2587adjust_unlock:
2588 mutex_unlock(&opp_table->lock);
2589adjust_put_table:
2590 dev_pm_opp_put_opp_table(opp_table);
2591 return r;
2592}
03649154 2593EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
25cb20a2 2594
e1f60b29 2595/**
5d4879cd 2596 * dev_pm_opp_enable() - Enable a specific OPP
e1f60b29
NM
2597 * @dev: device for which we do this operation
2598 * @freq: OPP frequency to enable
2599 *
2600 * Enables a provided opp. If the operation is valid, this returns 0, else the
2601 * corresponding error value. It is meant to be used for users an OPP available
5d4879cd 2602 * after being temporarily made unavailable with dev_pm_opp_disable.
e1f60b29 2603 *
984f16c8 2604 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 2605 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 2606 * successful.
e1f60b29 2607 */
5d4879cd 2608int dev_pm_opp_enable(struct device *dev, unsigned long freq)
e1f60b29 2609{
327854c8 2610 return _opp_set_availability(dev, freq, true);
e1f60b29 2611}
5d4879cd 2612EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
e1f60b29
NM
2613
2614/**
5d4879cd 2615 * dev_pm_opp_disable() - Disable a specific OPP
e1f60b29
NM
2616 * @dev: device for which we do this operation
2617 * @freq: OPP frequency to disable
2618 *
2619 * Disables a provided opp. If the operation is valid, this returns
2620 * 0, else the corresponding error value. It is meant to be a temporary
2621 * control by users to make this OPP not available until the circumstances are
5d4879cd 2622 * right to make it available again (with a call to dev_pm_opp_enable).
e1f60b29 2623 *
984f16c8 2624 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 2625 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 2626 * successful.
e1f60b29 2627 */
5d4879cd 2628int dev_pm_opp_disable(struct device *dev, unsigned long freq)
e1f60b29 2629{
327854c8 2630 return _opp_set_availability(dev, freq, false);
e1f60b29 2631}
5d4879cd 2632EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
e1f60b29 2633
03ca370f 2634/**
dc2c9ad5
VK
2635 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
2636 * @dev: Device for which notifier needs to be registered
2637 * @nb: Notifier block to be registered
984f16c8 2638 *
dc2c9ad5
VK
2639 * Return: 0 on success or a negative error value.
2640 */
2641int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
2642{
2643 struct opp_table *opp_table;
2644 int ret;
2645
dc2c9ad5 2646 opp_table = _find_opp_table(dev);
5b650b38
VK
2647 if (IS_ERR(opp_table))
2648 return PTR_ERR(opp_table);
2649
052c6f19 2650 ret = blocking_notifier_chain_register(&opp_table->head, nb);
dc2c9ad5 2651
5b650b38 2652 dev_pm_opp_put_opp_table(opp_table);
dc2c9ad5
VK
2653
2654 return ret;
2655}
2656EXPORT_SYMBOL(dev_pm_opp_register_notifier);
2657
2658/**
2659 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
2660 * @dev: Device for which notifier needs to be unregistered
2661 * @nb: Notifier block to be unregistered
984f16c8 2662 *
dc2c9ad5 2663 * Return: 0 on success or a negative error value.
03ca370f 2664 */
dc2c9ad5
VK
2665int dev_pm_opp_unregister_notifier(struct device *dev,
2666 struct notifier_block *nb)
03ca370f 2667{
dc2c9ad5
VK
2668 struct opp_table *opp_table;
2669 int ret;
03ca370f 2670
dc2c9ad5 2671 opp_table = _find_opp_table(dev);
5b650b38
VK
2672 if (IS_ERR(opp_table))
2673 return PTR_ERR(opp_table);
dc2c9ad5 2674
052c6f19 2675 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
03ca370f 2676
5b650b38 2677 dev_pm_opp_put_opp_table(opp_table);
dc2c9ad5
VK
2678
2679 return ret;
03ca370f 2680}
dc2c9ad5 2681EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
b496dfbc 2682
8aaf6264
VK
2683/**
2684 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
2685 * @dev: device pointer used to lookup OPP table.
2686 *
2687 * Free both OPPs created using static entries present in DT and the
2688 * dynamically added entries.
2689 */
2690void dev_pm_opp_remove_table(struct device *dev)
9274c892
VK
2691{
2692 struct opp_table *opp_table;
2693
2c2709dc
VK
2694 /* Check for existing table for 'dev' */
2695 opp_table = _find_opp_table(dev);
2696 if (IS_ERR(opp_table)) {
2697 int error = PTR_ERR(opp_table);
737002b5
VK
2698
2699 if (error != -ENODEV)
2c2709dc 2700 WARN(1, "%s: opp_table: %d\n",
737002b5
VK
2701 IS_ERR_OR_NULL(dev) ?
2702 "Invalid device" : dev_name(dev),
2703 error);
5b650b38 2704 return;
737002b5
VK
2705 }
2706
922ff075
VK
2707 /*
2708 * Drop the extra reference only if the OPP table was successfully added
2709 * with dev_pm_opp_of_add_table() earlier.
2710 **/
2711 if (_opp_remove_all_static(opp_table))
2712 dev_pm_opp_put_opp_table(opp_table);
cdd6ed90
VK
2713
2714 /* Drop reference taken by _find_opp_table() */
2715 dev_pm_opp_put_opp_table(opp_table);
737002b5 2716}
411466c5 2717EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
ce8073d8
DO
2718
2719/**
2720 * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
2721 * @dev: device for which we do this operation
2722 *
2723 * Sync voltage state of the OPP table regulators.
2724 *
2725 * Return: 0 on success or a negative error value.
2726 */
2727int dev_pm_opp_sync_regulators(struct device *dev)
2728{
2729 struct opp_table *opp_table;
2730 struct regulator *reg;
2731 int i, ret = 0;
2732
2733 /* Device may not have OPP table */
2734 opp_table = _find_opp_table(dev);
2735 if (IS_ERR(opp_table))
2736 return 0;
2737
2738 /* Regulator may not be required for the device */
2739 if (unlikely(!opp_table->regulators))
2740 goto put_table;
2741
2742 /* Nothing to sync if voltage wasn't changed */
2743 if (!opp_table->enabled)
2744 goto put_table;
2745
2746 for (i = 0; i < opp_table->regulator_count; i++) {
2747 reg = opp_table->regulators[i];
2748 ret = regulator_sync_voltage(reg);
2749 if (ret)
2750 break;
2751 }
2752put_table:
2753 /* Drop reference taken by _find_opp_table() */
2754 dev_pm_opp_put_opp_table(opp_table);
2755
2756 return ret;
2757}
2758EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);