s3c24xx-i2s.c 11 KB

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