1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28a7b1227SViresh Kumar /*
38a7b1227SViresh Kumar * CPU frequency scaling for DaVinci
48a7b1227SViresh Kumar *
58479eb82SAlexander A. Klimov * Copyright (C) 2009 Texas Instruments Incorporated - https://www.ti.com/
68a7b1227SViresh Kumar *
78a7b1227SViresh Kumar * Based on linux/arch/arm/plat-omap/cpu-omap.c. Original Copyright follows:
88a7b1227SViresh Kumar *
98a7b1227SViresh Kumar * Copyright (C) 2005 Nokia Corporation
108a7b1227SViresh Kumar * Written by Tony Lindgren <tony@atomide.com>
118a7b1227SViresh Kumar *
128a7b1227SViresh Kumar * Based on cpu-sa1110.c, Copyright (C) 2001 Russell King
138a7b1227SViresh Kumar *
148a7b1227SViresh Kumar * Copyright (C) 2007-2008 Texas Instruments, Inc.
158a7b1227SViresh Kumar * Updated to support OMAP3
168a7b1227SViresh Kumar * Rajendra Nayak <rnayak@ti.com>
178a7b1227SViresh Kumar */
188a7b1227SViresh Kumar #include <linux/types.h>
198a7b1227SViresh Kumar #include <linux/cpufreq.h>
208a7b1227SViresh Kumar #include <linux/init.h>
218a7b1227SViresh Kumar #include <linux/err.h>
228a7b1227SViresh Kumar #include <linux/clk.h>
2340b46b3bSBartosz Golaszewski #include <linux/platform_data/davinci-cpufreq.h>
248a7b1227SViresh Kumar #include <linux/platform_device.h>
258a7b1227SViresh Kumar #include <linux/export.h>
268a7b1227SViresh Kumar
278a7b1227SViresh Kumar struct davinci_cpufreq {
288a7b1227SViresh Kumar struct device *dev;
298a7b1227SViresh Kumar struct clk *armclk;
308a7b1227SViresh Kumar struct clk *asyncclk;
318a7b1227SViresh Kumar unsigned long asyncrate;
328a7b1227SViresh Kumar };
338a7b1227SViresh Kumar static struct davinci_cpufreq cpufreq;
348a7b1227SViresh Kumar
davinci_target(struct cpufreq_policy * policy,unsigned int idx)359c0ebcf7SViresh Kumar static int davinci_target(struct cpufreq_policy *policy, unsigned int idx)
368a7b1227SViresh Kumar {
378a7b1227SViresh Kumar struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
388a7b1227SViresh Kumar struct clk *armclk = cpufreq.armclk;
39d4019f0aSViresh Kumar unsigned int old_freq, new_freq;
40d4019f0aSViresh Kumar int ret = 0;
418a7b1227SViresh Kumar
42652ed95dSViresh Kumar old_freq = policy->cur;
43d4019f0aSViresh Kumar new_freq = pdata->freq_table[idx].frequency;
448a7b1227SViresh Kumar
458a7b1227SViresh Kumar /* if moving to higher frequency, up the voltage beforehand */
46d4019f0aSViresh Kumar if (pdata->set_voltage && new_freq > old_freq) {
478a7b1227SViresh Kumar ret = pdata->set_voltage(idx);
488a7b1227SViresh Kumar if (ret)
49d4019f0aSViresh Kumar return ret;
508a7b1227SViresh Kumar }
518a7b1227SViresh Kumar
52b4088173SBartosz Golaszewski ret = clk_set_rate(armclk, new_freq * 1000);
538a7b1227SViresh Kumar if (ret)
54d4019f0aSViresh Kumar return ret;
558a7b1227SViresh Kumar
568a7b1227SViresh Kumar if (cpufreq.asyncclk) {
578a7b1227SViresh Kumar ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
588a7b1227SViresh Kumar if (ret)
59d4019f0aSViresh Kumar return ret;
608a7b1227SViresh Kumar }
618a7b1227SViresh Kumar
628a7b1227SViresh Kumar /* if moving to lower freq, lower the voltage after lowering freq */
63d4019f0aSViresh Kumar if (pdata->set_voltage && new_freq < old_freq)
648a7b1227SViresh Kumar pdata->set_voltage(idx);
658a7b1227SViresh Kumar
66d4019f0aSViresh Kumar return 0;
678a7b1227SViresh Kumar }
688a7b1227SViresh Kumar
davinci_cpu_init(struct cpufreq_policy * policy)698a7b1227SViresh Kumar static int davinci_cpu_init(struct cpufreq_policy *policy)
708a7b1227SViresh Kumar {
718a7b1227SViresh Kumar int result = 0;
728a7b1227SViresh Kumar struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
738a7b1227SViresh Kumar struct cpufreq_frequency_table *freq_table = pdata->freq_table;
748a7b1227SViresh Kumar
758a7b1227SViresh Kumar if (policy->cpu != 0)
768a7b1227SViresh Kumar return -EINVAL;
778a7b1227SViresh Kumar
788a7b1227SViresh Kumar /* Finish platform specific initialization */
798a7b1227SViresh Kumar if (pdata->init) {
808a7b1227SViresh Kumar result = pdata->init();
818a7b1227SViresh Kumar if (result)
828a7b1227SViresh Kumar return result;
838a7b1227SViresh Kumar }
848a7b1227SViresh Kumar
85652ed95dSViresh Kumar policy->clk = cpufreq.armclk;
86652ed95dSViresh Kumar
878a7b1227SViresh Kumar /*
888a7b1227SViresh Kumar * Time measurement across the target() function yields ~1500-1800us
898a7b1227SViresh Kumar * time taken with no drivers on notification list.
908a7b1227SViresh Kumar * Setting the latency to 2000 us to accommodate addition of drivers
918a7b1227SViresh Kumar * to pre/post change notification list.
928a7b1227SViresh Kumar */
93c4dcc8a1SViresh Kumar cpufreq_generic_init(policy, freq_table, 2000 * 1000);
94c4dcc8a1SViresh Kumar return 0;
958a7b1227SViresh Kumar }
968a7b1227SViresh Kumar
978a7b1227SViresh Kumar static struct cpufreq_driver davinci_driver = {
985ae4a4b4SViresh Kumar .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
999d4de290SViresh Kumar .verify = cpufreq_generic_frequency_table_verify,
1009c0ebcf7SViresh Kumar .target_index = davinci_target,
101652ed95dSViresh Kumar .get = cpufreq_generic_get,
1028a7b1227SViresh Kumar .init = davinci_cpu_init,
1038a7b1227SViresh Kumar .name = "davinci",
10439d0c362SViresh Kumar .attr = cpufreq_generic_attr,
1058a7b1227SViresh Kumar };
1068a7b1227SViresh Kumar
davinci_cpufreq_probe(struct platform_device * pdev)1078a7b1227SViresh Kumar static int __init davinci_cpufreq_probe(struct platform_device *pdev)
1088a7b1227SViresh Kumar {
1098a7b1227SViresh Kumar struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
1108a7b1227SViresh Kumar struct clk *asyncclk;
1118a7b1227SViresh Kumar
1128a7b1227SViresh Kumar if (!pdata)
1138a7b1227SViresh Kumar return -EINVAL;
1148a7b1227SViresh Kumar if (!pdata->freq_table)
1158a7b1227SViresh Kumar return -EINVAL;
1168a7b1227SViresh Kumar
1178a7b1227SViresh Kumar cpufreq.dev = &pdev->dev;
1188a7b1227SViresh Kumar
1198a7b1227SViresh Kumar cpufreq.armclk = clk_get(NULL, "arm");
1208a7b1227SViresh Kumar if (IS_ERR(cpufreq.armclk)) {
1218a7b1227SViresh Kumar dev_err(cpufreq.dev, "Unable to get ARM clock\n");
1228a7b1227SViresh Kumar return PTR_ERR(cpufreq.armclk);
1238a7b1227SViresh Kumar }
1248a7b1227SViresh Kumar
1258a7b1227SViresh Kumar asyncclk = clk_get(cpufreq.dev, "async");
1268a7b1227SViresh Kumar if (!IS_ERR(asyncclk)) {
1278a7b1227SViresh Kumar cpufreq.asyncclk = asyncclk;
1288a7b1227SViresh Kumar cpufreq.asyncrate = clk_get_rate(asyncclk);
1298a7b1227SViresh Kumar }
1308a7b1227SViresh Kumar
1318a7b1227SViresh Kumar return cpufreq_register_driver(&davinci_driver);
1328a7b1227SViresh Kumar }
1338a7b1227SViresh Kumar
davinci_cpufreq_remove(struct platform_device * pdev)134*463ff6d4SYangtao Li static void __exit davinci_cpufreq_remove(struct platform_device *pdev)
1358a7b1227SViresh Kumar {
1365d8f384aSUwe Kleine-König cpufreq_unregister_driver(&davinci_driver);
1375d8f384aSUwe Kleine-König
1388a7b1227SViresh Kumar clk_put(cpufreq.armclk);
1398a7b1227SViresh Kumar
1408a7b1227SViresh Kumar if (cpufreq.asyncclk)
1418a7b1227SViresh Kumar clk_put(cpufreq.asyncclk);
1428a7b1227SViresh Kumar }
1438a7b1227SViresh Kumar
1448a7b1227SViresh Kumar static struct platform_driver davinci_cpufreq_driver = {
1458a7b1227SViresh Kumar .driver = {
1468a7b1227SViresh Kumar .name = "cpufreq-davinci",
1478a7b1227SViresh Kumar },
148*463ff6d4SYangtao Li .remove_new = __exit_p(davinci_cpufreq_remove),
1498a7b1227SViresh Kumar };
1508a7b1227SViresh Kumar
davinci_cpufreq_init(void)1518a7b1227SViresh Kumar int __init davinci_cpufreq_init(void)
1528a7b1227SViresh Kumar {
1538a7b1227SViresh Kumar return platform_driver_probe(&davinci_cpufreq_driver,
1548a7b1227SViresh Kumar davinci_cpufreq_probe);
1558a7b1227SViresh Kumar }
1568a7b1227SViresh Kumar
157