i2s_pll_clock.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Synopsys AXS10X SDP I2S PLL clock driver
  3. *
  4. * Copyright (C) 2016 Synopsys
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/platform_device.h>
  11. #include <linux/module.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/err.h>
  14. #include <linux/device.h>
  15. #include <linux/of_address.h>
  16. #include <linux/slab.h>
  17. #include <linux/of.h>
  18. /* PLL registers addresses */
  19. #define PLL_IDIV_REG 0x0
  20. #define PLL_FBDIV_REG 0x4
  21. #define PLL_ODIV0_REG 0x8
  22. #define PLL_ODIV1_REG 0xC
  23. struct i2s_pll_cfg {
  24. unsigned int rate;
  25. unsigned int idiv;
  26. unsigned int fbdiv;
  27. unsigned int odiv0;
  28. unsigned int odiv1;
  29. };
  30. static const struct i2s_pll_cfg i2s_pll_cfg_27m[] = {
  31. /* 27 Mhz */
  32. { 1024000, 0x104, 0x451, 0x10E38, 0x2000 },
  33. { 1411200, 0x104, 0x596, 0x10D35, 0x2000 },
  34. { 1536000, 0x208, 0xA28, 0x10B2C, 0x2000 },
  35. { 2048000, 0x82, 0x451, 0x10E38, 0x2000 },
  36. { 2822400, 0x82, 0x596, 0x10D35, 0x2000 },
  37. { 3072000, 0x104, 0xA28, 0x10B2C, 0x2000 },
  38. { 2116800, 0x82, 0x3CF, 0x10C30, 0x2000 },
  39. { 2304000, 0x104, 0x79E, 0x10B2C, 0x2000 },
  40. { 0, 0, 0, 0, 0 },
  41. };
  42. static const struct i2s_pll_cfg i2s_pll_cfg_28m[] = {
  43. /* 28.224 Mhz */
  44. { 1024000, 0x82, 0x105, 0x107DF, 0x2000 },
  45. { 1411200, 0x28A, 0x1, 0x10001, 0x2000 },
  46. { 1536000, 0xA28, 0x187, 0x10042, 0x2000 },
  47. { 2048000, 0x41, 0x105, 0x107DF, 0x2000 },
  48. { 2822400, 0x145, 0x1, 0x10001, 0x2000 },
  49. { 3072000, 0x514, 0x187, 0x10042, 0x2000 },
  50. { 2116800, 0x514, 0x42, 0x10001, 0x2000 },
  51. { 2304000, 0x619, 0x82, 0x10001, 0x2000 },
  52. { 0, 0, 0, 0, 0 },
  53. };
  54. struct i2s_pll_clk {
  55. void __iomem *base;
  56. struct clk_hw hw;
  57. struct device *dev;
  58. };
  59. static inline void i2s_pll_write(struct i2s_pll_clk *clk, unsigned int reg,
  60. unsigned int val)
  61. {
  62. writel_relaxed(val, clk->base + reg);
  63. }
  64. static inline unsigned int i2s_pll_read(struct i2s_pll_clk *clk,
  65. unsigned int reg)
  66. {
  67. return readl_relaxed(clk->base + reg);
  68. }
  69. static inline struct i2s_pll_clk *to_i2s_pll_clk(struct clk_hw *hw)
  70. {
  71. return container_of(hw, struct i2s_pll_clk, hw);
  72. }
  73. static inline unsigned int i2s_pll_get_value(unsigned int val)
  74. {
  75. return (val & 0x3F) + ((val >> 6) & 0x3F);
  76. }
  77. static const struct i2s_pll_cfg *i2s_pll_get_cfg(unsigned long prate)
  78. {
  79. switch (prate) {
  80. case 27000000:
  81. return i2s_pll_cfg_27m;
  82. case 28224000:
  83. return i2s_pll_cfg_28m;
  84. default:
  85. return NULL;
  86. }
  87. }
  88. static unsigned long i2s_pll_recalc_rate(struct clk_hw *hw,
  89. unsigned long parent_rate)
  90. {
  91. struct i2s_pll_clk *clk = to_i2s_pll_clk(hw);
  92. unsigned int idiv, fbdiv, odiv;
  93. idiv = i2s_pll_get_value(i2s_pll_read(clk, PLL_IDIV_REG));
  94. fbdiv = i2s_pll_get_value(i2s_pll_read(clk, PLL_FBDIV_REG));
  95. odiv = i2s_pll_get_value(i2s_pll_read(clk, PLL_ODIV0_REG));
  96. return ((parent_rate / idiv) * fbdiv) / odiv;
  97. }
  98. static long i2s_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  99. unsigned long *prate)
  100. {
  101. struct i2s_pll_clk *clk = to_i2s_pll_clk(hw);
  102. const struct i2s_pll_cfg *pll_cfg = i2s_pll_get_cfg(*prate);
  103. int i;
  104. if (!pll_cfg) {
  105. dev_err(clk->dev, "invalid parent rate=%ld\n", *prate);
  106. return -EINVAL;
  107. }
  108. for (i = 0; pll_cfg[i].rate != 0; i++)
  109. if (pll_cfg[i].rate == rate)
  110. return rate;
  111. return -EINVAL;
  112. }
  113. static int i2s_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  114. unsigned long parent_rate)
  115. {
  116. struct i2s_pll_clk *clk = to_i2s_pll_clk(hw);
  117. const struct i2s_pll_cfg *pll_cfg = i2s_pll_get_cfg(parent_rate);
  118. int i;
  119. if (!pll_cfg) {
  120. dev_err(clk->dev, "invalid parent rate=%ld\n", parent_rate);
  121. return -EINVAL;
  122. }
  123. for (i = 0; pll_cfg[i].rate != 0; i++) {
  124. if (pll_cfg[i].rate == rate) {
  125. i2s_pll_write(clk, PLL_IDIV_REG, pll_cfg[i].idiv);
  126. i2s_pll_write(clk, PLL_FBDIV_REG, pll_cfg[i].fbdiv);
  127. i2s_pll_write(clk, PLL_ODIV0_REG, pll_cfg[i].odiv0);
  128. i2s_pll_write(clk, PLL_ODIV1_REG, pll_cfg[i].odiv1);
  129. return 0;
  130. }
  131. }
  132. dev_err(clk->dev, "invalid rate=%ld, parent_rate=%ld\n", rate,
  133. parent_rate);
  134. return -EINVAL;
  135. }
  136. static const struct clk_ops i2s_pll_ops = {
  137. .recalc_rate = i2s_pll_recalc_rate,
  138. .round_rate = i2s_pll_round_rate,
  139. .set_rate = i2s_pll_set_rate,
  140. };
  141. static int i2s_pll_clk_probe(struct platform_device *pdev)
  142. {
  143. struct device *dev = &pdev->dev;
  144. struct device_node *node = dev->of_node;
  145. const char *clk_name;
  146. const char *parent_name;
  147. struct clk *clk;
  148. struct i2s_pll_clk *pll_clk;
  149. struct clk_init_data init;
  150. struct resource *mem;
  151. pll_clk = devm_kzalloc(dev, sizeof(*pll_clk), GFP_KERNEL);
  152. if (!pll_clk)
  153. return -ENOMEM;
  154. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  155. pll_clk->base = devm_ioremap_resource(dev, mem);
  156. if (IS_ERR(pll_clk->base))
  157. return PTR_ERR(pll_clk->base);
  158. memset(&init, 0, sizeof(init));
  159. clk_name = node->name;
  160. init.name = clk_name;
  161. init.ops = &i2s_pll_ops;
  162. parent_name = of_clk_get_parent_name(node, 0);
  163. init.parent_names = &parent_name;
  164. init.num_parents = 1;
  165. pll_clk->hw.init = &init;
  166. pll_clk->dev = dev;
  167. clk = devm_clk_register(dev, &pll_clk->hw);
  168. if (IS_ERR(clk)) {
  169. dev_err(dev, "failed to register %s clock (%ld)\n",
  170. clk_name, PTR_ERR(clk));
  171. return PTR_ERR(clk);
  172. }
  173. return of_clk_add_provider(node, of_clk_src_simple_get, clk);
  174. }
  175. static int i2s_pll_clk_remove(struct platform_device *pdev)
  176. {
  177. of_clk_del_provider(pdev->dev.of_node);
  178. return 0;
  179. }
  180. static const struct of_device_id i2s_pll_clk_id[] = {
  181. { .compatible = "snps,axs10x-i2s-pll-clock", },
  182. { },
  183. };
  184. MODULE_DEVICE_TABLE(of, i2s_pll_clk_id);
  185. static struct platform_driver i2s_pll_clk_driver = {
  186. .driver = {
  187. .name = "axs10x-i2s-pll-clock",
  188. .of_match_table = i2s_pll_clk_id,
  189. },
  190. .probe = i2s_pll_clk_probe,
  191. .remove = i2s_pll_clk_remove,
  192. };
  193. module_platform_driver(i2s_pll_clk_driver);
  194. MODULE_AUTHOR("Jose Abreu <joabreu@synopsys.com>");
  195. MODULE_DESCRIPTION("Synopsys AXS10X SDP I2S PLL Clock Driver");
  196. MODULE_LICENSE("GPL v2");