1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic OPP Interface
5 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/clk.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/device.h>
18 #include <linux/export.h>
19 #include <linux/pm_domain.h>
20 #include <linux/regulator/consumer.h>
25 * The root of the list of all opp-tables. All opp_table structures branch off
26 * from here, with each opp_table containing the list of opps it supports in
27 * various states of availability.
29 LIST_HEAD(opp_tables);
31 /* OPP tables with uninitialized required OPPs */
32 LIST_HEAD(lazy_opp_tables);
34 /* Lock to allow exclusive modification to the device and opp lists */
35 DEFINE_MUTEX(opp_table_lock);
36 /* Flag indicating that opp_tables list is being updated at the moment */
37 static bool opp_tables_busy;
39 static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table)
41 struct opp_device *opp_dev;
44 mutex_lock(&opp_table->lock);
45 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
46 if (opp_dev->dev == dev) {
51 mutex_unlock(&opp_table->lock);
55 static struct opp_table *_find_opp_table_unlocked(struct device *dev)
57 struct opp_table *opp_table;
59 list_for_each_entry(opp_table, &opp_tables, node) {
60 if (_find_opp_dev(dev, opp_table)) {
61 _get_opp_table_kref(opp_table);
66 return ERR_PTR(-ENODEV);
70 * _find_opp_table() - find opp_table struct using device pointer
71 * @dev: device pointer used to lookup OPP table
73 * Search OPP table for one containing matching device.
75 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
76 * -EINVAL based on type of error.
78 * The callers must call dev_pm_opp_put_opp_table() after the table is used.
80 struct opp_table *_find_opp_table(struct device *dev)
82 struct opp_table *opp_table;
84 if (IS_ERR_OR_NULL(dev)) {
85 pr_err("%s: Invalid parameters\n", __func__);
86 return ERR_PTR(-EINVAL);
89 mutex_lock(&opp_table_lock);
90 opp_table = _find_opp_table_unlocked(dev);
91 mutex_unlock(&opp_table_lock);
97 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
98 * @opp: opp for which voltage has to be returned for
100 * Return: voltage in micro volt corresponding to the opp, else
103 * This is useful only for devices with single power supply.
105 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
107 if (IS_ERR_OR_NULL(opp)) {
108 pr_err("%s: Invalid parameters\n", __func__);
112 return opp->supplies[0].u_volt;
114 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
117 * dev_pm_opp_get_power() - Gets the power corresponding to an opp
118 * @opp: opp for which power has to be returned for
120 * Return: power in micro watt corresponding to the opp, else
123 * This is useful only for devices with single power supply.
125 unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp)
127 unsigned long opp_power = 0;
130 if (IS_ERR_OR_NULL(opp)) {
131 pr_err("%s: Invalid parameters\n", __func__);
134 for (i = 0; i < opp->opp_table->regulator_count; i++)
135 opp_power += opp->supplies[i].u_watt;
139 EXPORT_SYMBOL_GPL(dev_pm_opp_get_power);
142 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
143 * @opp: opp for which frequency has to be returned for
145 * Return: frequency in hertz corresponding to the opp, else
148 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
150 if (IS_ERR_OR_NULL(opp)) {
151 pr_err("%s: Invalid parameters\n", __func__);
157 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
160 * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
161 * @opp: opp for which level value has to be returned for
163 * Return: level read from device tree corresponding to the opp, else
166 unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
168 if (IS_ERR_OR_NULL(opp) || !opp->available) {
169 pr_err("%s: Invalid parameters\n", __func__);
175 EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
178 * dev_pm_opp_get_required_pstate() - Gets the required performance state
179 * corresponding to an available opp
180 * @opp: opp for which performance state has to be returned for
181 * @index: index of the required opp
183 * Return: performance state read from device tree corresponding to the
184 * required opp, else return 0.
186 unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
189 if (IS_ERR_OR_NULL(opp) || !opp->available ||
190 index >= opp->opp_table->required_opp_count) {
191 pr_err("%s: Invalid parameters\n", __func__);
195 /* required-opps not fully initialized yet */
196 if (lazy_linking_pending(opp->opp_table))
199 return opp->required_opps[index]->pstate;
201 EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate);
204 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
205 * @opp: opp for which turbo mode is being verified
207 * Turbo OPPs are not for normal use, and can be enabled (under certain
208 * conditions) for short duration of times to finish high throughput work
209 * quickly. Running on them for longer times may overheat the chip.
211 * Return: true if opp is turbo opp, else false.
213 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
215 if (IS_ERR_OR_NULL(opp) || !opp->available) {
216 pr_err("%s: Invalid parameters\n", __func__);
222 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
225 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
226 * @dev: device for which we do this operation
228 * Return: This function returns the max clock latency in nanoseconds.
230 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
232 struct opp_table *opp_table;
233 unsigned long clock_latency_ns;
235 opp_table = _find_opp_table(dev);
236 if (IS_ERR(opp_table))
239 clock_latency_ns = opp_table->clock_latency_ns_max;
241 dev_pm_opp_put_opp_table(opp_table);
243 return clock_latency_ns;
245 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
248 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
249 * @dev: device for which we do this operation
251 * Return: This function returns the max voltage latency in nanoseconds.
253 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
255 struct opp_table *opp_table;
256 struct dev_pm_opp *opp;
257 struct regulator *reg;
258 unsigned long latency_ns = 0;
265 opp_table = _find_opp_table(dev);
266 if (IS_ERR(opp_table))
269 /* Regulator may not be required for the device */
270 if (!opp_table->regulators)
273 count = opp_table->regulator_count;
275 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
279 mutex_lock(&opp_table->lock);
281 for (i = 0; i < count; i++) {
285 list_for_each_entry(opp, &opp_table->opp_list, node) {
289 if (opp->supplies[i].u_volt_min < uV[i].min)
290 uV[i].min = opp->supplies[i].u_volt_min;
291 if (opp->supplies[i].u_volt_max > uV[i].max)
292 uV[i].max = opp->supplies[i].u_volt_max;
296 mutex_unlock(&opp_table->lock);
299 * The caller needs to ensure that opp_table (and hence the regulator)
300 * isn't freed, while we are executing this routine.
302 for (i = 0; i < count; i++) {
303 reg = opp_table->regulators[i];
304 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
306 latency_ns += ret * 1000;
311 dev_pm_opp_put_opp_table(opp_table);
315 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
318 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
320 * @dev: device for which we do this operation
322 * Return: This function returns the max transition latency, in nanoseconds, to
323 * switch from one OPP to other.
325 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
327 return dev_pm_opp_get_max_volt_latency(dev) +
328 dev_pm_opp_get_max_clock_latency(dev);
330 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
333 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
334 * @dev: device for which we do this operation
336 * Return: This function returns the frequency of the OPP marked as suspend_opp
337 * if one is available, else returns 0;
339 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
341 struct opp_table *opp_table;
342 unsigned long freq = 0;
344 opp_table = _find_opp_table(dev);
345 if (IS_ERR(opp_table))
348 if (opp_table->suspend_opp && opp_table->suspend_opp->available)
349 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
351 dev_pm_opp_put_opp_table(opp_table);
355 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
357 int _get_opp_count(struct opp_table *opp_table)
359 struct dev_pm_opp *opp;
362 mutex_lock(&opp_table->lock);
364 list_for_each_entry(opp, &opp_table->opp_list, node) {
369 mutex_unlock(&opp_table->lock);
375 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
376 * @dev: device for which we do this operation
378 * Return: This function returns the number of available opps if there are any,
379 * else returns 0 if none or the corresponding error value.
381 int dev_pm_opp_get_opp_count(struct device *dev)
383 struct opp_table *opp_table;
386 opp_table = _find_opp_table(dev);
387 if (IS_ERR(opp_table)) {
388 count = PTR_ERR(opp_table);
389 dev_dbg(dev, "%s: OPP table not found (%d)\n",
394 count = _get_opp_count(opp_table);
395 dev_pm_opp_put_opp_table(opp_table);
399 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
402 * dev_pm_opp_find_freq_exact() - search for an exact frequency
403 * @dev: device for which we do this operation
404 * @freq: frequency to search for
405 * @available: true/false - match for available opp
407 * Return: Searches for exact match in the opp table and returns pointer to the
408 * matching opp if found, else returns ERR_PTR in case of error and should
409 * be handled using IS_ERR. Error return values can be:
410 * EINVAL: for bad pointer
411 * ERANGE: no match found for search
412 * ENODEV: if device not found in list of registered devices
414 * Note: available is a modifier for the search. if available=true, then the
415 * match is for exact matching frequency and is available in the stored OPP
416 * table. if false, the match is for exact frequency which is not available.
418 * This provides a mechanism to enable an opp which is not available currently
419 * or the opposite as well.
421 * The callers are required to call dev_pm_opp_put() for the returned OPP after
424 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
428 struct opp_table *opp_table;
429 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
431 opp_table = _find_opp_table(dev);
432 if (IS_ERR(opp_table)) {
433 int r = PTR_ERR(opp_table);
435 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
439 mutex_lock(&opp_table->lock);
441 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
442 if (temp_opp->available == available &&
443 temp_opp->rate == freq) {
446 /* Increment the reference count of OPP */
452 mutex_unlock(&opp_table->lock);
453 dev_pm_opp_put_opp_table(opp_table);
457 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
460 * dev_pm_opp_find_level_exact() - search for an exact level
461 * @dev: device for which we do this operation
462 * @level: level to search for
464 * Return: Searches for exact match in the opp table and returns pointer to the
465 * matching opp if found, else returns ERR_PTR in case of error and should
466 * be handled using IS_ERR. Error return values can be:
467 * EINVAL: for bad pointer
468 * ERANGE: no match found for search
469 * ENODEV: if device not found in list of registered devices
471 * The callers are required to call dev_pm_opp_put() for the returned OPP after
474 struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
477 struct opp_table *opp_table;
478 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
480 opp_table = _find_opp_table(dev);
481 if (IS_ERR(opp_table)) {
482 int r = PTR_ERR(opp_table);
484 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
488 mutex_lock(&opp_table->lock);
490 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
491 if (temp_opp->level == level) {
494 /* Increment the reference count of OPP */
500 mutex_unlock(&opp_table->lock);
501 dev_pm_opp_put_opp_table(opp_table);
505 EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
508 * dev_pm_opp_find_level_ceil() - search for an rounded up level
509 * @dev: device for which we do this operation
510 * @level: level to search for
512 * Return: Searches for rounded up match in the opp table and returns pointer
513 * to the matching opp if found, else returns ERR_PTR in case of error and
514 * should be handled using IS_ERR. Error return values can be:
515 * EINVAL: for bad pointer
516 * ERANGE: no match found for search
517 * ENODEV: if device not found in list of registered devices
519 * The callers are required to call dev_pm_opp_put() for the returned OPP after
522 struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
525 struct opp_table *opp_table;
526 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
528 opp_table = _find_opp_table(dev);
529 if (IS_ERR(opp_table)) {
530 int r = PTR_ERR(opp_table);
532 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
536 mutex_lock(&opp_table->lock);
538 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
539 if (temp_opp->available && temp_opp->level >= *level) {
543 /* Increment the reference count of OPP */
549 mutex_unlock(&opp_table->lock);
550 dev_pm_opp_put_opp_table(opp_table);
554 EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
556 static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
559 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
561 mutex_lock(&opp_table->lock);
563 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
564 if (temp_opp->available && temp_opp->rate >= *freq) {
568 /* Increment the reference count of OPP */
574 mutex_unlock(&opp_table->lock);
580 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
581 * @dev: device for which we do this operation
582 * @freq: Start frequency
584 * Search for the matching ceil *available* OPP from a starting freq
587 * Return: matching *opp and refreshes *freq accordingly, else returns
588 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
590 * EINVAL: for bad pointer
591 * ERANGE: no match found for search
592 * ENODEV: if device not found in list of registered devices
594 * The callers are required to call dev_pm_opp_put() for the returned OPP after
597 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
600 struct opp_table *opp_table;
601 struct dev_pm_opp *opp;
604 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
605 return ERR_PTR(-EINVAL);
608 opp_table = _find_opp_table(dev);
609 if (IS_ERR(opp_table))
610 return ERR_CAST(opp_table);
612 opp = _find_freq_ceil(opp_table, freq);
614 dev_pm_opp_put_opp_table(opp_table);
618 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
621 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
622 * @dev: device for which we do this operation
623 * @freq: Start frequency
625 * Search for the matching floor *available* OPP from a starting freq
628 * Return: matching *opp and refreshes *freq accordingly, else returns
629 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
631 * EINVAL: for bad pointer
632 * ERANGE: no match found for search
633 * ENODEV: if device not found in list of registered devices
635 * The callers are required to call dev_pm_opp_put() for the returned OPP after
638 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
641 struct opp_table *opp_table;
642 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
645 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
646 return ERR_PTR(-EINVAL);
649 opp_table = _find_opp_table(dev);
650 if (IS_ERR(opp_table))
651 return ERR_CAST(opp_table);
653 mutex_lock(&opp_table->lock);
655 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
656 if (temp_opp->available) {
657 /* go to the next node, before choosing prev */
658 if (temp_opp->rate > *freq)
665 /* Increment the reference count of OPP */
668 mutex_unlock(&opp_table->lock);
669 dev_pm_opp_put_opp_table(opp_table);
676 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
679 * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for
681 * @dev: Device for which we do this operation.
682 * @u_volt: Target voltage.
684 * Search for OPP with highest (ceil) frequency and has voltage <= u_volt.
686 * Return: matching *opp, else returns ERR_PTR in case of error which should be
687 * handled using IS_ERR.
689 * Error return values can be:
690 * EINVAL: bad parameters
692 * The callers are required to call dev_pm_opp_put() for the returned OPP after
695 struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev,
696 unsigned long u_volt)
698 struct opp_table *opp_table;
699 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
701 if (!dev || !u_volt) {
702 dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__,
704 return ERR_PTR(-EINVAL);
707 opp_table = _find_opp_table(dev);
708 if (IS_ERR(opp_table))
709 return ERR_CAST(opp_table);
711 mutex_lock(&opp_table->lock);
713 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
714 if (temp_opp->available) {
715 if (temp_opp->supplies[0].u_volt > u_volt)
721 /* Increment the reference count of OPP */
725 mutex_unlock(&opp_table->lock);
726 dev_pm_opp_put_opp_table(opp_table);
730 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt);
732 static int _set_opp_voltage(struct device *dev, struct regulator *reg,
733 struct dev_pm_opp_supply *supply)
737 /* Regulator not available for device */
739 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
744 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
745 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
747 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
748 supply->u_volt, supply->u_volt_max);
750 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
751 __func__, supply->u_volt_min, supply->u_volt,
752 supply->u_volt_max, ret);
757 static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
762 /* We may reach here for devices which don't change frequency */
766 ret = clk_set_rate(clk, freq);
768 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
775 static int _generic_set_opp_regulator(struct opp_table *opp_table,
777 struct dev_pm_opp *opp,
781 struct regulator *reg = opp_table->regulators[0];
782 struct dev_pm_opp *old_opp = opp_table->current_opp;
785 /* This function only supports single regulator per device */
786 if (WARN_ON(opp_table->regulator_count > 1)) {
787 dev_err(dev, "multiple regulators are not supported\n");
791 /* Scaling up? Scale voltage before frequency */
793 ret = _set_opp_voltage(dev, reg, opp->supplies);
795 goto restore_voltage;
798 /* Change frequency */
799 ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
801 goto restore_voltage;
803 /* Scaling down? Scale voltage after frequency */
805 ret = _set_opp_voltage(dev, reg, opp->supplies);
811 * Enable the regulator after setting its voltages, otherwise it breaks
812 * some boot-enabled regulators.
814 if (unlikely(!opp_table->enabled)) {
815 ret = regulator_enable(reg);
817 dev_warn(dev, "Failed to enable regulator: %d", ret);
823 if (_generic_set_opp_clk_only(dev, opp_table->clk, old_opp->rate))
824 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
825 __func__, old_opp->rate);
827 /* This shouldn't harm even if the voltages weren't updated earlier */
828 _set_opp_voltage(dev, reg, old_opp->supplies);
833 static int _set_opp_bw(const struct opp_table *opp_table,
834 struct dev_pm_opp *opp, struct device *dev)
839 if (!opp_table->paths)
842 for (i = 0; i < opp_table->path_count; i++) {
847 avg = opp->bandwidth[i].avg;
848 peak = opp->bandwidth[i].peak;
850 ret = icc_set_bw(opp_table->paths[i], avg, peak);
852 dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
853 opp ? "set" : "remove", i, ret);
861 static int _set_opp_custom(const struct opp_table *opp_table,
862 struct device *dev, struct dev_pm_opp *opp,
865 struct dev_pm_set_opp_data *data = opp_table->set_opp_data;
866 struct dev_pm_opp *old_opp = opp_table->current_opp;
870 * We support this only if dev_pm_opp_set_regulators() was called
873 if (opp_table->sod_supplies) {
874 size = sizeof(*old_opp->supplies) * opp_table->regulator_count;
875 memcpy(data->old_opp.supplies, old_opp->supplies, size);
876 memcpy(data->new_opp.supplies, opp->supplies, size);
877 data->regulator_count = opp_table->regulator_count;
879 data->regulator_count = 0;
882 data->regulators = opp_table->regulators;
883 data->clk = opp_table->clk;
885 data->old_opp.rate = old_opp->rate;
886 data->new_opp.rate = freq;
888 return opp_table->set_opp(data);
891 static int _set_required_opp(struct device *dev, struct device *pd_dev,
892 struct dev_pm_opp *opp, int i)
894 unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0;
900 ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
902 dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
903 dev_name(pd_dev), pstate, ret);
909 /* This is only called for PM domain for now */
910 static int _set_required_opps(struct device *dev,
911 struct opp_table *opp_table,
912 struct dev_pm_opp *opp, bool up)
914 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
915 struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
918 if (!required_opp_tables)
921 /* required-opps not fully initialized yet */
922 if (lazy_linking_pending(opp_table))
926 * We only support genpd's OPPs in the "required-opps" for now, as we
927 * don't know much about other use cases. Error out if the required OPP
928 * doesn't belong to a genpd.
930 if (unlikely(!required_opp_tables[0]->is_genpd)) {
931 dev_err(dev, "required-opps don't belong to a genpd\n");
935 /* Single genpd case */
936 if (!genpd_virt_devs)
937 return _set_required_opp(dev, dev, opp, 0);
939 /* Multiple genpd case */
942 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
943 * after it is freed from another thread.
945 mutex_lock(&opp_table->genpd_virt_dev_lock);
947 /* Scaling up? Set required OPPs in normal order, else reverse */
949 for (i = 0; i < opp_table->required_opp_count; i++) {
950 ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
955 for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
956 ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
962 mutex_unlock(&opp_table->genpd_virt_dev_lock);
967 static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
969 struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
972 if (!IS_ERR(opp_table->clk)) {
973 freq = clk_get_rate(opp_table->clk);
974 opp = _find_freq_ceil(opp_table, &freq);
978 * Unable to find the current OPP ? Pick the first from the list since
979 * it is in ascending order, otherwise rest of the code will need to
980 * make special checks to validate current_opp.
983 mutex_lock(&opp_table->lock);
984 opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
986 mutex_unlock(&opp_table->lock);
989 opp_table->current_opp = opp;
992 static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
996 if (!opp_table->enabled)
1000 * Some drivers need to support cases where some platforms may
1001 * have OPP table for the device, while others don't and
1002 * opp_set_rate() just needs to behave like clk_set_rate().
1004 if (!_get_opp_count(opp_table))
1007 ret = _set_opp_bw(opp_table, NULL, dev);
1011 if (opp_table->regulators)
1012 regulator_disable(opp_table->regulators[0]);
1014 ret = _set_required_opps(dev, opp_table, NULL, false);
1016 opp_table->enabled = false;
1020 static int _set_opp(struct device *dev, struct opp_table *opp_table,
1021 struct dev_pm_opp *opp, unsigned long freq)
1023 struct dev_pm_opp *old_opp;
1024 int scaling_down, ret;
1027 return _disable_opp_table(dev, opp_table);
1029 /* Find the currently set OPP if we don't know already */
1030 if (unlikely(!opp_table->current_opp))
1031 _find_current_opp(dev, opp_table);
1033 old_opp = opp_table->current_opp;
1035 /* Return early if nothing to do */
1036 if (old_opp == opp && opp_table->current_rate == freq &&
1037 opp_table->enabled) {
1038 dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__);
1042 dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
1043 __func__, opp_table->current_rate, freq, old_opp->level,
1044 opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
1045 opp->bandwidth ? opp->bandwidth[0].peak : 0);
1047 scaling_down = _opp_compare_key(old_opp, opp);
1048 if (scaling_down == -1)
1051 /* Scaling up? Configure required OPPs before frequency */
1052 if (!scaling_down) {
1053 ret = _set_required_opps(dev, opp_table, opp, true);
1055 dev_err(dev, "Failed to set required opps: %d\n", ret);
1059 ret = _set_opp_bw(opp_table, opp, dev);
1061 dev_err(dev, "Failed to set bw: %d\n", ret);
1066 if (opp_table->set_opp) {
1067 ret = _set_opp_custom(opp_table, dev, opp, freq);
1068 } else if (opp_table->regulators) {
1069 ret = _generic_set_opp_regulator(opp_table, dev, opp, freq,
1072 /* Only frequency scaling */
1073 ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
1079 /* Scaling down? Configure required OPPs after frequency */
1081 ret = _set_opp_bw(opp_table, opp, dev);
1083 dev_err(dev, "Failed to set bw: %d\n", ret);
1087 ret = _set_required_opps(dev, opp_table, opp, false);
1089 dev_err(dev, "Failed to set required opps: %d\n", ret);
1094 opp_table->enabled = true;
1095 dev_pm_opp_put(old_opp);
1097 /* Make sure current_opp doesn't get freed */
1098 dev_pm_opp_get(opp);
1099 opp_table->current_opp = opp;
1100 opp_table->current_rate = freq;
1106 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
1107 * @dev: device for which we do this operation
1108 * @target_freq: frequency to achieve
1110 * This configures the power-supplies to the levels specified by the OPP
1111 * corresponding to the target_freq, and programs the clock to a value <=
1112 * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
1113 * provided by the opp, should have already rounded to the target OPP's
1116 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
1118 struct opp_table *opp_table;
1119 unsigned long freq = 0, temp_freq;
1120 struct dev_pm_opp *opp = NULL;
1123 opp_table = _find_opp_table(dev);
1124 if (IS_ERR(opp_table)) {
1125 dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
1126 return PTR_ERR(opp_table);
1131 * For IO devices which require an OPP on some platforms/SoCs
1132 * while just needing to scale the clock on some others
1133 * we look for empty OPP tables with just a clock handle and
1134 * scale only the clk. This makes dev_pm_opp_set_rate()
1135 * equivalent to a clk_set_rate()
1137 if (!_get_opp_count(opp_table)) {
1138 ret = _generic_set_opp_clk_only(dev, opp_table->clk, target_freq);
1142 freq = clk_round_rate(opp_table->clk, target_freq);
1143 if ((long)freq <= 0)
1147 * The clock driver may support finer resolution of the
1148 * frequencies than the OPP table, don't update the frequency we
1149 * pass to clk_set_rate() here.
1152 opp = _find_freq_ceil(opp_table, &temp_freq);
1155 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
1156 __func__, freq, ret);
1161 ret = _set_opp(dev, opp_table, opp, freq);
1164 dev_pm_opp_put(opp);
1166 dev_pm_opp_put_opp_table(opp_table);
1169 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
1172 * dev_pm_opp_set_opp() - Configure device for OPP
1173 * @dev: device for which we do this operation
1174 * @opp: OPP to set to
1176 * This configures the device based on the properties of the OPP passed to this
1179 * Return: 0 on success, a negative error number otherwise.
1181 int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
1183 struct opp_table *opp_table;
1186 opp_table = _find_opp_table(dev);
1187 if (IS_ERR(opp_table)) {
1188 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
1189 return PTR_ERR(opp_table);
1192 ret = _set_opp(dev, opp_table, opp, opp ? opp->rate : 0);
1193 dev_pm_opp_put_opp_table(opp_table);
1197 EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp);
1199 /* OPP-dev Helpers */
1200 static void _remove_opp_dev(struct opp_device *opp_dev,
1201 struct opp_table *opp_table)
1203 opp_debug_unregister(opp_dev, opp_table);
1204 list_del(&opp_dev->node);
1208 struct opp_device *_add_opp_dev(const struct device *dev,
1209 struct opp_table *opp_table)
1211 struct opp_device *opp_dev;
1213 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
1217 /* Initialize opp-dev */
1220 mutex_lock(&opp_table->lock);
1221 list_add(&opp_dev->node, &opp_table->dev_list);
1222 mutex_unlock(&opp_table->lock);
1224 /* Create debugfs entries for the opp_table */
1225 opp_debug_register(opp_dev, opp_table);
1230 static struct opp_table *_allocate_opp_table(struct device *dev, int index)
1232 struct opp_table *opp_table;
1233 struct opp_device *opp_dev;
1237 * Allocate a new OPP table. In the infrequent case where a new
1238 * device is needed to be added, we pay this penalty.
1240 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
1242 return ERR_PTR(-ENOMEM);
1244 mutex_init(&opp_table->lock);
1245 mutex_init(&opp_table->genpd_virt_dev_lock);
1246 INIT_LIST_HEAD(&opp_table->dev_list);
1247 INIT_LIST_HEAD(&opp_table->lazy);
1249 /* Mark regulator count uninitialized */
1250 opp_table->regulator_count = -1;
1252 opp_dev = _add_opp_dev(dev, opp_table);
1258 _of_init_opp_table(opp_table, dev, index);
1260 /* Find interconnect path(s) for the device */
1261 ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
1263 if (ret == -EPROBE_DEFER)
1264 goto remove_opp_dev;
1266 dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
1270 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
1271 INIT_LIST_HEAD(&opp_table->opp_list);
1272 kref_init(&opp_table->kref);
1277 _remove_opp_dev(opp_dev, opp_table);
1280 return ERR_PTR(ret);
1283 void _get_opp_table_kref(struct opp_table *opp_table)
1285 kref_get(&opp_table->kref);
1288 static struct opp_table *_update_opp_table_clk(struct device *dev,
1289 struct opp_table *opp_table,
1295 * Return early if we don't need to get clk or we have already tried it
1298 if (!getclk || IS_ERR(opp_table) || opp_table->clk)
1301 /* Find clk for the device */
1302 opp_table->clk = clk_get(dev, NULL);
1304 ret = PTR_ERR_OR_ZERO(opp_table->clk);
1308 if (ret == -ENOENT) {
1309 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
1313 dev_pm_opp_put_opp_table(opp_table);
1314 dev_err_probe(dev, ret, "Couldn't find clock\n");
1316 return ERR_PTR(ret);
1320 * We need to make sure that the OPP table for a device doesn't get added twice,
1321 * if this routine gets called in parallel with the same device pointer.
1323 * The simplest way to enforce that is to perform everything (find existing
1324 * table and if not found, create a new one) under the opp_table_lock, so only
1325 * one creator gets access to the same. But that expands the critical section
1326 * under the lock and may end up causing circular dependencies with frameworks
1327 * like debugfs, interconnect or clock framework as they may be direct or
1328 * indirect users of OPP core.
1330 * And for that reason we have to go for a bit tricky implementation here, which
1331 * uses the opp_tables_busy flag to indicate if another creator is in the middle
1332 * of adding an OPP table and others should wait for it to finish.
1334 struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
1337 struct opp_table *opp_table;
1340 mutex_lock(&opp_table_lock);
1342 opp_table = _find_opp_table_unlocked(dev);
1343 if (!IS_ERR(opp_table))
1347 * The opp_tables list or an OPP table's dev_list is getting updated by
1348 * another user, wait for it to finish.
1350 if (unlikely(opp_tables_busy)) {
1351 mutex_unlock(&opp_table_lock);
1356 opp_tables_busy = true;
1357 opp_table = _managed_opp(dev, index);
1359 /* Drop the lock to reduce the size of critical section */
1360 mutex_unlock(&opp_table_lock);
1363 if (!_add_opp_dev(dev, opp_table)) {
1364 dev_pm_opp_put_opp_table(opp_table);
1365 opp_table = ERR_PTR(-ENOMEM);
1368 mutex_lock(&opp_table_lock);
1370 opp_table = _allocate_opp_table(dev, index);
1372 mutex_lock(&opp_table_lock);
1373 if (!IS_ERR(opp_table))
1374 list_add(&opp_table->node, &opp_tables);
1377 opp_tables_busy = false;
1380 mutex_unlock(&opp_table_lock);
1382 return _update_opp_table_clk(dev, opp_table, getclk);
1385 static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
1387 return _add_opp_table_indexed(dev, 0, getclk);
1390 struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
1392 return _find_opp_table(dev);
1394 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
1396 static void _opp_table_kref_release(struct kref *kref)
1398 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
1399 struct opp_device *opp_dev, *temp;
1402 /* Drop the lock as soon as we can */
1403 list_del(&opp_table->node);
1404 mutex_unlock(&opp_table_lock);
1406 if (opp_table->current_opp)
1407 dev_pm_opp_put(opp_table->current_opp);
1409 _of_clear_opp_table(opp_table);
1412 if (!IS_ERR(opp_table->clk))
1413 clk_put(opp_table->clk);
1415 if (opp_table->paths) {
1416 for (i = 0; i < opp_table->path_count; i++)
1417 icc_put(opp_table->paths[i]);
1418 kfree(opp_table->paths);
1421 WARN_ON(!list_empty(&opp_table->opp_list));
1423 list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
1425 * The OPP table is getting removed, drop the performance state
1428 if (opp_table->genpd_performance_state)
1429 dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
1431 _remove_opp_dev(opp_dev, opp_table);
1434 mutex_destroy(&opp_table->genpd_virt_dev_lock);
1435 mutex_destroy(&opp_table->lock);
1439 void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
1441 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
1444 EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
1446 void _opp_free(struct dev_pm_opp *opp)
1451 static void _opp_kref_release(struct kref *kref)
1453 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1454 struct opp_table *opp_table = opp->opp_table;
1456 list_del(&opp->node);
1457 mutex_unlock(&opp_table->lock);
1460 * Notify the changes in the availability of the operable
1461 * frequency/voltage list.
1463 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
1464 _of_opp_free_required_opps(opp_table, opp);
1465 opp_debug_remove_one(opp);
1469 void dev_pm_opp_get(struct dev_pm_opp *opp)
1471 kref_get(&opp->kref);
1474 void dev_pm_opp_put(struct dev_pm_opp *opp)
1476 kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
1478 EXPORT_SYMBOL_GPL(dev_pm_opp_put);
1481 * dev_pm_opp_remove() - Remove an OPP from OPP table
1482 * @dev: device for which we do this operation
1483 * @freq: OPP to remove with matching 'freq'
1485 * This function removes an opp from the opp table.
1487 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1489 struct dev_pm_opp *opp;
1490 struct opp_table *opp_table;
1493 opp_table = _find_opp_table(dev);
1494 if (IS_ERR(opp_table))
1497 mutex_lock(&opp_table->lock);
1499 list_for_each_entry(opp, &opp_table->opp_list, node) {
1500 if (opp->rate == freq) {
1506 mutex_unlock(&opp_table->lock);
1509 dev_pm_opp_put(opp);
1511 /* Drop the reference taken by dev_pm_opp_add() */
1512 dev_pm_opp_put_opp_table(opp_table);
1514 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1518 /* Drop the reference taken by _find_opp_table() */
1519 dev_pm_opp_put_opp_table(opp_table);
1521 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1523 static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1526 struct dev_pm_opp *opp = NULL, *temp;
1528 mutex_lock(&opp_table->lock);
1529 list_for_each_entry(temp, &opp_table->opp_list, node) {
1531 * Refcount must be dropped only once for each OPP by OPP core,
1532 * do that with help of "removed" flag.
1534 if (!temp->removed && dynamic == temp->dynamic) {
1540 mutex_unlock(&opp_table->lock);
1545 * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to
1546 * happen lock less to avoid circular dependency issues. This routine must be
1547 * called without the opp_table->lock held.
1549 static void _opp_remove_all(struct opp_table *opp_table, bool dynamic)
1551 struct dev_pm_opp *opp;
1553 while ((opp = _opp_get_next(opp_table, dynamic))) {
1554 opp->removed = true;
1555 dev_pm_opp_put(opp);
1557 /* Drop the references taken by dev_pm_opp_add() */
1559 dev_pm_opp_put_opp_table(opp_table);
1563 bool _opp_remove_all_static(struct opp_table *opp_table)
1565 mutex_lock(&opp_table->lock);
1567 if (!opp_table->parsed_static_opps) {
1568 mutex_unlock(&opp_table->lock);
1572 if (--opp_table->parsed_static_opps) {
1573 mutex_unlock(&opp_table->lock);
1577 mutex_unlock(&opp_table->lock);
1579 _opp_remove_all(opp_table, false);
1584 * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
1585 * @dev: device for which we do this operation
1587 * This function removes all dynamically created OPPs from the opp table.
1589 void dev_pm_opp_remove_all_dynamic(struct device *dev)
1591 struct opp_table *opp_table;
1593 opp_table = _find_opp_table(dev);
1594 if (IS_ERR(opp_table))
1597 _opp_remove_all(opp_table, true);
1599 /* Drop the reference taken by _find_opp_table() */
1600 dev_pm_opp_put_opp_table(opp_table);
1602 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
1604 struct dev_pm_opp *_opp_allocate(struct opp_table *table)
1606 struct dev_pm_opp *opp;
1607 int supply_count, supply_size, icc_size;
1609 /* Allocate space for at least one supply */
1610 supply_count = table->regulator_count > 0 ? table->regulator_count : 1;
1611 supply_size = sizeof(*opp->supplies) * supply_count;
1612 icc_size = sizeof(*opp->bandwidth) * table->path_count;
1614 /* allocate new OPP node and supplies structures */
1615 opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL);
1620 /* Put the supplies at the end of the OPP structure as an empty array */
1621 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1623 opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count);
1624 INIT_LIST_HEAD(&opp->node);
1629 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
1630 struct opp_table *opp_table)
1632 struct regulator *reg;
1635 if (!opp_table->regulators)
1638 for (i = 0; i < opp_table->regulator_count; i++) {
1639 reg = opp_table->regulators[i];
1641 if (!regulator_is_supported_voltage(reg,
1642 opp->supplies[i].u_volt_min,
1643 opp->supplies[i].u_volt_max)) {
1644 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1645 __func__, opp->supplies[i].u_volt_min,
1646 opp->supplies[i].u_volt_max);
1654 int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
1656 if (opp1->rate != opp2->rate)
1657 return opp1->rate < opp2->rate ? -1 : 1;
1658 if (opp1->bandwidth && opp2->bandwidth &&
1659 opp1->bandwidth[0].peak != opp2->bandwidth[0].peak)
1660 return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1;
1661 if (opp1->level != opp2->level)
1662 return opp1->level < opp2->level ? -1 : 1;
1666 static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1667 struct opp_table *opp_table,
1668 struct list_head **head)
1670 struct dev_pm_opp *opp;
1674 * Insert new OPP in order of increasing frequency and discard if
1677 * Need to use &opp_table->opp_list in the condition part of the 'for'
1678 * loop, don't replace it with head otherwise it will become an infinite
1681 list_for_each_entry(opp, &opp_table->opp_list, node) {
1682 opp_cmp = _opp_compare_key(new_opp, opp);
1691 /* Duplicate OPPs */
1692 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1693 __func__, opp->rate, opp->supplies[0].u_volt,
1694 opp->available, new_opp->rate,
1695 new_opp->supplies[0].u_volt, new_opp->available);
1697 /* Should we compare voltages for all regulators here ? */
1698 return opp->available &&
1699 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1705 void _required_opps_available(struct dev_pm_opp *opp, int count)
1709 for (i = 0; i < count; i++) {
1710 if (opp->required_opps[i]->available)
1713 opp->available = false;
1714 pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n",
1715 __func__, opp->required_opps[i]->np, opp->rate);
1722 * 0: On success. And appropriate error message for duplicate OPPs.
1723 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1724 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1725 * sure we don't print error messages unnecessarily if different parts of
1726 * kernel try to initialize the OPP table.
1727 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1728 * should be considered an error by the callers of _opp_add().
1730 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1731 struct opp_table *opp_table, bool rate_not_available)
1733 struct list_head *head;
1736 mutex_lock(&opp_table->lock);
1737 head = &opp_table->opp_list;
1739 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1741 mutex_unlock(&opp_table->lock);
1745 list_add(&new_opp->node, head);
1746 mutex_unlock(&opp_table->lock);
1748 new_opp->opp_table = opp_table;
1749 kref_init(&new_opp->kref);
1751 opp_debug_create_one(new_opp, opp_table);
1753 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
1754 new_opp->available = false;
1755 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1756 __func__, new_opp->rate);
1759 /* required-opps not fully initialized yet */
1760 if (lazy_linking_pending(opp_table))
1763 _required_opps_available(new_opp, opp_table->required_opp_count);
1769 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
1770 * @opp_table: OPP table
1771 * @dev: device for which we do this operation
1772 * @freq: Frequency in Hz for this OPP
1773 * @u_volt: Voltage in uVolts for this OPP
1774 * @dynamic: Dynamically added OPPs.
1776 * This function adds an opp definition to the opp table and returns status.
1777 * The opp is made available by default and it can be controlled using
1778 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1780 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1781 * and freed by dev_pm_opp_of_remove_table.
1785 * Duplicate OPPs (both freq and volt are same) and opp->available
1786 * -EEXIST Freq are same and volt are different OR
1787 * Duplicate OPPs (both freq and volt are same) and !opp->available
1788 * -ENOMEM Memory allocation failure
1790 int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1791 unsigned long freq, long u_volt, bool dynamic)
1793 struct dev_pm_opp *new_opp;
1797 new_opp = _opp_allocate(opp_table);
1801 /* populate the opp table */
1802 new_opp->rate = freq;
1803 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
1804 new_opp->supplies[0].u_volt = u_volt;
1805 new_opp->supplies[0].u_volt_min = u_volt - tol;
1806 new_opp->supplies[0].u_volt_max = u_volt + tol;
1807 new_opp->available = true;
1808 new_opp->dynamic = dynamic;
1810 ret = _opp_add(dev, new_opp, opp_table, false);
1812 /* Don't return error for duplicate OPPs */
1819 * Notify the changes in the availability of the operable
1820 * frequency/voltage list.
1822 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
1832 * dev_pm_opp_set_supported_hw() - Set supported platforms
1833 * @dev: Device for which supported-hw has to be set.
1834 * @versions: Array of hierarchy of versions to match.
1835 * @count: Number of elements in the array.
1837 * This is required only for the V2 bindings, and it enables a platform to
1838 * specify the hierarchy of versions it supports. OPP layer will then enable
1839 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1842 struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1843 const u32 *versions, unsigned int count)
1845 struct opp_table *opp_table;
1847 opp_table = _add_opp_table(dev, false);
1848 if (IS_ERR(opp_table))
1851 /* Make sure there are no concurrent readers while updating opp_table */
1852 WARN_ON(!list_empty(&opp_table->opp_list));
1854 /* Another CPU that shares the OPP table has set the property ? */
1855 if (opp_table->supported_hw)
1858 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
1860 if (!opp_table->supported_hw) {
1861 dev_pm_opp_put_opp_table(opp_table);
1862 return ERR_PTR(-ENOMEM);
1865 opp_table->supported_hw_count = count;
1869 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1872 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
1873 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
1875 * This is required only for the V2 bindings, and is called for a matching
1876 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
1877 * will not be freed.
1879 void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
1881 if (unlikely(!opp_table))
1884 kfree(opp_table->supported_hw);
1885 opp_table->supported_hw = NULL;
1886 opp_table->supported_hw_count = 0;
1888 dev_pm_opp_put_opp_table(opp_table);
1890 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1892 static void devm_pm_opp_supported_hw_release(void *data)
1894 dev_pm_opp_put_supported_hw(data);
1898 * devm_pm_opp_set_supported_hw() - Set supported platforms
1899 * @dev: Device for which supported-hw has to be set.
1900 * @versions: Array of hierarchy of versions to match.
1901 * @count: Number of elements in the array.
1903 * This is a resource-managed variant of dev_pm_opp_set_supported_hw().
1905 * Return: 0 on success and errorno otherwise.
1907 int devm_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
1910 struct opp_table *opp_table;
1912 opp_table = dev_pm_opp_set_supported_hw(dev, versions, count);
1913 if (IS_ERR(opp_table))
1914 return PTR_ERR(opp_table);
1916 return devm_add_action_or_reset(dev, devm_pm_opp_supported_hw_release,
1919 EXPORT_SYMBOL_GPL(devm_pm_opp_set_supported_hw);
1922 * dev_pm_opp_set_prop_name() - Set prop-extn name
1923 * @dev: Device for which the prop-name has to be set.
1924 * @name: name to postfix to properties.
1926 * This is required only for the V2 bindings, and it enables a platform to
1927 * specify the extn to be used for certain property names. The properties to
1928 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1929 * should postfix the property name with -<name> while looking for them.
1931 struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1933 struct opp_table *opp_table;
1935 opp_table = _add_opp_table(dev, false);
1936 if (IS_ERR(opp_table))
1939 /* Make sure there are no concurrent readers while updating opp_table */
1940 WARN_ON(!list_empty(&opp_table->opp_list));
1942 /* Another CPU that shares the OPP table has set the property ? */
1943 if (opp_table->prop_name)
1946 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1947 if (!opp_table->prop_name) {
1948 dev_pm_opp_put_opp_table(opp_table);
1949 return ERR_PTR(-ENOMEM);
1954 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1957 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
1958 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
1960 * This is required only for the V2 bindings, and is called for a matching
1961 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
1962 * will not be freed.
1964 void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
1966 if (unlikely(!opp_table))
1969 kfree(opp_table->prop_name);
1970 opp_table->prop_name = NULL;
1972 dev_pm_opp_put_opp_table(opp_table);
1974 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1977 * dev_pm_opp_set_regulators() - Set regulator names for the device
1978 * @dev: Device for which regulator name is being set.
1979 * @names: Array of pointers to the names of the regulator.
1980 * @count: Number of regulators.
1982 * In order to support OPP switching, OPP layer needs to know the name of the
1983 * device's regulators, as the core would be required to switch voltages as
1986 * This must be called before any OPPs are initialized for the device.
1988 struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1989 const char * const names[],
1992 struct dev_pm_opp_supply *supplies;
1993 struct opp_table *opp_table;
1994 struct regulator *reg;
1997 opp_table = _add_opp_table(dev, false);
1998 if (IS_ERR(opp_table))
2001 /* This should be called before OPPs are initialized */
2002 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
2007 /* Another CPU that shares the OPP table has set the regulators ? */
2008 if (opp_table->regulators)
2011 opp_table->regulators = kmalloc_array(count,
2012 sizeof(*opp_table->regulators),
2014 if (!opp_table->regulators) {
2019 for (i = 0; i < count; i++) {
2020 reg = regulator_get_optional(dev, names[i]);
2023 if (ret != -EPROBE_DEFER)
2024 dev_err(dev, "%s: no regulator (%s) found: %d\n",
2025 __func__, names[i], ret);
2026 goto free_regulators;
2029 opp_table->regulators[i] = reg;
2032 opp_table->regulator_count = count;
2034 supplies = kmalloc_array(count * 2, sizeof(*supplies), GFP_KERNEL);
2037 goto free_regulators;
2040 mutex_lock(&opp_table->lock);
2041 opp_table->sod_supplies = supplies;
2042 if (opp_table->set_opp_data) {
2043 opp_table->set_opp_data->old_opp.supplies = supplies;
2044 opp_table->set_opp_data->new_opp.supplies = supplies + count;
2046 mutex_unlock(&opp_table->lock);
2052 regulator_put(opp_table->regulators[--i]);
2054 kfree(opp_table->regulators);
2055 opp_table->regulators = NULL;
2056 opp_table->regulator_count = -1;
2058 dev_pm_opp_put_opp_table(opp_table);
2060 return ERR_PTR(ret);
2062 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
2065 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
2066 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
2068 void dev_pm_opp_put_regulators(struct opp_table *opp_table)
2072 if (unlikely(!opp_table))
2075 if (!opp_table->regulators)
2078 if (opp_table->enabled) {
2079 for (i = opp_table->regulator_count - 1; i >= 0; i--)
2080 regulator_disable(opp_table->regulators[i]);
2083 for (i = opp_table->regulator_count - 1; i >= 0; i--)
2084 regulator_put(opp_table->regulators[i]);
2086 mutex_lock(&opp_table->lock);
2087 if (opp_table->set_opp_data) {
2088 opp_table->set_opp_data->old_opp.supplies = NULL;
2089 opp_table->set_opp_data->new_opp.supplies = NULL;
2092 kfree(opp_table->sod_supplies);
2093 opp_table->sod_supplies = NULL;
2094 mutex_unlock(&opp_table->lock);
2096 kfree(opp_table->regulators);
2097 opp_table->regulators = NULL;
2098 opp_table->regulator_count = -1;
2101 dev_pm_opp_put_opp_table(opp_table);
2103 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
2105 static void devm_pm_opp_regulators_release(void *data)
2107 dev_pm_opp_put_regulators(data);
2111 * devm_pm_opp_set_regulators() - Set regulator names for the device
2112 * @dev: Device for which regulator name is being set.
2113 * @names: Array of pointers to the names of the regulator.
2114 * @count: Number of regulators.
2116 * This is a resource-managed variant of dev_pm_opp_set_regulators().
2118 * Return: 0 on success and errorno otherwise.
2120 int devm_pm_opp_set_regulators(struct device *dev,
2121 const char * const names[],
2124 struct opp_table *opp_table;
2126 opp_table = dev_pm_opp_set_regulators(dev, names, count);
2127 if (IS_ERR(opp_table))
2128 return PTR_ERR(opp_table);
2130 return devm_add_action_or_reset(dev, devm_pm_opp_regulators_release,
2133 EXPORT_SYMBOL_GPL(devm_pm_opp_set_regulators);
2136 * dev_pm_opp_set_clkname() - Set clk name for the device
2137 * @dev: Device for which clk name is being set.
2140 * In order to support OPP switching, OPP layer needs to get pointer to the
2141 * clock for the device. Simple cases work fine without using this routine (i.e.
2142 * by passing connection-id as NULL), but for a device with multiple clocks
2143 * available, the OPP core needs to know the exact name of the clk to use.
2145 * This must be called before any OPPs are initialized for the device.
2147 struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
2149 struct opp_table *opp_table;
2152 opp_table = _add_opp_table(dev, false);
2153 if (IS_ERR(opp_table))
2156 /* This should be called before OPPs are initialized */
2157 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
2162 /* clk shouldn't be initialized at this point */
2163 if (WARN_ON(opp_table->clk)) {
2168 /* Find clk for the device */
2169 opp_table->clk = clk_get(dev, name);
2170 if (IS_ERR(opp_table->clk)) {
2171 ret = PTR_ERR(opp_table->clk);
2172 if (ret != -EPROBE_DEFER) {
2173 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
2182 dev_pm_opp_put_opp_table(opp_table);
2184 return ERR_PTR(ret);
2186 EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
2189 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
2190 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
2192 void dev_pm_opp_put_clkname(struct opp_table *opp_table)
2194 if (unlikely(!opp_table))
2197 clk_put(opp_table->clk);
2198 opp_table->clk = ERR_PTR(-EINVAL);
2200 dev_pm_opp_put_opp_table(opp_table);
2202 EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
2204 static void devm_pm_opp_clkname_release(void *data)
2206 dev_pm_opp_put_clkname(data);
2210 * devm_pm_opp_set_clkname() - Set clk name for the device
2211 * @dev: Device for which clk name is being set.
2214 * This is a resource-managed variant of dev_pm_opp_set_clkname().
2216 * Return: 0 on success and errorno otherwise.
2218 int devm_pm_opp_set_clkname(struct device *dev, const char *name)
2220 struct opp_table *opp_table;
2222 opp_table = dev_pm_opp_set_clkname(dev, name);
2223 if (IS_ERR(opp_table))
2224 return PTR_ERR(opp_table);
2226 return devm_add_action_or_reset(dev, devm_pm_opp_clkname_release,
2229 EXPORT_SYMBOL_GPL(devm_pm_opp_set_clkname);
2232 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
2233 * @dev: Device for which the helper is getting registered.
2234 * @set_opp: Custom set OPP helper.
2236 * This is useful to support complex platforms (like platforms with multiple
2237 * regulators per device), instead of the generic OPP set rate helper.
2239 * This must be called before any OPPs are initialized for the device.
2241 struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
2242 int (*set_opp)(struct dev_pm_set_opp_data *data))
2244 struct dev_pm_set_opp_data *data;
2245 struct opp_table *opp_table;
2248 return ERR_PTR(-EINVAL);
2250 opp_table = _add_opp_table(dev, false);
2251 if (IS_ERR(opp_table))
2254 /* This should be called before OPPs are initialized */
2255 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
2256 dev_pm_opp_put_opp_table(opp_table);
2257 return ERR_PTR(-EBUSY);
2260 /* Another CPU that shares the OPP table has set the helper ? */
2261 if (opp_table->set_opp)
2264 data = kzalloc(sizeof(*data), GFP_KERNEL);
2266 return ERR_PTR(-ENOMEM);
2268 mutex_lock(&opp_table->lock);
2269 opp_table->set_opp_data = data;
2270 if (opp_table->sod_supplies) {
2271 data->old_opp.supplies = opp_table->sod_supplies;
2272 data->new_opp.supplies = opp_table->sod_supplies +
2273 opp_table->regulator_count;
2275 mutex_unlock(&opp_table->lock);
2277 opp_table->set_opp = set_opp;
2281 EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
2284 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
2286 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
2288 * Release resources blocked for platform specific set_opp helper.
2290 void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
2292 if (unlikely(!opp_table))
2295 opp_table->set_opp = NULL;
2297 mutex_lock(&opp_table->lock);
2298 kfree(opp_table->set_opp_data);
2299 opp_table->set_opp_data = NULL;
2300 mutex_unlock(&opp_table->lock);
2302 dev_pm_opp_put_opp_table(opp_table);
2304 EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
2306 static void devm_pm_opp_unregister_set_opp_helper(void *data)
2308 dev_pm_opp_unregister_set_opp_helper(data);
2312 * devm_pm_opp_register_set_opp_helper() - Register custom set OPP helper
2313 * @dev: Device for which the helper is getting registered.
2314 * @set_opp: Custom set OPP helper.
2316 * This is a resource-managed version of dev_pm_opp_register_set_opp_helper().
2318 * Return: 0 on success and errorno otherwise.
2320 int devm_pm_opp_register_set_opp_helper(struct device *dev,
2321 int (*set_opp)(struct dev_pm_set_opp_data *data))
2323 struct opp_table *opp_table;
2325 opp_table = dev_pm_opp_register_set_opp_helper(dev, set_opp);
2326 if (IS_ERR(opp_table))
2327 return PTR_ERR(opp_table);
2329 return devm_add_action_or_reset(dev, devm_pm_opp_unregister_set_opp_helper,
2332 EXPORT_SYMBOL_GPL(devm_pm_opp_register_set_opp_helper);
2334 static void _opp_detach_genpd(struct opp_table *opp_table)
2338 if (!opp_table->genpd_virt_devs)
2341 for (index = 0; index < opp_table->required_opp_count; index++) {
2342 if (!opp_table->genpd_virt_devs[index])
2345 dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
2346 opp_table->genpd_virt_devs[index] = NULL;
2349 kfree(opp_table->genpd_virt_devs);
2350 opp_table->genpd_virt_devs = NULL;
2354 * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
2355 * @dev: Consumer device for which the genpd is getting attached.
2356 * @names: Null terminated array of pointers containing names of genpd to attach.
2357 * @virt_devs: Pointer to return the array of virtual devices.
2359 * Multiple generic power domains for a device are supported with the help of
2360 * virtual genpd devices, which are created for each consumer device - genpd
2361 * pair. These are the device structures which are attached to the power domain
2362 * and are required by the OPP core to set the performance state of the genpd.
2363 * The same API also works for the case where single genpd is available and so
2364 * we don't need to support that separately.
2366 * This helper will normally be called by the consumer driver of the device
2367 * "dev", as only that has details of the genpd names.
2369 * This helper needs to be called once with a list of all genpd to attach.
2370 * Otherwise the original device structure will be used instead by the OPP core.
2372 * The order of entries in the names array must match the order in which
2373 * "required-opps" are added in DT.
2375 struct opp_table *dev_pm_opp_attach_genpd(struct device *dev,
2376 const char * const *names, struct device ***virt_devs)
2378 struct opp_table *opp_table;
2379 struct device *virt_dev;
2380 int index = 0, ret = -EINVAL;
2381 const char * const *name = names;
2383 opp_table = _add_opp_table(dev, false);
2384 if (IS_ERR(opp_table))
2387 if (opp_table->genpd_virt_devs)
2391 * If the genpd's OPP table isn't already initialized, parsing of the
2392 * required-opps fail for dev. We should retry this after genpd's OPP
2395 if (!opp_table->required_opp_count) {
2396 ret = -EPROBE_DEFER;
2400 mutex_lock(&opp_table->genpd_virt_dev_lock);
2402 opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2403 sizeof(*opp_table->genpd_virt_devs),
2405 if (!opp_table->genpd_virt_devs)
2409 if (index >= opp_table->required_opp_count) {
2410 dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
2411 *name, opp_table->required_opp_count, index);
2415 virt_dev = dev_pm_domain_attach_by_name(dev, *name);
2416 if (IS_ERR(virt_dev)) {
2417 ret = PTR_ERR(virt_dev);
2418 dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
2422 opp_table->genpd_virt_devs[index] = virt_dev;
2428 *virt_devs = opp_table->genpd_virt_devs;
2429 mutex_unlock(&opp_table->genpd_virt_dev_lock);
2434 _opp_detach_genpd(opp_table);
2436 mutex_unlock(&opp_table->genpd_virt_dev_lock);
2439 dev_pm_opp_put_opp_table(opp_table);
2441 return ERR_PTR(ret);
2443 EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd);
2446 * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device.
2447 * @opp_table: OPP table returned by dev_pm_opp_attach_genpd().
2449 * This detaches the genpd(s), resets the virtual device pointers, and puts the
2452 void dev_pm_opp_detach_genpd(struct opp_table *opp_table)
2454 if (unlikely(!opp_table))
2458 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
2461 mutex_lock(&opp_table->genpd_virt_dev_lock);
2462 _opp_detach_genpd(opp_table);
2463 mutex_unlock(&opp_table->genpd_virt_dev_lock);
2465 dev_pm_opp_put_opp_table(opp_table);
2467 EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd);
2469 static void devm_pm_opp_detach_genpd(void *data)
2471 dev_pm_opp_detach_genpd(data);
2475 * devm_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual
2477 * @dev: Consumer device for which the genpd is getting attached.
2478 * @names: Null terminated array of pointers containing names of genpd to attach.
2479 * @virt_devs: Pointer to return the array of virtual devices.
2481 * This is a resource-managed version of dev_pm_opp_attach_genpd().
2483 * Return: 0 on success and errorno otherwise.
2485 int devm_pm_opp_attach_genpd(struct device *dev, const char * const *names,
2486 struct device ***virt_devs)
2488 struct opp_table *opp_table;
2490 opp_table = dev_pm_opp_attach_genpd(dev, names, virt_devs);
2491 if (IS_ERR(opp_table))
2492 return PTR_ERR(opp_table);
2494 return devm_add_action_or_reset(dev, devm_pm_opp_detach_genpd,
2497 EXPORT_SYMBOL_GPL(devm_pm_opp_attach_genpd);
2500 * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
2501 * @src_table: OPP table which has @dst_table as one of its required OPP table.
2502 * @dst_table: Required OPP table of the @src_table.
2503 * @src_opp: OPP from the @src_table.
2505 * This function returns the OPP (present in @dst_table) pointed out by the
2506 * "required-opps" property of the @src_opp (present in @src_table).
2508 * The callers are required to call dev_pm_opp_put() for the returned OPP after
2511 * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise.
2513 struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
2514 struct opp_table *dst_table,
2515 struct dev_pm_opp *src_opp)
2517 struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
2520 if (!src_table || !dst_table || !src_opp ||
2521 !src_table->required_opp_tables)
2522 return ERR_PTR(-EINVAL);
2524 /* required-opps not fully initialized yet */
2525 if (lazy_linking_pending(src_table))
2526 return ERR_PTR(-EBUSY);
2528 for (i = 0; i < src_table->required_opp_count; i++) {
2529 if (src_table->required_opp_tables[i] == dst_table) {
2530 mutex_lock(&src_table->lock);
2532 list_for_each_entry(opp, &src_table->opp_list, node) {
2533 if (opp == src_opp) {
2534 dest_opp = opp->required_opps[i];
2535 dev_pm_opp_get(dest_opp);
2540 mutex_unlock(&src_table->lock);
2545 if (IS_ERR(dest_opp)) {
2546 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__,
2547 src_table, dst_table);
2552 EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp);
2555 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2556 * @src_table: OPP table which has dst_table as one of its required OPP table.
2557 * @dst_table: Required OPP table of the src_table.
2558 * @pstate: Current performance state of the src_table.
2560 * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2561 * "required-opps" property of the OPP (present in @src_table) which has
2562 * performance state set to @pstate.
2564 * Return: Zero or positive performance state on success, otherwise negative
2567 int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2568 struct opp_table *dst_table,
2569 unsigned int pstate)
2571 struct dev_pm_opp *opp;
2572 int dest_pstate = -EINVAL;
2576 * Normally the src_table will have the "required_opps" property set to
2577 * point to one of the OPPs in the dst_table, but in some cases the
2578 * genpd and its master have one to one mapping of performance states
2579 * and so none of them have the "required-opps" property set. Return the
2580 * pstate of the src_table as it is in such cases.
2582 if (!src_table || !src_table->required_opp_count)
2585 /* required-opps not fully initialized yet */
2586 if (lazy_linking_pending(src_table))
2589 for (i = 0; i < src_table->required_opp_count; i++) {
2590 if (src_table->required_opp_tables[i]->np == dst_table->np)
2594 if (unlikely(i == src_table->required_opp_count)) {
2595 pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2596 __func__, src_table, dst_table);
2600 mutex_lock(&src_table->lock);
2602 list_for_each_entry(opp, &src_table->opp_list, node) {
2603 if (opp->pstate == pstate) {
2604 dest_pstate = opp->required_opps[i]->pstate;
2609 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2613 mutex_unlock(&src_table->lock);
2619 * dev_pm_opp_add() - Add an OPP table from a table definitions
2620 * @dev: device for which we do this operation
2621 * @freq: Frequency in Hz for this OPP
2622 * @u_volt: Voltage in uVolts for this OPP
2624 * This function adds an opp definition to the opp table and returns status.
2625 * The opp is made available by default and it can be controlled using
2626 * dev_pm_opp_enable/disable functions.
2630 * Duplicate OPPs (both freq and volt are same) and opp->available
2631 * -EEXIST Freq are same and volt are different OR
2632 * Duplicate OPPs (both freq and volt are same) and !opp->available
2633 * -ENOMEM Memory allocation failure
2635 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
2637 struct opp_table *opp_table;
2640 opp_table = _add_opp_table(dev, true);
2641 if (IS_ERR(opp_table))
2642 return PTR_ERR(opp_table);
2644 /* Fix regulator count for dynamic OPPs */
2645 opp_table->regulator_count = 1;
2647 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
2649 dev_pm_opp_put_opp_table(opp_table);
2653 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
2656 * _opp_set_availability() - helper to set the availability of an opp
2657 * @dev: device for which we do this operation
2658 * @freq: OPP frequency to modify availability
2659 * @availability_req: availability status requested for this opp
2661 * Set the availability of an OPP, opp_{enable,disable} share a common logic
2662 * which is isolated here.
2664 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2665 * copy operation, returns 0 if no modification was done OR modification was
2668 static int _opp_set_availability(struct device *dev, unsigned long freq,
2669 bool availability_req)
2671 struct opp_table *opp_table;
2672 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
2675 /* Find the opp_table */
2676 opp_table = _find_opp_table(dev);
2677 if (IS_ERR(opp_table)) {
2678 r = PTR_ERR(opp_table);
2679 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
2683 mutex_lock(&opp_table->lock);
2685 /* Do we have the frequency? */
2686 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
2687 if (tmp_opp->rate == freq) {
2698 /* Is update really needed? */
2699 if (opp->available == availability_req)
2702 opp->available = availability_req;
2704 dev_pm_opp_get(opp);
2705 mutex_unlock(&opp_table->lock);
2707 /* Notify the change of the OPP availability */
2708 if (availability_req)
2709 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
2712 blocking_notifier_call_chain(&opp_table->head,
2713 OPP_EVENT_DISABLE, opp);
2715 dev_pm_opp_put(opp);
2719 mutex_unlock(&opp_table->lock);
2721 dev_pm_opp_put_opp_table(opp_table);
2726 * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
2727 * @dev: device for which we do this operation
2728 * @freq: OPP frequency to adjust voltage of
2729 * @u_volt: new OPP target voltage
2730 * @u_volt_min: new OPP min voltage
2731 * @u_volt_max: new OPP max voltage
2733 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2734 * copy operation, returns 0 if no modifcation was done OR modification was
2737 int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
2738 unsigned long u_volt, unsigned long u_volt_min,
2739 unsigned long u_volt_max)
2742 struct opp_table *opp_table;
2743 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
2746 /* Find the opp_table */
2747 opp_table = _find_opp_table(dev);
2748 if (IS_ERR(opp_table)) {
2749 r = PTR_ERR(opp_table);
2750 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
2754 mutex_lock(&opp_table->lock);
2756 /* Do we have the frequency? */
2757 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
2758 if (tmp_opp->rate == freq) {
2769 /* Is update really needed? */
2770 if (opp->supplies->u_volt == u_volt)
2773 opp->supplies->u_volt = u_volt;
2774 opp->supplies->u_volt_min = u_volt_min;
2775 opp->supplies->u_volt_max = u_volt_max;
2777 dev_pm_opp_get(opp);
2778 mutex_unlock(&opp_table->lock);
2780 /* Notify the voltage change of the OPP */
2781 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
2784 dev_pm_opp_put(opp);
2785 goto adjust_put_table;
2788 mutex_unlock(&opp_table->lock);
2790 dev_pm_opp_put_opp_table(opp_table);
2793 EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
2796 * dev_pm_opp_enable() - Enable a specific OPP
2797 * @dev: device for which we do this operation
2798 * @freq: OPP frequency to enable
2800 * Enables a provided opp. If the operation is valid, this returns 0, else the
2801 * corresponding error value. It is meant to be used for users an OPP available
2802 * after being temporarily made unavailable with dev_pm_opp_disable.
2804 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2805 * copy operation, returns 0 if no modification was done OR modification was
2808 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
2810 return _opp_set_availability(dev, freq, true);
2812 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
2815 * dev_pm_opp_disable() - Disable a specific OPP
2816 * @dev: device for which we do this operation
2817 * @freq: OPP frequency to disable
2819 * Disables a provided opp. If the operation is valid, this returns
2820 * 0, else the corresponding error value. It is meant to be a temporary
2821 * control by users to make this OPP not available until the circumstances are
2822 * right to make it available again (with a call to dev_pm_opp_enable).
2824 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2825 * copy operation, returns 0 if no modification was done OR modification was
2828 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
2830 return _opp_set_availability(dev, freq, false);
2832 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
2835 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
2836 * @dev: Device for which notifier needs to be registered
2837 * @nb: Notifier block to be registered
2839 * Return: 0 on success or a negative error value.
2841 int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
2843 struct opp_table *opp_table;
2846 opp_table = _find_opp_table(dev);
2847 if (IS_ERR(opp_table))
2848 return PTR_ERR(opp_table);
2850 ret = blocking_notifier_chain_register(&opp_table->head, nb);
2852 dev_pm_opp_put_opp_table(opp_table);
2856 EXPORT_SYMBOL(dev_pm_opp_register_notifier);
2859 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
2860 * @dev: Device for which notifier needs to be unregistered
2861 * @nb: Notifier block to be unregistered
2863 * Return: 0 on success or a negative error value.
2865 int dev_pm_opp_unregister_notifier(struct device *dev,
2866 struct notifier_block *nb)
2868 struct opp_table *opp_table;
2871 opp_table = _find_opp_table(dev);
2872 if (IS_ERR(opp_table))
2873 return PTR_ERR(opp_table);
2875 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
2877 dev_pm_opp_put_opp_table(opp_table);
2881 EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
2884 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
2885 * @dev: device pointer used to lookup OPP table.
2887 * Free both OPPs created using static entries present in DT and the
2888 * dynamically added entries.
2890 void dev_pm_opp_remove_table(struct device *dev)
2892 struct opp_table *opp_table;
2894 /* Check for existing table for 'dev' */
2895 opp_table = _find_opp_table(dev);
2896 if (IS_ERR(opp_table)) {
2897 int error = PTR_ERR(opp_table);
2899 if (error != -ENODEV)
2900 WARN(1, "%s: opp_table: %d\n",
2901 IS_ERR_OR_NULL(dev) ?
2902 "Invalid device" : dev_name(dev),
2908 * Drop the extra reference only if the OPP table was successfully added
2909 * with dev_pm_opp_of_add_table() earlier.
2911 if (_opp_remove_all_static(opp_table))
2912 dev_pm_opp_put_opp_table(opp_table);
2914 /* Drop reference taken by _find_opp_table() */
2915 dev_pm_opp_put_opp_table(opp_table);
2917 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
2920 * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
2921 * @dev: device for which we do this operation
2923 * Sync voltage state of the OPP table regulators.
2925 * Return: 0 on success or a negative error value.
2927 int dev_pm_opp_sync_regulators(struct device *dev)
2929 struct opp_table *opp_table;
2930 struct regulator *reg;
2933 /* Device may not have OPP table */
2934 opp_table = _find_opp_table(dev);
2935 if (IS_ERR(opp_table))
2938 /* Regulator may not be required for the device */
2939 if (unlikely(!opp_table->regulators))
2942 /* Nothing to sync if voltage wasn't changed */
2943 if (!opp_table->enabled)
2946 for (i = 0; i < opp_table->regulator_count; i++) {
2947 reg = opp_table->regulators[i];
2948 ret = regulator_sync_voltage(reg);
2953 /* Drop reference taken by _find_opp_table() */
2954 dev_pm_opp_put_opp_table(opp_table);
2958 EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);