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