PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/sound/soc/samsung/neo1973_wm8753.c

https://bitbucket.org/SamThomas/linux
C | 472 lines | 342 code | 83 blank | 47 comment | 31 complexity | 511420dfec25065fb7ad79e70b0188f5 MD5 | raw file
  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. static int gta02_speaker_enabled;
  195. static int lm4853_set_spk(struct snd_kcontrol *kcontrol,
  196. struct snd_ctl_elem_value *ucontrol)
  197. {
  198. gta02_speaker_enabled = ucontrol->value.integer.value[0];
  199. gpio_set_value(GTA02_GPIO_HP_IN, !gta02_speaker_enabled);
  200. return 0;
  201. }
  202. static int lm4853_get_spk(struct snd_kcontrol *kcontrol,
  203. struct snd_ctl_elem_value *ucontrol)
  204. {
  205. ucontrol->value.integer.value[0] = gta02_speaker_enabled;
  206. return 0;
  207. }
  208. static int lm4853_event(struct snd_soc_dapm_widget *w,
  209. struct snd_kcontrol *k, int event)
  210. {
  211. gpio_set_value(GTA02_GPIO_AMP_SHUT, SND_SOC_DAPM_EVENT_OFF(event));
  212. return 0;
  213. }
  214. static const struct snd_soc_dapm_route neo1973_gta02_routes[] = {
  215. /* Connections to the amp */
  216. {"Stereo Out", NULL, "LOUT1"},
  217. {"Stereo Out", NULL, "ROUT1"},
  218. /* Call Speaker */
  219. {"Handset Spk", NULL, "LOUT2"},
  220. {"Handset Spk", NULL, "ROUT2"},
  221. };
  222. static const struct snd_kcontrol_new neo1973_gta02_wm8753_controls[] = {
  223. SOC_DAPM_PIN_SWITCH("Handset Spk"),
  224. SOC_DAPM_PIN_SWITCH("Stereo Out"),
  225. SOC_SINGLE_BOOL_EXT("Amp Spk Switch", 0,
  226. lm4853_get_spk,
  227. lm4853_set_spk),
  228. };
  229. static const struct snd_soc_dapm_widget neo1973_gta02_wm8753_dapm_widgets[] = {
  230. SND_SOC_DAPM_SPK("Handset Spk", NULL),
  231. SND_SOC_DAPM_SPK("Stereo Out", lm4853_event),
  232. };
  233. static int neo1973_gta02_wm8753_init(struct snd_soc_codec *codec)
  234. {
  235. struct snd_soc_dapm_context *dapm = &codec->dapm;
  236. int ret;
  237. ret = snd_soc_dapm_new_controls(dapm, neo1973_gta02_wm8753_dapm_widgets,
  238. ARRAY_SIZE(neo1973_gta02_wm8753_dapm_widgets));
  239. if (ret)
  240. return ret;
  241. ret = snd_soc_dapm_add_routes(dapm, neo1973_gta02_routes,
  242. ARRAY_SIZE(neo1973_gta02_routes));
  243. if (ret)
  244. return ret;
  245. ret = snd_soc_add_controls(codec, neo1973_gta02_wm8753_controls,
  246. ARRAY_SIZE(neo1973_gta02_wm8753_controls));
  247. if (ret)
  248. return ret;
  249. snd_soc_dapm_disable_pin(dapm, "Stereo Out");
  250. snd_soc_dapm_disable_pin(dapm, "Handset Spk");
  251. snd_soc_dapm_ignore_suspend(dapm, "Stereo Out");
  252. snd_soc_dapm_ignore_suspend(dapm, "Handset Spk");
  253. return 0;
  254. }
  255. static int neo1973_wm8753_init(struct snd_soc_pcm_runtime *rtd)
  256. {
  257. struct snd_soc_codec *codec = rtd->codec;
  258. struct snd_soc_dapm_context *dapm = &codec->dapm;
  259. int ret;
  260. /* set up NC codec pins */
  261. snd_soc_dapm_nc_pin(dapm, "OUT3");
  262. snd_soc_dapm_nc_pin(dapm, "OUT4");
  263. snd_soc_dapm_nc_pin(dapm, "LINE1");
  264. snd_soc_dapm_nc_pin(dapm, "LINE2");
  265. /* Add neo1973 specific widgets */
  266. ret = snd_soc_dapm_new_controls(dapm, neo1973_wm8753_dapm_widgets,
  267. ARRAY_SIZE(neo1973_wm8753_dapm_widgets));
  268. if (ret)
  269. return ret;
  270. /* add neo1973 specific controls */
  271. ret = snd_soc_add_controls(codec, neo1973_wm8753_controls,
  272. ARRAY_SIZE(neo1973_wm8753_controls));
  273. if (ret)
  274. return ret;
  275. /* set up neo1973 specific audio routes */
  276. ret = snd_soc_dapm_add_routes(dapm, neo1973_wm8753_routes,
  277. ARRAY_SIZE(neo1973_wm8753_routes));
  278. if (ret)
  279. return ret;
  280. /* set endpoints to default off mode */
  281. snd_soc_dapm_disable_pin(dapm, "GSM Line Out");
  282. snd_soc_dapm_disable_pin(dapm, "GSM Line In");
  283. snd_soc_dapm_disable_pin(dapm, "Headset Mic");
  284. snd_soc_dapm_disable_pin(dapm, "Handset Mic");
  285. /* allow audio paths from the GSM modem to run during suspend */
  286. snd_soc_dapm_ignore_suspend(dapm, "GSM Line Out");
  287. snd_soc_dapm_ignore_suspend(dapm, "GSM Line In");
  288. snd_soc_dapm_ignore_suspend(dapm, "Headset Mic");
  289. snd_soc_dapm_ignore_suspend(dapm, "Handset Mic");
  290. if (machine_is_neo1973_gta02()) {
  291. ret = neo1973_gta02_wm8753_init(codec);
  292. if (ret)
  293. return ret;
  294. }
  295. return 0;
  296. }
  297. static struct snd_soc_dai_link neo1973_dai[] = {
  298. { /* Hifi Playback - for similatious use with voice below */
  299. .name = "WM8753",
  300. .stream_name = "WM8753 HiFi",
  301. .platform_name = "samsung-audio",
  302. .cpu_dai_name = "s3c24xx-iis",
  303. .codec_dai_name = "wm8753-hifi",
  304. .codec_name = "wm8753-codec.0-001a",
  305. .init = neo1973_wm8753_init,
  306. .ops = &neo1973_hifi_ops,
  307. },
  308. { /* Voice via BT */
  309. .name = "Bluetooth",
  310. .stream_name = "Voice",
  311. .cpu_dai_name = "dfbmcs320-pcm",
  312. .codec_dai_name = "wm8753-voice",
  313. .codec_name = "wm8753-codec.0-001a",
  314. .ops = &neo1973_voice_ops,
  315. },
  316. };
  317. static struct snd_soc_aux_dev neo1973_aux_devs[] = {
  318. {
  319. .name = "dfbmcs320",
  320. .codec_name = "dfbmcs320.0",
  321. },
  322. };
  323. static struct snd_soc_codec_conf neo1973_codec_conf[] = {
  324. {
  325. .dev_name = "lm4857.0-007c",
  326. .name_prefix = "Amp",
  327. },
  328. };
  329. static const struct gpio neo1973_gta02_gpios[] = {
  330. { GTA02_GPIO_HP_IN, GPIOF_OUT_INIT_HIGH, "GTA02_HP_IN" },
  331. { GTA02_GPIO_AMP_SHUT, GPIOF_OUT_INIT_HIGH, "GTA02_AMP_SHUT" },
  332. };
  333. static struct snd_soc_card neo1973 = {
  334. .name = "neo1973",
  335. .owner = THIS_MODULE,
  336. .dai_link = neo1973_dai,
  337. .num_links = ARRAY_SIZE(neo1973_dai),
  338. .aux_dev = neo1973_aux_devs,
  339. .num_aux_devs = ARRAY_SIZE(neo1973_aux_devs),
  340. .codec_conf = neo1973_codec_conf,
  341. .num_configs = ARRAY_SIZE(neo1973_codec_conf),
  342. };
  343. static struct platform_device *neo1973_snd_device;
  344. static int __init neo1973_init(void)
  345. {
  346. int ret;
  347. if (!machine_is_neo1973_gta02())
  348. return -ENODEV;
  349. if (machine_is_neo1973_gta02()) {
  350. neo1973.name = "neo1973gta02";
  351. neo1973.num_aux_devs = 1;
  352. ret = gpio_request_array(neo1973_gta02_gpios,
  353. ARRAY_SIZE(neo1973_gta02_gpios));
  354. if (ret)
  355. return ret;
  356. }
  357. neo1973_snd_device = platform_device_alloc("soc-audio", -1);
  358. if (!neo1973_snd_device) {
  359. ret = -ENOMEM;
  360. goto err_gpio_free;
  361. }
  362. platform_set_drvdata(neo1973_snd_device, &neo1973);
  363. ret = platform_device_add(neo1973_snd_device);
  364. if (ret)
  365. goto err_put_device;
  366. return 0;
  367. err_put_device:
  368. platform_device_put(neo1973_snd_device);
  369. err_gpio_free:
  370. if (machine_is_neo1973_gta02()) {
  371. gpio_free_array(neo1973_gta02_gpios,
  372. ARRAY_SIZE(neo1973_gta02_gpios));
  373. }
  374. return ret;
  375. }
  376. module_init(neo1973_init);
  377. static void __exit neo1973_exit(void)
  378. {
  379. platform_device_unregister(neo1973_snd_device);
  380. if (machine_is_neo1973_gta02()) {
  381. gpio_free_array(neo1973_gta02_gpios,
  382. ARRAY_SIZE(neo1973_gta02_gpios));
  383. }
  384. }
  385. module_exit(neo1973_exit);
  386. /* Module information */
  387. MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org, www.openmoko.org");
  388. MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 and Frerunner");
  389. MODULE_LICENSE("GPL");