blob: 977474a3c64fbfaeb26d7f5e0564cd234e0249ab [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
87 * meant for book keeping and private to OPP library
88 */
89struct device_opp {
90 struct list_head node;
91
92 struct device *dev;
Viresh Kumarcd1a0682014-11-25 16:04:16 +053093 struct srcu_notifier_head srcu_head;
Viresh Kumar129eec52014-11-27 08:54:06 +053094 struct rcu_head rcu_head;
Nishanth Menone1f60b22010-10-13 00:13:10 +020095 struct list_head opp_list;
96};
97
98/*
99 * The root of the list of all devices. All device_opp structures branch off
100 * from here, with each device_opp containing the list of opp it supports in
101 * various states of availability.
102 */
103static LIST_HEAD(dev_opp_list);
104/* Lock to allow exclusive modification to the device and opp lists */
105static DEFINE_MUTEX(dev_opp_list_lock);
106
107/**
108 * find_device_opp() - find device_opp struct using device pointer
109 * @dev: device pointer used to lookup device OPPs
110 *
111 * Search list of device OPPs for one containing matching device. Does a RCU
112 * reader operation to grab the pointer needed.
113 *
114 * Returns pointer to 'struct device_opp' if found, otherwise -ENODEV or
115 * -EINVAL based on type of error.
116 *
117 * Locking: This function must be called under rcu_read_lock(). device_opp
118 * is a RCU protected pointer. This means that device_opp is valid as long
119 * as we are under RCU lock.
120 */
121static struct device_opp *find_device_opp(struct device *dev)
122{
123 struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV);
124
125 if (unlikely(IS_ERR_OR_NULL(dev))) {
126 pr_err("%s: Invalid parameters\n", __func__);
127 return ERR_PTR(-EINVAL);
128 }
129
130 list_for_each_entry_rcu(tmp_dev_opp, &dev_opp_list, node) {
131 if (tmp_dev_opp->dev == dev) {
132 dev_opp = tmp_dev_opp;
133 break;
134 }
135 }
136
137 return dev_opp;
138}
139
140/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500141 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200142 * @opp: opp for which voltage has to be returned for
143 *
144 * Return voltage in micro volt corresponding to the opp, else
145 * return 0
146 *
147 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
148 * protected pointer. This means that opp which could have been fetched by
149 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
150 * under RCU lock. The pointer returned by the opp_find_freq family must be
151 * used in the same section as the usage of this function with the pointer
152 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
153 * pointer.
154 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500155unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200156{
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500157 struct dev_pm_opp *tmp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200158 unsigned long v = 0;
159
160 tmp_opp = rcu_dereference(opp);
161 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
162 pr_err("%s: Invalid parameters\n", __func__);
163 else
164 v = tmp_opp->u_volt;
165
166 return v;
167}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500168EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200169
170/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500171 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200172 * @opp: opp for which frequency has to be returned for
173 *
174 * Return frequency in hertz corresponding to the opp, else
175 * return 0
176 *
177 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
178 * protected pointer. This means that opp which could have been fetched by
179 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
180 * under RCU lock. The pointer returned by the opp_find_freq family must be
181 * used in the same section as the usage of this function with the pointer
182 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
183 * pointer.
184 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500185unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200186{
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500187 struct dev_pm_opp *tmp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200188 unsigned long f = 0;
189
190 tmp_opp = rcu_dereference(opp);
191 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
192 pr_err("%s: Invalid parameters\n", __func__);
193 else
194 f = tmp_opp->rate;
195
196 return f;
197}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500198EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200199
200/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500201 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list
Nishanth Menone1f60b22010-10-13 00:13:10 +0200202 * @dev: device for which we do this operation
203 *
204 * This function returns the number of available opps if there are any,
205 * else returns 0 if none or the corresponding error value.
206 *
207 * Locking: This function must be called under rcu_read_lock(). This function
208 * internally references two RCU protected structures: device_opp and opp which
209 * are safe as long as we are under a common RCU locked section.
210 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500211int dev_pm_opp_get_opp_count(struct device *dev)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200212{
213 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500214 struct dev_pm_opp *temp_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200215 int count = 0;
216
217 dev_opp = find_device_opp(dev);
218 if (IS_ERR(dev_opp)) {
219 int r = PTR_ERR(dev_opp);
220 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
221 return r;
222 }
223
224 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
225 if (temp_opp->available)
226 count++;
227 }
228
229 return count;
230}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500231EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200232
233/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500234 * dev_pm_opp_find_freq_exact() - search for an exact frequency
Nishanth Menone1f60b22010-10-13 00:13:10 +0200235 * @dev: device for which we do this operation
236 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100237 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200238 *
239 * Searches for exact match in the opp list and returns pointer to the matching
240 * opp if found, else returns ERR_PTR in case of error and should be handled
Nishanth Menon07797262012-10-24 22:00:12 +0200241 * using IS_ERR. Error return values can be:
242 * EINVAL: for bad pointer
243 * ERANGE: no match found for search
244 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200245 *
246 * Note: available is a modifier for the search. if available=true, then the
247 * match is for exact matching frequency and is available in the stored OPP
248 * table. if false, the match is for exact frequency which is not available.
249 *
250 * This provides a mechanism to enable an opp which is not available currently
251 * or the opposite as well.
252 *
253 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
254 * protected pointer. The reason for the same is that the opp pointer which is
255 * returned will remain valid for use with opp_get_{voltage, freq} only while
256 * under the locked area. The pointer returned must be used prior to unlocking
257 * with rcu_read_unlock() to maintain the integrity of the pointer.
258 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500259struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
260 unsigned long freq,
261 bool available)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200262{
263 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500264 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200265
266 dev_opp = find_device_opp(dev);
267 if (IS_ERR(dev_opp)) {
268 int r = PTR_ERR(dev_opp);
269 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
270 return ERR_PTR(r);
271 }
272
273 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
274 if (temp_opp->available == available &&
275 temp_opp->rate == freq) {
276 opp = temp_opp;
277 break;
278 }
279 }
280
281 return opp;
282}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500283EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200284
285/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500286 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200287 * @dev: device for which we do this operation
288 * @freq: Start frequency
289 *
290 * Search for the matching ceil *available* OPP from a starting freq
291 * for a device.
292 *
293 * Returns matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200294 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
295 * values can be:
296 * EINVAL: for bad pointer
297 * ERANGE: no match found for search
298 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200299 *
300 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
301 * protected pointer. The reason for the same is that the opp pointer which is
302 * returned will remain valid for use with opp_get_{voltage, freq} only while
303 * under the locked area. The pointer returned must be used prior to unlocking
304 * with rcu_read_unlock() to maintain the integrity of the pointer.
305 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500306struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
307 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200308{
309 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500310 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200311
312 if (!dev || !freq) {
313 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
314 return ERR_PTR(-EINVAL);
315 }
316
317 dev_opp = find_device_opp(dev);
318 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200319 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200320
321 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
322 if (temp_opp->available && temp_opp->rate >= *freq) {
323 opp = temp_opp;
324 *freq = opp->rate;
325 break;
326 }
327 }
328
329 return opp;
330}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500331EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200332
333/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500334 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
Nishanth Menone1f60b22010-10-13 00:13:10 +0200335 * @dev: device for which we do this operation
336 * @freq: Start frequency
337 *
338 * Search for the matching floor *available* OPP from a starting freq
339 * for a device.
340 *
341 * Returns matching *opp and refreshes *freq accordingly, else returns
Nishanth Menon07797262012-10-24 22:00:12 +0200342 * ERR_PTR in case of error and should be handled using IS_ERR. Error return
343 * values can be:
344 * EINVAL: for bad pointer
345 * ERANGE: no match found for search
346 * ENODEV: if device not found in list of registered devices
Nishanth Menone1f60b22010-10-13 00:13:10 +0200347 *
348 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
349 * protected pointer. The reason for the same is that the opp pointer which is
350 * returned will remain valid for use with opp_get_{voltage, freq} only while
351 * under the locked area. The pointer returned must be used prior to unlocking
352 * with rcu_read_unlock() to maintain the integrity of the pointer.
353 */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500354struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
355 unsigned long *freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200356{
357 struct device_opp *dev_opp;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500358 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200359
360 if (!dev || !freq) {
361 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
362 return ERR_PTR(-EINVAL);
363 }
364
365 dev_opp = find_device_opp(dev);
366 if (IS_ERR(dev_opp))
Nishanth Menon07797262012-10-24 22:00:12 +0200367 return ERR_CAST(dev_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200368
369 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
370 if (temp_opp->available) {
371 /* go to the next node, before choosing prev */
372 if (temp_opp->rate > *freq)
373 break;
374 else
375 opp = temp_opp;
376 }
377 }
378 if (!IS_ERR(opp))
379 *freq = opp->rate;
380
381 return opp;
382}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500383EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200384
Viresh Kumar383934092014-11-25 16:04:18 +0530385static int dev_pm_opp_add_dynamic(struct device *dev, unsigned long freq,
386 unsigned long u_volt, bool dynamic)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200387{
388 struct device_opp *dev_opp = NULL;
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500389 struct dev_pm_opp *opp, *new_opp;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200390 struct list_head *head;
391
392 /* allocate new OPP node */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500393 new_opp = kzalloc(sizeof(*new_opp), GFP_KERNEL);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200394 if (!new_opp) {
395 dev_warn(dev, "%s: Unable to create new OPP node\n", __func__);
396 return -ENOMEM;
397 }
398
399 /* Hold our list modification lock here */
400 mutex_lock(&dev_opp_list_lock);
401
Viresh Kumara7470db2014-11-25 16:04:17 +0530402 /* populate the opp table */
403 new_opp->dev_opp = dev_opp;
404 new_opp->rate = freq;
405 new_opp->u_volt = u_volt;
406 new_opp->available = true;
Viresh Kumar383934092014-11-25 16:04:18 +0530407 new_opp->dynamic = dynamic;
Viresh Kumara7470db2014-11-25 16:04:17 +0530408
Nishanth Menone1f60b22010-10-13 00:13:10 +0200409 /* Check for existing list for 'dev' */
410 dev_opp = find_device_opp(dev);
411 if (IS_ERR(dev_opp)) {
412 /*
413 * Allocate a new device OPP table. In the infrequent case
414 * where a new device is needed to be added, we pay this
415 * penalty.
416 */
417 dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);
418 if (!dev_opp) {
419 mutex_unlock(&dev_opp_list_lock);
420 kfree(new_opp);
421 dev_warn(dev,
422 "%s: Unable to create device OPP structure\n",
423 __func__);
424 return -ENOMEM;
425 }
426
427 dev_opp->dev = dev;
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530428 srcu_init_notifier_head(&dev_opp->srcu_head);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200429 INIT_LIST_HEAD(&dev_opp->opp_list);
430
431 /* Secure the device list modification */
432 list_add_rcu(&dev_opp->node, &dev_opp_list);
Viresh Kumara7470db2014-11-25 16:04:17 +0530433 head = &dev_opp->opp_list;
434 goto list_add;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200435 }
436
Chander Kashyap64ce8542014-05-22 10:36:26 +0530437 /*
438 * Insert new OPP in order of increasing frequency
439 * and discard if already present
440 */
Nishanth Menone1f60b22010-10-13 00:13:10 +0200441 head = &dev_opp->opp_list;
442 list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
Chander Kashyap64ce8542014-05-22 10:36:26 +0530443 if (new_opp->rate <= opp->rate)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200444 break;
445 else
446 head = &opp->node;
447 }
448
Chander Kashyap64ce8542014-05-22 10:36:26 +0530449 /* Duplicate OPPs ? */
450 if (new_opp->rate == opp->rate) {
451 int ret = opp->available && new_opp->u_volt == opp->u_volt ?
452 0 : -EEXIST;
453
454 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
455 __func__, opp->rate, opp->u_volt, opp->available,
456 new_opp->rate, new_opp->u_volt, new_opp->available);
457 mutex_unlock(&dev_opp_list_lock);
458 kfree(new_opp);
459 return ret;
460 }
461
Viresh Kumara7470db2014-11-25 16:04:17 +0530462list_add:
Nishanth Menone1f60b22010-10-13 00:13:10 +0200463 list_add_rcu(&new_opp->node, head);
464 mutex_unlock(&dev_opp_list_lock);
465
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200466 /*
467 * Notify the changes in the availability of the operable
468 * frequency/voltage list.
469 */
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530470 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200471 return 0;
472}
Viresh Kumar383934092014-11-25 16:04:18 +0530473
474/**
475 * dev_pm_opp_add() - Add an OPP table from a table definitions
476 * @dev: device for which we do this operation
477 * @freq: Frequency in Hz for this OPP
478 * @u_volt: Voltage in uVolts for this OPP
479 *
480 * This function adds an opp definition to the opp list and returns status.
481 * The opp is made available by default and it can be controlled using
482 * dev_pm_opp_enable/disable functions.
483 *
484 * Locking: The internal device_opp and opp structures are RCU protected.
485 * Hence this function internally uses RCU updater strategy with mutex locks
486 * to keep the integrity of the internal data structures. Callers should ensure
487 * that this function is *NOT* called under RCU protection or in contexts where
488 * mutex cannot be locked.
489 *
490 * Return:
491 * 0: On success OR
492 * Duplicate OPPs (both freq and volt are same) and opp->available
493 * -EEXIST: Freq are same and volt are different OR
494 * Duplicate OPPs (both freq and volt are same) and !opp->available
495 * -ENOMEM: Memory allocation failure
496 */
497int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
498{
499 return dev_pm_opp_add_dynamic(dev, freq, u_volt, true);
500}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500501EXPORT_SYMBOL_GPL(dev_pm_opp_add);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200502
Viresh Kumar129eec52014-11-27 08:54:06 +0530503static void kfree_opp_rcu(struct rcu_head *head)
504{
505 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
506
507 kfree_rcu(opp, rcu_head);
508}
509
510static void kfree_device_rcu(struct rcu_head *head)
511{
512 struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head);
513
514 kfree(device_opp);
515}
516
517void __dev_pm_opp_remove(struct device_opp *dev_opp, struct dev_pm_opp *opp)
518{
519 /*
520 * Notify the changes in the availability of the operable
521 * frequency/voltage list.
522 */
523 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp);
524 list_del_rcu(&opp->node);
525 call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, kfree_opp_rcu);
526
527 if (list_empty(&dev_opp->opp_list)) {
528 list_del_rcu(&dev_opp->node);
529 call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head,
530 kfree_device_rcu);
531 }
532}
533
534/**
535 * dev_pm_opp_remove() - Remove an OPP from OPP list
536 * @dev: device for which we do this operation
537 * @freq: OPP to remove with matching 'freq'
538 *
539 * This function removes an opp from the opp list.
540 */
541void dev_pm_opp_remove(struct device *dev, unsigned long freq)
542{
543 struct dev_pm_opp *opp;
544 struct device_opp *dev_opp;
545 bool found = false;
546
547 /* Hold our list modification lock here */
548 mutex_lock(&dev_opp_list_lock);
549
550 dev_opp = find_device_opp(dev);
551 if (IS_ERR(dev_opp))
552 goto unlock;
553
554 list_for_each_entry(opp, &dev_opp->opp_list, node) {
555 if (opp->rate == freq) {
556 found = true;
557 break;
558 }
559 }
560
561 if (!found) {
562 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
563 __func__, freq);
564 goto unlock;
565 }
566
567 __dev_pm_opp_remove(dev_opp, opp);
568unlock:
569 mutex_unlock(&dev_opp_list_lock);
570}
571EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
572
Nishanth Menone1f60b22010-10-13 00:13:10 +0200573/**
574 * opp_set_availability() - helper to set the availability of an opp
575 * @dev: device for which we do this operation
576 * @freq: OPP frequency to modify availability
577 * @availability_req: availability status requested for this opp
578 *
579 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
580 * share a common logic which is isolated here.
581 *
582 * Returns -EINVAL for bad pointers, -ENOMEM if no memory available for the
583 * copy operation, returns 0 if no modifcation was done OR modification was
584 * successful.
585 *
586 * Locking: The internal device_opp and opp structures are RCU protected.
587 * Hence this function internally uses RCU updater strategy with mutex locks to
588 * keep the integrity of the internal data structures. Callers should ensure
589 * that this function is *NOT* called under RCU protection or in contexts where
590 * mutex locking or synchronize_rcu() blocking calls cannot be used.
591 */
592static int opp_set_availability(struct device *dev, unsigned long freq,
593 bool availability_req)
594{
Jonghwan Choifc928052011-07-26 16:08:16 -0700595 struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV);
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500596 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200597 int r = 0;
598
599 /* keep the node allocated */
Nishanth Menon47d43ba2013-09-19 16:03:51 -0500600 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200601 if (!new_opp) {
602 dev_warn(dev, "%s: Unable to create OPP\n", __func__);
603 return -ENOMEM;
604 }
605
606 mutex_lock(&dev_opp_list_lock);
607
608 /* Find the device_opp */
609 list_for_each_entry(tmp_dev_opp, &dev_opp_list, node) {
610 if (dev == tmp_dev_opp->dev) {
611 dev_opp = tmp_dev_opp;
612 break;
613 }
614 }
615 if (IS_ERR(dev_opp)) {
616 r = PTR_ERR(dev_opp);
617 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
618 goto unlock;
619 }
620
621 /* Do we have the frequency? */
622 list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
623 if (tmp_opp->rate == freq) {
624 opp = tmp_opp;
625 break;
626 }
627 }
628 if (IS_ERR(opp)) {
629 r = PTR_ERR(opp);
630 goto unlock;
631 }
632
633 /* Is update really needed? */
634 if (opp->available == availability_req)
635 goto unlock;
636 /* copy the old data over */
637 *new_opp = *opp;
638
639 /* plug in new node */
640 new_opp->available = availability_req;
641
642 list_replace_rcu(&opp->node, &new_opp->node);
643 mutex_unlock(&dev_opp_list_lock);
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530644 kfree_rcu(opp, rcu_head);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200645
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200646 /* Notify the change of the OPP availability */
647 if (availability_req)
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530648 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200649 new_opp);
650 else
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530651 srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE,
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200652 new_opp);
653
Vincent Guittotdde84372012-10-23 01:21:49 +0200654 return 0;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200655
656unlock:
657 mutex_unlock(&dev_opp_list_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200658 kfree(new_opp);
659 return r;
660}
661
662/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500663 * dev_pm_opp_enable() - Enable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +0200664 * @dev: device for which we do this operation
665 * @freq: OPP frequency to enable
666 *
667 * Enables a provided opp. If the operation is valid, this returns 0, else the
668 * corresponding error value. It is meant to be used for users an OPP available
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500669 * after being temporarily made unavailable with dev_pm_opp_disable.
Nishanth Menone1f60b22010-10-13 00:13:10 +0200670 *
671 * Locking: The internal device_opp and opp structures are RCU protected.
672 * Hence this function indirectly uses RCU and mutex locks to keep the
673 * integrity of the internal data structures. Callers should ensure that
674 * this function is *NOT* called under RCU protection or in contexts where
675 * mutex locking or synchronize_rcu() blocking calls cannot be used.
676 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500677int dev_pm_opp_enable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200678{
679 return opp_set_availability(dev, freq, true);
680}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500681EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200682
683/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500684 * dev_pm_opp_disable() - Disable a specific OPP
Nishanth Menone1f60b22010-10-13 00:13:10 +0200685 * @dev: device for which we do this operation
686 * @freq: OPP frequency to disable
687 *
688 * Disables a provided opp. If the operation is valid, this returns
689 * 0, else the corresponding error value. It is meant to be a temporary
690 * control by users to make this OPP not available until the circumstances are
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500691 * right to make it available again (with a call to dev_pm_opp_enable).
Nishanth Menone1f60b22010-10-13 00:13:10 +0200692 *
693 * Locking: The internal device_opp and opp structures are RCU protected.
694 * Hence this function indirectly uses RCU and mutex locks to keep the
695 * integrity of the internal data structures. Callers should ensure that
696 * this function is *NOT* called under RCU protection or in contexts where
697 * mutex locking or synchronize_rcu() blocking calls cannot be used.
698 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500699int dev_pm_opp_disable(struct device *dev, unsigned long freq)
Nishanth Menone1f60b22010-10-13 00:13:10 +0200700{
701 return opp_set_availability(dev, freq, false);
702}
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500703EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200704
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200705/**
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500706 * dev_pm_opp_get_notifier() - find notifier_head of the device with opp
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200707 * @dev: device pointer used to lookup device OPPs.
708 */
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500709struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200710{
711 struct device_opp *dev_opp = find_device_opp(dev);
712
713 if (IS_ERR(dev_opp))
Thomas Meyer156acb12011-11-08 22:34:00 +0100714 return ERR_CAST(dev_opp); /* matching type */
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200715
Viresh Kumarcd1a0682014-11-25 16:04:16 +0530716 return &dev_opp->srcu_head;
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200717}
Shawn Guob496dfb2012-09-05 01:09:12 +0200718
719#ifdef CONFIG_OF
720/**
721 * of_init_opp_table() - Initialize opp table from device tree
722 * @dev: device pointer used to lookup device OPPs.
723 *
724 * Register the initial OPP table with the OPP library for given device.
725 */
726int of_init_opp_table(struct device *dev)
727{
728 const struct property *prop;
729 const __be32 *val;
730 int nr;
731
732 prop = of_find_property(dev->of_node, "operating-points", NULL);
733 if (!prop)
734 return -ENODEV;
735 if (!prop->value)
736 return -ENODATA;
737
738 /*
739 * Each OPP is a set of tuples consisting of frequency and
740 * voltage like <freq-kHz vol-uV>.
741 */
742 nr = prop->length / sizeof(u32);
743 if (nr % 2) {
744 dev_err(dev, "%s: Invalid OPP list\n", __func__);
745 return -EINVAL;
746 }
747
748 val = prop->value;
749 while (nr) {
750 unsigned long freq = be32_to_cpup(val++) * 1000;
751 unsigned long volt = be32_to_cpup(val++);
752
Viresh Kumar383934092014-11-25 16:04:18 +0530753 if (dev_pm_opp_add_dynamic(dev, freq, volt, false))
Shawn Guob496dfb2012-09-05 01:09:12 +0200754 dev_warn(dev, "%s: Failed to add OPP %ld\n",
755 __func__, freq);
Shawn Guob496dfb2012-09-05 01:09:12 +0200756 nr -= 2;
757 }
758
759 return 0;
760}
Mark Langsdorf74c46c62013-01-28 18:26:16 +0000761EXPORT_SYMBOL_GPL(of_init_opp_table);
Viresh Kumar129eec52014-11-27 08:54:06 +0530762
763/**
764 * of_free_opp_table() - Free OPP table entries created from static DT entries
765 * @dev: device pointer used to lookup device OPPs.
766 *
767 * Free OPPs created using static entries present in DT.
768 */
769void of_free_opp_table(struct device *dev)
770{
771 struct device_opp *dev_opp = find_device_opp(dev);
772 struct dev_pm_opp *opp, *tmp;
773
774 /* Check for existing list for 'dev' */
775 dev_opp = find_device_opp(dev);
776 if (WARN(IS_ERR(dev_opp), "%s: dev_opp: %ld\n", dev_name(dev),
777 PTR_ERR(dev_opp)))
778 return;
779
780 /* Hold our list modification lock here */
781 mutex_lock(&dev_opp_list_lock);
782
783 /* Free static OPPs */
784 list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) {
785 if (!opp->dynamic)
786 __dev_pm_opp_remove(dev_opp, opp);
787 }
788
789 mutex_unlock(&dev_opp_list_lock);
790}
791EXPORT_SYMBOL_GPL(of_free_opp_table);
Shawn Guob496dfb2012-09-05 01:09:12 +0200792#endif