clk-vt8500.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * Clock implementation for VIA/Wondermedia SoC's
  3. * Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz>
  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 *clk;
  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. clk = clk_register(NULL, &dev_clk->hw);
  244. if (WARN_ON(IS_ERR(clk))) {
  245. kfree(dev_clk);
  246. return;
  247. }
  248. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  249. clk_register_clkdev(clk, clk_name, NULL);
  250. }
  251. CLK_OF_DECLARE(vt8500_device, "via,vt8500-device-clock", vtwm_device_clk_init);
  252. /* PLL clock related functions */
  253. #define to_clk_pll(_hw) container_of(_hw, struct clk_pll, hw)
  254. /* Helper macros for PLL_VT8500 */
  255. #define VT8500_PLL_MUL(x) ((x & 0x1F) << 1)
  256. #define VT8500_PLL_DIV(x) ((x & 0x100) ? 1 : 2)
  257. #define VT8500_BITS_TO_FREQ(r, m, d) \
  258. ((r / d) * m)
  259. #define VT8500_BITS_TO_VAL(m, d) \
  260. ((d == 2 ? 0 : 0x100) | ((m >> 1) & 0x1F))
  261. /* Helper macros for PLL_WM8650 */
  262. #define WM8650_PLL_MUL(x) (x & 0x3FF)
  263. #define WM8650_PLL_DIV(x) (((x >> 10) & 7) * (1 << ((x >> 13) & 3)))
  264. #define WM8650_BITS_TO_FREQ(r, m, d1, d2) \
  265. (r * m / (d1 * (1 << d2)))
  266. #define WM8650_BITS_TO_VAL(m, d1, d2) \
  267. ((d2 << 13) | (d1 << 10) | (m & 0x3FF))
  268. /* Helper macros for PLL_WM8750 */
  269. #define WM8750_PLL_MUL(x) (((x >> 16) & 0xFF) + 1)
  270. #define WM8750_PLL_DIV(x) ((((x >> 8) & 1) + 1) * (1 << (x & 7)))
  271. #define WM8750_BITS_TO_FREQ(r, m, d1, d2) \
  272. (r * (m+1) / ((d1+1) * (1 << d2)))
  273. #define WM8750_BITS_TO_VAL(f, m, d1, d2) \
  274. ((f << 24) | ((m - 1) << 16) | ((d1 - 1) << 8) | d2)
  275. /* Helper macros for PLL_WM8850 */
  276. #define WM8850_PLL_MUL(x) ((((x >> 16) & 0x7F) + 1) * 2)
  277. #define WM8850_PLL_DIV(x) ((((x >> 8) & 1) + 1) * (1 << (x & 3)))
  278. #define WM8850_BITS_TO_FREQ(r, m, d1, d2) \
  279. (r * ((m + 1) * 2) / ((d1+1) * (1 << d2)))
  280. #define WM8850_BITS_TO_VAL(m, d1, d2) \
  281. ((((m / 2) - 1) << 16) | ((d1 - 1) << 8) | d2)
  282. static int vt8500_find_pll_bits(unsigned long rate, unsigned long parent_rate,
  283. u32 *multiplier, u32 *prediv)
  284. {
  285. unsigned long tclk;
  286. /* sanity check */
  287. if ((rate < parent_rate * 4) || (rate > parent_rate * 62)) {
  288. pr_err("%s: requested rate out of range\n", __func__);
  289. *multiplier = 0;
  290. *prediv = 1;
  291. return -EINVAL;
  292. }
  293. if (rate <= parent_rate * 31)
  294. /* use the prediv to double the resolution */
  295. *prediv = 2;
  296. else
  297. *prediv = 1;
  298. *multiplier = rate / (parent_rate / *prediv);
  299. tclk = (parent_rate / *prediv) * *multiplier;
  300. if (tclk != rate)
  301. pr_warn("%s: requested rate %lu, found rate %lu\n", __func__,
  302. rate, tclk);
  303. return 0;
  304. }
  305. /*
  306. * M * parent [O1] => / P [O2] => / D [O3]
  307. * Where O1 is 900MHz...3GHz;
  308. * O2 is 600MHz >= (M * parent) / P >= 300MHz;
  309. * M is 36...120 [25MHz parent]; D is 1 or 2 or 4 or 8.
  310. * Possible ranges (O3):
  311. * D = 8: 37,5MHz...75MHz
  312. * D = 4: 75MHz...150MHz
  313. * D = 2: 150MHz...300MHz
  314. * D = 1: 300MHz...600MHz
  315. */
  316. static int wm8650_find_pll_bits(unsigned long rate,
  317. unsigned long parent_rate, u32 *multiplier, u32 *divisor1,
  318. u32 *divisor2)
  319. {
  320. unsigned long O1, min_err, rate_err;
  321. if (!parent_rate || (rate < 37500000) || (rate > 600000000))
  322. return -EINVAL;
  323. *divisor2 = rate <= 75000000 ? 3 : rate <= 150000000 ? 2 :
  324. rate <= 300000000 ? 1 : 0;
  325. /*
  326. * Divisor P cannot be calculated. Test all divisors and find where M
  327. * will be as close as possible to the requested rate.
  328. */
  329. min_err = ULONG_MAX;
  330. for (*divisor1 = 5; *divisor1 >= 3; (*divisor1)--) {
  331. O1 = rate * *divisor1 * (1 << (*divisor2));
  332. rate_err = O1 % parent_rate;
  333. if (rate_err < min_err) {
  334. *multiplier = O1 / parent_rate;
  335. if (rate_err == 0)
  336. return 0;
  337. min_err = rate_err;
  338. }
  339. }
  340. if ((*multiplier < 3) || (*multiplier > 1023))
  341. return -EINVAL;
  342. pr_warn("%s: rate error is %lu\n", __func__, min_err);
  343. return 0;
  344. }
  345. static u32 wm8750_get_filter(u32 parent_rate, u32 divisor1)
  346. {
  347. /* calculate frequency (MHz) after pre-divisor */
  348. u32 freq = (parent_rate / 1000000) / (divisor1 + 1);
  349. if ((freq < 10) || (freq > 200))
  350. pr_warn("%s: PLL recommended input frequency 10..200Mhz (requested %d Mhz)\n",
  351. __func__, freq);
  352. if (freq >= 166)
  353. return 7;
  354. else if (freq >= 104)
  355. return 6;
  356. else if (freq >= 65)
  357. return 5;
  358. else if (freq >= 42)
  359. return 4;
  360. else if (freq >= 26)
  361. return 3;
  362. else if (freq >= 16)
  363. return 2;
  364. else if (freq >= 10)
  365. return 1;
  366. return 0;
  367. }
  368. static int wm8750_find_pll_bits(unsigned long rate, unsigned long parent_rate,
  369. u32 *filter, u32 *multiplier, u32 *divisor1, u32 *divisor2)
  370. {
  371. u32 mul;
  372. int div1, div2;
  373. unsigned long tclk, rate_err, best_err;
  374. best_err = (unsigned long)-1;
  375. /* Find the closest match (lower or equal to requested) */
  376. for (div1 = 1; div1 >= 0; div1--)
  377. for (div2 = 7; div2 >= 0; div2--)
  378. for (mul = 0; mul <= 255; mul++) {
  379. tclk = parent_rate * (mul + 1) / ((div1 + 1) * (1 << div2));
  380. if (tclk > rate)
  381. continue;
  382. /* error will always be +ve */
  383. rate_err = rate - tclk;
  384. if (rate_err == 0) {
  385. *filter = wm8750_get_filter(parent_rate, div1);
  386. *multiplier = mul;
  387. *divisor1 = div1;
  388. *divisor2 = div2;
  389. return 0;
  390. }
  391. if (rate_err < best_err) {
  392. best_err = rate_err;
  393. *multiplier = mul;
  394. *divisor1 = div1;
  395. *divisor2 = div2;
  396. }
  397. }
  398. if (best_err == (unsigned long)-1) {
  399. pr_warn("%s: impossible rate %lu\n", __func__, rate);
  400. return -EINVAL;
  401. }
  402. /* if we got here, it wasn't an exact match */
  403. pr_warn("%s: requested rate %lu, found rate %lu\n", __func__, rate,
  404. rate - best_err);
  405. *filter = wm8750_get_filter(parent_rate, *divisor1);
  406. return 0;
  407. }
  408. static int wm8850_find_pll_bits(unsigned long rate, unsigned long parent_rate,
  409. u32 *multiplier, u32 *divisor1, u32 *divisor2)
  410. {
  411. u32 mul;
  412. int div1, div2;
  413. unsigned long tclk, rate_err, best_err;
  414. best_err = (unsigned long)-1;
  415. /* Find the closest match (lower or equal to requested) */
  416. for (div1 = 1; div1 >= 0; div1--)
  417. for (div2 = 3; div2 >= 0; div2--)
  418. for (mul = 0; mul <= 127; mul++) {
  419. tclk = parent_rate * ((mul + 1) * 2) /
  420. ((div1 + 1) * (1 << div2));
  421. if (tclk > rate)
  422. continue;
  423. /* error will always be +ve */
  424. rate_err = rate - tclk;
  425. if (rate_err == 0) {
  426. *multiplier = mul;
  427. *divisor1 = div1;
  428. *divisor2 = div2;
  429. return 0;
  430. }
  431. if (rate_err < best_err) {
  432. best_err = rate_err;
  433. *multiplier = mul;
  434. *divisor1 = div1;
  435. *divisor2 = div2;
  436. }
  437. }
  438. if (best_err == (unsigned long)-1) {
  439. pr_warn("%s: impossible rate %lu\n", __func__, rate);
  440. return -EINVAL;
  441. }
  442. /* if we got here, it wasn't an exact match */
  443. pr_warn("%s: requested rate %lu, found rate %lu\n", __func__, rate,
  444. rate - best_err);
  445. return 0;
  446. }
  447. static int vtwm_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  448. unsigned long parent_rate)
  449. {
  450. struct clk_pll *pll = to_clk_pll(hw);
  451. u32 filter, mul, div1, div2;
  452. u32 pll_val;
  453. unsigned long flags = 0;
  454. int ret;
  455. /* sanity check */
  456. switch (pll->type) {
  457. case PLL_TYPE_VT8500:
  458. ret = vt8500_find_pll_bits(rate, parent_rate, &mul, &div1);
  459. if (!ret)
  460. pll_val = VT8500_BITS_TO_VAL(mul, div1);
  461. break;
  462. case PLL_TYPE_WM8650:
  463. ret = wm8650_find_pll_bits(rate, parent_rate, &mul, &div1, &div2);
  464. if (!ret)
  465. pll_val = WM8650_BITS_TO_VAL(mul, div1, div2);
  466. break;
  467. case PLL_TYPE_WM8750:
  468. ret = wm8750_find_pll_bits(rate, parent_rate, &filter, &mul, &div1, &div2);
  469. if (!ret)
  470. pll_val = WM8750_BITS_TO_VAL(filter, mul, div1, div2);
  471. break;
  472. case PLL_TYPE_WM8850:
  473. ret = wm8850_find_pll_bits(rate, parent_rate, &mul, &div1, &div2);
  474. if (!ret)
  475. pll_val = WM8850_BITS_TO_VAL(mul, div1, div2);
  476. break;
  477. default:
  478. pr_err("%s: invalid pll type\n", __func__);
  479. ret = -EINVAL;
  480. }
  481. if (ret)
  482. return ret;
  483. spin_lock_irqsave(pll->lock, flags);
  484. vt8500_pmc_wait_busy();
  485. writel(pll_val, pll->reg);
  486. vt8500_pmc_wait_busy();
  487. spin_unlock_irqrestore(pll->lock, flags);
  488. return 0;
  489. }
  490. static long vtwm_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  491. unsigned long *prate)
  492. {
  493. struct clk_pll *pll = to_clk_pll(hw);
  494. u32 filter, mul, div1, div2;
  495. long round_rate;
  496. int ret;
  497. switch (pll->type) {
  498. case PLL_TYPE_VT8500:
  499. ret = vt8500_find_pll_bits(rate, *prate, &mul, &div1);
  500. if (!ret)
  501. round_rate = VT8500_BITS_TO_FREQ(*prate, mul, div1);
  502. break;
  503. case PLL_TYPE_WM8650:
  504. ret = wm8650_find_pll_bits(rate, *prate, &mul, &div1, &div2);
  505. if (!ret)
  506. round_rate = WM8650_BITS_TO_FREQ(*prate, mul, div1, div2);
  507. break;
  508. case PLL_TYPE_WM8750:
  509. ret = wm8750_find_pll_bits(rate, *prate, &filter, &mul, &div1, &div2);
  510. if (!ret)
  511. round_rate = WM8750_BITS_TO_FREQ(*prate, mul, div1, div2);
  512. break;
  513. case PLL_TYPE_WM8850:
  514. ret = wm8850_find_pll_bits(rate, *prate, &mul, &div1, &div2);
  515. if (!ret)
  516. round_rate = WM8850_BITS_TO_FREQ(*prate, mul, div1, div2);
  517. break;
  518. default:
  519. ret = -EINVAL;
  520. }
  521. if (ret)
  522. return ret;
  523. return round_rate;
  524. }
  525. static unsigned long vtwm_pll_recalc_rate(struct clk_hw *hw,
  526. unsigned long parent_rate)
  527. {
  528. struct clk_pll *pll = to_clk_pll(hw);
  529. u32 pll_val = readl(pll->reg);
  530. unsigned long pll_freq;
  531. switch (pll->type) {
  532. case PLL_TYPE_VT8500:
  533. pll_freq = parent_rate * VT8500_PLL_MUL(pll_val);
  534. pll_freq /= VT8500_PLL_DIV(pll_val);
  535. break;
  536. case PLL_TYPE_WM8650:
  537. pll_freq = parent_rate * WM8650_PLL_MUL(pll_val);
  538. pll_freq /= WM8650_PLL_DIV(pll_val);
  539. break;
  540. case PLL_TYPE_WM8750:
  541. pll_freq = parent_rate * WM8750_PLL_MUL(pll_val);
  542. pll_freq /= WM8750_PLL_DIV(pll_val);
  543. break;
  544. case PLL_TYPE_WM8850:
  545. pll_freq = parent_rate * WM8850_PLL_MUL(pll_val);
  546. pll_freq /= WM8850_PLL_DIV(pll_val);
  547. break;
  548. default:
  549. pll_freq = 0;
  550. }
  551. return pll_freq;
  552. }
  553. static const struct clk_ops vtwm_pll_ops = {
  554. .round_rate = vtwm_pll_round_rate,
  555. .set_rate = vtwm_pll_set_rate,
  556. .recalc_rate = vtwm_pll_recalc_rate,
  557. };
  558. static __init void vtwm_pll_clk_init(struct device_node *node, int pll_type)
  559. {
  560. u32 reg;
  561. struct clk *clk;
  562. struct clk_pll *pll_clk;
  563. const char *clk_name = node->name;
  564. const char *parent_name;
  565. struct clk_init_data init;
  566. int rc;
  567. if (!pmc_base)
  568. vtwm_set_pmc_base();
  569. rc = of_property_read_u32(node, "reg", &reg);
  570. if (WARN_ON(rc))
  571. return;
  572. pll_clk = kzalloc(sizeof(*pll_clk), GFP_KERNEL);
  573. if (WARN_ON(!pll_clk))
  574. return;
  575. pll_clk->reg = pmc_base + reg;
  576. pll_clk->lock = &_lock;
  577. pll_clk->type = pll_type;
  578. of_property_read_string(node, "clock-output-names", &clk_name);
  579. init.name = clk_name;
  580. init.ops = &vtwm_pll_ops;
  581. init.flags = 0;
  582. parent_name = of_clk_get_parent_name(node, 0);
  583. init.parent_names = &parent_name;
  584. init.num_parents = 1;
  585. pll_clk->hw.init = &init;
  586. clk = clk_register(NULL, &pll_clk->hw);
  587. if (WARN_ON(IS_ERR(clk))) {
  588. kfree(pll_clk);
  589. return;
  590. }
  591. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  592. clk_register_clkdev(clk, clk_name, NULL);
  593. }
  594. /* Wrappers for initialization functions */
  595. static void __init vt8500_pll_init(struct device_node *node)
  596. {
  597. vtwm_pll_clk_init(node, PLL_TYPE_VT8500);
  598. }
  599. CLK_OF_DECLARE(vt8500_pll, "via,vt8500-pll-clock", vt8500_pll_init);
  600. static void __init wm8650_pll_init(struct device_node *node)
  601. {
  602. vtwm_pll_clk_init(node, PLL_TYPE_WM8650);
  603. }
  604. CLK_OF_DECLARE(wm8650_pll, "wm,wm8650-pll-clock", wm8650_pll_init);
  605. static void __init wm8750_pll_init(struct device_node *node)
  606. {
  607. vtwm_pll_clk_init(node, PLL_TYPE_WM8750);
  608. }
  609. CLK_OF_DECLARE(wm8750_pll, "wm,wm8750-pll-clock", wm8750_pll_init);
  610. static void __init wm8850_pll_init(struct device_node *node)
  611. {
  612. vtwm_pll_clk_init(node, PLL_TYPE_WM8850);
  613. }
  614. CLK_OF_DECLARE(wm8850_pll, "wm,wm8850-pll-clock", wm8850_pll_init);