clk-audio-pll.c 14 KB

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