blob: 8c3fd57975fb015bb2830d86729e8cfeb1d6eecc [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
54 * @rate: Frequency in hertz
55 * @u_volt: Nominal voltage in microvolts corresponding to this OPP
56 * @dev_opp: points back to the device_opp struct this opp belongs to
Viresh Kumarcd1a0682014-11-25 16:04:16 +053057 * @rcu_head: RCU callback head used for deferred freeing
Nishanth Menone1f60b22010-10-13 00:13:10 +020058 *
59 * This structure stores the OPP information for a given device.
60 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -050061struct dev_pm_opp {
Nishanth Menone1f60b22010-10-13 00:13:10 +020062 struct list_head node;
63
64 bool available;
Viresh Kumar383934092014-11-25 16:04:18 +053065 bool dynamic;
Nishanth Menone1f60b22010-10-13 00:13:10 +020066 unsigned long rate;
67 unsigned long u_volt;
68
69 struct device_opp *dev_opp;
Viresh Kumarcd1a0682014-11-25 16:04:16 +053070 struct rcu_head rcu_head;
Nishanth Menone1f60b22010-10-13 00:13:10 +020071};
72
73/**
74 * struct device_opp - Device opp structure
75 * @node: list node - contains the devices with OPPs that
76 * have been registered. Nodes once added are not modified in this
77 * list.
78 * RCU usage: nodes are not modified in the list of device_opp,
79 * however addition is possible and is secured by dev_opp_list_lock
80 * @dev: device pointer
Viresh Kumarcd1a0682014-11-25 16:04:16 +053081 * @srcu_head: notifier head to notify the OPP availability changes.
Viresh Kumar129eec52014-11-27 08:54:06 +053082 * @rcu_head: RCU callback head used for deferred freeing
Nishanth Menone1f60b22010-10-13 00:13:10 +020083 * @opp_list: list of opps
84 *
85 * This is an internal data structure maintaining the link to opps attached to
86 * a device. This structure is not meant to be shared to users as it is
Viresh Kumar1c6a6622014-12-10 09:45:31 +053087 * meant for book keeping and private to OPP library.
88 *
89 * Because the opp structures can be used from both rcu and srcu readers, we
90 * need to wait for the grace period of both of them before freeing any
91 * resources. And so we have used kfree_rcu() from within call_srcu() handlers.
Nishanth Menone1f60b22010-10-13 00:13:10 +020092 */
93struct device_opp {
94 struct list_head node;
95
96 struct device *dev;
Viresh Kumarcd1a0682014-11-25 16:04:16 +053097 struct srcu_notifier_head srcu_head;
Viresh Kumar129eec52014-11-27 08:54:06 +053098 struct rcu_head rcu_head;
Nishanth Menone1f60b22010-10-13 00:13:10 +020099 struct list_head opp_list;
100};
101
102/*
103 * The root of the list of all devices. All device_opp structures branch off
104 * from here, with each device_opp containing the list of opp it supports in
105 * various states of availability.
106 */
107static LIST_HEAD(dev_opp_list);
108/* Lock to allow exclusive modification to the device and opp lists */
109static DEFINE_MUTEX(dev_opp_list_lock);
110
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800111#define opp_rcu_lockdep_assert() \
112do { \
113 rcu_lockdep_assert(rcu_read_lock_held() || \
114 lockdep_is_held(&dev_opp_list_lock), \
115 "Missing rcu_read_lock() or " \
116 "dev_opp_list_lock protection"); \
117} while (0)
118
Nishanth Menone1f60b22010-10-13 00:13:10 +0200119/**
Nishanth Menon327854c2014-12-24 11:22:56 -0600120 * _find_device_opp() - find device_opp struct using device pointer
Nishanth Menone1f60b22010-10-13 00:13:10 +0200121 * @dev: device pointer used to lookup device OPPs
122 *
123 * Search list of device OPPs for one containing matching device. Does a RCU
124 * reader operation to grab the pointer needed.
125 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600126 * Return: pointer to 'struct device_opp' if found, otherwise -ENODEV or
Nishanth Menone1f60b22010-10-13 00:13:10 +0200127 * -EINVAL based on type of error.
128 *
129 * Locking: This function must be called under rcu_read_lock(). device_opp
130 * is a RCU protected pointer. This means that device_opp is valid as long
131 * as we are under RCU lock.
132 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600133static struct device_opp *_find_device_opp(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200134{
135 struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV);
136
137 if (unlikely(IS_ERR_OR_NULL(dev))) {
138 pr_err("%s: Invalid parameters\n", __func__);
139 return ERR_PTR(-EINVAL);
140 }
141
142 list_for_each_entry_rcu(tmp_dev_opp, &dev_opp_list, node) {
143 if (tmp_dev_opp->dev == dev) {
144 dev_opp = tmp_dev_opp;
145 break;
146 }
147 }
148
149 return dev_opp;
150}
151
152/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500153 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200154 * @opp: opp for which voltage has to be returned for
155 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600156 * Return: voltage in micro volt corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +0200157 * return 0
158 *
159 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
160 * protected pointer. This means that opp which could have been fetched by
161 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
162 * under RCU lock. The pointer returned by the opp_find_freq family must be
163 * used in the same section as the usage of this function with the pointer
164 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
165 * pointer.
166 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500167unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200168{
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500169 struct dev_pm_opp *tmp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200170 unsigned long v = 0;
171
Krzysztof Kozlowski04bf1c72015-01-09 09:27:57 +0100172 opp_rcu_lockdep_assert();
173
Nishanth Menone1f60b22010-10-13 00:13:10 +0200174 tmp_opp = rcu_dereference(opp);
175 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
176 pr_err("%s: Invalid parameters\n", __func__);
177 else
178 v = tmp_opp->u_volt;
179
180 return v;
181}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500182EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200183
184/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500185 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200186 * @opp: opp for which frequency has to be returned for
187 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600188 * Return: frequency in hertz corresponding to the opp, else
Nishanth Menone1f60b22010-10-13 00:13:10 +0200189 * return 0
190 *
191 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
192 * protected pointer. This means that opp which could have been fetched by
193 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
194 * under RCU lock. The pointer returned by the opp_find_freq family must be
195 * used in the same section as the usage of this function with the pointer
196 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
197 * pointer.
198 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500199unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200200{
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500201 struct dev_pm_opp *tmp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200202 unsigned long f = 0;
203
Krzysztof Kozlowski04bf1c72015-01-09 09:27:57 +0100204 opp_rcu_lockdep_assert();
205
Nishanth Menone1f60b22010-10-13 00:13:10 +0200206 tmp_opp = rcu_dereference(opp);
207 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
208 pr_err("%s: Invalid parameters\n", __func__);
209 else
210 f = tmp_opp->rate;
211
212 return f;
213}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500214EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200215
216/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500217 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
Nishanth Menone1f60b22010-10-13 00:13:10 +0200218 * @dev: device for which we do this operation
219 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600220 * Return: This function returns the number of available opps if there are any,
Nishanth Menone1f60b22010-10-13 00:13:10 +0200221 * else returns 0 if none or the corresponding error value.
222 *
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800223 * Locking: This function takes rcu_read_lock().
Nishanth Menone1f60b22010-10-13 00:13:10 +0200224 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500225int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200226{
227 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500228 struct dev_pm_opp *temp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200229 int count = 0;
230
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800231 rcu_read_lock();
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800232
Nishanth Menon327854c2014-12-24 11:22:56 -0600233 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200234 if (IS_ERR(dev_opp)) {
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800235 count = PTR_ERR(dev_opp);
236 dev_err(dev, "%s: device OPP not found (%d)\n",
237 __func__, count);
238 goto out_unlock;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200239 }
240
241 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
242 if (temp_opp->available)
243 count++;
244 }
245
Dmitry Torokhovb4718c02014-12-16 15:09:38 -0800246out_unlock:
247 rcu_read_unlock();
Nishanth Menone1f60b22010-10-13 00:13:10 +0200248 return count;
249}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500250EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200251
252/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500253 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200254 * @dev: device for which we do this operation
255 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100256 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200257 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600258 * Return: Searches for exact match in the opp list and returns pointer to the
259 * matching opp if found, else returns ERR_PTR in case of error and should
260 * be handled using IS_ERR. Error return values can be:
Nishanth Menon07797262012-10-24 22:00:12 +0200261 * EINVAL: for bad pointer
262 * ERANGE: no match found for search
263 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200264 *
265 * Note: available is a modifier for the search. if available=true, then the
266 * match is for exact matching frequency and is available in the stored OPP
267 * table. if false, the match is for exact frequency which is not available.
268 *
269 * This provides a mechanism to enable an opp which is not available currently
270 * or the opposite as well.
271 *
272 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
273 * protected pointer. The reason for the same is that the opp pointer which is
274 * returned will remain valid for use with opp_get_{voltage, freq} only while
275 * under the locked area. The pointer returned must be used prior to unlocking
276 * with rcu_read_unlock() to maintain the integrity of the pointer.
277 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500278struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
279 unsigned long freq,
280 bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200281{
282 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500283 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200284
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800285 opp_rcu_lockdep_assert();
286
Nishanth Menon327854c2014-12-24 11:22:56 -0600287 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200288 if (IS_ERR(dev_opp)) {
289 int r = PTR_ERR(dev_opp);
290 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
291 return ERR_PTR(r);
292 }
293
294 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
295 if (temp_opp->available == available &&
296 temp_opp->rate == freq) {
297 opp = temp_opp;
298 break;
299 }
300 }
301
302 return opp;
303}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500304EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200305
306/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500307 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200308 * @dev: device for which we do this operation
309 * @freq: Start frequency
310 *
311 * Search for the matching ceil *available* OPP from a starting freq
312 * for a device.
313 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600314 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200315 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
316 * values can be:
317 * EINVAL: for bad pointer
318 * ERANGE: no match found for search
319 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200320 *
321 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
322 * protected pointer. The reason for the same is that the opp pointer which is
323 * returned will remain valid for use with opp_get_{voltage, freq} only while
324 * under the locked area. The pointer returned must be used prior to unlocking
325 * with rcu_read_unlock() to maintain the integrity of the pointer.
326 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500327struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
328 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200329{
330 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500331 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200332
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800333 opp_rcu_lockdep_assert();
334
Nishanth Menone1f60b22010-10-13 00:13:10 +0200335 if (!dev || !freq) {
336 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
337 return ERR_PTR(-EINVAL);
338 }
339
Nishanth Menon327854c2014-12-24 11:22:56 -0600340 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200341 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200342 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200343
344 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
345 if (temp_opp->available && temp_opp->rate >= *freq) {
346 opp = temp_opp;
347 *freq = opp->rate;
348 break;
349 }
350 }
351
352 return opp;
353}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500354EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200355
356/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500357 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200358 * @dev: device for which we do this operation
359 * @freq: Start frequency
360 *
361 * Search for the matching floor *available* OPP from a starting freq
362 * for a device.
363 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600364 * Return: matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200365 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
366 * values can be:
367 * EINVAL: for bad pointer
368 * ERANGE: no match found for search
369 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200370 *
371 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
372 * protected pointer. The reason for the same is that the opp pointer which is
373 * returned will remain valid for use with opp_get_{voltage, freq} only while
374 * under the locked area. The pointer returned must be used prior to unlocking
375 * with rcu_read_unlock() to maintain the integrity of the pointer.
376 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500377struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
378 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200379{
380 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500381 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200382
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800383 opp_rcu_lockdep_assert();
384
Nishanth Menone1f60b22010-10-13 00:13:10 +0200385 if (!dev || !freq) {
386 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
387 return ERR_PTR(-EINVAL);
388 }
389
Nishanth Menon327854c2014-12-24 11:22:56 -0600390 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200391 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200392 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200393
394 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
395 if (temp_opp->available) {
396 /* go to the next node, before choosing prev */
397 if (temp_opp->rate > *freq)
398 break;
399 else
400 opp = temp_opp;
401 }
402 }
403 if (!IS_ERR(opp))
404 *freq = opp->rate;
405
406 return opp;
407}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500408EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200409
Nishanth Menon984f16c2014-12-24 11:22:57 -0600410/**
411 * _add_device_opp() - Allocate a new device OPP table
412 * @dev: device for which we do this operation
413 *
414 * New device node which uses OPPs - used when multiple devices with OPP tables
415 * are maintained.
416 *
417 * Return: valid device_opp pointer if success, else NULL.
418 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600419static struct device_opp *_add_device_opp(struct device *dev)
Viresh Kumar07cce742014-12-10 09:45:34 +0530420{
421 struct device_opp *dev_opp;
422
423 /*
424 * Allocate a new device OPP table. In the infrequent case where a new
425 * device is needed to be added, we pay this penalty.
426 */
427 dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);
428 if (!dev_opp)
429 return NULL;
430
431 dev_opp->dev = dev;
432 srcu_init_notifier_head(&dev_opp->srcu_head);
433 INIT_LIST_HEAD(&dev_opp->opp_list);
434
435 /* Secure the device list modification */
436 list_add_rcu(&dev_opp->node, &dev_opp_list);
437 return dev_opp;
438}
439
Nishanth Menon984f16c2014-12-24 11:22:57 -0600440/**
Viresh Kumar737002b2015-07-29 16:22:58 +0530441 * _kfree_device_rcu() - Free device_opp RCU handler
442 * @head: RCU head
443 */
444static void _kfree_device_rcu(struct rcu_head *head)
445{
446 struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
447
448 kfree_rcu(device_opp, rcu_head);
449}
450
451/**
452 * _kfree_opp_rcu() - Free OPP RCU handler
453 * @head: RCU head
454 */
455static void _kfree_opp_rcu(struct rcu_head *head)
456{
457 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
458
459 kfree_rcu(opp, rcu_head);
460}
461
462/**
463 * _opp_remove() - Remove an OPP from a table definition
464 * @dev_opp: points back to the device_opp struct this opp belongs to
465 * @opp: pointer to the OPP to remove
466 *
467 * This function removes an opp definition from the opp list.
468 *
469 * Locking: The internal device_opp and opp structures are RCU protected.
470 * It is assumed that the caller holds required mutex for an RCU updater
471 * strategy.
472 */
473static void _opp_remove(struct device_opp *dev_opp,
474 struct dev_pm_opp *opp)
475{
476 /*
477 * Notify the changes in the availability of the operable
478 * frequency/voltage list.
479 */
480 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
481 list_del_rcu(&opp->node);
482 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
483
484 if (list_empty(&dev_opp->opp_list)) {
485 list_del_rcu(&dev_opp->node);
486 call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
487 _kfree_device_rcu);
488 }
489}
490
491/**
492 * dev_pm_opp_remove() - Remove an OPP from OPP list
493 * @dev: device for which we do this operation
494 * @freq: OPP to remove with matching 'freq'
495 *
496 * This function removes an opp from the opp list.
497 *
498 * Locking: The internal device_opp and opp structures are RCU protected.
499 * Hence this function internally uses RCU updater strategy with mutex locks
500 * to keep the integrity of the internal data structures. Callers should ensure
501 * that this function is *NOT* called under RCU protection or in contexts where
502 * mutex cannot be locked.
503 */
504void dev_pm_opp_remove(struct device *dev, unsigned long freq)
505{
506 struct dev_pm_opp *opp;
507 struct device_opp *dev_opp;
508 bool found = false;
509
510 /* Hold our list modification lock here */
511 mutex_lock(&dev_opp_list_lock);
512
513 dev_opp = _find_device_opp(dev);
514 if (IS_ERR(dev_opp))
515 goto unlock;
516
517 list_for_each_entry(opp, &dev_opp->opp_list, node) {
518 if (opp->rate == freq) {
519 found = true;
520 break;
521 }
522 }
523
524 if (!found) {
525 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
526 __func__, freq);
527 goto unlock;
528 }
529
530 _opp_remove(dev_opp, opp);
531unlock:
532 mutex_unlock(&dev_opp_list_lock);
533}
534EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
535
536/**
Nishanth Menon984f16c2014-12-24 11:22:57 -0600537 * _opp_add_dynamic() - Allocate a dynamic OPP.
538 * @dev: device for which we do this operation
539 * @freq: Frequency in Hz for this OPP
540 * @u_volt: Voltage in uVolts for this OPP
541 * @dynamic: Dynamically added OPPs.
542 *
543 * This function adds an opp definition to the opp list and returns status.
544 * The opp is made available by default and it can be controlled using
545 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
546 *
547 * NOTE: "dynamic" parameter impacts OPPs added by the of_init_opp_table and
548 * freed by of_free_opp_table.
549 *
550 * Locking: The internal device_opp and opp structures are RCU protected.
551 * Hence this function internally uses RCU updater strategy with mutex locks
552 * to keep the integrity of the internal data structures. Callers should ensure
553 * that this function is *NOT* called under RCU protection or in contexts where
554 * mutex cannot be locked.
555 *
556 * Return:
557 * 0 On success OR
558 * Duplicate OPPs (both freq and volt are same) and opp->available
559 * -EEXIST Freq are same and volt are different OR
560 * Duplicate OPPs (both freq and volt are same) and !opp->available
561 * -ENOMEM Memory allocation failure
562 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600563static int _opp_add_dynamic(struct device *dev, unsigned long freq,
564 long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200565{
566 struct device_opp *dev_opp = NULL;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500567 struct dev_pm_opp *opp, *new_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200568 struct list_head *head;
Viresh Kumar6ce41842014-12-10 09:45:35 +0530569 int ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200570
571 /* allocate new OPP node */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500572 new_opp = kzalloc(sizeof(*new_opp), GFP_KERNEL);
Quentin Lambert59d84ca2015-02-09 10:45:32 +0100573 if (!new_opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200574 return -ENOMEM;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200575
576 /* Hold our list modification lock here */
577 mutex_lock(&dev_opp_list_lock);
578
Viresh Kumara7470db2014-11-25 16:04:17 +0530579 /* populate the opp table */
Viresh Kumara7470db2014-11-25 16:04:17 +0530580 new_opp->rate = freq;
581 new_opp->u_volt = u_volt;
582 new_opp->available = true;
Viresh Kumar383934092014-11-25 16:04:18 +0530583 new_opp->dynamic = dynamic;
Viresh Kumara7470db2014-11-25 16:04:17 +0530584
Nishanth Menone1f60b22010-10-13 00:13:10 +0200585 /* Check for existing list for 'dev' */
Nishanth Menon327854c2014-12-24 11:22:56 -0600586 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200587 if (IS_ERR(dev_opp)) {
Nishanth Menon327854c2014-12-24 11:22:56 -0600588 dev_opp = _add_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200589 if (!dev_opp) {
Viresh Kumar6ce41842014-12-10 09:45:35 +0530590 ret = -ENOMEM;
591 goto free_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200592 }
593
Viresh Kumara7470db2014-11-25 16:04:17 +0530594 head = &dev_opp->opp_list;
595 goto list_add;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200596 }
597
Chander Kashyap64ce8542014-05-22 10:36:26 +0530598 /*
599 * Insert new OPP in order of increasing frequency
600 * and discard if already present
601 */
Nishanth Menone1f60b22010-10-13 00:13:10 +0200602 head = &dev_opp->opp_list;
603 list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
Chander Kashyap64ce8542014-05-22 10:36:26 +0530604 if (new_opp->rate <= opp->rate)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200605 break;
606 else
607 head = &opp->node;
608 }
609
Chander Kashyap64ce8542014-05-22 10:36:26 +0530610 /* Duplicate OPPs ? */
611 if (new_opp->rate == opp->rate) {
Viresh Kumar6ce41842014-12-10 09:45:35 +0530612 ret = opp->available && new_opp->u_volt == opp->u_volt ?
Chander Kashyap64ce8542014-05-22 10:36:26 +0530613 0 : -EEXIST;
614
615 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
616 __func__, opp->rate, opp->u_volt, opp->available,
617 new_opp->rate, new_opp->u_volt, new_opp->available);
Viresh Kumar6ce41842014-12-10 09:45:35 +0530618 goto free_opp;
Chander Kashyap64ce8542014-05-22 10:36:26 +0530619 }
620
Viresh Kumara7470db2014-11-25 16:04:17 +0530621list_add:
Viresh Kumar7284a002014-12-09 11:18:51 +0530622 new_opp->dev_opp = dev_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200623 list_add_rcu(&new_opp->node, head);
624 mutex_unlock(&dev_opp_list_lock);
625
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200626 /*
627 * Notify the changes in the availability of the operable
628 * frequency/voltage list.
629 */
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530630 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200631 return 0;
Viresh Kumar6ce41842014-12-10 09:45:35 +0530632
633free_opp:
634 mutex_unlock(&dev_opp_list_lock);
635 kfree(new_opp);
636 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200637}
Viresh Kumar383934092014-11-25 16:04:18 +0530638
639/**
640 * dev_pm_opp_add() - Add an OPP table from a table definitions
641 * @dev: device for which we do this operation
642 * @freq: Frequency in Hz for this OPP
643 * @u_volt: Voltage in uVolts for this OPP
644 *
645 * This function adds an opp definition to the opp list and returns status.
646 * The opp is made available by default and it can be controlled using
647 * dev_pm_opp_enable/disable functions.
648 *
649 * Locking: The internal device_opp and opp structures are RCU protected.
650 * Hence this function internally uses RCU updater strategy with mutex locks
651 * to keep the integrity of the internal data structures. Callers should ensure
652 * that this function is *NOT* called under RCU protection or in contexts where
653 * mutex cannot be locked.
654 *
655 * Return:
Nishanth Menon984f16c2014-12-24 11:22:57 -0600656 * 0 On success OR
Viresh Kumar383934092014-11-25 16:04:18 +0530657 * Duplicate OPPs (both freq and volt are same) and opp->available
Nishanth Menon984f16c2014-12-24 11:22:57 -0600658 * -EEXIST Freq are same and volt are different OR
Viresh Kumar383934092014-11-25 16:04:18 +0530659 * Duplicate OPPs (both freq and volt are same) and !opp->available
Nishanth Menon984f16c2014-12-24 11:22:57 -0600660 * -ENOMEM Memory allocation failure
Viresh Kumar383934092014-11-25 16:04:18 +0530661 */
662int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
663{
Nishanth Menon327854c2014-12-24 11:22:56 -0600664 return _opp_add_dynamic(dev, freq, u_volt, true);
Viresh Kumar383934092014-11-25 16:04:18 +0530665}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500666EXPORT_SYMBOL_GPL(dev_pm_opp_add);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200667
Nishanth Menon984f16c2014-12-24 11:22:57 -0600668/**
Nishanth Menon327854c2014-12-24 11:22:56 -0600669 * _opp_set_availability() - helper to set the availability of an opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200670 * @dev: device for which we do this operation
671 * @freq: OPP frequency to modify availability
672 * @availability_req: availability status requested for this opp
673 *
674 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
675 * share a common logic which is isolated here.
676 *
Nishanth Menon984f16c2014-12-24 11:22:57 -0600677 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
Nishanth Menone1f60b22010-10-13 00:13:10 +0200678 * copy operation, returns 0 if no modifcation was done OR modification was
679 * successful.
680 *
681 * Locking: The internal device_opp and opp structures are RCU protected.
682 * Hence this function internally uses RCU updater strategy with mutex locks to
683 * keep the integrity of the internal data structures. Callers should ensure
684 * that this function is *NOT* called under RCU protection or in contexts where
685 * mutex locking or synchronize_rcu() blocking calls cannot be used.
686 */
Nishanth Menon327854c2014-12-24 11:22:56 -0600687static int _opp_set_availability(struct device *dev, unsigned long freq,
688 bool availability_req)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200689{
Viresh Kumar29df0ee2014-12-10 09:45:33 +0530690 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500691 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200692 int r = 0;
693
694 /* keep the node allocated */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500695 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
Quentin Lambert59d84ca2015-02-09 10:45:32 +0100696 if (!new_opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200697 return -ENOMEM;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200698
699 mutex_lock(&dev_opp_list_lock);
700
701 /* Find the device_opp */
Nishanth Menon327854c2014-12-24 11:22:56 -0600702 dev_opp = _find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200703 if (IS_ERR(dev_opp)) {
704 r = PTR_ERR(dev_opp);
705 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
706 goto unlock;
707 }
708
709 /* Do we have the frequency? */
710 list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
711 if (tmp_opp->rate == freq) {
712 opp = tmp_opp;
713 break;
714 }
715 }
716 if (IS_ERR(opp)) {
717 r = PTR_ERR(opp);
718 goto unlock;
719 }
720
721 /* Is update really needed? */
722 if (opp->available == availability_req)
723 goto unlock;
724 /* copy the old data over */
725 *new_opp = *opp;
726
727 /* plug in new node */
728 new_opp->available = availability_req;
729
730 list_replace_rcu(&opp->node, &new_opp->node);
731 mutex_unlock(&dev_opp_list_lock);
Nishanth Menon327854c2014-12-24 11:22:56 -0600732 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200733
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200734 /* Notify the change of the OPP availability */
735 if (availability_req)
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530736 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200737 new_opp);
738 else
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530739 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200740 new_opp);
741
Vincent Guittotdde84372012-10-23 01:21:49 +0200742 return 0;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200743
744unlock:
745 mutex_unlock(&dev_opp_list_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200746 kfree(new_opp);
747 return r;
748}
749
750/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500751 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +0200752 * @dev: device for which we do this operation
753 * @freq: OPP frequency to enable
754 *
755 * Enables a provided opp. If the operation is valid, this returns 0, else the
756 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500757 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200758 *
759 * Locking: The internal device_opp and opp structures are RCU protected.
760 * Hence this function indirectly uses RCU and mutex locks to keep the
761 * integrity of the internal data structures. Callers should ensure that
762 * this function is *NOT* called under RCU protection or in contexts where
763 * mutex locking or synchronize_rcu() blocking calls cannot be used.
Nishanth Menon984f16c2014-12-24 11:22:57 -0600764 *
765 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
766 * copy operation, returns 0 if no modifcation was done OR modification was
767 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200768 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500769int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200770{
Nishanth Menon327854c2014-12-24 11:22:56 -0600771 return _opp_set_availability(dev, freq, true);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200772}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500773EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200774
775/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500776 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +0200777 * @dev: device for which we do this operation
778 * @freq: OPP frequency to disable
779 *
780 * Disables a provided opp. If the operation is valid, this returns
781 * 0, else the corresponding error value. It is meant to be a temporary
782 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500783 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +0200784 *
785 * Locking: The internal device_opp and opp structures are RCU protected.
786 * Hence this function indirectly uses RCU and mutex locks to keep the
787 * integrity of the internal data structures. Callers should ensure that
788 * this function is *NOT* called under RCU protection or in contexts where
789 * mutex locking or synchronize_rcu() blocking calls cannot be used.
Nishanth Menon984f16c2014-12-24 11:22:57 -0600790 *
791 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
792 * copy operation, returns 0 if no modifcation was done OR modification was
793 * successful.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200794 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500795int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200796{
Nishanth Menon327854c2014-12-24 11:22:56 -0600797 return _opp_set_availability(dev, freq, false);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200798}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500799EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200800
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200801/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500802 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200803 * @dev: device pointer used to lookup device OPPs.
Nishanth Menon984f16c2014-12-24 11:22:57 -0600804 *
805 * Return: pointer to notifier head if found, otherwise -ENODEV or
806 * -EINVAL based on type of error casted as pointer. value must be checked
807 * with IS_ERR to determine valid pointer or error result.
808 *
809 * Locking: This function must be called under rcu_read_lock(). dev_opp is a RCU
810 * protected pointer. The reason for the same is that the opp pointer which is
811 * returned will remain valid for use with opp_get_{voltage, freq} only while
812 * under the locked area. The pointer returned must be used prior to unlocking
813 * with rcu_read_unlock() to maintain the integrity of the pointer.
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200814 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500815struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200816{
Nishanth Menon327854c2014-12-24 11:22:56 -0600817 struct device_opp *dev_opp = _find_device_opp(dev);
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200818
819 if (IS_ERR(dev_opp))
Thomas Meyer156acb12011-11-08 22:34:00 +0100820 return ERR_CAST(dev_opp); /* matching type */
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200821
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530822 return &dev_opp->srcu_head;
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200823}
Nishanth Menon4679ec32014-12-24 11:22:55 -0600824EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
Shawn Guob496dfb2012-09-05 01:09:12 +0200825
826#ifdef CONFIG_OF
827/**
Viresh Kumar737002b2015-07-29 16:22:58 +0530828 * of_free_opp_table() - Free OPP table entries created from static DT entries
829 * @dev: device pointer used to lookup device OPPs.
830 *
831 * Free OPPs created using static entries present in DT.
832 *
833 * Locking: The internal device_opp and opp structures are RCU protected.
834 * Hence this function indirectly uses RCU updater strategy with mutex locks
835 * to keep the integrity of the internal data structures. Callers should ensure
836 * that this function is *NOT* called under RCU protection or in contexts where
837 * mutex cannot be locked.
838 */
839void of_free_opp_table(struct device *dev)
840{
841 struct device_opp *dev_opp;
842 struct dev_pm_opp *opp, *tmp;
843
844 /* Check for existing list for 'dev' */
845 dev_opp = _find_device_opp(dev);
846 if (IS_ERR(dev_opp)) {
847 int error = PTR_ERR(dev_opp);
848
849 if (error != -ENODEV)
850 WARN(1, "%s: dev_opp: %d\n",
851 IS_ERR_OR_NULL(dev) ?
852 "Invalid device" : dev_name(dev),
853 error);
854 return;
855 }
856
857 /* Hold our list modification lock here */
858 mutex_lock(&dev_opp_list_lock);
859
860 /* Free static OPPs */
861 list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
862 if (!opp->dynamic)
863 _opp_remove(dev_opp, opp);
864 }
865
866 mutex_unlock(&dev_opp_list_lock);
867}
868EXPORT_SYMBOL_GPL(of_free_opp_table);
869
870/**
Shawn Guob496dfb2012-09-05 01:09:12 +0200871 * of_init_opp_table() - Initialize opp table from device tree
872 * @dev: device pointer used to lookup device OPPs.
873 *
874 * Register the initial OPP table with the OPP library for given device.
Nishanth Menon984f16c2014-12-24 11:22:57 -0600875 *
876 * Locking: The internal device_opp and opp structures are RCU protected.
877 * Hence this function indirectly uses RCU updater strategy with mutex locks
878 * to keep the integrity of the internal data structures. Callers should ensure
879 * that this function is *NOT* called under RCU protection or in contexts where
880 * mutex cannot be locked.
881 *
882 * Return:
883 * 0 On success OR
884 * Duplicate OPPs (both freq and volt are same) and opp->available
885 * -EEXIST Freq are same and volt are different OR
886 * Duplicate OPPs (both freq and volt are same) and !opp->available
887 * -ENOMEM Memory allocation failure
888 * -ENODEV when 'operating-points' property is not found or is invalid data
889 * in device node.
890 * -ENODATA when empty 'operating-points' property is found
Shawn Guob496dfb2012-09-05 01:09:12 +0200891 */
892int of_init_opp_table(struct device *dev)
893{
894 const struct property *prop;
895 const __be32 *val;
896 int nr;
897
898 prop = of_find_property(dev->of_node, "operating-points", NULL);
899 if (!prop)
900 return -ENODEV;
901 if (!prop->value)
902 return -ENODATA;
903
904 /*
905 * Each OPP is a set of tuples consisting of frequency and
906 * voltage like <freq-kHz vol-uV>.
907 */
908 nr = prop->length / sizeof(u32);
909 if (nr % 2) {
910 dev_err(dev, "%s: Invalid OPP list\n", __func__);
911 return -EINVAL;
912 }
913
914 val = prop->value;
915 while (nr) {
916 unsigned long freq = be32_to_cpup(val++) * 1000;
917 unsigned long volt = be32_to_cpup(val++);
918
Nishanth Menon327854c2014-12-24 11:22:56 -0600919 if (_opp_add_dynamic(dev, freq, volt, false))
Shawn Guob496dfb2012-09-05 01:09:12 +0200920 dev_warn(dev, "%s: Failed to add OPP %ld\n",
921 __func__, freq);
Shawn Guob496dfb2012-09-05 01:09:12 +0200922 nr -= 2;
923 }
924
925 return 0;
926}
Mark Langsdorf74c46c62013-01-28 18:26:16 +0000927EXPORT_SYMBOL_GPL(of_init_opp_table);
Shawn Guob496dfb2012-09-05 01:09:12 +0200928#endif