bf5xx-i2s.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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-pcm.h"
  43. struct bf5xx_i2s_port {
  44. u16 tcr1;
  45. u16 rcr1;
  46. u16 tcr2;
  47. u16 rcr2;
  48. int configured;
  49. unsigned int slots;
  50. unsigned int tx_mask;
  51. unsigned int rx_mask;
  52. struct bf5xx_i2s_pcm_data tx_dma_data;
  53. struct bf5xx_i2s_pcm_data rx_dma_data;
  54. };
  55. static int bf5xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
  56. unsigned int fmt)
  57. {
  58. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(cpu_dai);
  59. struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
  60. int ret = 0;
  61. /* interface format:support I2S,slave mode */
  62. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  63. case SND_SOC_DAIFMT_I2S:
  64. bf5xx_i2s->tcr1 |= TFSR | TCKFE;
  65. bf5xx_i2s->rcr1 |= RFSR | RCKFE;
  66. bf5xx_i2s->tcr2 |= TSFSE;
  67. bf5xx_i2s->rcr2 |= RSFSE;
  68. break;
  69. case SND_SOC_DAIFMT_DSP_A:
  70. bf5xx_i2s->tcr1 |= TFSR;
  71. bf5xx_i2s->rcr1 |= RFSR;
  72. break;
  73. case SND_SOC_DAIFMT_LEFT_J:
  74. ret = -EINVAL;
  75. break;
  76. default:
  77. dev_err(cpu_dai->dev, "%s: Unknown DAI format type\n",
  78. __func__);
  79. ret = -EINVAL;
  80. break;
  81. }
  82. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  83. case SND_SOC_DAIFMT_CBM_CFM:
  84. break;
  85. case SND_SOC_DAIFMT_CBS_CFS:
  86. case SND_SOC_DAIFMT_CBM_CFS:
  87. case SND_SOC_DAIFMT_CBS_CFM:
  88. ret = -EINVAL;
  89. break;
  90. default:
  91. dev_err(cpu_dai->dev, "%s: Unknown DAI master type\n",
  92. __func__);
  93. ret = -EINVAL;
  94. break;
  95. }
  96. return ret;
  97. }
  98. static int bf5xx_i2s_hw_params(struct snd_pcm_substream *substream,
  99. struct snd_pcm_hw_params *params,
  100. struct snd_soc_dai *dai)
  101. {
  102. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  103. struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
  104. int ret = 0;
  105. bf5xx_i2s->tcr2 &= ~0x1f;
  106. bf5xx_i2s->rcr2 &= ~0x1f;
  107. switch (params_format(params)) {
  108. case SNDRV_PCM_FORMAT_S8:
  109. bf5xx_i2s->tcr2 |= 7;
  110. bf5xx_i2s->rcr2 |= 7;
  111. sport_handle->wdsize = 1;
  112. break;
  113. case SNDRV_PCM_FORMAT_S16_LE:
  114. bf5xx_i2s->tcr2 |= 15;
  115. bf5xx_i2s->rcr2 |= 15;
  116. sport_handle->wdsize = 2;
  117. break;
  118. case SNDRV_PCM_FORMAT_S24_LE:
  119. bf5xx_i2s->tcr2 |= 23;
  120. bf5xx_i2s->rcr2 |= 23;
  121. sport_handle->wdsize = 3;
  122. break;
  123. case SNDRV_PCM_FORMAT_S32_LE:
  124. bf5xx_i2s->tcr2 |= 31;
  125. bf5xx_i2s->rcr2 |= 31;
  126. sport_handle->wdsize = 4;
  127. break;
  128. }
  129. if (!bf5xx_i2s->configured) {
  130. /*
  131. * TX and RX are not independent,they are enabled at the
  132. * same time, even if only one side is running. So, we
  133. * need to configure both of them at the time when the first
  134. * stream is opened.
  135. *
  136. * CPU DAI:slave mode.
  137. */
  138. bf5xx_i2s->configured = 1;
  139. ret = sport_config_rx(sport_handle, bf5xx_i2s->rcr1,
  140. bf5xx_i2s->rcr2, 0, 0);
  141. if (ret) {
  142. dev_err(dai->dev, "SPORT is busy!\n");
  143. return -EBUSY;
  144. }
  145. ret = sport_config_tx(sport_handle, bf5xx_i2s->tcr1,
  146. bf5xx_i2s->tcr2, 0, 0);
  147. if (ret) {
  148. dev_err(dai->dev, "SPORT is busy!\n");
  149. return -EBUSY;
  150. }
  151. }
  152. return 0;
  153. }
  154. static void bf5xx_i2s_shutdown(struct snd_pcm_substream *substream,
  155. struct snd_soc_dai *dai)
  156. {
  157. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  158. struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
  159. dev_dbg(dai->dev, "%s enter\n", __func__);
  160. /* No active stream, SPORT is allowed to be configured again. */
  161. if (!dai->active)
  162. bf5xx_i2s->configured = 0;
  163. }
  164. static int bf5xx_i2s_set_channel_map(struct snd_soc_dai *dai,
  165. unsigned int tx_num, unsigned int *tx_slot,
  166. unsigned int rx_num, unsigned int *rx_slot)
  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. unsigned int tx_mapped = 0, rx_mapped = 0;
  171. unsigned int slot;
  172. int i;
  173. if ((tx_num > BFIN_TDM_DAI_MAX_SLOTS) ||
  174. (rx_num > BFIN_TDM_DAI_MAX_SLOTS))
  175. return -EINVAL;
  176. for (i = 0; i < tx_num; i++) {
  177. slot = tx_slot[i];
  178. if ((slot < BFIN_TDM_DAI_MAX_SLOTS) &&
  179. (!(tx_mapped & (1 << slot)))) {
  180. bf5xx_i2s->tx_dma_data.map[i] = slot;
  181. tx_mapped |= 1 << slot;
  182. } else
  183. return -EINVAL;
  184. }
  185. for (i = 0; i < rx_num; i++) {
  186. slot = rx_slot[i];
  187. if ((slot < BFIN_TDM_DAI_MAX_SLOTS) &&
  188. (!(rx_mapped & (1 << slot)))) {
  189. bf5xx_i2s->rx_dma_data.map[i] = slot;
  190. rx_mapped |= 1 << slot;
  191. } else
  192. return -EINVAL;
  193. }
  194. return 0;
  195. }
  196. static int bf5xx_i2s_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  197. unsigned int rx_mask, int slots, int width)
  198. {
  199. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  200. struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
  201. if (slots % 8 != 0 || slots > 8)
  202. return -EINVAL;
  203. if (width != 32)
  204. return -EINVAL;
  205. bf5xx_i2s->slots = slots;
  206. bf5xx_i2s->tx_mask = tx_mask;
  207. bf5xx_i2s->rx_mask = rx_mask;
  208. bf5xx_i2s->tx_dma_data.tdm_mode = slots != 0;
  209. bf5xx_i2s->rx_dma_data.tdm_mode = slots != 0;
  210. return sport_set_multichannel(sport_handle, slots, tx_mask, rx_mask, 0);
  211. }
  212. #ifdef CONFIG_PM
  213. static int bf5xx_i2s_suspend(struct snd_soc_dai *dai)
  214. {
  215. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  216. dev_dbg(dai->dev, "%s : sport %d\n", __func__, dai->id);
  217. if (dai->capture_active)
  218. sport_rx_stop(sport_handle);
  219. if (dai->playback_active)
  220. sport_tx_stop(sport_handle);
  221. return 0;
  222. }
  223. static int bf5xx_i2s_resume(struct snd_soc_dai *dai)
  224. {
  225. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  226. struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
  227. int ret;
  228. dev_dbg(dai->dev, "%s : sport %d\n", __func__, dai->id);
  229. ret = sport_config_rx(sport_handle, bf5xx_i2s->rcr1,
  230. bf5xx_i2s->rcr2, 0, 0);
  231. if (ret) {
  232. dev_err(dai->dev, "SPORT is busy!\n");
  233. return -EBUSY;
  234. }
  235. ret = sport_config_tx(sport_handle, bf5xx_i2s->tcr1,
  236. bf5xx_i2s->tcr2, 0, 0);
  237. if (ret) {
  238. dev_err(dai->dev, "SPORT is busy!\n");
  239. return -EBUSY;
  240. }
  241. return sport_set_multichannel(sport_handle, bf5xx_i2s->slots,
  242. bf5xx_i2s->tx_mask, bf5xx_i2s->rx_mask, 0);
  243. }
  244. #else
  245. #define bf5xx_i2s_suspend NULL
  246. #define bf5xx_i2s_resume NULL
  247. #endif
  248. static int bf5xx_i2s_dai_probe(struct snd_soc_dai *dai)
  249. {
  250. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  251. struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
  252. unsigned int i;
  253. for (i = 0; i < BFIN_TDM_DAI_MAX_SLOTS; i++) {
  254. bf5xx_i2s->tx_dma_data.map[i] = i;
  255. bf5xx_i2s->rx_dma_data.map[i] = i;
  256. }
  257. dai->playback_dma_data = &bf5xx_i2s->tx_dma_data;
  258. dai->capture_dma_data = &bf5xx_i2s->rx_dma_data;
  259. return 0;
  260. }
  261. #define BF5XX_I2S_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  262. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
  263. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
  264. SNDRV_PCM_RATE_96000)
  265. #define BF5XX_I2S_FORMATS \
  266. (SNDRV_PCM_FMTBIT_S8 | \
  267. SNDRV_PCM_FMTBIT_S16_LE | \
  268. SNDRV_PCM_FMTBIT_S24_LE | \
  269. SNDRV_PCM_FMTBIT_S32_LE)
  270. static const struct snd_soc_dai_ops bf5xx_i2s_dai_ops = {
  271. .shutdown = bf5xx_i2s_shutdown,
  272. .hw_params = bf5xx_i2s_hw_params,
  273. .set_fmt = bf5xx_i2s_set_dai_fmt,
  274. .set_tdm_slot = bf5xx_i2s_set_tdm_slot,
  275. .set_channel_map = bf5xx_i2s_set_channel_map,
  276. };
  277. static struct snd_soc_dai_driver bf5xx_i2s_dai = {
  278. .probe = bf5xx_i2s_dai_probe,
  279. .suspend = bf5xx_i2s_suspend,
  280. .resume = bf5xx_i2s_resume,
  281. .playback = {
  282. .channels_min = 2,
  283. .channels_max = 8,
  284. .rates = BF5XX_I2S_RATES,
  285. .formats = BF5XX_I2S_FORMATS,},
  286. .capture = {
  287. .channels_min = 2,
  288. .channels_max = 8,
  289. .rates = BF5XX_I2S_RATES,
  290. .formats = BF5XX_I2S_FORMATS,},
  291. .ops = &bf5xx_i2s_dai_ops,
  292. };
  293. static const struct snd_soc_component_driver bf5xx_i2s_component = {
  294. .name = "bf5xx-i2s",
  295. };
  296. static int bf5xx_i2s_probe(struct platform_device *pdev)
  297. {
  298. struct sport_device *sport_handle;
  299. int ret;
  300. /* configure SPORT for I2S */
  301. sport_handle = sport_init(pdev, 4, 8 * sizeof(u32),
  302. sizeof(struct bf5xx_i2s_port));
  303. if (!sport_handle)
  304. return -ENODEV;
  305. /* register with the ASoC layers */
  306. ret = snd_soc_register_component(&pdev->dev, &bf5xx_i2s_component,
  307. &bf5xx_i2s_dai, 1);
  308. if (ret) {
  309. dev_err(&pdev->dev, "Failed to register DAI: %d\n", ret);
  310. sport_done(sport_handle);
  311. return ret;
  312. }
  313. return 0;
  314. }
  315. static int bf5xx_i2s_remove(struct platform_device *pdev)
  316. {
  317. struct sport_device *sport_handle = platform_get_drvdata(pdev);
  318. dev_dbg(&pdev->dev, "%s enter\n", __func__);
  319. snd_soc_unregister_component(&pdev->dev);
  320. sport_done(sport_handle);
  321. return 0;
  322. }
  323. static struct platform_driver bfin_i2s_driver = {
  324. .probe = bf5xx_i2s_probe,
  325. .remove = bf5xx_i2s_remove,
  326. .driver = {
  327. .name = "bfin-i2s",
  328. },
  329. };
  330. module_platform_driver(bfin_i2s_driver);
  331. /* Module information */
  332. MODULE_AUTHOR("Cliff Cai");
  333. MODULE_DESCRIPTION("I2S driver for ADI Blackfin");
  334. MODULE_LICENSE("GPL");