smdk6410_wm8990.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * smdk6410_wm8990.c -- SoC audio for SMDK6410 with WM8990
  3. *
  4. * Copyright 2007, 2008 Wolfson Microelectronics PLC.
  5. * Author: Liam Girdwood
  6. * lg@opensource.wolfsonmicro.com or linux@wolfsonmicro.com
  7. *
  8. * Copyright (C) 2007, Ryu Euiyoul <ryu.real@gmail.com>
  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. * Revision history
  16. * 28th Feb 2008 Initial version.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/timer.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/i2c.h>
  25. #include <sound/driver.h>
  26. #include <sound/core.h>
  27. #include <sound/pcm.h>
  28. #include <sound/pcm_params.h>
  29. #include <sound/soc.h>
  30. #include <sound/soc-dapm.h>
  31. #include <asm/mach-types.h>
  32. #include <asm/hardware/scoop.h>
  33. #include <asm/arch/regs-iis.h>
  34. #include <asm/arch/regs-gpio.h>
  35. #include <asm/hardware.h>
  36. #include <asm/arch/audio.h>
  37. #include <asm/io.h>
  38. #include <asm/arch/spi-gpio.h>
  39. #include <asm/arch/regs-s3c6410-clock.h>
  40. #include "../codecs/wm8990.h"
  41. #include "s3c-pcm.h"
  42. #include "s3c-i2s.h"
  43. /* define the scenarios */
  44. #define SMDK6410_AUDIO_OFF 0
  45. #define SMDK6410_CAPTURE_MIC1 3
  46. #define SMDK6410_STEREO_TO_HEADPHONES 2
  47. #define SMDK6410_CAPTURE_LINE_IN 1
  48. #ifdef CONFIG_SND_DEBUG
  49. #define s3cdbg(x...) printk(x)
  50. #else
  51. #define s3cdbg(x...)
  52. #endif
  53. /*
  54. * TODO: - We need to work out PLL values for 256FS for every rate.
  55. */
  56. static int smdk6410_hifi_hw_params(struct snd_pcm_substream *substream,
  57. struct snd_pcm_hw_params *params)
  58. {
  59. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  60. struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
  61. struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
  62. unsigned int pll_out = 0, bclk = 0;
  63. int ret = 0;
  64. unsigned int iispsr, iismod;
  65. unsigned int prescaler = 4;
  66. s3cdbg("Entered %s, rate = %d\n", __FUNCTION__, params_rate(params));
  67. /*PCLK & SCLK gating enable*/
  68. writel(readl(S3C_PCLK_GATE)|S3C_CLKCON_PCLK_IIS0, S3C_PCLK_GATE);
  69. writel(readl(S3C_SCLK_GATE)|S3C_CLKCON_SCLK_AUDIO0, S3C_SCLK_GATE);
  70. iismod = readl(S3C_IIS0MOD);
  71. iismod &= ~S3C_IIS0MOD_FS_MASK;
  72. iismod &= ~S3C_IIS0MOD_BFS_MASK;
  73. /*Clear I2S prescaler value [13:8] and disable prescaler*/
  74. iispsr = readl(S3C_IIS0PSR);
  75. iispsr &=~((0x3f<<8)|(1<<15));
  76. writel(iispsr, S3C_IIS0PSR);
  77. s3cdbg("%s: %d , params = %d\n", __FUNCTION__, __LINE__, params_rate(params));
  78. switch (params_rate(params)) {
  79. case 8000:
  80. case 16000:
  81. case 32000:
  82. case 64100:
  83. writel(50332, S3C_EPLL_CON1);
  84. writel((1<<31)|(32<<16)|(1<<8)|(3<<0) ,S3C_EPLL_CON0);
  85. break;
  86. case 11025:
  87. case 22050:
  88. case 44100:
  89. case 88200:
  90. writel(10398, S3C_EPLL_CON1);
  91. writel((1<<31)|(45<<16)|(1<<8)|(3<<0) ,S3C_EPLL_CON0);
  92. break;
  93. case 48000:
  94. case 96000:
  95. writel(9961, S3C_EPLL_CON1);
  96. writel((1<<31)|(49<<16)|(1<<8)|(3<<0) ,S3C_EPLL_CON0);
  97. break;
  98. default:
  99. writel(0, S3C_EPLL_CON1);
  100. writel((1<<31)|(128<<16)|(25<<8)|(0<<0) ,S3C_EPLL_CON0);
  101. break;
  102. }
  103. s3cdbg("%s, IISCON: %x IISMOD: %x,IISFIC: %x,IISPSR: %x\n",
  104. __FUNCTION__ , readl(S3C_IIS0CON), readl(S3C_IIS0MOD),
  105. readl(S3C_IIS0FIC), readl(S3C_IIS0PSR));
  106. while(!(__raw_readl(S3C_EPLL_CON0)&(1<<30)));
  107. /* MUXepll : FOUTepll */
  108. writel(readl(S3C_CLK_SRC)|S3C_CLKSRC_EPLL_CLKSEL, S3C_CLK_SRC);
  109. /* AUDIO0 sel : FOUTepll */
  110. writel((readl(S3C_CLK_SRC)&~(0x7<<7))|(0<<7), S3C_CLK_SRC);
  111. /* CLK_DIV2 setting */
  112. writel(0x0,S3C_CLK_DIV2);
  113. switch (params_rate(params)) {
  114. case 8000:
  115. pll_out = 2048000;
  116. prescaler = 8;
  117. break;
  118. case 11025:
  119. pll_out = 2822400;
  120. prescaler = 8;
  121. break;
  122. case 16000:
  123. pll_out = 4096000;
  124. prescaler = 4;
  125. break;
  126. case 22050:
  127. pll_out = 5644800;
  128. prescaler = 4;
  129. break;
  130. case 32000:
  131. pll_out = 8192000;
  132. prescaler = 2;
  133. break;
  134. case 44100:
  135. pll_out = 11289600;
  136. //prescaler = 6;
  137. prescaler = 2;
  138. break;
  139. case 48000:
  140. pll_out = 12288000;
  141. prescaler = 2;
  142. break;
  143. case 88200:
  144. pll_out = 22579200;
  145. prescaler = 1;
  146. break;
  147. case 96000:
  148. pll_out = 24576000;
  149. prescaler = 1;
  150. break;
  151. default:
  152. /* somtimes 32000 rate comes to 96000
  153. default values are same as 32000 */
  154. iismod |= S3C_IIS0MOD_384FS;
  155. pll_out = 12288000;
  156. break;
  157. }
  158. /* set MCLK division for sample rate */
  159. switch (params_format(params)) {
  160. case SNDRV_PCM_FORMAT_S8:
  161. case SNDRV_PCM_FORMAT_S16_LE:
  162. iismod |= S3C_IIS0MOD_256FS | S3C_IIS0MOD_32FS;
  163. prescaler *= 3;
  164. break;
  165. case SNDRV_PCM_FORMAT_S24_LE:
  166. iismod |= S3C_IIS0MOD_384FS | S3C_IIS0MOD_48FS;
  167. prescaler *= 2;
  168. break;
  169. default:
  170. return -EINVAL;
  171. }
  172. prescaler = prescaler - 1;
  173. writel(iismod , S3C_IIS0MOD);
  174. /* set codec DAI configuration */
  175. ret = codec_dai->dai_ops.set_fmt(codec_dai,
  176. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  177. SND_SOC_DAIFMT_CBS_CFS );
  178. if (ret < 0)
  179. return ret;
  180. /* set cpu DAI configuration */
  181. ret = cpu_dai->dai_ops.set_fmt(cpu_dai,
  182. SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  183. SND_SOC_DAIFMT_CBS_CFS );
  184. if (ret < 0)
  185. return ret;
  186. /* set the codec system clock for DAC and ADC */
  187. ret = codec_dai->dai_ops.set_sysclk(codec_dai, WM8990_MCLK, pll_out,
  188. SND_SOC_CLOCK_IN);
  189. if (ret < 0)
  190. return ret;
  191. /* set prescaler division for sample rate */
  192. ret = cpu_dai->dai_ops.set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
  193. (prescaler << 0x8));
  194. if (ret < 0)
  195. return ret;
  196. return 0;
  197. }
  198. /*
  199. * Neo1973 WM8990 HiFi DAI opserations.
  200. */
  201. static struct snd_soc_ops smdk6410_hifi_ops = {
  202. .hw_params = smdk6410_hifi_hw_params,
  203. };
  204. static int smdk6410_scenario = 0;
  205. static int smdk6410_get_scenario(struct snd_kcontrol *kcontrol,
  206. struct snd_ctl_elem_value *ucontrol)
  207. {
  208. ucontrol->value.integer.value[0] = smdk6410_scenario;
  209. return 0;
  210. }
  211. static int set_scenario_endpoints(struct snd_soc_codec *codec, int scenario)
  212. {
  213. smdk6410_scenario = scenario;
  214. switch (smdk6410_scenario) {
  215. case SMDK6410_AUDIO_OFF:
  216. snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 0);
  217. snd_soc_dapm_set_endpoint(codec, "Mic1 Jack", 0);
  218. snd_soc_dapm_set_endpoint(codec, "Line In Jack", 0);
  219. break;
  220. case SMDK6410_STEREO_TO_HEADPHONES:
  221. snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 1);
  222. snd_soc_dapm_set_endpoint(codec, "Mic1 Jack", 0);
  223. snd_soc_dapm_set_endpoint(codec, "Line In Jack", 0);
  224. break;
  225. case SMDK6410_CAPTURE_MIC1:
  226. snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 0);
  227. snd_soc_dapm_set_endpoint(codec, "Mic1 Jack", 1);
  228. snd_soc_dapm_set_endpoint(codec, "Line In Jack", 0);
  229. break;
  230. case SMDK6410_CAPTURE_LINE_IN:
  231. snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 0);
  232. snd_soc_dapm_set_endpoint(codec, "Mic1 Jack", 0);
  233. snd_soc_dapm_set_endpoint(codec, "Line In Jack", 1);
  234. break;
  235. default:
  236. snd_soc_dapm_set_endpoint(codec, "Headphone Jack", 1);
  237. snd_soc_dapm_set_endpoint(codec, "Mic1 Jack", 1);
  238. snd_soc_dapm_set_endpoint(codec, "Line In Jack", 1);
  239. break;
  240. }
  241. snd_soc_dapm_sync_endpoints(codec);
  242. return 0;
  243. }
  244. static int smdk6410_set_scenario(struct snd_kcontrol *kcontrol,
  245. struct snd_ctl_elem_value *ucontrol)
  246. {
  247. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  248. if (smdk6410_scenario == ucontrol->value.integer.value[0])
  249. return 0;
  250. set_scenario_endpoints(codec, ucontrol->value.integer.value[0]);
  251. return 1;
  252. }
  253. static const struct snd_soc_dapm_widget wm8990_dapm_widgets[] = {
  254. SND_SOC_DAPM_HP("Headphone Jack", NULL),
  255. SND_SOC_DAPM_MIC("Mic1 Jack", NULL),
  256. SND_SOC_DAPM_LINE("Line In Jack", NULL),
  257. };
  258. /* example machine audio_mapnections */
  259. static const char* audio_map[][3] = {
  260. /* no irq jack detect */
  261. {"Headphone Jack", NULL, "LOUT"},
  262. {"Headphone Jack", NULL, "ROUT"},
  263. /* mic is connected to LIN1 and LIN2 - with bias */
  264. {"LIN1", NULL, "Mic1 Jack"},
  265. //{"LIN2", NULL, "Mic1 Jack"},
  266. {"LIN2", NULL, "Line In Jack"},
  267. {"RIN2", NULL, "Line In Jack"},
  268. {NULL, NULL, NULL},
  269. };
  270. static const char *smdk_scenarios[] = {
  271. "Off",
  272. "Capture Line In",
  273. "Headphones",
  274. "Capture Mic1",
  275. };
  276. static const struct soc_enum smdk_scenario_enum[] = {
  277. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(smdk_scenarios),smdk_scenarios),
  278. };
  279. static const struct snd_kcontrol_new wm8990_smdk6410_controls[] = {
  280. SOC_ENUM_EXT("SMDK Mode", smdk_scenario_enum[0],
  281. smdk6410_get_scenario, smdk6410_set_scenario),
  282. };
  283. /*
  284. * This is an example machine initialisation for a wm8990 connected to a
  285. * smdk6410. It is missing logic to detect hp/mic insertions and logic
  286. * to re-route the audio in such an event.
  287. */
  288. static int smdk6410_wm8990_init(struct snd_soc_codec *codec)
  289. {
  290. int i, err;
  291. /* Add smdk6410 specific widgets */
  292. for (i = 0; i < ARRAY_SIZE(wm8990_dapm_widgets); i++)
  293. snd_soc_dapm_new_control(codec, &wm8990_dapm_widgets[i]);
  294. /* add smdk6410 specific controls */
  295. for (i = 0; i < ARRAY_SIZE(wm8990_smdk6410_controls); i++) {
  296. err = snd_ctl_add(codec->card,
  297. snd_soc_cnew(&wm8990_smdk6410_controls[i],
  298. codec, NULL));
  299. if (err < 0)
  300. return err;
  301. }
  302. /* set up smdk6410 specific audio paths */
  303. for (i = 0; audio_map[i][0] != NULL; i++) {
  304. snd_soc_dapm_connect_input(codec, audio_map[i][0],
  305. audio_map[i][1], audio_map[i][2]);
  306. }
  307. /* not connected */
  308. snd_soc_dapm_set_endpoint(codec, "RIN1", 0);
  309. snd_soc_dapm_set_endpoint(codec, "LIN3", 0);
  310. snd_soc_dapm_set_endpoint(codec, "LIN4", 0);
  311. snd_soc_dapm_set_endpoint(codec, "RIN3", 0);
  312. snd_soc_dapm_set_endpoint(codec, "RIN4", 0);
  313. snd_soc_dapm_set_endpoint(codec, "OUT3", 0);
  314. snd_soc_dapm_set_endpoint(codec, "OUT4", 0);
  315. snd_soc_dapm_set_endpoint(codec, "SPKP", 0);
  316. snd_soc_dapm_set_endpoint(codec, "SPKN", 0);
  317. snd_soc_dapm_set_endpoint(codec, "ROP", 0);
  318. snd_soc_dapm_set_endpoint(codec, "RON", 0);
  319. snd_soc_dapm_set_endpoint(codec, "LOP", 0);
  320. snd_soc_dapm_set_endpoint(codec, "LON", 0);
  321. /* set endpoints to default mode & sync with DAPM */
  322. set_scenario_endpoints(codec, SMDK6410_STEREO_TO_HEADPHONES);
  323. return 0;
  324. }
  325. static struct snd_soc_dai_link smdk6410_dai[] = {
  326. { /* Hifi Playback - for similatious use with voice below */
  327. .name = "WM8990",
  328. .stream_name = "WM8990 HiFi",
  329. .cpu_dai = &s3c_i2s_dai,
  330. .codec_dai = &wm8990_dai,
  331. .init = smdk6410_wm8990_init,
  332. .ops = &smdk6410_hifi_ops,
  333. },
  334. };
  335. static struct snd_soc_machine smdk6410 = {
  336. .name = "smdk6410",
  337. .dai_link = smdk6410_dai,
  338. .num_links = ARRAY_SIZE(smdk6410_dai),
  339. };
  340. static struct wm8990_setup_data smdk6410_wm8990_setup = {
  341. .i2c_address = 0x1b,
  342. };
  343. static struct snd_soc_device smdk6410_snd_devdata = {
  344. .machine = &smdk6410,
  345. .platform = &s3c24xx_soc_platform,
  346. .codec_dev = &soc_codec_dev_wm8990,
  347. .codec_data = &smdk6410_wm8990_setup,
  348. };
  349. static struct platform_device *smdk6410_snd_device;
  350. static int __init smdk6410_init(void)
  351. {
  352. int ret;
  353. smdk6410_snd_device = platform_device_alloc("soc-audio", -1);
  354. if (!smdk6410_snd_device)
  355. return -ENOMEM;
  356. platform_set_drvdata(smdk6410_snd_device, &smdk6410_snd_devdata);
  357. smdk6410_snd_devdata.dev = &smdk6410_snd_device->dev;
  358. ret = platform_device_add(smdk6410_snd_device);
  359. if (ret)
  360. platform_device_put(smdk6410_snd_device);
  361. return ret;
  362. }
  363. static void __exit smdk6410_exit(void)
  364. {
  365. platform_device_unregister(smdk6410_snd_device);
  366. }
  367. module_init(smdk6410_init);
  368. module_exit(smdk6410_exit);
  369. /* Module information */
  370. MODULE_AUTHOR("Liam Girdwood");
  371. MODULE_DESCRIPTION("ALSA SoC WM8990 SMDK6410");
  372. MODULE_LICENSE("GPL");