s3c24xx-i2s.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 <sound/driver.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-iis.h>
  35. #include <asm/arch/regs-gpio.h>
  36. #include <asm/arch/regs-clock.h>
  37. #include <asm/arch/audio.h>
  38. #include <asm/dma.h>
  39. #include <asm/arch/dma.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 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. };
  59. static struct s3c24xx_pcm_dma_params s3c24xx_i2s_pcm_stereo_in = {
  60. .client = &s3c24xx_dma_client_in,
  61. .channel = DMACH_I2S_IN,
  62. .dma_addr = S3C2410_PA_IIS + S3C2410_IISFIFO
  63. };
  64. struct s3c24xx_i2s_info {
  65. void __iomem *regs;
  66. struct clk *iis_clk;
  67. };
  68. static struct s3c24xx_i2s_info s3c24xx_i2s;
  69. static void s3c24xx_snd_txctrl(int on)
  70. {
  71. u32 iisfcon;
  72. u32 iiscon;
  73. u32 iismod;
  74. DBG("Entered %s\n", __FUNCTION__);
  75. iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
  76. iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
  77. iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  78. DBG("r: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
  79. if (on) {
  80. iisfcon |= S3C2410_IISFCON_TXDMA | S3C2410_IISFCON_TXENABLE;
  81. iiscon |= S3C2410_IISCON_TXDMAEN | S3C2410_IISCON_IISEN;
  82. iiscon &= ~S3C2410_IISCON_TXIDLE;
  83. iismod |= S3C2410_IISMOD_TXMODE;
  84. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  85. writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
  86. writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
  87. } else {
  88. /* note, we have to disable the FIFOs otherwise bad things
  89. * seem to happen when the DMA stops. According to the
  90. * Samsung supplied kernel, this should allow the DMA
  91. * engine and FIFOs to reset. If this isn't allowed, the
  92. * DMA engine will simply freeze randomly.
  93. */
  94. iisfcon &= ~S3C2410_IISFCON_TXENABLE;
  95. iisfcon &= ~S3C2410_IISFCON_TXDMA;
  96. iiscon |= S3C2410_IISCON_TXIDLE;
  97. iiscon &= ~S3C2410_IISCON_TXDMAEN;
  98. iismod &= ~S3C2410_IISMOD_TXMODE;
  99. writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
  100. writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
  101. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  102. }
  103. DBG("w: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
  104. }
  105. static void s3c24xx_snd_rxctrl(int on)
  106. {
  107. u32 iisfcon;
  108. u32 iiscon;
  109. u32 iismod;
  110. DBG("Entered %s\n", __FUNCTION__);
  111. iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
  112. iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
  113. iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  114. DBG("r: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
  115. if (on) {
  116. iisfcon |= S3C2410_IISFCON_RXDMA | S3C2410_IISFCON_RXENABLE;
  117. iiscon |= S3C2410_IISCON_RXDMAEN | S3C2410_IISCON_IISEN;
  118. iiscon &= ~S3C2410_IISCON_RXIDLE;
  119. iismod |= S3C2410_IISMOD_RXMODE;
  120. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  121. writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
  122. writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
  123. } else {
  124. /* note, we have to disable the FIFOs otherwise bad things
  125. * seem to happen when the DMA stops. According to the
  126. * Samsung supplied kernel, this should allow the DMA
  127. * engine and FIFOs to reset. If this isn't allowed, the
  128. * DMA engine will simply freeze randomly.
  129. */
  130. iisfcon &= ~S3C2410_IISFCON_RXENABLE;
  131. iisfcon &= ~S3C2410_IISFCON_RXDMA;
  132. iiscon |= S3C2410_IISCON_RXIDLE;
  133. iiscon &= ~S3C2410_IISCON_RXDMAEN;
  134. iismod &= ~S3C2410_IISMOD_RXMODE;
  135. writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
  136. writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
  137. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  138. }
  139. DBG("w: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
  140. }
  141. /*
  142. * Wait for the LR signal to allow synchronisation to the L/R clock
  143. * from the codec. May only be needed for slave mode.
  144. */
  145. static int s3c24xx_snd_lrsync(void)
  146. {
  147. u32 iiscon;
  148. unsigned long timeout = jiffies + msecs_to_jiffies(5);
  149. DBG("Entered %s\n", __FUNCTION__);
  150. while (1) {
  151. iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
  152. if (iiscon & S3C2410_IISCON_LRINDEX)
  153. break;
  154. if (timeout < jiffies)
  155. return -ETIMEDOUT;
  156. }
  157. return 0;
  158. }
  159. /*
  160. * Check whether CPU is the master or slave
  161. */
  162. static inline int s3c24xx_snd_is_clkmaster(void)
  163. {
  164. DBG("Entered %s\n", __FUNCTION__);
  165. return (readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & S3C2410_IISMOD_SLAVE) ? 0:1;
  166. }
  167. /*
  168. * Set S3C24xx I2S DAI format
  169. */
  170. static int s3c24xx_i2s_set_fmt(struct snd_soc_cpu_dai *cpu_dai,
  171. unsigned int fmt)
  172. {
  173. u32 iismod;
  174. DBG("Entered %s\n", __FUNCTION__);
  175. iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  176. DBG("hw_params r: IISMOD: %lx \n", iismod);
  177. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  178. case SND_SOC_DAIFMT_CBM_CFM:
  179. iismod |= S3C2410_IISMOD_SLAVE;
  180. break;
  181. case SND_SOC_DAIFMT_CBS_CFS:
  182. break;
  183. default:
  184. return -EINVAL;
  185. }
  186. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  187. case SND_SOC_DAIFMT_LEFT_J:
  188. iismod |= S3C2410_IISMOD_MSB;
  189. break;
  190. case SND_SOC_DAIFMT_I2S:
  191. break;
  192. default:
  193. return -EINVAL;
  194. }
  195. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  196. DBG("hw_params w: IISMOD: %lx \n", iismod);
  197. return 0;
  198. }
  199. static int s3c24xx_i2s_hw_params(struct snd_pcm_substream *substream,
  200. struct snd_pcm_hw_params *params)
  201. {
  202. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  203. u32 iismod;
  204. DBG("Entered %s\n", __FUNCTION__);
  205. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  206. rtd->dai->cpu_dai->dma_data = &s3c24xx_i2s_pcm_stereo_out;
  207. else
  208. rtd->dai->cpu_dai->dma_data = &s3c24xx_i2s_pcm_stereo_in;
  209. /* Working copies of register */
  210. iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  211. DBG("hw_params r: IISMOD: %lx\n", iismod);
  212. switch (params_format(params)) {
  213. case SNDRV_PCM_FORMAT_S8:
  214. break;
  215. case SNDRV_PCM_FORMAT_S16_LE:
  216. iismod |= S3C2410_IISMOD_16BIT;
  217. break;
  218. }
  219. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  220. DBG("hw_params w: IISMOD: %lx\n", iismod);
  221. return 0;
  222. }
  223. static int s3c24xx_i2s_trigger(struct snd_pcm_substream *substream, int cmd)
  224. {
  225. int ret = 0;
  226. DBG("Entered %s\n", __FUNCTION__);
  227. switch (cmd) {
  228. case SNDRV_PCM_TRIGGER_START:
  229. case SNDRV_PCM_TRIGGER_RESUME:
  230. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  231. if (!s3c24xx_snd_is_clkmaster()) {
  232. ret = s3c24xx_snd_lrsync();
  233. if (ret)
  234. goto exit_err;
  235. }
  236. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  237. s3c24xx_snd_rxctrl(1);
  238. else
  239. s3c24xx_snd_txctrl(1);
  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_cpu_dai *cpu_dai,
  260. int clk_id, unsigned int freq, int dir)
  261. {
  262. u32 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  263. DBG("Entered %s\n", __FUNCTION__);
  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_cpu_dai *cpu_dai,
  281. int div_id, int div)
  282. {
  283. u32 reg;
  284. DBG("Entered %s\n", __FUNCTION__);
  285. switch (div_id) {
  286. case S3C24XX_DIV_MCLK:
  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_BCLK:
  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 platform_device *pdev)
  314. {
  315. DBG("Entered %s\n", __FUNCTION__);
  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(&pdev->dev, "iis");
  320. if (s3c24xx_i2s.iis_clk == NULL) {
  321. DBG("failed to get iis_clock\n");
  322. return -ENODEV;
  323. }
  324. clk_enable(s3c24xx_i2s.iis_clk);
  325. /* Configure the I2S pins in correct mode */
  326. s3c2410_gpio_cfgpin(S3C2410_GPE0, S3C2410_GPE0_I2SLRCK);
  327. s3c2410_gpio_cfgpin(S3C2410_GPE1, S3C2410_GPE1_I2SSCLK);
  328. s3c2410_gpio_cfgpin(S3C2410_GPE2, S3C2410_GPE2_CDCLK);
  329. s3c2410_gpio_cfgpin(S3C2410_GPE3, S3C2410_GPE3_I2SSDI);
  330. s3c2410_gpio_cfgpin(S3C2410_GPE4, S3C2410_GPE4_I2SSDO);
  331. writel(S3C2410_IISCON_IISEN, s3c24xx_i2s.regs + S3C2410_IISCON);
  332. s3c24xx_snd_txctrl(0);
  333. s3c24xx_snd_rxctrl(0);
  334. return 0;
  335. }
  336. #define S3C24XX_I2S_RATES \
  337. (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
  338. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  339. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
  340. struct snd_soc_cpu_dai s3c24xx_i2s_dai = {
  341. .name = "s3c24xx-i2s",
  342. .id = 0,
  343. .type = SND_SOC_DAI_I2S,
  344. .probe = s3c24xx_i2s_probe,
  345. .playback = {
  346. .channels_min = 2,
  347. .channels_max = 2,
  348. .rates = S3C24XX_I2S_RATES,
  349. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
  350. .capture = {
  351. .channels_min = 2,
  352. .channels_max = 2,
  353. .rates = S3C24XX_I2S_RATES,
  354. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
  355. .ops = {
  356. .trigger = s3c24xx_i2s_trigger,
  357. .hw_params = s3c24xx_i2s_hw_params,},
  358. .dai_ops = {
  359. .set_fmt = s3c24xx_i2s_set_fmt,
  360. .set_clkdiv = s3c24xx_i2s_set_clkdiv,
  361. .set_sysclk = s3c24xx_i2s_set_sysclk,
  362. },
  363. };
  364. EXPORT_SYMBOL_GPL(s3c24xx_i2s_dai);
  365. /* Module information */
  366. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  367. MODULE_DESCRIPTION("s3c24xx I2S SoC Interface");
  368. MODULE_LICENSE("GPL");