PM / OPP: Don't allocate OPP table from _opp_allocate()
[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;
4dab160e 690 int (*set_opp)(struct dev_pm_set_opp_data *data);
6a0712f6 691 struct dev_pm_opp *old_opp, *opp;
94735585
VK
692 struct regulator **regulators;
693 struct dev_pm_set_opp_data *data;
6a0712f6 694 struct clk *clk;
94735585 695 int ret, size;
6a0712f6
VK
696
697 if (unlikely(!target_freq)) {
698 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
699 target_freq);
700 return -EINVAL;
701 }
702
703 clk = _get_opp_clk(dev);
704 if (IS_ERR(clk))
705 return PTR_ERR(clk);
706
707 freq = clk_round_rate(clk, target_freq);
708 if ((long)freq <= 0)
709 freq = target_freq;
710
711 old_freq = clk_get_rate(clk);
712
713 /* Return early if nothing to do */
714 if (old_freq == freq) {
715 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
716 __func__, freq);
717 return 0;
718 }
719
720 rcu_read_lock();
721
2c2709dc
VK
722 opp_table = _find_opp_table(dev);
723 if (IS_ERR(opp_table)) {
6a0712f6
VK
724 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
725 rcu_read_unlock();
2c2709dc 726 return PTR_ERR(opp_table);
6a0712f6
VK
727 }
728
067b7ce0 729 old_opp = _find_freq_ceil(opp_table, &old_freq);
4df27c91 730 if (IS_ERR(old_opp)) {
6a0712f6
VK
731 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
732 __func__, old_freq, PTR_ERR(old_opp));
733 }
734
067b7ce0 735 opp = _find_freq_ceil(opp_table, &freq);
6a0712f6
VK
736 if (IS_ERR(opp)) {
737 ret = PTR_ERR(opp);
738 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
739 __func__, freq, ret);
740 rcu_read_unlock();
741 return ret;
742 }
743
94735585
VK
744 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
745 old_freq, freq);
dfbe4678 746
94735585
VK
747 regulators = opp_table->regulators;
748
749 /* Only frequency scaling */
750 if (!regulators) {
751 rcu_read_unlock();
752 return _generic_set_opp_clk_only(dev, clk, old_freq, freq);
dfbe4678
VK
753 }
754
4dab160e
VK
755 if (opp_table->set_opp)
756 set_opp = opp_table->set_opp;
757 else
758 set_opp = _generic_set_opp;
759
94735585
VK
760 data = opp_table->set_opp_data;
761 data->regulators = regulators;
762 data->regulator_count = opp_table->regulator_count;
763 data->clk = clk;
764 data->dev = dev;
765
766 data->old_opp.rate = old_freq;
767 size = sizeof(*opp->supplies) * opp_table->regulator_count;
ce31781a 768 if (IS_ERR(old_opp))
94735585 769 memset(data->old_opp.supplies, 0, size);
ce31781a 770 else
94735585 771 memcpy(data->old_opp.supplies, old_opp->supplies, size);
6a0712f6 772
94735585
VK
773 data->new_opp.rate = freq;
774 memcpy(data->new_opp.supplies, opp->supplies, size);
6a0712f6
VK
775
776 rcu_read_unlock();
777
4dab160e 778 return set_opp(data);
6a0712f6
VK
779}
780EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
781
2c2709dc
VK
782/* OPP-dev Helpers */
783static void _kfree_opp_dev_rcu(struct rcu_head *head)
06441658 784{
2c2709dc 785 struct opp_device *opp_dev;
06441658 786
2c2709dc
VK
787 opp_dev = container_of(head, struct opp_device, rcu_head);
788 kfree_rcu(opp_dev, rcu_head);
06441658
VK
789}
790
2c2709dc
VK
791static void _remove_opp_dev(struct opp_device *opp_dev,
792 struct opp_table *opp_table)
06441658 793{
2c2709dc
VK
794 opp_debug_unregister(opp_dev, opp_table);
795 list_del(&opp_dev->node);
796 call_srcu(&opp_table->srcu_head.srcu, &opp_dev->rcu_head,
797 _kfree_opp_dev_rcu);
06441658
VK
798}
799
2c2709dc
VK
800struct opp_device *_add_opp_dev(const struct device *dev,
801 struct opp_table *opp_table)
06441658 802{
2c2709dc 803 struct opp_device *opp_dev;
deaa5146 804 int ret;
06441658 805
2c2709dc
VK
806 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
807 if (!opp_dev)
06441658
VK
808 return NULL;
809
2c2709dc
VK
810 /* Initialize opp-dev */
811 opp_dev->dev = dev;
812 list_add_rcu(&opp_dev->node, &opp_table->dev_list);
06441658 813
2c2709dc
VK
814 /* Create debugfs entries for the opp_table */
815 ret = opp_debug_register(opp_dev, opp_table);
deaa5146
VK
816 if (ret)
817 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
818 __func__, ret);
819
2c2709dc 820 return opp_dev;
06441658
VK
821}
822
984f16c8 823/**
2c2709dc 824 * _add_opp_table() - Find OPP table or allocate a new one
984f16c8
NM
825 * @dev: device for which we do this operation
826 *
aa5f2f85
VK
827 * It tries to find an existing table first, if it couldn't find one, it
828 * allocates a new OPP table and returns that.
984f16c8 829 *
2c2709dc 830 * Return: valid opp_table pointer if success, else NULL.
984f16c8 831 */
8cd2f6e8 832struct opp_table *_add_opp_table(struct device *dev)
07cce74a 833{
2c2709dc
VK
834 struct opp_table *opp_table;
835 struct opp_device *opp_dev;
d54974c2 836 int ret;
07cce74a 837
2c2709dc
VK
838 /* Check for existing table for 'dev' first */
839 opp_table = _find_opp_table(dev);
840 if (!IS_ERR(opp_table))
841 return opp_table;
07cce74a
VK
842
843 /*
2c2709dc 844 * Allocate a new OPP table. In the infrequent case where a new
07cce74a
VK
845 * device is needed to be added, we pay this penalty.
846 */
2c2709dc
VK
847 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
848 if (!opp_table)
07cce74a
VK
849 return NULL;
850
2c2709dc 851 INIT_LIST_HEAD(&opp_table->dev_list);
06441658 852
2c2709dc
VK
853 opp_dev = _add_opp_dev(dev, opp_table);
854 if (!opp_dev) {
855 kfree(opp_table);
06441658
VK
856 return NULL;
857 }
858
f47b72a1 859 _of_init_opp_table(opp_table, dev);
50f8cfbd 860
d54974c2 861 /* Find clk for the device */
2c2709dc
VK
862 opp_table->clk = clk_get(dev, NULL);
863 if (IS_ERR(opp_table->clk)) {
864 ret = PTR_ERR(opp_table->clk);
d54974c2
VK
865 if (ret != -EPROBE_DEFER)
866 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
867 ret);
868 }
869
2c2709dc
VK
870 srcu_init_notifier_head(&opp_table->srcu_head);
871 INIT_LIST_HEAD(&opp_table->opp_list);
07cce74a 872
2c2709dc
VK
873 /* Secure the device table modification */
874 list_add_rcu(&opp_table->node, &opp_tables);
875 return opp_table;
07cce74a
VK
876}
877
984f16c8 878/**
2c2709dc 879 * _kfree_device_rcu() - Free opp_table RCU handler
737002b5 880 * @head: RCU head
984f16c8 881 */
737002b5 882static void _kfree_device_rcu(struct rcu_head *head)
e1f60b29 883{
2c2709dc
VK
884 struct opp_table *opp_table = container_of(head, struct opp_table,
885 rcu_head);
6ce4184d 886
2c2709dc 887 kfree_rcu(opp_table, rcu_head);
e1f60b29 888}
38393409
VK
889
890/**
2c2709dc
VK
891 * _remove_opp_table() - Removes a OPP table
892 * @opp_table: OPP table to be removed.
38393409 893 *
2c2709dc 894 * Removes/frees OPP table if it doesn't contain any OPPs.
38393409 895 */
2c2709dc 896static void _remove_opp_table(struct opp_table *opp_table)
38393409 897{
2c2709dc 898 struct opp_device *opp_dev;
06441658 899
2c2709dc 900 if (!list_empty(&opp_table->opp_list))
3bac42ca
VK
901 return;
902
2c2709dc 903 if (opp_table->supported_hw)
7de36b0a
VK
904 return;
905
2c2709dc 906 if (opp_table->prop_name)
01fb4d3c
VK
907 return;
908
dfbe4678 909 if (opp_table->regulators)
9f8ea969
VK
910 return;
911
4dab160e
VK
912 if (opp_table->set_opp)
913 return;
914
d54974c2 915 /* Release clk */
2c2709dc
VK
916 if (!IS_ERR(opp_table->clk))
917 clk_put(opp_table->clk);
d54974c2 918
2c2709dc
VK
919 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
920 node);
06441658 921
2c2709dc 922 _remove_opp_dev(opp_dev, opp_table);
06441658
VK
923
924 /* dev_list must be empty now */
2c2709dc 925 WARN_ON(!list_empty(&opp_table->dev_list));
06441658 926
2c2709dc
VK
927 list_del_rcu(&opp_table->node);
928 call_srcu(&opp_table->srcu_head.srcu, &opp_table->rcu_head,
3bac42ca 929 _kfree_device_rcu);
38393409 930}
e1f60b29 931
8cd2f6e8 932void _opp_free(struct dev_pm_opp *opp)
969fceb3
VK
933{
934 kfree(opp);
969fceb3
VK
935}
936
984f16c8
NM
937/**
938 * _kfree_opp_rcu() - Free OPP RCU handler
939 * @head: RCU head
940 */
327854c8 941static void _kfree_opp_rcu(struct rcu_head *head)
129eec55
VK
942{
943 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
944
945 kfree_rcu(opp, rcu_head);
946}
947
984f16c8
NM
948/**
949 * _opp_remove() - Remove an OPP from a table definition
2c2709dc 950 * @opp_table: points back to the opp_table struct this opp belongs to
984f16c8
NM
951 * @opp: pointer to the OPP to remove
952 *
2c2709dc 953 * This function removes an opp definition from the opp table.
984f16c8 954 *
2c2709dc 955 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
956 * It is assumed that the caller holds required mutex for an RCU updater
957 * strategy.
958 */
969fceb3 959static void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp)
129eec55
VK
960{
961 /*
962 * Notify the changes in the availability of the operable
963 * frequency/voltage list.
964 */
969fceb3 965 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_REMOVE, opp);
deaa5146 966 opp_debug_remove_one(opp);
129eec55 967 list_del_rcu(&opp->node);
2c2709dc 968 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
129eec55 969
2c2709dc 970 _remove_opp_table(opp_table);
129eec55
VK
971}
972
973/**
2c2709dc 974 * dev_pm_opp_remove() - Remove an OPP from OPP table
129eec55
VK
975 * @dev: device for which we do this operation
976 * @freq: OPP to remove with matching 'freq'
977 *
2c2709dc 978 * This function removes an opp from the opp table.
984f16c8 979 *
2c2709dc 980 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
981 * Hence this function internally uses RCU updater strategy with mutex locks
982 * to keep the integrity of the internal data structures. Callers should ensure
983 * that this function is *NOT* called under RCU protection or in contexts where
984 * mutex cannot be locked.
129eec55
VK
985 */
986void dev_pm_opp_remove(struct device *dev, unsigned long freq)
987{
988 struct dev_pm_opp *opp;
2c2709dc 989 struct opp_table *opp_table;
129eec55
VK
990 bool found = false;
991
2c2709dc
VK
992 /* Hold our table modification lock here */
993 mutex_lock(&opp_table_lock);
129eec55 994
2c2709dc
VK
995 opp_table = _find_opp_table(dev);
996 if (IS_ERR(opp_table))
129eec55
VK
997 goto unlock;
998
2c2709dc 999 list_for_each_entry(opp, &opp_table->opp_list, node) {
129eec55
VK
1000 if (opp->rate == freq) {
1001 found = true;
1002 break;
1003 }
1004 }
1005
1006 if (!found) {
1007 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1008 __func__, freq);
1009 goto unlock;
1010 }
1011
969fceb3 1012 _opp_remove(opp_table, opp);
129eec55 1013unlock:
2c2709dc 1014 mutex_unlock(&opp_table_lock);
129eec55
VK
1015}
1016EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1017
8cd2f6e8 1018struct dev_pm_opp *_opp_allocate(struct opp_table *table)
e1f60b29 1019{
23dacf6d 1020 struct dev_pm_opp *opp;
dfbe4678 1021 int count, supply_size;
e1f60b29 1022
dfbe4678
VK
1023 /* Allocate space for at least one supply */
1024 count = table->regulator_count ? table->regulator_count : 1;
1025 supply_size = sizeof(*opp->supplies) * count;
e1f60b29 1026
dfbe4678
VK
1027 /* allocate new OPP node and supplies structures */
1028 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
8cd2f6e8 1029 if (!opp)
23dacf6d 1030 return NULL;
23dacf6d 1031
dfbe4678
VK
1032 /* Put the supplies at the end of the OPP structure as an empty array */
1033 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1034 INIT_LIST_HEAD(&opp->node);
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
7f8538eb
VK
1061/*
1062 * Returns:
1063 * 0: On success. And appropriate error message for duplicate OPPs.
1064 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1065 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1066 * sure we don't print error messages unnecessarily if different parts of
1067 * kernel try to initialize the OPP table.
1068 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1069 * should be considered an error by the callers of _opp_add().
1070 */
f47b72a1
VK
1071int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1072 struct opp_table *opp_table)
23dacf6d
VK
1073{
1074 struct dev_pm_opp *opp;
2c2709dc 1075 struct list_head *head = &opp_table->opp_list;
deaa5146 1076 int ret;
23dacf6d
VK
1077
1078 /*
1079 * Insert new OPP in order of increasing frequency and discard if
1080 * already present.
1081 *
2c2709dc 1082 * Need to use &opp_table->opp_list in the condition part of the 'for'
23dacf6d
VK
1083 * loop, don't replace it with head otherwise it will become an infinite
1084 * loop.
1085 */
2c2709dc 1086 list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
23dacf6d
VK
1087 if (new_opp->rate > opp->rate) {
1088 head = &opp->node;
1089 continue;
1090 }
1091
1092 if (new_opp->rate < opp->rate)
1093 break;
1094
1095 /* Duplicate OPPs */
06441658 1096 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
dfbe4678
VK
1097 __func__, opp->rate, opp->supplies[0].u_volt,
1098 opp->available, new_opp->rate,
1099 new_opp->supplies[0].u_volt, new_opp->available);
23dacf6d 1100
dfbe4678 1101 /* Should we compare voltages for all regulators here ? */
0f0fe7e0 1102 return opp->available &&
7f8538eb 1103 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
23dacf6d
VK
1104 }
1105
2c2709dc 1106 new_opp->opp_table = opp_table;
23dacf6d
VK
1107 list_add_rcu(&new_opp->node, head);
1108
2c2709dc 1109 ret = opp_debug_create_one(new_opp, opp_table);
deaa5146
VK
1110 if (ret)
1111 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1112 __func__, ret);
1113
2c2709dc 1114 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
7d34d56e
VK
1115 new_opp->available = false;
1116 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1117 __func__, new_opp->rate);
1118 }
1119
23dacf6d
VK
1120 return 0;
1121}
1122
984f16c8 1123/**
b64b9c3f 1124 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
8cd2f6e8 1125 * @opp_table: OPP table
984f16c8
NM
1126 * @dev: device for which we do this operation
1127 * @freq: Frequency in Hz for this OPP
1128 * @u_volt: Voltage in uVolts for this OPP
1129 * @dynamic: Dynamically added OPPs.
1130 *
2c2709dc 1131 * This function adds an opp definition to the opp table and returns status.
984f16c8
NM
1132 * The opp is made available by default and it can be controlled using
1133 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1134 *
8f8d37b2
VK
1135 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1136 * and freed by dev_pm_opp_of_remove_table.
984f16c8 1137 *
2c2709dc 1138 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
1139 * Hence this function internally uses RCU updater strategy with mutex locks
1140 * to keep the integrity of the internal data structures. Callers should ensure
1141 * that this function is *NOT* called under RCU protection or in contexts where
1142 * mutex cannot be locked.
1143 *
1144 * Return:
1145 * 0 On success OR
1146 * Duplicate OPPs (both freq and volt are same) and opp->available
1147 * -EEXIST Freq are same and volt are different OR
1148 * Duplicate OPPs (both freq and volt are same) and !opp->available
1149 * -ENOMEM Memory allocation failure
1150 */
8cd2f6e8
VK
1151int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1152 unsigned long freq, long u_volt, bool dynamic)
e1f60b29 1153{
23dacf6d 1154 struct dev_pm_opp *new_opp;
50f8cfbd 1155 unsigned long tol;
6ce4184d 1156 int ret;
e1f60b29 1157
8cd2f6e8 1158 opp_rcu_lockdep_assert();
e1f60b29 1159
8cd2f6e8
VK
1160 new_opp = _opp_allocate(opp_table);
1161 if (!new_opp)
1162 return -ENOMEM;
23dacf6d 1163
a7470db6 1164 /* populate the opp table */
a7470db6 1165 new_opp->rate = freq;
2c2709dc 1166 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
dfbe4678
VK
1167 new_opp->supplies[0].u_volt = u_volt;
1168 new_opp->supplies[0].u_volt_min = u_volt - tol;
1169 new_opp->supplies[0].u_volt_max = u_volt + tol;
a7470db6 1170 new_opp->available = true;
23dacf6d 1171 new_opp->dynamic = dynamic;
a7470db6 1172
2c2709dc 1173 ret = _opp_add(dev, new_opp, opp_table);
7f8538eb
VK
1174 if (ret) {
1175 /* Don't return error for duplicate OPPs */
1176 if (ret == -EBUSY)
1177 ret = 0;
6ce4184d 1178 goto free_opp;
7f8538eb 1179 }
64ce8545 1180
03ca370f
MH
1181 /*
1182 * Notify the changes in the availability of the operable
1183 * frequency/voltage list.
1184 */
2c2709dc 1185 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);
e1f60b29 1186 return 0;
6ce4184d
VK
1187
1188free_opp:
8cd2f6e8
VK
1189 _opp_free(new_opp);
1190
6ce4184d 1191 return ret;
e1f60b29 1192}
38393409 1193
7de36b0a
VK
1194/**
1195 * dev_pm_opp_set_supported_hw() - Set supported platforms
1196 * @dev: Device for which supported-hw has to be set.
1197 * @versions: Array of hierarchy of versions to match.
1198 * @count: Number of elements in the array.
1199 *
1200 * This is required only for the V2 bindings, and it enables a platform to
1201 * specify the hierarchy of versions it supports. OPP layer will then enable
1202 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1203 * property.
1204 *
2c2709dc 1205 * Locking: The internal opp_table and opp structures are RCU protected.
7de36b0a
VK
1206 * Hence this function internally uses RCU updater strategy with mutex locks
1207 * to keep the integrity of the internal data structures. Callers should ensure
1208 * that this function is *NOT* called under RCU protection or in contexts where
1209 * mutex cannot be locked.
1210 */
1211int dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
1212 unsigned int count)
1213{
2c2709dc 1214 struct opp_table *opp_table;
7de36b0a
VK
1215 int ret = 0;
1216
2c2709dc
VK
1217 /* Hold our table modification lock here */
1218 mutex_lock(&opp_table_lock);
7de36b0a 1219
2c2709dc
VK
1220 opp_table = _add_opp_table(dev);
1221 if (!opp_table) {
7de36b0a
VK
1222 ret = -ENOMEM;
1223 goto unlock;
1224 }
1225
2c2709dc
VK
1226 /* Make sure there are no concurrent readers while updating opp_table */
1227 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1228
2c2709dc
VK
1229 /* Do we already have a version hierarchy associated with opp_table? */
1230 if (opp_table->supported_hw) {
7de36b0a
VK
1231 dev_err(dev, "%s: Already have supported hardware list\n",
1232 __func__);
1233 ret = -EBUSY;
1234 goto err;
1235 }
1236
2c2709dc 1237 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
7de36b0a 1238 GFP_KERNEL);
2c2709dc 1239 if (!opp_table->supported_hw) {
7de36b0a
VK
1240 ret = -ENOMEM;
1241 goto err;
1242 }
1243
2c2709dc
VK
1244 opp_table->supported_hw_count = count;
1245 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1246 return 0;
1247
1248err:
2c2709dc 1249 _remove_opp_table(opp_table);
7de36b0a 1250unlock:
2c2709dc 1251 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1252
1253 return ret;
1254}
1255EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1256
1257/**
1258 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
a5da6447 1259 * @dev: Device for which supported-hw has to be put.
7de36b0a
VK
1260 *
1261 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1262 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
7de36b0a
VK
1263 * will not be freed.
1264 *
2c2709dc 1265 * Locking: The internal opp_table and opp structures are RCU protected.
7de36b0a
VK
1266 * Hence this function internally uses RCU updater strategy with mutex locks
1267 * to keep the integrity of the internal data structures. Callers should ensure
1268 * that this function is *NOT* called under RCU protection or in contexts where
1269 * mutex cannot be locked.
1270 */
1271void dev_pm_opp_put_supported_hw(struct device *dev)
1272{
2c2709dc 1273 struct opp_table *opp_table;
7de36b0a 1274
2c2709dc
VK
1275 /* Hold our table modification lock here */
1276 mutex_lock(&opp_table_lock);
7de36b0a 1277
2c2709dc
VK
1278 /* Check for existing table for 'dev' first */
1279 opp_table = _find_opp_table(dev);
1280 if (IS_ERR(opp_table)) {
1281 dev_err(dev, "Failed to find opp_table: %ld\n",
1282 PTR_ERR(opp_table));
7de36b0a
VK
1283 goto unlock;
1284 }
1285
2c2709dc
VK
1286 /* Make sure there are no concurrent readers while updating opp_table */
1287 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1288
2c2709dc 1289 if (!opp_table->supported_hw) {
7de36b0a
VK
1290 dev_err(dev, "%s: Doesn't have supported hardware list\n",
1291 __func__);
1292 goto unlock;
1293 }
1294
2c2709dc
VK
1295 kfree(opp_table->supported_hw);
1296 opp_table->supported_hw = NULL;
1297 opp_table->supported_hw_count = 0;
7de36b0a 1298
2c2709dc
VK
1299 /* Try freeing opp_table if this was the last blocking resource */
1300 _remove_opp_table(opp_table);
7de36b0a
VK
1301
1302unlock:
2c2709dc 1303 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1304}
1305EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1306
01fb4d3c
VK
1307/**
1308 * dev_pm_opp_set_prop_name() - Set prop-extn name
a5da6447 1309 * @dev: Device for which the prop-name has to be set.
01fb4d3c
VK
1310 * @name: name to postfix to properties.
1311 *
1312 * This is required only for the V2 bindings, and it enables a platform to
1313 * specify the extn to be used for certain property names. The properties to
1314 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1315 * should postfix the property name with -<name> while looking for them.
1316 *
2c2709dc 1317 * Locking: The internal opp_table and opp structures are RCU protected.
01fb4d3c
VK
1318 * Hence this function internally uses RCU updater strategy with mutex locks
1319 * to keep the integrity of the internal data structures. Callers should ensure
1320 * that this function is *NOT* called under RCU protection or in contexts where
1321 * mutex cannot be locked.
1322 */
1323int dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1324{
2c2709dc 1325 struct opp_table *opp_table;
01fb4d3c
VK
1326 int ret = 0;
1327
2c2709dc
VK
1328 /* Hold our table modification lock here */
1329 mutex_lock(&opp_table_lock);
01fb4d3c 1330
2c2709dc
VK
1331 opp_table = _add_opp_table(dev);
1332 if (!opp_table) {
01fb4d3c
VK
1333 ret = -ENOMEM;
1334 goto unlock;
1335 }
1336
2c2709dc
VK
1337 /* Make sure there are no concurrent readers while updating opp_table */
1338 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1339
2c2709dc
VK
1340 /* Do we already have a prop-name associated with opp_table? */
1341 if (opp_table->prop_name) {
01fb4d3c 1342 dev_err(dev, "%s: Already have prop-name %s\n", __func__,
2c2709dc 1343 opp_table->prop_name);
01fb4d3c
VK
1344 ret = -EBUSY;
1345 goto err;
1346 }
1347
2c2709dc
VK
1348 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1349 if (!opp_table->prop_name) {
01fb4d3c
VK
1350 ret = -ENOMEM;
1351 goto err;
1352 }
1353
2c2709dc 1354 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1355 return 0;
1356
1357err:
2c2709dc 1358 _remove_opp_table(opp_table);
01fb4d3c 1359unlock:
2c2709dc 1360 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1361
1362 return ret;
1363}
1364EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1365
1366/**
1367 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
a5da6447 1368 * @dev: Device for which the prop-name has to be put.
01fb4d3c
VK
1369 *
1370 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1371 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
01fb4d3c
VK
1372 * will not be freed.
1373 *
2c2709dc 1374 * Locking: The internal opp_table and opp structures are RCU protected.
01fb4d3c
VK
1375 * Hence this function internally uses RCU updater strategy with mutex locks
1376 * to keep the integrity of the internal data structures. Callers should ensure
1377 * that this function is *NOT* called under RCU protection or in contexts where
1378 * mutex cannot be locked.
1379 */
1380void dev_pm_opp_put_prop_name(struct device *dev)
1381{
2c2709dc 1382 struct opp_table *opp_table;
01fb4d3c 1383
2c2709dc
VK
1384 /* Hold our table modification lock here */
1385 mutex_lock(&opp_table_lock);
01fb4d3c 1386
2c2709dc
VK
1387 /* Check for existing table for 'dev' first */
1388 opp_table = _find_opp_table(dev);
1389 if (IS_ERR(opp_table)) {
1390 dev_err(dev, "Failed to find opp_table: %ld\n",
1391 PTR_ERR(opp_table));
01fb4d3c
VK
1392 goto unlock;
1393 }
1394
2c2709dc
VK
1395 /* Make sure there are no concurrent readers while updating opp_table */
1396 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1397
2c2709dc 1398 if (!opp_table->prop_name) {
01fb4d3c
VK
1399 dev_err(dev, "%s: Doesn't have a prop-name\n", __func__);
1400 goto unlock;
1401 }
1402
2c2709dc
VK
1403 kfree(opp_table->prop_name);
1404 opp_table->prop_name = NULL;
01fb4d3c 1405
2c2709dc
VK
1406 /* Try freeing opp_table if this was the last blocking resource */
1407 _remove_opp_table(opp_table);
01fb4d3c
VK
1408
1409unlock:
2c2709dc 1410 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1411}
1412EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1413
94735585
VK
1414static int _allocate_set_opp_data(struct opp_table *opp_table)
1415{
1416 struct dev_pm_set_opp_data *data;
1417 int len, count = opp_table->regulator_count;
1418
1419 if (WARN_ON(!count))
1420 return -EINVAL;
1421
1422 /* space for set_opp_data */
1423 len = sizeof(*data);
1424
1425 /* space for old_opp.supplies and new_opp.supplies */
1426 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1427
1428 data = kzalloc(len, GFP_KERNEL);
1429 if (!data)
1430 return -ENOMEM;
1431
1432 data->old_opp.supplies = (void *)(data + 1);
1433 data->new_opp.supplies = data->old_opp.supplies + count;
1434
1435 opp_table->set_opp_data = data;
1436
1437 return 0;
1438}
1439
1440static void _free_set_opp_data(struct opp_table *opp_table)
1441{
1442 kfree(opp_table->set_opp_data);
1443 opp_table->set_opp_data = NULL;
1444}
1445
9f8ea969 1446/**
dfbe4678 1447 * dev_pm_opp_set_regulators() - Set regulator names for the device
9f8ea969 1448 * @dev: Device for which regulator name is being set.
dfbe4678
VK
1449 * @names: Array of pointers to the names of the regulator.
1450 * @count: Number of regulators.
9f8ea969
VK
1451 *
1452 * In order to support OPP switching, OPP layer needs to know the name of the
dfbe4678
VK
1453 * device's regulators, as the core would be required to switch voltages as
1454 * well.
9f8ea969
VK
1455 *
1456 * This must be called before any OPPs are initialized for the device.
1457 *
2c2709dc 1458 * Locking: The internal opp_table and opp structures are RCU protected.
9f8ea969
VK
1459 * Hence this function internally uses RCU updater strategy with mutex locks
1460 * to keep the integrity of the internal data structures. Callers should ensure
1461 * that this function is *NOT* called under RCU protection or in contexts where
1462 * mutex cannot be locked.
1463 */
dfbe4678
VK
1464struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1465 const char * const names[],
1466 unsigned int count)
9f8ea969 1467{
2c2709dc 1468 struct opp_table *opp_table;
9f8ea969 1469 struct regulator *reg;
dfbe4678 1470 int ret, i;
9f8ea969 1471
2c2709dc 1472 mutex_lock(&opp_table_lock);
9f8ea969 1473
2c2709dc
VK
1474 opp_table = _add_opp_table(dev);
1475 if (!opp_table) {
9f8ea969
VK
1476 ret = -ENOMEM;
1477 goto unlock;
1478 }
1479
1480 /* This should be called before OPPs are initialized */
2c2709dc 1481 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
9f8ea969
VK
1482 ret = -EBUSY;
1483 goto err;
1484 }
1485
dfbe4678 1486 /* Already have regulators set */
e231f8d7 1487 if (opp_table->regulators) {
9f8ea969
VK
1488 ret = -EBUSY;
1489 goto err;
1490 }
dfbe4678
VK
1491
1492 opp_table->regulators = kmalloc_array(count,
1493 sizeof(*opp_table->regulators),
1494 GFP_KERNEL);
1495 if (!opp_table->regulators) {
1496 ret = -ENOMEM;
9f8ea969
VK
1497 goto err;
1498 }
1499
dfbe4678
VK
1500 for (i = 0; i < count; i++) {
1501 reg = regulator_get_optional(dev, names[i]);
1502 if (IS_ERR(reg)) {
1503 ret = PTR_ERR(reg);
1504 if (ret != -EPROBE_DEFER)
1505 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1506 __func__, names[i], ret);
1507 goto free_regulators;
1508 }
1509
1510 opp_table->regulators[i] = reg;
1511 }
1512
1513 opp_table->regulator_count = count;
9f8ea969 1514
94735585
VK
1515 /* Allocate block only once to pass to set_opp() routines */
1516 ret = _allocate_set_opp_data(opp_table);
1517 if (ret)
1518 goto free_regulators;
1519
2c2709dc 1520 mutex_unlock(&opp_table_lock);
91291d9a 1521 return opp_table;
9f8ea969 1522
dfbe4678
VK
1523free_regulators:
1524 while (i != 0)
1525 regulator_put(opp_table->regulators[--i]);
1526
1527 kfree(opp_table->regulators);
1528 opp_table->regulators = NULL;
94735585 1529 opp_table->regulator_count = 0;
9f8ea969 1530err:
2c2709dc 1531 _remove_opp_table(opp_table);
9f8ea969 1532unlock:
2c2709dc 1533 mutex_unlock(&opp_table_lock);
9f8ea969 1534
91291d9a 1535 return ERR_PTR(ret);
9f8ea969 1536}
dfbe4678 1537EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
9f8ea969
VK
1538
1539/**
dfbe4678
VK
1540 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1541 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
9f8ea969 1542 *
2c2709dc 1543 * Locking: The internal opp_table and opp structures are RCU protected.
9f8ea969
VK
1544 * Hence this function internally uses RCU updater strategy with mutex locks
1545 * to keep the integrity of the internal data structures. Callers should ensure
1546 * that this function is *NOT* called under RCU protection or in contexts where
1547 * mutex cannot be locked.
1548 */
dfbe4678 1549void dev_pm_opp_put_regulators(struct opp_table *opp_table)
9f8ea969 1550{
dfbe4678
VK
1551 int i;
1552
2c2709dc 1553 mutex_lock(&opp_table_lock);
9f8ea969 1554
dfbe4678
VK
1555 if (!opp_table->regulators) {
1556 pr_err("%s: Doesn't have regulators set\n", __func__);
9f8ea969
VK
1557 goto unlock;
1558 }
1559
2c2709dc
VK
1560 /* Make sure there are no concurrent readers while updating opp_table */
1561 WARN_ON(!list_empty(&opp_table->opp_list));
9f8ea969 1562
dfbe4678
VK
1563 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1564 regulator_put(opp_table->regulators[i]);
1565
94735585
VK
1566 _free_set_opp_data(opp_table);
1567
dfbe4678
VK
1568 kfree(opp_table->regulators);
1569 opp_table->regulators = NULL;
1570 opp_table->regulator_count = 0;
9f8ea969 1571
2c2709dc
VK
1572 /* Try freeing opp_table if this was the last blocking resource */
1573 _remove_opp_table(opp_table);
9f8ea969
VK
1574
1575unlock:
2c2709dc 1576 mutex_unlock(&opp_table_lock);
9f8ea969 1577}
dfbe4678 1578EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
9f8ea969 1579
4dab160e
VK
1580/**
1581 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1582 * @dev: Device for which the helper is getting registered.
1583 * @set_opp: Custom set OPP helper.
1584 *
1585 * This is useful to support complex platforms (like platforms with multiple
1586 * regulators per device), instead of the generic OPP set rate helper.
1587 *
1588 * This must be called before any OPPs are initialized for the device.
1589 *
1590 * Locking: The internal opp_table and opp structures are RCU protected.
1591 * Hence this function internally uses RCU updater strategy with mutex locks
1592 * to keep the integrity of the internal data structures. Callers should ensure
1593 * that this function is *NOT* called under RCU protection or in contexts where
1594 * mutex cannot be locked.
1595 */
1596int dev_pm_opp_register_set_opp_helper(struct device *dev,
1597 int (*set_opp)(struct dev_pm_set_opp_data *data))
1598{
1599 struct opp_table *opp_table;
1600 int ret;
1601
1602 if (!set_opp)
1603 return -EINVAL;
1604
1605 mutex_lock(&opp_table_lock);
1606
1607 opp_table = _add_opp_table(dev);
1608 if (!opp_table) {
1609 ret = -ENOMEM;
1610 goto unlock;
1611 }
1612
1613 /* This should be called before OPPs are initialized */
1614 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1615 ret = -EBUSY;
1616 goto err;
1617 }
1618
1619 /* Already have custom set_opp helper */
1620 if (WARN_ON(opp_table->set_opp)) {
1621 ret = -EBUSY;
1622 goto err;
1623 }
1624
1625 opp_table->set_opp = set_opp;
1626
1627 mutex_unlock(&opp_table_lock);
1628 return 0;
1629
1630err:
1631 _remove_opp_table(opp_table);
1632unlock:
1633 mutex_unlock(&opp_table_lock);
1634
1635 return ret;
1636}
1637EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1638
1639/**
1640 * dev_pm_opp_register_put_opp_helper() - Releases resources blocked for
1641 * set_opp helper
1642 * @dev: Device for which custom set_opp helper has to be cleared.
1643 *
1644 * Locking: The internal opp_table and opp structures are RCU protected.
1645 * Hence this function internally uses RCU updater strategy with mutex locks
1646 * to keep the integrity of the internal data structures. Callers should ensure
1647 * that this function is *NOT* called under RCU protection or in contexts where
1648 * mutex cannot be locked.
1649 */
1650void dev_pm_opp_register_put_opp_helper(struct device *dev)
1651{
1652 struct opp_table *opp_table;
1653
1654 mutex_lock(&opp_table_lock);
1655
1656 /* Check for existing table for 'dev' first */
1657 opp_table = _find_opp_table(dev);
1658 if (IS_ERR(opp_table)) {
1659 dev_err(dev, "Failed to find opp_table: %ld\n",
1660 PTR_ERR(opp_table));
1661 goto unlock;
1662 }
1663
1664 if (!opp_table->set_opp) {
1665 dev_err(dev, "%s: Doesn't have custom set_opp helper set\n",
1666 __func__);
1667 goto unlock;
1668 }
1669
1670 /* Make sure there are no concurrent readers while updating opp_table */
1671 WARN_ON(!list_empty(&opp_table->opp_list));
1672
1673 opp_table->set_opp = NULL;
1674
1675 /* Try freeing opp_table if this was the last blocking resource */
1676 _remove_opp_table(opp_table);
1677
1678unlock:
1679 mutex_unlock(&opp_table_lock);
1680}
1681EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper);
1682
38393409
VK
1683/**
1684 * dev_pm_opp_add() - Add an OPP table from a table definitions
1685 * @dev: device for which we do this operation
1686 * @freq: Frequency in Hz for this OPP
1687 * @u_volt: Voltage in uVolts for this OPP
1688 *
2c2709dc 1689 * This function adds an opp definition to the opp table and returns status.
38393409
VK
1690 * The opp is made available by default and it can be controlled using
1691 * dev_pm_opp_enable/disable functions.
1692 *
2c2709dc 1693 * Locking: The internal opp_table and opp structures are RCU protected.
38393409
VK
1694 * Hence this function internally uses RCU updater strategy with mutex locks
1695 * to keep the integrity of the internal data structures. Callers should ensure
1696 * that this function is *NOT* called under RCU protection or in contexts where
1697 * mutex cannot be locked.
1698 *
1699 * Return:
984f16c8 1700 * 0 On success OR
38393409 1701 * Duplicate OPPs (both freq and volt are same) and opp->available
984f16c8 1702 * -EEXIST Freq are same and volt are different OR
38393409 1703 * Duplicate OPPs (both freq and volt are same) and !opp->available
984f16c8 1704 * -ENOMEM Memory allocation failure
38393409
VK
1705 */
1706int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1707{
8cd2f6e8
VK
1708 struct opp_table *opp_table;
1709 int ret;
1710
1711 /* Hold our table modification lock here */
1712 mutex_lock(&opp_table_lock);
1713
1714 opp_table = _add_opp_table(dev);
1715 if (!opp_table) {
1716 ret = -ENOMEM;
1717 goto unlock;
1718 }
1719
1720 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
1721 if (ret)
1722 _remove_opp_table(opp_table);
1723
1724unlock:
1725 mutex_unlock(&opp_table_lock);
1726 return ret;
38393409 1727}
5d4879cd 1728EXPORT_SYMBOL_GPL(dev_pm_opp_add);
e1f60b29
NM
1729
1730/**
327854c8 1731 * _opp_set_availability() - helper to set the availability of an opp
e1f60b29
NM
1732 * @dev: device for which we do this operation
1733 * @freq: OPP frequency to modify availability
1734 * @availability_req: availability status requested for this opp
1735 *
1736 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
1737 * share a common logic which is isolated here.
1738 *
984f16c8 1739 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1740 * copy operation, returns 0 if no modification was done OR modification was
e1f60b29
NM
1741 * successful.
1742 *
2c2709dc 1743 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1744 * Hence this function internally uses RCU updater strategy with mutex locks to
1745 * keep the integrity of the internal data structures. Callers should ensure
1746 * that this function is *NOT* called under RCU protection or in contexts where
1747 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1748 */
327854c8
NM
1749static int _opp_set_availability(struct device *dev, unsigned long freq,
1750 bool availability_req)
e1f60b29 1751{
2c2709dc 1752 struct opp_table *opp_table;
47d43ba7 1753 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
e1f60b29
NM
1754 int r = 0;
1755
1756 /* keep the node allocated */
47d43ba7 1757 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
59d84ca8 1758 if (!new_opp)
e1f60b29 1759 return -ENOMEM;
e1f60b29 1760
2c2709dc 1761 mutex_lock(&opp_table_lock);
e1f60b29 1762
2c2709dc
VK
1763 /* Find the opp_table */
1764 opp_table = _find_opp_table(dev);
1765 if (IS_ERR(opp_table)) {
1766 r = PTR_ERR(opp_table);
e1f60b29
NM
1767 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1768 goto unlock;
1769 }
1770
1771 /* Do we have the frequency? */
2c2709dc 1772 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
1773 if (tmp_opp->rate == freq) {
1774 opp = tmp_opp;
1775 break;
1776 }
1777 }
1778 if (IS_ERR(opp)) {
1779 r = PTR_ERR(opp);
1780 goto unlock;
1781 }
1782
1783 /* Is update really needed? */
1784 if (opp->available == availability_req)
1785 goto unlock;
1786 /* copy the old data over */
1787 *new_opp = *opp;
1788
1789 /* plug in new node */
1790 new_opp->available = availability_req;
1791
1792 list_replace_rcu(&opp->node, &new_opp->node);
2c2709dc
VK
1793 mutex_unlock(&opp_table_lock);
1794 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
e1f60b29 1795
03ca370f
MH
1796 /* Notify the change of the OPP availability */
1797 if (availability_req)
2c2709dc
VK
1798 srcu_notifier_call_chain(&opp_table->srcu_head,
1799 OPP_EVENT_ENABLE, new_opp);
03ca370f 1800 else
2c2709dc
VK
1801 srcu_notifier_call_chain(&opp_table->srcu_head,
1802 OPP_EVENT_DISABLE, new_opp);
03ca370f 1803
dde8437d 1804 return 0;
e1f60b29
NM
1805
1806unlock:
2c2709dc 1807 mutex_unlock(&opp_table_lock);
e1f60b29
NM
1808 kfree(new_opp);
1809 return r;
1810}
1811
1812/**
5d4879cd 1813 * dev_pm_opp_enable() - Enable a specific OPP
e1f60b29
NM
1814 * @dev: device for which we do this operation
1815 * @freq: OPP frequency to enable
1816 *
1817 * Enables a provided opp. If the operation is valid, this returns 0, else the
1818 * corresponding error value. It is meant to be used for users an OPP available
5d4879cd 1819 * after being temporarily made unavailable with dev_pm_opp_disable.
e1f60b29 1820 *
2c2709dc 1821 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1822 * Hence this function indirectly uses RCU and mutex locks to keep the
1823 * integrity of the internal data structures. Callers should ensure that
1824 * this function is *NOT* called under RCU protection or in contexts where
1825 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1826 *
1827 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1828 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 1829 * successful.
e1f60b29 1830 */
5d4879cd 1831int dev_pm_opp_enable(struct device *dev, unsigned long freq)
e1f60b29 1832{
327854c8 1833 return _opp_set_availability(dev, freq, true);
e1f60b29 1834}
5d4879cd 1835EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
e1f60b29
NM
1836
1837/**
5d4879cd 1838 * dev_pm_opp_disable() - Disable a specific OPP
e1f60b29
NM
1839 * @dev: device for which we do this operation
1840 * @freq: OPP frequency to disable
1841 *
1842 * Disables a provided opp. If the operation is valid, this returns
1843 * 0, else the corresponding error value. It is meant to be a temporary
1844 * control by users to make this OPP not available until the circumstances are
5d4879cd 1845 * right to make it available again (with a call to dev_pm_opp_enable).
e1f60b29 1846 *
2c2709dc 1847 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1848 * Hence this function indirectly uses RCU and mutex locks to keep the
1849 * integrity of the internal data structures. Callers should ensure that
1850 * this function is *NOT* called under RCU protection or in contexts where
1851 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1852 *
1853 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1854 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 1855 * successful.
e1f60b29 1856 */
5d4879cd 1857int dev_pm_opp_disable(struct device *dev, unsigned long freq)
e1f60b29 1858{
327854c8 1859 return _opp_set_availability(dev, freq, false);
e1f60b29 1860}
5d4879cd 1861EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
e1f60b29 1862
03ca370f 1863/**
5d4879cd 1864 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
2c2709dc 1865 * @dev: device pointer used to lookup OPP table.
984f16c8
NM
1866 *
1867 * Return: pointer to notifier head if found, otherwise -ENODEV or
1868 * -EINVAL based on type of error casted as pointer. value must be checked
1869 * with IS_ERR to determine valid pointer or error result.
1870 *
2c2709dc
VK
1871 * Locking: This function must be called under rcu_read_lock(). opp_table is a
1872 * RCU protected pointer. The reason for the same is that the opp pointer which
1873 * is returned will remain valid for use with opp_get_{voltage, freq} only while
984f16c8
NM
1874 * under the locked area. The pointer returned must be used prior to unlocking
1875 * with rcu_read_unlock() to maintain the integrity of the pointer.
03ca370f 1876 */
5d4879cd 1877struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
03ca370f 1878{
2c2709dc 1879 struct opp_table *opp_table = _find_opp_table(dev);
03ca370f 1880
2c2709dc
VK
1881 if (IS_ERR(opp_table))
1882 return ERR_CAST(opp_table); /* matching type */
03ca370f 1883
2c2709dc 1884 return &opp_table->srcu_head;
03ca370f 1885}
4679ec37 1886EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
b496dfbc 1887
411466c5
SH
1888/*
1889 * Free OPPs either created using static entries present in DT or even the
1890 * dynamically added entries based on remove_all param.
b496dfbc 1891 */
8cd2f6e8
VK
1892void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
1893 bool remove_all)
737002b5 1894{
737002b5
VK
1895 struct dev_pm_opp *opp, *tmp;
1896
9274c892
VK
1897 opp_rcu_lockdep_assert();
1898
1899 /* Find if opp_table manages a single device */
1900 if (list_is_singular(&opp_table->dev_list)) {
1901 /* Free static OPPs */
1902 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1903 if (remove_all || !opp->dynamic)
1904 _opp_remove(opp_table, opp);
1905 }
1906 } else {
1907 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
1908 }
1909}
1910
1911void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)
1912{
1913 struct opp_table *opp_table;
1914
2c2709dc
VK
1915 /* Hold our table modification lock here */
1916 mutex_lock(&opp_table_lock);
06441658 1917
2c2709dc
VK
1918 /* Check for existing table for 'dev' */
1919 opp_table = _find_opp_table(dev);
1920 if (IS_ERR(opp_table)) {
1921 int error = PTR_ERR(opp_table);
737002b5
VK
1922
1923 if (error != -ENODEV)
2c2709dc 1924 WARN(1, "%s: opp_table: %d\n",
737002b5
VK
1925 IS_ERR_OR_NULL(dev) ?
1926 "Invalid device" : dev_name(dev),
1927 error);
06441658 1928 goto unlock;
737002b5
VK
1929 }
1930
9274c892 1931 _dev_pm_opp_remove_table(opp_table, dev, remove_all);
737002b5 1932
06441658 1933unlock:
2c2709dc 1934 mutex_unlock(&opp_table_lock);
737002b5 1935}
129eec55
VK
1936
1937/**
411466c5 1938 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
2c2709dc 1939 * @dev: device pointer used to lookup OPP table.
129eec55 1940 *
411466c5
SH
1941 * Free both OPPs created using static entries present in DT and the
1942 * dynamically added entries.
984f16c8 1943 *
2c2709dc 1944 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
1945 * Hence this function indirectly uses RCU updater strategy with mutex locks
1946 * to keep the integrity of the internal data structures. Callers should ensure
1947 * that this function is *NOT* called under RCU protection or in contexts where
1948 * mutex cannot be locked.
129eec55 1949 */
411466c5 1950void dev_pm_opp_remove_table(struct device *dev)
129eec55 1951{
9274c892 1952 _dev_pm_opp_find_and_remove_table(dev, true);
8d4d4e98 1953}
411466c5 1954EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);