blob: 0e0eff4d9299cde699363c306316a724aa9a0867 [file] [log] [blame]
Nishanth Menone1f60b22010-10-13 00:13:10 +02001/*
2 * Generic OPP Interface
3 *
4 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
5 * Nishanth Menon
6 * Romit Dasgupta
7 * Kevin Hilman
8 *
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.
12 */
13
14#include <linux/kernel.h>
15#include <linux/errno.h>
16#include <linux/err.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020017#include <linux/slab.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050018#include <linux/device.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020019#include <linux/list.h>
20#include <linux/rculist.h>
21#include <linux/rcupdate.h>
Nishanth Menone4db1c72013-09-19 16:03:52 -050022#include <linux/pm_opp.h>
Shawn Guob496dfb2012-09-05 01:09:12 +020023#include <linux/of.h>
Liam Girdwood80126ce2012-10-23 01:27:44 +020024#include <linux/export.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020025
26/*
27 * Internal data structure organization with the OPP layer library is as
28 * follows:
29 * dev_opp_list (root)
30 * |- device 1 (represents voltage domain 1)
31 * | |- opp 1 (availability, freq, voltage)
32 * | |- opp 2 ..
33 * ... ...
34 * | `- opp n ..
35 * |- device 2 (represents the next voltage domain)
36 * ...
37 * `- device m (represents mth voltage domain)
38 * device 1, 2.. are represented by dev_opp structure while each opp
39 * is represented by the opp structure.
40 */
41
42/**
Nishanth Menon47d43ba2013-09-19 16:03:51 -050043 * struct dev_pm_opp - Generic OPP description structure
Nishanth Menone1f60b22010-10-13 00:13:10 +020044 * @node: opp list node. The nodes are maintained throughout the lifetime
45 * of boot. It is expected only an optimal set of OPPs are
46 * added to the library by the SoC framework.
47 * RCU usage: opp list is traversed with RCU locks. node
48 * modification is possible realtime, hence the modifications
49 * are protected by the dev_opp_list_lock for integrity.
50 * IMPORTANT: the opp nodes should be maintained in increasing
51 * order.
Viresh Kumar383934092014-11-25 16:04:18 +053052 * @dynamic: not-created from static DT entries.
Nishanth Menone1f60b22010-10-13 00:13:10 +020053 * @available: true/false - marks if this OPP as available or not
Viresh Kumar27465902015-07-29 16:23:02 +053054 * @turbo: true if turbo (boost) OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +020055 * @rate: Frequency in hertz
Viresh Kumar27465902015-07-29 16:23:02 +053056 * @u_volt: Target voltage in microvolts corresponding to this OPP
57 * @u_volt_min: Minimum voltage in microvolts corresponding to this OPP
58 * @u_volt_max: Maximum voltage in microvolts corresponding to this OPP
59 * @u_amp: Maximum current drawn by the device in microamperes
Nishanth Menone1f60b22010-10-13 00:13:10 +020060 * @dev_opp: points back to the device_opp struct this opp belongs to
Viresh Kumarcd1a0682014-11-25 16:04:16 +053061 * @rcu_head: RCU callback head used for deferred freeing
Viresh Kumar27465902015-07-29 16:23:02 +053062 * @np: OPP's device node.
Nishanth Menone1f60b22010-10-13 00:13:10 +020063 *
64 * This structure stores the OPP information for a given device.
65 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -050066struct dev_pm_opp {
Nishanth Menone1f60b22010-10-13 00:13:10 +020067 struct list_head node;
68
69 bool available;
Viresh Kumar383934092014-11-25 16:04:18 +053070 bool dynamic;
Viresh Kumar27465902015-07-29 16:23:02 +053071 bool turbo;
Nishanth Menone1f60b22010-10-13 00:13:10 +020072 unsigned long rate;
Viresh Kumar27465902015-07-29 16:23:02 +053073
Nishanth Menone1f60b22010-10-13 00:13:10 +020074 unsigned long u_volt;
Viresh Kumar27465902015-07-29 16:23:02 +053075 unsigned long u_volt_min;
76 unsigned long u_volt_max;
77 unsigned long u_amp;
Nishanth Menone1f60b22010-10-13 00:13:10 +020078
79 struct device_opp *dev_opp;
Viresh Kumarcd1a0682014-11-25 16:04:16 +053080 struct rcu_head rcu_head;
Viresh Kumar27465902015-07-29 16:23:02 +053081
82 struct device_node *np;
Nishanth Menone1f60b22010-10-13 00:13:10 +020083};
84
85/**
86 * struct device_opp - Device opp structure
87 * @node: list node - contains the devices with OPPs that
88 * have been registered. Nodes once added are not modified in this
89 * list.
90 * RCU usage: nodes are not modified in the list of device_opp,
91 * however addition is possible and is secured by dev_opp_list_lock
92 * @dev: device pointer
Viresh Kumarcd1a0682014-11-25 16:04:16 +053093 * @srcu_head: notifier head to notify the OPP availability changes.
Viresh Kumar129eec52014-11-27 08:54:06 +053094 * @rcu_head: RCU callback head used for deferred freeing
Nishanth Menone1f60b22010-10-13 00:13:10 +020095 * @opp_list: list of opps
96 *
97 * This is an internal data structure maintaining the link to opps attached to
98 * a device. This structure is not meant to be shared to users as it is
Viresh Kumar1c6a6622014-12-10 09:45:31 +053099 * meant for book keeping and private to OPP library.
100 *
101 * Because the opp structures can be used from both rcu and srcu readers, we
102 * need to wait for the grace period of both of them before freeing any
103 * resources. And so we have used kfree_rcu() from within call_srcu() handlers.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200104 */
105struct device_opp {
106 struct list_head node;
107
108 struct device *dev;
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530109 struct srcu_notifier_head srcu_head;
Viresh Kumar129eec52014-11-27 08:54:06 +0530110 struct rcu_head rcu_head;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200111 struct list_head opp_list;
112};
113
114/*
115 * The root of the list of all devices. All device_opp structures branch off
116 * from here, with each device_opp containing the list of opp it supports in
117 * various states of availability.
118 */
119static LIST_HEAD(dev_opp_list);
120/* Lock to allow exclusive modification to the device and opp lists */
121static DEFINE_MUTEX(dev_opp_list_lock);
122
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800123#define opp_rcu_lockdep_assert() \
124do { \
125 rcu_lockdep_assert(rcu_read_lock_held() || \
126 lockdep_is_held(&dev_opp_list_lock), \
127 "Missing rcu_read_lock() or " \
128 "dev_opp_list_lock protection"); \
129} while (0)
130
Nishanth Menone1f60b22010-10-13 00:13:10 +0200131/**
Nishanth Menon327854c2014-12-24 11:22:56 -0600132 * _find_device_opp() - find device_opp struct using device pointer
Nishanth Menone1f60b22010-10-13 00:13:10 +0200133 * @dev: device pointer used to lookup device OPPs
134 *
135 * Search list of device OPPs for one containing matching device. Does a RCU
136 * reader operation to grab the pointer needed.
137 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600138 * Return: pointer to 'struct device_opp' if found, otherwise -ENODEV or
Nishanth Menone1f60b22010-10-13 00:13:10 +0200139 * -EINVAL based on type of error.
140 *
141 * Locking: This function must be called under rcu_read_lock(). device_opp
142 * is a RCU protected pointer. This means that device_opp is valid as long
143 * as we are under RCU lock.
144 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600145static struct device_opp *_find_device_opp(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200146{
147 struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV);
148
149 if (unlikely(IS_ERR_OR_NULL(dev))) {
150 pr_err("%s: Invalid parameters\n", __func__);
151 return ERR_PTR(-EINVAL);
152 }
153
154 list_for_each_entry_rcu(tmp_dev_opp, &dev_opp_list, node) {
155 if (tmp_dev_opp->dev == dev) {
156 dev_opp = tmp_dev_opp;
157 break;
158 }
159 }
160
161 return dev_opp;
162}
163
164/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500165 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200166 * @opp: opp for which voltage has to be returned for
167 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600168 * Return: voltage in micro volt corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +0200169 * return 0
170 *
171 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
172 * protected pointer. This means that opp which could have been fetched by
173 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
174 * under RCU lock. The pointer returned by the opp_find_freq family must be
175 * used in the same section as the usage of this function with the pointer
176 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
177 * pointer.
178 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500179unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200180{
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500181 struct dev_pm_opp *tmp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200182 unsigned long v = 0;
183
Krzysztof Kozlowski04bf1c72015-01-09 09:27:57 +0100184 opp_rcu_lockdep_assert();
185
Nishanth Menone1f60b22010-10-13 00:13:10 +0200186 tmp_opp = rcu_dereference(opp);
187 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
188 pr_err("%s: Invalid parameters\n", __func__);
189 else
190 v = tmp_opp->u_volt;
191
192 return v;
193}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500194EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200195
196/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500197 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200198 * @opp: opp for which frequency has to be returned for
199 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600200 * Return: frequency in hertz corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +0200201 * return 0
202 *
203 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
204 * protected pointer. This means that opp which could have been fetched by
205 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
206 * under RCU lock. The pointer returned by the opp_find_freq family must be
207 * used in the same section as the usage of this function with the pointer
208 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
209 * pointer.
210 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500211unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200212{
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500213 struct dev_pm_opp *tmp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200214 unsigned long f = 0;
215
Krzysztof Kozlowski04bf1c72015-01-09 09:27:57 +0100216 opp_rcu_lockdep_assert();
217
Nishanth Menone1f60b22010-10-13 00:13:10 +0200218 tmp_opp = rcu_dereference(opp);
219 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
220 pr_err("%s: Invalid parameters\n", __func__);
221 else
222 f = tmp_opp->rate;
223
224 return f;
225}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500226EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200227
228/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500229 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
Nishanth Menone1f60b22010-10-13 00:13:10 +0200230 * @dev: device for which we do this operation
231 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600232 * Return: This function returns the number of available opps if there are any,
Nishanth Menone1f60b22010-10-13 00:13:10 +0200233 * else returns 0 if none or the corresponding error value.
234 *
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800235 * Locking: This function takes rcu_read_lock().
Nishanth Menone1f60b22010-10-13 00:13:10 +0200236 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500237int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200238{
239 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500240 struct dev_pm_opp *temp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200241 int count = 0;
242
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800243 rcu_read_lock();
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800244
Nishanth Menon327854c2014-12-24 11:22:56 -0600245 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200246 if (IS_ERR(dev_opp)) {
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800247 count = PTR_ERR(dev_opp);
248 dev_err(dev, "%s: device OPP not found (%d)\n",
249 __func__, count);
250 goto out_unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200251 }
252
253 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
254 if (temp_opp->available)
255 count++;
256 }
257
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800258out_unlock:
259 rcu_read_unlock();
Nishanth Menone1f60b22010-10-13 00:13:10 +0200260 return count;
261}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500262EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200263
264/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500265 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200266 * @dev: device for which we do this operation
267 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100268 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200269 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600270 * Return: Searches for exact match in the opp list and returns pointer to the
271 * matching opp if found, else returns ERR_PTR in case of error and should
272 * be handled using IS_ERR. Error return values can be:
Nishanth Menon07797262012-10-24 22:00:12 +0200273 * EINVAL: for bad pointer
274 * ERANGE: no match found for search
275 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200276 *
277 * Note: available is a modifier for the search. if available=true, then the
278 * match is for exact matching frequency and is available in the stored OPP
279 * table. if false, the match is for exact frequency which is not available.
280 *
281 * This provides a mechanism to enable an opp which is not available currently
282 * or the opposite as well.
283 *
284 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
285 * protected pointer. The reason for the same is that the opp pointer which is
286 * returned will remain valid for use with opp_get_{voltage, freq} only while
287 * under the locked area. The pointer returned must be used prior to unlocking
288 * with rcu_read_unlock() to maintain the integrity of the pointer.
289 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500290struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
291 unsigned long freq,
292 bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200293{
294 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500295 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200296
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800297 opp_rcu_lockdep_assert();
298
Nishanth Menon327854c2014-12-24 11:22:56 -0600299 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200300 if (IS_ERR(dev_opp)) {
301 int r = PTR_ERR(dev_opp);
302 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
303 return ERR_PTR(r);
304 }
305
306 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
307 if (temp_opp->available == available &&
308 temp_opp->rate == freq) {
309 opp = temp_opp;
310 break;
311 }
312 }
313
314 return opp;
315}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500316EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200317
318/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500319 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200320 * @dev: device for which we do this operation
321 * @freq: Start frequency
322 *
323 * Search for the matching ceil *available* OPP from a starting freq
324 * for a device.
325 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600326 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200327 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
328 * values can be:
329 * EINVAL: for bad pointer
330 * ERANGE: no match found for search
331 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200332 *
333 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
334 * protected pointer. The reason for the same is that the opp pointer which is
335 * returned will remain valid for use with opp_get_{voltage, freq} only while
336 * under the locked area. The pointer returned must be used prior to unlocking
337 * with rcu_read_unlock() to maintain the integrity of the pointer.
338 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500339struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
340 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200341{
342 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500343 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200344
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800345 opp_rcu_lockdep_assert();
346
Nishanth Menone1f60b22010-10-13 00:13:10 +0200347 if (!dev || !freq) {
348 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
349 return ERR_PTR(-EINVAL);
350 }
351
Nishanth Menon327854c2014-12-24 11:22:56 -0600352 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200353 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200354 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200355
356 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
357 if (temp_opp->available && temp_opp->rate >= *freq) {
358 opp = temp_opp;
359 *freq = opp->rate;
360 break;
361 }
362 }
363
364 return opp;
365}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500366EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200367
368/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500369 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200370 * @dev: device for which we do this operation
371 * @freq: Start frequency
372 *
373 * Search for the matching floor *available* OPP from a starting freq
374 * for a device.
375 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600376 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200377 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
378 * values can be:
379 * EINVAL: for bad pointer
380 * ERANGE: no match found for search
381 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200382 *
383 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
384 * protected pointer. The reason for the same is that the opp pointer which is
385 * returned will remain valid for use with opp_get_{voltage, freq} only while
386 * under the locked area. The pointer returned must be used prior to unlocking
387 * with rcu_read_unlock() to maintain the integrity of the pointer.
388 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500389struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
390 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200391{
392 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500393 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200394
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800395 opp_rcu_lockdep_assert();
396
Nishanth Menone1f60b22010-10-13 00:13:10 +0200397 if (!dev || !freq) {
398 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
399 return ERR_PTR(-EINVAL);
400 }
401
Nishanth Menon327854c2014-12-24 11:22:56 -0600402 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200403 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200404 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200405
406 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
407 if (temp_opp->available) {
408 /* go to the next node, before choosing prev */
409 if (temp_opp->rate > *freq)
410 break;
411 else
412 opp = temp_opp;
413 }
414 }
415 if (!IS_ERR(opp))
416 *freq = opp->rate;
417
418 return opp;
419}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500420EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200421
Nishanth Menon984f16c2014-12-24 11:22:57 -0600422/**
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530423 * _add_device_opp() - Find device OPP table or allocate a new one
Nishanth Menon984f16c2014-12-24 11:22:57 -0600424 * @dev: device for which we do this operation
425 *
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530426 * It tries to find an existing table first, if it couldn't find one, it
427 * allocates a new OPP table and returns that.
Nishanth Menon984f16c2014-12-24 11:22:57 -0600428 *
429 * Return: valid device_opp pointer if success, else NULL.
430 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600431static struct device_opp *_add_device_opp(struct device *dev)
Viresh Kumar07cce742014-12-10 09:45:34 +0530432{
433 struct device_opp *dev_opp;
434
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530435 /* Check for existing list for 'dev' first */
436 dev_opp = _find_device_opp(dev);
437 if (!IS_ERR(dev_opp))
438 return dev_opp;
439
Viresh Kumar07cce742014-12-10 09:45:34 +0530440 /*
441 * Allocate a new device OPP table. In the infrequent case where a new
442 * device is needed to be added, we pay this penalty.
443 */
444 dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);
445 if (!dev_opp)
446 return NULL;
447
448 dev_opp->dev = dev;
449 srcu_init_notifier_head(&dev_opp->srcu_head);
450 INIT_LIST_HEAD(&dev_opp->opp_list);
451
452 /* Secure the device list modification */
453 list_add_rcu(&dev_opp->node, &dev_opp_list);
454 return dev_opp;
455}
456
Nishanth Menon984f16c2014-12-24 11:22:57 -0600457/**
Viresh Kumar737002b2015-07-29 16:22:58 +0530458 * _kfree_device_rcu() - Free device_opp RCU handler
459 * @head: RCU head
460 */
461static void _kfree_device_rcu(struct rcu_head *head)
462{
463 struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
464
465 kfree_rcu(device_opp, rcu_head);
466}
467
468/**
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530469 * _remove_device_opp() - Removes a device OPP table
470 * @dev_opp: device OPP table to be removed.
471 *
472 * Removes/frees device OPP table it it doesn't contain any OPPs.
473 */
474static void _remove_device_opp(struct device_opp *dev_opp)
475{
476 if (!list_empty(&dev_opp->opp_list))
477 return;
478
479 list_del_rcu(&dev_opp->node);
480 call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
481 _kfree_device_rcu);
482}
483
484/**
Viresh Kumar737002b2015-07-29 16:22:58 +0530485 * _kfree_opp_rcu() - Free OPP RCU handler
486 * @head: RCU head
487 */
488static void _kfree_opp_rcu(struct rcu_head *head)
489{
490 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
491
492 kfree_rcu(opp, rcu_head);
493}
494
495/**
496 * _opp_remove() - Remove an OPP from a table definition
497 * @dev_opp: points back to the device_opp struct this opp belongs to
498 * @opp: pointer to the OPP to remove
Viresh Kumar23dacf62015-07-29 16:23:01 +0530499 * @notify: OPP_EVENT_REMOVE notification should be sent or not
Viresh Kumar737002b2015-07-29 16:22:58 +0530500 *
501 * This function removes an opp definition from the opp list.
502 *
503 * Locking: The internal device_opp and opp structures are RCU protected.
504 * It is assumed that the caller holds required mutex for an RCU updater
505 * strategy.
506 */
507static void _opp_remove(struct device_opp *dev_opp,
Viresh Kumar23dacf62015-07-29 16:23:01 +0530508 struct dev_pm_opp *opp, bool notify)
Viresh Kumar737002b2015-07-29 16:22:58 +0530509{
510 /*
511 * Notify the changes in the availability of the operable
512 * frequency/voltage list.
513 */
Viresh Kumar23dacf62015-07-29 16:23:01 +0530514 if (notify)
515 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
Viresh Kumar737002b2015-07-29 16:22:58 +0530516 list_del_rcu(&opp->node);
517 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
518
Viresh Kumar3bac42c2015-07-29 16:22:59 +0530519 _remove_device_opp(dev_opp);
Viresh Kumar737002b2015-07-29 16:22:58 +0530520}
521
522/**
523 * dev_pm_opp_remove() - Remove an OPP from OPP list
524 * @dev: device for which we do this operation
525 * @freq: OPP to remove with matching 'freq'
526 *
527 * This function removes an opp from the opp list.
528 *
529 * Locking: The internal device_opp and opp structures are RCU protected.
530 * Hence this function internally uses RCU updater strategy with mutex locks
531 * to keep the integrity of the internal data structures. Callers should ensure
532 * that this function is *NOT* called under RCU protection or in contexts where
533 * mutex cannot be locked.
534 */
535void dev_pm_opp_remove(struct device *dev, unsigned long freq)
536{
537 struct dev_pm_opp *opp;
538 struct device_opp *dev_opp;
539 bool found = false;
540
541 /* Hold our list modification lock here */
542 mutex_lock(&dev_opp_list_lock);
543
544 dev_opp = _find_device_opp(dev);
545 if (IS_ERR(dev_opp))
546 goto unlock;
547
548 list_for_each_entry(opp, &dev_opp->opp_list, node) {
549 if (opp->rate == freq) {
550 found = true;
551 break;
552 }
553 }
554
555 if (!found) {
556 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
557 __func__, freq);
558 goto unlock;
559 }
560
Viresh Kumar23dacf62015-07-29 16:23:01 +0530561 _opp_remove(dev_opp, opp, true);
Viresh Kumar737002b2015-07-29 16:22:58 +0530562unlock:
563 mutex_unlock(&dev_opp_list_lock);
564}
565EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
566
Viresh Kumar23dacf62015-07-29 16:23:01 +0530567static struct dev_pm_opp *_allocate_opp(struct device *dev,
568 struct device_opp **dev_opp)
569{
570 struct dev_pm_opp *opp;
571
572 /* allocate new OPP node */
573 opp = kzalloc(sizeof(*opp), GFP_KERNEL);
574 if (!opp)
575 return NULL;
576
577 INIT_LIST_HEAD(&opp->node);
578
579 *dev_opp = _add_device_opp(dev);
580 if (!*dev_opp) {
581 kfree(opp);
582 return NULL;
583 }
584
585 return opp;
586}
587
588static int _opp_add(struct dev_pm_opp *new_opp, struct device_opp *dev_opp)
589{
590 struct dev_pm_opp *opp;
591 struct list_head *head = &dev_opp->opp_list;
592
593 /*
594 * Insert new OPP in order of increasing frequency and discard if
595 * already present.
596 *
597 * Need to use &dev_opp->opp_list in the condition part of the 'for'
598 * loop, don't replace it with head otherwise it will become an infinite
599 * loop.
600 */
601 list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
602 if (new_opp->rate > opp->rate) {
603 head = &opp->node;
604 continue;
605 }
606
607 if (new_opp->rate < opp->rate)
608 break;
609
610 /* Duplicate OPPs */
611 dev_warn(dev_opp->dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
612 __func__, opp->rate, opp->u_volt, opp->available,
613 new_opp->rate, new_opp->u_volt, new_opp->available);
614
615 return opp->available && new_opp->u_volt == opp->u_volt ?
616 0 : -EEXIST;
617 }
618
619 new_opp->dev_opp = dev_opp;
620 list_add_rcu(&new_opp->node, head);
621
622 return 0;
623}
624
Viresh Kumar737002b2015-07-29 16:22:58 +0530625/**
Nishanth Menon984f16c2014-12-24 11:22:57 -0600626 * _opp_add_dynamic() - Allocate a dynamic OPP.
627 * @dev: device for which we do this operation
628 * @freq: Frequency in Hz for this OPP
629 * @u_volt: Voltage in uVolts for this OPP
630 * @dynamic: Dynamically added OPPs.
631 *
632 * This function adds an opp definition to the opp list and returns status.
633 * The opp is made available by default and it can be controlled using
634 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
635 *
636 * NOTE: "dynamic" parameter impacts OPPs added by the of_init_opp_table and
637 * freed by of_free_opp_table.
638 *
639 * Locking: The internal device_opp and opp structures are RCU protected.
640 * Hence this function internally uses RCU updater strategy with mutex locks
641 * to keep the integrity of the internal data structures. Callers should ensure
642 * that this function is *NOT* called under RCU protection or in contexts where
643 * mutex cannot be locked.
644 *
645 * Return:
646 * 0 On success OR
647 * Duplicate OPPs (both freq and volt are same) and opp->available
648 * -EEXIST Freq are same and volt are different OR
649 * Duplicate OPPs (both freq and volt are same) and !opp->available
650 * -ENOMEM Memory allocation failure
651 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600652static int _opp_add_dynamic(struct device *dev, unsigned long freq,
653 long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200654{
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530655 struct device_opp *dev_opp;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530656 struct dev_pm_opp *new_opp;
Viresh Kumar6ce41842014-12-10 09:45:35 +0530657 int ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200658
Nishanth Menone1f60b22010-10-13 00:13:10 +0200659 /* Hold our list modification lock here */
660 mutex_lock(&dev_opp_list_lock);
661
Viresh Kumar23dacf62015-07-29 16:23:01 +0530662 new_opp = _allocate_opp(dev, &dev_opp);
663 if (!new_opp) {
664 ret = -ENOMEM;
665 goto unlock;
666 }
667
Viresh Kumara7470db2014-11-25 16:04:17 +0530668 /* populate the opp table */
Viresh Kumara7470db2014-11-25 16:04:17 +0530669 new_opp->rate = freq;
670 new_opp->u_volt = u_volt;
671 new_opp->available = true;
Viresh Kumaraa5f2f82015-07-29 16:23:00 +0530672 new_opp->dynamic = dynamic;
Viresh Kumar23dacf62015-07-29 16:23:01 +0530673
674 ret = _opp_add(new_opp, dev_opp);
675 if (ret)
676 goto free_opp;
677
Nishanth Menone1f60b22010-10-13 00:13:10 +0200678 mutex_unlock(&dev_opp_list_lock);
679
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200680 /*
681 * Notify the changes in the availability of the operable
682 * frequency/voltage list.
683 */
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530684 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200685 return 0;
Viresh Kumar6ce41842014-12-10 09:45:35 +0530686
687free_opp:
Viresh Kumar23dacf62015-07-29 16:23:01 +0530688 _opp_remove(dev_opp, new_opp, false);
689unlock:
Viresh Kumar6ce41842014-12-10 09:45:35 +0530690 mutex_unlock(&dev_opp_list_lock);
Viresh Kumar6ce41842014-12-10 09:45:35 +0530691 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200692}
Viresh Kumar383934092014-11-25 16:04:18 +0530693
Viresh Kumar27465902015-07-29 16:23:02 +0530694/* TODO: Support multiple regulators */
695static int opp_get_microvolt(struct dev_pm_opp *opp, struct device *dev)
696{
697 u32 microvolt[3] = {0};
698 int count, ret;
699
700 count = of_property_count_u32_elems(opp->np, "opp-microvolt");
701 if (!count)
702 return 0;
703
704 /* There can be one or three elements here */
705 if (count != 1 && count != 3) {
706 dev_err(dev, "%s: Invalid number of elements in opp-microvolt property (%d)\n",
707 __func__, count);
708 return -EINVAL;
709 }
710
711 ret = of_property_read_u32_array(opp->np, "opp-microvolt", microvolt,
712 count);
713 if (ret) {
714 dev_err(dev, "%s: error parsing opp-microvolt: %d\n", __func__,
715 ret);
716 return -EINVAL;
717 }
718
719 opp->u_volt = microvolt[0];
720 opp->u_volt_min = microvolt[1];
721 opp->u_volt_max = microvolt[2];
722
723 return 0;
724}
725
726/**
727 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings)
728 * @dev: device for which we do this operation
729 * @np: device node
730 *
731 * This function adds an opp definition to the opp list and returns status. The
732 * opp can be controlled using dev_pm_opp_enable/disable functions and may be
733 * removed by dev_pm_opp_remove.
734 *
735 * Locking: The internal device_opp and opp structures are RCU protected.
736 * Hence this function internally uses RCU updater strategy with mutex locks
737 * to keep the integrity of the internal data structures. Callers should ensure
738 * that this function is *NOT* called under RCU protection or in contexts where
739 * mutex cannot be locked.
740 *
741 * Return:
742 * 0 On success OR
743 * Duplicate OPPs (both freq and volt are same) and opp->available
744 * -EEXIST Freq are same and volt are different OR
745 * Duplicate OPPs (both freq and volt are same) and !opp->available
746 * -ENOMEM Memory allocation failure
747 * -EINVAL Failed parsing the OPP node
748 */
749static int _opp_add_static_v2(struct device *dev, struct device_node *np)
750{
751 struct device_opp *dev_opp;
752 struct dev_pm_opp *new_opp;
753 u64 rate;
754 int ret;
755
756 /* Hold our list modification lock here */
757 mutex_lock(&dev_opp_list_lock);
758
759 new_opp = _allocate_opp(dev, &dev_opp);
760 if (!new_opp) {
761 ret = -ENOMEM;
762 goto unlock;
763 }
764
765 ret = of_property_read_u64(np, "opp-hz", &rate);
766 if (ret < 0) {
767 dev_err(dev, "%s: opp-hz not found\n", __func__);
768 goto free_opp;
769 }
770
771 /*
772 * Rate is defined as an unsigned long in clk API, and so casting
773 * explicitly to its type. Must be fixed once rate is 64 bit
774 * guaranteed in clk API.
775 */
776 new_opp->rate = (unsigned long)rate;
777 new_opp->turbo = of_property_read_bool(np, "turbo-mode");
778
779 new_opp->np = np;
780 new_opp->dynamic = false;
781 new_opp->available = true;
782
783 ret = opp_get_microvolt(new_opp, dev);
784 if (ret)
785 goto free_opp;
786
787 of_property_read_u32(np, "opp-microamp", (u32 *)&new_opp->u_amp);
788
789 ret = _opp_add(new_opp, dev_opp);
790 if (ret)
791 goto free_opp;
792
793 mutex_unlock(&dev_opp_list_lock);
794
795 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu\n",
796 __func__, new_opp->turbo, new_opp->rate, new_opp->u_volt,
797 new_opp->u_volt_min, new_opp->u_volt_max);
798
799 /*
800 * Notify the changes in the availability of the operable
801 * frequency/voltage list.
802 */
803 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
804 return 0;
805
806free_opp:
807 _opp_remove(dev_opp, new_opp, false);
808unlock:
809 mutex_unlock(&dev_opp_list_lock);
810 return ret;
811}
812
Viresh Kumar383934092014-11-25 16:04:18 +0530813/**
814 * dev_pm_opp_add() - Add an OPP table from a table definitions
815 * @dev: device for which we do this operation
816 * @freq: Frequency in Hz for this OPP
817 * @u_volt: Voltage in uVolts for this OPP
818 *
819 * This function adds an opp definition to the opp list and returns status.
820 * The opp is made available by default and it can be controlled using
821 * dev_pm_opp_enable/disable functions.
822 *
823 * Locking: The internal device_opp and opp structures are RCU protected.
824 * Hence this function internally uses RCU updater strategy with mutex locks
825 * to keep the integrity of the internal data structures. Callers should ensure
826 * that this function is *NOT* called under RCU protection or in contexts where
827 * mutex cannot be locked.
828 *
829 * Return:
Nishanth Menon984f16c2014-12-24 11:22:57 -0600830 * 0 On success OR
Viresh Kumar383934092014-11-25 16:04:18 +0530831 * Duplicate OPPs (both freq and volt are same) and opp->available
Nishanth Menon984f16c2014-12-24 11:22:57 -0600832 * -EEXIST Freq are same and volt are different OR
Viresh Kumar383934092014-11-25 16:04:18 +0530833 * Duplicate OPPs (both freq and volt are same) and !opp->available
Nishanth Menon984f16c2014-12-24 11:22:57 -0600834 * -ENOMEM Memory allocation failure
Viresh Kumar383934092014-11-25 16:04:18 +0530835 */
836int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
837{
Nishanth Menon327854c2014-12-24 11:22:56 -0600838 return _opp_add_dynamic(dev, freq, u_volt, true);
Viresh Kumar383934092014-11-25 16:04:18 +0530839}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500840EXPORT_SYMBOL_GPL(dev_pm_opp_add);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200841
Nishanth Menon984f16c2014-12-24 11:22:57 -0600842/**
Nishanth Menon327854c2014-12-24 11:22:56 -0600843 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200844 * @dev: device for which we do this operation
845 * @freq: OPP frequency to modify availability
846 * @availability_req: availability status requested for this opp
847 *
848 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
849 * share a common logic which is isolated here.
850 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600851 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Nishanth Menone1f60b22010-10-13 00:13:10 +0200852 * copy operation, returns 0 if no modifcation was done OR modification was
853 * successful.
854 *
855 * Locking: The internal device_opp and opp structures are RCU protected.
856 * Hence this function internally uses RCU updater strategy with mutex locks to
857 * keep the integrity of the internal data structures. Callers should ensure
858 * that this function is *NOT* called under RCU protection or in contexts where
859 * mutex locking or synchronize_rcu() blocking calls cannot be used.
860 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600861static int _opp_set_availability(struct device *dev, unsigned long freq,
862 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200863{
Viresh Kumar29df0ee2014-12-10 09:45:33 +0530864 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500865 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200866 int r = 0;
867
868 /* keep the node allocated */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500869 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
Quentin Lambert59d84ca2015-02-09 10:45:32 +0100870 if (!new_opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200871 return -ENOMEM;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200872
873 mutex_lock(&dev_opp_list_lock);
874
875 /* Find the device_opp */
Nishanth Menon327854c2014-12-24 11:22:56 -0600876 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200877 if (IS_ERR(dev_opp)) {
878 r = PTR_ERR(dev_opp);
879 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
880 goto unlock;
881 }
882
883 /* Do we have the frequency? */
884 list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
885 if (tmp_opp->rate == freq) {
886 opp = tmp_opp;
887 break;
888 }
889 }
890 if (IS_ERR(opp)) {
891 r = PTR_ERR(opp);
892 goto unlock;
893 }
894
895 /* Is update really needed? */
896 if (opp->available == availability_req)
897 goto unlock;
898 /* copy the old data over */
899 *new_opp = *opp;
900
901 /* plug in new node */
902 new_opp->available = availability_req;
903
904 list_replace_rcu(&opp->node, &new_opp->node);
905 mutex_unlock(&dev_opp_list_lock);
Nishanth Menon327854c2014-12-24 11:22:56 -0600906 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200907
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200908 /* Notify the change of the OPP availability */
909 if (availability_req)
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530910 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200911 new_opp);
912 else
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530913 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200914 new_opp);
915
Vincent Guittotdde84372012-10-23 01:21:49 +0200916 return 0;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200917
918unlock:
919 mutex_unlock(&dev_opp_list_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200920 kfree(new_opp);
921 return r;
922}
923
924/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500925 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +0200926 * @dev: device for which we do this operation
927 * @freq: OPP frequency to enable
928 *
929 * Enables a provided opp. If the operation is valid, this returns 0, else the
930 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500931 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200932 *
933 * Locking: The internal device_opp and opp structures are RCU protected.
934 * Hence this function indirectly uses RCU and mutex locks to keep the
935 * integrity of the internal data structures. Callers should ensure that
936 * this function is *NOT* called under RCU protection or in contexts where
937 * mutex locking or synchronize_rcu() blocking calls cannot be used.
Nishanth Menon984f16c2014-12-24 11:22:57 -0600938 *
939 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
940 * copy operation, returns 0 if no modifcation was done OR modification was
941 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200942 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500943int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200944{
Nishanth Menon327854c2014-12-24 11:22:56 -0600945 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200946}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500947EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200948
949/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500950 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +0200951 * @dev: device for which we do this operation
952 * @freq: OPP frequency to disable
953 *
954 * Disables a provided opp. If the operation is valid, this returns
955 * 0, else the corresponding error value. It is meant to be a temporary
956 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500957 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +0200958 *
959 * Locking: The internal device_opp and opp structures are RCU protected.
960 * Hence this function indirectly uses RCU and mutex locks to keep the
961 * integrity of the internal data structures. Callers should ensure that
962 * this function is *NOT* called under RCU protection or in contexts where
963 * mutex locking or synchronize_rcu() blocking calls cannot be used.
Nishanth Menon984f16c2014-12-24 11:22:57 -0600964 *
965 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
966 * copy operation, returns 0 if no modifcation was done OR modification was
967 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200968 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500969int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200970{
Nishanth Menon327854c2014-12-24 11:22:56 -0600971 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200972}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500973EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200974
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200975/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500976 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200977 * @dev: device pointer used to lookup device OPPs.
Nishanth Menon984f16c2014-12-24 11:22:57 -0600978 *
979 * Return: pointer to notifier head if found, otherwise -ENODEV or
980 * -EINVAL based on type of error casted as pointer. value must be checked
981 * with IS_ERR to determine valid pointer or error result.
982 *
983 * Locking: This function must be called under rcu_read_lock(). dev_opp is a RCU
984 * protected pointer. The reason for the same is that the opp pointer which is
985 * returned will remain valid for use with opp_get_{voltage, freq} only while
986 * under the locked area. The pointer returned must be used prior to unlocking
987 * with rcu_read_unlock() to maintain the integrity of the pointer.
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200988 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500989struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200990{
Nishanth Menon327854c2014-12-24 11:22:56 -0600991 struct device_opp *dev_opp = _find_device_opp(dev);
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200992
993 if (IS_ERR(dev_opp))
Thomas Meyer156acb12011-11-08 22:34:00 +0100994 return ERR_CAST(dev_opp); /* matching type */
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200995
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530996 return &dev_opp->srcu_head;
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200997}
Nishanth Menon4679ec32014-12-24 11:22:55 -0600998EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +0200999
1000#ifdef CONFIG_OF
1001/**
Viresh Kumar737002b2015-07-29 16:22:58 +05301002 * of_free_opp_table() - Free OPP table entries created from static DT entries
1003 * @dev: device pointer used to lookup device OPPs.
1004 *
1005 * Free OPPs created using static entries present in DT.
1006 *
1007 * Locking: The internal device_opp and opp structures are RCU protected.
1008 * Hence this function indirectly uses RCU updater strategy with mutex locks
1009 * to keep the integrity of the internal data structures. Callers should ensure
1010 * that this function is *NOT* called under RCU protection or in contexts where
1011 * mutex cannot be locked.
1012 */
1013void of_free_opp_table(struct device *dev)
1014{
1015 struct device_opp *dev_opp;
1016 struct dev_pm_opp *opp, *tmp;
1017
1018 /* Check for existing list for 'dev' */
1019 dev_opp = _find_device_opp(dev);
1020 if (IS_ERR(dev_opp)) {
1021 int error = PTR_ERR(dev_opp);
1022
1023 if (error != -ENODEV)
1024 WARN(1, "%s: dev_opp: %d\n",
1025 IS_ERR_OR_NULL(dev) ?
1026 "Invalid device" : dev_name(dev),
1027 error);
1028 return;
1029 }
1030
1031 /* Hold our list modification lock here */
1032 mutex_lock(&dev_opp_list_lock);
1033
1034 /* Free static OPPs */
1035 list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
1036 if (!opp->dynamic)
Viresh Kumar23dacf62015-07-29 16:23:01 +05301037 _opp_remove(dev_opp, opp, true);
Viresh Kumar737002b2015-07-29 16:22:58 +05301038 }
1039
1040 mutex_unlock(&dev_opp_list_lock);
1041}
1042EXPORT_SYMBOL_GPL(of_free_opp_table);
1043
Viresh Kumar27465902015-07-29 16:23:02 +05301044/* Returns opp descriptor node from its phandle. Caller must do of_node_put() */
1045static struct device_node *
1046_of_get_opp_desc_node_from_prop(struct device *dev, const struct property *prop)
1047{
1048 struct device_node *opp_np;
1049
1050 opp_np = of_find_node_by_phandle(be32_to_cpup(prop->value));
1051 if (!opp_np) {
1052 dev_err(dev, "%s: Prop: %s contains invalid opp desc phandle\n",
1053 __func__, prop->name);
1054 return ERR_PTR(-EINVAL);
1055 }
1056
1057 return opp_np;
1058}
1059
1060/* Initializes OPP tables based on new bindings */
1061static int _of_init_opp_table_v2(struct device *dev,
1062 const struct property *prop)
1063{
1064 struct device_node *opp_np, *np;
1065 int ret = 0, count = 0;
1066
1067 if (!prop->value)
1068 return -ENODATA;
1069
1070 /* Get opp node */
1071 opp_np = _of_get_opp_desc_node_from_prop(dev, prop);
1072 if (IS_ERR(opp_np))
1073 return PTR_ERR(opp_np);
1074
1075 /* We have opp-list node now, iterate over it and add OPPs */
1076 for_each_available_child_of_node(opp_np, np) {
1077 count++;
1078
1079 ret = _opp_add_static_v2(dev, np);
1080 if (ret) {
1081 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__,
1082 ret);
1083 break;
1084 }
1085 }
1086
1087 /* There should be one of more OPP defined */
1088 if (WARN_ON(!count))
1089 goto put_opp_np;
1090
1091 if (ret)
1092 of_free_opp_table(dev);
1093
1094put_opp_np:
1095 of_node_put(opp_np);
1096
1097 return ret;
1098}
1099
1100/* Initializes OPP tables based on old-deprecated bindings */
1101static int _of_init_opp_table_v1(struct device *dev)
Shawn Guob496dfb2012-09-05 01:09:12 +02001102{
1103 const struct property *prop;
1104 const __be32 *val;
1105 int nr;
1106
1107 prop = of_find_property(dev->of_node, "operating-points", NULL);
1108 if (!prop)
1109 return -ENODEV;
1110 if (!prop->value)
1111 return -ENODATA;
1112
1113 /*
1114 * Each OPP is a set of tuples consisting of frequency and
1115 * voltage like <freq-kHz vol-uV>.
1116 */
1117 nr = prop->length / sizeof(u32);
1118 if (nr % 2) {
1119 dev_err(dev, "%s: Invalid OPP list\n", __func__);
1120 return -EINVAL;
1121 }
1122
1123 val = prop->value;
1124 while (nr) {
1125 unsigned long freq = be32_to_cpup(val++) * 1000;
1126 unsigned long volt = be32_to_cpup(val++);
1127
Nishanth Menon327854c2014-12-24 11:22:56 -06001128 if (_opp_add_dynamic(dev, freq, volt, false))
Shawn Guob496dfb2012-09-05 01:09:12 +02001129 dev_warn(dev, "%s: Failed to add OPP %ld\n",
1130 __func__, freq);
Shawn Guob496dfb2012-09-05 01:09:12 +02001131 nr -= 2;
1132 }
1133
1134 return 0;
1135}
Viresh Kumar27465902015-07-29 16:23:02 +05301136
1137/**
1138 * of_init_opp_table() - Initialize opp table from device tree
1139 * @dev: device pointer used to lookup device OPPs.
1140 *
1141 * Register the initial OPP table with the OPP library for given device.
1142 *
1143 * Locking: The internal device_opp and opp structures are RCU protected.
1144 * Hence this function indirectly uses RCU updater strategy with mutex locks
1145 * to keep the integrity of the internal data structures. Callers should ensure
1146 * that this function is *NOT* called under RCU protection or in contexts where
1147 * mutex cannot be locked.
1148 *
1149 * Return:
1150 * 0 On success OR
1151 * Duplicate OPPs (both freq and volt are same) and opp->available
1152 * -EEXIST Freq are same and volt are different OR
1153 * Duplicate OPPs (both freq and volt are same) and !opp->available
1154 * -ENOMEM Memory allocation failure
1155 * -ENODEV when 'operating-points' property is not found or is invalid data
1156 * in device node.
1157 * -ENODATA when empty 'operating-points' property is found
1158 * -EINVAL when invalid entries are found in opp-v2 table
1159 */
1160int of_init_opp_table(struct device *dev)
1161{
1162 const struct property *prop;
1163
1164 /*
1165 * OPPs have two version of bindings now. The older one is deprecated,
1166 * try for the new binding first.
1167 */
1168 prop = of_find_property(dev->of_node, "operating-points-v2", NULL);
1169 if (!prop) {
1170 /*
1171 * Try old-deprecated bindings for backward compatibility with
1172 * older dtbs.
1173 */
1174 return _of_init_opp_table_v1(dev);
1175 }
1176
1177 return _of_init_opp_table_v2(dev, prop);
1178}
Mark Langsdorf74c46c62013-01-28 18:26:16 +00001179EXPORT_SYMBOL_GPL(of_init_opp_table);
Shawn Guob496dfb2012-09-05 01:09:12 +02001180#endif