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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
427 unsigned long *freq)
428{
429 struct opp_table *opp_table;
430 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
431
432 opp_rcu_lockdep_assert();
433
434 if (!dev || !freq) {
435 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
436 return ERR_PTR(-EINVAL);
437 }
438
439 opp_table = _find_opp_table(dev);
440 if (IS_ERR(opp_table))
441 return ERR_CAST(opp_table);
442
443 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
444 if (temp_opp->available && temp_opp->rate >= *freq) {
445 opp = temp_opp;
446 *freq = opp->rate;
447 break;
448 }
449 }
450
451 return opp;
452}
453EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
477 unsigned long *freq)
478{
479 struct opp_table *opp_table;
480 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
481
482 opp_rcu_lockdep_assert();
483
484 if (!dev || !freq) {
485 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
486 return ERR_PTR(-EINVAL);
487 }
488
489 opp_table = _find_opp_table(dev);
490 if (IS_ERR(opp_table))
491 return ERR_CAST(opp_table);
492
493 list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) {
494 if (temp_opp->available) {
495
496 if (temp_opp->rate > *freq)
497 break;
498 else
499 opp = temp_opp;
500 }
501 }
502 if (!IS_ERR(opp))
503 *freq = opp->rate;
504
505 return opp;
506}
507EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
508
509
510
511
512
513static struct clk *_get_opp_clk(struct device *dev)
514{
515 struct opp_table *opp_table;
516 struct clk *clk;
517
518 rcu_read_lock();
519
520 opp_table = _find_opp_table(dev);
521 if (IS_ERR(opp_table)) {
522 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
523 clk = ERR_CAST(opp_table);
524 goto unlock;
525 }
526
527 clk = opp_table->clk;
528 if (IS_ERR(clk))
529 dev_err(dev, "%s: No clock available for the device\n",
530 __func__);
531
532unlock:
533 rcu_read_unlock();
534 return clk;
535}
536
537static int _set_opp_voltage(struct device *dev, struct regulator *reg,
538 unsigned long u_volt, unsigned long u_volt_min,
539 unsigned long u_volt_max)
540{
541 int ret;
542
543
544 if (IS_ERR(reg)) {
545 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
546 PTR_ERR(reg));
547 return 0;
548 }
549
550 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, u_volt_min,
551 u_volt, u_volt_max);
552
553 ret = regulator_set_voltage_triplet(reg, u_volt_min, u_volt,
554 u_volt_max);
555 if (ret)
556 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
557 __func__, u_volt_min, u_volt, u_volt_max, ret);
558
559 return ret;
560}
561
562
563
564
565
566
567
568
569
570
571
572int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
573{
574 struct opp_table *opp_table;
575 struct dev_pm_opp *old_opp, *opp;
576 struct regulator *reg;
577 struct clk *clk;
578 unsigned long freq, old_freq;
579 unsigned long u_volt, u_volt_min, u_volt_max;
580 unsigned long ou_volt, ou_volt_min, ou_volt_max;
581 int ret;
582
583 if (unlikely(!target_freq)) {
584 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
585 target_freq);
586 return -EINVAL;
587 }
588
589 clk = _get_opp_clk(dev);
590 if (IS_ERR(clk))
591 return PTR_ERR(clk);
592
593 freq = clk_round_rate(clk, target_freq);
594 if ((long)freq <= 0)
595 freq = target_freq;
596
597 old_freq = clk_get_rate(clk);
598
599
600 if (old_freq == freq) {
601 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
602 __func__, freq);
603 return 0;
604 }
605
606 rcu_read_lock();
607
608 opp_table = _find_opp_table(dev);
609 if (IS_ERR(opp_table)) {
610 dev_err(dev, "%s: device opp doesn't exist\n", __func__);
611 rcu_read_unlock();
612 return PTR_ERR(opp_table);
613 }
614
615 old_opp = dev_pm_opp_find_freq_ceil(dev, &old_freq);
616 if (!IS_ERR(old_opp)) {
617 ou_volt = old_opp->u_volt;
618 ou_volt_min = old_opp->u_volt_min;
619 ou_volt_max = old_opp->u_volt_max;
620 } else {
621 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
622 __func__, old_freq, PTR_ERR(old_opp));
623 }
624
625 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
626 if (IS_ERR(opp)) {
627 ret = PTR_ERR(opp);
628 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
629 __func__, freq, ret);
630 rcu_read_unlock();
631 return ret;
632 }
633
634 u_volt = opp->u_volt;
635 u_volt_min = opp->u_volt_min;
636 u_volt_max = opp->u_volt_max;
637
638 reg = opp_table->regulator;
639
640 rcu_read_unlock();
641
642
643 if (freq > old_freq) {
644 ret = _set_opp_voltage(dev, reg, u_volt, u_volt_min,
645 u_volt_max);
646 if (ret)
647 goto restore_voltage;
648 }
649
650
651
652 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n",
653 __func__, old_freq, freq);
654
655 ret = clk_set_rate(clk, freq);
656 if (ret) {
657 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
658 ret);
659 goto restore_voltage;
660 }
661
662
663 if (freq < old_freq) {
664 ret = _set_opp_voltage(dev, reg, u_volt, u_volt_min,
665 u_volt_max);
666 if (ret)
667 goto restore_freq;
668 }
669
670 return 0;
671
672restore_freq:
673 if (clk_set_rate(clk, old_freq))
674 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
675 __func__, old_freq);
676restore_voltage:
677
678 if (!IS_ERR(old_opp))
679 _set_opp_voltage(dev, reg, ou_volt, ou_volt_min, ou_volt_max);
680
681 return ret;
682}
683EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
684
685
686static void _kfree_opp_dev_rcu(struct rcu_head *head)
687{
688 struct opp_device *opp_dev;
689
690 opp_dev = container_of(head, struct opp_device, rcu_head);
691 kfree_rcu(opp_dev, rcu_head);
692}
693
694static void _remove_opp_dev(struct opp_device *opp_dev,
695 struct opp_table *opp_table)
696{
697 opp_debug_unregister(opp_dev, opp_table);
698 list_del(&opp_dev->node);
699 call_srcu(&opp_table->srcu_head.srcu, &opp_dev->rcu_head,
700 _kfree_opp_dev_rcu);
701}
702
703struct opp_device *_add_opp_dev(const struct device *dev,
704 struct opp_table *opp_table)
705{
706 struct opp_device *opp_dev;
707 int ret;
708
709 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
710 if (!opp_dev)
711 return NULL;
712
713
714 opp_dev->dev = dev;
715 list_add_rcu(&opp_dev->node, &opp_table->dev_list);
716
717
718 ret = opp_debug_register(opp_dev, opp_table);
719 if (ret)
720 dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
721 __func__, ret);
722
723 return opp_dev;
724}
725
726
727
728
729
730
731
732
733
734
735static struct opp_table *_add_opp_table(struct device *dev)
736{
737 struct opp_table *opp_table;
738 struct opp_device *opp_dev;
739 int ret;
740
741
742 opp_table = _find_opp_table(dev);
743 if (!IS_ERR(opp_table))
744 return opp_table;
745
746
747
748
749
750 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
751 if (!opp_table)
752 return NULL;
753
754 INIT_LIST_HEAD(&opp_table->dev_list);
755
756 opp_dev = _add_opp_dev(dev, opp_table);
757 if (!opp_dev) {
758 kfree(opp_table);
759 return NULL;
760 }
761
762 _of_init_opp_table(opp_table, dev);
763
764
765 opp_table->regulator = ERR_PTR(-ENXIO);
766
767
768 opp_table->clk = clk_get(dev, NULL);
769 if (IS_ERR(opp_table->clk)) {
770 ret = PTR_ERR(opp_table->clk);
771 if (ret != -EPROBE_DEFER)
772 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
773 ret);
774 }
775
776 srcu_init_notifier_head(&opp_table->srcu_head);
777 INIT_LIST_HEAD(&opp_table->opp_list);
778
779
780 list_add_rcu(&opp_table->node, &opp_tables);
781 return opp_table;
782}
783
784
785
786
787
788static void _kfree_device_rcu(struct rcu_head *head)
789{
790 struct opp_table *opp_table = container_of(head, struct opp_table,
791 rcu_head);
792
793 kfree_rcu(opp_table, rcu_head);
794}
795
796
797
798
799
800
801
802static void _remove_opp_table(struct opp_table *opp_table)
803{
804 struct opp_device *opp_dev;
805
806 if (!list_empty(&opp_table->opp_list))
807 return;
808
809 if (opp_table->supported_hw)
810 return;
811
812 if (opp_table->prop_name)
813 return;
814
815 if (!IS_ERR(opp_table->regulator))
816 return;
817
818
819 if (!IS_ERR(opp_table->clk))
820 clk_put(opp_table->clk);
821
822 opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
823 node);
824
825 _remove_opp_dev(opp_dev, opp_table);
826
827
828 WARN_ON(!list_empty(&opp_table->dev_list));
829
830 list_del_rcu(&opp_table->node);
831 call_srcu(&opp_table->srcu_head.srcu, &opp_table->rcu_head,
832 _kfree_device_rcu);
833}
834
835
836
837
838
839static void _kfree_opp_rcu(struct rcu_head *head)
840{
841 struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head);
842
843 kfree_rcu(opp, rcu_head);
844}
845
846
847
848
849
850
851
852
853
854
855
856
857
858void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp,
859 bool notify)
860{
861
862
863
864
865 if (notify)
866 srcu_notifier_call_chain(&opp_table->srcu_head,
867 OPP_EVENT_REMOVE, opp);
868 opp_debug_remove_one(opp);
869 list_del_rcu(&opp->node);
870 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
871
872 _remove_opp_table(opp_table);
873}
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888void dev_pm_opp_remove(struct device *dev, unsigned long freq)
889{
890 struct dev_pm_opp *opp;
891 struct opp_table *opp_table;
892 bool found = false;
893
894
895 mutex_lock(&opp_table_lock);
896
897 opp_table = _find_opp_table(dev);
898 if (IS_ERR(opp_table))
899 goto unlock;
900
901 list_for_each_entry(opp, &opp_table->opp_list, node) {
902 if (opp->rate == freq) {
903 found = true;
904 break;
905 }
906 }
907
908 if (!found) {
909 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
910 __func__, freq);
911 goto unlock;
912 }
913
914 _opp_remove(opp_table, opp, true);
915unlock:
916 mutex_unlock(&opp_table_lock);
917}
918EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
919
920struct dev_pm_opp *_allocate_opp(struct device *dev,
921 struct opp_table **opp_table)
922{
923 struct dev_pm_opp *opp;
924
925
926 opp = kzalloc(sizeof(*opp), GFP_KERNEL);
927 if (!opp)
928 return NULL;
929
930 INIT_LIST_HEAD(&opp->node);
931
932 *opp_table = _add_opp_table(dev);
933 if (!*opp_table) {
934 kfree(opp);
935 return NULL;
936 }
937
938 return opp;
939}
940
941static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
942 struct opp_table *opp_table)
943{
944 struct regulator *reg = opp_table->regulator;
945
946 if (!IS_ERR(reg) &&
947 !regulator_is_supported_voltage(reg, opp->u_volt_min,
948 opp->u_volt_max)) {
949 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
950 __func__, opp->u_volt_min, opp->u_volt_max);
951 return false;
952 }
953
954 return true;
955}
956
957int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
958 struct opp_table *opp_table)
959{
960 struct dev_pm_opp *opp;
961 struct list_head *head = &opp_table->opp_list;
962 int ret;
963
964
965
966
967
968
969
970
971
972 list_for_each_entry_rcu(opp, &opp_table->opp_list, node) {
973 if (new_opp->rate > opp->rate) {
974 head = &opp->node;
975 continue;
976 }
977
978 if (new_opp->rate < opp->rate)
979 break;
980
981
982 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
983 __func__, opp->rate, opp->u_volt, opp->available,
984 new_opp->rate, new_opp->u_volt, new_opp->available);
985
986 return opp->available && new_opp->u_volt == opp->u_volt ?
987 0 : -EEXIST;
988 }
989
990 new_opp->opp_table = opp_table;
991 list_add_rcu(&new_opp->node, head);
992
993 ret = opp_debug_create_one(new_opp, opp_table);
994 if (ret)
995 dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
996 __func__, ret);
997
998 if (!_opp_supported_by_regulators(new_opp, opp_table)) {
999 new_opp->available = false;
1000 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
1001 __func__, new_opp->rate);
1002 }
1003
1004 return 0;
1005}
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt,
1035 bool dynamic)
1036{
1037 struct opp_table *opp_table;
1038 struct dev_pm_opp *new_opp;
1039 unsigned long tol;
1040 int ret;
1041
1042
1043 mutex_lock(&opp_table_lock);
1044
1045 new_opp = _allocate_opp(dev, &opp_table);
1046 if (!new_opp) {
1047 ret = -ENOMEM;
1048 goto unlock;
1049 }
1050
1051
1052 new_opp->rate = freq;
1053 tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
1054 new_opp->u_volt = u_volt;
1055 new_opp->u_volt_min = u_volt - tol;
1056 new_opp->u_volt_max = u_volt + tol;
1057 new_opp->available = true;
1058 new_opp->dynamic = dynamic;
1059
1060 ret = _opp_add(dev, new_opp, opp_table);
1061 if (ret)
1062 goto free_opp;
1063
1064 mutex_unlock(&opp_table_lock);
1065
1066
1067
1068
1069
1070 srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp);
1071 return 0;
1072
1073free_opp:
1074 _opp_remove(opp_table, new_opp, false);
1075unlock:
1076 mutex_unlock(&opp_table_lock);
1077 return ret;
1078}
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097int dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions,
1098 unsigned int count)
1099{
1100 struct opp_table *opp_table;
1101 int ret = 0;
1102
1103
1104 mutex_lock(&opp_table_lock);
1105
1106 opp_table = _add_opp_table(dev);
1107 if (!opp_table) {
1108 ret = -ENOMEM;
1109 goto unlock;
1110 }
1111
1112
1113 WARN_ON(!list_empty(&opp_table->opp_list));
1114
1115
1116 if (opp_table->supported_hw) {
1117 dev_err(dev, "%s: Already have supported hardware list\n",
1118 __func__);
1119 ret = -EBUSY;
1120 goto err;
1121 }
1122
1123 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
1124 GFP_KERNEL);
1125 if (!opp_table->supported_hw) {
1126 ret = -ENOMEM;
1127 goto err;
1128 }
1129
1130 opp_table->supported_hw_count = count;
1131 mutex_unlock(&opp_table_lock);
1132 return 0;
1133
1134err:
1135 _remove_opp_table(opp_table);
1136unlock:
1137 mutex_unlock(&opp_table_lock);
1138
1139 return ret;
1140}
1141EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157void dev_pm_opp_put_supported_hw(struct device *dev)
1158{
1159 struct opp_table *opp_table;
1160
1161
1162 mutex_lock(&opp_table_lock);
1163
1164
1165 opp_table = _find_opp_table(dev);
1166 if (IS_ERR(opp_table)) {
1167 dev_err(dev, "Failed to find opp_table: %ld\n",
1168 PTR_ERR(opp_table));
1169 goto unlock;
1170 }
1171
1172
1173 WARN_ON(!list_empty(&opp_table->opp_list));
1174
1175 if (!opp_table->supported_hw) {
1176 dev_err(dev, "%s: Doesn't have supported hardware list\n",
1177 __func__);
1178 goto unlock;
1179 }
1180
1181 kfree(opp_table->supported_hw);
1182 opp_table->supported_hw = NULL;
1183 opp_table->supported_hw_count = 0;
1184
1185
1186 _remove_opp_table(opp_table);
1187
1188unlock:
1189 mutex_unlock(&opp_table_lock);
1190}
1191EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209int dev_pm_opp_set_prop_name(struct device *dev, const char *name)
1210{
1211 struct opp_table *opp_table;
1212 int ret = 0;
1213
1214
1215 mutex_lock(&opp_table_lock);
1216
1217 opp_table = _add_opp_table(dev);
1218 if (!opp_table) {
1219 ret = -ENOMEM;
1220 goto unlock;
1221 }
1222
1223
1224 WARN_ON(!list_empty(&opp_table->opp_list));
1225
1226
1227 if (opp_table->prop_name) {
1228 dev_err(dev, "%s: Already have prop-name %s\n", __func__,
1229 opp_table->prop_name);
1230 ret = -EBUSY;
1231 goto err;
1232 }
1233
1234 opp_table->prop_name = kstrdup(name, GFP_KERNEL);
1235 if (!opp_table->prop_name) {
1236 ret = -ENOMEM;
1237 goto err;
1238 }
1239
1240 mutex_unlock(&opp_table_lock);
1241 return 0;
1242
1243err:
1244 _remove_opp_table(opp_table);
1245unlock:
1246 mutex_unlock(&opp_table_lock);
1247
1248 return ret;
1249}
1250EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266void dev_pm_opp_put_prop_name(struct device *dev)
1267{
1268 struct opp_table *opp_table;
1269
1270
1271 mutex_lock(&opp_table_lock);
1272
1273
1274 opp_table = _find_opp_table(dev);
1275 if (IS_ERR(opp_table)) {
1276 dev_err(dev, "Failed to find opp_table: %ld\n",
1277 PTR_ERR(opp_table));
1278 goto unlock;
1279 }
1280
1281
1282 WARN_ON(!list_empty(&opp_table->opp_list));
1283
1284 if (!opp_table->prop_name) {
1285 dev_err(dev, "%s: Doesn't have a prop-name\n", __func__);
1286 goto unlock;
1287 }
1288
1289 kfree(opp_table->prop_name);
1290 opp_table->prop_name = NULL;
1291
1292
1293 _remove_opp_table(opp_table);
1294
1295unlock:
1296 mutex_unlock(&opp_table_lock);
1297}
1298EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316int dev_pm_opp_set_regulator(struct device *dev, const char *name)
1317{
1318 struct opp_table *opp_table;
1319 struct regulator *reg;
1320 int ret;
1321
1322 mutex_lock(&opp_table_lock);
1323
1324 opp_table = _add_opp_table(dev);
1325 if (!opp_table) {
1326 ret = -ENOMEM;
1327 goto unlock;
1328 }
1329
1330
1331 if (WARN_ON(!list_empty(&opp_table->opp_list))) {
1332 ret = -EBUSY;
1333 goto err;
1334 }
1335
1336
1337 if (WARN_ON(!IS_ERR(opp_table->regulator))) {
1338 ret = -EBUSY;
1339 goto err;
1340 }
1341
1342 reg = regulator_get_optional(dev, name);
1343 if (IS_ERR(reg)) {
1344 ret = PTR_ERR(reg);
1345 if (ret != -EPROBE_DEFER)
1346 dev_err(dev, "%s: no regulator (%s) found: %d\n",
1347 __func__, name, ret);
1348 goto err;
1349 }
1350
1351 opp_table->regulator = reg;
1352
1353 mutex_unlock(&opp_table_lock);
1354 return 0;
1355
1356err:
1357 _remove_opp_table(opp_table);
1358unlock:
1359 mutex_unlock(&opp_table_lock);
1360
1361 return ret;
1362}
1363EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulator);
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375void dev_pm_opp_put_regulator(struct device *dev)
1376{
1377 struct opp_table *opp_table;
1378
1379 mutex_lock(&opp_table_lock);
1380
1381
1382 opp_table = _find_opp_table(dev);
1383 if (IS_ERR(opp_table)) {
1384 dev_err(dev, "Failed to find opp_table: %ld\n",
1385 PTR_ERR(opp_table));
1386 goto unlock;
1387 }
1388
1389 if (IS_ERR(opp_table->regulator)) {
1390 dev_err(dev, "%s: Doesn't have regulator set\n", __func__);
1391 goto unlock;
1392 }
1393
1394
1395 WARN_ON(!list_empty(&opp_table->opp_list));
1396
1397 regulator_put(opp_table->regulator);
1398 opp_table->regulator = ERR_PTR(-ENXIO);
1399
1400
1401 _remove_opp_table(opp_table);
1402
1403unlock:
1404 mutex_unlock(&opp_table_lock);
1405}
1406EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulator);
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
1432{
1433 return _opp_add_v1(dev, freq, u_volt, true);
1434}
1435EXPORT_SYMBOL_GPL(dev_pm_opp_add);
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456static int _opp_set_availability(struct device *dev, unsigned long freq,
1457 bool availability_req)
1458{
1459 struct opp_table *opp_table;
1460 struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV);
1461 int r = 0;
1462
1463
1464 new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL);
1465 if (!new_opp)
1466 return -ENOMEM;
1467
1468 mutex_lock(&opp_table_lock);
1469
1470
1471 opp_table = _find_opp_table(dev);
1472 if (IS_ERR(opp_table)) {
1473 r = PTR_ERR(opp_table);
1474 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
1475 goto unlock;
1476 }
1477
1478
1479 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
1480 if (tmp_opp->rate == freq) {
1481 opp = tmp_opp;
1482 break;
1483 }
1484 }
1485 if (IS_ERR(opp)) {
1486 r = PTR_ERR(opp);
1487 goto unlock;
1488 }
1489
1490
1491 if (opp->available == availability_req)
1492 goto unlock;
1493
1494 *new_opp = *opp;
1495
1496
1497 new_opp->available = availability_req;
1498
1499 list_replace_rcu(&opp->node, &new_opp->node);
1500 mutex_unlock(&opp_table_lock);
1501 call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu);
1502
1503
1504 if (availability_req)
1505 srcu_notifier_call_chain(&opp_table->srcu_head,
1506 OPP_EVENT_ENABLE, new_opp);
1507 else
1508 srcu_notifier_call_chain(&opp_table->srcu_head,
1509 OPP_EVENT_DISABLE, new_opp);
1510
1511 return 0;
1512
1513unlock:
1514 mutex_unlock(&opp_table_lock);
1515 kfree(new_opp);
1516 return r;
1517}
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538int dev_pm_opp_enable(struct device *dev, unsigned long freq)
1539{
1540 return _opp_set_availability(dev, freq, true);
1541}
1542EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564int dev_pm_opp_disable(struct device *dev, unsigned long freq)
1565{
1566 return _opp_set_availability(dev, freq, false);
1567}
1568EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev)
1585{
1586 struct opp_table *opp_table = _find_opp_table(dev);
1587
1588 if (IS_ERR(opp_table))
1589 return ERR_CAST(opp_table);
1590
1591 return &opp_table->srcu_head;
1592}
1593EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier);
1594
1595
1596
1597
1598
1599void _dev_pm_opp_remove_table(struct device *dev, bool remove_all)
1600{
1601 struct opp_table *opp_table;
1602 struct dev_pm_opp *opp, *tmp;
1603
1604
1605 mutex_lock(&opp_table_lock);
1606
1607
1608 opp_table = _find_opp_table(dev);
1609 if (IS_ERR(opp_table)) {
1610 int error = PTR_ERR(opp_table);
1611
1612 if (error != -ENODEV)
1613 WARN(1, "%s: opp_table: %d\n",
1614 IS_ERR_OR_NULL(dev) ?
1615 "Invalid device" : dev_name(dev),
1616 error);
1617 goto unlock;
1618 }
1619
1620
1621 if (list_is_singular(&opp_table->dev_list)) {
1622
1623 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
1624 if (remove_all || !opp->dynamic)
1625 _opp_remove(opp_table, opp, true);
1626 }
1627 } else {
1628 _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
1629 }
1630
1631unlock:
1632 mutex_unlock(&opp_table_lock);
1633}
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648void dev_pm_opp_remove_table(struct device *dev)
1649{
1650 _dev_pm_opp_remove_table(dev, true);
1651}
1652EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
1653