neo1973_wm8753.c 13 KB

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