blob: d24dd614a0bd3208c0d88fd79c35397db5be8a71 [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
111/**
112 * find_device_opp() - find device_opp struct using device pointer
113 * @dev: device pointer used to lookup device OPPs
114 *
115 * Search list of device OPPs for one containing matching device. Does a RCU
116 * reader operation to grab the pointer needed.
117 *
118 * Returns pointer to 'struct device_opp' if found, otherwise -ENODEV or
119 * -EINVAL based on type of error.
120 *
121 * Locking: This function must be called under rcu_read_lock(). device_opp
122 * is a RCU protected pointer. This means that device_opp is valid as long
123 * as we are under RCU lock.
124 */
125static struct device_opp *find_device_opp(struct device *dev)
126{
127 struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV);
128
129 if (unlikely(IS_ERR_OR_NULL(dev))) {
130 pr_err("%s: Invalid parameters\n", __func__);
131 return ERR_PTR(-EINVAL);
132 }
133
134 list_for_each_entry_rcu(tmp_dev_opp, &dev_opp_list, node) {
135 if (tmp_dev_opp->dev == dev) {
136 dev_opp = tmp_dev_opp;
137 break;
138 }
139 }
140
141 return dev_opp;
142}
143
144/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500145 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200146 * @opp: opp for which voltage has to be returned for
147 *
148 * Return voltage in micro volt corresponding to the opp, else
149 * return 0
150 *
151 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
152 * protected pointer. This means that opp which could have been fetched by
153 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
154 * under RCU lock. The pointer returned by the opp_find_freq family must be
155 * used in the same section as the usage of this function with the pointer
156 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
157 * pointer.
158 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500159unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200160{
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500161 struct dev_pm_opp *tmp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200162 unsigned long v = 0;
163
164 tmp_opp = rcu_dereference(opp);
165 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
166 pr_err("%s: Invalid parameters\n", __func__);
167 else
168 v = tmp_opp->u_volt;
169
170 return v;
171}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500172EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200173
174/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500175 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200176 * @opp: opp for which frequency has to be returned for
177 *
178 * Return frequency in hertz corresponding to the opp, else
179 * return 0
180 *
181 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
182 * protected pointer. This means that opp which could have been fetched by
183 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
184 * under RCU lock. The pointer returned by the opp_find_freq family must be
185 * used in the same section as the usage of this function with the pointer
186 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
187 * pointer.
188 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500189unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200190{
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500191 struct dev_pm_opp *tmp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200192 unsigned long f = 0;
193
194 tmp_opp = rcu_dereference(opp);
195 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
196 pr_err("%s: Invalid parameters\n", __func__);
197 else
198 f = tmp_opp->rate;
199
200 return f;
201}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500202EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200203
204/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500205 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
Nishanth Menone1f60b22010-10-13 00:13:10 +0200206 * @dev: device for which we do this operation
207 *
208 * This function returns the number of available opps if there are any,
209 * else returns 0 if none or the corresponding error value.
210 *
211 * Locking: This function must be called under rcu_read_lock(). This function
212 * internally references two RCU protected structures: device_opp and opp which
213 * are safe as long as we are under a common RCU locked section.
214 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500215int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200216{
217 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500218 struct dev_pm_opp *temp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200219 int count = 0;
220
221 dev_opp = find_device_opp(dev);
222 if (IS_ERR(dev_opp)) {
223 int r = PTR_ERR(dev_opp);
224 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
225 return r;
226 }
227
228 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
229 if (temp_opp->available)
230 count++;
231 }
232
233 return count;
234}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500235EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200236
237/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500238 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200239 * @dev: device for which we do this operation
240 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100241 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200242 *
243 * Searches for exact match in the opp list and returns pointer to the matching
244 * opp if found, else returns ERR_PTR in case of error and should be handled
Nishanth Menon07797262012-10-24 22:00:12 +0200245 * using IS_ERR. Error return values can be:
246 * EINVAL: for bad pointer
247 * ERANGE: no match found for search
248 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200249 *
250 * Note: available is a modifier for the search. if available=true, then the
251 * match is for exact matching frequency and is available in the stored OPP
252 * table. if false, the match is for exact frequency which is not available.
253 *
254 * This provides a mechanism to enable an opp which is not available currently
255 * or the opposite as well.
256 *
257 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
258 * protected pointer. The reason for the same is that the opp pointer which is
259 * returned will remain valid for use with opp_get_{voltage, freq} only while
260 * under the locked area. The pointer returned must be used prior to unlocking
261 * with rcu_read_unlock() to maintain the integrity of the pointer.
262 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500263struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
264 unsigned long freq,
265 bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200266{
267 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500268 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200269
270 dev_opp = find_device_opp(dev);
271 if (IS_ERR(dev_opp)) {
272 int r = PTR_ERR(dev_opp);
273 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
274 return ERR_PTR(r);
275 }
276
277 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
278 if (temp_opp->available == available &&
279 temp_opp->rate == freq) {
280 opp = temp_opp;
281 break;
282 }
283 }
284
285 return opp;
286}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500287EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200288
289/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500290 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200291 * @dev: device for which we do this operation
292 * @freq: Start frequency
293 *
294 * Search for the matching ceil *available* OPP from a starting freq
295 * for a device.
296 *
297 * Returns matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200298 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
299 * values can be:
300 * EINVAL: for bad pointer
301 * ERANGE: no match found for search
302 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200303 *
304 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
305 * protected pointer. The reason for the same is that the opp pointer which is
306 * returned will remain valid for use with opp_get_{voltage, freq} only while
307 * under the locked area. The pointer returned must be used prior to unlocking
308 * with rcu_read_unlock() to maintain the integrity of the pointer.
309 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500310struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
311 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200312{
313 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500314 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200315
316 if (!dev || !freq) {
317 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
318 return ERR_PTR(-EINVAL);
319 }
320
321 dev_opp = find_device_opp(dev);
322 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200323 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200324
325 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
326 if (temp_opp->available && temp_opp->rate >= *freq) {
327 opp = temp_opp;
328 *freq = opp->rate;
329 break;
330 }
331 }
332
333 return opp;
334}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500335EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200336
337/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500338 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200339 * @dev: device for which we do this operation
340 * @freq: Start frequency
341 *
342 * Search for the matching floor *available* OPP from a starting freq
343 * for a device.
344 *
345 * Returns matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200346 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
347 * values can be:
348 * EINVAL: for bad pointer
349 * ERANGE: no match found for search
350 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200351 *
352 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
353 * protected pointer. The reason for the same is that the opp pointer which is
354 * returned will remain valid for use with opp_get_{voltage, freq} only while
355 * under the locked area. The pointer returned must be used prior to unlocking
356 * with rcu_read_unlock() to maintain the integrity of the pointer.
357 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500358struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
359 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200360{
361 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500362 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200363
364 if (!dev || !freq) {
365 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
366 return ERR_PTR(-EINVAL);
367 }
368
369 dev_opp = find_device_opp(dev);
370 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200371 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200372
373 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
374 if (temp_opp->available) {
375 /* go to the next node, before choosing prev */
376 if (temp_opp->rate > *freq)
377 break;
378 else
379 opp = temp_opp;
380 }
381 }
382 if (!IS_ERR(opp))
383 *freq = opp->rate;
384
385 return opp;
386}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500387EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200388
Viresh Kumar07cce742014-12-10 09:45:34 +0530389static struct device_opp *add_device_opp(struct device *dev)
390{
391 struct device_opp *dev_opp;
392
393 /*
394 * Allocate a new device OPP table. In the infrequent case where a new
395 * device is needed to be added, we pay this penalty.
396 */
397 dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL);
398 if (!dev_opp)
399 return NULL;
400
401 dev_opp->dev = dev;
402 srcu_init_notifier_head(&dev_opp->srcu_head);
403 INIT_LIST_HEAD(&dev_opp->opp_list);
404
405 /* Secure the device list modification */
406 list_add_rcu(&dev_opp->node, &dev_opp_list);
407 return dev_opp;
408}
409
Viresh Kumar383934092014-11-25 16:04:18 +0530410static int dev_pm_opp_add_dynamic(struct device *dev, unsigned long freq,
411 unsigned long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200412{
413 struct device_opp *dev_opp = NULL;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500414 struct dev_pm_opp *opp, *new_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200415 struct list_head *head;
Viresh Kumar6ce41842014-12-10 09:45:35 +0530416 int ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200417
418 /* allocate new OPP node */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500419 new_opp = kzalloc(sizeof(*new_opp), GFP_KERNEL);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200420 if (!new_opp) {
421 dev_warn(dev, "%s: Unable to create new OPP node\n", __func__);
422 return -ENOMEM;
423 }
424
425 /* Hold our list modification lock here */
426 mutex_lock(&dev_opp_list_lock);
427
Viresh Kumara7470db2014-11-25 16:04:17 +0530428 /* populate the opp table */
Viresh Kumara7470db2014-11-25 16:04:17 +0530429 new_opp->rate = freq;
430 new_opp->u_volt = u_volt;
431 new_opp->available = true;
Viresh Kumar383934092014-11-25 16:04:18 +0530432 new_opp->dynamic = dynamic;
Viresh Kumara7470db2014-11-25 16:04:17 +0530433
Nishanth Menone1f60b22010-10-13 00:13:10 +0200434 /* Check for existing list for 'dev' */
435 dev_opp = find_device_opp(dev);
436 if (IS_ERR(dev_opp)) {
Viresh Kumar07cce742014-12-10 09:45:34 +0530437 dev_opp = add_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200438 if (!dev_opp) {
Viresh Kumar6ce41842014-12-10 09:45:35 +0530439 ret = -ENOMEM;
440 goto free_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200441 }
442
Viresh Kumara7470db2014-11-25 16:04:17 +0530443 head = &dev_opp->opp_list;
444 goto list_add;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200445 }
446
Chander Kashyap64ce8542014-05-22 10:36:26 +0530447 /*
448 * Insert new OPP in order of increasing frequency
449 * and discard if already present
450 */
Nishanth Menone1f60b22010-10-13 00:13:10 +0200451 head = &dev_opp->opp_list;
452 list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
Chander Kashyap64ce8542014-05-22 10:36:26 +0530453 if (new_opp->rate <= opp->rate)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200454 break;
455 else
456 head = &opp->node;
457 }
458
Chander Kashyap64ce8542014-05-22 10:36:26 +0530459 /* Duplicate OPPs ? */
460 if (new_opp->rate == opp->rate) {
Viresh Kumar6ce41842014-12-10 09:45:35 +0530461 ret = opp->available && new_opp->u_volt == opp->u_volt ?
Chander Kashyap64ce8542014-05-22 10:36:26 +0530462 0 : -EEXIST;
463
464 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
465 __func__, opp->rate, opp->u_volt, opp->available,
466 new_opp->rate, new_opp->u_volt, new_opp->available);
Viresh Kumar6ce41842014-12-10 09:45:35 +0530467 goto free_opp;
Chander Kashyap64ce8542014-05-22 10:36:26 +0530468 }
469
Viresh Kumara7470db2014-11-25 16:04:17 +0530470list_add:
Viresh Kumar7284a002014-12-09 11:18:51 +0530471 new_opp->dev_opp = dev_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200472 list_add_rcu(&new_opp->node, head);
473 mutex_unlock(&dev_opp_list_lock);
474
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200475 /*
476 * Notify the changes in the availability of the operable
477 * frequency/voltage list.
478 */
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530479 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200480 return 0;
Viresh Kumar6ce41842014-12-10 09:45:35 +0530481
482free_opp:
483 mutex_unlock(&dev_opp_list_lock);
484 kfree(new_opp);
485 return ret;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200486}
Viresh Kumar383934092014-11-25 16:04:18 +0530487
488/**
489 * dev_pm_opp_add() - Add an OPP table from a table definitions
490 * @dev: device for which we do this operation
491 * @freq: Frequency in Hz for this OPP
492 * @u_volt: Voltage in uVolts for this OPP
493 *
494 * This function adds an opp definition to the opp list and returns status.
495 * The opp is made available by default and it can be controlled using
496 * dev_pm_opp_enable/disable functions.
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 *
504 * Return:
505 * 0: On success OR
506 * Duplicate OPPs (both freq and volt are same) and opp->available
507 * -EEXIST: Freq are same and volt are different OR
508 * Duplicate OPPs (both freq and volt are same) and !opp->available
509 * -ENOMEM: Memory allocation failure
510 */
511int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
512{
513 return dev_pm_opp_add_dynamic(dev, freq, u_volt, true);
514}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500515EXPORT_SYMBOL_GPL(dev_pm_opp_add);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200516
Viresh Kumar129eec52014-11-27 08:54:06 +0530517static void kfree_opp_rcu(struct rcu_head *head)
518{
519 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
520
521 kfree_rcu(opp, rcu_head);
522}
523
524static void kfree_device_rcu(struct rcu_head *head)
525{
526 struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
527
Viresh Kumar1c6a6622014-12-10 09:45:31 +0530528 kfree_rcu(device_opp, rcu_head);
Viresh Kumar129eec52014-11-27 08:54:06 +0530529}
530
Viresh Kumar86453b42014-12-10 09:45:32 +0530531static void __dev_pm_opp_remove(struct device_opp *dev_opp,
532 struct dev_pm_opp *opp)
Viresh Kumar129eec52014-11-27 08:54:06 +0530533{
534 /*
535 * Notify the changes in the availability of the operable
536 * frequency/voltage list.
537 */
538 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
539 list_del_rcu(&opp->node);
540 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, kfree_opp_rcu);
541
542 if (list_empty(&dev_opp->opp_list)) {
543 list_del_rcu(&dev_opp->node);
544 call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
545 kfree_device_rcu);
546 }
547}
548
549/**
550 * dev_pm_opp_remove() - Remove an OPP from OPP list
551 * @dev: device for which we do this operation
552 * @freq: OPP to remove with matching 'freq'
553 *
554 * This function removes an opp from the opp list.
555 */
556void dev_pm_opp_remove(struct device *dev, unsigned long freq)
557{
558 struct dev_pm_opp *opp;
559 struct device_opp *dev_opp;
560 bool found = false;
561
562 /* Hold our list modification lock here */
563 mutex_lock(&dev_opp_list_lock);
564
565 dev_opp = find_device_opp(dev);
566 if (IS_ERR(dev_opp))
567 goto unlock;
568
569 list_for_each_entry(opp, &dev_opp->opp_list, node) {
570 if (opp->rate == freq) {
571 found = true;
572 break;
573 }
574 }
575
576 if (!found) {
577 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
578 __func__, freq);
579 goto unlock;
580 }
581
582 __dev_pm_opp_remove(dev_opp, opp);
583unlock:
584 mutex_unlock(&dev_opp_list_lock);
585}
586EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
587
Nishanth Menone1f60b22010-10-13 00:13:10 +0200588/**
589 * opp_set_availability() - helper to set the availability of an opp
590 * @dev: device for which we do this operation
591 * @freq: OPP frequency to modify availability
592 * @availability_req: availability status requested for this opp
593 *
594 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
595 * share a common logic which is isolated here.
596 *
597 * Returns -EINVAL for bad pointers, -ENOMEM if no memory available for the
598 * copy operation, returns 0 if no modifcation was done OR modification was
599 * successful.
600 *
601 * Locking: The internal device_opp and opp structures are RCU protected.
602 * Hence this function internally uses RCU updater strategy with mutex locks to
603 * keep the integrity of the internal data structures. Callers should ensure
604 * that this function is *NOT* called under RCU protection or in contexts where
605 * mutex locking or synchronize_rcu() blocking calls cannot be used.
606 */
607static int opp_set_availability(struct device *dev, unsigned long freq,
608 bool availability_req)
609{
Viresh Kumar29df0ee2014-12-10 09:45:33 +0530610 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500611 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200612 int r = 0;
613
614 /* keep the node allocated */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500615 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200616 if (!new_opp) {
617 dev_warn(dev, "%s: Unable to create OPP\n", __func__);
618 return -ENOMEM;
619 }
620
621 mutex_lock(&dev_opp_list_lock);
622
623 /* Find the device_opp */
Viresh Kumar29df0ee2014-12-10 09:45:33 +0530624 dev_opp = find_device_opp(dev);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200625 if (IS_ERR(dev_opp)) {
626 r = PTR_ERR(dev_opp);
627 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
628 goto unlock;
629 }
630
631 /* Do we have the frequency? */
632 list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
633 if (tmp_opp->rate == freq) {
634 opp = tmp_opp;
635 break;
636 }
637 }
638 if (IS_ERR(opp)) {
639 r = PTR_ERR(opp);
640 goto unlock;
641 }
642
643 /* Is update really needed? */
644 if (opp->available == availability_req)
645 goto unlock;
646 /* copy the old data over */
647 *new_opp = *opp;
648
649 /* plug in new node */
650 new_opp->available = availability_req;
651
652 list_replace_rcu(&opp->node, &new_opp->node);
653 mutex_unlock(&dev_opp_list_lock);
Viresh Kumarb4037aa2014-11-27 08:54:07 +0530654 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, kfree_opp_rcu);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200655
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200656 /* Notify the change of the OPP availability */
657 if (availability_req)
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530658 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200659 new_opp);
660 else
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530661 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200662 new_opp);
663
Vincent Guittotdde84372012-10-23 01:21:49 +0200664 return 0;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200665
666unlock:
667 mutex_unlock(&dev_opp_list_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200668 kfree(new_opp);
669 return r;
670}
671
672/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500673 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +0200674 * @dev: device for which we do this operation
675 * @freq: OPP frequency to enable
676 *
677 * Enables a provided opp. If the operation is valid, this returns 0, else the
678 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500679 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200680 *
681 * Locking: The internal device_opp and opp structures are RCU protected.
682 * Hence this function indirectly uses RCU and mutex locks to keep the
683 * integrity of the internal data structures. Callers should ensure that
684 * 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 Menon5d4879c2013-09-19 16:03:50 -0500687int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200688{
689 return opp_set_availability(dev, freq, true);
690}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500691EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200692
693/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500694 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +0200695 * @dev: device for which we do this operation
696 * @freq: OPP frequency to disable
697 *
698 * Disables a provided opp. If the operation is valid, this returns
699 * 0, else the corresponding error value. It is meant to be a temporary
700 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500701 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +0200702 *
703 * Locking: The internal device_opp and opp structures are RCU protected.
704 * Hence this function indirectly uses RCU and mutex locks to keep the
705 * integrity of the internal data structures. Callers should ensure that
706 * this function is *NOT* called under RCU protection or in contexts where
707 * mutex locking or synchronize_rcu() blocking calls cannot be used.
708 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500709int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200710{
711 return opp_set_availability(dev, freq, false);
712}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500713EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200714
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200715/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500716 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200717 * @dev: device pointer used to lookup device OPPs.
718 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500719struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200720{
721 struct device_opp *dev_opp = find_device_opp(dev);
722
723 if (IS_ERR(dev_opp))
Thomas Meyer156acb12011-11-08 22:34:00 +0100724 return ERR_CAST(dev_opp); /* matching type */
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200725
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530726 return &dev_opp->srcu_head;
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200727}
Shawn Guob496dfb2012-09-05 01:09:12 +0200728
729#ifdef CONFIG_OF
730/**
731 * of_init_opp_table() - Initialize opp table from device tree
732 * @dev: device pointer used to lookup device OPPs.
733 *
734 * Register the initial OPP table with the OPP library for given device.
735 */
736int of_init_opp_table(struct device *dev)
737{
738 const struct property *prop;
739 const __be32 *val;
740 int nr;
741
742 prop = of_find_property(dev->of_node, "operating-points", NULL);
743 if (!prop)
744 return -ENODEV;
745 if (!prop->value)
746 return -ENODATA;
747
748 /*
749 * Each OPP is a set of tuples consisting of frequency and
750 * voltage like <freq-kHz vol-uV>.
751 */
752 nr = prop->length / sizeof(u32);
753 if (nr % 2) {
754 dev_err(dev, "%s: Invalid OPP list\n", __func__);
755 return -EINVAL;
756 }
757
758 val = prop->value;
759 while (nr) {
760 unsigned long freq = be32_to_cpup(val++) * 1000;
761 unsigned long volt = be32_to_cpup(val++);
762
Viresh Kumar383934092014-11-25 16:04:18 +0530763 if (dev_pm_opp_add_dynamic(dev, freq, volt, false))
Shawn Guob496dfb2012-09-05 01:09:12 +0200764 dev_warn(dev, "%s: Failed to add OPP %ld\n",
765 __func__, freq);
Shawn Guob496dfb2012-09-05 01:09:12 +0200766 nr -= 2;
767 }
768
769 return 0;
770}
Mark Langsdorf74c46c62013-01-28 18:26:16 +0000771EXPORT_SYMBOL_GPL(of_init_opp_table);
Viresh Kumar129eec52014-11-27 08:54:06 +0530772
773/**
774 * of_free_opp_table() - Free OPP table entries created from static DT entries
775 * @dev: device pointer used to lookup device OPPs.
776 *
777 * Free OPPs created using static entries present in DT.
778 */
779void of_free_opp_table(struct device *dev)
780{
Viresh Kumar2a6127d02014-12-08 13:46:18 +0530781 struct device_opp *dev_opp;
Viresh Kumar129eec52014-11-27 08:54:06 +0530782 struct dev_pm_opp *opp, *tmp;
783
784 /* Check for existing list for 'dev' */
785 dev_opp = find_device_opp(dev);
786 if (WARN(IS_ERR(dev_opp), "%s: dev_opp: %ld\n", dev_name(dev),
787 PTR_ERR(dev_opp)))
788 return;
789
790 /* Hold our list modification lock here */
791 mutex_lock(&dev_opp_list_lock);
792
793 /* Free static OPPs */
794 list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
795 if (!opp->dynamic)
796 __dev_pm_opp_remove(dev_opp, opp);
797 }
798
799 mutex_unlock(&dev_opp_list_lock);
800}
801EXPORT_SYMBOL_GPL(of_free_opp_table);
Shawn Guob496dfb2012-09-05 01:09:12 +0200802#endif