Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 1 | /* |
| 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 Figa | 8a65d23 | 2012-11-22 00:21:17 +0900 | [diff] [blame] | 22 | #include <linux/of_platform.h> |
| 23 | #include <linux/sched.h> |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 24 | |
| 25 | #include <mach/regs-pmu.h> |
| 26 | #include <plat/devs.h> |
| 27 | |
| 28 | /* |
| 29 | * Exynos specific wrapper around the generic power domain |
| 30 | */ |
| 31 | struct exynos_pm_domain { |
| 32 | void __iomem *base; |
| 33 | char const *name; |
| 34 | bool is_off; |
| 35 | struct generic_pm_domain pd; |
| 36 | }; |
| 37 | |
| 38 | static int exynos_pd_power(struct generic_pm_domain *domain, bool power_on) |
| 39 | { |
| 40 | struct exynos_pm_domain *pd; |
| 41 | void __iomem *base; |
| 42 | u32 timeout, pwr; |
| 43 | char *op; |
| 44 | |
| 45 | pd = container_of(domain, struct exynos_pm_domain, pd); |
| 46 | base = pd->base; |
| 47 | |
| 48 | pwr = power_on ? S5P_INT_LOCAL_PWR_EN : 0; |
| 49 | __raw_writel(pwr, base); |
| 50 | |
| 51 | /* Wait max 1ms */ |
| 52 | timeout = 10; |
| 53 | |
| 54 | while ((__raw_readl(base + 0x4) & S5P_INT_LOCAL_PWR_EN) != pwr) { |
| 55 | if (!timeout) { |
| 56 | op = (power_on) ? "enable" : "disable"; |
| 57 | pr_err("Power domain %s %s failed\n", domain->name, op); |
| 58 | return -ETIMEDOUT; |
| 59 | } |
| 60 | timeout--; |
| 61 | cpu_relax(); |
| 62 | usleep_range(80, 100); |
| 63 | } |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int exynos_pd_power_on(struct generic_pm_domain *domain) |
| 68 | { |
| 69 | return exynos_pd_power(domain, true); |
| 70 | } |
| 71 | |
| 72 | static int exynos_pd_power_off(struct generic_pm_domain *domain) |
| 73 | { |
| 74 | return exynos_pd_power(domain, false); |
| 75 | } |
| 76 | |
Tomasz Figa | 8a65d23 | 2012-11-22 00:21:17 +0900 | [diff] [blame] | 77 | static void exynos_add_device_to_domain(struct exynos_pm_domain *pd, |
| 78 | struct device *dev) |
| 79 | { |
| 80 | int ret; |
| 81 | |
| 82 | dev_dbg(dev, "adding to power domain %s\n", pd->pd.name); |
| 83 | |
| 84 | while (1) { |
| 85 | ret = pm_genpd_add_device(&pd->pd, dev); |
| 86 | if (ret != -EAGAIN) |
| 87 | break; |
| 88 | cond_resched(); |
| 89 | } |
| 90 | |
| 91 | pm_genpd_dev_need_restore(dev, true); |
| 92 | } |
| 93 | |
| 94 | static void exynos_remove_device_from_domain(struct device *dev) |
| 95 | { |
| 96 | struct generic_pm_domain *genpd = dev_to_genpd(dev); |
| 97 | int ret; |
| 98 | |
| 99 | dev_dbg(dev, "removing from power domain %s\n", genpd->name); |
| 100 | |
| 101 | while (1) { |
| 102 | ret = pm_genpd_remove_device(genpd, dev); |
| 103 | if (ret != -EAGAIN) |
| 104 | break; |
| 105 | cond_resched(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static void exynos_read_domain_from_dt(struct device *dev) |
| 110 | { |
| 111 | struct platform_device *pd_pdev; |
| 112 | struct exynos_pm_domain *pd; |
| 113 | struct device_node *node; |
| 114 | |
| 115 | node = of_parse_phandle(dev->of_node, "samsung,power-domain", 0); |
| 116 | if (!node) |
| 117 | return; |
| 118 | pd_pdev = of_find_device_by_node(node); |
| 119 | if (!pd_pdev) |
| 120 | return; |
| 121 | pd = platform_get_drvdata(pd_pdev); |
| 122 | exynos_add_device_to_domain(pd, dev); |
| 123 | } |
| 124 | |
| 125 | static int exynos_pm_notifier_call(struct notifier_block *nb, |
| 126 | unsigned long event, void *data) |
| 127 | { |
| 128 | struct device *dev = data; |
| 129 | |
| 130 | switch (event) { |
| 131 | case BUS_NOTIFY_BIND_DRIVER: |
| 132 | if (dev->of_node) |
| 133 | exynos_read_domain_from_dt(dev); |
| 134 | |
| 135 | break; |
| 136 | |
| 137 | case BUS_NOTIFY_UNBOUND_DRIVER: |
| 138 | exynos_remove_device_from_domain(dev); |
| 139 | |
| 140 | break; |
| 141 | } |
| 142 | return NOTIFY_DONE; |
| 143 | } |
| 144 | |
| 145 | static struct notifier_block platform_nb = { |
| 146 | .notifier_call = exynos_pm_notifier_call, |
| 147 | }; |
| 148 | |
Tomasz Figa | 8eaa9e4 | 2013-06-15 09:13:25 +0900 | [diff] [blame] | 149 | static __init int exynos4_pm_init_power_domain(void) |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 150 | { |
Tomasz Figa | 8a65d23 | 2012-11-22 00:21:17 +0900 | [diff] [blame] | 151 | struct platform_device *pdev; |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 152 | struct device_node *np; |
| 153 | |
| 154 | for_each_compatible_node(np, NULL, "samsung,exynos4210-pd") { |
| 155 | struct exynos_pm_domain *pd; |
Tomasz Figa | 2ed5f23 | 2012-11-22 00:21:08 +0900 | [diff] [blame] | 156 | int on; |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 157 | |
Tomasz Figa | 8a65d23 | 2012-11-22 00:21:17 +0900 | [diff] [blame] | 158 | pdev = of_find_device_by_node(np); |
| 159 | |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 160 | pd = kzalloc(sizeof(*pd), GFP_KERNEL); |
| 161 | if (!pd) { |
| 162 | pr_err("%s: failed to allocate memory for domain\n", |
| 163 | __func__); |
| 164 | return -ENOMEM; |
| 165 | } |
| 166 | |
Tomasz Figa | 7add0ec | 2012-11-22 00:21:12 +0900 | [diff] [blame] | 167 | pd->pd.name = kstrdup(np->name, GFP_KERNEL); |
| 168 | pd->name = pd->pd.name; |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 169 | pd->base = of_iomap(np, 0); |
| 170 | pd->pd.power_off = exynos_pd_power_off; |
| 171 | pd->pd.power_on = exynos_pd_power_on; |
| 172 | pd->pd.of_node = np; |
Tomasz Figa | 2ed5f23 | 2012-11-22 00:21:08 +0900 | [diff] [blame] | 173 | |
Tomasz Figa | 8a65d23 | 2012-11-22 00:21:17 +0900 | [diff] [blame] | 174 | platform_set_drvdata(pdev, pd); |
| 175 | |
Tomasz Figa | 2ed5f23 | 2012-11-22 00:21:08 +0900 | [diff] [blame] | 176 | on = __raw_readl(pd->base + 0x4) & S5P_INT_LOCAL_PWR_EN; |
| 177 | |
| 178 | pm_genpd_init(&pd->pd, NULL, !on); |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 179 | } |
Tomasz Figa | 8a65d23 | 2012-11-22 00:21:17 +0900 | [diff] [blame] | 180 | |
| 181 | bus_register_notifier(&platform_bus_type, &platform_nb); |
| 182 | |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 183 | return 0; |
| 184 | } |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 185 | arch_initcall(exynos4_pm_init_power_domain); |
| 186 | |
Shawn Guo | bb13fab | 2012-04-26 10:35:40 +0800 | [diff] [blame] | 187 | int __init exynos_pm_late_initcall(void) |
Thomas Abraham | 91cfbd4 | 2012-01-27 15:25:00 +0900 | [diff] [blame] | 188 | { |
| 189 | pm_genpd_poweroff_unused(); |
| 190 | return 0; |
| 191 | } |