s3c24xx-i2s.c 12 KB

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