clk-r8a7778.c 3.4 KB

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