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> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 19 | #include <linux/module.h> |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 20 | |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 21 | #include <sound/soc.h> |
Seungwhan Youn | 0378b6a | 2011-01-11 07:26:06 +0900 | [diff] [blame] | 22 | #include <sound/pcm_params.h> |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 23 | |
| 24 | #include <asm/dma.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 25 | #include <mach/hardware.h> |
| 26 | #include <mach/dma.h> |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 27 | |
Jassi Brar | 4b640cf | 2010-11-22 15:35:57 +0900 | [diff] [blame] | 28 | #include "dma.h" |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 29 | |
Seungwhan Youn | f7f7418 | 2011-01-07 13:51:26 +0900 | [diff] [blame] | 30 | #define ST_RUNNING (1<<0) |
| 31 | #define ST_OPENED (1<<1) |
| 32 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 33 | static const struct snd_pcm_hardware dma_hardware = { |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 34 | .info = SNDRV_PCM_INFO_INTERLEAVED | |
| 35 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 36 | SNDRV_PCM_INFO_MMAP | |
Dylan Reid | 57b2d688 | 2012-09-01 01:38:19 -0700 | [diff] [blame] | 37 | SNDRV_PCM_INFO_MMAP_VALID, |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 38 | .formats = SNDRV_PCM_FMTBIT_S16_LE | |
| 39 | SNDRV_PCM_FMTBIT_U16_LE | |
| 40 | SNDRV_PCM_FMTBIT_U8 | |
| 41 | SNDRV_PCM_FMTBIT_S8, |
| 42 | .channels_min = 2, |
| 43 | .channels_max = 2, |
| 44 | .buffer_bytes_max = 128*1024, |
| 45 | .period_bytes_min = PAGE_SIZE, |
| 46 | .period_bytes_max = PAGE_SIZE*2, |
| 47 | .periods_min = 2, |
| 48 | .periods_max = 128, |
| 49 | .fifo_size = 32, |
| 50 | }; |
| 51 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 52 | struct runtime_data { |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 53 | spinlock_t lock; |
| 54 | int state; |
| 55 | unsigned int dma_loaded; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 56 | unsigned int dma_period; |
| 57 | dma_addr_t dma_start; |
| 58 | dma_addr_t dma_pos; |
| 59 | dma_addr_t dma_end; |
Jassi Brar | faa3177 | 2009-11-17 16:53:23 +0900 | [diff] [blame] | 60 | struct s3c_dma_params *params; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 63 | static void audio_buffdone(void *data); |
| 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. |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 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; |
Boojin Kim | 21c4afe | 2012-06-19 13:27:07 +0900 | [diff] [blame] | 75 | struct samsung_dma_prep dma_info; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 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 | |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 79 | limit = (prtd->dma_end - prtd->dma_start) / prtd->dma_period; |
Ben Dooks | e3d8024 | 2009-09-22 16:48:56 +0100 | [diff] [blame] | 80 | |
Jassi Brar | d3ff5a3 | 2009-11-17 16:53:31 +0900 | [diff] [blame] | 81 | pr_debug("%s: loaded %d, limit %d\n", |
| 82 | __func__, prtd->dma_loaded, limit); |
Ben Dooks | e3d8024 | 2009-09-22 16:48:56 +0100 | [diff] [blame] | 83 | |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 84 | dma_info.cap = (samsung_dma_has_circular() ? DMA_CYCLIC : DMA_SLAVE); |
| 85 | dma_info.direction = |
| 86 | (substream->stream == SNDRV_PCM_STREAM_PLAYBACK |
Vinod Koul | 35e1658 | 2011-10-14 10:49:30 +0530 | [diff] [blame] | 87 | ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM); |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 88 | dma_info.fp = audio_buffdone; |
| 89 | dma_info.fp_param = substream; |
| 90 | dma_info.period = prtd->dma_period; |
| 91 | dma_info.len = prtd->dma_period*limit; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 92 | |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 93 | while (prtd->dma_loaded < limit) { |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 94 | pr_debug("dma_loaded: %d\n", prtd->dma_loaded); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 95 | |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 96 | if ((pos + dma_info.period) > prtd->dma_end) { |
| 97 | dma_info.period = prtd->dma_end - pos; |
| 98 | pr_debug("%s: corrected dma len %ld\n", |
| 99 | __func__, dma_info.period); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 102 | dma_info.buf = pos; |
| 103 | prtd->params->ops->prepare(prtd->params->ch, &dma_info); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 104 | |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 105 | prtd->dma_loaded++; |
| 106 | pos += prtd->dma_period; |
| 107 | if (pos >= prtd->dma_end) |
| 108 | pos = prtd->dma_start; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | prtd->dma_pos = pos; |
| 112 | } |
| 113 | |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 114 | static void audio_buffdone(void *data) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 115 | { |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 116 | struct snd_pcm_substream *substream = data; |
| 117 | struct runtime_data *prtd = substream->runtime->private_data; |
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 | |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 121 | if (prtd->state & ST_RUNNING) { |
| 122 | prtd->dma_pos += prtd->dma_period; |
| 123 | if (prtd->dma_pos >= prtd->dma_end) |
| 124 | prtd->dma_pos = prtd->dma_start; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 125 | |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 126 | if (substream) |
| 127 | snd_pcm_period_elapsed(substream); |
Mark Brown | 5111c07 | 2008-04-30 17:19:32 +0200 | [diff] [blame] | 128 | |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 129 | spin_lock(&prtd->lock); |
| 130 | if (!samsung_dma_has_circular()) { |
| 131 | prtd->dma_loaded--; |
| 132 | dma_enqueue(substream); |
| 133 | } |
| 134 | spin_unlock(&prtd->lock); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 135 | } |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 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); |
Boojin Kim | 21c4afe | 2012-06-19 13:27:07 +0900 | [diff] [blame] | 147 | struct samsung_dma_req req; |
| 148 | struct samsung_dma_config config; |
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 | |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 166 | prtd->params->ops = samsung_dma_get_ops(); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 167 | |
Boojin Kim | 21c4afe | 2012-06-19 13:27:07 +0900 | [diff] [blame] | 168 | req.cap = (samsung_dma_has_circular() ? |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 169 | DMA_CYCLIC : DMA_SLAVE); |
Boojin Kim | 21c4afe | 2012-06-19 13:27:07 +0900 | [diff] [blame] | 170 | req.client = prtd->params->client; |
| 171 | config.direction = |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 172 | (substream->stream == SNDRV_PCM_STREAM_PLAYBACK |
Vinod Koul | 35e1658 | 2011-10-14 10:49:30 +0530 | [diff] [blame] | 173 | ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM); |
Boojin Kim | 21c4afe | 2012-06-19 13:27:07 +0900 | [diff] [blame] | 174 | config.width = prtd->params->dma_size; |
| 175 | config.fifo = prtd->params->dma_addr; |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 176 | prtd->params->ch = prtd->params->ops->request( |
Boojin Kim | 21c4afe | 2012-06-19 13:27:07 +0900 | [diff] [blame] | 177 | prtd->params->channel, &req); |
| 178 | prtd->params->ops->config(prtd->params->ch, &config); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 181 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 182 | |
| 183 | runtime->dma_bytes = totbytes; |
| 184 | |
| 185 | spin_lock_irq(&prtd->lock); |
| 186 | prtd->dma_loaded = 0; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 187 | prtd->dma_period = params_period_bytes(params); |
| 188 | prtd->dma_start = runtime->dma_addr; |
| 189 | prtd->dma_pos = prtd->dma_start; |
| 190 | prtd->dma_end = prtd->dma_start + totbytes; |
| 191 | spin_unlock_irq(&prtd->lock); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 196 | static int dma_hw_free(struct snd_pcm_substream *substream) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 197 | { |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 198 | struct runtime_data *prtd = substream->runtime->private_data; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 199 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 200 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 201 | |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 202 | snd_pcm_set_runtime_buffer(substream, NULL); |
| 203 | |
Graeme Gregory | 7f1bc26 | 2007-04-17 12:35:18 +0200 | [diff] [blame] | 204 | if (prtd->params) { |
Mark Brown | 2ca9576 | 2011-09-27 07:29:11 +0900 | [diff] [blame] | 205 | prtd->params->ops->flush(prtd->params->ch); |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 206 | prtd->params->ops->release(prtd->params->ch, |
| 207 | prtd->params->client); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 208 | prtd->params = NULL; |
| 209 | } |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 214 | static int dma_prepare(struct snd_pcm_substream *substream) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 215 | { |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 216 | struct runtime_data *prtd = substream->runtime->private_data; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 217 | int ret = 0; |
| 218 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 219 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 220 | |
| 221 | /* return if this is a bufferless transfer e.g. |
| 222 | * codec <--> BT codec or GSM modem -- lg FIXME */ |
| 223 | if (!prtd->params) |
Mark Brown | 5111c07 | 2008-04-30 17:19:32 +0200 | [diff] [blame] | 224 | return 0; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 225 | |
| 226 | /* flush the DMA channel */ |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 227 | prtd->params->ops->flush(prtd->params->ch); |
| 228 | |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 229 | prtd->dma_loaded = 0; |
| 230 | prtd->dma_pos = prtd->dma_start; |
| 231 | |
| 232 | /* enqueue dma buffers */ |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 233 | dma_enqueue(substream); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 234 | |
| 235 | return ret; |
| 236 | } |
| 237 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 238 | static int dma_trigger(struct snd_pcm_substream *substream, int cmd) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 239 | { |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 240 | struct runtime_data *prtd = substream->runtime->private_data; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 241 | int ret = 0; |
| 242 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 243 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 244 | |
| 245 | spin_lock(&prtd->lock); |
| 246 | |
| 247 | switch (cmd) { |
| 248 | case SNDRV_PCM_TRIGGER_START: |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 249 | prtd->state |= ST_RUNNING; |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 250 | prtd->params->ops->trigger(prtd->params->ch); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 251 | break; |
| 252 | |
| 253 | case SNDRV_PCM_TRIGGER_STOP: |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 254 | prtd->state &= ~ST_RUNNING; |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 255 | prtd->params->ops->stop(prtd->params->ch); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 256 | break; |
| 257 | |
| 258 | default: |
| 259 | ret = -EINVAL; |
| 260 | break; |
| 261 | } |
| 262 | |
| 263 | spin_unlock(&prtd->lock); |
| 264 | |
| 265 | return ret; |
| 266 | } |
| 267 | |
Mark Brown | 5111c07 | 2008-04-30 17:19:32 +0200 | [diff] [blame] | 268 | static snd_pcm_uframes_t |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 269 | dma_pointer(struct snd_pcm_substream *substream) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 270 | { |
| 271 | struct snd_pcm_runtime *runtime = substream->runtime; |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 272 | struct runtime_data *prtd = runtime->private_data; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 273 | unsigned long res; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 274 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 275 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 276 | |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 277 | res = prtd->dma_pos - prtd->dma_start; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 278 | |
Boojin Kim | 344b4c4 | 2011-09-02 09:44:43 +0900 | [diff] [blame] | 279 | pr_debug("Pointer offset: %lu\n", res); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 280 | |
| 281 | /* we seem to be getting the odd error from the pcm library due |
| 282 | * to out-of-bounds pointers. this is maybe due to the dma engine |
| 283 | * not having loaded the new values for the channel before being |
Justin P. Mattock | ceade6c | 2011-02-24 22:16:37 -0800 | [diff] [blame] | 284 | * called... (todo - fix ) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 285 | */ |
| 286 | |
| 287 | if (res >= snd_pcm_lib_buffer_bytes(substream)) { |
| 288 | if (res == snd_pcm_lib_buffer_bytes(substream)) |
| 289 | res = 0; |
| 290 | } |
| 291 | |
| 292 | return bytes_to_frames(substream->runtime, res); |
| 293 | } |
| 294 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 295 | static int dma_open(struct snd_pcm_substream *substream) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 296 | { |
| 297 | struct snd_pcm_runtime *runtime = substream->runtime; |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 298 | struct runtime_data *prtd; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 299 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 300 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 301 | |
Shine Liu | f61c890 | 2009-08-20 23:02:23 +0800 | [diff] [blame] | 302 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 303 | snd_soc_set_runtime_hwparams(substream, &dma_hardware); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 304 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 305 | prtd = kzalloc(sizeof(struct runtime_data), GFP_KERNEL); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 306 | if (prtd == NULL) |
| 307 | return -ENOMEM; |
| 308 | |
Zoltan Devai | c72816b | 2007-05-22 16:17:05 +0200 | [diff] [blame] | 309 | spin_lock_init(&prtd->lock); |
| 310 | |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 311 | runtime->private_data = prtd; |
| 312 | return 0; |
| 313 | } |
| 314 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 315 | static int dma_close(struct snd_pcm_substream *substream) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 316 | { |
| 317 | struct snd_pcm_runtime *runtime = substream->runtime; |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 318 | struct runtime_data *prtd = runtime->private_data; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 319 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 320 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 321 | |
Mark Brown | 5111c07 | 2008-04-30 17:19:32 +0200 | [diff] [blame] | 322 | if (!prtd) |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 323 | pr_debug("dma_close called with prtd == NULL\n"); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 324 | |
Mark Brown | 5111c07 | 2008-04-30 17:19:32 +0200 | [diff] [blame] | 325 | kfree(prtd); |
| 326 | |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 330 | static int dma_mmap(struct snd_pcm_substream *substream, |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 331 | struct vm_area_struct *vma) |
| 332 | { |
| 333 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 334 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 335 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 336 | |
| 337 | return dma_mmap_writecombine(substream->pcm->card->dev, vma, |
Mark Brown | 5111c07 | 2008-04-30 17:19:32 +0200 | [diff] [blame] | 338 | runtime->dma_area, |
| 339 | runtime->dma_addr, |
| 340 | runtime->dma_bytes); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 341 | } |
| 342 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 343 | static struct snd_pcm_ops dma_ops = { |
| 344 | .open = dma_open, |
| 345 | .close = dma_close, |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 346 | .ioctl = snd_pcm_lib_ioctl, |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 347 | .hw_params = dma_hw_params, |
| 348 | .hw_free = dma_hw_free, |
| 349 | .prepare = dma_prepare, |
| 350 | .trigger = dma_trigger, |
| 351 | .pointer = dma_pointer, |
| 352 | .mmap = dma_mmap, |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 353 | }; |
| 354 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 355 | static int preallocate_dma_buffer(struct snd_pcm *pcm, int stream) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 356 | { |
| 357 | struct snd_pcm_substream *substream = pcm->streams[stream].substream; |
| 358 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 359 | size_t size = dma_hardware.buffer_bytes_max; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 360 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 361 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 362 | |
| 363 | buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 364 | buf->dev.dev = pcm->card->dev; |
| 365 | buf->private_data = NULL; |
| 366 | buf->area = dma_alloc_writecombine(pcm->card->dev, size, |
| 367 | &buf->addr, GFP_KERNEL); |
| 368 | if (!buf->area) |
| 369 | return -ENOMEM; |
| 370 | buf->bytes = size; |
| 371 | return 0; |
| 372 | } |
| 373 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 374 | static void dma_free_dma_buffers(struct snd_pcm *pcm) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 375 | { |
| 376 | struct snd_pcm_substream *substream; |
| 377 | struct snd_dma_buffer *buf; |
| 378 | int stream; |
| 379 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 380 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 381 | |
| 382 | for (stream = 0; stream < 2; stream++) { |
| 383 | substream = pcm->streams[stream].substream; |
| 384 | if (!substream) |
| 385 | continue; |
| 386 | |
| 387 | buf = &substream->dma_buffer; |
| 388 | if (!buf->area) |
| 389 | continue; |
| 390 | |
| 391 | dma_free_writecombine(pcm->card->dev, buf->bytes, |
| 392 | buf->area, buf->addr); |
| 393 | buf->area = NULL; |
| 394 | } |
| 395 | } |
| 396 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 397 | static u64 dma_mask = DMA_BIT_MASK(32); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 398 | |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 399 | static int dma_new(struct snd_soc_pcm_runtime *rtd) |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 400 | { |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 401 | struct snd_card *card = rtd->card->snd_card; |
Liam Girdwood | 552d1ef | 2011-06-07 16:08:33 +0100 | [diff] [blame] | 402 | struct snd_pcm *pcm = rtd->pcm; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 403 | int ret = 0; |
| 404 | |
Mark Brown | ee7d476 | 2009-03-06 18:04:34 +0000 | [diff] [blame] | 405 | pr_debug("Entered %s\n", __func__); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 406 | |
| 407 | if (!card->dev->dma_mask) |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 408 | card->dev->dma_mask = &dma_mask; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 409 | if (!card->dev->coherent_dma_mask) |
Joachim Eastwood | 350e16d | 2012-01-01 02:43:03 +0100 | [diff] [blame] | 410 | card->dev->coherent_dma_mask = DMA_BIT_MASK(32); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 411 | |
Joachim Eastwood | 25e9e75 | 2012-01-01 01:58:44 +0100 | [diff] [blame] | 412 | if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) { |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 413 | ret = preallocate_dma_buffer(pcm, |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 414 | SNDRV_PCM_STREAM_PLAYBACK); |
| 415 | if (ret) |
| 416 | goto out; |
| 417 | } |
| 418 | |
Joachim Eastwood | 25e9e75 | 2012-01-01 01:58:44 +0100 | [diff] [blame] | 419 | if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 420 | ret = preallocate_dma_buffer(pcm, |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 421 | SNDRV_PCM_STREAM_CAPTURE); |
| 422 | if (ret) |
| 423 | goto out; |
| 424 | } |
Jassi Brar | 4b640cf | 2010-11-22 15:35:57 +0900 | [diff] [blame] | 425 | out: |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 426 | return ret; |
| 427 | } |
| 428 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 429 | static struct snd_soc_platform_driver samsung_asoc_platform = { |
| 430 | .ops = &dma_ops, |
| 431 | .pcm_new = dma_new, |
| 432 | .pcm_free = dma_free_dma_buffers, |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 433 | }; |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 434 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 435 | static int __devinit samsung_asoc_platform_probe(struct platform_device *pdev) |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 436 | { |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 437 | return snd_soc_register_platform(&pdev->dev, &samsung_asoc_platform); |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 438 | } |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 439 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 440 | static int __devexit samsung_asoc_platform_remove(struct platform_device *pdev) |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 441 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 442 | snd_soc_unregister_platform(&pdev->dev); |
| 443 | return 0; |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 444 | } |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 445 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 446 | static struct platform_driver asoc_dma_driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 447 | .driver = { |
Jassi Brar | 58bb407 | 2010-11-22 15:35:50 +0900 | [diff] [blame] | 448 | .name = "samsung-audio", |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 449 | .owner = THIS_MODULE, |
| 450 | }, |
| 451 | |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 452 | .probe = samsung_asoc_platform_probe, |
| 453 | .remove = __devexit_p(samsung_asoc_platform_remove), |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 454 | }; |
| 455 | |
Mark Brown | e00c3f5 | 2011-11-23 15:20:13 +0000 | [diff] [blame] | 456 | module_platform_driver(asoc_dma_driver); |
Mark Brown | 958e792 | 2008-12-03 19:58:17 +0000 | [diff] [blame] | 457 | |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 458 | MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>"); |
Jassi Brar | c3f2028 | 2010-11-22 15:36:50 +0900 | [diff] [blame] | 459 | MODULE_DESCRIPTION("Samsung ASoC DMA Driver"); |
Ben Dooks | c0f41bb | 2007-02-14 13:20:03 +0100 | [diff] [blame] | 460 | MODULE_LICENSE("GPL"); |
Jassi Brar | 58bb407 | 2010-11-22 15:35:50 +0900 | [diff] [blame] | 461 | MODULE_ALIAS("platform:samsung-audio"); |