reset-meson-audio-arb.c 4.7 KB

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