summaryrefslogtreecommitdiffstats
path: root/sound/pci/oxygen/xonar_wm87x6.c
blob: ad48356c54e42774c9622641d590b58cf37d3862 (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
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
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
/*
 * card driver for models with WM8776/WM8766 DACs (Xonar DS/HDAV1.3 Slim)
 *
 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
 *
 *
 *  This driver is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License, version 2.
 *
 *  This driver 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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this driver; if not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Xonar DS
 * --------
 *
 * CMI8788:
 *
 *   SPI 0 -> WM8766 (surround, center/LFE, back)
 *   SPI 1 -> WM8776 (front, input)
 *
 *   GPIO 4 <- headphone detect, 0 = plugged
 *   GPIO 6 -> route input jack to mic-in (0) or line-in (1)
 *   GPIO 7 -> enable output to front L/R speaker channels
 *   GPIO 8 -> enable output to other speaker channels and front panel headphone
 *
 * WM8776:
 *
 *   input 1 <- line
 *   input 2 <- mic
 *   input 3 <- front mic
 *   input 4 <- aux
 */

/*
 * Xonar HDAV1.3 Slim
 * ------------------
 *
 * CMI8788:
 *
 *   I²C <-> WM8776 (addr 0011010)
 *
 *   GPIO 0  -> disable HDMI output
 *   GPIO 1  -> enable HP output
 *   GPIO 6  -> firmware EEPROM I²C clock
 *   GPIO 7 <-> firmware EEPROM I²C data
 *
 *   UART <-> HDMI controller
 *
 * WM8776:
 *
 *   input 1 <- mic
 *   input 2 <- aux
 */

#include <linux/pci.h>
#include <linux/delay.h>
#include <sound/control.h>
#include <sound/core.h>
#include <sound/info.h>
#include <sound/jack.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/tlv.h>
#include "xonar.h"
#include "wm8776.h"
#include "wm8766.h"

#define GPIO_DS_HP_DETECT	0x0010
#define GPIO_DS_INPUT_ROUTE	0x0040
#define GPIO_DS_OUTPUT_FRONTLR	0x0080
#define GPIO_DS_OUTPUT_ENABLE	0x0100

#define GPIO_SLIM_HDMI_DISABLE	0x0001
#define GPIO_SLIM_OUTPUT_ENABLE	0x0002
#define GPIO_SLIM_FIRMWARE_CLK	0x0040
#define GPIO_SLIM_FIRMWARE_DATA	0x0080

#define I2C_DEVICE_WM8776	0x34	/* 001101, 0, /W=0 */

#define LC_CONTROL_LIMITER	0x40000000
#define LC_CONTROL_ALC		0x20000000

struct xonar_wm87x6 {
	struct xonar_generic generic;
	u16 wm8776_regs[0x17];
	u16 wm8766_regs[0x10];
	struct snd_kcontrol *line_adcmux_control;
	struct snd_kcontrol *mic_adcmux_control;
	struct snd_kcontrol *lc_controls[13];
	struct snd_jack *hp_jack;
	struct xonar_hdmi hdmi;
};

static void wm8776_write_spi(struct oxygen *chip,
			     unsigned int reg, unsigned int value)
{
	oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
			 OXYGEN_SPI_DATA_LENGTH_2 |
			 OXYGEN_SPI_CLOCK_160 |
			 (1 << OXYGEN_SPI_CODEC_SHIFT) |
			 OXYGEN_SPI_CEN_LATCH_CLOCK_LO,
			 (reg << 9) | value);
}

static void wm8776_write_i2c(struct oxygen *chip,
			     unsigned int reg, unsigned int value)
{
	oxygen_write_i2c(chip, I2C_DEVICE_WM8776,
			 (reg << 1) | (value >> 8), value);
}

static void wm8776_write(struct oxygen *chip,
			 unsigned int reg, unsigned int value)
{
	struct xonar_wm87x6 *data = chip->model_data;

	if ((chip->model.function_flags & OXYGEN_FUNCTION_2WIRE_SPI_MASK) ==
	    OXYGEN_FUNCTION_SPI)
		wm8776_write_spi(chip, reg, value);
	else
		wm8776_write_i2c(chip, reg, value);
	if (reg < ARRAY_SIZE(data->wm8776_regs)) {
		if (reg >= WM8776_HPLVOL && reg <= WM8776_DACMASTER)
			value &= ~WM8776_UPDATE;
		data->wm8776_regs[reg] = value;
	}
}

static void wm8776_write_cached(struct oxygen *chip,
				unsigned int reg, unsigned int value)
{
	struct xonar_wm87x6 *data = chip->model_data;

	if (reg >= ARRAY_SIZE(data->wm8776_regs) ||
	    value != data->wm8776_regs[reg])
		wm8776_write(chip, reg, value);
}

static void wm8766_write(struct oxygen *chip,
			 unsigned int reg, unsigned int value)
{
	struct xonar_wm87x6 *data = chip->model_data;

	oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
			 OXYGEN_SPI_DATA_LENGTH_2 |
			 OXYGEN_SPI_CLOCK_160 |
			 (0 << OXYGEN_SPI_CODEC_SHIFT) |
			 OXYGEN_SPI_CEN_LATCH_CLOCK_LO,
			 (reg << 9) | value);
	if (reg < ARRAY_SIZE(data->wm8766_regs)) {
		if ((reg >= WM8766_LDA1 && reg <= WM8766_RDA1) ||
		    (reg >= WM8766_LDA2 && reg <= WM8766_MASTDA))
			value &= ~WM8766_UPDATE;
		data->wm8766_regs[reg] = value;
	}
}

static void wm8766_write_cached(struct oxygen *chip,
				unsigned int reg, unsigned int value)
{
	struct xonar_wm87x6 *data = chip->model_data;

	if (reg >= ARRAY_SIZE(data->wm8766_regs) ||
	    value != data->wm8766_regs[reg])
		wm8766_write(chip, reg, value);
}

static void wm8776_registers_init(struct oxygen *chip)
{
	struct xonar_wm87x6 *data = chip->model_data;

	wm8776_write(chip, WM8776_RESET, 0);
	wm8776_write(chip, WM8776_DACCTRL1, WM8776_DZCEN |
		     WM8776_PL_LEFT_LEFT | WM8776_PL_RIGHT_RIGHT);
	wm8776_write(chip, WM8776_DACMUTE, chip->dac_mute ? WM8776_DMUTE : 0);
	wm8776_write(chip, WM8776_DACIFCTRL,
		     WM8776_DACFMT_LJUST | WM8776_DACWL_24);
	wm8776_write(chip, WM8776_ADCIFCTRL,
		     data->wm8776_regs[WM8776_ADCIFCTRL]);
	wm8776_write(chip, WM8776_MSTRCTRL, data->wm8776_regs[WM8776_MSTRCTRL]);
	wm8776_write(chip, WM8776_PWRDOWN, data->wm8776_regs[WM8776_PWRDOWN]);
	wm8776_write(chip, WM8776_HPLVOL, data->wm8776_regs[WM8776_HPLVOL]);
	wm8776_write(chip, WM8776_HPRVOL, data->wm8776_regs[WM8776_HPRVOL] |
		     WM8776_UPDATE);
	wm8776_write(chip, WM8776_ADCLVOL, data->wm8776_regs[WM8776_ADCLVOL]);
	wm8776_write(chip, WM8776_ADCRVOL, data->wm8776_regs[WM8776_ADCRVOL]);
	wm8776_write(chip, WM8776_ADCMUX, data->wm8776_regs[WM8776_ADCMUX]);
	wm8776_write(chip, WM8776_DACLVOL, chip->dac_volume[0]);
	wm8776_write(chip, WM8776_DACRVOL, chip->dac_volume[1] | WM8776_UPDATE);
}

static void wm8766_registers_init(struct oxygen *chip)
{
	struct xonar_wm87x6 *data = chip->model_data;

	wm8766_write(chip, WM8766_RESET, 0);
	wm8766_write(chip, WM8766_DAC_CTRL, data->wm8766_regs[WM8766_DAC_CTRL]);
	wm8766_write(chip, WM8766_INT_CTRL, WM8766_FMT_LJUST | WM8766_IWL_24);
	wm8766_write(chip, WM8766_DAC_CTRL2,
		     WM8766_ZCD | (chip->dac_mute ? WM8766_DMUTE_MASK : 0));
	wm8766_write(chip, WM8766_LDA1, chip->dac_volume[2]);
	wm8766_write(chip, WM8766_RDA1, chip->dac_volume[3]);
	wm8766_write(chip, WM8766_LDA2, chip->dac_volume[4]);
	wm8766_write(chip, WM8766_RDA2, chip->dac_volume[5]);
	wm8766_write(chip, WM8766_LDA3, chip->dac_volume[6]);
	wm8766_write(chip, WM8766_RDA3, chip->dac_volume[7] | WM8766_UPDATE);
}

static void wm8776_init(struct oxygen *chip)
{
	struct xonar_wm87x6 *data = chip->model_data;

	data->wm8776_regs[WM8776_HPLVOL] = (0x79 - 60) | WM8776_HPZCEN;
	data->wm8776_regs[WM8776_HPRVOL] = (0x79 - 60) | WM8776_HPZCEN;
	data->wm8776_regs[WM8776_ADCIFCTRL] =
		WM8776_ADCFMT_LJUST | WM8776_ADCWL_24 | WM8776_ADCMCLK;
	data->wm8776_regs[WM8776_MSTRCTRL] =
		WM8776_ADCRATE_256 | WM8776_DACRATE_256;
	data->wm8776_regs[WM8776_PWRDOWN] = WM8776_HPPD;
	data->wm8776_regs[WM8776_ADCLVOL] = 0xa5 | WM8776_ZCA;
	data->wm8776_regs[WM8776_ADCRVOL] = 0xa5 | WM8776_ZCA;
	data->wm8776_regs[WM8776_ADCMUX] = 0x001;
	wm8776_registers_init(chip);
}

static void wm8766_init(struct oxygen *chip)
{
	struct xonar_wm87x6 *data = chip->model_data;

	data->wm8766_regs[WM8766_DAC_CTRL] =
		WM8766_PL_LEFT_LEFT | WM8766_PL_RIGHT_RIGHT;
	wm8766_registers_init(chip);
}

static void xonar_ds_handle_hp_jack(struct oxygen *chip)
{
	struct xonar_wm87x6 *data = chip->model_data;
	bool hp_plugged;
	unsigned int reg;

	mutex_lock(&chip->mutex);

	hp_plugged = !(oxygen_read16(chip, OXYGEN_GPIO_DATA) &
		       GPIO_DS_HP_DETECT);

	oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
			      hp_plugged ? 0 : GPIO_DS_OUTPUT_FRONTLR,
			      GPIO_DS_OUTPUT_FRONTLR);

	reg = data->wm8766_regs[WM8766_DAC_CTRL] & ~WM8766_MUTEALL;
	if (hp_plugged)
		reg |= WM8766_MUTEALL;
	wm8766_write_cached(chip, WM8766_DAC_CTRL, reg);

	snd_jack_report(data->hp_jack, hp_plugged ? SND_JACK_HEADPHONE : 0);

	mutex_unlock(&chip->mutex);
}

static void xonar_ds_init(struct oxygen *chip)
{
	struct xonar_wm87x6 *data = chip->model_data;

	data->generic.anti_pop_delay = 300;
	data->generic.output_enable_bit = GPIO_DS_OUTPUT_ENABLE;

	wm8776_init(chip);
	wm8766_init(chip);

	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
			  GPIO_DS_INPUT_ROUTE | GPIO_DS_OUTPUT_FRONTLR);
	oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL,
			    GPIO_DS_HP_DETECT);
	oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, GPIO_DS_INPUT_ROUTE);
	oxygen_set_bits16(chip, OXYGEN_GPIO_INTERRUPT_MASK, GPIO_DS_HP_DETECT);
	chip->interrupt_mask |= OXYGEN_INT_GPIO;

	xonar_enable_output(chip);

	snd_jack_new(chip->card, "Headphone",
		     SND_JACK_HEADPHONE, &data->hp_jack);
	xonar_ds_handle_hp_jack(chip);

	snd_component_add(chip->card, "WM8776");
	snd_component_add(chip->card, "WM8766");
}

static void xonar_hdav_slim_init(struct oxygen *chip)
{
	struct xonar_wm87x6 *data = chip->model_data;

	data->generic.anti_pop_delay = 300;
	data->generic.output_enable_bit = GPIO_SLIM_OUTPUT_ENABLE;

	wm8776_init(chip);

	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
			  GPIO_SLIM_HDMI_DISABLE |
			  GPIO_SLIM_FIRMWARE_CLK |
			  GPIO_SLIM_FIRMWARE_DATA);

	xonar_hdmi_init(chip, &data->hdmi);
	xonar_enable_output(chip);

	snd_component_add(chip->card, "WM8776");
}

static void xonar_ds_cleanup(struct oxygen *chip)
{
	xonar_disable_output(chip);
	wm8776_write(chip, WM8776_RESET, 0);
}

static void xonar_hdav_slim_cleanup(struct oxygen *chip)
{
	xonar_hdmi_cleanup(chip);
	xonar_disable_output(chip);
	wm8776_write(chip, WM8776_RESET, 0);
	msleep(2);
}

static void xonar_ds_suspend(struct oxygen *chip)
{
	xonar_ds_cleanup(chip);
}

static void xonar_hdav_slim_suspend(struct oxygen *chip)
{
	xonar_hdav_slim_cleanup(chip);
}

static void xonar_ds_resume(struct oxygen *chip)
{
	wm8776_registers_init(chip);
	wm8766_registers_init(chip);
	xonar_enable_output(chip);
	xonar_ds_handle_hp_jack(chip);
}

static void xonar_hdav_slim_resume(struct oxygen *chip)
{
	struct xonar_wm87x6 *data = chip->model_data;

	wm8776_registers_init(chip);
	xonar_hdmi_resume(chip, &data->hdmi);
	xonar_enable_output(chip);
}

static void wm8776_adc_hardware_filter(unsigned int channel,
				       struct snd_pcm_hardware *hardware)
{
	if (channel == PCM_A) {
		hardware->rates = SNDRV_PCM_RATE_32000 |
				  SNDRV_PCM_RATE_44100 |
				  SNDRV_PCM_RATE_48000 |
				  SNDRV_PCM_RATE_64000 |
				  SNDRV_PCM_RATE_88200 |
				  SNDRV_PCM_RATE_96000;
		hardware->rate_max = 96000;
	}
}

static void xonar_hdav_slim_hardware_filter(unsigned int channel,
					    struct snd_pcm_hardware *hardware)
{
	wm8776_adc_hardware_filter(channel, hardware);
	xonar_hdmi_pcm_hardware_filter(channel, hardware);
}

static void set_wm87x6_dac_params(struct oxygen *chip,
				  struct snd_pcm_hw_params *params)
{
}

static void set_wm8776_adc_params(struct oxygen *chip,
				  struct snd_pcm_hw_params *params)
{
	u16 reg;

	reg = WM8776_ADCRATE_256 | WM8776_DACRATE_256;
	if (params_rate(params) > 48000)
		reg |= WM8776_ADCOSR;
	wm8776_write_cached(chip, WM8776_MSTRCTRL, reg);
}

static void set_hdav_slim_dac_params(struct oxygen *chip,
				     struct snd_pcm_hw_params *params)
{
	struct xonar_wm87x6 *data = chip->model_data;

	xonar_set_hdmi_params(chip, &data->hdmi, params);
}

static void update_wm8776_volume(struct oxygen *chip)
{
	struct xonar_wm87x6 *data = chip->model_data;
	u8 to_change;

	if (chip->dac_volume[0] == chip->dac_volume[1]) {
		if (chip->dac_volume[0] != data->wm8776_regs[WM8776_DACLVOL] ||
		    chip->dac_volume[1] != data->wm8776_regs[WM8776_DACRVOL]) {
			wm8776_write(chip, WM8776_DACMASTER,
				     chip->dac_volume[0] | WM8776_UPDATE);
			data->wm8776_regs[WM8776_DACLVOL] = chip->dac_volume[0];
			data->wm8776_regs[WM8776_DACRVOL] = chip->dac_volume[0];
		}
	} else {
		to_change = (chip->dac_volume[0] !=
			     data->wm8776_regs[WM8776_DACLVOL]) << 0;
		to_change |= (chip->dac_volume[1] !=
			      data->wm8776_regs[WM8776_DACLVOL]) << 1;
		if (to_change & 1)
			wm8776_write(chip, WM8776_DACLVOL, chip->dac_volume[0] |
				     ((to_change & 2) ? 0 : WM8776_UPDATE));
		if (to_change & 2)
			wm8776_write(chip, WM8776_DACRVOL,
				     chip->dac_volume[1] | WM8776_UPDATE);
	}
}

static void update_wm87x6_volume(struct oxygen *chip)
{
	static const u8 wm8766_regs[6] = {
		WM8766_LDA1, WM8766_RDA1,
		WM8766_LDA2, WM8766_RDA2,
		WM8766_LDA3, WM8766_RDA3,
	};
	struct xonar_wm87x6 *data = chip->model_data;
	unsigned int i;
	u8 to_change;

	update_wm8776_volume(chip);
	if (chip->dac_volume[2] == chip->dac_volume[3] &&
	    chip->dac_volume[2] == chip->dac_volume[4] &&
	    chip->dac_volume[2] == chip->dac_volume[5] &&
	    chip->dac_volume[2] == chip->dac_volume[6] &&
	    chip->dac_volume[2] == chip->dac_volume[7]) {
		to_change = 0;
		for (i = 0; i < 6; ++i)
			if (chip->dac_volume[2] !=
			    data->wm8766_regs[wm8766_regs[i]])
				to_change = 1;
		if (to_change) {
			wm8766_write(chip, WM8766_MASTDA,
				     chip->dac_volume[2] | WM8766_UPDATE);
			for (i = 0; i < 6; ++i)
				data->wm8766_regs[wm8766_regs[i]] =
					chip->dac_volume[2];
		}
	} else {
		to_change = 0;
		for (i = 0; i < 6; ++i)
			to_change |= (chip->dac_volume[2 + i] !=
				      data->wm8766_regs[wm8766_regs[i]]) << i;
		for (i = 0; i < 6; ++i)
			if (to_change & (1 << i))
				wm8766_write(chip, wm8766_regs[i],
					     chip->dac_volume[2 + i] |
					     ((to_change & (0x3e << i))
					      ? 0 : WM8766_UPDATE));
	}
}

static void update_wm8776_mute(struct oxygen *chip)
{
	wm8776_write_cached(chip, WM8776_DACMUTE,
			    chip->dac_mute ? WM8776_DMUTE : 0);
}

static void update_wm87x6_mute(struct oxygen *chip)
{
	update_wm8776_mute(chip);
	wm8766_write_cached(chip, WM8766_DAC_CTRL2, WM8766_ZCD |
			    (chip->dac_mute ? WM8766_DMUTE_MASK : 0));
}

static void update_wm8766_center_lfe_mix(struct oxygen *chip, bool mixed)
{
	struct xonar_wm87x6 *data = chip->model_data;
	unsigned int reg;

	/*
	 * The WM8766 can mix left and right channels, but this setting
	 * applies to all three stereo pairs.
	 */
	reg = data->wm8766_regs[WM8766_DAC_CTRL] &
		~(WM8766_PL_LEFT_MASK | WM8766_PL_RIGHT_MASK);
	if (mixed)
		reg |= WM8766_PL_LEFT_LRMIX | WM8766_PL_RIGHT_LRMIX;
	else
		reg |= WM8766_PL_LEFT_LEFT | WM8766_PL_RIGHT_RIGHT;
	wm8766_write_cached(chip, WM8766_DAC_CTRL, reg);
}

static void xonar_ds_gpio_changed(struct oxygen *chip)
{
	xonar_ds_handle_hp_jack(chip);
}

static int wm8776_bit_switch_get(struct snd_kcontrol *ctl,
				 struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	struct xonar_wm87x6 *data = chip->model_data;
	u16 bit = ctl->private_value & 0xffff;
	unsigned int reg_index = (ctl->private_value >> 16) & 0xff;
	bool invert = (ctl->private_value >> 24) & 1;

	value->value.integer.value[0] =
		((data->wm8776_regs[reg_index] & bit) != 0) ^ invert;
	return 0;
}

static int wm8776_bit_switch_put(struct snd_kcontrol *ctl,
				 struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	struct xonar_wm87x6 *data = chip->model_data;
	u16 bit = ctl->private_value & 0xffff;
	u16 reg_value;
	unsigned int reg_index = (ctl->private_value >> 16) & 0xff;
	bool invert = (ctl->private_value >> 24) & 1;
	int changed;

	mutex_lock(&chip->mutex);
	reg_value = data->wm8776_regs[reg_index] & ~bit;
	if (value->value.integer.value[0] ^ invert)
		reg_value |= bit;
	changed = reg_value != data->wm8776_regs[reg_index];
	if (changed)
		wm8776_write(chip, reg_index, reg_value);
	mutex_unlock(&chip->mutex);
	return changed;
}

static int wm8776_field_enum_info(struct snd_kcontrol *ctl,
				  struct snd_ctl_elem_info *info)
{
	static const char *const hld[16] = {
		"0 ms", "2.67 ms", "5.33 ms", "10.6 ms",
		"21.3 ms", "42.7 ms", "85.3 ms", "171 ms",
		"341 ms", "683 ms", "1.37 s", "2.73 s",
		"5.46 s", "10.9 s", "21.8 s", "43.7 s",
	};
	static const char *const atk_lim[11] = {
		"0.25 ms", "0.5 ms", "1 ms", "2 ms",
		"4 ms", "8 ms", "16 ms", "32 ms",
		"64 ms", "128 ms", "256 ms",
	};
	static const char *const atk_alc[11] = {
		"8.40 ms", "16.8 ms", "33.6 ms", "67.2 ms",
		"134 ms", "269 ms", "538 ms", "1.08 s",
		"2.15 s", "4.3 s", "8.6 s",
	};
	static const char *const dcy_lim[11] = {
		"1.2 ms", "2.4 ms", "4.8 ms", "9.6 ms",
		"19.2 ms", "38.4 ms", "76.8 ms", "154 ms",
		"307 ms", "614 ms", "1.23 s",
	};
	static const char *const dcy_alc[11] = {
		"33.5 ms", "67.0 ms", "134 ms", "268 ms",
		"536 ms", "1.07 s", "2.14 s", "4.29 s",
		"8.58 s", "17.2 s", "34.3 s",
	};
	static const char *const tranwin[8] = {
		"0 us", "62.5 us", "125 us", "250 us",
		"500 us", "1 ms", "2 ms", "4 ms",
	};
	u8 max;
	const char *const *names;

	max = (ctl->private_value >> 12) & 0xf;
	info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	info->count = 1;
	info->value.enumerated.items = max + 1;
	if (info->value.enumerated.item > max)
		info->value.enumerated.item = max;
	switch ((ctl->private_value >> 24) & 0x1f) {
	case WM8776_ALCCTRL2:
		names = hld;
		break;
	case WM8776_ALCCTRL3:
		if (((ctl->private_value >> 20) & 0xf) == 0) {
			if (ctl->private_value & LC_CONTROL_LIMITER)
				names = atk_lim;
			else
				names = atk_alc;
		} else {
			if (ctl->private_value & LC_CONTROL_LIMITER)
				names = dcy_lim;
			else
				names = dcy_alc;
		}
		break;
	case WM8776_LIMITER:
		names = tranwin;
		break;
	default:
		return -ENXIO;
	}
	strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
	return 0;
}

static int wm8776_field_volume_info(struct snd_kcontrol *ctl,
				    struct snd_ctl_elem_info *info)
{
	info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	info->count = 1;
	info->value.integer.min = (ctl->private_value >> 8) & 0xf;
	info->value.integer.max = (ctl->private_value >> 12) & 0xf;
	return 0;
}

static void wm8776_field_set_from_ctl(struct snd_kcontrol *ctl)
{
	struct oxygen *chip = ctl->private_data;
	struct xonar_wm87x6 *data = chip->model_data;
	unsigned int value, reg_index, mode;
	u8 min, max, shift;
	u16 mask, reg_value;
	bool invert;

	if ((data->wm8776_regs[WM8776_ALCCTRL1] & WM8776_LCSEL_MASK) ==
	    WM8776_LCSEL_LIMITER)
		mode = LC_CONTROL_LIMITER;
	else
		mode = LC_CONTROL_ALC;
	if (!(ctl->private_value & mode))
		return;

	value = ctl->private_value & 0xf;
	min = (ctl->private_value >> 8) & 0xf;
	max = (ctl->private_value >> 12) & 0xf;
	mask = (ctl->private_value >> 16) & 0xf;
	shift = (ctl->private_value >> 20) & 0xf;
	reg_index = (ctl->private_value >> 24) & 0x1f;
	invert = (ctl->private_value >> 29) & 0x1;

	if (invert)
		value = max - (value - min);
	reg_value = data->wm8776_regs[reg_index];
	reg_value &= ~(mask << shift);
	reg_value |= value << shift;
	wm8776_write_cached(chip, reg_index, reg_value);
}

static int wm8776_field_set(struct snd_kcontrol *ctl, unsigned int value)
{
	struct oxygen *chip = ctl->private_data;
	u8 min, max;
	int changed;

	min = (ctl->private_value >> 8) & 0xf;
	max = (ctl->private_value >> 12) & 0xf;
	if (value < min || value > max)
		return -EINVAL;
	mutex_lock(&chip->mutex);
	changed = value != (ctl->private_value & 0xf);
	if (changed) {
		ctl->private_value = (ctl->private_value & ~0xf) | value;
		wm8776_field_set_from_ctl(ctl);
	}
	mutex_unlock(&chip->mutex);
	return changed;
}

static int wm8776_field_enum_get(struct snd_kcontrol *ctl,
				 struct snd_ctl_elem_value *value)
{
	value->value.enumerated.item[0] = ctl->private_value & 0xf;
	return 0;
}

static int wm8776_field_volume_get(struct snd_kcontrol *ctl,
				   struct snd_ctl_elem_value *value)
{
	value->value.integer.value[0] = ctl->private_value & 0xf;
	return 0;
}

static int wm8776_field_enum_put(struct snd_kcontrol *ctl,
				 struct snd_ctl_elem_value *value)
{
	return wm8776_field_set(ctl, value->value.enumerated.item[0]);
}

static int wm8776_field_volume_put(struct snd_kcontrol *ctl,
				   struct snd_ctl_elem_value *value)
{
	return wm8776_field_set(ctl, value->value.integer.value[0]);
}

static int wm8776_hp_vol_info(struct snd_kcontrol *ctl,
			      struct snd_ctl_elem_info *info)
{
	info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	info->count = 2;
	info->value.integer.min = 0x79 - 60;
	info->value.integer.max = 0x7f;
	return 0;
}

static int wm8776_hp_vol_get(struct snd_kcontrol *ctl,
			     struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	struct xonar_wm87x6 *data = chip->model_data;

	mutex_lock(&chip->mutex);
	value->value.integer.value[0] =
		data->wm8776_regs[WM8776_HPLVOL] & WM8776_HPATT_MASK;
	value->value.integer.value[1] =
		data->wm8776_regs[WM8776_HPRVOL] & WM8776_HPATT_MASK;
	mutex_unlock(&chip->mutex);
	return 0;
}

static int wm8776_hp_vol_put(struct snd_kcontrol *ctl,
			     struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	struct xonar_wm87x6 *data = chip->model_data;
	u8 to_update;

	mutex_lock(&chip->mutex);
	to_update = (value->value.integer.value[0] !=
		     (data->wm8776_regs[WM8776_HPLVOL] & WM8776_HPATT_MASK))
		<< 0;
	to_update |= (value->value.integer.value[1] !=
		      (data->wm8776_regs[WM8776_HPRVOL] & WM8776_HPATT_MASK))
		<< 1;
	if (value->value.integer.value[0] == value->value.integer.value[1]) {
		if (to_update) {
			wm8776_write(chip, WM8776_HPMASTER,
				     value->value.integer.value[0] |
				     WM8776_HPZCEN | WM8776_UPDATE);
			data->wm8776_regs[WM8776_HPLVOL] =
				value->value.integer.value[0] | WM8776_HPZCEN;
			data->wm8776_regs[WM8776_HPRVOL] =
				value->value.integer.value[0] | WM8776_HPZCEN;
		}
	} else {
		if (to_update & 1)
			wm8776_write(chip, WM8776_HPLVOL,
				     value->value.integer.value[0] |
				     WM8776_HPZCEN |
				     ((to_update & 2) ? 0 : WM8776_UPDATE));
		if (to_update & 2)
			wm8776_write(chip, WM8776_HPRVOL,
				     value->value.integer.value[1] |
				     WM8776_HPZCEN | WM8776_UPDATE);
	}
	mutex_unlock(&chip->mutex);
	return to_update != 0;
}

static int wm8776_input_mux_get(struct snd_kcontrol *ctl,
				struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	struct xonar_wm87x6 *data = chip->model_data;
	unsigned int mux_bit = ctl->private_value;

	value->value.integer.value[0] =
		!!(data->wm8776_regs[WM8776_ADCMUX] & mux_bit);
	return 0;
}

static int wm8776_input_mux_put(struct snd_kcontrol *ctl,
				struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	struct xonar_wm87x6 *data = chip->model_data;
	struct snd_kcontrol *other_ctl;
	unsigned int mux_bit = ctl->private_value;
	u16 reg;
	int changed;

	mutex_lock(&chip->mutex);
	reg = data->wm8776_regs[WM8776_ADCMUX];
	if (value->value.integer.value[0]) {
		reg |= mux_bit;
		/* line-in and mic-in are exclusive */
		mux_bit ^= 3;
		if (reg & mux_bit) {
			reg &= ~mux_bit;
			if (mux_bit == 1)
				other_ctl = data->line_adcmux_control;
			else
				other_ctl = data->mic_adcmux_control;
			snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
				       &other_ctl->id);
		}
	} else
		reg &= ~mux_bit;
	changed = reg != data->wm8776_regs[WM8776_ADCMUX];
	if (changed) {
		oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
				      reg & 1 ? GPIO_DS_INPUT_ROUTE : 0,
				      GPIO_DS_INPUT_ROUTE);
		wm8776_write(chip, WM8776_ADCMUX, reg);
	}
	mutex_unlock(&chip->mutex);
	return changed;
}

static int wm8776_input_vol_info(struct snd_kcontrol *ctl,
				 struct snd_ctl_elem_info *info)
{
	info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	info->count = 2;
	info->value.integer.min = 0xa5;
	info->value.integer.max = 0xff;
	return 0;
}

static int wm8776_input_vol_get(struct snd_kcontrol *ctl,
				struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	struct xonar_wm87x6 *data = chip->model_data;

	mutex_lock(&chip->mutex);
	value->value.integer.value[0] =
		data->wm8776_regs[WM8776_ADCLVOL] & WM8776_AGMASK;
	value->value.integer.value[1] =
		data->wm8776_regs[WM8776_ADCRVOL] & WM8776_AGMASK;
	mutex_unlock(&chip->mutex);
	return 0;
}

static int wm8776_input_vol_put(struct snd_kcontrol *ctl,
				struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	struct xonar_wm87x6 *data = chip->model_data;
	int changed = 0;

	mutex_lock(&chip->mutex);
	changed = (value->value.integer.value[0] !=
		   (data->wm8776_regs[WM8776_ADCLVOL] & WM8776_AGMASK)) ||
		  (value->value.integer.value[1] !=
		   (data->wm8776_regs[WM8776_ADCRVOL] & WM8776_AGMASK));
	wm8776_write_cached(chip, WM8776_ADCLVOL,
			    value->value.integer.value[0] | WM8776_ZCA);
	wm8776_write_cached(chip, WM8776_ADCRVOL,
			    value->value.integer.value[1] | WM8776_ZCA);
	mutex_unlock(&chip->mutex);
	return changed;
}

static int wm8776_level_control_info(struct snd_kcontrol *ctl,
				     struct snd_ctl_elem_info *info)
{
	static const char *const names[3] = {
		"None", "Peak Limiter", "Automatic Level Control"
	};
	info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	info->count = 1;
	info->value.enumerated.items = 3;
	if (info->value.enumerated.item >= 3)
		info->value.enumerated.item = 2;
	strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
	return 0;
}

static int wm8776_level_control_get(struct snd_kcontrol *ctl,
				    struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	struct xonar_wm87x6 *data = chip->model_data;

	if (!(data->wm8776_regs[WM8776_ALCCTRL2] & WM8776_LCEN))
		value->value.enumerated.item[0] = 0;
	else if ((data->wm8776_regs[WM8776_ALCCTRL1] & WM8776_LCSEL_MASK) ==
		 WM8776_LCSEL_LIMITER)
		value->value.enumerated.item[0] = 1;
	else
		value->value.enumerated.item[0] = 2;
	return 0;
}

static void activate_control(struct oxygen *chip,
			     struct snd_kcontrol *ctl, unsigned int mode)
{
	unsigned int access;

	if (ctl->private_value & mode)
		access = 0;
	else
		access = SNDRV_CTL_ELEM_ACCESS_INACTIVE;
	if ((ctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_INACTIVE) != access) {
		ctl->vd[0].access ^= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
	}
}

static int wm8776_level_control_put(struct snd_kcontrol *ctl,
				    struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	struct xonar_wm87x6 *data = chip->model_data;
	unsigned int mode = 0, i;
	u16 ctrl1, ctrl2;
	int changed;

	if (value->value.enumerated.item[0] >= 3)
		return -EINVAL;
	mutex_lock(&chip->mutex);
	changed = value->value.enumerated.item[0] != ctl->private_value;
	if (changed) {
		ctl->private_value = value->value.enumerated.item[0];
		ctrl1 = data->wm8776_regs[WM8776_ALCCTRL1];
		ctrl2 = data->wm8776_regs[WM8776_ALCCTRL2];
		switch (value->value.enumerated.item[0]) {
		default:
			wm8776_write_cached(chip, WM8776_ALCCTRL2,
					    ctrl2 & ~WM8776_LCEN);
			break;
		case 1:
			wm8776_write_cached(chip, WM8776_ALCCTRL1,
					    (ctrl1 & ~WM8776_LCSEL_MASK) |
					    WM8776_LCSEL_LIMITER);
			wm8776_write_cached(chip, WM8776_ALCCTRL2,
					    ctrl2 | WM8776_LCEN);
			mode = LC_CONTROL_LIMITER;
			break;
		case 2:
			wm8776_write_cached(chip, WM8776_ALCCTRL1,
					    (ctrl1 & ~WM8776_LCSEL_MASK) |
					    WM8776_LCSEL_ALC_STEREO);
			wm8776_write_cached(chip, WM8776_ALCCTRL2,
					    ctrl2 | WM8776_LCEN);
			mode = LC_CONTROL_ALC;
			break;
		}
		for (i = 0; i < ARRAY_SIZE(data->lc_controls); ++i)
			activate_control(chip, data->lc_controls[i], mode);
	}
	mutex_unlock(&chip->mutex);
	return changed;
}

static int hpf_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
{
	static const char *const names[2] = {
		"None", "High-pass Filter"
	};

	info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	info->count = 1;
	info->value.enumerated.items = 2;
	if (info->value.enumerated.item >= 2)
		info->value.enumerated.item = 1;
	strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
	return 0;
}

static int hpf_get(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	struct xonar_wm87x6 *data = chip->model_data;

	value->value.enumerated.item[0] =
		!(data->wm8776_regs[WM8776_ADCIFCTRL] & WM8776_ADCHPD);
	return 0;
}

static int hpf_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	struct xonar_wm87x6 *data = chip->model_data;
	unsigned int reg;
	int changed;

	mutex_lock(&chip->mutex);
	reg = data->wm8776_regs[WM8776_ADCIFCTRL] & ~WM8776_ADCHPD;
	if (!value->value.enumerated.item[0])
		reg |= WM8776_ADCHPD;
	changed = reg != data->wm8776_regs[WM8776_ADCIFCTRL];
	if (changed)
		wm8776_write(chip, WM8776_ADCIFCTRL, reg);
	mutex_unlock(&chip->mutex);
	return changed;
}

#define WM8776_BIT_SWITCH(xname, reg, bit, invert, flags) { \
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.info = snd_ctl_boolean_mono_info, \
	.get = wm8776_bit_switch_get, \
	.put = wm8776_bit_switch_put, \
	.private_value = ((reg) << 16) | (bit) | ((invert) << 24) | (flags), \
}
#define _WM8776_FIELD_CTL(xname, reg, shift, initval, min, max, mask, flags) \
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
	.name = xname, \
	.private_value = (initval) | ((min) << 8) | ((max) << 12) | \
	((mask) << 16) | ((shift) << 20) | ((reg) << 24) | (flags)
#define WM8776_FIELD_CTL_ENUM(xname, reg, shift, init, min, max, mask, flags) {\
	_WM8776_FIELD_CTL(xname " Capture Enum", \
			  reg, shift, init, min, max, mask, flags), \
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
		  SNDRV_CTL_ELEM_ACCESS_INACTIVE, \
	.info = wm8776_field_enum_info, \
	.get = wm8776_field_enum_get, \
	.put = wm8776_field_enum_put, \
}
#define WM8776_FIELD_CTL_VOLUME(a, b, c, d, e, f, g, h, tlv_p) { \
	_WM8776_FIELD_CTL(a " Capture Volume", b, c, d, e, f, g, h), \
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
		  SNDRV_CTL_ELEM_ACCESS_INACTIVE | \
		  SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
	.info = wm8776_field_volume_info, \
	.get = wm8776_field_volume_get, \
	.put = wm8776_field_volume_put, \
	.tlv = { .p = tlv_p }, \
}

static const DECLARE_TLV_DB_SCALE(wm87x6_dac_db_scale, -6000, 50, 0);
static const DECLARE_TLV_DB_SCALE(wm8776_adc_db_scale, -2100, 50, 0);
static const DECLARE_TLV_DB_SCALE(wm8776_hp_db_scale, -6000, 100, 0);
static const DECLARE_TLV_DB_SCALE(wm8776_lct_db_scale, -1600, 100, 0);
static const DECLARE_TLV_DB_SCALE(wm8776_maxgain_db_scale, 0, 400, 0);
static const DECLARE_TLV_DB_SCALE(wm8776_ngth_db_scale, -7800, 600, 0);
static const DECLARE_TLV_DB_SCALE(wm8776_maxatten_lim_db_scale, -1200, 100, 0);
static const DECLARE_TLV_DB_SCALE(wm8776_maxatten_alc_db_scale, -2100, 400, 0);

static const struct snd_kcontrol_new ds_controls[] = {
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Headphone Playback Volume",
		.info = wm8776_hp_vol_info,
		.get = wm8776_hp_vol_get,
		.put = wm8776_hp_vol_put,
		.tlv = { .p = wm8776_hp_db_scale },
	},
	WM8776_BIT_SWITCH("Headphone Playback Switch",
			  WM8776_PWRDOWN, WM8776_HPPD, 1, 0),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Input Capture Volume",
		.info = wm8776_input_vol_info,
		.get = wm8776_input_vol_get,
		.put = wm8776_input_vol_put,
		.tlv = { .p = wm8776_adc_db_scale },
	},
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Line Capture Switch",
		.info = snd_ctl_boolean_mono_info,
		.get = wm8776_input_mux_get,
		.put = wm8776_input_mux_put,
		.private_value = 1 << 0,
	},
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Mic Capture Switch",
		.info = snd_ctl_boolean_mono_info,
		.get = wm8776_input_mux_get,
		.put = wm8776_input_mux_put,
		.private_value = 1 << 1,
	},
	WM8776_BIT_SWITCH("Front Mic Capture Switch",
			  WM8776_ADCMUX, 1 << 2, 0, 0),
	WM8776_BIT_SWITCH("Aux Capture Switch",
			  WM8776_ADCMUX, 1 << 3, 0, 0),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "ADC Filter Capture Enum",
		.info = hpf_info,
		.get = hpf_get,
		.put = hpf_put,
	},
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Level Control Capture Enum",
		.info = wm8776_level_control_info,
		.get = wm8776_level_control_get,
		.put = wm8776_level_control_put,
		.private_value = 0,
	},
};
static const struct snd_kcontrol_new hdav_slim_controls[] = {
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "HDMI Playback Switch",
		.info = snd_ctl_boolean_mono_info,
		.get = xonar_gpio_bit_switch_get,
		.put = xonar_gpio_bit_switch_put,
		.private_value = GPIO_SLIM_HDMI_DISABLE | XONAR_GPIO_BIT_INVERT,
	},
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Headphone Playback Volume",
		.info = wm8776_hp_vol_info,
		.get = wm8776_hp_vol_get,
		.put = wm8776_hp_vol_put,
		.tlv = { .p = wm8776_hp_db_scale },
	},
	WM8776_BIT_SWITCH("Headphone Playback Switch",
			  WM8776_PWRDOWN, WM8776_HPPD, 1, 0),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Input Capture Volume",
		.info = wm8776_input_vol_info,
		.get = wm8776_input_vol_get,
		.put = wm8776_input_vol_put,
		.tlv = { .p = wm8776_adc_db_scale },
	},
	WM8776_BIT_SWITCH("Mic Capture Switch",
			  WM8776_ADCMUX, 1 << 0, 0, 0),
	WM8776_BIT_SWITCH("Aux Capture Switch",
			  WM8776_ADCMUX, 1 << 1, 0, 0),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "ADC Filter Capture Enum",
		.info = hpf_info,
		.get = hpf_get,
		.put = hpf_put,
	},
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "Level Control Capture Enum",
		.info = wm8776_level_control_info,
		.get = wm8776_level_control_get,
		.put = wm8776_level_control_put,
		.private_value = 0,
	},
};
static const struct snd_kcontrol_new lc_controls[] = {
	WM8776_FIELD_CTL_VOLUME("Limiter Threshold",
				WM8776_ALCCTRL1, 0, 11, 0, 15, 0xf,
				LC_CONTROL_LIMITER, wm8776_lct_db_scale),
	WM8776_FIELD_CTL_ENUM("Limiter Attack Time",
			      WM8776_ALCCTRL3, 0, 2, 0, 10, 0xf,
			      LC_CONTROL_LIMITER),
	WM8776_FIELD_CTL_ENUM("Limiter Decay Time",
			      WM8776_ALCCTRL3, 4, 3, 0, 10, 0xf,
			      LC_CONTROL_LIMITER),
	WM8776_FIELD_CTL_ENUM("Limiter Transient Window",
			      WM8776_LIMITER, 4, 2, 0, 7, 0x7,
			      LC_CONTROL_LIMITER),
	WM8776_FIELD_CTL_VOLUME("Limiter Maximum Attenuation",
				WM8776_LIMITER, 0, 6, 3, 12, 0xf,
				LC_CONTROL_LIMITER,
				wm8776_maxatten_lim_db_scale),
	WM8776_FIELD_CTL_VOLUME("ALC Target Level",
				WM8776_ALCCTRL1, 0, 11, 0, 15, 0xf,
				LC_CONTROL_ALC, wm8776_lct_db_scale),
	WM8776_FIELD_CTL_ENUM("ALC Attack Time",
			      WM8776_ALCCTRL3, 0, 2, 0, 10, 0xf,
			      LC_CONTROL_ALC),
	WM8776_FIELD_CTL_ENUM("ALC Decay Time",
			      WM8776_ALCCTRL3, 4, 3, 0, 10, 0xf,
			      LC_CONTROL_ALC),
	WM8776_FIELD_CTL_VOLUME("ALC Maximum Gain",
				WM8776_ALCCTRL1, 4, 7, 1, 7, 0x7,
				LC_CONTROL_ALC, wm8776_maxgain_db_scale),
	WM8776_FIELD_CTL_VOLUME("ALC Maximum Attenuation",
				WM8776_LIMITER, 0, 10, 10, 15, 0xf,
				LC_CONTROL_ALC, wm8776_maxatten_alc_db_scale),
	WM8776_FIELD_CTL_ENUM("ALC Hold Time",
			      WM8776_ALCCTRL2, 0, 0, 0, 15, 0xf,
			      LC_CONTROL_ALC),
	WM8776_BIT_SWITCH("Noise Gate Capture Switch",
			  WM8776_NOISEGATE, WM8776_NGAT, 0,
			  LC_CONTROL_ALC),
	WM8776_FIELD_CTL_VOLUME("Noise Gate Threshold",
				WM8776_NOISEGATE, 2, 0, 0, 7, 0x7,
				LC_CONTROL_ALC, wm8776_ngth_db_scale),
};

static int add_lc_controls(struct oxygen *chip)
{
	struct xonar_wm87x6 *data = chip->model_data;
	unsigned int i;
	struct snd_kcontrol *ctl;
	int err;

	BUILD_BUG_ON(ARRAY_SIZE(lc_controls) != ARRAY_SIZE(data->lc_controls));
	for (i = 0; i < ARRAY_SIZE(lc_controls); ++i) {
		ctl = snd_ctl_new1(&lc_controls[i], chip);
		if (!ctl)
			return -ENOMEM;
		err = snd_ctl_add(chip->card, ctl);
		if (err < 0)
			return err;
		data->lc_controls[i] = ctl;
	}
	return 0;
}

static int xonar_ds_mixer_init(struct oxygen *chip)
{
	struct xonar_wm87x6 *data = chip->model_data;
	unsigned int i;
	struct snd_kcontrol *ctl;
	int err;

	for (i = 0; i < ARRAY_SIZE(ds_controls); ++i) {
		ctl = snd_ctl_new1(&ds_controls[i], chip);
		if (!ctl)
			return -ENOMEM;
		err = snd_ctl_add(chip->card, ctl);
		if (err < 0)
			return err;
		if (!strcmp(ctl->id.name, "Line Capture Switch"))
			data->line_adcmux_control = ctl;
		else if (!strcmp(ctl->id.name, "Mic Capture Switch"))
			data->mic_adcmux_control = ctl;
	}
	if (!data->line_adcmux_control || !data->mic_adcmux_control)
		return -ENXIO;

	return add_lc_controls(chip);
}

static int xonar_hdav_slim_mixer_init(struct oxygen *chip)
{
	unsigned int i;
	struct snd_kcontrol *ctl;
	int err;

	for (i = 0; i < ARRAY_SIZE(hdav_slim_controls); ++i) {
		ctl = snd_ctl_new1(&hdav_slim_controls[i], chip);
		if (!ctl)
			return -ENOMEM;
		err = snd_ctl_add(chip->card, ctl);
		if (err < 0)
			return err;
	}

	return add_lc_controls(chip);
}

static void dump_wm8776_registers(struct oxygen *chip,
				  struct snd_info_buffer *buffer)
{
	struct xonar_wm87x6 *data = chip->model_data;
	unsigned int i;

	snd_iprintf(buffer, "\nWM8776:\n00:");
	for (i = 0; i < 0x10; ++i)
		snd_iprintf(buffer, " %03x", data->wm8776_regs[i]);
	snd_iprintf(buffer, "\n10:");
	for (i = 0x10; i < 0x17; ++i)
		snd_iprintf(buffer, " %03x", data->wm8776_regs[i]);
	snd_iprintf(buffer, "\n");
}

static void dump_wm87x6_registers(struct oxygen *chip,
				  struct snd_info_buffer *buffer)
{
	struct xonar_wm87x6 *data = chip->model_data;
	unsigned int i;

	dump_wm8776_registers(chip, buffer);
	snd_iprintf(buffer, "\nWM8766:\n00:");
	for (i = 0; i < 0x10; ++i)
		snd_iprintf(buffer, " %03x", data->wm8766_regs[i]);
	snd_iprintf(buffer, "\n");
}

static const struct oxygen_model model_xonar_ds = {
	.shortname = "Xonar DS",
	.longname = "Asus Virtuoso 66",
	.chip = "AV200",
	.init = xonar_ds_init,
	.mixer_init = xonar_ds_mixer_init,
	.cleanup = xonar_ds_cleanup,
	.suspend = xonar_ds_suspend,
	.resume = xonar_ds_resume,
	.pcm_hardware_filter = wm8776_adc_hardware_filter,
	.set_dac_params = set_wm87x6_dac_params,
	.set_adc_params = set_wm8776_adc_params,
	.update_dac_volume = update_wm87x6_volume,
	.update_dac_mute = update_wm87x6_mute,
	.update_center_lfe_mix = update_wm8766_center_lfe_mix,
	.gpio_changed = xonar_ds_gpio_changed,
	.dump_registers = dump_wm87x6_registers,
	.dac_tlv = wm87x6_dac_db_scale,
	.model_data_size = sizeof(struct xonar_wm87x6),
	.device_config = PLAYBACK_0_TO_I2S |
			 PLAYBACK_1_TO_SPDIF |
			 CAPTURE_0_FROM_I2S_1,
	.dac_channels_pcm = 8,
	.dac_channels_mixer = 8,
	.dac_volume_min = 255 - 2*60,
	.dac_volume_max = 255,
	.function_flags = OXYGEN_FUNCTION_SPI,
	.dac_mclks = OXYGEN_MCLKS(256, 256, 128),
	.adc_mclks = OXYGEN_MCLKS(256, 256, 128),
	.dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
	.adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
};

static const struct oxygen_model model_xonar_hdav_slim = {
	.shortname = "Xonar HDAV1.3 Slim",
	.longname = "Asus Virtuoso 200",
	.chip = "AV200",
	.init = xonar_hdav_slim_init,
	.mixer_init = xonar_hdav_slim_mixer_init,
	.cleanup = xonar_hdav_slim_cleanup,
	.suspend = xonar_hdav_slim_suspend,
	.resume = xonar_hdav_slim_resume,
	.pcm_hardware_filter = xonar_hdav_slim_hardware_filter,
	.set_dac_params = set_hdav_slim_dac_params,
	.set_adc_params = set_wm8776_adc_params,
	.update_dac_volume = update_wm8776_volume,
	.update_dac_mute = update_wm8776_mute,
	.uart_input = xonar_hdmi_uart_input,
	.dump_registers = dump_wm8776_registers,
	.dac_tlv = wm87x6_dac_db_scale,
	.model_data_size = sizeof(struct xonar_wm87x6),
	.device_config = PLAYBACK_0_TO_I2S |
			 PLAYBACK_1_TO_SPDIF |
			 CAPTURE_0_FROM_I2S_1,
	.dac_channels_pcm = 8,
	.dac_channels_mixer = 2,
	.dac_volume_min = 255 - 2*60,
	.dac_volume_max = 255,
	.function_flags = OXYGEN_FUNCTION_2WIRE,
	.dac_mclks = OXYGEN_MCLKS(256, 256, 128),
	.adc_mclks = OXYGEN_MCLKS(256, 256, 128),
	.dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
	.adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
};

int __devinit get_xonar_wm87x6_model(struct oxygen *chip,
				     const struct pci_device_id *id)
{
	switch (id->subdevice) {
	case 0x838e:
		chip->model = model_xonar_ds;
		break;
	case 0x835e:
		chip->model = model_xonar_hdav_slim;
		break;
	default:
		return -EINVAL;
	}
	return 0;
}