clk-r8a7778.c 3.4 KB

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