Linux 3.17-rc2
[linux/fpc-iii.git] / arch / blackfin / mach-common / dpmc.c
blob724a8c5f5578d14a69f3dcda0933e165f379d21c
1 /*
2 * Copyright 2008 Analog Devices Inc.
4 * Licensed under the GPL-2 or later.
5 */
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>
17 #include <asm/delay.h>
18 #include <asm/dpmc.h>
20 #define DRIVER_NAME "bfin dpmc"
22 struct bfin_dpmc_platform_data *pdata;
24 /**
25 * bfin_set_vlev - Update VLEV field in VR_CTL Reg.
26 * Avoid BYPASS sequence
28 static void bfin_set_vlev(unsigned int vlev)
30 unsigned pll_lcnt;
32 pll_lcnt = bfin_read_PLL_LOCKCNT();
34 bfin_write_PLL_LOCKCNT(1);
35 bfin_write_VR_CTL((bfin_read_VR_CTL() & ~VLEV) | vlev);
36 bfin_write_PLL_LOCKCNT(pll_lcnt);
39 /**
40 * bfin_get_vlev - Get CPU specific VLEV from platform device data
42 static unsigned int bfin_get_vlev(unsigned int freq)
44 int i;
46 if (!pdata)
47 goto err_out;
49 freq >>= 16;
51 for (i = 0; i < pdata->tabsize; i++)
52 if (freq <= (pdata->tuple_tab[i] & 0xFFFF))
53 return pdata->tuple_tab[i] >> 16;
55 err_out:
56 printk(KERN_WARNING "DPMC: No suitable CCLK VDDINT voltage pair found\n");
57 return VLEV_120;
60 #ifdef CONFIG_CPU_FREQ
61 # ifdef CONFIG_SMP
62 static void bfin_idle_this_cpu(void *info)
64 unsigned long flags = 0;
65 unsigned long iwr0, iwr1, iwr2;
66 unsigned int cpu = smp_processor_id();
68 local_irq_save_hw(flags);
69 bfin_iwr_set_sup0(&iwr0, &iwr1, &iwr2);
71 platform_clear_ipi(cpu, IRQ_SUPPLE_0);
72 SSYNC();
73 asm("IDLE;");
74 bfin_iwr_restore(iwr0, iwr1, iwr2);
76 local_irq_restore_hw(flags);
79 static void bfin_idle_cpu(void)
81 smp_call_function(bfin_idle_this_cpu, NULL, 0);
84 static void bfin_wakeup_cpu(void)
86 unsigned int cpu;
87 unsigned int this_cpu = smp_processor_id();
88 cpumask_t mask;
90 cpumask_copy(&mask, cpu_online_mask);
91 cpumask_clear_cpu(this_cpu, &mask);
92 for_each_cpu(cpu, &mask)
93 platform_send_ipi_cpu(cpu, IRQ_SUPPLE_0);
96 # else
97 static void bfin_idle_cpu(void) {}
98 static void bfin_wakeup_cpu(void) {}
99 # endif
101 static int
102 vreg_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
104 struct cpufreq_freqs *freq = data;
106 if (freq->cpu != CPUFREQ_CPU)
107 return 0;
109 if (val == CPUFREQ_PRECHANGE && freq->old < freq->new) {
110 bfin_idle_cpu();
111 bfin_set_vlev(bfin_get_vlev(freq->new));
112 udelay(pdata->vr_settling_time); /* Wait until Volatge settled */
113 bfin_wakeup_cpu();
114 } else if (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) {
115 bfin_idle_cpu();
116 bfin_set_vlev(bfin_get_vlev(freq->new));
117 bfin_wakeup_cpu();
120 return 0;
123 static struct notifier_block vreg_cpufreq_notifier_block = {
124 .notifier_call = vreg_cpufreq_notifier
126 #endif /* CONFIG_CPU_FREQ */
129 * bfin_dpmc_probe -
132 static int bfin_dpmc_probe(struct platform_device *pdev)
134 if (pdev->dev.platform_data)
135 pdata = pdev->dev.platform_data;
136 else
137 return -EINVAL;
139 return cpufreq_register_notifier(&vreg_cpufreq_notifier_block,
140 CPUFREQ_TRANSITION_NOTIFIER);
144 * bfin_dpmc_remove -
146 static int bfin_dpmc_remove(struct platform_device *pdev)
148 pdata = NULL;
149 return cpufreq_unregister_notifier(&vreg_cpufreq_notifier_block,
150 CPUFREQ_TRANSITION_NOTIFIER);
153 struct platform_driver bfin_dpmc_device_driver = {
154 .probe = bfin_dpmc_probe,
155 .remove = bfin_dpmc_remove,
156 .driver = {
157 .name = DRIVER_NAME,
160 module_platform_driver(bfin_dpmc_device_driver);
162 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
163 MODULE_DESCRIPTION("cpu power management driver for Blackfin");
164 MODULE_LICENSE("GPL");