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