s3c24xx-i2s.c 13 KB

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