neo1973_wm8753.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 codec DAI configuration */
  66. ret = snd_soc_dai_set_fmt(codec_dai,
  67. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  68. SND_SOC_DAIFMT_CBM_CFM);
  69. if (ret < 0)
  70. return ret;
  71. /* set cpu DAI configuration */
  72. ret = snd_soc_dai_set_fmt(cpu_dai,
  73. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  74. SND_SOC_DAIFMT_CBM_CFM);
  75. if (ret < 0)
  76. return ret;
  77. /* set the codec system clock for DAC and ADC */
  78. ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
  79. SND_SOC_CLOCK_IN);
  80. if (ret < 0)
  81. return ret;
  82. /* set MCLK division for sample rate */
  83. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
  84. S3C2410_IISMOD_32FS);
  85. if (ret < 0)
  86. return ret;
  87. /* set codec BCLK division for sample rate */
  88. ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_BCLKDIV, bclk);
  89. if (ret < 0)
  90. return ret;
  91. /* set prescaler division for sample rate */
  92. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  93. S3C24XX_PRESCALE(4, 4));
  94. if (ret < 0)
  95. return ret;
  96. /* codec PLL input is PCLK/4 */
  97. ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0,
  98. iis_clkrate / 4, pll_out);
  99. if (ret < 0)
  100. return ret;
  101. return 0;
  102. }
  103. static int neo1973_hifi_hw_free(struct snd_pcm_substream *substream)
  104. {
  105. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  106. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  107. /* disable the PLL */
  108. return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0, 0);
  109. }
  110. /*
  111. * Neo1973 WM8753 HiFi DAI opserations.
  112. */
  113. static struct snd_soc_ops neo1973_hifi_ops = {
  114. .hw_params = neo1973_hifi_hw_params,
  115. .hw_free = neo1973_hifi_hw_free,
  116. };
  117. static int neo1973_voice_hw_params(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, 12288000,
  139. 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, pcmdiv);
  144. if (ret < 0)
  145. return ret;
  146. /* configure and enable PLL for 12.288MHz output */
  147. ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0,
  148. iis_clkrate / 4, 12288000);
  149. if (ret < 0)
  150. return ret;
  151. return 0;
  152. }
  153. static int neo1973_voice_hw_free(struct snd_pcm_substream *substream)
  154. {
  155. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  156. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  157. /* disable the PLL */
  158. return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0, 0);
  159. }
  160. static struct snd_soc_ops neo1973_voice_ops = {
  161. .hw_params = neo1973_voice_hw_params,
  162. .hw_free = neo1973_voice_hw_free,
  163. };
  164. static int gta02_speaker_enabled;
  165. static int lm4853_set_spk(struct snd_kcontrol *kcontrol,
  166. struct snd_ctl_elem_value *ucontrol)
  167. {
  168. gta02_speaker_enabled = ucontrol->value.integer.value[0];
  169. gpio_set_value(S3C2410_GPJ(2), !gta02_speaker_enabled);
  170. return 0;
  171. }
  172. static int lm4853_get_spk(struct snd_kcontrol *kcontrol,
  173. struct snd_ctl_elem_value *ucontrol)
  174. {
  175. ucontrol->value.integer.value[0] = gta02_speaker_enabled;
  176. return 0;
  177. }
  178. static int lm4853_event(struct snd_soc_dapm_widget *w,
  179. struct snd_kcontrol *k, int event)
  180. {
  181. gpio_set_value(S3C2410_GPJ(1), SND_SOC_DAPM_EVENT_OFF(event));
  182. return 0;
  183. }
  184. static const struct snd_soc_dapm_widget neo1973_wm8753_dapm_widgets[] = {
  185. SND_SOC_DAPM_LINE("GSM Line Out", NULL),
  186. SND_SOC_DAPM_LINE("GSM Line In", NULL),
  187. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  188. SND_SOC_DAPM_MIC("Handset Mic", NULL),
  189. SND_SOC_DAPM_SPK("Handset Spk", NULL),
  190. SND_SOC_DAPM_SPK("Stereo Out", lm4853_event),
  191. };
  192. static const struct snd_soc_dapm_route neo1973_wm8753_routes[] = {
  193. /* Connections to the GSM Module */
  194. {"GSM Line Out", NULL, "MONO1"},
  195. {"GSM Line Out", NULL, "MONO2"},
  196. {"RXP", NULL, "GSM Line In"},
  197. {"RXN", NULL, "GSM Line In"},
  198. /* Connections to Headset */
  199. {"MIC1", NULL, "Mic Bias"},
  200. {"Mic Bias", NULL, "Headset Mic"},
  201. /* Call Mic */
  202. {"MIC2", NULL, "Mic Bias"},
  203. {"MIC2N", NULL, "Mic Bias"},
  204. {"Mic Bias", NULL, "Handset Mic"},
  205. /* Connect the ALC pins */
  206. {"ACIN", NULL, "ACOP"},
  207. /* Connections to the amp */
  208. {"Stereo Out", NULL, "LOUT1"},
  209. {"Stereo Out", NULL, "ROUT1"},
  210. /* Call Speaker */
  211. {"Handset Spk", NULL, "LOUT2"},
  212. {"Handset Spk", NULL, "ROUT2"},
  213. };
  214. static const struct snd_kcontrol_new neo1973_wm8753_controls[] = {
  215. SOC_DAPM_PIN_SWITCH("GSM Line Out"),
  216. SOC_DAPM_PIN_SWITCH("GSM Line In"),
  217. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  218. SOC_DAPM_PIN_SWITCH("Handset Mic"),
  219. SOC_DAPM_PIN_SWITCH("Handset Spk"),
  220. SOC_DAPM_PIN_SWITCH("Stereo Out"),
  221. SOC_SINGLE_BOOL_EXT("Amp Spk Switch", 0,
  222. lm4853_get_spk,
  223. lm4853_set_spk),
  224. };
  225. static int neo1973_wm8753_init(struct snd_soc_pcm_runtime *rtd)
  226. {
  227. struct snd_soc_card *card = rtd->card;
  228. /* set endpoints to default off mode */
  229. snd_soc_dapm_disable_pin(&card->dapm, "GSM Line Out");
  230. snd_soc_dapm_disable_pin(&card->dapm, "GSM Line In");
  231. snd_soc_dapm_disable_pin(&card->dapm, "Headset Mic");
  232. snd_soc_dapm_disable_pin(&card->dapm, "Handset Mic");
  233. snd_soc_dapm_disable_pin(&card->dapm, "Stereo Out");
  234. snd_soc_dapm_disable_pin(&card->dapm, "Handset Spk");
  235. /* allow audio paths from the GSM modem to run during suspend */
  236. snd_soc_dapm_ignore_suspend(&card->dapm, "GSM Line Out");
  237. snd_soc_dapm_ignore_suspend(&card->dapm, "GSM Line In");
  238. snd_soc_dapm_ignore_suspend(&card->dapm, "Headset Mic");
  239. snd_soc_dapm_ignore_suspend(&card->dapm, "Handset Mic");
  240. snd_soc_dapm_ignore_suspend(&card->dapm, "Stereo Out");
  241. snd_soc_dapm_ignore_suspend(&card->dapm, "Handset Spk");
  242. return 0;
  243. }
  244. static struct snd_soc_dai_link neo1973_dai[] = {
  245. { /* Hifi Playback - for similatious use with voice below */
  246. .name = "WM8753",
  247. .stream_name = "WM8753 HiFi",
  248. .platform_name = "s3c24xx-iis",
  249. .cpu_dai_name = "s3c24xx-iis",
  250. .codec_dai_name = "wm8753-hifi",
  251. .codec_name = "wm8753.0-001a",
  252. .init = neo1973_wm8753_init,
  253. .ops = &neo1973_hifi_ops,
  254. },
  255. { /* Voice via BT */
  256. .name = "Bluetooth",
  257. .stream_name = "Voice",
  258. .cpu_dai_name = "bt-sco-pcm",
  259. .codec_dai_name = "wm8753-voice",
  260. .codec_name = "wm8753.0-001a",
  261. .ops = &neo1973_voice_ops,
  262. },
  263. };
  264. static struct snd_soc_aux_dev neo1973_aux_devs[] = {
  265. {
  266. .name = "dfbmcs320",
  267. .codec_name = "dfbmcs320.0",
  268. },
  269. };
  270. static struct snd_soc_codec_conf neo1973_codec_conf[] = {
  271. {
  272. .dev_name = "lm4857.0-007c",
  273. .name_prefix = "Amp",
  274. },
  275. };
  276. static const struct gpio neo1973_gta02_gpios[] = {
  277. { S3C2410_GPJ(2), GPIOF_OUT_INIT_HIGH, "GTA02_HP_IN" },
  278. { S3C2410_GPJ(1), GPIOF_OUT_INIT_HIGH, "GTA02_AMP_SHUT" },
  279. };
  280. static struct snd_soc_card neo1973 = {
  281. .name = "neo1973",
  282. .owner = THIS_MODULE,
  283. .dai_link = neo1973_dai,
  284. .num_links = ARRAY_SIZE(neo1973_dai),
  285. .aux_dev = neo1973_aux_devs,
  286. .num_aux_devs = ARRAY_SIZE(neo1973_aux_devs),
  287. .codec_conf = neo1973_codec_conf,
  288. .num_configs = ARRAY_SIZE(neo1973_codec_conf),
  289. .controls = neo1973_wm8753_controls,
  290. .num_controls = ARRAY_SIZE(neo1973_wm8753_controls),
  291. .dapm_widgets = neo1973_wm8753_dapm_widgets,
  292. .num_dapm_widgets = ARRAY_SIZE(neo1973_wm8753_dapm_widgets),
  293. .dapm_routes = neo1973_wm8753_routes,
  294. .num_dapm_routes = ARRAY_SIZE(neo1973_wm8753_routes),
  295. .fully_routed = true,
  296. };
  297. static struct platform_device *neo1973_snd_device;
  298. static int __init neo1973_init(void)
  299. {
  300. int ret;
  301. if (!machine_is_neo1973_gta02())
  302. return -ENODEV;
  303. if (machine_is_neo1973_gta02()) {
  304. neo1973.name = "neo1973gta02";
  305. neo1973.num_aux_devs = 1;
  306. ret = gpio_request_array(neo1973_gta02_gpios,
  307. ARRAY_SIZE(neo1973_gta02_gpios));
  308. if (ret)
  309. return ret;
  310. }
  311. neo1973_snd_device = platform_device_alloc("soc-audio", -1);
  312. if (!neo1973_snd_device) {
  313. ret = -ENOMEM;
  314. goto err_gpio_free;
  315. }
  316. platform_set_drvdata(neo1973_snd_device, &neo1973);
  317. ret = platform_device_add(neo1973_snd_device);
  318. if (ret)
  319. goto err_put_device;
  320. return 0;
  321. err_put_device:
  322. platform_device_put(neo1973_snd_device);
  323. err_gpio_free:
  324. if (machine_is_neo1973_gta02()) {
  325. gpio_free_array(neo1973_gta02_gpios,
  326. ARRAY_SIZE(neo1973_gta02_gpios));
  327. }
  328. return ret;
  329. }
  330. module_init(neo1973_init);
  331. static void __exit neo1973_exit(void)
  332. {
  333. platform_device_unregister(neo1973_snd_device);
  334. if (machine_is_neo1973_gta02()) {
  335. gpio_free_array(neo1973_gta02_gpios,
  336. ARRAY_SIZE(neo1973_gta02_gpios));
  337. }
  338. }
  339. module_exit(neo1973_exit);
  340. /* Module information */
  341. MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org, www.openmoko.org");
  342. MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 and Frerunner");
  343. MODULE_LICENSE("GPL");