bf5xx-tdm.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 =
  191. (struct sport_device *)dai->private_data;
  192. if (!dai->active)
  193. return 0;
  194. if (dai->capture.active)
  195. sport_rx_stop(sport);
  196. if (dai->playback.active)
  197. sport_tx_stop(sport);
  198. return 0;
  199. }
  200. static int bf5xx_tdm_resume(struct snd_soc_dai *dai)
  201. {
  202. int ret;
  203. struct sport_device *sport =
  204. (struct sport_device *)dai->private_data;
  205. if (!dai->active)
  206. return 0;
  207. ret = sport_set_multichannel(sport, 8, 0xFF, 1);
  208. if (ret) {
  209. pr_err("SPORT is busy!\n");
  210. ret = -EBUSY;
  211. }
  212. ret = sport_config_rx(sport, IRFS, 0x1F, 0, 0);
  213. if (ret) {
  214. pr_err("SPORT is busy!\n");
  215. ret = -EBUSY;
  216. }
  217. ret = sport_config_tx(sport, ITFS, 0x1F, 0, 0);
  218. if (ret) {
  219. pr_err("SPORT is busy!\n");
  220. ret = -EBUSY;
  221. }
  222. return 0;
  223. }
  224. #else
  225. #define bf5xx_tdm_suspend NULL
  226. #define bf5xx_tdm_resume NULL
  227. #endif
  228. static struct snd_soc_dai_ops bf5xx_tdm_dai_ops = {
  229. .hw_params = bf5xx_tdm_hw_params,
  230. .set_fmt = bf5xx_tdm_set_dai_fmt,
  231. .shutdown = bf5xx_tdm_shutdown,
  232. .set_channel_map = bf5xx_tdm_set_channel_map,
  233. };
  234. struct snd_soc_dai bf5xx_tdm_dai = {
  235. .name = "bf5xx-tdm",
  236. .id = 0,
  237. .suspend = bf5xx_tdm_suspend,
  238. .resume = bf5xx_tdm_resume,
  239. .playback = {
  240. .channels_min = 2,
  241. .channels_max = 8,
  242. .rates = SNDRV_PCM_RATE_48000,
  243. .formats = SNDRV_PCM_FMTBIT_S32_LE,},
  244. .capture = {
  245. .channels_min = 2,
  246. .channels_max = 8,
  247. .rates = SNDRV_PCM_RATE_48000,
  248. .formats = SNDRV_PCM_FMTBIT_S32_LE,},
  249. .ops = &bf5xx_tdm_dai_ops,
  250. };
  251. EXPORT_SYMBOL_GPL(bf5xx_tdm_dai);
  252. static int __devinit bfin_tdm_probe(struct platform_device *pdev)
  253. {
  254. int ret = 0;
  255. if (peripheral_request_list(&sport_req[sport_num][0], "soc-audio")) {
  256. pr_err("Requesting Peripherals failed\n");
  257. return -EFAULT;
  258. }
  259. /* request DMA for SPORT */
  260. sport_handle = sport_init(&sport_params[sport_num], 4, \
  261. 8 * sizeof(u32), NULL);
  262. if (!sport_handle) {
  263. peripheral_free_list(&sport_req[sport_num][0]);
  264. return -ENODEV;
  265. }
  266. /* SPORT works in TDM mode */
  267. ret = sport_set_multichannel(sport_handle, 8, 0xFF, 1);
  268. if (ret) {
  269. pr_err("SPORT is busy!\n");
  270. ret = -EBUSY;
  271. goto sport_config_err;
  272. }
  273. ret = sport_config_rx(sport_handle, IRFS, 0x1F, 0, 0);
  274. if (ret) {
  275. pr_err("SPORT is busy!\n");
  276. ret = -EBUSY;
  277. goto sport_config_err;
  278. }
  279. ret = sport_config_tx(sport_handle, ITFS, 0x1F, 0, 0);
  280. if (ret) {
  281. pr_err("SPORT is busy!\n");
  282. ret = -EBUSY;
  283. goto sport_config_err;
  284. }
  285. ret = snd_soc_register_dai(&bf5xx_tdm_dai);
  286. if (ret) {
  287. pr_err("Failed to register DAI: %d\n", ret);
  288. goto sport_config_err;
  289. }
  290. sport_handle->private_data = &bf5xx_tdm;
  291. return 0;
  292. sport_config_err:
  293. peripheral_free_list(&sport_req[sport_num][0]);
  294. return ret;
  295. }
  296. static int __devexit bfin_tdm_remove(struct platform_device *pdev)
  297. {
  298. peripheral_free_list(&sport_req[sport_num][0]);
  299. snd_soc_unregister_dai(&bf5xx_tdm_dai);
  300. return 0;
  301. }
  302. static struct platform_driver bfin_tdm_driver = {
  303. .probe = bfin_tdm_probe,
  304. .remove = __devexit_p(bfin_tdm_remove),
  305. .driver = {
  306. .name = "bfin-tdm",
  307. .owner = THIS_MODULE,
  308. },
  309. };
  310. static int __init bfin_tdm_init(void)
  311. {
  312. return platform_driver_register(&bfin_tdm_driver);
  313. }
  314. module_init(bfin_tdm_init);
  315. static void __exit bfin_tdm_exit(void)
  316. {
  317. platform_driver_unregister(&bfin_tdm_driver);
  318. }
  319. module_exit(bfin_tdm_exit);
  320. /* Module information */
  321. MODULE_AUTHOR("Barry Song");
  322. MODULE_DESCRIPTION("TDM driver for ADI Blackfin");
  323. MODULE_LICENSE("GPL");