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 #define opp_rcu_lockdep_assert() \
37 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
38 !lockdep_is_held(&opp_table_lock), \
39 "Missing rcu_read_lock() or " \
40 "opp_table_lock protection"); \
43 static struct opp_device *_find_opp_dev(const struct device *dev,
44 struct opp_table *opp_table)
46 struct opp_device *opp_dev;
48 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
49 if (opp_dev->dev == dev)
56 * _find_opp_table() - find opp_table struct using device pointer
57 * @dev: device pointer used to lookup OPP table
59 * Search OPP table for one containing matching device. Does a RCU reader
60 * operation to grab the pointer needed.
62 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
63 * -EINVAL based on type of error.
65 * Locking: For readers, this function must be called under rcu_read_lock().
66 * opp_table is a RCU protected pointer, which means that opp_table is valid
67 * as long as we are under RCU lock.
69 * For Writers, this function must be called with opp_table_lock held.
71 struct opp_table *_find_opp_table(struct device *dev)
73 struct opp_table *opp_table;
75 opp_rcu_lockdep_assert();
77 if (IS_ERR_OR_NULL(dev)) {
78 pr_err("%s: Invalid parameters\n", __func__);
79 return ERR_PTR(-EINVAL);
82 list_for_each_entry_rcu(opp_table, &opp_tables, node)
83 if (_find_opp_dev(dev, opp_table))
86 return ERR_PTR(-ENODEV);
90 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
91 * @opp: opp for which voltage has to be returned for
93 * Return: voltage in micro volt corresponding to the opp, else
96 * This is useful only for devices with single power supply.
98 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
99 * protected pointer. This means that opp which could have been fetched by
100 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
101 * under RCU lock. The pointer returned by the opp_find_freq family must be
102 * used in the same section as the usage of this function with the pointer
103 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
106 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
108 struct dev_pm_opp *tmp_opp;
111 opp_rcu_lockdep_assert();
113 tmp_opp = rcu_dereference(opp);
114 if (IS_ERR_OR_NULL(tmp_opp))
115 pr_err("%s: Invalid parameters\n", __func__);
117 v = tmp_opp->supplies[0].u_volt;
121 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
124 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
125 * @opp: opp for which frequency has to be returned for
127 * Return: frequency in hertz corresponding to the opp, else
130 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
131 * protected pointer. This means that opp which could have been fetched by
132 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
133 * under RCU lock. The pointer returned by the opp_find_freq family must be
134 * used in the same section as the usage of this function with the pointer
135 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
138 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
140 struct dev_pm_opp *tmp_opp;
143 opp_rcu_lockdep_assert();
145 tmp_opp = rcu_dereference(opp);
146 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available)
147 pr_err("%s: Invalid parameters\n", __func__);
153 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
156 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
157 * @opp: opp for which turbo mode is being verified
159 * Turbo OPPs are not for normal use, and can be enabled (under certain
160 * conditions) for short duration of times to finish high throughput work
161 * quickly. Running on them for longer times may overheat the chip.
163 * Return: true if opp is turbo opp, else false.
165 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
166 * protected pointer. This means that opp which could have been fetched by
167 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
168 * under RCU lock. The pointer returned by the opp_find_freq family must be
169 * used in the same section as the usage of this function with the pointer
170 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
173 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
175 struct dev_pm_opp *tmp_opp;
177 opp_rcu_lockdep_assert();
179 tmp_opp = rcu_dereference(opp);
180 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available) {
181 pr_err("%s: Invalid parameters\n", __func__);
185 return tmp_opp->turbo;
187 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
190 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
191 * @dev: device for which we do this operation
193 * Return: This function returns the max clock latency in nanoseconds.
195 * Locking: This function takes rcu_read_lock().
197 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
199 struct opp_table *opp_table;
200 unsigned long clock_latency_ns;
204 opp_table = _find_opp_table(dev);
205 if (IS_ERR(opp_table))
206 clock_latency_ns = 0;
208 clock_latency_ns = opp_table->clock_latency_ns_max;
211 return clock_latency_ns;
213 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
215 static int _get_regulator_count(struct device *dev)
217 struct opp_table *opp_table;
222 opp_table = _find_opp_table(dev);
223 if (!IS_ERR(opp_table))
224 count = opp_table->regulator_count;
234 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
235 * @dev: device for which we do this operation
237 * Return: This function returns the max voltage latency in nanoseconds.
239 * Locking: This function takes rcu_read_lock().
241 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
243 struct opp_table *opp_table;
244 struct dev_pm_opp *opp;
245 struct regulator *reg, **regulators;
246 unsigned long latency_ns = 0;
253 count = _get_regulator_count(dev);
255 /* Regulator may not be required for the device */
259 regulators = kmalloc_array(count, sizeof(*regulators), GFP_KERNEL);
263 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
265 goto free_regulators;
269 opp_table = _find_opp_table(dev);
270 if (IS_ERR(opp_table)) {
275 memcpy(regulators, opp_table->regulators, count * sizeof(*regulators));
277 for (i = 0; i < count; i++) {
281 list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
285 if (opp->supplies[i].u_volt_min < uV[i].min)
286 uV[i].min = opp->supplies[i].u_volt_min;
287 if (opp->supplies[i].u_volt_max > uV[i].max)
288 uV[i].max = opp->supplies[i].u_volt_max;
295 * The caller needs to ensure that opp_table (and hence the regulator)
296 * isn't freed, while we are executing this routine.
298 for (i = 0; reg = regulators[i], i < count; i++) {
299 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
301 latency_ns += ret * 1000;
311 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
314 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
316 * @dev: device for which we do this operation
318 * Return: This function returns the max transition latency, in nanoseconds, to
319 * switch from one OPP to other.
321 * Locking: This function takes rcu_read_lock().
323 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
325 return dev_pm_opp_get_max_volt_latency(dev) +
326 dev_pm_opp_get_max_clock_latency(dev);
328 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
331 * dev_pm_opp_get_suspend_opp() - Get suspend opp
332 * @dev: device for which we do this operation
334 * Return: This function returns pointer to the suspend opp if it is
335 * defined and available, otherwise it returns NULL.
337 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
338 * protected pointer. The reason for the same is that the opp pointer which is
339 * returned will remain valid for use with opp_get_{voltage, freq} only while
340 * under the locked area. The pointer returned must be used prior to unlocking
341 * with rcu_read_unlock() to maintain the integrity of the pointer.
343 struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev)
345 struct opp_table *opp_table;
347 opp_rcu_lockdep_assert();
349 opp_table = _find_opp_table(dev);
350 if (IS_ERR(opp_table) || !opp_table->suspend_opp ||
351 !opp_table->suspend_opp->available)
354 return opp_table->suspend_opp;
356 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp);
359 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
360 * @dev: device for which we do this operation
362 * Return: This function returns the number of available opps if there are any,
363 * else returns 0 if none or the corresponding error value.
365 * Locking: This function takes rcu_read_lock().
367 int dev_pm_opp_get_opp_count(struct device *dev)
369 struct opp_table *opp_table;
370 struct dev_pm_opp *temp_opp;
375 opp_table = _find_opp_table(dev);
376 if (IS_ERR(opp_table)) {
377 count = PTR_ERR(opp_table);
378 dev_err(dev, "%s: OPP table not found (%d)\n",
383 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
384 if (temp_opp->available)
392 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
395 * dev_pm_opp_find_freq_exact() - search for an exact frequency
396 * @dev: device for which we do this operation
397 * @freq: frequency to search for
398 * @available: true/false - match for available opp
400 * Return: Searches for exact match in the opp table and returns pointer to the
401 * matching opp if found, else returns ERR_PTR in case of error and should
402 * be handled using IS_ERR. Error return values can be:
403 * EINVAL: for bad pointer
404 * ERANGE: no match found for search
405 * ENODEV: if device not found in list of registered devices
407 * Note: available is a modifier for the search. if available=true, then the
408 * match is for exact matching frequency and is available in the stored OPP
409 * table. if false, the match is for exact frequency which is not available.
411 * This provides a mechanism to enable an opp which is not available currently
412 * or the opposite as well.
414 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
415 * protected pointer. The reason for the same is that the opp pointer which is
416 * returned will remain valid for use with opp_get_{voltage, freq} only while
417 * under the locked area. The pointer returned must be used prior to unlocking
418 * with rcu_read_unlock() to maintain the integrity of the pointer.
420 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
424 struct opp_table *opp_table;
425 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
427 opp_rcu_lockdep_assert();
429 opp_table = _find_opp_table(dev);
430 if (IS_ERR(opp_table)) {
431 int r = PTR_ERR(opp_table);
433 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
437 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
438 if (temp_opp->available == available &&
439 temp_opp->rate == freq) {
447 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
449 static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
452 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
454 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
455 if (temp_opp->available && temp_opp->rate >= *freq) {
466 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
467 * @dev: device for which we do this operation
468 * @freq: Start frequency
470 * Search for the matching ceil *available* OPP from a starting freq
473 * Return: matching *opp and refreshes *freq accordingly, else returns
474 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
476 * EINVAL: for bad pointer
477 * ERANGE: no match found for search
478 * ENODEV: if device not found in list of registered devices
480 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
481 * protected pointer. The reason for the same is that the opp pointer which is
482 * returned will remain valid for use with opp_get_{voltage, freq} only while
483 * under the locked area. The pointer returned must be used prior to unlocking
484 * with rcu_read_unlock() to maintain the integrity of the pointer.
486 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
489 struct opp_table *opp_table;
491 opp_rcu_lockdep_assert();
494 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
495 return ERR_PTR(-EINVAL);
498 opp_table = _find_opp_table(dev);
499 if (IS_ERR(opp_table))
500 return ERR_CAST(opp_table);
502 return _find_freq_ceil(opp_table, freq);
504 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
507 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
508 * @dev: device for which we do this operation
509 * @freq: Start frequency
511 * Search for the matching floor *available* OPP from a starting freq
514 * Return: matching *opp and refreshes *freq accordingly, else returns
515 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
517 * EINVAL: for bad pointer
518 * ERANGE: no match found for search
519 * ENODEV: if device not found in list of registered devices
521 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
522 * protected pointer. The reason for the same is that the opp pointer which is
523 * returned will remain valid for use with opp_get_{voltage, freq} only while
524 * under the locked area. The pointer returned must be used prior to unlocking
525 * with rcu_read_unlock() to maintain the integrity of the pointer.
527 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
530 struct opp_table *opp_table;
531 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
533 opp_rcu_lockdep_assert();
536 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
537 return ERR_PTR(-EINVAL);
540 opp_table = _find_opp_table(dev);
541 if (IS_ERR(opp_table))
542 return ERR_CAST(opp_table);
544 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
545 if (temp_opp->available) {
546 /* go to the next node, before choosing prev */
547 if (temp_opp->rate > *freq)
558 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
561 * The caller needs to ensure that opp_table (and hence the clk) isn't freed,
562 * while clk returned here is used.
564 static struct clk *_get_opp_clk(struct device *dev)
566 struct opp_table *opp_table;
571 opp_table = _find_opp_table(dev);
572 if (IS_ERR(opp_table)) {
573 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
574 clk = ERR_CAST(opp_table);
578 clk = opp_table->clk;
580 dev_err(dev, "%s: No clock available for the device\n",
588 static int _set_opp_voltage(struct device *dev, struct regulator *reg,
589 struct dev_pm_opp_supply *supply)
593 /* Regulator not available for device */
595 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
600 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
601 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
603 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
604 supply->u_volt, supply->u_volt_max);
606 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
607 __func__, supply->u_volt_min, supply->u_volt,
608 supply->u_volt_max, ret);
614 _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
615 unsigned long old_freq, unsigned long freq)
619 ret = clk_set_rate(clk, freq);
621 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
628 static int _generic_set_opp(struct dev_pm_set_opp_data *data)
630 struct dev_pm_opp_supply *old_supply = data->old_opp.supplies;
631 struct dev_pm_opp_supply *new_supply = data->new_opp.supplies;
632 unsigned long old_freq = data->old_opp.rate, freq = data->new_opp.rate;
633 struct regulator *reg = data->regulators[0];
634 struct device *dev= data->dev;
637 /* This function only supports single regulator per device */
638 if (WARN_ON(data->regulator_count > 1)) {
639 dev_err(dev, "multiple regulators are not supported\n");
643 /* Scaling up? Scale voltage before frequency */
644 if (freq > old_freq) {
645 ret = _set_opp_voltage(dev, reg, new_supply);
647 goto restore_voltage;
650 /* Change frequency */
651 ret = _generic_set_opp_clk_only(dev, data->clk, old_freq, freq);
653 goto restore_voltage;
655 /* Scaling down? Scale voltage after frequency */
656 if (freq < old_freq) {
657 ret = _set_opp_voltage(dev, reg, new_supply);
665 if (_generic_set_opp_clk_only(dev, data->clk, freq, old_freq))
666 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
669 /* This shouldn't harm even if the voltages weren't updated earlier */
670 if (old_supply->u_volt)
671 _set_opp_voltage(dev, reg, old_supply);
677 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
678 * @dev: device for which we do this operation
679 * @target_freq: frequency to achieve
681 * This configures the power-supplies and clock source to the levels specified
682 * by the OPP corresponding to the target_freq.
684 * Locking: This function takes rcu_read_lock().
686 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
688 struct opp_table *opp_table;
689 unsigned long freq, old_freq;
690 int (*set_opp)(struct dev_pm_set_opp_data *data);
691 struct dev_pm_opp *old_opp, *opp;
692 struct regulator **regulators;
693 struct dev_pm_set_opp_data *data;
697 if (unlikely(!target_freq)) {
698 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
703 clk = _get_opp_clk(dev);
707 freq = clk_round_rate(clk, target_freq);
711 old_freq = clk_get_rate(clk);
713 /* Return early if nothing to do */
714 if (old_freq == freq) {
715 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
722 opp_table = _find_opp_table(dev);
723 if (IS_ERR(opp_table)) {
724 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
726 return PTR_ERR(opp_table);
729 old_opp = _find_freq_ceil(opp_table, &old_freq);
730 if (IS_ERR(old_opp)) {
731 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
732 __func__, old_freq, PTR_ERR(old_opp));
735 opp = _find_freq_ceil(opp_table, &freq);
738 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
739 __func__, freq, ret);
744 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
747 regulators = opp_table->regulators;
749 /* Only frequency scaling */
752 return _generic_set_opp_clk_only(dev, clk, old_freq, freq);
755 if (opp_table->set_opp)
756 set_opp = opp_table->set_opp;
758 set_opp = _generic_set_opp;
760 data = opp_table->set_opp_data;
761 data->regulators = regulators;
762 data->regulator_count = opp_table->regulator_count;
766 data->old_opp.rate = old_freq;
767 size = sizeof(*opp->supplies) * opp_table->regulator_count;
769 memset(data->old_opp.supplies, 0, size);
771 memcpy(data->old_opp.supplies, old_opp->supplies, size);
773 data->new_opp.rate = freq;
774 memcpy(data->new_opp.supplies, opp->supplies, size);
778 return set_opp(data);
780 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
782 /* OPP-dev Helpers */
783 static void _kfree_opp_dev_rcu(struct rcu_head *head)
785 struct opp_device *opp_dev;
787 opp_dev = container_of(head, struct opp_device, rcu_head);
788 kfree_rcu(opp_dev, rcu_head);
791 static void _remove_opp_dev(struct opp_device *opp_dev,
792 struct opp_table *opp_table)
794 opp_debug_unregister(opp_dev, opp_table);
795 list_del(&opp_dev->node);
796 call_srcu(&opp_table->srcu_head.srcu, &opp_dev->rcu_head,
800 struct opp_device *_add_opp_dev(const struct device *dev,
801 struct opp_table *opp_table)
803 struct opp_device *opp_dev;
806 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
810 /* Initialize opp-dev */
812 list_add_rcu(&opp_dev->node, &opp_table->dev_list);
814 /* Create debugfs entries for the opp_table */
815 ret = opp_debug_register(opp_dev, opp_table);
817 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
824 * _add_opp_table() - Find OPP table or allocate a new one
825 * @dev: device for which we do this operation
827 * It tries to find an existing table first, if it couldn't find one, it
828 * allocates a new OPP table and returns that.
830 * Return: valid opp_table pointer if success, else NULL.
832 static struct opp_table *_add_opp_table(struct device *dev)
834 struct opp_table *opp_table;
835 struct opp_device *opp_dev;
838 /* Check for existing table for 'dev' first */
839 opp_table = _find_opp_table(dev);
840 if (!IS_ERR(opp_table))
844 * Allocate a new OPP table. In the infrequent case where a new
845 * device is needed to be added, we pay this penalty.
847 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
851 INIT_LIST_HEAD(&opp_table->dev_list);
853 opp_dev = _add_opp_dev(dev, opp_table);
859 _of_init_opp_table(opp_table, dev);
861 /* Find clk for the device */
862 opp_table->clk = clk_get(dev, NULL);
863 if (IS_ERR(opp_table->clk)) {
864 ret = PTR_ERR(opp_table->clk);
865 if (ret != -EPROBE_DEFER)
866 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
870 srcu_init_notifier_head(&opp_table->srcu_head);
871 INIT_LIST_HEAD(&opp_table->opp_list);
873 /* Secure the device table modification */
874 list_add_rcu(&opp_table->node, &opp_tables);
879 * _kfree_device_rcu() - Free opp_table RCU handler
882 static void _kfree_device_rcu(struct rcu_head *head)
884 struct opp_table *opp_table = container_of(head, struct opp_table,
887 kfree_rcu(opp_table, rcu_head);
891 * _remove_opp_table() - Removes a OPP table
892 * @opp_table: OPP table to be removed.
894 * Removes/frees OPP table if it doesn't contain any OPPs.
896 static void _remove_opp_table(struct opp_table *opp_table)
898 struct opp_device *opp_dev;
900 if (!list_empty(&opp_table->opp_list))
903 if (opp_table->supported_hw)
906 if (opp_table->prop_name)
909 if (opp_table->regulators)
912 if (opp_table->set_opp)
916 if (!IS_ERR(opp_table->clk))
917 clk_put(opp_table->clk);
919 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
922 _remove_opp_dev(opp_dev, opp_table);
924 /* dev_list must be empty now */
925 WARN_ON(!list_empty(&opp_table->dev_list));
927 list_del_rcu(&opp_table->node);
928 call_srcu(&opp_table->srcu_head.srcu, &opp_table->rcu_head,
933 * _kfree_opp_rcu() - Free OPP RCU handler
936 static void _kfree_opp_rcu(struct rcu_head *head)
938 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
940 kfree_rcu(opp, rcu_head);
944 * _opp_remove() - Remove an OPP from a table definition
945 * @opp_table: points back to the opp_table struct this opp belongs to
946 * @opp: pointer to the OPP to remove
947 * @notify: OPP_EVENT_REMOVE notification should be sent or not
949 * This function removes an opp definition from the opp table.
951 * Locking: The internal opp_table and opp structures are RCU protected.
952 * It is assumed that the caller holds required mutex for an RCU updater
955 void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp,
959 * Notify the changes in the availability of the operable
960 * frequency/voltage list.
963 srcu_notifier_call_chain(&opp_table->srcu_head,
964 OPP_EVENT_REMOVE, opp);
965 opp_debug_remove_one(opp);
966 list_del_rcu(&opp->node);
967 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
969 _remove_opp_table(opp_table);
973 * dev_pm_opp_remove() - Remove an OPP from OPP table
974 * @dev: device for which we do this operation
975 * @freq: OPP to remove with matching 'freq'
977 * This function removes an opp from the opp table.
979 * Locking: The internal opp_table and opp structures are RCU protected.
980 * Hence this function internally uses RCU updater strategy with mutex locks
981 * to keep the integrity of the internal data structures. Callers should ensure
982 * that this function is *NOT* called under RCU protection or in contexts where
983 * mutex cannot be locked.
985 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
987 struct dev_pm_opp *opp;
988 struct opp_table *opp_table;
991 /* Hold our table modification lock here */
992 mutex_lock(&opp_table_lock);
994 opp_table = _find_opp_table(dev);
995 if (IS_ERR(opp_table))
998 list_for_each_entry(opp, &opp_table->opp_list, node) {
999 if (opp->rate == freq) {
1006 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1011 _opp_remove(opp_table, opp, true);
1013 mutex_unlock(&opp_table_lock);
1015 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1017 struct dev_pm_opp *_allocate_opp(struct device *dev,
1018 struct opp_table **opp_table)
1020 struct dev_pm_opp *opp;
1021 int count, supply_size;
1022 struct opp_table *table;
1024 table = _add_opp_table(dev);
1028 /* Allocate space for at least one supply */
1029 count = table->regulator_count ? table->regulator_count : 1;
1030 supply_size = sizeof(*opp->supplies) * count;
1032 /* allocate new OPP node and supplies structures */
1033 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
1039 /* Put the supplies at the end of the OPP structure as an empty array */
1040 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1041 INIT_LIST_HEAD(&opp->node);
1048 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
1049 struct opp_table *opp_table)
1051 struct regulator *reg;
1054 for (i = 0; i < opp_table->regulator_count; i++) {
1055 reg = opp_table->regulators[i];
1057 if (!regulator_is_supported_voltage(reg,
1058 opp->supplies[i].u_volt_min,
1059 opp->supplies[i].u_volt_max)) {
1060 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1061 __func__, opp->supplies[i].u_volt_min,
1062 opp->supplies[i].u_volt_max);
1072 * 0: On success. And appropriate error message for duplicate OPPs.
1073 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1074 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1075 * sure we don't print error messages unnecessarily if different parts of
1076 * kernel try to initialize the OPP table.
1077 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1078 * should be considered an error by the callers of _opp_add().
1080 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1081 struct opp_table *opp_table)
1083 struct dev_pm_opp *opp;
1084 struct list_head *head = &opp_table->opp_list;
1088 * Insert new OPP in order of increasing frequency and discard if
1091 * Need to use &opp_table->opp_list in the condition part of the 'for'
1092 * loop, don't replace it with head otherwise it will become an infinite
1095 list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
1096 if (new_opp->rate > opp->rate) {
1101 if (new_opp->rate < opp->rate)
1104 /* Duplicate OPPs */
1105 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1106 __func__, opp->rate, opp->supplies[0].u_volt,
1107 opp->available, new_opp->rate,
1108 new_opp->supplies[0].u_volt, new_opp->available);
1110 /* Should we compare voltages for all regulators here ? */
1111 return opp->available &&
1112 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1115 new_opp->opp_table = opp_table;
1116 list_add_rcu(&new_opp->node, head);
1118 ret = opp_debug_create_one(new_opp, opp_table);
1120 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1123 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
1124 new_opp->available = false;
1125 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1126 __func__, new_opp->rate);
1133 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
1134 * @dev: device for which we do this operation
1135 * @freq: Frequency in Hz for this OPP
1136 * @u_volt: Voltage in uVolts for this OPP
1137 * @dynamic: Dynamically added OPPs.
1139 * This function adds an opp definition to the opp table and returns status.
1140 * The opp is made available by default and it can be controlled using
1141 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1143 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1144 * and freed by dev_pm_opp_of_remove_table.
1146 * Locking: The internal opp_table and opp structures are RCU protected.
1147 * Hence this function internally uses RCU updater strategy with mutex locks
1148 * to keep the integrity of the internal data structures. Callers should ensure
1149 * that this function is *NOT* called under RCU protection or in contexts where
1150 * mutex cannot be locked.
1154 * Duplicate OPPs (both freq and volt are same) and opp->available
1155 * -EEXIST Freq are same and volt are different OR
1156 * Duplicate OPPs (both freq and volt are same) and !opp->available
1157 * -ENOMEM Memory allocation failure
1159 int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
1162 struct opp_table *opp_table;
1163 struct dev_pm_opp *new_opp;
1167 /* Hold our table modification lock here */
1168 mutex_lock(&opp_table_lock);
1170 new_opp = _allocate_opp(dev, &opp_table);
1176 /* populate the opp table */
1177 new_opp->rate = freq;
1178 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
1179 new_opp->supplies[0].u_volt = u_volt;
1180 new_opp->supplies[0].u_volt_min = u_volt - tol;
1181 new_opp->supplies[0].u_volt_max = u_volt + tol;
1182 new_opp->available = true;
1183 new_opp->dynamic = dynamic;
1185 ret = _opp_add(dev, new_opp, opp_table);
1187 /* Don't return error for duplicate OPPs */
1193 mutex_unlock(&opp_table_lock);
1196 * Notify the changes in the availability of the operable
1197 * frequency/voltage list.
1199 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);
1203 _opp_remove(opp_table, new_opp, false);
1205 mutex_unlock(&opp_table_lock);
1210 * dev_pm_opp_set_supported_hw() - Set supported platforms
1211 * @dev: Device for which supported-hw has to be set.
1212 * @versions: Array of hierarchy of versions to match.
1213 * @count: Number of elements in the array.
1215 * This is required only for the V2 bindings, and it enables a platform to
1216 * specify the hierarchy of versions it supports. OPP layer will then enable
1217 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1220 * Locking: The internal opp_table and opp structures are RCU protected.
1221 * Hence this function internally uses RCU updater strategy with mutex locks
1222 * to keep the integrity of the internal data structures. Callers should ensure
1223 * that this function is *NOT* called under RCU protection or in contexts where
1224 * mutex cannot be locked.
1226 int dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
1229 struct opp_table *opp_table;
1232 /* Hold our table modification lock here */
1233 mutex_lock(&opp_table_lock);
1235 opp_table = _add_opp_table(dev);
1241 /* Make sure there are no concurrent readers while updating opp_table */
1242 WARN_ON(!list_empty(&opp_table->opp_list));
1244 /* Do we already have a version hierarchy associated with opp_table? */
1245 if (opp_table->supported_hw) {
1246 dev_err(dev, "%s: Already have supported hardware list\n",
1252 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
1254 if (!opp_table->supported_hw) {
1259 opp_table->supported_hw_count = count;
1260 mutex_unlock(&opp_table_lock);
1264 _remove_opp_table(opp_table);
1266 mutex_unlock(&opp_table_lock);
1270 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1273 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
1274 * @dev: Device for which supported-hw has to be put.
1276 * This is required only for the V2 bindings, and is called for a matching
1277 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
1278 * will not be freed.
1280 * Locking: The internal opp_table and opp structures are RCU protected.
1281 * Hence this function internally uses RCU updater strategy with mutex locks
1282 * to keep the integrity of the internal data structures. Callers should ensure
1283 * that this function is *NOT* called under RCU protection or in contexts where
1284 * mutex cannot be locked.
1286 void dev_pm_opp_put_supported_hw(struct device *dev)
1288 struct opp_table *opp_table;
1290 /* Hold our table modification lock here */
1291 mutex_lock(&opp_table_lock);
1293 /* Check for existing table for 'dev' first */
1294 opp_table = _find_opp_table(dev);
1295 if (IS_ERR(opp_table)) {
1296 dev_err(dev, "Failed to find opp_table: %ld\n",
1297 PTR_ERR(opp_table));
1301 /* Make sure there are no concurrent readers while updating opp_table */
1302 WARN_ON(!list_empty(&opp_table->opp_list));
1304 if (!opp_table->supported_hw) {
1305 dev_err(dev, "%s: Doesn't have supported hardware list\n",
1310 kfree(opp_table->supported_hw);
1311 opp_table->supported_hw = NULL;
1312 opp_table->supported_hw_count = 0;
1314 /* Try freeing opp_table if this was the last blocking resource */
1315 _remove_opp_table(opp_table);
1318 mutex_unlock(&opp_table_lock);
1320 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1323 * dev_pm_opp_set_prop_name() - Set prop-extn name
1324 * @dev: Device for which the prop-name has to be set.
1325 * @name: name to postfix to properties.
1327 * This is required only for the V2 bindings, and it enables a platform to
1328 * specify the extn to be used for certain property names. The properties to
1329 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1330 * should postfix the property name with -<name> while looking for them.
1332 * Locking: The internal opp_table and opp structures are RCU protected.
1333 * Hence this function internally uses RCU updater strategy with mutex locks
1334 * to keep the integrity of the internal data structures. Callers should ensure
1335 * that this function is *NOT* called under RCU protection or in contexts where
1336 * mutex cannot be locked.
1338 int dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1340 struct opp_table *opp_table;
1343 /* Hold our table modification lock here */
1344 mutex_lock(&opp_table_lock);
1346 opp_table = _add_opp_table(dev);
1352 /* Make sure there are no concurrent readers while updating opp_table */
1353 WARN_ON(!list_empty(&opp_table->opp_list));
1355 /* Do we already have a prop-name associated with opp_table? */
1356 if (opp_table->prop_name) {
1357 dev_err(dev, "%s: Already have prop-name %s\n", __func__,
1358 opp_table->prop_name);
1363 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1364 if (!opp_table->prop_name) {
1369 mutex_unlock(&opp_table_lock);
1373 _remove_opp_table(opp_table);
1375 mutex_unlock(&opp_table_lock);
1379 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1382 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
1383 * @dev: Device for which the prop-name has to be put.
1385 * This is required only for the V2 bindings, and is called for a matching
1386 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
1387 * will not be freed.
1389 * Locking: The internal opp_table and opp structures are RCU protected.
1390 * Hence this function internally uses RCU updater strategy with mutex locks
1391 * to keep the integrity of the internal data structures. Callers should ensure
1392 * that this function is *NOT* called under RCU protection or in contexts where
1393 * mutex cannot be locked.
1395 void dev_pm_opp_put_prop_name(struct device *dev)
1397 struct opp_table *opp_table;
1399 /* Hold our table modification lock here */
1400 mutex_lock(&opp_table_lock);
1402 /* Check for existing table for 'dev' first */
1403 opp_table = _find_opp_table(dev);
1404 if (IS_ERR(opp_table)) {
1405 dev_err(dev, "Failed to find opp_table: %ld\n",
1406 PTR_ERR(opp_table));
1410 /* Make sure there are no concurrent readers while updating opp_table */
1411 WARN_ON(!list_empty(&opp_table->opp_list));
1413 if (!opp_table->prop_name) {
1414 dev_err(dev, "%s: Doesn't have a prop-name\n", __func__);
1418 kfree(opp_table->prop_name);
1419 opp_table->prop_name = NULL;
1421 /* Try freeing opp_table if this was the last blocking resource */
1422 _remove_opp_table(opp_table);
1425 mutex_unlock(&opp_table_lock);
1427 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1429 static int _allocate_set_opp_data(struct opp_table *opp_table)
1431 struct dev_pm_set_opp_data *data;
1432 int len, count = opp_table->regulator_count;
1434 if (WARN_ON(!count))
1437 /* space for set_opp_data */
1438 len = sizeof(*data);
1440 /* space for old_opp.supplies and new_opp.supplies */
1441 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1443 data = kzalloc(len, GFP_KERNEL);
1447 data->old_opp.supplies = (void *)(data + 1);
1448 data->new_opp.supplies = data->old_opp.supplies + count;
1450 opp_table->set_opp_data = data;
1455 static void _free_set_opp_data(struct opp_table *opp_table)
1457 kfree(opp_table->set_opp_data);
1458 opp_table->set_opp_data = NULL;
1462 * dev_pm_opp_set_regulators() - Set regulator names for the device
1463 * @dev: Device for which regulator name is being set.
1464 * @names: Array of pointers to the names of the regulator.
1465 * @count: Number of regulators.
1467 * In order to support OPP switching, OPP layer needs to know the name of the
1468 * device's regulators, as the core would be required to switch voltages as
1471 * This must be called before any OPPs are initialized for the device.
1473 * Locking: The internal opp_table and opp structures are RCU protected.
1474 * Hence this function internally uses RCU updater strategy with mutex locks
1475 * to keep the integrity of the internal data structures. Callers should ensure
1476 * that this function is *NOT* called under RCU protection or in contexts where
1477 * mutex cannot be locked.
1479 struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1480 const char * const names[],
1483 struct opp_table *opp_table;
1484 struct regulator *reg;
1487 mutex_lock(&opp_table_lock);
1489 opp_table = _add_opp_table(dev);
1495 /* This should be called before OPPs are initialized */
1496 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1501 /* Already have regulators set */
1502 if (opp_table->regulators) {
1507 opp_table->regulators = kmalloc_array(count,
1508 sizeof(*opp_table->regulators),
1510 if (!opp_table->regulators) {
1515 for (i = 0; i < count; i++) {
1516 reg = regulator_get_optional(dev, names[i]);
1519 if (ret != -EPROBE_DEFER)
1520 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1521 __func__, names[i], ret);
1522 goto free_regulators;
1525 opp_table->regulators[i] = reg;
1528 opp_table->regulator_count = count;
1530 /* Allocate block only once to pass to set_opp() routines */
1531 ret = _allocate_set_opp_data(opp_table);
1533 goto free_regulators;
1535 mutex_unlock(&opp_table_lock);
1540 regulator_put(opp_table->regulators[--i]);
1542 kfree(opp_table->regulators);
1543 opp_table->regulators = NULL;
1544 opp_table->regulator_count = 0;
1546 _remove_opp_table(opp_table);
1548 mutex_unlock(&opp_table_lock);
1550 return ERR_PTR(ret);
1552 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
1555 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1556 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
1558 * Locking: The internal opp_table and opp structures are RCU protected.
1559 * Hence this function internally uses RCU updater strategy with mutex locks
1560 * to keep the integrity of the internal data structures. Callers should ensure
1561 * that this function is *NOT* called under RCU protection or in contexts where
1562 * mutex cannot be locked.
1564 void dev_pm_opp_put_regulators(struct opp_table *opp_table)
1568 mutex_lock(&opp_table_lock);
1570 if (!opp_table->regulators) {
1571 pr_err("%s: Doesn't have regulators set\n", __func__);
1575 /* Make sure there are no concurrent readers while updating opp_table */
1576 WARN_ON(!list_empty(&opp_table->opp_list));
1578 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1579 regulator_put(opp_table->regulators[i]);
1581 _free_set_opp_data(opp_table);
1583 kfree(opp_table->regulators);
1584 opp_table->regulators = NULL;
1585 opp_table->regulator_count = 0;
1587 /* Try freeing opp_table if this was the last blocking resource */
1588 _remove_opp_table(opp_table);
1591 mutex_unlock(&opp_table_lock);
1593 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
1596 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1597 * @dev: Device for which the helper is getting registered.
1598 * @set_opp: Custom set OPP helper.
1600 * This is useful to support complex platforms (like platforms with multiple
1601 * regulators per device), instead of the generic OPP set rate helper.
1603 * This must be called before any OPPs are initialized for the device.
1605 * Locking: The internal opp_table and opp structures are RCU protected.
1606 * Hence this function internally uses RCU updater strategy with mutex locks
1607 * to keep the integrity of the internal data structures. Callers should ensure
1608 * that this function is *NOT* called under RCU protection or in contexts where
1609 * mutex cannot be locked.
1611 int dev_pm_opp_register_set_opp_helper(struct device *dev,
1612 int (*set_opp)(struct dev_pm_set_opp_data *data))
1614 struct opp_table *opp_table;
1620 mutex_lock(&opp_table_lock);
1622 opp_table = _add_opp_table(dev);
1628 /* This should be called before OPPs are initialized */
1629 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1634 /* Already have custom set_opp helper */
1635 if (WARN_ON(opp_table->set_opp)) {
1640 opp_table->set_opp = set_opp;
1642 mutex_unlock(&opp_table_lock);
1646 _remove_opp_table(opp_table);
1648 mutex_unlock(&opp_table_lock);
1652 EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1655 * dev_pm_opp_register_put_opp_helper() - Releases resources blocked for
1657 * @dev: Device for which custom set_opp helper has to be cleared.
1659 * Locking: The internal opp_table and opp structures are RCU protected.
1660 * Hence this function internally uses RCU updater strategy with mutex locks
1661 * to keep the integrity of the internal data structures. Callers should ensure
1662 * that this function is *NOT* called under RCU protection or in contexts where
1663 * mutex cannot be locked.
1665 void dev_pm_opp_register_put_opp_helper(struct device *dev)
1667 struct opp_table *opp_table;
1669 mutex_lock(&opp_table_lock);
1671 /* Check for existing table for 'dev' first */
1672 opp_table = _find_opp_table(dev);
1673 if (IS_ERR(opp_table)) {
1674 dev_err(dev, "Failed to find opp_table: %ld\n",
1675 PTR_ERR(opp_table));
1679 if (!opp_table->set_opp) {
1680 dev_err(dev, "%s: Doesn't have custom set_opp helper set\n",
1685 /* Make sure there are no concurrent readers while updating opp_table */
1686 WARN_ON(!list_empty(&opp_table->opp_list));
1688 opp_table->set_opp = NULL;
1690 /* Try freeing opp_table if this was the last blocking resource */
1691 _remove_opp_table(opp_table);
1694 mutex_unlock(&opp_table_lock);
1696 EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper);
1699 * dev_pm_opp_add() - Add an OPP table from a table definitions
1700 * @dev: device for which we do this operation
1701 * @freq: Frequency in Hz for this OPP
1702 * @u_volt: Voltage in uVolts for this OPP
1704 * This function adds an opp definition to the opp table and returns status.
1705 * The opp is made available by default and it can be controlled using
1706 * dev_pm_opp_enable/disable functions.
1708 * Locking: The internal opp_table and opp structures are RCU protected.
1709 * Hence this function internally uses RCU updater strategy with mutex locks
1710 * to keep the integrity of the internal data structures. Callers should ensure
1711 * that this function is *NOT* called under RCU protection or in contexts where
1712 * mutex cannot be locked.
1716 * Duplicate OPPs (both freq and volt are same) and opp->available
1717 * -EEXIST Freq are same and volt are different OR
1718 * Duplicate OPPs (both freq and volt are same) and !opp->available
1719 * -ENOMEM Memory allocation failure
1721 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1723 return _opp_add_v1(dev, freq, u_volt, true);
1725 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1728 * _opp_set_availability() - helper to set the availability of an opp
1729 * @dev: device for which we do this operation
1730 * @freq: OPP frequency to modify availability
1731 * @availability_req: availability status requested for this opp
1733 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
1734 * share a common logic which is isolated here.
1736 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1737 * copy operation, returns 0 if no modification was done OR modification was
1740 * Locking: The internal opp_table and opp structures are RCU protected.
1741 * Hence this function internally uses RCU updater strategy with mutex locks to
1742 * keep the integrity of the internal data structures. Callers should ensure
1743 * that this function is *NOT* called under RCU protection or in contexts where
1744 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1746 static int _opp_set_availability(struct device *dev, unsigned long freq,
1747 bool availability_req)
1749 struct opp_table *opp_table;
1750 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
1753 /* keep the node allocated */
1754 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
1758 mutex_lock(&opp_table_lock);
1760 /* Find the opp_table */
1761 opp_table = _find_opp_table(dev);
1762 if (IS_ERR(opp_table)) {
1763 r = PTR_ERR(opp_table);
1764 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1768 /* Do we have the frequency? */
1769 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
1770 if (tmp_opp->rate == freq) {
1780 /* Is update really needed? */
1781 if (opp->available == availability_req)
1783 /* copy the old data over */
1786 /* plug in new node */
1787 new_opp->available = availability_req;
1789 list_replace_rcu(&opp->node, &new_opp->node);
1790 mutex_unlock(&opp_table_lock);
1791 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
1793 /* Notify the change of the OPP availability */
1794 if (availability_req)
1795 srcu_notifier_call_chain(&opp_table->srcu_head,
1796 OPP_EVENT_ENABLE, new_opp);
1798 srcu_notifier_call_chain(&opp_table->srcu_head,
1799 OPP_EVENT_DISABLE, new_opp);
1804 mutex_unlock(&opp_table_lock);
1810 * dev_pm_opp_enable() - Enable a specific OPP
1811 * @dev: device for which we do this operation
1812 * @freq: OPP frequency to enable
1814 * Enables a provided opp. If the operation is valid, this returns 0, else the
1815 * corresponding error value. It is meant to be used for users an OPP available
1816 * after being temporarily made unavailable with dev_pm_opp_disable.
1818 * Locking: The internal opp_table and opp structures are RCU protected.
1819 * Hence this function indirectly uses RCU and mutex locks to keep the
1820 * integrity of the internal data structures. Callers should ensure that
1821 * this function is *NOT* called under RCU protection or in contexts where
1822 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1824 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1825 * copy operation, returns 0 if no modification was done OR modification was
1828 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
1830 return _opp_set_availability(dev, freq, true);
1832 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
1835 * dev_pm_opp_disable() - Disable a specific OPP
1836 * @dev: device for which we do this operation
1837 * @freq: OPP frequency to disable
1839 * Disables a provided opp. If the operation is valid, this returns
1840 * 0, else the corresponding error value. It is meant to be a temporary
1841 * control by users to make this OPP not available until the circumstances are
1842 * right to make it available again (with a call to dev_pm_opp_enable).
1844 * Locking: The internal opp_table and opp structures are RCU protected.
1845 * Hence this function indirectly uses RCU and mutex locks to keep the
1846 * integrity of the internal data structures. Callers should ensure that
1847 * this function is *NOT* called under RCU protection or in contexts where
1848 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1850 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1851 * copy operation, returns 0 if no modification was done OR modification was
1854 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
1856 return _opp_set_availability(dev, freq, false);
1858 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
1861 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
1862 * @dev: device pointer used to lookup OPP table.
1864 * Return: pointer to notifier head if found, otherwise -ENODEV or
1865 * -EINVAL based on type of error casted as pointer. value must be checked
1866 * with IS_ERR to determine valid pointer or error result.
1868 * Locking: This function must be called under rcu_read_lock(). opp_table is a
1869 * RCU protected pointer. The reason for the same is that the opp pointer which
1870 * is returned will remain valid for use with opp_get_{voltage, freq} only while
1871 * under the locked area. The pointer returned must be used prior to unlocking
1872 * with rcu_read_unlock() to maintain the integrity of the pointer.
1874 struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
1876 struct opp_table *opp_table = _find_opp_table(dev);
1878 if (IS_ERR(opp_table))
1879 return ERR_CAST(opp_table); /* matching type */
1881 return &opp_table->srcu_head;
1883 EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
1886 * Free OPPs either created using static entries present in DT or even the
1887 * dynamically added entries based on remove_all param.
1889 void _dev_pm_opp_remove_table(struct device *dev, bool remove_all)
1891 struct opp_table *opp_table;
1892 struct dev_pm_opp *opp, *tmp;
1894 /* Hold our table modification lock here */
1895 mutex_lock(&opp_table_lock);
1897 /* Check for existing table for 'dev' */
1898 opp_table = _find_opp_table(dev);
1899 if (IS_ERR(opp_table)) {
1900 int error = PTR_ERR(opp_table);
1902 if (error != -ENODEV)
1903 WARN(1, "%s: opp_table: %d\n",
1904 IS_ERR_OR_NULL(dev) ?
1905 "Invalid device" : dev_name(dev),
1910 /* Find if opp_table manages a single device */
1911 if (list_is_singular(&opp_table->dev_list)) {
1912 /* Free static OPPs */
1913 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1914 if (remove_all || !opp->dynamic)
1915 _opp_remove(opp_table, opp, true);
1918 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
1922 mutex_unlock(&opp_table_lock);
1926 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
1927 * @dev: device pointer used to lookup OPP table.
1929 * Free both OPPs created using static entries present in DT and the
1930 * dynamically added entries.
1932 * Locking: The internal opp_table and opp structures are RCU protected.
1933 * Hence this function indirectly uses RCU updater strategy with mutex locks
1934 * to keep the integrity of the internal data structures. Callers should ensure
1935 * that this function is *NOT* called under RCU protection or in contexts where
1936 * mutex cannot be locked.
1938 void dev_pm_opp_remove_table(struct device *dev)
1940 _dev_pm_opp_remove_table(dev, true);
1942 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);