Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * atmel-pcm.c -- ALSA PCM interface for the Atmel atmel SoC. |
| 3 | * |
| 4 | * Copyright (C) 2005 SAN People |
| 5 | * Copyright (C) 2008 Atmel |
| 6 | * |
| 7 | * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com> |
| 8 | * |
| 9 | * Based on at91-pcm. by: |
| 10 | * Frank Mandarino <fmandarino@endrelia.com> |
| 11 | * Copyright 2006 Endrelia Technologies Inc. |
| 12 | * |
| 13 | * Based on pxa2xx-pcm.c by: |
| 14 | * |
| 15 | * Author: Nicolas Pitre |
| 16 | * Created: Nov 30, 2004 |
| 17 | * Copyright: (C) 2004 MontaVista Software, Inc. |
| 18 | * |
| 19 | * This program is free software; you can redistribute it and/or modify |
| 20 | * it under the terms of the GNU General Public License as published by |
| 21 | * the Free Software Foundation; either version 2 of the License, or |
| 22 | * (at your option) any later version. |
| 23 | * |
| 24 | * This program is distributed in the hope that it will be useful, |
| 25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | * GNU General Public License for more details. |
| 28 | * |
| 29 | * You should have received a copy of the GNU General Public License |
| 30 | * along with this program; if not, write to the Free Software |
| 31 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 32 | */ |
| 33 | |
| 34 | #include <linux/module.h> |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 35 | #include <linux/dma-mapping.h> |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 36 | #include <sound/pcm.h> |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 37 | #include <sound/soc.h> |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 38 | #include "atmel-pcm.h" |
| 39 | |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 40 | static int atmel_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, |
| 41 | int stream) |
| 42 | { |
| 43 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; |
| 44 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
Bo Shen | 92dfa61 | 2012-11-28 11:46:12 +0800 | [diff] [blame] | 45 | size_t size = ATMEL_SSC_DMABUF_SIZE; |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 46 | |
| 47 | buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 48 | buf->dev.dev = pcm->card->dev; |
| 49 | buf->private_data = NULL; |
| 50 | buf->area = dma_alloc_coherent(pcm->card->dev, size, |
Bo Shen | 92dfa61 | 2012-11-28 11:46:12 +0800 | [diff] [blame] | 51 | &buf->addr, GFP_KERNEL); |
Joachim Eastwood | 153f5a1 | 2012-12-08 14:23:22 +0100 | [diff] [blame] | 52 | pr_debug("atmel-pcm: alloc dma buffer: area=%p, addr=%p, size=%zu\n", |
Bo Shen | 92dfa61 | 2012-11-28 11:46:12 +0800 | [diff] [blame] | 53 | (void *)buf->area, (void *)buf->addr, size); |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 54 | |
| 55 | if (!buf->area) |
| 56 | return -ENOMEM; |
| 57 | |
| 58 | buf->bytes = size; |
| 59 | return 0; |
| 60 | } |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 61 | |
Bo Shen | 92dfa61 | 2012-11-28 11:46:12 +0800 | [diff] [blame] | 62 | int atmel_pcm_mmap(struct snd_pcm_substream *substream, |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 63 | struct vm_area_struct *vma) |
| 64 | { |
| 65 | return remap_pfn_range(vma, vma->vm_start, |
| 66 | substream->dma_buffer.addr >> PAGE_SHIFT, |
| 67 | vma->vm_end - vma->vm_start, vma->vm_page_prot); |
| 68 | } |
Bo Shen | 92dfa61 | 2012-11-28 11:46:12 +0800 | [diff] [blame] | 69 | EXPORT_SYMBOL_GPL(atmel_pcm_mmap); |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 70 | |
Joachim Eastwood | 350e16d | 2012-01-01 02:43:03 +0100 | [diff] [blame] | 71 | static u64 atmel_pcm_dmamask = DMA_BIT_MASK(32); |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 72 | |
Bo Shen | 92dfa61 | 2012-11-28 11:46:12 +0800 | [diff] [blame] | 73 | int atmel_pcm_new(struct snd_soc_pcm_runtime *rtd) |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 74 | { |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 75 | struct snd_card *card = rtd->card->snd_card; |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 76 | struct snd_pcm *pcm = rtd->pcm; |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 77 | int ret = 0; |
| 78 | |
| 79 | if (!card->dev->dma_mask) |
| 80 | card->dev->dma_mask = &atmel_pcm_dmamask; |
| 81 | if (!card->dev->coherent_dma_mask) |
Joachim Eastwood | 350e16d | 2012-01-01 02:43:03 +0100 | [diff] [blame] | 82 | card->dev->coherent_dma_mask = DMA_BIT_MASK(32); |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 83 | |
Joachim Eastwood | 25e9e75 | 2012-01-01 01:58:44 +0100 | [diff] [blame] | 84 | if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) { |
Bo Shen | 92dfa61 | 2012-11-28 11:46:12 +0800 | [diff] [blame] | 85 | pr_debug("atmel-pcm: allocating PCM playback DMA buffer\n"); |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 86 | ret = atmel_pcm_preallocate_dma_buffer(pcm, |
| 87 | SNDRV_PCM_STREAM_PLAYBACK); |
| 88 | if (ret) |
| 89 | goto out; |
| 90 | } |
| 91 | |
Joachim Eastwood | 25e9e75 | 2012-01-01 01:58:44 +0100 | [diff] [blame] | 92 | if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { |
Bo Shen | 92dfa61 | 2012-11-28 11:46:12 +0800 | [diff] [blame] | 93 | pr_debug("atmel-pcm: allocating PCM capture DMA buffer\n"); |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 94 | ret = atmel_pcm_preallocate_dma_buffer(pcm, |
| 95 | SNDRV_PCM_STREAM_CAPTURE); |
| 96 | if (ret) |
| 97 | goto out; |
| 98 | } |
| 99 | out: |
| 100 | return ret; |
| 101 | } |
Bo Shen | 92dfa61 | 2012-11-28 11:46:12 +0800 | [diff] [blame] | 102 | EXPORT_SYMBOL_GPL(atmel_pcm_new); |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 103 | |
Bo Shen | 92dfa61 | 2012-11-28 11:46:12 +0800 | [diff] [blame] | 104 | void atmel_pcm_free(struct snd_pcm *pcm) |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 105 | { |
| 106 | struct snd_pcm_substream *substream; |
| 107 | struct snd_dma_buffer *buf; |
| 108 | int stream; |
| 109 | |
| 110 | for (stream = 0; stream < 2; stream++) { |
| 111 | substream = pcm->streams[stream].substream; |
| 112 | if (!substream) |
| 113 | continue; |
| 114 | |
| 115 | buf = &substream->dma_buffer; |
| 116 | if (!buf->area) |
| 117 | continue; |
| 118 | dma_free_coherent(pcm->card->dev, buf->bytes, |
| 119 | buf->area, buf->addr); |
| 120 | buf->area = NULL; |
| 121 | } |
| 122 | } |
Bo Shen | 92dfa61 | 2012-11-28 11:46:12 +0800 | [diff] [blame] | 123 | EXPORT_SYMBOL_GPL(atmel_pcm_free); |
Sedji Gaouaou | 6c74250 | 2008-10-03 16:57:50 +0200 | [diff] [blame] | 124 | |