s3c24xx-i2s.c 12 KB

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