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 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/device.h>
19 #include <linux/export.h>
24 * The root of the list of all devices. All device_opp structures branch off
25 * from here, with each device_opp containing the list of opp it supports in
26 * various states of availability.
28 static LIST_HEAD(dev_opp_list);
29 /* Lock to allow exclusive modification to the device and opp lists */
30 static DEFINE_MUTEX(dev_opp_list_lock);
32 #define opp_rcu_lockdep_assert() \
34 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
35 !lockdep_is_held(&dev_opp_list_lock), \
36 "Missing rcu_read_lock() or " \
37 "dev_opp_list_lock protection"); \
40 static struct device_list_opp *_find_list_dev(const struct device *dev,
41 struct device_opp *dev_opp)
43 struct device_list_opp *list_dev;
45 list_for_each_entry(list_dev, &dev_opp->dev_list, node)
46 if (list_dev->dev == dev)
52 static struct device_opp *_managed_opp(const struct device_node *np)
54 struct device_opp *dev_opp;
56 list_for_each_entry_rcu(dev_opp, &dev_opp_list, node) {
57 if (dev_opp->np == np) {
59 * Multiple devices can point to the same OPP table and
60 * so will have same node-pointer, np.
62 * But the OPPs will be considered as shared only if the
63 * OPP table contains a "opp-shared" property.
65 return dev_opp->shared_opp ? dev_opp : NULL;
73 * _find_device_opp() - find device_opp struct using device pointer
74 * @dev: device pointer used to lookup device OPPs
76 * Search list of device OPPs for one containing matching device. Does a RCU
77 * reader operation to grab the pointer needed.
79 * Return: pointer to 'struct device_opp' if found, otherwise -ENODEV or
80 * -EINVAL based on type of error.
82 * Locking: This function must be called under rcu_read_lock(). device_opp
83 * is a RCU protected pointer. This means that device_opp is valid as long
84 * as we are under RCU lock.
86 struct device_opp *_find_device_opp(struct device *dev)
88 struct device_opp *dev_opp;
90 if (IS_ERR_OR_NULL(dev)) {
91 pr_err("%s: Invalid parameters\n", __func__);
92 return ERR_PTR(-EINVAL);
95 list_for_each_entry_rcu(dev_opp, &dev_opp_list, node)
96 if (_find_list_dev(dev, dev_opp))
99 return ERR_PTR(-ENODEV);
103 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
104 * @opp: opp for which voltage has to be returned for
106 * Return: voltage in micro volt corresponding to the opp, else
109 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
110 * protected pointer. This means that opp which could have been fetched by
111 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
112 * under RCU lock. The pointer returned by the opp_find_freq family must be
113 * used in the same section as the usage of this function with the pointer
114 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
117 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
119 struct dev_pm_opp *tmp_opp;
122 opp_rcu_lockdep_assert();
124 tmp_opp = rcu_dereference(opp);
125 if (IS_ERR_OR_NULL(tmp_opp))
126 pr_err("%s: Invalid parameters\n", __func__);
132 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
135 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
136 * @opp: opp for which frequency has to be returned for
138 * Return: frequency in hertz corresponding to the opp, else
141 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
142 * protected pointer. This means that opp which could have been fetched by
143 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
144 * under RCU lock. The pointer returned by the opp_find_freq family must be
145 * used in the same section as the usage of this function with the pointer
146 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
149 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
151 struct dev_pm_opp *tmp_opp;
154 opp_rcu_lockdep_assert();
156 tmp_opp = rcu_dereference(opp);
157 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available)
158 pr_err("%s: Invalid parameters\n", __func__);
164 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
167 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
168 * @opp: opp for which turbo mode is being verified
170 * Turbo OPPs are not for normal use, and can be enabled (under certain
171 * conditions) for short duration of times to finish high throughput work
172 * quickly. Running on them for longer times may overheat the chip.
174 * Return: true if opp is turbo opp, else false.
176 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
177 * protected pointer. This means that opp which could have been fetched by
178 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
179 * under RCU lock. The pointer returned by the opp_find_freq family must be
180 * used in the same section as the usage of this function with the pointer
181 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
184 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
186 struct dev_pm_opp *tmp_opp;
188 opp_rcu_lockdep_assert();
190 tmp_opp = rcu_dereference(opp);
191 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available) {
192 pr_err("%s: Invalid parameters\n", __func__);
196 return tmp_opp->turbo;
198 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
201 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
202 * @dev: device for which we do this operation
204 * Return: This function returns the max clock latency in nanoseconds.
206 * Locking: This function takes rcu_read_lock().
208 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
210 struct device_opp *dev_opp;
211 unsigned long clock_latency_ns;
215 dev_opp = _find_device_opp(dev);
217 clock_latency_ns = 0;
219 clock_latency_ns = dev_opp->clock_latency_ns_max;
222 return clock_latency_ns;
224 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
227 * dev_pm_opp_get_suspend_opp() - Get suspend opp
228 * @dev: device for which we do this operation
230 * Return: This function returns pointer to the suspend opp if it is
231 * defined and available, otherwise it returns NULL.
233 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
234 * protected pointer. The reason for the same is that the opp pointer which is
235 * returned will remain valid for use with opp_get_{voltage, freq} only while
236 * under the locked area. The pointer returned must be used prior to unlocking
237 * with rcu_read_unlock() to maintain the integrity of the pointer.
239 struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev)
241 struct device_opp *dev_opp;
243 opp_rcu_lockdep_assert();
245 dev_opp = _find_device_opp(dev);
246 if (IS_ERR(dev_opp) || !dev_opp->suspend_opp ||
247 !dev_opp->suspend_opp->available)
250 return dev_opp->suspend_opp;
252 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp);
255 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
256 * @dev: device for which we do this operation
258 * Return: This function returns the number of available opps if there are any,
259 * else returns 0 if none or the corresponding error value.
261 * Locking: This function takes rcu_read_lock().
263 int dev_pm_opp_get_opp_count(struct device *dev)
265 struct device_opp *dev_opp;
266 struct dev_pm_opp *temp_opp;
271 dev_opp = _find_device_opp(dev);
272 if (IS_ERR(dev_opp)) {
273 count = PTR_ERR(dev_opp);
274 dev_err(dev, "%s: device OPP not found (%d)\n",
279 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
280 if (temp_opp->available)
288 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
291 * dev_pm_opp_find_freq_exact() - search for an exact frequency
292 * @dev: device for which we do this operation
293 * @freq: frequency to search for
294 * @available: true/false - match for available opp
296 * Return: Searches for exact match in the opp list and returns pointer to the
297 * matching opp if found, else returns ERR_PTR in case of error and should
298 * be handled using IS_ERR. Error return values can be:
299 * EINVAL: for bad pointer
300 * ERANGE: no match found for search
301 * ENODEV: if device not found in list of registered devices
303 * Note: available is a modifier for the search. if available=true, then the
304 * match is for exact matching frequency and is available in the stored OPP
305 * table. if false, the match is for exact frequency which is not available.
307 * This provides a mechanism to enable an opp which is not available currently
308 * or the opposite as well.
310 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
311 * protected pointer. The reason for the same is that the opp pointer which is
312 * returned will remain valid for use with opp_get_{voltage, freq} only while
313 * under the locked area. The pointer returned must be used prior to unlocking
314 * with rcu_read_unlock() to maintain the integrity of the pointer.
316 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
320 struct device_opp *dev_opp;
321 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
323 opp_rcu_lockdep_assert();
325 dev_opp = _find_device_opp(dev);
326 if (IS_ERR(dev_opp)) {
327 int r = PTR_ERR(dev_opp);
328 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
332 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
333 if (temp_opp->available == available &&
334 temp_opp->rate == freq) {
342 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
345 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
346 * @dev: device for which we do this operation
347 * @freq: Start frequency
349 * Search for the matching ceil *available* OPP from a starting freq
352 * Return: matching *opp and refreshes *freq accordingly, else returns
353 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
355 * EINVAL: for bad pointer
356 * ERANGE: no match found for search
357 * ENODEV: if device not found in list of registered devices
359 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
360 * protected pointer. The reason for the same is that the opp pointer which is
361 * returned will remain valid for use with opp_get_{voltage, freq} only while
362 * under the locked area. The pointer returned must be used prior to unlocking
363 * with rcu_read_unlock() to maintain the integrity of the pointer.
365 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
368 struct device_opp *dev_opp;
369 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
371 opp_rcu_lockdep_assert();
374 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
375 return ERR_PTR(-EINVAL);
378 dev_opp = _find_device_opp(dev);
380 return ERR_CAST(dev_opp);
382 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
383 if (temp_opp->available && temp_opp->rate >= *freq) {
392 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
395 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
396 * @dev: device for which we do this operation
397 * @freq: Start frequency
399 * Search for the matching floor *available* OPP from a starting freq
402 * Return: matching *opp and refreshes *freq accordingly, else returns
403 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
405 * EINVAL: for bad pointer
406 * ERANGE: no match found for search
407 * ENODEV: if device not found in list of registered devices
409 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
410 * protected pointer. The reason for the same is that the opp pointer which is
411 * returned will remain valid for use with opp_get_{voltage, freq} only while
412 * under the locked area. The pointer returned must be used prior to unlocking
413 * with rcu_read_unlock() to maintain the integrity of the pointer.
415 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
418 struct device_opp *dev_opp;
419 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
421 opp_rcu_lockdep_assert();
424 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
425 return ERR_PTR(-EINVAL);
428 dev_opp = _find_device_opp(dev);
430 return ERR_CAST(dev_opp);
432 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
433 if (temp_opp->available) {
434 /* go to the next node, before choosing prev */
435 if (temp_opp->rate > *freq)
446 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
448 /* List-dev Helpers */
449 static void _kfree_list_dev_rcu(struct rcu_head *head)
451 struct device_list_opp *list_dev;
453 list_dev = container_of(head, struct device_list_opp, rcu_head);
454 kfree_rcu(list_dev, rcu_head);
457 static void _remove_list_dev(struct device_list_opp *list_dev,
458 struct device_opp *dev_opp)
460 list_del(&list_dev->node);
461 call_srcu(&dev_opp->srcu_head.srcu, &list_dev->rcu_head,
462 _kfree_list_dev_rcu);
465 struct device_list_opp *_add_list_dev(const struct device *dev,
466 struct device_opp *dev_opp)
468 struct device_list_opp *list_dev;
470 list_dev = kzalloc(sizeof(*list_dev), GFP_KERNEL);
474 /* Initialize list-dev */
476 list_add_rcu(&list_dev->node, &dev_opp->dev_list);
482 * _add_device_opp() - Find device OPP table or allocate a new one
483 * @dev: device for which we do this operation
485 * It tries to find an existing table first, if it couldn't find one, it
486 * allocates a new OPP table and returns that.
488 * Return: valid device_opp pointer if success, else NULL.
490 static struct device_opp *_add_device_opp(struct device *dev)
492 struct device_opp *dev_opp;
493 struct device_list_opp *list_dev;
495 /* Check for existing list for 'dev' first */
496 dev_opp = _find_device_opp(dev);
497 if (!IS_ERR(dev_opp))
501 * Allocate a new device OPP table. In the infrequent case where a new
502 * device is needed to be added, we pay this penalty.
504 dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);
508 INIT_LIST_HEAD(&dev_opp->dev_list);
510 list_dev = _add_list_dev(dev, dev_opp);
516 srcu_init_notifier_head(&dev_opp->srcu_head);
517 INIT_LIST_HEAD(&dev_opp->opp_list);
519 /* Secure the device list modification */
520 list_add_rcu(&dev_opp->node, &dev_opp_list);
525 * _kfree_device_rcu() - Free device_opp RCU handler
528 static void _kfree_device_rcu(struct rcu_head *head)
530 struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
532 kfree_rcu(device_opp, rcu_head);
536 * _remove_device_opp() - Removes a device OPP table
537 * @dev_opp: device OPP table to be removed.
539 * Removes/frees device OPP table it it doesn't contain any OPPs.
541 static void _remove_device_opp(struct device_opp *dev_opp)
543 struct device_list_opp *list_dev;
545 if (!list_empty(&dev_opp->opp_list))
548 list_dev = list_first_entry(&dev_opp->dev_list, struct device_list_opp,
551 _remove_list_dev(list_dev, dev_opp);
553 /* dev_list must be empty now */
554 WARN_ON(!list_empty(&dev_opp->dev_list));
556 list_del_rcu(&dev_opp->node);
557 call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
562 * _kfree_opp_rcu() - Free OPP RCU handler
565 static void _kfree_opp_rcu(struct rcu_head *head)
567 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
569 kfree_rcu(opp, rcu_head);
573 * _opp_remove() - Remove an OPP from a table definition
574 * @dev_opp: points back to the device_opp struct this opp belongs to
575 * @opp: pointer to the OPP to remove
576 * @notify: OPP_EVENT_REMOVE notification should be sent or not
578 * This function removes an opp definition from the opp list.
580 * Locking: The internal device_opp and opp structures are RCU protected.
581 * It is assumed that the caller holds required mutex for an RCU updater
584 static void _opp_remove(struct device_opp *dev_opp,
585 struct dev_pm_opp *opp, bool notify)
588 * Notify the changes in the availability of the operable
589 * frequency/voltage list.
592 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
593 list_del_rcu(&opp->node);
594 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
596 _remove_device_opp(dev_opp);
600 * dev_pm_opp_remove() - Remove an OPP from OPP list
601 * @dev: device for which we do this operation
602 * @freq: OPP to remove with matching 'freq'
604 * This function removes an opp from the opp list.
606 * Locking: The internal device_opp and opp structures are RCU protected.
607 * Hence this function internally uses RCU updater strategy with mutex locks
608 * to keep the integrity of the internal data structures. Callers should ensure
609 * that this function is *NOT* called under RCU protection or in contexts where
610 * mutex cannot be locked.
612 void dev_pm_opp_remove(struct device *dev, unsigned long freq)
614 struct dev_pm_opp *opp;
615 struct device_opp *dev_opp;
618 /* Hold our list modification lock here */
619 mutex_lock(&dev_opp_list_lock);
621 dev_opp = _find_device_opp(dev);
625 list_for_each_entry(opp, &dev_opp->opp_list, node) {
626 if (opp->rate == freq) {
633 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
638 _opp_remove(dev_opp, opp, true);
640 mutex_unlock(&dev_opp_list_lock);
642 EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
644 static struct dev_pm_opp *_allocate_opp(struct device *dev,
645 struct device_opp **dev_opp)
647 struct dev_pm_opp *opp;
649 /* allocate new OPP node */
650 opp = kzalloc(sizeof(*opp), GFP_KERNEL);
654 INIT_LIST_HEAD(&opp->node);
656 *dev_opp = _add_device_opp(dev);
665 static int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
666 struct device_opp *dev_opp)
668 struct dev_pm_opp *opp;
669 struct list_head *head = &dev_opp->opp_list;
672 * Insert new OPP in order of increasing frequency and discard if
675 * Need to use &dev_opp->opp_list in the condition part of the 'for'
676 * loop, don't replace it with head otherwise it will become an infinite
679 list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
680 if (new_opp->rate > opp->rate) {
685 if (new_opp->rate < opp->rate)
689 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
690 __func__, opp->rate, opp->u_volt, opp->available,
691 new_opp->rate, new_opp->u_volt, new_opp->available);
693 return opp->available && new_opp->u_volt == opp->u_volt ?
697 new_opp->dev_opp = dev_opp;
698 list_add_rcu(&new_opp->node, head);
704 * _opp_add_dynamic() - Allocate a dynamic OPP.
705 * @dev: device for which we do this operation
706 * @freq: Frequency in Hz for this OPP
707 * @u_volt: Voltage in uVolts for this OPP
708 * @dynamic: Dynamically added OPPs.
710 * This function adds an opp definition to the opp list and returns status.
711 * The opp is made available by default and it can be controlled using
712 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
714 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
715 * and freed by dev_pm_opp_of_remove_table.
717 * Locking: The internal device_opp and opp structures are RCU protected.
718 * Hence this function internally uses RCU updater strategy with mutex locks
719 * to keep the integrity of the internal data structures. Callers should ensure
720 * that this function is *NOT* called under RCU protection or in contexts where
721 * mutex cannot be locked.
725 * Duplicate OPPs (both freq and volt are same) and opp->available
726 * -EEXIST Freq are same and volt are different OR
727 * Duplicate OPPs (both freq and volt are same) and !opp->available
728 * -ENOMEM Memory allocation failure
730 static int _opp_add_dynamic(struct device *dev, unsigned long freq,
731 long u_volt, bool dynamic)
733 struct device_opp *dev_opp;
734 struct dev_pm_opp *new_opp;
737 /* Hold our list modification lock here */
738 mutex_lock(&dev_opp_list_lock);
740 new_opp = _allocate_opp(dev, &dev_opp);
746 /* populate the opp table */
747 new_opp->rate = freq;
748 new_opp->u_volt = u_volt;
749 new_opp->available = true;
750 new_opp->dynamic = dynamic;
752 ret = _opp_add(dev, new_opp, dev_opp);
756 mutex_unlock(&dev_opp_list_lock);
759 * Notify the changes in the availability of the operable
760 * frequency/voltage list.
762 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
766 _opp_remove(dev_opp, new_opp, false);
768 mutex_unlock(&dev_opp_list_lock);
772 /* TODO: Support multiple regulators */
773 static int opp_get_microvolt(struct dev_pm_opp *opp, struct device *dev)
775 u32 microvolt[3] = {0};
778 /* Missing property isn't a problem, but an invalid entry is */
779 if (!of_find_property(opp->np, "opp-microvolt", NULL))
782 count = of_property_count_u32_elems(opp->np, "opp-microvolt");
784 dev_err(dev, "%s: Invalid opp-microvolt property (%d)\n",
789 /* There can be one or three elements here */
790 if (count != 1 && count != 3) {
791 dev_err(dev, "%s: Invalid number of elements in opp-microvolt property (%d)\n",
796 ret = of_property_read_u32_array(opp->np, "opp-microvolt", microvolt,
799 dev_err(dev, "%s: error parsing opp-microvolt: %d\n", __func__,
804 opp->u_volt = microvolt[0];
805 opp->u_volt_min = microvolt[1];
806 opp->u_volt_max = microvolt[2];
812 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
813 * @dev: device for which we do this operation
816 * This function adds an opp definition to the opp list and returns status. The
817 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
818 * removed by dev_pm_opp_remove.
820 * Locking: The internal device_opp and opp structures are RCU protected.
821 * Hence this function internally uses RCU updater strategy with mutex locks
822 * to keep the integrity of the internal data structures. Callers should ensure
823 * that this function is *NOT* called under RCU protection or in contexts where
824 * mutex cannot be locked.
828 * Duplicate OPPs (both freq and volt are same) and opp->available
829 * -EEXIST Freq are same and volt are different OR
830 * Duplicate OPPs (both freq and volt are same) and !opp->available
831 * -ENOMEM Memory allocation failure
832 * -EINVAL Failed parsing the OPP node
834 static int _opp_add_static_v2(struct device *dev, struct device_node *np)
836 struct device_opp *dev_opp;
837 struct dev_pm_opp *new_opp;
842 /* Hold our list modification lock here */
843 mutex_lock(&dev_opp_list_lock);
845 new_opp = _allocate_opp(dev, &dev_opp);
851 ret = of_property_read_u64(np, "opp-hz", &rate);
853 dev_err(dev, "%s: opp-hz not found\n", __func__);
858 * Rate is defined as an unsigned long in clk API, and so casting
859 * explicitly to its type. Must be fixed once rate is 64 bit
860 * guaranteed in clk API.
862 new_opp->rate = (unsigned long)rate;
863 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
866 new_opp->dynamic = false;
867 new_opp->available = true;
869 if (!of_property_read_u32(np, "clock-latency-ns", &val))
870 new_opp->clock_latency_ns = val;
872 ret = opp_get_microvolt(new_opp, dev);
876 if (!of_property_read_u32(new_opp->np, "opp-microamp", &val))
877 new_opp->u_amp = val;
879 ret = _opp_add(dev, new_opp, dev_opp);
883 /* OPP to select on device suspend */
884 if (of_property_read_bool(np, "opp-suspend")) {
885 if (dev_opp->suspend_opp)
886 dev_warn(dev, "%s: Multiple suspend OPPs found (%lu %lu)\n",
887 __func__, dev_opp->suspend_opp->rate,
890 dev_opp->suspend_opp = new_opp;
893 if (new_opp->clock_latency_ns > dev_opp->clock_latency_ns_max)
894 dev_opp->clock_latency_ns_max = new_opp->clock_latency_ns;
896 mutex_unlock(&dev_opp_list_lock);
898 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n",
899 __func__, new_opp->turbo, new_opp->rate, new_opp->u_volt,
900 new_opp->u_volt_min, new_opp->u_volt_max,
901 new_opp->clock_latency_ns);
904 * Notify the changes in the availability of the operable
905 * frequency/voltage list.
907 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
911 _opp_remove(dev_opp, new_opp, false);
913 mutex_unlock(&dev_opp_list_lock);
918 * dev_pm_opp_add() - Add an OPP table from a table definitions
919 * @dev: device for which we do this operation
920 * @freq: Frequency in Hz for this OPP
921 * @u_volt: Voltage in uVolts for this OPP
923 * This function adds an opp definition to the opp list and returns status.
924 * The opp is made available by default and it can be controlled using
925 * dev_pm_opp_enable/disable functions.
927 * Locking: The internal device_opp and opp structures are RCU protected.
928 * Hence this function internally uses RCU updater strategy with mutex locks
929 * to keep the integrity of the internal data structures. Callers should ensure
930 * that this function is *NOT* called under RCU protection or in contexts where
931 * mutex cannot be locked.
935 * Duplicate OPPs (both freq and volt are same) and opp->available
936 * -EEXIST Freq are same and volt are different OR
937 * Duplicate OPPs (both freq and volt are same) and !opp->available
938 * -ENOMEM Memory allocation failure
940 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
942 return _opp_add_dynamic(dev, freq, u_volt, true);
944 EXPORT_SYMBOL_GPL(dev_pm_opp_add);
947 * _opp_set_availability() - helper to set the availability of an opp
948 * @dev: device for which we do this operation
949 * @freq: OPP frequency to modify availability
950 * @availability_req: availability status requested for this opp
952 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
953 * share a common logic which is isolated here.
955 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
956 * copy operation, returns 0 if no modification was done OR modification was
959 * Locking: The internal device_opp and opp structures are RCU protected.
960 * Hence this function internally uses RCU updater strategy with mutex locks to
961 * keep the integrity of the internal data structures. Callers should ensure
962 * that this function is *NOT* called under RCU protection or in contexts where
963 * mutex locking or synchronize_rcu() blocking calls cannot be used.
965 static int _opp_set_availability(struct device *dev, unsigned long freq,
966 bool availability_req)
968 struct device_opp *dev_opp;
969 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
972 /* keep the node allocated */
973 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
977 mutex_lock(&dev_opp_list_lock);
979 /* Find the device_opp */
980 dev_opp = _find_device_opp(dev);
981 if (IS_ERR(dev_opp)) {
982 r = PTR_ERR(dev_opp);
983 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
987 /* Do we have the frequency? */
988 list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
989 if (tmp_opp->rate == freq) {
999 /* Is update really needed? */
1000 if (opp->available == availability_req)
1002 /* copy the old data over */
1005 /* plug in new node */
1006 new_opp->available = availability_req;
1008 list_replace_rcu(&opp->node, &new_opp->node);
1009 mutex_unlock(&dev_opp_list_lock);
1010 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
1012 /* Notify the change of the OPP availability */
1013 if (availability_req)
1014 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
1017 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
1023 mutex_unlock(&dev_opp_list_lock);
1029 * dev_pm_opp_enable() - Enable a specific OPP
1030 * @dev: device for which we do this operation
1031 * @freq: OPP frequency to enable
1033 * Enables a provided opp. If the operation is valid, this returns 0, else the
1034 * corresponding error value. It is meant to be used for users an OPP available
1035 * after being temporarily made unavailable with dev_pm_opp_disable.
1037 * Locking: The internal device_opp and opp structures are RCU protected.
1038 * Hence this function indirectly uses RCU and mutex locks to keep the
1039 * integrity of the internal data structures. Callers should ensure that
1040 * this function is *NOT* called under RCU protection or in contexts where
1041 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1043 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1044 * copy operation, returns 0 if no modification was done OR modification was
1047 int dev_pm_opp_enable(struct device *dev, unsigned long freq)
1049 return _opp_set_availability(dev, freq, true);
1051 EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
1054 * dev_pm_opp_disable() - Disable a specific OPP
1055 * @dev: device for which we do this operation
1056 * @freq: OPP frequency to disable
1058 * Disables a provided opp. If the operation is valid, this returns
1059 * 0, else the corresponding error value. It is meant to be a temporary
1060 * control by users to make this OPP not available until the circumstances are
1061 * right to make it available again (with a call to dev_pm_opp_enable).
1063 * Locking: The internal device_opp and opp structures are RCU protected.
1064 * Hence this function indirectly uses RCU and mutex locks to keep the
1065 * integrity of the internal data structures. Callers should ensure that
1066 * this function is *NOT* called under RCU protection or in contexts where
1067 * mutex locking or synchronize_rcu() blocking calls cannot be used.
1069 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
1070 * copy operation, returns 0 if no modification was done OR modification was
1073 int dev_pm_opp_disable(struct device *dev, unsigned long freq)
1075 return _opp_set_availability(dev, freq, false);
1077 EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
1080 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
1081 * @dev: device pointer used to lookup device OPPs.
1083 * Return: pointer to notifier head if found, otherwise -ENODEV or
1084 * -EINVAL based on type of error casted as pointer. value must be checked
1085 * with IS_ERR to determine valid pointer or error result.
1087 * Locking: This function must be called under rcu_read_lock(). dev_opp is a RCU
1088 * protected pointer. The reason for the same is that the opp pointer which is
1089 * returned will remain valid for use with opp_get_{voltage, freq} only while
1090 * under the locked area. The pointer returned must be used prior to unlocking
1091 * with rcu_read_unlock() to maintain the integrity of the pointer.
1093 struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
1095 struct device_opp *dev_opp = _find_device_opp(dev);
1097 if (IS_ERR(dev_opp))
1098 return ERR_CAST(dev_opp); /* matching type */
1100 return &dev_opp->srcu_head;
1102 EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
1106 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT
1108 * @dev: device pointer used to lookup device OPPs.
1110 * Free OPPs created using static entries present in DT.
1112 * Locking: The internal device_opp and opp structures are RCU protected.
1113 * Hence this function indirectly uses RCU updater strategy with mutex locks
1114 * to keep the integrity of the internal data structures. Callers should ensure
1115 * that this function is *NOT* called under RCU protection or in contexts where
1116 * mutex cannot be locked.
1118 void dev_pm_opp_of_remove_table(struct device *dev)
1120 struct device_opp *dev_opp;
1121 struct dev_pm_opp *opp, *tmp;
1123 /* Hold our list modification lock here */
1124 mutex_lock(&dev_opp_list_lock);
1126 /* Check for existing list for 'dev' */
1127 dev_opp = _find_device_opp(dev);
1128 if (IS_ERR(dev_opp)) {
1129 int error = PTR_ERR(dev_opp);
1131 if (error != -ENODEV)
1132 WARN(1, "%s: dev_opp: %d\n",
1133 IS_ERR_OR_NULL(dev) ?
1134 "Invalid device" : dev_name(dev),
1139 /* Find if dev_opp manages a single device */
1140 if (list_is_singular(&dev_opp->dev_list)) {
1141 /* Free static OPPs */
1142 list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
1144 _opp_remove(dev_opp, opp, true);
1147 _remove_list_dev(_find_list_dev(dev, dev_opp), dev_opp);
1151 mutex_unlock(&dev_opp_list_lock);
1153 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table);
1155 /* Returns opp descriptor node for a device, caller must do of_node_put() */
1156 struct device_node *_of_get_opp_desc_node(struct device *dev)
1159 * TODO: Support for multiple OPP tables.
1161 * There should be only ONE phandle present in "operating-points-v2"
1165 return of_parse_phandle(dev->of_node, "operating-points-v2", 0);
1168 /* Initializes OPP tables based on new bindings */
1169 static int _of_add_opp_table_v2(struct device *dev, struct device_node *opp_np)
1171 struct device_node *np;
1172 struct device_opp *dev_opp;
1173 int ret = 0, count = 0;
1175 dev_opp = _managed_opp(opp_np);
1177 /* OPPs are already managed */
1178 if (!_add_list_dev(dev, dev_opp))
1183 /* We have opp-list node now, iterate over it and add OPPs */
1184 for_each_available_child_of_node(opp_np, np) {
1187 ret = _opp_add_static_v2(dev, np);
1189 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
1195 /* There should be one of more OPP defined */
1196 if (WARN_ON(!count))
1199 dev_opp = _find_device_opp(dev);
1200 if (WARN_ON(IS_ERR(dev_opp))) {
1201 ret = PTR_ERR(dev_opp);
1205 dev_opp->np = opp_np;
1206 dev_opp->shared_opp = of_property_read_bool(opp_np, "opp-shared");
1211 dev_pm_opp_of_remove_table(dev);
1216 /* Initializes OPP tables based on old-deprecated bindings */
1217 static int _of_add_opp_table_v1(struct device *dev)
1219 const struct property *prop;
1223 prop = of_find_property(dev->of_node, "operating-points", NULL);
1230 * Each OPP is a set of tuples consisting of frequency and
1231 * voltage like <freq-kHz vol-uV>.
1233 nr = prop->length / sizeof(u32);
1235 dev_err(dev, "%s: Invalid OPP list\n", __func__);
1241 unsigned long freq = be32_to_cpup(val++) * 1000;
1242 unsigned long volt = be32_to_cpup(val++);
1244 if (_opp_add_dynamic(dev, freq, volt, false))
1245 dev_warn(dev, "%s: Failed to add OPP %ld\n",
1254 * dev_pm_opp_of_add_table() - Initialize opp table from device tree
1255 * @dev: device pointer used to lookup device OPPs.
1257 * Register the initial OPP table with the OPP library for given device.
1259 * Locking: The internal device_opp and opp structures are RCU protected.
1260 * Hence this function indirectly uses RCU updater strategy with mutex locks
1261 * to keep the integrity of the internal data structures. Callers should ensure
1262 * that this function is *NOT* called under RCU protection or in contexts where
1263 * mutex cannot be locked.
1267 * Duplicate OPPs (both freq and volt are same) and opp->available
1268 * -EEXIST Freq are same and volt are different OR
1269 * Duplicate OPPs (both freq and volt are same) and !opp->available
1270 * -ENOMEM Memory allocation failure
1271 * -ENODEV when 'operating-points' property is not found or is invalid data
1273 * -ENODATA when empty 'operating-points' property is found
1274 * -EINVAL when invalid entries are found in opp-v2 table
1276 int dev_pm_opp_of_add_table(struct device *dev)
1278 struct device_node *opp_np;
1282 * OPPs have two version of bindings now. The older one is deprecated,
1283 * try for the new binding first.
1285 opp_np = _of_get_opp_desc_node(dev);
1288 * Try old-deprecated bindings for backward compatibility with
1291 return _of_add_opp_table_v1(dev);
1294 ret = _of_add_opp_table_v2(dev, opp_np);
1295 of_node_put(opp_np);
1299 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table);