xref: /linux/sound/pci/hda/patch_senarytech.c (revision 4b132aacb0768ac1e652cf517097ea6f237214b9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * HD audio interface patch for Senary HDA audio codec
4  *
5  * Initially based on sound/pci/hda/patch_conexant.c
6  */
7 
8 #include <linux/init.h>
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <sound/core.h>
13 #include <sound/jack.h>
14 
15 #include <sound/hda_codec.h>
16 #include "hda_local.h"
17 #include "hda_auto_parser.h"
18 #include "hda_beep.h"
19 #include "hda_jack.h"
20 #include "hda_generic.h"
21 
22 struct senary_spec {
23 	struct hda_gen_spec gen;
24 
25 	/* extra EAPD pins */
26 	unsigned int num_eapds;
27 	hda_nid_t eapds[4];
28 	hda_nid_t mute_led_eapd;
29 
30 	unsigned int parse_flags; /* flag for snd_hda_parse_pin_defcfg() */
31 
32 	int mute_led_polarity;
33 	unsigned int gpio_led;
34 	unsigned int gpio_mute_led_mask;
35 	unsigned int gpio_mic_led_mask;
36 };
37 
38 #ifdef CONFIG_SND_HDA_INPUT_BEEP
39 /* additional beep mixers; private_value will be overwritten */
40 static const struct snd_kcontrol_new senary_beep_mixer[] = {
41 	HDA_CODEC_VOLUME_MONO("Beep Playback Volume", 0, 1, 0, HDA_OUTPUT),
42 	HDA_CODEC_MUTE_BEEP_MONO("Beep Playback Switch", 0, 1, 0, HDA_OUTPUT),
43 };
44 
45 static int set_beep_amp(struct senary_spec *spec, hda_nid_t nid,
46 			int idx, int dir)
47 {
48 	struct snd_kcontrol_new *knew;
49 	unsigned int beep_amp = HDA_COMPOSE_AMP_VAL(nid, 1, idx, dir);
50 	int i;
51 
52 	spec->gen.beep_nid = nid;
53 	for (i = 0; i < ARRAY_SIZE(senary_beep_mixer); i++) {
54 		knew = snd_hda_gen_add_kctl(&spec->gen, NULL,
55 					    &senary_beep_mixer[i]);
56 		if (!knew)
57 			return -ENOMEM;
58 		knew->private_value = beep_amp;
59 	}
60 	return 0;
61 }
62 
63 static int senary_auto_parse_beep(struct hda_codec *codec)
64 {
65 	struct senary_spec *spec = codec->spec;
66 	hda_nid_t nid;
67 
68 	for_each_hda_codec_node(nid, codec)
69 		if ((get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_BEEP) &&
70 			(get_wcaps(codec, nid) & (AC_WCAP_OUT_AMP | AC_WCAP_AMP_OVRD)))
71 			return set_beep_amp(spec, nid, 0, HDA_OUTPUT);
72 	return 0;
73 }
74 #else
75 #define senary_auto_parse_beep(codec)	0
76 #endif
77 
78 /* parse EAPDs */
79 static void senary_auto_parse_eapd(struct hda_codec *codec)
80 {
81 	struct senary_spec *spec = codec->spec;
82 	hda_nid_t nid;
83 
84 	for_each_hda_codec_node(nid, codec) {
85 		if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
86 			continue;
87 		if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD))
88 			continue;
89 		spec->eapds[spec->num_eapds++] = nid;
90 		if (spec->num_eapds >= ARRAY_SIZE(spec->eapds))
91 			break;
92 	}
93 }
94 
95 static void senary_auto_turn_eapd(struct hda_codec *codec, int num_pins,
96 			      const hda_nid_t *pins, bool on)
97 {
98 	int i;
99 
100 	for (i = 0; i < num_pins; i++) {
101 		if (snd_hda_query_pin_caps(codec, pins[i]) & AC_PINCAP_EAPD)
102 			snd_hda_codec_write(codec, pins[i], 0,
103 					    AC_VERB_SET_EAPD_BTLENABLE,
104 					    on ? 0x02 : 0);
105 	}
106 }
107 
108 /* turn on/off EAPD according to Master switch */
109 static void senary_auto_vmaster_hook(void *private_data, int enabled)
110 {
111 	struct hda_codec *codec = private_data;
112 	struct senary_spec *spec = codec->spec;
113 
114 	senary_auto_turn_eapd(codec, spec->num_eapds, spec->eapds, enabled);
115 }
116 
117 static void senary_init_gpio_led(struct hda_codec *codec)
118 {
119 	struct senary_spec *spec = codec->spec;
120 	unsigned int mask = spec->gpio_mute_led_mask | spec->gpio_mic_led_mask;
121 
122 	if (mask) {
123 		snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
124 				    mask);
125 		snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
126 				    mask);
127 		snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
128 				    spec->gpio_led);
129 	}
130 }
131 
132 static int senary_auto_init(struct hda_codec *codec)
133 {
134 	snd_hda_gen_init(codec);
135 	senary_init_gpio_led(codec);
136 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_INIT);
137 
138 	return 0;
139 }
140 
141 static void senary_auto_shutdown(struct hda_codec *codec)
142 {
143 	struct senary_spec *spec = codec->spec;
144 
145 	/* Turn the problematic codec into D3 to avoid spurious noises
146 	 * from the internal speaker during (and after) reboot
147 	 */
148 	senary_auto_turn_eapd(codec, spec->num_eapds, spec->eapds, false);
149 }
150 
151 static void senary_auto_free(struct hda_codec *codec)
152 {
153 	senary_auto_shutdown(codec);
154 	snd_hda_gen_free(codec);
155 }
156 
157 static int senary_auto_suspend(struct hda_codec *codec)
158 {
159 	senary_auto_shutdown(codec);
160 	return 0;
161 }
162 
163 static const struct hda_codec_ops senary_auto_patch_ops = {
164 	.build_controls = snd_hda_gen_build_controls,
165 	.build_pcms = snd_hda_gen_build_pcms,
166 	.init = senary_auto_init,
167 	.free = senary_auto_free,
168 	.unsol_event = snd_hda_jack_unsol_event,
169 	.suspend = senary_auto_suspend,
170 	.check_power_status = snd_hda_gen_check_power_status,
171 };
172 
173 static int patch_senary_auto(struct hda_codec *codec)
174 {
175 	struct senary_spec *spec;
176 	int err;
177 
178 	codec_info(codec, "%s: BIOS auto-probing.\n", codec->core.chip_name);
179 
180 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
181 	if (!spec)
182 		return -ENOMEM;
183 	snd_hda_gen_spec_init(&spec->gen);
184 	codec->spec = spec;
185 	codec->patch_ops = senary_auto_patch_ops;
186 
187 	senary_auto_parse_eapd(codec);
188 	spec->gen.own_eapd_ctl = 1;
189 
190 	if (!spec->gen.vmaster_mute.hook)
191 		spec->gen.vmaster_mute.hook = senary_auto_vmaster_hook;
192 
193 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
194 
195 	err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL,
196 				       spec->parse_flags);
197 	if (err < 0)
198 		goto error;
199 
200 	err = senary_auto_parse_beep(codec);
201 	if (err < 0)
202 		goto error;
203 
204 	err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
205 	if (err < 0)
206 		goto error;
207 
208 	/* Some laptops with Senary chips show stalls in S3 resume,
209 	 * which falls into the single-cmd mode.
210 	 * Better to make reset, then.
211 	 */
212 	if (!codec->bus->core.sync_write) {
213 		codec_info(codec,
214 			   "Enable sync_write for stable communication\n");
215 		codec->bus->core.sync_write = 1;
216 		codec->bus->allow_bus_reset = 1;
217 	}
218 
219 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
220 
221 	return 0;
222 
223  error:
224 	senary_auto_free(codec);
225 	return err;
226 }
227 
228 /*
229  */
230 
231 static const struct hda_device_id snd_hda_id_senary[] = {
232 	HDA_CODEC_ENTRY(0x1fa86186, "SN6186", patch_senary_auto),
233 	{} /* terminator */
234 };
235 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_senary);
236 
237 MODULE_LICENSE("GPL");
238 MODULE_DESCRIPTION("Senarytech HD-audio codec");
239 
240 static struct hda_codec_driver senary_driver = {
241 	.id = snd_hda_id_senary,
242 };
243 
244 module_hda_codec_driver(senary_driver);
245