xref: /openbmc/linux/drivers/cpufreq/davinci-cpufreq.c (revision 9d4de2904ef0add3006df8896bfd07f534c44dd8)
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 
419c0ebcf7SViresh Kumar static int davinci_target(struct cpufreq_policy *policy, unsigned int idx)
428a7b1227SViresh Kumar {
438a7b1227SViresh Kumar 	struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
448a7b1227SViresh Kumar 	struct clk *armclk = cpufreq.armclk;
45d4019f0aSViresh Kumar 	unsigned int old_freq, new_freq;
46d4019f0aSViresh Kumar 	int ret = 0;
478a7b1227SViresh Kumar 
48652ed95dSViresh Kumar 	old_freq = policy->cur;
49d4019f0aSViresh Kumar 	new_freq = pdata->freq_table[idx].frequency;
508a7b1227SViresh Kumar 
518a7b1227SViresh Kumar 	/* if moving to higher frequency, up the voltage beforehand */
52d4019f0aSViresh Kumar 	if (pdata->set_voltage && new_freq > old_freq) {
538a7b1227SViresh Kumar 		ret = pdata->set_voltage(idx);
548a7b1227SViresh Kumar 		if (ret)
55d4019f0aSViresh Kumar 			return ret;
568a7b1227SViresh Kumar 	}
578a7b1227SViresh Kumar 
588a7b1227SViresh Kumar 	ret = clk_set_rate(armclk, idx);
598a7b1227SViresh Kumar 	if (ret)
60d4019f0aSViresh Kumar 		return ret;
618a7b1227SViresh Kumar 
628a7b1227SViresh Kumar 	if (cpufreq.asyncclk) {
638a7b1227SViresh Kumar 		ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
648a7b1227SViresh Kumar 		if (ret)
65d4019f0aSViresh Kumar 			return ret;
668a7b1227SViresh Kumar 	}
678a7b1227SViresh Kumar 
688a7b1227SViresh Kumar 	/* if moving to lower freq, lower the voltage after lowering freq */
69d4019f0aSViresh Kumar 	if (pdata->set_voltage && new_freq < old_freq)
708a7b1227SViresh Kumar 		pdata->set_voltage(idx);
718a7b1227SViresh Kumar 
72d4019f0aSViresh Kumar 	return 0;
738a7b1227SViresh Kumar }
748a7b1227SViresh Kumar 
758a7b1227SViresh Kumar static int davinci_cpu_init(struct cpufreq_policy *policy)
768a7b1227SViresh Kumar {
778a7b1227SViresh Kumar 	int result = 0;
788a7b1227SViresh Kumar 	struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
798a7b1227SViresh Kumar 	struct cpufreq_frequency_table *freq_table = pdata->freq_table;
808a7b1227SViresh Kumar 
818a7b1227SViresh Kumar 	if (policy->cpu != 0)
828a7b1227SViresh Kumar 		return -EINVAL;
838a7b1227SViresh Kumar 
848a7b1227SViresh Kumar 	/* Finish platform specific initialization */
858a7b1227SViresh Kumar 	if (pdata->init) {
868a7b1227SViresh Kumar 		result = pdata->init();
878a7b1227SViresh Kumar 		if (result)
888a7b1227SViresh Kumar 			return result;
898a7b1227SViresh Kumar 	}
908a7b1227SViresh Kumar 
91652ed95dSViresh Kumar 	policy->clk = cpufreq.armclk;
92652ed95dSViresh Kumar 
938a7b1227SViresh Kumar 	/*
948a7b1227SViresh Kumar 	 * Time measurement across the target() function yields ~1500-1800us
958a7b1227SViresh Kumar 	 * time taken with no drivers on notification list.
968a7b1227SViresh Kumar 	 * Setting the latency to 2000 us to accommodate addition of drivers
978a7b1227SViresh Kumar 	 * to pre/post change notification list.
988a7b1227SViresh Kumar 	 */
99af8c4cfaSViresh Kumar 	return cpufreq_generic_init(policy, freq_table, 2000 * 1000);
1008a7b1227SViresh Kumar }
1018a7b1227SViresh Kumar 
1028a7b1227SViresh Kumar static struct cpufreq_driver davinci_driver = {
103ae6b4271SViresh Kumar 	.flags		= CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
104*9d4de290SViresh Kumar 	.verify		= cpufreq_generic_frequency_table_verify,
1059c0ebcf7SViresh Kumar 	.target_index	= davinci_target,
106652ed95dSViresh Kumar 	.get		= cpufreq_generic_get,
1078a7b1227SViresh Kumar 	.init		= davinci_cpu_init,
1088a7b1227SViresh Kumar 	.name		= "davinci",
10939d0c362SViresh Kumar 	.attr		= cpufreq_generic_attr,
1108a7b1227SViresh Kumar };
1118a7b1227SViresh Kumar 
1128a7b1227SViresh Kumar static int __init davinci_cpufreq_probe(struct platform_device *pdev)
1138a7b1227SViresh Kumar {
1148a7b1227SViresh Kumar 	struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
1158a7b1227SViresh Kumar 	struct clk *asyncclk;
1168a7b1227SViresh Kumar 
1178a7b1227SViresh Kumar 	if (!pdata)
1188a7b1227SViresh Kumar 		return -EINVAL;
1198a7b1227SViresh Kumar 	if (!pdata->freq_table)
1208a7b1227SViresh Kumar 		return -EINVAL;
1218a7b1227SViresh Kumar 
1228a7b1227SViresh Kumar 	cpufreq.dev = &pdev->dev;
1238a7b1227SViresh Kumar 
1248a7b1227SViresh Kumar 	cpufreq.armclk = clk_get(NULL, "arm");
1258a7b1227SViresh Kumar 	if (IS_ERR(cpufreq.armclk)) {
1268a7b1227SViresh Kumar 		dev_err(cpufreq.dev, "Unable to get ARM clock\n");
1278a7b1227SViresh Kumar 		return PTR_ERR(cpufreq.armclk);
1288a7b1227SViresh Kumar 	}
1298a7b1227SViresh Kumar 
1308a7b1227SViresh Kumar 	asyncclk = clk_get(cpufreq.dev, "async");
1318a7b1227SViresh Kumar 	if (!IS_ERR(asyncclk)) {
1328a7b1227SViresh Kumar 		cpufreq.asyncclk = asyncclk;
1338a7b1227SViresh Kumar 		cpufreq.asyncrate = clk_get_rate(asyncclk);
1348a7b1227SViresh Kumar 	}
1358a7b1227SViresh Kumar 
1368a7b1227SViresh Kumar 	return cpufreq_register_driver(&davinci_driver);
1378a7b1227SViresh Kumar }
1388a7b1227SViresh Kumar 
1398a7b1227SViresh Kumar static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
1408a7b1227SViresh Kumar {
1418a7b1227SViresh Kumar 	clk_put(cpufreq.armclk);
1428a7b1227SViresh Kumar 
1438a7b1227SViresh Kumar 	if (cpufreq.asyncclk)
1448a7b1227SViresh Kumar 		clk_put(cpufreq.asyncclk);
1458a7b1227SViresh Kumar 
1468a7b1227SViresh Kumar 	return cpufreq_unregister_driver(&davinci_driver);
1478a7b1227SViresh Kumar }
1488a7b1227SViresh Kumar 
1498a7b1227SViresh Kumar static struct platform_driver davinci_cpufreq_driver = {
1508a7b1227SViresh Kumar 	.driver = {
1518a7b1227SViresh Kumar 		.name	 = "cpufreq-davinci",
1528a7b1227SViresh Kumar 	},
1538a7b1227SViresh Kumar 	.remove = __exit_p(davinci_cpufreq_remove),
1548a7b1227SViresh Kumar };
1558a7b1227SViresh Kumar 
1568a7b1227SViresh Kumar int __init davinci_cpufreq_init(void)
1578a7b1227SViresh Kumar {
1588a7b1227SViresh Kumar 	return platform_driver_probe(&davinci_cpufreq_driver,
1598a7b1227SViresh Kumar 							davinci_cpufreq_probe);
1608a7b1227SViresh Kumar }
1618a7b1227SViresh Kumar 
162