pxa2xx-i2s.c 9.7 KB

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