2 * Generic OPP Interface
4 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
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.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/clk.h>
17 #include <linux/errno.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/device.h>
21 #include <linux/export.h>
22 #include <linux/pm_domain.h>
23 #include <linux/regulator/consumer.h>
28 * The root of the list of all opp-tables. All opp_table structures branch off
29 * from here, with each opp_table containing the list of opps it supports in
30 * various states of availability.
32 LIST_HEAD(opp_tables
);
33 /* Lock to allow exclusive modification to the device and opp lists */
34 DEFINE_MUTEX(opp_table_lock
);
36 static void dev_pm_opp_get(struct dev_pm_opp
*opp
);
38 static struct opp_device
*_find_opp_dev(const struct device
*dev
,
39 struct opp_table
*opp_table
)
41 struct opp_device
*opp_dev
;
43 list_for_each_entry(opp_dev
, &opp_table
->dev_list
, node
)
44 if (opp_dev
->dev
== dev
)
50 static struct opp_table
*_find_opp_table_unlocked(struct device
*dev
)
52 struct opp_table
*opp_table
;
54 list_for_each_entry(opp_table
, &opp_tables
, node
) {
55 if (_find_opp_dev(dev
, opp_table
)) {
56 _get_opp_table_kref(opp_table
);
62 return ERR_PTR(-ENODEV
);
66 * _find_opp_table() - find opp_table struct using device pointer
67 * @dev: device pointer used to lookup OPP table
69 * Search OPP table for one containing matching device.
71 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
72 * -EINVAL based on type of error.
74 * The callers must call dev_pm_opp_put_opp_table() after the table is used.
76 struct opp_table
*_find_opp_table(struct device
*dev
)
78 struct opp_table
*opp_table
;
80 if (IS_ERR_OR_NULL(dev
)) {
81 pr_err("%s: Invalid parameters\n", __func__
);
82 return ERR_PTR(-EINVAL
);
85 mutex_lock(&opp_table_lock
);
86 opp_table
= _find_opp_table_unlocked(dev
);
87 mutex_unlock(&opp_table_lock
);
93 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
94 * @opp: opp for which voltage has to be returned for
96 * Return: voltage in micro volt corresponding to the opp, else
99 * This is useful only for devices with single power supply.
101 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp
*opp
)
103 if (IS_ERR_OR_NULL(opp
)) {
104 pr_err("%s: Invalid parameters\n", __func__
);
108 return opp
->supplies
[0].u_volt
;
110 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage
);
113 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
114 * @opp: opp for which frequency has to be returned for
116 * Return: frequency in hertz corresponding to the opp, else
119 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp
*opp
)
121 if (IS_ERR_OR_NULL(opp
) || !opp
->available
) {
122 pr_err("%s: Invalid parameters\n", __func__
);
128 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq
);
130 struct regulator
*dev_pm_opp_get_regulator(struct device
*dev
)
132 struct opp_table
*opp_table
;
133 struct regulator
*reg
;
137 opp_table
= _find_opp_table(dev
);
138 if (IS_ERR(opp_table
)) {
140 return ERR_CAST(opp_table
);
143 reg
= opp_table
->regulators
[0];
149 EXPORT_SYMBOL_GPL(dev_pm_opp_get_regulator
);
152 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
153 * @opp: opp for which turbo mode is being verified
155 * Turbo OPPs are not for normal use, and can be enabled (under certain
156 * conditions) for short duration of times to finish high throughput work
157 * quickly. Running on them for longer times may overheat the chip.
159 * Return: true if opp is turbo opp, else false.
161 bool dev_pm_opp_is_turbo(struct dev_pm_opp
*opp
)
163 if (IS_ERR_OR_NULL(opp
) || !opp
->available
) {
164 pr_err("%s: Invalid parameters\n", __func__
);
170 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo
);
173 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
174 * @dev: device for which we do this operation
176 * Return: This function returns the max clock latency in nanoseconds.
178 unsigned long dev_pm_opp_get_max_clock_latency(struct device
*dev
)
180 struct opp_table
*opp_table
;
181 unsigned long clock_latency_ns
;
183 opp_table
= _find_opp_table(dev
);
184 if (IS_ERR(opp_table
))
187 clock_latency_ns
= opp_table
->clock_latency_ns_max
;
189 dev_pm_opp_put_opp_table(opp_table
);
191 return clock_latency_ns
;
193 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency
);
196 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
197 * @dev: device for which we do this operation
199 * Return: This function returns the max voltage latency in nanoseconds.
201 unsigned long dev_pm_opp_get_max_volt_latency(struct device
*dev
)
203 struct opp_table
*opp_table
;
204 struct dev_pm_opp
*opp
;
205 struct regulator
*reg
;
206 unsigned long latency_ns
= 0;
213 opp_table
= _find_opp_table(dev
);
214 if (IS_ERR(opp_table
))
217 count
= opp_table
->regulator_count
;
219 /* Regulator may not be required for the device */
223 uV
= kmalloc_array(count
, sizeof(*uV
), GFP_KERNEL
);
227 mutex_lock(&opp_table
->lock
);
229 for (i
= 0; i
< count
; i
++) {
233 list_for_each_entry(opp
, &opp_table
->opp_list
, node
) {
237 if (opp
->supplies
[i
].u_volt_min
< uV
[i
].min
)
238 uV
[i
].min
= opp
->supplies
[i
].u_volt_min
;
239 if (opp
->supplies
[i
].u_volt_max
> uV
[i
].max
)
240 uV
[i
].max
= opp
->supplies
[i
].u_volt_max
;
244 mutex_unlock(&opp_table
->lock
);
247 * The caller needs to ensure that opp_table (and hence the regulator)
248 * isn't freed, while we are executing this routine.
250 for (i
= 0; i
< count
; i
++) {
251 reg
= opp_table
->regulators
[i
];
252 ret
= regulator_set_voltage_time(reg
, uV
[i
].min
, uV
[i
].max
);
254 latency_ns
+= ret
* 1000;
259 dev_pm_opp_put_opp_table(opp_table
);
263 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency
);
266 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
268 * @dev: device for which we do this operation
270 * Return: This function returns the max transition latency, in nanoseconds, to
271 * switch from one OPP to other.
273 unsigned long dev_pm_opp_get_max_transition_latency(struct device
*dev
)
275 return dev_pm_opp_get_max_volt_latency(dev
) +
276 dev_pm_opp_get_max_clock_latency(dev
);
278 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency
);
281 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
282 * @dev: device for which we do this operation
284 * Return: This function returns the frequency of the OPP marked as suspend_opp
285 * if one is available, else returns 0;
287 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device
*dev
)
289 struct opp_table
*opp_table
;
290 unsigned long freq
= 0;
292 opp_table
= _find_opp_table(dev
);
293 if (IS_ERR(opp_table
))
296 if (opp_table
->suspend_opp
&& opp_table
->suspend_opp
->available
)
297 freq
= dev_pm_opp_get_freq(opp_table
->suspend_opp
);
299 dev_pm_opp_put_opp_table(opp_table
);
303 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq
);
306 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
307 * @dev: device for which we do this operation
309 * Return: This function returns the number of available opps if there are any,
310 * else returns 0 if none or the corresponding error value.
312 int dev_pm_opp_get_opp_count(struct device
*dev
)
314 struct opp_table
*opp_table
;
315 struct dev_pm_opp
*temp_opp
;
318 opp_table
= _find_opp_table(dev
);
319 if (IS_ERR(opp_table
)) {
320 count
= PTR_ERR(opp_table
);
321 dev_dbg(dev
, "%s: OPP table not found (%d)\n",
326 mutex_lock(&opp_table
->lock
);
328 list_for_each_entry(temp_opp
, &opp_table
->opp_list
, node
) {
329 if (temp_opp
->available
)
333 mutex_unlock(&opp_table
->lock
);
334 dev_pm_opp_put_opp_table(opp_table
);
338 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count
);
341 * dev_pm_opp_find_freq_exact() - search for an exact frequency
342 * @dev: device for which we do this operation
343 * @freq: frequency to search for
344 * @available: true/false - match for available opp
346 * Return: Searches for exact match in the opp table and returns pointer to the
347 * matching opp if found, else returns ERR_PTR in case of error and should
348 * be handled using IS_ERR. Error return values can be:
349 * EINVAL: for bad pointer
350 * ERANGE: no match found for search
351 * ENODEV: if device not found in list of registered devices
353 * Note: available is a modifier for the search. if available=true, then the
354 * match is for exact matching frequency and is available in the stored OPP
355 * table. if false, the match is for exact frequency which is not available.
357 * This provides a mechanism to enable an opp which is not available currently
358 * or the opposite as well.
360 * The callers are required to call dev_pm_opp_put() for the returned OPP after
363 struct dev_pm_opp
*dev_pm_opp_find_freq_exact(struct device
*dev
,
367 struct opp_table
*opp_table
;
368 struct dev_pm_opp
*temp_opp
, *opp
= ERR_PTR(-ERANGE
);
370 opp_table
= _find_opp_table(dev
);
371 if (IS_ERR(opp_table
)) {
372 int r
= PTR_ERR(opp_table
);
374 dev_err(dev
, "%s: OPP table not found (%d)\n", __func__
, r
);
378 mutex_lock(&opp_table
->lock
);
380 list_for_each_entry(temp_opp
, &opp_table
->opp_list
, node
) {
381 if (temp_opp
->available
== available
&&
382 temp_opp
->rate
== freq
) {
385 /* Increment the reference count of OPP */
391 mutex_unlock(&opp_table
->lock
);
392 dev_pm_opp_put_opp_table(opp_table
);
396 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact
);
398 static noinline
struct dev_pm_opp
*_find_freq_ceil(struct opp_table
*opp_table
,
401 struct dev_pm_opp
*temp_opp
, *opp
= ERR_PTR(-ERANGE
);
403 mutex_lock(&opp_table
->lock
);
405 list_for_each_entry(temp_opp
, &opp_table
->opp_list
, node
) {
406 if (temp_opp
->available
&& temp_opp
->rate
>= *freq
) {
410 /* Increment the reference count of OPP */
416 mutex_unlock(&opp_table
->lock
);
422 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
423 * @dev: device for which we do this operation
424 * @freq: Start frequency
426 * Search for the matching ceil *available* OPP from a starting freq
429 * Return: matching *opp and refreshes *freq accordingly, else returns
430 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
432 * EINVAL: for bad pointer
433 * ERANGE: no match found for search
434 * ENODEV: if device not found in list of registered devices
436 * The callers are required to call dev_pm_opp_put() for the returned OPP after
439 struct dev_pm_opp
*dev_pm_opp_find_freq_ceil(struct device
*dev
,
442 struct opp_table
*opp_table
;
443 struct dev_pm_opp
*opp
;
446 dev_err(dev
, "%s: Invalid argument freq=%p\n", __func__
, freq
);
447 return ERR_PTR(-EINVAL
);
450 opp_table
= _find_opp_table(dev
);
451 if (IS_ERR(opp_table
))
452 return ERR_CAST(opp_table
);
454 opp
= _find_freq_ceil(opp_table
, freq
);
456 dev_pm_opp_put_opp_table(opp_table
);
460 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil
);
463 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
464 * @dev: device for which we do this operation
465 * @freq: Start frequency
467 * Search for the matching floor *available* OPP from a starting freq
470 * Return: matching *opp and refreshes *freq accordingly, else returns
471 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
473 * EINVAL: for bad pointer
474 * ERANGE: no match found for search
475 * ENODEV: if device not found in list of registered devices
477 * The callers are required to call dev_pm_opp_put() for the returned OPP after
480 struct dev_pm_opp
*dev_pm_opp_find_freq_floor(struct device
*dev
,
483 struct opp_table
*opp_table
;
484 struct dev_pm_opp
*temp_opp
, *opp
= ERR_PTR(-ERANGE
);
487 dev_err(dev
, "%s: Invalid argument freq=%p\n", __func__
, freq
);
488 return ERR_PTR(-EINVAL
);
491 opp_table
= _find_opp_table(dev
);
492 if (IS_ERR(opp_table
))
493 return ERR_CAST(opp_table
);
495 mutex_lock(&opp_table
->lock
);
497 list_for_each_entry(temp_opp
, &opp_table
->opp_list
, node
) {
498 if (temp_opp
->available
) {
499 /* go to the next node, before choosing prev */
500 if (temp_opp
->rate
> *freq
)
507 /* Increment the reference count of OPP */
510 mutex_unlock(&opp_table
->lock
);
511 dev_pm_opp_put_opp_table(opp_table
);
518 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor
);
520 static int _set_opp_voltage(struct device
*dev
, struct regulator
*reg
,
521 struct dev_pm_opp_supply
*supply
)
525 /* Regulator not available for device */
527 dev_dbg(dev
, "%s: regulator not available: %ld\n", __func__
,
532 dev_dbg(dev
, "%s: voltages (mV): %lu %lu %lu\n", __func__
,
533 supply
->u_volt_min
, supply
->u_volt
, supply
->u_volt_max
);
535 ret
= regulator_set_voltage_triplet(reg
, supply
->u_volt_min
,
536 supply
->u_volt
, supply
->u_volt_max
);
538 dev_err(dev
, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
539 __func__
, supply
->u_volt_min
, supply
->u_volt
,
540 supply
->u_volt_max
, ret
);
546 _generic_set_opp_clk_only(struct device
*dev
, struct clk
*clk
,
547 unsigned long old_freq
, unsigned long freq
)
551 ret
= clk_set_rate(clk
, freq
);
553 dev_err(dev
, "%s: failed to set clock rate: %d\n", __func__
,
561 _generic_set_opp_domain(struct device
*dev
, struct clk
*clk
,
562 unsigned long old_freq
, unsigned long freq
,
563 unsigned int old_pstate
, unsigned int new_pstate
)
567 /* Scaling up? Scale domain performance state before frequency */
568 if (freq
> old_freq
) {
569 ret
= dev_pm_genpd_set_performance_state(dev
, new_pstate
);
574 ret
= _generic_set_opp_clk_only(dev
, clk
, old_freq
, freq
);
576 goto restore_domain_state
;
578 /* Scaling down? Scale domain performance state after frequency */
579 if (freq
< old_freq
) {
580 ret
= dev_pm_genpd_set_performance_state(dev
, new_pstate
);
588 if (_generic_set_opp_clk_only(dev
, clk
, freq
, old_freq
))
589 dev_err(dev
, "%s: failed to restore old-freq (%lu Hz)\n",
591 restore_domain_state
:
593 dev_pm_genpd_set_performance_state(dev
, old_pstate
);
598 static int _generic_set_opp_regulator(const struct opp_table
*opp_table
,
600 unsigned long old_freq
,
602 struct dev_pm_opp_supply
*old_supply
,
603 struct dev_pm_opp_supply
*new_supply
)
605 struct regulator
*reg
= opp_table
->regulators
[0];
608 /* This function only supports single regulator per device */
609 if (WARN_ON(opp_table
->regulator_count
> 1)) {
610 dev_err(dev
, "multiple regulators are not supported\n");
614 /* Scaling up? Scale voltage before frequency */
615 if (freq
> old_freq
) {
616 ret
= _set_opp_voltage(dev
, reg
, new_supply
);
618 goto restore_voltage
;
621 /* Change frequency */
622 ret
= _generic_set_opp_clk_only(dev
, opp_table
->clk
, old_freq
, freq
);
624 goto restore_voltage
;
626 /* Scaling down? Scale voltage after frequency */
627 if (freq
< old_freq
) {
628 ret
= _set_opp_voltage(dev
, reg
, new_supply
);
636 if (_generic_set_opp_clk_only(dev
, opp_table
->clk
, freq
, old_freq
))
637 dev_err(dev
, "%s: failed to restore old-freq (%lu Hz)\n",
640 /* This shouldn't harm even if the voltages weren't updated earlier */
642 _set_opp_voltage(dev
, reg
, old_supply
);
648 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
649 * @dev: device for which we do this operation
650 * @target_freq: frequency to achieve
652 * This configures the power-supplies and clock source to the levels specified
653 * by the OPP corresponding to the target_freq.
655 int dev_pm_opp_set_rate(struct device
*dev
, unsigned long target_freq
)
657 struct opp_table
*opp_table
;
658 unsigned long freq
, old_freq
;
659 struct dev_pm_opp
*old_opp
, *opp
;
663 if (unlikely(!target_freq
)) {
664 dev_err(dev
, "%s: Invalid target frequency %lu\n", __func__
,
669 opp_table
= _find_opp_table(dev
);
670 if (IS_ERR(opp_table
)) {
671 dev_err(dev
, "%s: device opp doesn't exist\n", __func__
);
672 return PTR_ERR(opp_table
);
675 clk
= opp_table
->clk
;
677 dev_err(dev
, "%s: No clock available for the device\n",
683 freq
= clk_round_rate(clk
, target_freq
);
687 old_freq
= clk_get_rate(clk
);
689 /* Return early if nothing to do */
690 if (old_freq
== freq
) {
691 dev_dbg(dev
, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
697 old_opp
= _find_freq_ceil(opp_table
, &old_freq
);
698 if (IS_ERR(old_opp
)) {
699 dev_err(dev
, "%s: failed to find current OPP for freq %lu (%ld)\n",
700 __func__
, old_freq
, PTR_ERR(old_opp
));
703 opp
= _find_freq_ceil(opp_table
, &freq
);
706 dev_err(dev
, "%s: failed to find OPP for freq %lu (%d)\n",
707 __func__
, freq
, ret
);
711 dev_dbg(dev
, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__
,
714 /* Only frequency scaling */
715 if (!opp_table
->regulators
) {
717 * We don't support devices with both regulator and
718 * domain performance-state for now.
720 if (opp_table
->genpd_performance_state
)
721 ret
= _generic_set_opp_domain(dev
, clk
, old_freq
, freq
,
722 IS_ERR(old_opp
) ? 0 : old_opp
->pstate
,
725 ret
= _generic_set_opp_clk_only(dev
, clk
, old_freq
, freq
);
726 } else if (!opp_table
->set_opp
) {
727 ret
= _generic_set_opp_regulator(opp_table
, dev
, old_freq
, freq
,
728 IS_ERR(old_opp
) ? NULL
: old_opp
->supplies
,
731 struct dev_pm_set_opp_data
*data
;
733 data
= opp_table
->set_opp_data
;
734 data
->regulators
= opp_table
->regulators
;
735 data
->regulator_count
= opp_table
->regulator_count
;
739 data
->old_opp
.rate
= old_freq
;
740 size
= sizeof(*opp
->supplies
) * opp_table
->regulator_count
;
742 memset(data
->old_opp
.supplies
, 0, size
);
744 memcpy(data
->old_opp
.supplies
, old_opp
->supplies
, size
);
746 data
->new_opp
.rate
= freq
;
747 memcpy(data
->new_opp
.supplies
, opp
->supplies
, size
);
749 ret
= opp_table
->set_opp(data
);
754 if (!IS_ERR(old_opp
))
755 dev_pm_opp_put(old_opp
);
757 dev_pm_opp_put_opp_table(opp_table
);
760 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate
);
762 /* OPP-dev Helpers */
763 static void _remove_opp_dev(struct opp_device
*opp_dev
,
764 struct opp_table
*opp_table
)
766 opp_debug_unregister(opp_dev
, opp_table
);
767 list_del(&opp_dev
->node
);
771 struct opp_device
*_add_opp_dev(const struct device
*dev
,
772 struct opp_table
*opp_table
)
774 struct opp_device
*opp_dev
;
777 opp_dev
= kzalloc(sizeof(*opp_dev
), GFP_KERNEL
);
781 /* Initialize opp-dev */
783 list_add(&opp_dev
->node
, &opp_table
->dev_list
);
785 /* Create debugfs entries for the opp_table */
786 ret
= opp_debug_register(opp_dev
, opp_table
);
788 dev_err(dev
, "%s: Failed to register opp debugfs (%d)\n",
794 static struct opp_table
*_allocate_opp_table(struct device
*dev
)
796 struct opp_table
*opp_table
;
797 struct opp_device
*opp_dev
;
801 * Allocate a new OPP table. In the infrequent case where a new
802 * device is needed to be added, we pay this penalty.
804 opp_table
= kzalloc(sizeof(*opp_table
), GFP_KERNEL
);
808 INIT_LIST_HEAD(&opp_table
->dev_list
);
810 opp_dev
= _add_opp_dev(dev
, opp_table
);
816 _of_init_opp_table(opp_table
, dev
);
818 /* Find clk for the device */
819 opp_table
->clk
= clk_get(dev
, NULL
);
820 if (IS_ERR(opp_table
->clk
)) {
821 ret
= PTR_ERR(opp_table
->clk
);
822 if (ret
!= -EPROBE_DEFER
)
823 dev_dbg(dev
, "%s: Couldn't find clock: %d\n", __func__
,
827 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table
->head
);
828 INIT_LIST_HEAD(&opp_table
->opp_list
);
829 mutex_init(&opp_table
->lock
);
830 kref_init(&opp_table
->kref
);
832 /* Secure the device table modification */
833 list_add(&opp_table
->node
, &opp_tables
);
837 void _get_opp_table_kref(struct opp_table
*opp_table
)
839 kref_get(&opp_table
->kref
);
842 struct opp_table
*dev_pm_opp_get_opp_table(struct device
*dev
)
844 struct opp_table
*opp_table
;
846 /* Hold our table modification lock here */
847 mutex_lock(&opp_table_lock
);
849 opp_table
= _find_opp_table_unlocked(dev
);
850 if (!IS_ERR(opp_table
))
853 opp_table
= _allocate_opp_table(dev
);
856 mutex_unlock(&opp_table_lock
);
860 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table
);
862 static void _opp_table_kref_release(struct kref
*kref
)
864 struct opp_table
*opp_table
= container_of(kref
, struct opp_table
, kref
);
865 struct opp_device
*opp_dev
;
868 if (!IS_ERR(opp_table
->clk
))
869 clk_put(opp_table
->clk
);
871 opp_dev
= list_first_entry(&opp_table
->dev_list
, struct opp_device
,
874 _remove_opp_dev(opp_dev
, opp_table
);
876 /* dev_list must be empty now */
877 WARN_ON(!list_empty(&opp_table
->dev_list
));
879 mutex_destroy(&opp_table
->lock
);
880 list_del(&opp_table
->node
);
883 mutex_unlock(&opp_table_lock
);
886 void dev_pm_opp_put_opp_table(struct opp_table
*opp_table
)
888 kref_put_mutex(&opp_table
->kref
, _opp_table_kref_release
,
891 EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table
);
893 void _opp_free(struct dev_pm_opp
*opp
)
898 static void _opp_kref_release(struct kref
*kref
)
900 struct dev_pm_opp
*opp
= container_of(kref
, struct dev_pm_opp
, kref
);
901 struct opp_table
*opp_table
= opp
->opp_table
;
904 * Notify the changes in the availability of the operable
905 * frequency/voltage list.
907 blocking_notifier_call_chain(&opp_table
->head
, OPP_EVENT_REMOVE
, opp
);
908 opp_debug_remove_one(opp
);
909 list_del(&opp
->node
);
912 mutex_unlock(&opp_table
->lock
);
913 dev_pm_opp_put_opp_table(opp_table
);
916 static void dev_pm_opp_get(struct dev_pm_opp
*opp
)
918 kref_get(&opp
->kref
);
921 void dev_pm_opp_put(struct dev_pm_opp
*opp
)
923 kref_put_mutex(&opp
->kref
, _opp_kref_release
, &opp
->opp_table
->lock
);
925 EXPORT_SYMBOL_GPL(dev_pm_opp_put
);
928 * dev_pm_opp_remove() - Remove an OPP from OPP table
929 * @dev: device for which we do this operation
930 * @freq: OPP to remove with matching 'freq'
932 * This function removes an opp from the opp table.
934 void dev_pm_opp_remove(struct device
*dev
, unsigned long freq
)
936 struct dev_pm_opp
*opp
;
937 struct opp_table
*opp_table
;
940 opp_table
= _find_opp_table(dev
);
941 if (IS_ERR(opp_table
))
944 mutex_lock(&opp_table
->lock
);
946 list_for_each_entry(opp
, &opp_table
->opp_list
, node
) {
947 if (opp
->rate
== freq
) {
953 mutex_unlock(&opp_table
->lock
);
958 dev_warn(dev
, "%s: Couldn't find OPP with freq: %lu\n",
962 dev_pm_opp_put_opp_table(opp_table
);
964 EXPORT_SYMBOL_GPL(dev_pm_opp_remove
);
966 struct dev_pm_opp
*_opp_allocate(struct opp_table
*table
)
968 struct dev_pm_opp
*opp
;
969 int count
, supply_size
;
971 /* Allocate space for at least one supply */
972 count
= table
->regulator_count
? table
->regulator_count
: 1;
973 supply_size
= sizeof(*opp
->supplies
) * count
;
975 /* allocate new OPP node and supplies structures */
976 opp
= kzalloc(sizeof(*opp
) + supply_size
, GFP_KERNEL
);
980 /* Put the supplies at the end of the OPP structure as an empty array */
981 opp
->supplies
= (struct dev_pm_opp_supply
*)(opp
+ 1);
982 INIT_LIST_HEAD(&opp
->node
);
987 static bool _opp_supported_by_regulators(struct dev_pm_opp
*opp
,
988 struct opp_table
*opp_table
)
990 struct regulator
*reg
;
993 for (i
= 0; i
< opp_table
->regulator_count
; i
++) {
994 reg
= opp_table
->regulators
[i
];
996 if (!regulator_is_supported_voltage(reg
,
997 opp
->supplies
[i
].u_volt_min
,
998 opp
->supplies
[i
].u_volt_max
)) {
999 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1000 __func__
, opp
->supplies
[i
].u_volt_min
,
1001 opp
->supplies
[i
].u_volt_max
);
1011 * 0: On success. And appropriate error message for duplicate OPPs.
1012 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1013 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1014 * sure we don't print error messages unnecessarily if different parts of
1015 * kernel try to initialize the OPP table.
1016 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1017 * should be considered an error by the callers of _opp_add().
1019 int _opp_add(struct device
*dev
, struct dev_pm_opp
*new_opp
,
1020 struct opp_table
*opp_table
)
1022 struct dev_pm_opp
*opp
;
1023 struct list_head
*head
;
1027 * Insert new OPP in order of increasing frequency and discard if
1030 * Need to use &opp_table->opp_list in the condition part of the 'for'
1031 * loop, don't replace it with head otherwise it will become an infinite
1034 mutex_lock(&opp_table
->lock
);
1035 head
= &opp_table
->opp_list
;
1037 list_for_each_entry(opp
, &opp_table
->opp_list
, node
) {
1038 if (new_opp
->rate
> opp
->rate
) {
1043 if (new_opp
->rate
< opp
->rate
)
1046 /* Duplicate OPPs */
1047 dev_warn(dev
, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1048 __func__
, opp
->rate
, opp
->supplies
[0].u_volt
,
1049 opp
->available
, new_opp
->rate
,
1050 new_opp
->supplies
[0].u_volt
, new_opp
->available
);
1052 /* Should we compare voltages for all regulators here ? */
1053 ret
= opp
->available
&&
1054 new_opp
->supplies
[0].u_volt
== opp
->supplies
[0].u_volt
? -EBUSY
: -EEXIST
;
1056 mutex_unlock(&opp_table
->lock
);
1060 if (opp_table
->get_pstate
)
1061 new_opp
->pstate
= opp_table
->get_pstate(dev
, new_opp
->rate
);
1063 list_add(&new_opp
->node
, head
);
1064 mutex_unlock(&opp_table
->lock
);
1066 new_opp
->opp_table
= opp_table
;
1067 kref_init(&new_opp
->kref
);
1069 /* Get a reference to the OPP table */
1070 _get_opp_table_kref(opp_table
);
1072 ret
= opp_debug_create_one(new_opp
, opp_table
);
1074 dev_err(dev
, "%s: Failed to register opp to debugfs (%d)\n",
1077 if (!_opp_supported_by_regulators(new_opp
, opp_table
)) {
1078 new_opp
->available
= false;
1079 dev_warn(dev
, "%s: OPP not supported by regulators (%lu)\n",
1080 __func__
, new_opp
->rate
);
1087 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
1088 * @opp_table: OPP table
1089 * @dev: device for which we do this operation
1090 * @freq: Frequency in Hz for this OPP
1091 * @u_volt: Voltage in uVolts for this OPP
1092 * @dynamic: Dynamically added OPPs.
1094 * This function adds an opp definition to the opp table and returns status.
1095 * The opp is made available by default and it can be controlled using
1096 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1098 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1099 * and freed by dev_pm_opp_of_remove_table.
1103 * Duplicate OPPs (both freq and volt are same) and opp->available
1104 * -EEXIST Freq are same and volt are different OR
1105 * Duplicate OPPs (both freq and volt are same) and !opp->available
1106 * -ENOMEM Memory allocation failure
1108 int _opp_add_v1(struct opp_table
*opp_table
, struct device
*dev
,
1109 unsigned long freq
, long u_volt
, bool dynamic
)
1111 struct dev_pm_opp
*new_opp
;
1115 new_opp
= _opp_allocate(opp_table
);
1119 /* populate the opp table */
1120 new_opp
->rate
= freq
;
1121 tol
= u_volt
* opp_table
->voltage_tolerance_v1
/ 100;
1122 new_opp
->supplies
[0].u_volt
= u_volt
;
1123 new_opp
->supplies
[0].u_volt_min
= u_volt
- tol
;
1124 new_opp
->supplies
[0].u_volt_max
= u_volt
+ tol
;
1125 new_opp
->available
= true;
1126 new_opp
->dynamic
= dynamic
;
1128 ret
= _opp_add(dev
, new_opp
, opp_table
);
1130 /* Don't return error for duplicate OPPs */
1137 * Notify the changes in the availability of the operable
1138 * frequency/voltage list.
1140 blocking_notifier_call_chain(&opp_table
->head
, OPP_EVENT_ADD
, new_opp
);
1150 * dev_pm_opp_set_supported_hw() - Set supported platforms
1151 * @dev: Device for which supported-hw has to be set.
1152 * @versions: Array of hierarchy of versions to match.
1153 * @count: Number of elements in the array.
1155 * This is required only for the V2 bindings, and it enables a platform to
1156 * specify the hierarchy of versions it supports. OPP layer will then enable
1157 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1160 struct opp_table
*dev_pm_opp_set_supported_hw(struct device
*dev
,
1161 const u32
*versions
, unsigned int count
)
1163 struct opp_table
*opp_table
;
1166 opp_table
= dev_pm_opp_get_opp_table(dev
);
1168 return ERR_PTR(-ENOMEM
);
1170 /* Make sure there are no concurrent readers while updating opp_table */
1171 WARN_ON(!list_empty(&opp_table
->opp_list
));
1173 /* Do we already have a version hierarchy associated with opp_table? */
1174 if (opp_table
->supported_hw
) {
1175 dev_err(dev
, "%s: Already have supported hardware list\n",
1181 opp_table
->supported_hw
= kmemdup(versions
, count
* sizeof(*versions
),
1183 if (!opp_table
->supported_hw
) {
1188 opp_table
->supported_hw_count
= count
;
1193 dev_pm_opp_put_opp_table(opp_table
);
1195 return ERR_PTR(ret
);
1197 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw
);
1200 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
1201 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
1203 * This is required only for the V2 bindings, and is called for a matching
1204 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
1205 * will not be freed.
1207 void dev_pm_opp_put_supported_hw(struct opp_table
*opp_table
)
1209 /* Make sure there are no concurrent readers while updating opp_table */
1210 WARN_ON(!list_empty(&opp_table
->opp_list
));
1212 if (!opp_table
->supported_hw
) {
1213 pr_err("%s: Doesn't have supported hardware list\n",
1218 kfree(opp_table
->supported_hw
);
1219 opp_table
->supported_hw
= NULL
;
1220 opp_table
->supported_hw_count
= 0;
1222 dev_pm_opp_put_opp_table(opp_table
);
1224 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw
);
1227 * dev_pm_opp_set_prop_name() - Set prop-extn name
1228 * @dev: Device for which the prop-name has to be set.
1229 * @name: name to postfix to properties.
1231 * This is required only for the V2 bindings, and it enables a platform to
1232 * specify the extn to be used for certain property names. The properties to
1233 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1234 * should postfix the property name with -<name> while looking for them.
1236 struct opp_table
*dev_pm_opp_set_prop_name(struct device
*dev
, const char *name
)
1238 struct opp_table
*opp_table
;
1241 opp_table
= dev_pm_opp_get_opp_table(dev
);
1243 return ERR_PTR(-ENOMEM
);
1245 /* Make sure there are no concurrent readers while updating opp_table */
1246 WARN_ON(!list_empty(&opp_table
->opp_list
));
1248 /* Do we already have a prop-name associated with opp_table? */
1249 if (opp_table
->prop_name
) {
1250 dev_err(dev
, "%s: Already have prop-name %s\n", __func__
,
1251 opp_table
->prop_name
);
1256 opp_table
->prop_name
= kstrdup(name
, GFP_KERNEL
);
1257 if (!opp_table
->prop_name
) {
1265 dev_pm_opp_put_opp_table(opp_table
);
1267 return ERR_PTR(ret
);
1269 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name
);
1272 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
1273 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
1275 * This is required only for the V2 bindings, and is called for a matching
1276 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
1277 * will not be freed.
1279 void dev_pm_opp_put_prop_name(struct opp_table
*opp_table
)
1281 /* Make sure there are no concurrent readers while updating opp_table */
1282 WARN_ON(!list_empty(&opp_table
->opp_list
));
1284 if (!opp_table
->prop_name
) {
1285 pr_err("%s: Doesn't have a prop-name\n", __func__
);
1289 kfree(opp_table
->prop_name
);
1290 opp_table
->prop_name
= NULL
;
1292 dev_pm_opp_put_opp_table(opp_table
);
1294 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name
);
1296 static int _allocate_set_opp_data(struct opp_table
*opp_table
)
1298 struct dev_pm_set_opp_data
*data
;
1299 int len
, count
= opp_table
->regulator_count
;
1301 if (WARN_ON(!count
))
1304 /* space for set_opp_data */
1305 len
= sizeof(*data
);
1307 /* space for old_opp.supplies and new_opp.supplies */
1308 len
+= 2 * sizeof(struct dev_pm_opp_supply
) * count
;
1310 data
= kzalloc(len
, GFP_KERNEL
);
1314 data
->old_opp
.supplies
= (void *)(data
+ 1);
1315 data
->new_opp
.supplies
= data
->old_opp
.supplies
+ count
;
1317 opp_table
->set_opp_data
= data
;
1322 static void _free_set_opp_data(struct opp_table
*opp_table
)
1324 kfree(opp_table
->set_opp_data
);
1325 opp_table
->set_opp_data
= NULL
;
1329 * dev_pm_opp_set_regulators() - Set regulator names for the device
1330 * @dev: Device for which regulator name is being set.
1331 * @names: Array of pointers to the names of the regulator.
1332 * @count: Number of regulators.
1334 * In order to support OPP switching, OPP layer needs to know the name of the
1335 * device's regulators, as the core would be required to switch voltages as
1338 * This must be called before any OPPs are initialized for the device.
1340 struct opp_table
*dev_pm_opp_set_regulators(struct device
*dev
,
1341 const char * const names
[],
1344 struct opp_table
*opp_table
;
1345 struct regulator
*reg
;
1348 opp_table
= dev_pm_opp_get_opp_table(dev
);
1350 return ERR_PTR(-ENOMEM
);
1353 /* This should be called before OPPs are initialized */
1354 if (WARN_ON(!list_empty(&opp_table
->opp_list
))) {
1360 /* Already have regulators set */
1361 if (opp_table
->regulators
) {
1366 opp_table
->regulators
= kmalloc_array(count
,
1367 sizeof(*opp_table
->regulators
),
1369 if (!opp_table
->regulators
) {
1374 for (i
= 0; i
< count
; i
++) {
1375 reg
= regulator_get_optional(dev
, names
[i
]);
1378 if (ret
!= -EPROBE_DEFER
)
1379 dev_err(dev
, "%s: no regulator (%s) found: %d\n",
1380 __func__
, names
[i
], ret
);
1381 goto free_regulators
;
1384 opp_table
->regulators
[i
] = reg
;
1387 opp_table
->regulator_count
= count
;
1389 /* Allocate block only once to pass to set_opp() routines */
1390 ret
= _allocate_set_opp_data(opp_table
);
1392 goto free_regulators
;
1398 regulator_put(opp_table
->regulators
[--i
]);
1400 kfree(opp_table
->regulators
);
1401 opp_table
->regulators
= NULL
;
1402 opp_table
->regulator_count
= 0;
1404 dev_pm_opp_put_opp_table(opp_table
);
1406 return ERR_PTR(ret
);
1408 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators
);
1411 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1412 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
1414 void dev_pm_opp_put_regulators(struct opp_table
*opp_table
)
1418 if (!opp_table
->regulators
) {
1419 pr_err("%s: Doesn't have regulators set\n", __func__
);
1423 /* Make sure there are no concurrent readers while updating opp_table */
1424 WARN_ON(!list_empty(&opp_table
->opp_list
));
1426 for (i
= opp_table
->regulator_count
- 1; i
>= 0; i
--)
1427 regulator_put(opp_table
->regulators
[i
]);
1429 _free_set_opp_data(opp_table
);
1431 kfree(opp_table
->regulators
);
1432 opp_table
->regulators
= NULL
;
1433 opp_table
->regulator_count
= 0;
1435 dev_pm_opp_put_opp_table(opp_table
);
1437 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators
);
1440 * dev_pm_opp_set_clkname() - Set clk name for the device
1441 * @dev: Device for which clk name is being set.
1444 * In order to support OPP switching, OPP layer needs to get pointer to the
1445 * clock for the device. Simple cases work fine without using this routine (i.e.
1446 * by passing connection-id as NULL), but for a device with multiple clocks
1447 * available, the OPP core needs to know the exact name of the clk to use.
1449 * This must be called before any OPPs are initialized for the device.
1451 struct opp_table
*dev_pm_opp_set_clkname(struct device
*dev
, const char *name
)
1453 struct opp_table
*opp_table
;
1456 opp_table
= dev_pm_opp_get_opp_table(dev
);
1458 return ERR_PTR(-ENOMEM
);
1460 /* This should be called before OPPs are initialized */
1461 if (WARN_ON(!list_empty(&opp_table
->opp_list
))) {
1466 /* Already have default clk set, free it */
1467 if (!IS_ERR(opp_table
->clk
))
1468 clk_put(opp_table
->clk
);
1470 /* Find clk for the device */
1471 opp_table
->clk
= clk_get(dev
, name
);
1472 if (IS_ERR(opp_table
->clk
)) {
1473 ret
= PTR_ERR(opp_table
->clk
);
1474 if (ret
!= -EPROBE_DEFER
) {
1475 dev_err(dev
, "%s: Couldn't find clock: %d\n", __func__
,
1484 dev_pm_opp_put_opp_table(opp_table
);
1486 return ERR_PTR(ret
);
1488 EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname
);
1491 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1492 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1494 void dev_pm_opp_put_clkname(struct opp_table
*opp_table
)
1496 /* Make sure there are no concurrent readers while updating opp_table */
1497 WARN_ON(!list_empty(&opp_table
->opp_list
));
1499 clk_put(opp_table
->clk
);
1500 opp_table
->clk
= ERR_PTR(-EINVAL
);
1502 dev_pm_opp_put_opp_table(opp_table
);
1504 EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname
);
1507 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1508 * @dev: Device for which the helper is getting registered.
1509 * @set_opp: Custom set OPP helper.
1511 * This is useful to support complex platforms (like platforms with multiple
1512 * regulators per device), instead of the generic OPP set rate helper.
1514 * This must be called before any OPPs are initialized for the device.
1516 struct opp_table
*dev_pm_opp_register_set_opp_helper(struct device
*dev
,
1517 int (*set_opp
)(struct dev_pm_set_opp_data
*data
))
1519 struct opp_table
*opp_table
;
1523 return ERR_PTR(-EINVAL
);
1525 opp_table
= dev_pm_opp_get_opp_table(dev
);
1527 return ERR_PTR(-ENOMEM
);
1529 /* This should be called before OPPs are initialized */
1530 if (WARN_ON(!list_empty(&opp_table
->opp_list
))) {
1535 /* Already have custom set_opp helper */
1536 if (WARN_ON(opp_table
->set_opp
)) {
1541 opp_table
->set_opp
= set_opp
;
1546 dev_pm_opp_put_opp_table(opp_table
);
1548 return ERR_PTR(ret
);
1550 EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper
);
1553 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
1555 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
1557 * Release resources blocked for platform specific set_opp helper.
1559 void dev_pm_opp_unregister_set_opp_helper(struct opp_table
*opp_table
)
1561 if (!opp_table
->set_opp
) {
1562 pr_err("%s: Doesn't have custom set_opp helper set\n",
1567 /* Make sure there are no concurrent readers while updating opp_table */
1568 WARN_ON(!list_empty(&opp_table
->opp_list
));
1570 opp_table
->set_opp
= NULL
;
1572 dev_pm_opp_put_opp_table(opp_table
);
1574 EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper
);
1577 * dev_pm_opp_register_get_pstate_helper() - Register get_pstate() helper.
1578 * @dev: Device for which the helper is getting registered.
1579 * @get_pstate: Helper.
1581 * TODO: Remove this callback after the same information is available via Device
1584 * This allows a platform to initialize the performance states of individual
1585 * OPPs for its devices, until we get similar information directly from DT.
1587 * This must be called before the OPPs are initialized for the device.
1589 struct opp_table
*dev_pm_opp_register_get_pstate_helper(struct device
*dev
,
1590 int (*get_pstate
)(struct device
*dev
, unsigned long rate
))
1592 struct opp_table
*opp_table
;
1596 return ERR_PTR(-EINVAL
);
1598 opp_table
= dev_pm_opp_get_opp_table(dev
);
1600 return ERR_PTR(-ENOMEM
);
1602 /* This should be called before OPPs are initialized */
1603 if (WARN_ON(!list_empty(&opp_table
->opp_list
))) {
1608 /* Already have genpd_performance_state set */
1609 if (WARN_ON(opp_table
->genpd_performance_state
)) {
1614 opp_table
->genpd_performance_state
= true;
1615 opp_table
->get_pstate
= get_pstate
;
1620 dev_pm_opp_put_opp_table(opp_table
);
1622 return ERR_PTR(ret
);
1624 EXPORT_SYMBOL_GPL(dev_pm_opp_register_get_pstate_helper
);
1627 * dev_pm_opp_unregister_get_pstate_helper() - Releases resources blocked for
1628 * get_pstate() helper
1629 * @opp_table: OPP table returned from dev_pm_opp_register_get_pstate_helper().
1631 * Release resources blocked for platform specific get_pstate() helper.
1633 void dev_pm_opp_unregister_get_pstate_helper(struct opp_table
*opp_table
)
1635 if (!opp_table
->genpd_performance_state
) {
1636 pr_err("%s: Doesn't have performance states set\n",
1641 /* Make sure there are no concurrent readers while updating opp_table */
1642 WARN_ON(!list_empty(&opp_table
->opp_list
));
1644 opp_table
->genpd_performance_state
= false;
1645 opp_table
->get_pstate
= NULL
;
1647 dev_pm_opp_put_opp_table(opp_table
);
1649 EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_get_pstate_helper
);
1652 * dev_pm_opp_add() - Add an OPP table from a table definitions
1653 * @dev: device for which we do this operation
1654 * @freq: Frequency in Hz for this OPP
1655 * @u_volt: Voltage in uVolts for this OPP
1657 * This function adds an opp definition to the opp table and returns status.
1658 * The opp is made available by default and it can be controlled using
1659 * dev_pm_opp_enable/disable functions.
1663 * Duplicate OPPs (both freq and volt are same) and opp->available
1664 * -EEXIST Freq are same and volt are different OR
1665 * Duplicate OPPs (both freq and volt are same) and !opp->available
1666 * -ENOMEM Memory allocation failure
1668 int dev_pm_opp_add(struct device
*dev
, unsigned long freq
, unsigned long u_volt
)
1670 struct opp_table
*opp_table
;
1673 opp_table
= dev_pm_opp_get_opp_table(dev
);
1677 ret
= _opp_add_v1(opp_table
, dev
, freq
, u_volt
, true);
1679 dev_pm_opp_put_opp_table(opp_table
);
1682 EXPORT_SYMBOL_GPL(dev_pm_opp_add
);
1685 * _opp_set_availability() - helper to set the availability of an opp
1686 * @dev: device for which we do this operation
1687 * @freq: OPP frequency to modify availability
1688 * @availability_req: availability status requested for this opp
1690 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1691 * which is isolated here.
1693 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1694 * copy operation, returns 0 if no modification was done OR modification was
1697 static int _opp_set_availability(struct device
*dev
, unsigned long freq
,
1698 bool availability_req
)
1700 struct opp_table
*opp_table
;
1701 struct dev_pm_opp
*tmp_opp
, *opp
= ERR_PTR(-ENODEV
);
1704 /* Find the opp_table */
1705 opp_table
= _find_opp_table(dev
);
1706 if (IS_ERR(opp_table
)) {
1707 r
= PTR_ERR(opp_table
);
1708 dev_warn(dev
, "%s: Device OPP not found (%d)\n", __func__
, r
);
1712 mutex_lock(&opp_table
->lock
);
1714 /* Do we have the frequency? */
1715 list_for_each_entry(tmp_opp
, &opp_table
->opp_list
, node
) {
1716 if (tmp_opp
->rate
== freq
) {
1727 /* Is update really needed? */
1728 if (opp
->available
== availability_req
)
1731 opp
->available
= availability_req
;
1733 dev_pm_opp_get(opp
);
1734 mutex_unlock(&opp_table
->lock
);
1736 /* Notify the change of the OPP availability */
1737 if (availability_req
)
1738 blocking_notifier_call_chain(&opp_table
->head
, OPP_EVENT_ENABLE
,
1741 blocking_notifier_call_chain(&opp_table
->head
,
1742 OPP_EVENT_DISABLE
, opp
);
1744 dev_pm_opp_put(opp
);
1748 mutex_unlock(&opp_table
->lock
);
1750 dev_pm_opp_put_opp_table(opp_table
);
1755 * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
1756 * @dev: device for which we do this operation
1757 * @freq: OPP frequency to adjust voltage of
1758 * @u_volt: new OPP voltage
1760 * Change the voltage of an OPP with an RCU operation.
1762 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1763 * copy operation, returns 0 if no modifcation was done OR modification was
1766 * Locking: The internal device_opp and opp structures are RCU protected.
1767 * Hence this function internally uses RCU updater strategy with mutex locks to
1768 * keep the integrity of the internal data structures. Callers should ensure
1769 * that this function is *NOT* called under RCU protection or in contexts where
1770 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1772 int dev_pm_opp_adjust_voltage(struct device
*dev
, unsigned long freq
,
1773 unsigned long u_volt
)
1775 struct opp_table
*opp_table
;
1776 struct dev_pm_opp
*tmp_opp
, *opp
= ERR_PTR(-ENODEV
);
1780 mutex_lock(&opp_table_lock
);
1782 /* Find the opp_table */
1783 opp_table
= _find_opp_table_unlocked(dev
);
1784 if (IS_ERR(opp_table
)) {
1785 r
= PTR_ERR(opp_table
);
1786 dev_warn(dev
, "%s: Device OPP not found (%d)\n", __func__
, r
);
1790 /* Do we have the frequency? */
1791 list_for_each_entry(tmp_opp
, &opp_table
->opp_list
, node
) {
1792 if (tmp_opp
->rate
== freq
) {
1802 /* Is update really needed? */
1803 if (opp
->supplies
[0].u_volt
== u_volt
)
1806 /* adjust voltage node */
1807 tol
= u_volt
* opp_table
->voltage_tolerance_v1
/ 100;
1808 opp
->supplies
[0].u_volt
= u_volt
;
1809 opp
->supplies
[0].u_volt_min
= u_volt
- tol
;
1810 opp
->supplies
[0].u_volt_max
= u_volt
+ tol
;
1812 mutex_unlock(&opp_table_lock
);
1814 /* Notify the change of the OPP */
1815 blocking_notifier_call_chain(&opp_table
->head
, OPP_EVENT_ADJUST_VOLTAGE
, opp
);
1820 mutex_unlock(&opp_table_lock
);
1825 * dev_pm_opp_enable() - Enable a specific OPP
1826 * @dev: device for which we do this operation
1827 * @freq: OPP frequency to enable
1829 * Enables a provided opp. If the operation is valid, this returns 0, else the
1830 * corresponding error value. It is meant to be used for users an OPP available
1831 * after being temporarily made unavailable with dev_pm_opp_disable.
1833 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1834 * copy operation, returns 0 if no modification was done OR modification was
1837 int dev_pm_opp_enable(struct device
*dev
, unsigned long freq
)
1839 return _opp_set_availability(dev
, freq
, true);
1841 EXPORT_SYMBOL_GPL(dev_pm_opp_enable
);
1844 * dev_pm_opp_disable() - Disable a specific OPP
1845 * @dev: device for which we do this operation
1846 * @freq: OPP frequency to disable
1848 * Disables a provided opp. If the operation is valid, this returns
1849 * 0, else the corresponding error value. It is meant to be a temporary
1850 * control by users to make this OPP not available until the circumstances are
1851 * right to make it available again (with a call to dev_pm_opp_enable).
1853 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1854 * copy operation, returns 0 if no modification was done OR modification was
1857 int dev_pm_opp_disable(struct device
*dev
, unsigned long freq
)
1859 return _opp_set_availability(dev
, freq
, false);
1861 EXPORT_SYMBOL_GPL(dev_pm_opp_disable
);
1864 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
1865 * @dev: Device for which notifier needs to be registered
1866 * @nb: Notifier block to be registered
1868 * Return: 0 on success or a negative error value.
1870 int dev_pm_opp_register_notifier(struct device
*dev
, struct notifier_block
*nb
)
1872 struct opp_table
*opp_table
;
1875 opp_table
= _find_opp_table(dev
);
1876 if (IS_ERR(opp_table
))
1877 return PTR_ERR(opp_table
);
1879 ret
= blocking_notifier_chain_register(&opp_table
->head
, nb
);
1881 dev_pm_opp_put_opp_table(opp_table
);
1885 EXPORT_SYMBOL(dev_pm_opp_register_notifier
);
1888 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
1889 * @dev: Device for which notifier needs to be unregistered
1890 * @nb: Notifier block to be unregistered
1892 * Return: 0 on success or a negative error value.
1894 int dev_pm_opp_unregister_notifier(struct device
*dev
,
1895 struct notifier_block
*nb
)
1897 struct opp_table
*opp_table
;
1900 opp_table
= _find_opp_table(dev
);
1901 if (IS_ERR(opp_table
))
1902 return PTR_ERR(opp_table
);
1904 ret
= blocking_notifier_chain_unregister(&opp_table
->head
, nb
);
1906 dev_pm_opp_put_opp_table(opp_table
);
1910 EXPORT_SYMBOL(dev_pm_opp_unregister_notifier
);
1913 * Free OPPs either created using static entries present in DT or even the
1914 * dynamically added entries based on remove_all param.
1916 void _dev_pm_opp_remove_table(struct opp_table
*opp_table
, struct device
*dev
,
1919 struct dev_pm_opp
*opp
, *tmp
;
1921 /* Find if opp_table manages a single device */
1922 if (list_is_singular(&opp_table
->dev_list
)) {
1923 /* Free static OPPs */
1924 list_for_each_entry_safe(opp
, tmp
, &opp_table
->opp_list
, node
) {
1925 if (remove_all
|| !opp
->dynamic
)
1926 dev_pm_opp_put(opp
);
1930 * The OPP table is getting removed, drop the performance state
1933 if (opp_table
->genpd_performance_state
)
1934 dev_pm_genpd_set_performance_state(dev
, 0);
1936 _remove_opp_dev(_find_opp_dev(dev
, opp_table
), opp_table
);
1940 void _dev_pm_opp_find_and_remove_table(struct device
*dev
, bool remove_all
)
1942 struct opp_table
*opp_table
;
1944 /* Check for existing table for 'dev' */
1945 opp_table
= _find_opp_table(dev
);
1946 if (IS_ERR(opp_table
)) {
1947 int error
= PTR_ERR(opp_table
);
1949 if (error
!= -ENODEV
)
1950 WARN(1, "%s: opp_table: %d\n",
1951 IS_ERR_OR_NULL(dev
) ?
1952 "Invalid device" : dev_name(dev
),
1957 _dev_pm_opp_remove_table(opp_table
, dev
, remove_all
);
1959 dev_pm_opp_put_opp_table(opp_table
);
1963 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
1964 * @dev: device pointer used to lookup OPP table.
1966 * Free both OPPs created using static entries present in DT and the
1967 * dynamically added entries.
1969 void dev_pm_opp_remove_table(struct device
*dev
)
1971 _dev_pm_opp_find_and_remove_table(dev
, true);
1973 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table
);