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