clk-div6.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/init.h>
  14. #include <linux/io.h>
  15. #include <linux/kernel.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/slab.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. u32 src_shift;
  33. u32 src_width;
  34. u8 *parents;
  35. };
  36. #define to_div6_clock(_hw) container_of(_hw, struct div6_clock, hw)
  37. static int cpg_div6_clock_enable(struct clk_hw *hw)
  38. {
  39. struct div6_clock *clock = to_div6_clock(hw);
  40. u32 val;
  41. val = (clk_readl(clock->reg) & ~(CPG_DIV6_DIV_MASK | CPG_DIV6_CKSTP))
  42. | CPG_DIV6_DIV(clock->div - 1);
  43. clk_writel(val, clock->reg);
  44. return 0;
  45. }
  46. static void cpg_div6_clock_disable(struct clk_hw *hw)
  47. {
  48. struct div6_clock *clock = to_div6_clock(hw);
  49. u32 val;
  50. val = clk_readl(clock->reg);
  51. val |= CPG_DIV6_CKSTP;
  52. /*
  53. * DIV6 clocks require the divisor field to be non-zero when stopping
  54. * the clock. However, some clocks (e.g. ZB on sh73a0) fail to be
  55. * re-enabled later if the divisor field is changed when stopping the
  56. * clock
  57. */
  58. if (!(val & CPG_DIV6_DIV_MASK))
  59. val |= CPG_DIV6_DIV_MASK;
  60. clk_writel(val, clock->reg);
  61. }
  62. static int cpg_div6_clock_is_enabled(struct clk_hw *hw)
  63. {
  64. struct div6_clock *clock = to_div6_clock(hw);
  65. return !(clk_readl(clock->reg) & CPG_DIV6_CKSTP);
  66. }
  67. static unsigned long cpg_div6_clock_recalc_rate(struct clk_hw *hw,
  68. unsigned long parent_rate)
  69. {
  70. struct div6_clock *clock = to_div6_clock(hw);
  71. unsigned int div = (clk_readl(clock->reg) & CPG_DIV6_DIV_MASK) + 1;
  72. return parent_rate / div;
  73. }
  74. static unsigned int cpg_div6_clock_calc_div(unsigned long rate,
  75. unsigned long parent_rate)
  76. {
  77. unsigned int div;
  78. if (!rate)
  79. rate = 1;
  80. div = DIV_ROUND_CLOSEST(parent_rate, rate);
  81. return clamp_t(unsigned int, div, 1, 64);
  82. }
  83. static long cpg_div6_clock_round_rate(struct clk_hw *hw, unsigned long rate,
  84. unsigned long *parent_rate)
  85. {
  86. unsigned int div = cpg_div6_clock_calc_div(rate, *parent_rate);
  87. return *parent_rate / div;
  88. }
  89. static int cpg_div6_clock_set_rate(struct clk_hw *hw, unsigned long rate,
  90. unsigned long parent_rate)
  91. {
  92. struct div6_clock *clock = to_div6_clock(hw);
  93. unsigned int div = cpg_div6_clock_calc_div(rate, parent_rate);
  94. u32 val;
  95. clock->div = div;
  96. val = clk_readl(clock->reg) & ~CPG_DIV6_DIV_MASK;
  97. /* Only program the new divisor if the clock isn't stopped. */
  98. if (!(val & CPG_DIV6_CKSTP))
  99. clk_writel(val | CPG_DIV6_DIV(clock->div - 1), clock->reg);
  100. return 0;
  101. }
  102. static u8 cpg_div6_clock_get_parent(struct clk_hw *hw)
  103. {
  104. struct div6_clock *clock = to_div6_clock(hw);
  105. unsigned int i;
  106. u8 hw_index;
  107. if (clock->src_width == 0)
  108. return 0;
  109. hw_index = (clk_readl(clock->reg) >> clock->src_shift) &
  110. (BIT(clock->src_width) - 1);
  111. for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
  112. if (clock->parents[i] == hw_index)
  113. return i;
  114. }
  115. pr_err("%s: %s DIV6 clock set to invalid parent %u\n",
  116. __func__, clk_hw_get_name(hw), hw_index);
  117. return 0;
  118. }
  119. static int cpg_div6_clock_set_parent(struct clk_hw *hw, u8 index)
  120. {
  121. struct div6_clock *clock = to_div6_clock(hw);
  122. u8 hw_index;
  123. u32 mask;
  124. if (index >= clk_hw_get_num_parents(hw))
  125. return -EINVAL;
  126. mask = ~((BIT(clock->src_width) - 1) << clock->src_shift);
  127. hw_index = clock->parents[index];
  128. clk_writel((clk_readl(clock->reg) & mask) |
  129. (hw_index << clock->src_shift), clock->reg);
  130. return 0;
  131. }
  132. static const struct clk_ops cpg_div6_clock_ops = {
  133. .enable = cpg_div6_clock_enable,
  134. .disable = cpg_div6_clock_disable,
  135. .is_enabled = cpg_div6_clock_is_enabled,
  136. .get_parent = cpg_div6_clock_get_parent,
  137. .set_parent = cpg_div6_clock_set_parent,
  138. .recalc_rate = cpg_div6_clock_recalc_rate,
  139. .round_rate = cpg_div6_clock_round_rate,
  140. .set_rate = cpg_div6_clock_set_rate,
  141. };
  142. static void __init cpg_div6_clock_init(struct device_node *np)
  143. {
  144. unsigned int num_parents, valid_parents;
  145. const char **parent_names;
  146. struct clk_init_data init;
  147. struct div6_clock *clock;
  148. const char *name;
  149. struct clk *clk;
  150. unsigned int i;
  151. int ret;
  152. clock = kzalloc(sizeof(*clock), GFP_KERNEL);
  153. if (!clock)
  154. return;
  155. num_parents = of_clk_get_parent_count(np);
  156. if (num_parents < 1) {
  157. pr_err("%s: no parent found for %s DIV6 clock\n",
  158. __func__, np->name);
  159. return;
  160. }
  161. clock->parents = kmalloc_array(num_parents, sizeof(*clock->parents),
  162. GFP_KERNEL);
  163. parent_names = kmalloc_array(num_parents, sizeof(*parent_names),
  164. GFP_KERNEL);
  165. if (!parent_names)
  166. return;
  167. /* Remap the clock register and read the divisor. Disabling the
  168. * clock overwrites the divisor, so we need to cache its value for the
  169. * enable operation.
  170. */
  171. clock->reg = of_iomap(np, 0);
  172. if (clock->reg == NULL) {
  173. pr_err("%s: failed to map %s DIV6 clock register\n",
  174. __func__, np->name);
  175. goto error;
  176. }
  177. clock->div = (clk_readl(clock->reg) & CPG_DIV6_DIV_MASK) + 1;
  178. /* Parse the DT properties. */
  179. ret = of_property_read_string(np, "clock-output-names", &name);
  180. if (ret < 0) {
  181. pr_err("%s: failed to get %s DIV6 clock output name\n",
  182. __func__, np->name);
  183. goto error;
  184. }
  185. for (i = 0, valid_parents = 0; i < num_parents; i++) {
  186. const char *name = of_clk_get_parent_name(np, i);
  187. if (name) {
  188. parent_names[valid_parents] = name;
  189. clock->parents[valid_parents] = i;
  190. valid_parents++;
  191. }
  192. }
  193. switch (num_parents) {
  194. case 1:
  195. /* fixed parent clock */
  196. clock->src_shift = clock->src_width = 0;
  197. break;
  198. case 4:
  199. /* clock with EXSRC bits 6-7 */
  200. clock->src_shift = 6;
  201. clock->src_width = 2;
  202. break;
  203. case 8:
  204. /* VCLK with EXSRC bits 12-14 */
  205. clock->src_shift = 12;
  206. clock->src_width = 3;
  207. break;
  208. default:
  209. pr_err("%s: invalid number of parents for DIV6 clock %s\n",
  210. __func__, np->name);
  211. goto error;
  212. }
  213. /* Register the clock. */
  214. init.name = name;
  215. init.ops = &cpg_div6_clock_ops;
  216. init.flags = CLK_IS_BASIC;
  217. init.parent_names = parent_names;
  218. init.num_parents = valid_parents;
  219. clock->hw.init = &init;
  220. clk = clk_register(NULL, &clock->hw);
  221. if (IS_ERR(clk)) {
  222. pr_err("%s: failed to register %s DIV6 clock (%ld)\n",
  223. __func__, np->name, PTR_ERR(clk));
  224. goto error;
  225. }
  226. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  227. kfree(parent_names);
  228. return;
  229. error:
  230. if (clock->reg)
  231. iounmap(clock->reg);
  232. kfree(parent_names);
  233. kfree(clock);
  234. }
  235. CLK_OF_DECLARE(cpg_div6_clk, "renesas,cpg-div6-clock", cpg_div6_clock_init);