sound: Re-add Acer C720 support with some adjustments
authorMatthew Dillon <dillon@apollo.backplane.com>
Thu, 8 Jan 2015 23:25:02 +0000 (15:25 -0800)
committerMatthew Dillon <dillon@apollo.backplane.com>
Thu, 8 Jan 2015 23:25:02 +0000 (15:25 -0800)
* Add the Acer C720 infrastructure back in.

* Move the core register programming patch into
  gnu/dev/sound/pci/hda/hda_acer_c720_patch.c along with the original
  GPL copyright from the linux file.

  This is the only part of the patch which could reasonably be said to
  have been taken from the linux driver.

* The rest of the patch is basically two things.

  First, mandatory adjustments to the NID routing to detect
  headphone insertion and removal, which was done through trial
  an error with my Acer and then verified against the linux driver.

  Second, we did include the disabling of the remixer based on a
  comment in the linux driver.  Basically NID 11 is disabled, two
  lines of code, and there's only one way to do that.

sys/dev/sound/driver/hda/Makefile
sys/dev/sound/pci/hda/hdaa.c
sys/dev/sound/pci/hda/hdaa.h
sys/dev/sound/pci/hda/hdaa_patches.c
sys/dev/sound/pci/hda/hdaa_patches.h [new file with mode: 0644]
sys/dev/sound/pci/hda/hdac.h
sys/gnu/dev/sound/pci/hda/hda_acer_c720_patch.c [new file with mode: 0644]

index 3144856..fd798f7 100644 (file)
@@ -1,10 +1,12 @@
 # $FreeBSD: head/sys/modules/sound/driver/hda/Makefile 230130 2012-01-15 13:21:36Z mav $
 
 .PATH: ${.CURDIR}/../../pci/hda
+.PATH: ${.CURDIR}/../../../../gnu/dev/sound/pci/hda
 
 KMOD=  snd_hda
 SRCS=  device_if.h bus_if.h pci_if.h channel_if.h mixer_if.h hdac_if.h
 SRCS+= hdaa.c hdaa.h hdaa_patches.c hdac.c hdac_if.h hdac_if.c
 SRCS+= hdacc.c hdac_private.h hdac_reg.h hda_reg.h hdac.h
+SRCS+= hda_acer_c720_patch.c
 
 .include <bsd.kmod.mk>
index 7d39f6d..4086358 100644 (file)
@@ -385,6 +385,17 @@ hdaa_hpredir_handler(struct hdaa_widget *w)
        uint32_t val;
        int j, connected = w->wclass.pin.connected;
 
+       if (w->senseredir) {
+               for (j = 0; j < w->nconns; j++) {
+                       if (w->conns[j] == w->senseredir && connected)
+                               break;
+                       else if (w->conns[j] != w->senseredir && !connected)
+                               break;
+               }
+               if (j != w->nconns)
+                       hdaa_widget_connection_select(w, j);
+       }
+
        HDA_BOOTVERBOSE(
                device_printf((as->pdevinfo && as->pdevinfo->dev) ?
                    as->pdevinfo->dev : devinfo->dev,
@@ -393,7 +404,7 @@ hdaa_hpredir_handler(struct hdaa_widget *w)
        );
        /* (Un)Mute headphone pin. */
        ctl = hdaa_audio_ctl_amp_get(devinfo,
-           w->nid, HDAA_CTL_IN, -1, 1);
+                                    w->nid, HDAA_CTL_IN, -1, 1);
        if (ctl != NULL && ctl->mute) {
                /* If pin has muter - use it. */
                val = connected ? 0 : 1;
@@ -422,8 +433,30 @@ hdaa_hpredir_handler(struct hdaa_widget *w)
        for (j = 0; j < 15; j++) {
                if (as->pins[j] <= 0)
                        continue;
+               w1 = hdaa_widget_get(devinfo, as->pins[j]);
+               if (w == w1)
+                       continue;
+
+               /*
+                * When senseredir is set (typically in hdaa_patches.c) in
+                * a microphone nid it specifies which mux nid is being
+                * routed.
+                */
+               if (w->senseredir && w1) {
+                       int k;
+
+                       for (k = 0; k < w1->nconns; k++) {
+                               if (w1->conns[k] == w->senseredir && connected)
+                                       break;
+                               else if (w1->conns[k] != w->senseredir && !connected)
+                                       break;
+                       }
+                       if (k != w1->nconns)
+                               hdaa_widget_connection_select(w1, k);
+               }
+
                ctl = hdaa_audio_ctl_amp_get(devinfo,
-                   as->pins[j], HDAA_CTL_IN, -1, 1);
+                                            as->pins[j], HDAA_CTL_IN, -1, 1);
                if (ctl != NULL && ctl->mute) {
                        /* If pin has muter - use it. */
                        val = connected ? 1 : 0;
@@ -436,7 +469,6 @@ hdaa_hpredir_handler(struct hdaa_widget *w)
                        continue;
                }
                /* If there is no muter - disable pin output. */
-               w1 = hdaa_widget_get(devinfo, as->pins[j]);
                if (w1 != NULL) {
                        if (connected)
                                val = w1->wclass.pin.ctrl &
@@ -561,8 +593,11 @@ hdaa_presence_handler(struct hdaa_widget *w)
        );
 
        as = &devinfo->as[w->bindas];
-       if (as->hpredir >= 0 && as->pins[15] == w->nid)
+       if (w->senseredir)
                hdaa_hpredir_handler(w);
+       else if (as->hpredir >= 0 && as->pins[15] == w->nid)
+               hdaa_hpredir_handler(w);
+
        if (as->dir == HDAA_CTL_IN && old != 2)
                hdaa_autorecsrc_handler(as, w);
        if (old != 2)
index 3886a10..5ee3f26 100644 (file)
@@ -99,6 +99,7 @@ struct hdaa_widget {
        int nconns, selconn;
        int waspin;
        uint32_t pflags;
+       int senseredir;
        int bindas;
        int bindseqmask;
        int ossdev;
index b1923b7..c4ba40f 100644 (file)
@@ -41,6 +41,7 @@
 #include <dev/sound/pci/hda/hdac.h>
 #include <dev/sound/pci/hda/hdaa.h>
 #include <dev/sound/pci/hda/hda_reg.h>
+#include <dev/sound/pci/hda/hdaa_patches.h>
 
 SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pci/hda/hdaa_patches.c 269158 2014-07-27 20:14:22Z adrian $");
 
@@ -284,6 +285,24 @@ hdac_pin_patch(struct hdaa_widget *w)
        }
 
        /* New patches */
+       if (id == HDA_CODEC_ALC283 && subid == ACER_C720_SUBVENDOR) {
+               switch (nid) {
+               case 20:
+                       patch = "as=2 seq=0";
+                       break;
+               case 25:
+                       patch = "as=1 seq=0";
+                       break;
+               case 27:
+                       /*
+                       patch = "device=Headphones conn=Fixed as=2 seq=15";
+                       w->enable = 1;
+                       */
+                       break;
+               case 33:
+                       break;
+               }
+       } else
        if (id == HDA_CODEC_AD1984A &&
            subid == LENOVO_X300_SUBVENDOR) {
                switch (nid) {
@@ -420,6 +439,10 @@ hdaa_widget_patch(struct hdaa_widget *w)
        struct hdaa_devinfo *devinfo = w->devinfo;
        uint32_t orig;
        nid_t beeper = -1;
+       uint32_t id, subid;
+
+       id = hdaa_codec_id(devinfo);
+       subid = hdaa_card_id(devinfo);
 
        orig = w->param.widget_cap;
        /* On some codecs beeper is an input pin, but it is not recordable
@@ -465,6 +488,23 @@ hdaa_widget_patch(struct hdaa_widget *w)
                }
        );
 
+#if 1
+       /*
+        * Redirect the headphone plug sense (NID 33 -> redir to 12).
+        *
+        * Disable the remixer (NID 11).  There was a comment in the linux
+        * driver that disabling the remixer removes low level whitenoise.
+        * this makes sense since the mixer's unconnected inputs might have
+        * noise on them that leaks through.
+        */
+       if (id == HDA_CODEC_ALC283 && subid == ACER_C720_SUBVENDOR) {
+               if (w->nid == 33)
+                       w->senseredir = 12;
+               if (w->nid == 11)
+                       w->enable = 0;
+       }
+#endif
+
        if (w->type == HDA_PARAM_AUDIO_WIDGET_CAP_TYPE_PIN_COMPLEX)
                hdac_pin_patch(w);
 }
@@ -736,4 +776,27 @@ hdaa_patch_direct(struct hdaa_devinfo *devinfo)
                        hda_command(dev, HDA_CMD_SET_PROCESSING_COEFF(0, 0x20, val|0x80));
                }
        }
+       if (id == HDA_CODEC_ALC283) {
+               if (subid == ACER_C720_SUBVENDOR)
+                       hdaa_patch_direct_acer_c720(devinfo);
+       }
+}
+
+/* XXX move me to a better place */
+uint32_t
+hda_read_coef_idx(device_t dev, nid_t nid, unsigned int coef_idx)
+{
+       uint32_t val;
+
+       hda_command(dev, HDA_CMD_SET_COEFF_INDEX(0, nid, coef_idx));
+       val = hda_command(dev, HDA_CMD_GET_PROCESSING_COEFF(0, nid));
+       return val;
+}
+
+void
+hda_write_coef_idx(device_t dev, nid_t nid, unsigned int coef_idx,
+                  unsigned coef_val)
+{
+       hda_command(dev, HDA_CMD_SET_COEFF_INDEX(0, nid, coef_idx));
+       hda_command(dev, HDA_CMD_SET_PROCESSING_COEFF(0, nid, coef_val));
 }
diff --git a/sys/dev/sound/pci/hda/hdaa_patches.h b/sys/dev/sound/pci/hda/hdaa_patches.h
new file mode 100644 (file)
index 0000000..8eae907
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2015 The DragonFly Project.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name of The DragonFly Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific, prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+void hdaa_patch_direct_acer_c720(struct hdaa_devinfo *devinfo);
+uint32_t hda_read_coef_idx(device_t dev, nid_t nid, unsigned int coef_idx);
+void hda_write_coef_idx(device_t dev, nid_t nid, unsigned int coef_idx,
+                       unsigned coef_val);
index 93bf1fc..8bad02d 100644 (file)
 #define ACER_3681WXM_SUBVENDOR HDA_MODEL_CONSTRUCT(ACER, 0x0110)
 #define ACER_T6292_SUBVENDOR   HDA_MODEL_CONSTRUCT(ACER, 0x011b)
 #define ACER_T5320_SUBVENDOR   HDA_MODEL_CONSTRUCT(ACER, 0x011f)
+#define ACER_C720_SUBVENDOR    HDA_MODEL_CONSTRUCT(ACER, 0x0a11)
 #define ACER_ALL_SUBVENDOR     HDA_MODEL_CONSTRUCT(ACER, 0xffff)
 
 /* Asus */
 #define HDA_CODEC_ALC273       HDA_CODEC_CONSTRUCT(REALTEK, 0x0273)
 #define HDA_CODEC_ALC275       HDA_CODEC_CONSTRUCT(REALTEK, 0x0275)
 #define HDA_CODEC_ALC276       HDA_CODEC_CONSTRUCT(REALTEK, 0x0276)
+#define HDA_CODEC_ALC283       HDA_CODEC_CONSTRUCT(REALTEK, 0x0283)
 #define HDA_CODEC_ALC660       HDA_CODEC_CONSTRUCT(REALTEK, 0x0660)
 #define HDA_CODEC_ALC662       HDA_CODEC_CONSTRUCT(REALTEK, 0x0662)
 #define HDA_CODEC_ALC663       HDA_CODEC_CONSTRUCT(REALTEK, 0x0663)
diff --git a/sys/gnu/dev/sound/pci/hda/hda_acer_c720_patch.c b/sys/gnu/dev/sound/pci/hda/hda_acer_c720_patch.c
new file mode 100644 (file)
index 0000000..c46f0a0
--- /dev/null
@@ -0,0 +1,199 @@
+/*
+ * Universal Interface for Intel High Definition Audio Codec
+ *
+ * HD audio interface patch for Realtek ALC codecs
+ *
+ * Copyright (c) 2004 Kailang Yang <kailang@realtek.com.tw>
+ *                    PeiSen Hou <pshou@realtek.com.tw>
+ *                    Takashi Iwai <tiwai@suse.de>
+ *                    Jonathan Woithe <jwoithe@just42.net>
+ *
+ *  This driver is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This driver is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ *
+ * Taken from linux's patch_realtek.c
+ */
+
+#ifdef HAVE_KERNEL_OPTION_HEADERS
+#include "opt_snd.h"
+#endif
+
+#include <dev/sound/pcm/sound.h>
+
+#include <sys/ctype.h>
+
+#include <dev/sound/pci/hda/hdac.h>
+#include <dev/sound/pci/hda/hdaa.h>
+#include <dev/sound/pci/hda/hda_reg.h>
+#include <dev/sound/pci/hda/hdaa_patches.h>
+
+void
+hdaa_patch_direct_acer_c720(struct hdaa_devinfo *devinfo)
+{
+       struct hdaa_widget *w;
+       device_t dev = devinfo->dev;
+       uint32_t val;
+
+       kprintf("Acer C720 patch\n");
+
+       /* power down control */
+       hda_write_coef_idx(dev, 0x20, 0x03, 0x0002);
+       /* FIFO and filter clock */
+       hda_write_coef_idx(dev, 0x20, 0x05, 0x0700);
+       /* DMIC control */
+       hda_write_coef_idx(dev, 0x20, 0x07, 0x0200);
+       /* Analog clock */
+       val = hda_read_coef_idx(dev, 0x20, 0x06);
+       hda_write_coef_idx(dev, 0x20, 0x06, (val & ~0x00f0) | 0x0);
+
+       /* JD */
+       val = hda_read_coef_idx(dev, 0x20, 0x08);
+       hda_write_coef_idx(dev, 0x20, 0x08, (val & ~0xfffc) | 0x0c2c);
+       /* JD offset1 */
+       hda_write_coef_idx(dev, 0x20, 0x0a, 0xcccc);
+       /* JD offset2 */
+       hda_write_coef_idx(dev, 0x20, 0x0b, 0xcccc);
+       /* LD0/1/2/3, DAC/ADC */
+       hda_write_coef_idx(dev, 0x20, 0x0e, 0x6fc0);
+       /* JD */
+       val = hda_read_coef_idx(dev, 0x20, 0x0f);
+       hda_write_coef_idx(dev, 0x20, 0x0f, (val & ~0xf800) | 0x1000);
+
+       /* Capless */
+       val = hda_read_coef_idx(dev, 0x20, 0x10);
+       hda_write_coef_idx(dev, 0x20, 0x10, (val & ~0xfc00) | 0x0c00);
+       /* Class D test 4 */
+       hda_write_coef_idx(dev, 0x20, 0x3a, 0x0);
+       /* IO power down directly */
+       val = hda_read_coef_idx(dev, 0x20, 0x0c);
+       hda_write_coef_idx(dev, 0x20, 0x0c, (val & ~0xfe00) | 0x0);
+       /* ANC */
+       hda_write_coef_idx(dev, 0x20, 0x22, 0xa0c0);
+       /* AGC MUX */
+       val = hda_read_coef_idx(dev, 0x53, 0x01);
+       hda_write_coef_idx(dev, 0x53, 0x01, (val & ~0x000f) | 0x0008);
+
+       /* DAC simple content protection */
+       val = hda_read_coef_idx(dev, 0x20, 0x1d);
+       hda_write_coef_idx(dev, 0x20, 0x1d, (val & ~0x00e0) | 0x0);
+       /* ADC simple content protection */
+       val = hda_read_coef_idx(dev, 0x20, 0x1f);
+       hda_write_coef_idx(dev, 0x20, 0x1f, (val & ~0x00e0) | 0x0);
+       /* DAC ADC Zero Detection */
+       hda_write_coef_idx(dev, 0x20, 0x21, 0x8804);
+       /* PLL */
+       hda_write_coef_idx(dev, 0x20, 0x2e, 0x2902);
+       /* capless control 2 */
+       hda_write_coef_idx(dev, 0x20, 0x33, 0xa080);
+       /* capless control 3 */
+       hda_write_coef_idx(dev, 0x20, 0x34, 0x3400);
+       /* capless control 4 */
+       hda_write_coef_idx(dev, 0x20, 0x35, 0x2f3e);
+       /* capless control 5 */
+       hda_write_coef_idx(dev, 0x20, 0x36, 0x0);
+       /* class D test 2 */
+       val = hda_read_coef_idx(dev, 0x20, 0x38);
+       hda_write_coef_idx(dev, 0x20, 0x38, (val & ~0x0fff) | 0x0900);
+
+       /* class D test 3 */
+       hda_write_coef_idx(dev, 0x20, 0x39, 0x110a);
+       /* class D test 5 */
+       val = hda_read_coef_idx(dev, 0x20, 0x3b);
+       hda_write_coef_idx(dev, 0x20, 0x3b, (val & ~0x00f8) | 0x00d8);
+       /* class D test 6 */
+       hda_write_coef_idx(dev, 0x20, 0x3c, 0x0014);
+       /* classD OCP */
+       hda_write_coef_idx(dev, 0x20, 0x3d, 0xc2ba);
+       /* classD pure DC test */
+       val = hda_read_coef_idx(dev, 0x20, 0x42);
+       hda_write_coef_idx(dev, 0x20, 0x42, (val & ~0x0f80) | 0x0);
+       /* test mode */
+       hda_write_coef_idx(dev, 0x20, 0x49, 0x0);
+       /* Class D DC enable */
+       val = hda_read_coef_idx(dev, 0x20, 0x40);
+       hda_write_coef_idx(dev, 0x20, 0x40, (val & ~0xf800) | 0x9800);
+       /* DC offset */
+       val = hda_read_coef_idx(dev, 0x20, 0x42);
+       hda_write_coef_idx(dev, 0x20, 0x42, (val & ~0xf000) | 0x2000);
+       /* Class D amp control */
+       hda_write_coef_idx(dev, 0x20, 0x37, 0xfc06);
+
+       /* Index 0x43 direct drive HP AMP LPM Control 1 */
+       /* Headphone capless set to high power mode */
+       hda_write_coef_idx(dev, 0x20, 0x43, 0x9004);
+
+#if 0
+       /*
+        * This has to do with the 'mute internal speaker when
+        * ext headphone out jack is plugged' function.  nid 27
+        * comes from the special config bits (XXX currently hardwired)
+        *
+        * XXX doesn't apply to chromebook where we just have to change
+        *      the mixer selection for nid 33.
+        */
+       int dummy;
+       hda_command(dev, HDA_CMD_SET_AMP_GAIN_MUTE(0, 27, 0xb080));
+       tsleep(&dummy, 0, "hdaslp", hz / 10);
+       hda_command(dev, HDA_CMD_SET_PIN_WIDGET_CTRL(0, 27,
+                               HDA_CMD_SET_PIN_WIDGET_CTRL_OUT_ENABLE));
+       tsleep(&dummy, 0, "hdaslp", hz / 10);
+#endif
+
+       /* 0x46 combo jack auto switch control 2 */
+       /* 3k pull-down control for headset jack. */
+       val = hda_read_coef_idx(dev, 0x20, 0x46);
+       hda_write_coef_idx(dev, 0x20, 0x46, val & ~(3 << 12));
+       /* headphone capless set to normal mode */
+       hda_write_coef_idx(dev, 0x20, 0x43, 0x9614);
+
+#if 0
+       /*
+        * Fixup chromebook (? which chromebook?)
+        */
+       /* MIC2-VREF control */
+       /* set to manual mode */
+       val = hda_read_coef_idx(dev, 0x20, 0x06);
+       hda_write_coef_idx(dev, 0x20, 0x06, val & ~0x000c);
+       /* enable line1 input control by verb */
+       val = hda_read_coef_idx(dev, 0x20, 0x1a);
+       hda_write_coef_idx(dev, 0x20, 0x1a, val | (1 << 4));
+#endif
+
+       /*
+        * 31-30        : port connectivity
+        * 29-21        : reserved
+        * 20           : PCBEEP input
+        * 19-16        : checksum (15:1)
+        * 15-1         : Custom
+        * 0            : Override
+        *
+        * XXX this needs code from linux patch_realtek.c alc_subsystem_id().
+        * Chromebook: 0x4015812d
+        *      bit 30  physical connection present
+        *      bit 15  if set we want the 'mute internal speaker when
+        *              ext headphone out jack is plugged' function
+        *          bit 14:13   reserved
+        *          bit 12:11   headphone out 00: PortA, 01: PortE, 02: PortD,
+        *                                    03: Reserved (for C720 this is 0)
+        *          bit 10:8    jack location (for c720 this is 1)
+        *                              0, 0x1b, 0x14, 0x21 - nnid 27 is jack?
+        *
+        *      bit 5:3 -> 101 (5).  ALC_INIT_DEFAULT (default ext amp ctl)
+        *      bit 0   override (is set)
+        */
+       if ((w = hdaa_widget_get(devinfo, 0x1d)) != NULL) {
+               kprintf("WIDGET SPECIAL: %08x\n", w->wclass.pin.config);
+               /* XXX currently in hdaa_patch_direct_acer_c720(devinfo); */
+       }
+}