Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * omap-pcm.c -- ALSA PCM interface for the OMAP SoC |
| 3 | * |
| 4 | * Copyright (C) 2008 Nokia Corporation |
| 5 | * |
Jarkko Nikula | 7ec41ee | 2011-08-11 15:44:57 +0300 | [diff] [blame] | 6 | * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com> |
Peter Ujfalusi | 1c7687b | 2011-05-03 18:14:43 +0300 | [diff] [blame] | 7 | * Peter Ujfalusi <peter.ujfalusi@ti.com> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * version 2 as published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 21 | * 02110-1301 USA |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <linux/dma-mapping.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Peter Ujfalusi | 946cc36 | 2012-09-14 15:05:58 +0300 | [diff] [blame] | 28 | #include <linux/omap-dma.h> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 29 | #include <sound/core.h> |
| 30 | #include <sound/pcm.h> |
| 31 | #include <sound/pcm_params.h> |
Peter Ujfalusi | 946cc36 | 2012-09-14 15:05:58 +0300 | [diff] [blame] | 32 | #include <sound/dmaengine_pcm.h> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 33 | #include <sound/soc.h> |
| 34 | |
Tony Lindgren | 27615a9 | 2012-10-15 16:24:23 -0700 | [diff] [blame] | 35 | #ifdef CONFIG_ARCH_OMAP1 |
| 36 | #define pcm_omap1510() cpu_is_omap1510() |
| 37 | #else |
| 38 | #define pcm_omap1510() 0 |
| 39 | #endif |
| 40 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 41 | static const struct snd_pcm_hardware omap_pcm_hardware = { |
| 42 | .info = SNDRV_PCM_INFO_MMAP | |
| 43 | SNDRV_PCM_INFO_MMAP_VALID | |
| 44 | SNDRV_PCM_INFO_INTERLEAVED | |
| 45 | SNDRV_PCM_INFO_PAUSE | |
Peter Ujfalusi | b417382 | 2011-05-12 13:04:55 +0300 | [diff] [blame] | 46 | SNDRV_PCM_INFO_RESUME | |
| 47 | SNDRV_PCM_INFO_NO_PERIOD_WAKEUP, |
Misael Lopez Cruz | e17dd32 | 2010-02-22 15:09:19 -0600 | [diff] [blame] | 48 | .formats = SNDRV_PCM_FMTBIT_S16_LE | |
| 49 | SNDRV_PCM_FMTBIT_S32_LE, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 50 | .period_bytes_min = 32, |
| 51 | .period_bytes_max = 64 * 1024, |
| 52 | .periods_min = 2, |
| 53 | .periods_max = 255, |
| 54 | .buffer_bytes_max = 128 * 1024, |
| 55 | }; |
| 56 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 57 | /* this may get called several times by oss emulation */ |
| 58 | static int omap_pcm_hw_params(struct snd_pcm_substream *substream, |
| 59 | struct snd_pcm_hw_params *params) |
| 60 | { |
| 61 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 62 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 63 | struct omap_pcm_dma_data *dma_data; |
Peter Ujfalusi | 946cc36 | 2012-09-14 15:05:58 +0300 | [diff] [blame] | 64 | struct dma_slave_config config; |
| 65 | struct dma_chan *chan; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 66 | int err = 0; |
| 67 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 68 | dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 69 | |
Joonyoung Shim | 1b4246a | 2009-04-22 10:56:50 +0900 | [diff] [blame] | 70 | /* return if this is a bufferless transfer e.g. |
| 71 | * codec <--> BT codec or GSM modem -- lg FIXME */ |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 72 | if (!dma_data) |
Joonyoung Shim | 1b4246a | 2009-04-22 10:56:50 +0900 | [diff] [blame] | 73 | return 0; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 74 | |
| 75 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 76 | runtime->dma_bytes = params_buffer_bytes(params); |
| 77 | |
Peter Ujfalusi | 946cc36 | 2012-09-14 15:05:58 +0300 | [diff] [blame] | 78 | chan = snd_dmaengine_pcm_get_chan(substream); |
| 79 | if (!chan) |
| 80 | return -EINVAL; |
| 81 | |
| 82 | /* fills in addr_width and direction */ |
| 83 | err = snd_hwparams_to_dma_slave_config(substream, params, &config); |
| 84 | if (err) |
| 85 | return err; |
| 86 | |
Lars-Peter Clausen | 09ae3aa | 2013-04-03 11:06:05 +0200 | [diff] [blame] | 87 | snd_dmaengine_pcm_set_config_from_dai_data(substream, |
| 88 | snd_soc_dai_get_dma_data(rtd->cpu_dai, substream), |
| 89 | &config); |
Peter Ujfalusi | 946cc36 | 2012-09-14 15:05:58 +0300 | [diff] [blame] | 90 | |
| 91 | return dmaengine_slave_config(chan, &config); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static int omap_pcm_hw_free(struct snd_pcm_substream *substream) |
| 95 | { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 96 | snd_pcm_set_runtime_buffer(substream, NULL); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 100 | static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream) |
| 101 | { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 102 | snd_pcm_uframes_t offset; |
| 103 | |
Tony Lindgren | 27615a9 | 2012-10-15 16:24:23 -0700 | [diff] [blame] | 104 | if (pcm_omap1510()) |
Peter Ujfalusi | 946cc36 | 2012-09-14 15:05:58 +0300 | [diff] [blame] | 105 | offset = snd_dmaengine_pcm_pointer_no_residue(substream); |
| 106 | else |
| 107 | offset = snd_dmaengine_pcm_pointer(substream); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 108 | |
| 109 | return offset; |
| 110 | } |
| 111 | |
| 112 | static int omap_pcm_open(struct snd_pcm_substream *substream) |
| 113 | { |
Peter Ujfalusi | 946cc36 | 2012-09-14 15:05:58 +0300 | [diff] [blame] | 114 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Lars-Peter Clausen | 09ae3aa | 2013-04-03 11:06:05 +0200 | [diff] [blame] | 115 | struct snd_dmaengine_dai_dma_data *dma_data; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 116 | |
| 117 | snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware); |
| 118 | |
Peter Ujfalusi | 946cc36 | 2012-09-14 15:05:58 +0300 | [diff] [blame] | 119 | dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
Peter Ujfalusi | a92b531 | 2013-02-05 13:43:39 +0100 | [diff] [blame] | 120 | |
Lars-Peter Clausen | 7c1c1d4 | 2013-04-15 19:19:48 +0200 | [diff] [blame] | 121 | return snd_dmaengine_pcm_open_request_chan(substream, |
| 122 | omap_dma_filter_fn, |
| 123 | dma_data->filter_data); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 126 | static int omap_pcm_mmap(struct snd_pcm_substream *substream, |
| 127 | struct vm_area_struct *vma) |
| 128 | { |
| 129 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 130 | |
| 131 | return dma_mmap_writecombine(substream->pcm->card->dev, vma, |
| 132 | runtime->dma_area, |
| 133 | runtime->dma_addr, |
| 134 | runtime->dma_bytes); |
| 135 | } |
| 136 | |
Mark Brown | b2a19d0 | 2009-01-17 19:14:26 +0000 | [diff] [blame] | 137 | static struct snd_pcm_ops omap_pcm_ops = { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 138 | .open = omap_pcm_open, |
Lars-Peter Clausen | 7c1c1d4 | 2013-04-15 19:19:48 +0200 | [diff] [blame] | 139 | .close = snd_dmaengine_pcm_close_release_chan, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 140 | .ioctl = snd_pcm_lib_ioctl, |
| 141 | .hw_params = omap_pcm_hw_params, |
| 142 | .hw_free = omap_pcm_hw_free, |
Lars-Peter Clausen | abe9937 | 2013-03-25 16:58:16 +0100 | [diff] [blame] | 143 | .trigger = snd_dmaengine_pcm_trigger, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 144 | .pointer = omap_pcm_pointer, |
| 145 | .mmap = omap_pcm_mmap, |
| 146 | }; |
| 147 | |
Eduardo Valentin | a152ff2 | 2009-08-20 16:18:22 +0300 | [diff] [blame] | 148 | static u64 omap_pcm_dmamask = DMA_BIT_MASK(64); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 149 | |
| 150 | static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, |
| 151 | int stream) |
| 152 | { |
| 153 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; |
| 154 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
| 155 | size_t size = omap_pcm_hardware.buffer_bytes_max; |
| 156 | |
| 157 | buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 158 | buf->dev.dev = pcm->card->dev; |
| 159 | buf->private_data = NULL; |
| 160 | buf->area = dma_alloc_writecombine(pcm->card->dev, size, |
| 161 | &buf->addr, GFP_KERNEL); |
| 162 | if (!buf->area) |
| 163 | return -ENOMEM; |
| 164 | |
| 165 | buf->bytes = size; |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm) |
| 170 | { |
| 171 | struct snd_pcm_substream *substream; |
| 172 | struct snd_dma_buffer *buf; |
| 173 | int stream; |
| 174 | |
| 175 | for (stream = 0; stream < 2; stream++) { |
| 176 | substream = pcm->streams[stream].substream; |
| 177 | if (!substream) |
| 178 | continue; |
| 179 | |
| 180 | buf = &substream->dma_buffer; |
| 181 | if (!buf->area) |
| 182 | continue; |
| 183 | |
| 184 | dma_free_writecombine(pcm->card->dev, buf->bytes, |
| 185 | buf->area, buf->addr); |
| 186 | buf->area = NULL; |
| 187 | } |
| 188 | } |
| 189 | |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 190 | static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 191 | { |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 192 | struct snd_card *card = rtd->card->snd_card; |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 193 | struct snd_pcm *pcm = rtd->pcm; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 194 | int ret = 0; |
| 195 | |
| 196 | if (!card->dev->dma_mask) |
| 197 | card->dev->dma_mask = &omap_pcm_dmamask; |
| 198 | if (!card->dev->coherent_dma_mask) |
Eduardo Valentin | a152ff2 | 2009-08-20 16:18:22 +0300 | [diff] [blame] | 199 | card->dev->coherent_dma_mask = DMA_BIT_MASK(64); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 200 | |
Joachim Eastwood | 25e9e75 | 2012-01-01 01:58:44 +0100 | [diff] [blame] | 201 | if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 202 | ret = omap_pcm_preallocate_dma_buffer(pcm, |
| 203 | SNDRV_PCM_STREAM_PLAYBACK); |
| 204 | if (ret) |
| 205 | goto out; |
| 206 | } |
| 207 | |
Joachim Eastwood | 25e9e75 | 2012-01-01 01:58:44 +0100 | [diff] [blame] | 208 | if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 209 | ret = omap_pcm_preallocate_dma_buffer(pcm, |
| 210 | SNDRV_PCM_STREAM_CAPTURE); |
| 211 | if (ret) |
| 212 | goto out; |
| 213 | } |
| 214 | |
| 215 | out: |
Oleg Matcovschi | fad9365 | 2012-04-24 19:02:02 -0700 | [diff] [blame] | 216 | /* free preallocated buffers in case of error */ |
| 217 | if (ret) |
| 218 | omap_pcm_free_dma_buffers(pcm); |
| 219 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 220 | return ret; |
| 221 | } |
| 222 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 223 | static struct snd_soc_platform_driver omap_soc_platform = { |
| 224 | .ops = &omap_pcm_ops, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 225 | .pcm_new = omap_pcm_new, |
| 226 | .pcm_free = omap_pcm_free_dma_buffers, |
| 227 | }; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 228 | |
Bill Pemberton | 7ff6000 | 2012-12-07 09:26:29 -0500 | [diff] [blame] | 229 | static int omap_pcm_probe(struct platform_device *pdev) |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 230 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 231 | return snd_soc_register_platform(&pdev->dev, |
| 232 | &omap_soc_platform); |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 233 | } |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 234 | |
Bill Pemberton | 7ff6000 | 2012-12-07 09:26:29 -0500 | [diff] [blame] | 235 | static int omap_pcm_remove(struct platform_device *pdev) |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 236 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 237 | snd_soc_unregister_platform(&pdev->dev); |
| 238 | return 0; |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 239 | } |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 240 | |
| 241 | static struct platform_driver omap_pcm_driver = { |
| 242 | .driver = { |
| 243 | .name = "omap-pcm-audio", |
| 244 | .owner = THIS_MODULE, |
| 245 | }, |
| 246 | |
| 247 | .probe = omap_pcm_probe, |
Bill Pemberton | 7ff6000 | 2012-12-07 09:26:29 -0500 | [diff] [blame] | 248 | .remove = omap_pcm_remove, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
Axel Lin | beda5bf5 | 2011-11-25 10:12:16 +0800 | [diff] [blame] | 251 | module_platform_driver(omap_pcm_driver); |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 252 | |
Jarkko Nikula | 7ec41ee | 2011-08-11 15:44:57 +0300 | [diff] [blame] | 253 | MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>"); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 254 | MODULE_DESCRIPTION("OMAP PCM DMA module"); |
| 255 | MODULE_LICENSE("GPL"); |
Guillaume Gardet | 5e70b7fc | 2012-07-12 15:08:16 +0200 | [diff] [blame] | 256 | MODULE_ALIAS("platform:omap-pcm-audio"); |