1
2
3
4
5
6
7
8
9
10
11
12#undef DEBUG
13
14#include <linux/cpu_pm.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/list.h>
18#include <linux/errno.h>
19#include <linux/string.h>
20#include <linux/spinlock.h>
21#include <trace/events/power.h>
22
23#include "cm2xxx_3xxx.h"
24#include "prcm44xx.h"
25#include "cm44xx.h"
26#include "prm2xxx_3xxx.h"
27#include "prm44xx.h"
28
29#include <asm/cpu.h>
30
31#include "powerdomain.h"
32#include "clockdomain.h"
33#include "voltage.h"
34
35#include "soc.h"
36#include "pm.h"
37
38#define PWRDM_TRACE_STATES_FLAG (1<<31)
39
40void pwrdms_save_context(void);
41void pwrdms_restore_context(void);
42
43enum {
44 PWRDM_STATE_NOW = 0,
45 PWRDM_STATE_PREV,
46};
47
48
49
50
51
52
53
54#define ALREADYACTIVE_SWITCH 0
55#define FORCEWAKEUP_SWITCH 1
56#define LOWPOWERSTATE_SWITCH 2
57
58
59static LIST_HEAD(pwrdm_list);
60
61static struct pwrdm_ops *arch_pwrdm;
62
63
64
65static struct powerdomain *_pwrdm_lookup(const char *name)
66{
67 struct powerdomain *pwrdm, *temp_pwrdm;
68
69 pwrdm = NULL;
70
71 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
72 if (!strcmp(name, temp_pwrdm->name)) {
73 pwrdm = temp_pwrdm;
74 break;
75 }
76 }
77
78 return pwrdm;
79}
80
81
82
83
84
85
86
87
88
89static int _pwrdm_register(struct powerdomain *pwrdm)
90{
91 int i;
92 struct voltagedomain *voltdm;
93
94 if (!pwrdm || !pwrdm->name)
95 return -EINVAL;
96
97 if (cpu_is_omap44xx() &&
98 pwrdm->prcm_partition == OMAP4430_INVALID_PRCM_PARTITION) {
99 pr_err("powerdomain: %s: missing OMAP4 PRCM partition ID\n",
100 pwrdm->name);
101 return -EINVAL;
102 }
103
104 if (_pwrdm_lookup(pwrdm->name))
105 return -EEXIST;
106
107 if (arch_pwrdm && arch_pwrdm->pwrdm_has_voltdm)
108 if (!arch_pwrdm->pwrdm_has_voltdm())
109 goto skip_voltdm;
110
111 voltdm = voltdm_lookup(pwrdm->voltdm.name);
112 if (!voltdm) {
113 pr_err("powerdomain: %s: voltagedomain %s does not exist\n",
114 pwrdm->name, pwrdm->voltdm.name);
115 return -EINVAL;
116 }
117 pwrdm->voltdm.ptr = voltdm;
118 INIT_LIST_HEAD(&pwrdm->voltdm_node);
119skip_voltdm:
120 spin_lock_init(&pwrdm->_lock);
121
122 list_add(&pwrdm->node, &pwrdm_list);
123
124
125 for (i = 0; i < PWRDM_MAX_PWRSTS; i++)
126 pwrdm->state_counter[i] = 0;
127
128 pwrdm->ret_logic_off_counter = 0;
129 for (i = 0; i < pwrdm->banks; i++)
130 pwrdm->ret_mem_off_counter[i] = 0;
131
132 if (arch_pwrdm && arch_pwrdm->pwrdm_wait_transition)
133 arch_pwrdm->pwrdm_wait_transition(pwrdm);
134 pwrdm->state = pwrdm_read_pwrst(pwrdm);
135 pwrdm->state_counter[pwrdm->state] = 1;
136
137 pr_debug("powerdomain: registered %s\n", pwrdm->name);
138
139 return 0;
140}
141
142static void _update_logic_membank_counters(struct powerdomain *pwrdm)
143{
144 int i;
145 u8 prev_logic_pwrst, prev_mem_pwrst;
146
147 prev_logic_pwrst = pwrdm_read_prev_logic_pwrst(pwrdm);
148 if ((pwrdm->pwrsts_logic_ret == PWRSTS_OFF_RET) &&
149 (prev_logic_pwrst == PWRDM_POWER_OFF))
150 pwrdm->ret_logic_off_counter++;
151
152 for (i = 0; i < pwrdm->banks; i++) {
153 prev_mem_pwrst = pwrdm_read_prev_mem_pwrst(pwrdm, i);
154
155 if ((pwrdm->pwrsts_mem_ret[i] == PWRSTS_OFF_RET) &&
156 (prev_mem_pwrst == PWRDM_POWER_OFF))
157 pwrdm->ret_mem_off_counter[i]++;
158 }
159}
160
161static int _pwrdm_state_switch(struct powerdomain *pwrdm, int flag)
162{
163
164 int prev, next, state, trace_state = 0;
165
166 if (pwrdm == NULL)
167 return -EINVAL;
168
169 state = pwrdm_read_pwrst(pwrdm);
170
171 switch (flag) {
172 case PWRDM_STATE_NOW:
173 prev = pwrdm->state;
174 break;
175 case PWRDM_STATE_PREV:
176 prev = pwrdm_read_prev_pwrst(pwrdm);
177 if (pwrdm->state != prev)
178 pwrdm->state_counter[prev]++;
179 if (prev == PWRDM_POWER_RET)
180 _update_logic_membank_counters(pwrdm);
181
182
183
184
185 next = pwrdm_read_next_pwrst(pwrdm);
186 if (next != prev) {
187 trace_state = (PWRDM_TRACE_STATES_FLAG |
188 ((next & OMAP_POWERSTATE_MASK) << 8) |
189 ((prev & OMAP_POWERSTATE_MASK) << 0));
190 trace_power_domain_target_rcuidle(pwrdm->name,
191 trace_state,
192 raw_smp_processor_id());
193 }
194 break;
195 default:
196 return -EINVAL;
197 }
198
199 if (state != prev)
200 pwrdm->state_counter[state]++;
201
202 pm_dbg_update_time(pwrdm, prev);
203
204 pwrdm->state = state;
205
206 return 0;
207}
208
209static int _pwrdm_pre_transition_cb(struct powerdomain *pwrdm, void *unused)
210{
211 pwrdm_clear_all_prev_pwrst(pwrdm);
212 _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
213 return 0;
214}
215
216static int _pwrdm_post_transition_cb(struct powerdomain *pwrdm, void *unused)
217{
218 _pwrdm_state_switch(pwrdm, PWRDM_STATE_PREV);
219 return 0;
220}
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236static u8 _pwrdm_save_clkdm_state_and_activate(struct powerdomain *pwrdm,
237 u8 curr_pwrst, u8 pwrst)
238{
239 u8 sleep_switch;
240
241 if (curr_pwrst < PWRDM_POWER_ON) {
242 if (curr_pwrst > pwrst &&
243 pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE &&
244 arch_pwrdm->pwrdm_set_lowpwrstchange) {
245 sleep_switch = LOWPOWERSTATE_SWITCH;
246 } else {
247 clkdm_deny_idle_nolock(pwrdm->pwrdm_clkdms[0]);
248 sleep_switch = FORCEWAKEUP_SWITCH;
249 }
250 } else {
251 sleep_switch = ALREADYACTIVE_SWITCH;
252 }
253
254 return sleep_switch;
255}
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270static void _pwrdm_restore_clkdm_state(struct powerdomain *pwrdm,
271 u8 sleep_switch)
272{
273 switch (sleep_switch) {
274 case FORCEWAKEUP_SWITCH:
275 clkdm_allow_idle_nolock(pwrdm->pwrdm_clkdms[0]);
276 break;
277 case LOWPOWERSTATE_SWITCH:
278 if (pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE &&
279 arch_pwrdm->pwrdm_set_lowpwrstchange)
280 arch_pwrdm->pwrdm_set_lowpwrstchange(pwrdm);
281 pwrdm_state_switch_nolock(pwrdm);
282 break;
283 }
284}
285
286
287
288
289
290
291
292
293
294
295
296
297
298int pwrdm_register_platform_funcs(struct pwrdm_ops *po)
299{
300 if (!po)
301 return -EINVAL;
302
303 if (arch_pwrdm)
304 return -EEXIST;
305
306 arch_pwrdm = po;
307
308 return 0;
309}
310
311
312
313
314
315
316
317
318
319
320
321int pwrdm_register_pwrdms(struct powerdomain **ps)
322{
323 struct powerdomain **p = NULL;
324
325 if (!arch_pwrdm)
326 return -EEXIST;
327
328 if (!ps)
329 return -EINVAL;
330
331 for (p = ps; *p; p++)
332 _pwrdm_register(*p);
333
334 return 0;
335}
336
337static int cpu_notifier(struct notifier_block *nb, unsigned long cmd, void *v)
338{
339 switch (cmd) {
340 case CPU_CLUSTER_PM_ENTER:
341 if (enable_off_mode)
342 pwrdms_save_context();
343 break;
344 case CPU_CLUSTER_PM_EXIT:
345 if (enable_off_mode)
346 pwrdms_restore_context();
347 break;
348 }
349
350 return NOTIFY_OK;
351}
352
353
354
355
356
357
358
359
360
361
362
363
364int pwrdm_complete_init(void)
365{
366 struct powerdomain *temp_p;
367 static struct notifier_block nb;
368
369 if (list_empty(&pwrdm_list))
370 return -EACCES;
371
372 list_for_each_entry(temp_p, &pwrdm_list, node)
373 pwrdm_set_next_pwrst(temp_p, PWRDM_POWER_ON);
374
375
376 if (soc_is_am43xx()) {
377 nb.notifier_call = cpu_notifier;
378 cpu_pm_register_notifier(&nb);
379 }
380
381 return 0;
382}
383
384
385
386
387
388
389
390void pwrdm_lock(struct powerdomain *pwrdm)
391 __acquires(&pwrdm->_lock)
392{
393 spin_lock_irqsave(&pwrdm->_lock, pwrdm->_lock_flags);
394}
395
396
397
398
399
400
401
402void pwrdm_unlock(struct powerdomain *pwrdm)
403 __releases(&pwrdm->_lock)
404{
405 spin_unlock_irqrestore(&pwrdm->_lock, pwrdm->_lock_flags);
406}
407
408
409
410
411
412
413
414
415struct powerdomain *pwrdm_lookup(const char *name)
416{
417 struct powerdomain *pwrdm;
418
419 if (!name)
420 return NULL;
421
422 pwrdm = _pwrdm_lookup(name);
423
424 return pwrdm;
425}
426
427
428
429
430
431
432
433
434
435
436
437int pwrdm_for_each(int (*fn)(struct powerdomain *pwrdm, void *user),
438 void *user)
439{
440 struct powerdomain *temp_pwrdm;
441 int ret = 0;
442
443 if (!fn)
444 return -EINVAL;
445
446 list_for_each_entry(temp_pwrdm, &pwrdm_list, node) {
447 ret = (*fn)(temp_pwrdm, user);
448 if (ret)
449 break;
450 }
451
452 return ret;
453}
454
455
456
457
458
459
460
461
462
463
464
465int pwrdm_add_clkdm(struct powerdomain *pwrdm, struct clockdomain *clkdm)
466{
467 int i;
468 int ret = -EINVAL;
469
470 if (!pwrdm || !clkdm)
471 return -EINVAL;
472
473 pr_debug("powerdomain: %s: associating clockdomain %s\n",
474 pwrdm->name, clkdm->name);
475
476 for (i = 0; i < PWRDM_MAX_CLKDMS; i++) {
477 if (!pwrdm->pwrdm_clkdms[i])
478 break;
479#ifdef DEBUG
480 if (pwrdm->pwrdm_clkdms[i] == clkdm) {
481 ret = -EINVAL;
482 goto pac_exit;
483 }
484#endif
485 }
486
487 if (i == PWRDM_MAX_CLKDMS) {
488 pr_debug("powerdomain: %s: increase PWRDM_MAX_CLKDMS for clkdm %s\n",
489 pwrdm->name, clkdm->name);
490 WARN_ON(1);
491 ret = -ENOMEM;
492 goto pac_exit;
493 }
494
495 pwrdm->pwrdm_clkdms[i] = clkdm;
496
497 ret = 0;
498
499pac_exit:
500 return ret;
501}
502
503
504
505
506
507
508
509
510int pwrdm_get_mem_bank_count(struct powerdomain *pwrdm)
511{
512 if (!pwrdm)
513 return -EINVAL;
514
515 return pwrdm->banks;
516}
517
518
519
520
521
522
523
524
525
526
527
528
529int pwrdm_set_next_pwrst(struct powerdomain *pwrdm, u8 pwrst)
530{
531 int ret = -EINVAL;
532
533 if (!pwrdm)
534 return -EINVAL;
535
536 if (!(pwrdm->pwrsts & (1 << pwrst)))
537 return -EINVAL;
538
539 pr_debug("powerdomain: %s: setting next powerstate to %0x\n",
540 pwrdm->name, pwrst);
541
542 if (arch_pwrdm && arch_pwrdm->pwrdm_set_next_pwrst) {
543
544 trace_power_domain_target_rcuidle(pwrdm->name, pwrst,
545 raw_smp_processor_id());
546
547 ret = arch_pwrdm->pwrdm_set_next_pwrst(pwrdm, pwrst);
548 }
549
550 return ret;
551}
552
553
554
555
556
557
558
559
560
561int pwrdm_read_next_pwrst(struct powerdomain *pwrdm)
562{
563 int ret = -EINVAL;
564
565 if (!pwrdm)
566 return -EINVAL;
567
568 if (arch_pwrdm && arch_pwrdm->pwrdm_read_next_pwrst)
569 ret = arch_pwrdm->pwrdm_read_next_pwrst(pwrdm);
570
571 return ret;
572}
573
574
575
576
577
578
579
580
581
582
583int pwrdm_read_pwrst(struct powerdomain *pwrdm)
584{
585 int ret = -EINVAL;
586
587 if (!pwrdm)
588 return -EINVAL;
589
590 if (pwrdm->pwrsts == PWRSTS_ON)
591 return PWRDM_POWER_ON;
592
593 if (arch_pwrdm && arch_pwrdm->pwrdm_read_pwrst)
594 ret = arch_pwrdm->pwrdm_read_pwrst(pwrdm);
595
596 return ret;
597}
598
599
600
601
602
603
604
605
606
607int pwrdm_read_prev_pwrst(struct powerdomain *pwrdm)
608{
609 int ret = -EINVAL;
610
611 if (!pwrdm)
612 return -EINVAL;
613
614 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_pwrst)
615 ret = arch_pwrdm->pwrdm_read_prev_pwrst(pwrdm);
616
617 return ret;
618}
619
620
621
622
623
624
625
626
627
628
629
630
631int pwrdm_set_logic_retst(struct powerdomain *pwrdm, u8 pwrst)
632{
633 int ret = -EINVAL;
634
635 if (!pwrdm)
636 return -EINVAL;
637
638 if (!(pwrdm->pwrsts_logic_ret & (1 << pwrst)))
639 return -EINVAL;
640
641 pr_debug("powerdomain: %s: setting next logic powerstate to %0x\n",
642 pwrdm->name, pwrst);
643
644 if (arch_pwrdm && arch_pwrdm->pwrdm_set_logic_retst)
645 ret = arch_pwrdm->pwrdm_set_logic_retst(pwrdm, pwrst);
646
647 return ret;
648}
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665int pwrdm_set_mem_onst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
666{
667 int ret = -EINVAL;
668
669 if (!pwrdm)
670 return -EINVAL;
671
672 if (pwrdm->banks < (bank + 1))
673 return -EEXIST;
674
675 if (!(pwrdm->pwrsts_mem_on[bank] & (1 << pwrst)))
676 return -EINVAL;
677
678 pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-ON to %0x\n",
679 pwrdm->name, bank, pwrst);
680
681 if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_onst)
682 ret = arch_pwrdm->pwrdm_set_mem_onst(pwrdm, bank, pwrst);
683
684 return ret;
685}
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703int pwrdm_set_mem_retst(struct powerdomain *pwrdm, u8 bank, u8 pwrst)
704{
705 int ret = -EINVAL;
706
707 if (!pwrdm)
708 return -EINVAL;
709
710 if (pwrdm->banks < (bank + 1))
711 return -EEXIST;
712
713 if (!(pwrdm->pwrsts_mem_ret[bank] & (1 << pwrst)))
714 return -EINVAL;
715
716 pr_debug("powerdomain: %s: setting next memory powerstate for bank %0x while pwrdm-RET to %0x\n",
717 pwrdm->name, bank, pwrst);
718
719 if (arch_pwrdm && arch_pwrdm->pwrdm_set_mem_retst)
720 ret = arch_pwrdm->pwrdm_set_mem_retst(pwrdm, bank, pwrst);
721
722 return ret;
723}
724
725
726
727
728
729
730
731
732
733
734int pwrdm_read_logic_pwrst(struct powerdomain *pwrdm)
735{
736 int ret = -EINVAL;
737
738 if (!pwrdm)
739 return -EINVAL;
740
741 if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_pwrst)
742 ret = arch_pwrdm->pwrdm_read_logic_pwrst(pwrdm);
743
744 return ret;
745}
746
747
748
749
750
751
752
753
754
755int pwrdm_read_prev_logic_pwrst(struct powerdomain *pwrdm)
756{
757 int ret = -EINVAL;
758
759 if (!pwrdm)
760 return -EINVAL;
761
762 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_logic_pwrst)
763 ret = arch_pwrdm->pwrdm_read_prev_logic_pwrst(pwrdm);
764
765 return ret;
766}
767
768
769
770
771
772
773
774
775
776int pwrdm_read_logic_retst(struct powerdomain *pwrdm)
777{
778 int ret = -EINVAL;
779
780 if (!pwrdm)
781 return -EINVAL;
782
783 if (arch_pwrdm && arch_pwrdm->pwrdm_read_logic_retst)
784 ret = arch_pwrdm->pwrdm_read_logic_retst(pwrdm);
785
786 return ret;
787}
788
789
790
791
792
793
794
795
796
797
798
799int pwrdm_read_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
800{
801 int ret = -EINVAL;
802
803 if (!pwrdm)
804 return ret;
805
806 if (pwrdm->banks < (bank + 1))
807 return ret;
808
809 if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
810 bank = 1;
811
812 if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_pwrst)
813 ret = arch_pwrdm->pwrdm_read_mem_pwrst(pwrdm, bank);
814
815 return ret;
816}
817
818
819
820
821
822
823
824
825
826
827
828
829int pwrdm_read_prev_mem_pwrst(struct powerdomain *pwrdm, u8 bank)
830{
831 int ret = -EINVAL;
832
833 if (!pwrdm)
834 return ret;
835
836 if (pwrdm->banks < (bank + 1))
837 return ret;
838
839 if (pwrdm->flags & PWRDM_HAS_MPU_QUIRK)
840 bank = 1;
841
842 if (arch_pwrdm && arch_pwrdm->pwrdm_read_prev_mem_pwrst)
843 ret = arch_pwrdm->pwrdm_read_prev_mem_pwrst(pwrdm, bank);
844
845 return ret;
846}
847
848
849
850
851
852
853
854
855
856
857
858int pwrdm_read_mem_retst(struct powerdomain *pwrdm, u8 bank)
859{
860 int ret = -EINVAL;
861
862 if (!pwrdm)
863 return ret;
864
865 if (pwrdm->banks < (bank + 1))
866 return ret;
867
868 if (arch_pwrdm && arch_pwrdm->pwrdm_read_mem_retst)
869 ret = arch_pwrdm->pwrdm_read_mem_retst(pwrdm, bank);
870
871 return ret;
872}
873
874
875
876
877
878
879
880
881
882
883int pwrdm_clear_all_prev_pwrst(struct powerdomain *pwrdm)
884{
885 int ret = -EINVAL;
886
887 if (!pwrdm)
888 return ret;
889
890
891
892
893
894
895 pr_debug("powerdomain: %s: clearing previous power state reg\n",
896 pwrdm->name);
897
898 if (arch_pwrdm && arch_pwrdm->pwrdm_clear_all_prev_pwrst)
899 ret = arch_pwrdm->pwrdm_clear_all_prev_pwrst(pwrdm);
900
901 return ret;
902}
903
904
905
906
907
908
909
910
911
912
913
914
915int pwrdm_enable_hdwr_sar(struct powerdomain *pwrdm)
916{
917 int ret = -EINVAL;
918
919 if (!pwrdm)
920 return ret;
921
922 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
923 return ret;
924
925 pr_debug("powerdomain: %s: setting SAVEANDRESTORE bit\n", pwrdm->name);
926
927 if (arch_pwrdm && arch_pwrdm->pwrdm_enable_hdwr_sar)
928 ret = arch_pwrdm->pwrdm_enable_hdwr_sar(pwrdm);
929
930 return ret;
931}
932
933
934
935
936
937
938
939
940
941
942
943
944int pwrdm_disable_hdwr_sar(struct powerdomain *pwrdm)
945{
946 int ret = -EINVAL;
947
948 if (!pwrdm)
949 return ret;
950
951 if (!(pwrdm->flags & PWRDM_HAS_HDWR_SAR))
952 return ret;
953
954 pr_debug("powerdomain: %s: clearing SAVEANDRESTORE bit\n", pwrdm->name);
955
956 if (arch_pwrdm && arch_pwrdm->pwrdm_disable_hdwr_sar)
957 ret = arch_pwrdm->pwrdm_disable_hdwr_sar(pwrdm);
958
959 return ret;
960}
961
962
963
964
965
966
967
968
969bool pwrdm_has_hdwr_sar(struct powerdomain *pwrdm)
970{
971 return (pwrdm && pwrdm->flags & PWRDM_HAS_HDWR_SAR) ? 1 : 0;
972}
973
974int pwrdm_state_switch_nolock(struct powerdomain *pwrdm)
975{
976 int ret;
977
978 if (!pwrdm || !arch_pwrdm)
979 return -EINVAL;
980
981 ret = arch_pwrdm->pwrdm_wait_transition(pwrdm);
982 if (!ret)
983 ret = _pwrdm_state_switch(pwrdm, PWRDM_STATE_NOW);
984
985 return ret;
986}
987
988int __deprecated pwrdm_state_switch(struct powerdomain *pwrdm)
989{
990 int ret;
991
992 pwrdm_lock(pwrdm);
993 ret = pwrdm_state_switch_nolock(pwrdm);
994 pwrdm_unlock(pwrdm);
995
996 return ret;
997}
998
999int pwrdm_pre_transition(struct powerdomain *pwrdm)
1000{
1001 if (pwrdm)
1002 _pwrdm_pre_transition_cb(pwrdm, NULL);
1003 else
1004 pwrdm_for_each(_pwrdm_pre_transition_cb, NULL);
1005
1006 return 0;
1007}
1008
1009int pwrdm_post_transition(struct powerdomain *pwrdm)
1010{
1011 if (pwrdm)
1012 _pwrdm_post_transition_cb(pwrdm, NULL);
1013 else
1014 pwrdm_for_each(_pwrdm_post_transition_cb, NULL);
1015
1016 return 0;
1017}
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042u8 pwrdm_get_valid_lp_state(struct powerdomain *pwrdm,
1043 bool is_logic_state, u8 req_state)
1044{
1045 u8 pwrdm_states = is_logic_state ? pwrdm->pwrsts_logic_ret :
1046 pwrdm->pwrsts;
1047
1048 u8 default_pwrst = is_logic_state ? PWRDM_POWER_RET : PWRDM_POWER_ON;
1049 u8 new_pwrst;
1050 bool found;
1051
1052
1053 if (pwrdm_states & BIT(req_state))
1054 return req_state;
1055
1056 if (!req_state)
1057 goto up_search;
1058
1059
1060
1061
1062
1063 new_pwrst = req_state - 1;
1064 found = true;
1065 while (!(pwrdm_states & BIT(new_pwrst))) {
1066
1067 if (new_pwrst == PWRDM_POWER_OFF) {
1068 found = false;
1069 break;
1070 }
1071 new_pwrst--;
1072 }
1073
1074 if (found)
1075 goto done;
1076
1077up_search:
1078
1079 new_pwrst = req_state + 1;
1080 while (!(pwrdm_states & BIT(new_pwrst))) {
1081 if (new_pwrst > PWRDM_POWER_ON) {
1082 WARN(1, "powerdomain: %s: Fix max powerstate to ON\n",
1083 pwrdm->name);
1084 return PWRDM_POWER_ON;
1085 }
1086
1087 if (new_pwrst == default_pwrst)
1088 break;
1089 new_pwrst++;
1090 }
1091done:
1092 return new_pwrst;
1093}
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108int omap_set_pwrdm_state(struct powerdomain *pwrdm, u8 pwrst)
1109{
1110 u8 next_pwrst, sleep_switch;
1111 int curr_pwrst;
1112 int ret = 0;
1113
1114 if (!pwrdm || IS_ERR(pwrdm))
1115 return -EINVAL;
1116
1117 while (!(pwrdm->pwrsts & (1 << pwrst))) {
1118 if (pwrst == PWRDM_POWER_OFF)
1119 return ret;
1120 pwrst--;
1121 }
1122
1123 pwrdm_lock(pwrdm);
1124
1125 curr_pwrst = pwrdm_read_pwrst(pwrdm);
1126 if (curr_pwrst < 0) {
1127 ret = -EINVAL;
1128 goto osps_out;
1129 }
1130
1131 next_pwrst = pwrdm_read_next_pwrst(pwrdm);
1132 if (curr_pwrst == pwrst && next_pwrst == pwrst)
1133 goto osps_out;
1134
1135 sleep_switch = _pwrdm_save_clkdm_state_and_activate(pwrdm, curr_pwrst,
1136 pwrst);
1137
1138 ret = pwrdm_set_next_pwrst(pwrdm, pwrst);
1139 if (ret)
1140 pr_err("%s: unable to set power state of powerdomain: %s\n",
1141 __func__, pwrdm->name);
1142
1143 _pwrdm_restore_clkdm_state(pwrdm, sleep_switch);
1144
1145osps_out:
1146 pwrdm_unlock(pwrdm);
1147
1148 return ret;
1149}
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159int pwrdm_get_context_loss_count(struct powerdomain *pwrdm)
1160{
1161 int i, count;
1162
1163 if (!pwrdm) {
1164 WARN(1, "powerdomain: %s: pwrdm is null\n", __func__);
1165 return -ENODEV;
1166 }
1167
1168 count = pwrdm->state_counter[PWRDM_POWER_OFF];
1169 count += pwrdm->ret_logic_off_counter;
1170
1171 for (i = 0; i < pwrdm->banks; i++)
1172 count += pwrdm->ret_mem_off_counter[i];
1173
1174
1175
1176
1177
1178 count &= INT_MAX;
1179
1180 pr_debug("powerdomain: %s: context loss count = %d\n",
1181 pwrdm->name, count);
1182
1183 return count;
1184}
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198bool pwrdm_can_ever_lose_context(struct powerdomain *pwrdm)
1199{
1200 int i;
1201
1202 if (!pwrdm) {
1203 pr_debug("powerdomain: %s: invalid powerdomain pointer\n",
1204 __func__);
1205 return 1;
1206 }
1207
1208 if (pwrdm->pwrsts & PWRSTS_OFF)
1209 return 1;
1210
1211 if (pwrdm->pwrsts & PWRSTS_RET) {
1212 if (pwrdm->pwrsts_logic_ret & PWRSTS_OFF)
1213 return 1;
1214
1215 for (i = 0; i < pwrdm->banks; i++)
1216 if (pwrdm->pwrsts_mem_ret[i] & PWRSTS_OFF)
1217 return 1;
1218 }
1219
1220 for (i = 0; i < pwrdm->banks; i++)
1221 if (pwrdm->pwrsts_mem_on[i] & PWRSTS_OFF)
1222 return 1;
1223
1224 return 0;
1225}
1226
1227
1228
1229
1230
1231
1232
1233static int pwrdm_save_context(struct powerdomain *pwrdm, void *unused)
1234{
1235 if (arch_pwrdm && arch_pwrdm->pwrdm_save_context)
1236 arch_pwrdm->pwrdm_save_context(pwrdm);
1237 return 0;
1238}
1239
1240
1241
1242
1243
1244
1245
1246static int pwrdm_restore_context(struct powerdomain *pwrdm, void *unused)
1247{
1248 if (arch_pwrdm && arch_pwrdm->pwrdm_restore_context)
1249 arch_pwrdm->pwrdm_restore_context(pwrdm);
1250 return 0;
1251}
1252
1253static int pwrdm_lost_power(struct powerdomain *pwrdm, void *unused)
1254{
1255 int state;
1256
1257
1258
1259
1260
1261
1262 state = pwrdm_read_pwrst(pwrdm);
1263 if (state != PWRDM_POWER_OFF) {
1264 pwrdm->state_counter[state]++;
1265 pwrdm->state_counter[PWRDM_POWER_OFF]++;
1266 }
1267 pwrdm->state = state;
1268
1269 return 0;
1270}
1271
1272void pwrdms_save_context(void)
1273{
1274 pwrdm_for_each(pwrdm_save_context, NULL);
1275}
1276
1277void pwrdms_restore_context(void)
1278{
1279 pwrdm_for_each(pwrdm_restore_context, NULL);
1280}
1281
1282void pwrdms_lost_power(void)
1283{
1284 pwrdm_for_each(pwrdm_lost_power, NULL);
1285}
1286