eukrea-tlv320.c 6.2 KB

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