Merge remote-tracking branch 'georgi/icc-get-by-index' into opp/linux-next
[linux-block.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);
e1f60b29 32
2c2709dc
VK
33static struct opp_device *_find_opp_dev(const struct device *dev,
34 struct opp_table *opp_table)
06441658 35{
2c2709dc 36 struct opp_device *opp_dev;
06441658 37
2c2709dc
VK
38 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
39 if (opp_dev->dev == dev)
40 return opp_dev;
06441658
VK
41
42 return NULL;
43}
44
6ac42397 45static struct opp_table *_find_opp_table_unlocked(struct device *dev)
5b650b38
VK
46{
47 struct opp_table *opp_table;
3d255699 48 bool found;
5b650b38
VK
49
50 list_for_each_entry(opp_table, &opp_tables, node) {
3d255699
VK
51 mutex_lock(&opp_table->lock);
52 found = !!_find_opp_dev(dev, opp_table);
53 mutex_unlock(&opp_table->lock);
54
55 if (found) {
5b650b38
VK
56 _get_opp_table_kref(opp_table);
57
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{
052c6f19 121 if (IS_ERR_OR_NULL(opp) || !opp->available) {
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
19445b25
BZ
148/**
149 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
150 * @opp: opp for which turbo mode is being verified
151 *
152 * Turbo OPPs are not for normal use, and can be enabled (under certain
153 * conditions) for short duration of times to finish high throughput work
154 * quickly. Running on them for longer times may overheat the chip.
155 *
156 * Return: true if opp is turbo opp, else false.
19445b25
BZ
157 */
158bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
159{
052c6f19 160 if (IS_ERR_OR_NULL(opp) || !opp->available) {
19445b25
BZ
161 pr_err("%s: Invalid parameters\n", __func__);
162 return false;
163 }
164
052c6f19 165 return opp->turbo;
19445b25
BZ
166}
167EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
168
3ca9bb33
VK
169/**
170 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
171 * @dev: device for which we do this operation
172 *
173 * Return: This function returns the max clock latency in nanoseconds.
3ca9bb33
VK
174 */
175unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
176{
2c2709dc 177 struct opp_table *opp_table;
3ca9bb33
VK
178 unsigned long clock_latency_ns;
179
2c2709dc
VK
180 opp_table = _find_opp_table(dev);
181 if (IS_ERR(opp_table))
5b650b38
VK
182 return 0;
183
184 clock_latency_ns = opp_table->clock_latency_ns_max;
185
186 dev_pm_opp_put_opp_table(opp_table);
3ca9bb33 187
3ca9bb33
VK
188 return clock_latency_ns;
189}
190EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
191
655c9df9
VK
192/**
193 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
194 * @dev: device for which we do this operation
195 *
196 * Return: This function returns the max voltage latency in nanoseconds.
655c9df9
VK
197 */
198unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
199{
2c2709dc 200 struct opp_table *opp_table;
655c9df9 201 struct dev_pm_opp *opp;
478256bd 202 struct regulator *reg;
655c9df9 203 unsigned long latency_ns = 0;
dfbe4678
VK
204 int ret, i, count;
205 struct {
206 unsigned long min;
207 unsigned long max;
208 } *uV;
209
cdd3e614
VK
210 opp_table = _find_opp_table(dev);
211 if (IS_ERR(opp_table))
212 return 0;
213
dfbe4678 214 /* Regulator may not be required for the device */
90e3577b 215 if (!opp_table->regulators)
cdd3e614 216 goto put_opp_table;
dfbe4678 217
90e3577b
VK
218 count = opp_table->regulator_count;
219
dfbe4678
VK
220 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
221 if (!uV)
478256bd 222 goto put_opp_table;
655c9df9 223
052c6f19
VK
224 mutex_lock(&opp_table->lock);
225
dfbe4678
VK
226 for (i = 0; i < count; i++) {
227 uV[i].min = ~0;
228 uV[i].max = 0;
655c9df9 229
052c6f19 230 list_for_each_entry(opp, &opp_table->opp_list, node) {
dfbe4678
VK
231 if (!opp->available)
232 continue;
233
234 if (opp->supplies[i].u_volt_min < uV[i].min)
235 uV[i].min = opp->supplies[i].u_volt_min;
236 if (opp->supplies[i].u_volt_max > uV[i].max)
237 uV[i].max = opp->supplies[i].u_volt_max;
238 }
655c9df9
VK
239 }
240
052c6f19 241 mutex_unlock(&opp_table->lock);
655c9df9
VK
242
243 /*
2c2709dc 244 * The caller needs to ensure that opp_table (and hence the regulator)
655c9df9
VK
245 * isn't freed, while we are executing this routine.
246 */
8cc31116 247 for (i = 0; i < count; i++) {
478256bd 248 reg = opp_table->regulators[i];
dfbe4678
VK
249 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
250 if (ret > 0)
251 latency_ns += ret * 1000;
252 }
253
dfbe4678 254 kfree(uV);
cdd3e614
VK
255put_opp_table:
256 dev_pm_opp_put_opp_table(opp_table);
655c9df9
VK
257
258 return latency_ns;
259}
260EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
261
21743447
VK
262/**
263 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
264 * nanoseconds
265 * @dev: device for which we do this operation
266 *
267 * Return: This function returns the max transition latency, in nanoseconds, to
268 * switch from one OPP to other.
21743447
VK
269 */
270unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
271{
272 return dev_pm_opp_get_max_volt_latency(dev) +
273 dev_pm_opp_get_max_clock_latency(dev);
274}
275EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
276
4eafbd15 277/**
3aa26a3b 278 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
4eafbd15
BZ
279 * @dev: device for which we do this operation
280 *
3aa26a3b
VK
281 * Return: This function returns the frequency of the OPP marked as suspend_opp
282 * if one is available, else returns 0;
4eafbd15 283 */
3aa26a3b 284unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
4eafbd15 285{
2c2709dc 286 struct opp_table *opp_table;
3aa26a3b 287 unsigned long freq = 0;
4eafbd15 288
2c2709dc 289 opp_table = _find_opp_table(dev);
5b650b38
VK
290 if (IS_ERR(opp_table))
291 return 0;
3aa26a3b 292
5b650b38
VK
293 if (opp_table->suspend_opp && opp_table->suspend_opp->available)
294 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
295
296 dev_pm_opp_put_opp_table(opp_table);
4eafbd15 297
3aa26a3b 298 return freq;
4eafbd15 299}
3aa26a3b 300EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
4eafbd15 301
a1e8c136
VK
302int _get_opp_count(struct opp_table *opp_table)
303{
304 struct dev_pm_opp *opp;
305 int count = 0;
306
307 mutex_lock(&opp_table->lock);
308
309 list_for_each_entry(opp, &opp_table->opp_list, node) {
310 if (opp->available)
311 count++;
312 }
313
314 mutex_unlock(&opp_table->lock);
315
316 return count;
317}
318
e1f60b29 319/**
2c2709dc 320 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
e1f60b29
NM
321 * @dev: device for which we do this operation
322 *
984f16c8 323 * Return: This function returns the number of available opps if there are any,
e1f60b29 324 * else returns 0 if none or the corresponding error value.
e1f60b29 325 */
5d4879cd 326int dev_pm_opp_get_opp_count(struct device *dev)
e1f60b29 327{
2c2709dc 328 struct opp_table *opp_table;
a1e8c136 329 int count;
e1f60b29 330
2c2709dc
VK
331 opp_table = _find_opp_table(dev);
332 if (IS_ERR(opp_table)) {
333 count = PTR_ERR(opp_table);
035ed072 334 dev_dbg(dev, "%s: OPP table not found (%d)\n",
b4718c02 335 __func__, count);
09f662f9 336 return count;
e1f60b29
NM
337 }
338
a1e8c136 339 count = _get_opp_count(opp_table);
5b650b38
VK
340 dev_pm_opp_put_opp_table(opp_table);
341
e1f60b29
NM
342 return count;
343}
5d4879cd 344EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
e1f60b29
NM
345
346/**
5d4879cd 347 * dev_pm_opp_find_freq_exact() - search for an exact frequency
e1f60b29
NM
348 * @dev: device for which we do this operation
349 * @freq: frequency to search for
7ae49618 350 * @available: true/false - match for available opp
e1f60b29 351 *
2c2709dc 352 * Return: Searches for exact match in the opp table and returns pointer to the
984f16c8
NM
353 * matching opp if found, else returns ERR_PTR in case of error and should
354 * be handled using IS_ERR. Error return values can be:
0779726c
NM
355 * EINVAL: for bad pointer
356 * ERANGE: no match found for search
357 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
358 *
359 * Note: available is a modifier for the search. if available=true, then the
360 * match is for exact matching frequency and is available in the stored OPP
361 * table. if false, the match is for exact frequency which is not available.
362 *
363 * This provides a mechanism to enable an opp which is not available currently
364 * or the opposite as well.
365 *
8a31d9d9
VK
366 * The callers are required to call dev_pm_opp_put() for the returned OPP after
367 * use.
e1f60b29 368 */
47d43ba7
NM
369struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
370 unsigned long freq,
371 bool available)
e1f60b29 372{
2c2709dc 373 struct opp_table *opp_table;
47d43ba7 374 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 375
2c2709dc
VK
376 opp_table = _find_opp_table(dev);
377 if (IS_ERR(opp_table)) {
378 int r = PTR_ERR(opp_table);
379
380 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
e1f60b29
NM
381 return ERR_PTR(r);
382 }
383
052c6f19 384 mutex_lock(&opp_table->lock);
5b650b38 385
052c6f19 386 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
387 if (temp_opp->available == available &&
388 temp_opp->rate == freq) {
389 opp = temp_opp;
8a31d9d9
VK
390
391 /* Increment the reference count of OPP */
392 dev_pm_opp_get(opp);
e1f60b29
NM
393 break;
394 }
395 }
396
052c6f19 397 mutex_unlock(&opp_table->lock);
5b650b38 398 dev_pm_opp_put_opp_table(opp_table);
8a31d9d9 399
e1f60b29
NM
400 return opp;
401}
5d4879cd 402EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
e1f60b29 403
71419d84
NC
404/**
405 * dev_pm_opp_find_level_exact() - search for an exact level
406 * @dev: device for which we do this operation
407 * @level: level to search for
408 *
409 * Return: Searches for exact match in the opp table and returns pointer to the
410 * matching opp if found, else returns ERR_PTR in case of error and should
411 * be handled using IS_ERR. Error return values can be:
412 * EINVAL: for bad pointer
413 * ERANGE: no match found for search
414 * ENODEV: if device not found in list of registered devices
415 *
416 * The callers are required to call dev_pm_opp_put() for the returned OPP after
417 * use.
418 */
419struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
420 unsigned int level)
421{
422 struct opp_table *opp_table;
423 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
424
425 opp_table = _find_opp_table(dev);
426 if (IS_ERR(opp_table)) {
427 int r = PTR_ERR(opp_table);
428
429 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
430 return ERR_PTR(r);
431 }
432
433 mutex_lock(&opp_table->lock);
434
435 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
436 if (temp_opp->level == level) {
437 opp = temp_opp;
438
439 /* Increment the reference count of OPP */
440 dev_pm_opp_get(opp);
441 break;
442 }
443 }
444
445 mutex_unlock(&opp_table->lock);
446 dev_pm_opp_put_opp_table(opp_table);
447
448 return opp;
449}
450EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
451
067b7ce0
JZ
452static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
453 unsigned long *freq)
454{
455 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
456
052c6f19
VK
457 mutex_lock(&opp_table->lock);
458
459 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
067b7ce0
JZ
460 if (temp_opp->available && temp_opp->rate >= *freq) {
461 opp = temp_opp;
462 *freq = opp->rate;
8a31d9d9
VK
463
464 /* Increment the reference count of OPP */
465 dev_pm_opp_get(opp);
067b7ce0
JZ
466 break;
467 }
468 }
469
052c6f19
VK
470 mutex_unlock(&opp_table->lock);
471
067b7ce0
JZ
472 return opp;
473}
474
e1f60b29 475/**
5d4879cd 476 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
e1f60b29
NM
477 * @dev: device for which we do this operation
478 * @freq: Start frequency
479 *
480 * Search for the matching ceil *available* OPP from a starting freq
481 * for a device.
482 *
984f16c8 483 * Return: matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
484 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
485 * values can be:
486 * EINVAL: for bad pointer
487 * ERANGE: no match found for search
488 * ENODEV: if device not found in list of registered devices
e1f60b29 489 *
8a31d9d9
VK
490 * The callers are required to call dev_pm_opp_put() for the returned OPP after
491 * use.
e1f60b29 492 */
47d43ba7
NM
493struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
494 unsigned long *freq)
e1f60b29 495{
2c2709dc 496 struct opp_table *opp_table;
8a31d9d9 497 struct dev_pm_opp *opp;
b02ded24 498
e1f60b29
NM
499 if (!dev || !freq) {
500 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
501 return ERR_PTR(-EINVAL);
502 }
503
2c2709dc 504 opp_table = _find_opp_table(dev);
5b650b38 505 if (IS_ERR(opp_table))
2c2709dc 506 return ERR_CAST(opp_table);
5b650b38 507
8a31d9d9 508 opp = _find_freq_ceil(opp_table, freq);
e1f60b29 509
5b650b38 510 dev_pm_opp_put_opp_table(opp_table);
8a31d9d9
VK
511
512 return opp;
e1f60b29 513}
5d4879cd 514EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
e1f60b29
NM
515
516/**
5d4879cd 517 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
e1f60b29
NM
518 * @dev: device for which we do this operation
519 * @freq: Start frequency
520 *
521 * Search for the matching floor *available* OPP from a starting freq
522 * for a device.
523 *
984f16c8 524 * Return: matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
525 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
526 * values can be:
527 * EINVAL: for bad pointer
528 * ERANGE: no match found for search
529 * ENODEV: if device not found in list of registered devices
e1f60b29 530 *
8a31d9d9
VK
531 * The callers are required to call dev_pm_opp_put() for the returned OPP after
532 * use.
e1f60b29 533 */
47d43ba7
NM
534struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
535 unsigned long *freq)
e1f60b29 536{
2c2709dc 537 struct opp_table *opp_table;
47d43ba7 538 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29
NM
539
540 if (!dev || !freq) {
541 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
542 return ERR_PTR(-EINVAL);
543 }
544
2c2709dc 545 opp_table = _find_opp_table(dev);
5b650b38 546 if (IS_ERR(opp_table))
2c2709dc 547 return ERR_CAST(opp_table);
5b650b38 548
052c6f19 549 mutex_lock(&opp_table->lock);
e1f60b29 550
052c6f19 551 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
552 if (temp_opp->available) {
553 /* go to the next node, before choosing prev */
554 if (temp_opp->rate > *freq)
555 break;
556 else
557 opp = temp_opp;
558 }
559 }
8a31d9d9
VK
560
561 /* Increment the reference count of OPP */
562 if (!IS_ERR(opp))
563 dev_pm_opp_get(opp);
052c6f19 564 mutex_unlock(&opp_table->lock);
5b650b38 565 dev_pm_opp_put_opp_table(opp_table);
8a31d9d9 566
e1f60b29
NM
567 if (!IS_ERR(opp))
568 *freq = opp->rate;
569
570 return opp;
571}
5d4879cd 572EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
e1f60b29 573
2f36bde0
AC
574/**
575 * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for
576 * target voltage.
577 * @dev: Device for which we do this operation.
578 * @u_volt: Target voltage.
579 *
580 * Search for OPP with highest (ceil) frequency and has voltage <= u_volt.
581 *
582 * Return: matching *opp, else returns ERR_PTR in case of error which should be
583 * handled using IS_ERR.
584 *
585 * Error return values can be:
586 * EINVAL: bad parameters
587 *
588 * The callers are required to call dev_pm_opp_put() for the returned OPP after
589 * use.
590 */
591struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev,
592 unsigned long u_volt)
593{
594 struct opp_table *opp_table;
595 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
596
597 if (!dev || !u_volt) {
598 dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__,
599 u_volt);
600 return ERR_PTR(-EINVAL);
601 }
602
603 opp_table = _find_opp_table(dev);
604 if (IS_ERR(opp_table))
605 return ERR_CAST(opp_table);
606
607 mutex_lock(&opp_table->lock);
608
609 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
610 if (temp_opp->available) {
611 if (temp_opp->supplies[0].u_volt > u_volt)
612 break;
613 opp = temp_opp;
614 }
615 }
616
617 /* Increment the reference count of OPP */
618 if (!IS_ERR(opp))
619 dev_pm_opp_get(opp);
620
621 mutex_unlock(&opp_table->lock);
622 dev_pm_opp_put_opp_table(opp_table);
623
624 return opp;
625}
626EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt);
627
6a0712f6 628static int _set_opp_voltage(struct device *dev, struct regulator *reg,
ce31781a 629 struct dev_pm_opp_supply *supply)
6a0712f6
VK
630{
631 int ret;
632
633 /* Regulator not available for device */
634 if (IS_ERR(reg)) {
635 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
636 PTR_ERR(reg));
637 return 0;
638 }
639
ce31781a
VK
640 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
641 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
6a0712f6 642
ce31781a
VK
643 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
644 supply->u_volt, supply->u_volt_max);
6a0712f6
VK
645 if (ret)
646 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
ce31781a
VK
647 __func__, supply->u_volt_min, supply->u_volt,
648 supply->u_volt_max, ret);
6a0712f6
VK
649
650 return ret;
651}
652
285881b5
VK
653static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
654 unsigned long freq)
94735585
VK
655{
656 int ret;
657
658 ret = clk_set_rate(clk, freq);
659 if (ret) {
660 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
661 ret);
662 }
663
664 return ret;
665}
666
c74b32fa
VK
667static int _generic_set_opp_regulator(const struct opp_table *opp_table,
668 struct device *dev,
669 unsigned long old_freq,
670 unsigned long freq,
671 struct dev_pm_opp_supply *old_supply,
672 struct dev_pm_opp_supply *new_supply)
94735585 673{
c74b32fa 674 struct regulator *reg = opp_table->regulators[0];
94735585
VK
675 int ret;
676
677 /* This function only supports single regulator per device */
c74b32fa 678 if (WARN_ON(opp_table->regulator_count > 1)) {
94735585
VK
679 dev_err(dev, "multiple regulators are not supported\n");
680 return -EINVAL;
681 }
682
683 /* Scaling up? Scale voltage before frequency */
c5c2a97b 684 if (freq >= old_freq) {
94735585
VK
685 ret = _set_opp_voltage(dev, reg, new_supply);
686 if (ret)
687 goto restore_voltage;
688 }
689
690 /* Change frequency */
285881b5 691 ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
94735585
VK
692 if (ret)
693 goto restore_voltage;
694
695 /* Scaling down? Scale voltage after frequency */
696 if (freq < old_freq) {
697 ret = _set_opp_voltage(dev, reg, new_supply);
698 if (ret)
699 goto restore_freq;
700 }
701
702 return 0;
703
704restore_freq:
285881b5 705 if (_generic_set_opp_clk_only(dev, opp_table->clk, old_freq))
94735585
VK
706 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
707 __func__, old_freq);
708restore_voltage:
709 /* This shouldn't harm even if the voltages weren't updated earlier */
c74b32fa 710 if (old_supply)
94735585
VK
711 _set_opp_voltage(dev, reg, old_supply);
712
713 return ret;
714}
715
7e535993
VK
716static int _set_opp_custom(const struct opp_table *opp_table,
717 struct device *dev, unsigned long old_freq,
718 unsigned long freq,
719 struct dev_pm_opp_supply *old_supply,
720 struct dev_pm_opp_supply *new_supply)
721{
722 struct dev_pm_set_opp_data *data;
723 int size;
724
725 data = opp_table->set_opp_data;
726 data->regulators = opp_table->regulators;
727 data->regulator_count = opp_table->regulator_count;
728 data->clk = opp_table->clk;
729 data->dev = dev;
730
731 data->old_opp.rate = old_freq;
732 size = sizeof(*old_supply) * opp_table->regulator_count;
560d1bca 733 if (!old_supply)
7e535993
VK
734 memset(data->old_opp.supplies, 0, size);
735 else
736 memcpy(data->old_opp.supplies, old_supply, size);
737
738 data->new_opp.rate = freq;
739 memcpy(data->new_opp.supplies, new_supply, size);
740
741 return opp_table->set_opp(data);
742}
743
ca1b5d77
VK
744/* This is only called for PM domain for now */
745static int _set_required_opps(struct device *dev,
746 struct opp_table *opp_table,
747 struct dev_pm_opp *opp)
748{
749 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
750 struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
751 unsigned int pstate;
752 int i, ret = 0;
753
754 if (!required_opp_tables)
755 return 0;
756
757 /* Single genpd case */
758 if (!genpd_virt_devs) {
cd7ea582 759 pstate = likely(opp) ? opp->required_opps[0]->pstate : 0;
ca1b5d77
VK
760 ret = dev_pm_genpd_set_performance_state(dev, pstate);
761 if (ret) {
762 dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
763 dev_name(dev), pstate, ret);
764 }
765 return ret;
766 }
767
768 /* Multiple genpd case */
769
770 /*
771 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
772 * after it is freed from another thread.
773 */
774 mutex_lock(&opp_table->genpd_virt_dev_lock);
775
776 for (i = 0; i < opp_table->required_opp_count; i++) {
cd7ea582 777 pstate = likely(opp) ? opp->required_opps[i]->pstate : 0;
ca1b5d77
VK
778
779 if (!genpd_virt_devs[i])
780 continue;
781
782 ret = dev_pm_genpd_set_performance_state(genpd_virt_devs[i], pstate);
783 if (ret) {
784 dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
785 dev_name(genpd_virt_devs[i]), pstate, ret);
786 break;
787 }
788 }
789 mutex_unlock(&opp_table->genpd_virt_dev_lock);
790
791 return ret;
792}
793
6a0712f6
VK
794/**
795 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
796 * @dev: device for which we do this operation
797 * @target_freq: frequency to achieve
798 *
b3e3759e
SB
799 * This configures the power-supplies to the levels specified by the OPP
800 * corresponding to the target_freq, and programs the clock to a value <=
801 * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
802 * provided by the opp, should have already rounded to the target OPP's
803 * frequency.
6a0712f6
VK
804 */
805int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
806{
2c2709dc 807 struct opp_table *opp_table;
b3e3759e 808 unsigned long freq, old_freq, temp_freq;
6a0712f6 809 struct dev_pm_opp *old_opp, *opp;
6a0712f6 810 struct clk *clk;
7e535993 811 int ret;
6a0712f6 812
052c6f19
VK
813 opp_table = _find_opp_table(dev);
814 if (IS_ERR(opp_table)) {
815 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
816 return PTR_ERR(opp_table);
817 }
818
cd7ea582
RN
819 if (unlikely(!target_freq)) {
820 if (opp_table->required_opp_tables) {
821 ret = _set_required_opps(dev, opp_table, NULL);
aca48b61
RN
822 } else if (!_get_opp_count(opp_table)) {
823 return 0;
cd7ea582
RN
824 } else {
825 dev_err(dev, "target frequency can't be 0\n");
826 ret = -EINVAL;
827 }
828
829 goto put_opp_table;
830 }
831
052c6f19
VK
832 clk = opp_table->clk;
833 if (IS_ERR(clk)) {
834 dev_err(dev, "%s: No clock available for the device\n",
835 __func__);
836 ret = PTR_ERR(clk);
837 goto put_opp_table;
838 }
6a0712f6
VK
839
840 freq = clk_round_rate(clk, target_freq);
841 if ((long)freq <= 0)
842 freq = target_freq;
843
844 old_freq = clk_get_rate(clk);
845
846 /* Return early if nothing to do */
847 if (old_freq == freq) {
848 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
849 __func__, freq);
052c6f19
VK
850 ret = 0;
851 goto put_opp_table;
6a0712f6
VK
852 }
853
aca48b61
RN
854 /*
855 * For IO devices which require an OPP on some platforms/SoCs
856 * while just needing to scale the clock on some others
857 * we look for empty OPP tables with just a clock handle and
858 * scale only the clk. This makes dev_pm_opp_set_rate()
859 * equivalent to a clk_set_rate()
860 */
861 if (!_get_opp_count(opp_table)) {
862 ret = _generic_set_opp_clk_only(dev, clk, freq);
863 goto put_opp_table;
864 }
865
b3e3759e
SB
866 temp_freq = old_freq;
867 old_opp = _find_freq_ceil(opp_table, &temp_freq);
4df27c91 868 if (IS_ERR(old_opp)) {
6a0712f6
VK
869 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
870 __func__, old_freq, PTR_ERR(old_opp));
871 }
872
b3e3759e
SB
873 temp_freq = freq;
874 opp = _find_freq_ceil(opp_table, &temp_freq);
6a0712f6
VK
875 if (IS_ERR(opp)) {
876 ret = PTR_ERR(opp);
877 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
878 __func__, freq, ret);
052c6f19 879 goto put_old_opp;
6a0712f6
VK
880 }
881
94735585
VK
882 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
883 old_freq, freq);
dfbe4678 884
ca1b5d77 885 /* Scaling up? Configure required OPPs before frequency */
faef080f 886 if (freq >= old_freq) {
ca1b5d77
VK
887 ret = _set_required_opps(dev, opp_table, opp);
888 if (ret)
889 goto put_opp;
890 }
891
7e535993
VK
892 if (opp_table->set_opp) {
893 ret = _set_opp_custom(opp_table, dev, old_freq, freq,
894 IS_ERR(old_opp) ? NULL : old_opp->supplies,
895 opp->supplies);
896 } else if (opp_table->regulators) {
c74b32fa
VK
897 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
898 IS_ERR(old_opp) ? NULL : old_opp->supplies,
899 opp->supplies);
900 } else {
7e535993 901 /* Only frequency scaling */
285881b5 902 ret = _generic_set_opp_clk_only(dev, clk, freq);
ca1b5d77 903 }
c74b32fa 904
ca1b5d77
VK
905 /* Scaling down? Configure required OPPs after frequency */
906 if (!ret && freq < old_freq) {
907 ret = _set_required_opps(dev, opp_table, opp);
908 if (ret)
909 dev_err(dev, "Failed to set required opps: %d\n", ret);
dfbe4678
VK
910 }
911
ca1b5d77 912put_opp:
8a31d9d9 913 dev_pm_opp_put(opp);
052c6f19 914put_old_opp:
8a31d9d9
VK
915 if (!IS_ERR(old_opp))
916 dev_pm_opp_put(old_opp);
052c6f19 917put_opp_table:
5b650b38 918 dev_pm_opp_put_opp_table(opp_table);
052c6f19 919 return ret;
6a0712f6
VK
920}
921EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
922
2c2709dc 923/* OPP-dev Helpers */
2c2709dc
VK
924static void _remove_opp_dev(struct opp_device *opp_dev,
925 struct opp_table *opp_table)
06441658 926{
2c2709dc
VK
927 opp_debug_unregister(opp_dev, opp_table);
928 list_del(&opp_dev->node);
052c6f19 929 kfree(opp_dev);
06441658
VK
930}
931
283d55e6
VK
932static struct opp_device *_add_opp_dev_unlocked(const struct device *dev,
933 struct opp_table *opp_table)
06441658 934{
2c2709dc 935 struct opp_device *opp_dev;
06441658 936
2c2709dc
VK
937 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
938 if (!opp_dev)
06441658
VK
939 return NULL;
940
2c2709dc
VK
941 /* Initialize opp-dev */
942 opp_dev->dev = dev;
3d255699 943
052c6f19 944 list_add(&opp_dev->node, &opp_table->dev_list);
06441658 945
2c2709dc 946 /* Create debugfs entries for the opp_table */
a2dea4cb 947 opp_debug_register(opp_dev, opp_table);
283d55e6
VK
948
949 return opp_dev;
950}
951
952struct opp_device *_add_opp_dev(const struct device *dev,
953 struct opp_table *opp_table)
954{
955 struct opp_device *opp_dev;
956
957 mutex_lock(&opp_table->lock);
958 opp_dev = _add_opp_dev_unlocked(dev, opp_table);
3d255699 959 mutex_unlock(&opp_table->lock);
deaa5146 960
2c2709dc 961 return opp_dev;
06441658
VK
962}
963
eb7c8743 964static struct opp_table *_allocate_opp_table(struct device *dev, int index)
07cce74a 965{
2c2709dc
VK
966 struct opp_table *opp_table;
967 struct opp_device *opp_dev;
d54974c2 968 int ret;
07cce74a
VK
969
970 /*
2c2709dc 971 * Allocate a new OPP table. In the infrequent case where a new
07cce74a
VK
972 * device is needed to be added, we pay this penalty.
973 */
2c2709dc
VK
974 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
975 if (!opp_table)
07cce74a
VK
976 return NULL;
977
3d255699 978 mutex_init(&opp_table->lock);
4f018bc0 979 mutex_init(&opp_table->genpd_virt_dev_lock);
2c2709dc 980 INIT_LIST_HEAD(&opp_table->dev_list);
06441658 981
46f48aca
VK
982 /* Mark regulator count uninitialized */
983 opp_table->regulator_count = -1;
984
2c2709dc
VK
985 opp_dev = _add_opp_dev(dev, opp_table);
986 if (!opp_dev) {
987 kfree(opp_table);
06441658
VK
988 return NULL;
989 }
990
eb7c8743 991 _of_init_opp_table(opp_table, dev, index);
50f8cfbd 992
d54974c2 993 /* Find clk for the device */
2c2709dc
VK
994 opp_table->clk = clk_get(dev, NULL);
995 if (IS_ERR(opp_table->clk)) {
996 ret = PTR_ERR(opp_table->clk);
d54974c2
VK
997 if (ret != -EPROBE_DEFER)
998 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
999 ret);
1000 }
1001
052c6f19 1002 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
2c2709dc 1003 INIT_LIST_HEAD(&opp_table->opp_list);
f067a982 1004 kref_init(&opp_table->kref);
07cce74a 1005
2c2709dc 1006 /* Secure the device table modification */
052c6f19 1007 list_add(&opp_table->node, &opp_tables);
2c2709dc 1008 return opp_table;
07cce74a
VK
1009}
1010
f067a982 1011void _get_opp_table_kref(struct opp_table *opp_table)
b6160e26 1012{
f067a982
VK
1013 kref_get(&opp_table->kref);
1014}
1015
eb7c8743 1016static struct opp_table *_opp_get_opp_table(struct device *dev, int index)
f067a982
VK
1017{
1018 struct opp_table *opp_table;
1019
1020 /* Hold our table modification lock here */
1021 mutex_lock(&opp_table_lock);
1022
5b650b38
VK
1023 opp_table = _find_opp_table_unlocked(dev);
1024 if (!IS_ERR(opp_table))
f067a982 1025 goto unlock;
f067a982 1026
283d55e6
VK
1027 opp_table = _managed_opp(dev, index);
1028 if (opp_table) {
1029 if (!_add_opp_dev_unlocked(dev, opp_table)) {
1030 dev_pm_opp_put_opp_table(opp_table);
1031 opp_table = NULL;
1032 }
1033 goto unlock;
1034 }
1035
eb7c8743 1036 opp_table = _allocate_opp_table(dev, index);
f067a982
VK
1037
1038unlock:
1039 mutex_unlock(&opp_table_lock);
1040
1041 return opp_table;
1042}
eb7c8743
VK
1043
1044struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1045{
1046 return _opp_get_opp_table(dev, 0);
1047}
f067a982
VK
1048EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
1049
eb7c8743
VK
1050struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev,
1051 int index)
1052{
1053 return _opp_get_opp_table(dev, index);
1054}
1055
b83c1899 1056static void _opp_table_kref_release(struct kref *kref)
f067a982
VK
1057{
1058 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
cdd6ed90 1059 struct opp_device *opp_dev, *temp;
b6160e26 1060
5d6d106f
VK
1061 _of_clear_opp_table(opp_table);
1062
b6160e26
VK
1063 /* Release clk */
1064 if (!IS_ERR(opp_table->clk))
1065 clk_put(opp_table->clk);
1066
cdd6ed90 1067 WARN_ON(!list_empty(&opp_table->opp_list));
b6160e26 1068
cdd6ed90
VK
1069 list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
1070 /*
1071 * The OPP table is getting removed, drop the performance state
1072 * constraints.
1073 */
1074 if (opp_table->genpd_performance_state)
1075 dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
b6160e26 1076
cdd6ed90
VK
1077 _remove_opp_dev(opp_dev, opp_table);
1078 }
b6160e26 1079
4f018bc0 1080 mutex_destroy(&opp_table->genpd_virt_dev_lock);
37a73ec0 1081 mutex_destroy(&opp_table->lock);
052c6f19
VK
1082 list_del(&opp_table->node);
1083 kfree(opp_table);
b6160e26 1084
f067a982
VK
1085 mutex_unlock(&opp_table_lock);
1086}
1087
1088void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
1089{
1090 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
1091 &opp_table_lock);
1092}
1093EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
1094
8cd2f6e8 1095void _opp_free(struct dev_pm_opp *opp)
969fceb3
VK
1096{
1097 kfree(opp);
969fceb3
VK
1098}
1099
1690d8bb
VK
1100static void _opp_kref_release(struct dev_pm_opp *opp,
1101 struct opp_table *opp_table)
129eec55
VK
1102{
1103 /*
1104 * Notify the changes in the availability of the operable
1105 * frequency/voltage list.
1106 */
052c6f19 1107 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
da544b61 1108 _of_opp_free_required_opps(opp_table, opp);
deaa5146 1109 opp_debug_remove_one(opp);
052c6f19
VK
1110 list_del(&opp->node);
1111 kfree(opp);
1690d8bb 1112}
129eec55 1113
1690d8bb
VK
1114static void _opp_kref_release_unlocked(struct kref *kref)
1115{
1116 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1117 struct opp_table *opp_table = opp->opp_table;
1118
1119 _opp_kref_release(opp, opp_table);
1120}
1121
1122static void _opp_kref_release_locked(struct kref *kref)
1123{
1124 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1125 struct opp_table *opp_table = opp->opp_table;
1126
1127 _opp_kref_release(opp, opp_table);
37a73ec0 1128 mutex_unlock(&opp_table->lock);
129eec55
VK
1129}
1130
a88bd2a5 1131void dev_pm_opp_get(struct dev_pm_opp *opp)
8a31d9d9
VK
1132{
1133 kref_get(&opp->kref);
1134}
1135
7034764a
VK
1136void dev_pm_opp_put(struct dev_pm_opp *opp)
1137{
1690d8bb
VK
1138 kref_put_mutex(&opp->kref, _opp_kref_release_locked,
1139 &opp->opp_table->lock);
7034764a
VK
1140}
1141EXPORT_SYMBOL_GPL(dev_pm_opp_put);
1142
1690d8bb
VK
1143static void dev_pm_opp_put_unlocked(struct dev_pm_opp *opp)
1144{
1145 kref_put(&opp->kref, _opp_kref_release_unlocked);
1146}
1147
129eec55 1148/**
2c2709dc 1149 * dev_pm_opp_remove() - Remove an OPP from OPP table
129eec55
VK
1150 * @dev: device for which we do this operation
1151 * @freq: OPP to remove with matching 'freq'
1152 *
2c2709dc 1153 * This function removes an opp from the opp table.
129eec55
VK
1154 */
1155void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1156{
1157 struct dev_pm_opp *opp;
2c2709dc 1158 struct opp_table *opp_table;
129eec55
VK
1159 bool found = false;
1160
2c2709dc
VK
1161 opp_table = _find_opp_table(dev);
1162 if (IS_ERR(opp_table))
5b650b38 1163 return;
129eec55 1164
37a73ec0
VK
1165 mutex_lock(&opp_table->lock);
1166
2c2709dc 1167 list_for_each_entry(opp, &opp_table->opp_list, node) {
129eec55
VK
1168 if (opp->rate == freq) {
1169 found = true;
1170 break;
1171 }
1172 }
1173
37a73ec0
VK
1174 mutex_unlock(&opp_table->lock);
1175
5b650b38
VK
1176 if (found) {
1177 dev_pm_opp_put(opp);
0ad8c623
VK
1178
1179 /* Drop the reference taken by dev_pm_opp_add() */
1180 dev_pm_opp_put_opp_table(opp_table);
5b650b38 1181 } else {
129eec55
VK
1182 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1183 __func__, freq);
129eec55
VK
1184 }
1185
0ad8c623 1186 /* Drop the reference taken by _find_opp_table() */
5b650b38 1187 dev_pm_opp_put_opp_table(opp_table);
129eec55
VK
1188}
1189EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1190
03758d60
VK
1191void _opp_remove_all_static(struct opp_table *opp_table)
1192{
1193 struct dev_pm_opp *opp, *tmp;
1194
1195 mutex_lock(&opp_table->lock);
1196
1197 if (!opp_table->parsed_static_opps || --opp_table->parsed_static_opps)
1198 goto unlock;
1199
1200 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1201 if (!opp->dynamic)
1202 dev_pm_opp_put_unlocked(opp);
1203 }
1204
1205unlock:
1206 mutex_unlock(&opp_table->lock);
1207}
1208
1690d8bb
VK
1209/**
1210 * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
1211 * @dev: device for which we do this operation
1212 *
1213 * This function removes all dynamically created OPPs from the opp table.
1214 */
1215void dev_pm_opp_remove_all_dynamic(struct device *dev)
1216{
1217 struct opp_table *opp_table;
1218 struct dev_pm_opp *opp, *temp;
1219 int count = 0;
1220
1221 opp_table = _find_opp_table(dev);
1222 if (IS_ERR(opp_table))
1223 return;
1224
1225 mutex_lock(&opp_table->lock);
1226 list_for_each_entry_safe(opp, temp, &opp_table->opp_list, node) {
1227 if (opp->dynamic) {
1228 dev_pm_opp_put_unlocked(opp);
1229 count++;
1230 }
1231 }
1232 mutex_unlock(&opp_table->lock);
1233
1234 /* Drop the references taken by dev_pm_opp_add() */
1235 while (count--)
1236 dev_pm_opp_put_opp_table(opp_table);
1237
1238 /* Drop the reference taken by _find_opp_table() */
1239 dev_pm_opp_put_opp_table(opp_table);
1240}
1241EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
1242
8cd2f6e8 1243struct dev_pm_opp *_opp_allocate(struct opp_table *table)
e1f60b29 1244{
23dacf6d 1245 struct dev_pm_opp *opp;
dfbe4678 1246 int count, supply_size;
e1f60b29 1247
dfbe4678 1248 /* Allocate space for at least one supply */
46f48aca 1249 count = table->regulator_count > 0 ? table->regulator_count : 1;
dfbe4678 1250 supply_size = sizeof(*opp->supplies) * count;
e1f60b29 1251
dfbe4678
VK
1252 /* allocate new OPP node and supplies structures */
1253 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
8cd2f6e8 1254 if (!opp)
23dacf6d 1255 return NULL;
23dacf6d 1256
dfbe4678
VK
1257 /* Put the supplies at the end of the OPP structure as an empty array */
1258 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1259 INIT_LIST_HEAD(&opp->node);
1260
23dacf6d
VK
1261 return opp;
1262}
1263
7d34d56e 1264static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
2c2709dc 1265 struct opp_table *opp_table)
7d34d56e 1266{
dfbe4678
VK
1267 struct regulator *reg;
1268 int i;
1269
90e3577b
VK
1270 if (!opp_table->regulators)
1271 return true;
1272
dfbe4678
VK
1273 for (i = 0; i < opp_table->regulator_count; i++) {
1274 reg = opp_table->regulators[i];
1275
1276 if (!regulator_is_supported_voltage(reg,
1277 opp->supplies[i].u_volt_min,
1278 opp->supplies[i].u_volt_max)) {
1279 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1280 __func__, opp->supplies[i].u_volt_min,
1281 opp->supplies[i].u_volt_max);
1282 return false;
1283 }
7d34d56e
VK
1284 }
1285
1286 return true;
1287}
1288
6c591eec
SK
1289int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
1290{
1291 if (opp1->rate != opp2->rate)
1292 return opp1->rate < opp2->rate ? -1 : 1;
1293 if (opp1->level != opp2->level)
1294 return opp1->level < opp2->level ? -1 : 1;
1295 return 0;
1296}
1297
a1e8c136
VK
1298static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1299 struct opp_table *opp_table,
1300 struct list_head **head)
23dacf6d
VK
1301{
1302 struct dev_pm_opp *opp;
6c591eec 1303 int opp_cmp;
23dacf6d
VK
1304
1305 /*
1306 * Insert new OPP in order of increasing frequency and discard if
1307 * already present.
1308 *
2c2709dc 1309 * Need to use &opp_table->opp_list in the condition part of the 'for'
23dacf6d
VK
1310 * loop, don't replace it with head otherwise it will become an infinite
1311 * loop.
1312 */
052c6f19 1313 list_for_each_entry(opp, &opp_table->opp_list, node) {
6c591eec
SK
1314 opp_cmp = _opp_compare_key(new_opp, opp);
1315 if (opp_cmp > 0) {
a1e8c136 1316 *head = &opp->node;
23dacf6d
VK
1317 continue;
1318 }
1319
6c591eec 1320 if (opp_cmp < 0)
a1e8c136 1321 return 0;
23dacf6d
VK
1322
1323 /* Duplicate OPPs */
06441658 1324 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
dfbe4678
VK
1325 __func__, opp->rate, opp->supplies[0].u_volt,
1326 opp->available, new_opp->rate,
1327 new_opp->supplies[0].u_volt, new_opp->available);
23dacf6d 1328
dfbe4678 1329 /* Should we compare voltages for all regulators here ? */
a1e8c136
VK
1330 return opp->available &&
1331 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1332 }
1333
1334 return 0;
1335}
1336
1337/*
1338 * Returns:
1339 * 0: On success. And appropriate error message for duplicate OPPs.
1340 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1341 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1342 * sure we don't print error messages unnecessarily if different parts of
1343 * kernel try to initialize the OPP table.
1344 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1345 * should be considered an error by the callers of _opp_add().
1346 */
1347int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1348 struct opp_table *opp_table, bool rate_not_available)
1349{
1350 struct list_head *head;
1351 int ret;
1352
1353 mutex_lock(&opp_table->lock);
1354 head = &opp_table->opp_list;
37a73ec0 1355
a1e8c136
VK
1356 if (likely(!rate_not_available)) {
1357 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1358 if (ret) {
1359 mutex_unlock(&opp_table->lock);
1360 return ret;
1361 }
23dacf6d
VK
1362 }
1363
052c6f19 1364 list_add(&new_opp->node, head);
37a73ec0
VK
1365 mutex_unlock(&opp_table->lock);
1366
1367 new_opp->opp_table = opp_table;
7034764a 1368 kref_init(&new_opp->kref);
23dacf6d 1369
a2dea4cb 1370 opp_debug_create_one(new_opp, opp_table);
deaa5146 1371
2c2709dc 1372 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
7d34d56e
VK
1373 new_opp->available = false;
1374 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1375 __func__, new_opp->rate);
1376 }
1377
23dacf6d
VK
1378 return 0;
1379}
1380
984f16c8 1381/**
b64b9c3f 1382 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
8cd2f6e8 1383 * @opp_table: OPP table
984f16c8
NM
1384 * @dev: device for which we do this operation
1385 * @freq: Frequency in Hz for this OPP
1386 * @u_volt: Voltage in uVolts for this OPP
1387 * @dynamic: Dynamically added OPPs.
1388 *
2c2709dc 1389 * This function adds an opp definition to the opp table and returns status.
984f16c8
NM
1390 * The opp is made available by default and it can be controlled using
1391 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1392 *
8f8d37b2
VK
1393 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1394 * and freed by dev_pm_opp_of_remove_table.
984f16c8 1395 *
984f16c8
NM
1396 * Return:
1397 * 0 On success OR
1398 * Duplicate OPPs (both freq and volt are same) and opp->available
1399 * -EEXIST Freq are same and volt are different OR
1400 * Duplicate OPPs (both freq and volt are same) and !opp->available
1401 * -ENOMEM Memory allocation failure
1402 */
8cd2f6e8
VK
1403int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1404 unsigned long freq, long u_volt, bool dynamic)
e1f60b29 1405{
23dacf6d 1406 struct dev_pm_opp *new_opp;
50f8cfbd 1407 unsigned long tol;
6ce4184d 1408 int ret;
e1f60b29 1409
8cd2f6e8
VK
1410 new_opp = _opp_allocate(opp_table);
1411 if (!new_opp)
1412 return -ENOMEM;
23dacf6d 1413
a7470db6 1414 /* populate the opp table */
a7470db6 1415 new_opp->rate = freq;
2c2709dc 1416 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
dfbe4678
VK
1417 new_opp->supplies[0].u_volt = u_volt;
1418 new_opp->supplies[0].u_volt_min = u_volt - tol;
1419 new_opp->supplies[0].u_volt_max = u_volt + tol;
a7470db6 1420 new_opp->available = true;
23dacf6d 1421 new_opp->dynamic = dynamic;
a7470db6 1422
a1e8c136 1423 ret = _opp_add(dev, new_opp, opp_table, false);
7f8538eb
VK
1424 if (ret) {
1425 /* Don't return error for duplicate OPPs */
1426 if (ret == -EBUSY)
1427 ret = 0;
6ce4184d 1428 goto free_opp;
7f8538eb 1429 }
64ce8545 1430
03ca370f
MH
1431 /*
1432 * Notify the changes in the availability of the operable
1433 * frequency/voltage list.
1434 */
052c6f19 1435 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
e1f60b29 1436 return 0;
6ce4184d
VK
1437
1438free_opp:
8cd2f6e8
VK
1439 _opp_free(new_opp);
1440
6ce4184d 1441 return ret;
e1f60b29 1442}
38393409 1443
7de36b0a
VK
1444/**
1445 * dev_pm_opp_set_supported_hw() - Set supported platforms
1446 * @dev: Device for which supported-hw has to be set.
1447 * @versions: Array of hierarchy of versions to match.
1448 * @count: Number of elements in the array.
1449 *
1450 * This is required only for the V2 bindings, and it enables a platform to
1451 * specify the hierarchy of versions it supports. OPP layer will then enable
1452 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1453 * property.
7de36b0a 1454 */
fa30184d
VK
1455struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1456 const u32 *versions, unsigned int count)
7de36b0a 1457{
2c2709dc 1458 struct opp_table *opp_table;
7de36b0a 1459
fa30184d
VK
1460 opp_table = dev_pm_opp_get_opp_table(dev);
1461 if (!opp_table)
1462 return ERR_PTR(-ENOMEM);
7de36b0a 1463
2c2709dc
VK
1464 /* Make sure there are no concurrent readers while updating opp_table */
1465 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1466
25419de1
VK
1467 /* Another CPU that shares the OPP table has set the property ? */
1468 if (opp_table->supported_hw)
1469 return opp_table;
7de36b0a 1470
2c2709dc 1471 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
7de36b0a 1472 GFP_KERNEL);
2c2709dc 1473 if (!opp_table->supported_hw) {
25419de1
VK
1474 dev_pm_opp_put_opp_table(opp_table);
1475 return ERR_PTR(-ENOMEM);
7de36b0a
VK
1476 }
1477
2c2709dc 1478 opp_table->supported_hw_count = count;
fa30184d
VK
1479
1480 return opp_table;
7de36b0a
VK
1481}
1482EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1483
1484/**
1485 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
fa30184d 1486 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
7de36b0a
VK
1487 *
1488 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1489 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
7de36b0a 1490 * will not be freed.
7de36b0a 1491 */
fa30184d 1492void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
7de36b0a 1493{
2c2709dc
VK
1494 /* Make sure there are no concurrent readers while updating opp_table */
1495 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1496
2c2709dc
VK
1497 kfree(opp_table->supported_hw);
1498 opp_table->supported_hw = NULL;
1499 opp_table->supported_hw_count = 0;
7de36b0a 1500
fa30184d 1501 dev_pm_opp_put_opp_table(opp_table);
7de36b0a
VK
1502}
1503EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1504
01fb4d3c
VK
1505/**
1506 * dev_pm_opp_set_prop_name() - Set prop-extn name
a5da6447 1507 * @dev: Device for which the prop-name has to be set.
01fb4d3c
VK
1508 * @name: name to postfix to properties.
1509 *
1510 * This is required only for the V2 bindings, and it enables a platform to
1511 * specify the extn to be used for certain property names. The properties to
1512 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1513 * should postfix the property name with -<name> while looking for them.
01fb4d3c 1514 */
fa30184d 1515struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
01fb4d3c 1516{
2c2709dc 1517 struct opp_table *opp_table;
01fb4d3c 1518
fa30184d
VK
1519 opp_table = dev_pm_opp_get_opp_table(dev);
1520 if (!opp_table)
1521 return ERR_PTR(-ENOMEM);
01fb4d3c 1522
2c2709dc
VK
1523 /* Make sure there are no concurrent readers while updating opp_table */
1524 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1525
878ec1a9
VK
1526 /* Another CPU that shares the OPP table has set the property ? */
1527 if (opp_table->prop_name)
1528 return opp_table;
01fb4d3c 1529
2c2709dc
VK
1530 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1531 if (!opp_table->prop_name) {
878ec1a9
VK
1532 dev_pm_opp_put_opp_table(opp_table);
1533 return ERR_PTR(-ENOMEM);
01fb4d3c
VK
1534 }
1535
fa30184d 1536 return opp_table;
01fb4d3c
VK
1537}
1538EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1539
1540/**
1541 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
fa30184d 1542 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
01fb4d3c
VK
1543 *
1544 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1545 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
01fb4d3c 1546 * will not be freed.
01fb4d3c 1547 */
fa30184d 1548void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
01fb4d3c 1549{
2c2709dc
VK
1550 /* Make sure there are no concurrent readers while updating opp_table */
1551 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1552
2c2709dc
VK
1553 kfree(opp_table->prop_name);
1554 opp_table->prop_name = NULL;
01fb4d3c 1555
fa30184d 1556 dev_pm_opp_put_opp_table(opp_table);
01fb4d3c
VK
1557}
1558EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1559
94735585
VK
1560static int _allocate_set_opp_data(struct opp_table *opp_table)
1561{
1562 struct dev_pm_set_opp_data *data;
1563 int len, count = opp_table->regulator_count;
1564
90e3577b 1565 if (WARN_ON(!opp_table->regulators))
94735585
VK
1566 return -EINVAL;
1567
1568 /* space for set_opp_data */
1569 len = sizeof(*data);
1570
1571 /* space for old_opp.supplies and new_opp.supplies */
1572 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1573
1574 data = kzalloc(len, GFP_KERNEL);
1575 if (!data)
1576 return -ENOMEM;
1577
1578 data->old_opp.supplies = (void *)(data + 1);
1579 data->new_opp.supplies = data->old_opp.supplies + count;
1580
1581 opp_table->set_opp_data = data;
1582
1583 return 0;
1584}
1585
1586static void _free_set_opp_data(struct opp_table *opp_table)
1587{
1588 kfree(opp_table->set_opp_data);
1589 opp_table->set_opp_data = NULL;
1590}
1591
9f8ea969 1592/**
dfbe4678 1593 * dev_pm_opp_set_regulators() - Set regulator names for the device
9f8ea969 1594 * @dev: Device for which regulator name is being set.
dfbe4678
VK
1595 * @names: Array of pointers to the names of the regulator.
1596 * @count: Number of regulators.
9f8ea969
VK
1597 *
1598 * In order to support OPP switching, OPP layer needs to know the name of the
dfbe4678
VK
1599 * device's regulators, as the core would be required to switch voltages as
1600 * well.
9f8ea969
VK
1601 *
1602 * This must be called before any OPPs are initialized for the device.
9f8ea969 1603 */
dfbe4678
VK
1604struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1605 const char * const names[],
1606 unsigned int count)
9f8ea969 1607{
2c2709dc 1608 struct opp_table *opp_table;
9f8ea969 1609 struct regulator *reg;
dfbe4678 1610 int ret, i;
9f8ea969 1611
fa30184d
VK
1612 opp_table = dev_pm_opp_get_opp_table(dev);
1613 if (!opp_table)
1614 return ERR_PTR(-ENOMEM);
9f8ea969
VK
1615
1616 /* This should be called before OPPs are initialized */
2c2709dc 1617 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
9f8ea969
VK
1618 ret = -EBUSY;
1619 goto err;
1620 }
1621
779b783c
VK
1622 /* Another CPU that shares the OPP table has set the regulators ? */
1623 if (opp_table->regulators)
1624 return opp_table;
dfbe4678
VK
1625
1626 opp_table->regulators = kmalloc_array(count,
1627 sizeof(*opp_table->regulators),
1628 GFP_KERNEL);
1629 if (!opp_table->regulators) {
1630 ret = -ENOMEM;
9f8ea969
VK
1631 goto err;
1632 }
1633
dfbe4678
VK
1634 for (i = 0; i < count; i++) {
1635 reg = regulator_get_optional(dev, names[i]);
1636 if (IS_ERR(reg)) {
1637 ret = PTR_ERR(reg);
1638 if (ret != -EPROBE_DEFER)
1639 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1640 __func__, names[i], ret);
1641 goto free_regulators;
1642 }
1643
1644 opp_table->regulators[i] = reg;
1645 }
1646
1647 opp_table->regulator_count = count;
9f8ea969 1648
94735585
VK
1649 /* Allocate block only once to pass to set_opp() routines */
1650 ret = _allocate_set_opp_data(opp_table);
1651 if (ret)
1652 goto free_regulators;
1653
91291d9a 1654 return opp_table;
9f8ea969 1655
dfbe4678 1656free_regulators:
24957db1
MS
1657 while (i != 0)
1658 regulator_put(opp_table->regulators[--i]);
dfbe4678
VK
1659
1660 kfree(opp_table->regulators);
1661 opp_table->regulators = NULL;
46f48aca 1662 opp_table->regulator_count = -1;
9f8ea969 1663err:
fa30184d 1664 dev_pm_opp_put_opp_table(opp_table);
9f8ea969 1665
91291d9a 1666 return ERR_PTR(ret);
9f8ea969 1667}
dfbe4678 1668EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
9f8ea969
VK
1669
1670/**
dfbe4678
VK
1671 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1672 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
9f8ea969 1673 */
dfbe4678 1674void dev_pm_opp_put_regulators(struct opp_table *opp_table)
9f8ea969 1675{
dfbe4678
VK
1676 int i;
1677
779b783c
VK
1678 if (!opp_table->regulators)
1679 goto put_opp_table;
9f8ea969 1680
2c2709dc
VK
1681 /* Make sure there are no concurrent readers while updating opp_table */
1682 WARN_ON(!list_empty(&opp_table->opp_list));
9f8ea969 1683
24957db1 1684 for (i = opp_table->regulator_count - 1; i >= 0; i--)
dfbe4678
VK
1685 regulator_put(opp_table->regulators[i]);
1686
94735585
VK
1687 _free_set_opp_data(opp_table);
1688
dfbe4678
VK
1689 kfree(opp_table->regulators);
1690 opp_table->regulators = NULL;
46f48aca 1691 opp_table->regulator_count = -1;
9f8ea969 1692
779b783c 1693put_opp_table:
fa30184d 1694 dev_pm_opp_put_opp_table(opp_table);
9f8ea969 1695}
dfbe4678 1696EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
9f8ea969 1697
829a4e8c
VK
1698/**
1699 * dev_pm_opp_set_clkname() - Set clk name for the device
1700 * @dev: Device for which clk name is being set.
1701 * @name: Clk name.
1702 *
1703 * In order to support OPP switching, OPP layer needs to get pointer to the
1704 * clock for the device. Simple cases work fine without using this routine (i.e.
1705 * by passing connection-id as NULL), but for a device with multiple clocks
1706 * available, the OPP core needs to know the exact name of the clk to use.
1707 *
1708 * This must be called before any OPPs are initialized for the device.
1709 */
1710struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
1711{
1712 struct opp_table *opp_table;
1713 int ret;
1714
1715 opp_table = dev_pm_opp_get_opp_table(dev);
1716 if (!opp_table)
1717 return ERR_PTR(-ENOMEM);
1718
1719 /* This should be called before OPPs are initialized */
1720 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1721 ret = -EBUSY;
1722 goto err;
1723 }
1724
1725 /* Already have default clk set, free it */
1726 if (!IS_ERR(opp_table->clk))
1727 clk_put(opp_table->clk);
1728
1729 /* Find clk for the device */
1730 opp_table->clk = clk_get(dev, name);
1731 if (IS_ERR(opp_table->clk)) {
1732 ret = PTR_ERR(opp_table->clk);
1733 if (ret != -EPROBE_DEFER) {
1734 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
1735 ret);
1736 }
1737 goto err;
1738 }
1739
1740 return opp_table;
1741
1742err:
1743 dev_pm_opp_put_opp_table(opp_table);
1744
1745 return ERR_PTR(ret);
1746}
1747EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
1748
1749/**
1750 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1751 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1752 */
1753void dev_pm_opp_put_clkname(struct opp_table *opp_table)
1754{
1755 /* Make sure there are no concurrent readers while updating opp_table */
1756 WARN_ON(!list_empty(&opp_table->opp_list));
1757
1758 clk_put(opp_table->clk);
1759 opp_table->clk = ERR_PTR(-EINVAL);
1760
1761 dev_pm_opp_put_opp_table(opp_table);
1762}
1763EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
1764
4dab160e
VK
1765/**
1766 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1767 * @dev: Device for which the helper is getting registered.
1768 * @set_opp: Custom set OPP helper.
1769 *
1770 * This is useful to support complex platforms (like platforms with multiple
1771 * regulators per device), instead of the generic OPP set rate helper.
1772 *
1773 * This must be called before any OPPs are initialized for the device.
4dab160e 1774 */
fa30184d 1775struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
4dab160e
VK
1776 int (*set_opp)(struct dev_pm_set_opp_data *data))
1777{
1778 struct opp_table *opp_table;
4dab160e
VK
1779
1780 if (!set_opp)
fa30184d 1781 return ERR_PTR(-EINVAL);
4dab160e 1782
fa30184d
VK
1783 opp_table = dev_pm_opp_get_opp_table(dev);
1784 if (!opp_table)
1785 return ERR_PTR(-ENOMEM);
4dab160e
VK
1786
1787 /* This should be called before OPPs are initialized */
1788 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
5019acc6
VK
1789 dev_pm_opp_put_opp_table(opp_table);
1790 return ERR_PTR(-EBUSY);
4dab160e
VK
1791 }
1792
5019acc6
VK
1793 /* Another CPU that shares the OPP table has set the helper ? */
1794 if (!opp_table->set_opp)
1795 opp_table->set_opp = set_opp;
4dab160e 1796
fa30184d 1797 return opp_table;
4dab160e
VK
1798}
1799EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1800
1801/**
604a7aeb 1802 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
4dab160e 1803 * set_opp helper
fa30184d 1804 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
4dab160e 1805 *
fa30184d 1806 * Release resources blocked for platform specific set_opp helper.
4dab160e 1807 */
604a7aeb 1808void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
4dab160e 1809{
4dab160e
VK
1810 /* Make sure there are no concurrent readers while updating opp_table */
1811 WARN_ON(!list_empty(&opp_table->opp_list));
1812
1813 opp_table->set_opp = NULL;
fa30184d 1814 dev_pm_opp_put_opp_table(opp_table);
4dab160e 1815}
604a7aeb 1816EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
4dab160e 1817
6319aee1
VK
1818static void _opp_detach_genpd(struct opp_table *opp_table)
1819{
1820 int index;
1821
1822 for (index = 0; index < opp_table->required_opp_count; index++) {
1823 if (!opp_table->genpd_virt_devs[index])
1824 continue;
1825
1826 dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
1827 opp_table->genpd_virt_devs[index] = NULL;
1828 }
c0ab9e08
VK
1829
1830 kfree(opp_table->genpd_virt_devs);
1831 opp_table->genpd_virt_devs = NULL;
6319aee1
VK
1832}
1833
4f018bc0 1834/**
6319aee1
VK
1835 * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
1836 * @dev: Consumer device for which the genpd is getting attached.
1837 * @names: Null terminated array of pointers containing names of genpd to attach.
17a8f868 1838 * @virt_devs: Pointer to return the array of virtual devices.
4f018bc0
VK
1839 *
1840 * Multiple generic power domains for a device are supported with the help of
1841 * virtual genpd devices, which are created for each consumer device - genpd
1842 * pair. These are the device structures which are attached to the power domain
1843 * and are required by the OPP core to set the performance state of the genpd.
6319aee1
VK
1844 * The same API also works for the case where single genpd is available and so
1845 * we don't need to support that separately.
4f018bc0
VK
1846 *
1847 * This helper will normally be called by the consumer driver of the device
6319aee1 1848 * "dev", as only that has details of the genpd names.
4f018bc0 1849 *
6319aee1
VK
1850 * This helper needs to be called once with a list of all genpd to attach.
1851 * Otherwise the original device structure will be used instead by the OPP core.
baea35e4
VK
1852 *
1853 * The order of entries in the names array must match the order in which
1854 * "required-opps" are added in DT.
4f018bc0 1855 */
17a8f868
VK
1856struct opp_table *dev_pm_opp_attach_genpd(struct device *dev,
1857 const char **names, struct device ***virt_devs)
4f018bc0
VK
1858{
1859 struct opp_table *opp_table;
6319aee1 1860 struct device *virt_dev;
baea35e4 1861 int index = 0, ret = -EINVAL;
6319aee1 1862 const char **name = names;
4f018bc0
VK
1863
1864 opp_table = dev_pm_opp_get_opp_table(dev);
1865 if (!opp_table)
1866 return ERR_PTR(-ENOMEM);
1867
6319aee1
VK
1868 /*
1869 * If the genpd's OPP table isn't already initialized, parsing of the
1870 * required-opps fail for dev. We should retry this after genpd's OPP
1871 * table is added.
1872 */
1873 if (!opp_table->required_opp_count) {
1874 ret = -EPROBE_DEFER;
1875 goto put_table;
1876 }
1877
4f018bc0
VK
1878 mutex_lock(&opp_table->genpd_virt_dev_lock);
1879
c0ab9e08
VK
1880 opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
1881 sizeof(*opp_table->genpd_virt_devs),
1882 GFP_KERNEL);
1883 if (!opp_table->genpd_virt_devs)
1884 goto unlock;
4f018bc0 1885
6319aee1 1886 while (*name) {
6319aee1
VK
1887 if (index >= opp_table->required_opp_count) {
1888 dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
1889 *name, opp_table->required_opp_count, index);
1890 goto err;
1891 }
4f018bc0 1892
6319aee1
VK
1893 if (opp_table->genpd_virt_devs[index]) {
1894 dev_err(dev, "Genpd virtual device already set %s\n",
1895 *name);
1896 goto err;
1897 }
1898
1899 virt_dev = dev_pm_domain_attach_by_name(dev, *name);
1900 if (IS_ERR(virt_dev)) {
1901 ret = PTR_ERR(virt_dev);
1902 dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
1903 goto err;
1904 }
1905
1906 opp_table->genpd_virt_devs[index] = virt_dev;
baea35e4 1907 index++;
6319aee1 1908 name++;
4f018bc0
VK
1909 }
1910
17a8f868
VK
1911 if (virt_devs)
1912 *virt_devs = opp_table->genpd_virt_devs;
4f018bc0
VK
1913 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1914
1915 return opp_table;
6319aee1
VK
1916
1917err:
1918 _opp_detach_genpd(opp_table);
c0ab9e08 1919unlock:
6319aee1
VK
1920 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1921
1922put_table:
1923 dev_pm_opp_put_opp_table(opp_table);
1924
1925 return ERR_PTR(ret);
4f018bc0 1926}
6319aee1 1927EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd);
4f018bc0
VK
1928
1929/**
6319aee1
VK
1930 * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device.
1931 * @opp_table: OPP table returned by dev_pm_opp_attach_genpd().
4f018bc0 1932 *
6319aee1
VK
1933 * This detaches the genpd(s), resets the virtual device pointers, and puts the
1934 * OPP table.
4f018bc0 1935 */
6319aee1 1936void dev_pm_opp_detach_genpd(struct opp_table *opp_table)
4f018bc0 1937{
4f018bc0
VK
1938 /*
1939 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
1940 * used in parallel.
1941 */
1942 mutex_lock(&opp_table->genpd_virt_dev_lock);
6319aee1 1943 _opp_detach_genpd(opp_table);
4f018bc0
VK
1944 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1945
6319aee1 1946 dev_pm_opp_put_opp_table(opp_table);
4f018bc0 1947}
6319aee1 1948EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd);
4f018bc0 1949
c8a59103
VK
1950/**
1951 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
1952 * @src_table: OPP table which has dst_table as one of its required OPP table.
1953 * @dst_table: Required OPP table of the src_table.
1954 * @pstate: Current performance state of the src_table.
1955 *
1956 * This Returns pstate of the OPP (present in @dst_table) pointed out by the
1957 * "required-opps" property of the OPP (present in @src_table) which has
1958 * performance state set to @pstate.
1959 *
1960 * Return: Zero or positive performance state on success, otherwise negative
1961 * value on errors.
1962 */
1963int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
1964 struct opp_table *dst_table,
1965 unsigned int pstate)
1966{
1967 struct dev_pm_opp *opp;
1968 int dest_pstate = -EINVAL;
1969 int i;
1970
1971 if (!pstate)
1972 return 0;
1973
1974 /*
1975 * Normally the src_table will have the "required_opps" property set to
1976 * point to one of the OPPs in the dst_table, but in some cases the
1977 * genpd and its master have one to one mapping of performance states
1978 * and so none of them have the "required-opps" property set. Return the
1979 * pstate of the src_table as it is in such cases.
1980 */
1981 if (!src_table->required_opp_count)
1982 return pstate;
1983
1984 for (i = 0; i < src_table->required_opp_count; i++) {
1985 if (src_table->required_opp_tables[i]->np == dst_table->np)
1986 break;
1987 }
1988
1989 if (unlikely(i == src_table->required_opp_count)) {
1990 pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
1991 __func__, src_table, dst_table);
1992 return -EINVAL;
1993 }
1994
1995 mutex_lock(&src_table->lock);
1996
1997 list_for_each_entry(opp, &src_table->opp_list, node) {
1998 if (opp->pstate == pstate) {
1999 dest_pstate = opp->required_opps[i]->pstate;
2000 goto unlock;
2001 }
2002 }
2003
2004 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2005 dst_table);
2006
2007unlock:
2008 mutex_unlock(&src_table->lock);
2009
2010 return dest_pstate;
2011}
2012
38393409
VK
2013/**
2014 * dev_pm_opp_add() - Add an OPP table from a table definitions
2015 * @dev: device for which we do this operation
2016 * @freq: Frequency in Hz for this OPP
2017 * @u_volt: Voltage in uVolts for this OPP
2018 *
2c2709dc 2019 * This function adds an opp definition to the opp table and returns status.
38393409
VK
2020 * The opp is made available by default and it can be controlled using
2021 * dev_pm_opp_enable/disable functions.
2022 *
38393409 2023 * Return:
984f16c8 2024 * 0 On success OR
38393409 2025 * Duplicate OPPs (both freq and volt are same) and opp->available
984f16c8 2026 * -EEXIST Freq are same and volt are different OR
38393409 2027 * Duplicate OPPs (both freq and volt are same) and !opp->available
984f16c8 2028 * -ENOMEM Memory allocation failure
38393409
VK
2029 */
2030int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
2031{
8cd2f6e8
VK
2032 struct opp_table *opp_table;
2033 int ret;
2034
b83c1899
VK
2035 opp_table = dev_pm_opp_get_opp_table(dev);
2036 if (!opp_table)
2037 return -ENOMEM;
8cd2f6e8 2038
46f48aca
VK
2039 /* Fix regulator count for dynamic OPPs */
2040 opp_table->regulator_count = 1;
2041
8cd2f6e8 2042 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
0ad8c623
VK
2043 if (ret)
2044 dev_pm_opp_put_opp_table(opp_table);
8cd2f6e8 2045
8cd2f6e8 2046 return ret;
38393409 2047}
5d4879cd 2048EXPORT_SYMBOL_GPL(dev_pm_opp_add);
e1f60b29
NM
2049
2050/**
327854c8 2051 * _opp_set_availability() - helper to set the availability of an opp
e1f60b29
NM
2052 * @dev: device for which we do this operation
2053 * @freq: OPP frequency to modify availability
2054 * @availability_req: availability status requested for this opp
2055 *
052c6f19
VK
2056 * Set the availability of an OPP, opp_{enable,disable} share a common logic
2057 * which is isolated here.
e1f60b29 2058 *
984f16c8 2059 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 2060 * copy operation, returns 0 if no modification was done OR modification was
e1f60b29 2061 * successful.
e1f60b29 2062 */
327854c8
NM
2063static int _opp_set_availability(struct device *dev, unsigned long freq,
2064 bool availability_req)
e1f60b29 2065{
2c2709dc 2066 struct opp_table *opp_table;
a7f3987e 2067 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
e1f60b29
NM
2068 int r = 0;
2069
2c2709dc
VK
2070 /* Find the opp_table */
2071 opp_table = _find_opp_table(dev);
2072 if (IS_ERR(opp_table)) {
2073 r = PTR_ERR(opp_table);
e1f60b29 2074 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
a7f3987e 2075 return r;
e1f60b29
NM
2076 }
2077
37a73ec0
VK
2078 mutex_lock(&opp_table->lock);
2079
e1f60b29 2080 /* Do we have the frequency? */
2c2709dc 2081 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
2082 if (tmp_opp->rate == freq) {
2083 opp = tmp_opp;
2084 break;
2085 }
2086 }
37a73ec0 2087
e1f60b29
NM
2088 if (IS_ERR(opp)) {
2089 r = PTR_ERR(opp);
2090 goto unlock;
2091 }
2092
2093 /* Is update really needed? */
2094 if (opp->available == availability_req)
2095 goto unlock;
e1f60b29 2096
a7f3987e 2097 opp->available = availability_req;
e1f60b29 2098
e4d8ae00
VK
2099 dev_pm_opp_get(opp);
2100 mutex_unlock(&opp_table->lock);
2101
03ca370f
MH
2102 /* Notify the change of the OPP availability */
2103 if (availability_req)
052c6f19 2104 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
a7f3987e 2105 opp);
03ca370f 2106 else
052c6f19 2107 blocking_notifier_call_chain(&opp_table->head,
a7f3987e 2108 OPP_EVENT_DISABLE, opp);
e1f60b29 2109
e4d8ae00
VK
2110 dev_pm_opp_put(opp);
2111 goto put_table;
2112
e1f60b29 2113unlock:
5b650b38 2114 mutex_unlock(&opp_table->lock);
e4d8ae00 2115put_table:
5b650b38 2116 dev_pm_opp_put_opp_table(opp_table);
e1f60b29
NM
2117 return r;
2118}
2119
25cb20a2
SB
2120/**
2121 * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
2122 * @dev: device for which we do this operation
2123 * @freq: OPP frequency to adjust voltage of
2124 * @u_volt: new OPP target voltage
2125 * @u_volt_min: new OPP min voltage
2126 * @u_volt_max: new OPP max voltage
2127 *
2128 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2129 * copy operation, returns 0 if no modifcation was done OR modification was
2130 * successful.
2131 */
2132int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
2133 unsigned long u_volt, unsigned long u_volt_min,
2134 unsigned long u_volt_max)
2135
2136{
2137 struct opp_table *opp_table;
2138 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
2139 int r = 0;
2140
2141 /* Find the opp_table */
2142 opp_table = _find_opp_table(dev);
2143 if (IS_ERR(opp_table)) {
2144 r = PTR_ERR(opp_table);
2145 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
2146 return r;
2147 }
2148
2149 mutex_lock(&opp_table->lock);
2150
2151 /* Do we have the frequency? */
2152 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
2153 if (tmp_opp->rate == freq) {
2154 opp = tmp_opp;
2155 break;
2156 }
2157 }
2158
2159 if (IS_ERR(opp)) {
2160 r = PTR_ERR(opp);
2161 goto adjust_unlock;
2162 }
2163
2164 /* Is update really needed? */
2165 if (opp->supplies->u_volt == u_volt)
2166 goto adjust_unlock;
2167
2168 opp->supplies->u_volt = u_volt;
2169 opp->supplies->u_volt_min = u_volt_min;
2170 opp->supplies->u_volt_max = u_volt_max;
2171
2172 dev_pm_opp_get(opp);
2173 mutex_unlock(&opp_table->lock);
2174
2175 /* Notify the voltage change of the OPP */
2176 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
2177 opp);
2178
2179 dev_pm_opp_put(opp);
2180 goto adjust_put_table;
2181
2182adjust_unlock:
2183 mutex_unlock(&opp_table->lock);
2184adjust_put_table:
2185 dev_pm_opp_put_opp_table(opp_table);
2186 return r;
2187}
2188
e1f60b29 2189/**
5d4879cd 2190 * dev_pm_opp_enable() - Enable a specific OPP
e1f60b29
NM
2191 * @dev: device for which we do this operation
2192 * @freq: OPP frequency to enable
2193 *
2194 * Enables a provided opp. If the operation is valid, this returns 0, else the
2195 * corresponding error value. It is meant to be used for users an OPP available
5d4879cd 2196 * after being temporarily made unavailable with dev_pm_opp_disable.
e1f60b29 2197 *
984f16c8 2198 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 2199 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 2200 * successful.
e1f60b29 2201 */
5d4879cd 2202int dev_pm_opp_enable(struct device *dev, unsigned long freq)
e1f60b29 2203{
327854c8 2204 return _opp_set_availability(dev, freq, true);
e1f60b29 2205}
5d4879cd 2206EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
e1f60b29
NM
2207
2208/**
5d4879cd 2209 * dev_pm_opp_disable() - Disable a specific OPP
e1f60b29
NM
2210 * @dev: device for which we do this operation
2211 * @freq: OPP frequency to disable
2212 *
2213 * Disables a provided opp. If the operation is valid, this returns
2214 * 0, else the corresponding error value. It is meant to be a temporary
2215 * control by users to make this OPP not available until the circumstances are
5d4879cd 2216 * right to make it available again (with a call to dev_pm_opp_enable).
e1f60b29 2217 *
984f16c8 2218 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 2219 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 2220 * successful.
e1f60b29 2221 */
5d4879cd 2222int dev_pm_opp_disable(struct device *dev, unsigned long freq)
e1f60b29 2223{
327854c8 2224 return _opp_set_availability(dev, freq, false);
e1f60b29 2225}
5d4879cd 2226EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
e1f60b29 2227
03ca370f 2228/**
dc2c9ad5
VK
2229 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
2230 * @dev: Device for which notifier needs to be registered
2231 * @nb: Notifier block to be registered
984f16c8 2232 *
dc2c9ad5
VK
2233 * Return: 0 on success or a negative error value.
2234 */
2235int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
2236{
2237 struct opp_table *opp_table;
2238 int ret;
2239
dc2c9ad5 2240 opp_table = _find_opp_table(dev);
5b650b38
VK
2241 if (IS_ERR(opp_table))
2242 return PTR_ERR(opp_table);
2243
052c6f19 2244 ret = blocking_notifier_chain_register(&opp_table->head, nb);
dc2c9ad5 2245
5b650b38 2246 dev_pm_opp_put_opp_table(opp_table);
dc2c9ad5
VK
2247
2248 return ret;
2249}
2250EXPORT_SYMBOL(dev_pm_opp_register_notifier);
2251
2252/**
2253 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
2254 * @dev: Device for which notifier needs to be unregistered
2255 * @nb: Notifier block to be unregistered
984f16c8 2256 *
dc2c9ad5 2257 * Return: 0 on success or a negative error value.
03ca370f 2258 */
dc2c9ad5
VK
2259int dev_pm_opp_unregister_notifier(struct device *dev,
2260 struct notifier_block *nb)
03ca370f 2261{
dc2c9ad5
VK
2262 struct opp_table *opp_table;
2263 int ret;
03ca370f 2264
dc2c9ad5 2265 opp_table = _find_opp_table(dev);
5b650b38
VK
2266 if (IS_ERR(opp_table))
2267 return PTR_ERR(opp_table);
dc2c9ad5 2268
052c6f19 2269 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
03ca370f 2270
5b650b38 2271 dev_pm_opp_put_opp_table(opp_table);
dc2c9ad5
VK
2272
2273 return ret;
03ca370f 2274}
dc2c9ad5 2275EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
b496dfbc 2276
2a4eb735 2277void _dev_pm_opp_find_and_remove_table(struct device *dev)
9274c892
VK
2278{
2279 struct opp_table *opp_table;
2280
2c2709dc
VK
2281 /* Check for existing table for 'dev' */
2282 opp_table = _find_opp_table(dev);
2283 if (IS_ERR(opp_table)) {
2284 int error = PTR_ERR(opp_table);
737002b5
VK
2285
2286 if (error != -ENODEV)
2c2709dc 2287 WARN(1, "%s: opp_table: %d\n",
737002b5
VK
2288 IS_ERR_OR_NULL(dev) ?
2289 "Invalid device" : dev_name(dev),
2290 error);
5b650b38 2291 return;
737002b5
VK
2292 }
2293
03758d60 2294 _opp_remove_all_static(opp_table);
cdd6ed90
VK
2295
2296 /* Drop reference taken by _find_opp_table() */
2297 dev_pm_opp_put_opp_table(opp_table);
737002b5 2298
cdd6ed90 2299 /* Drop reference taken while the OPP table was added */
5b650b38 2300 dev_pm_opp_put_opp_table(opp_table);
737002b5 2301}
129eec55
VK
2302
2303/**
411466c5 2304 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
2c2709dc 2305 * @dev: device pointer used to lookup OPP table.
129eec55 2306 *
411466c5
SH
2307 * Free both OPPs created using static entries present in DT and the
2308 * dynamically added entries.
129eec55 2309 */
411466c5 2310void dev_pm_opp_remove_table(struct device *dev)
129eec55 2311{
2a4eb735 2312 _dev_pm_opp_find_and_remove_table(dev);
8d4d4e98 2313}
411466c5 2314EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);