mx27vis_wm8974.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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, 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, IGNORED_ARG,
  158. 0, 0);
  159. }
  160. /*
  161. * mx27vis WM8974 HiFi DAI opserations.
  162. */
  163. static struct snd_soc_ops mx27vis_hifi_ops = {
  164. .hw_params = mx27vis_hifi_hw_params,
  165. .hw_free = mx27vis_hifi_hw_free,
  166. };
  167. static int mx27vis_suspend(struct platform_device *pdev, pm_message_t state)
  168. {
  169. return 0;
  170. }
  171. static int mx27vis_resume(struct platform_device *pdev)
  172. {
  173. return 0;
  174. }
  175. static int mx27vis_probe(struct platform_device *pdev)
  176. {
  177. int ret = 0;
  178. ret = get_ssi_clk(0, &pdev->dev);
  179. if (ret < 0) {
  180. printk(KERN_ERR "%s: cant get ssi clock\n", __func__);
  181. return ret;
  182. }
  183. return 0;
  184. }
  185. static int mx27vis_remove(struct platform_device *pdev)
  186. {
  187. put_ssi_clk(0);
  188. return 0;
  189. }
  190. static struct snd_soc_dai_link mx27vis_dai[] = {
  191. { /* Hifi Playback*/
  192. .name = "WM8974",
  193. .stream_name = "WM8974 HiFi",
  194. .cpu_dai = &imx_ssi_pcm_dai[0],
  195. .codec_dai = &wm8974_dai,
  196. .ops = &mx27vis_hifi_ops,
  197. },
  198. };
  199. static struct snd_soc_card mx27vis = {
  200. .name = "mx27vis",
  201. .platform = &mx1_mx2_soc_platform,
  202. .probe = mx27vis_probe,
  203. .remove = mx27vis_remove,
  204. .suspend_pre = mx27vis_suspend,
  205. .resume_post = mx27vis_resume,
  206. .dai_link = mx27vis_dai,
  207. .num_links = ARRAY_SIZE(mx27vis_dai),
  208. };
  209. static struct snd_soc_device mx27vis_snd_devdata = {
  210. .card = &mx27vis,
  211. .codec_dev = &soc_codec_dev_wm8974,
  212. };
  213. static struct platform_device *mx27vis_snd_device;
  214. /* Temporal definition of board specific behaviour */
  215. void gpio_ssi_active(int ssi_num)
  216. {
  217. int ret = 0;
  218. unsigned int ssi1_pins[] = {
  219. PC20_PF_SSI1_FS,
  220. PC21_PF_SSI1_RXD,
  221. PC22_PF_SSI1_TXD,
  222. PC23_PF_SSI1_CLK,
  223. };
  224. unsigned int ssi2_pins[] = {
  225. PC24_PF_SSI2_FS,
  226. PC25_PF_SSI2_RXD,
  227. PC26_PF_SSI2_TXD,
  228. PC27_PF_SSI2_CLK,
  229. };
  230. if (ssi_num == 0)
  231. ret = mxc_gpio_setup_multiple_pins(ssi1_pins,
  232. ARRAY_SIZE(ssi1_pins), "USB OTG");
  233. else
  234. ret = mxc_gpio_setup_multiple_pins(ssi2_pins,
  235. ARRAY_SIZE(ssi2_pins), "USB OTG");
  236. if (ret)
  237. printk(KERN_ERR "Error requesting ssi %x pins\n", ssi_num);
  238. }
  239. static int __init mx27vis_init(void)
  240. {
  241. int ret;
  242. mx27vis_snd_device = platform_device_alloc("soc-audio", -1);
  243. if (!mx27vis_snd_device)
  244. return -ENOMEM;
  245. platform_set_drvdata(mx27vis_snd_device, &mx27vis_snd_devdata);
  246. mx27vis_snd_devdata.dev = &mx27vis_snd_device->dev;
  247. ret = platform_device_add(mx27vis_snd_device);
  248. if (ret) {
  249. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  250. platform_device_put(mx27vis_snd_device);
  251. }
  252. /* WM8974 uses SSI1 (HPCR1) via AUDMUX port 4 for audio (PPCR1) */
  253. gpio_ssi_active(0);
  254. audmux_connect_1_4();
  255. return ret;
  256. }
  257. static void __exit mx27vis_exit(void)
  258. {
  259. /* We should call some "ssi_gpio_inactive()" properly */
  260. }
  261. module_init(mx27vis_init);
  262. module_exit(mx27vis_exit);
  263. MODULE_AUTHOR("Javier Martin, javier.martin@vista-silicon.com");
  264. MODULE_DESCRIPTION("ALSA SoC WM8974 mx27vis");
  265. MODULE_LICENSE("GPL");