blob: ba3795e13ac6f5904a197572c22f908beae64760 [file] [log] [blame]
Tuomas Tynkkynen9eb15db2015-05-13 17:58:48 +03001/*
2 * Tegra 124 cpufreq driver
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/clk.h>
Tuomas Tynkkynen9eb15db2015-05-13 17:58:48 +030017#include <linux/err.h>
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/of_device.h>
22#include <linux/of.h>
23#include <linux/platform_device.h>
24#include <linux/pm_opp.h>
Tuomas Tynkkynen9eb15db2015-05-13 17:58:48 +030025#include <linux/types.h>
26
27struct tegra124_cpufreq_priv {
Tuomas Tynkkynen9eb15db2015-05-13 17:58:48 +030028 struct clk *cpu_clk;
29 struct clk *pllp_clk;
30 struct clk *pllx_clk;
31 struct clk *dfll_clk;
32 struct platform_device *cpufreq_dt_pdev;
33};
34
35static int tegra124_cpu_switch_to_dfll(struct tegra124_cpufreq_priv *priv)
36{
37 struct clk *orig_parent;
38 int ret;
39
40 ret = clk_set_rate(priv->dfll_clk, clk_get_rate(priv->cpu_clk));
41 if (ret)
42 return ret;
43
44 orig_parent = clk_get_parent(priv->cpu_clk);
45 clk_set_parent(priv->cpu_clk, priv->pllp_clk);
46
47 ret = clk_prepare_enable(priv->dfll_clk);
48 if (ret)
49 goto out;
50
51 clk_set_parent(priv->cpu_clk, priv->dfll_clk);
52
53 return 0;
54
55out:
56 clk_set_parent(priv->cpu_clk, orig_parent);
57
58 return ret;
59}
60
Tuomas Tynkkynen9eb15db2015-05-13 17:58:48 +030061static int tegra124_cpufreq_probe(struct platform_device *pdev)
62{
63 struct tegra124_cpufreq_priv *priv;
64 struct device_node *np;
65 struct device *cpu_dev;
66 struct platform_device_info cpufreq_dt_devinfo = {};
67 int ret;
68
69 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
70 if (!priv)
71 return -ENOMEM;
72
73 cpu_dev = get_cpu_device(0);
74 if (!cpu_dev)
75 return -ENODEV;
76
77 np = of_cpu_device_node_get(0);
78 if (!np)
79 return -ENODEV;
80
Tuomas Tynkkynen9eb15db2015-05-13 17:58:48 +030081 priv->cpu_clk = of_clk_get_by_name(np, "cpu_g");
82 if (IS_ERR(priv->cpu_clk)) {
83 ret = PTR_ERR(priv->cpu_clk);
Joseph Lo9f5ed5f2019-01-04 11:06:53 +080084 goto out_put_np;
Tuomas Tynkkynen9eb15db2015-05-13 17:58:48 +030085 }
86
87 priv->dfll_clk = of_clk_get_by_name(np, "dfll");
88 if (IS_ERR(priv->dfll_clk)) {
89 ret = PTR_ERR(priv->dfll_clk);
90 goto out_put_cpu_clk;
91 }
92
93 priv->pllx_clk = of_clk_get_by_name(np, "pll_x");
94 if (IS_ERR(priv->pllx_clk)) {
95 ret = PTR_ERR(priv->pllx_clk);
96 goto out_put_dfll_clk;
97 }
98
99 priv->pllp_clk = of_clk_get_by_name(np, "pll_p");
100 if (IS_ERR(priv->pllp_clk)) {
101 ret = PTR_ERR(priv->pllp_clk);
102 goto out_put_pllx_clk;
103 }
104
105 ret = tegra124_cpu_switch_to_dfll(priv);
106 if (ret)
107 goto out_put_pllp_clk;
108
109 cpufreq_dt_devinfo.name = "cpufreq-dt";
110 cpufreq_dt_devinfo.parent = &pdev->dev;
Tuomas Tynkkynen9eb15db2015-05-13 17:58:48 +0300111
112 priv->cpufreq_dt_pdev =
113 platform_device_register_full(&cpufreq_dt_devinfo);
114 if (IS_ERR(priv->cpufreq_dt_pdev)) {
115 ret = PTR_ERR(priv->cpufreq_dt_pdev);
Joseph Lo9f5ed5f2019-01-04 11:06:53 +0800116 goto out_put_pllp_clk;
Tuomas Tynkkynen9eb15db2015-05-13 17:58:48 +0300117 }
118
119 platform_set_drvdata(pdev, priv);
120
121 return 0;
122
Tuomas Tynkkynen9eb15db2015-05-13 17:58:48 +0300123out_put_pllp_clk:
124 clk_put(priv->pllp_clk);
125out_put_pllx_clk:
126 clk_put(priv->pllx_clk);
127out_put_dfll_clk:
128 clk_put(priv->dfll_clk);
129out_put_cpu_clk:
130 clk_put(priv->cpu_clk);
Tuomas Tynkkynen9eb15db2015-05-13 17:58:48 +0300131out_put_np:
132 of_node_put(np);
133
134 return ret;
135}
136
Tuomas Tynkkynen9eb15db2015-05-13 17:58:48 +0300137static struct platform_driver tegra124_cpufreq_platdrv = {
138 .driver.name = "cpufreq-tegra124",
139 .probe = tegra124_cpufreq_probe,
Tuomas Tynkkynen9eb15db2015-05-13 17:58:48 +0300140};
141
142static int __init tegra_cpufreq_init(void)
143{
144 int ret;
145 struct platform_device *pdev;
146
Joseph Loc06697d2019-01-04 11:06:54 +0800147 if (!(of_machine_is_compatible("nvidia,tegra124") ||
148 of_machine_is_compatible("nvidia,tegra210")))
Tuomas Tynkkynen9eb15db2015-05-13 17:58:48 +0300149 return -ENODEV;
150
151 /*
152 * Platform driver+device required for handling EPROBE_DEFER with
153 * the regulator and the DFLL clock
154 */
155 ret = platform_driver_register(&tegra124_cpufreq_platdrv);
156 if (ret)
157 return ret;
158
159 pdev = platform_device_register_simple("cpufreq-tegra124", -1, NULL, 0);
160 if (IS_ERR(pdev)) {
161 platform_driver_unregister(&tegra124_cpufreq_platdrv);
162 return PTR_ERR(pdev);
163 }
164
165 return 0;
166}
167module_init(tegra_cpufreq_init);
168
169MODULE_AUTHOR("Tuomas Tynkkynen <ttynkkynen@nvidia.com>");
170MODULE_DESCRIPTION("cpufreq driver for NVIDIA Tegra124");
171MODULE_LICENSE("GPL v2");