s3c24xx-i2s.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 <sound/core.h>
  28. #include <sound/pcm.h>
  29. #include <sound/pcm_params.h>
  30. #include <sound/initval.h>
  31. #include <sound/soc.h>
  32. #include <asm/hardware.h>
  33. #include <asm/io.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. break;
  189. default:
  190. return -EINVAL;
  191. }
  192. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  193. case SND_SOC_DAIFMT_LEFT_J:
  194. iismod |= S3C2410_IISMOD_MSB;
  195. break;
  196. case SND_SOC_DAIFMT_I2S:
  197. break;
  198. default:
  199. return -EINVAL;
  200. }
  201. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  202. DBG("hw_params w: IISMOD: %lx \n", iismod);
  203. return 0;
  204. }
  205. static int s3c24xx_i2s_hw_params(struct snd_pcm_substream *substream,
  206. struct snd_pcm_hw_params *params)
  207. {
  208. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  209. u32 iismod;
  210. DBG("Entered %s\n", __func__);
  211. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  212. rtd->dai->cpu_dai->dma_data = &s3c24xx_i2s_pcm_stereo_out;
  213. else
  214. rtd->dai->cpu_dai->dma_data = &s3c24xx_i2s_pcm_stereo_in;
  215. /* Working copies of register */
  216. iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  217. DBG("hw_params r: IISMOD: %lx\n", iismod);
  218. switch (params_format(params)) {
  219. case SNDRV_PCM_FORMAT_S8:
  220. break;
  221. case SNDRV_PCM_FORMAT_S16_LE:
  222. iismod |= S3C2410_IISMOD_16BIT;
  223. break;
  224. }
  225. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  226. DBG("hw_params w: IISMOD: %lx\n", iismod);
  227. return 0;
  228. }
  229. static int s3c24xx_i2s_trigger(struct snd_pcm_substream *substream, int cmd)
  230. {
  231. int ret = 0;
  232. DBG("Entered %s\n", __func__);
  233. switch (cmd) {
  234. case SNDRV_PCM_TRIGGER_START:
  235. case SNDRV_PCM_TRIGGER_RESUME:
  236. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  237. if (!s3c24xx_snd_is_clkmaster()) {
  238. ret = s3c24xx_snd_lrsync();
  239. if (ret)
  240. goto exit_err;
  241. }
  242. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  243. s3c24xx_snd_rxctrl(1);
  244. else
  245. s3c24xx_snd_txctrl(1);
  246. break;
  247. case SNDRV_PCM_TRIGGER_STOP:
  248. case SNDRV_PCM_TRIGGER_SUSPEND:
  249. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  250. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  251. s3c24xx_snd_rxctrl(0);
  252. else
  253. s3c24xx_snd_txctrl(0);
  254. break;
  255. default:
  256. ret = -EINVAL;
  257. break;
  258. }
  259. exit_err:
  260. return ret;
  261. }
  262. /*
  263. * Set S3C24xx Clock source
  264. */
  265. static int s3c24xx_i2s_set_sysclk(struct snd_soc_cpu_dai *cpu_dai,
  266. int clk_id, unsigned int freq, int dir)
  267. {
  268. u32 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  269. DBG("Entered %s\n", __func__);
  270. iismod &= ~S3C2440_IISMOD_MPLL;
  271. switch (clk_id) {
  272. case S3C24XX_CLKSRC_PCLK:
  273. break;
  274. case S3C24XX_CLKSRC_MPLL:
  275. iismod |= S3C2440_IISMOD_MPLL;
  276. break;
  277. default:
  278. return -EINVAL;
  279. }
  280. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  281. return 0;
  282. }
  283. /*
  284. * Set S3C24xx Clock dividers
  285. */
  286. static int s3c24xx_i2s_set_clkdiv(struct snd_soc_cpu_dai *cpu_dai,
  287. int div_id, int div)
  288. {
  289. u32 reg;
  290. DBG("Entered %s\n", __func__);
  291. switch (div_id) {
  292. case S3C24XX_DIV_BCLK:
  293. reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~S3C2410_IISMOD_FS_MASK;
  294. writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
  295. break;
  296. case S3C24XX_DIV_MCLK:
  297. reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~(S3C2410_IISMOD_384FS);
  298. writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
  299. break;
  300. case S3C24XX_DIV_PRESCALER:
  301. writel(div, s3c24xx_i2s.regs + S3C2410_IISPSR);
  302. reg = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
  303. writel(reg | S3C2410_IISCON_PSCEN, s3c24xx_i2s.regs + S3C2410_IISCON);
  304. break;
  305. default:
  306. return -EINVAL;
  307. }
  308. return 0;
  309. }
  310. /*
  311. * To avoid duplicating clock code, allow machine driver to
  312. * get the clockrate from here.
  313. */
  314. u32 s3c24xx_i2s_get_clockrate(void)
  315. {
  316. return clk_get_rate(s3c24xx_i2s.iis_clk);
  317. }
  318. EXPORT_SYMBOL_GPL(s3c24xx_i2s_get_clockrate);
  319. static int s3c24xx_i2s_probe(struct platform_device *pdev)
  320. {
  321. DBG("Entered %s\n", __func__);
  322. s3c24xx_i2s.regs = ioremap(S3C2410_PA_IIS, 0x100);
  323. if (s3c24xx_i2s.regs == NULL)
  324. return -ENXIO;
  325. s3c24xx_i2s.iis_clk=clk_get(&pdev->dev, "iis");
  326. if (s3c24xx_i2s.iis_clk == NULL) {
  327. DBG("failed to get iis_clock\n");
  328. iounmap(s3c24xx_i2s.regs);
  329. return -ENODEV;
  330. }
  331. clk_enable(s3c24xx_i2s.iis_clk);
  332. /* Configure the I2S pins in correct mode */
  333. s3c2410_gpio_cfgpin(S3C2410_GPE0, S3C2410_GPE0_I2SLRCK);
  334. s3c2410_gpio_cfgpin(S3C2410_GPE1, S3C2410_GPE1_I2SSCLK);
  335. s3c2410_gpio_cfgpin(S3C2410_GPE2, S3C2410_GPE2_CDCLK);
  336. s3c2410_gpio_cfgpin(S3C2410_GPE3, S3C2410_GPE3_I2SSDI);
  337. s3c2410_gpio_cfgpin(S3C2410_GPE4, S3C2410_GPE4_I2SSDO);
  338. writel(S3C2410_IISCON_IISEN, s3c24xx_i2s.regs + S3C2410_IISCON);
  339. s3c24xx_snd_txctrl(0);
  340. s3c24xx_snd_rxctrl(0);
  341. return 0;
  342. }
  343. #ifdef CONFIG_PM
  344. int s3c24xx_i2s_suspend(struct platform_device *pdev,
  345. struct snd_soc_cpu_dai *cpu_dai)
  346. {
  347. DBG("Entered %s\n", __func__);
  348. s3c24xx_i2s.iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
  349. s3c24xx_i2s.iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  350. s3c24xx_i2s.iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
  351. s3c24xx_i2s.iispsr = readl(s3c24xx_i2s.regs + S3C2410_IISPSR);
  352. clk_disable(s3c24xx_i2s.iis_clk);
  353. return 0;
  354. }
  355. int s3c24xx_i2s_resume(struct platform_device *pdev,
  356. struct snd_soc_cpu_dai *cpu_dai)
  357. {
  358. DBG("Entered %s\n", __func__);
  359. clk_enable(s3c24xx_i2s.iis_clk);
  360. writel(s3c24xx_i2s.iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
  361. writel(s3c24xx_i2s.iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  362. writel(s3c24xx_i2s.iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
  363. writel(s3c24xx_i2s.iispsr, s3c24xx_i2s.regs + S3C2410_IISPSR);
  364. return 0;
  365. }
  366. #else
  367. #define s3c24xx_i2s_suspend NULL
  368. #define s3c24xx_i2s_resume NULL
  369. #endif
  370. #define S3C24XX_I2S_RATES \
  371. (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
  372. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  373. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
  374. struct snd_soc_cpu_dai s3c24xx_i2s_dai = {
  375. .name = "s3c24xx-i2s",
  376. .id = 0,
  377. .type = SND_SOC_DAI_I2S,
  378. .probe = s3c24xx_i2s_probe,
  379. .suspend = s3c24xx_i2s_suspend,
  380. .resume = s3c24xx_i2s_resume,
  381. .playback = {
  382. .channels_min = 2,
  383. .channels_max = 2,
  384. .rates = S3C24XX_I2S_RATES,
  385. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
  386. .capture = {
  387. .channels_min = 2,
  388. .channels_max = 2,
  389. .rates = S3C24XX_I2S_RATES,
  390. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
  391. .ops = {
  392. .trigger = s3c24xx_i2s_trigger,
  393. .hw_params = s3c24xx_i2s_hw_params,},
  394. .dai_ops = {
  395. .set_fmt = s3c24xx_i2s_set_fmt,
  396. .set_clkdiv = s3c24xx_i2s_set_clkdiv,
  397. .set_sysclk = s3c24xx_i2s_set_sysclk,
  398. },
  399. };
  400. EXPORT_SYMBOL_GPL(s3c24xx_i2s_dai);
  401. /* Module information */
  402. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  403. MODULE_DESCRIPTION("s3c24xx I2S SoC Interface");
  404. MODULE_LICENSE("GPL");