s3c24xx-i2s.c 12 KB

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