i2s_pll_clock.c 5.6 KB

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