bf5xx-i2s.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. struct bf5xx_i2s_port {
  43. u16 tcr1;
  44. u16 rcr1;
  45. u16 tcr2;
  46. u16 rcr2;
  47. int configured;
  48. };
  49. static int bf5xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  50. unsigned int fmt)
  51. {
  52. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(cpu_dai);
  53. struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
  54. int ret = 0;
  55. /* interface format:support I2S,slave mode */
  56. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  57. case SND_SOC_DAIFMT_I2S:
  58. bf5xx_i2s->tcr1 |= TFSR | TCKFE;
  59. bf5xx_i2s->rcr1 |= RFSR | RCKFE;
  60. bf5xx_i2s->tcr2 |= TSFSE;
  61. bf5xx_i2s->rcr2 |= RSFSE;
  62. break;
  63. case SND_SOC_DAIFMT_DSP_A:
  64. bf5xx_i2s->tcr1 |= TFSR;
  65. bf5xx_i2s->rcr1 |= RFSR;
  66. break;
  67. case SND_SOC_DAIFMT_LEFT_J:
  68. ret = -EINVAL;
  69. break;
  70. default:
  71. printk(KERN_ERR "%s: Unknown DAI format type\n", __func__);
  72. ret = -EINVAL;
  73. break;
  74. }
  75. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  76. case SND_SOC_DAIFMT_CBM_CFM:
  77. break;
  78. case SND_SOC_DAIFMT_CBS_CFS:
  79. case SND_SOC_DAIFMT_CBM_CFS:
  80. case SND_SOC_DAIFMT_CBS_CFM:
  81. ret = -EINVAL;
  82. break;
  83. default:
  84. printk(KERN_ERR "%s: Unknown DAI master type\n", __func__);
  85. ret = -EINVAL;
  86. break;
  87. }
  88. return ret;
  89. }
  90. static int bf5xx_i2s_hw_params(struct snd_pcm_substream *substream,
  91. struct snd_pcm_hw_params *params,
  92. struct snd_soc_dai *dai)
  93. {
  94. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  95. struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
  96. int ret = 0;
  97. bf5xx_i2s->tcr2 &= ~0x1f;
  98. bf5xx_i2s->rcr2 &= ~0x1f;
  99. switch (params_format(params)) {
  100. case SNDRV_PCM_FORMAT_S8:
  101. bf5xx_i2s->tcr2 |= 7;
  102. bf5xx_i2s->rcr2 |= 7;
  103. sport_handle->wdsize = 1;
  104. case SNDRV_PCM_FORMAT_S16_LE:
  105. bf5xx_i2s->tcr2 |= 15;
  106. bf5xx_i2s->rcr2 |= 15;
  107. sport_handle->wdsize = 2;
  108. break;
  109. case SNDRV_PCM_FORMAT_S24_LE:
  110. bf5xx_i2s->tcr2 |= 23;
  111. bf5xx_i2s->rcr2 |= 23;
  112. sport_handle->wdsize = 3;
  113. break;
  114. case SNDRV_PCM_FORMAT_S32_LE:
  115. bf5xx_i2s->tcr2 |= 31;
  116. bf5xx_i2s->rcr2 |= 31;
  117. sport_handle->wdsize = 4;
  118. break;
  119. }
  120. if (!bf5xx_i2s->configured) {
  121. /*
  122. * TX and RX are not independent,they are enabled at the
  123. * same time, even if only one side is running. So, we
  124. * need to configure both of them at the time when the first
  125. * stream is opened.
  126. *
  127. * CPU DAI:slave mode.
  128. */
  129. bf5xx_i2s->configured = 1;
  130. ret = sport_config_rx(sport_handle, bf5xx_i2s->rcr1,
  131. bf5xx_i2s->rcr2, 0, 0);
  132. if (ret) {
  133. pr_err("SPORT is busy!\n");
  134. return -EBUSY;
  135. }
  136. ret = sport_config_tx(sport_handle, bf5xx_i2s->tcr1,
  137. bf5xx_i2s->tcr2, 0, 0);
  138. if (ret) {
  139. pr_err("SPORT is busy!\n");
  140. return -EBUSY;
  141. }
  142. }
  143. return 0;
  144. }
  145. static void bf5xx_i2s_shutdown(struct snd_pcm_substream *substream,
  146. struct snd_soc_dai *dai)
  147. {
  148. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  149. struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
  150. pr_debug("%s enter\n", __func__);
  151. /* No active stream, SPORT is allowed to be configured again. */
  152. if (!dai->active)
  153. bf5xx_i2s->configured = 0;
  154. }
  155. #ifdef CONFIG_PM
  156. static int bf5xx_i2s_suspend(struct snd_soc_dai *dai)
  157. {
  158. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  159. pr_debug("%s : sport %d\n", __func__, dai->id);
  160. if (dai->capture_active)
  161. sport_rx_stop(sport_handle);
  162. if (dai->playback_active)
  163. sport_tx_stop(sport_handle);
  164. return 0;
  165. }
  166. static int bf5xx_i2s_resume(struct snd_soc_dai *dai)
  167. {
  168. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  169. struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
  170. int ret;
  171. pr_debug("%s : sport %d\n", __func__, dai->id);
  172. ret = sport_config_rx(sport_handle, bf5xx_i2s->rcr1,
  173. bf5xx_i2s->rcr2, 0, 0);
  174. if (ret) {
  175. pr_err("SPORT is busy!\n");
  176. return -EBUSY;
  177. }
  178. ret = sport_config_tx(sport_handle, bf5xx_i2s->tcr1,
  179. bf5xx_i2s->tcr2, 0, 0);
  180. if (ret) {
  181. pr_err("SPORT is busy!\n");
  182. return -EBUSY;
  183. }
  184. return 0;
  185. }
  186. #else
  187. #define bf5xx_i2s_suspend NULL
  188. #define bf5xx_i2s_resume NULL
  189. #endif
  190. #define BF5XX_I2S_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  191. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
  192. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
  193. SNDRV_PCM_RATE_96000)
  194. #define BF5XX_I2S_FORMATS \
  195. (SNDRV_PCM_FMTBIT_S8 | \
  196. SNDRV_PCM_FMTBIT_S16_LE | \
  197. SNDRV_PCM_FMTBIT_S24_LE | \
  198. SNDRV_PCM_FMTBIT_S32_LE)
  199. static struct snd_soc_dai_ops bf5xx_i2s_dai_ops = {
  200. .shutdown = bf5xx_i2s_shutdown,
  201. .hw_params = bf5xx_i2s_hw_params,
  202. .set_fmt = bf5xx_i2s_set_dai_fmt,
  203. };
  204. static struct snd_soc_dai_driver bf5xx_i2s_dai = {
  205. .suspend = bf5xx_i2s_suspend,
  206. .resume = bf5xx_i2s_resume,
  207. .playback = {
  208. .channels_min = 1,
  209. .channels_max = 2,
  210. .rates = BF5XX_I2S_RATES,
  211. .formats = BF5XX_I2S_FORMATS,},
  212. .capture = {
  213. .channels_min = 1,
  214. .channels_max = 2,
  215. .rates = BF5XX_I2S_RATES,
  216. .formats = BF5XX_I2S_FORMATS,},
  217. .ops = &bf5xx_i2s_dai_ops,
  218. };
  219. static int __devinit bf5xx_i2s_probe(struct platform_device *pdev)
  220. {
  221. struct sport_device *sport_handle;
  222. int ret;
  223. /* configure SPORT for I2S */
  224. sport_handle = sport_init(pdev, 4, 2 * sizeof(u32),
  225. sizeof(struct bf5xx_i2s_port));
  226. if (!sport_handle)
  227. return -ENODEV;
  228. /* register with the ASoC layers */
  229. ret = snd_soc_register_dai(&pdev->dev, &bf5xx_i2s_dai);
  230. if (ret) {
  231. pr_err("Failed to register DAI: %d\n", ret);
  232. sport_done(sport_handle);
  233. return ret;
  234. }
  235. return 0;
  236. }
  237. static int __devexit bf5xx_i2s_remove(struct platform_device *pdev)
  238. {
  239. struct sport_device *sport_handle = platform_get_drvdata(pdev);
  240. pr_debug("%s enter\n", __func__);
  241. snd_soc_unregister_dai(&pdev->dev);
  242. sport_done(sport_handle);
  243. return 0;
  244. }
  245. static struct platform_driver bfin_i2s_driver = {
  246. .probe = bf5xx_i2s_probe,
  247. .remove = __devexit_p(bf5xx_i2s_remove),
  248. .driver = {
  249. .name = "bfin-i2s",
  250. .owner = THIS_MODULE,
  251. },
  252. };
  253. static int __init bfin_i2s_init(void)
  254. {
  255. return platform_driver_register(&bfin_i2s_driver);
  256. }
  257. static void __exit bfin_i2s_exit(void)
  258. {
  259. platform_driver_unregister(&bfin_i2s_driver);
  260. }
  261. module_init(bfin_i2s_init);
  262. module_exit(bfin_i2s_exit);
  263. /* Module information */
  264. MODULE_AUTHOR("Cliff Cai");
  265. MODULE_DESCRIPTION("I2S driver for ADI Blackfin");
  266. MODULE_LICENSE("GPL");