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/regulator/consumer.h>
27 * The root of the list of all opp-tables. All opp_table structures branch off
28 * from here, with each opp_table containing the list of opps it supports in
29 * various states of availability.
31 LIST_HEAD(opp_tables);
32 /* Lock to allow exclusive modification to the device and opp lists */
33 DEFINE_MUTEX(opp_table_lock);
35 static void dev_pm_opp_get(struct dev_pm_opp *opp);
37 static struct opp_device *_find_opp_dev(const struct device *dev,
38 struct opp_table *opp_table)
40 struct opp_device *opp_dev;
42 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
43 if (opp_dev->dev == dev)
49 static struct opp_table *_find_opp_table_unlocked(struct device *dev)
51 struct opp_table *opp_table;
53 list_for_each_entry(opp_table, &opp_tables, node) {
54 if (_find_opp_dev(dev, opp_table)) {
55 _get_opp_table_kref(opp_table);
61 return ERR_PTR(-ENODEV);
65 * _find_opp_table() - find opp_table struct using device pointer
66 * @dev: device pointer used to lookup OPP table
68 * Search OPP table for one containing matching device.
70 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
71 * -EINVAL based on type of error.
73 * The callers must call dev_pm_opp_put_opp_table() after the table is used.
75 struct opp_table *_find_opp_table(struct device *dev)
77 struct opp_table *opp_table;
79 if (IS_ERR_OR_NULL(dev)) {
80 pr_err("%s: Invalid parameters\n", __func__);
81 return ERR_PTR(-EINVAL);
84 mutex_lock(&opp_table_lock);
85 opp_table = _find_opp_table_unlocked(dev);
86 mutex_unlock(&opp_table_lock);
92 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
93 * @opp: opp for which voltage has to be returned for
95 * Return: voltage in micro volt corresponding to the opp, else
98 * This is useful only for devices with single power supply.
100 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
102 if (IS_ERR_OR_NULL(opp)) {
103 pr_err("%s: Invalid parameters\n", __func__);
107 return opp->supplies[0].u_volt;
109 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
112 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
113 * @opp: opp for which frequency has to be returned for
115 * Return: frequency in hertz corresponding to the opp, else
118 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
120 if (IS_ERR_OR_NULL(opp) || !opp->available) {
121 pr_err("%s: Invalid parameters\n", __func__);
127 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
130 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
131 * @opp: opp for which turbo mode is being verified
133 * Turbo OPPs are not for normal use, and can be enabled (under certain
134 * conditions) for short duration of times to finish high throughput work
135 * quickly. Running on them for longer times may overheat the chip.
137 * Return: true if opp is turbo opp, else false.
139 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
141 if (IS_ERR_OR_NULL(opp) || !opp->available) {
142 pr_err("%s: Invalid parameters\n", __func__);
148 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
151 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
152 * @dev: device for which we do this operation
154 * Return: This function returns the max clock latency in nanoseconds.
156 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
158 struct opp_table *opp_table;
159 unsigned long clock_latency_ns;
161 opp_table = _find_opp_table(dev);
162 if (IS_ERR(opp_table))
165 clock_latency_ns = opp_table->clock_latency_ns_max;
167 dev_pm_opp_put_opp_table(opp_table);
169 return clock_latency_ns;
171 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
174 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
175 * @dev: device for which we do this operation
177 * Return: This function returns the max voltage latency in nanoseconds.
179 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
181 struct opp_table *opp_table;
182 struct dev_pm_opp *opp;
183 struct regulator *reg, **regulators;
184 unsigned long latency_ns = 0;
191 opp_table = _find_opp_table(dev);
192 if (IS_ERR(opp_table))
195 count = opp_table->regulator_count;
197 /* Regulator may not be required for the device */
201 regulators = kmalloc_array(count, sizeof(*regulators), GFP_KERNEL);
205 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
207 goto free_regulators;
209 memcpy(regulators, opp_table->regulators, count * sizeof(*regulators));
211 mutex_lock(&opp_table->lock);
213 for (i = 0; i < count; i++) {
217 list_for_each_entry(opp, &opp_table->opp_list, node) {
221 if (opp->supplies[i].u_volt_min < uV[i].min)
222 uV[i].min = opp->supplies[i].u_volt_min;
223 if (opp->supplies[i].u_volt_max > uV[i].max)
224 uV[i].max = opp->supplies[i].u_volt_max;
228 mutex_unlock(&opp_table->lock);
231 * The caller needs to ensure that opp_table (and hence the regulator)
232 * isn't freed, while we are executing this routine.
234 for (i = 0; i < count; i++) {
236 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
238 latency_ns += ret * 1000;
245 dev_pm_opp_put_opp_table(opp_table);
249 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
252 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
254 * @dev: device for which we do this operation
256 * Return: This function returns the max transition latency, in nanoseconds, to
257 * switch from one OPP to other.
259 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
261 return dev_pm_opp_get_max_volt_latency(dev) +
262 dev_pm_opp_get_max_clock_latency(dev);
264 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
267 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
268 * @dev: device for which we do this operation
270 * Return: This function returns the frequency of the OPP marked as suspend_opp
271 * if one is available, else returns 0;
273 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
275 struct opp_table *opp_table;
276 unsigned long freq = 0;
278 opp_table = _find_opp_table(dev);
279 if (IS_ERR(opp_table))
282 if (opp_table->suspend_opp && opp_table->suspend_opp->available)
283 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
285 dev_pm_opp_put_opp_table(opp_table);
289 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
292 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
293 * @dev: device for which we do this operation
295 * Return: This function returns the number of available opps if there are any,
296 * else returns 0 if none or the corresponding error value.
298 int dev_pm_opp_get_opp_count(struct device *dev)
300 struct opp_table *opp_table;
301 struct dev_pm_opp *temp_opp;
304 opp_table = _find_opp_table(dev);
305 if (IS_ERR(opp_table)) {
306 count = PTR_ERR(opp_table);
307 dev_err(dev, "%s: OPP table not found (%d)\n",
312 mutex_lock(&opp_table->lock);
314 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
315 if (temp_opp->available)
319 mutex_unlock(&opp_table->lock);
320 dev_pm_opp_put_opp_table(opp_table);
324 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
327 * dev_pm_opp_find_freq_exact() - search for an exact frequency
328 * @dev: device for which we do this operation
329 * @freq: frequency to search for
330 * @available: true/false - match for available opp
332 * Return: Searches for exact match in the opp table and returns pointer to the
333 * matching opp if found, else returns ERR_PTR in case of error and should
334 * be handled using IS_ERR. Error return values can be:
335 * EINVAL: for bad pointer
336 * ERANGE: no match found for search
337 * ENODEV: if device not found in list of registered devices
339 * Note: available is a modifier for the search. if available=true, then the
340 * match is for exact matching frequency and is available in the stored OPP
341 * table. if false, the match is for exact frequency which is not available.
343 * This provides a mechanism to enable an opp which is not available currently
344 * or the opposite as well.
346 * The callers are required to call dev_pm_opp_put() for the returned OPP after
349 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
353 struct opp_table *opp_table;
354 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
356 opp_table = _find_opp_table(dev);
357 if (IS_ERR(opp_table)) {
358 int r = PTR_ERR(opp_table);
360 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
364 mutex_lock(&opp_table->lock);
366 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
367 if (temp_opp->available == available &&
368 temp_opp->rate == freq) {
371 /* Increment the reference count of OPP */
377 mutex_unlock(&opp_table->lock);
378 dev_pm_opp_put_opp_table(opp_table);
382 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
384 static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
387 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
389 mutex_lock(&opp_table->lock);
391 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
392 if (temp_opp->available && temp_opp->rate >= *freq) {
396 /* Increment the reference count of OPP */
402 mutex_unlock(&opp_table->lock);
408 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
409 * @dev: device for which we do this operation
410 * @freq: Start frequency
412 * Search for the matching ceil *available* OPP from a starting freq
415 * Return: matching *opp and refreshes *freq accordingly, else returns
416 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
418 * EINVAL: for bad pointer
419 * ERANGE: no match found for search
420 * ENODEV: if device not found in list of registered devices
422 * The callers are required to call dev_pm_opp_put() for the returned OPP after
425 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
428 struct opp_table *opp_table;
429 struct dev_pm_opp *opp;
432 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
433 return ERR_PTR(-EINVAL);
436 opp_table = _find_opp_table(dev);
437 if (IS_ERR(opp_table))
438 return ERR_CAST(opp_table);
440 opp = _find_freq_ceil(opp_table, freq);
442 dev_pm_opp_put_opp_table(opp_table);
446 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
449 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
450 * @dev: device for which we do this operation
451 * @freq: Start frequency
453 * Search for the matching floor *available* OPP from a starting freq
456 * Return: matching *opp and refreshes *freq accordingly, else returns
457 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
459 * EINVAL: for bad pointer
460 * ERANGE: no match found for search
461 * ENODEV: if device not found in list of registered devices
463 * The callers are required to call dev_pm_opp_put() for the returned OPP after
466 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
469 struct opp_table *opp_table;
470 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
473 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
474 return ERR_PTR(-EINVAL);
477 opp_table = _find_opp_table(dev);
478 if (IS_ERR(opp_table))
479 return ERR_CAST(opp_table);
481 mutex_lock(&opp_table->lock);
483 list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
484 if (temp_opp->available) {
485 /* go to the next node, before choosing prev */
486 if (temp_opp->rate > *freq)
493 /* Increment the reference count of OPP */
496 mutex_unlock(&opp_table->lock);
497 dev_pm_opp_put_opp_table(opp_table);
504 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
506 static int _set_opp_voltage(struct device *dev, struct regulator *reg,
507 struct dev_pm_opp_supply *supply)
511 /* Regulator not available for device */
513 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
518 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
519 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
521 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
522 supply->u_volt, supply->u_volt_max);
524 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
525 __func__, supply->u_volt_min, supply->u_volt,
526 supply->u_volt_max, ret);
532 _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
533 unsigned long old_freq, unsigned long freq)
537 ret = clk_set_rate(clk, freq);
539 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
546 static int _generic_set_opp(struct dev_pm_set_opp_data *data)
548 struct dev_pm_opp_supply *old_supply = data->old_opp.supplies;
549 struct dev_pm_opp_supply *new_supply = data->new_opp.supplies;
550 unsigned long old_freq = data->old_opp.rate, freq = data->new_opp.rate;
551 struct regulator *reg = data->regulators[0];
552 struct device *dev= data->dev;
555 /* This function only supports single regulator per device */
556 if (WARN_ON(data->regulator_count > 1)) {
557 dev_err(dev, "multiple regulators are not supported\n");
561 /* Scaling up? Scale voltage before frequency */
562 if (freq > old_freq) {
563 ret = _set_opp_voltage(dev, reg, new_supply);
565 goto restore_voltage;
568 /* Change frequency */
569 ret = _generic_set_opp_clk_only(dev, data->clk, old_freq, freq);
571 goto restore_voltage;
573 /* Scaling down? Scale voltage after frequency */
574 if (freq < old_freq) {
575 ret = _set_opp_voltage(dev, reg, new_supply);
583 if (_generic_set_opp_clk_only(dev, data->clk, freq, old_freq))
584 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
587 /* This shouldn't harm even if the voltages weren't updated earlier */
588 if (old_supply->u_volt)
589 _set_opp_voltage(dev, reg, old_supply);
595 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
596 * @dev: device for which we do this operation
597 * @target_freq: frequency to achieve
599 * This configures the power-supplies and clock source to the levels specified
600 * by the OPP corresponding to the target_freq.
602 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
604 struct opp_table *opp_table;
605 unsigned long freq, old_freq;
606 int (*set_opp)(struct dev_pm_set_opp_data *data);
607 struct dev_pm_opp *old_opp, *opp;
608 struct regulator **regulators;
609 struct dev_pm_set_opp_data *data;
613 if (unlikely(!target_freq)) {
614 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
619 opp_table = _find_opp_table(dev);
620 if (IS_ERR(opp_table)) {
621 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
622 return PTR_ERR(opp_table);
625 clk = opp_table->clk;
627 dev_err(dev, "%s: No clock available for the device\n",
633 freq = clk_round_rate(clk, target_freq);
637 old_freq = clk_get_rate(clk);
639 /* Return early if nothing to do */
640 if (old_freq == freq) {
641 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
647 old_opp = _find_freq_ceil(opp_table, &old_freq);
648 if (IS_ERR(old_opp)) {
649 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
650 __func__, old_freq, PTR_ERR(old_opp));
653 opp = _find_freq_ceil(opp_table, &freq);
656 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
657 __func__, freq, ret);
661 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
664 regulators = opp_table->regulators;
666 /* Only frequency scaling */
668 ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
672 if (opp_table->set_opp)
673 set_opp = opp_table->set_opp;
675 set_opp = _generic_set_opp;
677 data = opp_table->set_opp_data;
678 data->regulators = regulators;
679 data->regulator_count = opp_table->regulator_count;
683 data->old_opp.rate = old_freq;
684 size = sizeof(*opp->supplies) * opp_table->regulator_count;
686 memset(data->old_opp.supplies, 0, size);
688 memcpy(data->old_opp.supplies, old_opp->supplies, size);
690 data->new_opp.rate = freq;
691 memcpy(data->new_opp.supplies, opp->supplies, size);
698 if (!IS_ERR(old_opp))
699 dev_pm_opp_put(old_opp);
701 dev_pm_opp_put_opp_table(opp_table);
704 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
706 /* OPP-dev Helpers */
707 static void _remove_opp_dev(struct opp_device *opp_dev,
708 struct opp_table *opp_table)
710 opp_debug_unregister(opp_dev, opp_table);
711 list_del(&opp_dev->node);
715 struct opp_device *_add_opp_dev(const struct device *dev,
716 struct opp_table *opp_table)
718 struct opp_device *opp_dev;
721 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
725 /* Initialize opp-dev */
727 list_add(&opp_dev->node, &opp_table->dev_list);
729 /* Create debugfs entries for the opp_table */
730 ret = opp_debug_register(opp_dev, opp_table);
732 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
738 static struct opp_table *_allocate_opp_table(struct device *dev)
740 struct opp_table *opp_table;
741 struct opp_device *opp_dev;
745 * Allocate a new OPP table. In the infrequent case where a new
746 * device is needed to be added, we pay this penalty.
748 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
752 INIT_LIST_HEAD(&opp_table->dev_list);
754 opp_dev = _add_opp_dev(dev, opp_table);
760 _of_init_opp_table(opp_table, dev);
762 /* Find clk for the device */
763 opp_table->clk = clk_get(dev, NULL);
764 if (IS_ERR(opp_table->clk)) {
765 ret = PTR_ERR(opp_table->clk);
766 if (ret != -EPROBE_DEFER)
767 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
771 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
772 INIT_LIST_HEAD(&opp_table->opp_list);
773 mutex_init(&opp_table->lock);
774 kref_init(&opp_table->kref);
776 /* Secure the device table modification */
777 list_add(&opp_table->node, &opp_tables);
781 void _get_opp_table_kref(struct opp_table *opp_table)
783 kref_get(&opp_table->kref);
786 struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
788 struct opp_table *opp_table;
790 /* Hold our table modification lock here */
791 mutex_lock(&opp_table_lock);
793 opp_table = _find_opp_table_unlocked(dev);
794 if (!IS_ERR(opp_table))
797 opp_table = _allocate_opp_table(dev);
800 mutex_unlock(&opp_table_lock);
804 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
806 static void _opp_table_kref_release(struct kref *kref)
808 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
809 struct opp_device *opp_dev;
812 if (!IS_ERR(opp_table->clk))
813 clk_put(opp_table->clk);
815 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
818 _remove_opp_dev(opp_dev, opp_table);
820 /* dev_list must be empty now */
821 WARN_ON(!list_empty(&opp_table->dev_list));
823 mutex_destroy(&opp_table->lock);
824 list_del(&opp_table->node);
827 mutex_unlock(&opp_table_lock);
830 void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
832 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
835 EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
837 void _opp_free(struct dev_pm_opp *opp)
842 static void _opp_kref_release(struct kref *kref)
844 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
845 struct opp_table *opp_table = opp->opp_table;
848 * Notify the changes in the availability of the operable
849 * frequency/voltage list.
851 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
852 opp_debug_remove_one(opp);
853 list_del(&opp->node);
856 mutex_unlock(&opp_table->lock);
857 dev_pm_opp_put_opp_table(opp_table);
860 static void dev_pm_opp_get(struct dev_pm_opp *opp)
862 kref_get(&opp->kref);
865 void dev_pm_opp_put(struct dev_pm_opp *opp)
867 kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
869 EXPORT_SYMBOL_GPL(dev_pm_opp_put);
872 * dev_pm_opp_remove() - Remove an OPP from OPP table
873 * @dev: device for which we do this operation
874 * @freq: OPP to remove with matching 'freq'
876 * This function removes an opp from the opp table.
878 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
880 struct dev_pm_opp *opp;
881 struct opp_table *opp_table;
884 opp_table = _find_opp_table(dev);
885 if (IS_ERR(opp_table))
888 mutex_lock(&opp_table->lock);
890 list_for_each_entry(opp, &opp_table->opp_list, node) {
891 if (opp->rate == freq) {
897 mutex_unlock(&opp_table->lock);
902 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
906 dev_pm_opp_put_opp_table(opp_table);
908 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
910 struct dev_pm_opp *_opp_allocate(struct opp_table *table)
912 struct dev_pm_opp *opp;
913 int count, supply_size;
915 /* Allocate space for at least one supply */
916 count = table->regulator_count ? table->regulator_count : 1;
917 supply_size = sizeof(*opp->supplies) * count;
919 /* allocate new OPP node and supplies structures */
920 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
924 /* Put the supplies at the end of the OPP structure as an empty array */
925 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
926 INIT_LIST_HEAD(&opp->node);
931 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
932 struct opp_table *opp_table)
934 struct regulator *reg;
937 for (i = 0; i < opp_table->regulator_count; i++) {
938 reg = opp_table->regulators[i];
940 if (!regulator_is_supported_voltage(reg,
941 opp->supplies[i].u_volt_min,
942 opp->supplies[i].u_volt_max)) {
943 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
944 __func__, opp->supplies[i].u_volt_min,
945 opp->supplies[i].u_volt_max);
955 * 0: On success. And appropriate error message for duplicate OPPs.
956 * -EBUSY: For OPP with same freq/volt and is available. The callers of
957 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
958 * sure we don't print error messages unnecessarily if different parts of
959 * kernel try to initialize the OPP table.
960 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
961 * should be considered an error by the callers of _opp_add().
963 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
964 struct opp_table *opp_table)
966 struct dev_pm_opp *opp;
967 struct list_head *head;
971 * Insert new OPP in order of increasing frequency and discard if
974 * Need to use &opp_table->opp_list in the condition part of the 'for'
975 * loop, don't replace it with head otherwise it will become an infinite
978 mutex_lock(&opp_table->lock);
979 head = &opp_table->opp_list;
981 list_for_each_entry(opp, &opp_table->opp_list, node) {
982 if (new_opp->rate > opp->rate) {
987 if (new_opp->rate < opp->rate)
991 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
992 __func__, opp->rate, opp->supplies[0].u_volt,
993 opp->available, new_opp->rate,
994 new_opp->supplies[0].u_volt, new_opp->available);
996 /* Should we compare voltages for all regulators here ? */
997 ret = opp->available &&
998 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1000 mutex_unlock(&opp_table->lock);
1004 list_add(&new_opp->node, head);
1005 mutex_unlock(&opp_table->lock);
1007 new_opp->opp_table = opp_table;
1008 kref_init(&new_opp->kref);
1010 /* Get a reference to the OPP table */
1011 _get_opp_table_kref(opp_table);
1013 ret = opp_debug_create_one(new_opp, opp_table);
1015 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1018 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
1019 new_opp->available = false;
1020 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1021 __func__, new_opp->rate);
1028 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
1029 * @opp_table: OPP table
1030 * @dev: device for which we do this operation
1031 * @freq: Frequency in Hz for this OPP
1032 * @u_volt: Voltage in uVolts for this OPP
1033 * @dynamic: Dynamically added OPPs.
1035 * This function adds an opp definition to the opp table and returns status.
1036 * The opp is made available by default and it can be controlled using
1037 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1039 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1040 * and freed by dev_pm_opp_of_remove_table.
1044 * Duplicate OPPs (both freq and volt are same) and opp->available
1045 * -EEXIST Freq are same and volt are different OR
1046 * Duplicate OPPs (both freq and volt are same) and !opp->available
1047 * -ENOMEM Memory allocation failure
1049 int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1050 unsigned long freq, long u_volt, bool dynamic)
1052 struct dev_pm_opp *new_opp;
1056 new_opp = _opp_allocate(opp_table);
1060 /* populate the opp table */
1061 new_opp->rate = freq;
1062 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
1063 new_opp->supplies[0].u_volt = u_volt;
1064 new_opp->supplies[0].u_volt_min = u_volt - tol;
1065 new_opp->supplies[0].u_volt_max = u_volt + tol;
1066 new_opp->available = true;
1067 new_opp->dynamic = dynamic;
1069 ret = _opp_add(dev, new_opp, opp_table);
1071 /* Don't return error for duplicate OPPs */
1078 * Notify the changes in the availability of the operable
1079 * frequency/voltage list.
1081 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
1091 * dev_pm_opp_set_supported_hw() - Set supported platforms
1092 * @dev: Device for which supported-hw has to be set.
1093 * @versions: Array of hierarchy of versions to match.
1094 * @count: Number of elements in the array.
1096 * This is required only for the V2 bindings, and it enables a platform to
1097 * specify the hierarchy of versions it supports. OPP layer will then enable
1098 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1101 struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1102 const u32 *versions, unsigned int count)
1104 struct opp_table *opp_table;
1107 opp_table = dev_pm_opp_get_opp_table(dev);
1109 return ERR_PTR(-ENOMEM);
1111 /* Make sure there are no concurrent readers while updating opp_table */
1112 WARN_ON(!list_empty(&opp_table->opp_list));
1114 /* Do we already have a version hierarchy associated with opp_table? */
1115 if (opp_table->supported_hw) {
1116 dev_err(dev, "%s: Already have supported hardware list\n",
1122 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
1124 if (!opp_table->supported_hw) {
1129 opp_table->supported_hw_count = count;
1134 dev_pm_opp_put_opp_table(opp_table);
1136 return ERR_PTR(ret);
1138 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1141 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
1142 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
1144 * This is required only for the V2 bindings, and is called for a matching
1145 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
1146 * will not be freed.
1148 void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
1150 /* Make sure there are no concurrent readers while updating opp_table */
1151 WARN_ON(!list_empty(&opp_table->opp_list));
1153 if (!opp_table->supported_hw) {
1154 pr_err("%s: Doesn't have supported hardware list\n",
1159 kfree(opp_table->supported_hw);
1160 opp_table->supported_hw = NULL;
1161 opp_table->supported_hw_count = 0;
1163 dev_pm_opp_put_opp_table(opp_table);
1165 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1168 * dev_pm_opp_set_prop_name() - Set prop-extn name
1169 * @dev: Device for which the prop-name has to be set.
1170 * @name: name to postfix to properties.
1172 * This is required only for the V2 bindings, and it enables a platform to
1173 * specify the extn to be used for certain property names. The properties to
1174 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1175 * should postfix the property name with -<name> while looking for them.
1177 struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1179 struct opp_table *opp_table;
1182 opp_table = dev_pm_opp_get_opp_table(dev);
1184 return ERR_PTR(-ENOMEM);
1186 /* Make sure there are no concurrent readers while updating opp_table */
1187 WARN_ON(!list_empty(&opp_table->opp_list));
1189 /* Do we already have a prop-name associated with opp_table? */
1190 if (opp_table->prop_name) {
1191 dev_err(dev, "%s: Already have prop-name %s\n", __func__,
1192 opp_table->prop_name);
1197 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1198 if (!opp_table->prop_name) {
1206 dev_pm_opp_put_opp_table(opp_table);
1208 return ERR_PTR(ret);
1210 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1213 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
1214 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
1216 * This is required only for the V2 bindings, and is called for a matching
1217 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
1218 * will not be freed.
1220 void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
1222 /* Make sure there are no concurrent readers while updating opp_table */
1223 WARN_ON(!list_empty(&opp_table->opp_list));
1225 if (!opp_table->prop_name) {
1226 pr_err("%s: Doesn't have a prop-name\n", __func__);
1230 kfree(opp_table->prop_name);
1231 opp_table->prop_name = NULL;
1233 dev_pm_opp_put_opp_table(opp_table);
1235 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1237 static int _allocate_set_opp_data(struct opp_table *opp_table)
1239 struct dev_pm_set_opp_data *data;
1240 int len, count = opp_table->regulator_count;
1242 if (WARN_ON(!count))
1245 /* space for set_opp_data */
1246 len = sizeof(*data);
1248 /* space for old_opp.supplies and new_opp.supplies */
1249 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1251 data = kzalloc(len, GFP_KERNEL);
1255 data->old_opp.supplies = (void *)(data + 1);
1256 data->new_opp.supplies = data->old_opp.supplies + count;
1258 opp_table->set_opp_data = data;
1263 static void _free_set_opp_data(struct opp_table *opp_table)
1265 kfree(opp_table->set_opp_data);
1266 opp_table->set_opp_data = NULL;
1270 * dev_pm_opp_set_regulators() - Set regulator names for the device
1271 * @dev: Device for which regulator name is being set.
1272 * @names: Array of pointers to the names of the regulator.
1273 * @count: Number of regulators.
1275 * In order to support OPP switching, OPP layer needs to know the name of the
1276 * device's regulators, as the core would be required to switch voltages as
1279 * This must be called before any OPPs are initialized for the device.
1281 struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1282 const char * const names[],
1285 struct opp_table *opp_table;
1286 struct regulator *reg;
1289 opp_table = dev_pm_opp_get_opp_table(dev);
1291 return ERR_PTR(-ENOMEM);
1293 /* This should be called before OPPs are initialized */
1294 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1299 /* Already have regulators set */
1300 if (opp_table->regulators) {
1305 opp_table->regulators = kmalloc_array(count,
1306 sizeof(*opp_table->regulators),
1308 if (!opp_table->regulators) {
1313 for (i = 0; i < count; i++) {
1314 reg = regulator_get_optional(dev, names[i]);
1317 if (ret != -EPROBE_DEFER)
1318 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1319 __func__, names[i], ret);
1320 goto free_regulators;
1323 opp_table->regulators[i] = reg;
1326 opp_table->regulator_count = count;
1328 /* Allocate block only once to pass to set_opp() routines */
1329 ret = _allocate_set_opp_data(opp_table);
1331 goto free_regulators;
1337 regulator_put(opp_table->regulators[--i]);
1339 kfree(opp_table->regulators);
1340 opp_table->regulators = NULL;
1341 opp_table->regulator_count = 0;
1343 dev_pm_opp_put_opp_table(opp_table);
1345 return ERR_PTR(ret);
1347 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
1350 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1351 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
1353 void dev_pm_opp_put_regulators(struct opp_table *opp_table)
1357 if (!opp_table->regulators) {
1358 pr_err("%s: Doesn't have regulators set\n", __func__);
1362 /* Make sure there are no concurrent readers while updating opp_table */
1363 WARN_ON(!list_empty(&opp_table->opp_list));
1365 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1366 regulator_put(opp_table->regulators[i]);
1368 _free_set_opp_data(opp_table);
1370 kfree(opp_table->regulators);
1371 opp_table->regulators = NULL;
1372 opp_table->regulator_count = 0;
1374 dev_pm_opp_put_opp_table(opp_table);
1376 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
1379 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1380 * @dev: Device for which the helper is getting registered.
1381 * @set_opp: Custom set OPP helper.
1383 * This is useful to support complex platforms (like platforms with multiple
1384 * regulators per device), instead of the generic OPP set rate helper.
1386 * This must be called before any OPPs are initialized for the device.
1388 struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
1389 int (*set_opp)(struct dev_pm_set_opp_data *data))
1391 struct opp_table *opp_table;
1395 return ERR_PTR(-EINVAL);
1397 opp_table = dev_pm_opp_get_opp_table(dev);
1399 return ERR_PTR(-ENOMEM);
1401 /* This should be called before OPPs are initialized */
1402 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1407 /* Already have custom set_opp helper */
1408 if (WARN_ON(opp_table->set_opp)) {
1413 opp_table->set_opp = set_opp;
1418 dev_pm_opp_put_opp_table(opp_table);
1420 return ERR_PTR(ret);
1422 EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1425 * dev_pm_opp_register_put_opp_helper() - Releases resources blocked for
1427 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
1429 * Release resources blocked for platform specific set_opp helper.
1431 void dev_pm_opp_register_put_opp_helper(struct opp_table *opp_table)
1433 if (!opp_table->set_opp) {
1434 pr_err("%s: Doesn't have custom set_opp helper set\n",
1439 /* Make sure there are no concurrent readers while updating opp_table */
1440 WARN_ON(!list_empty(&opp_table->opp_list));
1442 opp_table->set_opp = NULL;
1444 dev_pm_opp_put_opp_table(opp_table);
1446 EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper);
1449 * dev_pm_opp_add() - Add an OPP table from a table definitions
1450 * @dev: device for which we do this operation
1451 * @freq: Frequency in Hz for this OPP
1452 * @u_volt: Voltage in uVolts for this OPP
1454 * This function adds an opp definition to the opp table and returns status.
1455 * The opp is made available by default and it can be controlled using
1456 * dev_pm_opp_enable/disable functions.
1460 * Duplicate OPPs (both freq and volt are same) and opp->available
1461 * -EEXIST Freq are same and volt are different OR
1462 * Duplicate OPPs (both freq and volt are same) and !opp->available
1463 * -ENOMEM Memory allocation failure
1465 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1467 struct opp_table *opp_table;
1470 opp_table = dev_pm_opp_get_opp_table(dev);
1474 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
1476 dev_pm_opp_put_opp_table(opp_table);
1479 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1482 * _opp_set_availability() - helper to set the availability of an opp
1483 * @dev: device for which we do this operation
1484 * @freq: OPP frequency to modify availability
1485 * @availability_req: availability status requested for this opp
1487 * Set the availability of an OPP, opp_{enable,disable} share a common logic
1488 * which is isolated here.
1490 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1491 * copy operation, returns 0 if no modification was done OR modification was
1494 static int _opp_set_availability(struct device *dev, unsigned long freq,
1495 bool availability_req)
1497 struct opp_table *opp_table;
1498 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
1501 /* Find the opp_table */
1502 opp_table = _find_opp_table(dev);
1503 if (IS_ERR(opp_table)) {
1504 r = PTR_ERR(opp_table);
1505 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1509 mutex_lock(&opp_table->lock);
1511 /* Do we have the frequency? */
1512 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
1513 if (tmp_opp->rate == freq) {
1524 /* Is update really needed? */
1525 if (opp->available == availability_req)
1528 opp->available = availability_req;
1530 /* Notify the change of the OPP availability */
1531 if (availability_req)
1532 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
1535 blocking_notifier_call_chain(&opp_table->head,
1536 OPP_EVENT_DISABLE, opp);
1539 mutex_unlock(&opp_table->lock);
1540 dev_pm_opp_put_opp_table(opp_table);
1545 * dev_pm_opp_enable() - Enable a specific OPP
1546 * @dev: device for which we do this operation
1547 * @freq: OPP frequency to enable
1549 * Enables a provided opp. If the operation is valid, this returns 0, else the
1550 * corresponding error value. It is meant to be used for users an OPP available
1551 * after being temporarily made unavailable with dev_pm_opp_disable.
1553 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1554 * copy operation, returns 0 if no modification was done OR modification was
1557 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
1559 return _opp_set_availability(dev, freq, true);
1561 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
1564 * dev_pm_opp_disable() - Disable a specific OPP
1565 * @dev: device for which we do this operation
1566 * @freq: OPP frequency to disable
1568 * Disables a provided opp. If the operation is valid, this returns
1569 * 0, else the corresponding error value. It is meant to be a temporary
1570 * control by users to make this OPP not available until the circumstances are
1571 * right to make it available again (with a call to dev_pm_opp_enable).
1573 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1574 * copy operation, returns 0 if no modification was done OR modification was
1577 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
1579 return _opp_set_availability(dev, freq, false);
1581 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
1584 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
1585 * @dev: Device for which notifier needs to be registered
1586 * @nb: Notifier block to be registered
1588 * Return: 0 on success or a negative error value.
1590 int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
1592 struct opp_table *opp_table;
1595 opp_table = _find_opp_table(dev);
1596 if (IS_ERR(opp_table))
1597 return PTR_ERR(opp_table);
1599 ret = blocking_notifier_chain_register(&opp_table->head, nb);
1601 dev_pm_opp_put_opp_table(opp_table);
1605 EXPORT_SYMBOL(dev_pm_opp_register_notifier);
1608 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
1609 * @dev: Device for which notifier needs to be unregistered
1610 * @nb: Notifier block to be unregistered
1612 * Return: 0 on success or a negative error value.
1614 int dev_pm_opp_unregister_notifier(struct device *dev,
1615 struct notifier_block *nb)
1617 struct opp_table *opp_table;
1620 opp_table = _find_opp_table(dev);
1621 if (IS_ERR(opp_table))
1622 return PTR_ERR(opp_table);
1624 ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
1626 dev_pm_opp_put_opp_table(opp_table);
1630 EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
1633 * Free OPPs either created using static entries present in DT or even the
1634 * dynamically added entries based on remove_all param.
1636 void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
1639 struct dev_pm_opp *opp, *tmp;
1641 /* Find if opp_table manages a single device */
1642 if (list_is_singular(&opp_table->dev_list)) {
1643 /* Free static OPPs */
1644 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1645 if (remove_all || !opp->dynamic)
1646 dev_pm_opp_put(opp);
1649 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
1653 void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)
1655 struct opp_table *opp_table;
1657 /* Check for existing table for 'dev' */
1658 opp_table = _find_opp_table(dev);
1659 if (IS_ERR(opp_table)) {
1660 int error = PTR_ERR(opp_table);
1662 if (error != -ENODEV)
1663 WARN(1, "%s: opp_table: %d\n",
1664 IS_ERR_OR_NULL(dev) ?
1665 "Invalid device" : dev_name(dev),
1670 _dev_pm_opp_remove_table(opp_table, dev, remove_all);
1672 dev_pm_opp_put_opp_table(opp_table);
1676 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
1677 * @dev: device pointer used to lookup OPP table.
1679 * Free both OPPs created using static entries present in DT and the
1680 * dynamically added entries.
1682 void dev_pm_opp_remove_table(struct device *dev)
1684 _dev_pm_opp_find_and_remove_table(dev, true);
1686 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);