clk-audio-pll.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * Copyright (C) 2016 Atmel Corporation,
  3. * Songjun Wu <songjun.wu@atmel.com>,
  4. * Nicolas Ferre <nicolas.ferre@atmel.com>
  5. * Copyright (C) 2017 Free Electrons,
  6. * Quentin Schulz <quentin.schulz@free-electrons.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; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * The Sama5d2 SoC has two audio PLLs (PMC and PAD) that shares the same parent
  14. * (FRAC). FRAC can output between 620 and 700MHz and only multiply the rate of
  15. * its own parent. PMC and PAD can then divide the FRAC rate to best match the
  16. * asked rate.
  17. *
  18. * Traits of FRAC clock:
  19. * enable - clk_enable writes nd, fracr parameters and enables PLL
  20. * rate - rate is adjustable.
  21. * clk->rate = parent->rate * ((nd + 1) + (fracr / 2^22))
  22. * parent - fixed parent. No clk_set_parent support
  23. *
  24. * Traits of PMC clock:
  25. * enable - clk_enable writes qdpmc, and enables PMC output
  26. * rate - rate is adjustable.
  27. * clk->rate = parent->rate / (qdpmc + 1)
  28. * parent - fixed parent. No clk_set_parent support
  29. *
  30. * Traits of PAD clock:
  31. * enable - clk_enable writes divisors and enables PAD output
  32. * rate - rate is adjustable.
  33. * clk->rate = parent->rate / (qdaudio * div))
  34. * parent - fixed parent. No clk_set_parent support
  35. *
  36. */
  37. #include <linux/clk.h>
  38. #include <linux/clk-provider.h>
  39. #include <linux/clk/at91_pmc.h>
  40. #include <linux/of.h>
  41. #include <linux/mfd/syscon.h>
  42. #include <linux/regmap.h>
  43. #include <linux/slab.h>
  44. #define AUDIO_PLL_DIV_FRAC BIT(22)
  45. #define AUDIO_PLL_ND_MAX (AT91_PMC_AUDIO_PLL_ND_MASK >> \
  46. AT91_PMC_AUDIO_PLL_ND_OFFSET)
  47. #define AUDIO_PLL_QDPAD(qd, div) ((AT91_PMC_AUDIO_PLL_QDPAD_EXTDIV(qd) & \
  48. AT91_PMC_AUDIO_PLL_QDPAD_EXTDIV_MASK) | \
  49. (AT91_PMC_AUDIO_PLL_QDPAD_DIV(div) & \
  50. AT91_PMC_AUDIO_PLL_QDPAD_DIV_MASK))
  51. #define AUDIO_PLL_QDPMC_MAX (AT91_PMC_AUDIO_PLL_QDPMC_MASK >> \
  52. AT91_PMC_AUDIO_PLL_QDPMC_OFFSET)
  53. #define AUDIO_PLL_FOUT_MIN 620000000UL
  54. #define AUDIO_PLL_FOUT_MAX 700000000UL
  55. struct clk_audio_frac {
  56. struct clk_hw hw;
  57. struct regmap *regmap;
  58. u32 fracr;
  59. u8 nd;
  60. };
  61. struct clk_audio_pad {
  62. struct clk_hw hw;
  63. struct regmap *regmap;
  64. u8 qdaudio;
  65. u8 div;
  66. };
  67. struct clk_audio_pmc {
  68. struct clk_hw hw;
  69. struct regmap *regmap;
  70. u8 qdpmc;
  71. };
  72. #define to_clk_audio_frac(hw) container_of(hw, struct clk_audio_frac, hw)
  73. #define to_clk_audio_pad(hw) container_of(hw, struct clk_audio_pad, hw)
  74. #define to_clk_audio_pmc(hw) container_of(hw, struct clk_audio_pmc, hw)
  75. static int clk_audio_pll_frac_enable(struct clk_hw *hw)
  76. {
  77. struct clk_audio_frac *frac = to_clk_audio_frac(hw);
  78. regmap_update_bits(frac->regmap, AT91_PMC_AUDIO_PLL0,
  79. AT91_PMC_AUDIO_PLL_RESETN, 0);
  80. regmap_update_bits(frac->regmap, AT91_PMC_AUDIO_PLL0,
  81. AT91_PMC_AUDIO_PLL_RESETN,
  82. AT91_PMC_AUDIO_PLL_RESETN);
  83. regmap_update_bits(frac->regmap, AT91_PMC_AUDIO_PLL1,
  84. AT91_PMC_AUDIO_PLL_FRACR_MASK, frac->fracr);
  85. /*
  86. * reset and enable have to be done in 2 separated writes
  87. * for AT91_PMC_AUDIO_PLL0
  88. */
  89. regmap_update_bits(frac->regmap, AT91_PMC_AUDIO_PLL0,
  90. AT91_PMC_AUDIO_PLL_PLLEN |
  91. AT91_PMC_AUDIO_PLL_ND_MASK,
  92. AT91_PMC_AUDIO_PLL_PLLEN |
  93. AT91_PMC_AUDIO_PLL_ND(frac->nd));
  94. return 0;
  95. }
  96. static int clk_audio_pll_pad_enable(struct clk_hw *hw)
  97. {
  98. struct clk_audio_pad *apad_ck = to_clk_audio_pad(hw);
  99. regmap_update_bits(apad_ck->regmap, AT91_PMC_AUDIO_PLL1,
  100. AT91_PMC_AUDIO_PLL_QDPAD_MASK,
  101. AUDIO_PLL_QDPAD(apad_ck->qdaudio, apad_ck->div));
  102. regmap_update_bits(apad_ck->regmap, AT91_PMC_AUDIO_PLL0,
  103. AT91_PMC_AUDIO_PLL_PADEN, AT91_PMC_AUDIO_PLL_PADEN);
  104. return 0;
  105. }
  106. static int clk_audio_pll_pmc_enable(struct clk_hw *hw)
  107. {
  108. struct clk_audio_pmc *apmc_ck = to_clk_audio_pmc(hw);
  109. regmap_update_bits(apmc_ck->regmap, AT91_PMC_AUDIO_PLL0,
  110. AT91_PMC_AUDIO_PLL_PMCEN |
  111. AT91_PMC_AUDIO_PLL_QDPMC_MASK,
  112. AT91_PMC_AUDIO_PLL_PMCEN |
  113. AT91_PMC_AUDIO_PLL_QDPMC(apmc_ck->qdpmc));
  114. return 0;
  115. }
  116. static void clk_audio_pll_frac_disable(struct clk_hw *hw)
  117. {
  118. struct clk_audio_frac *frac = to_clk_audio_frac(hw);
  119. regmap_update_bits(frac->regmap, AT91_PMC_AUDIO_PLL0,
  120. AT91_PMC_AUDIO_PLL_PLLEN, 0);
  121. /* do it in 2 separated writes */
  122. regmap_update_bits(frac->regmap, AT91_PMC_AUDIO_PLL0,
  123. AT91_PMC_AUDIO_PLL_RESETN, 0);
  124. }
  125. static void clk_audio_pll_pad_disable(struct clk_hw *hw)
  126. {
  127. struct clk_audio_pad *apad_ck = to_clk_audio_pad(hw);
  128. regmap_update_bits(apad_ck->regmap, AT91_PMC_AUDIO_PLL0,
  129. AT91_PMC_AUDIO_PLL_PADEN, 0);
  130. }
  131. static void clk_audio_pll_pmc_disable(struct clk_hw *hw)
  132. {
  133. struct clk_audio_pmc *apmc_ck = to_clk_audio_pmc(hw);
  134. regmap_update_bits(apmc_ck->regmap, AT91_PMC_AUDIO_PLL0,
  135. AT91_PMC_AUDIO_PLL_PMCEN, 0);
  136. }
  137. static unsigned long clk_audio_pll_fout(unsigned long parent_rate,
  138. unsigned long nd, unsigned long fracr)
  139. {
  140. unsigned long long fr = (unsigned long long)parent_rate * fracr;
  141. pr_debug("A PLL: %s, fr = %llu\n", __func__, fr);
  142. fr = DIV_ROUND_CLOSEST_ULL(fr, AUDIO_PLL_DIV_FRAC);
  143. pr_debug("A PLL: %s, fr = %llu\n", __func__, fr);
  144. return parent_rate * (nd + 1) + fr;
  145. }
  146. static unsigned long clk_audio_pll_frac_recalc_rate(struct clk_hw *hw,
  147. unsigned long parent_rate)
  148. {
  149. struct clk_audio_frac *frac = to_clk_audio_frac(hw);
  150. unsigned long fout;
  151. fout = clk_audio_pll_fout(parent_rate, frac->nd, frac->fracr);
  152. pr_debug("A PLL: %s, fout = %lu (nd = %u, fracr = %lu)\n", __func__,
  153. fout, frac->nd, (unsigned long)frac->fracr);
  154. return fout;
  155. }
  156. static unsigned long clk_audio_pll_pad_recalc_rate(struct clk_hw *hw,
  157. unsigned long parent_rate)
  158. {
  159. struct clk_audio_pad *apad_ck = to_clk_audio_pad(hw);
  160. unsigned long apad_rate = 0;
  161. if (apad_ck->qdaudio && apad_ck->div)
  162. apad_rate = parent_rate / (apad_ck->qdaudio * apad_ck->div);
  163. pr_debug("A PLL/PAD: %s, apad_rate = %lu (div = %u, qdaudio = %u)\n",
  164. __func__, apad_rate, apad_ck->div, apad_ck->qdaudio);
  165. return apad_rate;
  166. }
  167. static unsigned long clk_audio_pll_pmc_recalc_rate(struct clk_hw *hw,
  168. unsigned long parent_rate)
  169. {
  170. struct clk_audio_pmc *apmc_ck = to_clk_audio_pmc(hw);
  171. unsigned long apmc_rate = 0;
  172. apmc_rate = parent_rate / (apmc_ck->qdpmc + 1);
  173. pr_debug("A PLL/PMC: %s, apmc_rate = %lu (qdpmc = %u)\n", __func__,
  174. apmc_rate, apmc_ck->qdpmc);
  175. return apmc_rate;
  176. }
  177. static int clk_audio_pll_frac_compute_frac(unsigned long rate,
  178. unsigned long parent_rate,
  179. unsigned long *nd,
  180. unsigned long *fracr)
  181. {
  182. unsigned long long tmp, rem;
  183. if (!rate)
  184. return -EINVAL;
  185. tmp = rate;
  186. rem = do_div(tmp, parent_rate);
  187. if (!tmp || tmp >= AUDIO_PLL_ND_MAX)
  188. return -EINVAL;
  189. *nd = tmp - 1;
  190. tmp = rem * AUDIO_PLL_DIV_FRAC;
  191. tmp = DIV_ROUND_CLOSEST_ULL(tmp, parent_rate);
  192. if (tmp > AT91_PMC_AUDIO_PLL_FRACR_MASK)
  193. return -EINVAL;
  194. /* we can cast here as we verified the bounds just above */
  195. *fracr = (unsigned long)tmp;
  196. return 0;
  197. }
  198. static int clk_audio_pll_frac_determine_rate(struct clk_hw *hw,
  199. struct clk_rate_request *req)
  200. {
  201. unsigned long fracr, nd;
  202. int ret;
  203. pr_debug("A PLL: %s, rate = %lu (parent_rate = %lu)\n", __func__,
  204. req->rate, req->best_parent_rate);
  205. req->rate = clamp(req->rate, AUDIO_PLL_FOUT_MIN, AUDIO_PLL_FOUT_MAX);
  206. req->min_rate = max(req->min_rate, AUDIO_PLL_FOUT_MIN);
  207. req->max_rate = min(req->max_rate, AUDIO_PLL_FOUT_MAX);
  208. ret = clk_audio_pll_frac_compute_frac(req->rate, req->best_parent_rate,
  209. &nd, &fracr);
  210. if (ret)
  211. return ret;
  212. req->rate = clk_audio_pll_fout(req->best_parent_rate, nd, fracr);
  213. req->best_parent_hw = clk_hw_get_parent(hw);
  214. pr_debug("A PLL: %s, best_rate = %lu (nd = %lu, fracr = %lu)\n",
  215. __func__, req->rate, nd, fracr);
  216. return 0;
  217. }
  218. static long clk_audio_pll_pad_round_rate(struct clk_hw *hw, unsigned long rate,
  219. unsigned long *parent_rate)
  220. {
  221. struct clk_hw *pclk = clk_hw_get_parent(hw);
  222. long best_rate = -EINVAL;
  223. unsigned long best_parent_rate;
  224. unsigned long tmp_qd;
  225. u32 div;
  226. long tmp_rate;
  227. int tmp_diff;
  228. int best_diff = -1;
  229. pr_debug("A PLL/PAD: %s, rate = %lu (parent_rate = %lu)\n", __func__,
  230. rate, *parent_rate);
  231. /*
  232. * Rate divisor is actually made of two different divisors, multiplied
  233. * between themselves before dividing the rate.
  234. * tmp_qd goes from 1 to 31 and div is either 2 or 3.
  235. * In order to avoid testing twice the rate divisor (e.g. divisor 12 can
  236. * be found with (tmp_qd, div) = (2, 6) or (3, 4)), we remove any loop
  237. * for a rate divisor when div is 2 and tmp_qd is a multiple of 3.
  238. * We cannot inverse it (condition div is 3 and tmp_qd is even) or we
  239. * would miss some rate divisor that aren't reachable with div being 2
  240. * (e.g. rate divisor 90 is made with div = 3 and tmp_qd = 30, thus
  241. * tmp_qd is even so we skip it because we think div 2 could make this
  242. * rate divisor which isn't possible since tmp_qd has to be <= 31).
  243. */
  244. for (tmp_qd = 1; tmp_qd < AT91_PMC_AUDIO_PLL_QDPAD_EXTDIV_MAX; tmp_qd++)
  245. for (div = 2; div <= 3; div++) {
  246. if (div == 2 && tmp_qd % 3 == 0)
  247. continue;
  248. best_parent_rate = clk_hw_round_rate(pclk,
  249. rate * tmp_qd * div);
  250. tmp_rate = best_parent_rate / (div * tmp_qd);
  251. tmp_diff = abs(rate - tmp_rate);
  252. if (best_diff < 0 || best_diff > tmp_diff) {
  253. *parent_rate = best_parent_rate;
  254. best_rate = tmp_rate;
  255. best_diff = tmp_diff;
  256. }
  257. }
  258. pr_debug("A PLL/PAD: %s, best_rate = %ld, best_parent_rate = %lu\n",
  259. __func__, best_rate, best_parent_rate);
  260. return best_rate;
  261. }
  262. static long clk_audio_pll_pmc_round_rate(struct clk_hw *hw, unsigned long rate,
  263. unsigned long *parent_rate)
  264. {
  265. struct clk_hw *pclk = clk_hw_get_parent(hw);
  266. long best_rate = -EINVAL;
  267. unsigned long best_parent_rate = 0;
  268. u32 tmp_qd = 0, div;
  269. long tmp_rate;
  270. int tmp_diff;
  271. int best_diff = -1;
  272. pr_debug("A PLL/PMC: %s, rate = %lu (parent_rate = %lu)\n", __func__,
  273. rate, *parent_rate);
  274. for (div = 1; div <= AUDIO_PLL_QDPMC_MAX; div++) {
  275. best_parent_rate = clk_round_rate(pclk->clk, rate * div);
  276. tmp_rate = best_parent_rate / div;
  277. tmp_diff = abs(rate - tmp_rate);
  278. if (best_diff < 0 || best_diff > tmp_diff) {
  279. *parent_rate = best_parent_rate;
  280. best_rate = tmp_rate;
  281. best_diff = tmp_diff;
  282. tmp_qd = div;
  283. }
  284. }
  285. pr_debug("A PLL/PMC: %s, best_rate = %ld, best_parent_rate = %lu (qd = %d)\n",
  286. __func__, best_rate, *parent_rate, tmp_qd - 1);
  287. return best_rate;
  288. }
  289. static int clk_audio_pll_frac_set_rate(struct clk_hw *hw, unsigned long rate,
  290. unsigned long parent_rate)
  291. {
  292. struct clk_audio_frac *frac = to_clk_audio_frac(hw);
  293. unsigned long fracr, nd;
  294. int ret;
  295. pr_debug("A PLL: %s, rate = %lu (parent_rate = %lu)\n", __func__, rate,
  296. parent_rate);
  297. if (rate < AUDIO_PLL_FOUT_MIN || rate > AUDIO_PLL_FOUT_MAX)
  298. return -EINVAL;
  299. ret = clk_audio_pll_frac_compute_frac(rate, parent_rate, &nd, &fracr);
  300. if (ret)
  301. return ret;
  302. frac->nd = nd;
  303. frac->fracr = fracr;
  304. return 0;
  305. }
  306. static int clk_audio_pll_pad_set_rate(struct clk_hw *hw, unsigned long rate,
  307. unsigned long parent_rate)
  308. {
  309. struct clk_audio_pad *apad_ck = to_clk_audio_pad(hw);
  310. u8 tmp_div;
  311. pr_debug("A PLL/PAD: %s, rate = %lu (parent_rate = %lu)\n", __func__,
  312. rate, parent_rate);
  313. if (!rate)
  314. return -EINVAL;
  315. tmp_div = parent_rate / rate;
  316. if (tmp_div % 3 == 0) {
  317. apad_ck->qdaudio = tmp_div / 3;
  318. apad_ck->div = 3;
  319. } else {
  320. apad_ck->qdaudio = tmp_div / 2;
  321. apad_ck->div = 2;
  322. }
  323. return 0;
  324. }
  325. static int clk_audio_pll_pmc_set_rate(struct clk_hw *hw, unsigned long rate,
  326. unsigned long parent_rate)
  327. {
  328. struct clk_audio_pmc *apmc_ck = to_clk_audio_pmc(hw);
  329. if (!rate)
  330. return -EINVAL;
  331. pr_debug("A PLL/PMC: %s, rate = %lu (parent_rate = %lu)\n", __func__,
  332. rate, parent_rate);
  333. apmc_ck->qdpmc = parent_rate / rate - 1;
  334. return 0;
  335. }
  336. static const struct clk_ops audio_pll_frac_ops = {
  337. .enable = clk_audio_pll_frac_enable,
  338. .disable = clk_audio_pll_frac_disable,
  339. .recalc_rate = clk_audio_pll_frac_recalc_rate,
  340. .determine_rate = clk_audio_pll_frac_determine_rate,
  341. .set_rate = clk_audio_pll_frac_set_rate,
  342. };
  343. static const struct clk_ops audio_pll_pad_ops = {
  344. .enable = clk_audio_pll_pad_enable,
  345. .disable = clk_audio_pll_pad_disable,
  346. .recalc_rate = clk_audio_pll_pad_recalc_rate,
  347. .round_rate = clk_audio_pll_pad_round_rate,
  348. .set_rate = clk_audio_pll_pad_set_rate,
  349. };
  350. static const struct clk_ops audio_pll_pmc_ops = {
  351. .enable = clk_audio_pll_pmc_enable,
  352. .disable = clk_audio_pll_pmc_disable,
  353. .recalc_rate = clk_audio_pll_pmc_recalc_rate,
  354. .round_rate = clk_audio_pll_pmc_round_rate,
  355. .set_rate = clk_audio_pll_pmc_set_rate,
  356. };
  357. static int of_sama5d2_clk_audio_pll_setup(struct device_node *np,
  358. struct clk_init_data *init,
  359. struct clk_hw *hw,
  360. struct regmap **clk_audio_regmap)
  361. {
  362. struct regmap *regmap;
  363. const char *parent_names[1];
  364. int ret;
  365. regmap = syscon_node_to_regmap(of_get_parent(np));
  366. if (IS_ERR(regmap))
  367. return PTR_ERR(regmap);
  368. init->name = np->name;
  369. of_clk_parent_fill(np, parent_names, 1);
  370. init->parent_names = parent_names;
  371. init->num_parents = 1;
  372. hw->init = init;
  373. *clk_audio_regmap = regmap;
  374. ret = clk_hw_register(NULL, hw);
  375. if (ret)
  376. return ret;
  377. return of_clk_add_hw_provider(np, of_clk_hw_simple_get, hw);
  378. }
  379. static void __init of_sama5d2_clk_audio_pll_frac_setup(struct device_node *np)
  380. {
  381. struct clk_audio_frac *frac_ck;
  382. struct clk_init_data init = {};
  383. frac_ck = kzalloc(sizeof(*frac_ck), GFP_KERNEL);
  384. if (!frac_ck)
  385. return;
  386. init.ops = &audio_pll_frac_ops;
  387. init.flags = CLK_SET_RATE_GATE;
  388. if (of_sama5d2_clk_audio_pll_setup(np, &init, &frac_ck->hw,
  389. &frac_ck->regmap))
  390. kfree(frac_ck);
  391. }
  392. static void __init of_sama5d2_clk_audio_pll_pad_setup(struct device_node *np)
  393. {
  394. struct clk_audio_pad *apad_ck;
  395. struct clk_init_data init = {};
  396. apad_ck = kzalloc(sizeof(*apad_ck), GFP_KERNEL);
  397. if (!apad_ck)
  398. return;
  399. init.ops = &audio_pll_pad_ops;
  400. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
  401. CLK_SET_RATE_PARENT;
  402. if (of_sama5d2_clk_audio_pll_setup(np, &init, &apad_ck->hw,
  403. &apad_ck->regmap))
  404. kfree(apad_ck);
  405. }
  406. static void __init of_sama5d2_clk_audio_pll_pmc_setup(struct device_node *np)
  407. {
  408. struct clk_audio_pmc *apmc_ck;
  409. struct clk_init_data init = {};
  410. apmc_ck = kzalloc(sizeof(*apmc_ck), GFP_KERNEL);
  411. if (!apmc_ck)
  412. return;
  413. init.ops = &audio_pll_pmc_ops;
  414. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
  415. CLK_SET_RATE_PARENT;
  416. if (of_sama5d2_clk_audio_pll_setup(np, &init, &apmc_ck->hw,
  417. &apmc_ck->regmap))
  418. kfree(apmc_ck);
  419. }
  420. CLK_OF_DECLARE(of_sama5d2_clk_audio_pll_frac_setup,
  421. "atmel,sama5d2-clk-audio-pll-frac",
  422. of_sama5d2_clk_audio_pll_frac_setup);
  423. CLK_OF_DECLARE(of_sama5d2_clk_audio_pll_pad_setup,
  424. "atmel,sama5d2-clk-audio-pll-pad",
  425. of_sama5d2_clk_audio_pll_pad_setup);
  426. CLK_OF_DECLARE(of_sama5d2_clk_audio_pll_pmc_setup,
  427. "atmel,sama5d2-clk-audio-pll-pmc",
  428. of_sama5d2_clk_audio_pll_pmc_setup);