armada-37xx-tbg.c 3.8 KB

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