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