neo1973_gta02_wm8753.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * neo1973_gta02_wm8753.c -- SoC audio for Openmoko Freerunner(GTA02)
  3. *
  4. * Copyright 2007 Openmoko Inc
  5. * Author: Graeme Gregory <graeme@openmoko.org>
  6. * Copyright 2007 Wolfson Microelectronics PLC.
  7. * Author: Graeme Gregory <linux@wolfsonmicro.com>
  8. * Copyright 2009 Wolfson Microelectronics
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. */
  15. #include <linux/gpio.h>
  16. #include <sound/soc.h>
  17. #include <asm/mach-types.h>
  18. #include <plat/regs-iis.h>
  19. #include <mach/gta02.h>
  20. #include "../codecs/wm8753.h"
  21. #include "s3c24xx-i2s.h"
  22. static struct snd_soc_card neo1973_gta02;
  23. static int neo1973_gta02_hifi_hw_params(struct snd_pcm_substream *substream,
  24. struct snd_pcm_hw_params *params)
  25. {
  26. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  27. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  28. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  29. unsigned int pll_out = 0, bclk = 0;
  30. int ret = 0;
  31. unsigned long iis_clkrate;
  32. iis_clkrate = s3c24xx_i2s_get_clockrate();
  33. switch (params_rate(params)) {
  34. case 8000:
  35. case 16000:
  36. pll_out = 12288000;
  37. break;
  38. case 48000:
  39. bclk = WM8753_BCLK_DIV_4;
  40. pll_out = 12288000;
  41. break;
  42. case 96000:
  43. bclk = WM8753_BCLK_DIV_2;
  44. pll_out = 12288000;
  45. break;
  46. case 11025:
  47. bclk = WM8753_BCLK_DIV_16;
  48. pll_out = 11289600;
  49. break;
  50. case 22050:
  51. bclk = WM8753_BCLK_DIV_8;
  52. pll_out = 11289600;
  53. break;
  54. case 44100:
  55. bclk = WM8753_BCLK_DIV_4;
  56. pll_out = 11289600;
  57. break;
  58. case 88200:
  59. bclk = WM8753_BCLK_DIV_2;
  60. pll_out = 11289600;
  61. break;
  62. }
  63. /* set codec DAI configuration */
  64. ret = snd_soc_dai_set_fmt(codec_dai,
  65. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  66. SND_SOC_DAIFMT_CBM_CFM);
  67. if (ret < 0)
  68. return ret;
  69. /* set cpu DAI configuration */
  70. ret = snd_soc_dai_set_fmt(cpu_dai,
  71. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  72. SND_SOC_DAIFMT_CBM_CFM);
  73. if (ret < 0)
  74. return ret;
  75. /* set the codec system clock for DAC and ADC */
  76. ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
  77. SND_SOC_CLOCK_IN);
  78. if (ret < 0)
  79. return ret;
  80. /* set MCLK division for sample rate */
  81. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
  82. S3C2410_IISMOD_32FS);
  83. if (ret < 0)
  84. return ret;
  85. /* set codec BCLK division for sample rate */
  86. ret = snd_soc_dai_set_clkdiv(codec_dai,
  87. WM8753_BCLKDIV, bclk);
  88. if (ret < 0)
  89. return ret;
  90. /* set prescaler division for sample rate */
  91. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  92. S3C24XX_PRESCALE(4, 4));
  93. if (ret < 0)
  94. return ret;
  95. /* codec PLL input is PCLK/4 */
  96. ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0,
  97. iis_clkrate / 4, pll_out);
  98. if (ret < 0)
  99. return ret;
  100. return 0;
  101. }
  102. static int neo1973_gta02_hifi_hw_free(struct snd_pcm_substream *substream)
  103. {
  104. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  105. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  106. /* disable the PLL */
  107. return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0, 0);
  108. }
  109. /*
  110. * Neo1973 WM8753 HiFi DAI opserations.
  111. */
  112. static struct snd_soc_ops neo1973_gta02_hifi_ops = {
  113. .hw_params = neo1973_gta02_hifi_hw_params,
  114. .hw_free = neo1973_gta02_hifi_hw_free,
  115. };
  116. static int neo1973_gta02_voice_hw_params(
  117. struct snd_pcm_substream *substream,
  118. struct snd_pcm_hw_params *params)
  119. {
  120. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  121. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  122. unsigned int pcmdiv = 0;
  123. int ret = 0;
  124. unsigned long iis_clkrate;
  125. iis_clkrate = s3c24xx_i2s_get_clockrate();
  126. if (params_rate(params) != 8000)
  127. return -EINVAL;
  128. if (params_channels(params) != 1)
  129. return -EINVAL;
  130. pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
  131. /* todo: gg check mode (DSP_B) against CSR datasheet */
  132. /* set codec DAI configuration */
  133. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_B |
  134. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  135. if (ret < 0)
  136. return ret;
  137. /* set the codec system clock for DAC and ADC */
  138. ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK,
  139. 12288000, SND_SOC_CLOCK_IN);
  140. if (ret < 0)
  141. return ret;
  142. /* set codec PCM division for sample rate */
  143. ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV,
  144. pcmdiv);
  145. if (ret < 0)
  146. return ret;
  147. /* configure and enable PLL for 12.288MHz output */
  148. ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0,
  149. iis_clkrate / 4, 12288000);
  150. if (ret < 0)
  151. return ret;
  152. return 0;
  153. }
  154. static int neo1973_gta02_voice_hw_free(struct snd_pcm_substream *substream)
  155. {
  156. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  157. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  158. /* disable the PLL */
  159. return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0, 0);
  160. }
  161. static struct snd_soc_ops neo1973_gta02_voice_ops = {
  162. .hw_params = neo1973_gta02_voice_hw_params,
  163. .hw_free = neo1973_gta02_voice_hw_free,
  164. };
  165. static int gta02_speaker_enabled;
  166. static int lm4853_set_spk(struct snd_kcontrol *kcontrol,
  167. struct snd_ctl_elem_value *ucontrol)
  168. {
  169. gta02_speaker_enabled = ucontrol->value.integer.value[0];
  170. gpio_set_value(GTA02_GPIO_HP_IN, !gta02_speaker_enabled);
  171. return 0;
  172. }
  173. static int lm4853_get_spk(struct snd_kcontrol *kcontrol,
  174. struct snd_ctl_elem_value *ucontrol)
  175. {
  176. ucontrol->value.integer.value[0] = gta02_speaker_enabled;
  177. return 0;
  178. }
  179. static int lm4853_event(struct snd_soc_dapm_widget *w,
  180. struct snd_kcontrol *k, int event)
  181. {
  182. gpio_set_value(GTA02_GPIO_AMP_SHUT, SND_SOC_DAPM_EVENT_OFF(event));
  183. return 0;
  184. }
  185. static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
  186. SND_SOC_DAPM_SPK("Stereo Out", lm4853_event),
  187. SND_SOC_DAPM_LINE("GSM Line Out", NULL),
  188. SND_SOC_DAPM_LINE("GSM Line In", NULL),
  189. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  190. SND_SOC_DAPM_MIC("Handset Mic", NULL),
  191. SND_SOC_DAPM_SPK("Handset Spk", NULL),
  192. };
  193. /* example machine audio_mapnections */
  194. static const struct snd_soc_dapm_route audio_map[] = {
  195. /* Connections to the lm4853 amp */
  196. {"Stereo Out", NULL, "LOUT1"},
  197. {"Stereo Out", NULL, "ROUT1"},
  198. /* Connections to the GSM Module */
  199. {"GSM Line Out", NULL, "MONO1"},
  200. {"GSM Line Out", NULL, "MONO2"},
  201. {"RXP", NULL, "GSM Line In"},
  202. {"RXN", NULL, "GSM Line In"},
  203. /* Connections to Headset */
  204. {"MIC1", NULL, "Mic Bias"},
  205. {"Mic Bias", NULL, "Headset Mic"},
  206. /* Call Mic */
  207. {"MIC2", NULL, "Mic Bias"},
  208. {"MIC2N", NULL, "Mic Bias"},
  209. {"Mic Bias", NULL, "Handset Mic"},
  210. /* Call Speaker */
  211. {"Handset Spk", NULL, "LOUT2"},
  212. {"Handset Spk", NULL, "ROUT2"},
  213. /* Connect the ALC pins */
  214. {"ACIN", NULL, "ACOP"},
  215. };
  216. static const struct snd_kcontrol_new wm8753_neo1973_gta02_controls[] = {
  217. SOC_DAPM_PIN_SWITCH("Stereo Out"),
  218. SOC_DAPM_PIN_SWITCH("GSM Line Out"),
  219. SOC_DAPM_PIN_SWITCH("GSM Line In"),
  220. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  221. SOC_DAPM_PIN_SWITCH("Handset Mic"),
  222. SOC_DAPM_PIN_SWITCH("Handset Spk"),
  223. SOC_SINGLE_BOOL_EXT("Amp Spk Switch", 0,
  224. lm4853_get_spk,
  225. lm4853_set_spk),
  226. };
  227. /*
  228. * This is an example machine initialisation for a wm8753 connected to a
  229. * neo1973 GTA02.
  230. */
  231. static int neo1973_gta02_wm8753_init(struct snd_soc_pcm_runtime *rtd)
  232. {
  233. struct snd_soc_codec *codec = rtd->codec;
  234. struct snd_soc_dapm_context *dapm = &codec->dapm;
  235. int err;
  236. /* set up NC codec pins */
  237. snd_soc_dapm_nc_pin(dapm, "OUT3");
  238. snd_soc_dapm_nc_pin(dapm, "OUT4");
  239. snd_soc_dapm_nc_pin(dapm, "LINE1");
  240. snd_soc_dapm_nc_pin(dapm, "LINE2");
  241. /* Add neo1973 gta02 specific widgets */
  242. snd_soc_dapm_new_controls(dapm, wm8753_dapm_widgets,
  243. ARRAY_SIZE(wm8753_dapm_widgets));
  244. /* add neo1973 gta02 specific controls */
  245. err = snd_soc_add_controls(codec, wm8753_neo1973_gta02_controls,
  246. ARRAY_SIZE(wm8753_neo1973_gta02_controls));
  247. if (err < 0)
  248. return err;
  249. /* set up neo1973 gta02 specific audio path audio_map */
  250. snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
  251. /* set endpoints to default off mode */
  252. snd_soc_dapm_disable_pin(dapm, "Stereo Out");
  253. snd_soc_dapm_disable_pin(dapm, "GSM Line Out");
  254. snd_soc_dapm_disable_pin(dapm, "GSM Line In");
  255. snd_soc_dapm_disable_pin(dapm, "Headset Mic");
  256. snd_soc_dapm_disable_pin(dapm, "Handset Mic");
  257. snd_soc_dapm_disable_pin(dapm, "Handset Spk");
  258. /* allow audio paths from the GSM modem to run during suspend */
  259. snd_soc_dapm_ignore_suspend(dapm, "Stereo Out");
  260. snd_soc_dapm_ignore_suspend(dapm, "GSM Line Out");
  261. snd_soc_dapm_ignore_suspend(dapm, "GSM Line In");
  262. snd_soc_dapm_ignore_suspend(dapm, "Headset Mic");
  263. snd_soc_dapm_ignore_suspend(dapm, "Handset Mic");
  264. snd_soc_dapm_ignore_suspend(dapm, "Handset Spk");
  265. snd_soc_dapm_sync(dapm);
  266. return 0;
  267. }
  268. /*
  269. * BT Codec DAI
  270. */
  271. static struct snd_soc_dai_driver bt_dai = {
  272. .name = "bluetooth-dai",
  273. .playback = {
  274. .channels_min = 1,
  275. .channels_max = 1,
  276. .rates = SNDRV_PCM_RATE_8000,
  277. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  278. .capture = {
  279. .channels_min = 1,
  280. .channels_max = 1,
  281. .rates = SNDRV_PCM_RATE_8000,
  282. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  283. };
  284. static struct snd_soc_dai_link neo1973_gta02_dai[] = {
  285. { /* Hifi Playback - for similatious use with voice below */
  286. .name = "WM8753",
  287. .stream_name = "WM8753 HiFi",
  288. .cpu_dai_name = "s3c24xx-iis",
  289. .codec_dai_name = "wm8753-hifi",
  290. .init = neo1973_gta02_wm8753_init,
  291. .platform_name = "samsung-audio",
  292. .codec_name = "wm8753-codec.0-001a",
  293. .ops = &neo1973_gta02_hifi_ops,
  294. },
  295. { /* Voice via BT */
  296. .name = "Bluetooth",
  297. .stream_name = "Voice",
  298. .cpu_dai_name = "bluetooth-dai",
  299. .codec_dai_name = "wm8753-voice",
  300. .ops = &neo1973_gta02_voice_ops,
  301. .codec_name = "wm8753-codec.0-001a",
  302. .platform_name = "samsung-audio",
  303. },
  304. };
  305. static struct snd_soc_card neo1973_gta02 = {
  306. .name = "neo1973-gta02",
  307. .dai_link = neo1973_gta02_dai,
  308. .num_links = ARRAY_SIZE(neo1973_gta02_dai),
  309. };
  310. static const struct gpio neo1973_gta02_gpios[] = {
  311. { GTA02_GPIO_HP_IN, GPIOF_OUT_INIT_HIGH, "GTA02_HP_IN" },
  312. { GTA02_GPIO_AMP_SHUT, GPIOF_OUT_INIT_HIGH, "GTA02_AMP_SHUT" },
  313. };
  314. static struct platform_device *neo1973_gta02_snd_device;
  315. static int __init neo1973_gta02_init(void)
  316. {
  317. int ret;
  318. if (!machine_is_neo1973_gta02()) {
  319. printk(KERN_INFO
  320. "Only GTA02 is supported by this ASoC driver\n");
  321. return -ENODEV;
  322. }
  323. ret = gpio_request_array(neo1973_gta02_gpios,
  324. ARRAY_SIZE(neo1973_gta02_gpios));
  325. if (ret)
  326. return ret;
  327. neo1973_gta02_snd_device = platform_device_alloc("soc-audio", -1);
  328. if (!neo1973_gta02_snd_device) {
  329. ret = -ENOMEM;
  330. goto err_gpio_free;
  331. }
  332. /* register bluetooth DAI here */
  333. ret = snd_soc_register_dai(&neo1973_gta02_snd_device->dev, &bt_dai);
  334. if (ret)
  335. goto err_put_device;
  336. platform_set_drvdata(neo1973_gta02_snd_device, &neo1973_gta02);
  337. ret = platform_device_add(neo1973_gta02_snd_device);
  338. if (ret)
  339. goto err_unregister_dai;
  340. return 0;
  341. err_unregister_dai:
  342. snd_soc_unregister_dai(&neo1973_gta02_snd_device->dev);
  343. err_put_device:
  344. platform_device_put(neo1973_gta02_snd_device);
  345. err_gpio_free:
  346. gpio_free_array(neo1973_gta02_gpios, ARRAY_SIZE(neo1973_gta02_gpios));
  347. return ret;
  348. }
  349. module_init(neo1973_gta02_init);
  350. static void __exit neo1973_gta02_exit(void)
  351. {
  352. snd_soc_unregister_dai(&neo1973_gta02_snd_device->dev);
  353. platform_device_unregister(neo1973_gta02_snd_device);
  354. gpio_free_array(neo1973_gta02_gpios, ARRAY_SIZE(neo1973_gta02_gpios));
  355. }
  356. module_exit(neo1973_gta02_exit);
  357. /* Module information */
  358. MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org");
  359. MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 GTA02");
  360. MODULE_LICENSE("GPL");