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. case SNDRV_PCM_FORMAT_S16_LE:
  113. bf5xx_i2s->tcr2 |= 15;
  114. bf5xx_i2s->rcr2 |= 15;
  115. sport_handle->wdsize = 2;
  116. break;
  117. case SNDRV_PCM_FORMAT_S24_LE:
  118. bf5xx_i2s->tcr2 |= 23;
  119. bf5xx_i2s->rcr2 |= 23;
  120. sport_handle->wdsize = 3;
  121. break;
  122. case SNDRV_PCM_FORMAT_S32_LE:
  123. bf5xx_i2s->tcr2 |= 31;
  124. bf5xx_i2s->rcr2 |= 31;
  125. sport_handle->wdsize = 4;
  126. break;
  127. }
  128. if (!bf5xx_i2s->configured) {
  129. /*
  130. * TX and RX are not independent,they are enabled at the
  131. * same time, even if only one side is running. So, we
  132. * need to configure both of them at the time when the first
  133. * stream is opened.
  134. *
  135. * CPU DAI:slave mode.
  136. */
  137. bf5xx_i2s->configured = 1;
  138. ret = sport_config_rx(sport_handle, bf5xx_i2s->rcr1,
  139. bf5xx_i2s->rcr2, 0, 0);
  140. if (ret) {
  141. dev_err(dai->dev, "SPORT is busy!\n");
  142. return -EBUSY;
  143. }
  144. ret = sport_config_tx(sport_handle, bf5xx_i2s->tcr1,
  145. bf5xx_i2s->tcr2, 0, 0);
  146. if (ret) {
  147. dev_err(dai->dev, "SPORT is busy!\n");
  148. return -EBUSY;
  149. }
  150. }
  151. return 0;
  152. }
  153. static void bf5xx_i2s_shutdown(struct snd_pcm_substream *substream,
  154. struct snd_soc_dai *dai)
  155. {
  156. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  157. struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
  158. dev_dbg(dai->dev, "%s enter\n", __func__);
  159. /* No active stream, SPORT is allowed to be configured again. */
  160. if (!dai->active)
  161. bf5xx_i2s->configured = 0;
  162. }
  163. static int bf5xx_i2s_set_channel_map(struct snd_soc_dai *dai,
  164. unsigned int tx_num, unsigned int *tx_slot,
  165. unsigned int rx_num, unsigned int *rx_slot)
  166. {
  167. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  168. struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
  169. unsigned int tx_mapped = 0, rx_mapped = 0;
  170. unsigned int slot;
  171. int i;
  172. if ((tx_num > BFIN_TDM_DAI_MAX_SLOTS) ||
  173. (rx_num > BFIN_TDM_DAI_MAX_SLOTS))
  174. return -EINVAL;
  175. for (i = 0; i < tx_num; i++) {
  176. slot = tx_slot[i];
  177. if ((slot < BFIN_TDM_DAI_MAX_SLOTS) &&
  178. (!(tx_mapped & (1 << slot)))) {
  179. bf5xx_i2s->tx_dma_data.map[i] = slot;
  180. tx_mapped |= 1 << slot;
  181. } else
  182. return -EINVAL;
  183. }
  184. for (i = 0; i < rx_num; i++) {
  185. slot = rx_slot[i];
  186. if ((slot < BFIN_TDM_DAI_MAX_SLOTS) &&
  187. (!(rx_mapped & (1 << slot)))) {
  188. bf5xx_i2s->rx_dma_data.map[i] = slot;
  189. rx_mapped |= 1 << slot;
  190. } else
  191. return -EINVAL;
  192. }
  193. return 0;
  194. }
  195. static int bf5xx_i2s_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  196. unsigned int rx_mask, int slots, int width)
  197. {
  198. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  199. struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
  200. if (slots % 8 != 0 || slots > 8)
  201. return -EINVAL;
  202. if (width != 32)
  203. return -EINVAL;
  204. bf5xx_i2s->slots = slots;
  205. bf5xx_i2s->tx_mask = tx_mask;
  206. bf5xx_i2s->rx_mask = rx_mask;
  207. bf5xx_i2s->tx_dma_data.tdm_mode = slots != 0;
  208. bf5xx_i2s->rx_dma_data.tdm_mode = slots != 0;
  209. return sport_set_multichannel(sport_handle, slots, tx_mask, rx_mask, 0);
  210. }
  211. #ifdef CONFIG_PM
  212. static int bf5xx_i2s_suspend(struct snd_soc_dai *dai)
  213. {
  214. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  215. dev_dbg(dai->dev, "%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. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  225. struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
  226. int ret;
  227. dev_dbg(dai->dev, "%s : sport %d\n", __func__, dai->id);
  228. ret = sport_config_rx(sport_handle, bf5xx_i2s->rcr1,
  229. bf5xx_i2s->rcr2, 0, 0);
  230. if (ret) {
  231. dev_err(dai->dev, "SPORT is busy!\n");
  232. return -EBUSY;
  233. }
  234. ret = sport_config_tx(sport_handle, bf5xx_i2s->tcr1,
  235. bf5xx_i2s->tcr2, 0, 0);
  236. if (ret) {
  237. dev_err(dai->dev, "SPORT is busy!\n");
  238. return -EBUSY;
  239. }
  240. return sport_set_multichannel(sport_handle, bf5xx_i2s->slots,
  241. bf5xx_i2s->tx_mask, bf5xx_i2s->rx_mask, 0);
  242. }
  243. #else
  244. #define bf5xx_i2s_suspend NULL
  245. #define bf5xx_i2s_resume NULL
  246. #endif
  247. static int bf5xx_i2s_dai_probe(struct snd_soc_dai *dai)
  248. {
  249. struct sport_device *sport_handle = snd_soc_dai_get_drvdata(dai);
  250. struct bf5xx_i2s_port *bf5xx_i2s = sport_handle->private_data;
  251. unsigned int i;
  252. for (i = 0; i < BFIN_TDM_DAI_MAX_SLOTS; i++) {
  253. bf5xx_i2s->tx_dma_data.map[i] = i;
  254. bf5xx_i2s->rx_dma_data.map[i] = i;
  255. }
  256. dai->playback_dma_data = &bf5xx_i2s->tx_dma_data;
  257. dai->capture_dma_data = &bf5xx_i2s->rx_dma_data;
  258. return 0;
  259. }
  260. #define BF5XX_I2S_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  261. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
  262. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
  263. SNDRV_PCM_RATE_96000)
  264. #define BF5XX_I2S_FORMATS \
  265. (SNDRV_PCM_FMTBIT_S8 | \
  266. SNDRV_PCM_FMTBIT_S16_LE | \
  267. SNDRV_PCM_FMTBIT_S24_LE | \
  268. SNDRV_PCM_FMTBIT_S32_LE)
  269. static const struct snd_soc_dai_ops bf5xx_i2s_dai_ops = {
  270. .shutdown = bf5xx_i2s_shutdown,
  271. .hw_params = bf5xx_i2s_hw_params,
  272. .set_fmt = bf5xx_i2s_set_dai_fmt,
  273. .set_tdm_slot = bf5xx_i2s_set_tdm_slot,
  274. .set_channel_map = bf5xx_i2s_set_channel_map,
  275. };
  276. static struct snd_soc_dai_driver bf5xx_i2s_dai = {
  277. .probe = bf5xx_i2s_dai_probe,
  278. .suspend = bf5xx_i2s_suspend,
  279. .resume = bf5xx_i2s_resume,
  280. .playback = {
  281. .channels_min = 2,
  282. .channels_max = 8,
  283. .rates = BF5XX_I2S_RATES,
  284. .formats = BF5XX_I2S_FORMATS,},
  285. .capture = {
  286. .channels_min = 2,
  287. .channels_max = 8,
  288. .rates = BF5XX_I2S_RATES,
  289. .formats = BF5XX_I2S_FORMATS,},
  290. .ops = &bf5xx_i2s_dai_ops,
  291. };
  292. static const struct snd_soc_component_driver bf5xx_i2s_component = {
  293. .name = "bf5xx-i2s",
  294. };
  295. static int bf5xx_i2s_probe(struct platform_device *pdev)
  296. {
  297. struct sport_device *sport_handle;
  298. int ret;
  299. /* configure SPORT for I2S */
  300. sport_handle = sport_init(pdev, 4, 8 * sizeof(u32),
  301. sizeof(struct bf5xx_i2s_port));
  302. if (!sport_handle)
  303. return -ENODEV;
  304. /* register with the ASoC layers */
  305. ret = snd_soc_register_component(&pdev->dev, &bf5xx_i2s_component,
  306. &bf5xx_i2s_dai, 1);
  307. if (ret) {
  308. dev_err(&pdev->dev, "Failed to register DAI: %d\n", ret);
  309. sport_done(sport_handle);
  310. return ret;
  311. }
  312. return 0;
  313. }
  314. static int bf5xx_i2s_remove(struct platform_device *pdev)
  315. {
  316. struct sport_device *sport_handle = platform_get_drvdata(pdev);
  317. dev_dbg(&pdev->dev, "%s enter\n", __func__);
  318. snd_soc_unregister_component(&pdev->dev);
  319. sport_done(sport_handle);
  320. return 0;
  321. }
  322. static struct platform_driver bfin_i2s_driver = {
  323. .probe = bf5xx_i2s_probe,
  324. .remove = bf5xx_i2s_remove,
  325. .driver = {
  326. .name = "bfin-i2s",
  327. .owner = THIS_MODULE,
  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");