reset-meson-audio-arb.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. // Copyright (c) 2018 BayLibre, SAS.
  3. // Author: Jerome Brunet <jbrunet@baylibre.com>
  4. #include <linux/clk.h>
  5. #include <linux/io.h>
  6. #include <linux/module.h>
  7. #include <linux/of_platform.h>
  8. #include <linux/reset-controller.h>
  9. #include <linux/spinlock.h>
  10. #include <dt-bindings/reset/amlogic,meson-axg-audio-arb.h>
  11. struct meson_audio_arb_data {
  12. struct reset_controller_dev rstc;
  13. void __iomem *regs;
  14. struct clk *clk;
  15. const unsigned int *reset_bits;
  16. spinlock_t lock;
  17. };
  18. #define ARB_GENERAL_BIT 31
  19. static const unsigned int axg_audio_arb_reset_bits[] = {
  20. [AXG_ARB_TODDR_A] = 0,
  21. [AXG_ARB_TODDR_B] = 1,
  22. [AXG_ARB_TODDR_C] = 2,
  23. [AXG_ARB_FRDDR_A] = 4,
  24. [AXG_ARB_FRDDR_B] = 5,
  25. [AXG_ARB_FRDDR_C] = 6,
  26. };
  27. static int meson_audio_arb_update(struct reset_controller_dev *rcdev,
  28. unsigned long id, bool assert)
  29. {
  30. u32 val;
  31. struct meson_audio_arb_data *arb =
  32. container_of(rcdev, struct meson_audio_arb_data, rstc);
  33. spin_lock(&arb->lock);
  34. val = readl(arb->regs);
  35. if (assert)
  36. val &= ~BIT(arb->reset_bits[id]);
  37. else
  38. val |= BIT(arb->reset_bits[id]);
  39. writel(val, arb->regs);
  40. spin_unlock(&arb->lock);
  41. return 0;
  42. }
  43. static int meson_audio_arb_status(struct reset_controller_dev *rcdev,
  44. unsigned long id)
  45. {
  46. u32 val;
  47. struct meson_audio_arb_data *arb =
  48. container_of(rcdev, struct meson_audio_arb_data, rstc);
  49. val = readl(arb->regs);
  50. return !(val & BIT(arb->reset_bits[id]));
  51. }
  52. static int meson_audio_arb_assert(struct reset_controller_dev *rcdev,
  53. unsigned long id)
  54. {
  55. return meson_audio_arb_update(rcdev, id, true);
  56. }
  57. static int meson_audio_arb_deassert(struct reset_controller_dev *rcdev,
  58. unsigned long id)
  59. {
  60. return meson_audio_arb_update(rcdev, id, false);
  61. }
  62. static const struct reset_control_ops meson_audio_arb_rstc_ops = {
  63. .assert = meson_audio_arb_assert,
  64. .deassert = meson_audio_arb_deassert,
  65. .status = meson_audio_arb_status,
  66. };
  67. static const struct of_device_id meson_audio_arb_of_match[] = {
  68. { .compatible = "amlogic,meson-axg-audio-arb", },
  69. {}
  70. };
  71. MODULE_DEVICE_TABLE(of, meson_audio_arb_of_match);
  72. static int meson_audio_arb_remove(struct platform_device *pdev)
  73. {
  74. struct meson_audio_arb_data *arb = platform_get_drvdata(pdev);
  75. /* Disable all access */
  76. spin_lock(&arb->lock);
  77. writel(0, arb->regs);
  78. spin_unlock(&arb->lock);
  79. clk_disable_unprepare(arb->clk);
  80. return 0;
  81. }
  82. static int meson_audio_arb_probe(struct platform_device *pdev)
  83. {
  84. struct device *dev = &pdev->dev;
  85. struct meson_audio_arb_data *arb;
  86. struct resource *res;
  87. int ret;
  88. arb = devm_kzalloc(dev, sizeof(*arb), GFP_KERNEL);
  89. if (!arb)
  90. return -ENOMEM;
  91. platform_set_drvdata(pdev, arb);
  92. arb->clk = devm_clk_get(dev, NULL);
  93. if (IS_ERR(arb->clk)) {
  94. if (PTR_ERR(arb->clk) != -EPROBE_DEFER)
  95. dev_err(dev, "failed to get clock\n");
  96. return PTR_ERR(arb->clk);
  97. }
  98. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  99. arb->regs = devm_ioremap_resource(dev, res);
  100. if (IS_ERR(arb->regs))
  101. return PTR_ERR(arb->regs);
  102. spin_lock_init(&arb->lock);
  103. arb->reset_bits = axg_audio_arb_reset_bits;
  104. arb->rstc.nr_resets = ARRAY_SIZE(axg_audio_arb_reset_bits);
  105. arb->rstc.ops = &meson_audio_arb_rstc_ops;
  106. arb->rstc.of_node = dev->of_node;
  107. arb->rstc.owner = THIS_MODULE;
  108. /*
  109. * Enable general :
  110. * In the initial state, all memory interfaces are disabled
  111. * and the general bit is on
  112. */
  113. ret = clk_prepare_enable(arb->clk);
  114. if (ret) {
  115. dev_err(dev, "failed to enable arb clock\n");
  116. return ret;
  117. }
  118. writel(BIT(ARB_GENERAL_BIT), arb->regs);
  119. /* Register reset controller */
  120. ret = devm_reset_controller_register(dev, &arb->rstc);
  121. if (ret) {
  122. dev_err(dev, "failed to register arb reset controller\n");
  123. meson_audio_arb_remove(pdev);
  124. }
  125. return ret;
  126. }
  127. static struct platform_driver meson_audio_arb_pdrv = {
  128. .probe = meson_audio_arb_probe,
  129. .remove = meson_audio_arb_remove,
  130. .driver = {
  131. .name = "meson-audio-arb-reset",
  132. .of_match_table = meson_audio_arb_of_match,
  133. },
  134. };
  135. module_platform_driver(meson_audio_arb_pdrv);
  136. MODULE_DESCRIPTION("Amlogic A113 Audio Memory Arbiter");
  137. MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
  138. MODULE_LICENSE("GPL v2");