clk-xlnx-clock-wizard.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Xilinx 'Clocking Wizard' driver
  4. *
  5. * Copyright (C) 2013 - 2014 Xilinx
  6. *
  7. * Sören Brinkmann <soren.brinkmann@xilinx.com>
  8. */
  9. #include <linux/platform_device.h>
  10. #include <linux/clk.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/slab.h>
  13. #include <linux/io.h>
  14. #include <linux/of.h>
  15. #include <linux/module.h>
  16. #include <linux/err.h>
  17. #define WZRD_NUM_OUTPUTS 7
  18. #define WZRD_ACLK_MAX_FREQ 250000000UL
  19. #define WZRD_CLK_CFG_REG(n) (0x200 + 4 * (n))
  20. #define WZRD_CLKOUT0_FRAC_EN BIT(18)
  21. #define WZRD_CLKFBOUT_FRAC_EN BIT(26)
  22. #define WZRD_CLKFBOUT_MULT_SHIFT 8
  23. #define WZRD_CLKFBOUT_MULT_MASK (0xff << WZRD_CLKFBOUT_MULT_SHIFT)
  24. #define WZRD_DIVCLK_DIVIDE_SHIFT 0
  25. #define WZRD_DIVCLK_DIVIDE_MASK (0xff << WZRD_DIVCLK_DIVIDE_SHIFT)
  26. #define WZRD_CLKOUT_DIVIDE_SHIFT 0
  27. #define WZRD_CLKOUT_DIVIDE_MASK (0xff << WZRD_DIVCLK_DIVIDE_SHIFT)
  28. enum clk_wzrd_int_clks {
  29. wzrd_clk_mul,
  30. wzrd_clk_mul_div,
  31. wzrd_clk_int_max
  32. };
  33. /**
  34. * struct clk_wzrd:
  35. * @clk_data: Clock data
  36. * @nb: Notifier block
  37. * @base: Memory base
  38. * @clk_in1: Handle to input clock 'clk_in1'
  39. * @axi_clk: Handle to input clock 's_axi_aclk'
  40. * @clks_internal: Internal clocks
  41. * @clkout: Output clocks
  42. * @speed_grade: Speed grade of the device
  43. * @suspended: Flag indicating power state of the device
  44. */
  45. struct clk_wzrd {
  46. struct clk_onecell_data clk_data;
  47. struct notifier_block nb;
  48. void __iomem *base;
  49. struct clk *clk_in1;
  50. struct clk *axi_clk;
  51. struct clk *clks_internal[wzrd_clk_int_max];
  52. struct clk *clkout[WZRD_NUM_OUTPUTS];
  53. unsigned int speed_grade;
  54. bool suspended;
  55. };
  56. #define to_clk_wzrd(_nb) container_of(_nb, struct clk_wzrd, nb)
  57. /* maximum frequencies for input/output clocks per speed grade */
  58. static const unsigned long clk_wzrd_max_freq[] = {
  59. 800000000UL,
  60. 933000000UL,
  61. 1066000000UL
  62. };
  63. static int clk_wzrd_clk_notifier(struct notifier_block *nb, unsigned long event,
  64. void *data)
  65. {
  66. unsigned long max;
  67. struct clk_notifier_data *ndata = data;
  68. struct clk_wzrd *clk_wzrd = to_clk_wzrd(nb);
  69. if (clk_wzrd->suspended)
  70. return NOTIFY_OK;
  71. if (ndata->clk == clk_wzrd->clk_in1)
  72. max = clk_wzrd_max_freq[clk_wzrd->speed_grade - 1];
  73. else if (ndata->clk == clk_wzrd->axi_clk)
  74. max = WZRD_ACLK_MAX_FREQ;
  75. else
  76. return NOTIFY_DONE; /* should never happen */
  77. switch (event) {
  78. case PRE_RATE_CHANGE:
  79. if (ndata->new_rate > max)
  80. return NOTIFY_BAD;
  81. return NOTIFY_OK;
  82. case POST_RATE_CHANGE:
  83. case ABORT_RATE_CHANGE:
  84. default:
  85. return NOTIFY_DONE;
  86. }
  87. }
  88. static int __maybe_unused clk_wzrd_suspend(struct device *dev)
  89. {
  90. struct clk_wzrd *clk_wzrd = dev_get_drvdata(dev);
  91. clk_disable_unprepare(clk_wzrd->axi_clk);
  92. clk_wzrd->suspended = true;
  93. return 0;
  94. }
  95. static int __maybe_unused clk_wzrd_resume(struct device *dev)
  96. {
  97. int ret;
  98. struct clk_wzrd *clk_wzrd = dev_get_drvdata(dev);
  99. ret = clk_prepare_enable(clk_wzrd->axi_clk);
  100. if (ret) {
  101. dev_err(dev, "unable to enable s_axi_aclk\n");
  102. return ret;
  103. }
  104. clk_wzrd->suspended = false;
  105. return 0;
  106. }
  107. static SIMPLE_DEV_PM_OPS(clk_wzrd_dev_pm_ops, clk_wzrd_suspend,
  108. clk_wzrd_resume);
  109. static int clk_wzrd_probe(struct platform_device *pdev)
  110. {
  111. int i, ret;
  112. u32 reg;
  113. unsigned long rate;
  114. const char *clk_name;
  115. struct clk_wzrd *clk_wzrd;
  116. struct resource *mem;
  117. struct device_node *np = pdev->dev.of_node;
  118. clk_wzrd = devm_kzalloc(&pdev->dev, sizeof(*clk_wzrd), GFP_KERNEL);
  119. if (!clk_wzrd)
  120. return -ENOMEM;
  121. platform_set_drvdata(pdev, clk_wzrd);
  122. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  123. clk_wzrd->base = devm_ioremap_resource(&pdev->dev, mem);
  124. if (IS_ERR(clk_wzrd->base))
  125. return PTR_ERR(clk_wzrd->base);
  126. ret = of_property_read_u32(np, "speed-grade", &clk_wzrd->speed_grade);
  127. if (!ret) {
  128. if (clk_wzrd->speed_grade < 1 || clk_wzrd->speed_grade > 3) {
  129. dev_warn(&pdev->dev, "invalid speed grade '%d'\n",
  130. clk_wzrd->speed_grade);
  131. clk_wzrd->speed_grade = 0;
  132. }
  133. }
  134. clk_wzrd->clk_in1 = devm_clk_get(&pdev->dev, "clk_in1");
  135. if (IS_ERR(clk_wzrd->clk_in1)) {
  136. if (clk_wzrd->clk_in1 != ERR_PTR(-EPROBE_DEFER))
  137. dev_err(&pdev->dev, "clk_in1 not found\n");
  138. return PTR_ERR(clk_wzrd->clk_in1);
  139. }
  140. clk_wzrd->axi_clk = devm_clk_get(&pdev->dev, "s_axi_aclk");
  141. if (IS_ERR(clk_wzrd->axi_clk)) {
  142. if (clk_wzrd->axi_clk != ERR_PTR(-EPROBE_DEFER))
  143. dev_err(&pdev->dev, "s_axi_aclk not found\n");
  144. return PTR_ERR(clk_wzrd->axi_clk);
  145. }
  146. ret = clk_prepare_enable(clk_wzrd->axi_clk);
  147. if (ret) {
  148. dev_err(&pdev->dev, "enabling s_axi_aclk failed\n");
  149. return ret;
  150. }
  151. rate = clk_get_rate(clk_wzrd->axi_clk);
  152. if (rate > WZRD_ACLK_MAX_FREQ) {
  153. dev_err(&pdev->dev, "s_axi_aclk frequency (%lu) too high\n",
  154. rate);
  155. ret = -EINVAL;
  156. goto err_disable_clk;
  157. }
  158. /* we don't support fractional div/mul yet */
  159. reg = readl(clk_wzrd->base + WZRD_CLK_CFG_REG(0)) &
  160. WZRD_CLKFBOUT_FRAC_EN;
  161. reg |= readl(clk_wzrd->base + WZRD_CLK_CFG_REG(2)) &
  162. WZRD_CLKOUT0_FRAC_EN;
  163. if (reg)
  164. dev_warn(&pdev->dev, "fractional div/mul not supported\n");
  165. /* register multiplier */
  166. reg = (readl(clk_wzrd->base + WZRD_CLK_CFG_REG(0)) &
  167. WZRD_CLKFBOUT_MULT_MASK) >> WZRD_CLKFBOUT_MULT_SHIFT;
  168. clk_name = kasprintf(GFP_KERNEL, "%s_mul", dev_name(&pdev->dev));
  169. if (!clk_name) {
  170. ret = -ENOMEM;
  171. goto err_disable_clk;
  172. }
  173. clk_wzrd->clks_internal[wzrd_clk_mul] = clk_register_fixed_factor(
  174. &pdev->dev, clk_name,
  175. __clk_get_name(clk_wzrd->clk_in1),
  176. 0, reg, 1);
  177. kfree(clk_name);
  178. if (IS_ERR(clk_wzrd->clks_internal[wzrd_clk_mul])) {
  179. dev_err(&pdev->dev, "unable to register fixed-factor clock\n");
  180. ret = PTR_ERR(clk_wzrd->clks_internal[wzrd_clk_mul]);
  181. goto err_disable_clk;
  182. }
  183. /* register div */
  184. reg = (readl(clk_wzrd->base + WZRD_CLK_CFG_REG(0)) &
  185. WZRD_DIVCLK_DIVIDE_MASK) >> WZRD_DIVCLK_DIVIDE_SHIFT;
  186. clk_name = kasprintf(GFP_KERNEL, "%s_mul_div", dev_name(&pdev->dev));
  187. if (!clk_name) {
  188. ret = -ENOMEM;
  189. goto err_rm_int_clk;
  190. }
  191. clk_wzrd->clks_internal[wzrd_clk_mul_div] = clk_register_fixed_factor(
  192. &pdev->dev, clk_name,
  193. __clk_get_name(clk_wzrd->clks_internal[wzrd_clk_mul]),
  194. 0, 1, reg);
  195. if (IS_ERR(clk_wzrd->clks_internal[wzrd_clk_mul_div])) {
  196. dev_err(&pdev->dev, "unable to register divider clock\n");
  197. ret = PTR_ERR(clk_wzrd->clks_internal[wzrd_clk_mul_div]);
  198. goto err_rm_int_clk;
  199. }
  200. /* register div per output */
  201. for (i = WZRD_NUM_OUTPUTS - 1; i >= 0 ; i--) {
  202. const char *clkout_name;
  203. if (of_property_read_string_index(np, "clock-output-names", i,
  204. &clkout_name)) {
  205. dev_err(&pdev->dev,
  206. "clock output name not specified\n");
  207. ret = -EINVAL;
  208. goto err_rm_int_clks;
  209. }
  210. reg = readl(clk_wzrd->base + WZRD_CLK_CFG_REG(2) + i * 12);
  211. reg &= WZRD_CLKOUT_DIVIDE_MASK;
  212. reg >>= WZRD_CLKOUT_DIVIDE_SHIFT;
  213. clk_wzrd->clkout[i] = clk_register_fixed_factor(&pdev->dev,
  214. clkout_name, clk_name, 0, 1, reg);
  215. if (IS_ERR(clk_wzrd->clkout[i])) {
  216. int j;
  217. for (j = i + 1; j < WZRD_NUM_OUTPUTS; j++)
  218. clk_unregister(clk_wzrd->clkout[j]);
  219. dev_err(&pdev->dev,
  220. "unable to register divider clock\n");
  221. ret = PTR_ERR(clk_wzrd->clkout[i]);
  222. goto err_rm_int_clks;
  223. }
  224. }
  225. kfree(clk_name);
  226. clk_wzrd->clk_data.clks = clk_wzrd->clkout;
  227. clk_wzrd->clk_data.clk_num = ARRAY_SIZE(clk_wzrd->clkout);
  228. of_clk_add_provider(np, of_clk_src_onecell_get, &clk_wzrd->clk_data);
  229. if (clk_wzrd->speed_grade) {
  230. clk_wzrd->nb.notifier_call = clk_wzrd_clk_notifier;
  231. ret = clk_notifier_register(clk_wzrd->clk_in1,
  232. &clk_wzrd->nb);
  233. if (ret)
  234. dev_warn(&pdev->dev,
  235. "unable to register clock notifier\n");
  236. ret = clk_notifier_register(clk_wzrd->axi_clk, &clk_wzrd->nb);
  237. if (ret)
  238. dev_warn(&pdev->dev,
  239. "unable to register clock notifier\n");
  240. }
  241. return 0;
  242. err_rm_int_clks:
  243. clk_unregister(clk_wzrd->clks_internal[1]);
  244. err_rm_int_clk:
  245. kfree(clk_name);
  246. clk_unregister(clk_wzrd->clks_internal[0]);
  247. err_disable_clk:
  248. clk_disable_unprepare(clk_wzrd->axi_clk);
  249. return ret;
  250. }
  251. static int clk_wzrd_remove(struct platform_device *pdev)
  252. {
  253. int i;
  254. struct clk_wzrd *clk_wzrd = platform_get_drvdata(pdev);
  255. of_clk_del_provider(pdev->dev.of_node);
  256. for (i = 0; i < WZRD_NUM_OUTPUTS; i++)
  257. clk_unregister(clk_wzrd->clkout[i]);
  258. for (i = 0; i < wzrd_clk_int_max; i++)
  259. clk_unregister(clk_wzrd->clks_internal[i]);
  260. if (clk_wzrd->speed_grade) {
  261. clk_notifier_unregister(clk_wzrd->axi_clk, &clk_wzrd->nb);
  262. clk_notifier_unregister(clk_wzrd->clk_in1, &clk_wzrd->nb);
  263. }
  264. clk_disable_unprepare(clk_wzrd->axi_clk);
  265. return 0;
  266. }
  267. static const struct of_device_id clk_wzrd_ids[] = {
  268. { .compatible = "xlnx,clocking-wizard" },
  269. { },
  270. };
  271. MODULE_DEVICE_TABLE(of, clk_wzrd_ids);
  272. static struct platform_driver clk_wzrd_driver = {
  273. .driver = {
  274. .name = "clk-wizard",
  275. .of_match_table = clk_wzrd_ids,
  276. .pm = &clk_wzrd_dev_pm_ops,
  277. },
  278. .probe = clk_wzrd_probe,
  279. .remove = clk_wzrd_remove,
  280. };
  281. module_platform_driver(clk_wzrd_driver);
  282. MODULE_LICENSE("GPL");
  283. MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com");
  284. MODULE_DESCRIPTION("Driver for the Xilinx Clocking Wizard IP core");