neo1973_wm8753.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 <asm/mach-types.h>
  21. #include "regs-iis.h"
  22. #include "../codecs/wm8753.h"
  23. #include "s3c24xx-i2s.h"
  24. static int neo1973_hifi_hw_params(struct snd_pcm_substream *substream,
  25. struct snd_pcm_hw_params *params)
  26. {
  27. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  28. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  29. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  30. unsigned int pll_out = 0, bclk = 0;
  31. int ret = 0;
  32. unsigned long iis_clkrate;
  33. iis_clkrate = s3c24xx_i2s_get_clockrate();
  34. switch (params_rate(params)) {
  35. case 8000:
  36. case 16000:
  37. pll_out = 12288000;
  38. break;
  39. case 48000:
  40. bclk = WM8753_BCLK_DIV_4;
  41. pll_out = 12288000;
  42. break;
  43. case 96000:
  44. bclk = WM8753_BCLK_DIV_2;
  45. pll_out = 12288000;
  46. break;
  47. case 11025:
  48. bclk = WM8753_BCLK_DIV_16;
  49. pll_out = 11289600;
  50. break;
  51. case 22050:
  52. bclk = WM8753_BCLK_DIV_8;
  53. pll_out = 11289600;
  54. break;
  55. case 44100:
  56. bclk = WM8753_BCLK_DIV_4;
  57. pll_out = 11289600;
  58. break;
  59. case 88200:
  60. bclk = WM8753_BCLK_DIV_2;
  61. pll_out = 11289600;
  62. break;
  63. }
  64. /* set codec DAI configuration */
  65. ret = snd_soc_dai_set_fmt(codec_dai,
  66. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  67. SND_SOC_DAIFMT_CBM_CFM);
  68. if (ret < 0)
  69. return ret;
  70. /* set cpu DAI configuration */
  71. ret = snd_soc_dai_set_fmt(cpu_dai,
  72. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  73. SND_SOC_DAIFMT_CBM_CFM);
  74. if (ret < 0)
  75. return ret;
  76. /* set the codec system clock for DAC and ADC */
  77. ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
  78. SND_SOC_CLOCK_IN);
  79. if (ret < 0)
  80. return ret;
  81. /* set MCLK division for sample rate */
  82. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
  83. S3C2410_IISMOD_32FS);
  84. if (ret < 0)
  85. return ret;
  86. /* set codec BCLK division for sample rate */
  87. ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_BCLKDIV, bclk);
  88. if (ret < 0)
  89. return ret;
  90. /* set prescaler division for sample rate */
  91. ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  92. S3C24XX_PRESCALE(4, 4));
  93. if (ret < 0)
  94. return ret;
  95. /* codec PLL input is PCLK/4 */
  96. ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0,
  97. iis_clkrate / 4, pll_out);
  98. if (ret < 0)
  99. return ret;
  100. return 0;
  101. }
  102. static int neo1973_hifi_hw_free(struct snd_pcm_substream *substream)
  103. {
  104. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  105. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  106. /* disable the PLL */
  107. return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0, 0);
  108. }
  109. /*
  110. * Neo1973 WM8753 HiFi DAI opserations.
  111. */
  112. static struct snd_soc_ops neo1973_hifi_ops = {
  113. .hw_params = neo1973_hifi_hw_params,
  114. .hw_free = neo1973_hifi_hw_free,
  115. };
  116. static int neo1973_voice_hw_params(struct snd_pcm_substream *substream,
  117. struct snd_pcm_hw_params *params)
  118. {
  119. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  120. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  121. unsigned int pcmdiv = 0;
  122. int ret = 0;
  123. unsigned long iis_clkrate;
  124. iis_clkrate = s3c24xx_i2s_get_clockrate();
  125. if (params_rate(params) != 8000)
  126. return -EINVAL;
  127. if (params_channels(params) != 1)
  128. return -EINVAL;
  129. pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
  130. /* todo: gg check mode (DSP_B) against CSR datasheet */
  131. /* set codec DAI configuration */
  132. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_B |
  133. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  134. if (ret < 0)
  135. return ret;
  136. /* set the codec system clock for DAC and ADC */
  137. ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK, 12288000,
  138. SND_SOC_CLOCK_IN);
  139. if (ret < 0)
  140. return ret;
  141. /* set codec PCM division for sample rate */
  142. ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV, pcmdiv);
  143. if (ret < 0)
  144. return ret;
  145. /* configure and enable PLL for 12.288MHz output */
  146. ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0,
  147. iis_clkrate / 4, 12288000);
  148. if (ret < 0)
  149. return ret;
  150. return 0;
  151. }
  152. static int neo1973_voice_hw_free(struct snd_pcm_substream *substream)
  153. {
  154. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  155. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  156. /* disable the PLL */
  157. return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0, 0);
  158. }
  159. static struct snd_soc_ops neo1973_voice_ops = {
  160. .hw_params = neo1973_voice_hw_params,
  161. .hw_free = neo1973_voice_hw_free,
  162. };
  163. /* Shared routes and controls */
  164. static const struct snd_soc_dapm_widget neo1973_wm8753_dapm_widgets[] = {
  165. SND_SOC_DAPM_LINE("GSM Line Out", NULL),
  166. SND_SOC_DAPM_LINE("GSM Line In", NULL),
  167. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  168. SND_SOC_DAPM_MIC("Handset Mic", NULL),
  169. };
  170. static const struct snd_soc_dapm_route neo1973_wm8753_routes[] = {
  171. /* Connections to the GSM Module */
  172. {"GSM Line Out", NULL, "MONO1"},
  173. {"GSM Line Out", NULL, "MONO2"},
  174. {"RXP", NULL, "GSM Line In"},
  175. {"RXN", NULL, "GSM Line In"},
  176. /* Connections to Headset */
  177. {"MIC1", NULL, "Mic Bias"},
  178. {"Mic Bias", NULL, "Headset Mic"},
  179. /* Call Mic */
  180. {"MIC2", NULL, "Mic Bias"},
  181. {"MIC2N", NULL, "Mic Bias"},
  182. {"Mic Bias", NULL, "Handset Mic"},
  183. /* Connect the ALC pins */
  184. {"ACIN", NULL, "ACOP"},
  185. };
  186. static const struct snd_kcontrol_new neo1973_wm8753_controls[] = {
  187. SOC_DAPM_PIN_SWITCH("GSM Line Out"),
  188. SOC_DAPM_PIN_SWITCH("GSM Line In"),
  189. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  190. SOC_DAPM_PIN_SWITCH("Handset Mic"),
  191. };
  192. /* GTA02 specific routes and controls */
  193. static int gta02_speaker_enabled;
  194. static int lm4853_set_spk(struct snd_kcontrol *kcontrol,
  195. struct snd_ctl_elem_value *ucontrol)
  196. {
  197. gta02_speaker_enabled = ucontrol->value.integer.value[0];
  198. gpio_set_value(S3C2410_GPJ(2), !gta02_speaker_enabled);
  199. return 0;
  200. }
  201. static int lm4853_get_spk(struct snd_kcontrol *kcontrol,
  202. struct snd_ctl_elem_value *ucontrol)
  203. {
  204. ucontrol->value.integer.value[0] = gta02_speaker_enabled;
  205. return 0;
  206. }
  207. static int lm4853_event(struct snd_soc_dapm_widget *w,
  208. struct snd_kcontrol *k, int event)
  209. {
  210. gpio_set_value(S3C2410_GPJ(1), SND_SOC_DAPM_EVENT_OFF(event));
  211. return 0;
  212. }
  213. static const struct snd_soc_dapm_route neo1973_gta02_routes[] = {
  214. /* Connections to the amp */
  215. {"Stereo Out", NULL, "LOUT1"},
  216. {"Stereo Out", NULL, "ROUT1"},
  217. /* Call Speaker */
  218. {"Handset Spk", NULL, "LOUT2"},
  219. {"Handset Spk", NULL, "ROUT2"},
  220. };
  221. static const struct snd_kcontrol_new neo1973_gta02_wm8753_controls[] = {
  222. SOC_DAPM_PIN_SWITCH("Handset Spk"),
  223. SOC_DAPM_PIN_SWITCH("Stereo Out"),
  224. SOC_SINGLE_BOOL_EXT("Amp Spk Switch", 0,
  225. lm4853_get_spk,
  226. lm4853_set_spk),
  227. };
  228. static const struct snd_soc_dapm_widget neo1973_gta02_wm8753_dapm_widgets[] = {
  229. SND_SOC_DAPM_SPK("Handset Spk", NULL),
  230. SND_SOC_DAPM_SPK("Stereo Out", lm4853_event),
  231. };
  232. static int neo1973_gta02_wm8753_init(struct snd_soc_codec *codec)
  233. {
  234. struct snd_soc_dapm_context *dapm = &codec->dapm;
  235. int ret;
  236. ret = snd_soc_dapm_new_controls(dapm, neo1973_gta02_wm8753_dapm_widgets,
  237. ARRAY_SIZE(neo1973_gta02_wm8753_dapm_widgets));
  238. if (ret)
  239. return ret;
  240. ret = snd_soc_dapm_add_routes(dapm, neo1973_gta02_routes,
  241. ARRAY_SIZE(neo1973_gta02_routes));
  242. if (ret)
  243. return ret;
  244. ret = snd_soc_add_card_controls(codec->card, neo1973_gta02_wm8753_controls,
  245. ARRAY_SIZE(neo1973_gta02_wm8753_controls));
  246. if (ret)
  247. return ret;
  248. snd_soc_dapm_disable_pin(dapm, "Stereo Out");
  249. snd_soc_dapm_disable_pin(dapm, "Handset Spk");
  250. snd_soc_dapm_ignore_suspend(dapm, "Stereo Out");
  251. snd_soc_dapm_ignore_suspend(dapm, "Handset Spk");
  252. return 0;
  253. }
  254. static int neo1973_wm8753_init(struct snd_soc_pcm_runtime *rtd)
  255. {
  256. struct snd_soc_codec *codec = rtd->codec;
  257. struct snd_soc_dapm_context *dapm = &codec->dapm;
  258. int ret;
  259. /* set up NC codec pins */
  260. snd_soc_dapm_nc_pin(dapm, "OUT3");
  261. snd_soc_dapm_nc_pin(dapm, "OUT4");
  262. snd_soc_dapm_nc_pin(dapm, "LINE1");
  263. snd_soc_dapm_nc_pin(dapm, "LINE2");
  264. /* Add neo1973 specific widgets */
  265. ret = snd_soc_dapm_new_controls(dapm, neo1973_wm8753_dapm_widgets,
  266. ARRAY_SIZE(neo1973_wm8753_dapm_widgets));
  267. if (ret)
  268. return ret;
  269. /* add neo1973 specific controls */
  270. ret = snd_soc_add_card_controls(rtd->card, neo1973_wm8753_controls,
  271. ARRAY_SIZE(neo1973_wm8753_controls));
  272. if (ret)
  273. return ret;
  274. /* set up neo1973 specific audio routes */
  275. ret = snd_soc_dapm_add_routes(dapm, neo1973_wm8753_routes,
  276. ARRAY_SIZE(neo1973_wm8753_routes));
  277. if (ret)
  278. return ret;
  279. /* set endpoints to default off mode */
  280. snd_soc_dapm_disable_pin(dapm, "GSM Line Out");
  281. snd_soc_dapm_disable_pin(dapm, "GSM Line In");
  282. snd_soc_dapm_disable_pin(dapm, "Headset Mic");
  283. snd_soc_dapm_disable_pin(dapm, "Handset Mic");
  284. /* allow audio paths from the GSM modem to run during suspend */
  285. snd_soc_dapm_ignore_suspend(dapm, "GSM Line Out");
  286. snd_soc_dapm_ignore_suspend(dapm, "GSM Line In");
  287. snd_soc_dapm_ignore_suspend(dapm, "Headset Mic");
  288. snd_soc_dapm_ignore_suspend(dapm, "Handset Mic");
  289. if (machine_is_neo1973_gta02()) {
  290. ret = neo1973_gta02_wm8753_init(codec);
  291. if (ret)
  292. return ret;
  293. }
  294. return 0;
  295. }
  296. static struct snd_soc_dai_link neo1973_dai[] = {
  297. { /* Hifi Playback - for similatious use with voice below */
  298. .name = "WM8753",
  299. .stream_name = "WM8753 HiFi",
  300. .platform_name = "s3c24xx-iis",
  301. .cpu_dai_name = "s3c24xx-iis",
  302. .codec_dai_name = "wm8753-hifi",
  303. .codec_name = "wm8753.0-001a",
  304. .init = neo1973_wm8753_init,
  305. .ops = &neo1973_hifi_ops,
  306. },
  307. { /* Voice via BT */
  308. .name = "Bluetooth",
  309. .stream_name = "Voice",
  310. .cpu_dai_name = "bt-sco-pcm",
  311. .codec_dai_name = "wm8753-voice",
  312. .codec_name = "wm8753.0-001a",
  313. .ops = &neo1973_voice_ops,
  314. },
  315. };
  316. static struct snd_soc_aux_dev neo1973_aux_devs[] = {
  317. {
  318. .name = "dfbmcs320",
  319. .codec_name = "dfbmcs320.0",
  320. },
  321. };
  322. static struct snd_soc_codec_conf neo1973_codec_conf[] = {
  323. {
  324. .dev_name = "lm4857.0-007c",
  325. .name_prefix = "Amp",
  326. },
  327. };
  328. static const struct gpio neo1973_gta02_gpios[] = {
  329. { S3C2410_GPJ(2), GPIOF_OUT_INIT_HIGH, "GTA02_HP_IN" },
  330. { S3C2410_GPJ(1), GPIOF_OUT_INIT_HIGH, "GTA02_AMP_SHUT" },
  331. };
  332. static struct snd_soc_card neo1973 = {
  333. .name = "neo1973",
  334. .owner = THIS_MODULE,
  335. .dai_link = neo1973_dai,
  336. .num_links = ARRAY_SIZE(neo1973_dai),
  337. .aux_dev = neo1973_aux_devs,
  338. .num_aux_devs = ARRAY_SIZE(neo1973_aux_devs),
  339. .codec_conf = neo1973_codec_conf,
  340. .num_configs = ARRAY_SIZE(neo1973_codec_conf),
  341. };
  342. static struct platform_device *neo1973_snd_device;
  343. static int __init neo1973_init(void)
  344. {
  345. int ret;
  346. if (!machine_is_neo1973_gta02())
  347. return -ENODEV;
  348. if (machine_is_neo1973_gta02()) {
  349. neo1973.name = "neo1973gta02";
  350. neo1973.num_aux_devs = 1;
  351. ret = gpio_request_array(neo1973_gta02_gpios,
  352. ARRAY_SIZE(neo1973_gta02_gpios));
  353. if (ret)
  354. return ret;
  355. }
  356. neo1973_snd_device = platform_device_alloc("soc-audio", -1);
  357. if (!neo1973_snd_device) {
  358. ret = -ENOMEM;
  359. goto err_gpio_free;
  360. }
  361. platform_set_drvdata(neo1973_snd_device, &neo1973);
  362. ret = platform_device_add(neo1973_snd_device);
  363. if (ret)
  364. goto err_put_device;
  365. return 0;
  366. err_put_device:
  367. platform_device_put(neo1973_snd_device);
  368. err_gpio_free:
  369. if (machine_is_neo1973_gta02()) {
  370. gpio_free_array(neo1973_gta02_gpios,
  371. ARRAY_SIZE(neo1973_gta02_gpios));
  372. }
  373. return ret;
  374. }
  375. module_init(neo1973_init);
  376. static void __exit neo1973_exit(void)
  377. {
  378. platform_device_unregister(neo1973_snd_device);
  379. if (machine_is_neo1973_gta02()) {
  380. gpio_free_array(neo1973_gta02_gpios,
  381. ARRAY_SIZE(neo1973_gta02_gpios));
  382. }
  383. }
  384. module_exit(neo1973_exit);
  385. /* Module information */
  386. MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org, www.openmoko.org");
  387. MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 and Frerunner");
  388. MODULE_LICENSE("GPL");