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_get_level() - Gets the level corresponding to an available opp
135 * @opp: opp for which level value has to be returned for
137 * Return: level read from device tree corresponding to the opp, else
140 unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
142 if (IS_ERR_OR_NULL(opp) || !opp->available) {
143 pr_err("%s: Invalid parameters\n", __func__);
149 EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
152 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
153 * @opp: opp for which turbo mode is being verified
155 * Turbo OPPs are not for normal use, and can be enabled (under certain
156 * conditions) for short duration of times to finish high throughput work
157 * quickly. Running on them for longer times may overheat the chip.
159 * Return: true if opp is turbo opp, else false.
161 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
163 if (IS_ERR_OR_NULL(opp) || !opp->available) {
164 pr_err("%s: Invalid parameters\n", __func__);
170 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
173 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
174 * @dev: device for which we do this operation
176 * Return: This function returns the max clock latency in nanoseconds.
178 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
180 struct opp_table *opp_table;
181 unsigned long clock_latency_ns;
183 opp_table = _find_opp_table(dev);
184 if (IS_ERR(opp_table))
187 clock_latency_ns = opp_table->clock_latency_ns_max;
189 dev_pm_opp_put_opp_table(opp_table);
191 return clock_latency_ns;
193 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
196 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
197 * @dev: device for which we do this operation
199 * Return: This function returns the max voltage latency in nanoseconds.
201 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
203 struct opp_table *opp_table;
204 struct dev_pm_opp *opp;
205 struct regulator *reg;
206 unsigned long latency_ns = 0;
213 opp_table = _find_opp_table(dev);
214 if (IS_ERR(opp_table))
217 /* Regulator may not be required for the device */
218 if (!opp_table->regulators)
221 count = opp_table->regulator_count;
223 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
227 mutex_lock(&opp_table->lock);
229 for (i = 0; i < count; i++) {
233 list_for_each_entry(opp, &opp_table->opp_list, node) {
237 if (opp->supplies[i].u_volt_min < uV[i].min)
238 uV[i].min = opp->supplies[i].u_volt_min;
239 if (opp->supplies[i].u_volt_max > uV[i].max)
240 uV[i].max = opp->supplies[i].u_volt_max;
244 mutex_unlock(&opp_table->lock);
247 * The caller needs to ensure that opp_table (and hence the regulator)
248 * isn't freed, while we are executing this routine.
250 for (i = 0; i < count; i++) {
251 reg = opp_table->regulators[i];
252 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
254 latency_ns += ret * 1000;
259 dev_pm_opp_put_opp_table(opp_table);
263 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
266 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
268 * @dev: device for which we do this operation
270 * Return: This function returns the max transition latency, in nanoseconds, to
271 * switch from one OPP to other.
273 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
275 return dev_pm_opp_get_max_volt_latency(dev) +
276 dev_pm_opp_get_max_clock_latency(dev);
278 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
281 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
282 * @dev: device for which we do this operation
284 * Return: This function returns the frequency of the OPP marked as suspend_opp
285 * if one is available, else returns 0;
287 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
289 struct opp_table *opp_table;
290 unsigned long freq = 0;
292 opp_table = _find_opp_table(dev);
293 if (IS_ERR(opp_table))
296 if (opp_table->suspend_opp && opp_table->suspend_opp->available)
297 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
299 dev_pm_opp_put_opp_table(opp_table);
303 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
305 int _get_opp_count(struct opp_table *opp_table)
307 struct dev_pm_opp *opp;
310 mutex_lock(&opp_table->lock);
312 list_for_each_entry(opp, &opp_table->opp_list, node) {
317 mutex_unlock(&opp_table->lock);
323 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
324 * @dev: device for which we do this operation
326 * Return: This function returns the number of available opps if there are any,
327 * else returns 0 if none or the corresponding error value.
329 int dev_pm_opp_get_opp_count(struct device *dev)
331 struct opp_table *opp_table;
334 opp_table = _find_opp_table(dev);
335 if (IS_ERR(opp_table)) {
336 count = PTR_ERR(opp_table);
337 dev_dbg(dev, "%s: OPP table not found (%d)\n",
342 count = _get_opp_count(opp_table);
343 dev_pm_opp_put_opp_table(opp_table);
347 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
350 * dev_pm_opp_find_freq_exact() - search for an exact frequency
351 * @dev: device for which we do this operation
352 * @freq: frequency to search for
353 * @available: true/false - match for available opp
355 * Return: Searches for exact match in the opp table and returns pointer to the
356 * matching opp if found, else returns ERR_PTR in case of error and should
357 * be handled using IS_ERR. Error return values can be:
358 * EINVAL: for bad pointer
359 * ERANGE: no match found for search
360 * ENODEV: if device not found in list of registered devices
362 * Note: available is a modifier for the search. if available=true, then the
363 * match is for exact matching frequency and is available in the stored OPP
364 * table. if false, the match is for exact frequency which is not available.
366 * This provides a mechanism to enable an opp which is not available currently
367 * or the opposite as well.
369 * The callers are required to call dev_pm_opp_put() for the returned OPP after
372 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
376 struct opp_table *opp_table;
377 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
379 opp_table = _find_opp_table(dev);
380 if (IS_ERR(opp_table)) {
381 int r = PTR_ERR(opp_table);
383 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
387 mutex_lock(&opp_table->lock);
389 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
390 if (temp_opp->available == available &&
391 temp_opp->rate == freq) {
394 /* Increment the reference count of OPP */
400 mutex_unlock(&opp_table->lock);
401 dev_pm_opp_put_opp_table(opp_table);
405 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
407 static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
410 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
412 mutex_lock(&opp_table->lock);
414 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
415 if (temp_opp->available && temp_opp->rate >= *freq) {
419 /* Increment the reference count of OPP */
425 mutex_unlock(&opp_table->lock);
431 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
432 * @dev: device for which we do this operation
433 * @freq: Start frequency
435 * Search for the matching ceil *available* OPP from a starting freq
438 * Return: matching *opp and refreshes *freq accordingly, else returns
439 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
441 * EINVAL: for bad pointer
442 * ERANGE: no match found for search
443 * ENODEV: if device not found in list of registered devices
445 * The callers are required to call dev_pm_opp_put() for the returned OPP after
448 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
451 struct opp_table *opp_table;
452 struct dev_pm_opp *opp;
455 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
456 return ERR_PTR(-EINVAL);
459 opp_table = _find_opp_table(dev);
460 if (IS_ERR(opp_table))
461 return ERR_CAST(opp_table);
463 opp = _find_freq_ceil(opp_table, freq);
465 dev_pm_opp_put_opp_table(opp_table);
469 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
472 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
473 * @dev: device for which we do this operation
474 * @freq: Start frequency
476 * Search for the matching floor *available* OPP from a starting freq
479 * Return: matching *opp and refreshes *freq accordingly, else returns
480 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
482 * EINVAL: for bad pointer
483 * ERANGE: no match found for search
484 * ENODEV: if device not found in list of registered devices
486 * The callers are required to call dev_pm_opp_put() for the returned OPP after
489 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
492 struct opp_table *opp_table;
493 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
496 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
497 return ERR_PTR(-EINVAL);
500 opp_table = _find_opp_table(dev);
501 if (IS_ERR(opp_table))
502 return ERR_CAST(opp_table);
504 mutex_lock(&opp_table->lock);
506 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
507 if (temp_opp->available) {
508 /* go to the next node, before choosing prev */
509 if (temp_opp->rate > *freq)
516 /* Increment the reference count of OPP */
519 mutex_unlock(&opp_table->lock);
520 dev_pm_opp_put_opp_table(opp_table);
527 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
530 * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for
532 * @dev: Device for which we do this operation.
533 * @u_volt: Target voltage.
535 * Search for OPP with highest (ceil) frequency and has voltage <= u_volt.
537 * Return: matching *opp, else returns ERR_PTR in case of error which should be
538 * handled using IS_ERR.
540 * Error return values can be:
541 * EINVAL: bad parameters
543 * The callers are required to call dev_pm_opp_put() for the returned OPP after
546 struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev,
547 unsigned long u_volt)
549 struct opp_table *opp_table;
550 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
552 if (!dev || !u_volt) {
553 dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__,
555 return ERR_PTR(-EINVAL);
558 opp_table = _find_opp_table(dev);
559 if (IS_ERR(opp_table))
560 return ERR_CAST(opp_table);
562 mutex_lock(&opp_table->lock);
564 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
565 if (temp_opp->available) {
566 if (temp_opp->supplies[0].u_volt > u_volt)
572 /* Increment the reference count of OPP */
576 mutex_unlock(&opp_table->lock);
577 dev_pm_opp_put_opp_table(opp_table);
581 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt);
583 static int _set_opp_voltage(struct device *dev, struct regulator *reg,
584 struct dev_pm_opp_supply *supply)
588 /* Regulator not available for device */
590 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
595 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
596 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
598 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
599 supply->u_volt, supply->u_volt_max);
601 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
602 __func__, supply->u_volt_min, supply->u_volt,
603 supply->u_volt_max, ret);
608 static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
613 ret = clk_set_rate(clk, freq);
615 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
622 static int _generic_set_opp_regulator(const struct opp_table *opp_table,
624 unsigned long old_freq,
626 struct dev_pm_opp_supply *old_supply,
627 struct dev_pm_opp_supply *new_supply)
629 struct regulator *reg = opp_table->regulators[0];
632 /* This function only supports single regulator per device */
633 if (WARN_ON(opp_table->regulator_count > 1)) {
634 dev_err(dev, "multiple regulators are not supported\n");
638 /* Scaling up? Scale voltage before frequency */
639 if (freq >= old_freq) {
640 ret = _set_opp_voltage(dev, reg, new_supply);
642 goto restore_voltage;
645 /* Change frequency */
646 ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
648 goto restore_voltage;
650 /* Scaling down? Scale voltage after frequency */
651 if (freq < old_freq) {
652 ret = _set_opp_voltage(dev, reg, new_supply);
660 if (_generic_set_opp_clk_only(dev, opp_table->clk, old_freq))
661 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
664 /* This shouldn't harm even if the voltages weren't updated earlier */
666 _set_opp_voltage(dev, reg, old_supply);
671 static int _set_opp_custom(const struct opp_table *opp_table,
672 struct device *dev, unsigned long old_freq,
674 struct dev_pm_opp_supply *old_supply,
675 struct dev_pm_opp_supply *new_supply)
677 struct dev_pm_set_opp_data *data;
680 data = opp_table->set_opp_data;
681 data->regulators = opp_table->regulators;
682 data->regulator_count = opp_table->regulator_count;
683 data->clk = opp_table->clk;
686 data->old_opp.rate = old_freq;
687 size = sizeof(*old_supply) * opp_table->regulator_count;
688 if (IS_ERR(old_supply))
689 memset(data->old_opp.supplies, 0, size);
691 memcpy(data->old_opp.supplies, old_supply, size);
693 data->new_opp.rate = freq;
694 memcpy(data->new_opp.supplies, new_supply, size);
696 return opp_table->set_opp(data);
699 /* This is only called for PM domain for now */
700 static int _set_required_opps(struct device *dev,
701 struct opp_table *opp_table,
702 struct dev_pm_opp *opp)
704 struct opp_table **required_opp_tables = opp_table->required_opp_tables;
705 struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
709 if (!required_opp_tables)
712 /* Single genpd case */
713 if (!genpd_virt_devs) {
714 pstate = opp->required_opps[0]->pstate;
715 ret = dev_pm_genpd_set_performance_state(dev, pstate);
717 dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
718 dev_name(dev), pstate, ret);
723 /* Multiple genpd case */
726 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
727 * after it is freed from another thread.
729 mutex_lock(&opp_table->genpd_virt_dev_lock);
731 for (i = 0; i < opp_table->required_opp_count; i++) {
732 pstate = opp->required_opps[i]->pstate;
734 if (!genpd_virt_devs[i])
737 ret = dev_pm_genpd_set_performance_state(genpd_virt_devs[i], pstate);
739 dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
740 dev_name(genpd_virt_devs[i]), pstate, ret);
744 mutex_unlock(&opp_table->genpd_virt_dev_lock);
750 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
751 * @dev: device for which we do this operation
752 * @target_freq: frequency to achieve
754 * This configures the power-supplies to the levels specified by the OPP
755 * corresponding to the target_freq, and programs the clock to a value <=
756 * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
757 * provided by the opp, should have already rounded to the target OPP's
760 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
762 struct opp_table *opp_table;
763 unsigned long freq, old_freq, temp_freq;
764 struct dev_pm_opp *old_opp, *opp;
768 if (unlikely(!target_freq)) {
769 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
774 opp_table = _find_opp_table(dev);
775 if (IS_ERR(opp_table)) {
776 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
777 return PTR_ERR(opp_table);
780 clk = opp_table->clk;
782 dev_err(dev, "%s: No clock available for the device\n",
788 freq = clk_round_rate(clk, target_freq);
792 old_freq = clk_get_rate(clk);
794 /* Return early if nothing to do */
795 if (old_freq == freq) {
796 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
802 temp_freq = old_freq;
803 old_opp = _find_freq_ceil(opp_table, &temp_freq);
804 if (IS_ERR(old_opp)) {
805 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
806 __func__, old_freq, PTR_ERR(old_opp));
810 opp = _find_freq_ceil(opp_table, &temp_freq);
813 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
814 __func__, freq, ret);
818 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
821 /* Scaling up? Configure required OPPs before frequency */
822 if (freq >= old_freq) {
823 ret = _set_required_opps(dev, opp_table, opp);
828 if (opp_table->set_opp) {
829 ret = _set_opp_custom(opp_table, dev, old_freq, freq,
830 IS_ERR(old_opp) ? NULL : old_opp->supplies,
832 } else if (opp_table->regulators) {
833 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
834 IS_ERR(old_opp) ? NULL : old_opp->supplies,
837 /* Only frequency scaling */
838 ret = _generic_set_opp_clk_only(dev, clk, freq);
841 /* Scaling down? Configure required OPPs after frequency */
842 if (!ret && freq < old_freq) {
843 ret = _set_required_opps(dev, opp_table, opp);
845 dev_err(dev, "Failed to set required opps: %d\n", ret);
851 if (!IS_ERR(old_opp))
852 dev_pm_opp_put(old_opp);
854 dev_pm_opp_put_opp_table(opp_table);
857 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
859 /* OPP-dev Helpers */
860 static void _remove_opp_dev(struct opp_device *opp_dev,
861 struct opp_table *opp_table)
863 opp_debug_unregister(opp_dev, opp_table);
864 list_del(&opp_dev->node);
868 static struct opp_device *_add_opp_dev_unlocked(const struct device *dev,
869 struct opp_table *opp_table)
871 struct opp_device *opp_dev;
873 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
877 /* Initialize opp-dev */
880 list_add(&opp_dev->node, &opp_table->dev_list);
882 /* Create debugfs entries for the opp_table */
883 opp_debug_register(opp_dev, opp_table);
888 struct opp_device *_add_opp_dev(const struct device *dev,
889 struct opp_table *opp_table)
891 struct opp_device *opp_dev;
893 mutex_lock(&opp_table->lock);
894 opp_dev = _add_opp_dev_unlocked(dev, opp_table);
895 mutex_unlock(&opp_table->lock);
900 static struct opp_table *_allocate_opp_table(struct device *dev, int index)
902 struct opp_table *opp_table;
903 struct opp_device *opp_dev;
907 * Allocate a new OPP table. In the infrequent case where a new
908 * device is needed to be added, we pay this penalty.
910 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
914 mutex_init(&opp_table->lock);
915 mutex_init(&opp_table->genpd_virt_dev_lock);
916 INIT_LIST_HEAD(&opp_table->dev_list);
918 /* Mark regulator count uninitialized */
919 opp_table->regulator_count = -1;
921 opp_dev = _add_opp_dev(dev, opp_table);
927 _of_init_opp_table(opp_table, dev, index);
929 /* Find clk for the device */
930 opp_table->clk = clk_get(dev, NULL);
931 if (IS_ERR(opp_table->clk)) {
932 ret = PTR_ERR(opp_table->clk);
933 if (ret != -EPROBE_DEFER)
934 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
938 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
939 INIT_LIST_HEAD(&opp_table->opp_list);
940 kref_init(&opp_table->kref);
942 /* Secure the device table modification */
943 list_add(&opp_table->node, &opp_tables);
947 void _get_opp_table_kref(struct opp_table *opp_table)
949 kref_get(&opp_table->kref);
952 static struct opp_table *_opp_get_opp_table(struct device *dev, int index)
954 struct opp_table *opp_table;
956 /* Hold our table modification lock here */
957 mutex_lock(&opp_table_lock);
959 opp_table = _find_opp_table_unlocked(dev);
960 if (!IS_ERR(opp_table))
963 opp_table = _managed_opp(dev, index);
965 if (!_add_opp_dev_unlocked(dev, opp_table)) {
966 dev_pm_opp_put_opp_table(opp_table);
972 opp_table = _allocate_opp_table(dev, index);
975 mutex_unlock(&opp_table_lock);
980 struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
982 return _opp_get_opp_table(dev, 0);
984 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
986 struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev,
989 return _opp_get_opp_table(dev, index);
992 static void _opp_table_kref_release(struct kref *kref)
994 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
995 struct opp_device *opp_dev, *temp;
997 _of_clear_opp_table(opp_table);
1000 if (!IS_ERR(opp_table->clk))
1001 clk_put(opp_table->clk);
1003 WARN_ON(!list_empty(&opp_table->opp_list));
1005 list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
1007 * The OPP table is getting removed, drop the performance state
1010 if (opp_table->genpd_performance_state)
1011 dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
1013 _remove_opp_dev(opp_dev, opp_table);
1016 mutex_destroy(&opp_table->genpd_virt_dev_lock);
1017 mutex_destroy(&opp_table->lock);
1018 list_del(&opp_table->node);
1021 mutex_unlock(&opp_table_lock);
1024 void _opp_remove_all_static(struct opp_table *opp_table)
1026 struct dev_pm_opp *opp, *tmp;
1028 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1030 dev_pm_opp_put(opp);
1033 opp_table->parsed_static_opps = false;
1036 static void _opp_table_list_kref_release(struct kref *kref)
1038 struct opp_table *opp_table = container_of(kref, struct opp_table,
1041 _opp_remove_all_static(opp_table);
1042 mutex_unlock(&opp_table_lock);
1045 void _put_opp_list_kref(struct opp_table *opp_table)
1047 kref_put_mutex(&opp_table->list_kref, _opp_table_list_kref_release,
1051 void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
1053 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
1056 EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
1058 void _opp_free(struct dev_pm_opp *opp)
1063 static void _opp_kref_release(struct dev_pm_opp *opp,
1064 struct opp_table *opp_table)
1067 * Notify the changes in the availability of the operable
1068 * frequency/voltage list.
1070 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
1071 _of_opp_free_required_opps(opp_table, opp);
1072 opp_debug_remove_one(opp);
1073 list_del(&opp->node);
1077 static void _opp_kref_release_unlocked(struct kref *kref)
1079 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1080 struct opp_table *opp_table = opp->opp_table;
1082 _opp_kref_release(opp, opp_table);
1085 static void _opp_kref_release_locked(struct kref *kref)
1087 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1088 struct opp_table *opp_table = opp->opp_table;
1090 _opp_kref_release(opp, opp_table);
1091 mutex_unlock(&opp_table->lock);
1094 void dev_pm_opp_get(struct dev_pm_opp *opp)
1096 kref_get(&opp->kref);
1099 void dev_pm_opp_put(struct dev_pm_opp *opp)
1101 kref_put_mutex(&opp->kref, _opp_kref_release_locked,
1102 &opp->opp_table->lock);
1104 EXPORT_SYMBOL_GPL(dev_pm_opp_put);
1106 static void dev_pm_opp_put_unlocked(struct dev_pm_opp *opp)
1108 kref_put(&opp->kref, _opp_kref_release_unlocked);
1112 * dev_pm_opp_remove() - Remove an OPP from OPP table
1113 * @dev: device for which we do this operation
1114 * @freq: OPP to remove with matching 'freq'
1116 * This function removes an opp from the opp table.
1118 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1120 struct dev_pm_opp *opp;
1121 struct opp_table *opp_table;
1124 opp_table = _find_opp_table(dev);
1125 if (IS_ERR(opp_table))
1128 mutex_lock(&opp_table->lock);
1130 list_for_each_entry(opp, &opp_table->opp_list, node) {
1131 if (opp->rate == freq) {
1137 mutex_unlock(&opp_table->lock);
1140 dev_pm_opp_put(opp);
1142 /* Drop the reference taken by dev_pm_opp_add() */
1143 dev_pm_opp_put_opp_table(opp_table);
1145 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1149 /* Drop the reference taken by _find_opp_table() */
1150 dev_pm_opp_put_opp_table(opp_table);
1152 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1155 * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
1156 * @dev: device for which we do this operation
1158 * This function removes all dynamically created OPPs from the opp table.
1160 void dev_pm_opp_remove_all_dynamic(struct device *dev)
1162 struct opp_table *opp_table;
1163 struct dev_pm_opp *opp, *temp;
1166 opp_table = _find_opp_table(dev);
1167 if (IS_ERR(opp_table))
1170 mutex_lock(&opp_table->lock);
1171 list_for_each_entry_safe(opp, temp, &opp_table->opp_list, node) {
1173 dev_pm_opp_put_unlocked(opp);
1177 mutex_unlock(&opp_table->lock);
1179 /* Drop the references taken by dev_pm_opp_add() */
1181 dev_pm_opp_put_opp_table(opp_table);
1183 /* Drop the reference taken by _find_opp_table() */
1184 dev_pm_opp_put_opp_table(opp_table);
1186 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
1188 struct dev_pm_opp *_opp_allocate(struct opp_table *table)
1190 struct dev_pm_opp *opp;
1191 int count, supply_size;
1193 /* Allocate space for at least one supply */
1194 count = table->regulator_count > 0 ? table->regulator_count : 1;
1195 supply_size = sizeof(*opp->supplies) * count;
1197 /* allocate new OPP node and supplies structures */
1198 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
1202 /* Put the supplies at the end of the OPP structure as an empty array */
1203 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1204 INIT_LIST_HEAD(&opp->node);
1209 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
1210 struct opp_table *opp_table)
1212 struct regulator *reg;
1215 if (!opp_table->regulators)
1218 for (i = 0; i < opp_table->regulator_count; i++) {
1219 reg = opp_table->regulators[i];
1221 if (!regulator_is_supported_voltage(reg,
1222 opp->supplies[i].u_volt_min,
1223 opp->supplies[i].u_volt_max)) {
1224 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1225 __func__, opp->supplies[i].u_volt_min,
1226 opp->supplies[i].u_volt_max);
1234 static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1235 struct opp_table *opp_table,
1236 struct list_head **head)
1238 struct dev_pm_opp *opp;
1241 * Insert new OPP in order of increasing frequency and discard if
1244 * Need to use &opp_table->opp_list in the condition part of the 'for'
1245 * loop, don't replace it with head otherwise it will become an infinite
1248 list_for_each_entry(opp, &opp_table->opp_list, node) {
1249 if (new_opp->rate > opp->rate) {
1254 if (new_opp->rate < opp->rate)
1257 /* Duplicate OPPs */
1258 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1259 __func__, opp->rate, opp->supplies[0].u_volt,
1260 opp->available, new_opp->rate,
1261 new_opp->supplies[0].u_volt, new_opp->available);
1263 /* Should we compare voltages for all regulators here ? */
1264 return opp->available &&
1265 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1273 * 0: On success. And appropriate error message for duplicate OPPs.
1274 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1275 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1276 * sure we don't print error messages unnecessarily if different parts of
1277 * kernel try to initialize the OPP table.
1278 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1279 * should be considered an error by the callers of _opp_add().
1281 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1282 struct opp_table *opp_table, bool rate_not_available)
1284 struct list_head *head;
1287 mutex_lock(&opp_table->lock);
1288 head = &opp_table->opp_list;
1290 if (likely(!rate_not_available)) {
1291 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1293 mutex_unlock(&opp_table->lock);
1298 list_add(&new_opp->node, head);
1299 mutex_unlock(&opp_table->lock);
1301 new_opp->opp_table = opp_table;
1302 kref_init(&new_opp->kref);
1304 opp_debug_create_one(new_opp, opp_table);
1306 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
1307 new_opp->available = false;
1308 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1309 __func__, new_opp->rate);
1316 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
1317 * @opp_table: OPP table
1318 * @dev: device for which we do this operation
1319 * @freq: Frequency in Hz for this OPP
1320 * @u_volt: Voltage in uVolts for this OPP
1321 * @dynamic: Dynamically added OPPs.
1323 * This function adds an opp definition to the opp table and returns status.
1324 * The opp is made available by default and it can be controlled using
1325 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1327 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1328 * and freed by dev_pm_opp_of_remove_table.
1332 * Duplicate OPPs (both freq and volt are same) and opp->available
1333 * -EEXIST Freq are same and volt are different OR
1334 * Duplicate OPPs (both freq and volt are same) and !opp->available
1335 * -ENOMEM Memory allocation failure
1337 int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1338 unsigned long freq, long u_volt, bool dynamic)
1340 struct dev_pm_opp *new_opp;
1344 new_opp = _opp_allocate(opp_table);
1348 /* populate the opp table */
1349 new_opp->rate = freq;
1350 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
1351 new_opp->supplies[0].u_volt = u_volt;
1352 new_opp->supplies[0].u_volt_min = u_volt - tol;
1353 new_opp->supplies[0].u_volt_max = u_volt + tol;
1354 new_opp->available = true;
1355 new_opp->dynamic = dynamic;
1357 ret = _opp_add(dev, new_opp, opp_table, false);
1359 /* Don't return error for duplicate OPPs */
1366 * Notify the changes in the availability of the operable
1367 * frequency/voltage list.
1369 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
1379 * dev_pm_opp_set_supported_hw() - Set supported platforms
1380 * @dev: Device for which supported-hw has to be set.
1381 * @versions: Array of hierarchy of versions to match.
1382 * @count: Number of elements in the array.
1384 * This is required only for the V2 bindings, and it enables a platform to
1385 * specify the hierarchy of versions it supports. OPP layer will then enable
1386 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1389 struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1390 const u32 *versions, unsigned int count)
1392 struct opp_table *opp_table;
1394 opp_table = dev_pm_opp_get_opp_table(dev);
1396 return ERR_PTR(-ENOMEM);
1398 /* Make sure there are no concurrent readers while updating opp_table */
1399 WARN_ON(!list_empty(&opp_table->opp_list));
1401 /* Another CPU that shares the OPP table has set the property ? */
1402 if (opp_table->supported_hw)
1405 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
1407 if (!opp_table->supported_hw) {
1408 dev_pm_opp_put_opp_table(opp_table);
1409 return ERR_PTR(-ENOMEM);
1412 opp_table->supported_hw_count = count;
1416 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1419 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
1420 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
1422 * This is required only for the V2 bindings, and is called for a matching
1423 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
1424 * will not be freed.
1426 void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
1428 /* Make sure there are no concurrent readers while updating opp_table */
1429 WARN_ON(!list_empty(&opp_table->opp_list));
1431 kfree(opp_table->supported_hw);
1432 opp_table->supported_hw = NULL;
1433 opp_table->supported_hw_count = 0;
1435 dev_pm_opp_put_opp_table(opp_table);
1437 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1440 * dev_pm_opp_set_prop_name() - Set prop-extn name
1441 * @dev: Device for which the prop-name has to be set.
1442 * @name: name to postfix to properties.
1444 * This is required only for the V2 bindings, and it enables a platform to
1445 * specify the extn to be used for certain property names. The properties to
1446 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1447 * should postfix the property name with -<name> while looking for them.
1449 struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1451 struct opp_table *opp_table;
1453 opp_table = dev_pm_opp_get_opp_table(dev);
1455 return ERR_PTR(-ENOMEM);
1457 /* Make sure there are no concurrent readers while updating opp_table */
1458 WARN_ON(!list_empty(&opp_table->opp_list));
1460 /* Another CPU that shares the OPP table has set the property ? */
1461 if (opp_table->prop_name)
1464 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1465 if (!opp_table->prop_name) {
1466 dev_pm_opp_put_opp_table(opp_table);
1467 return ERR_PTR(-ENOMEM);
1472 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1475 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
1476 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
1478 * This is required only for the V2 bindings, and is called for a matching
1479 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
1480 * will not be freed.
1482 void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
1484 /* Make sure there are no concurrent readers while updating opp_table */
1485 WARN_ON(!list_empty(&opp_table->opp_list));
1487 kfree(opp_table->prop_name);
1488 opp_table->prop_name = NULL;
1490 dev_pm_opp_put_opp_table(opp_table);
1492 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1494 static int _allocate_set_opp_data(struct opp_table *opp_table)
1496 struct dev_pm_set_opp_data *data;
1497 int len, count = opp_table->regulator_count;
1499 if (WARN_ON(!opp_table->regulators))
1502 /* space for set_opp_data */
1503 len = sizeof(*data);
1505 /* space for old_opp.supplies and new_opp.supplies */
1506 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1508 data = kzalloc(len, GFP_KERNEL);
1512 data->old_opp.supplies = (void *)(data + 1);
1513 data->new_opp.supplies = data->old_opp.supplies + count;
1515 opp_table->set_opp_data = data;
1520 static void _free_set_opp_data(struct opp_table *opp_table)
1522 kfree(opp_table->set_opp_data);
1523 opp_table->set_opp_data = NULL;
1527 * dev_pm_opp_set_regulators() - Set regulator names for the device
1528 * @dev: Device for which regulator name is being set.
1529 * @names: Array of pointers to the names of the regulator.
1530 * @count: Number of regulators.
1532 * In order to support OPP switching, OPP layer needs to know the name of the
1533 * device's regulators, as the core would be required to switch voltages as
1536 * This must be called before any OPPs are initialized for the device.
1538 struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1539 const char * const names[],
1542 struct opp_table *opp_table;
1543 struct regulator *reg;
1546 opp_table = dev_pm_opp_get_opp_table(dev);
1548 return ERR_PTR(-ENOMEM);
1550 /* This should be called before OPPs are initialized */
1551 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1556 /* Another CPU that shares the OPP table has set the regulators ? */
1557 if (opp_table->regulators)
1560 opp_table->regulators = kmalloc_array(count,
1561 sizeof(*opp_table->regulators),
1563 if (!opp_table->regulators) {
1568 for (i = 0; i < count; i++) {
1569 reg = regulator_get_optional(dev, names[i]);
1572 if (ret != -EPROBE_DEFER)
1573 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1574 __func__, names[i], ret);
1575 goto free_regulators;
1578 opp_table->regulators[i] = reg;
1581 opp_table->regulator_count = count;
1583 /* Allocate block only once to pass to set_opp() routines */
1584 ret = _allocate_set_opp_data(opp_table);
1586 goto free_regulators;
1592 regulator_put(opp_table->regulators[--i]);
1594 kfree(opp_table->regulators);
1595 opp_table->regulators = NULL;
1596 opp_table->regulator_count = -1;
1598 dev_pm_opp_put_opp_table(opp_table);
1600 return ERR_PTR(ret);
1602 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
1605 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1606 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
1608 void dev_pm_opp_put_regulators(struct opp_table *opp_table)
1612 if (!opp_table->regulators)
1615 /* Make sure there are no concurrent readers while updating opp_table */
1616 WARN_ON(!list_empty(&opp_table->opp_list));
1618 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1619 regulator_put(opp_table->regulators[i]);
1621 _free_set_opp_data(opp_table);
1623 kfree(opp_table->regulators);
1624 opp_table->regulators = NULL;
1625 opp_table->regulator_count = -1;
1628 dev_pm_opp_put_opp_table(opp_table);
1630 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
1633 * dev_pm_opp_set_clkname() - Set clk name for the device
1634 * @dev: Device for which clk name is being set.
1637 * In order to support OPP switching, OPP layer needs to get pointer to the
1638 * clock for the device. Simple cases work fine without using this routine (i.e.
1639 * by passing connection-id as NULL), but for a device with multiple clocks
1640 * available, the OPP core needs to know the exact name of the clk to use.
1642 * This must be called before any OPPs are initialized for the device.
1644 struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
1646 struct opp_table *opp_table;
1649 opp_table = dev_pm_opp_get_opp_table(dev);
1651 return ERR_PTR(-ENOMEM);
1653 /* This should be called before OPPs are initialized */
1654 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1659 /* Already have default clk set, free it */
1660 if (!IS_ERR(opp_table->clk))
1661 clk_put(opp_table->clk);
1663 /* Find clk for the device */
1664 opp_table->clk = clk_get(dev, name);
1665 if (IS_ERR(opp_table->clk)) {
1666 ret = PTR_ERR(opp_table->clk);
1667 if (ret != -EPROBE_DEFER) {
1668 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
1677 dev_pm_opp_put_opp_table(opp_table);
1679 return ERR_PTR(ret);
1681 EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
1684 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1685 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1687 void dev_pm_opp_put_clkname(struct opp_table *opp_table)
1689 /* Make sure there are no concurrent readers while updating opp_table */
1690 WARN_ON(!list_empty(&opp_table->opp_list));
1692 clk_put(opp_table->clk);
1693 opp_table->clk = ERR_PTR(-EINVAL);
1695 dev_pm_opp_put_opp_table(opp_table);
1697 EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
1700 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1701 * @dev: Device for which the helper is getting registered.
1702 * @set_opp: Custom set OPP helper.
1704 * This is useful to support complex platforms (like platforms with multiple
1705 * regulators per device), instead of the generic OPP set rate helper.
1707 * This must be called before any OPPs are initialized for the device.
1709 struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
1710 int (*set_opp)(struct dev_pm_set_opp_data *data))
1712 struct opp_table *opp_table;
1715 return ERR_PTR(-EINVAL);
1717 opp_table = dev_pm_opp_get_opp_table(dev);
1719 return ERR_PTR(-ENOMEM);
1721 /* This should be called before OPPs are initialized */
1722 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1723 dev_pm_opp_put_opp_table(opp_table);
1724 return ERR_PTR(-EBUSY);
1727 /* Another CPU that shares the OPP table has set the helper ? */
1728 if (!opp_table->set_opp)
1729 opp_table->set_opp = set_opp;
1733 EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1736 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
1738 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
1740 * Release resources blocked for platform specific set_opp helper.
1742 void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
1744 /* Make sure there are no concurrent readers while updating opp_table */
1745 WARN_ON(!list_empty(&opp_table->opp_list));
1747 opp_table->set_opp = NULL;
1748 dev_pm_opp_put_opp_table(opp_table);
1750 EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
1752 static void _opp_detach_genpd(struct opp_table *opp_table)
1756 for (index = 0; index < opp_table->required_opp_count; index++) {
1757 if (!opp_table->genpd_virt_devs[index])
1760 dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
1761 opp_table->genpd_virt_devs[index] = NULL;
1764 kfree(opp_table->genpd_virt_devs);
1765 opp_table->genpd_virt_devs = NULL;
1769 * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
1770 * @dev: Consumer device for which the genpd is getting attached.
1771 * @names: Null terminated array of pointers containing names of genpd to attach.
1773 * Multiple generic power domains for a device are supported with the help of
1774 * virtual genpd devices, which are created for each consumer device - genpd
1775 * pair. These are the device structures which are attached to the power domain
1776 * and are required by the OPP core to set the performance state of the genpd.
1777 * The same API also works for the case where single genpd is available and so
1778 * we don't need to support that separately.
1780 * This helper will normally be called by the consumer driver of the device
1781 * "dev", as only that has details of the genpd names.
1783 * This helper needs to be called once with a list of all genpd to attach.
1784 * Otherwise the original device structure will be used instead by the OPP core.
1786 struct opp_table *dev_pm_opp_attach_genpd(struct device *dev, const char **names)
1788 struct opp_table *opp_table;
1789 struct device *virt_dev;
1790 int index, ret = -EINVAL;
1791 const char **name = names;
1793 opp_table = dev_pm_opp_get_opp_table(dev);
1795 return ERR_PTR(-ENOMEM);
1798 * If the genpd's OPP table isn't already initialized, parsing of the
1799 * required-opps fail for dev. We should retry this after genpd's OPP
1802 if (!opp_table->required_opp_count) {
1803 ret = -EPROBE_DEFER;
1807 mutex_lock(&opp_table->genpd_virt_dev_lock);
1809 opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
1810 sizeof(*opp_table->genpd_virt_devs),
1812 if (!opp_table->genpd_virt_devs)
1816 index = of_property_match_string(dev->of_node,
1817 "power-domain-names", *name);
1819 dev_err(dev, "Failed to find power domain: %s (%d)\n",
1824 if (index >= opp_table->required_opp_count) {
1825 dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
1826 *name, opp_table->required_opp_count, index);
1830 if (opp_table->genpd_virt_devs[index]) {
1831 dev_err(dev, "Genpd virtual device already set %s\n",
1836 virt_dev = dev_pm_domain_attach_by_name(dev, *name);
1837 if (IS_ERR(virt_dev)) {
1838 ret = PTR_ERR(virt_dev);
1839 dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
1843 opp_table->genpd_virt_devs[index] = virt_dev;
1847 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1852 _opp_detach_genpd(opp_table);
1854 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1857 dev_pm_opp_put_opp_table(opp_table);
1859 return ERR_PTR(ret);
1861 EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd);
1864 * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device.
1865 * @opp_table: OPP table returned by dev_pm_opp_attach_genpd().
1867 * This detaches the genpd(s), resets the virtual device pointers, and puts the
1870 void dev_pm_opp_detach_genpd(struct opp_table *opp_table)
1873 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
1876 mutex_lock(&opp_table->genpd_virt_dev_lock);
1877 _opp_detach_genpd(opp_table);
1878 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1880 dev_pm_opp_put_opp_table(opp_table);
1882 EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd);
1885 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
1886 * @src_table: OPP table which has dst_table as one of its required OPP table.
1887 * @dst_table: Required OPP table of the src_table.
1888 * @pstate: Current performance state of the src_table.
1890 * This Returns pstate of the OPP (present in @dst_table) pointed out by the
1891 * "required-opps" property of the OPP (present in @src_table) which has
1892 * performance state set to @pstate.
1894 * Return: Zero or positive performance state on success, otherwise negative
1897 int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
1898 struct opp_table *dst_table,
1899 unsigned int pstate)
1901 struct dev_pm_opp *opp;
1902 int dest_pstate = -EINVAL;
1909 * Normally the src_table will have the "required_opps" property set to
1910 * point to one of the OPPs in the dst_table, but in some cases the
1911 * genpd and its master have one to one mapping of performance states
1912 * and so none of them have the "required-opps" property set. Return the
1913 * pstate of the src_table as it is in such cases.
1915 if (!src_table->required_opp_count)
1918 for (i = 0; i < src_table->required_opp_count; i++) {
1919 if (src_table->required_opp_tables[i]->np == dst_table->np)
1923 if (unlikely(i == src_table->required_opp_count)) {
1924 pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
1925 __func__, src_table, dst_table);
1929 mutex_lock(&src_table->lock);
1931 list_for_each_entry(opp, &src_table->opp_list, node) {
1932 if (opp->pstate == pstate) {
1933 dest_pstate = opp->required_opps[i]->pstate;
1938 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
1942 mutex_unlock(&src_table->lock);
1948 * dev_pm_opp_add() - Add an OPP table from a table definitions
1949 * @dev: device for which we do this operation
1950 * @freq: Frequency in Hz for this OPP
1951 * @u_volt: Voltage in uVolts for this OPP
1953 * This function adds an opp definition to the opp table and returns status.
1954 * The opp is made available by default and it can be controlled using
1955 * dev_pm_opp_enable/disable functions.
1959 * Duplicate OPPs (both freq and volt are same) and opp->available
1960 * -EEXIST Freq are same and volt are different OR
1961 * Duplicate OPPs (both freq and volt are same) and !opp->available
1962 * -ENOMEM Memory allocation failure
1964 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1966 struct opp_table *opp_table;
1969 opp_table = dev_pm_opp_get_opp_table(dev);
1973 /* Fix regulator count for dynamic OPPs */
1974 opp_table->regulator_count = 1;
1976 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
1978 dev_pm_opp_put_opp_table(opp_table);
1982 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1985 * _opp_set_availability() - helper to set the availability of an opp
1986 * @dev: device for which we do this operation
1987 * @freq: OPP frequency to modify availability
1988 * @availability_req: availability status requested for this opp
1990 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1991 * which is isolated here.
1993 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1994 * copy operation, returns 0 if no modification was done OR modification was
1997 static int _opp_set_availability(struct device *dev, unsigned long freq,
1998 bool availability_req)
2000 struct opp_table *opp_table;
2001 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
2004 /* Find the opp_table */
2005 opp_table = _find_opp_table(dev);
2006 if (IS_ERR(opp_table)) {
2007 r = PTR_ERR(opp_table);
2008 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
2012 mutex_lock(&opp_table->lock);
2014 /* Do we have the frequency? */
2015 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
2016 if (tmp_opp->rate == freq) {
2027 /* Is update really needed? */
2028 if (opp->available == availability_req)
2031 opp->available = availability_req;
2033 dev_pm_opp_get(opp);
2034 mutex_unlock(&opp_table->lock);
2036 /* Notify the change of the OPP availability */
2037 if (availability_req)
2038 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
2041 blocking_notifier_call_chain(&opp_table->head,
2042 OPP_EVENT_DISABLE, opp);
2044 dev_pm_opp_put(opp);
2048 mutex_unlock(&opp_table->lock);
2050 dev_pm_opp_put_opp_table(opp_table);
2055 * dev_pm_opp_enable() - Enable a specific OPP
2056 * @dev: device for which we do this operation
2057 * @freq: OPP frequency to enable
2059 * Enables a provided opp. If the operation is valid, this returns 0, else the
2060 * corresponding error value. It is meant to be used for users an OPP available
2061 * after being temporarily made unavailable with dev_pm_opp_disable.
2063 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2064 * copy operation, returns 0 if no modification was done OR modification was
2067 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
2069 return _opp_set_availability(dev, freq, true);
2071 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
2074 * dev_pm_opp_disable() - Disable a specific OPP
2075 * @dev: device for which we do this operation
2076 * @freq: OPP frequency to disable
2078 * Disables a provided opp. If the operation is valid, this returns
2079 * 0, else the corresponding error value. It is meant to be a temporary
2080 * control by users to make this OPP not available until the circumstances are
2081 * right to make it available again (with a call to dev_pm_opp_enable).
2083 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2084 * copy operation, returns 0 if no modification was done OR modification was
2087 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
2089 return _opp_set_availability(dev, freq, false);
2091 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
2094 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
2095 * @dev: Device for which notifier needs to be registered
2096 * @nb: Notifier block to be registered
2098 * Return: 0 on success or a negative error value.
2100 int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
2102 struct opp_table *opp_table;
2105 opp_table = _find_opp_table(dev);
2106 if (IS_ERR(opp_table))
2107 return PTR_ERR(opp_table);
2109 ret = blocking_notifier_chain_register(&opp_table->head, nb);
2111 dev_pm_opp_put_opp_table(opp_table);
2115 EXPORT_SYMBOL(dev_pm_opp_register_notifier);
2118 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
2119 * @dev: Device for which notifier needs to be unregistered
2120 * @nb: Notifier block to be unregistered
2122 * Return: 0 on success or a negative error value.
2124 int dev_pm_opp_unregister_notifier(struct device *dev,
2125 struct notifier_block *nb)
2127 struct opp_table *opp_table;
2130 opp_table = _find_opp_table(dev);
2131 if (IS_ERR(opp_table))
2132 return PTR_ERR(opp_table);
2134 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
2136 dev_pm_opp_put_opp_table(opp_table);
2140 EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
2142 void _dev_pm_opp_find_and_remove_table(struct device *dev)
2144 struct opp_table *opp_table;
2146 /* Check for existing table for 'dev' */
2147 opp_table = _find_opp_table(dev);
2148 if (IS_ERR(opp_table)) {
2149 int error = PTR_ERR(opp_table);
2151 if (error != -ENODEV)
2152 WARN(1, "%s: opp_table: %d\n",
2153 IS_ERR_OR_NULL(dev) ?
2154 "Invalid device" : dev_name(dev),
2159 _put_opp_list_kref(opp_table);
2161 /* Drop reference taken by _find_opp_table() */
2162 dev_pm_opp_put_opp_table(opp_table);
2164 /* Drop reference taken while the OPP table was added */
2165 dev_pm_opp_put_opp_table(opp_table);
2169 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
2170 * @dev: device pointer used to lookup OPP table.
2172 * Free both OPPs created using static entries present in DT and the
2173 * dynamically added entries.
2175 void dev_pm_opp_remove_table(struct device *dev)
2177 _dev_pm_opp_find_and_remove_table(dev);
2179 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);