summaryrefslogtreecommitdiff
path: root/sound/soc/codecs/rk817_codec.c
blob: bd3d6be08cf9d182c729b8bb0eeff9b65f24e7b1 (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
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
/*
 * Copyright (c) 2018 Rockchip Electronics Co. Ltd.
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/clk.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/mfd/rk808.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <sound/core.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include "rk817_codec.h"

static int dbg_enable;
module_param_named(dbg_level, dbg_enable, int, 0644);

#define DBG(args...) \
	do { \
		if (dbg_enable) { \
			pr_info(args); \
		} \
	} while (0)

/* For route */
#define RK817_CODEC_PLAYBACK	1
#define RK817_CODEC_CAPTURE	2
#define RK817_CODEC_INCALL	4
#define RK817_CODEC_ALL	(RK817_CODEC_PLAYBACK |\
	RK817_CODEC_CAPTURE | RK817_CODEC_INCALL)

/*
 * DDAC L/R volume setting
 * 0db~-95db,0.375db/step,for example:
 * 0: 0dB
 * 0x0a: -3.75dB
 * 0x7d: -46dB
 * 0xff: -95dB
 */
#define OUT_VOLUME	(0x03)

/*
 * DADC L/R volume setting
 * 0db~-95db,0.375db/step,for example:
 * 0: 0dB
 * 0x0a: -3.75dB
 * 0x7d: -46dB
 * 0xff: -95dB
 */
#define CAPTURE_VOLUME	(0x0)

struct rk817_codec_priv {
	struct snd_soc_codec *codec;
	struct regmap *regmap;
	struct rk808 *rk817;
	struct clk *mclk;

	unsigned int stereo_sysclk;
	unsigned int rate;

	unsigned int spk_volume;
	unsigned int hp_volume;
	unsigned int capture_volume;

	bool mic_in_differential;
	bool pdmdata_out_enable;

	long int playback_path;
	long int capture_path;
};

static const struct reg_default rk817_reg_defaults[] = {
	{ RK817_CODEC_DTOP_VUCTL, 0x003 },
	{ RK817_CODEC_DTOP_VUCTIME, 0x00 },
	{ RK817_CODEC_DTOP_LPT_SRST, 0x00 },
	{ RK817_CODEC_DTOP_DIGEN_CLKE, 0x00 },
	{ RK817_CODEC_AREF_RTCFG0, 0x00 },
	{ RK817_CODEC_AREF_RTCFG1, 0x06 },
	{ RK817_CODEC_AADC_CFG0, 0xc8 },
	{ RK817_CODEC_AADC_CFG1, 0x00 },
	{ RK817_CODEC_DADC_VOLL, 0x00 },
	{ RK817_CODEC_DADC_VOLR, 0x00 },
	{ RK817_CODEC_DADC_SR_ACL0, 0x00 },
	{ RK817_CODEC_DADC_ALC1, 0x00 },
	{ RK817_CODEC_DADC_ALC2, 0x00 },
	{ RK817_CODEC_DADC_NG, 0x00 },
	{ RK817_CODEC_DADC_HPF, 0x00 },
	{ RK817_CODEC_DADC_RVOLL, 0xff },
	{ RK817_CODEC_DADC_RVOLR, 0xff },
	{ RK817_CODEC_AMIC_CFG0, 0x70 },
	{ RK817_CODEC_AMIC_CFG1, 0x00 },
	{ RK817_CODEC_DMIC_PGA_GAIN, 0x66 },
	{ RK817_CODEC_DMIC_LMT1, 0x00 },
	{ RK817_CODEC_DMIC_LMT2, 0x00 },
	{ RK817_CODEC_DMIC_NG1, 0x00 },
	{ RK817_CODEC_DMIC_NG2, 0x00 },
	{ RK817_CODEC_ADAC_CFG0, 0x00 },
	{ RK817_CODEC_ADAC_CFG1, 0x07 },
	{ RK817_CODEC_DDAC_POPD_DACST, 0x82 },
	{ RK817_CODEC_DDAC_VOLL, 0x00 },
	{ RK817_CODEC_DDAC_VOLR, 0x00 },
	{ RK817_CODEC_DDAC_SR_LMT0, 0x00 },
	{ RK817_CODEC_DDAC_LMT1, 0x00 },
	{ RK817_CODEC_DDAC_LMT2, 0x00 },
	{ RK817_CODEC_DDAC_MUTE_MIXCTL, 0xa0 },
	{ RK817_CODEC_DDAC_RVOLL, 0xff },
	{ RK817_CODEC_DDAC_RVOLR, 0xff },
	{ RK817_CODEC_AHP_ANTI0, 0x00 },
	{ RK817_CODEC_AHP_ANTI1, 0x00 },
	{ RK817_CODEC_AHP_CFG0, 0xe0 },
	{ RK817_CODEC_AHP_CFG1, 0x1f },
	{ RK817_CODEC_AHP_CP, 0x09 },
	{ RK817_CODEC_ACLASSD_CFG1, 0x69 },
	{ RK817_CODEC_ACLASSD_CFG2, 0x44 },
	{ RK817_CODEC_APLL_CFG0, 0x04 },
	{ RK817_CODEC_APLL_CFG1, 0x00 },
	{ RK817_CODEC_APLL_CFG2, 0x30 },
	{ RK817_CODEC_APLL_CFG3, 0x19 },
	{ RK817_CODEC_APLL_CFG4, 0x65 },
	{ RK817_CODEC_APLL_CFG5, 0x01 },
	{ RK817_CODEC_DI2S_CKM, 0x01 },
	{ RK817_CODEC_DI2S_RSD, 0x00 },
	{ RK817_CODEC_DI2S_RXCR1, 0x00 },
	{ RK817_CODEC_DI2S_RXCR2, 0x17 },
	{ RK817_CODEC_DI2S_RXCMD_TSD, 0x00 },
	{ RK817_CODEC_DI2S_TXCR1, 0x00 },
	{ RK817_CODEC_DI2S_TXCR2, 0x17 },
	{ RK817_CODEC_DI2S_TXCR3_TXCMD, 0x00 },
};

static bool rk817_volatile_register(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case RK817_CODEC_DTOP_LPT_SRST:
		return true;
	default:
		return false;
	}
}

static bool rk817_codec_register(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case RK817_CODEC_DTOP_VUCTL:
	case RK817_CODEC_DTOP_VUCTIME:
	case RK817_CODEC_DTOP_LPT_SRST:
	case RK817_CODEC_DTOP_DIGEN_CLKE:
	case RK817_CODEC_AREF_RTCFG0:
	case RK817_CODEC_AREF_RTCFG1:
	case RK817_CODEC_AADC_CFG0:
	case RK817_CODEC_AADC_CFG1:
	case RK817_CODEC_DADC_VOLL:
	case RK817_CODEC_DADC_VOLR:
	case RK817_CODEC_DADC_SR_ACL0:
	case RK817_CODEC_DADC_ALC1:
	case RK817_CODEC_DADC_ALC2:
	case RK817_CODEC_DADC_NG:
	case RK817_CODEC_DADC_HPF:
	case RK817_CODEC_DADC_RVOLL:
	case RK817_CODEC_DADC_RVOLR:
	case RK817_CODEC_AMIC_CFG0:
	case RK817_CODEC_AMIC_CFG1:
	case RK817_CODEC_DMIC_PGA_GAIN:
	case RK817_CODEC_DMIC_LMT1:
	case RK817_CODEC_DMIC_LMT2:
	case RK817_CODEC_DMIC_NG1:
	case RK817_CODEC_DMIC_NG2:
	case RK817_CODEC_ADAC_CFG0:
	case RK817_CODEC_ADAC_CFG1:
	case RK817_CODEC_DDAC_POPD_DACST:
	case RK817_CODEC_DDAC_VOLL:
	case RK817_CODEC_DDAC_VOLR:
	case RK817_CODEC_DDAC_SR_LMT0:
	case RK817_CODEC_DDAC_LMT1:
	case RK817_CODEC_DDAC_LMT2:
	case RK817_CODEC_DDAC_MUTE_MIXCTL:
	case RK817_CODEC_DDAC_RVOLL:
	case RK817_CODEC_DDAC_RVOLR:
	case RK817_CODEC_AHP_ANTI0:
	case RK817_CODEC_AHP_ANTI1:
	case RK817_CODEC_AHP_CFG0:
	case RK817_CODEC_AHP_CFG1:
	case RK817_CODEC_AHP_CP:
	case RK817_CODEC_ACLASSD_CFG1:
	case RK817_CODEC_ACLASSD_CFG2:
	case RK817_CODEC_APLL_CFG0:
	case RK817_CODEC_APLL_CFG1:
	case RK817_CODEC_APLL_CFG2:
	case RK817_CODEC_APLL_CFG3:
	case RK817_CODEC_APLL_CFG4:
	case RK817_CODEC_APLL_CFG5:
	case RK817_CODEC_DI2S_CKM:
	case RK817_CODEC_DI2S_RSD:
	case RK817_CODEC_DI2S_RXCR1:
	case RK817_CODEC_DI2S_RXCR2:
	case RK817_CODEC_DI2S_RXCMD_TSD:
	case RK817_CODEC_DI2S_TXCR1:
	case RK817_CODEC_DI2S_TXCR2:
	case RK817_CODEC_DI2S_TXCR3_TXCMD:
		return true;
	default:
		return false;
	}
}

static struct rk817_reg_val_typ playback_power_up_list[] = {
	{RK817_CODEC_AREF_RTCFG1, 0x40},
	{RK817_CODEC_DDAC_POPD_DACST, 0x02},
	{RK817_CODEC_DDAC_SR_LMT0, 0x02},
	/* {RK817_CODEC_DTOP_DIGEN_CLKE, 0x0f}, */
	/* APLL */
	{RK817_CODEC_APLL_CFG0, 0x04},
	{RK817_CODEC_APLL_CFG1, 0x58},
	{RK817_CODEC_APLL_CFG2, 0x2d},
	{RK817_CODEC_APLL_CFG3, 0x0c},
	{RK817_CODEC_APLL_CFG4, 0xa5},
	{RK817_CODEC_APLL_CFG5, 0x00},

	{RK817_CODEC_DI2S_RXCMD_TSD, 0x00},
	{RK817_CODEC_DI2S_RSD, 0x00},
	{RK817_CODEC_DI2S_CKM, 0x00},
	{RK817_CODEC_DI2S_RXCR1, 0x00},
	{RK817_CODEC_DI2S_RXCMD_TSD, 0x20},
	{RK817_CODEC_DTOP_VUCTIME, 0xf4},
	{RK817_CODEC_DDAC_MUTE_MIXCTL, 0x00},

	{RK817_CODEC_DDAC_VOLL, 0x0a},
	{RK817_CODEC_DDAC_VOLR, 0x0a},
};

#define RK817_CODEC_PLAYBACK_POWER_UP_LIST_LEN \
	ARRAY_SIZE(playback_power_up_list)

static struct rk817_reg_val_typ playback_power_down_list[] = {
	{RK817_CODEC_DDAC_MUTE_MIXCTL, 0x01},
	{RK817_CODEC_ADAC_CFG1, 0x0f},
	/* HP */
	{RK817_CODEC_AHP_CFG0, 0xe0},
	{RK817_CODEC_AHP_CP, 0x09},
	/* SPK */
	{RK817_CODEC_ACLASSD_CFG1, 0x69},
};

#define RK817_CODEC_PLAYBACK_POWER_DOWN_LIST_LEN \
	ARRAY_SIZE(playback_power_down_list)

static struct rk817_reg_val_typ capture_power_up_list[] = {
	{RK817_CODEC_AREF_RTCFG1, 0x40},
	{RK817_CODEC_DDAC_SR_LMT0, 0x02},
	{RK817_CODEC_DADC_SR_ACL0, 0x02},
	/* {RK817_CODEC_DTOP_DIGEN_CLKE, 0xff}, */
	{RK817_CODEC_APLL_CFG0, 0x04},
	{RK817_CODEC_APLL_CFG1, 0x58},
	{RK817_CODEC_APLL_CFG2, 0x2d},
	{RK817_CODEC_APLL_CFG3, 0x0c},
	{RK817_CODEC_APLL_CFG4, 0xa5},
	{RK817_CODEC_APLL_CFG5, 0x00},

	{RK817_CODEC_DI2S_RXCMD_TSD, 0x00},
	{RK817_CODEC_DI2S_RSD, 0x00},
	{RK817_CODEC_DI2S_CKM, 0x00},
	{RK817_CODEC_DI2S_RXCR1, 0x00},
	{RK817_CODEC_DI2S_RXCMD_TSD, 0x20},
	{RK817_CODEC_DTOP_VUCTIME, 0xf4},

	{RK817_CODEC_DDAC_MUTE_MIXCTL, 0x00},
	{RK817_CODEC_AADC_CFG0, 0x08},
	{RK817_CODEC_AMIC_CFG0, 0x0f},
	{RK817_CODEC_DI2S_TXCR3_TXCMD, 0x88},
	{RK817_CODEC_DDAC_POPD_DACST, 0x02},
	/* 0x29: -18db to 27db */
	{RK817_CODEC_DMIC_PGA_GAIN, 0xaa},
};

#define RK817_CODEC_CAPTURE_POWER_UP_LIST_LEN \
	ARRAY_SIZE(capture_power_up_list)

static struct rk817_reg_val_typ capture_power_down_list[] = {
	{RK817_CODEC_AADC_CFG0, 0xc8},
	{RK817_CODEC_AMIC_CFG0, 0x70},
};

#define RK817_CODEC_CAPTURE_POWER_DOWN_LIST_LEN \
	ARRAY_SIZE(capture_power_down_list)

static int rk817_codec_power_up(struct snd_soc_codec *codec, int type)
{
	struct rk817_codec_priv *rk817 = snd_soc_codec_get_drvdata(codec);
	int i;

	DBG("%s : power up %s %s %s\n", __func__,
	    type & RK817_CODEC_PLAYBACK ? "playback" : "",
	    type & RK817_CODEC_CAPTURE ? "capture" : "",
	    type & RK817_CODEC_INCALL ? "incall" : "");

	if (type & RK817_CODEC_PLAYBACK) {
		snd_soc_update_bits(codec, RK817_CODEC_DTOP_DIGEN_CLKE,
				    DAC_DIG_CLK_MASK, DAC_DIG_CLK_EN);
		for (i = 0; i < RK817_CODEC_PLAYBACK_POWER_UP_LIST_LEN; i++) {
			snd_soc_write(codec, playback_power_up_list[i].reg,
				      playback_power_up_list[i].value);
		}
	}

	if (type & RK817_CODEC_CAPTURE) {
		snd_soc_update_bits(codec, RK817_CODEC_DTOP_DIGEN_CLKE,
				    ADC_DIG_CLK_MASK, ADC_DIG_CLK_EN);
		for (i = 0; i < RK817_CODEC_CAPTURE_POWER_UP_LIST_LEN; i++) {
			snd_soc_write(codec, capture_power_up_list[i].reg,
				      capture_power_up_list[i].value);
		}

		if (rk817->mic_in_differential)
			snd_soc_update_bits(codec, RK817_CODEC_AMIC_CFG0,
					    MIC_DIFF_MASK, MIC_DIFF_EN);
		else
			snd_soc_update_bits(codec, RK817_CODEC_AMIC_CFG0,
					    MIC_DIFF_MASK, MIC_DIFF_DIS);

		if (rk817->pdmdata_out_enable)
			snd_soc_update_bits(codec, RK817_CODEC_DI2S_CKM,
					    PDM_EN_MASK, PDM_EN_ENABLE);

		snd_soc_write(codec, RK817_CODEC_DADC_VOLL,
			      rk817->capture_volume);
		snd_soc_write(codec, RK817_CODEC_DADC_VOLR,
			      rk817->capture_volume);
	}

	return 0;
}

static int rk817_codec_power_down(struct snd_soc_codec *codec, int type)
{
	int i;

	DBG("%s : power down %s %s %s\n", __func__,
	    type & RK817_CODEC_PLAYBACK ? "playback" : "",
	    type & RK817_CODEC_CAPTURE ? "capture" : "",
	    type & RK817_CODEC_INCALL ? "incall" : "");

	/* mute output for pop noise */
	if ((type & RK817_CODEC_PLAYBACK) ||
	    (type & RK817_CODEC_INCALL)) {
		snd_soc_update_bits(codec, RK817_CODEC_DDAC_MUTE_MIXCTL,
				    DACMT_ENABLE, DACMT_ENABLE);
	}

	if (type & RK817_CODEC_CAPTURE) {
		for (i = 0; i < RK817_CODEC_CAPTURE_POWER_DOWN_LIST_LEN; i++) {
			snd_soc_write(codec, capture_power_down_list[i].reg,
				      capture_power_down_list[i].value);
		}
		snd_soc_update_bits(codec, RK817_CODEC_DTOP_DIGEN_CLKE,
				    ADC_DIG_CLK_MASK, ADC_DIG_CLK_DIS);
	}

	if (type & RK817_CODEC_PLAYBACK) {
		for (i = 0; i < RK817_CODEC_PLAYBACK_POWER_DOWN_LIST_LEN; i++) {
			snd_soc_write(codec, playback_power_down_list[i].reg,
				      playback_power_down_list[i].value);
		}
		snd_soc_update_bits(codec, RK817_CODEC_DTOP_DIGEN_CLKE,
				    DAC_DIG_CLK_MASK, DAC_DIG_CLK_DIS);
	}

	if (type == RK817_CODEC_ALL) {
		for (i = 0; i < RK817_CODEC_PLAYBACK_POWER_DOWN_LIST_LEN; i++) {
			snd_soc_write(codec, playback_power_down_list[i].reg,
				      playback_power_down_list[i].value);
		}
		for (i = 0; i < RK817_CODEC_CAPTURE_POWER_DOWN_LIST_LEN; i++) {
			snd_soc_write(codec, capture_power_down_list[i].reg,
				      capture_power_down_list[i].value);
		}
		snd_soc_write(codec, RK817_CODEC_DTOP_DIGEN_CLKE, 0x00);
		snd_soc_write(codec, RK817_CODEC_APLL_CFG5, 0x01);
		snd_soc_write(codec, RK817_CODEC_AREF_RTCFG1, 0x06);
	}

	return 0;
}

/* For tiny alsa playback/capture/voice call path */
static const char * const rk817_playback_path_mode[] = {
	"OFF", "RCV", "SPK", "HP", "HP_NO_MIC", "BT", "SPK_HP", /* 0-6 */
	"RING_SPK", "RING_HP", "RING_HP_NO_MIC", "RING_SPK_HP"}; /* 7-10 */

static const char * const rk817_capture_path_mode[] = {
	"MIC OFF", "Main Mic", "Hands Free Mic", "BT Sco Mic"};

static const char * const rk817_call_path_mode[] = {
	"OFF", "RCV", "SPK", "HP", "HP_NO_MIC", "BT"}; /* 0-5 */

static const char * const rk817_modem_input_mode[] = {"OFF", "ON"};

static SOC_ENUM_SINGLE_DECL(rk817_playback_path_type,
	0, 0, rk817_playback_path_mode);

static SOC_ENUM_SINGLE_DECL(rk817_capture_path_type,
	0, 0, rk817_capture_path_mode);

static SOC_ENUM_SINGLE_DECL(rk817_call_path_type,
	0, 0, rk817_call_path_mode);

static SOC_ENUM_SINGLE_DECL(rk817_modem_input_type,
	0, 0, rk817_modem_input_mode);

static int rk817_playback_path_get(struct snd_kcontrol *kcontrol,
				   struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
	struct rk817_codec_priv *rk817 = snd_soc_codec_get_drvdata(codec);

	DBG("%s : playback_path %ld\n", __func__, rk817->playback_path);

	ucontrol->value.integer.value[0] = rk817->playback_path;

	return 0;
}

static int rk817_playback_path_put(struct snd_kcontrol *kcontrol,
				   struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
	struct rk817_codec_priv *rk817 = snd_soc_codec_get_drvdata(codec);
	long int pre_path;

	if (rk817->playback_path == ucontrol->value.integer.value[0]) {
		DBG("%s : playback_path is not changed!\n",
		    __func__);
		return 0;
	}

	pre_path = rk817->playback_path;
	rk817->playback_path = ucontrol->value.integer.value[0];

	DBG("%s : set playback_path %ld, pre_path %ld\n",
	    __func__, rk817->playback_path, pre_path);

	if (rk817->playback_path != OFF)
		clk_prepare_enable(rk817->mclk);
	else
		clk_disable_unprepare(rk817->mclk);

	switch (rk817->playback_path) {
	case OFF:
		if (pre_path != OFF && (pre_path != HP_PATH &&
			pre_path != HP_NO_MIC && pre_path != RING_HP &&
			pre_path != RING_HP_NO_MIC)) {
			rk817_codec_power_down(codec, RK817_CODEC_PLAYBACK);
			if (rk817->capture_path == 0)
				rk817_codec_power_down(codec, RK817_CODEC_ALL);
		}
		break;
	case RCV:
	case SPK_PATH:
	case RING_SPK:
		if (pre_path == OFF)
			rk817_codec_power_up(codec, RK817_CODEC_PLAYBACK);
		/* power on dac ibias/l/r */
		snd_soc_write(codec, RK817_CODEC_ADAC_CFG1,
			      PWD_DACBIAS_ON | PWD_DACD_ON |
			      PWD_DACL_ON | PWD_DACR_ON);
		/* CLASS D mode */
		snd_soc_write(codec, RK817_CODEC_DDAC_MUTE_MIXCTL, 0x10);
		/* CLASS D enable */
		snd_soc_write(codec, RK817_CODEC_ACLASSD_CFG1, 0xa5);
		/* restart CLASS D, OCPP/N */
		snd_soc_write(codec, RK817_CODEC_ACLASSD_CFG2, 0xc4);

		snd_soc_write(codec, RK817_CODEC_DDAC_VOLL, rk817->spk_volume);
		snd_soc_write(codec, RK817_CODEC_DDAC_VOLR, rk817->spk_volume);
		break;
	case HP_PATH:
	case HP_NO_MIC:
	case RING_HP:
	case RING_HP_NO_MIC:
		if (pre_path == OFF)
			rk817_codec_power_up(codec, RK817_CODEC_PLAYBACK);
		/* HP_CP_EN , CP 2.3V */
		snd_soc_write(codec, RK817_CODEC_AHP_CP, 0x11);
		/* power on HP two stage opamp ,HP amplitude 0db */
		snd_soc_write(codec, RK817_CODEC_AHP_CFG0, 0x80);
		/* power on dac ibias/l/r */
		snd_soc_write(codec, RK817_CODEC_ADAC_CFG1,
			      PWD_DACBIAS_ON | PWD_DACD_DOWN |
			      PWD_DACL_ON | PWD_DACR_ON);
		snd_soc_update_bits(codec, RK817_CODEC_DDAC_MUTE_MIXCTL,
				    DACMT_ENABLE, DACMT_DISABLE);

		snd_soc_write(codec, RK817_CODEC_DDAC_VOLL, rk817->hp_volume);
		snd_soc_write(codec, RK817_CODEC_DDAC_VOLR, rk817->hp_volume);
		break;
	case BT:
		break;
	case SPK_HP:
	case RING_SPK_HP:
		if (pre_path == OFF)
			rk817_codec_power_up(codec, RK817_CODEC_PLAYBACK);
		/* HP_CP_EN , CP 2.3V  */
		snd_soc_write(codec, RK817_CODEC_AHP_CP, 0x11);
		/* power on HP two stage opamp ,HP amplitude 0db */
		snd_soc_write(codec, RK817_CODEC_AHP_CFG0, 0x80);

		/* power on dac ibias/l/r */
		snd_soc_write(codec, RK817_CODEC_ADAC_CFG1,
			      PWD_DACBIAS_ON | PWD_DACD_ON |
			      PWD_DACL_ON | PWD_DACR_ON);
		/* CLASS D mode */
		snd_soc_write(codec, RK817_CODEC_DDAC_MUTE_MIXCTL, 0x10);
		/* CLASS D enable */
		snd_soc_write(codec, RK817_CODEC_ACLASSD_CFG1, 0xa5);
		/* restart CLASS D, OCPP/N */
		snd_soc_write(codec, RK817_CODEC_ACLASSD_CFG2, 0xc4);

		snd_soc_write(codec, RK817_CODEC_DDAC_VOLL, rk817->hp_volume);
		snd_soc_write(codec, RK817_CODEC_DDAC_VOLR, rk817->hp_volume);
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int rk817_capture_path_get(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
	struct rk817_codec_priv *rk817 = snd_soc_codec_get_drvdata(codec);

	DBG("%s : capture_path %ld\n", __func__,
	    rk817->capture_path);

	ucontrol->value.integer.value[0] = rk817->capture_path;

	return 0;
}

static int rk817_capture_path_put(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
	struct rk817_codec_priv *rk817 = snd_soc_codec_get_drvdata(codec);
	long int pre_path;

	if (rk817->capture_path == ucontrol->value.integer.value[0]) {
		DBG("%s : capture_path is not changed!\n",
		    __func__);
		return 0;
	}

	pre_path = rk817->capture_path;
	rk817->capture_path = ucontrol->value.integer.value[0];

	DBG("%s : set capture_path %ld, pre_path %ld\n", __func__,
	    rk817->capture_path, pre_path);

	if (rk817->capture_path != MIC_OFF)
		clk_prepare_enable(rk817->mclk);
	else
		clk_disable_unprepare(rk817->mclk);

	switch (rk817->capture_path) {
	case MIC_OFF:
		if (pre_path != MIC_OFF)
			rk817_codec_power_down(codec, RK817_CODEC_CAPTURE);
		break;
	case MAIN_MIC:
		if (pre_path == MIC_OFF)
			rk817_codec_power_up(codec, RK817_CODEC_CAPTURE);

		if (!rk817->mic_in_differential) {
			snd_soc_write(codec, RK817_CODEC_DADC_VOLR, 0xff);
			snd_soc_update_bits(codec, RK817_CODEC_AADC_CFG0,
					    ADC_R_PWD_MASK, ADC_R_PWD_EN);
			snd_soc_update_bits(codec, RK817_CODEC_AMIC_CFG0,
					    PWD_PGA_R_MASK, PWD_PGA_R_EN);
		}
		break;
	case HANDS_FREE_MIC:
		if (pre_path == MIC_OFF)
			rk817_codec_power_up(codec, RK817_CODEC_CAPTURE);

		if (!rk817->mic_in_differential) {
			snd_soc_write(codec, RK817_CODEC_DADC_VOLL, 0xff);
			snd_soc_update_bits(codec, RK817_CODEC_AADC_CFG0,
					    ADC_L_PWD_MASK, ADC_L_PWD_EN);
			snd_soc_update_bits(codec, RK817_CODEC_AMIC_CFG0,
					    PWD_PGA_L_MASK, PWD_PGA_L_EN);
			}
		break;
	case BT_SCO_MIC:
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static struct snd_kcontrol_new rk817_snd_path_controls[] = {
	SOC_ENUM_EXT("Playback Path", rk817_playback_path_type,
		     rk817_playback_path_get, rk817_playback_path_put),

	SOC_ENUM_EXT("Capture MIC Path", rk817_capture_path_type,
		     rk817_capture_path_get, rk817_capture_path_put),
};

static int rk817_set_dai_sysclk(struct snd_soc_dai *codec_dai,
				int clk_id, unsigned int freq, int dir)
{
	struct snd_soc_codec *codec = codec_dai->codec;
	struct rk817_codec_priv *rk817 = snd_soc_codec_get_drvdata(codec);

	rk817->stereo_sysclk = freq;

	DBG("%s : MCLK = %dHz\n", __func__, rk817->stereo_sysclk);

	return 0;
}

static int rk817_set_dai_fmt(struct snd_soc_dai *codec_dai,
			     unsigned int fmt)
{
	struct snd_soc_codec *codec = codec_dai->codec;
	unsigned int i2s_mst = 0;

	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBS_CFS:
		i2s_mst |= RK817_I2S_MODE_SLV;
		break;
	case SND_SOC_DAIFMT_CBM_CFM:
		i2s_mst |= RK817_I2S_MODE_MST;
		break;
	default:
		dev_err(codec->dev, "%s : set master mask failed!\n",
			__func__);
		return -EINVAL;
	}
	DBG("%s : i2s %s mode\n", __func__,
	    i2s_mst ? "master" : "slave");

	snd_soc_update_bits(codec, RK817_CODEC_DI2S_CKM,
			    RK817_I2S_MODE_MASK, i2s_mst);

	return 0;
}

static int rk817_hw_params(struct snd_pcm_substream *substream,
			   struct snd_pcm_hw_params *params,
			    struct snd_soc_dai *dai)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_codec *codec = rtd->codec;
	struct rk817_codec_priv *rk817 = snd_soc_codec_get_drvdata(codec);
	unsigned int rate = params_rate(params);

	DBG("%s : MCLK = %dHz, sample rate = %dHz\n",
	    __func__, rk817->stereo_sysclk, rate);

	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
		snd_soc_write(codec, RK817_CODEC_DI2S_RXCR2, VDW_RX_16BITS);
		snd_soc_write(codec, RK817_CODEC_DI2S_TXCR2, VDW_TX_16BITS);
		break;
	case SNDRV_PCM_FORMAT_S24_LE:
		snd_soc_write(codec, RK817_CODEC_DI2S_RXCR2, VDW_RX_24BITS);
		snd_soc_write(codec, RK817_CODEC_DI2S_TXCR2, VDW_TX_24BITS);
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int rk817_digital_mute(struct snd_soc_dai *dai, int mute)
{
	struct snd_soc_codec *codec = dai->codec;

	DBG("%s %d\n", __func__, mute);
	if (mute)
		snd_soc_update_bits(codec, RK817_CODEC_DDAC_MUTE_MIXCTL,
				    DACMT_ENABLE, DACMT_ENABLE);
	else
		snd_soc_update_bits(codec, RK817_CODEC_DDAC_MUTE_MIXCTL,
				    DACMT_ENABLE, DACMT_DISABLE);

	return 0;
}

#define RK817_PLAYBACK_RATES (SNDRV_PCM_RATE_8000 |\
			      SNDRV_PCM_RATE_16000 |	\
			      SNDRV_PCM_RATE_32000 |	\
			      SNDRV_PCM_RATE_44100 |	\
			      SNDRV_PCM_RATE_48000 |	\
			      SNDRV_PCM_RATE_96000)

#define RK817_CAPTURE_RATES (SNDRV_PCM_RATE_8000 |\
			      SNDRV_PCM_RATE_16000 |	\
			      SNDRV_PCM_RATE_32000 |	\
			      SNDRV_PCM_RATE_44100 |	\
			      SNDRV_PCM_RATE_48000 |	\
			      SNDRV_PCM_RATE_96000)

#define RK817_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
			SNDRV_PCM_FMTBIT_S20_3LE |\
			SNDRV_PCM_FMTBIT_S24_LE |\
			SNDRV_PCM_FMTBIT_S32_LE)

static struct snd_soc_dai_ops rk817_dai_ops = {
	.hw_params	= rk817_hw_params,
	.set_fmt	= rk817_set_dai_fmt,
	.set_sysclk	= rk817_set_dai_sysclk,
	.digital_mute	= rk817_digital_mute,
};

static struct snd_soc_dai_driver rk817_dai[] = {
	{
		.name = "rk817-hifi",
		.id = RK817_HIFI,
		.playback = {
			.stream_name = "HiFi Playback",
			.channels_min = 2,
			.channels_max = 8,
			.rates = RK817_PLAYBACK_RATES,
			.formats = RK817_FORMATS,
		},
		.capture = {
			.stream_name = "HiFi Capture",
			.channels_min = 2,
			.channels_max = 2,
			.rates = RK817_CAPTURE_RATES,
			.formats = RK817_FORMATS,
		},
		.ops = &rk817_dai_ops,
	},
};

static int rk817_suspend(struct snd_soc_codec *codec)
{
	rk817_codec_power_down(codec, RK817_CODEC_ALL);
	return 0;
}

static int rk817_resume(struct snd_soc_codec *codec)
{
	return 0;
}

static int rk817_probe(struct snd_soc_codec *codec)
{
	struct rk817_codec_priv *rk817 = snd_soc_codec_get_drvdata(codec);

	DBG("%s\n", __func__);

	if (!rk817) {
		dev_err(codec->dev, "%s : rk817 priv is NULL!\n",
			__func__);
		return -EINVAL;
	}
	rk817->codec = codec;
	rk817->playback_path = OFF;
	rk817->capture_path = MIC_OFF;

	snd_soc_add_codec_controls(codec, rk817_snd_path_controls,
				   ARRAY_SIZE(rk817_snd_path_controls));
	return 0;
}

/* power down chip */
static int rk817_remove(struct snd_soc_codec *codec)
{
	struct rk817_codec_priv *rk817 = snd_soc_codec_get_drvdata(codec);

	DBG("%s\n", __func__);

	if (!rk817) {
		dev_err(codec->dev, "%s : rk817 is NULL\n", __func__);
		return 0;
	}

	rk817_codec_power_down(codec, RK817_CODEC_ALL);
	mdelay(10);

	return 0;
}

static struct regmap *rk817_get_regmap(struct device *dev)
{
	struct rk817_codec_priv *rk817 = dev_get_drvdata(dev);

	return rk817->regmap;
}

static struct snd_soc_codec_driver soc_codec_dev_rk817 = {
	.probe = rk817_probe,
	.remove = rk817_remove,
	.get_regmap = rk817_get_regmap,
	.suspend = rk817_suspend,
	.resume = rk817_resume,
};

static int rk817_codec_parse_dt_property(struct device *dev,
					 struct rk817_codec_priv *rk817)
{
	struct device_node *node = dev->parent->of_node;
	int ret;

	DBG("%s()\n", __func__);

	if (!node) {
		dev_err(dev, "%s() dev->parent->of_node is NULL\n",
			__func__);
		return -ENODEV;
	}

	node = of_get_child_by_name(dev->parent->of_node, "codec");
	if (!node) {
		dev_err(dev, "%s() Can not get child: codec\n",
			__func__);
		return -ENODEV;
	}

	ret = of_property_read_u32(node, "skp-volume", &rk817->spk_volume);
	if (ret < 0) {
		DBG("%s() Can not read property skp-volume\n", __func__);
		rk817->spk_volume = OUT_VOLUME;
	}
	if (rk817->spk_volume < 3)
		rk817->spk_volume = 3;

	ret = of_property_read_u32(node, "hp-volume",
				   &rk817->hp_volume);
	if (ret < 0) {
		DBG("%s() Can not read property hp-volume\n",
		    __func__);
		rk817->hp_volume = OUT_VOLUME;
	}
	if (rk817->hp_volume < 3)
		rk817->hp_volume = 3;

	ret = of_property_read_u32(node, "capture-volume",
				   &rk817->capture_volume);
	if (ret < 0) {
		DBG("%s() Can not read property capture-volume\n",
		    __func__);
		rk817->capture_volume = CAPTURE_VOLUME;
	}

	rk817->mic_in_differential =
			of_property_read_bool(node, "mic-in-differential");

	rk817->pdmdata_out_enable =
			of_property_read_bool(node, "pdmdata-out-enable");

	return 0;
}

static const struct regmap_config rk817_codec_regmap_config = {
	.name = "rk817-codec",
	.reg_bits = 8,
	.val_bits = 8,
	.reg_stride = 1,
	.max_register = 0x4f,
	.cache_type = REGCACHE_NONE,
	.volatile_reg = rk817_volatile_register,
	.writeable_reg = rk817_codec_register,
	.readable_reg = rk817_codec_register,
	.reg_defaults = rk817_reg_defaults,
	.num_reg_defaults = ARRAY_SIZE(rk817_reg_defaults),
};

static int rk817_platform_probe(struct platform_device *pdev)
{
	struct rk808 *rk817 = dev_get_drvdata(pdev->dev.parent);
	struct rk817_codec_priv *rk817_codec_data;
	int ret;

	DBG("%s\n", __func__);

	if (!rk817) {
		dev_err(&pdev->dev, "%s : rk817 is NULL\n", __func__);
		return -EINVAL;
	}

	rk817_codec_data = devm_kzalloc(&pdev->dev,
					sizeof(struct rk817_codec_priv),
					GFP_KERNEL);
	if (!rk817_codec_data)
		return -ENOMEM;

	platform_set_drvdata(pdev, rk817_codec_data);

	ret = rk817_codec_parse_dt_property(&pdev->dev, rk817_codec_data);
	if (ret < 0) {
		dev_err(&pdev->dev, "%s() parse device tree property error %d\n",
			__func__, ret);
		goto err_;
	}

	rk817_codec_data->regmap = devm_regmap_init_i2c(rk817->i2c,
					    &rk817_codec_regmap_config);
	if (IS_ERR(rk817_codec_data->regmap)) {
		ret = PTR_ERR(rk817_codec_data->regmap);
		dev_err(&pdev->dev, "failed to allocate register map: %d\n",
			ret);
		goto err_;
	}

	rk817_codec_data->mclk = devm_clk_get(&pdev->dev, "mclk");
	if (IS_ERR(rk817_codec_data->mclk)) {
		dev_err(&pdev->dev, "Unable to get mclk\n");
		ret = -ENXIO;
		goto err_;
	}

	ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_rk817,
				     rk817_dai, ARRAY_SIZE(rk817_dai));
	if (ret < 0) {
		dev_err(&pdev->dev, "%s() register codec error %d\n",
			__func__, ret);
		goto err_;
	}

	return 0;
err_:

	return ret;
}

static int rk817_platform_remove(struct platform_device *pdev)
{
	snd_soc_unregister_codec(&pdev->dev);

	return 0;
}

static void rk817_platform_shutdown(struct platform_device *pdev)
{
	struct rk817_codec_priv *rk817 = dev_get_drvdata(&pdev->dev);

	DBG("%s\n", __func__);

	rk817_codec_power_down(rk817->codec, RK817_CODEC_ALL);

}

static const struct of_device_id rk817_codec_dt_ids[] = {
	{ .compatible = "rockchip,rk817-codec" },
	{},
};
MODULE_DEVICE_TABLE(of, rk817_codec_dt_ids);

static struct platform_driver rk817_codec_driver = {
	.driver = {
		   .name = "rk817-codec",
		   .of_match_table = rk817_codec_dt_ids,
		   },
	.probe = rk817_platform_probe,
	.remove = rk817_platform_remove,
	.shutdown = rk817_platform_shutdown,
};

module_platform_driver(rk817_codec_driver);

MODULE_DESCRIPTION("ASoC RK817 codec driver");
MODULE_AUTHOR("binyuan <kevan.lan@rock-chips.com>");
MODULE_LICENSE("GPL v2");