clk-div6.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. #include "clk-div6.h"
  20. #define CPG_DIV6_CKSTP BIT(8)
  21. #define CPG_DIV6_DIV(d) ((d) & 0x3f)
  22. #define CPG_DIV6_DIV_MASK 0x3f
  23. /**
  24. * struct div6_clock - CPG 6 bit divider clock
  25. * @hw: handle between common and hardware-specific interfaces
  26. * @reg: IO-remapped register
  27. * @div: divisor value (1-64)
  28. */
  29. struct div6_clock {
  30. struct clk_hw hw;
  31. void __iomem *reg;
  32. unsigned int div;
  33. u32 src_shift;
  34. u32 src_width;
  35. u8 *parents;
  36. };
  37. #define to_div6_clock(_hw) container_of(_hw, struct div6_clock, hw)
  38. static int cpg_div6_clock_enable(struct clk_hw *hw)
  39. {
  40. struct div6_clock *clock = to_div6_clock(hw);
  41. u32 val;
  42. val = (clk_readl(clock->reg) & ~(CPG_DIV6_DIV_MASK | CPG_DIV6_CKSTP))
  43. | CPG_DIV6_DIV(clock->div - 1);
  44. clk_writel(val, clock->reg);
  45. return 0;
  46. }
  47. static void cpg_div6_clock_disable(struct clk_hw *hw)
  48. {
  49. struct div6_clock *clock = to_div6_clock(hw);
  50. u32 val;
  51. val = clk_readl(clock->reg);
  52. val |= CPG_DIV6_CKSTP;
  53. /*
  54. * DIV6 clocks require the divisor field to be non-zero when stopping
  55. * the clock. However, some clocks (e.g. ZB on sh73a0) fail to be
  56. * re-enabled later if the divisor field is changed when stopping the
  57. * clock
  58. */
  59. if (!(val & CPG_DIV6_DIV_MASK))
  60. val |= CPG_DIV6_DIV_MASK;
  61. clk_writel(val, clock->reg);
  62. }
  63. static int cpg_div6_clock_is_enabled(struct clk_hw *hw)
  64. {
  65. struct div6_clock *clock = to_div6_clock(hw);
  66. return !(clk_readl(clock->reg) & CPG_DIV6_CKSTP);
  67. }
  68. static unsigned long cpg_div6_clock_recalc_rate(struct clk_hw *hw,
  69. unsigned long parent_rate)
  70. {
  71. struct div6_clock *clock = to_div6_clock(hw);
  72. return parent_rate / clock->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. /**
  143. * cpg_div6_register - Register a DIV6 clock
  144. * @name: Name of the DIV6 clock
  145. * @num_parents: Number of parent clocks of the DIV6 clock (1, 4, or 8)
  146. * @parent_names: Array containing the names of the parent clocks
  147. * @reg: Mapped register used to control the DIV6 clock
  148. */
  149. struct clk * __init cpg_div6_register(const char *name,
  150. unsigned int num_parents,
  151. const char **parent_names,
  152. void __iomem *reg)
  153. {
  154. unsigned int valid_parents;
  155. struct clk_init_data init;
  156. struct div6_clock *clock;
  157. struct clk *clk;
  158. unsigned int i;
  159. clock = kzalloc(sizeof(*clock), GFP_KERNEL);
  160. if (!clock)
  161. return ERR_PTR(-ENOMEM);
  162. clock->parents = kmalloc_array(num_parents, sizeof(*clock->parents),
  163. GFP_KERNEL);
  164. if (!clock->parents) {
  165. clk = ERR_PTR(-ENOMEM);
  166. goto free_clock;
  167. }
  168. clock->reg = reg;
  169. /*
  170. * Read the divisor. Disabling the clock overwrites the divisor, so we
  171. * need to cache its value for the enable operation.
  172. */
  173. clock->div = (clk_readl(clock->reg) & CPG_DIV6_DIV_MASK) + 1;
  174. switch (num_parents) {
  175. case 1:
  176. /* fixed parent clock */
  177. clock->src_shift = clock->src_width = 0;
  178. break;
  179. case 4:
  180. /* clock with EXSRC bits 6-7 */
  181. clock->src_shift = 6;
  182. clock->src_width = 2;
  183. break;
  184. case 8:
  185. /* VCLK with EXSRC bits 12-14 */
  186. clock->src_shift = 12;
  187. clock->src_width = 3;
  188. break;
  189. default:
  190. pr_err("%s: invalid number of parents for DIV6 clock %s\n",
  191. __func__, name);
  192. clk = ERR_PTR(-EINVAL);
  193. goto free_parents;
  194. }
  195. /* Filter out invalid parents */
  196. for (i = 0, valid_parents = 0; i < num_parents; i++) {
  197. if (parent_names[i]) {
  198. parent_names[valid_parents] = parent_names[i];
  199. clock->parents[valid_parents] = i;
  200. valid_parents++;
  201. }
  202. }
  203. /* Register the clock. */
  204. init.name = name;
  205. init.ops = &cpg_div6_clock_ops;
  206. init.flags = CLK_IS_BASIC;
  207. init.parent_names = parent_names;
  208. init.num_parents = valid_parents;
  209. clock->hw.init = &init;
  210. clk = clk_register(NULL, &clock->hw);
  211. if (IS_ERR(clk))
  212. goto free_parents;
  213. return clk;
  214. free_parents:
  215. kfree(clock->parents);
  216. free_clock:
  217. kfree(clock);
  218. return clk;
  219. }
  220. static void __init cpg_div6_clock_init(struct device_node *np)
  221. {
  222. unsigned int num_parents;
  223. const char **parent_names;
  224. const char *clk_name = np->name;
  225. void __iomem *reg;
  226. struct clk *clk;
  227. unsigned int i;
  228. num_parents = of_clk_get_parent_count(np);
  229. if (num_parents < 1) {
  230. pr_err("%s: no parent found for %s DIV6 clock\n",
  231. __func__, np->name);
  232. return;
  233. }
  234. parent_names = kmalloc_array(num_parents, sizeof(*parent_names),
  235. GFP_KERNEL);
  236. if (!parent_names)
  237. return;
  238. reg = of_iomap(np, 0);
  239. if (reg == NULL) {
  240. pr_err("%s: failed to map %s DIV6 clock register\n",
  241. __func__, np->name);
  242. goto error;
  243. }
  244. /* Parse the DT properties. */
  245. of_property_read_string(np, "clock-output-names", &clk_name);
  246. for (i = 0; i < num_parents; i++)
  247. parent_names[i] = of_clk_get_parent_name(np, i);
  248. clk = cpg_div6_register(clk_name, num_parents, parent_names, reg);
  249. if (IS_ERR(clk)) {
  250. pr_err("%s: failed to register %s DIV6 clock (%ld)\n",
  251. __func__, np->name, PTR_ERR(clk));
  252. goto error;
  253. }
  254. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  255. kfree(parent_names);
  256. return;
  257. error:
  258. if (reg)
  259. iounmap(reg);
  260. kfree(parent_names);
  261. }
  262. CLK_OF_DECLARE(cpg_div6_clk, "renesas,cpg-div6-clock", cpg_div6_clock_init);