blob: 1211210b15b06717fb0741cdebccac40335b7499 [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>
17#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/cpufreq.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050020#include <linux/device.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020021#include <linux/list.h>
22#include <linux/rculist.h>
23#include <linux/rcupdate.h>
24#include <linux/opp.h>
Shawn Guob496dfb2012-09-05 01:09:12 +020025#include <linux/of.h>
Nishanth Menone1f60b22010-10-13 00:13:10 +020026
27/*
28 * Internal data structure organization with the OPP layer library is as
29 * follows:
30 * dev_opp_list (root)
31 * |- device 1 (represents voltage domain 1)
32 * | |- opp 1 (availability, freq, voltage)
33 * | |- opp 2 ..
34 * ... ...
35 * | `- opp n ..
36 * |- device 2 (represents the next voltage domain)
37 * ...
38 * `- device m (represents mth voltage domain)
39 * device 1, 2.. are represented by dev_opp structure while each opp
40 * is represented by the opp structure.
41 */
42
43/**
44 * struct opp - Generic OPP description structure
45 * @node: opp list node. The nodes are maintained throughout the lifetime
46 * of boot. It is expected only an optimal set of OPPs are
47 * added to the library by the SoC framework.
48 * RCU usage: opp list is traversed with RCU locks. node
49 * modification is possible realtime, hence the modifications
50 * are protected by the dev_opp_list_lock for integrity.
51 * IMPORTANT: the opp nodes should be maintained in increasing
52 * order.
53 * @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
57 *
58 * This structure stores the OPP information for a given device.
59 */
60struct opp {
61 struct list_head node;
62
63 bool available;
64 unsigned long rate;
65 unsigned long u_volt;
66
67 struct device_opp *dev_opp;
Vincent Guittotdde84372012-10-23 01:21:49 +020068 struct rcu_head head;
Nishanth Menone1f60b22010-10-13 00:13:10 +020069};
70
71/**
72 * struct device_opp - Device opp structure
73 * @node: list node - contains the devices with OPPs that
74 * have been registered. Nodes once added are not modified in this
75 * list.
76 * RCU usage: nodes are not modified in the list of device_opp,
77 * however addition is possible and is secured by dev_opp_list_lock
78 * @dev: device pointer
MyungJoo Ham03ca3702011-09-30 22:35:12 +020079 * @head: notifier head to notify the OPP availability changes.
Nishanth Menone1f60b22010-10-13 00:13:10 +020080 * @opp_list: list of opps
81 *
82 * This is an internal data structure maintaining the link to opps attached to
83 * a device. This structure is not meant to be shared to users as it is
84 * meant for book keeping and private to OPP library
85 */
86struct device_opp {
87 struct list_head node;
88
89 struct device *dev;
MyungJoo Ham03ca3702011-09-30 22:35:12 +020090 struct srcu_notifier_head head;
Nishanth Menone1f60b22010-10-13 00:13:10 +020091 struct list_head opp_list;
92};
93
94/*
95 * The root of the list of all devices. All device_opp structures branch off
96 * from here, with each device_opp containing the list of opp it supports in
97 * various states of availability.
98 */
99static LIST_HEAD(dev_opp_list);
100/* Lock to allow exclusive modification to the device and opp lists */
101static DEFINE_MUTEX(dev_opp_list_lock);
102
103/**
104 * find_device_opp() - find device_opp struct using device pointer
105 * @dev: device pointer used to lookup device OPPs
106 *
107 * Search list of device OPPs for one containing matching device. Does a RCU
108 * reader operation to grab the pointer needed.
109 *
110 * Returns pointer to 'struct device_opp' if found, otherwise -ENODEV or
111 * -EINVAL based on type of error.
112 *
113 * Locking: This function must be called under rcu_read_lock(). device_opp
114 * is a RCU protected pointer. This means that device_opp is valid as long
115 * as we are under RCU lock.
116 */
117static struct device_opp *find_device_opp(struct device *dev)
118{
119 struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV);
120
121 if (unlikely(IS_ERR_OR_NULL(dev))) {
122 pr_err("%s: Invalid parameters\n", __func__);
123 return ERR_PTR(-EINVAL);
124 }
125
126 list_for_each_entry_rcu(tmp_dev_opp, &dev_opp_list, node) {
127 if (tmp_dev_opp->dev == dev) {
128 dev_opp = tmp_dev_opp;
129 break;
130 }
131 }
132
133 return dev_opp;
134}
135
136/**
137 * opp_get_voltage() - Gets the voltage corresponding to an available opp
138 * @opp: opp for which voltage has to be returned for
139 *
140 * Return voltage in micro volt corresponding to the opp, else
141 * return 0
142 *
143 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
144 * protected pointer. This means that opp which could have been fetched by
145 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
146 * under RCU lock. The pointer returned by the opp_find_freq family must be
147 * used in the same section as the usage of this function with the pointer
148 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
149 * pointer.
150 */
151unsigned long opp_get_voltage(struct opp *opp)
152{
153 struct opp *tmp_opp;
154 unsigned long v = 0;
155
156 tmp_opp = rcu_dereference(opp);
157 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
158 pr_err("%s: Invalid parameters\n", __func__);
159 else
160 v = tmp_opp->u_volt;
161
162 return v;
163}
164
165/**
166 * opp_get_freq() - Gets the frequency corresponding to an available opp
167 * @opp: opp for which frequency has to be returned for
168 *
169 * Return frequency in hertz corresponding to the opp, else
170 * return 0
171 *
172 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
173 * protected pointer. This means that opp which could have been fetched by
174 * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are
175 * under RCU lock. The pointer returned by the opp_find_freq family must be
176 * used in the same section as the usage of this function with the pointer
177 * prior to unlocking with rcu_read_unlock() to maintain the integrity of the
178 * pointer.
179 */
180unsigned long opp_get_freq(struct opp *opp)
181{
182 struct opp *tmp_opp;
183 unsigned long f = 0;
184
185 tmp_opp = rcu_dereference(opp);
186 if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available)
187 pr_err("%s: Invalid parameters\n", __func__);
188 else
189 f = tmp_opp->rate;
190
191 return f;
192}
193
194/**
195 * opp_get_opp_count() - Get number of opps available in the opp list
196 * @dev: device for which we do this operation
197 *
198 * This function returns the number of available opps if there are any,
199 * else returns 0 if none or the corresponding error value.
200 *
201 * Locking: This function must be called under rcu_read_lock(). This function
202 * internally references two RCU protected structures: device_opp and opp which
203 * are safe as long as we are under a common RCU locked section.
204 */
205int opp_get_opp_count(struct device *dev)
206{
207 struct device_opp *dev_opp;
208 struct opp *temp_opp;
209 int count = 0;
210
211 dev_opp = find_device_opp(dev);
212 if (IS_ERR(dev_opp)) {
213 int r = PTR_ERR(dev_opp);
214 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
215 return r;
216 }
217
218 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
219 if (temp_opp->available)
220 count++;
221 }
222
223 return count;
224}
225
226/**
227 * opp_find_freq_exact() - search for an exact frequency
228 * @dev: device for which we do this operation
229 * @freq: frequency to search for
Nishanth Menon7ae49612011-02-25 23:46:18 +0100230 * @available: true/false - match for available opp
Nishanth Menone1f60b22010-10-13 00:13:10 +0200231 *
232 * Searches for exact match in the opp list and returns pointer to the matching
233 * opp if found, else returns ERR_PTR in case of error and should be handled
234 * using IS_ERR.
235 *
236 * Note: available is a modifier for the search. if available=true, then the
237 * match is for exact matching frequency and is available in the stored OPP
238 * table. if false, the match is for exact frequency which is not available.
239 *
240 * This provides a mechanism to enable an opp which is not available currently
241 * or the opposite as well.
242 *
243 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
244 * protected pointer. The reason for the same is that the opp pointer which is
245 * returned will remain valid for use with opp_get_{voltage, freq} only while
246 * under the locked area. The pointer returned must be used prior to unlocking
247 * with rcu_read_unlock() to maintain the integrity of the pointer.
248 */
249struct opp *opp_find_freq_exact(struct device *dev, unsigned long freq,
250 bool available)
251{
252 struct device_opp *dev_opp;
253 struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
254
255 dev_opp = find_device_opp(dev);
256 if (IS_ERR(dev_opp)) {
257 int r = PTR_ERR(dev_opp);
258 dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r);
259 return ERR_PTR(r);
260 }
261
262 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
263 if (temp_opp->available == available &&
264 temp_opp->rate == freq) {
265 opp = temp_opp;
266 break;
267 }
268 }
269
270 return opp;
271}
272
273/**
274 * opp_find_freq_ceil() - Search for an rounded ceil freq
275 * @dev: device for which we do this operation
276 * @freq: Start frequency
277 *
278 * Search for the matching ceil *available* OPP from a starting freq
279 * for a device.
280 *
281 * Returns matching *opp and refreshes *freq accordingly, else returns
282 * ERR_PTR in case of error and should be handled using IS_ERR.
283 *
284 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
285 * protected pointer. The reason for the same is that the opp pointer which is
286 * returned will remain valid for use with opp_get_{voltage, freq} only while
287 * under the locked area. The pointer returned must be used prior to unlocking
288 * with rcu_read_unlock() to maintain the integrity of the pointer.
289 */
290struct opp *opp_find_freq_ceil(struct device *dev, unsigned long *freq)
291{
292 struct device_opp *dev_opp;
293 struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
294
295 if (!dev || !freq) {
296 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
297 return ERR_PTR(-EINVAL);
298 }
299
300 dev_opp = find_device_opp(dev);
301 if (IS_ERR(dev_opp))
302 return opp;
303
304 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
305 if (temp_opp->available && temp_opp->rate >= *freq) {
306 opp = temp_opp;
307 *freq = opp->rate;
308 break;
309 }
310 }
311
312 return opp;
313}
314
315/**
316 * opp_find_freq_floor() - Search for a rounded floor freq
317 * @dev: device for which we do this operation
318 * @freq: Start frequency
319 *
320 * Search for the matching floor *available* OPP from a starting freq
321 * for a device.
322 *
323 * Returns matching *opp and refreshes *freq accordingly, else returns
324 * ERR_PTR in case of error and should be handled using IS_ERR.
325 *
326 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
327 * protected pointer. The reason for the same is that the opp pointer which is
328 * returned will remain valid for use with opp_get_{voltage, freq} only while
329 * under the locked area. The pointer returned must be used prior to unlocking
330 * with rcu_read_unlock() to maintain the integrity of the pointer.
331 */
332struct opp *opp_find_freq_floor(struct device *dev, unsigned long *freq)
333{
334 struct device_opp *dev_opp;
335 struct opp *temp_opp, *opp = ERR_PTR(-ENODEV);
336
337 if (!dev || !freq) {
338 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
339 return ERR_PTR(-EINVAL);
340 }
341
342 dev_opp = find_device_opp(dev);
343 if (IS_ERR(dev_opp))
344 return opp;
345
346 list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) {
347 if (temp_opp->available) {
348 /* go to the next node, before choosing prev */
349 if (temp_opp->rate > *freq)
350 break;
351 else
352 opp = temp_opp;
353 }
354 }
355 if (!IS_ERR(opp))
356 *freq = opp->rate;
357
358 return opp;
359}
360
361/**
362 * opp_add() - Add an OPP table from a table definitions
363 * @dev: device for which we do this operation
364 * @freq: Frequency in Hz for this OPP
365 * @u_volt: Voltage in uVolts for this OPP
366 *
367 * This function adds an opp definition to the opp list and returns status.
368 * The opp is made available by default and it can be controlled using
369 * opp_enable/disable functions.
370 *
371 * Locking: The internal device_opp and opp structures are RCU protected.
372 * Hence this function internally uses RCU updater strategy with mutex locks
373 * to keep the integrity of the internal data structures. Callers should ensure
374 * that this function is *NOT* called under RCU protection or in contexts where
375 * mutex cannot be locked.
376 */
377int opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
378{
379 struct device_opp *dev_opp = NULL;
380 struct opp *opp, *new_opp;
381 struct list_head *head;
382
383 /* allocate new OPP node */
384 new_opp = kzalloc(sizeof(struct opp), GFP_KERNEL);
385 if (!new_opp) {
386 dev_warn(dev, "%s: Unable to create new OPP node\n", __func__);
387 return -ENOMEM;
388 }
389
390 /* Hold our list modification lock here */
391 mutex_lock(&dev_opp_list_lock);
392
393 /* Check for existing list for 'dev' */
394 dev_opp = find_device_opp(dev);
395 if (IS_ERR(dev_opp)) {
396 /*
397 * Allocate a new device OPP table. In the infrequent case
398 * where a new device is needed to be added, we pay this
399 * penalty.
400 */
401 dev_opp = kzalloc(sizeof(struct device_opp), GFP_KERNEL);
402 if (!dev_opp) {
403 mutex_unlock(&dev_opp_list_lock);
404 kfree(new_opp);
405 dev_warn(dev,
406 "%s: Unable to create device OPP structure\n",
407 __func__);
408 return -ENOMEM;
409 }
410
411 dev_opp->dev = dev;
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200412 srcu_init_notifier_head(&dev_opp->head);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200413 INIT_LIST_HEAD(&dev_opp->opp_list);
414
415 /* Secure the device list modification */
416 list_add_rcu(&dev_opp->node, &dev_opp_list);
417 }
418
419 /* populate the opp table */
420 new_opp->dev_opp = dev_opp;
421 new_opp->rate = freq;
422 new_opp->u_volt = u_volt;
423 new_opp->available = true;
424
425 /* Insert new OPP in order of increasing frequency */
426 head = &dev_opp->opp_list;
427 list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) {
428 if (new_opp->rate < opp->rate)
429 break;
430 else
431 head = &opp->node;
432 }
433
434 list_add_rcu(&new_opp->node, head);
435 mutex_unlock(&dev_opp_list_lock);
436
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200437 /*
438 * Notify the changes in the availability of the operable
439 * frequency/voltage list.
440 */
441 srcu_notifier_call_chain(&dev_opp->head, OPP_EVENT_ADD, new_opp);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200442 return 0;
443}
444
445/**
Vincent Guittotdde84372012-10-23 01:21:49 +0200446 * opp_free_rcu() - helper to clear the struct opp when grace period has
447 * elapsed without blocking the the caller of opp_set_availability
448 */
449static void opp_free_rcu(struct rcu_head *head)
450{
451 struct opp *opp = container_of(head, struct opp, head);
452
453 kfree(opp);
454}
455
456/**
Nishanth Menone1f60b22010-10-13 00:13:10 +0200457 * opp_set_availability() - helper to set the availability of an opp
458 * @dev: device for which we do this operation
459 * @freq: OPP frequency to modify availability
460 * @availability_req: availability status requested for this opp
461 *
462 * Set the availability of an OPP with an RCU operation, opp_{enable,disable}
463 * share a common logic which is isolated here.
464 *
465 * Returns -EINVAL for bad pointers, -ENOMEM if no memory available for the
466 * copy operation, returns 0 if no modifcation was done OR modification was
467 * successful.
468 *
469 * Locking: The internal device_opp and opp structures are RCU protected.
470 * Hence this function internally uses RCU updater strategy with mutex locks to
471 * keep the integrity of the internal data structures. Callers should ensure
472 * that this function is *NOT* called under RCU protection or in contexts where
473 * mutex locking or synchronize_rcu() blocking calls cannot be used.
474 */
475static int opp_set_availability(struct device *dev, unsigned long freq,
476 bool availability_req)
477{
Jonghwan Choifc928052011-07-26 16:08:16 -0700478 struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200479 struct opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
480 int r = 0;
481
482 /* keep the node allocated */
483 new_opp = kmalloc(sizeof(struct opp), GFP_KERNEL);
484 if (!new_opp) {
485 dev_warn(dev, "%s: Unable to create OPP\n", __func__);
486 return -ENOMEM;
487 }
488
489 mutex_lock(&dev_opp_list_lock);
490
491 /* Find the device_opp */
492 list_for_each_entry(tmp_dev_opp, &dev_opp_list, node) {
493 if (dev == tmp_dev_opp->dev) {
494 dev_opp = tmp_dev_opp;
495 break;
496 }
497 }
498 if (IS_ERR(dev_opp)) {
499 r = PTR_ERR(dev_opp);
500 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
501 goto unlock;
502 }
503
504 /* Do we have the frequency? */
505 list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) {
506 if (tmp_opp->rate == freq) {
507 opp = tmp_opp;
508 break;
509 }
510 }
511 if (IS_ERR(opp)) {
512 r = PTR_ERR(opp);
513 goto unlock;
514 }
515
516 /* Is update really needed? */
517 if (opp->available == availability_req)
518 goto unlock;
519 /* copy the old data over */
520 *new_opp = *opp;
521
522 /* plug in new node */
523 new_opp->available = availability_req;
524
525 list_replace_rcu(&opp->node, &new_opp->node);
526 mutex_unlock(&dev_opp_list_lock);
Vincent Guittotdde84372012-10-23 01:21:49 +0200527 call_rcu(&opp->head, opp_free_rcu);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200528
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200529 /* Notify the change of the OPP availability */
530 if (availability_req)
531 srcu_notifier_call_chain(&dev_opp->head, OPP_EVENT_ENABLE,
532 new_opp);
533 else
534 srcu_notifier_call_chain(&dev_opp->head, OPP_EVENT_DISABLE,
535 new_opp);
536
Vincent Guittotdde84372012-10-23 01:21:49 +0200537 return 0;
Nishanth Menone1f60b22010-10-13 00:13:10 +0200538
539unlock:
540 mutex_unlock(&dev_opp_list_lock);
Nishanth Menone1f60b22010-10-13 00:13:10 +0200541 kfree(new_opp);
542 return r;
543}
544
545/**
546 * opp_enable() - Enable a specific OPP
547 * @dev: device for which we do this operation
548 * @freq: OPP frequency to enable
549 *
550 * Enables a provided opp. If the operation is valid, this returns 0, else the
551 * corresponding error value. It is meant to be used for users an OPP available
552 * after being temporarily made unavailable with opp_disable.
553 *
554 * Locking: The internal device_opp and opp structures are RCU protected.
555 * Hence this function indirectly uses RCU and mutex locks to keep the
556 * integrity of the internal data structures. Callers should ensure that
557 * this function is *NOT* called under RCU protection or in contexts where
558 * mutex locking or synchronize_rcu() blocking calls cannot be used.
559 */
560int opp_enable(struct device *dev, unsigned long freq)
561{
562 return opp_set_availability(dev, freq, true);
563}
564
565/**
566 * opp_disable() - Disable a specific OPP
567 * @dev: device for which we do this operation
568 * @freq: OPP frequency to disable
569 *
570 * Disables a provided opp. If the operation is valid, this returns
571 * 0, else the corresponding error value. It is meant to be a temporary
572 * control by users to make this OPP not available until the circumstances are
573 * right to make it available again (with a call to opp_enable).
574 *
575 * Locking: The internal device_opp and opp structures are RCU protected.
576 * Hence this function indirectly uses RCU and mutex locks to keep the
577 * integrity of the internal data structures. Callers should ensure that
578 * this function is *NOT* called under RCU protection or in contexts where
579 * mutex locking or synchronize_rcu() blocking calls cannot be used.
580 */
581int opp_disable(struct device *dev, unsigned long freq)
582{
583 return opp_set_availability(dev, freq, false);
584}
585
586#ifdef CONFIG_CPU_FREQ
587/**
588 * opp_init_cpufreq_table() - create a cpufreq table for a device
589 * @dev: device for which we do this operation
590 * @table: Cpufreq table returned back to caller
591 *
592 * Generate a cpufreq table for a provided device- this assumes that the
593 * opp list is already initialized and ready for usage.
594 *
595 * This function allocates required memory for the cpufreq table. It is
596 * expected that the caller does the required maintenance such as freeing
597 * the table as required.
598 *
599 * Returns -EINVAL for bad pointers, -ENODEV if the device is not found, -ENOMEM
600 * if no memory available for the operation (table is not populated), returns 0
601 * if successful and table is populated.
602 *
603 * WARNING: It is important for the callers to ensure refreshing their copy of
604 * the table if any of the mentioned functions have been invoked in the interim.
605 *
606 * Locking: The internal device_opp and opp structures are RCU protected.
607 * To simplify the logic, we pretend we are updater and hold relevant mutex here
608 * Callers should ensure that this function is *NOT* called under RCU protection
609 * or in contexts where mutex locking cannot be used.
610 */
611int opp_init_cpufreq_table(struct device *dev,
612 struct cpufreq_frequency_table **table)
613{
614 struct device_opp *dev_opp;
615 struct opp *opp;
616 struct cpufreq_frequency_table *freq_table;
617 int i = 0;
618
619 /* Pretend as if I am an updater */
620 mutex_lock(&dev_opp_list_lock);
621
622 dev_opp = find_device_opp(dev);
623 if (IS_ERR(dev_opp)) {
624 int r = PTR_ERR(dev_opp);
625 mutex_unlock(&dev_opp_list_lock);
626 dev_err(dev, "%s: Device OPP not found (%d)\n", __func__, r);
627 return r;
628 }
629
630 freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) *
631 (opp_get_opp_count(dev) + 1), GFP_KERNEL);
632 if (!freq_table) {
633 mutex_unlock(&dev_opp_list_lock);
634 dev_warn(dev, "%s: Unable to allocate frequency table\n",
635 __func__);
636 return -ENOMEM;
637 }
638
639 list_for_each_entry(opp, &dev_opp->opp_list, node) {
640 if (opp->available) {
641 freq_table[i].index = i;
642 freq_table[i].frequency = opp->rate / 1000;
643 i++;
644 }
645 }
646 mutex_unlock(&dev_opp_list_lock);
647
648 freq_table[i].index = i;
649 freq_table[i].frequency = CPUFREQ_TABLE_END;
650
651 *table = &freq_table[0];
652
653 return 0;
654}
Nishanth Menon99f381d2011-06-10 20:24:57 +0200655
656/**
657 * opp_free_cpufreq_table() - free the cpufreq table
658 * @dev: device for which we do this operation
659 * @table: table to free
660 *
661 * Free up the table allocated by opp_init_cpufreq_table
662 */
663void opp_free_cpufreq_table(struct device *dev,
664 struct cpufreq_frequency_table **table)
665{
666 if (!table)
667 return;
668
669 kfree(*table);
670 *table = NULL;
671}
Nishanth Menone1f60b22010-10-13 00:13:10 +0200672#endif /* CONFIG_CPU_FREQ */
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200673
674/**
675 * opp_get_notifier() - find notifier_head of the device with opp
676 * @dev: device pointer used to lookup device OPPs.
677 */
678struct srcu_notifier_head *opp_get_notifier(struct device *dev)
679{
680 struct device_opp *dev_opp = find_device_opp(dev);
681
682 if (IS_ERR(dev_opp))
Thomas Meyer156acb12011-11-08 22:34:00 +0100683 return ERR_CAST(dev_opp); /* matching type */
MyungJoo Ham03ca3702011-09-30 22:35:12 +0200684
685 return &dev_opp->head;
686}
Shawn Guob496dfb2012-09-05 01:09:12 +0200687
688#ifdef CONFIG_OF
689/**
690 * of_init_opp_table() - Initialize opp table from device tree
691 * @dev: device pointer used to lookup device OPPs.
692 *
693 * Register the initial OPP table with the OPP library for given device.
694 */
695int of_init_opp_table(struct device *dev)
696{
697 const struct property *prop;
698 const __be32 *val;
699 int nr;
700
701 prop = of_find_property(dev->of_node, "operating-points", NULL);
702 if (!prop)
703 return -ENODEV;
704 if (!prop->value)
705 return -ENODATA;
706
707 /*
708 * Each OPP is a set of tuples consisting of frequency and
709 * voltage like <freq-kHz vol-uV>.
710 */
711 nr = prop->length / sizeof(u32);
712 if (nr % 2) {
713 dev_err(dev, "%s: Invalid OPP list\n", __func__);
714 return -EINVAL;
715 }
716
717 val = prop->value;
718 while (nr) {
719 unsigned long freq = be32_to_cpup(val++) * 1000;
720 unsigned long volt = be32_to_cpup(val++);
721
722 if (opp_add(dev, freq, volt)) {
723 dev_warn(dev, "%s: Failed to add OPP %ld\n",
724 __func__, freq);
725 continue;
726 }
727 nr -= 2;
728 }
729
730 return 0;
731}
732#endif