blob: 1fde1bf53fb65b4092f727f7e5fe246f49f02b5c [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Kevin Hilman6f88e9b2010-07-26 16:34:31 -06002/*
3 * pm.c - Common OMAP2+ power management-related code
4 *
5 * Copyright (C) 2010 Texas Instruments, Inc.
6 * Copyright (C) 2010 Nokia Corporation
Kevin Hilman6f88e9b2010-07-26 16:34:31 -06007 */
8
9#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/io.h>
12#include <linux/err.h>
Nishanth Menone4db1c72013-09-19 16:03:52 -050013#include <linux/pm_opp.h>
Paul Gortmakerdc280942011-07-31 16:17:29 -040014#include <linux/export.h>
Paul Walmsley14164082012-02-02 02:30:50 -070015#include <linux/suspend.h>
Tony Lindgren44773ba2018-04-16 10:22:01 -070016#include <linux/clk.h>
Kevin Hilman24d7b402012-09-06 14:03:08 -070017#include <linux/cpu.h>
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060018
Govindraj.R335aece2012-03-29 09:30:28 -070019#include <asm/system_misc.h>
20
Tony Lindgren25c7d492012-10-02 17:25:48 -070021#include "omap_device.h"
Tony Lindgren4e653312011-11-10 22:45:17 +010022#include "common.h"
Kevin Hilman6f88e9b2010-07-26 16:34:31 -060023
Tony Lindgrene4c060d2012-10-05 13:25:59 -070024#include "soc.h"
Paul Walmsley14164082012-02-02 02:30:50 -070025#include "prcm-common.h"
Paul Walmsleye1d6f472011-02-25 15:54:33 -070026#include "voltage.h"
Paul Walmsley72e06d02010-12-21 21:05:16 -070027#include "powerdomain.h"
Paul Walmsley1540f2142010-12-21 21:05:15 -070028#include "clockdomain.h"
Thara Gopinath0c0a5d62010-05-29 22:02:23 +053029#include "pm.h"
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +053030
Dave Gerlach2e4b62d2014-05-12 13:33:21 -050031#ifdef CONFIG_SUSPEND
Paul Walmsley14164082012-02-02 02:30:50 -070032/*
33 * omap_pm_suspend: points to a function that does the SoC-specific
34 * suspend work
35 */
Dave Gerlach2e4b62d2014-05-12 13:33:21 -050036static int (*omap_pm_suspend)(void);
37#endif
Paul Walmsley14164082012-02-02 02:30:50 -070038
Kevin Hilman74d29162012-11-14 17:13:04 -080039#ifdef CONFIG_PM
Tero Kristo908b75e2012-09-25 19:33:39 +030040/**
41 * struct omap2_oscillator - Describe the board main oscillator latencies
42 * @startup_time: oscillator startup latency
43 * @shutdown_time: oscillator shutdown latency
44 */
45struct omap2_oscillator {
46 u32 startup_time;
47 u32 shutdown_time;
48};
49
50static struct omap2_oscillator oscillator = {
51 .startup_time = ULONG_MAX,
52 .shutdown_time = ULONG_MAX,
53};
54
55void omap_pm_setup_oscillator(u32 tstart, u32 tshut)
56{
57 oscillator.startup_time = tstart;
58 oscillator.shutdown_time = tshut;
59}
60
61void omap_pm_get_oscillator(u32 *tstart, u32 *tshut)
62{
63 if (!tstart || !tshut)
64 return;
65
66 *tstart = oscillator.startup_time;
67 *tshut = oscillator.shutdown_time;
68}
Kevin Hilman74d29162012-11-14 17:13:04 -080069#endif
Tero Kristo908b75e2012-09-25 19:33:39 +030070
Dave Gerlach33d38422017-03-28 20:57:56 -050071int omap_pm_clkdms_setup(struct clockdomain *clkdm, void *unused)
Paul Walmsley92206fd2012-02-02 02:38:50 -070072{
Tero Kristo1d9a5422016-06-30 16:15:02 +030073 clkdm_allow_idle(clkdm);
Paul Walmsley92206fd2012-02-02 02:38:50 -070074 return 0;
75}
76
Santosh Shilimkareb6a2c72010-09-15 01:04:01 +053077/*
Johan Hovold1e2d2df2011-08-30 18:48:16 +020078 * This API is to be called during init to set the various voltage
Thara Gopinath1482d8b2010-05-29 22:02:25 +053079 * domains to the voltage as per the opp table. Typically we boot up
80 * at the nominal voltage. So this function finds out the rate of
81 * the clock associated with the voltage domain, finds out the correct
Johan Hovold1e2d2df2011-08-30 18:48:16 +020082 * opp entry and sets the voltage domain to the voltage specified
Thara Gopinath1482d8b2010-05-29 22:02:25 +053083 * in the opp entry
84 */
85static int __init omap2_set_init_voltage(char *vdd_name, char *clk_name,
Benoit Cousson0f7aa002011-08-16 15:02:20 +020086 const char *oh_name)
Thara Gopinath1482d8b2010-05-29 22:02:25 +053087{
88 struct voltagedomain *voltdm;
89 struct clk *clk;
Nishanth Menon47d43ba2013-09-19 16:03:51 -050090 struct dev_pm_opp *opp;
Thara Gopinath1482d8b2010-05-29 22:02:25 +053091 unsigned long freq, bootup_volt;
Benoit Cousson0f7aa002011-08-16 15:02:20 +020092 struct device *dev;
Thara Gopinath1482d8b2010-05-29 22:02:25 +053093
Benoit Cousson0f7aa002011-08-16 15:02:20 +020094 if (!vdd_name || !clk_name || !oh_name) {
Johan Hovolde9a51902011-08-30 18:48:17 +020095 pr_err("%s: invalid parameters\n", __func__);
Thara Gopinath1482d8b2010-05-29 22:02:25 +053096 goto exit;
97 }
98
Kevin Hilman24d7b402012-09-06 14:03:08 -070099 if (!strncmp(oh_name, "mpu", 3))
100 /*
101 * All current OMAPs share voltage rail and clock
102 * source, so CPU0 is used to represent the MPU-SS.
103 */
104 dev = get_cpu_device(0);
105 else
106 dev = omap_device_get_by_hwmod_name(oh_name);
107
Benoit Cousson0f7aa002011-08-16 15:02:20 +0200108 if (IS_ERR(dev)) {
109 pr_err("%s: Unable to get dev pointer for hwmod %s\n",
110 __func__, oh_name);
111 goto exit;
112 }
113
Kevin Hilman81a60482011-03-16 14:25:45 -0700114 voltdm = voltdm_lookup(vdd_name);
Wei Yongjun93b44be2012-09-27 13:54:36 +0800115 if (!voltdm) {
Johan Hovolde9a51902011-08-30 18:48:17 +0200116 pr_err("%s: unable to get vdd pointer for vdd_%s\n",
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530117 __func__, vdd_name);
118 goto exit;
119 }
120
121 clk = clk_get(NULL, clk_name);
122 if (IS_ERR(clk)) {
Johan Hovolde9a51902011-08-30 18:48:17 +0200123 pr_err("%s: unable to get clk %s\n", __func__, clk_name);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530124 goto exit;
125 }
126
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600127 freq = clk_get_rate(clk);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530128 clk_put(clk);
129
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500130 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530131 if (IS_ERR(opp)) {
Johan Hovolde9a51902011-08-30 18:48:17 +0200132 pr_err("%s: unable to find boot up OPP for vdd_%s\n",
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530133 __func__, vdd_name);
134 goto exit;
135 }
136
Nishanth Menon5d4879c2013-09-19 16:03:50 -0500137 bootup_volt = dev_pm_opp_get_voltage(opp);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530138 dev_pm_opp_put(opp);
139
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530140 if (!bootup_volt) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600141 pr_err("%s: unable to find voltage corresponding to the bootup OPP for vdd_%s\n",
142 __func__, vdd_name);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530143 goto exit;
144 }
145
Kevin Hilman5e5651b2011-04-05 16:27:21 -0700146 voltdm_scale(voltdm, bootup_volt);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530147 return 0;
148
149exit:
Johan Hovolde9a51902011-08-30 18:48:17 +0200150 pr_err("%s: unable to set vdd_%s\n", __func__, vdd_name);
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530151 return -EINVAL;
152}
153
Paul Walmsley14164082012-02-02 02:30:50 -0700154#ifdef CONFIG_SUSPEND
155static int omap_pm_enter(suspend_state_t suspend_state)
156{
157 int ret = 0;
158
159 if (!omap_pm_suspend)
160 return -ENOENT; /* XXX doublecheck */
161
162 switch (suspend_state) {
Paul Walmsley14164082012-02-02 02:30:50 -0700163 case PM_SUSPEND_MEM:
164 ret = omap_pm_suspend();
165 break;
166 default:
167 ret = -EINVAL;
168 }
169
170 return ret;
171}
172
173static int omap_pm_begin(suspend_state_t state)
174{
Thomas Gleixnerf7b861b2013-03-21 22:49:38 +0100175 cpu_idle_poll_ctrl(true);
Tony Lindgrencb6675d2016-10-17 00:08:40 -0700176 if (soc_is_omap34xx())
Paul Walmsley14164082012-02-02 02:30:50 -0700177 omap_prcm_irq_prepare();
178 return 0;
179}
180
181static void omap_pm_end(void)
182{
Thomas Gleixnerf7b861b2013-03-21 22:49:38 +0100183 cpu_idle_poll_ctrl(false);
Paul Walmsley14164082012-02-02 02:30:50 -0700184}
185
Tony Lindgrend3be6d22018-02-09 08:15:53 -0800186static void omap_pm_wake(void)
Paul Walmsley14164082012-02-02 02:30:50 -0700187{
Tony Lindgrencb6675d2016-10-17 00:08:40 -0700188 if (soc_is_omap34xx())
Paul Walmsley14164082012-02-02 02:30:50 -0700189 omap_prcm_irq_complete();
190}
191
192static const struct platform_suspend_ops omap_pm_ops = {
193 .begin = omap_pm_begin,
194 .end = omap_pm_end,
195 .enter = omap_pm_enter,
Tony Lindgrend3be6d22018-02-09 08:15:53 -0800196 .wake = omap_pm_wake,
Paul Walmsley14164082012-02-02 02:30:50 -0700197 .valid = suspend_valid_only_mem,
198};
199
Dave Gerlach2e4b62d2014-05-12 13:33:21 -0500200/**
201 * omap_common_suspend_init - Set common suspend routines for OMAP SoCs
202 * @pm_suspend: function pointer to SoC specific suspend function
203 */
204void omap_common_suspend_init(void *pm_suspend)
205{
206 omap_pm_suspend = pm_suspend;
207 suspend_set_ops(&omap_pm_ops);
208}
Paul Walmsley14164082012-02-02 02:30:50 -0700209#endif /* CONFIG_SUSPEND */
210
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530211static void __init omap3_init_voltages(void)
212{
Tony Lindgrencb6675d2016-10-17 00:08:40 -0700213 if (!soc_is_omap34xx())
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530214 return;
215
Benoit Cousson0f7aa002011-08-16 15:02:20 +0200216 omap2_set_init_voltage("mpu_iva", "dpll1_ck", "mpu");
217 omap2_set_init_voltage("core", "l3_ick", "l3_main");
Thara Gopinath1482d8b2010-05-29 22:02:25 +0530218}
219
Thara Gopinath1376ee12010-05-29 22:02:25 +0530220static void __init omap4_init_voltages(void)
221{
Tony Lindgrencb6675d2016-10-17 00:08:40 -0700222 if (!soc_is_omap44xx())
Thara Gopinath1376ee12010-05-29 22:02:25 +0530223 return;
224
Benoit Cousson0f7aa002011-08-16 15:02:20 +0200225 omap2_set_init_voltage("mpu", "dpll_mpu_ck", "mpu");
226 omap2_set_init_voltage("core", "l3_div_ck", "l3_main_1");
227 omap2_set_init_voltage("iva", "dpll_iva_m5x2_ck", "iva");
Thara Gopinath1376ee12010-05-29 22:02:25 +0530228}
229
Tony Lindgren02b83dc2018-04-16 10:23:46 -0700230int __maybe_unused omap_pm_nop_init(void)
231{
232 return 0;
233}
234
235int (*omap_pm_soc_init)(void);
236
Shawn Guobbd707a2012-04-26 16:06:50 +0800237int __init omap2_common_pm_late_init(void)
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530238{
Tony Lindgren02b83dc2018-04-16 10:23:46 -0700239 int error;
240
241 if (!omap_pm_soc_init)
242 return 0;
243
Tony Lindgren2ee5f522014-05-05 17:27:37 -0700244 /* Init the voltage layer */
Tony Lindgrencb6675d2016-10-17 00:08:40 -0700245 omap3_twl_init();
246 omap4_twl_init();
Tony Lindgren2ee5f522014-05-05 17:27:37 -0700247 omap_voltage_late_init();
248
249 /* Initialize the voltages */
250 omap3_init_voltages();
251 omap4_init_voltages();
252
253 /* Smartreflex device init */
254 omap_devinit_smartreflex();
255
Tony Lindgren02b83dc2018-04-16 10:23:46 -0700256 error = omap_pm_soc_init();
257 if (error)
258 pr_warn("%s: pm soc init failed: %i\n", __func__, error);
259
260 omap2_clk_enable_autoidle_all();
261
Thara Gopinath2f34ce82010-05-29 22:02:21 +0530262 return 0;
263}
Tony Lindgren02b83dc2018-04-16 10:23:46 -0700264omap_late_initcall(omap2_common_pm_late_init);