2 * drivers/powergate/tegra-powergate.c
4 * Copyright (c) 2010 Google, Inc
7 * Colin Cross <ccross@google.com>
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #include <linux/kernel.h>
21 #include <linux/clk.h>
22 #include <linux/debugfs.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/init.h>
27 #include <linux/seq_file.h>
28 #include <linux/spinlock.h>
31 #include <mach/iomap.h>
32 #include <mach/powergate.h>
36 #define PWRGATE_TOGGLE 0x30
37 #define PWRGATE_TOGGLE_START (1 << 8)
39 #define REMOVE_CLAMPING 0x34
41 #define PWRGATE_STATUS 0x38
43 static int tegra_num_powerdomains;
45 static DEFINE_SPINLOCK(tegra_powergate_lock);
47 static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
49 static u32 pmc_read(unsigned long reg)
51 return readl(pmc + reg);
54 static void pmc_write(u32 val, unsigned long reg)
56 writel(val, pmc + reg);
59 static int tegra_powergate_set(int id, bool new_state)
64 spin_lock_irqsave(&tegra_powergate_lock, flags);
66 status = pmc_read(PWRGATE_STATUS) & (1 << id);
68 if (status == new_state) {
69 spin_unlock_irqrestore(&tegra_powergate_lock, flags);
73 pmc_write(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
75 spin_unlock_irqrestore(&tegra_powergate_lock, flags);
80 int tegra_powergate_power_on(int id)
82 if (id < 0 || id >= tegra_num_powerdomains)
85 return tegra_powergate_set(id, true);
88 int tegra_powergate_power_off(int id)
90 if (id < 0 || id >= tegra_num_powerdomains)
93 return tegra_powergate_set(id, false);
96 int tegra_powergate_is_powered(int id)
100 if (id < 0 || id >= tegra_num_powerdomains)
103 status = pmc_read(PWRGATE_STATUS) & (1 << id);
107 int tegra_powergate_remove_clamping(int id)
111 if (id < 0 || id >= tegra_num_powerdomains)
115 * Tegra 2 has a bug where PCIE and VDE clamping masks are
116 * swapped relatively to the partition ids
118 if (id == TEGRA_POWERGATE_VDEC)
119 mask = (1 << TEGRA_POWERGATE_PCIE);
120 else if (id == TEGRA_POWERGATE_PCIE)
121 mask = (1 << TEGRA_POWERGATE_VDEC);
125 pmc_write(mask, REMOVE_CLAMPING);
130 /* Must be called with clk disabled, and returns with clk enabled */
131 int tegra_powergate_sequence_power_up(int id, struct clk *clk)
135 tegra_periph_reset_assert(clk);
137 ret = tegra_powergate_power_on(id);
141 ret = clk_enable(clk);
147 ret = tegra_powergate_remove_clamping(id);
152 tegra_periph_reset_deassert(clk);
159 tegra_powergate_power_off(id);
164 int __init tegra_powergate_init(void)
166 switch (tegra_chip_id) {
168 tegra_num_powerdomains = 7;
171 /* Unknown Tegra variant. Disable powergating */
172 tegra_num_powerdomains = 0;
178 arch_initcall(tegra_powergate_init);
180 #ifdef CONFIG_DEBUG_FS
182 static const char * const powergate_name[] = {
183 [TEGRA_POWERGATE_CPU] = "cpu",
184 [TEGRA_POWERGATE_3D] = "3d",
185 [TEGRA_POWERGATE_VENC] = "venc",
186 [TEGRA_POWERGATE_VDEC] = "vdec",
187 [TEGRA_POWERGATE_PCIE] = "pcie",
188 [TEGRA_POWERGATE_L2] = "l2",
189 [TEGRA_POWERGATE_MPE] = "mpe",
192 static int powergate_show(struct seq_file *s, void *data)
196 seq_printf(s, " powergate powered\n");
197 seq_printf(s, "------------------\n");
199 for (i = 0; i < tegra_num_powerdomains; i++)
200 seq_printf(s, " %9s %7s\n", powergate_name[i],
201 tegra_powergate_is_powered(i) ? "yes" : "no");
205 static int powergate_open(struct inode *inode, struct file *file)
207 return single_open(file, powergate_show, inode->i_private);
210 static const struct file_operations powergate_fops = {
211 .open = powergate_open,
214 .release = single_release,
217 static int __init powergate_debugfs_init(void)
222 d = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
230 late_initcall(powergate_debugfs_init);