]>
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 | ||
d54974c2 | 16 | #include <linux/clk.h> |
e1f60b29 NM |
17 | #include <linux/errno.h> |
18 | #include <linux/err.h> | |
e1f60b29 | 19 | #include <linux/slab.h> |
51990e82 | 20 | #include <linux/device.h> |
80126ce7 | 21 | #include <linux/export.h> |
9f8ea969 | 22 | #include <linux/regulator/consumer.h> |
e1f60b29 | 23 | |
f59d3ee8 | 24 | #include "opp.h" |
e1f60b29 NM |
25 | |
26 | /* | |
2c2709dc VK |
27 | * The root of the list of all opp-tables. All opp_table structures branch off |
28 | * from here, with each opp_table containing the list of opps it supports in | |
e1f60b29 NM |
29 | * various states of availability. |
30 | */ | |
f47b72a1 | 31 | LIST_HEAD(opp_tables); |
e1f60b29 | 32 | /* Lock to allow exclusive modification to the device and opp lists */ |
2c2709dc | 33 | DEFINE_MUTEX(opp_table_lock); |
e1f60b29 | 34 | |
b02ded24 DT |
35 | #define opp_rcu_lockdep_assert() \ |
36 | do { \ | |
f78f5b90 | 37 | RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ |
2c2709dc VK |
38 | !lockdep_is_held(&opp_table_lock), \ |
39 | "Missing rcu_read_lock() or " \ | |
40 | "opp_table_lock protection"); \ | |
b02ded24 DT |
41 | } while (0) |
42 | ||
2c2709dc VK |
43 | static struct opp_device *_find_opp_dev(const struct device *dev, |
44 | struct opp_table *opp_table) | |
06441658 | 45 | { |
2c2709dc | 46 | struct opp_device *opp_dev; |
06441658 | 47 | |
2c2709dc VK |
48 | list_for_each_entry(opp_dev, &opp_table->dev_list, node) |
49 | if (opp_dev->dev == dev) | |
50 | return opp_dev; | |
06441658 VK |
51 | |
52 | return NULL; | |
53 | } | |
54 | ||
e1f60b29 | 55 | /** |
2c2709dc VK |
56 | * _find_opp_table() - find opp_table struct using device pointer |
57 | * @dev: device pointer used to lookup OPP table | |
e1f60b29 | 58 | * |
2c2709dc VK |
59 | * Search OPP table for one containing matching device. Does a RCU reader |
60 | * operation to grab the pointer needed. | |
e1f60b29 | 61 | * |
2c2709dc | 62 | * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or |
e1f60b29 NM |
63 | * -EINVAL based on type of error. |
64 | * | |
0597e818 | 65 | * Locking: For readers, this function must be called under rcu_read_lock(). |
2c2709dc | 66 | * opp_table is a RCU protected pointer, which means that opp_table is valid |
0597e818 VK |
67 | * as long as we are under RCU lock. |
68 | * | |
2c2709dc | 69 | * For Writers, this function must be called with opp_table_lock held. |
e1f60b29 | 70 | */ |
2c2709dc | 71 | struct opp_table *_find_opp_table(struct device *dev) |
e1f60b29 | 72 | { |
2c2709dc | 73 | struct opp_table *opp_table; |
e1f60b29 | 74 | |
0597e818 VK |
75 | opp_rcu_lockdep_assert(); |
76 | ||
50a3cb04 | 77 | if (IS_ERR_OR_NULL(dev)) { |
e1f60b29 NM |
78 | pr_err("%s: Invalid parameters\n", __func__); |
79 | return ERR_PTR(-EINVAL); | |
80 | } | |
81 | ||
2c2709dc VK |
82 | list_for_each_entry_rcu(opp_table, &opp_tables, node) |
83 | if (_find_opp_dev(dev, opp_table)) | |
84 | return opp_table; | |
e1f60b29 | 85 | |
06441658 | 86 | return ERR_PTR(-ENODEV); |
e1f60b29 NM |
87 | } |
88 | ||
89 | /** | |
d6d00742 | 90 | * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp |
e1f60b29 NM |
91 | * @opp: opp for which voltage has to be returned for |
92 | * | |
984f16c8 | 93 | * Return: voltage in micro volt corresponding to the opp, else |
e1f60b29 NM |
94 | * return 0 |
95 | * | |
dfbe4678 VK |
96 | * This is useful only for devices with single power supply. |
97 | * | |
e1f60b29 NM |
98 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu |
99 | * protected pointer. This means that opp which could have been fetched by | |
100 | * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are | |
101 | * under RCU lock. The pointer returned by the opp_find_freq family must be | |
102 | * used in the same section as the usage of this function with the pointer | |
103 | * prior to unlocking with rcu_read_unlock() to maintain the integrity of the | |
104 | * pointer. | |
105 | */ | |
47d43ba7 | 106 | unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) |
e1f60b29 | 107 | { |
47d43ba7 | 108 | struct dev_pm_opp *tmp_opp; |
e1f60b29 NM |
109 | unsigned long v = 0; |
110 | ||
04bf1c7f KK |
111 | opp_rcu_lockdep_assert(); |
112 | ||
e1f60b29 | 113 | tmp_opp = rcu_dereference(opp); |
d6d00742 | 114 | if (IS_ERR_OR_NULL(tmp_opp)) |
e1f60b29 NM |
115 | pr_err("%s: Invalid parameters\n", __func__); |
116 | else | |
dfbe4678 | 117 | v = tmp_opp->supplies[0].u_volt; |
e1f60b29 NM |
118 | |
119 | return v; | |
120 | } | |
5d4879cd | 121 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage); |
e1f60b29 NM |
122 | |
123 | /** | |
5d4879cd | 124 | * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp |
e1f60b29 NM |
125 | * @opp: opp for which frequency has to be returned for |
126 | * | |
984f16c8 | 127 | * Return: frequency in hertz corresponding to the opp, else |
e1f60b29 NM |
128 | * return 0 |
129 | * | |
130 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
131 | * protected pointer. This means that opp which could have been fetched by | |
132 | * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are | |
133 | * under RCU lock. The pointer returned by the opp_find_freq family must be | |
134 | * used in the same section as the usage of this function with the pointer | |
135 | * prior to unlocking with rcu_read_unlock() to maintain the integrity of the | |
136 | * pointer. | |
137 | */ | |
47d43ba7 | 138 | unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp) |
e1f60b29 | 139 | { |
47d43ba7 | 140 | struct dev_pm_opp *tmp_opp; |
e1f60b29 NM |
141 | unsigned long f = 0; |
142 | ||
04bf1c7f KK |
143 | opp_rcu_lockdep_assert(); |
144 | ||
e1f60b29 | 145 | tmp_opp = rcu_dereference(opp); |
50a3cb04 | 146 | if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available) |
e1f60b29 NM |
147 | pr_err("%s: Invalid parameters\n", __func__); |
148 | else | |
149 | f = tmp_opp->rate; | |
150 | ||
151 | return f; | |
152 | } | |
5d4879cd | 153 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq); |
e1f60b29 | 154 | |
19445b25 BZ |
155 | /** |
156 | * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not | |
157 | * @opp: opp for which turbo mode is being verified | |
158 | * | |
159 | * Turbo OPPs are not for normal use, and can be enabled (under certain | |
160 | * conditions) for short duration of times to finish high throughput work | |
161 | * quickly. Running on them for longer times may overheat the chip. | |
162 | * | |
163 | * Return: true if opp is turbo opp, else false. | |
164 | * | |
165 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
166 | * protected pointer. This means that opp which could have been fetched by | |
167 | * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are | |
168 | * under RCU lock. The pointer returned by the opp_find_freq family must be | |
169 | * used in the same section as the usage of this function with the pointer | |
170 | * prior to unlocking with rcu_read_unlock() to maintain the integrity of the | |
171 | * pointer. | |
172 | */ | |
173 | bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp) | |
174 | { | |
175 | struct dev_pm_opp *tmp_opp; | |
176 | ||
177 | opp_rcu_lockdep_assert(); | |
178 | ||
179 | tmp_opp = rcu_dereference(opp); | |
180 | if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available) { | |
181 | pr_err("%s: Invalid parameters\n", __func__); | |
182 | return false; | |
183 | } | |
184 | ||
185 | return tmp_opp->turbo; | |
186 | } | |
187 | EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo); | |
188 | ||
3ca9bb33 VK |
189 | /** |
190 | * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds | |
191 | * @dev: device for which we do this operation | |
192 | * | |
193 | * Return: This function returns the max clock latency in nanoseconds. | |
194 | * | |
195 | * Locking: This function takes rcu_read_lock(). | |
196 | */ | |
197 | unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev) | |
198 | { | |
2c2709dc | 199 | struct opp_table *opp_table; |
3ca9bb33 VK |
200 | unsigned long clock_latency_ns; |
201 | ||
202 | rcu_read_lock(); | |
203 | ||
2c2709dc VK |
204 | opp_table = _find_opp_table(dev); |
205 | if (IS_ERR(opp_table)) | |
3ca9bb33 VK |
206 | clock_latency_ns = 0; |
207 | else | |
2c2709dc | 208 | clock_latency_ns = opp_table->clock_latency_ns_max; |
3ca9bb33 VK |
209 | |
210 | rcu_read_unlock(); | |
211 | return clock_latency_ns; | |
212 | } | |
213 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency); | |
214 | ||
dfbe4678 VK |
215 | static int _get_regulator_count(struct device *dev) |
216 | { | |
217 | struct opp_table *opp_table; | |
218 | int count; | |
219 | ||
220 | rcu_read_lock(); | |
221 | ||
222 | opp_table = _find_opp_table(dev); | |
223 | if (!IS_ERR(opp_table)) | |
224 | count = opp_table->regulator_count; | |
225 | else | |
226 | count = 0; | |
227 | ||
228 | rcu_read_unlock(); | |
229 | ||
230 | return count; | |
231 | } | |
232 | ||
655c9df9 VK |
233 | /** |
234 | * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds | |
235 | * @dev: device for which we do this operation | |
236 | * | |
237 | * Return: This function returns the max voltage latency in nanoseconds. | |
238 | * | |
239 | * Locking: This function takes rcu_read_lock(). | |
240 | */ | |
241 | unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev) | |
242 | { | |
2c2709dc | 243 | struct opp_table *opp_table; |
655c9df9 | 244 | struct dev_pm_opp *opp; |
dfbe4678 | 245 | struct regulator *reg, **regulators; |
655c9df9 | 246 | unsigned long latency_ns = 0; |
dfbe4678 VK |
247 | int ret, i, count; |
248 | struct { | |
249 | unsigned long min; | |
250 | unsigned long max; | |
251 | } *uV; | |
252 | ||
253 | count = _get_regulator_count(dev); | |
254 | ||
255 | /* Regulator may not be required for the device */ | |
256 | if (!count) | |
257 | return 0; | |
258 | ||
259 | regulators = kmalloc_array(count, sizeof(*regulators), GFP_KERNEL); | |
260 | if (!regulators) | |
261 | return 0; | |
262 | ||
263 | uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL); | |
264 | if (!uV) | |
265 | goto free_regulators; | |
655c9df9 VK |
266 | |
267 | rcu_read_lock(); | |
268 | ||
2c2709dc VK |
269 | opp_table = _find_opp_table(dev); |
270 | if (IS_ERR(opp_table)) { | |
655c9df9 | 271 | rcu_read_unlock(); |
dfbe4678 | 272 | goto free_uV; |
655c9df9 VK |
273 | } |
274 | ||
dfbe4678 | 275 | memcpy(regulators, opp_table->regulators, count * sizeof(*regulators)); |
655c9df9 | 276 | |
dfbe4678 VK |
277 | for (i = 0; i < count; i++) { |
278 | uV[i].min = ~0; | |
279 | uV[i].max = 0; | |
655c9df9 | 280 | |
dfbe4678 VK |
281 | list_for_each_entry_rcu(opp, &opp_table->opp_list, node) { |
282 | if (!opp->available) | |
283 | continue; | |
284 | ||
285 | if (opp->supplies[i].u_volt_min < uV[i].min) | |
286 | uV[i].min = opp->supplies[i].u_volt_min; | |
287 | if (opp->supplies[i].u_volt_max > uV[i].max) | |
288 | uV[i].max = opp->supplies[i].u_volt_max; | |
289 | } | |
655c9df9 VK |
290 | } |
291 | ||
292 | rcu_read_unlock(); | |
293 | ||
294 | /* | |
2c2709dc | 295 | * The caller needs to ensure that opp_table (and hence the regulator) |
655c9df9 VK |
296 | * isn't freed, while we are executing this routine. |
297 | */ | |
dfbe4678 VK |
298 | for (i = 0; reg = regulators[i], i < count; i++) { |
299 | ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max); | |
300 | if (ret > 0) | |
301 | latency_ns += ret * 1000; | |
302 | } | |
303 | ||
304 | free_uV: | |
305 | kfree(uV); | |
306 | free_regulators: | |
307 | kfree(regulators); | |
655c9df9 VK |
308 | |
309 | return latency_ns; | |
310 | } | |
311 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency); | |
312 | ||
21743447 VK |
313 | /** |
314 | * dev_pm_opp_get_max_transition_latency() - Get max transition latency in | |
315 | * nanoseconds | |
316 | * @dev: device for which we do this operation | |
317 | * | |
318 | * Return: This function returns the max transition latency, in nanoseconds, to | |
319 | * switch from one OPP to other. | |
320 | * | |
321 | * Locking: This function takes rcu_read_lock(). | |
322 | */ | |
323 | unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev) | |
324 | { | |
325 | return dev_pm_opp_get_max_volt_latency(dev) + | |
326 | dev_pm_opp_get_max_clock_latency(dev); | |
327 | } | |
328 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency); | |
329 | ||
4eafbd15 | 330 | /** |
3aa26a3b | 331 | * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz |
4eafbd15 BZ |
332 | * @dev: device for which we do this operation |
333 | * | |
3aa26a3b VK |
334 | * Return: This function returns the frequency of the OPP marked as suspend_opp |
335 | * if one is available, else returns 0; | |
4eafbd15 | 336 | */ |
3aa26a3b | 337 | unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev) |
4eafbd15 | 338 | { |
2c2709dc | 339 | struct opp_table *opp_table; |
3aa26a3b | 340 | unsigned long freq = 0; |
4eafbd15 | 341 | |
3aa26a3b | 342 | rcu_read_lock(); |
4eafbd15 | 343 | |
2c2709dc VK |
344 | opp_table = _find_opp_table(dev); |
345 | if (IS_ERR(opp_table) || !opp_table->suspend_opp || | |
346 | !opp_table->suspend_opp->available) | |
3aa26a3b VK |
347 | goto unlock; |
348 | ||
349 | freq = dev_pm_opp_get_freq(opp_table->suspend_opp); | |
4eafbd15 | 350 | |
3aa26a3b VK |
351 | unlock: |
352 | rcu_read_unlock(); | |
353 | return freq; | |
4eafbd15 | 354 | } |
3aa26a3b | 355 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq); |
4eafbd15 | 356 | |
e1f60b29 | 357 | /** |
2c2709dc | 358 | * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table |
e1f60b29 NM |
359 | * @dev: device for which we do this operation |
360 | * | |
984f16c8 | 361 | * Return: This function returns the number of available opps if there are any, |
e1f60b29 NM |
362 | * else returns 0 if none or the corresponding error value. |
363 | * | |
b4718c02 | 364 | * Locking: This function takes rcu_read_lock(). |
e1f60b29 | 365 | */ |
5d4879cd | 366 | int dev_pm_opp_get_opp_count(struct device *dev) |
e1f60b29 | 367 | { |
2c2709dc | 368 | struct opp_table *opp_table; |
47d43ba7 | 369 | struct dev_pm_opp *temp_opp; |
e1f60b29 NM |
370 | int count = 0; |
371 | ||
b4718c02 | 372 | rcu_read_lock(); |
b02ded24 | 373 | |
2c2709dc VK |
374 | opp_table = _find_opp_table(dev); |
375 | if (IS_ERR(opp_table)) { | |
376 | count = PTR_ERR(opp_table); | |
377 | dev_err(dev, "%s: OPP table not found (%d)\n", | |
b4718c02 DT |
378 | __func__, count); |
379 | goto out_unlock; | |
e1f60b29 NM |
380 | } |
381 | ||
2c2709dc | 382 | list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) { |
e1f60b29 NM |
383 | if (temp_opp->available) |
384 | count++; | |
385 | } | |
386 | ||
b4718c02 DT |
387 | out_unlock: |
388 | rcu_read_unlock(); | |
e1f60b29 NM |
389 | return count; |
390 | } | |
5d4879cd | 391 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); |
e1f60b29 NM |
392 | |
393 | /** | |
5d4879cd | 394 | * dev_pm_opp_find_freq_exact() - search for an exact frequency |
e1f60b29 NM |
395 | * @dev: device for which we do this operation |
396 | * @freq: frequency to search for | |
7ae49618 | 397 | * @available: true/false - match for available opp |
e1f60b29 | 398 | * |
2c2709dc | 399 | * Return: Searches for exact match in the opp table and returns pointer to the |
984f16c8 NM |
400 | * matching opp if found, else returns ERR_PTR in case of error and should |
401 | * be handled using IS_ERR. Error return values can be: | |
0779726c NM |
402 | * EINVAL: for bad pointer |
403 | * ERANGE: no match found for search | |
404 | * ENODEV: if device not found in list of registered devices | |
e1f60b29 NM |
405 | * |
406 | * Note: available is a modifier for the search. if available=true, then the | |
407 | * match is for exact matching frequency and is available in the stored OPP | |
408 | * table. if false, the match is for exact frequency which is not available. | |
409 | * | |
410 | * This provides a mechanism to enable an opp which is not available currently | |
411 | * or the opposite as well. | |
412 | * | |
413 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
414 | * protected pointer. The reason for the same is that the opp pointer which is | |
415 | * returned will remain valid for use with opp_get_{voltage, freq} only while | |
416 | * under the locked area. The pointer returned must be used prior to unlocking | |
417 | * with rcu_read_unlock() to maintain the integrity of the pointer. | |
418 | */ | |
47d43ba7 NM |
419 | struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, |
420 | unsigned long freq, | |
421 | bool available) | |
e1f60b29 | 422 | { |
2c2709dc | 423 | struct opp_table *opp_table; |
47d43ba7 | 424 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
e1f60b29 | 425 | |
b02ded24 DT |
426 | opp_rcu_lockdep_assert(); |
427 | ||
2c2709dc VK |
428 | opp_table = _find_opp_table(dev); |
429 | if (IS_ERR(opp_table)) { | |
430 | int r = PTR_ERR(opp_table); | |
431 | ||
432 | dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); | |
e1f60b29 NM |
433 | return ERR_PTR(r); |
434 | } | |
435 | ||
2c2709dc | 436 | list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) { |
e1f60b29 NM |
437 | if (temp_opp->available == available && |
438 | temp_opp->rate == freq) { | |
439 | opp = temp_opp; | |
440 | break; | |
441 | } | |
442 | } | |
443 | ||
444 | return opp; | |
445 | } | |
5d4879cd | 446 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); |
e1f60b29 | 447 | |
067b7ce0 JZ |
448 | static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table, |
449 | unsigned long *freq) | |
450 | { | |
451 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); | |
452 | ||
453 | list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) { | |
454 | if (temp_opp->available && temp_opp->rate >= *freq) { | |
455 | opp = temp_opp; | |
456 | *freq = opp->rate; | |
457 | break; | |
458 | } | |
459 | } | |
460 | ||
461 | return opp; | |
462 | } | |
463 | ||
e1f60b29 | 464 | /** |
5d4879cd | 465 | * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq |
e1f60b29 NM |
466 | * @dev: device for which we do this operation |
467 | * @freq: Start frequency | |
468 | * | |
469 | * Search for the matching ceil *available* OPP from a starting freq | |
470 | * for a device. | |
471 | * | |
984f16c8 | 472 | * Return: matching *opp and refreshes *freq accordingly, else returns |
0779726c NM |
473 | * ERR_PTR in case of error and should be handled using IS_ERR. Error return |
474 | * values can be: | |
475 | * EINVAL: for bad pointer | |
476 | * ERANGE: no match found for search | |
477 | * ENODEV: if device not found in list of registered devices | |
e1f60b29 NM |
478 | * |
479 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
480 | * protected pointer. The reason for the same is that the opp pointer which is | |
481 | * returned will remain valid for use with opp_get_{voltage, freq} only while | |
482 | * under the locked area. The pointer returned must be used prior to unlocking | |
483 | * with rcu_read_unlock() to maintain the integrity of the pointer. | |
484 | */ | |
47d43ba7 NM |
485 | struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, |
486 | unsigned long *freq) | |
e1f60b29 | 487 | { |
2c2709dc | 488 | struct opp_table *opp_table; |
e1f60b29 | 489 | |
b02ded24 DT |
490 | opp_rcu_lockdep_assert(); |
491 | ||
e1f60b29 NM |
492 | if (!dev || !freq) { |
493 | dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); | |
494 | return ERR_PTR(-EINVAL); | |
495 | } | |
496 | ||
2c2709dc VK |
497 | opp_table = _find_opp_table(dev); |
498 | if (IS_ERR(opp_table)) | |
499 | return ERR_CAST(opp_table); | |
e1f60b29 | 500 | |
067b7ce0 | 501 | return _find_freq_ceil(opp_table, freq); |
e1f60b29 | 502 | } |
5d4879cd | 503 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); |
e1f60b29 NM |
504 | |
505 | /** | |
5d4879cd | 506 | * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq |
e1f60b29 NM |
507 | * @dev: device for which we do this operation |
508 | * @freq: Start frequency | |
509 | * | |
510 | * Search for the matching floor *available* OPP from a starting freq | |
511 | * for a device. | |
512 | * | |
984f16c8 | 513 | * Return: matching *opp and refreshes *freq accordingly, else returns |
0779726c NM |
514 | * ERR_PTR in case of error and should be handled using IS_ERR. Error return |
515 | * values can be: | |
516 | * EINVAL: for bad pointer | |
517 | * ERANGE: no match found for search | |
518 | * ENODEV: if device not found in list of registered devices | |
e1f60b29 NM |
519 | * |
520 | * Locking: This function must be called under rcu_read_lock(). opp is a rcu | |
521 | * protected pointer. The reason for the same is that the opp pointer which is | |
522 | * returned will remain valid for use with opp_get_{voltage, freq} only while | |
523 | * under the locked area. The pointer returned must be used prior to unlocking | |
524 | * with rcu_read_unlock() to maintain the integrity of the pointer. | |
525 | */ | |
47d43ba7 NM |
526 | struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, |
527 | unsigned long *freq) | |
e1f60b29 | 528 | { |
2c2709dc | 529 | struct opp_table *opp_table; |
47d43ba7 | 530 | struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); |
e1f60b29 | 531 | |
b02ded24 DT |
532 | opp_rcu_lockdep_assert(); |
533 | ||
e1f60b29 NM |
534 | if (!dev || !freq) { |
535 | dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); | |
536 | return ERR_PTR(-EINVAL); | |
537 | } | |
538 | ||
2c2709dc VK |
539 | opp_table = _find_opp_table(dev); |
540 | if (IS_ERR(opp_table)) | |
541 | return ERR_CAST(opp_table); | |
e1f60b29 | 542 | |
2c2709dc | 543 | list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) { |
e1f60b29 NM |
544 | if (temp_opp->available) { |
545 | /* go to the next node, before choosing prev */ | |
546 | if (temp_opp->rate > *freq) | |
547 | break; | |
548 | else | |
549 | opp = temp_opp; | |
550 | } | |
551 | } | |
552 | if (!IS_ERR(opp)) | |
553 | *freq = opp->rate; | |
554 | ||
555 | return opp; | |
556 | } | |
5d4879cd | 557 | EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); |
e1f60b29 | 558 | |
6a0712f6 | 559 | /* |
2c2709dc | 560 | * The caller needs to ensure that opp_table (and hence the clk) isn't freed, |
6a0712f6 VK |
561 | * while clk returned here is used. |
562 | */ | |
563 | static struct clk *_get_opp_clk(struct device *dev) | |
564 | { | |
2c2709dc | 565 | struct opp_table *opp_table; |
6a0712f6 VK |
566 | struct clk *clk; |
567 | ||
568 | rcu_read_lock(); | |
569 | ||
2c2709dc VK |
570 | opp_table = _find_opp_table(dev); |
571 | if (IS_ERR(opp_table)) { | |
6a0712f6 | 572 | dev_err(dev, "%s: device opp doesn't exist\n", __func__); |
2c2709dc | 573 | clk = ERR_CAST(opp_table); |
6a0712f6 VK |
574 | goto unlock; |
575 | } | |
576 | ||
2c2709dc | 577 | clk = opp_table->clk; |
6a0712f6 VK |
578 | if (IS_ERR(clk)) |
579 | dev_err(dev, "%s: No clock available for the device\n", | |
580 | __func__); | |
581 | ||
582 | unlock: | |
583 | rcu_read_unlock(); | |
584 | return clk; | |
585 | } | |
586 | ||
587 | static int _set_opp_voltage(struct device *dev, struct regulator *reg, | |
ce31781a | 588 | struct dev_pm_opp_supply *supply) |
6a0712f6 VK |
589 | { |
590 | int ret; | |
591 | ||
592 | /* Regulator not available for device */ | |
593 | if (IS_ERR(reg)) { | |
594 | dev_dbg(dev, "%s: regulator not available: %ld\n", __func__, | |
595 | PTR_ERR(reg)); | |
596 | return 0; | |
597 | } | |
598 | ||
ce31781a VK |
599 | dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, |
600 | supply->u_volt_min, supply->u_volt, supply->u_volt_max); | |
6a0712f6 | 601 | |
ce31781a VK |
602 | ret = regulator_set_voltage_triplet(reg, supply->u_volt_min, |
603 | supply->u_volt, supply->u_volt_max); | |
6a0712f6 VK |
604 | if (ret) |
605 | dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", | |
ce31781a VK |
606 | __func__, supply->u_volt_min, supply->u_volt, |
607 | supply->u_volt_max, ret); | |
6a0712f6 VK |
608 | |
609 | return ret; | |
610 | } | |
611 | ||
94735585 VK |
612 | static inline int |
613 | _generic_set_opp_clk_only(struct device *dev, struct clk *clk, | |
614 | unsigned long old_freq, unsigned long freq) | |
615 | { | |
616 | int ret; | |
617 | ||
618 | ret = clk_set_rate(clk, freq); | |
619 | if (ret) { | |
620 | dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, | |
621 | ret); | |
622 | } | |
623 | ||
624 | return ret; | |
625 | } | |
626 | ||
627 | static int _generic_set_opp(struct dev_pm_set_opp_data *data) | |
628 | { | |
629 | struct dev_pm_opp_supply *old_supply = data->old_opp.supplies; | |
630 | struct dev_pm_opp_supply *new_supply = data->new_opp.supplies; | |
631 | unsigned long old_freq = data->old_opp.rate, freq = data->new_opp.rate; | |
632 | struct regulator *reg = data->regulators[0]; | |
633 | struct device *dev= data->dev; | |
634 | int ret; | |
635 | ||
636 | /* This function only supports single regulator per device */ | |
637 | if (WARN_ON(data->regulator_count > 1)) { | |
638 | dev_err(dev, "multiple regulators are not supported\n"); | |
639 | return -EINVAL; | |
640 | } | |
641 | ||
642 | /* Scaling up? Scale voltage before frequency */ | |
643 | if (freq > old_freq) { | |
644 | ret = _set_opp_voltage(dev, reg, new_supply); | |
645 | if (ret) | |
646 | goto restore_voltage; | |
647 | } | |
648 | ||
649 | /* Change frequency */ | |
650 | ret = _generic_set_opp_clk_only(dev, data->clk, old_freq, freq); | |
651 | if (ret) | |
652 | goto restore_voltage; | |
653 | ||
654 | /* Scaling down? Scale voltage after frequency */ | |
655 | if (freq < old_freq) { | |
656 | ret = _set_opp_voltage(dev, reg, new_supply); | |
657 | if (ret) | |
658 | goto restore_freq; | |
659 | } | |
660 | ||
661 | return 0; | |
662 | ||
663 | restore_freq: | |
664 | if (_generic_set_opp_clk_only(dev, data->clk, freq, old_freq)) | |
665 | dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", | |
666 | __func__, old_freq); | |
667 | restore_voltage: | |
668 | /* This shouldn't harm even if the voltages weren't updated earlier */ | |
669 | if (old_supply->u_volt) | |
670 | _set_opp_voltage(dev, reg, old_supply); | |
671 | ||
672 | return ret; | |
673 | } | |
674 | ||
6a0712f6 VK |
675 | /** |
676 | * dev_pm_opp_set_rate() - Configure new OPP based on frequency | |
677 | * @dev: device for which we do this operation | |
678 | * @target_freq: frequency to achieve | |
679 | * | |
680 | * This configures the power-supplies and clock source to the levels specified | |
681 | * by the OPP corresponding to the target_freq. | |
682 | * | |
683 | * Locking: This function takes rcu_read_lock(). | |
684 | */ | |
685 | int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) | |
686 | { | |
2c2709dc | 687 | struct opp_table *opp_table; |
94735585 | 688 | unsigned long freq, old_freq; |
4dab160e | 689 | int (*set_opp)(struct dev_pm_set_opp_data *data); |
6a0712f6 | 690 | struct dev_pm_opp *old_opp, *opp; |
94735585 VK |
691 | struct regulator **regulators; |
692 | struct dev_pm_set_opp_data *data; | |
6a0712f6 | 693 | struct clk *clk; |
94735585 | 694 | int ret, size; |
6a0712f6 VK |
695 | |
696 | if (unlikely(!target_freq)) { | |
697 | dev_err(dev, "%s: Invalid target frequency %lu\n", __func__, | |
698 | target_freq); | |
699 | return -EINVAL; | |
700 | } | |
701 | ||
702 | clk = _get_opp_clk(dev); | |
703 | if (IS_ERR(clk)) | |
704 | return PTR_ERR(clk); | |
705 | ||
706 | freq = clk_round_rate(clk, target_freq); | |
707 | if ((long)freq <= 0) | |
708 | freq = target_freq; | |
709 | ||
710 | old_freq = clk_get_rate(clk); | |
711 | ||
712 | /* Return early if nothing to do */ | |
713 | if (old_freq == freq) { | |
714 | dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n", | |
715 | __func__, freq); | |
716 | return 0; | |
717 | } | |
718 | ||
719 | rcu_read_lock(); | |
720 | ||
2c2709dc VK |
721 | opp_table = _find_opp_table(dev); |
722 | if (IS_ERR(opp_table)) { | |
6a0712f6 VK |
723 | dev_err(dev, "%s: device opp doesn't exist\n", __func__); |
724 | rcu_read_unlock(); | |
2c2709dc | 725 | return PTR_ERR(opp_table); |
6a0712f6 VK |
726 | } |
727 | ||
067b7ce0 | 728 | old_opp = _find_freq_ceil(opp_table, &old_freq); |
4df27c91 | 729 | if (IS_ERR(old_opp)) { |
6a0712f6 VK |
730 | dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n", |
731 | __func__, old_freq, PTR_ERR(old_opp)); | |
732 | } | |
733 | ||
067b7ce0 | 734 | opp = _find_freq_ceil(opp_table, &freq); |
6a0712f6 VK |
735 | if (IS_ERR(opp)) { |
736 | ret = PTR_ERR(opp); | |
737 | dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", | |
738 | __func__, freq, ret); | |
739 | rcu_read_unlock(); | |
740 | return ret; | |
741 | } | |
742 | ||
94735585 VK |
743 | dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__, |
744 | old_freq, freq); | |
dfbe4678 | 745 | |
94735585 VK |
746 | regulators = opp_table->regulators; |
747 | ||
748 | /* Only frequency scaling */ | |
749 | if (!regulators) { | |
750 | rcu_read_unlock(); | |
751 | return _generic_set_opp_clk_only(dev, clk, old_freq, freq); | |
dfbe4678 VK |
752 | } |
753 | ||
4dab160e VK |
754 | if (opp_table->set_opp) |
755 | set_opp = opp_table->set_opp; | |
756 | else | |
757 | set_opp = _generic_set_opp; | |
758 | ||
94735585 VK |
759 | data = opp_table->set_opp_data; |
760 | data->regulators = regulators; | |
761 | data->regulator_count = opp_table->regulator_count; | |
762 | data->clk = clk; | |
763 | data->dev = dev; | |
764 | ||
765 | data->old_opp.rate = old_freq; | |
766 | size = sizeof(*opp->supplies) * opp_table->regulator_count; | |
ce31781a | 767 | if (IS_ERR(old_opp)) |
94735585 | 768 | memset(data->old_opp.supplies, 0, size); |
ce31781a | 769 | else |
94735585 | 770 | memcpy(data->old_opp.supplies, old_opp->supplies, size); |
6a0712f6 | 771 | |
94735585 VK |
772 | data->new_opp.rate = freq; |
773 | memcpy(data->new_opp.supplies, opp->supplies, size); | |
6a0712f6 VK |
774 | |
775 | rcu_read_unlock(); | |
776 | ||
4dab160e | 777 | return set_opp(data); |
6a0712f6 VK |
778 | } |
779 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); | |
780 | ||
2c2709dc VK |
781 | /* OPP-dev Helpers */ |
782 | static void _kfree_opp_dev_rcu(struct rcu_head *head) | |
06441658 | 783 | { |
2c2709dc | 784 | struct opp_device *opp_dev; |
06441658 | 785 | |
2c2709dc VK |
786 | opp_dev = container_of(head, struct opp_device, rcu_head); |
787 | kfree_rcu(opp_dev, rcu_head); | |
06441658 VK |
788 | } |
789 | ||
2c2709dc VK |
790 | static void _remove_opp_dev(struct opp_device *opp_dev, |
791 | struct opp_table *opp_table) | |
06441658 | 792 | { |
2c2709dc VK |
793 | opp_debug_unregister(opp_dev, opp_table); |
794 | list_del(&opp_dev->node); | |
795 | call_srcu(&opp_table->srcu_head.srcu, &opp_dev->rcu_head, | |
796 | _kfree_opp_dev_rcu); | |
06441658 VK |
797 | } |
798 | ||
2c2709dc VK |
799 | struct opp_device *_add_opp_dev(const struct device *dev, |
800 | struct opp_table *opp_table) | |
06441658 | 801 | { |
2c2709dc | 802 | struct opp_device *opp_dev; |
deaa5146 | 803 | int ret; |
06441658 | 804 | |
2c2709dc VK |
805 | opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL); |
806 | if (!opp_dev) | |
06441658 VK |
807 | return NULL; |
808 | ||
2c2709dc VK |
809 | /* Initialize opp-dev */ |
810 | opp_dev->dev = dev; | |
811 | list_add_rcu(&opp_dev->node, &opp_table->dev_list); | |
06441658 | 812 | |
2c2709dc VK |
813 | /* Create debugfs entries for the opp_table */ |
814 | ret = opp_debug_register(opp_dev, opp_table); | |
deaa5146 VK |
815 | if (ret) |
816 | dev_err(dev, "%s: Failed to register opp debugfs (%d)\n", | |
817 | __func__, ret); | |
818 | ||
2c2709dc | 819 | return opp_dev; |
06441658 VK |
820 | } |
821 | ||
b6160e26 | 822 | static struct opp_table *_allocate_opp_table(struct device *dev) |
07cce74a | 823 | { |
2c2709dc VK |
824 | struct opp_table *opp_table; |
825 | struct opp_device *opp_dev; | |
d54974c2 | 826 | int ret; |
07cce74a VK |
827 | |
828 | /* | |
2c2709dc | 829 | * Allocate a new OPP table. In the infrequent case where a new |
07cce74a VK |
830 | * device is needed to be added, we pay this penalty. |
831 | */ | |
2c2709dc VK |
832 | opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL); |
833 | if (!opp_table) | |
07cce74a VK |
834 | return NULL; |
835 | ||
2c2709dc | 836 | INIT_LIST_HEAD(&opp_table->dev_list); |
06441658 | 837 | |
2c2709dc VK |
838 | opp_dev = _add_opp_dev(dev, opp_table); |
839 | if (!opp_dev) { | |
840 | kfree(opp_table); | |
06441658 VK |
841 | return NULL; |
842 | } | |
843 | ||
f47b72a1 | 844 | _of_init_opp_table(opp_table, dev); |
50f8cfbd | 845 | |
d54974c2 | 846 | /* Find clk for the device */ |
2c2709dc VK |
847 | opp_table->clk = clk_get(dev, NULL); |
848 | if (IS_ERR(opp_table->clk)) { | |
849 | ret = PTR_ERR(opp_table->clk); | |
d54974c2 VK |
850 | if (ret != -EPROBE_DEFER) |
851 | dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, | |
852 | ret); | |
853 | } | |
854 | ||
2c2709dc VK |
855 | srcu_init_notifier_head(&opp_table->srcu_head); |
856 | INIT_LIST_HEAD(&opp_table->opp_list); | |
37a73ec0 | 857 | mutex_init(&opp_table->lock); |
f067a982 | 858 | kref_init(&opp_table->kref); |
07cce74a | 859 | |
2c2709dc VK |
860 | /* Secure the device table modification */ |
861 | list_add_rcu(&opp_table->node, &opp_tables); | |
862 | return opp_table; | |
07cce74a VK |
863 | } |
864 | ||
984f16c8 | 865 | /** |
2c2709dc | 866 | * _kfree_device_rcu() - Free opp_table RCU handler |
737002b5 | 867 | * @head: RCU head |
984f16c8 | 868 | */ |
737002b5 | 869 | static void _kfree_device_rcu(struct rcu_head *head) |
e1f60b29 | 870 | { |
2c2709dc VK |
871 | struct opp_table *opp_table = container_of(head, struct opp_table, |
872 | rcu_head); | |
6ce4184d | 873 | |
2c2709dc | 874 | kfree_rcu(opp_table, rcu_head); |
e1f60b29 | 875 | } |
38393409 | 876 | |
f067a982 | 877 | void _get_opp_table_kref(struct opp_table *opp_table) |
b6160e26 | 878 | { |
f067a982 VK |
879 | kref_get(&opp_table->kref); |
880 | } | |
881 | ||
882 | struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) | |
883 | { | |
884 | struct opp_table *opp_table; | |
885 | ||
886 | /* Hold our table modification lock here */ | |
887 | mutex_lock(&opp_table_lock); | |
888 | ||
889 | opp_table = _find_opp_table(dev); | |
890 | if (!IS_ERR(opp_table)) { | |
891 | _get_opp_table_kref(opp_table); | |
892 | goto unlock; | |
893 | } | |
894 | ||
895 | opp_table = _allocate_opp_table(dev); | |
896 | ||
897 | unlock: | |
898 | mutex_unlock(&opp_table_lock); | |
899 | ||
900 | return opp_table; | |
901 | } | |
902 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); | |
903 | ||
b83c1899 | 904 | static void _opp_table_kref_release(struct kref *kref) |
f067a982 VK |
905 | { |
906 | struct opp_table *opp_table = container_of(kref, struct opp_table, kref); | |
b6160e26 VK |
907 | struct opp_device *opp_dev; |
908 | ||
909 | /* Release clk */ | |
910 | if (!IS_ERR(opp_table->clk)) | |
911 | clk_put(opp_table->clk); | |
912 | ||
913 | opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device, | |
914 | node); | |
915 | ||
916 | _remove_opp_dev(opp_dev, opp_table); | |
917 | ||
918 | /* dev_list must be empty now */ | |
919 | WARN_ON(!list_empty(&opp_table->dev_list)); | |
920 | ||
37a73ec0 | 921 | mutex_destroy(&opp_table->lock); |
b6160e26 VK |
922 | list_del_rcu(&opp_table->node); |
923 | call_srcu(&opp_table->srcu_head.srcu, &opp_table->rcu_head, | |
924 | _kfree_device_rcu); | |
b6160e26 | 925 | |
f067a982 VK |
926 | mutex_unlock(&opp_table_lock); |
927 | } | |
928 | ||
929 | void dev_pm_opp_put_opp_table(struct opp_table *opp_table) | |
930 | { | |
931 | kref_put_mutex(&opp_table->kref, _opp_table_kref_release, | |
932 | &opp_table_lock); | |
933 | } | |
934 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); | |
935 | ||
8cd2f6e8 | 936 | void _opp_free(struct dev_pm_opp *opp) |
969fceb3 VK |
937 | { |
938 | kfree(opp); | |
969fceb3 VK |
939 | } |
940 | ||
984f16c8 NM |
941 | /** |
942 | * _kfree_opp_rcu() - Free OPP RCU handler | |
943 | * @head: RCU head | |
944 | */ | |
327854c8 | 945 | static void _kfree_opp_rcu(struct rcu_head *head) |
129eec55 VK |
946 | { |
947 | struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head); | |
948 | ||
949 | kfree_rcu(opp, rcu_head); | |
950 | } | |
951 | ||
7034764a | 952 | static void _opp_kref_release(struct kref *kref) |
129eec55 | 953 | { |
7034764a VK |
954 | struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); |
955 | struct opp_table *opp_table = opp->opp_table; | |
37a73ec0 | 956 | |
129eec55 VK |
957 | /* |
958 | * Notify the changes in the availability of the operable | |
959 | * frequency/voltage list. | |
960 | */ | |
969fceb3 | 961 | srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_REMOVE, opp); |
deaa5146 | 962 | opp_debug_remove_one(opp); |
129eec55 | 963 | list_del_rcu(&opp->node); |
2c2709dc | 964 | call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu); |
129eec55 | 965 | |
37a73ec0 | 966 | mutex_unlock(&opp_table->lock); |
31641cda | 967 | dev_pm_opp_put_opp_table(opp_table); |
129eec55 VK |
968 | } |
969 | ||
7034764a VK |
970 | void dev_pm_opp_put(struct dev_pm_opp *opp) |
971 | { | |
972 | kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock); | |
973 | } | |
974 | EXPORT_SYMBOL_GPL(dev_pm_opp_put); | |
975 | ||
129eec55 | 976 | /** |
2c2709dc | 977 | * dev_pm_opp_remove() - Remove an OPP from OPP table |
129eec55 VK |
978 | * @dev: device for which we do this operation |
979 | * @freq: OPP to remove with matching 'freq' | |
980 | * | |
2c2709dc | 981 | * This function removes an opp from the opp table. |
984f16c8 | 982 | * |
2c2709dc | 983 | * Locking: The internal opp_table and opp structures are RCU protected. |
984f16c8 NM |
984 | * Hence this function internally uses RCU updater strategy with mutex locks |
985 | * to 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 cannot be locked. | |
129eec55 VK |
988 | */ |
989 | void dev_pm_opp_remove(struct device *dev, unsigned long freq) | |
990 | { | |
991 | struct dev_pm_opp *opp; | |
2c2709dc | 992 | struct opp_table *opp_table; |
129eec55 VK |
993 | bool found = false; |
994 | ||
2c2709dc VK |
995 | /* Hold our table modification lock here */ |
996 | mutex_lock(&opp_table_lock); | |
129eec55 | 997 | |
2c2709dc VK |
998 | opp_table = _find_opp_table(dev); |
999 | if (IS_ERR(opp_table)) | |
129eec55 VK |
1000 | goto unlock; |
1001 | ||
37a73ec0 VK |
1002 | mutex_lock(&opp_table->lock); |
1003 | ||
2c2709dc | 1004 | list_for_each_entry(opp, &opp_table->opp_list, node) { |
129eec55 VK |
1005 | if (opp->rate == freq) { |
1006 | found = true; | |
1007 | break; | |
1008 | } | |
1009 | } | |
1010 | ||
37a73ec0 VK |
1011 | mutex_unlock(&opp_table->lock); |
1012 | ||
129eec55 VK |
1013 | if (!found) { |
1014 | dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", | |
1015 | __func__, freq); | |
1016 | goto unlock; | |
1017 | } | |
1018 | ||
7034764a | 1019 | dev_pm_opp_put(opp); |
129eec55 | 1020 | unlock: |
2c2709dc | 1021 | mutex_unlock(&opp_table_lock); |
129eec55 VK |
1022 | } |
1023 | EXPORT_SYMBOL_GPL(dev_pm_opp_remove); | |
1024 | ||
8cd2f6e8 | 1025 | struct dev_pm_opp *_opp_allocate(struct opp_table *table) |
e1f60b29 | 1026 | { |
23dacf6d | 1027 | struct dev_pm_opp *opp; |
dfbe4678 | 1028 | int count, supply_size; |
e1f60b29 | 1029 | |
dfbe4678 VK |
1030 | /* Allocate space for at least one supply */ |
1031 | count = table->regulator_count ? table->regulator_count : 1; | |
1032 | supply_size = sizeof(*opp->supplies) * count; | |
e1f60b29 | 1033 | |
dfbe4678 VK |
1034 | /* allocate new OPP node and supplies structures */ |
1035 | opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL); | |
8cd2f6e8 | 1036 | if (!opp) |
23dacf6d | 1037 | return NULL; |
23dacf6d | 1038 | |
dfbe4678 VK |
1039 | /* Put the supplies at the end of the OPP structure as an empty array */ |
1040 | opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); | |
1041 | INIT_LIST_HEAD(&opp->node); | |
1042 | ||
23dacf6d VK |
1043 | return opp; |
1044 | } | |
1045 | ||
7d34d56e | 1046 | static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, |
2c2709dc | 1047 | struct opp_table *opp_table) |
7d34d56e | 1048 | { |
dfbe4678 VK |
1049 | struct regulator *reg; |
1050 | int i; | |
1051 | ||
1052 | for (i = 0; i < opp_table->regulator_count; i++) { | |
1053 | reg = opp_table->regulators[i]; | |
1054 | ||
1055 | if (!regulator_is_supported_voltage(reg, | |
1056 | opp->supplies[i].u_volt_min, | |
1057 | opp->supplies[i].u_volt_max)) { | |
1058 | pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n", | |
1059 | __func__, opp->supplies[i].u_volt_min, | |
1060 | opp->supplies[i].u_volt_max); | |
1061 | return false; | |
1062 | } | |
7d34d56e VK |
1063 | } |
1064 | ||
1065 | return true; | |
1066 | } | |
1067 | ||
7f8538eb VK |
1068 | /* |
1069 | * Returns: | |
1070 | * 0: On success. And appropriate error message for duplicate OPPs. | |
1071 | * -EBUSY: For OPP with same freq/volt and is available. The callers of | |
1072 | * _opp_add() must return 0 if they receive -EBUSY from it. This is to make | |
1073 | * sure we don't print error messages unnecessarily if different parts of | |
1074 | * kernel try to initialize the OPP table. | |
1075 | * -EEXIST: For OPP with same freq but different volt or is unavailable. This | |
1076 | * should be considered an error by the callers of _opp_add(). | |
1077 | */ | |
f47b72a1 VK |
1078 | int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, |
1079 | struct opp_table *opp_table) | |
23dacf6d VK |
1080 | { |
1081 | struct dev_pm_opp *opp; | |
37a73ec0 | 1082 | struct list_head *head; |
deaa5146 | 1083 | int ret; |
23dacf6d VK |
1084 | |
1085 | /* | |
1086 | * Insert new OPP in order of increasing frequency and discard if | |
1087 | * already present. | |
1088 | * | |
2c2709dc | 1089 | * Need to use &opp_table->opp_list in the condition part of the 'for' |
23dacf6d VK |
1090 | * loop, don't replace it with head otherwise it will become an infinite |
1091 | * loop. | |
1092 | */ | |
37a73ec0 VK |
1093 | mutex_lock(&opp_table->lock); |
1094 | head = &opp_table->opp_list; | |
1095 | ||
2c2709dc | 1096 | list_for_each_entry_rcu(opp, &opp_table->opp_list, node) { |
23dacf6d VK |
1097 | if (new_opp->rate > opp->rate) { |
1098 | head = &opp->node; | |
1099 | continue; | |
1100 | } | |
1101 | ||
1102 | if (new_opp->rate < opp->rate) | |
1103 | break; | |
1104 | ||
1105 | /* Duplicate OPPs */ | |
06441658 | 1106 | dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", |
dfbe4678 VK |
1107 | __func__, opp->rate, opp->supplies[0].u_volt, |
1108 | opp->available, new_opp->rate, | |
1109 | new_opp->supplies[0].u_volt, new_opp->available); | |
23dacf6d | 1110 | |
dfbe4678 | 1111 | /* Should we compare voltages for all regulators here ? */ |
37a73ec0 VK |
1112 | ret = opp->available && |
1113 | new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; | |
1114 | ||
1115 | mutex_unlock(&opp_table->lock); | |
1116 | return ret; | |
23dacf6d VK |
1117 | } |
1118 | ||
23dacf6d | 1119 | list_add_rcu(&new_opp->node, head); |
37a73ec0 VK |
1120 | mutex_unlock(&opp_table->lock); |
1121 | ||
1122 | new_opp->opp_table = opp_table; | |
7034764a | 1123 | kref_init(&new_opp->kref); |
23dacf6d | 1124 | |
31641cda VK |
1125 | /* Get a reference to the OPP table */ |
1126 | _get_opp_table_kref(opp_table); | |
1127 | ||
2c2709dc | 1128 | ret = opp_debug_create_one(new_opp, opp_table); |
deaa5146 VK |
1129 | if (ret) |
1130 | dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n", | |
1131 | __func__, ret); | |
1132 | ||
2c2709dc | 1133 | if (!_opp_supported_by_regulators(new_opp, opp_table)) { |
7d34d56e VK |
1134 | new_opp->available = false; |
1135 | dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n", | |
1136 | __func__, new_opp->rate); | |
1137 | } | |
1138 | ||
23dacf6d VK |
1139 | return 0; |
1140 | } | |
1141 | ||
984f16c8 | 1142 | /** |
b64b9c3f | 1143 | * _opp_add_v1() - Allocate a OPP based on v1 bindings. |
8cd2f6e8 | 1144 | * @opp_table: OPP table |
984f16c8 NM |
1145 | * @dev: device for which we do this operation |
1146 | * @freq: Frequency in Hz for this OPP | |
1147 | * @u_volt: Voltage in uVolts for this OPP | |
1148 | * @dynamic: Dynamically added OPPs. | |
1149 | * | |
2c2709dc | 1150 | * This function adds an opp definition to the opp table and returns status. |
984f16c8 NM |
1151 | * The opp is made available by default and it can be controlled using |
1152 | * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. | |
1153 | * | |
8f8d37b2 VK |
1154 | * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table |
1155 | * and freed by dev_pm_opp_of_remove_table. | |
984f16c8 | 1156 | * |
2c2709dc | 1157 | * Locking: The internal opp_table and opp structures are RCU protected. |
984f16c8 NM |
1158 | * Hence this function internally uses RCU updater strategy with mutex locks |
1159 | * to keep the integrity of the internal data structures. Callers should ensure | |
1160 | * that this function is *NOT* called under RCU protection or in contexts where | |
1161 | * mutex cannot be locked. | |
1162 | * | |
1163 | * Return: | |
1164 | * 0 On success OR | |
1165 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
1166 | * -EEXIST Freq are same and volt are different OR | |
1167 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
1168 | * -ENOMEM Memory allocation failure | |
1169 | */ | |
8cd2f6e8 VK |
1170 | int _opp_add_v1(struct opp_table *opp_table, struct device *dev, |
1171 | unsigned long freq, long u_volt, bool dynamic) | |
e1f60b29 | 1172 | { |
23dacf6d | 1173 | struct dev_pm_opp *new_opp; |
50f8cfbd | 1174 | unsigned long tol; |
6ce4184d | 1175 | int ret; |
e1f60b29 | 1176 | |
8cd2f6e8 VK |
1177 | new_opp = _opp_allocate(opp_table); |
1178 | if (!new_opp) | |
1179 | return -ENOMEM; | |
23dacf6d | 1180 | |
a7470db6 | 1181 | /* populate the opp table */ |
a7470db6 | 1182 | new_opp->rate = freq; |
2c2709dc | 1183 | tol = u_volt * opp_table->voltage_tolerance_v1 / 100; |
dfbe4678 VK |
1184 | new_opp->supplies[0].u_volt = u_volt; |
1185 | new_opp->supplies[0].u_volt_min = u_volt - tol; | |
1186 | new_opp->supplies[0].u_volt_max = u_volt + tol; | |
a7470db6 | 1187 | new_opp->available = true; |
23dacf6d | 1188 | new_opp->dynamic = dynamic; |
a7470db6 | 1189 | |
2c2709dc | 1190 | ret = _opp_add(dev, new_opp, opp_table); |
7f8538eb VK |
1191 | if (ret) { |
1192 | /* Don't return error for duplicate OPPs */ | |
1193 | if (ret == -EBUSY) | |
1194 | ret = 0; | |
6ce4184d | 1195 | goto free_opp; |
7f8538eb | 1196 | } |
64ce8545 | 1197 | |
03ca370f MH |
1198 | /* |
1199 | * Notify the changes in the availability of the operable | |
1200 | * frequency/voltage list. | |
1201 | */ | |
2c2709dc | 1202 | srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp); |
e1f60b29 | 1203 | return 0; |
6ce4184d VK |
1204 | |
1205 | free_opp: | |
8cd2f6e8 VK |
1206 | _opp_free(new_opp); |
1207 | ||
6ce4184d | 1208 | return ret; |
e1f60b29 | 1209 | } |
38393409 | 1210 | |
7de36b0a VK |
1211 | /** |
1212 | * dev_pm_opp_set_supported_hw() - Set supported platforms | |
1213 | * @dev: Device for which supported-hw has to be set. | |
1214 | * @versions: Array of hierarchy of versions to match. | |
1215 | * @count: Number of elements in the array. | |
1216 | * | |
1217 | * This is required only for the V2 bindings, and it enables a platform to | |
1218 | * specify the hierarchy of versions it supports. OPP layer will then enable | |
1219 | * OPPs, which are available for those versions, based on its 'opp-supported-hw' | |
1220 | * property. | |
7de36b0a | 1221 | */ |
fa30184d VK |
1222 | struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev, |
1223 | const u32 *versions, unsigned int count) | |
7de36b0a | 1224 | { |
2c2709dc | 1225 | struct opp_table *opp_table; |
fa30184d | 1226 | int ret; |
7de36b0a | 1227 | |
fa30184d VK |
1228 | opp_table = dev_pm_opp_get_opp_table(dev); |
1229 | if (!opp_table) | |
1230 | return ERR_PTR(-ENOMEM); | |
7de36b0a | 1231 | |
2c2709dc VK |
1232 | /* Make sure there are no concurrent readers while updating opp_table */ |
1233 | WARN_ON(!list_empty(&opp_table->opp_list)); | |
7de36b0a | 1234 | |
2c2709dc VK |
1235 | /* Do we already have a version hierarchy associated with opp_table? */ |
1236 | if (opp_table->supported_hw) { | |
7de36b0a VK |
1237 | dev_err(dev, "%s: Already have supported hardware list\n", |
1238 | __func__); | |
1239 | ret = -EBUSY; | |
1240 | goto err; | |
1241 | } | |
1242 | ||
2c2709dc | 1243 | opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions), |
7de36b0a | 1244 | GFP_KERNEL); |
2c2709dc | 1245 | if (!opp_table->supported_hw) { |
7de36b0a VK |
1246 | ret = -ENOMEM; |
1247 | goto err; | |
1248 | } | |
1249 | ||
2c2709dc | 1250 | opp_table->supported_hw_count = count; |
fa30184d VK |
1251 | |
1252 | return opp_table; | |
7de36b0a VK |
1253 | |
1254 | err: | |
fa30184d | 1255 | dev_pm_opp_put_opp_table(opp_table); |
7de36b0a | 1256 | |
fa30184d | 1257 | return ERR_PTR(ret); |
7de36b0a VK |
1258 | } |
1259 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw); | |
1260 | ||
1261 | /** | |
1262 | * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw | |
fa30184d | 1263 | * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw(). |
7de36b0a VK |
1264 | * |
1265 | * This is required only for the V2 bindings, and is called for a matching | |
2c2709dc | 1266 | * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure |
7de36b0a | 1267 | * will not be freed. |
7de36b0a | 1268 | */ |
fa30184d | 1269 | void dev_pm_opp_put_supported_hw(struct opp_table *opp_table) |
7de36b0a | 1270 | { |
2c2709dc VK |
1271 | /* Make sure there are no concurrent readers while updating opp_table */ |
1272 | WARN_ON(!list_empty(&opp_table->opp_list)); | |
7de36b0a | 1273 | |
2c2709dc | 1274 | if (!opp_table->supported_hw) { |
fa30184d VK |
1275 | pr_err("%s: Doesn't have supported hardware list\n", |
1276 | __func__); | |
1277 | return; | |
7de36b0a VK |
1278 | } |
1279 | ||
2c2709dc VK |
1280 | kfree(opp_table->supported_hw); |
1281 | opp_table->supported_hw = NULL; | |
1282 | opp_table->supported_hw_count = 0; | |
7de36b0a | 1283 | |
fa30184d | 1284 | dev_pm_opp_put_opp_table(opp_table); |
7de36b0a VK |
1285 | } |
1286 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw); | |
1287 | ||
01fb4d3c VK |
1288 | /** |
1289 | * dev_pm_opp_set_prop_name() - Set prop-extn name | |
a5da6447 | 1290 | * @dev: Device for which the prop-name has to be set. |
01fb4d3c VK |
1291 | * @name: name to postfix to properties. |
1292 | * | |
1293 | * This is required only for the V2 bindings, and it enables a platform to | |
1294 | * specify the extn to be used for certain property names. The properties to | |
1295 | * which the extension will apply are opp-microvolt and opp-microamp. OPP core | |
1296 | * should postfix the property name with -<name> while looking for them. | |
01fb4d3c | 1297 | */ |
fa30184d | 1298 | struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name) |
01fb4d3c | 1299 | { |
2c2709dc | 1300 | struct opp_table *opp_table; |
fa30184d | 1301 | int ret; |
01fb4d3c | 1302 | |
fa30184d VK |
1303 | opp_table = dev_pm_opp_get_opp_table(dev); |
1304 | if (!opp_table) | |
1305 | return ERR_PTR(-ENOMEM); | |
01fb4d3c | 1306 | |
2c2709dc VK |
1307 | /* Make sure there are no concurrent readers while updating opp_table */ |
1308 | WARN_ON(!list_empty(&opp_table->opp_list)); | |
01fb4d3c | 1309 | |
2c2709dc VK |
1310 | /* Do we already have a prop-name associated with opp_table? */ |
1311 | if (opp_table->prop_name) { | |
01fb4d3c | 1312 | dev_err(dev, "%s: Already have prop-name %s\n", __func__, |
2c2709dc | 1313 | opp_table->prop_name); |
01fb4d3c VK |
1314 | ret = -EBUSY; |
1315 | goto err; | |
1316 | } | |
1317 | ||
2c2709dc VK |
1318 | opp_table->prop_name = kstrdup(name, GFP_KERNEL); |
1319 | if (!opp_table->prop_name) { | |
01fb4d3c VK |
1320 | ret = -ENOMEM; |
1321 | goto err; | |
1322 | } | |
1323 | ||
fa30184d | 1324 | return opp_table; |
01fb4d3c VK |
1325 | |
1326 | err: | |
fa30184d | 1327 | dev_pm_opp_put_opp_table(opp_table); |
01fb4d3c | 1328 | |
fa30184d | 1329 | return ERR_PTR(ret); |
01fb4d3c VK |
1330 | } |
1331 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name); | |
1332 | ||
1333 | /** | |
1334 | * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name | |
fa30184d | 1335 | * @opp_table: OPP table returned by dev_pm_opp_set_prop_name(). |
01fb4d3c VK |
1336 | * |
1337 | * This is required only for the V2 bindings, and is called for a matching | |
2c2709dc | 1338 | * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure |
01fb4d3c | 1339 | * will not be freed. |
01fb4d3c | 1340 | */ |
fa30184d | 1341 | void dev_pm_opp_put_prop_name(struct opp_table *opp_table) |
01fb4d3c | 1342 | { |
2c2709dc VK |
1343 | /* Make sure there are no concurrent readers while updating opp_table */ |
1344 | WARN_ON(!list_empty(&opp_table->opp_list)); | |
01fb4d3c | 1345 | |
2c2709dc | 1346 | if (!opp_table->prop_name) { |
fa30184d VK |
1347 | pr_err("%s: Doesn't have a prop-name\n", __func__); |
1348 | return; | |
01fb4d3c VK |
1349 | } |
1350 | ||
2c2709dc VK |
1351 | kfree(opp_table->prop_name); |
1352 | opp_table->prop_name = NULL; | |
01fb4d3c | 1353 | |
fa30184d | 1354 | dev_pm_opp_put_opp_table(opp_table); |
01fb4d3c VK |
1355 | } |
1356 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name); | |
1357 | ||
94735585 VK |
1358 | static int _allocate_set_opp_data(struct opp_table *opp_table) |
1359 | { | |
1360 | struct dev_pm_set_opp_data *data; | |
1361 | int len, count = opp_table->regulator_count; | |
1362 | ||
1363 | if (WARN_ON(!count)) | |
1364 | return -EINVAL; | |
1365 | ||
1366 | /* space for set_opp_data */ | |
1367 | len = sizeof(*data); | |
1368 | ||
1369 | /* space for old_opp.supplies and new_opp.supplies */ | |
1370 | len += 2 * sizeof(struct dev_pm_opp_supply) * count; | |
1371 | ||
1372 | data = kzalloc(len, GFP_KERNEL); | |
1373 | if (!data) | |
1374 | return -ENOMEM; | |
1375 | ||
1376 | data->old_opp.supplies = (void *)(data + 1); | |
1377 | data->new_opp.supplies = data->old_opp.supplies + count; | |
1378 | ||
1379 | opp_table->set_opp_data = data; | |
1380 | ||
1381 | return 0; | |
1382 | } | |
1383 | ||
1384 | static void _free_set_opp_data(struct opp_table *opp_table) | |
1385 | { | |
1386 | kfree(opp_table->set_opp_data); | |
1387 | opp_table->set_opp_data = NULL; | |
1388 | } | |
1389 | ||
9f8ea969 | 1390 | /** |
dfbe4678 | 1391 | * dev_pm_opp_set_regulators() - Set regulator names for the device |
9f8ea969 | 1392 | * @dev: Device for which regulator name is being set. |
dfbe4678 VK |
1393 | * @names: Array of pointers to the names of the regulator. |
1394 | * @count: Number of regulators. | |
9f8ea969 VK |
1395 | * |
1396 | * In order to support OPP switching, OPP layer needs to know the name of the | |
dfbe4678 VK |
1397 | * device's regulators, as the core would be required to switch voltages as |
1398 | * well. | |
9f8ea969 VK |
1399 | * |
1400 | * This must be called before any OPPs are initialized for the device. | |
9f8ea969 | 1401 | */ |
dfbe4678 VK |
1402 | struct opp_table *dev_pm_opp_set_regulators(struct device *dev, |
1403 | const char * const names[], | |
1404 | unsigned int count) | |
9f8ea969 | 1405 | { |
2c2709dc | 1406 | struct opp_table *opp_table; |
9f8ea969 | 1407 | struct regulator *reg; |
dfbe4678 | 1408 | int ret, i; |
9f8ea969 | 1409 | |
fa30184d VK |
1410 | opp_table = dev_pm_opp_get_opp_table(dev); |
1411 | if (!opp_table) | |
1412 | return ERR_PTR(-ENOMEM); | |
9f8ea969 VK |
1413 | |
1414 | /* This should be called before OPPs are initialized */ | |
2c2709dc | 1415 | if (WARN_ON(!list_empty(&opp_table->opp_list))) { |
9f8ea969 VK |
1416 | ret = -EBUSY; |
1417 | goto err; | |
1418 | } | |
1419 | ||
dfbe4678 | 1420 | /* Already have regulators set */ |
e231f8d7 | 1421 | if (opp_table->regulators) { |
9f8ea969 VK |
1422 | ret = -EBUSY; |
1423 | goto err; | |
1424 | } | |
dfbe4678 VK |
1425 | |
1426 | opp_table->regulators = kmalloc_array(count, | |
1427 | sizeof(*opp_table->regulators), | |
1428 | GFP_KERNEL); | |
1429 | if (!opp_table->regulators) { | |
1430 | ret = -ENOMEM; | |
9f8ea969 VK |
1431 | goto err; |
1432 | } | |
1433 | ||
dfbe4678 VK |
1434 | for (i = 0; i < count; i++) { |
1435 | reg = regulator_get_optional(dev, names[i]); | |
1436 | if (IS_ERR(reg)) { | |
1437 | ret = PTR_ERR(reg); | |
1438 | if (ret != -EPROBE_DEFER) | |
1439 | dev_err(dev, "%s: no regulator (%s) found: %d\n", | |
1440 | __func__, names[i], ret); | |
1441 | goto free_regulators; | |
1442 | } | |
1443 | ||
1444 | opp_table->regulators[i] = reg; | |
1445 | } | |
1446 | ||
1447 | opp_table->regulator_count = count; | |
9f8ea969 | 1448 | |
94735585 VK |
1449 | /* Allocate block only once to pass to set_opp() routines */ |
1450 | ret = _allocate_set_opp_data(opp_table); | |
1451 | if (ret) | |
1452 | goto free_regulators; | |
1453 | ||
91291d9a | 1454 | return opp_table; |
9f8ea969 | 1455 | |
dfbe4678 VK |
1456 | free_regulators: |
1457 | while (i != 0) | |
1458 | regulator_put(opp_table->regulators[--i]); | |
1459 | ||
1460 | kfree(opp_table->regulators); | |
1461 | opp_table->regulators = NULL; | |
94735585 | 1462 | opp_table->regulator_count = 0; |
9f8ea969 | 1463 | err: |
fa30184d | 1464 | dev_pm_opp_put_opp_table(opp_table); |
9f8ea969 | 1465 | |
91291d9a | 1466 | return ERR_PTR(ret); |
9f8ea969 | 1467 | } |
dfbe4678 | 1468 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators); |
9f8ea969 VK |
1469 | |
1470 | /** | |
dfbe4678 VK |
1471 | * dev_pm_opp_put_regulators() - Releases resources blocked for regulator |
1472 | * @opp_table: OPP table returned from dev_pm_opp_set_regulators(). | |
9f8ea969 | 1473 | */ |
dfbe4678 | 1474 | void dev_pm_opp_put_regulators(struct opp_table *opp_table) |
9f8ea969 | 1475 | { |
dfbe4678 VK |
1476 | int i; |
1477 | ||
dfbe4678 VK |
1478 | if (!opp_table->regulators) { |
1479 | pr_err("%s: Doesn't have regulators set\n", __func__); | |
fa30184d | 1480 | return; |
9f8ea969 VK |
1481 | } |
1482 | ||
2c2709dc VK |
1483 | /* Make sure there are no concurrent readers while updating opp_table */ |
1484 | WARN_ON(!list_empty(&opp_table->opp_list)); | |
9f8ea969 | 1485 | |
dfbe4678 VK |
1486 | for (i = opp_table->regulator_count - 1; i >= 0; i--) |
1487 | regulator_put(opp_table->regulators[i]); | |
1488 | ||
94735585 VK |
1489 | _free_set_opp_data(opp_table); |
1490 | ||
dfbe4678 VK |
1491 | kfree(opp_table->regulators); |
1492 | opp_table->regulators = NULL; | |
1493 | opp_table->regulator_count = 0; | |
9f8ea969 | 1494 | |
fa30184d | 1495 | dev_pm_opp_put_opp_table(opp_table); |
9f8ea969 | 1496 | } |
dfbe4678 | 1497 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators); |
9f8ea969 | 1498 | |
4dab160e VK |
1499 | /** |
1500 | * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper | |
1501 | * @dev: Device for which the helper is getting registered. | |
1502 | * @set_opp: Custom set OPP helper. | |
1503 | * | |
1504 | * This is useful to support complex platforms (like platforms with multiple | |
1505 | * regulators per device), instead of the generic OPP set rate helper. | |
1506 | * | |
1507 | * This must be called before any OPPs are initialized for the device. | |
4dab160e | 1508 | */ |
fa30184d | 1509 | struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev, |
4dab160e VK |
1510 | int (*set_opp)(struct dev_pm_set_opp_data *data)) |
1511 | { | |
1512 | struct opp_table *opp_table; | |
1513 | int ret; | |
1514 | ||
1515 | if (!set_opp) | |
fa30184d | 1516 | return ERR_PTR(-EINVAL); |
4dab160e | 1517 | |
fa30184d VK |
1518 | opp_table = dev_pm_opp_get_opp_table(dev); |
1519 | if (!opp_table) | |
1520 | return ERR_PTR(-ENOMEM); | |
4dab160e VK |
1521 | |
1522 | /* This should be called before OPPs are initialized */ | |
1523 | if (WARN_ON(!list_empty(&opp_table->opp_list))) { | |
1524 | ret = -EBUSY; | |
1525 | goto err; | |
1526 | } | |
1527 | ||
1528 | /* Already have custom set_opp helper */ | |
1529 | if (WARN_ON(opp_table->set_opp)) { | |
1530 | ret = -EBUSY; | |
1531 | goto err; | |
1532 | } | |
1533 | ||
1534 | opp_table->set_opp = set_opp; | |
1535 | ||
fa30184d | 1536 | return opp_table; |
4dab160e VK |
1537 | |
1538 | err: | |
fa30184d | 1539 | dev_pm_opp_put_opp_table(opp_table); |
4dab160e | 1540 | |
fa30184d | 1541 | return ERR_PTR(ret); |
4dab160e VK |
1542 | } |
1543 | EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper); | |
1544 | ||
1545 | /** | |
1546 | * dev_pm_opp_register_put_opp_helper() - Releases resources blocked for | |
1547 | * set_opp helper | |
fa30184d | 1548 | * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper(). |
4dab160e | 1549 | * |
fa30184d | 1550 | * Release resources blocked for platform specific set_opp helper. |
4dab160e | 1551 | */ |
fa30184d | 1552 | void dev_pm_opp_register_put_opp_helper(struct opp_table *opp_table) |
4dab160e | 1553 | { |
4dab160e | 1554 | if (!opp_table->set_opp) { |
fa30184d VK |
1555 | pr_err("%s: Doesn't have custom set_opp helper set\n", |
1556 | __func__); | |
1557 | return; | |
4dab160e VK |
1558 | } |
1559 | ||
1560 | /* Make sure there are no concurrent readers while updating opp_table */ | |
1561 | WARN_ON(!list_empty(&opp_table->opp_list)); | |
1562 | ||
1563 | opp_table->set_opp = NULL; | |
1564 | ||
fa30184d | 1565 | dev_pm_opp_put_opp_table(opp_table); |
4dab160e VK |
1566 | } |
1567 | EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper); | |
1568 | ||
38393409 VK |
1569 | /** |
1570 | * dev_pm_opp_add() - Add an OPP table from a table definitions | |
1571 | * @dev: device for which we do this operation | |
1572 | * @freq: Frequency in Hz for this OPP | |
1573 | * @u_volt: Voltage in uVolts for this OPP | |
1574 | * | |
2c2709dc | 1575 | * This function adds an opp definition to the opp table and returns status. |
38393409 VK |
1576 | * The opp is made available by default and it can be controlled using |
1577 | * dev_pm_opp_enable/disable functions. | |
1578 | * | |
2c2709dc | 1579 | * Locking: The internal opp_table and opp structures are RCU protected. |
38393409 VK |
1580 | * Hence this function internally uses RCU updater strategy with mutex locks |
1581 | * to keep the integrity of the internal data structures. Callers should ensure | |
1582 | * that this function is *NOT* called under RCU protection or in contexts where | |
1583 | * mutex cannot be locked. | |
1584 | * | |
1585 | * Return: | |
984f16c8 | 1586 | * 0 On success OR |
38393409 | 1587 | * Duplicate OPPs (both freq and volt are same) and opp->available |
984f16c8 | 1588 | * -EEXIST Freq are same and volt are different OR |
38393409 | 1589 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
984f16c8 | 1590 | * -ENOMEM Memory allocation failure |
38393409 VK |
1591 | */ |
1592 | int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) | |
1593 | { | |
8cd2f6e8 VK |
1594 | struct opp_table *opp_table; |
1595 | int ret; | |
1596 | ||
b83c1899 VK |
1597 | opp_table = dev_pm_opp_get_opp_table(dev); |
1598 | if (!opp_table) | |
1599 | return -ENOMEM; | |
8cd2f6e8 VK |
1600 | |
1601 | ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); | |
8cd2f6e8 | 1602 | |
b83c1899 | 1603 | dev_pm_opp_put_opp_table(opp_table); |
8cd2f6e8 | 1604 | return ret; |
38393409 | 1605 | } |
5d4879cd | 1606 | EXPORT_SYMBOL_GPL(dev_pm_opp_add); |
e1f60b29 NM |
1607 | |
1608 | /** | |
327854c8 | 1609 | * _opp_set_availability() - helper to set the availability of an opp |
e1f60b29 NM |
1610 | * @dev: device for which we do this operation |
1611 | * @freq: OPP frequency to modify availability | |
1612 | * @availability_req: availability status requested for this opp | |
1613 | * | |
1614 | * Set the availability of an OPP with an RCU operation, opp_{enable,disable} | |
1615 | * share a common logic which is isolated here. | |
1616 | * | |
984f16c8 | 1617 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the |
e1a2d49c | 1618 | * copy operation, returns 0 if no modification was done OR modification was |
e1f60b29 NM |
1619 | * successful. |
1620 | * | |
2c2709dc | 1621 | * Locking: The internal opp_table and opp structures are RCU protected. |
e1f60b29 NM |
1622 | * Hence this function internally uses RCU updater strategy with mutex locks to |
1623 | * keep the integrity of the internal data structures. Callers should ensure | |
1624 | * that this function is *NOT* called under RCU protection or in contexts where | |
1625 | * mutex locking or synchronize_rcu() blocking calls cannot be used. | |
1626 | */ | |
327854c8 NM |
1627 | static int _opp_set_availability(struct device *dev, unsigned long freq, |
1628 | bool availability_req) | |
e1f60b29 | 1629 | { |
2c2709dc | 1630 | struct opp_table *opp_table; |
47d43ba7 | 1631 | struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV); |
e1f60b29 NM |
1632 | int r = 0; |
1633 | ||
1634 | /* keep the node allocated */ | |
47d43ba7 | 1635 | new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL); |
59d84ca8 | 1636 | if (!new_opp) |
e1f60b29 | 1637 | return -ENOMEM; |
e1f60b29 | 1638 | |
2c2709dc | 1639 | mutex_lock(&opp_table_lock); |
e1f60b29 | 1640 | |
2c2709dc VK |
1641 | /* Find the opp_table */ |
1642 | opp_table = _find_opp_table(dev); | |
1643 | if (IS_ERR(opp_table)) { | |
1644 | r = PTR_ERR(opp_table); | |
e1f60b29 NM |
1645 | dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); |
1646 | goto unlock; | |
1647 | } | |
1648 | ||
37a73ec0 VK |
1649 | mutex_lock(&opp_table->lock); |
1650 | ||
e1f60b29 | 1651 | /* Do we have the frequency? */ |
2c2709dc | 1652 | list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { |
e1f60b29 NM |
1653 | if (tmp_opp->rate == freq) { |
1654 | opp = tmp_opp; | |
1655 | break; | |
1656 | } | |
1657 | } | |
37a73ec0 VK |
1658 | |
1659 | mutex_unlock(&opp_table->lock); | |
1660 | ||
e1f60b29 NM |
1661 | if (IS_ERR(opp)) { |
1662 | r = PTR_ERR(opp); | |
1663 | goto unlock; | |
1664 | } | |
1665 | ||
1666 | /* Is update really needed? */ | |
1667 | if (opp->available == availability_req) | |
1668 | goto unlock; | |
1669 | /* copy the old data over */ | |
1670 | *new_opp = *opp; | |
1671 | ||
1672 | /* plug in new node */ | |
1673 | new_opp->available = availability_req; | |
1674 | ||
1675 | list_replace_rcu(&opp->node, &new_opp->node); | |
2c2709dc VK |
1676 | mutex_unlock(&opp_table_lock); |
1677 | call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu); | |
e1f60b29 | 1678 | |
03ca370f MH |
1679 | /* Notify the change of the OPP availability */ |
1680 | if (availability_req) | |
2c2709dc VK |
1681 | srcu_notifier_call_chain(&opp_table->srcu_head, |
1682 | OPP_EVENT_ENABLE, new_opp); | |
03ca370f | 1683 | else |
2c2709dc VK |
1684 | srcu_notifier_call_chain(&opp_table->srcu_head, |
1685 | OPP_EVENT_DISABLE, new_opp); | |
03ca370f | 1686 | |
dde8437d | 1687 | return 0; |
e1f60b29 NM |
1688 | |
1689 | unlock: | |
2c2709dc | 1690 | mutex_unlock(&opp_table_lock); |
e1f60b29 NM |
1691 | kfree(new_opp); |
1692 | return r; | |
1693 | } | |
1694 | ||
1695 | /** | |
5d4879cd | 1696 | * dev_pm_opp_enable() - Enable a specific OPP |
e1f60b29 NM |
1697 | * @dev: device for which we do this operation |
1698 | * @freq: OPP frequency to enable | |
1699 | * | |
1700 | * Enables a provided opp. If the operation is valid, this returns 0, else the | |
1701 | * corresponding error value. It is meant to be used for users an OPP available | |
5d4879cd | 1702 | * after being temporarily made unavailable with dev_pm_opp_disable. |
e1f60b29 | 1703 | * |
2c2709dc | 1704 | * Locking: The internal opp_table and opp structures are RCU protected. |
e1f60b29 NM |
1705 | * Hence this function indirectly uses RCU and mutex locks to keep the |
1706 | * integrity of the internal data structures. Callers should ensure that | |
1707 | * this function is *NOT* called under RCU protection or in contexts where | |
1708 | * mutex locking or synchronize_rcu() blocking calls cannot be used. | |
984f16c8 NM |
1709 | * |
1710 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the | |
e1a2d49c | 1711 | * copy operation, returns 0 if no modification was done OR modification was |
984f16c8 | 1712 | * successful. |
e1f60b29 | 1713 | */ |
5d4879cd | 1714 | int dev_pm_opp_enable(struct device *dev, unsigned long freq) |
e1f60b29 | 1715 | { |
327854c8 | 1716 | return _opp_set_availability(dev, freq, true); |
e1f60b29 | 1717 | } |
5d4879cd | 1718 | EXPORT_SYMBOL_GPL(dev_pm_opp_enable); |
e1f60b29 NM |
1719 | |
1720 | /** | |
5d4879cd | 1721 | * dev_pm_opp_disable() - Disable a specific OPP |
e1f60b29 NM |
1722 | * @dev: device for which we do this operation |
1723 | * @freq: OPP frequency to disable | |
1724 | * | |
1725 | * Disables a provided opp. If the operation is valid, this returns | |
1726 | * 0, else the corresponding error value. It is meant to be a temporary | |
1727 | * control by users to make this OPP not available until the circumstances are | |
5d4879cd | 1728 | * right to make it available again (with a call to dev_pm_opp_enable). |
e1f60b29 | 1729 | * |
2c2709dc | 1730 | * Locking: The internal opp_table and opp structures are RCU protected. |
e1f60b29 NM |
1731 | * Hence this function indirectly uses RCU and mutex locks to keep the |
1732 | * integrity of the internal data structures. Callers should ensure that | |
1733 | * this function is *NOT* called under RCU protection or in contexts where | |
1734 | * mutex locking or synchronize_rcu() blocking calls cannot be used. | |
984f16c8 NM |
1735 | * |
1736 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the | |
e1a2d49c | 1737 | * copy operation, returns 0 if no modification was done OR modification was |
984f16c8 | 1738 | * successful. |
e1f60b29 | 1739 | */ |
5d4879cd | 1740 | int dev_pm_opp_disable(struct device *dev, unsigned long freq) |
e1f60b29 | 1741 | { |
327854c8 | 1742 | return _opp_set_availability(dev, freq, false); |
e1f60b29 | 1743 | } |
5d4879cd | 1744 | EXPORT_SYMBOL_GPL(dev_pm_opp_disable); |
e1f60b29 | 1745 | |
03ca370f | 1746 | /** |
dc2c9ad5 VK |
1747 | * dev_pm_opp_register_notifier() - Register OPP notifier for the device |
1748 | * @dev: Device for which notifier needs to be registered | |
1749 | * @nb: Notifier block to be registered | |
984f16c8 | 1750 | * |
dc2c9ad5 VK |
1751 | * Return: 0 on success or a negative error value. |
1752 | */ | |
1753 | int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) | |
1754 | { | |
1755 | struct opp_table *opp_table; | |
1756 | int ret; | |
1757 | ||
1758 | rcu_read_lock(); | |
1759 | ||
1760 | opp_table = _find_opp_table(dev); | |
1761 | if (IS_ERR(opp_table)) { | |
1762 | ret = PTR_ERR(opp_table); | |
1763 | goto unlock; | |
1764 | } | |
1765 | ||
1766 | ret = srcu_notifier_chain_register(&opp_table->srcu_head, nb); | |
1767 | ||
1768 | unlock: | |
1769 | rcu_read_unlock(); | |
1770 | ||
1771 | return ret; | |
1772 | } | |
1773 | EXPORT_SYMBOL(dev_pm_opp_register_notifier); | |
1774 | ||
1775 | /** | |
1776 | * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device | |
1777 | * @dev: Device for which notifier needs to be unregistered | |
1778 | * @nb: Notifier block to be unregistered | |
984f16c8 | 1779 | * |
dc2c9ad5 | 1780 | * Return: 0 on success or a negative error value. |
03ca370f | 1781 | */ |
dc2c9ad5 VK |
1782 | int dev_pm_opp_unregister_notifier(struct device *dev, |
1783 | struct notifier_block *nb) | |
03ca370f | 1784 | { |
dc2c9ad5 VK |
1785 | struct opp_table *opp_table; |
1786 | int ret; | |
03ca370f | 1787 | |
dc2c9ad5 VK |
1788 | rcu_read_lock(); |
1789 | ||
1790 | opp_table = _find_opp_table(dev); | |
1791 | if (IS_ERR(opp_table)) { | |
1792 | ret = PTR_ERR(opp_table); | |
1793 | goto unlock; | |
1794 | } | |
1795 | ||
1796 | ret = srcu_notifier_chain_unregister(&opp_table->srcu_head, nb); | |
03ca370f | 1797 | |
dc2c9ad5 VK |
1798 | unlock: |
1799 | rcu_read_unlock(); | |
1800 | ||
1801 | return ret; | |
03ca370f | 1802 | } |
dc2c9ad5 | 1803 | EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); |
b496dfbc | 1804 | |
411466c5 SH |
1805 | /* |
1806 | * Free OPPs either created using static entries present in DT or even the | |
1807 | * dynamically added entries based on remove_all param. | |
b496dfbc | 1808 | */ |
8cd2f6e8 VK |
1809 | void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev, |
1810 | bool remove_all) | |
737002b5 | 1811 | { |
737002b5 VK |
1812 | struct dev_pm_opp *opp, *tmp; |
1813 | ||
9274c892 VK |
1814 | /* Find if opp_table manages a single device */ |
1815 | if (list_is_singular(&opp_table->dev_list)) { | |
1816 | /* Free static OPPs */ | |
1817 | list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) { | |
1818 | if (remove_all || !opp->dynamic) | |
7034764a | 1819 | dev_pm_opp_put(opp); |
9274c892 VK |
1820 | } |
1821 | } else { | |
1822 | _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table); | |
1823 | } | |
1824 | } | |
1825 | ||
1826 | void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all) | |
1827 | { | |
1828 | struct opp_table *opp_table; | |
1829 | ||
2c2709dc VK |
1830 | /* Hold our table modification lock here */ |
1831 | mutex_lock(&opp_table_lock); | |
06441658 | 1832 | |
2c2709dc VK |
1833 | /* Check for existing table for 'dev' */ |
1834 | opp_table = _find_opp_table(dev); | |
1835 | if (IS_ERR(opp_table)) { | |
1836 | int error = PTR_ERR(opp_table); | |
737002b5 VK |
1837 | |
1838 | if (error != -ENODEV) | |
2c2709dc | 1839 | WARN(1, "%s: opp_table: %d\n", |
737002b5 VK |
1840 | IS_ERR_OR_NULL(dev) ? |
1841 | "Invalid device" : dev_name(dev), | |
1842 | error); | |
06441658 | 1843 | goto unlock; |
737002b5 VK |
1844 | } |
1845 | ||
9274c892 | 1846 | _dev_pm_opp_remove_table(opp_table, dev, remove_all); |
737002b5 | 1847 | |
06441658 | 1848 | unlock: |
2c2709dc | 1849 | mutex_unlock(&opp_table_lock); |
737002b5 | 1850 | } |
129eec55 VK |
1851 | |
1852 | /** | |
411466c5 | 1853 | * dev_pm_opp_remove_table() - Free all OPPs associated with the device |
2c2709dc | 1854 | * @dev: device pointer used to lookup OPP table. |
129eec55 | 1855 | * |
411466c5 SH |
1856 | * Free both OPPs created using static entries present in DT and the |
1857 | * dynamically added entries. | |
984f16c8 | 1858 | * |
2c2709dc | 1859 | * Locking: The internal opp_table and opp structures are RCU protected. |
984f16c8 NM |
1860 | * Hence this function indirectly uses RCU updater strategy with mutex locks |
1861 | * to keep the integrity of the internal data structures. Callers should ensure | |
1862 | * that this function is *NOT* called under RCU protection or in contexts where | |
1863 | * mutex cannot be locked. | |
129eec55 | 1864 | */ |
411466c5 | 1865 | void dev_pm_opp_remove_table(struct device *dev) |
129eec55 | 1866 | { |
9274c892 | 1867 | _dev_pm_opp_find_and_remove_table(dev, true); |
8d4d4e98 | 1868 | } |
411466c5 | 1869 | EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table); |