s3c24xx-i2s.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * s3c24xx-i2s.c -- ALSA Soc Audio Layer
  3. *
  4. * (c) 2006 Wolfson Microelectronics PLC.
  5. * Graeme Gregory graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
  6. *
  7. * Copyright 2004-2005 Simtec Electronics
  8. * http://armlinux.simtec.co.uk/
  9. * Ben Dooks <ben@simtec.co.uk>
  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/delay.h>
  17. #include <linux/clk.h>
  18. #include <linux/io.h>
  19. #include <linux/gpio.h>
  20. #include <linux/module.h>
  21. #include <sound/soc.h>
  22. #include <sound/pcm_params.h>
  23. #include <mach/regs-gpio.h>
  24. #include <mach/dma.h>
  25. #include <plat/regs-iis.h>
  26. #include "dma.h"
  27. #include "s3c24xx-i2s.h"
  28. static struct s3c2410_dma_client s3c24xx_dma_client_out = {
  29. .name = "I2S PCM Stereo out"
  30. };
  31. static struct s3c2410_dma_client s3c24xx_dma_client_in = {
  32. .name = "I2S PCM Stereo in"
  33. };
  34. static struct s3c_dma_params s3c24xx_i2s_pcm_stereo_out = {
  35. .client = &s3c24xx_dma_client_out,
  36. .channel = DMACH_I2S_OUT,
  37. .dma_addr = S3C2410_PA_IIS + S3C2410_IISFIFO,
  38. .dma_size = 2,
  39. };
  40. static struct s3c_dma_params s3c24xx_i2s_pcm_stereo_in = {
  41. .client = &s3c24xx_dma_client_in,
  42. .channel = DMACH_I2S_IN,
  43. .dma_addr = S3C2410_PA_IIS + S3C2410_IISFIFO,
  44. .dma_size = 2,
  45. };
  46. struct s3c24xx_i2s_info {
  47. void __iomem *regs;
  48. struct clk *iis_clk;
  49. u32 iiscon;
  50. u32 iismod;
  51. u32 iisfcon;
  52. u32 iispsr;
  53. };
  54. static struct s3c24xx_i2s_info s3c24xx_i2s;
  55. static void s3c24xx_snd_txctrl(int on)
  56. {
  57. u32 iisfcon;
  58. u32 iiscon;
  59. u32 iismod;
  60. pr_debug("Entered %s\n", __func__);
  61. iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
  62. iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
  63. iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  64. pr_debug("r: IISCON: %x IISMOD: %x IISFCON: %x\n", iiscon, iismod, iisfcon);
  65. if (on) {
  66. iisfcon |= S3C2410_IISFCON_TXDMA | S3C2410_IISFCON_TXENABLE;
  67. iiscon |= S3C2410_IISCON_TXDMAEN | S3C2410_IISCON_IISEN;
  68. iiscon &= ~S3C2410_IISCON_TXIDLE;
  69. iismod |= S3C2410_IISMOD_TXMODE;
  70. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  71. writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
  72. writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
  73. } else {
  74. /* note, we have to disable the FIFOs otherwise bad things
  75. * seem to happen when the DMA stops. According to the
  76. * Samsung supplied kernel, this should allow the DMA
  77. * engine and FIFOs to reset. If this isn't allowed, the
  78. * DMA engine will simply freeze randomly.
  79. */
  80. iisfcon &= ~S3C2410_IISFCON_TXENABLE;
  81. iisfcon &= ~S3C2410_IISFCON_TXDMA;
  82. iiscon |= S3C2410_IISCON_TXIDLE;
  83. iiscon &= ~S3C2410_IISCON_TXDMAEN;
  84. iismod &= ~S3C2410_IISMOD_TXMODE;
  85. writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
  86. writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
  87. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  88. }
  89. pr_debug("w: IISCON: %x IISMOD: %x IISFCON: %x\n", iiscon, iismod, iisfcon);
  90. }
  91. static void s3c24xx_snd_rxctrl(int on)
  92. {
  93. u32 iisfcon;
  94. u32 iiscon;
  95. u32 iismod;
  96. pr_debug("Entered %s\n", __func__);
  97. iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
  98. iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
  99. iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  100. pr_debug("r: IISCON: %x IISMOD: %x IISFCON: %x\n", iiscon, iismod, iisfcon);
  101. if (on) {
  102. iisfcon |= S3C2410_IISFCON_RXDMA | S3C2410_IISFCON_RXENABLE;
  103. iiscon |= S3C2410_IISCON_RXDMAEN | S3C2410_IISCON_IISEN;
  104. iiscon &= ~S3C2410_IISCON_RXIDLE;
  105. iismod |= S3C2410_IISMOD_RXMODE;
  106. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  107. writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
  108. writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
  109. } else {
  110. /* note, we have to disable the FIFOs otherwise bad things
  111. * seem to happen when the DMA stops. According to the
  112. * Samsung supplied kernel, this should allow the DMA
  113. * engine and FIFOs to reset. If this isn't allowed, the
  114. * DMA engine will simply freeze randomly.
  115. */
  116. iisfcon &= ~S3C2410_IISFCON_RXENABLE;
  117. iisfcon &= ~S3C2410_IISFCON_RXDMA;
  118. iiscon |= S3C2410_IISCON_RXIDLE;
  119. iiscon &= ~S3C2410_IISCON_RXDMAEN;
  120. iismod &= ~S3C2410_IISMOD_RXMODE;
  121. writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
  122. writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
  123. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  124. }
  125. pr_debug("w: IISCON: %x IISMOD: %x IISFCON: %x\n", iiscon, iismod, iisfcon);
  126. }
  127. /*
  128. * Wait for the LR signal to allow synchronisation to the L/R clock
  129. * from the codec. May only be needed for slave mode.
  130. */
  131. static int s3c24xx_snd_lrsync(void)
  132. {
  133. u32 iiscon;
  134. int timeout = 50; /* 5ms */
  135. pr_debug("Entered %s\n", __func__);
  136. while (1) {
  137. iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
  138. if (iiscon & S3C2410_IISCON_LRINDEX)
  139. break;
  140. if (!timeout--)
  141. return -ETIMEDOUT;
  142. udelay(100);
  143. }
  144. return 0;
  145. }
  146. /*
  147. * Check whether CPU is the master or slave
  148. */
  149. static inline int s3c24xx_snd_is_clkmaster(void)
  150. {
  151. pr_debug("Entered %s\n", __func__);
  152. return (readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & S3C2410_IISMOD_SLAVE) ? 0:1;
  153. }
  154. /*
  155. * Set S3C24xx I2S DAI format
  156. */
  157. static int s3c24xx_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
  158. unsigned int fmt)
  159. {
  160. u32 iismod;
  161. pr_debug("Entered %s\n", __func__);
  162. iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  163. pr_debug("hw_params r: IISMOD: %x \n", iismod);
  164. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  165. case SND_SOC_DAIFMT_CBM_CFM:
  166. iismod |= S3C2410_IISMOD_SLAVE;
  167. break;
  168. case SND_SOC_DAIFMT_CBS_CFS:
  169. iismod &= ~S3C2410_IISMOD_SLAVE;
  170. break;
  171. default:
  172. return -EINVAL;
  173. }
  174. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  175. case SND_SOC_DAIFMT_LEFT_J:
  176. iismod |= S3C2410_IISMOD_MSB;
  177. break;
  178. case SND_SOC_DAIFMT_I2S:
  179. iismod &= ~S3C2410_IISMOD_MSB;
  180. break;
  181. default:
  182. return -EINVAL;
  183. }
  184. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  185. pr_debug("hw_params w: IISMOD: %x \n", iismod);
  186. return 0;
  187. }
  188. static int s3c24xx_i2s_hw_params(struct snd_pcm_substream *substream,
  189. struct snd_pcm_hw_params *params,
  190. struct snd_soc_dai *dai)
  191. {
  192. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  193. struct s3c_dma_params *dma_data;
  194. u32 iismod;
  195. pr_debug("Entered %s\n", __func__);
  196. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  197. dma_data = &s3c24xx_i2s_pcm_stereo_out;
  198. else
  199. dma_data = &s3c24xx_i2s_pcm_stereo_in;
  200. snd_soc_dai_set_dma_data(rtd->cpu_dai, substream, dma_data);
  201. /* Working copies of register */
  202. iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  203. pr_debug("hw_params r: IISMOD: %x\n", iismod);
  204. switch (params_format(params)) {
  205. case SNDRV_PCM_FORMAT_S8:
  206. iismod &= ~S3C2410_IISMOD_16BIT;
  207. dma_data->dma_size = 1;
  208. break;
  209. case SNDRV_PCM_FORMAT_S16_LE:
  210. iismod |= S3C2410_IISMOD_16BIT;
  211. dma_data->dma_size = 2;
  212. break;
  213. default:
  214. return -EINVAL;
  215. }
  216. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  217. pr_debug("hw_params w: IISMOD: %x\n", iismod);
  218. return 0;
  219. }
  220. static int s3c24xx_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  221. struct snd_soc_dai *dai)
  222. {
  223. int ret = 0;
  224. struct s3c_dma_params *dma_data =
  225. snd_soc_dai_get_dma_data(dai, substream);
  226. pr_debug("Entered %s\n", __func__);
  227. switch (cmd) {
  228. case SNDRV_PCM_TRIGGER_START:
  229. case SNDRV_PCM_TRIGGER_RESUME:
  230. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  231. if (!s3c24xx_snd_is_clkmaster()) {
  232. ret = s3c24xx_snd_lrsync();
  233. if (ret)
  234. goto exit_err;
  235. }
  236. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  237. s3c24xx_snd_rxctrl(1);
  238. else
  239. s3c24xx_snd_txctrl(1);
  240. s3c2410_dma_ctrl(dma_data->channel, S3C2410_DMAOP_STARTED);
  241. break;
  242. case SNDRV_PCM_TRIGGER_STOP:
  243. case SNDRV_PCM_TRIGGER_SUSPEND:
  244. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  245. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  246. s3c24xx_snd_rxctrl(0);
  247. else
  248. s3c24xx_snd_txctrl(0);
  249. break;
  250. default:
  251. ret = -EINVAL;
  252. break;
  253. }
  254. exit_err:
  255. return ret;
  256. }
  257. /*
  258. * Set S3C24xx Clock source
  259. */
  260. static int s3c24xx_i2s_set_sysclk(struct snd_soc_dai *cpu_dai,
  261. int clk_id, unsigned int freq, int dir)
  262. {
  263. u32 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  264. pr_debug("Entered %s\n", __func__);
  265. iismod &= ~S3C2440_IISMOD_MPLL;
  266. switch (clk_id) {
  267. case S3C24XX_CLKSRC_PCLK:
  268. break;
  269. case S3C24XX_CLKSRC_MPLL:
  270. iismod |= S3C2440_IISMOD_MPLL;
  271. break;
  272. default:
  273. return -EINVAL;
  274. }
  275. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  276. return 0;
  277. }
  278. /*
  279. * Set S3C24xx Clock dividers
  280. */
  281. static int s3c24xx_i2s_set_clkdiv(struct snd_soc_dai *cpu_dai,
  282. int div_id, int div)
  283. {
  284. u32 reg;
  285. pr_debug("Entered %s\n", __func__);
  286. switch (div_id) {
  287. case S3C24XX_DIV_BCLK:
  288. reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~S3C2410_IISMOD_FS_MASK;
  289. writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
  290. break;
  291. case S3C24XX_DIV_MCLK:
  292. reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~(S3C2410_IISMOD_384FS);
  293. writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
  294. break;
  295. case S3C24XX_DIV_PRESCALER:
  296. writel(div, s3c24xx_i2s.regs + S3C2410_IISPSR);
  297. reg = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
  298. writel(reg | S3C2410_IISCON_PSCEN, s3c24xx_i2s.regs + S3C2410_IISCON);
  299. break;
  300. default:
  301. return -EINVAL;
  302. }
  303. return 0;
  304. }
  305. /*
  306. * To avoid duplicating clock code, allow machine driver to
  307. * get the clockrate from here.
  308. */
  309. u32 s3c24xx_i2s_get_clockrate(void)
  310. {
  311. return clk_get_rate(s3c24xx_i2s.iis_clk);
  312. }
  313. EXPORT_SYMBOL_GPL(s3c24xx_i2s_get_clockrate);
  314. static int s3c24xx_i2s_probe(struct snd_soc_dai *dai)
  315. {
  316. pr_debug("Entered %s\n", __func__);
  317. s3c24xx_i2s.regs = ioremap(S3C2410_PA_IIS, 0x100);
  318. if (s3c24xx_i2s.regs == NULL)
  319. return -ENXIO;
  320. s3c24xx_i2s.iis_clk = clk_get(dai->dev, "iis");
  321. if (IS_ERR(s3c24xx_i2s.iis_clk)) {
  322. pr_err("failed to get iis_clock\n");
  323. iounmap(s3c24xx_i2s.regs);
  324. return PTR_ERR(s3c24xx_i2s.iis_clk);
  325. }
  326. clk_enable(s3c24xx_i2s.iis_clk);
  327. /* Configure the I2S pins in correct mode */
  328. s3c2410_gpio_cfgpin(S3C2410_GPE0, S3C2410_GPE0_I2SLRCK);
  329. s3c2410_gpio_cfgpin(S3C2410_GPE1, S3C2410_GPE1_I2SSCLK);
  330. s3c2410_gpio_cfgpin(S3C2410_GPE2, S3C2410_GPE2_CDCLK);
  331. s3c2410_gpio_cfgpin(S3C2410_GPE3, S3C2410_GPE3_I2SSDI);
  332. s3c2410_gpio_cfgpin(S3C2410_GPE4, S3C2410_GPE4_I2SSDO);
  333. writel(S3C2410_IISCON_IISEN, s3c24xx_i2s.regs + S3C2410_IISCON);
  334. s3c24xx_snd_txctrl(0);
  335. s3c24xx_snd_rxctrl(0);
  336. return 0;
  337. }
  338. #ifdef CONFIG_PM
  339. static int s3c24xx_i2s_suspend(struct snd_soc_dai *cpu_dai)
  340. {
  341. pr_debug("Entered %s\n", __func__);
  342. s3c24xx_i2s.iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
  343. s3c24xx_i2s.iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  344. s3c24xx_i2s.iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
  345. s3c24xx_i2s.iispsr = readl(s3c24xx_i2s.regs + S3C2410_IISPSR);
  346. clk_disable(s3c24xx_i2s.iis_clk);
  347. return 0;
  348. }
  349. static int s3c24xx_i2s_resume(struct snd_soc_dai *cpu_dai)
  350. {
  351. pr_debug("Entered %s\n", __func__);
  352. clk_enable(s3c24xx_i2s.iis_clk);
  353. writel(s3c24xx_i2s.iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
  354. writel(s3c24xx_i2s.iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  355. writel(s3c24xx_i2s.iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
  356. writel(s3c24xx_i2s.iispsr, s3c24xx_i2s.regs + S3C2410_IISPSR);
  357. return 0;
  358. }
  359. #else
  360. #define s3c24xx_i2s_suspend NULL
  361. #define s3c24xx_i2s_resume NULL
  362. #endif
  363. #define S3C24XX_I2S_RATES \
  364. (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
  365. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  366. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
  367. static const struct snd_soc_dai_ops s3c24xx_i2s_dai_ops = {
  368. .trigger = s3c24xx_i2s_trigger,
  369. .hw_params = s3c24xx_i2s_hw_params,
  370. .set_fmt = s3c24xx_i2s_set_fmt,
  371. .set_clkdiv = s3c24xx_i2s_set_clkdiv,
  372. .set_sysclk = s3c24xx_i2s_set_sysclk,
  373. };
  374. static struct snd_soc_dai_driver s3c24xx_i2s_dai = {
  375. .probe = s3c24xx_i2s_probe,
  376. .suspend = s3c24xx_i2s_suspend,
  377. .resume = s3c24xx_i2s_resume,
  378. .playback = {
  379. .channels_min = 2,
  380. .channels_max = 2,
  381. .rates = S3C24XX_I2S_RATES,
  382. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
  383. .capture = {
  384. .channels_min = 2,
  385. .channels_max = 2,
  386. .rates = S3C24XX_I2S_RATES,
  387. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
  388. .ops = &s3c24xx_i2s_dai_ops,
  389. };
  390. static __devinit int s3c24xx_iis_dev_probe(struct platform_device *pdev)
  391. {
  392. return snd_soc_register_dai(&pdev->dev, &s3c24xx_i2s_dai);
  393. }
  394. static __devexit int s3c24xx_iis_dev_remove(struct platform_device *pdev)
  395. {
  396. snd_soc_unregister_dai(&pdev->dev);
  397. return 0;
  398. }
  399. static struct platform_driver s3c24xx_iis_driver = {
  400. .probe = s3c24xx_iis_dev_probe,
  401. .remove = __devexit_p(s3c24xx_iis_dev_remove),
  402. .driver = {
  403. .name = "s3c24xx-iis",
  404. .owner = THIS_MODULE,
  405. },
  406. };
  407. module_platform_driver(s3c24xx_iis_driver);
  408. /* Module information */
  409. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  410. MODULE_DESCRIPTION("s3c24xx I2S SoC Interface");
  411. MODULE_LICENSE("GPL");
  412. MODULE_ALIAS("platform:s3c24xx-iis");