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 | ||
dfbe4678 | 199 | /* Regulator may not be required for the device */ |
90e3577b | 200 | if (!opp_table->regulators) |
cdd3e614 | 201 | goto put_opp_table; |
dfbe4678 | 202 | |
90e3577b VK |
203 | count = opp_table->regulator_count; |
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); |
09f662f9 | 321 | return count; |
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 | ||
285881b5 VK |
536 | static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk, |
537 | unsigned long freq) | |
94735585 VK |
538 | { |
539 | int ret; | |
540 | ||
541 | ret = clk_set_rate(clk, freq); | |
542 | if (ret) { | |
543 | dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, | |
544 | ret); | |
545 | } | |
546 | ||
547 | return ret; | |
548 | } | |
549 | ||
c74b32fa VK |
550 | static int _generic_set_opp_regulator(const struct opp_table *opp_table, |
551 | struct device *dev, | |
552 | unsigned long old_freq, | |
553 | unsigned long freq, | |
554 | struct dev_pm_opp_supply *old_supply, | |
555 | struct dev_pm_opp_supply *new_supply) | |
94735585 | 556 | { |
c74b32fa | 557 | struct regulator *reg = opp_table->regulators[0]; |
94735585 VK |
558 | int ret; |
559 | ||
560 | /* This function only supports single regulator per device */ | |
c74b32fa | 561 | if (WARN_ON(opp_table->regulator_count > 1)) { |
94735585 VK |
562 | dev_err(dev, "multiple regulators are not supported\n"); |
563 | return -EINVAL; | |
564 | } | |
565 | ||
566 | /* Scaling up? Scale voltage before frequency */ | |
c5c2a97b | 567 | if (freq >= old_freq) { |
94735585 VK |
568 | ret = _set_opp_voltage(dev, reg, new_supply); |
569 | if (ret) | |
570 | goto restore_voltage; | |
571 | } | |
572 | ||
573 | /* Change frequency */ | |
285881b5 | 574 | ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq); |
94735585 VK |
575 | if (ret) |
576 | goto restore_voltage; | |
577 | ||
578 | /* Scaling down? Scale voltage after frequency */ | |
579 | if (freq < old_freq) { | |
580 | ret = _set_opp_voltage(dev, reg, new_supply); | |
581 | if (ret) | |
582 | goto restore_freq; | |
583 | } | |
584 | ||
585 | return 0; | |
586 | ||
587 | restore_freq: | |
285881b5 | 588 | if (_generic_set_opp_clk_only(dev, opp_table->clk, old_freq)) |
94735585 VK |
589 | dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", |
590 | __func__, old_freq); | |
591 | restore_voltage: | |
592 | /* This shouldn't harm even if the voltages weren't updated earlier */ | |
c74b32fa | 593 | if (old_supply) |
94735585 VK |
594 | _set_opp_voltage(dev, reg, old_supply); |
595 | ||
596 | return ret; | |
597 | } | |
598 | ||
7e535993 VK |
599 | static int _set_opp_custom(const struct opp_table *opp_table, |
600 | struct device *dev, unsigned long old_freq, | |
601 | unsigned long freq, | |
602 | struct dev_pm_opp_supply *old_supply, | |
603 | struct dev_pm_opp_supply *new_supply) | |
604 | { | |
605 | struct dev_pm_set_opp_data *data; | |
606 | int size; | |
607 | ||
608 | data = opp_table->set_opp_data; | |
609 | data->regulators = opp_table->regulators; | |
610 | data->regulator_count = opp_table->regulator_count; | |
611 | data->clk = opp_table->clk; | |
612 | data->dev = dev; | |
613 | ||
614 | data->old_opp.rate = old_freq; | |
615 | size = sizeof(*old_supply) * opp_table->regulator_count; | |
616 | if (IS_ERR(old_supply)) | |
617 | memset(data->old_opp.supplies, 0, size); | |
618 | else | |
619 | memcpy(data->old_opp.supplies, old_supply, size); | |
620 | ||
621 | data->new_opp.rate = freq; | |
622 | memcpy(data->new_opp.supplies, new_supply, size); | |
623 | ||
624 | return opp_table->set_opp(data); | |
625 | } | |
626 | ||
ca1b5d77 VK |
627 | /* This is only called for PM domain for now */ |
628 | static int _set_required_opps(struct device *dev, | |
629 | struct opp_table *opp_table, | |
630 | struct dev_pm_opp *opp) | |
631 | { | |
632 | struct opp_table **required_opp_tables = opp_table->required_opp_tables; | |
633 | struct device **genpd_virt_devs = opp_table->genpd_virt_devs; | |
634 | unsigned int pstate; | |
635 | int i, ret = 0; | |
636 | ||
637 | if (!required_opp_tables) | |
638 | return 0; | |
639 | ||
640 | /* Single genpd case */ | |
641 | if (!genpd_virt_devs) { | |
642 | pstate = opp->required_opps[0]->pstate; | |
643 | ret = dev_pm_genpd_set_performance_state(dev, pstate); | |
644 | if (ret) { | |
645 | dev_err(dev, "Failed to set performance state of %s: %d (%d)\n", | |
646 | dev_name(dev), pstate, ret); | |
647 | } | |
648 | return ret; | |
649 | } | |
650 | ||
651 | /* Multiple genpd case */ | |
652 | ||
653 | /* | |
654 | * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev | |
655 | * after it is freed from another thread. | |
656 | */ | |
657 | mutex_lock(&opp_table->genpd_virt_dev_lock); | |
658 | ||
659 | for (i = 0; i < opp_table->required_opp_count; i++) { | |
660 | pstate = opp->required_opps[i]->pstate; | |
661 | ||
662 | if (!genpd_virt_devs[i]) | |
663 | continue; | |
664 | ||
665 | ret = dev_pm_genpd_set_performance_state(genpd_virt_devs[i], pstate); | |
666 | if (ret) { | |
667 | dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n", | |
668 | dev_name(genpd_virt_devs[i]), pstate, ret); | |
669 | break; | |
670 | } | |
671 | } | |
672 | mutex_unlock(&opp_table->genpd_virt_dev_lock); | |
673 | ||
674 | return ret; | |
675 | } | |
676 | ||
6a0712f6 VK |
677 | /** |
678 | * dev_pm_opp_set_rate() - Configure new OPP based on frequency | |
679 | * @dev: device for which we do this operation | |
680 | * @target_freq: frequency to achieve | |
681 | * | |
682 | * This configures the power-supplies and clock source to the levels specified | |
683 | * by the OPP corresponding to the target_freq. | |
6a0712f6 VK |
684 | */ |
685 | int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) | |
686 | { | |
2c2709dc | 687 | struct opp_table *opp_table; |
94735585 | 688 | unsigned long freq, old_freq; |
6a0712f6 | 689 | struct dev_pm_opp *old_opp, *opp; |
6a0712f6 | 690 | struct clk *clk; |
7e535993 | 691 | int ret; |
6a0712f6 VK |
692 | |
693 | if (unlikely(!target_freq)) { | |
694 | dev_err(dev, "%s: Invalid target frequency %lu\n", __func__, | |
695 | target_freq); | |
696 | return -EINVAL; | |
697 | } | |
698 | ||
052c6f19 VK |
699 | opp_table = _find_opp_table(dev); |
700 | if (IS_ERR(opp_table)) { | |
701 | dev_err(dev, "%s: device opp doesn't exist\n", __func__); | |
702 | return PTR_ERR(opp_table); | |
703 | } | |
704 | ||
705 | clk = opp_table->clk; | |
706 | if (IS_ERR(clk)) { | |
707 | dev_err(dev, "%s: No clock available for the device\n", | |
708 | __func__); | |
709 | ret = PTR_ERR(clk); | |
710 | goto put_opp_table; | |
711 | } | |
6a0712f6 VK |
712 | |
713 | freq = clk_round_rate(clk, target_freq); | |
714 | if ((long)freq <= 0) | |
715 | freq = target_freq; | |
716 | ||
717 | old_freq = clk_get_rate(clk); | |
718 | ||
719 | /* Return early if nothing to do */ | |
720 | if (old_freq == freq) { | |
721 | dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n", | |
722 | __func__, freq); | |
052c6f19 VK |
723 | ret = 0; |
724 | goto put_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); | |
052c6f19 | 738 | goto put_old_opp; |
6a0712f6 VK |
739 | } |
740 | ||
94735585 VK |
741 | dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__, |
742 | old_freq, freq); | |
dfbe4678 | 743 | |
ca1b5d77 VK |
744 | /* Scaling up? Configure required OPPs before frequency */ |
745 | if (freq > old_freq) { | |
746 | ret = _set_required_opps(dev, opp_table, opp); | |
747 | if (ret) | |
748 | goto put_opp; | |
749 | } | |
750 | ||
7e535993 VK |
751 | if (opp_table->set_opp) { |
752 | ret = _set_opp_custom(opp_table, dev, old_freq, freq, | |
753 | IS_ERR(old_opp) ? NULL : old_opp->supplies, | |
754 | opp->supplies); | |
755 | } else if (opp_table->regulators) { | |
c74b32fa VK |
756 | ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq, |
757 | IS_ERR(old_opp) ? NULL : old_opp->supplies, | |
758 | opp->supplies); | |
759 | } else { | |
7e535993 | 760 | /* Only frequency scaling */ |
285881b5 | 761 | ret = _generic_set_opp_clk_only(dev, clk, freq); |
ca1b5d77 | 762 | } |
c74b32fa | 763 | |
ca1b5d77 VK |
764 | /* Scaling down? Configure required OPPs after frequency */ |
765 | if (!ret && freq < old_freq) { | |
766 | ret = _set_required_opps(dev, opp_table, opp); | |
767 | if (ret) | |
768 | dev_err(dev, "Failed to set required opps: %d\n", ret); | |
dfbe4678 VK |
769 | } |
770 | ||
ca1b5d77 | 771 | put_opp: |
8a31d9d9 | 772 | dev_pm_opp_put(opp); |
052c6f19 | 773 | put_old_opp: |
8a31d9d9 VK |
774 | if (!IS_ERR(old_opp)) |
775 | dev_pm_opp_put(old_opp); | |
052c6f19 | 776 | put_opp_table: |
5b650b38 | 777 | dev_pm_opp_put_opp_table(opp_table); |
052c6f19 | 778 | return ret; |
6a0712f6 VK |
779 | } |
780 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); | |
781 | ||
2c2709dc | 782 | /* OPP-dev Helpers */ |
2c2709dc VK |
783 | static void _remove_opp_dev(struct opp_device *opp_dev, |
784 | struct opp_table *opp_table) | |
06441658 | 785 | { |
2c2709dc VK |
786 | opp_debug_unregister(opp_dev, opp_table); |
787 | list_del(&opp_dev->node); | |
052c6f19 | 788 | kfree(opp_dev); |
06441658 VK |
789 | } |
790 | ||
283d55e6 VK |
791 | static struct opp_device *_add_opp_dev_unlocked(const struct device *dev, |
792 | struct opp_table *opp_table) | |
06441658 | 793 | { |
2c2709dc | 794 | struct opp_device *opp_dev; |
deaa5146 | 795 | int ret; |
06441658 | 796 | |
2c2709dc VK |
797 | opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL); |
798 | if (!opp_dev) | |
06441658 VK |
799 | return NULL; |
800 | ||
2c2709dc VK |
801 | /* Initialize opp-dev */ |
802 | opp_dev->dev = dev; | |
3d255699 | 803 | |
052c6f19 | 804 | list_add(&opp_dev->node, &opp_table->dev_list); |
06441658 | 805 | |
2c2709dc VK |
806 | /* Create debugfs entries for the opp_table */ |
807 | ret = opp_debug_register(opp_dev, opp_table); | |
deaa5146 VK |
808 | if (ret) |
809 | dev_err(dev, "%s: Failed to register opp debugfs (%d)\n", | |
810 | __func__, ret); | |
283d55e6 VK |
811 | |
812 | return opp_dev; | |
813 | } | |
814 | ||
815 | struct opp_device *_add_opp_dev(const struct device *dev, | |
816 | struct opp_table *opp_table) | |
817 | { | |
818 | struct opp_device *opp_dev; | |
819 | ||
820 | mutex_lock(&opp_table->lock); | |
821 | opp_dev = _add_opp_dev_unlocked(dev, opp_table); | |
3d255699 | 822 | mutex_unlock(&opp_table->lock); |
deaa5146 | 823 | |
2c2709dc | 824 | return opp_dev; |
06441658 VK |
825 | } |
826 | ||
eb7c8743 | 827 | static struct opp_table *_allocate_opp_table(struct device *dev, int index) |
07cce74a | 828 | { |
2c2709dc VK |
829 | struct opp_table *opp_table; |
830 | struct opp_device *opp_dev; | |
d54974c2 | 831 | int ret; |
07cce74a VK |
832 | |
833 | /* | |
2c2709dc | 834 | * Allocate a new OPP table. In the infrequent case where a new |
07cce74a VK |
835 | * device is needed to be added, we pay this penalty. |
836 | */ | |
2c2709dc VK |
837 | opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL); |
838 | if (!opp_table) | |
07cce74a VK |
839 | return NULL; |
840 | ||
3d255699 | 841 | mutex_init(&opp_table->lock); |
4f018bc0 | 842 | mutex_init(&opp_table->genpd_virt_dev_lock); |
2c2709dc | 843 | INIT_LIST_HEAD(&opp_table->dev_list); |
06441658 | 844 | |
46f48aca VK |
845 | /* Mark regulator count uninitialized */ |
846 | opp_table->regulator_count = -1; | |
847 | ||
2c2709dc VK |
848 | opp_dev = _add_opp_dev(dev, opp_table); |
849 | if (!opp_dev) { | |
850 | kfree(opp_table); | |
06441658 VK |
851 | return NULL; |
852 | } | |
853 | ||
eb7c8743 | 854 | _of_init_opp_table(opp_table, dev, index); |
50f8cfbd | 855 | |
d54974c2 | 856 | /* Find clk for the device */ |
2c2709dc VK |
857 | opp_table->clk = clk_get(dev, NULL); |
858 | if (IS_ERR(opp_table->clk)) { | |
859 | ret = PTR_ERR(opp_table->clk); | |
d54974c2 VK |
860 | if (ret != -EPROBE_DEFER) |
861 | dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, | |
862 | ret); | |
863 | } | |
864 | ||
052c6f19 | 865 | BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); |
2c2709dc | 866 | INIT_LIST_HEAD(&opp_table->opp_list); |
f067a982 | 867 | kref_init(&opp_table->kref); |
07cce74a | 868 | |
2c2709dc | 869 | /* Secure the device table modification */ |
052c6f19 | 870 | list_add(&opp_table->node, &opp_tables); |
2c2709dc | 871 | return opp_table; |
07cce74a VK |
872 | } |
873 | ||
f067a982 | 874 | void _get_opp_table_kref(struct opp_table *opp_table) |
b6160e26 | 875 | { |
f067a982 VK |
876 | kref_get(&opp_table->kref); |
877 | } | |
878 | ||
eb7c8743 | 879 | static struct opp_table *_opp_get_opp_table(struct device *dev, int index) |
f067a982 VK |
880 | { |
881 | struct opp_table *opp_table; | |
882 | ||
883 | /* Hold our table modification lock here */ | |
884 | mutex_lock(&opp_table_lock); | |
885 | ||
5b650b38 VK |
886 | opp_table = _find_opp_table_unlocked(dev); |
887 | if (!IS_ERR(opp_table)) | |
f067a982 | 888 | goto unlock; |
f067a982 | 889 | |
283d55e6 VK |
890 | opp_table = _managed_opp(dev, index); |
891 | if (opp_table) { | |
892 | if (!_add_opp_dev_unlocked(dev, opp_table)) { | |
893 | dev_pm_opp_put_opp_table(opp_table); | |
894 | opp_table = NULL; | |
895 | } | |
896 | goto unlock; | |
897 | } | |
898 | ||
eb7c8743 | 899 | opp_table = _allocate_opp_table(dev, index); |
f067a982 VK |
900 | |
901 | unlock: | |
902 | mutex_unlock(&opp_table_lock); | |
903 | ||
904 | return opp_table; | |
905 | } | |
eb7c8743 VK |
906 | |
907 | struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) | |
908 | { | |
909 | return _opp_get_opp_table(dev, 0); | |
910 | } | |
f067a982 VK |
911 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); |
912 | ||
eb7c8743 VK |
913 | struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev, |
914 | int index) | |
915 | { | |
916 | return _opp_get_opp_table(dev, index); | |
917 | } | |
918 | ||
b83c1899 | 919 | static void _opp_table_kref_release(struct kref *kref) |
f067a982 VK |
920 | { |
921 | struct opp_table *opp_table = container_of(kref, struct opp_table, kref); | |
cdd6ed90 | 922 | struct opp_device *opp_dev, *temp; |
b6160e26 | 923 | |
5d6d106f VK |
924 | _of_clear_opp_table(opp_table); |
925 | ||
b6160e26 VK |
926 | /* Release clk */ |
927 | if (!IS_ERR(opp_table->clk)) | |
928 | clk_put(opp_table->clk); | |
929 | ||
cdd6ed90 | 930 | WARN_ON(!list_empty(&opp_table->opp_list)); |
b6160e26 | 931 | |
cdd6ed90 VK |
932 | list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) { |
933 | /* | |
934 | * The OPP table is getting removed, drop the performance state | |
935 | * constraints. | |
936 | */ | |
937 | if (opp_table->genpd_performance_state) | |
938 | dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0); | |
b6160e26 | 939 | |
cdd6ed90 VK |
940 | _remove_opp_dev(opp_dev, opp_table); |
941 | } | |
b6160e26 | 942 | |
4f018bc0 | 943 | mutex_destroy(&opp_table->genpd_virt_dev_lock); |
37a73ec0 | 944 | mutex_destroy(&opp_table->lock); |
052c6f19 VK |
945 | list_del(&opp_table->node); |
946 | kfree(opp_table); | |
b6160e26 | 947 | |
f067a982 VK |
948 | mutex_unlock(&opp_table_lock); |
949 | } | |
950 | ||
d0e8ae6c VK |
951 | void _opp_remove_all_static(struct opp_table *opp_table) |
952 | { | |
953 | struct dev_pm_opp *opp, *tmp; | |
954 | ||
955 | list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) { | |
956 | if (!opp->dynamic) | |
957 | dev_pm_opp_put(opp); | |
958 | } | |
959 | ||
960 | opp_table->parsed_static_opps = false; | |
961 | } | |
962 | ||
963 | static void _opp_table_list_kref_release(struct kref *kref) | |
964 | { | |
965 | struct opp_table *opp_table = container_of(kref, struct opp_table, | |
966 | list_kref); | |
967 | ||
968 | _opp_remove_all_static(opp_table); | |
969 | mutex_unlock(&opp_table_lock); | |
970 | } | |
971 | ||
972 | void _put_opp_list_kref(struct opp_table *opp_table) | |
973 | { | |
974 | kref_put_mutex(&opp_table->list_kref, _opp_table_list_kref_release, | |
975 | &opp_table_lock); | |
976 | } | |
977 | ||
f067a982 VK |
978 | void dev_pm_opp_put_opp_table(struct opp_table *opp_table) |
979 | { | |
980 | kref_put_mutex(&opp_table->kref, _opp_table_kref_release, | |
981 | &opp_table_lock); | |
982 | } | |
983 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); | |
984 | ||
8cd2f6e8 | 985 | void _opp_free(struct dev_pm_opp *opp) |
969fceb3 VK |
986 | { |
987 | kfree(opp); | |
969fceb3 VK |
988 | } |
989 | ||
7034764a | 990 | static void _opp_kref_release(struct kref *kref) |
129eec55 | 991 | { |
7034764a VK |
992 | struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); |
993 | struct opp_table *opp_table = opp->opp_table; | |
37a73ec0 | 994 | |
129eec55 VK |
995 | /* |
996 | * Notify the changes in the availability of the operable | |
997 | * frequency/voltage list. | |
998 | */ | |
052c6f19 | 999 | blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp); |
da544b61 | 1000 | _of_opp_free_required_opps(opp_table, opp); |
deaa5146 | 1001 | opp_debug_remove_one(opp); |
052c6f19 VK |
1002 | list_del(&opp->node); |
1003 | kfree(opp); | |
129eec55 | 1004 | |
37a73ec0 | 1005 | mutex_unlock(&opp_table->lock); |
129eec55 VK |
1006 | } |
1007 | ||
a88bd2a5 | 1008 | void dev_pm_opp_get(struct dev_pm_opp *opp) |
8a31d9d9 VK |
1009 | { |
1010 | kref_get(&opp->kref); | |
1011 | } | |
1012 | ||
7034764a VK |
1013 | void dev_pm_opp_put(struct dev_pm_opp *opp) |
1014 | { | |
1015 | kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock); | |
1016 | } | |
1017 | EXPORT_SYMBOL_GPL(dev_pm_opp_put); | |
1018 | ||
129eec55 | 1019 | /** |
2c2709dc | 1020 | * dev_pm_opp_remove() - Remove an OPP from OPP table |
129eec55 VK |
1021 | * @dev: device for which we do this operation |
1022 | * @freq: OPP to remove with matching 'freq' | |
1023 | * | |
2c2709dc | 1024 | * This function removes an opp from the opp table. |
129eec55 VK |
1025 | */ |
1026 | void dev_pm_opp_remove(struct device *dev, unsigned long freq) | |
1027 | { | |
1028 | struct dev_pm_opp *opp; | |
2c2709dc | 1029 | struct opp_table *opp_table; |
129eec55 VK |
1030 | bool found = false; |
1031 | ||
2c2709dc VK |
1032 | opp_table = _find_opp_table(dev); |
1033 | if (IS_ERR(opp_table)) | |
5b650b38 | 1034 | return; |
129eec55 | 1035 | |
37a73ec0 VK |
1036 | mutex_lock(&opp_table->lock); |
1037 | ||
2c2709dc | 1038 | list_for_each_entry(opp, &opp_table->opp_list, node) { |
129eec55 VK |
1039 | if (opp->rate == freq) { |
1040 | found = true; | |
1041 | break; | |
1042 | } | |
1043 | } | |
1044 | ||
37a73ec0 VK |
1045 | mutex_unlock(&opp_table->lock); |
1046 | ||
5b650b38 VK |
1047 | if (found) { |
1048 | dev_pm_opp_put(opp); | |
0ad8c623 VK |
1049 | |
1050 | /* Drop the reference taken by dev_pm_opp_add() */ | |
1051 | dev_pm_opp_put_opp_table(opp_table); | |
5b650b38 | 1052 | } else { |
129eec55 VK |
1053 | dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", |
1054 | __func__, freq); | |
129eec55 VK |
1055 | } |
1056 | ||
0ad8c623 | 1057 | /* Drop the reference taken by _find_opp_table() */ |
5b650b38 | 1058 | dev_pm_opp_put_opp_table(opp_table); |
129eec55 VK |
1059 | } |
1060 | EXPORT_SYMBOL_GPL(dev_pm_opp_remove); | |
1061 | ||
8cd2f6e8 | 1062 | struct dev_pm_opp *_opp_allocate(struct opp_table *table) |
e1f60b29 | 1063 | { |
23dacf6d | 1064 | struct dev_pm_opp *opp; |
dfbe4678 | 1065 | int count, supply_size; |
e1f60b29 | 1066 | |
dfbe4678 | 1067 | /* Allocate space for at least one supply */ |
46f48aca | 1068 | count = table->regulator_count > 0 ? table->regulator_count : 1; |
dfbe4678 | 1069 | supply_size = sizeof(*opp->supplies) * count; |
e1f60b29 | 1070 | |
dfbe4678 VK |
1071 | /* allocate new OPP node and supplies structures */ |
1072 | opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL); | |
8cd2f6e8 | 1073 | if (!opp) |
23dacf6d | 1074 | return NULL; |
23dacf6d | 1075 | |
dfbe4678 VK |
1076 | /* Put the supplies at the end of the OPP structure as an empty array */ |
1077 | opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); | |
1078 | INIT_LIST_HEAD(&opp->node); | |
1079 | ||
23dacf6d VK |
1080 | return opp; |
1081 | } | |
1082 | ||
7d34d56e | 1083 | static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, |
2c2709dc | 1084 | struct opp_table *opp_table) |
7d34d56e | 1085 | { |
dfbe4678 VK |
1086 | struct regulator *reg; |
1087 | int i; | |
1088 | ||
90e3577b VK |
1089 | if (!opp_table->regulators) |
1090 | return true; | |
1091 | ||
dfbe4678 VK |
1092 | for (i = 0; i < opp_table->regulator_count; i++) { |
1093 | reg = opp_table->regulators[i]; | |
1094 | ||
1095 | if (!regulator_is_supported_voltage(reg, | |
1096 | opp->supplies[i].u_volt_min, | |
1097 | opp->supplies[i].u_volt_max)) { | |
1098 | pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n", | |
1099 | __func__, opp->supplies[i].u_volt_min, | |
1100 | opp->supplies[i].u_volt_max); | |
1101 | return false; | |
1102 | } | |
7d34d56e VK |
1103 | } |
1104 | ||
1105 | return true; | |
1106 | } | |
1107 | ||
a1e8c136 VK |
1108 | static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp, |
1109 | struct opp_table *opp_table, | |
1110 | struct list_head **head) | |
23dacf6d VK |
1111 | { |
1112 | struct dev_pm_opp *opp; | |
23dacf6d VK |
1113 | |
1114 | /* | |
1115 | * Insert new OPP in order of increasing frequency and discard if | |
1116 | * already present. | |
1117 | * | |
2c2709dc | 1118 | * Need to use &opp_table->opp_list in the condition part of the 'for' |
23dacf6d VK |
1119 | * loop, don't replace it with head otherwise it will become an infinite |
1120 | * loop. | |
1121 | */ | |
052c6f19 | 1122 | list_for_each_entry(opp, &opp_table->opp_list, node) { |
23dacf6d | 1123 | if (new_opp->rate > opp->rate) { |
a1e8c136 | 1124 | *head = &opp->node; |
23dacf6d VK |
1125 | continue; |
1126 | } | |
1127 | ||
1128 | if (new_opp->rate < opp->rate) | |
a1e8c136 | 1129 | return 0; |
23dacf6d VK |
1130 | |
1131 | /* Duplicate OPPs */ | |
06441658 | 1132 | dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", |
dfbe4678 VK |
1133 | __func__, opp->rate, opp->supplies[0].u_volt, |
1134 | opp->available, new_opp->rate, | |
1135 | new_opp->supplies[0].u_volt, new_opp->available); | |
23dacf6d | 1136 | |
dfbe4678 | 1137 | /* Should we compare voltages for all regulators here ? */ |
a1e8c136 VK |
1138 | return opp->available && |
1139 | new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; | |
1140 | } | |
1141 | ||
1142 | return 0; | |
1143 | } | |
1144 | ||
1145 | /* | |
1146 | * Returns: | |
1147 | * 0: On success. And appropriate error message for duplicate OPPs. | |
1148 | * -EBUSY: For OPP with same freq/volt and is available. The callers of | |
1149 | * _opp_add() must return 0 if they receive -EBUSY from it. This is to make | |
1150 | * sure we don't print error messages unnecessarily if different parts of | |
1151 | * kernel try to initialize the OPP table. | |
1152 | * -EEXIST: For OPP with same freq but different volt or is unavailable. This | |
1153 | * should be considered an error by the callers of _opp_add(). | |
1154 | */ | |
1155 | int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, | |
1156 | struct opp_table *opp_table, bool rate_not_available) | |
1157 | { | |
1158 | struct list_head *head; | |
1159 | int ret; | |
1160 | ||
1161 | mutex_lock(&opp_table->lock); | |
1162 | head = &opp_table->opp_list; | |
37a73ec0 | 1163 | |
a1e8c136 VK |
1164 | if (likely(!rate_not_available)) { |
1165 | ret = _opp_is_duplicate(dev, new_opp, opp_table, &head); | |
1166 | if (ret) { | |
1167 | mutex_unlock(&opp_table->lock); | |
1168 | return ret; | |
1169 | } | |
23dacf6d VK |
1170 | } |
1171 | ||
052c6f19 | 1172 | list_add(&new_opp->node, head); |
37a73ec0 VK |
1173 | mutex_unlock(&opp_table->lock); |
1174 | ||
1175 | new_opp->opp_table = opp_table; | |
7034764a | 1176 | kref_init(&new_opp->kref); |
23dacf6d | 1177 | |
2c2709dc | 1178 | ret = opp_debug_create_one(new_opp, opp_table); |
deaa5146 VK |
1179 | if (ret) |
1180 | dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n", | |
1181 | __func__, ret); | |
1182 | ||
2c2709dc | 1183 | if (!_opp_supported_by_regulators(new_opp, opp_table)) { |
7d34d56e VK |
1184 | new_opp->available = false; |
1185 | dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n", | |
1186 | __func__, new_opp->rate); | |
1187 | } | |
1188 | ||
23dacf6d VK |
1189 | return 0; |
1190 | } | |
1191 | ||
984f16c8 | 1192 | /** |
b64b9c3f | 1193 | * _opp_add_v1() - Allocate a OPP based on v1 bindings. |
8cd2f6e8 | 1194 | * @opp_table: OPP table |
984f16c8 NM |
1195 | * @dev: device for which we do this operation |
1196 | * @freq: Frequency in Hz for this OPP | |
1197 | * @u_volt: Voltage in uVolts for this OPP | |
1198 | * @dynamic: Dynamically added OPPs. | |
1199 | * | |
2c2709dc | 1200 | * This function adds an opp definition to the opp table and returns status. |
984f16c8 NM |
1201 | * The opp is made available by default and it can be controlled using |
1202 | * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. | |
1203 | * | |
8f8d37b2 VK |
1204 | * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table |
1205 | * and freed by dev_pm_opp_of_remove_table. | |
984f16c8 | 1206 | * |
984f16c8 NM |
1207 | * Return: |
1208 | * 0 On success OR | |
1209 | * Duplicate OPPs (both freq and volt are same) and opp->available | |
1210 | * -EEXIST Freq are same and volt are different OR | |
1211 | * Duplicate OPPs (both freq and volt are same) and !opp->available | |
1212 | * -ENOMEM Memory allocation failure | |
1213 | */ | |
8cd2f6e8 VK |
1214 | int _opp_add_v1(struct opp_table *opp_table, struct device *dev, |
1215 | unsigned long freq, long u_volt, bool dynamic) | |
e1f60b29 | 1216 | { |
23dacf6d | 1217 | struct dev_pm_opp *new_opp; |
50f8cfbd | 1218 | unsigned long tol; |
6ce4184d | 1219 | int ret; |
e1f60b29 | 1220 | |
8cd2f6e8 VK |
1221 | new_opp = _opp_allocate(opp_table); |
1222 | if (!new_opp) | |
1223 | return -ENOMEM; | |
23dacf6d | 1224 | |
a7470db6 | 1225 | /* populate the opp table */ |
a7470db6 | 1226 | new_opp->rate = freq; |
2c2709dc | 1227 | tol = u_volt * opp_table->voltage_tolerance_v1 / 100; |
dfbe4678 VK |
1228 | new_opp->supplies[0].u_volt = u_volt; |
1229 | new_opp->supplies[0].u_volt_min = u_volt - tol; | |
1230 | new_opp->supplies[0].u_volt_max = u_volt + tol; | |
a7470db6 | 1231 | new_opp->available = true; |
23dacf6d | 1232 | new_opp->dynamic = dynamic; |
a7470db6 | 1233 | |
a1e8c136 | 1234 | ret = _opp_add(dev, new_opp, opp_table, false); |
7f8538eb VK |
1235 | if (ret) { |
1236 | /* Don't return error for duplicate OPPs */ | |
1237 | if (ret == -EBUSY) | |
1238 | ret = 0; | |
6ce4184d | 1239 | goto free_opp; |
7f8538eb | 1240 | } |
64ce8545 | 1241 | |
03ca370f MH |
1242 | /* |
1243 | * Notify the changes in the availability of the operable | |
1244 | * frequency/voltage list. | |
1245 | */ | |
052c6f19 | 1246 | blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); |
e1f60b29 | 1247 | return 0; |
6ce4184d VK |
1248 | |
1249 | free_opp: | |
8cd2f6e8 VK |
1250 | _opp_free(new_opp); |
1251 | ||
6ce4184d | 1252 | return ret; |
e1f60b29 | 1253 | } |
38393409 | 1254 | |
7de36b0a VK |
1255 | /** |
1256 | * dev_pm_opp_set_supported_hw() - Set supported platforms | |
1257 | * @dev: Device for which supported-hw has to be set. | |
1258 | * @versions: Array of hierarchy of versions to match. | |
1259 | * @count: Number of elements in the array. | |
1260 | * | |
1261 | * This is required only for the V2 bindings, and it enables a platform to | |
1262 | * specify the hierarchy of versions it supports. OPP layer will then enable | |
1263 | * OPPs, which are available for those versions, based on its 'opp-supported-hw' | |
1264 | * property. | |
7de36b0a | 1265 | */ |
fa30184d VK |
1266 | struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev, |
1267 | const u32 *versions, unsigned int count) | |
7de36b0a | 1268 | { |
2c2709dc | 1269 | struct opp_table *opp_table; |
7de36b0a | 1270 | |
fa30184d VK |
1271 | opp_table = dev_pm_opp_get_opp_table(dev); |
1272 | if (!opp_table) | |
1273 | return ERR_PTR(-ENOMEM); | |
7de36b0a | 1274 | |
2c2709dc VK |
1275 | /* Make sure there are no concurrent readers while updating opp_table */ |
1276 | WARN_ON(!list_empty(&opp_table->opp_list)); | |
7de36b0a | 1277 | |
25419de1 VK |
1278 | /* Another CPU that shares the OPP table has set the property ? */ |
1279 | if (opp_table->supported_hw) | |
1280 | return opp_table; | |
7de36b0a | 1281 | |
2c2709dc | 1282 | opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions), |
7de36b0a | 1283 | GFP_KERNEL); |
2c2709dc | 1284 | if (!opp_table->supported_hw) { |
25419de1 VK |
1285 | dev_pm_opp_put_opp_table(opp_table); |
1286 | return ERR_PTR(-ENOMEM); | |
7de36b0a VK |
1287 | } |
1288 | ||
2c2709dc | 1289 | opp_table->supported_hw_count = count; |
fa30184d VK |
1290 | |
1291 | return opp_table; | |
7de36b0a VK |
1292 | } |
1293 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw); | |
1294 | ||
1295 | /** | |
1296 | * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw | |
fa30184d | 1297 | * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw(). |
7de36b0a VK |
1298 | * |
1299 | * This is required only for the V2 bindings, and is called for a matching | |
2c2709dc | 1300 | * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure |
7de36b0a | 1301 | * will not be freed. |
7de36b0a | 1302 | */ |
fa30184d | 1303 | void dev_pm_opp_put_supported_hw(struct opp_table *opp_table) |
7de36b0a | 1304 | { |
2c2709dc VK |
1305 | /* Make sure there are no concurrent readers while updating opp_table */ |
1306 | WARN_ON(!list_empty(&opp_table->opp_list)); | |
7de36b0a | 1307 | |
2c2709dc VK |
1308 | kfree(opp_table->supported_hw); |
1309 | opp_table->supported_hw = NULL; | |
1310 | opp_table->supported_hw_count = 0; | |
7de36b0a | 1311 | |
fa30184d | 1312 | dev_pm_opp_put_opp_table(opp_table); |
7de36b0a VK |
1313 | } |
1314 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw); | |
1315 | ||
01fb4d3c VK |
1316 | /** |
1317 | * dev_pm_opp_set_prop_name() - Set prop-extn name | |
a5da6447 | 1318 | * @dev: Device for which the prop-name has to be set. |
01fb4d3c VK |
1319 | * @name: name to postfix to properties. |
1320 | * | |
1321 | * This is required only for the V2 bindings, and it enables a platform to | |
1322 | * specify the extn to be used for certain property names. The properties to | |
1323 | * which the extension will apply are opp-microvolt and opp-microamp. OPP core | |
1324 | * should postfix the property name with -<name> while looking for them. | |
01fb4d3c | 1325 | */ |
fa30184d | 1326 | struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name) |
01fb4d3c | 1327 | { |
2c2709dc | 1328 | struct opp_table *opp_table; |
01fb4d3c | 1329 | |
fa30184d VK |
1330 | opp_table = dev_pm_opp_get_opp_table(dev); |
1331 | if (!opp_table) | |
1332 | return ERR_PTR(-ENOMEM); | |
01fb4d3c | 1333 | |
2c2709dc VK |
1334 | /* Make sure there are no concurrent readers while updating opp_table */ |
1335 | WARN_ON(!list_empty(&opp_table->opp_list)); | |
01fb4d3c | 1336 | |
878ec1a9 VK |
1337 | /* Another CPU that shares the OPP table has set the property ? */ |
1338 | if (opp_table->prop_name) | |
1339 | return opp_table; | |
01fb4d3c | 1340 | |
2c2709dc VK |
1341 | opp_table->prop_name = kstrdup(name, GFP_KERNEL); |
1342 | if (!opp_table->prop_name) { | |
878ec1a9 VK |
1343 | dev_pm_opp_put_opp_table(opp_table); |
1344 | return ERR_PTR(-ENOMEM); | |
01fb4d3c VK |
1345 | } |
1346 | ||
fa30184d | 1347 | return opp_table; |
01fb4d3c VK |
1348 | } |
1349 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name); | |
1350 | ||
1351 | /** | |
1352 | * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name | |
fa30184d | 1353 | * @opp_table: OPP table returned by dev_pm_opp_set_prop_name(). |
01fb4d3c VK |
1354 | * |
1355 | * This is required only for the V2 bindings, and is called for a matching | |
2c2709dc | 1356 | * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure |
01fb4d3c | 1357 | * will not be freed. |
01fb4d3c | 1358 | */ |
fa30184d | 1359 | void dev_pm_opp_put_prop_name(struct opp_table *opp_table) |
01fb4d3c | 1360 | { |
2c2709dc VK |
1361 | /* Make sure there are no concurrent readers while updating opp_table */ |
1362 | WARN_ON(!list_empty(&opp_table->opp_list)); | |
01fb4d3c | 1363 | |
2c2709dc VK |
1364 | kfree(opp_table->prop_name); |
1365 | opp_table->prop_name = NULL; | |
01fb4d3c | 1366 | |
fa30184d | 1367 | dev_pm_opp_put_opp_table(opp_table); |
01fb4d3c VK |
1368 | } |
1369 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name); | |
1370 | ||
94735585 VK |
1371 | static int _allocate_set_opp_data(struct opp_table *opp_table) |
1372 | { | |
1373 | struct dev_pm_set_opp_data *data; | |
1374 | int len, count = opp_table->regulator_count; | |
1375 | ||
90e3577b | 1376 | if (WARN_ON(!opp_table->regulators)) |
94735585 VK |
1377 | return -EINVAL; |
1378 | ||
1379 | /* space for set_opp_data */ | |
1380 | len = sizeof(*data); | |
1381 | ||
1382 | /* space for old_opp.supplies and new_opp.supplies */ | |
1383 | len += 2 * sizeof(struct dev_pm_opp_supply) * count; | |
1384 | ||
1385 | data = kzalloc(len, GFP_KERNEL); | |
1386 | if (!data) | |
1387 | return -ENOMEM; | |
1388 | ||
1389 | data->old_opp.supplies = (void *)(data + 1); | |
1390 | data->new_opp.supplies = data->old_opp.supplies + count; | |
1391 | ||
1392 | opp_table->set_opp_data = data; | |
1393 | ||
1394 | return 0; | |
1395 | } | |
1396 | ||
1397 | static void _free_set_opp_data(struct opp_table *opp_table) | |
1398 | { | |
1399 | kfree(opp_table->set_opp_data); | |
1400 | opp_table->set_opp_data = NULL; | |
1401 | } | |
1402 | ||
9f8ea969 | 1403 | /** |
dfbe4678 | 1404 | * dev_pm_opp_set_regulators() - Set regulator names for the device |
9f8ea969 | 1405 | * @dev: Device for which regulator name is being set. |
dfbe4678 VK |
1406 | * @names: Array of pointers to the names of the regulator. |
1407 | * @count: Number of regulators. | |
9f8ea969 VK |
1408 | * |
1409 | * In order to support OPP switching, OPP layer needs to know the name of the | |
dfbe4678 VK |
1410 | * device's regulators, as the core would be required to switch voltages as |
1411 | * well. | |
9f8ea969 VK |
1412 | * |
1413 | * This must be called before any OPPs are initialized for the device. | |
9f8ea969 | 1414 | */ |
dfbe4678 VK |
1415 | struct opp_table *dev_pm_opp_set_regulators(struct device *dev, |
1416 | const char * const names[], | |
1417 | unsigned int count) | |
9f8ea969 | 1418 | { |
2c2709dc | 1419 | struct opp_table *opp_table; |
9f8ea969 | 1420 | struct regulator *reg; |
dfbe4678 | 1421 | int ret, i; |
9f8ea969 | 1422 | |
fa30184d VK |
1423 | opp_table = dev_pm_opp_get_opp_table(dev); |
1424 | if (!opp_table) | |
1425 | return ERR_PTR(-ENOMEM); | |
9f8ea969 VK |
1426 | |
1427 | /* This should be called before OPPs are initialized */ | |
2c2709dc | 1428 | if (WARN_ON(!list_empty(&opp_table->opp_list))) { |
9f8ea969 VK |
1429 | ret = -EBUSY; |
1430 | goto err; | |
1431 | } | |
1432 | ||
779b783c VK |
1433 | /* Another CPU that shares the OPP table has set the regulators ? */ |
1434 | if (opp_table->regulators) | |
1435 | return opp_table; | |
dfbe4678 VK |
1436 | |
1437 | opp_table->regulators = kmalloc_array(count, | |
1438 | sizeof(*opp_table->regulators), | |
1439 | GFP_KERNEL); | |
1440 | if (!opp_table->regulators) { | |
1441 | ret = -ENOMEM; | |
9f8ea969 VK |
1442 | goto err; |
1443 | } | |
1444 | ||
dfbe4678 VK |
1445 | for (i = 0; i < count; i++) { |
1446 | reg = regulator_get_optional(dev, names[i]); | |
1447 | if (IS_ERR(reg)) { | |
1448 | ret = PTR_ERR(reg); | |
1449 | if (ret != -EPROBE_DEFER) | |
1450 | dev_err(dev, "%s: no regulator (%s) found: %d\n", | |
1451 | __func__, names[i], ret); | |
1452 | goto free_regulators; | |
1453 | } | |
1454 | ||
1455 | opp_table->regulators[i] = reg; | |
1456 | } | |
1457 | ||
1458 | opp_table->regulator_count = count; | |
9f8ea969 | 1459 | |
94735585 VK |
1460 | /* Allocate block only once to pass to set_opp() routines */ |
1461 | ret = _allocate_set_opp_data(opp_table); | |
1462 | if (ret) | |
1463 | goto free_regulators; | |
1464 | ||
91291d9a | 1465 | return opp_table; |
9f8ea969 | 1466 | |
dfbe4678 VK |
1467 | free_regulators: |
1468 | while (i != 0) | |
1469 | regulator_put(opp_table->regulators[--i]); | |
1470 | ||
1471 | kfree(opp_table->regulators); | |
1472 | opp_table->regulators = NULL; | |
46f48aca | 1473 | opp_table->regulator_count = -1; |
9f8ea969 | 1474 | err: |
fa30184d | 1475 | dev_pm_opp_put_opp_table(opp_table); |
9f8ea969 | 1476 | |
91291d9a | 1477 | return ERR_PTR(ret); |
9f8ea969 | 1478 | } |
dfbe4678 | 1479 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators); |
9f8ea969 VK |
1480 | |
1481 | /** | |
dfbe4678 VK |
1482 | * dev_pm_opp_put_regulators() - Releases resources blocked for regulator |
1483 | * @opp_table: OPP table returned from dev_pm_opp_set_regulators(). | |
9f8ea969 | 1484 | */ |
dfbe4678 | 1485 | void dev_pm_opp_put_regulators(struct opp_table *opp_table) |
9f8ea969 | 1486 | { |
dfbe4678 VK |
1487 | int i; |
1488 | ||
779b783c VK |
1489 | if (!opp_table->regulators) |
1490 | goto put_opp_table; | |
9f8ea969 | 1491 | |
2c2709dc VK |
1492 | /* Make sure there are no concurrent readers while updating opp_table */ |
1493 | WARN_ON(!list_empty(&opp_table->opp_list)); | |
9f8ea969 | 1494 | |
dfbe4678 VK |
1495 | for (i = opp_table->regulator_count - 1; i >= 0; i--) |
1496 | regulator_put(opp_table->regulators[i]); | |
1497 | ||
94735585 VK |
1498 | _free_set_opp_data(opp_table); |
1499 | ||
dfbe4678 VK |
1500 | kfree(opp_table->regulators); |
1501 | opp_table->regulators = NULL; | |
46f48aca | 1502 | opp_table->regulator_count = -1; |
9f8ea969 | 1503 | |
779b783c | 1504 | put_opp_table: |
fa30184d | 1505 | dev_pm_opp_put_opp_table(opp_table); |
9f8ea969 | 1506 | } |
dfbe4678 | 1507 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators); |
9f8ea969 | 1508 | |
829a4e8c VK |
1509 | /** |
1510 | * dev_pm_opp_set_clkname() - Set clk name for the device | |
1511 | * @dev: Device for which clk name is being set. | |
1512 | * @name: Clk name. | |
1513 | * | |
1514 | * In order to support OPP switching, OPP layer needs to get pointer to the | |
1515 | * clock for the device. Simple cases work fine without using this routine (i.e. | |
1516 | * by passing connection-id as NULL), but for a device with multiple clocks | |
1517 | * available, the OPP core needs to know the exact name of the clk to use. | |
1518 | * | |
1519 | * This must be called before any OPPs are initialized for the device. | |
1520 | */ | |
1521 | struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name) | |
1522 | { | |
1523 | struct opp_table *opp_table; | |
1524 | int ret; | |
1525 | ||
1526 | opp_table = dev_pm_opp_get_opp_table(dev); | |
1527 | if (!opp_table) | |
1528 | return ERR_PTR(-ENOMEM); | |
1529 | ||
1530 | /* This should be called before OPPs are initialized */ | |
1531 | if (WARN_ON(!list_empty(&opp_table->opp_list))) { | |
1532 | ret = -EBUSY; | |
1533 | goto err; | |
1534 | } | |
1535 | ||
1536 | /* Already have default clk set, free it */ | |
1537 | if (!IS_ERR(opp_table->clk)) | |
1538 | clk_put(opp_table->clk); | |
1539 | ||
1540 | /* Find clk for the device */ | |
1541 | opp_table->clk = clk_get(dev, name); | |
1542 | if (IS_ERR(opp_table->clk)) { | |
1543 | ret = PTR_ERR(opp_table->clk); | |
1544 | if (ret != -EPROBE_DEFER) { | |
1545 | dev_err(dev, "%s: Couldn't find clock: %d\n", __func__, | |
1546 | ret); | |
1547 | } | |
1548 | goto err; | |
1549 | } | |
1550 | ||
1551 | return opp_table; | |
1552 | ||
1553 | err: | |
1554 | dev_pm_opp_put_opp_table(opp_table); | |
1555 | ||
1556 | return ERR_PTR(ret); | |
1557 | } | |
1558 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname); | |
1559 | ||
1560 | /** | |
1561 | * dev_pm_opp_put_clkname() - Releases resources blocked for clk. | |
1562 | * @opp_table: OPP table returned from dev_pm_opp_set_clkname(). | |
1563 | */ | |
1564 | void dev_pm_opp_put_clkname(struct opp_table *opp_table) | |
1565 | { | |
1566 | /* Make sure there are no concurrent readers while updating opp_table */ | |
1567 | WARN_ON(!list_empty(&opp_table->opp_list)); | |
1568 | ||
1569 | clk_put(opp_table->clk); | |
1570 | opp_table->clk = ERR_PTR(-EINVAL); | |
1571 | ||
1572 | dev_pm_opp_put_opp_table(opp_table); | |
1573 | } | |
1574 | EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname); | |
1575 | ||
4dab160e VK |
1576 | /** |
1577 | * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper | |
1578 | * @dev: Device for which the helper is getting registered. | |
1579 | * @set_opp: Custom set OPP helper. | |
1580 | * | |
1581 | * This is useful to support complex platforms (like platforms with multiple | |
1582 | * regulators per device), instead of the generic OPP set rate helper. | |
1583 | * | |
1584 | * This must be called before any OPPs are initialized for the device. | |
4dab160e | 1585 | */ |
fa30184d | 1586 | struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev, |
4dab160e VK |
1587 | int (*set_opp)(struct dev_pm_set_opp_data *data)) |
1588 | { | |
1589 | struct opp_table *opp_table; | |
4dab160e VK |
1590 | |
1591 | if (!set_opp) | |
fa30184d | 1592 | return ERR_PTR(-EINVAL); |
4dab160e | 1593 | |
fa30184d VK |
1594 | opp_table = dev_pm_opp_get_opp_table(dev); |
1595 | if (!opp_table) | |
1596 | return ERR_PTR(-ENOMEM); | |
4dab160e VK |
1597 | |
1598 | /* This should be called before OPPs are initialized */ | |
1599 | if (WARN_ON(!list_empty(&opp_table->opp_list))) { | |
5019acc6 VK |
1600 | dev_pm_opp_put_opp_table(opp_table); |
1601 | return ERR_PTR(-EBUSY); | |
4dab160e VK |
1602 | } |
1603 | ||
5019acc6 VK |
1604 | /* Another CPU that shares the OPP table has set the helper ? */ |
1605 | if (!opp_table->set_opp) | |
1606 | opp_table->set_opp = set_opp; | |
4dab160e | 1607 | |
fa30184d | 1608 | return opp_table; |
4dab160e VK |
1609 | } |
1610 | EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper); | |
1611 | ||
1612 | /** | |
604a7aeb | 1613 | * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for |
4dab160e | 1614 | * set_opp helper |
fa30184d | 1615 | * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper(). |
4dab160e | 1616 | * |
fa30184d | 1617 | * Release resources blocked for platform specific set_opp helper. |
4dab160e | 1618 | */ |
604a7aeb | 1619 | void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table) |
4dab160e | 1620 | { |
4dab160e VK |
1621 | /* Make sure there are no concurrent readers while updating opp_table */ |
1622 | WARN_ON(!list_empty(&opp_table->opp_list)); | |
1623 | ||
1624 | opp_table->set_opp = NULL; | |
fa30184d | 1625 | dev_pm_opp_put_opp_table(opp_table); |
4dab160e | 1626 | } |
604a7aeb | 1627 | EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper); |
4dab160e | 1628 | |
4f018bc0 VK |
1629 | /** |
1630 | * dev_pm_opp_set_genpd_virt_dev - Set virtual genpd device for an index | |
1631 | * @dev: Consumer device for which the genpd device is getting set. | |
1632 | * @virt_dev: virtual genpd device. | |
1633 | * @index: index. | |
1634 | * | |
1635 | * Multiple generic power domains for a device are supported with the help of | |
1636 | * virtual genpd devices, which are created for each consumer device - genpd | |
1637 | * pair. These are the device structures which are attached to the power domain | |
1638 | * and are required by the OPP core to set the performance state of the genpd. | |
1639 | * | |
1640 | * This helper will normally be called by the consumer driver of the device | |
1641 | * "dev", as only that has details of the genpd devices. | |
1642 | * | |
1643 | * This helper needs to be called once for each of those virtual devices, but | |
1644 | * only if multiple domains are available for a device. Otherwise the original | |
1645 | * device structure will be used instead by the OPP core. | |
1646 | */ | |
1647 | struct opp_table *dev_pm_opp_set_genpd_virt_dev(struct device *dev, | |
1648 | struct device *virt_dev, | |
1649 | int index) | |
1650 | { | |
1651 | struct opp_table *opp_table; | |
1652 | ||
1653 | opp_table = dev_pm_opp_get_opp_table(dev); | |
1654 | if (!opp_table) | |
1655 | return ERR_PTR(-ENOMEM); | |
1656 | ||
1657 | mutex_lock(&opp_table->genpd_virt_dev_lock); | |
1658 | ||
1659 | if (unlikely(!opp_table->genpd_virt_devs || | |
1660 | index >= opp_table->required_opp_count || | |
1661 | opp_table->genpd_virt_devs[index])) { | |
1662 | ||
1663 | dev_err(dev, "Invalid request to set required device\n"); | |
1664 | dev_pm_opp_put_opp_table(opp_table); | |
1665 | mutex_unlock(&opp_table->genpd_virt_dev_lock); | |
1666 | ||
1667 | return ERR_PTR(-EINVAL); | |
1668 | } | |
1669 | ||
1670 | opp_table->genpd_virt_devs[index] = virt_dev; | |
1671 | mutex_unlock(&opp_table->genpd_virt_dev_lock); | |
1672 | ||
1673 | return opp_table; | |
1674 | } | |
1675 | ||
1676 | /** | |
1677 | * dev_pm_opp_put_genpd_virt_dev() - Releases resources blocked for genpd device. | |
1678 | * @opp_table: OPP table returned by dev_pm_opp_set_genpd_virt_dev(). | |
1679 | * @virt_dev: virtual genpd device. | |
1680 | * | |
1681 | * This releases the resource previously acquired with a call to | |
1682 | * dev_pm_opp_set_genpd_virt_dev(). The consumer driver shall call this helper | |
1683 | * if it doesn't want OPP core to update performance state of a power domain | |
1684 | * anymore. | |
1685 | */ | |
1686 | void dev_pm_opp_put_genpd_virt_dev(struct opp_table *opp_table, | |
1687 | struct device *virt_dev) | |
1688 | { | |
1689 | int i; | |
1690 | ||
1691 | /* | |
1692 | * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting | |
1693 | * used in parallel. | |
1694 | */ | |
1695 | mutex_lock(&opp_table->genpd_virt_dev_lock); | |
1696 | ||
1697 | for (i = 0; i < opp_table->required_opp_count; i++) { | |
1698 | if (opp_table->genpd_virt_devs[i] != virt_dev) | |
1699 | continue; | |
1700 | ||
1701 | opp_table->genpd_virt_devs[i] = NULL; | |
1702 | dev_pm_opp_put_opp_table(opp_table); | |
1703 | ||
1704 | /* Drop the vote */ | |
1705 | dev_pm_genpd_set_performance_state(virt_dev, 0); | |
1706 | break; | |
1707 | } | |
1708 | ||
1709 | mutex_unlock(&opp_table->genpd_virt_dev_lock); | |
1710 | ||
1711 | if (unlikely(i == opp_table->required_opp_count)) | |
1712 | dev_err(virt_dev, "Failed to find required device entry\n"); | |
1713 | } | |
1714 | ||
c8a59103 VK |
1715 | /** |
1716 | * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table. | |
1717 | * @src_table: OPP table which has dst_table as one of its required OPP table. | |
1718 | * @dst_table: Required OPP table of the src_table. | |
1719 | * @pstate: Current performance state of the src_table. | |
1720 | * | |
1721 | * This Returns pstate of the OPP (present in @dst_table) pointed out by the | |
1722 | * "required-opps" property of the OPP (present in @src_table) which has | |
1723 | * performance state set to @pstate. | |
1724 | * | |
1725 | * Return: Zero or positive performance state on success, otherwise negative | |
1726 | * value on errors. | |
1727 | */ | |
1728 | int dev_pm_opp_xlate_performance_state(struct opp_table *src_table, | |
1729 | struct opp_table *dst_table, | |
1730 | unsigned int pstate) | |
1731 | { | |
1732 | struct dev_pm_opp *opp; | |
1733 | int dest_pstate = -EINVAL; | |
1734 | int i; | |
1735 | ||
1736 | if (!pstate) | |
1737 | return 0; | |
1738 | ||
1739 | /* | |
1740 | * Normally the src_table will have the "required_opps" property set to | |
1741 | * point to one of the OPPs in the dst_table, but in some cases the | |
1742 | * genpd and its master have one to one mapping of performance states | |
1743 | * and so none of them have the "required-opps" property set. Return the | |
1744 | * pstate of the src_table as it is in such cases. | |
1745 | */ | |
1746 | if (!src_table->required_opp_count) | |
1747 | return pstate; | |
1748 | ||
1749 | for (i = 0; i < src_table->required_opp_count; i++) { | |
1750 | if (src_table->required_opp_tables[i]->np == dst_table->np) | |
1751 | break; | |
1752 | } | |
1753 | ||
1754 | if (unlikely(i == src_table->required_opp_count)) { | |
1755 | pr_err("%s: Couldn't find matching OPP table (%p: %p)\n", | |
1756 | __func__, src_table, dst_table); | |
1757 | return -EINVAL; | |
1758 | } | |
1759 | ||
1760 | mutex_lock(&src_table->lock); | |
1761 | ||
1762 | list_for_each_entry(opp, &src_table->opp_list, node) { | |
1763 | if (opp->pstate == pstate) { | |
1764 | dest_pstate = opp->required_opps[i]->pstate; | |
1765 | goto unlock; | |
1766 | } | |
1767 | } | |
1768 | ||
1769 | pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table, | |
1770 | dst_table); | |
1771 | ||
1772 | unlock: | |
1773 | mutex_unlock(&src_table->lock); | |
1774 | ||
1775 | return dest_pstate; | |
1776 | } | |
1777 | ||
38393409 VK |
1778 | /** |
1779 | * dev_pm_opp_add() - Add an OPP table from a table definitions | |
1780 | * @dev: device for which we do this operation | |
1781 | * @freq: Frequency in Hz for this OPP | |
1782 | * @u_volt: Voltage in uVolts for this OPP | |
1783 | * | |
2c2709dc | 1784 | * This function adds an opp definition to the opp table and returns status. |
38393409 VK |
1785 | * The opp is made available by default and it can be controlled using |
1786 | * dev_pm_opp_enable/disable functions. | |
1787 | * | |
38393409 | 1788 | * Return: |
984f16c8 | 1789 | * 0 On success OR |
38393409 | 1790 | * Duplicate OPPs (both freq and volt are same) and opp->available |
984f16c8 | 1791 | * -EEXIST Freq are same and volt are different OR |
38393409 | 1792 | * Duplicate OPPs (both freq and volt are same) and !opp->available |
984f16c8 | 1793 | * -ENOMEM Memory allocation failure |
38393409 VK |
1794 | */ |
1795 | int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) | |
1796 | { | |
8cd2f6e8 VK |
1797 | struct opp_table *opp_table; |
1798 | int ret; | |
1799 | ||
b83c1899 VK |
1800 | opp_table = dev_pm_opp_get_opp_table(dev); |
1801 | if (!opp_table) | |
1802 | return -ENOMEM; | |
8cd2f6e8 | 1803 | |
46f48aca VK |
1804 | /* Fix regulator count for dynamic OPPs */ |
1805 | opp_table->regulator_count = 1; | |
1806 | ||
8cd2f6e8 | 1807 | ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); |
0ad8c623 VK |
1808 | if (ret) |
1809 | dev_pm_opp_put_opp_table(opp_table); | |
8cd2f6e8 | 1810 | |
8cd2f6e8 | 1811 | return ret; |
38393409 | 1812 | } |
5d4879cd | 1813 | EXPORT_SYMBOL_GPL(dev_pm_opp_add); |
e1f60b29 NM |
1814 | |
1815 | /** | |
327854c8 | 1816 | * _opp_set_availability() - helper to set the availability of an opp |
e1f60b29 NM |
1817 | * @dev: device for which we do this operation |
1818 | * @freq: OPP frequency to modify availability | |
1819 | * @availability_req: availability status requested for this opp | |
1820 | * | |
052c6f19 VK |
1821 | * Set the availability of an OPP, opp_{enable,disable} share a common logic |
1822 | * which is isolated here. | |
e1f60b29 | 1823 | * |
984f16c8 | 1824 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the |
e1a2d49c | 1825 | * copy operation, returns 0 if no modification was done OR modification was |
e1f60b29 | 1826 | * successful. |
e1f60b29 | 1827 | */ |
327854c8 NM |
1828 | static int _opp_set_availability(struct device *dev, unsigned long freq, |
1829 | bool availability_req) | |
e1f60b29 | 1830 | { |
2c2709dc | 1831 | struct opp_table *opp_table; |
a7f3987e | 1832 | struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); |
e1f60b29 NM |
1833 | int r = 0; |
1834 | ||
2c2709dc VK |
1835 | /* Find the opp_table */ |
1836 | opp_table = _find_opp_table(dev); | |
1837 | if (IS_ERR(opp_table)) { | |
1838 | r = PTR_ERR(opp_table); | |
e1f60b29 | 1839 | dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); |
a7f3987e | 1840 | return r; |
e1f60b29 NM |
1841 | } |
1842 | ||
37a73ec0 VK |
1843 | mutex_lock(&opp_table->lock); |
1844 | ||
e1f60b29 | 1845 | /* Do we have the frequency? */ |
2c2709dc | 1846 | list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { |
e1f60b29 NM |
1847 | if (tmp_opp->rate == freq) { |
1848 | opp = tmp_opp; | |
1849 | break; | |
1850 | } | |
1851 | } | |
37a73ec0 | 1852 | |
e1f60b29 NM |
1853 | if (IS_ERR(opp)) { |
1854 | r = PTR_ERR(opp); | |
1855 | goto unlock; | |
1856 | } | |
1857 | ||
1858 | /* Is update really needed? */ | |
1859 | if (opp->available == availability_req) | |
1860 | goto unlock; | |
e1f60b29 | 1861 | |
a7f3987e | 1862 | opp->available = availability_req; |
e1f60b29 | 1863 | |
e4d8ae00 VK |
1864 | dev_pm_opp_get(opp); |
1865 | mutex_unlock(&opp_table->lock); | |
1866 | ||
03ca370f MH |
1867 | /* Notify the change of the OPP availability */ |
1868 | if (availability_req) | |
052c6f19 | 1869 | blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE, |
a7f3987e | 1870 | opp); |
03ca370f | 1871 | else |
052c6f19 | 1872 | blocking_notifier_call_chain(&opp_table->head, |
a7f3987e | 1873 | OPP_EVENT_DISABLE, opp); |
e1f60b29 | 1874 | |
e4d8ae00 VK |
1875 | dev_pm_opp_put(opp); |
1876 | goto put_table; | |
1877 | ||
e1f60b29 | 1878 | unlock: |
5b650b38 | 1879 | mutex_unlock(&opp_table->lock); |
e4d8ae00 | 1880 | put_table: |
5b650b38 | 1881 | dev_pm_opp_put_opp_table(opp_table); |
e1f60b29 NM |
1882 | return r; |
1883 | } | |
1884 | ||
1885 | /** | |
5d4879cd | 1886 | * dev_pm_opp_enable() - Enable a specific OPP |
e1f60b29 NM |
1887 | * @dev: device for which we do this operation |
1888 | * @freq: OPP frequency to enable | |
1889 | * | |
1890 | * Enables a provided opp. If the operation is valid, this returns 0, else the | |
1891 | * corresponding error value. It is meant to be used for users an OPP available | |
5d4879cd | 1892 | * after being temporarily made unavailable with dev_pm_opp_disable. |
e1f60b29 | 1893 | * |
984f16c8 | 1894 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the |
e1a2d49c | 1895 | * copy operation, returns 0 if no modification was done OR modification was |
984f16c8 | 1896 | * successful. |
e1f60b29 | 1897 | */ |
5d4879cd | 1898 | int dev_pm_opp_enable(struct device *dev, unsigned long freq) |
e1f60b29 | 1899 | { |
327854c8 | 1900 | return _opp_set_availability(dev, freq, true); |
e1f60b29 | 1901 | } |
5d4879cd | 1902 | EXPORT_SYMBOL_GPL(dev_pm_opp_enable); |
e1f60b29 NM |
1903 | |
1904 | /** | |
5d4879cd | 1905 | * dev_pm_opp_disable() - Disable a specific OPP |
e1f60b29 NM |
1906 | * @dev: device for which we do this operation |
1907 | * @freq: OPP frequency to disable | |
1908 | * | |
1909 | * Disables a provided opp. If the operation is valid, this returns | |
1910 | * 0, else the corresponding error value. It is meant to be a temporary | |
1911 | * control by users to make this OPP not available until the circumstances are | |
5d4879cd | 1912 | * right to make it available again (with a call to dev_pm_opp_enable). |
e1f60b29 | 1913 | * |
984f16c8 | 1914 | * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the |
e1a2d49c | 1915 | * copy operation, returns 0 if no modification was done OR modification was |
984f16c8 | 1916 | * successful. |
e1f60b29 | 1917 | */ |
5d4879cd | 1918 | int dev_pm_opp_disable(struct device *dev, unsigned long freq) |
e1f60b29 | 1919 | { |
327854c8 | 1920 | return _opp_set_availability(dev, freq, false); |
e1f60b29 | 1921 | } |
5d4879cd | 1922 | EXPORT_SYMBOL_GPL(dev_pm_opp_disable); |
e1f60b29 | 1923 | |
03ca370f | 1924 | /** |
dc2c9ad5 VK |
1925 | * dev_pm_opp_register_notifier() - Register OPP notifier for the device |
1926 | * @dev: Device for which notifier needs to be registered | |
1927 | * @nb: Notifier block to be registered | |
984f16c8 | 1928 | * |
dc2c9ad5 VK |
1929 | * Return: 0 on success or a negative error value. |
1930 | */ | |
1931 | int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) | |
1932 | { | |
1933 | struct opp_table *opp_table; | |
1934 | int ret; | |
1935 | ||
dc2c9ad5 | 1936 | opp_table = _find_opp_table(dev); |
5b650b38 VK |
1937 | if (IS_ERR(opp_table)) |
1938 | return PTR_ERR(opp_table); | |
1939 | ||
052c6f19 | 1940 | ret = blocking_notifier_chain_register(&opp_table->head, nb); |
dc2c9ad5 | 1941 | |
5b650b38 | 1942 | dev_pm_opp_put_opp_table(opp_table); |
dc2c9ad5 VK |
1943 | |
1944 | return ret; | |
1945 | } | |
1946 | EXPORT_SYMBOL(dev_pm_opp_register_notifier); | |
1947 | ||
1948 | /** | |
1949 | * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device | |
1950 | * @dev: Device for which notifier needs to be unregistered | |
1951 | * @nb: Notifier block to be unregistered | |
984f16c8 | 1952 | * |
dc2c9ad5 | 1953 | * Return: 0 on success or a negative error value. |
03ca370f | 1954 | */ |
dc2c9ad5 VK |
1955 | int dev_pm_opp_unregister_notifier(struct device *dev, |
1956 | struct notifier_block *nb) | |
03ca370f | 1957 | { |
dc2c9ad5 VK |
1958 | struct opp_table *opp_table; |
1959 | int ret; | |
03ca370f | 1960 | |
dc2c9ad5 | 1961 | opp_table = _find_opp_table(dev); |
5b650b38 VK |
1962 | if (IS_ERR(opp_table)) |
1963 | return PTR_ERR(opp_table); | |
dc2c9ad5 | 1964 | |
052c6f19 | 1965 | ret = blocking_notifier_chain_unregister(&opp_table->head, nb); |
03ca370f | 1966 | |
5b650b38 | 1967 | dev_pm_opp_put_opp_table(opp_table); |
dc2c9ad5 VK |
1968 | |
1969 | return ret; | |
03ca370f | 1970 | } |
dc2c9ad5 | 1971 | EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); |
b496dfbc | 1972 | |
2a4eb735 | 1973 | void _dev_pm_opp_find_and_remove_table(struct device *dev) |
9274c892 VK |
1974 | { |
1975 | struct opp_table *opp_table; | |
1976 | ||
2c2709dc VK |
1977 | /* Check for existing table for 'dev' */ |
1978 | opp_table = _find_opp_table(dev); | |
1979 | if (IS_ERR(opp_table)) { | |
1980 | int error = PTR_ERR(opp_table); | |
737002b5 VK |
1981 | |
1982 | if (error != -ENODEV) | |
2c2709dc | 1983 | WARN(1, "%s: opp_table: %d\n", |
737002b5 VK |
1984 | IS_ERR_OR_NULL(dev) ? |
1985 | "Invalid device" : dev_name(dev), | |
1986 | error); | |
5b650b38 | 1987 | return; |
737002b5 VK |
1988 | } |
1989 | ||
cdd6ed90 VK |
1990 | _put_opp_list_kref(opp_table); |
1991 | ||
1992 | /* Drop reference taken by _find_opp_table() */ | |
1993 | dev_pm_opp_put_opp_table(opp_table); | |
737002b5 | 1994 | |
cdd6ed90 | 1995 | /* Drop reference taken while the OPP table was added */ |
5b650b38 | 1996 | dev_pm_opp_put_opp_table(opp_table); |
737002b5 | 1997 | } |
129eec55 VK |
1998 | |
1999 | /** | |
411466c5 | 2000 | * dev_pm_opp_remove_table() - Free all OPPs associated with the device |
2c2709dc | 2001 | * @dev: device pointer used to lookup OPP table. |
129eec55 | 2002 | * |
411466c5 SH |
2003 | * Free both OPPs created using static entries present in DT and the |
2004 | * dynamically added entries. | |
129eec55 | 2005 | */ |
411466c5 | 2006 | void dev_pm_opp_remove_table(struct device *dev) |
129eec55 | 2007 | { |
2a4eb735 | 2008 | _dev_pm_opp_find_and_remove_table(dev); |
8d4d4e98 | 2009 | } |
411466c5 | 2010 | EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table); |