s3c24xx-i2s.c 12 KB

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