linux/sound/soc/codecs/rt5660.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * rt5660.c  --  RT5660 ALSA SoC audio codec driver
   4 *
   5 * Copyright 2016 Realtek Semiconductor Corp.
   6 * Author: Oder Chiou <oder_chiou@realtek.com>
   7 */
   8
   9#include <linux/module.h>
  10#include <linux/moduleparam.h>
  11#include <linux/init.h>
  12#include <linux/delay.h>
  13#include <linux/pm.h>
  14#include <linux/gpio.h>
  15#include <linux/i2c.h>
  16#include <linux/regmap.h>
  17#include <linux/of.h>
  18#include <linux/of_gpio.h>
  19#include <linux/platform_device.h>
  20#include <linux/spi/spi.h>
  21#include <linux/acpi.h>
  22#include <sound/core.h>
  23#include <sound/pcm.h>
  24#include <sound/pcm_params.h>
  25#include <sound/soc.h>
  26#include <sound/soc-dapm.h>
  27#include <sound/initval.h>
  28#include <sound/tlv.h>
  29
  30#include "rl6231.h"
  31#include "rt5660.h"
  32
  33#define RT5660_DEVICE_ID 0x6338
  34
  35#define RT5660_PR_RANGE_BASE (0xff + 1)
  36#define RT5660_PR_SPACING 0x100
  37
  38#define RT5660_PR_BASE (RT5660_PR_RANGE_BASE + (0 * RT5660_PR_SPACING))
  39
  40static const struct regmap_range_cfg rt5660_ranges[] = {
  41        { .name = "PR", .range_min = RT5660_PR_BASE,
  42          .range_max = RT5660_PR_BASE + 0xf3,
  43          .selector_reg = RT5660_PRIV_INDEX,
  44          .selector_mask = 0xff,
  45          .selector_shift = 0x0,
  46          .window_start = RT5660_PRIV_DATA,
  47          .window_len = 0x1, },
  48};
  49
  50static const struct reg_sequence rt5660_patch[] = {
  51        { RT5660_ALC_PGA_CTRL2,         0x44c3 },
  52        { RT5660_PR_BASE + 0x3d,        0x2600 },
  53};
  54
  55static const struct reg_default rt5660_reg[] = {
  56        { 0x00, 0x0000 },
  57        { 0x01, 0xc800 },
  58        { 0x02, 0xc8c8 },
  59        { 0x0d, 0x1010 },
  60        { 0x0e, 0x1010 },
  61        { 0x19, 0xafaf },
  62        { 0x1c, 0x2f2f },
  63        { 0x1e, 0x0000 },
  64        { 0x27, 0x6060 },
  65        { 0x29, 0x8080 },
  66        { 0x2a, 0x4242 },
  67        { 0x2f, 0x0000 },
  68        { 0x3b, 0x0000 },
  69        { 0x3c, 0x007f },
  70        { 0x3d, 0x0000 },
  71        { 0x3e, 0x007f },
  72        { 0x45, 0xe000 },
  73        { 0x46, 0x003e },
  74        { 0x48, 0xf800 },
  75        { 0x4a, 0x0004 },
  76        { 0x4d, 0x0000 },
  77        { 0x4e, 0x0000 },
  78        { 0x4f, 0x01ff },
  79        { 0x50, 0x0000 },
  80        { 0x51, 0x0000 },
  81        { 0x52, 0x01ff },
  82        { 0x61, 0x0000 },
  83        { 0x62, 0x0000 },
  84        { 0x63, 0x00c0 },
  85        { 0x64, 0x0000 },
  86        { 0x65, 0x0000 },
  87        { 0x66, 0x0000 },
  88        { 0x70, 0x8000 },
  89        { 0x73, 0x7000 },
  90        { 0x74, 0x3c00 },
  91        { 0x75, 0x2800 },
  92        { 0x80, 0x0000 },
  93        { 0x81, 0x0000 },
  94        { 0x82, 0x0000 },
  95        { 0x8c, 0x0228 },
  96        { 0x8d, 0xa000 },
  97        { 0x8e, 0x0000 },
  98        { 0x92, 0x0000 },
  99        { 0x93, 0x3000 },
 100        { 0xa1, 0x0059 },
 101        { 0xa2, 0x0001 },
 102        { 0xa3, 0x5c80 },
 103        { 0xa4, 0x0146 },
 104        { 0xa5, 0x1f1f },
 105        { 0xa6, 0x78c6 },
 106        { 0xa7, 0xe5ec },
 107        { 0xa8, 0xba61 },
 108        { 0xa9, 0x3c78 },
 109        { 0xaa, 0x8ae2 },
 110        { 0xab, 0xe5ec },
 111        { 0xac, 0xc600 },
 112        { 0xad, 0xba61 },
 113        { 0xae, 0x17ed },
 114        { 0xb0, 0x2080 },
 115        { 0xb1, 0x0000 },
 116        { 0xb3, 0x001f },
 117        { 0xb4, 0x020c },
 118        { 0xb5, 0x1f00 },
 119        { 0xb6, 0x0000 },
 120        { 0xb7, 0x4000 },
 121        { 0xbb, 0x0000 },
 122        { 0xbd, 0x0000 },
 123        { 0xbe, 0x0000 },
 124        { 0xbf, 0x0100 },
 125        { 0xc0, 0x0000 },
 126        { 0xc2, 0x0000 },
 127        { 0xd3, 0xa220 },
 128        { 0xd9, 0x0809 },
 129        { 0xda, 0x0000 },
 130        { 0xe0, 0x8000 },
 131        { 0xe1, 0x0200 },
 132        { 0xe2, 0x8000 },
 133        { 0xe3, 0x0200 },
 134        { 0xe4, 0x0f20 },
 135        { 0xe5, 0x001f },
 136        { 0xe6, 0x020c },
 137        { 0xe7, 0x1f00 },
 138        { 0xe8, 0x0000 },
 139        { 0xe9, 0x4000 },
 140        { 0xea, 0x00a6 },
 141        { 0xeb, 0x04c3 },
 142        { 0xec, 0x27c8 },
 143        { 0xed, 0x7418 },
 144        { 0xee, 0xbf50 },
 145        { 0xef, 0x0045 },
 146        { 0xf0, 0x0007 },
 147        { 0xfa, 0x0000 },
 148        { 0xfd, 0x0000 },
 149        { 0xfe, 0x10ec },
 150        { 0xff, 0x6338 },
 151};
 152
 153static bool rt5660_volatile_register(struct device *dev, unsigned int reg)
 154{
 155        int i;
 156
 157        for (i = 0; i < ARRAY_SIZE(rt5660_ranges); i++)
 158                if ((reg >= rt5660_ranges[i].window_start &&
 159                     reg <= rt5660_ranges[i].window_start +
 160                     rt5660_ranges[i].window_len) ||
 161                    (reg >= rt5660_ranges[i].range_min &&
 162                     reg <= rt5660_ranges[i].range_max))
 163                        return true;
 164
 165        switch (reg) {
 166        case RT5660_RESET:
 167        case RT5660_PRIV_DATA:
 168        case RT5660_EQ_CTRL1:
 169        case RT5660_IRQ_CTRL2:
 170        case RT5660_INT_IRQ_ST:
 171        case RT5660_VENDOR_ID:
 172        case RT5660_VENDOR_ID1:
 173        case RT5660_VENDOR_ID2:
 174                return true;
 175        default:
 176                return false;
 177        }
 178}
 179
 180static bool rt5660_readable_register(struct device *dev, unsigned int reg)
 181{
 182        int i;
 183
 184        for (i = 0; i < ARRAY_SIZE(rt5660_ranges); i++)
 185                if ((reg >= rt5660_ranges[i].window_start &&
 186                     reg <= rt5660_ranges[i].window_start +
 187                     rt5660_ranges[i].window_len) ||
 188                    (reg >= rt5660_ranges[i].range_min &&
 189                     reg <= rt5660_ranges[i].range_max))
 190                        return true;
 191
 192        switch (reg) {
 193        case RT5660_RESET:
 194        case RT5660_SPK_VOL:
 195        case RT5660_LOUT_VOL:
 196        case RT5660_IN1_IN2:
 197        case RT5660_IN3_IN4:
 198        case RT5660_DAC1_DIG_VOL:
 199        case RT5660_STO1_ADC_DIG_VOL:
 200        case RT5660_ADC_BST_VOL1:
 201        case RT5660_STO1_ADC_MIXER:
 202        case RT5660_AD_DA_MIXER:
 203        case RT5660_STO_DAC_MIXER:
 204        case RT5660_DIG_INF1_DATA:
 205        case RT5660_REC_L1_MIXER:
 206        case RT5660_REC_L2_MIXER:
 207        case RT5660_REC_R1_MIXER:
 208        case RT5660_REC_R2_MIXER:
 209        case RT5660_LOUT_MIXER:
 210        case RT5660_SPK_MIXER:
 211        case RT5660_SPO_MIXER:
 212        case RT5660_SPO_CLSD_RATIO:
 213        case RT5660_OUT_L_GAIN1:
 214        case RT5660_OUT_L_GAIN2:
 215        case RT5660_OUT_L1_MIXER:
 216        case RT5660_OUT_R_GAIN1:
 217        case RT5660_OUT_R_GAIN2:
 218        case RT5660_OUT_R1_MIXER:
 219        case RT5660_PWR_DIG1:
 220        case RT5660_PWR_DIG2:
 221        case RT5660_PWR_ANLG1:
 222        case RT5660_PWR_ANLG2:
 223        case RT5660_PWR_MIXER:
 224        case RT5660_PWR_VOL:
 225        case RT5660_PRIV_INDEX:
 226        case RT5660_PRIV_DATA:
 227        case RT5660_I2S1_SDP:
 228        case RT5660_ADDA_CLK1:
 229        case RT5660_ADDA_CLK2:
 230        case RT5660_DMIC_CTRL1:
 231        case RT5660_GLB_CLK:
 232        case RT5660_PLL_CTRL1:
 233        case RT5660_PLL_CTRL2:
 234        case RT5660_CLSD_AMP_OC_CTRL:
 235        case RT5660_CLSD_AMP_CTRL:
 236        case RT5660_LOUT_AMP_CTRL:
 237        case RT5660_SPK_AMP_SPKVDD:
 238        case RT5660_MICBIAS:
 239        case RT5660_CLSD_OUT_CTRL1:
 240        case RT5660_CLSD_OUT_CTRL2:
 241        case RT5660_DIPOLE_MIC_CTRL1:
 242        case RT5660_DIPOLE_MIC_CTRL2:
 243        case RT5660_DIPOLE_MIC_CTRL3:
 244        case RT5660_DIPOLE_MIC_CTRL4:
 245        case RT5660_DIPOLE_MIC_CTRL5:
 246        case RT5660_DIPOLE_MIC_CTRL6:
 247        case RT5660_DIPOLE_MIC_CTRL7:
 248        case RT5660_DIPOLE_MIC_CTRL8:
 249        case RT5660_DIPOLE_MIC_CTRL9:
 250        case RT5660_DIPOLE_MIC_CTRL10:
 251        case RT5660_DIPOLE_MIC_CTRL11:
 252        case RT5660_DIPOLE_MIC_CTRL12:
 253        case RT5660_EQ_CTRL1:
 254        case RT5660_EQ_CTRL2:
 255        case RT5660_DRC_AGC_CTRL1:
 256        case RT5660_DRC_AGC_CTRL2:
 257        case RT5660_DRC_AGC_CTRL3:
 258        case RT5660_DRC_AGC_CTRL4:
 259        case RT5660_DRC_AGC_CTRL5:
 260        case RT5660_JD_CTRL:
 261        case RT5660_IRQ_CTRL1:
 262        case RT5660_IRQ_CTRL2:
 263        case RT5660_INT_IRQ_ST:
 264        case RT5660_GPIO_CTRL1:
 265        case RT5660_GPIO_CTRL2:
 266        case RT5660_WIND_FILTER_CTRL1:
 267        case RT5660_SV_ZCD1:
 268        case RT5660_SV_ZCD2:
 269        case RT5660_DRC1_LM_CTRL1:
 270        case RT5660_DRC1_LM_CTRL2:
 271        case RT5660_DRC2_LM_CTRL1:
 272        case RT5660_DRC2_LM_CTRL2:
 273        case RT5660_MULTI_DRC_CTRL:
 274        case RT5660_DRC2_CTRL1:
 275        case RT5660_DRC2_CTRL2:
 276        case RT5660_DRC2_CTRL3:
 277        case RT5660_DRC2_CTRL4:
 278        case RT5660_DRC2_CTRL5:
 279        case RT5660_ALC_PGA_CTRL1:
 280        case RT5660_ALC_PGA_CTRL2:
 281        case RT5660_ALC_PGA_CTRL3:
 282        case RT5660_ALC_PGA_CTRL4:
 283        case RT5660_ALC_PGA_CTRL5:
 284        case RT5660_ALC_PGA_CTRL6:
 285        case RT5660_ALC_PGA_CTRL7:
 286        case RT5660_GEN_CTRL1:
 287        case RT5660_GEN_CTRL2:
 288        case RT5660_GEN_CTRL3:
 289        case RT5660_VENDOR_ID:
 290        case RT5660_VENDOR_ID1:
 291        case RT5660_VENDOR_ID2:
 292                return true;
 293        default:
 294                return false;
 295        }
 296}
 297
 298static const DECLARE_TLV_DB_SCALE(rt5660_out_vol_tlv, -4650, 150, 0);
 299static const DECLARE_TLV_DB_SCALE(rt5660_dac_vol_tlv, -6525, 75, 0);
 300static const DECLARE_TLV_DB_SCALE(rt5660_adc_vol_tlv, -1725, 75, 0);
 301static const DECLARE_TLV_DB_SCALE(rt5660_adc_bst_tlv, 0, 1200, 0);
 302static const DECLARE_TLV_DB_SCALE(rt5660_bst_tlv, -1200, 75, 0);
 303
 304static const struct snd_kcontrol_new rt5660_snd_controls[] = {
 305        /* Speaker Output Volume */
 306        SOC_SINGLE("Speaker Playback Switch", RT5660_SPK_VOL, RT5660_L_MUTE_SFT,
 307                1, 1),
 308        SOC_SINGLE_TLV("Speaker Playback Volume", RT5660_SPK_VOL,
 309                RT5660_L_VOL_SFT, 39, 1, rt5660_out_vol_tlv),
 310
 311        /* OUTPUT Control */
 312        SOC_DOUBLE("OUT Playback Switch", RT5660_LOUT_VOL, RT5660_L_MUTE_SFT,
 313                RT5660_R_MUTE_SFT, 1, 1),
 314        SOC_DOUBLE_TLV("OUT Playback Volume", RT5660_LOUT_VOL, RT5660_L_VOL_SFT,
 315                RT5660_R_VOL_SFT, 39, 1, rt5660_out_vol_tlv),
 316
 317        /* DAC Digital Volume */
 318        SOC_DOUBLE_TLV("DAC1 Playback Volume", RT5660_DAC1_DIG_VOL,
 319                RT5660_DAC_L1_VOL_SFT, RT5660_DAC_R1_VOL_SFT, 87, 0,
 320                rt5660_dac_vol_tlv),
 321
 322        /* IN1/IN2/IN3 Control */
 323        SOC_SINGLE_TLV("IN1 Boost Volume", RT5660_IN1_IN2, RT5660_BST_SFT1, 69,
 324                0, rt5660_bst_tlv),
 325        SOC_SINGLE_TLV("IN2 Boost Volume", RT5660_IN1_IN2, RT5660_BST_SFT2, 69,
 326                0, rt5660_bst_tlv),
 327        SOC_SINGLE_TLV("IN3 Boost Volume", RT5660_IN3_IN4, RT5660_BST_SFT3, 69,
 328                0, rt5660_bst_tlv),
 329
 330        /* ADC Digital Volume Control */
 331        SOC_DOUBLE("ADC Capture Switch", RT5660_STO1_ADC_DIG_VOL,
 332                RT5660_L_MUTE_SFT, RT5660_R_MUTE_SFT, 1, 1),
 333        SOC_DOUBLE_TLV("ADC Capture Volume", RT5660_STO1_ADC_DIG_VOL,
 334                RT5660_ADC_L_VOL_SFT, RT5660_ADC_R_VOL_SFT, 63, 0,
 335                rt5660_adc_vol_tlv),
 336
 337        /* ADC Boost Volume Control */
 338        SOC_DOUBLE_TLV("STO1 ADC Boost Gain Volume", RT5660_ADC_BST_VOL1,
 339                RT5660_STO1_ADC_L_BST_SFT, RT5660_STO1_ADC_R_BST_SFT, 3, 0,
 340                rt5660_adc_bst_tlv),
 341};
 342
 343/**
 344 * rt5660_set_dmic_clk - Set parameter of dmic.
 345 *
 346 * @w: DAPM widget.
 347 * @kcontrol: The kcontrol of this widget.
 348 * @event: Event id.
 349 *
 350 */
 351static int rt5660_set_dmic_clk(struct snd_soc_dapm_widget *w,
 352        struct snd_kcontrol *kcontrol, int event)
 353{
 354        struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
 355        struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
 356        int idx, rate;
 357
 358        rate = rt5660->sysclk / rl6231_get_pre_div(rt5660->regmap,
 359                RT5660_ADDA_CLK1, RT5660_I2S_PD1_SFT);
 360        idx = rl6231_calc_dmic_clk(rate);
 361        if (idx < 0)
 362                dev_err(component->dev, "Failed to set DMIC clock\n");
 363        else
 364                snd_soc_component_update_bits(component, RT5660_DMIC_CTRL1,
 365                        RT5660_DMIC_CLK_MASK, idx << RT5660_DMIC_CLK_SFT);
 366
 367        return idx;
 368}
 369
 370static int rt5660_is_sys_clk_from_pll(struct snd_soc_dapm_widget *source,
 371                         struct snd_soc_dapm_widget *sink)
 372{
 373        struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
 374        unsigned int val;
 375
 376        val = snd_soc_component_read32(component, RT5660_GLB_CLK);
 377        val &= RT5660_SCLK_SRC_MASK;
 378        if (val == RT5660_SCLK_SRC_PLL1)
 379                return 1;
 380        else
 381                return 0;
 382}
 383
 384/* Digital Mixer */
 385static const struct snd_kcontrol_new rt5660_sto1_adc_l_mix[] = {
 386        SOC_DAPM_SINGLE("ADC1 Switch", RT5660_STO1_ADC_MIXER,
 387                        RT5660_M_ADC_L1_SFT, 1, 1),
 388        SOC_DAPM_SINGLE("ADC2 Switch", RT5660_STO1_ADC_MIXER,
 389                        RT5660_M_ADC_L2_SFT, 1, 1),
 390};
 391
 392static const struct snd_kcontrol_new rt5660_sto1_adc_r_mix[] = {
 393        SOC_DAPM_SINGLE("ADC1 Switch", RT5660_STO1_ADC_MIXER,
 394                        RT5660_M_ADC_R1_SFT, 1, 1),
 395        SOC_DAPM_SINGLE("ADC2 Switch", RT5660_STO1_ADC_MIXER,
 396                        RT5660_M_ADC_R2_SFT, 1, 1),
 397};
 398
 399static const struct snd_kcontrol_new rt5660_dac_l_mix[] = {
 400        SOC_DAPM_SINGLE("Stereo ADC Switch", RT5660_AD_DA_MIXER,
 401                        RT5660_M_ADCMIX_L_SFT, 1, 1),
 402        SOC_DAPM_SINGLE("DAC1 Switch", RT5660_AD_DA_MIXER,
 403                        RT5660_M_DAC1_L_SFT, 1, 1),
 404};
 405
 406static const struct snd_kcontrol_new rt5660_dac_r_mix[] = {
 407        SOC_DAPM_SINGLE("Stereo ADC Switch", RT5660_AD_DA_MIXER,
 408                        RT5660_M_ADCMIX_R_SFT, 1, 1),
 409        SOC_DAPM_SINGLE("DAC1 Switch", RT5660_AD_DA_MIXER,
 410                        RT5660_M_DAC1_R_SFT, 1, 1),
 411};
 412
 413static const struct snd_kcontrol_new rt5660_sto_dac_l_mix[] = {
 414        SOC_DAPM_SINGLE("DAC L1 Switch", RT5660_STO_DAC_MIXER,
 415                        RT5660_M_DAC_L1_SFT, 1, 1),
 416        SOC_DAPM_SINGLE("DAC R1 Switch", RT5660_STO_DAC_MIXER,
 417                        RT5660_M_DAC_R1_STO_L_SFT, 1, 1),
 418};
 419
 420static const struct snd_kcontrol_new rt5660_sto_dac_r_mix[] = {
 421        SOC_DAPM_SINGLE("DAC R1 Switch", RT5660_STO_DAC_MIXER,
 422                        RT5660_M_DAC_R1_SFT, 1, 1),
 423        SOC_DAPM_SINGLE("DAC L1 Switch", RT5660_STO_DAC_MIXER,
 424                        RT5660_M_DAC_L1_STO_R_SFT, 1, 1),
 425};
 426
 427/* Analog Input Mixer */
 428static const struct snd_kcontrol_new rt5660_rec_l_mix[] = {
 429        SOC_DAPM_SINGLE("BST3 Switch", RT5660_REC_L2_MIXER,
 430                        RT5660_M_BST3_RM_L_SFT, 1, 1),
 431        SOC_DAPM_SINGLE("BST2 Switch", RT5660_REC_L2_MIXER,
 432                        RT5660_M_BST2_RM_L_SFT, 1, 1),
 433        SOC_DAPM_SINGLE("BST1 Switch", RT5660_REC_L2_MIXER,
 434                        RT5660_M_BST1_RM_L_SFT, 1, 1),
 435        SOC_DAPM_SINGLE("OUT MIXL Switch", RT5660_REC_L2_MIXER,
 436                        RT5660_M_OM_L_RM_L_SFT, 1, 1),
 437};
 438
 439static const struct snd_kcontrol_new rt5660_rec_r_mix[] = {
 440        SOC_DAPM_SINGLE("BST3 Switch", RT5660_REC_R2_MIXER,
 441                        RT5660_M_BST3_RM_R_SFT, 1, 1),
 442        SOC_DAPM_SINGLE("BST2 Switch", RT5660_REC_R2_MIXER,
 443                        RT5660_M_BST2_RM_R_SFT, 1, 1),
 444        SOC_DAPM_SINGLE("BST1 Switch", RT5660_REC_R2_MIXER,
 445                        RT5660_M_BST1_RM_R_SFT, 1, 1),
 446        SOC_DAPM_SINGLE("OUT MIXR Switch", RT5660_REC_R2_MIXER,
 447                        RT5660_M_OM_R_RM_R_SFT, 1, 1),
 448};
 449
 450static const struct snd_kcontrol_new rt5660_spk_mix[] = {
 451        SOC_DAPM_SINGLE("BST3 Switch", RT5660_SPK_MIXER,
 452                        RT5660_M_BST3_SM_SFT, 1, 1),
 453        SOC_DAPM_SINGLE("BST1 Switch", RT5660_SPK_MIXER,
 454                        RT5660_M_BST1_SM_SFT, 1, 1),
 455        SOC_DAPM_SINGLE("DACL Switch", RT5660_SPK_MIXER,
 456                        RT5660_M_DACL_SM_SFT, 1, 1),
 457        SOC_DAPM_SINGLE("DACR Switch", RT5660_SPK_MIXER,
 458                        RT5660_M_DACR_SM_SFT, 1, 1),
 459        SOC_DAPM_SINGLE("OUTMIXL Switch", RT5660_SPK_MIXER,
 460                        RT5660_M_OM_L_SM_SFT, 1, 1),
 461};
 462
 463static const struct snd_kcontrol_new rt5660_out_l_mix[] = {
 464        SOC_DAPM_SINGLE("BST3 Switch", RT5660_OUT_L1_MIXER,
 465                        RT5660_M_BST3_OM_L_SFT, 1, 1),
 466        SOC_DAPM_SINGLE("BST2 Switch", RT5660_OUT_L1_MIXER,
 467                        RT5660_M_BST2_OM_L_SFT, 1, 1),
 468        SOC_DAPM_SINGLE("BST1 Switch", RT5660_OUT_L1_MIXER,
 469                        RT5660_M_BST1_OM_L_SFT, 1, 1),
 470        SOC_DAPM_SINGLE("RECMIXL Switch", RT5660_OUT_L1_MIXER,
 471                        RT5660_M_RM_L_OM_L_SFT, 1, 1),
 472        SOC_DAPM_SINGLE("DACR Switch", RT5660_OUT_L1_MIXER,
 473                        RT5660_M_DAC_R_OM_L_SFT, 1, 1),
 474        SOC_DAPM_SINGLE("DACL Switch", RT5660_OUT_L1_MIXER,
 475                        RT5660_M_DAC_L_OM_L_SFT, 1, 1),
 476};
 477
 478static const struct snd_kcontrol_new rt5660_out_r_mix[] = {
 479        SOC_DAPM_SINGLE("BST2 Switch", RT5660_OUT_R1_MIXER,
 480                        RT5660_M_BST2_OM_R_SFT, 1, 1),
 481        SOC_DAPM_SINGLE("BST1 Switch", RT5660_OUT_R1_MIXER,
 482                        RT5660_M_BST1_OM_R_SFT, 1, 1),
 483        SOC_DAPM_SINGLE("RECMIXR Switch", RT5660_OUT_R1_MIXER,
 484                        RT5660_M_RM_R_OM_R_SFT, 1, 1),
 485        SOC_DAPM_SINGLE("DACR Switch", RT5660_OUT_R1_MIXER,
 486                        RT5660_M_DAC_R_OM_R_SFT, 1, 1),
 487        SOC_DAPM_SINGLE("DACL Switch", RT5660_OUT_R1_MIXER,
 488                        RT5660_M_DAC_L_OM_R_SFT, 1, 1),
 489};
 490
 491static const struct snd_kcontrol_new rt5660_spo_mix[] = {
 492        SOC_DAPM_SINGLE("DACR Switch", RT5660_SPO_MIXER,
 493                        RT5660_M_DAC_R_SPM_SFT, 1, 1),
 494        SOC_DAPM_SINGLE("DACL Switch", RT5660_SPO_MIXER,
 495                        RT5660_M_DAC_L_SPM_SFT, 1, 1),
 496        SOC_DAPM_SINGLE("SPKVOL Switch", RT5660_SPO_MIXER,
 497                        RT5660_M_SV_SPM_SFT, 1, 1),
 498        SOC_DAPM_SINGLE("BST1 Switch", RT5660_SPO_MIXER,
 499                        RT5660_M_BST1_SPM_SFT, 1, 1),
 500};
 501
 502static const struct snd_kcontrol_new rt5660_lout_mix[] = {
 503        SOC_DAPM_SINGLE("DAC Switch", RT5660_LOUT_MIXER,
 504                        RT5660_M_DAC1_LM_SFT, 1, 1),
 505        SOC_DAPM_SINGLE("OUTMIX Switch", RT5660_LOUT_MIXER,
 506                        RT5660_M_LOVOL_LM_SFT, 1, 1),
 507};
 508
 509static const struct snd_kcontrol_new spk_vol_control =
 510        SOC_DAPM_SINGLE("Switch", RT5660_SPK_VOL,
 511                RT5660_VOL_L_SFT, 1, 1);
 512
 513static const struct snd_kcontrol_new lout_l_vol_control =
 514        SOC_DAPM_SINGLE("Switch", RT5660_LOUT_VOL,
 515                RT5660_VOL_L_SFT, 1, 1);
 516
 517static const struct snd_kcontrol_new lout_r_vol_control =
 518        SOC_DAPM_SINGLE("Switch", RT5660_LOUT_VOL,
 519                RT5660_VOL_R_SFT, 1, 1);
 520
 521/* Interface data select */
 522static const char * const rt5660_data_select[] = {
 523        "L/R", "R/L", "L/L", "R/R"
 524};
 525
 526static SOC_ENUM_SINGLE_DECL(rt5660_if1_dac_enum,
 527        RT5660_DIG_INF1_DATA, RT5660_IF1_DAC_IN_SFT, rt5660_data_select);
 528
 529static SOC_ENUM_SINGLE_DECL(rt5660_if1_adc_enum,
 530        RT5660_DIG_INF1_DATA, RT5660_IF1_ADC_IN_SFT, rt5660_data_select);
 531
 532static const struct snd_kcontrol_new rt5660_if1_dac_swap_mux =
 533        SOC_DAPM_ENUM("IF1 DAC Swap Source", rt5660_if1_dac_enum);
 534
 535static const struct snd_kcontrol_new rt5660_if1_adc_swap_mux =
 536        SOC_DAPM_ENUM("IF1 ADC Swap Source", rt5660_if1_adc_enum);
 537
 538static int rt5660_lout_event(struct snd_soc_dapm_widget *w,
 539        struct snd_kcontrol *kcontrol, int event)
 540{
 541        struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
 542
 543        switch (event) {
 544        case SND_SOC_DAPM_POST_PMU:
 545                snd_soc_component_update_bits(component, RT5660_LOUT_AMP_CTRL,
 546                        RT5660_LOUT_CO_MASK | RT5660_LOUT_CB_MASK,
 547                        RT5660_LOUT_CO_EN | RT5660_LOUT_CB_PU);
 548                break;
 549
 550        case SND_SOC_DAPM_PRE_PMD:
 551                snd_soc_component_update_bits(component, RT5660_LOUT_AMP_CTRL,
 552                        RT5660_LOUT_CO_MASK | RT5660_LOUT_CB_MASK,
 553                        RT5660_LOUT_CO_DIS | RT5660_LOUT_CB_PD);
 554                break;
 555
 556        default:
 557                return 0;
 558        }
 559
 560        return 0;
 561}
 562
 563static const struct snd_soc_dapm_widget rt5660_dapm_widgets[] = {
 564        SND_SOC_DAPM_SUPPLY("LDO2", RT5660_PWR_ANLG1,
 565                RT5660_PWR_LDO2_BIT, 0, NULL, 0),
 566        SND_SOC_DAPM_SUPPLY("PLL1", RT5660_PWR_ANLG2,
 567                RT5660_PWR_PLL_BIT, 0, NULL, 0),
 568
 569        /* MICBIAS */
 570        SND_SOC_DAPM_SUPPLY("MICBIAS1", RT5660_PWR_ANLG2,
 571                        RT5660_PWR_MB1_BIT, 0, NULL, 0),
 572        SND_SOC_DAPM_SUPPLY("MICBIAS2", RT5660_PWR_ANLG2,
 573                        RT5660_PWR_MB2_BIT, 0, NULL, 0),
 574
 575        /* Input Side */
 576        /* Input Lines */
 577        SND_SOC_DAPM_INPUT("DMIC L1"),
 578        SND_SOC_DAPM_INPUT("DMIC R1"),
 579
 580        SND_SOC_DAPM_INPUT("IN1P"),
 581        SND_SOC_DAPM_INPUT("IN1N"),
 582        SND_SOC_DAPM_INPUT("IN2P"),
 583        SND_SOC_DAPM_INPUT("IN3P"),
 584        SND_SOC_DAPM_INPUT("IN3N"),
 585
 586        SND_SOC_DAPM_SUPPLY("DMIC CLK", SND_SOC_NOPM, 0, 0,
 587                rt5660_set_dmic_clk, SND_SOC_DAPM_PRE_PMU),
 588        SND_SOC_DAPM_SUPPLY("DMIC Power", RT5660_DMIC_CTRL1,
 589                RT5660_DMIC_1_EN_SFT, 0, NULL, 0),
 590
 591        /* Boost */
 592        SND_SOC_DAPM_PGA("BST1", RT5660_PWR_ANLG2, RT5660_PWR_BST1_BIT, 0,
 593                NULL, 0),
 594        SND_SOC_DAPM_PGA("BST2", RT5660_PWR_ANLG2, RT5660_PWR_BST2_BIT, 0,
 595                NULL, 0),
 596        SND_SOC_DAPM_PGA("BST3", RT5660_PWR_ANLG2, RT5660_PWR_BST3_BIT, 0,
 597                NULL, 0),
 598
 599        /* REC Mixer */
 600        SND_SOC_DAPM_MIXER("RECMIXL", RT5660_PWR_MIXER, RT5660_PWR_RM_L_BIT,
 601                        0, rt5660_rec_l_mix, ARRAY_SIZE(rt5660_rec_l_mix)),
 602        SND_SOC_DAPM_MIXER("RECMIXR", RT5660_PWR_MIXER, RT5660_PWR_RM_R_BIT,
 603                        0, rt5660_rec_r_mix, ARRAY_SIZE(rt5660_rec_r_mix)),
 604
 605        /* ADCs */
 606        SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0),
 607        SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0),
 608
 609        SND_SOC_DAPM_SUPPLY("ADC L power", RT5660_PWR_DIG1,
 610                        RT5660_PWR_ADC_L_BIT, 0, NULL, 0),
 611        SND_SOC_DAPM_SUPPLY("ADC R power", RT5660_PWR_DIG1,
 612                        RT5660_PWR_ADC_R_BIT, 0, NULL, 0),
 613        SND_SOC_DAPM_SUPPLY("ADC clock", RT5660_PR_BASE + RT5660_CHOP_DAC_ADC,
 614                        12, 0, NULL, 0),
 615
 616        /* ADC Mixer */
 617        SND_SOC_DAPM_SUPPLY("adc stereo1 filter", RT5660_PWR_DIG2,
 618                RT5660_PWR_ADC_S1F_BIT, 0, NULL, 0),
 619        SND_SOC_DAPM_MIXER("Sto1 ADC MIXL", SND_SOC_NOPM, 0, 0,
 620                rt5660_sto1_adc_l_mix, ARRAY_SIZE(rt5660_sto1_adc_l_mix)),
 621        SND_SOC_DAPM_MIXER("Sto1 ADC MIXR", SND_SOC_NOPM, 0, 0,
 622                rt5660_sto1_adc_r_mix, ARRAY_SIZE(rt5660_sto1_adc_r_mix)),
 623
 624        /* ADC */
 625        SND_SOC_DAPM_ADC("Stereo1 ADC MIXL", NULL, RT5660_STO1_ADC_DIG_VOL,
 626                RT5660_L_MUTE_SFT, 1),
 627        SND_SOC_DAPM_ADC("Stereo1 ADC MIXR", NULL, RT5660_STO1_ADC_DIG_VOL,
 628                RT5660_R_MUTE_SFT, 1),
 629
 630        /* Digital Interface */
 631        SND_SOC_DAPM_SUPPLY("I2S1", RT5660_PWR_DIG1, RT5660_PWR_I2S1_BIT, 0,
 632                NULL, 0),
 633        SND_SOC_DAPM_PGA("IF1 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
 634        SND_SOC_DAPM_PGA("IF1 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
 635        SND_SOC_DAPM_PGA("IF1 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
 636        SND_SOC_DAPM_MUX("IF1 DAC Swap Mux", SND_SOC_NOPM, 0, 0,
 637                        &rt5660_if1_dac_swap_mux),
 638        SND_SOC_DAPM_PGA("IF1 ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
 639        SND_SOC_DAPM_MUX("IF1 ADC Swap Mux", SND_SOC_NOPM, 0, 0,
 640                        &rt5660_if1_adc_swap_mux),
 641
 642        /* Audio Interface */
 643        SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
 644        SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
 645
 646        /* Output Side */
 647        /* DAC mixer before sound effect  */
 648        SND_SOC_DAPM_MIXER("DAC1 MIXL", SND_SOC_NOPM, 0, 0, rt5660_dac_l_mix,
 649                ARRAY_SIZE(rt5660_dac_l_mix)),
 650        SND_SOC_DAPM_MIXER("DAC1 MIXR", SND_SOC_NOPM, 0, 0, rt5660_dac_r_mix,
 651                ARRAY_SIZE(rt5660_dac_r_mix)),
 652
 653        /* DAC Mixer */
 654        SND_SOC_DAPM_SUPPLY("dac stereo1 filter", RT5660_PWR_DIG2,
 655                RT5660_PWR_DAC_S1F_BIT, 0, NULL, 0),
 656        SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM, 0, 0,
 657                rt5660_sto_dac_l_mix, ARRAY_SIZE(rt5660_sto_dac_l_mix)),
 658        SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM, 0, 0,
 659                rt5660_sto_dac_r_mix, ARRAY_SIZE(rt5660_sto_dac_r_mix)),
 660
 661        /* DACs */
 662        SND_SOC_DAPM_DAC("DAC L1", NULL, RT5660_PWR_DIG1,
 663                        RT5660_PWR_DAC_L1_BIT, 0),
 664        SND_SOC_DAPM_DAC("DAC R1", NULL, RT5660_PWR_DIG1,
 665                        RT5660_PWR_DAC_R1_BIT, 0),
 666
 667        /* OUT Mixer */
 668        SND_SOC_DAPM_MIXER("SPK MIX", RT5660_PWR_MIXER, RT5660_PWR_SM_BIT,
 669                0, rt5660_spk_mix, ARRAY_SIZE(rt5660_spk_mix)),
 670        SND_SOC_DAPM_MIXER("OUT MIXL", RT5660_PWR_MIXER, RT5660_PWR_OM_L_BIT,
 671                0, rt5660_out_l_mix, ARRAY_SIZE(rt5660_out_l_mix)),
 672        SND_SOC_DAPM_MIXER("OUT MIXR", RT5660_PWR_MIXER, RT5660_PWR_OM_R_BIT,
 673                0, rt5660_out_r_mix, ARRAY_SIZE(rt5660_out_r_mix)),
 674
 675        /* Output Volume */
 676        SND_SOC_DAPM_SWITCH("SPKVOL", RT5660_PWR_VOL,
 677                RT5660_PWR_SV_BIT, 0, &spk_vol_control),
 678        SND_SOC_DAPM_PGA("DAC 1", SND_SOC_NOPM,
 679                0, 0, NULL, 0),
 680        SND_SOC_DAPM_PGA("LOUTVOL", SND_SOC_NOPM,
 681                0, 0, NULL, 0),
 682        SND_SOC_DAPM_SWITCH("LOUTVOL L", SND_SOC_NOPM,
 683                RT5660_PWR_LV_L_BIT, 0, &lout_l_vol_control),
 684        SND_SOC_DAPM_SWITCH("LOUTVOL R", SND_SOC_NOPM,
 685                RT5660_PWR_LV_R_BIT, 0, &lout_r_vol_control),
 686
 687        /* HPO/LOUT/Mono Mixer */
 688        SND_SOC_DAPM_MIXER("SPO MIX", SND_SOC_NOPM, 0,
 689                0, rt5660_spo_mix, ARRAY_SIZE(rt5660_spo_mix)),
 690        SND_SOC_DAPM_MIXER("LOUT MIX", SND_SOC_NOPM, 0, 0,
 691                rt5660_lout_mix, ARRAY_SIZE(rt5660_lout_mix)),
 692        SND_SOC_DAPM_SUPPLY("VREF HP", RT5660_GEN_CTRL1,
 693                RT5660_PWR_VREF_HP_SFT, 0, NULL, 0),
 694        SND_SOC_DAPM_PGA_S("LOUT amp", 1, RT5660_PWR_ANLG1,
 695                RT5660_PWR_HA_BIT, 0, rt5660_lout_event,
 696                SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
 697        SND_SOC_DAPM_PGA_S("SPK amp", 1, RT5660_PWR_DIG1,
 698                RT5660_PWR_CLS_D_BIT, 0, NULL, 0),
 699
 700        /* Output Lines */
 701        SND_SOC_DAPM_OUTPUT("LOUTL"),
 702        SND_SOC_DAPM_OUTPUT("LOUTR"),
 703        SND_SOC_DAPM_OUTPUT("SPO"),
 704};
 705
 706static const struct snd_soc_dapm_route rt5660_dapm_routes[] = {
 707        { "MICBIAS1", NULL, "LDO2" },
 708        { "MICBIAS2", NULL, "LDO2" },
 709
 710        { "BST1", NULL, "IN1P" },
 711        { "BST1", NULL, "IN1N" },
 712        { "BST2", NULL, "IN2P" },
 713        { "BST3", NULL, "IN3P" },
 714        { "BST3", NULL, "IN3N" },
 715
 716        { "RECMIXL", "BST3 Switch", "BST3" },
 717        { "RECMIXL", "BST2 Switch", "BST2" },
 718        { "RECMIXL", "BST1 Switch", "BST1" },
 719        { "RECMIXL", "OUT MIXL Switch", "OUT MIXL" },
 720
 721        { "RECMIXR", "BST3 Switch", "BST3" },
 722        { "RECMIXR", "BST2 Switch", "BST2" },
 723        { "RECMIXR", "BST1 Switch", "BST1" },
 724        { "RECMIXR", "OUT MIXR Switch", "OUT MIXR" },
 725
 726        { "ADC L", NULL, "RECMIXL" },
 727        { "ADC L", NULL, "ADC L power" },
 728        { "ADC L", NULL, "ADC clock" },
 729        { "ADC R", NULL, "RECMIXR" },
 730        { "ADC R", NULL, "ADC R power" },
 731        { "ADC R", NULL, "ADC clock" },
 732
 733        {"DMIC L1", NULL, "DMIC CLK"},
 734        {"DMIC L1", NULL, "DMIC Power"},
 735        {"DMIC R1", NULL, "DMIC CLK"},
 736        {"DMIC R1", NULL, "DMIC Power"},
 737
 738        { "Sto1 ADC MIXL", "ADC1 Switch", "ADC L" },
 739        { "Sto1 ADC MIXL", "ADC2 Switch", "DMIC L1" },
 740        { "Sto1 ADC MIXR", "ADC1 Switch", "ADC R" },
 741        { "Sto1 ADC MIXR", "ADC2 Switch", "DMIC R1" },
 742
 743        { "Stereo1 ADC MIXL", NULL, "Sto1 ADC MIXL" },
 744        { "Stereo1 ADC MIXL", NULL, "adc stereo1 filter" },
 745        { "adc stereo1 filter", NULL, "PLL1", rt5660_is_sys_clk_from_pll },
 746
 747        { "Stereo1 ADC MIXR", NULL, "Sto1 ADC MIXR" },
 748        { "Stereo1 ADC MIXR", NULL, "adc stereo1 filter" },
 749        { "adc stereo1 filter", NULL, "PLL1", rt5660_is_sys_clk_from_pll },
 750
 751        { "IF1 ADC", NULL, "Stereo1 ADC MIXL" },
 752        { "IF1 ADC", NULL, "Stereo1 ADC MIXR" },
 753        { "IF1 ADC", NULL, "I2S1" },
 754
 755        { "IF1 ADC Swap Mux", "L/R", "IF1 ADC" },
 756        { "IF1 ADC Swap Mux", "R/L", "IF1 ADC" },
 757        { "IF1 ADC Swap Mux", "L/L", "IF1 ADC" },
 758        { "IF1 ADC Swap Mux", "R/R", "IF1 ADC" },
 759        { "AIF1TX", NULL, "IF1 ADC Swap Mux" },
 760
 761        { "IF1 DAC", NULL, "AIF1RX" },
 762        { "IF1 DAC", NULL, "I2S1" },
 763
 764        { "IF1 DAC Swap Mux", "L/R", "IF1 DAC" },
 765        { "IF1 DAC Swap Mux", "R/L", "IF1 DAC" },
 766        { "IF1 DAC Swap Mux", "L/L", "IF1 DAC" },
 767        { "IF1 DAC Swap Mux", "R/R", "IF1 DAC" },
 768
 769        { "IF1 DAC L", NULL, "IF1 DAC Swap Mux" },
 770        { "IF1 DAC R", NULL, "IF1 DAC Swap Mux" },
 771
 772        { "DAC1 MIXL", "Stereo ADC Switch", "Stereo1 ADC MIXL" },
 773        { "DAC1 MIXL", "DAC1 Switch", "IF1 DAC L" },
 774        { "DAC1 MIXR", "Stereo ADC Switch", "Stereo1 ADC MIXR" },
 775        { "DAC1 MIXR", "DAC1 Switch", "IF1 DAC R" },
 776
 777        { "Stereo DAC MIXL", "DAC L1 Switch", "DAC1 MIXL" },
 778        { "Stereo DAC MIXL", "DAC R1 Switch", "DAC1 MIXR" },
 779        { "Stereo DAC MIXL", NULL, "dac stereo1 filter" },
 780        { "dac stereo1 filter", NULL, "PLL1", rt5660_is_sys_clk_from_pll },
 781        { "Stereo DAC MIXR", "DAC R1 Switch", "DAC1 MIXR" },
 782        { "Stereo DAC MIXR", "DAC L1 Switch", "DAC1 MIXL" },
 783        { "Stereo DAC MIXR", NULL, "dac stereo1 filter" },
 784        { "dac stereo1 filter", NULL, "PLL1", rt5660_is_sys_clk_from_pll },
 785
 786        { "DAC L1", NULL, "Stereo DAC MIXL" },
 787        { "DAC R1", NULL, "Stereo DAC MIXR" },
 788
 789        { "SPK MIX", "BST3 Switch", "BST3" },
 790        { "SPK MIX", "BST1 Switch", "BST1" },
 791        { "SPK MIX", "DACL Switch", "DAC L1" },
 792        { "SPK MIX", "DACR Switch", "DAC R1" },
 793        { "SPK MIX", "OUTMIXL Switch", "OUT MIXL" },
 794
 795        { "OUT MIXL", "BST3 Switch", "BST3" },
 796        { "OUT MIXL", "BST2 Switch", "BST2" },
 797        { "OUT MIXL", "BST1 Switch", "BST1" },
 798        { "OUT MIXL", "RECMIXL Switch", "RECMIXL" },
 799        { "OUT MIXL", "DACR Switch", "DAC R1" },
 800        { "OUT MIXL", "DACL Switch", "DAC L1" },
 801
 802        { "OUT MIXR", "BST2 Switch", "BST2" },
 803        { "OUT MIXR", "BST1 Switch", "BST1" },
 804        { "OUT MIXR", "RECMIXR Switch", "RECMIXR" },
 805        { "OUT MIXR", "DACR Switch", "DAC R1" },
 806        { "OUT MIXR", "DACL Switch", "DAC L1" },
 807
 808        { "SPO MIX", "DACR Switch", "DAC R1" },
 809        { "SPO MIX", "DACL Switch", "DAC L1" },
 810        { "SPO MIX", "SPKVOL Switch", "SPKVOL" },
 811        { "SPO MIX", "BST1 Switch", "BST1" },
 812
 813        { "SPKVOL", "Switch", "SPK MIX" },
 814        { "LOUTVOL L", "Switch", "OUT MIXL" },
 815        { "LOUTVOL R", "Switch", "OUT MIXR" },
 816
 817        { "LOUTVOL", NULL, "LOUTVOL L" },
 818        { "LOUTVOL", NULL, "LOUTVOL R" },
 819
 820        { "DAC 1", NULL, "DAC L1" },
 821        { "DAC 1", NULL, "DAC R1" },
 822
 823        { "LOUT MIX", "DAC Switch", "DAC 1" },
 824        { "LOUT MIX", "OUTMIX Switch", "LOUTVOL" },
 825
 826        { "LOUT amp", NULL, "LOUT MIX" },
 827        { "LOUT amp", NULL, "VREF HP" },
 828        { "LOUTL", NULL, "LOUT amp" },
 829        { "LOUTR", NULL, "LOUT amp" },
 830
 831        { "SPK amp", NULL, "SPO MIX" },
 832        { "SPO", NULL, "SPK amp" },
 833};
 834
 835static int rt5660_hw_params(struct snd_pcm_substream *substream,
 836        struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
 837{
 838        struct snd_soc_component *component = dai->component;
 839        struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
 840        unsigned int val_len = 0, val_clk, mask_clk;
 841        int pre_div, bclk_ms, frame_size;
 842
 843        rt5660->lrck[dai->id] = params_rate(params);
 844        pre_div = rl6231_get_clk_info(rt5660->sysclk, rt5660->lrck[dai->id]);
 845        if (pre_div < 0) {
 846                dev_err(component->dev, "Unsupported clock setting %d for DAI %d\n",
 847                        rt5660->lrck[dai->id], dai->id);
 848                return -EINVAL;
 849        }
 850
 851        frame_size = snd_soc_params_to_frame_size(params);
 852        if (frame_size < 0) {
 853                dev_err(component->dev, "Unsupported frame size: %d\n", frame_size);
 854                return frame_size;
 855        }
 856
 857        if (frame_size > 32)
 858                bclk_ms = 1;
 859        else
 860                bclk_ms = 0;
 861
 862        rt5660->bclk[dai->id] = rt5660->lrck[dai->id] * (32 << bclk_ms);
 863
 864        dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n",
 865                rt5660->bclk[dai->id], rt5660->lrck[dai->id]);
 866        dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
 867                                bclk_ms, pre_div, dai->id);
 868
 869        switch (params_width(params)) {
 870        case 16:
 871                break;
 872        case 20:
 873                val_len |= RT5660_I2S_DL_20;
 874                break;
 875        case 24:
 876                val_len |= RT5660_I2S_DL_24;
 877                break;
 878        case 8:
 879                val_len |= RT5660_I2S_DL_8;
 880                break;
 881        default:
 882                return -EINVAL;
 883        }
 884
 885        switch (dai->id) {
 886        case RT5660_AIF1:
 887                mask_clk = RT5660_I2S_BCLK_MS1_MASK | RT5660_I2S_PD1_MASK;
 888                val_clk = bclk_ms << RT5660_I2S_BCLK_MS1_SFT |
 889                        pre_div << RT5660_I2S_PD1_SFT;
 890                snd_soc_component_update_bits(component, RT5660_I2S1_SDP, RT5660_I2S_DL_MASK,
 891                        val_len);
 892                snd_soc_component_update_bits(component, RT5660_ADDA_CLK1, mask_clk, val_clk);
 893                break;
 894
 895        default:
 896                dev_err(component->dev, "Invalid dai->id: %d\n", dai->id);
 897                return -EINVAL;
 898        }
 899
 900        return 0;
 901}
 902
 903static int rt5660_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
 904{
 905        struct snd_soc_component *component = dai->component;
 906        struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
 907        unsigned int reg_val = 0;
 908
 909        switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
 910        case SND_SOC_DAIFMT_CBM_CFM:
 911                rt5660->master[dai->id] = 1;
 912                break;
 913
 914        case SND_SOC_DAIFMT_CBS_CFS:
 915                reg_val |= RT5660_I2S_MS_S;
 916                rt5660->master[dai->id] = 0;
 917                break;
 918
 919        default:
 920                return -EINVAL;
 921        }
 922
 923        switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
 924        case SND_SOC_DAIFMT_NB_NF:
 925                break;
 926
 927        case SND_SOC_DAIFMT_IB_NF:
 928                reg_val |= RT5660_I2S_BP_INV;
 929                break;
 930
 931        default:
 932                return -EINVAL;
 933        }
 934
 935        switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
 936        case SND_SOC_DAIFMT_I2S:
 937                break;
 938
 939        case SND_SOC_DAIFMT_LEFT_J:
 940                reg_val |= RT5660_I2S_DF_LEFT;
 941                break;
 942
 943        case SND_SOC_DAIFMT_DSP_A:
 944                reg_val |= RT5660_I2S_DF_PCM_A;
 945                break;
 946
 947        case SND_SOC_DAIFMT_DSP_B:
 948                reg_val  |= RT5660_I2S_DF_PCM_B;
 949                break;
 950
 951        default:
 952                return -EINVAL;
 953        }
 954
 955        switch (dai->id) {
 956        case RT5660_AIF1:
 957                snd_soc_component_update_bits(component, RT5660_I2S1_SDP,
 958                        RT5660_I2S_MS_MASK | RT5660_I2S_BP_MASK |
 959                        RT5660_I2S_DF_MASK, reg_val);
 960                break;
 961
 962        default:
 963                dev_err(component->dev, "Invalid dai->id: %d\n", dai->id);
 964                return -EINVAL;
 965        }
 966
 967        return 0;
 968}
 969
 970static int rt5660_set_dai_sysclk(struct snd_soc_dai *dai,
 971                int clk_id, unsigned int freq, int dir)
 972{
 973        struct snd_soc_component *component = dai->component;
 974        struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
 975        unsigned int reg_val = 0;
 976
 977        if (freq == rt5660->sysclk && clk_id == rt5660->sysclk_src)
 978                return 0;
 979
 980        switch (clk_id) {
 981        case RT5660_SCLK_S_MCLK:
 982                reg_val |= RT5660_SCLK_SRC_MCLK;
 983                break;
 984
 985        case RT5660_SCLK_S_PLL1:
 986                reg_val |= RT5660_SCLK_SRC_PLL1;
 987                break;
 988
 989        case RT5660_SCLK_S_RCCLK:
 990                reg_val |= RT5660_SCLK_SRC_RCCLK;
 991                break;
 992
 993        default:
 994                dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
 995                return -EINVAL;
 996        }
 997
 998        snd_soc_component_update_bits(component, RT5660_GLB_CLK, RT5660_SCLK_SRC_MASK,
 999                reg_val);
1000
1001        rt5660->sysclk = freq;
1002        rt5660->sysclk_src = clk_id;
1003
1004        dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
1005
1006        return 0;
1007}
1008
1009static int rt5660_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
1010                        unsigned int freq_in, unsigned int freq_out)
1011{
1012        struct snd_soc_component *component = dai->component;
1013        struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
1014        struct rl6231_pll_code pll_code;
1015        int ret;
1016
1017        if (source == rt5660->pll_src && freq_in == rt5660->pll_in &&
1018                freq_out == rt5660->pll_out)
1019                return 0;
1020
1021        if (!freq_in || !freq_out) {
1022                dev_dbg(component->dev, "PLL disabled\n");
1023
1024                rt5660->pll_in = 0;
1025                rt5660->pll_out = 0;
1026                snd_soc_component_update_bits(component, RT5660_GLB_CLK,
1027                        RT5660_SCLK_SRC_MASK, RT5660_SCLK_SRC_MCLK);
1028                return 0;
1029        }
1030
1031        switch (source) {
1032        case RT5660_PLL1_S_MCLK:
1033                snd_soc_component_update_bits(component, RT5660_GLB_CLK,
1034                        RT5660_PLL1_SRC_MASK, RT5660_PLL1_SRC_MCLK);
1035                break;
1036
1037        case RT5660_PLL1_S_BCLK:
1038                snd_soc_component_update_bits(component, RT5660_GLB_CLK,
1039                        RT5660_PLL1_SRC_MASK, RT5660_PLL1_SRC_BCLK1);
1040                break;
1041
1042        default:
1043                dev_err(component->dev, "Unknown PLL source %d\n", source);
1044                return -EINVAL;
1045        }
1046
1047        ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
1048        if (ret < 0) {
1049                dev_err(component->dev, "Unsupport input clock %d\n", freq_in);
1050                return ret;
1051        }
1052
1053        dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n",
1054                pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
1055                pll_code.n_code, pll_code.k_code);
1056
1057        snd_soc_component_write(component, RT5660_PLL_CTRL1,
1058                pll_code.n_code << RT5660_PLL_N_SFT | pll_code.k_code);
1059        snd_soc_component_write(component, RT5660_PLL_CTRL2,
1060                (pll_code.m_bp ? 0 : pll_code.m_code) << RT5660_PLL_M_SFT |
1061                pll_code.m_bp << RT5660_PLL_M_BP_SFT);
1062
1063        rt5660->pll_in = freq_in;
1064        rt5660->pll_out = freq_out;
1065        rt5660->pll_src = source;
1066
1067        return 0;
1068}
1069
1070static int rt5660_set_bias_level(struct snd_soc_component *component,
1071                        enum snd_soc_bias_level level)
1072{
1073        struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
1074        int ret;
1075
1076        switch (level) {
1077        case SND_SOC_BIAS_ON:
1078                break;
1079
1080        case SND_SOC_BIAS_PREPARE:
1081                snd_soc_component_update_bits(component, RT5660_GEN_CTRL1,
1082                        RT5660_DIG_GATE_CTRL, RT5660_DIG_GATE_CTRL);
1083
1084                if (IS_ERR(rt5660->mclk))
1085                        break;
1086
1087                if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_ON) {
1088                        clk_disable_unprepare(rt5660->mclk);
1089                } else {
1090                        ret = clk_prepare_enable(rt5660->mclk);
1091                        if (ret)
1092                                return ret;
1093                }
1094                break;
1095
1096        case SND_SOC_BIAS_STANDBY:
1097                if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
1098                        snd_soc_component_update_bits(component, RT5660_PWR_ANLG1,
1099                                RT5660_PWR_VREF1 | RT5660_PWR_MB |
1100                                RT5660_PWR_BG | RT5660_PWR_VREF2,
1101                                RT5660_PWR_VREF1 | RT5660_PWR_MB |
1102                                RT5660_PWR_BG | RT5660_PWR_VREF2);
1103                        usleep_range(10000, 15000);
1104                        snd_soc_component_update_bits(component, RT5660_PWR_ANLG1,
1105                                RT5660_PWR_FV1 | RT5660_PWR_FV2,
1106                                RT5660_PWR_FV1 | RT5660_PWR_FV2);
1107                }
1108                break;
1109
1110        case SND_SOC_BIAS_OFF:
1111                snd_soc_component_update_bits(component, RT5660_GEN_CTRL1,
1112                        RT5660_DIG_GATE_CTRL, 0);
1113                break;
1114
1115        default:
1116                break;
1117        }
1118
1119        return 0;
1120}
1121
1122static int rt5660_probe(struct snd_soc_component *component)
1123{
1124        struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
1125
1126        rt5660->component = component;
1127
1128        return 0;
1129}
1130
1131static void rt5660_remove(struct snd_soc_component *component)
1132{
1133        snd_soc_component_write(component, RT5660_RESET, 0);
1134}
1135
1136#ifdef CONFIG_PM
1137static int rt5660_suspend(struct snd_soc_component *component)
1138{
1139        struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
1140
1141        regcache_cache_only(rt5660->regmap, true);
1142        regcache_mark_dirty(rt5660->regmap);
1143
1144        return 0;
1145}
1146
1147static int rt5660_resume(struct snd_soc_component *component)
1148{
1149        struct rt5660_priv *rt5660 = snd_soc_component_get_drvdata(component);
1150
1151        if (rt5660->pdata.poweroff_codec_in_suspend)
1152                msleep(350);
1153
1154        regcache_cache_only(rt5660->regmap, false);
1155        regcache_sync(rt5660->regmap);
1156
1157        return 0;
1158}
1159#else
1160#define rt5660_suspend NULL
1161#define rt5660_resume NULL
1162#endif
1163
1164#define RT5660_STEREO_RATES SNDRV_PCM_RATE_8000_192000
1165#define RT5660_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1166                        SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
1167
1168static const struct snd_soc_dai_ops rt5660_aif_dai_ops = {
1169        .hw_params = rt5660_hw_params,
1170        .set_fmt = rt5660_set_dai_fmt,
1171        .set_sysclk = rt5660_set_dai_sysclk,
1172        .set_pll = rt5660_set_dai_pll,
1173};
1174
1175static struct snd_soc_dai_driver rt5660_dai[] = {
1176        {
1177                .name = "rt5660-aif1",
1178                .id = RT5660_AIF1,
1179                .playback = {
1180                        .stream_name = "AIF1 Playback",
1181                        .channels_min = 1,
1182                        .channels_max = 2,
1183                        .rates = RT5660_STEREO_RATES,
1184                        .formats = RT5660_FORMATS,
1185                },
1186                .capture = {
1187                        .stream_name = "AIF1 Capture",
1188                        .channels_min = 1,
1189                        .channels_max = 2,
1190                        .rates = RT5660_STEREO_RATES,
1191                        .formats = RT5660_FORMATS,
1192                },
1193                .ops = &rt5660_aif_dai_ops,
1194        },
1195};
1196
1197static const struct snd_soc_component_driver soc_component_dev_rt5660 = {
1198        .probe                  = rt5660_probe,
1199        .remove                 = rt5660_remove,
1200        .suspend                = rt5660_suspend,
1201        .resume                 = rt5660_resume,
1202        .set_bias_level         = rt5660_set_bias_level,
1203        .controls               = rt5660_snd_controls,
1204        .num_controls           = ARRAY_SIZE(rt5660_snd_controls),
1205        .dapm_widgets           = rt5660_dapm_widgets,
1206        .num_dapm_widgets       = ARRAY_SIZE(rt5660_dapm_widgets),
1207        .dapm_routes            = rt5660_dapm_routes,
1208        .num_dapm_routes        = ARRAY_SIZE(rt5660_dapm_routes),
1209        .use_pmdown_time        = 1,
1210        .endianness             = 1,
1211        .non_legacy_dai_naming  = 1,
1212};
1213
1214static const struct regmap_config rt5660_regmap = {
1215        .reg_bits = 8,
1216        .val_bits = 16,
1217        .use_single_read = true,
1218        .use_single_write = true,
1219
1220        .max_register = RT5660_VENDOR_ID2 + 1 + (ARRAY_SIZE(rt5660_ranges) *
1221                                               RT5660_PR_SPACING),
1222        .volatile_reg = rt5660_volatile_register,
1223        .readable_reg = rt5660_readable_register,
1224
1225        .cache_type = REGCACHE_RBTREE,
1226        .reg_defaults = rt5660_reg,
1227        .num_reg_defaults = ARRAY_SIZE(rt5660_reg),
1228        .ranges = rt5660_ranges,
1229        .num_ranges = ARRAY_SIZE(rt5660_ranges),
1230};
1231
1232static const struct i2c_device_id rt5660_i2c_id[] = {
1233        { "rt5660", 0 },
1234        { }
1235};
1236MODULE_DEVICE_TABLE(i2c, rt5660_i2c_id);
1237
1238static const struct of_device_id rt5660_of_match[] = {
1239        { .compatible = "realtek,rt5660", },
1240        {},
1241};
1242MODULE_DEVICE_TABLE(of, rt5660_of_match);
1243
1244static const struct acpi_device_id rt5660_acpi_match[] = {
1245        { "10EC5660", 0 },
1246        { "10EC3277", 0 },
1247        { },
1248};
1249MODULE_DEVICE_TABLE(acpi, rt5660_acpi_match);
1250
1251static int rt5660_parse_dt(struct rt5660_priv *rt5660, struct device *dev)
1252{
1253        rt5660->pdata.in1_diff = device_property_read_bool(dev,
1254                                        "realtek,in1-differential");
1255        rt5660->pdata.in3_diff = device_property_read_bool(dev,
1256                                        "realtek,in3-differential");
1257        rt5660->pdata.poweroff_codec_in_suspend = device_property_read_bool(dev,
1258                                        "realtek,poweroff-in-suspend");
1259        device_property_read_u32(dev, "realtek,dmic1-data-pin",
1260                &rt5660->pdata.dmic1_data_pin);
1261
1262        return 0;
1263}
1264
1265static int rt5660_i2c_probe(struct i2c_client *i2c,
1266                    const struct i2c_device_id *id)
1267{
1268        struct rt5660_platform_data *pdata = dev_get_platdata(&i2c->dev);
1269        struct rt5660_priv *rt5660;
1270        int ret;
1271        unsigned int val;
1272
1273        rt5660 = devm_kzalloc(&i2c->dev, sizeof(struct rt5660_priv),
1274                GFP_KERNEL);
1275
1276        if (rt5660 == NULL)
1277                return -ENOMEM;
1278
1279        /* Check if MCLK provided */
1280        rt5660->mclk = devm_clk_get(&i2c->dev, "mclk");
1281        if (PTR_ERR(rt5660->mclk) == -EPROBE_DEFER)
1282                return -EPROBE_DEFER;
1283
1284        i2c_set_clientdata(i2c, rt5660);
1285
1286        if (pdata)
1287                rt5660->pdata = *pdata;
1288        else if (i2c->dev.of_node)
1289                rt5660_parse_dt(rt5660, &i2c->dev);
1290
1291        rt5660->regmap = devm_regmap_init_i2c(i2c, &rt5660_regmap);
1292        if (IS_ERR(rt5660->regmap)) {
1293                ret = PTR_ERR(rt5660->regmap);
1294                dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1295                        ret);
1296                return ret;
1297        }
1298
1299        regmap_read(rt5660->regmap, RT5660_VENDOR_ID2, &val);
1300        if (val != RT5660_DEVICE_ID) {
1301                dev_err(&i2c->dev,
1302                        "Device with ID register %#x is not rt5660\n", val);
1303                return -ENODEV;
1304        }
1305
1306        regmap_write(rt5660->regmap, RT5660_RESET, 0);
1307
1308        ret = regmap_register_patch(rt5660->regmap, rt5660_patch,
1309                                    ARRAY_SIZE(rt5660_patch));
1310        if (ret != 0)
1311                dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
1312
1313        regmap_update_bits(rt5660->regmap, RT5660_GEN_CTRL1,
1314                RT5660_AUTO_DIS_AMP | RT5660_MCLK_DET | RT5660_POW_CLKDET,
1315                RT5660_AUTO_DIS_AMP | RT5660_MCLK_DET | RT5660_POW_CLKDET);
1316
1317        if (rt5660->pdata.dmic1_data_pin) {
1318                regmap_update_bits(rt5660->regmap, RT5660_GPIO_CTRL1,
1319                        RT5660_GP1_PIN_MASK, RT5660_GP1_PIN_DMIC1_SCL);
1320
1321                if (rt5660->pdata.dmic1_data_pin == RT5660_DMIC1_DATA_GPIO2)
1322                        regmap_update_bits(rt5660->regmap, RT5660_DMIC_CTRL1,
1323                                RT5660_SEL_DMIC_DATA_MASK,
1324                                RT5660_SEL_DMIC_DATA_GPIO2);
1325                else if (rt5660->pdata.dmic1_data_pin == RT5660_DMIC1_DATA_IN1P)
1326                        regmap_update_bits(rt5660->regmap, RT5660_DMIC_CTRL1,
1327                                RT5660_SEL_DMIC_DATA_MASK,
1328                                RT5660_SEL_DMIC_DATA_IN1P);
1329        }
1330
1331        return devm_snd_soc_register_component(&i2c->dev,
1332                                      &soc_component_dev_rt5660,
1333                                      rt5660_dai, ARRAY_SIZE(rt5660_dai));
1334}
1335
1336static struct i2c_driver rt5660_i2c_driver = {
1337        .driver = {
1338                .name = "rt5660",
1339                .acpi_match_table = ACPI_PTR(rt5660_acpi_match),
1340                .of_match_table = of_match_ptr(rt5660_of_match),
1341        },
1342        .probe = rt5660_i2c_probe,
1343        .id_table = rt5660_i2c_id,
1344};
1345module_i2c_driver(rt5660_i2c_driver);
1346
1347MODULE_DESCRIPTION("ASoC RT5660 driver");
1348MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>");
1349MODULE_LICENSE("GPL v2");
1350