clk-xlnx-clock-wizard.c 9.3 KB

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