s3c24xx-i2s.c 13 KB

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