1
2
3
4
5
6
7
8
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
20#include <cpufreq.h>
21#include <loongson1.h>
22
23struct ls1x_cpufreq {
24 struct device *dev;
25 struct clk *clk;
26 struct clk *mux_clk;
27 struct clk *pll_clk;
28 struct clk *osc_clk;
29 unsigned int max_freq;
30 unsigned int min_freq;
31};
32
33static struct ls1x_cpufreq *cpufreq;
34
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{
51 struct device *cpu_dev = get_cpu_device(policy->cpu);
52 unsigned int old_freq, new_freq;
53
54 old_freq = policy->cur;
55 new_freq = policy->freq_table[index].frequency;
56
57
58
59
60
61
62
63
64
65
66 clk_set_parent(policy->clk, cpufreq->osc_clk);
67 __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);
71 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);
74
75 return 0;
76}
77
78static int ls1x_cpufreq_init(struct cpufreq_policy *policy)
79{
80 struct device *cpu_dev = get_cpu_device(policy->cpu);
81 struct cpufreq_frequency_table *freq_tbl;
82 unsigned int pll_freq, freq;
83 int steps, i;
84
85 pll_freq = clk_get_rate(cpufreq->pll_clk) / 1000;
86
87 steps = 1 << DIV_CPU_WIDTH;
88 freq_tbl = kcalloc(steps, sizeof(*freq_tbl), GFP_KERNEL);
89 if (!freq_tbl)
90 return -ENOMEM;
91
92 for (i = 0; i < (steps - 1); i++) {
93 freq = pll_freq / (i + 1);
94 if ((freq < cpufreq->min_freq) || (freq > cpufreq->max_freq))
95 freq_tbl[i].frequency = CPUFREQ_ENTRY_INVALID;
96 else
97 freq_tbl[i].frequency = freq;
98 dev_dbg(cpu_dev,
99 "cpufreq table: index %d: frequency %d\n", i,
100 freq_tbl[i].frequency);
101 }
102 freq_tbl[i].frequency = CPUFREQ_TABLE_END;
103
104 policy->clk = cpufreq->clk;
105 cpufreq_generic_init(policy, freq_tbl, 0);
106
107 return 0;
108}
109
110static int ls1x_cpufreq_exit(struct cpufreq_policy *policy)
111{
112 kfree(policy->freq_table);
113 return 0;
114}
115
116static struct cpufreq_driver ls1x_cpufreq_driver = {
117 .name = "cpufreq-ls1x",
118 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
119 .verify = cpufreq_generic_frequency_table_verify,
120 .target_index = ls1x_cpufreq_target,
121 .get = cpufreq_generic_get,
122 .init = ls1x_cpufreq_init,
123 .exit = ls1x_cpufreq_exit,
124 .attr = cpufreq_generic_attr,
125};
126
127static int ls1x_cpufreq_remove(struct platform_device *pdev)
128{
129 cpufreq_unregister_notifier(&ls1x_cpufreq_notifier_block,
130 CPUFREQ_TRANSITION_NOTIFIER);
131 cpufreq_unregister_driver(&ls1x_cpufreq_driver);
132
133 return 0;
134}
135
136static int ls1x_cpufreq_probe(struct platform_device *pdev)
137{
138 struct plat_ls1x_cpufreq *pdata = dev_get_platdata(&pdev->dev);
139 struct clk *clk;
140 int ret;
141
142 if (!pdata || !pdata->clk_name || !pdata->osc_clk_name) {
143 dev_err(&pdev->dev, "platform data missing\n");
144 return -EINVAL;
145 }
146
147 cpufreq =
148 devm_kzalloc(&pdev->dev, sizeof(struct ls1x_cpufreq), GFP_KERNEL);
149 if (!cpufreq)
150 return -ENOMEM;
151
152 cpufreq->dev = &pdev->dev;
153
154 clk = devm_clk_get(&pdev->dev, pdata->clk_name);
155 if (IS_ERR(clk)) {
156 dev_err(&pdev->dev, "unable to get %s clock\n",
157 pdata->clk_name);
158 return PTR_ERR(clk);
159 }
160 cpufreq->clk = clk;
161
162 clk = clk_get_parent(clk);
163 if (IS_ERR(clk)) {
164 dev_err(&pdev->dev, "unable to get parent of %s clock\n",
165 __clk_get_name(cpufreq->clk));
166 return PTR_ERR(clk);
167 }
168 cpufreq->mux_clk = clk;
169
170 clk = clk_get_parent(clk);
171 if (IS_ERR(clk)) {
172 dev_err(&pdev->dev, "unable to get parent of %s clock\n",
173 __clk_get_name(cpufreq->mux_clk));
174 return PTR_ERR(clk);
175 }
176 cpufreq->pll_clk = clk;
177
178 clk = devm_clk_get(&pdev->dev, pdata->osc_clk_name);
179 if (IS_ERR(clk)) {
180 dev_err(&pdev->dev, "unable to get %s clock\n",
181 pdata->osc_clk_name);
182 return PTR_ERR(clk);
183 }
184 cpufreq->osc_clk = clk;
185
186 cpufreq->max_freq = pdata->max_freq;
187 cpufreq->min_freq = pdata->min_freq;
188
189 ret = cpufreq_register_driver(&ls1x_cpufreq_driver);
190 if (ret) {
191 dev_err(&pdev->dev,
192 "failed to register CPUFreq driver: %d\n", ret);
193 return ret;
194 }
195
196 ret = cpufreq_register_notifier(&ls1x_cpufreq_notifier_block,
197 CPUFREQ_TRANSITION_NOTIFIER);
198
199 if (ret) {
200 dev_err(&pdev->dev,
201 "failed to register CPUFreq notifier: %d\n",ret);
202 cpufreq_unregister_driver(&ls1x_cpufreq_driver);
203 }
204
205 return ret;
206}
207
208static struct platform_driver ls1x_cpufreq_platdrv = {
209 .probe = ls1x_cpufreq_probe,
210 .remove = ls1x_cpufreq_remove,
211 .driver = {
212 .name = "ls1x-cpufreq",
213 },
214};
215
216module_platform_driver(ls1x_cpufreq_platdrv);
217
218MODULE_AUTHOR("Kelvin Cheung <keguang.zhang@gmail.com>");
219MODULE_DESCRIPTION("Loongson1 CPUFreq driver");
220MODULE_LICENSE("GPL");
221