omap-pcm.c 6.7 KB
Newer Older
1 2 3 4 5
/*
 * omap-pcm.c  --  ALSA PCM interface for the OMAP SoC
 *
 * Copyright (C) 2008 Nokia Corporation
 *
6
 * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
7
 *          Peter Ujfalusi <peter.ujfalusi@ti.com>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <linux/dma-mapping.h>
26
#include <linux/slab.h>
27
#include <linux/module.h>
28
#include <linux/omap-dma.h>
29 30 31
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
32
#include <sound/dmaengine_pcm.h>
33
#include <sound/soc.h>
34
#include <sound/omap-pcm.h>
35

36 37 38 39 40 41
#ifdef CONFIG_ARCH_OMAP1
#define pcm_omap1510()	cpu_is_omap1510()
#else
#define pcm_omap1510()	0
#endif

42
static struct snd_pcm_hardware omap_pcm_hardware = {
43 44 45 46
	.info			= SNDRV_PCM_INFO_MMAP |
				  SNDRV_PCM_INFO_MMAP_VALID |
				  SNDRV_PCM_INFO_INTERLEAVED |
				  SNDRV_PCM_INFO_PAUSE |
47 48
				  SNDRV_PCM_INFO_RESUME |
				  SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
49 50 51 52 53 54 55
	.period_bytes_min	= 32,
	.period_bytes_max	= 64 * 1024,
	.periods_min		= 2,
	.periods_max		= 255,
	.buffer_bytes_max	= 128 * 1024,
};

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
/* sDMA supports only 1, 2, and 4 byte transfer elements. */
static void omap_pcm_limit_supported_formats(void)
{
	int i;

	for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) {
		switch (snd_pcm_format_physical_width(i)) {
		case 8:
		case 16:
		case 32:
			omap_pcm_hardware.formats |= (1LL << i);
			break;
		default:
			break;
		}
	}
}

74 75 76 77 78 79
/* this may get called several times by oss emulation */
static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
			      struct snd_pcm_hw_params *params)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
80
	struct omap_pcm_dma_data *dma_data;
81 82
	struct dma_slave_config config;
	struct dma_chan *chan;
83 84
	int err = 0;

85 86
	memset(&config, 0x00, sizeof(config));

87
	dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
88

89 90
	/* return if this is a bufferless transfer e.g.
	 * codec <--> BT codec or GSM modem -- lg FIXME */
91
	if (!dma_data)
92
		return 0;
93 94 95 96

	snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
	runtime->dma_bytes = params_buffer_bytes(params);

97 98 99
	chan = snd_dmaengine_pcm_get_chan(substream);
	if (!chan)
		return -EINVAL;
100

101 102 103 104
	/* fills in addr_width and direction */
	err = snd_hwparams_to_dma_slave_config(substream, params, &config);
	if (err)
		return err;
105

106 107 108
	snd_dmaengine_pcm_set_config_from_dai_data(substream,
			snd_soc_dai_get_dma_data(rtd->cpu_dai, substream),
			&config);
109

110
	return dmaengine_slave_config(chan, &config);
111 112
}

113
static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
114
{
115
	snd_pcm_set_runtime_buffer(substream, NULL);
116 117 118 119 120 121 122
	return 0;
}

static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
{
	snd_pcm_uframes_t offset;

123
	if (pcm_omap1510())
124 125 126
		offset = snd_dmaengine_pcm_pointer_no_residue(substream);
	else
		offset = snd_dmaengine_pcm_pointer(substream);
127 128 129 130 131 132

	return offset;
}

static int omap_pcm_open(struct snd_pcm_substream *substream)
{
133
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
134
	struct snd_dmaengine_dai_dma_data *dma_data;
135
	int ret;
136 137 138

	snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);

139
	dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
140

141 142 143 144 145 146 147 148 149 150 151 152 153
	/* DT boot: filter_data is the DMA name */
	if (rtd->cpu_dai->dev->of_node) {
		struct dma_chan *chan;

		chan = dma_request_slave_channel(rtd->cpu_dai->dev,
						 dma_data->filter_data);
		ret = snd_dmaengine_pcm_open(substream, chan);
	} else {
		ret = snd_dmaengine_pcm_open_request_chan(substream,
							  omap_dma_filter_fn,
							  dma_data->filter_data);
	}
	return ret;
154 155 156 157 158 159 160 161 162 163 164 165 166
}

static int omap_pcm_mmap(struct snd_pcm_substream *substream,
	struct vm_area_struct *vma)
{
	struct snd_pcm_runtime *runtime = substream->runtime;

	return dma_mmap_writecombine(substream->pcm->card->dev, vma,
				     runtime->dma_area,
				     runtime->dma_addr,
				     runtime->dma_bytes);
}

167
static struct snd_pcm_ops omap_pcm_ops = {
168
	.open		= omap_pcm_open,
169
	.close		= snd_dmaengine_pcm_close_release_chan,
170 171 172
	.ioctl		= snd_pcm_lib_ioctl,
	.hw_params	= omap_pcm_hw_params,
	.hw_free	= omap_pcm_hw_free,
173
	.trigger	= snd_dmaengine_pcm_trigger,
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	.pointer	= omap_pcm_pointer,
	.mmap		= omap_pcm_mmap,
};

static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
	int stream)
{
	struct snd_pcm_substream *substream = pcm->streams[stream].substream;
	struct snd_dma_buffer *buf = &substream->dma_buffer;
	size_t size = omap_pcm_hardware.buffer_bytes_max;

	buf->dev.type = SNDRV_DMA_TYPE_DEV;
	buf->dev.dev = pcm->card->dev;
	buf->private_data = NULL;
	buf->area = dma_alloc_writecombine(pcm->card->dev, size,
					   &buf->addr, GFP_KERNEL);
	if (!buf->area)
		return -ENOMEM;

	buf->bytes = size;
	return 0;
}

static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
{
	struct snd_pcm_substream *substream;
	struct snd_dma_buffer *buf;
	int stream;

	for (stream = 0; stream < 2; stream++) {
		substream = pcm->streams[stream].substream;
		if (!substream)
			continue;

		buf = &substream->dma_buffer;
		if (!buf->area)
			continue;

		dma_free_writecombine(pcm->card->dev, buf->bytes,
				      buf->area, buf->addr);
		buf->area = NULL;
	}
}

218
static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
219
{
220 221
	struct snd_card *card = rtd->card->snd_card;
	struct snd_pcm *pcm = rtd->pcm;
222
	int ret;
223

224
	ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
225 226
	if (ret)
		return ret;
227

228
	if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
229 230 231 232 233 234
		ret = omap_pcm_preallocate_dma_buffer(pcm,
			SNDRV_PCM_STREAM_PLAYBACK);
		if (ret)
			goto out;
	}

235
	if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
236 237 238 239 240 241 242
		ret = omap_pcm_preallocate_dma_buffer(pcm,
			SNDRV_PCM_STREAM_CAPTURE);
		if (ret)
			goto out;
	}

out:
243 244 245 246
	/* free preallocated buffers in case of error */
	if (ret)
		omap_pcm_free_dma_buffers(pcm);

247 248 249
	return ret;
}

250 251
static struct snd_soc_platform_driver omap_soc_platform = {
	.ops		= &omap_pcm_ops,
252 253 254 255
	.pcm_new	= omap_pcm_new,
	.pcm_free	= omap_pcm_free_dma_buffers,
};

256 257
int omap_pcm_platform_register(struct device *dev)
{
258
	omap_pcm_limit_supported_formats();
259 260 261 262
	return devm_snd_soc_register_platform(dev, &omap_soc_platform);
}
EXPORT_SYMBOL_GPL(omap_pcm_platform_register);

263
MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
264 265
MODULE_DESCRIPTION("OMAP PCM DMA module");
MODULE_LICENSE("GPL");