clk-r8a7778.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * r8a7778 Core CPG Clocks
  4. *
  5. * Copyright (C) 2014 Ulrich Hecht
  6. */
  7. #include <linux/clk-provider.h>
  8. #include <linux/clk/renesas.h>
  9. #include <linux/of_address.h>
  10. #include <linux/slab.h>
  11. #include <linux/soc/renesas/rcar-rst.h>
  12. struct r8a7778_cpg {
  13. struct clk_onecell_data data;
  14. spinlock_t lock;
  15. void __iomem *reg;
  16. };
  17. /* PLL multipliers per bits 11, 12, and 18 of MODEMR */
  18. static const struct {
  19. unsigned long plla_mult;
  20. unsigned long pllb_mult;
  21. } r8a7778_rates[] __initconst = {
  22. [0] = { 21, 21 },
  23. [1] = { 24, 24 },
  24. [2] = { 28, 28 },
  25. [3] = { 32, 32 },
  26. [5] = { 24, 21 },
  27. [6] = { 28, 21 },
  28. [7] = { 32, 24 },
  29. };
  30. /* Clock dividers per bits 1 and 2 of MODEMR */
  31. static const struct {
  32. const char *name;
  33. unsigned int div[4];
  34. } r8a7778_divs[6] __initconst = {
  35. { "b", { 12, 12, 16, 18 } },
  36. { "out", { 12, 12, 16, 18 } },
  37. { "p", { 16, 12, 16, 12 } },
  38. { "s", { 4, 3, 4, 3 } },
  39. { "s1", { 8, 6, 8, 6 } },
  40. };
  41. static u32 cpg_mode_rates __initdata;
  42. static u32 cpg_mode_divs __initdata;
  43. static struct clk * __init
  44. r8a7778_cpg_register_clock(struct device_node *np, struct r8a7778_cpg *cpg,
  45. const char *name)
  46. {
  47. if (!strcmp(name, "plla")) {
  48. return clk_register_fixed_factor(NULL, "plla",
  49. of_clk_get_parent_name(np, 0), 0,
  50. r8a7778_rates[cpg_mode_rates].plla_mult, 1);
  51. } else if (!strcmp(name, "pllb")) {
  52. return clk_register_fixed_factor(NULL, "pllb",
  53. of_clk_get_parent_name(np, 0), 0,
  54. r8a7778_rates[cpg_mode_rates].pllb_mult, 1);
  55. } else {
  56. unsigned int i;
  57. for (i = 0; i < ARRAY_SIZE(r8a7778_divs); i++) {
  58. if (!strcmp(name, r8a7778_divs[i].name)) {
  59. return clk_register_fixed_factor(NULL,
  60. r8a7778_divs[i].name,
  61. "plla", 0, 1,
  62. r8a7778_divs[i].div[cpg_mode_divs]);
  63. }
  64. }
  65. }
  66. return ERR_PTR(-EINVAL);
  67. }
  68. static void __init r8a7778_cpg_clocks_init(struct device_node *np)
  69. {
  70. struct r8a7778_cpg *cpg;
  71. struct clk **clks;
  72. unsigned int i;
  73. int num_clks;
  74. u32 mode;
  75. if (rcar_rst_read_mode_pins(&mode))
  76. return;
  77. BUG_ON(!(mode & BIT(19)));
  78. cpg_mode_rates = (!!(mode & BIT(18)) << 2) |
  79. (!!(mode & BIT(12)) << 1) |
  80. (!!(mode & BIT(11)));
  81. cpg_mode_divs = (!!(mode & BIT(2)) << 1) |
  82. (!!(mode & BIT(1)));
  83. num_clks = of_property_count_strings(np, "clock-output-names");
  84. if (num_clks < 0) {
  85. pr_err("%s: failed to count clocks\n", __func__);
  86. return;
  87. }
  88. cpg = kzalloc(sizeof(*cpg), GFP_KERNEL);
  89. clks = kcalloc(num_clks, sizeof(*clks), GFP_KERNEL);
  90. if (cpg == NULL || clks == NULL) {
  91. /* We're leaking memory on purpose, there's no point in cleaning
  92. * up as the system won't boot anyway.
  93. */
  94. return;
  95. }
  96. spin_lock_init(&cpg->lock);
  97. cpg->data.clks = clks;
  98. cpg->data.clk_num = num_clks;
  99. cpg->reg = of_iomap(np, 0);
  100. if (WARN_ON(cpg->reg == NULL))
  101. return;
  102. for (i = 0; i < num_clks; ++i) {
  103. const char *name;
  104. struct clk *clk;
  105. of_property_read_string_index(np, "clock-output-names", i,
  106. &name);
  107. clk = r8a7778_cpg_register_clock(np, cpg, name);
  108. if (IS_ERR(clk))
  109. pr_err("%s: failed to register %pOFn %s clock (%ld)\n",
  110. __func__, np, name, PTR_ERR(clk));
  111. else
  112. cpg->data.clks[i] = clk;
  113. }
  114. of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
  115. cpg_mstp_add_clk_domain(np);
  116. }
  117. CLK_OF_DECLARE(r8a7778_cpg_clks, "renesas,r8a7778-cpg-clocks",
  118. r8a7778_cpg_clocks_init);