xref: /openbmc/linux/drivers/cpufreq/omap-cpufreq.c (revision c1b547bc222f4027d9394b6bd8f4a6bb0bd7b1b4)
1731e0cc6SSantosh Shilimkar /*
2ffe4f0f1SNishanth Menon  *  CPU frequency scaling for OMAP using OPP information
3731e0cc6SSantosh Shilimkar  *
4731e0cc6SSantosh Shilimkar  *  Copyright (C) 2005 Nokia Corporation
5731e0cc6SSantosh Shilimkar  *  Written by Tony Lindgren <tony@atomide.com>
6731e0cc6SSantosh Shilimkar  *
7731e0cc6SSantosh Shilimkar  *  Based on cpu-sa1110.c, Copyright (C) 2001 Russell King
8731e0cc6SSantosh Shilimkar  *
9731e0cc6SSantosh Shilimkar  * Copyright (C) 2007-2011 Texas Instruments, Inc.
10731e0cc6SSantosh Shilimkar  * - OMAP3/4 support by Rajendra Nayak, Santosh Shilimkar
11731e0cc6SSantosh Shilimkar  *
12731e0cc6SSantosh Shilimkar  * This program is free software; you can redistribute it and/or modify
13731e0cc6SSantosh Shilimkar  * it under the terms of the GNU General Public License version 2 as
14731e0cc6SSantosh Shilimkar  * published by the Free Software Foundation.
15731e0cc6SSantosh Shilimkar  */
16731e0cc6SSantosh Shilimkar #include <linux/types.h>
17731e0cc6SSantosh Shilimkar #include <linux/kernel.h>
18731e0cc6SSantosh Shilimkar #include <linux/sched.h>
19731e0cc6SSantosh Shilimkar #include <linux/cpufreq.h>
20731e0cc6SSantosh Shilimkar #include <linux/delay.h>
21731e0cc6SSantosh Shilimkar #include <linux/init.h>
22731e0cc6SSantosh Shilimkar #include <linux/err.h>
23731e0cc6SSantosh Shilimkar #include <linux/clk.h>
24731e0cc6SSantosh Shilimkar #include <linux/io.h>
25731e0cc6SSantosh Shilimkar #include <linux/opp.h>
2646c12216SRussell King #include <linux/cpu.h>
27*c1b547bcSKevin Hilman #include <linux/module.h>
28731e0cc6SSantosh Shilimkar 
29731e0cc6SSantosh Shilimkar #include <asm/system.h>
30731e0cc6SSantosh Shilimkar #include <asm/smp_plat.h>
3146c12216SRussell King #include <asm/cpu.h>
32731e0cc6SSantosh Shilimkar 
33731e0cc6SSantosh Shilimkar #include <plat/clock.h>
34731e0cc6SSantosh Shilimkar #include <plat/omap-pm.h>
35731e0cc6SSantosh Shilimkar #include <plat/common.h>
36*c1b547bcSKevin Hilman #include <plat/omap_device.h>
37731e0cc6SSantosh Shilimkar 
38731e0cc6SSantosh Shilimkar #include <mach/hardware.h>
39731e0cc6SSantosh Shilimkar 
4046c12216SRussell King #ifdef CONFIG_SMP
4146c12216SRussell King struct lpj_info {
4246c12216SRussell King 	unsigned long	ref;
4346c12216SRussell King 	unsigned int	freq;
4446c12216SRussell King };
4546c12216SRussell King 
4646c12216SRussell King static DEFINE_PER_CPU(struct lpj_info, lpj_ref);
4746c12216SRussell King static struct lpj_info global_lpj_ref;
4846c12216SRussell King #endif
4946c12216SRussell King 
50731e0cc6SSantosh Shilimkar static struct cpufreq_frequency_table *freq_table;
511c78217fSNishanth Menon static atomic_t freq_table_users = ATOMIC_INIT(0);
52731e0cc6SSantosh Shilimkar static struct clk *mpu_clk;
5308ca3e3bSNishanth Menon static char *mpu_clk_name;
54a820ffa8SNishanth Menon static struct device *mpu_dev;
55731e0cc6SSantosh Shilimkar 
56731e0cc6SSantosh Shilimkar static int omap_verify_speed(struct cpufreq_policy *policy)
57731e0cc6SSantosh Shilimkar {
58bf2a359dSNishanth Menon 	if (!freq_table)
59731e0cc6SSantosh Shilimkar 		return -EINVAL;
60bf2a359dSNishanth Menon 	return cpufreq_frequency_table_verify(policy, freq_table);
61731e0cc6SSantosh Shilimkar }
62731e0cc6SSantosh Shilimkar 
63731e0cc6SSantosh Shilimkar static unsigned int omap_getspeed(unsigned int cpu)
64731e0cc6SSantosh Shilimkar {
65731e0cc6SSantosh Shilimkar 	unsigned long rate;
66731e0cc6SSantosh Shilimkar 
6746c12216SRussell King 	if (cpu >= NR_CPUS)
68731e0cc6SSantosh Shilimkar 		return 0;
69731e0cc6SSantosh Shilimkar 
70731e0cc6SSantosh Shilimkar 	rate = clk_get_rate(mpu_clk) / 1000;
71731e0cc6SSantosh Shilimkar 	return rate;
72731e0cc6SSantosh Shilimkar }
73731e0cc6SSantosh Shilimkar 
74731e0cc6SSantosh Shilimkar static int omap_target(struct cpufreq_policy *policy,
75731e0cc6SSantosh Shilimkar 		       unsigned int target_freq,
76731e0cc6SSantosh Shilimkar 		       unsigned int relation)
77731e0cc6SSantosh Shilimkar {
78bf2a359dSNishanth Menon 	unsigned int i;
79bf2a359dSNishanth Menon 	int ret = 0;
80731e0cc6SSantosh Shilimkar 	struct cpufreq_freqs freqs;
81731e0cc6SSantosh Shilimkar 
82bf2a359dSNishanth Menon 	if (!freq_table) {
83bf2a359dSNishanth Menon 		dev_err(mpu_dev, "%s: cpu%d: no freq table!\n", __func__,
84bf2a359dSNishanth Menon 				policy->cpu);
85bf2a359dSNishanth Menon 		return -EINVAL;
86bf2a359dSNishanth Menon 	}
87bf2a359dSNishanth Menon 
88bf2a359dSNishanth Menon 	ret = cpufreq_frequency_table_target(policy, freq_table, target_freq,
89bf2a359dSNishanth Menon 			relation, &i);
90bf2a359dSNishanth Menon 	if (ret) {
91bf2a359dSNishanth Menon 		dev_dbg(mpu_dev, "%s: cpu%d: no freq match for %d(ret=%d)\n",
92bf2a359dSNishanth Menon 			__func__, policy->cpu, target_freq, ret);
93bf2a359dSNishanth Menon 		return ret;
94bf2a359dSNishanth Menon 	}
95bf2a359dSNishanth Menon 	freqs.new = freq_table[i].frequency;
96bf2a359dSNishanth Menon 	if (!freqs.new) {
97bf2a359dSNishanth Menon 		dev_err(mpu_dev, "%s: cpu%d: no match for freq %d\n", __func__,
98bf2a359dSNishanth Menon 			policy->cpu, target_freq);
99bf2a359dSNishanth Menon 		return -EINVAL;
100bf2a359dSNishanth Menon 	}
101731e0cc6SSantosh Shilimkar 
10246c12216SRussell King 	freqs.old = omap_getspeed(policy->cpu);
10346c12216SRussell King 	freqs.cpu = policy->cpu;
104731e0cc6SSantosh Shilimkar 
105022ac03bSColin Cross 	if (freqs.old == freqs.new && policy->cur == freqs.new)
106731e0cc6SSantosh Shilimkar 		return ret;
107731e0cc6SSantosh Shilimkar 
10846c12216SRussell King 	/* notifiers */
10946c12216SRussell King 	for_each_cpu(i, policy->cpus) {
11046c12216SRussell King 		freqs.cpu = i;
111731e0cc6SSantosh Shilimkar 		cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
11246c12216SRussell King 	}
113731e0cc6SSantosh Shilimkar 
114731e0cc6SSantosh Shilimkar #ifdef CONFIG_CPU_FREQ_DEBUG
115731e0cc6SSantosh Shilimkar 	pr_info("cpufreq-omap: transition: %u --> %u\n", freqs.old, freqs.new);
116731e0cc6SSantosh Shilimkar #endif
117731e0cc6SSantosh Shilimkar 
118731e0cc6SSantosh Shilimkar 	ret = clk_set_rate(mpu_clk, freqs.new * 1000);
11946c12216SRussell King 	freqs.new = omap_getspeed(policy->cpu);
120731e0cc6SSantosh Shilimkar 
12146c12216SRussell King #ifdef CONFIG_SMP
12246c12216SRussell King 	/*
12346c12216SRussell King 	 * Note that loops_per_jiffy is not updated on SMP systems in
12446c12216SRussell King 	 * cpufreq driver. So, update the per-CPU loops_per_jiffy value
12546c12216SRussell King 	 * on frequency transition. We need to update all dependent CPUs.
12646c12216SRussell King 	 */
12746c12216SRussell King 	for_each_cpu(i, policy->cpus) {
12846c12216SRussell King 		struct lpj_info *lpj = &per_cpu(lpj_ref, i);
12946c12216SRussell King 		if (!lpj->freq) {
13046c12216SRussell King 			lpj->ref = per_cpu(cpu_data, i).loops_per_jiffy;
13146c12216SRussell King 			lpj->freq = freqs.old;
13246c12216SRussell King 		}
13346c12216SRussell King 
13446c12216SRussell King 		per_cpu(cpu_data, i).loops_per_jiffy =
13546c12216SRussell King 			cpufreq_scale(lpj->ref, lpj->freq, freqs.new);
13646c12216SRussell King 	}
13746c12216SRussell King 
13846c12216SRussell King 	/* And don't forget to adjust the global one */
13946c12216SRussell King 	if (!global_lpj_ref.freq) {
14046c12216SRussell King 		global_lpj_ref.ref = loops_per_jiffy;
14146c12216SRussell King 		global_lpj_ref.freq = freqs.old;
14246c12216SRussell King 	}
14346c12216SRussell King 	loops_per_jiffy = cpufreq_scale(global_lpj_ref.ref, global_lpj_ref.freq,
14446c12216SRussell King 					freqs.new);
14546c12216SRussell King #endif
14646c12216SRussell King 
14746c12216SRussell King 	/* notifiers */
14846c12216SRussell King 	for_each_cpu(i, policy->cpus) {
14946c12216SRussell King 		freqs.cpu = i;
150731e0cc6SSantosh Shilimkar 		cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
15146c12216SRussell King 	}
152731e0cc6SSantosh Shilimkar 
153731e0cc6SSantosh Shilimkar 	return ret;
154731e0cc6SSantosh Shilimkar }
155731e0cc6SSantosh Shilimkar 
1561c78217fSNishanth Menon static inline void freq_table_free(void)
1571c78217fSNishanth Menon {
1581c78217fSNishanth Menon 	if (atomic_dec_and_test(&freq_table_users))
1591c78217fSNishanth Menon 		opp_free_cpufreq_table(mpu_dev, &freq_table);
1601c78217fSNishanth Menon }
1611c78217fSNishanth Menon 
162731e0cc6SSantosh Shilimkar static int __cpuinit omap_cpu_init(struct cpufreq_policy *policy)
163731e0cc6SSantosh Shilimkar {
164731e0cc6SSantosh Shilimkar 	int result = 0;
165731e0cc6SSantosh Shilimkar 
16608ca3e3bSNishanth Menon 	mpu_clk = clk_get(NULL, mpu_clk_name);
167731e0cc6SSantosh Shilimkar 	if (IS_ERR(mpu_clk))
168731e0cc6SSantosh Shilimkar 		return PTR_ERR(mpu_clk);
169731e0cc6SSantosh Shilimkar 
17011e04fddSNishanth Menon 	if (policy->cpu >= NR_CPUS) {
17111e04fddSNishanth Menon 		result = -EINVAL;
17211e04fddSNishanth Menon 		goto fail_ck;
17311e04fddSNishanth Menon 	}
174731e0cc6SSantosh Shilimkar 
17546c12216SRussell King 	policy->cur = policy->min = policy->max = omap_getspeed(policy->cpu);
1761c78217fSNishanth Menon 
1771c78217fSNishanth Menon 	if (atomic_inc_return(&freq_table_users) == 1)
178bf2a359dSNishanth Menon 		result = opp_init_cpufreq_table(mpu_dev, &freq_table);
179731e0cc6SSantosh Shilimkar 
180bf2a359dSNishanth Menon 	if (result) {
181bf2a359dSNishanth Menon 		dev_err(mpu_dev, "%s: cpu%d: failed creating freq table[%d]\n",
182bf2a359dSNishanth Menon 				__func__, policy->cpu, result);
18311e04fddSNishanth Menon 		goto fail_ck;
184bf2a359dSNishanth Menon 	}
185bf2a359dSNishanth Menon 
186731e0cc6SSantosh Shilimkar 	result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
1871c78217fSNishanth Menon 	if (result)
1881c78217fSNishanth Menon 		goto fail_table;
1891c78217fSNishanth Menon 
190bf2a359dSNishanth Menon 	cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
191731e0cc6SSantosh Shilimkar 
192731e0cc6SSantosh Shilimkar 	policy->min = policy->cpuinfo.min_freq;
193731e0cc6SSantosh Shilimkar 	policy->max = policy->cpuinfo.max_freq;
19446c12216SRussell King 	policy->cur = omap_getspeed(policy->cpu);
19546c12216SRussell King 
19646c12216SRussell King 	/*
19746c12216SRussell King 	 * On OMAP SMP configuartion, both processors share the voltage
19846c12216SRussell King 	 * and clock. So both CPUs needs to be scaled together and hence
19946c12216SRussell King 	 * needs software co-ordination. Use cpufreq affected_cpus
20046c12216SRussell King 	 * interface to handle this scenario. Additional is_smp() check
20146c12216SRussell King 	 * is to keep SMP_ON_UP build working.
20246c12216SRussell King 	 */
20346c12216SRussell King 	if (is_smp()) {
20446c12216SRussell King 		policy->shared_type = CPUFREQ_SHARED_TYPE_ANY;
205ed8ce00cSTodd Poynor 		cpumask_setall(policy->cpus);
20646c12216SRussell King 	}
207731e0cc6SSantosh Shilimkar 
208731e0cc6SSantosh Shilimkar 	/* FIXME: what's the actual transition time? */
209731e0cc6SSantosh Shilimkar 	policy->cpuinfo.transition_latency = 300 * 1000;
210731e0cc6SSantosh Shilimkar 
211731e0cc6SSantosh Shilimkar 	return 0;
21211e04fddSNishanth Menon 
2131c78217fSNishanth Menon fail_table:
2141c78217fSNishanth Menon 	freq_table_free();
21511e04fddSNishanth Menon fail_ck:
21611e04fddSNishanth Menon 	clk_put(mpu_clk);
21711e04fddSNishanth Menon 	return result;
218731e0cc6SSantosh Shilimkar }
219731e0cc6SSantosh Shilimkar 
220731e0cc6SSantosh Shilimkar static int omap_cpu_exit(struct cpufreq_policy *policy)
221731e0cc6SSantosh Shilimkar {
2221c78217fSNishanth Menon 	freq_table_free();
223731e0cc6SSantosh Shilimkar 	clk_put(mpu_clk);
224731e0cc6SSantosh Shilimkar 	return 0;
225731e0cc6SSantosh Shilimkar }
226731e0cc6SSantosh Shilimkar 
227731e0cc6SSantosh Shilimkar static struct freq_attr *omap_cpufreq_attr[] = {
228731e0cc6SSantosh Shilimkar 	&cpufreq_freq_attr_scaling_available_freqs,
229731e0cc6SSantosh Shilimkar 	NULL,
230731e0cc6SSantosh Shilimkar };
231731e0cc6SSantosh Shilimkar 
232731e0cc6SSantosh Shilimkar static struct cpufreq_driver omap_driver = {
233731e0cc6SSantosh Shilimkar 	.flags		= CPUFREQ_STICKY,
234731e0cc6SSantosh Shilimkar 	.verify		= omap_verify_speed,
235731e0cc6SSantosh Shilimkar 	.target		= omap_target,
236731e0cc6SSantosh Shilimkar 	.get		= omap_getspeed,
237731e0cc6SSantosh Shilimkar 	.init		= omap_cpu_init,
238731e0cc6SSantosh Shilimkar 	.exit		= omap_cpu_exit,
239731e0cc6SSantosh Shilimkar 	.name		= "omap",
240731e0cc6SSantosh Shilimkar 	.attr		= omap_cpufreq_attr,
241731e0cc6SSantosh Shilimkar };
242731e0cc6SSantosh Shilimkar 
243731e0cc6SSantosh Shilimkar static int __init omap_cpufreq_init(void)
244731e0cc6SSantosh Shilimkar {
24508ca3e3bSNishanth Menon 	if (cpu_is_omap24xx())
24608ca3e3bSNishanth Menon 		mpu_clk_name = "virt_prcm_set";
24708ca3e3bSNishanth Menon 	else if (cpu_is_omap34xx())
24808ca3e3bSNishanth Menon 		mpu_clk_name = "dpll1_ck";
24908ca3e3bSNishanth Menon 	else if (cpu_is_omap44xx())
25008ca3e3bSNishanth Menon 		mpu_clk_name = "dpll_mpu_ck";
25108ca3e3bSNishanth Menon 
25208ca3e3bSNishanth Menon 	if (!mpu_clk_name) {
25308ca3e3bSNishanth Menon 		pr_err("%s: unsupported Silicon?\n", __func__);
25408ca3e3bSNishanth Menon 		return -EINVAL;
25508ca3e3bSNishanth Menon 	}
256a820ffa8SNishanth Menon 
257*c1b547bcSKevin Hilman 	mpu_dev = omap_device_get_by_hwmod_name("mpu");
258a820ffa8SNishanth Menon 	if (!mpu_dev) {
259a820ffa8SNishanth Menon 		pr_warning("%s: unable to get the mpu device\n", __func__);
260a820ffa8SNishanth Menon 		return -EINVAL;
261a820ffa8SNishanth Menon 	}
262a820ffa8SNishanth Menon 
263731e0cc6SSantosh Shilimkar 	return cpufreq_register_driver(&omap_driver);
264731e0cc6SSantosh Shilimkar }
265731e0cc6SSantosh Shilimkar 
266731e0cc6SSantosh Shilimkar static void __exit omap_cpufreq_exit(void)
267731e0cc6SSantosh Shilimkar {
268731e0cc6SSantosh Shilimkar 	cpufreq_unregister_driver(&omap_driver);
269731e0cc6SSantosh Shilimkar }
270731e0cc6SSantosh Shilimkar 
271731e0cc6SSantosh Shilimkar MODULE_DESCRIPTION("cpufreq driver for OMAP SoCs");
272731e0cc6SSantosh Shilimkar MODULE_LICENSE("GPL");
273731e0cc6SSantosh Shilimkar module_init(omap_cpufreq_init);
274731e0cc6SSantosh Shilimkar module_exit(omap_cpufreq_exit);
275