blob: 8fd24882f0b1eb5a1963fdb985c577729170e081 [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>
20#include <linux/delay.h>
21#include <linux/of_address.h>
Tomasz Figa8a65d232012-11-22 00:21:17 +090022#include <linux/of_platform.h>
23#include <linux/sched.h>
Thomas Abraham91cfbd42012-01-27 15:25:00 +090024
Thomas Abraham91cfbd42012-01-27 15:25:00 +090025#include <plat/devs.h>
26
Kukjin Kim65c9a852013-12-19 04:06:56 +090027#include "regs-pmu.h"
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;
37};
38
39static int exynos_pd_power(struct generic_pm_domain *domain, bool power_on)
40{
41 struct exynos_pm_domain *pd;
42 void __iomem *base;
43 u32 timeout, pwr;
44 char *op;
45
46 pd = container_of(domain, struct exynos_pm_domain, pd);
47 base = pd->base;
48
49 pwr = power_on ? S5P_INT_LOCAL_PWR_EN : 0;
50 __raw_writel(pwr, base);
51
52 /* Wait max 1ms */
53 timeout = 10;
54
55 while ((__raw_readl(base + 0x4) & S5P_INT_LOCAL_PWR_EN) != pwr) {
56 if (!timeout) {
57 op = (power_on) ? "enable" : "disable";
58 pr_err("Power domain %s %s failed\n", domain->name, op);
59 return -ETIMEDOUT;
60 }
61 timeout--;
62 cpu_relax();
63 usleep_range(80, 100);
64 }
65 return 0;
66}
67
68static int exynos_pd_power_on(struct generic_pm_domain *domain)
69{
70 return exynos_pd_power(domain, true);
71}
72
73static int exynos_pd_power_off(struct generic_pm_domain *domain)
74{
75 return exynos_pd_power(domain, false);
76}
77
Tomasz Figa8a65d232012-11-22 00:21:17 +090078static void exynos_add_device_to_domain(struct exynos_pm_domain *pd,
79 struct device *dev)
80{
81 int ret;
82
83 dev_dbg(dev, "adding to power domain %s\n", pd->pd.name);
84
85 while (1) {
86 ret = pm_genpd_add_device(&pd->pd, dev);
87 if (ret != -EAGAIN)
88 break;
89 cond_resched();
90 }
91
92 pm_genpd_dev_need_restore(dev, true);
93}
94
95static void exynos_remove_device_from_domain(struct device *dev)
96{
97 struct generic_pm_domain *genpd = dev_to_genpd(dev);
98 int ret;
99
100 dev_dbg(dev, "removing from power domain %s\n", genpd->name);
101
102 while (1) {
103 ret = pm_genpd_remove_device(genpd, dev);
104 if (ret != -EAGAIN)
105 break;
106 cond_resched();
107 }
108}
109
110static void exynos_read_domain_from_dt(struct device *dev)
111{
112 struct platform_device *pd_pdev;
113 struct exynos_pm_domain *pd;
114 struct device_node *node;
115
116 node = of_parse_phandle(dev->of_node, "samsung,power-domain", 0);
117 if (!node)
118 return;
119 pd_pdev = of_find_device_by_node(node);
120 if (!pd_pdev)
121 return;
122 pd = platform_get_drvdata(pd_pdev);
123 exynos_add_device_to_domain(pd, dev);
124}
125
126static int exynos_pm_notifier_call(struct notifier_block *nb,
127 unsigned long event, void *data)
128{
129 struct device *dev = data;
130
131 switch (event) {
132 case BUS_NOTIFY_BIND_DRIVER:
133 if (dev->of_node)
134 exynos_read_domain_from_dt(dev);
135
136 break;
137
138 case BUS_NOTIFY_UNBOUND_DRIVER:
139 exynos_remove_device_from_domain(dev);
140
141 break;
142 }
143 return NOTIFY_DONE;
144}
145
146static struct notifier_block platform_nb = {
147 .notifier_call = exynos_pm_notifier_call,
148};
149
Tomasz Figa8eaa9e42013-06-15 09:13:25 +0900150static __init int exynos4_pm_init_power_domain(void)
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900151{
Tomasz Figa8a65d232012-11-22 00:21:17 +0900152 struct platform_device *pdev;
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900153 struct device_node *np;
154
155 for_each_compatible_node(np, NULL, "samsung,exynos4210-pd") {
156 struct exynos_pm_domain *pd;
Tomasz Figa2ed5f232012-11-22 00:21:08 +0900157 int on;
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900158
Tomasz Figa8a65d232012-11-22 00:21:17 +0900159 pdev = of_find_device_by_node(np);
160
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900161 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
162 if (!pd) {
163 pr_err("%s: failed to allocate memory for domain\n",
164 __func__);
165 return -ENOMEM;
166 }
167
Tomasz Figa7add0ec2012-11-22 00:21:12 +0900168 pd->pd.name = kstrdup(np->name, GFP_KERNEL);
169 pd->name = pd->pd.name;
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900170 pd->base = of_iomap(np, 0);
171 pd->pd.power_off = exynos_pd_power_off;
172 pd->pd.power_on = exynos_pd_power_on;
173 pd->pd.of_node = np;
Tomasz Figa2ed5f232012-11-22 00:21:08 +0900174
Tomasz Figa8a65d232012-11-22 00:21:17 +0900175 platform_set_drvdata(pdev, pd);
176
Tomasz Figa2ed5f232012-11-22 00:21:08 +0900177 on = __raw_readl(pd->base + 0x4) & S5P_INT_LOCAL_PWR_EN;
178
179 pm_genpd_init(&pd->pd, NULL, !on);
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900180 }
Tomasz Figa8a65d232012-11-22 00:21:17 +0900181
182 bus_register_notifier(&platform_bus_type, &platform_nb);
183
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900184 return 0;
185}
Thomas Abraham91cfbd42012-01-27 15:25:00 +0900186arch_initcall(exynos4_pm_init_power_domain);