linux/sound/soc/codecs/es8316.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * es8316.c -- es8316 ALSA SoC audio driver
   4 * Copyright Everest Semiconductor Co.,Ltd
   5 *
   6 * Authors: David Yang <yangxiaohua@everest-semi.com>,
   7 *          Daniel Drake <drake@endlessm.com>
   8 */
   9
  10#include <linux/module.h>
  11#include <linux/acpi.h>
  12#include <linux/clk.h>
  13#include <linux/delay.h>
  14#include <linux/i2c.h>
  15#include <linux/mod_devicetable.h>
  16#include <linux/mutex.h>
  17#include <linux/regmap.h>
  18#include <sound/pcm.h>
  19#include <sound/pcm_params.h>
  20#include <sound/soc.h>
  21#include <sound/soc-dapm.h>
  22#include <sound/tlv.h>
  23#include <sound/jack.h>
  24#include "es8316.h"
  25
  26/* In slave mode at single speed, the codec is documented as accepting 5
  27 * MCLK/LRCK ratios, but we also add ratio 400, which is commonly used on
  28 * Intel Cherry Trail platforms (19.2MHz MCLK, 48kHz LRCK).
  29 */
  30#define NR_SUPPORTED_MCLK_LRCK_RATIOS 6
  31static const unsigned int supported_mclk_lrck_ratios[] = {
  32        256, 384, 400, 512, 768, 1024
  33};
  34
  35struct es8316_priv {
  36        struct mutex lock;
  37        struct clk *mclk;
  38        struct regmap *regmap;
  39        struct snd_soc_component *component;
  40        struct snd_soc_jack *jack;
  41        int irq;
  42        unsigned int sysclk;
  43        unsigned int allowed_rates[NR_SUPPORTED_MCLK_LRCK_RATIOS];
  44        struct snd_pcm_hw_constraint_list sysclk_constraints;
  45        bool jd_inverted;
  46};
  47
  48/*
  49 * ES8316 controls
  50 */
  51static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9600, 50, 1);
  52static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9600, 50, 1);
  53static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_max_gain_tlv, -650, 150, 0);
  54static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_min_gain_tlv, -1200, 150, 0);
  55static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_target_tlv, -1650, 150, 0);
  56static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpmixer_gain_tlv,
  57        0, 4, TLV_DB_SCALE_ITEM(-1200, 150, 0),
  58        8, 11, TLV_DB_SCALE_ITEM(-450, 150, 0),
  59);
  60
  61static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(adc_pga_gain_tlv,
  62        0, 0, TLV_DB_SCALE_ITEM(-350, 0, 0),
  63        1, 1, TLV_DB_SCALE_ITEM(0, 0, 0),
  64        2, 2, TLV_DB_SCALE_ITEM(250, 0, 0),
  65        3, 3, TLV_DB_SCALE_ITEM(450, 0, 0),
  66        4, 4, TLV_DB_SCALE_ITEM(700, 0, 0),
  67        5, 5, TLV_DB_SCALE_ITEM(1000, 0, 0),
  68        6, 6, TLV_DB_SCALE_ITEM(1300, 0, 0),
  69        7, 7, TLV_DB_SCALE_ITEM(1600, 0, 0),
  70        8, 8, TLV_DB_SCALE_ITEM(1800, 0, 0),
  71        9, 9, TLV_DB_SCALE_ITEM(2100, 0, 0),
  72        10, 10, TLV_DB_SCALE_ITEM(2400, 0, 0),
  73);
  74
  75static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpout_vol_tlv,
  76        0, 0, TLV_DB_SCALE_ITEM(-4800, 0, 0),
  77        1, 3, TLV_DB_SCALE_ITEM(-2400, 1200, 0),
  78);
  79
  80static const char * const ng_type_txt[] =
  81        { "Constant PGA Gain", "Mute ADC Output" };
  82static const struct soc_enum ng_type =
  83        SOC_ENUM_SINGLE(ES8316_ADC_ALC_NG, 6, 2, ng_type_txt);
  84
  85static const char * const adcpol_txt[] = { "Normal", "Invert" };
  86static const struct soc_enum adcpol =
  87        SOC_ENUM_SINGLE(ES8316_ADC_MUTE, 1, 2, adcpol_txt);
  88static const char *const dacpol_txt[] =
  89        { "Normal", "R Invert", "L Invert", "L + R Invert" };
  90static const struct soc_enum dacpol =
  91        SOC_ENUM_SINGLE(ES8316_DAC_SET1, 0, 4, dacpol_txt);
  92
  93static const struct snd_kcontrol_new es8316_snd_controls[] = {
  94        SOC_DOUBLE_TLV("Headphone Playback Volume", ES8316_CPHP_ICAL_VOL,
  95                       4, 0, 3, 1, hpout_vol_tlv),
  96        SOC_DOUBLE_TLV("Headphone Mixer Volume", ES8316_HPMIX_VOL,
  97                       4, 0, 11, 0, hpmixer_gain_tlv),
  98
  99        SOC_ENUM("Playback Polarity", dacpol),
 100        SOC_DOUBLE_R_TLV("DAC Playback Volume", ES8316_DAC_VOLL,
 101                         ES8316_DAC_VOLR, 0, 0xc0, 1, dac_vol_tlv),
 102        SOC_SINGLE("DAC Soft Ramp Switch", ES8316_DAC_SET1, 4, 1, 1),
 103        SOC_SINGLE("DAC Soft Ramp Rate", ES8316_DAC_SET1, 2, 4, 0),
 104        SOC_SINGLE("DAC Notch Filter Switch", ES8316_DAC_SET2, 6, 1, 0),
 105        SOC_SINGLE("DAC Double Fs Switch", ES8316_DAC_SET2, 7, 1, 0),
 106        SOC_SINGLE("DAC Stereo Enhancement", ES8316_DAC_SET3, 0, 7, 0),
 107        SOC_SINGLE("DAC Mono Mix Switch", ES8316_DAC_SET3, 3, 1, 0),
 108
 109        SOC_ENUM("Capture Polarity", adcpol),
 110        SOC_SINGLE("Mic Boost Switch", ES8316_ADC_D2SEPGA, 0, 1, 0),
 111        SOC_SINGLE_TLV("ADC Capture Volume", ES8316_ADC_VOLUME,
 112                       0, 0xc0, 1, adc_vol_tlv),
 113        SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8316_ADC_PGAGAIN,
 114                       4, 10, 0, adc_pga_gain_tlv),
 115        SOC_SINGLE("ADC Soft Ramp Switch", ES8316_ADC_MUTE, 4, 1, 0),
 116        SOC_SINGLE("ADC Double Fs Switch", ES8316_ADC_DMIC, 4, 1, 0),
 117
 118        SOC_SINGLE("ALC Capture Switch", ES8316_ADC_ALC1, 6, 1, 0),
 119        SOC_SINGLE_TLV("ALC Capture Max Volume", ES8316_ADC_ALC1, 0, 28, 0,
 120                       alc_max_gain_tlv),
 121        SOC_SINGLE_TLV("ALC Capture Min Volume", ES8316_ADC_ALC2, 0, 28, 0,
 122                       alc_min_gain_tlv),
 123        SOC_SINGLE_TLV("ALC Capture Target Volume", ES8316_ADC_ALC3, 4, 10, 0,
 124                       alc_target_tlv),
 125        SOC_SINGLE("ALC Capture Hold Time", ES8316_ADC_ALC3, 0, 10, 0),
 126        SOC_SINGLE("ALC Capture Decay Time", ES8316_ADC_ALC4, 4, 10, 0),
 127        SOC_SINGLE("ALC Capture Attack Time", ES8316_ADC_ALC4, 0, 10, 0),
 128        SOC_SINGLE("ALC Capture Noise Gate Switch", ES8316_ADC_ALC_NG,
 129                   5, 1, 0),
 130        SOC_SINGLE("ALC Capture Noise Gate Threshold", ES8316_ADC_ALC_NG,
 131                   0, 31, 0),
 132        SOC_ENUM("ALC Capture Noise Gate Type", ng_type),
 133};
 134
 135/* Analog Input Mux */
 136static const char * const es8316_analog_in_txt[] = {
 137                "lin1-rin1",
 138                "lin2-rin2",
 139                "lin1-rin1 with 20db Boost",
 140                "lin2-rin2 with 20db Boost"
 141};
 142static const unsigned int es8316_analog_in_values[] = { 0, 1, 2, 3 };
 143static const struct soc_enum es8316_analog_input_enum =
 144        SOC_VALUE_ENUM_SINGLE(ES8316_ADC_PDN_LINSEL, 4, 3,
 145                              ARRAY_SIZE(es8316_analog_in_txt),
 146                              es8316_analog_in_txt,
 147                              es8316_analog_in_values);
 148static const struct snd_kcontrol_new es8316_analog_in_mux_controls =
 149        SOC_DAPM_ENUM("Route", es8316_analog_input_enum);
 150
 151static const char * const es8316_dmic_txt[] = {
 152                "dmic disable",
 153                "dmic data at high level",
 154                "dmic data at low level",
 155};
 156static const unsigned int es8316_dmic_values[] = { 0, 1, 2 };
 157static const struct soc_enum es8316_dmic_src_enum =
 158        SOC_VALUE_ENUM_SINGLE(ES8316_ADC_DMIC, 0, 3,
 159                              ARRAY_SIZE(es8316_dmic_txt),
 160                              es8316_dmic_txt,
 161                              es8316_dmic_values);
 162static const struct snd_kcontrol_new es8316_dmic_src_controls =
 163        SOC_DAPM_ENUM("Route", es8316_dmic_src_enum);
 164
 165/* hp mixer mux */
 166static const char * const es8316_hpmux_texts[] = {
 167        "lin1-rin1",
 168        "lin2-rin2",
 169        "lin-rin with Boost",
 170        "lin-rin with Boost and PGA"
 171};
 172
 173static SOC_ENUM_SINGLE_DECL(es8316_left_hpmux_enum, ES8316_HPMIX_SEL,
 174        4, es8316_hpmux_texts);
 175
 176static const struct snd_kcontrol_new es8316_left_hpmux_controls =
 177        SOC_DAPM_ENUM("Route", es8316_left_hpmux_enum);
 178
 179static SOC_ENUM_SINGLE_DECL(es8316_right_hpmux_enum, ES8316_HPMIX_SEL,
 180        0, es8316_hpmux_texts);
 181
 182static const struct snd_kcontrol_new es8316_right_hpmux_controls =
 183        SOC_DAPM_ENUM("Route", es8316_right_hpmux_enum);
 184
 185/* headphone Output Mixer */
 186static const struct snd_kcontrol_new es8316_out_left_mix[] = {
 187        SOC_DAPM_SINGLE("LLIN Switch", ES8316_HPMIX_SWITCH, 6, 1, 0),
 188        SOC_DAPM_SINGLE("Left DAC Switch", ES8316_HPMIX_SWITCH, 7, 1, 0),
 189};
 190static const struct snd_kcontrol_new es8316_out_right_mix[] = {
 191        SOC_DAPM_SINGLE("RLIN Switch", ES8316_HPMIX_SWITCH, 2, 1, 0),
 192        SOC_DAPM_SINGLE("Right DAC Switch", ES8316_HPMIX_SWITCH, 3, 1, 0),
 193};
 194
 195/* DAC data source mux */
 196static const char * const es8316_dacsrc_texts[] = {
 197        "LDATA TO LDAC, RDATA TO RDAC",
 198        "LDATA TO LDAC, LDATA TO RDAC",
 199        "RDATA TO LDAC, RDATA TO RDAC",
 200        "RDATA TO LDAC, LDATA TO RDAC",
 201};
 202
 203static SOC_ENUM_SINGLE_DECL(es8316_dacsrc_mux_enum, ES8316_DAC_SET1,
 204        6, es8316_dacsrc_texts);
 205
 206static const struct snd_kcontrol_new es8316_dacsrc_mux_controls =
 207        SOC_DAPM_ENUM("Route", es8316_dacsrc_mux_enum);
 208
 209static const struct snd_soc_dapm_widget es8316_dapm_widgets[] = {
 210        SND_SOC_DAPM_SUPPLY("Bias", ES8316_SYS_PDN, 3, 1, NULL, 0),
 211        SND_SOC_DAPM_SUPPLY("Analog power", ES8316_SYS_PDN, 4, 1, NULL, 0),
 212        SND_SOC_DAPM_SUPPLY("Mic Bias", ES8316_SYS_PDN, 5, 1, NULL, 0),
 213
 214        SND_SOC_DAPM_INPUT("DMIC"),
 215        SND_SOC_DAPM_INPUT("MIC1"),
 216        SND_SOC_DAPM_INPUT("MIC2"),
 217
 218        /* Input Mux */
 219        SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
 220                         &es8316_analog_in_mux_controls),
 221
 222        SND_SOC_DAPM_SUPPLY("ADC Vref", ES8316_SYS_PDN, 1, 1, NULL, 0),
 223        SND_SOC_DAPM_SUPPLY("ADC bias", ES8316_SYS_PDN, 2, 1, NULL, 0),
 224        SND_SOC_DAPM_SUPPLY("ADC Clock", ES8316_CLKMGR_CLKSW, 3, 0, NULL, 0),
 225        SND_SOC_DAPM_PGA("Line input PGA", ES8316_ADC_PDN_LINSEL,
 226                         7, 1, NULL, 0),
 227        SND_SOC_DAPM_ADC("Mono ADC", NULL, ES8316_ADC_PDN_LINSEL, 6, 1),
 228        SND_SOC_DAPM_MUX("Digital Mic Mux", SND_SOC_NOPM, 0, 0,
 229                         &es8316_dmic_src_controls),
 230
 231        /* Digital Interface */
 232        SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture",  1,
 233                             ES8316_SERDATA_ADC, 6, 1),
 234        SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0,
 235                            SND_SOC_NOPM, 0, 0),
 236
 237        SND_SOC_DAPM_MUX("DAC Source Mux", SND_SOC_NOPM, 0, 0,
 238                         &es8316_dacsrc_mux_controls),
 239
 240        SND_SOC_DAPM_SUPPLY("DAC Vref", ES8316_SYS_PDN, 0, 1, NULL, 0),
 241        SND_SOC_DAPM_SUPPLY("DAC Clock", ES8316_CLKMGR_CLKSW, 2, 0, NULL, 0),
 242        SND_SOC_DAPM_DAC("Right DAC", NULL, ES8316_DAC_PDN, 0, 1),
 243        SND_SOC_DAPM_DAC("Left DAC", NULL, ES8316_DAC_PDN, 4, 1),
 244
 245        /* Headphone Output Side */
 246        SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
 247                         &es8316_left_hpmux_controls),
 248        SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
 249                         &es8316_right_hpmux_controls),
 250        SND_SOC_DAPM_MIXER("Left Headphone Mixer", ES8316_HPMIX_PDN,
 251                           5, 1, &es8316_out_left_mix[0],
 252                           ARRAY_SIZE(es8316_out_left_mix)),
 253        SND_SOC_DAPM_MIXER("Right Headphone Mixer", ES8316_HPMIX_PDN,
 254                           1, 1, &es8316_out_right_mix[0],
 255                           ARRAY_SIZE(es8316_out_right_mix)),
 256        SND_SOC_DAPM_PGA("Left Headphone Mixer Out", ES8316_HPMIX_PDN,
 257                         4, 1, NULL, 0),
 258        SND_SOC_DAPM_PGA("Right Headphone Mixer Out", ES8316_HPMIX_PDN,
 259                         0, 1, NULL, 0),
 260
 261        SND_SOC_DAPM_OUT_DRV("Left Headphone Charge Pump", ES8316_CPHP_OUTEN,
 262                             6, 0, NULL, 0),
 263        SND_SOC_DAPM_OUT_DRV("Right Headphone Charge Pump", ES8316_CPHP_OUTEN,
 264                             2, 0, NULL, 0),
 265        SND_SOC_DAPM_SUPPLY("Headphone Charge Pump", ES8316_CPHP_PDN2,
 266                            5, 1, NULL, 0),
 267        SND_SOC_DAPM_SUPPLY("Headphone Charge Pump Clock", ES8316_CLKMGR_CLKSW,
 268                            4, 0, NULL, 0),
 269
 270        SND_SOC_DAPM_OUT_DRV("Left Headphone Driver", ES8316_CPHP_OUTEN,
 271                             5, 0, NULL, 0),
 272        SND_SOC_DAPM_OUT_DRV("Right Headphone Driver", ES8316_CPHP_OUTEN,
 273                             1, 0, NULL, 0),
 274        SND_SOC_DAPM_SUPPLY("Headphone Out", ES8316_CPHP_PDN1, 2, 1, NULL, 0),
 275
 276        /* pdn_Lical and pdn_Rical bits are documented as Reserved, but must
 277         * be explicitly unset in order to enable HP output
 278         */
 279        SND_SOC_DAPM_SUPPLY("Left Headphone ical", ES8316_CPHP_ICAL_VOL,
 280                            7, 1, NULL, 0),
 281        SND_SOC_DAPM_SUPPLY("Right Headphone ical", ES8316_CPHP_ICAL_VOL,
 282                            3, 1, NULL, 0),
 283
 284        SND_SOC_DAPM_OUTPUT("HPOL"),
 285        SND_SOC_DAPM_OUTPUT("HPOR"),
 286};
 287
 288static const struct snd_soc_dapm_route es8316_dapm_routes[] = {
 289        /* Recording */
 290        {"MIC1", NULL, "Mic Bias"},
 291        {"MIC2", NULL, "Mic Bias"},
 292        {"MIC1", NULL, "Bias"},
 293        {"MIC2", NULL, "Bias"},
 294        {"MIC1", NULL, "Analog power"},
 295        {"MIC2", NULL, "Analog power"},
 296
 297        {"Differential Mux", "lin1-rin1", "MIC1"},
 298        {"Differential Mux", "lin2-rin2", "MIC2"},
 299        {"Line input PGA", NULL, "Differential Mux"},
 300
 301        {"Mono ADC", NULL, "ADC Clock"},
 302        {"Mono ADC", NULL, "ADC Vref"},
 303        {"Mono ADC", NULL, "ADC bias"},
 304        {"Mono ADC", NULL, "Line input PGA"},
 305
 306        /* It's not clear why, but to avoid recording only silence,
 307         * the DAC clock must be running for the ADC to work.
 308         */
 309        {"Mono ADC", NULL, "DAC Clock"},
 310
 311        {"Digital Mic Mux", "dmic disable", "Mono ADC"},
 312
 313        {"I2S OUT", NULL, "Digital Mic Mux"},
 314
 315        /* Playback */
 316        {"DAC Source Mux", "LDATA TO LDAC, RDATA TO RDAC", "I2S IN"},
 317
 318        {"Left DAC", NULL, "DAC Clock"},
 319        {"Right DAC", NULL, "DAC Clock"},
 320
 321        {"Left DAC", NULL, "DAC Vref"},
 322        {"Right DAC", NULL, "DAC Vref"},
 323
 324        {"Left DAC", NULL, "DAC Source Mux"},
 325        {"Right DAC", NULL, "DAC Source Mux"},
 326
 327        {"Left Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
 328        {"Right Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
 329
 330        {"Left Headphone Mixer", "LLIN Switch", "Left Headphone Mux"},
 331        {"Left Headphone Mixer", "Left DAC Switch", "Left DAC"},
 332
 333        {"Right Headphone Mixer", "RLIN Switch", "Right Headphone Mux"},
 334        {"Right Headphone Mixer", "Right DAC Switch", "Right DAC"},
 335
 336        {"Left Headphone Mixer Out", NULL, "Left Headphone Mixer"},
 337        {"Right Headphone Mixer Out", NULL, "Right Headphone Mixer"},
 338
 339        {"Left Headphone Charge Pump", NULL, "Left Headphone Mixer Out"},
 340        {"Right Headphone Charge Pump", NULL, "Right Headphone Mixer Out"},
 341
 342        {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump"},
 343        {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump"},
 344
 345        {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
 346        {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
 347
 348        {"Left Headphone Driver", NULL, "Left Headphone Charge Pump"},
 349        {"Right Headphone Driver", NULL, "Right Headphone Charge Pump"},
 350
 351        {"HPOL", NULL, "Left Headphone Driver"},
 352        {"HPOR", NULL, "Right Headphone Driver"},
 353
 354        {"HPOL", NULL, "Left Headphone ical"},
 355        {"HPOR", NULL, "Right Headphone ical"},
 356
 357        {"Headphone Out", NULL, "Bias"},
 358        {"Headphone Out", NULL, "Analog power"},
 359        {"HPOL", NULL, "Headphone Out"},
 360        {"HPOR", NULL, "Headphone Out"},
 361};
 362
 363static int es8316_set_dai_sysclk(struct snd_soc_dai *codec_dai,
 364                                 int clk_id, unsigned int freq, int dir)
 365{
 366        struct snd_soc_component *component = codec_dai->component;
 367        struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
 368        int i, ret;
 369        int count = 0;
 370
 371        es8316->sysclk = freq;
 372
 373        if (freq == 0) {
 374                es8316->sysclk_constraints.list = NULL;
 375                es8316->sysclk_constraints.count = 0;
 376
 377                return 0;
 378        }
 379
 380        ret = clk_set_rate(es8316->mclk, freq);
 381        if (ret)
 382                return ret;
 383
 384        /* Limit supported sample rates to ones that can be autodetected
 385         * by the codec running in slave mode.
 386         */
 387        for (i = 0; i < NR_SUPPORTED_MCLK_LRCK_RATIOS; i++) {
 388                const unsigned int ratio = supported_mclk_lrck_ratios[i];
 389
 390                if (freq % ratio == 0)
 391                        es8316->allowed_rates[count++] = freq / ratio;
 392        }
 393
 394        es8316->sysclk_constraints.list = es8316->allowed_rates;
 395        es8316->sysclk_constraints.count = count;
 396
 397        return 0;
 398}
 399
 400static int es8316_set_dai_fmt(struct snd_soc_dai *codec_dai,
 401                              unsigned int fmt)
 402{
 403        struct snd_soc_component *component = codec_dai->component;
 404        u8 serdata1 = 0;
 405        u8 serdata2 = 0;
 406        u8 clksw;
 407        u8 mask;
 408
 409        if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) {
 410                dev_err(component->dev, "Codec driver only supports slave mode\n");
 411                return -EINVAL;
 412        }
 413
 414        if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_I2S) {
 415                dev_err(component->dev, "Codec driver only supports I2S format\n");
 416                return -EINVAL;
 417        }
 418
 419        /* Clock inversion */
 420        switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
 421        case SND_SOC_DAIFMT_NB_NF:
 422                break;
 423        case SND_SOC_DAIFMT_IB_IF:
 424                serdata1 |= ES8316_SERDATA1_BCLK_INV;
 425                serdata2 |= ES8316_SERDATA2_ADCLRP;
 426                break;
 427        case SND_SOC_DAIFMT_IB_NF:
 428                serdata1 |= ES8316_SERDATA1_BCLK_INV;
 429                break;
 430        case SND_SOC_DAIFMT_NB_IF:
 431                serdata2 |= ES8316_SERDATA2_ADCLRP;
 432                break;
 433        default:
 434                return -EINVAL;
 435        }
 436
 437        mask = ES8316_SERDATA1_MASTER | ES8316_SERDATA1_BCLK_INV;
 438        snd_soc_component_update_bits(component, ES8316_SERDATA1, mask, serdata1);
 439
 440        mask = ES8316_SERDATA2_FMT_MASK | ES8316_SERDATA2_ADCLRP;
 441        snd_soc_component_update_bits(component, ES8316_SERDATA_ADC, mask, serdata2);
 442        snd_soc_component_update_bits(component, ES8316_SERDATA_DAC, mask, serdata2);
 443
 444        /* Enable BCLK and MCLK inputs in slave mode */
 445        clksw = ES8316_CLKMGR_CLKSW_MCLK_ON | ES8316_CLKMGR_CLKSW_BCLK_ON;
 446        snd_soc_component_update_bits(component, ES8316_CLKMGR_CLKSW, clksw, clksw);
 447
 448        return 0;
 449}
 450
 451static int es8316_pcm_startup(struct snd_pcm_substream *substream,
 452                              struct snd_soc_dai *dai)
 453{
 454        struct snd_soc_component *component = dai->component;
 455        struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
 456
 457        if (es8316->sysclk_constraints.list)
 458                snd_pcm_hw_constraint_list(substream->runtime, 0,
 459                                           SNDRV_PCM_HW_PARAM_RATE,
 460                                           &es8316->sysclk_constraints);
 461
 462        return 0;
 463}
 464
 465static int es8316_pcm_hw_params(struct snd_pcm_substream *substream,
 466                                struct snd_pcm_hw_params *params,
 467                                struct snd_soc_dai *dai)
 468{
 469        struct snd_soc_component *component = dai->component;
 470        struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
 471        u8 wordlen = 0;
 472        int i;
 473
 474        /* Validate supported sample rates that are autodetected from MCLK */
 475        for (i = 0; i < NR_SUPPORTED_MCLK_LRCK_RATIOS; i++) {
 476                const unsigned int ratio = supported_mclk_lrck_ratios[i];
 477
 478                if (es8316->sysclk % ratio != 0)
 479                        continue;
 480                if (es8316->sysclk / ratio == params_rate(params))
 481                        break;
 482        }
 483        if (i == NR_SUPPORTED_MCLK_LRCK_RATIOS)
 484                return -EINVAL;
 485
 486        switch (params_format(params)) {
 487        case SNDRV_PCM_FORMAT_S16_LE:
 488                wordlen = ES8316_SERDATA2_LEN_16;
 489                break;
 490        case SNDRV_PCM_FORMAT_S20_3LE:
 491                wordlen = ES8316_SERDATA2_LEN_20;
 492                break;
 493        case SNDRV_PCM_FORMAT_S24_LE:
 494                wordlen = ES8316_SERDATA2_LEN_24;
 495                break;
 496        case SNDRV_PCM_FORMAT_S32_LE:
 497                wordlen = ES8316_SERDATA2_LEN_32;
 498                break;
 499        default:
 500                return -EINVAL;
 501        }
 502
 503        snd_soc_component_update_bits(component, ES8316_SERDATA_DAC,
 504                            ES8316_SERDATA2_LEN_MASK, wordlen);
 505        snd_soc_component_update_bits(component, ES8316_SERDATA_ADC,
 506                            ES8316_SERDATA2_LEN_MASK, wordlen);
 507        return 0;
 508}
 509
 510static int es8316_mute(struct snd_soc_dai *dai, int mute, int direction)
 511{
 512        snd_soc_component_update_bits(dai->component, ES8316_DAC_SET1, 0x20,
 513                            mute ? 0x20 : 0);
 514        return 0;
 515}
 516
 517#define ES8316_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
 518                        SNDRV_PCM_FMTBIT_S24_LE)
 519
 520static const struct snd_soc_dai_ops es8316_ops = {
 521        .startup = es8316_pcm_startup,
 522        .hw_params = es8316_pcm_hw_params,
 523        .set_fmt = es8316_set_dai_fmt,
 524        .set_sysclk = es8316_set_dai_sysclk,
 525        .mute_stream = es8316_mute,
 526        .no_capture_mute = 1,
 527};
 528
 529static struct snd_soc_dai_driver es8316_dai = {
 530        .name = "ES8316 HiFi",
 531        .playback = {
 532                .stream_name = "Playback",
 533                .channels_min = 1,
 534                .channels_max = 2,
 535                .rates = SNDRV_PCM_RATE_8000_48000,
 536                .formats = ES8316_FORMATS,
 537        },
 538        .capture = {
 539                .stream_name = "Capture",
 540                .channels_min = 1,
 541                .channels_max = 2,
 542                .rates = SNDRV_PCM_RATE_8000_48000,
 543                .formats = ES8316_FORMATS,
 544        },
 545        .ops = &es8316_ops,
 546        .symmetric_rates = 1,
 547};
 548
 549static void es8316_enable_micbias_for_mic_gnd_short_detect(
 550        struct snd_soc_component *component)
 551{
 552        struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
 553
 554        snd_soc_dapm_mutex_lock(dapm);
 555        snd_soc_dapm_force_enable_pin_unlocked(dapm, "Bias");
 556        snd_soc_dapm_force_enable_pin_unlocked(dapm, "Analog power");
 557        snd_soc_dapm_force_enable_pin_unlocked(dapm, "Mic Bias");
 558        snd_soc_dapm_sync_unlocked(dapm);
 559        snd_soc_dapm_mutex_unlock(dapm);
 560
 561        msleep(20);
 562}
 563
 564static void es8316_disable_micbias_for_mic_gnd_short_detect(
 565        struct snd_soc_component *component)
 566{
 567        struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
 568
 569        snd_soc_dapm_mutex_lock(dapm);
 570        snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Bias");
 571        snd_soc_dapm_disable_pin_unlocked(dapm, "Analog power");
 572        snd_soc_dapm_disable_pin_unlocked(dapm, "Bias");
 573        snd_soc_dapm_sync_unlocked(dapm);
 574        snd_soc_dapm_mutex_unlock(dapm);
 575}
 576
 577static irqreturn_t es8316_irq(int irq, void *data)
 578{
 579        struct es8316_priv *es8316 = data;
 580        struct snd_soc_component *comp = es8316->component;
 581        unsigned int flags;
 582
 583        mutex_lock(&es8316->lock);
 584
 585        regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
 586        if (flags == 0x00)
 587                goto out; /* Powered-down / reset */
 588
 589        /* Catch spurious IRQ before set_jack is called */
 590        if (!es8316->jack)
 591                goto out;
 592
 593        if (es8316->jd_inverted)
 594                flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
 595
 596        dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
 597        if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
 598                /* Jack removed, or spurious IRQ? */
 599                if (es8316->jack->status & SND_JACK_MICROPHONE)
 600                        es8316_disable_micbias_for_mic_gnd_short_detect(comp);
 601
 602                if (es8316->jack->status & SND_JACK_HEADPHONE) {
 603                        snd_soc_jack_report(es8316->jack, 0,
 604                                            SND_JACK_HEADSET | SND_JACK_BTN_0);
 605                        dev_dbg(comp->dev, "jack unplugged\n");
 606                }
 607        } else if (!(es8316->jack->status & SND_JACK_HEADPHONE)) {
 608                /* Jack inserted, determine type */
 609                es8316_enable_micbias_for_mic_gnd_short_detect(comp);
 610                regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
 611                if (es8316->jd_inverted)
 612                        flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
 613                dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
 614                if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
 615                        /* Jack unplugged underneath us */
 616                        es8316_disable_micbias_for_mic_gnd_short_detect(comp);
 617                } else if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
 618                        /* Open, headset */
 619                        snd_soc_jack_report(es8316->jack,
 620                                            SND_JACK_HEADSET,
 621                                            SND_JACK_HEADSET);
 622                        /* Keep mic-gnd-short detection on for button press */
 623                } else {
 624                        /* Shorted, headphones */
 625                        snd_soc_jack_report(es8316->jack,
 626                                            SND_JACK_HEADPHONE,
 627                                            SND_JACK_HEADSET);
 628                        /* No longer need mic-gnd-short detection */
 629                        es8316_disable_micbias_for_mic_gnd_short_detect(comp);
 630                }
 631        } else if (es8316->jack->status & SND_JACK_MICROPHONE) {
 632                /* Interrupt while jack inserted, report button state */
 633                if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
 634                        /* Open, button release */
 635                        snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
 636                } else {
 637                        /* Short, button press */
 638                        snd_soc_jack_report(es8316->jack,
 639                                            SND_JACK_BTN_0,
 640                                            SND_JACK_BTN_0);
 641                }
 642        }
 643
 644out:
 645        mutex_unlock(&es8316->lock);
 646        return IRQ_HANDLED;
 647}
 648
 649static void es8316_enable_jack_detect(struct snd_soc_component *component,
 650                                      struct snd_soc_jack *jack)
 651{
 652        struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
 653
 654        /*
 655         * Init es8316->jd_inverted here and not in the probe, as we cannot
 656         * guarantee that the bytchr-es8316 driver, which might set this
 657         * property, will probe before us.
 658         */
 659        es8316->jd_inverted = device_property_read_bool(component->dev,
 660                                                        "everest,jack-detect-inverted");
 661
 662        mutex_lock(&es8316->lock);
 663
 664        es8316->jack = jack;
 665
 666        if (es8316->jack->status & SND_JACK_MICROPHONE)
 667                es8316_enable_micbias_for_mic_gnd_short_detect(component);
 668
 669        snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
 670                                      ES8316_GPIO_ENABLE_INTERRUPT,
 671                                      ES8316_GPIO_ENABLE_INTERRUPT);
 672
 673        mutex_unlock(&es8316->lock);
 674
 675        /* Enable irq and sync initial jack state */
 676        enable_irq(es8316->irq);
 677        es8316_irq(es8316->irq, es8316);
 678}
 679
 680static void es8316_disable_jack_detect(struct snd_soc_component *component)
 681{
 682        struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
 683
 684        disable_irq(es8316->irq);
 685
 686        mutex_lock(&es8316->lock);
 687
 688        snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
 689                                      ES8316_GPIO_ENABLE_INTERRUPT, 0);
 690
 691        if (es8316->jack->status & SND_JACK_MICROPHONE) {
 692                es8316_disable_micbias_for_mic_gnd_short_detect(component);
 693                snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
 694        }
 695
 696        es8316->jack = NULL;
 697
 698        mutex_unlock(&es8316->lock);
 699}
 700
 701static int es8316_set_jack(struct snd_soc_component *component,
 702                           struct snd_soc_jack *jack, void *data)
 703{
 704        if (jack)
 705                es8316_enable_jack_detect(component, jack);
 706        else
 707                es8316_disable_jack_detect(component);
 708
 709        return 0;
 710}
 711
 712static int es8316_probe(struct snd_soc_component *component)
 713{
 714        struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
 715        int ret;
 716
 717        es8316->component = component;
 718
 719        es8316->mclk = devm_clk_get_optional(component->dev, "mclk");
 720        if (IS_ERR(es8316->mclk)) {
 721                dev_err(component->dev, "unable to get mclk\n");
 722                return PTR_ERR(es8316->mclk);
 723        }
 724        if (!es8316->mclk)
 725                dev_warn(component->dev, "assuming static mclk\n");
 726
 727        ret = clk_prepare_enable(es8316->mclk);
 728        if (ret) {
 729                dev_err(component->dev, "unable to enable mclk\n");
 730                return ret;
 731        }
 732
 733        /* Reset codec and enable current state machine */
 734        snd_soc_component_write(component, ES8316_RESET, 0x3f);
 735        usleep_range(5000, 5500);
 736        snd_soc_component_write(component, ES8316_RESET, ES8316_RESET_CSM_ON);
 737        msleep(30);
 738
 739        /*
 740         * Documentation is unclear, but this value from the vendor driver is
 741         * needed otherwise audio output is silent.
 742         */
 743        snd_soc_component_write(component, ES8316_SYS_VMIDSEL, 0xff);
 744
 745        /*
 746         * Documentation for this register is unclear and incomplete,
 747         * but here is a vendor-provided value that improves volume
 748         * and quality for Intel CHT platforms.
 749         */
 750        snd_soc_component_write(component, ES8316_CLKMGR_ADCOSR, 0x32);
 751
 752        return 0;
 753}
 754
 755static void es8316_remove(struct snd_soc_component *component)
 756{
 757        struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
 758
 759        clk_disable_unprepare(es8316->mclk);
 760}
 761
 762static const struct snd_soc_component_driver soc_component_dev_es8316 = {
 763        .probe                  = es8316_probe,
 764        .remove                 = es8316_remove,
 765        .set_jack               = es8316_set_jack,
 766        .controls               = es8316_snd_controls,
 767        .num_controls           = ARRAY_SIZE(es8316_snd_controls),
 768        .dapm_widgets           = es8316_dapm_widgets,
 769        .num_dapm_widgets       = ARRAY_SIZE(es8316_dapm_widgets),
 770        .dapm_routes            = es8316_dapm_routes,
 771        .num_dapm_routes        = ARRAY_SIZE(es8316_dapm_routes),
 772        .use_pmdown_time        = 1,
 773        .endianness             = 1,
 774        .non_legacy_dai_naming  = 1,
 775};
 776
 777static const struct regmap_range es8316_volatile_ranges[] = {
 778        regmap_reg_range(ES8316_GPIO_FLAG, ES8316_GPIO_FLAG),
 779};
 780
 781static const struct regmap_access_table es8316_volatile_table = {
 782        .yes_ranges     = es8316_volatile_ranges,
 783        .n_yes_ranges   = ARRAY_SIZE(es8316_volatile_ranges),
 784};
 785
 786static const struct regmap_config es8316_regmap = {
 787        .reg_bits = 8,
 788        .val_bits = 8,
 789        .max_register = 0x53,
 790        .volatile_table = &es8316_volatile_table,
 791        .cache_type = REGCACHE_RBTREE,
 792};
 793
 794static int es8316_i2c_probe(struct i2c_client *i2c_client,
 795                            const struct i2c_device_id *id)
 796{
 797        struct device *dev = &i2c_client->dev;
 798        struct es8316_priv *es8316;
 799        int ret;
 800
 801        es8316 = devm_kzalloc(&i2c_client->dev, sizeof(struct es8316_priv),
 802                              GFP_KERNEL);
 803        if (es8316 == NULL)
 804                return -ENOMEM;
 805
 806        i2c_set_clientdata(i2c_client, es8316);
 807
 808        es8316->regmap = devm_regmap_init_i2c(i2c_client, &es8316_regmap);
 809        if (IS_ERR(es8316->regmap))
 810                return PTR_ERR(es8316->regmap);
 811
 812        es8316->irq = i2c_client->irq;
 813        mutex_init(&es8316->lock);
 814
 815        ret = devm_request_threaded_irq(dev, es8316->irq, NULL, es8316_irq,
 816                                        IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
 817                                        "es8316", es8316);
 818        if (ret == 0) {
 819                /* Gets re-enabled by es8316_set_jack() */
 820                disable_irq(es8316->irq);
 821        } else {
 822                dev_warn(dev, "Failed to get IRQ %d: %d\n", es8316->irq, ret);
 823                es8316->irq = -ENXIO;
 824        }
 825
 826        return devm_snd_soc_register_component(&i2c_client->dev,
 827                                      &soc_component_dev_es8316,
 828                                      &es8316_dai, 1);
 829}
 830
 831static const struct i2c_device_id es8316_i2c_id[] = {
 832        {"es8316", 0 },
 833        {}
 834};
 835MODULE_DEVICE_TABLE(i2c, es8316_i2c_id);
 836
 837#ifdef CONFIG_OF
 838static const struct of_device_id es8316_of_match[] = {
 839        { .compatible = "everest,es8316", },
 840        {},
 841};
 842MODULE_DEVICE_TABLE(of, es8316_of_match);
 843#endif
 844
 845#ifdef CONFIG_ACPI
 846static const struct acpi_device_id es8316_acpi_match[] = {
 847        {"ESSX8316", 0},
 848        {},
 849};
 850MODULE_DEVICE_TABLE(acpi, es8316_acpi_match);
 851#endif
 852
 853static struct i2c_driver es8316_i2c_driver = {
 854        .driver = {
 855                .name                   = "es8316",
 856                .acpi_match_table       = ACPI_PTR(es8316_acpi_match),
 857                .of_match_table         = of_match_ptr(es8316_of_match),
 858        },
 859        .probe          = es8316_i2c_probe,
 860        .id_table       = es8316_i2c_id,
 861};
 862module_i2c_driver(es8316_i2c_driver);
 863
 864MODULE_DESCRIPTION("Everest Semi ES8316 ALSA SoC Codec Driver");
 865MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
 866MODULE_LICENSE("GPL v2");
 867