bf5xx-i2s.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * File: sound/soc/blackfin/bf5xx-i2s.c
  3. * Author: Cliff Cai <Cliff.Cai@analog.com>
  4. *
  5. * Created: Tue June 06 2008
  6. * Description: Blackfin I2S CPU DAI driver
  7. *
  8. * Modified:
  9. * Copyright 2008 Analog Devices Inc.
  10. *
  11. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, see the file COPYING, or write
  25. * to the Free Software Foundation, Inc.,
  26. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. #include <linux/init.h>
  29. #include <linux/module.h>
  30. #include <linux/device.h>
  31. #include <linux/delay.h>
  32. #include <sound/core.h>
  33. #include <sound/pcm.h>
  34. #include <sound/pcm_params.h>
  35. #include <sound/initval.h>
  36. #include <sound/soc.h>
  37. #include <asm/irq.h>
  38. #include <asm/portmux.h>
  39. #include <linux/mutex.h>
  40. #include <linux/gpio.h>
  41. #include "bf5xx-sport.h"
  42. #include "bf5xx-i2s.h"
  43. struct bf5xx_i2s_port {
  44. u16 tcr1;
  45. u16 rcr1;
  46. u16 tcr2;
  47. u16 rcr2;
  48. int configured;
  49. };
  50. static struct bf5xx_i2s_port bf5xx_i2s;
  51. static int sport_num = CONFIG_SND_BF5XX_SPORT_NUM;
  52. static struct sport_param sport_params[2] = {
  53. {
  54. .dma_rx_chan = CH_SPORT0_RX,
  55. .dma_tx_chan = CH_SPORT0_TX,
  56. .err_irq = IRQ_SPORT0_ERROR,
  57. .regs = (struct sport_register *)SPORT0_TCR1,
  58. },
  59. {
  60. .dma_rx_chan = CH_SPORT1_RX,
  61. .dma_tx_chan = CH_SPORT1_TX,
  62. .err_irq = IRQ_SPORT1_ERROR,
  63. .regs = (struct sport_register *)SPORT1_TCR1,
  64. }
  65. };
  66. /*
  67. * Setting the TFS pin selector for SPORT 0 based on whether the selected
  68. * port id F or G. If the port is F then no conflict should exist for the
  69. * TFS. When Port G is selected and EMAC then there is a conflict between
  70. * the PHY interrupt line and TFS. Current settings prevent the conflict
  71. * by ignoring the TFS pin when Port G is selected. This allows both
  72. * codecs and EMAC using Port G concurrently.
  73. */
  74. #ifdef CONFIG_BF527_SPORT0_PORTG
  75. #define LOCAL_SPORT0_TFS (0)
  76. #else
  77. #define LOCAL_SPORT0_TFS (P_SPORT0_TFS)
  78. #endif
  79. static u16 sport_req[][7] = { {P_SPORT0_DTPRI, P_SPORT0_TSCLK, P_SPORT0_RFS,
  80. P_SPORT0_DRPRI, P_SPORT0_RSCLK, LOCAL_SPORT0_TFS, 0},
  81. {P_SPORT1_DTPRI, P_SPORT1_TSCLK, P_SPORT1_RFS, P_SPORT1_DRPRI,
  82. P_SPORT1_RSCLK, P_SPORT1_TFS, 0} };
  83. static int bf5xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  84. unsigned int fmt)
  85. {
  86. int ret = 0;
  87. /* interface format:support I2S,slave mode */
  88. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  89. case SND_SOC_DAIFMT_I2S:
  90. bf5xx_i2s.tcr1 |= TFSR | TCKFE;
  91. bf5xx_i2s.rcr1 |= RFSR | RCKFE;
  92. bf5xx_i2s.tcr2 |= TSFSE;
  93. bf5xx_i2s.rcr2 |= RSFSE;
  94. break;
  95. case SND_SOC_DAIFMT_DSP_A:
  96. bf5xx_i2s.tcr1 |= TFSR;
  97. bf5xx_i2s.rcr1 |= RFSR;
  98. break;
  99. case SND_SOC_DAIFMT_LEFT_J:
  100. ret = -EINVAL;
  101. break;
  102. default:
  103. printk(KERN_ERR "%s: Unknown DAI format type\n", __func__);
  104. ret = -EINVAL;
  105. break;
  106. }
  107. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  108. case SND_SOC_DAIFMT_CBM_CFM:
  109. break;
  110. case SND_SOC_DAIFMT_CBS_CFS:
  111. case SND_SOC_DAIFMT_CBM_CFS:
  112. case SND_SOC_DAIFMT_CBS_CFM:
  113. ret = -EINVAL;
  114. break;
  115. default:
  116. printk(KERN_ERR "%s: Unknown DAI master type\n", __func__);
  117. ret = -EINVAL;
  118. break;
  119. }
  120. return ret;
  121. }
  122. static int bf5xx_i2s_hw_params(struct snd_pcm_substream *substream,
  123. struct snd_pcm_hw_params *params,
  124. struct snd_soc_dai *dai)
  125. {
  126. int ret = 0;
  127. bf5xx_i2s.tcr2 &= ~0x1f;
  128. bf5xx_i2s.rcr2 &= ~0x1f;
  129. switch (params_format(params)) {
  130. case SNDRV_PCM_FORMAT_S16_LE:
  131. bf5xx_i2s.tcr2 |= 15;
  132. bf5xx_i2s.rcr2 |= 15;
  133. sport_handle->wdsize = 2;
  134. break;
  135. case SNDRV_PCM_FORMAT_S24_LE:
  136. bf5xx_i2s.tcr2 |= 23;
  137. bf5xx_i2s.rcr2 |= 23;
  138. sport_handle->wdsize = 3;
  139. break;
  140. case SNDRV_PCM_FORMAT_S32_LE:
  141. bf5xx_i2s.tcr2 |= 31;
  142. bf5xx_i2s.rcr2 |= 31;
  143. sport_handle->wdsize = 4;
  144. break;
  145. }
  146. if (!bf5xx_i2s.configured) {
  147. /*
  148. * TX and RX are not independent,they are enabled at the
  149. * same time, even if only one side is running. So, we
  150. * need to configure both of them at the time when the first
  151. * stream is opened.
  152. *
  153. * CPU DAI:slave mode.
  154. */
  155. bf5xx_i2s.configured = 1;
  156. ret = sport_config_rx(sport_handle, bf5xx_i2s.rcr1,
  157. bf5xx_i2s.rcr2, 0, 0);
  158. if (ret) {
  159. pr_err("SPORT is busy!\n");
  160. return -EBUSY;
  161. }
  162. ret = sport_config_tx(sport_handle, bf5xx_i2s.tcr1,
  163. bf5xx_i2s.tcr2, 0, 0);
  164. if (ret) {
  165. pr_err("SPORT is busy!\n");
  166. return -EBUSY;
  167. }
  168. }
  169. return 0;
  170. }
  171. static void bf5xx_i2s_shutdown(struct snd_pcm_substream *substream,
  172. struct snd_soc_dai *dai)
  173. {
  174. pr_debug("%s enter\n", __func__);
  175. /* No active stream, SPORT is allowed to be configured again. */
  176. if (!dai->active)
  177. bf5xx_i2s.configured = 0;
  178. }
  179. static int bf5xx_i2s_probe(struct platform_device *pdev,
  180. struct snd_soc_dai *dai)
  181. {
  182. pr_debug("%s enter\n", __func__);
  183. if (peripheral_request_list(&sport_req[sport_num][0], "soc-audio")) {
  184. pr_err("Requesting Peripherals failed\n");
  185. return -EFAULT;
  186. }
  187. /* request DMA for SPORT */
  188. sport_handle = sport_init(&sport_params[sport_num], 4, \
  189. 2 * sizeof(u32), NULL);
  190. if (!sport_handle) {
  191. peripheral_free_list(&sport_req[sport_num][0]);
  192. return -ENODEV;
  193. }
  194. return 0;
  195. }
  196. static void bf5xx_i2s_remove(struct platform_device *pdev,
  197. struct snd_soc_dai *dai)
  198. {
  199. pr_debug("%s enter\n", __func__);
  200. peripheral_free_list(&sport_req[sport_num][0]);
  201. }
  202. #ifdef CONFIG_PM
  203. static int bf5xx_i2s_suspend(struct snd_soc_dai *dai)
  204. {
  205. pr_debug("%s : sport %d\n", __func__, dai->id);
  206. if (dai->capture.active)
  207. sport_rx_stop(sport_handle);
  208. if (dai->playback.active)
  209. sport_tx_stop(sport_handle);
  210. return 0;
  211. }
  212. static int bf5xx_i2s_resume(struct snd_soc_dai *dai)
  213. {
  214. int ret;
  215. pr_debug("%s : sport %d\n", __func__, dai->id);
  216. ret = sport_config_rx(sport_handle, bf5xx_i2s.rcr1,
  217. bf5xx_i2s.rcr2, 0, 0);
  218. if (ret) {
  219. pr_err("SPORT is busy!\n");
  220. return -EBUSY;
  221. }
  222. ret = sport_config_tx(sport_handle, bf5xx_i2s.tcr1,
  223. bf5xx_i2s.tcr2, 0, 0);
  224. if (ret) {
  225. pr_err("SPORT is busy!\n");
  226. return -EBUSY;
  227. }
  228. return 0;
  229. }
  230. #else
  231. #define bf5xx_i2s_suspend NULL
  232. #define bf5xx_i2s_resume NULL
  233. #endif
  234. #define BF5XX_I2S_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  235. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
  236. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
  237. SNDRV_PCM_RATE_96000)
  238. #define BF5XX_I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |\
  239. SNDRV_PCM_FMTBIT_S32_LE)
  240. static struct snd_soc_dai_ops bf5xx_i2s_dai_ops = {
  241. .shutdown = bf5xx_i2s_shutdown,
  242. .hw_params = bf5xx_i2s_hw_params,
  243. .set_fmt = bf5xx_i2s_set_dai_fmt,
  244. };
  245. struct snd_soc_dai bf5xx_i2s_dai = {
  246. .name = "bf5xx-i2s",
  247. .id = 0,
  248. .probe = bf5xx_i2s_probe,
  249. .remove = bf5xx_i2s_remove,
  250. .suspend = bf5xx_i2s_suspend,
  251. .resume = bf5xx_i2s_resume,
  252. .playback = {
  253. .channels_min = 1,
  254. .channels_max = 2,
  255. .rates = BF5XX_I2S_RATES,
  256. .formats = BF5XX_I2S_FORMATS,},
  257. .capture = {
  258. .channels_min = 1,
  259. .channels_max = 2,
  260. .rates = BF5XX_I2S_RATES,
  261. .formats = BF5XX_I2S_FORMATS,},
  262. .ops = &bf5xx_i2s_dai_ops,
  263. };
  264. EXPORT_SYMBOL_GPL(bf5xx_i2s_dai);
  265. static int __init bfin_i2s_init(void)
  266. {
  267. return snd_soc_register_dai(&bf5xx_i2s_dai);
  268. }
  269. module_init(bfin_i2s_init);
  270. static void __exit bfin_i2s_exit(void)
  271. {
  272. snd_soc_unregister_dai(&bf5xx_i2s_dai);
  273. }
  274. module_exit(bfin_i2s_exit);
  275. /* Module information */
  276. MODULE_AUTHOR("Cliff Cai");
  277. MODULE_DESCRIPTION("I2S driver for ADI Blackfin");
  278. MODULE_LICENSE("GPL");