/kern_2.6.32/sound/soc/blackfin/bf5xx-i2s.c

http://omnia2droid.googlecode.com/ · C · 330 lines · 241 code · 41 blank · 48 comment · 13 complexity · 37b15c8547650b12182798ec73507124 MD5 · raw file

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