neo1973_wm8753.c 11 KB

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