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_write(codec, codec->core.afg, 0, AC_VERB_SET_GPIO_MASK, 164 mask); 165 snd_hda_codec_write(codec, codec->core.afg, 0, AC_VERB_SET_GPIO_DIRECTION, 166 mask); 167 snd_hda_codec_write(codec, codec->core.afg, 0, AC_VERB_SET_GPIO_DATA, 168 spec->gpio_led); 169 } 170 } 171 172 static int senary_init(struct hda_codec *codec) 173 { 174 struct senary_spec *spec = codec->spec; 175 176 snd_hda_gen_init(codec); 177 senary_init_gpio_led(codec); 178 senary_init_verb(codec); 179 if (!spec->dynamic_eapd) 180 senary_auto_turn_eapd(codec, spec->num_eapds, spec->eapds, true); 181 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_INIT); 182 183 return 0; 184 } 185 186 static void senary_shutdown(struct hda_codec *codec) 187 { 188 struct senary_spec *spec = codec->spec; 189 190 /* Turn the problematic codec into D3 to avoid spurious noises 191 * from the internal speaker during (and after) reboot 192 */ 193 senary_auto_turn_eapd(codec, spec->num_eapds, spec->eapds, false); 194 } 195 196 static void senary_remove(struct hda_codec *codec) 197 { 198 senary_shutdown(codec); 199 snd_hda_gen_remove(codec); 200 } 201 202 static int senary_suspend(struct hda_codec *codec) 203 { 204 senary_shutdown(codec); 205 return 0; 206 } 207 208 static int senary_probe(struct hda_codec *codec, const struct hda_device_id *id) 209 { 210 struct senary_spec *spec; 211 int err; 212 213 codec_info(codec, "%s: BIOS auto-probing.\n", codec->core.chip_name); 214 215 spec = kzalloc_obj(*spec); 216 if (!spec) 217 return -ENOMEM; 218 snd_hda_gen_spec_init(&spec->gen); 219 codec->spec = spec; 220 221 senary_auto_parse_eapd(codec); 222 spec->gen.own_eapd_ctl = 1; 223 224 /* Setup fixups based on codec vendor ID */ 225 switch (codec->core.vendor_id) { 226 case 0x1fa86186: 227 codec->pin_amp_workaround = 1; 228 spec->gen.mixer_nid = 0x15; 229 snd_hda_pick_fixup(codec, NULL, sn6186_fixups, senary_fixups); 230 231 /* If no specific quirk found, apply the default pin configuration */ 232 if (codec->fixup_id == HDA_FIXUP_ID_NOT_SET) 233 codec->fixup_id = SENARY_FIXUP_PINCFG_DEFAULT; 234 break; 235 default: 236 snd_hda_pick_fixup(codec, NULL, sn6186_fixups, senary_fixups); 237 break; 238 } 239 240 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE); 241 242 /* Run hardware init verbs once during probe */ 243 senary_init_verb(codec); 244 245 if (!spec->gen.vmaster_mute.hook) 246 spec->gen.vmaster_mute.hook = senary_auto_vmaster_hook; 247 248 err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 249 spec->parse_flags); 250 if (err < 0) 251 goto error; 252 253 err = senary_auto_parse_beep(codec); 254 if (err < 0) 255 goto error; 256 257 err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg); 258 if (err < 0) 259 goto error; 260 261 /* Some laptops with Senary chips show stalls in S3 resume, 262 * which falls into the single-cmd mode. 263 * Better to make reset, then. 264 */ 265 if (!codec->bus->core.sync_write) { 266 codec_info(codec, 267 "Enable sync_write for stable communication\n"); 268 codec->bus->core.sync_write = 1; 269 codec->bus->allow_bus_reset = 1; 270 } 271 272 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE); 273 274 return 0; 275 276 error: 277 senary_remove(codec); 278 return err; 279 } 280 281 static const struct hda_codec_ops senary_codec_ops = { 282 .probe = senary_probe, 283 .remove = senary_remove, 284 .build_controls = snd_hda_gen_build_controls, 285 .build_pcms = snd_hda_gen_build_pcms, 286 .init = senary_init, 287 .unsol_event = snd_hda_jack_unsol_event, 288 .suspend = senary_suspend, 289 .check_power_status = snd_hda_gen_check_power_status, 290 .stream_pm = snd_hda_gen_stream_pm, 291 }; 292 293 /* 294 */ 295 296 static const struct hda_device_id snd_hda_id_senary[] = { 297 HDA_CODEC_ID(0x1fa86186, "SN6186"), 298 {} /* terminator */ 299 }; 300 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_senary); 301 302 MODULE_LICENSE("GPL"); 303 MODULE_DESCRIPTION("Senarytech HD-audio codec"); 304 305 static struct hda_codec_driver senary_driver = { 306 .id = snd_hda_id_senary, 307 .ops = &senary_codec_ops, 308 }; 309 310 module_hda_codec_driver(senary_driver); 311