blob: 4a87e86dec45d1546153ca0ebb7310bbd5f82d93 [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
Pankaj Dubeyb634e38f2014-07-08 07:54:19 +090026#define INT_LOCAL_PWR_EN 0x7
Prathyush Kc7605692014-07-11 08:02:15 +090027#define MAX_CLK_PER_DOMAIN 4
28
Thomas Abraham91cfbd42012-01-27 15:25:00 +090029/*
30 * Exynos specific wrapper around the generic power domain
31 */
32struct exynos_pm_domain {
33 void __iomem *base;
34 char const *name;
35 bool is_off;
36 struct generic_pm_domain pd;
Prathyush Kc7605692014-07-11 08:02:15 +090037 struct clk *oscclk;
38 struct clk *clk[MAX_CLK_PER_DOMAIN];
39 struct clk *pclk[MAX_CLK_PER_DOMAIN];
Andrzej Hajda528eae62015-03-18 02:12:23 +090040 struct clk *asb_clk[MAX_CLK_PER_DOMAIN];
Thomas Abraham91cfbd42012-01-27 15:25:00 +090041};
42
43static int exynos_pd_power(struct generic_pm_domain *domain, bool power_on)
44{
45 struct exynos_pm_domain *pd;
46 void __iomem *base;
47 u32 timeout, pwr;
48 char *op;
Andrzej Hajda528eae62015-03-18 02:12:23 +090049 int i;
Thomas Abraham91cfbd42012-01-27 15:25:00 +090050
51 pd = container_of(domain, struct exynos_pm_domain, pd);
52 base = pd->base;
53
Andrzej Hajda528eae62015-03-18 02:12:23 +090054 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
55 if (IS_ERR(pd->asb_clk[i]))
56 break;
57 clk_prepare_enable(pd->asb_clk[i]);
58 }
59
Prathyush Kc7605692014-07-11 08:02:15 +090060 /* Set oscclk before powering off a domain*/
61 if (!power_on) {
Prathyush Kc7605692014-07-11 08:02:15 +090062 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
63 if (IS_ERR(pd->clk[i]))
64 break;
Krzysztof Kozlowski29e5eea2015-04-03 11:25:54 +020065 pd->pclk[i] = clk_get_parent(pd->clk[i]);
Prathyush Kc7605692014-07-11 08:02:15 +090066 if (clk_set_parent(pd->clk[i], pd->oscclk))
67 pr_err("%s: error setting oscclk as parent to clock %d\n",
68 pd->name, i);
69 }
70 }
71
Pankaj Dubeyb634e38f2014-07-08 07:54:19 +090072 pwr = power_on ? INT_LOCAL_PWR_EN : 0;
Thomas Abraham91cfbd42012-01-27 15:25:00 +090073 __raw_writel(pwr, base);
74
75 /* Wait max 1ms */
76 timeout = 10;
77
Pankaj Dubeyb634e38f2014-07-08 07:54:19 +090078 while ((__raw_readl(base + 0x4) & INT_LOCAL_PWR_EN) != pwr) {
Thomas Abraham91cfbd42012-01-27 15:25:00 +090079 if (!timeout) {
80 op = (power_on) ? "enable" : "disable";
81 pr_err("Power domain %s %s failed\n", domain->name, op);
82 return -ETIMEDOUT;
83 }
84 timeout--;
85 cpu_relax();
86 usleep_range(80, 100);
87 }
Prathyush Kc7605692014-07-11 08:02:15 +090088
89 /* Restore clocks after powering on a domain*/
90 if (power_on) {
Prathyush Kc7605692014-07-11 08:02:15 +090091 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
92 if (IS_ERR(pd->clk[i]))
93 break;
Krzysztof Kozlowski29e5eea2015-04-03 11:25:54 +020094
95 if (IS_ERR(pd->clk[i]))
96 continue; /* Skip on first power up */
Prathyush Kc7605692014-07-11 08:02:15 +090097 if (clk_set_parent(pd->clk[i], pd->pclk[i]))
98 pr_err("%s: error setting parent to clock%d\n",
99 pd->name, i);
100 }
101 }
102
Andrzej Hajda528eae62015-03-18 02:12:23 +0900103 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
104 if (IS_ERR(pd->asb_clk[i]))
105 break;
106 clk_disable_unprepare(pd->asb_clk[i]);
107 }
108
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900109 return 0;
110}
111
112static int exynos_pd_power_on(struct generic_pm_domain *domain)
113{
114 return exynos_pd_power(domain, true);
115}
116
117static int exynos_pd_power_off(struct generic_pm_domain *domain)
118{
119 return exynos_pd_power(domain, false);
120}
121
Tomasz Figa8eaa9e42013-06-15 09:13:25 +0900122static __init int exynos4_pm_init_power_domain(void)
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900123{
124 struct device_node *np;
125
126 for_each_compatible_node(np, NULL, "samsung,exynos4210-pd") {
127 struct exynos_pm_domain *pd;
Prathyush Kc7605692014-07-11 08:02:15 +0900128 int on, i;
Tomasz Figa8a65d232012-11-22 00:21:17 +0900129
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900130 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
131 if (!pd) {
132 pr_err("%s: failed to allocate memory for domain\n",
133 __func__);
Krzysztof Kozlowskife4034a2015-03-27 13:21:28 +0100134 of_node_put(np);
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900135 return -ENOMEM;
136 }
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900137 pd->pd.name = kstrdup_const(strrchr(np->full_name, '/') + 1,
138 GFP_KERNEL);
Krzysztof Kozlowskic88cad32015-03-27 13:12:00 +0100139 if (!pd->pd.name) {
140 kfree(pd);
141 of_node_put(np);
142 return -ENOMEM;
143 }
144
Tomasz Figa7add0ec2012-11-22 00:21:12 +0900145 pd->name = pd->pd.name;
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900146 pd->base = of_iomap(np, 0);
Krzysztof Kozlowskief2156c2015-03-27 13:10:06 +0100147 if (!pd->base) {
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900148 pr_warn("%s: failed to map memory\n", __func__);
Vladimir Zapolskiy27bbd232015-07-31 03:09:49 +0300149 kfree_const(pd->pd.name);
Krzysztof Kozlowskief2156c2015-03-27 13:10:06 +0100150 kfree(pd);
Krzysztof Kozlowskief2156c2015-03-27 13:10:06 +0100151 continue;
152 }
153
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900154 pd->pd.power_off = exynos_pd_power_off;
155 pd->pd.power_on = exynos_pd_power_on;
Tomasz Figa2ed5f232012-11-22 00:21:08 +0900156
Andrzej Hajda528eae62015-03-18 02:12:23 +0900157 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
158 char clk_name[8];
159
160 snprintf(clk_name, sizeof(clk_name), "asb%d", i);
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900161 pd->asb_clk[i] = of_clk_get_by_name(np, clk_name);
Andrzej Hajda528eae62015-03-18 02:12:23 +0900162 if (IS_ERR(pd->asb_clk[i]))
163 break;
164 }
165
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900166 pd->oscclk = of_clk_get_by_name(np, "oscclk");
Prathyush Kc7605692014-07-11 08:02:15 +0900167 if (IS_ERR(pd->oscclk))
168 goto no_clk;
169
170 for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
171 char clk_name[8];
172
173 snprintf(clk_name, sizeof(clk_name), "clk%d", i);
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900174 pd->clk[i] = of_clk_get_by_name(np, clk_name);
Prathyush Kc7605692014-07-11 08:02:15 +0900175 if (IS_ERR(pd->clk[i]))
176 break;
Krzysztof Kozlowski29e5eea2015-04-03 11:25:54 +0200177 /*
178 * Skip setting parent on first power up.
179 * The parent at this time may not be useful at all.
180 */
181 pd->pclk[i] = ERR_PTR(-EINVAL);
Prathyush Kc7605692014-07-11 08:02:15 +0900182 }
183
184 if (IS_ERR(pd->clk[0]))
185 clk_put(pd->oscclk);
186
187no_clk:
Pankaj Dubeyb634e38f2014-07-08 07:54:19 +0900188 on = __raw_readl(pd->base + 0x4) & INT_LOCAL_PWR_EN;
Tomasz Figa2ed5f232012-11-22 00:21:08 +0900189
190 pm_genpd_init(&pd->pd, NULL, !on);
Tomasz Figaa4a8c2c2014-09-19 20:27:43 +0200191 of_genpd_add_provider_simple(np, &pd->pd);
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900192 }
Tomasz Figa8a65d232012-11-22 00:21:17 +0900193
Marek Szyprowski0f780752015-02-04 23:44:15 +0900194 /* Assign the child power domains to their parents */
195 for_each_compatible_node(np, NULL, "samsung,exynos4210-pd") {
196 struct generic_pm_domain *child_domain, *parent_domain;
197 struct of_phandle_args args;
198
199 args.np = np;
200 args.args_count = 0;
201 child_domain = of_genpd_get_from_provider(&args);
Krzysztof Kozlowski0b7dc0f2015-05-13 17:45:52 +0900202 if (IS_ERR(child_domain))
Krzysztof Kozlowskife4034a2015-03-27 13:21:28 +0100203 goto next_pd;
Marek Szyprowski0f780752015-02-04 23:44:15 +0900204
205 if (of_parse_phandle_with_args(np, "power-domains",
206 "#power-domain-cells", 0, &args) != 0)
Krzysztof Kozlowskife4034a2015-03-27 13:21:28 +0100207 goto next_pd;
Marek Szyprowski0f780752015-02-04 23:44:15 +0900208
209 parent_domain = of_genpd_get_from_provider(&args);
Krzysztof Kozlowski0b7dc0f2015-05-13 17:45:52 +0900210 if (IS_ERR(parent_domain))
Krzysztof Kozlowskife4034a2015-03-27 13:21:28 +0100211 goto next_pd;
Marek Szyprowski0f780752015-02-04 23:44:15 +0900212
213 if (pm_genpd_add_subdomain(parent_domain, child_domain))
214 pr_warn("%s failed to add subdomain: %s\n",
215 parent_domain->name, child_domain->name);
216 else
217 pr_info("%s has as child subdomain: %s.\n",
218 parent_domain->name, child_domain->name);
Krzysztof Kozlowskife4034a2015-03-27 13:21:28 +0100219next_pd:
Marek Szyprowski0f780752015-02-04 23:44:15 +0900220 of_node_put(np);
221 }
222
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900223 return 0;
224}
Marek Szyprowski2be2a3f2015-06-04 08:09:27 +0900225core_initcall(exynos4_pm_init_power_domain);