xref: /linux/sound/hda/codecs/realtek/alc268.c (revision 68a052239fc4b351e961f698b824f7654a346091)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include "realtek.h"
6 
7 /* bind Beep switches of both NID 0x0f and 0x10 */
8 static int alc268_beep_switch_put(struct snd_kcontrol *kcontrol,
9 				  struct snd_ctl_elem_value *ucontrol)
10 {
11 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
12 	unsigned long pval;
13 	int err;
14 
15 	guard(mutex)(&codec->control_mutex);
16 	pval = kcontrol->private_value;
17 	kcontrol->private_value = (pval & ~0xff) | 0x0f;
18 	err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
19 	if (err >= 0) {
20 		kcontrol->private_value = (pval & ~0xff) | 0x10;
21 		err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
22 	}
23 	kcontrol->private_value = pval;
24 	return err;
25 }
26 
27 static const struct snd_kcontrol_new alc268_beep_mixer[] = {
28 	HDA_CODEC_VOLUME("Beep Playback Volume", 0x1d, 0x0, HDA_INPUT),
29 	{
30 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
31 		.name = "Beep Playback Switch",
32 		.subdevice = HDA_SUBDEV_AMP_FLAG,
33 		.info = snd_hda_mixer_amp_switch_info,
34 		.get = snd_hda_mixer_amp_switch_get,
35 		.put = alc268_beep_switch_put,
36 		.private_value = HDA_COMPOSE_AMP_VAL(0x0f, 3, 1, HDA_INPUT)
37 	},
38 };
39 
40 /* set PCBEEP vol = 0, mute connections */
41 static const struct hda_verb alc268_beep_init_verbs[] = {
42 	{0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
43 	{0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
44 	{0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
45 	{ }
46 };
47 
48 enum {
49 	ALC268_FIXUP_INV_DMIC,
50 	ALC268_FIXUP_HP_EAPD,
51 	ALC268_FIXUP_SPDIF,
52 };
53 
54 static const struct hda_fixup alc268_fixups[] = {
55 	[ALC268_FIXUP_INV_DMIC] = {
56 		.type = HDA_FIXUP_FUNC,
57 		.v.func = alc_fixup_inv_dmic,
58 	},
59 	[ALC268_FIXUP_HP_EAPD] = {
60 		.type = HDA_FIXUP_VERBS,
61 		.v.verbs = (const struct hda_verb[]) {
62 			{0x15, AC_VERB_SET_EAPD_BTLENABLE, 0},
63 			{}
64 		}
65 	},
66 	[ALC268_FIXUP_SPDIF] = {
67 		.type = HDA_FIXUP_PINS,
68 		.v.pins = (const struct hda_pintbl[]) {
69 			{ 0x1e, 0x014b1180 }, /* enable SPDIF out */
70 			{}
71 		}
72 	},
73 };
74 
75 static const struct hda_model_fixup alc268_fixup_models[] = {
76 	{.id = ALC268_FIXUP_INV_DMIC, .name = "inv-dmic"},
77 	{.id = ALC268_FIXUP_HP_EAPD, .name = "hp-eapd"},
78 	{.id = ALC268_FIXUP_SPDIF, .name = "spdif"},
79 	{}
80 };
81 
82 static const struct hda_quirk alc268_fixup_tbl[] = {
83 	SND_PCI_QUIRK(0x1025, 0x0139, "Acer TravelMate 6293", ALC268_FIXUP_SPDIF),
84 	SND_PCI_QUIRK(0x1025, 0x015b, "Acer AOA 150 (ZG5)", ALC268_FIXUP_INV_DMIC),
85 	/* below is codec SSID since multiple Toshiba laptops have the
86 	 * same PCI SSID 1179:ff00
87 	 */
88 	SND_PCI_QUIRK(0x1179, 0xff06, "Toshiba P200", ALC268_FIXUP_HP_EAPD),
89 	{}
90 };
91 
92 /*
93  * BIOS auto configuration
94  */
95 static int alc268_parse_auto_config(struct hda_codec *codec)
96 {
97 	static const hda_nid_t alc268_ssids[] = { 0x15, 0x1b, 0x14, 0 };
98 	return alc_parse_auto_config(codec, NULL, alc268_ssids);
99 }
100 
101 /*
102  */
103 static int alc268_probe(struct hda_codec *codec, const struct hda_device_id *id)
104 {
105 	struct alc_spec *spec;
106 	int i, err;
107 
108 	/* ALC268 has no aa-loopback mixer */
109 	err = alc_alloc_spec(codec, 0);
110 	if (err < 0)
111 		return err;
112 
113 	spec = codec->spec;
114 	if (has_cdefine_beep(codec))
115 		spec->gen.beep_nid = 0x01;
116 
117 	spec->shutup = alc_eapd_shutup;
118 
119 	alc_pre_init(codec);
120 
121 	snd_hda_pick_fixup(codec, alc268_fixup_models, alc268_fixup_tbl, alc268_fixups);
122 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
123 
124 	/* automatic parse from the BIOS config */
125 	err = alc268_parse_auto_config(codec);
126 	if (err < 0)
127 		goto error;
128 
129 	if (err > 0 && !spec->gen.no_analog &&
130 	    spec->gen.autocfg.speaker_pins[0] != 0x1d) {
131 		for (i = 0; i < ARRAY_SIZE(alc268_beep_mixer); i++) {
132 			if (!snd_hda_gen_add_kctl(&spec->gen, NULL,
133 						  &alc268_beep_mixer[i])) {
134 				err = -ENOMEM;
135 				goto error;
136 			}
137 		}
138 		snd_hda_add_verbs(codec, alc268_beep_init_verbs);
139 		if (!query_amp_caps(codec, 0x1d, HDA_INPUT))
140 			/* override the amp caps for beep generator */
141 			snd_hda_override_amp_caps(codec, 0x1d, HDA_INPUT,
142 					  (0x0c << AC_AMPCAP_OFFSET_SHIFT) |
143 					  (0x0c << AC_AMPCAP_NUM_STEPS_SHIFT) |
144 					  (0x07 << AC_AMPCAP_STEP_SIZE_SHIFT) |
145 					  (0 << AC_AMPCAP_MUTE_SHIFT));
146 	}
147 
148 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
149 
150 	return 0;
151 
152  error:
153 	snd_hda_gen_remove(codec);
154 	return err;
155 }
156 
157 static const struct hda_codec_ops alc268_codec_ops = {
158 	.probe = alc268_probe,
159 	.remove = snd_hda_gen_remove,
160 	.build_controls = alc_build_controls,
161 	.build_pcms = snd_hda_gen_build_pcms,
162 	.init = alc_init,
163 	.unsol_event = snd_hda_jack_unsol_event,
164 	.resume = alc_resume,
165 	.suspend = alc_suspend,
166 	.check_power_status = snd_hda_gen_check_power_status,
167 	.stream_pm = snd_hda_gen_stream_pm,
168 };
169 
170 /*
171  * driver entries
172  */
173 static const struct hda_device_id snd_hda_id_alc268[] = {
174 	HDA_CODEC_ID(0x10ec0267, "ALC267"),
175 	HDA_CODEC_ID(0x10ec0268, "ALC268"),
176 	{} /* terminator */
177 };
178 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_alc268);
179 
180 MODULE_LICENSE("GPL");
181 MODULE_DESCRIPTION("Realtek ALC267/268 HD-audio codec");
182 MODULE_IMPORT_NS("SND_HDA_CODEC_REALTEK");
183 
184 static struct hda_codec_driver alc268_driver = {
185 	.id = snd_hda_id_alc268,
186 	.ops = &alc268_codec_ops,
187 };
188 
189 module_hda_codec_driver(alc268_driver);
190