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