xref: /openbmc/linux/drivers/cpufreq/davinci-cpufreq.c (revision 652ed95d5fa6074b3c4ea245deb0691f1acb6656)
18a7b1227SViresh Kumar /*
28a7b1227SViresh Kumar  * CPU frequency scaling for DaVinci
38a7b1227SViresh Kumar  *
48a7b1227SViresh Kumar  * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
58a7b1227SViresh Kumar  *
68a7b1227SViresh Kumar  * Based on linux/arch/arm/plat-omap/cpu-omap.c. Original Copyright follows:
78a7b1227SViresh Kumar  *
88a7b1227SViresh Kumar  *  Copyright (C) 2005 Nokia Corporation
98a7b1227SViresh Kumar  *  Written by Tony Lindgren <tony@atomide.com>
108a7b1227SViresh Kumar  *
118a7b1227SViresh Kumar  *  Based on cpu-sa1110.c, Copyright (C) 2001 Russell King
128a7b1227SViresh Kumar  *
138a7b1227SViresh Kumar  * Copyright (C) 2007-2008 Texas Instruments, Inc.
148a7b1227SViresh Kumar  * Updated to support OMAP3
158a7b1227SViresh Kumar  * Rajendra Nayak <rnayak@ti.com>
168a7b1227SViresh Kumar  *
178a7b1227SViresh Kumar  * This program is free software; you can redistribute it and/or modify
188a7b1227SViresh Kumar  * it under the terms of the GNU General Public License version 2 as
198a7b1227SViresh Kumar  * published by the Free Software Foundation.
208a7b1227SViresh Kumar  */
218a7b1227SViresh Kumar #include <linux/types.h>
228a7b1227SViresh Kumar #include <linux/cpufreq.h>
238a7b1227SViresh Kumar #include <linux/init.h>
248a7b1227SViresh Kumar #include <linux/err.h>
258a7b1227SViresh Kumar #include <linux/clk.h>
268a7b1227SViresh Kumar #include <linux/platform_device.h>
278a7b1227SViresh Kumar #include <linux/export.h>
288a7b1227SViresh Kumar 
298a7b1227SViresh Kumar #include <mach/hardware.h>
308a7b1227SViresh Kumar #include <mach/cpufreq.h>
318a7b1227SViresh Kumar #include <mach/common.h>
328a7b1227SViresh Kumar 
338a7b1227SViresh Kumar struct davinci_cpufreq {
348a7b1227SViresh Kumar 	struct device *dev;
358a7b1227SViresh Kumar 	struct clk *armclk;
368a7b1227SViresh Kumar 	struct clk *asyncclk;
378a7b1227SViresh Kumar 	unsigned long asyncrate;
388a7b1227SViresh Kumar };
398a7b1227SViresh Kumar static struct davinci_cpufreq cpufreq;
408a7b1227SViresh Kumar 
418a7b1227SViresh Kumar static int davinci_verify_speed(struct cpufreq_policy *policy)
428a7b1227SViresh Kumar {
438a7b1227SViresh Kumar 	struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
448a7b1227SViresh Kumar 	struct cpufreq_frequency_table *freq_table = pdata->freq_table;
458a7b1227SViresh Kumar 	struct clk *armclk = cpufreq.armclk;
468a7b1227SViresh Kumar 
478a7b1227SViresh Kumar 	if (freq_table)
488a7b1227SViresh Kumar 		return cpufreq_frequency_table_verify(policy, freq_table);
498a7b1227SViresh Kumar 
508a7b1227SViresh Kumar 	if (policy->cpu)
518a7b1227SViresh Kumar 		return -EINVAL;
528a7b1227SViresh Kumar 
53be49e346SViresh Kumar 	cpufreq_verify_within_cpu_limits(policy);
548a7b1227SViresh Kumar 	policy->min = clk_round_rate(armclk, policy->min * 1000) / 1000;
558a7b1227SViresh Kumar 	policy->max = clk_round_rate(armclk, policy->max * 1000) / 1000;
568a7b1227SViresh Kumar 	cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
578a7b1227SViresh Kumar 						policy->cpuinfo.max_freq);
588a7b1227SViresh Kumar 	return 0;
598a7b1227SViresh Kumar }
608a7b1227SViresh Kumar 
619c0ebcf7SViresh Kumar static int davinci_target(struct cpufreq_policy *policy, unsigned int idx)
628a7b1227SViresh Kumar {
638a7b1227SViresh Kumar 	struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
648a7b1227SViresh Kumar 	struct clk *armclk = cpufreq.armclk;
65d4019f0aSViresh Kumar 	unsigned int old_freq, new_freq;
66d4019f0aSViresh Kumar 	int ret = 0;
678a7b1227SViresh Kumar 
68*652ed95dSViresh Kumar 	old_freq = policy->cur;
69d4019f0aSViresh Kumar 	new_freq = pdata->freq_table[idx].frequency;
708a7b1227SViresh Kumar 
718a7b1227SViresh Kumar 	/* if moving to higher frequency, up the voltage beforehand */
72d4019f0aSViresh Kumar 	if (pdata->set_voltage && new_freq > old_freq) {
738a7b1227SViresh Kumar 		ret = pdata->set_voltage(idx);
748a7b1227SViresh Kumar 		if (ret)
75d4019f0aSViresh Kumar 			return ret;
768a7b1227SViresh Kumar 	}
778a7b1227SViresh Kumar 
788a7b1227SViresh Kumar 	ret = clk_set_rate(armclk, idx);
798a7b1227SViresh Kumar 	if (ret)
80d4019f0aSViresh Kumar 		return ret;
818a7b1227SViresh Kumar 
828a7b1227SViresh Kumar 	if (cpufreq.asyncclk) {
838a7b1227SViresh Kumar 		ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
848a7b1227SViresh Kumar 		if (ret)
85d4019f0aSViresh Kumar 			return ret;
868a7b1227SViresh Kumar 	}
878a7b1227SViresh Kumar 
888a7b1227SViresh Kumar 	/* if moving to lower freq, lower the voltage after lowering freq */
89d4019f0aSViresh Kumar 	if (pdata->set_voltage && new_freq < old_freq)
908a7b1227SViresh Kumar 		pdata->set_voltage(idx);
918a7b1227SViresh Kumar 
92d4019f0aSViresh Kumar 	return 0;
938a7b1227SViresh Kumar }
948a7b1227SViresh Kumar 
958a7b1227SViresh Kumar static int davinci_cpu_init(struct cpufreq_policy *policy)
968a7b1227SViresh Kumar {
978a7b1227SViresh Kumar 	int result = 0;
988a7b1227SViresh Kumar 	struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
998a7b1227SViresh Kumar 	struct cpufreq_frequency_table *freq_table = pdata->freq_table;
1008a7b1227SViresh Kumar 
1018a7b1227SViresh Kumar 	if (policy->cpu != 0)
1028a7b1227SViresh Kumar 		return -EINVAL;
1038a7b1227SViresh Kumar 
1048a7b1227SViresh Kumar 	/* Finish platform specific initialization */
1058a7b1227SViresh Kumar 	if (pdata->init) {
1068a7b1227SViresh Kumar 		result = pdata->init();
1078a7b1227SViresh Kumar 		if (result)
1088a7b1227SViresh Kumar 			return result;
1098a7b1227SViresh Kumar 	}
1108a7b1227SViresh Kumar 
111*652ed95dSViresh Kumar 	policy->clk = cpufreq.armclk;
112*652ed95dSViresh Kumar 
1138a7b1227SViresh Kumar 	/*
1148a7b1227SViresh Kumar 	 * Time measurement across the target() function yields ~1500-1800us
1158a7b1227SViresh Kumar 	 * time taken with no drivers on notification list.
1168a7b1227SViresh Kumar 	 * Setting the latency to 2000 us to accommodate addition of drivers
1178a7b1227SViresh Kumar 	 * to pre/post change notification list.
1188a7b1227SViresh Kumar 	 */
119af8c4cfaSViresh Kumar 	return cpufreq_generic_init(policy, freq_table, 2000 * 1000);
1208a7b1227SViresh Kumar }
1218a7b1227SViresh Kumar 
1228a7b1227SViresh Kumar static struct cpufreq_driver davinci_driver = {
123ae6b4271SViresh Kumar 	.flags		= CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
1248a7b1227SViresh Kumar 	.verify		= davinci_verify_speed,
1259c0ebcf7SViresh Kumar 	.target_index	= davinci_target,
126*652ed95dSViresh Kumar 	.get		= cpufreq_generic_get,
1278a7b1227SViresh Kumar 	.init		= davinci_cpu_init,
12839d0c362SViresh Kumar 	.exit		= cpufreq_generic_exit,
1298a7b1227SViresh Kumar 	.name		= "davinci",
13039d0c362SViresh Kumar 	.attr		= cpufreq_generic_attr,
1318a7b1227SViresh Kumar };
1328a7b1227SViresh Kumar 
1338a7b1227SViresh Kumar static int __init davinci_cpufreq_probe(struct platform_device *pdev)
1348a7b1227SViresh Kumar {
1358a7b1227SViresh Kumar 	struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
1368a7b1227SViresh Kumar 	struct clk *asyncclk;
1378a7b1227SViresh Kumar 
1388a7b1227SViresh Kumar 	if (!pdata)
1398a7b1227SViresh Kumar 		return -EINVAL;
1408a7b1227SViresh Kumar 	if (!pdata->freq_table)
1418a7b1227SViresh Kumar 		return -EINVAL;
1428a7b1227SViresh Kumar 
1438a7b1227SViresh Kumar 	cpufreq.dev = &pdev->dev;
1448a7b1227SViresh Kumar 
1458a7b1227SViresh Kumar 	cpufreq.armclk = clk_get(NULL, "arm");
1468a7b1227SViresh Kumar 	if (IS_ERR(cpufreq.armclk)) {
1478a7b1227SViresh Kumar 		dev_err(cpufreq.dev, "Unable to get ARM clock\n");
1488a7b1227SViresh Kumar 		return PTR_ERR(cpufreq.armclk);
1498a7b1227SViresh Kumar 	}
1508a7b1227SViresh Kumar 
1518a7b1227SViresh Kumar 	asyncclk = clk_get(cpufreq.dev, "async");
1528a7b1227SViresh Kumar 	if (!IS_ERR(asyncclk)) {
1538a7b1227SViresh Kumar 		cpufreq.asyncclk = asyncclk;
1548a7b1227SViresh Kumar 		cpufreq.asyncrate = clk_get_rate(asyncclk);
1558a7b1227SViresh Kumar 	}
1568a7b1227SViresh Kumar 
1578a7b1227SViresh Kumar 	return cpufreq_register_driver(&davinci_driver);
1588a7b1227SViresh Kumar }
1598a7b1227SViresh Kumar 
1608a7b1227SViresh Kumar static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
1618a7b1227SViresh Kumar {
1628a7b1227SViresh Kumar 	clk_put(cpufreq.armclk);
1638a7b1227SViresh Kumar 
1648a7b1227SViresh Kumar 	if (cpufreq.asyncclk)
1658a7b1227SViresh Kumar 		clk_put(cpufreq.asyncclk);
1668a7b1227SViresh Kumar 
1678a7b1227SViresh Kumar 	return cpufreq_unregister_driver(&davinci_driver);
1688a7b1227SViresh Kumar }
1698a7b1227SViresh Kumar 
1708a7b1227SViresh Kumar static struct platform_driver davinci_cpufreq_driver = {
1718a7b1227SViresh Kumar 	.driver = {
1728a7b1227SViresh Kumar 		.name	 = "cpufreq-davinci",
1738a7b1227SViresh Kumar 		.owner	 = THIS_MODULE,
1748a7b1227SViresh Kumar 	},
1758a7b1227SViresh Kumar 	.remove = __exit_p(davinci_cpufreq_remove),
1768a7b1227SViresh Kumar };
1778a7b1227SViresh Kumar 
1788a7b1227SViresh Kumar int __init davinci_cpufreq_init(void)
1798a7b1227SViresh Kumar {
1808a7b1227SViresh Kumar 	return platform_driver_probe(&davinci_cpufreq_driver,
1818a7b1227SViresh Kumar 							davinci_cpufreq_probe);
1828a7b1227SViresh Kumar }
1838a7b1227SViresh Kumar 
184