/kern_oII/sound/soc/blackfin/bf5xx-ac97-pcm.c

http://omnia2droid.googlecode.com/ · C · 468 lines · 383 code · 47 blank · 38 comment · 59 complexity · 5ee47bdfe6f18ee32a6520f5e88b1eb5 MD5 · raw file

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