bf5xx-tdm.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 int bf5xx_tdm_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  46. unsigned int fmt)
  47. {
  48. int ret = 0;
  49. /* interface format:support TDM,slave mode */
  50. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  51. case SND_SOC_DAIFMT_DSP_A:
  52. break;
  53. default:
  54. printk(KERN_ERR "%s: Unknown DAI format type\n", __func__);
  55. ret = -EINVAL;
  56. break;
  57. }
  58. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  59. case SND_SOC_DAIFMT_CBM_CFM:
  60. break;
  61. case SND_SOC_DAIFMT_CBS_CFS:
  62. case SND_SOC_DAIFMT_CBM_CFS:
  63. case SND_SOC_DAIFMT_CBS_CFM:
  64. ret = -EINVAL;
  65. break;
  66. default:
  67. printk(KERN_ERR "%s: Unknown DAI master type\n", __func__);
  68. ret = -EINVAL;
  69. break;
  70. }
  71. return ret;
  72. }
  73. static int bf5xx_tdm_hw_params(struct snd_pcm_substream *substream,
  74. struct snd_pcm_hw_params *params,
  75. struct snd_soc_dai *dai)
  76. {
  77. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  78. struct bf5xx_tdm_port *bf5xx_tdm = sport_handle->private_data;
  79. int ret = 0;
  80. bf5xx_tdm->tcr2 &= ~0x1f;
  81. bf5xx_tdm->rcr2 &= ~0x1f;
  82. switch (params_format(params)) {
  83. case SNDRV_PCM_FORMAT_S32_LE:
  84. bf5xx_tdm->tcr2 |= 31;
  85. bf5xx_tdm->rcr2 |= 31;
  86. sport_handle->wdsize = 4;
  87. break;
  88. /* at present, we only support 32bit transfer */
  89. default:
  90. pr_err("not supported PCM format yet\n");
  91. return -EINVAL;
  92. break;
  93. }
  94. if (!bf5xx_tdm->configured) {
  95. /*
  96. * TX and RX are not independent,they are enabled at the
  97. * same time, even if only one side is running. So, we
  98. * need to configure both of them at the time when the first
  99. * stream is opened.
  100. *
  101. * CPU DAI:slave mode.
  102. */
  103. ret = sport_config_rx(sport_handle, bf5xx_tdm->rcr1,
  104. bf5xx_tdm->rcr2, 0, 0);
  105. if (ret) {
  106. pr_err("SPORT is busy!\n");
  107. return -EBUSY;
  108. }
  109. ret = sport_config_tx(sport_handle, bf5xx_tdm->tcr1,
  110. bf5xx_tdm->tcr2, 0, 0);
  111. if (ret) {
  112. pr_err("SPORT is busy!\n");
  113. return -EBUSY;
  114. }
  115. bf5xx_tdm->configured = 1;
  116. }
  117. return 0;
  118. }
  119. static void bf5xx_tdm_shutdown(struct snd_pcm_substream *substream,
  120. struct snd_soc_dai *dai)
  121. {
  122. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  123. struct bf5xx_tdm_port *bf5xx_tdm = sport_handle->private_data;
  124. /* No active stream, SPORT is allowed to be configured again. */
  125. if (!dai->active)
  126. bf5xx_tdm->configured = 0;
  127. }
  128. static int bf5xx_tdm_set_channel_map(struct snd_soc_dai *dai,
  129. unsigned int tx_num, unsigned int *tx_slot,
  130. unsigned int rx_num, unsigned int *rx_slot)
  131. {
  132. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  133. struct bf5xx_tdm_port *bf5xx_tdm = sport_handle->private_data;
  134. int i;
  135. unsigned int slot;
  136. unsigned int tx_mapped = 0, rx_mapped = 0;
  137. if ((tx_num > BFIN_TDM_DAI_MAX_SLOTS) ||
  138. (rx_num > BFIN_TDM_DAI_MAX_SLOTS))
  139. return -EINVAL;
  140. for (i = 0; i < tx_num; i++) {
  141. slot = tx_slot[i];
  142. if ((slot < BFIN_TDM_DAI_MAX_SLOTS) &&
  143. (!(tx_mapped & (1 << slot)))) {
  144. bf5xx_tdm->tx_map[i] = slot;
  145. tx_mapped |= 1 << slot;
  146. } else
  147. return -EINVAL;
  148. }
  149. for (i = 0; i < rx_num; i++) {
  150. slot = rx_slot[i];
  151. if ((slot < BFIN_TDM_DAI_MAX_SLOTS) &&
  152. (!(rx_mapped & (1 << slot)))) {
  153. bf5xx_tdm->rx_map[i] = slot;
  154. rx_mapped |= 1 << slot;
  155. } else
  156. return -EINVAL;
  157. }
  158. return 0;
  159. }
  160. #ifdef CONFIG_PM
  161. static int bf5xx_tdm_suspend(struct snd_soc_dai *dai)
  162. {
  163. struct sport_device *sport = snd_soc_dai_get_drvdata(dai);
  164. if (dai->playback_active)
  165. sport_tx_stop(sport);
  166. if (dai->capture_active)
  167. sport_rx_stop(sport);
  168. /* isolate sync/clock pins from codec while sports resume */
  169. peripheral_free_list(sport->pin_req);
  170. return 0;
  171. }
  172. static int bf5xx_tdm_resume(struct snd_soc_dai *dai)
  173. {
  174. int ret;
  175. struct sport_device *sport = snd_soc_dai_get_drvdata(dai);
  176. ret = sport_set_multichannel(sport, 8, 0xFF, 1);
  177. if (ret) {
  178. pr_err("SPORT is busy!\n");
  179. ret = -EBUSY;
  180. }
  181. ret = sport_config_rx(sport, 0, 0x1F, 0, 0);
  182. if (ret) {
  183. pr_err("SPORT is busy!\n");
  184. ret = -EBUSY;
  185. }
  186. ret = sport_config_tx(sport, 0, 0x1F, 0, 0);
  187. if (ret) {
  188. pr_err("SPORT is busy!\n");
  189. ret = -EBUSY;
  190. }
  191. peripheral_request_list(sport->pin_req, "soc-audio");
  192. return 0;
  193. }
  194. #else
  195. #define bf5xx_tdm_suspend NULL
  196. #define bf5xx_tdm_resume NULL
  197. #endif
  198. static const struct snd_soc_dai_ops bf5xx_tdm_dai_ops = {
  199. .hw_params = bf5xx_tdm_hw_params,
  200. .set_fmt = bf5xx_tdm_set_dai_fmt,
  201. .shutdown = bf5xx_tdm_shutdown,
  202. .set_channel_map = bf5xx_tdm_set_channel_map,
  203. };
  204. static struct snd_soc_dai_driver bf5xx_tdm_dai = {
  205. .suspend = bf5xx_tdm_suspend,
  206. .resume = bf5xx_tdm_resume,
  207. .playback = {
  208. .channels_min = 2,
  209. .channels_max = 8,
  210. .rates = SNDRV_PCM_RATE_48000,
  211. .formats = SNDRV_PCM_FMTBIT_S32_LE,},
  212. .capture = {
  213. .channels_min = 2,
  214. .channels_max = 8,
  215. .rates = SNDRV_PCM_RATE_48000,
  216. .formats = SNDRV_PCM_FMTBIT_S32_LE,},
  217. .ops = &bf5xx_tdm_dai_ops,
  218. };
  219. static int __devinit bfin_tdm_probe(struct platform_device *pdev)
  220. {
  221. struct sport_device *sport_handle;
  222. int ret;
  223. /* configure SPORT for TDM */
  224. sport_handle = sport_init(pdev, 4, 8 * sizeof(u32),
  225. sizeof(struct bf5xx_tdm_port));
  226. if (!sport_handle)
  227. return -ENODEV;
  228. /* SPORT works in TDM mode */
  229. ret = sport_set_multichannel(sport_handle, 8, 0xFF, 1);
  230. if (ret) {
  231. pr_err("SPORT is busy!\n");
  232. ret = -EBUSY;
  233. goto sport_config_err;
  234. }
  235. ret = sport_config_rx(sport_handle, 0, 0x1F, 0, 0);
  236. if (ret) {
  237. pr_err("SPORT is busy!\n");
  238. ret = -EBUSY;
  239. goto sport_config_err;
  240. }
  241. ret = sport_config_tx(sport_handle, 0, 0x1F, 0, 0);
  242. if (ret) {
  243. pr_err("SPORT is busy!\n");
  244. ret = -EBUSY;
  245. goto sport_config_err;
  246. }
  247. ret = snd_soc_register_dai(&pdev->dev, &bf5xx_tdm_dai);
  248. if (ret) {
  249. pr_err("Failed to register DAI: %d\n", ret);
  250. goto sport_config_err;
  251. }
  252. return 0;
  253. sport_config_err:
  254. sport_done(sport_handle);
  255. return ret;
  256. }
  257. static int __devexit bfin_tdm_remove(struct platform_device *pdev)
  258. {
  259. struct sport_device *sport_handle = platform_get_drvdata(pdev);
  260. snd_soc_unregister_dai(&pdev->dev);
  261. sport_done(sport_handle);
  262. return 0;
  263. }
  264. static struct platform_driver bfin_tdm_driver = {
  265. .probe = bfin_tdm_probe,
  266. .remove = __devexit_p(bfin_tdm_remove),
  267. .driver = {
  268. .name = "bfin-tdm",
  269. .owner = THIS_MODULE,
  270. },
  271. };
  272. module_platform_driver(bfin_tdm_driver);
  273. /* Module information */
  274. MODULE_AUTHOR("Barry Song");
  275. MODULE_DESCRIPTION("TDM driver for ADI Blackfin");
  276. MODULE_LICENSE("GPL");