0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/clk.h>
0009 #include <linux/clk-provider.h>
0010 #include <linux/clkdev.h>
0011 #include <linux/io.h>
0012 #include <linux/of.h>
0013 #include <linux/of_address.h>
0014 #include <linux/reset-controller.h>
0015 #include <linux/slab.h>
0016 #include <linux/spinlock.h>
0017 #include <linux/log2.h>
0018
0019 #include "clk-factors.h"
0020
0021 static DEFINE_SPINLOCK(clk_lock);
0022
0023
0024 #define SUNXI_MAX_PARENTS 5
0025
0026
0027
0028
0029
0030
0031
0032
0033 static void sun4i_get_pll1_factors(struct factors_request *req)
0034 {
0035 u8 div;
0036
0037
0038 div = req->rate / 6000000;
0039 req->rate = 6000000 * div;
0040
0041
0042 req->m = 0;
0043
0044
0045 if (req->rate >= 768000000 || req->rate == 42000000 ||
0046 req->rate == 54000000)
0047 req->k = 1;
0048 else
0049 req->k = 0;
0050
0051
0052 if (div < 10)
0053 req->p = 3;
0054
0055
0056 else if (div < 20 || (div < 32 && (div & 1)))
0057 req->p = 2;
0058
0059
0060
0061 else if (div < 40 || (div < 64 && (div & 2)))
0062 req->p = 1;
0063
0064
0065 else
0066 req->p = 0;
0067
0068
0069 div <<= req->p;
0070 div /= (req->k + 1);
0071 req->n = div / 4;
0072 }
0073
0074
0075
0076
0077
0078
0079
0080 static void sun6i_a31_get_pll1_factors(struct factors_request *req)
0081 {
0082
0083
0084
0085
0086 u32 freq_mhz = req->rate / 1000000;
0087 u32 parent_freq_mhz = req->parent_rate / 1000000;
0088
0089
0090
0091
0092
0093 u32 round_freq_6 = rounddown(freq_mhz, 6);
0094 u32 round_freq_16 = round_down(freq_mhz, 16);
0095
0096 if (round_freq_6 > round_freq_16)
0097 freq_mhz = round_freq_6;
0098 else
0099 freq_mhz = round_freq_16;
0100
0101 req->rate = freq_mhz * 1000000;
0102
0103
0104 if (!(freq_mhz % 32))
0105 req->k = 3;
0106
0107 else if (!(freq_mhz % 9))
0108 req->k = 2;
0109
0110 else if (!(freq_mhz % 8))
0111 req->k = 1;
0112
0113 else
0114 req->k = 0;
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124 if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
0125 req->m = 2;
0126
0127
0128
0129
0130 else if ((freq_mhz / 6) & 1)
0131 req->m = 3;
0132
0133 else
0134 req->m = 1;
0135
0136
0137 req->n = freq_mhz * (req->m + 1) / ((req->k + 1) * parent_freq_mhz)
0138 - 1;
0139
0140
0141
0142
0143
0144 if ((req->n + 1) > 31 && (req->m + 1) > 1) {
0145 req->n = (req->n + 1) / 2 - 1;
0146 req->m = (req->m + 1) / 2 - 1;
0147 }
0148 }
0149
0150
0151
0152
0153
0154
0155
0156
0157 static void sun8i_a23_get_pll1_factors(struct factors_request *req)
0158 {
0159 u8 div;
0160
0161
0162 div = req->rate / 6000000;
0163 req->rate = 6000000 * div;
0164
0165
0166 req->m = 0;
0167
0168
0169 if (req->rate >= 768000000 || req->rate == 42000000 ||
0170 req->rate == 54000000)
0171 req->k = 1;
0172 else
0173 req->k = 0;
0174
0175
0176 if (div < 20 || (div < 32 && (div & 1)))
0177 req->p = 2;
0178
0179
0180
0181 else if (div < 40 || (div < 64 && (div & 2)))
0182 req->p = 1;
0183
0184
0185 else
0186 req->p = 0;
0187
0188
0189 div <<= req->p;
0190 div /= (req->k + 1);
0191 req->n = div / 4 - 1;
0192 }
0193
0194
0195
0196
0197
0198
0199
0200
0201 static void sun4i_get_pll5_factors(struct factors_request *req)
0202 {
0203 u8 div;
0204
0205
0206 div = req->rate / req->parent_rate;
0207 req->rate = req->parent_rate * div;
0208
0209 if (div < 31)
0210 req->k = 0;
0211 else if (div / 2 < 31)
0212 req->k = 1;
0213 else if (div / 3 < 31)
0214 req->k = 2;
0215 else
0216 req->k = 3;
0217
0218 req->n = DIV_ROUND_UP(div, (req->k + 1));
0219 }
0220
0221
0222
0223
0224
0225
0226
0227
0228 static void sun6i_a31_get_pll6_factors(struct factors_request *req)
0229 {
0230 u8 div;
0231
0232
0233 div = req->rate / req->parent_rate;
0234 req->rate = req->parent_rate * div;
0235
0236 req->k = div / 32;
0237 if (req->k > 3)
0238 req->k = 3;
0239
0240 req->n = DIV_ROUND_UP(div, (req->k + 1)) - 1;
0241 }
0242
0243
0244
0245
0246
0247
0248
0249 static void sun5i_a13_get_ahb_factors(struct factors_request *req)
0250 {
0251 u32 div;
0252
0253
0254 if (req->parent_rate < req->rate)
0255 req->rate = req->parent_rate;
0256
0257
0258
0259
0260
0261 if (req->rate < 8000)
0262 req->rate = 8000;
0263 if (req->rate > 300000000)
0264 req->rate = 300000000;
0265
0266 div = order_base_2(DIV_ROUND_UP(req->parent_rate, req->rate));
0267
0268
0269 if (div > 3)
0270 div = 3;
0271
0272 req->rate = req->parent_rate >> div;
0273
0274 req->p = div;
0275 }
0276
0277 #define SUN6I_AHB1_PARENT_PLL6 3
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288 static void sun6i_get_ahb1_factors(struct factors_request *req)
0289 {
0290 u8 div, calcp, calcm = 1;
0291
0292
0293
0294
0295
0296 if (req->parent_rate && req->rate > req->parent_rate)
0297 req->rate = req->parent_rate;
0298
0299 div = DIV_ROUND_UP(req->parent_rate, req->rate);
0300
0301
0302 if (req->parent_index == SUN6I_AHB1_PARENT_PLL6) {
0303 if (div < 4)
0304 calcp = 0;
0305 else if (div / 2 < 4)
0306 calcp = 1;
0307 else if (div / 4 < 4)
0308 calcp = 2;
0309 else
0310 calcp = 3;
0311
0312 calcm = DIV_ROUND_UP(div, 1 << calcp);
0313 } else {
0314 calcp = __roundup_pow_of_two(div);
0315 calcp = calcp > 3 ? 3 : calcp;
0316 }
0317
0318 req->rate = (req->parent_rate / calcm) >> calcp;
0319 req->p = calcp;
0320 req->m = calcm - 1;
0321 }
0322
0323
0324
0325
0326
0327 static void sun6i_ahb1_recalc(struct factors_request *req)
0328 {
0329 req->rate = req->parent_rate;
0330
0331
0332 if (req->parent_index == SUN6I_AHB1_PARENT_PLL6)
0333 req->rate /= req->m + 1;
0334
0335
0336 req->rate >>= req->p;
0337 }
0338
0339
0340
0341
0342
0343
0344
0345 static void sun4i_get_apb1_factors(struct factors_request *req)
0346 {
0347 u8 calcm, calcp;
0348 int div;
0349
0350 if (req->parent_rate < req->rate)
0351 req->rate = req->parent_rate;
0352
0353 div = DIV_ROUND_UP(req->parent_rate, req->rate);
0354
0355
0356 if (div > 32)
0357 return;
0358
0359 if (div <= 4)
0360 calcp = 0;
0361 else if (div <= 8)
0362 calcp = 1;
0363 else if (div <= 16)
0364 calcp = 2;
0365 else
0366 calcp = 3;
0367
0368 calcm = (div >> calcp) - 1;
0369
0370 req->rate = (req->parent_rate >> calcp) / (calcm + 1);
0371 req->m = calcm;
0372 req->p = calcp;
0373 }
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384 static void sun7i_a20_get_out_factors(struct factors_request *req)
0385 {
0386 u8 div, calcm, calcp;
0387
0388
0389
0390 if (req->rate > req->parent_rate)
0391 req->rate = req->parent_rate;
0392
0393 div = DIV_ROUND_UP(req->parent_rate, req->rate);
0394
0395 if (div < 32)
0396 calcp = 0;
0397 else if (div / 2 < 32)
0398 calcp = 1;
0399 else if (div / 4 < 32)
0400 calcp = 2;
0401 else
0402 calcp = 3;
0403
0404 calcm = DIV_ROUND_UP(div, 1 << calcp);
0405
0406 req->rate = (req->parent_rate >> calcp) / calcm;
0407 req->m = calcm - 1;
0408 req->p = calcp;
0409 }
0410
0411
0412
0413
0414
0415 static const struct clk_factors_config sun4i_pll1_config = {
0416 .nshift = 8,
0417 .nwidth = 5,
0418 .kshift = 4,
0419 .kwidth = 2,
0420 .mshift = 0,
0421 .mwidth = 2,
0422 .pshift = 16,
0423 .pwidth = 2,
0424 };
0425
0426 static const struct clk_factors_config sun6i_a31_pll1_config = {
0427 .nshift = 8,
0428 .nwidth = 5,
0429 .kshift = 4,
0430 .kwidth = 2,
0431 .mshift = 0,
0432 .mwidth = 2,
0433 .n_start = 1,
0434 };
0435
0436 static const struct clk_factors_config sun8i_a23_pll1_config = {
0437 .nshift = 8,
0438 .nwidth = 5,
0439 .kshift = 4,
0440 .kwidth = 2,
0441 .mshift = 0,
0442 .mwidth = 2,
0443 .pshift = 16,
0444 .pwidth = 2,
0445 .n_start = 1,
0446 };
0447
0448 static const struct clk_factors_config sun4i_pll5_config = {
0449 .nshift = 8,
0450 .nwidth = 5,
0451 .kshift = 4,
0452 .kwidth = 2,
0453 };
0454
0455 static const struct clk_factors_config sun6i_a31_pll6_config = {
0456 .nshift = 8,
0457 .nwidth = 5,
0458 .kshift = 4,
0459 .kwidth = 2,
0460 .n_start = 1,
0461 };
0462
0463 static const struct clk_factors_config sun5i_a13_ahb_config = {
0464 .pshift = 4,
0465 .pwidth = 2,
0466 };
0467
0468 static const struct clk_factors_config sun6i_ahb1_config = {
0469 .mshift = 6,
0470 .mwidth = 2,
0471 .pshift = 4,
0472 .pwidth = 2,
0473 };
0474
0475 static const struct clk_factors_config sun4i_apb1_config = {
0476 .mshift = 0,
0477 .mwidth = 5,
0478 .pshift = 16,
0479 .pwidth = 2,
0480 };
0481
0482
0483 static const struct clk_factors_config sun7i_a20_out_config = {
0484 .mshift = 8,
0485 .mwidth = 5,
0486 .pshift = 20,
0487 .pwidth = 2,
0488 };
0489
0490 static const struct factors_data sun4i_pll1_data __initconst = {
0491 .enable = 31,
0492 .table = &sun4i_pll1_config,
0493 .getter = sun4i_get_pll1_factors,
0494 };
0495
0496 static const struct factors_data sun6i_a31_pll1_data __initconst = {
0497 .enable = 31,
0498 .table = &sun6i_a31_pll1_config,
0499 .getter = sun6i_a31_get_pll1_factors,
0500 };
0501
0502 static const struct factors_data sun8i_a23_pll1_data __initconst = {
0503 .enable = 31,
0504 .table = &sun8i_a23_pll1_config,
0505 .getter = sun8i_a23_get_pll1_factors,
0506 };
0507
0508 static const struct factors_data sun7i_a20_pll4_data __initconst = {
0509 .enable = 31,
0510 .table = &sun4i_pll5_config,
0511 .getter = sun4i_get_pll5_factors,
0512 };
0513
0514 static const struct factors_data sun4i_pll5_data __initconst = {
0515 .enable = 31,
0516 .table = &sun4i_pll5_config,
0517 .getter = sun4i_get_pll5_factors,
0518 };
0519
0520 static const struct factors_data sun6i_a31_pll6_data __initconst = {
0521 .enable = 31,
0522 .table = &sun6i_a31_pll6_config,
0523 .getter = sun6i_a31_get_pll6_factors,
0524 };
0525
0526 static const struct factors_data sun5i_a13_ahb_data __initconst = {
0527 .mux = 6,
0528 .muxmask = BIT(1) | BIT(0),
0529 .table = &sun5i_a13_ahb_config,
0530 .getter = sun5i_a13_get_ahb_factors,
0531 };
0532
0533 static const struct factors_data sun6i_ahb1_data __initconst = {
0534 .mux = 12,
0535 .muxmask = BIT(1) | BIT(0),
0536 .table = &sun6i_ahb1_config,
0537 .getter = sun6i_get_ahb1_factors,
0538 .recalc = sun6i_ahb1_recalc,
0539 };
0540
0541 static const struct factors_data sun4i_apb1_data __initconst = {
0542 .mux = 24,
0543 .muxmask = BIT(1) | BIT(0),
0544 .table = &sun4i_apb1_config,
0545 .getter = sun4i_get_apb1_factors,
0546 };
0547
0548 static const struct factors_data sun7i_a20_out_data __initconst = {
0549 .enable = 31,
0550 .mux = 24,
0551 .muxmask = BIT(1) | BIT(0),
0552 .table = &sun7i_a20_out_config,
0553 .getter = sun7i_a20_get_out_factors,
0554 };
0555
0556 static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
0557 const struct factors_data *data)
0558 {
0559 void __iomem *reg;
0560
0561 reg = of_iomap(node, 0);
0562 if (!reg) {
0563 pr_err("Could not get registers for factors-clk: %pOFn\n",
0564 node);
0565 return NULL;
0566 }
0567
0568 return sunxi_factors_register(node, data, &clk_lock, reg);
0569 }
0570
0571 static void __init sun4i_pll1_clk_setup(struct device_node *node)
0572 {
0573 sunxi_factors_clk_setup(node, &sun4i_pll1_data);
0574 }
0575 CLK_OF_DECLARE(sun4i_pll1, "allwinner,sun4i-a10-pll1-clk",
0576 sun4i_pll1_clk_setup);
0577
0578 static void __init sun6i_pll1_clk_setup(struct device_node *node)
0579 {
0580 sunxi_factors_clk_setup(node, &sun6i_a31_pll1_data);
0581 }
0582 CLK_OF_DECLARE(sun6i_pll1, "allwinner,sun6i-a31-pll1-clk",
0583 sun6i_pll1_clk_setup);
0584
0585 static void __init sun8i_pll1_clk_setup(struct device_node *node)
0586 {
0587 sunxi_factors_clk_setup(node, &sun8i_a23_pll1_data);
0588 }
0589 CLK_OF_DECLARE(sun8i_pll1, "allwinner,sun8i-a23-pll1-clk",
0590 sun8i_pll1_clk_setup);
0591
0592 static void __init sun7i_pll4_clk_setup(struct device_node *node)
0593 {
0594 sunxi_factors_clk_setup(node, &sun7i_a20_pll4_data);
0595 }
0596 CLK_OF_DECLARE(sun7i_pll4, "allwinner,sun7i-a20-pll4-clk",
0597 sun7i_pll4_clk_setup);
0598
0599 static void __init sun5i_ahb_clk_setup(struct device_node *node)
0600 {
0601 sunxi_factors_clk_setup(node, &sun5i_a13_ahb_data);
0602 }
0603 CLK_OF_DECLARE(sun5i_ahb, "allwinner,sun5i-a13-ahb-clk",
0604 sun5i_ahb_clk_setup);
0605
0606 static void __init sun6i_ahb1_clk_setup(struct device_node *node)
0607 {
0608 sunxi_factors_clk_setup(node, &sun6i_ahb1_data);
0609 }
0610 CLK_OF_DECLARE(sun6i_a31_ahb1, "allwinner,sun6i-a31-ahb1-clk",
0611 sun6i_ahb1_clk_setup);
0612
0613 static void __init sun4i_apb1_clk_setup(struct device_node *node)
0614 {
0615 sunxi_factors_clk_setup(node, &sun4i_apb1_data);
0616 }
0617 CLK_OF_DECLARE(sun4i_apb1, "allwinner,sun4i-a10-apb1-clk",
0618 sun4i_apb1_clk_setup);
0619
0620 static void __init sun7i_out_clk_setup(struct device_node *node)
0621 {
0622 sunxi_factors_clk_setup(node, &sun7i_a20_out_data);
0623 }
0624 CLK_OF_DECLARE(sun7i_out, "allwinner,sun7i-a20-out-clk",
0625 sun7i_out_clk_setup);
0626
0627
0628
0629
0630
0631
0632 #define SUNXI_MUX_GATE_WIDTH 2
0633
0634 struct mux_data {
0635 u8 shift;
0636 };
0637
0638 static const struct mux_data sun4i_cpu_mux_data __initconst = {
0639 .shift = 16,
0640 };
0641
0642 static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
0643 .shift = 12,
0644 };
0645
0646 static const struct mux_data sun8i_h3_ahb2_mux_data __initconst = {
0647 .shift = 0,
0648 };
0649
0650 static struct clk * __init sunxi_mux_clk_setup(struct device_node *node,
0651 const struct mux_data *data,
0652 unsigned long flags)
0653 {
0654 struct clk *clk;
0655 const char *clk_name = node->name;
0656 const char *parents[SUNXI_MAX_PARENTS];
0657 void __iomem *reg;
0658 int i;
0659
0660 reg = of_iomap(node, 0);
0661 if (!reg) {
0662 pr_err("Could not map registers for mux-clk: %pOF\n", node);
0663 return NULL;
0664 }
0665
0666 i = of_clk_parent_fill(node, parents, SUNXI_MAX_PARENTS);
0667 if (of_property_read_string(node, "clock-output-names", &clk_name)) {
0668 pr_err("%s: could not read clock-output-names from \"%pOF\"\n",
0669 __func__, node);
0670 goto out_unmap;
0671 }
0672
0673 clk = clk_register_mux(NULL, clk_name, parents, i,
0674 CLK_SET_RATE_PARENT | flags, reg,
0675 data->shift, SUNXI_MUX_GATE_WIDTH,
0676 0, &clk_lock);
0677
0678 if (IS_ERR(clk)) {
0679 pr_err("%s: failed to register mux clock %s: %ld\n", __func__,
0680 clk_name, PTR_ERR(clk));
0681 goto out_unmap;
0682 }
0683
0684 if (of_clk_add_provider(node, of_clk_src_simple_get, clk)) {
0685 pr_err("%s: failed to add clock provider for %s\n",
0686 __func__, clk_name);
0687 clk_unregister_divider(clk);
0688 goto out_unmap;
0689 }
0690
0691 return clk;
0692 out_unmap:
0693 iounmap(reg);
0694 return NULL;
0695 }
0696
0697 static void __init sun4i_cpu_clk_setup(struct device_node *node)
0698 {
0699
0700 sunxi_mux_clk_setup(node, &sun4i_cpu_mux_data, CLK_IS_CRITICAL);
0701 }
0702 CLK_OF_DECLARE(sun4i_cpu, "allwinner,sun4i-a10-cpu-clk",
0703 sun4i_cpu_clk_setup);
0704
0705 static void __init sun6i_ahb1_mux_clk_setup(struct device_node *node)
0706 {
0707 sunxi_mux_clk_setup(node, &sun6i_a31_ahb1_mux_data, 0);
0708 }
0709 CLK_OF_DECLARE(sun6i_ahb1_mux, "allwinner,sun6i-a31-ahb1-mux-clk",
0710 sun6i_ahb1_mux_clk_setup);
0711
0712 static void __init sun8i_ahb2_clk_setup(struct device_node *node)
0713 {
0714 sunxi_mux_clk_setup(node, &sun8i_h3_ahb2_mux_data, 0);
0715 }
0716 CLK_OF_DECLARE(sun8i_ahb2, "allwinner,sun8i-h3-ahb2-clk",
0717 sun8i_ahb2_clk_setup);
0718
0719
0720
0721
0722
0723
0724 struct div_data {
0725 u8 shift;
0726 u8 pow;
0727 u8 width;
0728 const struct clk_div_table *table;
0729 };
0730
0731 static const struct div_data sun4i_axi_data __initconst = {
0732 .shift = 0,
0733 .pow = 0,
0734 .width = 2,
0735 };
0736
0737 static const struct clk_div_table sun8i_a23_axi_table[] __initconst = {
0738 { .val = 0, .div = 1 },
0739 { .val = 1, .div = 2 },
0740 { .val = 2, .div = 3 },
0741 { .val = 3, .div = 4 },
0742 { .val = 4, .div = 4 },
0743 { .val = 5, .div = 4 },
0744 { .val = 6, .div = 4 },
0745 { .val = 7, .div = 4 },
0746 { }
0747 };
0748
0749 static const struct div_data sun8i_a23_axi_data __initconst = {
0750 .width = 3,
0751 .table = sun8i_a23_axi_table,
0752 };
0753
0754 static const struct div_data sun4i_ahb_data __initconst = {
0755 .shift = 4,
0756 .pow = 1,
0757 .width = 2,
0758 };
0759
0760 static const struct clk_div_table sun4i_apb0_table[] __initconst = {
0761 { .val = 0, .div = 2 },
0762 { .val = 1, .div = 2 },
0763 { .val = 2, .div = 4 },
0764 { .val = 3, .div = 8 },
0765 { }
0766 };
0767
0768 static const struct div_data sun4i_apb0_data __initconst = {
0769 .shift = 8,
0770 .pow = 1,
0771 .width = 2,
0772 .table = sun4i_apb0_table,
0773 };
0774
0775 static void __init sunxi_divider_clk_setup(struct device_node *node,
0776 const struct div_data *data)
0777 {
0778 struct clk *clk;
0779 const char *clk_name = node->name;
0780 const char *clk_parent;
0781 void __iomem *reg;
0782
0783 reg = of_iomap(node, 0);
0784 if (!reg) {
0785 pr_err("Could not map registers for mux-clk: %pOF\n", node);
0786 return;
0787 }
0788
0789 clk_parent = of_clk_get_parent_name(node, 0);
0790
0791 if (of_property_read_string(node, "clock-output-names", &clk_name)) {
0792 pr_err("%s: could not read clock-output-names from \"%pOF\"\n",
0793 __func__, node);
0794 goto out_unmap;
0795 }
0796
0797 clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0,
0798 reg, data->shift, data->width,
0799 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
0800 data->table, &clk_lock);
0801 if (IS_ERR(clk)) {
0802 pr_err("%s: failed to register divider clock %s: %ld\n",
0803 __func__, clk_name, PTR_ERR(clk));
0804 goto out_unmap;
0805 }
0806
0807 if (of_clk_add_provider(node, of_clk_src_simple_get, clk)) {
0808 pr_err("%s: failed to add clock provider for %s\n",
0809 __func__, clk_name);
0810 goto out_unregister;
0811 }
0812
0813 if (clk_register_clkdev(clk, clk_name, NULL)) {
0814 of_clk_del_provider(node);
0815 goto out_unregister;
0816 }
0817
0818 return;
0819 out_unregister:
0820 clk_unregister_divider(clk);
0821
0822 out_unmap:
0823 iounmap(reg);
0824 }
0825
0826 static void __init sun4i_ahb_clk_setup(struct device_node *node)
0827 {
0828 sunxi_divider_clk_setup(node, &sun4i_ahb_data);
0829 }
0830 CLK_OF_DECLARE(sun4i_ahb, "allwinner,sun4i-a10-ahb-clk",
0831 sun4i_ahb_clk_setup);
0832
0833 static void __init sun4i_apb0_clk_setup(struct device_node *node)
0834 {
0835 sunxi_divider_clk_setup(node, &sun4i_apb0_data);
0836 }
0837 CLK_OF_DECLARE(sun4i_apb0, "allwinner,sun4i-a10-apb0-clk",
0838 sun4i_apb0_clk_setup);
0839
0840 static void __init sun4i_axi_clk_setup(struct device_node *node)
0841 {
0842 sunxi_divider_clk_setup(node, &sun4i_axi_data);
0843 }
0844 CLK_OF_DECLARE(sun4i_axi, "allwinner,sun4i-a10-axi-clk",
0845 sun4i_axi_clk_setup);
0846
0847 static void __init sun8i_axi_clk_setup(struct device_node *node)
0848 {
0849 sunxi_divider_clk_setup(node, &sun8i_a23_axi_data);
0850 }
0851 CLK_OF_DECLARE(sun8i_axi, "allwinner,sun8i-a23-axi-clk",
0852 sun8i_axi_clk_setup);
0853
0854
0855
0856
0857
0858
0859
0860 #define SUNXI_GATES_MAX_SIZE 64
0861
0862 struct gates_data {
0863 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
0864 };
0865
0866
0867
0868
0869
0870 #define SUNXI_DIVS_MAX_QTY 4
0871 #define SUNXI_DIVISOR_WIDTH 2
0872
0873 struct divs_data {
0874 const struct factors_data *factors;
0875 int ndivs;
0876
0877
0878
0879
0880
0881
0882 struct {
0883 u8 self;
0884 u8 fixed;
0885 struct clk_div_table *table;
0886 u8 shift;
0887 u8 pow;
0888 u8 gate;
0889 bool critical;
0890 } div[SUNXI_DIVS_MAX_QTY];
0891 };
0892
0893 static struct clk_div_table pll6_sata_tbl[] = {
0894 { .val = 0, .div = 6, },
0895 { .val = 1, .div = 12, },
0896 { .val = 2, .div = 18, },
0897 { .val = 3, .div = 24, },
0898 { }
0899 };
0900
0901 static const struct divs_data pll5_divs_data __initconst = {
0902 .factors = &sun4i_pll5_data,
0903 .ndivs = 2,
0904 .div = {
0905
0906 { .shift = 0, .pow = 0, .critical = true },
0907 { .shift = 16, .pow = 1, },
0908
0909 }
0910 };
0911
0912 static const struct divs_data pll6_divs_data __initconst = {
0913 .factors = &sun4i_pll5_data,
0914 .ndivs = 4,
0915 .div = {
0916 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 },
0917 { .fixed = 2 },
0918 { .self = 1 },
0919 { .fixed = 4 },
0920 }
0921 };
0922
0923 static const struct divs_data sun6i_a31_pll6_divs_data __initconst = {
0924 .factors = &sun6i_a31_pll6_data,
0925 .ndivs = 2,
0926 .div = {
0927 { .fixed = 2 },
0928 { .self = 1 },
0929 }
0930 };
0931
0932
0933
0934
0935
0936
0937
0938
0939
0940
0941
0942
0943 static struct clk ** __init sunxi_divs_clk_setup(struct device_node *node,
0944 const struct divs_data *data)
0945 {
0946 struct clk_onecell_data *clk_data;
0947 const char *parent;
0948 const char *clk_name;
0949 struct clk **clks, *pclk;
0950 struct clk_hw *gate_hw, *rate_hw;
0951 const struct clk_ops *rate_ops;
0952 struct clk_gate *gate = NULL;
0953 struct clk_fixed_factor *fix_factor;
0954 struct clk_divider *divider;
0955 struct factors_data factors = *data->factors;
0956 char *derived_name = NULL;
0957 void __iomem *reg;
0958 int ndivs = SUNXI_DIVS_MAX_QTY, i = 0;
0959 int flags, clkflags;
0960
0961
0962 if (data->ndivs)
0963 ndivs = data->ndivs;
0964
0965
0966 for (i = 0; i < ndivs; i++) {
0967 if (data->div[i].self) {
0968 of_property_read_string_index(node, "clock-output-names",
0969 i, &factors.name);
0970 break;
0971 }
0972 }
0973
0974 if (factors.name == NULL) {
0975 char *endp;
0976
0977 of_property_read_string_index(node, "clock-output-names",
0978 0, &clk_name);
0979 endp = strchr(clk_name, '_');
0980 if (endp) {
0981 derived_name = kstrndup(clk_name, endp - clk_name,
0982 GFP_KERNEL);
0983 if (!derived_name)
0984 return NULL;
0985 factors.name = derived_name;
0986 } else {
0987 factors.name = clk_name;
0988 }
0989 }
0990
0991
0992 pclk = sunxi_factors_clk_setup(node, &factors);
0993 if (!pclk)
0994 return NULL;
0995
0996 parent = __clk_get_name(pclk);
0997 kfree(derived_name);
0998
0999 reg = of_iomap(node, 0);
1000 if (!reg) {
1001 pr_err("Could not map registers for divs-clk: %pOF\n", node);
1002 return NULL;
1003 }
1004
1005 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
1006 if (!clk_data)
1007 goto out_unmap;
1008
1009 clks = kcalloc(ndivs, sizeof(*clks), GFP_KERNEL);
1010 if (!clks)
1011 goto free_clkdata;
1012
1013 clk_data->clks = clks;
1014
1015
1016
1017 clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
1018
1019 for (i = 0; i < ndivs; i++) {
1020 if (of_property_read_string_index(node, "clock-output-names",
1021 i, &clk_name) != 0)
1022 break;
1023
1024
1025 if (data->div[i].self) {
1026 clk_data->clks[i] = pclk;
1027 continue;
1028 }
1029
1030 gate_hw = NULL;
1031 rate_hw = NULL;
1032 rate_ops = NULL;
1033
1034
1035 if (data->div[i].gate) {
1036 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1037 if (!gate)
1038 goto free_clks;
1039
1040 gate->reg = reg;
1041 gate->bit_idx = data->div[i].gate;
1042 gate->lock = &clk_lock;
1043
1044 gate_hw = &gate->hw;
1045 }
1046
1047
1048 if (data->div[i].fixed) {
1049 fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
1050 if (!fix_factor)
1051 goto free_gate;
1052
1053 fix_factor->mult = 1;
1054 fix_factor->div = data->div[i].fixed;
1055
1056 rate_hw = &fix_factor->hw;
1057 rate_ops = &clk_fixed_factor_ops;
1058 } else {
1059 divider = kzalloc(sizeof(*divider), GFP_KERNEL);
1060 if (!divider)
1061 goto free_gate;
1062
1063 flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
1064
1065 divider->reg = reg;
1066 divider->shift = data->div[i].shift;
1067 divider->width = SUNXI_DIVISOR_WIDTH;
1068 divider->flags = flags;
1069 divider->lock = &clk_lock;
1070 divider->table = data->div[i].table;
1071
1072 rate_hw = ÷r->hw;
1073 rate_ops = &clk_divider_ops;
1074 }
1075
1076
1077
1078 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
1079 NULL, NULL,
1080 rate_hw, rate_ops,
1081 gate_hw, &clk_gate_ops,
1082 clkflags |
1083 (data->div[i].critical ?
1084 CLK_IS_CRITICAL : 0));
1085
1086 WARN_ON(IS_ERR(clk_data->clks[i]));
1087 }
1088
1089
1090 clk_data->clk_num = i;
1091
1092 if (of_clk_add_provider(node, of_clk_src_onecell_get, clk_data)) {
1093 pr_err("%s: failed to add clock provider for %s\n",
1094 __func__, clk_name);
1095 goto free_gate;
1096 }
1097
1098 return clks;
1099 free_gate:
1100 kfree(gate);
1101 free_clks:
1102 kfree(clks);
1103 free_clkdata:
1104 kfree(clk_data);
1105 out_unmap:
1106 iounmap(reg);
1107 return NULL;
1108 }
1109
1110 static void __init sun4i_pll5_clk_setup(struct device_node *node)
1111 {
1112 sunxi_divs_clk_setup(node, &pll5_divs_data);
1113 }
1114 CLK_OF_DECLARE(sun4i_pll5, "allwinner,sun4i-a10-pll5-clk",
1115 sun4i_pll5_clk_setup);
1116
1117 static void __init sun4i_pll6_clk_setup(struct device_node *node)
1118 {
1119 sunxi_divs_clk_setup(node, &pll6_divs_data);
1120 }
1121 CLK_OF_DECLARE(sun4i_pll6, "allwinner,sun4i-a10-pll6-clk",
1122 sun4i_pll6_clk_setup);
1123
1124 static void __init sun6i_pll6_clk_setup(struct device_node *node)
1125 {
1126 sunxi_divs_clk_setup(node, &sun6i_a31_pll6_divs_data);
1127 }
1128 CLK_OF_DECLARE(sun6i_pll6, "allwinner,sun6i-a31-pll6-clk",
1129 sun6i_pll6_clk_setup);
1130
1131
1132
1133
1134
1135
1136 static void sun6i_display_factors(struct factors_request *req)
1137 {
1138 u8 m;
1139
1140 if (req->rate > req->parent_rate)
1141 req->rate = req->parent_rate;
1142
1143 m = DIV_ROUND_UP(req->parent_rate, req->rate);
1144
1145 req->rate = req->parent_rate / m;
1146 req->m = m - 1;
1147 }
1148
1149 static const struct clk_factors_config sun6i_display_config = {
1150 .mshift = 0,
1151 .mwidth = 4,
1152 };
1153
1154 static const struct factors_data sun6i_display_data __initconst = {
1155 .enable = 31,
1156 .mux = 24,
1157 .muxmask = BIT(2) | BIT(1) | BIT(0),
1158 .table = &sun6i_display_config,
1159 .getter = sun6i_display_factors,
1160 };
1161
1162 static void __init sun6i_display_setup(struct device_node *node)
1163 {
1164 sunxi_factors_clk_setup(node, &sun6i_display_data);
1165 }
1166 CLK_OF_DECLARE(sun6i_display, "allwinner,sun6i-a31-display-clk",
1167 sun6i_display_setup);