Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 1 | /* |
Jassi Brar | 4b640cf | 2010-11-22 15:35:57 +0900 | [diff] [blame] | 2 | * dma.c -- ALSA Soc Audio Layer |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 3 | * |
| 4 | * (c) 2006 Wolfson Microelectronics PLC. |
| 5 | * Graeme Gregory graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com |
| 6 | * |
Ben Dooks | c8efef1 | 2009-02-28 17:09:57 +0000 | [diff] [blame] | 7 | * Copyright 2004-2005 Simtec Electronics |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 8 | * http://armlinux.simtec.co.uk/ |
| 9 | * Ben Dooks <ben@simtec.co.uk> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License as published by the |
| 13 | * Free Software Foundation; either version 2 of the License, or (at your |
| 14 | * option) any later version. |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | #include <linux/dma-mapping.h> |
| 19 | |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 20 | #include <sound/soc.h> |
Seungwhan Youn | 0378b6a | 2011-01-11 07:26:06 +0900 | [diff] [blame] | 21 | #include <sound/pcm_params.h> |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 22 | |
| 23 | #include <asm/dma.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 24 | #include <mach/hardware.h> |
| 25 | #include <mach/dma.h> |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 26 | |
Jassi Brar | 4b640cf | 2010-11-22 15:35:57 +0900 | [diff] [blame] | 27 | #include "dma.h" |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 28 | |
Seungwhan Youn | f7f7418 | 2011-01-07 13:51:26 +0900 | [diff] [blame] | 29 | #define ST_RUNNING (1<<0) |
| 30 | #define ST_OPENED (1<<1) |
| 31 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 32 | static const struct snd_pcm_hardware dma_hardware = { |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 33 | .info = SNDRV_PCM_INFO_INTERLEAVED | |
| 34 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 35 | SNDRV_PCM_INFO_MMAP | |
Graeme Gregory | 96d90e1 | 2008-01-10 14:44:24 +0100 | [diff] [blame] | 36 | SNDRV_PCM_INFO_MMAP_VALID | |
| 37 | SNDRV_PCM_INFO_PAUSE | |
| 38 | SNDRV_PCM_INFO_RESUME, |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 39 | .formats = SNDRV_PCM_FMTBIT_S16_LE | |
| 40 | SNDRV_PCM_FMTBIT_U16_LE | |
| 41 | SNDRV_PCM_FMTBIT_U8 | |
| 42 | SNDRV_PCM_FMTBIT_S8, |
| 43 | .channels_min = 2, |
| 44 | .channels_max = 2, |
| 45 | .buffer_bytes_max = 128*1024, |
| 46 | .period_bytes_min = PAGE_SIZE, |
| 47 | .period_bytes_max = PAGE_SIZE*2, |
| 48 | .periods_min = 2, |
| 49 | .periods_max = 128, |
| 50 | .fifo_size = 32, |
| 51 | }; |
| 52 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 53 | struct runtime_data { |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 54 | spinlock_t lock; |
| 55 | int state; |
| 56 | unsigned int dma_loaded; |
| 57 | unsigned int dma_limit; |
| 58 | unsigned int dma_period; |
| 59 | dma_addr_t dma_start; |
| 60 | dma_addr_t dma_pos; |
| 61 | dma_addr_t dma_end; |
Jassi Brar | faa3177 | 2009-11-17 16:53:23 +0900 | [diff] [blame] | 62 | struct s3c_dma_params *params; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 65 | /* dma_enqueue |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 66 | * |
| 67 | * place a dma buffer onto the queue for the dma system |
| 68 | * to handle. |
| 69 | */ |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 70 | static void dma_enqueue(struct snd_pcm_substream *substream) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 71 | { |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 72 | struct runtime_data *prtd = substream->runtime->private_data; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 73 | dma_addr_t pos = prtd->dma_pos; |
Ben Dooks | e3d8024 | 2009-09-22 16:48:56 +0100 | [diff] [blame] | 74 | unsigned int limit; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 75 | int ret; |
| 76 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 77 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 78 | |
Jassi Brar | d3ff5a3 | 2009-11-17 16:53:31 +0900 | [diff] [blame] | 79 | if (s3c_dma_has_circular()) |
Ben Dooks | e3d8024 | 2009-09-22 16:48:56 +0100 | [diff] [blame] | 80 | limit = (prtd->dma_end - prtd->dma_start) / prtd->dma_period; |
Jassi Brar | d3ff5a3 | 2009-11-17 16:53:31 +0900 | [diff] [blame] | 81 | else |
Ben Dooks | e3d8024 | 2009-09-22 16:48:56 +0100 | [diff] [blame] | 82 | limit = prtd->dma_limit; |
| 83 | |
Jassi Brar | d3ff5a3 | 2009-11-17 16:53:31 +0900 | [diff] [blame] | 84 | pr_debug("%s: loaded %d, limit %d\n", |
| 85 | __func__, prtd->dma_loaded, limit); |
Ben Dooks | e3d8024 | 2009-09-22 16:48:56 +0100 | [diff] [blame] | 86 | |
| 87 | while (prtd->dma_loaded < limit) { |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 88 | unsigned long len = prtd->dma_period; |
| 89 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 90 | pr_debug("dma_loaded: %d\n", prtd->dma_loaded); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 91 | |
| 92 | if ((pos + len) > prtd->dma_end) { |
| 93 | len = prtd->dma_end - pos; |
Joe Perches | 147fcf1 | 2010-09-11 22:10:59 -0700 | [diff] [blame] | 94 | pr_debug("%s: corrected dma len %ld\n", __func__, len); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Mark Brown | 5111c07 | 2008-04-30 17:19:32 +0200 | [diff] [blame] | 97 | ret = s3c2410_dma_enqueue(prtd->params->channel, |
Graeme Gregory | 7f1bc26 | 2007-04-17 12:35:18 +0200 | [diff] [blame] | 98 | substream, pos, len); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 99 | |
| 100 | if (ret == 0) { |
| 101 | prtd->dma_loaded++; |
| 102 | pos += prtd->dma_period; |
| 103 | if (pos >= prtd->dma_end) |
| 104 | pos = prtd->dma_start; |
| 105 | } else |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | prtd->dma_pos = pos; |
| 110 | } |
| 111 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 112 | static void audio_buffdone(struct s3c2410_dma_chan *channel, |
Graeme Gregory | 7f1bc26 | 2007-04-17 12:35:18 +0200 | [diff] [blame] | 113 | void *dev_id, int size, |
| 114 | enum s3c2410_dma_buffresult result) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 115 | { |
| 116 | struct snd_pcm_substream *substream = dev_id; |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 117 | struct runtime_data *prtd; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 118 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 119 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 120 | |
| 121 | if (result == S3C2410_RES_ABORT || result == S3C2410_RES_ERR) |
| 122 | return; |
| 123 | |
Graeme Gregory | 7f1bc26 | 2007-04-17 12:35:18 +0200 | [diff] [blame] | 124 | prtd = substream->runtime->private_data; |
Mark Brown | 5111c07 | 2008-04-30 17:19:32 +0200 | [diff] [blame] | 125 | |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 126 | if (substream) |
| 127 | snd_pcm_period_elapsed(substream); |
| 128 | |
| 129 | spin_lock(&prtd->lock); |
Ben Dooks | e3d8024 | 2009-09-22 16:48:56 +0100 | [diff] [blame] | 130 | if (prtd->state & ST_RUNNING && !s3c_dma_has_circular()) { |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 131 | prtd->dma_loaded--; |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 132 | dma_enqueue(substream); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | spin_unlock(&prtd->lock); |
| 136 | } |
| 137 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 138 | static int dma_hw_params(struct snd_pcm_substream *substream, |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 139 | struct snd_pcm_hw_params *params) |
| 140 | { |
| 141 | struct snd_pcm_runtime *runtime = substream->runtime; |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 142 | struct runtime_data *prtd = runtime->private_data; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 143 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 144 | unsigned long totbytes = params_buffer_bytes(params); |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 145 | struct s3c_dma_params *dma = |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 146 | snd_soc_dai_get_dma_data(rtd->cpu_dai, substream); |
Mark Brown | 5111c07 | 2008-04-30 17:19:32 +0200 | [diff] [blame] | 147 | int ret = 0; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 148 | |
Daniel Mack | 5f712b2 | 2010-03-22 10:11:15 +0100 | [diff] [blame] | 149 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 150 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 151 | |
| 152 | /* return if this is a bufferless transfer e.g. |
| 153 | * codec <--> BT codec or GSM modem -- lg FIXME */ |
| 154 | if (!dma) |
| 155 | return 0; |
| 156 | |
Harald Welte | 646ab16 | 2007-07-24 12:49:39 +0200 | [diff] [blame] | 157 | /* this may get called several times by oss emulation |
| 158 | * with different params -HW */ |
| 159 | if (prtd->params == NULL) { |
| 160 | /* prepare DMA */ |
| 161 | prtd->params = dma; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 162 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 163 | pr_debug("params %p, client %p, channel %d\n", prtd->params, |
Harald Welte | 646ab16 | 2007-07-24 12:49:39 +0200 | [diff] [blame] | 164 | prtd->params->client, prtd->params->channel); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 165 | |
Harald Welte | 646ab16 | 2007-07-24 12:49:39 +0200 | [diff] [blame] | 166 | ret = s3c2410_dma_request(prtd->params->channel, |
| 167 | prtd->params->client, NULL); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 168 | |
Davide Rizzo | d642617 | 2008-05-05 14:56:07 +0200 | [diff] [blame] | 169 | if (ret < 0) { |
Mark Brown | b52a519 | 2009-03-06 18:13:43 +0000 | [diff] [blame] | 170 | printk(KERN_ERR "failed to get dma channel\n"); |
Harald Welte | 646ab16 | 2007-07-24 12:49:39 +0200 | [diff] [blame] | 171 | return ret; |
| 172 | } |
Ben Dooks | e3d8024 | 2009-09-22 16:48:56 +0100 | [diff] [blame] | 173 | |
| 174 | /* use the circular buffering if we have it available. */ |
| 175 | if (s3c_dma_has_circular()) |
| 176 | s3c2410_dma_setflags(prtd->params->channel, |
| 177 | S3C2410_DMAF_CIRCULAR); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 180 | s3c2410_dma_set_buffdone_fn(prtd->params->channel, |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 181 | audio_buffdone); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 182 | |
| 183 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 184 | |
| 185 | runtime->dma_bytes = totbytes; |
| 186 | |
| 187 | spin_lock_irq(&prtd->lock); |
| 188 | prtd->dma_loaded = 0; |
| 189 | prtd->dma_limit = runtime->hw.periods_min; |
| 190 | prtd->dma_period = params_period_bytes(params); |
| 191 | prtd->dma_start = runtime->dma_addr; |
| 192 | prtd->dma_pos = prtd->dma_start; |
| 193 | prtd->dma_end = prtd->dma_start + totbytes; |
| 194 | spin_unlock_irq(&prtd->lock); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 199 | static int dma_hw_free(struct snd_pcm_substream *substream) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 200 | { |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 201 | struct runtime_data *prtd = substream->runtime->private_data; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 202 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 203 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 204 | |
| 205 | /* TODO - do we need to ensure DMA flushed */ |
| 206 | snd_pcm_set_runtime_buffer(substream, NULL); |
| 207 | |
Graeme Gregory | 7f1bc26 | 2007-04-17 12:35:18 +0200 | [diff] [blame] | 208 | if (prtd->params) { |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 209 | s3c2410_dma_free(prtd->params->channel, prtd->params->client); |
| 210 | prtd->params = NULL; |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 216 | static int dma_prepare(struct snd_pcm_substream *substream) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 217 | { |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 218 | struct runtime_data *prtd = substream->runtime->private_data; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 219 | int ret = 0; |
| 220 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 221 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 222 | |
| 223 | /* return if this is a bufferless transfer e.g. |
| 224 | * codec <--> BT codec or GSM modem -- lg FIXME */ |
| 225 | if (!prtd->params) |
Mark Brown | 5111c07 | 2008-04-30 17:19:32 +0200 | [diff] [blame] | 226 | return 0; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 227 | |
Graeme Gregory | 96d90e1 | 2008-01-10 14:44:24 +0100 | [diff] [blame] | 228 | /* channel needs configuring for mem=>device, increment memory addr, |
| 229 | * sync to pclk, half-word transfers to the IIS-FIFO. */ |
| 230 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 231 | s3c2410_dma_devconfig(prtd->params->channel, |
Ben Dooks | 8970ef4 | 2009-03-19 15:02:34 +0000 | [diff] [blame] | 232 | S3C2410_DMASRC_MEM, |
| 233 | prtd->params->dma_addr); |
Graeme Gregory | 96d90e1 | 2008-01-10 14:44:24 +0100 | [diff] [blame] | 234 | } else { |
Graeme Gregory | 96d90e1 | 2008-01-10 14:44:24 +0100 | [diff] [blame] | 235 | s3c2410_dma_devconfig(prtd->params->channel, |
Ben Dooks | 8970ef4 | 2009-03-19 15:02:34 +0000 | [diff] [blame] | 236 | S3C2410_DMASRC_HW, |
| 237 | prtd->params->dma_addr); |
Graeme Gregory | 96d90e1 | 2008-01-10 14:44:24 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Ben Dooks | 8970ef4 | 2009-03-19 15:02:34 +0000 | [diff] [blame] | 240 | s3c2410_dma_config(prtd->params->channel, |
| 241 | prtd->params->dma_size); |
| 242 | |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 243 | /* flush the DMA channel */ |
| 244 | s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_FLUSH); |
| 245 | prtd->dma_loaded = 0; |
| 246 | prtd->dma_pos = prtd->dma_start; |
| 247 | |
| 248 | /* enqueue dma buffers */ |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 249 | dma_enqueue(substream); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 250 | |
| 251 | return ret; |
| 252 | } |
| 253 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 254 | static int dma_trigger(struct snd_pcm_substream *substream, int cmd) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 255 | { |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 256 | struct runtime_data *prtd = substream->runtime->private_data; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 257 | int ret = 0; |
| 258 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 259 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 260 | |
| 261 | spin_lock(&prtd->lock); |
| 262 | |
| 263 | switch (cmd) { |
| 264 | case SNDRV_PCM_TRIGGER_START: |
| 265 | case SNDRV_PCM_TRIGGER_RESUME: |
| 266 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 267 | prtd->state |= ST_RUNNING; |
| 268 | s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_START); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 269 | break; |
| 270 | |
| 271 | case SNDRV_PCM_TRIGGER_STOP: |
| 272 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 273 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 274 | prtd->state &= ~ST_RUNNING; |
| 275 | s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STOP); |
| 276 | break; |
| 277 | |
| 278 | default: |
| 279 | ret = -EINVAL; |
| 280 | break; |
| 281 | } |
| 282 | |
| 283 | spin_unlock(&prtd->lock); |
| 284 | |
| 285 | return ret; |
| 286 | } |
| 287 | |
Mark Brown | 5111c07 | 2008-04-30 17:19:32 +0200 | [diff] [blame] | 288 | static snd_pcm_uframes_t |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 289 | dma_pointer(struct snd_pcm_substream *substream) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 290 | { |
| 291 | struct snd_pcm_runtime *runtime = substream->runtime; |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 292 | struct runtime_data *prtd = runtime->private_data; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 293 | unsigned long res; |
| 294 | dma_addr_t src, dst; |
| 295 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 296 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 297 | |
| 298 | spin_lock(&prtd->lock); |
| 299 | s3c2410_dma_getposition(prtd->params->channel, &src, &dst); |
| 300 | |
| 301 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 302 | res = dst - prtd->dma_start; |
| 303 | else |
| 304 | res = src - prtd->dma_start; |
| 305 | |
| 306 | spin_unlock(&prtd->lock); |
| 307 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 308 | pr_debug("Pointer %x %x\n", src, dst); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 309 | |
| 310 | /* we seem to be getting the odd error from the pcm library due |
| 311 | * to out-of-bounds pointers. this is maybe due to the dma engine |
| 312 | * not having loaded the new values for the channel before being |
Justin P. Mattock | ceade6c | 2011-02-24 22:16:37 -0800 | [diff] [blame] | 313 | * called... (todo - fix ) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 314 | */ |
| 315 | |
| 316 | if (res >= snd_pcm_lib_buffer_bytes(substream)) { |
| 317 | if (res == snd_pcm_lib_buffer_bytes(substream)) |
| 318 | res = 0; |
| 319 | } |
| 320 | |
| 321 | return bytes_to_frames(substream->runtime, res); |
| 322 | } |
| 323 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 324 | static int dma_open(struct snd_pcm_substream *substream) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 325 | { |
| 326 | struct snd_pcm_runtime *runtime = substream->runtime; |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 327 | struct runtime_data *prtd; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 328 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 329 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 330 | |
Shine Liu | f61c890 | 2009-08-20 23:02:23 +0800 | [diff] [blame] | 331 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 332 | snd_soc_set_runtime_hwparams(substream, &dma_hardware); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 333 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 334 | prtd = kzalloc(sizeof(struct runtime_data), GFP_KERNEL); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 335 | if (prtd == NULL) |
| 336 | return -ENOMEM; |
| 337 | |
Zoltan Devai | c72816b | 2007-05-22 16:17:05 +0200 | [diff] [blame] | 338 | spin_lock_init(&prtd->lock); |
| 339 | |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 340 | runtime->private_data = prtd; |
| 341 | return 0; |
| 342 | } |
| 343 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 344 | static int dma_close(struct snd_pcm_substream *substream) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 345 | { |
| 346 | struct snd_pcm_runtime *runtime = substream->runtime; |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 347 | struct runtime_data *prtd = runtime->private_data; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 348 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 349 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 350 | |
Mark Brown | 5111c07 | 2008-04-30 17:19:32 +0200 | [diff] [blame] | 351 | if (!prtd) |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 352 | pr_debug("dma_close called with prtd == NULL\n"); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 353 | |
Mark Brown | 5111c07 | 2008-04-30 17:19:32 +0200 | [diff] [blame] | 354 | kfree(prtd); |
| 355 | |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 359 | static int dma_mmap(struct snd_pcm_substream *substream, |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 360 | struct vm_area_struct *vma) |
| 361 | { |
| 362 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 363 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 364 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 365 | |
| 366 | return dma_mmap_writecombine(substream->pcm->card->dev, vma, |
Mark Brown | 5111c07 | 2008-04-30 17:19:32 +0200 | [diff] [blame] | 367 | runtime->dma_area, |
| 368 | runtime->dma_addr, |
| 369 | runtime->dma_bytes); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 370 | } |
| 371 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 372 | static struct snd_pcm_ops dma_ops = { |
| 373 | .open = dma_open, |
| 374 | .close = dma_close, |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 375 | .ioctl = snd_pcm_lib_ioctl, |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 376 | .hw_params = dma_hw_params, |
| 377 | .hw_free = dma_hw_free, |
| 378 | .prepare = dma_prepare, |
| 379 | .trigger = dma_trigger, |
| 380 | .pointer = dma_pointer, |
| 381 | .mmap = dma_mmap, |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 382 | }; |
| 383 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 384 | static int preallocate_dma_buffer(struct snd_pcm *pcm, int stream) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 385 | { |
| 386 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; |
| 387 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 388 | size_t size = dma_hardware.buffer_bytes_max; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 389 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 390 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 391 | |
| 392 | buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 393 | buf->dev.dev = pcm->card->dev; |
| 394 | buf->private_data = NULL; |
| 395 | buf->area = dma_alloc_writecombine(pcm->card->dev, size, |
| 396 | &buf->addr, GFP_KERNEL); |
| 397 | if (!buf->area) |
| 398 | return -ENOMEM; |
| 399 | buf->bytes = size; |
| 400 | return 0; |
| 401 | } |
| 402 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 403 | static void dma_free_dma_buffers(struct snd_pcm *pcm) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 404 | { |
| 405 | struct snd_pcm_substream *substream; |
| 406 | struct snd_dma_buffer *buf; |
| 407 | int stream; |
| 408 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 409 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 410 | |
| 411 | for (stream = 0; stream < 2; stream++) { |
| 412 | substream = pcm->streams[stream].substream; |
| 413 | if (!substream) |
| 414 | continue; |
| 415 | |
| 416 | buf = &substream->dma_buffer; |
| 417 | if (!buf->area) |
| 418 | continue; |
| 419 | |
| 420 | dma_free_writecombine(pcm->card->dev, buf->bytes, |
| 421 | buf->area, buf->addr); |
| 422 | buf->area = NULL; |
| 423 | } |
| 424 | } |
| 425 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 426 | static u64 dma_mask = DMA_BIT_MASK(32); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 427 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 428 | static int dma_new(struct snd_card *card, |
Liam Girdwood | 1992a6f | 2008-07-07 16:08:24 +0100 | [diff] [blame] | 429 | struct snd_soc_dai *dai, struct snd_pcm *pcm) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 430 | { |
| 431 | int ret = 0; |
| 432 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 433 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 434 | |
| 435 | if (!card->dev->dma_mask) |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 436 | card->dev->dma_mask = &dma_mask; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 437 | if (!card->dev->coherent_dma_mask) |
| 438 | card->dev->coherent_dma_mask = 0xffffffff; |
| 439 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 440 | if (dai->driver->playback.channels_min) { |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 441 | ret = preallocate_dma_buffer(pcm, |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 442 | SNDRV_PCM_STREAM_PLAYBACK); |
| 443 | if (ret) |
| 444 | goto out; |
| 445 | } |
| 446 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 447 | if (dai->driver->capture.channels_min) { |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 448 | ret = preallocate_dma_buffer(pcm, |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 449 | SNDRV_PCM_STREAM_CAPTURE); |
| 450 | if (ret) |
| 451 | goto out; |
| 452 | } |
Jassi Brar | 4b640cf | 2010-11-22 15:35:57 +0900 | [diff] [blame] | 453 | out: |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 454 | return ret; |
| 455 | } |
| 456 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 457 | static struct snd_soc_platform_driver samsung_asoc_platform = { |
| 458 | .ops = &dma_ops, |
| 459 | .pcm_new = dma_new, |
| 460 | .pcm_free = dma_free_dma_buffers, |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 461 | }; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 462 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 463 | static int __devinit samsung_asoc_platform_probe(struct platform_device *pdev) |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 464 | { |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 465 | return snd_soc_register_platform(&pdev->dev, &samsung_asoc_platform); |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 466 | } |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 467 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 468 | static int __devexit samsung_asoc_platform_remove(struct platform_device *pdev) |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 469 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 470 | snd_soc_unregister_platform(&pdev->dev); |
| 471 | return 0; |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 472 | } |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 473 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 474 | static struct platform_driver asoc_dma_driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 475 | .driver = { |
Jassi Brar | 58bb407 | 2010-11-22 15:35:50 +0900 | [diff] [blame] | 476 | .name = "samsung-audio", |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 477 | .owner = THIS_MODULE, |
| 478 | }, |
| 479 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 480 | .probe = samsung_asoc_platform_probe, |
| 481 | .remove = __devexit_p(samsung_asoc_platform_remove), |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 482 | }; |
| 483 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 484 | static int __init samsung_asoc_init(void) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 485 | { |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 486 | return platform_driver_register(&asoc_dma_driver); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 487 | } |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 488 | module_init(samsung_asoc_init); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 489 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 490 | static void __exit samsung_asoc_exit(void) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 491 | { |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 492 | platform_driver_unregister(&asoc_dma_driver); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 493 | } |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 494 | module_exit(samsung_asoc_exit); |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 495 | |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 496 | MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>"); |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 497 | MODULE_DESCRIPTION("Samsung ASoC DMA Driver"); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 498 | MODULE_LICENSE("GPL"); |
Jassi Brar | 58bb407 | 2010-11-22 15:35:50 +0900 | [diff] [blame] | 499 | MODULE_ALIAS("platform:samsung-audio"); |