PM / OPP: Add infrastructure to manage multiple regulators
[linux-2.6/btrfs-unstable.git] / drivers / base / power / opp / core.c
blobb4da31c5a5eb4a27dc25ce214420f0f9833bca32
1 /*
2 * Generic OPP Interface
4 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
5 * Nishanth Menon
6 * Romit Dasgupta
7 * Kevin Hilman
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>
24 #include "opp.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() \
36 do { \
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"); \
41 } while (0)
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)
50 return opp_dev;
52 return NULL;
55 /**
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))
84 return opp_table;
86 return ERR_PTR(-ENODEV);
89 /**
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
94 * return 0
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
104 * pointer.
106 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
108 struct dev_pm_opp *tmp_opp;
109 unsigned long v = 0;
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__);
116 else
117 v = tmp_opp->supplies[0].u_volt;
119 return v;
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
128 * return 0
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
136 * pointer.
138 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
140 struct dev_pm_opp *tmp_opp;
141 unsigned long f = 0;
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__);
148 else
149 f = tmp_opp->rate;
151 return f;
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
171 * pointer.
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__);
182 return false;
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;
202 rcu_read_lock();
204 opp_table = _find_opp_table(dev);
205 if (IS_ERR(opp_table))
206 clock_latency_ns = 0;
207 else
208 clock_latency_ns = opp_table->clock_latency_ns_max;
210 rcu_read_unlock();
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;
218 int count;
220 rcu_read_lock();
222 opp_table = _find_opp_table(dev);
223 if (!IS_ERR(opp_table))
224 count = opp_table->regulator_count;
225 else
226 count = 0;
228 rcu_read_unlock();
230 return 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;
247 int ret, i, count;
248 struct {
249 unsigned long min;
250 unsigned long max;
251 } *uV;
253 count = _get_regulator_count(dev);
255 /* Regulator may not be required for the device */
256 if (!count)
257 return 0;
259 regulators = kmalloc_array(count, sizeof(*regulators), GFP_KERNEL);
260 if (!regulators)
261 return 0;
263 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
264 if (!uV)
265 goto free_regulators;
267 rcu_read_lock();
269 opp_table = _find_opp_table(dev);
270 if (IS_ERR(opp_table)) {
271 rcu_read_unlock();
272 goto free_uV;
275 memcpy(regulators, opp_table->regulators, count * sizeof(*regulators));
277 for (i = 0; i < count; i++) {
278 uV[i].min = ~0;
279 uV[i].max = 0;
281 list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
282 if (!opp->available)
283 continue;
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;
292 rcu_read_unlock();
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);
300 if (ret > 0)
301 latency_ns += ret * 1000;
304 free_uV:
305 kfree(uV);
306 free_regulators:
307 kfree(regulators);
309 return latency_ns;
311 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
314 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
315 * nanoseconds
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)
352 return NULL;
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;
371 int count = 0;
373 rcu_read_lock();
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",
379 __func__, count);
380 goto out_unlock;
383 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
384 if (temp_opp->available)
385 count++;
388 out_unlock:
389 rcu_read_unlock();
390 return count;
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,
421 unsigned long freq,
422 bool available)
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);
434 return ERR_PTR(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) {
440 opp = temp_opp;
441 break;
445 return opp;
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,
450 unsigned long *freq)
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) {
456 opp = temp_opp;
457 *freq = opp->rate;
458 break;
462 return opp;
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
471 * for a device.
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
475 * values can be:
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,
487 unsigned long *freq)
489 struct opp_table *opp_table;
491 opp_rcu_lockdep_assert();
493 if (!dev || !freq) {
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
512 * for a device.
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
516 * values can be:
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,
528 unsigned long *freq)
530 struct opp_table *opp_table;
531 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
533 opp_rcu_lockdep_assert();
535 if (!dev || !freq) {
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)
548 break;
549 else
550 opp = temp_opp;
553 if (!IS_ERR(opp))
554 *freq = opp->rate;
556 return opp;
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;
567 struct clk *clk;
569 rcu_read_lock();
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);
575 goto unlock;
578 clk = opp_table->clk;
579 if (IS_ERR(clk))
580 dev_err(dev, "%s: No clock available for the device\n",
581 __func__);
583 unlock:
584 rcu_read_unlock();
585 return clk;
588 static int _set_opp_voltage(struct device *dev, struct regulator *reg,
589 struct dev_pm_opp_supply *supply)
591 int ret;
593 /* Regulator not available for device */
594 if (IS_ERR(reg)) {
595 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
596 PTR_ERR(reg));
597 return 0;
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);
605 if (ret)
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);
610 return ret;
614 * dev_pm_opp_set_rate() - Configure new OPP based on frequency
615 * @dev: device for which we do this operation
616 * @target_freq: frequency to achieve
618 * This configures the power-supplies and clock source to the levels specified
619 * by the OPP corresponding to the target_freq.
621 * Locking: This function takes rcu_read_lock().
623 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
625 struct opp_table *opp_table;
626 struct dev_pm_opp *old_opp, *opp;
627 struct regulator *reg = ERR_PTR(-ENXIO);
628 struct clk *clk;
629 unsigned long freq, old_freq;
630 struct dev_pm_opp_supply old_supply, new_supply;
631 int ret;
633 if (unlikely(!target_freq)) {
634 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
635 target_freq);
636 return -EINVAL;
639 clk = _get_opp_clk(dev);
640 if (IS_ERR(clk))
641 return PTR_ERR(clk);
643 freq = clk_round_rate(clk, target_freq);
644 if ((long)freq <= 0)
645 freq = target_freq;
647 old_freq = clk_get_rate(clk);
649 /* Return early if nothing to do */
650 if (old_freq == freq) {
651 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
652 __func__, freq);
653 return 0;
656 rcu_read_lock();
658 opp_table = _find_opp_table(dev);
659 if (IS_ERR(opp_table)) {
660 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
661 rcu_read_unlock();
662 return PTR_ERR(opp_table);
665 old_opp = _find_freq_ceil(opp_table, &old_freq);
666 if (IS_ERR(old_opp)) {
667 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
668 __func__, old_freq, PTR_ERR(old_opp));
671 opp = _find_freq_ceil(opp_table, &freq);
672 if (IS_ERR(opp)) {
673 ret = PTR_ERR(opp);
674 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
675 __func__, freq, ret);
676 rcu_read_unlock();
677 return ret;
680 if (opp_table->regulators) {
681 /* This function only supports single regulator per device */
682 if (WARN_ON(opp_table->regulator_count > 1)) {
683 dev_err(dev, "multiple regulators not supported\n");
684 rcu_read_unlock();
685 return -EINVAL;
688 reg = opp_table->regulators[0];
691 if (IS_ERR(old_opp))
692 old_supply.u_volt = 0;
693 else
694 memcpy(&old_supply, old_opp->supplies, sizeof(old_supply));
696 memcpy(&new_supply, opp->supplies, sizeof(new_supply));
698 rcu_read_unlock();
700 /* Scaling up? Scale voltage before frequency */
701 if (freq > old_freq) {
702 ret = _set_opp_voltage(dev, reg, &new_supply);
703 if (ret)
704 goto restore_voltage;
707 /* Change frequency */
709 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n",
710 __func__, old_freq, freq);
712 ret = clk_set_rate(clk, freq);
713 if (ret) {
714 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
715 ret);
716 goto restore_voltage;
719 /* Scaling down? Scale voltage after frequency */
720 if (freq < old_freq) {
721 ret = _set_opp_voltage(dev, reg, &new_supply);
722 if (ret)
723 goto restore_freq;
726 return 0;
728 restore_freq:
729 if (clk_set_rate(clk, old_freq))
730 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
731 __func__, old_freq);
732 restore_voltage:
733 /* This shouldn't harm even if the voltages weren't updated earlier */
734 if (old_supply.u_volt)
735 _set_opp_voltage(dev, reg, &old_supply);
737 return ret;
739 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
741 /* OPP-dev Helpers */
742 static void _kfree_opp_dev_rcu(struct rcu_head *head)
744 struct opp_device *opp_dev;
746 opp_dev = container_of(head, struct opp_device, rcu_head);
747 kfree_rcu(opp_dev, rcu_head);
750 static void _remove_opp_dev(struct opp_device *opp_dev,
751 struct opp_table *opp_table)
753 opp_debug_unregister(opp_dev, opp_table);
754 list_del(&opp_dev->node);
755 call_srcu(&opp_table->srcu_head.srcu, &opp_dev->rcu_head,
756 _kfree_opp_dev_rcu);
759 struct opp_device *_add_opp_dev(const struct device *dev,
760 struct opp_table *opp_table)
762 struct opp_device *opp_dev;
763 int ret;
765 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
766 if (!opp_dev)
767 return NULL;
769 /* Initialize opp-dev */
770 opp_dev->dev = dev;
771 list_add_rcu(&opp_dev->node, &opp_table->dev_list);
773 /* Create debugfs entries for the opp_table */
774 ret = opp_debug_register(opp_dev, opp_table);
775 if (ret)
776 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
777 __func__, ret);
779 return opp_dev;
783 * _add_opp_table() - Find OPP table or allocate a new one
784 * @dev: device for which we do this operation
786 * It tries to find an existing table first, if it couldn't find one, it
787 * allocates a new OPP table and returns that.
789 * Return: valid opp_table pointer if success, else NULL.
791 static struct opp_table *_add_opp_table(struct device *dev)
793 struct opp_table *opp_table;
794 struct opp_device *opp_dev;
795 int ret;
797 /* Check for existing table for 'dev' first */
798 opp_table = _find_opp_table(dev);
799 if (!IS_ERR(opp_table))
800 return opp_table;
803 * Allocate a new OPP table. In the infrequent case where a new
804 * device is needed to be added, we pay this penalty.
806 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
807 if (!opp_table)
808 return NULL;
810 INIT_LIST_HEAD(&opp_table->dev_list);
812 opp_dev = _add_opp_dev(dev, opp_table);
813 if (!opp_dev) {
814 kfree(opp_table);
815 return NULL;
818 _of_init_opp_table(opp_table, dev);
820 /* Find clk for the device */
821 opp_table->clk = clk_get(dev, NULL);
822 if (IS_ERR(opp_table->clk)) {
823 ret = PTR_ERR(opp_table->clk);
824 if (ret != -EPROBE_DEFER)
825 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
826 ret);
829 srcu_init_notifier_head(&opp_table->srcu_head);
830 INIT_LIST_HEAD(&opp_table->opp_list);
832 /* Secure the device table modification */
833 list_add_rcu(&opp_table->node, &opp_tables);
834 return opp_table;
838 * _kfree_device_rcu() - Free opp_table RCU handler
839 * @head: RCU head
841 static void _kfree_device_rcu(struct rcu_head *head)
843 struct opp_table *opp_table = container_of(head, struct opp_table,
844 rcu_head);
846 kfree_rcu(opp_table, rcu_head);
850 * _remove_opp_table() - Removes a OPP table
851 * @opp_table: OPP table to be removed.
853 * Removes/frees OPP table if it doesn't contain any OPPs.
855 static void _remove_opp_table(struct opp_table *opp_table)
857 struct opp_device *opp_dev;
859 if (!list_empty(&opp_table->opp_list))
860 return;
862 if (opp_table->supported_hw)
863 return;
865 if (opp_table->prop_name)
866 return;
868 if (opp_table->regulators)
869 return;
871 /* Release clk */
872 if (!IS_ERR(opp_table->clk))
873 clk_put(opp_table->clk);
875 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
876 node);
878 _remove_opp_dev(opp_dev, opp_table);
880 /* dev_list must be empty now */
881 WARN_ON(!list_empty(&opp_table->dev_list));
883 list_del_rcu(&opp_table->node);
884 call_srcu(&opp_table->srcu_head.srcu, &opp_table->rcu_head,
885 _kfree_device_rcu);
889 * _kfree_opp_rcu() - Free OPP RCU handler
890 * @head: RCU head
892 static void _kfree_opp_rcu(struct rcu_head *head)
894 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
896 kfree_rcu(opp, rcu_head);
900 * _opp_remove() - Remove an OPP from a table definition
901 * @opp_table: points back to the opp_table struct this opp belongs to
902 * @opp: pointer to the OPP to remove
903 * @notify: OPP_EVENT_REMOVE notification should be sent or not
905 * This function removes an opp definition from the opp table.
907 * Locking: The internal opp_table and opp structures are RCU protected.
908 * It is assumed that the caller holds required mutex for an RCU updater
909 * strategy.
911 void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp,
912 bool notify)
915 * Notify the changes in the availability of the operable
916 * frequency/voltage list.
918 if (notify)
919 srcu_notifier_call_chain(&opp_table->srcu_head,
920 OPP_EVENT_REMOVE, opp);
921 opp_debug_remove_one(opp);
922 list_del_rcu(&opp->node);
923 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
925 _remove_opp_table(opp_table);
929 * dev_pm_opp_remove() - Remove an OPP from OPP table
930 * @dev: device for which we do this operation
931 * @freq: OPP to remove with matching 'freq'
933 * This function removes an opp from the opp table.
935 * Locking: The internal opp_table and opp structures are RCU protected.
936 * Hence this function internally uses RCU updater strategy with mutex locks
937 * to keep the integrity of the internal data structures. Callers should ensure
938 * that this function is *NOT* called under RCU protection or in contexts where
939 * mutex cannot be locked.
941 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
943 struct dev_pm_opp *opp;
944 struct opp_table *opp_table;
945 bool found = false;
947 /* Hold our table modification lock here */
948 mutex_lock(&opp_table_lock);
950 opp_table = _find_opp_table(dev);
951 if (IS_ERR(opp_table))
952 goto unlock;
954 list_for_each_entry(opp, &opp_table->opp_list, node) {
955 if (opp->rate == freq) {
956 found = true;
957 break;
961 if (!found) {
962 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
963 __func__, freq);
964 goto unlock;
967 _opp_remove(opp_table, opp, true);
968 unlock:
969 mutex_unlock(&opp_table_lock);
971 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
973 struct dev_pm_opp *_allocate_opp(struct device *dev,
974 struct opp_table **opp_table)
976 struct dev_pm_opp *opp;
977 int count, supply_size;
978 struct opp_table *table;
980 table = _add_opp_table(dev);
981 if (!table)
982 return NULL;
984 /* Allocate space for at least one supply */
985 count = table->regulator_count ? table->regulator_count : 1;
986 supply_size = sizeof(*opp->supplies) * count;
988 /* allocate new OPP node and supplies structures */
989 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
990 if (!opp) {
991 kfree(table);
992 return NULL;
995 /* Put the supplies at the end of the OPP structure as an empty array */
996 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
997 INIT_LIST_HEAD(&opp->node);
999 *opp_table = table;
1001 return opp;
1004 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
1005 struct opp_table *opp_table)
1007 struct regulator *reg;
1008 int i;
1010 for (i = 0; i < opp_table->regulator_count; i++) {
1011 reg = opp_table->regulators[i];
1013 if (!regulator_is_supported_voltage(reg,
1014 opp->supplies[i].u_volt_min,
1015 opp->supplies[i].u_volt_max)) {
1016 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
1017 __func__, opp->supplies[i].u_volt_min,
1018 opp->supplies[i].u_volt_max);
1019 return false;
1023 return true;
1026 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
1027 struct opp_table *opp_table)
1029 struct dev_pm_opp *opp;
1030 struct list_head *head = &opp_table->opp_list;
1031 int ret;
1034 * Insert new OPP in order of increasing frequency and discard if
1035 * already present.
1037 * Need to use &opp_table->opp_list in the condition part of the 'for'
1038 * loop, don't replace it with head otherwise it will become an infinite
1039 * loop.
1041 list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
1042 if (new_opp->rate > opp->rate) {
1043 head = &opp->node;
1044 continue;
1047 if (new_opp->rate < opp->rate)
1048 break;
1050 /* Duplicate OPPs */
1051 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
1052 __func__, opp->rate, opp->supplies[0].u_volt,
1053 opp->available, new_opp->rate,
1054 new_opp->supplies[0].u_volt, new_opp->available);
1056 /* Should we compare voltages for all regulators here ? */
1057 return opp->available &&
1058 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? 0 : -EEXIST;
1061 new_opp->opp_table = opp_table;
1062 list_add_rcu(&new_opp->node, head);
1064 ret = opp_debug_create_one(new_opp, opp_table);
1065 if (ret)
1066 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1067 __func__, ret);
1069 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
1070 new_opp->available = false;
1071 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1072 __func__, new_opp->rate);
1075 return 0;
1079 * _opp_add_v1() - Allocate a OPP based on v1 bindings.
1080 * @dev: device for which we do this operation
1081 * @freq: Frequency in Hz for this OPP
1082 * @u_volt: Voltage in uVolts for this OPP
1083 * @dynamic: Dynamically added OPPs.
1085 * This function adds an opp definition to the opp table and returns status.
1086 * The opp is made available by default and it can be controlled using
1087 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
1089 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
1090 * and freed by dev_pm_opp_of_remove_table.
1092 * Locking: The internal opp_table and opp structures are RCU protected.
1093 * Hence this function internally uses RCU updater strategy with mutex locks
1094 * to keep the integrity of the internal data structures. Callers should ensure
1095 * that this function is *NOT* called under RCU protection or in contexts where
1096 * mutex cannot be locked.
1098 * Return:
1099 * 0 On success OR
1100 * Duplicate OPPs (both freq and volt are same) and opp->available
1101 * -EEXIST Freq are same and volt are different OR
1102 * Duplicate OPPs (both freq and volt are same) and !opp->available
1103 * -ENOMEM Memory allocation failure
1105 int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
1106 bool dynamic)
1108 struct opp_table *opp_table;
1109 struct dev_pm_opp *new_opp;
1110 unsigned long tol;
1111 int ret;
1113 /* Hold our table modification lock here */
1114 mutex_lock(&opp_table_lock);
1116 new_opp = _allocate_opp(dev, &opp_table);
1117 if (!new_opp) {
1118 ret = -ENOMEM;
1119 goto unlock;
1122 /* populate the opp table */
1123 new_opp->rate = freq;
1124 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
1125 new_opp->supplies[0].u_volt = u_volt;
1126 new_opp->supplies[0].u_volt_min = u_volt - tol;
1127 new_opp->supplies[0].u_volt_max = u_volt + tol;
1128 new_opp->available = true;
1129 new_opp->dynamic = dynamic;
1131 ret = _opp_add(dev, new_opp, opp_table);
1132 if (ret)
1133 goto free_opp;
1135 mutex_unlock(&opp_table_lock);
1138 * Notify the changes in the availability of the operable
1139 * frequency/voltage list.
1141 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);
1142 return 0;
1144 free_opp:
1145 _opp_remove(opp_table, new_opp, false);
1146 unlock:
1147 mutex_unlock(&opp_table_lock);
1148 return ret;
1152 * dev_pm_opp_set_supported_hw() - Set supported platforms
1153 * @dev: Device for which supported-hw has to be set.
1154 * @versions: Array of hierarchy of versions to match.
1155 * @count: Number of elements in the array.
1157 * This is required only for the V2 bindings, and it enables a platform to
1158 * specify the hierarchy of versions it supports. OPP layer will then enable
1159 * OPPs, which are available for those versions, based on its 'opp-supported-hw'
1160 * property.
1162 * Locking: The internal opp_table and opp structures are RCU protected.
1163 * Hence this function internally uses RCU updater strategy with mutex locks
1164 * to keep the integrity of the internal data structures. Callers should ensure
1165 * that this function is *NOT* called under RCU protection or in contexts where
1166 * mutex cannot be locked.
1168 int dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
1169 unsigned int count)
1171 struct opp_table *opp_table;
1172 int ret = 0;
1174 /* Hold our table modification lock here */
1175 mutex_lock(&opp_table_lock);
1177 opp_table = _add_opp_table(dev);
1178 if (!opp_table) {
1179 ret = -ENOMEM;
1180 goto unlock;
1183 /* Make sure there are no concurrent readers while updating opp_table */
1184 WARN_ON(!list_empty(&opp_table->opp_list));
1186 /* Do we already have a version hierarchy associated with opp_table? */
1187 if (opp_table->supported_hw) {
1188 dev_err(dev, "%s: Already have supported hardware list\n",
1189 __func__);
1190 ret = -EBUSY;
1191 goto err;
1194 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
1195 GFP_KERNEL);
1196 if (!opp_table->supported_hw) {
1197 ret = -ENOMEM;
1198 goto err;
1201 opp_table->supported_hw_count = count;
1202 mutex_unlock(&opp_table_lock);
1203 return 0;
1205 err:
1206 _remove_opp_table(opp_table);
1207 unlock:
1208 mutex_unlock(&opp_table_lock);
1210 return ret;
1212 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1215 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
1216 * @dev: Device for which supported-hw has to be put.
1218 * This is required only for the V2 bindings, and is called for a matching
1219 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
1220 * will not be freed.
1222 * Locking: The internal opp_table and opp structures are RCU protected.
1223 * Hence this function internally uses RCU updater strategy with mutex locks
1224 * to keep the integrity of the internal data structures. Callers should ensure
1225 * that this function is *NOT* called under RCU protection or in contexts where
1226 * mutex cannot be locked.
1228 void dev_pm_opp_put_supported_hw(struct device *dev)
1230 struct opp_table *opp_table;
1232 /* Hold our table modification lock here */
1233 mutex_lock(&opp_table_lock);
1235 /* Check for existing table for 'dev' first */
1236 opp_table = _find_opp_table(dev);
1237 if (IS_ERR(opp_table)) {
1238 dev_err(dev, "Failed to find opp_table: %ld\n",
1239 PTR_ERR(opp_table));
1240 goto unlock;
1243 /* Make sure there are no concurrent readers while updating opp_table */
1244 WARN_ON(!list_empty(&opp_table->opp_list));
1246 if (!opp_table->supported_hw) {
1247 dev_err(dev, "%s: Doesn't have supported hardware list\n",
1248 __func__);
1249 goto unlock;
1252 kfree(opp_table->supported_hw);
1253 opp_table->supported_hw = NULL;
1254 opp_table->supported_hw_count = 0;
1256 /* Try freeing opp_table if this was the last blocking resource */
1257 _remove_opp_table(opp_table);
1259 unlock:
1260 mutex_unlock(&opp_table_lock);
1262 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1265 * dev_pm_opp_set_prop_name() - Set prop-extn name
1266 * @dev: Device for which the prop-name has to be set.
1267 * @name: name to postfix to properties.
1269 * This is required only for the V2 bindings, and it enables a platform to
1270 * specify the extn to be used for certain property names. The properties to
1271 * which the extension will apply are opp-microvolt and opp-microamp. OPP core
1272 * should postfix the property name with -<name> while looking for them.
1274 * Locking: The internal opp_table and opp structures are RCU protected.
1275 * Hence this function internally uses RCU updater strategy with mutex locks
1276 * to keep the integrity of the internal data structures. Callers should ensure
1277 * that this function is *NOT* called under RCU protection or in contexts where
1278 * mutex cannot be locked.
1280 int dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1282 struct opp_table *opp_table;
1283 int ret = 0;
1285 /* Hold our table modification lock here */
1286 mutex_lock(&opp_table_lock);
1288 opp_table = _add_opp_table(dev);
1289 if (!opp_table) {
1290 ret = -ENOMEM;
1291 goto unlock;
1294 /* Make sure there are no concurrent readers while updating opp_table */
1295 WARN_ON(!list_empty(&opp_table->opp_list));
1297 /* Do we already have a prop-name associated with opp_table? */
1298 if (opp_table->prop_name) {
1299 dev_err(dev, "%s: Already have prop-name %s\n", __func__,
1300 opp_table->prop_name);
1301 ret = -EBUSY;
1302 goto err;
1305 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1306 if (!opp_table->prop_name) {
1307 ret = -ENOMEM;
1308 goto err;
1311 mutex_unlock(&opp_table_lock);
1312 return 0;
1314 err:
1315 _remove_opp_table(opp_table);
1316 unlock:
1317 mutex_unlock(&opp_table_lock);
1319 return ret;
1321 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1324 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
1325 * @dev: Device for which the prop-name has to be put.
1327 * This is required only for the V2 bindings, and is called for a matching
1328 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
1329 * will not be freed.
1331 * Locking: The internal opp_table and opp structures are RCU protected.
1332 * Hence this function internally uses RCU updater strategy with mutex locks
1333 * to keep the integrity of the internal data structures. Callers should ensure
1334 * that this function is *NOT* called under RCU protection or in contexts where
1335 * mutex cannot be locked.
1337 void dev_pm_opp_put_prop_name(struct device *dev)
1339 struct opp_table *opp_table;
1341 /* Hold our table modification lock here */
1342 mutex_lock(&opp_table_lock);
1344 /* Check for existing table for 'dev' first */
1345 opp_table = _find_opp_table(dev);
1346 if (IS_ERR(opp_table)) {
1347 dev_err(dev, "Failed to find opp_table: %ld\n",
1348 PTR_ERR(opp_table));
1349 goto unlock;
1352 /* Make sure there are no concurrent readers while updating opp_table */
1353 WARN_ON(!list_empty(&opp_table->opp_list));
1355 if (!opp_table->prop_name) {
1356 dev_err(dev, "%s: Doesn't have a prop-name\n", __func__);
1357 goto unlock;
1360 kfree(opp_table->prop_name);
1361 opp_table->prop_name = NULL;
1363 /* Try freeing opp_table if this was the last blocking resource */
1364 _remove_opp_table(opp_table);
1366 unlock:
1367 mutex_unlock(&opp_table_lock);
1369 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1372 * dev_pm_opp_set_regulators() - Set regulator names for the device
1373 * @dev: Device for which regulator name is being set.
1374 * @names: Array of pointers to the names of the regulator.
1375 * @count: Number of regulators.
1377 * In order to support OPP switching, OPP layer needs to know the name of the
1378 * device's regulators, as the core would be required to switch voltages as
1379 * well.
1381 * This must be called before any OPPs are initialized for the device.
1383 * Locking: The internal opp_table and opp structures are RCU protected.
1384 * Hence this function internally uses RCU updater strategy with mutex locks
1385 * to keep the integrity of the internal data structures. Callers should ensure
1386 * that this function is *NOT* called under RCU protection or in contexts where
1387 * mutex cannot be locked.
1389 struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
1390 const char * const names[],
1391 unsigned int count)
1393 struct opp_table *opp_table;
1394 struct regulator *reg;
1395 int ret, i;
1397 mutex_lock(&opp_table_lock);
1399 opp_table = _add_opp_table(dev);
1400 if (!opp_table) {
1401 ret = -ENOMEM;
1402 goto unlock;
1405 /* This should be called before OPPs are initialized */
1406 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1407 ret = -EBUSY;
1408 goto err;
1411 /* Already have regulators set */
1412 if (WARN_ON(opp_table->regulators)) {
1413 ret = -EBUSY;
1414 goto err;
1417 opp_table->regulators = kmalloc_array(count,
1418 sizeof(*opp_table->regulators),
1419 GFP_KERNEL);
1420 if (!opp_table->regulators) {
1421 ret = -ENOMEM;
1422 goto err;
1425 for (i = 0; i < count; i++) {
1426 reg = regulator_get_optional(dev, names[i]);
1427 if (IS_ERR(reg)) {
1428 ret = PTR_ERR(reg);
1429 if (ret != -EPROBE_DEFER)
1430 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1431 __func__, names[i], ret);
1432 goto free_regulators;
1435 opp_table->regulators[i] = reg;
1438 opp_table->regulator_count = count;
1440 mutex_unlock(&opp_table_lock);
1441 return opp_table;
1443 free_regulators:
1444 while (i != 0)
1445 regulator_put(opp_table->regulators[--i]);
1447 kfree(opp_table->regulators);
1448 opp_table->regulators = NULL;
1449 err:
1450 _remove_opp_table(opp_table);
1451 unlock:
1452 mutex_unlock(&opp_table_lock);
1454 return ERR_PTR(ret);
1456 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
1459 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
1460 * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
1462 * Locking: The internal opp_table and opp structures are RCU protected.
1463 * Hence this function internally uses RCU updater strategy with mutex locks
1464 * to keep the integrity of the internal data structures. Callers should ensure
1465 * that this function is *NOT* called under RCU protection or in contexts where
1466 * mutex cannot be locked.
1468 void dev_pm_opp_put_regulators(struct opp_table *opp_table)
1470 int i;
1472 mutex_lock(&opp_table_lock);
1474 if (!opp_table->regulators) {
1475 pr_err("%s: Doesn't have regulators set\n", __func__);
1476 goto unlock;
1479 /* Make sure there are no concurrent readers while updating opp_table */
1480 WARN_ON(!list_empty(&opp_table->opp_list));
1482 for (i = opp_table->regulator_count - 1; i >= 0; i--)
1483 regulator_put(opp_table->regulators[i]);
1485 kfree(opp_table->regulators);
1486 opp_table->regulators = NULL;
1487 opp_table->regulator_count = 0;
1489 /* Try freeing opp_table if this was the last blocking resource */
1490 _remove_opp_table(opp_table);
1492 unlock:
1493 mutex_unlock(&opp_table_lock);
1495 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
1498 * dev_pm_opp_add() - Add an OPP table from a table definitions
1499 * @dev: device for which we do this operation
1500 * @freq: Frequency in Hz for this OPP
1501 * @u_volt: Voltage in uVolts for this OPP
1503 * This function adds an opp definition to the opp table and returns status.
1504 * The opp is made available by default and it can be controlled using
1505 * dev_pm_opp_enable/disable functions.
1507 * Locking: The internal opp_table and opp structures are RCU protected.
1508 * Hence this function internally uses RCU updater strategy with mutex locks
1509 * to keep the integrity of the internal data structures. Callers should ensure
1510 * that this function is *NOT* called under RCU protection or in contexts where
1511 * mutex cannot be locked.
1513 * Return:
1514 * 0 On success OR
1515 * Duplicate OPPs (both freq and volt are same) and opp->available
1516 * -EEXIST Freq are same and volt are different OR
1517 * Duplicate OPPs (both freq and volt are same) and !opp->available
1518 * -ENOMEM Memory allocation failure
1520 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1522 return _opp_add_v1(dev, freq, u_volt, true);
1524 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1527 * _opp_set_availability() - helper to set the availability of an opp
1528 * @dev: device for which we do this operation
1529 * @freq: OPP frequency to modify availability
1530 * @availability_req: availability status requested for this opp
1532 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
1533 * share a common logic which is isolated here.
1535 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1536 * copy operation, returns 0 if no modification was done OR modification was
1537 * successful.
1539 * Locking: The internal opp_table and opp structures are RCU protected.
1540 * Hence this function internally uses RCU updater strategy with mutex locks to
1541 * keep the integrity of the internal data structures. Callers should ensure
1542 * that this function is *NOT* called under RCU protection or in contexts where
1543 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1545 static int _opp_set_availability(struct device *dev, unsigned long freq,
1546 bool availability_req)
1548 struct opp_table *opp_table;
1549 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
1550 int r = 0;
1552 /* keep the node allocated */
1553 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
1554 if (!new_opp)
1555 return -ENOMEM;
1557 mutex_lock(&opp_table_lock);
1559 /* Find the opp_table */
1560 opp_table = _find_opp_table(dev);
1561 if (IS_ERR(opp_table)) {
1562 r = PTR_ERR(opp_table);
1563 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1564 goto unlock;
1567 /* Do we have the frequency? */
1568 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
1569 if (tmp_opp->rate == freq) {
1570 opp = tmp_opp;
1571 break;
1574 if (IS_ERR(opp)) {
1575 r = PTR_ERR(opp);
1576 goto unlock;
1579 /* Is update really needed? */
1580 if (opp->available == availability_req)
1581 goto unlock;
1582 /* copy the old data over */
1583 *new_opp = *opp;
1585 /* plug in new node */
1586 new_opp->available = availability_req;
1588 list_replace_rcu(&opp->node, &new_opp->node);
1589 mutex_unlock(&opp_table_lock);
1590 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
1592 /* Notify the change of the OPP availability */
1593 if (availability_req)
1594 srcu_notifier_call_chain(&opp_table->srcu_head,
1595 OPP_EVENT_ENABLE, new_opp);
1596 else
1597 srcu_notifier_call_chain(&opp_table->srcu_head,
1598 OPP_EVENT_DISABLE, new_opp);
1600 return 0;
1602 unlock:
1603 mutex_unlock(&opp_table_lock);
1604 kfree(new_opp);
1605 return r;
1609 * dev_pm_opp_enable() - Enable a specific OPP
1610 * @dev: device for which we do this operation
1611 * @freq: OPP frequency to enable
1613 * Enables a provided opp. If the operation is valid, this returns 0, else the
1614 * corresponding error value. It is meant to be used for users an OPP available
1615 * after being temporarily made unavailable with dev_pm_opp_disable.
1617 * Locking: The internal opp_table and opp structures are RCU protected.
1618 * Hence this function indirectly uses RCU and mutex locks to keep the
1619 * integrity of the internal data structures. Callers should ensure that
1620 * this function is *NOT* called under RCU protection or in contexts where
1621 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1623 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1624 * copy operation, returns 0 if no modification was done OR modification was
1625 * successful.
1627 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
1629 return _opp_set_availability(dev, freq, true);
1631 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
1634 * dev_pm_opp_disable() - Disable a specific OPP
1635 * @dev: device for which we do this operation
1636 * @freq: OPP frequency to disable
1638 * Disables a provided opp. If the operation is valid, this returns
1639 * 0, else the corresponding error value. It is meant to be a temporary
1640 * control by users to make this OPP not available until the circumstances are
1641 * right to make it available again (with a call to dev_pm_opp_enable).
1643 * Locking: The internal opp_table and opp structures are RCU protected.
1644 * Hence this function indirectly uses RCU and mutex locks to keep the
1645 * integrity of the internal data structures. Callers should ensure that
1646 * this function is *NOT* called under RCU protection or in contexts where
1647 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1649 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1650 * copy operation, returns 0 if no modification was done OR modification was
1651 * successful.
1653 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
1655 return _opp_set_availability(dev, freq, false);
1657 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
1660 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
1661 * @dev: device pointer used to lookup OPP table.
1663 * Return: pointer to notifier head if found, otherwise -ENODEV or
1664 * -EINVAL based on type of error casted as pointer. value must be checked
1665 * with IS_ERR to determine valid pointer or error result.
1667 * Locking: This function must be called under rcu_read_lock(). opp_table is a
1668 * RCU protected pointer. The reason for the same is that the opp pointer which
1669 * is returned will remain valid for use with opp_get_{voltage, freq} only while
1670 * under the locked area. The pointer returned must be used prior to unlocking
1671 * with rcu_read_unlock() to maintain the integrity of the pointer.
1673 struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
1675 struct opp_table *opp_table = _find_opp_table(dev);
1677 if (IS_ERR(opp_table))
1678 return ERR_CAST(opp_table); /* matching type */
1680 return &opp_table->srcu_head;
1682 EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
1685 * Free OPPs either created using static entries present in DT or even the
1686 * dynamically added entries based on remove_all param.
1688 void _dev_pm_opp_remove_table(struct device *dev, bool remove_all)
1690 struct opp_table *opp_table;
1691 struct dev_pm_opp *opp, *tmp;
1693 /* Hold our table modification lock here */
1694 mutex_lock(&opp_table_lock);
1696 /* Check for existing table for 'dev' */
1697 opp_table = _find_opp_table(dev);
1698 if (IS_ERR(opp_table)) {
1699 int error = PTR_ERR(opp_table);
1701 if (error != -ENODEV)
1702 WARN(1, "%s: opp_table: %d\n",
1703 IS_ERR_OR_NULL(dev) ?
1704 "Invalid device" : dev_name(dev),
1705 error);
1706 goto unlock;
1709 /* Find if opp_table manages a single device */
1710 if (list_is_singular(&opp_table->dev_list)) {
1711 /* Free static OPPs */
1712 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1713 if (remove_all || !opp->dynamic)
1714 _opp_remove(opp_table, opp, true);
1716 } else {
1717 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
1720 unlock:
1721 mutex_unlock(&opp_table_lock);
1725 * dev_pm_opp_remove_table() - Free all OPPs associated with the device
1726 * @dev: device pointer used to lookup OPP table.
1728 * Free both OPPs created using static entries present in DT and the
1729 * dynamically added entries.
1731 * Locking: The internal opp_table and opp structures are RCU protected.
1732 * Hence this function indirectly uses RCU updater strategy with mutex locks
1733 * to keep the integrity of the internal data structures. Callers should ensure
1734 * that this function is *NOT* called under RCU protection or in contexts where
1735 * mutex cannot be locked.
1737 void dev_pm_opp_remove_table(struct device *dev)
1739 _dev_pm_opp_remove_table(dev, true);
1741 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);