linux/sound/soc/codecs/alc5623.c
<<
>>
Prefs
   1/*
   2 * alc5623.c  --  alc562[123] ALSA Soc Audio driver
   3 *
   4 * Copyright 2008 Realtek Microelectronics
   5 * Author: flove <flove@realtek.com> Ethan <eku@marvell.com>
   6 *
   7 * Copyright 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
   8 *
   9 *
  10 * Based on WM8753.c
  11 *
  12 * This program is free software; you can redistribute it and/or modify
  13 * it under the terms of the GNU General Public License version 2 as
  14 * published by the Free Software Foundation.
  15 *
  16 */
  17
  18#include <linux/module.h>
  19#include <linux/kernel.h>
  20#include <linux/init.h>
  21#include <linux/delay.h>
  22#include <linux/pm.h>
  23#include <linux/i2c.h>
  24#include <linux/regmap.h>
  25#include <linux/slab.h>
  26#include <linux/of.h>
  27#include <sound/core.h>
  28#include <sound/pcm.h>
  29#include <sound/pcm_params.h>
  30#include <sound/tlv.h>
  31#include <sound/soc.h>
  32#include <sound/initval.h>
  33#include <sound/alc5623.h>
  34
  35#include "alc5623.h"
  36
  37static int caps_charge = 2000;
  38module_param(caps_charge, int, 0);
  39MODULE_PARM_DESC(caps_charge, "ALC5623 cap charge time (msecs)");
  40
  41/* codec private data */
  42struct alc5623_priv {
  43        struct regmap *regmap;
  44        u8 id;
  45        unsigned int sysclk;
  46        unsigned int add_ctrl;
  47        unsigned int jack_det_ctrl;
  48};
  49
  50static inline int alc5623_reset(struct snd_soc_codec *codec)
  51{
  52        return snd_soc_write(codec, ALC5623_RESET, 0);
  53}
  54
  55static int amp_mixer_event(struct snd_soc_dapm_widget *w,
  56        struct snd_kcontrol *kcontrol, int event)
  57{
  58        struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  59
  60        /* to power-on/off class-d amp generators/speaker */
  61        /* need to write to 'index-46h' register :        */
  62        /* so write index num (here 0x46) to reg 0x6a     */
  63        /* and then 0xffff/0 to reg 0x6c                  */
  64        snd_soc_write(codec, ALC5623_HID_CTRL_INDEX, 0x46);
  65
  66        switch (event) {
  67        case SND_SOC_DAPM_PRE_PMU:
  68                snd_soc_write(codec, ALC5623_HID_CTRL_DATA, 0xFFFF);
  69                break;
  70        case SND_SOC_DAPM_POST_PMD:
  71                snd_soc_write(codec, ALC5623_HID_CTRL_DATA, 0);
  72                break;
  73        }
  74
  75        return 0;
  76}
  77
  78/*
  79 * ALC5623 Controls
  80 */
  81
  82static const DECLARE_TLV_DB_SCALE(vol_tlv, -3450, 150, 0);
  83static const DECLARE_TLV_DB_SCALE(hp_tlv, -4650, 150, 0);
  84static const DECLARE_TLV_DB_SCALE(adc_rec_tlv, -1650, 150, 0);
  85static const DECLARE_TLV_DB_RANGE(boost_tlv,
  86        0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
  87        1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
  88        2, 2, TLV_DB_SCALE_ITEM(3000, 0, 0)
  89);
  90static const DECLARE_TLV_DB_SCALE(dig_tlv, 0, 600, 0);
  91
  92static const struct snd_kcontrol_new alc5621_vol_snd_controls[] = {
  93        SOC_DOUBLE_TLV("Speaker Playback Volume",
  94                        ALC5623_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
  95        SOC_DOUBLE("Speaker Playback Switch",
  96                        ALC5623_SPK_OUT_VOL, 15, 7, 1, 1),
  97        SOC_DOUBLE_TLV("Headphone Playback Volume",
  98                        ALC5623_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
  99        SOC_DOUBLE("Headphone Playback Switch",
 100                        ALC5623_HP_OUT_VOL, 15, 7, 1, 1),
 101};
 102
 103static const struct snd_kcontrol_new alc5622_vol_snd_controls[] = {
 104        SOC_DOUBLE_TLV("Speaker Playback Volume",
 105                        ALC5623_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
 106        SOC_DOUBLE("Speaker Playback Switch",
 107                        ALC5623_SPK_OUT_VOL, 15, 7, 1, 1),
 108        SOC_DOUBLE_TLV("Line Playback Volume",
 109                        ALC5623_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
 110        SOC_DOUBLE("Line Playback Switch",
 111                        ALC5623_HP_OUT_VOL, 15, 7, 1, 1),
 112};
 113
 114static const struct snd_kcontrol_new alc5623_vol_snd_controls[] = {
 115        SOC_DOUBLE_TLV("Line Playback Volume",
 116                        ALC5623_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
 117        SOC_DOUBLE("Line Playback Switch",
 118                        ALC5623_SPK_OUT_VOL, 15, 7, 1, 1),
 119        SOC_DOUBLE_TLV("Headphone Playback Volume",
 120                        ALC5623_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
 121        SOC_DOUBLE("Headphone Playback Switch",
 122                        ALC5623_HP_OUT_VOL, 15, 7, 1, 1),
 123};
 124
 125static const struct snd_kcontrol_new alc5623_snd_controls[] = {
 126        SOC_DOUBLE_TLV("Auxout Playback Volume",
 127                        ALC5623_MONO_AUX_OUT_VOL, 8, 0, 31, 1, hp_tlv),
 128        SOC_DOUBLE("Auxout Playback Switch",
 129                        ALC5623_MONO_AUX_OUT_VOL, 15, 7, 1, 1),
 130        SOC_DOUBLE_TLV("PCM Playback Volume",
 131                        ALC5623_STEREO_DAC_VOL, 8, 0, 31, 1, vol_tlv),
 132        SOC_DOUBLE_TLV("AuxI Capture Volume",
 133                        ALC5623_AUXIN_VOL, 8, 0, 31, 1, vol_tlv),
 134        SOC_DOUBLE_TLV("LineIn Capture Volume",
 135                        ALC5623_LINE_IN_VOL, 8, 0, 31, 1, vol_tlv),
 136        SOC_SINGLE_TLV("Mic1 Capture Volume",
 137                        ALC5623_MIC_VOL, 8, 31, 1, vol_tlv),
 138        SOC_SINGLE_TLV("Mic2 Capture Volume",
 139                        ALC5623_MIC_VOL, 0, 31, 1, vol_tlv),
 140        SOC_DOUBLE_TLV("Rec Capture Volume",
 141                        ALC5623_ADC_REC_GAIN, 7, 0, 31, 0, adc_rec_tlv),
 142        SOC_SINGLE_TLV("Mic 1 Boost Volume",
 143                        ALC5623_MIC_CTRL, 10, 2, 0, boost_tlv),
 144        SOC_SINGLE_TLV("Mic 2 Boost Volume",
 145                        ALC5623_MIC_CTRL, 8, 2, 0, boost_tlv),
 146        SOC_SINGLE_TLV("Digital Boost Volume",
 147                        ALC5623_ADD_CTRL_REG, 4, 3, 0, dig_tlv),
 148};
 149
 150/*
 151 * DAPM Controls
 152 */
 153static const struct snd_kcontrol_new alc5623_hp_mixer_controls[] = {
 154SOC_DAPM_SINGLE("LI2HP Playback Switch", ALC5623_LINE_IN_VOL, 15, 1, 1),
 155SOC_DAPM_SINGLE("AUXI2HP Playback Switch", ALC5623_AUXIN_VOL, 15, 1, 1),
 156SOC_DAPM_SINGLE("MIC12HP Playback Switch", ALC5623_MIC_ROUTING_CTRL, 15, 1, 1),
 157SOC_DAPM_SINGLE("MIC22HP Playback Switch", ALC5623_MIC_ROUTING_CTRL, 7, 1, 1),
 158SOC_DAPM_SINGLE("DAC2HP Playback Switch", ALC5623_STEREO_DAC_VOL, 15, 1, 1),
 159};
 160
 161static const struct snd_kcontrol_new alc5623_hpl_mixer_controls[] = {
 162SOC_DAPM_SINGLE("ADC2HP_L Playback Switch", ALC5623_ADC_REC_GAIN, 15, 1, 1),
 163};
 164
 165static const struct snd_kcontrol_new alc5623_hpr_mixer_controls[] = {
 166SOC_DAPM_SINGLE("ADC2HP_R Playback Switch", ALC5623_ADC_REC_GAIN, 14, 1, 1),
 167};
 168
 169static const struct snd_kcontrol_new alc5623_mono_mixer_controls[] = {
 170SOC_DAPM_SINGLE("ADC2MONO_L Playback Switch", ALC5623_ADC_REC_GAIN, 13, 1, 1),
 171SOC_DAPM_SINGLE("ADC2MONO_R Playback Switch", ALC5623_ADC_REC_GAIN, 12, 1, 1),
 172SOC_DAPM_SINGLE("LI2MONO Playback Switch", ALC5623_LINE_IN_VOL, 13, 1, 1),
 173SOC_DAPM_SINGLE("AUXI2MONO Playback Switch", ALC5623_AUXIN_VOL, 13, 1, 1),
 174SOC_DAPM_SINGLE("MIC12MONO Playback Switch", ALC5623_MIC_ROUTING_CTRL, 13, 1, 1),
 175SOC_DAPM_SINGLE("MIC22MONO Playback Switch", ALC5623_MIC_ROUTING_CTRL, 5, 1, 1),
 176SOC_DAPM_SINGLE("DAC2MONO Playback Switch", ALC5623_STEREO_DAC_VOL, 13, 1, 1),
 177};
 178
 179static const struct snd_kcontrol_new alc5623_speaker_mixer_controls[] = {
 180SOC_DAPM_SINGLE("LI2SPK Playback Switch", ALC5623_LINE_IN_VOL, 14, 1, 1),
 181SOC_DAPM_SINGLE("AUXI2SPK Playback Switch", ALC5623_AUXIN_VOL, 14, 1, 1),
 182SOC_DAPM_SINGLE("MIC12SPK Playback Switch", ALC5623_MIC_ROUTING_CTRL, 14, 1, 1),
 183SOC_DAPM_SINGLE("MIC22SPK Playback Switch", ALC5623_MIC_ROUTING_CTRL, 6, 1, 1),
 184SOC_DAPM_SINGLE("DAC2SPK Playback Switch", ALC5623_STEREO_DAC_VOL, 14, 1, 1),
 185};
 186
 187/* Left Record Mixer */
 188static const struct snd_kcontrol_new alc5623_captureL_mixer_controls[] = {
 189SOC_DAPM_SINGLE("Mic1 Capture Switch", ALC5623_ADC_REC_MIXER, 14, 1, 1),
 190SOC_DAPM_SINGLE("Mic2 Capture Switch", ALC5623_ADC_REC_MIXER, 13, 1, 1),
 191SOC_DAPM_SINGLE("LineInL Capture Switch", ALC5623_ADC_REC_MIXER, 12, 1, 1),
 192SOC_DAPM_SINGLE("Left AuxI Capture Switch", ALC5623_ADC_REC_MIXER, 11, 1, 1),
 193SOC_DAPM_SINGLE("HPMixerL Capture Switch", ALC5623_ADC_REC_MIXER, 10, 1, 1),
 194SOC_DAPM_SINGLE("SPKMixer Capture Switch", ALC5623_ADC_REC_MIXER, 9, 1, 1),
 195SOC_DAPM_SINGLE("MonoMixer Capture Switch", ALC5623_ADC_REC_MIXER, 8, 1, 1),
 196};
 197
 198/* Right Record Mixer */
 199static const struct snd_kcontrol_new alc5623_captureR_mixer_controls[] = {
 200SOC_DAPM_SINGLE("Mic1 Capture Switch", ALC5623_ADC_REC_MIXER, 6, 1, 1),
 201SOC_DAPM_SINGLE("Mic2 Capture Switch", ALC5623_ADC_REC_MIXER, 5, 1, 1),
 202SOC_DAPM_SINGLE("LineInR Capture Switch", ALC5623_ADC_REC_MIXER, 4, 1, 1),
 203SOC_DAPM_SINGLE("Right AuxI Capture Switch", ALC5623_ADC_REC_MIXER, 3, 1, 1),
 204SOC_DAPM_SINGLE("HPMixerR Capture Switch", ALC5623_ADC_REC_MIXER, 2, 1, 1),
 205SOC_DAPM_SINGLE("SPKMixer Capture Switch", ALC5623_ADC_REC_MIXER, 1, 1, 1),
 206SOC_DAPM_SINGLE("MonoMixer Capture Switch", ALC5623_ADC_REC_MIXER, 0, 1, 1),
 207};
 208
 209static const char *alc5623_spk_n_sour_sel[] = {
 210                "RN/-R", "RP/+R", "LN/-R", "Vmid" };
 211static const char *alc5623_hpl_out_input_sel[] = {
 212                "Vmid", "HP Left Mix"};
 213static const char *alc5623_hpr_out_input_sel[] = {
 214                "Vmid", "HP Right Mix"};
 215static const char *alc5623_spkout_input_sel[] = {
 216                "Vmid", "HPOut Mix", "Speaker Mix", "Mono Mix"};
 217static const char *alc5623_aux_out_input_sel[] = {
 218                "Vmid", "HPOut Mix", "Speaker Mix", "Mono Mix"};
 219
 220/* auxout output mux */
 221static SOC_ENUM_SINGLE_DECL(alc5623_aux_out_input_enum,
 222                            ALC5623_OUTPUT_MIXER_CTRL, 6,
 223                            alc5623_aux_out_input_sel);
 224static const struct snd_kcontrol_new alc5623_auxout_mux_controls =
 225SOC_DAPM_ENUM("Route", alc5623_aux_out_input_enum);
 226
 227/* speaker output mux */
 228static SOC_ENUM_SINGLE_DECL(alc5623_spkout_input_enum,
 229                            ALC5623_OUTPUT_MIXER_CTRL, 10,
 230                            alc5623_spkout_input_sel);
 231static const struct snd_kcontrol_new alc5623_spkout_mux_controls =
 232SOC_DAPM_ENUM("Route", alc5623_spkout_input_enum);
 233
 234/* headphone left output mux */
 235static SOC_ENUM_SINGLE_DECL(alc5623_hpl_out_input_enum,
 236                            ALC5623_OUTPUT_MIXER_CTRL, 9,
 237                            alc5623_hpl_out_input_sel);
 238static const struct snd_kcontrol_new alc5623_hpl_out_mux_controls =
 239SOC_DAPM_ENUM("Route", alc5623_hpl_out_input_enum);
 240
 241/* headphone right output mux */
 242static SOC_ENUM_SINGLE_DECL(alc5623_hpr_out_input_enum,
 243                            ALC5623_OUTPUT_MIXER_CTRL, 8,
 244                            alc5623_hpr_out_input_sel);
 245static const struct snd_kcontrol_new alc5623_hpr_out_mux_controls =
 246SOC_DAPM_ENUM("Route", alc5623_hpr_out_input_enum);
 247
 248/* speaker output N select */
 249static SOC_ENUM_SINGLE_DECL(alc5623_spk_n_sour_enum,
 250                            ALC5623_OUTPUT_MIXER_CTRL, 14,
 251                            alc5623_spk_n_sour_sel);
 252static const struct snd_kcontrol_new alc5623_spkoutn_mux_controls =
 253SOC_DAPM_ENUM("Route", alc5623_spk_n_sour_enum);
 254
 255static const struct snd_soc_dapm_widget alc5623_dapm_widgets[] = {
 256/* Muxes */
 257SND_SOC_DAPM_MUX("AuxOut Mux", SND_SOC_NOPM, 0, 0,
 258        &alc5623_auxout_mux_controls),
 259SND_SOC_DAPM_MUX("SpeakerOut Mux", SND_SOC_NOPM, 0, 0,
 260        &alc5623_spkout_mux_controls),
 261SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
 262        &alc5623_hpl_out_mux_controls),
 263SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
 264        &alc5623_hpr_out_mux_controls),
 265SND_SOC_DAPM_MUX("SpeakerOut N Mux", SND_SOC_NOPM, 0, 0,
 266        &alc5623_spkoutn_mux_controls),
 267
 268/* output mixers */
 269SND_SOC_DAPM_MIXER("HP Mix", SND_SOC_NOPM, 0, 0,
 270        &alc5623_hp_mixer_controls[0],
 271        ARRAY_SIZE(alc5623_hp_mixer_controls)),
 272SND_SOC_DAPM_MIXER("HPR Mix", ALC5623_PWR_MANAG_ADD2, 4, 0,
 273        &alc5623_hpr_mixer_controls[0],
 274        ARRAY_SIZE(alc5623_hpr_mixer_controls)),
 275SND_SOC_DAPM_MIXER("HPL Mix", ALC5623_PWR_MANAG_ADD2, 5, 0,
 276        &alc5623_hpl_mixer_controls[0],
 277        ARRAY_SIZE(alc5623_hpl_mixer_controls)),
 278SND_SOC_DAPM_MIXER("HPOut Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
 279SND_SOC_DAPM_MIXER("Mono Mix", ALC5623_PWR_MANAG_ADD2, 2, 0,
 280        &alc5623_mono_mixer_controls[0],
 281        ARRAY_SIZE(alc5623_mono_mixer_controls)),
 282SND_SOC_DAPM_MIXER("Speaker Mix", ALC5623_PWR_MANAG_ADD2, 3, 0,
 283        &alc5623_speaker_mixer_controls[0],
 284        ARRAY_SIZE(alc5623_speaker_mixer_controls)),
 285
 286/* input mixers */
 287SND_SOC_DAPM_MIXER("Left Capture Mix", ALC5623_PWR_MANAG_ADD2, 1, 0,
 288        &alc5623_captureL_mixer_controls[0],
 289        ARRAY_SIZE(alc5623_captureL_mixer_controls)),
 290SND_SOC_DAPM_MIXER("Right Capture Mix", ALC5623_PWR_MANAG_ADD2, 0, 0,
 291        &alc5623_captureR_mixer_controls[0],
 292        ARRAY_SIZE(alc5623_captureR_mixer_controls)),
 293
 294SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
 295        ALC5623_PWR_MANAG_ADD2, 9, 0),
 296SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
 297        ALC5623_PWR_MANAG_ADD2, 8, 0),
 298SND_SOC_DAPM_MIXER("I2S Mix", ALC5623_PWR_MANAG_ADD1, 15, 0, NULL, 0),
 299SND_SOC_DAPM_MIXER("AuxI Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
 300SND_SOC_DAPM_MIXER("Line Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
 301SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture",
 302        ALC5623_PWR_MANAG_ADD2, 7, 0),
 303SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture",
 304        ALC5623_PWR_MANAG_ADD2, 6, 0),
 305SND_SOC_DAPM_PGA("Left Headphone", ALC5623_PWR_MANAG_ADD3, 10, 0, NULL, 0),
 306SND_SOC_DAPM_PGA("Right Headphone", ALC5623_PWR_MANAG_ADD3, 9, 0, NULL, 0),
 307SND_SOC_DAPM_PGA("SpeakerOut", ALC5623_PWR_MANAG_ADD3, 12, 0, NULL, 0),
 308SND_SOC_DAPM_PGA("Left AuxOut", ALC5623_PWR_MANAG_ADD3, 14, 0, NULL, 0),
 309SND_SOC_DAPM_PGA("Right AuxOut", ALC5623_PWR_MANAG_ADD3, 13, 0, NULL, 0),
 310SND_SOC_DAPM_PGA("Left LineIn", ALC5623_PWR_MANAG_ADD3, 7, 0, NULL, 0),
 311SND_SOC_DAPM_PGA("Right LineIn", ALC5623_PWR_MANAG_ADD3, 6, 0, NULL, 0),
 312SND_SOC_DAPM_PGA("Left AuxI", ALC5623_PWR_MANAG_ADD3, 5, 0, NULL, 0),
 313SND_SOC_DAPM_PGA("Right AuxI", ALC5623_PWR_MANAG_ADD3, 4, 0, NULL, 0),
 314SND_SOC_DAPM_PGA("MIC1 PGA", ALC5623_PWR_MANAG_ADD3, 3, 0, NULL, 0),
 315SND_SOC_DAPM_PGA("MIC2 PGA", ALC5623_PWR_MANAG_ADD3, 2, 0, NULL, 0),
 316SND_SOC_DAPM_PGA("MIC1 Pre Amp", ALC5623_PWR_MANAG_ADD3, 1, 0, NULL, 0),
 317SND_SOC_DAPM_PGA("MIC2 Pre Amp", ALC5623_PWR_MANAG_ADD3, 0, 0, NULL, 0),
 318SND_SOC_DAPM_MICBIAS("Mic Bias1", ALC5623_PWR_MANAG_ADD1, 11, 0),
 319
 320SND_SOC_DAPM_OUTPUT("AUXOUTL"),
 321SND_SOC_DAPM_OUTPUT("AUXOUTR"),
 322SND_SOC_DAPM_OUTPUT("HPL"),
 323SND_SOC_DAPM_OUTPUT("HPR"),
 324SND_SOC_DAPM_OUTPUT("SPKOUT"),
 325SND_SOC_DAPM_OUTPUT("SPKOUTN"),
 326SND_SOC_DAPM_INPUT("LINEINL"),
 327SND_SOC_DAPM_INPUT("LINEINR"),
 328SND_SOC_DAPM_INPUT("AUXINL"),
 329SND_SOC_DAPM_INPUT("AUXINR"),
 330SND_SOC_DAPM_INPUT("MIC1"),
 331SND_SOC_DAPM_INPUT("MIC2"),
 332SND_SOC_DAPM_VMID("Vmid"),
 333};
 334
 335static const char *alc5623_amp_names[] = {"AB Amp", "D Amp"};
 336static SOC_ENUM_SINGLE_DECL(alc5623_amp_enum,
 337                            ALC5623_OUTPUT_MIXER_CTRL, 13,
 338                            alc5623_amp_names);
 339static const struct snd_kcontrol_new alc5623_amp_mux_controls =
 340        SOC_DAPM_ENUM("Route", alc5623_amp_enum);
 341
 342static const struct snd_soc_dapm_widget alc5623_dapm_amp_widgets[] = {
 343SND_SOC_DAPM_PGA_E("D Amp", ALC5623_PWR_MANAG_ADD2, 14, 0, NULL, 0,
 344        amp_mixer_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
 345SND_SOC_DAPM_PGA("AB Amp", ALC5623_PWR_MANAG_ADD2, 15, 0, NULL, 0),
 346SND_SOC_DAPM_MUX("AB-D Amp Mux", SND_SOC_NOPM, 0, 0,
 347        &alc5623_amp_mux_controls),
 348};
 349
 350static const struct snd_soc_dapm_route intercon[] = {
 351        /* virtual mixer - mixes left & right channels */
 352        {"I2S Mix", NULL,                               "Left DAC"},
 353        {"I2S Mix", NULL,                               "Right DAC"},
 354        {"Line Mix", NULL,                              "Right LineIn"},
 355        {"Line Mix", NULL,                              "Left LineIn"},
 356        {"AuxI Mix", NULL,                              "Left AuxI"},
 357        {"AuxI Mix", NULL,                              "Right AuxI"},
 358        {"AUXOUTL", NULL,                               "Left AuxOut"},
 359        {"AUXOUTR", NULL,                               "Right AuxOut"},
 360
 361        /* HP mixer */
 362        {"HPL Mix", "ADC2HP_L Playback Switch",         "Left Capture Mix"},
 363        {"HPL Mix", NULL,                               "HP Mix"},
 364        {"HPR Mix", "ADC2HP_R Playback Switch",         "Right Capture Mix"},
 365        {"HPR Mix", NULL,                               "HP Mix"},
 366        {"HP Mix", "LI2HP Playback Switch",             "Line Mix"},
 367        {"HP Mix", "AUXI2HP Playback Switch",           "AuxI Mix"},
 368        {"HP Mix", "MIC12HP Playback Switch",           "MIC1 PGA"},
 369        {"HP Mix", "MIC22HP Playback Switch",           "MIC2 PGA"},
 370        {"HP Mix", "DAC2HP Playback Switch",            "I2S Mix"},
 371
 372        /* speaker mixer */
 373        {"Speaker Mix", "LI2SPK Playback Switch",       "Line Mix"},
 374        {"Speaker Mix", "AUXI2SPK Playback Switch",     "AuxI Mix"},
 375        {"Speaker Mix", "MIC12SPK Playback Switch",     "MIC1 PGA"},
 376        {"Speaker Mix", "MIC22SPK Playback Switch",     "MIC2 PGA"},
 377        {"Speaker Mix", "DAC2SPK Playback Switch",      "I2S Mix"},
 378
 379        /* mono mixer */
 380        {"Mono Mix", "ADC2MONO_L Playback Switch",      "Left Capture Mix"},
 381        {"Mono Mix", "ADC2MONO_R Playback Switch",      "Right Capture Mix"},
 382        {"Mono Mix", "LI2MONO Playback Switch",         "Line Mix"},
 383        {"Mono Mix", "AUXI2MONO Playback Switch",       "AuxI Mix"},
 384        {"Mono Mix", "MIC12MONO Playback Switch",       "MIC1 PGA"},
 385        {"Mono Mix", "MIC22MONO Playback Switch",       "MIC2 PGA"},
 386        {"Mono Mix", "DAC2MONO Playback Switch",        "I2S Mix"},
 387
 388        /* Left record mixer */
 389        {"Left Capture Mix", "LineInL Capture Switch",  "LINEINL"},
 390        {"Left Capture Mix", "Left AuxI Capture Switch", "AUXINL"},
 391        {"Left Capture Mix", "Mic1 Capture Switch",     "MIC1 Pre Amp"},
 392        {"Left Capture Mix", "Mic2 Capture Switch",     "MIC2 Pre Amp"},
 393        {"Left Capture Mix", "HPMixerL Capture Switch", "HPL Mix"},
 394        {"Left Capture Mix", "SPKMixer Capture Switch", "Speaker Mix"},
 395        {"Left Capture Mix", "MonoMixer Capture Switch", "Mono Mix"},
 396
 397        /*Right record mixer */
 398        {"Right Capture Mix", "LineInR Capture Switch", "LINEINR"},
 399        {"Right Capture Mix", "Right AuxI Capture Switch",      "AUXINR"},
 400        {"Right Capture Mix", "Mic1 Capture Switch",    "MIC1 Pre Amp"},
 401        {"Right Capture Mix", "Mic2 Capture Switch",    "MIC2 Pre Amp"},
 402        {"Right Capture Mix", "HPMixerR Capture Switch", "HPR Mix"},
 403        {"Right Capture Mix", "SPKMixer Capture Switch", "Speaker Mix"},
 404        {"Right Capture Mix", "MonoMixer Capture Switch", "Mono Mix"},
 405
 406        /* headphone left mux */
 407        {"Left Headphone Mux", "HP Left Mix",           "HPL Mix"},
 408        {"Left Headphone Mux", "Vmid",                  "Vmid"},
 409
 410        /* headphone right mux */
 411        {"Right Headphone Mux", "HP Right Mix",         "HPR Mix"},
 412        {"Right Headphone Mux", "Vmid",                 "Vmid"},
 413
 414        /* speaker out mux */
 415        {"SpeakerOut Mux", "Vmid",                      "Vmid"},
 416        {"SpeakerOut Mux", "HPOut Mix",                 "HPOut Mix"},
 417        {"SpeakerOut Mux", "Speaker Mix",               "Speaker Mix"},
 418        {"SpeakerOut Mux", "Mono Mix",                  "Mono Mix"},
 419
 420        /* Mono/Aux Out mux */
 421        {"AuxOut Mux", "Vmid",                          "Vmid"},
 422        {"AuxOut Mux", "HPOut Mix",                     "HPOut Mix"},
 423        {"AuxOut Mux", "Speaker Mix",                   "Speaker Mix"},
 424        {"AuxOut Mux", "Mono Mix",                      "Mono Mix"},
 425
 426        /* output pga */
 427        {"HPL", NULL,                                   "Left Headphone"},
 428        {"Left Headphone", NULL,                        "Left Headphone Mux"},
 429        {"HPR", NULL,                                   "Right Headphone"},
 430        {"Right Headphone", NULL,                       "Right Headphone Mux"},
 431        {"Left AuxOut", NULL,                           "AuxOut Mux"},
 432        {"Right AuxOut", NULL,                          "AuxOut Mux"},
 433
 434        /* input pga */
 435        {"Left LineIn", NULL,                           "LINEINL"},
 436        {"Right LineIn", NULL,                          "LINEINR"},
 437        {"Left AuxI", NULL,                             "AUXINL"},
 438        {"Right AuxI", NULL,                            "AUXINR"},
 439        {"MIC1 Pre Amp", NULL,                          "MIC1"},
 440        {"MIC2 Pre Amp", NULL,                          "MIC2"},
 441        {"MIC1 PGA", NULL,                              "MIC1 Pre Amp"},
 442        {"MIC2 PGA", NULL,                              "MIC2 Pre Amp"},
 443
 444        /* left ADC */
 445        {"Left ADC", NULL,                              "Left Capture Mix"},
 446
 447        /* right ADC */
 448        {"Right ADC", NULL,                             "Right Capture Mix"},
 449
 450        {"SpeakerOut N Mux", "RN/-R",                   "SpeakerOut"},
 451        {"SpeakerOut N Mux", "RP/+R",                   "SpeakerOut"},
 452        {"SpeakerOut N Mux", "LN/-R",                   "SpeakerOut"},
 453        {"SpeakerOut N Mux", "Vmid",                    "Vmid"},
 454
 455        {"SPKOUT", NULL,                                "SpeakerOut"},
 456        {"SPKOUTN", NULL,                               "SpeakerOut N Mux"},
 457};
 458
 459static const struct snd_soc_dapm_route intercon_spk[] = {
 460        {"SpeakerOut", NULL,                            "SpeakerOut Mux"},
 461};
 462
 463static const struct snd_soc_dapm_route intercon_amp_spk[] = {
 464        {"AB Amp", NULL,                                "SpeakerOut Mux"},
 465        {"D Amp", NULL,                                 "SpeakerOut Mux"},
 466        {"AB-D Amp Mux", "AB Amp",                      "AB Amp"},
 467        {"AB-D Amp Mux", "D Amp",                       "D Amp"},
 468        {"SpeakerOut", NULL,                            "AB-D Amp Mux"},
 469};
 470
 471/* PLL divisors */
 472struct _pll_div {
 473        u32 pll_in;
 474        u32 pll_out;
 475        u16 regvalue;
 476};
 477
 478/* Note : pll code from original alc5623 driver. Not sure of how good it is */
 479/* useful only for master mode */
 480static const struct _pll_div codec_master_pll_div[] = {
 481
 482        {  2048000,  8192000,   0x0ea0},
 483        {  3686400,  8192000,   0x4e27},
 484        { 12000000,  8192000,   0x456b},
 485        { 13000000,  8192000,   0x495f},
 486        { 13100000,  8192000,   0x0320},
 487        {  2048000,  11289600,  0xf637},
 488        {  3686400,  11289600,  0x2f22},
 489        { 12000000,  11289600,  0x3e2f},
 490        { 13000000,  11289600,  0x4d5b},
 491        { 13100000,  11289600,  0x363b},
 492        {  2048000,  16384000,  0x1ea0},
 493        {  3686400,  16384000,  0x9e27},
 494        { 12000000,  16384000,  0x452b},
 495        { 13000000,  16384000,  0x542f},
 496        { 13100000,  16384000,  0x03a0},
 497        {  2048000,  16934400,  0xe625},
 498        {  3686400,  16934400,  0x9126},
 499        { 12000000,  16934400,  0x4d2c},
 500        { 13000000,  16934400,  0x742f},
 501        { 13100000,  16934400,  0x3c27},
 502        {  2048000,  22579200,  0x2aa0},
 503        {  3686400,  22579200,  0x2f20},
 504        { 12000000,  22579200,  0x7e2f},
 505        { 13000000,  22579200,  0x742f},
 506        { 13100000,  22579200,  0x3c27},
 507        {  2048000,  24576000,  0x2ea0},
 508        {  3686400,  24576000,  0xee27},
 509        { 12000000,  24576000,  0x2915},
 510        { 13000000,  24576000,  0x772e},
 511        { 13100000,  24576000,  0x0d20},
 512};
 513
 514static const struct _pll_div codec_slave_pll_div[] = {
 515
 516        {  1024000,  16384000,  0x3ea0},
 517        {  1411200,  22579200,  0x3ea0},
 518        {  1536000,  24576000,  0x3ea0},
 519        {  2048000,  16384000,  0x1ea0},
 520        {  2822400,  22579200,  0x1ea0},
 521        {  3072000,  24576000,  0x1ea0},
 522
 523};
 524
 525static int alc5623_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
 526                int source, unsigned int freq_in, unsigned int freq_out)
 527{
 528        int i;
 529        struct snd_soc_codec *codec = codec_dai->codec;
 530        int gbl_clk = 0, pll_div = 0;
 531        u16 reg;
 532
 533        if (pll_id < ALC5623_PLL_FR_MCLK || pll_id > ALC5623_PLL_FR_BCK)
 534                return -ENODEV;
 535
 536        /* Disable PLL power */
 537        snd_soc_update_bits(codec, ALC5623_PWR_MANAG_ADD2,
 538                                ALC5623_PWR_ADD2_PLL,
 539                                0);
 540
 541        /* pll is not used in slave mode */
 542        reg = snd_soc_read(codec, ALC5623_DAI_CONTROL);
 543        if (reg & ALC5623_DAI_SDP_SLAVE_MODE)
 544                return 0;
 545
 546        if (!freq_in || !freq_out)
 547                return 0;
 548
 549        switch (pll_id) {
 550        case ALC5623_PLL_FR_MCLK:
 551                for (i = 0; i < ARRAY_SIZE(codec_master_pll_div); i++) {
 552                        if (codec_master_pll_div[i].pll_in == freq_in
 553                           && codec_master_pll_div[i].pll_out == freq_out) {
 554                                /* PLL source from MCLK */
 555                                pll_div  = codec_master_pll_div[i].regvalue;
 556                                break;
 557                        }
 558                }
 559                break;
 560        case ALC5623_PLL_FR_BCK:
 561                for (i = 0; i < ARRAY_SIZE(codec_slave_pll_div); i++) {
 562                        if (codec_slave_pll_div[i].pll_in == freq_in
 563                           && codec_slave_pll_div[i].pll_out == freq_out) {
 564                                /* PLL source from Bitclk */
 565                                gbl_clk = ALC5623_GBL_CLK_PLL_SOUR_SEL_BITCLK;
 566                                pll_div = codec_slave_pll_div[i].regvalue;
 567                                break;
 568                        }
 569                }
 570                break;
 571        default:
 572                return -EINVAL;
 573        }
 574
 575        if (!pll_div)
 576                return -EINVAL;
 577
 578        snd_soc_write(codec, ALC5623_GLOBAL_CLK_CTRL_REG, gbl_clk);
 579        snd_soc_write(codec, ALC5623_PLL_CTRL, pll_div);
 580        snd_soc_update_bits(codec, ALC5623_PWR_MANAG_ADD2,
 581                                ALC5623_PWR_ADD2_PLL,
 582                                ALC5623_PWR_ADD2_PLL);
 583        gbl_clk |= ALC5623_GBL_CLK_SYS_SOUR_SEL_PLL;
 584        snd_soc_write(codec, ALC5623_GLOBAL_CLK_CTRL_REG, gbl_clk);
 585
 586        return 0;
 587}
 588
 589struct _coeff_div {
 590        u16 fs;
 591        u16 regvalue;
 592};
 593
 594/* codec hifi mclk (after PLL) clock divider coefficients */
 595/* values inspired from column BCLK=32Fs of Appendix A table */
 596static const struct _coeff_div coeff_div[] = {
 597        {256*8, 0x3a69},
 598        {384*8, 0x3c6b},
 599        {256*4, 0x2a69},
 600        {384*4, 0x2c6b},
 601        {256*2, 0x1a69},
 602        {384*2, 0x1c6b},
 603        {256*1, 0x0a69},
 604        {384*1, 0x0c6b},
 605};
 606
 607static int get_coeff(struct snd_soc_codec *codec, int rate)
 608{
 609        struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
 610        int i;
 611
 612        for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
 613                if (coeff_div[i].fs * rate == alc5623->sysclk)
 614                        return i;
 615        }
 616        return -EINVAL;
 617}
 618
 619/*
 620 * Clock after PLL and dividers
 621 */
 622static int alc5623_set_dai_sysclk(struct snd_soc_dai *codec_dai,
 623                int clk_id, unsigned int freq, int dir)
 624{
 625        struct snd_soc_codec *codec = codec_dai->codec;
 626        struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
 627
 628        switch (freq) {
 629        case  8192000:
 630        case 11289600:
 631        case 12288000:
 632        case 16384000:
 633        case 16934400:
 634        case 18432000:
 635        case 22579200:
 636        case 24576000:
 637                alc5623->sysclk = freq;
 638                return 0;
 639        }
 640        return -EINVAL;
 641}
 642
 643static int alc5623_set_dai_fmt(struct snd_soc_dai *codec_dai,
 644                unsigned int fmt)
 645{
 646        struct snd_soc_codec *codec = codec_dai->codec;
 647        u16 iface = 0;
 648
 649        /* set master/slave audio interface */
 650        switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
 651        case SND_SOC_DAIFMT_CBM_CFM:
 652                iface = ALC5623_DAI_SDP_MASTER_MODE;
 653                break;
 654        case SND_SOC_DAIFMT_CBS_CFS:
 655                iface = ALC5623_DAI_SDP_SLAVE_MODE;
 656                break;
 657        default:
 658                return -EINVAL;
 659        }
 660
 661        /* interface format */
 662        switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
 663        case SND_SOC_DAIFMT_I2S:
 664                iface |= ALC5623_DAI_I2S_DF_I2S;
 665                break;
 666        case SND_SOC_DAIFMT_RIGHT_J:
 667                iface |= ALC5623_DAI_I2S_DF_RIGHT;
 668                break;
 669        case SND_SOC_DAIFMT_LEFT_J:
 670                iface |= ALC5623_DAI_I2S_DF_LEFT;
 671                break;
 672        case SND_SOC_DAIFMT_DSP_A:
 673                iface |= ALC5623_DAI_I2S_DF_PCM;
 674                break;
 675        case SND_SOC_DAIFMT_DSP_B:
 676                iface |= ALC5623_DAI_I2S_DF_PCM | ALC5623_DAI_I2S_PCM_MODE;
 677                break;
 678        default:
 679                return -EINVAL;
 680        }
 681
 682        /* clock inversion */
 683        switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
 684        case SND_SOC_DAIFMT_NB_NF:
 685                break;
 686        case SND_SOC_DAIFMT_IB_IF:
 687                iface |= ALC5623_DAI_MAIN_I2S_BCLK_POL_CTRL;
 688                break;
 689        case SND_SOC_DAIFMT_IB_NF:
 690                iface |= ALC5623_DAI_MAIN_I2S_BCLK_POL_CTRL;
 691                break;
 692        case SND_SOC_DAIFMT_NB_IF:
 693                break;
 694        default:
 695                return -EINVAL;
 696        }
 697
 698        return snd_soc_write(codec, ALC5623_DAI_CONTROL, iface);
 699}
 700
 701static int alc5623_pcm_hw_params(struct snd_pcm_substream *substream,
 702                struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
 703{
 704        struct snd_soc_codec *codec = dai->codec;
 705        struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
 706        int coeff, rate;
 707        u16 iface;
 708
 709        iface = snd_soc_read(codec, ALC5623_DAI_CONTROL);
 710        iface &= ~ALC5623_DAI_I2S_DL_MASK;
 711
 712        /* bit size */
 713        switch (params_width(params)) {
 714        case 16:
 715                iface |= ALC5623_DAI_I2S_DL_16;
 716                break;
 717        case 20:
 718                iface |= ALC5623_DAI_I2S_DL_20;
 719                break;
 720        case 24:
 721                iface |= ALC5623_DAI_I2S_DL_24;
 722                break;
 723        case 32:
 724                iface |= ALC5623_DAI_I2S_DL_32;
 725                break;
 726        default:
 727                return -EINVAL;
 728        }
 729
 730        /* set iface & srate */
 731        snd_soc_write(codec, ALC5623_DAI_CONTROL, iface);
 732        rate = params_rate(params);
 733        coeff = get_coeff(codec, rate);
 734        if (coeff < 0)
 735                return -EINVAL;
 736
 737        coeff = coeff_div[coeff].regvalue;
 738        dev_dbg(codec->dev, "%s: sysclk=%d,rate=%d,coeff=0x%04x\n",
 739                __func__, alc5623->sysclk, rate, coeff);
 740        snd_soc_write(codec, ALC5623_STEREO_AD_DA_CLK_CTRL, coeff);
 741
 742        return 0;
 743}
 744
 745static int alc5623_mute(struct snd_soc_dai *dai, int mute)
 746{
 747        struct snd_soc_codec *codec = dai->codec;
 748        u16 hp_mute = ALC5623_MISC_M_DAC_L_INPUT | ALC5623_MISC_M_DAC_R_INPUT;
 749        u16 mute_reg = snd_soc_read(codec, ALC5623_MISC_CTRL) & ~hp_mute;
 750
 751        if (mute)
 752                mute_reg |= hp_mute;
 753
 754        return snd_soc_write(codec, ALC5623_MISC_CTRL, mute_reg);
 755}
 756
 757#define ALC5623_ADD2_POWER_EN (ALC5623_PWR_ADD2_VREF \
 758        | ALC5623_PWR_ADD2_DAC_REF_CIR)
 759
 760#define ALC5623_ADD3_POWER_EN (ALC5623_PWR_ADD3_MAIN_BIAS \
 761        | ALC5623_PWR_ADD3_MIC1_BOOST_AD)
 762
 763#define ALC5623_ADD1_POWER_EN \
 764        (ALC5623_PWR_ADD1_SHORT_CURR_DET_EN | ALC5623_PWR_ADD1_SOFTGEN_EN \
 765        | ALC5623_PWR_ADD1_DEPOP_BUF_HP | ALC5623_PWR_ADD1_HP_OUT_AMP \
 766        | ALC5623_PWR_ADD1_HP_OUT_ENH_AMP)
 767
 768#define ALC5623_ADD1_POWER_EN_5622 \
 769        (ALC5623_PWR_ADD1_SHORT_CURR_DET_EN \
 770        | ALC5623_PWR_ADD1_HP_OUT_AMP)
 771
 772static void enable_power_depop(struct snd_soc_codec *codec)
 773{
 774        struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
 775
 776        snd_soc_update_bits(codec, ALC5623_PWR_MANAG_ADD1,
 777                                ALC5623_PWR_ADD1_SOFTGEN_EN,
 778                                ALC5623_PWR_ADD1_SOFTGEN_EN);
 779
 780        snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3, ALC5623_ADD3_POWER_EN);
 781
 782        snd_soc_update_bits(codec, ALC5623_MISC_CTRL,
 783                                ALC5623_MISC_HP_DEPOP_MODE2_EN,
 784                                ALC5623_MISC_HP_DEPOP_MODE2_EN);
 785
 786        msleep(500);
 787
 788        snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2, ALC5623_ADD2_POWER_EN);
 789
 790        /* avoid writing '1' into 5622 reserved bits */
 791        if (alc5623->id == 0x22)
 792                snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1,
 793                        ALC5623_ADD1_POWER_EN_5622);
 794        else
 795                snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1,
 796                        ALC5623_ADD1_POWER_EN);
 797
 798        /* disable HP Depop2 */
 799        snd_soc_update_bits(codec, ALC5623_MISC_CTRL,
 800                                ALC5623_MISC_HP_DEPOP_MODE2_EN,
 801                                0);
 802
 803}
 804
 805static int alc5623_set_bias_level(struct snd_soc_codec *codec,
 806                                      enum snd_soc_bias_level level)
 807{
 808        switch (level) {
 809        case SND_SOC_BIAS_ON:
 810                enable_power_depop(codec);
 811                break;
 812        case SND_SOC_BIAS_PREPARE:
 813                break;
 814        case SND_SOC_BIAS_STANDBY:
 815                /* everything off except vref/vmid, */
 816                snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2,
 817                                ALC5623_PWR_ADD2_VREF);
 818                snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3,
 819                                ALC5623_PWR_ADD3_MAIN_BIAS);
 820                break;
 821        case SND_SOC_BIAS_OFF:
 822                /* everything off, dac mute, inactive */
 823                snd_soc_write(codec, ALC5623_PWR_MANAG_ADD2, 0);
 824                snd_soc_write(codec, ALC5623_PWR_MANAG_ADD3, 0);
 825                snd_soc_write(codec, ALC5623_PWR_MANAG_ADD1, 0);
 826                break;
 827        }
 828        return 0;
 829}
 830
 831#define ALC5623_FORMATS (SNDRV_PCM_FMTBIT_S16_LE \
 832                        | SNDRV_PCM_FMTBIT_S24_LE \
 833                        | SNDRV_PCM_FMTBIT_S32_LE)
 834
 835static const struct snd_soc_dai_ops alc5623_dai_ops = {
 836                .hw_params = alc5623_pcm_hw_params,
 837                .digital_mute = alc5623_mute,
 838                .set_fmt = alc5623_set_dai_fmt,
 839                .set_sysclk = alc5623_set_dai_sysclk,
 840                .set_pll = alc5623_set_dai_pll,
 841};
 842
 843static struct snd_soc_dai_driver alc5623_dai = {
 844        .name = "alc5623-hifi",
 845        .playback = {
 846                .stream_name = "Playback",
 847                .channels_min = 1,
 848                .channels_max = 2,
 849                .rate_min =     8000,
 850                .rate_max =     48000,
 851                .rates = SNDRV_PCM_RATE_8000_48000,
 852                .formats = ALC5623_FORMATS,},
 853        .capture = {
 854                .stream_name = "Capture",
 855                .channels_min = 1,
 856                .channels_max = 2,
 857                .rate_min =     8000,
 858                .rate_max =     48000,
 859                .rates = SNDRV_PCM_RATE_8000_48000,
 860                .formats = ALC5623_FORMATS,},
 861
 862        .ops = &alc5623_dai_ops,
 863};
 864
 865static int alc5623_suspend(struct snd_soc_codec *codec)
 866{
 867        struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
 868
 869        regcache_cache_only(alc5623->regmap, true);
 870
 871        return 0;
 872}
 873
 874static int alc5623_resume(struct snd_soc_codec *codec)
 875{
 876        struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
 877        int ret;
 878
 879        /* Sync reg_cache with the hardware */
 880        regcache_cache_only(alc5623->regmap, false);
 881        ret = regcache_sync(alc5623->regmap);
 882        if (ret != 0) {
 883                dev_err(codec->dev, "Failed to sync register cache: %d\n",
 884                        ret);
 885                regcache_cache_only(alc5623->regmap, true);
 886                return ret;
 887        }
 888
 889        return 0;
 890}
 891
 892static int alc5623_probe(struct snd_soc_codec *codec)
 893{
 894        struct alc5623_priv *alc5623 = snd_soc_codec_get_drvdata(codec);
 895        struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
 896
 897        alc5623_reset(codec);
 898
 899        if (alc5623->add_ctrl) {
 900                snd_soc_write(codec, ALC5623_ADD_CTRL_REG,
 901                                alc5623->add_ctrl);
 902        }
 903
 904        if (alc5623->jack_det_ctrl) {
 905                snd_soc_write(codec, ALC5623_JACK_DET_CTRL,
 906                                alc5623->jack_det_ctrl);
 907        }
 908
 909        switch (alc5623->id) {
 910        case 0x21:
 911                snd_soc_add_codec_controls(codec, alc5621_vol_snd_controls,
 912                        ARRAY_SIZE(alc5621_vol_snd_controls));
 913                break;
 914        case 0x22:
 915                snd_soc_add_codec_controls(codec, alc5622_vol_snd_controls,
 916                        ARRAY_SIZE(alc5622_vol_snd_controls));
 917                break;
 918        case 0x23:
 919                snd_soc_add_codec_controls(codec, alc5623_vol_snd_controls,
 920                        ARRAY_SIZE(alc5623_vol_snd_controls));
 921                break;
 922        default:
 923                return -EINVAL;
 924        }
 925
 926        snd_soc_add_codec_controls(codec, alc5623_snd_controls,
 927                        ARRAY_SIZE(alc5623_snd_controls));
 928
 929        snd_soc_dapm_new_controls(dapm, alc5623_dapm_widgets,
 930                                        ARRAY_SIZE(alc5623_dapm_widgets));
 931
 932        /* set up audio path interconnects */
 933        snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
 934
 935        switch (alc5623->id) {
 936        case 0x21:
 937        case 0x22:
 938                snd_soc_dapm_new_controls(dapm, alc5623_dapm_amp_widgets,
 939                                        ARRAY_SIZE(alc5623_dapm_amp_widgets));
 940                snd_soc_dapm_add_routes(dapm, intercon_amp_spk,
 941                                        ARRAY_SIZE(intercon_amp_spk));
 942                break;
 943        case 0x23:
 944                snd_soc_dapm_add_routes(dapm, intercon_spk,
 945                                        ARRAY_SIZE(intercon_spk));
 946                break;
 947        default:
 948                return -EINVAL;
 949        }
 950
 951        return 0;
 952}
 953
 954static struct snd_soc_codec_driver soc_codec_device_alc5623 = {
 955        .probe = alc5623_probe,
 956        .suspend = alc5623_suspend,
 957        .resume = alc5623_resume,
 958        .set_bias_level = alc5623_set_bias_level,
 959        .suspend_bias_off = true,
 960};
 961
 962static const struct regmap_config alc5623_regmap = {
 963        .reg_bits = 8,
 964        .val_bits = 16,
 965        .reg_stride = 2,
 966
 967        .max_register = ALC5623_VENDOR_ID2,
 968        .cache_type = REGCACHE_RBTREE,
 969};
 970
 971/*
 972 * ALC5623 2 wire address is determined by A1 pin
 973 * state during powerup.
 974 *    low  = 0x1a
 975 *    high = 0x1b
 976 */
 977static int alc5623_i2c_probe(struct i2c_client *client,
 978                             const struct i2c_device_id *id)
 979{
 980        struct alc5623_platform_data *pdata;
 981        struct alc5623_priv *alc5623;
 982        struct device_node *np;
 983        unsigned int vid1, vid2;
 984        int ret;
 985        u32 val32;
 986
 987        alc5623 = devm_kzalloc(&client->dev, sizeof(struct alc5623_priv),
 988                               GFP_KERNEL);
 989        if (alc5623 == NULL)
 990                return -ENOMEM;
 991
 992        alc5623->regmap = devm_regmap_init_i2c(client, &alc5623_regmap);
 993        if (IS_ERR(alc5623->regmap)) {
 994                ret = PTR_ERR(alc5623->regmap);
 995                dev_err(&client->dev, "Failed to initialise I/O: %d\n", ret);
 996                return ret;
 997        }
 998
 999        ret = regmap_read(alc5623->regmap, ALC5623_VENDOR_ID1, &vid1);
1000        if (ret < 0) {
1001                dev_err(&client->dev, "failed to read vendor ID1: %d\n", ret);
1002                return ret;
1003        }
1004
1005        ret = regmap_read(alc5623->regmap, ALC5623_VENDOR_ID2, &vid2);
1006        if (ret < 0) {
1007                dev_err(&client->dev, "failed to read vendor ID2: %d\n", ret);
1008                return ret;
1009        }
1010        vid2 >>= 8;
1011
1012        if ((vid1 != 0x10ec) || (vid2 != id->driver_data)) {
1013                dev_err(&client->dev, "unknown or wrong codec\n");
1014                dev_err(&client->dev, "Expected %x:%lx, got %x:%x\n",
1015                                0x10ec, id->driver_data,
1016                                vid1, vid2);
1017                return -ENODEV;
1018        }
1019
1020        dev_dbg(&client->dev, "Found codec id : alc56%02x\n", vid2);
1021
1022        pdata = client->dev.platform_data;
1023        if (pdata) {
1024                alc5623->add_ctrl = pdata->add_ctrl;
1025                alc5623->jack_det_ctrl = pdata->jack_det_ctrl;
1026        } else {
1027                if (client->dev.of_node) {
1028                        np = client->dev.of_node;
1029                        ret = of_property_read_u32(np, "add-ctrl", &val32);
1030                        if (!ret)
1031                                alc5623->add_ctrl = val32;
1032                        ret = of_property_read_u32(np, "jack-det-ctrl", &val32);
1033                        if (!ret)
1034                                alc5623->jack_det_ctrl = val32;
1035                }
1036        }
1037
1038        alc5623->id = vid2;
1039        switch (alc5623->id) {
1040        case 0x21:
1041                alc5623_dai.name = "alc5621-hifi";
1042                break;
1043        case 0x22:
1044                alc5623_dai.name = "alc5622-hifi";
1045                break;
1046        case 0x23:
1047                alc5623_dai.name = "alc5623-hifi";
1048                break;
1049        default:
1050                return -EINVAL;
1051        }
1052
1053        i2c_set_clientdata(client, alc5623);
1054
1055        ret =  snd_soc_register_codec(&client->dev,
1056                &soc_codec_device_alc5623, &alc5623_dai, 1);
1057        if (ret != 0)
1058                dev_err(&client->dev, "Failed to register codec: %d\n", ret);
1059
1060        return ret;
1061}
1062
1063static int alc5623_i2c_remove(struct i2c_client *client)
1064{
1065        snd_soc_unregister_codec(&client->dev);
1066        return 0;
1067}
1068
1069static const struct i2c_device_id alc5623_i2c_table[] = {
1070        {"alc5621", 0x21},
1071        {"alc5622", 0x22},
1072        {"alc5623", 0x23},
1073        {}
1074};
1075MODULE_DEVICE_TABLE(i2c, alc5623_i2c_table);
1076
1077static const struct of_device_id alc5623_of_match[] = {
1078        { .compatible = "realtek,alc5623", },
1079        { }
1080};
1081MODULE_DEVICE_TABLE(of, alc5623_of_match);
1082
1083/*  i2c codec control layer */
1084static struct i2c_driver alc5623_i2c_driver = {
1085        .driver = {
1086                .name = "alc562x-codec",
1087                .of_match_table = of_match_ptr(alc5623_of_match),
1088        },
1089        .probe = alc5623_i2c_probe,
1090        .remove =  alc5623_i2c_remove,
1091        .id_table = alc5623_i2c_table,
1092};
1093
1094module_i2c_driver(alc5623_i2c_driver);
1095
1096MODULE_DESCRIPTION("ASoC alc5621/2/3 driver");
1097MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
1098MODULE_LICENSE("GPL");
1099