clk-div6.c 8.8 KB

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