mx27vis_wm8974.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * mx27vis_wm8974.c -- SoC audio for mx27vis
  3. *
  4. * Copyright 2009 Vista Silicon S.L.
  5. * Author: Javier Martin
  6. * javier.martin@vista-silicon.com
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.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 <sound/soc-dapm.h>
  22. #include "../codecs/wm8974.h"
  23. #include "mx1_mx2-pcm.h"
  24. #include "mxc-ssi.h"
  25. #include <mach/gpio.h>
  26. #include <mach/iomux.h>
  27. #define IGNORED_ARG 0
  28. static struct snd_soc_card mx27vis;
  29. /**
  30. * This function connects SSI1 (HPCR1) as slave to
  31. * SSI1 external signals (PPCR1)
  32. * As slave, HPCR1 must set TFSDIR and TCLKDIR as inputs from
  33. * port 4
  34. */
  35. void audmux_connect_1_4(void)
  36. {
  37. pr_debug("AUDMUX: normal operation mode\n");
  38. /* Reset HPCR1 and PPCR1 */
  39. DAM_HPCR1 = 0x00000000;
  40. DAM_PPCR1 = 0x00000000;
  41. /* set to synchronous */
  42. DAM_HPCR1 |= AUDMUX_HPCR_SYN;
  43. DAM_PPCR1 |= AUDMUX_PPCR_SYN;
  44. /* set Rx sources 1 <--> 4 */
  45. DAM_HPCR1 |= AUDMUX_HPCR_RXDSEL(3); /* port 4 */
  46. DAM_PPCR1 |= AUDMUX_PPCR_RXDSEL(0); /* port 1 */
  47. /* set Tx frame and Clock direction and source 4 --> 1 output */
  48. DAM_HPCR1 |= AUDMUX_HPCR_TFSDIR | AUDMUX_HPCR_TCLKDIR;
  49. DAM_HPCR1 |= AUDMUX_HPCR_TFCSEL(3); /* TxDS and TxCclk from port 4 */
  50. return;
  51. }
  52. static int mx27vis_hifi_hw_params(struct snd_pcm_substream *substream,
  53. struct snd_pcm_hw_params *params)
  54. {
  55. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  56. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  57. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  58. unsigned int pll_out = 0, bclk = 0, fmt = 0, mclk = 0;
  59. int ret = 0;
  60. /*
  61. * The WM8974 is better at generating accurate audio clocks than the
  62. * MX27 SSI controller, so we will use it as master when we can.
  63. */
  64. switch (params_rate(params)) {
  65. case 8000:
  66. fmt = SND_SOC_DAIFMT_CBM_CFM;
  67. mclk = WM8974_MCLKDIV_12;
  68. pll_out = 24576000;
  69. break;
  70. case 16000:
  71. fmt = SND_SOC_DAIFMT_CBM_CFM;
  72. pll_out = 12288000;
  73. break;
  74. case 48000:
  75. fmt = SND_SOC_DAIFMT_CBM_CFM;
  76. bclk = WM8974_BCLKDIV_4;
  77. pll_out = 12288000;
  78. break;
  79. case 96000:
  80. fmt = SND_SOC_DAIFMT_CBM_CFM;
  81. bclk = WM8974_BCLKDIV_2;
  82. pll_out = 12288000;
  83. break;
  84. case 11025:
  85. fmt = SND_SOC_DAIFMT_CBM_CFM;
  86. bclk = WM8974_BCLKDIV_16;
  87. pll_out = 11289600;
  88. break;
  89. case 22050:
  90. fmt = SND_SOC_DAIFMT_CBM_CFM;
  91. bclk = WM8974_BCLKDIV_8;
  92. pll_out = 11289600;
  93. break;
  94. case 44100:
  95. fmt = SND_SOC_DAIFMT_CBM_CFM;
  96. bclk = WM8974_BCLKDIV_4;
  97. mclk = WM8974_MCLKDIV_2;
  98. pll_out = 11289600;
  99. break;
  100. case 88200:
  101. fmt = SND_SOC_DAIFMT_CBM_CFM;
  102. bclk = WM8974_BCLKDIV_2;
  103. pll_out = 11289600;
  104. break;
  105. }
  106. /* set codec DAI configuration */
  107. ret = codec_dai->ops->set_fmt(codec_dai,
  108. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_IF |
  109. SND_SOC_DAIFMT_SYNC | fmt);
  110. if (ret < 0) {
  111. printk(KERN_ERR "Error from codec DAI configuration\n");
  112. return ret;
  113. }
  114. /* set cpu DAI configuration */
  115. ret = cpu_dai->ops->set_fmt(cpu_dai,
  116. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  117. SND_SOC_DAIFMT_SYNC | fmt);
  118. if (ret < 0) {
  119. printk(KERN_ERR "Error from cpu DAI configuration\n");
  120. return ret;
  121. }
  122. /* Put DC field of STCCR to 1 (not zero) */
  123. ret = cpu_dai->ops->set_tdm_slot(cpu_dai, 0, 2);
  124. /* set the SSI system clock as input */
  125. ret = cpu_dai->ops->set_sysclk(cpu_dai, IMX_SSP_SYS_CLK, 0,
  126. SND_SOC_CLOCK_IN);
  127. if (ret < 0) {
  128. printk(KERN_ERR "Error when setting system SSI clk\n");
  129. return ret;
  130. }
  131. /* set codec BCLK division for sample rate */
  132. ret = codec_dai->ops->set_clkdiv(codec_dai, WM8974_BCLKDIV, bclk);
  133. if (ret < 0) {
  134. printk(KERN_ERR "Error when setting BCLK division\n");
  135. return ret;
  136. }
  137. /* codec PLL input is 25 MHz */
  138. ret = codec_dai->ops->set_pll(codec_dai, IGNORED_ARG,
  139. 25000000, pll_out);
  140. if (ret < 0) {
  141. printk(KERN_ERR "Error when setting PLL input\n");
  142. return ret;
  143. }
  144. /*set codec MCLK division for sample rate */
  145. ret = codec_dai->ops->set_clkdiv(codec_dai, WM8974_MCLKDIV, mclk);
  146. if (ret < 0) {
  147. printk(KERN_ERR "Error when setting MCLK division\n");
  148. return ret;
  149. }
  150. return 0;
  151. }
  152. static int mx27vis_hifi_hw_free(struct snd_pcm_substream *substream)
  153. {
  154. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  155. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  156. /* disable the PLL */
  157. return codec_dai->ops->set_pll(codec_dai, IGNORED_ARG, 0, 0);
  158. }
  159. /*
  160. * mx27vis WM8974 HiFi DAI opserations.
  161. */
  162. static struct snd_soc_ops mx27vis_hifi_ops = {
  163. .hw_params = mx27vis_hifi_hw_params,
  164. .hw_free = mx27vis_hifi_hw_free,
  165. };
  166. static int mx27vis_suspend(struct platform_device *pdev, pm_message_t state)
  167. {
  168. return 0;
  169. }
  170. static int mx27vis_resume(struct platform_device *pdev)
  171. {
  172. return 0;
  173. }
  174. static int mx27vis_probe(struct platform_device *pdev)
  175. {
  176. int ret = 0;
  177. ret = get_ssi_clk(0, &pdev->dev);
  178. if (ret < 0) {
  179. printk(KERN_ERR "%s: cant get ssi clock\n", __func__);
  180. return ret;
  181. }
  182. return 0;
  183. }
  184. static int mx27vis_remove(struct platform_device *pdev)
  185. {
  186. put_ssi_clk(0);
  187. return 0;
  188. }
  189. static struct snd_soc_dai_link mx27vis_dai[] = {
  190. { /* Hifi Playback*/
  191. .name = "WM8974",
  192. .stream_name = "WM8974 HiFi",
  193. .cpu_dai = &imx_ssi_pcm_dai[0],
  194. .codec_dai = &wm8974_dai,
  195. .ops = &mx27vis_hifi_ops,
  196. },
  197. };
  198. static struct snd_soc_card mx27vis = {
  199. .name = "mx27vis",
  200. .platform = &mx1_mx2_soc_platform,
  201. .probe = mx27vis_probe,
  202. .remove = mx27vis_remove,
  203. .suspend_pre = mx27vis_suspend,
  204. .resume_post = mx27vis_resume,
  205. .dai_link = mx27vis_dai,
  206. .num_links = ARRAY_SIZE(mx27vis_dai),
  207. };
  208. static struct snd_soc_device mx27vis_snd_devdata = {
  209. .card = &mx27vis,
  210. .codec_dev = &soc_codec_dev_wm8974,
  211. };
  212. static struct platform_device *mx27vis_snd_device;
  213. /* Temporal definition of board specific behaviour */
  214. void gpio_ssi_active(int ssi_num)
  215. {
  216. int ret = 0;
  217. unsigned int ssi1_pins[] = {
  218. PC20_PF_SSI1_FS,
  219. PC21_PF_SSI1_RXD,
  220. PC22_PF_SSI1_TXD,
  221. PC23_PF_SSI1_CLK,
  222. };
  223. unsigned int ssi2_pins[] = {
  224. PC24_PF_SSI2_FS,
  225. PC25_PF_SSI2_RXD,
  226. PC26_PF_SSI2_TXD,
  227. PC27_PF_SSI2_CLK,
  228. };
  229. if (ssi_num == 0)
  230. ret = mxc_gpio_setup_multiple_pins(ssi1_pins,
  231. ARRAY_SIZE(ssi1_pins), "USB OTG");
  232. else
  233. ret = mxc_gpio_setup_multiple_pins(ssi2_pins,
  234. ARRAY_SIZE(ssi2_pins), "USB OTG");
  235. if (ret)
  236. printk(KERN_ERR "Error requesting ssi %x pins\n", ssi_num);
  237. }
  238. static int __init mx27vis_init(void)
  239. {
  240. int ret;
  241. mx27vis_snd_device = platform_device_alloc("soc-audio", -1);
  242. if (!mx27vis_snd_device)
  243. return -ENOMEM;
  244. platform_set_drvdata(mx27vis_snd_device, &mx27vis_snd_devdata);
  245. mx27vis_snd_devdata.dev = &mx27vis_snd_device->dev;
  246. ret = platform_device_add(mx27vis_snd_device);
  247. if (ret) {
  248. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  249. platform_device_put(mx27vis_snd_device);
  250. }
  251. /* WM8974 uses SSI1 (HPCR1) via AUDMUX port 4 for audio (PPCR1) */
  252. gpio_ssi_active(0);
  253. audmux_connect_1_4();
  254. return ret;
  255. }
  256. static void __exit mx27vis_exit(void)
  257. {
  258. /* We should call some "ssi_gpio_inactive()" properly */
  259. }
  260. module_init(mx27vis_init);
  261. module_exit(mx27vis_exit);
  262. MODULE_AUTHOR("Javier Martin, javier.martin@vista-silicon.com");
  263. MODULE_DESCRIPTION("ALSA SoC WM8974 mx27vis");
  264. MODULE_LICENSE("GPL");