Merge tag 'for-5.4-rc7-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[platform/kernel/linux-rpi.git] / drivers / cpufreq / kirkwood-cpufreq.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *      kirkwood_freq.c: cpufreq driver for the Marvell kirkwood
4  *
5  *      Copyright (C) 2013 Andrew Lunn <andrew@lunn.ch>
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/clk.h>
11 #include <linux/cpufreq.h>
12 #include <linux/of_device.h>
13 #include <linux/platform_device.h>
14 #include <linux/io.h>
15 #include <asm/proc-fns.h>
16
17 #define CPU_SW_INT_BLK BIT(28)
18
19 static struct priv
20 {
21         struct clk *cpu_clk;
22         struct clk *ddr_clk;
23         struct clk *powersave_clk;
24         struct device *dev;
25         void __iomem *base;
26 } priv;
27
28 #define STATE_CPU_FREQ 0x01
29 #define STATE_DDR_FREQ 0x02
30
31 /*
32  * Kirkwood can swap the clock to the CPU between two clocks:
33  *
34  * - cpu clk
35  * - ddr clk
36  *
37  * The frequencies are set at runtime before registering this table.
38  */
39 static struct cpufreq_frequency_table kirkwood_freq_table[] = {
40         {0, STATE_CPU_FREQ,     0}, /* CPU uses cpuclk */
41         {0, STATE_DDR_FREQ,     0}, /* CPU uses ddrclk */
42         {0, 0,                  CPUFREQ_TABLE_END},
43 };
44
45 static unsigned int kirkwood_cpufreq_get_cpu_frequency(unsigned int cpu)
46 {
47         return clk_get_rate(priv.powersave_clk) / 1000;
48 }
49
50 static int kirkwood_cpufreq_target(struct cpufreq_policy *policy,
51                             unsigned int index)
52 {
53         unsigned int state = kirkwood_freq_table[index].driver_data;
54         unsigned long reg;
55
56         local_irq_disable();
57
58         /* Disable interrupts to the CPU */
59         reg = readl_relaxed(priv.base);
60         reg |= CPU_SW_INT_BLK;
61         writel_relaxed(reg, priv.base);
62
63         switch (state) {
64         case STATE_CPU_FREQ:
65                 clk_set_parent(priv.powersave_clk, priv.cpu_clk);
66                 break;
67         case STATE_DDR_FREQ:
68                 clk_set_parent(priv.powersave_clk, priv.ddr_clk);
69                 break;
70         }
71
72         /* Wait-for-Interrupt, while the hardware changes frequency */
73         cpu_do_idle();
74
75         /* Enable interrupts to the CPU */
76         reg = readl_relaxed(priv.base);
77         reg &= ~CPU_SW_INT_BLK;
78         writel_relaxed(reg, priv.base);
79
80         local_irq_enable();
81
82         return 0;
83 }
84
85 /* Module init and exit code */
86 static int kirkwood_cpufreq_cpu_init(struct cpufreq_policy *policy)
87 {
88         cpufreq_generic_init(policy, kirkwood_freq_table, 5000);
89         return 0;
90 }
91
92 static struct cpufreq_driver kirkwood_cpufreq_driver = {
93         .flags  = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
94         .get    = kirkwood_cpufreq_get_cpu_frequency,
95         .verify = cpufreq_generic_frequency_table_verify,
96         .target_index = kirkwood_cpufreq_target,
97         .init   = kirkwood_cpufreq_cpu_init,
98         .name   = "kirkwood-cpufreq",
99         .attr   = cpufreq_generic_attr,
100 };
101
102 static int kirkwood_cpufreq_probe(struct platform_device *pdev)
103 {
104         struct device_node *np;
105         struct resource *res;
106         int err;
107
108         priv.dev = &pdev->dev;
109
110         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
111         priv.base = devm_ioremap_resource(&pdev->dev, res);
112         if (IS_ERR(priv.base))
113                 return PTR_ERR(priv.base);
114
115         np = of_cpu_device_node_get(0);
116         if (!np) {
117                 dev_err(&pdev->dev, "failed to get cpu device node\n");
118                 return -ENODEV;
119         }
120
121         priv.cpu_clk = of_clk_get_by_name(np, "cpu_clk");
122         if (IS_ERR(priv.cpu_clk)) {
123                 dev_err(priv.dev, "Unable to get cpuclk\n");
124                 err = PTR_ERR(priv.cpu_clk);
125                 goto out_node;
126         }
127
128         err = clk_prepare_enable(priv.cpu_clk);
129         if (err) {
130                 dev_err(priv.dev, "Unable to prepare cpuclk\n");
131                 goto out_node;
132         }
133
134         kirkwood_freq_table[0].frequency = clk_get_rate(priv.cpu_clk) / 1000;
135
136         priv.ddr_clk = of_clk_get_by_name(np, "ddrclk");
137         if (IS_ERR(priv.ddr_clk)) {
138                 dev_err(priv.dev, "Unable to get ddrclk\n");
139                 err = PTR_ERR(priv.ddr_clk);
140                 goto out_cpu;
141         }
142
143         err = clk_prepare_enable(priv.ddr_clk);
144         if (err) {
145                 dev_err(priv.dev, "Unable to prepare ddrclk\n");
146                 goto out_cpu;
147         }
148         kirkwood_freq_table[1].frequency = clk_get_rate(priv.ddr_clk) / 1000;
149
150         priv.powersave_clk = of_clk_get_by_name(np, "powersave");
151         if (IS_ERR(priv.powersave_clk)) {
152                 dev_err(priv.dev, "Unable to get powersave\n");
153                 err = PTR_ERR(priv.powersave_clk);
154                 goto out_ddr;
155         }
156         err = clk_prepare_enable(priv.powersave_clk);
157         if (err) {
158                 dev_err(priv.dev, "Unable to prepare powersave clk\n");
159                 goto out_ddr;
160         }
161
162         err = cpufreq_register_driver(&kirkwood_cpufreq_driver);
163         if (err) {
164                 dev_err(priv.dev, "Failed to register cpufreq driver\n");
165                 goto out_powersave;
166         }
167
168         of_node_put(np);
169         return 0;
170
171 out_powersave:
172         clk_disable_unprepare(priv.powersave_clk);
173 out_ddr:
174         clk_disable_unprepare(priv.ddr_clk);
175 out_cpu:
176         clk_disable_unprepare(priv.cpu_clk);
177 out_node:
178         of_node_put(np);
179
180         return err;
181 }
182
183 static int kirkwood_cpufreq_remove(struct platform_device *pdev)
184 {
185         cpufreq_unregister_driver(&kirkwood_cpufreq_driver);
186
187         clk_disable_unprepare(priv.powersave_clk);
188         clk_disable_unprepare(priv.ddr_clk);
189         clk_disable_unprepare(priv.cpu_clk);
190
191         return 0;
192 }
193
194 static struct platform_driver kirkwood_cpufreq_platform_driver = {
195         .probe = kirkwood_cpufreq_probe,
196         .remove = kirkwood_cpufreq_remove,
197         .driver = {
198                 .name = "kirkwood-cpufreq",
199         },
200 };
201
202 module_platform_driver(kirkwood_cpufreq_platform_driver);
203
204 MODULE_LICENSE("GPL v2");
205 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch");
206 MODULE_DESCRIPTION("cpufreq driver for Marvell's kirkwood CPU");
207 MODULE_ALIAS("platform:kirkwood-cpufreq");