blob: b78c14d30da2c095d587f158f494aea2fe2038ea [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/**
120 * find_device_opp() - find device_opp struct using device pointer
121 * @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 *
126 * Returns pointer to 'struct device_opp' if found, otherwise -ENODEV or
127 * -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 */
133static struct device_opp *find_device_opp(struct device *dev)
134{
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 *
156 * Return voltage in micro volt corresponding to the opp, else
157 * 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
172 tmp_opp = rcu_dereference(opp);
173 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
174 pr_err("%s: Invalid parameters\n", __func__);
175 else
176 v = tmp_opp->u_volt;
177
178 return v;
179}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500180EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200181
182/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500183 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200184 * @opp: opp for which frequency has to be returned for
185 *
186 * Return frequency in hertz corresponding to the opp, else
187 * return 0
188 *
189 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
190 * protected pointer. This means that opp which could have been fetched by
191 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
192 * under RCU lock. The pointer returned by the opp_find_freq family must be
193 * used in the same section as the usage of this function with the pointer
194 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
195 * pointer.
196 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500197unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200198{
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500199 struct dev_pm_opp *tmp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200200 unsigned long f = 0;
201
202 tmp_opp = rcu_dereference(opp);
203 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
204 pr_err("%s: Invalid parameters\n", __func__);
205 else
206 f = tmp_opp->rate;
207
208 return f;
209}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500210EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200211
212/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500213 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
Nishanth Menone1f60b22010-10-13 00:13:10 +0200214 * @dev: device for which we do this operation
215 *
216 * This function returns the number of available opps if there are any,
217 * else returns 0 if none or the corresponding error value.
218 *
219 * Locking: This function must be called under rcu_read_lock(). This function
220 * internally references two RCU protected structures: device_opp and opp which
221 * are safe as long as we are under a common RCU locked section.
222 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500223int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200224{
225 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500226 struct dev_pm_opp *temp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200227 int count = 0;
228
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800229 opp_rcu_lockdep_assert();
230
Nishanth Menone1f60b22010-10-13 00:13:10 +0200231 dev_opp = find_device_opp(dev);
232 if (IS_ERR(dev_opp)) {
233 int r = PTR_ERR(dev_opp);
234 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
235 return r;
236 }
237
238 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
239 if (temp_opp->available)
240 count++;
241 }
242
243 return count;
244}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500245EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200246
247/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500248 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200249 * @dev: device for which we do this operation
250 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100251 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200252 *
253 * Searches for exact match in the opp list and returns pointer to the matching
254 * opp if found, else returns ERR_PTR in case of error and should be handled
Nishanth Menon07797262012-10-24 22:00:12 +0200255 * using IS_ERR. Error return values can be:
256 * EINVAL: for bad pointer
257 * ERANGE: no match found for search
258 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200259 *
260 * Note: available is a modifier for the search. if available=true, then the
261 * match is for exact matching frequency and is available in the stored OPP
262 * table. if false, the match is for exact frequency which is not available.
263 *
264 * This provides a mechanism to enable an opp which is not available currently
265 * or the opposite as well.
266 *
267 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
268 * protected pointer. The reason for the same is that the opp pointer which is
269 * returned will remain valid for use with opp_get_{voltage, freq} only while
270 * under the locked area. The pointer returned must be used prior to unlocking
271 * with rcu_read_unlock() to maintain the integrity of the pointer.
272 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500273struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
274 unsigned long freq,
275 bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200276{
277 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500278 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200279
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800280 opp_rcu_lockdep_assert();
281
Nishanth Menone1f60b22010-10-13 00:13:10 +0200282 dev_opp = find_device_opp(dev);
283 if (IS_ERR(dev_opp)) {
284 int r = PTR_ERR(dev_opp);
285 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
286 return ERR_PTR(r);
287 }
288
289 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
290 if (temp_opp->available == available &&
291 temp_opp->rate == freq) {
292 opp = temp_opp;
293 break;
294 }
295 }
296
297 return opp;
298}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500299EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200300
301/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500302 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200303 * @dev: device for which we do this operation
304 * @freq: Start frequency
305 *
306 * Search for the matching ceil *available* OPP from a starting freq
307 * for a device.
308 *
309 * Returns matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200310 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
311 * values can be:
312 * EINVAL: for bad pointer
313 * ERANGE: no match found for search
314 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200315 *
316 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
317 * protected pointer. The reason for the same is that the opp pointer which is
318 * returned will remain valid for use with opp_get_{voltage, freq} only while
319 * under the locked area. The pointer returned must be used prior to unlocking
320 * with rcu_read_unlock() to maintain the integrity of the pointer.
321 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500322struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
323 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200324{
325 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500326 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200327
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800328 opp_rcu_lockdep_assert();
329
Nishanth Menone1f60b22010-10-13 00:13:10 +0200330 if (!dev || !freq) {
331 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
332 return ERR_PTR(-EINVAL);
333 }
334
335 dev_opp = find_device_opp(dev);
336 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200337 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200338
339 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
340 if (temp_opp->available && temp_opp->rate >= *freq) {
341 opp = temp_opp;
342 *freq = opp->rate;
343 break;
344 }
345 }
346
347 return opp;
348}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500349EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200350
351/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500352 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200353 * @dev: device for which we do this operation
354 * @freq: Start frequency
355 *
356 * Search for the matching floor *available* OPP from a starting freq
357 * for a device.
358 *
359 * Returns matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200360 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
361 * values can be:
362 * EINVAL: for bad pointer
363 * ERANGE: no match found for search
364 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200365 *
366 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
367 * protected pointer. The reason for the same is that the opp pointer which is
368 * returned will remain valid for use with opp_get_{voltage, freq} only while
369 * under the locked area. The pointer returned must be used prior to unlocking
370 * with rcu_read_unlock() to maintain the integrity of the pointer.
371 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500372struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
373 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200374{
375 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500376 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200377
Dmitry Torokhovb02ded22014-12-16 15:09:36 -0800378 opp_rcu_lockdep_assert();
379
Nishanth Menone1f60b22010-10-13 00:13:10 +0200380 if (!dev || !freq) {
381 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
382 return ERR_PTR(-EINVAL);
383 }
384
385 dev_opp = find_device_opp(dev);
386 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200387 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200388
389 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
390 if (temp_opp->available) {
391 /* go to the next node, before choosing prev */
392 if (temp_opp->rate > *freq)
393 break;
394 else
395 opp = temp_opp;
396 }
397 }
398 if (!IS_ERR(opp))
399 *freq = opp->rate;
400
401 return opp;
402}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500403EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200404
Viresh Kumar07cce742014-12-10 09:45:34 +0530405static struct device_opp *add_device_opp(struct device *dev)
406{
407 struct device_opp *dev_opp;
408
409 /*
410 * Allocate a new device OPP table. In the infrequent case where a new
411 * device is needed to be added, we pay this penalty.
412 */
413 dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);
414 if (!dev_opp)
415 return NULL;
416
417 dev_opp->dev = dev;
418 srcu_init_notifier_head(&dev_opp->srcu_head);
419 INIT_LIST_HEAD(&dev_opp->opp_list);
420
421 /* Secure the device list modification */
422 list_add_rcu(&dev_opp->node, &dev_opp_list);
423 return dev_opp;
424}
425
Viresh Kumar383934092014-11-25 16:04:18 +0530426static int dev_pm_opp_add_dynamic(struct device *dev, unsigned long freq,
427 unsigned long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200428{
429 struct device_opp *dev_opp = NULL;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500430 struct dev_pm_opp *opp, *new_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200431 struct list_head *head;
Viresh Kumar6ce41842014-12-10 09:45:35 +0530432 int ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200433
434 /* allocate new OPP node */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500435 new_opp = kzalloc(sizeof(*new_opp), GFP_KERNEL);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200436 if (!new_opp) {
437 dev_warn(dev, "%s: Unable to create new OPP node\n", __func__);
438 return -ENOMEM;
439 }
440
441 /* Hold our list modification lock here */
442 mutex_lock(&dev_opp_list_lock);
443
Viresh Kumara7470db2014-11-25 16:04:17 +0530444 /* populate the opp table */
Viresh Kumara7470db2014-11-25 16:04:17 +0530445 new_opp->rate = freq;
446 new_opp->u_volt = u_volt;
447 new_opp->available = true;
Viresh Kumar383934092014-11-25 16:04:18 +0530448 new_opp->dynamic = dynamic;
Viresh Kumara7470db2014-11-25 16:04:17 +0530449
Nishanth Menone1f60b22010-10-13 00:13:10 +0200450 /* Check for existing list for 'dev' */
451 dev_opp = find_device_opp(dev);
452 if (IS_ERR(dev_opp)) {
Viresh Kumar07cce742014-12-10 09:45:34 +0530453 dev_opp = add_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200454 if (!dev_opp) {
Viresh Kumar6ce41842014-12-10 09:45:35 +0530455 ret = -ENOMEM;
456 goto free_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200457 }
458
Viresh Kumara7470db2014-11-25 16:04:17 +0530459 head = &dev_opp->opp_list;
460 goto list_add;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200461 }
462
Chander Kashyap64ce8542014-05-22 10:36:26 +0530463 /*
464 * Insert new OPP in order of increasing frequency
465 * and discard if already present
466 */
Nishanth Menone1f60b22010-10-13 00:13:10 +0200467 head = &dev_opp->opp_list;
468 list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
Chander Kashyap64ce8542014-05-22 10:36:26 +0530469 if (new_opp->rate <= opp->rate)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200470 break;
471 else
472 head = &opp->node;
473 }
474
Chander Kashyap64ce8542014-05-22 10:36:26 +0530475 /* Duplicate OPPs ? */
476 if (new_opp->rate == opp->rate) {
Viresh Kumar6ce41842014-12-10 09:45:35 +0530477 ret = opp->available && new_opp->u_volt == opp->u_volt ?
Chander Kashyap64ce8542014-05-22 10:36:26 +0530478 0 : -EEXIST;
479
480 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
481 __func__, opp->rate, opp->u_volt, opp->available,
482 new_opp->rate, new_opp->u_volt, new_opp->available);
Viresh Kumar6ce41842014-12-10 09:45:35 +0530483 goto free_opp;
Chander Kashyap64ce8542014-05-22 10:36:26 +0530484 }
485
Viresh Kumara7470db2014-11-25 16:04:17 +0530486list_add:
Viresh Kumar7284a002014-12-09 11:18:51 +0530487 new_opp->dev_opp = dev_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200488 list_add_rcu(&new_opp->node, head);
489 mutex_unlock(&dev_opp_list_lock);
490
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200491 /*
492 * Notify the changes in the availability of the operable
493 * frequency/voltage list.
494 */
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530495 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200496 return 0;
Viresh Kumar6ce41842014-12-10 09:45:35 +0530497
498free_opp:
499 mutex_unlock(&dev_opp_list_lock);
500 kfree(new_opp);
501 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200502}
Viresh Kumar383934092014-11-25 16:04:18 +0530503
504/**
505 * dev_pm_opp_add() - Add an OPP table from a table definitions
506 * @dev: device for which we do this operation
507 * @freq: Frequency in Hz for this OPP
508 * @u_volt: Voltage in uVolts for this OPP
509 *
510 * This function adds an opp definition to the opp list and returns status.
511 * The opp is made available by default and it can be controlled using
512 * dev_pm_opp_enable/disable functions.
513 *
514 * Locking: The internal device_opp and opp structures are RCU protected.
515 * Hence this function internally uses RCU updater strategy with mutex locks
516 * to keep the integrity of the internal data structures. Callers should ensure
517 * that this function is *NOT* called under RCU protection or in contexts where
518 * mutex cannot be locked.
519 *
520 * Return:
521 * 0: On success OR
522 * Duplicate OPPs (both freq and volt are same) and opp->available
523 * -EEXIST: Freq are same and volt are different OR
524 * Duplicate OPPs (both freq and volt are same) and !opp->available
525 * -ENOMEM: Memory allocation failure
526 */
527int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
528{
529 return dev_pm_opp_add_dynamic(dev, freq, u_volt, true);
530}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500531EXPORT_SYMBOL_GPL(dev_pm_opp_add);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200532
Viresh Kumar129eec52014-11-27 08:54:06 +0530533static void kfree_opp_rcu(struct rcu_head *head)
534{
535 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
536
537 kfree_rcu(opp, rcu_head);
538}
539
540static void kfree_device_rcu(struct rcu_head *head)
541{
542 struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
543
Viresh Kumar1c6a6622014-12-10 09:45:31 +0530544 kfree_rcu(device_opp, rcu_head);
Viresh Kumar129eec52014-11-27 08:54:06 +0530545}
546
Viresh Kumar86453b42014-12-10 09:45:32 +0530547static void __dev_pm_opp_remove(struct device_opp *dev_opp,
548 struct dev_pm_opp *opp)
Viresh Kumar129eec52014-11-27 08:54:06 +0530549{
550 /*
551 * Notify the changes in the availability of the operable
552 * frequency/voltage list.
553 */
554 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
555 list_del_rcu(&opp->node);
556 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, kfree_opp_rcu);
557
558 if (list_empty(&dev_opp->opp_list)) {
559 list_del_rcu(&dev_opp->node);
560 call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
561 kfree_device_rcu);
562 }
563}
564
565/**
566 * dev_pm_opp_remove() - Remove an OPP from OPP list
567 * @dev: device for which we do this operation
568 * @freq: OPP to remove with matching 'freq'
569 *
570 * This function removes an opp from the opp list.
571 */
572void dev_pm_opp_remove(struct device *dev, unsigned long freq)
573{
574 struct dev_pm_opp *opp;
575 struct device_opp *dev_opp;
576 bool found = false;
577
578 /* Hold our list modification lock here */
579 mutex_lock(&dev_opp_list_lock);
580
581 dev_opp = find_device_opp(dev);
582 if (IS_ERR(dev_opp))
583 goto unlock;
584
585 list_for_each_entry(opp, &dev_opp->opp_list, node) {
586 if (opp->rate == freq) {
587 found = true;
588 break;
589 }
590 }
591
592 if (!found) {
593 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
594 __func__, freq);
595 goto unlock;
596 }
597
598 __dev_pm_opp_remove(dev_opp, opp);
599unlock:
600 mutex_unlock(&dev_opp_list_lock);
601}
602EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
603
Nishanth Menone1f60b22010-10-13 00:13:10 +0200604/**
605 * opp_set_availability() - helper to set the availability of an opp
606 * @dev: device for which we do this operation
607 * @freq: OPP frequency to modify availability
608 * @availability_req: availability status requested for this opp
609 *
610 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
611 * share a common logic which is isolated here.
612 *
613 * Returns -EINVAL for bad pointers, -ENOMEM if no memory available for the
614 * copy operation, returns 0 if no modifcation was done OR modification was
615 * successful.
616 *
617 * Locking: The internal device_opp and opp structures are RCU protected.
618 * Hence this function internally uses RCU updater strategy with mutex locks to
619 * keep the integrity of the internal data structures. Callers should ensure
620 * that this function is *NOT* called under RCU protection or in contexts where
621 * mutex locking or synchronize_rcu() blocking calls cannot be used.
622 */
623static int opp_set_availability(struct device *dev, unsigned long freq,
624 bool availability_req)
625{
Viresh Kumar29df0ee2014-12-10 09:45:33 +0530626 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500627 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200628 int r = 0;
629
630 /* keep the node allocated */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500631 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200632 if (!new_opp) {
633 dev_warn(dev, "%s: Unable to create OPP\n", __func__);
634 return -ENOMEM;
635 }
636
637 mutex_lock(&dev_opp_list_lock);
638
639 /* Find the device_opp */
Viresh Kumar29df0ee2014-12-10 09:45:33 +0530640 dev_opp = find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200641 if (IS_ERR(dev_opp)) {
642 r = PTR_ERR(dev_opp);
643 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
644 goto unlock;
645 }
646
647 /* Do we have the frequency? */
648 list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
649 if (tmp_opp->rate == freq) {
650 opp = tmp_opp;
651 break;
652 }
653 }
654 if (IS_ERR(opp)) {
655 r = PTR_ERR(opp);
656 goto unlock;
657 }
658
659 /* Is update really needed? */
660 if (opp->available == availability_req)
661 goto unlock;
662 /* copy the old data over */
663 *new_opp = *opp;
664
665 /* plug in new node */
666 new_opp->available = availability_req;
667
668 list_replace_rcu(&opp->node, &new_opp->node);
669 mutex_unlock(&dev_opp_list_lock);
Viresh Kumarb4037aa2014-11-27 08:54:07 +0530670 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, kfree_opp_rcu);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200671
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200672 /* Notify the change of the OPP availability */
673 if (availability_req)
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530674 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200675 new_opp);
676 else
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530677 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200678 new_opp);
679
Vincent Guittotdde84372012-10-23 01:21:49 +0200680 return 0;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200681
682unlock:
683 mutex_unlock(&dev_opp_list_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200684 kfree(new_opp);
685 return r;
686}
687
688/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500689 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +0200690 * @dev: device for which we do this operation
691 * @freq: OPP frequency to enable
692 *
693 * Enables a provided opp. If the operation is valid, this returns 0, else the
694 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500695 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200696 *
697 * Locking: The internal device_opp and opp structures are RCU protected.
698 * Hence this function indirectly uses RCU and mutex locks to keep the
699 * integrity of the internal data structures. Callers should ensure that
700 * this function is *NOT* called under RCU protection or in contexts where
701 * mutex locking or synchronize_rcu() blocking calls cannot be used.
702 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500703int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200704{
705 return opp_set_availability(dev, freq, true);
706}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500707EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200708
709/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500710 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +0200711 * @dev: device for which we do this operation
712 * @freq: OPP frequency to disable
713 *
714 * Disables a provided opp. If the operation is valid, this returns
715 * 0, else the corresponding error value. It is meant to be a temporary
716 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500717 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +0200718 *
719 * Locking: The internal device_opp and opp structures are RCU protected.
720 * Hence this function indirectly uses RCU and mutex locks to keep the
721 * integrity of the internal data structures. Callers should ensure that
722 * this function is *NOT* called under RCU protection or in contexts where
723 * mutex locking or synchronize_rcu() blocking calls cannot be used.
724 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500725int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200726{
727 return opp_set_availability(dev, freq, false);
728}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500729EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200730
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200731/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500732 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200733 * @dev: device pointer used to lookup device OPPs.
734 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500735struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200736{
737 struct device_opp *dev_opp = find_device_opp(dev);
738
739 if (IS_ERR(dev_opp))
Thomas Meyer156acb12011-11-08 22:34:00 +0100740 return ERR_CAST(dev_opp); /* matching type */
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200741
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530742 return &dev_opp->srcu_head;
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200743}
Shawn Guob496dfb2012-09-05 01:09:12 +0200744
745#ifdef CONFIG_OF
746/**
747 * of_init_opp_table() - Initialize opp table from device tree
748 * @dev: device pointer used to lookup device OPPs.
749 *
750 * Register the initial OPP table with the OPP library for given device.
751 */
752int of_init_opp_table(struct device *dev)
753{
754 const struct property *prop;
755 const __be32 *val;
756 int nr;
757
758 prop = of_find_property(dev->of_node, "operating-points", NULL);
759 if (!prop)
760 return -ENODEV;
761 if (!prop->value)
762 return -ENODATA;
763
764 /*
765 * Each OPP is a set of tuples consisting of frequency and
766 * voltage like <freq-kHz vol-uV>.
767 */
768 nr = prop->length / sizeof(u32);
769 if (nr % 2) {
770 dev_err(dev, "%s: Invalid OPP list\n", __func__);
771 return -EINVAL;
772 }
773
774 val = prop->value;
775 while (nr) {
776 unsigned long freq = be32_to_cpup(val++) * 1000;
777 unsigned long volt = be32_to_cpup(val++);
778
Viresh Kumar383934092014-11-25 16:04:18 +0530779 if (dev_pm_opp_add_dynamic(dev, freq, volt, false))
Shawn Guob496dfb2012-09-05 01:09:12 +0200780 dev_warn(dev, "%s: Failed to add OPP %ld\n",
781 __func__, freq);
Shawn Guob496dfb2012-09-05 01:09:12 +0200782 nr -= 2;
783 }
784
785 return 0;
786}
Mark Langsdorf74c46c62013-01-28 18:26:16 +0000787EXPORT_SYMBOL_GPL(of_init_opp_table);
Viresh Kumar129eec52014-11-27 08:54:06 +0530788
789/**
790 * of_free_opp_table() - Free OPP table entries created from static DT entries
791 * @dev: device pointer used to lookup device OPPs.
792 *
793 * Free OPPs created using static entries present in DT.
794 */
795void of_free_opp_table(struct device *dev)
796{
Viresh Kumar2a6127d02014-12-08 13:46:18 +0530797 struct device_opp *dev_opp;
Viresh Kumar129eec52014-11-27 08:54:06 +0530798 struct dev_pm_opp *opp, *tmp;
799
800 /* Check for existing list for 'dev' */
801 dev_opp = find_device_opp(dev);
802 if (WARN(IS_ERR(dev_opp), "%s: dev_opp: %ld\n", dev_name(dev),
803 PTR_ERR(dev_opp)))
804 return;
805
806 /* Hold our list modification lock here */
807 mutex_lock(&dev_opp_list_lock);
808
809 /* Free static OPPs */
810 list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
811 if (!opp->dynamic)
812 __dev_pm_opp_remove(dev_opp, opp);
813 }
814
815 mutex_unlock(&dev_opp_list_lock);
816}
817EXPORT_SYMBOL_GPL(of_free_opp_table);
Shawn Guob496dfb2012-09-05 01:09:12 +0200818#endif