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
);
131 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
132 * @opp: opp for which turbo mode is being verified
134 * Turbo OPPs are not for normal use, and can be enabled (under certain
135 * conditions) for short duration of times to finish high throughput work
136 * quickly. Running on them for longer times may overheat the chip.
138 * Return: true if opp is turbo opp, else false.
140 bool dev_pm_opp_is_turbo(struct dev_pm_opp
*opp
)
142 if (IS_ERR_OR_NULL(opp
) || !opp
->available
) {
143 pr_err("%s: Invalid parameters\n", __func__
);
149 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo
);
152 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
153 * @dev: device for which we do this operation
155 * Return: This function returns the max clock latency in nanoseconds.
157 unsigned long dev_pm_opp_get_max_clock_latency(struct device
*dev
)
159 struct opp_table
*opp_table
;
160 unsigned long clock_latency_ns
;
162 opp_table
= _find_opp_table(dev
);
163 if (IS_ERR(opp_table
))
166 clock_latency_ns
= opp_table
->clock_latency_ns_max
;
168 dev_pm_opp_put_opp_table(opp_table
);
170 return clock_latency_ns
;
172 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency
);
175 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
176 * @dev: device for which we do this operation
178 * Return: This function returns the max voltage latency in nanoseconds.
180 unsigned long dev_pm_opp_get_max_volt_latency(struct device
*dev
)
182 struct opp_table
*opp_table
;
183 struct dev_pm_opp
*opp
;
184 struct regulator
*reg
;
185 unsigned long latency_ns
= 0;
192 opp_table
= _find_opp_table(dev
);
193 if (IS_ERR(opp_table
))
196 count
= opp_table
->regulator_count
;
198 /* Regulator may not be required for the device */
202 uV
= kmalloc_array(count
, sizeof(*uV
), GFP_KERNEL
);
206 mutex_lock(&opp_table
->lock
);
208 for (i
= 0; i
< count
; i
++) {
212 list_for_each_entry(opp
, &opp_table
->opp_list
, node
) {
216 if (opp
->supplies
[i
].u_volt_min
< uV
[i
].min
)
217 uV
[i
].min
= opp
->supplies
[i
].u_volt_min
;
218 if (opp
->supplies
[i
].u_volt_max
> uV
[i
].max
)
219 uV
[i
].max
= opp
->supplies
[i
].u_volt_max
;
223 mutex_unlock(&opp_table
->lock
);
226 * The caller needs to ensure that opp_table (and hence the regulator)
227 * isn't freed, while we are executing this routine.
229 for (i
= 0; i
< count
; i
++) {
230 reg
= opp_table
->regulators
[i
];
231 ret
= regulator_set_voltage_time(reg
, uV
[i
].min
, uV
[i
].max
);
233 latency_ns
+= ret
* 1000;
238 dev_pm_opp_put_opp_table(opp_table
);
242 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency
);
245 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
247 * @dev: device for which we do this operation
249 * Return: This function returns the max transition latency, in nanoseconds, to
250 * switch from one OPP to other.
252 unsigned long dev_pm_opp_get_max_transition_latency(struct device
*dev
)
254 return dev_pm_opp_get_max_volt_latency(dev
) +
255 dev_pm_opp_get_max_clock_latency(dev
);
257 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency
);
260 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
261 * @dev: device for which we do this operation
263 * Return: This function returns the frequency of the OPP marked as suspend_opp
264 * if one is available, else returns 0;
266 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device
*dev
)
268 struct opp_table
*opp_table
;
269 unsigned long freq
= 0;
271 opp_table
= _find_opp_table(dev
);
272 if (IS_ERR(opp_table
))
275 if (opp_table
->suspend_opp
&& opp_table
->suspend_opp
->available
)
276 freq
= dev_pm_opp_get_freq(opp_table
->suspend_opp
);
278 dev_pm_opp_put_opp_table(opp_table
);
282 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq
);
285 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
286 * @dev: device for which we do this operation
288 * Return: This function returns the number of available opps if there are any,
289 * else returns 0 if none or the corresponding error value.
291 int dev_pm_opp_get_opp_count(struct device
*dev
)
293 struct opp_table
*opp_table
;
294 struct dev_pm_opp
*temp_opp
;
297 opp_table
= _find_opp_table(dev
);
298 if (IS_ERR(opp_table
)) {
299 count
= PTR_ERR(opp_table
);
300 dev_dbg(dev
, "%s: OPP table not found (%d)\n",
305 mutex_lock(&opp_table
->lock
);
307 list_for_each_entry(temp_opp
, &opp_table
->opp_list
, node
) {
308 if (temp_opp
->available
)
312 mutex_unlock(&opp_table
->lock
);
313 dev_pm_opp_put_opp_table(opp_table
);
317 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count
);
320 * dev_pm_opp_find_freq_exact() - search for an exact frequency
321 * @dev: device for which we do this operation
322 * @freq: frequency to search for
323 * @available: true/false - match for available opp
325 * Return: Searches for exact match in the opp table and returns pointer to the
326 * matching opp if found, else returns ERR_PTR in case of error and should
327 * be handled using IS_ERR. Error return values can be:
328 * EINVAL: for bad pointer
329 * ERANGE: no match found for search
330 * ENODEV: if device not found in list of registered devices
332 * Note: available is a modifier for the search. if available=true, then the
333 * match is for exact matching frequency and is available in the stored OPP
334 * table. if false, the match is for exact frequency which is not available.
336 * This provides a mechanism to enable an opp which is not available currently
337 * or the opposite as well.
339 * The callers are required to call dev_pm_opp_put() for the returned OPP after
342 struct dev_pm_opp
*dev_pm_opp_find_freq_exact(struct device
*dev
,
346 struct opp_table
*opp_table
;
347 struct dev_pm_opp
*temp_opp
, *opp
= ERR_PTR(-ERANGE
);
349 opp_table
= _find_opp_table(dev
);
350 if (IS_ERR(opp_table
)) {
351 int r
= PTR_ERR(opp_table
);
353 dev_err(dev
, "%s: OPP table not found (%d)\n", __func__
, r
);
357 mutex_lock(&opp_table
->lock
);
359 list_for_each_entry(temp_opp
, &opp_table
->opp_list
, node
) {
360 if (temp_opp
->available
== available
&&
361 temp_opp
->rate
== freq
) {
364 /* Increment the reference count of OPP */
370 mutex_unlock(&opp_table
->lock
);
371 dev_pm_opp_put_opp_table(opp_table
);
375 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact
);
377 static noinline
struct dev_pm_opp
*_find_freq_ceil(struct opp_table
*opp_table
,
380 struct dev_pm_opp
*temp_opp
, *opp
= ERR_PTR(-ERANGE
);
382 mutex_lock(&opp_table
->lock
);
384 list_for_each_entry(temp_opp
, &opp_table
->opp_list
, node
) {
385 if (temp_opp
->available
&& temp_opp
->rate
>= *freq
) {
389 /* Increment the reference count of OPP */
395 mutex_unlock(&opp_table
->lock
);
401 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
402 * @dev: device for which we do this operation
403 * @freq: Start frequency
405 * Search for the matching ceil *available* OPP from a starting freq
408 * Return: matching *opp and refreshes *freq accordingly, else returns
409 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
411 * EINVAL: for bad pointer
412 * ERANGE: no match found for search
413 * ENODEV: if device not found in list of registered devices
415 * The callers are required to call dev_pm_opp_put() for the returned OPP after
418 struct dev_pm_opp
*dev_pm_opp_find_freq_ceil(struct device
*dev
,
421 struct opp_table
*opp_table
;
422 struct dev_pm_opp
*opp
;
425 dev_err(dev
, "%s: Invalid argument freq=%p\n", __func__
, freq
);
426 return ERR_PTR(-EINVAL
);
429 opp_table
= _find_opp_table(dev
);
430 if (IS_ERR(opp_table
))
431 return ERR_CAST(opp_table
);
433 opp
= _find_freq_ceil(opp_table
, freq
);
435 dev_pm_opp_put_opp_table(opp_table
);
439 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil
);
442 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
443 * @dev: device for which we do this operation
444 * @freq: Start frequency
446 * Search for the matching floor *available* OPP from a starting freq
449 * Return: matching *opp and refreshes *freq accordingly, else returns
450 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
452 * EINVAL: for bad pointer
453 * ERANGE: no match found for search
454 * ENODEV: if device not found in list of registered devices
456 * The callers are required to call dev_pm_opp_put() for the returned OPP after
459 struct dev_pm_opp
*dev_pm_opp_find_freq_floor(struct device
*dev
,
462 struct opp_table
*opp_table
;
463 struct dev_pm_opp
*temp_opp
, *opp
= ERR_PTR(-ERANGE
);
466 dev_err(dev
, "%s: Invalid argument freq=%p\n", __func__
, freq
);
467 return ERR_PTR(-EINVAL
);
470 opp_table
= _find_opp_table(dev
);
471 if (IS_ERR(opp_table
))
472 return ERR_CAST(opp_table
);
474 mutex_lock(&opp_table
->lock
);
476 list_for_each_entry(temp_opp
, &opp_table
->opp_list
, node
) {
477 if (temp_opp
->available
) {
478 /* go to the next node, before choosing prev */
479 if (temp_opp
->rate
> *freq
)
486 /* Increment the reference count of OPP */
489 mutex_unlock(&opp_table
->lock
);
490 dev_pm_opp_put_opp_table(opp_table
);
497 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor
);
499 static int _set_opp_voltage(struct device
*dev
, struct regulator
*reg
,
500 struct dev_pm_opp_supply
*supply
)
504 /* Regulator not available for device */
506 dev_dbg(dev
, "%s: regulator not available: %ld\n", __func__
,
511 dev_dbg(dev
, "%s: voltages (mV): %lu %lu %lu\n", __func__
,
512 supply
->u_volt_min
, supply
->u_volt
, supply
->u_volt_max
);
514 ret
= regulator_set_voltage_triplet(reg
, supply
->u_volt_min
,
515 supply
->u_volt
, supply
->u_volt_max
);
517 dev_err(dev
, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
518 __func__
, supply
->u_volt_min
, supply
->u_volt
,
519 supply
->u_volt_max
, ret
);
525 _generic_set_opp_clk_only(struct device
*dev
, struct clk
*clk
,
526 unsigned long old_freq
, unsigned long freq
)
530 ret
= clk_set_rate(clk
, freq
);
532 dev_err(dev
, "%s: failed to set clock rate: %d\n", __func__
,
540 _generic_set_opp_domain(struct device
*dev
, struct clk
*clk
,
541 unsigned long old_freq
, unsigned long freq
,
542 unsigned int old_pstate
, unsigned int new_pstate
)
546 /* Scaling up? Scale domain performance state before frequency */
547 if (freq
> old_freq
) {
548 ret
= dev_pm_genpd_set_performance_state(dev
, new_pstate
);
553 ret
= _generic_set_opp_clk_only(dev
, clk
, old_freq
, freq
);
555 goto restore_domain_state
;
557 /* Scaling down? Scale domain performance state after frequency */
558 if (freq
< old_freq
) {
559 ret
= dev_pm_genpd_set_performance_state(dev
, new_pstate
);
567 if (_generic_set_opp_clk_only(dev
, clk
, freq
, old_freq
))
568 dev_err(dev
, "%s: failed to restore old-freq (%lu Hz)\n",
570 restore_domain_state
:
572 dev_pm_genpd_set_performance_state(dev
, old_pstate
);
577 static int _generic_set_opp_regulator(const struct opp_table
*opp_table
,
579 unsigned long old_freq
,
581 struct dev_pm_opp_supply
*old_supply
,
582 struct dev_pm_opp_supply
*new_supply
)
584 struct regulator
*reg
= opp_table
->regulators
[0];
587 /* This function only supports single regulator per device */
588 if (WARN_ON(opp_table
->regulator_count
> 1)) {
589 dev_err(dev
, "multiple regulators are not supported\n");
593 /* Scaling up? Scale voltage before frequency */
594 if (freq
> old_freq
) {
595 ret
= _set_opp_voltage(dev
, reg
, new_supply
);
597 goto restore_voltage
;
600 /* Change frequency */
601 ret
= _generic_set_opp_clk_only(dev
, opp_table
->clk
, old_freq
, freq
);
603 goto restore_voltage
;
605 /* Scaling down? Scale voltage after frequency */
606 if (freq
< old_freq
) {
607 ret
= _set_opp_voltage(dev
, reg
, new_supply
);
615 if (_generic_set_opp_clk_only(dev
, opp_table
->clk
, freq
, old_freq
))
616 dev_err(dev
, "%s: failed to restore old-freq (%lu Hz)\n",
619 /* This shouldn't harm even if the voltages weren't updated earlier */
621 _set_opp_voltage(dev
, reg
, old_supply
);
627 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
628 * @dev: device for which we do this operation
629 * @target_freq: frequency to achieve
631 * This configures the power-supplies and clock source to the levels specified
632 * by the OPP corresponding to the target_freq.
634 int dev_pm_opp_set_rate(struct device
*dev
, unsigned long target_freq
)
636 struct opp_table
*opp_table
;
637 unsigned long freq
, old_freq
;
638 struct dev_pm_opp
*old_opp
, *opp
;
642 if (unlikely(!target_freq
)) {
643 dev_err(dev
, "%s: Invalid target frequency %lu\n", __func__
,
648 opp_table
= _find_opp_table(dev
);
649 if (IS_ERR(opp_table
)) {
650 dev_err(dev
, "%s: device opp doesn't exist\n", __func__
);
651 return PTR_ERR(opp_table
);
654 clk
= opp_table
->clk
;
656 dev_err(dev
, "%s: No clock available for the device\n",
662 freq
= clk_round_rate(clk
, target_freq
);
666 old_freq
= clk_get_rate(clk
);
668 /* Return early if nothing to do */
669 if (old_freq
== freq
) {
670 dev_dbg(dev
, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
676 old_opp
= _find_freq_ceil(opp_table
, &old_freq
);
677 if (IS_ERR(old_opp
)) {
678 dev_err(dev
, "%s: failed to find current OPP for freq %lu (%ld)\n",
679 __func__
, old_freq
, PTR_ERR(old_opp
));
682 opp
= _find_freq_ceil(opp_table
, &freq
);
685 dev_err(dev
, "%s: failed to find OPP for freq %lu (%d)\n",
686 __func__
, freq
, ret
);
690 dev_dbg(dev
, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__
,
693 /* Only frequency scaling */
694 if (!opp_table
->regulators
) {
696 * We don't support devices with both regulator and
697 * domain performance-state for now.
699 if (opp_table
->genpd_performance_state
)
700 ret
= _generic_set_opp_domain(dev
, clk
, old_freq
, freq
,
701 IS_ERR(old_opp
) ? 0 : old_opp
->pstate
,
704 ret
= _generic_set_opp_clk_only(dev
, clk
, old_freq
, freq
);
705 } else if (!opp_table
->set_opp
) {
706 ret
= _generic_set_opp_regulator(opp_table
, dev
, old_freq
, freq
,
707 IS_ERR(old_opp
) ? NULL
: old_opp
->supplies
,
710 struct dev_pm_set_opp_data
*data
;
712 data
= opp_table
->set_opp_data
;
713 data
->regulators
= opp_table
->regulators
;
714 data
->regulator_count
= opp_table
->regulator_count
;
718 data
->old_opp
.rate
= old_freq
;
719 size
= sizeof(*opp
->supplies
) * opp_table
->regulator_count
;
721 memset(data
->old_opp
.supplies
, 0, size
);
723 memcpy(data
->old_opp
.supplies
, old_opp
->supplies
, size
);
725 data
->new_opp
.rate
= freq
;
726 memcpy(data
->new_opp
.supplies
, opp
->supplies
, size
);
728 ret
= opp_table
->set_opp(data
);
733 if (!IS_ERR(old_opp
))
734 dev_pm_opp_put(old_opp
);
736 dev_pm_opp_put_opp_table(opp_table
);
739 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate
);
741 /* OPP-dev Helpers */
742 static void _remove_opp_dev(struct opp_device
*opp_dev
,
743 struct opp_table
*opp_table
)
745 opp_debug_unregister(opp_dev
, opp_table
);
746 list_del(&opp_dev
->node
);
750 struct opp_device
*_add_opp_dev(const struct device
*dev
,
751 struct opp_table
*opp_table
)
753 struct opp_device
*opp_dev
;
756 opp_dev
= kzalloc(sizeof(*opp_dev
), GFP_KERNEL
);
760 /* Initialize opp-dev */
762 list_add(&opp_dev
->node
, &opp_table
->dev_list
);
764 /* Create debugfs entries for the opp_table */
765 ret
= opp_debug_register(opp_dev
, opp_table
);
767 dev_err(dev
, "%s: Failed to register opp debugfs (%d)\n",
773 static struct opp_table
*_allocate_opp_table(struct device
*dev
)
775 struct opp_table
*opp_table
;
776 struct opp_device
*opp_dev
;
780 * Allocate a new OPP table. In the infrequent case where a new
781 * device is needed to be added, we pay this penalty.
783 opp_table
= kzalloc(sizeof(*opp_table
), GFP_KERNEL
);
787 INIT_LIST_HEAD(&opp_table
->dev_list
);
789 opp_dev
= _add_opp_dev(dev
, opp_table
);
795 _of_init_opp_table(opp_table
, dev
);
797 /* Find clk for the device */
798 opp_table
->clk
= clk_get(dev
, NULL
);
799 if (IS_ERR(opp_table
->clk
)) {
800 ret
= PTR_ERR(opp_table
->clk
);
801 if (ret
!= -EPROBE_DEFER
)
802 dev_dbg(dev
, "%s: Couldn't find clock: %d\n", __func__
,
806 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table
->head
);
807 INIT_LIST_HEAD(&opp_table
->opp_list
);
808 mutex_init(&opp_table
->lock
);
809 kref_init(&opp_table
->kref
);
811 /* Secure the device table modification */
812 list_add(&opp_table
->node
, &opp_tables
);
816 void _get_opp_table_kref(struct opp_table
*opp_table
)
818 kref_get(&opp_table
->kref
);
821 struct opp_table
*dev_pm_opp_get_opp_table(struct device
*dev
)
823 struct opp_table
*opp_table
;
825 /* Hold our table modification lock here */
826 mutex_lock(&opp_table_lock
);
828 opp_table
= _find_opp_table_unlocked(dev
);
829 if (!IS_ERR(opp_table
))
832 opp_table
= _allocate_opp_table(dev
);
835 mutex_unlock(&opp_table_lock
);
839 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table
);
841 static void _opp_table_kref_release(struct kref
*kref
)
843 struct opp_table
*opp_table
= container_of(kref
, struct opp_table
, kref
);
844 struct opp_device
*opp_dev
;
847 if (!IS_ERR(opp_table
->clk
))
848 clk_put(opp_table
->clk
);
850 opp_dev
= list_first_entry(&opp_table
->dev_list
, struct opp_device
,
853 _remove_opp_dev(opp_dev
, opp_table
);
855 /* dev_list must be empty now */
856 WARN_ON(!list_empty(&opp_table
->dev_list
));
858 mutex_destroy(&opp_table
->lock
);
859 list_del(&opp_table
->node
);
862 mutex_unlock(&opp_table_lock
);
865 void dev_pm_opp_put_opp_table(struct opp_table
*opp_table
)
867 kref_put_mutex(&opp_table
->kref
, _opp_table_kref_release
,
870 EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table
);
872 void _opp_free(struct dev_pm_opp
*opp
)
877 static void _opp_kref_release(struct kref
*kref
)
879 struct dev_pm_opp
*opp
= container_of(kref
, struct dev_pm_opp
, kref
);
880 struct opp_table
*opp_table
= opp
->opp_table
;
883 * Notify the changes in the availability of the operable
884 * frequency/voltage list.
886 blocking_notifier_call_chain(&opp_table
->head
, OPP_EVENT_REMOVE
, opp
);
887 opp_debug_remove_one(opp
);
888 list_del(&opp
->node
);
891 mutex_unlock(&opp_table
->lock
);
892 dev_pm_opp_put_opp_table(opp_table
);
895 static void dev_pm_opp_get(struct dev_pm_opp
*opp
)
897 kref_get(&opp
->kref
);
900 void dev_pm_opp_put(struct dev_pm_opp
*opp
)
902 kref_put_mutex(&opp
->kref
, _opp_kref_release
, &opp
->opp_table
->lock
);
904 EXPORT_SYMBOL_GPL(dev_pm_opp_put
);
907 * dev_pm_opp_remove() - Remove an OPP from OPP table
908 * @dev: device for which we do this operation
909 * @freq: OPP to remove with matching 'freq'
911 * This function removes an opp from the opp table.
913 void dev_pm_opp_remove(struct device
*dev
, unsigned long freq
)
915 struct dev_pm_opp
*opp
;
916 struct opp_table
*opp_table
;
919 opp_table
= _find_opp_table(dev
);
920 if (IS_ERR(opp_table
))
923 mutex_lock(&opp_table
->lock
);
925 list_for_each_entry(opp
, &opp_table
->opp_list
, node
) {
926 if (opp
->rate
== freq
) {
932 mutex_unlock(&opp_table
->lock
);
937 dev_warn(dev
, "%s: Couldn't find OPP with freq: %lu\n",
941 dev_pm_opp_put_opp_table(opp_table
);
943 EXPORT_SYMBOL_GPL(dev_pm_opp_remove
);
945 struct dev_pm_opp
*_opp_allocate(struct opp_table
*table
)
947 struct dev_pm_opp
*opp
;
948 int count
, supply_size
;
950 /* Allocate space for at least one supply */
951 count
= table
->regulator_count
? table
->regulator_count
: 1;
952 supply_size
= sizeof(*opp
->supplies
) * count
;
954 /* allocate new OPP node and supplies structures */
955 opp
= kzalloc(sizeof(*opp
) + supply_size
, GFP_KERNEL
);
959 /* Put the supplies at the end of the OPP structure as an empty array */
960 opp
->supplies
= (struct dev_pm_opp_supply
*)(opp
+ 1);
961 INIT_LIST_HEAD(&opp
->node
);
966 static bool _opp_supported_by_regulators(struct dev_pm_opp
*opp
,
967 struct opp_table
*opp_table
)
969 struct regulator
*reg
;
972 for (i
= 0; i
< opp_table
->regulator_count
; i
++) {
973 reg
= opp_table
->regulators
[i
];
975 if (!regulator_is_supported_voltage(reg
,
976 opp
->supplies
[i
].u_volt_min
,
977 opp
->supplies
[i
].u_volt_max
)) {
978 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
979 __func__
, opp
->supplies
[i
].u_volt_min
,
980 opp
->supplies
[i
].u_volt_max
);
990 * 0: On success. And appropriate error message for duplicate OPPs.
991 * -EBUSY: For OPP with same freq/volt and is available. The callers of
992 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
993 * sure we don't print error messages unnecessarily if different parts of
994 * kernel try to initialize the OPP table.
995 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
996 * should be considered an error by the callers of _opp_add().
998 int _opp_add(struct device
*dev
, struct dev_pm_opp
*new_opp
,
999 struct opp_table
*opp_table
)
1001 struct dev_pm_opp
*opp
;
1002 struct list_head
*head
;
1006 * Insert new OPP in order of increasing frequency and discard if
1009 * Need to use &opp_table->opp_list in the condition part of the 'for'
1010 * loop, don't replace it with head otherwise it will become an infinite
1013 mutex_lock(&opp_table
->lock
);
1014 head
= &opp_table
->opp_list
;
1016 list_for_each_entry(opp
, &opp_table
->opp_list
, node
) {
1017 if (new_opp
->rate
> opp
->rate
) {
1022 if (new_opp
->rate
< opp
->rate
)
1025 /* Duplicate OPPs */
1026 dev_warn(dev
, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1027 __func__
, opp
->rate
, opp
->supplies
[0].u_volt
,
1028 opp
->available
, new_opp
->rate
,
1029 new_opp
->supplies
[0].u_volt
, new_opp
->available
);
1031 /* Should we compare voltages for all regulators here ? */
1032 ret
= opp
->available
&&
1033 new_opp
->supplies
[0].u_volt
== opp
->supplies
[0].u_volt
? -EBUSY
: -EEXIST
;
1035 mutex_unlock(&opp_table
->lock
);
1039 if (opp_table
->get_pstate
)
1040 new_opp
->pstate
= opp_table
->get_pstate(dev
, new_opp
->rate
);
1042 list_add(&new_opp
->node
, head
);
1043 mutex_unlock(&opp_table
->lock
);
1045 new_opp
->opp_table
= opp_table
;
1046 kref_init(&new_opp
->kref
);
1048 /* Get a reference to the OPP table */
1049 _get_opp_table_kref(opp_table
);
1051 ret
= opp_debug_create_one(new_opp
, opp_table
);
1053 dev_err(dev
, "%s: Failed to register opp to debugfs (%d)\n",
1056 if (!_opp_supported_by_regulators(new_opp
, opp_table
)) {
1057 new_opp
->available
= false;
1058 dev_warn(dev
, "%s: OPP not supported by regulators (%lu)\n",
1059 __func__
, new_opp
->rate
);
1066 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
1067 * @opp_table: OPP table
1068 * @dev: device for which we do this operation
1069 * @freq: Frequency in Hz for this OPP
1070 * @u_volt: Voltage in uVolts for this OPP
1071 * @dynamic: Dynamically added OPPs.
1073 * This function adds an opp definition to the opp table and returns status.
1074 * The opp is made available by default and it can be controlled using
1075 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1077 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1078 * and freed by dev_pm_opp_of_remove_table.
1082 * Duplicate OPPs (both freq and volt are same) and opp->available
1083 * -EEXIST Freq are same and volt are different OR
1084 * Duplicate OPPs (both freq and volt are same) and !opp->available
1085 * -ENOMEM Memory allocation failure
1087 int _opp_add_v1(struct opp_table
*opp_table
, struct device
*dev
,
1088 unsigned long freq
, long u_volt
, bool dynamic
)
1090 struct dev_pm_opp
*new_opp
;
1094 new_opp
= _opp_allocate(opp_table
);
1098 /* populate the opp table */
1099 new_opp
->rate
= freq
;
1100 tol
= u_volt
* opp_table
->voltage_tolerance_v1
/ 100;
1101 new_opp
->supplies
[0].u_volt
= u_volt
;
1102 new_opp
->supplies
[0].u_volt_min
= u_volt
- tol
;
1103 new_opp
->supplies
[0].u_volt_max
= u_volt
+ tol
;
1104 new_opp
->available
= true;
1105 new_opp
->dynamic
= dynamic
;
1107 ret
= _opp_add(dev
, new_opp
, opp_table
);
1109 /* Don't return error for duplicate OPPs */
1116 * Notify the changes in the availability of the operable
1117 * frequency/voltage list.
1119 blocking_notifier_call_chain(&opp_table
->head
, OPP_EVENT_ADD
, new_opp
);
1129 * dev_pm_opp_set_supported_hw() - Set supported platforms
1130 * @dev: Device for which supported-hw has to be set.
1131 * @versions: Array of hierarchy of versions to match.
1132 * @count: Number of elements in the array.
1134 * This is required only for the V2 bindings, and it enables a platform to
1135 * specify the hierarchy of versions it supports. OPP layer will then enable
1136 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1139 struct opp_table
*dev_pm_opp_set_supported_hw(struct device
*dev
,
1140 const u32
*versions
, unsigned int count
)
1142 struct opp_table
*opp_table
;
1145 opp_table
= dev_pm_opp_get_opp_table(dev
);
1147 return ERR_PTR(-ENOMEM
);
1149 /* Make sure there are no concurrent readers while updating opp_table */
1150 WARN_ON(!list_empty(&opp_table
->opp_list
));
1152 /* Do we already have a version hierarchy associated with opp_table? */
1153 if (opp_table
->supported_hw
) {
1154 dev_err(dev
, "%s: Already have supported hardware list\n",
1160 opp_table
->supported_hw
= kmemdup(versions
, count
* sizeof(*versions
),
1162 if (!opp_table
->supported_hw
) {
1167 opp_table
->supported_hw_count
= count
;
1172 dev_pm_opp_put_opp_table(opp_table
);
1174 return ERR_PTR(ret
);
1176 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw
);
1179 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
1180 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
1182 * This is required only for the V2 bindings, and is called for a matching
1183 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
1184 * will not be freed.
1186 void dev_pm_opp_put_supported_hw(struct opp_table
*opp_table
)
1188 /* Make sure there are no concurrent readers while updating opp_table */
1189 WARN_ON(!list_empty(&opp_table
->opp_list
));
1191 if (!opp_table
->supported_hw
) {
1192 pr_err("%s: Doesn't have supported hardware list\n",
1197 kfree(opp_table
->supported_hw
);
1198 opp_table
->supported_hw
= NULL
;
1199 opp_table
->supported_hw_count
= 0;
1201 dev_pm_opp_put_opp_table(opp_table
);
1203 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw
);
1206 * dev_pm_opp_set_prop_name() - Set prop-extn name
1207 * @dev: Device for which the prop-name has to be set.
1208 * @name: name to postfix to properties.
1210 * This is required only for the V2 bindings, and it enables a platform to
1211 * specify the extn to be used for certain property names. The properties to
1212 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1213 * should postfix the property name with -<name> while looking for them.
1215 struct opp_table
*dev_pm_opp_set_prop_name(struct device
*dev
, const char *name
)
1217 struct opp_table
*opp_table
;
1220 opp_table
= dev_pm_opp_get_opp_table(dev
);
1222 return ERR_PTR(-ENOMEM
);
1224 /* Make sure there are no concurrent readers while updating opp_table */
1225 WARN_ON(!list_empty(&opp_table
->opp_list
));
1227 /* Do we already have a prop-name associated with opp_table? */
1228 if (opp_table
->prop_name
) {
1229 dev_err(dev
, "%s: Already have prop-name %s\n", __func__
,
1230 opp_table
->prop_name
);
1235 opp_table
->prop_name
= kstrdup(name
, GFP_KERNEL
);
1236 if (!opp_table
->prop_name
) {
1244 dev_pm_opp_put_opp_table(opp_table
);
1246 return ERR_PTR(ret
);
1248 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name
);
1251 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
1252 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
1254 * This is required only for the V2 bindings, and is called for a matching
1255 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
1256 * will not be freed.
1258 void dev_pm_opp_put_prop_name(struct opp_table
*opp_table
)
1260 /* Make sure there are no concurrent readers while updating opp_table */
1261 WARN_ON(!list_empty(&opp_table
->opp_list
));
1263 if (!opp_table
->prop_name
) {
1264 pr_err("%s: Doesn't have a prop-name\n", __func__
);
1268 kfree(opp_table
->prop_name
);
1269 opp_table
->prop_name
= NULL
;
1271 dev_pm_opp_put_opp_table(opp_table
);
1273 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name
);
1275 static int _allocate_set_opp_data(struct opp_table
*opp_table
)
1277 struct dev_pm_set_opp_data
*data
;
1278 int len
, count
= opp_table
->regulator_count
;
1280 if (WARN_ON(!count
))
1283 /* space for set_opp_data */
1284 len
= sizeof(*data
);
1286 /* space for old_opp.supplies and new_opp.supplies */
1287 len
+= 2 * sizeof(struct dev_pm_opp_supply
) * count
;
1289 data
= kzalloc(len
, GFP_KERNEL
);
1293 data
->old_opp
.supplies
= (void *)(data
+ 1);
1294 data
->new_opp
.supplies
= data
->old_opp
.supplies
+ count
;
1296 opp_table
->set_opp_data
= data
;
1301 static void _free_set_opp_data(struct opp_table
*opp_table
)
1303 kfree(opp_table
->set_opp_data
);
1304 opp_table
->set_opp_data
= NULL
;
1308 * dev_pm_opp_set_regulators() - Set regulator names for the device
1309 * @dev: Device for which regulator name is being set.
1310 * @names: Array of pointers to the names of the regulator.
1311 * @count: Number of regulators.
1313 * In order to support OPP switching, OPP layer needs to know the name of the
1314 * device's regulators, as the core would be required to switch voltages as
1317 * This must be called before any OPPs are initialized for the device.
1319 struct opp_table
*dev_pm_opp_set_regulators(struct device
*dev
,
1320 const char * const names
[],
1323 struct opp_table
*opp_table
;
1324 struct regulator
*reg
;
1327 opp_table
= dev_pm_opp_get_opp_table(dev
);
1329 return ERR_PTR(-ENOMEM
);
1331 /* This should be called before OPPs are initialized */
1332 if (WARN_ON(!list_empty(&opp_table
->opp_list
))) {
1337 /* Already have regulators set */
1338 if (opp_table
->regulators
) {
1343 opp_table
->regulators
= kmalloc_array(count
,
1344 sizeof(*opp_table
->regulators
),
1346 if (!opp_table
->regulators
) {
1351 for (i
= 0; i
< count
; i
++) {
1352 reg
= regulator_get_optional(dev
, names
[i
]);
1355 if (ret
!= -EPROBE_DEFER
)
1356 dev_err(dev
, "%s: no regulator (%s) found: %d\n",
1357 __func__
, names
[i
], ret
);
1358 goto free_regulators
;
1361 opp_table
->regulators
[i
] = reg
;
1364 opp_table
->regulator_count
= count
;
1366 /* Allocate block only once to pass to set_opp() routines */
1367 ret
= _allocate_set_opp_data(opp_table
);
1369 goto free_regulators
;
1375 regulator_put(opp_table
->regulators
[--i
]);
1377 kfree(opp_table
->regulators
);
1378 opp_table
->regulators
= NULL
;
1379 opp_table
->regulator_count
= 0;
1381 dev_pm_opp_put_opp_table(opp_table
);
1383 return ERR_PTR(ret
);
1385 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators
);
1388 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1389 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
1391 void dev_pm_opp_put_regulators(struct opp_table
*opp_table
)
1395 if (!opp_table
->regulators
) {
1396 pr_err("%s: Doesn't have regulators set\n", __func__
);
1400 /* Make sure there are no concurrent readers while updating opp_table */
1401 WARN_ON(!list_empty(&opp_table
->opp_list
));
1403 for (i
= opp_table
->regulator_count
- 1; i
>= 0; i
--)
1404 regulator_put(opp_table
->regulators
[i
]);
1406 _free_set_opp_data(opp_table
);
1408 kfree(opp_table
->regulators
);
1409 opp_table
->regulators
= NULL
;
1410 opp_table
->regulator_count
= 0;
1412 dev_pm_opp_put_opp_table(opp_table
);
1414 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators
);
1417 * dev_pm_opp_set_clkname() - Set clk name for the device
1418 * @dev: Device for which clk name is being set.
1421 * In order to support OPP switching, OPP layer needs to get pointer to the
1422 * clock for the device. Simple cases work fine without using this routine (i.e.
1423 * by passing connection-id as NULL), but for a device with multiple clocks
1424 * available, the OPP core needs to know the exact name of the clk to use.
1426 * This must be called before any OPPs are initialized for the device.
1428 struct opp_table
*dev_pm_opp_set_clkname(struct device
*dev
, const char *name
)
1430 struct opp_table
*opp_table
;
1433 opp_table
= dev_pm_opp_get_opp_table(dev
);
1435 return ERR_PTR(-ENOMEM
);
1437 /* This should be called before OPPs are initialized */
1438 if (WARN_ON(!list_empty(&opp_table
->opp_list
))) {
1443 /* Already have default clk set, free it */
1444 if (!IS_ERR(opp_table
->clk
))
1445 clk_put(opp_table
->clk
);
1447 /* Find clk for the device */
1448 opp_table
->clk
= clk_get(dev
, name
);
1449 if (IS_ERR(opp_table
->clk
)) {
1450 ret
= PTR_ERR(opp_table
->clk
);
1451 if (ret
!= -EPROBE_DEFER
) {
1452 dev_err(dev
, "%s: Couldn't find clock: %d\n", __func__
,
1461 dev_pm_opp_put_opp_table(opp_table
);
1463 return ERR_PTR(ret
);
1465 EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname
);
1468 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1469 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1471 void dev_pm_opp_put_clkname(struct opp_table
*opp_table
)
1473 /* Make sure there are no concurrent readers while updating opp_table */
1474 WARN_ON(!list_empty(&opp_table
->opp_list
));
1476 clk_put(opp_table
->clk
);
1477 opp_table
->clk
= ERR_PTR(-EINVAL
);
1479 dev_pm_opp_put_opp_table(opp_table
);
1481 EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname
);
1484 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1485 * @dev: Device for which the helper is getting registered.
1486 * @set_opp: Custom set OPP helper.
1488 * This is useful to support complex platforms (like platforms with multiple
1489 * regulators per device), instead of the generic OPP set rate helper.
1491 * This must be called before any OPPs are initialized for the device.
1493 struct opp_table
*dev_pm_opp_register_set_opp_helper(struct device
*dev
,
1494 int (*set_opp
)(struct dev_pm_set_opp_data
*data
))
1496 struct opp_table
*opp_table
;
1500 return ERR_PTR(-EINVAL
);
1502 opp_table
= dev_pm_opp_get_opp_table(dev
);
1504 return ERR_PTR(-ENOMEM
);
1506 /* This should be called before OPPs are initialized */
1507 if (WARN_ON(!list_empty(&opp_table
->opp_list
))) {
1512 /* Already have custom set_opp helper */
1513 if (WARN_ON(opp_table
->set_opp
)) {
1518 opp_table
->set_opp
= set_opp
;
1523 dev_pm_opp_put_opp_table(opp_table
);
1525 return ERR_PTR(ret
);
1527 EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper
);
1530 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
1532 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
1534 * Release resources blocked for platform specific set_opp helper.
1536 void dev_pm_opp_unregister_set_opp_helper(struct opp_table
*opp_table
)
1538 if (!opp_table
->set_opp
) {
1539 pr_err("%s: Doesn't have custom set_opp helper set\n",
1544 /* Make sure there are no concurrent readers while updating opp_table */
1545 WARN_ON(!list_empty(&opp_table
->opp_list
));
1547 opp_table
->set_opp
= NULL
;
1549 dev_pm_opp_put_opp_table(opp_table
);
1551 EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper
);
1554 * dev_pm_opp_register_get_pstate_helper() - Register get_pstate() helper.
1555 * @dev: Device for which the helper is getting registered.
1556 * @get_pstate: Helper.
1558 * TODO: Remove this callback after the same information is available via Device
1561 * This allows a platform to initialize the performance states of individual
1562 * OPPs for its devices, until we get similar information directly from DT.
1564 * This must be called before the OPPs are initialized for the device.
1566 struct opp_table
*dev_pm_opp_register_get_pstate_helper(struct device
*dev
,
1567 int (*get_pstate
)(struct device
*dev
, unsigned long rate
))
1569 struct opp_table
*opp_table
;
1573 return ERR_PTR(-EINVAL
);
1575 opp_table
= dev_pm_opp_get_opp_table(dev
);
1577 return ERR_PTR(-ENOMEM
);
1579 /* This should be called before OPPs are initialized */
1580 if (WARN_ON(!list_empty(&opp_table
->opp_list
))) {
1585 /* Already have genpd_performance_state set */
1586 if (WARN_ON(opp_table
->genpd_performance_state
)) {
1591 opp_table
->genpd_performance_state
= true;
1592 opp_table
->get_pstate
= get_pstate
;
1597 dev_pm_opp_put_opp_table(opp_table
);
1599 return ERR_PTR(ret
);
1601 EXPORT_SYMBOL_GPL(dev_pm_opp_register_get_pstate_helper
);
1604 * dev_pm_opp_unregister_get_pstate_helper() - Releases resources blocked for
1605 * get_pstate() helper
1606 * @opp_table: OPP table returned from dev_pm_opp_register_get_pstate_helper().
1608 * Release resources blocked for platform specific get_pstate() helper.
1610 void dev_pm_opp_unregister_get_pstate_helper(struct opp_table
*opp_table
)
1612 if (!opp_table
->genpd_performance_state
) {
1613 pr_err("%s: Doesn't have performance states set\n",
1618 /* Make sure there are no concurrent readers while updating opp_table */
1619 WARN_ON(!list_empty(&opp_table
->opp_list
));
1621 opp_table
->genpd_performance_state
= false;
1622 opp_table
->get_pstate
= NULL
;
1624 dev_pm_opp_put_opp_table(opp_table
);
1626 EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_get_pstate_helper
);
1629 * dev_pm_opp_add() - Add an OPP table from a table definitions
1630 * @dev: device for which we do this operation
1631 * @freq: Frequency in Hz for this OPP
1632 * @u_volt: Voltage in uVolts for this OPP
1634 * This function adds an opp definition to the opp table and returns status.
1635 * The opp is made available by default and it can be controlled using
1636 * dev_pm_opp_enable/disable functions.
1640 * Duplicate OPPs (both freq and volt are same) and opp->available
1641 * -EEXIST Freq are same and volt are different OR
1642 * Duplicate OPPs (both freq and volt are same) and !opp->available
1643 * -ENOMEM Memory allocation failure
1645 int dev_pm_opp_add(struct device
*dev
, unsigned long freq
, unsigned long u_volt
)
1647 struct opp_table
*opp_table
;
1650 opp_table
= dev_pm_opp_get_opp_table(dev
);
1654 ret
= _opp_add_v1(opp_table
, dev
, freq
, u_volt
, true);
1656 dev_pm_opp_put_opp_table(opp_table
);
1659 EXPORT_SYMBOL_GPL(dev_pm_opp_add
);
1662 * _opp_set_availability() - helper to set the availability of an opp
1663 * @dev: device for which we do this operation
1664 * @freq: OPP frequency to modify availability
1665 * @availability_req: availability status requested for this opp
1667 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1668 * which is isolated here.
1670 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1671 * copy operation, returns 0 if no modification was done OR modification was
1674 static int _opp_set_availability(struct device
*dev
, unsigned long freq
,
1675 bool availability_req
)
1677 struct opp_table
*opp_table
;
1678 struct dev_pm_opp
*tmp_opp
, *opp
= ERR_PTR(-ENODEV
);
1681 /* Find the opp_table */
1682 opp_table
= _find_opp_table(dev
);
1683 if (IS_ERR(opp_table
)) {
1684 r
= PTR_ERR(opp_table
);
1685 dev_warn(dev
, "%s: Device OPP not found (%d)\n", __func__
, r
);
1689 mutex_lock(&opp_table
->lock
);
1691 /* Do we have the frequency? */
1692 list_for_each_entry(tmp_opp
, &opp_table
->opp_list
, node
) {
1693 if (tmp_opp
->rate
== freq
) {
1704 /* Is update really needed? */
1705 if (opp
->available
== availability_req
)
1708 opp
->available
= availability_req
;
1710 dev_pm_opp_get(opp
);
1711 mutex_unlock(&opp_table
->lock
);
1713 /* Notify the change of the OPP availability */
1714 if (availability_req
)
1715 blocking_notifier_call_chain(&opp_table
->head
, OPP_EVENT_ENABLE
,
1718 blocking_notifier_call_chain(&opp_table
->head
,
1719 OPP_EVENT_DISABLE
, opp
);
1721 dev_pm_opp_put(opp
);
1725 mutex_unlock(&opp_table
->lock
);
1727 dev_pm_opp_put_opp_table(opp_table
);
1732 * dev_pm_opp_enable() - Enable a specific OPP
1733 * @dev: device for which we do this operation
1734 * @freq: OPP frequency to enable
1736 * Enables a provided opp. If the operation is valid, this returns 0, else the
1737 * corresponding error value. It is meant to be used for users an OPP available
1738 * after being temporarily made unavailable with dev_pm_opp_disable.
1740 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1741 * copy operation, returns 0 if no modification was done OR modification was
1744 int dev_pm_opp_enable(struct device
*dev
, unsigned long freq
)
1746 return _opp_set_availability(dev
, freq
, true);
1748 EXPORT_SYMBOL_GPL(dev_pm_opp_enable
);
1751 * dev_pm_opp_disable() - Disable a specific OPP
1752 * @dev: device for which we do this operation
1753 * @freq: OPP frequency to disable
1755 * Disables a provided opp. If the operation is valid, this returns
1756 * 0, else the corresponding error value. It is meant to be a temporary
1757 * control by users to make this OPP not available until the circumstances are
1758 * right to make it available again (with a call to dev_pm_opp_enable).
1760 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1761 * copy operation, returns 0 if no modification was done OR modification was
1764 int dev_pm_opp_disable(struct device
*dev
, unsigned long freq
)
1766 return _opp_set_availability(dev
, freq
, false);
1768 EXPORT_SYMBOL_GPL(dev_pm_opp_disable
);
1771 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
1772 * @dev: Device for which notifier needs to be registered
1773 * @nb: Notifier block to be registered
1775 * Return: 0 on success or a negative error value.
1777 int dev_pm_opp_register_notifier(struct device
*dev
, struct notifier_block
*nb
)
1779 struct opp_table
*opp_table
;
1782 opp_table
= _find_opp_table(dev
);
1783 if (IS_ERR(opp_table
))
1784 return PTR_ERR(opp_table
);
1786 ret
= blocking_notifier_chain_register(&opp_table
->head
, nb
);
1788 dev_pm_opp_put_opp_table(opp_table
);
1792 EXPORT_SYMBOL(dev_pm_opp_register_notifier
);
1795 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
1796 * @dev: Device for which notifier needs to be unregistered
1797 * @nb: Notifier block to be unregistered
1799 * Return: 0 on success or a negative error value.
1801 int dev_pm_opp_unregister_notifier(struct device
*dev
,
1802 struct notifier_block
*nb
)
1804 struct opp_table
*opp_table
;
1807 opp_table
= _find_opp_table(dev
);
1808 if (IS_ERR(opp_table
))
1809 return PTR_ERR(opp_table
);
1811 ret
= blocking_notifier_chain_unregister(&opp_table
->head
, nb
);
1813 dev_pm_opp_put_opp_table(opp_table
);
1817 EXPORT_SYMBOL(dev_pm_opp_unregister_notifier
);
1820 * Free OPPs either created using static entries present in DT or even the
1821 * dynamically added entries based on remove_all param.
1823 void _dev_pm_opp_remove_table(struct opp_table
*opp_table
, struct device
*dev
,
1826 struct dev_pm_opp
*opp
, *tmp
;
1828 /* Find if opp_table manages a single device */
1829 if (list_is_singular(&opp_table
->dev_list
)) {
1830 /* Free static OPPs */
1831 list_for_each_entry_safe(opp
, tmp
, &opp_table
->opp_list
, node
) {
1832 if (remove_all
|| !opp
->dynamic
)
1833 dev_pm_opp_put(opp
);
1837 * The OPP table is getting removed, drop the performance state
1840 if (opp_table
->genpd_performance_state
)
1841 dev_pm_genpd_set_performance_state(dev
, 0);
1843 _remove_opp_dev(_find_opp_dev(dev
, opp_table
), opp_table
);
1847 void _dev_pm_opp_find_and_remove_table(struct device
*dev
, bool remove_all
)
1849 struct opp_table
*opp_table
;
1851 /* Check for existing table for 'dev' */
1852 opp_table
= _find_opp_table(dev
);
1853 if (IS_ERR(opp_table
)) {
1854 int error
= PTR_ERR(opp_table
);
1856 if (error
!= -ENODEV
)
1857 WARN(1, "%s: opp_table: %d\n",
1858 IS_ERR_OR_NULL(dev
) ?
1859 "Invalid device" : dev_name(dev
),
1864 _dev_pm_opp_remove_table(opp_table
, dev
, remove_all
);
1866 dev_pm_opp_put_opp_table(opp_table
);
1870 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
1871 * @dev: device pointer used to lookup OPP table.
1873 * Free both OPPs created using static entries present in DT and the
1874 * dynamically added entries.
1876 void dev_pm_opp_remove_table(struct device
*dev
)
1878 _dev_pm_opp_find_and_remove_table(dev
, true);
1880 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table
);