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