pxa2xx-i2s.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 <sound/dmaengine_pcm.h>
  26. #include <mach/hardware.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 snd_dmaengine_dai_dma_data pxa2xx_i2s_pcm_stereo_out = {
  76. .addr = __PREG(SADR),
  77. .addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  78. .chan_name = "tx",
  79. .maxburst = 32,
  80. };
  81. static struct snd_dmaengine_dai_dma_data pxa2xx_i2s_pcm_stereo_in = {
  82. .addr = __PREG(SADR),
  83. .addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  84. .chan_name = "rx",
  85. .maxburst = 32,
  86. };
  87. static int pxa2xx_i2s_startup(struct snd_pcm_substream *substream,
  88. struct snd_soc_dai *dai)
  89. {
  90. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  91. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  92. if (IS_ERR(clk_i2s))
  93. return PTR_ERR(clk_i2s);
  94. if (!cpu_dai->active)
  95. SACR0 = 0;
  96. return 0;
  97. }
  98. /* wait for I2S controller to be ready */
  99. static int pxa_i2s_wait(void)
  100. {
  101. int i;
  102. /* flush the Rx FIFO */
  103. for (i = 0; i < 16; i++)
  104. SADR;
  105. return 0;
  106. }
  107. static int pxa2xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  108. unsigned int fmt)
  109. {
  110. /* interface format */
  111. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  112. case SND_SOC_DAIFMT_I2S:
  113. pxa_i2s.fmt = 0;
  114. break;
  115. case SND_SOC_DAIFMT_LEFT_J:
  116. pxa_i2s.fmt = SACR1_AMSL;
  117. break;
  118. }
  119. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  120. case SND_SOC_DAIFMT_CBS_CFS:
  121. pxa_i2s.master = 1;
  122. break;
  123. case SND_SOC_DAIFMT_CBM_CFS:
  124. pxa_i2s.master = 0;
  125. break;
  126. default:
  127. break;
  128. }
  129. return 0;
  130. }
  131. static int pxa2xx_i2s_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
  132. int clk_id, unsigned int freq, int dir)
  133. {
  134. if (clk_id != PXA2XX_I2S_SYSCLK)
  135. return -ENODEV;
  136. return 0;
  137. }
  138. static int pxa2xx_i2s_hw_params(struct snd_pcm_substream *substream,
  139. struct snd_pcm_hw_params *params,
  140. struct snd_soc_dai *dai)
  141. {
  142. struct snd_dmaengine_dai_dma_data *dma_data;
  143. if (WARN_ON(IS_ERR(clk_i2s)))
  144. return -EINVAL;
  145. clk_prepare_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_unprepare(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. snd_soc_dai_init_dma_data(dai, &pxa2xx_i2s_pcm_stereo_out,
  277. &pxa2xx_i2s_pcm_stereo_in);
  278. return 0;
  279. }
  280. static int pxa2xx_i2s_remove(struct snd_soc_dai *dai)
  281. {
  282. clk_put(clk_i2s);
  283. clk_i2s = ERR_PTR(-ENOENT);
  284. return 0;
  285. }
  286. #define PXA2XX_I2S_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  287. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
  288. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000)
  289. static const struct snd_soc_dai_ops pxa_i2s_dai_ops = {
  290. .startup = pxa2xx_i2s_startup,
  291. .shutdown = pxa2xx_i2s_shutdown,
  292. .trigger = pxa2xx_i2s_trigger,
  293. .hw_params = pxa2xx_i2s_hw_params,
  294. .set_fmt = pxa2xx_i2s_set_dai_fmt,
  295. .set_sysclk = pxa2xx_i2s_set_dai_sysclk,
  296. };
  297. static struct snd_soc_dai_driver pxa_i2s_dai = {
  298. .probe = pxa2xx_i2s_probe,
  299. .remove = pxa2xx_i2s_remove,
  300. .suspend = pxa2xx_i2s_suspend,
  301. .resume = pxa2xx_i2s_resume,
  302. .playback = {
  303. .channels_min = 2,
  304. .channels_max = 2,
  305. .rates = PXA2XX_I2S_RATES,
  306. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  307. .capture = {
  308. .channels_min = 2,
  309. .channels_max = 2,
  310. .rates = PXA2XX_I2S_RATES,
  311. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  312. .ops = &pxa_i2s_dai_ops,
  313. .symmetric_rates = 1,
  314. };
  315. static const struct snd_soc_component_driver pxa_i2s_component = {
  316. .name = "pxa-i2s",
  317. .ops = &pxa2xx_pcm_ops,
  318. .pcm_new = pxa2xx_soc_pcm_new,
  319. .pcm_free = pxa2xx_pcm_free_dma_buffers,
  320. };
  321. static int pxa2xx_i2s_drv_probe(struct platform_device *pdev)
  322. {
  323. return devm_snd_soc_register_component(&pdev->dev, &pxa_i2s_component,
  324. &pxa_i2s_dai, 1);
  325. }
  326. static struct platform_driver pxa2xx_i2s_driver = {
  327. .probe = pxa2xx_i2s_drv_probe,
  328. .driver = {
  329. .name = "pxa2xx-i2s",
  330. },
  331. };
  332. static int __init pxa2xx_i2s_init(void)
  333. {
  334. clk_i2s = ERR_PTR(-ENOENT);
  335. return platform_driver_register(&pxa2xx_i2s_driver);
  336. }
  337. static void __exit pxa2xx_i2s_exit(void)
  338. {
  339. platform_driver_unregister(&pxa2xx_i2s_driver);
  340. }
  341. module_init(pxa2xx_i2s_init);
  342. module_exit(pxa2xx_i2s_exit);
  343. /* Module information */
  344. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  345. MODULE_DESCRIPTION("pxa2xx I2S SoC Interface");
  346. MODULE_LICENSE("GPL");
  347. MODULE_ALIAS("platform:pxa2xx-i2s");