s3c24xx-i2s.c 13 KB

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