s3c24xx-i2s.c 13 KB

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