linux/sound/soc/codecs/da7213.c
<<
>>
Prefs
   1/*
   2 * DA7213 ALSA SoC Codec Driver
   3 *
   4 * Copyright (c) 2013 Dialog Semiconductor
   5 *
   6 * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
   7 * Based on DA9055 ALSA SoC codec driver.
   8 *
   9 * This program is free software; you can redistribute  it and/or modify it
  10 * under  the terms of  the GNU General  Public License as published by the
  11 * Free Software Foundation;  either version 2 of the  License, or (at your
  12 * option) any later version.
  13 */
  14
  15#include <linux/delay.h>
  16#include <linux/i2c.h>
  17#include <linux/regmap.h>
  18#include <linux/slab.h>
  19#include <linux/module.h>
  20#include <sound/pcm.h>
  21#include <sound/pcm_params.h>
  22#include <sound/soc.h>
  23#include <sound/initval.h>
  24#include <sound/tlv.h>
  25
  26#include <sound/da7213.h>
  27#include "da7213.h"
  28
  29
  30/* Gain and Volume */
  31static const unsigned int aux_vol_tlv[] = {
  32        TLV_DB_RANGE_HEAD(2),
  33        /* -54dB */
  34        0x0, 0x11, TLV_DB_SCALE_ITEM(-5400, 0, 0),
  35        /* -52.5dB to 15dB */
  36        0x12, 0x3f, TLV_DB_SCALE_ITEM(-5250, 150, 0)
  37};
  38
  39static const unsigned int digital_gain_tlv[] = {
  40        TLV_DB_RANGE_HEAD(2),
  41        0x0, 0x07, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
  42        /* -78dB to 12dB */
  43        0x08, 0x7f, TLV_DB_SCALE_ITEM(-7800, 75, 0)
  44};
  45
  46static const unsigned int alc_analog_gain_tlv[] = {
  47        TLV_DB_RANGE_HEAD(2),
  48        0x0, 0x0, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
  49        /* 0dB to 36dB */
  50        0x01, 0x07, TLV_DB_SCALE_ITEM(0, 600, 0)
  51};
  52
  53static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, -600, 600, 0);
  54static const DECLARE_TLV_DB_SCALE(mixin_gain_tlv, -450, 150, 0);
  55static const DECLARE_TLV_DB_SCALE(eq_gain_tlv, -1050, 150, 0);
  56static const DECLARE_TLV_DB_SCALE(hp_vol_tlv, -5700, 100, 0);
  57static const DECLARE_TLV_DB_SCALE(lineout_vol_tlv, -4800, 100, 0);
  58static const DECLARE_TLV_DB_SCALE(alc_threshold_tlv, -9450, 150, 0);
  59static const DECLARE_TLV_DB_SCALE(alc_gain_tlv, 0, 600, 0);
  60
  61/* ADC and DAC voice mode (8kHz) high pass cutoff value */
  62static const char * const da7213_voice_hpf_corner_txt[] = {
  63        "2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
  64};
  65
  66static SOC_ENUM_SINGLE_DECL(da7213_dac_voice_hpf_corner,
  67                            DA7213_DAC_FILTERS1,
  68                            DA7213_VOICE_HPF_CORNER_SHIFT,
  69                            da7213_voice_hpf_corner_txt);
  70
  71static SOC_ENUM_SINGLE_DECL(da7213_adc_voice_hpf_corner,
  72                            DA7213_ADC_FILTERS1,
  73                            DA7213_VOICE_HPF_CORNER_SHIFT,
  74                            da7213_voice_hpf_corner_txt);
  75
  76/* ADC and DAC high pass filter cutoff value */
  77static const char * const da7213_audio_hpf_corner_txt[] = {
  78        "Fs/24000", "Fs/12000", "Fs/6000", "Fs/3000"
  79};
  80
  81static SOC_ENUM_SINGLE_DECL(da7213_dac_audio_hpf_corner,
  82                            DA7213_DAC_FILTERS1
  83                            , DA7213_AUDIO_HPF_CORNER_SHIFT,
  84                            da7213_audio_hpf_corner_txt);
  85
  86static SOC_ENUM_SINGLE_DECL(da7213_adc_audio_hpf_corner,
  87                            DA7213_ADC_FILTERS1,
  88                            DA7213_AUDIO_HPF_CORNER_SHIFT,
  89                            da7213_audio_hpf_corner_txt);
  90
  91/* Gain ramping rate value */
  92static const char * const da7213_gain_ramp_rate_txt[] = {
  93        "nominal rate * 8", "nominal rate * 16", "nominal rate / 16",
  94        "nominal rate / 32"
  95};
  96
  97static SOC_ENUM_SINGLE_DECL(da7213_gain_ramp_rate,
  98                            DA7213_GAIN_RAMP_CTRL,
  99                            DA7213_GAIN_RAMP_RATE_SHIFT,
 100                            da7213_gain_ramp_rate_txt);
 101
 102/* DAC noise gate setup time value */
 103static const char * const da7213_dac_ng_setup_time_txt[] = {
 104        "256 samples", "512 samples", "1024 samples", "2048 samples"
 105};
 106
 107static SOC_ENUM_SINGLE_DECL(da7213_dac_ng_setup_time,
 108                            DA7213_DAC_NG_SETUP_TIME,
 109                            DA7213_DAC_NG_SETUP_TIME_SHIFT,
 110                            da7213_dac_ng_setup_time_txt);
 111
 112/* DAC noise gate rampup rate value */
 113static const char * const da7213_dac_ng_rampup_txt[] = {
 114        "0.02 ms/dB", "0.16 ms/dB"
 115};
 116
 117static SOC_ENUM_SINGLE_DECL(da7213_dac_ng_rampup_rate,
 118                            DA7213_DAC_NG_SETUP_TIME,
 119                            DA7213_DAC_NG_RAMPUP_RATE_SHIFT,
 120                            da7213_dac_ng_rampup_txt);
 121
 122/* DAC noise gate rampdown rate value */
 123static const char * const da7213_dac_ng_rampdown_txt[] = {
 124        "0.64 ms/dB", "20.48 ms/dB"
 125};
 126
 127static SOC_ENUM_SINGLE_DECL(da7213_dac_ng_rampdown_rate,
 128                            DA7213_DAC_NG_SETUP_TIME,
 129                            DA7213_DAC_NG_RAMPDN_RATE_SHIFT,
 130                            da7213_dac_ng_rampdown_txt);
 131
 132/* DAC soft mute rate value */
 133static const char * const da7213_dac_soft_mute_rate_txt[] = {
 134        "1", "2", "4", "8", "16", "32", "64"
 135};
 136
 137static SOC_ENUM_SINGLE_DECL(da7213_dac_soft_mute_rate,
 138                            DA7213_DAC_FILTERS5,
 139                            DA7213_DAC_SOFTMUTE_RATE_SHIFT,
 140                            da7213_dac_soft_mute_rate_txt);
 141
 142/* ALC Attack Rate select */
 143static const char * const da7213_alc_attack_rate_txt[] = {
 144        "44/fs", "88/fs", "176/fs", "352/fs", "704/fs", "1408/fs", "2816/fs",
 145        "5632/fs", "11264/fs", "22528/fs", "45056/fs", "90112/fs", "180224/fs"
 146};
 147
 148static SOC_ENUM_SINGLE_DECL(da7213_alc_attack_rate,
 149                            DA7213_ALC_CTRL2,
 150                            DA7213_ALC_ATTACK_SHIFT,
 151                            da7213_alc_attack_rate_txt);
 152
 153/* ALC Release Rate select */
 154static const char * const da7213_alc_release_rate_txt[] = {
 155        "176/fs", "352/fs", "704/fs", "1408/fs", "2816/fs", "5632/fs",
 156        "11264/fs", "22528/fs", "45056/fs", "90112/fs", "180224/fs"
 157};
 158
 159static SOC_ENUM_SINGLE_DECL(da7213_alc_release_rate,
 160                            DA7213_ALC_CTRL2,
 161                            DA7213_ALC_RELEASE_SHIFT,
 162                            da7213_alc_release_rate_txt);
 163
 164/* ALC Hold Time select */
 165static const char * const da7213_alc_hold_time_txt[] = {
 166        "62/fs", "124/fs", "248/fs", "496/fs", "992/fs", "1984/fs", "3968/fs",
 167        "7936/fs", "15872/fs", "31744/fs", "63488/fs", "126976/fs",
 168        "253952/fs", "507904/fs", "1015808/fs", "2031616/fs"
 169};
 170
 171static SOC_ENUM_SINGLE_DECL(da7213_alc_hold_time,
 172                            DA7213_ALC_CTRL3,
 173                            DA7213_ALC_HOLD_SHIFT,
 174                            da7213_alc_hold_time_txt);
 175
 176/* ALC Input Signal Tracking rate select */
 177static const char * const da7213_alc_integ_rate_txt[] = {
 178        "1/4", "1/16", "1/256", "1/65536"
 179};
 180
 181static SOC_ENUM_SINGLE_DECL(da7213_alc_integ_attack_rate,
 182                            DA7213_ALC_CTRL3,
 183                            DA7213_ALC_INTEG_ATTACK_SHIFT,
 184                            da7213_alc_integ_rate_txt);
 185
 186static SOC_ENUM_SINGLE_DECL(da7213_alc_integ_release_rate,
 187                            DA7213_ALC_CTRL3,
 188                            DA7213_ALC_INTEG_RELEASE_SHIFT,
 189                            da7213_alc_integ_rate_txt);
 190
 191
 192/*
 193 * Control Functions
 194 */
 195
 196static int da7213_get_alc_data(struct snd_soc_codec *codec, u8 reg_val)
 197{
 198        int mid_data, top_data;
 199        int sum = 0;
 200        u8 iteration;
 201
 202        for (iteration = 0; iteration < DA7213_ALC_AVG_ITERATIONS;
 203             iteration++) {
 204                /* Select the left or right channel and capture data */
 205                snd_soc_write(codec, DA7213_ALC_CIC_OP_LVL_CTRL, reg_val);
 206
 207                /* Select middle 8 bits for read back from data register */
 208                snd_soc_write(codec, DA7213_ALC_CIC_OP_LVL_CTRL,
 209                              reg_val | DA7213_ALC_DATA_MIDDLE);
 210                mid_data = snd_soc_read(codec, DA7213_ALC_CIC_OP_LVL_DATA);
 211
 212                /* Select top 8 bits for read back from data register */
 213                snd_soc_write(codec, DA7213_ALC_CIC_OP_LVL_CTRL,
 214                              reg_val | DA7213_ALC_DATA_TOP);
 215                top_data = snd_soc_read(codec, DA7213_ALC_CIC_OP_LVL_DATA);
 216
 217                sum += ((mid_data << 8) | (top_data << 16));
 218        }
 219
 220        return sum / DA7213_ALC_AVG_ITERATIONS;
 221}
 222
 223static void da7213_alc_calib_man(struct snd_soc_codec *codec)
 224{
 225        u8 reg_val;
 226        int avg_left_data, avg_right_data, offset_l, offset_r;
 227
 228        /* Calculate average for Left and Right data */
 229        /* Left Data */
 230        avg_left_data = da7213_get_alc_data(codec,
 231                        DA7213_ALC_CIC_OP_CHANNEL_LEFT);
 232        /* Right Data */
 233        avg_right_data = da7213_get_alc_data(codec,
 234                         DA7213_ALC_CIC_OP_CHANNEL_RIGHT);
 235
 236        /* Calculate DC offset */
 237        offset_l = -avg_left_data;
 238        offset_r = -avg_right_data;
 239
 240        reg_val = (offset_l & DA7213_ALC_OFFSET_15_8) >> 8;
 241        snd_soc_write(codec, DA7213_ALC_OFFSET_MAN_M_L, reg_val);
 242        reg_val = (offset_l & DA7213_ALC_OFFSET_19_16) >> 16;
 243        snd_soc_write(codec, DA7213_ALC_OFFSET_MAN_U_L, reg_val);
 244
 245        reg_val = (offset_r & DA7213_ALC_OFFSET_15_8) >> 8;
 246        snd_soc_write(codec, DA7213_ALC_OFFSET_MAN_M_R, reg_val);
 247        reg_val = (offset_r & DA7213_ALC_OFFSET_19_16) >> 16;
 248        snd_soc_write(codec, DA7213_ALC_OFFSET_MAN_U_R, reg_val);
 249
 250        /* Enable analog/digital gain mode & offset cancellation */
 251        snd_soc_update_bits(codec, DA7213_ALC_CTRL1,
 252                            DA7213_ALC_OFFSET_EN | DA7213_ALC_SYNC_MODE,
 253                            DA7213_ALC_OFFSET_EN | DA7213_ALC_SYNC_MODE);
 254}
 255
 256static void da7213_alc_calib_auto(struct snd_soc_codec *codec)
 257{
 258        u8 alc_ctrl1;
 259
 260        /* Begin auto calibration and wait for completion */
 261        snd_soc_update_bits(codec, DA7213_ALC_CTRL1, DA7213_ALC_AUTO_CALIB_EN,
 262                            DA7213_ALC_AUTO_CALIB_EN);
 263        do {
 264                alc_ctrl1 = snd_soc_read(codec, DA7213_ALC_CTRL1);
 265        } while (alc_ctrl1 & DA7213_ALC_AUTO_CALIB_EN);
 266
 267        /* If auto calibration fails, fall back to digital gain only mode */
 268        if (alc_ctrl1 & DA7213_ALC_CALIB_OVERFLOW) {
 269                dev_warn(codec->dev,
 270                         "ALC auto calibration failed with overflow\n");
 271                snd_soc_update_bits(codec, DA7213_ALC_CTRL1,
 272                                    DA7213_ALC_OFFSET_EN | DA7213_ALC_SYNC_MODE,
 273                                    0);
 274        } else {
 275                /* Enable analog/digital gain mode & offset cancellation */
 276                snd_soc_update_bits(codec, DA7213_ALC_CTRL1,
 277                                    DA7213_ALC_OFFSET_EN | DA7213_ALC_SYNC_MODE,
 278                                    DA7213_ALC_OFFSET_EN | DA7213_ALC_SYNC_MODE);
 279        }
 280
 281}
 282
 283static void da7213_alc_calib(struct snd_soc_codec *codec)
 284{
 285        struct da7213_priv *da7213 = snd_soc_codec_get_drvdata(codec);
 286        u8 adc_l_ctrl, adc_r_ctrl;
 287        u8 mixin_l_sel, mixin_r_sel;
 288        u8 mic_1_ctrl, mic_2_ctrl;
 289
 290        /* Save current values from ADC control registers */
 291        adc_l_ctrl = snd_soc_read(codec, DA7213_ADC_L_CTRL);
 292        adc_r_ctrl = snd_soc_read(codec, DA7213_ADC_R_CTRL);
 293
 294        /* Save current values from MIXIN_L/R_SELECT registers */
 295        mixin_l_sel = snd_soc_read(codec, DA7213_MIXIN_L_SELECT);
 296        mixin_r_sel = snd_soc_read(codec, DA7213_MIXIN_R_SELECT);
 297
 298        /* Save current values from MIC control registers */
 299        mic_1_ctrl = snd_soc_read(codec, DA7213_MIC_1_CTRL);
 300        mic_2_ctrl = snd_soc_read(codec, DA7213_MIC_2_CTRL);
 301
 302        /* Enable ADC Left and Right */
 303        snd_soc_update_bits(codec, DA7213_ADC_L_CTRL, DA7213_ADC_EN,
 304                            DA7213_ADC_EN);
 305        snd_soc_update_bits(codec, DA7213_ADC_R_CTRL, DA7213_ADC_EN,
 306                            DA7213_ADC_EN);
 307
 308        /* Enable MIC paths */
 309        snd_soc_update_bits(codec, DA7213_MIXIN_L_SELECT,
 310                            DA7213_MIXIN_L_MIX_SELECT_MIC_1 |
 311                            DA7213_MIXIN_L_MIX_SELECT_MIC_2,
 312                            DA7213_MIXIN_L_MIX_SELECT_MIC_1 |
 313                            DA7213_MIXIN_L_MIX_SELECT_MIC_2);
 314        snd_soc_update_bits(codec, DA7213_MIXIN_R_SELECT,
 315                            DA7213_MIXIN_R_MIX_SELECT_MIC_2 |
 316                            DA7213_MIXIN_R_MIX_SELECT_MIC_1,
 317                            DA7213_MIXIN_R_MIX_SELECT_MIC_2 |
 318                            DA7213_MIXIN_R_MIX_SELECT_MIC_1);
 319
 320        /* Mute MIC PGAs */
 321        snd_soc_update_bits(codec, DA7213_MIC_1_CTRL, DA7213_MUTE_EN,
 322                            DA7213_MUTE_EN);
 323        snd_soc_update_bits(codec, DA7213_MIC_2_CTRL, DA7213_MUTE_EN,
 324                            DA7213_MUTE_EN);
 325
 326        /* Perform calibration */
 327        if (da7213->alc_calib_auto)
 328                da7213_alc_calib_auto(codec);
 329        else
 330                da7213_alc_calib_man(codec);
 331
 332        /* Restore MIXIN_L/R_SELECT registers to their original states */
 333        snd_soc_write(codec, DA7213_MIXIN_L_SELECT, mixin_l_sel);
 334        snd_soc_write(codec, DA7213_MIXIN_R_SELECT, mixin_r_sel);
 335
 336        /* Restore ADC control registers to their original states */
 337        snd_soc_write(codec, DA7213_ADC_L_CTRL, adc_l_ctrl);
 338        snd_soc_write(codec, DA7213_ADC_R_CTRL, adc_r_ctrl);
 339
 340        /* Restore original values of MIC control registers */
 341        snd_soc_write(codec, DA7213_MIC_1_CTRL, mic_1_ctrl);
 342        snd_soc_write(codec, DA7213_MIC_2_CTRL, mic_2_ctrl);
 343}
 344
 345static int da7213_put_mixin_gain(struct snd_kcontrol *kcontrol,
 346                                struct snd_ctl_elem_value *ucontrol)
 347{
 348        struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
 349        struct da7213_priv *da7213 = snd_soc_codec_get_drvdata(codec);
 350        int ret;
 351
 352        ret = snd_soc_put_volsw_2r(kcontrol, ucontrol);
 353
 354        /* If ALC in operation, make sure calibrated offsets are updated */
 355        if ((!ret) && (da7213->alc_en))
 356                da7213_alc_calib(codec);
 357
 358        return ret;
 359}
 360
 361static int da7213_put_alc_sw(struct snd_kcontrol *kcontrol,
 362                            struct snd_ctl_elem_value *ucontrol)
 363{
 364        struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
 365        struct da7213_priv *da7213 = snd_soc_codec_get_drvdata(codec);
 366
 367        /* Force ALC offset calibration if enabling ALC */
 368        if (ucontrol->value.integer.value[0] ||
 369            ucontrol->value.integer.value[1]) {
 370                if (!da7213->alc_en) {
 371                        da7213_alc_calib(codec);
 372                        da7213->alc_en = true;
 373                }
 374        } else {
 375                da7213->alc_en = false;
 376        }
 377
 378        return snd_soc_put_volsw(kcontrol, ucontrol);
 379}
 380
 381
 382/*
 383 * KControls
 384 */
 385
 386static const struct snd_kcontrol_new da7213_snd_controls[] = {
 387
 388        /* Volume controls */
 389        SOC_SINGLE_TLV("Mic 1 Volume", DA7213_MIC_1_GAIN,
 390                       DA7213_MIC_AMP_GAIN_SHIFT, DA7213_MIC_AMP_GAIN_MAX,
 391                       DA7213_NO_INVERT, mic_vol_tlv),
 392        SOC_SINGLE_TLV("Mic 2 Volume", DA7213_MIC_2_GAIN,
 393                       DA7213_MIC_AMP_GAIN_SHIFT, DA7213_MIC_AMP_GAIN_MAX,
 394                       DA7213_NO_INVERT, mic_vol_tlv),
 395        SOC_DOUBLE_R_TLV("Aux Volume", DA7213_AUX_L_GAIN, DA7213_AUX_R_GAIN,
 396                         DA7213_AUX_AMP_GAIN_SHIFT, DA7213_AUX_AMP_GAIN_MAX,
 397                         DA7213_NO_INVERT, aux_vol_tlv),
 398        SOC_DOUBLE_R_EXT_TLV("Mixin PGA Volume", DA7213_MIXIN_L_GAIN,
 399                             DA7213_MIXIN_R_GAIN, DA7213_MIXIN_AMP_GAIN_SHIFT,
 400                             DA7213_MIXIN_AMP_GAIN_MAX, DA7213_NO_INVERT,
 401                             snd_soc_get_volsw_2r, da7213_put_mixin_gain,
 402                             mixin_gain_tlv),
 403        SOC_DOUBLE_R_TLV("ADC Volume", DA7213_ADC_L_GAIN, DA7213_ADC_R_GAIN,
 404                         DA7213_ADC_AMP_GAIN_SHIFT, DA7213_ADC_AMP_GAIN_MAX,
 405                         DA7213_NO_INVERT, digital_gain_tlv),
 406        SOC_DOUBLE_R_TLV("DAC Volume", DA7213_DAC_L_GAIN, DA7213_DAC_R_GAIN,
 407                         DA7213_DAC_AMP_GAIN_SHIFT, DA7213_DAC_AMP_GAIN_MAX,
 408                         DA7213_NO_INVERT, digital_gain_tlv),
 409        SOC_DOUBLE_R_TLV("Headphone Volume", DA7213_HP_L_GAIN, DA7213_HP_R_GAIN,
 410                         DA7213_HP_AMP_GAIN_SHIFT, DA7213_HP_AMP_GAIN_MAX,
 411                         DA7213_NO_INVERT, hp_vol_tlv),
 412        SOC_SINGLE_TLV("Lineout Volume", DA7213_LINE_GAIN,
 413                       DA7213_LINE_AMP_GAIN_SHIFT, DA7213_LINE_AMP_GAIN_MAX,
 414                       DA7213_NO_INVERT, lineout_vol_tlv),
 415
 416        /* DAC Equalizer controls */
 417        SOC_SINGLE("DAC EQ Switch", DA7213_DAC_FILTERS4, DA7213_DAC_EQ_EN_SHIFT,
 418                   DA7213_DAC_EQ_EN_MAX, DA7213_NO_INVERT),
 419        SOC_SINGLE_TLV("DAC EQ1 Volume", DA7213_DAC_FILTERS2,
 420                       DA7213_DAC_EQ_BAND1_SHIFT, DA7213_DAC_EQ_BAND_MAX,
 421                       DA7213_NO_INVERT, eq_gain_tlv),
 422        SOC_SINGLE_TLV("DAC EQ2 Volume", DA7213_DAC_FILTERS2,
 423                       DA7213_DAC_EQ_BAND2_SHIFT, DA7213_DAC_EQ_BAND_MAX,
 424                       DA7213_NO_INVERT, eq_gain_tlv),
 425        SOC_SINGLE_TLV("DAC EQ3 Volume", DA7213_DAC_FILTERS3,
 426                       DA7213_DAC_EQ_BAND3_SHIFT, DA7213_DAC_EQ_BAND_MAX,
 427                       DA7213_NO_INVERT, eq_gain_tlv),
 428        SOC_SINGLE_TLV("DAC EQ4 Volume", DA7213_DAC_FILTERS3,
 429                       DA7213_DAC_EQ_BAND4_SHIFT, DA7213_DAC_EQ_BAND_MAX,
 430                       DA7213_NO_INVERT, eq_gain_tlv),
 431        SOC_SINGLE_TLV("DAC EQ5 Volume", DA7213_DAC_FILTERS4,
 432                       DA7213_DAC_EQ_BAND5_SHIFT, DA7213_DAC_EQ_BAND_MAX,
 433                       DA7213_NO_INVERT, eq_gain_tlv),
 434
 435        /* High Pass Filter and Voice Mode controls */
 436        SOC_SINGLE("ADC HPF Switch", DA7213_ADC_FILTERS1, DA7213_HPF_EN_SHIFT,
 437                   DA7213_HPF_EN_MAX, DA7213_NO_INVERT),
 438        SOC_ENUM("ADC HPF Cutoff", da7213_adc_audio_hpf_corner),
 439        SOC_SINGLE("ADC Voice Mode Switch", DA7213_ADC_FILTERS1,
 440                   DA7213_VOICE_EN_SHIFT, DA7213_VOICE_EN_MAX,
 441                   DA7213_NO_INVERT),
 442        SOC_ENUM("ADC Voice Cutoff", da7213_adc_voice_hpf_corner),
 443
 444        SOC_SINGLE("DAC HPF Switch", DA7213_DAC_FILTERS1, DA7213_HPF_EN_SHIFT,
 445                   DA7213_HPF_EN_MAX, DA7213_NO_INVERT),
 446        SOC_ENUM("DAC HPF Cutoff", da7213_dac_audio_hpf_corner),
 447        SOC_SINGLE("DAC Voice Mode Switch", DA7213_DAC_FILTERS1,
 448                   DA7213_VOICE_EN_SHIFT, DA7213_VOICE_EN_MAX,
 449                   DA7213_NO_INVERT),
 450        SOC_ENUM("DAC Voice Cutoff", da7213_dac_voice_hpf_corner),
 451
 452        /* Mute controls */
 453        SOC_SINGLE("Mic 1 Switch", DA7213_MIC_1_CTRL, DA7213_MUTE_EN_SHIFT,
 454                   DA7213_MUTE_EN_MAX, DA7213_INVERT),
 455        SOC_SINGLE("Mic 2 Switch", DA7213_MIC_2_CTRL, DA7213_MUTE_EN_SHIFT,
 456                   DA7213_MUTE_EN_MAX, DA7213_INVERT),
 457        SOC_DOUBLE_R("Aux Switch", DA7213_AUX_L_CTRL, DA7213_AUX_R_CTRL,
 458                     DA7213_MUTE_EN_SHIFT, DA7213_MUTE_EN_MAX, DA7213_INVERT),
 459        SOC_DOUBLE_R("Mixin PGA Switch", DA7213_MIXIN_L_CTRL,
 460                     DA7213_MIXIN_R_CTRL, DA7213_MUTE_EN_SHIFT,
 461                     DA7213_MUTE_EN_MAX, DA7213_INVERT),
 462        SOC_DOUBLE_R("ADC Switch", DA7213_ADC_L_CTRL, DA7213_ADC_R_CTRL,
 463                     DA7213_MUTE_EN_SHIFT, DA7213_MUTE_EN_MAX, DA7213_INVERT),
 464        SOC_DOUBLE_R("Headphone Switch", DA7213_HP_L_CTRL, DA7213_HP_R_CTRL,
 465                     DA7213_MUTE_EN_SHIFT, DA7213_MUTE_EN_MAX, DA7213_INVERT),
 466        SOC_SINGLE("Lineout Switch", DA7213_LINE_CTRL, DA7213_MUTE_EN_SHIFT,
 467                   DA7213_MUTE_EN_MAX, DA7213_INVERT),
 468        SOC_SINGLE("DAC Soft Mute Switch", DA7213_DAC_FILTERS5,
 469                   DA7213_DAC_SOFTMUTE_EN_SHIFT, DA7213_DAC_SOFTMUTE_EN_MAX,
 470                   DA7213_NO_INVERT),
 471        SOC_ENUM("DAC Soft Mute Rate", da7213_dac_soft_mute_rate),
 472
 473        /* Zero Cross controls */
 474        SOC_DOUBLE_R("Aux ZC Switch", DA7213_AUX_L_CTRL, DA7213_AUX_R_CTRL,
 475                     DA7213_ZC_EN_SHIFT, DA7213_ZC_EN_MAX, DA7213_NO_INVERT),
 476        SOC_DOUBLE_R("Mixin PGA ZC Switch", DA7213_MIXIN_L_CTRL,
 477                     DA7213_MIXIN_R_CTRL, DA7213_ZC_EN_SHIFT, DA7213_ZC_EN_MAX,
 478                     DA7213_NO_INVERT),
 479        SOC_DOUBLE_R("Headphone ZC Switch", DA7213_HP_L_CTRL, DA7213_HP_R_CTRL,
 480                     DA7213_ZC_EN_SHIFT, DA7213_ZC_EN_MAX, DA7213_NO_INVERT),
 481
 482        /* Gain Ramping controls */
 483        SOC_DOUBLE_R("Aux Gain Ramping Switch", DA7213_AUX_L_CTRL,
 484                     DA7213_AUX_R_CTRL, DA7213_GAIN_RAMP_EN_SHIFT,
 485                     DA7213_GAIN_RAMP_EN_MAX, DA7213_NO_INVERT),
 486        SOC_DOUBLE_R("Mixin Gain Ramping Switch", DA7213_MIXIN_L_CTRL,
 487                     DA7213_MIXIN_R_CTRL, DA7213_GAIN_RAMP_EN_SHIFT,
 488                     DA7213_GAIN_RAMP_EN_MAX, DA7213_NO_INVERT),
 489        SOC_DOUBLE_R("ADC Gain Ramping Switch", DA7213_ADC_L_CTRL,
 490                     DA7213_ADC_R_CTRL, DA7213_GAIN_RAMP_EN_SHIFT,
 491                     DA7213_GAIN_RAMP_EN_MAX, DA7213_NO_INVERT),
 492        SOC_DOUBLE_R("DAC Gain Ramping Switch", DA7213_DAC_L_CTRL,
 493                     DA7213_DAC_R_CTRL, DA7213_GAIN_RAMP_EN_SHIFT,
 494                     DA7213_GAIN_RAMP_EN_MAX, DA7213_NO_INVERT),
 495        SOC_DOUBLE_R("Headphone Gain Ramping Switch", DA7213_HP_L_CTRL,
 496                     DA7213_HP_R_CTRL, DA7213_GAIN_RAMP_EN_SHIFT,
 497                     DA7213_GAIN_RAMP_EN_MAX, DA7213_NO_INVERT),
 498        SOC_SINGLE("Lineout Gain Ramping Switch", DA7213_LINE_CTRL,
 499                   DA7213_GAIN_RAMP_EN_SHIFT, DA7213_GAIN_RAMP_EN_MAX,
 500                   DA7213_NO_INVERT),
 501        SOC_ENUM("Gain Ramping Rate", da7213_gain_ramp_rate),
 502
 503        /* DAC Noise Gate controls */
 504        SOC_SINGLE("DAC NG Switch", DA7213_DAC_NG_CTRL, DA7213_DAC_NG_EN_SHIFT,
 505                   DA7213_DAC_NG_EN_MAX, DA7213_NO_INVERT),
 506        SOC_ENUM("DAC NG Setup Time", da7213_dac_ng_setup_time),
 507        SOC_ENUM("DAC NG Rampup Rate", da7213_dac_ng_rampup_rate),
 508        SOC_ENUM("DAC NG Rampdown Rate", da7213_dac_ng_rampdown_rate),
 509        SOC_SINGLE("DAC NG OFF Threshold", DA7213_DAC_NG_OFF_THRESHOLD,
 510                   DA7213_DAC_NG_THRESHOLD_SHIFT, DA7213_DAC_NG_THRESHOLD_MAX,
 511                   DA7213_NO_INVERT),
 512        SOC_SINGLE("DAC NG ON Threshold", DA7213_DAC_NG_ON_THRESHOLD,
 513                   DA7213_DAC_NG_THRESHOLD_SHIFT, DA7213_DAC_NG_THRESHOLD_MAX,
 514                   DA7213_NO_INVERT),
 515
 516        /* DAC Routing & Inversion */
 517        SOC_DOUBLE("DAC Mono Switch", DA7213_DIG_ROUTING_DAC,
 518                   DA7213_DAC_L_MONO_SHIFT, DA7213_DAC_R_MONO_SHIFT,
 519                   DA7213_DAC_MONO_MAX, DA7213_NO_INVERT),
 520        SOC_DOUBLE("DAC Invert Switch", DA7213_DIG_CTRL, DA7213_DAC_L_INV_SHIFT,
 521                   DA7213_DAC_R_INV_SHIFT, DA7213_DAC_INV_MAX,
 522                   DA7213_NO_INVERT),
 523
 524        /* DMIC controls */
 525        SOC_DOUBLE_R("DMIC Switch", DA7213_MIXIN_L_SELECT,
 526                     DA7213_MIXIN_R_SELECT, DA7213_DMIC_EN_SHIFT,
 527                     DA7213_DMIC_EN_MAX, DA7213_NO_INVERT),
 528
 529        /* ALC Controls */
 530        SOC_DOUBLE_EXT("ALC Switch", DA7213_ALC_CTRL1, DA7213_ALC_L_EN_SHIFT,
 531                       DA7213_ALC_R_EN_SHIFT, DA7213_ALC_EN_MAX,
 532                       DA7213_NO_INVERT, snd_soc_get_volsw, da7213_put_alc_sw),
 533        SOC_ENUM("ALC Attack Rate", da7213_alc_attack_rate),
 534        SOC_ENUM("ALC Release Rate", da7213_alc_release_rate),
 535        SOC_ENUM("ALC Hold Time", da7213_alc_hold_time),
 536        /*
 537         * Rate at which input signal envelope is tracked as the signal gets
 538         * larger
 539         */
 540        SOC_ENUM("ALC Integ Attack Rate", da7213_alc_integ_attack_rate),
 541        /*
 542         * Rate at which input signal envelope is tracked as the signal gets
 543         * smaller
 544         */
 545        SOC_ENUM("ALC Integ Release Rate", da7213_alc_integ_release_rate),
 546        SOC_SINGLE_TLV("ALC Noise Threshold Volume", DA7213_ALC_NOISE,
 547                       DA7213_ALC_THRESHOLD_SHIFT, DA7213_ALC_THRESHOLD_MAX,
 548                       DA7213_INVERT, alc_threshold_tlv),
 549        SOC_SINGLE_TLV("ALC Min Threshold Volume", DA7213_ALC_TARGET_MIN,
 550                       DA7213_ALC_THRESHOLD_SHIFT, DA7213_ALC_THRESHOLD_MAX,
 551                       DA7213_INVERT, alc_threshold_tlv),
 552        SOC_SINGLE_TLV("ALC Max Threshold Volume", DA7213_ALC_TARGET_MAX,
 553                       DA7213_ALC_THRESHOLD_SHIFT, DA7213_ALC_THRESHOLD_MAX,
 554                       DA7213_INVERT, alc_threshold_tlv),
 555        SOC_SINGLE_TLV("ALC Max Attenuation Volume", DA7213_ALC_GAIN_LIMITS,
 556                       DA7213_ALC_ATTEN_MAX_SHIFT,
 557                       DA7213_ALC_ATTEN_GAIN_MAX_MAX, DA7213_NO_INVERT,
 558                       alc_gain_tlv),
 559        SOC_SINGLE_TLV("ALC Max Gain Volume", DA7213_ALC_GAIN_LIMITS,
 560                       DA7213_ALC_GAIN_MAX_SHIFT, DA7213_ALC_ATTEN_GAIN_MAX_MAX,
 561                       DA7213_NO_INVERT, alc_gain_tlv),
 562        SOC_SINGLE_TLV("ALC Min Analog Gain Volume", DA7213_ALC_ANA_GAIN_LIMITS,
 563                       DA7213_ALC_ANA_GAIN_MIN_SHIFT, DA7213_ALC_ANA_GAIN_MAX,
 564                       DA7213_NO_INVERT, alc_analog_gain_tlv),
 565        SOC_SINGLE_TLV("ALC Max Analog Gain Volume", DA7213_ALC_ANA_GAIN_LIMITS,
 566                       DA7213_ALC_ANA_GAIN_MAX_SHIFT, DA7213_ALC_ANA_GAIN_MAX,
 567                       DA7213_NO_INVERT, alc_analog_gain_tlv),
 568        SOC_SINGLE("ALC Anticlip Mode Switch", DA7213_ALC_ANTICLIP_CTRL,
 569                   DA7213_ALC_ANTICLIP_EN_SHIFT, DA7213_ALC_ANTICLIP_EN_MAX,
 570                   DA7213_NO_INVERT),
 571        SOC_SINGLE("ALC Anticlip Level", DA7213_ALC_ANTICLIP_LEVEL,
 572                   DA7213_ALC_ANTICLIP_LEVEL_SHIFT,
 573                   DA7213_ALC_ANTICLIP_LEVEL_MAX, DA7213_NO_INVERT),
 574};
 575
 576
 577/*
 578 * DAPM
 579 */
 580
 581/*
 582 * Enums
 583 */
 584
 585/* MIC PGA source select */
 586static const char * const da7213_mic_amp_in_sel_txt[] = {
 587        "Differential", "MIC_P", "MIC_N"
 588};
 589
 590static SOC_ENUM_SINGLE_DECL(da7213_mic_1_amp_in_sel,
 591                            DA7213_MIC_1_CTRL,
 592                            DA7213_MIC_AMP_IN_SEL_SHIFT,
 593                            da7213_mic_amp_in_sel_txt);
 594static const struct snd_kcontrol_new da7213_mic_1_amp_in_sel_mux =
 595        SOC_DAPM_ENUM("Mic 1 Amp Source MUX", da7213_mic_1_amp_in_sel);
 596
 597static SOC_ENUM_SINGLE_DECL(da7213_mic_2_amp_in_sel,
 598                            DA7213_MIC_2_CTRL,
 599                            DA7213_MIC_AMP_IN_SEL_SHIFT,
 600                            da7213_mic_amp_in_sel_txt);
 601static const struct snd_kcontrol_new da7213_mic_2_amp_in_sel_mux =
 602        SOC_DAPM_ENUM("Mic 2 Amp Source MUX", da7213_mic_2_amp_in_sel);
 603
 604/* DAI routing select */
 605static const char * const da7213_dai_src_txt[] = {
 606        "ADC Left", "ADC Right", "DAI Input Left", "DAI Input Right"
 607};
 608
 609static SOC_ENUM_SINGLE_DECL(da7213_dai_l_src,
 610                            DA7213_DIG_ROUTING_DAI,
 611                            DA7213_DAI_L_SRC_SHIFT,
 612                            da7213_dai_src_txt);
 613static const struct snd_kcontrol_new da7213_dai_l_src_mux =
 614        SOC_DAPM_ENUM("DAI Left Source MUX", da7213_dai_l_src);
 615
 616static SOC_ENUM_SINGLE_DECL(da7213_dai_r_src,
 617                            DA7213_DIG_ROUTING_DAI,
 618                            DA7213_DAI_R_SRC_SHIFT,
 619                            da7213_dai_src_txt);
 620static const struct snd_kcontrol_new da7213_dai_r_src_mux =
 621        SOC_DAPM_ENUM("DAI Right Source MUX", da7213_dai_r_src);
 622
 623/* DAC routing select */
 624static const char * const da7213_dac_src_txt[] = {
 625        "ADC Output Left", "ADC Output Right", "DAI Input Left",
 626        "DAI Input Right"
 627};
 628
 629static SOC_ENUM_SINGLE_DECL(da7213_dac_l_src,
 630                            DA7213_DIG_ROUTING_DAC,
 631                            DA7213_DAC_L_SRC_SHIFT,
 632                            da7213_dac_src_txt);
 633static const struct snd_kcontrol_new da7213_dac_l_src_mux =
 634        SOC_DAPM_ENUM("DAC Left Source MUX", da7213_dac_l_src);
 635
 636static SOC_ENUM_SINGLE_DECL(da7213_dac_r_src,
 637                            DA7213_DIG_ROUTING_DAC,
 638                            DA7213_DAC_R_SRC_SHIFT,
 639                            da7213_dac_src_txt);
 640static const struct snd_kcontrol_new da7213_dac_r_src_mux =
 641        SOC_DAPM_ENUM("DAC Right Source MUX", da7213_dac_r_src);
 642
 643/*
 644 * Mixer Controls
 645 */
 646
 647/* Mixin Left */
 648static const struct snd_kcontrol_new da7213_dapm_mixinl_controls[] = {
 649        SOC_DAPM_SINGLE("Aux Left Switch", DA7213_MIXIN_L_SELECT,
 650                        DA7213_MIXIN_L_MIX_SELECT_AUX_L_SHIFT,
 651                        DA7213_MIXIN_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
 652        SOC_DAPM_SINGLE("Mic 1 Switch", DA7213_MIXIN_L_SELECT,
 653                        DA7213_MIXIN_L_MIX_SELECT_MIC_1_SHIFT,
 654                        DA7213_MIXIN_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
 655        SOC_DAPM_SINGLE("Mic 2 Switch", DA7213_MIXIN_L_SELECT,
 656                        DA7213_MIXIN_L_MIX_SELECT_MIC_2_SHIFT,
 657                        DA7213_MIXIN_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
 658        SOC_DAPM_SINGLE("Mixin Right Switch", DA7213_MIXIN_L_SELECT,
 659                        DA7213_MIXIN_L_MIX_SELECT_MIXIN_R_SHIFT,
 660                        DA7213_MIXIN_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
 661};
 662
 663/* Mixin Right */
 664static const struct snd_kcontrol_new da7213_dapm_mixinr_controls[] = {
 665        SOC_DAPM_SINGLE("Aux Right Switch", DA7213_MIXIN_R_SELECT,
 666                        DA7213_MIXIN_R_MIX_SELECT_AUX_R_SHIFT,
 667                        DA7213_MIXIN_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
 668        SOC_DAPM_SINGLE("Mic 2 Switch", DA7213_MIXIN_R_SELECT,
 669                        DA7213_MIXIN_R_MIX_SELECT_MIC_2_SHIFT,
 670                        DA7213_MIXIN_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
 671        SOC_DAPM_SINGLE("Mic 1 Switch", DA7213_MIXIN_R_SELECT,
 672                        DA7213_MIXIN_R_MIX_SELECT_MIC_1_SHIFT,
 673                        DA7213_MIXIN_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
 674        SOC_DAPM_SINGLE("Mixin Left Switch", DA7213_MIXIN_R_SELECT,
 675                        DA7213_MIXIN_R_MIX_SELECT_MIXIN_L_SHIFT,
 676                        DA7213_MIXIN_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
 677};
 678
 679/* Mixout Left */
 680static const struct snd_kcontrol_new da7213_dapm_mixoutl_controls[] = {
 681        SOC_DAPM_SINGLE("Aux Left Switch", DA7213_MIXOUT_L_SELECT,
 682                        DA7213_MIXOUT_L_MIX_SELECT_AUX_L_SHIFT,
 683                        DA7213_MIXOUT_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
 684        SOC_DAPM_SINGLE("Mixin Left Switch", DA7213_MIXOUT_L_SELECT,
 685                        DA7213_MIXOUT_L_MIX_SELECT_MIXIN_L_SHIFT,
 686                        DA7213_MIXOUT_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
 687        SOC_DAPM_SINGLE("Mixin Right Switch", DA7213_MIXOUT_L_SELECT,
 688                        DA7213_MIXOUT_L_MIX_SELECT_MIXIN_R_SHIFT,
 689                        DA7213_MIXOUT_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
 690        SOC_DAPM_SINGLE("DAC Left Switch", DA7213_MIXOUT_L_SELECT,
 691                        DA7213_MIXOUT_L_MIX_SELECT_DAC_L_SHIFT,
 692                        DA7213_MIXOUT_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
 693        SOC_DAPM_SINGLE("Aux Left Invert Switch", DA7213_MIXOUT_L_SELECT,
 694                        DA7213_MIXOUT_L_MIX_SELECT_AUX_L_INVERTED_SHIFT,
 695                        DA7213_MIXOUT_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
 696        SOC_DAPM_SINGLE("Mixin Left Invert Switch", DA7213_MIXOUT_L_SELECT,
 697                        DA7213_MIXOUT_L_MIX_SELECT_MIXIN_L_INVERTED_SHIFT,
 698                        DA7213_MIXOUT_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
 699        SOC_DAPM_SINGLE("Mixin Right Invert Switch", DA7213_MIXOUT_L_SELECT,
 700                        DA7213_MIXOUT_L_MIX_SELECT_MIXIN_R_INVERTED_SHIFT,
 701                        DA7213_MIXOUT_L_MIX_SELECT_MAX, DA7213_NO_INVERT),
 702};
 703
 704/* Mixout Right */
 705static const struct snd_kcontrol_new da7213_dapm_mixoutr_controls[] = {
 706        SOC_DAPM_SINGLE("Aux Right Switch", DA7213_MIXOUT_R_SELECT,
 707                        DA7213_MIXOUT_R_MIX_SELECT_AUX_R_SHIFT,
 708                        DA7213_MIXOUT_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
 709        SOC_DAPM_SINGLE("Mixin Right Switch", DA7213_MIXOUT_R_SELECT,
 710                        DA7213_MIXOUT_R_MIX_SELECT_MIXIN_R_SHIFT,
 711                        DA7213_MIXOUT_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
 712        SOC_DAPM_SINGLE("Mixin Left Switch", DA7213_MIXOUT_R_SELECT,
 713                        DA7213_MIXOUT_R_MIX_SELECT_MIXIN_L_SHIFT,
 714                        DA7213_MIXOUT_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
 715        SOC_DAPM_SINGLE("DAC Right Switch", DA7213_MIXOUT_R_SELECT,
 716                        DA7213_MIXOUT_R_MIX_SELECT_DAC_R_SHIFT,
 717                        DA7213_MIXOUT_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
 718        SOC_DAPM_SINGLE("Aux Right Invert Switch", DA7213_MIXOUT_R_SELECT,
 719                        DA7213_MIXOUT_R_MIX_SELECT_AUX_R_INVERTED_SHIFT,
 720                        DA7213_MIXOUT_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
 721        SOC_DAPM_SINGLE("Mixin Right Invert Switch", DA7213_MIXOUT_R_SELECT,
 722                        DA7213_MIXOUT_R_MIX_SELECT_MIXIN_R_INVERTED_SHIFT,
 723                        DA7213_MIXOUT_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
 724        SOC_DAPM_SINGLE("Mixin Left Invert Switch", DA7213_MIXOUT_R_SELECT,
 725                        DA7213_MIXOUT_R_MIX_SELECT_MIXIN_L_INVERTED_SHIFT,
 726                        DA7213_MIXOUT_R_MIX_SELECT_MAX, DA7213_NO_INVERT),
 727};
 728
 729
 730/*
 731 * DAPM widgets
 732 */
 733
 734static const struct snd_soc_dapm_widget da7213_dapm_widgets[] = {
 735        /*
 736         * Input & Output
 737         */
 738
 739        /* Use a supply here as this controls both input & output DAIs */
 740        SND_SOC_DAPM_SUPPLY("DAI", DA7213_DAI_CTRL, DA7213_DAI_EN_SHIFT,
 741                            DA7213_NO_INVERT, NULL, 0),
 742
 743        /*
 744         * Input
 745         */
 746
 747        /* Input Lines */
 748        SND_SOC_DAPM_INPUT("MIC1"),
 749        SND_SOC_DAPM_INPUT("MIC2"),
 750        SND_SOC_DAPM_INPUT("AUXL"),
 751        SND_SOC_DAPM_INPUT("AUXR"),
 752
 753        /* MUXs for Mic PGA source selection */
 754        SND_SOC_DAPM_MUX("Mic 1 Amp Source MUX", SND_SOC_NOPM, 0, 0,
 755                         &da7213_mic_1_amp_in_sel_mux),
 756        SND_SOC_DAPM_MUX("Mic 2 Amp Source MUX", SND_SOC_NOPM, 0, 0,
 757                         &da7213_mic_2_amp_in_sel_mux),
 758
 759        /* Input PGAs */
 760        SND_SOC_DAPM_PGA("Mic 1 PGA", DA7213_MIC_1_CTRL, DA7213_AMP_EN_SHIFT,
 761                         DA7213_NO_INVERT, NULL, 0),
 762        SND_SOC_DAPM_PGA("Mic 2 PGA", DA7213_MIC_2_CTRL, DA7213_AMP_EN_SHIFT,
 763                         DA7213_NO_INVERT, NULL, 0),
 764        SND_SOC_DAPM_PGA("Aux Left PGA", DA7213_AUX_L_CTRL, DA7213_AMP_EN_SHIFT,
 765                         DA7213_NO_INVERT, NULL, 0),
 766        SND_SOC_DAPM_PGA("Aux Right PGA", DA7213_AUX_R_CTRL,
 767                         DA7213_AMP_EN_SHIFT, DA7213_NO_INVERT, NULL, 0),
 768        SND_SOC_DAPM_PGA("Mixin Left PGA", DA7213_MIXIN_L_CTRL,
 769                         DA7213_AMP_EN_SHIFT, DA7213_NO_INVERT, NULL, 0),
 770        SND_SOC_DAPM_PGA("Mixin Right PGA", DA7213_MIXIN_R_CTRL,
 771                         DA7213_AMP_EN_SHIFT, DA7213_NO_INVERT, NULL, 0),
 772
 773        /* Mic Biases */
 774        SND_SOC_DAPM_SUPPLY("Mic Bias 1", DA7213_MICBIAS_CTRL,
 775                            DA7213_MICBIAS1_EN_SHIFT, DA7213_NO_INVERT,
 776                            NULL, 0),
 777        SND_SOC_DAPM_SUPPLY("Mic Bias 2", DA7213_MICBIAS_CTRL,
 778                            DA7213_MICBIAS2_EN_SHIFT, DA7213_NO_INVERT,
 779                            NULL, 0),
 780
 781        /* Input Mixers */
 782        SND_SOC_DAPM_MIXER("Mixin Left", SND_SOC_NOPM, 0, 0,
 783                           &da7213_dapm_mixinl_controls[0],
 784                           ARRAY_SIZE(da7213_dapm_mixinl_controls)),
 785        SND_SOC_DAPM_MIXER("Mixin Right", SND_SOC_NOPM, 0, 0,
 786                           &da7213_dapm_mixinr_controls[0],
 787                           ARRAY_SIZE(da7213_dapm_mixinr_controls)),
 788
 789        /* ADCs */
 790        SND_SOC_DAPM_ADC("ADC Left", NULL, DA7213_ADC_L_CTRL,
 791                         DA7213_ADC_EN_SHIFT, DA7213_NO_INVERT),
 792        SND_SOC_DAPM_ADC("ADC Right", NULL, DA7213_ADC_R_CTRL,
 793                         DA7213_ADC_EN_SHIFT, DA7213_NO_INVERT),
 794
 795        /* DAI */
 796        SND_SOC_DAPM_MUX("DAI Left Source MUX", SND_SOC_NOPM, 0, 0,
 797                         &da7213_dai_l_src_mux),
 798        SND_SOC_DAPM_MUX("DAI Right Source MUX", SND_SOC_NOPM, 0, 0,
 799                         &da7213_dai_r_src_mux),
 800        SND_SOC_DAPM_AIF_OUT("DAIOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
 801        SND_SOC_DAPM_AIF_OUT("DAIOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
 802
 803        /*
 804         * Output
 805         */
 806
 807        /* DAI */
 808        SND_SOC_DAPM_AIF_IN("DAIINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
 809        SND_SOC_DAPM_AIF_IN("DAIINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
 810        SND_SOC_DAPM_MUX("DAC Left Source MUX", SND_SOC_NOPM, 0, 0,
 811                         &da7213_dac_l_src_mux),
 812        SND_SOC_DAPM_MUX("DAC Right Source MUX", SND_SOC_NOPM, 0, 0,
 813                         &da7213_dac_r_src_mux),
 814
 815        /* DACs */
 816        SND_SOC_DAPM_DAC("DAC Left", NULL, DA7213_DAC_L_CTRL,
 817                         DA7213_DAC_EN_SHIFT, DA7213_NO_INVERT),
 818        SND_SOC_DAPM_DAC("DAC Right", NULL, DA7213_DAC_R_CTRL,
 819                         DA7213_DAC_EN_SHIFT, DA7213_NO_INVERT),
 820
 821        /* Output Mixers */
 822        SND_SOC_DAPM_MIXER("Mixout Left", SND_SOC_NOPM, 0, 0,
 823                           &da7213_dapm_mixoutl_controls[0],
 824                           ARRAY_SIZE(da7213_dapm_mixoutl_controls)),
 825        SND_SOC_DAPM_MIXER("Mixout Right", SND_SOC_NOPM, 0, 0,
 826                           &da7213_dapm_mixoutr_controls[0],
 827                           ARRAY_SIZE(da7213_dapm_mixoutr_controls)),
 828
 829        /* Output PGAs */
 830        SND_SOC_DAPM_PGA("Mixout Left PGA", DA7213_MIXOUT_L_CTRL,
 831                         DA7213_AMP_EN_SHIFT, DA7213_NO_INVERT, NULL, 0),
 832        SND_SOC_DAPM_PGA("Mixout Right PGA", DA7213_MIXOUT_R_CTRL,
 833                         DA7213_AMP_EN_SHIFT, DA7213_NO_INVERT, NULL, 0),
 834        SND_SOC_DAPM_PGA("Lineout PGA", DA7213_LINE_CTRL, DA7213_AMP_EN_SHIFT,
 835                         DA7213_NO_INVERT, NULL, 0),
 836        SND_SOC_DAPM_PGA("Headphone Left PGA", DA7213_HP_L_CTRL,
 837                         DA7213_AMP_EN_SHIFT, DA7213_NO_INVERT, NULL, 0),
 838        SND_SOC_DAPM_PGA("Headphone Right PGA", DA7213_HP_R_CTRL,
 839                         DA7213_AMP_EN_SHIFT, DA7213_NO_INVERT, NULL, 0),
 840
 841        /* Charge Pump */
 842        SND_SOC_DAPM_SUPPLY("Charge Pump", DA7213_CP_CTRL, DA7213_CP_EN_SHIFT,
 843                            DA7213_NO_INVERT, NULL, 0),
 844
 845        /* Output Lines */
 846        SND_SOC_DAPM_OUTPUT("HPL"),
 847        SND_SOC_DAPM_OUTPUT("HPR"),
 848        SND_SOC_DAPM_OUTPUT("LINE"),
 849};
 850
 851
 852/*
 853 * DAPM audio route definition
 854 */
 855
 856static const struct snd_soc_dapm_route da7213_audio_map[] = {
 857        /* Dest       Connecting Widget    source */
 858
 859        /* Input path */
 860        {"MIC1", NULL, "Mic Bias 1"},
 861        {"MIC2", NULL, "Mic Bias 2"},
 862
 863        {"Mic 1 Amp Source MUX", "Differential", "MIC1"},
 864        {"Mic 1 Amp Source MUX", "MIC_P", "MIC1"},
 865        {"Mic 1 Amp Source MUX", "MIC_N", "MIC1"},
 866
 867        {"Mic 2 Amp Source MUX", "Differential", "MIC2"},
 868        {"Mic 2 Amp Source MUX", "MIC_P", "MIC2"},
 869        {"Mic 2 Amp Source MUX", "MIC_N", "MIC2"},
 870
 871        {"Mic 1 PGA", NULL, "Mic 1 Amp Source MUX"},
 872        {"Mic 2 PGA", NULL, "Mic 2 Amp Source MUX"},
 873
 874        {"Aux Left PGA", NULL, "AUXL"},
 875        {"Aux Right PGA", NULL, "AUXR"},
 876
 877        {"Mixin Left", "Aux Left Switch", "Aux Left PGA"},
 878        {"Mixin Left", "Mic 1 Switch", "Mic 1 PGA"},
 879        {"Mixin Left", "Mic 2 Switch", "Mic 2 PGA"},
 880        {"Mixin Left", "Mixin Right Switch", "Mixin Right PGA"},
 881
 882        {"Mixin Right", "Aux Right Switch", "Aux Right PGA"},
 883        {"Mixin Right", "Mic 2 Switch", "Mic 2 PGA"},
 884        {"Mixin Right", "Mic 1 Switch", "Mic 1 PGA"},
 885        {"Mixin Right", "Mixin Left Switch", "Mixin Left PGA"},
 886
 887        {"Mixin Left PGA", NULL, "Mixin Left"},
 888        {"ADC Left", NULL, "Mixin Left PGA"},
 889
 890        {"Mixin Right PGA", NULL, "Mixin Right"},
 891        {"ADC Right", NULL, "Mixin Right PGA"},
 892
 893        {"DAI Left Source MUX", "ADC Left", "ADC Left"},
 894        {"DAI Left Source MUX", "ADC Right", "ADC Right"},
 895        {"DAI Left Source MUX", "DAI Input Left", "DAIINL"},
 896        {"DAI Left Source MUX", "DAI Input Right", "DAIINR"},
 897
 898        {"DAI Right Source MUX", "ADC Left", "ADC Left"},
 899        {"DAI Right Source MUX", "ADC Right", "ADC Right"},
 900        {"DAI Right Source MUX", "DAI Input Left", "DAIINL"},
 901        {"DAI Right Source MUX", "DAI Input Right", "DAIINR"},
 902
 903        {"DAIOUTL", NULL, "DAI Left Source MUX"},
 904        {"DAIOUTR", NULL, "DAI Right Source MUX"},
 905
 906        {"DAIOUTL", NULL, "DAI"},
 907        {"DAIOUTR", NULL, "DAI"},
 908
 909        /* Output path */
 910        {"DAIINL", NULL, "DAI"},
 911        {"DAIINR", NULL, "DAI"},
 912
 913        {"DAC Left Source MUX", "ADC Output Left", "ADC Left"},
 914        {"DAC Left Source MUX", "ADC Output Right", "ADC Right"},
 915        {"DAC Left Source MUX", "DAI Input Left", "DAIINL"},
 916        {"DAC Left Source MUX", "DAI Input Right", "DAIINR"},
 917
 918        {"DAC Right Source MUX", "ADC Output Left", "ADC Left"},
 919        {"DAC Right Source MUX", "ADC Output Right", "ADC Right"},
 920        {"DAC Right Source MUX", "DAI Input Left", "DAIINL"},
 921        {"DAC Right Source MUX", "DAI Input Right", "DAIINR"},
 922
 923        {"DAC Left", NULL, "DAC Left Source MUX"},
 924        {"DAC Right", NULL, "DAC Right Source MUX"},
 925
 926        {"Mixout Left", "Aux Left Switch", "Aux Left PGA"},
 927        {"Mixout Left", "Mixin Left Switch", "Mixin Left PGA"},
 928        {"Mixout Left", "Mixin Right Switch", "Mixin Right PGA"},
 929        {"Mixout Left", "DAC Left Switch", "DAC Left"},
 930        {"Mixout Left", "Aux Left Invert Switch", "Aux Left PGA"},
 931        {"Mixout Left", "Mixin Left Invert Switch", "Mixin Left PGA"},
 932        {"Mixout Left", "Mixin Right Invert Switch", "Mixin Right PGA"},
 933
 934        {"Mixout Right", "Aux Right Switch", "Aux Right PGA"},
 935        {"Mixout Right", "Mixin Right Switch", "Mixin Right PGA"},
 936        {"Mixout Right", "Mixin Left Switch", "Mixin Left PGA"},
 937        {"Mixout Right", "DAC Right Switch", "DAC Right"},
 938        {"Mixout Right", "Aux Right Invert Switch", "Aux Right PGA"},
 939        {"Mixout Right", "Mixin Right Invert Switch", "Mixin Right PGA"},
 940        {"Mixout Right", "Mixin Left Invert Switch", "Mixin Left PGA"},
 941
 942        {"Mixout Left PGA", NULL, "Mixout Left"},
 943        {"Mixout Right PGA", NULL, "Mixout Right"},
 944
 945        {"Headphone Left PGA", NULL, "Mixout Left PGA"},
 946        {"Headphone Left PGA", NULL, "Charge Pump"},
 947        {"HPL", NULL, "Headphone Left PGA"},
 948
 949        {"Headphone Right PGA", NULL, "Mixout Right PGA"},
 950        {"Headphone Right PGA", NULL, "Charge Pump"},
 951        {"HPR", NULL, "Headphone Right PGA"},
 952
 953        {"Lineout PGA", NULL, "Mixout Right PGA"},
 954        {"LINE", NULL, "Lineout PGA"},
 955};
 956
 957static struct reg_default da7213_reg_defaults[] = {
 958        { DA7213_DIG_ROUTING_DAI, 0x10 },
 959        { DA7213_SR, 0x0A },
 960        { DA7213_REFERENCES, 0x80 },
 961        { DA7213_PLL_FRAC_TOP, 0x00 },
 962        { DA7213_PLL_FRAC_BOT, 0x00 },
 963        { DA7213_PLL_INTEGER, 0x20 },
 964        { DA7213_PLL_CTRL, 0x0C },
 965        { DA7213_DAI_CLK_MODE, 0x01 },
 966        { DA7213_DAI_CTRL, 0x08 },
 967        { DA7213_DIG_ROUTING_DAC, 0x32 },
 968        { DA7213_AUX_L_GAIN, 0x35 },
 969        { DA7213_AUX_R_GAIN, 0x35 },
 970        { DA7213_MIXIN_L_SELECT, 0x00 },
 971        { DA7213_MIXIN_R_SELECT, 0x00 },
 972        { DA7213_MIXIN_L_GAIN, 0x03 },
 973        { DA7213_MIXIN_R_GAIN, 0x03 },
 974        { DA7213_ADC_L_GAIN, 0x6F },
 975        { DA7213_ADC_R_GAIN, 0x6F },
 976        { DA7213_ADC_FILTERS1, 0x80 },
 977        { DA7213_MIC_1_GAIN, 0x01 },
 978        { DA7213_MIC_2_GAIN, 0x01 },
 979        { DA7213_DAC_FILTERS5, 0x00 },
 980        { DA7213_DAC_FILTERS2, 0x88 },
 981        { DA7213_DAC_FILTERS3, 0x88 },
 982        { DA7213_DAC_FILTERS4, 0x08 },
 983        { DA7213_DAC_FILTERS1, 0x80 },
 984        { DA7213_DAC_L_GAIN, 0x6F },
 985        { DA7213_DAC_R_GAIN, 0x6F },
 986        { DA7213_CP_CTRL, 0x61 },
 987        { DA7213_HP_L_GAIN, 0x39 },
 988        { DA7213_HP_R_GAIN, 0x39 },
 989        { DA7213_LINE_GAIN, 0x30 },
 990        { DA7213_MIXOUT_L_SELECT, 0x00 },
 991        { DA7213_MIXOUT_R_SELECT, 0x00 },
 992        { DA7213_SYSTEM_MODES_INPUT, 0x00 },
 993        { DA7213_SYSTEM_MODES_OUTPUT, 0x00 },
 994        { DA7213_AUX_L_CTRL, 0x44 },
 995        { DA7213_AUX_R_CTRL, 0x44 },
 996        { DA7213_MICBIAS_CTRL, 0x11 },
 997        { DA7213_MIC_1_CTRL, 0x40 },
 998        { DA7213_MIC_2_CTRL, 0x40 },
 999        { DA7213_MIXIN_L_CTRL, 0x40 },
1000        { DA7213_MIXIN_R_CTRL, 0x40 },
1001        { DA7213_ADC_L_CTRL, 0x40 },
1002        { DA7213_ADC_R_CTRL, 0x40 },
1003        { DA7213_DAC_L_CTRL, 0x48 },
1004        { DA7213_DAC_R_CTRL, 0x40 },
1005        { DA7213_HP_L_CTRL, 0x41 },
1006        { DA7213_HP_R_CTRL, 0x40 },
1007        { DA7213_LINE_CTRL, 0x40 },
1008        { DA7213_MIXOUT_L_CTRL, 0x10 },
1009        { DA7213_MIXOUT_R_CTRL, 0x10 },
1010        { DA7213_LDO_CTRL, 0x00 },
1011        { DA7213_IO_CTRL, 0x00 },
1012        { DA7213_GAIN_RAMP_CTRL, 0x00},
1013        { DA7213_MIC_CONFIG, 0x00 },
1014        { DA7213_PC_COUNT, 0x00 },
1015        { DA7213_CP_VOL_THRESHOLD1, 0x32 },
1016        { DA7213_CP_DELAY, 0x95 },
1017        { DA7213_CP_DETECTOR, 0x00 },
1018        { DA7213_DAI_OFFSET, 0x00 },
1019        { DA7213_DIG_CTRL, 0x00 },
1020        { DA7213_ALC_CTRL2, 0x00 },
1021        { DA7213_ALC_CTRL3, 0x00 },
1022        { DA7213_ALC_NOISE, 0x3F },
1023        { DA7213_ALC_TARGET_MIN, 0x3F },
1024        { DA7213_ALC_TARGET_MAX, 0x00 },
1025        { DA7213_ALC_GAIN_LIMITS, 0xFF },
1026        { DA7213_ALC_ANA_GAIN_LIMITS, 0x71 },
1027        { DA7213_ALC_ANTICLIP_CTRL, 0x00 },
1028        { DA7213_ALC_ANTICLIP_LEVEL, 0x00 },
1029        { DA7213_ALC_OFFSET_MAN_M_L, 0x00 },
1030        { DA7213_ALC_OFFSET_MAN_U_L, 0x00 },
1031        { DA7213_ALC_OFFSET_MAN_M_R, 0x00 },
1032        { DA7213_ALC_OFFSET_MAN_U_R, 0x00 },
1033        { DA7213_ALC_CIC_OP_LVL_CTRL, 0x00 },
1034        { DA7213_DAC_NG_SETUP_TIME, 0x00 },
1035        { DA7213_DAC_NG_OFF_THRESHOLD, 0x00 },
1036        { DA7213_DAC_NG_ON_THRESHOLD, 0x00 },
1037        { DA7213_DAC_NG_CTRL, 0x00 },
1038};
1039
1040static bool da7213_volatile_register(struct device *dev, unsigned int reg)
1041{
1042        switch (reg) {
1043        case DA7213_STATUS1:
1044        case DA7213_PLL_STATUS:
1045        case DA7213_AUX_L_GAIN_STATUS:
1046        case DA7213_AUX_R_GAIN_STATUS:
1047        case DA7213_MIC_1_GAIN_STATUS:
1048        case DA7213_MIC_2_GAIN_STATUS:
1049        case DA7213_MIXIN_L_GAIN_STATUS:
1050        case DA7213_MIXIN_R_GAIN_STATUS:
1051        case DA7213_ADC_L_GAIN_STATUS:
1052        case DA7213_ADC_R_GAIN_STATUS:
1053        case DA7213_DAC_L_GAIN_STATUS:
1054        case DA7213_DAC_R_GAIN_STATUS:
1055        case DA7213_HP_L_GAIN_STATUS:
1056        case DA7213_HP_R_GAIN_STATUS:
1057        case DA7213_LINE_GAIN_STATUS:
1058        case DA7213_ALC_CTRL1:
1059        case DA7213_ALC_OFFSET_AUTO_M_L:
1060        case DA7213_ALC_OFFSET_AUTO_U_L:
1061        case DA7213_ALC_OFFSET_AUTO_M_R:
1062        case DA7213_ALC_OFFSET_AUTO_U_R:
1063        case DA7213_ALC_CIC_OP_LVL_DATA:
1064                return 1;
1065        default:
1066                return 0;
1067        }
1068}
1069
1070static int da7213_hw_params(struct snd_pcm_substream *substream,
1071                            struct snd_pcm_hw_params *params,
1072                            struct snd_soc_dai *dai)
1073{
1074        struct snd_soc_codec *codec = dai->codec;
1075        u8 dai_ctrl = 0;
1076        u8 fs;
1077
1078        /* Set DAI format */
1079        switch (params_width(params)) {
1080        case 16:
1081                dai_ctrl |= DA7213_DAI_WORD_LENGTH_S16_LE;
1082                break;
1083        case 20:
1084                dai_ctrl |= DA7213_DAI_WORD_LENGTH_S20_LE;
1085                break;
1086        case 24:
1087                dai_ctrl |= DA7213_DAI_WORD_LENGTH_S24_LE;
1088                break;
1089        case 32:
1090                dai_ctrl |= DA7213_DAI_WORD_LENGTH_S32_LE;
1091                break;
1092        default:
1093                return -EINVAL;
1094        }
1095
1096        /* Set sampling rate */
1097        switch (params_rate(params)) {
1098        case 8000:
1099                fs = DA7213_SR_8000;
1100                break;
1101        case 11025:
1102                fs = DA7213_SR_11025;
1103                break;
1104        case 12000:
1105                fs = DA7213_SR_12000;
1106                break;
1107        case 16000:
1108                fs = DA7213_SR_16000;
1109                break;
1110        case 22050:
1111                fs = DA7213_SR_22050;
1112                break;
1113        case 32000:
1114                fs = DA7213_SR_32000;
1115                break;
1116        case 44100:
1117                fs = DA7213_SR_44100;
1118                break;
1119        case 48000:
1120                fs = DA7213_SR_48000;
1121                break;
1122        case 88200:
1123                fs = DA7213_SR_88200;
1124                break;
1125        case 96000:
1126                fs = DA7213_SR_96000;
1127                break;
1128        default:
1129                return -EINVAL;
1130        }
1131
1132        snd_soc_update_bits(codec, DA7213_DAI_CTRL, DA7213_DAI_WORD_LENGTH_MASK,
1133                            dai_ctrl);
1134        snd_soc_write(codec, DA7213_SR, fs);
1135
1136        return 0;
1137}
1138
1139static int da7213_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
1140{
1141        struct snd_soc_codec *codec = codec_dai->codec;
1142        struct da7213_priv *da7213 = snd_soc_codec_get_drvdata(codec);
1143        u8 dai_clk_mode = 0, dai_ctrl = 0;
1144
1145        /* Set master/slave mode */
1146        switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1147        case SND_SOC_DAIFMT_CBM_CFM:
1148                dai_clk_mode |= DA7213_DAI_CLK_EN_MASTER_MODE;
1149                da7213->master = true;
1150                break;
1151        case SND_SOC_DAIFMT_CBS_CFS:
1152                dai_clk_mode |= DA7213_DAI_CLK_EN_SLAVE_MODE;
1153                da7213->master = false;
1154                break;
1155        default:
1156                return -EINVAL;
1157        }
1158
1159        /* Set clock normal/inverted */
1160        switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1161        case SND_SOC_DAIFMT_NB_NF:
1162                break;
1163        case SND_SOC_DAIFMT_NB_IF:
1164                dai_clk_mode |= DA7213_DAI_WCLK_POL_INV;
1165                break;
1166        case SND_SOC_DAIFMT_IB_NF:
1167                dai_clk_mode |= DA7213_DAI_CLK_POL_INV;
1168                break;
1169        case SND_SOC_DAIFMT_IB_IF:
1170                dai_clk_mode |= DA7213_DAI_WCLK_POL_INV | DA7213_DAI_CLK_POL_INV;
1171                break;
1172        default:
1173                return -EINVAL;
1174        }
1175
1176        /* Only I2S is supported */
1177        switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1178        case SND_SOC_DAIFMT_I2S:
1179                dai_ctrl |= DA7213_DAI_FORMAT_I2S_MODE;
1180                break;
1181        case SND_SOC_DAIFMT_LEFT_J:
1182                dai_ctrl |= DA7213_DAI_FORMAT_LEFT_J;
1183                break;
1184        case SND_SOC_DAIFMT_RIGHT_J:
1185                dai_ctrl |= DA7213_DAI_FORMAT_RIGHT_J;
1186                break;
1187        default:
1188                return -EINVAL;
1189        }
1190
1191        /* By default only 32 BCLK per WCLK is supported */
1192        dai_clk_mode |= DA7213_DAI_BCLKS_PER_WCLK_32;
1193
1194        snd_soc_write(codec, DA7213_DAI_CLK_MODE, dai_clk_mode);
1195        snd_soc_update_bits(codec, DA7213_DAI_CTRL, DA7213_DAI_FORMAT_MASK,
1196                            dai_ctrl);
1197
1198        return 0;
1199}
1200
1201static int da7213_mute(struct snd_soc_dai *dai, int mute)
1202{
1203        struct snd_soc_codec *codec = dai->codec;
1204
1205        if (mute) {
1206                snd_soc_update_bits(codec, DA7213_DAC_L_CTRL,
1207                                    DA7213_MUTE_EN, DA7213_MUTE_EN);
1208                snd_soc_update_bits(codec, DA7213_DAC_R_CTRL,
1209                                    DA7213_MUTE_EN, DA7213_MUTE_EN);
1210        } else {
1211                snd_soc_update_bits(codec, DA7213_DAC_L_CTRL,
1212                                    DA7213_MUTE_EN, 0);
1213                snd_soc_update_bits(codec, DA7213_DAC_R_CTRL,
1214                                    DA7213_MUTE_EN, 0);
1215        }
1216
1217        return 0;
1218}
1219
1220#define DA7213_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1221                        SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
1222
1223static int da7213_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1224                                 int clk_id, unsigned int freq, int dir)
1225{
1226        struct snd_soc_codec *codec = codec_dai->codec;
1227        struct da7213_priv *da7213 = snd_soc_codec_get_drvdata(codec);
1228
1229        switch (clk_id) {
1230        case DA7213_CLKSRC_MCLK:
1231                if ((freq == 32768) ||
1232                    ((freq >= 5000000) && (freq <= 54000000))) {
1233                        da7213->mclk_rate = freq;
1234                        return 0;
1235                } else {
1236                        dev_err(codec_dai->dev, "Unsupported MCLK value %d\n",
1237                                freq);
1238                        return -EINVAL;
1239                }
1240                break;
1241        default:
1242                dev_err(codec_dai->dev, "Unknown clock source %d\n", clk_id);
1243                return -EINVAL;
1244        }
1245}
1246
1247/* Supported PLL input frequencies are 5MHz - 54MHz. */
1248static int da7213_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
1249                              int source, unsigned int fref, unsigned int fout)
1250{
1251        struct snd_soc_codec *codec = codec_dai->codec;
1252        struct da7213_priv *da7213 = snd_soc_codec_get_drvdata(codec);
1253
1254        u8 pll_ctrl, indiv_bits, indiv;
1255        u8 pll_frac_top, pll_frac_bot, pll_integer;
1256        u32 freq_ref;
1257        u64 frac_div;
1258
1259        /* Reset PLL configuration */
1260        snd_soc_write(codec, DA7213_PLL_CTRL, 0);
1261
1262        pll_ctrl = 0;
1263
1264        /* Workout input divider based on MCLK rate */
1265        if ((da7213->mclk_rate == 32768) && (source == DA7213_SYSCLK_PLL)) {
1266                /* 32KHz PLL Mode */
1267                indiv_bits = DA7213_PLL_INDIV_10_20_MHZ;
1268                indiv = DA7213_PLL_INDIV_10_20_MHZ_VAL;
1269                freq_ref = 3750000;
1270                pll_ctrl |= DA7213_PLL_32K_MODE;
1271        } else {
1272                /* 5 - 54MHz MCLK */
1273                if (da7213->mclk_rate < 5000000) {
1274                        goto pll_err;
1275                } else if (da7213->mclk_rate <= 10000000) {
1276                        indiv_bits = DA7213_PLL_INDIV_5_10_MHZ;
1277                        indiv = DA7213_PLL_INDIV_5_10_MHZ_VAL;
1278                } else if (da7213->mclk_rate <= 20000000) {
1279                        indiv_bits = DA7213_PLL_INDIV_10_20_MHZ;
1280                        indiv = DA7213_PLL_INDIV_10_20_MHZ_VAL;
1281                } else if (da7213->mclk_rate <= 40000000) {
1282                        indiv_bits = DA7213_PLL_INDIV_20_40_MHZ;
1283                        indiv = DA7213_PLL_INDIV_20_40_MHZ_VAL;
1284                } else if (da7213->mclk_rate <= 54000000) {
1285                        indiv_bits = DA7213_PLL_INDIV_40_54_MHZ;
1286                        indiv = DA7213_PLL_INDIV_40_54_MHZ_VAL;
1287                } else {
1288                        goto pll_err;
1289                }
1290                freq_ref = (da7213->mclk_rate / indiv);
1291        }
1292
1293        pll_ctrl |= indiv_bits;
1294
1295        /* PLL Bypass mode */
1296        if (source == DA7213_SYSCLK_MCLK) {
1297                snd_soc_write(codec, DA7213_PLL_CTRL, pll_ctrl);
1298                return 0;
1299        }
1300
1301        /*
1302         * If Codec is slave and SRM enabled,
1303         * freq_out is (98304000 + 90316800)/2 = 94310400
1304         */
1305        if (!da7213->master && da7213->srm_en) {
1306                fout = DA7213_PLL_FREQ_OUT_94310400;
1307                pll_ctrl |= DA7213_PLL_SRM_EN;
1308        }
1309
1310        /* Enable MCLK squarer if required */
1311        if (da7213->mclk_squarer_en)
1312                pll_ctrl |= DA7213_PLL_MCLK_SQR_EN;
1313
1314        /* Calculate dividers for PLL */
1315        pll_integer = fout / freq_ref;
1316        frac_div = (u64)(fout % freq_ref) * 8192ULL;
1317        do_div(frac_div, freq_ref);
1318        pll_frac_top = (frac_div >> DA7213_BYTE_SHIFT) & DA7213_BYTE_MASK;
1319        pll_frac_bot = (frac_div) & DA7213_BYTE_MASK;
1320
1321        /* Write PLL dividers */
1322        snd_soc_write(codec, DA7213_PLL_FRAC_TOP, pll_frac_top);
1323        snd_soc_write(codec, DA7213_PLL_FRAC_BOT, pll_frac_bot);
1324        snd_soc_write(codec, DA7213_PLL_INTEGER, pll_integer);
1325
1326        /* Enable PLL */
1327        pll_ctrl |= DA7213_PLL_EN;
1328        snd_soc_write(codec, DA7213_PLL_CTRL, pll_ctrl);
1329
1330        return 0;
1331
1332pll_err:
1333        dev_err(codec_dai->dev, "Unsupported PLL input frequency %d\n",
1334                da7213->mclk_rate);
1335        return -EINVAL;
1336}
1337
1338/* DAI operations */
1339static const struct snd_soc_dai_ops da7213_dai_ops = {
1340        .hw_params      = da7213_hw_params,
1341        .set_fmt        = da7213_set_dai_fmt,
1342        .set_sysclk     = da7213_set_dai_sysclk,
1343        .set_pll        = da7213_set_dai_pll,
1344        .digital_mute   = da7213_mute,
1345};
1346
1347static struct snd_soc_dai_driver da7213_dai = {
1348        .name = "da7213-hifi",
1349        /* Playback Capabilities */
1350        .playback = {
1351                .stream_name = "Playback",
1352                .channels_min = 1,
1353                .channels_max = 2,
1354                .rates = SNDRV_PCM_RATE_8000_96000,
1355                .formats = DA7213_FORMATS,
1356        },
1357        /* Capture Capabilities */
1358        .capture = {
1359                .stream_name = "Capture",
1360                .channels_min = 1,
1361                .channels_max = 2,
1362                .rates = SNDRV_PCM_RATE_8000_96000,
1363                .formats = DA7213_FORMATS,
1364        },
1365        .ops = &da7213_dai_ops,
1366        .symmetric_rates = 1,
1367};
1368
1369static int da7213_set_bias_level(struct snd_soc_codec *codec,
1370                                 enum snd_soc_bias_level level)
1371{
1372        switch (level) {
1373        case SND_SOC_BIAS_ON:
1374        case SND_SOC_BIAS_PREPARE:
1375                break;
1376        case SND_SOC_BIAS_STANDBY:
1377                if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
1378                        /* Enable VMID reference & master bias */
1379                        snd_soc_update_bits(codec, DA7213_REFERENCES,
1380                                            DA7213_VMID_EN | DA7213_BIAS_EN,
1381                                            DA7213_VMID_EN | DA7213_BIAS_EN);
1382                }
1383                break;
1384        case SND_SOC_BIAS_OFF:
1385                /* Disable VMID reference & master bias */
1386                snd_soc_update_bits(codec, DA7213_REFERENCES,
1387                                    DA7213_VMID_EN | DA7213_BIAS_EN, 0);
1388                break;
1389        }
1390        codec->dapm.bias_level = level;
1391        return 0;
1392}
1393
1394static int da7213_probe(struct snd_soc_codec *codec)
1395{
1396        struct da7213_priv *da7213 = snd_soc_codec_get_drvdata(codec);
1397        struct da7213_platform_data *pdata = da7213->pdata;
1398
1399        /* Default to using ALC auto offset calibration mode. */
1400        snd_soc_update_bits(codec, DA7213_ALC_CTRL1,
1401                            DA7213_ALC_CALIB_MODE_MAN, 0);
1402        da7213->alc_calib_auto = true;
1403
1404        /* Default to using SRM for slave mode */
1405        da7213->srm_en = true;
1406
1407        /* Enable all Gain Ramps */
1408        snd_soc_update_bits(codec, DA7213_AUX_L_CTRL,
1409                            DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1410        snd_soc_update_bits(codec, DA7213_AUX_R_CTRL,
1411                            DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1412        snd_soc_update_bits(codec, DA7213_MIXIN_L_CTRL,
1413                            DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1414        snd_soc_update_bits(codec, DA7213_MIXIN_R_CTRL,
1415                            DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1416        snd_soc_update_bits(codec, DA7213_ADC_L_CTRL,
1417                            DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1418        snd_soc_update_bits(codec, DA7213_ADC_R_CTRL,
1419                            DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1420        snd_soc_update_bits(codec, DA7213_DAC_L_CTRL,
1421                            DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1422        snd_soc_update_bits(codec, DA7213_DAC_R_CTRL,
1423                            DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1424        snd_soc_update_bits(codec, DA7213_HP_L_CTRL,
1425                            DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1426        snd_soc_update_bits(codec, DA7213_HP_R_CTRL,
1427                            DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1428        snd_soc_update_bits(codec, DA7213_LINE_CTRL,
1429                            DA7213_GAIN_RAMP_EN, DA7213_GAIN_RAMP_EN);
1430
1431        /*
1432         * There are two separate control bits for input and output mixers as
1433         * well as headphone and line outs.
1434         * One to enable corresponding amplifier and other to enable its
1435         * output. As amplifier bits are related to power control, they are
1436         * being managed by DAPM while other (non power related) bits are
1437         * enabled here
1438         */
1439        snd_soc_update_bits(codec, DA7213_MIXIN_L_CTRL,
1440                            DA7213_MIXIN_MIX_EN, DA7213_MIXIN_MIX_EN);
1441        snd_soc_update_bits(codec, DA7213_MIXIN_R_CTRL,
1442                            DA7213_MIXIN_MIX_EN, DA7213_MIXIN_MIX_EN);
1443
1444        snd_soc_update_bits(codec, DA7213_MIXOUT_L_CTRL,
1445                            DA7213_MIXOUT_MIX_EN, DA7213_MIXOUT_MIX_EN);
1446        snd_soc_update_bits(codec, DA7213_MIXOUT_R_CTRL,
1447                            DA7213_MIXOUT_MIX_EN, DA7213_MIXOUT_MIX_EN);
1448
1449        snd_soc_update_bits(codec, DA7213_HP_L_CTRL,
1450                            DA7213_HP_AMP_OE, DA7213_HP_AMP_OE);
1451        snd_soc_update_bits(codec, DA7213_HP_R_CTRL,
1452                            DA7213_HP_AMP_OE, DA7213_HP_AMP_OE);
1453
1454        snd_soc_update_bits(codec, DA7213_LINE_CTRL,
1455                            DA7213_LINE_AMP_OE, DA7213_LINE_AMP_OE);
1456
1457        /* Set platform data values */
1458        if (da7213->pdata) {
1459                u8 micbias_lvl = 0, dmic_cfg = 0;
1460
1461                /* Set Mic Bias voltages */
1462                switch (pdata->micbias1_lvl) {
1463                case DA7213_MICBIAS_1_6V:
1464                case DA7213_MICBIAS_2_2V:
1465                case DA7213_MICBIAS_2_5V:
1466                case DA7213_MICBIAS_3_0V:
1467                        micbias_lvl |= (pdata->micbias1_lvl <<
1468                                        DA7213_MICBIAS1_LEVEL_SHIFT);
1469                        break;
1470                }
1471                switch (pdata->micbias2_lvl) {
1472                case DA7213_MICBIAS_1_6V:
1473                case DA7213_MICBIAS_2_2V:
1474                case DA7213_MICBIAS_2_5V:
1475                case DA7213_MICBIAS_3_0V:
1476                        micbias_lvl |= (pdata->micbias2_lvl <<
1477                                         DA7213_MICBIAS2_LEVEL_SHIFT);
1478                        break;
1479                }
1480                snd_soc_update_bits(codec, DA7213_MICBIAS_CTRL,
1481                                    DA7213_MICBIAS1_LEVEL_MASK |
1482                                    DA7213_MICBIAS2_LEVEL_MASK, micbias_lvl);
1483
1484                /* Set DMIC configuration */
1485                switch (pdata->dmic_data_sel) {
1486                case DA7213_DMIC_DATA_LFALL_RRISE:
1487                case DA7213_DMIC_DATA_LRISE_RFALL:
1488                        dmic_cfg |= (pdata->dmic_data_sel <<
1489                                     DA7213_DMIC_DATA_SEL_SHIFT);
1490                        break;
1491                }
1492                switch (pdata->dmic_samplephase) {
1493                case DA7213_DMIC_SAMPLE_ON_CLKEDGE:
1494                case DA7213_DMIC_SAMPLE_BETWEEN_CLKEDGE:
1495                        dmic_cfg |= (pdata->dmic_samplephase <<
1496                                     DA7213_DMIC_SAMPLEPHASE_SHIFT);
1497                        break;
1498                }
1499                switch (pdata->dmic_clk_rate) {
1500                case DA7213_DMIC_CLK_3_0MHZ:
1501                case DA7213_DMIC_CLK_1_5MHZ:
1502                        dmic_cfg |= (pdata->dmic_clk_rate <<
1503                                     DA7213_DMIC_CLK_RATE_SHIFT);
1504                        break;
1505                }
1506                snd_soc_update_bits(codec, DA7213_MIC_CONFIG,
1507                                    DA7213_DMIC_DATA_SEL_MASK |
1508                                    DA7213_DMIC_SAMPLEPHASE_MASK |
1509                                    DA7213_DMIC_CLK_RATE_MASK, dmic_cfg);
1510
1511                /* Set MCLK squaring */
1512                da7213->mclk_squarer_en = pdata->mclk_squaring;
1513        }
1514        return 0;
1515}
1516
1517static struct snd_soc_codec_driver soc_codec_dev_da7213 = {
1518        .probe                  = da7213_probe,
1519        .set_bias_level         = da7213_set_bias_level,
1520
1521        .controls               = da7213_snd_controls,
1522        .num_controls           = ARRAY_SIZE(da7213_snd_controls),
1523
1524        .dapm_widgets           = da7213_dapm_widgets,
1525        .num_dapm_widgets       = ARRAY_SIZE(da7213_dapm_widgets),
1526        .dapm_routes            = da7213_audio_map,
1527        .num_dapm_routes        = ARRAY_SIZE(da7213_audio_map),
1528};
1529
1530static const struct regmap_config da7213_regmap_config = {
1531        .reg_bits = 8,
1532        .val_bits = 8,
1533
1534        .reg_defaults = da7213_reg_defaults,
1535        .num_reg_defaults = ARRAY_SIZE(da7213_reg_defaults),
1536        .volatile_reg = da7213_volatile_register,
1537        .cache_type = REGCACHE_RBTREE,
1538};
1539
1540static int da7213_i2c_probe(struct i2c_client *i2c,
1541                            const struct i2c_device_id *id)
1542{
1543        struct da7213_priv *da7213;
1544        struct da7213_platform_data *pdata = dev_get_platdata(&i2c->dev);
1545        int ret;
1546
1547        da7213 = devm_kzalloc(&i2c->dev, sizeof(struct da7213_priv),
1548                              GFP_KERNEL);
1549        if (!da7213)
1550                return -ENOMEM;
1551
1552        if (pdata)
1553                da7213->pdata = pdata;
1554
1555        i2c_set_clientdata(i2c, da7213);
1556
1557        da7213->regmap = devm_regmap_init_i2c(i2c, &da7213_regmap_config);
1558        if (IS_ERR(da7213->regmap)) {
1559                ret = PTR_ERR(da7213->regmap);
1560                dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
1561                return ret;
1562        }
1563
1564        ret = snd_soc_register_codec(&i2c->dev,
1565                        &soc_codec_dev_da7213, &da7213_dai, 1);
1566        if (ret < 0) {
1567                dev_err(&i2c->dev, "Failed to register da7213 codec: %d\n",
1568                        ret);
1569        }
1570        return ret;
1571}
1572
1573static int da7213_remove(struct i2c_client *client)
1574{
1575        snd_soc_unregister_codec(&client->dev);
1576        return 0;
1577}
1578
1579static const struct i2c_device_id da7213_i2c_id[] = {
1580        { "da7213", 0 },
1581        { }
1582};
1583MODULE_DEVICE_TABLE(i2c, da7213_i2c_id);
1584
1585/* I2C codec control layer */
1586static struct i2c_driver da7213_i2c_driver = {
1587        .driver = {
1588                .name = "da7213",
1589                .owner = THIS_MODULE,
1590        },
1591        .probe          = da7213_i2c_probe,
1592        .remove         = da7213_remove,
1593        .id_table       = da7213_i2c_id,
1594};
1595
1596module_i2c_driver(da7213_i2c_driver);
1597
1598MODULE_DESCRIPTION("ASoC DA7213 Codec driver");
1599MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
1600MODULE_LICENSE("GPL");
1601