blob: be89416e2358fcbb0464e525421f7f6607d1883b [file] [log] [blame]
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +08001/*
2 * CPU Frequency Scaling for Loongson 1 SoC
3 *
Kelvin Cheung6a1d55c2016-04-12 18:40:15 +08004 * Copyright (C) 2014-2016 Zhang, Keguang <keguang.zhang@gmail.com>
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +08005 *
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
9 */
10
11#include <linux/clk.h>
12#include <linux/clk-provider.h>
13#include <linux/cpu.h>
14#include <linux/cpufreq.h>
15#include <linux/delay.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
19
Huacai Chen30ad29b2015-04-21 10:00:35 +080020#include <cpufreq.h>
21#include <loongson1.h>
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080022
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080023struct ls1x_cpufreq {
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080024 struct device *dev;
25 struct clk *clk; /* CPU clk */
26 struct clk *mux_clk; /* MUX of CPU clk */
27 struct clk *pll_clk; /* PLL clk */
28 struct clk *osc_clk; /* OSC clk */
29 unsigned int max_freq;
30 unsigned int min_freq;
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080031};
32
33static struct ls1x_cpufreq *cpufreq;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080034
35static int ls1x_cpufreq_notifier(struct notifier_block *nb,
36 unsigned long val, void *data)
37{
38 if (val == CPUFREQ_POSTCHANGE)
39 current_cpu_data.udelay_val = loops_per_jiffy;
40
41 return NOTIFY_OK;
42}
43
44static struct notifier_block ls1x_cpufreq_notifier_block = {
45 .notifier_call = ls1x_cpufreq_notifier
46};
47
48static int ls1x_cpufreq_target(struct cpufreq_policy *policy,
49 unsigned int index)
50{
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080051 struct device *cpu_dev = get_cpu_device(policy->cpu);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080052 unsigned int old_freq, new_freq;
53
54 old_freq = policy->cur;
55 new_freq = policy->freq_table[index].frequency;
56
57 /*
58 * The procedure of reconfiguring CPU clk is as below.
59 *
60 * - Reparent CPU clk to OSC clk
61 * - Reset CPU clock (very important)
62 * - Reconfigure CPU DIV
63 * - Reparent CPU clk back to CPU DIV clk
64 */
65
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080066 clk_set_parent(policy->clk, cpufreq->osc_clk);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080067 __raw_writel(__raw_readl(LS1X_CLK_PLL_DIV) | RST_CPU_EN | RST_CPU,
68 LS1X_CLK_PLL_DIV);
69 __raw_writel(__raw_readl(LS1X_CLK_PLL_DIV) & ~(RST_CPU_EN | RST_CPU),
70 LS1X_CLK_PLL_DIV);
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080071 clk_set_rate(cpufreq->mux_clk, new_freq * 1000);
72 clk_set_parent(policy->clk, cpufreq->mux_clk);
73 dev_dbg(cpu_dev, "%u KHz --> %u KHz\n", old_freq, new_freq);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080074
75 return 0;
76}
77
78static int ls1x_cpufreq_init(struct cpufreq_policy *policy)
79{
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080080 struct device *cpu_dev = get_cpu_device(policy->cpu);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080081 struct cpufreq_frequency_table *freq_tbl;
82 unsigned int pll_freq, freq;
83 int steps, i, ret;
84
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080085 pll_freq = clk_get_rate(cpufreq->pll_clk) / 1000;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080086
87 steps = 1 << DIV_CPU_WIDTH;
Kelvin Cheung379e38a2016-04-12 18:40:16 +080088 freq_tbl = kcalloc(steps, sizeof(*freq_tbl), GFP_KERNEL);
89 if (!freq_tbl)
90 return -ENOMEM;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080091
92 for (i = 0; i < (steps - 1); i++) {
93 freq = pll_freq / (i + 1);
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080094 if ((freq < cpufreq->min_freq) || (freq > cpufreq->max_freq))
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080095 freq_tbl[i].frequency = CPUFREQ_ENTRY_INVALID;
96 else
97 freq_tbl[i].frequency = freq;
Kelvin Cheung99bf2e62016-04-12 18:40:18 +080098 dev_dbg(cpu_dev,
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +080099 "cpufreq table: index %d: frequency %d\n", i,
100 freq_tbl[i].frequency);
101 }
102 freq_tbl[i].frequency = CPUFREQ_TABLE_END;
103
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800104 policy->clk = cpufreq->clk;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800105 ret = cpufreq_generic_init(policy, freq_tbl, 0);
106 if (ret)
107 kfree(freq_tbl);
Kelvin Cheung379e38a2016-04-12 18:40:16 +0800108
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800109 return ret;
110}
111
112static int ls1x_cpufreq_exit(struct cpufreq_policy *policy)
113{
114 kfree(policy->freq_table);
115 return 0;
116}
117
118static struct cpufreq_driver ls1x_cpufreq_driver = {
119 .name = "cpufreq-ls1x",
120 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
121 .verify = cpufreq_generic_frequency_table_verify,
122 .target_index = ls1x_cpufreq_target,
123 .get = cpufreq_generic_get,
124 .init = ls1x_cpufreq_init,
125 .exit = ls1x_cpufreq_exit,
126 .attr = cpufreq_generic_attr,
127};
128
129static int ls1x_cpufreq_remove(struct platform_device *pdev)
130{
131 cpufreq_unregister_notifier(&ls1x_cpufreq_notifier_block,
132 CPUFREQ_TRANSITION_NOTIFIER);
133 cpufreq_unregister_driver(&ls1x_cpufreq_driver);
134
135 return 0;
136}
137
138static int ls1x_cpufreq_probe(struct platform_device *pdev)
139{
Kelvin Cheung25581d22016-04-12 18:40:17 +0800140 struct plat_ls1x_cpufreq *pdata = dev_get_platdata(&pdev->dev);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800141 struct clk *clk;
142 int ret;
143
Kelvin Cheung65b28492016-04-12 18:40:19 +0800144 if (!pdata || !pdata->clk_name || !pdata->osc_clk_name) {
145 dev_err(&pdev->dev, "platform data missing\n");
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800146 return -EINVAL;
Kelvin Cheung65b28492016-04-12 18:40:19 +0800147 }
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800148
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800149 cpufreq =
150 devm_kzalloc(&pdev->dev, sizeof(struct ls1x_cpufreq), GFP_KERNEL);
151 if (!cpufreq)
152 return -ENOMEM;
153
154 cpufreq->dev = &pdev->dev;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800155
156 clk = devm_clk_get(&pdev->dev, pdata->clk_name);
157 if (IS_ERR(clk)) {
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800158 dev_err(&pdev->dev, "unable to get %s clock\n",
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800159 pdata->clk_name);
Kelvin Cheung65b28492016-04-12 18:40:19 +0800160 return PTR_ERR(clk);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800161 }
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800162 cpufreq->clk = clk;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800163
164 clk = clk_get_parent(clk);
165 if (IS_ERR(clk)) {
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800166 dev_err(&pdev->dev, "unable to get parent of %s clock\n",
167 __clk_get_name(cpufreq->clk));
Kelvin Cheung65b28492016-04-12 18:40:19 +0800168 return PTR_ERR(clk);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800169 }
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800170 cpufreq->mux_clk = clk;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800171
172 clk = clk_get_parent(clk);
173 if (IS_ERR(clk)) {
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800174 dev_err(&pdev->dev, "unable to get parent of %s clock\n",
175 __clk_get_name(cpufreq->mux_clk));
Kelvin Cheung65b28492016-04-12 18:40:19 +0800176 return PTR_ERR(clk);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800177 }
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800178 cpufreq->pll_clk = clk;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800179
180 clk = devm_clk_get(&pdev->dev, pdata->osc_clk_name);
181 if (IS_ERR(clk)) {
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800182 dev_err(&pdev->dev, "unable to get %s clock\n",
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800183 pdata->osc_clk_name);
Kelvin Cheung65b28492016-04-12 18:40:19 +0800184 return PTR_ERR(clk);
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800185 }
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800186 cpufreq->osc_clk = clk;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800187
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800188 cpufreq->max_freq = pdata->max_freq;
189 cpufreq->min_freq = pdata->min_freq;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800190
191 ret = cpufreq_register_driver(&ls1x_cpufreq_driver);
192 if (ret) {
Kelvin Cheung99bf2e62016-04-12 18:40:18 +0800193 dev_err(&pdev->dev,
194 "failed to register CPUFreq driver: %d\n", ret);
Kelvin Cheung65b28492016-04-12 18:40:19 +0800195 return ret;
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800196 }
197
198 ret = cpufreq_register_notifier(&ls1x_cpufreq_notifier_block,
199 CPUFREQ_TRANSITION_NOTIFIER);
200
Kelvin Cheung65b28492016-04-12 18:40:19 +0800201 if (ret) {
202 dev_err(&pdev->dev,
203 "failed to register CPUFreq notifier: %d\n",ret);
204 cpufreq_unregister_driver(&ls1x_cpufreq_driver);
205 }
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800206
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800207 return ret;
208}
209
210static struct platform_driver ls1x_cpufreq_platdrv = {
Kelvin Cheung6a1d55c2016-04-12 18:40:15 +0800211 .probe = ls1x_cpufreq_probe,
212 .remove = ls1x_cpufreq_remove,
213 .driver = {
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800214 .name = "ls1x-cpufreq",
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800215 },
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800216};
217
218module_platform_driver(ls1x_cpufreq_platdrv);
219
220MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>");
Kelvin Cheung6a1d55c2016-04-12 18:40:15 +0800221MODULE_DESCRIPTION("Loongson1 CPUFreq driver");
Kelvin Cheunga0a22cf2014-10-17 18:23:31 +0800222MODULE_LICENSE("GPL");