Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/i2c Add driver for Everest Semi ES8316 Low Power Aud...



details:   https://anonhg.NetBSD.org/src/rev/d06c5f2b5d18
branches:  trunk
changeset: 466823:d06c5f2b5d18
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Thu Jan 02 22:06:59 2020 +0000

description:
Add driver for Everest Semi ES8316 Low Power Audio CODEC

diffstat:

 sys/dev/i2c/es8316ac.c |  753 +++++++++++++++++++++++++++++++++++++++++++++++++
 sys/dev/i2c/files.i2c  |    7 +-
 2 files changed, 759 insertions(+), 1 deletions(-)

diffs (truncated from 775 to 300 lines):

diff -r eaba6c7501e2 -r d06c5f2b5d18 sys/dev/i2c/es8316ac.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/i2c/es8316ac.c    Thu Jan 02 22:06:59 2020 +0000
@@ -0,0 +1,753 @@
+/* $NetBSD: es8316ac.c,v 1.1 2020/01/02 22:06:59 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2020 Jared McNeill <jmcneill%invisible.ca@localhost>
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: es8316ac.c,v 1.1 2020/01/02 22:06:59 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/device.h>
+#include <sys/intr.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/conf.h>
+#include <sys/bitops.h>
+
+#include <dev/audio/audio_dai.h>
+
+#include <dev/i2c/i2cvar.h>
+
+#include <dev/fdt/fdtvar.h>
+
+#define        ESCODEC_RESET_REG               0x00
+#define         RESET_ALL                      __BITS(5,0)
+#define         RESET_CSM_ON                   __BIT(7)
+#define        ESCODEC_CLKMAN1_REG             0x01
+#define         CLKMAN1_MCLK_ON                __BIT(6)
+#define         CLKMAN1_BCLK_ON                __BIT(5)
+#define         CLKMAN1_CLK_CP_ON              __BIT(4)
+#define         CLKMAN1_CLK_DAC_ON             __BIT(2)
+#define         CLKMAN1_ANACLK_DAC_ON          __BIT(0)
+#define        ESCODEC_ADC_OSR_REG             0x03
+#define        ESCODEC_SD_CLK_REG              0x09
+#define         SD_CLK_MSC                     __BIT(7)
+#define         SD_CLK_BCLK_INV                __BIT(5)
+#define        ESCODEC_SD_ADC_REG              0x0a
+#define        ESCODEC_SD_DAC_REG              0x0b
+#define         SD_FMT_LRP                     __BIT(5)
+#define         SD_FMT_WL                      __BITS(4,2)
+#define          SD_FMT_WL_16                  3
+#define         SD_FMT_MASK                    __BITS(1,0)
+#define          SD_FMT_I2S                    0
+#define        ESCODEC_VMID_REG                0x0c
+#define        ESCODEC_PDN_REG                 0x0d
+#define        ESCODEC_HPSEL_REG               0x13
+#define        ESCODEC_HPMIXRT_REG             0x14
+#define         HPMIXRT_LD2LHPMIX              __BIT(7)
+#define         HPMIXRT_RD2RHPMIX              __BIT(3)
+#define        ESCODEC_HPMIX_REG               0x15
+#define         HPMIX_LHPMIX_MUTE              __BIT(5)
+#define         HPMIX_PDN_LHP_MIX              __BIT(4)
+#define         HPMIX_RHPMIX_MUTE              __BIT(1)
+#define         HPMIX_PDN_RHP_MIX              __BIT(0)
+#define        ESCODEC_HPMIXVOL_REG            0x16
+#define         HPMIXVOL_LHPMIXVOL             __BITS(7,4)
+#define         HPMIXVOL_RHPMIXVOL             __BITS(3,0)
+#define        ESCODEC_HPOUTEN_REG             0x17
+#define         HPOUTEN_EN_HPL                 __BIT(6)
+#define         HPOUTEN_HPL_OUTEN              __BIT(5)
+#define         HPOUTEN_EN_HPR                 __BIT(2)
+#define         HPOUTEN_HPR_OUTEN              __BIT(1)
+#define        ESCODEC_HPVOL_REG               0x18
+#define         HPVOL_PDN_LICAL                __BIT(7)
+#define         HPVOL_HPLVOL                   __BITS(5,4)
+#define         HPVOL_PDN_RICAL                __BIT(3)
+#define         HPVOL_HPRVOL                   __BITS(1,0)
+#define        ESCODEC_HPPWR_REG               0x19
+#define         HPPWR_PDN_CPHP                 __BIT(2)
+#define        ESCODEC_CPPWR_REG               0x1a
+#define         CPPWR_PDN_CP                   __BIT(5)
+#define        ESCODEC_DACPWR_REG              0x2f
+#define         DACPWR_PDN_DAC_L               __BIT(4)
+#define         DACPWR_PDN_DAC_R               __BIT(0)
+#define        ESCODEC_DACCTL1_REG             0x30
+#define         DACCTL1_MUTE                   __BIT(5)
+#define        ESCODEC_DACVOL_L_REG            0x33
+#define         DACVOL_L_DACVOLUME             __BITS(7,0)
+#define        ESCODEC_DACVOL_R_REG            0x34
+#define         DACVOL_R_DACVOLUME             __BITS(7,0)
+
+static const struct device_compatible_entry compat_data[] = {
+       { "everest,es8316",             1 },
+       { NULL }
+};
+
+struct escodec_softc {
+       device_t                sc_dev;
+       i2c_tag_t               sc_i2c;
+       i2c_addr_t              sc_addr;
+       int                     sc_phandle;
+       struct clk              *sc_clk;
+
+       struct audio_dai_device sc_dai;
+       audio_dai_tag_t         sc_amplifier;
+
+       uint8_t                 sc_swvol;
+};
+
+static void
+escodec_lock(struct escodec_softc *sc)
+{
+       iic_acquire_bus(sc->sc_i2c, 0);
+}
+
+static void
+escodec_unlock(struct escodec_softc *sc)
+{
+       iic_release_bus(sc->sc_i2c, 0);
+}
+
+static uint8_t
+escodec_read(struct escodec_softc *sc, uint8_t reg)
+{
+       uint8_t val;
+
+       if (iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr, reg, &val, I2C_F_POLL) != 0)
+               val = 0xff;
+
+       return val;
+}
+
+static void
+escodec_write(struct escodec_softc *sc, uint8_t reg, uint8_t val)
+{
+       (void)iic_smbus_write_byte(sc->sc_i2c, sc->sc_addr, reg, val, I2C_F_POLL);
+}
+
+enum escodec_mixer_ctrl {
+       ESCODEC_OUTPUT_CLASS,
+       ESCODEC_INPUT_CLASS,
+       ESCODEC_OUTPUT_MASTER,
+       ESCODEC_INPUT_DAC,
+       ESCODEC_INPUT_DAC_MUTE,
+       ESCODEC_INPUT_HEADPHONE,
+       ESCODEC_INPUT_MIXEROUT,
+       ESCODEC_INPUT_MIXEROUT_MUTE,
+
+       ESCODEC_MIXER_CTRL_LAST
+};
+
+enum escodec_mixer_type {
+       ESCODEC_MIXER_CLASS,
+       ESCODEC_MIXER_SWVOL,
+       ESCODEC_MIXER_AMPLIFIER,
+       ESCODEC_MIXER_ATTENUATOR,
+       ESCODEC_MIXER_MUTE,
+};
+
+static const struct escodec_mixer {
+       const char *                    name;
+       int                             mixer_class;
+       int                             prev, next;
+       enum escodec_mixer_ctrl         ctrl;
+       enum escodec_mixer_type         type;
+       u_int                           reg[2];
+       uint8_t                         mask[2];
+       uint8_t                         maxval;
+} escodec_mixers[ESCODEC_MIXER_CTRL_LAST] = {
+       /*
+        * Mixer classes
+        */
+       [ESCODEC_OUTPUT_CLASS] = {
+               .name = AudioCoutputs,
+               .type = ESCODEC_MIXER_CLASS,
+       },
+       [ESCODEC_INPUT_CLASS] = {
+               .name = AudioCinputs,
+               .type = ESCODEC_MIXER_CLASS,
+       },
+
+       /*
+        * Software master volume
+        */
+       [ESCODEC_OUTPUT_MASTER] = {
+               .name = AudioNmaster,
+               .mixer_class = ESCODEC_OUTPUT_CLASS,
+               .prev = AUDIO_MIXER_LAST,
+               .next = AUDIO_MIXER_LAST,
+               .type = ESCODEC_MIXER_SWVOL,
+       },
+
+       /*
+        * Stereo DAC
+        */
+       [ESCODEC_INPUT_DAC] = {
+               .name = AudioNdac,
+               .mixer_class = ESCODEC_INPUT_CLASS,
+               .prev = AUDIO_MIXER_LAST,
+               .next = ESCODEC_INPUT_DAC_MUTE,
+               .type = ESCODEC_MIXER_ATTENUATOR,
+               .reg = {
+                       [AUDIO_MIXER_LEVEL_LEFT] = ESCODEC_DACVOL_L_REG,
+                       [AUDIO_MIXER_LEVEL_RIGHT] = ESCODEC_DACVOL_R_REG,
+               },
+               .mask = {
+                       [AUDIO_MIXER_LEVEL_LEFT] = DACVOL_L_DACVOLUME,
+                       [AUDIO_MIXER_LEVEL_RIGHT] = DACVOL_R_DACVOLUME,
+               },
+               .maxval = 0xc0,
+       },
+       [ESCODEC_INPUT_DAC_MUTE] = {
+               .name = AudioNmute,
+               .mixer_class = ESCODEC_INPUT_CLASS,
+               .prev = ESCODEC_INPUT_DAC,
+               .next = AUDIO_MIXER_LAST,
+               .type = ESCODEC_MIXER_MUTE,
+               .reg = {
+                       [AUDIO_MIXER_LEVEL_MONO] = ESCODEC_DACCTL1_REG,
+               },
+               .mask = {
+                       [AUDIO_MIXER_LEVEL_MONO] = DACCTL1_MUTE,
+               }
+       },
+
+       /*
+        * Charge Pump Headphones
+        */
+       [ESCODEC_INPUT_HEADPHONE] = {
+               .name = AudioNheadphone,
+               .mixer_class = ESCODEC_INPUT_CLASS,
+               .prev = AUDIO_MIXER_LAST,
+               .next = AUDIO_MIXER_LAST,
+               .type = ESCODEC_MIXER_ATTENUATOR,
+               .reg = {
+                       [AUDIO_MIXER_LEVEL_LEFT] = ESCODEC_HPVOL_REG,
+                       [AUDIO_MIXER_LEVEL_RIGHT] = ESCODEC_HPVOL_REG,
+               },
+               .mask = {
+                       [AUDIO_MIXER_LEVEL_LEFT] = HPVOL_HPLVOL,
+                       [AUDIO_MIXER_LEVEL_RIGHT] = HPVOL_HPRVOL,
+               }
+       },
+
+       /*
+        * Headphone mixer
+        */
+       [ESCODEC_INPUT_MIXEROUT] = {
+               .name = AudioNmixerout,
+               .mixer_class = ESCODEC_INPUT_CLASS,
+               .prev = AUDIO_MIXER_LAST,
+               .next = ESCODEC_INPUT_MIXEROUT_MUTE,
+               .type = ESCODEC_MIXER_AMPLIFIER,
+               .reg = {
+                       [AUDIO_MIXER_LEVEL_LEFT] = ESCODEC_HPMIXVOL_REG,
+                       [AUDIO_MIXER_LEVEL_RIGHT] = ESCODEC_HPMIXVOL_REG,
+               },
+               .mask = {
+                       [AUDIO_MIXER_LEVEL_LEFT] = HPMIXVOL_LHPMIXVOL,
+                       [AUDIO_MIXER_LEVEL_RIGHT] = HPMIXVOL_RHPMIXVOL
+               },
+               /*
+                * Datasheet says this field goes up to 0xb, but values
+                * above 0x4 result in noisy output in practice.
+                */
+               .maxval = 0x4,
+       },
+       [ESCODEC_INPUT_MIXEROUT_MUTE] = {
+               .name = AudioNmute,
+               .mixer_class = ESCODEC_INPUT_CLASS,
+               .prev = ESCODEC_INPUT_MIXEROUT,
+               .next = AUDIO_MIXER_LAST,
+               .type = ESCODEC_MIXER_MUTE,
+               .reg = {
+                       [AUDIO_MIXER_LEVEL_MONO] = ESCODEC_HPMIX_REG,
+               },
+               .mask = {
+                       [AUDIO_MIXER_LEVEL_MONO] = HPMIX_LHPMIX_MUTE | HPMIX_RHPMIX_MUTE,
+               }
+       },
+};
+
+static void



Home | Main Index | Thread Index | Old Index