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