summaryrefslogtreecommitdiff
path: root/sound/soc/imx/imx-pcm.c
blob: 3e8a90da35c2ae3bdc504d3ec29723f194523382 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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
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
415
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
 * imx-pcm.c -- ALSA SoC interface for the Freescale i.MX3 CPU's
 *
 * Copyright 2006 Wolfson Microelectronics PLC.
 * Copyright (C) 2006-2010 Freescale Semiconductor, Inc.
 * Author: Liam Girdwood
 *         liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
 *
 * Based on imx31-pcm.c by Nicolas Pitre, (C) 2004 MontaVista Software, Inc.
 * and on mxc-alsa-mc13783 (C) 2006 Freescale.
 *
 * 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>
#include <linux/iram_alloc.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <mach/dma.h>
#include <mach/clock.h>
#include <asm/mach-types.h>
#include <mach/hardware.h>

#include "imx-pcm.h"
#include "imx-ssi.h"
#include "imx-esai.h"

#if defined(CONFIG_MXC_ASRC) || defined(CONFIG_MXC_ASRC_MODULE)
#include <linux/delay.h>
#include <linux/mxc_asrc.h>
#endif

#ifdef CONFIG_SND_MXC_SOC_IRAM
static bool UseIram = 1;
#else
static bool UseIram;
#endif

/* debug */
#define IMX_PCM_DEBUG 0
#if IMX_PCM_DEBUG
#define dbg(format, arg...) printk(format, ## arg)
#else
#define dbg(format, arg...)
#endif

static const struct snd_pcm_hardware imx_pcm_hardware = {
	.info = (SNDRV_PCM_INFO_INTERLEAVED |
		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
		 SNDRV_PCM_INFO_MMAP |
		 SNDRV_PCM_INFO_MMAP_VALID |
		 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
	.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
#ifdef CONFIG_SND_MXC_SOC_IRAM
	.buffer_bytes_max = SND_RAM_SIZE,
	.period_bytes_max = SND_RAM_SIZE / 4,
#else
	.buffer_bytes_max = 64 * 1024,
	.period_bytes_max = 16 * 1024,
#endif
	.period_bytes_min = 2 * SZ_1K,
	.periods_min = 2,
	.periods_max = 255,
	.fifo_size = 0,
};

static struct vm_operations_struct snd_mxc_audio_playback_vm_ops = {
	.open = snd_pcm_mmap_data_open,
	.close = snd_pcm_mmap_data_close,
};

/*
	enable user space access to iram buffer
*/
static int imx_iram_audio_playback_mmap(struct snd_pcm_substream *substream,
					struct vm_area_struct *area)
{
	struct snd_dma_buffer *buf = &substream->dma_buffer;
	unsigned long off;
	unsigned long phys;
	unsigned long size;
	int ret = 0;

	area->vm_ops = &snd_mxc_audio_playback_vm_ops;
	area->vm_private_data = substream;

	off = area->vm_pgoff << PAGE_SHIFT;
	phys = buf->addr + off;
	size = area->vm_end - area->vm_start;

	if (off + size > SND_RAM_SIZE)
		return -EINVAL;

	area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
	area->vm_flags |= VM_IO;
	ret =
	    remap_pfn_range(area, area->vm_start, phys >> PAGE_SHIFT,
			    size, area->vm_page_prot);
	if (ret == 0)
		area->vm_ops->open(area);

	return ret;
}

static int imx_get_sdma_transfer(int format, int dai_port,
				 struct snd_pcm_substream *substream)
{
	int transfer = -1;

#if defined(CONFIG_MXC_ASRC) || defined(CONFIG_MXC_ASRC_MODULE)
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct mxc_runtime_data *prtd = runtime->private_data;
	if (prtd->asrc_enable == 1) {
		if (dai_port == IMX_DAI_SSI0) {
			if (prtd->asrc_index == 0)
				transfer = MXC_DMA_ASRCA_SSI1_TX0;
			else if (prtd->asrc_index == 1)
				transfer = MXC_DMA_ASRCB_SSI1_TX0;
		} else if (dai_port == IMX_DAI_SSI1) {
			if (prtd->asrc_index == 0)
				transfer = MXC_DMA_ASRCA_SSI1_TX1;
			else if (prtd->asrc_index == 1)
				transfer = MXC_DMA_ASRCB_SSI1_TX1;
		} else if (dai_port == IMX_DAI_SSI2) {
			if (prtd->asrc_index == 0)
				transfer = MXC_DMA_ASRCA_SSI2_TX0;
			else if (prtd->asrc_index == 1)
				transfer = MXC_DMA_ASRCB_SSI2_TX0;
		} else if (dai_port == IMX_DAI_SSI3) {
			if (prtd->asrc_index == 0)
				transfer = MXC_DMA_ASRCA_SSI2_TX1;
			else if (prtd->asrc_index == 1)
				transfer = MXC_DMA_ASRCB_SSI2_TX1;
		} else if (dai_port & IMX_DAI_ESAI_TX) {
			if (prtd->asrc_index == 0)
				transfer = MXC_DMA_ASRCA_ESAI;
			else if (prtd->asrc_index == 1)
				transfer = MXC_DMA_ASRCB_ESAI;
			else
				transfer = MXC_DMA_ASRCC_ESAI;
		}
	} else {
#endif

		if (dai_port == IMX_DAI_SSI0) {
			if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
				if (format == SNDRV_PCM_FORMAT_S16_LE)
					transfer = MXC_DMA_SSI1_16BIT_TX0;
				else if (format == SNDRV_PCM_FORMAT_S24_LE)
					transfer = MXC_DMA_SSI1_24BIT_TX0;
				else if (format == SNDRV_PCM_FORMAT_S20_3LE)
					transfer = MXC_DMA_SSI1_24BIT_TX0;
			} else {
				if (format == SNDRV_PCM_FORMAT_S16_LE)
					transfer = MXC_DMA_SSI1_16BIT_RX0;
				else if (format == SNDRV_PCM_FORMAT_S24_LE)
					transfer = MXC_DMA_SSI1_24BIT_RX0;
				else if (format == SNDRV_PCM_FORMAT_S20_3LE)
					transfer = MXC_DMA_SSI1_24BIT_RX0;
			}
		} else if (dai_port == IMX_DAI_SSI1) {
			if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
				if (format == SNDRV_PCM_FORMAT_S16_LE)
					transfer = MXC_DMA_SSI1_16BIT_TX1;
				else if (format == SNDRV_PCM_FORMAT_S24_LE)
					transfer = MXC_DMA_SSI1_24BIT_TX1;
				else if (format == SNDRV_PCM_FORMAT_S20_3LE)
					transfer = MXC_DMA_SSI1_24BIT_TX1;
			} else {
				if (format == SNDRV_PCM_FORMAT_S16_LE)
					transfer = MXC_DMA_SSI1_16BIT_RX1;
				else if (format == SNDRV_PCM_FORMAT_S24_LE)
					transfer = MXC_DMA_SSI1_24BIT_RX1;
				else if (format == SNDRV_PCM_FORMAT_S20_3LE)
					transfer = MXC_DMA_SSI1_24BIT_RX1;
			}
		} else if (dai_port == IMX_DAI_SSI2) {
			if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
				if (format == SNDRV_PCM_FORMAT_S16_LE)
					transfer = MXC_DMA_SSI2_16BIT_TX0;
				else if (format == SNDRV_PCM_FORMAT_S24_LE)
					transfer = MXC_DMA_SSI2_24BIT_TX0;
				else if (format == SNDRV_PCM_FORMAT_S20_3LE)
					transfer = MXC_DMA_SSI2_24BIT_TX0;
			} else {
				if (format == SNDRV_PCM_FORMAT_S16_LE)
					transfer = MXC_DMA_SSI2_16BIT_RX0;
				else if (format == SNDRV_PCM_FORMAT_S24_LE)
					transfer = MXC_DMA_SSI2_24BIT_RX0;
				else if (format == SNDRV_PCM_FORMAT_S20_3LE)
					transfer = MXC_DMA_SSI2_24BIT_RX0;
			}
		} else if (dai_port == IMX_DAI_SSI3) {
			if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
				if (format == SNDRV_PCM_FORMAT_S16_LE)
					transfer = MXC_DMA_SSI2_16BIT_TX1;
				else if (format == SNDRV_PCM_FORMAT_S24_LE)
					transfer = MXC_DMA_SSI2_24BIT_TX1;
				else if (format == SNDRV_PCM_FORMAT_S20_3LE)
					transfer = MXC_DMA_SSI2_24BIT_TX1;
			} else {
				if (format == SNDRV_PCM_FORMAT_S16_LE)
					transfer = MXC_DMA_SSI2_16BIT_RX1;
				else if (format == SNDRV_PCM_FORMAT_S24_LE)
					transfer = MXC_DMA_SSI2_24BIT_RX1;
				else if (format == SNDRV_PCM_FORMAT_S20_3LE)
					transfer = MXC_DMA_SSI2_24BIT_RX1;
			}
		} else if ((dai_port & IMX_DAI_ESAI_TX)
			   || (dai_port & IMX_DAI_ESAI_RX)) {
			if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
				if (format == SNDRV_PCM_FORMAT_S16_LE)
					transfer = MXC_DMA_ESAI_16BIT_TX;
				else if (format == SNDRV_PCM_FORMAT_S24_LE)
					transfer = MXC_DMA_ESAI_24BIT_TX;
				else if (format == SNDRV_PCM_FORMAT_S20_3LE)
					transfer = MXC_DMA_ESAI_24BIT_TX;
			} else {
				if (format == SNDRV_PCM_FORMAT_S16_LE)
					transfer = MXC_DMA_ESAI_16BIT_RX;
				else if (format == SNDRV_PCM_FORMAT_S24_LE)
					transfer = MXC_DMA_ESAI_24BIT_RX;
				else if (format == SNDRV_PCM_FORMAT_S20_3LE)
					transfer = MXC_DMA_ESAI_24BIT_RX;
			}
		}
#if defined(CONFIG_MXC_ASRC) || defined(CONFIG_MXC_ASRC_MODULE)
	}
#endif
	return transfer;
}

static int dma_new_period(struct snd_pcm_substream *substream)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct mxc_runtime_data *prtd = runtime->private_data;
	unsigned int dma_size = frames_to_bytes(runtime, runtime->period_size);
	unsigned int offset = dma_size * prtd->period;
	int ret = 0;
	mxc_dma_requestbuf_t sdma_request;

	if (!prtd->active)
		return 0;

	memset(&sdma_request, 0, sizeof(mxc_dma_requestbuf_t));

	dbg("period pos  ALSA %x DMA %x\n", runtime->periods, prtd->period);
	dbg("period size ALSA %x DMA %x Offset %x dmasize %x\n",
	    (unsigned int)runtime->period_size,
	    runtime->dma_bytes, offset, dma_size);
	dbg("DMA addr %x\n", runtime->dma_addr + offset);

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		sdma_request.src_addr =
		    (dma_addr_t) (runtime->dma_addr + offset);
	else
		sdma_request.dst_addr =
		    (dma_addr_t) (runtime->dma_addr + offset);

	sdma_request.num_of_bytes = dma_size;

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		mxc_dma_config(prtd->dma_wchannel,
			       &sdma_request, 1, MXC_DMA_MODE_WRITE);
		ret = mxc_dma_enable(prtd->dma_wchannel);
	} else {

		mxc_dma_config(prtd->dma_wchannel,
			       &sdma_request, 1, MXC_DMA_MODE_READ);
		ret = mxc_dma_enable(prtd->dma_wchannel);
	}
	prtd->dma_active = 1;
	prtd->period++;
	prtd->period %= runtime->periods;

	return ret;
}

static void audio_dma_irq(void *data)
{
	struct snd_pcm_substream *substream = (struct snd_pcm_substream *)data;
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct mxc_runtime_data *prtd = runtime->private_data;

	prtd->dma_active = 0;
	prtd->periods++;
	prtd->periods %= runtime->periods;

	dbg("irq per %d offset %x\n", prtd->periods,
	    frames_to_bytes(runtime, runtime->period_size) * prtd->periods);

	if (prtd->active)
		snd_pcm_period_elapsed(substream);
	dma_new_period(substream);
}

static int imx_pcm_prepare(struct snd_pcm_substream *substream)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct mxc_runtime_data *prtd = runtime->private_data;
	int ret = 0, channel = 0;

	if (prtd->dma_alloc) {
		mxc_dma_free(prtd->dma_wchannel);
		prtd->dma_alloc = 0;
	}

	/* only allocate the DMA chn once */
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
#if defined(CONFIG_MXC_ASRC) || defined(CONFIG_MXC_ASRC_MODULE)
		if (prtd->asrc_enable == 1) {
			struct dma_channel_info info;
			mxc_dma_requestbuf_t sdma_request;
			info.asrc.channs = runtime->channels;
			if (prtd->dma_asrc) {
				mxc_dma_free(prtd->dma_asrc);
				prtd->dma_asrc = 0;
			}
			memset(&sdma_request, 0, sizeof(mxc_dma_requestbuf_t));
			/* num_of_bytes can be set any value except for zero */
			sdma_request.num_of_bytes = 0x40;
			channel =
			    mxc_dma_request_ext(prtd->dma_ch,
						"ALSA TX SDMA", &info);

			mxc_dma_config(channel, &sdma_request,
				       1, MXC_DMA_MODE_WRITE);
			prtd->dma_asrc = channel;
			if (prtd->asrc_index == 0)
				prtd->dma_ch = MXC_DMA_ASRC_A_RX;
			else if (prtd->asrc_index == 1)
				prtd->dma_ch = MXC_DMA_ASRC_B_RX;
			else
				prtd->dma_ch = MXC_DMA_ASRC_C_RX;

			channel =
			    mxc_dma_request(MXC_DMA_ASRC_A_RX, "ALSA ASRC RX");
		} else
			channel = mxc_dma_request(prtd->dma_ch, "ALSA TX SDMA");
#else
		channel = mxc_dma_request(prtd->dma_ch, "ALSA TX SDMA");
#endif
		if (channel < 0) {
			pr_err("imx-pcm: error requesting \
					a write dma channel\n");
			return channel;
		}
		ret = mxc_dma_callback_set(channel, (mxc_dma_callback_t)
					   audio_dma_irq, (void *)substream);

	} else {
		channel = mxc_dma_request(prtd->dma_ch, "ALSA RX SDMA");
		if (channel < 0) {
			pr_err("imx-pcm: error requesting \
				a read dma channel\n");
			return channel;
		}
		ret = mxc_dma_callback_set(channel, (mxc_dma_callback_t)
					   audio_dma_irq, (void *)substream);
	}
	prtd->dma_wchannel = channel;
	prtd->dma_alloc = 1;

	prtd->period = 0;
	prtd->periods = 0;
	return 0;
}

static int imx_pcm_hw_params(struct snd_pcm_substream
			     *substream, struct snd_pcm_hw_params *params)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct mxc_runtime_data *prtd = runtime->private_data;
	struct snd_soc_pcm_runtime *rtd = substream->private_data;

	prtd->dma_ch =
	    imx_get_sdma_transfer(params_format(params),
				  rtd->dai->cpu_dai->id, substream);

	if (prtd->dma_ch < 0) {
		printk(KERN_ERR "imx-pcm: invaild sdma transfer type");
		return -1;
	}

	snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);

	return 0;
}

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

	if (prtd->dma_wchannel) {
		mxc_dma_free(prtd->dma_wchannel);
		prtd->dma_wchannel = 0;
		prtd->dma_alloc = 0;
	}
#if defined(CONFIG_MXC_ASRC) || defined(CONFIG_MXC_ASRC_MODULE)
	if ((prtd->asrc_enable == 1) && prtd->dma_asrc) {
		mxc_dma_free(prtd->dma_asrc);
		prtd->dma_asrc = 0;
	}
#endif

	return 0;
}

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

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
		prtd->dma_active = 0;
		prtd->active = 1;
		ret = dma_new_period(substream);
		ret = dma_new_period(substream);
#if defined(CONFIG_MXC_ASRC) || defined(CONFIG_MXC_ASRC_MODULE)
		if (prtd->asrc_enable == 1) {
			ret = mxc_dma_enable(prtd->dma_asrc);
			asrc_start_conv(prtd->asrc_index);
			/* There is underrun, if immediately enable SSI after
			   start ASRC */
			mdelay(1);
		}
#endif
		break;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
		prtd->active = 0;
#if defined(CONFIG_MXC_ASRC) || defined(CONFIG_MXC_ASRC_MODULE)
		if (prtd->asrc_enable == 1) {
			mxc_dma_disable(prtd->dma_asrc);
			asrc_stop_conv(prtd->asrc_index);
		}
#endif
		break;
	default:
		ret = -EINVAL;
		break;
	}

	return ret;
}

static snd_pcm_uframes_t imx_pcm_pointer(struct
					 snd_pcm_substream
					 *substream)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct mxc_runtime_data *prtd = runtime->private_data;
	unsigned int offset = 0;

	offset = (runtime->period_size * (prtd->periods));
	if (offset >= runtime->buffer_size)
		offset = 0;
	dbg("pointer offset %x\n", offset);

	return offset;
}

static int imx_pcm_open(struct snd_pcm_substream *substream)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct mxc_runtime_data *prtd;
	int ret;

	snd_soc_set_runtime_hwparams(substream, &imx_pcm_hardware);

	ret = snd_pcm_hw_constraint_integer(runtime,
					    SNDRV_PCM_HW_PARAM_PERIODS);
	if (ret < 0)
		return ret;

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

	runtime->private_data = prtd;
	return 0;
}

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

	kfree(prtd);
	return 0;
}

static int
imx_pcm_mmap(struct snd_pcm_substream *substream, struct vm_area_struct *vma)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_device *socdev = rtd->socdev;
	struct snd_soc_dai *cpu_dai = socdev->card->dai_link->cpu_dai;
	struct mxc_audio_platform_data *dev_data;
	int ext_ram = 0;
	int ret = 0;

	dbg("+imx_pcm_mmap:"
	    "UseIram=%d dma_addr=%x dma_area=%x dma_bytes=%d\n",
	    UseIram, (unsigned int)runtime->dma_addr,
	    runtime->dma_area, runtime->dma_bytes);

	if (cpu_dai->dev && cpu_dai->dev->platform_data) {
		dev_data = cpu_dai->dev->platform_data;
		ext_ram = dev_data->ext_ram;
	}

	if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE)
	    || ext_ram || !UseIram) {
		ret =
		    dma_mmap_writecombine(substream->pcm->card->
					  dev, vma,
					  runtime->dma_area,
					  runtime->dma_addr,
					  runtime->dma_bytes);
		return ret;
	} else
		return imx_iram_audio_playback_mmap(substream, vma);
}

struct snd_pcm_ops imx_pcm_ops = {
	.open = imx_pcm_open,
	.close = imx_pcm_close,
	.ioctl = snd_pcm_lib_ioctl,
	.hw_params = imx_pcm_hw_params,
	.hw_free = imx_pcm_hw_free,
	.prepare = imx_pcm_prepare,
	.trigger = imx_pcm_trigger,
	.pointer = imx_pcm_pointer,
	.mmap = imx_pcm_mmap,
};

static int imx_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;
	struct snd_soc_pcm_runtime *rtd = pcm->private_data;
	struct snd_soc_device *socdev = rtd->socdev;
	struct snd_soc_dai *cpu_dai = socdev->card->dai_link->cpu_dai;
	struct mxc_audio_platform_data *dev_data;
	unsigned long buf_paddr;
	int ext_ram = 0;
	size_t size = imx_pcm_hardware.buffer_bytes_max;

	if (cpu_dai->dev && cpu_dai->dev->platform_data) {
		dev_data = cpu_dai->dev->platform_data;
		ext_ram = dev_data->ext_ram;
	}

	buf->dev.type = SNDRV_DMA_TYPE_DEV;
	buf->dev.dev = pcm->card->dev;
	buf->private_data = NULL;

	if ((stream == SNDRV_PCM_STREAM_CAPTURE) || ext_ram || !UseIram)
		buf->area =
		    dma_alloc_writecombine(pcm->card->dev, size,
					   &buf->addr, GFP_KERNEL);
	else {
		buf->area = iram_alloc(size, &buf_paddr);
		buf->addr = buf_paddr;

		if (!buf->area) {
			pr_warning("imx-pcm: Falling back to external ram.\n");
			UseIram = 0;
			buf->area =
			    dma_alloc_writecombine(pcm->card->dev, size,
						   &buf->addr, GFP_KERNEL);
		}
	}

	if (!buf->area)
		return -ENOMEM;
	buf->bytes = size;
	printk(KERN_INFO "DMA Sound Buffers Allocated:"
	       "UseIram=%d buf->addr=%x buf->area=%p size=%d\n",
	       UseIram, buf->addr, buf->area, size);
	return 0;
}

static void imx_pcm_free_dma_buffers(struct snd_pcm *pcm)
{
	struct snd_pcm_substream *substream;
	struct snd_dma_buffer *buf;
	struct snd_soc_pcm_runtime *rtd = pcm->private_data;
	struct snd_soc_device *socdev = rtd->socdev;
	struct snd_soc_dai *cpu_dai = socdev->card->dai_link->cpu_dai;
	struct mxc_audio_platform_data *dev_data;
	int ext_ram = 0;
	int stream;

	if (cpu_dai->dev && cpu_dai->dev->platform_data) {
		dev_data = cpu_dai->dev->platform_data;
		ext_ram = dev_data->ext_ram;
	}

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

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

		if ((stream == SNDRV_PCM_STREAM_CAPTURE)
		    || ext_ram || !UseIram)
			dma_free_writecombine(pcm->card->dev,
					      buf->bytes, buf->area, buf->addr);
		else {
			iram_free(buf->addr, imx_pcm_hardware.buffer_bytes_max);
		}
		buf->area = NULL;
	}
}

static u64 imx_pcm_dmamask = 0xffffffff;

static int imx_pcm_new(struct snd_card *card,
		       struct snd_soc_dai *dai, struct snd_pcm *pcm)
{
	int ret = 0;

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

	if (dai->playback.channels_min) {
		ret = imx_pcm_preallocate_dma_buffer(pcm,
						     SNDRV_PCM_STREAM_PLAYBACK);
		if (ret)
			goto out;
	}

	if (dai->capture.channels_min) {
		ret = imx_pcm_preallocate_dma_buffer(pcm,
						     SNDRV_PCM_STREAM_CAPTURE);
		if (ret)
			goto out;
	}
      out:
	return ret;
}

struct snd_soc_platform imx_soc_platform = {
	.name = "imx-audio",
	.pcm_ops = &imx_pcm_ops,
	.pcm_new = imx_pcm_new,
	.pcm_free = imx_pcm_free_dma_buffers,
};
EXPORT_SYMBOL_GPL(imx_soc_platform);

static int __init imx_pcm_init(void)
{
	return snd_soc_register_platform(&imx_soc_platform);
}
module_init(imx_pcm_init);

static void __exit imx_pcm_exit(void)
{
	snd_soc_unregister_platform(&imx_soc_platform);
}
module_exit(imx_pcm_exit);

MODULE_AUTHOR("Liam Girdwood");
MODULE_DESCRIPTION("Freescale i.MX3x PCM DMA module");
MODULE_LICENSE("GPL");