clk-r8a7778.c 3.0 KB

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