wm1133-ev1.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * wm1133-ev1.c - Audio for WM1133-EV1 on i.MX31ADS
  3. *
  4. * Copyright (c) 2010 Wolfson Microelectronics plc
  5. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  6. *
  7. * Based on an earlier driver for the same hardware by Liam Girdwood.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/platform_device.h>
  15. #include <linux/clk.h>
  16. #include <linux/module.h>
  17. #include <sound/core.h>
  18. #include <sound/jack.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/soc.h>
  22. #include "imx-ssi.h"
  23. #include "../codecs/wm8350.h"
  24. #include "imx-audmux.h"
  25. /* There is a silicon mic on the board optionally connected via a solder pad
  26. * SP1. Define this to enable it.
  27. */
  28. #undef USE_SIMIC
  29. struct _wm8350_audio {
  30. unsigned int channels;
  31. snd_pcm_format_t format;
  32. unsigned int rate;
  33. unsigned int sysclk;
  34. unsigned int bclkdiv;
  35. unsigned int clkdiv;
  36. unsigned int lr_rate;
  37. };
  38. /* in order of power consumption per rate (lowest first) */
  39. static const struct _wm8350_audio wm8350_audio[] = {
  40. /* 16bit mono modes */
  41. {1, SNDRV_PCM_FORMAT_S16_LE, 8000, 12288000 >> 1,
  42. WM8350_BCLK_DIV_48, WM8350_DACDIV_3, 16,},
  43. /* 16 bit stereo modes */
  44. {2, SNDRV_PCM_FORMAT_S16_LE, 8000, 12288000,
  45. WM8350_BCLK_DIV_48, WM8350_DACDIV_6, 32,},
  46. {2, SNDRV_PCM_FORMAT_S16_LE, 16000, 12288000,
  47. WM8350_BCLK_DIV_24, WM8350_DACDIV_3, 32,},
  48. {2, SNDRV_PCM_FORMAT_S16_LE, 32000, 12288000,
  49. WM8350_BCLK_DIV_12, WM8350_DACDIV_1_5, 32,},
  50. {2, SNDRV_PCM_FORMAT_S16_LE, 48000, 12288000,
  51. WM8350_BCLK_DIV_8, WM8350_DACDIV_1, 32,},
  52. {2, SNDRV_PCM_FORMAT_S16_LE, 96000, 24576000,
  53. WM8350_BCLK_DIV_8, WM8350_DACDIV_1, 32,},
  54. {2, SNDRV_PCM_FORMAT_S16_LE, 11025, 11289600,
  55. WM8350_BCLK_DIV_32, WM8350_DACDIV_4, 32,},
  56. {2, SNDRV_PCM_FORMAT_S16_LE, 22050, 11289600,
  57. WM8350_BCLK_DIV_16, WM8350_DACDIV_2, 32,},
  58. {2, SNDRV_PCM_FORMAT_S16_LE, 44100, 11289600,
  59. WM8350_BCLK_DIV_8, WM8350_DACDIV_1, 32,},
  60. {2, SNDRV_PCM_FORMAT_S16_LE, 88200, 22579200,
  61. WM8350_BCLK_DIV_8, WM8350_DACDIV_1, 32,},
  62. /* 24bit stereo modes */
  63. {2, SNDRV_PCM_FORMAT_S24_LE, 48000, 12288000,
  64. WM8350_BCLK_DIV_4, WM8350_DACDIV_1, 64,},
  65. {2, SNDRV_PCM_FORMAT_S24_LE, 96000, 24576000,
  66. WM8350_BCLK_DIV_4, WM8350_DACDIV_1, 64,},
  67. {2, SNDRV_PCM_FORMAT_S24_LE, 44100, 11289600,
  68. WM8350_BCLK_DIV_4, WM8350_DACDIV_1, 64,},
  69. {2, SNDRV_PCM_FORMAT_S24_LE, 88200, 22579200,
  70. WM8350_BCLK_DIV_4, WM8350_DACDIV_1, 64,},
  71. };
  72. static int wm1133_ev1_hw_params(struct snd_pcm_substream *substream,
  73. struct snd_pcm_hw_params *params)
  74. {
  75. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  76. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  77. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  78. int i, found = 0;
  79. snd_pcm_format_t format = params_format(params);
  80. unsigned int rate = params_rate(params);
  81. unsigned int channels = params_channels(params);
  82. u32 dai_format;
  83. /* find the correct audio parameters */
  84. for (i = 0; i < ARRAY_SIZE(wm8350_audio); i++) {
  85. if (rate == wm8350_audio[i].rate &&
  86. format == wm8350_audio[i].format &&
  87. channels == wm8350_audio[i].channels) {
  88. found = 1;
  89. break;
  90. }
  91. }
  92. if (!found)
  93. return -EINVAL;
  94. /* codec FLL input is 14.75 MHz from MCLK */
  95. snd_soc_dai_set_pll(codec_dai, 0, 0, 14750000, wm8350_audio[i].sysclk);
  96. dai_format = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  97. SND_SOC_DAIFMT_CBM_CFM;
  98. /* set codec DAI configuration */
  99. snd_soc_dai_set_fmt(codec_dai, dai_format);
  100. /* set cpu DAI configuration */
  101. snd_soc_dai_set_fmt(cpu_dai, dai_format);
  102. /* TODO: The SSI driver should figure this out for us */
  103. switch (channels) {
  104. case 2:
  105. snd_soc_dai_set_tdm_slot(cpu_dai, 0xffffffc, 0xffffffc, 2, 0);
  106. break;
  107. case 1:
  108. snd_soc_dai_set_tdm_slot(cpu_dai, 0xffffffe, 0xffffffe, 1, 0);
  109. break;
  110. default:
  111. return -EINVAL;
  112. }
  113. /* set MCLK as the codec system clock for DAC and ADC */
  114. snd_soc_dai_set_sysclk(codec_dai, WM8350_MCLK_SEL_PLL_MCLK,
  115. wm8350_audio[i].sysclk, SND_SOC_CLOCK_IN);
  116. /* set codec BCLK division for sample rate */
  117. snd_soc_dai_set_clkdiv(codec_dai, WM8350_BCLK_CLKDIV,
  118. wm8350_audio[i].bclkdiv);
  119. /* DAI is synchronous and clocked with DAC LRCLK & ADC LRC */
  120. snd_soc_dai_set_clkdiv(codec_dai,
  121. WM8350_DACLR_CLKDIV, wm8350_audio[i].lr_rate);
  122. snd_soc_dai_set_clkdiv(codec_dai,
  123. WM8350_ADCLR_CLKDIV, wm8350_audio[i].lr_rate);
  124. /* now configure DAC and ADC clocks */
  125. snd_soc_dai_set_clkdiv(codec_dai,
  126. WM8350_DAC_CLKDIV, wm8350_audio[i].clkdiv);
  127. snd_soc_dai_set_clkdiv(codec_dai,
  128. WM8350_ADC_CLKDIV, wm8350_audio[i].clkdiv);
  129. return 0;
  130. }
  131. static struct snd_soc_ops wm1133_ev1_ops = {
  132. .hw_params = wm1133_ev1_hw_params,
  133. };
  134. static const struct snd_soc_dapm_widget wm1133_ev1_widgets[] = {
  135. #ifdef USE_SIMIC
  136. SND_SOC_DAPM_MIC("SiMIC", NULL),
  137. #endif
  138. SND_SOC_DAPM_MIC("Mic1 Jack", NULL),
  139. SND_SOC_DAPM_MIC("Mic2 Jack", NULL),
  140. SND_SOC_DAPM_LINE("Line In Jack", NULL),
  141. SND_SOC_DAPM_LINE("Line Out Jack", NULL),
  142. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  143. };
  144. /* imx32ads soc_card audio map */
  145. static const struct snd_soc_dapm_route wm1133_ev1_map[] = {
  146. #ifdef USE_SIMIC
  147. /* SiMIC --> IN1LN (with automatic bias) via SP1 */
  148. { "IN1LN", NULL, "Mic Bias" },
  149. { "Mic Bias", NULL, "SiMIC" },
  150. #endif
  151. /* Mic 1 Jack --> IN1LN and IN1LP (with automatic bias) */
  152. { "IN1LN", NULL, "Mic Bias" },
  153. { "IN1LP", NULL, "Mic1 Jack" },
  154. { "Mic Bias", NULL, "Mic1 Jack" },
  155. /* Mic 2 Jack --> IN1RN and IN1RP (with automatic bias) */
  156. { "IN1RN", NULL, "Mic Bias" },
  157. { "IN1RP", NULL, "Mic2 Jack" },
  158. { "Mic Bias", NULL, "Mic2 Jack" },
  159. /* Line in Jack --> AUX (L+R) */
  160. { "IN3R", NULL, "Line In Jack" },
  161. { "IN3L", NULL, "Line In Jack" },
  162. /* Out1 --> Headphone Jack */
  163. { "Headphone Jack", NULL, "OUT1R" },
  164. { "Headphone Jack", NULL, "OUT1L" },
  165. /* Out1 --> Line Out Jack */
  166. { "Line Out Jack", NULL, "OUT2R" },
  167. { "Line Out Jack", NULL, "OUT2L" },
  168. };
  169. static struct snd_soc_jack hp_jack;
  170. static struct snd_soc_jack_pin hp_jack_pins[] = {
  171. { .pin = "Headphone Jack", .mask = SND_JACK_HEADPHONE },
  172. };
  173. static struct snd_soc_jack mic_jack;
  174. static struct snd_soc_jack_pin mic_jack_pins[] = {
  175. { .pin = "Mic1 Jack", .mask = SND_JACK_MICROPHONE },
  176. { .pin = "Mic2 Jack", .mask = SND_JACK_MICROPHONE },
  177. };
  178. static int wm1133_ev1_init(struct snd_soc_pcm_runtime *rtd)
  179. {
  180. struct snd_soc_codec *codec = rtd->codec;
  181. struct snd_soc_dapm_context *dapm = &codec->dapm;
  182. snd_soc_dapm_new_controls(dapm, wm1133_ev1_widgets,
  183. ARRAY_SIZE(wm1133_ev1_widgets));
  184. snd_soc_dapm_add_routes(dapm, wm1133_ev1_map,
  185. ARRAY_SIZE(wm1133_ev1_map));
  186. /* Headphone jack detection */
  187. snd_soc_jack_new(codec, "Headphone", SND_JACK_HEADPHONE, &hp_jack);
  188. snd_soc_jack_add_pins(&hp_jack, ARRAY_SIZE(hp_jack_pins),
  189. hp_jack_pins);
  190. wm8350_hp_jack_detect(codec, WM8350_JDR, &hp_jack, SND_JACK_HEADPHONE);
  191. /* Microphone jack detection */
  192. snd_soc_jack_new(codec, "Microphone",
  193. SND_JACK_MICROPHONE | SND_JACK_BTN_0, &mic_jack);
  194. snd_soc_jack_add_pins(&mic_jack, ARRAY_SIZE(mic_jack_pins),
  195. mic_jack_pins);
  196. wm8350_mic_jack_detect(codec, &mic_jack, SND_JACK_MICROPHONE,
  197. SND_JACK_BTN_0);
  198. snd_soc_dapm_force_enable_pin(dapm, "Mic Bias");
  199. return 0;
  200. }
  201. static struct snd_soc_dai_link wm1133_ev1_dai = {
  202. .name = "WM1133-EV1",
  203. .stream_name = "Audio",
  204. .cpu_dai_name = "imx-ssi.0",
  205. .codec_dai_name = "wm8350-hifi",
  206. .platform_name = "imx-fiq-pcm-audio.0",
  207. .codec_name = "wm8350-codec.0-0x1a",
  208. .init = wm1133_ev1_init,
  209. .ops = &wm1133_ev1_ops,
  210. .symmetric_rates = 1,
  211. };
  212. static struct snd_soc_card wm1133_ev1 = {
  213. .name = "WM1133-EV1",
  214. .owner = THIS_MODULE,
  215. .dai_link = &wm1133_ev1_dai,
  216. .num_links = 1,
  217. };
  218. static struct platform_device *wm1133_ev1_snd_device;
  219. static int __init wm1133_ev1_audio_init(void)
  220. {
  221. int ret;
  222. unsigned int ptcr, pdcr;
  223. /* SSI0 mastered by port 5 */
  224. ptcr = IMX_AUDMUX_V2_PTCR_SYN |
  225. IMX_AUDMUX_V2_PTCR_TFSDIR |
  226. IMX_AUDMUX_V2_PTCR_TFSEL(MX31_AUDMUX_PORT5_SSI_PINS_5) |
  227. IMX_AUDMUX_V2_PTCR_TCLKDIR |
  228. IMX_AUDMUX_V2_PTCR_TCSEL(MX31_AUDMUX_PORT5_SSI_PINS_5);
  229. pdcr = IMX_AUDMUX_V2_PDCR_RXDSEL(MX31_AUDMUX_PORT5_SSI_PINS_5);
  230. imx_audmux_v2_configure_port(MX31_AUDMUX_PORT1_SSI0, ptcr, pdcr);
  231. ptcr = IMX_AUDMUX_V2_PTCR_SYN;
  232. pdcr = IMX_AUDMUX_V2_PDCR_RXDSEL(MX31_AUDMUX_PORT1_SSI0);
  233. imx_audmux_v2_configure_port(MX31_AUDMUX_PORT5_SSI_PINS_5, ptcr, pdcr);
  234. wm1133_ev1_snd_device = platform_device_alloc("soc-audio", -1);
  235. if (!wm1133_ev1_snd_device)
  236. return -ENOMEM;
  237. platform_set_drvdata(wm1133_ev1_snd_device, &wm1133_ev1);
  238. ret = platform_device_add(wm1133_ev1_snd_device);
  239. if (ret)
  240. platform_device_put(wm1133_ev1_snd_device);
  241. return ret;
  242. }
  243. module_init(wm1133_ev1_audio_init);
  244. static void __exit wm1133_ev1_audio_exit(void)
  245. {
  246. platform_device_unregister(wm1133_ev1_snd_device);
  247. }
  248. module_exit(wm1133_ev1_audio_exit);
  249. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  250. MODULE_DESCRIPTION("Audio for WM1133-EV1 on i.MX31ADS");
  251. MODULE_LICENSE("GPL");