stm32_sai.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * STM32 ALSA SoC Digital Audio Interface (SAI) driver.
  3. *
  4. * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
  5. * Author(s): Olivier Moysan <olivier.moysan@st.com> for STMicroelectronics.
  6. *
  7. * License terms: GPL V2.0.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  16. * details.
  17. */
  18. #include <linux/bitfield.h>
  19. #include <linux/clk.h>
  20. #include <linux/delay.h>
  21. #include <linux/module.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/reset.h>
  24. #include <sound/dmaengine_pcm.h>
  25. #include <sound/core.h>
  26. #include "stm32_sai.h"
  27. static const struct stm32_sai_conf stm32_sai_conf_f4 = {
  28. .version = SAI_STM32F4,
  29. .has_spdif = false,
  30. };
  31. static const struct stm32_sai_conf stm32_sai_conf_h7 = {
  32. .version = SAI_STM32H7,
  33. .has_spdif = true,
  34. };
  35. static const struct of_device_id stm32_sai_ids[] = {
  36. { .compatible = "st,stm32f4-sai", .data = (void *)&stm32_sai_conf_f4 },
  37. { .compatible = "st,stm32h7-sai", .data = (void *)&stm32_sai_conf_h7 },
  38. {}
  39. };
  40. static int stm32_sai_sync_conf_client(struct stm32_sai_data *sai, int synci)
  41. {
  42. int ret;
  43. /* Enable peripheral clock to allow GCR register access */
  44. ret = clk_prepare_enable(sai->pclk);
  45. if (ret) {
  46. dev_err(&sai->pdev->dev, "failed to enable clock: %d\n", ret);
  47. return ret;
  48. }
  49. writel_relaxed(FIELD_PREP(SAI_GCR_SYNCIN_MASK, (synci - 1)), sai->base);
  50. clk_disable_unprepare(sai->pclk);
  51. return 0;
  52. }
  53. static int stm32_sai_sync_conf_provider(struct stm32_sai_data *sai, int synco)
  54. {
  55. u32 prev_synco;
  56. int ret;
  57. /* Enable peripheral clock to allow GCR register access */
  58. ret = clk_prepare_enable(sai->pclk);
  59. if (ret) {
  60. dev_err(&sai->pdev->dev, "failed to enable clock: %d\n", ret);
  61. return ret;
  62. }
  63. dev_dbg(&sai->pdev->dev, "Set %s%s as synchro provider\n",
  64. sai->pdev->dev.of_node->name,
  65. synco == STM_SAI_SYNC_OUT_A ? "A" : "B");
  66. prev_synco = FIELD_GET(SAI_GCR_SYNCOUT_MASK, readl_relaxed(sai->base));
  67. if (prev_synco != STM_SAI_SYNC_OUT_NONE && synco != prev_synco) {
  68. dev_err(&sai->pdev->dev, "%s%s already set as sync provider\n",
  69. sai->pdev->dev.of_node->name,
  70. prev_synco == STM_SAI_SYNC_OUT_A ? "A" : "B");
  71. clk_disable_unprepare(sai->pclk);
  72. return -EINVAL;
  73. }
  74. writel_relaxed(FIELD_PREP(SAI_GCR_SYNCOUT_MASK, synco), sai->base);
  75. clk_disable_unprepare(sai->pclk);
  76. return 0;
  77. }
  78. static int stm32_sai_set_sync(struct stm32_sai_data *sai_client,
  79. struct device_node *np_provider,
  80. int synco, int synci)
  81. {
  82. struct platform_device *pdev = of_find_device_by_node(np_provider);
  83. struct stm32_sai_data *sai_provider;
  84. int ret;
  85. if (!pdev) {
  86. dev_err(&sai_client->pdev->dev,
  87. "Device not found for node %pOFn\n", np_provider);
  88. return -ENODEV;
  89. }
  90. sai_provider = platform_get_drvdata(pdev);
  91. if (!sai_provider) {
  92. dev_err(&sai_client->pdev->dev,
  93. "SAI sync provider data not found\n");
  94. return -EINVAL;
  95. }
  96. /* Configure sync client */
  97. ret = stm32_sai_sync_conf_client(sai_client, synci);
  98. if (ret < 0)
  99. return ret;
  100. /* Configure sync provider */
  101. return stm32_sai_sync_conf_provider(sai_provider, synco);
  102. }
  103. static int stm32_sai_probe(struct platform_device *pdev)
  104. {
  105. struct stm32_sai_data *sai;
  106. struct reset_control *rst;
  107. struct resource *res;
  108. const struct of_device_id *of_id;
  109. sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
  110. if (!sai)
  111. return -ENOMEM;
  112. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  113. sai->base = devm_ioremap_resource(&pdev->dev, res);
  114. if (IS_ERR(sai->base))
  115. return PTR_ERR(sai->base);
  116. of_id = of_match_device(stm32_sai_ids, &pdev->dev);
  117. if (of_id)
  118. sai->conf = (struct stm32_sai_conf *)of_id->data;
  119. else
  120. return -EINVAL;
  121. if (!STM_SAI_IS_F4(sai)) {
  122. sai->pclk = devm_clk_get(&pdev->dev, "pclk");
  123. if (IS_ERR(sai->pclk)) {
  124. dev_err(&pdev->dev, "missing bus clock pclk\n");
  125. return PTR_ERR(sai->pclk);
  126. }
  127. }
  128. sai->clk_x8k = devm_clk_get(&pdev->dev, "x8k");
  129. if (IS_ERR(sai->clk_x8k)) {
  130. dev_err(&pdev->dev, "missing x8k parent clock\n");
  131. return PTR_ERR(sai->clk_x8k);
  132. }
  133. sai->clk_x11k = devm_clk_get(&pdev->dev, "x11k");
  134. if (IS_ERR(sai->clk_x11k)) {
  135. dev_err(&pdev->dev, "missing x11k parent clock\n");
  136. return PTR_ERR(sai->clk_x11k);
  137. }
  138. /* init irqs */
  139. sai->irq = platform_get_irq(pdev, 0);
  140. if (sai->irq < 0) {
  141. dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
  142. return sai->irq;
  143. }
  144. /* reset */
  145. rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  146. if (!IS_ERR(rst)) {
  147. reset_control_assert(rst);
  148. udelay(2);
  149. reset_control_deassert(rst);
  150. }
  151. sai->pdev = pdev;
  152. sai->set_sync = &stm32_sai_set_sync;
  153. platform_set_drvdata(pdev, sai);
  154. return devm_of_platform_populate(&pdev->dev);
  155. }
  156. MODULE_DEVICE_TABLE(of, stm32_sai_ids);
  157. static struct platform_driver stm32_sai_driver = {
  158. .driver = {
  159. .name = "st,stm32-sai",
  160. .of_match_table = stm32_sai_ids,
  161. },
  162. .probe = stm32_sai_probe,
  163. };
  164. module_platform_driver(stm32_sai_driver);
  165. MODULE_DESCRIPTION("STM32 Soc SAI Interface");
  166. MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>");
  167. MODULE_ALIAS("platform:st,stm32-sai");
  168. MODULE_LICENSE("GPL v2");