Merge branch 'gart/rename' of git://git.kernel.org/pub/scm/linux/kernel/git/joro...
[linux-2.6.git] / arch / blackfin / mach-common / dpmc.c
1 /*
2  * Copyright 2008 Analog Devices Inc.
3  *
4  * Licensed under the GPL-2 or later.
5  */
6
7 #include <linux/cdev.h>
8 #include <linux/device.h>
9 #include <linux/errno.h>
10 #include <linux/fs.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/types.h>
15 #include <linux/cpufreq.h>
16
17 #include <asm/delay.h>
18 #include <asm/dpmc.h>
19
20 #define DRIVER_NAME "bfin dpmc"
21
22 #define dprintk(msg...) \
23         cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, DRIVER_NAME, msg)
24
25 struct bfin_dpmc_platform_data *pdata;
26
27 /**
28  *      bfin_set_vlev - Update VLEV field in VR_CTL Reg.
29  *                      Avoid BYPASS sequence
30  */
31 static void bfin_set_vlev(unsigned int vlev)
32 {
33         unsigned pll_lcnt;
34
35         pll_lcnt = bfin_read_PLL_LOCKCNT();
36
37         bfin_write_PLL_LOCKCNT(1);
38         bfin_write_VR_CTL((bfin_read_VR_CTL() & ~VLEV) | vlev);
39         bfin_write_PLL_LOCKCNT(pll_lcnt);
40 }
41
42 /**
43  *      bfin_get_vlev - Get CPU specific VLEV from platform device data
44  */
45 static unsigned int bfin_get_vlev(unsigned int freq)
46 {
47         int i;
48
49         if (!pdata)
50                 goto err_out;
51
52         freq >>= 16;
53
54         for (i = 0; i < pdata->tabsize; i++)
55                 if (freq <= (pdata->tuple_tab[i] & 0xFFFF))
56                         return pdata->tuple_tab[i] >> 16;
57
58 err_out:
59         printk(KERN_WARNING "DPMC: No suitable CCLK VDDINT voltage pair found\n");
60         return VLEV_120;
61 }
62
63 #ifdef CONFIG_CPU_FREQ
64 # ifdef CONFIG_SMP
65 static void bfin_idle_this_cpu(void *info)
66 {
67         unsigned long flags = 0;
68         unsigned long iwr0, iwr1, iwr2;
69         unsigned int cpu = smp_processor_id();
70
71         local_irq_save_hw(flags);
72         bfin_iwr_set_sup0(&iwr0, &iwr1, &iwr2);
73
74         platform_clear_ipi(cpu, IRQ_SUPPLE_0);
75         SSYNC();
76         asm("IDLE;");
77         bfin_iwr_restore(iwr0, iwr1, iwr2);
78
79         local_irq_restore_hw(flags);
80 }
81
82 static void bfin_idle_cpu(void)
83 {
84         smp_call_function(bfin_idle_this_cpu, NULL, 0);
85 }
86
87 static void bfin_wakeup_cpu(void)
88 {
89         unsigned int cpu;
90         unsigned int this_cpu = smp_processor_id();
91         cpumask_t mask = cpu_online_map;
92
93         cpu_clear(this_cpu, mask);
94         for_each_cpu_mask(cpu, mask)
95                 platform_send_ipi_cpu(cpu, IRQ_SUPPLE_0);
96 }
97
98 # else
99 static void bfin_idle_cpu(void) {}
100 static void bfin_wakeup_cpu(void) {}
101 # endif
102
103 static int
104 vreg_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
105 {
106         struct cpufreq_freqs *freq = data;
107
108         if (freq->cpu != CPUFREQ_CPU)
109                 return 0;
110
111         if (val == CPUFREQ_PRECHANGE && freq->old < freq->new) {
112                 bfin_idle_cpu();
113                 bfin_set_vlev(bfin_get_vlev(freq->new));
114                 udelay(pdata->vr_settling_time); /* Wait until Volatge settled */
115                 bfin_wakeup_cpu();
116         } else if (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) {
117                 bfin_idle_cpu();
118                 bfin_set_vlev(bfin_get_vlev(freq->new));
119                 bfin_wakeup_cpu();
120         }
121
122         return 0;
123 }
124
125 static struct notifier_block vreg_cpufreq_notifier_block = {
126         .notifier_call  = vreg_cpufreq_notifier
127 };
128 #endif /* CONFIG_CPU_FREQ */
129
130 /**
131  *      bfin_dpmc_probe -
132  *
133  */
134 static int __devinit bfin_dpmc_probe(struct platform_device *pdev)
135 {
136         if (pdev->dev.platform_data)
137                 pdata = pdev->dev.platform_data;
138         else
139                 return -EINVAL;
140
141         return cpufreq_register_notifier(&vreg_cpufreq_notifier_block,
142                                          CPUFREQ_TRANSITION_NOTIFIER);
143 }
144
145 /**
146  *      bfin_dpmc_remove -
147  */
148 static int __devexit bfin_dpmc_remove(struct platform_device *pdev)
149 {
150         pdata = NULL;
151         return cpufreq_unregister_notifier(&vreg_cpufreq_notifier_block,
152                                          CPUFREQ_TRANSITION_NOTIFIER);
153 }
154
155 struct platform_driver bfin_dpmc_device_driver = {
156         .probe   = bfin_dpmc_probe,
157         .remove  = __devexit_p(bfin_dpmc_remove),
158         .driver  = {
159                 .name = DRIVER_NAME,
160         }
161 };
162
163 /**
164  *      bfin_dpmc_init - Init driver
165  */
166 static int __init bfin_dpmc_init(void)
167 {
168         return platform_driver_register(&bfin_dpmc_device_driver);
169 }
170 module_init(bfin_dpmc_init);
171
172 /**
173  *      bfin_dpmc_exit - break down driver
174  */
175 static void __exit bfin_dpmc_exit(void)
176 {
177         platform_driver_unregister(&bfin_dpmc_device_driver);
178 }
179 module_exit(bfin_dpmc_exit);
180
181 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
182 MODULE_DESCRIPTION("cpu power management driver for Blackfin");
183 MODULE_LICENSE("GPL");