blob: 4822346aadc6c8766e3d9cee67b57afea74f76d0 [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;
38 char const *name;
39 bool is_off;
40 struct generic_pm_domain pd;
Prathyush Kc7605692014-07-11 08:02:15 +090041 struct clk *oscclk;
42 struct clk *clk[MAX_CLK_PER_DOMAIN];
43 struct clk *pclk[MAX_CLK_PER_DOMAIN];
Andrzej Hajda528eae62015-03-18 02:12:23 +090044 struct clk *asb_clk[MAX_CLK_PER_DOMAIN];
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +020045 u32 local_pwr_cfg;
Thomas Abraham91cfbd42012-01-27 15:25:00 +090046};
47
48static int exynos_pd_power(struct generic_pm_domain *domain, bool power_on)
49{
50 struct exynos_pm_domain *pd;
51 void __iomem *base;
52 u32 timeout, pwr;
53 char *op;
Andrzej Hajda528eae62015-03-18 02:12:23 +090054 int i;
Thomas Abraham91cfbd42012-01-27 15:25:00 +090055
56 pd = container_of(domain, struct exynos_pm_domain, pd);
57 base = pd->base;
58
Andrzej Hajda528eae62015-03-18 02:12:23 +090059 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
60 if (IS_ERR(pd->asb_clk[i]))
61 break;
62 clk_prepare_enable(pd->asb_clk[i]);
63 }
64
Prathyush Kc7605692014-07-11 08:02:15 +090065 /* Set oscclk before powering off a domain*/
66 if (!power_on) {
Prathyush Kc7605692014-07-11 08:02:15 +090067 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
68 if (IS_ERR(pd->clk[i]))
69 break;
Krzysztof Kozlowski29e5eea2015-04-03 11:25:54 +020070 pd->pclk[i] = clk_get_parent(pd->clk[i]);
Prathyush Kc7605692014-07-11 08:02:15 +090071 if (clk_set_parent(pd->clk[i], pd->oscclk))
72 pr_err("%s: error setting oscclk as parent to clock %d\n",
73 pd->name, i);
74 }
75 }
76
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +020077 pwr = power_on ? pd->local_pwr_cfg : 0;
Ben Dooksd0ceee02016-06-21 11:20:25 +010078 writel_relaxed(pwr, base);
Thomas Abraham91cfbd42012-01-27 15:25:00 +090079
80 /* Wait max 1ms */
81 timeout = 10;
82
Linus Torvalds43a0a982016-08-01 18:36:01 -040083 while ((readl_relaxed(base + 0x4) & pd->local_pwr_cfg) != pwr) {
Thomas Abraham91cfbd42012-01-27 15:25:00 +090084 if (!timeout) {
85 op = (power_on) ? "enable" : "disable";
86 pr_err("Power domain %s %s failed\n", domain->name, op);
87 return -ETIMEDOUT;
88 }
89 timeout--;
90 cpu_relax();
91 usleep_range(80, 100);
92 }
Prathyush Kc7605692014-07-11 08:02:15 +090093
94 /* Restore clocks after powering on a domain*/
95 if (power_on) {
Prathyush Kc7605692014-07-11 08:02:15 +090096 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
97 if (IS_ERR(pd->clk[i]))
98 break;
Krzysztof Kozlowski29e5eea2015-04-03 11:25:54 +020099
Krzysztof Kozlowskia0a966b2016-04-22 09:26:52 +0200100 if (IS_ERR(pd->pclk[i]))
Krzysztof Kozlowski29e5eea2015-04-03 11:25:54 +0200101 continue; /* Skip on first power up */
Prathyush Kc7605692014-07-11 08:02:15 +0900102 if (clk_set_parent(pd->clk[i], pd->pclk[i]))
103 pr_err("%s: error setting parent to clock%d\n",
104 pd->name, i);
105 }
106 }
107
Andrzej Hajda528eae62015-03-18 02:12:23 +0900108 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
109 if (IS_ERR(pd->asb_clk[i]))
110 break;
111 clk_disable_unprepare(pd->asb_clk[i]);
112 }
113
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900114 return 0;
115}
116
117static int exynos_pd_power_on(struct generic_pm_domain *domain)
118{
119 return exynos_pd_power(domain, true);
120}
121
122static int exynos_pd_power_off(struct generic_pm_domain *domain)
123{
124 return exynos_pd_power(domain, false);
125}
126
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +0200127static const struct exynos_pm_domain_config exynos4210_cfg __initconst = {
128 .local_pwr_cfg = 0x7,
129};
130
131static const struct of_device_id exynos_pm_domain_of_match[] __initconst = {
132 {
133 .compatible = "samsung,exynos4210-pd",
134 .data = &exynos4210_cfg,
135 },
136 { },
137};
138
Tomasz Figa8eaa9e42013-06-15 09:13:25 +0900139static __init int exynos4_pm_init_power_domain(void)
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900140{
141 struct device_node *np;
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +0200142 const struct of_device_id *match;
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900143
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +0200144 for_each_matching_node_and_match(np, exynos_pm_domain_of_match, &match) {
145 const struct exynos_pm_domain_config *pm_domain_cfg;
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900146 struct exynos_pm_domain *pd;
Prathyush Kc7605692014-07-11 08:02:15 +0900147 int on, i;
Tomasz Figa8a65d232012-11-22 00:21:17 +0900148
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +0200149 pm_domain_cfg = match->data;
150
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900151 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
152 if (!pd) {
153 pr_err("%s: failed to allocate memory for domain\n",
154 __func__);
Krzysztof Kozlowskife4034a2015-03-27 13:21:28 +0100155 of_node_put(np);
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900156 return -ENOMEM;
157 }
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900158 pd->pd.name = kstrdup_const(strrchr(np->full_name, '/') + 1,
159 GFP_KERNEL);
Krzysztof Kozlowskic88cad32015-03-27 13:12:00 +0100160 if (!pd->pd.name) {
161 kfree(pd);
162 of_node_put(np);
163 return -ENOMEM;
164 }
165
Tomasz Figa7add0ec2012-11-22 00:21:12 +0900166 pd->name = pd->pd.name;
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900167 pd->base = of_iomap(np, 0);
Krzysztof Kozlowskief2156c2015-03-27 13:10:06 +0100168 if (!pd->base) {
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900169 pr_warn("%s: failed to map memory\n", __func__);
Vladimir Zapolskiy27bbd232015-07-31 03:09:49 +0300170 kfree_const(pd->pd.name);
Krzysztof Kozlowskief2156c2015-03-27 13:10:06 +0100171 kfree(pd);
Krzysztof Kozlowskief2156c2015-03-27 13:10:06 +0100172 continue;
173 }
174
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900175 pd->pd.power_off = exynos_pd_power_off;
176 pd->pd.power_on = exynos_pd_power_on;
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +0200177 pd->local_pwr_cfg = pm_domain_cfg->local_pwr_cfg;
Tomasz Figa2ed5f232012-11-22 00:21:08 +0900178
Andrzej Hajda528eae62015-03-18 02:12:23 +0900179 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
180 char clk_name[8];
181
182 snprintf(clk_name, sizeof(clk_name), "asb%d", i);
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900183 pd->asb_clk[i] = of_clk_get_by_name(np, clk_name);
Andrzej Hajda528eae62015-03-18 02:12:23 +0900184 if (IS_ERR(pd->asb_clk[i]))
185 break;
186 }
187
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900188 pd->oscclk = of_clk_get_by_name(np, "oscclk");
Prathyush Kc7605692014-07-11 08:02:15 +0900189 if (IS_ERR(pd->oscclk))
190 goto no_clk;
191
192 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
193 char clk_name[8];
194
195 snprintf(clk_name, sizeof(clk_name), "clk%d", i);
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900196 pd->clk[i] = of_clk_get_by_name(np, clk_name);
Prathyush Kc7605692014-07-11 08:02:15 +0900197 if (IS_ERR(pd->clk[i]))
198 break;
Krzysztof Kozlowski29e5eea2015-04-03 11:25:54 +0200199 /*
200 * Skip setting parent on first power up.
201 * The parent at this time may not be useful at all.
202 */
203 pd->pclk[i] = ERR_PTR(-EINVAL);
Prathyush Kc7605692014-07-11 08:02:15 +0900204 }
205
206 if (IS_ERR(pd->clk[0]))
207 clk_put(pd->oscclk);
208
209no_clk:
Linus Torvalds43a0a982016-08-01 18:36:01 -0400210 on = readl_relaxed(pd->base + 0x4) & pd->local_pwr_cfg;
Tomasz Figa2ed5f232012-11-22 00:21:08 +0900211
212 pm_genpd_init(&pd->pd, NULL, !on);
Tomasz Figaa4a8c2c2014-09-19 20:27:43 +0200213 of_genpd_add_provider_simple(np, &pd->pd);
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900214 }
Tomasz Figa8a65d232012-11-22 00:21:17 +0900215
Marek Szyprowski0f780752015-02-04 23:44:15 +0900216 /* Assign the child power domains to their parents */
Krzysztof Kozlowskic028e172016-05-10 15:52:21 +0200217 for_each_matching_node(np, exynos_pm_domain_of_match) {
Marek Szyprowski0f780752015-02-04 23:44:15 +0900218 struct generic_pm_domain *child_domain, *parent_domain;
219 struct of_phandle_args args;
220
221 args.np = np;
222 args.args_count = 0;
223 child_domain = of_genpd_get_from_provider(&args);
Krzysztof Kozlowski0b7dc0f2015-05-13 17:45:52 +0900224 if (IS_ERR(child_domain))
Krzysztof Kozlowski51a62562015-10-13 04:32:49 +0900225 continue;
Marek Szyprowski0f780752015-02-04 23:44:15 +0900226
227 if (of_parse_phandle_with_args(np, "power-domains",
228 "#power-domain-cells", 0, &args) != 0)
Krzysztof Kozlowski51a62562015-10-13 04:32:49 +0900229 continue;
Marek Szyprowski0f780752015-02-04 23:44:15 +0900230
231 parent_domain = of_genpd_get_from_provider(&args);
Krzysztof Kozlowski0b7dc0f2015-05-13 17:45:52 +0900232 if (IS_ERR(parent_domain))
Krzysztof Kozlowski51a62562015-10-13 04:32:49 +0900233 continue;
Marek Szyprowski0f780752015-02-04 23:44:15 +0900234
235 if (pm_genpd_add_subdomain(parent_domain, child_domain))
236 pr_warn("%s failed to add subdomain: %s\n",
237 parent_domain->name, child_domain->name);
238 else
239 pr_info("%s has as child subdomain: %s.\n",
240 parent_domain->name, child_domain->name);
Marek Szyprowski0f780752015-02-04 23:44:15 +0900241 }
242
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900243 return 0;
244}
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900245core_initcall(exynos4_pm_init_power_domain);