s3c24xx-i2s.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. .dma_size = 2,
  59. };
  60. static struct s3c24xx_pcm_dma_params s3c24xx_i2s_pcm_stereo_in = {
  61. .client = &s3c24xx_dma_client_in,
  62. .channel = DMACH_I2S_IN,
  63. .dma_addr = S3C2410_PA_IIS + S3C2410_IISFIFO,
  64. .dma_size = 2,
  65. };
  66. struct s3c24xx_i2s_info {
  67. void __iomem *regs;
  68. struct clk *iis_clk;
  69. };
  70. static struct s3c24xx_i2s_info s3c24xx_i2s;
  71. static void s3c24xx_snd_txctrl(int on)
  72. {
  73. u32 iisfcon;
  74. u32 iiscon;
  75. u32 iismod;
  76. DBG("Entered %s\n", __FUNCTION__);
  77. iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
  78. iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
  79. iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  80. DBG("r: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
  81. if (on) {
  82. iisfcon |= S3C2410_IISFCON_TXDMA | S3C2410_IISFCON_TXENABLE;
  83. iiscon |= S3C2410_IISCON_TXDMAEN | S3C2410_IISCON_IISEN;
  84. iiscon &= ~S3C2410_IISCON_TXIDLE;
  85. iismod |= S3C2410_IISMOD_TXMODE;
  86. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  87. writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
  88. writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
  89. } else {
  90. /* note, we have to disable the FIFOs otherwise bad things
  91. * seem to happen when the DMA stops. According to the
  92. * Samsung supplied kernel, this should allow the DMA
  93. * engine and FIFOs to reset. If this isn't allowed, the
  94. * DMA engine will simply freeze randomly.
  95. */
  96. iisfcon &= ~S3C2410_IISFCON_TXENABLE;
  97. iisfcon &= ~S3C2410_IISFCON_TXDMA;
  98. iiscon |= S3C2410_IISCON_TXIDLE;
  99. iiscon &= ~S3C2410_IISCON_TXDMAEN;
  100. iismod &= ~S3C2410_IISMOD_TXMODE;
  101. writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
  102. writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
  103. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  104. }
  105. DBG("w: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
  106. }
  107. static void s3c24xx_snd_rxctrl(int on)
  108. {
  109. u32 iisfcon;
  110. u32 iiscon;
  111. u32 iismod;
  112. DBG("Entered %s\n", __FUNCTION__);
  113. iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
  114. iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
  115. iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  116. DBG("r: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
  117. if (on) {
  118. iisfcon |= S3C2410_IISFCON_RXDMA | S3C2410_IISFCON_RXENABLE;
  119. iiscon |= S3C2410_IISCON_RXDMAEN | S3C2410_IISCON_IISEN;
  120. iiscon &= ~S3C2410_IISCON_RXIDLE;
  121. iismod |= S3C2410_IISMOD_RXMODE;
  122. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  123. writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
  124. writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
  125. } else {
  126. /* note, we have to disable the FIFOs otherwise bad things
  127. * seem to happen when the DMA stops. According to the
  128. * Samsung supplied kernel, this should allow the DMA
  129. * engine and FIFOs to reset. If this isn't allowed, the
  130. * DMA engine will simply freeze randomly.
  131. */
  132. iisfcon &= ~S3C2410_IISFCON_RXENABLE;
  133. iisfcon &= ~S3C2410_IISFCON_RXDMA;
  134. iiscon |= S3C2410_IISCON_RXIDLE;
  135. iiscon &= ~S3C2410_IISCON_RXDMAEN;
  136. iismod &= ~S3C2410_IISMOD_RXMODE;
  137. writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
  138. writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
  139. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  140. }
  141. DBG("w: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
  142. }
  143. /*
  144. * Wait for the LR signal to allow synchronisation to the L/R clock
  145. * from the codec. May only be needed for slave mode.
  146. */
  147. static int s3c24xx_snd_lrsync(void)
  148. {
  149. u32 iiscon;
  150. unsigned long timeout = jiffies + msecs_to_jiffies(5);
  151. DBG("Entered %s\n", __FUNCTION__);
  152. while (1) {
  153. iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
  154. if (iiscon & S3C2410_IISCON_LRINDEX)
  155. break;
  156. if (timeout < jiffies)
  157. return -ETIMEDOUT;
  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", __FUNCTION__);
  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_cpu_dai *cpu_dai,
  173. unsigned int fmt)
  174. {
  175. u32 iismod;
  176. DBG("Entered %s\n", __FUNCTION__);
  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. break;
  185. default:
  186. return -EINVAL;
  187. }
  188. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  189. case SND_SOC_DAIFMT_LEFT_J:
  190. iismod |= S3C2410_IISMOD_MSB;
  191. break;
  192. case SND_SOC_DAIFMT_I2S:
  193. break;
  194. default:
  195. return -EINVAL;
  196. }
  197. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  198. DBG("hw_params w: IISMOD: %lx \n", iismod);
  199. return 0;
  200. }
  201. static int s3c24xx_i2s_hw_params(struct snd_pcm_substream *substream,
  202. struct snd_pcm_hw_params *params)
  203. {
  204. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  205. u32 iismod;
  206. DBG("Entered %s\n", __FUNCTION__);
  207. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  208. rtd->dai->cpu_dai->dma_data = &s3c24xx_i2s_pcm_stereo_out;
  209. else
  210. rtd->dai->cpu_dai->dma_data = &s3c24xx_i2s_pcm_stereo_in;
  211. /* Working copies of register */
  212. iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  213. DBG("hw_params r: IISMOD: %lx\n", iismod);
  214. switch (params_format(params)) {
  215. case SNDRV_PCM_FORMAT_S8:
  216. break;
  217. case SNDRV_PCM_FORMAT_S16_LE:
  218. iismod |= S3C2410_IISMOD_16BIT;
  219. break;
  220. }
  221. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  222. DBG("hw_params w: IISMOD: %lx\n", iismod);
  223. return 0;
  224. }
  225. static int s3c24xx_i2s_trigger(struct snd_pcm_substream *substream, int cmd)
  226. {
  227. int ret = 0;
  228. DBG("Entered %s\n", __FUNCTION__);
  229. switch (cmd) {
  230. case SNDRV_PCM_TRIGGER_START:
  231. case SNDRV_PCM_TRIGGER_RESUME:
  232. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  233. if (!s3c24xx_snd_is_clkmaster()) {
  234. ret = s3c24xx_snd_lrsync();
  235. if (ret)
  236. goto exit_err;
  237. }
  238. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  239. s3c24xx_snd_rxctrl(1);
  240. else
  241. s3c24xx_snd_txctrl(1);
  242. break;
  243. case SNDRV_PCM_TRIGGER_STOP:
  244. case SNDRV_PCM_TRIGGER_SUSPEND:
  245. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  246. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  247. s3c24xx_snd_rxctrl(0);
  248. else
  249. s3c24xx_snd_txctrl(0);
  250. break;
  251. default:
  252. ret = -EINVAL;
  253. break;
  254. }
  255. exit_err:
  256. return ret;
  257. }
  258. /*
  259. * Set S3C24xx Clock source
  260. */
  261. static int s3c24xx_i2s_set_sysclk(struct snd_soc_cpu_dai *cpu_dai,
  262. int clk_id, unsigned int freq, int dir)
  263. {
  264. u32 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
  265. DBG("Entered %s\n", __FUNCTION__);
  266. iismod &= ~S3C2440_IISMOD_MPLL;
  267. switch (clk_id) {
  268. case S3C24XX_CLKSRC_PCLK:
  269. break;
  270. case S3C24XX_CLKSRC_MPLL:
  271. iismod |= S3C2440_IISMOD_MPLL;
  272. break;
  273. default:
  274. return -EINVAL;
  275. }
  276. writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
  277. return 0;
  278. }
  279. /*
  280. * Set S3C24xx Clock dividers
  281. */
  282. static int s3c24xx_i2s_set_clkdiv(struct snd_soc_cpu_dai *cpu_dai,
  283. int div_id, int div)
  284. {
  285. u32 reg;
  286. DBG("Entered %s\n", __FUNCTION__);
  287. switch (div_id) {
  288. case S3C24XX_DIV_MCLK:
  289. reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~S3C2410_IISMOD_FS_MASK;
  290. writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
  291. break;
  292. case S3C24XX_DIV_BCLK:
  293. reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~(S3C2410_IISMOD_384FS);
  294. writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
  295. break;
  296. case S3C24XX_DIV_PRESCALER:
  297. writel(div, s3c24xx_i2s.regs + S3C2410_IISPSR);
  298. reg = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
  299. writel(reg | S3C2410_IISCON_PSCEN, s3c24xx_i2s.regs + S3C2410_IISCON);
  300. break;
  301. default:
  302. return -EINVAL;
  303. }
  304. return 0;
  305. }
  306. /*
  307. * To avoid duplicating clock code, allow machine driver to
  308. * get the clockrate from here.
  309. */
  310. u32 s3c24xx_i2s_get_clockrate(void)
  311. {
  312. return clk_get_rate(s3c24xx_i2s.iis_clk);
  313. }
  314. EXPORT_SYMBOL_GPL(s3c24xx_i2s_get_clockrate);
  315. static int s3c24xx_i2s_probe(struct platform_device *pdev)
  316. {
  317. DBG("Entered %s\n", __FUNCTION__);
  318. s3c24xx_i2s.regs = ioremap(S3C2410_PA_IIS, 0x100);
  319. if (s3c24xx_i2s.regs == NULL)
  320. return -ENXIO;
  321. s3c24xx_i2s.iis_clk=clk_get(&pdev->dev, "iis");
  322. if (s3c24xx_i2s.iis_clk == NULL) {
  323. DBG("failed to get iis_clock\n");
  324. return -ENODEV;
  325. }
  326. clk_enable(s3c24xx_i2s.iis_clk);
  327. /* Configure the I2S pins in correct mode */
  328. s3c2410_gpio_cfgpin(S3C2410_GPE0, S3C2410_GPE0_I2SLRCK);
  329. s3c2410_gpio_cfgpin(S3C2410_GPE1, S3C2410_GPE1_I2SSCLK);
  330. s3c2410_gpio_cfgpin(S3C2410_GPE2, S3C2410_GPE2_CDCLK);
  331. s3c2410_gpio_cfgpin(S3C2410_GPE3, S3C2410_GPE3_I2SSDI);
  332. s3c2410_gpio_cfgpin(S3C2410_GPE4, S3C2410_GPE4_I2SSDO);
  333. writel(S3C2410_IISCON_IISEN, s3c24xx_i2s.regs + S3C2410_IISCON);
  334. s3c24xx_snd_txctrl(0);
  335. s3c24xx_snd_rxctrl(0);
  336. return 0;
  337. }
  338. #define S3C24XX_I2S_RATES \
  339. (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
  340. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  341. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
  342. struct snd_soc_cpu_dai s3c24xx_i2s_dai = {
  343. .name = "s3c24xx-i2s",
  344. .id = 0,
  345. .type = SND_SOC_DAI_I2S,
  346. .probe = s3c24xx_i2s_probe,
  347. .playback = {
  348. .channels_min = 2,
  349. .channels_max = 2,
  350. .rates = S3C24XX_I2S_RATES,
  351. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
  352. .capture = {
  353. .channels_min = 2,
  354. .channels_max = 2,
  355. .rates = S3C24XX_I2S_RATES,
  356. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
  357. .ops = {
  358. .trigger = s3c24xx_i2s_trigger,
  359. .hw_params = s3c24xx_i2s_hw_params,},
  360. .dai_ops = {
  361. .set_fmt = s3c24xx_i2s_set_fmt,
  362. .set_clkdiv = s3c24xx_i2s_set_clkdiv,
  363. .set_sysclk = s3c24xx_i2s_set_sysclk,
  364. },
  365. };
  366. EXPORT_SYMBOL_GPL(s3c24xx_i2s_dai);
  367. /* Module information */
  368. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  369. MODULE_DESCRIPTION("s3c24xx I2S SoC Interface");
  370. MODULE_LICENSE("GPL");