blob: a6a5d807cc2bae5a1b2cc4fbbc7c39136fe12e20 [file] [log] [blame]
Thomas Abraham91cfbd42012-01-27 15:25:00 +09001/*
2 * Exynos Generic power domain support.
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
6 *
7 * Implementation of Exynos specific power domain control which is used in
8 * conjunction with runtime-pm. Support for both device-tree and non-device-tree
9 * based power domain support is included.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14*/
15
16#include <linux/io.h>
17#include <linux/err.h>
18#include <linux/slab.h>
19#include <linux/pm_domain.h>
Prathyush Kc7605692014-07-11 08:02:15 +090020#include <linux/clk.h>
Thomas Abraham91cfbd42012-01-27 15:25:00 +090021#include <linux/delay.h>
22#include <linux/of_address.h>
Tomasz Figa8a65d232012-11-22 00:21:17 +090023#include <linux/of_platform.h>
24#include <linux/sched.h>
Thomas Abraham91cfbd42012-01-27 15:25:00 +090025
Prathyush Kc7605692014-07-11 08:02:15 +090026#define MAX_CLK_PER_DOMAIN 4
27
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +020028struct exynos_pm_domain_config {
29 /* Value for LOCAL_PWR_CFG and STATUS fields for each domain */
30 u32 local_pwr_cfg;
31};
32
Thomas Abraham91cfbd42012-01-27 15:25:00 +090033/*
34 * Exynos specific wrapper around the generic power domain
35 */
36struct exynos_pm_domain {
37 void __iomem *base;
Thomas Abraham91cfbd42012-01-27 15:25:00 +090038 bool is_off;
39 struct generic_pm_domain pd;
Prathyush Kc7605692014-07-11 08:02:15 +090040 struct clk *oscclk;
41 struct clk *clk[MAX_CLK_PER_DOMAIN];
42 struct clk *pclk[MAX_CLK_PER_DOMAIN];
Andrzej Hajda528eae62015-03-18 02:12:23 +090043 struct clk *asb_clk[MAX_CLK_PER_DOMAIN];
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +020044 u32 local_pwr_cfg;
Thomas Abraham91cfbd42012-01-27 15:25:00 +090045};
46
47static int exynos_pd_power(struct generic_pm_domain *domain, bool power_on)
48{
49 struct exynos_pm_domain *pd;
50 void __iomem *base;
51 u32 timeout, pwr;
52 char *op;
Andrzej Hajda528eae62015-03-18 02:12:23 +090053 int i;
Thomas Abraham91cfbd42012-01-27 15:25:00 +090054
55 pd = container_of(domain, struct exynos_pm_domain, pd);
56 base = pd->base;
57
Andrzej Hajda528eae62015-03-18 02:12:23 +090058 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
59 if (IS_ERR(pd->asb_clk[i]))
60 break;
61 clk_prepare_enable(pd->asb_clk[i]);
62 }
63
Prathyush Kc7605692014-07-11 08:02:15 +090064 /* Set oscclk before powering off a domain*/
65 if (!power_on) {
Prathyush Kc7605692014-07-11 08:02:15 +090066 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
67 if (IS_ERR(pd->clk[i]))
68 break;
Krzysztof Kozlowski29e5eea2015-04-03 11:25:54 +020069 pd->pclk[i] = clk_get_parent(pd->clk[i]);
Prathyush Kc7605692014-07-11 08:02:15 +090070 if (clk_set_parent(pd->clk[i], pd->oscclk))
71 pr_err("%s: error setting oscclk as parent to clock %d\n",
Marek Szyprowski066502d2017-01-30 13:18:57 +010072 domain->name, i);
Prathyush Kc7605692014-07-11 08:02:15 +090073 }
74 }
75
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +020076 pwr = power_on ? pd->local_pwr_cfg : 0;
Ben Dooksd0ceee02016-06-21 11:20:25 +010077 writel_relaxed(pwr, base);
Thomas Abraham91cfbd42012-01-27 15:25:00 +090078
79 /* Wait max 1ms */
80 timeout = 10;
81
Linus Torvalds43a0a982016-08-01 18:36:01 -040082 while ((readl_relaxed(base + 0x4) & pd->local_pwr_cfg) != pwr) {
Thomas Abraham91cfbd42012-01-27 15:25:00 +090083 if (!timeout) {
84 op = (power_on) ? "enable" : "disable";
85 pr_err("Power domain %s %s failed\n", domain->name, op);
86 return -ETIMEDOUT;
87 }
88 timeout--;
89 cpu_relax();
90 usleep_range(80, 100);
91 }
Prathyush Kc7605692014-07-11 08:02:15 +090092
93 /* Restore clocks after powering on a domain*/
94 if (power_on) {
Prathyush Kc7605692014-07-11 08:02:15 +090095 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
96 if (IS_ERR(pd->clk[i]))
97 break;
Krzysztof Kozlowski29e5eea2015-04-03 11:25:54 +020098
Krzysztof Kozlowskia0a966b2016-04-22 09:26:52 +020099 if (IS_ERR(pd->pclk[i]))
Krzysztof Kozlowski29e5eea2015-04-03 11:25:54 +0200100 continue; /* Skip on first power up */
Prathyush Kc7605692014-07-11 08:02:15 +0900101 if (clk_set_parent(pd->clk[i], pd->pclk[i]))
102 pr_err("%s: error setting parent to clock%d\n",
Marek Szyprowski066502d2017-01-30 13:18:57 +0100103 domain->name, i);
Prathyush Kc7605692014-07-11 08:02:15 +0900104 }
105 }
106
Andrzej Hajda528eae62015-03-18 02:12:23 +0900107 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
108 if (IS_ERR(pd->asb_clk[i]))
109 break;
110 clk_disable_unprepare(pd->asb_clk[i]);
111 }
112
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900113 return 0;
114}
115
116static int exynos_pd_power_on(struct generic_pm_domain *domain)
117{
118 return exynos_pd_power(domain, true);
119}
120
121static int exynos_pd_power_off(struct generic_pm_domain *domain)
122{
123 return exynos_pd_power(domain, false);
124}
125
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +0200126static const struct exynos_pm_domain_config exynos4210_cfg __initconst = {
127 .local_pwr_cfg = 0x7,
128};
129
Chanwoo Choi6bce1972017-01-25 12:55:35 +0100130static const struct exynos_pm_domain_config exynos5433_cfg __initconst = {
131 .local_pwr_cfg = 0xf,
132};
133
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +0200134static const struct of_device_id exynos_pm_domain_of_match[] __initconst = {
135 {
136 .compatible = "samsung,exynos4210-pd",
137 .data = &exynos4210_cfg,
Chanwoo Choi6bce1972017-01-25 12:55:35 +0100138 }, {
139 .compatible = "samsung,exynos5433-pd",
140 .data = &exynos5433_cfg,
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +0200141 },
142 { },
143};
144
Marek Szyprowskib13b2332017-01-30 13:18:59 +0100145static __init const char *exynos_get_domain_name(struct device_node *node)
146{
147 const char *name;
148
149 if (of_property_read_string(node, "label", &name) < 0)
150 name = strrchr(node->full_name, '/') + 1;
151 return kstrdup_const(name, GFP_KERNEL);
152}
153
Tomasz Figa8eaa9e42013-06-15 09:13:25 +0900154static __init int exynos4_pm_init_power_domain(void)
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900155{
156 struct device_node *np;
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +0200157 const struct of_device_id *match;
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900158
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +0200159 for_each_matching_node_and_match(np, exynos_pm_domain_of_match, &match) {
160 const struct exynos_pm_domain_config *pm_domain_cfg;
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900161 struct exynos_pm_domain *pd;
Prathyush Kc7605692014-07-11 08:02:15 +0900162 int on, i;
Tomasz Figa8a65d232012-11-22 00:21:17 +0900163
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +0200164 pm_domain_cfg = match->data;
165
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900166 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
167 if (!pd) {
Krzysztof Kozlowskife4034a2015-03-27 13:21:28 +0100168 of_node_put(np);
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900169 return -ENOMEM;
170 }
Marek Szyprowskib13b2332017-01-30 13:18:59 +0100171 pd->pd.name = exynos_get_domain_name(np);
Krzysztof Kozlowskic88cad32015-03-27 13:12:00 +0100172 if (!pd->pd.name) {
173 kfree(pd);
174 of_node_put(np);
175 return -ENOMEM;
176 }
177
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900178 pd->base = of_iomap(np, 0);
Krzysztof Kozlowskief2156c2015-03-27 13:10:06 +0100179 if (!pd->base) {
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900180 pr_warn("%s: failed to map memory\n", __func__);
Vladimir Zapolskiy27bbd232015-07-31 03:09:49 +0300181 kfree_const(pd->pd.name);
Krzysztof Kozlowskief2156c2015-03-27 13:10:06 +0100182 kfree(pd);
Krzysztof Kozlowskief2156c2015-03-27 13:10:06 +0100183 continue;
184 }
185
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900186 pd->pd.power_off = exynos_pd_power_off;
187 pd->pd.power_on = exynos_pd_power_on;
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +0200188 pd->local_pwr_cfg = pm_domain_cfg->local_pwr_cfg;
Tomasz Figa2ed5f232012-11-22 00:21:08 +0900189
Andrzej Hajda528eae62015-03-18 02:12:23 +0900190 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
191 char clk_name[8];
192
193 snprintf(clk_name, sizeof(clk_name), "asb%d", i);
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900194 pd->asb_clk[i] = of_clk_get_by_name(np, clk_name);
Andrzej Hajda528eae62015-03-18 02:12:23 +0900195 if (IS_ERR(pd->asb_clk[i]))
196 break;
197 }
198
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900199 pd->oscclk = of_clk_get_by_name(np, "oscclk");
Prathyush Kc7605692014-07-11 08:02:15 +0900200 if (IS_ERR(pd->oscclk))
201 goto no_clk;
202
203 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
204 char clk_name[8];
205
206 snprintf(clk_name, sizeof(clk_name), "clk%d", i);
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900207 pd->clk[i] = of_clk_get_by_name(np, clk_name);
Prathyush Kc7605692014-07-11 08:02:15 +0900208 if (IS_ERR(pd->clk[i]))
209 break;
Krzysztof Kozlowski29e5eea2015-04-03 11:25:54 +0200210 /*
211 * Skip setting parent on first power up.
212 * The parent at this time may not be useful at all.
213 */
214 pd->pclk[i] = ERR_PTR(-EINVAL);
Prathyush Kc7605692014-07-11 08:02:15 +0900215 }
216
217 if (IS_ERR(pd->clk[0]))
218 clk_put(pd->oscclk);
219
220no_clk:
Linus Torvalds43a0a982016-08-01 18:36:01 -0400221 on = readl_relaxed(pd->base + 0x4) & pd->local_pwr_cfg;
Tomasz Figa2ed5f232012-11-22 00:21:08 +0900222
223 pm_genpd_init(&pd->pd, NULL, !on);
Tomasz Figaa4a8c2c2014-09-19 20:27:43 +0200224 of_genpd_add_provider_simple(np, &pd->pd);
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900225 }
Tomasz Figa8a65d232012-11-22 00:21:17 +0900226
Marek Szyprowski0f780752015-02-04 23:44:15 +0900227 /* Assign the child power domains to their parents */
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +0200228 for_each_matching_node(np, exynos_pm_domain_of_match) {
Jon Hunter8d68c372016-09-12 12:01:06 +0100229 struct of_phandle_args child, parent;
Marek Szyprowski0f780752015-02-04 23:44:15 +0900230
Jon Hunter8d68c372016-09-12 12:01:06 +0100231 child.np = np;
232 child.args_count = 0;
Marek Szyprowski0f780752015-02-04 23:44:15 +0900233
234 if (of_parse_phandle_with_args(np, "power-domains",
Jon Hunter8d68c372016-09-12 12:01:06 +0100235 "#power-domain-cells", 0,
236 &parent) != 0)
Krzysztof Kozlowski51a62562015-10-13 04:32:49 +0900237 continue;
Marek Szyprowski0f780752015-02-04 23:44:15 +0900238
Jon Hunter8d68c372016-09-12 12:01:06 +0100239 if (of_genpd_add_subdomain(&parent, &child))
Marek Szyprowski0f780752015-02-04 23:44:15 +0900240 pr_warn("%s failed to add subdomain: %s\n",
Marek Szyprowski402e73c2017-01-30 13:18:56 +0100241 parent.np->full_name, child.np->full_name);
Marek Szyprowski0f780752015-02-04 23:44:15 +0900242 else
243 pr_info("%s has as child subdomain: %s.\n",
Marek Szyprowski402e73c2017-01-30 13:18:56 +0100244 parent.np->full_name, child.np->full_name);
Marek Szyprowski0f780752015-02-04 23:44:15 +0900245 }
246
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900247 return 0;
248}
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900249core_initcall(exynos4_pm_init_power_domain);