/
patch_via.c
6311 lines (5506 loc) · 175 KB
/
patch_via.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Universal Interface for Intel High Definition Audio Codec
*
* HD audio interface patch for VIA VT17xx/VT18xx/VT20xx codec
*
* (C) 2006-2009 VIA Technology, Inc.
* (C) 2006-2008 Takashi Iwai <tiwai@suse.de>
*
* This driver is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This 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 program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* * * * * * * * * * * * * * Release History * * * * * * * * * * * * * * * * */
/* */
/* 2006-03-03 Lydia Wang Create the basic patch to support VT1708 codec */
/* 2006-03-14 Lydia Wang Modify hard code for some pin widget nid */
/* 2006-08-02 Lydia Wang Add support to VT1709 codec */
/* 2006-09-08 Lydia Wang Fix internal loopback recording source select bug */
/* 2007-09-12 Lydia Wang Add EAPD enable during driver initialization */
/* 2007-09-17 Lydia Wang Add VT1708B codec support */
/* 2007-11-14 Lydia Wang Add VT1708A codec HP and CD pin connect config */
/* 2008-02-03 Lydia Wang Fix Rear channels and Back channels inverse issue */
/* 2008-03-06 Lydia Wang Add VT1702 codec and VT1708S codec support */
/* 2008-04-09 Lydia Wang Add mute front speaker when HP plugin */
/* 2008-04-09 Lydia Wang Add Independent HP feature */
/* 2008-05-28 Lydia Wang Add second S/PDIF Out support for VT1702 */
/* 2008-09-15 Logan Li Add VT1708S Mic Boost workaround/backdoor */
/* 2009-02-16 Logan Li Add support for VT1718S */
/* 2009-03-13 Logan Li Add support for VT1716S */
/* 2009-04-14 Lydai Wang Add support for VT1828S and VT2020 */
/* 2009-07-08 Lydia Wang Add support for VT2002P */
/* 2009-07-21 Lydia Wang Add support for VT1812 */
/* 2009-09-19 Lydia Wang Add support for VT1818S */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <sound/core.h>
#include <sound/asoundef.h>
#include "hda_codec.h"
#include "hda_local.h"
#define NID_MAPPING (-1)
/* amp values */
#define AMP_VAL_IDX_SHIFT 19
#define AMP_VAL_IDX_MASK (0x0f<<19)
/* Pin Widget NID */
#define VT1708_HP_NID 0x13
#define VT1708_DIGOUT_NID 0x14
#define VT1708_DIGIN_NID 0x16
#define VT1708_DIGIN_PIN 0x26
#define VT1708_HP_PIN_NID 0x20
#define VT1708_CD_PIN_NID 0x24
#define VT1709_HP_DAC_NID 0x28
#define VT1709_DIGOUT_NID 0x13
#define VT1709_DIGIN_NID 0x17
#define VT1709_DIGIN_PIN 0x25
#define VT1708B_HP_NID 0x25
#define VT1708B_DIGOUT_NID 0x12
#define VT1708B_DIGIN_NID 0x15
#define VT1708B_DIGIN_PIN 0x21
#define VT1708S_HP_NID 0x25
#define VT1708S_DIGOUT_NID 0x12
#define VT1702_HP_NID 0x17
#define VT1702_DIGOUT_NID 0x11
enum VIA_HDA_CODEC {
UNKNOWN = -1,
VT1708,
VT1709_10CH,
VT1709_6CH,
VT1708B_8CH,
VT1708B_4CH,
VT1708S,
VT1708BCE,
VT1702,
VT1718S,
VT1716S,
VT2002P,
VT1812,
VT1802,
CODEC_TYPES,
};
#define VT2002P_COMPATIBLE(spec) \
((spec)->codec_type == VT2002P ||\
(spec)->codec_type == VT1812 ||\
(spec)->codec_type == VT1802)
struct via_spec {
/* codec parameterization */
const struct snd_kcontrol_new *mixers[6];
unsigned int num_mixers;
const struct hda_verb *init_verbs[5];
unsigned int num_iverbs;
char *stream_name_analog;
const struct hda_pcm_stream *stream_analog_playback;
const struct hda_pcm_stream *stream_analog_capture;
char *stream_name_digital;
const struct hda_pcm_stream *stream_digital_playback;
const struct hda_pcm_stream *stream_digital_capture;
/* playback */
struct hda_multi_out multiout;
hda_nid_t slave_dig_outs[2];
/* capture */
unsigned int num_adc_nids;
const hda_nid_t *adc_nids;
hda_nid_t mux_nids[3];
hda_nid_t dig_in_nid;
hda_nid_t dig_in_pin;
/* capture source */
const struct hda_input_mux *input_mux;
unsigned int cur_mux[3];
/* PCM information */
struct hda_pcm pcm_rec[3];
/* dynamic controls, init_verbs and input_mux */
struct auto_pin_cfg autocfg;
struct snd_array kctls;
struct hda_input_mux private_imux[2];
hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
/* HP mode source */
const struct hda_input_mux *hp_mux;
unsigned int hp_independent_mode;
unsigned int hp_independent_mode_index;
unsigned int smart51_enabled;
unsigned int dmic_enabled;
enum VIA_HDA_CODEC codec_type;
/* work to check hp jack state */
struct hda_codec *codec;
struct delayed_work vt1708_hp_work;
int vt1708_jack_detectect;
int vt1708_hp_present;
void (*set_widgets_power_state)(struct hda_codec *codec);
#ifdef CONFIG_SND_HDA_POWER_SAVE
struct hda_loopback_check loopback;
#endif
};
static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
static struct via_spec * via_new_spec(struct hda_codec *codec)
{
struct via_spec *spec;
spec = kzalloc(sizeof(*spec), GFP_KERNEL);
if (spec == NULL)
return NULL;
codec->spec = spec;
spec->codec = codec;
spec->codec_type = get_codec_type(codec);
/* VT1708BCE & VT1708S are almost same */
if (spec->codec_type == VT1708BCE)
spec->codec_type = VT1708S;
return spec;
}
static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
{
u32 vendor_id = codec->vendor_id;
u16 ven_id = vendor_id >> 16;
u16 dev_id = vendor_id & 0xffff;
enum VIA_HDA_CODEC codec_type;
/* get codec type */
if (ven_id != 0x1106)
codec_type = UNKNOWN;
else if (dev_id >= 0x1708 && dev_id <= 0x170b)
codec_type = VT1708;
else if (dev_id >= 0xe710 && dev_id <= 0xe713)
codec_type = VT1709_10CH;
else if (dev_id >= 0xe714 && dev_id <= 0xe717)
codec_type = VT1709_6CH;
else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
codec_type = VT1708B_8CH;
if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
codec_type = VT1708BCE;
} else if (dev_id >= 0xe724 && dev_id <= 0xe727)
codec_type = VT1708B_4CH;
else if ((dev_id & 0xfff) == 0x397
&& (dev_id >> 12) < 8)
codec_type = VT1708S;
else if ((dev_id & 0xfff) == 0x398
&& (dev_id >> 12) < 8)
codec_type = VT1702;
else if ((dev_id & 0xfff) == 0x428
&& (dev_id >> 12) < 8)
codec_type = VT1718S;
else if (dev_id == 0x0433 || dev_id == 0xa721)
codec_type = VT1716S;
else if (dev_id == 0x0441 || dev_id == 0x4441)
codec_type = VT1718S;
else if (dev_id == 0x0438 || dev_id == 0x4438)
codec_type = VT2002P;
else if (dev_id == 0x0448)
codec_type = VT1812;
else if (dev_id == 0x0440)
codec_type = VT1708S;
else if ((dev_id & 0xfff) == 0x446)
codec_type = VT1802;
else
codec_type = UNKNOWN;
return codec_type;
};
#define VIA_JACK_EVENT 0x20
#define VIA_HP_EVENT 0x01
#define VIA_GPIO_EVENT 0x02
#define VIA_MONO_EVENT 0x03
#define VIA_SPEAKER_EVENT 0x04
#define VIA_BIND_HP_EVENT 0x05
enum {
VIA_CTL_WIDGET_VOL,
VIA_CTL_WIDGET_MUTE,
VIA_CTL_WIDGET_ANALOG_MUTE,
VIA_CTL_WIDGET_BIND_PIN_MUTE,
};
enum {
AUTO_SEQ_FRONT = 0,
AUTO_SEQ_SURROUND,
AUTO_SEQ_CENLFE,
AUTO_SEQ_SIDE
};
static void analog_low_current_mode(struct hda_codec *codec, int stream_idle);
static int is_aa_path_mute(struct hda_codec *codec);
static void vt1708_start_hp_work(struct via_spec *spec)
{
if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
return;
snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
!spec->vt1708_jack_detectect);
if (!delayed_work_pending(&spec->vt1708_hp_work))
schedule_delayed_work(&spec->vt1708_hp_work,
msecs_to_jiffies(100));
}
static void vt1708_stop_hp_work(struct via_spec *spec)
{
if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
return;
if (snd_hda_get_bool_hint(spec->codec, "analog_loopback_hp_detect") == 1
&& !is_aa_path_mute(spec->codec))
return;
snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81,
!spec->vt1708_jack_detectect);
cancel_delayed_work_sync(&spec->vt1708_hp_work);
}
static void set_widgets_power_state(struct hda_codec *codec)
{
struct via_spec *spec = codec->spec;
if (spec->set_widgets_power_state)
spec->set_widgets_power_state(codec);
}
static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
set_widgets_power_state(codec);
analog_low_current_mode(snd_kcontrol_chip(kcontrol), -1);
if (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1) {
if (is_aa_path_mute(codec))
vt1708_start_hp_work(codec->spec);
else
vt1708_stop_hp_work(codec->spec);
}
return change;
}
/* modify .put = snd_hda_mixer_amp_switch_put */
#define ANALOG_INPUT_MUTE \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
.name = NULL, \
.index = 0, \
.info = snd_hda_mixer_amp_switch_info, \
.get = snd_hda_mixer_amp_switch_get, \
.put = analog_input_switch_put, \
.private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
static void via_hp_bind_automute(struct hda_codec *codec);
static int bind_pin_switch_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
struct via_spec *spec = codec->spec;
int i;
int change = 0;
long *valp = ucontrol->value.integer.value;
int lmute, rmute;
if (strstr(kcontrol->id.name, "Switch") == NULL) {
snd_printd("Invalid control!\n");
return change;
}
change = snd_hda_mixer_amp_switch_put(kcontrol,
ucontrol);
/* Get mute value */
lmute = *valp ? 0 : HDA_AMP_MUTE;
valp++;
rmute = *valp ? 0 : HDA_AMP_MUTE;
/* Set hp pins */
if (!spec->hp_independent_mode) {
for (i = 0; i < spec->autocfg.hp_outs; i++) {
snd_hda_codec_amp_update(
codec, spec->autocfg.hp_pins[i],
0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
lmute);
snd_hda_codec_amp_update(
codec, spec->autocfg.hp_pins[i],
1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
rmute);
}
}
if (!lmute && !rmute) {
/* Line Outs */
for (i = 0; i < spec->autocfg.line_outs; i++)
snd_hda_codec_amp_stereo(
codec, spec->autocfg.line_out_pins[i],
HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
/* Speakers */
for (i = 0; i < spec->autocfg.speaker_outs; i++)
snd_hda_codec_amp_stereo(
codec, spec->autocfg.speaker_pins[i],
HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
/* unmute */
via_hp_bind_automute(codec);
} else {
if (lmute) {
/* Mute all left channels */
for (i = 1; i < spec->autocfg.line_outs; i++)
snd_hda_codec_amp_update(
codec,
spec->autocfg.line_out_pins[i],
0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
lmute);
for (i = 0; i < spec->autocfg.speaker_outs; i++)
snd_hda_codec_amp_update(
codec,
spec->autocfg.speaker_pins[i],
0, HDA_OUTPUT, 0, HDA_AMP_MUTE,
lmute);
}
if (rmute) {
/* mute all right channels */
for (i = 1; i < spec->autocfg.line_outs; i++)
snd_hda_codec_amp_update(
codec,
spec->autocfg.line_out_pins[i],
1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
rmute);
for (i = 0; i < spec->autocfg.speaker_outs; i++)
snd_hda_codec_amp_update(
codec,
spec->autocfg.speaker_pins[i],
1, HDA_OUTPUT, 0, HDA_AMP_MUTE,
rmute);
}
}
return change;
}
#define BIND_PIN_MUTE \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
.name = NULL, \
.index = 0, \
.info = snd_hda_mixer_amp_switch_info, \
.get = snd_hda_mixer_amp_switch_get, \
.put = bind_pin_switch_put, \
.private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
static const struct snd_kcontrol_new via_control_templates[] = {
HDA_CODEC_VOLUME(NULL, 0, 0, 0),
HDA_CODEC_MUTE(NULL, 0, 0, 0),
ANALOG_INPUT_MUTE,
BIND_PIN_MUTE,
};
static const hda_nid_t vt1708_adc_nids[2] = {
/* ADC1-2 */
0x15, 0x27
};
static const hda_nid_t vt1709_adc_nids[3] = {
/* ADC1-2 */
0x14, 0x15, 0x16
};
static const hda_nid_t vt1708B_adc_nids[2] = {
/* ADC1-2 */
0x13, 0x14
};
static const hda_nid_t vt1708S_adc_nids[2] = {
/* ADC1-2 */
0x13, 0x14
};
static const hda_nid_t vt1702_adc_nids[3] = {
/* ADC1-2 */
0x12, 0x20, 0x1F
};
static const hda_nid_t vt1718S_adc_nids[2] = {
/* ADC1-2 */
0x10, 0x11
};
static const hda_nid_t vt1716S_adc_nids[2] = {
/* ADC1-2 */
0x13, 0x14
};
static const hda_nid_t vt2002P_adc_nids[2] = {
/* ADC1-2 */
0x10, 0x11
};
static const hda_nid_t vt1812_adc_nids[2] = {
/* ADC1-2 */
0x10, 0x11
};
/* add dynamic controls */
static int __via_add_control(struct via_spec *spec, int type, const char *name,
int idx, unsigned long val)
{
struct snd_kcontrol_new *knew;
snd_array_init(&spec->kctls, sizeof(*knew), 32);
knew = snd_array_new(&spec->kctls);
if (!knew)
return -ENOMEM;
*knew = via_control_templates[type];
knew->name = kstrdup(name, GFP_KERNEL);
if (!knew->name)
return -ENOMEM;
if (get_amp_nid_(val))
knew->subdevice = HDA_SUBDEV_AMP_FLAG;
knew->private_value = val;
return 0;
}
#define via_add_control(spec, type, name, val) \
__via_add_control(spec, type, name, 0, val)
static struct snd_kcontrol_new *via_clone_control(struct via_spec *spec,
const struct snd_kcontrol_new *tmpl)
{
struct snd_kcontrol_new *knew;
snd_array_init(&spec->kctls, sizeof(*knew), 32);
knew = snd_array_new(&spec->kctls);
if (!knew)
return NULL;
*knew = *tmpl;
knew->name = kstrdup(tmpl->name, GFP_KERNEL);
if (!knew->name)
return NULL;
return knew;
}
static void via_free_kctls(struct hda_codec *codec)
{
struct via_spec *spec = codec->spec;
if (spec->kctls.list) {
struct snd_kcontrol_new *kctl = spec->kctls.list;
int i;
for (i = 0; i < spec->kctls.used; i++)
kfree(kctl[i].name);
}
snd_array_free(&spec->kctls);
}
/* create input playback/capture controls for the given pin */
static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
int type_idx, int idx, int mix_nid)
{
char name[32];
int err;
sprintf(name, "%s Playback Volume", ctlname);
err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
if (err < 0)
return err;
sprintf(name, "%s Playback Switch", ctlname);
err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
if (err < 0)
return err;
return 0;
}
static void via_auto_set_output_and_unmute(struct hda_codec *codec,
hda_nid_t nid, int pin_type,
int dac_idx)
{
/* set as output */
snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
pin_type);
snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
AMP_OUT_UNMUTE);
if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
snd_hda_codec_write(codec, nid, 0,
AC_VERB_SET_EAPD_BTLENABLE, 0x02);
}
static void via_auto_init_multi_out(struct hda_codec *codec)
{
struct via_spec *spec = codec->spec;
int i;
for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
hda_nid_t nid = spec->autocfg.line_out_pins[i];
if (nid)
via_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
}
}
static void via_auto_init_hp_out(struct hda_codec *codec)
{
struct via_spec *spec = codec->spec;
hda_nid_t pin;
int i;
for (i = 0; i < spec->autocfg.hp_outs; i++) {
pin = spec->autocfg.hp_pins[i];
if (pin) /* connect to front */
via_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
}
}
static int is_smart51_pins(struct via_spec *spec, hda_nid_t pin);
static void via_auto_init_analog_input(struct hda_codec *codec)
{
struct via_spec *spec = codec->spec;
const struct auto_pin_cfg *cfg = &spec->autocfg;
unsigned int ctl;
int i;
for (i = 0; i < cfg->num_inputs; i++) {
hda_nid_t nid = cfg->inputs[i].pin;
if (spec->smart51_enabled && is_smart51_pins(spec, nid))
ctl = PIN_OUT;
else if (cfg->inputs[i].type == AUTO_PIN_MIC)
ctl = PIN_VREF50;
else
ctl = PIN_IN;
snd_hda_codec_write(codec, nid, 0,
AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
}
}
static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
unsigned int *affected_parm)
{
unsigned parm;
unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
>> AC_DEFCFG_MISC_SHIFT
& AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
unsigned present = snd_hda_jack_detect(codec, nid);
struct via_spec *spec = codec->spec;
if ((spec->smart51_enabled && is_smart51_pins(spec, nid))
|| ((no_presence || present)
&& get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
*affected_parm = AC_PWRST_D0; /* if it's connected */
parm = AC_PWRST_D0;
} else
parm = AC_PWRST_D3;
snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
}
/*
* input MUX handling
*/
static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_info *uinfo)
{
struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
struct via_spec *spec = codec->spec;
return snd_hda_input_mux_info(spec->input_mux, uinfo);
}
static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
struct via_spec *spec = codec->spec;
unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
return 0;
}
static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
struct via_spec *spec = codec->spec;
unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
int ret;
if (!spec->mux_nids[adc_idx])
return -EINVAL;
/* switch to D0 beofre change index */
if (snd_hda_codec_read(codec, spec->mux_nids[adc_idx], 0,
AC_VERB_GET_POWER_STATE, 0x00) != AC_PWRST_D0)
snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
ret = snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
spec->mux_nids[adc_idx],
&spec->cur_mux[adc_idx]);
/* update jack power state */
set_widgets_power_state(codec);
return ret;
}
static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_info *uinfo)
{
struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
struct via_spec *spec = codec->spec;
return snd_hda_input_mux_info(spec->hp_mux, uinfo);
}
static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
hda_nid_t nid = kcontrol->private_value;
unsigned int pinsel;
/* use !! to translate conn sel 2 for VT1718S */
pinsel = !!snd_hda_codec_read(codec, nid, 0,
AC_VERB_GET_CONNECT_SEL,
0x00);
ucontrol->value.enumerated.item[0] = pinsel;
return 0;
}
static void activate_ctl(struct hda_codec *codec, const char *name, int active)
{
struct snd_kcontrol *ctl = snd_hda_find_mixer_ctl(codec, name);
if (ctl) {
ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
ctl->vd[0].access |= active
? 0 : SNDRV_CTL_ELEM_ACCESS_INACTIVE;
snd_ctl_notify(codec->bus->card,
SNDRV_CTL_EVENT_MASK_VALUE, &ctl->id);
}
}
static hda_nid_t side_mute_channel(struct via_spec *spec)
{
switch (spec->codec_type) {
case VT1708: return 0x1b;
case VT1709_10CH: return 0x29;
case VT1708B_8CH: /* fall thru */
case VT1708S: return 0x27;
case VT2002P: return 0x19;
case VT1802: return 0x15;
case VT1812: return 0x15;
default: return 0;
}
}
static int update_side_mute_status(struct hda_codec *codec)
{
/* mute side channel */
struct via_spec *spec = codec->spec;
unsigned int parm;
hda_nid_t sw3 = side_mute_channel(spec);
if (sw3) {
if (VT2002P_COMPATIBLE(spec))
parm = spec->hp_independent_mode ?
AMP_IN_MUTE(1) : AMP_IN_UNMUTE(1);
else
parm = spec->hp_independent_mode ?
AMP_OUT_MUTE : AMP_OUT_UNMUTE;
snd_hda_codec_write(codec, sw3, 0,
AC_VERB_SET_AMP_GAIN_MUTE, parm);
if (spec->codec_type == VT1812)
snd_hda_codec_write(codec, 0x1d, 0,
AC_VERB_SET_AMP_GAIN_MUTE, parm);
}
return 0;
}
static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
struct via_spec *spec = codec->spec;
hda_nid_t nid = kcontrol->private_value;
unsigned int pinsel = ucontrol->value.enumerated.item[0];
/* Get Independent Mode index of headphone pin widget */
spec->hp_independent_mode = spec->hp_independent_mode_index == pinsel
? 1 : 0;
if (spec->codec_type == VT1718S)
snd_hda_codec_write(codec, nid, 0,
AC_VERB_SET_CONNECT_SEL, pinsel ? 2 : 0);
else
snd_hda_codec_write(codec, nid, 0,
AC_VERB_SET_CONNECT_SEL, pinsel);
if (spec->codec_type == VT1812)
snd_hda_codec_write(codec, 0x35, 0,
AC_VERB_SET_CONNECT_SEL, pinsel);
if (spec->multiout.hp_nid && spec->multiout.hp_nid
!= spec->multiout.dac_nids[HDA_FRONT])
snd_hda_codec_setup_stream(codec, spec->multiout.hp_nid,
0, 0, 0);
update_side_mute_status(codec);
/* update HP volume/swtich active state */
if (spec->codec_type == VT1708S
|| spec->codec_type == VT1702
|| spec->codec_type == VT1718S
|| spec->codec_type == VT1716S
|| VT2002P_COMPATIBLE(spec)) {
activate_ctl(codec, "Headphone Playback Volume",
spec->hp_independent_mode);
activate_ctl(codec, "Headphone Playback Switch",
spec->hp_independent_mode);
}
/* update jack power state */
set_widgets_power_state(codec);
return 0;
}
static const struct snd_kcontrol_new via_hp_mixer[2] = {
{
.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
.name = "Independent HP",
.info = via_independent_hp_info,
.get = via_independent_hp_get,
.put = via_independent_hp_put,
},
{
.iface = NID_MAPPING,
.name = "Independent HP",
},
};
static int via_hp_build(struct hda_codec *codec)
{
struct via_spec *spec = codec->spec;
struct snd_kcontrol_new *knew;
hda_nid_t nid;
int nums;
hda_nid_t conn[HDA_MAX_CONNECTIONS];
switch (spec->codec_type) {
case VT1718S:
nid = 0x34;
break;
case VT2002P:
case VT1802:
nid = 0x35;
break;
case VT1812:
nid = 0x3d;
break;
default:
nid = spec->autocfg.hp_pins[0];
break;
}
if (spec->codec_type != VT1708) {
nums = snd_hda_get_connections(codec, nid,
conn, HDA_MAX_CONNECTIONS);
if (nums <= 1)
return 0;
}
knew = via_clone_control(spec, &via_hp_mixer[0]);
if (knew == NULL)
return -ENOMEM;
knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
knew->private_value = nid;
knew = via_clone_control(spec, &via_hp_mixer[1]);
if (knew == NULL)
return -ENOMEM;
knew->subdevice = side_mute_channel(spec);
return 0;
}
static void notify_aa_path_ctls(struct hda_codec *codec)
{
int i;
struct snd_ctl_elem_id id;
const char *labels[] = {"Mic", "Front Mic", "Line", "Rear Mic"};
struct snd_kcontrol *ctl;
memset(&id, 0, sizeof(id));
id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
for (i = 0; i < ARRAY_SIZE(labels); i++) {
sprintf(id.name, "%s Playback Volume", labels[i]);
ctl = snd_hda_find_mixer_ctl(codec, id.name);
if (ctl)
snd_ctl_notify(codec->bus->card,
SNDRV_CTL_EVENT_MASK_VALUE,
&ctl->id);
}
}
static void mute_aa_path(struct hda_codec *codec, int mute)
{
struct via_spec *spec = codec->spec;
hda_nid_t nid_mixer;
int start_idx;
int end_idx;
int i;
/* get nid of MW0 and start & end index */
switch (spec->codec_type) {
case VT1708:
nid_mixer = 0x17;
start_idx = 2;
end_idx = 4;
break;
case VT1709_10CH:
case VT1709_6CH:
nid_mixer = 0x18;
start_idx = 2;
end_idx = 4;
break;
case VT1708B_8CH:
case VT1708B_4CH:
case VT1708S:
case VT1716S:
nid_mixer = 0x16;
start_idx = 2;
end_idx = 4;
break;
case VT1718S:
nid_mixer = 0x21;
start_idx = 1;
end_idx = 3;
break;
default:
return;
}
/* check AA path's mute status */
for (i = start_idx; i <= end_idx; i++) {
int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
snd_hda_codec_amp_stereo(codec, nid_mixer, HDA_INPUT, i,
HDA_AMP_MUTE, val);
}
}
static int is_smart51_pins(struct via_spec *spec, hda_nid_t pin)
{
const struct auto_pin_cfg *cfg = &spec->autocfg;
int i;
for (i = 0; i < cfg->num_inputs; i++) {
if (pin == cfg->inputs[i].pin)
return cfg->inputs[i].type <= AUTO_PIN_LINE_IN;
}
return 0;
}
static int via_smart51_info(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_info *uinfo)
{
uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
uinfo->count = 1;
uinfo->value.integer.min = 0;
uinfo->value.integer.max = 1;
return 0;
}
static int via_smart51_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
struct via_spec *spec = codec->spec;
const struct auto_pin_cfg *cfg = &spec->autocfg;
int on = 1;
int i;
for (i = 0; i < cfg->num_inputs; i++) {
hda_nid_t nid = cfg->inputs[i].pin;
int ctl = snd_hda_codec_read(codec, nid, 0,
AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
continue;
if (cfg->inputs[i].type == AUTO_PIN_MIC &&
spec->hp_independent_mode && spec->codec_type != VT1718S)
continue; /* ignore FMic for independent HP */
if ((ctl & AC_PINCTL_IN_EN) && !(ctl & AC_PINCTL_OUT_EN))
on = 0;
}
*ucontrol->value.integer.value = on;
return 0;
}
static int via_smart51_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
struct via_spec *spec = codec->spec;
const struct auto_pin_cfg *cfg = &spec->autocfg;
int out_in = *ucontrol->value.integer.value
? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
int i;
for (i = 0; i < cfg->num_inputs; i++) {
hda_nid_t nid = cfg->inputs[i].pin;
unsigned int parm;
if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
continue;
if (cfg->inputs[i].type == AUTO_PIN_MIC &&
spec->hp_independent_mode && spec->codec_type != VT1718S)
continue; /* don't retask FMic for independent HP */
parm = snd_hda_codec_read(codec, nid, 0,
AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
parm |= out_in;
snd_hda_codec_write(codec, nid, 0,
AC_VERB_SET_PIN_WIDGET_CONTROL,
parm);
if (out_in == AC_PINCTL_OUT_EN) {
mute_aa_path(codec, 1);
notify_aa_path_ctls(codec);
}
if (spec->codec_type == VT1718S) {
snd_hda_codec_amp_stereo(
codec, nid, HDA_OUTPUT, 0, HDA_AMP_MUTE,
HDA_AMP_UNMUTE);
}
if (cfg->inputs[i].type == AUTO_PIN_MIC) {
if (spec->codec_type == VT1708S
|| spec->codec_type == VT1716S) {
/* input = index 1 (AOW3) */
snd_hda_codec_write(
codec, nid, 0,
AC_VERB_SET_CONNECT_SEL, 1);
snd_hda_codec_amp_stereo(
codec, nid, HDA_OUTPUT,
0, HDA_AMP_MUTE, HDA_AMP_UNMUTE);
}
}
}