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