1
2
3
4
5
6
7
8
9
10
11
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/regulator/consumer.h>
23
24#include "opp.h"
25
26
27
28
29
30
31LIST_HEAD(opp_tables);
32
33DEFINE_MUTEX(opp_table_lock);
34
35#define opp_rcu_lockdep_assert() \
36do { \
37 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
38 !lockdep_is_held(&opp_table_lock), \
39 "Missing rcu_read_lock() or " \
40 "opp_table_lock protection"); \
41} while (0)
42
43static struct opp_device *_find_opp_dev(const struct device *dev,
44 struct opp_table *opp_table)
45{
46 struct opp_device *opp_dev;
47
48 list_for_each_entry(opp_dev, &opp_table->dev_list, node)
49 if (opp_dev->dev == dev)
50 return opp_dev;
51
52 return NULL;
53}
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71struct opp_table *_find_opp_table(struct device *dev)
72{
73 struct opp_table *opp_table;
74
75 opp_rcu_lockdep_assert();
76
77 if (IS_ERR_OR_NULL(dev)) {
78 pr_err("%s: Invalid parameters\n", __func__);
79 return ERR_PTR(-EINVAL);
80 }
81
82 list_for_each_entry_rcu(opp_table, &opp_tables, node)
83 if (_find_opp_dev(dev, opp_table))
84 return opp_table;
85
86 return ERR_PTR(-ENODEV);
87}
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
105{
106 struct dev_pm_opp *tmp_opp;
107 unsigned long v = 0;
108
109 opp_rcu_lockdep_assert();
110
111 tmp_opp = rcu_dereference(opp);
112 if (IS_ERR_OR_NULL(tmp_opp))
113 pr_err("%s: Invalid parameters\n", __func__);
114 else
115 v = tmp_opp->u_volt;
116
117 return v;
118}
119EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
137{
138 struct dev_pm_opp *tmp_opp;
139 unsigned long f = 0;
140
141 opp_rcu_lockdep_assert();
142
143 tmp_opp = rcu_dereference(opp);
144 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available)
145 pr_err("%s: Invalid parameters\n", __func__);
146 else
147 f = tmp_opp->rate;
148
149 return f;
150}
151EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
172{
173 struct dev_pm_opp *tmp_opp;
174
175 opp_rcu_lockdep_assert();
176
177 tmp_opp = rcu_dereference(opp);
178 if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available) {
179 pr_err("%s: Invalid parameters\n", __func__);
180 return false;
181 }
182
183 return tmp_opp->turbo;
184}
185EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
186
187
188
189
190
191
192
193
194
195unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
196{
197 struct opp_table *opp_table;
198 unsigned long clock_latency_ns;
199
200 rcu_read_lock();
201
202 opp_table = _find_opp_table(dev);
203 if (IS_ERR(opp_table))
204 clock_latency_ns = 0;
205 else
206 clock_latency_ns = opp_table->clock_latency_ns_max;
207
208 rcu_read_unlock();
209 return clock_latency_ns;
210}
211EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
212
213
214
215
216
217
218
219
220
221unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
222{
223 struct opp_table *opp_table;
224 struct dev_pm_opp *opp;
225 struct regulator *reg;
226 unsigned long latency_ns = 0;
227 unsigned long min_uV = ~0, max_uV = 0;
228 int ret;
229
230 rcu_read_lock();
231
232 opp_table = _find_opp_table(dev);
233 if (IS_ERR(opp_table)) {
234 rcu_read_unlock();
235 return 0;
236 }
237
238 reg = opp_table->regulator;
239 if (IS_ERR(reg)) {
240
241 rcu_read_unlock();
242 return 0;
243 }
244
245 list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
246 if (!opp->available)
247 continue;
248
249 if (opp->u_volt_min < min_uV)
250 min_uV = opp->u_volt_min;
251 if (opp->u_volt_max > max_uV)
252 max_uV = opp->u_volt_max;
253 }
254
255 rcu_read_unlock();
256
257
258
259
260
261 ret = regulator_set_voltage_time(reg, min_uV, max_uV);
262 if (ret > 0)
263 latency_ns = ret * 1000;
264
265 return latency_ns;
266}
267EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
268
269
270
271
272
273
274
275
276
277
278
279unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
280{
281 return dev_pm_opp_get_max_volt_latency(dev) +
282 dev_pm_opp_get_max_clock_latency(dev);
283}
284EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev)
300{
301 struct opp_table *opp_table;
302
303 opp_rcu_lockdep_assert();
304
305 opp_table = _find_opp_table(dev);
306 if (IS_ERR(opp_table) || !opp_table->suspend_opp ||
307 !opp_table->suspend_opp->available)
308 return NULL;
309
310 return opp_table->suspend_opp;
311}
312EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp);
313
314
315
316
317
318
319
320
321
322
323int dev_pm_opp_get_opp_count(struct device *dev)
324{
325 struct opp_table *opp_table;
326 struct dev_pm_opp *temp_opp;
327 int count = 0;
328
329 rcu_read_lock();
330
331 opp_table = _find_opp_table(dev);
332 if (IS_ERR(opp_table)) {
333 count = PTR_ERR(opp_table);
334 dev_err(dev, "%s: OPP table not found (%d)\n",
335 __func__, count);
336 goto out_unlock;
337 }
338
339 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
340 if (temp_opp->available)
341 count++;
342 }
343
344out_unlock:
345 rcu_read_unlock();
346 return count;
347}
348EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
377 unsigned long freq,
378 bool available)
379{
380 struct opp_table *opp_table;
381 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
382
383 opp_rcu_lockdep_assert();
384
385 opp_table = _find_opp_table(dev);
386 if (IS_ERR(opp_table)) {
387 int r = PTR_ERR(opp_table);
388
389 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
390 return ERR_PTR(r);
391 }
392
393 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
394 if (temp_opp->available == available &&
395 temp_opp->rate == freq) {
396 opp = temp_opp;
397 break;
398 }
399 }
400
401 return opp;
402}
403EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
404
405static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
406 unsigned long *freq)
407{
408 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
409
410 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
411 if (temp_opp->available && temp_opp->rate >= *freq) {
412 opp = temp_opp;
413 *freq = opp->rate;
414 break;
415 }
416 }
417
418 return opp;
419}
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
443 unsigned long *freq)
444{
445 struct opp_table *opp_table;
446
447 opp_rcu_lockdep_assert();
448
449 if (!dev || !freq) {
450 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
451 return ERR_PTR(-EINVAL);
452 }
453
454 opp_table = _find_opp_table(dev);
455 if (IS_ERR(opp_table))
456 return ERR_CAST(opp_table);
457
458 return _find_freq_ceil(opp_table, freq);
459}
460EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
484 unsigned long *freq)
485{
486 struct opp_table *opp_table;
487 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
488
489 opp_rcu_lockdep_assert();
490
491 if (!dev || !freq) {
492 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
493 return ERR_PTR(-EINVAL);
494 }
495
496 opp_table = _find_opp_table(dev);
497 if (IS_ERR(opp_table))
498 return ERR_CAST(opp_table);
499
500 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
501 if (temp_opp->available) {
502
503 if (temp_opp->rate > *freq)
504 break;
505 else
506 opp = temp_opp;
507 }
508 }
509 if (!IS_ERR(opp))
510 *freq = opp->rate;
511
512 return opp;
513}
514EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
515
516
517
518
519
520static struct clk *_get_opp_clk(struct device *dev)
521{
522 struct opp_table *opp_table;
523 struct clk *clk;
524
525 rcu_read_lock();
526
527 opp_table = _find_opp_table(dev);
528 if (IS_ERR(opp_table)) {
529 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
530 clk = ERR_CAST(opp_table);
531 goto unlock;
532 }
533
534 clk = opp_table->clk;
535 if (IS_ERR(clk))
536 dev_err(dev, "%s: No clock available for the device\n",
537 __func__);
538
539unlock:
540 rcu_read_unlock();
541 return clk;
542}
543
544static int _set_opp_voltage(struct device *dev, struct regulator *reg,
545 unsigned long u_volt, unsigned long u_volt_min,
546 unsigned long u_volt_max)
547{
548 int ret;
549
550
551 if (IS_ERR(reg)) {
552 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
553 PTR_ERR(reg));
554 return 0;
555 }
556
557 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, u_volt_min,
558 u_volt, u_volt_max);
559
560 ret = regulator_set_voltage_triplet(reg, u_volt_min, u_volt,
561 u_volt_max);
562 if (ret)
563 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
564 __func__, u_volt_min, u_volt, u_volt_max, ret);
565
566 return ret;
567}
568
569
570
571
572
573
574
575
576
577
578
579int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
580{
581 struct opp_table *opp_table;
582 struct dev_pm_opp *old_opp, *opp;
583 struct regulator *reg;
584 struct clk *clk;
585 unsigned long freq, old_freq;
586 unsigned long u_volt, u_volt_min, u_volt_max;
587 unsigned long ou_volt, ou_volt_min, ou_volt_max;
588 int ret;
589
590 if (unlikely(!target_freq)) {
591 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
592 target_freq);
593 return -EINVAL;
594 }
595
596 clk = _get_opp_clk(dev);
597 if (IS_ERR(clk))
598 return PTR_ERR(clk);
599
600 freq = clk_round_rate(clk, target_freq);
601 if ((long)freq <= 0)
602 freq = target_freq;
603
604 old_freq = clk_get_rate(clk);
605
606
607 if (old_freq == freq) {
608 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
609 __func__, freq);
610 return 0;
611 }
612
613 rcu_read_lock();
614
615 opp_table = _find_opp_table(dev);
616 if (IS_ERR(opp_table)) {
617 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
618 rcu_read_unlock();
619 return PTR_ERR(opp_table);
620 }
621
622 old_opp = _find_freq_ceil(opp_table, &old_freq);
623 if (!IS_ERR(old_opp)) {
624 ou_volt = old_opp->u_volt;
625 ou_volt_min = old_opp->u_volt_min;
626 ou_volt_max = old_opp->u_volt_max;
627 } else {
628 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
629 __func__, old_freq, PTR_ERR(old_opp));
630 }
631
632 opp = _find_freq_ceil(opp_table, &freq);
633 if (IS_ERR(opp)) {
634 ret = PTR_ERR(opp);
635 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
636 __func__, freq, ret);
637 rcu_read_unlock();
638 return ret;
639 }
640
641 u_volt = opp->u_volt;
642 u_volt_min = opp->u_volt_min;
643 u_volt_max = opp->u_volt_max;
644
645 reg = opp_table->regulator;
646
647 rcu_read_unlock();
648
649
650 if (freq > old_freq) {
651 ret = _set_opp_voltage(dev, reg, u_volt, u_volt_min,
652 u_volt_max);
653 if (ret)
654 goto restore_voltage;
655 }
656
657
658
659 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n",
660 __func__, old_freq, freq);
661
662 ret = clk_set_rate(clk, freq);
663 if (ret) {
664 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
665 ret);
666 goto restore_voltage;
667 }
668
669
670 if (freq < old_freq) {
671 ret = _set_opp_voltage(dev, reg, u_volt, u_volt_min,
672 u_volt_max);
673 if (ret)
674 goto restore_freq;
675 }
676
677 return 0;
678
679restore_freq:
680 if (clk_set_rate(clk, old_freq))
681 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
682 __func__, old_freq);
683restore_voltage:
684
685 if (!IS_ERR(old_opp))
686 _set_opp_voltage(dev, reg, ou_volt, ou_volt_min, ou_volt_max);
687
688 return ret;
689}
690EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
691
692
693static void _kfree_opp_dev_rcu(struct rcu_head *head)
694{
695 struct opp_device *opp_dev;
696
697 opp_dev = container_of(head, struct opp_device, rcu_head);
698 kfree_rcu(opp_dev, rcu_head);
699}
700
701static void _remove_opp_dev(struct opp_device *opp_dev,
702 struct opp_table *opp_table)
703{
704 opp_debug_unregister(opp_dev, opp_table);
705 list_del(&opp_dev->node);
706 call_srcu(&opp_table->srcu_head.srcu, &opp_dev->rcu_head,
707 _kfree_opp_dev_rcu);
708}
709
710struct opp_device *_add_opp_dev(const struct device *dev,
711 struct opp_table *opp_table)
712{
713 struct opp_device *opp_dev;
714 int ret;
715
716 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
717 if (!opp_dev)
718 return NULL;
719
720
721 opp_dev->dev = dev;
722 list_add_rcu(&opp_dev->node, &opp_table->dev_list);
723
724
725 ret = opp_debug_register(opp_dev, opp_table);
726 if (ret)
727 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
728 __func__, ret);
729
730 return opp_dev;
731}
732
733
734
735
736
737
738
739
740
741
742static struct opp_table *_add_opp_table(struct device *dev)
743{
744 struct opp_table *opp_table;
745 struct opp_device *opp_dev;
746 int ret;
747
748
749 opp_table = _find_opp_table(dev);
750 if (!IS_ERR(opp_table))
751 return opp_table;
752
753
754
755
756
757 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
758 if (!opp_table)
759 return NULL;
760
761 INIT_LIST_HEAD(&opp_table->dev_list);
762
763 opp_dev = _add_opp_dev(dev, opp_table);
764 if (!opp_dev) {
765 kfree(opp_table);
766 return NULL;
767 }
768
769 _of_init_opp_table(opp_table, dev);
770
771
772 opp_table->regulator = ERR_PTR(-ENXIO);
773
774
775 opp_table->clk = clk_get(dev, NULL);
776 if (IS_ERR(opp_table->clk)) {
777 ret = PTR_ERR(opp_table->clk);
778 if (ret != -EPROBE_DEFER)
779 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
780 ret);
781 }
782
783 srcu_init_notifier_head(&opp_table->srcu_head);
784 INIT_LIST_HEAD(&opp_table->opp_list);
785
786
787 list_add_rcu(&opp_table->node, &opp_tables);
788 return opp_table;
789}
790
791
792
793
794
795static void _kfree_device_rcu(struct rcu_head *head)
796{
797 struct opp_table *opp_table = container_of(head, struct opp_table,
798 rcu_head);
799
800 kfree_rcu(opp_table, rcu_head);
801}
802
803
804
805
806
807
808
809static void _remove_opp_table(struct opp_table *opp_table)
810{
811 struct opp_device *opp_dev;
812
813 if (!list_empty(&opp_table->opp_list))
814 return;
815
816 if (opp_table->supported_hw)
817 return;
818
819 if (opp_table->prop_name)
820 return;
821
822 if (!IS_ERR(opp_table->regulator))
823 return;
824
825
826 if (!IS_ERR(opp_table->clk))
827 clk_put(opp_table->clk);
828
829 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
830 node);
831
832 _remove_opp_dev(opp_dev, opp_table);
833
834
835 WARN_ON(!list_empty(&opp_table->dev_list));
836
837 list_del_rcu(&opp_table->node);
838 call_srcu(&opp_table->srcu_head.srcu, &opp_table->rcu_head,
839 _kfree_device_rcu);
840}
841
842
843
844
845
846static void _kfree_opp_rcu(struct rcu_head *head)
847{
848 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
849
850 kfree_rcu(opp, rcu_head);
851}
852
853
854
855
856
857
858
859
860
861
862
863
864
865void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp,
866 bool notify)
867{
868
869
870
871
872 if (notify)
873 srcu_notifier_call_chain(&opp_table->srcu_head,
874 OPP_EVENT_REMOVE, opp);
875 opp_debug_remove_one(opp);
876 list_del_rcu(&opp->node);
877 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
878
879 _remove_opp_table(opp_table);
880}
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895void dev_pm_opp_remove(struct device *dev, unsigned long freq)
896{
897 struct dev_pm_opp *opp;
898 struct opp_table *opp_table;
899 bool found = false;
900
901
902 mutex_lock(&opp_table_lock);
903
904 opp_table = _find_opp_table(dev);
905 if (IS_ERR(opp_table))
906 goto unlock;
907
908 list_for_each_entry(opp, &opp_table->opp_list, node) {
909 if (opp->rate == freq) {
910 found = true;
911 break;
912 }
913 }
914
915 if (!found) {
916 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
917 __func__, freq);
918 goto unlock;
919 }
920
921 _opp_remove(opp_table, opp, true);
922unlock:
923 mutex_unlock(&opp_table_lock);
924}
925EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
926
927struct dev_pm_opp *_allocate_opp(struct device *dev,
928 struct opp_table **opp_table)
929{
930 struct dev_pm_opp *opp;
931
932
933 opp = kzalloc(sizeof(*opp), GFP_KERNEL);
934 if (!opp)
935 return NULL;
936
937 INIT_LIST_HEAD(&opp->node);
938
939 *opp_table = _add_opp_table(dev);
940 if (!*opp_table) {
941 kfree(opp);
942 return NULL;
943 }
944
945 return opp;
946}
947
948static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
949 struct opp_table *opp_table)
950{
951 struct regulator *reg = opp_table->regulator;
952
953 if (!IS_ERR(reg) &&
954 !regulator_is_supported_voltage(reg, opp->u_volt_min,
955 opp->u_volt_max)) {
956 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
957 __func__, opp->u_volt_min, opp->u_volt_max);
958 return false;
959 }
960
961 return true;
962}
963
964int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
965 struct opp_table *opp_table)
966{
967 struct dev_pm_opp *opp;
968 struct list_head *head = &opp_table->opp_list;
969 int ret;
970
971
972
973
974
975
976
977
978
979 list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
980 if (new_opp->rate > opp->rate) {
981 head = &opp->node;
982 continue;
983 }
984
985 if (new_opp->rate < opp->rate)
986 break;
987
988
989 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
990 __func__, opp->rate, opp->u_volt, opp->available,
991 new_opp->rate, new_opp->u_volt, new_opp->available);
992
993 return opp->available && new_opp->u_volt == opp->u_volt ?
994 0 : -EEXIST;
995 }
996
997 new_opp->opp_table = opp_table;
998 list_add_rcu(&new_opp->node, head);
999
1000 ret = opp_debug_create_one(new_opp, opp_table);
1001 if (ret)
1002 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
1003 __func__, ret);
1004
1005 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
1006 new_opp->available = false;
1007 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1008 __func__, new_opp->rate);
1009 }
1010
1011 return 0;
1012}
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
1042 bool dynamic)
1043{
1044 struct opp_table *opp_table;
1045 struct dev_pm_opp *new_opp;
1046 unsigned long tol;
1047 int ret;
1048
1049
1050 mutex_lock(&opp_table_lock);
1051
1052 new_opp = _allocate_opp(dev, &opp_table);
1053 if (!new_opp) {
1054 ret = -ENOMEM;
1055 goto unlock;
1056 }
1057
1058
1059 new_opp->rate = freq;
1060 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
1061 new_opp->u_volt = u_volt;
1062 new_opp->u_volt_min = u_volt - tol;
1063 new_opp->u_volt_max = u_volt + tol;
1064 new_opp->available = true;
1065 new_opp->dynamic = dynamic;
1066
1067 ret = _opp_add(dev, new_opp, opp_table);
1068 if (ret)
1069 goto free_opp;
1070
1071 mutex_unlock(&opp_table_lock);
1072
1073
1074
1075
1076
1077 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);
1078 return 0;
1079
1080free_opp:
1081 _opp_remove(opp_table, new_opp, false);
1082unlock:
1083 mutex_unlock(&opp_table_lock);
1084 return ret;
1085}
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104int dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
1105 unsigned int count)
1106{
1107 struct opp_table *opp_table;
1108 int ret = 0;
1109
1110
1111 mutex_lock(&opp_table_lock);
1112
1113 opp_table = _add_opp_table(dev);
1114 if (!opp_table) {
1115 ret = -ENOMEM;
1116 goto unlock;
1117 }
1118
1119
1120 WARN_ON(!list_empty(&opp_table->opp_list));
1121
1122
1123 if (opp_table->supported_hw) {
1124 dev_err(dev, "%s: Already have supported hardware list\n",
1125 __func__);
1126 ret = -EBUSY;
1127 goto err;
1128 }
1129
1130 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
1131 GFP_KERNEL);
1132 if (!opp_table->supported_hw) {
1133 ret = -ENOMEM;
1134 goto err;
1135 }
1136
1137 opp_table->supported_hw_count = count;
1138 mutex_unlock(&opp_table_lock);
1139 return 0;
1140
1141err:
1142 _remove_opp_table(opp_table);
1143unlock:
1144 mutex_unlock(&opp_table_lock);
1145
1146 return ret;
1147}
1148EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164void dev_pm_opp_put_supported_hw(struct device *dev)
1165{
1166 struct opp_table *opp_table;
1167
1168
1169 mutex_lock(&opp_table_lock);
1170
1171
1172 opp_table = _find_opp_table(dev);
1173 if (IS_ERR(opp_table)) {
1174 dev_err(dev, "Failed to find opp_table: %ld\n",
1175 PTR_ERR(opp_table));
1176 goto unlock;
1177 }
1178
1179
1180 WARN_ON(!list_empty(&opp_table->opp_list));
1181
1182 if (!opp_table->supported_hw) {
1183 dev_err(dev, "%s: Doesn't have supported hardware list\n",
1184 __func__);
1185 goto unlock;
1186 }
1187
1188 kfree(opp_table->supported_hw);
1189 opp_table->supported_hw = NULL;
1190 opp_table->supported_hw_count = 0;
1191
1192
1193 _remove_opp_table(opp_table);
1194
1195unlock:
1196 mutex_unlock(&opp_table_lock);
1197}
1198EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216int dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1217{
1218 struct opp_table *opp_table;
1219 int ret = 0;
1220
1221
1222 mutex_lock(&opp_table_lock);
1223
1224 opp_table = _add_opp_table(dev);
1225 if (!opp_table) {
1226 ret = -ENOMEM;
1227 goto unlock;
1228 }
1229
1230
1231 WARN_ON(!list_empty(&opp_table->opp_list));
1232
1233
1234 if (opp_table->prop_name) {
1235 dev_err(dev, "%s: Already have prop-name %s\n", __func__,
1236 opp_table->prop_name);
1237 ret = -EBUSY;
1238 goto err;
1239 }
1240
1241 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1242 if (!opp_table->prop_name) {
1243 ret = -ENOMEM;
1244 goto err;
1245 }
1246
1247 mutex_unlock(&opp_table_lock);
1248 return 0;
1249
1250err:
1251 _remove_opp_table(opp_table);
1252unlock:
1253 mutex_unlock(&opp_table_lock);
1254
1255 return ret;
1256}
1257EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273void dev_pm_opp_put_prop_name(struct device *dev)
1274{
1275 struct opp_table *opp_table;
1276
1277
1278 mutex_lock(&opp_table_lock);
1279
1280
1281 opp_table = _find_opp_table(dev);
1282 if (IS_ERR(opp_table)) {
1283 dev_err(dev, "Failed to find opp_table: %ld\n",
1284 PTR_ERR(opp_table));
1285 goto unlock;
1286 }
1287
1288
1289 WARN_ON(!list_empty(&opp_table->opp_list));
1290
1291 if (!opp_table->prop_name) {
1292 dev_err(dev, "%s: Doesn't have a prop-name\n", __func__);
1293 goto unlock;
1294 }
1295
1296 kfree(opp_table->prop_name);
1297 opp_table->prop_name = NULL;
1298
1299
1300 _remove_opp_table(opp_table);
1301
1302unlock:
1303 mutex_unlock(&opp_table_lock);
1304}
1305EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323int dev_pm_opp_set_regulator(struct device *dev, const char *name)
1324{
1325 struct opp_table *opp_table;
1326 struct regulator *reg;
1327 int ret;
1328
1329 mutex_lock(&opp_table_lock);
1330
1331 opp_table = _add_opp_table(dev);
1332 if (!opp_table) {
1333 ret = -ENOMEM;
1334 goto unlock;
1335 }
1336
1337
1338 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1339 ret = -EBUSY;
1340 goto err;
1341 }
1342
1343
1344 if (WARN_ON(!IS_ERR(opp_table->regulator))) {
1345 ret = -EBUSY;
1346 goto err;
1347 }
1348
1349 reg = regulator_get_optional(dev, name);
1350 if (IS_ERR(reg)) {
1351 ret = PTR_ERR(reg);
1352 if (ret != -EPROBE_DEFER)
1353 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1354 __func__, name, ret);
1355 goto err;
1356 }
1357
1358 opp_table->regulator = reg;
1359
1360 mutex_unlock(&opp_table_lock);
1361 return 0;
1362
1363err:
1364 _remove_opp_table(opp_table);
1365unlock:
1366 mutex_unlock(&opp_table_lock);
1367
1368 return ret;
1369}
1370EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulator);
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382void dev_pm_opp_put_regulator(struct device *dev)
1383{
1384 struct opp_table *opp_table;
1385
1386 mutex_lock(&opp_table_lock);
1387
1388
1389 opp_table = _find_opp_table(dev);
1390 if (IS_ERR(opp_table)) {
1391 dev_err(dev, "Failed to find opp_table: %ld\n",
1392 PTR_ERR(opp_table));
1393 goto unlock;
1394 }
1395
1396 if (IS_ERR(opp_table->regulator)) {
1397 dev_err(dev, "%s: Doesn't have regulator set\n", __func__);
1398 goto unlock;
1399 }
1400
1401
1402 WARN_ON(!list_empty(&opp_table->opp_list));
1403
1404 regulator_put(opp_table->regulator);
1405 opp_table->regulator = ERR_PTR(-ENXIO);
1406
1407
1408 _remove_opp_table(opp_table);
1409
1410unlock:
1411 mutex_unlock(&opp_table_lock);
1412}
1413EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulator);
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1439{
1440 return _opp_add_v1(dev, freq, u_volt, true);
1441}
1442EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463static int _opp_set_availability(struct device *dev, unsigned long freq,
1464 bool availability_req)
1465{
1466 struct opp_table *opp_table;
1467 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
1468 int r = 0;
1469
1470
1471 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
1472 if (!new_opp)
1473 return -ENOMEM;
1474
1475 mutex_lock(&opp_table_lock);
1476
1477
1478 opp_table = _find_opp_table(dev);
1479 if (IS_ERR(opp_table)) {
1480 r = PTR_ERR(opp_table);
1481 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1482 goto unlock;
1483 }
1484
1485
1486 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
1487 if (tmp_opp->rate == freq) {
1488 opp = tmp_opp;
1489 break;
1490 }
1491 }
1492 if (IS_ERR(opp)) {
1493 r = PTR_ERR(opp);
1494 goto unlock;
1495 }
1496
1497
1498 if (opp->available == availability_req)
1499 goto unlock;
1500
1501 *new_opp = *opp;
1502
1503
1504 new_opp->available = availability_req;
1505
1506 list_replace_rcu(&opp->node, &new_opp->node);
1507 mutex_unlock(&opp_table_lock);
1508 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
1509
1510
1511 if (availability_req)
1512 srcu_notifier_call_chain(&opp_table->srcu_head,
1513 OPP_EVENT_ENABLE, new_opp);
1514 else
1515 srcu_notifier_call_chain(&opp_table->srcu_head,
1516 OPP_EVENT_DISABLE, new_opp);
1517
1518 return 0;
1519
1520unlock:
1521 mutex_unlock(&opp_table_lock);
1522 kfree(new_opp);
1523 return r;
1524}
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545int dev_pm_opp_enable(struct device *dev, unsigned long freq)
1546{
1547 return _opp_set_availability(dev, freq, true);
1548}
1549EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571int dev_pm_opp_disable(struct device *dev, unsigned long freq)
1572{
1573 return _opp_set_availability(dev, freq, false);
1574}
1575EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
1592{
1593 struct opp_table *opp_table = _find_opp_table(dev);
1594
1595 if (IS_ERR(opp_table))
1596 return ERR_CAST(opp_table);
1597
1598 return &opp_table->srcu_head;
1599}
1600EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
1601
1602
1603
1604
1605
1606void _dev_pm_opp_remove_table(struct device *dev, bool remove_all)
1607{
1608 struct opp_table *opp_table;
1609 struct dev_pm_opp *opp, *tmp;
1610
1611
1612 mutex_lock(&opp_table_lock);
1613
1614
1615 opp_table = _find_opp_table(dev);
1616 if (IS_ERR(opp_table)) {
1617 int error = PTR_ERR(opp_table);
1618
1619 if (error != -ENODEV)
1620 WARN(1, "%s: opp_table: %d\n",
1621 IS_ERR_OR_NULL(dev) ?
1622 "Invalid device" : dev_name(dev),
1623 error);
1624 goto unlock;
1625 }
1626
1627
1628 if (list_is_singular(&opp_table->dev_list)) {
1629
1630 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1631 if (remove_all || !opp->dynamic)
1632 _opp_remove(opp_table, opp, true);
1633 }
1634 } else {
1635 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
1636 }
1637
1638unlock:
1639 mutex_unlock(&opp_table_lock);
1640}
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655void dev_pm_opp_remove_table(struct device *dev)
1656{
1657 _dev_pm_opp_remove_table(dev, true);
1658}
1659EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
1660