summaryrefslogtreecommitdiff
path: root/sound/soc/imx/imx-esai.c
blob: 0c6b234f29ec048914ca8b6e4b731b270f49c043 (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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
 * Copyright 2008-2010 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

 /*!
  * @file       imx-esai.c
  * @brief      this file implements the esai interface
  *             in according to ASoC architeture
  */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/dma-mapping.h>
#include <linux/clk.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-esai.h"
#include "imx-pcm.h"

static int imx_esai_txrx_state;
static struct imx_esai imx_esai_priv[3];
static void __iomem *esai_ioaddr;

static int imx_esai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
				   int clk_id, unsigned int freq, int dir)
{
	u32 ecr, tccr, rccr;

	ecr = __raw_readl(ESAI_ECR);
	tccr = __raw_readl(ESAI_TCCR);
	rccr = __raw_readl(ESAI_RCCR);

	if (dir == SND_SOC_CLOCK_IN) {
		if (cpu_dai->id & IMX_DAI_ESAI_TX)
			tccr &=
			    ~(ESAI_TCCR_THCKD | ESAI_TCCR_TCKD |
			      ESAI_TCCR_TFSD);
		if (cpu_dai->id & IMX_DAI_ESAI_RX)
			rccr &=
			    ~(ESAI_RCCR_RHCKD | ESAI_RCCR_RCKD |
			      ESAI_RCCR_RFSD);
	} else {
			tccr |=
			    ESAI_TCCR_THCKD | ESAI_TCCR_TCKD | ESAI_TCCR_TFSD;
			rccr |=
			    ESAI_RCCR_RHCKD | ESAI_RCCR_RCKD | ESAI_RCCR_RFSD;
		if (clk_id == ESAI_CLK_FSYS) {
			if (cpu_dai->id & IMX_DAI_ESAI_TX)
				ecr &= ~(ESAI_ECR_ETI | ESAI_ECR_ETO);
			if (cpu_dai->id & IMX_DAI_ESAI_RX)
				ecr &= ~(ESAI_ECR_ERI | ESAI_ECR_ERO);
		} else if (clk_id == ESAI_CLK_EXTAL) {
				ecr |= ESAI_ECR_ETI;
				ecr &= ~ESAI_ECR_ETO;
				ecr |= ESAI_ECR_ERI;
				ecr &= ~ESAI_ECR_ERO;
		}
	}

	__raw_writel(ecr, ESAI_ECR);
	if (cpu_dai->id & IMX_DAI_ESAI_TX)
		__raw_writel(tccr, ESAI_TCCR);
	if (cpu_dai->id & IMX_DAI_ESAI_RX)
		__raw_writel(rccr, ESAI_RCCR);

	ESAI_DUMP();

	return 0;
}

static int imx_esai_set_dai_clkdiv(struct snd_soc_dai *cpu_dai,
				   int div_id, int div)
{
	u32 tccr, rccr;

	tccr = __raw_readl(ESAI_TCCR);
	rccr = __raw_readl(ESAI_RCCR);

	switch (div_id) {
	case ESAI_TX_DIV_PSR:
		tccr &= ESAI_TCCR_TPSR_MASK;
		if (div)
			tccr |= ESAI_TCCR_TPSR_BYPASS;
		else
			tccr &= ~ESAI_TCCR_TPSR_DIV8;
		break;
	case ESAI_TX_DIV_PM:
		tccr &= ESAI_TCCR_TPM_MASK;
		tccr |= ESAI_TCCR_TPM(div);
		break;
	case ESAI_TX_DIV_FP:
		tccr &= ESAI_TCCR_TFP_MASK;
		tccr |= ESAI_TCCR_TFP(div);
		break;
	case ESAI_RX_DIV_PSR:
		rccr &= ESAI_RCCR_RPSR_MASK;
		if (div)
			rccr |= ESAI_RCCR_RPSR_BYPASS;
		else
			rccr &= ~ESAI_RCCR_RPSR_DIV8;
		break;
	case ESAI_RX_DIV_PM:
		rccr &= ESAI_RCCR_RPM_MASK;
		rccr |= ESAI_RCCR_RPM(div);
		break;
	case ESAI_RX_DIV_FP:
		rccr &= ESAI_RCCR_RFP_MASK;
		rccr |= ESAI_RCCR_RFP(div);
		break;
		return -EINVAL;
	}
	if (cpu_dai->id & IMX_DAI_ESAI_TX)
		__raw_writel(tccr, ESAI_TCCR);
	if (cpu_dai->id & IMX_DAI_ESAI_RX)
		__raw_writel(rccr, ESAI_RCCR);
	return 0;
}

/*
 * ESAI Network Mode or TDM slots configuration.
 */
static int imx_esai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai,
				     unsigned int mask, int slots)
{
	u32 tccr, rccr;

	if (cpu_dai->id & IMX_DAI_ESAI_TX) {
		tccr = __raw_readl(ESAI_TCCR);

		tccr &= ESAI_TCCR_TDC_MASK;
		tccr |= ESAI_TCCR_TDC(slots - 1);

		__raw_writel(tccr, ESAI_TCCR);
		__raw_writel((mask & 0xffff), ESAI_TSMA);
		__raw_writel(((mask >> 16) & 0xffff), ESAI_TSMB);
	}

	if (cpu_dai->id & IMX_DAI_ESAI_RX) {
		rccr = __raw_readl(ESAI_RCCR);

		rccr &= ESAI_RCCR_RDC_MASK;
		rccr |= ESAI_RCCR_RDC(slots - 1);

		__raw_writel(rccr, ESAI_RCCR);
		__raw_writel((mask & 0xffff), ESAI_RSMA);
		__raw_writel(((mask >> 16) & 0xffff), ESAI_RSMB);
	}

	ESAI_DUMP();

	return 0;
}

/*
 * ESAI DAI format configuration.
 */
static int imx_esai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
{
	struct imx_esai *esai_mode = (struct imx_esai *)cpu_dai->private_data;
	u32 tcr, tccr, rcr, rccr, saicr;

	tcr = __raw_readl(ESAI_TCR);
	tccr = __raw_readl(ESAI_TCCR);
	rcr = __raw_readl(ESAI_RCR);
	rccr = __raw_readl(ESAI_RCCR);
	saicr = __raw_readl(ESAI_SAICR);

	/* DAI mode */
	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_I2S:
		/* data on rising edge of bclk, frame low 1clk before data */
		tcr &= ~ESAI_TCR_TFSL;
		tcr |= ESAI_TCR_TFSR;
		rcr &= ~ESAI_RCR_RFSL;
		rcr |= ESAI_RCR_RFSR;
		break;
	case SND_SOC_DAIFMT_LEFT_J:
		/* data on rising edge of bclk, frame high with data */
		tcr &= ~(ESAI_TCR_TFSL | ESAI_TCR_TFSR);
		rcr &= ~(ESAI_RCR_RFSL | ESAI_RCR_RFSR);
		break;
	case SND_SOC_DAIFMT_DSP_B:
		/* data on rising edge of bclk, frame high with data */
		tcr |= ESAI_TCR_TFSL;
		rcr |= ESAI_RCR_RFSL;
		break;
	case SND_SOC_DAIFMT_DSP_A:
		/* data on rising edge of bclk, frame high 1clk before data */
		tcr |= ESAI_TCR_TFSL;
		rcr |= ESAI_RCR_RFSL;
		break;
	}

	/* DAI clock inversion */
	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
	case SND_SOC_DAIFMT_IB_IF:
		tccr |= ESAI_TCCR_TFSP;
		tccr &= ~(ESAI_TCCR_TCKP | ESAI_TCCR_THCKP);
		rccr &= ~(ESAI_RCCR_RCKP | ESAI_RCCR_RHCKP);
		rccr |= ESAI_RCCR_RFSP;
		break;
	case SND_SOC_DAIFMT_IB_NF:
		tccr &= ~(ESAI_TCCR_TCKP | ESAI_TCCR_THCKP | ESAI_TCCR_TFSP);
		rccr &= ~(ESAI_RCCR_RCKP | ESAI_RCCR_RHCKP | ESAI_RCCR_RFSP);
		break;
	case SND_SOC_DAIFMT_NB_IF:
		tccr |= ESAI_TCCR_TCKP | ESAI_TCCR_THCKP | ESAI_TCCR_TFSP;
		rccr |= ESAI_RCCR_RCKP | ESAI_RCCR_RHCKP | ESAI_RCCR_RFSP;
		break;
	case SND_SOC_DAIFMT_NB_NF:
		tccr &= ~ESAI_TCCR_TFSP;
		tccr |= ESAI_TCCR_TCKP | ESAI_TCCR_THCKP;
		rccr &= ~ESAI_RCCR_RFSP;
		rccr |= ESAI_RCCR_RCKP | ESAI_RCCR_RHCKP;
		break;
	}

	/* DAI clock master masks */
	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBM_CFM:
		tccr &= ~(ESAI_TCCR_TFSD | ESAI_TCCR_TCKD);
		rccr &= ~(ESAI_RCCR_RFSD | ESAI_RCCR_RCKD);
		break;
	case SND_SOC_DAIFMT_CBS_CFM:
		tccr &= ~ESAI_TCCR_TFSD;
		tccr |= ESAI_TCCR_TCKD;
		rccr &= ~ESAI_RCCR_RFSD;
		rccr |= ESAI_RCCR_RCKD;
		break;
	case SND_SOC_DAIFMT_CBM_CFS:
		tccr &= ~ESAI_TCCR_TCKD;
		tccr |= ESAI_TCCR_TFSD;
		rccr &= ~ESAI_RCCR_RCKD;
		rccr |= ESAI_RCCR_RFSD;
		break;
	case SND_SOC_DAIFMT_CBS_CFS:
		tccr |= (ESAI_TCCR_TFSD | ESAI_TCCR_TCKD);
		rccr |= (ESAI_RCCR_RFSD | ESAI_RCCR_RCKD);
	}

	/* sync */
	if (esai_mode->sync_mode)
		saicr |= ESAI_SAICR_SYNC;
	else
		saicr &= ~ESAI_SAICR_SYNC;

	tcr &= ESAI_TCR_TMOD_MASK;
	rcr &= ESAI_RCR_RMOD_MASK;
	if (esai_mode->network_mode) {
		tcr |= ESAI_TCR_TMOD_NETWORK;
		rcr |= ESAI_RCR_RMOD_NETWORK;
	} else {
		tcr |= ESAI_TCR_TMOD_NORMAL;
		rcr |= ESAI_RCR_RMOD_NORMAL;
	}

	if (cpu_dai->id & IMX_DAI_ESAI_TX) {
		__raw_writel(tcr, ESAI_TCR);
		__raw_writel(tccr, ESAI_TCCR);
	}
	if (cpu_dai->id & IMX_DAI_ESAI_RX) {
		__raw_writel(rcr, ESAI_RCR);
		__raw_writel(rccr, ESAI_RCCR);
	}

	__raw_writel(saicr, ESAI_SAICR);

	ESAI_DUMP();
	return 0;
}

static struct clk *esai_clk;

static int fifo_err_counter;

static irqreturn_t esai_irq(int irq, void *dev_id)
{
	if (fifo_err_counter++ % 1000 == 0)
		printk(KERN_ERR
		       "esai_irq SAISR %x fifo_errs=%d\n",
		       __raw_readl(ESAI_SAISR), fifo_err_counter);
	return IRQ_HANDLED;
}

static int imx_esai_startup(struct snd_pcm_substream *substream,
			    struct snd_soc_dai *cpu_dai)
{
	if (cpu_dai->playback.active && (cpu_dai->id & IMX_DAI_ESAI_TX))
		return 0;
	if (cpu_dai->capture.active && (cpu_dai->id & IMX_DAI_ESAI_RX))
		return 0;

	if (!(imx_esai_txrx_state & IMX_DAI_ESAI_TXRX)) {
		if (request_irq(MXC_INT_ESAI, esai_irq, 0, "esai", NULL)) {
			pr_err("%s: failure requesting esai irq\n", __func__);
			return -EBUSY;
		}
		clk_enable(esai_clk);
		__raw_writel(ESAI_ECR_ERST, ESAI_ECR);
		__raw_writel(ESAI_ECR_ESAIEN, ESAI_ECR);

		__raw_writel(ESAI_GPIO_ESAI, ESAI_PRRC);
		__raw_writel(ESAI_GPIO_ESAI, ESAI_PCRC);
	}

	if (cpu_dai->id & IMX_DAI_ESAI_TX) {
		imx_esai_txrx_state |= IMX_DAI_ESAI_TX;
		__raw_writel(ESAI_TCR_TPR, ESAI_TCR);
	}
	if (cpu_dai->id & IMX_DAI_ESAI_RX) {
		imx_esai_txrx_state |= IMX_DAI_ESAI_RX;
		__raw_writel(ESAI_RCR_RPR, ESAI_RCR);
	}

	ESAI_DUMP();
	return 0;
}

/*
 * This function is called to initialize the TX port before enable
 * the tx port.
 */
static int imx_esai_hw_tx_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *params,
				 struct snd_soc_dai *dai)
{
	u32 tcr, tfcr;
	unsigned int channels;

	tcr = __raw_readl(ESAI_TCR);
	tfcr = __raw_readl(ESAI_TFCR);

	tfcr |= ESAI_TFCR_TFR;
	__raw_writel(tfcr, ESAI_TFCR);
	tfcr &= ~ESAI_TFCR_TFR;
	/* DAI data (word) size */
	tfcr &= ESAI_TFCR_TWA_MASK;
	tcr &= ESAI_TCR_TSWS_MASK;
	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
		tfcr |= ESAI_WORD_LEN_16;
		tcr |= ESAI_TCR_TSHFD_MSB | ESAI_TCR_TSWS_STL32_WDL16;
		break;
	case SNDRV_PCM_FORMAT_S20_3LE:
		tfcr |= ESAI_WORD_LEN_20;
		tcr |= ESAI_TCR_TSHFD_MSB | ESAI_TCR_TSWS_STL32_WDL20;
		break;
	case SNDRV_PCM_FORMAT_S24_LE:
		tfcr |= ESAI_WORD_LEN_24;
		tcr |= ESAI_TCR_TSHFD_MSB | ESAI_TCR_TSWS_STL32_WDL24;
		break;
	}

	channels = params_channels(params);
	tfcr &= ESAI_TFCR_TE_MASK;
	tfcr |= ESAI_TFCR_TE(channels);

	tfcr |= ESAI_TFCR_TFWM(64);

	/* Left aligned, Zero padding */
	tcr |= ESAI_TCR_PADC;

	__raw_writel(tcr, ESAI_TCR);
	__raw_writel(tfcr, ESAI_TFCR);

	ESAI_DUMP();
	return 0;
}

/*
 * This function is called to initialize the RX port before enable
 * the rx port.
 */
static int imx_esai_hw_rx_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *params,
				 struct snd_soc_dai *dai)
{
	u32 rcr, rfcr;
	unsigned int channels;

	rcr = __raw_readl(ESAI_RCR);
	rfcr = __raw_readl(ESAI_RFCR);

	rfcr |= ESAI_RFCR_RFR;
	__raw_writel(rfcr, ESAI_RFCR);
	rfcr &= ~ESAI_RFCR_RFR;

	rfcr &= ESAI_RFCR_RWA_MASK;
	rcr &= ESAI_RCR_RSWS_MASK;
	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
		rfcr |= ESAI_WORD_LEN_16;
		rcr |= ESAI_RCR_RSHFD_MSB | ESAI_RCR_RSWS_STL32_WDL16;
		break;
	}

	channels = params_channels(params);
	rfcr &= ESAI_RFCR_RE_MASK;
	rfcr |= ESAI_RFCR_RE(channels);

	rfcr |= ESAI_RFCR_RFWM(64);

	__raw_writel(rcr, ESAI_RCR);
	__raw_writel(rfcr, ESAI_RFCR);
	return 0;
}

/*
 * This function is called to initialize the TX or RX port,
 */
static int imx_esai_hw_params(struct snd_pcm_substream *substream,
			      struct snd_pcm_hw_params *params,
			      struct snd_soc_dai *dai)
{
	/* Tx/Rx config */
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		if (__raw_readl(ESAI_TCR) & ESAI_TCR_TE0)
			return 0;
		return imx_esai_hw_tx_params(substream, params, dai);
	} else {
		if (__raw_readl(ESAI_RCR) & ESAI_RCR_RE1)
			return 0;
		return imx_esai_hw_rx_params(substream, params, dai);
	}
}

static int imx_esai_trigger(struct snd_pcm_substream *substream, int cmd,
			    struct snd_soc_dai *dai)
{
	u32 reg, tfcr = 0, rfcr = 0;
	u32 temp;

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		tfcr = __raw_readl(ESAI_TFCR);
		reg = __raw_readl(ESAI_TCR);
	} else {
		rfcr = __raw_readl(ESAI_RFCR);
		reg = __raw_readl(ESAI_RCR);
	}
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
			tfcr |= ESAI_TFCR_TFEN;
			__raw_writel(tfcr, ESAI_TFCR);
			reg &= ~ESAI_TCR_TPR;
			reg |= ESAI_TCR_TE(substream->runtime->channels);
			__raw_writel(reg, ESAI_TCR);
		} else {
			temp = __raw_readl(ESAI_TCR);
			temp &= ~ESAI_TCR_TPR;
			__raw_writel(temp, ESAI_TCR);
			rfcr |= ESAI_RFCR_RFEN;
			__raw_writel(rfcr, ESAI_RFCR);
			reg &= ~ESAI_RCR_RPR;
			reg |= ESAI_RCR_RE(substream->runtime->channels);
			__raw_writel(reg, ESAI_RCR);
		}
		break;
	case SNDRV_PCM_TRIGGER_SUSPEND:
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
			reg &= ~ESAI_TCR_TE(substream->runtime->channels);
			__raw_writel(reg, ESAI_TCR);
			reg |= ESAI_TCR_TPR;
			__raw_writel(reg, ESAI_TCR);
			tfcr |= ESAI_TFCR_TFR;
			tfcr &= ~ESAI_TFCR_TFEN;
			__raw_writel(tfcr, ESAI_TFCR);
			tfcr &= ~ESAI_TFCR_TFR;
			__raw_writel(tfcr, ESAI_TFCR);
		} else {
			reg &= ~ESAI_RCR_RE(substream->runtime->channels);
			__raw_writel(reg, ESAI_RCR);
			reg |= ESAI_RCR_RPR;
			__raw_writel(reg, ESAI_RCR);
			rfcr |= ESAI_RFCR_RFR;
			rfcr &= ~ESAI_RFCR_RFEN;
			__raw_writel(rfcr, ESAI_RFCR);
			rfcr &= ~ESAI_RFCR_RFR;
			__raw_writel(rfcr, ESAI_RFCR);
		}
		break;
	default:
		return -EINVAL;
	}
	ESAI_DUMP();
	return 0;
}

static void imx_esai_shutdown(struct snd_pcm_substream *substream,
			      struct snd_soc_dai *dai)
{
	if (dai->id & IMX_DAI_ESAI_TX)
		imx_esai_txrx_state &= ~IMX_DAI_ESAI_TX;
	if (dai->id & IMX_DAI_ESAI_RX)
		imx_esai_txrx_state &= ~IMX_DAI_ESAI_RX;

	/* shutdown ESAI if neither Tx or Rx is active */
	if (!(imx_esai_txrx_state & IMX_DAI_ESAI_TXRX)) {
		free_irq(MXC_INT_ESAI, NULL);
		clk_disable(esai_clk);
	}
}

#ifdef CONFIG_PM
static int imx_esai_suspend(struct snd_soc_dai *dai)
{
	if (!dai->active)
		return 0;

	/*do we need to disable any clocks */
	return 0;
}

static int imx_esai_resume(struct snd_soc_dai *dai)
{
	if (!dai->active)
		return 0;

	/* do we need to enable any clocks */
	return 0;
}

#else
#define imx_esai_suspend	NULL
#define imx_esai_resume	NULL
#endif

static int imx_esai_probe(struct platform_device *pdev, struct snd_soc_dai *dai)
{
	imx_esai_txrx_state = 0;

	esai_clk = clk_get(NULL, "esai_clk");

	return 0;
}

static void imx_esai_remove(struct platform_device *pdev,
			    struct snd_soc_dai *dai)
{

	clk_put(esai_clk);
}

#define IMX_ESAI_RATES  SNDRV_PCM_RATE_8000_192000

#define IMX_ESAI_FORMATS \
	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
	SNDRV_PCM_FMTBIT_S24_LE)

static struct snd_soc_dai_ops imx_esai_dai_ops = {
	.startup = imx_esai_startup,
	.shutdown = imx_esai_shutdown,
	.trigger = imx_esai_trigger,
	.hw_params = imx_esai_hw_params,
	.set_sysclk = imx_esai_set_dai_sysclk,
	.set_clkdiv = imx_esai_set_dai_clkdiv,
	.set_fmt = imx_esai_set_dai_fmt,
	.set_tdm_slot = imx_esai_set_dai_tdm_slot,
};

struct snd_soc_dai imx_esai_dai[] = {
	{
	 .name = "imx-esai-tx",
	 .id = IMX_DAI_ESAI_TX,
	 .probe = imx_esai_probe,
	 .remove = imx_esai_remove,
	 .suspend = imx_esai_suspend,
	 .resume = imx_esai_resume,
	 .playback = {
		      .channels_min = 1,
		      .channels_max = 6,
		      .rates = IMX_ESAI_RATES,
		      .formats = IMX_ESAI_FORMATS,
		      },
	 .capture = {
		     .channels_min = 1,
		     .channels_max = 4,
		     .rates = IMX_ESAI_RATES,
		     .formats = IMX_ESAI_FORMATS,
		     },
	 .ops = &imx_esai_dai_ops,
	 .private_data = &imx_esai_priv[0],
	 },
	{
	 .name = "imx-esai-rx",
	 .id = IMX_DAI_ESAI_RX,
	 .probe = imx_esai_probe,
	 .remove = imx_esai_remove,
	 .suspend = imx_esai_suspend,
	 .resume = imx_esai_resume,
	 .playback = {
		      .channels_min = 1,
		      .channels_max = 6,
		      .rates = IMX_ESAI_RATES,
		      .formats = IMX_ESAI_FORMATS,
		      },
	 .capture = {
		     .channels_min = 1,
		     .channels_max = 4,
		     .rates = IMX_ESAI_RATES,
		     .formats = IMX_ESAI_FORMATS,
		     },
	 .ops = &imx_esai_dai_ops,
	 .private_data = &imx_esai_priv[1],
	 },
	{
	 .name = "imx-esai-txrx",
	 .id = IMX_DAI_ESAI_TXRX,
	 .probe = imx_esai_probe,
	 .remove = imx_esai_remove,
	 .suspend = imx_esai_suspend,
	 .resume = imx_esai_resume,
	 .playback = {
		      .channels_min = 1,
		      .channels_max = 6,
		      .rates = IMX_ESAI_RATES,
		      .formats = IMX_ESAI_FORMATS,
		      },
	 .capture = {
		     .channels_min = 1,
		     .channels_max = 4,
		     .rates = IMX_ESAI_RATES,
		     .formats = IMX_ESAI_FORMATS,
		     },
	 .ops = &imx_esai_dai_ops,
	 .private_data = &imx_esai_priv[2],
	 },

};

EXPORT_SYMBOL_GPL(imx_esai_dai);

static int imx_esai_dev_probe(struct platform_device *pdev)
{
	struct resource *res;
	struct mxc_esai_platform_data *plat_data = pdev->dev.platform_data;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res)
		return -ENODEV;

	esai_ioaddr = ioremap(res->start, res->end - res->start + 1);

	if (plat_data->activate_esai_ports)
		plat_data->activate_esai_ports();

	snd_soc_register_dais(imx_esai_dai, ARRAY_SIZE(imx_esai_dai));
	return 0;
}

static int __devexit imx_esai_dev_remove(struct platform_device *pdev)
{

	struct mxc_esai_platform_data *plat_data = pdev->dev.platform_data;
	iounmap(esai_ioaddr);
	if (plat_data->deactivate_esai_ports)
		plat_data->deactivate_esai_ports();

	snd_soc_unregister_dais(imx_esai_dai, ARRAY_SIZE(imx_esai_dai));
	return 0;
}


static struct platform_driver imx_esai_driver = {
	.probe = imx_esai_dev_probe,
	.remove = __devexit_p(imx_esai_dev_remove),
	.driver = {
		   .name = "mxc_esai",
		   },
};

static int __init imx_esai_init(void)
{
	return platform_driver_register(&imx_esai_driver);
}

static void __exit imx_esai_exit(void)
{
	platform_driver_unregister(&imx_esai_driver);
}

module_init(imx_esai_init);
module_exit(imx_esai_exit);

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("i.MX ASoC ESAI driver");
MODULE_LICENSE("GPL");