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