bf5xx-tdm.c 8.1 KB

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