-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrt5647.c
4099 lines (3576 loc) · 119 KB
/
rt5647.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
/*
* rt5647.c -- RT5647 ALSA SoC audio codec driver
*
* Copyright 2012 Realtek Semiconductor Corp.
* Author: Bard Liao <[email protected]>
*
* 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.
*/
#define DEBUG 1
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/i2c.h>
#include <linux/platform_device.h>
#include <linux/spi/spi.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/jack.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/initval.h>
#include <sound/tlv.h>
#define RTK_IOCTL
#ifdef RTK_IOCTL
#if defined(CONFIG_SND_HWDEP) || defined(CONFIG_SND_HWDEP_MODULE)
#include "rt_codec_ioctl.h"
#include "rt5647_ioctl.h"
#endif
#endif
#include "rt5647.h"
#define RT5647_DET_EXT_MIC 0
/* #define USE_INT_CLK */
/* #define JD1_FUNC */
/* #define ALC_DRC_FUNC */
#define USE_ASRC 1
#define USE_TDM 1
/* #define GPIO_LDO1_EN 158 */
#define VERSION "0.1.2 alsa 1.0.25"
struct rt5647_init_reg {
u8 reg;
u16 val;
};
static bool irq2_on = false;
static struct snd_soc_codec *rt5647_codec = NULL;
static struct timer_list jd_check_timer;
struct work_struct jd_check_work;
struct snd_soc_jack *rt5647_jack;
static struct rt5647_init_reg init_list[] = {
{ RT5647_ADDA_CLK1 , 0x0000 },
{ RT5647_IL_CMD2 , 0x0010 }, /* set Inline Command Window */
{ RT5647_PRIV_INDEX , 0x003d },
{ RT5647_PRIV_DATA , 0x3600 },
{ RT5647_GEN_CTRL2 , 0x0028 },
{ RT5647_ASRC_4 , 0x0000 },
/* playback */
{ RT5647_CHARGE_PUMP , 0x0e06 },
{ RT5647_STO_DAC_MIXER , 0x7756 },
{ RT5647_LOUT1 , 0x8888 },
/* record */
{ RT5647_STO1_ADC_DIG_VOL, 0xafaf },
{ RT5647_MONO_ADC_DIG_VOL, 0xafaf },
{ RT5647_GPIO_CTRL1 , 0xc000 },
{ RT5647_GPIO_CTRL2 , 0x0004 },
#ifdef JD1_FUNC
{ RT5647_IRQ_CTRL2 , 0x0200 },
{ RT5647_A_JD_CTRL1 , 0x0001 },
{ RT5647_MICBIAS , 0x0008 },
{ RT5647_GEN_CTRL3 , 0x1080 },
#endif
{ RT5647_CJ_CTRL1 , 0x0022 },
{ RT5647_CJ_CTRL2 , 0x00a7 },
{ RT5647_CJ_CTRL3 , 0x4000 },
{ RT5647_BASE_BACK , 0x0017 },
};
#define RT5647_INIT_REG_LEN ARRAY_SIZE(init_list)
#ifdef ALC_DRC_FUNC
static struct rt5647_init_reg alc_drc_list[] = {
{ RT5647_ALC_DRC_CTRL1 , 0x0000 },
{ RT5647_ALC_DRC_CTRL2 , 0x0000 },
{ RT5647_ALC_CTRL_2 , 0x0000 },
{ RT5647_ALC_CTRL_3 , 0x0000 },
{ RT5647_ALC_CTRL_4 , 0x0000 },
{ RT5647_ALC_CTRL_1 , 0x0000 },
};
#define RT5647_ALC_DRC_REG_LEN ARRAY_SIZE(alc_drc_list)
#endif
static int rt5647_reg_init(struct snd_soc_codec *codec)
{
int i;
for (i = 0; i < RT5647_INIT_REG_LEN; i++)
snd_soc_write(codec, init_list[i].reg, init_list[i].val);
#ifdef ALC_DRC_FUNC
for (i = 0; i < RT5647_ALC_DRC_REG_LEN; i++)
snd_soc_write(codec, alc_drc_list[i].reg, alc_drc_list[i].val);
#endif
return 0;
}
static int rt5647_index_sync(struct snd_soc_codec *codec)
{
int i;
for (i = 0; i < RT5647_INIT_REG_LEN; i++)
if (RT5647_PRIV_INDEX == init_list[i].reg ||
RT5647_PRIV_DATA == init_list[i].reg)
snd_soc_write(codec, init_list[i].reg,
init_list[i].val);
return 0;
}
static const u16 rt5647_reg[RT5647_VENDOR_ID2 + 1] = {
[RT5647_HP_VOL] = 0xc8c8,
[RT5647_SPK_VOL] = 0xc8c8,
[RT5647_LOUT1] = 0xc8c8,
[RT5647_MONO_OUT] = 0xc80a,
[RT5647_CJ_CTRL1] = 0x0002,
[RT5647_CJ_CTRL2] = 0x0827,
[RT5647_CJ_CTRL3] = 0xe000,
[RT5647_INL1_INR1_VOL] = 0x0808,
[RT5647_SIDETONE_CTRL] = 0x018b,
[RT5647_DAC1_DIG_VOL] = 0xafaf,
[RT5647_DAC2_DIG_VOL] = 0xafaf,
[RT5647_DAC_CTRL] = 0x0001,
[RT5647_STO1_ADC_DIG_VOL] = 0x2f2f,
[RT5647_MONO_ADC_DIG_VOL] = 0x2f2f,
[RT5647_STO1_ADC_MIXER] = 0x7060,
[RT5647_MONO_ADC_MIXER] = 0x7070,
[RT5647_AD_DA_MIXER] = 0x8080,
[RT5647_STO_DAC_MIXER] = 0x5656,
[RT5647_MONO_DAC_MIXER] = 0x5454,
[RT5647_DIG_MIXER] = 0xaaa0,
[RT5647_DIG_INF1_DATA] = 0x1002,
[RT5647_PDM_OUT_CTRL] = 0x5000,
[RT5647_REC_L2_MIXER] = 0x007f,
[RT5647_REC_R2_MIXER] = 0x007f,
[RT5647_HPOMIXL_CTRL] = 0x001f,
[RT5647_HPOMIXR_CTRL] = 0x001f,
[RT5647_HPO_MIXER] = 0x6000,
[RT5647_SPK_L_MIXER] = 0x003e,
[RT5647_SPK_R_MIXER] = 0x003e,
[RT5647_SPO_MIXER] = 0xf807,
[RT5647_SPO_CLSD_RATIO] = 0x0004,
[RT5647_MONO_MIXER2] = 0x031f,
[RT5647_OUTMIXL_CTRL3] = 0x01ff,
[RT5647_OUTMIXR_CTRL3] = 0x01ff,
[RT5647_LOUT_MIXER] = 0xf000,
[RT5647_HAPTIC_CTRL1] = 0x0111,
[RT5647_HAPTIC_CTRL2] = 0x0064,
[RT5647_HAPTIC_CTRL3] = 0xef0e,
[RT5647_HAPTIC_CTRL4] = 0xf0f0,
[RT5647_HAPTIC_CTRL5] = 0xef0e,
[RT5647_HAPTIC_CTRL6] = 0xf0f0,
[RT5647_HAPTIC_CTRL7] = 0xef0e,
[RT5647_HAPTIC_CTRL8] = 0xf0f0,
[RT5647_HAPTIC_CTRL9] = 0xf000,
[RT5647_PWR_DIG1] = 0x0300,
[RT5647_PWR_ANLG1] = 0x00c2,
[RT5647_I2S1_SDP] = 0x8000,
[RT5647_I2S2_SDP] = 0x8000,
[RT5647_I2S3_SDP] = 0x8000,
[RT5647_ADDA_CLK1] = 0x1110,
[RT5647_ADDA_CLK2] = 0x3e00,
[RT5647_DMIC_CTRL1] = 0x2409,
[RT5647_DMIC_CTRL2] = 0x000a,
[RT5647_TDM_CTRL_3] = 0x0123,
[RT5647_ASRC_3] = 0x0003,
[RT5647_DEPOP_M1] = 0x0004,
[RT5647_DEPOP_M2] = 0x1100,
[RT5647_DEPOP_M3] = 0x0646,
[RT5647_CHARGE_PUMP] = 0x0c06,
[RT5647_MICBIAS] = 0x3000,
[RT5647_A_JD_CTRL1] = 0x0200,
[RT5647_VAD_CTRL1] = 0x2184,
[RT5647_VAD_CTRL2] = 0x010a,
[RT5647_VAD_CTRL3] = 0x0aea,
[RT5647_VAD_CTRL4] = 0x000c,
[RT5647_VAD_CTRL5] = 0x0400,
[RT5647_CLSD_OUT_CTRL] = 0xa0a8,
[RT5647_CLSD_OUT_CTRL1] = 0x0059,
[RT5647_CLSD_OUT_CTRL2] = 0x0001,
[RT5647_ADC_EQ_CTRL1] = 0x6000,
[RT5647_EQ_CTRL1] = 0x6000,
[RT5647_ALC_DRC_CTRL2] = 0x001f,
[RT5647_ALC_CTRL_1] = 0x020c,
[RT5647_ALC_CTRL_2] = 0x1f00,
[RT5647_ALC_CTRL_4] = 0x4000,
[RT5647_INT_IRQ_ST] = 0x0180,
[RT5647_GPIO_CTRL4] = 0x2000,
[RT5647_BASE_BACK] = 0x0013,
[RT5647_MP3_PLUS1] = 0x0690,
[RT5647_MP3_PLUS2] = 0x1c17,
[RT5647_ADJ_HPF1] = 0xb320,
[RT5647_HP_CALIB_AMP_DET] = 0x0400,
[RT5647_SV_ZCD1] = 0x0809,
[RT5647_IL_CMD] = 0x0003,
[RT5647_IL_CMD2] = 0x0049,
[RT5647_IL_CMD3] = 0x001b,
[RT5647_DRC1_HL_CTRL1] = 0x8000,
[RT5647_DRC1_HL_CTRL2] = 0x0200,
[RT5647_DRC2_HL_CTRL1] = 0x8000,
[RT5647_DRC2_HL_CTRL2] = 0x0200,
[RT5647_MUTI_DRC_CTRL1] = 0x0f20,
[RT5647_ADC_MONO_HP_CTRL1] = 0xb300,
[RT5647_DRC2_CTRL1] = 0x001f,
[RT5647_DRC2_CTRL2] = 0x020c,
[RT5647_DRC2_CTRL3] = 0x1f00,
[RT5647_DRC2_CTRL5] = 0x4000,
[RT5647_DIG_MISC] = 0x2060,
};
static int rt5647_reset(struct snd_soc_codec *codec)
{
return snd_soc_write(codec, RT5647_RESET, 0);
}
/**
* rt5647_index_write - Write private register.
* @codec: SoC audio codec device.
* @reg: Private register index.
* @value: Private register Data.
*
* Modify private register for advanced setting. It can be written through
* private index (0x6a) and data (0x6c) register.
*
* Returns 0 for success or negative error code.
*/
static int rt5647_index_write(struct snd_soc_codec *codec,
unsigned int reg, unsigned int value)
{
int ret;
ret = snd_soc_write(codec, RT5647_PRIV_INDEX, reg);
if (ret < 0) {
dev_err(codec->dev, "Failed to set private addr: %d\n", ret);
goto err;
}
ret = snd_soc_write(codec, RT5647_PRIV_DATA, value);
if (ret < 0) {
dev_err(codec->dev, "Failed to set private value: %d\n", ret);
goto err;
}
return 0;
err:
return ret;
}
/**
* rt5647_index_read - Read private register.
* @codec: SoC audio codec device.
* @reg: Private register index.
*
* Read advanced setting from private register. It can be read through
* private index (0x6a) and data (0x6c) register.
*
* Returns private register value or negative error code.
*/
static unsigned int rt5647_index_read(
struct snd_soc_codec *codec, unsigned int reg)
{
int ret;
ret = snd_soc_write(codec, RT5647_PRIV_INDEX, reg);
if (ret < 0) {
dev_err(codec->dev, "Failed to set private addr: %d\n", ret);
return ret;
}
return snd_soc_read(codec, RT5647_PRIV_DATA);
}
/**
* rt5647_index_update_bits - update private register bits
* @codec: audio codec
* @reg: Private register index.
* @mask: register mask
* @value: new value
*
* Writes new register value.
*
* Returns 1 for change, 0 for no change, or negative error code.
*/
static int rt5647_index_update_bits(struct snd_soc_codec *codec,
unsigned int reg, unsigned int mask, unsigned int value)
{
unsigned int old, new;
int change, ret;
ret = rt5647_index_read(codec, reg);
if (ret < 0) {
dev_err(codec->dev, "Failed to read private reg: %d\n", ret);
goto err;
}
old = ret;
new = (old & ~mask) | (value & mask);
change = old != new;
if (change) {
ret = rt5647_index_write(codec, reg, new);
if (ret < 0) {
dev_err(codec->dev,
"Failed to write private reg: %d\n", ret);
goto err;
}
}
return change;
err:
return ret;
}
static unsigned rt5647_pdm1_read(struct snd_soc_codec *codec,
unsigned int reg)
{
int ret;
ret = snd_soc_write(codec, RT5647_PDM1_DATA_CTRL2, reg<<8);
if (ret < 0) {
dev_err(codec->dev, "Failed to set private value: %d\n", ret);
goto err;
}
ret = snd_soc_write(codec, RT5647_PDM_DATA_CTRL1, 0x0200);
if (ret < 0) {
dev_err(codec->dev, "Failed to set private value: %d\n", ret);
goto err;
}
do {
ret = snd_soc_read(codec, RT5647_PDM_DATA_CTRL1);
} while (ret & 0x0100);
return snd_soc_read(codec, RT5647_PDM1_DATA_CTRL4);
err:
return ret;
}
static int rt5647_pdm1_write(struct snd_soc_codec *codec,
unsigned int reg, unsigned int value)
{
int ret;
ret = snd_soc_write(codec, RT5647_PDM1_DATA_CTRL3, value);
if (ret < 0) {
dev_err(codec->dev, "Failed to set private addr: %d\n", ret);
goto err;
}
ret = snd_soc_write(codec, RT5647_PDM1_DATA_CTRL2, reg<<8);
if (ret < 0) {
dev_err(codec->dev, "Failed to set private value: %d\n", ret);
goto err;
}
ret = snd_soc_write(codec, RT5647_PDM_DATA_CTRL1, 0x0600);
if (ret < 0) {
dev_err(codec->dev, "Failed to set private value: %d\n", ret);
goto err;
}
ret = snd_soc_write(codec, RT5647_PDM_DATA_CTRL1, 0x3600);
if (ret < 0) {
dev_err(codec->dev, "Failed to set private value: %d\n", ret);
goto err;
}
do {
ret = snd_soc_read(codec, RT5647_PDM_DATA_CTRL1);
} while (ret & 0x0100);
return 0;
err:
return ret;
}
static int rt5647_volatile_register(
struct snd_soc_codec *codec, unsigned int reg)
{
switch (reg) {
case RT5647_RESET:
case RT5647_PDM_DATA_CTRL1:
case RT5647_PDM1_DATA_CTRL4:
case RT5647_PRIV_DATA:
case RT5647_CJ_CTRL1:
case RT5647_CJ_CTRL2:
case RT5647_CJ_CTRL3:
case RT5647_A_JD_CTRL1:
case RT5647_VAD_CTRL5:
case RT5647_ADC_EQ_CTRL1:
case RT5647_EQ_CTRL1:
case RT5647_ALC_CTRL_1:
case RT5647_IRQ_CTRL2:
case RT5647_IRQ_CTRL3:
case RT5647_INT_IRQ_ST:
case RT5647_IL_CMD:
case RT5647_JD_CTRL3:
case RT5647_GEN_CTRL3:
case RT5647_DIG_MISC:
case RT5647_VENDOR_ID:
case RT5647_VENDOR_ID1:
case RT5647_VENDOR_ID2:
return 1;
default:
return 0;
}
}
static int rt5647_readable_register(
struct snd_soc_codec *codec, unsigned int reg)
{
switch (reg) {
case RT5647_RESET:
case RT5647_SPK_VOL:
case RT5647_HP_VOL:
case RT5647_LOUT1:
case RT5647_LOUT2:
case RT5647_MONO_OUT:
case RT5647_CJ_CTRL1:
case RT5647_CJ_CTRL2:
case RT5647_CJ_CTRL3:
case RT5647_IN2:
case RT5647_IN3:
case RT5647_INL1_INR1_VOL:
case RT5647_SIDETONE_CTRL:
case RT5647_DAC1_DIG_VOL:
case RT5647_DAC2_DIG_VOL:
case RT5647_DAC_CTRL:
case RT5647_STO1_ADC_DIG_VOL:
case RT5647_MONO_ADC_DIG_VOL:
case RT5647_ADC_BST_VOL1:
case RT5647_ADC_BST_VOL2:
case RT5647_STO1_ADC_MIXER:
case RT5647_MONO_ADC_MIXER:
case RT5647_AD_DA_MIXER:
case RT5647_STO_DAC_MIXER:
case RT5647_MONO_DAC_MIXER:
case RT5647_DIG_MIXER:
case RT5647_DIG_INF1_DATA:
case RT5647_PDM_OUT_CTRL:
case RT5647_PDM_DATA_CTRL1:
case RT5647_PDM1_DATA_CTRL2:
case RT5647_PDM1_DATA_CTRL3:
case RT5647_PDM1_DATA_CTRL4:
case RT5647_REC_L1_MIXER:
case RT5647_REC_L2_MIXER:
case RT5647_REC_R1_MIXER:
case RT5647_REC_R2_MIXER:
case RT5647_HPMIXL_CTRL:
case RT5647_HPOMIXL_CTRL:
case RT5647_HPMIXR_CTRL:
case RT5647_HPOMIXR_CTRL:
case RT5647_HPO_MIXER:
case RT5647_SPK_L_MIXER:
case RT5647_SPK_R_MIXER:
case RT5647_SPO_MIXER:
case RT5647_SPO_CLSD_RATIO:
case RT5647_MONO_MIXER1:
case RT5647_MONO_MIXER2:
case RT5647_OUTMIXL_CTRL1:
case RT5647_OUTMIXL_CTRL2:
case RT5647_OUTMIXL_CTRL3:
case RT5647_OUTMIXR_CTRL1:
case RT5647_OUTMIXR_CTRL2:
case RT5647_OUTMIXR_CTRL3:
case RT5647_LOUT_MIXER:
case RT5647_HAPTIC_CTRL1:
case RT5647_HAPTIC_CTRL2:
case RT5647_HAPTIC_CTRL3:
case RT5647_HAPTIC_CTRL4:
case RT5647_HAPTIC_CTRL5:
case RT5647_HAPTIC_CTRL6:
case RT5647_HAPTIC_CTRL7:
case RT5647_HAPTIC_CTRL8:
case RT5647_HAPTIC_CTRL9:
case RT5647_HAPTIC_CTRL10:
case RT5647_PWR_DIG1:
case RT5647_PWR_DIG2:
case RT5647_PWR_ANLG1:
case RT5647_PWR_ANLG2:
case RT5647_PWR_MIXER:
case RT5647_PWR_VOL:
case RT5647_PRIV_INDEX:
case RT5647_PRIV_DATA:
case RT5647_I2S1_SDP:
case RT5647_I2S2_SDP:
case RT5647_I2S3_SDP:
case RT5647_ADDA_CLK1:
case RT5647_ADDA_CLK2:
case RT5647_DMIC_CTRL1:
case RT5647_DMIC_CTRL2:
case RT5647_TDM_CTRL_1:
case RT5647_TDM_CTRL_2:
case RT5647_TDM_CTRL_3:
case RT5647_GLB_CLK:
case RT5647_PLL_CTRL1:
case RT5647_PLL_CTRL2:
case RT5647_ASRC_1:
case RT5647_ASRC_2:
case RT5647_ASRC_3:
case RT5647_ASRC_4:
case RT5647_DEPOP_M1:
case RT5647_DEPOP_M2:
case RT5647_DEPOP_M3:
case RT5647_CHARGE_PUMP:
case RT5647_MICBIAS:
case RT5647_A_JD_CTRL1:
case RT5647_VAD_CTRL1:
case RT5647_VAD_CTRL2:
case RT5647_VAD_CTRL3:
case RT5647_VAD_CTRL4:
case RT5647_VAD_CTRL5:
case RT5647_CLSD_OUT_CTRL:
case RT5647_CLSD_OUT_CTRL1:
case RT5647_CLSD_OUT_CTRL2:
case RT5647_ADC_EQ_CTRL1:
case RT5647_ADC_EQ_CTRL2:
case RT5647_EQ_CTRL1:
case RT5647_EQ_CTRL2:
case RT5647_ALC_DRC_CTRL1:
case RT5647_ALC_DRC_CTRL2:
case RT5647_ALC_CTRL_1:
case RT5647_ALC_CTRL_2:
case RT5647_ALC_CTRL_3:
case RT5647_JD_CTRL:
case RT5647_IRQ_CTRL1:
case RT5647_IRQ_CTRL2:
case RT5647_IRQ_CTRL3:
case RT5647_INT_IRQ_ST:
case RT5647_GPIO_CTRL1:
case RT5647_GPIO_CTRL2:
case RT5647_GPIO_CTRL3:
case RT5647_GPIO_CTRL4:
case RT5647_SCRABBLE_FUN:
case RT5647_SCRABBLE_CTRL:
case RT5647_BASE_BACK:
case RT5647_MP3_PLUS1:
case RT5647_MP3_PLUS2:
case RT5647_ADJ_HPF1:
case RT5647_ADJ_HPF2:
case RT5647_HP_CALIB_AMP_DET:
case RT5647_SV_ZCD1:
case RT5647_SV_ZCD2:
case RT5647_IL_CMD:
case RT5647_IL_CMD2:
case RT5647_IL_CMD3:
case RT5647_DRC1_HL_CTRL1:
case RT5647_DRC1_HL_CTRL2:
case RT5647_ADC_MONO_HP_CTRL1:
case RT5647_ADC_MONO_HP_CTRL2:
case RT5647_DRC2_CTRL1:
case RT5647_DRC2_CTRL2:
case RT5647_DRC2_CTRL3:
case RT5647_DRC2_CTRL4:
case RT5647_DRC2_CTRL5:
case RT5647_JD_CTRL3:
case RT5647_JD_CTRL4:
case RT5647_DIG_MISC:
case RT5647_GEN_CTRL2:
case RT5647_GEN_CTRL3:
case RT5647_VENDOR_ID:
case RT5647_VENDOR_ID1:
case RT5647_VENDOR_ID2:
return 1;
default:
return 0;
}
}
void dc_calibrate(struct snd_soc_codec *codec)
{
unsigned int sclk_src;
sclk_src = snd_soc_read(codec, RT5647_GLB_CLK) &
RT5647_SCLK_SRC_MASK;
snd_soc_update_bits(codec, RT5647_PWR_ANLG2,
RT5647_PWR_MB1, RT5647_PWR_MB1);
snd_soc_update_bits(codec, RT5647_DEPOP_M2,
RT5647_DEPOP_MASK, RT5647_DEPOP_MAN);
snd_soc_update_bits(codec, RT5647_DEPOP_M1,
RT5647_HP_CP_MASK | RT5647_HP_SG_MASK | RT5647_HP_CB_MASK,
RT5647_HP_CP_PU | RT5647_HP_SG_DIS | RT5647_HP_CB_PU);
snd_soc_update_bits(codec, RT5647_GLB_CLK,
RT5647_SCLK_SRC_MASK, 0x2 << RT5647_SCLK_SRC_SFT);
rt5647_index_write(codec, RT5647_HP_DCC_INT1, 0x9f01);
snd_soc_update_bits(codec, RT5647_PWR_ANLG2,
RT5647_PWR_MB1, 0);
snd_soc_update_bits(codec, RT5647_GLB_CLK,
RT5647_SCLK_SRC_MASK, sclk_src);
}
/**
* rt5647_headset_detect - Detect headset.
* @codec: SoC audio codec device.
* @jack_insert: Jack insert or not.
*
* Detect whether is headset or not when jack inserted.
*
* Returns detect status.
*/
int rt5647_headset_detect(struct snd_soc_codec *codec, int jack_insert)
{
struct rt5647_priv *rt5647 = snd_soc_codec_get_drvdata(codec);
int jack_type = 0, val;
if (jack_insert) {
snd_soc_update_bits(codec, RT5647_PWR_ANLG1,
RT5647_PWR_MB | RT5647_PWR_VREF2,
RT5647_PWR_MB | RT5647_PWR_VREF2);
snd_soc_dapm_force_enable_pin(&codec->dapm, "micbias1");
snd_soc_dapm_force_enable_pin(&codec->dapm, "micbias2");
snd_soc_dapm_force_enable_pin(&codec->dapm, "LDO2");
snd_soc_dapm_force_enable_pin(&codec->dapm, "Mic Det Power");
snd_soc_dapm_sync(&codec->dapm);
snd_soc_write(codec, RT5647_CJ_CTRL1, 0x0006);
snd_soc_write(codec, RT5647_JD_CTRL3, 0x00b0);
snd_soc_update_bits(codec, RT5647_CJ_CTRL2,
RT5647_CBJ_MN_JD, 0);
snd_soc_update_bits(codec, RT5647_CJ_CTRL2,
RT5647_CBJ_MN_JD, RT5647_CBJ_MN_JD);
msleep(400);
val = snd_soc_read(codec, RT5647_CJ_CTRL3) & 0x7;
pr_debug("val=%d\n", val);
switch (val) {
case 0x1: /* Nokia type*/
case 0x2: /* iPhone type*/
jack_type = SND_JACK_HEADSET;
break;
default:
snd_soc_dapm_disable_pin(&codec->dapm, "micbias1");
snd_soc_dapm_disable_pin(&codec->dapm, "micbias2");
snd_soc_dapm_disable_pin(&codec->dapm, "LDO2");
snd_soc_dapm_disable_pin(&codec->dapm, "Mic Det Power");
snd_soc_dapm_sync(&codec->dapm);
snd_soc_update_bits(codec, RT5647_INT_IRQ_ST, 0x8, 0x0);
snd_soc_update_bits(codec, RT5647_GEN_CTRL2, 0x8, 0x0);
jack_type = SND_JACK_HEADPHONE;
break;
}
} else {
snd_soc_update_bits(codec, RT5647_INT_IRQ_ST, 0x8, 0x0);
snd_soc_update_bits(codec, RT5647_GEN_CTRL2, 0x8, 0x0);
snd_soc_dapm_disable_pin(&codec->dapm, "micbias1");
snd_soc_dapm_disable_pin(&codec->dapm, "micbias2");
snd_soc_dapm_disable_pin(&codec->dapm, "LDO2");
snd_soc_dapm_disable_pin(&codec->dapm, "Mic Det Power");
snd_soc_dapm_sync(&codec->dapm);
jack_type = 0;
}
rt5647->jack_type = jack_type;
pr_debug("jack_type=%d\n",jack_type);
return jack_type;
}
EXPORT_SYMBOL(rt5647_headset_detect);
void rt5647_enable_push_button_irq(struct snd_soc_codec *codec,
struct snd_soc_jack *jack)
{
snd_soc_update_bits(codec, RT5647_GEN_CTRL2, 0x8, 0x8);
snd_soc_update_bits(codec, RT5647_INT_IRQ_ST, 0x8, 0x8);
snd_soc_update_bits(codec, RT5647_IL_CMD, 0x40, 0x40);
snd_soc_read(codec, RT5647_IL_CMD);
rt5647_jack = jack;
mod_timer( &jd_check_timer, jiffies );
}
EXPORT_SYMBOL(rt5647_enable_push_button_irq);
int rt5647_button_detect(struct snd_soc_codec *codec)
{
int btn_type, val;
if (!(snd_soc_read(codec, RT5647_INT_IRQ_ST) & 0x4))
return 0;
snd_soc_update_bits(codec, RT5647_IL_CMD, 0x40, 0x40);
val = snd_soc_read(codec, RT5647_IL_CMD);
btn_type = val & 0xff80;
pr_debug("btn_type=0x%x\n", btn_type);
snd_soc_write(codec, RT5647_IL_CMD, val);
msleep(20);
if (btn_type == 0 ||
((snd_soc_read(codec, RT5647_IL_CMD) & 0xff80) == 0)) {
pr_debug("button release\n");
btn_type = 0;
}
return btn_type;
}
EXPORT_SYMBOL(rt5647_button_detect);
int rt5647_check_irq_event(struct snd_soc_codec *codec)
{
struct rt5647_priv *rt5647 = snd_soc_codec_get_drvdata(codec);
int val, ret = RT5647_UN_EVENT;
val = snd_soc_read(codec, RT5647_A_JD_CTRL1) & 0x0020;
pr_debug(" rt5647_check_irq_event : val = 0x%x rt5647->jack_type=0x%x\n", val, rt5647->jack_type);
switch (val) {
case 0x20: /* No plug */
/* rt5647->jack_type = rt5647_headset_detect(codec, 0); */
ret = RT5647_J_OUT_EVENT;
del_timer( &jd_check_timer);
break;
case 0x0: /* plug */
switch (rt5647->jack_type) {
case 0:
ret = RT5647_J_IN_EVENT;
break;
case SND_JACK_HEADSET:
if (rt5647_button_detect(codec))
ret = RT5647_BP_EVENT;
else
ret = RT5647_BR_EVENT;
break;
case SND_JACK_HEADPHONE:
default:
pr_err("There is no button event on a headphone\n");
break;
}
break;
default:
pr_err("Unknown JD value\n");
}
return ret;
}
EXPORT_SYMBOL(rt5647_check_irq_event);
void rt5647_i2s2_func_switch(struct snd_soc_codec *codec, bool enable)
{
snd_soc_update_bits(codec, RT5647_GPIO_CTRL1,
RT5647_I2S2_SEL, (!enable) << RT5647_I2S2_SEL_SFT);
}
EXPORT_SYMBOL(rt5647_i2s2_func_switch);
void rt5647_enable_push_button_irq2(bool on)
{
printk("rt5647_enable_push_button_irq2.\n");
struct snd_soc_codec *codec = rt5647_codec;
struct rt5647_priv *rt5647;
if (codec == NULL) {
printk("Cannot enable push button since codec driver have not loaded yet.\n");
return;
}
rt5647 = snd_soc_codec_get_drvdata(codec);
if (on) {
printk("Enable irq2 for pushing button.\n");
rt5647->push_button_en = true;
snd_soc_update_bits(codec, RT5647_CJ_CTRL1, 0x0580, 0x0580);
snd_soc_update_bits(codec, RT5647_CJ_CTRL2,
RT5647_CBJ_MN_JD | RT5647_CBJ_DET_MODE,
RT5647_CBJ_MN_JD | RT5647_CBJ_DET_MODE);
snd_soc_update_bits(codec, RT5647_PWR_ANLG1,
RT5647_PWR_MB | RT5647_PWR_BG,
RT5647_PWR_MB | RT5647_PWR_BG);
snd_soc_update_bits(codec, RT5647_PWR_ANLG2, RT5647_PWR_MB1,
RT5647_PWR_MB1);
snd_soc_update_bits(codec, RT5647_PWR_MIXER, RT5647_PWR_LDO2,
RT5647_PWR_LDO2);
snd_soc_update_bits(codec, RT5647_PWR_VOL, RT5647_PWR_MIC_DET,
RT5647_PWR_MIC_DET);
snd_soc_update_bits(codec, RT5647_GEN_CTRL2, 0x8, 0x8);
snd_soc_update_bits(codec, RT5647_JD_CTRL3, 0xb8, 0xb0);
snd_soc_update_bits(codec, RT5647_INT_IRQ_ST, 0x8, 0x8);
snd_soc_update_bits(codec, RT5647_GPIO_CTRL1,
RT5647_GP1_PIN_MASK, RT5647_GP1_PIN_IRQ);
snd_soc_update_bits(codec, RT5647_IL_CMD, 0xff80, 0xff80);
irq2_on = true;
} else {
printk("Disable irq2 for pushing button.\n");
rt5647->push_button_en = false;
if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
snd_soc_update_bits(codec, RT5647_PWR_ANLG1,
RT5647_PWR_MB | RT5647_PWR_BG, 0);
snd_soc_update_bits(codec, RT5647_PWR_ANLG2,
RT5647_PWR_MB1, 0);
snd_soc_update_bits(codec, RT5647_PWR_MIXER,
RT5647_PWR_LDO2, 0);
snd_soc_update_bits(codec, RT5647_PWR_VOL,
RT5647_PWR_MIC_DET, 0);
}
snd_soc_update_bits(codec, RT5647_IL_CMD, 0xff80, 0xff80);
}
}
EXPORT_SYMBOL(rt5647_enable_push_button_irq2);
int rt5647_button_detect2(void)
{
struct snd_soc_codec *codec = rt5647_codec;
int btn_type, val;
if (codec == NULL)
return 0;
if (!irq2_on) {
printk("Call enable_push_button_irq2() since jack was inserted while boot-up.\n");
rt5647_enable_push_button_irq2(true);
}
for(val = 0; val < 40; val++) {
if (snd_soc_read(codec, RT5647_INT_IRQ_ST) & 0x4)
{
printk("Delay: %dms\n", val*5);
break;
}
msleep(5);
}
val = snd_soc_read(codec, RT5647_IL_CMD);
printk("Value: 0x%x\n", val);
btn_type = val & 0xff80;
printk("btn_type=0x%x\n", btn_type);
snd_soc_update_bits(codec, RT5647_IL_CMD, 0xff80, 0xff80);
return btn_type;
}
EXPORT_SYMBOL(rt5647_button_detect2);
void rt5647_button_release(void)
{
struct snd_soc_codec *codec = rt5647_codec;
if ((codec == NULL) || !irq2_on)
return;
snd_soc_update_bits(codec, RT5647_IL_CMD, 0xff80, 0xff80);
}
EXPORT_SYMBOL(rt5647_button_release);
static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -4650, 150, 0);
static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -65625, 375, 0);
static const DECLARE_TLV_DB_SCALE(dac_gain_tlv, -900, 300, 0);
static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -600, 600, 0);
static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -3450, 150, 0);
static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -17625, 375, 0);
static const DECLARE_TLV_DB_SCALE(adc_bst_tlv, 0, 1200, 0);
static const DECLARE_TLV_DB_SCALE(adc_com_tlv, 0, 100, 0);
static const DECLARE_TLV_DB_SCALE(mix_bst_tlv, -1800, 300, 0);
static const DECLARE_TLV_DB_SCALE(bb_bst_tlv, 0, 150, 0);
/* {0, +20, +24, +30, +35, +40, +44, +50, +52} dB */
static unsigned int bst_tlv[] = {
TLV_DB_RANGE_HEAD(7),
0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
3, 5, TLV_DB_SCALE_ITEM(3000, 500, 0),
6, 6, TLV_DB_SCALE_ITEM(4400, 0, 0),
7, 7, TLV_DB_SCALE_ITEM(5000, 0, 0),
8, 8, TLV_DB_SCALE_ITEM(5200, 0, 0),
};
/* {-6dB, -4.5dB, -3dB, -1.5dB, 0dB, 0.83dB, 1.58dB, 2.28dB} */
static unsigned int speaker_gain_tlv[] = {
TLV_DB_RANGE_HEAD(4),
0, 4, TLV_DB_SCALE_ITEM(-6000, 1500, 0),
5, 5, TLV_DB_SCALE_ITEM(830, 0, 0),
6, 6, TLV_DB_SCALE_ITEM(1580, 0, 0),
7, 7, TLV_DB_SCALE_ITEM(2280, 0, 0),
};
/* {-11.625, -10.5, -9, -6.75, -4.5, -3, -1.875, -0.375, 0.375, 1.5
2.25, 3, 3,75, 4.5, 4.875, 5.625, 6, 6.375, 7.125, 7.5} dB */
static unsigned int tt_bst_tlv[] = {
TLV_DB_RANGE_HEAD(20),
0, 0, TLV_DB_SCALE_ITEM(-1162, 0, 0),
1, 1, TLV_DB_SCALE_ITEM(-1050, 0, 0),
2, 2, TLV_DB_SCALE_ITEM(-900, 0, 0),
3, 3, TLV_DB_SCALE_ITEM(-675, 0, 0),
4, 4, TLV_DB_SCALE_ITEM(-450, 0, 0),
5, 5, TLV_DB_SCALE_ITEM(-300, 0, 0),
6, 6, TLV_DB_SCALE_ITEM(-187, 0, 0),
7, 7, TLV_DB_SCALE_ITEM(-37, 0, 0),
8, 8, TLV_DB_SCALE_ITEM(37, 0, 0),
9, 9, TLV_DB_SCALE_ITEM(150, 0, 0),
10, 10, TLV_DB_SCALE_ITEM(225, 0, 0),
11, 11, TLV_DB_SCALE_ITEM(300, 0, 0),
12, 12, TLV_DB_SCALE_ITEM(375, 0, 0),
13, 13, TLV_DB_SCALE_ITEM(450, 0, 0),
14, 14, TLV_DB_SCALE_ITEM(487, 0, 0),
15, 15, TLV_DB_SCALE_ITEM(562, 0, 0),
16, 16, TLV_DB_SCALE_ITEM(600, 0, 0),
17, 17, TLV_DB_SCALE_ITEM(637, 0, 0),
18, 18, TLV_DB_SCALE_ITEM(712, 0, 0),
19, 19, TLV_DB_SCALE_ITEM(750, 0, 0),
};
/* IN1/IN2 Input Type */
static const char *rt5647_input_mode[] = {
"Single ended", "Differential"
};
static const SOC_ENUM_SINGLE_DECL(
rt5647_in2_mode_enum, RT5647_IN2,
RT5647_IN_SFT2, rt5647_input_mode);
static const SOC_ENUM_SINGLE_DECL(
rt5647_in3_mode_enum, RT5647_IN3,
RT5647_IN_SFT1, rt5647_input_mode);
/* Interface data select */
static const char *rt5647_data_select[] = {
"Normal", "Swap", "left copy to right", "right copy to left"
};
static const SOC_ENUM_SINGLE_DECL(rt5647_if2_dac_enum, RT5647_DIG_INF1_DATA,
RT5647_IF2_DAC_SEL_SFT, rt5647_data_select);
static const SOC_ENUM_SINGLE_DECL(rt5647_if2_adc_enum, RT5647_DIG_INF1_DATA,
RT5647_IF2_ADC_SEL_SFT, rt5647_data_select);
static const SOC_ENUM_SINGLE_DECL(rt5647_if3_dac_enum, RT5647_DIG_INF1_DATA,
RT5647_IF3_DAC_SEL_SFT, rt5647_data_select);
static const SOC_ENUM_SINGLE_DECL(rt5647_if3_adc_enum, RT5647_DIG_INF1_DATA,
RT5647_IF3_ADC_SEL_SFT, rt5647_data_select);
/* Haptic */
static const char *rt5647_haptic_act_select[] = {
"AC", "DC"
};
static const SOC_ENUM_SINGLE_DECL(rt5647_haptic_act_enum, RT5647_HAPTIC_CTRL1,
RT5647_HAPTIC_TYPE_SFT, rt5647_haptic_act_select);
static const char *rt5647_haptic_trigger_select[] = {
"Disable", "One shot", "Continuous", "Programmable"
};
static const SOC_ENUM_SINGLE_DECL(rt5647_haptic_trigger_enum,
RT5647_HAPTIC_CTRL1, RT5647_HAPTIC_TG_SFT,
rt5647_haptic_trigger_select);
/* Class D speaker gain ratio */
static const char * const rt5647_clsd_spk_ratio[] = {
"1.66x", "1.83x", "1.94x", "2x", "2.11x", "2.22x",
"2.33x", "2.44x", "2.55x", "2.66x", "2.77x"
};
static const SOC_ENUM_SINGLE_DECL(rt5647_clsd_spk_ratio_enum,
RT5647_CLSD_OUT_CTRL, RT5647_CLSD_RATIO_SFT,
rt5647_clsd_spk_ratio);
#if 1 /* Only for debug */
static const char *rt5647_push_btn_mode[] = {
"Disable", "read"
};
static const SOC_ENUM_SINGLE_DECL(rt5647_push_btn_enum, 0, 0, rt5647_push_btn_mode);
static int rt5647_push_btn_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
ucontrol->value.integer.value[0] = 0;
return 0;
}
static int rt5647_push_btn_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
printk(KERN_INFO "ret=0x%x\n", rt5647_button_detect(codec));
return 0;
}
static const char *rt5647_jack_type_mode[] = {
"Disable", "read"
};
static const SOC_ENUM_SINGLE_DECL(rt5647_jack_type_enum, 0, 0, rt5647_jack_type_mode);
static int rt5647_jack_type_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
ucontrol->value.integer.value[0] = 0;
return 0;
}
static int rt5647_jack_type_put(struct snd_kcontrol *kcontrol,