summaryrefslogtreecommitdiff
path: root/sound/soc/imx/imx-esai.c
blob: 9b4d1ace242177212ad7c92f6bf8ad78bfce5ed5 (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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
/*
 * Copyright 2008-2009 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"

/*#define IMX_ESAI_DUMP 1*/

#ifdef IMX_ESAI_DUMP
#define ESAI_DUMP() \
	do {pr_info("dump @ %s\n", __func__); \
	pr_info("ecr %x\n", __raw_readl(ESAI_ECR)); \
	pr_info("esr %x\n", __raw_readl(ESAI_ESR)); \
	pr_info("tfcr %x\n", __raw_readl(ESAI_TFCR)); \
	pr_info("tfsr %x\n", __raw_readl(ESAI_TFSR)); \
	pr_info("rfcr %x\n", __raw_readl(ESAI_RFCR)); \
	pr_info("rfsr %x\n", __raw_readl(ESAI_RFSR)); \
	pr_info("tsr %x\n", __raw_readl(ESAI_TSR)); \
	pr_info("saisr %x\n", __raw_readl(ESAI_SAISR)); \
	pr_info("saicr %x\n", __raw_readl(ESAI_SAICR)); \
	pr_info("tcr %x\n", __raw_readl(ESAI_TCR)); \
	pr_info("tccr %x\n", __raw_readl(ESAI_TCCR)); \
	pr_info("rcr %x\n", __raw_readl(ESAI_RCR)); \
	pr_info("rccr %x\n", __raw_readl(ESAI_RCCR)); \
	pr_info("tsma %x\n", __raw_readl(ESAI_TSMA)); \
	pr_info("tsmb %x\n", __raw_readl(ESAI_TSMB)); \
	pr_info("rsma %x\n", __raw_readl(ESAI_RSMA)); \
	pr_info("rsmb %x\n", __raw_readl(ESAI_RSMB)); \
	pr_info("prrc %x\n", __raw_readl(ESAI_PRRC)); \
	pr_info("pcrc %x\n", __raw_readl(ESAI_PCRC)); } while (0);
#else
#define ESAI_DUMP()
#endif

#define ESAI_IO_BASE_ADDR	IO_ADDRESS(ESAI_BASE_ADDR)

#define ESAI_ETDR	(ESAI_IO_BASE_ADDR + 0x00)
#define ESAI_ERDR	(ESAI_IO_BASE_ADDR + 0x04)
#define ESAI_ECR	(ESAI_IO_BASE_ADDR + 0x08)
#define ESAI_ESR	(ESAI_IO_BASE_ADDR + 0x0C)
#define ESAI_TFCR	(ESAI_IO_BASE_ADDR + 0x10)
#define ESAI_TFSR	(ESAI_IO_BASE_ADDR + 0x14)
#define ESAI_RFCR	(ESAI_IO_BASE_ADDR + 0x18)
#define ESAI_RFSR	(ESAI_IO_BASE_ADDR + 0x1C)
#define ESAI_TX0	(ESAI_IO_BASE_ADDR + 0x80)
#define ESAI_TX1	(ESAI_IO_BASE_ADDR + 0x84)
#define ESAI_TX2	(ESAI_IO_BASE_ADDR + 0x88)
#define ESAI_TX3	(ESAI_IO_BASE_ADDR + 0x8C)
#define ESAI_TX4	(ESAI_IO_BASE_ADDR + 0x90)
#define ESAI_TX5	(ESAI_IO_BASE_ADDR + 0x94)
#define ESAI_TSR	(ESAI_IO_BASE_ADDR + 0x98)
#define ESAI_RX0	(ESAI_IO_BASE_ADDR + 0xA0)
#define ESAI_RX1	(ESAI_IO_BASE_ADDR + 0xA4)
#define ESAI_RX2	(ESAI_IO_BASE_ADDR + 0xA8)
#define ESAI_RX3	(ESAI_IO_BASE_ADDR + 0xAC)
#define ESAI_SAISR	(ESAI_IO_BASE_ADDR + 0xCC)
#define ESAI_SAICR	(ESAI_IO_BASE_ADDR + 0xD0)
#define ESAI_TCR	(ESAI_IO_BASE_ADDR + 0xD4)
#define ESAI_TCCR	(ESAI_IO_BASE_ADDR + 0xD8)
#define ESAI_RCR	(ESAI_IO_BASE_ADDR + 0xDC)
#define ESAI_RCCR	(ESAI_IO_BASE_ADDR + 0xE0)
#define ESAI_TSMA	(ESAI_IO_BASE_ADDR + 0xE4)
#define ESAI_TSMB	(ESAI_IO_BASE_ADDR + 0xE8)
#define ESAI_RSMA	(ESAI_IO_BASE_ADDR + 0xEC)
#define ESAI_RSMB	(ESAI_IO_BASE_ADDR + 0xF0)
#define ESAI_PRRC	(ESAI_IO_BASE_ADDR + 0xF8)
#define ESAI_PCRC	(ESAI_IO_BASE_ADDR + 0xFC)

#define ESAI_ECR_ETI	(1 << 19)
#define ESAI_ECR_ETO	(1 << 18)
#define ESAI_ECR_ERI	(1 << 17)
#define ESAI_ECR_ERO	(1 << 16)
#define ESAI_ECR_ERST	(1 << 1)
#define ESAI_ECR_ESAIEN	(1 << 0)

#define ESAI_ESR_TINIT	(1 << 10)
#define ESAI_ESR_RFF	(1 << 9)
#define ESAI_ESR_TFE	(1 << 8)
#define ESAI_ESR_TLS	(1 << 7)
#define ESAI_ESR_TDE	(1 << 6)
#define ESAI_ESR_TED	(1 << 5)
#define ESAI_ESR_TD	(1 << 4)
#define ESAI_ESR_RLS	(1 << 3)
#define ESAI_ESR_RDE	(1 << 2)
#define ESAI_ESR_RED	(1 << 1)
#define ESAI_ESR_RD	(1 << 0)

#define ESAI_TFCR_TIEN	(1 << 19)
#define ESAI_TFCR_TE5	(1 << 7)
#define ESAI_TFCR_TE4	(1 << 6)
#define ESAI_TFCR_TE3	(1 << 5)
#define ESAI_TFCR_TE2	(1 << 4)
#define ESAI_TFCR_TE1	(1 << 3)
#define ESAI_TFCR_TE0	(1 << 2)
#define ESAI_TFCR_TFR	(1 << 1)
#define ESAI_TFCR_TFEN	(1 << 0)
#define ESAI_TFCR_TE(x) ((0x3f >> (6 - ((x + 1) >> 1))) << 2)
#define ESAI_TFCR_TE_MASK	0xfff03
#define ESAI_TFCR_TFWM(x)	((x - 1) << 8)
#define ESAI_TFCR_TWA_MASK	0xf8ffff

#define ESAI_RFCR_REXT	(1 << 19)
#define ESAI_RFCR_RE3	(1 << 5)
#define ESAI_RFCR_RE2	(1 << 4)
#define ESAI_RFCR_RE1	(1 << 3)
#define ESAI_RFCR_RE0	(1 << 2)
#define ESAI_RFCR_RFR	(1 << 1)
#define ESAI_RFCR_RFEN	(1 << 0)
#define ESAI_RFCR_RE(x) ((0xf >> (4 - ((x + 1) >> 1))) << 2)
#define ESAI_RFCR_RE_MASK	0xfffc3
#define ESAI_RFCR_RFWM(x)       ((x-1) << 8)
#define ESAI_RFCR_RWA_MASK	0xf8ffff

#define ESAI_WORD_LEN_32	(0x00 << 16)
#define ESAI_WORD_LEN_28	(0x01 << 16)
#define ESAI_WORD_LEN_24	(0x02 << 16)
#define ESAI_WORD_LEN_20	(0x03 << 16)
#define ESAI_WORD_LEN_16	(0x04 << 16)
#define ESAI_WORD_LEN_12	(0x05 << 16)
#define ESAI_WORD_LEN_8	(0x06 << 16)
#define ESAI_WORD_LEN_4	(0x07 << 16)

#define ESAI_SAISR_TODFE	(1 << 17)
#define ESAI_SAISR_TEDE	(1 << 16)
#define ESAI_SAISR_TDE	(1 << 15)
#define ESAI_SAISR_TUE	(1 << 14)
#define ESAI_SAISR_TFS	(1 << 13)
#define ESAI_SAISR_RODF	(1 << 10)
#define ESAI_SAISR_REDF	(1 << 9)
#define ESAI_SAISR_RDF	(1 << 8)
#define ESAI_SAISR_ROE	(1 << 7)
#define ESAI_SAISR_RFS	(1 << 6)
#define ESAI_SAISR_IF2	(1 << 2)
#define ESAI_SAISR_IF1	(1 << 1)
#define ESAI_SAISR_IF0	(1 << 0)

#define ESAI_SAICR_ALC	(1 << 8)
#define ESAI_SAICR_TEBE	(1 << 7)
#define ESAI_SAICR_SYNC	(1 << 6)
#define ESAI_SAICR_OF2	(1 << 2)
#define ESAI_SAICR_OF1	(1 << 1)
#define ESAI_SAICR_OF0	(1 << 0)

#define ESAI_TCR_TLIE	(1 << 23)
#define ESAI_TCR_TIE	(1 << 22)
#define ESAI_TCR_TEDIE	(1 << 21)
#define ESAI_TCR_TEIE	(1 << 20)
#define ESAI_TCR_TPR	(1 << 19)
#define ESAI_TCR_PADC	(1 << 17)
#define ESAI_TCR_TFSR	(1 << 16)
#define ESAI_TCR_TFSL	(1 << 15)
#define ESAI_TCR_TWA	(1 << 7)
#define ESAI_TCR_TSHFD_MSB	(0 << 6)
#define ESAI_TCR_TSHFD_LSB	(1 << 6)
#define ESAI_TCR_TE5	(1 << 5)
#define ESAI_TCR_TE4	(1 << 4)
#define ESAI_TCR_TE3	(1 << 3)
#define ESAI_TCR_TE2	(1 << 2)
#define ESAI_TCR_TE1	(1 << 1)
#define ESAI_TCR_TE0	(1 << 0)
#define ESAI_TCR_TE(x) (0x3f >> (6 - ((x + 1) >> 1)))

#define ESAI_TCR_TSWS_MASK	0xff83ff
#define ESAI_TCR_TSWS_STL8_WDL8	(0x00 << 10)
#define ESAI_TCR_TSWS_STL12_WDL8	(0x04 << 10)
#define ESAI_TCR_TSWS_STL12_WDL12	(0x01 << 10)
#define ESAI_TCR_TSWS_STL16_WDL8	(0x08 << 10)
#define ESAI_TCR_TSWS_STL16_WDL12	(0x05 << 10)
#define ESAI_TCR_TSWS_STL16_WDL16	(0x02 << 10)
#define ESAI_TCR_TSWS_STL20_WDL8	(0x0c << 10)
#define ESAI_TCR_TSWS_STL20_WDL12	(0x09 << 10)
#define ESAI_TCR_TSWS_STL20_WDL16	(0x06 << 10)
#define ESAI_TCR_TSWS_STL20_WDL20	(0x03 << 10)
#define ESAI_TCR_TSWS_STL24_WDL8	(0x10 << 10)
#define ESAI_TCR_TSWS_STL24_WDL12	(0x0d << 10)
#define ESAI_TCR_TSWS_STL24_WDL16	(0x0a << 10)
#define ESAI_TCR_TSWS_STL24_WDL20	(0x07 << 10)
#define ESAI_TCR_TSWS_STL24_WDL24	(0x1e << 10)
#define ESAI_TCR_TSWS_STL32_WDL8	(0x18 << 10)
#define ESAI_TCR_TSWS_STL32_WDL12	(0x15 << 10)
#define ESAI_TCR_TSWS_STL32_WDL16	(0x12 << 10)
#define ESAI_TCR_TSWS_STL32_WDL20	(0x0f << 10)
#define ESAI_TCR_TSWS_STL32_WDL24	(0x1f << 10)

#define ESAI_TCR_TMOD_MASK	0xfffcff
#define ESAI_TCR_TMOD_NORMAL	(0x00 << 8)
#define ESAI_TCR_TMOD_ONDEMAND	(0x01 << 8)
#define ESAI_TCR_TMOD_NETWORK	(0x01 << 8)
#define ESAI_TCR_TMOD_RESERVED (0x02 << 8)
#define ESAI_TCR_TMOD_AC97	(0x03 << 8)

#define ESAI_TCCR_THCKD	(1 << 23)
#define ESAI_TCCR_TFSD	(1 << 22)
#define ESAI_TCCR_TCKD	(1 << 21)
#define ESAI_TCCR_THCKP	(1 << 20)
#define ESAI_TCCR_TFSP	(1 << 19)
#define ESAI_TCCR_TCKP	(1 << 18)

#define ESAI_TCCR_TPSR_MASK 0xfffeff
#define ESAI_TCCR_TPSR_BYPASS (1 << 8)
#define ESAI_TCCR_TPSR_DIV8 (0 << 8)

#define ESAI_TCCR_TFP_MASK	0xfc3fff
#define ESAI_TCCR_TFP(x)	((x & 0xf) << 14)

#define ESAI_TCCR_TDC_MASK	0xffc1ff
#define ESAI_TCCR_TDC(x)	(((x) & 0x1f) << 9)

#define ESAI_TCCR_TPM_MASK	0xffff00
#define ESAI_TCCR_TPM(x)	(x & 0xff)

#define ESAI_RCR_RLIE	(1 << 23)
#define ESAI_RCR_RIE	(1 << 22)
#define ESAI_RCR_REDIE	(1 << 21)
#define ESAI_RCR_REIE	(1 << 20)
#define ESAI_RCR_RPR	(1 << 19)
#define ESAI_RCR_RFSR	(1 << 16)
#define ESAI_RCR_RFSL	(1 << 15)
#define ESAI_RCR_RWA	(1 << 7)
#define ESAI_RCR_RSHFD_MSB (0 << 6)
#define ESAI_RCR_RSHFD_LSB (1 << 6)
#define ESAI_RCR_RE3	(1 << 3)
#define ESAI_RCR_RE2	(1 << 2)
#define ESAI_RCR_RE1	(1 << 1)
#define ESAI_RCR_RE0	(1 << 0)
#define ESAI_RCR_RE(x) (0xf >> (4 - ((x + 1) >> 1)))

#define ESAI_TCR_RSWS_MASK	0xff83ff
#define ESAI_RCR_RSWS_STL8_WDL8	(0x00 << 10)
#define ESAI_RCR_RSWS_STL12_WDL8	(0x04 << 10)
#define ESAI_RCR_RSWS_STL12_WDL12	(0x01 << 10)
#define ESAI_RCR_RSWS_STL16_WDL8	(0x08 << 10)
#define ESAI_RCR_RSWS_STL16_WDL12	(0x05 << 10)
#define ESAI_RCR_RSWS_STL16_WDL16	(0x02 << 10)
#define ESAI_RCR_RSWS_STL20_WDL8	(0x0c << 10)
#define ESAI_RCR_RSWS_STL20_WDL12	(0x09 << 10)
#define ESAI_RCR_RSWS_STL20_WDL16	(0x06 << 10)
#define ESAI_RCR_RSWS_STL20_WDL20	(0x03 << 10)
#define ESAI_RCR_RSWS_STL24_WDL8	(0x10 << 10)
#define ESAI_RCR_RSWS_STL24_WDL12	(0x0d << 10)
#define ESAI_RCR_RSWS_STL24_WDL16	(0x0a << 10)
#define ESAI_RCR_RSWS_STL24_WDL20	(0x07 << 10)
#define ESAI_RCR_RSWS_STL24_WDL24	(0x1e << 10)
#define ESAI_RCR_RSWS_STL32_WDL8	(0x18 << 10)
#define ESAI_RCR_RSWS_STL32_WDL12	(0x15 << 10)
#define ESAI_RCR_RSWS_STL32_WDL16	(0x12 << 10)
#define ESAI_RCR_RSWS_STL32_WDL20	(0x0f << 10)
#define ESAI_RCR_RSWS_STL32_WDL24	(0x1f << 10)

#define ESAI_RCR_RMOD_MASK	0xfffcff
#define ESAI_RCR_RMOD_NORMAL	(0x00 << 8)
#define ESAI_RCR_RMOD_ONDEMAND	(0x01 << 8)
#define ESAI_RCR_RMOD_NETWORK	(0x01 << 8)
#define ESAI_RCR_RMOD_RESERVED (0x02 << 8)
#define ESAI_RCR_RMOD_AC97	(0x03 << 8)

#define ESAI_RCCR_RHCKD	(1 << 23)
#define ESAI_RCCR_RFSD	(1 << 22)
#define ESAI_RCCR_RCKD	(1 << 21)
#define ESAI_RCCR_RHCKP	(1 << 20)
#define ESAI_RCCR_RFSP	(1 << 19)
#define ESAI_RCCR_RCKP	(1 << 18)

#define ESAI_RCCR_RPSR_MASK 0xfffeff
#define ESAI_RCCR_RPSR_BYPASS (1 << 8)
#define ESAI_RCCR_RPSR_DIV8 (0 << 8)

#define ESAI_RCCR_RFP_MASK	0xfc3fff
#define ESAI_RCCR_RFP(x)	((x & 0xf) << 14)

#define ESAI_RCCR_RDC_MASK	0xffc1ff
#define ESAI_RCCR_RDC(x)	(((x) & 0x1f) << 9)

#define ESAI_RCCR_RPM_MASK	0xffff00
#define ESAI_RCCR_RPM(x)	(x & 0xff)

#define ESAI_GPIO_ESAI	0xfff

/* ESAI clock source */
#define ESAI_CLK_FSYS	0
#define ESAI_CLK_EXTAL 1

/* ESAI clock divider */
#define ESAI_TX_DIV_PSR	0
#define ESAI_TX_DIV_PM 1
#define ESAI_TX_DIV_FP	2
#define ESAI_RX_DIV_PSR	3
#define ESAI_RX_DIV_PM	4
#define ESAI_RX_DIV_FP	5

static int imx_esai_txrx_state;

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 {
		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;
		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) {
			if (cpu_dai->id & IMX_DAI_ESAI_TX) {
				ecr |= ESAI_ECR_ETI;
				ecr &= ~ESAI_ECR_ETO;
			}
			if (cpu_dai->id & IMX_DAI_ESAI_RX) {
				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;
		tccr |= div;
		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;
		rccr |= div;
		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)
{
	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 (fmt & SND_SOC_DAIFMT_ASYNC)
		saicr &= ~ESAI_SAICR_SYNC;
	else
		saicr |= ESAI_SAICR_SYNC;

	tcr &= ESAI_TCR_TMOD_MASK;
	/* tdm - only for stereo atm */
	if (fmt & SND_SOC_DAIFMT_TDM)
		tcr |= ESAI_TCR_TMOD_NETWORK;
	else
		tcr |= ESAI_TCR_TMOD_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_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *cpu_dai = rtd->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)
{
	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)
{
	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_TCR_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;
	case SNDRV_PCM_FORMAT_S20_3LE:
		rfcr |= ESAI_WORD_LEN_20;
		rcr |= ESAI_RCR_RSHFD_MSB | ESAI_RCR_RSWS_STL32_WDL20;
		break;
	case SNDRV_PCM_FORMAT_S24_LE:
		rfcr |= ESAI_WORD_LEN_24;
		rcr |= ESAI_RCR_RSHFD_MSB | ESAI_RCR_RSWS_STL32_WDL24;
		break;
	}

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

	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)
{
	/* 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);
	} else {
		if (__raw_readl(ESAI_RCR) & ESAI_RCR_RE0)
			return 0;
		return imx_esai_hw_rx_params(substream, params);
	}
}

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

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		tfcr = __raw_readl(ESAI_TFCR);
		reg = __raw_readl(ESAI_TCR);
	} else {
		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 {
			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);
		}
		break;
	default:
		return -EINVAL;
	}

	ESAI_DUMP();
	return 0;
}

static void imx_esai_shutdown(struct snd_pcm_substream
			      *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;

	if (cpu_dai->id & IMX_DAI_ESAI_TX)
		imx_esai_txrx_state &= ~IMX_DAI_ESAI_TX;
	if (cpu_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 platform_device *dev,
			    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 platform_device *dev, 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)
{
	if (!strcmp("imx-esai-tx", dai->name))
		dai->id = IMX_DAI_ESAI_TX;
	else if (!strcmp("imx-esai-rx", dai->name))
		dai->id = IMX_DAI_ESAI_RX;
	else if (!strcmp("imx-esai-txrx", dai->name))
		dai->id = IMX_DAI_ESAI_TXRX;
	else {
		pr_err("%s: invalid device %s\n", __func__, dai->name);
		return -ENODEV;
	}

	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)

struct snd_soc_dai imx_esai_dai = {
	.name = "imx-esai",
	.id = 0,
	.type = SND_SOC_DAI_PCM,
	.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 = {
		.startup = imx_esai_startup,
		.shutdown = imx_esai_shutdown,
		.trigger = imx_esai_trigger,
		.hw_params = imx_esai_hw_params,
		},
	.dai_ops = {
		    .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,
		    },
};

EXPORT_SYMBOL_GPL(imx_esai_dai);

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