2 * Generic OPP Interface
4 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/clk.h>
17 #include <linux/errno.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/device.h>
21 #include <linux/export.h>
22 #include <linux/pm_domain.h>
23 #include <linux/regulator/consumer.h>
28 * The root of the list of all opp-tables. All opp_table structures branch off
29 * from here, with each opp_table containing the list of opps it supports in
30 * various states of availability.
32 LIST_HEAD(opp_tables);
33 /* Lock to allow exclusive modification to the device and opp lists */
34 DEFINE_MUTEX(opp_table_lock);
36 static struct opp_device *_find_opp_dev(const struct device *dev,
37 struct opp_table *opp_table)
39 struct opp_device *opp_dev;
41 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
42 if (opp_dev->dev == dev)
48 static struct opp_table *_find_opp_table_unlocked(struct device *dev)
50 struct opp_table *opp_table;
53 list_for_each_entry(opp_table, &opp_tables, node) {
54 mutex_lock(&opp_table->lock);
55 found = !!_find_opp_dev(dev, opp_table);
56 mutex_unlock(&opp_table->lock);
59 _get_opp_table_kref(opp_table);
65 return ERR_PTR(-ENODEV);
69 * _find_opp_table() - find opp_table struct using device pointer
70 * @dev: device pointer used to lookup OPP table
72 * Search OPP table for one containing matching device.
74 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
75 * -EINVAL based on type of error.
77 * The callers must call dev_pm_opp_put_opp_table() after the table is used.
79 struct opp_table *_find_opp_table(struct device *dev)
81 struct opp_table *opp_table;
83 if (IS_ERR_OR_NULL(dev)) {
84 pr_err("%s: Invalid parameters\n", __func__);
85 return ERR_PTR(-EINVAL);
88 mutex_lock(&opp_table_lock);
89 opp_table = _find_opp_table_unlocked(dev);
90 mutex_unlock(&opp_table_lock);
96 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
97 * @opp: opp for which voltage has to be returned for
99 * Return: voltage in micro volt corresponding to the opp, else
102 * This is useful only for devices with single power supply.
104 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
106 if (IS_ERR_OR_NULL(opp)) {
107 pr_err("%s: Invalid parameters\n", __func__);
111 return opp->supplies[0].u_volt;
113 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
116 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
117 * @opp: opp for which frequency has to be returned for
119 * Return: frequency in hertz corresponding to the opp, else
122 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
124 if (IS_ERR_OR_NULL(opp) || !opp->available) {
125 pr_err("%s: Invalid parameters\n", __func__);
131 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
134 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
135 * @opp: opp for which turbo mode is being verified
137 * Turbo OPPs are not for normal use, and can be enabled (under certain
138 * conditions) for short duration of times to finish high throughput work
139 * quickly. Running on them for longer times may overheat the chip.
141 * Return: true if opp is turbo opp, else false.
143 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
145 if (IS_ERR_OR_NULL(opp) || !opp->available) {
146 pr_err("%s: Invalid parameters\n", __func__);
152 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
155 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
156 * @dev: device for which we do this operation
158 * Return: This function returns the max clock latency in nanoseconds.
160 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
162 struct opp_table *opp_table;
163 unsigned long clock_latency_ns;
165 opp_table = _find_opp_table(dev);
166 if (IS_ERR(opp_table))
169 clock_latency_ns = opp_table->clock_latency_ns_max;
171 dev_pm_opp_put_opp_table(opp_table);
173 return clock_latency_ns;
175 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
178 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
179 * @dev: device for which we do this operation
181 * Return: This function returns the max voltage latency in nanoseconds.
183 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
185 struct opp_table *opp_table;
186 struct dev_pm_opp *opp;
187 struct regulator *reg;
188 unsigned long latency_ns = 0;
195 opp_table = _find_opp_table(dev);
196 if (IS_ERR(opp_table))
199 count = opp_table->regulator_count;
201 /* Regulator may not be required for the device */
205 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
209 mutex_lock(&opp_table->lock);
211 for (i = 0; i < count; i++) {
215 list_for_each_entry(opp, &opp_table->opp_list, node) {
219 if (opp->supplies[i].u_volt_min < uV[i].min)
220 uV[i].min = opp->supplies[i].u_volt_min;
221 if (opp->supplies[i].u_volt_max > uV[i].max)
222 uV[i].max = opp->supplies[i].u_volt_max;
226 mutex_unlock(&opp_table->lock);
229 * The caller needs to ensure that opp_table (and hence the regulator)
230 * isn't freed, while we are executing this routine.
232 for (i = 0; i < count; i++) {
233 reg = opp_table->regulators[i];
234 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
236 latency_ns += ret * 1000;
241 dev_pm_opp_put_opp_table(opp_table);
245 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
248 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
250 * @dev: device for which we do this operation
252 * Return: This function returns the max transition latency, in nanoseconds, to
253 * switch from one OPP to other.
255 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
257 return dev_pm_opp_get_max_volt_latency(dev) +
258 dev_pm_opp_get_max_clock_latency(dev);
260 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
263 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
264 * @dev: device for which we do this operation
266 * Return: This function returns the frequency of the OPP marked as suspend_opp
267 * if one is available, else returns 0;
269 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
271 struct opp_table *opp_table;
272 unsigned long freq = 0;
274 opp_table = _find_opp_table(dev);
275 if (IS_ERR(opp_table))
278 if (opp_table->suspend_opp && opp_table->suspend_opp->available)
279 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
281 dev_pm_opp_put_opp_table(opp_table);
285 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
287 int _get_opp_count(struct opp_table *opp_table)
289 struct dev_pm_opp *opp;
292 mutex_lock(&opp_table->lock);
294 list_for_each_entry(opp, &opp_table->opp_list, node) {
299 mutex_unlock(&opp_table->lock);
305 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
306 * @dev: device for which we do this operation
308 * Return: This function returns the number of available opps if there are any,
309 * else returns 0 if none or the corresponding error value.
311 int dev_pm_opp_get_opp_count(struct device *dev)
313 struct opp_table *opp_table;
316 opp_table = _find_opp_table(dev);
317 if (IS_ERR(opp_table)) {
318 count = PTR_ERR(opp_table);
319 dev_dbg(dev, "%s: OPP table not found (%d)\n",
324 count = _get_opp_count(opp_table);
325 dev_pm_opp_put_opp_table(opp_table);
329 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
332 * dev_pm_opp_find_freq_exact() - search for an exact frequency
333 * @dev: device for which we do this operation
334 * @freq: frequency to search for
335 * @available: true/false - match for available opp
337 * Return: Searches for exact match in the opp table and returns pointer to the
338 * matching opp if found, else returns ERR_PTR in case of error and should
339 * be handled using IS_ERR. Error return values can be:
340 * EINVAL: for bad pointer
341 * ERANGE: no match found for search
342 * ENODEV: if device not found in list of registered devices
344 * Note: available is a modifier for the search. if available=true, then the
345 * match is for exact matching frequency and is available in the stored OPP
346 * table. if false, the match is for exact frequency which is not available.
348 * This provides a mechanism to enable an opp which is not available currently
349 * or the opposite as well.
351 * The callers are required to call dev_pm_opp_put() for the returned OPP after
354 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
358 struct opp_table *opp_table;
359 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
361 opp_table = _find_opp_table(dev);
362 if (IS_ERR(opp_table)) {
363 int r = PTR_ERR(opp_table);
365 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
369 mutex_lock(&opp_table->lock);
371 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
372 if (temp_opp->available == available &&
373 temp_opp->rate == freq) {
376 /* Increment the reference count of OPP */
382 mutex_unlock(&opp_table->lock);
383 dev_pm_opp_put_opp_table(opp_table);
387 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
389 static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
392 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
394 mutex_lock(&opp_table->lock);
396 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
397 if (temp_opp->available && temp_opp->rate >= *freq) {
401 /* Increment the reference count of OPP */
407 mutex_unlock(&opp_table->lock);
413 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
414 * @dev: device for which we do this operation
415 * @freq: Start frequency
417 * Search for the matching ceil *available* OPP from a starting freq
420 * Return: matching *opp and refreshes *freq accordingly, else returns
421 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
423 * EINVAL: for bad pointer
424 * ERANGE: no match found for search
425 * ENODEV: if device not found in list of registered devices
427 * The callers are required to call dev_pm_opp_put() for the returned OPP after
430 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
433 struct opp_table *opp_table;
434 struct dev_pm_opp *opp;
437 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
438 return ERR_PTR(-EINVAL);
441 opp_table = _find_opp_table(dev);
442 if (IS_ERR(opp_table))
443 return ERR_CAST(opp_table);
445 opp = _find_freq_ceil(opp_table, freq);
447 dev_pm_opp_put_opp_table(opp_table);
451 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
454 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
455 * @dev: device for which we do this operation
456 * @freq: Start frequency
458 * Search for the matching floor *available* OPP from a starting freq
461 * Return: matching *opp and refreshes *freq accordingly, else returns
462 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
464 * EINVAL: for bad pointer
465 * ERANGE: no match found for search
466 * ENODEV: if device not found in list of registered devices
468 * The callers are required to call dev_pm_opp_put() for the returned OPP after
471 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
474 struct opp_table *opp_table;
475 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
478 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
479 return ERR_PTR(-EINVAL);
482 opp_table = _find_opp_table(dev);
483 if (IS_ERR(opp_table))
484 return ERR_CAST(opp_table);
486 mutex_lock(&opp_table->lock);
488 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
489 if (temp_opp->available) {
490 /* go to the next node, before choosing prev */
491 if (temp_opp->rate > *freq)
498 /* Increment the reference count of OPP */
501 mutex_unlock(&opp_table->lock);
502 dev_pm_opp_put_opp_table(opp_table);
509 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
511 static int _set_opp_voltage(struct device *dev, struct regulator *reg,
512 struct dev_pm_opp_supply *supply)
516 /* Regulator not available for device */
518 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
523 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
524 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
526 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
527 supply->u_volt, supply->u_volt_max);
529 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
530 __func__, supply->u_volt_min, supply->u_volt,
531 supply->u_volt_max, ret);
537 _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
538 unsigned long old_freq, unsigned long freq)
542 ret = clk_set_rate(clk, freq);
544 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
552 _generic_set_opp_domain(struct device *dev, struct clk *clk,
553 unsigned long old_freq, unsigned long freq,
554 unsigned int old_pstate, unsigned int new_pstate)
558 /* Scaling up? Scale domain performance state before frequency */
559 if (freq > old_freq) {
560 ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
565 ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
567 goto restore_domain_state;
569 /* Scaling down? Scale domain performance state after frequency */
570 if (freq < old_freq) {
571 ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
579 if (_generic_set_opp_clk_only(dev, clk, freq, old_freq))
580 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
582 restore_domain_state:
584 dev_pm_genpd_set_performance_state(dev, old_pstate);
589 static int _generic_set_opp_regulator(const struct opp_table *opp_table,
591 unsigned long old_freq,
593 struct dev_pm_opp_supply *old_supply,
594 struct dev_pm_opp_supply *new_supply)
596 struct regulator *reg = opp_table->regulators[0];
599 /* This function only supports single regulator per device */
600 if (WARN_ON(opp_table->regulator_count > 1)) {
601 dev_err(dev, "multiple regulators are not supported\n");
605 /* Scaling up? Scale voltage before frequency */
606 if (freq >= old_freq) {
607 ret = _set_opp_voltage(dev, reg, new_supply);
609 goto restore_voltage;
612 /* Change frequency */
613 ret = _generic_set_opp_clk_only(dev, opp_table->clk, old_freq, freq);
615 goto restore_voltage;
617 /* Scaling down? Scale voltage after frequency */
618 if (freq < old_freq) {
619 ret = _set_opp_voltage(dev, reg, new_supply);
627 if (_generic_set_opp_clk_only(dev, opp_table->clk, freq, old_freq))
628 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
631 /* This shouldn't harm even if the voltages weren't updated earlier */
633 _set_opp_voltage(dev, reg, old_supply);
639 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
640 * @dev: device for which we do this operation
641 * @target_freq: frequency to achieve
643 * This configures the power-supplies and clock source to the levels specified
644 * by the OPP corresponding to the target_freq.
646 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
648 struct opp_table *opp_table;
649 unsigned long freq, old_freq;
650 struct dev_pm_opp *old_opp, *opp;
654 if (unlikely(!target_freq)) {
655 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
660 opp_table = _find_opp_table(dev);
661 if (IS_ERR(opp_table)) {
662 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
663 return PTR_ERR(opp_table);
666 clk = opp_table->clk;
668 dev_err(dev, "%s: No clock available for the device\n",
674 freq = clk_round_rate(clk, target_freq);
678 old_freq = clk_get_rate(clk);
680 /* Return early if nothing to do */
681 if (old_freq == freq) {
682 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
688 old_opp = _find_freq_ceil(opp_table, &old_freq);
689 if (IS_ERR(old_opp)) {
690 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
691 __func__, old_freq, PTR_ERR(old_opp));
694 opp = _find_freq_ceil(opp_table, &freq);
697 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
698 __func__, freq, ret);
702 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
705 /* Only frequency scaling */
706 if (!opp_table->regulators) {
708 * We don't support devices with both regulator and
709 * domain performance-state for now.
711 if (opp_table->genpd_performance_state)
712 ret = _generic_set_opp_domain(dev, clk, old_freq, freq,
713 IS_ERR(old_opp) ? 0 : old_opp->pstate,
716 ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
717 } else if (!opp_table->set_opp) {
718 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
719 IS_ERR(old_opp) ? NULL : old_opp->supplies,
722 struct dev_pm_set_opp_data *data;
724 data = opp_table->set_opp_data;
725 data->regulators = opp_table->regulators;
726 data->regulator_count = opp_table->regulator_count;
730 data->old_opp.rate = old_freq;
731 size = sizeof(*opp->supplies) * opp_table->regulator_count;
733 memset(data->old_opp.supplies, 0, size);
735 memcpy(data->old_opp.supplies, old_opp->supplies, size);
737 data->new_opp.rate = freq;
738 memcpy(data->new_opp.supplies, opp->supplies, size);
740 ret = opp_table->set_opp(data);
745 if (!IS_ERR(old_opp))
746 dev_pm_opp_put(old_opp);
748 dev_pm_opp_put_opp_table(opp_table);
751 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
753 /* OPP-dev Helpers */
754 static void _remove_opp_dev(struct opp_device *opp_dev,
755 struct opp_table *opp_table)
757 opp_debug_unregister(opp_dev, opp_table);
758 list_del(&opp_dev->node);
762 struct opp_device *_add_opp_dev(const struct device *dev,
763 struct opp_table *opp_table)
765 struct opp_device *opp_dev;
768 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
772 /* Initialize opp-dev */
775 mutex_lock(&opp_table->lock);
776 list_add(&opp_dev->node, &opp_table->dev_list);
778 /* Create debugfs entries for the opp_table */
779 ret = opp_debug_register(opp_dev, opp_table);
781 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
783 mutex_unlock(&opp_table->lock);
788 static struct opp_table *_allocate_opp_table(struct device *dev, int index)
790 struct opp_table *opp_table;
791 struct opp_device *opp_dev;
795 * Allocate a new OPP table. In the infrequent case where a new
796 * device is needed to be added, we pay this penalty.
798 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
802 mutex_init(&opp_table->lock);
803 INIT_LIST_HEAD(&opp_table->dev_list);
805 opp_dev = _add_opp_dev(dev, opp_table);
811 _of_init_opp_table(opp_table, dev, index);
813 /* Find clk for the device */
814 opp_table->clk = clk_get(dev, NULL);
815 if (IS_ERR(opp_table->clk)) {
816 ret = PTR_ERR(opp_table->clk);
817 if (ret != -EPROBE_DEFER)
818 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
822 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
823 INIT_LIST_HEAD(&opp_table->opp_list);
824 kref_init(&opp_table->kref);
826 /* Secure the device table modification */
827 list_add(&opp_table->node, &opp_tables);
831 void _get_opp_table_kref(struct opp_table *opp_table)
833 kref_get(&opp_table->kref);
836 static struct opp_table *_opp_get_opp_table(struct device *dev, int index)
838 struct opp_table *opp_table;
840 /* Hold our table modification lock here */
841 mutex_lock(&opp_table_lock);
843 opp_table = _find_opp_table_unlocked(dev);
844 if (!IS_ERR(opp_table))
847 opp_table = _allocate_opp_table(dev, index);
850 mutex_unlock(&opp_table_lock);
855 struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
857 return _opp_get_opp_table(dev, 0);
859 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
861 struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev,
864 return _opp_get_opp_table(dev, index);
867 static void _opp_table_kref_release(struct kref *kref)
869 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
870 struct opp_device *opp_dev;
873 if (!IS_ERR(opp_table->clk))
874 clk_put(opp_table->clk);
877 * No need to take opp_table->lock here as we are guaranteed that no
878 * references to the OPP table are taken at this point.
880 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
883 _remove_opp_dev(opp_dev, opp_table);
885 /* dev_list must be empty now */
886 WARN_ON(!list_empty(&opp_table->dev_list));
888 mutex_destroy(&opp_table->lock);
889 list_del(&opp_table->node);
892 mutex_unlock(&opp_table_lock);
895 void _opp_remove_all_static(struct opp_table *opp_table)
897 struct dev_pm_opp *opp, *tmp;
899 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
904 opp_table->parsed_static_opps = false;
907 static void _opp_table_list_kref_release(struct kref *kref)
909 struct opp_table *opp_table = container_of(kref, struct opp_table,
912 _opp_remove_all_static(opp_table);
913 mutex_unlock(&opp_table_lock);
916 void _put_opp_list_kref(struct opp_table *opp_table)
918 kref_put_mutex(&opp_table->list_kref, _opp_table_list_kref_release,
922 void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
924 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
927 EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
929 void _opp_free(struct dev_pm_opp *opp)
934 static void _opp_kref_release(struct kref *kref)
936 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
937 struct opp_table *opp_table = opp->opp_table;
940 * Notify the changes in the availability of the operable
941 * frequency/voltage list.
943 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
944 opp_debug_remove_one(opp);
945 list_del(&opp->node);
948 mutex_unlock(&opp_table->lock);
951 void dev_pm_opp_get(struct dev_pm_opp *opp)
953 kref_get(&opp->kref);
956 void dev_pm_opp_put(struct dev_pm_opp *opp)
958 kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
960 EXPORT_SYMBOL_GPL(dev_pm_opp_put);
963 * dev_pm_opp_remove() - Remove an OPP from OPP table
964 * @dev: device for which we do this operation
965 * @freq: OPP to remove with matching 'freq'
967 * This function removes an opp from the opp table.
969 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
971 struct dev_pm_opp *opp;
972 struct opp_table *opp_table;
975 opp_table = _find_opp_table(dev);
976 if (IS_ERR(opp_table))
979 mutex_lock(&opp_table->lock);
981 list_for_each_entry(opp, &opp_table->opp_list, node) {
982 if (opp->rate == freq) {
988 mutex_unlock(&opp_table->lock);
993 /* Drop the reference taken by dev_pm_opp_add() */
994 dev_pm_opp_put_opp_table(opp_table);
996 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1000 /* Drop the reference taken by _find_opp_table() */
1001 dev_pm_opp_put_opp_table(opp_table);
1003 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1005 struct dev_pm_opp *_opp_allocate(struct opp_table *table)
1007 struct dev_pm_opp *opp;
1008 int count, supply_size;
1010 /* Allocate space for at least one supply */
1011 count = table->regulator_count ? table->regulator_count : 1;
1012 supply_size = sizeof(*opp->supplies) * count;
1014 /* allocate new OPP node and supplies structures */
1015 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
1019 /* Put the supplies at the end of the OPP structure as an empty array */
1020 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1021 INIT_LIST_HEAD(&opp->node);
1026 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
1027 struct opp_table *opp_table)
1029 struct regulator *reg;
1032 for (i = 0; i < opp_table->regulator_count; i++) {
1033 reg = opp_table->regulators[i];
1035 if (!regulator_is_supported_voltage(reg,
1036 opp->supplies[i].u_volt_min,
1037 opp->supplies[i].u_volt_max)) {
1038 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1039 __func__, opp->supplies[i].u_volt_min,
1040 opp->supplies[i].u_volt_max);
1048 static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1049 struct opp_table *opp_table,
1050 struct list_head **head)
1052 struct dev_pm_opp *opp;
1055 * Insert new OPP in order of increasing frequency and discard if
1058 * Need to use &opp_table->opp_list in the condition part of the 'for'
1059 * loop, don't replace it with head otherwise it will become an infinite
1062 list_for_each_entry(opp, &opp_table->opp_list, node) {
1063 if (new_opp->rate > opp->rate) {
1068 if (new_opp->rate < opp->rate)
1071 /* Duplicate OPPs */
1072 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1073 __func__, opp->rate, opp->supplies[0].u_volt,
1074 opp->available, new_opp->rate,
1075 new_opp->supplies[0].u_volt, new_opp->available);
1077 /* Should we compare voltages for all regulators here ? */
1078 return opp->available &&
1079 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1087 * 0: On success. And appropriate error message for duplicate OPPs.
1088 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1089 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1090 * sure we don't print error messages unnecessarily if different parts of
1091 * kernel try to initialize the OPP table.
1092 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1093 * should be considered an error by the callers of _opp_add().
1095 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1096 struct opp_table *opp_table, bool rate_not_available)
1098 struct list_head *head;
1101 mutex_lock(&opp_table->lock);
1102 head = &opp_table->opp_list;
1104 if (likely(!rate_not_available)) {
1105 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1107 mutex_unlock(&opp_table->lock);
1112 list_add(&new_opp->node, head);
1113 mutex_unlock(&opp_table->lock);
1115 new_opp->opp_table = opp_table;
1116 kref_init(&new_opp->kref);
1118 ret = opp_debug_create_one(new_opp, opp_table);
1120 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1123 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
1124 new_opp->available = false;
1125 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1126 __func__, new_opp->rate);
1133 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
1134 * @opp_table: OPP table
1135 * @dev: device for which we do this operation
1136 * @freq: Frequency in Hz for this OPP
1137 * @u_volt: Voltage in uVolts for this OPP
1138 * @dynamic: Dynamically added OPPs.
1140 * This function adds an opp definition to the opp table and returns status.
1141 * The opp is made available by default and it can be controlled using
1142 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1144 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1145 * and freed by dev_pm_opp_of_remove_table.
1149 * Duplicate OPPs (both freq and volt are same) and opp->available
1150 * -EEXIST Freq are same and volt are different OR
1151 * Duplicate OPPs (both freq and volt are same) and !opp->available
1152 * -ENOMEM Memory allocation failure
1154 int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1155 unsigned long freq, long u_volt, bool dynamic)
1157 struct dev_pm_opp *new_opp;
1161 new_opp = _opp_allocate(opp_table);
1165 /* populate the opp table */
1166 new_opp->rate = freq;
1167 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
1168 new_opp->supplies[0].u_volt = u_volt;
1169 new_opp->supplies[0].u_volt_min = u_volt - tol;
1170 new_opp->supplies[0].u_volt_max = u_volt + tol;
1171 new_opp->available = true;
1172 new_opp->dynamic = dynamic;
1174 ret = _opp_add(dev, new_opp, opp_table, false);
1176 /* Don't return error for duplicate OPPs */
1183 * Notify the changes in the availability of the operable
1184 * frequency/voltage list.
1186 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
1196 * dev_pm_opp_set_supported_hw() - Set supported platforms
1197 * @dev: Device for which supported-hw has to be set.
1198 * @versions: Array of hierarchy of versions to match.
1199 * @count: Number of elements in the array.
1201 * This is required only for the V2 bindings, and it enables a platform to
1202 * specify the hierarchy of versions it supports. OPP layer will then enable
1203 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1206 struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1207 const u32 *versions, unsigned int count)
1209 struct opp_table *opp_table;
1211 opp_table = dev_pm_opp_get_opp_table(dev);
1213 return ERR_PTR(-ENOMEM);
1215 /* Make sure there are no concurrent readers while updating opp_table */
1216 WARN_ON(!list_empty(&opp_table->opp_list));
1218 /* Another CPU that shares the OPP table has set the property ? */
1219 if (opp_table->supported_hw)
1222 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
1224 if (!opp_table->supported_hw) {
1225 dev_pm_opp_put_opp_table(opp_table);
1226 return ERR_PTR(-ENOMEM);
1229 opp_table->supported_hw_count = count;
1233 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1236 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
1237 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
1239 * This is required only for the V2 bindings, and is called for a matching
1240 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
1241 * will not be freed.
1243 void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
1245 /* Make sure there are no concurrent readers while updating opp_table */
1246 WARN_ON(!list_empty(&opp_table->opp_list));
1248 kfree(opp_table->supported_hw);
1249 opp_table->supported_hw = NULL;
1250 opp_table->supported_hw_count = 0;
1252 dev_pm_opp_put_opp_table(opp_table);
1254 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1257 * dev_pm_opp_set_prop_name() - Set prop-extn name
1258 * @dev: Device for which the prop-name has to be set.
1259 * @name: name to postfix to properties.
1261 * This is required only for the V2 bindings, and it enables a platform to
1262 * specify the extn to be used for certain property names. The properties to
1263 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1264 * should postfix the property name with -<name> while looking for them.
1266 struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1268 struct opp_table *opp_table;
1270 opp_table = dev_pm_opp_get_opp_table(dev);
1272 return ERR_PTR(-ENOMEM);
1274 /* Make sure there are no concurrent readers while updating opp_table */
1275 WARN_ON(!list_empty(&opp_table->opp_list));
1277 /* Another CPU that shares the OPP table has set the property ? */
1278 if (opp_table->prop_name)
1281 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1282 if (!opp_table->prop_name) {
1283 dev_pm_opp_put_opp_table(opp_table);
1284 return ERR_PTR(-ENOMEM);
1289 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1292 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
1293 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
1295 * This is required only for the V2 bindings, and is called for a matching
1296 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
1297 * will not be freed.
1299 void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
1301 /* Make sure there are no concurrent readers while updating opp_table */
1302 WARN_ON(!list_empty(&opp_table->opp_list));
1304 kfree(opp_table->prop_name);
1305 opp_table->prop_name = NULL;
1307 dev_pm_opp_put_opp_table(opp_table);
1309 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1311 static int _allocate_set_opp_data(struct opp_table *opp_table)
1313 struct dev_pm_set_opp_data *data;
1314 int len, count = opp_table->regulator_count;
1316 if (WARN_ON(!count))
1319 /* space for set_opp_data */
1320 len = sizeof(*data);
1322 /* space for old_opp.supplies and new_opp.supplies */
1323 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1325 data = kzalloc(len, GFP_KERNEL);
1329 data->old_opp.supplies = (void *)(data + 1);
1330 data->new_opp.supplies = data->old_opp.supplies + count;
1332 opp_table->set_opp_data = data;
1337 static void _free_set_opp_data(struct opp_table *opp_table)
1339 kfree(opp_table->set_opp_data);
1340 opp_table->set_opp_data = NULL;
1344 * dev_pm_opp_set_regulators() - Set regulator names for the device
1345 * @dev: Device for which regulator name is being set.
1346 * @names: Array of pointers to the names of the regulator.
1347 * @count: Number of regulators.
1349 * In order to support OPP switching, OPP layer needs to know the name of the
1350 * device's regulators, as the core would be required to switch voltages as
1353 * This must be called before any OPPs are initialized for the device.
1355 struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1356 const char * const names[],
1359 struct opp_table *opp_table;
1360 struct regulator *reg;
1363 opp_table = dev_pm_opp_get_opp_table(dev);
1365 return ERR_PTR(-ENOMEM);
1367 /* This should be called before OPPs are initialized */
1368 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1373 /* Another CPU that shares the OPP table has set the regulators ? */
1374 if (opp_table->regulators)
1377 opp_table->regulators = kmalloc_array(count,
1378 sizeof(*opp_table->regulators),
1380 if (!opp_table->regulators) {
1385 for (i = 0; i < count; i++) {
1386 reg = regulator_get_optional(dev, names[i]);
1389 if (ret != -EPROBE_DEFER)
1390 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1391 __func__, names[i], ret);
1392 goto free_regulators;
1395 opp_table->regulators[i] = reg;
1398 opp_table->regulator_count = count;
1400 /* Allocate block only once to pass to set_opp() routines */
1401 ret = _allocate_set_opp_data(opp_table);
1403 goto free_regulators;
1409 regulator_put(opp_table->regulators[--i]);
1411 kfree(opp_table->regulators);
1412 opp_table->regulators = NULL;
1413 opp_table->regulator_count = 0;
1415 dev_pm_opp_put_opp_table(opp_table);
1417 return ERR_PTR(ret);
1419 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
1422 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1423 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
1425 void dev_pm_opp_put_regulators(struct opp_table *opp_table)
1429 if (!opp_table->regulators)
1432 /* Make sure there are no concurrent readers while updating opp_table */
1433 WARN_ON(!list_empty(&opp_table->opp_list));
1435 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1436 regulator_put(opp_table->regulators[i]);
1438 _free_set_opp_data(opp_table);
1440 kfree(opp_table->regulators);
1441 opp_table->regulators = NULL;
1442 opp_table->regulator_count = 0;
1445 dev_pm_opp_put_opp_table(opp_table);
1447 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
1450 * dev_pm_opp_set_clkname() - Set clk name for the device
1451 * @dev: Device for which clk name is being set.
1454 * In order to support OPP switching, OPP layer needs to get pointer to the
1455 * clock for the device. Simple cases work fine without using this routine (i.e.
1456 * by passing connection-id as NULL), but for a device with multiple clocks
1457 * available, the OPP core needs to know the exact name of the clk to use.
1459 * This must be called before any OPPs are initialized for the device.
1461 struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
1463 struct opp_table *opp_table;
1466 opp_table = dev_pm_opp_get_opp_table(dev);
1468 return ERR_PTR(-ENOMEM);
1470 /* This should be called before OPPs are initialized */
1471 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1476 /* Already have default clk set, free it */
1477 if (!IS_ERR(opp_table->clk))
1478 clk_put(opp_table->clk);
1480 /* Find clk for the device */
1481 opp_table->clk = clk_get(dev, name);
1482 if (IS_ERR(opp_table->clk)) {
1483 ret = PTR_ERR(opp_table->clk);
1484 if (ret != -EPROBE_DEFER) {
1485 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
1494 dev_pm_opp_put_opp_table(opp_table);
1496 return ERR_PTR(ret);
1498 EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
1501 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1502 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1504 void dev_pm_opp_put_clkname(struct opp_table *opp_table)
1506 /* Make sure there are no concurrent readers while updating opp_table */
1507 WARN_ON(!list_empty(&opp_table->opp_list));
1509 clk_put(opp_table->clk);
1510 opp_table->clk = ERR_PTR(-EINVAL);
1512 dev_pm_opp_put_opp_table(opp_table);
1514 EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
1517 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1518 * @dev: Device for which the helper is getting registered.
1519 * @set_opp: Custom set OPP helper.
1521 * This is useful to support complex platforms (like platforms with multiple
1522 * regulators per device), instead of the generic OPP set rate helper.
1524 * This must be called before any OPPs are initialized for the device.
1526 struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
1527 int (*set_opp)(struct dev_pm_set_opp_data *data))
1529 struct opp_table *opp_table;
1532 return ERR_PTR(-EINVAL);
1534 opp_table = dev_pm_opp_get_opp_table(dev);
1536 return ERR_PTR(-ENOMEM);
1538 /* This should be called before OPPs are initialized */
1539 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1540 dev_pm_opp_put_opp_table(opp_table);
1541 return ERR_PTR(-EBUSY);
1544 /* Another CPU that shares the OPP table has set the helper ? */
1545 if (!opp_table->set_opp)
1546 opp_table->set_opp = set_opp;
1550 EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1553 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
1555 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
1557 * Release resources blocked for platform specific set_opp helper.
1559 void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
1561 /* Make sure there are no concurrent readers while updating opp_table */
1562 WARN_ON(!list_empty(&opp_table->opp_list));
1564 opp_table->set_opp = NULL;
1565 dev_pm_opp_put_opp_table(opp_table);
1567 EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
1570 * dev_pm_opp_add() - Add an OPP table from a table definitions
1571 * @dev: device for which we do this operation
1572 * @freq: Frequency in Hz for this OPP
1573 * @u_volt: Voltage in uVolts for this OPP
1575 * This function adds an opp definition to the opp table and returns status.
1576 * The opp is made available by default and it can be controlled using
1577 * dev_pm_opp_enable/disable functions.
1581 * Duplicate OPPs (both freq and volt are same) and opp->available
1582 * -EEXIST Freq are same and volt are different OR
1583 * Duplicate OPPs (both freq and volt are same) and !opp->available
1584 * -ENOMEM Memory allocation failure
1586 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1588 struct opp_table *opp_table;
1591 opp_table = dev_pm_opp_get_opp_table(dev);
1595 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
1597 dev_pm_opp_put_opp_table(opp_table);
1601 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1604 * _opp_set_availability() - helper to set the availability of an opp
1605 * @dev: device for which we do this operation
1606 * @freq: OPP frequency to modify availability
1607 * @availability_req: availability status requested for this opp
1609 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1610 * which is isolated here.
1612 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1613 * copy operation, returns 0 if no modification was done OR modification was
1616 static int _opp_set_availability(struct device *dev, unsigned long freq,
1617 bool availability_req)
1619 struct opp_table *opp_table;
1620 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
1623 /* Find the opp_table */
1624 opp_table = _find_opp_table(dev);
1625 if (IS_ERR(opp_table)) {
1626 r = PTR_ERR(opp_table);
1627 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1631 mutex_lock(&opp_table->lock);
1633 /* Do we have the frequency? */
1634 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
1635 if (tmp_opp->rate == freq) {
1646 /* Is update really needed? */
1647 if (opp->available == availability_req)
1650 opp->available = availability_req;
1652 dev_pm_opp_get(opp);
1653 mutex_unlock(&opp_table->lock);
1655 /* Notify the change of the OPP availability */
1656 if (availability_req)
1657 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
1660 blocking_notifier_call_chain(&opp_table->head,
1661 OPP_EVENT_DISABLE, opp);
1663 dev_pm_opp_put(opp);
1667 mutex_unlock(&opp_table->lock);
1669 dev_pm_opp_put_opp_table(opp_table);
1674 * dev_pm_opp_enable() - Enable a specific OPP
1675 * @dev: device for which we do this operation
1676 * @freq: OPP frequency to enable
1678 * Enables a provided opp. If the operation is valid, this returns 0, else the
1679 * corresponding error value. It is meant to be used for users an OPP available
1680 * after being temporarily made unavailable with dev_pm_opp_disable.
1682 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1683 * copy operation, returns 0 if no modification was done OR modification was
1686 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
1688 return _opp_set_availability(dev, freq, true);
1690 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
1693 * dev_pm_opp_disable() - Disable a specific OPP
1694 * @dev: device for which we do this operation
1695 * @freq: OPP frequency to disable
1697 * Disables a provided opp. If the operation is valid, this returns
1698 * 0, else the corresponding error value. It is meant to be a temporary
1699 * control by users to make this OPP not available until the circumstances are
1700 * right to make it available again (with a call to dev_pm_opp_enable).
1702 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1703 * copy operation, returns 0 if no modification was done OR modification was
1706 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
1708 return _opp_set_availability(dev, freq, false);
1710 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
1713 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
1714 * @dev: Device for which notifier needs to be registered
1715 * @nb: Notifier block to be registered
1717 * Return: 0 on success or a negative error value.
1719 int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
1721 struct opp_table *opp_table;
1724 opp_table = _find_opp_table(dev);
1725 if (IS_ERR(opp_table))
1726 return PTR_ERR(opp_table);
1728 ret = blocking_notifier_chain_register(&opp_table->head, nb);
1730 dev_pm_opp_put_opp_table(opp_table);
1734 EXPORT_SYMBOL(dev_pm_opp_register_notifier);
1737 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
1738 * @dev: Device for which notifier needs to be unregistered
1739 * @nb: Notifier block to be unregistered
1741 * Return: 0 on success or a negative error value.
1743 int dev_pm_opp_unregister_notifier(struct device *dev,
1744 struct notifier_block *nb)
1746 struct opp_table *opp_table;
1749 opp_table = _find_opp_table(dev);
1750 if (IS_ERR(opp_table))
1751 return PTR_ERR(opp_table);
1753 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
1755 dev_pm_opp_put_opp_table(opp_table);
1759 EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
1762 * Free OPPs either created using static entries present in DT.
1764 void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev)
1766 /* Protect dev_list */
1767 mutex_lock(&opp_table->lock);
1769 /* Find if opp_table manages a single device */
1770 if (list_is_singular(&opp_table->dev_list)) {
1771 /* Free static OPPs */
1772 _put_opp_list_kref(opp_table);
1775 * The OPP table is getting removed, drop the performance state
1778 if (opp_table->genpd_performance_state)
1779 dev_pm_genpd_set_performance_state(dev, 0);
1781 _put_opp_list_kref(opp_table);
1782 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
1785 mutex_unlock(&opp_table->lock);
1788 void _dev_pm_opp_find_and_remove_table(struct device *dev)
1790 struct opp_table *opp_table;
1792 /* Check for existing table for 'dev' */
1793 opp_table = _find_opp_table(dev);
1794 if (IS_ERR(opp_table)) {
1795 int error = PTR_ERR(opp_table);
1797 if (error != -ENODEV)
1798 WARN(1, "%s: opp_table: %d\n",
1799 IS_ERR_OR_NULL(dev) ?
1800 "Invalid device" : dev_name(dev),
1805 _dev_pm_opp_remove_table(opp_table, dev);
1807 dev_pm_opp_put_opp_table(opp_table);
1811 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
1812 * @dev: device pointer used to lookup OPP table.
1814 * Free both OPPs created using static entries present in DT and the
1815 * dynamically added entries.
1817 void dev_pm_opp_remove_table(struct device *dev)
1819 _dev_pm_opp_find_and_remove_table(dev);
1821 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);