/
sunxi-daudio.c
1426 lines (1299 loc) · 40.8 KB
/
sunxi-daudio.c
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* sound\soc\sunxi\sunxi-daudio.c
* (C) Copyright 2015-2017
* Allwinner Technology Co., Ltd. <www.allwinnertech.com>
* wolfgang huang <huangjinhui@allwinnertech.com>
*
* some simple description for this code
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/clk.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_address.h>
#include <linux/regmap.h>
#include <linux/dma/sunxi-dma.h>
#include <linux/pinctrl/consumer.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/dmaengine_pcm.h>
#include <sound/pcm_params.h>
#include <sound/initval.h>
#include <sound/soc.h>
#include <linux/delay.h>
#include "sunxi-daudio.h"
#include "sunxi-pcm.h"
#define DRV_NAME "sunxi-daudio"
#define SUNXI_DAUDIO_EXTERNAL_TYPE 1
#define SUNXI_DAUDIO_TDMHDMI_TYPE 2
#define SUNXI_DAUDIO_DRQDST(sunxi_daudio, x) \
((sunxi_daudio)->playback_dma_param.dma_drq_type_num = \
DRQDST_DAUDIO_##x##_TX)
#define SUNXI_DAUDIO_DRQSRC(sunxi_daudio, x) \
((sunxi_daudio)->capture_dma_param.dma_drq_type_num = \
DRQSRC_DAUDIO_##x##_RX)
struct sunxi_daudio_platform_data {
unsigned int daudio_type;
unsigned int external_type;
unsigned int daudio_master;
unsigned int pcm_lrck_period;
unsigned int slot_width_select;
unsigned int audio_format;
unsigned int signal_inversion;
unsigned int frame_type;
unsigned int tdm_config;
unsigned int tdm_num;
unsigned int mclk_div;
};
struct sunxi_daudio_info {
struct device *dev;
struct regmap *regmap;
struct clk *pllclk;
struct clk *moduleclk;
struct mutex mutex;
struct sunxi_dma_params playback_dma_param;
struct sunxi_dma_params capture_dma_param;
struct pinctrl *pinctrl;
struct pinctrl_state *pinstate;
struct pinctrl_state *pinstate_sleep;
struct sunxi_daudio_platform_data *pdata;
unsigned int hub_mode;
unsigned int hdmi_en;
};
static bool daudio_loop_en;
module_param(daudio_loop_en, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(daudio_loop_en,
"SUNXI Digital audio loopback debug(Y=enable, N=disable)");
/*
* Some codec on electric timing need debugging
*/
int daudio_set_clk_onoff(struct snd_soc_dai *dai, u32 mask, u32 onoff)
{
struct sunxi_daudio_info *sunxi_daudio = snd_soc_dai_get_drvdata(dai);
switch (mask) {
case SUNXI_DAUDIO_BCLK:
if (onoff)
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_FIFOCTL,
(1<<BCLK_OUT), (1<<BCLK_OUT));
else
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_FIFOCTL,
(1<<BCLK_OUT), (0<<BCLK_OUT));
break;
case SUNXI_DAUDIO_LRCK:
if (onoff)
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_FIFOCTL,
(1<<LRCK_OUT), (1<<LRCK_OUT));
else
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_FIFOCTL,
(1<<LRCK_OUT), (0<<LRCK_OUT));
break;
case SUNXI_DAUDIO_MCLK:
if (onoff)
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_CLKDIV,
(1<<MCLKOUT_EN), (1<<MCLKOUT_EN));
else
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_CLKDIV,
(1<<MCLKOUT_EN), (0<<MCLKOUT_EN));
break;
case SUNXI_DAUDIO_GEN:
if (onoff)
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_CTL,
(1<<GLOBAL_EN), (1<<GLOBAL_EN));
else
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_CTL,
(1<<GLOBAL_EN), (0<<GLOBAL_EN));
break;
default:
return -EINVAL;
}
return 0;
}
EXPORT_SYMBOL_GPL(daudio_set_clk_onoff);
static int sunxi_daudio_get_hub_mode(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
struct sunxi_daudio_info *sunxi_daudio =
snd_soc_codec_get_drvdata(codec);
unsigned int reg_val;
regmap_read(sunxi_daudio->regmap, SUNXI_DAUDIO_FIFOCTL, ®_val);
ucontrol->value.integer.value[0] = ((reg_val & (1<<HUB_EN)) ? 2 : 1);
return 0;
}
static int sunxi_daudio_set_hub_mode(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
struct sunxi_daudio_info *sunxi_daudio =
snd_soc_codec_get_drvdata(codec);
switch (ucontrol->value.integer.value[0]) {
case 0:
case 1:
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_FIFOCTL,
(1<<HUB_EN), (0<<HUB_EN));
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CTL,
(1<<CTL_TXEN), (0<<CTL_TXEN));
break;
case 2:
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_FIFOCTL,
(1<<HUB_EN), (1<<HUB_EN));
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CTL,
(1<<CTL_TXEN), (1<<CTL_TXEN));
break;
default:
return -EINVAL;
}
return 0;
}
static const char *daudio_format_function[] = {"null",
"hub_disable", "hub_enable"};
static const struct soc_enum daudio_format_enum[] = {
SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(daudio_format_function),
daudio_format_function),
};
/* dts pcm Audio Mode Select */
static const struct snd_kcontrol_new sunxi_daudio_controls[] = {
SOC_ENUM_EXT("sunxi daudio audio hub mode", daudio_format_enum[0],
sunxi_daudio_get_hub_mode, sunxi_daudio_set_hub_mode),
};
static void sunxi_daudio_txctrl_enable(struct sunxi_daudio_info *sunxi_daudio,
int enable)
{
pr_debug("Enter %s, enable %d\n", __func__, enable);
if (enable) {
/* HDMI audio Transmit Clock just enable at startup */
if (sunxi_daudio->pdata->daudio_type
!= SUNXI_DAUDIO_TDMHDMI_TYPE)
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_CTL,
(1<<CTL_TXEN), (1<<CTL_TXEN));
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_INTCTL,
(1<<TXDRQEN), (1<<TXDRQEN));
} else {
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_INTCTL,
(1<<TXDRQEN), (0<<TXDRQEN));
if (sunxi_daudio->pdata->daudio_type
!= SUNXI_DAUDIO_TDMHDMI_TYPE)
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_CTL,
(1<<CTL_TXEN), (0<<CTL_TXEN));
}
pr_debug("End %s, enable %d\n", __func__, enable);
}
static void sunxi_daudio_rxctrl_enable(struct sunxi_daudio_info *sunxi_daudio,
int enable)
{
if (enable) {
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CTL,
(1<<CTL_RXEN), (1<<CTL_RXEN));
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_INTCTL,
(1<<RXDRQEN), (1<<RXDRQEN));
} else {
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_INTCTL,
(1<<RXDRQEN), (0<<RXDRQEN));
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CTL,
(1<<CTL_RXEN), (0<<CTL_RXEN));
}
}
static int sunxi_daudio_global_enable(struct sunxi_daudio_info *sunxi_daudio,
int enable)
{
if (enable) {
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CTL,
(1<<SDO0_EN), (1<<SDO0_EN));
if (sunxi_daudio->hdmi_en) {
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_CTL, (1<<SDO1_EN), (1<<SDO1_EN));
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_CTL, (1<<SDO2_EN), (1<<SDO2_EN));
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_CTL, (1<<SDO3_EN), (1<<SDO3_EN));
}
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CTL,
(1<<GLOBAL_EN), (1<<GLOBAL_EN));
} else {
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CTL,
(1<<GLOBAL_EN), (0<<GLOBAL_EN));
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CTL,
(1<<SDO0_EN), (0<<SDO0_EN));
if (sunxi_daudio->hdmi_en) {
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_CTL, (1<<SDO1_EN), (0<<SDO1_EN));
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_CTL, (1<<SDO2_EN), (0<<SDO2_EN));
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_CTL, (1<<SDO3_EN), (0<<SDO3_EN));
}
}
return 0;
}
static int sunxi_daudio_mclk_setting(struct sunxi_daudio_info *sunxi_daudio)
{
unsigned int mclk_div;
if (sunxi_daudio->pdata->mclk_div) {
switch (sunxi_daudio->pdata->mclk_div) {
case 1:
mclk_div = SUNXI_DAUDIO_MCLK_DIV_1;
break;
case 2:
mclk_div = SUNXI_DAUDIO_MCLK_DIV_2;
break;
case 4:
mclk_div = SUNXI_DAUDIO_MCLK_DIV_3;
break;
case 6:
mclk_div = SUNXI_DAUDIO_MCLK_DIV_4;
break;
case 8:
mclk_div = SUNXI_DAUDIO_MCLK_DIV_5;
break;
case 12:
mclk_div = SUNXI_DAUDIO_MCLK_DIV_6;
break;
case 16:
mclk_div = SUNXI_DAUDIO_MCLK_DIV_7;
break;
case 24:
mclk_div = SUNXI_DAUDIO_MCLK_DIV_8;
break;
case 32:
mclk_div = SUNXI_DAUDIO_MCLK_DIV_9;
break;
case 48:
mclk_div = SUNXI_DAUDIO_MCLK_DIV_10;
break;
case 64:
mclk_div = SUNXI_DAUDIO_MCLK_DIV_11;
break;
case 96:
mclk_div = SUNXI_DAUDIO_MCLK_DIV_12;
break;
case 128:
mclk_div = SUNXI_DAUDIO_MCLK_DIV_13;
break;
case 176:
mclk_div = SUNXI_DAUDIO_MCLK_DIV_14;
break;
case 192:
mclk_div = SUNXI_DAUDIO_MCLK_DIV_15;
break;
default:
dev_err(sunxi_daudio->dev, "unsupport mclk_div\n");
return -EINVAL;
}
/* setting Mclk as external codec input clk */
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CLKDIV,
(SUNXI_DAUDIO_MCLK_DIV_MASK<<MCLK_DIV),
(mclk_div<<MCLK_DIV));
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CLKDIV,
(1<<MCLKOUT_EN), (1<<MCLKOUT_EN));
} else {
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CLKDIV,
(1<<MCLKOUT_EN), (0<<MCLKOUT_EN));
}
return 0;
}
static int sunxi_daudio_init_fmt(struct sunxi_daudio_info *sunxi_daudio,
unsigned int fmt)
{
unsigned int offset, mode;
unsigned int lrck_polarity, brck_polarity;
switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
case SND_SOC_DAIFMT_CBM_CFM:
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CTL,
(SUNXI_DAUDIO_LRCK_OUT_MASK<<LRCK_OUT),
(SUNXI_DAUDIO_LRCK_OUT_DISABLE<<LRCK_OUT));
break;
case SND_SOC_DAIFMT_CBS_CFS:
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CTL,
(SUNXI_DAUDIO_LRCK_OUT_MASK<<LRCK_OUT),
(SUNXI_DAUDIO_LRCK_OUT_ENABLE<<LRCK_OUT));
break;
default:
dev_err(sunxi_daudio->dev, "unknown maser/slave format\n");
return -EINVAL;
}
switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
case SND_SOC_DAIFMT_I2S:
offset = SUNXI_DAUDIO_TX_OFFSET_1;
mode = SUNXI_DAUDIO_MODE_CTL_I2S;
break;
case SND_SOC_DAIFMT_RIGHT_J:
offset = SUNXI_DAUDIO_TX_OFFSET_0;
mode = SUNXI_DAUDIO_MODE_CTL_RIGHT;
break;
case SND_SOC_DAIFMT_LEFT_J:
offset = SUNXI_DAUDIO_TX_OFFSET_0;
mode = SUNXI_DAUDIO_MODE_CTL_LEFT;
break;
case SND_SOC_DAIFMT_DSP_A:
offset = SUNXI_DAUDIO_TX_OFFSET_1;
mode = SUNXI_DAUDIO_MODE_CTL_PCM;
break;
case SND_SOC_DAIFMT_DSP_B:
offset = SUNXI_DAUDIO_TX_OFFSET_0;
mode = SUNXI_DAUDIO_MODE_CTL_PCM;
break;
default:
dev_err(sunxi_daudio->dev, "format setting failed\n");
return -EINVAL;
}
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CTL,
(SUNXI_DAUDIO_MODE_CTL_MASK<<MODE_SEL),
(mode<<MODE_SEL));
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_TX0CHSEL,
(SUNXI_DAUDIO_TX_OFFSET_MASK<<TX_OFFSET),
(offset<<TX_OFFSET));
if (sunxi_daudio->hdmi_en) {
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_TX1CHSEL,
(SUNXI_DAUDIO_TX_OFFSET_MASK<<TX_OFFSET),
(offset<<TX_OFFSET));
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_TX2CHSEL,
(SUNXI_DAUDIO_TX_OFFSET_MASK<<TX_OFFSET),
(offset<<TX_OFFSET));
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_TX3CHSEL,
(SUNXI_DAUDIO_TX_OFFSET_MASK<<TX_OFFSET),
(offset<<TX_OFFSET));
}
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_RXCHSEL,
(SUNXI_DAUDIO_RX_OFFSET_MASK<<RX_OFFSET),
(offset<<RX_OFFSET));
switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
case SND_SOC_DAIFMT_NB_NF:
lrck_polarity = SUNXI_DAUDIO_LRCK_POLARITY_NOR;
brck_polarity = SUNXI_DAUDIO_BCLK_POLARITY_NOR;
break;
case SND_SOC_DAIFMT_NB_IF:
lrck_polarity = SUNXI_DAUDIO_LRCK_POLARITY_INV;
brck_polarity = SUNXI_DAUDIO_BCLK_POLARITY_NOR;
break;
case SND_SOC_DAIFMT_IB_NF:
lrck_polarity = SUNXI_DAUDIO_LRCK_POLARITY_NOR;
brck_polarity = SUNXI_DAUDIO_BCLK_POLARITY_INV;
break;
case SND_SOC_DAIFMT_IB_IF:
lrck_polarity = SUNXI_DAUDIO_LRCK_POLARITY_INV;
brck_polarity = SUNXI_DAUDIO_BCLK_POLARITY_INV;
break;
default:
dev_err(sunxi_daudio->dev, "invert clk setting failed\n");
return -EINVAL;
}
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_FMT0,
(1<<LRCK_POLARITY), (lrck_polarity<<LRCK_POLARITY));
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_FMT0,
(1<<BRCK_POLARITY), (brck_polarity<<BRCK_POLARITY));
return 0;
}
static int sunxi_daudio_init(struct sunxi_daudio_info *sunxi_daudio)
{
struct sunxi_daudio_platform_data *pdat = sunxi_daudio->pdata;
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_FMT0,
(1<<LRCK_WIDTH),
(pdat->frame_type<<LRCK_WIDTH));
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_FMT0,
(SUNXI_DAUDIO_LRCK_PERIOD_MASK)<<LRCK_PERIOD,
((pdat->pcm_lrck_period-1)<<LRCK_PERIOD));
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_FMT0,
(SUNXI_DAUDIO_SLOT_WIDTH_MASK<<SLOT_WIDTH),
(((pdat->slot_width_select>>2)-1)<<SLOT_WIDTH));
/*
* MSB on the transmit format, always be first.
* default using Linear-PCM, without no companding.
* A-law<Eourpean standard> or U-law<US-Japan> not working ok.
*/
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_FMT1, SUNXI_DAUDIO_FMT1_DEF);
sunxi_daudio_init_fmt(sunxi_daudio, (pdat->audio_format
| (pdat->signal_inversion<<SND_SOC_DAIFMT_SIG_SHIFT)
| (pdat->daudio_master<<SND_SOC_DAIFMT_MASTER_SHIFT)));
return sunxi_daudio_mclk_setting(sunxi_daudio);
}
static int sunxi_daudio_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
{
struct sunxi_daudio_info *sunxi_daudio = snd_soc_dai_get_drvdata(dai);
struct snd_soc_pcm_runtime *rtd = substream->private_data;
struct snd_soc_card *card = rtd->card;
struct sunxi_hdmi_priv *sunxi_hdmi = snd_soc_card_get_drvdata(card);
#ifdef SUNXI_HDMI_AUDIO_ENABLE
unsigned int reg_val;
#endif
switch (params_format(params)) {
case SNDRV_PCM_FORMAT_S16_LE:
/*
* Special procesing for hdmi, HDMI card name is
* "sndhdmi" or sndhdmiraw. if card not HDMI,
* strstr func just return NULL, jump to right section.
* Not HDMI card, sunxi_hdmi maybe a NULL pointer.
*/
if (sunxi_daudio->pdata->daudio_type ==
SUNXI_DAUDIO_TDMHDMI_TYPE
&& (sunxi_hdmi->hdmi_format > 1)) {
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_FMT0,
(SUNXI_DAUDIO_SR_MASK<<SAMPLE_RESOLUTION),
(SUNXI_DAUDIO_SR_24BIT<<SAMPLE_RESOLUTION));
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_FIFOCTL,
(SUNXI_DAUDIO_TXIM_MASK<<TXIM),
(SUNXI_DAUDIO_TXIM_VALID_MSB<<TXIM));
} else {
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_FMT0,
(SUNXI_DAUDIO_SR_MASK<<SAMPLE_RESOLUTION),
(SUNXI_DAUDIO_SR_16BIT<<SAMPLE_RESOLUTION));
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_FIFOCTL,
(SUNXI_DAUDIO_TXIM_MASK<<TXIM),
(SUNXI_DAUDIO_TXIM_VALID_LSB<<TXIM));
else
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_FIFOCTL,
(SUNXI_DAUDIO_RXOM_MASK<<RXOM),
(SUNXI_DAUDIO_RXOM_EXPH<<RXOM));
}
break;
case SNDRV_PCM_FORMAT_S20_3LE:
case SNDRV_PCM_FORMAT_S24_LE:
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_FMT0,
(SUNXI_DAUDIO_SR_MASK<<SAMPLE_RESOLUTION),
(SUNXI_DAUDIO_SR_24BIT<<SAMPLE_RESOLUTION));
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_FIFOCTL,
(SUNXI_DAUDIO_TXIM_MASK<<TXIM),
(SUNXI_DAUDIO_TXIM_VALID_LSB<<TXIM));
else
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_FIFOCTL,
(SUNXI_DAUDIO_RXOM_MASK<<RXOM),
(SUNXI_DAUDIO_RXOM_EXPH<<RXOM));
break;
case SNDRV_PCM_FORMAT_S32_LE:
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_FMT0,
(SUNXI_DAUDIO_SR_MASK<<SAMPLE_RESOLUTION),
(SUNXI_DAUDIO_SR_32BIT<<SAMPLE_RESOLUTION));
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_FIFOCTL,
(SUNXI_DAUDIO_TXIM_MASK<<TXIM),
(SUNXI_DAUDIO_TXIM_VALID_LSB<<TXIM));
else
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_FIFOCTL,
(SUNXI_DAUDIO_RXOM_MASK<<RXOM),
(SUNXI_DAUDIO_RXOM_EXPH<<RXOM));
break;
default:
dev_err(sunxi_daudio->dev, "unrecognized format\n");
return -EINVAL;
}
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CHCFG,
(SUNXI_DAUDIO_TX_SLOT_MASK<<TX_SLOT_NUM),
((params_channels(params)-1)<<TX_SLOT_NUM));
if (sunxi_daudio->hdmi_en == 0) {
#ifdef SUNXI_DAUDIO_MODE_B
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX0CHMAP0, SUNXI_DEFAULT_CHMAP0);
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX0CHMAP1, SUNXI_DEFAULT_CHMAP1);
#else
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX0CHMAP0, SUNXI_DEFAULT_CHMAP);
#endif
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX0CHSEL,
(SUNXI_DAUDIO_TX_CHSEL_MASK<<TX_CHSEL),
((params_channels(params)-1)<<TX_CHSEL));
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX0CHSEL,
(SUNXI_DAUDIO_TX_CHEN_MASK<<TX_CHEN),
((1<<params_channels(params))-1)<<TX_CHEN);
} else {
/* HDMI multi-channel processing */
#ifdef SUNXI_HDMI_AUDIO_ENABLE
#ifdef SUNXI_DAUDIO_MODE_B
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX0CHMAP1, 0x10);
if (sunxi_hdmi->hdmi_format > 1) {
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX1CHMAP1, 0x32);
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX2CHMAP1, 0x54);
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX3CHMAP1, 0x76);
} else {
if (params_channels(params) > 2)
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX1CHMAP1, 0x23);
if (params_channels(params) > 4) {
if (params_channels(params) == 6)
regmap_write(
sunxi_daudio->regmap,
SUNXI_DAUDIO_TX2CHMAP1,
0x54);
else
regmap_write(
sunxi_daudio->regmap,
SUNXI_DAUDIO_TX2CHMAP1,
0x76);
}
if (params_channels(params) > 6)
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX3CHMAP1,
0x54);
}
#else
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX0CHMAP0, 0x10);
if (sunxi_hdmi->hdmi_format > 1) {
/* support for HBR */
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX1CHMAP0, 0x32);
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX2CHMAP0, 0x54);
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX3CHMAP0, 0x76);
} else {
/* LPCM 5.1 & 7.1 support */
if (params_channels(params) > 2)
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX1CHMAP0, 0x23);
if (params_channels(params) > 4) {
if (params_channels(params) == 6)
regmap_write(
sunxi_daudio->regmap,
SUNXI_DAUDIO_TX2CHMAP0,
0x54);
else
regmap_write(
sunxi_daudio->regmap,
SUNXI_DAUDIO_TX2CHMAP0,
0x76);
}
if (params_channels(params) > 6)
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX3CHMAP0,
0x54);
}
#endif
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX0CHSEL,
0x01 << TX_CHSEL, 0x01 << TX_CHSEL);
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX0CHSEL,
0x03 << TX_CHEN, 0x03 << TX_CHEN);
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX1CHSEL,
0x01 << TX_CHSEL, 0x01 << TX_CHSEL);
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX1CHSEL,
(0x03)<<TX_CHEN, 0x03 << TX_CHEN);
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX2CHSEL,
0x01 << TX_CHSEL, 0x01 << TX_CHSEL);
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX2CHSEL,
(0x03)<<TX_CHEN, 0x03 << TX_CHEN);
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX3CHSEL,
0x01 << TX_CHSEL, 0x01 << TX_CHSEL);
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_TX3CHSEL,
(0x03)<<TX_CHEN, 0x03 << TX_CHEN);
#endif /* HDMI */
}
} else {
#ifdef SUNXI_DAUDIO_MODE_B
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_RXCHMAP0, SUNXI_DEFAULT_CHMAP0);
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_RXCHMAP1, SUNXI_DEFAULT_CHMAP1);
#else
regmap_write(sunxi_daudio->regmap,
SUNXI_DAUDIO_RXCHMAP, SUNXI_DEFAULT_CHMAP);
#endif
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CHCFG,
(SUNXI_DAUDIO_RX_SLOT_MASK<<RX_SLOT_NUM),
((params_channels(params)-1)<<RX_SLOT_NUM));
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_RXCHSEL,
(SUNXI_DAUDIO_RX_CHSEL_MASK<<RX_CHSEL),
((params_channels(params)-1)<<RX_CHSEL));
}
#ifdef SUNXI_HDMI_AUDIO_ENABLE
/* Special processing for HDMI hub playback to enable hdmi module */
if (sunxi_daudio->pdata->daudio_type == SUNXI_DAUDIO_TDMHDMI_TYPE) {
mutex_lock(&sunxi_daudio->mutex);
regmap_read(sunxi_daudio->regmap,
SUNXI_DAUDIO_FIFOCTL, ®_val);
sunxi_daudio->hub_mode = (reg_val & (1<<HUB_EN));
if (sunxi_daudio->hub_mode) {
sunxi_hdmi_codec_hw_params(substream, params, NULL);
sunxi_hdmi_codec_prepare(substream, NULL);
}
mutex_unlock(&sunxi_daudio->mutex);
}
#endif
return 0;
}
static int sunxi_daudio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
{
struct sunxi_daudio_info *sunxi_daudio = snd_soc_dai_get_drvdata(dai);
sunxi_daudio_init_fmt(sunxi_daudio, fmt);
return 0;
}
static int sunxi_daudio_set_sysclk(struct snd_soc_dai *dai,
int clk_id, unsigned int freq, int dir)
{
struct sunxi_daudio_info *sunxi_daudio = snd_soc_dai_get_drvdata(dai);
if (clk_set_rate(sunxi_daudio->pllclk, freq)) {
dev_err(sunxi_daudio->dev, "set pllclk rate failed\n");
printk("set sysclk error");
return -EBUSY;
}
return 0;
}
static int sunxi_daudio_set_clkdiv(struct snd_soc_dai *dai,
int clk_id, int clk_div)
{
struct sunxi_daudio_info *sunxi_daudio = snd_soc_dai_get_drvdata(dai);
unsigned int bclk_div, div_ratio;
if (sunxi_daudio->pdata->tdm_config)
/* I2S/TDM two channel mode */
div_ratio = clk_div/(2 * sunxi_daudio->pdata->pcm_lrck_period);
else
/* PCM mode */
div_ratio = clk_div / sunxi_daudio->pdata->pcm_lrck_period;
switch (div_ratio) {
case 1:
bclk_div = SUNXI_DAUDIO_BCLK_DIV_1;
break;
case 2:
bclk_div = SUNXI_DAUDIO_BCLK_DIV_2;
break;
case 4:
bclk_div = SUNXI_DAUDIO_BCLK_DIV_3;
break;
case 6:
bclk_div = SUNXI_DAUDIO_BCLK_DIV_4;
break;
case 8:
bclk_div = SUNXI_DAUDIO_BCLK_DIV_5;
break;
case 12:
bclk_div = SUNXI_DAUDIO_BCLK_DIV_6;
break;
case 16:
bclk_div = SUNXI_DAUDIO_BCLK_DIV_7;
break;
case 24:
bclk_div = SUNXI_DAUDIO_BCLK_DIV_8;
break;
case 32:
bclk_div = SUNXI_DAUDIO_BCLK_DIV_9;
break;
case 48:
bclk_div = SUNXI_DAUDIO_BCLK_DIV_10;
break;
case 64:
bclk_div = SUNXI_DAUDIO_BCLK_DIV_11;
break;
case 96:
bclk_div = SUNXI_DAUDIO_BCLK_DIV_12;
break;
case 128:
bclk_div = SUNXI_DAUDIO_BCLK_DIV_13;
break;
case 176:
bclk_div = SUNXI_DAUDIO_BCLK_DIV_14;
break;
case 192:
bclk_div = SUNXI_DAUDIO_BCLK_DIV_15;
break;
default:
dev_err(sunxi_daudio->dev, "unsupport clk_div\n");
return -EINVAL;
}
/* setting bclk to driver external codec bit clk */
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CLKDIV,
(SUNXI_DAUDIO_BCLK_DIV_MASK<<BCLK_DIV),
(bclk_div<<BCLK_DIV));
return 0;
}
static int sunxi_daudio_dai_startup(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
struct sunxi_daudio_info *sunxi_daudio = snd_soc_dai_get_drvdata(dai);
/* FIXME: As HDMI module to play audio, it need at least 1100ms to sync.
* if we not wait we lost audio data to playback, or we wait for 1100ms
* to playback, user experience worst than you can imagine. So we need
* to cutdown that sync time by keeping clock signal on. we just enable
* it at startup and resume, cutdown it at remove and suspend time.
*/
if (sunxi_daudio->pdata->daudio_type == SUNXI_DAUDIO_TDMHDMI_TYPE)
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CTL,
(1<<CTL_TXEN), (1<<CTL_TXEN));
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
snd_soc_dai_set_dma_data(dai, substream,
&sunxi_daudio->playback_dma_param);
else
snd_soc_dai_set_dma_data(dai, substream,
&sunxi_daudio->capture_dma_param);
return 0;
}
static int sunxi_daudio_trigger(struct snd_pcm_substream *substream,
int cmd, struct snd_soc_dai *dai)
{
struct sunxi_daudio_info *sunxi_daudio = snd_soc_dai_get_drvdata(dai);
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) {
if (daudio_loop_en)
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_CTL,
(1<<LOOP_EN), (1<<LOOP_EN));
else
regmap_update_bits(sunxi_daudio->regmap,
SUNXI_DAUDIO_CTL,
(1<<LOOP_EN), (0<<LOOP_EN));
sunxi_daudio_txctrl_enable(sunxi_daudio, 1);
} else {
sunxi_daudio_rxctrl_enable(sunxi_daudio, 1);
}
break;
case SNDRV_PCM_TRIGGER_STOP:
case SNDRV_PCM_TRIGGER_SUSPEND:
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
sunxi_daudio_txctrl_enable(sunxi_daudio, 0);
else
sunxi_daudio_rxctrl_enable(sunxi_daudio, 0);
break;
default:
return -EINVAL;
}
return 0;
}
static int sunxi_daudio_prepare(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
unsigned int i;
struct sunxi_daudio_info *sunxi_daudio = snd_soc_dai_get_drvdata(dai);
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
for (i = 0 ; i < SUNXI_DAUDIO_FTX_TIMES ; i++) {
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_FIFOCTL,
(1<<FIFO_CTL_FTX), (1<<FIFO_CTL_FTX));
mdelay(1);
}
regmap_write(sunxi_daudio->regmap, SUNXI_DAUDIO_TXCNT, 0);
} else {
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_FIFOCTL,
(1<<FIFO_CTL_FRX), (1<<FIFO_CTL_FRX));
regmap_write(sunxi_daudio->regmap, SUNXI_DAUDIO_RXCNT, 0);
}
return 0;
}
static int sunxi_daudio_probe(struct snd_soc_dai *dai)
{
struct sunxi_daudio_info *sunxi_daudio = snd_soc_dai_get_drvdata(dai);
mutex_init(&sunxi_daudio->mutex);
sunxi_daudio_init(sunxi_daudio);
return 0;
}
static void sunxi_daudio_shutdown(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
struct sunxi_daudio_info *sunxi_daudio = snd_soc_dai_get_drvdata(dai);
/* Special processing for HDMI hub playback to shutdown hdmi module */
if (sunxi_daudio->pdata->daudio_type == SUNXI_DAUDIO_TDMHDMI_TYPE) {
mutex_lock(&sunxi_daudio->mutex);
if (sunxi_daudio->hub_mode)
sunxi_hdmi_codec_shutdown(substream, NULL);
mutex_unlock(&sunxi_daudio->mutex);
}
}
static int sunxi_daudio_remove(struct snd_soc_dai *dai)
{
struct sunxi_daudio_info *sunxi_daudio = snd_soc_dai_get_drvdata(dai);
if (sunxi_daudio->pdata->daudio_type == SUNXI_DAUDIO_TDMHDMI_TYPE)
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CTL,
(1<<CTL_TXEN), (0<<CTL_TXEN));
return 0;
}
static int sunxi_daudio_suspend(struct snd_soc_dai *dai)
{
struct sunxi_daudio_info *sunxi_daudio = snd_soc_dai_get_drvdata(dai);
int ret = 0;
pr_debug("[daudio] suspend .%s\n", dev_name(sunxi_daudio->dev));
/* Global disable I2S/TDM module */
sunxi_daudio_global_enable(sunxi_daudio, 0);
if (sunxi_daudio->pdata->daudio_type == SUNXI_DAUDIO_TDMHDMI_TYPE)
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CTL,
(1<<CTL_TXEN), (0<<CTL_TXEN));
clk_disable_unprepare(sunxi_daudio->moduleclk);
clk_disable_unprepare(sunxi_daudio->pllclk);
if (sunxi_daudio->pdata->external_type) {
ret = pinctrl_select_state(sunxi_daudio->pinctrl,
sunxi_daudio->pinstate_sleep);
if (ret) {
pr_warn("[daudio]select pin sleep state failed\n");
return ret;
}
devm_pinctrl_put(sunxi_daudio->pinctrl);
}
return 0;
}
static int sunxi_daudio_resume(struct snd_soc_dai *dai)
{
struct sunxi_daudio_info *sunxi_daudio = snd_soc_dai_get_drvdata(dai);
int ret;
pr_debug("[daudio] resume .%s\n", dev_name(sunxi_daudio->dev));
if (clk_prepare_enable(sunxi_daudio->pllclk)) {
dev_err(sunxi_daudio->dev, "pllclk resume failed\n");
ret = -EBUSY;
goto err_resume_out;
}
if (clk_prepare_enable(sunxi_daudio->moduleclk)) {
dev_err(sunxi_daudio->dev, "moduleclk resume failed\n");
ret = -EBUSY;
goto err_pllclk_disable;
}
if (sunxi_daudio->pdata->external_type) {
sunxi_daudio->pinctrl = devm_pinctrl_get(sunxi_daudio->dev);
if (IS_ERR_OR_NULL(sunxi_daudio)) {
dev_err(sunxi_daudio->dev, "pinctrl resume get fail\n");
ret = -ENOMEM;
goto err_moduleclk_disable;
}
sunxi_daudio->pinstate = pinctrl_lookup_state(
sunxi_daudio->pinctrl, PINCTRL_STATE_DEFAULT);
if (IS_ERR_OR_NULL(sunxi_daudio->pinstate)) {
dev_err(sunxi_daudio->dev,
"pinctrl default state get failed\n");
ret = -EINVAL;
goto err_pinctrl_put;
}
sunxi_daudio->pinstate_sleep = pinctrl_lookup_state(
sunxi_daudio->pinctrl, PINCTRL_STATE_SLEEP);
if (IS_ERR_OR_NULL(sunxi_daudio->pinstate_sleep)) {
dev_err(sunxi_daudio->dev,
"pinctrl sleep state get failed\n");
ret = -EINVAL;
goto err_pinctrl_put;
}
ret = pinctrl_select_state(sunxi_daudio->pinctrl,
sunxi_daudio->pinstate);
if (ret)
dev_warn(sunxi_daudio->dev,
"daudio set pinctrl default state fail\n");
}
sunxi_daudio_init(sunxi_daudio);
/* Global enable I2S/TDM module */
sunxi_daudio_global_enable(sunxi_daudio, 1);
if (sunxi_daudio->pdata->daudio_type == SUNXI_DAUDIO_TDMHDMI_TYPE)
regmap_update_bits(sunxi_daudio->regmap, SUNXI_DAUDIO_CTL,
(1<<CTL_TXEN), (1<<CTL_TXEN));
return 0;