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