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 and clock source to the levels specified
755 * by the OPP corresponding to the target_freq.
757 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
759 struct opp_table *opp_table;
760 unsigned long freq, old_freq;
761 struct dev_pm_opp *old_opp, *opp;
765 if (unlikely(!target_freq)) {
766 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
771 opp_table = _find_opp_table(dev);
772 if (IS_ERR(opp_table)) {
773 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
774 return PTR_ERR(opp_table);
777 clk = opp_table->clk;
779 dev_err(dev, "%s: No clock available for the device\n",
785 freq = clk_round_rate(clk, target_freq);
789 old_freq = clk_get_rate(clk);
791 /* Return early if nothing to do */
792 if (old_freq == freq) {
793 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
799 old_opp = _find_freq_ceil(opp_table, &old_freq);
800 if (IS_ERR(old_opp)) {
801 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
802 __func__, old_freq, PTR_ERR(old_opp));
805 opp = _find_freq_ceil(opp_table, &freq);
808 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
809 __func__, freq, ret);
813 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
816 /* Scaling up? Configure required OPPs before frequency */
817 if (freq >= old_freq) {
818 ret = _set_required_opps(dev, opp_table, opp);
823 if (opp_table->set_opp) {
824 ret = _set_opp_custom(opp_table, dev, old_freq, freq,
825 IS_ERR(old_opp) ? NULL : old_opp->supplies,
827 } else if (opp_table->regulators) {
828 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
829 IS_ERR(old_opp) ? NULL : old_opp->supplies,
832 /* Only frequency scaling */
833 ret = _generic_set_opp_clk_only(dev, clk, freq);
836 /* Scaling down? Configure required OPPs after frequency */
837 if (!ret && freq < old_freq) {
838 ret = _set_required_opps(dev, opp_table, opp);
840 dev_err(dev, "Failed to set required opps: %d\n", ret);
846 if (!IS_ERR(old_opp))
847 dev_pm_opp_put(old_opp);
849 dev_pm_opp_put_opp_table(opp_table);
852 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
854 /* OPP-dev Helpers */
855 static void _remove_opp_dev(struct opp_device *opp_dev,
856 struct opp_table *opp_table)
858 opp_debug_unregister(opp_dev, opp_table);
859 list_del(&opp_dev->node);
863 static struct opp_device *_add_opp_dev_unlocked(const struct device *dev,
864 struct opp_table *opp_table)
866 struct opp_device *opp_dev;
868 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
872 /* Initialize opp-dev */
875 list_add(&opp_dev->node, &opp_table->dev_list);
877 /* Create debugfs entries for the opp_table */
878 opp_debug_register(opp_dev, opp_table);
883 struct opp_device *_add_opp_dev(const struct device *dev,
884 struct opp_table *opp_table)
886 struct opp_device *opp_dev;
888 mutex_lock(&opp_table->lock);
889 opp_dev = _add_opp_dev_unlocked(dev, opp_table);
890 mutex_unlock(&opp_table->lock);
895 static struct opp_table *_allocate_opp_table(struct device *dev, int index)
897 struct opp_table *opp_table;
898 struct opp_device *opp_dev;
902 * Allocate a new OPP table. In the infrequent case where a new
903 * device is needed to be added, we pay this penalty.
905 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
909 mutex_init(&opp_table->lock);
910 mutex_init(&opp_table->genpd_virt_dev_lock);
911 INIT_LIST_HEAD(&opp_table->dev_list);
913 /* Mark regulator count uninitialized */
914 opp_table->regulator_count = -1;
916 opp_dev = _add_opp_dev(dev, opp_table);
922 _of_init_opp_table(opp_table, dev, index);
924 /* Find clk for the device */
925 opp_table->clk = clk_get(dev, NULL);
926 if (IS_ERR(opp_table->clk)) {
927 ret = PTR_ERR(opp_table->clk);
928 if (ret != -EPROBE_DEFER)
929 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
933 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
934 INIT_LIST_HEAD(&opp_table->opp_list);
935 kref_init(&opp_table->kref);
937 /* Secure the device table modification */
938 list_add(&opp_table->node, &opp_tables);
942 void _get_opp_table_kref(struct opp_table *opp_table)
944 kref_get(&opp_table->kref);
947 static struct opp_table *_opp_get_opp_table(struct device *dev, int index)
949 struct opp_table *opp_table;
951 /* Hold our table modification lock here */
952 mutex_lock(&opp_table_lock);
954 opp_table = _find_opp_table_unlocked(dev);
955 if (!IS_ERR(opp_table))
958 opp_table = _managed_opp(dev, index);
960 if (!_add_opp_dev_unlocked(dev, opp_table)) {
961 dev_pm_opp_put_opp_table(opp_table);
967 opp_table = _allocate_opp_table(dev, index);
970 mutex_unlock(&opp_table_lock);
975 struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
977 return _opp_get_opp_table(dev, 0);
979 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
981 struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev,
984 return _opp_get_opp_table(dev, index);
987 static void _opp_table_kref_release(struct kref *kref)
989 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
990 struct opp_device *opp_dev, *temp;
992 _of_clear_opp_table(opp_table);
995 if (!IS_ERR(opp_table->clk))
996 clk_put(opp_table->clk);
998 WARN_ON(!list_empty(&opp_table->opp_list));
1000 list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
1002 * The OPP table is getting removed, drop the performance state
1005 if (opp_table->genpd_performance_state)
1006 dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
1008 _remove_opp_dev(opp_dev, opp_table);
1011 mutex_destroy(&opp_table->genpd_virt_dev_lock);
1012 mutex_destroy(&opp_table->lock);
1013 list_del(&opp_table->node);
1016 mutex_unlock(&opp_table_lock);
1019 void _opp_remove_all_static(struct opp_table *opp_table)
1021 struct dev_pm_opp *opp, *tmp;
1023 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1025 dev_pm_opp_put(opp);
1028 opp_table->parsed_static_opps = false;
1031 static void _opp_table_list_kref_release(struct kref *kref)
1033 struct opp_table *opp_table = container_of(kref, struct opp_table,
1036 _opp_remove_all_static(opp_table);
1037 mutex_unlock(&opp_table_lock);
1040 void _put_opp_list_kref(struct opp_table *opp_table)
1042 kref_put_mutex(&opp_table->list_kref, _opp_table_list_kref_release,
1046 void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
1048 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
1051 EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
1053 void _opp_free(struct dev_pm_opp *opp)
1058 static void _opp_kref_release(struct dev_pm_opp *opp,
1059 struct opp_table *opp_table)
1062 * Notify the changes in the availability of the operable
1063 * frequency/voltage list.
1065 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
1066 _of_opp_free_required_opps(opp_table, opp);
1067 opp_debug_remove_one(opp);
1068 list_del(&opp->node);
1072 static void _opp_kref_release_unlocked(struct kref *kref)
1074 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1075 struct opp_table *opp_table = opp->opp_table;
1077 _opp_kref_release(opp, opp_table);
1080 static void _opp_kref_release_locked(struct kref *kref)
1082 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
1083 struct opp_table *opp_table = opp->opp_table;
1085 _opp_kref_release(opp, opp_table);
1086 mutex_unlock(&opp_table->lock);
1089 void dev_pm_opp_get(struct dev_pm_opp *opp)
1091 kref_get(&opp->kref);
1094 void dev_pm_opp_put(struct dev_pm_opp *opp)
1096 kref_put_mutex(&opp->kref, _opp_kref_release_locked,
1097 &opp->opp_table->lock);
1099 EXPORT_SYMBOL_GPL(dev_pm_opp_put);
1101 static void dev_pm_opp_put_unlocked(struct dev_pm_opp *opp)
1103 kref_put(&opp->kref, _opp_kref_release_unlocked);
1107 * dev_pm_opp_remove() - Remove an OPP from OPP table
1108 * @dev: device for which we do this operation
1109 * @freq: OPP to remove with matching 'freq'
1111 * This function removes an opp from the opp table.
1113 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1115 struct dev_pm_opp *opp;
1116 struct opp_table *opp_table;
1119 opp_table = _find_opp_table(dev);
1120 if (IS_ERR(opp_table))
1123 mutex_lock(&opp_table->lock);
1125 list_for_each_entry(opp, &opp_table->opp_list, node) {
1126 if (opp->rate == freq) {
1132 mutex_unlock(&opp_table->lock);
1135 dev_pm_opp_put(opp);
1137 /* Drop the reference taken by dev_pm_opp_add() */
1138 dev_pm_opp_put_opp_table(opp_table);
1140 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1144 /* Drop the reference taken by _find_opp_table() */
1145 dev_pm_opp_put_opp_table(opp_table);
1147 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1150 * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
1151 * @dev: device for which we do this operation
1153 * This function removes all dynamically created OPPs from the opp table.
1155 void dev_pm_opp_remove_all_dynamic(struct device *dev)
1157 struct opp_table *opp_table;
1158 struct dev_pm_opp *opp, *temp;
1161 opp_table = _find_opp_table(dev);
1162 if (IS_ERR(opp_table))
1165 mutex_lock(&opp_table->lock);
1166 list_for_each_entry_safe(opp, temp, &opp_table->opp_list, node) {
1168 dev_pm_opp_put_unlocked(opp);
1172 mutex_unlock(&opp_table->lock);
1174 /* Drop the references taken by dev_pm_opp_add() */
1176 dev_pm_opp_put_opp_table(opp_table);
1178 /* Drop the reference taken by _find_opp_table() */
1179 dev_pm_opp_put_opp_table(opp_table);
1181 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
1183 struct dev_pm_opp *_opp_allocate(struct opp_table *table)
1185 struct dev_pm_opp *opp;
1186 int count, supply_size;
1188 /* Allocate space for at least one supply */
1189 count = table->regulator_count > 0 ? table->regulator_count : 1;
1190 supply_size = sizeof(*opp->supplies) * count;
1192 /* allocate new OPP node and supplies structures */
1193 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
1197 /* Put the supplies at the end of the OPP structure as an empty array */
1198 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1199 INIT_LIST_HEAD(&opp->node);
1204 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
1205 struct opp_table *opp_table)
1207 struct regulator *reg;
1210 if (!opp_table->regulators)
1213 for (i = 0; i < opp_table->regulator_count; i++) {
1214 reg = opp_table->regulators[i];
1216 if (!regulator_is_supported_voltage(reg,
1217 opp->supplies[i].u_volt_min,
1218 opp->supplies[i].u_volt_max)) {
1219 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1220 __func__, opp->supplies[i].u_volt_min,
1221 opp->supplies[i].u_volt_max);
1229 static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
1230 struct opp_table *opp_table,
1231 struct list_head **head)
1233 struct dev_pm_opp *opp;
1236 * Insert new OPP in order of increasing frequency and discard if
1239 * Need to use &opp_table->opp_list in the condition part of the 'for'
1240 * loop, don't replace it with head otherwise it will become an infinite
1243 list_for_each_entry(opp, &opp_table->opp_list, node) {
1244 if (new_opp->rate > opp->rate) {
1249 if (new_opp->rate < opp->rate)
1252 /* Duplicate OPPs */
1253 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1254 __func__, opp->rate, opp->supplies[0].u_volt,
1255 opp->available, new_opp->rate,
1256 new_opp->supplies[0].u_volt, new_opp->available);
1258 /* Should we compare voltages for all regulators here ? */
1259 return opp->available &&
1260 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1268 * 0: On success. And appropriate error message for duplicate OPPs.
1269 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1270 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1271 * sure we don't print error messages unnecessarily if different parts of
1272 * kernel try to initialize the OPP table.
1273 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1274 * should be considered an error by the callers of _opp_add().
1276 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1277 struct opp_table *opp_table, bool rate_not_available)
1279 struct list_head *head;
1282 mutex_lock(&opp_table->lock);
1283 head = &opp_table->opp_list;
1285 if (likely(!rate_not_available)) {
1286 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
1288 mutex_unlock(&opp_table->lock);
1293 list_add(&new_opp->node, head);
1294 mutex_unlock(&opp_table->lock);
1296 new_opp->opp_table = opp_table;
1297 kref_init(&new_opp->kref);
1299 opp_debug_create_one(new_opp, opp_table);
1301 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
1302 new_opp->available = false;
1303 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1304 __func__, new_opp->rate);
1311 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
1312 * @opp_table: OPP table
1313 * @dev: device for which we do this operation
1314 * @freq: Frequency in Hz for this OPP
1315 * @u_volt: Voltage in uVolts for this OPP
1316 * @dynamic: Dynamically added OPPs.
1318 * This function adds an opp definition to the opp table and returns status.
1319 * The opp is made available by default and it can be controlled using
1320 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1322 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1323 * and freed by dev_pm_opp_of_remove_table.
1327 * Duplicate OPPs (both freq and volt are same) and opp->available
1328 * -EEXIST Freq are same and volt are different OR
1329 * Duplicate OPPs (both freq and volt are same) and !opp->available
1330 * -ENOMEM Memory allocation failure
1332 int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1333 unsigned long freq, long u_volt, bool dynamic)
1335 struct dev_pm_opp *new_opp;
1339 new_opp = _opp_allocate(opp_table);
1343 /* populate the opp table */
1344 new_opp->rate = freq;
1345 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
1346 new_opp->supplies[0].u_volt = u_volt;
1347 new_opp->supplies[0].u_volt_min = u_volt - tol;
1348 new_opp->supplies[0].u_volt_max = u_volt + tol;
1349 new_opp->available = true;
1350 new_opp->dynamic = dynamic;
1352 ret = _opp_add(dev, new_opp, opp_table, false);
1354 /* Don't return error for duplicate OPPs */
1361 * Notify the changes in the availability of the operable
1362 * frequency/voltage list.
1364 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
1374 * dev_pm_opp_set_supported_hw() - Set supported platforms
1375 * @dev: Device for which supported-hw has to be set.
1376 * @versions: Array of hierarchy of versions to match.
1377 * @count: Number of elements in the array.
1379 * This is required only for the V2 bindings, and it enables a platform to
1380 * specify the hierarchy of versions it supports. OPP layer will then enable
1381 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1384 struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1385 const u32 *versions, unsigned int count)
1387 struct opp_table *opp_table;
1389 opp_table = dev_pm_opp_get_opp_table(dev);
1391 return ERR_PTR(-ENOMEM);
1393 /* Make sure there are no concurrent readers while updating opp_table */
1394 WARN_ON(!list_empty(&opp_table->opp_list));
1396 /* Another CPU that shares the OPP table has set the property ? */
1397 if (opp_table->supported_hw)
1400 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
1402 if (!opp_table->supported_hw) {
1403 dev_pm_opp_put_opp_table(opp_table);
1404 return ERR_PTR(-ENOMEM);
1407 opp_table->supported_hw_count = count;
1411 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1414 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
1415 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
1417 * This is required only for the V2 bindings, and is called for a matching
1418 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
1419 * will not be freed.
1421 void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
1423 /* Make sure there are no concurrent readers while updating opp_table */
1424 WARN_ON(!list_empty(&opp_table->opp_list));
1426 kfree(opp_table->supported_hw);
1427 opp_table->supported_hw = NULL;
1428 opp_table->supported_hw_count = 0;
1430 dev_pm_opp_put_opp_table(opp_table);
1432 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1435 * dev_pm_opp_set_prop_name() - Set prop-extn name
1436 * @dev: Device for which the prop-name has to be set.
1437 * @name: name to postfix to properties.
1439 * This is required only for the V2 bindings, and it enables a platform to
1440 * specify the extn to be used for certain property names. The properties to
1441 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1442 * should postfix the property name with -<name> while looking for them.
1444 struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1446 struct opp_table *opp_table;
1448 opp_table = dev_pm_opp_get_opp_table(dev);
1450 return ERR_PTR(-ENOMEM);
1452 /* Make sure there are no concurrent readers while updating opp_table */
1453 WARN_ON(!list_empty(&opp_table->opp_list));
1455 /* Another CPU that shares the OPP table has set the property ? */
1456 if (opp_table->prop_name)
1459 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1460 if (!opp_table->prop_name) {
1461 dev_pm_opp_put_opp_table(opp_table);
1462 return ERR_PTR(-ENOMEM);
1467 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1470 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
1471 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
1473 * This is required only for the V2 bindings, and is called for a matching
1474 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
1475 * will not be freed.
1477 void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
1479 /* Make sure there are no concurrent readers while updating opp_table */
1480 WARN_ON(!list_empty(&opp_table->opp_list));
1482 kfree(opp_table->prop_name);
1483 opp_table->prop_name = NULL;
1485 dev_pm_opp_put_opp_table(opp_table);
1487 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1489 static int _allocate_set_opp_data(struct opp_table *opp_table)
1491 struct dev_pm_set_opp_data *data;
1492 int len, count = opp_table->regulator_count;
1494 if (WARN_ON(!opp_table->regulators))
1497 /* space for set_opp_data */
1498 len = sizeof(*data);
1500 /* space for old_opp.supplies and new_opp.supplies */
1501 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1503 data = kzalloc(len, GFP_KERNEL);
1507 data->old_opp.supplies = (void *)(data + 1);
1508 data->new_opp.supplies = data->old_opp.supplies + count;
1510 opp_table->set_opp_data = data;
1515 static void _free_set_opp_data(struct opp_table *opp_table)
1517 kfree(opp_table->set_opp_data);
1518 opp_table->set_opp_data = NULL;
1522 * dev_pm_opp_set_regulators() - Set regulator names for the device
1523 * @dev: Device for which regulator name is being set.
1524 * @names: Array of pointers to the names of the regulator.
1525 * @count: Number of regulators.
1527 * In order to support OPP switching, OPP layer needs to know the name of the
1528 * device's regulators, as the core would be required to switch voltages as
1531 * This must be called before any OPPs are initialized for the device.
1533 struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1534 const char * const names[],
1537 struct opp_table *opp_table;
1538 struct regulator *reg;
1541 opp_table = dev_pm_opp_get_opp_table(dev);
1543 return ERR_PTR(-ENOMEM);
1545 /* This should be called before OPPs are initialized */
1546 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1551 /* Another CPU that shares the OPP table has set the regulators ? */
1552 if (opp_table->regulators)
1555 opp_table->regulators = kmalloc_array(count,
1556 sizeof(*opp_table->regulators),
1558 if (!opp_table->regulators) {
1563 for (i = 0; i < count; i++) {
1564 reg = regulator_get_optional(dev, names[i]);
1567 if (ret != -EPROBE_DEFER)
1568 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1569 __func__, names[i], ret);
1570 goto free_regulators;
1573 opp_table->regulators[i] = reg;
1576 opp_table->regulator_count = count;
1578 /* Allocate block only once to pass to set_opp() routines */
1579 ret = _allocate_set_opp_data(opp_table);
1581 goto free_regulators;
1587 regulator_put(opp_table->regulators[--i]);
1589 kfree(opp_table->regulators);
1590 opp_table->regulators = NULL;
1591 opp_table->regulator_count = -1;
1593 dev_pm_opp_put_opp_table(opp_table);
1595 return ERR_PTR(ret);
1597 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
1600 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1601 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
1603 void dev_pm_opp_put_regulators(struct opp_table *opp_table)
1607 if (!opp_table->regulators)
1610 /* Make sure there are no concurrent readers while updating opp_table */
1611 WARN_ON(!list_empty(&opp_table->opp_list));
1613 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1614 regulator_put(opp_table->regulators[i]);
1616 _free_set_opp_data(opp_table);
1618 kfree(opp_table->regulators);
1619 opp_table->regulators = NULL;
1620 opp_table->regulator_count = -1;
1623 dev_pm_opp_put_opp_table(opp_table);
1625 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
1628 * dev_pm_opp_set_clkname() - Set clk name for the device
1629 * @dev: Device for which clk name is being set.
1632 * In order to support OPP switching, OPP layer needs to get pointer to the
1633 * clock for the device. Simple cases work fine without using this routine (i.e.
1634 * by passing connection-id as NULL), but for a device with multiple clocks
1635 * available, the OPP core needs to know the exact name of the clk to use.
1637 * This must be called before any OPPs are initialized for the device.
1639 struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
1641 struct opp_table *opp_table;
1644 opp_table = dev_pm_opp_get_opp_table(dev);
1646 return ERR_PTR(-ENOMEM);
1648 /* This should be called before OPPs are initialized */
1649 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1654 /* Already have default clk set, free it */
1655 if (!IS_ERR(opp_table->clk))
1656 clk_put(opp_table->clk);
1658 /* Find clk for the device */
1659 opp_table->clk = clk_get(dev, name);
1660 if (IS_ERR(opp_table->clk)) {
1661 ret = PTR_ERR(opp_table->clk);
1662 if (ret != -EPROBE_DEFER) {
1663 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
1672 dev_pm_opp_put_opp_table(opp_table);
1674 return ERR_PTR(ret);
1676 EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
1679 * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
1680 * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
1682 void dev_pm_opp_put_clkname(struct opp_table *opp_table)
1684 /* Make sure there are no concurrent readers while updating opp_table */
1685 WARN_ON(!list_empty(&opp_table->opp_list));
1687 clk_put(opp_table->clk);
1688 opp_table->clk = ERR_PTR(-EINVAL);
1690 dev_pm_opp_put_opp_table(opp_table);
1692 EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
1695 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1696 * @dev: Device for which the helper is getting registered.
1697 * @set_opp: Custom set OPP helper.
1699 * This is useful to support complex platforms (like platforms with multiple
1700 * regulators per device), instead of the generic OPP set rate helper.
1702 * This must be called before any OPPs are initialized for the device.
1704 struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
1705 int (*set_opp)(struct dev_pm_set_opp_data *data))
1707 struct opp_table *opp_table;
1710 return ERR_PTR(-EINVAL);
1712 opp_table = dev_pm_opp_get_opp_table(dev);
1714 return ERR_PTR(-ENOMEM);
1716 /* This should be called before OPPs are initialized */
1717 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1718 dev_pm_opp_put_opp_table(opp_table);
1719 return ERR_PTR(-EBUSY);
1722 /* Another CPU that shares the OPP table has set the helper ? */
1723 if (!opp_table->set_opp)
1724 opp_table->set_opp = set_opp;
1728 EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1731 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
1733 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
1735 * Release resources blocked for platform specific set_opp helper.
1737 void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
1739 /* Make sure there are no concurrent readers while updating opp_table */
1740 WARN_ON(!list_empty(&opp_table->opp_list));
1742 opp_table->set_opp = NULL;
1743 dev_pm_opp_put_opp_table(opp_table);
1745 EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
1748 * dev_pm_opp_set_genpd_virt_dev - Set virtual genpd device for an index
1749 * @dev: Consumer device for which the genpd device is getting set.
1750 * @virt_dev: virtual genpd device.
1753 * Multiple generic power domains for a device are supported with the help of
1754 * virtual genpd devices, which are created for each consumer device - genpd
1755 * pair. These are the device structures which are attached to the power domain
1756 * and are required by the OPP core to set the performance state of the genpd.
1758 * This helper will normally be called by the consumer driver of the device
1759 * "dev", as only that has details of the genpd devices.
1761 * This helper needs to be called once for each of those virtual devices, but
1762 * only if multiple domains are available for a device. Otherwise the original
1763 * device structure will be used instead by the OPP core.
1765 struct opp_table *dev_pm_opp_set_genpd_virt_dev(struct device *dev,
1766 struct device *virt_dev,
1769 struct opp_table *opp_table;
1771 opp_table = dev_pm_opp_get_opp_table(dev);
1773 return ERR_PTR(-ENOMEM);
1775 mutex_lock(&opp_table->genpd_virt_dev_lock);
1777 if (unlikely(!opp_table->genpd_virt_devs ||
1778 index >= opp_table->required_opp_count ||
1779 opp_table->genpd_virt_devs[index])) {
1781 dev_err(dev, "Invalid request to set required device\n");
1782 dev_pm_opp_put_opp_table(opp_table);
1783 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1785 return ERR_PTR(-EINVAL);
1788 opp_table->genpd_virt_devs[index] = virt_dev;
1789 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1795 * dev_pm_opp_put_genpd_virt_dev() - Releases resources blocked for genpd device.
1796 * @opp_table: OPP table returned by dev_pm_opp_set_genpd_virt_dev().
1797 * @virt_dev: virtual genpd device.
1799 * This releases the resource previously acquired with a call to
1800 * dev_pm_opp_set_genpd_virt_dev(). The consumer driver shall call this helper
1801 * if it doesn't want OPP core to update performance state of a power domain
1804 void dev_pm_opp_put_genpd_virt_dev(struct opp_table *opp_table,
1805 struct device *virt_dev)
1810 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
1813 mutex_lock(&opp_table->genpd_virt_dev_lock);
1815 for (i = 0; i < opp_table->required_opp_count; i++) {
1816 if (opp_table->genpd_virt_devs[i] != virt_dev)
1819 opp_table->genpd_virt_devs[i] = NULL;
1820 dev_pm_opp_put_opp_table(opp_table);
1823 dev_pm_genpd_set_performance_state(virt_dev, 0);
1827 mutex_unlock(&opp_table->genpd_virt_dev_lock);
1829 if (unlikely(i == opp_table->required_opp_count))
1830 dev_err(virt_dev, "Failed to find required device entry\n");
1834 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
1835 * @src_table: OPP table which has dst_table as one of its required OPP table.
1836 * @dst_table: Required OPP table of the src_table.
1837 * @pstate: Current performance state of the src_table.
1839 * This Returns pstate of the OPP (present in @dst_table) pointed out by the
1840 * "required-opps" property of the OPP (present in @src_table) which has
1841 * performance state set to @pstate.
1843 * Return: Zero or positive performance state on success, otherwise negative
1846 int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
1847 struct opp_table *dst_table,
1848 unsigned int pstate)
1850 struct dev_pm_opp *opp;
1851 int dest_pstate = -EINVAL;
1858 * Normally the src_table will have the "required_opps" property set to
1859 * point to one of the OPPs in the dst_table, but in some cases the
1860 * genpd and its master have one to one mapping of performance states
1861 * and so none of them have the "required-opps" property set. Return the
1862 * pstate of the src_table as it is in such cases.
1864 if (!src_table->required_opp_count)
1867 for (i = 0; i < src_table->required_opp_count; i++) {
1868 if (src_table->required_opp_tables[i]->np == dst_table->np)
1872 if (unlikely(i == src_table->required_opp_count)) {
1873 pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
1874 __func__, src_table, dst_table);
1878 mutex_lock(&src_table->lock);
1880 list_for_each_entry(opp, &src_table->opp_list, node) {
1881 if (opp->pstate == pstate) {
1882 dest_pstate = opp->required_opps[i]->pstate;
1887 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
1891 mutex_unlock(&src_table->lock);
1897 * dev_pm_opp_add() - Add an OPP table from a table definitions
1898 * @dev: device for which we do this operation
1899 * @freq: Frequency in Hz for this OPP
1900 * @u_volt: Voltage in uVolts for this OPP
1902 * This function adds an opp definition to the opp table and returns status.
1903 * The opp is made available by default and it can be controlled using
1904 * dev_pm_opp_enable/disable functions.
1908 * Duplicate OPPs (both freq and volt are same) and opp->available
1909 * -EEXIST Freq are same and volt are different OR
1910 * Duplicate OPPs (both freq and volt are same) and !opp->available
1911 * -ENOMEM Memory allocation failure
1913 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1915 struct opp_table *opp_table;
1918 opp_table = dev_pm_opp_get_opp_table(dev);
1922 /* Fix regulator count for dynamic OPPs */
1923 opp_table->regulator_count = 1;
1925 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
1927 dev_pm_opp_put_opp_table(opp_table);
1931 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1934 * _opp_set_availability() - helper to set the availability of an opp
1935 * @dev: device for which we do this operation
1936 * @freq: OPP frequency to modify availability
1937 * @availability_req: availability status requested for this opp
1939 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1940 * which is isolated here.
1942 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1943 * copy operation, returns 0 if no modification was done OR modification was
1946 static int _opp_set_availability(struct device *dev, unsigned long freq,
1947 bool availability_req)
1949 struct opp_table *opp_table;
1950 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
1953 /* Find the opp_table */
1954 opp_table = _find_opp_table(dev);
1955 if (IS_ERR(opp_table)) {
1956 r = PTR_ERR(opp_table);
1957 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1961 mutex_lock(&opp_table->lock);
1963 /* Do we have the frequency? */
1964 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
1965 if (tmp_opp->rate == freq) {
1976 /* Is update really needed? */
1977 if (opp->available == availability_req)
1980 opp->available = availability_req;
1982 dev_pm_opp_get(opp);
1983 mutex_unlock(&opp_table->lock);
1985 /* Notify the change of the OPP availability */
1986 if (availability_req)
1987 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
1990 blocking_notifier_call_chain(&opp_table->head,
1991 OPP_EVENT_DISABLE, opp);
1993 dev_pm_opp_put(opp);
1997 mutex_unlock(&opp_table->lock);
1999 dev_pm_opp_put_opp_table(opp_table);
2004 * dev_pm_opp_enable() - Enable a specific OPP
2005 * @dev: device for which we do this operation
2006 * @freq: OPP frequency to enable
2008 * Enables a provided opp. If the operation is valid, this returns 0, else the
2009 * corresponding error value. It is meant to be used for users an OPP available
2010 * after being temporarily made unavailable with dev_pm_opp_disable.
2012 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2013 * copy operation, returns 0 if no modification was done OR modification was
2016 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
2018 return _opp_set_availability(dev, freq, true);
2020 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
2023 * dev_pm_opp_disable() - Disable a specific OPP
2024 * @dev: device for which we do this operation
2025 * @freq: OPP frequency to disable
2027 * Disables a provided opp. If the operation is valid, this returns
2028 * 0, else the corresponding error value. It is meant to be a temporary
2029 * control by users to make this OPP not available until the circumstances are
2030 * right to make it available again (with a call to dev_pm_opp_enable).
2032 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
2033 * copy operation, returns 0 if no modification was done OR modification was
2036 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
2038 return _opp_set_availability(dev, freq, false);
2040 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
2043 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
2044 * @dev: Device for which notifier needs to be registered
2045 * @nb: Notifier block to be registered
2047 * Return: 0 on success or a negative error value.
2049 int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
2051 struct opp_table *opp_table;
2054 opp_table = _find_opp_table(dev);
2055 if (IS_ERR(opp_table))
2056 return PTR_ERR(opp_table);
2058 ret = blocking_notifier_chain_register(&opp_table->head, nb);
2060 dev_pm_opp_put_opp_table(opp_table);
2064 EXPORT_SYMBOL(dev_pm_opp_register_notifier);
2067 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
2068 * @dev: Device for which notifier needs to be unregistered
2069 * @nb: Notifier block to be unregistered
2071 * Return: 0 on success or a negative error value.
2073 int dev_pm_opp_unregister_notifier(struct device *dev,
2074 struct notifier_block *nb)
2076 struct opp_table *opp_table;
2079 opp_table = _find_opp_table(dev);
2080 if (IS_ERR(opp_table))
2081 return PTR_ERR(opp_table);
2083 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
2085 dev_pm_opp_put_opp_table(opp_table);
2089 EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
2091 void _dev_pm_opp_find_and_remove_table(struct device *dev)
2093 struct opp_table *opp_table;
2095 /* Check for existing table for 'dev' */
2096 opp_table = _find_opp_table(dev);
2097 if (IS_ERR(opp_table)) {
2098 int error = PTR_ERR(opp_table);
2100 if (error != -ENODEV)
2101 WARN(1, "%s: opp_table: %d\n",
2102 IS_ERR_OR_NULL(dev) ?
2103 "Invalid device" : dev_name(dev),
2108 _put_opp_list_kref(opp_table);
2110 /* Drop reference taken by _find_opp_table() */
2111 dev_pm_opp_put_opp_table(opp_table);
2113 /* Drop reference taken while the OPP table was added */
2114 dev_pm_opp_put_opp_table(opp_table);
2118 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
2119 * @dev: device pointer used to lookup OPP table.
2121 * Free both OPPs created using static entries present in DT and the
2122 * dynamically added entries.
2124 void dev_pm_opp_remove_table(struct device *dev)
2126 _dev_pm_opp_find_and_remove_table(dev);
2128 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);