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