clk-vt8500.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Clock implementation for VIA/Wondermedia SoC's
  4. * Copyright (C) 2012 Tony Prisk <[email protected]>
  5. */
  6. #include <linux/io.h>
  7. #include <linux/of.h>
  8. #include <linux/of_address.h>
  9. #include <linux/slab.h>
  10. #include <linux/bitops.h>
  11. #include <linux/clkdev.h>
  12. #include <linux/clk-provider.h>
  13. #define LEGACY_PMC_BASE 0xD8130000
  14. /* All clocks share the same lock as none can be changed concurrently */
  15. static DEFINE_SPINLOCK(_lock);
  16. struct clk_device {
  17. struct clk_hw hw;
  18. void __iomem *div_reg;
  19. unsigned int div_mask;
  20. void __iomem *en_reg;
  21. int en_bit;
  22. spinlock_t *lock;
  23. };
  24. /*
  25. * Add new PLL_TYPE_x definitions here as required. Use the first known model
  26. * to support the new type as the name.
  27. * Add case statements to vtwm_pll_recalc_rate(), vtwm_pll_round_round() and
  28. * vtwm_pll_set_rate() to handle the new PLL_TYPE_x
  29. */
  30. #define PLL_TYPE_VT8500 0
  31. #define PLL_TYPE_WM8650 1
  32. #define PLL_TYPE_WM8750 2
  33. #define PLL_TYPE_WM8850 3
  34. struct clk_pll {
  35. struct clk_hw hw;
  36. void __iomem *reg;
  37. spinlock_t *lock;
  38. int type;
  39. };
  40. static void __iomem *pmc_base;
  41. static __init void vtwm_set_pmc_base(void)
  42. {
  43. struct device_node *np =
  44. of_find_compatible_node(NULL, NULL, "via,vt8500-pmc");
  45. if (np)
  46. pmc_base = of_iomap(np, 0);
  47. else
  48. pmc_base = ioremap(LEGACY_PMC_BASE, 0x1000);
  49. of_node_put(np);
  50. if (!pmc_base)
  51. pr_err("%s:of_iomap(pmc) failed\n", __func__);
  52. }
  53. #define to_clk_device(_hw) container_of(_hw, struct clk_device, hw)
  54. #define VT8500_PMC_BUSY_MASK 0x18
  55. static void vt8500_pmc_wait_busy(void)
  56. {
  57. while (readl(pmc_base) & VT8500_PMC_BUSY_MASK)
  58. cpu_relax();
  59. }
  60. static int vt8500_dclk_enable(struct clk_hw *hw)
  61. {
  62. struct clk_device *cdev = to_clk_device(hw);
  63. u32 en_val;
  64. unsigned long flags = 0;
  65. spin_lock_irqsave(cdev->lock, flags);
  66. en_val = readl(cdev->en_reg);
  67. en_val |= BIT(cdev->en_bit);
  68. writel(en_val, cdev->en_reg);
  69. spin_unlock_irqrestore(cdev->lock, flags);
  70. return 0;
  71. }
  72. static void vt8500_dclk_disable(struct clk_hw *hw)
  73. {
  74. struct clk_device *cdev = to_clk_device(hw);
  75. u32 en_val;
  76. unsigned long flags = 0;
  77. spin_lock_irqsave(cdev->lock, flags);
  78. en_val = readl(cdev->en_reg);
  79. en_val &= ~BIT(cdev->en_bit);
  80. writel(en_val, cdev->en_reg);
  81. spin_unlock_irqrestore(cdev->lock, flags);
  82. }
  83. static int vt8500_dclk_is_enabled(struct clk_hw *hw)
  84. {
  85. struct clk_device *cdev = to_clk_device(hw);
  86. u32 en_val = (readl(cdev->en_reg) & BIT(cdev->en_bit));
  87. return en_val ? 1 : 0;
  88. }
  89. static unsigned long vt8500_dclk_recalc_rate(struct clk_hw *hw,
  90. unsigned long parent_rate)
  91. {
  92. struct clk_device *cdev = to_clk_device(hw);
  93. u32 div = readl(cdev->div_reg) & cdev->div_mask;
  94. /* Special case for SDMMC devices */
  95. if ((cdev->div_mask == 0x3F) && (div & BIT(5)))
  96. div = 64 * (div & 0x1f);
  97. /* div == 0 is actually the highest divisor */
  98. if (div == 0)
  99. div = (cdev->div_mask + 1);
  100. return parent_rate / div;
  101. }
  102. static long vt8500_dclk_round_rate(struct clk_hw *hw, unsigned long rate,
  103. unsigned long *prate)
  104. {
  105. struct clk_device *cdev = to_clk_device(hw);
  106. u32 divisor;
  107. if (rate == 0)
  108. return 0;
  109. divisor = *prate / rate;
  110. /* If prate / rate would be decimal, incr the divisor */
  111. if (rate * divisor < *prate)
  112. divisor++;
  113. /*
  114. * If this is a request for SDMMC we have to adjust the divisor
  115. * when >31 to use the fixed predivisor
  116. */
  117. if ((cdev->div_mask == 0x3F) && (divisor > 31)) {
  118. divisor = 64 * ((divisor / 64) + 1);
  119. }
  120. return *prate / divisor;
  121. }
  122. static int vt8500_dclk_set_rate(struct clk_hw *hw, unsigned long rate,
  123. unsigned long parent_rate)
  124. {
  125. struct clk_device *cdev = to_clk_device(hw);
  126. u32 divisor;
  127. unsigned long flags = 0;
  128. if (rate == 0)
  129. return 0;
  130. divisor = parent_rate / rate;
  131. if (divisor == cdev->div_mask + 1)
  132. divisor = 0;
  133. /* SDMMC mask may need to be corrected before testing if its valid */
  134. if ((cdev->div_mask == 0x3F) && (divisor > 31)) {
  135. /*
  136. * Bit 5 is a fixed /64 predivisor. If the requested divisor
  137. * is >31 then correct for the fixed divisor being required.
  138. */
  139. divisor = 0x20 + (divisor / 64);
  140. }
  141. if (divisor > cdev->div_mask) {
  142. pr_err("%s: invalid divisor for clock\n", __func__);
  143. return -EINVAL;
  144. }
  145. spin_lock_irqsave(cdev->lock, flags);
  146. vt8500_pmc_wait_busy();
  147. writel(divisor, cdev->div_reg);
  148. vt8500_pmc_wait_busy();
  149. spin_unlock_irqrestore(cdev->lock, flags);
  150. return 0;
  151. }
  152. static const struct clk_ops vt8500_gated_clk_ops = {
  153. .enable = vt8500_dclk_enable,
  154. .disable = vt8500_dclk_disable,
  155. .is_enabled = vt8500_dclk_is_enabled,
  156. };
  157. static const struct clk_ops vt8500_divisor_clk_ops = {
  158. .round_rate = vt8500_dclk_round_rate,
  159. .set_rate = vt8500_dclk_set_rate,
  160. .recalc_rate = vt8500_dclk_recalc_rate,
  161. };
  162. static const struct clk_ops vt8500_gated_divisor_clk_ops = {
  163. .enable = vt8500_dclk_enable,
  164. .disable = vt8500_dclk_disable,
  165. .is_enabled = vt8500_dclk_is_enabled,
  166. .round_rate = vt8500_dclk_round_rate,
  167. .set_rate = vt8500_dclk_set_rate,
  168. .recalc_rate = vt8500_dclk_recalc_rate,
  169. };
  170. #define CLK_INIT_GATED BIT(0)
  171. #define CLK_INIT_DIVISOR BIT(1)
  172. #define CLK_INIT_GATED_DIVISOR (CLK_INIT_DIVISOR | CLK_INIT_GATED)
  173. static __init void vtwm_device_clk_init(struct device_node *node)
  174. {
  175. u32 en_reg, div_reg;
  176. struct clk_hw *hw;
  177. struct clk_device *dev_clk;
  178. const char *clk_name = node->name;
  179. const char *parent_name;
  180. struct clk_init_data init;
  181. int rc;
  182. int clk_init_flags = 0;
  183. if (!pmc_base)
  184. vtwm_set_pmc_base();
  185. dev_clk = kzalloc(sizeof(*dev_clk), GFP_KERNEL);
  186. if (WARN_ON(!dev_clk))
  187. return;
  188. dev_clk->lock = &_lock;
  189. rc = of_property_read_u32(node, "enable-reg", &en_reg);
  190. if (!rc) {
  191. dev_clk->en_reg = pmc_base + en_reg;
  192. rc = of_property_read_u32(node, "enable-bit", &dev_clk->en_bit);
  193. if (rc) {
  194. pr_err("%s: enable-bit property required for gated clock\n",
  195. __func__);
  196. return;
  197. }
  198. clk_init_flags |= CLK_INIT_GATED;
  199. }
  200. rc = of_property_read_u32(node, "divisor-reg", &div_reg);
  201. if (!rc) {
  202. dev_clk->div_reg = pmc_base + div_reg;
  203. /*
  204. * use 0x1f as the default mask since it covers
  205. * almost all the clocks and reduces dts properties
  206. */
  207. dev_clk->div_mask = 0x1f;
  208. of_property_read_u32(node, "divisor-mask", &dev_clk->div_mask);
  209. clk_init_flags |= CLK_INIT_DIVISOR;
  210. }
  211. of_property_read_string(node, "clock-output-names", &clk_name);
  212. switch (clk_init_flags) {
  213. case CLK_INIT_GATED:
  214. init.ops = &vt8500_gated_clk_ops;
  215. break;
  216. case CLK_INIT_DIVISOR:
  217. init.ops = &vt8500_divisor_clk_ops;
  218. break;
  219. case CLK_INIT_GATED_DIVISOR:
  220. init.ops = &vt8500_gated_divisor_clk_ops;
  221. break;
  222. default:
  223. pr_err("%s: Invalid clock description in device tree\n",
  224. __func__);
  225. kfree(dev_clk);
  226. return;
  227. }
  228. init.name = clk_name;
  229. init.flags = 0;
  230. parent_name = of_clk_get_parent_name(node, 0);
  231. init.parent_names = &parent_name;
  232. init.num_parents = 1;
  233. dev_clk->hw.init = &init;
  234. hw = &dev_clk->hw;
  235. rc = clk_hw_register(NULL, hw);
  236. if (WARN_ON(rc)) {
  237. kfree(dev_clk);
  238. return;
  239. }
  240. rc = of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw);
  241. clk_hw_register_clkdev(hw, clk_name, NULL);
  242. }
  243. CLK_OF_DECLARE(vt8500_device, "via,vt8500-device-clock", vtwm_device_clk_init);
  244. /* PLL clock related functions */
  245. #define to_clk_pll(_hw) container_of(_hw, struct clk_pll, hw)
  246. /* Helper macros for PLL_VT8500 */
  247. #define VT8500_PLL_MUL(x) ((x & 0x1F) << 1)
  248. #define VT8500_PLL_DIV(x) ((x & 0x100) ? 1 : 2)
  249. #define VT8500_BITS_TO_FREQ(r, m, d) \
  250. ((r / d) * m)
  251. #define VT8500_BITS_TO_VAL(m, d) \
  252. ((d == 2 ? 0 : 0x100) | ((m >> 1) & 0x1F))
  253. /* Helper macros for PLL_WM8650 */
  254. #define WM8650_PLL_MUL(x) (x & 0x3FF)
  255. #define WM8650_PLL_DIV(x) (((x >> 10) & 7) * (1 << ((x >> 13) & 3)))
  256. #define WM8650_BITS_TO_FREQ(r, m, d1, d2) \
  257. (r * m / (d1 * (1 << d2)))
  258. #define WM8650_BITS_TO_VAL(m, d1, d2) \
  259. ((d2 << 13) | (d1 << 10) | (m & 0x3FF))
  260. /* Helper macros for PLL_WM8750 */
  261. #define WM8750_PLL_MUL(x) (((x >> 16) & 0xFF) + 1)
  262. #define WM8750_PLL_DIV(x) ((((x >> 8) & 1) + 1) * (1 << (x & 7)))
  263. #define WM8750_BITS_TO_FREQ(r, m, d1, d2) \
  264. (r * (m+1) / ((d1+1) * (1 << d2)))
  265. #define WM8750_BITS_TO_VAL(f, m, d1, d2) \
  266. ((f << 24) | ((m - 1) << 16) | ((d1 - 1) << 8) | d2)
  267. /* Helper macros for PLL_WM8850 */
  268. #define WM8850_PLL_MUL(x) ((((x >> 16) & 0x7F) + 1) * 2)
  269. #define WM8850_PLL_DIV(x) ((((x >> 8) & 1) + 1) * (1 << (x & 3)))
  270. #define WM8850_BITS_TO_FREQ(r, m, d1, d2) \
  271. (r * ((m + 1) * 2) / ((d1+1) * (1 << d2)))
  272. #define WM8850_BITS_TO_VAL(m, d1, d2) \
  273. ((((m / 2) - 1) << 16) | ((d1 - 1) << 8) | d2)
  274. static int vt8500_find_pll_bits(unsigned long rate, unsigned long parent_rate,
  275. u32 *multiplier, u32 *prediv)
  276. {
  277. unsigned long tclk;
  278. /* sanity check */
  279. if ((rate < parent_rate * 4) || (rate > parent_rate * 62)) {
  280. pr_err("%s: requested rate out of range\n", __func__);
  281. *multiplier = 0;
  282. *prediv = 1;
  283. return -EINVAL;
  284. }
  285. if (rate <= parent_rate * 31)
  286. /* use the prediv to double the resolution */
  287. *prediv = 2;
  288. else
  289. *prediv = 1;
  290. *multiplier = rate / (parent_rate / *prediv);
  291. tclk = (parent_rate / *prediv) * *multiplier;
  292. if (tclk != rate)
  293. pr_warn("%s: requested rate %lu, found rate %lu\n", __func__,
  294. rate, tclk);
  295. return 0;
  296. }
  297. /*
  298. * M * parent [O1] => / P [O2] => / D [O3]
  299. * Where O1 is 900MHz...3GHz;
  300. * O2 is 600MHz >= (M * parent) / P >= 300MHz;
  301. * M is 36...120 [25MHz parent]; D is 1 or 2 or 4 or 8.
  302. * Possible ranges (O3):
  303. * D = 8: 37,5MHz...75MHz
  304. * D = 4: 75MHz...150MHz
  305. * D = 2: 150MHz...300MHz
  306. * D = 1: 300MHz...600MHz
  307. */
  308. static int wm8650_find_pll_bits(unsigned long rate,
  309. unsigned long parent_rate, u32 *multiplier, u32 *divisor1,
  310. u32 *divisor2)
  311. {
  312. unsigned long O1, min_err, rate_err;
  313. if (!parent_rate || (rate < 37500000) || (rate > 600000000))
  314. return -EINVAL;
  315. *divisor2 = rate <= 75000000 ? 3 : rate <= 150000000 ? 2 :
  316. rate <= 300000000 ? 1 : 0;
  317. /*
  318. * Divisor P cannot be calculated. Test all divisors and find where M
  319. * will be as close as possible to the requested rate.
  320. */
  321. min_err = ULONG_MAX;
  322. for (*divisor1 = 5; *divisor1 >= 3; (*divisor1)--) {
  323. O1 = rate * *divisor1 * (1 << (*divisor2));
  324. rate_err = O1 % parent_rate;
  325. if (rate_err < min_err) {
  326. *multiplier = O1 / parent_rate;
  327. if (rate_err == 0)
  328. return 0;
  329. min_err = rate_err;
  330. }
  331. }
  332. if ((*multiplier < 3) || (*multiplier > 1023))
  333. return -EINVAL;
  334. pr_warn("%s: rate error is %lu\n", __func__, min_err);
  335. return 0;
  336. }
  337. static u32 wm8750_get_filter(u32 parent_rate, u32 divisor1)
  338. {
  339. /* calculate frequency (MHz) after pre-divisor */
  340. u32 freq = (parent_rate / 1000000) / (divisor1 + 1);
  341. if ((freq < 10) || (freq > 200))
  342. pr_warn("%s: PLL recommended input frequency 10..200Mhz (requested %d Mhz)\n",
  343. __func__, freq);
  344. if (freq >= 166)
  345. return 7;
  346. else if (freq >= 104)
  347. return 6;
  348. else if (freq >= 65)
  349. return 5;
  350. else if (freq >= 42)
  351. return 4;
  352. else if (freq >= 26)
  353. return 3;
  354. else if (freq >= 16)
  355. return 2;
  356. else if (freq >= 10)
  357. return 1;
  358. return 0;
  359. }
  360. static int wm8750_find_pll_bits(unsigned long rate, unsigned long parent_rate,
  361. u32 *filter, u32 *multiplier, u32 *divisor1, u32 *divisor2)
  362. {
  363. u32 mul;
  364. int div1, div2;
  365. unsigned long tclk, rate_err, best_err;
  366. best_err = (unsigned long)-1;
  367. /* Find the closest match (lower or equal to requested) */
  368. for (div1 = 1; div1 >= 0; div1--)
  369. for (div2 = 7; div2 >= 0; div2--)
  370. for (mul = 0; mul <= 255; mul++) {
  371. tclk = parent_rate * (mul + 1) / ((div1 + 1) * (1 << div2));
  372. if (tclk > rate)
  373. continue;
  374. /* error will always be +ve */
  375. rate_err = rate - tclk;
  376. if (rate_err == 0) {
  377. *filter = wm8750_get_filter(parent_rate, div1);
  378. *multiplier = mul;
  379. *divisor1 = div1;
  380. *divisor2 = div2;
  381. return 0;
  382. }
  383. if (rate_err < best_err) {
  384. best_err = rate_err;
  385. *multiplier = mul;
  386. *divisor1 = div1;
  387. *divisor2 = div2;
  388. }
  389. }
  390. if (best_err == (unsigned long)-1) {
  391. pr_warn("%s: impossible rate %lu\n", __func__, rate);
  392. return -EINVAL;
  393. }
  394. /* if we got here, it wasn't an exact match */
  395. pr_warn("%s: requested rate %lu, found rate %lu\n", __func__, rate,
  396. rate - best_err);
  397. *filter = wm8750_get_filter(parent_rate, *divisor1);
  398. return 0;
  399. }
  400. static int wm8850_find_pll_bits(unsigned long rate, unsigned long parent_rate,
  401. u32 *multiplier, u32 *divisor1, u32 *divisor2)
  402. {
  403. u32 mul;
  404. int div1, div2;
  405. unsigned long tclk, rate_err, best_err;
  406. best_err = (unsigned long)-1;
  407. /* Find the closest match (lower or equal to requested) */
  408. for (div1 = 1; div1 >= 0; div1--)
  409. for (div2 = 3; div2 >= 0; div2--)
  410. for (mul = 0; mul <= 127; mul++) {
  411. tclk = parent_rate * ((mul + 1) * 2) /
  412. ((div1 + 1) * (1 << div2));
  413. if (tclk > rate)
  414. continue;
  415. /* error will always be +ve */
  416. rate_err = rate - tclk;
  417. if (rate_err == 0) {
  418. *multiplier = mul;
  419. *divisor1 = div1;
  420. *divisor2 = div2;
  421. return 0;
  422. }
  423. if (rate_err < best_err) {
  424. best_err = rate_err;
  425. *multiplier = mul;
  426. *divisor1 = div1;
  427. *divisor2 = div2;
  428. }
  429. }
  430. if (best_err == (unsigned long)-1) {
  431. pr_warn("%s: impossible rate %lu\n", __func__, rate);
  432. return -EINVAL;
  433. }
  434. /* if we got here, it wasn't an exact match */
  435. pr_warn("%s: requested rate %lu, found rate %lu\n", __func__, rate,
  436. rate - best_err);
  437. return 0;
  438. }
  439. static int vtwm_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  440. unsigned long parent_rate)
  441. {
  442. struct clk_pll *pll = to_clk_pll(hw);
  443. u32 filter, mul, div1, div2;
  444. u32 pll_val;
  445. unsigned long flags = 0;
  446. int ret;
  447. /* sanity check */
  448. switch (pll->type) {
  449. case PLL_TYPE_VT8500:
  450. ret = vt8500_find_pll_bits(rate, parent_rate, &mul, &div1);
  451. if (!ret)
  452. pll_val = VT8500_BITS_TO_VAL(mul, div1);
  453. break;
  454. case PLL_TYPE_WM8650:
  455. ret = wm8650_find_pll_bits(rate, parent_rate, &mul, &div1, &div2);
  456. if (!ret)
  457. pll_val = WM8650_BITS_TO_VAL(mul, div1, div2);
  458. break;
  459. case PLL_TYPE_WM8750:
  460. ret = wm8750_find_pll_bits(rate, parent_rate, &filter, &mul, &div1, &div2);
  461. if (!ret)
  462. pll_val = WM8750_BITS_TO_VAL(filter, mul, div1, div2);
  463. break;
  464. case PLL_TYPE_WM8850:
  465. ret = wm8850_find_pll_bits(rate, parent_rate, &mul, &div1, &div2);
  466. if (!ret)
  467. pll_val = WM8850_BITS_TO_VAL(mul, div1, div2);
  468. break;
  469. default:
  470. pr_err("%s: invalid pll type\n", __func__);
  471. ret = -EINVAL;
  472. }
  473. if (ret)
  474. return ret;
  475. spin_lock_irqsave(pll->lock, flags);
  476. vt8500_pmc_wait_busy();
  477. writel(pll_val, pll->reg);
  478. vt8500_pmc_wait_busy();
  479. spin_unlock_irqrestore(pll->lock, flags);
  480. return 0;
  481. }
  482. static long vtwm_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  483. unsigned long *prate)
  484. {
  485. struct clk_pll *pll = to_clk_pll(hw);
  486. u32 filter, mul, div1, div2;
  487. long round_rate;
  488. int ret;
  489. switch (pll->type) {
  490. case PLL_TYPE_VT8500:
  491. ret = vt8500_find_pll_bits(rate, *prate, &mul, &div1);
  492. if (!ret)
  493. round_rate = VT8500_BITS_TO_FREQ(*prate, mul, div1);
  494. break;
  495. case PLL_TYPE_WM8650:
  496. ret = wm8650_find_pll_bits(rate, *prate, &mul, &div1, &div2);
  497. if (!ret)
  498. round_rate = WM8650_BITS_TO_FREQ(*prate, mul, div1, div2);
  499. break;
  500. case PLL_TYPE_WM8750:
  501. ret = wm8750_find_pll_bits(rate, *prate, &filter, &mul, &div1, &div2);
  502. if (!ret)
  503. round_rate = WM8750_BITS_TO_FREQ(*prate, mul, div1, div2);
  504. break;
  505. case PLL_TYPE_WM8850:
  506. ret = wm8850_find_pll_bits(rate, *prate, &mul, &div1, &div2);
  507. if (!ret)
  508. round_rate = WM8850_BITS_TO_FREQ(*prate, mul, div1, div2);
  509. break;
  510. default:
  511. ret = -EINVAL;
  512. }
  513. if (ret)
  514. return ret;
  515. return round_rate;
  516. }
  517. static unsigned long vtwm_pll_recalc_rate(struct clk_hw *hw,
  518. unsigned long parent_rate)
  519. {
  520. struct clk_pll *pll = to_clk_pll(hw);
  521. u32 pll_val = readl(pll->reg);
  522. unsigned long pll_freq;
  523. switch (pll->type) {
  524. case PLL_TYPE_VT8500:
  525. pll_freq = parent_rate * VT8500_PLL_MUL(pll_val);
  526. pll_freq /= VT8500_PLL_DIV(pll_val);
  527. break;
  528. case PLL_TYPE_WM8650:
  529. pll_freq = parent_rate * WM8650_PLL_MUL(pll_val);
  530. pll_freq /= WM8650_PLL_DIV(pll_val);
  531. break;
  532. case PLL_TYPE_WM8750:
  533. pll_freq = parent_rate * WM8750_PLL_MUL(pll_val);
  534. pll_freq /= WM8750_PLL_DIV(pll_val);
  535. break;
  536. case PLL_TYPE_WM8850:
  537. pll_freq = parent_rate * WM8850_PLL_MUL(pll_val);
  538. pll_freq /= WM8850_PLL_DIV(pll_val);
  539. break;
  540. default:
  541. pll_freq = 0;
  542. }
  543. return pll_freq;
  544. }
  545. static const struct clk_ops vtwm_pll_ops = {
  546. .round_rate = vtwm_pll_round_rate,
  547. .set_rate = vtwm_pll_set_rate,
  548. .recalc_rate = vtwm_pll_recalc_rate,
  549. };
  550. static __init void vtwm_pll_clk_init(struct device_node *node, int pll_type)
  551. {
  552. u32 reg;
  553. struct clk_hw *hw;
  554. struct clk_pll *pll_clk;
  555. const char *clk_name = node->name;
  556. const char *parent_name;
  557. struct clk_init_data init;
  558. int rc;
  559. if (!pmc_base)
  560. vtwm_set_pmc_base();
  561. rc = of_property_read_u32(node, "reg", &reg);
  562. if (WARN_ON(rc))
  563. return;
  564. pll_clk = kzalloc(sizeof(*pll_clk), GFP_KERNEL);
  565. if (WARN_ON(!pll_clk))
  566. return;
  567. pll_clk->reg = pmc_base + reg;
  568. pll_clk->lock = &_lock;
  569. pll_clk->type = pll_type;
  570. of_property_read_string(node, "clock-output-names", &clk_name);
  571. init.name = clk_name;
  572. init.ops = &vtwm_pll_ops;
  573. init.flags = 0;
  574. parent_name = of_clk_get_parent_name(node, 0);
  575. init.parent_names = &parent_name;
  576. init.num_parents = 1;
  577. pll_clk->hw.init = &init;
  578. hw = &pll_clk->hw;
  579. rc = clk_hw_register(NULL, &pll_clk->hw);
  580. if (WARN_ON(rc)) {
  581. kfree(pll_clk);
  582. return;
  583. }
  584. rc = of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw);
  585. clk_hw_register_clkdev(hw, clk_name, NULL);
  586. }
  587. /* Wrappers for initialization functions */
  588. static void __init vt8500_pll_init(struct device_node *node)
  589. {
  590. vtwm_pll_clk_init(node, PLL_TYPE_VT8500);
  591. }
  592. CLK_OF_DECLARE(vt8500_pll, "via,vt8500-pll-clock", vt8500_pll_init);
  593. static void __init wm8650_pll_init(struct device_node *node)
  594. {
  595. vtwm_pll_clk_init(node, PLL_TYPE_WM8650);
  596. }
  597. CLK_OF_DECLARE(wm8650_pll, "wm,wm8650-pll-clock", wm8650_pll_init);
  598. static void __init wm8750_pll_init(struct device_node *node)
  599. {
  600. vtwm_pll_clk_init(node, PLL_TYPE_WM8750);
  601. }
  602. CLK_OF_DECLARE(wm8750_pll, "wm,wm8750-pll-clock", wm8750_pll_init);
  603. static void __init wm8850_pll_init(struct device_node *node)
  604. {
  605. vtwm_pll_clk_init(node, PLL_TYPE_WM8850);
  606. }
  607. CLK_OF_DECLARE(wm8850_pll, "wm,wm8850-pll-clock", wm8850_pll_init);