armada-37xx-tbg.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Marvell Armada 37xx SoC Time Base Generator clocks
  4. *
  5. * Copyright (C) 2016 Marvell
  6. *
  7. * Gregory CLEMENT <gregory.clement@free-electrons.com>
  8. */
  9. #include <linux/clk-provider.h>
  10. #include <linux/clk.h>
  11. #include <linux/io.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #define NUM_TBG 4
  17. #define TBG_CTRL0 0x4
  18. #define TBG_CTRL1 0x8
  19. #define TBG_CTRL7 0x20
  20. #define TBG_CTRL8 0x30
  21. #define TBG_DIV_MASK 0x1FF
  22. #define TBG_A_REFDIV 0
  23. #define TBG_B_REFDIV 16
  24. #define TBG_A_FBDIV 2
  25. #define TBG_B_FBDIV 18
  26. #define TBG_A_VCODIV_SE 0
  27. #define TBG_B_VCODIV_SE 16
  28. #define TBG_A_VCODIV_DIFF 1
  29. #define TBG_B_VCODIV_DIFF 17
  30. struct tbg_def {
  31. char *name;
  32. u32 refdiv_offset;
  33. u32 fbdiv_offset;
  34. u32 vcodiv_reg;
  35. u32 vcodiv_offset;
  36. };
  37. static const struct tbg_def tbg[NUM_TBG] = {
  38. {"TBG-A-P", TBG_A_REFDIV, TBG_A_FBDIV, TBG_CTRL8, TBG_A_VCODIV_DIFF},
  39. {"TBG-B-P", TBG_B_REFDIV, TBG_B_FBDIV, TBG_CTRL8, TBG_B_VCODIV_DIFF},
  40. {"TBG-A-S", TBG_A_REFDIV, TBG_A_FBDIV, TBG_CTRL1, TBG_A_VCODIV_SE},
  41. {"TBG-B-S", TBG_B_REFDIV, TBG_B_FBDIV, TBG_CTRL1, TBG_B_VCODIV_SE},
  42. };
  43. static unsigned int tbg_get_mult(void __iomem *reg, const struct tbg_def *ptbg)
  44. {
  45. u32 val;
  46. val = readl(reg + TBG_CTRL0);
  47. return ((val >> ptbg->fbdiv_offset) & TBG_DIV_MASK) << 2;
  48. }
  49. static unsigned int tbg_get_div(void __iomem *reg, const struct tbg_def *ptbg)
  50. {
  51. u32 val;
  52. unsigned int div;
  53. val = readl(reg + TBG_CTRL7);
  54. div = (val >> ptbg->refdiv_offset) & TBG_DIV_MASK;
  55. if (div == 0)
  56. div = 1;
  57. val = readl(reg + ptbg->vcodiv_reg);
  58. div *= 1 << ((val >> ptbg->vcodiv_offset) & TBG_DIV_MASK);
  59. return div;
  60. }
  61. static int armada_3700_tbg_clock_probe(struct platform_device *pdev)
  62. {
  63. struct device_node *np = pdev->dev.of_node;
  64. struct clk_hw_onecell_data *hw_tbg_data;
  65. struct device *dev = &pdev->dev;
  66. const char *parent_name;
  67. struct resource *res;
  68. struct clk *parent;
  69. void __iomem *reg;
  70. int i, ret;
  71. hw_tbg_data = devm_kzalloc(&pdev->dev,
  72. struct_size(hw_tbg_data, hws, NUM_TBG),
  73. GFP_KERNEL);
  74. if (!hw_tbg_data)
  75. return -ENOMEM;
  76. hw_tbg_data->num = NUM_TBG;
  77. platform_set_drvdata(pdev, hw_tbg_data);
  78. parent = clk_get(dev, NULL);
  79. if (IS_ERR(parent)) {
  80. dev_err(dev, "Could get the clock parent\n");
  81. return -EINVAL;
  82. }
  83. parent_name = __clk_get_name(parent);
  84. clk_put(parent);
  85. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  86. reg = devm_ioremap_resource(dev, res);
  87. if (IS_ERR(reg))
  88. return PTR_ERR(reg);
  89. for (i = 0; i < NUM_TBG; i++) {
  90. const char *name;
  91. unsigned int mult, div;
  92. name = tbg[i].name;
  93. mult = tbg_get_mult(reg, &tbg[i]);
  94. div = tbg_get_div(reg, &tbg[i]);
  95. hw_tbg_data->hws[i] = clk_hw_register_fixed_factor(NULL, name,
  96. parent_name, 0, mult, div);
  97. if (IS_ERR(hw_tbg_data->hws[i]))
  98. dev_err(dev, "Can't register TBG clock %s\n", name);
  99. }
  100. ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, hw_tbg_data);
  101. return ret;
  102. }
  103. static int armada_3700_tbg_clock_remove(struct platform_device *pdev)
  104. {
  105. int i;
  106. struct clk_hw_onecell_data *hw_tbg_data = platform_get_drvdata(pdev);
  107. of_clk_del_provider(pdev->dev.of_node);
  108. for (i = 0; i < hw_tbg_data->num; i++)
  109. clk_hw_unregister_fixed_factor(hw_tbg_data->hws[i]);
  110. return 0;
  111. }
  112. static const struct of_device_id armada_3700_tbg_clock_of_match[] = {
  113. { .compatible = "marvell,armada-3700-tbg-clock", },
  114. { }
  115. };
  116. static struct platform_driver armada_3700_tbg_clock_driver = {
  117. .probe = armada_3700_tbg_clock_probe,
  118. .remove = armada_3700_tbg_clock_remove,
  119. .driver = {
  120. .name = "marvell-armada-3700-tbg-clock",
  121. .of_match_table = armada_3700_tbg_clock_of_match,
  122. },
  123. };
  124. builtin_platform_driver(armada_3700_tbg_clock_driver);