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