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