PM / OPP: Split out part of _add_opp_table() and _remove_opp_table()
[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 330/**
3aa26a3b 331 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
4eafbd15
BZ
332 * @dev: device for which we do this operation
333 *
3aa26a3b
VK
334 * Return: This function returns the frequency of the OPP marked as suspend_opp
335 * if one is available, else returns 0;
4eafbd15 336 */
3aa26a3b 337unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
4eafbd15 338{
2c2709dc 339 struct opp_table *opp_table;
3aa26a3b 340 unsigned long freq = 0;
4eafbd15 341
3aa26a3b 342 rcu_read_lock();
4eafbd15 343
2c2709dc
VK
344 opp_table = _find_opp_table(dev);
345 if (IS_ERR(opp_table) || !opp_table->suspend_opp ||
346 !opp_table->suspend_opp->available)
3aa26a3b
VK
347 goto unlock;
348
349 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
4eafbd15 350
3aa26a3b
VK
351unlock:
352 rcu_read_unlock();
353 return freq;
4eafbd15 354}
3aa26a3b 355EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
4eafbd15 356
e1f60b29 357/**
2c2709dc 358 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
e1f60b29
NM
359 * @dev: device for which we do this operation
360 *
984f16c8 361 * Return: This function returns the number of available opps if there are any,
e1f60b29
NM
362 * else returns 0 if none or the corresponding error value.
363 *
b4718c02 364 * Locking: This function takes rcu_read_lock().
e1f60b29 365 */
5d4879cd 366int dev_pm_opp_get_opp_count(struct device *dev)
e1f60b29 367{
2c2709dc 368 struct opp_table *opp_table;
47d43ba7 369 struct dev_pm_opp *temp_opp;
e1f60b29
NM
370 int count = 0;
371
b4718c02 372 rcu_read_lock();
b02ded24 373
2c2709dc
VK
374 opp_table = _find_opp_table(dev);
375 if (IS_ERR(opp_table)) {
376 count = PTR_ERR(opp_table);
377 dev_err(dev, "%s: OPP table not found (%d)\n",
b4718c02
DT
378 __func__, count);
379 goto out_unlock;
e1f60b29
NM
380 }
381
2c2709dc 382 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
383 if (temp_opp->available)
384 count++;
385 }
386
b4718c02
DT
387out_unlock:
388 rcu_read_unlock();
e1f60b29
NM
389 return count;
390}
5d4879cd 391EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
e1f60b29
NM
392
393/**
5d4879cd 394 * dev_pm_opp_find_freq_exact() - search for an exact frequency
e1f60b29
NM
395 * @dev: device for which we do this operation
396 * @freq: frequency to search for
7ae49618 397 * @available: true/false - match for available opp
e1f60b29 398 *
2c2709dc 399 * Return: Searches for exact match in the opp table and returns pointer to the
984f16c8
NM
400 * matching opp if found, else returns ERR_PTR in case of error and should
401 * be handled using IS_ERR. Error return values can be:
0779726c
NM
402 * EINVAL: for bad pointer
403 * ERANGE: no match found for search
404 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
405 *
406 * Note: available is a modifier for the search. if available=true, then the
407 * match is for exact matching frequency and is available in the stored OPP
408 * table. if false, the match is for exact frequency which is not available.
409 *
410 * This provides a mechanism to enable an opp which is not available currently
411 * or the opposite as well.
412 *
413 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
414 * protected pointer. The reason for the same is that the opp pointer which is
415 * returned will remain valid for use with opp_get_{voltage, freq} only while
416 * under the locked area. The pointer returned must be used prior to unlocking
417 * with rcu_read_unlock() to maintain the integrity of the pointer.
418 */
47d43ba7
NM
419struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
420 unsigned long freq,
421 bool available)
e1f60b29 422{
2c2709dc 423 struct opp_table *opp_table;
47d43ba7 424 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 425
b02ded24
DT
426 opp_rcu_lockdep_assert();
427
2c2709dc
VK
428 opp_table = _find_opp_table(dev);
429 if (IS_ERR(opp_table)) {
430 int r = PTR_ERR(opp_table);
431
432 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
e1f60b29
NM
433 return ERR_PTR(r);
434 }
435
2c2709dc 436 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
437 if (temp_opp->available == available &&
438 temp_opp->rate == freq) {
439 opp = temp_opp;
440 break;
441 }
442 }
443
444 return opp;
445}
5d4879cd 446EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
e1f60b29 447
067b7ce0
JZ
448static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
449 unsigned long *freq)
450{
451 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
452
453 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
454 if (temp_opp->available && temp_opp->rate >= *freq) {
455 opp = temp_opp;
456 *freq = opp->rate;
457 break;
458 }
459 }
460
461 return opp;
462}
463
e1f60b29 464/**
5d4879cd 465 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
e1f60b29
NM
466 * @dev: device for which we do this operation
467 * @freq: Start frequency
468 *
469 * Search for the matching ceil *available* OPP from a starting freq
470 * for a device.
471 *
984f16c8 472 * Return: matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
473 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
474 * values can be:
475 * EINVAL: for bad pointer
476 * ERANGE: no match found for search
477 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
478 *
479 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
480 * protected pointer. The reason for the same is that the opp pointer which is
481 * returned will remain valid for use with opp_get_{voltage, freq} only while
482 * under the locked area. The pointer returned must be used prior to unlocking
483 * with rcu_read_unlock() to maintain the integrity of the pointer.
484 */
47d43ba7
NM
485struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
486 unsigned long *freq)
e1f60b29 487{
2c2709dc 488 struct opp_table *opp_table;
e1f60b29 489
b02ded24
DT
490 opp_rcu_lockdep_assert();
491
e1f60b29
NM
492 if (!dev || !freq) {
493 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
494 return ERR_PTR(-EINVAL);
495 }
496
2c2709dc
VK
497 opp_table = _find_opp_table(dev);
498 if (IS_ERR(opp_table))
499 return ERR_CAST(opp_table);
e1f60b29 500
067b7ce0 501 return _find_freq_ceil(opp_table, freq);
e1f60b29 502}
5d4879cd 503EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
e1f60b29
NM
504
505/**
5d4879cd 506 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
e1f60b29
NM
507 * @dev: device for which we do this operation
508 * @freq: Start frequency
509 *
510 * Search for the matching floor *available* OPP from a starting freq
511 * for a device.
512 *
984f16c8 513 * Return: matching *opp and refreshes *freq accordingly, else returns
0779726c
NM
514 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
515 * values can be:
516 * EINVAL: for bad pointer
517 * ERANGE: no match found for search
518 * ENODEV: if device not found in list of registered devices
e1f60b29
NM
519 *
520 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
521 * protected pointer. The reason for the same is that the opp pointer which is
522 * returned will remain valid for use with opp_get_{voltage, freq} only while
523 * under the locked area. The pointer returned must be used prior to unlocking
524 * with rcu_read_unlock() to maintain the integrity of the pointer.
525 */
47d43ba7
NM
526struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
527 unsigned long *freq)
e1f60b29 528{
2c2709dc 529 struct opp_table *opp_table;
47d43ba7 530 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
e1f60b29 531
b02ded24
DT
532 opp_rcu_lockdep_assert();
533
e1f60b29
NM
534 if (!dev || !freq) {
535 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
536 return ERR_PTR(-EINVAL);
537 }
538
2c2709dc
VK
539 opp_table = _find_opp_table(dev);
540 if (IS_ERR(opp_table))
541 return ERR_CAST(opp_table);
e1f60b29 542
2c2709dc 543 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
544 if (temp_opp->available) {
545 /* go to the next node, before choosing prev */
546 if (temp_opp->rate > *freq)
547 break;
548 else
549 opp = temp_opp;
550 }
551 }
552 if (!IS_ERR(opp))
553 *freq = opp->rate;
554
555 return opp;
556}
5d4879cd 557EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
e1f60b29 558
6a0712f6 559/*
2c2709dc 560 * The caller needs to ensure that opp_table (and hence the clk) isn't freed,
6a0712f6
VK
561 * while clk returned here is used.
562 */
563static struct clk *_get_opp_clk(struct device *dev)
564{
2c2709dc 565 struct opp_table *opp_table;
6a0712f6
VK
566 struct clk *clk;
567
568 rcu_read_lock();
569
2c2709dc
VK
570 opp_table = _find_opp_table(dev);
571 if (IS_ERR(opp_table)) {
6a0712f6 572 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
2c2709dc 573 clk = ERR_CAST(opp_table);
6a0712f6
VK
574 goto unlock;
575 }
576
2c2709dc 577 clk = opp_table->clk;
6a0712f6
VK
578 if (IS_ERR(clk))
579 dev_err(dev, "%s: No clock available for the device\n",
580 __func__);
581
582unlock:
583 rcu_read_unlock();
584 return clk;
585}
586
587static int _set_opp_voltage(struct device *dev, struct regulator *reg,
ce31781a 588 struct dev_pm_opp_supply *supply)
6a0712f6
VK
589{
590 int ret;
591
592 /* Regulator not available for device */
593 if (IS_ERR(reg)) {
594 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
595 PTR_ERR(reg));
596 return 0;
597 }
598
ce31781a
VK
599 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
600 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
6a0712f6 601
ce31781a
VK
602 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
603 supply->u_volt, supply->u_volt_max);
6a0712f6
VK
604 if (ret)
605 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
ce31781a
VK
606 __func__, supply->u_volt_min, supply->u_volt,
607 supply->u_volt_max, ret);
6a0712f6
VK
608
609 return ret;
610}
611
94735585
VK
612static inline int
613_generic_set_opp_clk_only(struct device *dev, struct clk *clk,
614 unsigned long old_freq, unsigned long freq)
615{
616 int ret;
617
618 ret = clk_set_rate(clk, freq);
619 if (ret) {
620 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
621 ret);
622 }
623
624 return ret;
625}
626
627static int _generic_set_opp(struct dev_pm_set_opp_data *data)
628{
629 struct dev_pm_opp_supply *old_supply = data->old_opp.supplies;
630 struct dev_pm_opp_supply *new_supply = data->new_opp.supplies;
631 unsigned long old_freq = data->old_opp.rate, freq = data->new_opp.rate;
632 struct regulator *reg = data->regulators[0];
633 struct device *dev= data->dev;
634 int ret;
635
636 /* This function only supports single regulator per device */
637 if (WARN_ON(data->regulator_count > 1)) {
638 dev_err(dev, "multiple regulators are not supported\n");
639 return -EINVAL;
640 }
641
642 /* Scaling up? Scale voltage before frequency */
643 if (freq > old_freq) {
644 ret = _set_opp_voltage(dev, reg, new_supply);
645 if (ret)
646 goto restore_voltage;
647 }
648
649 /* Change frequency */
650 ret = _generic_set_opp_clk_only(dev, data->clk, old_freq, freq);
651 if (ret)
652 goto restore_voltage;
653
654 /* Scaling down? Scale voltage after frequency */
655 if (freq < old_freq) {
656 ret = _set_opp_voltage(dev, reg, new_supply);
657 if (ret)
658 goto restore_freq;
659 }
660
661 return 0;
662
663restore_freq:
664 if (_generic_set_opp_clk_only(dev, data->clk, freq, old_freq))
665 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
666 __func__, old_freq);
667restore_voltage:
668 /* This shouldn't harm even if the voltages weren't updated earlier */
669 if (old_supply->u_volt)
670 _set_opp_voltage(dev, reg, old_supply);
671
672 return ret;
673}
674
6a0712f6
VK
675/**
676 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
677 * @dev: device for which we do this operation
678 * @target_freq: frequency to achieve
679 *
680 * This configures the power-supplies and clock source to the levels specified
681 * by the OPP corresponding to the target_freq.
682 *
683 * Locking: This function takes rcu_read_lock().
684 */
685int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
686{
2c2709dc 687 struct opp_table *opp_table;
94735585 688 unsigned long freq, old_freq;
4dab160e 689 int (*set_opp)(struct dev_pm_set_opp_data *data);
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
4dab160e
VK
754 if (opp_table->set_opp)
755 set_opp = opp_table->set_opp;
756 else
757 set_opp = _generic_set_opp;
758
94735585
VK
759 data = opp_table->set_opp_data;
760 data->regulators = regulators;
761 data->regulator_count = opp_table->regulator_count;
762 data->clk = clk;
763 data->dev = dev;
764
765 data->old_opp.rate = old_freq;
766 size = sizeof(*opp->supplies) * opp_table->regulator_count;
ce31781a 767 if (IS_ERR(old_opp))
94735585 768 memset(data->old_opp.supplies, 0, size);
ce31781a 769 else
94735585 770 memcpy(data->old_opp.supplies, old_opp->supplies, size);
6a0712f6 771
94735585
VK
772 data->new_opp.rate = freq;
773 memcpy(data->new_opp.supplies, opp->supplies, size);
6a0712f6
VK
774
775 rcu_read_unlock();
776
4dab160e 777 return set_opp(data);
6a0712f6
VK
778}
779EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
780
2c2709dc
VK
781/* OPP-dev Helpers */
782static void _kfree_opp_dev_rcu(struct rcu_head *head)
06441658 783{
2c2709dc 784 struct opp_device *opp_dev;
06441658 785
2c2709dc
VK
786 opp_dev = container_of(head, struct opp_device, rcu_head);
787 kfree_rcu(opp_dev, rcu_head);
06441658
VK
788}
789
2c2709dc
VK
790static void _remove_opp_dev(struct opp_device *opp_dev,
791 struct opp_table *opp_table)
06441658 792{
2c2709dc
VK
793 opp_debug_unregister(opp_dev, opp_table);
794 list_del(&opp_dev->node);
795 call_srcu(&opp_table->srcu_head.srcu, &opp_dev->rcu_head,
796 _kfree_opp_dev_rcu);
06441658
VK
797}
798
2c2709dc
VK
799struct opp_device *_add_opp_dev(const struct device *dev,
800 struct opp_table *opp_table)
06441658 801{
2c2709dc 802 struct opp_device *opp_dev;
deaa5146 803 int ret;
06441658 804
2c2709dc
VK
805 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
806 if (!opp_dev)
06441658
VK
807 return NULL;
808
2c2709dc
VK
809 /* Initialize opp-dev */
810 opp_dev->dev = dev;
811 list_add_rcu(&opp_dev->node, &opp_table->dev_list);
06441658 812
2c2709dc
VK
813 /* Create debugfs entries for the opp_table */
814 ret = opp_debug_register(opp_dev, opp_table);
deaa5146
VK
815 if (ret)
816 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
817 __func__, ret);
818
2c2709dc 819 return opp_dev;
06441658
VK
820}
821
b6160e26 822static struct opp_table *_allocate_opp_table(struct device *dev)
07cce74a 823{
2c2709dc
VK
824 struct opp_table *opp_table;
825 struct opp_device *opp_dev;
d54974c2 826 int ret;
07cce74a
VK
827
828 /*
2c2709dc 829 * Allocate a new OPP table. In the infrequent case where a new
07cce74a
VK
830 * device is needed to be added, we pay this penalty.
831 */
2c2709dc
VK
832 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
833 if (!opp_table)
07cce74a
VK
834 return NULL;
835
2c2709dc 836 INIT_LIST_HEAD(&opp_table->dev_list);
06441658 837
2c2709dc
VK
838 opp_dev = _add_opp_dev(dev, opp_table);
839 if (!opp_dev) {
840 kfree(opp_table);
06441658
VK
841 return NULL;
842 }
843
f47b72a1 844 _of_init_opp_table(opp_table, dev);
50f8cfbd 845
d54974c2 846 /* Find clk for the device */
2c2709dc
VK
847 opp_table->clk = clk_get(dev, NULL);
848 if (IS_ERR(opp_table->clk)) {
849 ret = PTR_ERR(opp_table->clk);
d54974c2
VK
850 if (ret != -EPROBE_DEFER)
851 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
852 ret);
853 }
854
2c2709dc
VK
855 srcu_init_notifier_head(&opp_table->srcu_head);
856 INIT_LIST_HEAD(&opp_table->opp_list);
07cce74a 857
2c2709dc
VK
858 /* Secure the device table modification */
859 list_add_rcu(&opp_table->node, &opp_tables);
860 return opp_table;
07cce74a
VK
861}
862
b6160e26
VK
863/**
864 * _add_opp_table() - Find OPP table or allocate a new one
865 * @dev: device for which we do this operation
866 *
867 * It tries to find an existing table first, if it couldn't find one, it
868 * allocates a new OPP table and returns that.
869 *
870 * Return: valid opp_table pointer if success, else NULL.
871 */
872struct opp_table *_add_opp_table(struct device *dev)
873{
874 struct opp_table *opp_table;
875
876 /* Check for existing table for 'dev' first */
877 opp_table = _find_opp_table(dev);
878 if (!IS_ERR(opp_table))
879 return opp_table;
880
881 return _allocate_opp_table(dev);
882}
883
984f16c8 884/**
2c2709dc 885 * _kfree_device_rcu() - Free opp_table RCU handler
737002b5 886 * @head: RCU head
984f16c8 887 */
737002b5 888static void _kfree_device_rcu(struct rcu_head *head)
e1f60b29 889{
2c2709dc
VK
890 struct opp_table *opp_table = container_of(head, struct opp_table,
891 rcu_head);
6ce4184d 892
2c2709dc 893 kfree_rcu(opp_table, rcu_head);
e1f60b29 894}
38393409 895
b6160e26
VK
896static void _free_opp_table(struct opp_table *opp_table)
897{
898 struct opp_device *opp_dev;
899
900 /* Release clk */
901 if (!IS_ERR(opp_table->clk))
902 clk_put(opp_table->clk);
903
904 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
905 node);
906
907 _remove_opp_dev(opp_dev, opp_table);
908
909 /* dev_list must be empty now */
910 WARN_ON(!list_empty(&opp_table->dev_list));
911
912 list_del_rcu(&opp_table->node);
913 call_srcu(&opp_table->srcu_head.srcu, &opp_table->rcu_head,
914 _kfree_device_rcu);
915}
916
38393409 917/**
2c2709dc
VK
918 * _remove_opp_table() - Removes a OPP table
919 * @opp_table: OPP table to be removed.
38393409 920 *
2c2709dc 921 * Removes/frees OPP table if it doesn't contain any OPPs.
38393409 922 */
2c2709dc 923static void _remove_opp_table(struct opp_table *opp_table)
38393409 924{
2c2709dc 925 if (!list_empty(&opp_table->opp_list))
3bac42ca
VK
926 return;
927
2c2709dc 928 if (opp_table->supported_hw)
7de36b0a
VK
929 return;
930
2c2709dc 931 if (opp_table->prop_name)
01fb4d3c
VK
932 return;
933
dfbe4678 934 if (opp_table->regulators)
9f8ea969
VK
935 return;
936
4dab160e
VK
937 if (opp_table->set_opp)
938 return;
939
b6160e26 940 _free_opp_table(opp_table);
38393409 941}
e1f60b29 942
8cd2f6e8 943void _opp_free(struct dev_pm_opp *opp)
969fceb3
VK
944{
945 kfree(opp);
969fceb3
VK
946}
947
984f16c8
NM
948/**
949 * _kfree_opp_rcu() - Free OPP RCU handler
950 * @head: RCU head
951 */
327854c8 952static void _kfree_opp_rcu(struct rcu_head *head)
129eec55
VK
953{
954 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
955
956 kfree_rcu(opp, rcu_head);
957}
958
984f16c8
NM
959/**
960 * _opp_remove() - Remove an OPP from a table definition
2c2709dc 961 * @opp_table: points back to the opp_table struct this opp belongs to
984f16c8
NM
962 * @opp: pointer to the OPP to remove
963 *
2c2709dc 964 * This function removes an opp definition from the opp table.
984f16c8 965 *
2c2709dc 966 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
967 * It is assumed that the caller holds required mutex for an RCU updater
968 * strategy.
969 */
969fceb3 970static void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp)
129eec55
VK
971{
972 /*
973 * Notify the changes in the availability of the operable
974 * frequency/voltage list.
975 */
969fceb3 976 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_REMOVE, opp);
deaa5146 977 opp_debug_remove_one(opp);
129eec55 978 list_del_rcu(&opp->node);
2c2709dc 979 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
129eec55 980
2c2709dc 981 _remove_opp_table(opp_table);
129eec55
VK
982}
983
984/**
2c2709dc 985 * dev_pm_opp_remove() - Remove an OPP from OPP table
129eec55
VK
986 * @dev: device for which we do this operation
987 * @freq: OPP to remove with matching 'freq'
988 *
2c2709dc 989 * This function removes an opp from the opp table.
984f16c8 990 *
2c2709dc 991 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
992 * Hence this function internally uses RCU updater strategy with mutex locks
993 * to keep the integrity of the internal data structures. Callers should ensure
994 * that this function is *NOT* called under RCU protection or in contexts where
995 * mutex cannot be locked.
129eec55
VK
996 */
997void dev_pm_opp_remove(struct device *dev, unsigned long freq)
998{
999 struct dev_pm_opp *opp;
2c2709dc 1000 struct opp_table *opp_table;
129eec55
VK
1001 bool found = false;
1002
2c2709dc
VK
1003 /* Hold our table modification lock here */
1004 mutex_lock(&opp_table_lock);
129eec55 1005
2c2709dc
VK
1006 opp_table = _find_opp_table(dev);
1007 if (IS_ERR(opp_table))
129eec55
VK
1008 goto unlock;
1009
2c2709dc 1010 list_for_each_entry(opp, &opp_table->opp_list, node) {
129eec55
VK
1011 if (opp->rate == freq) {
1012 found = true;
1013 break;
1014 }
1015 }
1016
1017 if (!found) {
1018 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1019 __func__, freq);
1020 goto unlock;
1021 }
1022
969fceb3 1023 _opp_remove(opp_table, opp);
129eec55 1024unlock:
2c2709dc 1025 mutex_unlock(&opp_table_lock);
129eec55
VK
1026}
1027EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1028
8cd2f6e8 1029struct dev_pm_opp *_opp_allocate(struct opp_table *table)
e1f60b29 1030{
23dacf6d 1031 struct dev_pm_opp *opp;
dfbe4678 1032 int count, supply_size;
e1f60b29 1033
dfbe4678
VK
1034 /* Allocate space for at least one supply */
1035 count = table->regulator_count ? table->regulator_count : 1;
1036 supply_size = sizeof(*opp->supplies) * count;
e1f60b29 1037
dfbe4678
VK
1038 /* allocate new OPP node and supplies structures */
1039 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
8cd2f6e8 1040 if (!opp)
23dacf6d 1041 return NULL;
23dacf6d 1042
dfbe4678
VK
1043 /* Put the supplies at the end of the OPP structure as an empty array */
1044 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1045 INIT_LIST_HEAD(&opp->node);
1046
23dacf6d
VK
1047 return opp;
1048}
1049
7d34d56e 1050static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
2c2709dc 1051 struct opp_table *opp_table)
7d34d56e 1052{
dfbe4678
VK
1053 struct regulator *reg;
1054 int i;
1055
1056 for (i = 0; i < opp_table->regulator_count; i++) {
1057 reg = opp_table->regulators[i];
1058
1059 if (!regulator_is_supported_voltage(reg,
1060 opp->supplies[i].u_volt_min,
1061 opp->supplies[i].u_volt_max)) {
1062 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1063 __func__, opp->supplies[i].u_volt_min,
1064 opp->supplies[i].u_volt_max);
1065 return false;
1066 }
7d34d56e
VK
1067 }
1068
1069 return true;
1070}
1071
7f8538eb
VK
1072/*
1073 * Returns:
1074 * 0: On success. And appropriate error message for duplicate OPPs.
1075 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1076 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1077 * sure we don't print error messages unnecessarily if different parts of
1078 * kernel try to initialize the OPP table.
1079 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1080 * should be considered an error by the callers of _opp_add().
1081 */
f47b72a1
VK
1082int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1083 struct opp_table *opp_table)
23dacf6d
VK
1084{
1085 struct dev_pm_opp *opp;
2c2709dc 1086 struct list_head *head = &opp_table->opp_list;
deaa5146 1087 int ret;
23dacf6d
VK
1088
1089 /*
1090 * Insert new OPP in order of increasing frequency and discard if
1091 * already present.
1092 *
2c2709dc 1093 * Need to use &opp_table->opp_list in the condition part of the 'for'
23dacf6d
VK
1094 * loop, don't replace it with head otherwise it will become an infinite
1095 * loop.
1096 */
2c2709dc 1097 list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
23dacf6d
VK
1098 if (new_opp->rate > opp->rate) {
1099 head = &opp->node;
1100 continue;
1101 }
1102
1103 if (new_opp->rate < opp->rate)
1104 break;
1105
1106 /* Duplicate OPPs */
06441658 1107 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
dfbe4678
VK
1108 __func__, opp->rate, opp->supplies[0].u_volt,
1109 opp->available, new_opp->rate,
1110 new_opp->supplies[0].u_volt, new_opp->available);
23dacf6d 1111
dfbe4678 1112 /* Should we compare voltages for all regulators here ? */
0f0fe7e0 1113 return opp->available &&
7f8538eb 1114 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
23dacf6d
VK
1115 }
1116
2c2709dc 1117 new_opp->opp_table = opp_table;
23dacf6d
VK
1118 list_add_rcu(&new_opp->node, head);
1119
2c2709dc 1120 ret = opp_debug_create_one(new_opp, opp_table);
deaa5146
VK
1121 if (ret)
1122 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1123 __func__, ret);
1124
2c2709dc 1125 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
7d34d56e
VK
1126 new_opp->available = false;
1127 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1128 __func__, new_opp->rate);
1129 }
1130
23dacf6d
VK
1131 return 0;
1132}
1133
984f16c8 1134/**
b64b9c3f 1135 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
8cd2f6e8 1136 * @opp_table: OPP table
984f16c8
NM
1137 * @dev: device for which we do this operation
1138 * @freq: Frequency in Hz for this OPP
1139 * @u_volt: Voltage in uVolts for this OPP
1140 * @dynamic: Dynamically added OPPs.
1141 *
2c2709dc 1142 * This function adds an opp definition to the opp table and returns status.
984f16c8
NM
1143 * The opp is made available by default and it can be controlled using
1144 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1145 *
8f8d37b2
VK
1146 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1147 * and freed by dev_pm_opp_of_remove_table.
984f16c8 1148 *
2c2709dc 1149 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
1150 * Hence this function internally uses RCU updater strategy with mutex locks
1151 * to keep the integrity of the internal data structures. Callers should ensure
1152 * that this function is *NOT* called under RCU protection or in contexts where
1153 * mutex cannot be locked.
1154 *
1155 * Return:
1156 * 0 On success OR
1157 * Duplicate OPPs (both freq and volt are same) and opp->available
1158 * -EEXIST Freq are same and volt are different OR
1159 * Duplicate OPPs (both freq and volt are same) and !opp->available
1160 * -ENOMEM Memory allocation failure
1161 */
8cd2f6e8
VK
1162int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1163 unsigned long freq, long u_volt, bool dynamic)
e1f60b29 1164{
23dacf6d 1165 struct dev_pm_opp *new_opp;
50f8cfbd 1166 unsigned long tol;
6ce4184d 1167 int ret;
e1f60b29 1168
8cd2f6e8 1169 opp_rcu_lockdep_assert();
e1f60b29 1170
8cd2f6e8
VK
1171 new_opp = _opp_allocate(opp_table);
1172 if (!new_opp)
1173 return -ENOMEM;
23dacf6d 1174
a7470db6 1175 /* populate the opp table */
a7470db6 1176 new_opp->rate = freq;
2c2709dc 1177 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
dfbe4678
VK
1178 new_opp->supplies[0].u_volt = u_volt;
1179 new_opp->supplies[0].u_volt_min = u_volt - tol;
1180 new_opp->supplies[0].u_volt_max = u_volt + tol;
a7470db6 1181 new_opp->available = true;
23dacf6d 1182 new_opp->dynamic = dynamic;
a7470db6 1183
2c2709dc 1184 ret = _opp_add(dev, new_opp, opp_table);
7f8538eb
VK
1185 if (ret) {
1186 /* Don't return error for duplicate OPPs */
1187 if (ret == -EBUSY)
1188 ret = 0;
6ce4184d 1189 goto free_opp;
7f8538eb 1190 }
64ce8545 1191
03ca370f
MH
1192 /*
1193 * Notify the changes in the availability of the operable
1194 * frequency/voltage list.
1195 */
2c2709dc 1196 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);
e1f60b29 1197 return 0;
6ce4184d
VK
1198
1199free_opp:
8cd2f6e8
VK
1200 _opp_free(new_opp);
1201
6ce4184d 1202 return ret;
e1f60b29 1203}
38393409 1204
7de36b0a
VK
1205/**
1206 * dev_pm_opp_set_supported_hw() - Set supported platforms
1207 * @dev: Device for which supported-hw has to be set.
1208 * @versions: Array of hierarchy of versions to match.
1209 * @count: Number of elements in the array.
1210 *
1211 * This is required only for the V2 bindings, and it enables a platform to
1212 * specify the hierarchy of versions it supports. OPP layer will then enable
1213 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1214 * property.
1215 *
2c2709dc 1216 * Locking: The internal opp_table and opp structures are RCU protected.
7de36b0a
VK
1217 * Hence this function internally uses RCU updater strategy with mutex locks
1218 * to keep the integrity of the internal data structures. Callers should ensure
1219 * that this function is *NOT* called under RCU protection or in contexts where
1220 * mutex cannot be locked.
1221 */
1222int dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
1223 unsigned int count)
1224{
2c2709dc 1225 struct opp_table *opp_table;
7de36b0a
VK
1226 int ret = 0;
1227
2c2709dc
VK
1228 /* Hold our table modification lock here */
1229 mutex_lock(&opp_table_lock);
7de36b0a 1230
2c2709dc
VK
1231 opp_table = _add_opp_table(dev);
1232 if (!opp_table) {
7de36b0a
VK
1233 ret = -ENOMEM;
1234 goto unlock;
1235 }
1236
2c2709dc
VK
1237 /* Make sure there are no concurrent readers while updating opp_table */
1238 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1239
2c2709dc
VK
1240 /* Do we already have a version hierarchy associated with opp_table? */
1241 if (opp_table->supported_hw) {
7de36b0a
VK
1242 dev_err(dev, "%s: Already have supported hardware list\n",
1243 __func__);
1244 ret = -EBUSY;
1245 goto err;
1246 }
1247
2c2709dc 1248 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
7de36b0a 1249 GFP_KERNEL);
2c2709dc 1250 if (!opp_table->supported_hw) {
7de36b0a
VK
1251 ret = -ENOMEM;
1252 goto err;
1253 }
1254
2c2709dc
VK
1255 opp_table->supported_hw_count = count;
1256 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1257 return 0;
1258
1259err:
2c2709dc 1260 _remove_opp_table(opp_table);
7de36b0a 1261unlock:
2c2709dc 1262 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1263
1264 return ret;
1265}
1266EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1267
1268/**
1269 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
a5da6447 1270 * @dev: Device for which supported-hw has to be put.
7de36b0a
VK
1271 *
1272 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1273 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
7de36b0a
VK
1274 * will not be freed.
1275 *
2c2709dc 1276 * Locking: The internal opp_table and opp structures are RCU protected.
7de36b0a
VK
1277 * Hence this function internally uses RCU updater strategy with mutex locks
1278 * to keep the integrity of the internal data structures. Callers should ensure
1279 * that this function is *NOT* called under RCU protection or in contexts where
1280 * mutex cannot be locked.
1281 */
1282void dev_pm_opp_put_supported_hw(struct device *dev)
1283{
2c2709dc 1284 struct opp_table *opp_table;
7de36b0a 1285
2c2709dc
VK
1286 /* Hold our table modification lock here */
1287 mutex_lock(&opp_table_lock);
7de36b0a 1288
2c2709dc
VK
1289 /* Check for existing table for 'dev' first */
1290 opp_table = _find_opp_table(dev);
1291 if (IS_ERR(opp_table)) {
1292 dev_err(dev, "Failed to find opp_table: %ld\n",
1293 PTR_ERR(opp_table));
7de36b0a
VK
1294 goto unlock;
1295 }
1296
2c2709dc
VK
1297 /* Make sure there are no concurrent readers while updating opp_table */
1298 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1299
2c2709dc 1300 if (!opp_table->supported_hw) {
7de36b0a
VK
1301 dev_err(dev, "%s: Doesn't have supported hardware list\n",
1302 __func__);
1303 goto unlock;
1304 }
1305
2c2709dc
VK
1306 kfree(opp_table->supported_hw);
1307 opp_table->supported_hw = NULL;
1308 opp_table->supported_hw_count = 0;
7de36b0a 1309
2c2709dc
VK
1310 /* Try freeing opp_table if this was the last blocking resource */
1311 _remove_opp_table(opp_table);
7de36b0a
VK
1312
1313unlock:
2c2709dc 1314 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1315}
1316EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1317
01fb4d3c
VK
1318/**
1319 * dev_pm_opp_set_prop_name() - Set prop-extn name
a5da6447 1320 * @dev: Device for which the prop-name has to be set.
01fb4d3c
VK
1321 * @name: name to postfix to properties.
1322 *
1323 * This is required only for the V2 bindings, and it enables a platform to
1324 * specify the extn to be used for certain property names. The properties to
1325 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1326 * should postfix the property name with -<name> while looking for them.
1327 *
2c2709dc 1328 * Locking: The internal opp_table and opp structures are RCU protected.
01fb4d3c
VK
1329 * Hence this function internally uses RCU updater strategy with mutex locks
1330 * to keep the integrity of the internal data structures. Callers should ensure
1331 * that this function is *NOT* called under RCU protection or in contexts where
1332 * mutex cannot be locked.
1333 */
1334int dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1335{
2c2709dc 1336 struct opp_table *opp_table;
01fb4d3c
VK
1337 int ret = 0;
1338
2c2709dc
VK
1339 /* Hold our table modification lock here */
1340 mutex_lock(&opp_table_lock);
01fb4d3c 1341
2c2709dc
VK
1342 opp_table = _add_opp_table(dev);
1343 if (!opp_table) {
01fb4d3c
VK
1344 ret = -ENOMEM;
1345 goto unlock;
1346 }
1347
2c2709dc
VK
1348 /* Make sure there are no concurrent readers while updating opp_table */
1349 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1350
2c2709dc
VK
1351 /* Do we already have a prop-name associated with opp_table? */
1352 if (opp_table->prop_name) {
01fb4d3c 1353 dev_err(dev, "%s: Already have prop-name %s\n", __func__,
2c2709dc 1354 opp_table->prop_name);
01fb4d3c
VK
1355 ret = -EBUSY;
1356 goto err;
1357 }
1358
2c2709dc
VK
1359 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1360 if (!opp_table->prop_name) {
01fb4d3c
VK
1361 ret = -ENOMEM;
1362 goto err;
1363 }
1364
2c2709dc 1365 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1366 return 0;
1367
1368err:
2c2709dc 1369 _remove_opp_table(opp_table);
01fb4d3c 1370unlock:
2c2709dc 1371 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1372
1373 return ret;
1374}
1375EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1376
1377/**
1378 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
a5da6447 1379 * @dev: Device for which the prop-name has to be put.
01fb4d3c
VK
1380 *
1381 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1382 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
01fb4d3c
VK
1383 * will not be freed.
1384 *
2c2709dc 1385 * Locking: The internal opp_table and opp structures are RCU protected.
01fb4d3c
VK
1386 * Hence this function internally uses RCU updater strategy with mutex locks
1387 * to keep the integrity of the internal data structures. Callers should ensure
1388 * that this function is *NOT* called under RCU protection or in contexts where
1389 * mutex cannot be locked.
1390 */
1391void dev_pm_opp_put_prop_name(struct device *dev)
1392{
2c2709dc 1393 struct opp_table *opp_table;
01fb4d3c 1394
2c2709dc
VK
1395 /* Hold our table modification lock here */
1396 mutex_lock(&opp_table_lock);
01fb4d3c 1397
2c2709dc
VK
1398 /* Check for existing table for 'dev' first */
1399 opp_table = _find_opp_table(dev);
1400 if (IS_ERR(opp_table)) {
1401 dev_err(dev, "Failed to find opp_table: %ld\n",
1402 PTR_ERR(opp_table));
01fb4d3c
VK
1403 goto unlock;
1404 }
1405
2c2709dc
VK
1406 /* Make sure there are no concurrent readers while updating opp_table */
1407 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1408
2c2709dc 1409 if (!opp_table->prop_name) {
01fb4d3c
VK
1410 dev_err(dev, "%s: Doesn't have a prop-name\n", __func__);
1411 goto unlock;
1412 }
1413
2c2709dc
VK
1414 kfree(opp_table->prop_name);
1415 opp_table->prop_name = NULL;
01fb4d3c 1416
2c2709dc
VK
1417 /* Try freeing opp_table if this was the last blocking resource */
1418 _remove_opp_table(opp_table);
01fb4d3c
VK
1419
1420unlock:
2c2709dc 1421 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1422}
1423EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1424
94735585
VK
1425static int _allocate_set_opp_data(struct opp_table *opp_table)
1426{
1427 struct dev_pm_set_opp_data *data;
1428 int len, count = opp_table->regulator_count;
1429
1430 if (WARN_ON(!count))
1431 return -EINVAL;
1432
1433 /* space for set_opp_data */
1434 len = sizeof(*data);
1435
1436 /* space for old_opp.supplies and new_opp.supplies */
1437 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1438
1439 data = kzalloc(len, GFP_KERNEL);
1440 if (!data)
1441 return -ENOMEM;
1442
1443 data->old_opp.supplies = (void *)(data + 1);
1444 data->new_opp.supplies = data->old_opp.supplies + count;
1445
1446 opp_table->set_opp_data = data;
1447
1448 return 0;
1449}
1450
1451static void _free_set_opp_data(struct opp_table *opp_table)
1452{
1453 kfree(opp_table->set_opp_data);
1454 opp_table->set_opp_data = NULL;
1455}
1456
9f8ea969 1457/**
dfbe4678 1458 * dev_pm_opp_set_regulators() - Set regulator names for the device
9f8ea969 1459 * @dev: Device for which regulator name is being set.
dfbe4678
VK
1460 * @names: Array of pointers to the names of the regulator.
1461 * @count: Number of regulators.
9f8ea969
VK
1462 *
1463 * In order to support OPP switching, OPP layer needs to know the name of the
dfbe4678
VK
1464 * device's regulators, as the core would be required to switch voltages as
1465 * well.
9f8ea969
VK
1466 *
1467 * This must be called before any OPPs are initialized for the device.
1468 *
2c2709dc 1469 * Locking: The internal opp_table and opp structures are RCU protected.
9f8ea969
VK
1470 * Hence this function internally uses RCU updater strategy with mutex locks
1471 * to keep the integrity of the internal data structures. Callers should ensure
1472 * that this function is *NOT* called under RCU protection or in contexts where
1473 * mutex cannot be locked.
1474 */
dfbe4678
VK
1475struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1476 const char * const names[],
1477 unsigned int count)
9f8ea969 1478{
2c2709dc 1479 struct opp_table *opp_table;
9f8ea969 1480 struct regulator *reg;
dfbe4678 1481 int ret, i;
9f8ea969 1482
2c2709dc 1483 mutex_lock(&opp_table_lock);
9f8ea969 1484
2c2709dc
VK
1485 opp_table = _add_opp_table(dev);
1486 if (!opp_table) {
9f8ea969
VK
1487 ret = -ENOMEM;
1488 goto unlock;
1489 }
1490
1491 /* This should be called before OPPs are initialized */
2c2709dc 1492 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
9f8ea969
VK
1493 ret = -EBUSY;
1494 goto err;
1495 }
1496
dfbe4678 1497 /* Already have regulators set */
e231f8d7 1498 if (opp_table->regulators) {
9f8ea969
VK
1499 ret = -EBUSY;
1500 goto err;
1501 }
dfbe4678
VK
1502
1503 opp_table->regulators = kmalloc_array(count,
1504 sizeof(*opp_table->regulators),
1505 GFP_KERNEL);
1506 if (!opp_table->regulators) {
1507 ret = -ENOMEM;
9f8ea969
VK
1508 goto err;
1509 }
1510
dfbe4678
VK
1511 for (i = 0; i < count; i++) {
1512 reg = regulator_get_optional(dev, names[i]);
1513 if (IS_ERR(reg)) {
1514 ret = PTR_ERR(reg);
1515 if (ret != -EPROBE_DEFER)
1516 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1517 __func__, names[i], ret);
1518 goto free_regulators;
1519 }
1520
1521 opp_table->regulators[i] = reg;
1522 }
1523
1524 opp_table->regulator_count = count;
9f8ea969 1525
94735585
VK
1526 /* Allocate block only once to pass to set_opp() routines */
1527 ret = _allocate_set_opp_data(opp_table);
1528 if (ret)
1529 goto free_regulators;
1530
2c2709dc 1531 mutex_unlock(&opp_table_lock);
91291d9a 1532 return opp_table;
9f8ea969 1533
dfbe4678
VK
1534free_regulators:
1535 while (i != 0)
1536 regulator_put(opp_table->regulators[--i]);
1537
1538 kfree(opp_table->regulators);
1539 opp_table->regulators = NULL;
94735585 1540 opp_table->regulator_count = 0;
9f8ea969 1541err:
2c2709dc 1542 _remove_opp_table(opp_table);
9f8ea969 1543unlock:
2c2709dc 1544 mutex_unlock(&opp_table_lock);
9f8ea969 1545
91291d9a 1546 return ERR_PTR(ret);
9f8ea969 1547}
dfbe4678 1548EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
9f8ea969
VK
1549
1550/**
dfbe4678
VK
1551 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1552 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
9f8ea969 1553 *
2c2709dc 1554 * Locking: The internal opp_table and opp structures are RCU protected.
9f8ea969
VK
1555 * Hence this function internally uses RCU updater strategy with mutex locks
1556 * to keep the integrity of the internal data structures. Callers should ensure
1557 * that this function is *NOT* called under RCU protection or in contexts where
1558 * mutex cannot be locked.
1559 */
dfbe4678 1560void dev_pm_opp_put_regulators(struct opp_table *opp_table)
9f8ea969 1561{
dfbe4678
VK
1562 int i;
1563
2c2709dc 1564 mutex_lock(&opp_table_lock);
9f8ea969 1565
dfbe4678
VK
1566 if (!opp_table->regulators) {
1567 pr_err("%s: Doesn't have regulators set\n", __func__);
9f8ea969
VK
1568 goto unlock;
1569 }
1570
2c2709dc
VK
1571 /* Make sure there are no concurrent readers while updating opp_table */
1572 WARN_ON(!list_empty(&opp_table->opp_list));
9f8ea969 1573
dfbe4678
VK
1574 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1575 regulator_put(opp_table->regulators[i]);
1576
94735585
VK
1577 _free_set_opp_data(opp_table);
1578
dfbe4678
VK
1579 kfree(opp_table->regulators);
1580 opp_table->regulators = NULL;
1581 opp_table->regulator_count = 0;
9f8ea969 1582
2c2709dc
VK
1583 /* Try freeing opp_table if this was the last blocking resource */
1584 _remove_opp_table(opp_table);
9f8ea969
VK
1585
1586unlock:
2c2709dc 1587 mutex_unlock(&opp_table_lock);
9f8ea969 1588}
dfbe4678 1589EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
9f8ea969 1590
4dab160e
VK
1591/**
1592 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1593 * @dev: Device for which the helper is getting registered.
1594 * @set_opp: Custom set OPP helper.
1595 *
1596 * This is useful to support complex platforms (like platforms with multiple
1597 * regulators per device), instead of the generic OPP set rate helper.
1598 *
1599 * This must be called before any OPPs are initialized for the device.
1600 *
1601 * Locking: The internal opp_table and opp structures are RCU protected.
1602 * Hence this function internally uses RCU updater strategy with mutex locks
1603 * to keep the integrity of the internal data structures. Callers should ensure
1604 * that this function is *NOT* called under RCU protection or in contexts where
1605 * mutex cannot be locked.
1606 */
1607int dev_pm_opp_register_set_opp_helper(struct device *dev,
1608 int (*set_opp)(struct dev_pm_set_opp_data *data))
1609{
1610 struct opp_table *opp_table;
1611 int ret;
1612
1613 if (!set_opp)
1614 return -EINVAL;
1615
1616 mutex_lock(&opp_table_lock);
1617
1618 opp_table = _add_opp_table(dev);
1619 if (!opp_table) {
1620 ret = -ENOMEM;
1621 goto unlock;
1622 }
1623
1624 /* This should be called before OPPs are initialized */
1625 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1626 ret = -EBUSY;
1627 goto err;
1628 }
1629
1630 /* Already have custom set_opp helper */
1631 if (WARN_ON(opp_table->set_opp)) {
1632 ret = -EBUSY;
1633 goto err;
1634 }
1635
1636 opp_table->set_opp = set_opp;
1637
1638 mutex_unlock(&opp_table_lock);
1639 return 0;
1640
1641err:
1642 _remove_opp_table(opp_table);
1643unlock:
1644 mutex_unlock(&opp_table_lock);
1645
1646 return ret;
1647}
1648EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1649
1650/**
1651 * dev_pm_opp_register_put_opp_helper() - Releases resources blocked for
1652 * set_opp helper
1653 * @dev: Device for which custom set_opp helper has to be cleared.
1654 *
1655 * Locking: The internal opp_table and opp structures are RCU protected.
1656 * Hence this function internally uses RCU updater strategy with mutex locks
1657 * to keep the integrity of the internal data structures. Callers should ensure
1658 * that this function is *NOT* called under RCU protection or in contexts where
1659 * mutex cannot be locked.
1660 */
1661void dev_pm_opp_register_put_opp_helper(struct device *dev)
1662{
1663 struct opp_table *opp_table;
1664
1665 mutex_lock(&opp_table_lock);
1666
1667 /* Check for existing table for 'dev' first */
1668 opp_table = _find_opp_table(dev);
1669 if (IS_ERR(opp_table)) {
1670 dev_err(dev, "Failed to find opp_table: %ld\n",
1671 PTR_ERR(opp_table));
1672 goto unlock;
1673 }
1674
1675 if (!opp_table->set_opp) {
1676 dev_err(dev, "%s: Doesn't have custom set_opp helper set\n",
1677 __func__);
1678 goto unlock;
1679 }
1680
1681 /* Make sure there are no concurrent readers while updating opp_table */
1682 WARN_ON(!list_empty(&opp_table->opp_list));
1683
1684 opp_table->set_opp = NULL;
1685
1686 /* Try freeing opp_table if this was the last blocking resource */
1687 _remove_opp_table(opp_table);
1688
1689unlock:
1690 mutex_unlock(&opp_table_lock);
1691}
1692EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper);
1693
38393409
VK
1694/**
1695 * dev_pm_opp_add() - Add an OPP table from a table definitions
1696 * @dev: device for which we do this operation
1697 * @freq: Frequency in Hz for this OPP
1698 * @u_volt: Voltage in uVolts for this OPP
1699 *
2c2709dc 1700 * This function adds an opp definition to the opp table and returns status.
38393409
VK
1701 * The opp is made available by default and it can be controlled using
1702 * dev_pm_opp_enable/disable functions.
1703 *
2c2709dc 1704 * Locking: The internal opp_table and opp structures are RCU protected.
38393409
VK
1705 * Hence this function internally uses RCU updater strategy with mutex locks
1706 * to keep the integrity of the internal data structures. Callers should ensure
1707 * that this function is *NOT* called under RCU protection or in contexts where
1708 * mutex cannot be locked.
1709 *
1710 * Return:
984f16c8 1711 * 0 On success OR
38393409 1712 * Duplicate OPPs (both freq and volt are same) and opp->available
984f16c8 1713 * -EEXIST Freq are same and volt are different OR
38393409 1714 * Duplicate OPPs (both freq and volt are same) and !opp->available
984f16c8 1715 * -ENOMEM Memory allocation failure
38393409
VK
1716 */
1717int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1718{
8cd2f6e8
VK
1719 struct opp_table *opp_table;
1720 int ret;
1721
1722 /* Hold our table modification lock here */
1723 mutex_lock(&opp_table_lock);
1724
1725 opp_table = _add_opp_table(dev);
1726 if (!opp_table) {
1727 ret = -ENOMEM;
1728 goto unlock;
1729 }
1730
1731 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
1732 if (ret)
1733 _remove_opp_table(opp_table);
1734
1735unlock:
1736 mutex_unlock(&opp_table_lock);
1737 return ret;
38393409 1738}
5d4879cd 1739EXPORT_SYMBOL_GPL(dev_pm_opp_add);
e1f60b29
NM
1740
1741/**
327854c8 1742 * _opp_set_availability() - helper to set the availability of an opp
e1f60b29
NM
1743 * @dev: device for which we do this operation
1744 * @freq: OPP frequency to modify availability
1745 * @availability_req: availability status requested for this opp
1746 *
1747 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
1748 * share a common logic which is isolated here.
1749 *
984f16c8 1750 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1751 * copy operation, returns 0 if no modification was done OR modification was
e1f60b29
NM
1752 * successful.
1753 *
2c2709dc 1754 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1755 * Hence this function internally uses RCU updater strategy with mutex locks to
1756 * keep the integrity of the internal data structures. Callers should ensure
1757 * that this function is *NOT* called under RCU protection or in contexts where
1758 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1759 */
327854c8
NM
1760static int _opp_set_availability(struct device *dev, unsigned long freq,
1761 bool availability_req)
e1f60b29 1762{
2c2709dc 1763 struct opp_table *opp_table;
47d43ba7 1764 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
e1f60b29
NM
1765 int r = 0;
1766
1767 /* keep the node allocated */
47d43ba7 1768 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
59d84ca8 1769 if (!new_opp)
e1f60b29 1770 return -ENOMEM;
e1f60b29 1771
2c2709dc 1772 mutex_lock(&opp_table_lock);
e1f60b29 1773
2c2709dc
VK
1774 /* Find the opp_table */
1775 opp_table = _find_opp_table(dev);
1776 if (IS_ERR(opp_table)) {
1777 r = PTR_ERR(opp_table);
e1f60b29
NM
1778 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1779 goto unlock;
1780 }
1781
1782 /* Do we have the frequency? */
2c2709dc 1783 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
1784 if (tmp_opp->rate == freq) {
1785 opp = tmp_opp;
1786 break;
1787 }
1788 }
1789 if (IS_ERR(opp)) {
1790 r = PTR_ERR(opp);
1791 goto unlock;
1792 }
1793
1794 /* Is update really needed? */
1795 if (opp->available == availability_req)
1796 goto unlock;
1797 /* copy the old data over */
1798 *new_opp = *opp;
1799
1800 /* plug in new node */
1801 new_opp->available = availability_req;
1802
1803 list_replace_rcu(&opp->node, &new_opp->node);
2c2709dc
VK
1804 mutex_unlock(&opp_table_lock);
1805 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
e1f60b29 1806
03ca370f
MH
1807 /* Notify the change of the OPP availability */
1808 if (availability_req)
2c2709dc
VK
1809 srcu_notifier_call_chain(&opp_table->srcu_head,
1810 OPP_EVENT_ENABLE, new_opp);
03ca370f 1811 else
2c2709dc
VK
1812 srcu_notifier_call_chain(&opp_table->srcu_head,
1813 OPP_EVENT_DISABLE, new_opp);
03ca370f 1814
dde8437d 1815 return 0;
e1f60b29
NM
1816
1817unlock:
2c2709dc 1818 mutex_unlock(&opp_table_lock);
e1f60b29
NM
1819 kfree(new_opp);
1820 return r;
1821}
1822
1823/**
5d4879cd 1824 * dev_pm_opp_enable() - Enable a specific OPP
e1f60b29
NM
1825 * @dev: device for which we do this operation
1826 * @freq: OPP frequency to enable
1827 *
1828 * Enables a provided opp. If the operation is valid, this returns 0, else the
1829 * corresponding error value. It is meant to be used for users an OPP available
5d4879cd 1830 * after being temporarily made unavailable with dev_pm_opp_disable.
e1f60b29 1831 *
2c2709dc 1832 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1833 * Hence this function indirectly uses RCU and mutex locks to keep the
1834 * integrity of the internal data structures. Callers should ensure that
1835 * this function is *NOT* called under RCU protection or in contexts where
1836 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1837 *
1838 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1839 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 1840 * successful.
e1f60b29 1841 */
5d4879cd 1842int dev_pm_opp_enable(struct device *dev, unsigned long freq)
e1f60b29 1843{
327854c8 1844 return _opp_set_availability(dev, freq, true);
e1f60b29 1845}
5d4879cd 1846EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
e1f60b29
NM
1847
1848/**
5d4879cd 1849 * dev_pm_opp_disable() - Disable a specific OPP
e1f60b29
NM
1850 * @dev: device for which we do this operation
1851 * @freq: OPP frequency to disable
1852 *
1853 * Disables a provided opp. If the operation is valid, this returns
1854 * 0, else the corresponding error value. It is meant to be a temporary
1855 * control by users to make this OPP not available until the circumstances are
5d4879cd 1856 * right to make it available again (with a call to dev_pm_opp_enable).
e1f60b29 1857 *
2c2709dc 1858 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1859 * Hence this function indirectly uses RCU and mutex locks to keep the
1860 * integrity of the internal data structures. Callers should ensure that
1861 * this function is *NOT* called under RCU protection or in contexts where
1862 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1863 *
1864 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1865 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 1866 * successful.
e1f60b29 1867 */
5d4879cd 1868int dev_pm_opp_disable(struct device *dev, unsigned long freq)
e1f60b29 1869{
327854c8 1870 return _opp_set_availability(dev, freq, false);
e1f60b29 1871}
5d4879cd 1872EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
e1f60b29 1873
03ca370f 1874/**
dc2c9ad5
VK
1875 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
1876 * @dev: Device for which notifier needs to be registered
1877 * @nb: Notifier block to be registered
984f16c8 1878 *
dc2c9ad5
VK
1879 * Return: 0 on success or a negative error value.
1880 */
1881int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
1882{
1883 struct opp_table *opp_table;
1884 int ret;
1885
1886 rcu_read_lock();
1887
1888 opp_table = _find_opp_table(dev);
1889 if (IS_ERR(opp_table)) {
1890 ret = PTR_ERR(opp_table);
1891 goto unlock;
1892 }
1893
1894 ret = srcu_notifier_chain_register(&opp_table->srcu_head, nb);
1895
1896unlock:
1897 rcu_read_unlock();
1898
1899 return ret;
1900}
1901EXPORT_SYMBOL(dev_pm_opp_register_notifier);
1902
1903/**
1904 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
1905 * @dev: Device for which notifier needs to be unregistered
1906 * @nb: Notifier block to be unregistered
984f16c8 1907 *
dc2c9ad5 1908 * Return: 0 on success or a negative error value.
03ca370f 1909 */
dc2c9ad5
VK
1910int dev_pm_opp_unregister_notifier(struct device *dev,
1911 struct notifier_block *nb)
03ca370f 1912{
dc2c9ad5
VK
1913 struct opp_table *opp_table;
1914 int ret;
03ca370f 1915
dc2c9ad5
VK
1916 rcu_read_lock();
1917
1918 opp_table = _find_opp_table(dev);
1919 if (IS_ERR(opp_table)) {
1920 ret = PTR_ERR(opp_table);
1921 goto unlock;
1922 }
1923
1924 ret = srcu_notifier_chain_unregister(&opp_table->srcu_head, nb);
03ca370f 1925
dc2c9ad5
VK
1926unlock:
1927 rcu_read_unlock();
1928
1929 return ret;
03ca370f 1930}
dc2c9ad5 1931EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
b496dfbc 1932
411466c5
SH
1933/*
1934 * Free OPPs either created using static entries present in DT or even the
1935 * dynamically added entries based on remove_all param.
b496dfbc 1936 */
8cd2f6e8
VK
1937void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
1938 bool remove_all)
737002b5 1939{
737002b5
VK
1940 struct dev_pm_opp *opp, *tmp;
1941
9274c892
VK
1942 opp_rcu_lockdep_assert();
1943
1944 /* Find if opp_table manages a single device */
1945 if (list_is_singular(&opp_table->dev_list)) {
1946 /* Free static OPPs */
1947 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1948 if (remove_all || !opp->dynamic)
1949 _opp_remove(opp_table, opp);
1950 }
1951 } else {
1952 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
1953 }
1954}
1955
1956void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)
1957{
1958 struct opp_table *opp_table;
1959
2c2709dc
VK
1960 /* Hold our table modification lock here */
1961 mutex_lock(&opp_table_lock);
06441658 1962
2c2709dc
VK
1963 /* Check for existing table for 'dev' */
1964 opp_table = _find_opp_table(dev);
1965 if (IS_ERR(opp_table)) {
1966 int error = PTR_ERR(opp_table);
737002b5
VK
1967
1968 if (error != -ENODEV)
2c2709dc 1969 WARN(1, "%s: opp_table: %d\n",
737002b5
VK
1970 IS_ERR_OR_NULL(dev) ?
1971 "Invalid device" : dev_name(dev),
1972 error);
06441658 1973 goto unlock;
737002b5
VK
1974 }
1975
9274c892 1976 _dev_pm_opp_remove_table(opp_table, dev, remove_all);
737002b5 1977
06441658 1978unlock:
2c2709dc 1979 mutex_unlock(&opp_table_lock);
737002b5 1980}
129eec55
VK
1981
1982/**
411466c5 1983 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
2c2709dc 1984 * @dev: device pointer used to lookup OPP table.
129eec55 1985 *
411466c5
SH
1986 * Free both OPPs created using static entries present in DT and the
1987 * dynamically added entries.
984f16c8 1988 *
2c2709dc 1989 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
1990 * Hence this function indirectly uses RCU updater strategy with mutex locks
1991 * to keep the integrity of the internal data structures. Callers should ensure
1992 * that this function is *NOT* called under RCU protection or in contexts where
1993 * mutex cannot be locked.
129eec55 1994 */
411466c5 1995void dev_pm_opp_remove_table(struct device *dev)
129eec55 1996{
9274c892 1997 _dev_pm_opp_find_and_remove_table(dev, true);
8d4d4e98 1998}
411466c5 1999EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);