clk-div6.c 8.6 KB

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