GNU Linux-libre 4.19.286-gnu1
[releases.git] / arch / arm / mach-omap2 / pm.c
1 /*
2  * pm.c - Common OMAP2+ power management-related code
3  *
4  * Copyright (C) 2010 Texas Instruments, Inc.
5  * Copyright (C) 2010 Nokia Corporation
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/io.h>
15 #include <linux/err.h>
16 #include <linux/pm_opp.h>
17 #include <linux/export.h>
18 #include <linux/suspend.h>
19 #include <linux/clk.h>
20 #include <linux/cpu.h>
21
22 #include <asm/system_misc.h>
23
24 #include "omap_device.h"
25 #include "common.h"
26
27 #include "soc.h"
28 #include "prcm-common.h"
29 #include "voltage.h"
30 #include "powerdomain.h"
31 #include "clockdomain.h"
32 #include "pm.h"
33
34 #ifdef CONFIG_SUSPEND
35 /*
36  * omap_pm_suspend: points to a function that does the SoC-specific
37  * suspend work
38  */
39 static int (*omap_pm_suspend)(void);
40 #endif
41
42 #ifdef CONFIG_PM
43 /**
44  * struct omap2_oscillator - Describe the board main oscillator latencies
45  * @startup_time: oscillator startup latency
46  * @shutdown_time: oscillator shutdown latency
47  */
48 struct omap2_oscillator {
49         u32 startup_time;
50         u32 shutdown_time;
51 };
52
53 static struct omap2_oscillator oscillator = {
54         .startup_time = ULONG_MAX,
55         .shutdown_time = ULONG_MAX,
56 };
57
58 void omap_pm_setup_oscillator(u32 tstart, u32 tshut)
59 {
60         oscillator.startup_time = tstart;
61         oscillator.shutdown_time = tshut;
62 }
63
64 void omap_pm_get_oscillator(u32 *tstart, u32 *tshut)
65 {
66         if (!tstart || !tshut)
67                 return;
68
69         *tstart = oscillator.startup_time;
70         *tshut = oscillator.shutdown_time;
71 }
72 #endif
73
74 int omap_pm_clkdms_setup(struct clockdomain *clkdm, void *unused)
75 {
76         clkdm_allow_idle(clkdm);
77         return 0;
78 }
79
80 #ifdef CONFIG_SUSPEND
81 static int omap_pm_enter(suspend_state_t suspend_state)
82 {
83         int ret = 0;
84
85         if (!omap_pm_suspend)
86                 return -ENOENT; /* XXX doublecheck */
87
88         switch (suspend_state) {
89         case PM_SUSPEND_MEM:
90                 ret = omap_pm_suspend();
91                 break;
92         default:
93                 ret = -EINVAL;
94         }
95
96         return ret;
97 }
98
99 static int omap_pm_begin(suspend_state_t state)
100 {
101         cpu_idle_poll_ctrl(true);
102         if (soc_is_omap34xx())
103                 omap_prcm_irq_prepare();
104         return 0;
105 }
106
107 static void omap_pm_end(void)
108 {
109         cpu_idle_poll_ctrl(false);
110 }
111
112 static void omap_pm_wake(void)
113 {
114         if (soc_is_omap34xx())
115                 omap_prcm_irq_complete();
116 }
117
118 static const struct platform_suspend_ops omap_pm_ops = {
119         .begin          = omap_pm_begin,
120         .end            = omap_pm_end,
121         .enter          = omap_pm_enter,
122         .wake           = omap_pm_wake,
123         .valid          = suspend_valid_only_mem,
124 };
125
126 /**
127  * omap_common_suspend_init - Set common suspend routines for OMAP SoCs
128  * @pm_suspend: function pointer to SoC specific suspend function
129  */
130 void omap_common_suspend_init(void *pm_suspend)
131 {
132         omap_pm_suspend = pm_suspend;
133         suspend_set_ops(&omap_pm_ops);
134 }
135 #endif /* CONFIG_SUSPEND */
136
137 int __maybe_unused omap_pm_nop_init(void)
138 {
139         return 0;
140 }
141
142 int (*omap_pm_soc_init)(void);
143
144 int __init omap2_common_pm_late_init(void)
145 {
146         int error;
147
148         if (!omap_pm_soc_init)
149                 return 0;
150
151         /* Init the voltage layer */
152         omap3_twl_init();
153         omap4_twl_init();
154         omap_voltage_late_init();
155
156         /* Smartreflex device init */
157         omap_devinit_smartreflex();
158
159         error = omap_pm_soc_init();
160         if (error)
161                 pr_warn("%s: pm soc init failed: %i\n", __func__, error);
162
163         omap2_clk_enable_autoidle_all();
164
165         return 0;
166 }
167 omap_late_initcall(omap2_common_pm_late_init);