clk-vt8500.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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 void 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;
  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. }
  304. static void wm8650_find_pll_bits(unsigned long rate, unsigned long parent_rate,
  305. u32 *multiplier, u32 *divisor1, u32 *divisor2)
  306. {
  307. u32 mul, div1, div2;
  308. u32 best_mul, best_div1, best_div2;
  309. unsigned long tclk, rate_err, best_err;
  310. best_err = (unsigned long)-1;
  311. /* Find the closest match (lower or equal to requested) */
  312. for (div1 = 5; div1 >= 3; div1--)
  313. for (div2 = 3; div2 >= 0; div2--)
  314. for (mul = 3; mul <= 1023; mul++) {
  315. tclk = parent_rate * mul / (div1 * (1 << div2));
  316. if (tclk > rate)
  317. continue;
  318. /* error will always be +ve */
  319. rate_err = rate - tclk;
  320. if (rate_err == 0) {
  321. *multiplier = mul;
  322. *divisor1 = div1;
  323. *divisor2 = div2;
  324. return;
  325. }
  326. if (rate_err < best_err) {
  327. best_err = rate_err;
  328. best_mul = mul;
  329. best_div1 = div1;
  330. best_div2 = div2;
  331. }
  332. }
  333. /* if we got here, it wasn't an exact match */
  334. pr_warn("%s: requested rate %lu, found rate %lu\n", __func__, rate,
  335. rate - best_err);
  336. *multiplier = best_mul;
  337. *divisor1 = best_div1;
  338. *divisor2 = best_div2;
  339. }
  340. static u32 wm8750_get_filter(u32 parent_rate, u32 divisor1)
  341. {
  342. /* calculate frequency (MHz) after pre-divisor */
  343. u32 freq = (parent_rate / 1000000) / (divisor1 + 1);
  344. if ((freq < 10) || (freq > 200))
  345. pr_warn("%s: PLL recommended input frequency 10..200Mhz (requested %d Mhz)\n",
  346. __func__, freq);
  347. if (freq >= 166)
  348. return 7;
  349. else if (freq >= 104)
  350. return 6;
  351. else if (freq >= 65)
  352. return 5;
  353. else if (freq >= 42)
  354. return 4;
  355. else if (freq >= 26)
  356. return 3;
  357. else if (freq >= 16)
  358. return 2;
  359. else if (freq >= 10)
  360. return 1;
  361. return 0;
  362. }
  363. static void wm8750_find_pll_bits(unsigned long rate, unsigned long parent_rate,
  364. u32 *filter, u32 *multiplier, u32 *divisor1, u32 *divisor2)
  365. {
  366. u32 mul, div1, div2;
  367. u32 best_mul, best_div1, best_div2;
  368. unsigned long tclk, rate_err, best_err;
  369. best_err = (unsigned long)-1;
  370. /* Find the closest match (lower or equal to requested) */
  371. for (div1 = 1; div1 >= 0; div1--)
  372. for (div2 = 7; div2 >= 0; div2--)
  373. for (mul = 0; mul <= 255; mul++) {
  374. tclk = parent_rate * (mul + 1) / ((div1 + 1) * (1 << div2));
  375. if (tclk > rate)
  376. continue;
  377. /* error will always be +ve */
  378. rate_err = rate - tclk;
  379. if (rate_err == 0) {
  380. *filter = wm8750_get_filter(parent_rate, div1);
  381. *multiplier = mul;
  382. *divisor1 = div1;
  383. *divisor2 = div2;
  384. return;
  385. }
  386. if (rate_err < best_err) {
  387. best_err = rate_err;
  388. best_mul = mul;
  389. best_div1 = div1;
  390. best_div2 = div2;
  391. }
  392. }
  393. /* if we got here, it wasn't an exact match */
  394. pr_warn("%s: requested rate %lu, found rate %lu\n", __func__, rate,
  395. rate - best_err);
  396. *filter = wm8750_get_filter(parent_rate, best_div1);
  397. *multiplier = best_mul;
  398. *divisor1 = best_div1;
  399. *divisor2 = best_div2;
  400. }
  401. static void wm8850_find_pll_bits(unsigned long rate, unsigned long parent_rate,
  402. u32 *multiplier, u32 *divisor1, u32 *divisor2)
  403. {
  404. u32 mul, div1, div2;
  405. u32 best_mul, best_div1, best_div2;
  406. unsigned long tclk, rate_err, best_err;
  407. best_err = (unsigned long)-1;
  408. /* Find the closest match (lower or equal to requested) */
  409. for (div1 = 1; div1 >= 0; div1--)
  410. for (div2 = 3; div2 >= 0; div2--)
  411. for (mul = 0; mul <= 127; mul++) {
  412. tclk = parent_rate * ((mul + 1) * 2) /
  413. ((div1 + 1) * (1 << div2));
  414. if (tclk > rate)
  415. continue;
  416. /* error will always be +ve */
  417. rate_err = rate - tclk;
  418. if (rate_err == 0) {
  419. *multiplier = mul;
  420. *divisor1 = div1;
  421. *divisor2 = div2;
  422. return;
  423. }
  424. if (rate_err < best_err) {
  425. best_err = rate_err;
  426. best_mul = mul;
  427. best_div1 = div1;
  428. best_div2 = div2;
  429. }
  430. }
  431. /* if we got here, it wasn't an exact match */
  432. pr_warn("%s: requested rate %lu, found rate %lu\n", __func__, rate,
  433. rate - best_err);
  434. *multiplier = best_mul;
  435. *divisor1 = best_div1;
  436. *divisor2 = best_div2;
  437. }
  438. static int vtwm_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  439. unsigned long parent_rate)
  440. {
  441. struct clk_pll *pll = to_clk_pll(hw);
  442. u32 filter, mul, div1, div2;
  443. u32 pll_val;
  444. unsigned long flags = 0;
  445. /* sanity check */
  446. switch (pll->type) {
  447. case PLL_TYPE_VT8500:
  448. vt8500_find_pll_bits(rate, parent_rate, &mul, &div1);
  449. pll_val = VT8500_BITS_TO_VAL(mul, div1);
  450. break;
  451. case PLL_TYPE_WM8650:
  452. wm8650_find_pll_bits(rate, parent_rate, &mul, &div1, &div2);
  453. pll_val = WM8650_BITS_TO_VAL(mul, div1, div2);
  454. break;
  455. case PLL_TYPE_WM8750:
  456. wm8750_find_pll_bits(rate, parent_rate, &filter, &mul, &div1, &div2);
  457. pll_val = WM8750_BITS_TO_VAL(filter, mul, div1, div2);
  458. break;
  459. case PLL_TYPE_WM8850:
  460. wm8850_find_pll_bits(rate, parent_rate, &mul, &div1, &div2);
  461. pll_val = WM8850_BITS_TO_VAL(mul, div1, div2);
  462. break;
  463. default:
  464. pr_err("%s: invalid pll type\n", __func__);
  465. return 0;
  466. }
  467. spin_lock_irqsave(pll->lock, flags);
  468. vt8500_pmc_wait_busy();
  469. writel(pll_val, pll->reg);
  470. vt8500_pmc_wait_busy();
  471. spin_unlock_irqrestore(pll->lock, flags);
  472. return 0;
  473. }
  474. static long vtwm_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  475. unsigned long *prate)
  476. {
  477. struct clk_pll *pll = to_clk_pll(hw);
  478. u32 filter, mul, div1, div2;
  479. long round_rate;
  480. switch (pll->type) {
  481. case PLL_TYPE_VT8500:
  482. vt8500_find_pll_bits(rate, *prate, &mul, &div1);
  483. round_rate = VT8500_BITS_TO_FREQ(*prate, mul, div1);
  484. break;
  485. case PLL_TYPE_WM8650:
  486. wm8650_find_pll_bits(rate, *prate, &mul, &div1, &div2);
  487. round_rate = WM8650_BITS_TO_FREQ(*prate, mul, div1, div2);
  488. break;
  489. case PLL_TYPE_WM8750:
  490. wm8750_find_pll_bits(rate, *prate, &filter, &mul, &div1, &div2);
  491. round_rate = WM8750_BITS_TO_FREQ(*prate, mul, div1, div2);
  492. break;
  493. case PLL_TYPE_WM8850:
  494. wm8850_find_pll_bits(rate, *prate, &mul, &div1, &div2);
  495. round_rate = WM8850_BITS_TO_FREQ(*prate, mul, div1, div2);
  496. break;
  497. default:
  498. round_rate = 0;
  499. }
  500. return round_rate;
  501. }
  502. static unsigned long vtwm_pll_recalc_rate(struct clk_hw *hw,
  503. unsigned long parent_rate)
  504. {
  505. struct clk_pll *pll = to_clk_pll(hw);
  506. u32 pll_val = readl(pll->reg);
  507. unsigned long pll_freq;
  508. switch (pll->type) {
  509. case PLL_TYPE_VT8500:
  510. pll_freq = parent_rate * VT8500_PLL_MUL(pll_val);
  511. pll_freq /= VT8500_PLL_DIV(pll_val);
  512. break;
  513. case PLL_TYPE_WM8650:
  514. pll_freq = parent_rate * WM8650_PLL_MUL(pll_val);
  515. pll_freq /= WM8650_PLL_DIV(pll_val);
  516. break;
  517. case PLL_TYPE_WM8750:
  518. pll_freq = parent_rate * WM8750_PLL_MUL(pll_val);
  519. pll_freq /= WM8750_PLL_DIV(pll_val);
  520. break;
  521. case PLL_TYPE_WM8850:
  522. pll_freq = parent_rate * WM8850_PLL_MUL(pll_val);
  523. pll_freq /= WM8850_PLL_DIV(pll_val);
  524. break;
  525. default:
  526. pll_freq = 0;
  527. }
  528. return pll_freq;
  529. }
  530. static const struct clk_ops vtwm_pll_ops = {
  531. .round_rate = vtwm_pll_round_rate,
  532. .set_rate = vtwm_pll_set_rate,
  533. .recalc_rate = vtwm_pll_recalc_rate,
  534. };
  535. static __init void vtwm_pll_clk_init(struct device_node *node, int pll_type)
  536. {
  537. u32 reg;
  538. struct clk *clk;
  539. struct clk_pll *pll_clk;
  540. const char *clk_name = node->name;
  541. const char *parent_name;
  542. struct clk_init_data init;
  543. int rc;
  544. if (!pmc_base)
  545. vtwm_set_pmc_base();
  546. rc = of_property_read_u32(node, "reg", &reg);
  547. if (WARN_ON(rc))
  548. return;
  549. pll_clk = kzalloc(sizeof(*pll_clk), GFP_KERNEL);
  550. if (WARN_ON(!pll_clk))
  551. return;
  552. pll_clk->reg = pmc_base + reg;
  553. pll_clk->lock = &_lock;
  554. pll_clk->type = pll_type;
  555. of_property_read_string(node, "clock-output-names", &clk_name);
  556. init.name = clk_name;
  557. init.ops = &vtwm_pll_ops;
  558. init.flags = 0;
  559. parent_name = of_clk_get_parent_name(node, 0);
  560. init.parent_names = &parent_name;
  561. init.num_parents = 1;
  562. pll_clk->hw.init = &init;
  563. clk = clk_register(NULL, &pll_clk->hw);
  564. if (WARN_ON(IS_ERR(clk))) {
  565. kfree(pll_clk);
  566. return;
  567. }
  568. rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  569. clk_register_clkdev(clk, clk_name, NULL);
  570. }
  571. /* Wrappers for initialization functions */
  572. static void __init vt8500_pll_init(struct device_node *node)
  573. {
  574. vtwm_pll_clk_init(node, PLL_TYPE_VT8500);
  575. }
  576. CLK_OF_DECLARE(vt8500_pll, "via,vt8500-pll-clock", vt8500_pll_init);
  577. static void __init wm8650_pll_init(struct device_node *node)
  578. {
  579. vtwm_pll_clk_init(node, PLL_TYPE_WM8650);
  580. }
  581. CLK_OF_DECLARE(wm8650_pll, "wm,wm8650-pll-clock", wm8650_pll_init);
  582. static void __init wm8750_pll_init(struct device_node *node)
  583. {
  584. vtwm_pll_clk_init(node, PLL_TYPE_WM8750);
  585. }
  586. CLK_OF_DECLARE(wm8750_pll, "wm,wm8750-pll-clock", wm8750_pll_init);
  587. static void __init wm8850_pll_init(struct device_node *node)
  588. {
  589. vtwm_pll_clk_init(node, PLL_TYPE_WM8850);
  590. }
  591. CLK_OF_DECLARE(wm8850_pll, "wm,wm8850-pll-clock", wm8850_pll_init);