Thomas Gleixner | 9c92ab6 | 2019-05-29 07:17:56 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 2 | /* |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 3 | * 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 Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Dmitry Osipenko | 9a25ba9 | 2018-05-18 23:06:34 +0300 | [diff] [blame] | 10 | #include <linux/clk.h> |
| 11 | #include <linux/cpufreq.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/init.h> |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 15 | #include <linux/platform_device.h> |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 16 | #include <linux/types.h> |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 17 | |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 18 | static struct cpufreq_frequency_table freq_table[] = { |
Viresh Kumar | 5d69030 | 2013-05-14 19:08:50 +0530 | [diff] [blame] | 19 | { .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 Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 30 | struct 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 Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 38 | |
Viresh Kumar | 00917dd | 2014-06-02 22:49:29 +0530 | [diff] [blame] | 39 | static unsigned int tegra_get_intermediate(struct cpufreq_policy *policy, |
| 40 | unsigned int index) |
| 41 | { |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 42 | struct tegra20_cpufreq *cpufreq = cpufreq_get_driver_data(); |
| 43 | unsigned int ifreq = clk_get_rate(cpufreq->pll_p_clk) / 1000; |
Viresh Kumar | 00917dd | 2014-06-02 22:49:29 +0530 | [diff] [blame] | 44 | |
| 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 Osipenko | c22d1cb | 2018-05-18 23:06:38 +0300 | [diff] [blame] | 50 | if (freq_table[index].frequency == ifreq || policy->cur == ifreq) |
Viresh Kumar | 00917dd | 2014-06-02 22:49:29 +0530 | [diff] [blame] | 51 | return 0; |
| 52 | |
| 53 | return ifreq; |
| 54 | } |
| 55 | |
| 56 | static int tegra_target_intermediate(struct cpufreq_policy *policy, |
| 57 | unsigned int index) |
Stephen Warren | ce32dda | 2012-09-10 17:05:01 -0600 | [diff] [blame] | 58 | { |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 59 | struct tegra20_cpufreq *cpufreq = cpufreq_get_driver_data(); |
Stephen Warren | ce32dda | 2012-09-10 17:05:01 -0600 | [diff] [blame] | 60 | int ret; |
| 61 | |
| 62 | /* |
| 63 | * Take an extra reference to the main pll so it doesn't turn |
Viresh Kumar | 00917dd | 2014-06-02 22:49:29 +0530 | [diff] [blame] | 64 | * off when we move the cpu off of it as enabling it again while we |
Viresh Kumar | 40cc549 | 2014-06-10 10:27:12 +0530 | [diff] [blame] | 65 | * 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 Warren | ce32dda | 2012-09-10 17:05:01 -0600 | [diff] [blame] | 71 | */ |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 72 | clk_prepare_enable(cpufreq->pll_x_clk); |
Stephen Warren | ce32dda | 2012-09-10 17:05:01 -0600 | [diff] [blame] | 73 | |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 74 | ret = clk_set_parent(cpufreq->cpu_clk, cpufreq->pll_p_clk); |
Viresh Kumar | 00917dd | 2014-06-02 22:49:29 +0530 | [diff] [blame] | 75 | if (ret) |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 76 | clk_disable_unprepare(cpufreq->pll_x_clk); |
Viresh Kumar | 00917dd | 2014-06-02 22:49:29 +0530 | [diff] [blame] | 77 | else |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 78 | cpufreq->pll_x_prepared = true; |
Stephen Warren | ce32dda | 2012-09-10 17:05:01 -0600 | [diff] [blame] | 79 | |
Stephen Warren | ce32dda | 2012-09-10 17:05:01 -0600 | [diff] [blame] | 80 | return ret; |
| 81 | } |
| 82 | |
Viresh Kumar | e7b453d | 2014-05-15 11:21:19 +0530 | [diff] [blame] | 83 | static int tegra_target(struct cpufreq_policy *policy, unsigned int index) |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 84 | { |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 85 | struct tegra20_cpufreq *cpufreq = cpufreq_get_driver_data(); |
Viresh Kumar | e7b453d | 2014-05-15 11:21:19 +0530 | [diff] [blame] | 86 | unsigned long rate = freq_table[index].frequency; |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 87 | unsigned int ifreq = clk_get_rate(cpufreq->pll_p_clk) / 1000; |
Dmitry Osipenko | f39d4d5 | 2018-05-18 23:06:39 +0300 | [diff] [blame] | 88 | int ret; |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 89 | |
Colin Cross | 7a28128 | 2010-11-22 18:54:36 -0800 | [diff] [blame] | 90 | /* |
Viresh Kumar | 00917dd | 2014-06-02 22:49:29 +0530 | [diff] [blame] | 91 | * 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 Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 95 | return clk_set_parent(cpufreq->cpu_clk, cpufreq->pll_p_clk); |
Viresh Kumar | 00917dd | 2014-06-02 22:49:29 +0530 | [diff] [blame] | 96 | |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 97 | ret = clk_set_rate(cpufreq->pll_x_clk, rate * 1000); |
Viresh Kumar | 00917dd | 2014-06-02 22:49:29 +0530 | [diff] [blame] | 98 | /* Restore to earlier frequency on error, i.e. pll_x */ |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 99 | if (ret) |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 100 | dev_err(cpufreq->dev, "Failed to change pll_x to %lu\n", rate); |
Viresh Kumar | 00917dd | 2014-06-02 22:49:29 +0530 | [diff] [blame] | 101 | |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 102 | ret = clk_set_parent(cpufreq->cpu_clk, cpufreq->pll_x_clk); |
Viresh Kumar | 00917dd | 2014-06-02 22:49:29 +0530 | [diff] [blame] | 103 | /* 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 Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 110 | if (cpufreq->pll_x_prepared) { |
| 111 | clk_disable_unprepare(cpufreq->pll_x_clk); |
| 112 | cpufreq->pll_x_prepared = false; |
Viresh Kumar | 00917dd | 2014-06-02 22:49:29 +0530 | [diff] [blame] | 113 | } |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 114 | |
Viresh Kumar | f56cc99 | 2013-06-19 11:18:20 +0530 | [diff] [blame] | 115 | return ret; |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 118 | static int tegra_cpu_init(struct cpufreq_policy *policy) |
| 119 | { |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 120 | struct tegra20_cpufreq *cpufreq = cpufreq_get_driver_data(); |
Viresh Kumar | 99d428c | 2013-10-03 20:42:11 +0530 | [diff] [blame] | 121 | |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 122 | clk_prepare_enable(cpufreq->cpu_clk); |
Colin Cross | 89a5fb8 | 2010-10-20 17:47:59 -0700 | [diff] [blame] | 123 | |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 124 | /* FIXME: what's the actual transition time? */ |
Viresh Kumar | c4dcc8a | 2019-07-16 09:36:08 +0530 | [diff] [blame] | 125 | cpufreq_generic_init(policy, freq_table, 300 * 1000); |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 126 | policy->clk = cpufreq->cpu_clk; |
Viresh Kumar | d351cb3 | 2014-03-04 11:00:30 +0800 | [diff] [blame] | 127 | policy->suspend_freq = freq_table[0].frequency; |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int tegra_cpu_exit(struct cpufreq_policy *policy) |
| 132 | { |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 133 | struct tegra20_cpufreq *cpufreq = cpufreq_get_driver_data(); |
| 134 | |
| 135 | clk_disable_unprepare(cpufreq->cpu_clk); |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 139 | static int tegra20_cpufreq_probe(struct platform_device *pdev) |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 140 | { |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 141 | struct tegra20_cpufreq *cpufreq; |
Dmitry Osipenko | 64cd64e | 2018-05-18 23:06:36 +0300 | [diff] [blame] | 142 | int err; |
| 143 | |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 144 | cpufreq = devm_kzalloc(&pdev->dev, sizeof(*cpufreq), GFP_KERNEL); |
| 145 | if (!cpufreq) |
| 146 | return -ENOMEM; |
Dmitry Osipenko | a413d2c | 2018-05-18 23:06:40 +0300 | [diff] [blame] | 147 | |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 148 | cpufreq->cpu_clk = clk_get_sys(NULL, "cclk"); |
| 149 | if (IS_ERR(cpufreq->cpu_clk)) |
| 150 | return PTR_ERR(cpufreq->cpu_clk); |
Richard Zhao | c26cefd | 2012-12-21 00:09:55 +0000 | [diff] [blame] | 151 | |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 152 | 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 Osipenko | 64cd64e | 2018-05-18 23:06:36 +0300 | [diff] [blame] | 155 | goto put_cpu; |
| 156 | } |
Richard Zhao | c26cefd | 2012-12-21 00:09:55 +0000 | [diff] [blame] | 157 | |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 158 | 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 Osipenko | 64cd64e | 2018-05-18 23:06:36 +0300 | [diff] [blame] | 161 | goto put_pll_x; |
| 162 | } |
Richard Zhao | c26cefd | 2012-12-21 00:09:55 +0000 | [diff] [blame] | 163 | |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 164 | 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 Osipenko | 64cd64e | 2018-05-18 23:06:36 +0300 | [diff] [blame] | 179 | if (err) |
| 180 | goto put_pll_p; |
| 181 | |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 182 | platform_set_drvdata(pdev, cpufreq); |
| 183 | |
Dmitry Osipenko | 64cd64e | 2018-05-18 23:06:36 +0300 | [diff] [blame] | 184 | return 0; |
| 185 | |
| 186 | put_pll_p: |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 187 | clk_put(cpufreq->pll_p_clk); |
Dmitry Osipenko | 64cd64e | 2018-05-18 23:06:36 +0300 | [diff] [blame] | 188 | put_pll_x: |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 189 | clk_put(cpufreq->pll_x_clk); |
Dmitry Osipenko | 64cd64e | 2018-05-18 23:06:36 +0300 | [diff] [blame] | 190 | put_cpu: |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 191 | clk_put(cpufreq->cpu_clk); |
Dmitry Osipenko | 64cd64e | 2018-05-18 23:06:36 +0300 | [diff] [blame] | 192 | |
| 193 | return err; |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 196 | static int tegra20_cpufreq_remove(struct platform_device *pdev) |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 197 | { |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 198 | 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 Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Dmitry Osipenko | dc628cd | 2018-05-18 23:06:42 +0300 | [diff] [blame] | 209 | static struct platform_driver tegra20_cpufreq_driver = { |
| 210 | .probe = tegra20_cpufreq_probe, |
| 211 | .remove = tegra20_cpufreq_remove, |
| 212 | .driver = { |
| 213 | .name = "tegra20-cpufreq", |
| 214 | }, |
| 215 | }; |
| 216 | module_platform_driver(tegra20_cpufreq_driver); |
| 217 | |
| 218 | MODULE_ALIAS("platform:tegra20-cpufreq"); |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 219 | MODULE_AUTHOR("Colin Cross <ccross@android.com>"); |
Dmitry Osipenko | 4964027 | 2018-05-18 23:06:32 +0300 | [diff] [blame] | 220 | MODULE_DESCRIPTION("NVIDIA Tegra20 cpufreq driver"); |
Colin Cross | 7056d42 | 2010-04-22 20:30:13 -0700 | [diff] [blame] | 221 | MODULE_LICENSE("GPL"); |