clk-vt8500.c 17 KB

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