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