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 void dev_pm_opp_get(struct dev_pm_opp *opp);
45 static struct opp_device *_find_opp_dev(const struct device *dev,
46 struct opp_table *opp_table)
48 struct opp_device *opp_dev;
50 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
51 if (opp_dev->dev == dev)
58 * _find_opp_table() - find opp_table struct using device pointer
59 * @dev: device pointer used to lookup OPP table
61 * Search OPP table for one containing matching device. Does a RCU reader
62 * operation to grab the pointer needed.
64 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
65 * -EINVAL based on type of error.
67 * Locking: For readers, this function must be called under rcu_read_lock().
68 * opp_table is a RCU protected pointer, which means that opp_table is valid
69 * as long as we are under RCU lock.
71 * For Writers, this function must be called with opp_table_lock held.
73 struct opp_table *_find_opp_table(struct device *dev)
75 struct opp_table *opp_table;
77 opp_rcu_lockdep_assert();
79 if (IS_ERR_OR_NULL(dev)) {
80 pr_err("%s: Invalid parameters\n", __func__);
81 return ERR_PTR(-EINVAL);
84 list_for_each_entry_rcu(opp_table, &opp_tables, node)
85 if (_find_opp_dev(dev, opp_table))
88 return ERR_PTR(-ENODEV);
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 struct dev_pm_opp *tmp_opp;
107 tmp_opp = rcu_dereference(opp);
108 if (IS_ERR_OR_NULL(tmp_opp))
109 pr_err("%s: Invalid parameters\n", __func__);
111 v = tmp_opp->supplies[0].u_volt;
116 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
119 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
120 * @opp: opp for which frequency has to be returned for
122 * Return: frequency in hertz corresponding to the opp, else
125 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
127 struct dev_pm_opp *tmp_opp;
132 tmp_opp = rcu_dereference(opp);
133 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available)
134 pr_err("%s: Invalid parameters\n", __func__);
141 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
144 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
145 * @opp: opp for which turbo mode is being verified
147 * Turbo OPPs are not for normal use, and can be enabled (under certain
148 * conditions) for short duration of times to finish high throughput work
149 * quickly. Running on them for longer times may overheat the chip.
151 * Return: true if opp is turbo opp, else false.
153 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
155 struct dev_pm_opp *tmp_opp;
160 tmp_opp = rcu_dereference(opp);
161 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available) {
162 pr_err("%s: Invalid parameters\n", __func__);
166 turbo = tmp_opp->turbo;
171 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
174 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
175 * @dev: device for which we do this operation
177 * Return: This function returns the max clock latency in nanoseconds.
179 * Locking: This function takes rcu_read_lock().
181 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
183 struct opp_table *opp_table;
184 unsigned long clock_latency_ns;
188 opp_table = _find_opp_table(dev);
189 if (IS_ERR(opp_table))
190 clock_latency_ns = 0;
192 clock_latency_ns = opp_table->clock_latency_ns_max;
195 return clock_latency_ns;
197 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
199 static int _get_regulator_count(struct device *dev)
201 struct opp_table *opp_table;
206 opp_table = _find_opp_table(dev);
207 if (!IS_ERR(opp_table))
208 count = opp_table->regulator_count;
218 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
219 * @dev: device for which we do this operation
221 * Return: This function returns the max voltage latency in nanoseconds.
223 * Locking: This function takes rcu_read_lock().
225 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
227 struct opp_table *opp_table;
228 struct dev_pm_opp *opp;
229 struct regulator *reg, **regulators;
230 unsigned long latency_ns = 0;
237 count = _get_regulator_count(dev);
239 /* Regulator may not be required for the device */
243 regulators = kmalloc_array(count, sizeof(*regulators), GFP_KERNEL);
247 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
249 goto free_regulators;
253 opp_table = _find_opp_table(dev);
254 if (IS_ERR(opp_table)) {
259 memcpy(regulators, opp_table->regulators, count * sizeof(*regulators));
261 for (i = 0; i < count; i++) {
265 list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
269 if (opp->supplies[i].u_volt_min < uV[i].min)
270 uV[i].min = opp->supplies[i].u_volt_min;
271 if (opp->supplies[i].u_volt_max > uV[i].max)
272 uV[i].max = opp->supplies[i].u_volt_max;
279 * The caller needs to ensure that opp_table (and hence the regulator)
280 * isn't freed, while we are executing this routine.
282 for (i = 0; reg = regulators[i], i < count; i++) {
283 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
285 latency_ns += ret * 1000;
295 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
298 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
300 * @dev: device for which we do this operation
302 * Return: This function returns the max transition latency, in nanoseconds, to
303 * switch from one OPP to other.
305 * Locking: This function takes rcu_read_lock().
307 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
309 return dev_pm_opp_get_max_volt_latency(dev) +
310 dev_pm_opp_get_max_clock_latency(dev);
312 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
315 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
316 * @dev: device for which we do this operation
318 * Return: This function returns the frequency of the OPP marked as suspend_opp
319 * if one is available, else returns 0;
321 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
323 struct opp_table *opp_table;
324 unsigned long freq = 0;
328 opp_table = _find_opp_table(dev);
329 if (IS_ERR(opp_table) || !opp_table->suspend_opp ||
330 !opp_table->suspend_opp->available)
333 freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
339 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
342 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
343 * @dev: device for which we do this operation
345 * Return: This function returns the number of available opps if there are any,
346 * else returns 0 if none or the corresponding error value.
348 * Locking: This function takes rcu_read_lock().
350 int dev_pm_opp_get_opp_count(struct device *dev)
352 struct opp_table *opp_table;
353 struct dev_pm_opp *temp_opp;
358 opp_table = _find_opp_table(dev);
359 if (IS_ERR(opp_table)) {
360 count = PTR_ERR(opp_table);
361 dev_err(dev, "%s: OPP table not found (%d)\n",
366 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
367 if (temp_opp->available)
375 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
378 * dev_pm_opp_find_freq_exact() - search for an exact frequency
379 * @dev: device for which we do this operation
380 * @freq: frequency to search for
381 * @available: true/false - match for available opp
383 * Return: Searches for exact match in the opp table and returns pointer to the
384 * matching opp if found, else returns ERR_PTR in case of error and should
385 * be handled using IS_ERR. Error return values can be:
386 * EINVAL: for bad pointer
387 * ERANGE: no match found for search
388 * ENODEV: if device not found in list of registered devices
390 * Note: available is a modifier for the search. if available=true, then the
391 * match is for exact matching frequency and is available in the stored OPP
392 * table. if false, the match is for exact frequency which is not available.
394 * This provides a mechanism to enable an opp which is not available currently
395 * or the opposite as well.
397 * The callers are required to call dev_pm_opp_put() for the returned OPP after
400 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
404 struct opp_table *opp_table;
405 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
409 opp_table = _find_opp_table(dev);
410 if (IS_ERR(opp_table)) {
411 int r = PTR_ERR(opp_table);
413 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
418 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
419 if (temp_opp->available == available &&
420 temp_opp->rate == freq) {
423 /* Increment the reference count of OPP */
433 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
435 static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
438 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
440 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
441 if (temp_opp->available && temp_opp->rate >= *freq) {
445 /* Increment the reference count of OPP */
455 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
456 * @dev: device for which we do this operation
457 * @freq: Start frequency
459 * Search for the matching ceil *available* OPP from a starting freq
462 * Return: matching *opp and refreshes *freq accordingly, else returns
463 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
465 * EINVAL: for bad pointer
466 * ERANGE: no match found for search
467 * ENODEV: if device not found in list of registered devices
469 * The callers are required to call dev_pm_opp_put() for the returned OPP after
472 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
475 struct opp_table *opp_table;
476 struct dev_pm_opp *opp;
479 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
480 return ERR_PTR(-EINVAL);
485 opp_table = _find_opp_table(dev);
486 if (IS_ERR(opp_table)) {
488 return ERR_CAST(opp_table);
491 opp = _find_freq_ceil(opp_table, freq);
497 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
500 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
501 * @dev: device for which we do this operation
502 * @freq: Start frequency
504 * Search for the matching floor *available* OPP from a starting freq
507 * Return: matching *opp and refreshes *freq accordingly, else returns
508 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
510 * EINVAL: for bad pointer
511 * ERANGE: no match found for search
512 * ENODEV: if device not found in list of registered devices
514 * The callers are required to call dev_pm_opp_put() for the returned OPP after
517 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
520 struct opp_table *opp_table;
521 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
524 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
525 return ERR_PTR(-EINVAL);
530 opp_table = _find_opp_table(dev);
531 if (IS_ERR(opp_table)) {
533 return ERR_CAST(opp_table);
536 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
537 if (temp_opp->available) {
538 /* go to the next node, before choosing prev */
539 if (temp_opp->rate > *freq)
546 /* Increment the reference count of OPP */
556 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
559 * The caller needs to ensure that opp_table (and hence the clk) isn't freed,
560 * while clk returned here is used.
562 static struct clk *_get_opp_clk(struct device *dev)
564 struct opp_table *opp_table;
569 opp_table = _find_opp_table(dev);
570 if (IS_ERR(opp_table)) {
571 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
572 clk = ERR_CAST(opp_table);
576 clk = opp_table->clk;
578 dev_err(dev, "%s: No clock available for the device\n",
586 static int _set_opp_voltage(struct device *dev, struct regulator *reg,
587 struct dev_pm_opp_supply *supply)
591 /* Regulator not available for device */
593 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
598 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
599 supply->u_volt_min, supply->u_volt, supply->u_volt_max);
601 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
602 supply->u_volt, supply->u_volt_max);
604 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
605 __func__, supply->u_volt_min, supply->u_volt,
606 supply->u_volt_max, ret);
612 _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
613 unsigned long old_freq, unsigned long freq)
617 ret = clk_set_rate(clk, freq);
619 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
626 static int _generic_set_opp(struct dev_pm_set_opp_data *data)
628 struct dev_pm_opp_supply *old_supply = data->old_opp.supplies;
629 struct dev_pm_opp_supply *new_supply = data->new_opp.supplies;
630 unsigned long old_freq = data->old_opp.rate, freq = data->new_opp.rate;
631 struct regulator *reg = data->regulators[0];
632 struct device *dev= data->dev;
635 /* This function only supports single regulator per device */
636 if (WARN_ON(data->regulator_count > 1)) {
637 dev_err(dev, "multiple regulators are not supported\n");
641 /* Scaling up? Scale voltage before frequency */
642 if (freq > old_freq) {
643 ret = _set_opp_voltage(dev, reg, new_supply);
645 goto restore_voltage;
648 /* Change frequency */
649 ret = _generic_set_opp_clk_only(dev, data->clk, old_freq, freq);
651 goto restore_voltage;
653 /* Scaling down? Scale voltage after frequency */
654 if (freq < old_freq) {
655 ret = _set_opp_voltage(dev, reg, new_supply);
663 if (_generic_set_opp_clk_only(dev, data->clk, freq, old_freq))
664 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
667 /* This shouldn't harm even if the voltages weren't updated earlier */
668 if (old_supply->u_volt)
669 _set_opp_voltage(dev, reg, old_supply);
675 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
676 * @dev: device for which we do this operation
677 * @target_freq: frequency to achieve
679 * This configures the power-supplies and clock source to the levels specified
680 * by the OPP corresponding to the target_freq.
682 * Locking: This function takes rcu_read_lock().
684 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
686 struct opp_table *opp_table;
687 unsigned long freq, old_freq;
688 int (*set_opp)(struct dev_pm_set_opp_data *data);
689 struct dev_pm_opp *old_opp, *opp;
690 struct regulator **regulators;
691 struct dev_pm_set_opp_data *data;
695 if (unlikely(!target_freq)) {
696 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
701 clk = _get_opp_clk(dev);
705 freq = clk_round_rate(clk, target_freq);
709 old_freq = clk_get_rate(clk);
711 /* Return early if nothing to do */
712 if (old_freq == freq) {
713 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
720 opp_table = _find_opp_table(dev);
721 if (IS_ERR(opp_table)) {
722 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
724 return PTR_ERR(opp_table);
727 old_opp = _find_freq_ceil(opp_table, &old_freq);
728 if (IS_ERR(old_opp)) {
729 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
730 __func__, old_freq, PTR_ERR(old_opp));
733 opp = _find_freq_ceil(opp_table, &freq);
736 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
737 __func__, freq, ret);
738 if (!IS_ERR(old_opp))
739 dev_pm_opp_put(old_opp);
744 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
747 regulators = opp_table->regulators;
749 /* Only frequency scaling */
752 if (!IS_ERR(old_opp))
753 dev_pm_opp_put(old_opp);
755 return _generic_set_opp_clk_only(dev, clk, old_freq, freq);
758 if (opp_table->set_opp)
759 set_opp = opp_table->set_opp;
761 set_opp = _generic_set_opp;
763 data = opp_table->set_opp_data;
764 data->regulators = regulators;
765 data->regulator_count = opp_table->regulator_count;
769 data->old_opp.rate = old_freq;
770 size = sizeof(*opp->supplies) * opp_table->regulator_count;
772 memset(data->old_opp.supplies, 0, size);
774 memcpy(data->old_opp.supplies, old_opp->supplies, size);
776 data->new_opp.rate = freq;
777 memcpy(data->new_opp.supplies, opp->supplies, size);
780 if (!IS_ERR(old_opp))
781 dev_pm_opp_put(old_opp);
784 return set_opp(data);
786 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
788 /* OPP-dev Helpers */
789 static void _kfree_opp_dev_rcu(struct rcu_head *head)
791 struct opp_device *opp_dev;
793 opp_dev = container_of(head, struct opp_device, rcu_head);
794 kfree_rcu(opp_dev, rcu_head);
797 static void _remove_opp_dev(struct opp_device *opp_dev,
798 struct opp_table *opp_table)
800 opp_debug_unregister(opp_dev, opp_table);
801 list_del(&opp_dev->node);
802 call_srcu(&opp_table->srcu_head.srcu, &opp_dev->rcu_head,
806 struct opp_device *_add_opp_dev(const struct device *dev,
807 struct opp_table *opp_table)
809 struct opp_device *opp_dev;
812 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
816 /* Initialize opp-dev */
818 list_add_rcu(&opp_dev->node, &opp_table->dev_list);
820 /* Create debugfs entries for the opp_table */
821 ret = opp_debug_register(opp_dev, opp_table);
823 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
829 static struct opp_table *_allocate_opp_table(struct device *dev)
831 struct opp_table *opp_table;
832 struct opp_device *opp_dev;
836 * Allocate a new OPP table. In the infrequent case where a new
837 * device is needed to be added, we pay this penalty.
839 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
843 INIT_LIST_HEAD(&opp_table->dev_list);
845 opp_dev = _add_opp_dev(dev, opp_table);
851 _of_init_opp_table(opp_table, dev);
853 /* Find clk for the device */
854 opp_table->clk = clk_get(dev, NULL);
855 if (IS_ERR(opp_table->clk)) {
856 ret = PTR_ERR(opp_table->clk);
857 if (ret != -EPROBE_DEFER)
858 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
862 srcu_init_notifier_head(&opp_table->srcu_head);
863 INIT_LIST_HEAD(&opp_table->opp_list);
864 mutex_init(&opp_table->lock);
865 kref_init(&opp_table->kref);
867 /* Secure the device table modification */
868 list_add_rcu(&opp_table->node, &opp_tables);
873 * _kfree_device_rcu() - Free opp_table RCU handler
876 static void _kfree_device_rcu(struct rcu_head *head)
878 struct opp_table *opp_table = container_of(head, struct opp_table,
881 kfree_rcu(opp_table, rcu_head);
884 void _get_opp_table_kref(struct opp_table *opp_table)
886 kref_get(&opp_table->kref);
889 struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
891 struct opp_table *opp_table;
893 /* Hold our table modification lock here */
894 mutex_lock(&opp_table_lock);
896 opp_table = _find_opp_table(dev);
897 if (!IS_ERR(opp_table)) {
898 _get_opp_table_kref(opp_table);
902 opp_table = _allocate_opp_table(dev);
905 mutex_unlock(&opp_table_lock);
909 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
911 static void _opp_table_kref_release(struct kref *kref)
913 struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
914 struct opp_device *opp_dev;
917 if (!IS_ERR(opp_table->clk))
918 clk_put(opp_table->clk);
920 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
923 _remove_opp_dev(opp_dev, opp_table);
925 /* dev_list must be empty now */
926 WARN_ON(!list_empty(&opp_table->dev_list));
928 mutex_destroy(&opp_table->lock);
929 list_del_rcu(&opp_table->node);
930 call_srcu(&opp_table->srcu_head.srcu, &opp_table->rcu_head,
933 mutex_unlock(&opp_table_lock);
936 void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
938 kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
941 EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
943 void _opp_free(struct dev_pm_opp *opp)
949 * _kfree_opp_rcu() - Free OPP RCU handler
952 static void _kfree_opp_rcu(struct rcu_head *head)
954 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
956 kfree_rcu(opp, rcu_head);
959 static void _opp_kref_release(struct kref *kref)
961 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
962 struct opp_table *opp_table = opp->opp_table;
965 * Notify the changes in the availability of the operable
966 * frequency/voltage list.
968 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_REMOVE, opp);
969 opp_debug_remove_one(opp);
970 list_del_rcu(&opp->node);
971 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
973 mutex_unlock(&opp_table->lock);
974 dev_pm_opp_put_opp_table(opp_table);
977 static void dev_pm_opp_get(struct dev_pm_opp *opp)
979 kref_get(&opp->kref);
982 void dev_pm_opp_put(struct dev_pm_opp *opp)
984 kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
986 EXPORT_SYMBOL_GPL(dev_pm_opp_put);
989 * dev_pm_opp_remove() - Remove an OPP from OPP table
990 * @dev: device for which we do this operation
991 * @freq: OPP to remove with matching 'freq'
993 * This function removes an opp from the opp table.
995 * Locking: The internal opp_table and opp structures are RCU protected.
996 * Hence this function internally uses RCU updater strategy with mutex locks
997 * to keep the integrity of the internal data structures. Callers should ensure
998 * that this function is *NOT* called under RCU protection or in contexts where
999 * mutex cannot be locked.
1001 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
1003 struct dev_pm_opp *opp;
1004 struct opp_table *opp_table;
1007 /* Hold our table modification lock here */
1008 mutex_lock(&opp_table_lock);
1010 opp_table = _find_opp_table(dev);
1011 if (IS_ERR(opp_table))
1014 mutex_lock(&opp_table->lock);
1016 list_for_each_entry(opp, &opp_table->opp_list, node) {
1017 if (opp->rate == freq) {
1023 mutex_unlock(&opp_table->lock);
1026 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
1031 dev_pm_opp_put(opp);
1033 mutex_unlock(&opp_table_lock);
1035 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
1037 struct dev_pm_opp *_opp_allocate(struct opp_table *table)
1039 struct dev_pm_opp *opp;
1040 int count, supply_size;
1042 /* Allocate space for at least one supply */
1043 count = table->regulator_count ? table->regulator_count : 1;
1044 supply_size = sizeof(*opp->supplies) * count;
1046 /* allocate new OPP node and supplies structures */
1047 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
1051 /* Put the supplies at the end of the OPP structure as an empty array */
1052 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
1053 INIT_LIST_HEAD(&opp->node);
1058 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
1059 struct opp_table *opp_table)
1061 struct regulator *reg;
1064 for (i = 0; i < opp_table->regulator_count; i++) {
1065 reg = opp_table->regulators[i];
1067 if (!regulator_is_supported_voltage(reg,
1068 opp->supplies[i].u_volt_min,
1069 opp->supplies[i].u_volt_max)) {
1070 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1071 __func__, opp->supplies[i].u_volt_min,
1072 opp->supplies[i].u_volt_max);
1082 * 0: On success. And appropriate error message for duplicate OPPs.
1083 * -EBUSY: For OPP with same freq/volt and is available. The callers of
1084 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
1085 * sure we don't print error messages unnecessarily if different parts of
1086 * kernel try to initialize the OPP table.
1087 * -EEXIST: For OPP with same freq but different volt or is unavailable. This
1088 * should be considered an error by the callers of _opp_add().
1090 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1091 struct opp_table *opp_table)
1093 struct dev_pm_opp *opp;
1094 struct list_head *head;
1098 * Insert new OPP in order of increasing frequency and discard if
1101 * Need to use &opp_table->opp_list in the condition part of the 'for'
1102 * loop, don't replace it with head otherwise it will become an infinite
1105 mutex_lock(&opp_table->lock);
1106 head = &opp_table->opp_list;
1108 list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
1109 if (new_opp->rate > opp->rate) {
1114 if (new_opp->rate < opp->rate)
1117 /* Duplicate OPPs */
1118 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1119 __func__, opp->rate, opp->supplies[0].u_volt,
1120 opp->available, new_opp->rate,
1121 new_opp->supplies[0].u_volt, new_opp->available);
1123 /* Should we compare voltages for all regulators here ? */
1124 ret = opp->available &&
1125 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
1127 mutex_unlock(&opp_table->lock);
1131 list_add_rcu(&new_opp->node, head);
1132 mutex_unlock(&opp_table->lock);
1134 new_opp->opp_table = opp_table;
1135 kref_init(&new_opp->kref);
1137 /* Get a reference to the OPP table */
1138 _get_opp_table_kref(opp_table);
1140 ret = opp_debug_create_one(new_opp, opp_table);
1142 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1145 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
1146 new_opp->available = false;
1147 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1148 __func__, new_opp->rate);
1155 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
1156 * @opp_table: OPP table
1157 * @dev: device for which we do this operation
1158 * @freq: Frequency in Hz for this OPP
1159 * @u_volt: Voltage in uVolts for this OPP
1160 * @dynamic: Dynamically added OPPs.
1162 * This function adds an opp definition to the opp table and returns status.
1163 * The opp is made available by default and it can be controlled using
1164 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1166 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1167 * and freed by dev_pm_opp_of_remove_table.
1169 * Locking: The internal opp_table and opp structures are RCU protected.
1170 * Hence this function internally uses RCU updater strategy with mutex locks
1171 * to keep the integrity of the internal data structures. Callers should ensure
1172 * that this function is *NOT* called under RCU protection or in contexts where
1173 * mutex cannot be locked.
1177 * Duplicate OPPs (both freq and volt are same) and opp->available
1178 * -EEXIST Freq are same and volt are different OR
1179 * Duplicate OPPs (both freq and volt are same) and !opp->available
1180 * -ENOMEM Memory allocation failure
1182 int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
1183 unsigned long freq, long u_volt, bool dynamic)
1185 struct dev_pm_opp *new_opp;
1189 new_opp = _opp_allocate(opp_table);
1193 /* populate the opp table */
1194 new_opp->rate = freq;
1195 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
1196 new_opp->supplies[0].u_volt = u_volt;
1197 new_opp->supplies[0].u_volt_min = u_volt - tol;
1198 new_opp->supplies[0].u_volt_max = u_volt + tol;
1199 new_opp->available = true;
1200 new_opp->dynamic = dynamic;
1202 ret = _opp_add(dev, new_opp, opp_table);
1204 /* Don't return error for duplicate OPPs */
1211 * Notify the changes in the availability of the operable
1212 * frequency/voltage list.
1214 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);
1224 * dev_pm_opp_set_supported_hw() - Set supported platforms
1225 * @dev: Device for which supported-hw has to be set.
1226 * @versions: Array of hierarchy of versions to match.
1227 * @count: Number of elements in the array.
1229 * This is required only for the V2 bindings, and it enables a platform to
1230 * specify the hierarchy of versions it supports. OPP layer will then enable
1231 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1234 struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
1235 const u32 *versions, unsigned int count)
1237 struct opp_table *opp_table;
1240 opp_table = dev_pm_opp_get_opp_table(dev);
1242 return ERR_PTR(-ENOMEM);
1244 /* Make sure there are no concurrent readers while updating opp_table */
1245 WARN_ON(!list_empty(&opp_table->opp_list));
1247 /* Do we already have a version hierarchy associated with opp_table? */
1248 if (opp_table->supported_hw) {
1249 dev_err(dev, "%s: Already have supported hardware list\n",
1255 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
1257 if (!opp_table->supported_hw) {
1262 opp_table->supported_hw_count = count;
1267 dev_pm_opp_put_opp_table(opp_table);
1269 return ERR_PTR(ret);
1271 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1274 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
1275 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
1277 * This is required only for the V2 bindings, and is called for a matching
1278 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
1279 * will not be freed.
1281 void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
1283 /* Make sure there are no concurrent readers while updating opp_table */
1284 WARN_ON(!list_empty(&opp_table->opp_list));
1286 if (!opp_table->supported_hw) {
1287 pr_err("%s: Doesn't have supported hardware list\n",
1292 kfree(opp_table->supported_hw);
1293 opp_table->supported_hw = NULL;
1294 opp_table->supported_hw_count = 0;
1296 dev_pm_opp_put_opp_table(opp_table);
1298 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1301 * dev_pm_opp_set_prop_name() - Set prop-extn name
1302 * @dev: Device for which the prop-name has to be set.
1303 * @name: name to postfix to properties.
1305 * This is required only for the V2 bindings, and it enables a platform to
1306 * specify the extn to be used for certain property names. The properties to
1307 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1308 * should postfix the property name with -<name> while looking for them.
1310 struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1312 struct opp_table *opp_table;
1315 opp_table = dev_pm_opp_get_opp_table(dev);
1317 return ERR_PTR(-ENOMEM);
1319 /* Make sure there are no concurrent readers while updating opp_table */
1320 WARN_ON(!list_empty(&opp_table->opp_list));
1322 /* Do we already have a prop-name associated with opp_table? */
1323 if (opp_table->prop_name) {
1324 dev_err(dev, "%s: Already have prop-name %s\n", __func__,
1325 opp_table->prop_name);
1330 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1331 if (!opp_table->prop_name) {
1339 dev_pm_opp_put_opp_table(opp_table);
1341 return ERR_PTR(ret);
1343 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1346 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
1347 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
1349 * This is required only for the V2 bindings, and is called for a matching
1350 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
1351 * will not be freed.
1353 void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
1355 /* Make sure there are no concurrent readers while updating opp_table */
1356 WARN_ON(!list_empty(&opp_table->opp_list));
1358 if (!opp_table->prop_name) {
1359 pr_err("%s: Doesn't have a prop-name\n", __func__);
1363 kfree(opp_table->prop_name);
1364 opp_table->prop_name = NULL;
1366 dev_pm_opp_put_opp_table(opp_table);
1368 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1370 static int _allocate_set_opp_data(struct opp_table *opp_table)
1372 struct dev_pm_set_opp_data *data;
1373 int len, count = opp_table->regulator_count;
1375 if (WARN_ON(!count))
1378 /* space for set_opp_data */
1379 len = sizeof(*data);
1381 /* space for old_opp.supplies and new_opp.supplies */
1382 len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1384 data = kzalloc(len, GFP_KERNEL);
1388 data->old_opp.supplies = (void *)(data + 1);
1389 data->new_opp.supplies = data->old_opp.supplies + count;
1391 opp_table->set_opp_data = data;
1396 static void _free_set_opp_data(struct opp_table *opp_table)
1398 kfree(opp_table->set_opp_data);
1399 opp_table->set_opp_data = NULL;
1403 * dev_pm_opp_set_regulators() - Set regulator names for the device
1404 * @dev: Device for which regulator name is being set.
1405 * @names: Array of pointers to the names of the regulator.
1406 * @count: Number of regulators.
1408 * In order to support OPP switching, OPP layer needs to know the name of the
1409 * device's regulators, as the core would be required to switch voltages as
1412 * This must be called before any OPPs are initialized for the device.
1414 struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1415 const char * const names[],
1418 struct opp_table *opp_table;
1419 struct regulator *reg;
1422 opp_table = dev_pm_opp_get_opp_table(dev);
1424 return ERR_PTR(-ENOMEM);
1426 /* This should be called before OPPs are initialized */
1427 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1432 /* Already have regulators set */
1433 if (opp_table->regulators) {
1438 opp_table->regulators = kmalloc_array(count,
1439 sizeof(*opp_table->regulators),
1441 if (!opp_table->regulators) {
1446 for (i = 0; i < count; i++) {
1447 reg = regulator_get_optional(dev, names[i]);
1450 if (ret != -EPROBE_DEFER)
1451 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1452 __func__, names[i], ret);
1453 goto free_regulators;
1456 opp_table->regulators[i] = reg;
1459 opp_table->regulator_count = count;
1461 /* Allocate block only once to pass to set_opp() routines */
1462 ret = _allocate_set_opp_data(opp_table);
1464 goto free_regulators;
1470 regulator_put(opp_table->regulators[--i]);
1472 kfree(opp_table->regulators);
1473 opp_table->regulators = NULL;
1474 opp_table->regulator_count = 0;
1476 dev_pm_opp_put_opp_table(opp_table);
1478 return ERR_PTR(ret);
1480 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
1483 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1484 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
1486 void dev_pm_opp_put_regulators(struct opp_table *opp_table)
1490 if (!opp_table->regulators) {
1491 pr_err("%s: Doesn't have regulators set\n", __func__);
1495 /* Make sure there are no concurrent readers while updating opp_table */
1496 WARN_ON(!list_empty(&opp_table->opp_list));
1498 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1499 regulator_put(opp_table->regulators[i]);
1501 _free_set_opp_data(opp_table);
1503 kfree(opp_table->regulators);
1504 opp_table->regulators = NULL;
1505 opp_table->regulator_count = 0;
1507 dev_pm_opp_put_opp_table(opp_table);
1509 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
1512 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
1513 * @dev: Device for which the helper is getting registered.
1514 * @set_opp: Custom set OPP helper.
1516 * This is useful to support complex platforms (like platforms with multiple
1517 * regulators per device), instead of the generic OPP set rate helper.
1519 * This must be called before any OPPs are initialized for the device.
1521 struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
1522 int (*set_opp)(struct dev_pm_set_opp_data *data))
1524 struct opp_table *opp_table;
1528 return ERR_PTR(-EINVAL);
1530 opp_table = dev_pm_opp_get_opp_table(dev);
1532 return ERR_PTR(-ENOMEM);
1534 /* This should be called before OPPs are initialized */
1535 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1540 /* Already have custom set_opp helper */
1541 if (WARN_ON(opp_table->set_opp)) {
1546 opp_table->set_opp = set_opp;
1551 dev_pm_opp_put_opp_table(opp_table);
1553 return ERR_PTR(ret);
1555 EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
1558 * dev_pm_opp_register_put_opp_helper() - Releases resources blocked for
1560 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
1562 * Release resources blocked for platform specific set_opp helper.
1564 void dev_pm_opp_register_put_opp_helper(struct opp_table *opp_table)
1566 if (!opp_table->set_opp) {
1567 pr_err("%s: Doesn't have custom set_opp helper set\n",
1572 /* Make sure there are no concurrent readers while updating opp_table */
1573 WARN_ON(!list_empty(&opp_table->opp_list));
1575 opp_table->set_opp = NULL;
1577 dev_pm_opp_put_opp_table(opp_table);
1579 EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper);
1582 * dev_pm_opp_add() - Add an OPP table from a table definitions
1583 * @dev: device for which we do this operation
1584 * @freq: Frequency in Hz for this OPP
1585 * @u_volt: Voltage in uVolts for this OPP
1587 * This function adds an opp definition to the opp table and returns status.
1588 * The opp is made available by default and it can be controlled using
1589 * dev_pm_opp_enable/disable functions.
1591 * Locking: The internal opp_table and opp structures are RCU protected.
1592 * Hence this function internally uses RCU updater strategy with mutex locks
1593 * to keep the integrity of the internal data structures. Callers should ensure
1594 * that this function is *NOT* called under RCU protection or in contexts where
1595 * mutex cannot be locked.
1599 * Duplicate OPPs (both freq and volt are same) and opp->available
1600 * -EEXIST Freq are same and volt are different OR
1601 * Duplicate OPPs (both freq and volt are same) and !opp->available
1602 * -ENOMEM Memory allocation failure
1604 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1606 struct opp_table *opp_table;
1609 opp_table = dev_pm_opp_get_opp_table(dev);
1613 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
1615 dev_pm_opp_put_opp_table(opp_table);
1618 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1621 * _opp_set_availability() - helper to set the availability of an opp
1622 * @dev: device for which we do this operation
1623 * @freq: OPP frequency to modify availability
1624 * @availability_req: availability status requested for this opp
1626 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
1627 * share a common logic which is isolated here.
1629 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1630 * copy operation, returns 0 if no modification was done OR modification was
1633 * Locking: The internal opp_table and opp structures are RCU protected.
1634 * Hence this function internally uses RCU updater strategy with mutex locks to
1635 * keep the integrity of the internal data structures. Callers should ensure
1636 * that this function is *NOT* called under RCU protection or in contexts where
1637 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1639 static int _opp_set_availability(struct device *dev, unsigned long freq,
1640 bool availability_req)
1642 struct opp_table *opp_table;
1643 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
1646 /* keep the node allocated */
1647 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
1651 mutex_lock(&opp_table_lock);
1653 /* Find the opp_table */
1654 opp_table = _find_opp_table(dev);
1655 if (IS_ERR(opp_table)) {
1656 r = PTR_ERR(opp_table);
1657 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1661 mutex_lock(&opp_table->lock);
1663 /* Do we have the frequency? */
1664 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
1665 if (tmp_opp->rate == freq) {
1671 mutex_unlock(&opp_table->lock);
1678 /* Is update really needed? */
1679 if (opp->available == availability_req)
1681 /* copy the old data over */
1684 /* plug in new node */
1685 new_opp->available = availability_req;
1687 list_replace_rcu(&opp->node, &new_opp->node);
1688 mutex_unlock(&opp_table_lock);
1689 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
1691 /* Notify the change of the OPP availability */
1692 if (availability_req)
1693 srcu_notifier_call_chain(&opp_table->srcu_head,
1694 OPP_EVENT_ENABLE, new_opp);
1696 srcu_notifier_call_chain(&opp_table->srcu_head,
1697 OPP_EVENT_DISABLE, new_opp);
1702 mutex_unlock(&opp_table_lock);
1708 * dev_pm_opp_enable() - Enable a specific OPP
1709 * @dev: device for which we do this operation
1710 * @freq: OPP frequency to enable
1712 * Enables a provided opp. If the operation is valid, this returns 0, else the
1713 * corresponding error value. It is meant to be used for users an OPP available
1714 * after being temporarily made unavailable with dev_pm_opp_disable.
1716 * Locking: The internal opp_table and opp structures are RCU protected.
1717 * Hence this function indirectly uses RCU and mutex locks to keep the
1718 * integrity of the internal data structures. Callers should ensure that
1719 * this function is *NOT* called under RCU protection or in contexts where
1720 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1722 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1723 * copy operation, returns 0 if no modification was done OR modification was
1726 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
1728 return _opp_set_availability(dev, freq, true);
1730 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
1733 * dev_pm_opp_disable() - Disable a specific OPP
1734 * @dev: device for which we do this operation
1735 * @freq: OPP frequency to disable
1737 * Disables a provided opp. If the operation is valid, this returns
1738 * 0, else the corresponding error value. It is meant to be a temporary
1739 * control by users to make this OPP not available until the circumstances are
1740 * right to make it available again (with a call to dev_pm_opp_enable).
1742 * Locking: The internal opp_table and opp structures are RCU protected.
1743 * Hence this function indirectly uses RCU and mutex locks to keep the
1744 * integrity of the internal data structures. Callers should ensure that
1745 * this function is *NOT* called under RCU protection or in contexts where
1746 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1748 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1749 * copy operation, returns 0 if no modification was done OR modification was
1752 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
1754 return _opp_set_availability(dev, freq, false);
1756 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
1759 * dev_pm_opp_register_notifier() - Register OPP notifier for the device
1760 * @dev: Device for which notifier needs to be registered
1761 * @nb: Notifier block to be registered
1763 * Return: 0 on success or a negative error value.
1765 int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
1767 struct opp_table *opp_table;
1772 opp_table = _find_opp_table(dev);
1773 if (IS_ERR(opp_table)) {
1774 ret = PTR_ERR(opp_table);
1778 ret = srcu_notifier_chain_register(&opp_table->srcu_head, nb);
1785 EXPORT_SYMBOL(dev_pm_opp_register_notifier);
1788 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
1789 * @dev: Device for which notifier needs to be unregistered
1790 * @nb: Notifier block to be unregistered
1792 * Return: 0 on success or a negative error value.
1794 int dev_pm_opp_unregister_notifier(struct device *dev,
1795 struct notifier_block *nb)
1797 struct opp_table *opp_table;
1802 opp_table = _find_opp_table(dev);
1803 if (IS_ERR(opp_table)) {
1804 ret = PTR_ERR(opp_table);
1808 ret = srcu_notifier_chain_unregister(&opp_table->srcu_head, nb);
1815 EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
1818 * Free OPPs either created using static entries present in DT or even the
1819 * dynamically added entries based on remove_all param.
1821 void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
1824 struct dev_pm_opp *opp, *tmp;
1826 /* Find if opp_table manages a single device */
1827 if (list_is_singular(&opp_table->dev_list)) {
1828 /* Free static OPPs */
1829 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1830 if (remove_all || !opp->dynamic)
1831 dev_pm_opp_put(opp);
1834 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
1838 void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)
1840 struct opp_table *opp_table;
1842 /* Hold our table modification lock here */
1843 mutex_lock(&opp_table_lock);
1845 /* Check for existing table for 'dev' */
1846 opp_table = _find_opp_table(dev);
1847 if (IS_ERR(opp_table)) {
1848 int error = PTR_ERR(opp_table);
1850 if (error != -ENODEV)
1851 WARN(1, "%s: opp_table: %d\n",
1852 IS_ERR_OR_NULL(dev) ?
1853 "Invalid device" : dev_name(dev),
1858 _dev_pm_opp_remove_table(opp_table, dev, remove_all);
1861 mutex_unlock(&opp_table_lock);
1865 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
1866 * @dev: device pointer used to lookup OPP table.
1868 * Free both OPPs created using static entries present in DT and the
1869 * dynamically added entries.
1871 * Locking: The internal opp_table and opp structures are RCU protected.
1872 * Hence this function indirectly uses RCU updater strategy with mutex locks
1873 * to keep the integrity of the internal data structures. Callers should ensure
1874 * that this function is *NOT* called under RCU protection or in contexts where
1875 * mutex cannot be locked.
1877 void dev_pm_opp_remove_table(struct device *dev)
1879 _dev_pm_opp_find_and_remove_table(dev, true);
1881 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);