eukrea-tlv320.c 6.7 KB

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