clk-vt8500.c 17 KB

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