Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 1 | /* |
| 2 | * linux/sound/arm/ep93xx-pcm.c - EP93xx ALSA PCM interface |
| 3 | * |
| 4 | * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org> |
| 5 | * Copyright (C) 2006 Applied Data Systems |
| 6 | * |
| 7 | * Rewritten for the SoC audio subsystem (Based on PXA2xx code): |
Ryan Mallon | 1c5454e | 2011-06-15 14:45:36 +1000 | [diff] [blame] | 8 | * Copyright (c) 2008 Ryan Mallon |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/slab.h> |
Mika Westerberg | 51e2cc0 | 2011-05-29 13:10:04 +0300 | [diff] [blame] | 19 | #include <linux/dmaengine.h> |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 20 | #include <linux/dma-mapping.h> |
| 21 | |
| 22 | #include <sound/core.h> |
| 23 | #include <sound/pcm.h> |
| 24 | #include <sound/pcm_params.h> |
| 25 | #include <sound/soc.h> |
Lars-Peter Clausen | d7a42e1 | 2012-03-05 14:02:15 +0100 | [diff] [blame] | 26 | #include <sound/dmaengine_pcm.h> |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 27 | |
Arnd Bergmann | a3b2924 | 2012-08-24 15:12:11 +0200 | [diff] [blame] | 28 | #include <linux/platform_data/dma-ep93xx.h> |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 29 | #include <mach/hardware.h> |
| 30 | #include <mach/ep93xx-regs.h> |
| 31 | |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 32 | static const struct snd_pcm_hardware ep93xx_pcm_hardware = { |
| 33 | .info = (SNDRV_PCM_INFO_MMAP | |
| 34 | SNDRV_PCM_INFO_MMAP_VALID | |
| 35 | SNDRV_PCM_INFO_INTERLEAVED | |
| 36 | SNDRV_PCM_INFO_BLOCK_TRANSFER), |
| 37 | |
Alexander Sverdlin | 4cfeb69 | 2011-03-07 20:30:12 +0300 | [diff] [blame] | 38 | .rates = SNDRV_PCM_RATE_8000_192000, |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 39 | .rate_min = SNDRV_PCM_RATE_8000, |
Alexander Sverdlin | 4cfeb69 | 2011-03-07 20:30:12 +0300 | [diff] [blame] | 40 | .rate_max = SNDRV_PCM_RATE_192000, |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 41 | |
| 42 | .formats = (SNDRV_PCM_FMTBIT_S16_LE | |
| 43 | SNDRV_PCM_FMTBIT_S24_LE | |
| 44 | SNDRV_PCM_FMTBIT_S32_LE), |
| 45 | |
| 46 | .buffer_bytes_max = 131072, |
| 47 | .period_bytes_min = 32, |
| 48 | .period_bytes_max = 32768, |
| 49 | .periods_min = 1, |
| 50 | .periods_max = 32, |
| 51 | .fifo_size = 32, |
| 52 | }; |
| 53 | |
Mika Westerberg | 51e2cc0 | 2011-05-29 13:10:04 +0300 | [diff] [blame] | 54 | static bool ep93xx_pcm_dma_filter(struct dma_chan *chan, void *filter_param) |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 55 | { |
Mika Westerberg | 51e2cc0 | 2011-05-29 13:10:04 +0300 | [diff] [blame] | 56 | struct ep93xx_dma_data *data = filter_param; |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 57 | |
Mika Westerberg | 51e2cc0 | 2011-05-29 13:10:04 +0300 | [diff] [blame] | 58 | if (data->direction == ep93xx_dma_chan_direction(chan)) { |
| 59 | chan->private = data; |
| 60 | return true; |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 61 | } |
Mika Westerberg | 51e2cc0 | 2011-05-29 13:10:04 +0300 | [diff] [blame] | 62 | |
| 63 | return false; |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static int ep93xx_pcm_open(struct snd_pcm_substream *substream) |
| 67 | { |
Lars-Peter Clausen | d7a42e1 | 2012-03-05 14:02:15 +0100 | [diff] [blame] | 68 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 69 | |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 70 | snd_soc_set_runtime_hwparams(substream, &ep93xx_pcm_hardware); |
| 71 | |
Lars-Peter Clausen | 453807f | 2013-03-22 14:12:10 +0100 | [diff] [blame^] | 72 | return snd_dmaengine_pcm_open(substream, ep93xx_pcm_dma_filter, |
| 73 | snd_soc_dai_get_dma_data(rtd->cpu_dai, substream)); |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static int ep93xx_pcm_hw_params(struct snd_pcm_substream *substream, |
| 77 | struct snd_pcm_hw_params *params) |
| 78 | { |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 79 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 80 | |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int ep93xx_pcm_hw_free(struct snd_pcm_substream *substream) |
| 85 | { |
| 86 | snd_pcm_set_runtime_buffer(substream, NULL); |
| 87 | return 0; |
| 88 | } |
| 89 | |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 90 | static int ep93xx_pcm_mmap(struct snd_pcm_substream *substream, |
| 91 | struct vm_area_struct *vma) |
| 92 | { |
| 93 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 94 | |
| 95 | return dma_mmap_writecombine(substream->pcm->card->dev, vma, |
| 96 | runtime->dma_area, |
| 97 | runtime->dma_addr, |
| 98 | runtime->dma_bytes); |
| 99 | } |
| 100 | |
| 101 | static struct snd_pcm_ops ep93xx_pcm_ops = { |
| 102 | .open = ep93xx_pcm_open, |
Lars-Peter Clausen | 453807f | 2013-03-22 14:12:10 +0100 | [diff] [blame^] | 103 | .close = snd_dmaengine_pcm_close, |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 104 | .ioctl = snd_pcm_lib_ioctl, |
| 105 | .hw_params = ep93xx_pcm_hw_params, |
| 106 | .hw_free = ep93xx_pcm_hw_free, |
Lars-Peter Clausen | d7a42e1 | 2012-03-05 14:02:15 +0100 | [diff] [blame] | 107 | .trigger = snd_dmaengine_pcm_trigger, |
Lars-Peter Clausen | 9883ab2 | 2012-06-11 20:11:41 +0200 | [diff] [blame] | 108 | .pointer = snd_dmaengine_pcm_pointer_no_residue, |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 109 | .mmap = ep93xx_pcm_mmap, |
| 110 | }; |
| 111 | |
| 112 | static int ep93xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream) |
| 113 | { |
| 114 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; |
| 115 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
| 116 | size_t size = ep93xx_pcm_hardware.buffer_bytes_max; |
| 117 | |
| 118 | buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 119 | buf->dev.dev = pcm->card->dev; |
| 120 | buf->private_data = NULL; |
| 121 | buf->area = dma_alloc_writecombine(pcm->card->dev, size, |
| 122 | &buf->addr, GFP_KERNEL); |
| 123 | buf->bytes = size; |
| 124 | |
| 125 | return (buf->area == NULL) ? -ENOMEM : 0; |
| 126 | } |
| 127 | |
| 128 | static void ep93xx_pcm_free_dma_buffers(struct snd_pcm *pcm) |
| 129 | { |
| 130 | struct snd_pcm_substream *substream; |
| 131 | struct snd_dma_buffer *buf; |
| 132 | int stream; |
| 133 | |
| 134 | for (stream = 0; stream < 2; stream++) { |
| 135 | substream = pcm->streams[stream].substream; |
| 136 | if (!substream) |
| 137 | continue; |
| 138 | |
| 139 | buf = &substream->dma_buffer; |
| 140 | if (!buf->area) |
| 141 | continue; |
| 142 | |
| 143 | dma_free_writecombine(pcm->card->dev, buf->bytes, buf->area, |
| 144 | buf->addr); |
| 145 | buf->area = NULL; |
| 146 | } |
| 147 | } |
| 148 | |
Joachim Eastwood | 350e16d | 2012-01-01 02:43:03 +0100 | [diff] [blame] | 149 | static u64 ep93xx_pcm_dmamask = DMA_BIT_MASK(32); |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 150 | |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 151 | static int ep93xx_pcm_new(struct snd_soc_pcm_runtime *rtd) |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 152 | { |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 153 | struct snd_card *card = rtd->card->snd_card; |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 154 | struct snd_pcm *pcm = rtd->pcm; |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 155 | int ret = 0; |
| 156 | |
| 157 | if (!card->dev->dma_mask) |
| 158 | card->dev->dma_mask = &ep93xx_pcm_dmamask; |
| 159 | if (!card->dev->coherent_dma_mask) |
Joachim Eastwood | 350e16d | 2012-01-01 02:43:03 +0100 | [diff] [blame] | 160 | card->dev->coherent_dma_mask = DMA_BIT_MASK(32); |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 161 | |
Joachim Eastwood | 25e9e75 | 2012-01-01 01:58:44 +0100 | [diff] [blame] | 162 | if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) { |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 163 | ret = ep93xx_pcm_preallocate_dma_buffer(pcm, |
| 164 | SNDRV_PCM_STREAM_PLAYBACK); |
| 165 | if (ret) |
| 166 | return ret; |
| 167 | } |
| 168 | |
Joachim Eastwood | 25e9e75 | 2012-01-01 01:58:44 +0100 | [diff] [blame] | 169 | if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 170 | ret = ep93xx_pcm_preallocate_dma_buffer(pcm, |
| 171 | SNDRV_PCM_STREAM_CAPTURE); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 179 | static struct snd_soc_platform_driver ep93xx_soc_platform = { |
| 180 | .ops = &ep93xx_pcm_ops, |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 181 | .pcm_new = &ep93xx_pcm_new, |
| 182 | .pcm_free = &ep93xx_pcm_free_dma_buffers, |
| 183 | }; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 184 | |
Bill Pemberton | 145e287 | 2012-12-07 09:26:23 -0500 | [diff] [blame] | 185 | static int ep93xx_soc_platform_probe(struct platform_device *pdev) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 186 | { |
| 187 | return snd_soc_register_platform(&pdev->dev, &ep93xx_soc_platform); |
| 188 | } |
| 189 | |
Bill Pemberton | 145e287 | 2012-12-07 09:26:23 -0500 | [diff] [blame] | 190 | static int ep93xx_soc_platform_remove(struct platform_device *pdev) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 191 | { |
| 192 | snd_soc_unregister_platform(&pdev->dev); |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static struct platform_driver ep93xx_pcm_driver = { |
| 197 | .driver = { |
| 198 | .name = "ep93xx-pcm-audio", |
| 199 | .owner = THIS_MODULE, |
| 200 | }, |
| 201 | |
| 202 | .probe = ep93xx_soc_platform_probe, |
Bill Pemberton | 145e287 | 2012-12-07 09:26:23 -0500 | [diff] [blame] | 203 | .remove = ep93xx_soc_platform_remove, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 204 | }; |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 205 | |
Axel Lin | ee18f63 | 2011-11-24 12:07:55 +0800 | [diff] [blame] | 206 | module_platform_driver(ep93xx_pcm_driver); |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 207 | |
Ryan Mallon | 1c5454e | 2011-06-15 14:45:36 +1000 | [diff] [blame] | 208 | MODULE_AUTHOR("Ryan Mallon"); |
Ryan Mallon | db5bf41 | 2010-06-04 17:11:24 +1200 | [diff] [blame] | 209 | MODULE_DESCRIPTION("EP93xx ALSA PCM interface"); |
| 210 | MODULE_LICENSE("GPL"); |
Mika Westerberg | 9306816 | 2011-09-11 12:28:49 +0300 | [diff] [blame] | 211 | MODULE_ALIAS("platform:ep93xx-pcm-audio"); |