blob: 3de48ae60c292d6dc7ead88de6cbb0594e61eda3 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Sekhar Nori6601b802009-09-22 21:14:00 +05302/*
3 * CPU frequency scaling for DaVinci
4 *
5 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
6 *
7 * Based on linux/arch/arm/plat-omap/cpu-omap.c. Original Copyright follows:
8 *
9 * Copyright (C) 2005 Nokia Corporation
10 * Written by Tony Lindgren <tony@atomide.com>
11 *
12 * Based on cpu-sa1110.c, Copyright (C) 2001 Russell King
13 *
14 * Copyright (C) 2007-2008 Texas Instruments, Inc.
15 * Updated to support OMAP3
16 * Rajendra Nayak <rnayak@ti.com>
Sekhar Nori6601b802009-09-22 21:14:00 +053017 */
18#include <linux/types.h>
19#include <linux/cpufreq.h>
20#include <linux/init.h>
21#include <linux/err.h>
22#include <linux/clk.h>
Bartosz Golaszewski40b46b32019-02-14 18:05:05 +010023#include <linux/platform_data/davinci-cpufreq.h>
Sekhar Nori6601b802009-09-22 21:14:00 +053024#include <linux/platform_device.h>
Paul Gortmakerdc280942011-07-31 16:17:29 -040025#include <linux/export.h>
Sekhar Nori6601b802009-09-22 21:14:00 +053026
Sekhar Nori6601b802009-09-22 21:14:00 +053027struct davinci_cpufreq {
28 struct device *dev;
29 struct clk *armclk;
Sekhar Nori30a2c5d2010-07-20 16:46:50 +053030 struct clk *asyncclk;
31 unsigned long asyncrate;
Sekhar Nori6601b802009-09-22 21:14:00 +053032};
33static struct davinci_cpufreq cpufreq;
34
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053035static int davinci_target(struct cpufreq_policy *policy, unsigned int idx)
Sekhar Nori6601b802009-09-22 21:14:00 +053036{
Sekhar Nori6601b802009-09-22 21:14:00 +053037 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
38 struct clk *armclk = cpufreq.armclk;
Viresh Kumard4019f02013-08-14 19:38:24 +053039 unsigned int old_freq, new_freq;
40 int ret = 0;
Sekhar Nori6601b802009-09-22 21:14:00 +053041
Viresh Kumar652ed952014-01-09 20:38:43 +053042 old_freq = policy->cur;
Viresh Kumard4019f02013-08-14 19:38:24 +053043 new_freq = pdata->freq_table[idx].frequency;
Sekhar Nori6601b802009-09-22 21:14:00 +053044
45 /* if moving to higher frequency, up the voltage beforehand */
Viresh Kumard4019f02013-08-14 19:38:24 +053046 if (pdata->set_voltage && new_freq > old_freq) {
Sekhar Norifca97b32010-07-20 16:46:48 +053047 ret = pdata->set_voltage(idx);
48 if (ret)
Viresh Kumard4019f02013-08-14 19:38:24 +053049 return ret;
Sekhar Norifca97b32010-07-20 16:46:48 +053050 }
Sekhar Nori6601b802009-09-22 21:14:00 +053051
Bartosz Golaszewskib4088172016-12-07 16:22:18 +010052 ret = clk_set_rate(armclk, new_freq * 1000);
Sekhar Norifca97b32010-07-20 16:46:48 +053053 if (ret)
Viresh Kumard4019f02013-08-14 19:38:24 +053054 return ret;
Sekhar Nori6601b802009-09-22 21:14:00 +053055
Sekhar Nori30a2c5d2010-07-20 16:46:50 +053056 if (cpufreq.asyncclk) {
57 ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
58 if (ret)
Viresh Kumard4019f02013-08-14 19:38:24 +053059 return ret;
Sekhar Nori30a2c5d2010-07-20 16:46:50 +053060 }
61
Sekhar Nori6601b802009-09-22 21:14:00 +053062 /* if moving to lower freq, lower the voltage after lowering freq */
Viresh Kumard4019f02013-08-14 19:38:24 +053063 if (pdata->set_voltage && new_freq < old_freq)
Sekhar Nori6601b802009-09-22 21:14:00 +053064 pdata->set_voltage(idx);
65
Viresh Kumard4019f02013-08-14 19:38:24 +053066 return 0;
Sekhar Nori6601b802009-09-22 21:14:00 +053067}
68
Axel Lin079db592011-02-28 15:51:33 +053069static int davinci_cpu_init(struct cpufreq_policy *policy)
Sekhar Nori6601b802009-09-22 21:14:00 +053070{
71 int result = 0;
72 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
73 struct cpufreq_frequency_table *freq_table = pdata->freq_table;
74
75 if (policy->cpu != 0)
76 return -EINVAL;
77
Sekhar Nori13d5e272009-10-22 15:12:16 +053078 /* Finish platform specific initialization */
79 if (pdata->init) {
80 result = pdata->init();
81 if (result)
82 return result;
83 }
84
Viresh Kumar652ed952014-01-09 20:38:43 +053085 policy->clk = cpufreq.armclk;
86
Sekhar Nori6601b802009-09-22 21:14:00 +053087 /*
88 * Time measurement across the target() function yields ~1500-1800us
89 * time taken with no drivers on notification list.
Lucas De Marchi25985ed2011-03-30 22:57:33 -030090 * Setting the latency to 2000 us to accommodate addition of drivers
Sekhar Nori6601b802009-09-22 21:14:00 +053091 * to pre/post change notification list.
92 */
Viresh Kumaraf8c4cf2013-10-03 20:29:11 +053093 return cpufreq_generic_init(policy, freq_table, 2000 * 1000);
Sekhar Nori6601b802009-09-22 21:14:00 +053094}
95
Sekhar Nori6601b802009-09-22 21:14:00 +053096static struct cpufreq_driver davinci_driver = {
Viresh Kumarae6b4272013-12-03 11:20:45 +053097 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
Viresh Kumar9d4de292016-06-03 10:58:52 +053098 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053099 .target_index = davinci_target,
Viresh Kumar652ed952014-01-09 20:38:43 +0530100 .get = cpufreq_generic_get,
Sekhar Nori6601b802009-09-22 21:14:00 +0530101 .init = davinci_cpu_init,
Sekhar Nori6601b802009-09-22 21:14:00 +0530102 .name = "davinci",
Viresh Kumar39d0c362013-10-03 20:28:02 +0530103 .attr = cpufreq_generic_attr,
Sekhar Nori6601b802009-09-22 21:14:00 +0530104};
105
106static int __init davinci_cpufreq_probe(struct platform_device *pdev)
107{
108 struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530109 struct clk *asyncclk;
Sekhar Nori6601b802009-09-22 21:14:00 +0530110
111 if (!pdata)
112 return -EINVAL;
113 if (!pdata->freq_table)
114 return -EINVAL;
115
116 cpufreq.dev = &pdev->dev;
117
118 cpufreq.armclk = clk_get(NULL, "arm");
119 if (IS_ERR(cpufreq.armclk)) {
120 dev_err(cpufreq.dev, "Unable to get ARM clock\n");
121 return PTR_ERR(cpufreq.armclk);
122 }
123
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530124 asyncclk = clk_get(cpufreq.dev, "async");
125 if (!IS_ERR(asyncclk)) {
126 cpufreq.asyncclk = asyncclk;
127 cpufreq.asyncrate = clk_get_rate(asyncclk);
128 }
129
Sekhar Nori6601b802009-09-22 21:14:00 +0530130 return cpufreq_register_driver(&davinci_driver);
131}
132
133static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
134{
135 clk_put(cpufreq.armclk);
136
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530137 if (cpufreq.asyncclk)
138 clk_put(cpufreq.asyncclk);
139
Sekhar Nori6601b802009-09-22 21:14:00 +0530140 return cpufreq_unregister_driver(&davinci_driver);
141}
142
143static struct platform_driver davinci_cpufreq_driver = {
144 .driver = {
145 .name = "cpufreq-davinci",
Sekhar Nori6601b802009-09-22 21:14:00 +0530146 },
147 .remove = __exit_p(davinci_cpufreq_remove),
148};
149
Shawn Guo3aa3e842012-04-26 09:45:39 +0800150int __init davinci_cpufreq_init(void)
Sekhar Nori6601b802009-09-22 21:14:00 +0530151{
152 return platform_driver_probe(&davinci_cpufreq_driver,
153 davinci_cpufreq_probe);
154}
Sekhar Nori6601b802009-09-22 21:14:00 +0530155