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 = NULL, *iter;
1490 struct opp_table *opp_table;
1492 opp_table = _find_opp_table(dev);
1493 if (IS_ERR(opp_table))
1496 mutex_lock(&opp_table->lock);
1498 list_for_each_entry(iter, &opp_table->opp_list, node) {
1499 if (iter->rate == freq) {
1505 mutex_unlock(&opp_table->lock);
1508 dev_pm_opp_put(opp);
1510 /* Drop the reference taken by dev_pm_opp_add() */
1511 dev_pm_opp_put_opp_table(opp_table);
1513 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1517 /* Drop the reference taken by _find_opp_table() */
1518 dev_pm_opp_put_opp_table(opp_table);
1520 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1522 static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
1525 struct dev_pm_opp *opp = NULL, *temp;
1527 mutex_lock(&opp_table->lock);
1528 list_for_each_entry(temp, &opp_table->opp_list, node) {
1530 * Refcount must be dropped only once for each OPP by OPP core,
1531 * do that with help of "removed" flag.
1533 if (!temp->removed && dynamic == temp->dynamic) {
1539 mutex_unlock(&opp_table->lock);
1544 * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to
1545 * happen lock less to avoid circular dependency issues. This routine must be
1546 * called without the opp_table->lock held.
1548 static void _opp_remove_all(struct opp_table *opp_table, bool dynamic)
1550 struct dev_pm_opp *opp;
1552 while ((opp = _opp_get_next(opp_table, dynamic))) {
1553 opp->removed = true;
1554 dev_pm_opp_put(opp);
1556 /* Drop the references taken by dev_pm_opp_add() */
1558 dev_pm_opp_put_opp_table(opp_table);
1562 bool _opp_remove_all_static(struct opp_table *opp_table)
1564 mutex_lock(&opp_table->lock);
1566 if (!opp_table->parsed_static_opps) {
1567 mutex_unlock(&opp_table->lock);
1571 if (--opp_table->parsed_static_opps) {
1572 mutex_unlock(&opp_table->lock);
1576 mutex_unlock(&opp_table->lock);
1578 _opp_remove_all(opp_table, false);
1583 * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
1584 * @dev: device for which we do this operation
1586 * This function removes all dynamically created OPPs from the opp table.
1588 void dev_pm_opp_remove_all_dynamic(struct device *dev)
1590 struct opp_table *opp_table;
1592 opp_table = _find_opp_table(dev);
1593 if (IS_ERR(opp_table))
1596 _opp_remove_all(opp_table, true);
1598 /* Drop the reference taken by _find_opp_table() */
1599 dev_pm_opp_put_opp_table(opp_table);
1601 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
1603 struct dev_pm_opp *_opp_allocate(struct opp_table *table)
1605 struct dev_pm_opp *opp;
1606 int supply_count, supply_size, icc_size;
1608 /* Allocate space for at least one supply */
1609 supply_count = table->regulator_count > 0 ? table->regulator_count : 1;
1610 supply_size = sizeof(*opp->supplies) * supply_count;
1611 icc_size = sizeof(*opp->bandwidth) * table->path_count;
1613 /* allocate new OPP node and supplies structures */
1614 opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL);
1619 /* Put the supplies at the end of the OPP structure as an empty array */
1620 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1622 opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count);
1623 INIT_LIST_HEAD(&opp->node);
1628 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
1629 struct opp_table *opp_table)
1631 struct regulator *reg;
1634 if (!opp_table->regulators)
1637 for (i = 0; i < opp_table->regulator_count; i++) {
1638 reg = opp_table->regulators[i];
1640 if (!regulator_is_supported_voltage(reg,
1641 opp->supplies[i].u_volt_min,
1642 opp->supplies[i].u_volt_max)) {
1643 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1644 __func__, opp->supplies[i].u_volt_min,
1645 opp->supplies[i].u_volt_max);
1653 int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
1655 if (opp1->rate != opp2->rate)
1656 return opp1->rate < opp2->rate ? -1 : 1;
1657 if (opp1->bandwidth && opp2->bandwidth &&
1658 opp1->bandwidth[0].peak != opp2->bandwidth[0].peak)
1659 return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1;
1660 if (opp1->level != opp2->level)
1661 return opp1->level < opp2->level ? -1 : 1;
1665 static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1666 struct opp_table *opp_table,
1667 struct list_head **head)
1669 struct dev_pm_opp *opp;
1673 * Insert new OPP in order of increasing frequency and discard if
1676 * Need to use &opp_table->opp_list in the condition part of the 'for'
1677 * loop, don't replace it with head otherwise it will become an infinite
1680 list_for_each_entry(opp, &opp_table->opp_list, node) {
1681 opp_cmp = _opp_compare_key(new_opp, opp);
1690 /* Duplicate OPPs */
1691 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1692 __func__, opp->rate, opp->supplies[0].u_volt,
1693 opp->available, new_opp->rate,
1694 new_opp->supplies[0].u_volt, new_opp->available);
1696 /* Should we compare voltages for all regulators here ? */
1697 return opp->available &&
1698 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1704 void _required_opps_available(struct dev_pm_opp *opp, int count)
1708 for (i = 0; i < count; i++) {
1709 if (opp->required_opps[i]->available)
1712 opp->available = false;
1713 pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n",
1714 __func__, opp->required_opps[i]->np, opp->rate);
1721 * 0: On success. And appropriate error message for duplicate OPPs.
1722 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1723 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1724 * sure we don't print error messages unnecessarily if different parts of
1725 * kernel try to initialize the OPP table.
1726 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1727 * should be considered an error by the callers of _opp_add().
1729 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1730 struct opp_table *opp_table, bool rate_not_available)
1732 struct list_head *head;
1735 mutex_lock(&opp_table->lock);
1736 head = &opp_table->opp_list;
1738 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1740 mutex_unlock(&opp_table->lock);
1744 list_add(&new_opp->node, head);
1745 mutex_unlock(&opp_table->lock);
1747 new_opp->opp_table = opp_table;
1748 kref_init(&new_opp->kref);
1750 opp_debug_create_one(new_opp, opp_table);
1752 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
1753 new_opp->available = false;
1754 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1755 __func__, new_opp->rate);
1758 /* required-opps not fully initialized yet */
1759 if (lazy_linking_pending(opp_table))
1762 _required_opps_available(new_opp, opp_table->required_opp_count);
1768 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
1769 * @opp_table: OPP table
1770 * @dev: device for which we do this operation
1771 * @freq: Frequency in Hz for this OPP
1772 * @u_volt: Voltage in uVolts for this OPP
1773 * @dynamic: Dynamically added OPPs.
1775 * This function adds an opp definition to the opp table and returns status.
1776 * The opp is made available by default and it can be controlled using
1777 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1779 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1780 * and freed by dev_pm_opp_of_remove_table.
1784 * Duplicate OPPs (both freq and volt are same) and opp->available
1785 * -EEXIST Freq are same and volt are different OR
1786 * Duplicate OPPs (both freq and volt are same) and !opp->available
1787 * -ENOMEM Memory allocation failure
1789 int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1790 unsigned long freq, long u_volt, bool dynamic)
1792 struct dev_pm_opp *new_opp;
1796 new_opp = _opp_allocate(opp_table);
1800 /* populate the opp table */
1801 new_opp->rate = freq;
1802 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
1803 new_opp->supplies[0].u_volt = u_volt;
1804 new_opp->supplies[0].u_volt_min = u_volt - tol;
1805 new_opp->supplies[0].u_volt_max = u_volt + tol;
1806 new_opp->available = true;
1807 new_opp->dynamic = dynamic;
1809 ret = _opp_add(dev, new_opp, opp_table, false);
1811 /* Don't return error for duplicate OPPs */
1818 * Notify the changes in the availability of the operable
1819 * frequency/voltage list.
1821 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
1831 * dev_pm_opp_set_supported_hw() - Set supported platforms
1832 * @dev: Device for which supported-hw has to be set.
1833 * @versions: Array of hierarchy of versions to match.
1834 * @count: Number of elements in the array.
1836 * This is required only for the V2 bindings, and it enables a platform to
1837 * specify the hierarchy of versions it supports. OPP layer will then enable
1838 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1841 struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1842 const u32 *versions, unsigned int count)
1844 struct opp_table *opp_table;
1846 opp_table = _add_opp_table(dev, false);
1847 if (IS_ERR(opp_table))
1850 /* Make sure there are no concurrent readers while updating opp_table */
1851 WARN_ON(!list_empty(&opp_table->opp_list));
1853 /* Another CPU that shares the OPP table has set the property ? */
1854 if (opp_table->supported_hw)
1857 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
1859 if (!opp_table->supported_hw) {
1860 dev_pm_opp_put_opp_table(opp_table);
1861 return ERR_PTR(-ENOMEM);
1864 opp_table->supported_hw_count = count;
1868 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1871 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
1872 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
1874 * This is required only for the V2 bindings, and is called for a matching
1875 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
1876 * will not be freed.
1878 void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
1880 if (unlikely(!opp_table))
1883 kfree(opp_table->supported_hw);
1884 opp_table->supported_hw = NULL;
1885 opp_table->supported_hw_count = 0;
1887 dev_pm_opp_put_opp_table(opp_table);
1889 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1891 static void devm_pm_opp_supported_hw_release(void *data)
1893 dev_pm_opp_put_supported_hw(data);
1897 * devm_pm_opp_set_supported_hw() - Set supported platforms
1898 * @dev: Device for which supported-hw has to be set.
1899 * @versions: Array of hierarchy of versions to match.
1900 * @count: Number of elements in the array.
1902 * This is a resource-managed variant of dev_pm_opp_set_supported_hw().
1904 * Return: 0 on success and errorno otherwise.
1906 int devm_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
1909 struct opp_table *opp_table;
1911 opp_table = dev_pm_opp_set_supported_hw(dev, versions, count);
1912 if (IS_ERR(opp_table))
1913 return PTR_ERR(opp_table);
1915 return devm_add_action_or_reset(dev, devm_pm_opp_supported_hw_release,
1918 EXPORT_SYMBOL_GPL(devm_pm_opp_set_supported_hw);
1921 * dev_pm_opp_set_prop_name() - Set prop-extn name
1922 * @dev: Device for which the prop-name has to be set.
1923 * @name: name to postfix to properties.
1925 * This is required only for the V2 bindings, and it enables a platform to
1926 * specify the extn to be used for certain property names. The properties to
1927 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1928 * should postfix the property name with -<name> while looking for them.
1930 struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1932 struct opp_table *opp_table;
1934 opp_table = _add_opp_table(dev, false);
1935 if (IS_ERR(opp_table))
1938 /* Make sure there are no concurrent readers while updating opp_table */
1939 WARN_ON(!list_empty(&opp_table->opp_list));
1941 /* Another CPU that shares the OPP table has set the property ? */
1942 if (opp_table->prop_name)
1945 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1946 if (!opp_table->prop_name) {
1947 dev_pm_opp_put_opp_table(opp_table);
1948 return ERR_PTR(-ENOMEM);
1953 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1956 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
1957 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
1959 * This is required only for the V2 bindings, and is called for a matching
1960 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
1961 * will not be freed.
1963 void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
1965 if (unlikely(!opp_table))
1968 kfree(opp_table->prop_name);
1969 opp_table->prop_name = NULL;
1971 dev_pm_opp_put_opp_table(opp_table);
1973 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1976 * dev_pm_opp_set_regulators() - Set regulator names for the device
1977 * @dev: Device for which regulator name is being set.
1978 * @names: Array of pointers to the names of the regulator.
1979 * @count: Number of regulators.
1981 * In order to support OPP switching, OPP layer needs to know the name of the
1982 * device's regulators, as the core would be required to switch voltages as
1985 * This must be called before any OPPs are initialized for the device.
1987 struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1988 const char * const names[],
1991 struct dev_pm_opp_supply *supplies;
1992 struct opp_table *opp_table;
1993 struct regulator *reg;
1996 opp_table = _add_opp_table(dev, false);
1997 if (IS_ERR(opp_table))
2000 /* This should be called before OPPs are initialized */
2001 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
2006 /* Another CPU that shares the OPP table has set the regulators ? */
2007 if (opp_table->regulators)
2010 opp_table->regulators = kmalloc_array(count,
2011 sizeof(*opp_table->regulators),
2013 if (!opp_table->regulators) {
2018 for (i = 0; i < count; i++) {
2019 reg = regulator_get_optional(dev, names[i]);
2021 ret = dev_err_probe(dev, PTR_ERR(reg),
2022 "%s: no regulator (%s) found\n",
2023 __func__, names[i]);
2024 goto free_regulators;
2027 opp_table->regulators[i] = reg;
2030 opp_table->regulator_count = count;
2032 supplies = kmalloc_array(count * 2, sizeof(*supplies), GFP_KERNEL);
2035 goto free_regulators;
2038 mutex_lock(&opp_table->lock);
2039 opp_table->sod_supplies = supplies;
2040 if (opp_table->set_opp_data) {
2041 opp_table->set_opp_data->old_opp.supplies = supplies;
2042 opp_table->set_opp_data->new_opp.supplies = supplies + count;
2044 mutex_unlock(&opp_table->lock);
2050 regulator_put(opp_table->regulators[--i]);
2052 kfree(opp_table->regulators);
2053 opp_table->regulators = NULL;
2054 opp_table->regulator_count = -1;
2056 dev_pm_opp_put_opp_table(opp_table);
2058 return ERR_PTR(ret);
2060 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
2063 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
2064 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
2066 void dev_pm_opp_put_regulators(struct opp_table *opp_table)
2070 if (unlikely(!opp_table))
2073 if (!opp_table->regulators)
2076 if (opp_table->enabled) {
2077 for (i = opp_table->regulator_count - 1; i >= 0; i--)
2078 regulator_disable(opp_table->regulators[i]);
2081 for (i = opp_table->regulator_count - 1; i >= 0; i--)
2082 regulator_put(opp_table->regulators[i]);
2084 mutex_lock(&opp_table->lock);
2085 if (opp_table->set_opp_data) {
2086 opp_table->set_opp_data->old_opp.supplies = NULL;
2087 opp_table->set_opp_data->new_opp.supplies = NULL;
2090 kfree(opp_table->sod_supplies);
2091 opp_table->sod_supplies = NULL;
2092 mutex_unlock(&opp_table->lock);
2094 kfree(opp_table->regulators);
2095 opp_table->regulators = NULL;
2096 opp_table->regulator_count = -1;
2099 dev_pm_opp_put_opp_table(opp_table);
2101 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
2103 static void devm_pm_opp_regulators_release(void *data)
2105 dev_pm_opp_put_regulators(data);
2109 * devm_pm_opp_set_regulators() - Set regulator names for the device
2110 * @dev: Device for which regulator name is being set.
2111 * @names: Array of pointers to the names of the regulator.
2112 * @count: Number of regulators.
2114 * This is a resource-managed variant of dev_pm_opp_set_regulators().
2116 * Return: 0 on success and errorno otherwise.
2118 int devm_pm_opp_set_regulators(struct device *dev,
2119 const char * const names[],
2122 struct opp_table *opp_table;
2124 opp_table = dev_pm_opp_set_regulators(dev, names, count);
2125 if (IS_ERR(opp_table))
2126 return PTR_ERR(opp_table);
2128 return devm_add_action_or_reset(dev, devm_pm_opp_regulators_release,
2131 EXPORT_SYMBOL_GPL(devm_pm_opp_set_regulators);
2134 * dev_pm_opp_set_clkname() - Set clk name for the device
2135 * @dev: Device for which clk name is being set.
2138 * In order to support OPP switching, OPP layer needs to get pointer to the
2139 * clock for the device. Simple cases work fine without using this routine (i.e.
2140 * by passing connection-id as NULL), but for a device with multiple clocks
2141 * available, the OPP core needs to know the exact name of the clk to use.
2143 * This must be called before any OPPs are initialized for the device.
2145 struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
2147 struct opp_table *opp_table;
2150 opp_table = _add_opp_table(dev, false);
2151 if (IS_ERR(opp_table))
2154 /* This should be called before OPPs are initialized */
2155 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
2160 /* clk shouldn't be initialized at this point */
2161 if (WARN_ON(opp_table->clk)) {
2166 /* Find clk for the device */
2167 opp_table->clk = clk_get(dev, name);
2168 if (IS_ERR(opp_table->clk)) {
2169 ret = dev_err_probe(dev, PTR_ERR(opp_table->clk),
2170 "%s: Couldn't find clock\n", __func__);
2177 dev_pm_opp_put_opp_table(opp_table);
2179 return ERR_PTR(ret);
2181 EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
2184 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
2185 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
2187 void dev_pm_opp_put_clkname(struct opp_table *opp_table)
2189 if (unlikely(!opp_table))
2192 clk_put(opp_table->clk);
2193 opp_table->clk = ERR_PTR(-EINVAL);
2195 dev_pm_opp_put_opp_table(opp_table);
2197 EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
2199 static void devm_pm_opp_clkname_release(void *data)
2201 dev_pm_opp_put_clkname(data);
2205 * devm_pm_opp_set_clkname() - Set clk name for the device
2206 * @dev: Device for which clk name is being set.
2209 * This is a resource-managed variant of dev_pm_opp_set_clkname().
2211 * Return: 0 on success and errorno otherwise.
2213 int devm_pm_opp_set_clkname(struct device *dev, const char *name)
2215 struct opp_table *opp_table;
2217 opp_table = dev_pm_opp_set_clkname(dev, name);
2218 if (IS_ERR(opp_table))
2219 return PTR_ERR(opp_table);
2221 return devm_add_action_or_reset(dev, devm_pm_opp_clkname_release,
2224 EXPORT_SYMBOL_GPL(devm_pm_opp_set_clkname);
2227 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
2228 * @dev: Device for which the helper is getting registered.
2229 * @set_opp: Custom set OPP helper.
2231 * This is useful to support complex platforms (like platforms with multiple
2232 * regulators per device), instead of the generic OPP set rate helper.
2234 * This must be called before any OPPs are initialized for the device.
2236 struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
2237 int (*set_opp)(struct dev_pm_set_opp_data *data))
2239 struct dev_pm_set_opp_data *data;
2240 struct opp_table *opp_table;
2243 return ERR_PTR(-EINVAL);
2245 opp_table = _add_opp_table(dev, false);
2246 if (IS_ERR(opp_table))
2249 /* This should be called before OPPs are initialized */
2250 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
2251 dev_pm_opp_put_opp_table(opp_table);
2252 return ERR_PTR(-EBUSY);
2255 /* Another CPU that shares the OPP table has set the helper ? */
2256 if (opp_table->set_opp)
2259 data = kzalloc(sizeof(*data), GFP_KERNEL);
2261 return ERR_PTR(-ENOMEM);
2263 mutex_lock(&opp_table->lock);
2264 opp_table->set_opp_data = data;
2265 if (opp_table->sod_supplies) {
2266 data->old_opp.supplies = opp_table->sod_supplies;
2267 data->new_opp.supplies = opp_table->sod_supplies +
2268 opp_table->regulator_count;
2270 mutex_unlock(&opp_table->lock);
2272 opp_table->set_opp = set_opp;
2276 EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
2279 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
2281 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
2283 * Release resources blocked for platform specific set_opp helper.
2285 void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
2287 if (unlikely(!opp_table))
2290 opp_table->set_opp = NULL;
2292 mutex_lock(&opp_table->lock);
2293 kfree(opp_table->set_opp_data);
2294 opp_table->set_opp_data = NULL;
2295 mutex_unlock(&opp_table->lock);
2297 dev_pm_opp_put_opp_table(opp_table);
2299 EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
2301 static void devm_pm_opp_unregister_set_opp_helper(void *data)
2303 dev_pm_opp_unregister_set_opp_helper(data);
2307 * devm_pm_opp_register_set_opp_helper() - Register custom set OPP helper
2308 * @dev: Device for which the helper is getting registered.
2309 * @set_opp: Custom set OPP helper.
2311 * This is a resource-managed version of dev_pm_opp_register_set_opp_helper().
2313 * Return: 0 on success and errorno otherwise.
2315 int devm_pm_opp_register_set_opp_helper(struct device *dev,
2316 int (*set_opp)(struct dev_pm_set_opp_data *data))
2318 struct opp_table *opp_table;
2320 opp_table = dev_pm_opp_register_set_opp_helper(dev, set_opp);
2321 if (IS_ERR(opp_table))
2322 return PTR_ERR(opp_table);
2324 return devm_add_action_or_reset(dev, devm_pm_opp_unregister_set_opp_helper,
2327 EXPORT_SYMBOL_GPL(devm_pm_opp_register_set_opp_helper);
2329 static void _opp_detach_genpd(struct opp_table *opp_table)
2333 if (!opp_table->genpd_virt_devs)
2336 for (index = 0; index < opp_table->required_opp_count; index++) {
2337 if (!opp_table->genpd_virt_devs[index])
2340 dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
2341 opp_table->genpd_virt_devs[index] = NULL;
2344 kfree(opp_table->genpd_virt_devs);
2345 opp_table->genpd_virt_devs = NULL;
2349 * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
2350 * @dev: Consumer device for which the genpd is getting attached.
2351 * @names: Null terminated array of pointers containing names of genpd to attach.
2352 * @virt_devs: Pointer to return the array of virtual devices.
2354 * Multiple generic power domains for a device are supported with the help of
2355 * virtual genpd devices, which are created for each consumer device - genpd
2356 * pair. These are the device structures which are attached to the power domain
2357 * and are required by the OPP core to set the performance state of the genpd.
2358 * The same API also works for the case where single genpd is available and so
2359 * we don't need to support that separately.
2361 * This helper will normally be called by the consumer driver of the device
2362 * "dev", as only that has details of the genpd names.
2364 * This helper needs to be called once with a list of all genpd to attach.
2365 * Otherwise the original device structure will be used instead by the OPP core.
2367 * The order of entries in the names array must match the order in which
2368 * "required-opps" are added in DT.
2370 struct opp_table *dev_pm_opp_attach_genpd(struct device *dev,
2371 const char * const *names, struct device ***virt_devs)
2373 struct opp_table *opp_table;
2374 struct device *virt_dev;
2375 int index = 0, ret = -EINVAL;
2376 const char * const *name = names;
2378 opp_table = _add_opp_table(dev, false);
2379 if (IS_ERR(opp_table))
2382 if (opp_table->genpd_virt_devs)
2386 * If the genpd's OPP table isn't already initialized, parsing of the
2387 * required-opps fail for dev. We should retry this after genpd's OPP
2390 if (!opp_table->required_opp_count) {
2391 ret = -EPROBE_DEFER;
2395 mutex_lock(&opp_table->genpd_virt_dev_lock);
2397 opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
2398 sizeof(*opp_table->genpd_virt_devs),
2400 if (!opp_table->genpd_virt_devs)
2404 if (index >= opp_table->required_opp_count) {
2405 dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
2406 *name, opp_table->required_opp_count, index);
2410 virt_dev = dev_pm_domain_attach_by_name(dev, *name);
2411 if (IS_ERR(virt_dev)) {
2412 ret = PTR_ERR(virt_dev);
2413 dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
2417 opp_table->genpd_virt_devs[index] = virt_dev;
2423 *virt_devs = opp_table->genpd_virt_devs;
2424 mutex_unlock(&opp_table->genpd_virt_dev_lock);
2429 _opp_detach_genpd(opp_table);
2431 mutex_unlock(&opp_table->genpd_virt_dev_lock);
2434 dev_pm_opp_put_opp_table(opp_table);
2436 return ERR_PTR(ret);
2438 EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd);
2441 * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device.
2442 * @opp_table: OPP table returned by dev_pm_opp_attach_genpd().
2444 * This detaches the genpd(s), resets the virtual device pointers, and puts the
2447 void dev_pm_opp_detach_genpd(struct opp_table *opp_table)
2449 if (unlikely(!opp_table))
2453 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
2456 mutex_lock(&opp_table->genpd_virt_dev_lock);
2457 _opp_detach_genpd(opp_table);
2458 mutex_unlock(&opp_table->genpd_virt_dev_lock);
2460 dev_pm_opp_put_opp_table(opp_table);
2462 EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd);
2464 static void devm_pm_opp_detach_genpd(void *data)
2466 dev_pm_opp_detach_genpd(data);
2470 * devm_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual
2472 * @dev: Consumer device for which the genpd is getting attached.
2473 * @names: Null terminated array of pointers containing names of genpd to attach.
2474 * @virt_devs: Pointer to return the array of virtual devices.
2476 * This is a resource-managed version of dev_pm_opp_attach_genpd().
2478 * Return: 0 on success and errorno otherwise.
2480 int devm_pm_opp_attach_genpd(struct device *dev, const char * const *names,
2481 struct device ***virt_devs)
2483 struct opp_table *opp_table;
2485 opp_table = dev_pm_opp_attach_genpd(dev, names, virt_devs);
2486 if (IS_ERR(opp_table))
2487 return PTR_ERR(opp_table);
2489 return devm_add_action_or_reset(dev, devm_pm_opp_detach_genpd,
2492 EXPORT_SYMBOL_GPL(devm_pm_opp_attach_genpd);
2495 * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
2496 * @src_table: OPP table which has @dst_table as one of its required OPP table.
2497 * @dst_table: Required OPP table of the @src_table.
2498 * @src_opp: OPP from the @src_table.
2500 * This function returns the OPP (present in @dst_table) pointed out by the
2501 * "required-opps" property of the @src_opp (present in @src_table).
2503 * The callers are required to call dev_pm_opp_put() for the returned OPP after
2506 * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise.
2508 struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
2509 struct opp_table *dst_table,
2510 struct dev_pm_opp *src_opp)
2512 struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
2515 if (!src_table || !dst_table || !src_opp ||
2516 !src_table->required_opp_tables)
2517 return ERR_PTR(-EINVAL);
2519 /* required-opps not fully initialized yet */
2520 if (lazy_linking_pending(src_table))
2521 return ERR_PTR(-EBUSY);
2523 for (i = 0; i < src_table->required_opp_count; i++) {
2524 if (src_table->required_opp_tables[i] == dst_table) {
2525 mutex_lock(&src_table->lock);
2527 list_for_each_entry(opp, &src_table->opp_list, node) {
2528 if (opp == src_opp) {
2529 dest_opp = opp->required_opps[i];
2530 dev_pm_opp_get(dest_opp);
2535 mutex_unlock(&src_table->lock);
2540 if (IS_ERR(dest_opp)) {
2541 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__,
2542 src_table, dst_table);
2547 EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp);
2550 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
2551 * @src_table: OPP table which has dst_table as one of its required OPP table.
2552 * @dst_table: Required OPP table of the src_table.
2553 * @pstate: Current performance state of the src_table.
2555 * This Returns pstate of the OPP (present in @dst_table) pointed out by the
2556 * "required-opps" property of the OPP (present in @src_table) which has
2557 * performance state set to @pstate.
2559 * Return: Zero or positive performance state on success, otherwise negative
2562 int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
2563 struct opp_table *dst_table,
2564 unsigned int pstate)
2566 struct dev_pm_opp *opp;
2567 int dest_pstate = -EINVAL;
2571 * Normally the src_table will have the "required_opps" property set to
2572 * point to one of the OPPs in the dst_table, but in some cases the
2573 * genpd and its master have one to one mapping of performance states
2574 * and so none of them have the "required-opps" property set. Return the
2575 * pstate of the src_table as it is in such cases.
2577 if (!src_table || !src_table->required_opp_count)
2580 /* required-opps not fully initialized yet */
2581 if (lazy_linking_pending(src_table))
2584 for (i = 0; i < src_table->required_opp_count; i++) {
2585 if (src_table->required_opp_tables[i]->np == dst_table->np)
2589 if (unlikely(i == src_table->required_opp_count)) {
2590 pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
2591 __func__, src_table, dst_table);
2595 mutex_lock(&src_table->lock);
2597 list_for_each_entry(opp, &src_table->opp_list, node) {
2598 if (opp->pstate == pstate) {
2599 dest_pstate = opp->required_opps[i]->pstate;
2604 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
2608 mutex_unlock(&src_table->lock);
2614 * dev_pm_opp_add() - Add an OPP table from a table definitions
2615 * @dev: device for which we do this operation
2616 * @freq: Frequency in Hz for this OPP
2617 * @u_volt: Voltage in uVolts for this OPP
2619 * This function adds an opp definition to the opp table and returns status.
2620 * The opp is made available by default and it can be controlled using
2621 * dev_pm_opp_enable/disable functions.
2625 * Duplicate OPPs (both freq and volt are same) and opp->available
2626 * -EEXIST Freq are same and volt are different OR
2627 * Duplicate OPPs (both freq and volt are same) and !opp->available
2628 * -ENOMEM Memory allocation failure
2630 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
2632 struct opp_table *opp_table;
2635 opp_table = _add_opp_table(dev, true);
2636 if (IS_ERR(opp_table))
2637 return PTR_ERR(opp_table);
2639 /* Fix regulator count for dynamic OPPs */
2640 opp_table->regulator_count = 1;
2642 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
2644 dev_pm_opp_put_opp_table(opp_table);
2648 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
2651 * _opp_set_availability() - helper to set the availability of an opp
2652 * @dev: device for which we do this operation
2653 * @freq: OPP frequency to modify availability
2654 * @availability_req: availability status requested for this opp
2656 * Set the availability of an OPP, opp_{enable,disable} share a common logic
2657 * which is isolated here.
2659 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2660 * copy operation, returns 0 if no modification was done OR modification was
2663 static int _opp_set_availability(struct device *dev, unsigned long freq,
2664 bool availability_req)
2666 struct opp_table *opp_table;
2667 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
2670 /* Find the opp_table */
2671 opp_table = _find_opp_table(dev);
2672 if (IS_ERR(opp_table)) {
2673 r = PTR_ERR(opp_table);
2674 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
2678 mutex_lock(&opp_table->lock);
2680 /* Do we have the frequency? */
2681 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
2682 if (tmp_opp->rate == freq) {
2693 /* Is update really needed? */
2694 if (opp->available == availability_req)
2697 opp->available = availability_req;
2699 dev_pm_opp_get(opp);
2700 mutex_unlock(&opp_table->lock);
2702 /* Notify the change of the OPP availability */
2703 if (availability_req)
2704 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
2707 blocking_notifier_call_chain(&opp_table->head,
2708 OPP_EVENT_DISABLE, opp);
2710 dev_pm_opp_put(opp);
2714 mutex_unlock(&opp_table->lock);
2716 dev_pm_opp_put_opp_table(opp_table);
2721 * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
2722 * @dev: device for which we do this operation
2723 * @freq: OPP frequency to adjust voltage of
2724 * @u_volt: new OPP target voltage
2725 * @u_volt_min: new OPP min voltage
2726 * @u_volt_max: new OPP max voltage
2728 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2729 * copy operation, returns 0 if no modifcation was done OR modification was
2732 int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
2733 unsigned long u_volt, unsigned long u_volt_min,
2734 unsigned long u_volt_max)
2737 struct opp_table *opp_table;
2738 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
2741 /* Find the opp_table */
2742 opp_table = _find_opp_table(dev);
2743 if (IS_ERR(opp_table)) {
2744 r = PTR_ERR(opp_table);
2745 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
2749 mutex_lock(&opp_table->lock);
2751 /* Do we have the frequency? */
2752 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
2753 if (tmp_opp->rate == freq) {
2764 /* Is update really needed? */
2765 if (opp->supplies->u_volt == u_volt)
2768 opp->supplies->u_volt = u_volt;
2769 opp->supplies->u_volt_min = u_volt_min;
2770 opp->supplies->u_volt_max = u_volt_max;
2772 dev_pm_opp_get(opp);
2773 mutex_unlock(&opp_table->lock);
2775 /* Notify the voltage change of the OPP */
2776 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
2779 dev_pm_opp_put(opp);
2780 goto adjust_put_table;
2783 mutex_unlock(&opp_table->lock);
2785 dev_pm_opp_put_opp_table(opp_table);
2788 EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
2791 * dev_pm_opp_enable() - Enable a specific OPP
2792 * @dev: device for which we do this operation
2793 * @freq: OPP frequency to enable
2795 * Enables a provided opp. If the operation is valid, this returns 0, else the
2796 * corresponding error value. It is meant to be used for users an OPP available
2797 * after being temporarily made unavailable with dev_pm_opp_disable.
2799 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2800 * copy operation, returns 0 if no modification was done OR modification was
2803 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
2805 return _opp_set_availability(dev, freq, true);
2807 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
2810 * dev_pm_opp_disable() - Disable a specific OPP
2811 * @dev: device for which we do this operation
2812 * @freq: OPP frequency to disable
2814 * Disables a provided opp. If the operation is valid, this returns
2815 * 0, else the corresponding error value. It is meant to be a temporary
2816 * control by users to make this OPP not available until the circumstances are
2817 * right to make it available again (with a call to dev_pm_opp_enable).
2819 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2820 * copy operation, returns 0 if no modification was done OR modification was
2823 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
2825 return _opp_set_availability(dev, freq, false);
2827 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
2830 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
2831 * @dev: Device for which notifier needs to be registered
2832 * @nb: Notifier block to be registered
2834 * Return: 0 on success or a negative error value.
2836 int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
2838 struct opp_table *opp_table;
2841 opp_table = _find_opp_table(dev);
2842 if (IS_ERR(opp_table))
2843 return PTR_ERR(opp_table);
2845 ret = blocking_notifier_chain_register(&opp_table->head, nb);
2847 dev_pm_opp_put_opp_table(opp_table);
2851 EXPORT_SYMBOL(dev_pm_opp_register_notifier);
2854 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
2855 * @dev: Device for which notifier needs to be unregistered
2856 * @nb: Notifier block to be unregistered
2858 * Return: 0 on success or a negative error value.
2860 int dev_pm_opp_unregister_notifier(struct device *dev,
2861 struct notifier_block *nb)
2863 struct opp_table *opp_table;
2866 opp_table = _find_opp_table(dev);
2867 if (IS_ERR(opp_table))
2868 return PTR_ERR(opp_table);
2870 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
2872 dev_pm_opp_put_opp_table(opp_table);
2876 EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
2879 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
2880 * @dev: device pointer used to lookup OPP table.
2882 * Free both OPPs created using static entries present in DT and the
2883 * dynamically added entries.
2885 void dev_pm_opp_remove_table(struct device *dev)
2887 struct opp_table *opp_table;
2889 /* Check for existing table for 'dev' */
2890 opp_table = _find_opp_table(dev);
2891 if (IS_ERR(opp_table)) {
2892 int error = PTR_ERR(opp_table);
2894 if (error != -ENODEV)
2895 WARN(1, "%s: opp_table: %d\n",
2896 IS_ERR_OR_NULL(dev) ?
2897 "Invalid device" : dev_name(dev),
2903 * Drop the extra reference only if the OPP table was successfully added
2904 * with dev_pm_opp_of_add_table() earlier.
2906 if (_opp_remove_all_static(opp_table))
2907 dev_pm_opp_put_opp_table(opp_table);
2909 /* Drop reference taken by _find_opp_table() */
2910 dev_pm_opp_put_opp_table(opp_table);
2912 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
2915 * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
2916 * @dev: device for which we do this operation
2918 * Sync voltage state of the OPP table regulators.
2920 * Return: 0 on success or a negative error value.
2922 int dev_pm_opp_sync_regulators(struct device *dev)
2924 struct opp_table *opp_table;
2925 struct regulator *reg;
2928 /* Device may not have OPP table */
2929 opp_table = _find_opp_table(dev);
2930 if (IS_ERR(opp_table))
2933 /* Regulator may not be required for the device */
2934 if (unlikely(!opp_table->regulators))
2937 /* Nothing to sync if voltage wasn't changed */
2938 if (!opp_table->enabled)
2941 for (i = 0; i < opp_table->regulator_count; i++) {
2942 reg = opp_table->regulators[i];
2943 ret = regulator_sync_voltage(reg);
2948 /* Drop reference taken by _find_opp_table() */
2949 dev_pm_opp_put_opp_table(opp_table);
2953 EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);