neo1973_gta02_wm8753.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 <sound/soc-dapm.h>
  25. #include <asm/mach-types.h>
  26. #include <plat/regs-iis.h>
  27. #include <mach/regs-clock.h>
  28. #include <asm/io.h>
  29. #include <mach/gta02.h>
  30. #include "../codecs/wm8753.h"
  31. #include "s3c-dma.h"
  32. #include "s3c24xx-i2s.h"
  33. static struct snd_soc_card neo1973_gta02;
  34. static int neo1973_gta02_hifi_hw_params(struct snd_pcm_substream *substream,
  35. struct snd_pcm_hw_params *params)
  36. {
  37. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  38. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  39. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  40. unsigned int pll_out = 0, bclk = 0;
  41. int ret = 0;
  42. unsigned long iis_clkrate;
  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,
  98. WM8753_BCLKDIV, bclk);
  99. if (ret < 0)
  100. return ret;
  101. /* set prescaler division for sample rate */
  102. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  103. S3C24XX_PRESCALE(4, 4));
  104. if (ret < 0)
  105. return ret;
  106. /* codec PLL input is PCLK/4 */
  107. ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0,
  108. iis_clkrate / 4, pll_out);
  109. if (ret < 0)
  110. return ret;
  111. return 0;
  112. }
  113. static int neo1973_gta02_hifi_hw_free(struct snd_pcm_substream *substream)
  114. {
  115. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  116. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  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_gta02_hifi_ops = {
  124. .hw_params = neo1973_gta02_hifi_hw_params,
  125. .hw_free = neo1973_gta02_hifi_hw_free,
  126. };
  127. static int neo1973_gta02_voice_hw_params(
  128. struct snd_pcm_substream *substream,
  129. struct snd_pcm_hw_params *params)
  130. {
  131. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  132. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  133. unsigned int pcmdiv = 0;
  134. int ret = 0;
  135. unsigned long iis_clkrate;
  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,
  150. 12288000, 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,
  155. pcmdiv);
  156. if (ret < 0)
  157. return ret;
  158. /* configue and enable PLL for 12.288MHz output */
  159. ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0,
  160. iis_clkrate / 4, 12288000);
  161. if (ret < 0)
  162. return ret;
  163. return 0;
  164. }
  165. static int neo1973_gta02_voice_hw_free(struct snd_pcm_substream *substream)
  166. {
  167. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  168. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  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_gta02_voice_ops = {
  173. .hw_params = neo1973_gta02_voice_hw_params,
  174. .hw_free = neo1973_gta02_voice_hw_free,
  175. };
  176. #define LM4853_AMP 1
  177. #define LM4853_SPK 2
  178. static u8 lm4853_state;
  179. /* This has no effect, it exists only to maintain compatibility with
  180. * existing ALSA state files.
  181. */
  182. static int lm4853_set_state(struct snd_kcontrol *kcontrol,
  183. struct snd_ctl_elem_value *ucontrol)
  184. {
  185. int val = ucontrol->value.integer.value[0];
  186. if (val)
  187. lm4853_state |= LM4853_AMP;
  188. else
  189. lm4853_state &= ~LM4853_AMP;
  190. return 0;
  191. }
  192. static int lm4853_get_state(struct snd_kcontrol *kcontrol,
  193. struct snd_ctl_elem_value *ucontrol)
  194. {
  195. ucontrol->value.integer.value[0] = lm4853_state & LM4853_AMP;
  196. return 0;
  197. }
  198. static int lm4853_set_spk(struct snd_kcontrol *kcontrol,
  199. struct snd_ctl_elem_value *ucontrol)
  200. {
  201. int val = ucontrol->value.integer.value[0];
  202. if (val) {
  203. lm4853_state |= LM4853_SPK;
  204. gpio_set_value(GTA02_GPIO_HP_IN, 0);
  205. } else {
  206. lm4853_state &= ~LM4853_SPK;
  207. gpio_set_value(GTA02_GPIO_HP_IN, 1);
  208. }
  209. return 0;
  210. }
  211. static int lm4853_get_spk(struct snd_kcontrol *kcontrol,
  212. struct snd_ctl_elem_value *ucontrol)
  213. {
  214. ucontrol->value.integer.value[0] = (lm4853_state & LM4853_SPK) >> 1;
  215. return 0;
  216. }
  217. static int lm4853_event(struct snd_soc_dapm_widget *w,
  218. struct snd_kcontrol *k,
  219. int event)
  220. {
  221. gpio_set_value(GTA02_GPIO_AMP_SHUT, SND_SOC_DAPM_EVENT_OFF(value));
  222. return 0;
  223. }
  224. static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
  225. SND_SOC_DAPM_SPK("Stereo Out", lm4853_event),
  226. SND_SOC_DAPM_LINE("GSM Line Out", NULL),
  227. SND_SOC_DAPM_LINE("GSM Line In", NULL),
  228. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  229. SND_SOC_DAPM_MIC("Handset Mic", NULL),
  230. SND_SOC_DAPM_SPK("Handset Spk", NULL),
  231. };
  232. /* example machine audio_mapnections */
  233. static const struct snd_soc_dapm_route audio_map[] = {
  234. /* Connections to the lm4853 amp */
  235. {"Stereo Out", NULL, "LOUT1"},
  236. {"Stereo Out", NULL, "ROUT1"},
  237. /* Connections to the GSM Module */
  238. {"GSM Line Out", NULL, "MONO1"},
  239. {"GSM Line Out", NULL, "MONO2"},
  240. {"RXP", NULL, "GSM Line In"},
  241. {"RXN", NULL, "GSM Line In"},
  242. /* Connections to Headset */
  243. {"MIC1", NULL, "Mic Bias"},
  244. {"Mic Bias", NULL, "Headset Mic"},
  245. /* Call Mic */
  246. {"MIC2", NULL, "Mic Bias"},
  247. {"MIC2N", NULL, "Mic Bias"},
  248. {"Mic Bias", NULL, "Handset Mic"},
  249. /* Call Speaker */
  250. {"Handset Spk", NULL, "LOUT2"},
  251. {"Handset Spk", NULL, "ROUT2"},
  252. /* Connect the ALC pins */
  253. {"ACIN", NULL, "ACOP"},
  254. };
  255. static const struct snd_kcontrol_new wm8753_neo1973_gta02_controls[] = {
  256. SOC_DAPM_PIN_SWITCH("Stereo Out"),
  257. SOC_DAPM_PIN_SWITCH("GSM Line Out"),
  258. SOC_DAPM_PIN_SWITCH("GSM Line In"),
  259. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  260. SOC_DAPM_PIN_SWITCH("Handset Mic"),
  261. SOC_DAPM_PIN_SWITCH("Handset Spk"),
  262. /* This has no effect, it exists only to maintain compatibility with
  263. * existing ALSA state files.
  264. */
  265. SOC_SINGLE_EXT("Amp State Switch", 6, 0, 1, 0,
  266. lm4853_get_state,
  267. lm4853_set_state),
  268. SOC_SINGLE_EXT("Amp Spk Switch", 7, 0, 1, 0,
  269. lm4853_get_spk,
  270. lm4853_set_spk),
  271. };
  272. /*
  273. * This is an example machine initialisation for a wm8753 connected to a
  274. * neo1973 GTA02.
  275. */
  276. static int neo1973_gta02_wm8753_init(struct snd_soc_codec *codec)
  277. {
  278. int err;
  279. /* set up NC codec pins */
  280. snd_soc_dapm_nc_pin(codec, "OUT3");
  281. snd_soc_dapm_nc_pin(codec, "OUT4");
  282. snd_soc_dapm_nc_pin(codec, "LINE1");
  283. snd_soc_dapm_nc_pin(codec, "LINE2");
  284. /* Add neo1973 gta02 specific widgets */
  285. snd_soc_dapm_new_controls(codec, wm8753_dapm_widgets,
  286. ARRAY_SIZE(wm8753_dapm_widgets));
  287. /* add neo1973 gta02 specific controls */
  288. err = snd_soc_add_controls(codec, wm8753_neo1973_gta02_controls,
  289. ARRAY_SIZE(wm8753_neo1973_gta02_controls));
  290. if (err < 0)
  291. return err;
  292. /* set up neo1973 gta02 specific audio path audio_map */
  293. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  294. /* set endpoints to default off mode */
  295. snd_soc_dapm_disable_pin(codec, "Stereo Out");
  296. snd_soc_dapm_disable_pin(codec, "GSM Line Out");
  297. snd_soc_dapm_disable_pin(codec, "GSM Line In");
  298. snd_soc_dapm_disable_pin(codec, "Headset Mic");
  299. snd_soc_dapm_disable_pin(codec, "Handset Mic");
  300. snd_soc_dapm_disable_pin(codec, "Handset Spk");
  301. snd_soc_dapm_sync(codec);
  302. return 0;
  303. }
  304. /*
  305. * BT Codec DAI
  306. */
  307. static struct snd_soc_dai bt_dai = {
  308. .name = "Bluetooth",
  309. .id = 0,
  310. .playback = {
  311. .channels_min = 1,
  312. .channels_max = 1,
  313. .rates = SNDRV_PCM_RATE_8000,
  314. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  315. .capture = {
  316. .channels_min = 1,
  317. .channels_max = 1,
  318. .rates = SNDRV_PCM_RATE_8000,
  319. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  320. };
  321. static struct snd_soc_dai_link neo1973_gta02_dai[] = {
  322. { /* Hifi Playback - for similatious use with voice below */
  323. .name = "WM8753",
  324. .stream_name = "WM8753 HiFi",
  325. .cpu_dai = &s3c24xx_i2s_dai,
  326. .codec_dai = &wm8753_dai[WM8753_DAI_HIFI],
  327. .init = neo1973_gta02_wm8753_init,
  328. .ops = &neo1973_gta02_hifi_ops,
  329. },
  330. { /* Voice via BT */
  331. .name = "Bluetooth",
  332. .stream_name = "Voice",
  333. .cpu_dai = &bt_dai,
  334. .codec_dai = &wm8753_dai[WM8753_DAI_VOICE],
  335. .ops = &neo1973_gta02_voice_ops,
  336. },
  337. };
  338. static struct snd_soc_card neo1973_gta02 = {
  339. .name = "neo1973-gta02",
  340. .platform = &s3c24xx_soc_platform,
  341. .dai_link = neo1973_gta02_dai,
  342. .num_links = ARRAY_SIZE(neo1973_gta02_dai),
  343. };
  344. static struct snd_soc_device neo1973_gta02_snd_devdata = {
  345. .card = &neo1973_gta02,
  346. .codec_dev = &soc_codec_dev_wm8753,
  347. };
  348. static struct platform_device *neo1973_gta02_snd_device;
  349. static int __init neo1973_gta02_init(void)
  350. {
  351. int ret;
  352. if (!machine_is_neo1973_gta02()) {
  353. printk(KERN_INFO
  354. "Only GTA02 is supported by this ASoC driver\n");
  355. return -ENODEV;
  356. }
  357. /* register bluetooth DAI here */
  358. ret = snd_soc_register_dai(&bt_dai);
  359. if (ret)
  360. return ret;
  361. neo1973_gta02_snd_device = platform_device_alloc("soc-audio", -1);
  362. if (!neo1973_gta02_snd_device)
  363. return -ENOMEM;
  364. platform_set_drvdata(neo1973_gta02_snd_device,
  365. &neo1973_gta02_snd_devdata);
  366. neo1973_gta02_snd_devdata.dev = &neo1973_gta02_snd_device->dev;
  367. ret = platform_device_add(neo1973_gta02_snd_device);
  368. if (ret) {
  369. platform_device_put(neo1973_gta02_snd_device);
  370. return ret;
  371. }
  372. /* Initialise GPIOs used by amp */
  373. ret = gpio_request(GTA02_GPIO_HP_IN, "GTA02_HP_IN");
  374. if (ret) {
  375. pr_err("gta02_wm8753: Failed to register GPIO %d\n", GTA02_GPIO_HP_IN);
  376. goto err_unregister_device;
  377. }
  378. ret = gpio_direction_output(GTA02_GPIO_AMP_HP_IN, 1);
  379. if (ret) {
  380. pr_err("gta02_wm8753: Failed to configure GPIO %d\n", GTA02_GPIO_HP_IN);
  381. goto err_free_gpio_hp_in;
  382. }
  383. ret = gpio_request(GTA02_GPIO_AMP_SHUT, "GTA02_AMP_SHUT");
  384. if (ret) {
  385. pr_err("gta02_wm8753: Failed to register GPIO %d\n", GTA02_GPIO_AMP_SHUT);
  386. goto err_free_gpio_hp_in;
  387. }
  388. ret = gpio_direction_output(GTA02_GPIO_AMP_SHUT, 1);
  389. if (ret) {
  390. pr_err("gta02_wm8753: Failed to configure GPIO %d\n", GTA02_GPIO_AMP_SHUT);
  391. goto err_free_gpio_amp_shut;
  392. }
  393. return 0;
  394. err_free_gpio_amp_shut:
  395. gpio_free(GTA02_GPIO_AMP_SHUT);
  396. err_free_gpio_hp_in:
  397. gpio_free(GTA02_GPIO_HP_IN);
  398. err_unregister_device:
  399. platform_device_unregister(neo1973_gta02_snd_device);
  400. return ret;
  401. }
  402. module_init(neo1973_gta02_init);
  403. static void __exit neo1973_gta02_exit(void)
  404. {
  405. snd_soc_unregister_dai(&bt_dai);
  406. platform_device_unregister(neo1973_gta02_snd_device);
  407. gpio_free(GTA02_GPIO_HP_IN);
  408. gpio_free(GTA02_GPIO_AMP_SHUT);
  409. }
  410. module_exit(neo1973_gta02_exit);
  411. /* Module information */
  412. MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org");
  413. MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 GTA02");
  414. MODULE_LICENSE("GPL");