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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
// SPDX-License-Identifier: GPL-2.0-only
/*
 * es8389.c  --  ES8389 ALSA SoC Audio Codec
 *
 * Copyright Everest Semiconductor Co., Ltd
 *
 * Authors:  Michael Zhang (zhangyi@everest-semi.com)
 *
 * 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.
 */

#include <linux/clk.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/tlv.h>
#include <sound/soc.h>

#include "es8389.h"


/* codec private data */

struct	es8389_private {
	struct regmap *regmap;
	struct clk *mclk;
	struct regulator_bulk_data core_supply[2];
	unsigned int sysclk;
	int mastermode;

	u8 hpfl;
	u8 hpfr;
	u32 hpf_freq;
	u32 capture_rate;
	u8 mclk_src;
	u8 vddd;
	int version;
	enum snd_soc_bias_level bias_level;
};

static const char * const es8389_core_supplies[] = {
	"vddd",
	"vdda",
};

static bool es8389_volatile_register(struct device *dev,
			unsigned int reg)
{
	switch (reg) {
	case ES8389_ADCL_VOL:
	case ES8389_ADCR_VOL:
	case ES8389_MIC1_GAIN:
	case ES8389_MIC2_GAIN:
	case ES8389_DACL_VOL:
	case ES8389_DACR_VOL:
	case ES8389_ALC_ON:
	case ES8389_ALC_CTL:
	case ES8389_ALC_TARGET:
	case ES8389_ALC_GAIN:
	case ES8389_ADC_MUTE:
	case ES8389_OSR_VOL:
	case ES8389_DAC_INV:
	case ES8389_MIX_VOL:
	case ES8389_DAC_MIX:
	case ES8389_ADC_RESET:
	case ES8389_ADC_MODE:
	case ES8389_DMIC_EN:
		return false;
	default:
		return true;
	}
}

static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -9550, 50, 0);
static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -9550, 50, 0);
static const DECLARE_TLV_DB_SCALE(pga_vol_tlv, 0, 300, 0);
static const DECLARE_TLV_DB_SCALE(mix_vol_tlv, -9500, 100, 0);
static const DECLARE_TLV_DB_SCALE(alc_target_tlv, -3200, 200, 0);
static const DECLARE_TLV_DB_SCALE(alc_max_level, -3200, 200, 0);

static const u32 hpf_table[10][10] = {
	{1020, 754, 624, 559, 527, 511, 502, 498, 497, 496},
	{754, 495, 368, 306, 274, 259, 251, 247, 246, 244},
	{624, 368, 243, 182, 151, 136, 128, 124, 123, 121},
	{559, 306, 182, 120, 90, 75, 68, 63, 62, 60},
	{527, 274, 151, 90, 60, 45, 38, 33, 32, 31},
	{511, 259, 136, 75, 45, 30, 23, 19, 18, 17},
	{502, 251, 128, 68, 38, 23, 16, 13, 11, 11},
	{498, 247, 124, 63, 33, 19, 13, 10, 8, 8},
	{497, 246, 123, 62, 32, 18, 11, 8, 8, 0},
	{496, 244, 121, 60, 31, 17, 11, 8, 0, 0}
};

static bool find_best_hpf_freq(u32 target_hz, u8 *hpf1, u8 *hpf2, u32 *out)
{
	int best_row = -1, best_col = -1;
	u32 min_diff = U32_MAX;
	u32 f, diff;
	int i, j;

	if (target_hz > 1020)
		return false;

	for (i = 0; i < 10; i++) {
		for (j = i; j < 10; j++) {
			f = hpf_table[i][j];

			diff = (target_hz > f) ? (target_hz - f) : (f - target_hz);
			if (diff < min_diff) {
				min_diff = diff;
				best_row = i;
				best_col = j;
				*out = f;
			}
		}
	}

	*hpf1 = best_col + ES8389_HPF_OFFSET;
	*hpf2 = best_row + ES8389_HPF_OFFSET;

	return true;
}

static int es8389_hpf_get(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
	struct es8389_private *es8389 = snd_soc_component_get_drvdata(component);

	ucontrol->value.integer.value[0] = es8389->hpf_freq;
	return 0;
}

static int es8389_hpf_set(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
	struct es8389_private *es8389 = snd_soc_component_get_drvdata(component);
	u32 freq;
	bool hpf;

	if (es8389->hpf_freq == ucontrol->value.integer.value[0])
		return 0;

	if (es8389->capture_rate) {
		freq = (ucontrol->value.integer.value[0] * 48000) / es8389->capture_rate;

		hpf = find_best_hpf_freq(freq, &es8389->hpfl, &es8389->hpfr, &es8389->hpf_freq);
		if (!hpf)
			return -EBUSY;

		if (es8389->hpf_freq != ucontrol->value.integer.value[0])
			dev_dbg(component->dev, "At the %u Hz sampling rate, %ld Hz could not be obtained."
				"the frequency has been set to the closest value, %u Hz\n",
				es8389->capture_rate, ucontrol->value.integer.value[0], es8389->hpf_freq);

		regmap_update_bits(es8389->regmap, ES8389_ADC_HPF1, 0x0f, es8389->hpfl);
		regmap_update_bits(es8389->regmap, ES8389_ADC_HPF2, 0x0f, es8389->hpfr);
	} else {
		es8389->hpf_freq = ucontrol->value.integer.value[0];
		dev_dbg(component->dev, "PCM_STREAM_CAPTURE is not active.retain the input frequency\n");
	}

	return 1;
}

static int es8389_dmic_set(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *component = snd_soc_dapm_kcontrol_to_component(kcontrol);
	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
	struct es8389_private *es8389 = snd_soc_component_get_drvdata(component);
	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
	unsigned int val;
	bool changed1, changed2;

	val = ucontrol->value.integer.value[0];
	if (val > 1)
		return -EINVAL;

	if (val) {
		regmap_update_bits_check(es8389->regmap, ES8389_DMIC_EN, 0xC0, 0xC0, &changed1);
		regmap_update_bits_check(es8389->regmap, ES8389_ADC_MODE, 0x03, 0x03, &changed2);
	} else {
		regmap_update_bits_check(es8389->regmap, ES8389_DMIC_EN, 0xC0, 0x00, &changed1);
		regmap_update_bits_check(es8389->regmap, ES8389_ADC_MODE, 0x03, 0x00, &changed2);
	}

	if (changed1 & changed2)
		return snd_soc_dapm_mux_update_power(dapm, kcontrol, val, e, NULL);
	else
		return 0;
}

static const char *const alc[] = {
	"ALC OFF",
	"ADCR ALC ON",
	"ADCL ALC ON",
	"ADCL & ADCL ALC ON",
};

static const char *const ramprate[] = {
	"0.125db/1  LRCK",
	"0.125db/4  LRCK",
	"0.125db/8  LRCK",
	"0.125db/16  LRCK",
	"0.125db/32  LRCK",
	"0.125db/64  LRCK",
	"0.125db/128  LRCK",
	"0.125db/256  LRCK",
	"0.125db/512  LRCK",
	"0.125db/1024  LRCK",
	"0.125db/2048  LRCK",
	"0.125db/4096  LRCK",
	"0.125db/8192  LRCK",
	"0.125db/16384  LRCK",
	"0.125db/32768  LRCK",
	"0.125db/65536  LRCK",
};

static const char *const winsize[] = {
	"2 LRCK",
	"4  LRCK",
	"8  LRCK",
	"16  LRCK",
	"32  LRCK",
	"64  LRCK",
	"128  LRCK",
	"256  LRCK",
	"512  LRCK",
	"1024  LRCK",
	"2048  LRCK",
	"4096  LRCK",
	"8192  LRCK",
	"16384  LRCK",
	"32768  LRCK",
	"65536  LRCK",
};

static const struct soc_enum alc_enable =
	SOC_ENUM_SINGLE(ES8389_ALC_ON, 5, 4, alc);
static const struct soc_enum alc_ramprate =
	SOC_ENUM_SINGLE(ES8389_ALC_CTL, 4, 16, ramprate);
static const struct soc_enum alc_winsize =
	SOC_ENUM_SINGLE(ES8389_ALC_CTL, 0, 16, winsize);

static const char *const es8389_adcl_mux_txt[] = {
	"Normal",
	"ADC2 channel to ADC1 channel",
};

static const char *const es8389_adcr_mux_txt[] = {
	"Normal",
	"ADC1 channel to ADC2 channel",
};

static const char *const es8389_outl_mux_txt[] = {
	"Normal",
	"DAC2 channel to DAC1 channel",
};

static const char *const es8389_outr_mux_txt[] = {
	"Normal",
	"DAC1 channel to DAC2 channel",
};

static const char *const es8389_dmic_mux_txt[] = {
	"AMIC",
	"DMIC",
};

static const char *const es8389_pga1_texts[] = {
	"DifferentialL",  "Line 1P", "Line 2P"
};

static const char *const es8389_pga2_texts[] = {
	"DifferentialR",  "Line 2N", "Line 1N"
};

static const unsigned int es8389_pga_values[] = {
	1, 5, 6
};

static const struct soc_enum es8389_adcl_mux_enum =
	SOC_ENUM_SINGLE(ES8389_ADC_MODE, 5,
			ARRAY_SIZE(es8389_adcl_mux_txt), es8389_adcl_mux_txt);

static const struct snd_kcontrol_new es8389_adcl_mux_controls =
	SOC_DAPM_ENUM("INPUTL MUX", es8389_adcl_mux_enum);

static const struct soc_enum es8389_adcr_mux_enum =
	SOC_ENUM_SINGLE(ES8389_ADC_MODE, 4,
			ARRAY_SIZE(es8389_adcr_mux_txt), es8389_adcr_mux_txt);

static const struct snd_kcontrol_new es8389_adcr_mux_controls =
	SOC_DAPM_ENUM("INPUTR MUX", es8389_adcr_mux_enum);

static const struct soc_enum es8389_outl_mux_enum =
	SOC_ENUM_SINGLE(ES8389_DAC_MIX, 5,
			ARRAY_SIZE(es8389_outl_mux_txt), es8389_outl_mux_txt);

static const struct snd_kcontrol_new es8389_outl_mux_controls =
	SOC_DAPM_ENUM("OUTL MUX", es8389_outl_mux_enum);

static const struct soc_enum es8389_outr_mux_enum =
	SOC_ENUM_SINGLE(ES8389_DAC_MIX, 4,
			ARRAY_SIZE(es8389_outr_mux_txt), es8389_outr_mux_txt);

static const struct snd_kcontrol_new es8389_outr_mux_controls =
	SOC_DAPM_ENUM("OUTR MUX", es8389_outr_mux_enum);

static SOC_ENUM_SINGLE_DECL(
	es8389_dmic_mux_enum, ES8389_DMIC_EN, 6, es8389_dmic_mux_txt);

static const struct soc_enum es8389_pgal_enum =
	SOC_VALUE_ENUM_SINGLE(ES8389_MIC1_GAIN, 4, 7,
			ARRAY_SIZE(es8389_pga1_texts), es8389_pga1_texts,
			es8389_pga_values);

static const struct soc_enum es8389_pgar_enum =
	SOC_VALUE_ENUM_SINGLE(ES8389_MIC2_GAIN, 4, 7,
			ARRAY_SIZE(es8389_pga2_texts), es8389_pga2_texts,
			es8389_pga_values);

static const struct snd_kcontrol_new es8389_dmic_mux_controls =
	SOC_DAPM_ENUM_EXT("ADC MUX", es8389_dmic_mux_enum,
			snd_soc_dapm_get_enum_double, es8389_dmic_set);

static const struct snd_kcontrol_new es8389_left_mixer_controls[] = {
	SOC_DAPM_SINGLE("DACR DACL Mixer", ES8389_DAC_MIX, 3, 1, 0),
};

static const struct snd_kcontrol_new es8389_right_mixer_controls[] = {
	SOC_DAPM_SINGLE("DACL DACR Mixer", ES8389_DAC_MIX, 2, 1, 0),
};

static const struct snd_kcontrol_new es8389_leftadc_mixer_controls[] = {
	SOC_DAPM_SINGLE("ADCL DACL Mixer", ES8389_DAC_MIX, 1, 1, 0),
};

static const struct snd_kcontrol_new es8389_rightadc_mixer_controls[] = {
	SOC_DAPM_SINGLE("ADCR DACR Mixer", ES8389_DAC_MIX, 0, 1, 0),
};

static const struct snd_kcontrol_new es8389_adc_mixer_controls[] = {
	SOC_DAPM_SINGLE("DACL ADCL Mixer", ES8389_ADC_RESET, 7, 1, 0),
	SOC_DAPM_SINGLE("DACR ADCR Mixer", ES8389_ADC_RESET, 6, 1, 0),
};

static const struct snd_kcontrol_new es8389_snd_controls[] = {
	SOC_SINGLE_TLV("ADCL Capture Volume", ES8389_ADCL_VOL, 0, 0xFF, 0, adc_vol_tlv),
	SOC_SINGLE_TLV("ADCR Capture Volume", ES8389_ADCR_VOL, 0, 0xFF, 0, adc_vol_tlv),
	SOC_SINGLE_TLV("ADCL PGA Volume", ES8389_MIC1_GAIN, 0, 0x0E, 0, pga_vol_tlv),
	SOC_SINGLE_TLV("ADCR PGA Volume", ES8389_MIC2_GAIN, 0, 0x0E, 0, pga_vol_tlv),

	SOC_ENUM("PGAL Select", es8389_pgal_enum),
	SOC_ENUM("PGAR Select", es8389_pgar_enum),
	SOC_ENUM("ALC Capture Switch", alc_enable),
	SOC_SINGLE_TLV("ALC Capture Target Level", ES8389_ALC_TARGET,
			0, 0x0f, 0, alc_target_tlv),
	SOC_SINGLE_TLV("ALC Capture Max Gain", ES8389_ALC_GAIN,
			0, 0x0f, 0, alc_max_level),
	SOC_ENUM("ADC Ramp Rate", alc_ramprate),
	SOC_ENUM("ALC Capture Winsize", alc_winsize),
	SOC_DOUBLE("ADC OSR Volume ON Switch", ES8389_ADC_MUTE, 6, 7, 1, 0),
	SOC_SINGLE_TLV("ADC OSR Volume", ES8389_OSR_VOL, 0, 0xFF, 0, adc_vol_tlv),
	SOC_DOUBLE("ADC OUTPUT Invert Switch", ES8389_ADC_HPF2, 5, 6, 1, 0),
	SOC_SINGLE_EXT("ADC HPF Freq Select", SND_SOC_NOPM, 0, 1020, 0,
				es8389_hpf_get, es8389_hpf_set),

	SOC_SINGLE_TLV("DACL Playback Volume", ES8389_DACL_VOL, 0, 0xFF, 0, dac_vol_tlv),
	SOC_SINGLE_TLV("DACR Playback Volume", ES8389_DACR_VOL, 0, 0xFF, 0, dac_vol_tlv),
	SOC_DOUBLE("DAC OUTPUT Invert Switch", ES8389_DAC_INV, 5, 6, 1, 0),
	SOC_SINGLE_TLV("ADC2DAC Mixer Volume", ES8389_MIX_VOL, 0, 0x7F, 0, mix_vol_tlv),
};

static const struct snd_soc_dapm_widget es8389_dapm_widgets[] = {
	/*Input Side*/
	SND_SOC_DAPM_INPUT("INPUT1"),
	SND_SOC_DAPM_INPUT("INPUT2"),
	SND_SOC_DAPM_INPUT("DMIC"),
	SND_SOC_DAPM_PGA("PGAL", SND_SOC_NOPM, 4, 0, NULL, 0),
	SND_SOC_DAPM_PGA("PGAR", SND_SOC_NOPM, 4, 0, NULL, 0),

	/*ADCs*/
	SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),

	/* Audio Interface */
	SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S Capture", 0, SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_AIF_IN("I2S IN", "I2S Playback", 0, SND_SOC_NOPM, 0, 0),

	/*DACs*/
	SND_SOC_DAPM_DAC("DACL", NULL, SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_DAC("DACR", NULL, SND_SOC_NOPM, 0, 0),

	/*Output Side*/
	SND_SOC_DAPM_OUTPUT("HPOL"),
	SND_SOC_DAPM_OUTPUT("HPOR"),

	/* Digital Interface */
	SND_SOC_DAPM_PGA("IF DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
	SND_SOC_DAPM_PGA("IF DACL1", SND_SOC_NOPM, 0, 0, NULL, 0),
	SND_SOC_DAPM_PGA("IF DACR1", SND_SOC_NOPM, 0, 0, NULL, 0),
	SND_SOC_DAPM_PGA("IF DACL2", SND_SOC_NOPM, 0, 0, NULL, 0),
	SND_SOC_DAPM_PGA("IF DACR2", SND_SOC_NOPM, 0, 0, NULL, 0),
	SND_SOC_DAPM_PGA("IF DACL3", SND_SOC_NOPM, 0, 0, NULL, 0),
	SND_SOC_DAPM_PGA("IF DACR3", SND_SOC_NOPM, 0, 0, NULL, 0),

	/* Digital Interface Select */
	SND_SOC_DAPM_MIXER("IF DACL Mixer", SND_SOC_NOPM, 0, 0,
			   &es8389_left_mixer_controls[0],
			   ARRAY_SIZE(es8389_left_mixer_controls)),
	SND_SOC_DAPM_MIXER("IF DACR Mixer", SND_SOC_NOPM, 0, 0,
			   &es8389_right_mixer_controls[0],
			   ARRAY_SIZE(es8389_right_mixer_controls)),
	SND_SOC_DAPM_MIXER("IF ADCDACL Mixer", SND_SOC_NOPM, 0, 0,
			   &es8389_leftadc_mixer_controls[0],
			   ARRAY_SIZE(es8389_leftadc_mixer_controls)),
	SND_SOC_DAPM_MIXER("IF ADCDACR Mixer", SND_SOC_NOPM, 0, 0,
			   &es8389_rightadc_mixer_controls[0],
			   ARRAY_SIZE(es8389_rightadc_mixer_controls)),

	SND_SOC_DAPM_MIXER("ADC Mixer", SND_SOC_NOPM, 0, 0,
			   &es8389_adc_mixer_controls[0],
			   ARRAY_SIZE(es8389_adc_mixer_controls)),
	SND_SOC_DAPM_MUX("ADC MUX", SND_SOC_NOPM, 0, 0, &es8389_dmic_mux_controls),
	SND_SOC_DAPM_MUX("INPUTL MUX", SND_SOC_NOPM, 0, 0, &es8389_adcl_mux_controls),
	SND_SOC_DAPM_MUX("INPUTR MUX", SND_SOC_NOPM, 0, 0, &es8389_adcr_mux_controls),

	SND_SOC_DAPM_MUX("OUTL MUX", SND_SOC_NOPM, 0, 0, &es8389_outl_mux_controls),
	SND_SOC_DAPM_MUX("OUTR MUX", SND_SOC_NOPM, 0, 0, &es8389_outr_mux_controls),
};


static const struct snd_soc_dapm_route es8389_dapm_routes[] = {
	{"PGAL", NULL, "INPUT1"},
	{"PGAR", NULL, "INPUT2"},

	{"ADCL", NULL, "PGAL"},
	{"ADCR", NULL, "PGAR"},

	{"INPUTL MUX", "Normal", "ADCL"},
	{"INPUTL MUX", "ADC2 channel to ADC1 channel", "ADCR"},
	{"INPUTR MUX", "Normal", "ADCR"},
	{"INPUTR MUX", "ADC1 channel to ADC2 channel", "ADCL"},

	{"ADC Mixer", "DACL ADCL Mixer", "DACL"},
	{"ADC Mixer", "DACR ADCR Mixer", "DACR"},
	{"ADC Mixer", NULL, "INPUTL MUX"},
	{"ADC Mixer", NULL, "INPUTR MUX"},

	{"ADC MUX", "AMIC", "ADC Mixer"},
	{"ADC MUX", "DMIC", "DMIC"},

	{"I2S OUT", NULL, "ADC MUX"},

	{"DACL", NULL, "I2S IN"},
	{"DACR", NULL, "I2S IN"},

	{"IF DACL1", NULL, "DACL"},
	{"IF DACR1", NULL, "DACR"},
	{"IF DACL2", NULL, "DACL"},
	{"IF DACR2", NULL, "DACR"},
	{"IF DACL3", NULL, "DACL"},
	{"IF DACR3", NULL, "DACR"},

	{"IF DACL Mixer", NULL, "IF DACL2"},
	{"IF DACL Mixer", "DACR DACL Mixer", "IF DACR1"},
	{"IF DACR Mixer", NULL, "IF DACR2"},
	{"IF DACR Mixer", "DACL DACR Mixer", "IF DACL1"},

	{"IF ADCDACL Mixer", NULL, "IF DACL Mixer"},
	{"IF ADCDACL Mixer", "ADCL DACL Mixer", "IF DACL3"},
	{"IF ADCDACR Mixer", NULL, "IF DACR Mixer"},
	{"IF ADCDACR Mixer", "ADCR DACR Mixer", "IF DACR3"},

	{"OUTL MUX", "Normal", "IF ADCDACL Mixer"},
	{"OUTL MUX", "DAC2 channel to DAC1 channel", "IF ADCDACR Mixer"},
	{"OUTR MUX", "Normal", "IF ADCDACR Mixer"},
	{"OUTR MUX", "DAC1 channel to DAC2 channel", "IF ADCDACL Mixer"},

	{"HPOL", NULL, "OUTL MUX"},
	{"HPOR", NULL, "OUTR MUX"},

};

struct _coeff_div {
	u16 fs;
	u32 mclk;
	u32 rate;
	u8 Reg0x04;
	u8 Reg0x05;
	u8 Reg0x06;
	u8 Reg0x07;
	u8 Reg0x08;
	u8 Reg0x09;
	u8 Reg0x0A;
	u8 Reg0x0F;
	u8 Reg0x11;
	u8 Reg0x21;
	u8 Reg0x22;
	u8 Reg0x26;
	u8 Reg0x30;
	u8 Reg0x41;
	u8 Reg0x42;
	u8 Reg0x43;
	u8 Reg0xF0;
	u8 Reg0xF1;
	u8 Reg0x16;
	u8 Reg0x18;
	u8 Reg0x19;
	u8 dvdd_vol;
	u8 dmic_sel;
};

/* codec hifi mclk clock divider coefficients */
static const struct _coeff_div  coeff_div[] = {
	{32, 256000, 8000, 0x00, 0x57, 0x84, 0xD0, 0x03, 0xC1, 0xB0, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{36, 288000, 8000, 0x00, 0x55, 0x84, 0xD0, 0x01, 0xC1, 0x90, 0x00, 0x00, 0x23, 0x8F, 0xBF, 0xC0, 0x1F, 0x8F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{48, 384000, 8000, 0x02, 0x5F, 0x04, 0xC0, 0x03, 0xC1, 0xB0, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{50, 400000, 8000, 0x00, 0x75, 0x05, 0xC8, 0x01, 0xC1, 0x90, 0x00, 0x00, 0x18, 0xC7, 0xD0, 0xC0, 0x8F, 0xC7, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 0},
	{50, 400000, 8000, 0x00, 0x15, 0x85, 0xD0, 0x01, 0xC1, 0x90, 0x00, 0x00, 0x31, 0xC7, 0xC5, 0x00, 0x8F, 0xC7, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 1},
	{64, 512000, 8000, 0x00, 0x4D, 0x24, 0xC0, 0x03, 0xD1, 0xB0, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{72, 576000, 8000, 0x00, 0x45, 0x24, 0xC0, 0x01, 0xD1, 0x90, 0x00, 0x00, 0x23, 0x8F, 0xBF, 0xC0, 0x1F, 0x8F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{96, 768000, 8000, 0x02, 0x57, 0x84, 0xD0, 0x03, 0xC1, 0xB0, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{128, 1024000, 8000, 0x00, 0x45, 0x04, 0xD0, 0x03, 0xC1, 0xB0, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{192, 1536000, 8000, 0x02, 0x4D, 0x24, 0xC0, 0x03, 0xD1, 0xB0, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{256, 2048000, 8000, 0x01, 0x45, 0x04, 0xD0, 0x03, 0xC1, 0xB0, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{288, 2304000, 8000, 0x01, 0x51, 0x00, 0xC0, 0x01, 0xC1, 0x90, 0x00, 0x00, 0x23, 0x8F, 0xBF, 0xC0, 0x1F, 0x8F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{384, 3072000, 8000, 0x02, 0x45, 0x04, 0xD0, 0x03, 0xC1, 0xB0, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{512, 4096000, 8000, 0x00, 0x41, 0x04, 0xE0, 0x00, 0xD1, 0xB0, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{600, 4800000, 8000, 0x05, 0x65, 0x25, 0xF9, 0x00, 0xD1, 0x90, 0x00, 0x00, 0x18, 0xC7, 0xD0, 0xC0, 0x8F, 0xC7, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 0},
	{600, 4800000, 8000, 0x02, 0x24, 0x05, 0xD0, 0x00, 0xC2, 0x80, 0x00, 0x00, 0x31, 0xC7, 0xC5, 0x00, 0x8F, 0xC7, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 1},
	{768, 6144000, 8000, 0x05, 0x45, 0x04, 0xD0, 0x03, 0xC1, 0xB0, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{1024, 8192000, 8000, 0x01, 0x41, 0x06, 0xE0, 0x00, 0xD1, 0xB0, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{1500, 12000000, 8000, 0x0E, 0x25, 0x25, 0xE8, 0x00, 0xD1, 0x90, 0x40, 0x00, 0x31, 0xC7, 0xC5, 0x00, 0x8F, 0xC7, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{1536, 12288000, 8000, 0x02, 0x41, 0x04, 0xE0, 0x00, 0xD1, 0xB0, 0x40, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{1625, 13000000, 8000, 0x40, 0x6E, 0x05, 0xC8, 0x01, 0xC2, 0x90, 0x40, 0x00, 0x18, 0x95, 0xD0, 0xC0, 0x63, 0x95, 0x00, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{2048, 16384000, 8000, 0x03, 0x44, 0x01, 0xC0, 0x00, 0xD2, 0x80, 0x40, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{2304, 18432000, 8000, 0x11, 0x45, 0x25, 0xF0, 0x00, 0xD1, 0xB0, 0x40, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{2400, 19200000, 8000, 0x05, 0x14, 0x01, 0xC9, 0x00, 0xD2, 0x80, 0x80, 0x00, 0x31, 0xC7, 0xC5, 0x00, 0x8F, 0xC7, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 1},
	{2400, 19200000, 8000, 0x0B, 0x01, 0x00, 0xD0, 0x00, 0xD1, 0x80, 0x80, 0x00, 0x31, 0xC7, 0xC5, 0x00, 0xC7, 0xC7, 0x00, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 0},
	{3000, 24000000, 8000, 0x0E, 0x24, 0x05, 0xD0, 0x00, 0xC2, 0x80, 0xC0, 0x00, 0x31, 0xC7, 0xC5, 0x00, 0x8F, 0xC7, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{3072, 24576000, 8000, 0x05, 0x44, 0x01, 0xC0, 0x00, 0xD2, 0x80, 0x40, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 2},
	{3250, 26000000, 8000, 0x40, 0x15, 0x85, 0xD0, 0x01, 0xC1, 0x90, 0xC0, 0x00, 0x31, 0xC7, 0xC5, 0x00, 0x8F, 0xC7, 0x01, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 1},
	{3250, 26000000, 8000, 0x40, 0x05, 0xA4, 0xC0, 0x00, 0xD1, 0x80, 0xC0, 0x00, 0x31, 0xC7, 0xC5, 0x00, 0xC7, 0xC7, 0x00, 0x12, 0x00, 0x09, 0x19, 0x07, 2, 0},
	{32, 512000, 16000, 0x00, 0x55, 0x84, 0xD0, 0x01, 0xC1, 0x90, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{36, 576000, 16000, 0x00, 0x55, 0x84, 0xD0, 0x01, 0xC1, 0x90, 0x00, 0x00, 0x23, 0x8F, 0xBF, 0xC0, 0x1F, 0x8F, 0x01, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{48, 768000, 16000, 0x02, 0x57, 0x04, 0xC0, 0x01, 0xC1, 0x90, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{50, 800000, 16000, 0x00, 0x7E, 0x01, 0xD9, 0x00, 0xC2, 0x80, 0x00, 0x00, 0x18, 0x95, 0xD0, 0xC0, 0xC7, 0x95, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{64, 1024000, 16000, 0x00, 0x45, 0x24, 0xC0, 0x01, 0xC1, 0x90, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{72, 1152000, 16000, 0x00, 0x45, 0x24, 0xC0, 0x01, 0xC1, 0x90, 0x00, 0x00, 0x23, 0x8F, 0xBF, 0xC0, 0x1F, 0x8F, 0x01, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{96, 1536000, 16000, 0x02, 0x55, 0x84, 0xD0, 0x01, 0xC1, 0x90, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{128, 2048000, 16000, 0x00, 0x51, 0x04, 0xD0, 0x01, 0xC1, 0x90, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{144, 2304000, 16000, 0x00, 0x51, 0x00, 0xC0, 0x01, 0xC1, 0x90, 0x00, 0x00, 0x23, 0x8F, 0xBF, 0xC0, 0x1F, 0x8F, 0x01, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{150, 2400000, 16000, 0x02, 0x7E, 0x01, 0xC9, 0x00, 0xC2, 0x80, 0x40, 0x00, 0x18, 0x95, 0xD0, 0xC0, 0xC7, 0x95, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{192, 3072000, 16000, 0x02, 0x65, 0x25, 0xE0, 0x00, 0xE1, 0x90, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{256, 4096000, 16000, 0x00, 0x41, 0x04, 0xC0, 0x01, 0xC1, 0x90, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{300, 4800000, 16000, 0x02, 0x66, 0x01, 0xD9, 0x00, 0xC2, 0x80, 0x00, 0x00, 0x18, 0x95, 0xD0, 0xC0, 0xC7, 0x95, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{384, 6144000, 16000, 0x02, 0x51, 0x04, 0xD0, 0x01, 0xC1, 0x90, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{512, 8192000, 16000, 0x01, 0x41, 0x04, 0xC0, 0x01, 0xC1, 0x90, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{750, 12000000, 16000, 0x0E, 0x7E, 0x01, 0xC9, 0x00, 0xC2, 0x80, 0x40, 0x00, 0x18, 0x95, 0xD0, 0xC0, 0xC7, 0x95, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{768, 12288000, 16000, 0x02, 0x41, 0x04, 0xC0, 0x01, 0xC1, 0x90, 0x40, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{1024, 16384000, 16000, 0x03, 0x41, 0x04, 0xC0, 0x01, 0xC1, 0x90, 0x40, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{1152, 18432000, 16000, 0x08, 0x51, 0x04, 0xD0, 0x01, 0xC1, 0x90, 0x40, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{1200, 19200000, 16000, 0x0B, 0x66, 0x01, 0xD9, 0x00, 0xC2, 0x80, 0x40, 0x00, 0x18, 0x95, 0xD0, 0xC0, 0xC7, 0x95, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{1500, 24000000, 16000, 0x0E, 0x26, 0x01, 0xD9, 0x00, 0xC2, 0x80, 0xC0, 0x00, 0x18, 0x95, 0xD0, 0xC0, 0xC7, 0x95, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{1536, 24576000, 16000, 0x05, 0x41, 0x04, 0xC0, 0x01, 0xC1, 0x90, 0xC0, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0xFF, 0x7F, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{1625, 26000000, 16000, 0x40, 0x6E, 0x05, 0xC8, 0x01, 0xC2, 0x90, 0xC0, 0x00, 0x18, 0x95, 0xD0, 0xC0, 0x63, 0x95, 0x00, 0x12, 0x00, 0x12, 0x31, 0x0E, 2, 2},
	{800, 19200000, 24000, 0x07, 0x66, 0x01, 0xD9, 0x00, 0xC2, 0x80, 0x40, 0x00, 0x18, 0x95, 0xD0, 0xC0, 0xC7, 0x95, 0x00, 0x12, 0x00, 0x1A, 0x49, 0x14, 2, 2},
	{375, 12000000, 32000, 0x0E, 0x2E, 0x05, 0xC8, 0x00, 0xC2, 0x80, 0x40, 0x01, 0x18, 0x95, 0xD0, 0xC0, 0x63, 0x95, 0x00, 0x12, 0x00, 0x23, 0x61, 0x1B, 2, 0},
	{600, 19200000, 32000, 0x05, 0x46, 0x01, 0xD8, 0x10, 0xC2, 0x80, 0x40, 0x00, 0x18, 0x95, 0xD0, 0xC0, 0x63, 0x95, 0x00, 0x12, 0x00, 0x23, 0x61, 0x1B, 2, 2},
	{32, 1411200, 44100, 0x00, 0x45, 0xA4, 0xD0, 0x10, 0xC1, 0x80, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{64, 2822400, 44100, 0x00, 0x51, 0x00, 0xC0, 0x10, 0xC1, 0x80, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{128, 5644800, 44100, 0x00, 0x41, 0x04, 0xD0, 0x10, 0xC1, 0x80, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{256, 11289600, 44100, 0x01, 0x41, 0x04, 0xD0, 0x10, 0xC1, 0x80, 0x40, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{512, 22579200, 44100, 0x03, 0x41, 0x04, 0xD0, 0x10, 0xC1, 0x80, 0xC0, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{32, 1536000, 48000, 0x00, 0x45, 0xA4, 0xD0, 0x10, 0xC1, 0x80, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{48, 2304000, 48000, 0x02, 0x55, 0x04, 0xC0, 0x10, 0xC1, 0x80, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{50, 2400000, 48000, 0x00, 0x76, 0x01, 0xC8, 0x10, 0xC2, 0x80, 0x00, 0x00, 0x18, 0x95, 0xD0, 0xC0, 0x63, 0x95, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{64, 3072000, 48000, 0x00, 0x51, 0x04, 0xC0, 0x10, 0xC1, 0x80, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{100, 4800000, 48000, 0x00, 0x46, 0x01, 0xD8, 0x10, 0xC2, 0x80, 0x00, 0x00, 0x18, 0x95, 0xD0, 0xC0, 0x63, 0x95, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{125, 6000000, 48000, 0x04, 0x6E, 0x05, 0xC8, 0x10, 0xC2, 0x80, 0x00, 0x01, 0x18, 0x95, 0xD0, 0xC0, 0x63, 0x95, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{128, 6144000, 48000, 0x00, 0x41, 0x04, 0xD0, 0x10, 0xC1, 0x80, 0x00, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{200, 9600000, 48000, 0x01, 0x46, 0x01, 0xD8, 0x10, 0xC2, 0x80, 0x00, 0x00, 0x18, 0x95, 0xD0, 0xC0, 0x63, 0x95, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{250, 12000000, 48000, 0x04, 0x76, 0x01, 0xC8, 0x10, 0xC2, 0x80, 0x40, 0x00, 0x18, 0x95, 0xD0, 0xC0, 0x63, 0x95, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{256, 12288000, 48000, 0x01, 0x41, 0x04, 0xD0, 0x10, 0xC1, 0x80, 0x40, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{384, 18432000, 48000, 0x02, 0x41, 0x04, 0xD0, 0x10, 0xC1, 0x80, 0x40, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{400, 19200000, 48000, 0x03, 0x46, 0x01, 0xD8, 0x10, 0xC2, 0x80, 0x40, 0x00, 0x18, 0x95, 0xD0, 0xC0, 0x63, 0x95, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{500, 24000000, 48000, 0x04, 0x46, 0x01, 0xD8, 0x10, 0xC2, 0x80, 0xC0, 0x00, 0x18, 0x95, 0xD0, 0xC0, 0x63, 0x95, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{512, 24576000, 48000, 0x03, 0x41, 0x04, 0xD0, 0x10, 0xC1, 0x80, 0xC0, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{800, 38400000, 48000, 0x18, 0x45, 0x04, 0xC0, 0x10, 0xC1, 0x80, 0xC0, 0x00, 0x1F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x00, 0x12, 0x00, 0x35, 0x91, 0x28, 2, 2},
	{128, 11289600, 88200, 0x00, 0x50, 0x00, 0xC0, 0x10, 0xC1, 0x80, 0x40, 0x00, 0x9F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x80, 0x12, 0xC0, 0x32, 0x89, 0x25, 2, 2},
	{64, 6144000, 96000, 0x00, 0x41, 0x00, 0xD0, 0x10, 0xC1, 0x80, 0x00, 0x00, 0x9F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x80, 0x12, 0xC0, 0x35, 0x91, 0x28, 2, 2},
	{96, 9216000, 96000, 0x02, 0x43, 0x00, 0xC0, 0x10, 0xC0, 0x80, 0x00, 0x00, 0x9F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x80, 0x12, 0xC0, 0x35, 0x91, 0x28, 2, 2},
	{256, 24576000, 96000, 0x00, 0x40, 0x00, 0xC0, 0x10, 0xC1, 0x80, 0xC0, 0x00, 0x9F, 0x7F, 0xBF, 0xC0, 0x7F, 0x7F, 0x80, 0x12, 0xC0, 0x35, 0x91, 0x28, 2, 2},
	{128, 24576000, 192000, 0x00, 0x50, 0x00, 0xC0, 0x18, 0xC1, 0x81, 0xC0, 0x00, 0x8F, 0x7F, 0xBF, 0xC0, 0x3F, 0x7F, 0x80, 0x12, 0xC0, 0x3F, 0xF9, 0x3F, 2, 2},
	{64, 12288000, 192000, 0x00, 0x41, 0x00, 0xC0, 0x18, 0xC1, 0x80, 0x00, 0x00, 0x8F, 0x7F, 0xEF, 0xC0, 0x7F, 0x7F, 0x80, 0x12, 0xC0, 0x3F, 0xF9, 0x3F, 1, 0},
};

static inline int get_coeff(u8 vddd, u8 dmic, int mclk, int rate)
{
	int i;
	u8 dmic_det, vddd_det;

	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) {
			vddd_det = ~(coeff_div[i].dvdd_vol ^ vddd) & 0x01;
			dmic_det = ~(coeff_div[i].dmic_sel ^ dmic) & 0x01;
			vddd_det |= ~(coeff_div[i].dvdd_vol % 2) & 0x01;
			dmic_det |= ~(coeff_div[i].dmic_sel % 2) & 0x01;

			if (vddd_det && dmic_det)
				return i;
		}
	}

	return -EINVAL;
}

/*
 * if PLL not be used, use internal clk1 for mclk,otherwise, use internal clk2 for PLL source.
 */
static int es8389_set_dai_sysclk(struct snd_soc_dai *dai,
			int clk_id, unsigned int freq, int dir)
{
	struct snd_soc_component *component = dai->component;
	struct es8389_private *es8389 = snd_soc_component_get_drvdata(component);

	es8389->sysclk = freq;

	return 0;
}

static int es8389_set_tdm_slot(struct snd_soc_dai *dai,
	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
{
	struct snd_soc_component *component = dai->component;
	struct es8389_private *es8389 = snd_soc_component_get_drvdata(component);

	regmap_update_bits(es8389->regmap, ES8389_PTDM_SLOT,
				ES8389_TDM_SLOT, (slots << ES8389_TDM_SHIFT));
	regmap_update_bits(es8389->regmap, ES8389_DAC_RAMP,
				ES8389_TDM_SLOT, (slots << ES8389_TDM_SHIFT));

	return 0;
}

static int es8389_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
{
	struct snd_soc_component *component = dai->component;
	struct es8389_private *es8389 = snd_soc_component_get_drvdata(component);
	u8 state = 0;

	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
	case SND_SOC_DAIFMT_CBC_CFP:
		regmap_update_bits(es8389->regmap, ES8389_MASTER_MODE,
				ES8389_MASTER_MODE_EN, ES8389_MASTER_MODE_EN);
		es8389->mastermode = 1;
		break;
	case SND_SOC_DAIFMT_CBC_CFC:
		es8389->mastermode = 0;
		break;
	default:
		return -EINVAL;
	}

	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_I2S:
		state |= ES8389_DAIFMT_I2S;
		break;
	case SND_SOC_DAIFMT_RIGHT_J:
		dev_err(component->dev, "component driver does not support right justified\n");
		return -EINVAL;
	case SND_SOC_DAIFMT_LEFT_J:
		state |= ES8389_DAIFMT_LEFT_J;
		break;
	case SND_SOC_DAIFMT_DSP_A:
		state |= ES8389_DAIFMT_DSP_A;
		break;
	case SND_SOC_DAIFMT_DSP_B:
		state |= ES8389_DAIFMT_DSP_B;
		break;
	default:
		break;
	}
	regmap_update_bits(es8389->regmap, ES8389_ADC_FORMAT_MUTE, ES8389_DAIFMT_MASK, state);
	regmap_update_bits(es8389->regmap, ES8389_DAC_FORMAT_MUTE, ES8389_DAIFMT_MASK, state);

	return 0;
}

static int es8389_pcm_hw_params(struct snd_pcm_substream *substream,
			struct snd_pcm_hw_params *params,
			struct snd_soc_dai *dai)
{
	struct snd_soc_component *component = dai->component;
	struct es8389_private *es8389 = snd_soc_component_get_drvdata(component);
	int coeff, ret;
	u8 dmic_enable, state = 0;
	unsigned int regv;
	u32 freq;
	bool hpf;

	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
		state |= ES8389_S16_LE;
		break;
	case SNDRV_PCM_FORMAT_S20_3LE:
		state |= ES8389_S20_3_LE;
		break;
	case SNDRV_PCM_FORMAT_S18_3LE:
		state |= ES8389_S18_LE;
		break;
	case SNDRV_PCM_FORMAT_S24_LE:
		state |= ES8389_S24_LE;
		break;
	case SNDRV_PCM_FORMAT_S32_LE:
		state |= ES8389_S32_LE;
		break;
	default:
		return -EINVAL;
	}

	regmap_update_bits(es8389->regmap, ES8389_ADC_FORMAT_MUTE, ES8389_DATA_LEN_MASK, state);
	regmap_update_bits(es8389->regmap, ES8389_DAC_FORMAT_MUTE, ES8389_DATA_LEN_MASK, state);

	if (es8389->mclk_src == ES8389_SCLK_PIN) {
		regmap_update_bits(es8389->regmap, ES8389_MASTER_CLK,
					ES8389_MCLK_MASK, ES8389_MCLK_FROM_SCLK);
		es8389->sysclk = params_channels(params) * params_width(params) * params_rate(params);
	}

	regmap_read(es8389->regmap, ES8389_DMIC_EN, &regv);
	dmic_enable = regv >> 7 & 0x01;

	ret = regulator_get_voltage(es8389->core_supply[ES8389_SUPPLY_VD].consumer);
	switch (ret) {
	case 1800000 ... 2000000:
		es8389->vddd = ES8389_1V8;
		break;
	case 2500000 ... 3300000:
		es8389->vddd = ES8389_3V3;
		break;
	default:
		es8389->vddd = ES8389_3V3;
		break;
	}

	coeff = get_coeff(es8389->vddd, dmic_enable, es8389->sysclk, params_rate(params));
	if (coeff >= 0) {
		regmap_write(es8389->regmap, ES8389_CLK_DIV1, coeff_div[coeff].Reg0x04);
		regmap_write(es8389->regmap, ES8389_CLK_MUL, coeff_div[coeff].Reg0x05);
		regmap_write(es8389->regmap, ES8389_CLK_MUX1, coeff_div[coeff].Reg0x06);
		regmap_write(es8389->regmap, ES8389_CLK_MUX2, coeff_div[coeff].Reg0x07);
		regmap_write(es8389->regmap, ES8389_CLK_CTL1, coeff_div[coeff].Reg0x08);
		regmap_write(es8389->regmap, ES8389_CLK_CTL2, coeff_div[coeff].Reg0x09);
		regmap_write(es8389->regmap, ES8389_CLK_CTL3, coeff_div[coeff].Reg0x0A);
		regmap_update_bits(es8389->regmap, ES8389_OSC_CLK,
						0xC0, coeff_div[coeff].Reg0x0F);
		regmap_write(es8389->regmap, ES8389_CLK_DIV2, coeff_div[coeff].Reg0x11);
		regmap_write(es8389->regmap, ES8389_ADC_OSR, coeff_div[coeff].Reg0x21);
		regmap_write(es8389->regmap, ES8389_ADC_DSP, coeff_div[coeff].Reg0x22);
		regmap_write(es8389->regmap, ES8389_OSR_VOL, coeff_div[coeff].Reg0x26);
		regmap_update_bits(es8389->regmap, ES8389_SYSTEM30,
						0xC0, coeff_div[coeff].Reg0x30);
		regmap_write(es8389->regmap, ES8389_DAC_DSM_OSR, coeff_div[coeff].Reg0x41);
		regmap_write(es8389->regmap, ES8389_DAC_DSP_OSR, coeff_div[coeff].Reg0x42);
		regmap_update_bits(es8389->regmap, ES8389_DAC_MISC,
						0x81, coeff_div[coeff].Reg0x43);
		regmap_update_bits(es8389->regmap, ES8389_CHIP_MISC,
						0x72, coeff_div[coeff].Reg0xF0);
		regmap_write(es8389->regmap, ES8389_CSM_STATE1, coeff_div[coeff].Reg0xF1);
		regmap_write(es8389->regmap, ES8389_SYSTEM16, coeff_div[coeff].Reg0x16);
		regmap_write(es8389->regmap, ES8389_SYSTEM18, coeff_div[coeff].Reg0x18);
		regmap_write(es8389->regmap, ES8389_SYSTEM19, coeff_div[coeff].Reg0x19);
	} else {
		dev_warn(component->dev, "Clock coefficients do not match");
	}

	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
		es8389->capture_rate = params_rate(params);
		freq = (es8389->hpf_freq * 48000) / params_rate(params);
		hpf = find_best_hpf_freq(freq, &es8389->hpfl, &es8389->hpfr, &es8389->hpf_freq);
		if (!hpf) {
			dev_err(component->dev, "The HPF frequency is invalid\n");
			return -EINVAL;
		}
	}

	return 0;
}

static int es8389_pcm_hw_free(struct snd_pcm_substream *substream,
		struct snd_soc_dai *dai)
{
	struct snd_soc_component *component = dai->component;
	struct es8389_private *es8389 = snd_soc_component_get_drvdata(component);

	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
		es8389->capture_rate = 0;

	return 0;
}

static int es8389_set_bias_level(struct snd_soc_component *component,
			enum snd_soc_bias_level level)
{
	int ret;
	struct es8389_private *es8389 = snd_soc_component_get_drvdata(component);

	switch (level) {
	case SND_SOC_BIAS_ON:
		ret = clk_prepare_enable(es8389->mclk);
		if (ret)
			return ret;

		regmap_update_bits(es8389->regmap, ES8389_HPSW, 0x20, 0x20);
		regmap_write(es8389->regmap, ES8389_ANA_CTL1, 0xD9);
		regmap_write(es8389->regmap, ES8389_ADC_EN, 0x8F);
		regmap_write(es8389->regmap, ES8389_CSM_JUMP, 0xE4);
		regmap_write(es8389->regmap, ES8389_RESET, 0x01);
		regmap_write(es8389->regmap, ES8389_CLK_OFF1, 0xC3);
		break;
	case SND_SOC_BIAS_PREPARE:
		break;
	case SND_SOC_BIAS_STANDBY:
		regmap_update_bits(es8389->regmap, ES8389_ADC_HPF1, 0x0f, 0x04);
		regmap_update_bits(es8389->regmap, ES8389_ADC_HPF2, 0x0f, 0x04);
		regmap_write(es8389->regmap, ES8389_CSM_JUMP, 0xD4);
		usleep_range(70000, 72000);
		regmap_write(es8389->regmap, ES8389_ANA_CTL1, 0x59);
		regmap_write(es8389->regmap, ES8389_ADC_EN, 0x00);
		regmap_write(es8389->regmap, ES8389_CLK_OFF1, 0x00);
		regmap_write(es8389->regmap, ES8389_RESET, 0x3E);
		regmap_update_bits(es8389->regmap, ES8389_DAC_INV, 0x80, 0x80);
		usleep_range(8000, 8500);
		regmap_update_bits(es8389->regmap, ES8389_DAC_INV, 0x80, 0x00);

		clk_disable_unprepare(es8389->mclk);
		break;
	case SND_SOC_BIAS_OFF:
		break;
	}
	return 0;
}



static int es8389_mute(struct snd_soc_dai *dai, int mute, int direction)
{
	struct snd_soc_component *component = dai->component;
	struct es8389_private *es8389 = snd_soc_component_get_drvdata(component);
	unsigned int regv;

	if (mute) {
		if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
			regmap_update_bits(es8389->regmap, ES8389_DAC_FORMAT_MUTE,
						0x03, 0x03);
		} else {
			regmap_update_bits(es8389->regmap, ES8389_ADC_FORMAT_MUTE,
						0x03, 0x03);
		}
	} else {
		regmap_read(es8389->regmap, ES8389_CSM_STATE1, &regv);
		if (regv != ES8389_STATE_ON) {
			regmap_update_bits(es8389->regmap, ES8389_HPSW, 0x20, 0x20);
			regmap_write(es8389->regmap, ES8389_ANA_CTL1, 0xD9);
			regmap_write(es8389->regmap, ES8389_ADC_EN, 0x8F);
			regmap_write(es8389->regmap, ES8389_CSM_JUMP, 0xE4);
			regmap_write(es8389->regmap, ES8389_RESET, 0x01);
			regmap_write(es8389->regmap, ES8389_CLK_OFF1, 0xC3);
		}

		if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
			if (!es8389->version) {
				regmap_write(es8389->regmap, ES8389_DAC_RESET, 0X00);
				usleep_range(70000, 72000);
			}
			regmap_update_bits(es8389->regmap, ES8389_DAC_FORMAT_MUTE,
						0x03, 0x00);
		} else {
			regmap_update_bits(es8389->regmap, ES8389_ADC_HPF1, 0x0f, es8389->hpfl);
			regmap_update_bits(es8389->regmap, ES8389_ADC_HPF2, 0x0f, es8389->hpfr);
			regmap_update_bits(es8389->regmap, ES8389_ADC_FORMAT_MUTE,
						0x03, 0x00);
		}
	}

	return 0;
}

#define es8389_RATES SNDRV_PCM_RATE_8000_96000

#define es8389_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)

static const struct snd_soc_dai_ops es8389_ops = {
	.hw_params = es8389_pcm_hw_params,
	.hw_free = es8389_pcm_hw_free,
	.set_fmt = es8389_set_dai_fmt,
	.set_sysclk = es8389_set_dai_sysclk,
	.set_tdm_slot = es8389_set_tdm_slot,
	.mute_stream = es8389_mute,
};

static struct snd_soc_dai_driver es8389_dai = {
	.name = "ES8389 HiFi",
	.playback = {
		.stream_name = "Playback",
		.channels_min = 1,
		.channels_max = 2,
		.rates = es8389_RATES,
		.formats = es8389_FORMATS,
	},
	.capture = {
		.stream_name = "Capture",
		.channels_min = 1,
		.channels_max = 2,
		.rates = es8389_RATES,
		.formats = es8389_FORMATS,
	},
	.ops = &es8389_ops,
	.symmetric_rate = 1,
};

static void es8389_init(struct snd_soc_component *component)
{
	struct es8389_private *es8389 = snd_soc_component_get_drvdata(component);
	unsigned int reg;

	regmap_read(es8389->regmap, ES8389_MAX_REGISTER, &reg);
	es8389->version = reg;
	regmap_write(es8389->regmap, ES8389_ISO_CTL, 0x56);
	regmap_write(es8389->regmap, ES8389_RESET, 0x7E);
	regmap_write(es8389->regmap, ES8389_ISO_CTL, 0x38);
	regmap_write(es8389->regmap, ES8389_ADC_HPF1, 0x64);
	regmap_write(es8389->regmap, ES8389_ADC_HPF2, 0x04);
	regmap_write(es8389->regmap, ES8389_DAC_INV, 0x03);

	regmap_write(es8389->regmap, ES8389_VMID, 0x2A);
	regmap_write(es8389->regmap, ES8389_ANA_CTL1, 0xC9);
	regmap_write(es8389->regmap, ES8389_ANA_VSEL, 0x4F);
	regmap_write(es8389->regmap, ES8389_ANA_CTL2, 0x06);
	regmap_write(es8389->regmap, ES8389_LOW_POWER1, 0x00);
	regmap_write(es8389->regmap, ES8389_DMIC_EN, 0x16);

	regmap_write(es8389->regmap, ES8389_PGA_SW, 0xAA);
	regmap_write(es8389->regmap, ES8389_MOD_SW1, 0x66);
	regmap_write(es8389->regmap, ES8389_MOD_SW2, 0x99);
	regmap_write(es8389->regmap, ES8389_ADC_MODE, (0x00 | ES8389_TDM_MODE));
	regmap_update_bits(es8389->regmap, ES8389_DMIC_EN, 0xC0, 0x00);
	regmap_update_bits(es8389->regmap, ES8389_ADC_MODE, 0x03, 0x00);

	regmap_update_bits(es8389->regmap, ES8389_MIC1_GAIN,
					ES8389_MIC_SEL_MASK, ES8389_MIC_DEFAULT);
	regmap_update_bits(es8389->regmap, ES8389_MIC2_GAIN,
					ES8389_MIC_SEL_MASK, ES8389_MIC_DEFAULT);
	regmap_write(es8389->regmap, ES8389_CSM_JUMP, 0xC4);
	regmap_write(es8389->regmap, ES8389_MASTER_MODE, 0x08);
	regmap_write(es8389->regmap, ES8389_CSM_STATE1, 0x00);
	regmap_write(es8389->regmap, ES8389_SYSTEM12, 0x01);
	regmap_write(es8389->regmap, ES8389_SYSTEM13, 0x01);
	regmap_write(es8389->regmap, ES8389_SYSTEM14, 0x01);
	regmap_write(es8389->regmap, ES8389_SYSTEM15, 0x01);
	regmap_write(es8389->regmap, ES8389_SYSTEM16, 0x35);
	regmap_write(es8389->regmap, ES8389_SYSTEM17, 0x09);
	regmap_write(es8389->regmap, ES8389_SYSTEM18, 0x91);
	regmap_write(es8389->regmap, ES8389_SYSTEM19, 0x28);
	regmap_write(es8389->regmap, ES8389_SYSTEM1A, 0x01);
	regmap_write(es8389->regmap, ES8389_SYSTEM1B, 0x01);
	regmap_write(es8389->regmap, ES8389_SYSTEM1C, 0x11);

	regmap_write(es8389->regmap, ES8389_CHIP_MISC, 0x13);
	regmap_write(es8389->regmap, ES8389_MASTER_CLK, 0x00);
	regmap_write(es8389->regmap, ES8389_CLK_DIV1, 0x00);
	regmap_write(es8389->regmap, ES8389_CLK_MUL, 0x10);
	regmap_write(es8389->regmap, ES8389_CLK_MUX1, 0x00);
	regmap_write(es8389->regmap, ES8389_CLK_MUX2, 0xC0);
	regmap_write(es8389->regmap, ES8389_CLK_CTL1, 0x00);
	regmap_write(es8389->regmap, ES8389_CLK_CTL2, 0xC0);
	regmap_write(es8389->regmap, ES8389_CLK_CTL3, 0x80);
	regmap_write(es8389->regmap, ES8389_SCLK_DIV, 0x04);
	regmap_write(es8389->regmap, ES8389_LRCK_DIV1, 0x01);
	regmap_write(es8389->regmap, ES8389_LRCK_DIV2, 0x00);
	regmap_write(es8389->regmap, ES8389_OSC_CLK, 0x10);
	regmap_write(es8389->regmap, ES8389_ADC_OSR, 0x1F);
	regmap_write(es8389->regmap, ES8389_ADC_DSP, 0x7F);
	regmap_write(es8389->regmap, ES8389_ADC_MUTE, 0xC0);
	regmap_write(es8389->regmap, ES8389_SYSTEM30, 0xF4);
	regmap_write(es8389->regmap, ES8389_DAC_DSM_OSR, 0x7F);
	regmap_write(es8389->regmap, ES8389_DAC_DSP_OSR, 0x7F);
	regmap_write(es8389->regmap, ES8389_DAC_MISC, 0x10);
	regmap_write(es8389->regmap, ES8389_DAC_RAMP, 0x0F);
	regmap_write(es8389->regmap, ES8389_SYSTEM4C, 0xC0);
	regmap_write(es8389->regmap, ES8389_RESET, 0x00);
	regmap_write(es8389->regmap, ES8389_CLK_OFF1, 0xC1);
	regmap_write(es8389->regmap, ES8389_RESET, 0x01);
	regmap_write(es8389->regmap, ES8389_DAC_RESET, 0x02);

	regmap_update_bits(es8389->regmap, ES8389_ADC_FORMAT_MUTE, 0x03, 0x03);
	regmap_update_bits(es8389->regmap, ES8389_DAC_FORMAT_MUTE, 0x03, 0x03);
}

static int es8389_suspend(struct snd_soc_component *component)
{
	struct es8389_private *es8389 = snd_soc_component_get_drvdata(component);

	es8389_set_bias_level(component, SND_SOC_BIAS_STANDBY);
	regcache_cache_only(es8389->regmap, true);
	regcache_mark_dirty(es8389->regmap);

	return 0;
}

static int es8389_resume(struct snd_soc_component *component)
{
	struct es8389_private *es8389 = snd_soc_component_get_drvdata(component);
	unsigned int regv;

	regcache_cache_only(es8389->regmap, false);
	regcache_cache_bypass(es8389->regmap, true);
	regmap_read(es8389->regmap, ES8389_RESET, &regv);

	if (regv == 0xff)
		es8389_init(component);
	else
		es8389_set_bias_level(component, SND_SOC_BIAS_ON);

	regcache_cache_bypass(es8389->regmap, false);
	regcache_sync(es8389->regmap);

	return 0;
}

static int es8389_probe(struct snd_soc_component *component)
{
	int ret, i;
	struct es8389_private *es8389 = snd_soc_component_get_drvdata(component);

	ret = device_property_read_u8(component->dev, "everest,mclk-src", &es8389->mclk_src);
	if (ret != 0) {
		dev_dbg(component->dev, "mclk-src return %d", ret);
		es8389->mclk_src = ES8389_MCLK_SOURCE;
	}

	for (i = 0; i < ARRAY_SIZE(es8389_core_supplies); i++)
		es8389->core_supply[i].supply = es8389_core_supplies[i];
	ret = devm_regulator_bulk_get(component->dev, ARRAY_SIZE(es8389_core_supplies), es8389->core_supply);
	if (ret) {
		dev_err(component->dev, "Failed to request core supplies %d\n", ret);
		return ret;
	}

	es8389->mclk = devm_clk_get_optional(component->dev, "mclk");
	if (IS_ERR(es8389->mclk))
		return dev_err_probe(component->dev, PTR_ERR(es8389->mclk),
			"ES8389 is unable to get mclk\n");

	if (!es8389->mclk)
		dev_err(component->dev, "%s, assuming static mclk\n", __func__);

	ret = clk_prepare_enable(es8389->mclk);
	if (ret) {
		dev_err(component->dev, "%s, unable to enable mclk\n", __func__);
		return ret;
	}

	ret = regulator_bulk_enable(ARRAY_SIZE(es8389_core_supplies), es8389->core_supply);
	if (ret) {
		dev_err(component->dev, "Failed to enable core supplies: %d\n", ret);
		clk_disable_unprepare(es8389->mclk);
		return ret;
	}

	es8389->hpf_freq = ES8389_HPF_DEFAULT;
	es8389_init(component);
	es8389_set_bias_level(component, SND_SOC_BIAS_STANDBY);

	return 0;
}

static void es8389_remove(struct snd_soc_component *component)
{
	struct es8389_private *es8389 = snd_soc_component_get_drvdata(component);

	regmap_write(es8389->regmap, ES8389_MASTER_MODE, 0x28);
	regmap_write(es8389->regmap, ES8389_HPSW, 0x00);
	regmap_write(es8389->regmap, ES8389_VMID, 0x00);
	regmap_write(es8389->regmap, ES8389_RESET, 0x00);
	regmap_write(es8389->regmap, ES8389_CSM_JUMP, 0xCC);
	usleep_range(500000, 550000);//500MS
	regmap_write(es8389->regmap, ES8389_CSM_JUMP, 0x00);
	regmap_write(es8389->regmap, ES8389_ANA_CTL1, 0x08);
	regmap_write(es8389->regmap, ES8389_ISO_CTL, 0xC1);
	regmap_write(es8389->regmap, ES8389_PULL_DOWN, 0x00);

}

static const struct snd_soc_component_driver soc_codec_dev_es8389 = {
	.probe = es8389_probe,
	.remove = es8389_remove,
	.suspend = es8389_suspend,
	.resume = es8389_resume,
	.set_bias_level = es8389_set_bias_level,

	.controls = es8389_snd_controls,
	.num_controls = ARRAY_SIZE(es8389_snd_controls),
	.dapm_widgets = es8389_dapm_widgets,
	.num_dapm_widgets = ARRAY_SIZE(es8389_dapm_widgets),
	.dapm_routes = es8389_dapm_routes,
	.num_dapm_routes = ARRAY_SIZE(es8389_dapm_routes),
	.idle_bias_on = 1,
	.use_pmdown_time = 1,
};

static const struct regmap_config es8389_regmap = {
	.reg_bits = 8,
	.val_bits = 8,

	.max_register = ES8389_MAX_REGISTER,

	.volatile_reg = es8389_volatile_register,
	.cache_type = REGCACHE_MAPLE,
};

static void es8389_i2c_shutdown(struct i2c_client *i2c)
{
	struct es8389_private *es8389;

	es8389 = i2c_get_clientdata(i2c);

	regmap_write(es8389->regmap, ES8389_MASTER_MODE, 0x28);
	regmap_write(es8389->regmap, ES8389_HPSW, 0x00);
	regmap_write(es8389->regmap, ES8389_VMID, 0x00);
	regmap_write(es8389->regmap, ES8389_RESET, 0x00);
	regmap_write(es8389->regmap, ES8389_CSM_JUMP, 0xCC);
	usleep_range(500000, 550000);//500MS
	regmap_write(es8389->regmap, ES8389_CSM_JUMP, 0x00);
	regmap_write(es8389->regmap, ES8389_ANA_CTL1, 0x08);
	regmap_write(es8389->regmap, ES8389_ISO_CTL, 0xC1);
	regmap_write(es8389->regmap, ES8389_PULL_DOWN, 0x00);

	regulator_bulk_disable(ARRAY_SIZE(es8389_core_supplies), es8389->core_supply);
}

static int es8389_i2c_probe(struct i2c_client *i2c_client)
{
	struct es8389_private *es8389;
	int ret;

	es8389 = devm_kzalloc(&i2c_client->dev, sizeof(*es8389), GFP_KERNEL);
	if (es8389 == NULL)
		return -ENOMEM;

	i2c_set_clientdata(i2c_client, es8389);
	es8389->regmap = devm_regmap_init_i2c(i2c_client, &es8389_regmap);
	if (IS_ERR(es8389->regmap))
		return dev_err_probe(&i2c_client->dev, PTR_ERR(es8389->regmap),
			"regmap_init() failed\n");

	ret =  devm_snd_soc_register_component(&i2c_client->dev,
			&soc_codec_dev_es8389,
			&es8389_dai,
			1);

	return ret;
}

#ifdef CONFIG_OF
static const struct of_device_id es8389_if_dt_ids[] = {
	{ .compatible = "everest,es8389", },
	{ }
};
MODULE_DEVICE_TABLE(of, es8389_if_dt_ids);
#endif

static const struct i2c_device_id es8389_i2c_id[] = {
	{ .name = "es8389" },
	{ }
};
MODULE_DEVICE_TABLE(i2c, es8389_i2c_id);

static struct i2c_driver es8389_i2c_driver = {
	.driver = {
		.name	= "es8389",
		.of_match_table = of_match_ptr(es8389_if_dt_ids),
	},
	.shutdown = es8389_i2c_shutdown,
	.probe = es8389_i2c_probe,
	.id_table = es8389_i2c_id,
};
module_i2c_driver(es8389_i2c_driver);

MODULE_DESCRIPTION("ASoC es8389 driver");
MODULE_AUTHOR("Michael Zhang <zhangyi@everest-semi.com>");
MODULE_LICENSE("GPL");