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