PM / OPP: Add light weight _opp_free() routine
[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 */
2c2709dc 832static struct 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
969fceb3
VK
932void _opp_free(struct dev_pm_opp *opp, struct opp_table *opp_table)
933{
934 kfree(opp);
935 _remove_opp_table(opp_table);
936}
937
984f16c8
NM
938/**
939 * _kfree_opp_rcu() - Free OPP RCU handler
940 * @head: RCU head
941 */
327854c8 942static void _kfree_opp_rcu(struct rcu_head *head)
129eec55
VK
943{
944 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
945
946 kfree_rcu(opp, rcu_head);
947}
948
984f16c8
NM
949/**
950 * _opp_remove() - Remove an OPP from a table definition
2c2709dc 951 * @opp_table: points back to the opp_table struct this opp belongs to
984f16c8
NM
952 * @opp: pointer to the OPP to remove
953 *
2c2709dc 954 * This function removes an opp definition from the opp table.
984f16c8 955 *
2c2709dc 956 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
957 * It is assumed that the caller holds required mutex for an RCU updater
958 * strategy.
959 */
969fceb3 960static void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp)
129eec55
VK
961{
962 /*
963 * Notify the changes in the availability of the operable
964 * frequency/voltage list.
965 */
969fceb3 966 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_REMOVE, opp);
deaa5146 967 opp_debug_remove_one(opp);
129eec55 968 list_del_rcu(&opp->node);
2c2709dc 969 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
129eec55 970
2c2709dc 971 _remove_opp_table(opp_table);
129eec55
VK
972}
973
974/**
2c2709dc 975 * dev_pm_opp_remove() - Remove an OPP from OPP table
129eec55
VK
976 * @dev: device for which we do this operation
977 * @freq: OPP to remove with matching 'freq'
978 *
2c2709dc 979 * This function removes an opp from the opp table.
984f16c8 980 *
2c2709dc 981 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
982 * Hence this function internally uses RCU updater strategy with mutex locks
983 * to keep the integrity of the internal data structures. Callers should ensure
984 * that this function is *NOT* called under RCU protection or in contexts where
985 * mutex cannot be locked.
129eec55
VK
986 */
987void dev_pm_opp_remove(struct device *dev, unsigned long freq)
988{
989 struct dev_pm_opp *opp;
2c2709dc 990 struct opp_table *opp_table;
129eec55
VK
991 bool found = false;
992
2c2709dc
VK
993 /* Hold our table modification lock here */
994 mutex_lock(&opp_table_lock);
129eec55 995
2c2709dc
VK
996 opp_table = _find_opp_table(dev);
997 if (IS_ERR(opp_table))
129eec55
VK
998 goto unlock;
999
2c2709dc 1000 list_for_each_entry(opp, &opp_table->opp_list, node) {
129eec55
VK
1001 if (opp->rate == freq) {
1002 found = true;
1003 break;
1004 }
1005 }
1006
1007 if (!found) {
1008 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1009 __func__, freq);
1010 goto unlock;
1011 }
1012
969fceb3 1013 _opp_remove(opp_table, opp);
129eec55 1014unlock:
2c2709dc 1015 mutex_unlock(&opp_table_lock);
129eec55
VK
1016}
1017EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1018
63a69ea4 1019struct dev_pm_opp *_opp_allocate(struct device *dev,
f47b72a1 1020 struct opp_table **opp_table)
e1f60b29 1021{
23dacf6d 1022 struct dev_pm_opp *opp;
dfbe4678
VK
1023 int count, supply_size;
1024 struct opp_table *table;
e1f60b29 1025
dfbe4678
VK
1026 table = _add_opp_table(dev);
1027 if (!table)
23dacf6d 1028 return NULL;
e1f60b29 1029
dfbe4678
VK
1030 /* Allocate space for at least one supply */
1031 count = table->regulator_count ? table->regulator_count : 1;
1032 supply_size = sizeof(*opp->supplies) * count;
e1f60b29 1033
dfbe4678
VK
1034 /* allocate new OPP node and supplies structures */
1035 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
1036 if (!opp) {
1037 kfree(table);
23dacf6d
VK
1038 return NULL;
1039 }
1040
dfbe4678
VK
1041 /* Put the supplies at the end of the OPP structure as an empty array */
1042 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1043 INIT_LIST_HEAD(&opp->node);
1044
1045 *opp_table = table;
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.
984f16c8
NM
1136 * @dev: device for which we do this operation
1137 * @freq: Frequency in Hz for this OPP
1138 * @u_volt: Voltage in uVolts for this OPP
1139 * @dynamic: Dynamically added OPPs.
1140 *
2c2709dc 1141 * This function adds an opp definition to the opp table and returns status.
984f16c8
NM
1142 * The opp is made available by default and it can be controlled using
1143 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1144 *
8f8d37b2
VK
1145 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1146 * and freed by dev_pm_opp_of_remove_table.
984f16c8 1147 *
2c2709dc 1148 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
1149 * Hence this function internally uses RCU updater strategy with mutex locks
1150 * to keep the integrity of the internal data structures. Callers should ensure
1151 * that this function is *NOT* called under RCU protection or in contexts where
1152 * mutex cannot be locked.
1153 *
1154 * Return:
1155 * 0 On success OR
1156 * Duplicate OPPs (both freq and volt are same) and opp->available
1157 * -EEXIST Freq are same and volt are different OR
1158 * Duplicate OPPs (both freq and volt are same) and !opp->available
1159 * -ENOMEM Memory allocation failure
1160 */
f47b72a1
VK
1161int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
1162 bool dynamic)
e1f60b29 1163{
2c2709dc 1164 struct opp_table *opp_table;
23dacf6d 1165 struct dev_pm_opp *new_opp;
50f8cfbd 1166 unsigned long tol;
6ce4184d 1167 int ret;
e1f60b29 1168
2c2709dc
VK
1169 /* Hold our table modification lock here */
1170 mutex_lock(&opp_table_lock);
e1f60b29 1171
63a69ea4 1172 new_opp = _opp_allocate(dev, &opp_table);
23dacf6d
VK
1173 if (!new_opp) {
1174 ret = -ENOMEM;
1175 goto unlock;
1176 }
1177
a7470db6 1178 /* populate the opp table */
a7470db6 1179 new_opp->rate = freq;
2c2709dc 1180 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
dfbe4678
VK
1181 new_opp->supplies[0].u_volt = u_volt;
1182 new_opp->supplies[0].u_volt_min = u_volt - tol;
1183 new_opp->supplies[0].u_volt_max = u_volt + tol;
a7470db6 1184 new_opp->available = true;
23dacf6d 1185 new_opp->dynamic = dynamic;
a7470db6 1186
2c2709dc 1187 ret = _opp_add(dev, new_opp, opp_table);
7f8538eb
VK
1188 if (ret) {
1189 /* Don't return error for duplicate OPPs */
1190 if (ret == -EBUSY)
1191 ret = 0;
6ce4184d 1192 goto free_opp;
7f8538eb 1193 }
64ce8545 1194
2c2709dc 1195 mutex_unlock(&opp_table_lock);
e1f60b29 1196
03ca370f
MH
1197 /*
1198 * Notify the changes in the availability of the operable
1199 * frequency/voltage list.
1200 */
2c2709dc 1201 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);
e1f60b29 1202 return 0;
6ce4184d
VK
1203
1204free_opp:
969fceb3 1205 _opp_free(new_opp, opp_table);
23dacf6d 1206unlock:
2c2709dc 1207 mutex_unlock(&opp_table_lock);
6ce4184d 1208 return ret;
e1f60b29 1209}
38393409 1210
7de36b0a
VK
1211/**
1212 * dev_pm_opp_set_supported_hw() - Set supported platforms
1213 * @dev: Device for which supported-hw has to be set.
1214 * @versions: Array of hierarchy of versions to match.
1215 * @count: Number of elements in the array.
1216 *
1217 * This is required only for the V2 bindings, and it enables a platform to
1218 * specify the hierarchy of versions it supports. OPP layer will then enable
1219 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1220 * property.
1221 *
2c2709dc 1222 * Locking: The internal opp_table and opp structures are RCU protected.
7de36b0a
VK
1223 * Hence this function internally uses RCU updater strategy with mutex locks
1224 * to keep the integrity of the internal data structures. Callers should ensure
1225 * that this function is *NOT* called under RCU protection or in contexts where
1226 * mutex cannot be locked.
1227 */
1228int dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
1229 unsigned int count)
1230{
2c2709dc 1231 struct opp_table *opp_table;
7de36b0a
VK
1232 int ret = 0;
1233
2c2709dc
VK
1234 /* Hold our table modification lock here */
1235 mutex_lock(&opp_table_lock);
7de36b0a 1236
2c2709dc
VK
1237 opp_table = _add_opp_table(dev);
1238 if (!opp_table) {
7de36b0a
VK
1239 ret = -ENOMEM;
1240 goto unlock;
1241 }
1242
2c2709dc
VK
1243 /* Make sure there are no concurrent readers while updating opp_table */
1244 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1245
2c2709dc
VK
1246 /* Do we already have a version hierarchy associated with opp_table? */
1247 if (opp_table->supported_hw) {
7de36b0a
VK
1248 dev_err(dev, "%s: Already have supported hardware list\n",
1249 __func__);
1250 ret = -EBUSY;
1251 goto err;
1252 }
1253
2c2709dc 1254 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
7de36b0a 1255 GFP_KERNEL);
2c2709dc 1256 if (!opp_table->supported_hw) {
7de36b0a
VK
1257 ret = -ENOMEM;
1258 goto err;
1259 }
1260
2c2709dc
VK
1261 opp_table->supported_hw_count = count;
1262 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1263 return 0;
1264
1265err:
2c2709dc 1266 _remove_opp_table(opp_table);
7de36b0a 1267unlock:
2c2709dc 1268 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1269
1270 return ret;
1271}
1272EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1273
1274/**
1275 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
a5da6447 1276 * @dev: Device for which supported-hw has to be put.
7de36b0a
VK
1277 *
1278 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1279 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
7de36b0a
VK
1280 * will not be freed.
1281 *
2c2709dc 1282 * Locking: The internal opp_table and opp structures are RCU protected.
7de36b0a
VK
1283 * Hence this function internally uses RCU updater strategy with mutex locks
1284 * to keep the integrity of the internal data structures. Callers should ensure
1285 * that this function is *NOT* called under RCU protection or in contexts where
1286 * mutex cannot be locked.
1287 */
1288void dev_pm_opp_put_supported_hw(struct device *dev)
1289{
2c2709dc 1290 struct opp_table *opp_table;
7de36b0a 1291
2c2709dc
VK
1292 /* Hold our table modification lock here */
1293 mutex_lock(&opp_table_lock);
7de36b0a 1294
2c2709dc
VK
1295 /* Check for existing table for 'dev' first */
1296 opp_table = _find_opp_table(dev);
1297 if (IS_ERR(opp_table)) {
1298 dev_err(dev, "Failed to find opp_table: %ld\n",
1299 PTR_ERR(opp_table));
7de36b0a
VK
1300 goto unlock;
1301 }
1302
2c2709dc
VK
1303 /* Make sure there are no concurrent readers while updating opp_table */
1304 WARN_ON(!list_empty(&opp_table->opp_list));
7de36b0a 1305
2c2709dc 1306 if (!opp_table->supported_hw) {
7de36b0a
VK
1307 dev_err(dev, "%s: Doesn't have supported hardware list\n",
1308 __func__);
1309 goto unlock;
1310 }
1311
2c2709dc
VK
1312 kfree(opp_table->supported_hw);
1313 opp_table->supported_hw = NULL;
1314 opp_table->supported_hw_count = 0;
7de36b0a 1315
2c2709dc
VK
1316 /* Try freeing opp_table if this was the last blocking resource */
1317 _remove_opp_table(opp_table);
7de36b0a
VK
1318
1319unlock:
2c2709dc 1320 mutex_unlock(&opp_table_lock);
7de36b0a
VK
1321}
1322EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1323
01fb4d3c
VK
1324/**
1325 * dev_pm_opp_set_prop_name() - Set prop-extn name
a5da6447 1326 * @dev: Device for which the prop-name has to be set.
01fb4d3c
VK
1327 * @name: name to postfix to properties.
1328 *
1329 * This is required only for the V2 bindings, and it enables a platform to
1330 * specify the extn to be used for certain property names. The properties to
1331 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1332 * should postfix the property name with -<name> while looking for them.
1333 *
2c2709dc 1334 * Locking: The internal opp_table and opp structures are RCU protected.
01fb4d3c
VK
1335 * Hence this function internally uses RCU updater strategy with mutex locks
1336 * to keep the integrity of the internal data structures. Callers should ensure
1337 * that this function is *NOT* called under RCU protection or in contexts where
1338 * mutex cannot be locked.
1339 */
1340int dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1341{
2c2709dc 1342 struct opp_table *opp_table;
01fb4d3c
VK
1343 int ret = 0;
1344
2c2709dc
VK
1345 /* Hold our table modification lock here */
1346 mutex_lock(&opp_table_lock);
01fb4d3c 1347
2c2709dc
VK
1348 opp_table = _add_opp_table(dev);
1349 if (!opp_table) {
01fb4d3c
VK
1350 ret = -ENOMEM;
1351 goto unlock;
1352 }
1353
2c2709dc
VK
1354 /* Make sure there are no concurrent readers while updating opp_table */
1355 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1356
2c2709dc
VK
1357 /* Do we already have a prop-name associated with opp_table? */
1358 if (opp_table->prop_name) {
01fb4d3c 1359 dev_err(dev, "%s: Already have prop-name %s\n", __func__,
2c2709dc 1360 opp_table->prop_name);
01fb4d3c
VK
1361 ret = -EBUSY;
1362 goto err;
1363 }
1364
2c2709dc
VK
1365 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1366 if (!opp_table->prop_name) {
01fb4d3c
VK
1367 ret = -ENOMEM;
1368 goto err;
1369 }
1370
2c2709dc 1371 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1372 return 0;
1373
1374err:
2c2709dc 1375 _remove_opp_table(opp_table);
01fb4d3c 1376unlock:
2c2709dc 1377 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1378
1379 return ret;
1380}
1381EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1382
1383/**
1384 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
a5da6447 1385 * @dev: Device for which the prop-name has to be put.
01fb4d3c
VK
1386 *
1387 * This is required only for the V2 bindings, and is called for a matching
2c2709dc 1388 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
01fb4d3c
VK
1389 * will not be freed.
1390 *
2c2709dc 1391 * Locking: The internal opp_table and opp structures are RCU protected.
01fb4d3c
VK
1392 * Hence this function internally uses RCU updater strategy with mutex locks
1393 * to keep the integrity of the internal data structures. Callers should ensure
1394 * that this function is *NOT* called under RCU protection or in contexts where
1395 * mutex cannot be locked.
1396 */
1397void dev_pm_opp_put_prop_name(struct device *dev)
1398{
2c2709dc 1399 struct opp_table *opp_table;
01fb4d3c 1400
2c2709dc
VK
1401 /* Hold our table modification lock here */
1402 mutex_lock(&opp_table_lock);
01fb4d3c 1403
2c2709dc
VK
1404 /* Check for existing table for 'dev' first */
1405 opp_table = _find_opp_table(dev);
1406 if (IS_ERR(opp_table)) {
1407 dev_err(dev, "Failed to find opp_table: %ld\n",
1408 PTR_ERR(opp_table));
01fb4d3c
VK
1409 goto unlock;
1410 }
1411
2c2709dc
VK
1412 /* Make sure there are no concurrent readers while updating opp_table */
1413 WARN_ON(!list_empty(&opp_table->opp_list));
01fb4d3c 1414
2c2709dc 1415 if (!opp_table->prop_name) {
01fb4d3c
VK
1416 dev_err(dev, "%s: Doesn't have a prop-name\n", __func__);
1417 goto unlock;
1418 }
1419
2c2709dc
VK
1420 kfree(opp_table->prop_name);
1421 opp_table->prop_name = NULL;
01fb4d3c 1422
2c2709dc
VK
1423 /* Try freeing opp_table if this was the last blocking resource */
1424 _remove_opp_table(opp_table);
01fb4d3c
VK
1425
1426unlock:
2c2709dc 1427 mutex_unlock(&opp_table_lock);
01fb4d3c
VK
1428}
1429EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1430
94735585
VK
1431static int _allocate_set_opp_data(struct opp_table *opp_table)
1432{
1433 struct dev_pm_set_opp_data *data;
1434 int len, count = opp_table->regulator_count;
1435
1436 if (WARN_ON(!count))
1437 return -EINVAL;
1438
1439 /* space for set_opp_data */
1440 len = sizeof(*data);
1441
1442 /* space for old_opp.supplies and new_opp.supplies */
1443 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1444
1445 data = kzalloc(len, GFP_KERNEL);
1446 if (!data)
1447 return -ENOMEM;
1448
1449 data->old_opp.supplies = (void *)(data + 1);
1450 data->new_opp.supplies = data->old_opp.supplies + count;
1451
1452 opp_table->set_opp_data = data;
1453
1454 return 0;
1455}
1456
1457static void _free_set_opp_data(struct opp_table *opp_table)
1458{
1459 kfree(opp_table->set_opp_data);
1460 opp_table->set_opp_data = NULL;
1461}
1462
9f8ea969 1463/**
dfbe4678 1464 * dev_pm_opp_set_regulators() - Set regulator names for the device
9f8ea969 1465 * @dev: Device for which regulator name is being set.
dfbe4678
VK
1466 * @names: Array of pointers to the names of the regulator.
1467 * @count: Number of regulators.
9f8ea969
VK
1468 *
1469 * In order to support OPP switching, OPP layer needs to know the name of the
dfbe4678
VK
1470 * device's regulators, as the core would be required to switch voltages as
1471 * well.
9f8ea969
VK
1472 *
1473 * This must be called before any OPPs are initialized for the device.
1474 *
2c2709dc 1475 * Locking: The internal opp_table and opp structures are RCU protected.
9f8ea969
VK
1476 * Hence this function internally uses RCU updater strategy with mutex locks
1477 * to keep the integrity of the internal data structures. Callers should ensure
1478 * that this function is *NOT* called under RCU protection or in contexts where
1479 * mutex cannot be locked.
1480 */
dfbe4678
VK
1481struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1482 const char * const names[],
1483 unsigned int count)
9f8ea969 1484{
2c2709dc 1485 struct opp_table *opp_table;
9f8ea969 1486 struct regulator *reg;
dfbe4678 1487 int ret, i;
9f8ea969 1488
2c2709dc 1489 mutex_lock(&opp_table_lock);
9f8ea969 1490
2c2709dc
VK
1491 opp_table = _add_opp_table(dev);
1492 if (!opp_table) {
9f8ea969
VK
1493 ret = -ENOMEM;
1494 goto unlock;
1495 }
1496
1497 /* This should be called before OPPs are initialized */
2c2709dc 1498 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
9f8ea969
VK
1499 ret = -EBUSY;
1500 goto err;
1501 }
1502
dfbe4678 1503 /* Already have regulators set */
e231f8d7 1504 if (opp_table->regulators) {
9f8ea969
VK
1505 ret = -EBUSY;
1506 goto err;
1507 }
dfbe4678
VK
1508
1509 opp_table->regulators = kmalloc_array(count,
1510 sizeof(*opp_table->regulators),
1511 GFP_KERNEL);
1512 if (!opp_table->regulators) {
1513 ret = -ENOMEM;
9f8ea969
VK
1514 goto err;
1515 }
1516
dfbe4678
VK
1517 for (i = 0; i < count; i++) {
1518 reg = regulator_get_optional(dev, names[i]);
1519 if (IS_ERR(reg)) {
1520 ret = PTR_ERR(reg);
1521 if (ret != -EPROBE_DEFER)
1522 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1523 __func__, names[i], ret);
1524 goto free_regulators;
1525 }
1526
1527 opp_table->regulators[i] = reg;
1528 }
1529
1530 opp_table->regulator_count = count;
9f8ea969 1531
94735585
VK
1532 /* Allocate block only once to pass to set_opp() routines */
1533 ret = _allocate_set_opp_data(opp_table);
1534 if (ret)
1535 goto free_regulators;
1536
2c2709dc 1537 mutex_unlock(&opp_table_lock);
91291d9a 1538 return opp_table;
9f8ea969 1539
dfbe4678
VK
1540free_regulators:
1541 while (i != 0)
1542 regulator_put(opp_table->regulators[--i]);
1543
1544 kfree(opp_table->regulators);
1545 opp_table->regulators = NULL;
94735585 1546 opp_table->regulator_count = 0;
9f8ea969 1547err:
2c2709dc 1548 _remove_opp_table(opp_table);
9f8ea969 1549unlock:
2c2709dc 1550 mutex_unlock(&opp_table_lock);
9f8ea969 1551
91291d9a 1552 return ERR_PTR(ret);
9f8ea969 1553}
dfbe4678 1554EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
9f8ea969
VK
1555
1556/**
dfbe4678
VK
1557 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1558 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
9f8ea969 1559 *
2c2709dc 1560 * Locking: The internal opp_table and opp structures are RCU protected.
9f8ea969
VK
1561 * Hence this function internally uses RCU updater strategy with mutex locks
1562 * to keep the integrity of the internal data structures. Callers should ensure
1563 * that this function is *NOT* called under RCU protection or in contexts where
1564 * mutex cannot be locked.
1565 */
dfbe4678 1566void dev_pm_opp_put_regulators(struct opp_table *opp_table)
9f8ea969 1567{
dfbe4678
VK
1568 int i;
1569
2c2709dc 1570 mutex_lock(&opp_table_lock);
9f8ea969 1571
dfbe4678
VK
1572 if (!opp_table->regulators) {
1573 pr_err("%s: Doesn't have regulators set\n", __func__);
9f8ea969
VK
1574 goto unlock;
1575 }
1576
2c2709dc
VK
1577 /* Make sure there are no concurrent readers while updating opp_table */
1578 WARN_ON(!list_empty(&opp_table->opp_list));
9f8ea969 1579
dfbe4678
VK
1580 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1581 regulator_put(opp_table->regulators[i]);
1582
94735585
VK
1583 _free_set_opp_data(opp_table);
1584
dfbe4678
VK
1585 kfree(opp_table->regulators);
1586 opp_table->regulators = NULL;
1587 opp_table->regulator_count = 0;
9f8ea969 1588
2c2709dc
VK
1589 /* Try freeing opp_table if this was the last blocking resource */
1590 _remove_opp_table(opp_table);
9f8ea969
VK
1591
1592unlock:
2c2709dc 1593 mutex_unlock(&opp_table_lock);
9f8ea969 1594}
dfbe4678 1595EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
9f8ea969 1596
4dab160e
VK
1597/**
1598 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1599 * @dev: Device for which the helper is getting registered.
1600 * @set_opp: Custom set OPP helper.
1601 *
1602 * This is useful to support complex platforms (like platforms with multiple
1603 * regulators per device), instead of the generic OPP set rate helper.
1604 *
1605 * This must be called before any OPPs are initialized for the device.
1606 *
1607 * Locking: The internal opp_table and opp structures are RCU protected.
1608 * Hence this function internally uses RCU updater strategy with mutex locks
1609 * to keep the integrity of the internal data structures. Callers should ensure
1610 * that this function is *NOT* called under RCU protection or in contexts where
1611 * mutex cannot be locked.
1612 */
1613int dev_pm_opp_register_set_opp_helper(struct device *dev,
1614 int (*set_opp)(struct dev_pm_set_opp_data *data))
1615{
1616 struct opp_table *opp_table;
1617 int ret;
1618
1619 if (!set_opp)
1620 return -EINVAL;
1621
1622 mutex_lock(&opp_table_lock);
1623
1624 opp_table = _add_opp_table(dev);
1625 if (!opp_table) {
1626 ret = -ENOMEM;
1627 goto unlock;
1628 }
1629
1630 /* This should be called before OPPs are initialized */
1631 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1632 ret = -EBUSY;
1633 goto err;
1634 }
1635
1636 /* Already have custom set_opp helper */
1637 if (WARN_ON(opp_table->set_opp)) {
1638 ret = -EBUSY;
1639 goto err;
1640 }
1641
1642 opp_table->set_opp = set_opp;
1643
1644 mutex_unlock(&opp_table_lock);
1645 return 0;
1646
1647err:
1648 _remove_opp_table(opp_table);
1649unlock:
1650 mutex_unlock(&opp_table_lock);
1651
1652 return ret;
1653}
1654EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1655
1656/**
1657 * dev_pm_opp_register_put_opp_helper() - Releases resources blocked for
1658 * set_opp helper
1659 * @dev: Device for which custom set_opp helper has to be cleared.
1660 *
1661 * Locking: The internal opp_table and opp structures are RCU protected.
1662 * Hence this function internally uses RCU updater strategy with mutex locks
1663 * to keep the integrity of the internal data structures. Callers should ensure
1664 * that this function is *NOT* called under RCU protection or in contexts where
1665 * mutex cannot be locked.
1666 */
1667void dev_pm_opp_register_put_opp_helper(struct device *dev)
1668{
1669 struct opp_table *opp_table;
1670
1671 mutex_lock(&opp_table_lock);
1672
1673 /* Check for existing table for 'dev' first */
1674 opp_table = _find_opp_table(dev);
1675 if (IS_ERR(opp_table)) {
1676 dev_err(dev, "Failed to find opp_table: %ld\n",
1677 PTR_ERR(opp_table));
1678 goto unlock;
1679 }
1680
1681 if (!opp_table->set_opp) {
1682 dev_err(dev, "%s: Doesn't have custom set_opp helper set\n",
1683 __func__);
1684 goto unlock;
1685 }
1686
1687 /* Make sure there are no concurrent readers while updating opp_table */
1688 WARN_ON(!list_empty(&opp_table->opp_list));
1689
1690 opp_table->set_opp = NULL;
1691
1692 /* Try freeing opp_table if this was the last blocking resource */
1693 _remove_opp_table(opp_table);
1694
1695unlock:
1696 mutex_unlock(&opp_table_lock);
1697}
1698EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper);
1699
38393409
VK
1700/**
1701 * dev_pm_opp_add() - Add an OPP table from a table definitions
1702 * @dev: device for which we do this operation
1703 * @freq: Frequency in Hz for this OPP
1704 * @u_volt: Voltage in uVolts for this OPP
1705 *
2c2709dc 1706 * This function adds an opp definition to the opp table and returns status.
38393409
VK
1707 * The opp is made available by default and it can be controlled using
1708 * dev_pm_opp_enable/disable functions.
1709 *
2c2709dc 1710 * Locking: The internal opp_table and opp structures are RCU protected.
38393409
VK
1711 * Hence this function internally uses RCU updater strategy with mutex locks
1712 * to keep the integrity of the internal data structures. Callers should ensure
1713 * that this function is *NOT* called under RCU protection or in contexts where
1714 * mutex cannot be locked.
1715 *
1716 * Return:
984f16c8 1717 * 0 On success OR
38393409 1718 * Duplicate OPPs (both freq and volt are same) and opp->available
984f16c8 1719 * -EEXIST Freq are same and volt are different OR
38393409 1720 * Duplicate OPPs (both freq and volt are same) and !opp->available
984f16c8 1721 * -ENOMEM Memory allocation failure
38393409
VK
1722 */
1723int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1724{
b64b9c3f 1725 return _opp_add_v1(dev, freq, u_volt, true);
38393409 1726}
5d4879cd 1727EXPORT_SYMBOL_GPL(dev_pm_opp_add);
e1f60b29
NM
1728
1729/**
327854c8 1730 * _opp_set_availability() - helper to set the availability of an opp
e1f60b29
NM
1731 * @dev: device for which we do this operation
1732 * @freq: OPP frequency to modify availability
1733 * @availability_req: availability status requested for this opp
1734 *
1735 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
1736 * share a common logic which is isolated here.
1737 *
984f16c8 1738 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1739 * copy operation, returns 0 if no modification was done OR modification was
e1f60b29
NM
1740 * successful.
1741 *
2c2709dc 1742 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1743 * Hence this function internally uses RCU updater strategy with mutex locks to
1744 * keep the integrity of the internal data structures. Callers should ensure
1745 * that this function is *NOT* called under RCU protection or in contexts where
1746 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1747 */
327854c8
NM
1748static int _opp_set_availability(struct device *dev, unsigned long freq,
1749 bool availability_req)
e1f60b29 1750{
2c2709dc 1751 struct opp_table *opp_table;
47d43ba7 1752 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
e1f60b29
NM
1753 int r = 0;
1754
1755 /* keep the node allocated */
47d43ba7 1756 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
59d84ca8 1757 if (!new_opp)
e1f60b29 1758 return -ENOMEM;
e1f60b29 1759
2c2709dc 1760 mutex_lock(&opp_table_lock);
e1f60b29 1761
2c2709dc
VK
1762 /* Find the opp_table */
1763 opp_table = _find_opp_table(dev);
1764 if (IS_ERR(opp_table)) {
1765 r = PTR_ERR(opp_table);
e1f60b29
NM
1766 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1767 goto unlock;
1768 }
1769
1770 /* Do we have the frequency? */
2c2709dc 1771 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
e1f60b29
NM
1772 if (tmp_opp->rate == freq) {
1773 opp = tmp_opp;
1774 break;
1775 }
1776 }
1777 if (IS_ERR(opp)) {
1778 r = PTR_ERR(opp);
1779 goto unlock;
1780 }
1781
1782 /* Is update really needed? */
1783 if (opp->available == availability_req)
1784 goto unlock;
1785 /* copy the old data over */
1786 *new_opp = *opp;
1787
1788 /* plug in new node */
1789 new_opp->available = availability_req;
1790
1791 list_replace_rcu(&opp->node, &new_opp->node);
2c2709dc
VK
1792 mutex_unlock(&opp_table_lock);
1793 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
e1f60b29 1794
03ca370f
MH
1795 /* Notify the change of the OPP availability */
1796 if (availability_req)
2c2709dc
VK
1797 srcu_notifier_call_chain(&opp_table->srcu_head,
1798 OPP_EVENT_ENABLE, new_opp);
03ca370f 1799 else
2c2709dc
VK
1800 srcu_notifier_call_chain(&opp_table->srcu_head,
1801 OPP_EVENT_DISABLE, new_opp);
03ca370f 1802
dde8437d 1803 return 0;
e1f60b29
NM
1804
1805unlock:
2c2709dc 1806 mutex_unlock(&opp_table_lock);
e1f60b29
NM
1807 kfree(new_opp);
1808 return r;
1809}
1810
1811/**
5d4879cd 1812 * dev_pm_opp_enable() - Enable a specific OPP
e1f60b29
NM
1813 * @dev: device for which we do this operation
1814 * @freq: OPP frequency to enable
1815 *
1816 * Enables a provided opp. If the operation is valid, this returns 0, else the
1817 * corresponding error value. It is meant to be used for users an OPP available
5d4879cd 1818 * after being temporarily made unavailable with dev_pm_opp_disable.
e1f60b29 1819 *
2c2709dc 1820 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1821 * Hence this function indirectly uses RCU and mutex locks to keep the
1822 * integrity of the internal data structures. Callers should ensure that
1823 * this function is *NOT* called under RCU protection or in contexts where
1824 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1825 *
1826 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1827 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 1828 * successful.
e1f60b29 1829 */
5d4879cd 1830int dev_pm_opp_enable(struct device *dev, unsigned long freq)
e1f60b29 1831{
327854c8 1832 return _opp_set_availability(dev, freq, true);
e1f60b29 1833}
5d4879cd 1834EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
e1f60b29
NM
1835
1836/**
5d4879cd 1837 * dev_pm_opp_disable() - Disable a specific OPP
e1f60b29
NM
1838 * @dev: device for which we do this operation
1839 * @freq: OPP frequency to disable
1840 *
1841 * Disables a provided opp. If the operation is valid, this returns
1842 * 0, else the corresponding error value. It is meant to be a temporary
1843 * control by users to make this OPP not available until the circumstances are
5d4879cd 1844 * right to make it available again (with a call to dev_pm_opp_enable).
e1f60b29 1845 *
2c2709dc 1846 * Locking: The internal opp_table and opp structures are RCU protected.
e1f60b29
NM
1847 * Hence this function indirectly uses RCU and mutex locks to keep the
1848 * integrity of the internal data structures. Callers should ensure that
1849 * this function is *NOT* called under RCU protection or in contexts where
1850 * mutex locking or synchronize_rcu() blocking calls cannot be used.
984f16c8
NM
1851 *
1852 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
e1a2d49c 1853 * copy operation, returns 0 if no modification was done OR modification was
984f16c8 1854 * successful.
e1f60b29 1855 */
5d4879cd 1856int dev_pm_opp_disable(struct device *dev, unsigned long freq)
e1f60b29 1857{
327854c8 1858 return _opp_set_availability(dev, freq, false);
e1f60b29 1859}
5d4879cd 1860EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
e1f60b29 1861
03ca370f 1862/**
5d4879cd 1863 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
2c2709dc 1864 * @dev: device pointer used to lookup OPP table.
984f16c8
NM
1865 *
1866 * Return: pointer to notifier head if found, otherwise -ENODEV or
1867 * -EINVAL based on type of error casted as pointer. value must be checked
1868 * with IS_ERR to determine valid pointer or error result.
1869 *
2c2709dc
VK
1870 * Locking: This function must be called under rcu_read_lock(). opp_table is a
1871 * RCU protected pointer. The reason for the same is that the opp pointer which
1872 * is returned will remain valid for use with opp_get_{voltage, freq} only while
984f16c8
NM
1873 * under the locked area. The pointer returned must be used prior to unlocking
1874 * with rcu_read_unlock() to maintain the integrity of the pointer.
03ca370f 1875 */
5d4879cd 1876struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
03ca370f 1877{
2c2709dc 1878 struct opp_table *opp_table = _find_opp_table(dev);
03ca370f 1879
2c2709dc
VK
1880 if (IS_ERR(opp_table))
1881 return ERR_CAST(opp_table); /* matching type */
03ca370f 1882
2c2709dc 1883 return &opp_table->srcu_head;
03ca370f 1884}
4679ec37 1885EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
b496dfbc 1886
411466c5
SH
1887/*
1888 * Free OPPs either created using static entries present in DT or even the
1889 * dynamically added entries based on remove_all param.
b496dfbc 1890 */
f47b72a1 1891void _dev_pm_opp_remove_table(struct device *dev, bool remove_all)
737002b5 1892{
2c2709dc 1893 struct opp_table *opp_table;
737002b5
VK
1894 struct dev_pm_opp *opp, *tmp;
1895
2c2709dc
VK
1896 /* Hold our table modification lock here */
1897 mutex_lock(&opp_table_lock);
06441658 1898
2c2709dc
VK
1899 /* Check for existing table for 'dev' */
1900 opp_table = _find_opp_table(dev);
1901 if (IS_ERR(opp_table)) {
1902 int error = PTR_ERR(opp_table);
737002b5
VK
1903
1904 if (error != -ENODEV)
2c2709dc 1905 WARN(1, "%s: opp_table: %d\n",
737002b5
VK
1906 IS_ERR_OR_NULL(dev) ?
1907 "Invalid device" : dev_name(dev),
1908 error);
06441658 1909 goto unlock;
737002b5
VK
1910 }
1911
2c2709dc
VK
1912 /* Find if opp_table manages a single device */
1913 if (list_is_singular(&opp_table->dev_list)) {
06441658 1914 /* Free static OPPs */
2c2709dc 1915 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
411466c5 1916 if (remove_all || !opp->dynamic)
969fceb3 1917 _opp_remove(opp_table, opp);
06441658
VK
1918 }
1919 } else {
2c2709dc 1920 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
737002b5
VK
1921 }
1922
06441658 1923unlock:
2c2709dc 1924 mutex_unlock(&opp_table_lock);
737002b5 1925}
129eec55
VK
1926
1927/**
411466c5 1928 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
2c2709dc 1929 * @dev: device pointer used to lookup OPP table.
129eec55 1930 *
411466c5
SH
1931 * Free both OPPs created using static entries present in DT and the
1932 * dynamically added entries.
984f16c8 1933 *
2c2709dc 1934 * Locking: The internal opp_table and opp structures are RCU protected.
984f16c8
NM
1935 * Hence this function indirectly uses RCU updater strategy with mutex locks
1936 * to keep the integrity of the internal data structures. Callers should ensure
1937 * that this function is *NOT* called under RCU protection or in contexts where
1938 * mutex cannot be locked.
129eec55 1939 */
411466c5 1940void dev_pm_opp_remove_table(struct device *dev)
129eec55 1941{
411466c5 1942 _dev_pm_opp_remove_table(dev, true);
8d4d4e98 1943}
411466c5 1944EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);