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> |
Lars-Peter Clausen | 76a77f4 | 2014-06-19 09:40:29 +0200 | [diff] [blame] | 34 | #include <sound/omap-pcm.h> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 35 | |
Tony Lindgren | 27615a9 | 2012-10-15 16:24:23 -0700 | [diff] [blame] | 36 | #ifdef CONFIG_ARCH_OMAP1 |
| 37 | #define pcm_omap1510() cpu_is_omap1510() |
| 38 | #else |
| 39 | #define pcm_omap1510() 0 |
| 40 | #endif |
| 41 | |
Jyri Sarha | 6742e15 | 2015-03-03 13:28:53 +0200 | [diff] [blame] | 42 | static struct snd_pcm_hardware omap_pcm_hardware = { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 43 | .info = SNDRV_PCM_INFO_MMAP | |
| 44 | SNDRV_PCM_INFO_MMAP_VALID | |
| 45 | SNDRV_PCM_INFO_INTERLEAVED | |
| 46 | SNDRV_PCM_INFO_PAUSE | |
Peter Ujfalusi | b417382 | 2011-05-12 13:04:55 +0300 | [diff] [blame] | 47 | SNDRV_PCM_INFO_RESUME | |
| 48 | SNDRV_PCM_INFO_NO_PERIOD_WAKEUP, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 49 | .period_bytes_min = 32, |
| 50 | .period_bytes_max = 64 * 1024, |
| 51 | .periods_min = 2, |
| 52 | .periods_max = 255, |
| 53 | .buffer_bytes_max = 128 * 1024, |
| 54 | }; |
| 55 | |
Jyri Sarha | 6742e15 | 2015-03-03 13:28:53 +0200 | [diff] [blame] | 56 | /* sDMA supports only 1, 2, and 4 byte transfer elements. */ |
| 57 | static void omap_pcm_limit_supported_formats(void) |
| 58 | { |
| 59 | int i; |
| 60 | |
| 61 | for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) { |
| 62 | switch (snd_pcm_format_physical_width(i)) { |
| 63 | case 8: |
| 64 | case 16: |
| 65 | case 32: |
| 66 | omap_pcm_hardware.formats |= (1LL << i); |
| 67 | break; |
| 68 | default: |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 74 | /* this may get called several times by oss emulation */ |
| 75 | static int omap_pcm_hw_params(struct snd_pcm_substream *substream, |
| 76 | struct snd_pcm_hw_params *params) |
| 77 | { |
| 78 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 79 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 80 | struct omap_pcm_dma_data *dma_data; |
Peter Ujfalusi | 946cc36 | 2012-09-14 15:05:58 +0300 | [diff] [blame] | 81 | struct dma_slave_config config; |
| 82 | struct dma_chan *chan; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 83 | int err = 0; |
| 84 | |
Jim Lodes | 989ff77 | 2016-04-25 11:10:07 -0500 | [diff] [blame] | 85 | memset(&config, 0x00, sizeof(config)); |
| 86 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 87 | dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 88 | |
Joonyoung Shim | 1b4246a | 2009-04-22 10:56:50 +0900 | [diff] [blame] | 89 | /* return if this is a bufferless transfer e.g. |
| 90 | * codec <--> BT codec or GSM modem -- lg FIXME */ |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 91 | if (!dma_data) |
Joonyoung Shim | 1b4246a | 2009-04-22 10:56:50 +0900 | [diff] [blame] | 92 | return 0; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 93 | |
| 94 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 95 | runtime->dma_bytes = params_buffer_bytes(params); |
| 96 | |
Peter Ujfalusi | 946cc36 | 2012-09-14 15:05:58 +0300 | [diff] [blame] | 97 | chan = snd_dmaengine_pcm_get_chan(substream); |
| 98 | if (!chan) |
| 99 | return -EINVAL; |
| 100 | |
| 101 | /* fills in addr_width and direction */ |
| 102 | err = snd_hwparams_to_dma_slave_config(substream, params, &config); |
| 103 | if (err) |
| 104 | return err; |
| 105 | |
Lars-Peter Clausen | 09ae3aa | 2013-04-03 11:06:05 +0200 | [diff] [blame] | 106 | snd_dmaengine_pcm_set_config_from_dai_data(substream, |
| 107 | snd_soc_dai_get_dma_data(rtd->cpu_dai, substream), |
| 108 | &config); |
Peter Ujfalusi | 946cc36 | 2012-09-14 15:05:58 +0300 | [diff] [blame] | 109 | |
| 110 | return dmaengine_slave_config(chan, &config); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static int omap_pcm_hw_free(struct snd_pcm_substream *substream) |
| 114 | { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 115 | snd_pcm_set_runtime_buffer(substream, NULL); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 119 | static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream) |
| 120 | { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 121 | snd_pcm_uframes_t offset; |
| 122 | |
Tony Lindgren | 27615a9 | 2012-10-15 16:24:23 -0700 | [diff] [blame] | 123 | if (pcm_omap1510()) |
Peter Ujfalusi | 946cc36 | 2012-09-14 15:05:58 +0300 | [diff] [blame] | 124 | offset = snd_dmaengine_pcm_pointer_no_residue(substream); |
| 125 | else |
| 126 | offset = snd_dmaengine_pcm_pointer(substream); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 127 | |
| 128 | return offset; |
| 129 | } |
| 130 | |
| 131 | static int omap_pcm_open(struct snd_pcm_substream *substream) |
| 132 | { |
Peter Ujfalusi | 946cc36 | 2012-09-14 15:05:58 +0300 | [diff] [blame] | 133 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Lars-Peter Clausen | 09ae3aa | 2013-04-03 11:06:05 +0200 | [diff] [blame] | 134 | struct snd_dmaengine_dai_dma_data *dma_data; |
Peter Ujfalusi | f6becf0 | 2013-07-11 14:35:43 +0200 | [diff] [blame] | 135 | int ret; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 136 | |
| 137 | snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware); |
| 138 | |
Peter Ujfalusi | 946cc36 | 2012-09-14 15:05:58 +0300 | [diff] [blame] | 139 | dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
Peter Ujfalusi | a92b531 | 2013-02-05 13:43:39 +0100 | [diff] [blame] | 140 | |
Peter Ujfalusi | f6becf0 | 2013-07-11 14:35:43 +0200 | [diff] [blame] | 141 | /* DT boot: filter_data is the DMA name */ |
| 142 | if (rtd->cpu_dai->dev->of_node) { |
| 143 | struct dma_chan *chan; |
| 144 | |
| 145 | chan = dma_request_slave_channel(rtd->cpu_dai->dev, |
| 146 | dma_data->filter_data); |
| 147 | ret = snd_dmaengine_pcm_open(substream, chan); |
| 148 | } else { |
| 149 | ret = snd_dmaengine_pcm_open_request_chan(substream, |
| 150 | omap_dma_filter_fn, |
| 151 | dma_data->filter_data); |
| 152 | } |
| 153 | return ret; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 156 | static int omap_pcm_mmap(struct snd_pcm_substream *substream, |
| 157 | struct vm_area_struct *vma) |
| 158 | { |
| 159 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 160 | |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 161 | return dma_mmap_wc(substream->pcm->card->dev, vma, runtime->dma_area, |
| 162 | runtime->dma_addr, runtime->dma_bytes); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Mark Brown | b2a19d0 | 2009-01-17 19:14:26 +0000 | [diff] [blame] | 165 | static struct snd_pcm_ops omap_pcm_ops = { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 166 | .open = omap_pcm_open, |
Lars-Peter Clausen | 7c1c1d4 | 2013-04-15 19:19:48 +0200 | [diff] [blame] | 167 | .close = snd_dmaengine_pcm_close_release_chan, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 168 | .ioctl = snd_pcm_lib_ioctl, |
| 169 | .hw_params = omap_pcm_hw_params, |
| 170 | .hw_free = omap_pcm_hw_free, |
Lars-Peter Clausen | abe9937 | 2013-03-25 16:58:16 +0100 | [diff] [blame] | 171 | .trigger = snd_dmaengine_pcm_trigger, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 172 | .pointer = omap_pcm_pointer, |
| 173 | .mmap = omap_pcm_mmap, |
| 174 | }; |
| 175 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 176 | static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, |
| 177 | int stream) |
| 178 | { |
| 179 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; |
| 180 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
| 181 | size_t size = omap_pcm_hardware.buffer_bytes_max; |
| 182 | |
| 183 | buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 184 | buf->dev.dev = pcm->card->dev; |
| 185 | buf->private_data = NULL; |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 186 | buf->area = dma_alloc_wc(pcm->card->dev, size, &buf->addr, GFP_KERNEL); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 187 | if (!buf->area) |
| 188 | return -ENOMEM; |
| 189 | |
| 190 | buf->bytes = size; |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm) |
| 195 | { |
| 196 | struct snd_pcm_substream *substream; |
| 197 | struct snd_dma_buffer *buf; |
| 198 | int stream; |
| 199 | |
| 200 | for (stream = 0; stream < 2; stream++) { |
| 201 | substream = pcm->streams[stream].substream; |
| 202 | if (!substream) |
| 203 | continue; |
| 204 | |
| 205 | buf = &substream->dma_buffer; |
| 206 | if (!buf->area) |
| 207 | continue; |
| 208 | |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 209 | dma_free_wc(pcm->card->dev, buf->bytes, buf->area, buf->addr); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 210 | buf->area = NULL; |
| 211 | } |
| 212 | } |
| 213 | |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 214 | static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 215 | { |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 216 | struct snd_card *card = rtd->card->snd_card; |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 217 | struct snd_pcm *pcm = rtd->pcm; |
Russell King | c9bd5e6 | 2013-06-27 12:53:37 +0100 | [diff] [blame] | 218 | int ret; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 219 | |
Peter Ujfalusi | d51199a | 2015-03-03 13:38:14 +0200 | [diff] [blame] | 220 | ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32)); |
Russell King | c9bd5e6 | 2013-06-27 12:53:37 +0100 | [diff] [blame] | 221 | if (ret) |
| 222 | return ret; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 223 | |
Joachim Eastwood | 25e9e75 | 2012-01-01 01:58:44 +0100 | [diff] [blame] | 224 | if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 225 | ret = omap_pcm_preallocate_dma_buffer(pcm, |
| 226 | SNDRV_PCM_STREAM_PLAYBACK); |
| 227 | if (ret) |
| 228 | goto out; |
| 229 | } |
| 230 | |
Joachim Eastwood | 25e9e75 | 2012-01-01 01:58:44 +0100 | [diff] [blame] | 231 | if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 232 | ret = omap_pcm_preallocate_dma_buffer(pcm, |
| 233 | SNDRV_PCM_STREAM_CAPTURE); |
| 234 | if (ret) |
| 235 | goto out; |
| 236 | } |
| 237 | |
| 238 | out: |
Oleg Matcovschi | fad9365 | 2012-04-24 19:02:02 -0700 | [diff] [blame] | 239 | /* free preallocated buffers in case of error */ |
| 240 | if (ret) |
| 241 | omap_pcm_free_dma_buffers(pcm); |
| 242 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 243 | return ret; |
| 244 | } |
| 245 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 246 | static struct snd_soc_platform_driver omap_soc_platform = { |
| 247 | .ops = &omap_pcm_ops, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 248 | .pcm_new = omap_pcm_new, |
| 249 | .pcm_free = omap_pcm_free_dma_buffers, |
| 250 | }; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 251 | |
Peter Ujfalusi | fe7b586 | 2014-04-16 15:46:12 +0300 | [diff] [blame] | 252 | int omap_pcm_platform_register(struct device *dev) |
| 253 | { |
Jyri Sarha | 6742e15 | 2015-03-03 13:28:53 +0200 | [diff] [blame] | 254 | omap_pcm_limit_supported_formats(); |
Peter Ujfalusi | fe7b586 | 2014-04-16 15:46:12 +0300 | [diff] [blame] | 255 | return devm_snd_soc_register_platform(dev, &omap_soc_platform); |
| 256 | } |
| 257 | EXPORT_SYMBOL_GPL(omap_pcm_platform_register); |
| 258 | |
Jarkko Nikula | 7ec41ee | 2011-08-11 15:44:57 +0300 | [diff] [blame] | 259 | MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>"); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 260 | MODULE_DESCRIPTION("OMAP PCM DMA module"); |
| 261 | MODULE_LICENSE("GPL"); |