PM / OPP: Separate out _generic_set_opp()
[linux-2.6-block.git] / drivers / base / power / 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>
9f8ea969 22#include <linux/regulator/consumer.h>
e1f60b29 23
f59d3ee8 24#include "opp.h"
e1f60b29
NM
25
26/*
2c2709dc
VK
27 * The root of the list of all opp-tables. All opp_table structures branch off
28 * from here, with each opp_table containing the list of opps it supports in
e1f60b29
NM
29 * various states of availability.
30 */
f47b72a1 31LIST_HEAD(opp_tables);
e1f60b29 32/* Lock to allow exclusive modification to the device and opp lists */
2c2709dc 33DEFINE_MUTEX(opp_table_lock);
e1f60b29 34
b02ded24
DT
35#define opp_rcu_lockdep_assert() \
36do { \
f78f5b90 37 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
2c2709dc
VK
38 !lockdep_is_held(&opp_table_lock), \
39 "Missing rcu_read_lock() or " \
40 "opp_table_lock protection"); \
b02ded24
DT
41} while (0)
42
2c2709dc
VK
43static struct opp_device *_find_opp_dev(const struct device *dev,
44 struct opp_table *opp_table)
06441658 45{
2c2709dc 46 struct opp_device *opp_dev;
06441658 47
2c2709dc
VK
48 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
49 if (opp_dev->dev == dev)
50 return opp_dev;
06441658
VK
51
52 return NULL;
53}
54
e1f60b29 55/**
2c2709dc
VK
56 * _find_opp_table() - find opp_table struct using device pointer
57 * @dev: device pointer used to lookup OPP table
e1f60b29 58 *
2c2709dc
VK
59 * Search OPP table for one containing matching device. Does a RCU reader
60 * operation to grab the pointer needed.
e1f60b29 61 *
2c2709dc 62 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
e1f60b29
NM
63 * -EINVAL based on type of error.
64 *
0597e818 65 * Locking: For readers, this function must be called under rcu_read_lock().
2c2709dc 66 * opp_table is a RCU protected pointer, which means that opp_table is valid
0597e818
VK
67 * as long as we are under RCU lock.
68 *
2c2709dc 69 * For Writers, this function must be called with opp_table_lock held.
e1f60b29 70 */
2c2709dc 71struct opp_table *_find_opp_table(struct device *dev)
e1f60b29 72{
2c2709dc 73 struct opp_table *opp_table;
e1f60b29 74
0597e818
VK
75 opp_rcu_lockdep_assert();
76
50a3cb04 77 if (IS_ERR_OR_NULL(dev)) {
e1f60b29
NM
78 pr_err("%s: Invalid parameters\n", __func__);
79 return ERR_PTR(-EINVAL);
80 }
81
2c2709dc
VK
82 list_for_each_entry_rcu(opp_table, &opp_tables, node)
83 if (_find_opp_dev(dev, opp_table))
84 return opp_table;
e1f60b29 85
06441658 86 return ERR_PTR(-ENODEV);
e1f60b29
NM
87}
88
89/**
d6d00742 90 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
e1f60b29
NM
91 * @opp: opp for which voltage has to be returned for
92 *
984f16c8 93 * Return: voltage in micro volt corresponding to the opp, else
e1f60b29
NM
94 * return 0
95 *
dfbe4678
VK
96 * This is useful only for devices with single power supply.
97 *
e1f60b29
NM
98 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
99 * protected pointer. This means that opp which could have been fetched by
100 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
101 * under RCU lock. The pointer returned by the opp_find_freq family must be
102 * used in the same section as the usage of this function with the pointer
103 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
104 * pointer.
105 */
47d43ba7 106unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
e1f60b29 107{
47d43ba7 108 struct dev_pm_opp *tmp_opp;
e1f60b29
NM
109 unsigned long v = 0;
110
04bf1c7f
KK
111 opp_rcu_lockdep_assert();
112
e1f60b29 113 tmp_opp = rcu_dereference(opp);
d6d00742 114 if (IS_ERR_OR_NULL(tmp_opp))
e1f60b29
NM
115 pr_err("%s: Invalid parameters\n", __func__);
116 else
dfbe4678 117 v = tmp_opp->supplies[0].u_volt;
e1f60b29
NM
118
119 return v;
120}
5d4879cd 121EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
e1f60b29
NM
122
123/**
5d4879cd 124 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
e1f60b29
NM
125 * @opp: opp for which frequency has to be returned for
126 *
984f16c8 127 * Return: frequency in hertz corresponding to the opp, else
e1f60b29
NM
128 * return 0
129 *
130 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
131 * protected pointer. This means that opp which could have been fetched by
132 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
133 * under RCU lock. The pointer returned by the opp_find_freq family must be
134 * used in the same section as the usage of this function with the pointer
135 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
136 * pointer.
137 */
47d43ba7 138unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
e1f60b29 139{
47d43ba7 140 struct dev_pm_opp *tmp_opp;
e1f60b29
NM
141 unsigned long f = 0;
142
04bf1c7f
KK
143 opp_rcu_lockdep_assert();
144
e1f60b29 145 tmp_opp = rcu_dereference(opp);
50a3cb04 146 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available)
e1f60b29
NM
147 pr_err("%s: Invalid parameters\n", __func__);
148 else
149 f = tmp_opp->rate;
150
151 return f;
152}
5d4879cd 153EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
e1f60b29 154
19445b25
BZ
155/**
156 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
157 * @opp: opp for which turbo mode is being verified
158 *
159 * Turbo OPPs are not for normal use, and can be enabled (under certain
160 * conditions) for short duration of times to finish high throughput work
161 * quickly. Running on them for longer times may overheat the chip.
162 *
163 * Return: true if opp is turbo opp, else false.
164 *
165 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
166 * protected pointer. This means that opp which could have been fetched by
167 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
168 * under RCU lock. The pointer returned by the opp_find_freq family must be
169 * used in the same section as the usage of this function with the pointer
170 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
171 * pointer.
172 */
173bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
174{
175 struct dev_pm_opp *tmp_opp;
176
177 opp_rcu_lockdep_assert();
178
179 tmp_opp = rcu_dereference(opp);
180 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available) {
181 pr_err("%s: Invalid parameters\n", __func__);
182 return false;
183 }
184
185 return tmp_opp->turbo;
186}
187EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
188
3ca9bb33
VK
189/**
190 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
191 * @dev: device for which we do this operation
192 *
193 * Return: This function returns the max clock latency in nanoseconds.
194 *
195 * Locking: This function takes rcu_read_lock().
196 */
197unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
198{
2c2709dc 199 struct opp_table *opp_table;
3ca9bb33
VK
200 unsigned long clock_latency_ns;
201
202 rcu_read_lock();
203
2c2709dc
VK
204 opp_table = _find_opp_table(dev);
205 if (IS_ERR(opp_table))
3ca9bb33
VK
206 clock_latency_ns = 0;
207 else
2c2709dc 208 clock_latency_ns = opp_table->clock_latency_ns_max;
3ca9bb33
VK
209
210 rcu_read_unlock();
211 return clock_latency_ns;
212}
213EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
214
dfbe4678
VK
215static int _get_regulator_count(struct device *dev)
216{
217 struct opp_table *opp_table;
218 int count;
219
220 rcu_read_lock();
221
222 opp_table = _find_opp_table(dev);
223 if (!IS_ERR(opp_table))
224 count = opp_table->regulator_count;
225 else
226 count = 0;
227
228 rcu_read_unlock();
229
230 return count;
231}
232
655c9df9
VK
233/**
234 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
235 * @dev: device for which we do this operation
236 *
237 * Return: This function returns the max voltage latency in nanoseconds.
238 *
239 * Locking: This function takes rcu_read_lock().
240 */
241unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
242{
2c2709dc 243 struct opp_table *opp_table;
655c9df9 244 struct dev_pm_opp *opp;
dfbe4678 245 struct regulator *reg, **regulators;
655c9df9 246 unsigned long latency_ns = 0;
dfbe4678
VK
247 int ret, i, count;
248 struct {
249 unsigned long min;
250 unsigned long max;
251 } *uV;
252
253 count = _get_regulator_count(dev);
254
255 /* Regulator may not be required for the device */
256 if (!count)
257 return 0;
258
259 regulators = kmalloc_array(count, sizeof(*regulators), GFP_KERNEL);
260 if (!regulators)
261 return 0;
262
263 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
264 if (!uV)
265 goto free_regulators;
655c9df9
VK
266
267 rcu_read_lock();
268
2c2709dc
VK
269 opp_table = _find_opp_table(dev);
270 if (IS_ERR(opp_table)) {
655c9df9 271 rcu_read_unlock();
dfbe4678 272 goto free_uV;
655c9df9
VK
273 }
274
dfbe4678 275 memcpy(regulators, opp_table->regulators, count * sizeof(*regulators));
655c9df9 276
dfbe4678
VK
277 for (i = 0; i < count; i++) {
278 uV[i].min = ~0;
279 uV[i].max = 0;
655c9df9 280
dfbe4678
VK
281 list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
282 if (!opp->available)
283 continue;
284
285 if (opp->supplies[i].u_volt_min < uV[i].min)
286 uV[i].min = opp->supplies[i].u_volt_min;
287 if (opp->supplies[i].u_volt_max > uV[i].max)
288 uV[i].max = opp->supplies[i].u_volt_max;
289 }
655c9df9
VK
290 }
291
292 rcu_read_unlock();
293
294 /*
2c2709dc 295 * The caller needs to ensure that opp_table (and hence the regulator)
655c9df9
VK
296 * isn't freed, while we are executing this routine.
297 */
dfbe4678
VK
298 for (i = 0; reg = regulators[i], i < count; i++) {
299 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
300 if (ret > 0)
301 latency_ns += ret * 1000;
302 }
303
304free_uV:
305 kfree(uV);
306free_regulators:
307 kfree(regulators);
655c9df9
VK
308
309 return latency_ns;
310}
311EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
312
21743447
VK
313/**
314 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
315 * nanoseconds
316 * @dev: device for which we do this operation
317 *
318 * Return: This function returns the max transition latency, in nanoseconds, to
319 * switch from one OPP to other.
320 *
321 * Locking: This function takes rcu_read_lock().
322 */
323unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
324{
325 return dev_pm_opp_get_max_volt_latency(dev) +
326 dev_pm_opp_get_max_clock_latency(dev);
327}
328EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
329
4eafbd15
BZ
330/**
331 * dev_pm_opp_get_suspend_opp() - Get suspend opp
332 * @dev: device for which we do this operation
333 *
334 * Return: This function returns pointer to the suspend opp if it is
1b2b90cb 335 * defined and available, otherwise it returns NULL.
4eafbd15
BZ
336 *
337 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
338 * protected pointer. The reason for the same is that the opp pointer which is
339 * returned will remain valid for use with opp_get_{voltage, freq} only while
340 * under the locked area. The pointer returned must be used prior to unlocking
341 * with rcu_read_unlock() to maintain the integrity of the pointer.
342 */
343struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev)
344{
2c2709dc 345 struct opp_table *opp_table;
4eafbd15
BZ
346
347 opp_rcu_lockdep_assert();
348
2c2709dc
VK
349 opp_table = _find_opp_table(dev);
350 if (IS_ERR(opp_table) || !opp_table->suspend_opp ||
351 !opp_table->suspend_opp->available)
1b2b90cb 352 return NULL;
4eafbd15 353
2c2709dc 354 return opp_table->suspend_opp;
4eafbd15
BZ
355}
356EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp);
357
e1f60b29 358/**
2c2709dc 359 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
e1f60b29
NM
360 * @dev: device for which we do this operation
361 *
984f16c8 362 * Return: This function returns the number of available opps if there are any,
e1f60b29
NM
363 * else returns 0 if none or the corresponding error value.
364 *
b4718c02 365 * Locking: This function takes rcu_read_lock().
e1f60b29 366 */
5d4879cd 367int dev_pm_opp_get_opp_count(struct device *dev)
e1f60b29 368{
2c2709dc 369 struct opp_table *opp_table;
47d43ba7 370 struct dev_pm_opp *temp_opp;
e1f60b29
NM
371 int count = 0;
372
b4718c02 373 rcu_read_lock();
b02ded24 374
2c2709dc
VK
375 opp_table = _find_opp_table(dev);
376 if (IS_ERR(opp_table)) {
377 count = PTR_ERR(opp_table);
378 dev_err(dev, "%s: OPP table not found (%d)\n",
b4718c02
DT
379 __func__, count);
380 goto out_unlock;
e1f60b29
NM
381 }
382
2c2709dc 383 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
384 if (temp_opp->available)
385 count++;
386 }
387
b4718c02
DT
388out_unlock:
389 rcu_read_unlock();
e1f60b29
NM
390 return count;
391}
5d4879cd 392EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
e1f60b29
NM
393
394/**
5d4879cd 395 * dev_pm_opp_find_freq_exact() - search for an exact frequency
e1f60b29
NM
396 * @dev: device for which we do this operation
397 * @freq: frequency to search for
7ae49618 398 * @available: true/false - match for available opp
e1f60b29 399 *
2c2709dc 400 * Return: Searches for exact match in the opp table and returns pointer to the
984f16c8
NM
401 * matching opp if found, else returns ERR_PTR in case of error and should
402 * be handled using IS_ERR. Error return values can be:
0779726c
NM
403 * EINVAL: for bad pointer
404 * ERANGE: no match found for search
405 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
406 *
407 * Note: available is a modifier for the search. if available=true, then the
408 * match is for exact matching frequency and is available in the stored OPP
409 * table. if false, the match is for exact frequency which is not available.
410 *
411 * This provides a mechanism to enable an opp which is not available currently
412 * or the opposite as well.
413 *
414 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
415 * protected pointer. The reason for the same is that the opp pointer which is
416 * returned will remain valid for use with opp_get_{voltage, freq} only while
417 * under the locked area. The pointer returned must be used prior to unlocking
418 * with rcu_read_unlock() to maintain the integrity of the pointer.
419 */
47d43ba7
NM
420struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
421 unsigned long freq,
422 bool available)
e1f60b29 423{
2c2709dc 424 struct opp_table *opp_table;
47d43ba7 425 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 426
b02ded24
DT
427 opp_rcu_lockdep_assert();
428
2c2709dc
VK
429 opp_table = _find_opp_table(dev);
430 if (IS_ERR(opp_table)) {
431 int r = PTR_ERR(opp_table);
432
433 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
e1f60b29
NM
434 return ERR_PTR(r);
435 }
436
2c2709dc 437 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
438 if (temp_opp->available == available &&
439 temp_opp->rate == freq) {
440 opp = temp_opp;
441 break;
442 }
443 }
444
445 return opp;
446}
5d4879cd 447EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
e1f60b29 448
067b7ce0
JZ
449static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
450 unsigned long *freq)
451{
452 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
453
454 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
455 if (temp_opp->available && temp_opp->rate >= *freq) {
456 opp = temp_opp;
457 *freq = opp->rate;
458 break;
459 }
460 }
461
462 return opp;
463}
464
e1f60b29 465/**
5d4879cd 466 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
e1f60b29
NM
467 * @dev: device for which we do this operation
468 * @freq: Start frequency
469 *
470 * Search for the matching ceil *available* OPP from a starting freq
471 * for a device.
472 *
984f16c8 473 * Return: matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
474 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
475 * values can be:
476 * EINVAL: for bad pointer
477 * ERANGE: no match found for search
478 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
479 *
480 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
481 * protected pointer. The reason for the same is that the opp pointer which is
482 * returned will remain valid for use with opp_get_{voltage, freq} only while
483 * under the locked area. The pointer returned must be used prior to unlocking
484 * with rcu_read_unlock() to maintain the integrity of the pointer.
485 */
47d43ba7
NM
486struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
487 unsigned long *freq)
e1f60b29 488{
2c2709dc 489 struct opp_table *opp_table;
e1f60b29 490
b02ded24
DT
491 opp_rcu_lockdep_assert();
492
e1f60b29
NM
493 if (!dev || !freq) {
494 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
495 return ERR_PTR(-EINVAL);
496 }
497
2c2709dc
VK
498 opp_table = _find_opp_table(dev);
499 if (IS_ERR(opp_table))
500 return ERR_CAST(opp_table);
e1f60b29 501
067b7ce0 502 return _find_freq_ceil(opp_table, freq);
e1f60b29 503}
5d4879cd 504EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
e1f60b29
NM
505
506/**
5d4879cd 507 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
e1f60b29
NM
508 * @dev: device for which we do this operation
509 * @freq: Start frequency
510 *
511 * Search for the matching floor *available* OPP from a starting freq
512 * for a device.
513 *
984f16c8 514 * Return: matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
515 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
516 * values can be:
517 * EINVAL: for bad pointer
518 * ERANGE: no match found for search
519 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
520 *
521 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
522 * protected pointer. The reason for the same is that the opp pointer which is
523 * returned will remain valid for use with opp_get_{voltage, freq} only while
524 * under the locked area. The pointer returned must be used prior to unlocking
525 * with rcu_read_unlock() to maintain the integrity of the pointer.
526 */
47d43ba7
NM
527struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
528 unsigned long *freq)
e1f60b29 529{
2c2709dc 530 struct opp_table *opp_table;
47d43ba7 531 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 532
b02ded24
DT
533 opp_rcu_lockdep_assert();
534
e1f60b29
NM
535 if (!dev || !freq) {
536 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
537 return ERR_PTR(-EINVAL);
538 }
539
2c2709dc
VK
540 opp_table = _find_opp_table(dev);
541 if (IS_ERR(opp_table))
542 return ERR_CAST(opp_table);
e1f60b29 543
2c2709dc 544 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
545 if (temp_opp->available) {
546 /* go to the next node, before choosing prev */
547 if (temp_opp->rate > *freq)
548 break;
549 else
550 opp = temp_opp;
551 }
552 }
553 if (!IS_ERR(opp))
554 *freq = opp->rate;
555
556 return opp;
557}
5d4879cd 558EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
e1f60b29 559
6a0712f6 560/*
2c2709dc 561 * The caller needs to ensure that opp_table (and hence the clk) isn't freed,
6a0712f6
VK
562 * while clk returned here is used.
563 */
564static struct clk *_get_opp_clk(struct device *dev)
565{
2c2709dc 566 struct opp_table *opp_table;
6a0712f6
VK
567 struct clk *clk;
568
569 rcu_read_lock();
570
2c2709dc
VK
571 opp_table = _find_opp_table(dev);
572 if (IS_ERR(opp_table)) {
6a0712f6 573 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
2c2709dc 574 clk = ERR_CAST(opp_table);
6a0712f6
VK
575 goto unlock;
576 }
577
2c2709dc 578 clk = opp_table->clk;
6a0712f6
VK
579 if (IS_ERR(clk))
580 dev_err(dev, "%s: No clock available for the device\n",
581 __func__);
582
583unlock:
584 rcu_read_unlock();
585 return clk;
586}
587
588static int _set_opp_voltage(struct device *dev, struct regulator *reg,
ce31781a 589 struct dev_pm_opp_supply *supply)
6a0712f6
VK
590{
591 int ret;
592
593 /* Regulator not available for device */
594 if (IS_ERR(reg)) {
595 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
596 PTR_ERR(reg));
597 return 0;
598 }
599
ce31781a
VK
600 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
601 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
6a0712f6 602
ce31781a
VK
603 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
604 supply->u_volt, supply->u_volt_max);
6a0712f6
VK
605 if (ret)
606 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
ce31781a
VK
607 __func__, supply->u_volt_min, supply->u_volt,
608 supply->u_volt_max, ret);
6a0712f6
VK
609
610 return ret;
611}
612
94735585
VK
613static inline int
614_generic_set_opp_clk_only(struct device *dev, struct clk *clk,
615 unsigned long old_freq, unsigned long freq)
616{
617 int ret;
618
619 ret = clk_set_rate(clk, freq);
620 if (ret) {
621 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
622 ret);
623 }
624
625 return ret;
626}
627
628static int _generic_set_opp(struct dev_pm_set_opp_data *data)
629{
630 struct dev_pm_opp_supply *old_supply = data->old_opp.supplies;
631 struct dev_pm_opp_supply *new_supply = data->new_opp.supplies;
632 unsigned long old_freq = data->old_opp.rate, freq = data->new_opp.rate;
633 struct regulator *reg = data->regulators[0];
634 struct device *dev= data->dev;
635 int ret;
636
637 /* This function only supports single regulator per device */
638 if (WARN_ON(data->regulator_count > 1)) {
639 dev_err(dev, "multiple regulators are not supported\n");
640 return -EINVAL;
641 }
642
643 /* Scaling up? Scale voltage before frequency */
644 if (freq > old_freq) {
645 ret = _set_opp_voltage(dev, reg, new_supply);
646 if (ret)
647 goto restore_voltage;
648 }
649
650 /* Change frequency */
651 ret = _generic_set_opp_clk_only(dev, data->clk, old_freq, freq);
652 if (ret)
653 goto restore_voltage;
654
655 /* Scaling down? Scale voltage after frequency */
656 if (freq < old_freq) {
657 ret = _set_opp_voltage(dev, reg, new_supply);
658 if (ret)
659 goto restore_freq;
660 }
661
662 return 0;
663
664restore_freq:
665 if (_generic_set_opp_clk_only(dev, data->clk, freq, old_freq))
666 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
667 __func__, old_freq);
668restore_voltage:
669 /* This shouldn't harm even if the voltages weren't updated earlier */
670 if (old_supply->u_volt)
671 _set_opp_voltage(dev, reg, old_supply);
672
673 return ret;
674}
675
6a0712f6
VK
676/**
677 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
678 * @dev: device for which we do this operation
679 * @target_freq: frequency to achieve
680 *
681 * This configures the power-supplies and clock source to the levels specified
682 * by the OPP corresponding to the target_freq.
683 *
684 * Locking: This function takes rcu_read_lock().
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;
94735585
VK
691 struct regulator **regulators;
692 struct dev_pm_set_opp_data *data;
6a0712f6 693 struct clk *clk;
94735585 694 int ret, size;
6a0712f6
VK
695
696 if (unlikely(!target_freq)) {
697 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
698 target_freq);
699 return -EINVAL;
700 }
701
702 clk = _get_opp_clk(dev);
703 if (IS_ERR(clk))
704 return PTR_ERR(clk);
705
706 freq = clk_round_rate(clk, target_freq);
707 if ((long)freq <= 0)
708 freq = target_freq;
709
710 old_freq = clk_get_rate(clk);
711
712 /* Return early if nothing to do */
713 if (old_freq == freq) {
714 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
715 __func__, freq);
716 return 0;
717 }
718
719 rcu_read_lock();
720
2c2709dc
VK
721 opp_table = _find_opp_table(dev);
722 if (IS_ERR(opp_table)) {
6a0712f6
VK
723 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
724 rcu_read_unlock();
2c2709dc 725 return PTR_ERR(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);
739 rcu_read_unlock();
740 return ret;
741 }
742
94735585
VK
743 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
744 old_freq, freq);
dfbe4678 745
94735585
VK
746 regulators = opp_table->regulators;
747
748 /* Only frequency scaling */
749 if (!regulators) {
750 rcu_read_unlock();
751 return _generic_set_opp_clk_only(dev, clk, old_freq, freq);
dfbe4678
VK
752 }
753
94735585
VK
754 data = opp_table->set_opp_data;
755 data->regulators = regulators;
756 data->regulator_count = opp_table->regulator_count;
757 data->clk = clk;
758 data->dev = dev;
759
760 data->old_opp.rate = old_freq;
761 size = sizeof(*opp->supplies) * opp_table->regulator_count;
ce31781a 762 if (IS_ERR(old_opp))
94735585 763 memset(data->old_opp.supplies, 0, size);
ce31781a 764 else
94735585 765 memcpy(data->old_opp.supplies, old_opp->supplies, size);
6a0712f6 766
94735585
VK
767 data->new_opp.rate = freq;
768 memcpy(data->new_opp.supplies, opp->supplies, size);
6a0712f6
VK
769
770 rcu_read_unlock();
771
94735585 772 return _generic_set_opp(data);
6a0712f6
VK
773}
774EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
775
2c2709dc
VK
776/* OPP-dev Helpers */
777static void _kfree_opp_dev_rcu(struct rcu_head *head)
06441658 778{
2c2709dc 779 struct opp_device *opp_dev;
06441658 780
2c2709dc
VK
781 opp_dev = container_of(head, struct opp_device, rcu_head);
782 kfree_rcu(opp_dev, rcu_head);
06441658
VK
783}
784
2c2709dc
VK
785static void _remove_opp_dev(struct opp_device *opp_dev,
786 struct opp_table *opp_table)
06441658 787{
2c2709dc
VK
788 opp_debug_unregister(opp_dev, opp_table);
789 list_del(&opp_dev->node);
790 call_srcu(&opp_table->srcu_head.srcu, &opp_dev->rcu_head,
791 _kfree_opp_dev_rcu);
06441658
VK
792}
793
2c2709dc
VK
794struct opp_device *_add_opp_dev(const struct device *dev,
795 struct opp_table *opp_table)
06441658 796{
2c2709dc 797 struct opp_device *opp_dev;
deaa5146 798 int ret;
06441658 799
2c2709dc
VK
800 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
801 if (!opp_dev)
06441658
VK
802 return NULL;
803
2c2709dc
VK
804 /* Initialize opp-dev */
805 opp_dev->dev = dev;
806 list_add_rcu(&opp_dev->node, &opp_table->dev_list);
06441658 807
2c2709dc
VK
808 /* Create debugfs entries for the opp_table */
809 ret = opp_debug_register(opp_dev, opp_table);
deaa5146
VK
810 if (ret)
811 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
812 __func__, ret);
813
2c2709dc 814 return opp_dev;
06441658
VK
815}
816
984f16c8 817/**
2c2709dc 818 * _add_opp_table() - Find OPP table or allocate a new one
984f16c8
NM
819 * @dev: device for which we do this operation
820 *
aa5f2f85
VK
821 * It tries to find an existing table first, if it couldn't find one, it
822 * allocates a new OPP table and returns that.
984f16c8 823 *
2c2709dc 824 * Return: valid opp_table pointer if success, else NULL.
984f16c8 825 */
2c2709dc 826static struct opp_table *_add_opp_table(struct device *dev)
07cce74a 827{
2c2709dc
VK
828 struct opp_table *opp_table;
829 struct opp_device *opp_dev;
d54974c2 830 int ret;
07cce74a 831
2c2709dc
VK
832 /* Check for existing table for 'dev' first */
833 opp_table = _find_opp_table(dev);
834 if (!IS_ERR(opp_table))
835 return opp_table;
07cce74a
VK
836
837 /*
2c2709dc 838 * Allocate a new OPP table. In the infrequent case where a new
07cce74a
VK
839 * device is needed to be added, we pay this penalty.
840 */
2c2709dc
VK
841 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
842 if (!opp_table)
07cce74a
VK
843 return NULL;
844
2c2709dc 845 INIT_LIST_HEAD(&opp_table->dev_list);
06441658 846
2c2709dc
VK
847 opp_dev = _add_opp_dev(dev, opp_table);
848 if (!opp_dev) {
849 kfree(opp_table);
06441658
VK
850 return NULL;
851 }
852
f47b72a1 853 _of_init_opp_table(opp_table, dev);
50f8cfbd 854
d54974c2 855 /* Find clk for the device */
2c2709dc
VK
856 opp_table->clk = clk_get(dev, NULL);
857 if (IS_ERR(opp_table->clk)) {
858 ret = PTR_ERR(opp_table->clk);
d54974c2
VK
859 if (ret != -EPROBE_DEFER)
860 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
861 ret);
862 }
863
2c2709dc
VK
864 srcu_init_notifier_head(&opp_table->srcu_head);
865 INIT_LIST_HEAD(&opp_table->opp_list);
07cce74a 866
2c2709dc
VK
867 /* Secure the device table modification */
868 list_add_rcu(&opp_table->node, &opp_tables);
869 return opp_table;
07cce74a
VK
870}
871
984f16c8 872/**
2c2709dc 873 * _kfree_device_rcu() - Free opp_table RCU handler
737002b5 874 * @head: RCU head
984f16c8 875 */
737002b5 876static void _kfree_device_rcu(struct rcu_head *head)
e1f60b29 877{
2c2709dc
VK
878 struct opp_table *opp_table = container_of(head, struct opp_table,
879 rcu_head);
6ce4184d 880
2c2709dc 881 kfree_rcu(opp_table, rcu_head);
e1f60b29 882}
38393409
VK
883
884/**
2c2709dc
VK
885 * _remove_opp_table() - Removes a OPP table
886 * @opp_table: OPP table to be removed.
38393409 887 *
2c2709dc 888 * Removes/frees OPP table if it doesn't contain any OPPs.
38393409 889 */
2c2709dc 890static void _remove_opp_table(struct opp_table *opp_table)
38393409 891{
2c2709dc 892 struct opp_device *opp_dev;
06441658 893
2c2709dc 894 if (!list_empty(&opp_table->opp_list))
3bac42ca
VK
895 return;
896
2c2709dc 897 if (opp_table->supported_hw)
7de36b0a
VK
898 return;
899
2c2709dc 900 if (opp_table->prop_name)
01fb4d3c
VK
901 return;
902
dfbe4678 903 if (opp_table->regulators)
9f8ea969
VK
904 return;
905
d54974c2 906 /* Release clk */
2c2709dc
VK
907 if (!IS_ERR(opp_table->clk))
908 clk_put(opp_table->clk);
d54974c2 909
2c2709dc
VK
910 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
911 node);
06441658 912
2c2709dc 913 _remove_opp_dev(opp_dev, opp_table);
06441658
VK
914
915 /* dev_list must be empty now */
2c2709dc 916 WARN_ON(!list_empty(&opp_table->dev_list));
06441658 917
2c2709dc
VK
918 list_del_rcu(&opp_table->node);
919 call_srcu(&opp_table->srcu_head.srcu, &opp_table->rcu_head,
3bac42ca 920 _kfree_device_rcu);
38393409 921}
e1f60b29 922
984f16c8
NM
923/**
924 * _kfree_opp_rcu() - Free OPP RCU handler
925 * @head: RCU head
926 */
327854c8 927static void _kfree_opp_rcu(struct rcu_head *head)
129eec55
VK
928{
929 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
930
931 kfree_rcu(opp, rcu_head);
932}
933
984f16c8
NM
934/**
935 * _opp_remove() - Remove an OPP from a table definition
2c2709dc 936 * @opp_table: points back to the opp_table struct this opp belongs to
984f16c8 937 * @opp: pointer to the OPP to remove
23dacf6d 938 * @notify: OPP_EVENT_REMOVE notification should be sent or not
984f16c8 939 *
2c2709dc 940 * This function removes an opp definition from the opp table.
984f16c8 941 *
2c2709dc 942 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
943 * It is assumed that the caller holds required mutex for an RCU updater
944 * strategy.
945 */
f47b72a1
VK
946void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp,
947 bool notify)
129eec55
VK
948{
949 /*
950 * Notify the changes in the availability of the operable
951 * frequency/voltage list.
952 */
23dacf6d 953 if (notify)
2c2709dc
VK
954 srcu_notifier_call_chain(&opp_table->srcu_head,
955 OPP_EVENT_REMOVE, opp);
deaa5146 956 opp_debug_remove_one(opp);
129eec55 957 list_del_rcu(&opp->node);
2c2709dc 958 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
129eec55 959
2c2709dc 960 _remove_opp_table(opp_table);
129eec55
VK
961}
962
963/**
2c2709dc 964 * dev_pm_opp_remove() - Remove an OPP from OPP table
129eec55
VK
965 * @dev: device for which we do this operation
966 * @freq: OPP to remove with matching 'freq'
967 *
2c2709dc 968 * This function removes an opp from the opp table.
984f16c8 969 *
2c2709dc 970 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
971 * Hence this function internally uses RCU updater strategy with mutex locks
972 * to keep the integrity of the internal data structures. Callers should ensure
973 * that this function is *NOT* called under RCU protection or in contexts where
974 * mutex cannot be locked.
129eec55
VK
975 */
976void dev_pm_opp_remove(struct device *dev, unsigned long freq)
977{
978 struct dev_pm_opp *opp;
2c2709dc 979 struct opp_table *opp_table;
129eec55
VK
980 bool found = false;
981
2c2709dc
VK
982 /* Hold our table modification lock here */
983 mutex_lock(&opp_table_lock);
129eec55 984
2c2709dc
VK
985 opp_table = _find_opp_table(dev);
986 if (IS_ERR(opp_table))
129eec55
VK
987 goto unlock;
988
2c2709dc 989 list_for_each_entry(opp, &opp_table->opp_list, node) {
129eec55
VK
990 if (opp->rate == freq) {
991 found = true;
992 break;
993 }
994 }
995
996 if (!found) {
997 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
998 __func__, freq);
999 goto unlock;
1000 }
1001
2c2709dc 1002 _opp_remove(opp_table, opp, true);
129eec55 1003unlock:
2c2709dc 1004 mutex_unlock(&opp_table_lock);
129eec55
VK
1005}
1006EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1007
f47b72a1
VK
1008struct dev_pm_opp *_allocate_opp(struct device *dev,
1009 struct opp_table **opp_table)
e1f60b29 1010{
23dacf6d 1011 struct dev_pm_opp *opp;
dfbe4678
VK
1012 int count, supply_size;
1013 struct opp_table *table;
e1f60b29 1014
dfbe4678
VK
1015 table = _add_opp_table(dev);
1016 if (!table)
23dacf6d 1017 return NULL;
e1f60b29 1018
dfbe4678
VK
1019 /* Allocate space for at least one supply */
1020 count = table->regulator_count ? table->regulator_count : 1;
1021 supply_size = sizeof(*opp->supplies) * count;
e1f60b29 1022
dfbe4678
VK
1023 /* allocate new OPP node and supplies structures */
1024 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
1025 if (!opp) {
1026 kfree(table);
23dacf6d
VK
1027 return NULL;
1028 }
1029
dfbe4678
VK
1030 /* Put the supplies at the end of the OPP structure as an empty array */
1031 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1032 INIT_LIST_HEAD(&opp->node);
1033
1034 *opp_table = table;
1035
23dacf6d
VK
1036 return opp;
1037}
1038
7d34d56e 1039static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
2c2709dc 1040 struct opp_table *opp_table)
7d34d56e 1041{
dfbe4678
VK
1042 struct regulator *reg;
1043 int i;
1044
1045 for (i = 0; i < opp_table->regulator_count; i++) {
1046 reg = opp_table->regulators[i];
1047
1048 if (!regulator_is_supported_voltage(reg,
1049 opp->supplies[i].u_volt_min,
1050 opp->supplies[i].u_volt_max)) {
1051 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1052 __func__, opp->supplies[i].u_volt_min,
1053 opp->supplies[i].u_volt_max);
1054 return false;
1055 }
7d34d56e
VK
1056 }
1057
1058 return true;
1059}
1060
f47b72a1
VK
1061int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1062 struct opp_table *opp_table)
23dacf6d
VK
1063{
1064 struct dev_pm_opp *opp;
2c2709dc 1065 struct list_head *head = &opp_table->opp_list;
deaa5146 1066 int ret;
23dacf6d
VK
1067
1068 /*
1069 * Insert new OPP in order of increasing frequency and discard if
1070 * already present.
1071 *
2c2709dc 1072 * Need to use &opp_table->opp_list in the condition part of the 'for'
23dacf6d
VK
1073 * loop, don't replace it with head otherwise it will become an infinite
1074 * loop.
1075 */
2c2709dc 1076 list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
23dacf6d
VK
1077 if (new_opp->rate > opp->rate) {
1078 head = &opp->node;
1079 continue;
1080 }
1081
1082 if (new_opp->rate < opp->rate)
1083 break;
1084
1085 /* Duplicate OPPs */
06441658 1086 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
dfbe4678
VK
1087 __func__, opp->rate, opp->supplies[0].u_volt,
1088 opp->available, new_opp->rate,
1089 new_opp->supplies[0].u_volt, new_opp->available);
23dacf6d 1090
dfbe4678 1091 /* Should we compare voltages for all regulators here ? */
0f0fe7e0 1092 return opp->available &&
dfbe4678 1093 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? 0 : -EEXIST;
23dacf6d
VK
1094 }
1095
2c2709dc 1096 new_opp->opp_table = opp_table;
23dacf6d
VK
1097 list_add_rcu(&new_opp->node, head);
1098
2c2709dc 1099 ret = opp_debug_create_one(new_opp, opp_table);
deaa5146
VK
1100 if (ret)
1101 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1102 __func__, ret);
1103
2c2709dc 1104 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
7d34d56e
VK
1105 new_opp->available = false;
1106 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1107 __func__, new_opp->rate);
1108 }
1109
23dacf6d
VK
1110 return 0;
1111}
1112
984f16c8 1113/**
b64b9c3f 1114 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
984f16c8
NM
1115 * @dev: device for which we do this operation
1116 * @freq: Frequency in Hz for this OPP
1117 * @u_volt: Voltage in uVolts for this OPP
1118 * @dynamic: Dynamically added OPPs.
1119 *
2c2709dc 1120 * This function adds an opp definition to the opp table and returns status.
984f16c8
NM
1121 * The opp is made available by default and it can be controlled using
1122 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1123 *
8f8d37b2
VK
1124 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1125 * and freed by dev_pm_opp_of_remove_table.
984f16c8 1126 *
2c2709dc 1127 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
1128 * Hence this function internally uses RCU updater strategy with mutex locks
1129 * to keep the integrity of the internal data structures. Callers should ensure
1130 * that this function is *NOT* called under RCU protection or in contexts where
1131 * mutex cannot be locked.
1132 *
1133 * Return:
1134 * 0 On success OR
1135 * Duplicate OPPs (both freq and volt are same) and opp->available
1136 * -EEXIST Freq are same and volt are different OR
1137 * Duplicate OPPs (both freq and volt are same) and !opp->available
1138 * -ENOMEM Memory allocation failure
1139 */
f47b72a1
VK
1140int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
1141 bool dynamic)
e1f60b29 1142{
2c2709dc 1143 struct opp_table *opp_table;
23dacf6d 1144 struct dev_pm_opp *new_opp;
50f8cfbd 1145 unsigned long tol;
6ce4184d 1146 int ret;
e1f60b29 1147
2c2709dc
VK
1148 /* Hold our table modification lock here */
1149 mutex_lock(&opp_table_lock);
e1f60b29 1150
2c2709dc 1151 new_opp = _allocate_opp(dev, &opp_table);
23dacf6d
VK
1152 if (!new_opp) {
1153 ret = -ENOMEM;
1154 goto unlock;
1155 }
1156
a7470db6 1157 /* populate the opp table */
a7470db6 1158 new_opp->rate = freq;
2c2709dc 1159 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
dfbe4678
VK
1160 new_opp->supplies[0].u_volt = u_volt;
1161 new_opp->supplies[0].u_volt_min = u_volt - tol;
1162 new_opp->supplies[0].u_volt_max = u_volt + tol;
a7470db6 1163 new_opp->available = true;
23dacf6d 1164 new_opp->dynamic = dynamic;
a7470db6 1165
2c2709dc 1166 ret = _opp_add(dev, new_opp, opp_table);
23dacf6d 1167 if (ret)
6ce4184d 1168 goto free_opp;
64ce8545 1169
2c2709dc 1170 mutex_unlock(&opp_table_lock);
e1f60b29 1171
03ca370f
MH
1172 /*
1173 * Notify the changes in the availability of the operable
1174 * frequency/voltage list.
1175 */
2c2709dc 1176 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);
e1f60b29 1177 return 0;
6ce4184d
VK
1178
1179free_opp:
2c2709dc 1180 _opp_remove(opp_table, new_opp, false);
23dacf6d 1181unlock:
2c2709dc 1182 mutex_unlock(&opp_table_lock);
6ce4184d 1183 return ret;
e1f60b29 1184}
38393409 1185
7de36b0a
VK
1186/**
1187 * dev_pm_opp_set_supported_hw() - Set supported platforms
1188 * @dev: Device for which supported-hw has to be set.
1189 * @versions: Array of hierarchy of versions to match.
1190 * @count: Number of elements in the array.
1191 *
1192 * This is required only for the V2 bindings, and it enables a platform to
1193 * specify the hierarchy of versions it supports. OPP layer will then enable
1194 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1195 * property.
1196 *
2c2709dc 1197 * Locking: The internal opp_table and opp structures are RCU protected.
7de36b0a
VK
1198 * Hence this function internally uses RCU updater strategy with mutex locks
1199 * to keep the integrity of the internal data structures. Callers should ensure
1200 * that this function is *NOT* called under RCU protection or in contexts where
1201 * mutex cannot be locked.
1202 */
1203int dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
1204 unsigned int count)
1205{
2c2709dc 1206 struct opp_table *opp_table;
7de36b0a
VK
1207 int ret = 0;
1208
2c2709dc
VK
1209 /* Hold our table modification lock here */
1210 mutex_lock(&opp_table_lock);
7de36b0a 1211
2c2709dc
VK
1212 opp_table = _add_opp_table(dev);
1213 if (!opp_table) {
7de36b0a
VK
1214 ret = -ENOMEM;
1215 goto unlock;
1216 }
1217
2c2709dc
VK
1218 /* Make sure there are no concurrent readers while updating opp_table */
1219 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1220
2c2709dc
VK
1221 /* Do we already have a version hierarchy associated with opp_table? */
1222 if (opp_table->supported_hw) {
7de36b0a
VK
1223 dev_err(dev, "%s: Already have supported hardware list\n",
1224 __func__);
1225 ret = -EBUSY;
1226 goto err;
1227 }
1228
2c2709dc 1229 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
7de36b0a 1230 GFP_KERNEL);
2c2709dc 1231 if (!opp_table->supported_hw) {
7de36b0a
VK
1232 ret = -ENOMEM;
1233 goto err;
1234 }
1235
2c2709dc
VK
1236 opp_table->supported_hw_count = count;
1237 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1238 return 0;
1239
1240err:
2c2709dc 1241 _remove_opp_table(opp_table);
7de36b0a 1242unlock:
2c2709dc 1243 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1244
1245 return ret;
1246}
1247EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1248
1249/**
1250 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
a5da6447 1251 * @dev: Device for which supported-hw has to be put.
7de36b0a
VK
1252 *
1253 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1254 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
7de36b0a
VK
1255 * will not be freed.
1256 *
2c2709dc 1257 * Locking: The internal opp_table and opp structures are RCU protected.
7de36b0a
VK
1258 * Hence this function internally uses RCU updater strategy with mutex locks
1259 * to keep the integrity of the internal data structures. Callers should ensure
1260 * that this function is *NOT* called under RCU protection or in contexts where
1261 * mutex cannot be locked.
1262 */
1263void dev_pm_opp_put_supported_hw(struct device *dev)
1264{
2c2709dc 1265 struct opp_table *opp_table;
7de36b0a 1266
2c2709dc
VK
1267 /* Hold our table modification lock here */
1268 mutex_lock(&opp_table_lock);
7de36b0a 1269
2c2709dc
VK
1270 /* Check for existing table for 'dev' first */
1271 opp_table = _find_opp_table(dev);
1272 if (IS_ERR(opp_table)) {
1273 dev_err(dev, "Failed to find opp_table: %ld\n",
1274 PTR_ERR(opp_table));
7de36b0a
VK
1275 goto unlock;
1276 }
1277
2c2709dc
VK
1278 /* Make sure there are no concurrent readers while updating opp_table */
1279 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1280
2c2709dc 1281 if (!opp_table->supported_hw) {
7de36b0a
VK
1282 dev_err(dev, "%s: Doesn't have supported hardware list\n",
1283 __func__);
1284 goto unlock;
1285 }
1286
2c2709dc
VK
1287 kfree(opp_table->supported_hw);
1288 opp_table->supported_hw = NULL;
1289 opp_table->supported_hw_count = 0;
7de36b0a 1290
2c2709dc
VK
1291 /* Try freeing opp_table if this was the last blocking resource */
1292 _remove_opp_table(opp_table);
7de36b0a
VK
1293
1294unlock:
2c2709dc 1295 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1296}
1297EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1298
01fb4d3c
VK
1299/**
1300 * dev_pm_opp_set_prop_name() - Set prop-extn name
a5da6447 1301 * @dev: Device for which the prop-name has to be set.
01fb4d3c
VK
1302 * @name: name to postfix to properties.
1303 *
1304 * This is required only for the V2 bindings, and it enables a platform to
1305 * specify the extn to be used for certain property names. The properties to
1306 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1307 * should postfix the property name with -<name> while looking for them.
1308 *
2c2709dc 1309 * Locking: The internal opp_table and opp structures are RCU protected.
01fb4d3c
VK
1310 * Hence this function internally uses RCU updater strategy with mutex locks
1311 * to keep the integrity of the internal data structures. Callers should ensure
1312 * that this function is *NOT* called under RCU protection or in contexts where
1313 * mutex cannot be locked.
1314 */
1315int dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1316{
2c2709dc 1317 struct opp_table *opp_table;
01fb4d3c
VK
1318 int ret = 0;
1319
2c2709dc
VK
1320 /* Hold our table modification lock here */
1321 mutex_lock(&opp_table_lock);
01fb4d3c 1322
2c2709dc
VK
1323 opp_table = _add_opp_table(dev);
1324 if (!opp_table) {
01fb4d3c
VK
1325 ret = -ENOMEM;
1326 goto unlock;
1327 }
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
2c2709dc
VK
1332 /* Do we already have a prop-name associated with opp_table? */
1333 if (opp_table->prop_name) {
01fb4d3c 1334 dev_err(dev, "%s: Already have prop-name %s\n", __func__,
2c2709dc 1335 opp_table->prop_name);
01fb4d3c
VK
1336 ret = -EBUSY;
1337 goto err;
1338 }
1339
2c2709dc
VK
1340 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1341 if (!opp_table->prop_name) {
01fb4d3c
VK
1342 ret = -ENOMEM;
1343 goto err;
1344 }
1345
2c2709dc 1346 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1347 return 0;
1348
1349err:
2c2709dc 1350 _remove_opp_table(opp_table);
01fb4d3c 1351unlock:
2c2709dc 1352 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1353
1354 return ret;
1355}
1356EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1357
1358/**
1359 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
a5da6447 1360 * @dev: Device for which the prop-name has to be put.
01fb4d3c
VK
1361 *
1362 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1363 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
01fb4d3c
VK
1364 * will not be freed.
1365 *
2c2709dc 1366 * Locking: The internal opp_table and opp structures are RCU protected.
01fb4d3c
VK
1367 * Hence this function internally uses RCU updater strategy with mutex locks
1368 * to keep the integrity of the internal data structures. Callers should ensure
1369 * that this function is *NOT* called under RCU protection or in contexts where
1370 * mutex cannot be locked.
1371 */
1372void dev_pm_opp_put_prop_name(struct device *dev)
1373{
2c2709dc 1374 struct opp_table *opp_table;
01fb4d3c 1375
2c2709dc
VK
1376 /* Hold our table modification lock here */
1377 mutex_lock(&opp_table_lock);
01fb4d3c 1378
2c2709dc
VK
1379 /* Check for existing table for 'dev' first */
1380 opp_table = _find_opp_table(dev);
1381 if (IS_ERR(opp_table)) {
1382 dev_err(dev, "Failed to find opp_table: %ld\n",
1383 PTR_ERR(opp_table));
01fb4d3c
VK
1384 goto unlock;
1385 }
1386
2c2709dc
VK
1387 /* Make sure there are no concurrent readers while updating opp_table */
1388 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1389
2c2709dc 1390 if (!opp_table->prop_name) {
01fb4d3c
VK
1391 dev_err(dev, "%s: Doesn't have a prop-name\n", __func__);
1392 goto unlock;
1393 }
1394
2c2709dc
VK
1395 kfree(opp_table->prop_name);
1396 opp_table->prop_name = NULL;
01fb4d3c 1397
2c2709dc
VK
1398 /* Try freeing opp_table if this was the last blocking resource */
1399 _remove_opp_table(opp_table);
01fb4d3c
VK
1400
1401unlock:
2c2709dc 1402 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1403}
1404EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1405
94735585
VK
1406static int _allocate_set_opp_data(struct opp_table *opp_table)
1407{
1408 struct dev_pm_set_opp_data *data;
1409 int len, count = opp_table->regulator_count;
1410
1411 if (WARN_ON(!count))
1412 return -EINVAL;
1413
1414 /* space for set_opp_data */
1415 len = sizeof(*data);
1416
1417 /* space for old_opp.supplies and new_opp.supplies */
1418 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1419
1420 data = kzalloc(len, GFP_KERNEL);
1421 if (!data)
1422 return -ENOMEM;
1423
1424 data->old_opp.supplies = (void *)(data + 1);
1425 data->new_opp.supplies = data->old_opp.supplies + count;
1426
1427 opp_table->set_opp_data = data;
1428
1429 return 0;
1430}
1431
1432static void _free_set_opp_data(struct opp_table *opp_table)
1433{
1434 kfree(opp_table->set_opp_data);
1435 opp_table->set_opp_data = NULL;
1436}
1437
9f8ea969 1438/**
dfbe4678 1439 * dev_pm_opp_set_regulators() - Set regulator names for the device
9f8ea969 1440 * @dev: Device for which regulator name is being set.
dfbe4678
VK
1441 * @names: Array of pointers to the names of the regulator.
1442 * @count: Number of regulators.
9f8ea969
VK
1443 *
1444 * In order to support OPP switching, OPP layer needs to know the name of the
dfbe4678
VK
1445 * device's regulators, as the core would be required to switch voltages as
1446 * well.
9f8ea969
VK
1447 *
1448 * This must be called before any OPPs are initialized for the device.
1449 *
2c2709dc 1450 * Locking: The internal opp_table and opp structures are RCU protected.
9f8ea969
VK
1451 * Hence this function internally uses RCU updater strategy with mutex locks
1452 * to keep the integrity of the internal data structures. Callers should ensure
1453 * that this function is *NOT* called under RCU protection or in contexts where
1454 * mutex cannot be locked.
1455 */
dfbe4678
VK
1456struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1457 const char * const names[],
1458 unsigned int count)
9f8ea969 1459{
2c2709dc 1460 struct opp_table *opp_table;
9f8ea969 1461 struct regulator *reg;
dfbe4678 1462 int ret, i;
9f8ea969 1463
2c2709dc 1464 mutex_lock(&opp_table_lock);
9f8ea969 1465
2c2709dc
VK
1466 opp_table = _add_opp_table(dev);
1467 if (!opp_table) {
9f8ea969
VK
1468 ret = -ENOMEM;
1469 goto unlock;
1470 }
1471
1472 /* This should be called before OPPs are initialized */
2c2709dc 1473 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
9f8ea969
VK
1474 ret = -EBUSY;
1475 goto err;
1476 }
1477
dfbe4678
VK
1478 /* Already have regulators set */
1479 if (WARN_ON(opp_table->regulators)) {
9f8ea969
VK
1480 ret = -EBUSY;
1481 goto err;
1482 }
dfbe4678
VK
1483
1484 opp_table->regulators = kmalloc_array(count,
1485 sizeof(*opp_table->regulators),
1486 GFP_KERNEL);
1487 if (!opp_table->regulators) {
1488 ret = -ENOMEM;
9f8ea969
VK
1489 goto err;
1490 }
1491
dfbe4678
VK
1492 for (i = 0; i < count; i++) {
1493 reg = regulator_get_optional(dev, names[i]);
1494 if (IS_ERR(reg)) {
1495 ret = PTR_ERR(reg);
1496 if (ret != -EPROBE_DEFER)
1497 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1498 __func__, names[i], ret);
1499 goto free_regulators;
1500 }
1501
1502 opp_table->regulators[i] = reg;
1503 }
1504
1505 opp_table->regulator_count = count;
9f8ea969 1506
94735585
VK
1507 /* Allocate block only once to pass to set_opp() routines */
1508 ret = _allocate_set_opp_data(opp_table);
1509 if (ret)
1510 goto free_regulators;
1511
2c2709dc 1512 mutex_unlock(&opp_table_lock);
91291d9a 1513 return opp_table;
9f8ea969 1514
dfbe4678
VK
1515free_regulators:
1516 while (i != 0)
1517 regulator_put(opp_table->regulators[--i]);
1518
1519 kfree(opp_table->regulators);
1520 opp_table->regulators = NULL;
94735585 1521 opp_table->regulator_count = 0;
9f8ea969 1522err:
2c2709dc 1523 _remove_opp_table(opp_table);
9f8ea969 1524unlock:
2c2709dc 1525 mutex_unlock(&opp_table_lock);
9f8ea969 1526
91291d9a 1527 return ERR_PTR(ret);
9f8ea969 1528}
dfbe4678 1529EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
9f8ea969
VK
1530
1531/**
dfbe4678
VK
1532 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1533 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
9f8ea969 1534 *
2c2709dc 1535 * Locking: The internal opp_table and opp structures are RCU protected.
9f8ea969
VK
1536 * Hence this function internally uses RCU updater strategy with mutex locks
1537 * to keep the integrity of the internal data structures. Callers should ensure
1538 * that this function is *NOT* called under RCU protection or in contexts where
1539 * mutex cannot be locked.
1540 */
dfbe4678 1541void dev_pm_opp_put_regulators(struct opp_table *opp_table)
9f8ea969 1542{
dfbe4678
VK
1543 int i;
1544
2c2709dc 1545 mutex_lock(&opp_table_lock);
9f8ea969 1546
dfbe4678
VK
1547 if (!opp_table->regulators) {
1548 pr_err("%s: Doesn't have regulators set\n", __func__);
9f8ea969
VK
1549 goto unlock;
1550 }
1551
2c2709dc
VK
1552 /* Make sure there are no concurrent readers while updating opp_table */
1553 WARN_ON(!list_empty(&opp_table->opp_list));
9f8ea969 1554
dfbe4678
VK
1555 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1556 regulator_put(opp_table->regulators[i]);
1557
94735585
VK
1558 _free_set_opp_data(opp_table);
1559
dfbe4678
VK
1560 kfree(opp_table->regulators);
1561 opp_table->regulators = NULL;
1562 opp_table->regulator_count = 0;
9f8ea969 1563
2c2709dc
VK
1564 /* Try freeing opp_table if this was the last blocking resource */
1565 _remove_opp_table(opp_table);
9f8ea969
VK
1566
1567unlock:
2c2709dc 1568 mutex_unlock(&opp_table_lock);
9f8ea969 1569}
dfbe4678 1570EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
9f8ea969 1571
38393409
VK
1572/**
1573 * dev_pm_opp_add() - Add an OPP table from a table definitions
1574 * @dev: device for which we do this operation
1575 * @freq: Frequency in Hz for this OPP
1576 * @u_volt: Voltage in uVolts for this OPP
1577 *
2c2709dc 1578 * This function adds an opp definition to the opp table and returns status.
38393409
VK
1579 * The opp is made available by default and it can be controlled using
1580 * dev_pm_opp_enable/disable functions.
1581 *
2c2709dc 1582 * Locking: The internal opp_table and opp structures are RCU protected.
38393409
VK
1583 * Hence this function internally uses RCU updater strategy with mutex locks
1584 * to keep the integrity of the internal data structures. Callers should ensure
1585 * that this function is *NOT* called under RCU protection or in contexts where
1586 * mutex cannot be locked.
1587 *
1588 * Return:
984f16c8 1589 * 0 On success OR
38393409 1590 * Duplicate OPPs (both freq and volt are same) and opp->available
984f16c8 1591 * -EEXIST Freq are same and volt are different OR
38393409 1592 * Duplicate OPPs (both freq and volt are same) and !opp->available
984f16c8 1593 * -ENOMEM Memory allocation failure
38393409
VK
1594 */
1595int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1596{
b64b9c3f 1597 return _opp_add_v1(dev, freq, u_volt, true);
38393409 1598}
5d4879cd 1599EXPORT_SYMBOL_GPL(dev_pm_opp_add);
e1f60b29
NM
1600
1601/**
327854c8 1602 * _opp_set_availability() - helper to set the availability of an opp
e1f60b29
NM
1603 * @dev: device for which we do this operation
1604 * @freq: OPP frequency to modify availability
1605 * @availability_req: availability status requested for this opp
1606 *
1607 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
1608 * share a common logic which is isolated here.
1609 *
984f16c8 1610 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1611 * copy operation, returns 0 if no modification was done OR modification was
e1f60b29
NM
1612 * successful.
1613 *
2c2709dc 1614 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1615 * Hence this function internally uses RCU updater strategy with mutex locks to
1616 * keep the integrity of the internal data structures. Callers should ensure
1617 * that this function is *NOT* called under RCU protection or in contexts where
1618 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1619 */
327854c8
NM
1620static int _opp_set_availability(struct device *dev, unsigned long freq,
1621 bool availability_req)
e1f60b29 1622{
2c2709dc 1623 struct opp_table *opp_table;
47d43ba7 1624 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
e1f60b29
NM
1625 int r = 0;
1626
1627 /* keep the node allocated */
47d43ba7 1628 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
59d84ca8 1629 if (!new_opp)
e1f60b29 1630 return -ENOMEM;
e1f60b29 1631
2c2709dc 1632 mutex_lock(&opp_table_lock);
e1f60b29 1633
2c2709dc
VK
1634 /* Find the opp_table */
1635 opp_table = _find_opp_table(dev);
1636 if (IS_ERR(opp_table)) {
1637 r = PTR_ERR(opp_table);
e1f60b29
NM
1638 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1639 goto unlock;
1640 }
1641
1642 /* Do we have the frequency? */
2c2709dc 1643 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
1644 if (tmp_opp->rate == freq) {
1645 opp = tmp_opp;
1646 break;
1647 }
1648 }
1649 if (IS_ERR(opp)) {
1650 r = PTR_ERR(opp);
1651 goto unlock;
1652 }
1653
1654 /* Is update really needed? */
1655 if (opp->available == availability_req)
1656 goto unlock;
1657 /* copy the old data over */
1658 *new_opp = *opp;
1659
1660 /* plug in new node */
1661 new_opp->available = availability_req;
1662
1663 list_replace_rcu(&opp->node, &new_opp->node);
2c2709dc
VK
1664 mutex_unlock(&opp_table_lock);
1665 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
e1f60b29 1666
03ca370f
MH
1667 /* Notify the change of the OPP availability */
1668 if (availability_req)
2c2709dc
VK
1669 srcu_notifier_call_chain(&opp_table->srcu_head,
1670 OPP_EVENT_ENABLE, new_opp);
03ca370f 1671 else
2c2709dc
VK
1672 srcu_notifier_call_chain(&opp_table->srcu_head,
1673 OPP_EVENT_DISABLE, new_opp);
03ca370f 1674
dde8437d 1675 return 0;
e1f60b29
NM
1676
1677unlock:
2c2709dc 1678 mutex_unlock(&opp_table_lock);
e1f60b29
NM
1679 kfree(new_opp);
1680 return r;
1681}
1682
1683/**
5d4879cd 1684 * dev_pm_opp_enable() - Enable a specific OPP
e1f60b29
NM
1685 * @dev: device for which we do this operation
1686 * @freq: OPP frequency to enable
1687 *
1688 * Enables a provided opp. If the operation is valid, this returns 0, else the
1689 * corresponding error value. It is meant to be used for users an OPP available
5d4879cd 1690 * after being temporarily made unavailable with dev_pm_opp_disable.
e1f60b29 1691 *
2c2709dc 1692 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1693 * Hence this function indirectly uses RCU and mutex locks to keep the
1694 * integrity of the internal data structures. Callers should ensure that
1695 * this function is *NOT* called under RCU protection or in contexts where
1696 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1697 *
1698 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1699 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 1700 * successful.
e1f60b29 1701 */
5d4879cd 1702int dev_pm_opp_enable(struct device *dev, unsigned long freq)
e1f60b29 1703{
327854c8 1704 return _opp_set_availability(dev, freq, true);
e1f60b29 1705}
5d4879cd 1706EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
e1f60b29
NM
1707
1708/**
5d4879cd 1709 * dev_pm_opp_disable() - Disable a specific OPP
e1f60b29
NM
1710 * @dev: device for which we do this operation
1711 * @freq: OPP frequency to disable
1712 *
1713 * Disables a provided opp. If the operation is valid, this returns
1714 * 0, else the corresponding error value. It is meant to be a temporary
1715 * control by users to make this OPP not available until the circumstances are
5d4879cd 1716 * right to make it available again (with a call to dev_pm_opp_enable).
e1f60b29 1717 *
2c2709dc 1718 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1719 * Hence this function indirectly uses RCU and mutex locks to keep the
1720 * integrity of the internal data structures. Callers should ensure that
1721 * this function is *NOT* called under RCU protection or in contexts where
1722 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1723 *
1724 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1725 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 1726 * successful.
e1f60b29 1727 */
5d4879cd 1728int dev_pm_opp_disable(struct device *dev, unsigned long freq)
e1f60b29 1729{
327854c8 1730 return _opp_set_availability(dev, freq, false);
e1f60b29 1731}
5d4879cd 1732EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
e1f60b29 1733
03ca370f 1734/**
5d4879cd 1735 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
2c2709dc 1736 * @dev: device pointer used to lookup OPP table.
984f16c8
NM
1737 *
1738 * Return: pointer to notifier head if found, otherwise -ENODEV or
1739 * -EINVAL based on type of error casted as pointer. value must be checked
1740 * with IS_ERR to determine valid pointer or error result.
1741 *
2c2709dc
VK
1742 * Locking: This function must be called under rcu_read_lock(). opp_table is a
1743 * RCU protected pointer. The reason for the same is that the opp pointer which
1744 * is returned will remain valid for use with opp_get_{voltage, freq} only while
984f16c8
NM
1745 * under the locked area. The pointer returned must be used prior to unlocking
1746 * with rcu_read_unlock() to maintain the integrity of the pointer.
03ca370f 1747 */
5d4879cd 1748struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
03ca370f 1749{
2c2709dc 1750 struct opp_table *opp_table = _find_opp_table(dev);
03ca370f 1751
2c2709dc
VK
1752 if (IS_ERR(opp_table))
1753 return ERR_CAST(opp_table); /* matching type */
03ca370f 1754
2c2709dc 1755 return &opp_table->srcu_head;
03ca370f 1756}
4679ec37 1757EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
b496dfbc 1758
411466c5
SH
1759/*
1760 * Free OPPs either created using static entries present in DT or even the
1761 * dynamically added entries based on remove_all param.
b496dfbc 1762 */
f47b72a1 1763void _dev_pm_opp_remove_table(struct device *dev, bool remove_all)
737002b5 1764{
2c2709dc 1765 struct opp_table *opp_table;
737002b5
VK
1766 struct dev_pm_opp *opp, *tmp;
1767
2c2709dc
VK
1768 /* Hold our table modification lock here */
1769 mutex_lock(&opp_table_lock);
06441658 1770
2c2709dc
VK
1771 /* Check for existing table for 'dev' */
1772 opp_table = _find_opp_table(dev);
1773 if (IS_ERR(opp_table)) {
1774 int error = PTR_ERR(opp_table);
737002b5
VK
1775
1776 if (error != -ENODEV)
2c2709dc 1777 WARN(1, "%s: opp_table: %d\n",
737002b5
VK
1778 IS_ERR_OR_NULL(dev) ?
1779 "Invalid device" : dev_name(dev),
1780 error);
06441658 1781 goto unlock;
737002b5
VK
1782 }
1783
2c2709dc
VK
1784 /* Find if opp_table manages a single device */
1785 if (list_is_singular(&opp_table->dev_list)) {
06441658 1786 /* Free static OPPs */
2c2709dc 1787 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
411466c5 1788 if (remove_all || !opp->dynamic)
2c2709dc 1789 _opp_remove(opp_table, opp, true);
06441658
VK
1790 }
1791 } else {
2c2709dc 1792 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
737002b5
VK
1793 }
1794
06441658 1795unlock:
2c2709dc 1796 mutex_unlock(&opp_table_lock);
737002b5 1797}
129eec55
VK
1798
1799/**
411466c5 1800 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
2c2709dc 1801 * @dev: device pointer used to lookup OPP table.
129eec55 1802 *
411466c5
SH
1803 * Free both OPPs created using static entries present in DT and the
1804 * dynamically added entries.
984f16c8 1805 *
2c2709dc 1806 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
1807 * Hence this function indirectly uses RCU updater strategy with mutex locks
1808 * to keep the integrity of the internal data structures. Callers should ensure
1809 * that this function is *NOT* called under RCU protection or in contexts where
1810 * mutex cannot be locked.
129eec55 1811 */
411466c5 1812void dev_pm_opp_remove_table(struct device *dev)
129eec55 1813{
411466c5 1814 _dev_pm_opp_remove_table(dev, true);
8d4d4e98 1815}
411466c5 1816EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);