Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[sfrench/cifs-2.6.git] / drivers / clk / samsung / clk-s3c2410.c
1 /*
2  * Copyright (c) 2013 Heiko Stuebner <heiko@sntech.de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * Common Clock Framework support for S3C2410 and following SoCs.
9  */
10
11 #include <linux/clk-provider.h>
12 #include <linux/of.h>
13 #include <linux/of_address.h>
14
15 #include <dt-bindings/clock/s3c2410.h>
16
17 #include "clk.h"
18 #include "clk-pll.h"
19
20 #define LOCKTIME        0x00
21 #define MPLLCON         0x04
22 #define UPLLCON         0x08
23 #define CLKCON          0x0c
24 #define CLKSLOW         0x10
25 #define CLKDIVN         0x14
26 #define CAMDIVN         0x18
27
28 /* the soc types */
29 enum supported_socs {
30         S3C2410,
31         S3C2440,
32         S3C2442,
33 };
34
35 /* list of PLLs to be registered */
36 enum s3c2410_plls {
37         mpll, upll,
38 };
39
40 static void __iomem *reg_base;
41
42 /*
43  * list of controller registers to be saved and restored during a
44  * suspend/resume cycle.
45  */
46 static unsigned long s3c2410_clk_regs[] __initdata = {
47         LOCKTIME,
48         MPLLCON,
49         UPLLCON,
50         CLKCON,
51         CLKSLOW,
52         CLKDIVN,
53         CAMDIVN,
54 };
55
56 PNAME(fclk_p) = { "mpll", "div_slow" };
57
58 static struct samsung_mux_clock s3c2410_common_muxes[] __initdata = {
59         MUX(FCLK, "fclk", fclk_p, CLKSLOW, 4, 1),
60 };
61
62 static struct clk_div_table divslow_d[] = {
63         { .val = 0, .div = 1 },
64         { .val = 1, .div = 2 },
65         { .val = 2, .div = 4 },
66         { .val = 3, .div = 6 },
67         { .val = 4, .div = 8 },
68         { .val = 5, .div = 10 },
69         { .val = 6, .div = 12 },
70         { .val = 7, .div = 14 },
71         { /* sentinel */ },
72 };
73
74 static struct samsung_div_clock s3c2410_common_dividers[] __initdata = {
75         DIV_T(0, "div_slow", "xti", CLKSLOW, 0, 3, divslow_d),
76         DIV(PCLK, "pclk", "hclk", CLKDIVN, 0, 1),
77 };
78
79 static struct samsung_gate_clock s3c2410_common_gates[] __initdata = {
80         GATE(PCLK_SPI, "spi", "pclk", CLKCON, 18, 0, 0),
81         GATE(PCLK_I2S, "i2s", "pclk", CLKCON, 17, 0, 0),
82         GATE(PCLK_I2C, "i2c", "pclk", CLKCON, 16, 0, 0),
83         GATE(PCLK_ADC, "adc", "pclk", CLKCON, 15, 0, 0),
84         GATE(PCLK_RTC, "rtc", "pclk", CLKCON, 14, 0, 0),
85         GATE(PCLK_GPIO, "gpio", "pclk", CLKCON, 13, CLK_IGNORE_UNUSED, 0),
86         GATE(PCLK_UART2, "uart2", "pclk", CLKCON, 12, 0, 0),
87         GATE(PCLK_UART1, "uart1", "pclk", CLKCON, 11, 0, 0),
88         GATE(PCLK_UART0, "uart0", "pclk", CLKCON, 10, 0, 0),
89         GATE(PCLK_SDI, "sdi", "pclk", CLKCON, 9, 0, 0),
90         GATE(PCLK_PWM, "pwm", "pclk", CLKCON, 8, 0, 0),
91         GATE(HCLK_USBD, "usb-device", "hclk", CLKCON, 7, 0, 0),
92         GATE(HCLK_USBH, "usb-host", "hclk", CLKCON, 6, 0, 0),
93         GATE(HCLK_LCD, "lcd", "hclk", CLKCON, 5, 0, 0),
94         GATE(HCLK_NAND, "nand", "hclk", CLKCON, 4, 0, 0),
95 };
96
97 /* should be added _after_ the soc-specific clocks are created */
98 static struct samsung_clock_alias s3c2410_common_aliases[] __initdata = {
99         ALIAS(PCLK_I2C, "s3c2410-i2c.0", "i2c"),
100         ALIAS(PCLK_ADC, NULL, "adc"),
101         ALIAS(PCLK_RTC, NULL, "rtc"),
102         ALIAS(PCLK_PWM, NULL, "timers"),
103         ALIAS(HCLK_LCD, NULL, "lcd"),
104         ALIAS(HCLK_USBD, NULL, "usb-device"),
105         ALIAS(HCLK_USBH, NULL, "usb-host"),
106         ALIAS(UCLK, NULL, "usb-bus-host"),
107         ALIAS(UCLK, NULL, "usb-bus-gadget"),
108         ALIAS(ARMCLK, NULL, "armclk"),
109         ALIAS(UCLK, NULL, "uclk"),
110         ALIAS(HCLK, NULL, "hclk"),
111         ALIAS(MPLL, NULL, "mpll"),
112         ALIAS(FCLK, NULL, "fclk"),
113         ALIAS(PCLK, NULL, "watchdog"),
114         ALIAS(PCLK_SDI, NULL, "sdi"),
115         ALIAS(HCLK_NAND, NULL, "nand"),
116         ALIAS(PCLK_I2S, NULL, "iis"),
117         ALIAS(PCLK_I2C, NULL, "i2c"),
118 };
119
120 /* S3C2410 specific clocks */
121
122 static struct samsung_pll_rate_table pll_s3c2410_12mhz_tbl[] __initdata = {
123         /* sorted in descending order */
124         /* 2410A extras */
125         PLL_S3C2410_MPLL_RATE(12 * MHZ, 270000000, 127, 1, 1),
126         PLL_S3C2410_MPLL_RATE(12 * MHZ, 268000000, 126, 1, 1),
127         PLL_S3C2410_MPLL_RATE(12 * MHZ, 266000000, 125, 1, 1),
128         PLL_S3C2410_MPLL_RATE(12 * MHZ, 226000000, 105, 1, 1),
129         PLL_S3C2410_MPLL_RATE(12 * MHZ, 210000000, 132, 2, 1),
130         /* 2410 common */
131         PLL_S3C2410_MPLL_RATE(12 * MHZ, 202800000, 161, 3, 1),
132         PLL_S3C2410_MPLL_RATE(12 * MHZ, 192000000, 88, 1, 1),
133         PLL_S3C2410_MPLL_RATE(12 * MHZ, 186000000, 85, 1, 1),
134         PLL_S3C2410_MPLL_RATE(12 * MHZ, 180000000, 82, 1, 1),
135         PLL_S3C2410_MPLL_RATE(12 * MHZ, 170000000, 77, 1, 1),
136         PLL_S3C2410_MPLL_RATE(12 * MHZ, 158000000, 71, 1, 1),
137         PLL_S3C2410_MPLL_RATE(12 * MHZ, 152000000, 68, 1, 1),
138         PLL_S3C2410_MPLL_RATE(12 * MHZ, 147000000, 90, 2, 1),
139         PLL_S3C2410_MPLL_RATE(12 * MHZ, 135000000, 82, 2, 1),
140         PLL_S3C2410_MPLL_RATE(12 * MHZ, 124000000, 116, 1, 2),
141         PLL_S3C2410_MPLL_RATE(12 * MHZ, 118500000, 150, 2, 2),
142         PLL_S3C2410_MPLL_RATE(12 * MHZ, 113000000, 105, 1, 2),
143         PLL_S3C2410_MPLL_RATE(12 * MHZ, 101250000, 127, 2, 2),
144         PLL_S3C2410_MPLL_RATE(12 * MHZ, 90000000, 112, 2, 2),
145         PLL_S3C2410_MPLL_RATE(12 * MHZ, 84750000, 105, 2, 2),
146         PLL_S3C2410_MPLL_RATE(12 * MHZ, 79000000, 71, 1, 2),
147         PLL_S3C2410_MPLL_RATE(12 * MHZ, 67500000, 82, 2, 2),
148         PLL_S3C2410_MPLL_RATE(12 * MHZ, 56250000, 142, 2, 3),
149         PLL_S3C2410_MPLL_RATE(12 * MHZ, 48000000, 120, 2, 3),
150         PLL_S3C2410_MPLL_RATE(12 * MHZ, 50700000, 161, 3, 3),
151         PLL_S3C2410_MPLL_RATE(12 * MHZ, 45000000, 82, 1, 3),
152         PLL_S3C2410_MPLL_RATE(12 * MHZ, 33750000, 82, 2, 3),
153         { /* sentinel */ },
154 };
155
156 static struct samsung_pll_clock s3c2410_plls[] __initdata = {
157         [mpll] = PLL(pll_s3c2410_mpll, MPLL, "mpll", "xti",
158                                                 LOCKTIME, MPLLCON, NULL),
159         [upll] = PLL(pll_s3c2410_upll, UPLL, "upll", "xti",
160                                                 LOCKTIME, UPLLCON, NULL),
161 };
162
163 static struct samsung_div_clock s3c2410_dividers[] __initdata = {
164         DIV(HCLK, "hclk", "mpll", CLKDIVN, 1, 1),
165 };
166
167 static struct samsung_fixed_factor_clock s3c2410_ffactor[] __initdata = {
168         /*
169          * armclk is directly supplied by the fclk, without
170          * switching possibility like on the s3c244x below.
171          */
172         FFACTOR(ARMCLK, "armclk", "fclk", 1, 1, 0),
173
174         /* uclk is fed from the unmodified upll */
175         FFACTOR(UCLK, "uclk", "upll", 1, 1, 0),
176 };
177
178 static struct samsung_clock_alias s3c2410_aliases[] __initdata = {
179         ALIAS(PCLK_UART0, "s3c2410-uart.0", "uart"),
180         ALIAS(PCLK_UART1, "s3c2410-uart.1", "uart"),
181         ALIAS(PCLK_UART2, "s3c2410-uart.2", "uart"),
182         ALIAS(PCLK_UART0, "s3c2410-uart.0", "clk_uart_baud0"),
183         ALIAS(PCLK_UART1, "s3c2410-uart.1", "clk_uart_baud0"),
184         ALIAS(PCLK_UART2, "s3c2410-uart.2", "clk_uart_baud0"),
185         ALIAS(UCLK, NULL, "clk_uart_baud1"),
186 };
187
188 /* S3C244x specific clocks */
189
190 static struct samsung_pll_rate_table pll_s3c244x_12mhz_tbl[] __initdata = {
191         /* sorted in descending order */
192         PLL_S3C2440_MPLL_RATE(12 * MHZ, 400000000, 0x5c, 1, 1),
193         PLL_S3C2440_MPLL_RATE(12 * MHZ, 390000000, 0x7a, 2, 1),
194         PLL_S3C2440_MPLL_RATE(12 * MHZ, 380000000, 0x57, 1, 1),
195         PLL_S3C2440_MPLL_RATE(12 * MHZ, 370000000, 0xb1, 4, 1),
196         PLL_S3C2440_MPLL_RATE(12 * MHZ, 360000000, 0x70, 2, 1),
197         PLL_S3C2440_MPLL_RATE(12 * MHZ, 350000000, 0xa7, 4, 1),
198         PLL_S3C2440_MPLL_RATE(12 * MHZ, 340000000, 0x4d, 1, 1),
199         PLL_S3C2440_MPLL_RATE(12 * MHZ, 330000000, 0x66, 2, 1),
200         PLL_S3C2440_MPLL_RATE(12 * MHZ, 320000000, 0x98, 4, 1),
201         PLL_S3C2440_MPLL_RATE(12 * MHZ, 310000000, 0x93, 4, 1),
202         PLL_S3C2440_MPLL_RATE(12 * MHZ, 300000000, 0x75, 3, 1),
203         PLL_S3C2440_MPLL_RATE(12 * MHZ, 240000000, 0x70, 1, 2),
204         PLL_S3C2440_MPLL_RATE(12 * MHZ, 230000000, 0x6b, 1, 2),
205         PLL_S3C2440_MPLL_RATE(12 * MHZ, 220000000, 0x66, 1, 2),
206         PLL_S3C2440_MPLL_RATE(12 * MHZ, 210000000, 0x84, 2, 2),
207         PLL_S3C2440_MPLL_RATE(12 * MHZ, 200000000, 0x5c, 1, 2),
208         PLL_S3C2440_MPLL_RATE(12 * MHZ, 190000000, 0x57, 1, 2),
209         PLL_S3C2440_MPLL_RATE(12 * MHZ, 180000000, 0x70, 2, 2),
210         PLL_S3C2440_MPLL_RATE(12 * MHZ, 170000000, 0x4d, 1, 2),
211         PLL_S3C2440_MPLL_RATE(12 * MHZ, 160000000, 0x98, 4, 2),
212         PLL_S3C2440_MPLL_RATE(12 * MHZ, 150000000, 0x75, 3, 2),
213         PLL_S3C2440_MPLL_RATE(12 * MHZ, 120000000, 0x70, 1, 3),
214         PLL_S3C2440_MPLL_RATE(12 * MHZ, 110000000, 0x66, 1, 3),
215         PLL_S3C2440_MPLL_RATE(12 * MHZ, 100000000, 0x5c, 1, 3),
216         PLL_S3C2440_MPLL_RATE(12 * MHZ, 90000000, 0x70, 2, 3),
217         PLL_S3C2440_MPLL_RATE(12 * MHZ, 80000000, 0x98, 4, 3),
218         PLL_S3C2440_MPLL_RATE(12 * MHZ, 75000000, 0x75, 3, 3),
219         { /* sentinel */ },
220 };
221
222 static struct samsung_pll_clock s3c244x_common_plls[] __initdata = {
223         [mpll] = PLL(pll_s3c2440_mpll, MPLL, "mpll", "xti",
224                                                 LOCKTIME, MPLLCON, NULL),
225         [upll] = PLL(pll_s3c2410_upll, UPLL, "upll", "xti",
226                                                 LOCKTIME, UPLLCON, NULL),
227 };
228
229 PNAME(hclk_p) = { "fclk", "div_hclk_2", "div_hclk_4", "div_hclk_3" };
230 PNAME(armclk_p) = { "fclk", "hclk" };
231
232 static struct samsung_mux_clock s3c244x_common_muxes[] __initdata = {
233         MUX(HCLK, "hclk", hclk_p, CLKDIVN, 1, 2),
234         MUX(ARMCLK, "armclk", armclk_p, CAMDIVN, 12, 1),
235 };
236
237 static struct samsung_fixed_factor_clock s3c244x_common_ffactor[] __initdata = {
238         FFACTOR(0, "div_hclk_2", "fclk", 1, 2, 0),
239         FFACTOR(0, "ff_cam", "div_cam", 2, 1, CLK_SET_RATE_PARENT),
240 };
241
242 static struct clk_div_table div_hclk_4_d[] = {
243         { .val = 0, .div = 4 },
244         { .val = 1, .div = 8 },
245         { /* sentinel */ },
246 };
247
248 static struct clk_div_table div_hclk_3_d[] = {
249         { .val = 0, .div = 3 },
250         { .val = 1, .div = 6 },
251         { /* sentinel */ },
252 };
253
254 static struct samsung_div_clock s3c244x_common_dividers[] __initdata = {
255         DIV(UCLK, "uclk", "upll", CLKDIVN, 3, 1),
256         DIV(0, "div_hclk", "fclk", CLKDIVN, 1, 1),
257         DIV_T(0, "div_hclk_4", "fclk", CAMDIVN, 9, 1, div_hclk_4_d),
258         DIV_T(0, "div_hclk_3", "fclk", CAMDIVN, 8, 1, div_hclk_3_d),
259         DIV(0, "div_cam", "upll", CAMDIVN, 0, 3),
260 };
261
262 static struct samsung_gate_clock s3c244x_common_gates[] __initdata = {
263         GATE(HCLK_CAM, "cam", "hclk", CLKCON, 19, 0, 0),
264 };
265
266 static struct samsung_clock_alias s3c244x_common_aliases[] __initdata = {
267         ALIAS(PCLK_UART0, "s3c2440-uart.0", "uart"),
268         ALIAS(PCLK_UART1, "s3c2440-uart.1", "uart"),
269         ALIAS(PCLK_UART2, "s3c2440-uart.2", "uart"),
270         ALIAS(PCLK_UART0, "s3c2440-uart.0", "clk_uart_baud2"),
271         ALIAS(PCLK_UART1, "s3c2440-uart.1", "clk_uart_baud2"),
272         ALIAS(PCLK_UART2, "s3c2440-uart.2", "clk_uart_baud2"),
273         ALIAS(HCLK_CAM, NULL, "camif"),
274         ALIAS(CAMIF, NULL, "camif-upll"),
275 };
276
277 /* S3C2440 specific clocks */
278
279 PNAME(s3c2440_camif_p) = { "upll", "ff_cam" };
280
281 static struct samsung_mux_clock s3c2440_muxes[] __initdata = {
282         MUX(CAMIF, "camif", s3c2440_camif_p, CAMDIVN, 4, 1),
283 };
284
285 static struct samsung_gate_clock s3c2440_gates[] __initdata = {
286         GATE(PCLK_AC97, "ac97", "pclk", CLKCON, 20, 0, 0),
287 };
288
289 /* S3C2442 specific clocks */
290
291 static struct samsung_fixed_factor_clock s3c2442_ffactor[] __initdata = {
292         FFACTOR(0, "upll_3", "upll", 1, 3, 0),
293 };
294
295 PNAME(s3c2442_camif_p) = { "upll", "ff_cam", "upll", "upll_3" };
296
297 static struct samsung_mux_clock s3c2442_muxes[] __initdata = {
298         MUX(CAMIF, "camif", s3c2442_camif_p, CAMDIVN, 4, 2),
299 };
300
301 /*
302  * fixed rate clocks generated outside the soc
303  * Only necessary until the devicetree-move is complete
304  */
305 #define XTI     1
306 static struct samsung_fixed_rate_clock s3c2410_common_frate_clks[] __initdata = {
307         FRATE(XTI, "xti", NULL, 0, 0),
308 };
309
310 static void __init s3c2410_common_clk_register_fixed_ext(
311                 struct samsung_clk_provider *ctx,
312                 unsigned long xti_f)
313 {
314         struct samsung_clock_alias xti_alias = ALIAS(XTI, NULL, "xtal");
315
316         s3c2410_common_frate_clks[0].fixed_rate = xti_f;
317         samsung_clk_register_fixed_rate(ctx, s3c2410_common_frate_clks,
318                                 ARRAY_SIZE(s3c2410_common_frate_clks));
319
320         samsung_clk_register_alias(ctx, &xti_alias, 1);
321 }
322
323 void __init s3c2410_common_clk_init(struct device_node *np, unsigned long xti_f,
324                                     int current_soc,
325                                     void __iomem *base)
326 {
327         struct samsung_clk_provider *ctx;
328         reg_base = base;
329
330         if (np) {
331                 reg_base = of_iomap(np, 0);
332                 if (!reg_base)
333                         panic("%s: failed to map registers\n", __func__);
334         }
335
336         ctx = samsung_clk_init(np, reg_base, NR_CLKS);
337
338         /* Register external clocks only in non-dt cases */
339         if (!np)
340                 s3c2410_common_clk_register_fixed_ext(ctx, xti_f);
341
342         if (current_soc == S3C2410) {
343                 if (_get_rate("xti") == 12 * MHZ) {
344                         s3c2410_plls[mpll].rate_table = pll_s3c2410_12mhz_tbl;
345                         s3c2410_plls[upll].rate_table = pll_s3c2410_12mhz_tbl;
346                 }
347
348                 /* Register PLLs. */
349                 samsung_clk_register_pll(ctx, s3c2410_plls,
350                                 ARRAY_SIZE(s3c2410_plls), reg_base);
351
352         } else { /* S3C2440, S3C2442 */
353                 if (_get_rate("xti") == 12 * MHZ) {
354                         /*
355                          * plls follow different calculation schemes, with the
356                          * upll following the same scheme as the s3c2410 plls
357                          */
358                         s3c244x_common_plls[mpll].rate_table =
359                                                         pll_s3c244x_12mhz_tbl;
360                         s3c244x_common_plls[upll].rate_table =
361                                                         pll_s3c2410_12mhz_tbl;
362                 }
363
364                 /* Register PLLs. */
365                 samsung_clk_register_pll(ctx, s3c244x_common_plls,
366                                 ARRAY_SIZE(s3c244x_common_plls), reg_base);
367         }
368
369         /* Register common internal clocks. */
370         samsung_clk_register_mux(ctx, s3c2410_common_muxes,
371                         ARRAY_SIZE(s3c2410_common_muxes));
372         samsung_clk_register_div(ctx, s3c2410_common_dividers,
373                         ARRAY_SIZE(s3c2410_common_dividers));
374         samsung_clk_register_gate(ctx, s3c2410_common_gates,
375                 ARRAY_SIZE(s3c2410_common_gates));
376
377         if (current_soc == S3C2440 || current_soc == S3C2442) {
378                 samsung_clk_register_div(ctx, s3c244x_common_dividers,
379                                 ARRAY_SIZE(s3c244x_common_dividers));
380                 samsung_clk_register_gate(ctx, s3c244x_common_gates,
381                                 ARRAY_SIZE(s3c244x_common_gates));
382                 samsung_clk_register_mux(ctx, s3c244x_common_muxes,
383                                 ARRAY_SIZE(s3c244x_common_muxes));
384                 samsung_clk_register_fixed_factor(ctx, s3c244x_common_ffactor,
385                                 ARRAY_SIZE(s3c244x_common_ffactor));
386         }
387
388         /* Register SoC-specific clocks. */
389         switch (current_soc) {
390         case S3C2410:
391                 samsung_clk_register_div(ctx, s3c2410_dividers,
392                                 ARRAY_SIZE(s3c2410_dividers));
393                 samsung_clk_register_fixed_factor(ctx, s3c2410_ffactor,
394                                 ARRAY_SIZE(s3c2410_ffactor));
395                 samsung_clk_register_alias(ctx, s3c2410_aliases,
396                         ARRAY_SIZE(s3c2410_aliases));
397                 break;
398         case S3C2440:
399                 samsung_clk_register_mux(ctx, s3c2440_muxes,
400                                 ARRAY_SIZE(s3c2440_muxes));
401                 samsung_clk_register_gate(ctx, s3c2440_gates,
402                                 ARRAY_SIZE(s3c2440_gates));
403                 break;
404         case S3C2442:
405                 samsung_clk_register_mux(ctx, s3c2442_muxes,
406                                 ARRAY_SIZE(s3c2442_muxes));
407                 samsung_clk_register_fixed_factor(ctx, s3c2442_ffactor,
408                                 ARRAY_SIZE(s3c2442_ffactor));
409                 break;
410         }
411
412         /*
413          * Register common aliases at the end, as some of the aliased clocks
414          * are SoC specific.
415          */
416         samsung_clk_register_alias(ctx, s3c2410_common_aliases,
417                 ARRAY_SIZE(s3c2410_common_aliases));
418
419         if (current_soc == S3C2440 || current_soc == S3C2442) {
420                 samsung_clk_register_alias(ctx, s3c244x_common_aliases,
421                         ARRAY_SIZE(s3c244x_common_aliases));
422         }
423
424         samsung_clk_sleep_init(reg_base, s3c2410_clk_regs,
425                                ARRAY_SIZE(s3c2410_clk_regs));
426
427         samsung_clk_of_add_provider(np, ctx);
428 }
429
430 static void __init s3c2410_clk_init(struct device_node *np)
431 {
432         s3c2410_common_clk_init(np, 0, S3C2410, NULL);
433 }
434 CLK_OF_DECLARE(s3c2410_clk, "samsung,s3c2410-clock", s3c2410_clk_init);
435
436 static void __init s3c2440_clk_init(struct device_node *np)
437 {
438         s3c2410_common_clk_init(np, 0, S3C2440, NULL);
439 }
440 CLK_OF_DECLARE(s3c2440_clk, "samsung,s3c2440-clock", s3c2440_clk_init);
441
442 static void __init s3c2442_clk_init(struct device_node *np)
443 {
444         s3c2410_common_clk_init(np, 0, S3C2442, NULL);
445 }
446 CLK_OF_DECLARE(s3c2442_clk, "samsung,s3c2442-clock", s3c2442_clk_init);