bf5xx-tdm.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * File: sound/soc/blackfin/bf5xx-tdm.c
  3. * Author: Barry Song <Barry.Song@analog.com>
  4. *
  5. * Created: Thurs June 04 2009
  6. * Description: Blackfin I2S(TDM) CPU DAI driver
  7. * Even though TDM mode can be as part of I2S DAI, but there
  8. * are so much difference in configuration and data flow,
  9. * it's very ugly to integrate I2S and TDM into a module
  10. *
  11. * Modified:
  12. * Copyright 2009 Analog Devices Inc.
  13. *
  14. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, see the file COPYING, or write
  28. * to the Free Software Foundation, Inc.,
  29. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  30. */
  31. #include <linux/init.h>
  32. #include <linux/module.h>
  33. #include <linux/device.h>
  34. #include <sound/core.h>
  35. #include <sound/pcm.h>
  36. #include <sound/pcm_params.h>
  37. #include <sound/initval.h>
  38. #include <sound/soc.h>
  39. #include <asm/irq.h>
  40. #include <asm/portmux.h>
  41. #include <linux/mutex.h>
  42. #include <linux/gpio.h>
  43. #include "bf5xx-sport.h"
  44. #include "bf5xx-tdm.h"
  45. static struct bf5xx_tdm_port bf5xx_tdm;
  46. static int sport_num = CONFIG_SND_BF5XX_SPORT_NUM;
  47. static struct sport_param sport_params[2] = {
  48. {
  49. .dma_rx_chan = CH_SPORT0_RX,
  50. .dma_tx_chan = CH_SPORT0_TX,
  51. .err_irq = IRQ_SPORT0_ERROR,
  52. .regs = (struct sport_register *)SPORT0_TCR1,
  53. },
  54. {
  55. .dma_rx_chan = CH_SPORT1_RX,
  56. .dma_tx_chan = CH_SPORT1_TX,
  57. .err_irq = IRQ_SPORT1_ERROR,
  58. .regs = (struct sport_register *)SPORT1_TCR1,
  59. }
  60. };
  61. /*
  62. * Setting the TFS pin selector for SPORT 0 based on whether the selected
  63. * port id F or G. If the port is F then no conflict should exist for the
  64. * TFS. When Port G is selected and EMAC then there is a conflict between
  65. * the PHY interrupt line and TFS. Current settings prevent the conflict
  66. * by ignoring the TFS pin when Port G is selected. This allows both
  67. * codecs and EMAC using Port G concurrently.
  68. */
  69. #ifdef CONFIG_BF527_SPORT0_PORTG
  70. #define LOCAL_SPORT0_TFS (0)
  71. #else
  72. #define LOCAL_SPORT0_TFS (P_SPORT0_TFS)
  73. #endif
  74. static u16 sport_req[][7] = { {P_SPORT0_DTPRI, P_SPORT0_TSCLK, P_SPORT0_RFS,
  75. P_SPORT0_DRPRI, P_SPORT0_RSCLK, LOCAL_SPORT0_TFS, 0},
  76. {P_SPORT1_DTPRI, P_SPORT1_TSCLK, P_SPORT1_RFS, P_SPORT1_DRPRI,
  77. P_SPORT1_RSCLK, P_SPORT1_TFS, 0} };
  78. static int bf5xx_tdm_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  79. unsigned int fmt)
  80. {
  81. int ret = 0;
  82. /* interface format:support TDM,slave mode */
  83. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  84. case SND_SOC_DAIFMT_DSP_A:
  85. break;
  86. default:
  87. printk(KERN_ERR "%s: Unknown DAI format type\n", __func__);
  88. ret = -EINVAL;
  89. break;
  90. }
  91. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  92. case SND_SOC_DAIFMT_CBM_CFM:
  93. break;
  94. case SND_SOC_DAIFMT_CBS_CFS:
  95. case SND_SOC_DAIFMT_CBM_CFS:
  96. case SND_SOC_DAIFMT_CBS_CFM:
  97. ret = -EINVAL;
  98. break;
  99. default:
  100. printk(KERN_ERR "%s: Unknown DAI master type\n", __func__);
  101. ret = -EINVAL;
  102. break;
  103. }
  104. return ret;
  105. }
  106. static int bf5xx_tdm_hw_params(struct snd_pcm_substream *substream,
  107. struct snd_pcm_hw_params *params,
  108. struct snd_soc_dai *dai)
  109. {
  110. int ret = 0;
  111. bf5xx_tdm.tcr2 &= ~0x1f;
  112. bf5xx_tdm.rcr2 &= ~0x1f;
  113. switch (params_format(params)) {
  114. case SNDRV_PCM_FORMAT_S32_LE:
  115. bf5xx_tdm.tcr2 |= 31;
  116. bf5xx_tdm.rcr2 |= 31;
  117. sport_handle->wdsize = 4;
  118. break;
  119. /* at present, we only support 32bit transfer */
  120. default:
  121. pr_err("not supported PCM format yet\n");
  122. return -EINVAL;
  123. break;
  124. }
  125. if (!bf5xx_tdm.configured) {
  126. /*
  127. * TX and RX are not independent,they are enabled at the
  128. * same time, even if only one side is running. So, we
  129. * need to configure both of them at the time when the first
  130. * stream is opened.
  131. *
  132. * CPU DAI:slave mode.
  133. */
  134. ret = sport_config_rx(sport_handle, bf5xx_tdm.rcr1,
  135. bf5xx_tdm.rcr2, 0, 0);
  136. if (ret) {
  137. pr_err("SPORT is busy!\n");
  138. return -EBUSY;
  139. }
  140. ret = sport_config_tx(sport_handle, bf5xx_tdm.tcr1,
  141. bf5xx_tdm.tcr2, 0, 0);
  142. if (ret) {
  143. pr_err("SPORT is busy!\n");
  144. return -EBUSY;
  145. }
  146. bf5xx_tdm.configured = 1;
  147. }
  148. return 0;
  149. }
  150. static void bf5xx_tdm_shutdown(struct snd_pcm_substream *substream,
  151. struct snd_soc_dai *dai)
  152. {
  153. /* No active stream, SPORT is allowed to be configured again. */
  154. if (!dai->active)
  155. bf5xx_tdm.configured = 0;
  156. }
  157. static int bf5xx_tdm_set_channel_map(struct snd_soc_dai *dai,
  158. unsigned int tx_num, unsigned int *tx_slot,
  159. unsigned int rx_num, unsigned int *rx_slot)
  160. {
  161. int i;
  162. unsigned int slot;
  163. unsigned int tx_mapped = 0, rx_mapped = 0;
  164. if ((tx_num > BFIN_TDM_DAI_MAX_SLOTS) ||
  165. (rx_num > BFIN_TDM_DAI_MAX_SLOTS))
  166. return -EINVAL;
  167. for (i = 0; i < tx_num; i++) {
  168. slot = tx_slot[i];
  169. if ((slot < BFIN_TDM_DAI_MAX_SLOTS) &&
  170. (!(tx_mapped & (1 << slot)))) {
  171. bf5xx_tdm.tx_map[i] = slot;
  172. tx_mapped |= 1 << slot;
  173. } else
  174. return -EINVAL;
  175. }
  176. for (i = 0; i < rx_num; i++) {
  177. slot = rx_slot[i];
  178. if ((slot < BFIN_TDM_DAI_MAX_SLOTS) &&
  179. (!(rx_mapped & (1 << slot)))) {
  180. bf5xx_tdm.rx_map[i] = slot;
  181. rx_mapped |= 1 << slot;
  182. } else
  183. return -EINVAL;
  184. }
  185. return 0;
  186. }
  187. #ifdef CONFIG_PM
  188. static int bf5xx_tdm_suspend(struct snd_soc_dai *dai)
  189. {
  190. struct sport_device *sport = snd_soc_dai_get_drvdata(dai);
  191. if (!dai->active)
  192. return 0;
  193. if (dai->capture_active)
  194. sport_rx_stop(sport);
  195. if (dai->playback_active)
  196. sport_tx_stop(sport);
  197. return 0;
  198. }
  199. static int bf5xx_tdm_resume(struct snd_soc_dai *dai)
  200. {
  201. int ret;
  202. struct sport_device *sport = snd_soc_dai_get_drvdata(dai);
  203. if (!dai->active)
  204. return 0;
  205. ret = sport_set_multichannel(sport, 8, 0xFF, 1);
  206. if (ret) {
  207. pr_err("SPORT is busy!\n");
  208. ret = -EBUSY;
  209. }
  210. ret = sport_config_rx(sport, 0, 0x1F, 0, 0);
  211. if (ret) {
  212. pr_err("SPORT is busy!\n");
  213. ret = -EBUSY;
  214. }
  215. ret = sport_config_tx(sport, 0, 0x1F, 0, 0);
  216. if (ret) {
  217. pr_err("SPORT is busy!\n");
  218. ret = -EBUSY;
  219. }
  220. return 0;
  221. }
  222. #else
  223. #define bf5xx_tdm_suspend NULL
  224. #define bf5xx_tdm_resume NULL
  225. #endif
  226. static struct snd_soc_dai_ops bf5xx_tdm_dai_ops = {
  227. .hw_params = bf5xx_tdm_hw_params,
  228. .set_fmt = bf5xx_tdm_set_dai_fmt,
  229. .shutdown = bf5xx_tdm_shutdown,
  230. .set_channel_map = bf5xx_tdm_set_channel_map,
  231. };
  232. static struct snd_soc_dai_driver bf5xx_tdm_dai = {
  233. .suspend = bf5xx_tdm_suspend,
  234. .resume = bf5xx_tdm_resume,
  235. .playback = {
  236. .channels_min = 2,
  237. .channels_max = 8,
  238. .rates = SNDRV_PCM_RATE_48000,
  239. .formats = SNDRV_PCM_FMTBIT_S32_LE,},
  240. .capture = {
  241. .channels_min = 2,
  242. .channels_max = 8,
  243. .rates = SNDRV_PCM_RATE_48000,
  244. .formats = SNDRV_PCM_FMTBIT_S32_LE,},
  245. .ops = &bf5xx_tdm_dai_ops,
  246. };
  247. static int __devinit bfin_tdm_probe(struct platform_device *pdev)
  248. {
  249. int ret = 0;
  250. if (peripheral_request_list(&sport_req[sport_num][0], "soc-audio")) {
  251. pr_err("Requesting Peripherals failed\n");
  252. return -EFAULT;
  253. }
  254. /* request DMA for SPORT */
  255. sport_handle = sport_init(&sport_params[sport_num], 4, \
  256. 8 * sizeof(u32), NULL);
  257. if (!sport_handle) {
  258. peripheral_free_list(&sport_req[sport_num][0]);
  259. return -ENODEV;
  260. }
  261. /* SPORT works in TDM mode */
  262. ret = sport_set_multichannel(sport_handle, 8, 0xFF, 1);
  263. if (ret) {
  264. pr_err("SPORT is busy!\n");
  265. ret = -EBUSY;
  266. goto sport_config_err;
  267. }
  268. ret = sport_config_rx(sport_handle, 0, 0x1F, 0, 0);
  269. if (ret) {
  270. pr_err("SPORT is busy!\n");
  271. ret = -EBUSY;
  272. goto sport_config_err;
  273. }
  274. ret = sport_config_tx(sport_handle, 0, 0x1F, 0, 0);
  275. if (ret) {
  276. pr_err("SPORT is busy!\n");
  277. ret = -EBUSY;
  278. goto sport_config_err;
  279. }
  280. ret = snd_soc_register_dai(&pdev->dev, &bf5xx_tdm_dai);
  281. if (ret) {
  282. pr_err("Failed to register DAI: %d\n", ret);
  283. goto sport_config_err;
  284. }
  285. sport_handle->private_data = &bf5xx_tdm;
  286. return 0;
  287. sport_config_err:
  288. peripheral_free_list(&sport_req[sport_num][0]);
  289. return ret;
  290. }
  291. static int __devexit bfin_tdm_remove(struct platform_device *pdev)
  292. {
  293. peripheral_free_list(&sport_req[sport_num][0]);
  294. snd_soc_unregister_dai(&pdev->dev);
  295. return 0;
  296. }
  297. static struct platform_driver bfin_tdm_driver = {
  298. .probe = bfin_tdm_probe,
  299. .remove = __devexit_p(bfin_tdm_remove),
  300. .driver = {
  301. .name = "bfin-tdm",
  302. .owner = THIS_MODULE,
  303. },
  304. };
  305. static int __init bfin_tdm_init(void)
  306. {
  307. return platform_driver_register(&bfin_tdm_driver);
  308. }
  309. module_init(bfin_tdm_init);
  310. static void __exit bfin_tdm_exit(void)
  311. {
  312. platform_driver_unregister(&bfin_tdm_driver);
  313. }
  314. module_exit(bfin_tdm_exit);
  315. /* Module information */
  316. MODULE_AUTHOR("Barry Song");
  317. MODULE_DESCRIPTION("TDM driver for ADI Blackfin");
  318. MODULE_LICENSE("GPL");