blob: f84ecd22f488612f9c3a2aa8637c6ffd581094f0 [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
Colin Cross7056d422010-04-22 20:30:13 -07002/*
Colin Cross7056d422010-04-22 20:30:13 -07003 * Copyright (C) 2010 Google, Inc.
4 *
5 * Author:
6 * Colin Cross <ccross@google.com>
7 * Based on arch/arm/plat-omap/cpu-omap.c, (C) 2005 Nokia Corporation
Colin Cross7056d422010-04-22 20:30:13 -07008 */
9
Dmitry Osipenko9a25ba92018-05-18 23:06:34 +030010#include <linux/clk.h>
11#include <linux/cpufreq.h>
12#include <linux/err.h>
13#include <linux/init.h>
Colin Cross7056d422010-04-22 20:30:13 -070014#include <linux/module.h>
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +030015#include <linux/platform_device.h>
Colin Cross7056d422010-04-22 20:30:13 -070016#include <linux/types.h>
Colin Cross7056d422010-04-22 20:30:13 -070017
Colin Cross7056d422010-04-22 20:30:13 -070018static struct cpufreq_frequency_table freq_table[] = {
Viresh Kumar5d690302013-05-14 19:08:50 +053019 { .frequency = 216000 },
20 { .frequency = 312000 },
21 { .frequency = 456000 },
22 { .frequency = 608000 },
23 { .frequency = 760000 },
24 { .frequency = 816000 },
25 { .frequency = 912000 },
26 { .frequency = 1000000 },
27 { .frequency = CPUFREQ_TABLE_END },
Colin Cross7056d422010-04-22 20:30:13 -070028};
29
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +030030struct tegra20_cpufreq {
31 struct device *dev;
32 struct cpufreq_driver driver;
33 struct clk *cpu_clk;
34 struct clk *pll_x_clk;
35 struct clk *pll_p_clk;
36 bool pll_x_prepared;
37};
Colin Cross7056d422010-04-22 20:30:13 -070038
Viresh Kumar00917dd2014-06-02 22:49:29 +053039static unsigned int tegra_get_intermediate(struct cpufreq_policy *policy,
40 unsigned int index)
41{
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +030042 struct tegra20_cpufreq *cpufreq = cpufreq_get_driver_data();
43 unsigned int ifreq = clk_get_rate(cpufreq->pll_p_clk) / 1000;
Viresh Kumar00917dd2014-06-02 22:49:29 +053044
45 /*
46 * Don't switch to intermediate freq if:
47 * - we are already at it, i.e. policy->cur == ifreq
48 * - index corresponds to ifreq
49 */
Dmitry Osipenkoc22d1cb2018-05-18 23:06:38 +030050 if (freq_table[index].frequency == ifreq || policy->cur == ifreq)
Viresh Kumar00917dd2014-06-02 22:49:29 +053051 return 0;
52
53 return ifreq;
54}
55
56static int tegra_target_intermediate(struct cpufreq_policy *policy,
57 unsigned int index)
Stephen Warrence32dda2012-09-10 17:05:01 -060058{
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +030059 struct tegra20_cpufreq *cpufreq = cpufreq_get_driver_data();
Stephen Warrence32dda2012-09-10 17:05:01 -060060 int ret;
61
62 /*
63 * Take an extra reference to the main pll so it doesn't turn
Viresh Kumar00917dd2014-06-02 22:49:29 +053064 * off when we move the cpu off of it as enabling it again while we
Viresh Kumar40cc5492014-06-10 10:27:12 +053065 * switch to it from tegra_target() would take additional time.
66 *
67 * When target-freq is equal to intermediate freq we don't need to
68 * switch to an intermediate freq and so this routine isn't called.
69 * Also, we wouldn't be using pll_x anymore and must not take extra
70 * reference to it, as it can be disabled now to save some power.
Stephen Warrence32dda2012-09-10 17:05:01 -060071 */
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +030072 clk_prepare_enable(cpufreq->pll_x_clk);
Stephen Warrence32dda2012-09-10 17:05:01 -060073
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +030074 ret = clk_set_parent(cpufreq->cpu_clk, cpufreq->pll_p_clk);
Viresh Kumar00917dd2014-06-02 22:49:29 +053075 if (ret)
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +030076 clk_disable_unprepare(cpufreq->pll_x_clk);
Viresh Kumar00917dd2014-06-02 22:49:29 +053077 else
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +030078 cpufreq->pll_x_prepared = true;
Stephen Warrence32dda2012-09-10 17:05:01 -060079
Stephen Warrence32dda2012-09-10 17:05:01 -060080 return ret;
81}
82
Viresh Kumare7b453d2014-05-15 11:21:19 +053083static int tegra_target(struct cpufreq_policy *policy, unsigned int index)
Colin Cross7056d422010-04-22 20:30:13 -070084{
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +030085 struct tegra20_cpufreq *cpufreq = cpufreq_get_driver_data();
Viresh Kumare7b453d2014-05-15 11:21:19 +053086 unsigned long rate = freq_table[index].frequency;
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +030087 unsigned int ifreq = clk_get_rate(cpufreq->pll_p_clk) / 1000;
Dmitry Osipenkof39d4d52018-05-18 23:06:39 +030088 int ret;
Colin Cross7056d422010-04-22 20:30:13 -070089
Colin Cross7a281282010-11-22 18:54:36 -080090 /*
Viresh Kumar00917dd2014-06-02 22:49:29 +053091 * target freq == pll_p, don't need to take extra reference to pll_x_clk
92 * as it isn't used anymore.
93 */
94 if (rate == ifreq)
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +030095 return clk_set_parent(cpufreq->cpu_clk, cpufreq->pll_p_clk);
Viresh Kumar00917dd2014-06-02 22:49:29 +053096
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +030097 ret = clk_set_rate(cpufreq->pll_x_clk, rate * 1000);
Viresh Kumar00917dd2014-06-02 22:49:29 +053098 /* Restore to earlier frequency on error, i.e. pll_x */
Viresh Kumard4019f02013-08-14 19:38:24 +053099 if (ret)
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300100 dev_err(cpufreq->dev, "Failed to change pll_x to %lu\n", rate);
Viresh Kumar00917dd2014-06-02 22:49:29 +0530101
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300102 ret = clk_set_parent(cpufreq->cpu_clk, cpufreq->pll_x_clk);
Viresh Kumar00917dd2014-06-02 22:49:29 +0530103 /* This shouldn't fail while changing or restoring */
104 WARN_ON(ret);
105
106 /*
107 * Drop count to pll_x clock only if we switched to intermediate freq
108 * earlier while transitioning to a target frequency.
109 */
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300110 if (cpufreq->pll_x_prepared) {
111 clk_disable_unprepare(cpufreq->pll_x_clk);
112 cpufreq->pll_x_prepared = false;
Viresh Kumar00917dd2014-06-02 22:49:29 +0530113 }
Colin Cross7056d422010-04-22 20:30:13 -0700114
Viresh Kumarf56cc992013-06-19 11:18:20 +0530115 return ret;
Colin Cross7056d422010-04-22 20:30:13 -0700116}
117
Colin Cross7056d422010-04-22 20:30:13 -0700118static int tegra_cpu_init(struct cpufreq_policy *policy)
119{
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300120 struct tegra20_cpufreq *cpufreq = cpufreq_get_driver_data();
Viresh Kumar99d428c2013-10-03 20:42:11 +0530121
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300122 clk_prepare_enable(cpufreq->cpu_clk);
Colin Cross89a5fb82010-10-20 17:47:59 -0700123
Colin Cross7056d422010-04-22 20:30:13 -0700124 /* FIXME: what's the actual transition time? */
Viresh Kumarc4dcc8a2019-07-16 09:36:08 +0530125 cpufreq_generic_init(policy, freq_table, 300 * 1000);
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300126 policy->clk = cpufreq->cpu_clk;
Viresh Kumard351cb32014-03-04 11:00:30 +0800127 policy->suspend_freq = freq_table[0].frequency;
Colin Cross7056d422010-04-22 20:30:13 -0700128 return 0;
129}
130
131static int tegra_cpu_exit(struct cpufreq_policy *policy)
132{
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300133 struct tegra20_cpufreq *cpufreq = cpufreq_get_driver_data();
134
135 clk_disable_unprepare(cpufreq->cpu_clk);
Colin Cross7056d422010-04-22 20:30:13 -0700136 return 0;
137}
138
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300139static int tegra20_cpufreq_probe(struct platform_device *pdev)
Colin Cross7056d422010-04-22 20:30:13 -0700140{
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300141 struct tegra20_cpufreq *cpufreq;
Dmitry Osipenko64cd64e2018-05-18 23:06:36 +0300142 int err;
143
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300144 cpufreq = devm_kzalloc(&pdev->dev, sizeof(*cpufreq), GFP_KERNEL);
145 if (!cpufreq)
146 return -ENOMEM;
Dmitry Osipenkoa413d2c2018-05-18 23:06:40 +0300147
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300148 cpufreq->cpu_clk = clk_get_sys(NULL, "cclk");
149 if (IS_ERR(cpufreq->cpu_clk))
150 return PTR_ERR(cpufreq->cpu_clk);
Richard Zhaoc26cefd2012-12-21 00:09:55 +0000151
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300152 cpufreq->pll_x_clk = clk_get_sys(NULL, "pll_x");
153 if (IS_ERR(cpufreq->pll_x_clk)) {
154 err = PTR_ERR(cpufreq->pll_x_clk);
Dmitry Osipenko64cd64e2018-05-18 23:06:36 +0300155 goto put_cpu;
156 }
Richard Zhaoc26cefd2012-12-21 00:09:55 +0000157
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300158 cpufreq->pll_p_clk = clk_get_sys(NULL, "pll_p");
159 if (IS_ERR(cpufreq->pll_p_clk)) {
160 err = PTR_ERR(cpufreq->pll_p_clk);
Dmitry Osipenko64cd64e2018-05-18 23:06:36 +0300161 goto put_pll_x;
162 }
Richard Zhaoc26cefd2012-12-21 00:09:55 +0000163
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300164 cpufreq->dev = &pdev->dev;
165 cpufreq->driver.get = cpufreq_generic_get;
166 cpufreq->driver.attr = cpufreq_generic_attr;
167 cpufreq->driver.init = tegra_cpu_init;
168 cpufreq->driver.exit = tegra_cpu_exit;
169 cpufreq->driver.flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK;
170 cpufreq->driver.verify = cpufreq_generic_frequency_table_verify;
171 cpufreq->driver.suspend = cpufreq_generic_suspend;
172 cpufreq->driver.driver_data = cpufreq;
173 cpufreq->driver.target_index = tegra_target;
174 cpufreq->driver.get_intermediate = tegra_get_intermediate;
175 cpufreq->driver.target_intermediate = tegra_target_intermediate;
176 snprintf(cpufreq->driver.name, CPUFREQ_NAME_LEN, "tegra");
177
178 err = cpufreq_register_driver(&cpufreq->driver);
Dmitry Osipenko64cd64e2018-05-18 23:06:36 +0300179 if (err)
180 goto put_pll_p;
181
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300182 platform_set_drvdata(pdev, cpufreq);
183
Dmitry Osipenko64cd64e2018-05-18 23:06:36 +0300184 return 0;
185
186put_pll_p:
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300187 clk_put(cpufreq->pll_p_clk);
Dmitry Osipenko64cd64e2018-05-18 23:06:36 +0300188put_pll_x:
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300189 clk_put(cpufreq->pll_x_clk);
Dmitry Osipenko64cd64e2018-05-18 23:06:36 +0300190put_cpu:
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300191 clk_put(cpufreq->cpu_clk);
Dmitry Osipenko64cd64e2018-05-18 23:06:36 +0300192
193 return err;
Colin Cross7056d422010-04-22 20:30:13 -0700194}
195
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300196static int tegra20_cpufreq_remove(struct platform_device *pdev)
Colin Cross7056d422010-04-22 20:30:13 -0700197{
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300198 struct tegra20_cpufreq *cpufreq = platform_get_drvdata(pdev);
199
200 cpufreq_unregister_driver(&cpufreq->driver);
201
202 clk_put(cpufreq->pll_p_clk);
203 clk_put(cpufreq->pll_x_clk);
204 clk_put(cpufreq->cpu_clk);
205
206 return 0;
Colin Cross7056d422010-04-22 20:30:13 -0700207}
208
Dmitry Osipenkodc628cd2018-05-18 23:06:42 +0300209static struct platform_driver tegra20_cpufreq_driver = {
210 .probe = tegra20_cpufreq_probe,
211 .remove = tegra20_cpufreq_remove,
212 .driver = {
213 .name = "tegra20-cpufreq",
214 },
215};
216module_platform_driver(tegra20_cpufreq_driver);
217
218MODULE_ALIAS("platform:tegra20-cpufreq");
Colin Cross7056d422010-04-22 20:30:13 -0700219MODULE_AUTHOR("Colin Cross <ccross@android.com>");
Dmitry Osipenko49640272018-05-18 23:06:32 +0300220MODULE_DESCRIPTION("NVIDIA Tegra20 cpufreq driver");
Colin Cross7056d422010-04-22 20:30:13 -0700221MODULE_LICENSE("GPL");