neo1973_wm8753.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * neo1973_wm8753.c -- SoC audio for Openmoko Neo1973 and Freerunner devices
  3. *
  4. * Copyright 2007 Openmoko Inc
  5. * Author: Graeme Gregory <graeme@openmoko.org>
  6. * Copyright 2007 Wolfson Microelectronics PLC.
  7. * Author: Graeme Gregory
  8. * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
  9. * Copyright 2009 Wolfson Microelectronics
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/gpio.h>
  19. #include <sound/soc.h>
  20. #include <mach/gpio-samsung.h>
  21. #include <asm/mach-types.h>
  22. #include "regs-iis.h"
  23. #include "../codecs/wm8753.h"
  24. #include "s3c24xx-i2s.h"
  25. static int neo1973_hifi_hw_params(struct snd_pcm_substream *substream,
  26. struct snd_pcm_hw_params *params)
  27. {
  28. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  29. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  30. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  31. unsigned int pll_out = 0, bclk = 0;
  32. int ret = 0;
  33. unsigned long iis_clkrate;
  34. iis_clkrate = s3c24xx_i2s_get_clockrate();
  35. switch (params_rate(params)) {
  36. case 8000:
  37. case 16000:
  38. pll_out = 12288000;
  39. break;
  40. case 48000:
  41. bclk = WM8753_BCLK_DIV_4;
  42. pll_out = 12288000;
  43. break;
  44. case 96000:
  45. bclk = WM8753_BCLK_DIV_2;
  46. pll_out = 12288000;
  47. break;
  48. case 11025:
  49. bclk = WM8753_BCLK_DIV_16;
  50. pll_out = 11289600;
  51. break;
  52. case 22050:
  53. bclk = WM8753_BCLK_DIV_8;
  54. pll_out = 11289600;
  55. break;
  56. case 44100:
  57. bclk = WM8753_BCLK_DIV_4;
  58. pll_out = 11289600;
  59. break;
  60. case 88200:
  61. bclk = WM8753_BCLK_DIV_2;
  62. pll_out = 11289600;
  63. break;
  64. }
  65. /* set the codec system clock for DAC and ADC */
  66. ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
  67. SND_SOC_CLOCK_IN);
  68. if (ret < 0)
  69. return ret;
  70. /* set MCLK division for sample rate */
  71. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
  72. S3C2410_IISMOD_32FS);
  73. if (ret < 0)
  74. return ret;
  75. /* set codec BCLK division for sample rate */
  76. ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_BCLKDIV, bclk);
  77. if (ret < 0)
  78. return ret;
  79. /* set prescaler division for sample rate */
  80. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  81. S3C24XX_PRESCALE(4, 4));
  82. if (ret < 0)
  83. return ret;
  84. /* codec PLL input is PCLK/4 */
  85. ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0,
  86. iis_clkrate / 4, pll_out);
  87. if (ret < 0)
  88. return ret;
  89. return 0;
  90. }
  91. static int neo1973_hifi_hw_free(struct snd_pcm_substream *substream)
  92. {
  93. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  94. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  95. /* disable the PLL */
  96. return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0, 0);
  97. }
  98. /*
  99. * Neo1973 WM8753 HiFi DAI opserations.
  100. */
  101. static struct snd_soc_ops neo1973_hifi_ops = {
  102. .hw_params = neo1973_hifi_hw_params,
  103. .hw_free = neo1973_hifi_hw_free,
  104. };
  105. static int neo1973_voice_hw_params(struct snd_pcm_substream *substream,
  106. struct snd_pcm_hw_params *params)
  107. {
  108. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  109. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  110. unsigned int pcmdiv = 0;
  111. int ret = 0;
  112. unsigned long iis_clkrate;
  113. iis_clkrate = s3c24xx_i2s_get_clockrate();
  114. if (params_rate(params) != 8000)
  115. return -EINVAL;
  116. if (params_channels(params) != 1)
  117. return -EINVAL;
  118. pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
  119. /* set the codec system clock for DAC and ADC */
  120. ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK, 12288000,
  121. SND_SOC_CLOCK_IN);
  122. if (ret < 0)
  123. return ret;
  124. /* set codec PCM division for sample rate */
  125. ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV, pcmdiv);
  126. if (ret < 0)
  127. return ret;
  128. /* configure and enable PLL for 12.288MHz output */
  129. ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0,
  130. iis_clkrate / 4, 12288000);
  131. if (ret < 0)
  132. return ret;
  133. return 0;
  134. }
  135. static int neo1973_voice_hw_free(struct snd_pcm_substream *substream)
  136. {
  137. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  138. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  139. /* disable the PLL */
  140. return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0, 0);
  141. }
  142. static struct snd_soc_ops neo1973_voice_ops = {
  143. .hw_params = neo1973_voice_hw_params,
  144. .hw_free = neo1973_voice_hw_free,
  145. };
  146. static int gta02_speaker_enabled;
  147. static int lm4853_set_spk(struct snd_kcontrol *kcontrol,
  148. struct snd_ctl_elem_value *ucontrol)
  149. {
  150. gta02_speaker_enabled = ucontrol->value.integer.value[0];
  151. gpio_set_value(S3C2410_GPJ(2), !gta02_speaker_enabled);
  152. return 0;
  153. }
  154. static int lm4853_get_spk(struct snd_kcontrol *kcontrol,
  155. struct snd_ctl_elem_value *ucontrol)
  156. {
  157. ucontrol->value.integer.value[0] = gta02_speaker_enabled;
  158. return 0;
  159. }
  160. static int lm4853_event(struct snd_soc_dapm_widget *w,
  161. struct snd_kcontrol *k, int event)
  162. {
  163. gpio_set_value(S3C2410_GPJ(1), SND_SOC_DAPM_EVENT_OFF(event));
  164. return 0;
  165. }
  166. static const struct snd_soc_dapm_widget neo1973_wm8753_dapm_widgets[] = {
  167. SND_SOC_DAPM_LINE("GSM Line Out", NULL),
  168. SND_SOC_DAPM_LINE("GSM Line In", NULL),
  169. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  170. SND_SOC_DAPM_MIC("Handset Mic", NULL),
  171. SND_SOC_DAPM_SPK("Handset Spk", NULL),
  172. SND_SOC_DAPM_SPK("Stereo Out", lm4853_event),
  173. };
  174. static const struct snd_soc_dapm_route neo1973_wm8753_routes[] = {
  175. /* Connections to the GSM Module */
  176. {"GSM Line Out", NULL, "MONO1"},
  177. {"GSM Line Out", NULL, "MONO2"},
  178. {"RXP", NULL, "GSM Line In"},
  179. {"RXN", NULL, "GSM Line In"},
  180. /* Connections to Headset */
  181. {"MIC1", NULL, "Mic Bias"},
  182. {"Mic Bias", NULL, "Headset Mic"},
  183. /* Call Mic */
  184. {"MIC2", NULL, "Mic Bias"},
  185. {"MIC2N", NULL, "Mic Bias"},
  186. {"Mic Bias", NULL, "Handset Mic"},
  187. /* Connect the ALC pins */
  188. {"ACIN", NULL, "ACOP"},
  189. /* Connections to the amp */
  190. {"Stereo Out", NULL, "LOUT1"},
  191. {"Stereo Out", NULL, "ROUT1"},
  192. /* Call Speaker */
  193. {"Handset Spk", NULL, "LOUT2"},
  194. {"Handset Spk", NULL, "ROUT2"},
  195. };
  196. static const struct snd_kcontrol_new neo1973_wm8753_controls[] = {
  197. SOC_DAPM_PIN_SWITCH("GSM Line Out"),
  198. SOC_DAPM_PIN_SWITCH("GSM Line In"),
  199. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  200. SOC_DAPM_PIN_SWITCH("Handset Mic"),
  201. SOC_DAPM_PIN_SWITCH("Handset Spk"),
  202. SOC_DAPM_PIN_SWITCH("Stereo Out"),
  203. SOC_SINGLE_BOOL_EXT("Amp Spk Switch", 0,
  204. lm4853_get_spk,
  205. lm4853_set_spk),
  206. };
  207. static int neo1973_wm8753_init(struct snd_soc_pcm_runtime *rtd)
  208. {
  209. struct snd_soc_card *card = rtd->card;
  210. /* set endpoints to default off mode */
  211. snd_soc_dapm_disable_pin(&card->dapm, "GSM Line Out");
  212. snd_soc_dapm_disable_pin(&card->dapm, "GSM Line In");
  213. snd_soc_dapm_disable_pin(&card->dapm, "Headset Mic");
  214. snd_soc_dapm_disable_pin(&card->dapm, "Handset Mic");
  215. snd_soc_dapm_disable_pin(&card->dapm, "Stereo Out");
  216. snd_soc_dapm_disable_pin(&card->dapm, "Handset Spk");
  217. /* allow audio paths from the GSM modem to run during suspend */
  218. snd_soc_dapm_ignore_suspend(&card->dapm, "GSM Line Out");
  219. snd_soc_dapm_ignore_suspend(&card->dapm, "GSM Line In");
  220. snd_soc_dapm_ignore_suspend(&card->dapm, "Headset Mic");
  221. snd_soc_dapm_ignore_suspend(&card->dapm, "Handset Mic");
  222. snd_soc_dapm_ignore_suspend(&card->dapm, "Stereo Out");
  223. snd_soc_dapm_ignore_suspend(&card->dapm, "Handset Spk");
  224. return 0;
  225. }
  226. static struct snd_soc_dai_link neo1973_dai[] = {
  227. { /* Hifi Playback - for similatious use with voice below */
  228. .name = "WM8753",
  229. .stream_name = "WM8753 HiFi",
  230. .platform_name = "s3c24xx-iis",
  231. .cpu_dai_name = "s3c24xx-iis",
  232. .codec_dai_name = "wm8753-hifi",
  233. .codec_name = "wm8753.0-001a",
  234. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  235. SND_SOC_DAIFMT_CBM_CFM,
  236. .init = neo1973_wm8753_init,
  237. .ops = &neo1973_hifi_ops,
  238. },
  239. { /* Voice via BT */
  240. .name = "Bluetooth",
  241. .stream_name = "Voice",
  242. .cpu_dai_name = "bt-sco-pcm",
  243. .codec_dai_name = "wm8753-voice",
  244. .codec_name = "wm8753.0-001a",
  245. .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF |
  246. SND_SOC_DAIFMT_CBS_CFS,
  247. .ops = &neo1973_voice_ops,
  248. },
  249. };
  250. static struct snd_soc_aux_dev neo1973_aux_devs[] = {
  251. {
  252. .name = "dfbmcs320",
  253. .codec_name = "dfbmcs320.0",
  254. },
  255. };
  256. static struct snd_soc_codec_conf neo1973_codec_conf[] = {
  257. {
  258. .dev_name = "lm4857.0-007c",
  259. .name_prefix = "Amp",
  260. },
  261. };
  262. static const struct gpio neo1973_gta02_gpios[] = {
  263. { S3C2410_GPJ(2), GPIOF_OUT_INIT_HIGH, "GTA02_HP_IN" },
  264. { S3C2410_GPJ(1), GPIOF_OUT_INIT_HIGH, "GTA02_AMP_SHUT" },
  265. };
  266. static struct snd_soc_card neo1973 = {
  267. .name = "neo1973",
  268. .owner = THIS_MODULE,
  269. .dai_link = neo1973_dai,
  270. .num_links = ARRAY_SIZE(neo1973_dai),
  271. .aux_dev = neo1973_aux_devs,
  272. .num_aux_devs = ARRAY_SIZE(neo1973_aux_devs),
  273. .codec_conf = neo1973_codec_conf,
  274. .num_configs = ARRAY_SIZE(neo1973_codec_conf),
  275. .controls = neo1973_wm8753_controls,
  276. .num_controls = ARRAY_SIZE(neo1973_wm8753_controls),
  277. .dapm_widgets = neo1973_wm8753_dapm_widgets,
  278. .num_dapm_widgets = ARRAY_SIZE(neo1973_wm8753_dapm_widgets),
  279. .dapm_routes = neo1973_wm8753_routes,
  280. .num_dapm_routes = ARRAY_SIZE(neo1973_wm8753_routes),
  281. .fully_routed = true,
  282. };
  283. static struct platform_device *neo1973_snd_device;
  284. static int __init neo1973_init(void)
  285. {
  286. int ret;
  287. if (!machine_is_neo1973_gta02())
  288. return -ENODEV;
  289. if (machine_is_neo1973_gta02()) {
  290. neo1973.name = "neo1973gta02";
  291. neo1973.num_aux_devs = 1;
  292. ret = gpio_request_array(neo1973_gta02_gpios,
  293. ARRAY_SIZE(neo1973_gta02_gpios));
  294. if (ret)
  295. return ret;
  296. }
  297. neo1973_snd_device = platform_device_alloc("soc-audio", -1);
  298. if (!neo1973_snd_device) {
  299. ret = -ENOMEM;
  300. goto err_gpio_free;
  301. }
  302. platform_set_drvdata(neo1973_snd_device, &neo1973);
  303. ret = platform_device_add(neo1973_snd_device);
  304. if (ret)
  305. goto err_put_device;
  306. return 0;
  307. err_put_device:
  308. platform_device_put(neo1973_snd_device);
  309. err_gpio_free:
  310. if (machine_is_neo1973_gta02()) {
  311. gpio_free_array(neo1973_gta02_gpios,
  312. ARRAY_SIZE(neo1973_gta02_gpios));
  313. }
  314. return ret;
  315. }
  316. module_init(neo1973_init);
  317. static void __exit neo1973_exit(void)
  318. {
  319. platform_device_unregister(neo1973_snd_device);
  320. if (machine_is_neo1973_gta02()) {
  321. gpio_free_array(neo1973_gta02_gpios,
  322. ARRAY_SIZE(neo1973_gta02_gpios));
  323. }
  324. }
  325. module_exit(neo1973_exit);
  326. /* Module information */
  327. MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org, www.openmoko.org");
  328. MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 and Frerunner");
  329. MODULE_LICENSE("GPL");