linux/sound/soc/codecs/wm9712.c
<<
>>
Prefs
   1/*
   2 * wm9712.c  --  ALSA Soc WM9712 codec support
   3 *
   4 * Copyright 2006-12 Wolfson Microelectronics PLC.
   5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
   6 *
   7 *  This program is free software; you can redistribute  it and/or modify it
   8 *  under  the terms of  the GNU General  Public License as published by the
   9 *  Free Software Foundation;  either version 2 of the  License, or (at your
  10 *  option) any later version.
  11 */
  12
  13#include <linux/init.h>
  14#include <linux/slab.h>
  15#include <linux/module.h>
  16#include <linux/kernel.h>
  17#include <linux/device.h>
  18#include <sound/core.h>
  19#include <sound/pcm.h>
  20#include <sound/ac97_codec.h>
  21#include <sound/initval.h>
  22#include <sound/soc.h>
  23#include <sound/tlv.h>
  24#include "wm9712.h"
  25
  26#define WM9712_VENDOR_ID 0x574d4c12
  27#define WM9712_VENDOR_ID_MASK 0xffffffff
  28
  29struct wm9712_priv {
  30        struct snd_ac97 *ac97;
  31        unsigned int hp_mixer[2];
  32        struct mutex lock;
  33};
  34
  35static unsigned int ac97_read(struct snd_soc_codec *codec,
  36        unsigned int reg);
  37static int ac97_write(struct snd_soc_codec *codec,
  38        unsigned int reg, unsigned int val);
  39
  40/*
  41 * WM9712 register cache
  42 */
  43static const u16 wm9712_reg[] = {
  44        0x6174, 0x8000, 0x8000, 0x8000, /*  6 */
  45        0x0f0f, 0xaaa0, 0xc008, 0x6808, /*  e */
  46        0xe808, 0xaaa0, 0xad00, 0x8000, /* 16 */
  47        0xe808, 0x3000, 0x8000, 0x0000, /* 1e */
  48        0x0000, 0x0000, 0x0000, 0x000f, /* 26 */
  49        0x0405, 0x0410, 0xbb80, 0xbb80, /* 2e */
  50        0x0000, 0xbb80, 0x0000, 0x0000, /* 36 */
  51        0x0000, 0x2000, 0x0000, 0x0000, /* 3e */
  52        0x0000, 0x0000, 0x0000, 0x0000, /* 46 */
  53        0x0000, 0x0000, 0xf83e, 0xffff, /* 4e */
  54        0x0000, 0x0000, 0x0000, 0xf83e, /* 56 */
  55        0x0008, 0x0000, 0x0000, 0x0000, /* 5e */
  56        0xb032, 0x3e00, 0x0000, 0x0000, /* 66 */
  57        0x0000, 0x0000, 0x0000, 0x0000, /* 6e */
  58        0x0000, 0x0000, 0x0000, 0x0006, /* 76 */
  59        0x0001, 0x0000, 0x574d, 0x4c12, /* 7e */
  60};
  61
  62#define HPL_MIXER       0x0
  63#define HPR_MIXER       0x1
  64
  65static const char *wm9712_alc_select[] = {"None", "Left", "Right", "Stereo"};
  66static const char *wm9712_alc_mux[] = {"Stereo", "Left", "Right", "None"};
  67static const char *wm9712_out3_src[] = {"Left", "VREF", "Left + Right",
  68        "Mono"};
  69static const char *wm9712_spk_src[] = {"Speaker Mix", "Headphone Mix"};
  70static const char *wm9712_rec_adc[] = {"Stereo", "Left", "Right", "Mute"};
  71static const char *wm9712_base[] = {"Linear Control", "Adaptive Boost"};
  72static const char *wm9712_rec_gain[] = {"+1.5dB Steps", "+0.75dB Steps"};
  73static const char *wm9712_mic[] = {"Mic 1", "Differential", "Mic 2",
  74        "Stereo"};
  75static const char *wm9712_rec_sel[] = {"Mic", "NC", "NC", "Speaker Mixer",
  76        "Line", "Headphone Mixer", "Phone Mixer", "Phone"};
  77static const char *wm9712_ng_type[] = {"Constant Gain", "Mute"};
  78static const char *wm9712_diff_sel[] = {"Mic", "Line"};
  79
  80static const DECLARE_TLV_DB_SCALE(main_tlv, -3450, 150, 0);
  81static const DECLARE_TLV_DB_SCALE(boost_tlv, 0, 2000, 0);
  82
  83static const struct soc_enum wm9712_enum[] = {
  84SOC_ENUM_SINGLE(AC97_PCI_SVID, 14, 4, wm9712_alc_select),
  85SOC_ENUM_SINGLE(AC97_VIDEO, 12, 4, wm9712_alc_mux),
  86SOC_ENUM_SINGLE(AC97_AUX, 9, 4, wm9712_out3_src),
  87SOC_ENUM_SINGLE(AC97_AUX, 8, 2, wm9712_spk_src),
  88SOC_ENUM_SINGLE(AC97_REC_SEL, 12, 4, wm9712_rec_adc),
  89SOC_ENUM_SINGLE(AC97_MASTER_TONE, 15, 2, wm9712_base),
  90SOC_ENUM_DOUBLE(AC97_REC_GAIN, 14, 6, 2, wm9712_rec_gain),
  91SOC_ENUM_SINGLE(AC97_MIC, 5, 4, wm9712_mic),
  92SOC_ENUM_SINGLE(AC97_REC_SEL, 8, 8, wm9712_rec_sel),
  93SOC_ENUM_SINGLE(AC97_REC_SEL, 0, 8, wm9712_rec_sel),
  94SOC_ENUM_SINGLE(AC97_PCI_SVID, 5, 2, wm9712_ng_type),
  95SOC_ENUM_SINGLE(0x5c, 8, 2, wm9712_diff_sel),
  96};
  97
  98static const struct snd_kcontrol_new wm9712_snd_ac97_controls[] = {
  99SOC_DOUBLE("Speaker Playback Volume", AC97_MASTER, 8, 0, 31, 1),
 100SOC_SINGLE("Speaker Playback Switch", AC97_MASTER, 15, 1, 1),
 101SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
 102SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1),
 103SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1),
 104
 105SOC_SINGLE("Speaker Playback ZC Switch", AC97_MASTER, 7, 1, 0),
 106SOC_SINGLE("Speaker Playback Invert Switch", AC97_MASTER, 6, 1, 0),
 107SOC_SINGLE("Headphone Playback ZC Switch", AC97_HEADPHONE, 7, 1, 0),
 108SOC_SINGLE("Mono Playback ZC Switch", AC97_MASTER_MONO, 7, 1, 0),
 109SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1),
 110SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
 111
 112SOC_SINGLE("ALC Target Volume", AC97_CODEC_CLASS_REV, 12, 15, 0),
 113SOC_SINGLE("ALC Hold Time", AC97_CODEC_CLASS_REV, 8, 15, 0),
 114SOC_SINGLE("ALC Decay Time", AC97_CODEC_CLASS_REV, 4, 15, 0),
 115SOC_SINGLE("ALC Attack Time", AC97_CODEC_CLASS_REV, 0, 15, 0),
 116SOC_ENUM("ALC Function", wm9712_enum[0]),
 117SOC_SINGLE("ALC Max Volume", AC97_PCI_SVID, 11, 7, 0),
 118SOC_SINGLE("ALC ZC Timeout", AC97_PCI_SVID, 9, 3, 1),
 119SOC_SINGLE("ALC ZC Switch", AC97_PCI_SVID, 8, 1, 0),
 120SOC_SINGLE("ALC NG Switch", AC97_PCI_SVID, 7, 1, 0),
 121SOC_ENUM("ALC NG Type", wm9712_enum[10]),
 122SOC_SINGLE("ALC NG Threshold", AC97_PCI_SVID, 0, 31, 1),
 123
 124SOC_SINGLE("Mic Headphone  Volume", AC97_VIDEO, 12, 7, 1),
 125SOC_SINGLE("ALC Headphone Volume", AC97_VIDEO, 7, 7, 1),
 126
 127SOC_SINGLE("Out3 Switch", AC97_AUX, 15, 1, 1),
 128SOC_SINGLE("Out3 ZC Switch", AC97_AUX, 7, 1, 1),
 129SOC_SINGLE("Out3 Volume", AC97_AUX, 0, 31, 1),
 130
 131SOC_SINGLE("PCBeep Bypass Headphone Volume", AC97_PC_BEEP, 12, 7, 1),
 132SOC_SINGLE("PCBeep Bypass Speaker Volume", AC97_PC_BEEP, 8, 7, 1),
 133SOC_SINGLE("PCBeep Bypass Phone Volume", AC97_PC_BEEP, 4, 7, 1),
 134
 135SOC_SINGLE("Aux Playback Headphone Volume", AC97_CD, 12, 7, 1),
 136SOC_SINGLE("Aux Playback Speaker Volume", AC97_CD, 8, 7, 1),
 137SOC_SINGLE("Aux Playback Phone Volume", AC97_CD, 4, 7, 1),
 138
 139SOC_SINGLE("Phone Volume", AC97_PHONE, 0, 15, 1),
 140SOC_DOUBLE("Line Capture Volume", AC97_LINE, 8, 0, 31, 1),
 141
 142SOC_SINGLE_TLV("Capture Boost Switch", AC97_REC_SEL, 14, 1, 0, boost_tlv),
 143SOC_SINGLE_TLV("Capture to Phone Boost Switch", AC97_REC_SEL, 11, 1, 1,
 144               boost_tlv),
 145
 146SOC_SINGLE("3D Upper Cut-off Switch", AC97_3D_CONTROL, 5, 1, 1),
 147SOC_SINGLE("3D Lower Cut-off Switch", AC97_3D_CONTROL, 4, 1, 1),
 148SOC_SINGLE("3D Playback Volume", AC97_3D_CONTROL, 0, 15, 0),
 149
 150SOC_ENUM("Bass Control", wm9712_enum[5]),
 151SOC_SINGLE("Bass Cut-off Switch", AC97_MASTER_TONE, 12, 1, 1),
 152SOC_SINGLE("Tone Cut-off Switch", AC97_MASTER_TONE, 4, 1, 1),
 153SOC_SINGLE("Playback Attenuate (-6dB) Switch", AC97_MASTER_TONE, 6, 1, 0),
 154SOC_SINGLE("Bass Volume", AC97_MASTER_TONE, 8, 15, 1),
 155SOC_SINGLE("Treble Volume", AC97_MASTER_TONE, 0, 15, 1),
 156
 157SOC_SINGLE("Capture Switch", AC97_REC_GAIN, 15, 1, 1),
 158SOC_ENUM("Capture Volume Steps", wm9712_enum[6]),
 159SOC_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 63, 0),
 160SOC_SINGLE("Capture ZC Switch", AC97_REC_GAIN, 7, 1, 0),
 161
 162SOC_SINGLE_TLV("Mic 1 Volume", AC97_MIC, 8, 31, 1, main_tlv),
 163SOC_SINGLE_TLV("Mic 2 Volume", AC97_MIC, 0, 31, 1, main_tlv),
 164SOC_SINGLE_TLV("Mic Boost Volume", AC97_MIC, 7, 1, 0, boost_tlv),
 165};
 166
 167static const unsigned int wm9712_mixer_mute_regs[] = {
 168        AC97_VIDEO,
 169        AC97_PCM,
 170        AC97_LINE,
 171        AC97_PHONE,
 172        AC97_CD,
 173        AC97_PC_BEEP,
 174};
 175
 176/* We have to create a fake left and right HP mixers because
 177 * the codec only has a single control that is shared by both channels.
 178 * This makes it impossible to determine the audio path.
 179 */
 180static int wm9712_hp_mixer_put(struct snd_kcontrol *kcontrol,
 181        struct snd_ctl_elem_value *ucontrol)
 182{
 183        struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
 184        struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
 185        struct wm9712_priv *wm9712 = snd_soc_codec_get_drvdata(codec);
 186        unsigned int val = ucontrol->value.integer.value[0];
 187        struct soc_mixer_control *mc =
 188                (struct soc_mixer_control *)kcontrol->private_value;
 189        unsigned int mixer, mask, shift, old;
 190        struct snd_soc_dapm_update update;
 191        bool change;
 192
 193        mixer = mc->shift >> 8;
 194        shift = mc->shift & 0xff;
 195        mask = 1 << shift;
 196
 197        mutex_lock(&wm9712->lock);
 198        old = wm9712->hp_mixer[mixer];
 199        if (ucontrol->value.integer.value[0])
 200                wm9712->hp_mixer[mixer] |= mask;
 201        else
 202                wm9712->hp_mixer[mixer] &= ~mask;
 203
 204        change = old != wm9712->hp_mixer[mixer];
 205        if (change) {
 206                update.kcontrol = kcontrol;
 207                update.reg = wm9712_mixer_mute_regs[shift];
 208                update.mask = 0x8000;
 209                if ((wm9712->hp_mixer[0] & mask) ||
 210                    (wm9712->hp_mixer[1] & mask))
 211                        update.val = 0x0;
 212                else
 213                        update.val = 0x8000;
 214
 215                snd_soc_dapm_mixer_update_power(dapm, kcontrol, val,
 216                        &update);
 217        }
 218
 219        mutex_unlock(&wm9712->lock);
 220
 221        return change;
 222}
 223
 224static int wm9712_hp_mixer_get(struct snd_kcontrol *kcontrol,
 225        struct snd_ctl_elem_value *ucontrol)
 226{
 227        struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
 228        struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
 229        struct wm9712_priv *wm9712 = snd_soc_codec_get_drvdata(codec);
 230        struct soc_mixer_control *mc =
 231                (struct soc_mixer_control *)kcontrol->private_value;
 232        unsigned int shift, mixer;
 233
 234        mixer = mc->shift >> 8;
 235        shift = mc->shift & 0xff;
 236
 237        ucontrol->value.integer.value[0] =
 238                (wm9712->hp_mixer[mixer] >> shift) & 1;
 239
 240        return 0;
 241}
 242
 243#define WM9712_HP_MIXER_CTRL(xname, xmixer, xshift) { \
 244        .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
 245        .info = snd_soc_info_volsw, \
 246        .get = wm9712_hp_mixer_get, .put = wm9712_hp_mixer_put, \
 247        .private_value = SOC_SINGLE_VALUE(SND_SOC_NOPM, \
 248                (xmixer << 8) | xshift, 1, 0, 0) \
 249}
 250
 251/* Left Headphone Mixers */
 252static const struct snd_kcontrol_new wm9712_hpl_mixer_controls[] = {
 253        WM9712_HP_MIXER_CTRL("PCBeep Bypass Switch", HPL_MIXER, 5),
 254        WM9712_HP_MIXER_CTRL("Aux Playback Switch", HPL_MIXER, 4),
 255        WM9712_HP_MIXER_CTRL("Phone Bypass Switch", HPL_MIXER, 3),
 256        WM9712_HP_MIXER_CTRL("Line Bypass Switch", HPL_MIXER, 2),
 257        WM9712_HP_MIXER_CTRL("PCM Playback Switch", HPL_MIXER, 1),
 258        WM9712_HP_MIXER_CTRL("Mic Sidetone Switch", HPL_MIXER, 0),
 259};
 260
 261/* Right Headphone Mixers */
 262static const struct snd_kcontrol_new wm9712_hpr_mixer_controls[] = {
 263        WM9712_HP_MIXER_CTRL("PCBeep Bypass Switch", HPR_MIXER, 5),
 264        WM9712_HP_MIXER_CTRL("Aux Playback Switch", HPR_MIXER, 4),
 265        WM9712_HP_MIXER_CTRL("Phone Bypass Switch", HPR_MIXER, 3),
 266        WM9712_HP_MIXER_CTRL("Line Bypass Switch", HPR_MIXER, 2),
 267        WM9712_HP_MIXER_CTRL("PCM Playback Switch", HPR_MIXER, 1),
 268        WM9712_HP_MIXER_CTRL("Mic Sidetone Switch", HPR_MIXER, 0),
 269};
 270
 271/* Speaker Mixer */
 272static const struct snd_kcontrol_new wm9712_speaker_mixer_controls[] = {
 273        SOC_DAPM_SINGLE("PCBeep Bypass Switch", AC97_PC_BEEP, 11, 1, 1),
 274        SOC_DAPM_SINGLE("Aux Playback Switch", AC97_CD, 11, 1, 1),
 275        SOC_DAPM_SINGLE("Phone Bypass Switch", AC97_PHONE, 14, 1, 1),
 276        SOC_DAPM_SINGLE("Line Bypass Switch", AC97_LINE, 14, 1, 1),
 277        SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PCM, 14, 1, 1),
 278};
 279
 280/* Phone Mixer */
 281static const struct snd_kcontrol_new wm9712_phone_mixer_controls[] = {
 282        SOC_DAPM_SINGLE("PCBeep Bypass Switch", AC97_PC_BEEP, 7, 1, 1),
 283        SOC_DAPM_SINGLE("Aux Playback Switch", AC97_CD, 7, 1, 1),
 284        SOC_DAPM_SINGLE("Line Bypass Switch", AC97_LINE, 13, 1, 1),
 285        SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PCM, 13, 1, 1),
 286        SOC_DAPM_SINGLE("Mic 1 Sidetone Switch", AC97_MIC, 14, 1, 1),
 287        SOC_DAPM_SINGLE("Mic 2 Sidetone Switch", AC97_MIC, 13, 1, 1),
 288};
 289
 290/* ALC headphone mux */
 291static const struct snd_kcontrol_new wm9712_alc_mux_controls =
 292SOC_DAPM_ENUM("Route", wm9712_enum[1]);
 293
 294/* out 3 mux */
 295static const struct snd_kcontrol_new wm9712_out3_mux_controls =
 296SOC_DAPM_ENUM("Route", wm9712_enum[2]);
 297
 298/* spk mux */
 299static const struct snd_kcontrol_new wm9712_spk_mux_controls =
 300SOC_DAPM_ENUM("Route", wm9712_enum[3]);
 301
 302/* Capture to Phone mux */
 303static const struct snd_kcontrol_new wm9712_capture_phone_mux_controls =
 304SOC_DAPM_ENUM("Route", wm9712_enum[4]);
 305
 306/* Capture left select */
 307static const struct snd_kcontrol_new wm9712_capture_selectl_controls =
 308SOC_DAPM_ENUM("Route", wm9712_enum[8]);
 309
 310/* Capture right select */
 311static const struct snd_kcontrol_new wm9712_capture_selectr_controls =
 312SOC_DAPM_ENUM("Route", wm9712_enum[9]);
 313
 314/* Mic select */
 315static const struct snd_kcontrol_new wm9712_mic_src_controls =
 316SOC_DAPM_ENUM("Mic Source Select", wm9712_enum[7]);
 317
 318/* diff select */
 319static const struct snd_kcontrol_new wm9712_diff_sel_controls =
 320SOC_DAPM_ENUM("Route", wm9712_enum[11]);
 321
 322static const struct snd_soc_dapm_widget wm9712_dapm_widgets[] = {
 323SND_SOC_DAPM_MUX("ALC Sidetone Mux", SND_SOC_NOPM, 0, 0,
 324        &wm9712_alc_mux_controls),
 325SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM, 0, 0,
 326        &wm9712_out3_mux_controls),
 327SND_SOC_DAPM_MUX("Speaker Mux", SND_SOC_NOPM, 0, 0,
 328        &wm9712_spk_mux_controls),
 329SND_SOC_DAPM_MUX("Capture Phone Mux", SND_SOC_NOPM, 0, 0,
 330        &wm9712_capture_phone_mux_controls),
 331SND_SOC_DAPM_MUX("Left Capture Select", SND_SOC_NOPM, 0, 0,
 332        &wm9712_capture_selectl_controls),
 333SND_SOC_DAPM_MUX("Right Capture Select", SND_SOC_NOPM, 0, 0,
 334        &wm9712_capture_selectr_controls),
 335SND_SOC_DAPM_MUX("Left Mic Select Source", SND_SOC_NOPM, 0, 0,
 336        &wm9712_mic_src_controls),
 337SND_SOC_DAPM_MUX("Right Mic Select Source", SND_SOC_NOPM, 0, 0,
 338        &wm9712_mic_src_controls),
 339SND_SOC_DAPM_MUX("Differential Source", SND_SOC_NOPM, 0, 0,
 340        &wm9712_diff_sel_controls),
 341SND_SOC_DAPM_MIXER("AC97 Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
 342SND_SOC_DAPM_MIXER("Left HP Mixer", AC97_INT_PAGING, 9, 1,
 343        &wm9712_hpl_mixer_controls[0], ARRAY_SIZE(wm9712_hpl_mixer_controls)),
 344SND_SOC_DAPM_MIXER("Right HP Mixer", AC97_INT_PAGING, 8, 1,
 345        &wm9712_hpr_mixer_controls[0], ARRAY_SIZE(wm9712_hpr_mixer_controls)),
 346SND_SOC_DAPM_MIXER("Phone Mixer", AC97_INT_PAGING, 6, 1,
 347        &wm9712_phone_mixer_controls[0], ARRAY_SIZE(wm9712_phone_mixer_controls)),
 348SND_SOC_DAPM_MIXER("Speaker Mixer", AC97_INT_PAGING, 7, 1,
 349        &wm9712_speaker_mixer_controls[0],
 350        ARRAY_SIZE(wm9712_speaker_mixer_controls)),
 351SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
 352SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", AC97_INT_PAGING, 14, 1),
 353SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", AC97_INT_PAGING, 13, 1),
 354SND_SOC_DAPM_DAC("Aux DAC", "Aux Playback", SND_SOC_NOPM, 0, 0),
 355SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture", AC97_INT_PAGING, 12, 1),
 356SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture", AC97_INT_PAGING, 11, 1),
 357SND_SOC_DAPM_PGA("Headphone PGA", AC97_INT_PAGING, 4, 1, NULL, 0),
 358SND_SOC_DAPM_PGA("Speaker PGA", AC97_INT_PAGING, 3, 1, NULL, 0),
 359SND_SOC_DAPM_PGA("Out 3 PGA", AC97_INT_PAGING, 5, 1, NULL, 0),
 360SND_SOC_DAPM_PGA("Line PGA", AC97_INT_PAGING, 2, 1, NULL, 0),
 361SND_SOC_DAPM_PGA("Phone PGA", AC97_INT_PAGING, 1, 1, NULL, 0),
 362SND_SOC_DAPM_PGA("Mic PGA", AC97_INT_PAGING, 0, 1, NULL, 0),
 363SND_SOC_DAPM_PGA("Differential Mic", SND_SOC_NOPM, 0, 0, NULL, 0),
 364SND_SOC_DAPM_MICBIAS("Mic Bias", AC97_INT_PAGING, 10, 1),
 365SND_SOC_DAPM_OUTPUT("MONOOUT"),
 366SND_SOC_DAPM_OUTPUT("HPOUTL"),
 367SND_SOC_DAPM_OUTPUT("HPOUTR"),
 368SND_SOC_DAPM_OUTPUT("LOUT2"),
 369SND_SOC_DAPM_OUTPUT("ROUT2"),
 370SND_SOC_DAPM_OUTPUT("OUT3"),
 371SND_SOC_DAPM_INPUT("LINEINL"),
 372SND_SOC_DAPM_INPUT("LINEINR"),
 373SND_SOC_DAPM_INPUT("PHONE"),
 374SND_SOC_DAPM_INPUT("PCBEEP"),
 375SND_SOC_DAPM_INPUT("MIC1"),
 376SND_SOC_DAPM_INPUT("MIC2"),
 377};
 378
 379static const struct snd_soc_dapm_route wm9712_audio_map[] = {
 380        /* virtual mixer - mixes left & right channels for spk and mono */
 381        {"AC97 Mixer", NULL, "Left DAC"},
 382        {"AC97 Mixer", NULL, "Right DAC"},
 383
 384        /* Left HP mixer */
 385        {"Left HP Mixer", "PCBeep Bypass Switch", "PCBEEP"},
 386        {"Left HP Mixer", "Aux Playback Switch",  "Aux DAC"},
 387        {"Left HP Mixer", "Phone Bypass Switch",  "Phone PGA"},
 388        {"Left HP Mixer", "Line Bypass Switch",   "Line PGA"},
 389        {"Left HP Mixer", "PCM Playback Switch",  "Left DAC"},
 390        {"Left HP Mixer", "Mic Sidetone Switch",  "Mic PGA"},
 391        {"Left HP Mixer", NULL,  "ALC Sidetone Mux"},
 392
 393        /* Right HP mixer */
 394        {"Right HP Mixer", "PCBeep Bypass Switch", "PCBEEP"},
 395        {"Right HP Mixer", "Aux Playback Switch",  "Aux DAC"},
 396        {"Right HP Mixer", "Phone Bypass Switch",  "Phone PGA"},
 397        {"Right HP Mixer", "Line Bypass Switch",   "Line PGA"},
 398        {"Right HP Mixer", "PCM Playback Switch",  "Right DAC"},
 399        {"Right HP Mixer", "Mic Sidetone Switch",  "Mic PGA"},
 400        {"Right HP Mixer", NULL,  "ALC Sidetone Mux"},
 401
 402        /* speaker mixer */
 403        {"Speaker Mixer", "PCBeep Bypass Switch", "PCBEEP"},
 404        {"Speaker Mixer", "Line Bypass Switch",   "Line PGA"},
 405        {"Speaker Mixer", "PCM Playback Switch",  "AC97 Mixer"},
 406        {"Speaker Mixer", "Phone Bypass Switch",  "Phone PGA"},
 407        {"Speaker Mixer", "Aux Playback Switch",  "Aux DAC"},
 408
 409        /* Phone mixer */
 410        {"Phone Mixer", "PCBeep Bypass Switch",  "PCBEEP"},
 411        {"Phone Mixer", "Line Bypass Switch",    "Line PGA"},
 412        {"Phone Mixer", "Aux Playback Switch",   "Aux DAC"},
 413        {"Phone Mixer", "PCM Playback Switch",   "AC97 Mixer"},
 414        {"Phone Mixer", "Mic 1 Sidetone Switch", "Mic PGA"},
 415        {"Phone Mixer", "Mic 2 Sidetone Switch", "Mic PGA"},
 416
 417        /* inputs */
 418        {"Line PGA", NULL, "LINEINL"},
 419        {"Line PGA", NULL, "LINEINR"},
 420        {"Phone PGA", NULL, "PHONE"},
 421        {"Mic PGA", NULL, "MIC1"},
 422        {"Mic PGA", NULL, "MIC2"},
 423
 424        /* microphones */
 425        {"Differential Mic", NULL, "MIC1"},
 426        {"Differential Mic", NULL, "MIC2"},
 427        {"Left Mic Select Source", "Mic 1", "MIC1"},
 428        {"Left Mic Select Source", "Mic 2", "MIC2"},
 429        {"Left Mic Select Source", "Stereo", "MIC1"},
 430        {"Left Mic Select Source", "Differential", "Differential Mic"},
 431        {"Right Mic Select Source", "Mic 1", "MIC1"},
 432        {"Right Mic Select Source", "Mic 2", "MIC2"},
 433        {"Right Mic Select Source", "Stereo", "MIC2"},
 434        {"Right Mic Select Source", "Differential", "Differential Mic"},
 435
 436        /* left capture selector */
 437        {"Left Capture Select", "Mic", "MIC1"},
 438        {"Left Capture Select", "Speaker Mixer", "Speaker Mixer"},
 439        {"Left Capture Select", "Line", "LINEINL"},
 440        {"Left Capture Select", "Headphone Mixer", "Left HP Mixer"},
 441        {"Left Capture Select", "Phone Mixer", "Phone Mixer"},
 442        {"Left Capture Select", "Phone", "PHONE"},
 443
 444        /* right capture selector */
 445        {"Right Capture Select", "Mic", "MIC2"},
 446        {"Right Capture Select", "Speaker Mixer", "Speaker Mixer"},
 447        {"Right Capture Select", "Line", "LINEINR"},
 448        {"Right Capture Select", "Headphone Mixer", "Right HP Mixer"},
 449        {"Right Capture Select", "Phone Mixer", "Phone Mixer"},
 450        {"Right Capture Select", "Phone", "PHONE"},
 451
 452        /* ALC Sidetone */
 453        {"ALC Sidetone Mux", "Stereo", "Left Capture Select"},
 454        {"ALC Sidetone Mux", "Stereo", "Right Capture Select"},
 455        {"ALC Sidetone Mux", "Left", "Left Capture Select"},
 456        {"ALC Sidetone Mux", "Right", "Right Capture Select"},
 457
 458        /* ADC's */
 459        {"Left ADC", NULL, "Left Capture Select"},
 460        {"Right ADC", NULL, "Right Capture Select"},
 461
 462        /* outputs */
 463        {"MONOOUT", NULL, "Phone Mixer"},
 464        {"HPOUTL", NULL, "Headphone PGA"},
 465        {"Headphone PGA", NULL, "Left HP Mixer"},
 466        {"HPOUTR", NULL, "Headphone PGA"},
 467        {"Headphone PGA", NULL, "Right HP Mixer"},
 468
 469        /* mono mixer */
 470        {"Mono Mixer", NULL, "Left HP Mixer"},
 471        {"Mono Mixer", NULL, "Right HP Mixer"},
 472
 473        /* Out3 Mux */
 474        {"Out3 Mux", "Left", "Left HP Mixer"},
 475        {"Out3 Mux", "Mono", "Phone Mixer"},
 476        {"Out3 Mux", "Left + Right", "Mono Mixer"},
 477        {"Out 3 PGA", NULL, "Out3 Mux"},
 478        {"OUT3", NULL, "Out 3 PGA"},
 479
 480        /* speaker Mux */
 481        {"Speaker Mux", "Speaker Mix", "Speaker Mixer"},
 482        {"Speaker Mux", "Headphone Mix", "Mono Mixer"},
 483        {"Speaker PGA", NULL, "Speaker Mux"},
 484        {"LOUT2", NULL, "Speaker PGA"},
 485        {"ROUT2", NULL, "Speaker PGA"},
 486};
 487
 488static unsigned int ac97_read(struct snd_soc_codec *codec,
 489        unsigned int reg)
 490{
 491        struct wm9712_priv *wm9712 = snd_soc_codec_get_drvdata(codec);
 492        u16 *cache = codec->reg_cache;
 493
 494        if (reg == AC97_RESET || reg == AC97_GPIO_STATUS ||
 495                reg == AC97_VENDOR_ID1 || reg == AC97_VENDOR_ID2 ||
 496                reg == AC97_REC_GAIN)
 497                return soc_ac97_ops->read(wm9712->ac97, reg);
 498        else {
 499                reg = reg >> 1;
 500
 501                if (reg >= (ARRAY_SIZE(wm9712_reg)))
 502                        return -EIO;
 503
 504                return cache[reg];
 505        }
 506}
 507
 508static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
 509        unsigned int val)
 510{
 511        struct wm9712_priv *wm9712 = snd_soc_codec_get_drvdata(codec);
 512        u16 *cache = codec->reg_cache;
 513
 514        soc_ac97_ops->write(wm9712->ac97, reg, val);
 515        reg = reg >> 1;
 516        if (reg < (ARRAY_SIZE(wm9712_reg)))
 517                cache[reg] = val;
 518
 519        return 0;
 520}
 521
 522static int ac97_prepare(struct snd_pcm_substream *substream,
 523                        struct snd_soc_dai *dai)
 524{
 525        struct snd_soc_codec *codec = dai->codec;
 526        int reg;
 527        u16 vra;
 528        struct snd_pcm_runtime *runtime = substream->runtime;
 529
 530        vra = ac97_read(codec, AC97_EXTENDED_STATUS);
 531        ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
 532
 533        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
 534                reg = AC97_PCM_FRONT_DAC_RATE;
 535        else
 536                reg = AC97_PCM_LR_ADC_RATE;
 537
 538        return ac97_write(codec, reg, runtime->rate);
 539}
 540
 541static int ac97_aux_prepare(struct snd_pcm_substream *substream,
 542                            struct snd_soc_dai *dai)
 543{
 544        struct snd_soc_codec *codec = dai->codec;
 545        u16 vra, xsle;
 546        struct snd_pcm_runtime *runtime = substream->runtime;
 547
 548        vra = ac97_read(codec, AC97_EXTENDED_STATUS);
 549        ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
 550        xsle = ac97_read(codec, AC97_PCI_SID);
 551        ac97_write(codec, AC97_PCI_SID, xsle | 0x8000);
 552
 553        if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
 554                return -ENODEV;
 555
 556        return ac97_write(codec, AC97_PCM_SURR_DAC_RATE, runtime->rate);
 557}
 558
 559#define WM9712_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
 560                SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 |\
 561                SNDRV_PCM_RATE_48000)
 562
 563static const struct snd_soc_dai_ops wm9712_dai_ops_hifi = {
 564        .prepare        = ac97_prepare,
 565};
 566
 567static const struct snd_soc_dai_ops wm9712_dai_ops_aux = {
 568        .prepare        = ac97_aux_prepare,
 569};
 570
 571static struct snd_soc_dai_driver wm9712_dai[] = {
 572{
 573        .name = "wm9712-hifi",
 574        .playback = {
 575                .stream_name = "HiFi Playback",
 576                .channels_min = 1,
 577                .channels_max = 2,
 578                .rates = WM9712_AC97_RATES,
 579                .formats = SND_SOC_STD_AC97_FMTS,},
 580        .capture = {
 581                .stream_name = "HiFi Capture",
 582                .channels_min = 1,
 583                .channels_max = 2,
 584                .rates = WM9712_AC97_RATES,
 585                .formats = SND_SOC_STD_AC97_FMTS,},
 586        .ops = &wm9712_dai_ops_hifi,
 587},
 588{
 589        .name = "wm9712-aux",
 590        .playback = {
 591                .stream_name = "Aux Playback",
 592                .channels_min = 1,
 593                .channels_max = 1,
 594                .rates = WM9712_AC97_RATES,
 595                .formats = SND_SOC_STD_AC97_FMTS,},
 596        .ops = &wm9712_dai_ops_aux,
 597}
 598};
 599
 600static int wm9712_set_bias_level(struct snd_soc_codec *codec,
 601                                 enum snd_soc_bias_level level)
 602{
 603        switch (level) {
 604        case SND_SOC_BIAS_ON:
 605        case SND_SOC_BIAS_PREPARE:
 606                break;
 607        case SND_SOC_BIAS_STANDBY:
 608                ac97_write(codec, AC97_POWERDOWN, 0x0000);
 609                break;
 610        case SND_SOC_BIAS_OFF:
 611                /* disable everything including AC link */
 612                ac97_write(codec, AC97_EXTENDED_MSTATUS, 0xffff);
 613                ac97_write(codec, AC97_POWERDOWN, 0xffff);
 614                break;
 615        }
 616        return 0;
 617}
 618
 619static int wm9712_soc_resume(struct snd_soc_codec *codec)
 620{
 621        struct wm9712_priv *wm9712 = snd_soc_codec_get_drvdata(codec);
 622        int i, ret;
 623        u16 *cache = codec->reg_cache;
 624
 625        ret = snd_ac97_reset(wm9712->ac97, true, WM9712_VENDOR_ID,
 626                WM9712_VENDOR_ID_MASK);
 627        if (ret < 0)
 628                return ret;
 629
 630        snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_STANDBY);
 631
 632        if (ret == 0) {
 633                /* Sync reg_cache with the hardware after cold reset */
 634                for (i = 2; i < ARRAY_SIZE(wm9712_reg) << 1; i += 2) {
 635                        if (i == AC97_INT_PAGING || i == AC97_POWERDOWN ||
 636                            (i > 0x58 && i != 0x5c))
 637                                continue;
 638                        soc_ac97_ops->write(wm9712->ac97, i, cache[i>>1]);
 639                }
 640        }
 641
 642        return ret;
 643}
 644
 645static int wm9712_soc_probe(struct snd_soc_codec *codec)
 646{
 647        struct wm9712_priv *wm9712 = snd_soc_codec_get_drvdata(codec);
 648        int ret;
 649
 650        wm9712->ac97 = snd_soc_new_ac97_codec(codec, WM9712_VENDOR_ID,
 651                WM9712_VENDOR_ID_MASK);
 652        if (IS_ERR(wm9712->ac97)) {
 653                ret = PTR_ERR(wm9712->ac97);
 654                dev_err(codec->dev, "Failed to register AC97 codec: %d\n", ret);
 655                return ret;
 656        }
 657
 658        /* set alc mux to none */
 659        ac97_write(codec, AC97_VIDEO, ac97_read(codec, AC97_VIDEO) | 0x3000);
 660
 661        return 0;
 662}
 663
 664static int wm9712_soc_remove(struct snd_soc_codec *codec)
 665{
 666        struct wm9712_priv *wm9712 = snd_soc_codec_get_drvdata(codec);
 667
 668        snd_soc_free_ac97_codec(wm9712->ac97);
 669        return 0;
 670}
 671
 672static const struct snd_soc_codec_driver soc_codec_dev_wm9712 = {
 673        .probe =        wm9712_soc_probe,
 674        .remove =       wm9712_soc_remove,
 675        .resume =       wm9712_soc_resume,
 676        .read = ac97_read,
 677        .write = ac97_write,
 678        .set_bias_level = wm9712_set_bias_level,
 679        .suspend_bias_off = true,
 680        .reg_cache_size = ARRAY_SIZE(wm9712_reg),
 681        .reg_word_size = sizeof(u16),
 682        .reg_cache_step = 2,
 683        .reg_cache_default = wm9712_reg,
 684
 685        .component_driver = {
 686                .controls               = wm9712_snd_ac97_controls,
 687                .num_controls           = ARRAY_SIZE(wm9712_snd_ac97_controls),
 688                .dapm_widgets           = wm9712_dapm_widgets,
 689                .num_dapm_widgets       = ARRAY_SIZE(wm9712_dapm_widgets),
 690                .dapm_routes            = wm9712_audio_map,
 691                .num_dapm_routes        = ARRAY_SIZE(wm9712_audio_map),
 692        },
 693};
 694
 695static int wm9712_probe(struct platform_device *pdev)
 696{
 697        struct wm9712_priv *wm9712;
 698
 699        wm9712 = devm_kzalloc(&pdev->dev, sizeof(*wm9712), GFP_KERNEL);
 700        if (wm9712 == NULL)
 701                return -ENOMEM;
 702
 703        mutex_init(&wm9712->lock);
 704
 705        platform_set_drvdata(pdev, wm9712);
 706
 707        return snd_soc_register_codec(&pdev->dev,
 708                        &soc_codec_dev_wm9712, wm9712_dai, ARRAY_SIZE(wm9712_dai));
 709}
 710
 711static int wm9712_remove(struct platform_device *pdev)
 712{
 713        snd_soc_unregister_codec(&pdev->dev);
 714        return 0;
 715}
 716
 717static struct platform_driver wm9712_codec_driver = {
 718        .driver = {
 719                .name = "wm9712-codec",
 720        },
 721
 722        .probe = wm9712_probe,
 723        .remove = wm9712_remove,
 724};
 725
 726module_platform_driver(wm9712_codec_driver);
 727
 728MODULE_DESCRIPTION("ASoC WM9711/WM9712 driver");
 729MODULE_AUTHOR("Liam Girdwood");
 730MODULE_LICENSE("GPL");
 731