1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * HD audio codec driver for Senary HDA audio codec 4 * 5 * Initially based on 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 "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 bool dynamic_eapd; 29 hda_nid_t mute_led_eapd; 30 31 unsigned int parse_flags; /* flag for snd_hda_parse_pin_defcfg() */ 32 33 int mute_led_polarity; 34 unsigned int gpio_led; 35 unsigned int gpio_mute_led_mask; 36 unsigned int gpio_mic_led_mask; 37 }; 38 39 enum { 40 SENARY_FIXUP_PINCFG_DEFAULT, 41 }; 42 43 static const struct hda_pintbl senary_pincfg_default[] = { 44 { 0x16, 0x02211020 }, /* Headphone */ 45 { 0x17, 0x40f001f0 }, /* Not used */ 46 { 0x18, 0x05a1904d }, /* Mic */ 47 { 0x19, 0x02a1104e }, /* Headset Mic */ 48 { 0x1a, 0x01819030 }, /* Line-in */ 49 { 0x1d, 0x01014010 }, /* Line-out */ 50 {} 51 }; 52 53 static const struct hda_fixup senary_fixups[] = { 54 [SENARY_FIXUP_PINCFG_DEFAULT] = { 55 .type = HDA_FIXUP_PINS, 56 .v.pins = senary_pincfg_default, 57 }, 58 }; 59 60 /* Quirk table for specific machines can be added here */ 61 static const struct hda_quirk sn6186_fixups[] = { 62 {} 63 }; 64 65 #ifdef CONFIG_SND_HDA_INPUT_BEEP 66 /* additional beep mixers; private_value will be overwritten */ 67 static const struct snd_kcontrol_new senary_beep_mixer[] = { 68 HDA_CODEC_VOLUME_MONO("Beep Playback Volume", 0, 1, 0, HDA_OUTPUT), 69 HDA_CODEC_MUTE_BEEP_MONO("Beep Playback Switch", 0, 1, 0, HDA_OUTPUT), 70 }; 71 72 static int set_beep_amp(struct senary_spec *spec, hda_nid_t nid, 73 int idx, int dir) 74 { 75 struct snd_kcontrol_new *knew; 76 unsigned int beep_amp = HDA_COMPOSE_AMP_VAL(nid, 1, idx, dir); 77 int i; 78 79 for (i = 0; i < ARRAY_SIZE(senary_beep_mixer); i++) { 80 knew = snd_hda_gen_add_kctl(&spec->gen, NULL, 81 &senary_beep_mixer[i]); 82 if (!knew) 83 return -ENOMEM; 84 knew->private_value = beep_amp; 85 } 86 87 spec->gen.beep_nid = nid; 88 return 0; 89 } 90 91 static int senary_auto_parse_beep(struct hda_codec *codec) 92 { 93 struct senary_spec *spec = codec->spec; 94 hda_nid_t nid; 95 96 for_each_hda_codec_node(nid, codec) 97 if ((get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_BEEP) && 98 (get_wcaps(codec, nid) & (AC_WCAP_OUT_AMP | AC_WCAP_AMP_OVRD))) 99 return set_beep_amp(spec, nid, 0, HDA_OUTPUT); 100 return 0; 101 } 102 #else 103 #define senary_auto_parse_beep(codec) 0 104 #endif 105 106 /* parse EAPDs */ 107 static void senary_auto_parse_eapd(struct hda_codec *codec) 108 { 109 struct senary_spec *spec = codec->spec; 110 hda_nid_t nid; 111 112 for_each_hda_codec_node(nid, codec) { 113 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) 114 continue; 115 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) 116 continue; 117 spec->eapds[spec->num_eapds++] = nid; 118 if (spec->num_eapds >= ARRAY_SIZE(spec->eapds)) 119 break; 120 } 121 } 122 123 /* Hardware specific initialization verbs */ 124 static void senary_init_verb(struct hda_codec *codec) 125 { 126 /* Vendor specific init sequence */ 127 snd_hda_codec_write(codec, 0x1b, 0x0, 0x05a, 0xaa); 128 snd_hda_codec_write(codec, 0x1b, 0x0, 0x059, 0x48); 129 snd_hda_codec_write(codec, 0x1b, 0x0, 0x01b, 0x00); 130 snd_hda_codec_write(codec, 0x1b, 0x0, 0x01c, 0x00); 131 132 /* Override pin caps for headset mic */ 133 snd_hda_override_pin_caps(codec, 0x19, 0x2124); 134 } 135 136 static void senary_auto_turn_eapd(struct hda_codec *codec, int num_pins, 137 const hda_nid_t *pins, bool on) 138 { 139 int i; 140 141 for (i = 0; i < num_pins; i++) { 142 snd_hda_codec_write(codec, pins[i], 0, 143 AC_VERB_SET_EAPD_BTLENABLE, 144 on ? 0x02 : 0); 145 } 146 } 147 148 /* turn on/off EAPD according to Master switch */ 149 static void senary_auto_vmaster_hook(void *private_data, int enabled) 150 { 151 struct hda_codec *codec = private_data; 152 struct senary_spec *spec = codec->spec; 153 154 senary_auto_turn_eapd(codec, spec->num_eapds, spec->eapds, enabled); 155 } 156 157 static void senary_init_gpio_led(struct hda_codec *codec) 158 { 159 struct senary_spec *spec = codec->spec; 160 unsigned int mask = spec->gpio_mute_led_mask | spec->gpio_mic_led_mask; 161 162 if (mask) 163 snd_hda_codec_set_gpio(codec, mask, mask, spec->gpio_led, 0); 164 } 165 166 static int senary_init(struct hda_codec *codec) 167 { 168 struct senary_spec *spec = codec->spec; 169 170 snd_hda_gen_init(codec); 171 senary_init_gpio_led(codec); 172 senary_init_verb(codec); 173 if (!spec->dynamic_eapd) 174 senary_auto_turn_eapd(codec, spec->num_eapds, spec->eapds, true); 175 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_INIT); 176 177 return 0; 178 } 179 180 static void senary_shutdown(struct hda_codec *codec) 181 { 182 struct senary_spec *spec = codec->spec; 183 184 /* Turn the problematic codec into D3 to avoid spurious noises 185 * from the internal speaker during (and after) reboot 186 */ 187 senary_auto_turn_eapd(codec, spec->num_eapds, spec->eapds, false); 188 } 189 190 static void senary_remove(struct hda_codec *codec) 191 { 192 senary_shutdown(codec); 193 snd_hda_gen_remove(codec); 194 } 195 196 static int senary_suspend(struct hda_codec *codec) 197 { 198 senary_shutdown(codec); 199 return 0; 200 } 201 202 static int senary_probe(struct hda_codec *codec, const struct hda_device_id *id) 203 { 204 struct senary_spec *spec; 205 int err; 206 207 codec_info(codec, "%s: BIOS auto-probing.\n", codec->core.chip_name); 208 209 spec = kzalloc_obj(*spec); 210 if (!spec) 211 return -ENOMEM; 212 snd_hda_gen_spec_init(&spec->gen); 213 codec->spec = spec; 214 215 senary_auto_parse_eapd(codec); 216 spec->gen.own_eapd_ctl = 1; 217 218 /* Setup fixups based on codec vendor ID */ 219 switch (codec->core.vendor_id) { 220 case 0x1fa86186: 221 codec->pin_amp_workaround = 1; 222 spec->gen.mixer_nid = 0x15; 223 snd_hda_pick_fixup(codec, NULL, sn6186_fixups, senary_fixups); 224 225 /* If no specific quirk found, apply the default pin configuration */ 226 if (codec->fixup_id == HDA_FIXUP_ID_NOT_SET) 227 codec->fixup_id = SENARY_FIXUP_PINCFG_DEFAULT; 228 break; 229 default: 230 snd_hda_pick_fixup(codec, NULL, sn6186_fixups, senary_fixups); 231 break; 232 } 233 234 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE); 235 236 /* Run hardware init verbs once during probe */ 237 senary_init_verb(codec); 238 239 if (!spec->gen.vmaster_mute.hook) 240 spec->gen.vmaster_mute.hook = senary_auto_vmaster_hook; 241 242 err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 243 spec->parse_flags); 244 if (err < 0) 245 goto error; 246 247 err = senary_auto_parse_beep(codec); 248 if (err < 0) 249 goto error; 250 251 err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg); 252 if (err < 0) 253 goto error; 254 255 /* Some laptops with Senary chips show stalls in S3 resume, 256 * which falls into the single-cmd mode. 257 * Better to make reset, then. 258 */ 259 if (!codec->bus->core.sync_write) { 260 codec_info(codec, 261 "Enable sync_write for stable communication\n"); 262 codec->bus->core.sync_write = 1; 263 codec->bus->allow_bus_reset = 1; 264 } 265 266 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE); 267 268 return 0; 269 270 error: 271 senary_remove(codec); 272 return err; 273 } 274 275 static const struct hda_codec_ops senary_codec_ops = { 276 .probe = senary_probe, 277 .remove = senary_remove, 278 .build_controls = snd_hda_gen_build_controls, 279 .build_pcms = snd_hda_gen_build_pcms, 280 .init = senary_init, 281 .unsol_event = snd_hda_jack_unsol_event, 282 .suspend = senary_suspend, 283 .check_power_status = snd_hda_gen_check_power_status, 284 .stream_pm = snd_hda_gen_stream_pm, 285 }; 286 287 /* 288 */ 289 290 static const struct hda_device_id snd_hda_id_senary[] = { 291 HDA_CODEC_ID(0x1fa86186, "SN6186"), 292 {} /* terminator */ 293 }; 294 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_senary); 295 296 MODULE_LICENSE("GPL"); 297 MODULE_DESCRIPTION("Senarytech HD-audio codec"); 298 299 static struct hda_codec_driver senary_driver = { 300 .id = snd_hda_id_senary, 301 .ops = &senary_codec_ops, 302 }; 303 304 module_hda_codec_driver(senary_driver); 305