of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / cpufreq / davinci-cpufreq.c
blob7e336d20c184b4a175ab196b2583ddd50069d475
1 /*
2 * CPU frequency scaling for DaVinci
4 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
6 * Based on linux/arch/arm/plat-omap/cpu-omap.c. Original Copyright follows:
8 * Copyright (C) 2005 Nokia Corporation
9 * Written by Tony Lindgren <tony@atomide.com>
11 * Based on cpu-sa1110.c, Copyright (C) 2001 Russell King
13 * Copyright (C) 2007-2008 Texas Instruments, Inc.
14 * Updated to support OMAP3
15 * Rajendra Nayak <rnayak@ti.com>
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
21 #include <linux/types.h>
22 #include <linux/cpufreq.h>
23 #include <linux/init.h>
24 #include <linux/err.h>
25 #include <linux/clk.h>
26 #include <linux/platform_device.h>
27 #include <linux/export.h>
29 #include <mach/hardware.h>
30 #include <mach/cpufreq.h>
31 #include <mach/common.h>
33 struct davinci_cpufreq {
34 struct device *dev;
35 struct clk *armclk;
36 struct clk *asyncclk;
37 unsigned long asyncrate;
39 static struct davinci_cpufreq cpufreq;
41 static int davinci_verify_speed(struct cpufreq_policy *policy)
43 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
44 struct cpufreq_frequency_table *freq_table = pdata->freq_table;
45 struct clk *armclk = cpufreq.armclk;
47 if (freq_table)
48 return cpufreq_frequency_table_verify(policy, freq_table);
50 if (policy->cpu)
51 return -EINVAL;
53 cpufreq_verify_within_cpu_limits(policy);
54 policy->min = clk_round_rate(armclk, policy->min * 1000) / 1000;
55 policy->max = clk_round_rate(armclk, policy->max * 1000) / 1000;
56 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
57 policy->cpuinfo.max_freq);
58 return 0;
61 static int davinci_target(struct cpufreq_policy *policy, unsigned int idx)
63 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
64 struct clk *armclk = cpufreq.armclk;
65 unsigned int old_freq, new_freq;
66 int ret = 0;
68 old_freq = policy->cur;
69 new_freq = pdata->freq_table[idx].frequency;
71 /* if moving to higher frequency, up the voltage beforehand */
72 if (pdata->set_voltage && new_freq > old_freq) {
73 ret = pdata->set_voltage(idx);
74 if (ret)
75 return ret;
78 ret = clk_set_rate(armclk, idx);
79 if (ret)
80 return ret;
82 if (cpufreq.asyncclk) {
83 ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
84 if (ret)
85 return ret;
88 /* if moving to lower freq, lower the voltage after lowering freq */
89 if (pdata->set_voltage && new_freq < old_freq)
90 pdata->set_voltage(idx);
92 return 0;
95 static int davinci_cpu_init(struct cpufreq_policy *policy)
97 int result = 0;
98 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
99 struct cpufreq_frequency_table *freq_table = pdata->freq_table;
101 if (policy->cpu != 0)
102 return -EINVAL;
104 /* Finish platform specific initialization */
105 if (pdata->init) {
106 result = pdata->init();
107 if (result)
108 return result;
111 policy->clk = cpufreq.armclk;
114 * Time measurement across the target() function yields ~1500-1800us
115 * time taken with no drivers on notification list.
116 * Setting the latency to 2000 us to accommodate addition of drivers
117 * to pre/post change notification list.
119 return cpufreq_generic_init(policy, freq_table, 2000 * 1000);
122 static struct cpufreq_driver davinci_driver = {
123 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
124 .verify = davinci_verify_speed,
125 .target_index = davinci_target,
126 .get = cpufreq_generic_get,
127 .init = davinci_cpu_init,
128 .name = "davinci",
129 .attr = cpufreq_generic_attr,
132 static int __init davinci_cpufreq_probe(struct platform_device *pdev)
134 struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
135 struct clk *asyncclk;
137 if (!pdata)
138 return -EINVAL;
139 if (!pdata->freq_table)
140 return -EINVAL;
142 cpufreq.dev = &pdev->dev;
144 cpufreq.armclk = clk_get(NULL, "arm");
145 if (IS_ERR(cpufreq.armclk)) {
146 dev_err(cpufreq.dev, "Unable to get ARM clock\n");
147 return PTR_ERR(cpufreq.armclk);
150 asyncclk = clk_get(cpufreq.dev, "async");
151 if (!IS_ERR(asyncclk)) {
152 cpufreq.asyncclk = asyncclk;
153 cpufreq.asyncrate = clk_get_rate(asyncclk);
156 return cpufreq_register_driver(&davinci_driver);
159 static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
161 clk_put(cpufreq.armclk);
163 if (cpufreq.asyncclk)
164 clk_put(cpufreq.asyncclk);
166 return cpufreq_unregister_driver(&davinci_driver);
169 static struct platform_driver davinci_cpufreq_driver = {
170 .driver = {
171 .name = "cpufreq-davinci",
173 .remove = __exit_p(davinci_cpufreq_remove),
176 int __init davinci_cpufreq_init(void)
178 return platform_driver_probe(&davinci_cpufreq_driver,
179 davinci_cpufreq_probe);