linux/sound/soc/codecs/alc5632.c
<<
>>
Prefs
   1/*
   2* alc5632.c  --  ALC5632 ALSA SoC Audio Codec
   3*
   4* Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
   5*
   6* Authors:  Leon Romanovsky <leon@leon.nu>
   7*           Andrey Danin <danindrey@mail.ru>
   8*           Ilya Petrov <ilya.muromec@gmail.com>
   9*           Marc Dietrich <marvin24@gmx.de>
  10*
  11* Based on alc5623.c by Arnaud Patard
  12*
  13* This program is free software; you can redistribute it and/or modify
  14* it under the terms of the GNU General Public License version 2 as
  15* published by the Free Software Foundation.
  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/slab.h>
  25#include <linux/regmap.h>
  26#include <sound/core.h>
  27#include <sound/pcm.h>
  28#include <sound/pcm_params.h>
  29#include <sound/tlv.h>
  30#include <sound/soc.h>
  31#include <sound/initval.h>
  32
  33#include "alc5632.h"
  34
  35/*
  36 * ALC5632 register cache
  37 */
  38static const struct reg_default alc5632_reg_defaults[] = {
  39        {   2, 0x8080 },        /* R2   - Speaker Output Volume */
  40        {   4, 0x8080 },        /* R4   - Headphone Output Volume */
  41        {   6, 0x8080 },        /* R6   - AUXOUT Volume */
  42        {   8, 0xC800 },        /* R8   - Phone Input */
  43        {  10, 0xE808 },        /* R10  - LINE_IN Volume */
  44        {  12, 0x1010 },        /* R12  - STEREO DAC Input Volume */
  45        {  14, 0x0808 },        /* R14  - MIC Input Volume */
  46        {  16, 0xEE0F },        /* R16  - Stereo DAC and MIC Routing Control */
  47        {  18, 0xCBCB },        /* R18  - ADC Record Gain */
  48        {  20, 0x7F7F },        /* R20  - ADC Record Mixer Control */
  49        {  24, 0xE010 },        /* R24  - Voice DAC Volume */
  50        {  28, 0x8008 },        /* R28  - Output Mixer Control */
  51        {  34, 0x0000 },        /* R34  - Microphone Control */
  52        {  36, 0x00C0 },    /* R36  - Codec Digital MIC/Digital Boost
  53                                                   Control */
  54        {  46, 0x0000 },        /* R46  - Stereo DAC/Voice DAC/Stereo ADC
  55                                                   Function Select */
  56        {  52, 0x8000 },        /* R52  - Main Serial Data Port Control
  57                                                   (Stereo I2S) */
  58        {  54, 0x0000 },        /* R54  - Extend Serial Data Port Control
  59                                                   (VoDAC_I2S/PCM) */
  60        {  58, 0x0000 },        /* R58  - Power Management Addition 1 */
  61        {  60, 0x0000 },        /* R60  - Power Management Addition 2 */
  62        {  62, 0x8000 },        /* R62  - Power Management Addition 3 */
  63        {  64, 0x0C0A },        /* R64  - General Purpose Control Register 1 */
  64        {  66, 0x0000 },        /* R66  - General Purpose Control Register 2 */
  65        {  68, 0x0000 },        /* R68  - PLL1 Control */
  66        {  70, 0x0000 },        /* R70  - PLL2 Control */
  67        {  76, 0xBE3E },        /* R76  - GPIO Pin Configuration */
  68        {  78, 0xBE3E },        /* R78  - GPIO Pin Polarity */
  69        {  80, 0x0000 },        /* R80  - GPIO Pin Sticky */
  70        {  82, 0x0000 },        /* R82  - GPIO Pin Wake Up */
  71        {  86, 0x0000 },        /* R86  - Pin Sharing */
  72        {  90, 0x0009 },        /* R90  - Soft Volume Control Setting */
  73        {  92, 0x0000 },        /* R92  - GPIO_Output Pin Control */
  74        {  94, 0x3000 },        /* R94  - MISC Control */
  75        {  96, 0x3075 },        /* R96  - Stereo DAC Clock Control_1 */
  76        {  98, 0x1010 },        /* R98  - Stereo DAC Clock Control_2 */
  77        { 100, 0x3110 },        /* R100 - VoDAC_PCM Clock Control_1 */
  78        { 104, 0x0553 },        /* R104 - Pseudo Stereo and Spatial Effect
  79                                                   Block Control */
  80        { 106, 0x0000 },        /* R106 - Private Register Address */
  81};
  82
  83/* codec private data */
  84struct alc5632_priv {
  85        struct regmap *regmap;
  86        u8 id;
  87        unsigned int sysclk;
  88};
  89
  90static bool alc5632_volatile_register(struct device *dev,
  91                                                        unsigned int reg)
  92{
  93        switch (reg) {
  94        case ALC5632_RESET:
  95        case ALC5632_PWR_DOWN_CTRL_STATUS:
  96        case ALC5632_GPIO_PIN_STATUS:
  97        case ALC5632_OVER_CURR_STATUS:
  98        case ALC5632_HID_CTRL_DATA:
  99        case ALC5632_EQ_CTRL:
 100        case ALC5632_VENDOR_ID1:
 101        case ALC5632_VENDOR_ID2:
 102                return true;
 103
 104        default:
 105                break;
 106        }
 107
 108        return false;
 109}
 110
 111static inline int alc5632_reset(struct regmap *map)
 112{
 113        return regmap_write(map, ALC5632_RESET, 0x59B4);
 114}
 115
 116static int amp_mixer_event(struct snd_soc_dapm_widget *w,
 117        struct snd_kcontrol *kcontrol, int event)
 118{
 119        struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
 120
 121        /* to power-on/off class-d amp generators/speaker */
 122        /* need to write to 'index-46h' register :        */
 123        /* so write index num (here 0x46) to reg 0x6a     */
 124        /* and then 0xffff/0 to reg 0x6c                  */
 125        snd_soc_write(codec, ALC5632_HID_CTRL_INDEX, 0x46);
 126
 127        switch (event) {
 128        case SND_SOC_DAPM_PRE_PMU:
 129                snd_soc_write(codec, ALC5632_HID_CTRL_DATA, 0xFFFF);
 130                break;
 131        case SND_SOC_DAPM_POST_PMD:
 132                snd_soc_write(codec, ALC5632_HID_CTRL_DATA, 0);
 133                break;
 134        }
 135
 136        return 0;
 137}
 138
 139/*
 140 * ALC5632 Controls
 141 */
 142
 143/* -34.5db min scale, 1.5db steps, no mute */
 144static const DECLARE_TLV_DB_SCALE(vol_tlv, -3450, 150, 0);
 145/* -46.5db min scale, 1.5db steps, no mute */
 146static const DECLARE_TLV_DB_SCALE(hp_tlv, -4650, 150, 0);
 147/* -16.5db min scale, 1.5db steps, no mute */
 148static const DECLARE_TLV_DB_SCALE(adc_rec_tlv, -1650, 150, 0);
 149static const DECLARE_TLV_DB_RANGE(boost_tlv,
 150        0, 1, TLV_DB_SCALE_ITEM(0, 2000, 0),
 151        1, 3, TLV_DB_SCALE_ITEM(2000, 1000, 0)
 152);
 153/* 0db min scale, 6 db steps, no mute */
 154static const DECLARE_TLV_DB_SCALE(dig_tlv, 0, 600, 0);
 155/* 0db min scalem 0.75db steps, no mute */
 156static const DECLARE_TLV_DB_SCALE(vdac_tlv, -3525, 75, 0);
 157
 158static const struct snd_kcontrol_new alc5632_vol_snd_controls[] = {
 159        /* left starts at bit 8, right at bit 0 */
 160        /* 31 steps (5 bit), -46.5db scale */
 161        SOC_DOUBLE_TLV("Speaker Playback Volume",
 162                        ALC5632_SPK_OUT_VOL, 8, 0, 31, 1, hp_tlv),
 163        /* bit 15 mutes left, bit 7 right */
 164        SOC_DOUBLE("Speaker Playback Switch",
 165                        ALC5632_SPK_OUT_VOL, 15, 7, 1, 1),
 166        SOC_DOUBLE_TLV("Headphone Playback Volume",
 167                        ALC5632_HP_OUT_VOL, 8, 0, 31, 1, hp_tlv),
 168        SOC_DOUBLE("Headphone Playback Switch",
 169                        ALC5632_HP_OUT_VOL, 15, 7, 1, 1),
 170};
 171
 172static const struct snd_kcontrol_new alc5632_snd_controls[] = {
 173        SOC_DOUBLE_TLV("Auxout Playback Volume",
 174                        ALC5632_AUX_OUT_VOL, 8, 0, 31, 1, hp_tlv),
 175        SOC_DOUBLE("Auxout Playback Switch",
 176                        ALC5632_AUX_OUT_VOL, 15, 7, 1, 1),
 177        SOC_SINGLE_TLV("Voice DAC Playback Volume",
 178                        ALC5632_VOICE_DAC_VOL, 0, 63, 0, vdac_tlv),
 179        SOC_SINGLE("Voice DAC Playback Switch",
 180                        ALC5632_VOICE_DAC_VOL, 12, 1, 1),
 181        SOC_SINGLE_TLV("Phone Playback Volume",
 182                        ALC5632_PHONE_IN_VOL, 8, 31, 1, vol_tlv),
 183        SOC_DOUBLE_TLV("LineIn Playback Volume",
 184                        ALC5632_LINE_IN_VOL, 8, 0, 31, 1, vol_tlv),
 185        SOC_DOUBLE_TLV("Master Playback Volume",
 186                        ALC5632_STEREO_DAC_IN_VOL, 8, 0, 63, 1, vdac_tlv),
 187        SOC_DOUBLE("Master Playback Switch",
 188                        ALC5632_STEREO_DAC_IN_VOL, 15, 7, 1, 1),
 189        SOC_SINGLE_TLV("Mic1 Playback Volume",
 190                        ALC5632_MIC_VOL, 8, 31, 1, vol_tlv),
 191        SOC_SINGLE_TLV("Mic2 Playback Volume",
 192                        ALC5632_MIC_VOL, 0, 31, 1, vol_tlv),
 193        SOC_DOUBLE_TLV("Rec Capture Volume",
 194                        ALC5632_ADC_REC_GAIN, 8, 0, 31, 0, adc_rec_tlv),
 195        SOC_SINGLE_TLV("Mic 1 Boost Volume",
 196                        ALC5632_MIC_CTRL, 10, 3, 0, boost_tlv),
 197        SOC_SINGLE_TLV("Mic 2 Boost Volume",
 198                        ALC5632_MIC_CTRL, 8, 3, 0, boost_tlv),
 199        SOC_SINGLE_TLV("DMIC Boost Capture Volume",
 200                        ALC5632_DIGI_BOOST_CTRL, 0, 7, 0, dig_tlv),
 201        SOC_SINGLE("DMIC En Capture Switch",
 202                        ALC5632_DIGI_BOOST_CTRL, 15, 1, 0),
 203        SOC_SINGLE("DMIC PreFilter Capture Switch",
 204                        ALC5632_DIGI_BOOST_CTRL, 12, 1, 0),
 205};
 206
 207/*
 208 * DAPM Controls
 209 */
 210static const struct snd_kcontrol_new alc5632_hp_mixer_controls[] = {
 211SOC_DAPM_SINGLE("LI2HP Playback Switch", ALC5632_LINE_IN_VOL, 15, 1, 1),
 212SOC_DAPM_SINGLE("PHONE2HP Playback Switch", ALC5632_PHONE_IN_VOL, 15, 1, 1),
 213SOC_DAPM_SINGLE("MIC12HP Playback Switch", ALC5632_MIC_ROUTING_CTRL, 15, 1, 1),
 214SOC_DAPM_SINGLE("MIC22HP Playback Switch", ALC5632_MIC_ROUTING_CTRL, 11, 1, 1),
 215SOC_DAPM_SINGLE("VOICE2HP Playback Switch", ALC5632_VOICE_DAC_VOL, 15, 1, 1),
 216};
 217
 218static const struct snd_kcontrol_new alc5632_hpl_mixer_controls[] = {
 219SOC_DAPM_SINGLE("ADC2HP_L Playback Switch", ALC5632_ADC_REC_GAIN, 15, 1, 1),
 220SOC_DAPM_SINGLE("DACL2HP Playback Switch", ALC5632_MIC_ROUTING_CTRL, 3, 1, 1),
 221};
 222
 223static const struct snd_kcontrol_new alc5632_hpr_mixer_controls[] = {
 224SOC_DAPM_SINGLE("ADC2HP_R Playback Switch", ALC5632_ADC_REC_GAIN, 7, 1, 1),
 225SOC_DAPM_SINGLE("DACR2HP Playback Switch", ALC5632_MIC_ROUTING_CTRL, 2, 1, 1),
 226};
 227
 228static const struct snd_kcontrol_new alc5632_mono_mixer_controls[] = {
 229SOC_DAPM_SINGLE("ADC2MONO_L Playback Switch", ALC5632_ADC_REC_GAIN, 14, 1, 1),
 230SOC_DAPM_SINGLE("ADC2MONO_R Playback Switch", ALC5632_ADC_REC_GAIN, 6, 1, 1),
 231SOC_DAPM_SINGLE("LI2MONO Playback Switch", ALC5632_LINE_IN_VOL, 13, 1, 1),
 232SOC_DAPM_SINGLE("MIC12MONO Playback Switch",
 233                                        ALC5632_MIC_ROUTING_CTRL, 13, 1, 1),
 234SOC_DAPM_SINGLE("MIC22MONO Playback Switch",
 235                                        ALC5632_MIC_ROUTING_CTRL, 9, 1, 1),
 236SOC_DAPM_SINGLE("DAC2MONO Playback Switch", ALC5632_MIC_ROUTING_CTRL, 0, 1, 1),
 237SOC_DAPM_SINGLE("VOICE2MONO Playback Switch", ALC5632_VOICE_DAC_VOL, 13, 1, 1),
 238};
 239
 240static const struct snd_kcontrol_new alc5632_speaker_mixer_controls[] = {
 241SOC_DAPM_SINGLE("LI2SPK Playback Switch", ALC5632_LINE_IN_VOL, 14, 1, 1),
 242SOC_DAPM_SINGLE("PHONE2SPK Playback Switch", ALC5632_PHONE_IN_VOL, 14, 1, 1),
 243SOC_DAPM_SINGLE("MIC12SPK Playback Switch",
 244                                        ALC5632_MIC_ROUTING_CTRL, 14, 1, 1),
 245SOC_DAPM_SINGLE("MIC22SPK Playback Switch",
 246                                        ALC5632_MIC_ROUTING_CTRL, 10, 1, 1),
 247SOC_DAPM_SINGLE("DAC2SPK Playback Switch", ALC5632_MIC_ROUTING_CTRL, 1, 1, 1),
 248SOC_DAPM_SINGLE("VOICE2SPK Playback Switch", ALC5632_VOICE_DAC_VOL, 14, 1, 1),
 249};
 250
 251/* Left Record Mixer */
 252static const struct snd_kcontrol_new alc5632_captureL_mixer_controls[] = {
 253SOC_DAPM_SINGLE("MIC12REC_L Capture Switch", ALC5632_ADC_REC_MIXER, 14, 1, 1),
 254SOC_DAPM_SINGLE("MIC22REC_L Capture Switch", ALC5632_ADC_REC_MIXER, 13, 1, 1),
 255SOC_DAPM_SINGLE("LIL2REC Capture Switch", ALC5632_ADC_REC_MIXER, 12, 1, 1),
 256SOC_DAPM_SINGLE("PH2REC_L Capture Switch", ALC5632_ADC_REC_MIXER, 11, 1, 1),
 257SOC_DAPM_SINGLE("HPL2REC Capture Switch", ALC5632_ADC_REC_MIXER, 10, 1, 1),
 258SOC_DAPM_SINGLE("SPK2REC_L Capture Switch", ALC5632_ADC_REC_MIXER, 9, 1, 1),
 259SOC_DAPM_SINGLE("MONO2REC_L Capture Switch", ALC5632_ADC_REC_MIXER, 8, 1, 1),
 260};
 261
 262/* Right Record Mixer */
 263static const struct snd_kcontrol_new alc5632_captureR_mixer_controls[] = {
 264SOC_DAPM_SINGLE("MIC12REC_R Capture Switch", ALC5632_ADC_REC_MIXER, 6, 1, 1),
 265SOC_DAPM_SINGLE("MIC22REC_R Capture Switch", ALC5632_ADC_REC_MIXER, 5, 1, 1),
 266SOC_DAPM_SINGLE("LIR2REC Capture Switch", ALC5632_ADC_REC_MIXER, 4, 1, 1),
 267SOC_DAPM_SINGLE("PH2REC_R Capture Switch", ALC5632_ADC_REC_MIXER, 3, 1, 1),
 268SOC_DAPM_SINGLE("HPR2REC Capture Switch", ALC5632_ADC_REC_MIXER, 2, 1, 1),
 269SOC_DAPM_SINGLE("SPK2REC_R Capture Switch", ALC5632_ADC_REC_MIXER, 1, 1, 1),
 270SOC_DAPM_SINGLE("MONO2REC_R Capture Switch", ALC5632_ADC_REC_MIXER, 0, 1, 1),
 271};
 272
 273/* Dmic Mixer */
 274static const struct snd_kcontrol_new alc5632_dmicl_mixer_controls[] = {
 275SOC_DAPM_SINGLE("DMICL2ADC Capture Switch", ALC5632_DIGI_BOOST_CTRL, 7, 1, 1),
 276};
 277static const struct snd_kcontrol_new alc5632_dmicr_mixer_controls[] = {
 278SOC_DAPM_SINGLE("DMICR2ADC Capture Switch", ALC5632_DIGI_BOOST_CTRL, 6, 1, 1),
 279};
 280
 281static const char * const alc5632_spk_n_sour_sel[] = {
 282                "RN/-R", "RP/+R", "LN/-R", "Mute"};
 283static const char * const alc5632_hpl_out_input_sel[] = {
 284                "Vmid", "HP Left Mix"};
 285static const char * const alc5632_hpr_out_input_sel[] = {
 286                "Vmid", "HP Right Mix"};
 287static const char * const alc5632_spkout_input_sel[] = {
 288                "Vmid", "HPOut Mix", "Speaker Mix", "Mono Mix"};
 289static const char * const alc5632_aux_out_input_sel[] = {
 290                "Vmid", "HPOut Mix", "Speaker Mix", "Mono Mix"};
 291static const char * const alc5632_adcr_func_sel[] = {
 292                "Stereo ADC", "Voice ADC"};
 293static const char * const alc5632_i2s_out_sel[] = {
 294                "ADC LR", "Voice Stereo Digital"};
 295
 296/* auxout output mux */
 297static SOC_ENUM_SINGLE_DECL(alc5632_aux_out_input_enum,
 298                            ALC5632_OUTPUT_MIXER_CTRL, 6,
 299                            alc5632_aux_out_input_sel);
 300static const struct snd_kcontrol_new alc5632_auxout_mux_controls =
 301SOC_DAPM_ENUM("AuxOut Mux", alc5632_aux_out_input_enum);
 302
 303/* speaker output mux */
 304static SOC_ENUM_SINGLE_DECL(alc5632_spkout_input_enum,
 305                            ALC5632_OUTPUT_MIXER_CTRL, 10,
 306                            alc5632_spkout_input_sel);
 307static const struct snd_kcontrol_new alc5632_spkout_mux_controls =
 308SOC_DAPM_ENUM("SpeakerOut Mux", alc5632_spkout_input_enum);
 309
 310/* headphone left output mux */
 311static SOC_ENUM_SINGLE_DECL(alc5632_hpl_out_input_enum,
 312                            ALC5632_OUTPUT_MIXER_CTRL, 9,
 313                            alc5632_hpl_out_input_sel);
 314static const struct snd_kcontrol_new alc5632_hpl_out_mux_controls =
 315SOC_DAPM_ENUM("Left Headphone Mux", alc5632_hpl_out_input_enum);
 316
 317/* headphone right output mux */
 318static SOC_ENUM_SINGLE_DECL(alc5632_hpr_out_input_enum,
 319                            ALC5632_OUTPUT_MIXER_CTRL, 8,
 320                            alc5632_hpr_out_input_sel);
 321static const struct snd_kcontrol_new alc5632_hpr_out_mux_controls =
 322SOC_DAPM_ENUM("Right Headphone Mux", alc5632_hpr_out_input_enum);
 323
 324/* speaker output N select */
 325static SOC_ENUM_SINGLE_DECL(alc5632_spk_n_sour_enum,
 326                            ALC5632_OUTPUT_MIXER_CTRL, 14,
 327                            alc5632_spk_n_sour_sel);
 328static const struct snd_kcontrol_new alc5632_spkoutn_mux_controls =
 329SOC_DAPM_ENUM("SpeakerOut N Mux", alc5632_spk_n_sour_enum);
 330
 331/* speaker amplifier */
 332static const char *alc5632_amp_names[] = {"AB Amp", "D Amp"};
 333static SOC_ENUM_SINGLE_DECL(alc5632_amp_enum,
 334                            ALC5632_OUTPUT_MIXER_CTRL, 13,
 335                            alc5632_amp_names);
 336static const struct snd_kcontrol_new alc5632_amp_mux_controls =
 337        SOC_DAPM_ENUM("AB-D Amp Mux", alc5632_amp_enum);
 338
 339/* ADC output select */
 340static SOC_ENUM_SINGLE_DECL(alc5632_adcr_func_enum,
 341                            ALC5632_DAC_FUNC_SELECT, 5,
 342                            alc5632_adcr_func_sel);
 343static const struct snd_kcontrol_new alc5632_adcr_func_controls =
 344        SOC_DAPM_ENUM("ADCR Mux", alc5632_adcr_func_enum);
 345
 346/* I2S out select */
 347static SOC_ENUM_SINGLE_DECL(alc5632_i2s_out_enum,
 348                            ALC5632_I2S_OUT_CTL, 5,
 349                            alc5632_i2s_out_sel);
 350static const struct snd_kcontrol_new alc5632_i2s_out_controls =
 351        SOC_DAPM_ENUM("I2SOut Mux", alc5632_i2s_out_enum);
 352
 353static const struct snd_soc_dapm_widget alc5632_dapm_widgets[] = {
 354/* Muxes */
 355SND_SOC_DAPM_MUX("AuxOut Mux", SND_SOC_NOPM, 0, 0,
 356        &alc5632_auxout_mux_controls),
 357SND_SOC_DAPM_MUX("SpeakerOut Mux", SND_SOC_NOPM, 0, 0,
 358        &alc5632_spkout_mux_controls),
 359SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
 360        &alc5632_hpl_out_mux_controls),
 361SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
 362        &alc5632_hpr_out_mux_controls),
 363SND_SOC_DAPM_MUX("SpeakerOut N Mux", SND_SOC_NOPM, 0, 0,
 364        &alc5632_spkoutn_mux_controls),
 365SND_SOC_DAPM_MUX("ADCR Mux", SND_SOC_NOPM, 0, 0,
 366        &alc5632_adcr_func_controls),
 367SND_SOC_DAPM_MUX("I2SOut Mux", ALC5632_PWR_MANAG_ADD1, 11, 0,
 368        &alc5632_i2s_out_controls),
 369
 370/* output mixers */
 371SND_SOC_DAPM_MIXER("HP Mix", SND_SOC_NOPM, 0, 0,
 372        &alc5632_hp_mixer_controls[0],
 373        ARRAY_SIZE(alc5632_hp_mixer_controls)),
 374SND_SOC_DAPM_MIXER("HPR Mix", ALC5632_PWR_MANAG_ADD2, 4, 0,
 375        &alc5632_hpr_mixer_controls[0],
 376        ARRAY_SIZE(alc5632_hpr_mixer_controls)),
 377SND_SOC_DAPM_MIXER("HPL Mix", ALC5632_PWR_MANAG_ADD2, 5, 0,
 378        &alc5632_hpl_mixer_controls[0],
 379        ARRAY_SIZE(alc5632_hpl_mixer_controls)),
 380SND_SOC_DAPM_MIXER("HPOut Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
 381SND_SOC_DAPM_MIXER("Mono Mix", ALC5632_PWR_MANAG_ADD2, 2, 0,
 382        &alc5632_mono_mixer_controls[0],
 383        ARRAY_SIZE(alc5632_mono_mixer_controls)),
 384SND_SOC_DAPM_MIXER("Speaker Mix", ALC5632_PWR_MANAG_ADD2, 3, 0,
 385        &alc5632_speaker_mixer_controls[0],
 386        ARRAY_SIZE(alc5632_speaker_mixer_controls)),
 387SND_SOC_DAPM_MIXER("DMICL Mix", SND_SOC_NOPM, 0, 0,
 388        &alc5632_dmicl_mixer_controls[0],
 389        ARRAY_SIZE(alc5632_dmicl_mixer_controls)),
 390SND_SOC_DAPM_MIXER("DMICR Mix", SND_SOC_NOPM, 0, 0,
 391        &alc5632_dmicr_mixer_controls[0],
 392        ARRAY_SIZE(alc5632_dmicr_mixer_controls)),
 393
 394/* input mixers */
 395SND_SOC_DAPM_MIXER("Left Capture Mix", ALC5632_PWR_MANAG_ADD2, 1, 0,
 396        &alc5632_captureL_mixer_controls[0],
 397        ARRAY_SIZE(alc5632_captureL_mixer_controls)),
 398SND_SOC_DAPM_MIXER("Right Capture Mix", ALC5632_PWR_MANAG_ADD2, 0, 0,
 399        &alc5632_captureR_mixer_controls[0],
 400        ARRAY_SIZE(alc5632_captureR_mixer_controls)),
 401
 402SND_SOC_DAPM_AIF_IN("AIFRXL", "Left HiFi Playback", 0, SND_SOC_NOPM, 0, 0),
 403SND_SOC_DAPM_AIF_IN("AIFRXR", "Right HiFi Playback", 0, SND_SOC_NOPM, 0, 0),
 404SND_SOC_DAPM_AIF_OUT("AIFTXL", "Left HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
 405SND_SOC_DAPM_AIF_OUT("AIFTXR", "Right HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
 406SND_SOC_DAPM_AIF_IN("VAIFRX", "Voice Playback", 0, SND_SOC_NOPM, 0, 0),
 407SND_SOC_DAPM_AIF_OUT("VAIFTX", "Voice Capture", 0, SND_SOC_NOPM, 0, 0),
 408
 409SND_SOC_DAPM_DAC("Voice DAC", NULL, ALC5632_PWR_MANAG_ADD2, 10, 0),
 410SND_SOC_DAPM_DAC("Left DAC", NULL, ALC5632_PWR_MANAG_ADD2, 9, 0),
 411SND_SOC_DAPM_DAC("Right DAC", NULL, ALC5632_PWR_MANAG_ADD2, 8, 0),
 412SND_SOC_DAPM_ADC("Left ADC", NULL, ALC5632_PWR_MANAG_ADD2, 7, 0),
 413SND_SOC_DAPM_ADC("Right ADC", NULL, ALC5632_PWR_MANAG_ADD2, 6, 0),
 414
 415SND_SOC_DAPM_MIXER("DAC Left Channel", ALC5632_PWR_MANAG_ADD1, 15, 0, NULL, 0),
 416SND_SOC_DAPM_MIXER("DAC Right Channel",
 417        ALC5632_PWR_MANAG_ADD1, 14, 0, NULL, 0),
 418SND_SOC_DAPM_MIXER("I2S Mix", ALC5632_PWR_MANAG_ADD1, 11, 0, NULL, 0),
 419SND_SOC_DAPM_MIXER("Phone Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
 420SND_SOC_DAPM_MIXER("Line Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
 421SND_SOC_DAPM_MIXER("Voice Mix", SND_SOC_NOPM, 0, 0, NULL, 0),
 422SND_SOC_DAPM_MIXER("ADCLR", SND_SOC_NOPM, 0, 0, NULL, 0),
 423
 424SND_SOC_DAPM_PGA("Left Headphone", ALC5632_PWR_MANAG_ADD3, 11, 0, NULL, 0),
 425SND_SOC_DAPM_PGA("Right Headphone", ALC5632_PWR_MANAG_ADD3, 10, 0, NULL, 0),
 426SND_SOC_DAPM_PGA("Left Speaker", ALC5632_PWR_MANAG_ADD3, 13, 0, NULL, 0),
 427SND_SOC_DAPM_PGA("Right Speaker", ALC5632_PWR_MANAG_ADD3, 12, 0, NULL, 0),
 428SND_SOC_DAPM_PGA("Aux Out", ALC5632_PWR_MANAG_ADD3, 14, 0, NULL, 0),
 429SND_SOC_DAPM_PGA("Left LineIn", ALC5632_PWR_MANAG_ADD3, 7, 0, NULL, 0),
 430SND_SOC_DAPM_PGA("Right LineIn", ALC5632_PWR_MANAG_ADD3, 6, 0, NULL, 0),
 431SND_SOC_DAPM_PGA("Phone", ALC5632_PWR_MANAG_ADD3, 5, 0, NULL, 0),
 432SND_SOC_DAPM_PGA("Phone ADMix", ALC5632_PWR_MANAG_ADD3, 4, 0, NULL, 0),
 433SND_SOC_DAPM_PGA("MIC1 PGA", ALC5632_PWR_MANAG_ADD3, 3, 0, NULL, 0),
 434SND_SOC_DAPM_PGA("MIC2 PGA", ALC5632_PWR_MANAG_ADD3, 2, 0, NULL, 0),
 435SND_SOC_DAPM_PGA("MIC1 Pre Amp", ALC5632_PWR_MANAG_ADD3, 1, 0, NULL, 0),
 436SND_SOC_DAPM_PGA("MIC2 Pre Amp", ALC5632_PWR_MANAG_ADD3, 0, 0, NULL, 0),
 437SND_SOC_DAPM_SUPPLY("MICBIAS1", ALC5632_PWR_MANAG_ADD1, 3, 0, NULL, 0),
 438SND_SOC_DAPM_SUPPLY("MICBIAS2", ALC5632_PWR_MANAG_ADD1, 2, 0, NULL, 0),
 439
 440SND_SOC_DAPM_PGA_E("D Amp", ALC5632_PWR_MANAG_ADD2, 14, 0, NULL, 0,
 441        amp_mixer_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
 442SND_SOC_DAPM_PGA("AB Amp", ALC5632_PWR_MANAG_ADD2, 15, 0, NULL, 0),
 443SND_SOC_DAPM_MUX("AB-D Amp Mux", ALC5632_PWR_MANAG_ADD1, 10, 0,
 444        &alc5632_amp_mux_controls),
 445
 446SND_SOC_DAPM_OUTPUT("AUXOUT"),
 447SND_SOC_DAPM_OUTPUT("HPL"),
 448SND_SOC_DAPM_OUTPUT("HPR"),
 449SND_SOC_DAPM_OUTPUT("SPKOUT"),
 450SND_SOC_DAPM_OUTPUT("SPKOUTN"),
 451
 452SND_SOC_DAPM_INPUT("LINEINL"),
 453SND_SOC_DAPM_INPUT("LINEINR"),
 454SND_SOC_DAPM_INPUT("PHONEP"),
 455SND_SOC_DAPM_INPUT("PHONEN"),
 456SND_SOC_DAPM_INPUT("DMICDAT"),
 457SND_SOC_DAPM_INPUT("MIC1"),
 458SND_SOC_DAPM_INPUT("MIC2"),
 459SND_SOC_DAPM_VMID("Vmid"),
 460};
 461
 462
 463static const struct snd_soc_dapm_route alc5632_dapm_routes[] = {
 464        /* Playback streams */
 465        {"Left DAC", NULL, "AIFRXL"},
 466        {"Right DAC", NULL, "AIFRXR"},
 467
 468        /* virtual mixer - mixes left & right channels */
 469        {"I2S Mix",     NULL,   "Left DAC"},
 470        {"I2S Mix",     NULL,   "Right DAC"},
 471        {"Line Mix",    NULL,   "Right LineIn"},
 472        {"Line Mix",    NULL,   "Left LineIn"},
 473        {"Phone Mix",   NULL,   "Phone"},
 474        {"Phone Mix",   NULL,   "Phone ADMix"},
 475        {"AUXOUT",              NULL,   "Aux Out"},
 476
 477        /* DAC */
 478        {"DAC Right Channel",   NULL,   "I2S Mix"},
 479        {"DAC Left Channel",    NULL,   "I2S Mix"},
 480
 481        /* HP mixer */
 482        {"HPL Mix",     "ADC2HP_L Playback Switch",     "Left Capture Mix"},
 483        {"HPL Mix", NULL,                                       "HP Mix"},
 484        {"HPR Mix", "ADC2HP_R Playback Switch", "Right Capture Mix"},
 485        {"HPR Mix", NULL,                                       "HP Mix"},
 486        {"HP Mix",      "LI2HP Playback Switch",        "Line Mix"},
 487        {"HP Mix",      "PHONE2HP Playback Switch",     "Phone Mix"},
 488        {"HP Mix",      "MIC12HP Playback Switch",      "MIC1 PGA"},
 489        {"HP Mix",      "MIC22HP Playback Switch",      "MIC2 PGA"},
 490        {"HP Mix", "VOICE2HP Playback Switch",  "Voice Mix"},
 491        {"HPR Mix", "DACR2HP Playback Switch",  "DAC Right Channel"},
 492        {"HPL Mix", "DACL2HP Playback Switch",  "DAC Left Channel"},
 493        {"HPOut Mix", NULL, "HP Mix"},
 494        {"HPOut Mix", NULL, "HPR Mix"},
 495        {"HPOut Mix", NULL, "HPL Mix"},
 496
 497        /* speaker mixer */
 498        {"Speaker Mix", "LI2SPK Playback Switch",       "Line Mix"},
 499        {"Speaker Mix", "PHONE2SPK Playback Switch", "Phone Mix"},
 500        {"Speaker Mix", "MIC12SPK Playback Switch",     "MIC1 PGA"},
 501        {"Speaker Mix", "MIC22SPK Playback Switch",     "MIC2 PGA"},
 502        {"Speaker Mix", "DAC2SPK Playback Switch",      "DAC Left Channel"},
 503        {"Speaker Mix", "VOICE2SPK Playback Switch",    "Voice Mix"},
 504
 505        /* mono mixer */
 506        {"Mono Mix", "ADC2MONO_L Playback Switch",      "Left Capture Mix"},
 507        {"Mono Mix", "ADC2MONO_R Playback Switch",      "Right Capture Mix"},
 508        {"Mono Mix", "LI2MONO Playback Switch",         "Line Mix"},
 509        {"Mono Mix", "MIC12MONO Playback Switch",       "MIC1 PGA"},
 510        {"Mono Mix", "MIC22MONO Playback Switch",       "MIC2 PGA"},
 511        {"Mono Mix", "DAC2MONO Playback Switch",        "DAC Left Channel"},
 512        {"Mono Mix", "VOICE2MONO Playback Switch",      "Voice Mix"},
 513
 514        /* Left record mixer */
 515        {"Left Capture Mix", "LIL2REC Capture Switch", "LINEINL"},
 516        {"Left Capture Mix", "PH2REC_L Capture Switch", "PHONEN"},
 517        {"Left Capture Mix", "MIC12REC_L Capture Switch", "MIC1 Pre Amp"},
 518        {"Left Capture Mix", "MIC22REC_L Capture Switch", "MIC2 Pre Amp"},
 519        {"Left Capture Mix", "HPL2REC Capture Switch", "HPL Mix"},
 520        {"Left Capture Mix", "SPK2REC_L Capture Switch", "Speaker Mix"},
 521        {"Left Capture Mix", "MONO2REC_L Capture Switch", "Mono Mix"},
 522
 523        /*Right record mixer */
 524        {"Right Capture Mix", "LIR2REC Capture Switch", "LINEINR"},
 525        {"Right Capture Mix", "PH2REC_R Capture Switch", "PHONEP"},
 526        {"Right Capture Mix", "MIC12REC_R Capture Switch", "MIC1 Pre Amp"},
 527        {"Right Capture Mix", "MIC22REC_R Capture Switch", "MIC2 Pre Amp"},
 528        {"Right Capture Mix", "HPR2REC Capture Switch", "HPR Mix"},
 529        {"Right Capture Mix", "SPK2REC_R Capture Switch", "Speaker Mix"},
 530        {"Right Capture Mix", "MONO2REC_R Capture Switch", "Mono Mix"},
 531
 532        /* headphone left mux */
 533        {"Left Headphone Mux", "HP Left Mix",           "HPL Mix"},
 534        {"Left Headphone Mux", "Vmid",                  "Vmid"},
 535
 536        /* headphone right mux */
 537        {"Right Headphone Mux", "HP Right Mix",         "HPR Mix"},
 538        {"Right Headphone Mux", "Vmid",                 "Vmid"},
 539
 540        /* speaker out mux */
 541        {"SpeakerOut Mux", "Vmid",                      "Vmid"},
 542        {"SpeakerOut Mux", "HPOut Mix",                 "HPOut Mix"},
 543        {"SpeakerOut Mux", "Speaker Mix",               "Speaker Mix"},
 544        {"SpeakerOut Mux", "Mono Mix",                  "Mono Mix"},
 545
 546        /* Mono/Aux Out mux */
 547        {"AuxOut Mux", "Vmid",                          "Vmid"},
 548        {"AuxOut Mux", "HPOut Mix",                     "HPOut Mix"},
 549        {"AuxOut Mux", "Speaker Mix",                   "Speaker Mix"},
 550        {"AuxOut Mux", "Mono Mix",                      "Mono Mix"},
 551
 552        /* output pga */
 553        {"HPL", NULL,                                   "Left Headphone"},
 554        {"Left Headphone", NULL,                        "Left Headphone Mux"},
 555        {"HPR", NULL,                                   "Right Headphone"},
 556        {"Right Headphone", NULL,                       "Right Headphone Mux"},
 557        {"Aux Out", NULL,                               "AuxOut Mux"},
 558
 559        /* input pga */
 560        {"Left LineIn", NULL,                           "LINEINL"},
 561        {"Right LineIn", NULL,                          "LINEINR"},
 562        {"Phone", NULL,                         "PHONEP"},
 563        {"MIC1 Pre Amp", NULL,                          "MIC1"},
 564        {"MIC2 Pre Amp", NULL,                          "MIC2"},
 565        {"MIC1 PGA", NULL,                              "MIC1 Pre Amp"},
 566        {"MIC2 PGA", NULL,                              "MIC2 Pre Amp"},
 567
 568        /* left ADC */
 569        {"Left ADC", NULL,                              "Left Capture Mix"},
 570        {"DMICL Mix", "DMICL2ADC Capture Switch", "DMICDAT"},
 571        {"Left ADC", NULL,                              "DMICL Mix"},
 572        {"ADCLR", NULL,                                 "Left ADC"},
 573
 574        /* right ADC */
 575        {"Right ADC", NULL, "Right Capture Mix"},
 576        {"DMICR Mix", "DMICR2ADC Capture Switch", "DMICDAT"},
 577        {"Right ADC", NULL, "DMICR Mix"},
 578        {"ADCR Mux", "Stereo ADC", "Right ADC"},
 579        {"ADCR Mux", "Voice ADC", "Right ADC"},
 580        {"ADCLR", NULL, "ADCR Mux"},
 581        {"VAIFTX", NULL, "ADCR Mux"},
 582
 583        /* Digital I2S out */
 584        {"I2SOut Mux", "ADC LR", "ADCLR"},
 585        {"I2SOut Mux", "Voice Stereo Digital", "VAIFRX"},
 586        {"AIFTXL", NULL, "I2SOut Mux"},
 587        {"AIFTXR", NULL, "I2SOut Mux"},
 588
 589        /* Voice Mix */
 590        {"Voice DAC", NULL, "VAIFRX"},
 591        {"Voice Mix", NULL, "Voice DAC"},
 592
 593        /* Speaker Output */
 594        {"SpeakerOut N Mux", "RN/-R",                   "Left Speaker"},
 595        {"SpeakerOut N Mux", "RP/+R",                   "Left Speaker"},
 596        {"SpeakerOut N Mux", "LN/-R",                   "Left Speaker"},
 597        {"SpeakerOut N Mux", "Mute",                    "Vmid"},
 598
 599        {"SpeakerOut N Mux", "RN/-R",                   "Right Speaker"},
 600        {"SpeakerOut N Mux", "RP/+R",                   "Right Speaker"},
 601        {"SpeakerOut N Mux", "LN/-R",                   "Right Speaker"},
 602        {"SpeakerOut N Mux", "Mute",                    "Vmid"},
 603
 604        {"AB Amp", NULL,                                "SpeakerOut Mux"},
 605        {"D Amp", NULL,                                 "SpeakerOut Mux"},
 606        {"AB-D Amp Mux", "AB Amp",                      "AB Amp"},
 607        {"AB-D Amp Mux", "D Amp",                       "D Amp"},
 608        {"Left Speaker", NULL,                          "AB-D Amp Mux"},
 609        {"Right Speaker", NULL,                         "AB-D Amp Mux"},
 610
 611        {"SPKOUT", NULL,                                "Left Speaker"},
 612        {"SPKOUT", NULL,                                "Right Speaker"},
 613
 614        {"SPKOUTN", NULL,                               "SpeakerOut N Mux"},
 615
 616};
 617
 618/* PLL divisors */
 619struct _pll_div {
 620        u32 pll_in;
 621        u32 pll_out;
 622        u16 regvalue;
 623};
 624
 625/* Note : pll code from original alc5632 driver. Not sure of how good it is */
 626/* useful only for master mode */
 627static const struct _pll_div codec_master_pll_div[] = {
 628
 629        {  2048000,  8192000,   0x0ea0},
 630        {  3686400,  8192000,   0x4e27},
 631        { 12000000,  8192000,   0x456b},
 632        { 13000000,  8192000,   0x495f},
 633        { 13100000,  8192000,   0x0320},
 634        {  2048000,  11289600,  0xf637},
 635        {  3686400,  11289600,  0x2f22},
 636        { 12000000,  11289600,  0x3e2f},
 637        { 13000000,  11289600,  0x4d5b},
 638        { 13100000,  11289600,  0x363b},
 639        {  2048000,  16384000,  0x1ea0},
 640        {  3686400,  16384000,  0x9e27},
 641        { 12000000,  16384000,  0x452b},
 642        { 13000000,  16384000,  0x542f},
 643        { 13100000,  16384000,  0x03a0},
 644        {  2048000,  16934400,  0xe625},
 645        {  3686400,  16934400,  0x9126},
 646        { 12000000,  16934400,  0x4d2c},
 647        { 13000000,  16934400,  0x742f},
 648        { 13100000,  16934400,  0x3c27},
 649        {  2048000,  22579200,  0x2aa0},
 650        {  3686400,  22579200,  0x2f20},
 651        { 12000000,  22579200,  0x7e2f},
 652        { 13000000,  22579200,  0x742f},
 653        { 13100000,  22579200,  0x3c27},
 654        {  2048000,  24576000,  0x2ea0},
 655        {  3686400,  24576000,  0xee27},
 656        { 12000000,  24576000,  0x2915},
 657        { 13000000,  24576000,  0x772e},
 658        { 13100000,  24576000,  0x0d20},
 659};
 660
 661/* FOUT = MCLK*(N+2)/((M+2)*(K+2))
 662   N: bit 15:8 (div 2 .. div 257)
 663   K: bit  6:4 typical 2
 664   M: bit  3:0 (div 2 .. div 17)
 665
 666   same as for 5623 - thanks!
 667*/
 668
 669static const struct _pll_div codec_slave_pll_div[] = {
 670
 671        {  1024000,  16384000,  0x3ea0},
 672        {  1411200,  22579200,  0x3ea0},
 673        {  1536000,  24576000,  0x3ea0},
 674        {  2048000,  16384000,  0x1ea0},
 675        {  2822400,  22579200,  0x1ea0},
 676        {  3072000,  24576000,  0x1ea0},
 677
 678};
 679
 680static int alc5632_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
 681                int source, unsigned int freq_in, unsigned int freq_out)
 682{
 683        int i;
 684        struct snd_soc_codec *codec = codec_dai->codec;
 685        int gbl_clk = 0, pll_div = 0;
 686        u16 reg;
 687
 688        if (pll_id < ALC5632_PLL_FR_MCLK || pll_id > ALC5632_PLL_FR_VBCLK)
 689                return -EINVAL;
 690
 691        /* Disable PLL power */
 692        snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
 693                                ALC5632_PWR_ADD2_PLL1,
 694                                0);
 695        snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
 696                                ALC5632_PWR_ADD2_PLL2,
 697                                0);
 698
 699        /* pll is not used in slave mode */
 700        reg = snd_soc_read(codec, ALC5632_DAI_CONTROL);
 701        if (reg & ALC5632_DAI_SDP_SLAVE_MODE)
 702                return 0;
 703
 704        if (!freq_in || !freq_out)
 705                return 0;
 706
 707        switch (pll_id) {
 708        case ALC5632_PLL_FR_MCLK:
 709                for (i = 0; i < ARRAY_SIZE(codec_master_pll_div); i++) {
 710                        if (codec_master_pll_div[i].pll_in == freq_in
 711                           && codec_master_pll_div[i].pll_out == freq_out) {
 712                                /* PLL source from MCLK */
 713                                pll_div  = codec_master_pll_div[i].regvalue;
 714                                break;
 715                        }
 716                }
 717                break;
 718        case ALC5632_PLL_FR_BCLK:
 719                for (i = 0; i < ARRAY_SIZE(codec_slave_pll_div); i++) {
 720                        if (codec_slave_pll_div[i].pll_in == freq_in
 721                           && codec_slave_pll_div[i].pll_out == freq_out) {
 722                                /* PLL source from Bitclk */
 723                                gbl_clk = ALC5632_PLL_FR_BCLK;
 724                                pll_div = codec_slave_pll_div[i].regvalue;
 725                                break;
 726                        }
 727                }
 728                break;
 729        case ALC5632_PLL_FR_VBCLK:
 730                for (i = 0; i < ARRAY_SIZE(codec_slave_pll_div); i++) {
 731                        if (codec_slave_pll_div[i].pll_in == freq_in
 732                           && codec_slave_pll_div[i].pll_out == freq_out) {
 733                                /* PLL source from voice clock */
 734                                gbl_clk = ALC5632_PLL_FR_VBCLK;
 735                                pll_div = codec_slave_pll_div[i].regvalue;
 736                                break;
 737                        }
 738                }
 739                break;
 740        default:
 741                return -EINVAL;
 742        }
 743
 744        if (!pll_div)
 745                return -EINVAL;
 746
 747        /* choose MCLK/BCLK/VBCLK */
 748        snd_soc_write(codec, ALC5632_GPCR2, gbl_clk);
 749        /* choose PLL1 clock rate */
 750        snd_soc_write(codec, ALC5632_PLL1_CTRL, pll_div);
 751        /* enable PLL1 */
 752        snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
 753                                ALC5632_PWR_ADD2_PLL1,
 754                                ALC5632_PWR_ADD2_PLL1);
 755        /* enable PLL2 */
 756        snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
 757                                ALC5632_PWR_ADD2_PLL2,
 758                                ALC5632_PWR_ADD2_PLL2);
 759        /* use PLL1 as main SYSCLK */
 760        snd_soc_update_bits(codec, ALC5632_GPCR1,
 761                        ALC5632_GPCR1_CLK_SYS_SRC_SEL_PLL1,
 762                        ALC5632_GPCR1_CLK_SYS_SRC_SEL_PLL1);
 763
 764        return 0;
 765}
 766
 767struct _coeff_div {
 768        u16 fs;
 769        u16 regvalue;
 770};
 771
 772/* codec hifi mclk (after PLL) clock divider coefficients */
 773/* values inspired from column BCLK=32Fs of Appendix A table */
 774static const struct _coeff_div coeff_div[] = {
 775        {512*1, 0x3075},
 776};
 777
 778static int get_coeff(struct snd_soc_codec *codec, int rate)
 779{
 780        struct alc5632_priv *alc5632 = snd_soc_codec_get_drvdata(codec);
 781        int i;
 782
 783        for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
 784                if (coeff_div[i].fs * rate == alc5632->sysclk)
 785                        return i;
 786        }
 787        return -EINVAL;
 788}
 789
 790/*
 791 * Clock after PLL and dividers
 792 */
 793static int alc5632_set_dai_sysclk(struct snd_soc_dai *codec_dai,
 794                int clk_id, unsigned int freq, int dir)
 795{
 796        struct snd_soc_codec *codec = codec_dai->codec;
 797        struct alc5632_priv *alc5632 = snd_soc_codec_get_drvdata(codec);
 798
 799        switch (freq) {
 800        case  4096000:
 801        case  8192000:
 802        case 11289600:
 803        case 12288000:
 804        case 16384000:
 805        case 16934400:
 806        case 18432000:
 807        case 22579200:
 808        case 24576000:
 809                alc5632->sysclk = freq;
 810                return 0;
 811        }
 812        return -EINVAL;
 813}
 814
 815static int alc5632_set_dai_fmt(struct snd_soc_dai *codec_dai,
 816                unsigned int fmt)
 817{
 818        struct snd_soc_codec *codec = codec_dai->codec;
 819        u16 iface = 0;
 820
 821        /* set master/slave audio interface */
 822        switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
 823        case SND_SOC_DAIFMT_CBM_CFM:
 824                iface = ALC5632_DAI_SDP_MASTER_MODE;
 825                break;
 826        case SND_SOC_DAIFMT_CBS_CFS:
 827                iface = ALC5632_DAI_SDP_SLAVE_MODE;
 828                break;
 829        default:
 830                return -EINVAL;
 831        }
 832
 833        /* interface format */
 834        switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
 835        case SND_SOC_DAIFMT_I2S:
 836                iface |= ALC5632_DAI_I2S_DF_I2S;
 837                break;
 838        case SND_SOC_DAIFMT_LEFT_J:
 839                iface |= ALC5632_DAI_I2S_DF_LEFT;
 840                break;
 841        case SND_SOC_DAIFMT_DSP_A:
 842                iface |= ALC5632_DAI_I2S_DF_PCM_A;
 843                break;
 844        case SND_SOC_DAIFMT_DSP_B:
 845                iface |= ALC5632_DAI_I2S_DF_PCM_B;
 846                break;
 847        default:
 848                return -EINVAL;
 849        }
 850
 851        /* clock inversion */
 852        switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
 853        case SND_SOC_DAIFMT_NB_NF:
 854                break;
 855        case SND_SOC_DAIFMT_IB_IF:
 856                iface |= ALC5632_DAI_MAIN_I2S_BCLK_POL_CTRL;
 857                break;
 858        case SND_SOC_DAIFMT_IB_NF:
 859                iface |= ALC5632_DAI_MAIN_I2S_BCLK_POL_CTRL;
 860                break;
 861        case SND_SOC_DAIFMT_NB_IF:
 862                break;
 863        default:
 864                return -EINVAL;
 865        }
 866
 867        return snd_soc_write(codec, ALC5632_DAI_CONTROL, iface);
 868}
 869
 870static int alc5632_pcm_hw_params(struct snd_pcm_substream *substream,
 871                struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
 872{
 873        struct snd_soc_codec *codec = dai->codec;
 874        int coeff, rate;
 875        u16 iface;
 876
 877        iface = snd_soc_read(codec, ALC5632_DAI_CONTROL);
 878        iface &= ~ALC5632_DAI_I2S_DL_MASK;
 879
 880        /* bit size */
 881        switch (params_width(params)) {
 882        case 16:
 883                iface |= ALC5632_DAI_I2S_DL_16;
 884                break;
 885        case 20:
 886                iface |= ALC5632_DAI_I2S_DL_20;
 887                break;
 888        case 24:
 889                iface |= ALC5632_DAI_I2S_DL_24;
 890                break;
 891        default:
 892                return -EINVAL;
 893        }
 894
 895        /* set iface & srate */
 896        snd_soc_write(codec, ALC5632_DAI_CONTROL, iface);
 897        rate = params_rate(params);
 898        coeff = get_coeff(codec, rate);
 899        if (coeff < 0)
 900                return -EINVAL;
 901
 902        coeff = coeff_div[coeff].regvalue;
 903        snd_soc_write(codec, ALC5632_DAC_CLK_CTRL1, coeff);
 904
 905        return 0;
 906}
 907
 908static int alc5632_mute(struct snd_soc_dai *dai, int mute)
 909{
 910        struct snd_soc_codec *codec = dai->codec;
 911        u16 hp_mute = ALC5632_MISC_HP_DEPOP_MUTE_L
 912                                                |ALC5632_MISC_HP_DEPOP_MUTE_R;
 913        u16 mute_reg = snd_soc_read(codec, ALC5632_MISC_CTRL) & ~hp_mute;
 914
 915        if (mute)
 916                mute_reg |= hp_mute;
 917
 918        return snd_soc_write(codec, ALC5632_MISC_CTRL, mute_reg);
 919}
 920
 921#define ALC5632_ADD2_POWER_EN (ALC5632_PWR_ADD2_VREF)
 922
 923#define ALC5632_ADD3_POWER_EN (ALC5632_PWR_ADD3_MIC1_BOOST_AD)
 924
 925#define ALC5632_ADD1_POWER_EN \
 926                (ALC5632_PWR_ADD1_DAC_REF \
 927                | ALC5632_PWR_ADD1_SOFTGEN_EN \
 928                | ALC5632_PWR_ADD1_HP_OUT_AMP \
 929                | ALC5632_PWR_ADD1_HP_OUT_ENH_AMP \
 930                | ALC5632_PWR_ADD1_MAIN_BIAS)
 931
 932static void enable_power_depop(struct snd_soc_codec *codec)
 933{
 934        snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD1,
 935                                ALC5632_PWR_ADD1_SOFTGEN_EN,
 936                                ALC5632_PWR_ADD1_SOFTGEN_EN);
 937
 938        snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD3,
 939                                ALC5632_ADD3_POWER_EN,
 940                                ALC5632_ADD3_POWER_EN);
 941
 942        snd_soc_update_bits(codec, ALC5632_MISC_CTRL,
 943                                ALC5632_MISC_HP_DEPOP_MODE2_EN,
 944                                ALC5632_MISC_HP_DEPOP_MODE2_EN);
 945
 946        /* "normal" mode: 0 @ 26 */
 947        /* set all PR0-7 mixers to 0 */
 948        snd_soc_update_bits(codec, ALC5632_PWR_DOWN_CTRL_STATUS,
 949                                ALC5632_PWR_DOWN_CTRL_STATUS_MASK,
 950                                0);
 951
 952        msleep(500);
 953
 954        snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
 955                                ALC5632_ADD2_POWER_EN,
 956                                ALC5632_ADD2_POWER_EN);
 957
 958        snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD1,
 959                                ALC5632_ADD1_POWER_EN,
 960                                ALC5632_ADD1_POWER_EN);
 961
 962        /* disable HP Depop2 */
 963        snd_soc_update_bits(codec, ALC5632_MISC_CTRL,
 964                                ALC5632_MISC_HP_DEPOP_MODE2_EN,
 965                                0);
 966
 967}
 968
 969static int alc5632_set_bias_level(struct snd_soc_codec *codec,
 970                                      enum snd_soc_bias_level level)
 971{
 972        switch (level) {
 973        case SND_SOC_BIAS_ON:
 974                enable_power_depop(codec);
 975                break;
 976        case SND_SOC_BIAS_PREPARE:
 977                break;
 978        case SND_SOC_BIAS_STANDBY:
 979                /* everything off except vref/vmid, */
 980                snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD1,
 981                                ALC5632_PWR_MANAG_ADD1_MASK,
 982                                ALC5632_PWR_ADD1_MAIN_BIAS);
 983                snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
 984                                ALC5632_PWR_MANAG_ADD2_MASK,
 985                                ALC5632_PWR_ADD2_VREF);
 986                /* "normal" mode: 0 @ 26 */
 987                snd_soc_update_bits(codec, ALC5632_PWR_DOWN_CTRL_STATUS,
 988                                ALC5632_PWR_DOWN_CTRL_STATUS_MASK,
 989                                0xffff ^ (ALC5632_PWR_VREF_PR3
 990                                | ALC5632_PWR_VREF_PR2));
 991                break;
 992        case SND_SOC_BIAS_OFF:
 993                /* everything off, dac mute, inactive */
 994                snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD2,
 995                                ALC5632_PWR_MANAG_ADD2_MASK, 0);
 996                snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD3,
 997                                ALC5632_PWR_MANAG_ADD3_MASK, 0);
 998                snd_soc_update_bits(codec, ALC5632_PWR_MANAG_ADD1,
 999                                ALC5632_PWR_MANAG_ADD1_MASK, 0);
1000                break;
1001        }
1002        return 0;
1003}
1004
1005#define ALC5632_FORMATS (SNDRV_PCM_FMTBIT_S16_LE \
1006                        | SNDRV_PCM_FMTBIT_S24_LE \
1007                        | SNDRV_PCM_FMTBIT_S32_LE)
1008
1009static const struct snd_soc_dai_ops alc5632_dai_ops = {
1010                .hw_params = alc5632_pcm_hw_params,
1011                .digital_mute = alc5632_mute,
1012                .set_fmt = alc5632_set_dai_fmt,
1013                .set_sysclk = alc5632_set_dai_sysclk,
1014                .set_pll = alc5632_set_dai_pll,
1015};
1016
1017static struct snd_soc_dai_driver alc5632_dai = {
1018        .name = "alc5632-hifi",
1019        .playback = {
1020                .stream_name = "HiFi Playback",
1021                .channels_min = 1,
1022                .channels_max = 2,
1023                .rate_min =     8000,
1024                .rate_max =     48000,
1025                .rates = SNDRV_PCM_RATE_8000_48000,
1026                .formats = ALC5632_FORMATS,},
1027        .capture = {
1028                .stream_name = "HiFi Capture",
1029                .channels_min = 1,
1030                .channels_max = 2,
1031                .rate_min =     8000,
1032                .rate_max =     48000,
1033                .rates = SNDRV_PCM_RATE_8000_48000,
1034                .formats = ALC5632_FORMATS,},
1035
1036        .ops = &alc5632_dai_ops,
1037        .symmetric_rates = 1,
1038};
1039
1040#ifdef CONFIG_PM
1041static int alc5632_resume(struct snd_soc_codec *codec)
1042{
1043        struct alc5632_priv *alc5632 = snd_soc_codec_get_drvdata(codec);
1044
1045        regcache_sync(alc5632->regmap);
1046
1047        return 0;
1048}
1049#else
1050#define alc5632_resume  NULL
1051#endif
1052
1053static int alc5632_probe(struct snd_soc_codec *codec)
1054{
1055        struct alc5632_priv *alc5632 = snd_soc_codec_get_drvdata(codec);
1056
1057        switch (alc5632->id) {
1058        case 0x5c:
1059                snd_soc_add_codec_controls(codec, alc5632_vol_snd_controls,
1060                        ARRAY_SIZE(alc5632_vol_snd_controls));
1061                break;
1062        default:
1063                return -EINVAL;
1064        }
1065
1066        return 0;
1067}
1068
1069static const struct snd_soc_codec_driver soc_codec_device_alc5632 = {
1070        .probe = alc5632_probe,
1071        .resume = alc5632_resume,
1072        .set_bias_level = alc5632_set_bias_level,
1073        .suspend_bias_off = true,
1074
1075        .component_driver = {
1076                .controls               = alc5632_snd_controls,
1077                .num_controls           = ARRAY_SIZE(alc5632_snd_controls),
1078                .dapm_widgets           = alc5632_dapm_widgets,
1079                .num_dapm_widgets       = ARRAY_SIZE(alc5632_dapm_widgets),
1080                .dapm_routes            = alc5632_dapm_routes,
1081                .num_dapm_routes        = ARRAY_SIZE(alc5632_dapm_routes),
1082        },
1083};
1084
1085static const struct regmap_config alc5632_regmap = {
1086        .reg_bits = 8,
1087        .val_bits = 16,
1088
1089        .max_register = ALC5632_MAX_REGISTER,
1090        .reg_defaults = alc5632_reg_defaults,
1091        .num_reg_defaults = ARRAY_SIZE(alc5632_reg_defaults),
1092        .volatile_reg = alc5632_volatile_register,
1093        .cache_type = REGCACHE_RBTREE,
1094};
1095
1096/*
1097 * alc5632 2 wire address is determined by A1 pin
1098 * state during powerup.
1099 *    low  = 0x1a
1100 *    high = 0x1b
1101 */
1102static int alc5632_i2c_probe(struct i2c_client *client,
1103                             const struct i2c_device_id *id)
1104{
1105        struct alc5632_priv *alc5632;
1106        int ret, ret1, ret2;
1107        unsigned int vid1, vid2;
1108
1109        alc5632 = devm_kzalloc(&client->dev,
1110                         sizeof(struct alc5632_priv), GFP_KERNEL);
1111        if (alc5632 == NULL)
1112                return -ENOMEM;
1113
1114        i2c_set_clientdata(client, alc5632);
1115
1116        alc5632->regmap = devm_regmap_init_i2c(client, &alc5632_regmap);
1117        if (IS_ERR(alc5632->regmap)) {
1118                ret = PTR_ERR(alc5632->regmap);
1119                dev_err(&client->dev, "regmap_init() failed: %d\n", ret);
1120                return ret;
1121        }
1122
1123        ret1 = regmap_read(alc5632->regmap, ALC5632_VENDOR_ID1, &vid1);
1124        ret2 = regmap_read(alc5632->regmap, ALC5632_VENDOR_ID2, &vid2);
1125        if (ret1 != 0 || ret2 != 0) {
1126                dev_err(&client->dev,
1127                "Failed to read chip ID: ret1=%d, ret2=%d\n", ret1, ret2);
1128                return -EIO;
1129        }
1130
1131        vid2 >>= 8;
1132
1133        if ((vid1 != 0x10EC) || (vid2 != id->driver_data)) {
1134                dev_err(&client->dev,
1135                "Device is not a ALC5632: VID1=0x%x, VID2=0x%x\n", vid1, vid2);
1136                return -EINVAL;
1137        }
1138
1139        ret = alc5632_reset(alc5632->regmap);
1140        if (ret < 0) {
1141                dev_err(&client->dev, "Failed to issue reset\n");
1142                return ret;
1143        }
1144
1145        alc5632->id = vid2;
1146        switch (alc5632->id) {
1147        case 0x5c:
1148                alc5632_dai.name = "alc5632-hifi";
1149                break;
1150        default:
1151                return -EINVAL;
1152        }
1153
1154        ret = snd_soc_register_codec(&client->dev,
1155                &soc_codec_device_alc5632, &alc5632_dai, 1);
1156
1157        if (ret < 0) {
1158                dev_err(&client->dev, "Failed to register codec: %d\n", ret);
1159                return ret;
1160        }
1161
1162        return ret;
1163}
1164
1165static int alc5632_i2c_remove(struct i2c_client *client)
1166{
1167        snd_soc_unregister_codec(&client->dev);
1168        return 0;
1169}
1170
1171static const struct i2c_device_id alc5632_i2c_table[] = {
1172        {"alc5632", 0x5c},
1173        {}
1174};
1175MODULE_DEVICE_TABLE(i2c, alc5632_i2c_table);
1176
1177static const struct of_device_id alc5632_of_match[] = {
1178        { .compatible = "realtek,alc5632", },
1179        { }
1180};
1181MODULE_DEVICE_TABLE(of, alc5632_of_match);
1182
1183/* i2c codec control layer */
1184static struct i2c_driver alc5632_i2c_driver = {
1185        .driver = {
1186                .name = "alc5632",
1187                .of_match_table = of_match_ptr(alc5632_of_match),
1188        },
1189        .probe = alc5632_i2c_probe,
1190        .remove =  alc5632_i2c_remove,
1191        .id_table = alc5632_i2c_table,
1192};
1193
1194module_i2c_driver(alc5632_i2c_driver);
1195
1196MODULE_DESCRIPTION("ASoC ALC5632 driver");
1197MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
1198MODULE_LICENSE("GPL");
1199