]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/clk/mvebu/ap806-system-controller.c
73ba8fd7860f9839b09d31f2575a701837f57cf8
[linux.git] / drivers / clk / mvebu / ap806-system-controller.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Marvell Armada AP806 System Controller
4  *
5  * Copyright (C) 2016 Marvell
6  *
7  * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
8  *
9  */
10
11 #define pr_fmt(fmt) "ap806-system-controller: " fmt
12
13 #include "armada_ap_cp_helper.h"
14 #include <linux/clk-provider.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/init.h>
17 #include <linux/of.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
20
21 #define AP806_SAR_REG                   0x400
22 #define AP806_SAR_CLKFREQ_MODE_MASK     0x1f
23
24 #define AP806_CLK_NUM                   5
25
26 static struct clk *ap806_clks[AP806_CLK_NUM];
27
28 static struct clk_onecell_data ap806_clk_data = {
29         .clks = ap806_clks,
30         .clk_num = AP806_CLK_NUM,
31 };
32
33 static int ap806_syscon_common_probe(struct platform_device *pdev,
34                                      struct device_node *syscon_node)
35 {
36         unsigned int freq_mode, cpuclk_freq;
37         const char *name, *fixedclk_name;
38         struct device *dev = &pdev->dev;
39         struct device_node *np = dev->of_node;
40         struct regmap *regmap;
41         u32 reg;
42         int ret;
43
44         regmap = syscon_node_to_regmap(syscon_node);
45         if (IS_ERR(regmap)) {
46                 dev_err(dev, "cannot get regmap\n");
47                 return PTR_ERR(regmap);
48         }
49
50         ret = regmap_read(regmap, AP806_SAR_REG, &reg);
51         if (ret) {
52                 dev_err(dev, "cannot read from regmap\n");
53                 return ret;
54         }
55
56         freq_mode = reg & AP806_SAR_CLKFREQ_MODE_MASK;
57         switch (freq_mode) {
58         case 0x0:
59         case 0x1:
60                 cpuclk_freq = 2000;
61                 break;
62         case 0x6:
63         case 0x7:
64                 cpuclk_freq = 1800;
65                 break;
66         case 0x4:
67         case 0xB:
68         case 0xD:
69                 cpuclk_freq = 1600;
70                 break;
71         case 0x1a:
72                 cpuclk_freq = 1400;
73                 break;
74         case 0x14:
75         case 0x17:
76                 cpuclk_freq = 1300;
77                 break;
78         case 0x19:
79                 cpuclk_freq = 1200;
80                 break;
81         case 0x13:
82         case 0x1d:
83                 cpuclk_freq = 1000;
84                 break;
85         case 0x1c:
86                 cpuclk_freq = 800;
87                 break;
88         case 0x1b:
89                 cpuclk_freq = 600;
90                 break;
91         default:
92                 dev_err(dev, "invalid SAR value\n");
93                 return -EINVAL;
94         }
95
96         /* Convert to hertz */
97         cpuclk_freq *= 1000 * 1000;
98
99         /* CPU clocks depend on the Sample At Reset configuration */
100         name = ap_cp_unique_name(dev, syscon_node, "pll-cluster-0");
101         ap806_clks[0] = clk_register_fixed_rate(dev, name, NULL,
102                                                 0, cpuclk_freq);
103         if (IS_ERR(ap806_clks[0])) {
104                 ret = PTR_ERR(ap806_clks[0]);
105                 goto fail0;
106         }
107
108         name = ap_cp_unique_name(dev, syscon_node, "pll-cluster-1");
109         ap806_clks[1] = clk_register_fixed_rate(dev, name, NULL, 0,
110                                                 cpuclk_freq);
111         if (IS_ERR(ap806_clks[1])) {
112                 ret = PTR_ERR(ap806_clks[1]);
113                 goto fail1;
114         }
115
116         /* Fixed clock is always 1200 Mhz */
117         fixedclk_name = ap_cp_unique_name(dev, syscon_node, "fixed");
118         ap806_clks[2] = clk_register_fixed_rate(dev, fixedclk_name, NULL,
119                                                 0, 1200 * 1000 * 1000);
120         if (IS_ERR(ap806_clks[2])) {
121                 ret = PTR_ERR(ap806_clks[2]);
122                 goto fail2;
123         }
124
125         /* MSS Clock is fixed clock divided by 6 */
126         name = ap_cp_unique_name(dev, syscon_node, "mss");
127         ap806_clks[3] = clk_register_fixed_factor(NULL, name, fixedclk_name,
128                                                   0, 1, 6);
129         if (IS_ERR(ap806_clks[3])) {
130                 ret = PTR_ERR(ap806_clks[3]);
131                 goto fail3;
132         }
133
134         /* SDIO(/eMMC) Clock is fixed clock divided by 3 */
135         name = ap_cp_unique_name(dev, syscon_node, "sdio");
136         ap806_clks[4] = clk_register_fixed_factor(NULL, name,
137                                                   fixedclk_name,
138                                                   0, 1, 3);
139         if (IS_ERR(ap806_clks[4])) {
140                 ret = PTR_ERR(ap806_clks[4]);
141                 goto fail4;
142         }
143
144         ret = of_clk_add_provider(np, of_clk_src_onecell_get, &ap806_clk_data);
145         if (ret)
146                 goto fail_clk_add;
147
148         return 0;
149
150 fail_clk_add:
151         clk_unregister_fixed_factor(ap806_clks[4]);
152 fail4:
153         clk_unregister_fixed_factor(ap806_clks[3]);
154 fail3:
155         clk_unregister_fixed_rate(ap806_clks[2]);
156 fail2:
157         clk_unregister_fixed_rate(ap806_clks[1]);
158 fail1:
159         clk_unregister_fixed_rate(ap806_clks[0]);
160 fail0:
161         return ret;
162 }
163
164 static int ap806_syscon_legacy_probe(struct platform_device *pdev)
165 {
166         dev_warn(&pdev->dev, FW_WARN "Using legacy device tree binding\n");
167         dev_warn(&pdev->dev, FW_WARN "Update your device tree:\n");
168         dev_warn(&pdev->dev, FW_WARN
169                  "This binding won't be supported in future kernel\n");
170
171         return ap806_syscon_common_probe(pdev, pdev->dev.of_node);
172
173 }
174
175 static int ap806_clock_probe(struct platform_device *pdev)
176 {
177         return ap806_syscon_common_probe(pdev, pdev->dev.of_node->parent);
178 }
179
180 static const struct of_device_id ap806_syscon_legacy_of_match[] = {
181         { .compatible = "marvell,ap806-system-controller", },
182         { }
183 };
184
185 static struct platform_driver ap806_syscon_legacy_driver = {
186         .probe = ap806_syscon_legacy_probe,
187         .driver         = {
188                 .name   = "marvell-ap806-system-controller",
189                 .of_match_table = ap806_syscon_legacy_of_match,
190                 .suppress_bind_attrs = true,
191         },
192 };
193 builtin_platform_driver(ap806_syscon_legacy_driver);
194
195 static const struct of_device_id ap806_clock_of_match[] = {
196         { .compatible = "marvell,ap806-clock", },
197         { }
198 };
199
200 static struct platform_driver ap806_clock_driver = {
201         .probe = ap806_clock_probe,
202         .driver         = {
203                 .name   = "marvell-ap806-clock",
204                 .of_match_table = ap806_clock_of_match,
205                 .suppress_bind_attrs = true,
206         },
207 };
208 builtin_platform_driver(ap806_clock_driver);