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