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