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> | |
e1f60b29 | 17 | #include <linux/slab.h> |
51990e82 | 18 | #include <linux/device.h> |
e1f60b29 NM |
19 | #include <linux/list.h> |
20 | #include <linux/rculist.h> | |
21 | #include <linux/rcupdate.h> | |
e4db1c74 | 22 | #include <linux/pm_opp.h> |
b496dfbc | 23 | #include <linux/of.h> |
80126ce7 | 24 | #include <linux/export.h> |
e1f60b29 NM |
25 | |
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 | /** | |
47d43ba7 | 43 | * struct dev_pm_opp - Generic OPP description structure |
e1f60b29 NM |
44 | * @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. | |
38393409 | 52 | * @dynamic: not-created from static DT entries. |
e1f60b29 | 53 | * @available: true/false - marks if this OPP as available or not |
27465902 | 54 | * @turbo: true if turbo (boost) OPP |
e1f60b29 | 55 | * @rate: Frequency in hertz |
27465902 VK |
56 | * @u_volt: Target voltage in microvolts corresponding to this OPP |
57 | * @u_volt_min: Minimum voltage in microvolts corresponding to this OPP | |
58 | * @u_volt_max: Maximum voltage in microvolts corresponding to this OPP | |
59 | * @u_amp: Maximum current drawn by the device in microamperes | |
3ca9bb33 VK |
60 | * @clock_latency_ns: Latency (in nanoseconds) of switching to this OPP's |
61 | * frequency from any other OPP's frequency. | |
e1f60b29 | 62 | * @dev_opp: points back to the device_opp struct this opp belongs to |
cd1a068a | 63 | * @rcu_head: RCU callback head used for deferred freeing |
27465902 | 64 | * @np: OPP's device node. |
e1f60b29 NM |
65 | * |
66 | * This structure stores the OPP information for a given device. | |
67 | */ | |
47d43ba7 | 68 | struct dev_pm_opp { |
e1f60b29 NM |
69 | struct list_head node; |
70 | ||
71 | bool available; | |
38393409 | 72 | bool dynamic; |
27465902 | 73 | bool turbo; |
e1f60b29 | 74 | unsigned long rate; |
27465902 | 75 | |
e1f60b29 | 76 | unsigned long u_volt; |
27465902 VK |
77 | unsigned long u_volt_min; |
78 | unsigned long u_volt_max; | |
79 | unsigned long u_amp; | |
3ca9bb33 | 80 | unsigned long clock_latency_ns; |
e1f60b29 NM |
81 | |
82 | struct device_opp *dev_opp; | |
cd1a068a | 83 | struct rcu_head rcu_head; |
27465902 VK |
84 | |
85 | struct device_node *np; | |
e1f60b29 NM |
86 | }; |
87 | ||
88 | /** | |
89 | * struct device_opp - Device opp structure | |
90 | * @node: list node - contains the devices with OPPs that | |
91 | * have been registered. Nodes once added are not modified in this | |
92 | * list. | |
93 | * RCU usage: nodes are not modified in the list of device_opp, | |
94 | * however addition is possible and is secured by dev_opp_list_lock | |
95 | * @dev: device pointer | |
cd1a068a | 96 | * @srcu_head: notifier head to notify the OPP availability changes. |
129eec55 | 97 | * @rcu_head: RCU callback head used for deferred freeing |
e1f60b29 NM |
98 | * @opp_list: list of opps |
99 | * | |
100 | * This is an internal data structure maintaining the link to opps attached to | |
101 | * a device. This structure is not meant to be shared to users as it is | |
1c6a662f VK |
102 | * meant for book keeping and private to OPP library. |
103 | * | |
104 | * Because the opp structures can be used from both rcu and srcu readers, we | |
105 | * need to wait for the grace period of both of them before freeing any | |
106 | * resources. And so we have used kfree_rcu() from within call_srcu() handlers. | |
e1f60b29 NM |
107 | */ |
108 | struct device_opp { | |
109 | struct list_head node; | |
110 | ||
111 | struct device *dev; | |
cd1a068a | 112 | struct srcu_notifier_head srcu_head; |
129eec55 | 113 | struct rcu_head rcu_head; |
e1f60b29 | 114 | struct list_head opp_list; |
3ca9bb33 VK |
115 | |
116 | unsigned long clock_latency_ns_max; | |
e1f60b29 NM |
117 | }; |
118 | ||
119 | /* | |
120 | * The root of the list of all devices. All device_opp structures branch off | |
121 | * from here, with each device_opp containing the list of opp it supports in | |
122 | * various states of availability. | |
123 | */ | |
124 | static LIST_HEAD(dev_opp_list); | |
125 | /* Lock to allow exclusive modification to the device and opp lists */ | |
126 | static DEFINE_MUTEX(dev_opp_list_lock); | |
127 | ||
b02ded24 DT |
128 | #define opp_rcu_lockdep_assert() \ |
129 | do { \ | |
130 | rcu_lockdep_assert(rcu_read_lock_held() || \ | |
131 | lockdep_is_held(&dev_opp_list_lock), \ | |
132 | "Missing rcu_read_lock() or " \ | |
133 | "dev_opp_list_lock protection"); \ | |
134 | } while (0) | |
135 | ||
e1f60b29 | 136 | /** |
327854c8 | 137 | * _find_device_opp() - find device_opp struct using device pointer |
e1f60b29 NM |
138 | * @dev: device pointer used to lookup device OPPs |
139 | * | |
140 | * Search list of device OPPs for one containing matching device. Does a RCU | |
141 | * reader operation to grab the pointer needed. | |
142 | * | |
984f16c8 | 143 | * Return: pointer to 'struct device_opp' if found, otherwise -ENODEV or |
e1f60b29 NM |
144 | * -EINVAL based on type of error. |
145 | * | |
146 | * Locking: This function must be called under rcu_read_lock(). device_opp | |
147 | * is a RCU protected pointer. This means that device_opp is valid as long | |
148 | * as we are under RCU lock. | |
149 | */ | |
327854c8 | 150 | static struct device_opp *_find_device_opp(struct device *dev) |
e1f60b29 NM |
151 | { |
152 | struct device_opp *tmp_dev_opp, *dev_opp = ERR_PTR(-ENODEV); | |
153 | ||
154 | if (unlikely(IS_ERR_OR_NULL(dev))) { | |
155 | pr_err("%s: Invalid parameters\n", __func__); | |
156 | return ERR_PTR(-EINVAL); | |
157 | } | |
158 | ||
159 | list_for_each_entry_rcu(tmp_dev_opp, &dev_opp_list, node) { | |
160 | if (tmp_dev_opp->dev == dev) { | |
161 | dev_opp = tmp_dev_opp; | |
162 | break; | |
163 | } | |
164 | } | |
165 | ||
166 | return dev_opp; | |
167 | } | |
168 | ||
169 | /** | |
5d4879cd | 170 | * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an available opp |
e1f60b29 NM |
171 | * @opp: opp for which voltage has to be returned for |
172 | * | |
984f16c8 | 173 | * Return: voltage in micro volt corresponding to the opp, else |
e1f60b29 NM |
174 | * return 0 |
175 | * | |
176 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
177 | * protected pointer. This means that opp which could have been fetched by | |
178 | * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are | |
179 | * under RCU lock. The pointer returned by the opp_find_freq family must be | |
180 | * used in the same section as the usage of this function with the pointer | |
181 | * prior to unlocking with rcu_read_unlock() to maintain the integrity of the | |
182 | * pointer. | |
183 | */ | |
47d43ba7 | 184 | unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) |
e1f60b29 | 185 | { |
47d43ba7 | 186 | struct dev_pm_opp *tmp_opp; |
e1f60b29 NM |
187 | unsigned long v = 0; |
188 | ||
04bf1c7f KK |
189 | opp_rcu_lockdep_assert(); |
190 | ||
e1f60b29 NM |
191 | tmp_opp = rcu_dereference(opp); |
192 | if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available) | |
193 | pr_err("%s: Invalid parameters\n", __func__); | |
194 | else | |
195 | v = tmp_opp->u_volt; | |
196 | ||
197 | return v; | |
198 | } | |
5d4879cd | 199 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage); |
e1f60b29 NM |
200 | |
201 | /** | |
5d4879cd | 202 | * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp |
e1f60b29 NM |
203 | * @opp: opp for which frequency has to be returned for |
204 | * | |
984f16c8 | 205 | * Return: frequency in hertz corresponding to the opp, else |
e1f60b29 NM |
206 | * return 0 |
207 | * | |
208 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
209 | * protected pointer. This means that opp which could have been fetched by | |
210 | * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are | |
211 | * under RCU lock. The pointer returned by the opp_find_freq family must be | |
212 | * used in the same section as the usage of this function with the pointer | |
213 | * prior to unlocking with rcu_read_unlock() to maintain the integrity of the | |
214 | * pointer. | |
215 | */ | |
47d43ba7 | 216 | unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp) |
e1f60b29 | 217 | { |
47d43ba7 | 218 | struct dev_pm_opp *tmp_opp; |
e1f60b29 NM |
219 | unsigned long f = 0; |
220 | ||
04bf1c7f KK |
221 | opp_rcu_lockdep_assert(); |
222 | ||
e1f60b29 NM |
223 | tmp_opp = rcu_dereference(opp); |
224 | if (unlikely(IS_ERR_OR_NULL(tmp_opp)) || !tmp_opp->available) | |
225 | pr_err("%s: Invalid parameters\n", __func__); | |
226 | else | |
227 | f = tmp_opp->rate; | |
228 | ||
229 | return f; | |
230 | } | |
5d4879cd | 231 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq); |
e1f60b29 | 232 | |
3ca9bb33 VK |
233 | /** |
234 | * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds | |
235 | * @dev: device for which we do this operation | |
236 | * | |
237 | * Return: This function returns the max clock latency in nanoseconds. | |
238 | * | |
239 | * Locking: This function takes rcu_read_lock(). | |
240 | */ | |
241 | unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev) | |
242 | { | |
243 | struct device_opp *dev_opp; | |
244 | unsigned long clock_latency_ns; | |
245 | ||
246 | rcu_read_lock(); | |
247 | ||
248 | dev_opp = _find_device_opp(dev); | |
249 | if (IS_ERR(dev_opp)) | |
250 | clock_latency_ns = 0; | |
251 | else | |
252 | clock_latency_ns = dev_opp->clock_latency_ns_max; | |
253 | ||
254 | rcu_read_unlock(); | |
255 | return clock_latency_ns; | |
256 | } | |
257 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency); | |
258 | ||
e1f60b29 | 259 | /** |
5d4879cd | 260 | * dev_pm_opp_get_opp_count() - Get number of opps available in the opp list |
e1f60b29 NM |
261 | * @dev: device for which we do this operation |
262 | * | |
984f16c8 | 263 | * Return: This function returns the number of available opps if there are any, |
e1f60b29 NM |
264 | * else returns 0 if none or the corresponding error value. |
265 | * | |
b4718c02 | 266 | * Locking: This function takes rcu_read_lock(). |
e1f60b29 | 267 | */ |
5d4879cd | 268 | int dev_pm_opp_get_opp_count(struct device *dev) |
e1f60b29 NM |
269 | { |
270 | struct device_opp *dev_opp; | |
47d43ba7 | 271 | struct dev_pm_opp *temp_opp; |
e1f60b29 NM |
272 | int count = 0; |
273 | ||
b4718c02 | 274 | rcu_read_lock(); |
b02ded24 | 275 | |
327854c8 | 276 | dev_opp = _find_device_opp(dev); |
e1f60b29 | 277 | if (IS_ERR(dev_opp)) { |
b4718c02 DT |
278 | count = PTR_ERR(dev_opp); |
279 | dev_err(dev, "%s: device OPP not found (%d)\n", | |
280 | __func__, count); | |
281 | goto out_unlock; | |
e1f60b29 NM |
282 | } |
283 | ||
284 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { | |
285 | if (temp_opp->available) | |
286 | count++; | |
287 | } | |
288 | ||
b4718c02 DT |
289 | out_unlock: |
290 | rcu_read_unlock(); | |
e1f60b29 NM |
291 | return count; |
292 | } | |
5d4879cd | 293 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); |
e1f60b29 NM |
294 | |
295 | /** | |
5d4879cd | 296 | * dev_pm_opp_find_freq_exact() - search for an exact frequency |
e1f60b29 NM |
297 | * @dev: device for which we do this operation |
298 | * @freq: frequency to search for | |
7ae49618 | 299 | * @available: true/false - match for available opp |
e1f60b29 | 300 | * |
984f16c8 NM |
301 | * Return: Searches for exact match in the opp list and returns pointer to the |
302 | * matching opp if found, else returns ERR_PTR in case of error and should | |
303 | * be handled using IS_ERR. Error return values can be: | |
0779726c NM |
304 | * EINVAL: for bad pointer |
305 | * ERANGE: no match found for search | |
306 | * ENODEV: if device not found in list of registered devices | |
e1f60b29 NM |
307 | * |
308 | * Note: available is a modifier for the search. if available=true, then the | |
309 | * match is for exact matching frequency and is available in the stored OPP | |
310 | * table. if false, the match is for exact frequency which is not available. | |
311 | * | |
312 | * This provides a mechanism to enable an opp which is not available currently | |
313 | * or the opposite as well. | |
314 | * | |
315 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
316 | * protected pointer. The reason for the same is that the opp pointer which is | |
317 | * returned will remain valid for use with opp_get_{voltage, freq} only while | |
318 | * under the locked area. The pointer returned must be used prior to unlocking | |
319 | * with rcu_read_unlock() to maintain the integrity of the pointer. | |
320 | */ | |
47d43ba7 NM |
321 | struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, |
322 | unsigned long freq, | |
323 | bool available) | |
e1f60b29 NM |
324 | { |
325 | struct device_opp *dev_opp; | |
47d43ba7 | 326 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
e1f60b29 | 327 | |
b02ded24 DT |
328 | opp_rcu_lockdep_assert(); |
329 | ||
327854c8 | 330 | dev_opp = _find_device_opp(dev); |
e1f60b29 NM |
331 | if (IS_ERR(dev_opp)) { |
332 | int r = PTR_ERR(dev_opp); | |
333 | dev_err(dev, "%s: device OPP not found (%d)\n", __func__, r); | |
334 | return ERR_PTR(r); | |
335 | } | |
336 | ||
337 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { | |
338 | if (temp_opp->available == available && | |
339 | temp_opp->rate == freq) { | |
340 | opp = temp_opp; | |
341 | break; | |
342 | } | |
343 | } | |
344 | ||
345 | return opp; | |
346 | } | |
5d4879cd | 347 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); |
e1f60b29 NM |
348 | |
349 | /** | |
5d4879cd | 350 | * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq |
e1f60b29 NM |
351 | * @dev: device for which we do this operation |
352 | * @freq: Start frequency | |
353 | * | |
354 | * Search for the matching ceil *available* OPP from a starting freq | |
355 | * for a device. | |
356 | * | |
984f16c8 | 357 | * Return: matching *opp and refreshes *freq accordingly, else returns |
0779726c NM |
358 | * ERR_PTR in case of error and should be handled using IS_ERR. Error return |
359 | * values can be: | |
360 | * EINVAL: for bad pointer | |
361 | * ERANGE: no match found for search | |
362 | * ENODEV: if device not found in list of registered devices | |
e1f60b29 NM |
363 | * |
364 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
365 | * protected pointer. The reason for the same is that the opp pointer which is | |
366 | * returned will remain valid for use with opp_get_{voltage, freq} only while | |
367 | * under the locked area. The pointer returned must be used prior to unlocking | |
368 | * with rcu_read_unlock() to maintain the integrity of the pointer. | |
369 | */ | |
47d43ba7 NM |
370 | struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, |
371 | unsigned long *freq) | |
e1f60b29 NM |
372 | { |
373 | struct device_opp *dev_opp; | |
47d43ba7 | 374 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
e1f60b29 | 375 | |
b02ded24 DT |
376 | opp_rcu_lockdep_assert(); |
377 | ||
e1f60b29 NM |
378 | if (!dev || !freq) { |
379 | dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); | |
380 | return ERR_PTR(-EINVAL); | |
381 | } | |
382 | ||
327854c8 | 383 | dev_opp = _find_device_opp(dev); |
e1f60b29 | 384 | if (IS_ERR(dev_opp)) |
0779726c | 385 | return ERR_CAST(dev_opp); |
e1f60b29 NM |
386 | |
387 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { | |
388 | if (temp_opp->available && temp_opp->rate >= *freq) { | |
389 | opp = temp_opp; | |
390 | *freq = opp->rate; | |
391 | break; | |
392 | } | |
393 | } | |
394 | ||
395 | return opp; | |
396 | } | |
5d4879cd | 397 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); |
e1f60b29 NM |
398 | |
399 | /** | |
5d4879cd | 400 | * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq |
e1f60b29 NM |
401 | * @dev: device for which we do this operation |
402 | * @freq: Start frequency | |
403 | * | |
404 | * Search for the matching floor *available* OPP from a starting freq | |
405 | * for a device. | |
406 | * | |
984f16c8 | 407 | * Return: matching *opp and refreshes *freq accordingly, else returns |
0779726c NM |
408 | * ERR_PTR in case of error and should be handled using IS_ERR. Error return |
409 | * values can be: | |
410 | * EINVAL: for bad pointer | |
411 | * ERANGE: no match found for search | |
412 | * ENODEV: if device not found in list of registered devices | |
e1f60b29 NM |
413 | * |
414 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
415 | * protected pointer. The reason for the same is that the opp pointer which is | |
416 | * returned will remain valid for use with opp_get_{voltage, freq} only while | |
417 | * under the locked area. The pointer returned must be used prior to unlocking | |
418 | * with rcu_read_unlock() to maintain the integrity of the pointer. | |
419 | */ | |
47d43ba7 NM |
420 | struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, |
421 | unsigned long *freq) | |
e1f60b29 NM |
422 | { |
423 | struct device_opp *dev_opp; | |
47d43ba7 | 424 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
e1f60b29 | 425 | |
b02ded24 DT |
426 | opp_rcu_lockdep_assert(); |
427 | ||
e1f60b29 NM |
428 | if (!dev || !freq) { |
429 | dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); | |
430 | return ERR_PTR(-EINVAL); | |
431 | } | |
432 | ||
327854c8 | 433 | dev_opp = _find_device_opp(dev); |
e1f60b29 | 434 | if (IS_ERR(dev_opp)) |
0779726c | 435 | return ERR_CAST(dev_opp); |
e1f60b29 NM |
436 | |
437 | list_for_each_entry_rcu(temp_opp, &dev_opp->opp_list, node) { | |
438 | if (temp_opp->available) { | |
439 | /* go to the next node, before choosing prev */ | |
440 | if (temp_opp->rate > *freq) | |
441 | break; | |
442 | else | |
443 | opp = temp_opp; | |
444 | } | |
445 | } | |
446 | if (!IS_ERR(opp)) | |
447 | *freq = opp->rate; | |
448 | ||
449 | return opp; | |
450 | } | |
5d4879cd | 451 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); |
e1f60b29 | 452 | |
984f16c8 | 453 | /** |
aa5f2f85 | 454 | * _add_device_opp() - Find device OPP table or allocate a new one |
984f16c8 NM |
455 | * @dev: device for which we do this operation |
456 | * | |
aa5f2f85 VK |
457 | * It tries to find an existing table first, if it couldn't find one, it |
458 | * allocates a new OPP table and returns that. | |
984f16c8 NM |
459 | * |
460 | * Return: valid device_opp pointer if success, else NULL. | |
461 | */ | |
327854c8 | 462 | static struct device_opp *_add_device_opp(struct device *dev) |
07cce74a VK |
463 | { |
464 | struct device_opp *dev_opp; | |
465 | ||
aa5f2f85 VK |
466 | /* Check for existing list for 'dev' first */ |
467 | dev_opp = _find_device_opp(dev); | |
468 | if (!IS_ERR(dev_opp)) | |
469 | return dev_opp; | |
470 | ||
07cce74a VK |
471 | /* |
472 | * Allocate a new device OPP table. In the infrequent case where a new | |
473 | * device is needed to be added, we pay this penalty. | |
474 | */ | |
475 | dev_opp = kzalloc(sizeof(*dev_opp), GFP_KERNEL); | |
476 | if (!dev_opp) | |
477 | return NULL; | |
478 | ||
479 | dev_opp->dev = dev; | |
480 | srcu_init_notifier_head(&dev_opp->srcu_head); | |
481 | INIT_LIST_HEAD(&dev_opp->opp_list); | |
482 | ||
483 | /* Secure the device list modification */ | |
484 | list_add_rcu(&dev_opp->node, &dev_opp_list); | |
485 | return dev_opp; | |
486 | } | |
487 | ||
737002b5 VK |
488 | /** |
489 | * _kfree_device_rcu() - Free device_opp RCU handler | |
490 | * @head: RCU head | |
491 | */ | |
492 | static void _kfree_device_rcu(struct rcu_head *head) | |
493 | { | |
494 | struct device_opp *device_opp = container_of(head, struct device_opp, rcu_head); | |
495 | ||
496 | kfree_rcu(device_opp, rcu_head); | |
497 | } | |
498 | ||
3bac42ca VK |
499 | /** |
500 | * _remove_device_opp() - Removes a device OPP table | |
501 | * @dev_opp: device OPP table to be removed. | |
502 | * | |
503 | * Removes/frees device OPP table it it doesn't contain any OPPs. | |
504 | */ | |
505 | static void _remove_device_opp(struct device_opp *dev_opp) | |
506 | { | |
507 | if (!list_empty(&dev_opp->opp_list)) | |
508 | return; | |
509 | ||
510 | list_del_rcu(&dev_opp->node); | |
511 | call_srcu(&dev_opp->srcu_head.srcu, &dev_opp->rcu_head, | |
512 | _kfree_device_rcu); | |
513 | } | |
514 | ||
737002b5 VK |
515 | /** |
516 | * _kfree_opp_rcu() - Free OPP RCU handler | |
517 | * @head: RCU head | |
518 | */ | |
519 | static void _kfree_opp_rcu(struct rcu_head *head) | |
520 | { | |
521 | struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head); | |
522 | ||
523 | kfree_rcu(opp, rcu_head); | |
524 | } | |
525 | ||
526 | /** | |
527 | * _opp_remove() - Remove an OPP from a table definition | |
528 | * @dev_opp: points back to the device_opp struct this opp belongs to | |
529 | * @opp: pointer to the OPP to remove | |
23dacf6d | 530 | * @notify: OPP_EVENT_REMOVE notification should be sent or not |
737002b5 VK |
531 | * |
532 | * This function removes an opp definition from the opp list. | |
533 | * | |
534 | * Locking: The internal device_opp and opp structures are RCU protected. | |
535 | * It is assumed that the caller holds required mutex for an RCU updater | |
536 | * strategy. | |
537 | */ | |
538 | static void _opp_remove(struct device_opp *dev_opp, | |
23dacf6d | 539 | struct dev_pm_opp *opp, bool notify) |
737002b5 VK |
540 | { |
541 | /* | |
542 | * Notify the changes in the availability of the operable | |
543 | * frequency/voltage list. | |
544 | */ | |
23dacf6d VK |
545 | if (notify) |
546 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_REMOVE, opp); | |
737002b5 VK |
547 | list_del_rcu(&opp->node); |
548 | call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu); | |
549 | ||
3bac42ca | 550 | _remove_device_opp(dev_opp); |
737002b5 VK |
551 | } |
552 | ||
553 | /** | |
554 | * dev_pm_opp_remove() - Remove an OPP from OPP list | |
555 | * @dev: device for which we do this operation | |
556 | * @freq: OPP to remove with matching 'freq' | |
557 | * | |
558 | * This function removes an opp from the opp list. | |
559 | * | |
560 | * Locking: The internal device_opp and opp structures are RCU protected. | |
561 | * Hence this function internally uses RCU updater strategy with mutex locks | |
562 | * to keep the integrity of the internal data structures. Callers should ensure | |
563 | * that this function is *NOT* called under RCU protection or in contexts where | |
564 | * mutex cannot be locked. | |
565 | */ | |
566 | void dev_pm_opp_remove(struct device *dev, unsigned long freq) | |
567 | { | |
568 | struct dev_pm_opp *opp; | |
569 | struct device_opp *dev_opp; | |
570 | bool found = false; | |
571 | ||
572 | /* Hold our list modification lock here */ | |
573 | mutex_lock(&dev_opp_list_lock); | |
574 | ||
575 | dev_opp = _find_device_opp(dev); | |
576 | if (IS_ERR(dev_opp)) | |
577 | goto unlock; | |
578 | ||
579 | list_for_each_entry(opp, &dev_opp->opp_list, node) { | |
580 | if (opp->rate == freq) { | |
581 | found = true; | |
582 | break; | |
583 | } | |
584 | } | |
585 | ||
586 | if (!found) { | |
587 | dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", | |
588 | __func__, freq); | |
589 | goto unlock; | |
590 | } | |
591 | ||
23dacf6d | 592 | _opp_remove(dev_opp, opp, true); |
737002b5 VK |
593 | unlock: |
594 | mutex_unlock(&dev_opp_list_lock); | |
595 | } | |
596 | EXPORT_SYMBOL_GPL(dev_pm_opp_remove); | |
597 | ||
23dacf6d VK |
598 | static struct dev_pm_opp *_allocate_opp(struct device *dev, |
599 | struct device_opp **dev_opp) | |
600 | { | |
601 | struct dev_pm_opp *opp; | |
602 | ||
603 | /* allocate new OPP node */ | |
604 | opp = kzalloc(sizeof(*opp), GFP_KERNEL); | |
605 | if (!opp) | |
606 | return NULL; | |
607 | ||
608 | INIT_LIST_HEAD(&opp->node); | |
609 | ||
610 | *dev_opp = _add_device_opp(dev); | |
611 | if (!*dev_opp) { | |
612 | kfree(opp); | |
613 | return NULL; | |
614 | } | |
615 | ||
616 | return opp; | |
617 | } | |
618 | ||
619 | static int _opp_add(struct dev_pm_opp *new_opp, struct device_opp *dev_opp) | |
620 | { | |
621 | struct dev_pm_opp *opp; | |
622 | struct list_head *head = &dev_opp->opp_list; | |
623 | ||
624 | /* | |
625 | * Insert new OPP in order of increasing frequency and discard if | |
626 | * already present. | |
627 | * | |
628 | * Need to use &dev_opp->opp_list in the condition part of the 'for' | |
629 | * loop, don't replace it with head otherwise it will become an infinite | |
630 | * loop. | |
631 | */ | |
632 | list_for_each_entry_rcu(opp, &dev_opp->opp_list, node) { | |
633 | if (new_opp->rate > opp->rate) { | |
634 | head = &opp->node; | |
635 | continue; | |
636 | } | |
637 | ||
638 | if (new_opp->rate < opp->rate) | |
639 | break; | |
640 | ||
641 | /* Duplicate OPPs */ | |
642 | dev_warn(dev_opp->dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", | |
643 | __func__, opp->rate, opp->u_volt, opp->available, | |
644 | new_opp->rate, new_opp->u_volt, new_opp->available); | |
645 | ||
646 | return opp->available && new_opp->u_volt == opp->u_volt ? | |
647 | 0 : -EEXIST; | |
648 | } | |
649 | ||
650 | new_opp->dev_opp = dev_opp; | |
651 | list_add_rcu(&new_opp->node, head); | |
652 | ||
653 | return 0; | |
654 | } | |
655 | ||
984f16c8 NM |
656 | /** |
657 | * _opp_add_dynamic() - Allocate a dynamic OPP. | |
658 | * @dev: device for which we do this operation | |
659 | * @freq: Frequency in Hz for this OPP | |
660 | * @u_volt: Voltage in uVolts for this OPP | |
661 | * @dynamic: Dynamically added OPPs. | |
662 | * | |
663 | * This function adds an opp definition to the opp list and returns status. | |
664 | * The opp is made available by default and it can be controlled using | |
665 | * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. | |
666 | * | |
667 | * NOTE: "dynamic" parameter impacts OPPs added by the of_init_opp_table and | |
668 | * freed by of_free_opp_table. | |
669 | * | |
670 | * Locking: The internal device_opp and opp structures are RCU protected. | |
671 | * Hence this function internally uses RCU updater strategy with mutex locks | |
672 | * to keep the integrity of the internal data structures. Callers should ensure | |
673 | * that this function is *NOT* called under RCU protection or in contexts where | |
674 | * mutex cannot be locked. | |
675 | * | |
676 | * Return: | |
677 | * 0 On success OR | |
678 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
679 | * -EEXIST Freq are same and volt are different OR | |
680 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
681 | * -ENOMEM Memory allocation failure | |
682 | */ | |
327854c8 NM |
683 | static int _opp_add_dynamic(struct device *dev, unsigned long freq, |
684 | long u_volt, bool dynamic) | |
e1f60b29 | 685 | { |
aa5f2f85 | 686 | struct device_opp *dev_opp; |
23dacf6d | 687 | struct dev_pm_opp *new_opp; |
6ce4184d | 688 | int ret; |
e1f60b29 | 689 | |
e1f60b29 NM |
690 | /* Hold our list modification lock here */ |
691 | mutex_lock(&dev_opp_list_lock); | |
692 | ||
23dacf6d VK |
693 | new_opp = _allocate_opp(dev, &dev_opp); |
694 | if (!new_opp) { | |
695 | ret = -ENOMEM; | |
696 | goto unlock; | |
697 | } | |
698 | ||
a7470db6 | 699 | /* populate the opp table */ |
a7470db6 VK |
700 | new_opp->rate = freq; |
701 | new_opp->u_volt = u_volt; | |
702 | new_opp->available = true; | |
23dacf6d | 703 | new_opp->dynamic = dynamic; |
a7470db6 | 704 | |
23dacf6d VK |
705 | ret = _opp_add(new_opp, dev_opp); |
706 | if (ret) | |
6ce4184d | 707 | goto free_opp; |
64ce8545 | 708 | |
e1f60b29 NM |
709 | mutex_unlock(&dev_opp_list_lock); |
710 | ||
03ca370f MH |
711 | /* |
712 | * Notify the changes in the availability of the operable | |
713 | * frequency/voltage list. | |
714 | */ | |
cd1a068a | 715 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp); |
e1f60b29 | 716 | return 0; |
6ce4184d VK |
717 | |
718 | free_opp: | |
23dacf6d VK |
719 | _opp_remove(dev_opp, new_opp, false); |
720 | unlock: | |
6ce4184d | 721 | mutex_unlock(&dev_opp_list_lock); |
6ce4184d | 722 | return ret; |
e1f60b29 | 723 | } |
38393409 | 724 | |
27465902 VK |
725 | /* TODO: Support multiple regulators */ |
726 | static int opp_get_microvolt(struct dev_pm_opp *opp, struct device *dev) | |
727 | { | |
728 | u32 microvolt[3] = {0}; | |
729 | int count, ret; | |
730 | ||
731 | count = of_property_count_u32_elems(opp->np, "opp-microvolt"); | |
732 | if (!count) | |
733 | return 0; | |
734 | ||
735 | /* There can be one or three elements here */ | |
736 | if (count != 1 && count != 3) { | |
737 | dev_err(dev, "%s: Invalid number of elements in opp-microvolt property (%d)\n", | |
738 | __func__, count); | |
739 | return -EINVAL; | |
740 | } | |
741 | ||
742 | ret = of_property_read_u32_array(opp->np, "opp-microvolt", microvolt, | |
743 | count); | |
744 | if (ret) { | |
745 | dev_err(dev, "%s: error parsing opp-microvolt: %d\n", __func__, | |
746 | ret); | |
747 | return -EINVAL; | |
748 | } | |
749 | ||
750 | opp->u_volt = microvolt[0]; | |
751 | opp->u_volt_min = microvolt[1]; | |
752 | opp->u_volt_max = microvolt[2]; | |
753 | ||
754 | return 0; | |
755 | } | |
756 | ||
757 | /** | |
758 | * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings) | |
759 | * @dev: device for which we do this operation | |
760 | * @np: device node | |
761 | * | |
762 | * This function adds an opp definition to the opp list and returns status. The | |
763 | * opp can be controlled using dev_pm_opp_enable/disable functions and may be | |
764 | * removed by dev_pm_opp_remove. | |
765 | * | |
766 | * Locking: The internal device_opp and opp structures are RCU protected. | |
767 | * Hence this function internally uses RCU updater strategy with mutex locks | |
768 | * to keep the integrity of the internal data structures. Callers should ensure | |
769 | * that this function is *NOT* called under RCU protection or in contexts where | |
770 | * mutex cannot be locked. | |
771 | * | |
772 | * Return: | |
773 | * 0 On success OR | |
774 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
775 | * -EEXIST Freq are same and volt are different OR | |
776 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
777 | * -ENOMEM Memory allocation failure | |
778 | * -EINVAL Failed parsing the OPP node | |
779 | */ | |
780 | static int _opp_add_static_v2(struct device *dev, struct device_node *np) | |
781 | { | |
782 | struct device_opp *dev_opp; | |
783 | struct dev_pm_opp *new_opp; | |
784 | u64 rate; | |
785 | int ret; | |
786 | ||
787 | /* Hold our list modification lock here */ | |
788 | mutex_lock(&dev_opp_list_lock); | |
789 | ||
790 | new_opp = _allocate_opp(dev, &dev_opp); | |
791 | if (!new_opp) { | |
792 | ret = -ENOMEM; | |
793 | goto unlock; | |
794 | } | |
795 | ||
796 | ret = of_property_read_u64(np, "opp-hz", &rate); | |
797 | if (ret < 0) { | |
798 | dev_err(dev, "%s: opp-hz not found\n", __func__); | |
799 | goto free_opp; | |
800 | } | |
801 | ||
802 | /* | |
803 | * Rate is defined as an unsigned long in clk API, and so casting | |
804 | * explicitly to its type. Must be fixed once rate is 64 bit | |
805 | * guaranteed in clk API. | |
806 | */ | |
807 | new_opp->rate = (unsigned long)rate; | |
808 | new_opp->turbo = of_property_read_bool(np, "turbo-mode"); | |
809 | ||
810 | new_opp->np = np; | |
811 | new_opp->dynamic = false; | |
812 | new_opp->available = true; | |
3ca9bb33 VK |
813 | of_property_read_u32(np, "clock-latency-ns", |
814 | (u32 *)&new_opp->clock_latency_ns); | |
27465902 VK |
815 | |
816 | ret = opp_get_microvolt(new_opp, dev); | |
817 | if (ret) | |
818 | goto free_opp; | |
819 | ||
820 | of_property_read_u32(np, "opp-microamp", (u32 *)&new_opp->u_amp); | |
821 | ||
822 | ret = _opp_add(new_opp, dev_opp); | |
823 | if (ret) | |
824 | goto free_opp; | |
825 | ||
3ca9bb33 VK |
826 | if (new_opp->clock_latency_ns > dev_opp->clock_latency_ns_max) |
827 | dev_opp->clock_latency_ns_max = new_opp->clock_latency_ns; | |
828 | ||
27465902 VK |
829 | mutex_unlock(&dev_opp_list_lock); |
830 | ||
3ca9bb33 | 831 | pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n", |
27465902 | 832 | __func__, new_opp->turbo, new_opp->rate, new_opp->u_volt, |
3ca9bb33 VK |
833 | new_opp->u_volt_min, new_opp->u_volt_max, |
834 | new_opp->clock_latency_ns); | |
27465902 VK |
835 | |
836 | /* | |
837 | * Notify the changes in the availability of the operable | |
838 | * frequency/voltage list. | |
839 | */ | |
840 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ADD, new_opp); | |
841 | return 0; | |
842 | ||
843 | free_opp: | |
844 | _opp_remove(dev_opp, new_opp, false); | |
845 | unlock: | |
846 | mutex_unlock(&dev_opp_list_lock); | |
847 | return ret; | |
848 | } | |
849 | ||
38393409 VK |
850 | /** |
851 | * dev_pm_opp_add() - Add an OPP table from a table definitions | |
852 | * @dev: device for which we do this operation | |
853 | * @freq: Frequency in Hz for this OPP | |
854 | * @u_volt: Voltage in uVolts for this OPP | |
855 | * | |
856 | * This function adds an opp definition to the opp list and returns status. | |
857 | * The opp is made available by default and it can be controlled using | |
858 | * dev_pm_opp_enable/disable functions. | |
859 | * | |
860 | * Locking: The internal device_opp and opp structures are RCU protected. | |
861 | * Hence this function internally uses RCU updater strategy with mutex locks | |
862 | * to keep the integrity of the internal data structures. Callers should ensure | |
863 | * that this function is *NOT* called under RCU protection or in contexts where | |
864 | * mutex cannot be locked. | |
865 | * | |
866 | * Return: | |
984f16c8 | 867 | * 0 On success OR |
38393409 | 868 | * Duplicate OPPs (both freq and volt are same) and opp->available |
984f16c8 | 869 | * -EEXIST Freq are same and volt are different OR |
38393409 | 870 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
984f16c8 | 871 | * -ENOMEM Memory allocation failure |
38393409 VK |
872 | */ |
873 | int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) | |
874 | { | |
327854c8 | 875 | return _opp_add_dynamic(dev, freq, u_volt, true); |
38393409 | 876 | } |
5d4879cd | 877 | EXPORT_SYMBOL_GPL(dev_pm_opp_add); |
e1f60b29 NM |
878 | |
879 | /** | |
327854c8 | 880 | * _opp_set_availability() - helper to set the availability of an opp |
e1f60b29 NM |
881 | * @dev: device for which we do this operation |
882 | * @freq: OPP frequency to modify availability | |
883 | * @availability_req: availability status requested for this opp | |
884 | * | |
885 | * Set the availability of an OPP with an RCU operation, opp_{enable,disable} | |
886 | * share a common logic which is isolated here. | |
887 | * | |
984f16c8 | 888 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the |
e1f60b29 NM |
889 | * copy operation, returns 0 if no modifcation was done OR modification was |
890 | * successful. | |
891 | * | |
892 | * Locking: The internal device_opp and opp structures are RCU protected. | |
893 | * Hence this function internally uses RCU updater strategy with mutex locks to | |
894 | * keep the integrity of the internal data structures. Callers should ensure | |
895 | * that this function is *NOT* called under RCU protection or in contexts where | |
896 | * mutex locking or synchronize_rcu() blocking calls cannot be used. | |
897 | */ | |
327854c8 NM |
898 | static int _opp_set_availability(struct device *dev, unsigned long freq, |
899 | bool availability_req) | |
e1f60b29 | 900 | { |
29df0ee1 | 901 | struct device_opp *dev_opp; |
47d43ba7 | 902 | struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV); |
e1f60b29 NM |
903 | int r = 0; |
904 | ||
905 | /* keep the node allocated */ | |
47d43ba7 | 906 | new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL); |
59d84ca8 | 907 | if (!new_opp) |
e1f60b29 | 908 | return -ENOMEM; |
e1f60b29 NM |
909 | |
910 | mutex_lock(&dev_opp_list_lock); | |
911 | ||
912 | /* Find the device_opp */ | |
327854c8 | 913 | dev_opp = _find_device_opp(dev); |
e1f60b29 NM |
914 | if (IS_ERR(dev_opp)) { |
915 | r = PTR_ERR(dev_opp); | |
916 | dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); | |
917 | goto unlock; | |
918 | } | |
919 | ||
920 | /* Do we have the frequency? */ | |
921 | list_for_each_entry(tmp_opp, &dev_opp->opp_list, node) { | |
922 | if (tmp_opp->rate == freq) { | |
923 | opp = tmp_opp; | |
924 | break; | |
925 | } | |
926 | } | |
927 | if (IS_ERR(opp)) { | |
928 | r = PTR_ERR(opp); | |
929 | goto unlock; | |
930 | } | |
931 | ||
932 | /* Is update really needed? */ | |
933 | if (opp->available == availability_req) | |
934 | goto unlock; | |
935 | /* copy the old data over */ | |
936 | *new_opp = *opp; | |
937 | ||
938 | /* plug in new node */ | |
939 | new_opp->available = availability_req; | |
940 | ||
941 | list_replace_rcu(&opp->node, &new_opp->node); | |
942 | mutex_unlock(&dev_opp_list_lock); | |
327854c8 | 943 | call_srcu(&dev_opp->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu); |
e1f60b29 | 944 | |
03ca370f MH |
945 | /* Notify the change of the OPP availability */ |
946 | if (availability_req) | |
cd1a068a | 947 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_ENABLE, |
03ca370f MH |
948 | new_opp); |
949 | else | |
cd1a068a | 950 | srcu_notifier_call_chain(&dev_opp->srcu_head, OPP_EVENT_DISABLE, |
03ca370f MH |
951 | new_opp); |
952 | ||
dde8437d | 953 | return 0; |
e1f60b29 NM |
954 | |
955 | unlock: | |
956 | mutex_unlock(&dev_opp_list_lock); | |
e1f60b29 NM |
957 | kfree(new_opp); |
958 | return r; | |
959 | } | |
960 | ||
961 | /** | |
5d4879cd | 962 | * dev_pm_opp_enable() - Enable a specific OPP |
e1f60b29 NM |
963 | * @dev: device for which we do this operation |
964 | * @freq: OPP frequency to enable | |
965 | * | |
966 | * Enables a provided opp. If the operation is valid, this returns 0, else the | |
967 | * corresponding error value. It is meant to be used for users an OPP available | |
5d4879cd | 968 | * after being temporarily made unavailable with dev_pm_opp_disable. |
e1f60b29 NM |
969 | * |
970 | * Locking: The internal device_opp and opp structures are RCU protected. | |
971 | * Hence this function indirectly uses RCU and mutex locks to keep the | |
972 | * integrity of the internal data structures. Callers should ensure that | |
973 | * this function is *NOT* called under RCU protection or in contexts where | |
974 | * mutex locking or synchronize_rcu() blocking calls cannot be used. | |
984f16c8 NM |
975 | * |
976 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the | |
977 | * copy operation, returns 0 if no modifcation was done OR modification was | |
978 | * successful. | |
e1f60b29 | 979 | */ |
5d4879cd | 980 | int dev_pm_opp_enable(struct device *dev, unsigned long freq) |
e1f60b29 | 981 | { |
327854c8 | 982 | return _opp_set_availability(dev, freq, true); |
e1f60b29 | 983 | } |
5d4879cd | 984 | EXPORT_SYMBOL_GPL(dev_pm_opp_enable); |
e1f60b29 NM |
985 | |
986 | /** | |
5d4879cd | 987 | * dev_pm_opp_disable() - Disable a specific OPP |
e1f60b29 NM |
988 | * @dev: device for which we do this operation |
989 | * @freq: OPP frequency to disable | |
990 | * | |
991 | * Disables a provided opp. If the operation is valid, this returns | |
992 | * 0, else the corresponding error value. It is meant to be a temporary | |
993 | * control by users to make this OPP not available until the circumstances are | |
5d4879cd | 994 | * right to make it available again (with a call to dev_pm_opp_enable). |
e1f60b29 NM |
995 | * |
996 | * Locking: The internal device_opp and opp structures are RCU protected. | |
997 | * Hence this function indirectly uses RCU and mutex locks to keep the | |
998 | * integrity of the internal data structures. Callers should ensure that | |
999 | * this function is *NOT* called under RCU protection or in contexts where | |
1000 | * mutex locking or synchronize_rcu() blocking calls cannot be used. | |
984f16c8 NM |
1001 | * |
1002 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the | |
1003 | * copy operation, returns 0 if no modifcation was done OR modification was | |
1004 | * successful. | |
e1f60b29 | 1005 | */ |
5d4879cd | 1006 | int dev_pm_opp_disable(struct device *dev, unsigned long freq) |
e1f60b29 | 1007 | { |
327854c8 | 1008 | return _opp_set_availability(dev, freq, false); |
e1f60b29 | 1009 | } |
5d4879cd | 1010 | EXPORT_SYMBOL_GPL(dev_pm_opp_disable); |
e1f60b29 | 1011 | |
03ca370f | 1012 | /** |
5d4879cd | 1013 | * dev_pm_opp_get_notifier() - find notifier_head of the device with opp |
03ca370f | 1014 | * @dev: device pointer used to lookup device OPPs. |
984f16c8 NM |
1015 | * |
1016 | * Return: pointer to notifier head if found, otherwise -ENODEV or | |
1017 | * -EINVAL based on type of error casted as pointer. value must be checked | |
1018 | * with IS_ERR to determine valid pointer or error result. | |
1019 | * | |
1020 | * Locking: This function must be called under rcu_read_lock(). dev_opp is a RCU | |
1021 | * protected pointer. The reason for the same is that the opp pointer which is | |
1022 | * returned will remain valid for use with opp_get_{voltage, freq} only while | |
1023 | * under the locked area. The pointer returned must be used prior to unlocking | |
1024 | * with rcu_read_unlock() to maintain the integrity of the pointer. | |
03ca370f | 1025 | */ |
5d4879cd | 1026 | struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev) |
03ca370f | 1027 | { |
327854c8 | 1028 | struct device_opp *dev_opp = _find_device_opp(dev); |
03ca370f MH |
1029 | |
1030 | if (IS_ERR(dev_opp)) | |
156acb16 | 1031 | return ERR_CAST(dev_opp); /* matching type */ |
03ca370f | 1032 | |
cd1a068a | 1033 | return &dev_opp->srcu_head; |
03ca370f | 1034 | } |
4679ec37 | 1035 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier); |
b496dfbc SG |
1036 | |
1037 | #ifdef CONFIG_OF | |
737002b5 VK |
1038 | /** |
1039 | * of_free_opp_table() - Free OPP table entries created from static DT entries | |
1040 | * @dev: device pointer used to lookup device OPPs. | |
1041 | * | |
1042 | * Free OPPs created using static entries present in DT. | |
1043 | * | |
1044 | * Locking: The internal device_opp and opp structures are RCU protected. | |
1045 | * Hence this function indirectly uses RCU updater strategy with mutex locks | |
1046 | * to keep the integrity of the internal data structures. Callers should ensure | |
1047 | * that this function is *NOT* called under RCU protection or in contexts where | |
1048 | * mutex cannot be locked. | |
1049 | */ | |
1050 | void of_free_opp_table(struct device *dev) | |
1051 | { | |
1052 | struct device_opp *dev_opp; | |
1053 | struct dev_pm_opp *opp, *tmp; | |
1054 | ||
1055 | /* Check for existing list for 'dev' */ | |
1056 | dev_opp = _find_device_opp(dev); | |
1057 | if (IS_ERR(dev_opp)) { | |
1058 | int error = PTR_ERR(dev_opp); | |
1059 | ||
1060 | if (error != -ENODEV) | |
1061 | WARN(1, "%s: dev_opp: %d\n", | |
1062 | IS_ERR_OR_NULL(dev) ? | |
1063 | "Invalid device" : dev_name(dev), | |
1064 | error); | |
1065 | return; | |
1066 | } | |
1067 | ||
1068 | /* Hold our list modification lock here */ | |
1069 | mutex_lock(&dev_opp_list_lock); | |
1070 | ||
1071 | /* Free static OPPs */ | |
1072 | list_for_each_entry_safe(opp, tmp, &dev_opp->opp_list, node) { | |
1073 | if (!opp->dynamic) | |
23dacf6d | 1074 | _opp_remove(dev_opp, opp, true); |
737002b5 VK |
1075 | } |
1076 | ||
1077 | mutex_unlock(&dev_opp_list_lock); | |
1078 | } | |
1079 | EXPORT_SYMBOL_GPL(of_free_opp_table); | |
1080 | ||
27465902 VK |
1081 | /* Returns opp descriptor node from its phandle. Caller must do of_node_put() */ |
1082 | static struct device_node * | |
1083 | _of_get_opp_desc_node_from_prop(struct device *dev, const struct property *prop) | |
1084 | { | |
1085 | struct device_node *opp_np; | |
1086 | ||
1087 | opp_np = of_find_node_by_phandle(be32_to_cpup(prop->value)); | |
1088 | if (!opp_np) { | |
1089 | dev_err(dev, "%s: Prop: %s contains invalid opp desc phandle\n", | |
1090 | __func__, prop->name); | |
1091 | return ERR_PTR(-EINVAL); | |
1092 | } | |
1093 | ||
1094 | return opp_np; | |
1095 | } | |
1096 | ||
1097 | /* Initializes OPP tables based on new bindings */ | |
1098 | static int _of_init_opp_table_v2(struct device *dev, | |
1099 | const struct property *prop) | |
1100 | { | |
1101 | struct device_node *opp_np, *np; | |
1102 | int ret = 0, count = 0; | |
1103 | ||
1104 | if (!prop->value) | |
1105 | return -ENODATA; | |
1106 | ||
1107 | /* Get opp node */ | |
1108 | opp_np = _of_get_opp_desc_node_from_prop(dev, prop); | |
1109 | if (IS_ERR(opp_np)) | |
1110 | return PTR_ERR(opp_np); | |
1111 | ||
1112 | /* We have opp-list node now, iterate over it and add OPPs */ | |
1113 | for_each_available_child_of_node(opp_np, np) { | |
1114 | count++; | |
1115 | ||
1116 | ret = _opp_add_static_v2(dev, np); | |
1117 | if (ret) { | |
1118 | dev_err(dev, "%s: Failed to add OPP, %d\n", __func__, | |
1119 | ret); | |
1120 | break; | |
1121 | } | |
1122 | } | |
1123 | ||
1124 | /* There should be one of more OPP defined */ | |
1125 | if (WARN_ON(!count)) | |
1126 | goto put_opp_np; | |
1127 | ||
1128 | if (ret) | |
1129 | of_free_opp_table(dev); | |
1130 | ||
1131 | put_opp_np: | |
1132 | of_node_put(opp_np); | |
1133 | ||
1134 | return ret; | |
1135 | } | |
1136 | ||
1137 | /* Initializes OPP tables based on old-deprecated bindings */ | |
1138 | static int _of_init_opp_table_v1(struct device *dev) | |
b496dfbc SG |
1139 | { |
1140 | const struct property *prop; | |
1141 | const __be32 *val; | |
1142 | int nr; | |
1143 | ||
1144 | prop = of_find_property(dev->of_node, "operating-points", NULL); | |
1145 | if (!prop) | |
1146 | return -ENODEV; | |
1147 | if (!prop->value) | |
1148 | return -ENODATA; | |
1149 | ||
1150 | /* | |
1151 | * Each OPP is a set of tuples consisting of frequency and | |
1152 | * voltage like <freq-kHz vol-uV>. | |
1153 | */ | |
1154 | nr = prop->length / sizeof(u32); | |
1155 | if (nr % 2) { | |
1156 | dev_err(dev, "%s: Invalid OPP list\n", __func__); | |
1157 | return -EINVAL; | |
1158 | } | |
1159 | ||
1160 | val = prop->value; | |
1161 | while (nr) { | |
1162 | unsigned long freq = be32_to_cpup(val++) * 1000; | |
1163 | unsigned long volt = be32_to_cpup(val++); | |
1164 | ||
327854c8 | 1165 | if (_opp_add_dynamic(dev, freq, volt, false)) |
b496dfbc SG |
1166 | dev_warn(dev, "%s: Failed to add OPP %ld\n", |
1167 | __func__, freq); | |
b496dfbc SG |
1168 | nr -= 2; |
1169 | } | |
1170 | ||
1171 | return 0; | |
1172 | } | |
27465902 VK |
1173 | |
1174 | /** | |
1175 | * of_init_opp_table() - Initialize opp table from device tree | |
1176 | * @dev: device pointer used to lookup device OPPs. | |
1177 | * | |
1178 | * Register the initial OPP table with the OPP library for given device. | |
1179 | * | |
1180 | * Locking: The internal device_opp and opp structures are RCU protected. | |
1181 | * Hence this function indirectly uses RCU updater strategy with mutex locks | |
1182 | * to keep the integrity of the internal data structures. Callers should ensure | |
1183 | * that this function is *NOT* called under RCU protection or in contexts where | |
1184 | * mutex cannot be locked. | |
1185 | * | |
1186 | * Return: | |
1187 | * 0 On success OR | |
1188 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
1189 | * -EEXIST Freq are same and volt are different OR | |
1190 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
1191 | * -ENOMEM Memory allocation failure | |
1192 | * -ENODEV when 'operating-points' property is not found or is invalid data | |
1193 | * in device node. | |
1194 | * -ENODATA when empty 'operating-points' property is found | |
1195 | * -EINVAL when invalid entries are found in opp-v2 table | |
1196 | */ | |
1197 | int of_init_opp_table(struct device *dev) | |
1198 | { | |
1199 | const struct property *prop; | |
1200 | ||
1201 | /* | |
1202 | * OPPs have two version of bindings now. The older one is deprecated, | |
1203 | * try for the new binding first. | |
1204 | */ | |
1205 | prop = of_find_property(dev->of_node, "operating-points-v2", NULL); | |
1206 | if (!prop) { | |
1207 | /* | |
1208 | * Try old-deprecated bindings for backward compatibility with | |
1209 | * older dtbs. | |
1210 | */ | |
1211 | return _of_init_opp_table_v1(dev); | |
1212 | } | |
1213 | ||
1214 | return _of_init_opp_table_v2(dev, prop); | |
1215 | } | |
74c46c6e | 1216 | EXPORT_SYMBOL_GPL(of_init_opp_table); |
b496dfbc | 1217 | #endif |