bf5xx-ac97-pcm.c 13 KB

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