clk-div6.c 9.1 KB

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