bf5xx-i2s.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. struct bf5xx_i2s_port {
  43. u16 tcr1;
  44. u16 rcr1;
  45. u16 tcr2;
  46. u16 rcr2;
  47. int configured;
  48. };
  49. static struct bf5xx_i2s_port bf5xx_i2s;
  50. static int sport_num = CONFIG_SND_BF5XX_SPORT_NUM;
  51. static struct sport_param sport_params[2] = {
  52. {
  53. .dma_rx_chan = CH_SPORT0_RX,
  54. .dma_tx_chan = CH_SPORT0_TX,
  55. .err_irq = IRQ_SPORT0_ERROR,
  56. .regs = (struct sport_register *)SPORT0_TCR1,
  57. },
  58. {
  59. .dma_rx_chan = CH_SPORT1_RX,
  60. .dma_tx_chan = CH_SPORT1_TX,
  61. .err_irq = IRQ_SPORT1_ERROR,
  62. .regs = (struct sport_register *)SPORT1_TCR1,
  63. }
  64. };
  65. /*
  66. * Setting the TFS pin selector for SPORT 0 based on whether the selected
  67. * port id F or G. If the port is F then no conflict should exist for the
  68. * TFS. When Port G is selected and EMAC then there is a conflict between
  69. * the PHY interrupt line and TFS. Current settings prevent the conflict
  70. * by ignoring the TFS pin when Port G is selected. This allows both
  71. * codecs and EMAC using Port G concurrently.
  72. */
  73. #ifdef CONFIG_BF527_SPORT0_PORTG
  74. #define LOCAL_SPORT0_TFS (0)
  75. #else
  76. #define LOCAL_SPORT0_TFS (P_SPORT0_TFS)
  77. #endif
  78. static u16 sport_req[][7] = { {P_SPORT0_DTPRI, P_SPORT0_TSCLK, P_SPORT0_RFS,
  79. P_SPORT0_DRPRI, P_SPORT0_RSCLK, LOCAL_SPORT0_TFS, 0},
  80. {P_SPORT1_DTPRI, P_SPORT1_TSCLK, P_SPORT1_RFS, P_SPORT1_DRPRI,
  81. P_SPORT1_RSCLK, P_SPORT1_TFS, 0} };
  82. static int bf5xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  83. unsigned int fmt)
  84. {
  85. int ret = 0;
  86. /* interface format:support I2S,slave mode */
  87. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  88. case SND_SOC_DAIFMT_I2S:
  89. bf5xx_i2s.tcr1 |= TFSR | TCKFE;
  90. bf5xx_i2s.rcr1 |= RFSR | RCKFE;
  91. bf5xx_i2s.tcr2 |= TSFSE;
  92. bf5xx_i2s.rcr2 |= RSFSE;
  93. break;
  94. case SND_SOC_DAIFMT_DSP_A:
  95. bf5xx_i2s.tcr1 |= TFSR;
  96. bf5xx_i2s.rcr1 |= RFSR;
  97. break;
  98. case SND_SOC_DAIFMT_LEFT_J:
  99. ret = -EINVAL;
  100. break;
  101. default:
  102. printk(KERN_ERR "%s: Unknown DAI format type\n", __func__);
  103. ret = -EINVAL;
  104. break;
  105. }
  106. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  107. case SND_SOC_DAIFMT_CBM_CFM:
  108. break;
  109. case SND_SOC_DAIFMT_CBS_CFS:
  110. case SND_SOC_DAIFMT_CBM_CFS:
  111. case SND_SOC_DAIFMT_CBS_CFM:
  112. ret = -EINVAL;
  113. break;
  114. default:
  115. printk(KERN_ERR "%s: Unknown DAI master type\n", __func__);
  116. ret = -EINVAL;
  117. break;
  118. }
  119. return ret;
  120. }
  121. static int bf5xx_i2s_hw_params(struct snd_pcm_substream *substream,
  122. struct snd_pcm_hw_params *params,
  123. struct snd_soc_dai *dai)
  124. {
  125. int ret = 0;
  126. bf5xx_i2s.tcr2 &= ~0x1f;
  127. bf5xx_i2s.rcr2 &= ~0x1f;
  128. switch (params_format(params)) {
  129. case SNDRV_PCM_FORMAT_S8:
  130. bf5xx_i2s->tcr2 |= 7;
  131. bf5xx_i2s->rcr2 |= 7;
  132. sport_handle->wdsize = 1;
  133. case SNDRV_PCM_FORMAT_S16_LE:
  134. bf5xx_i2s.tcr2 |= 15;
  135. bf5xx_i2s.rcr2 |= 15;
  136. sport_handle->wdsize = 2;
  137. break;
  138. case SNDRV_PCM_FORMAT_S24_LE:
  139. bf5xx_i2s.tcr2 |= 23;
  140. bf5xx_i2s.rcr2 |= 23;
  141. sport_handle->wdsize = 3;
  142. break;
  143. case SNDRV_PCM_FORMAT_S32_LE:
  144. bf5xx_i2s.tcr2 |= 31;
  145. bf5xx_i2s.rcr2 |= 31;
  146. sport_handle->wdsize = 4;
  147. break;
  148. }
  149. if (!bf5xx_i2s.configured) {
  150. /*
  151. * TX and RX are not independent,they are enabled at the
  152. * same time, even if only one side is running. So, we
  153. * need to configure both of them at the time when the first
  154. * stream is opened.
  155. *
  156. * CPU DAI:slave mode.
  157. */
  158. bf5xx_i2s.configured = 1;
  159. ret = sport_config_rx(sport_handle, bf5xx_i2s.rcr1,
  160. bf5xx_i2s.rcr2, 0, 0);
  161. if (ret) {
  162. pr_err("SPORT is busy!\n");
  163. return -EBUSY;
  164. }
  165. ret = sport_config_tx(sport_handle, bf5xx_i2s.tcr1,
  166. bf5xx_i2s.tcr2, 0, 0);
  167. if (ret) {
  168. pr_err("SPORT is busy!\n");
  169. return -EBUSY;
  170. }
  171. }
  172. return 0;
  173. }
  174. static void bf5xx_i2s_shutdown(struct snd_pcm_substream *substream,
  175. struct snd_soc_dai *dai)
  176. {
  177. pr_debug("%s enter\n", __func__);
  178. /* No active stream, SPORT is allowed to be configured again. */
  179. if (!dai->active)
  180. bf5xx_i2s.configured = 0;
  181. }
  182. static int bf5xx_i2s_probe(struct snd_soc_dai *dai)
  183. {
  184. pr_debug("%s enter\n", __func__);
  185. if (peripheral_request_list(&sport_req[sport_num][0], "soc-audio")) {
  186. pr_err("Requesting Peripherals failed\n");
  187. return -EFAULT;
  188. }
  189. /* request DMA for SPORT */
  190. sport_handle = sport_init(&sport_params[sport_num], 4, \
  191. 2 * sizeof(u32), NULL);
  192. if (!sport_handle) {
  193. peripheral_free_list(&sport_req[sport_num][0]);
  194. return -ENODEV;
  195. }
  196. return 0;
  197. }
  198. static int bf5xx_i2s_remove(struct snd_soc_dai *dai)
  199. {
  200. pr_debug("%s enter\n", __func__);
  201. peripheral_free_list(&sport_req[sport_num][0]);
  202. return 0;
  203. }
  204. #ifdef CONFIG_PM
  205. static int bf5xx_i2s_suspend(struct snd_soc_dai *dai)
  206. {
  207. pr_debug("%s : sport %d\n", __func__, dai->id);
  208. if (dai->capture_active)
  209. sport_rx_stop(sport_handle);
  210. if (dai->playback_active)
  211. sport_tx_stop(sport_handle);
  212. return 0;
  213. }
  214. static int bf5xx_i2s_resume(struct snd_soc_dai *dai)
  215. {
  216. int ret;
  217. pr_debug("%s : sport %d\n", __func__, dai->id);
  218. ret = sport_config_rx(sport_handle, bf5xx_i2s.rcr1,
  219. bf5xx_i2s.rcr2, 0, 0);
  220. if (ret) {
  221. pr_err("SPORT is busy!\n");
  222. return -EBUSY;
  223. }
  224. ret = sport_config_tx(sport_handle, bf5xx_i2s.tcr1,
  225. bf5xx_i2s.tcr2, 0, 0);
  226. if (ret) {
  227. pr_err("SPORT is busy!\n");
  228. return -EBUSY;
  229. }
  230. return 0;
  231. }
  232. #else
  233. #define bf5xx_i2s_suspend NULL
  234. #define bf5xx_i2s_resume NULL
  235. #endif
  236. #define BF5XX_I2S_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  237. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
  238. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
  239. SNDRV_PCM_RATE_96000)
  240. #define BF5XX_I2S_FORMATS \
  241. (SNDRV_PCM_FMTBIT_S8 | \
  242. SNDRV_PCM_FMTBIT_S16_LE | \
  243. SNDRV_PCM_FMTBIT_S24_LE | \
  244. SNDRV_PCM_FMTBIT_S32_LE)
  245. static struct snd_soc_dai_ops bf5xx_i2s_dai_ops = {
  246. .shutdown = bf5xx_i2s_shutdown,
  247. .hw_params = bf5xx_i2s_hw_params,
  248. .set_fmt = bf5xx_i2s_set_dai_fmt,
  249. };
  250. static struct snd_soc_dai_driver bf5xx_i2s_dai = {
  251. .probe = bf5xx_i2s_probe,
  252. .remove = bf5xx_i2s_remove,
  253. .suspend = bf5xx_i2s_suspend,
  254. .resume = bf5xx_i2s_resume,
  255. .playback = {
  256. .channels_min = 1,
  257. .channels_max = 2,
  258. .rates = BF5XX_I2S_RATES,
  259. .formats = BF5XX_I2S_FORMATS,},
  260. .capture = {
  261. .channels_min = 1,
  262. .channels_max = 2,
  263. .rates = BF5XX_I2S_RATES,
  264. .formats = BF5XX_I2S_FORMATS,},
  265. .ops = &bf5xx_i2s_dai_ops,
  266. };
  267. static int bfin_i2s_drv_probe(struct platform_device *pdev)
  268. {
  269. return snd_soc_register_dai(&pdev->dev, &bf5xx_i2s_dai);
  270. }
  271. static int __devexit bfin_i2s_drv_remove(struct platform_device *pdev)
  272. {
  273. snd_soc_unregister_dai(&pdev->dev);
  274. return 0;
  275. }
  276. static struct platform_driver bfin_i2s_driver = {
  277. .probe = bfin_i2s_drv_probe,
  278. .remove = __devexit_p(bfin_i2s_drv_remove),
  279. .driver = {
  280. .name = "bfin-i2s",
  281. .owner = THIS_MODULE,
  282. },
  283. };
  284. static int __init bfin_i2s_init(void)
  285. {
  286. return platform_driver_register(&bfin_i2s_driver);
  287. }
  288. static void __exit bfin_i2s_exit(void)
  289. {
  290. platform_driver_unregister(&bfin_i2s_driver);
  291. }
  292. module_init(bfin_i2s_init);
  293. module_exit(bfin_i2s_exit);
  294. /* Module information */
  295. MODULE_AUTHOR("Cliff Cai");
  296. MODULE_DESCRIPTION("I2S driver for ADI Blackfin");
  297. MODULE_LICENSE("GPL");