pxa2xx-i2s.c 9.9 KB

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