clk-div6.c 7.8 KB

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