bf5xx-ac97-pcm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * File: sound/soc/blackfin/bf5xx-ac97-pcm.c
  3. * Author: Cliff Cai <Cliff.Cai@analog.com>
  4. *
  5. * Created: Tue June 06 2008
  6. * Description: DMA Driver for AC97 sound chip
  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/module.h>
  29. #include <linux/init.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/slab.h>
  32. #include <linux/dma-mapping.h>
  33. #include <sound/core.h>
  34. #include <sound/pcm.h>
  35. #include <sound/pcm_params.h>
  36. #include <sound/soc.h>
  37. #include <asm/dma.h>
  38. #include "bf5xx-ac97-pcm.h"
  39. #include "bf5xx-ac97.h"
  40. #include "bf5xx-sport.h"
  41. static unsigned int ac97_chan_mask[] = {
  42. SP_FL, /* Mono */
  43. SP_STEREO, /* Stereo */
  44. SP_2DOT1, /* 2.1*/
  45. SP_QUAD,/*Quadraquic*/
  46. SP_FL | SP_FR | SP_FC | SP_SL | SP_SR,/*5 channels */
  47. SP_5DOT1, /* 5.1 */
  48. };
  49. #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
  50. static void bf5xx_mmap_copy(struct snd_pcm_substream *substream,
  51. snd_pcm_uframes_t count)
  52. {
  53. struct snd_pcm_runtime *runtime = substream->runtime;
  54. struct sport_device *sport = runtime->private_data;
  55. unsigned int chan_mask = ac97_chan_mask[runtime->channels - 1];
  56. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  57. bf5xx_pcm_to_ac97((struct ac97_frame *)sport->tx_dma_buf +
  58. sport->tx_pos, (__u16 *)runtime->dma_area + sport->tx_pos *
  59. runtime->channels, count, chan_mask);
  60. sport->tx_pos += runtime->period_size;
  61. if (sport->tx_pos >= runtime->buffer_size)
  62. sport->tx_pos %= runtime->buffer_size;
  63. sport->tx_delay_pos = sport->tx_pos;
  64. } else {
  65. bf5xx_ac97_to_pcm((struct ac97_frame *)sport->rx_dma_buf +
  66. sport->rx_pos, (__u16 *)runtime->dma_area + sport->rx_pos *
  67. runtime->channels, count);
  68. sport->rx_pos += runtime->period_size;
  69. if (sport->rx_pos >= runtime->buffer_size)
  70. sport->rx_pos %= runtime->buffer_size;
  71. }
  72. }
  73. #endif
  74. static void bf5xx_dma_irq(void *data)
  75. {
  76. struct snd_pcm_substream *pcm = data;
  77. #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
  78. struct snd_pcm_runtime *runtime = pcm->runtime;
  79. struct sport_device *sport = runtime->private_data;
  80. bf5xx_mmap_copy(pcm, runtime->period_size);
  81. if (pcm->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  82. if (sport->once == 0) {
  83. snd_pcm_period_elapsed(pcm);
  84. bf5xx_mmap_copy(pcm, runtime->period_size);
  85. sport->once = 1;
  86. }
  87. }
  88. #endif
  89. snd_pcm_period_elapsed(pcm);
  90. }
  91. /* The memory size for pure pcm data is 128*1024 = 0x20000 bytes.
  92. * The total rx/tx buffer is for ac97 frame to hold all pcm data
  93. * is 0x20000 * sizeof(struct ac97_frame) / 4.
  94. */
  95. #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
  96. static const struct snd_pcm_hardware bf5xx_pcm_hardware = {
  97. .info = SNDRV_PCM_INFO_INTERLEAVED |
  98. SNDRV_PCM_INFO_MMAP |
  99. SNDRV_PCM_INFO_MMAP_VALID |
  100. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  101. #else
  102. static const struct snd_pcm_hardware bf5xx_pcm_hardware = {
  103. .info = SNDRV_PCM_INFO_INTERLEAVED |
  104. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  105. #endif
  106. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  107. .period_bytes_min = 32,
  108. .period_bytes_max = 0x10000,
  109. .periods_min = 1,
  110. .periods_max = PAGE_SIZE/32,
  111. .buffer_bytes_max = 0x20000, /* 128 kbytes */
  112. .fifo_size = 16,
  113. };
  114. static int bf5xx_pcm_hw_params(struct snd_pcm_substream *substream,
  115. struct snd_pcm_hw_params *params)
  116. {
  117. size_t size = bf5xx_pcm_hardware.buffer_bytes_max
  118. * sizeof(struct ac97_frame) / 4;
  119. snd_pcm_lib_malloc_pages(substream, size);
  120. return 0;
  121. }
  122. static int bf5xx_pcm_hw_free(struct snd_pcm_substream *substream)
  123. {
  124. #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
  125. struct snd_pcm_runtime *runtime = substream->runtime;
  126. struct sport_device *sport = runtime->private_data;
  127. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  128. sport->once = 0;
  129. if (runtime->dma_area)
  130. memset(runtime->dma_area, 0, runtime->buffer_size);
  131. memset(sport->tx_dma_buf, 0, runtime->buffer_size *
  132. sizeof(struct ac97_frame));
  133. } else
  134. memset(sport->rx_dma_buf, 0, runtime->buffer_size *
  135. sizeof(struct ac97_frame));
  136. #endif
  137. snd_pcm_lib_free_pages(substream);
  138. return 0;
  139. }
  140. static int bf5xx_pcm_prepare(struct snd_pcm_substream *substream)
  141. {
  142. struct snd_pcm_runtime *runtime = substream->runtime;
  143. struct sport_device *sport = runtime->private_data;
  144. /* An intermediate buffer is introduced for implementing mmap for
  145. * SPORT working in TMD mode(include AC97).
  146. */
  147. #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
  148. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  149. sport_set_tx_callback(sport, bf5xx_dma_irq, substream);
  150. sport_config_tx_dma(sport, sport->tx_dma_buf, runtime->periods,
  151. runtime->period_size * sizeof(struct ac97_frame));
  152. } else {
  153. sport_set_rx_callback(sport, bf5xx_dma_irq, substream);
  154. sport_config_rx_dma(sport, sport->rx_dma_buf, runtime->periods,
  155. runtime->period_size * sizeof(struct ac97_frame));
  156. }
  157. #else
  158. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  159. sport_set_tx_callback(sport, bf5xx_dma_irq, substream);
  160. sport_config_tx_dma(sport, runtime->dma_area, runtime->periods,
  161. runtime->period_size * sizeof(struct ac97_frame));
  162. } else {
  163. sport_set_rx_callback(sport, bf5xx_dma_irq, substream);
  164. sport_config_rx_dma(sport, runtime->dma_area, runtime->periods,
  165. runtime->period_size * sizeof(struct ac97_frame));
  166. }
  167. #endif
  168. return 0;
  169. }
  170. static int bf5xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  171. {
  172. struct snd_pcm_runtime *runtime = substream->runtime;
  173. struct sport_device *sport = runtime->private_data;
  174. int ret = 0;
  175. pr_debug("%s enter\n", __func__);
  176. switch (cmd) {
  177. case SNDRV_PCM_TRIGGER_START:
  178. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  179. #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
  180. bf5xx_mmap_copy(substream, runtime->period_size);
  181. sport->tx_delay_pos = 0;
  182. #endif
  183. sport_tx_start(sport);
  184. } else
  185. sport_rx_start(sport);
  186. break;
  187. case SNDRV_PCM_TRIGGER_STOP:
  188. case SNDRV_PCM_TRIGGER_SUSPEND:
  189. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  190. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  191. #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
  192. sport->tx_pos = 0;
  193. #endif
  194. sport_tx_stop(sport);
  195. } else {
  196. #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
  197. sport->rx_pos = 0;
  198. #endif
  199. sport_rx_stop(sport);
  200. }
  201. break;
  202. default:
  203. ret = -EINVAL;
  204. }
  205. return ret;
  206. }
  207. static snd_pcm_uframes_t bf5xx_pcm_pointer(struct snd_pcm_substream *substream)
  208. {
  209. struct snd_pcm_runtime *runtime = substream->runtime;
  210. struct sport_device *sport = runtime->private_data;
  211. unsigned int curr;
  212. #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
  213. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  214. curr = sport->tx_delay_pos;
  215. else
  216. curr = sport->rx_pos;
  217. #else
  218. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  219. curr = sport_curr_offset_tx(sport) / sizeof(struct ac97_frame);
  220. else
  221. curr = sport_curr_offset_rx(sport) / sizeof(struct ac97_frame);
  222. #endif
  223. return curr;
  224. }
  225. static int bf5xx_pcm_open(struct snd_pcm_substream *substream)
  226. {
  227. struct snd_pcm_runtime *runtime = substream->runtime;
  228. int ret;
  229. pr_debug("%s enter\n", __func__);
  230. snd_soc_set_runtime_hwparams(substream, &bf5xx_pcm_hardware);
  231. ret = snd_pcm_hw_constraint_integer(runtime,
  232. SNDRV_PCM_HW_PARAM_PERIODS);
  233. if (ret < 0)
  234. goto out;
  235. if (sport_handle != NULL)
  236. runtime->private_data = sport_handle;
  237. else {
  238. pr_err("sport_handle is NULL\n");
  239. return -1;
  240. }
  241. return 0;
  242. out:
  243. return ret;
  244. }
  245. #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
  246. static int bf5xx_pcm_mmap(struct snd_pcm_substream *substream,
  247. struct vm_area_struct *vma)
  248. {
  249. struct snd_pcm_runtime *runtime = substream->runtime;
  250. size_t size = vma->vm_end - vma->vm_start;
  251. vma->vm_start = (unsigned long)runtime->dma_area;
  252. vma->vm_end = vma->vm_start + size;
  253. vma->vm_flags |= VM_SHARED;
  254. return 0 ;
  255. }
  256. #else
  257. static int bf5xx_pcm_copy(struct snd_pcm_substream *substream, int channel,
  258. snd_pcm_uframes_t pos,
  259. void __user *buf, snd_pcm_uframes_t count)
  260. {
  261. struct snd_pcm_runtime *runtime = substream->runtime;
  262. unsigned int chan_mask = ac97_chan_mask[runtime->channels - 1];
  263. pr_debug("%s copy pos:0x%lx count:0x%lx\n",
  264. substream->stream ? "Capture" : "Playback", pos, count);
  265. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  266. bf5xx_pcm_to_ac97((struct ac97_frame *)runtime->dma_area + pos,
  267. (__u16 *)buf, count, chan_mask);
  268. else
  269. bf5xx_ac97_to_pcm((struct ac97_frame *)runtime->dma_area + pos,
  270. (__u16 *)buf, count);
  271. return 0;
  272. }
  273. #endif
  274. struct snd_pcm_ops bf5xx_pcm_ac97_ops = {
  275. .open = bf5xx_pcm_open,
  276. .ioctl = snd_pcm_lib_ioctl,
  277. .hw_params = bf5xx_pcm_hw_params,
  278. .hw_free = bf5xx_pcm_hw_free,
  279. .prepare = bf5xx_pcm_prepare,
  280. .trigger = bf5xx_pcm_trigger,
  281. .pointer = bf5xx_pcm_pointer,
  282. #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
  283. .mmap = bf5xx_pcm_mmap,
  284. #else
  285. .copy = bf5xx_pcm_copy,
  286. #endif
  287. };
  288. static int bf5xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  289. {
  290. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  291. struct snd_dma_buffer *buf = &substream->dma_buffer;
  292. size_t size = bf5xx_pcm_hardware.buffer_bytes_max
  293. * sizeof(struct ac97_frame) / 4;
  294. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  295. buf->dev.dev = pcm->card->dev;
  296. buf->private_data = NULL;
  297. buf->area = dma_alloc_coherent(pcm->card->dev, size,
  298. &buf->addr, GFP_KERNEL);
  299. if (!buf->area) {
  300. pr_err("Failed to allocate dma memory\n");
  301. pr_err("Please increase uncached DMA memory region\n");
  302. return -ENOMEM;
  303. }
  304. buf->bytes = size;
  305. pr_debug("%s, area:%p, size:0x%08lx\n", __func__,
  306. buf->area, buf->bytes);
  307. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  308. sport_handle->tx_buf = buf->area;
  309. else
  310. sport_handle->rx_buf = buf->area;
  311. /*
  312. * Need to allocate local buffer when enable
  313. * MMAP for SPORT working in TMD mode (include AC97).
  314. */
  315. #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
  316. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  317. if (!sport_handle->tx_dma_buf) {
  318. sport_handle->tx_dma_buf = dma_alloc_coherent(NULL, \
  319. size, &sport_handle->tx_dma_phy, GFP_KERNEL);
  320. if (!sport_handle->tx_dma_buf) {
  321. pr_err("Failed to allocate memory for tx dma \
  322. buf - Please increase uncached DMA \
  323. memory region\n");
  324. return -ENOMEM;
  325. } else
  326. memset(sport_handle->tx_dma_buf, 0, size);
  327. } else
  328. memset(sport_handle->tx_dma_buf, 0, size);
  329. } else {
  330. if (!sport_handle->rx_dma_buf) {
  331. sport_handle->rx_dma_buf = dma_alloc_coherent(NULL, \
  332. size, &sport_handle->rx_dma_phy, GFP_KERNEL);
  333. if (!sport_handle->rx_dma_buf) {
  334. pr_err("Failed to allocate memory for rx dma \
  335. buf - Please increase uncached DMA \
  336. memory region\n");
  337. return -ENOMEM;
  338. } else
  339. memset(sport_handle->rx_dma_buf, 0, size);
  340. } else
  341. memset(sport_handle->rx_dma_buf, 0, size);
  342. }
  343. #endif
  344. return 0;
  345. }
  346. static void bf5xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  347. {
  348. struct snd_pcm_substream *substream;
  349. struct snd_dma_buffer *buf;
  350. int stream;
  351. #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
  352. size_t size = bf5xx_pcm_hardware.buffer_bytes_max *
  353. sizeof(struct ac97_frame) / 4;
  354. #endif
  355. for (stream = 0; stream < 2; stream++) {
  356. substream = pcm->streams[stream].substream;
  357. if (!substream)
  358. continue;
  359. buf = &substream->dma_buffer;
  360. if (!buf->area)
  361. continue;
  362. dma_free_coherent(NULL, buf->bytes, buf->area, 0);
  363. buf->area = NULL;
  364. #if defined(CONFIG_SND_BF5XX_MMAP_SUPPORT)
  365. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  366. if (sport_handle->tx_dma_buf)
  367. dma_free_coherent(NULL, size, \
  368. sport_handle->tx_dma_buf, 0);
  369. sport_handle->tx_dma_buf = NULL;
  370. } else {
  371. if (sport_handle->rx_dma_buf)
  372. dma_free_coherent(NULL, size, \
  373. sport_handle->rx_dma_buf, 0);
  374. sport_handle->rx_dma_buf = NULL;
  375. }
  376. #endif
  377. }
  378. if (sport_handle)
  379. sport_done(sport_handle);
  380. }
  381. static u64 bf5xx_pcm_dmamask = DMA_32BIT_MASK;
  382. int bf5xx_pcm_ac97_new(struct snd_card *card, struct snd_soc_dai *dai,
  383. struct snd_pcm *pcm)
  384. {
  385. int ret = 0;
  386. pr_debug("%s enter\n", __func__);
  387. if (!card->dev->dma_mask)
  388. card->dev->dma_mask = &bf5xx_pcm_dmamask;
  389. if (!card->dev->coherent_dma_mask)
  390. card->dev->coherent_dma_mask = DMA_32BIT_MASK;
  391. if (dai->playback.channels_min) {
  392. ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
  393. SNDRV_PCM_STREAM_PLAYBACK);
  394. if (ret)
  395. goto out;
  396. }
  397. if (dai->capture.channels_min) {
  398. ret = bf5xx_pcm_preallocate_dma_buffer(pcm,
  399. SNDRV_PCM_STREAM_CAPTURE);
  400. if (ret)
  401. goto out;
  402. }
  403. out:
  404. return ret;
  405. }
  406. struct snd_soc_platform bf5xx_ac97_soc_platform = {
  407. .name = "bf5xx-audio",
  408. .pcm_ops = &bf5xx_pcm_ac97_ops,
  409. .pcm_new = bf5xx_pcm_ac97_new,
  410. .pcm_free = bf5xx_pcm_free_dma_buffers,
  411. };
  412. EXPORT_SYMBOL_GPL(bf5xx_ac97_soc_platform);
  413. MODULE_AUTHOR("Cliff Cai");
  414. MODULE_DESCRIPTION("ADI Blackfin AC97 PCM DMA module");
  415. MODULE_LICENSE("GPL");