neo1973_gta02_wm8753.c 13 KB

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