clk-vt8500.c 18 KB

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