Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved. |
| 3 | * |
| 4 | * Based on sound/soc/imx/imx-pcm-dma-mx2.c |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/clk.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/device.h> |
| 24 | #include <linux/dma-mapping.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/dmaengine.h> |
Huang Shijie | 3946860 | 2012-02-16 14:17:32 +0800 | [diff] [blame] | 31 | #include <linux/fsl/mxs-dma.h> |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 32 | |
| 33 | #include <sound/core.h> |
| 34 | #include <sound/initval.h> |
| 35 | #include <sound/pcm.h> |
| 36 | #include <sound/pcm_params.h> |
| 37 | #include <sound/soc.h> |
Lars-Peter Clausen | 016ab46 | 2012-02-22 10:49:10 +0100 | [diff] [blame] | 38 | #include <sound/dmaengine_pcm.h> |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 39 | |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 40 | #include "mxs-pcm.h" |
| 41 | |
Lars-Peter Clausen | 016ab46 | 2012-02-22 10:49:10 +0100 | [diff] [blame] | 42 | struct mxs_pcm_dma_data { |
| 43 | struct mxs_dma_data dma_data; |
| 44 | struct mxs_pcm_dma_params *dma_params; |
| 45 | }; |
| 46 | |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 47 | static struct snd_pcm_hardware snd_mxs_hardware = { |
| 48 | .info = SNDRV_PCM_INFO_MMAP | |
| 49 | SNDRV_PCM_INFO_MMAP_VALID | |
| 50 | SNDRV_PCM_INFO_PAUSE | |
| 51 | SNDRV_PCM_INFO_RESUME | |
| 52 | SNDRV_PCM_INFO_INTERLEAVED, |
| 53 | .formats = SNDRV_PCM_FMTBIT_S16_LE | |
| 54 | SNDRV_PCM_FMTBIT_S20_3LE | |
| 55 | SNDRV_PCM_FMTBIT_S24_LE, |
| 56 | .channels_min = 2, |
| 57 | .channels_max = 2, |
| 58 | .period_bytes_min = 32, |
| 59 | .period_bytes_max = 8192, |
| 60 | .periods_min = 1, |
| 61 | .periods_max = 52, |
| 62 | .buffer_bytes_max = 64 * 1024, |
| 63 | .fifo_size = 32, |
| 64 | |
| 65 | }; |
| 66 | |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 67 | static bool filter(struct dma_chan *chan, void *param) |
| 68 | { |
Lars-Peter Clausen | 016ab46 | 2012-02-22 10:49:10 +0100 | [diff] [blame] | 69 | struct mxs_pcm_dma_data *pcm_dma_data = param; |
| 70 | struct mxs_pcm_dma_params *dma_params = pcm_dma_data->dma_params; |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 71 | |
| 72 | if (!mxs_dma_is_apbx(chan)) |
| 73 | return false; |
| 74 | |
| 75 | if (chan->chan_id != dma_params->chan_num) |
| 76 | return false; |
| 77 | |
Lars-Peter Clausen | 016ab46 | 2012-02-22 10:49:10 +0100 | [diff] [blame] | 78 | chan->private = &pcm_dma_data->dma_data; |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 79 | |
| 80 | return true; |
| 81 | } |
| 82 | |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 83 | static int snd_mxs_pcm_hw_params(struct snd_pcm_substream *substream, |
| 84 | struct snd_pcm_hw_params *params) |
| 85 | { |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 86 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 87 | |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 91 | static int snd_mxs_open(struct snd_pcm_substream *substream) |
| 92 | { |
Lars-Peter Clausen | 016ab46 | 2012-02-22 10:49:10 +0100 | [diff] [blame] | 93 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 94 | struct mxs_pcm_dma_data *pcm_dma_data; |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 95 | int ret; |
| 96 | |
Lars-Peter Clausen | 016ab46 | 2012-02-22 10:49:10 +0100 | [diff] [blame] | 97 | pcm_dma_data = kzalloc(sizeof(*pcm_dma_data), GFP_KERNEL); |
| 98 | if (pcm_dma_data == NULL) |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 99 | return -ENOMEM; |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 100 | |
Lars-Peter Clausen | 016ab46 | 2012-02-22 10:49:10 +0100 | [diff] [blame] | 101 | pcm_dma_data->dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
| 102 | pcm_dma_data->dma_data.chan_irq = pcm_dma_data->dma_params->chan_irq; |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 103 | |
Lars-Peter Clausen | 016ab46 | 2012-02-22 10:49:10 +0100 | [diff] [blame] | 104 | ret = snd_dmaengine_pcm_open(substream, filter, pcm_dma_data); |
Lars-Peter Clausen | 95a771c | 2012-02-22 10:49:07 +0100 | [diff] [blame] | 105 | if (ret) { |
Lars-Peter Clausen | 016ab46 | 2012-02-22 10:49:10 +0100 | [diff] [blame] | 106 | kfree(pcm_dma_data); |
Lars-Peter Clausen | 95a771c | 2012-02-22 10:49:07 +0100 | [diff] [blame] | 107 | return ret; |
| 108 | } |
| 109 | |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 110 | snd_soc_set_runtime_hwparams(substream, &snd_mxs_hardware); |
| 111 | |
Lars-Peter Clausen | 016ab46 | 2012-02-22 10:49:10 +0100 | [diff] [blame] | 112 | snd_dmaengine_pcm_set_data(substream, pcm_dma_data); |
| 113 | |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int snd_mxs_close(struct snd_pcm_substream *substream) |
| 118 | { |
Lars-Peter Clausen | 016ab46 | 2012-02-22 10:49:10 +0100 | [diff] [blame] | 119 | struct mxs_pcm_dma_data *pcm_dma_data = snd_dmaengine_pcm_get_data(substream); |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 120 | |
Lars-Peter Clausen | 016ab46 | 2012-02-22 10:49:10 +0100 | [diff] [blame] | 121 | snd_dmaengine_pcm_close(substream); |
| 122 | kfree(pcm_dma_data); |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int snd_mxs_pcm_mmap(struct snd_pcm_substream *substream, |
| 128 | struct vm_area_struct *vma) |
| 129 | { |
| 130 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 131 | |
| 132 | return dma_mmap_writecombine(substream->pcm->card->dev, vma, |
| 133 | runtime->dma_area, |
| 134 | runtime->dma_addr, |
| 135 | runtime->dma_bytes); |
| 136 | } |
| 137 | |
| 138 | static struct snd_pcm_ops mxs_pcm_ops = { |
| 139 | .open = snd_mxs_open, |
| 140 | .close = snd_mxs_close, |
| 141 | .ioctl = snd_pcm_lib_ioctl, |
| 142 | .hw_params = snd_mxs_pcm_hw_params, |
Lars-Peter Clausen | 016ab46 | 2012-02-22 10:49:10 +0100 | [diff] [blame] | 143 | .trigger = snd_dmaengine_pcm_trigger, |
Lars-Peter Clausen | 9883ab2 | 2012-06-11 20:11:41 +0200 | [diff] [blame] | 144 | .pointer = snd_dmaengine_pcm_pointer_no_residue, |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 145 | .mmap = snd_mxs_pcm_mmap, |
| 146 | }; |
| 147 | |
| 148 | static int mxs_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream) |
| 149 | { |
| 150 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; |
| 151 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
| 152 | size_t size = snd_mxs_hardware.buffer_bytes_max; |
| 153 | |
| 154 | buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 155 | buf->dev.dev = pcm->card->dev; |
| 156 | buf->private_data = NULL; |
| 157 | buf->area = dma_alloc_writecombine(pcm->card->dev, size, |
| 158 | &buf->addr, GFP_KERNEL); |
| 159 | if (!buf->area) |
| 160 | return -ENOMEM; |
| 161 | buf->bytes = size; |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static u64 mxs_pcm_dmamask = DMA_BIT_MASK(32); |
| 167 | static int mxs_pcm_new(struct snd_soc_pcm_runtime *rtd) |
| 168 | { |
| 169 | struct snd_card *card = rtd->card->snd_card; |
| 170 | struct snd_pcm *pcm = rtd->pcm; |
| 171 | int ret = 0; |
| 172 | |
| 173 | if (!card->dev->dma_mask) |
| 174 | card->dev->dma_mask = &mxs_pcm_dmamask; |
| 175 | if (!card->dev->coherent_dma_mask) |
| 176 | card->dev->coherent_dma_mask = DMA_BIT_MASK(32); |
| 177 | |
| 178 | if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) { |
| 179 | ret = mxs_pcm_preallocate_dma_buffer(pcm, |
| 180 | SNDRV_PCM_STREAM_PLAYBACK); |
| 181 | if (ret) |
| 182 | goto out; |
| 183 | } |
| 184 | |
| 185 | if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { |
| 186 | ret = mxs_pcm_preallocate_dma_buffer(pcm, |
| 187 | SNDRV_PCM_STREAM_CAPTURE); |
| 188 | if (ret) |
| 189 | goto out; |
| 190 | } |
| 191 | |
| 192 | out: |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | static void mxs_pcm_free(struct snd_pcm *pcm) |
| 197 | { |
| 198 | struct snd_pcm_substream *substream; |
| 199 | struct snd_dma_buffer *buf; |
| 200 | int stream; |
| 201 | |
| 202 | for (stream = 0; stream < 2; stream++) { |
| 203 | substream = pcm->streams[stream].substream; |
| 204 | if (!substream) |
| 205 | continue; |
| 206 | |
| 207 | buf = &substream->dma_buffer; |
| 208 | if (!buf->area) |
| 209 | continue; |
| 210 | |
| 211 | dma_free_writecombine(pcm->card->dev, buf->bytes, |
| 212 | buf->area, buf->addr); |
| 213 | buf->area = NULL; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static struct snd_soc_platform_driver mxs_soc_platform = { |
| 218 | .ops = &mxs_pcm_ops, |
| 219 | .pcm_new = mxs_pcm_new, |
| 220 | .pcm_free = mxs_pcm_free, |
| 221 | }; |
| 222 | |
Bill Pemberton | fd58273 | 2012-12-07 09:26:27 -0500 | [diff] [blame] | 223 | int mxs_pcm_platform_register(struct device *dev) |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 224 | { |
Shawn Guo | 4da3fe7 | 2012-05-11 22:24:16 +0800 | [diff] [blame] | 225 | return snd_soc_register_platform(dev, &mxs_soc_platform); |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 226 | } |
Shawn Guo | 4da3fe7 | 2012-05-11 22:24:16 +0800 | [diff] [blame] | 227 | EXPORT_SYMBOL_GPL(mxs_pcm_platform_register); |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 228 | |
Bill Pemberton | fd58273 | 2012-12-07 09:26:27 -0500 | [diff] [blame] | 229 | void mxs_pcm_platform_unregister(struct device *dev) |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 230 | { |
Shawn Guo | 4da3fe7 | 2012-05-11 22:24:16 +0800 | [diff] [blame] | 231 | snd_soc_unregister_platform(dev); |
Dong Aisheng | ed6e1d0 | 2011-07-21 12:36:55 +0800 | [diff] [blame] | 232 | } |
Shawn Guo | 4da3fe7 | 2012-05-11 22:24:16 +0800 | [diff] [blame] | 233 | EXPORT_SYMBOL_GPL(mxs_pcm_platform_unregister); |
Lothar Waßmann | 06c8eb9 | 2011-12-09 14:38:11 +0100 | [diff] [blame] | 234 | |
| 235 | MODULE_LICENSE("GPL"); |