123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/types.h>
- #include <linux/sched.h>
- #include <linux/cpufreq.h>
- #include <linux/delay.h>
- #include <linux/init.h>
- #include <linux/err.h>
- #include <linux/clk.h>
- #include <linux/io.h>
- static struct cpufreq_frequency_table freq_table[] = {
- { .frequency = 216000 },
- { .frequency = 312000 },
- { .frequency = 456000 },
- { .frequency = 608000 },
- { .frequency = 760000 },
- { .frequency = 816000 },
- { .frequency = 912000 },
- { .frequency = 1000000 },
- { .frequency = CPUFREQ_TABLE_END },
- };
- #define NUM_CPUS 2
- static struct clk *cpu_clk;
- static struct clk *pll_x_clk;
- static struct clk *pll_p_clk;
- static struct clk *emc_clk;
- static bool pll_x_prepared;
- static unsigned int tegra_get_intermediate(struct cpufreq_policy *policy,
- unsigned int index)
- {
- unsigned int ifreq = clk_get_rate(pll_p_clk) / 1000;
-
- if ((freq_table[index].frequency == ifreq) || (policy->cur == ifreq))
- return 0;
- return ifreq;
- }
- static int tegra_target_intermediate(struct cpufreq_policy *policy,
- unsigned int index)
- {
- int ret;
-
- clk_prepare_enable(pll_x_clk);
- ret = clk_set_parent(cpu_clk, pll_p_clk);
- if (ret)
- clk_disable_unprepare(pll_x_clk);
- else
- pll_x_prepared = true;
- return ret;
- }
- static int tegra_target(struct cpufreq_policy *policy, unsigned int index)
- {
- unsigned long rate = freq_table[index].frequency;
- unsigned int ifreq = clk_get_rate(pll_p_clk) / 1000;
- int ret = 0;
-
- if (rate >= 816000)
- clk_set_rate(emc_clk, 600000000);
- else if (rate >= 456000)
- clk_set_rate(emc_clk, 300000000);
- else
- clk_set_rate(emc_clk, 100000000);
-
- if (rate == ifreq)
- return clk_set_parent(cpu_clk, pll_p_clk);
- ret = clk_set_rate(pll_x_clk, rate * 1000);
-
- if (ret)
- pr_err("Failed to change pll_x to %lu\n", rate);
- ret = clk_set_parent(cpu_clk, pll_x_clk);
-
- WARN_ON(ret);
-
- if (pll_x_prepared) {
- clk_disable_unprepare(pll_x_clk);
- pll_x_prepared = false;
- }
- return ret;
- }
- static int tegra_cpu_init(struct cpufreq_policy *policy)
- {
- int ret;
- if (policy->cpu >= NUM_CPUS)
- return -EINVAL;
- clk_prepare_enable(emc_clk);
- clk_prepare_enable(cpu_clk);
-
- ret = cpufreq_generic_init(policy, freq_table, 300 * 1000);
- if (ret) {
- clk_disable_unprepare(cpu_clk);
- clk_disable_unprepare(emc_clk);
- return ret;
- }
- policy->clk = cpu_clk;
- policy->suspend_freq = freq_table[0].frequency;
- return 0;
- }
- static int tegra_cpu_exit(struct cpufreq_policy *policy)
- {
- clk_disable_unprepare(cpu_clk);
- clk_disable_unprepare(emc_clk);
- return 0;
- }
- static struct cpufreq_driver tegra_cpufreq_driver = {
- .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
- .verify = cpufreq_generic_frequency_table_verify,
- .get_intermediate = tegra_get_intermediate,
- .target_intermediate = tegra_target_intermediate,
- .target_index = tegra_target,
- .get = cpufreq_generic_get,
- .init = tegra_cpu_init,
- .exit = tegra_cpu_exit,
- .name = "tegra",
- .attr = cpufreq_generic_attr,
- .suspend = cpufreq_generic_suspend,
- };
- static int __init tegra_cpufreq_init(void)
- {
- cpu_clk = clk_get_sys(NULL, "cclk");
- if (IS_ERR(cpu_clk))
- return PTR_ERR(cpu_clk);
- pll_x_clk = clk_get_sys(NULL, "pll_x");
- if (IS_ERR(pll_x_clk))
- return PTR_ERR(pll_x_clk);
- pll_p_clk = clk_get_sys(NULL, "pll_p");
- if (IS_ERR(pll_p_clk))
- return PTR_ERR(pll_p_clk);
- emc_clk = clk_get_sys("cpu", "emc");
- if (IS_ERR(emc_clk)) {
- clk_put(cpu_clk);
- return PTR_ERR(emc_clk);
- }
- return cpufreq_register_driver(&tegra_cpufreq_driver);
- }
- static void __exit tegra_cpufreq_exit(void)
- {
- cpufreq_unregister_driver(&tegra_cpufreq_driver);
- clk_put(emc_clk);
- clk_put(cpu_clk);
- }
- MODULE_AUTHOR("Colin Cross <ccross@android.com>");
- MODULE_DESCRIPTION("cpufreq driver for Nvidia Tegra2");
- MODULE_LICENSE("GPL");
- module_init(tegra_cpufreq_init);
- module_exit(tegra_cpufreq_exit);
|