stm32_sai.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 %s\n", np_provider->name);
  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. ret = -EINVAL;
  95. goto out_put_dev;
  96. }
  97. /* Configure sync client */
  98. ret = stm32_sai_sync_conf_client(sai_client, synci);
  99. if (ret < 0)
  100. goto out_put_dev;
  101. /* Configure sync provider */
  102. ret = stm32_sai_sync_conf_provider(sai_provider, synco);
  103. out_put_dev:
  104. put_device(&pdev->dev);
  105. return ret;
  106. }
  107. static int stm32_sai_probe(struct platform_device *pdev)
  108. {
  109. struct stm32_sai_data *sai;
  110. struct reset_control *rst;
  111. struct resource *res;
  112. const struct of_device_id *of_id;
  113. sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
  114. if (!sai)
  115. return -ENOMEM;
  116. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  117. sai->base = devm_ioremap_resource(&pdev->dev, res);
  118. if (IS_ERR(sai->base))
  119. return PTR_ERR(sai->base);
  120. of_id = of_match_device(stm32_sai_ids, &pdev->dev);
  121. if (of_id)
  122. sai->conf = (struct stm32_sai_conf *)of_id->data;
  123. else
  124. return -EINVAL;
  125. if (!STM_SAI_IS_F4(sai)) {
  126. sai->pclk = devm_clk_get(&pdev->dev, "pclk");
  127. if (IS_ERR(sai->pclk)) {
  128. dev_err(&pdev->dev, "missing bus clock pclk\n");
  129. return PTR_ERR(sai->pclk);
  130. }
  131. }
  132. sai->clk_x8k = devm_clk_get(&pdev->dev, "x8k");
  133. if (IS_ERR(sai->clk_x8k)) {
  134. dev_err(&pdev->dev, "missing x8k parent clock\n");
  135. return PTR_ERR(sai->clk_x8k);
  136. }
  137. sai->clk_x11k = devm_clk_get(&pdev->dev, "x11k");
  138. if (IS_ERR(sai->clk_x11k)) {
  139. dev_err(&pdev->dev, "missing x11k parent clock\n");
  140. return PTR_ERR(sai->clk_x11k);
  141. }
  142. /* init irqs */
  143. sai->irq = platform_get_irq(pdev, 0);
  144. if (sai->irq < 0) {
  145. dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
  146. return sai->irq;
  147. }
  148. /* reset */
  149. rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  150. if (!IS_ERR(rst)) {
  151. reset_control_assert(rst);
  152. udelay(2);
  153. reset_control_deassert(rst);
  154. }
  155. sai->pdev = pdev;
  156. sai->set_sync = &stm32_sai_set_sync;
  157. platform_set_drvdata(pdev, sai);
  158. return devm_of_platform_populate(&pdev->dev);
  159. }
  160. MODULE_DEVICE_TABLE(of, stm32_sai_ids);
  161. static struct platform_driver stm32_sai_driver = {
  162. .driver = {
  163. .name = "st,stm32-sai",
  164. .of_match_table = stm32_sai_ids,
  165. },
  166. .probe = stm32_sai_probe,
  167. };
  168. module_platform_driver(stm32_sai_driver);
  169. MODULE_DESCRIPTION("STM32 Soc SAI Interface");
  170. MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>");
  171. MODULE_ALIAS("platform:st,stm32-sai");
  172. MODULE_LICENSE("GPL v2");