clk-div6.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * r8a7790 Common Clock Framework support
  3. *
  4. * Copyright (C) 2013 Renesas Solutions Corp.
  5. *
  6. * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. */
  12. #include <linux/clk-provider.h>
  13. #include <linux/clkdev.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #define CPG_DIV6_CKSTP BIT(8)
  20. #define CPG_DIV6_DIV(d) ((d) & 0x3f)
  21. #define CPG_DIV6_DIV_MASK 0x3f
  22. /**
  23. * struct div6_clock - CPG 6 bit divider clock
  24. * @hw: handle between common and hardware-specific interfaces
  25. * @reg: IO-remapped register
  26. * @div: divisor value (1-64)
  27. */
  28. struct div6_clock {
  29. struct clk_hw hw;
  30. void __iomem *reg;
  31. unsigned int div;
  32. };
  33. #define to_div6_clock(_hw) container_of(_hw, struct div6_clock, hw)
  34. static int cpg_div6_clock_enable(struct clk_hw *hw)
  35. {
  36. struct div6_clock *clock = to_div6_clock(hw);
  37. clk_writel(CPG_DIV6_DIV(clock->div - 1), clock->reg);
  38. return 0;
  39. }
  40. static void cpg_div6_clock_disable(struct clk_hw *hw)
  41. {
  42. struct div6_clock *clock = to_div6_clock(hw);
  43. /* DIV6 clocks require the divisor field to be non-zero when stopping
  44. * the clock.
  45. */
  46. clk_writel(CPG_DIV6_CKSTP | CPG_DIV6_DIV(CPG_DIV6_DIV_MASK),
  47. clock->reg);
  48. }
  49. static int cpg_div6_clock_is_enabled(struct clk_hw *hw)
  50. {
  51. struct div6_clock *clock = to_div6_clock(hw);
  52. return !(clk_readl(clock->reg) & CPG_DIV6_CKSTP);
  53. }
  54. static unsigned long cpg_div6_clock_recalc_rate(struct clk_hw *hw,
  55. unsigned long parent_rate)
  56. {
  57. struct div6_clock *clock = to_div6_clock(hw);
  58. unsigned int div = (clk_readl(clock->reg) & CPG_DIV6_DIV_MASK) + 1;
  59. return parent_rate / div;
  60. }
  61. static unsigned int cpg_div6_clock_calc_div(unsigned long rate,
  62. unsigned long parent_rate)
  63. {
  64. unsigned int div;
  65. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  66. return clamp_t(unsigned int, div, 1, 64);
  67. }
  68. static long cpg_div6_clock_round_rate(struct clk_hw *hw, unsigned long rate,
  69. unsigned long *parent_rate)
  70. {
  71. unsigned int div = cpg_div6_clock_calc_div(rate, *parent_rate);
  72. return *parent_rate / div;
  73. }
  74. static int cpg_div6_clock_set_rate(struct clk_hw *hw, unsigned long rate,
  75. unsigned long parent_rate)
  76. {
  77. struct div6_clock *clock = to_div6_clock(hw);
  78. unsigned int div = cpg_div6_clock_calc_div(rate, parent_rate);
  79. clock->div = div;
  80. /* Only program the new divisor if the clock isn't stopped. */
  81. if (!(clk_readl(clock->reg) & CPG_DIV6_CKSTP))
  82. clk_writel(CPG_DIV6_DIV(clock->div - 1), clock->reg);
  83. return 0;
  84. }
  85. static const struct clk_ops cpg_div6_clock_ops = {
  86. .enable = cpg_div6_clock_enable,
  87. .disable = cpg_div6_clock_disable,
  88. .is_enabled = cpg_div6_clock_is_enabled,
  89. .recalc_rate = cpg_div6_clock_recalc_rate,
  90. .round_rate = cpg_div6_clock_round_rate,
  91. .set_rate = cpg_div6_clock_set_rate,
  92. };
  93. static void __init cpg_div6_clock_init(struct device_node *np)
  94. {
  95. struct clk_init_data init;
  96. struct div6_clock *clock;
  97. const char *parent_name;
  98. const char *name;
  99. struct clk *clk;
  100. int ret;
  101. clock = kzalloc(sizeof(*clock), GFP_KERNEL);
  102. if (!clock) {
  103. pr_err("%s: failed to allocate %s DIV6 clock\n",
  104. __func__, np->name);
  105. return;
  106. }
  107. /* Remap the clock register and read the divisor. Disabling the
  108. * clock overwrites the divisor, so we need to cache its value for the
  109. * enable operation.
  110. */
  111. clock->reg = of_iomap(np, 0);
  112. if (clock->reg == NULL) {
  113. pr_err("%s: failed to map %s DIV6 clock register\n",
  114. __func__, np->name);
  115. goto error;
  116. }
  117. clock->div = (clk_readl(clock->reg) & CPG_DIV6_DIV_MASK) + 1;
  118. /* Parse the DT properties. */
  119. ret = of_property_read_string(np, "clock-output-names", &name);
  120. if (ret < 0) {
  121. pr_err("%s: failed to get %s DIV6 clock output name\n",
  122. __func__, np->name);
  123. goto error;
  124. }
  125. parent_name = of_clk_get_parent_name(np, 0);
  126. if (parent_name == NULL) {
  127. pr_err("%s: failed to get %s DIV6 clock parent name\n",
  128. __func__, np->name);
  129. goto error;
  130. }
  131. /* Register the clock. */
  132. init.name = name;
  133. init.ops = &cpg_div6_clock_ops;
  134. init.flags = CLK_IS_BASIC;
  135. init.parent_names = &parent_name;
  136. init.num_parents = 1;
  137. clock->hw.init = &init;
  138. clk = clk_register(NULL, &clock->hw);
  139. if (IS_ERR(clk)) {
  140. pr_err("%s: failed to register %s DIV6 clock (%ld)\n",
  141. __func__, np->name, PTR_ERR(clk));
  142. goto error;
  143. }
  144. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  145. return;
  146. error:
  147. if (clock->reg)
  148. iounmap(clock->reg);
  149. kfree(clock);
  150. }
  151. CLK_OF_DECLARE(cpg_div6_clk, "renesas,cpg-div6-clock", cpg_div6_clock_init);