s3c24xx-i2s.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. struct s3c_dma_params *dma_data;
  203. u32 iismod;
  204. pr_debug("Entered %s\n", __func__);
  205. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  206. dma_data = &s3c24xx_i2s_pcm_stereo_out;
  207. else
  208. dma_data = &s3c24xx_i2s_pcm_stereo_in;
  209. snd_soc_dai_set_dma_data(rtd->cpu_dai, substream, dma_data);
  210. /* Working copies of register */
  211. iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  212. pr_debug("hw_params r: IISMOD: %x\n", iismod);
  213. switch (params_format(params)) {
  214. case SNDRV_PCM_FORMAT_S8:
  215. iismod &= ~S3C2410_IISMOD_16BIT;
  216. dma_data->dma_size = 1;
  217. break;
  218. case SNDRV_PCM_FORMAT_S16_LE:
  219. iismod |= S3C2410_IISMOD_16BIT;
  220. 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 s3c_dma_params *dma_data =
  234. snd_soc_dai_get_dma_data(dai, substream);
  235. pr_debug("Entered %s\n", __func__);
  236. switch (cmd) {
  237. case SNDRV_PCM_TRIGGER_START:
  238. case SNDRV_PCM_TRIGGER_RESUME:
  239. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  240. if (!s3c24xx_snd_is_clkmaster()) {
  241. ret = s3c24xx_snd_lrsync();
  242. if (ret)
  243. goto exit_err;
  244. }
  245. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  246. s3c24xx_snd_rxctrl(1);
  247. else
  248. s3c24xx_snd_txctrl(1);
  249. s3c2410_dma_ctrl(dma_data->channel, S3C2410_DMAOP_STARTED);
  250. break;
  251. case SNDRV_PCM_TRIGGER_STOP:
  252. case SNDRV_PCM_TRIGGER_SUSPEND:
  253. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  254. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  255. s3c24xx_snd_rxctrl(0);
  256. else
  257. s3c24xx_snd_txctrl(0);
  258. break;
  259. default:
  260. ret = -EINVAL;
  261. break;
  262. }
  263. exit_err:
  264. return ret;
  265. }
  266. /*
  267. * Set S3C24xx Clock source
  268. */
  269. static int s3c24xx_i2s_set_sysclk(struct snd_soc_dai *cpu_dai,
  270. int clk_id, unsigned int freq, int dir)
  271. {
  272. u32 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  273. pr_debug("Entered %s\n", __func__);
  274. iismod &= ~S3C2440_IISMOD_MPLL;
  275. switch (clk_id) {
  276. case S3C24XX_CLKSRC_PCLK:
  277. break;
  278. case S3C24XX_CLKSRC_MPLL:
  279. iismod |= S3C2440_IISMOD_MPLL;
  280. break;
  281. default:
  282. return -EINVAL;
  283. }
  284. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  285. return 0;
  286. }
  287. /*
  288. * Set S3C24xx Clock dividers
  289. */
  290. static int s3c24xx_i2s_set_clkdiv(struct snd_soc_dai *cpu_dai,
  291. int div_id, int div)
  292. {
  293. u32 reg;
  294. pr_debug("Entered %s\n", __func__);
  295. switch (div_id) {
  296. case S3C24XX_DIV_BCLK:
  297. reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~S3C2410_IISMOD_FS_MASK;
  298. writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
  299. break;
  300. case S3C24XX_DIV_MCLK:
  301. reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~(S3C2410_IISMOD_384FS);
  302. writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
  303. break;
  304. case S3C24XX_DIV_PRESCALER:
  305. writel(div, s3c24xx_i2s.regs + S3C2410_IISPSR);
  306. reg = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
  307. writel(reg | S3C2410_IISCON_PSCEN, s3c24xx_i2s.regs + S3C2410_IISCON);
  308. break;
  309. default:
  310. return -EINVAL;
  311. }
  312. return 0;
  313. }
  314. /*
  315. * To avoid duplicating clock code, allow machine driver to
  316. * get the clockrate from here.
  317. */
  318. u32 s3c24xx_i2s_get_clockrate(void)
  319. {
  320. return clk_get_rate(s3c24xx_i2s.iis_clk);
  321. }
  322. EXPORT_SYMBOL_GPL(s3c24xx_i2s_get_clockrate);
  323. static int s3c24xx_i2s_probe(struct snd_soc_dai *dai)
  324. {
  325. pr_debug("Entered %s\n", __func__);
  326. s3c24xx_i2s.regs = ioremap(S3C2410_PA_IIS, 0x100);
  327. if (s3c24xx_i2s.regs == NULL)
  328. return -ENXIO;
  329. s3c24xx_i2s.iis_clk = clk_get(dai->dev, "iis");
  330. if (s3c24xx_i2s.iis_clk == NULL) {
  331. pr_err("failed to get iis_clock\n");
  332. iounmap(s3c24xx_i2s.regs);
  333. return -ENODEV;
  334. }
  335. clk_enable(s3c24xx_i2s.iis_clk);
  336. /* Configure the I2S pins in correct mode */
  337. s3c2410_gpio_cfgpin(S3C2410_GPE0, S3C2410_GPE0_I2SLRCK);
  338. s3c2410_gpio_cfgpin(S3C2410_GPE1, S3C2410_GPE1_I2SSCLK);
  339. s3c2410_gpio_cfgpin(S3C2410_GPE2, S3C2410_GPE2_CDCLK);
  340. s3c2410_gpio_cfgpin(S3C2410_GPE3, S3C2410_GPE3_I2SSDI);
  341. s3c2410_gpio_cfgpin(S3C2410_GPE4, S3C2410_GPE4_I2SSDO);
  342. writel(S3C2410_IISCON_IISEN, s3c24xx_i2s.regs + S3C2410_IISCON);
  343. s3c24xx_snd_txctrl(0);
  344. s3c24xx_snd_rxctrl(0);
  345. return 0;
  346. }
  347. #ifdef CONFIG_PM
  348. static int s3c24xx_i2s_suspend(struct snd_soc_dai *cpu_dai)
  349. {
  350. pr_debug("Entered %s\n", __func__);
  351. s3c24xx_i2s.iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
  352. s3c24xx_i2s.iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  353. s3c24xx_i2s.iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
  354. s3c24xx_i2s.iispsr = readl(s3c24xx_i2s.regs + S3C2410_IISPSR);
  355. clk_disable(s3c24xx_i2s.iis_clk);
  356. return 0;
  357. }
  358. static int s3c24xx_i2s_resume(struct snd_soc_dai *cpu_dai)
  359. {
  360. pr_debug("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. static struct snd_soc_dai_ops s3c24xx_i2s_dai_ops = {
  377. .trigger = s3c24xx_i2s_trigger,
  378. .hw_params = s3c24xx_i2s_hw_params,
  379. .set_fmt = s3c24xx_i2s_set_fmt,
  380. .set_clkdiv = s3c24xx_i2s_set_clkdiv,
  381. .set_sysclk = s3c24xx_i2s_set_sysclk,
  382. };
  383. static struct snd_soc_dai_driver s3c24xx_i2s_dai = {
  384. .probe = s3c24xx_i2s_probe,
  385. .suspend = s3c24xx_i2s_suspend,
  386. .resume = s3c24xx_i2s_resume,
  387. .playback = {
  388. .channels_min = 2,
  389. .channels_max = 2,
  390. .rates = S3C24XX_I2S_RATES,
  391. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
  392. .capture = {
  393. .channels_min = 2,
  394. .channels_max = 2,
  395. .rates = S3C24XX_I2S_RATES,
  396. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
  397. .ops = &s3c24xx_i2s_dai_ops,
  398. };
  399. static __devinit int s3c24xx_iis_dev_probe(struct platform_device *pdev)
  400. {
  401. return snd_soc_register_dai(&pdev->dev, &s3c24xx_i2s_dai);
  402. }
  403. static __devexit int s3c24xx_iis_dev_remove(struct platform_device *pdev)
  404. {
  405. snd_soc_unregister_dai(&pdev->dev);
  406. return 0;
  407. }
  408. static struct platform_driver s3c24xx_iis_driver = {
  409. .probe = s3c24xx_iis_dev_probe,
  410. .remove = s3c24xx_iis_dev_remove,
  411. .driver = {
  412. .name = "s3c24xx-iis",
  413. .owner = THIS_MODULE,
  414. },
  415. };
  416. static int __init s3c24xx_i2s_init(void)
  417. {
  418. return platform_driver_register(&s3c24xx_iis_driver);
  419. }
  420. module_init(s3c24xx_i2s_init);
  421. static void __exit s3c24xx_i2s_exit(void)
  422. {
  423. platform_driver_unregister(&s3c24xx_iis_driver);
  424. }
  425. module_exit(s3c24xx_i2s_exit);
  426. /* Module information */
  427. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  428. MODULE_DESCRIPTION("s3c24xx I2S SoC Interface");
  429. MODULE_LICENSE("GPL");
  430. MODULE_ALIAS("platform:s3c24xx-iis");