eukrea-tlv320.c 6.2 KB

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