bf5xx-i2s.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 counter;
  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. static u16 sport_req[][7] = {
  67. { P_SPORT0_DTPRI, P_SPORT0_TSCLK, P_SPORT0_RFS,
  68. P_SPORT0_DRPRI, P_SPORT0_RSCLK, 0},
  69. { P_SPORT1_DTPRI, P_SPORT1_TSCLK, P_SPORT1_RFS,
  70. P_SPORT1_DRPRI, P_SPORT1_RSCLK, 0},
  71. };
  72. static int bf5xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  73. unsigned int fmt)
  74. {
  75. int ret = 0;
  76. /* interface format:support I2S,slave mode */
  77. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  78. case SND_SOC_DAIFMT_I2S:
  79. bf5xx_i2s.tcr1 |= TFSR | TCKFE;
  80. bf5xx_i2s.rcr1 |= RFSR | RCKFE;
  81. bf5xx_i2s.tcr2 |= TSFSE;
  82. bf5xx_i2s.rcr2 |= RSFSE;
  83. break;
  84. case SND_SOC_DAIFMT_DSP_A:
  85. bf5xx_i2s.tcr1 |= TFSR;
  86. bf5xx_i2s.rcr1 |= RFSR;
  87. break;
  88. case SND_SOC_DAIFMT_LEFT_J:
  89. ret = -EINVAL;
  90. break;
  91. default:
  92. ret = -EINVAL;
  93. break;
  94. }
  95. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  96. case SND_SOC_DAIFMT_CBS_CFS:
  97. ret = -EINVAL;
  98. break;
  99. case SND_SOC_DAIFMT_CBM_CFS:
  100. ret = -EINVAL;
  101. break;
  102. case SND_SOC_DAIFMT_CBM_CFM:
  103. break;
  104. case SND_SOC_DAIFMT_CBS_CFM:
  105. ret = -EINVAL;
  106. break;
  107. default:
  108. ret = -EINVAL;
  109. break;
  110. }
  111. return ret;
  112. }
  113. static int bf5xx_i2s_startup(struct snd_pcm_substream *substream)
  114. {
  115. pr_debug("%s enter\n", __func__);
  116. /*this counter is used for counting how many pcm streams are opened*/
  117. bf5xx_i2s.counter++;
  118. return 0;
  119. }
  120. static int bf5xx_i2s_hw_params(struct snd_pcm_substream *substream,
  121. struct snd_pcm_hw_params *params)
  122. {
  123. int ret = 0;
  124. bf5xx_i2s.tcr2 &= ~0x1f;
  125. bf5xx_i2s.rcr2 &= ~0x1f;
  126. switch (params_format(params)) {
  127. case SNDRV_PCM_FORMAT_S16_LE:
  128. bf5xx_i2s.tcr2 |= 15;
  129. bf5xx_i2s.rcr2 |= 15;
  130. sport_handle->wdsize = 2;
  131. break;
  132. case SNDRV_PCM_FORMAT_S24_LE:
  133. bf5xx_i2s.tcr2 |= 23;
  134. bf5xx_i2s.rcr2 |= 23;
  135. sport_handle->wdsize = 3;
  136. break;
  137. case SNDRV_PCM_FORMAT_S32_LE:
  138. bf5xx_i2s.tcr2 |= 31;
  139. bf5xx_i2s.rcr2 |= 31;
  140. sport_handle->wdsize = 4;
  141. break;
  142. }
  143. if (bf5xx_i2s.counter == 1) {
  144. /*
  145. * TX and RX are not independent,they are enabled at the
  146. * same time, even if only one side is running. So, we
  147. * need to configure both of them at the time when the first
  148. * stream is opened.
  149. *
  150. * CPU DAI:slave mode.
  151. */
  152. ret = sport_config_rx(sport_handle, bf5xx_i2s.rcr1,
  153. bf5xx_i2s.rcr2, 0, 0);
  154. if (ret) {
  155. pr_err("SPORT is busy!\n");
  156. return -EBUSY;
  157. }
  158. ret = sport_config_tx(sport_handle, bf5xx_i2s.tcr1,
  159. bf5xx_i2s.tcr2, 0, 0);
  160. if (ret) {
  161. pr_err("SPORT is busy!\n");
  162. return -EBUSY;
  163. }
  164. }
  165. return 0;
  166. }
  167. static void bf5xx_i2s_shutdown(struct snd_pcm_substream *substream)
  168. {
  169. pr_debug("%s enter\n", __func__);
  170. bf5xx_i2s.counter--;
  171. }
  172. static int bf5xx_i2s_probe(struct platform_device *pdev,
  173. struct snd_soc_dai *dai)
  174. {
  175. pr_debug("%s enter\n", __func__);
  176. if (peripheral_request_list(&sport_req[sport_num][0], "soc-audio")) {
  177. pr_err("Requesting Peripherals failed\n");
  178. return -EFAULT;
  179. }
  180. /* request DMA for SPORT */
  181. sport_handle = sport_init(&sport_params[sport_num], 4, \
  182. 2 * sizeof(u32), NULL);
  183. if (!sport_handle) {
  184. peripheral_free_list(&sport_req[sport_num][0]);
  185. return -ENODEV;
  186. }
  187. return 0;
  188. }
  189. static void bf5xx_i2s_remove(struct platform_device *pdev,
  190. struct snd_soc_dai *dai)
  191. {
  192. pr_debug("%s enter\n", __func__);
  193. peripheral_free_list(&sport_req[sport_num][0]);
  194. }
  195. #ifdef CONFIG_PM
  196. static int bf5xx_i2s_suspend(struct platform_device *dev,
  197. struct snd_soc_dai *dai)
  198. {
  199. struct sport_device *sport =
  200. (struct sport_device *)dai->private_data;
  201. pr_debug("%s : sport %d\n", __func__, dai->id);
  202. if (!dai->active)
  203. return 0;
  204. if (dai->capture.active)
  205. sport_rx_stop(sport);
  206. if (dai->playback.active)
  207. sport_tx_stop(sport);
  208. return 0;
  209. }
  210. static int bf5xx_i2s_resume(struct platform_device *pdev,
  211. struct snd_soc_dai *dai)
  212. {
  213. int ret;
  214. struct sport_device *sport =
  215. (struct sport_device *)dai->private_data;
  216. pr_debug("%s : sport %d\n", __func__, dai->id);
  217. if (!dai->active)
  218. return 0;
  219. ret = sport_config_rx(sport_handle, RFSR | RCKFE, RSFSE|0x1f, 0, 0);
  220. if (ret) {
  221. pr_err("SPORT is busy!\n");
  222. return -EBUSY;
  223. }
  224. ret = sport_config_tx(sport_handle, TFSR | TCKFE, TSFSE|0x1f, 0, 0);
  225. if (ret) {
  226. pr_err("SPORT is busy!\n");
  227. return -EBUSY;
  228. }
  229. if (dai->capture.active)
  230. sport_rx_start(sport);
  231. if (dai->playback.active)
  232. sport_tx_start(sport);
  233. return 0;
  234. }
  235. #else
  236. #define bf5xx_i2s_suspend NULL
  237. #define bf5xx_i2s_resume NULL
  238. #endif
  239. #define BF5XX_I2S_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  240. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
  241. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
  242. SNDRV_PCM_RATE_96000)
  243. #define BF5XX_I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |\
  244. SNDRV_PCM_FMTBIT_S32_LE)
  245. struct snd_soc_dai bf5xx_i2s_dai = {
  246. .name = "bf5xx-i2s",
  247. .id = 0,
  248. .type = SND_SOC_DAI_I2S,
  249. .probe = bf5xx_i2s_probe,
  250. .remove = bf5xx_i2s_remove,
  251. .suspend = bf5xx_i2s_suspend,
  252. .resume = bf5xx_i2s_resume,
  253. .playback = {
  254. .channels_min = 1,
  255. .channels_max = 2,
  256. .rates = BF5XX_I2S_RATES,
  257. .formats = BF5XX_I2S_FORMATS,},
  258. .capture = {
  259. .channels_min = 1,
  260. .channels_max = 2,
  261. .rates = BF5XX_I2S_RATES,
  262. .formats = BF5XX_I2S_FORMATS,},
  263. .ops = {
  264. .startup = bf5xx_i2s_startup,
  265. .shutdown = bf5xx_i2s_shutdown,
  266. .hw_params = bf5xx_i2s_hw_params,},
  267. .dai_ops = {
  268. .set_fmt = bf5xx_i2s_set_dai_fmt,
  269. },
  270. };
  271. EXPORT_SYMBOL_GPL(bf5xx_i2s_dai);
  272. /* Module information */
  273. MODULE_AUTHOR("Cliff Cai");
  274. MODULE_DESCRIPTION("I2S driver for ADI Blackfin");
  275. MODULE_LICENSE("GPL");