eukrea-tlv320.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // eukrea-tlv320.c -- SoC audio for eukrea_cpuimxXX in I2S mode
  4. //
  5. // Copyright 2010 Eric Bénard, Eukréa Electromatique <eric@eukrea.com>
  6. //
  7. // based on sound/soc/s3c24xx/s3c24xx_simtec_tlv320aic23.c
  8. // which is Copyright 2009 Simtec Electronics
  9. // and on sound/soc/imx/phycore-ac97.c which is
  10. // Copyright 2009 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  11. #include <linux/errno.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/of.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/device.h>
  17. #include <linux/i2c.h>
  18. #include <sound/core.h>
  19. #include <sound/pcm.h>
  20. #include <sound/soc.h>
  21. #include <asm/mach-types.h>
  22. #include "../codecs/tlv320aic23.h"
  23. #include "imx-ssi.h"
  24. #include "imx-audmux.h"
  25. #define CODEC_CLOCK 12000000
  26. static int eukrea_tlv320_hw_params(struct snd_pcm_substream *substream,
  27. struct snd_pcm_hw_params *params)
  28. {
  29. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  30. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  31. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  32. int ret;
  33. ret = snd_soc_dai_set_sysclk(codec_dai, 0,
  34. CODEC_CLOCK, SND_SOC_CLOCK_OUT);
  35. if (ret) {
  36. dev_err(cpu_dai->dev,
  37. "Failed to set the codec sysclk.\n");
  38. return ret;
  39. }
  40. snd_soc_dai_set_tdm_slot(cpu_dai, 0x3, 0x3, 2, 0);
  41. ret = snd_soc_dai_set_sysclk(cpu_dai, IMX_SSP_SYS_CLK, 0,
  42. SND_SOC_CLOCK_IN);
  43. /* fsl_ssi lacks the set_sysclk ops */
  44. if (ret && ret != -EINVAL) {
  45. dev_err(cpu_dai->dev,
  46. "Can't set the IMX_SSP_SYS_CLK CPU system clock.\n");
  47. return ret;
  48. }
  49. return 0;
  50. }
  51. static const struct snd_soc_ops eukrea_tlv320_snd_ops = {
  52. .hw_params = eukrea_tlv320_hw_params,
  53. };
  54. SND_SOC_DAILINK_DEFS(hifi,
  55. DAILINK_COMP_ARRAY(COMP_EMPTY()),
  56. DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "tlv320aic23-hifi")),
  57. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  58. static struct snd_soc_dai_link eukrea_tlv320_dai = {
  59. .name = "tlv320aic23",
  60. .stream_name = "TLV320AIC23",
  61. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  62. SND_SOC_DAIFMT_CBM_CFM,
  63. .ops = &eukrea_tlv320_snd_ops,
  64. SND_SOC_DAILINK_REG(hifi),
  65. };
  66. static struct snd_soc_card eukrea_tlv320 = {
  67. .owner = THIS_MODULE,
  68. .dai_link = &eukrea_tlv320_dai,
  69. .num_links = 1,
  70. };
  71. static int eukrea_tlv320_probe(struct platform_device *pdev)
  72. {
  73. int ret;
  74. int int_port = 0, ext_port;
  75. struct device_node *np = pdev->dev.of_node;
  76. struct device_node *ssi_np = NULL, *codec_np = NULL;
  77. eukrea_tlv320.dev = &pdev->dev;
  78. if (np) {
  79. ret = snd_soc_of_parse_card_name(&eukrea_tlv320,
  80. "eukrea,model");
  81. if (ret) {
  82. dev_err(&pdev->dev,
  83. "eukrea,model node missing or invalid.\n");
  84. goto err;
  85. }
  86. ssi_np = of_parse_phandle(pdev->dev.of_node,
  87. "ssi-controller", 0);
  88. if (!ssi_np) {
  89. dev_err(&pdev->dev,
  90. "ssi-controller missing or invalid.\n");
  91. ret = -ENODEV;
  92. goto err;
  93. }
  94. codec_np = of_parse_phandle(ssi_np, "codec-handle", 0);
  95. if (codec_np)
  96. eukrea_tlv320_dai.codecs->of_node = codec_np;
  97. else
  98. dev_err(&pdev->dev, "codec-handle node missing or invalid.\n");
  99. ret = of_property_read_u32(np, "fsl,mux-int-port", &int_port);
  100. if (ret) {
  101. dev_err(&pdev->dev,
  102. "fsl,mux-int-port node missing or invalid.\n");
  103. goto err;
  104. }
  105. ret = of_property_read_u32(np, "fsl,mux-ext-port", &ext_port);
  106. if (ret) {
  107. dev_err(&pdev->dev,
  108. "fsl,mux-ext-port node missing or invalid.\n");
  109. goto err;
  110. }
  111. /*
  112. * The port numbering in the hardware manual starts at 1, while
  113. * the audmux API expects it starts at 0.
  114. */
  115. int_port--;
  116. ext_port--;
  117. eukrea_tlv320_dai.cpus->of_node = ssi_np;
  118. eukrea_tlv320_dai.platforms->of_node = ssi_np;
  119. } else {
  120. eukrea_tlv320_dai.cpus->dai_name = "imx-ssi.0";
  121. eukrea_tlv320_dai.platforms->name = "imx-ssi.0";
  122. eukrea_tlv320_dai.codecs->name = "tlv320aic23-codec.0-001a";
  123. eukrea_tlv320.name = "cpuimx-audio";
  124. }
  125. if (machine_is_eukrea_cpuimx27() ||
  126. of_find_compatible_node(NULL, NULL, "fsl,imx21-audmux")) {
  127. imx_audmux_v1_configure_port(MX27_AUDMUX_HPCR1_SSI0,
  128. IMX_AUDMUX_V1_PCR_SYN |
  129. IMX_AUDMUX_V1_PCR_TFSDIR |
  130. IMX_AUDMUX_V1_PCR_TCLKDIR |
  131. IMX_AUDMUX_V1_PCR_RFSDIR |
  132. IMX_AUDMUX_V1_PCR_RCLKDIR |
  133. IMX_AUDMUX_V1_PCR_TFCSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4) |
  134. IMX_AUDMUX_V1_PCR_RFCSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4) |
  135. IMX_AUDMUX_V1_PCR_RXDSEL(MX27_AUDMUX_HPCR3_SSI_PINS_4)
  136. );
  137. imx_audmux_v1_configure_port(MX27_AUDMUX_HPCR3_SSI_PINS_4,
  138. IMX_AUDMUX_V1_PCR_SYN |
  139. IMX_AUDMUX_V1_PCR_RXDSEL(MX27_AUDMUX_HPCR1_SSI0)
  140. );
  141. } else if (machine_is_eukrea_cpuimx25sd() ||
  142. machine_is_eukrea_cpuimx35sd() ||
  143. machine_is_eukrea_cpuimx51sd() ||
  144. of_find_compatible_node(NULL, NULL, "fsl,imx31-audmux")) {
  145. if (!np)
  146. ext_port = machine_is_eukrea_cpuimx25sd() ?
  147. 4 : 3;
  148. imx_audmux_v2_configure_port(int_port,
  149. IMX_AUDMUX_V2_PTCR_SYN |
  150. IMX_AUDMUX_V2_PTCR_TFSDIR |
  151. IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
  152. IMX_AUDMUX_V2_PTCR_TCLKDIR |
  153. IMX_AUDMUX_V2_PTCR_TCSEL(ext_port),
  154. IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port)
  155. );
  156. imx_audmux_v2_configure_port(ext_port,
  157. IMX_AUDMUX_V2_PTCR_SYN,
  158. IMX_AUDMUX_V2_PDCR_RXDSEL(int_port)
  159. );
  160. } else {
  161. if (np) {
  162. /* The eukrea,asoc-tlv320 driver was explicitly
  163. * requested (through the device tree).
  164. */
  165. dev_err(&pdev->dev,
  166. "Missing or invalid audmux DT node.\n");
  167. return -ENODEV;
  168. } else {
  169. /* Return happy.
  170. * We might run on a totally different machine.
  171. */
  172. return 0;
  173. }
  174. }
  175. ret = snd_soc_register_card(&eukrea_tlv320);
  176. err:
  177. if (ret)
  178. dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
  179. of_node_put(ssi_np);
  180. return ret;
  181. }
  182. static int eukrea_tlv320_remove(struct platform_device *pdev)
  183. {
  184. snd_soc_unregister_card(&eukrea_tlv320);
  185. return 0;
  186. }
  187. static const struct of_device_id imx_tlv320_dt_ids[] = {
  188. { .compatible = "eukrea,asoc-tlv320"},
  189. { /* sentinel */ }
  190. };
  191. MODULE_DEVICE_TABLE(of, imx_tlv320_dt_ids);
  192. static struct platform_driver eukrea_tlv320_driver = {
  193. .driver = {
  194. .name = "eukrea_tlv320",
  195. .of_match_table = imx_tlv320_dt_ids,
  196. },
  197. .probe = eukrea_tlv320_probe,
  198. .remove = eukrea_tlv320_remove,
  199. };
  200. module_platform_driver(eukrea_tlv320_driver);
  201. MODULE_AUTHOR("Eric Bénard <eric@eukrea.com>");
  202. MODULE_DESCRIPTION("CPUIMX ALSA SoC driver");
  203. MODULE_LICENSE("GPL");
  204. MODULE_ALIAS("platform:eukrea_tlv320");