pxa2xx-i2s.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * pxa2xx-i2s.c -- ALSA Soc Audio Layer
  3. *
  4. * Copyright 2005 Wolfson Microelectronics PLC.
  5. * Author: Liam Girdwood
  6. * lrg@slimlogic.co.uk
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/device.h>
  16. #include <linux/delay.h>
  17. #include <linux/clk.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/io.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/initval.h>
  23. #include <sound/soc.h>
  24. #include <sound/pxa2xx-lib.h>
  25. #include <mach/hardware.h>
  26. #include <mach/dma.h>
  27. #include <mach/audio.h>
  28. #include "pxa2xx-i2s.h"
  29. /*
  30. * I2S Controller Register and Bit Definitions
  31. */
  32. #define SACR0 __REG(0x40400000) /* Global Control Register */
  33. #define SACR1 __REG(0x40400004) /* Serial Audio I 2 S/MSB-Justified Control Register */
  34. #define SASR0 __REG(0x4040000C) /* Serial Audio I 2 S/MSB-Justified Interface and FIFO Status Register */
  35. #define SAIMR __REG(0x40400014) /* Serial Audio Interrupt Mask Register */
  36. #define SAICR __REG(0x40400018) /* Serial Audio Interrupt Clear Register */
  37. #define SADIV __REG(0x40400060) /* Audio Clock Divider Register. */
  38. #define SADR __REG(0x40400080) /* Serial Audio Data Register (TX and RX FIFO access Register). */
  39. #define SACR0_RFTH(x) ((x) << 12) /* Rx FIFO Interrupt or DMA Trigger Threshold */
  40. #define SACR0_TFTH(x) ((x) << 8) /* Tx FIFO Interrupt or DMA Trigger Threshold */
  41. #define SACR0_STRF (1 << 5) /* FIFO Select for EFWR Special Function */
  42. #define SACR0_EFWR (1 << 4) /* Enable EFWR Function */
  43. #define SACR0_RST (1 << 3) /* FIFO, i2s Register Reset */
  44. #define SACR0_BCKD (1 << 2) /* Bit Clock Direction */
  45. #define SACR0_ENB (1 << 0) /* Enable I2S Link */
  46. #define SACR1_ENLBF (1 << 5) /* Enable Loopback */
  47. #define SACR1_DRPL (1 << 4) /* Disable Replaying Function */
  48. #define SACR1_DREC (1 << 3) /* Disable Recording Function */
  49. #define SACR1_AMSL (1 << 0) /* Specify Alternate Mode */
  50. #define SASR0_I2SOFF (1 << 7) /* Controller Status */
  51. #define SASR0_ROR (1 << 6) /* Rx FIFO Overrun */
  52. #define SASR0_TUR (1 << 5) /* Tx FIFO Underrun */
  53. #define SASR0_RFS (1 << 4) /* Rx FIFO Service Request */
  54. #define SASR0_TFS (1 << 3) /* Tx FIFO Service Request */
  55. #define SASR0_BSY (1 << 2) /* I2S Busy */
  56. #define SASR0_RNE (1 << 1) /* Rx FIFO Not Empty */
  57. #define SASR0_TNF (1 << 0) /* Tx FIFO Not Empty */
  58. #define SAICR_ROR (1 << 6) /* Clear Rx FIFO Overrun Interrupt */
  59. #define SAICR_TUR (1 << 5) /* Clear Tx FIFO Underrun Interrupt */
  60. #define SAIMR_ROR (1 << 6) /* Enable Rx FIFO Overrun Condition Interrupt */
  61. #define SAIMR_TUR (1 << 5) /* Enable Tx FIFO Underrun Condition Interrupt */
  62. #define SAIMR_RFS (1 << 4) /* Enable Rx FIFO Service Interrupt */
  63. #define SAIMR_TFS (1 << 3) /* Enable Tx FIFO Service Interrupt */
  64. struct pxa_i2s_port {
  65. u32 sadiv;
  66. u32 sacr0;
  67. u32 sacr1;
  68. u32 saimr;
  69. int master;
  70. u32 fmt;
  71. };
  72. static struct pxa_i2s_port pxa_i2s;
  73. static struct clk *clk_i2s;
  74. static int clk_ena = 0;
  75. static struct pxa2xx_pcm_dma_params pxa2xx_i2s_pcm_stereo_out = {
  76. .name = "I2S PCM Stereo out",
  77. .dev_addr = __PREG(SADR),
  78. .drcmr = &DRCMR(3),
  79. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  80. DCMD_BURST32 | DCMD_WIDTH4,
  81. };
  82. static struct pxa2xx_pcm_dma_params pxa2xx_i2s_pcm_stereo_in = {
  83. .name = "I2S PCM Stereo in",
  84. .dev_addr = __PREG(SADR),
  85. .drcmr = &DRCMR(2),
  86. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  87. DCMD_BURST32 | DCMD_WIDTH4,
  88. };
  89. static int pxa2xx_i2s_startup(struct snd_pcm_substream *substream,
  90. struct snd_soc_dai *dai)
  91. {
  92. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  93. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  94. if (IS_ERR(clk_i2s))
  95. return PTR_ERR(clk_i2s);
  96. if (!cpu_dai->active)
  97. SACR0 = 0;
  98. return 0;
  99. }
  100. /* wait for I2S controller to be ready */
  101. static int pxa_i2s_wait(void)
  102. {
  103. int i;
  104. /* flush the Rx FIFO */
  105. for(i = 0; i < 16; i++)
  106. SADR;
  107. return 0;
  108. }
  109. static int pxa2xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  110. unsigned int fmt)
  111. {
  112. /* interface format */
  113. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  114. case SND_SOC_DAIFMT_I2S:
  115. pxa_i2s.fmt = 0;
  116. break;
  117. case SND_SOC_DAIFMT_LEFT_J:
  118. pxa_i2s.fmt = SACR1_AMSL;
  119. break;
  120. }
  121. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  122. case SND_SOC_DAIFMT_CBS_CFS:
  123. pxa_i2s.master = 1;
  124. break;
  125. case SND_SOC_DAIFMT_CBM_CFS:
  126. pxa_i2s.master = 0;
  127. break;
  128. default:
  129. break;
  130. }
  131. return 0;
  132. }
  133. static int pxa2xx_i2s_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  134. int clk_id, unsigned int freq, int dir)
  135. {
  136. if (clk_id != PXA2XX_I2S_SYSCLK)
  137. return -ENODEV;
  138. return 0;
  139. }
  140. static int pxa2xx_i2s_hw_params(struct snd_pcm_substream *substream,
  141. struct snd_pcm_hw_params *params,
  142. struct snd_soc_dai *dai)
  143. {
  144. struct pxa2xx_pcm_dma_params *dma_data;
  145. BUG_ON(IS_ERR(clk_i2s));
  146. clk_enable(clk_i2s);
  147. clk_ena = 1;
  148. pxa_i2s_wait();
  149. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  150. dma_data = &pxa2xx_i2s_pcm_stereo_out;
  151. else
  152. dma_data = &pxa2xx_i2s_pcm_stereo_in;
  153. snd_soc_dai_set_dma_data(dai, substream, dma_data);
  154. /* is port used by another stream */
  155. if (!(SACR0 & SACR0_ENB)) {
  156. SACR0 = 0;
  157. if (pxa_i2s.master)
  158. SACR0 |= SACR0_BCKD;
  159. SACR0 |= SACR0_RFTH(14) | SACR0_TFTH(1);
  160. SACR1 |= pxa_i2s.fmt;
  161. }
  162. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  163. SAIMR |= SAIMR_TFS;
  164. else
  165. SAIMR |= SAIMR_RFS;
  166. switch (params_rate(params)) {
  167. case 8000:
  168. SADIV = 0x48;
  169. break;
  170. case 11025:
  171. SADIV = 0x34;
  172. break;
  173. case 16000:
  174. SADIV = 0x24;
  175. break;
  176. case 22050:
  177. SADIV = 0x1a;
  178. break;
  179. case 44100:
  180. SADIV = 0xd;
  181. break;
  182. case 48000:
  183. SADIV = 0xc;
  184. break;
  185. case 96000: /* not in manual and possibly slightly inaccurate */
  186. SADIV = 0x6;
  187. break;
  188. }
  189. return 0;
  190. }
  191. static int pxa2xx_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. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  198. SACR1 &= ~SACR1_DRPL;
  199. else
  200. SACR1 &= ~SACR1_DREC;
  201. SACR0 |= SACR0_ENB;
  202. break;
  203. case SNDRV_PCM_TRIGGER_RESUME:
  204. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  205. case SNDRV_PCM_TRIGGER_STOP:
  206. case SNDRV_PCM_TRIGGER_SUSPEND:
  207. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  208. break;
  209. default:
  210. ret = -EINVAL;
  211. }
  212. return ret;
  213. }
  214. static void pxa2xx_i2s_shutdown(struct snd_pcm_substream *substream,
  215. struct snd_soc_dai *dai)
  216. {
  217. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  218. SACR1 |= SACR1_DRPL;
  219. SAIMR &= ~SAIMR_TFS;
  220. } else {
  221. SACR1 |= SACR1_DREC;
  222. SAIMR &= ~SAIMR_RFS;
  223. }
  224. if ((SACR1 & (SACR1_DREC | SACR1_DRPL)) == (SACR1_DREC | SACR1_DRPL)) {
  225. SACR0 &= ~SACR0_ENB;
  226. pxa_i2s_wait();
  227. if (clk_ena) {
  228. clk_disable(clk_i2s);
  229. clk_ena = 0;
  230. }
  231. }
  232. }
  233. #ifdef CONFIG_PM
  234. static int pxa2xx_i2s_suspend(struct snd_soc_dai *dai)
  235. {
  236. /* store registers */
  237. pxa_i2s.sacr0 = SACR0;
  238. pxa_i2s.sacr1 = SACR1;
  239. pxa_i2s.saimr = SAIMR;
  240. pxa_i2s.sadiv = SADIV;
  241. /* deactivate link */
  242. SACR0 &= ~SACR0_ENB;
  243. pxa_i2s_wait();
  244. return 0;
  245. }
  246. static int pxa2xx_i2s_resume(struct snd_soc_dai *dai)
  247. {
  248. pxa_i2s_wait();
  249. SACR0 = pxa_i2s.sacr0 & ~SACR0_ENB;
  250. SACR1 = pxa_i2s.sacr1;
  251. SAIMR = pxa_i2s.saimr;
  252. SADIV = pxa_i2s.sadiv;
  253. SACR0 = pxa_i2s.sacr0;
  254. return 0;
  255. }
  256. #else
  257. #define pxa2xx_i2s_suspend NULL
  258. #define pxa2xx_i2s_resume NULL
  259. #endif
  260. static int pxa2xx_i2s_probe(struct snd_soc_dai *dai)
  261. {
  262. clk_i2s = clk_get(dai->dev, "I2SCLK");
  263. if (IS_ERR(clk_i2s))
  264. return PTR_ERR(clk_i2s);
  265. /*
  266. * PXA Developer's Manual:
  267. * If SACR0[ENB] is toggled in the middle of a normal operation,
  268. * the SACR0[RST] bit must also be set and cleared to reset all
  269. * I2S controller registers.
  270. */
  271. SACR0 = SACR0_RST;
  272. SACR0 = 0;
  273. /* Make sure RPL and REC are disabled */
  274. SACR1 = SACR1_DRPL | SACR1_DREC;
  275. /* Along with FIFO servicing */
  276. SAIMR &= ~(SAIMR_RFS | SAIMR_TFS);
  277. return 0;
  278. }
  279. static int pxa2xx_i2s_remove(struct snd_soc_dai *dai)
  280. {
  281. clk_put(clk_i2s);
  282. clk_i2s = ERR_PTR(-ENOENT);
  283. return 0;
  284. }
  285. #define PXA2XX_I2S_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  286. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
  287. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000)
  288. static const struct snd_soc_dai_ops pxa_i2s_dai_ops = {
  289. .startup = pxa2xx_i2s_startup,
  290. .shutdown = pxa2xx_i2s_shutdown,
  291. .trigger = pxa2xx_i2s_trigger,
  292. .hw_params = pxa2xx_i2s_hw_params,
  293. .set_fmt = pxa2xx_i2s_set_dai_fmt,
  294. .set_sysclk = pxa2xx_i2s_set_dai_sysclk,
  295. };
  296. static struct snd_soc_dai_driver pxa_i2s_dai = {
  297. .probe = pxa2xx_i2s_probe,
  298. .remove = pxa2xx_i2s_remove,
  299. .suspend = pxa2xx_i2s_suspend,
  300. .resume = pxa2xx_i2s_resume,
  301. .playback = {
  302. .channels_min = 2,
  303. .channels_max = 2,
  304. .rates = PXA2XX_I2S_RATES,
  305. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  306. .capture = {
  307. .channels_min = 2,
  308. .channels_max = 2,
  309. .rates = PXA2XX_I2S_RATES,
  310. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  311. .ops = &pxa_i2s_dai_ops,
  312. .symmetric_rates = 1,
  313. };
  314. static int pxa2xx_i2s_drv_probe(struct platform_device *pdev)
  315. {
  316. return snd_soc_register_dai(&pdev->dev, &pxa_i2s_dai);
  317. }
  318. static int __devexit pxa2xx_i2s_drv_remove(struct platform_device *pdev)
  319. {
  320. snd_soc_unregister_dai(&pdev->dev);
  321. return 0;
  322. }
  323. static struct platform_driver pxa2xx_i2s_driver = {
  324. .probe = pxa2xx_i2s_drv_probe,
  325. .remove = __devexit_p(pxa2xx_i2s_drv_remove),
  326. .driver = {
  327. .name = "pxa2xx-i2s",
  328. .owner = THIS_MODULE,
  329. },
  330. };
  331. static int __init pxa2xx_i2s_init(void)
  332. {
  333. clk_i2s = ERR_PTR(-ENOENT);
  334. return platform_driver_register(&pxa2xx_i2s_driver);
  335. }
  336. static void __exit pxa2xx_i2s_exit(void)
  337. {
  338. platform_driver_unregister(&pxa2xx_i2s_driver);
  339. }
  340. module_init(pxa2xx_i2s_init);
  341. module_exit(pxa2xx_i2s_exit);
  342. /* Module information */
  343. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  344. MODULE_DESCRIPTION("pxa2xx I2S SoC Interface");
  345. MODULE_LICENSE("GPL");
  346. MODULE_ALIAS("platform:pxa2xx-i2s");