]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - sound/pci/hda/patch_realtek.c
ALSA: hda/realtek - Cache COEF 0 value
[mirror_ubuntu-bionic-kernel.git] / sound / pci / hda / patch_realtek.c
index bf53663186c9e9fbdfe0a71a503bd818b2152a4b..f9d24c33ce93b230719b4c2e1fbfec3351ca1106 100644 (file)
@@ -197,6 +197,7 @@ struct alc_spec {
        /* for PLL fix */
        hda_nid_t pll_nid;
        unsigned int pll_coef_idx, pll_coef_bit;
+       unsigned int coef0;
 
        /* fix-up list */
        int fixup_id;
@@ -1554,6 +1555,15 @@ static void alc_write_coef_idx(struct hda_codec *codec, unsigned int coef_idx,
                            coef_val);
 }
 
+/* a special bypass for COEF 0; read the cached value at the second time */
+static unsigned int alc_get_coef0(struct hda_codec *codec)
+{
+       struct alc_spec *spec = codec->spec;
+       if (!spec->coef0)
+               spec->coef0 = alc_read_coef_idx(codec, 0);
+       return spec->coef0;
+}
+
 /*
  * Digital I/O handling
  */
@@ -2479,6 +2489,47 @@ static int alc_codec_rename(struct hda_codec *codec, const char *name)
        return 0;
 }
 
+/*
+ * Rename codecs appropriately from COEF value
+ */
+struct alc_codec_rename_table {
+       unsigned int vendor_id;
+       unsigned short coef_mask;
+       unsigned short coef_bits;
+       const char *name;
+};
+
+static struct alc_codec_rename_table rename_tbl[] = {
+       { 0x10ec0269, 0xfff0, 0x3010, "ALC277" },
+       { 0x10ec0269, 0xf0f0, 0x2010, "ALC259" },
+       { 0x10ec0269, 0xf0f0, 0x3010, "ALC258" },
+       { 0x10ec0269, 0x00f0, 0x0010, "ALC269VB" },
+       { 0x10ec0269, 0xffff, 0xa023, "ALC259" },
+       { 0x10ec0269, 0xffff, 0x6023, "ALC281X" },
+       { 0x10ec0269, 0x00f0, 0x0020, "ALC269VC" },
+       { 0x10ec0887, 0x00f0, 0x0030, "ALC887-VD" },
+       { 0x10ec0888, 0x00f0, 0x0030, "ALC888-VD" },
+       { 0x10ec0888, 0xf0f0, 0x3020, "ALC886" },
+       { 0x10ec0899, 0x2000, 0x2000, "ALC899" },
+       { 0x10ec0892, 0xffff, 0x8020, "ALC661" },
+       { 0x10ec0892, 0xffff, 0x8011, "ALC661" },
+       { 0x10ec0892, 0xffff, 0x4011, "ALC656" },
+       { } /* terminator */
+};
+
+static int alc_codec_rename_from_preset(struct hda_codec *codec)
+{
+       const struct alc_codec_rename_table *p;
+
+       for (p = rename_tbl; p->vendor_id; p++) {
+               if (p->vendor_id != codec->vendor_id)
+                       continue;
+               if ((alc_get_coef0(codec) & p->coef_mask) == p->coef_bits)
+                       return alc_codec_rename(codec, p->name);
+       }
+       return 0;
+}
+
 /*
  * Automatic parse of I/O pins from the BIOS configuration
  */
@@ -3263,7 +3314,7 @@ static void alc_auto_init_extra_out(struct hda_codec *codec)
        int i;
        hda_nid_t pin, dac;
 
-       for (i = 0; i < spec->autocfg.speaker_outs; i++) {
+       for (i = 0; i < spec->autocfg.hp_outs; i++) {
                pin = spec->autocfg.hp_pins[i];
                if (!pin)
                        break;
@@ -3853,10 +3904,8 @@ static int patch_alc880(struct hda_codec *codec)
        if (board_config == ALC_MODEL_AUTO) {
                /* automatic parse from the BIOS config */
                err = alc880_parse_auto_config(codec);
-               if (err < 0) {
-                       alc_free(codec);
-                       return err;
-               }
+               if (err < 0)
+                       goto error;
 #ifdef CONFIG_SND_HDA_ENABLE_REALTEK_QUIRKS
                else if (!err) {
                        printk(KERN_INFO
@@ -3881,10 +3930,8 @@ static int patch_alc880(struct hda_codec *codec)
 
        if (!spec->no_analog) {
                err = snd_hda_attach_beep_device(codec, 0x1);
-               if (err < 0) {
-                       alc_free(codec);
-                       return err;
-               }
+               if (err < 0)
+                       goto error;
                set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
        }
 
@@ -3899,6 +3946,10 @@ static int patch_alc880(struct hda_codec *codec)
 #endif
 
        return 0;
+
+ error:
+       alc_free(codec);
+       return err;
 }
 
 
@@ -3980,10 +4031,8 @@ static int patch_alc260(struct hda_codec *codec)
        if (board_config == ALC_MODEL_AUTO) {
                /* automatic parse from the BIOS config */
                err = alc260_parse_auto_config(codec);
-               if (err < 0) {
-                       alc_free(codec);
-                       return err;
-               }
+               if (err < 0)
+                       goto error;
 #ifdef CONFIG_SND_HDA_ENABLE_REALTEK_QUIRKS
                else if (!err) {
                        printk(KERN_INFO
@@ -4008,10 +4057,8 @@ static int patch_alc260(struct hda_codec *codec)
 
        if (!spec->no_analog) {
                err = snd_hda_attach_beep_device(codec, 0x1);
-               if (err < 0) {
-                       alc_free(codec);
-                       return err;
-               }
+               if (err < 0)
+                       goto error;
                set_beep_amp(spec, 0x07, 0x05, HDA_INPUT);
        }
 
@@ -4029,6 +4076,10 @@ static int patch_alc260(struct hda_codec *codec)
 #endif
 
        return 0;
+
+ error:
+       alc_free(codec);
+       return err;
 }
 
 
@@ -4136,6 +4187,10 @@ static int patch_alc882(struct hda_codec *codec)
                break;
        }
 
+       err = alc_codec_rename_from_preset(codec);
+       if (err < 0)
+               goto error;
+
        board_config = alc_board_config(codec, ALC882_MODEL_LAST,
                                        alc882_models, alc882_cfg_tbl);
 
@@ -4159,10 +4214,8 @@ static int patch_alc882(struct hda_codec *codec)
        if (board_config == ALC_MODEL_AUTO) {
                /* automatic parse from the BIOS config */
                err = alc882_parse_auto_config(codec);
-               if (err < 0) {
-                       alc_free(codec);
-                       return err;
-               }
+               if (err < 0)
+                       goto error;
 #ifdef CONFIG_SND_HDA_ENABLE_REALTEK_QUIRKS
                else if (!err) {
                        printk(KERN_INFO
@@ -4187,10 +4240,8 @@ static int patch_alc882(struct hda_codec *codec)
 
        if (!spec->no_analog && has_cdefine_beep(codec)) {
                err = snd_hda_attach_beep_device(codec, 0x1);
-               if (err < 0) {
-                       alc_free(codec);
-                       return err;
-               }
+               if (err < 0)
+                       goto error;
                set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
        }
 
@@ -4209,6 +4260,10 @@ static int patch_alc882(struct hda_codec *codec)
 #endif
 
        return 0;
+
+ error:
+       alc_free(codec);
+       return err;
 }
 
 
@@ -4313,10 +4368,8 @@ static int patch_alc262(struct hda_codec *codec)
        if (board_config == ALC_MODEL_AUTO) {
                /* automatic parse from the BIOS config */
                err = alc262_parse_auto_config(codec);
-               if (err < 0) {
-                       alc_free(codec);
-                       return err;
-               }
+               if (err < 0)
+                       goto error;
 #ifdef CONFIG_SND_HDA_ENABLE_REALTEK_QUIRKS
                else if (!err) {
                        printk(KERN_INFO
@@ -4341,10 +4394,8 @@ static int patch_alc262(struct hda_codec *codec)
 
        if (!spec->no_analog && has_cdefine_beep(codec)) {
                err = snd_hda_attach_beep_device(codec, 0x1);
-               if (err < 0) {
-                       alc_free(codec);
-                       return err;
-               }
+               if (err < 0)
+                       goto error;
                set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
        }
 
@@ -4364,6 +4415,10 @@ static int patch_alc262(struct hda_codec *codec)
 #endif
 
        return 0;
+
+ error:
+       alc_free(codec);
+       return err;
 }
 
 /*
@@ -4427,10 +4482,8 @@ static int patch_alc268(struct hda_codec *codec)
 
        /* automatic parse from the BIOS config */
        err = alc268_parse_auto_config(codec);
-       if (err < 0) {
-               alc_free(codec);
-               return err;
-       }
+       if (err < 0)
+               goto error;
 
        has_beep = 0;
        for (i = 0; i < spec->num_mixers; i++) {
@@ -4442,10 +4495,8 @@ static int patch_alc268(struct hda_codec *codec)
 
        if (has_beep) {
                err = snd_hda_attach_beep_device(codec, 0x1);
-               if (err < 0) {
-                       alc_free(codec);
-                       return err;
-               }
+               if (err < 0)
+                       goto error;
                if (!query_amp_caps(codec, 0x1d, HDA_INPUT))
                        /* override the amp caps for beep generator */
                        snd_hda_override_amp_caps(codec, 0x1d, HDA_INPUT,
@@ -4473,6 +4524,10 @@ static int patch_alc268(struct hda_codec *codec)
        alc_init_jacks(codec);
 
        return 0;
+
+ error:
+       alc_free(codec);
+       return err;
 }
 
 /*
@@ -4566,9 +4621,9 @@ static void alc269_toggle_power_output(struct hda_codec *codec, int power_up)
 
 static void alc269_shutup(struct hda_codec *codec)
 {
-       if ((alc_read_coef_idx(codec, 0) & 0x00ff) == 0x017)
+       if ((alc_get_coef0(codec) & 0x00ff) == 0x017)
                alc269_toggle_power_output(codec, 0);
-       if ((alc_read_coef_idx(codec, 0) & 0x00ff) == 0x018) {
+       if ((alc_get_coef0(codec) & 0x00ff) == 0x018) {
                alc269_toggle_power_output(codec, 0);
                msleep(150);
        }
@@ -4577,19 +4632,19 @@ static void alc269_shutup(struct hda_codec *codec)
 #ifdef CONFIG_PM
 static int alc269_resume(struct hda_codec *codec)
 {
-       if ((alc_read_coef_idx(codec, 0) & 0x00ff) == 0x018) {
+       if ((alc_get_coef0(codec) & 0x00ff) == 0x018) {
                alc269_toggle_power_output(codec, 0);
                msleep(150);
        }
 
        codec->patch_ops.init(codec);
 
-       if ((alc_read_coef_idx(codec, 0) & 0x00ff) == 0x017) {
+       if ((alc_get_coef0(codec) & 0x00ff) == 0x017) {
                alc269_toggle_power_output(codec, 1);
                msleep(200);
        }
 
-       if ((alc_read_coef_idx(codec, 0) & 0x00ff) == 0x018)
+       if ((alc_get_coef0(codec) & 0x00ff) == 0x018)
                alc269_toggle_power_output(codec, 1);
 
        snd_hda_codec_resume_amp(codec);
@@ -4907,23 +4962,23 @@ static int alc269_fill_coef(struct hda_codec *codec)
 {
        int val;
 
-       if ((alc_read_coef_idx(codec, 0) & 0x00ff) < 0x015) {
+       if ((alc_get_coef0(codec) & 0x00ff) < 0x015) {
                alc_write_coef_idx(codec, 0xf, 0x960b);
                alc_write_coef_idx(codec, 0xe, 0x8817);
        }
 
-       if ((alc_read_coef_idx(codec, 0) & 0x00ff) == 0x016) {
+       if ((alc_get_coef0(codec) & 0x00ff) == 0x016) {
                alc_write_coef_idx(codec, 0xf, 0x960b);
                alc_write_coef_idx(codec, 0xe, 0x8814);
        }
 
-       if ((alc_read_coef_idx(codec, 0) & 0x00ff) == 0x017) {
+       if ((alc_get_coef0(codec) & 0x00ff) == 0x017) {
                val = alc_read_coef_idx(codec, 0x04);
                /* Power up output pin */
                alc_write_coef_idx(codec, 0x04, val | (1<<11));
        }
 
-       if ((alc_read_coef_idx(codec, 0) & 0x00ff) == 0x018) {
+       if ((alc_get_coef0(codec) & 0x00ff) == 0x018) {
                val = alc_read_coef_idx(codec, 0xd);
                if ((val & 0x0c00) >> 10 != 0x1) {
                        /* Capless ramp up clock control */
@@ -4950,7 +5005,7 @@ static int alc269_fill_coef(struct hda_codec *codec)
 static int patch_alc269(struct hda_codec *codec)
 {
        struct alc_spec *spec;
-       int err;
+       int err = 0;
 
        spec = kzalloc(sizeof(*spec), GFP_KERNEL);
        if (spec == NULL)
@@ -4962,37 +5017,30 @@ static int patch_alc269(struct hda_codec *codec)
 
        alc_auto_parse_customize_define(codec);
 
+       err = alc_codec_rename_from_preset(codec);
+       if (err < 0)
+               goto error;
+
        if (codec->vendor_id == 0x10ec0269) {
-               unsigned int coef;
                spec->codec_variant = ALC269_TYPE_ALC269VA;
-               coef = alc_read_coef_idx(codec, 0);
-               if ((coef & 0x00f0) == 0x0010) {
+               switch (alc_get_coef0(codec) & 0x00f0) {
+               case 0x0010:
                        if (codec->bus->pci->subsystem_vendor == 0x1025 &&
-                           spec->cdefine.platform_type == 1) {
-                               alc_codec_rename(codec, "ALC271X");
-                       } else if ((coef & 0xf000) == 0x2000) {
-                               alc_codec_rename(codec, "ALC259");
-                       } else if ((coef & 0xf000) == 0x3000) {
-                               alc_codec_rename(codec, "ALC258");
-                       } else if ((coef & 0xfff0) == 0x3010) {
-                               alc_codec_rename(codec, "ALC277");
-                       } else {
-                               alc_codec_rename(codec, "ALC269VB");
-                       }
+                           spec->cdefine.platform_type == 1)
+                               err = alc_codec_rename(codec, "ALC271X");
                        spec->codec_variant = ALC269_TYPE_ALC269VB;
-               } else if ((coef & 0x00f0) == 0x0020) {
-                       if (coef == 0xa023)
-                               alc_codec_rename(codec, "ALC259");
-                       else if (coef == 0x6023)
-                               alc_codec_rename(codec, "ALC281X");
-                       else if (codec->bus->pci->subsystem_vendor == 0x17aa &&
-                                codec->bus->pci->subsystem_device == 0x21f3)
-                               alc_codec_rename(codec, "ALC3202");
-                       else
-                               alc_codec_rename(codec, "ALC269VC");
+                       break;
+               case 0x0020:
+                       if (codec->bus->pci->subsystem_vendor == 0x17aa &&
+                           codec->bus->pci->subsystem_device == 0x21f3)
+                               err = alc_codec_rename(codec, "ALC3202");
                        spec->codec_variant = ALC269_TYPE_ALC269VC;
-               } else
+                       break;
+               default:
                        alc_fix_pll_init(codec, 0x20, 0x04, 15);
+               }
+               if (err < 0)
+                       goto error;
                alc269_fill_coef(codec);
        }
 
@@ -5002,10 +5050,8 @@ static int patch_alc269(struct hda_codec *codec)
 
        /* automatic parse from the BIOS config */
        err = alc269_parse_auto_config(codec);
-       if (err < 0) {
-               alc_free(codec);
-               return err;
-       }
+       if (err < 0)
+               goto error;
 
        if (!spec->no_analog && !spec->adc_nids) {
                alc_auto_fill_adc_caps(codec);
@@ -5018,10 +5064,8 @@ static int patch_alc269(struct hda_codec *codec)
 
        if (!spec->no_analog && has_cdefine_beep(codec)) {
                err = snd_hda_attach_beep_device(codec, 0x1);
-               if (err < 0) {
-                       alc_free(codec);
-                       return err;
-               }
+               if (err < 0)
+                       goto error;
                set_beep_amp(spec, 0x0b, 0x04, HDA_INPUT);
        }
 
@@ -5045,6 +5089,10 @@ static int patch_alc269(struct hda_codec *codec)
 #endif
 
        return 0;
+
+ error:
+       alc_free(codec);
+       return err;
 }
 
 /*
@@ -5110,10 +5158,8 @@ static int patch_alc861(struct hda_codec *codec)
 
        /* automatic parse from the BIOS config */
        err = alc861_parse_auto_config(codec);
-       if (err < 0) {
-               alc_free(codec);
-               return err;
-       }
+       if (err < 0)
+               goto error;
 
        if (!spec->no_analog && !spec->adc_nids) {
                alc_auto_fill_adc_caps(codec);
@@ -5126,10 +5172,8 @@ static int patch_alc861(struct hda_codec *codec)
 
        if (!spec->no_analog) {
                err = snd_hda_attach_beep_device(codec, 0x23);
-               if (err < 0) {
-                       alc_free(codec);
-                       return err;
-               }
+               if (err < 0)
+                       goto error;
                set_beep_amp(spec, 0x23, 0, HDA_OUTPUT);
        }
 
@@ -5146,6 +5190,10 @@ static int patch_alc861(struct hda_codec *codec)
 #endif
 
        return 0;
+
+ error:
+       alc_free(codec);
+       return err;
 }
 
 /*
@@ -5231,10 +5279,8 @@ static int patch_alc861vd(struct hda_codec *codec)
 
        /* automatic parse from the BIOS config */
        err = alc861vd_parse_auto_config(codec);
-       if (err < 0) {
-               alc_free(codec);
-               return err;
-       }
+       if (err < 0)
+               goto error;
 
        if (codec->vendor_id == 0x10ec0660) {
                /* always turn on EAPD */
@@ -5252,10 +5298,8 @@ static int patch_alc861vd(struct hda_codec *codec)
 
        if (!spec->no_analog) {
                err = snd_hda_attach_beep_device(codec, 0x23);
-               if (err < 0) {
-                       alc_free(codec);
-                       return err;
-               }
+               if (err < 0)
+                       goto error;
                set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
        }
 
@@ -5273,6 +5317,10 @@ static int patch_alc861vd(struct hda_codec *codec)
 #endif
 
        return 0;
+
+ error:
+       alc_free(codec);
+       return err;
 }
 
 /*
@@ -5576,8 +5624,7 @@ static const struct alc_model_fixup alc662_fixup_models[] = {
 static int patch_alc662(struct hda_codec *codec)
 {
        struct alc_spec *spec;
-       int err;
-       int coef;
+       int err = 0;
 
        spec = kzalloc(sizeof(*spec), GFP_KERNEL);
        if (!spec)
@@ -5594,25 +5641,24 @@ static int patch_alc662(struct hda_codec *codec)
 
        alc_fix_pll_init(codec, 0x20, 0x04, 15);
 
-       coef = alc_read_coef_idx(codec, 0);
-       if (coef == 0x8020 || coef == 0x8011)
-               alc_codec_rename(codec, "ALC661");
-       else if (coef & (1 << 14) &&
-               codec->bus->pci->subsystem_vendor == 0x1025 &&
-               spec->cdefine.platform_type == 1)
-               alc_codec_rename(codec, "ALC272X");
-       else if (coef == 0x4011)
-               alc_codec_rename(codec, "ALC656");
+       err = alc_codec_rename_from_preset(codec);
+       if (err < 0)
+               goto error;
+
+       if ((alc_get_coef0(codec) & (1 << 14)) &&
+           codec->bus->pci->subsystem_vendor == 0x1025 &&
+           spec->cdefine.platform_type == 1) {
+               if (alc_codec_rename(codec, "ALC272X") < 0)
+                       goto error;
+       }
 
        alc_pick_fixup(codec, alc662_fixup_models,
                       alc662_fixup_tbl, alc662_fixups);
        alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
        /* automatic parse from the BIOS config */
        err = alc662_parse_auto_config(codec);
-       if (err < 0) {
-               alc_free(codec);
-               return err;
-       }
+       if (err < 0)
+               goto error;
 
        if (!spec->no_analog && !spec->adc_nids) {
                alc_auto_fill_adc_caps(codec);
@@ -5625,10 +5671,8 @@ static int patch_alc662(struct hda_codec *codec)
 
        if (!spec->no_analog && has_cdefine_beep(codec)) {
                err = snd_hda_attach_beep_device(codec, 0x1);
-               if (err < 0) {
-                       alc_free(codec);
-                       return err;
-               }
+               if (err < 0)
+                       goto error;
                switch (codec->vendor_id) {
                case 0x10ec0662:
                        set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
@@ -5659,32 +5703,10 @@ static int patch_alc662(struct hda_codec *codec)
 #endif
 
        return 0;
-}
 
-static int patch_alc888(struct hda_codec *codec)
-{
-       if ((alc_read_coef_idx(codec, 0) & 0x00f0)==0x0030){
-               kfree(codec->chip_name);
-               if (codec->vendor_id == 0x10ec0887)
-                       codec->chip_name = kstrdup("ALC887-VD", GFP_KERNEL);
-               else
-                       codec->chip_name = kstrdup("ALC888-VD", GFP_KERNEL);
-               if (!codec->chip_name) {
-                       alc_free(codec);
-                       return -ENOMEM;
-               }
-               return patch_alc662(codec);
-       }
-       return patch_alc882(codec);
-}
-
-static int patch_alc899(struct hda_codec *codec)
-{
-       if ((alc_read_coef_idx(codec, 0) & 0x2000) != 0x2000) {
-               kfree(codec->chip_name);
-               codec->chip_name = kstrdup("ALC898", GFP_KERNEL);
-       }
-       return patch_alc882(codec);
+ error:
+       alc_free(codec);
+       return err;
 }
 
 /*
@@ -5764,13 +5786,13 @@ static const struct hda_codec_preset snd_hda_preset_realtek[] = {
        { .id = 0x10ec0885, .rev = 0x100103, .name = "ALC889A",
          .patch = patch_alc882 },
        { .id = 0x10ec0885, .name = "ALC885", .patch = patch_alc882 },
-       { .id = 0x10ec0887, .name = "ALC887", .patch = patch_alc888 },
+       { .id = 0x10ec0887, .name = "ALC887", .patch = patch_alc882 },
        { .id = 0x10ec0888, .rev = 0x100101, .name = "ALC1200",
          .patch = patch_alc882 },
-       { .id = 0x10ec0888, .name = "ALC888", .patch = patch_alc888 },
+       { .id = 0x10ec0888, .name = "ALC888", .patch = patch_alc882 },
        { .id = 0x10ec0889, .name = "ALC889", .patch = patch_alc882 },
        { .id = 0x10ec0892, .name = "ALC892", .patch = patch_alc662 },
-       { .id = 0x10ec0899, .name = "ALC899", .patch = patch_alc899 },
+       { .id = 0x10ec0899, .name = "ALC898", .patch = patch_alc882 },
        {} /* terminator */
 };