s3c24xx-i2s.c 12 KB

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