davinci-pcm.c 25.4 KB
Newer Older
V
Vladimir Barinov 已提交
1 2 3
/*
 * ALSA PCM interface for the TI DAVINCI processor
 *
4
 * Author:      Vladimir Barinov, <vbarinov@embeddedalley.com>
V
Vladimir Barinov 已提交
5
 * Copyright:   (C) 2007 MontaVista Software, Inc., <source@mvista.com>
6
 * added SRAM ping/pong (C) 2008 Troy Kisky <troy.kisky@boundarydevices.com>
V
Vladimir Barinov 已提交
7 8 9 10 11 12 13 14 15 16 17
 *
 * 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.
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/dma-mapping.h>
18
#include <linux/kernel.h>
V
Vladimir Barinov 已提交
19 20 21 22 23 24 25

#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>

#include <asm/dma.h>
D
David Brownell 已提交
26
#include <mach/edma.h>
27
#include <mach/sram.h>
V
Vladimir Barinov 已提交
28 29 30

#include "davinci-pcm.h"

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#ifdef DEBUG
static void print_buf_info(int slot, char *name)
{
	struct edmacc_param p;
	if (slot < 0)
		return;
	edma_read_slot(slot, &p);
	printk(KERN_DEBUG "%s: 0x%x, opt=%x, src=%x, a_b_cnt=%x dst=%x\n",
			name, slot, p.opt, p.src, p.a_b_cnt, p.dst);
	printk(KERN_DEBUG "    src_dst_bidx=%x link_bcntrld=%x src_dst_cidx=%x ccnt=%x\n",
			p.src_dst_bidx, p.link_bcntrld, p.src_dst_cidx, p.ccnt);
}
#else
static void print_buf_info(int slot, char *name)
{
}
#endif

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#define DAVINCI_PCM_FMTBITS	(\
				SNDRV_PCM_FMTBIT_S8	|\
				SNDRV_PCM_FMTBIT_U8	|\
				SNDRV_PCM_FMTBIT_S16_LE	|\
				SNDRV_PCM_FMTBIT_S16_BE	|\
				SNDRV_PCM_FMTBIT_U16_LE	|\
				SNDRV_PCM_FMTBIT_U16_BE	|\
				SNDRV_PCM_FMTBIT_S24_LE	|\
				SNDRV_PCM_FMTBIT_S24_BE	|\
				SNDRV_PCM_FMTBIT_U24_LE	|\
				SNDRV_PCM_FMTBIT_U24_BE	|\
				SNDRV_PCM_FMTBIT_S32_LE	|\
				SNDRV_PCM_FMTBIT_S32_BE	|\
				SNDRV_PCM_FMTBIT_U32_LE	|\
				SNDRV_PCM_FMTBIT_U32_BE)

65
static struct snd_pcm_hardware pcm_hardware_playback = {
V
Vladimir Barinov 已提交
66 67
	.info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
		 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
68 69
		 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME|
		 SNDRV_PCM_INFO_BATCH),
70
	.formats = DAVINCI_PCM_FMTBITS,
V
Vladimir Barinov 已提交
71 72 73 74 75 76 77 78
	.rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
		  SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
		  SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
		  SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
		  SNDRV_PCM_RATE_KNOT),
	.rate_min = 8000,
	.rate_max = 96000,
	.channels_min = 2,
79
	.channels_max = 384,
V
Vladimir Barinov 已提交
80 81 82 83 84 85 86 87
	.buffer_bytes_max = 128 * 1024,
	.period_bytes_min = 32,
	.period_bytes_max = 8 * 1024,
	.periods_min = 16,
	.periods_max = 255,
	.fifo_size = 0,
};

88 89 90
static struct snd_pcm_hardware pcm_hardware_capture = {
	.info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
		 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
91 92
		 SNDRV_PCM_INFO_PAUSE |
		 SNDRV_PCM_INFO_BATCH),
93
	.formats = DAVINCI_PCM_FMTBITS,
94 95 96 97 98 99 100 101
	.rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
		  SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
		  SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
		  SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
		  SNDRV_PCM_RATE_KNOT),
	.rate_min = 8000,
	.rate_max = 96000,
	.channels_min = 2,
102
	.channels_max = 384,
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	.buffer_bytes_max = 128 * 1024,
	.period_bytes_min = 32,
	.period_bytes_max = 8 * 1024,
	.periods_min = 16,
	.periods_max = 255,
	.fifo_size = 0,
};

/*
 * How ping/pong works....
 *
 * Playback:
 * ram_params - copys 2*ping_size from start of SDRAM to iram,
 * 	links to ram_link2
 * ram_link2 - copys rest of SDRAM to iram in ping_size units,
 * 	links to ram_link
 * ram_link - copys entire SDRAM to iram in ping_size uints,
 * 	links to self
 *
 * asp_params - same as asp_link[0]
 * asp_link[0] - copys from lower half of iram to asp port
 * 	links to asp_link[1], triggers iram copy event on completion
 * asp_link[1] - copys from upper half of iram to asp port
 * 	links to asp_link[0], triggers iram copy event on completion
 * 	triggers interrupt only needed to let upper SOC levels update position
 * 	in stream on completion
 *
 * When playback is started:
 * 	ram_params started
 * 	asp_params started
 *
 * Capture:
 * ram_params - same as ram_link,
 * 	links to ram_link
 * ram_link - same as playback
 * 	links to self
 *
 * asp_params - same as playback
 * asp_link[0] - same as playback
 * asp_link[1] - same as playback
 *
 * When capture is started:
 * 	asp_params started
 */
V
Vladimir Barinov 已提交
147 148 149
struct davinci_runtime_data {
	spinlock_t lock;
	int period;		/* current DMA period */
150 151
	int asp_channel;	/* Master DMA channel */
	int asp_link[2];	/* asp parameter link channel, ping/pong */
V
Vladimir Barinov 已提交
152
	struct davinci_pcm_dma_params *params;	/* DMA params */
153 154 155 156 157
	int ram_channel;
	int ram_link;
	int ram_link2;
	struct edmacc_param asp_params;
	struct edmacc_param ram_params;
V
Vladimir Barinov 已提交
158 159
};

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
static void davinci_pcm_period_elapsed(struct snd_pcm_substream *substream)
{
	struct davinci_runtime_data *prtd = substream->runtime->private_data;
	struct snd_pcm_runtime *runtime = substream->runtime;

	prtd->period++;
	if (unlikely(prtd->period >= runtime->periods))
		prtd->period = 0;
}

static void davinci_pcm_period_reset(struct snd_pcm_substream *substream)
{
	struct davinci_runtime_data *prtd = substream->runtime->private_data;

	prtd->period = 0;
}
176 177 178
/*
 * Not used with ping/pong
 */
V
Vladimir Barinov 已提交
179 180 181 182
static void davinci_pcm_enqueue_dma(struct snd_pcm_substream *substream)
{
	struct davinci_runtime_data *prtd = substream->runtime->private_data;
	struct snd_pcm_runtime *runtime = substream->runtime;
183
	int link = prtd->asp_link[0];
V
Vladimir Barinov 已提交
184 185 186 187 188
	unsigned int period_size;
	unsigned int dma_offset;
	dma_addr_t dma_pos;
	dma_addr_t src, dst;
	unsigned short src_bidx, dst_bidx;
189
	unsigned short src_cidx, dst_cidx;
V
Vladimir Barinov 已提交
190
	unsigned int data_type;
191
	unsigned short acnt;
V
Vladimir Barinov 已提交
192
	unsigned int count;
193
	unsigned int fifo_level;
V
Vladimir Barinov 已提交
194 195 196 197

	period_size = snd_pcm_lib_period_bytes(substream);
	dma_offset = prtd->period * period_size;
	dma_pos = runtime->dma_addr + dma_offset;
198
	fifo_level = prtd->params->fifo_level;
V
Vladimir Barinov 已提交
199

200
	pr_debug("davinci_pcm: audio_set_dma_params_play channel = %d "
201
		"dma_ptr = %x period_size=%x\n", link, dma_pos, period_size);
V
Vladimir Barinov 已提交
202 203 204

	data_type = prtd->params->data_type;
	count = period_size / data_type;
205 206
	if (fifo_level)
		count /= fifo_level;
V
Vladimir Barinov 已提交
207 208 209 210 211 212

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		src = dma_pos;
		dst = prtd->params->dma_addr;
		src_bidx = data_type;
		dst_bidx = 0;
213 214
		src_cidx = data_type * fifo_level;
		dst_cidx = 0;
V
Vladimir Barinov 已提交
215 216 217 218 219
	} else {
		src = prtd->params->dma_addr;
		dst = dma_pos;
		src_bidx = 0;
		dst_bidx = data_type;
220 221
		src_cidx = 0;
		dst_cidx = data_type * fifo_level;
V
Vladimir Barinov 已提交
222 223
	}

224
	acnt = prtd->params->acnt;
225 226
	edma_set_src(link, src, INCR, W8BIT);
	edma_set_dest(link, dst, INCR, W8BIT);
227

228 229
	edma_set_src_index(link, src_bidx, src_cidx);
	edma_set_dest_index(link, dst_bidx, dst_cidx);
230 231

	if (!fifo_level)
232
		edma_set_transfer_params(link, acnt, count, 1, 0, ASYNC);
233
	else
234
		edma_set_transfer_params(link, acnt, fifo_level, count,
235
							fifo_level, ABSYNC);
V
Vladimir Barinov 已提交
236 237
}

238
static void davinci_pcm_dma_irq(unsigned link, u16 ch_status, void *data)
V
Vladimir Barinov 已提交
239 240 241 242
{
	struct snd_pcm_substream *substream = data;
	struct davinci_runtime_data *prtd = substream->runtime->private_data;

243
	print_buf_info(prtd->ram_channel, "i ram_channel");
244
	pr_debug("davinci_pcm: link=%d, status=0x%x\n", link, ch_status);
V
Vladimir Barinov 已提交
245 246 247 248 249

	if (unlikely(ch_status != DMA_COMPLETE))
		return;

	if (snd_pcm_running(substream)) {
250
		spin_lock(&prtd->lock);
251 252 253 254
		if (prtd->ram_channel < 0) {
			/* No ping/pong must fix up link dma data*/
			davinci_pcm_enqueue_dma(substream);
		}
255 256
		davinci_pcm_period_elapsed(substream);
		spin_unlock(&prtd->lock);
V
Vladimir Barinov 已提交
257
		snd_pcm_period_elapsed(substream);
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	}
}

static int allocate_sram(struct snd_pcm_substream *substream, unsigned size,
		struct snd_pcm_hardware *ppcm)
{
	struct snd_dma_buffer *buf = &substream->dma_buffer;
	struct snd_dma_buffer *iram_dma = NULL;
	dma_addr_t iram_phys = 0;
	void *iram_virt = NULL;

	if (buf->private_data || !size)
		return 0;

	ppcm->period_bytes_max = size;
	iram_virt = sram_alloc(size, &iram_phys);
	if (!iram_virt)
		goto exit1;
	iram_dma = kzalloc(sizeof(*iram_dma), GFP_KERNEL);
	if (!iram_dma)
		goto exit2;
	iram_dma->area = iram_virt;
	iram_dma->addr = iram_phys;
	memset(iram_dma->area, 0, size);
	iram_dma->bytes = size;
	buf->private_data = iram_dma;
	return 0;
exit2:
	if (iram_virt)
		sram_free(iram_virt, size);
exit1:
	return -ENOMEM;
}
V
Vladimir Barinov 已提交
291

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
/*
 * Only used with ping/pong.
 * This is called after runtime->dma_addr, period_bytes and data_type are valid
 */
static int ping_pong_dma_setup(struct snd_pcm_substream *substream)
{
	unsigned short ram_src_cidx, ram_dst_cidx;
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct davinci_runtime_data *prtd = runtime->private_data;
	struct snd_dma_buffer *iram_dma =
		(struct snd_dma_buffer *)substream->dma_buffer.private_data;
	struct davinci_pcm_dma_params *params = prtd->params;
	unsigned int data_type = params->data_type;
	unsigned int acnt = params->acnt;
	/* divide by 2 for ping/pong */
	unsigned int ping_size = snd_pcm_lib_period_bytes(substream) >> 1;
	int link = prtd->asp_link[1];
	unsigned int fifo_level = prtd->params->fifo_level;
	unsigned int count;
	if ((data_type == 0) || (data_type > 4)) {
		printk(KERN_ERR "%s: data_type=%i\n", __func__, data_type);
		return -EINVAL;
	}
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		dma_addr_t asp_src_pong = iram_dma->addr + ping_size;
		ram_src_cidx = ping_size;
		ram_dst_cidx = -ping_size;
		edma_set_src(link, asp_src_pong, INCR, W8BIT);

		link = prtd->asp_link[0];
		edma_set_src_index(link, data_type, data_type * fifo_level);
		link = prtd->asp_link[1];
		edma_set_src_index(link, data_type, data_type * fifo_level);

		link = prtd->ram_link;
		edma_set_src(link, runtime->dma_addr, INCR, W32BIT);
	} else {
		dma_addr_t asp_dst_pong = iram_dma->addr + ping_size;
		ram_src_cidx = -ping_size;
		ram_dst_cidx = ping_size;
		edma_set_dest(link, asp_dst_pong, INCR, W8BIT);

		link = prtd->asp_link[0];
		edma_set_dest_index(link, data_type, data_type * fifo_level);
		link = prtd->asp_link[1];
		edma_set_dest_index(link, data_type, data_type * fifo_level);

		link = prtd->ram_link;
		edma_set_dest(link, runtime->dma_addr, INCR, W32BIT);
	}

	if (!fifo_level) {
		count = ping_size / data_type;
		edma_set_transfer_params(prtd->asp_link[0], acnt, count,
				1, 0, ASYNC);
		edma_set_transfer_params(prtd->asp_link[1], acnt, count,
				1, 0, ASYNC);
	} else {
		count = ping_size / (data_type * fifo_level);
		edma_set_transfer_params(prtd->asp_link[0], acnt, fifo_level,
				count, fifo_level, ABSYNC);
		edma_set_transfer_params(prtd->asp_link[1], acnt, fifo_level,
				count, fifo_level, ABSYNC);
	}

	link = prtd->ram_link;
	edma_set_src_index(link, ping_size, ram_src_cidx);
	edma_set_dest_index(link, ping_size, ram_dst_cidx);
	edma_set_transfer_params(link, ping_size, 2,
			runtime->periods, 2, ASYNC);

	/* init master params */
	edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
	edma_read_slot(prtd->ram_link, &prtd->ram_params);
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		struct edmacc_param p_ram;
		/* Copy entire iram buffer before playback started */
		prtd->ram_params.a_b_cnt = (1 << 16) | (ping_size << 1);
		/* 0 dst_bidx */
		prtd->ram_params.src_dst_bidx = (ping_size << 1);
		/* 0 dst_cidx */
		prtd->ram_params.src_dst_cidx = (ping_size << 1);
		prtd->ram_params.ccnt = 1;

		/* Skip 1st period */
		edma_read_slot(prtd->ram_link, &p_ram);
		p_ram.src += (ping_size << 1);
		p_ram.ccnt -= 1;
		edma_write_slot(prtd->ram_link2, &p_ram);
		/*
		 * When 1st started, ram -> iram dma channel will fill the
		 * entire iram.  Then, whenever a ping/pong asp buffer finishes,
		 * 1/2 iram will be filled.
		 */
		prtd->ram_params.link_bcntrld =
			EDMA_CHAN_SLOT(prtd->ram_link2) << 5;
	}
	return 0;
}

/* 1 asp tx or rx channel using 2 parameter channels
 * 1 ram to/from iram channel using 1 parameter channel
 *
 * Playback
 * ram copy channel kicks off first,
 * 1st ram copy of entire iram buffer completion kicks off asp channel
 * asp tcc always kicks off ram copy of 1/2 iram buffer
 *
 * Record
 * asp channel starts, tcc kicks off ram copy
 */
static int request_ping_pong(struct snd_pcm_substream *substream,
		struct davinci_runtime_data *prtd,
		struct snd_dma_buffer *iram_dma)
{
	dma_addr_t asp_src_ping;
	dma_addr_t asp_dst_ping;
	int link;
	struct davinci_pcm_dma_params *params = prtd->params;

	/* Request ram master channel */
	link = prtd->ram_channel = edma_alloc_channel(EDMA_CHANNEL_ANY,
				  davinci_pcm_dma_irq, substream,
415
				  prtd->params->ram_chan_q);
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
	if (link < 0)
		goto exit1;

	/* Request ram link channel */
	link = prtd->ram_link = edma_alloc_slot(
			EDMA_CTLR(prtd->ram_channel), EDMA_SLOT_ANY);
	if (link < 0)
		goto exit2;

	link = prtd->asp_link[1] = edma_alloc_slot(
			EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
	if (link < 0)
		goto exit3;

	prtd->ram_link2 = -1;
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		link = prtd->ram_link2 = edma_alloc_slot(
			EDMA_CTLR(prtd->ram_channel), EDMA_SLOT_ANY);
		if (link < 0)
			goto exit4;
	}
	/* circle ping-pong buffers */
	edma_link(prtd->asp_link[0], prtd->asp_link[1]);
	edma_link(prtd->asp_link[1], prtd->asp_link[0]);
	/* circle ram buffers */
	edma_link(prtd->ram_link, prtd->ram_link);

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		asp_src_ping = iram_dma->addr;
		asp_dst_ping = params->dma_addr;	/* fifo */
	} else {
		asp_src_ping = params->dma_addr;	/* fifo */
		asp_dst_ping = iram_dma->addr;
V
Vladimir Barinov 已提交
449
	}
450 451 452 453 454 455 456 457 458
	/* ping */
	link = prtd->asp_link[0];
	edma_set_src(link, asp_src_ping, INCR, W16BIT);
	edma_set_dest(link, asp_dst_ping, INCR, W16BIT);
	edma_set_src_index(link, 0, 0);
	edma_set_dest_index(link, 0, 0);

	edma_read_slot(link, &prtd->asp_params);
	prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f) | TCINTEN);
459 460
	prtd->asp_params.opt |= TCCHEN |
		EDMA_TCC(prtd->ram_channel & 0x3f);
461 462 463 464 465 466 467 468 469 470 471 472 473
	edma_write_slot(link, &prtd->asp_params);

	/* pong */
	link = prtd->asp_link[1];
	edma_set_src(link, asp_src_ping, INCR, W16BIT);
	edma_set_dest(link, asp_dst_ping, INCR, W16BIT);
	edma_set_src_index(link, 0, 0);
	edma_set_dest_index(link, 0, 0);

	edma_read_slot(link, &prtd->asp_params);
	prtd->asp_params.opt &= ~(TCCMODE | EDMA_TCC(0x3f));
	/* interrupt after every pong completion */
	prtd->asp_params.opt |= TCINTEN | TCCHEN |
474
		EDMA_TCC(prtd->ram_channel & 0x3f);
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
	edma_write_slot(link, &prtd->asp_params);

	/* ram */
	link = prtd->ram_link;
	edma_set_src(link, iram_dma->addr, INCR, W32BIT);
	edma_set_dest(link, iram_dma->addr, INCR, W32BIT);
	pr_debug("%s: audio dma channels/slots in use for ram:%u %u %u,"
		"for asp:%u %u %u\n", __func__,
		prtd->ram_channel, prtd->ram_link, prtd->ram_link2,
		prtd->asp_channel, prtd->asp_link[0],
		prtd->asp_link[1]);
	return 0;
exit4:
	edma_free_channel(prtd->asp_link[1]);
	prtd->asp_link[1] = -1;
exit3:
	edma_free_channel(prtd->ram_link);
	prtd->ram_link = -1;
exit2:
	edma_free_channel(prtd->ram_channel);
	prtd->ram_channel = -1;
exit1:
	return link;
V
Vladimir Barinov 已提交
498 499 500 501
}

static int davinci_pcm_dma_request(struct snd_pcm_substream *substream)
{
502
	struct snd_dma_buffer *iram_dma;
V
Vladimir Barinov 已提交
503
	struct davinci_runtime_data *prtd = substream->runtime->private_data;
504 505
	struct davinci_pcm_dma_params *params = prtd->params;
	int link;
V
Vladimir Barinov 已提交
506

507 508
	if (!params)
		return -ENODEV;
V
Vladimir Barinov 已提交
509

510 511
	/* Request asp master DMA channel */
	link = prtd->asp_channel = edma_alloc_channel(params->channel,
512 513
			davinci_pcm_dma_irq, substream,
			prtd->params->asp_chan_q);
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
	if (link < 0)
		goto exit1;

	/* Request asp link channels */
	link = prtd->asp_link[0] = edma_alloc_slot(
			EDMA_CTLR(prtd->asp_channel), EDMA_SLOT_ANY);
	if (link < 0)
		goto exit2;

	iram_dma = (struct snd_dma_buffer *)substream->dma_buffer.private_data;
	if (iram_dma) {
		if (request_ping_pong(substream, prtd, iram_dma) == 0)
			return 0;
		printk(KERN_WARNING "%s: dma channel allocation failed,"
				"not using sram\n", __func__);
V
Vladimir Barinov 已提交
529
	}
D
David Brownell 已提交
530 531 532 533 534 535 536 537 538 539

	/* Issue transfer completion IRQ when the channel completes a
	 * transfer, then always reload from the same slot (by a kind
	 * of loopback link).  The completion IRQ handler will update
	 * the reload slot with a new buffer.
	 *
	 * REVISIT save p_ram here after setting up everything except
	 * the buffer and its length (ccnt) ... use it as a template
	 * so davinci_pcm_enqueue_dma() takes less time in IRQ.
	 */
540 541 542 543 544
	edma_read_slot(link, &prtd->asp_params);
	prtd->asp_params.opt |= TCINTEN |
		EDMA_TCC(EDMA_CHAN_SLOT(prtd->asp_channel));
	prtd->asp_params.link_bcntrld = EDMA_CHAN_SLOT(link) << 5;
	edma_write_slot(link, &prtd->asp_params);
V
Vladimir Barinov 已提交
545
	return 0;
546 547 548 549 550
exit2:
	edma_free_channel(prtd->asp_channel);
	prtd->asp_channel = -1;
exit1:
	return link;
V
Vladimir Barinov 已提交
551 552 553 554 555 556 557 558 559 560 561
}

static int davinci_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
{
	struct davinci_runtime_data *prtd = substream->runtime->private_data;
	int ret = 0;

	spin_lock(&prtd->lock);

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
562 563 564 565 566 567 568
		edma_start(prtd->asp_channel);
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
		    prtd->ram_channel >= 0) {
			/* copy 1st iram buffer */
			edma_start(prtd->ram_channel);
		}
		break;
V
Vladimir Barinov 已提交
569 570
	case SNDRV_PCM_TRIGGER_RESUME:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
571
		edma_resume(prtd->asp_channel);
V
Vladimir Barinov 已提交
572 573 574 575
		break;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
576
		edma_pause(prtd->asp_channel);
V
Vladimir Barinov 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
		break;
	default:
		ret = -EINVAL;
		break;
	}

	spin_unlock(&prtd->lock);

	return ret;
}

static int davinci_pcm_prepare(struct snd_pcm_substream *substream)
{
	struct davinci_runtime_data *prtd = substream->runtime->private_data;

592
	davinci_pcm_period_reset(substream);
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
	if (prtd->ram_channel >= 0) {
		int ret = ping_pong_dma_setup(substream);
		if (ret < 0)
			return ret;

		edma_write_slot(prtd->ram_channel, &prtd->ram_params);
		edma_write_slot(prtd->asp_channel, &prtd->asp_params);

		print_buf_info(prtd->ram_channel, "ram_channel");
		print_buf_info(prtd->ram_link, "ram_link");
		print_buf_info(prtd->ram_link2, "ram_link2");
		print_buf_info(prtd->asp_channel, "asp_channel");
		print_buf_info(prtd->asp_link[0], "asp_link[0]");
		print_buf_info(prtd->asp_link[1], "asp_link[1]");

608 609 610 611 612 613 614 615 616 617 618 619
		/*
		 * There is a phase offset of 2 periods between the position
		 * used by dma setup and the position reported in the pointer
		 * function.
		 *
		 * The phase offset, when not using ping-pong buffers, is due to
		 * the two consecutive calls to davinci_pcm_enqueue_dma() below.
		 *
		 * Whereas here, with ping-pong buffers, the phase is due to
		 * there being an entire buffer transfer complete before the
		 * first dma completion event triggers davinci_pcm_dma_irq().
		 */
620 621 622
		davinci_pcm_period_elapsed(substream);
		davinci_pcm_period_elapsed(substream);

623 624
		return 0;
	}
V
Vladimir Barinov 已提交
625
	davinci_pcm_enqueue_dma(substream);
626
	davinci_pcm_period_elapsed(substream);
V
Vladimir Barinov 已提交
627

D
David Brownell 已提交
628
	/* Copy self-linked parameter RAM entry into master channel */
629 630
	edma_read_slot(prtd->asp_link[0], &prtd->asp_params);
	edma_write_slot(prtd->asp_channel, &prtd->asp_params);
631
	davinci_pcm_enqueue_dma(substream);
632
	davinci_pcm_period_elapsed(substream);
V
Vladimir Barinov 已提交
633 634 635 636 637 638 639 640 641 642

	return 0;
}

static snd_pcm_uframes_t
davinci_pcm_pointer(struct snd_pcm_substream *substream)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct davinci_runtime_data *prtd = runtime->private_data;
	unsigned int offset;
643
	int asp_count;
644
	unsigned int period_size = snd_pcm_lib_period_bytes(substream);
V
Vladimir Barinov 已提交
645

646 647 648 649 650 651 652
	/*
	 * There is a phase offset of 2 periods between the position used by dma
	 * setup and the position reported in the pointer function. Either +2 in
	 * the dma setup or -2 here in the pointer function (with wrapping,
	 * both) accounts for this offset -- choose the latter since it makes
	 * the first-time setup clearer.
	 */
V
Vladimir Barinov 已提交
653
	spin_lock(&prtd->lock);
654
	asp_count = prtd->period - 2;
V
Vladimir Barinov 已提交
655 656
	spin_unlock(&prtd->lock);

657 658 659 660
	if (asp_count < 0)
		asp_count += runtime->periods;
	asp_count *= period_size;

661
	offset = bytes_to_frames(runtime, asp_count);
V
Vladimir Barinov 已提交
662 663 664 665 666 667 668 669 670 671
	if (offset >= runtime->buffer_size)
		offset = 0;

	return offset;
}

static int davinci_pcm_open(struct snd_pcm_substream *substream)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct davinci_runtime_data *prtd;
672
	struct snd_pcm_hardware *ppcm;
V
Vladimir Barinov 已提交
673
	int ret = 0;
674
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
675
	struct davinci_pcm_dma_params *pa;
676
	struct davinci_pcm_dma_params *params;
677

678
	pa = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
679
	if (!pa)
680
		return -ENODEV;
681
	params = &pa[substream->stream];
V
Vladimir Barinov 已提交
682

683 684 685 686
	ppcm = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
			&pcm_hardware_playback : &pcm_hardware_capture;
	allocate_sram(substream, params->sram_size, ppcm);
	snd_soc_set_runtime_hwparams(substream, ppcm);
687 688 689 690 691
	/* ensure that buffer size is a multiple of period size */
	ret = snd_pcm_hw_constraint_integer(runtime,
						SNDRV_PCM_HW_PARAM_PERIODS);
	if (ret < 0)
		return ret;
V
Vladimir Barinov 已提交
692 693 694 695 696 697

	prtd = kzalloc(sizeof(struct davinci_runtime_data), GFP_KERNEL);
	if (prtd == NULL)
		return -ENOMEM;

	spin_lock_init(&prtd->lock);
698
	prtd->params = params;
699 700 701 702 703
	prtd->asp_channel = -1;
	prtd->asp_link[0] = prtd->asp_link[1] = -1;
	prtd->ram_channel = -1;
	prtd->ram_link = -1;
	prtd->ram_link2 = -1;
V
Vladimir Barinov 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720

	runtime->private_data = prtd;

	ret = davinci_pcm_dma_request(substream);
	if (ret) {
		printk(KERN_ERR "davinci_pcm: Failed to get dma channels\n");
		kfree(prtd);
	}

	return ret;
}

static int davinci_pcm_close(struct snd_pcm_substream *substream)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct davinci_runtime_data *prtd = runtime->private_data;

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
	if (prtd->ram_channel >= 0)
		edma_stop(prtd->ram_channel);
	if (prtd->asp_channel >= 0)
		edma_stop(prtd->asp_channel);
	if (prtd->asp_link[0] >= 0)
		edma_unlink(prtd->asp_link[0]);
	if (prtd->asp_link[1] >= 0)
		edma_unlink(prtd->asp_link[1]);
	if (prtd->ram_link >= 0)
		edma_unlink(prtd->ram_link);

	if (prtd->asp_link[0] >= 0)
		edma_free_slot(prtd->asp_link[0]);
	if (prtd->asp_link[1] >= 0)
		edma_free_slot(prtd->asp_link[1]);
	if (prtd->asp_channel >= 0)
		edma_free_channel(prtd->asp_channel);
	if (prtd->ram_link >= 0)
		edma_free_slot(prtd->ram_link);
	if (prtd->ram_link2 >= 0)
		edma_free_slot(prtd->ram_link2);
	if (prtd->ram_channel >= 0)
		edma_free_channel(prtd->ram_channel);
V
Vladimir Barinov 已提交
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772

	kfree(prtd);

	return 0;
}

static int davinci_pcm_hw_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *hw_params)
{
	return snd_pcm_lib_malloc_pages(substream,
					params_buffer_bytes(hw_params));
}

static int davinci_pcm_hw_free(struct snd_pcm_substream *substream)
{
	return snd_pcm_lib_free_pages(substream);
}

static int davinci_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);
}

773
static struct snd_pcm_ops davinci_pcm_ops = {
V
Vladimir Barinov 已提交
774 775 776 777 778 779 780 781 782 783 784
	.open = 	davinci_pcm_open,
	.close = 	davinci_pcm_close,
	.ioctl = 	snd_pcm_lib_ioctl,
	.hw_params = 	davinci_pcm_hw_params,
	.hw_free = 	davinci_pcm_hw_free,
	.prepare = 	davinci_pcm_prepare,
	.trigger = 	davinci_pcm_trigger,
	.pointer = 	davinci_pcm_pointer,
	.mmap = 	davinci_pcm_mmap,
};

785 786
static int davinci_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream,
		size_t size)
V
Vladimir Barinov 已提交
787 788 789 790 791 792 793 794 795 796
{
	struct snd_pcm_substream *substream = pcm->streams[stream].substream;
	struct snd_dma_buffer *buf = &substream->dma_buffer;

	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);

797 798
	pr_debug("davinci_pcm: preallocate_dma_buffer: area=%p, addr=%p, "
		"size=%d\n", (void *) buf->area, (void *) buf->addr, size);
V
Vladimir Barinov 已提交
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813

	if (!buf->area)
		return -ENOMEM;

	buf->bytes = size;
	return 0;
}

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

	for (stream = 0; stream < 2; stream++) {
814
		struct snd_dma_buffer *iram_dma;
V
Vladimir Barinov 已提交
815 816 817 818 819 820 821 822 823 824 825
		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;
826
		iram_dma = buf->private_data;
827 828 829 830
		if (iram_dma) {
			sram_free(iram_dma->area, iram_dma->bytes);
			kfree(iram_dma);
		}
V
Vladimir Barinov 已提交
831 832 833 834 835 836
	}
}

static u64 davinci_pcm_dmamask = 0xffffffff;

static int davinci_pcm_new(struct snd_card *card,
837
			   struct snd_soc_dai *dai, struct snd_pcm *pcm)
V
Vladimir Barinov 已提交
838 839 840 841 842 843 844 845
{
	int ret;

	if (!card->dev->dma_mask)
		card->dev->dma_mask = &davinci_pcm_dmamask;
	if (!card->dev->coherent_dma_mask)
		card->dev->coherent_dma_mask = 0xffffffff;

846
	if (dai->driver->playback.channels_min) {
V
Vladimir Barinov 已提交
847
		ret = davinci_pcm_preallocate_dma_buffer(pcm,
848 849
			SNDRV_PCM_STREAM_PLAYBACK,
			pcm_hardware_playback.buffer_bytes_max);
V
Vladimir Barinov 已提交
850 851 852 853
		if (ret)
			return ret;
	}

854
	if (dai->driver->capture.channels_min) {
V
Vladimir Barinov 已提交
855
		ret = davinci_pcm_preallocate_dma_buffer(pcm,
856 857
			SNDRV_PCM_STREAM_CAPTURE,
			pcm_hardware_capture.buffer_bytes_max);
V
Vladimir Barinov 已提交
858 859 860 861 862 863 864
		if (ret)
			return ret;
	}

	return 0;
}

865 866
static struct snd_soc_platform_driver davinci_soc_platform = {
	.ops =		&davinci_pcm_ops,
V
Vladimir Barinov 已提交
867 868 869 870
	.pcm_new = 	davinci_pcm_new,
	.pcm_free = 	davinci_pcm_free,
};

871
static int __devinit davinci_soc_platform_probe(struct platform_device *pdev)
M
Mark Brown 已提交
872
{
873
	return snd_soc_register_platform(&pdev->dev, &davinci_soc_platform);
M
Mark Brown 已提交
874 875
}

876
static int __devexit davinci_soc_platform_remove(struct platform_device *pdev)
M
Mark Brown 已提交
877
{
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
	snd_soc_unregister_platform(&pdev->dev);
	return 0;
}

static struct platform_driver davinci_pcm_driver = {
	.driver = {
			.name = "davinci-pcm-audio",
			.owner = THIS_MODULE,
	},

	.probe = davinci_soc_platform_probe,
	.remove = __devexit_p(davinci_soc_platform_remove),
};

static int __init snd_davinci_pcm_init(void)
{
	return platform_driver_register(&davinci_pcm_driver);
}
module_init(snd_davinci_pcm_init);

static void __exit snd_davinci_pcm_exit(void)
{
	platform_driver_unregister(&davinci_pcm_driver);
M
Mark Brown 已提交
901
}
902
module_exit(snd_davinci_pcm_exit);
M
Mark Brown 已提交
903

V
Vladimir Barinov 已提交
904 905 906
MODULE_AUTHOR("Vladimir Barinov");
MODULE_DESCRIPTION("TI DAVINCI PCM DMA module");
MODULE_LICENSE("GPL");