wm1133-ev1.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /* find the correct audio parameters */
  83. for (i = 0; i < ARRAY_SIZE(wm8350_audio); i++) {
  84. if (rate == wm8350_audio[i].rate &&
  85. format == wm8350_audio[i].format &&
  86. channels == wm8350_audio[i].channels) {
  87. found = 1;
  88. break;
  89. }
  90. }
  91. if (!found)
  92. return -EINVAL;
  93. /* codec FLL input is 14.75 MHz from MCLK */
  94. snd_soc_dai_set_pll(codec_dai, 0, 0, 14750000, wm8350_audio[i].sysclk);
  95. /* TODO: The SSI driver should figure this out for us */
  96. switch (channels) {
  97. case 2:
  98. snd_soc_dai_set_tdm_slot(cpu_dai, 0x3, 0x3, 2, 0);
  99. break;
  100. case 1:
  101. snd_soc_dai_set_tdm_slot(cpu_dai, 0x1, 0x1, 1, 0);
  102. break;
  103. default:
  104. return -EINVAL;
  105. }
  106. /* set MCLK as the codec system clock for DAC and ADC */
  107. snd_soc_dai_set_sysclk(codec_dai, WM8350_MCLK_SEL_PLL_MCLK,
  108. wm8350_audio[i].sysclk, SND_SOC_CLOCK_IN);
  109. /* set codec BCLK division for sample rate */
  110. snd_soc_dai_set_clkdiv(codec_dai, WM8350_BCLK_CLKDIV,
  111. wm8350_audio[i].bclkdiv);
  112. /* DAI is synchronous and clocked with DAC LRCLK & ADC LRC */
  113. snd_soc_dai_set_clkdiv(codec_dai,
  114. WM8350_DACLR_CLKDIV, wm8350_audio[i].lr_rate);
  115. snd_soc_dai_set_clkdiv(codec_dai,
  116. WM8350_ADCLR_CLKDIV, wm8350_audio[i].lr_rate);
  117. /* now configure DAC and ADC clocks */
  118. snd_soc_dai_set_clkdiv(codec_dai,
  119. WM8350_DAC_CLKDIV, wm8350_audio[i].clkdiv);
  120. snd_soc_dai_set_clkdiv(codec_dai,
  121. WM8350_ADC_CLKDIV, wm8350_audio[i].clkdiv);
  122. return 0;
  123. }
  124. static struct snd_soc_ops wm1133_ev1_ops = {
  125. .hw_params = wm1133_ev1_hw_params,
  126. };
  127. static const struct snd_soc_dapm_widget wm1133_ev1_widgets[] = {
  128. #ifdef USE_SIMIC
  129. SND_SOC_DAPM_MIC("SiMIC", NULL),
  130. #endif
  131. SND_SOC_DAPM_MIC("Mic1 Jack", NULL),
  132. SND_SOC_DAPM_MIC("Mic2 Jack", NULL),
  133. SND_SOC_DAPM_LINE("Line In Jack", NULL),
  134. SND_SOC_DAPM_LINE("Line Out Jack", NULL),
  135. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  136. };
  137. /* imx32ads soc_card audio map */
  138. static const struct snd_soc_dapm_route wm1133_ev1_map[] = {
  139. #ifdef USE_SIMIC
  140. /* SiMIC --> IN1LN (with automatic bias) via SP1 */
  141. { "IN1LN", NULL, "Mic Bias" },
  142. { "Mic Bias", NULL, "SiMIC" },
  143. #endif
  144. /* Mic 1 Jack --> IN1LN and IN1LP (with automatic bias) */
  145. { "IN1LN", NULL, "Mic Bias" },
  146. { "IN1LP", NULL, "Mic1 Jack" },
  147. { "Mic Bias", NULL, "Mic1 Jack" },
  148. /* Mic 2 Jack --> IN1RN and IN1RP (with automatic bias) */
  149. { "IN1RN", NULL, "Mic Bias" },
  150. { "IN1RP", NULL, "Mic2 Jack" },
  151. { "Mic Bias", NULL, "Mic2 Jack" },
  152. /* Line in Jack --> AUX (L+R) */
  153. { "IN3R", NULL, "Line In Jack" },
  154. { "IN3L", NULL, "Line In Jack" },
  155. /* Out1 --> Headphone Jack */
  156. { "Headphone Jack", NULL, "OUT1R" },
  157. { "Headphone Jack", NULL, "OUT1L" },
  158. /* Out1 --> Line Out Jack */
  159. { "Line Out Jack", NULL, "OUT2R" },
  160. { "Line Out Jack", NULL, "OUT2L" },
  161. };
  162. static struct snd_soc_jack hp_jack;
  163. static struct snd_soc_jack_pin hp_jack_pins[] = {
  164. { .pin = "Headphone Jack", .mask = SND_JACK_HEADPHONE },
  165. };
  166. static struct snd_soc_jack mic_jack;
  167. static struct snd_soc_jack_pin mic_jack_pins[] = {
  168. { .pin = "Mic1 Jack", .mask = SND_JACK_MICROPHONE },
  169. { .pin = "Mic2 Jack", .mask = SND_JACK_MICROPHONE },
  170. };
  171. static int wm1133_ev1_init(struct snd_soc_pcm_runtime *rtd)
  172. {
  173. struct snd_soc_codec *codec = rtd->codec;
  174. /* Headphone jack detection */
  175. snd_soc_card_jack_new(rtd->card, "Headphone", SND_JACK_HEADPHONE,
  176. &hp_jack, hp_jack_pins, ARRAY_SIZE(hp_jack_pins));
  177. wm8350_hp_jack_detect(codec, WM8350_JDR, &hp_jack, SND_JACK_HEADPHONE);
  178. /* Microphone jack detection */
  179. snd_soc_card_jack_new(rtd->card, "Microphone",
  180. SND_JACK_MICROPHONE | SND_JACK_BTN_0, &mic_jack,
  181. mic_jack_pins, ARRAY_SIZE(mic_jack_pins));
  182. wm8350_mic_jack_detect(codec, &mic_jack, SND_JACK_MICROPHONE,
  183. SND_JACK_BTN_0);
  184. snd_soc_dapm_force_enable_pin(&rtd->card->dapm, "Mic Bias");
  185. return 0;
  186. }
  187. static struct snd_soc_dai_link wm1133_ev1_dai = {
  188. .name = "WM1133-EV1",
  189. .stream_name = "Audio",
  190. .cpu_dai_name = "imx-ssi.0",
  191. .codec_dai_name = "wm8350-hifi",
  192. .platform_name = "imx-ssi.0",
  193. .codec_name = "wm8350-codec.0-0x1a",
  194. .init = wm1133_ev1_init,
  195. .ops = &wm1133_ev1_ops,
  196. .symmetric_rates = 1,
  197. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  198. SND_SOC_DAIFMT_CBM_CFM,
  199. };
  200. static struct snd_soc_card wm1133_ev1 = {
  201. .name = "WM1133-EV1",
  202. .owner = THIS_MODULE,
  203. .dai_link = &wm1133_ev1_dai,
  204. .num_links = 1,
  205. .dapm_widgets = wm1133_ev1_widgets,
  206. .num_dapm_widgets = ARRAY_SIZE(wm1133_ev1_widgets),
  207. .dapm_routes = wm1133_ev1_map,
  208. .num_dapm_routes = ARRAY_SIZE(wm1133_ev1_map),
  209. };
  210. static struct platform_device *wm1133_ev1_snd_device;
  211. static int __init wm1133_ev1_audio_init(void)
  212. {
  213. int ret;
  214. unsigned int ptcr, pdcr;
  215. /* SSI0 mastered by port 5 */
  216. ptcr = IMX_AUDMUX_V2_PTCR_SYN |
  217. IMX_AUDMUX_V2_PTCR_TFSDIR |
  218. IMX_AUDMUX_V2_PTCR_TFSEL(MX31_AUDMUX_PORT5_SSI_PINS_5) |
  219. IMX_AUDMUX_V2_PTCR_TCLKDIR |
  220. IMX_AUDMUX_V2_PTCR_TCSEL(MX31_AUDMUX_PORT5_SSI_PINS_5);
  221. pdcr = IMX_AUDMUX_V2_PDCR_RXDSEL(MX31_AUDMUX_PORT5_SSI_PINS_5);
  222. imx_audmux_v2_configure_port(MX31_AUDMUX_PORT1_SSI0, ptcr, pdcr);
  223. ptcr = IMX_AUDMUX_V2_PTCR_SYN;
  224. pdcr = IMX_AUDMUX_V2_PDCR_RXDSEL(MX31_AUDMUX_PORT1_SSI0);
  225. imx_audmux_v2_configure_port(MX31_AUDMUX_PORT5_SSI_PINS_5, ptcr, pdcr);
  226. wm1133_ev1_snd_device = platform_device_alloc("soc-audio", -1);
  227. if (!wm1133_ev1_snd_device)
  228. return -ENOMEM;
  229. platform_set_drvdata(wm1133_ev1_snd_device, &wm1133_ev1);
  230. ret = platform_device_add(wm1133_ev1_snd_device);
  231. if (ret)
  232. platform_device_put(wm1133_ev1_snd_device);
  233. return ret;
  234. }
  235. module_init(wm1133_ev1_audio_init);
  236. static void __exit wm1133_ev1_audio_exit(void)
  237. {
  238. platform_device_unregister(wm1133_ev1_snd_device);
  239. }
  240. module_exit(wm1133_ev1_audio_exit);
  241. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  242. MODULE_DESCRIPTION("Audio for WM1133-EV1 on i.MX31ADS");
  243. MODULE_LICENSE("GPL");