neo1973_gta02_wm8753.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * neo1973_gta02_wm8753.c -- SoC audio for Neo1973
  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/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/timer.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/platform_device.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/soc.h>
  23. #include <sound/soc-dapm.h>
  24. #include <asm/mach-types.h>
  25. #include <plat/regs-iis.h>
  26. #include <mach/regs-clock.h>
  27. #include <mach/regs-gpio.h>
  28. #include <mach/hardware.h>
  29. #include <asm/io.h>
  30. #include <mach/regs-gpioj.h>
  31. #include <mach/gta02.h>
  32. #include "../codecs/wm8753.h"
  33. #include "s3c24xx-pcm.h"
  34. #include "s3c24xx-i2s.h"
  35. static struct snd_soc_card neo1973_gta02;
  36. static int neo1973_gta02_hifi_hw_params(struct snd_pcm_substream *substream,
  37. struct snd_pcm_hw_params *params)
  38. {
  39. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  40. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  41. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  42. unsigned int pll_out = 0, bclk = 0;
  43. int ret = 0;
  44. unsigned long iis_clkrate;
  45. iis_clkrate = s3c24xx_i2s_get_clockrate();
  46. switch (params_rate(params)) {
  47. case 8000:
  48. case 16000:
  49. pll_out = 12288000;
  50. break;
  51. case 48000:
  52. bclk = WM8753_BCLK_DIV_4;
  53. pll_out = 12288000;
  54. break;
  55. case 96000:
  56. bclk = WM8753_BCLK_DIV_2;
  57. pll_out = 12288000;
  58. break;
  59. case 11025:
  60. bclk = WM8753_BCLK_DIV_16;
  61. pll_out = 11289600;
  62. break;
  63. case 22050:
  64. bclk = WM8753_BCLK_DIV_8;
  65. pll_out = 11289600;
  66. break;
  67. case 44100:
  68. bclk = WM8753_BCLK_DIV_4;
  69. pll_out = 11289600;
  70. break;
  71. case 88200:
  72. bclk = WM8753_BCLK_DIV_2;
  73. pll_out = 11289600;
  74. break;
  75. }
  76. /* set codec DAI configuration */
  77. ret = snd_soc_dai_set_fmt(codec_dai,
  78. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  79. SND_SOC_DAIFMT_CBM_CFM);
  80. if (ret < 0)
  81. return ret;
  82. /* set cpu DAI configuration */
  83. ret = snd_soc_dai_set_fmt(cpu_dai,
  84. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  85. SND_SOC_DAIFMT_CBM_CFM);
  86. if (ret < 0)
  87. return ret;
  88. /* set the codec system clock for DAC and ADC */
  89. ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
  90. SND_SOC_CLOCK_IN);
  91. if (ret < 0)
  92. return ret;
  93. /* set MCLK division for sample rate */
  94. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
  95. S3C2410_IISMOD_32FS);
  96. if (ret < 0)
  97. return ret;
  98. /* set codec BCLK division for sample rate */
  99. ret = snd_soc_dai_set_clkdiv(codec_dai,
  100. WM8753_BCLKDIV, bclk);
  101. if (ret < 0)
  102. return ret;
  103. /* set prescaler division for sample rate */
  104. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  105. S3C24XX_PRESCALE(4, 4));
  106. if (ret < 0)
  107. return ret;
  108. /* codec PLL input is PCLK/4 */
  109. ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1,
  110. iis_clkrate / 4, pll_out);
  111. if (ret < 0)
  112. return ret;
  113. return 0;
  114. }
  115. static int neo1973_gta02_hifi_hw_free(struct snd_pcm_substream *substream)
  116. {
  117. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  118. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  119. /* disable the PLL */
  120. return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0);
  121. }
  122. /*
  123. * Neo1973 WM8753 HiFi DAI opserations.
  124. */
  125. static struct snd_soc_ops neo1973_gta02_hifi_ops = {
  126. .hw_params = neo1973_gta02_hifi_hw_params,
  127. .hw_free = neo1973_gta02_hifi_hw_free,
  128. };
  129. static int neo1973_gta02_voice_hw_params(
  130. struct snd_pcm_substream *substream,
  131. struct snd_pcm_hw_params *params)
  132. {
  133. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  134. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  135. unsigned int pcmdiv = 0;
  136. int ret = 0;
  137. unsigned long iis_clkrate;
  138. iis_clkrate = s3c24xx_i2s_get_clockrate();
  139. if (params_rate(params) != 8000)
  140. return -EINVAL;
  141. if (params_channels(params) != 1)
  142. return -EINVAL;
  143. pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
  144. /* todo: gg check mode (DSP_B) against CSR datasheet */
  145. /* set codec DAI configuration */
  146. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_B |
  147. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  148. if (ret < 0)
  149. return ret;
  150. /* set the codec system clock for DAC and ADC */
  151. ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK,
  152. 12288000, SND_SOC_CLOCK_IN);
  153. if (ret < 0)
  154. return ret;
  155. /* set codec PCM division for sample rate */
  156. ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV,
  157. pcmdiv);
  158. if (ret < 0)
  159. return ret;
  160. /* configue and enable PLL for 12.288MHz output */
  161. ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2,
  162. iis_clkrate / 4, 12288000);
  163. if (ret < 0)
  164. return ret;
  165. return 0;
  166. }
  167. static int neo1973_gta02_voice_hw_free(struct snd_pcm_substream *substream)
  168. {
  169. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  170. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  171. /* disable the PLL */
  172. return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0);
  173. }
  174. static struct snd_soc_ops neo1973_gta02_voice_ops = {
  175. .hw_params = neo1973_gta02_voice_hw_params,
  176. .hw_free = neo1973_gta02_voice_hw_free,
  177. };
  178. #define LM4853_AMP 1
  179. #define LM4853_SPK 2
  180. static u8 lm4853_state;
  181. /* This has no effect, it exists only to maintain compatibility with
  182. * existing ALSA state files.
  183. */
  184. static int lm4853_set_state(struct snd_kcontrol *kcontrol,
  185. struct snd_ctl_elem_value *ucontrol)
  186. {
  187. int val = ucontrol->value.integer.value[0];
  188. if (val)
  189. lm4853_state |= LM4853_AMP;
  190. else
  191. lm4853_state &= ~LM4853_AMP;
  192. return 0;
  193. }
  194. static int lm4853_get_state(struct snd_kcontrol *kcontrol,
  195. struct snd_ctl_elem_value *ucontrol)
  196. {
  197. ucontrol->value.integer.value[0] = lm4853_state & LM4853_AMP;
  198. return 0;
  199. }
  200. static int lm4853_set_spk(struct snd_kcontrol *kcontrol,
  201. struct snd_ctl_elem_value *ucontrol)
  202. {
  203. int val = ucontrol->value.integer.value[0];
  204. if (val) {
  205. lm4853_state |= LM4853_SPK;
  206. s3c2410_gpio_setpin(GTA02_GPIO_HP_IN, 0);
  207. } else {
  208. lm4853_state &= ~LM4853_SPK;
  209. s3c2410_gpio_setpin(GTA02_GPIO_HP_IN, 1);
  210. }
  211. return 0;
  212. }
  213. static int lm4853_get_spk(struct snd_kcontrol *kcontrol,
  214. struct snd_ctl_elem_value *ucontrol)
  215. {
  216. ucontrol->value.integer.value[0] = (lm4853_state & LM4853_SPK) >> 1;
  217. return 0;
  218. }
  219. static int lm4853_event(struct snd_soc_dapm_widget *w,
  220. struct snd_kcontrol *k,
  221. int event)
  222. {
  223. if (SND_SOC_DAPM_EVENT_ON(event))
  224. s3c2410_gpio_setpin(GTA02_GPIO_AMP_SHUT, 0);
  225. if (SND_SOC_DAPM_EVENT_OFF(event))
  226. s3c2410_gpio_setpin(GTA02_GPIO_AMP_SHUT, 1);
  227. return 0;
  228. }
  229. static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
  230. SND_SOC_DAPM_SPK("Stereo Out", lm4853_event),
  231. SND_SOC_DAPM_LINE("GSM Line Out", NULL),
  232. SND_SOC_DAPM_LINE("GSM Line In", NULL),
  233. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  234. SND_SOC_DAPM_MIC("Handset Mic", NULL),
  235. SND_SOC_DAPM_SPK("Handset Spk", NULL),
  236. };
  237. /* example machine audio_mapnections */
  238. static const struct snd_soc_dapm_route audio_map[] = {
  239. /* Connections to the lm4853 amp */
  240. {"Stereo Out", NULL, "LOUT1"},
  241. {"Stereo Out", NULL, "ROUT1"},
  242. /* Connections to the GSM Module */
  243. {"GSM Line Out", NULL, "MONO1"},
  244. {"GSM Line Out", NULL, "MONO2"},
  245. {"RXP", NULL, "GSM Line In"},
  246. {"RXN", NULL, "GSM Line In"},
  247. /* Connections to Headset */
  248. {"MIC1", NULL, "Mic Bias"},
  249. {"Mic Bias", NULL, "Headset Mic"},
  250. /* Call Mic */
  251. {"MIC2", NULL, "Mic Bias"},
  252. {"MIC2N", NULL, "Mic Bias"},
  253. {"Mic Bias", NULL, "Handset Mic"},
  254. /* Call Speaker */
  255. {"Handset Spk", NULL, "LOUT2"},
  256. {"Handset Spk", NULL, "ROUT2"},
  257. /* Connect the ALC pins */
  258. {"ACIN", NULL, "ACOP"},
  259. };
  260. static const struct snd_kcontrol_new wm8753_neo1973_gta02_controls[] = {
  261. SOC_DAPM_PIN_SWITCH("Stereo Out"),
  262. SOC_DAPM_PIN_SWITCH("GSM Line Out"),
  263. SOC_DAPM_PIN_SWITCH("GSM Line In"),
  264. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  265. SOC_DAPM_PIN_SWITCH("Handset Mic"),
  266. SOC_DAPM_PIN_SWITCH("Handset Spk"),
  267. /* This has no effect, it exists only to maintain compatibility with
  268. * existing ALSA state files.
  269. */
  270. SOC_SINGLE_EXT("Amp State Switch", 6, 0, 1, 0,
  271. lm4853_get_state,
  272. lm4853_set_state),
  273. SOC_SINGLE_EXT("Amp Spk Switch", 7, 0, 1, 0,
  274. lm4853_get_spk,
  275. lm4853_set_spk),
  276. };
  277. /*
  278. * This is an example machine initialisation for a wm8753 connected to a
  279. * neo1973 GTA02.
  280. */
  281. static int neo1973_gta02_wm8753_init(struct snd_soc_codec *codec)
  282. {
  283. int err;
  284. /* set up NC codec pins */
  285. snd_soc_dapm_nc_pin(codec, "OUT3");
  286. snd_soc_dapm_nc_pin(codec, "OUT4");
  287. snd_soc_dapm_nc_pin(codec, "LINE1");
  288. snd_soc_dapm_nc_pin(codec, "LINE2");
  289. /* Add neo1973 gta02 specific widgets */
  290. snd_soc_dapm_new_controls(codec, wm8753_dapm_widgets,
  291. ARRAY_SIZE(wm8753_dapm_widgets));
  292. /* add neo1973 gta02 specific controls */
  293. err = snd_soc_add_controls(codec, wm8753_neo1973_gta02_controls,
  294. ARRAY_SIZE(wm8753_neo1973_gta02_controls));
  295. if (err < 0)
  296. return err;
  297. /* set up neo1973 gta02 specific audio path audio_map */
  298. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  299. /* set endpoints to default off mode */
  300. snd_soc_dapm_disable_pin(codec, "Stereo Out");
  301. snd_soc_dapm_disable_pin(codec, "GSM Line Out");
  302. snd_soc_dapm_disable_pin(codec, "GSM Line In");
  303. snd_soc_dapm_disable_pin(codec, "Headset Mic");
  304. snd_soc_dapm_disable_pin(codec, "Handset Mic");
  305. snd_soc_dapm_disable_pin(codec, "Handset Spk");
  306. snd_soc_dapm_sync(codec);
  307. return 0;
  308. }
  309. /*
  310. * BT Codec DAI
  311. */
  312. static struct snd_soc_dai bt_dai = {
  313. .name = "Bluetooth",
  314. .id = 0,
  315. .playback = {
  316. .channels_min = 1,
  317. .channels_max = 1,
  318. .rates = SNDRV_PCM_RATE_8000,
  319. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  320. .capture = {
  321. .channels_min = 1,
  322. .channels_max = 1,
  323. .rates = SNDRV_PCM_RATE_8000,
  324. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  325. };
  326. static struct snd_soc_dai_link neo1973_gta02_dai[] = {
  327. { /* Hifi Playback - for similatious use with voice below */
  328. .name = "WM8753",
  329. .stream_name = "WM8753 HiFi",
  330. .cpu_dai = &s3c24xx_i2s_dai,
  331. .codec_dai = &wm8753_dai[WM8753_DAI_HIFI],
  332. .init = neo1973_gta02_wm8753_init,
  333. .ops = &neo1973_gta02_hifi_ops,
  334. },
  335. { /* Voice via BT */
  336. .name = "Bluetooth",
  337. .stream_name = "Voice",
  338. .cpu_dai = &bt_dai,
  339. .codec_dai = &wm8753_dai[WM8753_DAI_VOICE],
  340. .ops = &neo1973_gta02_voice_ops,
  341. },
  342. };
  343. static struct snd_soc_card neo1973_gta02 = {
  344. .name = "neo1973-gta02",
  345. .platform = &s3c24xx_soc_platform,
  346. .dai_link = neo1973_gta02_dai,
  347. .num_links = ARRAY_SIZE(neo1973_gta02_dai),
  348. };
  349. static struct snd_soc_device neo1973_gta02_snd_devdata = {
  350. .card = &neo1973_gta02,
  351. .codec_dev = &soc_codec_dev_wm8753,
  352. };
  353. static struct platform_device *neo1973_gta02_snd_device;
  354. static int __init neo1973_gta02_init(void)
  355. {
  356. int ret;
  357. if (!machine_is_neo1973_gta02()) {
  358. printk(KERN_INFO
  359. "Only GTA02 is supported by this ASoC driver\n");
  360. return -ENODEV;
  361. }
  362. /* register bluetooth DAI here */
  363. ret = snd_soc_register_dai(&bt_dai);
  364. if (ret)
  365. return ret;
  366. neo1973_gta02_snd_device = platform_device_alloc("soc-audio", -1);
  367. if (!neo1973_gta02_snd_device)
  368. return -ENOMEM;
  369. platform_set_drvdata(neo1973_gta02_snd_device,
  370. &neo1973_gta02_snd_devdata);
  371. neo1973_gta02_snd_devdata.dev = &neo1973_gta02_snd_device->dev;
  372. ret = platform_device_add(neo1973_gta02_snd_device);
  373. if (ret) {
  374. platform_device_put(neo1973_gta02_snd_device);
  375. return ret;
  376. }
  377. /* Initialise GPIOs used by amp */
  378. s3c2410_gpio_cfgpin(GTA02_GPIO_HP_IN, S3C2410_GPIO_OUTPUT);
  379. s3c2410_gpio_cfgpin(GTA02_GPIO_AMP_SHUT, S3C2410_GPIO_OUTPUT);
  380. /* Amp off by default */
  381. s3c2410_gpio_setpin(GTA02_GPIO_AMP_SHUT, 1);
  382. /* Speaker off by default */
  383. s3c2410_gpio_setpin(GTA02_GPIO_HP_IN, 1);
  384. return ret;
  385. }
  386. module_init(neo1973_gta02_init);
  387. static void __exit neo1973_gta02_exit(void)
  388. {
  389. snd_soc_unregister_dai(&bt_dai);
  390. platform_device_unregister(neo1973_gta02_snd_device);
  391. }
  392. module_exit(neo1973_gta02_exit);
  393. /* Module information */
  394. MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org");
  395. MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 GTA02");
  396. MODULE_LICENSE("GPL");