1aab1ad11SSeven Lee // SPDX-License-Identifier: GPL-2.0-only 2aab1ad11SSeven Lee // 3aab1ad11SSeven Lee // nau8821.c -- Nuvoton NAU88L21 audio codec driver 4aab1ad11SSeven Lee // 5aab1ad11SSeven Lee // Copyright 2021 Nuvoton Technology Corp. 6aab1ad11SSeven Lee // Author: John Hsu <kchsu0@nuvoton.com> 7aab1ad11SSeven Lee // Co-author: Seven Lee <wtli@nuvoton.com> 8aab1ad11SSeven Lee // 9aab1ad11SSeven Lee 10aab1ad11SSeven Lee #include <linux/acpi.h> 11aab1ad11SSeven Lee #include <linux/clk.h> 12aab1ad11SSeven Lee #include <linux/delay.h> 131bc40efdSEdson Juliano Drosdeck #include <linux/dmi.h> 14aab1ad11SSeven Lee #include <linux/init.h> 15aab1ad11SSeven Lee #include <linux/i2c.h> 16aab1ad11SSeven Lee #include <linux/module.h> 17aab1ad11SSeven Lee #include <linux/math64.h> 18aab1ad11SSeven Lee #include <linux/regmap.h> 19aab1ad11SSeven Lee #include <linux/slab.h> 20aab1ad11SSeven Lee #include <sound/core.h> 21aab1ad11SSeven Lee #include <sound/initval.h> 22aab1ad11SSeven Lee #include <sound/jack.h> 23aab1ad11SSeven Lee #include <sound/pcm.h> 24aab1ad11SSeven Lee #include <sound/pcm_params.h> 25aab1ad11SSeven Lee #include <sound/soc.h> 26aab1ad11SSeven Lee #include <sound/tlv.h> 27aab1ad11SSeven Lee #include "nau8821.h" 28aab1ad11SSeven Lee 291bc40efdSEdson Juliano Drosdeck #define NAU8821_JD_ACTIVE_HIGH BIT(0) 301bc40efdSEdson Juliano Drosdeck 311bc40efdSEdson Juliano Drosdeck static int nau8821_quirk; 321bc40efdSEdson Juliano Drosdeck static int quirk_override = -1; 331bc40efdSEdson Juliano Drosdeck module_param_named(quirk, quirk_override, uint, 0444); 341bc40efdSEdson Juliano Drosdeck MODULE_PARM_DESC(quirk, "Board-specific quirk override"); 351bc40efdSEdson Juliano Drosdeck 36aab1ad11SSeven Lee #define NAU_FREF_MAX 13500000 37aab1ad11SSeven Lee #define NAU_FVCO_MAX 100000000 38aab1ad11SSeven Lee #define NAU_FVCO_MIN 90000000 39aab1ad11SSeven Lee 402551b6e8SSeven Lee #define NAU8821_BUTTON SND_JACK_BTN_0 412551b6e8SSeven Lee 42aab1ad11SSeven Lee /* the maximum frequency of CLK_ADC and CLK_DAC */ 43aab1ad11SSeven Lee #define CLK_DA_AD_MAX 6144000 44aab1ad11SSeven Lee 45aab1ad11SSeven Lee static int nau8821_configure_sysclk(struct nau8821 *nau8821, 46aab1ad11SSeven Lee int clk_id, unsigned int freq); 470cf470c0SWallace Lin static bool nau8821_is_jack_inserted(struct regmap *regmap); 48aab1ad11SSeven Lee 49aab1ad11SSeven Lee struct nau8821_fll { 50aab1ad11SSeven Lee int mclk_src; 51aab1ad11SSeven Lee int ratio; 52aab1ad11SSeven Lee int fll_frac; 53aab1ad11SSeven Lee int fll_int; 54aab1ad11SSeven Lee int clk_ref_div; 55aab1ad11SSeven Lee }; 56aab1ad11SSeven Lee 57aab1ad11SSeven Lee struct nau8821_fll_attr { 58aab1ad11SSeven Lee unsigned int param; 59aab1ad11SSeven Lee unsigned int val; 60aab1ad11SSeven Lee }; 61aab1ad11SSeven Lee 62aab1ad11SSeven Lee /* scaling for mclk from sysclk_src output */ 63aab1ad11SSeven Lee static const struct nau8821_fll_attr mclk_src_scaling[] = { 64aab1ad11SSeven Lee { 1, 0x0 }, 65aab1ad11SSeven Lee { 2, 0x2 }, 66aab1ad11SSeven Lee { 4, 0x3 }, 67aab1ad11SSeven Lee { 8, 0x4 }, 68aab1ad11SSeven Lee { 16, 0x5 }, 69aab1ad11SSeven Lee { 32, 0x6 }, 70aab1ad11SSeven Lee { 3, 0x7 }, 71aab1ad11SSeven Lee { 6, 0xa }, 72aab1ad11SSeven Lee { 12, 0xb }, 73aab1ad11SSeven Lee { 24, 0xc }, 74aab1ad11SSeven Lee { 48, 0xd }, 75aab1ad11SSeven Lee { 96, 0xe }, 76aab1ad11SSeven Lee { 5, 0xf }, 77aab1ad11SSeven Lee }; 78aab1ad11SSeven Lee 79aab1ad11SSeven Lee /* ratio for input clk freq */ 80aab1ad11SSeven Lee static const struct nau8821_fll_attr fll_ratio[] = { 81aab1ad11SSeven Lee { 512000, 0x01 }, 82aab1ad11SSeven Lee { 256000, 0x02 }, 83aab1ad11SSeven Lee { 128000, 0x04 }, 84aab1ad11SSeven Lee { 64000, 0x08 }, 85aab1ad11SSeven Lee { 32000, 0x10 }, 86aab1ad11SSeven Lee { 8000, 0x20 }, 87aab1ad11SSeven Lee { 4000, 0x40 }, 88aab1ad11SSeven Lee }; 89aab1ad11SSeven Lee 90aab1ad11SSeven Lee static const struct nau8821_fll_attr fll_pre_scalar[] = { 91aab1ad11SSeven Lee { 0, 0x0 }, 92aab1ad11SSeven Lee { 1, 0x1 }, 93aab1ad11SSeven Lee { 2, 0x2 }, 94aab1ad11SSeven Lee { 3, 0x3 }, 95aab1ad11SSeven Lee }; 96aab1ad11SSeven Lee 97aab1ad11SSeven Lee /* over sampling rate */ 98aab1ad11SSeven Lee struct nau8821_osr_attr { 99aab1ad11SSeven Lee unsigned int osr; 100aab1ad11SSeven Lee unsigned int clk_src; 101aab1ad11SSeven Lee }; 102aab1ad11SSeven Lee 103aab1ad11SSeven Lee static const struct nau8821_osr_attr osr_dac_sel[] = { 104aab1ad11SSeven Lee { 64, 2 }, /* OSR 64, SRC 1/4 */ 105aab1ad11SSeven Lee { 256, 0 }, /* OSR 256, SRC 1 */ 106aab1ad11SSeven Lee { 128, 1 }, /* OSR 128, SRC 1/2 */ 107aab1ad11SSeven Lee { 0, 0 }, 108aab1ad11SSeven Lee { 32, 3 }, /* OSR 32, SRC 1/8 */ 109aab1ad11SSeven Lee }; 110aab1ad11SSeven Lee 111aab1ad11SSeven Lee static const struct nau8821_osr_attr osr_adc_sel[] = { 112aab1ad11SSeven Lee { 32, 3 }, /* OSR 32, SRC 1/8 */ 113aab1ad11SSeven Lee { 64, 2 }, /* OSR 64, SRC 1/4 */ 114aab1ad11SSeven Lee { 128, 1 }, /* OSR 128, SRC 1/2 */ 115aab1ad11SSeven Lee { 256, 0 }, /* OSR 256, SRC 1 */ 116aab1ad11SSeven Lee }; 117aab1ad11SSeven Lee 118aab1ad11SSeven Lee struct nau8821_dmic_speed { 119aab1ad11SSeven Lee unsigned int param; 120aab1ad11SSeven Lee unsigned int val; 121aab1ad11SSeven Lee }; 122aab1ad11SSeven Lee 123aab1ad11SSeven Lee static const struct nau8821_dmic_speed dmic_speed_sel[] = { 124aab1ad11SSeven Lee { 0, 0x0 }, /*SPEED 1, SRC 1 */ 125aab1ad11SSeven Lee { 1, 0x1 }, /*SPEED 2, SRC 1/2 */ 126aab1ad11SSeven Lee { 2, 0x2 }, /*SPEED 4, SRC 1/4 */ 127aab1ad11SSeven Lee { 3, 0x3 }, /*SPEED 8, SRC 1/8 */ 128aab1ad11SSeven Lee }; 129aab1ad11SSeven Lee 130aab1ad11SSeven Lee static const struct reg_default nau8821_reg_defaults[] = { 131aab1ad11SSeven Lee { NAU8821_R01_ENA_CTRL, 0x00ff }, 132aab1ad11SSeven Lee { NAU8821_R03_CLK_DIVIDER, 0x0050 }, 133aab1ad11SSeven Lee { NAU8821_R04_FLL1, 0x0 }, 134aab1ad11SSeven Lee { NAU8821_R05_FLL2, 0x00bc }, 135aab1ad11SSeven Lee { NAU8821_R06_FLL3, 0x0008 }, 136aab1ad11SSeven Lee { NAU8821_R07_FLL4, 0x0010 }, 137aab1ad11SSeven Lee { NAU8821_R08_FLL5, 0x4000 }, 138aab1ad11SSeven Lee { NAU8821_R09_FLL6, 0x6900 }, 139aab1ad11SSeven Lee { NAU8821_R0A_FLL7, 0x0031 }, 140aab1ad11SSeven Lee { NAU8821_R0B_FLL8, 0x26e9 }, 141aab1ad11SSeven Lee { NAU8821_R0D_JACK_DET_CTRL, 0x0 }, 142aab1ad11SSeven Lee { NAU8821_R0F_INTERRUPT_MASK, 0x0 }, 143aab1ad11SSeven Lee { NAU8821_R12_INTERRUPT_DIS_CTRL, 0xffff }, 144aab1ad11SSeven Lee { NAU8821_R13_DMIC_CTRL, 0x0 }, 145aab1ad11SSeven Lee { NAU8821_R1A_GPIO12_CTRL, 0x0 }, 146aab1ad11SSeven Lee { NAU8821_R1B_TDM_CTRL, 0x0 }, 147aab1ad11SSeven Lee { NAU8821_R1C_I2S_PCM_CTRL1, 0x000a }, 148aab1ad11SSeven Lee { NAU8821_R1D_I2S_PCM_CTRL2, 0x8010 }, 149aab1ad11SSeven Lee { NAU8821_R1E_LEFT_TIME_SLOT, 0x0 }, 150aab1ad11SSeven Lee { NAU8821_R1F_RIGHT_TIME_SLOT, 0x0 }, 151aab1ad11SSeven Lee { NAU8821_R21_BIQ0_COF1, 0x0 }, 152aab1ad11SSeven Lee { NAU8821_R22_BIQ0_COF2, 0x0 }, 153aab1ad11SSeven Lee { NAU8821_R23_BIQ0_COF3, 0x0 }, 154aab1ad11SSeven Lee { NAU8821_R24_BIQ0_COF4, 0x0 }, 155aab1ad11SSeven Lee { NAU8821_R25_BIQ0_COF5, 0x0 }, 156aab1ad11SSeven Lee { NAU8821_R26_BIQ0_COF6, 0x0 }, 157aab1ad11SSeven Lee { NAU8821_R27_BIQ0_COF7, 0x0 }, 158aab1ad11SSeven Lee { NAU8821_R28_BIQ0_COF8, 0x0 }, 159aab1ad11SSeven Lee { NAU8821_R29_BIQ0_COF9, 0x0 }, 160aab1ad11SSeven Lee { NAU8821_R2A_BIQ0_COF10, 0x0 }, 161aab1ad11SSeven Lee { NAU8821_R2B_ADC_RATE, 0x0002 }, 162aab1ad11SSeven Lee { NAU8821_R2C_DAC_CTRL1, 0x0082 }, 163aab1ad11SSeven Lee { NAU8821_R2D_DAC_CTRL2, 0x0 }, 164aab1ad11SSeven Lee { NAU8821_R2F_DAC_DGAIN_CTRL, 0x0 }, 165aab1ad11SSeven Lee { NAU8821_R30_ADC_DGAIN_CTRL, 0x0 }, 166aab1ad11SSeven Lee { NAU8821_R31_MUTE_CTRL, 0x0 }, 167aab1ad11SSeven Lee { NAU8821_R32_HSVOL_CTRL, 0x0 }, 168aab1ad11SSeven Lee { NAU8821_R34_DACR_CTRL, 0xcfcf }, 169aab1ad11SSeven Lee { NAU8821_R35_ADC_DGAIN_CTRL1, 0xcfcf }, 170aab1ad11SSeven Lee { NAU8821_R36_ADC_DRC_KNEE_IP12, 0x1486 }, 171aab1ad11SSeven Lee { NAU8821_R37_ADC_DRC_KNEE_IP34, 0x0f12 }, 172aab1ad11SSeven Lee { NAU8821_R38_ADC_DRC_SLOPES, 0x25ff }, 173aab1ad11SSeven Lee { NAU8821_R39_ADC_DRC_ATKDCY, 0x3457 }, 174aab1ad11SSeven Lee { NAU8821_R3A_DAC_DRC_KNEE_IP12, 0x1486 }, 175aab1ad11SSeven Lee { NAU8821_R3B_DAC_DRC_KNEE_IP34, 0x0f12 }, 176aab1ad11SSeven Lee { NAU8821_R3C_DAC_DRC_SLOPES, 0x25f9 }, 177aab1ad11SSeven Lee { NAU8821_R3D_DAC_DRC_ATKDCY, 0x3457 }, 178aab1ad11SSeven Lee { NAU8821_R41_BIQ1_COF1, 0x0 }, 179aab1ad11SSeven Lee { NAU8821_R42_BIQ1_COF2, 0x0 }, 180aab1ad11SSeven Lee { NAU8821_R43_BIQ1_COF3, 0x0 }, 181aab1ad11SSeven Lee { NAU8821_R44_BIQ1_COF4, 0x0 }, 182aab1ad11SSeven Lee { NAU8821_R45_BIQ1_COF5, 0x0 }, 183aab1ad11SSeven Lee { NAU8821_R46_BIQ1_COF6, 0x0 }, 184aab1ad11SSeven Lee { NAU8821_R47_BIQ1_COF7, 0x0 }, 185aab1ad11SSeven Lee { NAU8821_R48_BIQ1_COF8, 0x0 }, 186aab1ad11SSeven Lee { NAU8821_R49_BIQ1_COF9, 0x0 }, 187aab1ad11SSeven Lee { NAU8821_R4A_BIQ1_COF10, 0x0 }, 188aab1ad11SSeven Lee { NAU8821_R4B_CLASSG_CTRL, 0x0 }, 189aab1ad11SSeven Lee { NAU8821_R4C_IMM_MODE_CTRL, 0x0 }, 190aab1ad11SSeven Lee { NAU8821_R4D_IMM_RMS_L, 0x0 }, 191aab1ad11SSeven Lee { NAU8821_R53_OTPDOUT_1, 0xaad8 }, 192aab1ad11SSeven Lee { NAU8821_R54_OTPDOUT_2, 0x0002 }, 193aab1ad11SSeven Lee { NAU8821_R55_MISC_CTRL, 0x0 }, 194aab1ad11SSeven Lee { NAU8821_R66_BIAS_ADJ, 0x0 }, 195aab1ad11SSeven Lee { NAU8821_R68_TRIM_SETTINGS, 0x0 }, 196aab1ad11SSeven Lee { NAU8821_R69_ANALOG_CONTROL_1, 0x0 }, 197aab1ad11SSeven Lee { NAU8821_R6A_ANALOG_CONTROL_2, 0x0 }, 198aab1ad11SSeven Lee { NAU8821_R6B_PGA_MUTE, 0x0 }, 199aab1ad11SSeven Lee { NAU8821_R71_ANALOG_ADC_1, 0x0011 }, 200aab1ad11SSeven Lee { NAU8821_R72_ANALOG_ADC_2, 0x0020 }, 201aab1ad11SSeven Lee { NAU8821_R73_RDAC, 0x0008 }, 202aab1ad11SSeven Lee { NAU8821_R74_MIC_BIAS, 0x0006 }, 203aab1ad11SSeven Lee { NAU8821_R76_BOOST, 0x0 }, 204aab1ad11SSeven Lee { NAU8821_R77_FEPGA, 0x0 }, 205aab1ad11SSeven Lee { NAU8821_R7E_PGA_GAIN, 0x0 }, 206aab1ad11SSeven Lee { NAU8821_R7F_POWER_UP_CONTROL, 0x0 }, 207aab1ad11SSeven Lee { NAU8821_R80_CHARGE_PUMP, 0x0 }, 208aab1ad11SSeven Lee }; 209aab1ad11SSeven Lee 210aab1ad11SSeven Lee static bool nau8821_readable_reg(struct device *dev, unsigned int reg) 211aab1ad11SSeven Lee { 212aab1ad11SSeven Lee switch (reg) { 213aab1ad11SSeven Lee case NAU8821_R00_RESET ... NAU8821_R01_ENA_CTRL: 214aab1ad11SSeven Lee case NAU8821_R03_CLK_DIVIDER ... NAU8821_R0B_FLL8: 215aab1ad11SSeven Lee case NAU8821_R0D_JACK_DET_CTRL: 216aab1ad11SSeven Lee case NAU8821_R0F_INTERRUPT_MASK ... NAU8821_R13_DMIC_CTRL: 217aab1ad11SSeven Lee case NAU8821_R1A_GPIO12_CTRL ... NAU8821_R1F_RIGHT_TIME_SLOT: 218aab1ad11SSeven Lee case NAU8821_R21_BIQ0_COF1 ... NAU8821_R2D_DAC_CTRL2: 219aab1ad11SSeven Lee case NAU8821_R2F_DAC_DGAIN_CTRL ... NAU8821_R32_HSVOL_CTRL: 220aab1ad11SSeven Lee case NAU8821_R34_DACR_CTRL ... NAU8821_R3D_DAC_DRC_ATKDCY: 221aab1ad11SSeven Lee case NAU8821_R41_BIQ1_COF1 ... NAU8821_R4F_FUSE_CTRL3: 222aab1ad11SSeven Lee case NAU8821_R51_FUSE_CTRL1: 223aab1ad11SSeven Lee case NAU8821_R53_OTPDOUT_1 ... NAU8821_R55_MISC_CTRL: 224aab1ad11SSeven Lee case NAU8821_R58_I2C_DEVICE_ID ... NAU8821_R5A_SOFTWARE_RST: 225aab1ad11SSeven Lee case NAU8821_R66_BIAS_ADJ: 226aab1ad11SSeven Lee case NAU8821_R68_TRIM_SETTINGS ... NAU8821_R6B_PGA_MUTE: 227aab1ad11SSeven Lee case NAU8821_R71_ANALOG_ADC_1 ... NAU8821_R74_MIC_BIAS: 228aab1ad11SSeven Lee case NAU8821_R76_BOOST ... NAU8821_R77_FEPGA: 229aab1ad11SSeven Lee case NAU8821_R7E_PGA_GAIN ... NAU8821_R82_GENERAL_STATUS: 230aab1ad11SSeven Lee return true; 231aab1ad11SSeven Lee default: 232aab1ad11SSeven Lee return false; 233aab1ad11SSeven Lee } 234aab1ad11SSeven Lee } 235aab1ad11SSeven Lee 236aab1ad11SSeven Lee static bool nau8821_writeable_reg(struct device *dev, unsigned int reg) 237aab1ad11SSeven Lee { 238aab1ad11SSeven Lee switch (reg) { 239aab1ad11SSeven Lee case NAU8821_R00_RESET ... NAU8821_R01_ENA_CTRL: 240aab1ad11SSeven Lee case NAU8821_R03_CLK_DIVIDER ... NAU8821_R0B_FLL8: 241aab1ad11SSeven Lee case NAU8821_R0D_JACK_DET_CTRL: 242aab1ad11SSeven Lee case NAU8821_R0F_INTERRUPT_MASK: 243aab1ad11SSeven Lee case NAU8821_R11_INT_CLR_KEY_STATUS ... NAU8821_R13_DMIC_CTRL: 244aab1ad11SSeven Lee case NAU8821_R1A_GPIO12_CTRL ... NAU8821_R1F_RIGHT_TIME_SLOT: 245aab1ad11SSeven Lee case NAU8821_R21_BIQ0_COF1 ... NAU8821_R2D_DAC_CTRL2: 246aab1ad11SSeven Lee case NAU8821_R2F_DAC_DGAIN_CTRL ... NAU8821_R32_HSVOL_CTRL: 247aab1ad11SSeven Lee case NAU8821_R34_DACR_CTRL ... NAU8821_R3D_DAC_DRC_ATKDCY: 248aab1ad11SSeven Lee case NAU8821_R41_BIQ1_COF1 ... NAU8821_R4C_IMM_MODE_CTRL: 249aab1ad11SSeven Lee case NAU8821_R4E_FUSE_CTRL2 ... NAU8821_R4F_FUSE_CTRL3: 250aab1ad11SSeven Lee case NAU8821_R51_FUSE_CTRL1: 251aab1ad11SSeven Lee case NAU8821_R55_MISC_CTRL: 252aab1ad11SSeven Lee case NAU8821_R5A_SOFTWARE_RST: 253aab1ad11SSeven Lee case NAU8821_R66_BIAS_ADJ: 254aab1ad11SSeven Lee case NAU8821_R68_TRIM_SETTINGS ... NAU8821_R6B_PGA_MUTE: 255aab1ad11SSeven Lee case NAU8821_R71_ANALOG_ADC_1 ... NAU8821_R74_MIC_BIAS: 256aab1ad11SSeven Lee case NAU8821_R76_BOOST ... NAU8821_R77_FEPGA: 257aab1ad11SSeven Lee case NAU8821_R7E_PGA_GAIN ... NAU8821_R80_CHARGE_PUMP: 258aab1ad11SSeven Lee return true; 259aab1ad11SSeven Lee default: 260aab1ad11SSeven Lee return false; 261aab1ad11SSeven Lee } 262aab1ad11SSeven Lee } 263aab1ad11SSeven Lee 264aab1ad11SSeven Lee static bool nau8821_volatile_reg(struct device *dev, unsigned int reg) 265aab1ad11SSeven Lee { 266aab1ad11SSeven Lee switch (reg) { 267aab1ad11SSeven Lee case NAU8821_R00_RESET: 268aab1ad11SSeven Lee case NAU8821_R10_IRQ_STATUS ... NAU8821_R11_INT_CLR_KEY_STATUS: 269aab1ad11SSeven Lee case NAU8821_R21_BIQ0_COF1 ... NAU8821_R2A_BIQ0_COF10: 270aab1ad11SSeven Lee case NAU8821_R41_BIQ1_COF1 ... NAU8821_R4A_BIQ1_COF10: 271aab1ad11SSeven Lee case NAU8821_R4D_IMM_RMS_L: 272aab1ad11SSeven Lee case NAU8821_R53_OTPDOUT_1 ... NAU8821_R54_OTPDOUT_2: 273aab1ad11SSeven Lee case NAU8821_R58_I2C_DEVICE_ID ... NAU8821_R5A_SOFTWARE_RST: 274aab1ad11SSeven Lee case NAU8821_R81_CHARGE_PUMP_INPUT_READ ... NAU8821_R82_GENERAL_STATUS: 275aab1ad11SSeven Lee return true; 276aab1ad11SSeven Lee default: 277aab1ad11SSeven Lee return false; 278aab1ad11SSeven Lee } 279aab1ad11SSeven Lee } 280aab1ad11SSeven Lee 281aab1ad11SSeven Lee static int nau8821_biq_coeff_get(struct snd_kcontrol *kcontrol, 282aab1ad11SSeven Lee struct snd_ctl_elem_value *ucontrol) 283aab1ad11SSeven Lee { 284aab1ad11SSeven Lee struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 285aab1ad11SSeven Lee struct soc_bytes_ext *params = (void *)kcontrol->private_value; 286aab1ad11SSeven Lee 287aab1ad11SSeven Lee if (!component->regmap) 288aab1ad11SSeven Lee return -EINVAL; 289aab1ad11SSeven Lee 290aab1ad11SSeven Lee regmap_raw_read(component->regmap, NAU8821_R21_BIQ0_COF1, 291aab1ad11SSeven Lee ucontrol->value.bytes.data, params->max); 292aab1ad11SSeven Lee 293aab1ad11SSeven Lee return 0; 294aab1ad11SSeven Lee } 295aab1ad11SSeven Lee 296aab1ad11SSeven Lee static int nau8821_biq_coeff_put(struct snd_kcontrol *kcontrol, 297aab1ad11SSeven Lee struct snd_ctl_elem_value *ucontrol) 298aab1ad11SSeven Lee { 299aab1ad11SSeven Lee struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 300aab1ad11SSeven Lee struct soc_bytes_ext *params = (void *)kcontrol->private_value; 301aab1ad11SSeven Lee void *data; 302aab1ad11SSeven Lee 303aab1ad11SSeven Lee if (!component->regmap) 304aab1ad11SSeven Lee return -EINVAL; 305aab1ad11SSeven Lee 306aab1ad11SSeven Lee data = kmemdup(ucontrol->value.bytes.data, 307aab1ad11SSeven Lee params->max, GFP_KERNEL | GFP_DMA); 308aab1ad11SSeven Lee if (!data) 309aab1ad11SSeven Lee return -ENOMEM; 310aab1ad11SSeven Lee 311aab1ad11SSeven Lee regmap_raw_write(component->regmap, NAU8821_R21_BIQ0_COF1, 312aab1ad11SSeven Lee data, params->max); 313aab1ad11SSeven Lee 314aab1ad11SSeven Lee kfree(data); 315aab1ad11SSeven Lee 316aab1ad11SSeven Lee return 0; 317aab1ad11SSeven Lee } 318aab1ad11SSeven Lee 319aab1ad11SSeven Lee static const char * const nau8821_adc_decimation[] = { 320aab1ad11SSeven Lee "32", "64", "128", "256" }; 321aab1ad11SSeven Lee 322aab1ad11SSeven Lee static const struct soc_enum nau8821_adc_decimation_enum = 323aab1ad11SSeven Lee SOC_ENUM_SINGLE(NAU8821_R2B_ADC_RATE, NAU8821_ADC_SYNC_DOWN_SFT, 324aab1ad11SSeven Lee ARRAY_SIZE(nau8821_adc_decimation), nau8821_adc_decimation); 325aab1ad11SSeven Lee 326aab1ad11SSeven Lee static const char * const nau8821_dac_oversampl[] = { 327aab1ad11SSeven Lee "64", "256", "128", "", "32" }; 328aab1ad11SSeven Lee 329aab1ad11SSeven Lee static const struct soc_enum nau8821_dac_oversampl_enum = 330aab1ad11SSeven Lee SOC_ENUM_SINGLE(NAU8821_R2C_DAC_CTRL1, NAU8821_DAC_OVERSAMPLE_SFT, 331aab1ad11SSeven Lee ARRAY_SIZE(nau8821_dac_oversampl), nau8821_dac_oversampl); 332aab1ad11SSeven Lee 333cd01b5f0SSeven Lee static const char * const nau8821_adc_drc_noise_gate[] = { 334cd01b5f0SSeven Lee "1:1", "2:1", "4:1", "8:1" }; 335cd01b5f0SSeven Lee 336cd01b5f0SSeven Lee static const struct soc_enum nau8821_adc_drc_noise_gate_enum = 337cd01b5f0SSeven Lee SOC_ENUM_SINGLE(NAU8821_R38_ADC_DRC_SLOPES, NAU8821_DRC_NG_SLP_ADC_SFT, 338cd01b5f0SSeven Lee ARRAY_SIZE(nau8821_adc_drc_noise_gate), 339cd01b5f0SSeven Lee nau8821_adc_drc_noise_gate); 340cd01b5f0SSeven Lee 341cd01b5f0SSeven Lee static const char * const nau8821_adc_drc_expansion_slope[] = { 342cd01b5f0SSeven Lee "1:1", "2:1", "4:1" }; 343cd01b5f0SSeven Lee 344cd01b5f0SSeven Lee static const struct soc_enum nau8821_adc_drc_expansion_slope_enum = 345cd01b5f0SSeven Lee SOC_ENUM_SINGLE(NAU8821_R38_ADC_DRC_SLOPES, NAU8821_DRC_EXP_SLP_ADC_SFT, 346cd01b5f0SSeven Lee ARRAY_SIZE(nau8821_adc_drc_expansion_slope), 347cd01b5f0SSeven Lee nau8821_adc_drc_expansion_slope); 348cd01b5f0SSeven Lee 349cd01b5f0SSeven Lee static const char * const nau8821_adc_drc_lower_region[] = { 350cd01b5f0SSeven Lee "0", "1:2", "1:4", "1:8", "1:16", "", "", "1:1" }; 351cd01b5f0SSeven Lee 352cd01b5f0SSeven Lee static const struct soc_enum nau8821_adc_drc_lower_region_enum = 353cd01b5f0SSeven Lee SOC_ENUM_SINGLE(NAU8821_R38_ADC_DRC_SLOPES, 354cd01b5f0SSeven Lee NAU8821_DRC_CMP2_SLP_ADC_SFT, 355cd01b5f0SSeven Lee ARRAY_SIZE(nau8821_adc_drc_lower_region), 356cd01b5f0SSeven Lee nau8821_adc_drc_lower_region); 357cd01b5f0SSeven Lee 358cd01b5f0SSeven Lee static const char * const nau8821_higher_region[] = { 359cd01b5f0SSeven Lee "0", "1:2", "1:4", "1:8", "1:16", "", "", "1:1" }; 360cd01b5f0SSeven Lee 361cd01b5f0SSeven Lee static const struct soc_enum nau8821_higher_region_enum = 362cd01b5f0SSeven Lee SOC_ENUM_SINGLE(NAU8821_R38_ADC_DRC_SLOPES, 363cd01b5f0SSeven Lee NAU8821_DRC_CMP1_SLP_ADC_SFT, 364cd01b5f0SSeven Lee ARRAY_SIZE(nau8821_higher_region), 365cd01b5f0SSeven Lee nau8821_higher_region); 366cd01b5f0SSeven Lee 367cd01b5f0SSeven Lee static const char * const nau8821_limiter_slope[] = { 368cd01b5f0SSeven Lee "0", "1:2", "1:4", "1:8", "1:16", "1:32", "1:64", "1:1" }; 369cd01b5f0SSeven Lee 370cd01b5f0SSeven Lee static const struct soc_enum nau8821_limiter_slope_enum = 371cd01b5f0SSeven Lee SOC_ENUM_SINGLE(NAU8821_R38_ADC_DRC_SLOPES, 372cd01b5f0SSeven Lee NAU8821_DRC_LMT_SLP_ADC_SFT, ARRAY_SIZE(nau8821_limiter_slope), 373cd01b5f0SSeven Lee nau8821_limiter_slope); 374cd01b5f0SSeven Lee 375cd01b5f0SSeven Lee static const char * const nau8821_detection_attack_time[] = { 376cd01b5f0SSeven Lee "Ts", "3Ts", "7Ts", "15Ts", "31Ts", "63Ts", "127Ts", "255Ts", 377cd01b5f0SSeven Lee "", "511Ts" }; 378cd01b5f0SSeven Lee 379cd01b5f0SSeven Lee static const struct soc_enum nau8821_detection_attack_time_enum = 380cd01b5f0SSeven Lee SOC_ENUM_SINGLE(NAU8821_R39_ADC_DRC_ATKDCY, 381cd01b5f0SSeven Lee NAU8821_DRC_PK_COEF1_ADC_SFT, 382cd01b5f0SSeven Lee ARRAY_SIZE(nau8821_detection_attack_time), 383cd01b5f0SSeven Lee nau8821_detection_attack_time); 384cd01b5f0SSeven Lee 385cd01b5f0SSeven Lee static const char * const nau8821_detection_release_time[] = { 386cd01b5f0SSeven Lee "63Ts", "127Ts", "255Ts", "511Ts", "1023Ts", "2047Ts", "4095Ts", 387cd01b5f0SSeven Lee "8191Ts", "", "16383Ts" }; 388cd01b5f0SSeven Lee 389cd01b5f0SSeven Lee static const struct soc_enum nau8821_detection_release_time_enum = 390cd01b5f0SSeven Lee SOC_ENUM_SINGLE(NAU8821_R39_ADC_DRC_ATKDCY, 391cd01b5f0SSeven Lee NAU8821_DRC_PK_COEF2_ADC_SFT, 392cd01b5f0SSeven Lee ARRAY_SIZE(nau8821_detection_release_time), 393cd01b5f0SSeven Lee nau8821_detection_release_time); 394cd01b5f0SSeven Lee 395cd01b5f0SSeven Lee static const char * const nau8821_attack_time[] = { 396cd01b5f0SSeven Lee "Ts", "3Ts", "7Ts", "15Ts", "31Ts", "63Ts", "127Ts", "255Ts", 397cd01b5f0SSeven Lee "511Ts", "1023Ts", "2047Ts", "4095Ts", "8191Ts" }; 398cd01b5f0SSeven Lee 399cd01b5f0SSeven Lee static const struct soc_enum nau8821_attack_time_enum = 400cd01b5f0SSeven Lee SOC_ENUM_SINGLE(NAU8821_R39_ADC_DRC_ATKDCY, NAU8821_DRC_ATK_ADC_SFT, 401cd01b5f0SSeven Lee ARRAY_SIZE(nau8821_attack_time), nau8821_attack_time); 402cd01b5f0SSeven Lee 403cd01b5f0SSeven Lee static const char * const nau8821_decay_time[] = { 404cd01b5f0SSeven Lee "63Ts", "127Ts", "255Ts", "511Ts", "1023Ts", "2047Ts", "4095Ts", 405cd01b5f0SSeven Lee "8191Ts", "16383Ts", "32757Ts", "65535Ts" }; 406cd01b5f0SSeven Lee 407cd01b5f0SSeven Lee static const struct soc_enum nau8821_decay_time_enum = 408cd01b5f0SSeven Lee SOC_ENUM_SINGLE(NAU8821_R39_ADC_DRC_ATKDCY, NAU8821_DRC_DCY_ADC_SFT, 409cd01b5f0SSeven Lee ARRAY_SIZE(nau8821_decay_time), nau8821_decay_time); 410cd01b5f0SSeven Lee 411aab1ad11SSeven Lee static const DECLARE_TLV_DB_MINMAX_MUTE(adc_vol_tlv, -6600, 2400); 412aab1ad11SSeven Lee static const DECLARE_TLV_DB_MINMAX_MUTE(sidetone_vol_tlv, -4200, 0); 413aab1ad11SSeven Lee static const DECLARE_TLV_DB_MINMAX(hp_vol_tlv, -900, 0); 414aab1ad11SSeven Lee static const DECLARE_TLV_DB_SCALE(playback_vol_tlv, -6600, 50, 1); 415aab1ad11SSeven Lee static const DECLARE_TLV_DB_MINMAX(fepga_gain_tlv, -100, 3600); 416aab1ad11SSeven Lee static const DECLARE_TLV_DB_MINMAX_MUTE(crosstalk_vol_tlv, -7000, 2400); 417cd01b5f0SSeven Lee static const DECLARE_TLV_DB_MINMAX(drc_knee4_tlv, -9800, -3500); 418cd01b5f0SSeven Lee static const DECLARE_TLV_DB_MINMAX(drc_knee3_tlv, -8100, -1800); 419aab1ad11SSeven Lee 420aab1ad11SSeven Lee static const struct snd_kcontrol_new nau8821_controls[] = { 421aab1ad11SSeven Lee SOC_DOUBLE_TLV("Mic Volume", NAU8821_R35_ADC_DGAIN_CTRL1, 422aab1ad11SSeven Lee NAU8821_ADCL_CH_VOL_SFT, NAU8821_ADCR_CH_VOL_SFT, 423aab1ad11SSeven Lee 0xff, 0, adc_vol_tlv), 424aab1ad11SSeven Lee SOC_DOUBLE_TLV("Headphone Bypass Volume", NAU8821_R30_ADC_DGAIN_CTRL, 425aab1ad11SSeven Lee 12, 8, 0x0f, 0, sidetone_vol_tlv), 426aab1ad11SSeven Lee SOC_DOUBLE_TLV("Headphone Volume", NAU8821_R32_HSVOL_CTRL, 427aab1ad11SSeven Lee NAU8821_HPL_VOL_SFT, NAU8821_HPR_VOL_SFT, 0x3, 1, hp_vol_tlv), 428aab1ad11SSeven Lee SOC_DOUBLE_TLV("Digital Playback Volume", NAU8821_R34_DACR_CTRL, 429aab1ad11SSeven Lee NAU8821_DACL_CH_VOL_SFT, NAU8821_DACR_CH_VOL_SFT, 430aab1ad11SSeven Lee 0xcf, 0, playback_vol_tlv), 431aab1ad11SSeven Lee SOC_DOUBLE_TLV("Frontend PGA Volume", NAU8821_R7E_PGA_GAIN, 432aab1ad11SSeven Lee NAU8821_PGA_GAIN_L_SFT, NAU8821_PGA_GAIN_R_SFT, 433aab1ad11SSeven Lee 37, 0, fepga_gain_tlv), 434aab1ad11SSeven Lee SOC_DOUBLE_TLV("Headphone Crosstalk Volume", 435aab1ad11SSeven Lee NAU8821_R2F_DAC_DGAIN_CTRL, 436aab1ad11SSeven Lee 0, 8, 0xff, 0, crosstalk_vol_tlv), 437cd01b5f0SSeven Lee SOC_SINGLE_TLV("ADC DRC KNEE4", NAU8821_R37_ADC_DRC_KNEE_IP34, 438cd01b5f0SSeven Lee NAU8821_DRC_KNEE4_IP_ADC_SFT, 0x3f, 1, drc_knee4_tlv), 439cd01b5f0SSeven Lee SOC_SINGLE_TLV("ADC DRC KNEE3", NAU8821_R37_ADC_DRC_KNEE_IP34, 440cd01b5f0SSeven Lee NAU8821_DRC_KNEE3_IP_ADC_SFT, 0x3f, 1, drc_knee3_tlv), 441cd01b5f0SSeven Lee 442cd01b5f0SSeven Lee SOC_ENUM("ADC DRC Noise Gate", nau8821_adc_drc_noise_gate_enum), 443cd01b5f0SSeven Lee SOC_ENUM("ADC DRC Expansion Slope", nau8821_adc_drc_expansion_slope_enum), 444cd01b5f0SSeven Lee SOC_ENUM("ADC DRC Lower Region", nau8821_adc_drc_lower_region_enum), 445cd01b5f0SSeven Lee SOC_ENUM("ADC DRC Higher Region", nau8821_higher_region_enum), 446cd01b5f0SSeven Lee SOC_ENUM("ADC DRC Limiter Slope", nau8821_limiter_slope_enum), 447cd01b5f0SSeven Lee SOC_ENUM("ADC DRC Peak Detection Attack Time", nau8821_detection_attack_time_enum), 448cd01b5f0SSeven Lee SOC_ENUM("ADC DRC Peak Detection Release Time", nau8821_detection_release_time_enum), 449cd01b5f0SSeven Lee SOC_ENUM("ADC DRC Attack Time", nau8821_attack_time_enum), 450cd01b5f0SSeven Lee SOC_ENUM("ADC DRC Decay Time", nau8821_decay_time_enum), 451cd01b5f0SSeven Lee SOC_SINGLE("DRC Enable Switch", NAU8821_R36_ADC_DRC_KNEE_IP12, 452cd01b5f0SSeven Lee NAU8821_DRC_ENA_ADC_SFT, 1, 0), 453aab1ad11SSeven Lee 454aab1ad11SSeven Lee SOC_ENUM("ADC Decimation Rate", nau8821_adc_decimation_enum), 455aab1ad11SSeven Lee SOC_ENUM("DAC Oversampling Rate", nau8821_dac_oversampl_enum), 456aab1ad11SSeven Lee SND_SOC_BYTES_EXT("BIQ Coefficients", 20, 457aab1ad11SSeven Lee nau8821_biq_coeff_get, nau8821_biq_coeff_put), 458aab1ad11SSeven Lee SOC_SINGLE("ADC Phase Switch", NAU8821_R1B_TDM_CTRL, 459aab1ad11SSeven Lee NAU8821_ADCPHS_SFT, 1, 0), 460aab1ad11SSeven Lee }; 461aab1ad11SSeven Lee 462aab1ad11SSeven Lee static const struct snd_kcontrol_new nau8821_dmic_mode_switch = 463aab1ad11SSeven Lee SOC_DAPM_SINGLE("Switch", NAU8821_R13_DMIC_CTRL, 464aab1ad11SSeven Lee NAU8821_DMIC_EN_SFT, 1, 0); 465aab1ad11SSeven Lee 466aab1ad11SSeven Lee static int dmic_clock_control(struct snd_soc_dapm_widget *w, 467aab1ad11SSeven Lee struct snd_kcontrol *k, int event) 468aab1ad11SSeven Lee { 469aab1ad11SSeven Lee struct snd_soc_component *component = 470aab1ad11SSeven Lee snd_soc_dapm_to_component(w->dapm); 471aab1ad11SSeven Lee struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 472aab1ad11SSeven Lee int i, speed_selection = -1, clk_adc_src, clk_adc; 473aab1ad11SSeven Lee unsigned int clk_divider_r03; 474aab1ad11SSeven Lee 475aab1ad11SSeven Lee /* The DMIC clock is gotten from adc clock divided by 476aab1ad11SSeven Lee * CLK_DMIC_SRC (1, 2, 4, 8). The clock has to be equal or 477aab1ad11SSeven Lee * less than nau8821->dmic_clk_threshold. 478aab1ad11SSeven Lee */ 479aab1ad11SSeven Lee regmap_read(nau8821->regmap, NAU8821_R03_CLK_DIVIDER, 480aab1ad11SSeven Lee &clk_divider_r03); 481aab1ad11SSeven Lee clk_adc_src = (clk_divider_r03 & NAU8821_CLK_ADC_SRC_MASK) 482aab1ad11SSeven Lee >> NAU8821_CLK_ADC_SRC_SFT; 483aab1ad11SSeven Lee clk_adc = (nau8821->fs * 256) >> clk_adc_src; 484aab1ad11SSeven Lee 485aab1ad11SSeven Lee for (i = 0 ; i < 4 ; i++) 486aab1ad11SSeven Lee if ((clk_adc >> dmic_speed_sel[i].param) <= 487aab1ad11SSeven Lee nau8821->dmic_clk_threshold) { 488aab1ad11SSeven Lee speed_selection = dmic_speed_sel[i].val; 489aab1ad11SSeven Lee break; 490aab1ad11SSeven Lee } 49146ae0b3fSPierre-Louis Bossart if (i == 4) 492aab1ad11SSeven Lee return -EINVAL; 493aab1ad11SSeven Lee 494aab1ad11SSeven Lee dev_dbg(nau8821->dev, 495aab1ad11SSeven Lee "clk_adc=%d, dmic_clk_threshold = %d, param=%d, val = %d\n", 496aab1ad11SSeven Lee clk_adc, nau8821->dmic_clk_threshold, 497aab1ad11SSeven Lee dmic_speed_sel[i].param, dmic_speed_sel[i].val); 498aab1ad11SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R13_DMIC_CTRL, 499aab1ad11SSeven Lee NAU8821_DMIC_SRC_MASK, 500aab1ad11SSeven Lee (speed_selection << NAU8821_DMIC_SRC_SFT)); 501aab1ad11SSeven Lee 502aab1ad11SSeven Lee return 0; 503aab1ad11SSeven Lee } 504aab1ad11SSeven Lee 505aab1ad11SSeven Lee static int nau8821_left_adc_event(struct snd_soc_dapm_widget *w, 506aab1ad11SSeven Lee struct snd_kcontrol *kcontrol, int event) 507aab1ad11SSeven Lee { 508aab1ad11SSeven Lee struct snd_soc_component *component = 509aab1ad11SSeven Lee snd_soc_dapm_to_component(w->dapm); 510aab1ad11SSeven Lee struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 511aab1ad11SSeven Lee 512aab1ad11SSeven Lee switch (event) { 513aab1ad11SSeven Lee case SND_SOC_DAPM_POST_PMU: 514*b37fdd42SSeven Lee msleep(nau8821->adc_delay); 515aab1ad11SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R01_ENA_CTRL, 516aab1ad11SSeven Lee NAU8821_EN_ADCL, NAU8821_EN_ADCL); 517aab1ad11SSeven Lee break; 518aab1ad11SSeven Lee case SND_SOC_DAPM_POST_PMD: 519aab1ad11SSeven Lee regmap_update_bits(nau8821->regmap, 520aab1ad11SSeven Lee NAU8821_R01_ENA_CTRL, NAU8821_EN_ADCL, 0); 521aab1ad11SSeven Lee break; 522aab1ad11SSeven Lee default: 523aab1ad11SSeven Lee return -EINVAL; 524aab1ad11SSeven Lee } 525aab1ad11SSeven Lee 526aab1ad11SSeven Lee return 0; 527aab1ad11SSeven Lee } 528aab1ad11SSeven Lee 529aab1ad11SSeven Lee static int nau8821_right_adc_event(struct snd_soc_dapm_widget *w, 530aab1ad11SSeven Lee struct snd_kcontrol *kcontrol, int event) 531aab1ad11SSeven Lee { 532aab1ad11SSeven Lee struct snd_soc_component *component = 533aab1ad11SSeven Lee snd_soc_dapm_to_component(w->dapm); 534aab1ad11SSeven Lee struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 535aab1ad11SSeven Lee 536aab1ad11SSeven Lee switch (event) { 537aab1ad11SSeven Lee case SND_SOC_DAPM_POST_PMU: 538*b37fdd42SSeven Lee msleep(nau8821->adc_delay); 539aab1ad11SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R01_ENA_CTRL, 540aab1ad11SSeven Lee NAU8821_EN_ADCR, NAU8821_EN_ADCR); 541aab1ad11SSeven Lee break; 542aab1ad11SSeven Lee case SND_SOC_DAPM_POST_PMD: 543aab1ad11SSeven Lee regmap_update_bits(nau8821->regmap, 544aab1ad11SSeven Lee NAU8821_R01_ENA_CTRL, NAU8821_EN_ADCR, 0); 545aab1ad11SSeven Lee break; 546aab1ad11SSeven Lee default: 547aab1ad11SSeven Lee return -EINVAL; 548aab1ad11SSeven Lee } 549aab1ad11SSeven Lee 550aab1ad11SSeven Lee return 0; 551aab1ad11SSeven Lee } 552aab1ad11SSeven Lee 553aab1ad11SSeven Lee static int nau8821_pump_event(struct snd_soc_dapm_widget *w, 554aab1ad11SSeven Lee struct snd_kcontrol *kcontrol, int event) 555aab1ad11SSeven Lee { 556aab1ad11SSeven Lee struct snd_soc_component *component = 557aab1ad11SSeven Lee snd_soc_dapm_to_component(w->dapm); 558aab1ad11SSeven Lee struct nau8821 *nau8821 = 559aab1ad11SSeven Lee snd_soc_component_get_drvdata(component); 560aab1ad11SSeven Lee 561aab1ad11SSeven Lee switch (event) { 562aab1ad11SSeven Lee case SND_SOC_DAPM_POST_PMU: 563aab1ad11SSeven Lee /* Prevent startup click by letting charge pump to ramp up */ 564aab1ad11SSeven Lee msleep(20); 565aab1ad11SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R80_CHARGE_PUMP, 566aab1ad11SSeven Lee NAU8821_JAMNODCLOW, NAU8821_JAMNODCLOW); 567aab1ad11SSeven Lee break; 568aab1ad11SSeven Lee case SND_SOC_DAPM_PRE_PMD: 569aab1ad11SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R80_CHARGE_PUMP, 570aab1ad11SSeven Lee NAU8821_JAMNODCLOW, 0); 571aab1ad11SSeven Lee break; 572aab1ad11SSeven Lee default: 573aab1ad11SSeven Lee return -EINVAL; 574aab1ad11SSeven Lee } 575aab1ad11SSeven Lee 576aab1ad11SSeven Lee return 0; 577aab1ad11SSeven Lee } 578aab1ad11SSeven Lee 579aab1ad11SSeven Lee static int nau8821_output_dac_event(struct snd_soc_dapm_widget *w, 580aab1ad11SSeven Lee struct snd_kcontrol *kcontrol, int event) 581aab1ad11SSeven Lee { 582aab1ad11SSeven Lee struct snd_soc_component *component = 583aab1ad11SSeven Lee snd_soc_dapm_to_component(w->dapm); 584aab1ad11SSeven Lee struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 585aab1ad11SSeven Lee 586aab1ad11SSeven Lee switch (event) { 587aab1ad11SSeven Lee case SND_SOC_DAPM_PRE_PMU: 588aab1ad11SSeven Lee /* Disables the TESTDAC to let DAC signal pass through. */ 589aab1ad11SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R66_BIAS_ADJ, 590aab1ad11SSeven Lee NAU8821_BIAS_TESTDAC_EN, 0); 591aab1ad11SSeven Lee break; 592aab1ad11SSeven Lee case SND_SOC_DAPM_POST_PMD: 593aab1ad11SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R66_BIAS_ADJ, 594aab1ad11SSeven Lee NAU8821_BIAS_TESTDAC_EN, NAU8821_BIAS_TESTDAC_EN); 595aab1ad11SSeven Lee break; 596aab1ad11SSeven Lee default: 597aab1ad11SSeven Lee return -EINVAL; 598aab1ad11SSeven Lee } 599aab1ad11SSeven Lee 600aab1ad11SSeven Lee return 0; 601aab1ad11SSeven Lee } 602aab1ad11SSeven Lee 6030cf470c0SWallace Lin static int system_clock_control(struct snd_soc_dapm_widget *w, 6040cf470c0SWallace Lin struct snd_kcontrol *k, int event) 6050cf470c0SWallace Lin { 6060cf470c0SWallace Lin struct snd_soc_component *component = 6070cf470c0SWallace Lin snd_soc_dapm_to_component(w->dapm); 6080cf470c0SWallace Lin struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 6090cf470c0SWallace Lin 6100cf470c0SWallace Lin if (SND_SOC_DAPM_EVENT_OFF(event)) { 6110cf470c0SWallace Lin dev_dbg(nau8821->dev, "system clock control : POWER OFF\n"); 6120cf470c0SWallace Lin /* Set clock source to disable or internal clock before the 6130cf470c0SWallace Lin * playback or capture end. Codec needs clock for Jack 6140cf470c0SWallace Lin * detection and button press if jack inserted; otherwise, 6150cf470c0SWallace Lin * the clock should be closed. 6160cf470c0SWallace Lin */ 6170cf470c0SWallace Lin if (nau8821_is_jack_inserted(nau8821->regmap)) { 6180cf470c0SWallace Lin nau8821_configure_sysclk(nau8821, 6190cf470c0SWallace Lin NAU8821_CLK_INTERNAL, 0); 6200cf470c0SWallace Lin } else { 6210cf470c0SWallace Lin nau8821_configure_sysclk(nau8821, NAU8821_CLK_DIS, 0); 6220cf470c0SWallace Lin } 6230cf470c0SWallace Lin } 6240cf470c0SWallace Lin return 0; 6250cf470c0SWallace Lin } 6260cf470c0SWallace Lin 627014ee069SSeven Lee static int nau8821_left_fepga_event(struct snd_soc_dapm_widget *w, 628014ee069SSeven Lee struct snd_kcontrol *kcontrol, int event) 629014ee069SSeven Lee { 630014ee069SSeven Lee struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 631014ee069SSeven Lee struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 632014ee069SSeven Lee 633014ee069SSeven Lee if (!nau8821->left_input_single_end) 634014ee069SSeven Lee return 0; 635014ee069SSeven Lee 636014ee069SSeven Lee switch (event) { 637014ee069SSeven Lee case SND_SOC_DAPM_POST_PMU: 638014ee069SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R77_FEPGA, 639014ee069SSeven Lee NAU8821_ACDC_CTRL_MASK | NAU8821_FEPGA_MODEL_MASK, 640014ee069SSeven Lee NAU8821_ACDC_VREF_MICN | NAU8821_FEPGA_MODEL_AAF); 641014ee069SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R76_BOOST, 642014ee069SSeven Lee NAU8821_HP_BOOST_DISCHRG_EN, NAU8821_HP_BOOST_DISCHRG_EN); 643014ee069SSeven Lee break; 644014ee069SSeven Lee case SND_SOC_DAPM_POST_PMD: 645014ee069SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R77_FEPGA, 646014ee069SSeven Lee NAU8821_ACDC_CTRL_MASK | NAU8821_FEPGA_MODEL_MASK, 0); 647014ee069SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R76_BOOST, 648014ee069SSeven Lee NAU8821_HP_BOOST_DISCHRG_EN, 0); 649014ee069SSeven Lee break; 650014ee069SSeven Lee default: 651014ee069SSeven Lee break; 652014ee069SSeven Lee } 653014ee069SSeven Lee 654014ee069SSeven Lee return 0; 655014ee069SSeven Lee } 656014ee069SSeven Lee 657aab1ad11SSeven Lee static const struct snd_soc_dapm_widget nau8821_dapm_widgets[] = { 6580cf470c0SWallace Lin SND_SOC_DAPM_SUPPLY("System Clock", SND_SOC_NOPM, 0, 0, 6590cf470c0SWallace Lin system_clock_control, SND_SOC_DAPM_POST_PMD), 660aab1ad11SSeven Lee SND_SOC_DAPM_SUPPLY("MICBIAS", NAU8821_R74_MIC_BIAS, 661aab1ad11SSeven Lee NAU8821_MICBIAS_POWERUP_SFT, 0, NULL, 0), 662aab1ad11SSeven Lee SND_SOC_DAPM_SUPPLY("DMIC Clock", SND_SOC_NOPM, 0, 0, 663aab1ad11SSeven Lee dmic_clock_control, SND_SOC_DAPM_POST_PMU), 664aab1ad11SSeven Lee SND_SOC_DAPM_ADC("ADCL Power", NULL, NAU8821_R72_ANALOG_ADC_2, 665aab1ad11SSeven Lee NAU8821_POWERUP_ADCL_SFT, 0), 666aab1ad11SSeven Lee SND_SOC_DAPM_ADC("ADCR Power", NULL, NAU8821_R72_ANALOG_ADC_2, 667aab1ad11SSeven Lee NAU8821_POWERUP_ADCR_SFT, 0), 668014ee069SSeven Lee /* single-ended design only on the left */ 669aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("Frontend PGA L", 1, NAU8821_R7F_POWER_UP_CONTROL, 670014ee069SSeven Lee NAU8821_PUP_PGA_L_SFT, 0, nau8821_left_fepga_event, 671014ee069SSeven Lee SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), 672aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("Frontend PGA R", 1, NAU8821_R7F_POWER_UP_CONTROL, 673aab1ad11SSeven Lee NAU8821_PUP_PGA_R_SFT, 0, NULL, 0), 674aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("ADCL Digital path", 0, NAU8821_R01_ENA_CTRL, 675aab1ad11SSeven Lee NAU8821_EN_ADCL_SFT, 0, nau8821_left_adc_event, 676aab1ad11SSeven Lee SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), 677aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("ADCR Digital path", 0, NAU8821_R01_ENA_CTRL, 678aab1ad11SSeven Lee NAU8821_EN_ADCR_SFT, 0, nau8821_right_adc_event, 679aab1ad11SSeven Lee SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), 680aab1ad11SSeven Lee SND_SOC_DAPM_SWITCH("DMIC Enable", SND_SOC_NOPM, 681aab1ad11SSeven Lee 0, 0, &nau8821_dmic_mode_switch), 682aab1ad11SSeven Lee SND_SOC_DAPM_AIF_OUT("AIFTX", "Capture", 0, NAU8821_R1D_I2S_PCM_CTRL2, 683aab1ad11SSeven Lee NAU8821_I2S_TRISTATE_SFT, 1), 684aab1ad11SSeven Lee SND_SOC_DAPM_AIF_IN("AIFRX", "Playback", 0, SND_SOC_NOPM, 0, 0), 685aab1ad11SSeven Lee 686aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("ADACL", 2, NAU8821_R73_RDAC, 687aab1ad11SSeven Lee NAU8821_DACL_EN_SFT, 0, NULL, 0), 688aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("ADACR", 2, NAU8821_R73_RDAC, 689aab1ad11SSeven Lee NAU8821_DACR_EN_SFT, 0, NULL, 0), 690aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("ADACL Clock", 3, NAU8821_R73_RDAC, 691aab1ad11SSeven Lee NAU8821_DACL_CLK_EN_SFT, 0, NULL, 0), 692aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("ADACR Clock", 3, NAU8821_R73_RDAC, 693aab1ad11SSeven Lee NAU8821_DACR_CLK_EN_SFT, 0, NULL, 0), 694aab1ad11SSeven Lee SND_SOC_DAPM_DAC("DDACR", NULL, NAU8821_R01_ENA_CTRL, 695aab1ad11SSeven Lee NAU8821_EN_DACR_SFT, 0), 696aab1ad11SSeven Lee SND_SOC_DAPM_DAC("DDACL", NULL, NAU8821_R01_ENA_CTRL, 697aab1ad11SSeven Lee NAU8821_EN_DACL_SFT, 0), 698aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("HP amp L", 0, NAU8821_R4B_CLASSG_CTRL, 699aab1ad11SSeven Lee NAU8821_CLASSG_LDAC_EN_SFT, 0, NULL, 0), 700aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("HP amp R", 0, NAU8821_R4B_CLASSG_CTRL, 701aab1ad11SSeven Lee NAU8821_CLASSG_RDAC_EN_SFT, 0, NULL, 0), 702aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("Charge Pump", 1, NAU8821_R80_CHARGE_PUMP, 703aab1ad11SSeven Lee NAU8821_CHANRGE_PUMP_EN_SFT, 0, nau8821_pump_event, 704aab1ad11SSeven Lee SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 705aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("Output Driver R Stage 1", 4, 706aab1ad11SSeven Lee NAU8821_R7F_POWER_UP_CONTROL, 707aab1ad11SSeven Lee NAU8821_PUP_INTEG_R_SFT, 0, NULL, 0), 708aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("Output Driver L Stage 1", 4, 709aab1ad11SSeven Lee NAU8821_R7F_POWER_UP_CONTROL, 710aab1ad11SSeven Lee NAU8821_PUP_INTEG_L_SFT, 0, NULL, 0), 711aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("Output Driver R Stage 2", 5, 712aab1ad11SSeven Lee NAU8821_R7F_POWER_UP_CONTROL, 713aab1ad11SSeven Lee NAU8821_PUP_DRV_INSTG_R_SFT, 0, NULL, 0), 714aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("Output Driver L Stage 2", 5, 715aab1ad11SSeven Lee NAU8821_R7F_POWER_UP_CONTROL, 716aab1ad11SSeven Lee NAU8821_PUP_DRV_INSTG_L_SFT, 0, NULL, 0), 717aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("Output Driver R Stage 3", 6, 718aab1ad11SSeven Lee NAU8821_R7F_POWER_UP_CONTROL, 719aab1ad11SSeven Lee NAU8821_PUP_MAIN_DRV_R_SFT, 0, NULL, 0), 720aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("Output Driver L Stage 3", 6, 721aab1ad11SSeven Lee NAU8821_R7F_POWER_UP_CONTROL, 722aab1ad11SSeven Lee NAU8821_PUP_MAIN_DRV_L_SFT, 0, NULL, 0), 723aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("Output DACL", 7, 724aab1ad11SSeven Lee NAU8821_R80_CHARGE_PUMP, NAU8821_POWER_DOWN_DACL_SFT, 725aab1ad11SSeven Lee 0, nau8821_output_dac_event, 726aab1ad11SSeven Lee SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), 727aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("Output DACR", 7, 728aab1ad11SSeven Lee NAU8821_R80_CHARGE_PUMP, NAU8821_POWER_DOWN_DACR_SFT, 729aab1ad11SSeven Lee 0, nau8821_output_dac_event, 730aab1ad11SSeven Lee SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), 731aab1ad11SSeven Lee 732aab1ad11SSeven Lee /* HPOL/R are ungrounded by disabling 16 Ohm pull-downs on playback */ 733aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("HPOL Pulldown", 8, 734aab1ad11SSeven Lee NAU8821_R0D_JACK_DET_CTRL, 735aab1ad11SSeven Lee NAU8821_SPKR_DWN1L_SFT, 0, NULL, 0), 736aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("HPOR Pulldown", 8, 737aab1ad11SSeven Lee NAU8821_R0D_JACK_DET_CTRL, 738aab1ad11SSeven Lee NAU8821_SPKR_DWN1R_SFT, 0, NULL, 0), 739aab1ad11SSeven Lee 740aab1ad11SSeven Lee /* High current HPOL/R boost driver */ 741aab1ad11SSeven Lee SND_SOC_DAPM_PGA_S("HP Boost Driver", 9, 742aab1ad11SSeven Lee NAU8821_R76_BOOST, NAU8821_HP_BOOST_DIS_SFT, 1, NULL, 0), 743aab1ad11SSeven Lee SND_SOC_DAPM_PGA("Class G", NAU8821_R4B_CLASSG_CTRL, 744aab1ad11SSeven Lee NAU8821_CLASSG_EN_SFT, 0, NULL, 0), 745aab1ad11SSeven Lee 746aab1ad11SSeven Lee SND_SOC_DAPM_INPUT("MICL"), 747aab1ad11SSeven Lee SND_SOC_DAPM_INPUT("MICR"), 748aab1ad11SSeven Lee SND_SOC_DAPM_INPUT("DMIC"), 749aab1ad11SSeven Lee SND_SOC_DAPM_OUTPUT("HPOL"), 750aab1ad11SSeven Lee SND_SOC_DAPM_OUTPUT("HPOR"), 751aab1ad11SSeven Lee }; 752aab1ad11SSeven Lee 753aab1ad11SSeven Lee static const struct snd_soc_dapm_route nau8821_dapm_routes[] = { 754aab1ad11SSeven Lee {"DMIC Enable", "Switch", "DMIC"}, 755aab1ad11SSeven Lee {"DMIC Enable", NULL, "DMIC Clock"}, 756aab1ad11SSeven Lee 757aab1ad11SSeven Lee {"Frontend PGA L", NULL, "MICL"}, 758aab1ad11SSeven Lee {"Frontend PGA R", NULL, "MICR"}, 759aab1ad11SSeven Lee {"Frontend PGA L", NULL, "MICBIAS"}, 760aab1ad11SSeven Lee {"Frontend PGA R", NULL, "MICBIAS"}, 761aab1ad11SSeven Lee 762aab1ad11SSeven Lee {"ADCL Power", NULL, "Frontend PGA L"}, 763aab1ad11SSeven Lee {"ADCR Power", NULL, "Frontend PGA R"}, 764aab1ad11SSeven Lee 765aab1ad11SSeven Lee {"ADCL Digital path", NULL, "ADCL Power"}, 766aab1ad11SSeven Lee {"ADCR Digital path", NULL, "ADCR Power"}, 767aab1ad11SSeven Lee {"ADCL Digital path", NULL, "DMIC Enable"}, 768aab1ad11SSeven Lee {"ADCR Digital path", NULL, "DMIC Enable"}, 769aab1ad11SSeven Lee 770aab1ad11SSeven Lee {"AIFTX", NULL, "ADCL Digital path"}, 771aab1ad11SSeven Lee {"AIFTX", NULL, "ADCR Digital path"}, 772aab1ad11SSeven Lee 7730cf470c0SWallace Lin {"AIFTX", NULL, "System Clock"}, 7740cf470c0SWallace Lin {"AIFRX", NULL, "System Clock"}, 7750cf470c0SWallace Lin 776aab1ad11SSeven Lee {"DDACL", NULL, "AIFRX"}, 777aab1ad11SSeven Lee {"DDACR", NULL, "AIFRX"}, 778aab1ad11SSeven Lee 779aab1ad11SSeven Lee {"HP amp L", NULL, "DDACL"}, 780aab1ad11SSeven Lee {"HP amp R", NULL, "DDACR"}, 781aab1ad11SSeven Lee 782aab1ad11SSeven Lee {"Charge Pump", NULL, "HP amp L"}, 783aab1ad11SSeven Lee {"Charge Pump", NULL, "HP amp R"}, 784aab1ad11SSeven Lee 785aab1ad11SSeven Lee {"ADACL", NULL, "Charge Pump"}, 786aab1ad11SSeven Lee {"ADACR", NULL, "Charge Pump"}, 787aab1ad11SSeven Lee {"ADACL Clock", NULL, "ADACL"}, 788aab1ad11SSeven Lee {"ADACR Clock", NULL, "ADACR"}, 789aab1ad11SSeven Lee 790aab1ad11SSeven Lee {"Output Driver L Stage 1", NULL, "ADACL Clock"}, 791aab1ad11SSeven Lee {"Output Driver R Stage 1", NULL, "ADACR Clock"}, 792aab1ad11SSeven Lee {"Output Driver L Stage 2", NULL, "Output Driver L Stage 1"}, 793aab1ad11SSeven Lee {"Output Driver R Stage 2", NULL, "Output Driver R Stage 1"}, 794aab1ad11SSeven Lee {"Output Driver L Stage 3", NULL, "Output Driver L Stage 2"}, 795aab1ad11SSeven Lee {"Output Driver R Stage 3", NULL, "Output Driver R Stage 2"}, 796aab1ad11SSeven Lee {"Output DACL", NULL, "Output Driver L Stage 3"}, 797aab1ad11SSeven Lee {"Output DACR", NULL, "Output Driver R Stage 3"}, 798aab1ad11SSeven Lee 799aab1ad11SSeven Lee {"HPOL Pulldown", NULL, "Output DACL"}, 800aab1ad11SSeven Lee {"HPOR Pulldown", NULL, "Output DACR"}, 801aab1ad11SSeven Lee {"HP Boost Driver", NULL, "HPOL Pulldown"}, 802aab1ad11SSeven Lee {"HP Boost Driver", NULL, "HPOR Pulldown"}, 803aab1ad11SSeven Lee 804aab1ad11SSeven Lee {"Class G", NULL, "HP Boost Driver"}, 805aab1ad11SSeven Lee {"HPOL", NULL, "Class G"}, 806aab1ad11SSeven Lee {"HPOR", NULL, "Class G"}, 807aab1ad11SSeven Lee }; 808aab1ad11SSeven Lee 809cf507187STakashi Iwai static const struct nau8821_osr_attr * 810cf507187STakashi Iwai nau8821_get_osr(struct nau8821 *nau8821, int stream) 811aab1ad11SSeven Lee { 812cf507187STakashi Iwai unsigned int osr; 813aab1ad11SSeven Lee 814aab1ad11SSeven Lee if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 815cf507187STakashi Iwai regmap_read(nau8821->regmap, NAU8821_R2C_DAC_CTRL1, &osr); 816cf507187STakashi Iwai osr &= NAU8821_DAC_OVERSAMPLE_MASK; 817aab1ad11SSeven Lee if (osr >= ARRAY_SIZE(osr_dac_sel)) 818cf507187STakashi Iwai return NULL; 819cf507187STakashi Iwai return &osr_dac_sel[osr]; 820aab1ad11SSeven Lee } else { 821cf507187STakashi Iwai regmap_read(nau8821->regmap, NAU8821_R2B_ADC_RATE, &osr); 822cf507187STakashi Iwai osr &= NAU8821_ADC_SYNC_DOWN_MASK; 823aab1ad11SSeven Lee if (osr >= ARRAY_SIZE(osr_adc_sel)) 824cf507187STakashi Iwai return NULL; 825cf507187STakashi Iwai return &osr_adc_sel[osr]; 826cf507187STakashi Iwai } 827aab1ad11SSeven Lee } 828aab1ad11SSeven Lee 829cf507187STakashi Iwai static int nau8821_dai_startup(struct snd_pcm_substream *substream, 830cf507187STakashi Iwai struct snd_soc_dai *dai) 831cf507187STakashi Iwai { 832cf507187STakashi Iwai struct snd_soc_component *component = dai->component; 833cf507187STakashi Iwai struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 834cf507187STakashi Iwai const struct nau8821_osr_attr *osr; 835aab1ad11SSeven Lee 836cf507187STakashi Iwai osr = nau8821_get_osr(nau8821, substream->stream); 837cf507187STakashi Iwai if (!osr || !osr->osr) 838cf507187STakashi Iwai return -EINVAL; 839cf507187STakashi Iwai 840cf507187STakashi Iwai return snd_pcm_hw_constraint_minmax(substream->runtime, 841cf507187STakashi Iwai SNDRV_PCM_HW_PARAM_RATE, 842cf507187STakashi Iwai 0, CLK_DA_AD_MAX / osr->osr); 843aab1ad11SSeven Lee } 844aab1ad11SSeven Lee 845aab1ad11SSeven Lee static int nau8821_hw_params(struct snd_pcm_substream *substream, 846aab1ad11SSeven Lee struct snd_pcm_hw_params *params, struct snd_soc_dai *dai) 847aab1ad11SSeven Lee { 848aab1ad11SSeven Lee struct snd_soc_component *component = dai->component; 849aab1ad11SSeven Lee struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 850cf507187STakashi Iwai unsigned int val_len = 0, ctrl_val, bclk_fs, clk_div; 851cf507187STakashi Iwai const struct nau8821_osr_attr *osr; 852aab1ad11SSeven Lee 853aab1ad11SSeven Lee nau8821->fs = params_rate(params); 854aab1ad11SSeven Lee /* CLK_DAC or CLK_ADC = OSR * FS 855aab1ad11SSeven Lee * DAC or ADC clock frequency is defined as Over Sampling Rate (OSR) 856aab1ad11SSeven Lee * multiplied by the audio sample rate (Fs). Note that the OSR and Fs 857aab1ad11SSeven Lee * values must be selected such that the maximum frequency is less 858aab1ad11SSeven Lee * than 6.144 MHz. 859aab1ad11SSeven Lee */ 860cf507187STakashi Iwai osr = nau8821_get_osr(nau8821, substream->stream); 861cf507187STakashi Iwai if (!osr || !osr->osr) 862aab1ad11SSeven Lee return -EINVAL; 863cf507187STakashi Iwai if (nau8821->fs * osr->osr > CLK_DA_AD_MAX) 864cf507187STakashi Iwai return -EINVAL; 865cf507187STakashi Iwai if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 866aab1ad11SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R03_CLK_DIVIDER, 867aab1ad11SSeven Lee NAU8821_CLK_DAC_SRC_MASK, 868cf507187STakashi Iwai osr->clk_src << NAU8821_CLK_DAC_SRC_SFT); 869cf507187STakashi Iwai else 870aab1ad11SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R03_CLK_DIVIDER, 871aab1ad11SSeven Lee NAU8821_CLK_ADC_SRC_MASK, 872cf507187STakashi Iwai osr->clk_src << NAU8821_CLK_ADC_SRC_SFT); 873aab1ad11SSeven Lee 874aab1ad11SSeven Lee /* make BCLK and LRC divde configuration if the codec as master. */ 875aab1ad11SSeven Lee regmap_read(nau8821->regmap, NAU8821_R1D_I2S_PCM_CTRL2, &ctrl_val); 876aab1ad11SSeven Lee if (ctrl_val & NAU8821_I2S_MS_MASTER) { 877aab1ad11SSeven Lee /* get the bclk and fs ratio */ 878aab1ad11SSeven Lee bclk_fs = snd_soc_params_to_bclk(params) / nau8821->fs; 879aab1ad11SSeven Lee if (bclk_fs <= 32) 880aab1ad11SSeven Lee clk_div = 3; 881aab1ad11SSeven Lee else if (bclk_fs <= 64) 882aab1ad11SSeven Lee clk_div = 2; 883aab1ad11SSeven Lee else if (bclk_fs <= 128) 884aab1ad11SSeven Lee clk_div = 1; 885aab1ad11SSeven Lee else { 886aab1ad11SSeven Lee return -EINVAL; 887aab1ad11SSeven Lee } 888aab1ad11SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R1D_I2S_PCM_CTRL2, 889aab1ad11SSeven Lee NAU8821_I2S_LRC_DIV_MASK | NAU8821_I2S_BLK_DIV_MASK, 890aab1ad11SSeven Lee (clk_div << NAU8821_I2S_LRC_DIV_SFT) | clk_div); 891aab1ad11SSeven Lee } 892aab1ad11SSeven Lee 893aab1ad11SSeven Lee switch (params_width(params)) { 894aab1ad11SSeven Lee case 16: 895aab1ad11SSeven Lee val_len |= NAU8821_I2S_DL_16; 896aab1ad11SSeven Lee break; 897aab1ad11SSeven Lee case 20: 898aab1ad11SSeven Lee val_len |= NAU8821_I2S_DL_20; 899aab1ad11SSeven Lee break; 900aab1ad11SSeven Lee case 24: 901aab1ad11SSeven Lee val_len |= NAU8821_I2S_DL_24; 902aab1ad11SSeven Lee break; 903aab1ad11SSeven Lee case 32: 904aab1ad11SSeven Lee val_len |= NAU8821_I2S_DL_32; 905aab1ad11SSeven Lee break; 906aab1ad11SSeven Lee default: 907aab1ad11SSeven Lee return -EINVAL; 908aab1ad11SSeven Lee } 909aab1ad11SSeven Lee 910aab1ad11SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R1C_I2S_PCM_CTRL1, 911aab1ad11SSeven Lee NAU8821_I2S_DL_MASK, val_len); 912aab1ad11SSeven Lee 913aab1ad11SSeven Lee return 0; 914aab1ad11SSeven Lee } 915aab1ad11SSeven Lee 916aab1ad11SSeven Lee static int nau8821_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt) 917aab1ad11SSeven Lee { 918aab1ad11SSeven Lee struct snd_soc_component *component = codec_dai->component; 919aab1ad11SSeven Lee struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 920aab1ad11SSeven Lee unsigned int ctrl1_val = 0, ctrl2_val = 0; 921aab1ad11SSeven Lee 922aab1ad11SSeven Lee switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 923aab1ad11SSeven Lee case SND_SOC_DAIFMT_CBP_CFP: 924aab1ad11SSeven Lee ctrl2_val |= NAU8821_I2S_MS_MASTER; 925aab1ad11SSeven Lee break; 926aab1ad11SSeven Lee case SND_SOC_DAIFMT_CBC_CFC: 927aab1ad11SSeven Lee break; 928aab1ad11SSeven Lee default: 929aab1ad11SSeven Lee return -EINVAL; 930aab1ad11SSeven Lee } 931aab1ad11SSeven Lee 932aab1ad11SSeven Lee switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 933aab1ad11SSeven Lee case SND_SOC_DAIFMT_NB_NF: 934aab1ad11SSeven Lee break; 935aab1ad11SSeven Lee case SND_SOC_DAIFMT_IB_NF: 936aab1ad11SSeven Lee ctrl1_val |= NAU8821_I2S_BP_INV; 937aab1ad11SSeven Lee break; 938aab1ad11SSeven Lee default: 939aab1ad11SSeven Lee return -EINVAL; 940aab1ad11SSeven Lee } 941aab1ad11SSeven Lee 942aab1ad11SSeven Lee switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 943aab1ad11SSeven Lee case SND_SOC_DAIFMT_I2S: 944aab1ad11SSeven Lee ctrl1_val |= NAU8821_I2S_DF_I2S; 945aab1ad11SSeven Lee break; 946aab1ad11SSeven Lee case SND_SOC_DAIFMT_LEFT_J: 947aab1ad11SSeven Lee ctrl1_val |= NAU8821_I2S_DF_LEFT; 948aab1ad11SSeven Lee break; 949aab1ad11SSeven Lee case SND_SOC_DAIFMT_RIGHT_J: 950aab1ad11SSeven Lee ctrl1_val |= NAU8821_I2S_DF_RIGTH; 951aab1ad11SSeven Lee break; 952aab1ad11SSeven Lee case SND_SOC_DAIFMT_DSP_A: 953aab1ad11SSeven Lee ctrl1_val |= NAU8821_I2S_DF_PCM_AB; 954aab1ad11SSeven Lee break; 955aab1ad11SSeven Lee case SND_SOC_DAIFMT_DSP_B: 956aab1ad11SSeven Lee ctrl1_val |= NAU8821_I2S_DF_PCM_AB; 957aab1ad11SSeven Lee ctrl1_val |= NAU8821_I2S_PCMB_EN; 958aab1ad11SSeven Lee break; 959aab1ad11SSeven Lee default: 960aab1ad11SSeven Lee return -EINVAL; 961aab1ad11SSeven Lee } 962aab1ad11SSeven Lee 963aab1ad11SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R1C_I2S_PCM_CTRL1, 964aab1ad11SSeven Lee NAU8821_I2S_DL_MASK | NAU8821_I2S_DF_MASK | 965aab1ad11SSeven Lee NAU8821_I2S_BP_MASK | NAU8821_I2S_PCMB_MASK, ctrl1_val); 966aab1ad11SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R1D_I2S_PCM_CTRL2, 967aab1ad11SSeven Lee NAU8821_I2S_MS_MASK, ctrl2_val); 968aab1ad11SSeven Lee 969aab1ad11SSeven Lee return 0; 970aab1ad11SSeven Lee } 971aab1ad11SSeven Lee 972aab1ad11SSeven Lee static int nau8821_digital_mute(struct snd_soc_dai *dai, int mute, 973aab1ad11SSeven Lee int direction) 974aab1ad11SSeven Lee { 975aab1ad11SSeven Lee struct snd_soc_component *component = dai->component; 976aab1ad11SSeven Lee struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 977aab1ad11SSeven Lee unsigned int val = 0; 978aab1ad11SSeven Lee 979aab1ad11SSeven Lee if (mute) 980aab1ad11SSeven Lee val = NAU8821_DAC_SOFT_MUTE; 981aab1ad11SSeven Lee 982aab1ad11SSeven Lee return regmap_update_bits(nau8821->regmap, 983aab1ad11SSeven Lee NAU8821_R31_MUTE_CTRL, NAU8821_DAC_SOFT_MUTE, val); 984aab1ad11SSeven Lee } 985aab1ad11SSeven Lee 986aab1ad11SSeven Lee static const struct snd_soc_dai_ops nau8821_dai_ops = { 987cf507187STakashi Iwai .startup = nau8821_dai_startup, 988aab1ad11SSeven Lee .hw_params = nau8821_hw_params, 989aab1ad11SSeven Lee .set_fmt = nau8821_set_dai_fmt, 990aab1ad11SSeven Lee .mute_stream = nau8821_digital_mute, 991aa9753a4SVijendar Mukunda .no_capture_mute = 1, 992aab1ad11SSeven Lee }; 993aab1ad11SSeven Lee 994aab1ad11SSeven Lee #define NAU8821_RATES SNDRV_PCM_RATE_8000_192000 995aab1ad11SSeven Lee #define NAU8821_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \ 996aab1ad11SSeven Lee | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE) 997aab1ad11SSeven Lee 998aab1ad11SSeven Lee static struct snd_soc_dai_driver nau8821_dai = { 999aab1ad11SSeven Lee .name = NUVOTON_CODEC_DAI, 1000aab1ad11SSeven Lee .playback = { 1001aab1ad11SSeven Lee .stream_name = "Playback", 1002aab1ad11SSeven Lee .channels_min = 1, 1003aab1ad11SSeven Lee .channels_max = 2, 1004aab1ad11SSeven Lee .rates = NAU8821_RATES, 1005aab1ad11SSeven Lee .formats = NAU8821_FORMATS, 1006aab1ad11SSeven Lee }, 1007aab1ad11SSeven Lee .capture = { 1008aab1ad11SSeven Lee .stream_name = "Capture", 1009aab1ad11SSeven Lee .channels_min = 1, 1010aab1ad11SSeven Lee .channels_max = 2, 1011aab1ad11SSeven Lee .rates = NAU8821_RATES, 1012aab1ad11SSeven Lee .formats = NAU8821_FORMATS, 1013aab1ad11SSeven Lee }, 1014aab1ad11SSeven Lee .ops = &nau8821_dai_ops, 1015aab1ad11SSeven Lee }; 1016aab1ad11SSeven Lee 1017aab1ad11SSeven Lee 1018aab1ad11SSeven Lee static bool nau8821_is_jack_inserted(struct regmap *regmap) 1019aab1ad11SSeven Lee { 1020aab1ad11SSeven Lee bool active_high, is_high; 1021aab1ad11SSeven Lee int status, jkdet; 1022aab1ad11SSeven Lee 1023aab1ad11SSeven Lee regmap_read(regmap, NAU8821_R0D_JACK_DET_CTRL, &jkdet); 1024aab1ad11SSeven Lee active_high = jkdet & NAU8821_JACK_POLARITY; 1025aab1ad11SSeven Lee regmap_read(regmap, NAU8821_R82_GENERAL_STATUS, &status); 1026aab1ad11SSeven Lee is_high = status & NAU8821_GPIO2_IN; 1027aab1ad11SSeven Lee /* return jack connection status according to jack insertion logic 1028aab1ad11SSeven Lee * active high or active low. 1029aab1ad11SSeven Lee */ 1030aab1ad11SSeven Lee return active_high == is_high; 1031aab1ad11SSeven Lee } 1032aab1ad11SSeven Lee 1033aab1ad11SSeven Lee static void nau8821_int_status_clear_all(struct regmap *regmap) 1034aab1ad11SSeven Lee { 1035aab1ad11SSeven Lee int active_irq, clear_irq, i; 1036aab1ad11SSeven Lee 1037aab1ad11SSeven Lee /* Reset the intrruption status from rightmost bit if the corres- 1038aab1ad11SSeven Lee * ponding irq event occurs. 1039aab1ad11SSeven Lee */ 1040aab1ad11SSeven Lee regmap_read(regmap, NAU8821_R10_IRQ_STATUS, &active_irq); 1041aab1ad11SSeven Lee for (i = 0; i < NAU8821_REG_DATA_LEN; i++) { 1042aab1ad11SSeven Lee clear_irq = (0x1 << i); 1043aab1ad11SSeven Lee if (active_irq & clear_irq) 1044aab1ad11SSeven Lee regmap_write(regmap, 1045aab1ad11SSeven Lee NAU8821_R11_INT_CLR_KEY_STATUS, clear_irq); 1046aab1ad11SSeven Lee } 1047aab1ad11SSeven Lee } 1048aab1ad11SSeven Lee 1049aab1ad11SSeven Lee static void nau8821_eject_jack(struct nau8821 *nau8821) 1050aab1ad11SSeven Lee { 1051aab1ad11SSeven Lee struct snd_soc_dapm_context *dapm = nau8821->dapm; 1052aab1ad11SSeven Lee struct regmap *regmap = nau8821->regmap; 1053aab1ad11SSeven Lee struct snd_soc_component *component = snd_soc_dapm_to_component(dapm); 1054aab1ad11SSeven Lee 1055aab1ad11SSeven Lee /* Detach 2kOhm Resistors from MICBIAS to MICGND */ 1056aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R74_MIC_BIAS, 1057aab1ad11SSeven Lee NAU8821_MICBIAS_JKR2, 0); 1058aab1ad11SSeven Lee /* HPL/HPR short to ground */ 1059aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL, 1060aab1ad11SSeven Lee NAU8821_SPKR_DWN1R | NAU8821_SPKR_DWN1L, 0); 1061aab1ad11SSeven Lee snd_soc_component_disable_pin(component, "MICBIAS"); 1062aab1ad11SSeven Lee snd_soc_dapm_sync(dapm); 1063aab1ad11SSeven Lee 1064aab1ad11SSeven Lee /* Clear all interruption status */ 1065aab1ad11SSeven Lee nau8821_int_status_clear_all(regmap); 1066aab1ad11SSeven Lee 1067aab1ad11SSeven Lee /* Enable the insertion interruption, disable the ejection inter- 1068aab1ad11SSeven Lee * ruption, and then bypass de-bounce circuit. 1069aab1ad11SSeven Lee */ 1070aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R12_INTERRUPT_DIS_CTRL, 1071aab1ad11SSeven Lee NAU8821_IRQ_EJECT_DIS | NAU8821_IRQ_INSERT_DIS, 1072aab1ad11SSeven Lee NAU8821_IRQ_EJECT_DIS); 1073aab1ad11SSeven Lee /* Mask unneeded IRQs: 1 - disable, 0 - enable */ 1074aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK, 1075aab1ad11SSeven Lee NAU8821_IRQ_EJECT_EN | NAU8821_IRQ_INSERT_EN, 1076aab1ad11SSeven Lee NAU8821_IRQ_EJECT_EN); 1077aab1ad11SSeven Lee 1078aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL, 1079aab1ad11SSeven Lee NAU8821_JACK_DET_DB_BYPASS, NAU8821_JACK_DET_DB_BYPASS); 1080aab1ad11SSeven Lee 1081aab1ad11SSeven Lee /* Close clock for jack type detection at manual mode */ 1082aab1ad11SSeven Lee if (dapm->bias_level < SND_SOC_BIAS_PREPARE) 1083aab1ad11SSeven Lee nau8821_configure_sysclk(nau8821, NAU8821_CLK_DIS, 0); 1084aab1ad11SSeven Lee 1085aab1ad11SSeven Lee /* Recover to normal channel input */ 1086aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R2B_ADC_RATE, 1087aab1ad11SSeven Lee NAU8821_ADC_R_SRC_EN, 0); 10882551b6e8SSeven Lee if (nau8821->key_enable) { 10892551b6e8SSeven Lee regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK, 10902551b6e8SSeven Lee NAU8821_IRQ_KEY_RELEASE_EN | 10912551b6e8SSeven Lee NAU8821_IRQ_KEY_PRESS_EN, 10922551b6e8SSeven Lee NAU8821_IRQ_KEY_RELEASE_EN | 10932551b6e8SSeven Lee NAU8821_IRQ_KEY_PRESS_EN); 10942551b6e8SSeven Lee regmap_update_bits(regmap, 10952551b6e8SSeven Lee NAU8821_R12_INTERRUPT_DIS_CTRL, 10962551b6e8SSeven Lee NAU8821_IRQ_KEY_RELEASE_DIS | 10972551b6e8SSeven Lee NAU8821_IRQ_KEY_PRESS_DIS, 10982551b6e8SSeven Lee NAU8821_IRQ_KEY_RELEASE_DIS | 10992551b6e8SSeven Lee NAU8821_IRQ_KEY_PRESS_DIS); 11002551b6e8SSeven Lee } 11012551b6e8SSeven Lee 1102aab1ad11SSeven Lee } 1103aab1ad11SSeven Lee 1104aab1ad11SSeven Lee static void nau8821_jdet_work(struct work_struct *work) 1105aab1ad11SSeven Lee { 1106aab1ad11SSeven Lee struct nau8821 *nau8821 = 1107aab1ad11SSeven Lee container_of(work, struct nau8821, jdet_work); 1108aab1ad11SSeven Lee struct snd_soc_dapm_context *dapm = nau8821->dapm; 1109aab1ad11SSeven Lee struct snd_soc_component *component = snd_soc_dapm_to_component(dapm); 1110aab1ad11SSeven Lee struct regmap *regmap = nau8821->regmap; 1111aab1ad11SSeven Lee int jack_status_reg, mic_detected, event = 0, event_mask = 0; 1112aab1ad11SSeven Lee 1113aab1ad11SSeven Lee snd_soc_component_force_enable_pin(component, "MICBIAS"); 1114aab1ad11SSeven Lee snd_soc_dapm_sync(dapm); 1115aab1ad11SSeven Lee msleep(20); 1116aab1ad11SSeven Lee 1117aab1ad11SSeven Lee regmap_read(regmap, NAU8821_R58_I2C_DEVICE_ID, &jack_status_reg); 1118aab1ad11SSeven Lee mic_detected = !(jack_status_reg & NAU8821_KEYDET); 1119aab1ad11SSeven Lee if (mic_detected) { 1120aab1ad11SSeven Lee dev_dbg(nau8821->dev, "Headset connected\n"); 1121aab1ad11SSeven Lee event |= SND_JACK_HEADSET; 1122aab1ad11SSeven Lee 1123aab1ad11SSeven Lee /* 2kOhm Resistor from MICBIAS to MICGND1 */ 1124aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R74_MIC_BIAS, 1125aab1ad11SSeven Lee NAU8821_MICBIAS_JKR2, NAU8821_MICBIAS_JKR2); 1126aab1ad11SSeven Lee /* Latch Right Channel Analog data 1127aab1ad11SSeven Lee * input into the Right Channel Filter 1128aab1ad11SSeven Lee */ 1129aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R2B_ADC_RATE, 1130aab1ad11SSeven Lee NAU8821_ADC_R_SRC_EN, NAU8821_ADC_R_SRC_EN); 11312551b6e8SSeven Lee if (nau8821->key_enable) { 11322551b6e8SSeven Lee regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK, 11332551b6e8SSeven Lee NAU8821_IRQ_KEY_RELEASE_EN | 11342551b6e8SSeven Lee NAU8821_IRQ_KEY_PRESS_EN, 0); 11352551b6e8SSeven Lee regmap_update_bits(regmap, 11362551b6e8SSeven Lee NAU8821_R12_INTERRUPT_DIS_CTRL, 11372551b6e8SSeven Lee NAU8821_IRQ_KEY_RELEASE_DIS | 11382551b6e8SSeven Lee NAU8821_IRQ_KEY_PRESS_DIS, 0); 11398885ab34SSeven Lee } else { 11408885ab34SSeven Lee snd_soc_component_disable_pin(component, "MICBIAS"); 11418885ab34SSeven Lee snd_soc_dapm_sync(nau8821->dapm); 11422551b6e8SSeven Lee } 1143aab1ad11SSeven Lee } else { 1144aab1ad11SSeven Lee dev_dbg(nau8821->dev, "Headphone connected\n"); 1145aab1ad11SSeven Lee event |= SND_JACK_HEADPHONE; 1146aab1ad11SSeven Lee snd_soc_component_disable_pin(component, "MICBIAS"); 1147aab1ad11SSeven Lee snd_soc_dapm_sync(dapm); 1148aab1ad11SSeven Lee } 1149aab1ad11SSeven Lee event_mask |= SND_JACK_HEADSET; 1150aab1ad11SSeven Lee snd_soc_jack_report(nau8821->jack, event, event_mask); 1151aab1ad11SSeven Lee } 1152aab1ad11SSeven Lee 1153aab1ad11SSeven Lee /* Enable interruptions with internal clock. */ 1154aab1ad11SSeven Lee static void nau8821_setup_inserted_irq(struct nau8821 *nau8821) 1155aab1ad11SSeven Lee { 1156aab1ad11SSeven Lee struct regmap *regmap = nau8821->regmap; 1157aab1ad11SSeven Lee 1158aab1ad11SSeven Lee /* Enable internal VCO needed for interruptions */ 1159aab1ad11SSeven Lee if (nau8821->dapm->bias_level < SND_SOC_BIAS_PREPARE) 1160aab1ad11SSeven Lee nau8821_configure_sysclk(nau8821, NAU8821_CLK_INTERNAL, 0); 1161aab1ad11SSeven Lee 1162aab1ad11SSeven Lee /* Chip needs one FSCLK cycle in order to generate interruptions, 1163aab1ad11SSeven Lee * as we cannot guarantee one will be provided by the system. Turning 1164aab1ad11SSeven Lee * master mode on then off enables us to generate that FSCLK cycle 1165aab1ad11SSeven Lee * with a minimum of contention on the clock bus. 1166aab1ad11SSeven Lee */ 1167aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R1D_I2S_PCM_CTRL2, 1168aab1ad11SSeven Lee NAU8821_I2S_MS_MASK, NAU8821_I2S_MS_MASTER); 1169aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R1D_I2S_PCM_CTRL2, 1170aab1ad11SSeven Lee NAU8821_I2S_MS_MASK, NAU8821_I2S_MS_SLAVE); 1171aab1ad11SSeven Lee 1172aab1ad11SSeven Lee /* Not bypass de-bounce circuit */ 1173aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL, 1174aab1ad11SSeven Lee NAU8821_JACK_DET_DB_BYPASS, 0); 1175aab1ad11SSeven Lee 1176aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK, 1177aab1ad11SSeven Lee NAU8821_IRQ_EJECT_EN, 0); 1178aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R12_INTERRUPT_DIS_CTRL, 1179aab1ad11SSeven Lee NAU8821_IRQ_EJECT_DIS, 0); 1180aab1ad11SSeven Lee } 1181aab1ad11SSeven Lee 1182aab1ad11SSeven Lee static irqreturn_t nau8821_interrupt(int irq, void *data) 1183aab1ad11SSeven Lee { 1184aab1ad11SSeven Lee struct nau8821 *nau8821 = (struct nau8821 *)data; 1185aab1ad11SSeven Lee struct regmap *regmap = nau8821->regmap; 1186aab1ad11SSeven Lee int active_irq, clear_irq = 0, event = 0, event_mask = 0; 1187aab1ad11SSeven Lee 1188aab1ad11SSeven Lee if (regmap_read(regmap, NAU8821_R10_IRQ_STATUS, &active_irq)) { 1189aab1ad11SSeven Lee dev_err(nau8821->dev, "failed to read irq status\n"); 1190aab1ad11SSeven Lee return IRQ_NONE; 1191aab1ad11SSeven Lee } 1192aab1ad11SSeven Lee 1193aab1ad11SSeven Lee dev_dbg(nau8821->dev, "IRQ %d\n", active_irq); 1194aab1ad11SSeven Lee 1195aab1ad11SSeven Lee if ((active_irq & NAU8821_JACK_EJECT_IRQ_MASK) == 1196aab1ad11SSeven Lee NAU8821_JACK_EJECT_DETECTED) { 1197aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R71_ANALOG_ADC_1, 1198aab1ad11SSeven Lee NAU8821_MICDET_MASK, NAU8821_MICDET_DIS); 1199aab1ad11SSeven Lee nau8821_eject_jack(nau8821); 1200aab1ad11SSeven Lee event_mask |= SND_JACK_HEADSET; 1201aab1ad11SSeven Lee clear_irq = NAU8821_JACK_EJECT_IRQ_MASK; 12022551b6e8SSeven Lee } else if (active_irq & NAU8821_KEY_SHORT_PRESS_IRQ) { 12032551b6e8SSeven Lee event |= NAU8821_BUTTON; 12042551b6e8SSeven Lee event_mask |= NAU8821_BUTTON; 12052551b6e8SSeven Lee clear_irq = NAU8821_KEY_SHORT_PRESS_IRQ; 12062551b6e8SSeven Lee } else if (active_irq & NAU8821_KEY_RELEASE_IRQ) { 12072551b6e8SSeven Lee event_mask = NAU8821_BUTTON; 12082551b6e8SSeven Lee clear_irq = NAU8821_KEY_RELEASE_IRQ; 1209aab1ad11SSeven Lee } else if ((active_irq & NAU8821_JACK_INSERT_IRQ_MASK) == 1210aab1ad11SSeven Lee NAU8821_JACK_INSERT_DETECTED) { 1211aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R71_ANALOG_ADC_1, 1212aab1ad11SSeven Lee NAU8821_MICDET_MASK, NAU8821_MICDET_EN); 1213aab1ad11SSeven Lee if (nau8821_is_jack_inserted(regmap)) { 1214aab1ad11SSeven Lee /* detect microphone and jack type */ 1215aab1ad11SSeven Lee cancel_work_sync(&nau8821->jdet_work); 1216aab1ad11SSeven Lee schedule_work(&nau8821->jdet_work); 1217aab1ad11SSeven Lee /* Turn off insertion interruption at manual mode */ 1218aab1ad11SSeven Lee regmap_update_bits(regmap, 1219aab1ad11SSeven Lee NAU8821_R12_INTERRUPT_DIS_CTRL, 1220aab1ad11SSeven Lee NAU8821_IRQ_INSERT_DIS, 1221aab1ad11SSeven Lee NAU8821_IRQ_INSERT_DIS); 1222aab1ad11SSeven Lee regmap_update_bits(regmap, 1223aab1ad11SSeven Lee NAU8821_R0F_INTERRUPT_MASK, 1224aab1ad11SSeven Lee NAU8821_IRQ_INSERT_EN, 1225aab1ad11SSeven Lee NAU8821_IRQ_INSERT_EN); 1226aab1ad11SSeven Lee nau8821_setup_inserted_irq(nau8821); 1227aab1ad11SSeven Lee } else { 1228aab1ad11SSeven Lee dev_warn(nau8821->dev, 1229aab1ad11SSeven Lee "Inserted IRQ fired but not connected\n"); 1230aab1ad11SSeven Lee nau8821_eject_jack(nau8821); 1231aab1ad11SSeven Lee } 1232aab1ad11SSeven Lee } 1233aab1ad11SSeven Lee 1234aab1ad11SSeven Lee if (!clear_irq) 1235aab1ad11SSeven Lee clear_irq = active_irq; 1236aab1ad11SSeven Lee /* clears the rightmost interruption */ 1237aab1ad11SSeven Lee regmap_write(regmap, NAU8821_R11_INT_CLR_KEY_STATUS, clear_irq); 1238aab1ad11SSeven Lee 1239aab1ad11SSeven Lee if (event_mask) 1240aab1ad11SSeven Lee snd_soc_jack_report(nau8821->jack, event, event_mask); 1241aab1ad11SSeven Lee 1242aab1ad11SSeven Lee return IRQ_HANDLED; 1243aab1ad11SSeven Lee } 1244aab1ad11SSeven Lee 1245aab1ad11SSeven Lee static const struct regmap_config nau8821_regmap_config = { 1246aab1ad11SSeven Lee .val_bits = NAU8821_REG_DATA_LEN, 1247aab1ad11SSeven Lee .reg_bits = NAU8821_REG_ADDR_LEN, 1248aab1ad11SSeven Lee 1249aab1ad11SSeven Lee .max_register = NAU8821_REG_MAX, 1250aab1ad11SSeven Lee .readable_reg = nau8821_readable_reg, 1251aab1ad11SSeven Lee .writeable_reg = nau8821_writeable_reg, 1252aab1ad11SSeven Lee .volatile_reg = nau8821_volatile_reg, 1253aab1ad11SSeven Lee 1254aab1ad11SSeven Lee .cache_type = REGCACHE_RBTREE, 1255aab1ad11SSeven Lee .reg_defaults = nau8821_reg_defaults, 1256aab1ad11SSeven Lee .num_reg_defaults = ARRAY_SIZE(nau8821_reg_defaults), 1257aab1ad11SSeven Lee }; 1258aab1ad11SSeven Lee 1259aab1ad11SSeven Lee static int nau8821_component_probe(struct snd_soc_component *component) 1260aab1ad11SSeven Lee { 1261aab1ad11SSeven Lee struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 1262aab1ad11SSeven Lee struct snd_soc_dapm_context *dapm = 1263aab1ad11SSeven Lee snd_soc_component_get_dapm(component); 1264aab1ad11SSeven Lee 1265aab1ad11SSeven Lee nau8821->dapm = dapm; 1266aab1ad11SSeven Lee 1267aab1ad11SSeven Lee return 0; 1268aab1ad11SSeven Lee } 1269aab1ad11SSeven Lee 1270aab1ad11SSeven Lee /** 1271aab1ad11SSeven Lee * nau8821_calc_fll_param - Calculate FLL parameters. 1272aab1ad11SSeven Lee * @fll_in: external clock provided to codec. 1273aab1ad11SSeven Lee * @fs: sampling rate. 1274aab1ad11SSeven Lee * @fll_param: Pointer to structure of FLL parameters. 1275aab1ad11SSeven Lee * 1276aab1ad11SSeven Lee * Calculate FLL parameters to configure codec. 1277aab1ad11SSeven Lee * 1278aab1ad11SSeven Lee * Returns 0 for success or negative error code. 1279aab1ad11SSeven Lee */ 1280aab1ad11SSeven Lee static int nau8821_calc_fll_param(unsigned int fll_in, 1281aab1ad11SSeven Lee unsigned int fs, struct nau8821_fll *fll_param) 1282aab1ad11SSeven Lee { 1283aab1ad11SSeven Lee u64 fvco, fvco_max; 1284aab1ad11SSeven Lee unsigned int fref, i, fvco_sel; 1285aab1ad11SSeven Lee 1286aab1ad11SSeven Lee /* Ensure the reference clock frequency (FREF) is <= 13.5MHz by 1287aab1ad11SSeven Lee * dividing freq_in by 1, 2, 4, or 8 using FLL pre-scalar. 1288aab1ad11SSeven Lee * FREF = freq_in / NAU8821_FLL_REF_DIV_MASK 1289aab1ad11SSeven Lee */ 1290aab1ad11SSeven Lee for (i = 0; i < ARRAY_SIZE(fll_pre_scalar); i++) { 1291aab1ad11SSeven Lee fref = fll_in >> fll_pre_scalar[i].param; 1292aab1ad11SSeven Lee if (fref <= NAU_FREF_MAX) 1293aab1ad11SSeven Lee break; 1294aab1ad11SSeven Lee } 1295aab1ad11SSeven Lee if (i == ARRAY_SIZE(fll_pre_scalar)) 1296aab1ad11SSeven Lee return -EINVAL; 1297aab1ad11SSeven Lee fll_param->clk_ref_div = fll_pre_scalar[i].val; 1298aab1ad11SSeven Lee 1299aab1ad11SSeven Lee /* Choose the FLL ratio based on FREF */ 1300aab1ad11SSeven Lee for (i = 0; i < ARRAY_SIZE(fll_ratio); i++) { 1301aab1ad11SSeven Lee if (fref >= fll_ratio[i].param) 1302aab1ad11SSeven Lee break; 1303aab1ad11SSeven Lee } 1304aab1ad11SSeven Lee if (i == ARRAY_SIZE(fll_ratio)) 1305aab1ad11SSeven Lee return -EINVAL; 1306aab1ad11SSeven Lee fll_param->ratio = fll_ratio[i].val; 1307aab1ad11SSeven Lee 1308aab1ad11SSeven Lee /* Calculate the frequency of DCO (FDCO) given freq_out = 256 * Fs. 1309aab1ad11SSeven Lee * FDCO must be within the 90MHz - 100MHz or the FFL cannot be 1310aab1ad11SSeven Lee * guaranteed across the full range of operation. 1311aab1ad11SSeven Lee * FDCO = freq_out * 2 * mclk_src_scaling 1312aab1ad11SSeven Lee */ 1313aab1ad11SSeven Lee fvco_max = 0; 1314aab1ad11SSeven Lee fvco_sel = ARRAY_SIZE(mclk_src_scaling); 1315aab1ad11SSeven Lee for (i = 0; i < ARRAY_SIZE(mclk_src_scaling); i++) { 1316aab1ad11SSeven Lee fvco = 256ULL * fs * 2 * mclk_src_scaling[i].param; 1317aab1ad11SSeven Lee if (fvco > NAU_FVCO_MIN && fvco < NAU_FVCO_MAX && 1318aab1ad11SSeven Lee fvco_max < fvco) { 1319aab1ad11SSeven Lee fvco_max = fvco; 1320aab1ad11SSeven Lee fvco_sel = i; 1321aab1ad11SSeven Lee } 1322aab1ad11SSeven Lee } 1323aab1ad11SSeven Lee if (ARRAY_SIZE(mclk_src_scaling) == fvco_sel) 1324aab1ad11SSeven Lee return -EINVAL; 1325aab1ad11SSeven Lee fll_param->mclk_src = mclk_src_scaling[fvco_sel].val; 1326aab1ad11SSeven Lee 1327aab1ad11SSeven Lee /* Calculate the FLL 10-bit integer input and the FLL 24-bit fractional 1328aab1ad11SSeven Lee * input based on FDCO, FREF and FLL ratio. 1329aab1ad11SSeven Lee */ 1330aab1ad11SSeven Lee fvco = div_u64(fvco_max << 24, fref * fll_param->ratio); 1331aab1ad11SSeven Lee fll_param->fll_int = (fvco >> 24) & 0x3ff; 1332aab1ad11SSeven Lee fll_param->fll_frac = fvco & 0xffffff; 1333aab1ad11SSeven Lee 1334aab1ad11SSeven Lee return 0; 1335aab1ad11SSeven Lee } 1336aab1ad11SSeven Lee 1337aab1ad11SSeven Lee static void nau8821_fll_apply(struct nau8821 *nau8821, 1338aab1ad11SSeven Lee struct nau8821_fll *fll_param) 1339aab1ad11SSeven Lee { 1340aab1ad11SSeven Lee struct regmap *regmap = nau8821->regmap; 1341aab1ad11SSeven Lee 1342aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R03_CLK_DIVIDER, 1343aab1ad11SSeven Lee NAU8821_CLK_SRC_MASK | NAU8821_CLK_MCLK_SRC_MASK, 1344aab1ad11SSeven Lee NAU8821_CLK_SRC_MCLK | fll_param->mclk_src); 1345aab1ad11SSeven Lee /* Make DSP operate at high speed for better performance. */ 1346aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R04_FLL1, 1347aab1ad11SSeven Lee NAU8821_FLL_RATIO_MASK | NAU8821_ICTRL_LATCH_MASK, 1348aab1ad11SSeven Lee fll_param->ratio | (0x6 << NAU8821_ICTRL_LATCH_SFT)); 1349aab1ad11SSeven Lee /* FLL 24-bit fractional input */ 1350aab1ad11SSeven Lee regmap_write(regmap, NAU8821_R0A_FLL7, 1351aab1ad11SSeven Lee (fll_param->fll_frac >> 16) & 0xff); 1352aab1ad11SSeven Lee regmap_write(regmap, NAU8821_R0B_FLL8, fll_param->fll_frac & 0xffff); 1353aab1ad11SSeven Lee /* FLL 10-bit integer input */ 1354aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R06_FLL3, 1355aab1ad11SSeven Lee NAU8821_FLL_INTEGER_MASK, fll_param->fll_int); 1356aab1ad11SSeven Lee /* FLL pre-scaler */ 1357aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R07_FLL4, 1358aab1ad11SSeven Lee NAU8821_HIGHBW_EN | NAU8821_FLL_REF_DIV_MASK, 1359aab1ad11SSeven Lee NAU8821_HIGHBW_EN | 1360aab1ad11SSeven Lee (fll_param->clk_ref_div << NAU8821_FLL_REF_DIV_SFT)); 1361aab1ad11SSeven Lee /* select divided VCO input */ 1362aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R08_FLL5, 1363aab1ad11SSeven Lee NAU8821_FLL_CLK_SW_MASK, NAU8821_FLL_CLK_SW_REF); 1364aab1ad11SSeven Lee /* Disable free-running mode */ 1365aab1ad11SSeven Lee regmap_update_bits(regmap, 1366aab1ad11SSeven Lee NAU8821_R09_FLL6, NAU8821_DCO_EN, 0); 1367aab1ad11SSeven Lee if (fll_param->fll_frac) { 1368aab1ad11SSeven Lee /* set FLL loop filter enable and cutoff frequency at 500Khz */ 1369aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R08_FLL5, 1370aab1ad11SSeven Lee NAU8821_FLL_PDB_DAC_EN | NAU8821_FLL_LOOP_FTR_EN | 1371aab1ad11SSeven Lee NAU8821_FLL_FTR_SW_MASK, 1372aab1ad11SSeven Lee NAU8821_FLL_PDB_DAC_EN | NAU8821_FLL_LOOP_FTR_EN | 1373aab1ad11SSeven Lee NAU8821_FLL_FTR_SW_FILTER); 1374aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R09_FLL6, 1375aab1ad11SSeven Lee NAU8821_SDM_EN | NAU8821_CUTOFF500, 1376aab1ad11SSeven Lee NAU8821_SDM_EN | NAU8821_CUTOFF500); 1377aab1ad11SSeven Lee } else { 1378aab1ad11SSeven Lee /* disable FLL loop filter and cutoff frequency */ 1379aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R08_FLL5, 1380aab1ad11SSeven Lee NAU8821_FLL_PDB_DAC_EN | NAU8821_FLL_LOOP_FTR_EN | 1381aab1ad11SSeven Lee NAU8821_FLL_FTR_SW_MASK, NAU8821_FLL_FTR_SW_ACCU); 1382aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R09_FLL6, 1383aab1ad11SSeven Lee NAU8821_SDM_EN | NAU8821_CUTOFF500, 0); 1384aab1ad11SSeven Lee } 1385aab1ad11SSeven Lee } 1386aab1ad11SSeven Lee 1387aab1ad11SSeven Lee /** 1388aab1ad11SSeven Lee * nau8821_set_fll - FLL configuration of nau8821 1389765e08bdSPierre-Louis Bossart * @component: codec component 1390765e08bdSPierre-Louis Bossart * @pll_id: PLL requested 1391765e08bdSPierre-Louis Bossart * @source: clock source 1392aab1ad11SSeven Lee * @freq_in: frequency of input clock source 1393aab1ad11SSeven Lee * @freq_out: must be 256*Fs in order to achieve the best performance 1394aab1ad11SSeven Lee * 1395aab1ad11SSeven Lee * The FLL function can select BCLK or MCLK as the input clock source. 1396aab1ad11SSeven Lee * 1397aab1ad11SSeven Lee * Returns 0 if the parameters have been applied successfully 1398aab1ad11SSeven Lee * or negative error code. 1399aab1ad11SSeven Lee */ 1400aab1ad11SSeven Lee static int nau8821_set_fll(struct snd_soc_component *component, 1401aab1ad11SSeven Lee int pll_id, int source, unsigned int freq_in, unsigned int freq_out) 1402aab1ad11SSeven Lee { 1403aab1ad11SSeven Lee struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 1404aab1ad11SSeven Lee struct nau8821_fll fll_set_param, *fll_param = &fll_set_param; 1405aab1ad11SSeven Lee int ret, fs; 1406aab1ad11SSeven Lee 1407aab1ad11SSeven Lee fs = freq_out >> 8; 1408aab1ad11SSeven Lee ret = nau8821_calc_fll_param(freq_in, fs, fll_param); 1409aab1ad11SSeven Lee if (ret) { 1410aab1ad11SSeven Lee dev_err(nau8821->dev, 1411aab1ad11SSeven Lee "Unsupported input clock %d to output clock %d\n", 1412aab1ad11SSeven Lee freq_in, freq_out); 1413aab1ad11SSeven Lee return ret; 1414aab1ad11SSeven Lee } 1415aab1ad11SSeven Lee dev_dbg(nau8821->dev, 1416aab1ad11SSeven Lee "mclk_src=%x ratio=%x fll_frac=%x fll_int=%x clk_ref_div=%x\n", 1417aab1ad11SSeven Lee fll_param->mclk_src, fll_param->ratio, fll_param->fll_frac, 1418aab1ad11SSeven Lee fll_param->fll_int, fll_param->clk_ref_div); 1419aab1ad11SSeven Lee 1420aab1ad11SSeven Lee nau8821_fll_apply(nau8821, fll_param); 1421aab1ad11SSeven Lee mdelay(2); 1422aab1ad11SSeven Lee regmap_update_bits(nau8821->regmap, NAU8821_R03_CLK_DIVIDER, 1423aab1ad11SSeven Lee NAU8821_CLK_SRC_MASK, NAU8821_CLK_SRC_VCO); 1424aab1ad11SSeven Lee 1425aab1ad11SSeven Lee return 0; 1426aab1ad11SSeven Lee } 1427aab1ad11SSeven Lee 1428aab1ad11SSeven Lee static void nau8821_configure_mclk_as_sysclk(struct regmap *regmap) 1429aab1ad11SSeven Lee { 1430aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R03_CLK_DIVIDER, 1431aab1ad11SSeven Lee NAU8821_CLK_SRC_MASK, NAU8821_CLK_SRC_MCLK); 1432aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R09_FLL6, 1433aab1ad11SSeven Lee NAU8821_DCO_EN, 0); 1434aab1ad11SSeven Lee /* Make DSP operate as default setting for power saving. */ 1435aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R04_FLL1, 1436aab1ad11SSeven Lee NAU8821_ICTRL_LATCH_MASK, 0); 1437aab1ad11SSeven Lee } 1438aab1ad11SSeven Lee 1439aab1ad11SSeven Lee static int nau8821_configure_sysclk(struct nau8821 *nau8821, 1440aab1ad11SSeven Lee int clk_id, unsigned int freq) 1441aab1ad11SSeven Lee { 1442aab1ad11SSeven Lee struct regmap *regmap = nau8821->regmap; 1443aab1ad11SSeven Lee 1444aab1ad11SSeven Lee switch (clk_id) { 1445aab1ad11SSeven Lee case NAU8821_CLK_DIS: 1446aab1ad11SSeven Lee /* Clock provided externally and disable internal VCO clock */ 1447aab1ad11SSeven Lee nau8821_configure_mclk_as_sysclk(regmap); 1448aab1ad11SSeven Lee break; 1449aab1ad11SSeven Lee case NAU8821_CLK_MCLK: 1450aab1ad11SSeven Lee nau8821_configure_mclk_as_sysclk(regmap); 1451aab1ad11SSeven Lee /* MCLK not changed by clock tree */ 1452aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R03_CLK_DIVIDER, 1453aab1ad11SSeven Lee NAU8821_CLK_MCLK_SRC_MASK, 0); 1454aab1ad11SSeven Lee break; 1455aab1ad11SSeven Lee case NAU8821_CLK_INTERNAL: 1456aab1ad11SSeven Lee if (nau8821_is_jack_inserted(regmap)) { 1457aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R09_FLL6, 1458aab1ad11SSeven Lee NAU8821_DCO_EN, NAU8821_DCO_EN); 1459aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R03_CLK_DIVIDER, 1460aab1ad11SSeven Lee NAU8821_CLK_SRC_MASK, NAU8821_CLK_SRC_VCO); 1461aab1ad11SSeven Lee /* Decrease the VCO frequency and make DSP operate 1462aab1ad11SSeven Lee * as default setting for power saving. 1463aab1ad11SSeven Lee */ 1464aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R03_CLK_DIVIDER, 1465aab1ad11SSeven Lee NAU8821_CLK_MCLK_SRC_MASK, 0xf); 1466aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R04_FLL1, 1467aab1ad11SSeven Lee NAU8821_ICTRL_LATCH_MASK | 1468aab1ad11SSeven Lee NAU8821_FLL_RATIO_MASK, 0x10); 1469aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R09_FLL6, 1470aab1ad11SSeven Lee NAU8821_SDM_EN, NAU8821_SDM_EN); 1471aab1ad11SSeven Lee } 1472aab1ad11SSeven Lee break; 1473aab1ad11SSeven Lee case NAU8821_CLK_FLL_MCLK: 1474aab1ad11SSeven Lee /* Higher FLL reference input frequency can only set lower 1475aab1ad11SSeven Lee * gain error, such as 0000 for input reference from MCLK 1476aab1ad11SSeven Lee * 12.288Mhz. 1477aab1ad11SSeven Lee */ 1478aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R06_FLL3, 1479aab1ad11SSeven Lee NAU8821_FLL_CLK_SRC_MASK | NAU8821_GAIN_ERR_MASK, 1480aab1ad11SSeven Lee NAU8821_FLL_CLK_SRC_MCLK | 0); 1481aab1ad11SSeven Lee break; 1482aab1ad11SSeven Lee case NAU8821_CLK_FLL_BLK: 1483aab1ad11SSeven Lee /* If FLL reference input is from low frequency source, 1484aab1ad11SSeven Lee * higher error gain can apply such as 0xf which has 1485aab1ad11SSeven Lee * the most sensitive gain error correction threshold, 1486aab1ad11SSeven Lee * Therefore, FLL has the most accurate DCO to 1487aab1ad11SSeven Lee * target frequency. 1488aab1ad11SSeven Lee */ 1489aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R06_FLL3, 1490aab1ad11SSeven Lee NAU8821_FLL_CLK_SRC_MASK | NAU8821_GAIN_ERR_MASK, 1491aab1ad11SSeven Lee NAU8821_FLL_CLK_SRC_BLK | 1492aab1ad11SSeven Lee (0xf << NAU8821_GAIN_ERR_SFT)); 1493aab1ad11SSeven Lee break; 1494aab1ad11SSeven Lee case NAU8821_CLK_FLL_FS: 1495aab1ad11SSeven Lee /* If FLL reference input is from low frequency source, 1496aab1ad11SSeven Lee * higher error gain can apply such as 0xf which has 1497aab1ad11SSeven Lee * the most sensitive gain error correction threshold, 1498aab1ad11SSeven Lee * Therefore, FLL has the most accurate DCO to 1499aab1ad11SSeven Lee * target frequency. 1500aab1ad11SSeven Lee */ 1501aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R06_FLL3, 1502aab1ad11SSeven Lee NAU8821_FLL_CLK_SRC_MASK | NAU8821_GAIN_ERR_MASK, 1503aab1ad11SSeven Lee NAU8821_FLL_CLK_SRC_FS | 1504aab1ad11SSeven Lee (0xf << NAU8821_GAIN_ERR_SFT)); 1505aab1ad11SSeven Lee break; 1506aab1ad11SSeven Lee default: 1507aab1ad11SSeven Lee dev_err(nau8821->dev, "Invalid clock id (%d)\n", clk_id); 1508aab1ad11SSeven Lee return -EINVAL; 1509aab1ad11SSeven Lee } 1510aab1ad11SSeven Lee nau8821->clk_id = clk_id; 1511aab1ad11SSeven Lee dev_dbg(nau8821->dev, "Sysclk is %dHz and clock id is %d\n", freq, 1512aab1ad11SSeven Lee nau8821->clk_id); 1513aab1ad11SSeven Lee 1514aab1ad11SSeven Lee return 0; 1515aab1ad11SSeven Lee } 1516aab1ad11SSeven Lee 1517aab1ad11SSeven Lee static int nau8821_set_sysclk(struct snd_soc_component *component, int clk_id, 1518aab1ad11SSeven Lee int source, unsigned int freq, int dir) 1519aab1ad11SSeven Lee { 1520aab1ad11SSeven Lee struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 1521aab1ad11SSeven Lee 1522aab1ad11SSeven Lee return nau8821_configure_sysclk(nau8821, clk_id, freq); 1523aab1ad11SSeven Lee } 1524aab1ad11SSeven Lee 1525aab1ad11SSeven Lee static int nau8821_resume_setup(struct nau8821 *nau8821) 1526aab1ad11SSeven Lee { 1527aab1ad11SSeven Lee struct regmap *regmap = nau8821->regmap; 1528aab1ad11SSeven Lee 1529aab1ad11SSeven Lee /* Close clock when jack type detection at manual mode */ 1530aab1ad11SSeven Lee nau8821_configure_sysclk(nau8821, NAU8821_CLK_DIS, 0); 1531aab1ad11SSeven Lee if (nau8821->irq) { 1532aab1ad11SSeven Lee /* Clear all interruption status */ 1533aab1ad11SSeven Lee nau8821_int_status_clear_all(regmap); 1534aab1ad11SSeven Lee 1535aab1ad11SSeven Lee /* Enable both insertion and ejection interruptions, and then 1536aab1ad11SSeven Lee * bypass de-bounce circuit. 1537aab1ad11SSeven Lee */ 1538aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK, 1539aab1ad11SSeven Lee NAU8821_IRQ_EJECT_EN | NAU8821_IRQ_INSERT_EN, 0); 1540aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL, 1541aab1ad11SSeven Lee NAU8821_JACK_DET_DB_BYPASS, 1542aab1ad11SSeven Lee NAU8821_JACK_DET_DB_BYPASS); 1543aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R12_INTERRUPT_DIS_CTRL, 1544aab1ad11SSeven Lee NAU8821_IRQ_INSERT_DIS | NAU8821_IRQ_EJECT_DIS, 0); 1545aab1ad11SSeven Lee } 1546aab1ad11SSeven Lee 1547aab1ad11SSeven Lee return 0; 1548aab1ad11SSeven Lee } 1549aab1ad11SSeven Lee 1550aab1ad11SSeven Lee static int nau8821_set_bias_level(struct snd_soc_component *component, 1551aab1ad11SSeven Lee enum snd_soc_bias_level level) 1552aab1ad11SSeven Lee { 1553aab1ad11SSeven Lee struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 1554aab1ad11SSeven Lee struct regmap *regmap = nau8821->regmap; 1555aab1ad11SSeven Lee 1556aab1ad11SSeven Lee switch (level) { 1557aab1ad11SSeven Lee case SND_SOC_BIAS_ON: 1558aab1ad11SSeven Lee break; 1559aab1ad11SSeven Lee 1560aab1ad11SSeven Lee case SND_SOC_BIAS_PREPARE: 1561aab1ad11SSeven Lee break; 1562aab1ad11SSeven Lee 1563aab1ad11SSeven Lee case SND_SOC_BIAS_STANDBY: 1564aab1ad11SSeven Lee /* Setup codec configuration after resume */ 1565aab1ad11SSeven Lee if (snd_soc_component_get_bias_level(component) == 1566aab1ad11SSeven Lee SND_SOC_BIAS_OFF) 1567aab1ad11SSeven Lee nau8821_resume_setup(nau8821); 1568aab1ad11SSeven Lee break; 1569aab1ad11SSeven Lee 1570aab1ad11SSeven Lee case SND_SOC_BIAS_OFF: 1571aab1ad11SSeven Lee /* HPL/HPR short to ground */ 1572aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL, 1573aab1ad11SSeven Lee NAU8821_SPKR_DWN1R | NAU8821_SPKR_DWN1L, 0); 1574aab1ad11SSeven Lee if (nau8821->irq) { 1575aab1ad11SSeven Lee /* Reset the configuration of jack type for detection. 1576aab1ad11SSeven Lee * Detach 2kOhm Resistors from MICBIAS to MICGND1/2. 1577aab1ad11SSeven Lee */ 1578aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R74_MIC_BIAS, 1579aab1ad11SSeven Lee NAU8821_MICBIAS_JKR2, 0); 1580aab1ad11SSeven Lee /* Turn off all interruptions before system shutdown. 1581aab1ad11SSeven Lee * Keep theinterruption quiet before resume 1582aab1ad11SSeven Lee * setup completes. 1583aab1ad11SSeven Lee */ 1584aab1ad11SSeven Lee regmap_write(regmap, 1585aab1ad11SSeven Lee NAU8821_R12_INTERRUPT_DIS_CTRL, 0xffff); 1586aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK, 1587aab1ad11SSeven Lee NAU8821_IRQ_EJECT_EN | NAU8821_IRQ_INSERT_EN, 1588aab1ad11SSeven Lee NAU8821_IRQ_EJECT_EN | NAU8821_IRQ_INSERT_EN); 1589aab1ad11SSeven Lee } 1590aab1ad11SSeven Lee break; 1591aab1ad11SSeven Lee default: 1592aab1ad11SSeven Lee break; 1593aab1ad11SSeven Lee } 1594aab1ad11SSeven Lee 1595aab1ad11SSeven Lee return 0; 1596aab1ad11SSeven Lee } 1597aab1ad11SSeven Lee 1598aab1ad11SSeven Lee static int __maybe_unused nau8821_suspend(struct snd_soc_component *component) 1599aab1ad11SSeven Lee { 1600aab1ad11SSeven Lee struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 1601aab1ad11SSeven Lee 1602aab1ad11SSeven Lee if (nau8821->irq) 1603aab1ad11SSeven Lee disable_irq(nau8821->irq); 1604aab1ad11SSeven Lee snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF); 1605aab1ad11SSeven Lee /* Power down codec power; don't support button wakeup */ 1606aab1ad11SSeven Lee snd_soc_component_disable_pin(component, "MICBIAS"); 1607aab1ad11SSeven Lee snd_soc_dapm_sync(nau8821->dapm); 1608aab1ad11SSeven Lee regcache_cache_only(nau8821->regmap, true); 1609aab1ad11SSeven Lee regcache_mark_dirty(nau8821->regmap); 1610aab1ad11SSeven Lee 1611aab1ad11SSeven Lee return 0; 1612aab1ad11SSeven Lee } 1613aab1ad11SSeven Lee 1614aab1ad11SSeven Lee static int __maybe_unused nau8821_resume(struct snd_soc_component *component) 1615aab1ad11SSeven Lee { 1616aab1ad11SSeven Lee struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 1617aab1ad11SSeven Lee 1618aab1ad11SSeven Lee regcache_cache_only(nau8821->regmap, false); 1619aab1ad11SSeven Lee regcache_sync(nau8821->regmap); 1620aab1ad11SSeven Lee if (nau8821->irq) 1621aab1ad11SSeven Lee enable_irq(nau8821->irq); 1622aab1ad11SSeven Lee 1623aab1ad11SSeven Lee return 0; 1624aab1ad11SSeven Lee } 1625aab1ad11SSeven Lee 1626aab1ad11SSeven Lee static const struct snd_soc_component_driver nau8821_component_driver = { 1627aab1ad11SSeven Lee .probe = nau8821_component_probe, 1628aab1ad11SSeven Lee .set_sysclk = nau8821_set_sysclk, 1629aab1ad11SSeven Lee .set_pll = nau8821_set_fll, 1630aab1ad11SSeven Lee .set_bias_level = nau8821_set_bias_level, 1631aab1ad11SSeven Lee .suspend = nau8821_suspend, 1632aab1ad11SSeven Lee .resume = nau8821_resume, 1633aab1ad11SSeven Lee .controls = nau8821_controls, 1634aab1ad11SSeven Lee .num_controls = ARRAY_SIZE(nau8821_controls), 1635aab1ad11SSeven Lee .dapm_widgets = nau8821_dapm_widgets, 1636aab1ad11SSeven Lee .num_dapm_widgets = ARRAY_SIZE(nau8821_dapm_widgets), 1637aab1ad11SSeven Lee .dapm_routes = nau8821_dapm_routes, 1638aab1ad11SSeven Lee .num_dapm_routes = ARRAY_SIZE(nau8821_dapm_routes), 1639aab1ad11SSeven Lee .suspend_bias_off = 1, 1640aab1ad11SSeven Lee .idle_bias_on = 1, 1641aab1ad11SSeven Lee .use_pmdown_time = 1, 1642aab1ad11SSeven Lee .endianness = 1, 1643aab1ad11SSeven Lee }; 1644aab1ad11SSeven Lee 1645aab1ad11SSeven Lee /** 1646aab1ad11SSeven Lee * nau8821_enable_jack_detect - Specify a jack for event reporting 1647aab1ad11SSeven Lee * 1648aab1ad11SSeven Lee * @component: component to register the jack with 1649aab1ad11SSeven Lee * @jack: jack to use to report headset and button events on 1650aab1ad11SSeven Lee * 1651aab1ad11SSeven Lee * After this function has been called the headset insert/remove and button 1652aab1ad11SSeven Lee * events will be routed to the given jack. Jack can be null to stop 1653aab1ad11SSeven Lee * reporting. 1654aab1ad11SSeven Lee */ 1655aab1ad11SSeven Lee int nau8821_enable_jack_detect(struct snd_soc_component *component, 1656aab1ad11SSeven Lee struct snd_soc_jack *jack) 1657aab1ad11SSeven Lee { 1658aab1ad11SSeven Lee struct nau8821 *nau8821 = snd_soc_component_get_drvdata(component); 1659aab1ad11SSeven Lee int ret; 1660aab1ad11SSeven Lee 1661aab1ad11SSeven Lee nau8821->jack = jack; 1662aab1ad11SSeven Lee /* Initiate jack detection work queue */ 1663aab1ad11SSeven Lee INIT_WORK(&nau8821->jdet_work, nau8821_jdet_work); 1664aab1ad11SSeven Lee ret = devm_request_threaded_irq(nau8821->dev, nau8821->irq, NULL, 1665aab1ad11SSeven Lee nau8821_interrupt, IRQF_TRIGGER_LOW | IRQF_ONESHOT, 1666aab1ad11SSeven Lee "nau8821", nau8821); 1667aab1ad11SSeven Lee if (ret) { 1668aab1ad11SSeven Lee dev_err(nau8821->dev, "Cannot request irq %d (%d)\n", 1669aab1ad11SSeven Lee nau8821->irq, ret); 1670aab1ad11SSeven Lee return ret; 1671aab1ad11SSeven Lee } 1672aab1ad11SSeven Lee 1673aab1ad11SSeven Lee return ret; 1674aab1ad11SSeven Lee } 1675aab1ad11SSeven Lee EXPORT_SYMBOL_GPL(nau8821_enable_jack_detect); 1676aab1ad11SSeven Lee 1677aab1ad11SSeven Lee static void nau8821_reset_chip(struct regmap *regmap) 1678aab1ad11SSeven Lee { 1679aab1ad11SSeven Lee regmap_write(regmap, NAU8821_R00_RESET, 0xffff); 1680aab1ad11SSeven Lee regmap_write(regmap, NAU8821_R00_RESET, 0xffff); 1681aab1ad11SSeven Lee } 1682aab1ad11SSeven Lee 1683aab1ad11SSeven Lee static void nau8821_print_device_properties(struct nau8821 *nau8821) 1684aab1ad11SSeven Lee { 1685aab1ad11SSeven Lee struct device *dev = nau8821->dev; 1686aab1ad11SSeven Lee 1687aab1ad11SSeven Lee dev_dbg(dev, "jkdet-enable: %d\n", nau8821->jkdet_enable); 1688aab1ad11SSeven Lee dev_dbg(dev, "jkdet-pull-enable: %d\n", nau8821->jkdet_pull_enable); 1689aab1ad11SSeven Lee dev_dbg(dev, "jkdet-pull-up: %d\n", nau8821->jkdet_pull_up); 1690aab1ad11SSeven Lee dev_dbg(dev, "jkdet-polarity: %d\n", nau8821->jkdet_polarity); 1691aab1ad11SSeven Lee dev_dbg(dev, "micbias-voltage: %d\n", nau8821->micbias_voltage); 1692aab1ad11SSeven Lee dev_dbg(dev, "vref-impedance: %d\n", nau8821->vref_impedance); 1693aab1ad11SSeven Lee dev_dbg(dev, "jack-insert-debounce: %d\n", 1694aab1ad11SSeven Lee nau8821->jack_insert_debounce); 1695aab1ad11SSeven Lee dev_dbg(dev, "jack-eject-debounce: %d\n", 1696aab1ad11SSeven Lee nau8821->jack_eject_debounce); 1697aab1ad11SSeven Lee dev_dbg(dev, "dmic-clk-threshold: %d\n", 1698aab1ad11SSeven Lee nau8821->dmic_clk_threshold); 16992551b6e8SSeven Lee dev_dbg(dev, "key_enable: %d\n", nau8821->key_enable); 1700*b37fdd42SSeven Lee dev_dbg(dev, "adc-delay-ms: %d\n", nau8821->adc_delay); 1701aab1ad11SSeven Lee } 1702aab1ad11SSeven Lee 1703aab1ad11SSeven Lee static int nau8821_read_device_properties(struct device *dev, 1704aab1ad11SSeven Lee struct nau8821 *nau8821) 1705aab1ad11SSeven Lee { 1706aab1ad11SSeven Lee int ret; 1707aab1ad11SSeven Lee 1708aab1ad11SSeven Lee nau8821->jkdet_enable = device_property_read_bool(dev, 1709aab1ad11SSeven Lee "nuvoton,jkdet-enable"); 1710aab1ad11SSeven Lee nau8821->jkdet_pull_enable = device_property_read_bool(dev, 1711aab1ad11SSeven Lee "nuvoton,jkdet-pull-enable"); 1712aab1ad11SSeven Lee nau8821->jkdet_pull_up = device_property_read_bool(dev, 1713aab1ad11SSeven Lee "nuvoton,jkdet-pull-up"); 17142551b6e8SSeven Lee nau8821->key_enable = device_property_read_bool(dev, 17152551b6e8SSeven Lee "nuvoton,key-enable"); 1716014ee069SSeven Lee nau8821->left_input_single_end = device_property_read_bool(dev, 1717014ee069SSeven Lee "nuvoton,left-input-single-end"); 1718aab1ad11SSeven Lee ret = device_property_read_u32(dev, "nuvoton,jkdet-polarity", 1719aab1ad11SSeven Lee &nau8821->jkdet_polarity); 1720aab1ad11SSeven Lee if (ret) 1721aab1ad11SSeven Lee nau8821->jkdet_polarity = 1; 1722aab1ad11SSeven Lee ret = device_property_read_u32(dev, "nuvoton,micbias-voltage", 1723aab1ad11SSeven Lee &nau8821->micbias_voltage); 1724aab1ad11SSeven Lee if (ret) 1725aab1ad11SSeven Lee nau8821->micbias_voltage = 6; 1726aab1ad11SSeven Lee ret = device_property_read_u32(dev, "nuvoton,vref-impedance", 1727aab1ad11SSeven Lee &nau8821->vref_impedance); 1728aab1ad11SSeven Lee if (ret) 1729aab1ad11SSeven Lee nau8821->vref_impedance = 2; 1730aab1ad11SSeven Lee ret = device_property_read_u32(dev, "nuvoton,jack-insert-debounce", 1731aab1ad11SSeven Lee &nau8821->jack_insert_debounce); 1732aab1ad11SSeven Lee if (ret) 1733aab1ad11SSeven Lee nau8821->jack_insert_debounce = 7; 1734aab1ad11SSeven Lee ret = device_property_read_u32(dev, "nuvoton,jack-eject-debounce", 1735aab1ad11SSeven Lee &nau8821->jack_eject_debounce); 1736aab1ad11SSeven Lee if (ret) 1737aab1ad11SSeven Lee nau8821->jack_eject_debounce = 0; 1738aab1ad11SSeven Lee ret = device_property_read_u32(dev, "nuvoton,dmic-clk-threshold", 1739aab1ad11SSeven Lee &nau8821->dmic_clk_threshold); 1740aab1ad11SSeven Lee if (ret) 1741aab1ad11SSeven Lee nau8821->dmic_clk_threshold = 3072000; 174291d1a18bSSeven Lee ret = device_property_read_u32(dev, "nuvoton,dmic-slew-rate", 174391d1a18bSSeven Lee &nau8821->dmic_slew_rate); 174491d1a18bSSeven Lee if (ret) 174591d1a18bSSeven Lee nau8821->dmic_slew_rate = 0; 1746*b37fdd42SSeven Lee ret = device_property_read_u32(dev, "nuvoton,adc-delay-ms", 1747*b37fdd42SSeven Lee &nau8821->adc_delay); 1748*b37fdd42SSeven Lee if (ret) 1749*b37fdd42SSeven Lee nau8821->adc_delay = 125; 1750*b37fdd42SSeven Lee if (nau8821->adc_delay < 125 || nau8821->adc_delay > 500) 1751*b37fdd42SSeven Lee dev_warn(dev, "Please set the suitable delay time!\n"); 1752aab1ad11SSeven Lee 1753aab1ad11SSeven Lee return 0; 1754aab1ad11SSeven Lee } 1755aab1ad11SSeven Lee 1756aab1ad11SSeven Lee static void nau8821_init_regs(struct nau8821 *nau8821) 1757aab1ad11SSeven Lee { 1758aab1ad11SSeven Lee struct regmap *regmap = nau8821->regmap; 1759aab1ad11SSeven Lee 1760aab1ad11SSeven Lee /* Enable Bias/Vmid */ 1761aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R66_BIAS_ADJ, 1762aab1ad11SSeven Lee NAU8821_BIAS_VMID, NAU8821_BIAS_VMID); 1763aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R76_BOOST, 1764aab1ad11SSeven Lee NAU8821_GLOBAL_BIAS_EN, NAU8821_GLOBAL_BIAS_EN); 1765aab1ad11SSeven Lee /* VMID Tieoff setting and enable TESTDAC. 1766aab1ad11SSeven Lee * This sets the analog DAC inputs to a '0' input signal to avoid 1767aab1ad11SSeven Lee * any glitches due to power up transients in both the analog and 1768aab1ad11SSeven Lee * digital DAC circuit. 1769aab1ad11SSeven Lee */ 1770aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R66_BIAS_ADJ, 1771aab1ad11SSeven Lee NAU8821_BIAS_VMID_SEL_MASK | NAU8821_BIAS_TESTDAC_EN, 1772aab1ad11SSeven Lee (nau8821->vref_impedance << NAU8821_BIAS_VMID_SEL_SFT) | 1773aab1ad11SSeven Lee NAU8821_BIAS_TESTDAC_EN); 1774aab1ad11SSeven Lee /* Disable short Frame Sync detection logic */ 1775aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R1E_LEFT_TIME_SLOT, 1776aab1ad11SSeven Lee NAU8821_DIS_FS_SHORT_DET, NAU8821_DIS_FS_SHORT_DET); 1777aab1ad11SSeven Lee /* Disable Boost Driver, Automatic Short circuit protection enable */ 1778aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R76_BOOST, 1779aab1ad11SSeven Lee NAU8821_PRECHARGE_DIS | NAU8821_HP_BOOST_DIS | 1780aab1ad11SSeven Lee NAU8821_HP_BOOST_G_DIS | NAU8821_SHORT_SHUTDOWN_EN, 1781aab1ad11SSeven Lee NAU8821_PRECHARGE_DIS | NAU8821_HP_BOOST_DIS | 1782aab1ad11SSeven Lee NAU8821_HP_BOOST_G_DIS | NAU8821_SHORT_SHUTDOWN_EN); 1783aab1ad11SSeven Lee /* Class G timer 64ms */ 1784aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R4B_CLASSG_CTRL, 1785aab1ad11SSeven Lee NAU8821_CLASSG_TIMER_MASK, 1786aab1ad11SSeven Lee 0x20 << NAU8821_CLASSG_TIMER_SFT); 1787aab1ad11SSeven Lee /* Class AB bias current to 2x, DAC Capacitor enable MSB/LSB */ 1788aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R6A_ANALOG_CONTROL_2, 1789aab1ad11SSeven Lee NAU8821_HP_NON_CLASSG_CURRENT_2xADJ | 1790aab1ad11SSeven Lee NAU8821_DAC_CAPACITOR_MSB | NAU8821_DAC_CAPACITOR_LSB, 1791aab1ad11SSeven Lee NAU8821_HP_NON_CLASSG_CURRENT_2xADJ | 1792aab1ad11SSeven Lee NAU8821_DAC_CAPACITOR_MSB | NAU8821_DAC_CAPACITOR_LSB); 1793aab1ad11SSeven Lee /* Disable DACR/L power */ 1794aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R80_CHARGE_PUMP, 1795aab1ad11SSeven Lee NAU8821_POWER_DOWN_DACR | NAU8821_POWER_DOWN_DACL, 0); 1796aab1ad11SSeven Lee /* DAC clock delay 2ns, VREF */ 1797aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R73_RDAC, 1798aab1ad11SSeven Lee NAU8821_DAC_CLK_DELAY_MASK | NAU8821_DAC_VREF_MASK, 1799aab1ad11SSeven Lee (0x2 << NAU8821_DAC_CLK_DELAY_SFT) | 1800aab1ad11SSeven Lee (0x3 << NAU8821_DAC_VREF_SFT)); 1801aab1ad11SSeven Lee 1802aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R74_MIC_BIAS, 1803aab1ad11SSeven Lee NAU8821_MICBIAS_VOLTAGE_MASK, nau8821->micbias_voltage); 1804aab1ad11SSeven Lee /* Default oversampling/decimations settings are unusable 1805aab1ad11SSeven Lee * (audible hiss). Set it to something better. 1806aab1ad11SSeven Lee */ 1807aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R2B_ADC_RATE, 1808aab1ad11SSeven Lee NAU8821_ADC_SYNC_DOWN_MASK, NAU8821_ADC_SYNC_DOWN_64); 1809aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R2C_DAC_CTRL1, 1810aab1ad11SSeven Lee NAU8821_DAC_OVERSAMPLE_MASK, NAU8821_DAC_OVERSAMPLE_64); 181191d1a18bSSeven Lee regmap_update_bits(regmap, NAU8821_R13_DMIC_CTRL, 181291d1a18bSSeven Lee NAU8821_DMIC_SLEW_MASK, nau8821->dmic_slew_rate << 181391d1a18bSSeven Lee NAU8821_DMIC_SLEW_SFT); 1814014ee069SSeven Lee if (nau8821->left_input_single_end) { 1815014ee069SSeven Lee regmap_update_bits(regmap, NAU8821_R6B_PGA_MUTE, 1816014ee069SSeven Lee NAU8821_MUTE_MICNL_EN, NAU8821_MUTE_MICNL_EN); 1817014ee069SSeven Lee regmap_update_bits(regmap, NAU8821_R74_MIC_BIAS, 1818014ee069SSeven Lee NAU8821_MICBIAS_LOWNOISE_EN, NAU8821_MICBIAS_LOWNOISE_EN); 1819014ee069SSeven Lee } 1820aab1ad11SSeven Lee } 1821aab1ad11SSeven Lee 1822aab1ad11SSeven Lee static int nau8821_setup_irq(struct nau8821 *nau8821) 1823aab1ad11SSeven Lee { 1824aab1ad11SSeven Lee struct regmap *regmap = nau8821->regmap; 1825aab1ad11SSeven Lee 1826aab1ad11SSeven Lee /* Jack detection */ 1827aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R1A_GPIO12_CTRL, 1828aab1ad11SSeven Lee NAU8821_JKDET_OUTPUT_EN, 1829aab1ad11SSeven Lee nau8821->jkdet_enable ? 0 : NAU8821_JKDET_OUTPUT_EN); 1830aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R1A_GPIO12_CTRL, 1831aab1ad11SSeven Lee NAU8821_JKDET_PULL_EN, 1832aab1ad11SSeven Lee nau8821->jkdet_pull_enable ? 0 : NAU8821_JKDET_PULL_EN); 1833aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R1A_GPIO12_CTRL, 1834aab1ad11SSeven Lee NAU8821_JKDET_PULL_UP, 1835aab1ad11SSeven Lee nau8821->jkdet_pull_up ? NAU8821_JKDET_PULL_UP : 0); 1836aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL, 1837aab1ad11SSeven Lee NAU8821_JACK_POLARITY, 1838aab1ad11SSeven Lee /* jkdet_polarity - 1 is for active-low */ 1839aab1ad11SSeven Lee nau8821->jkdet_polarity ? 0 : NAU8821_JACK_POLARITY); 1840aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL, 1841aab1ad11SSeven Lee NAU8821_JACK_INSERT_DEBOUNCE_MASK, 1842aab1ad11SSeven Lee nau8821->jack_insert_debounce << 1843aab1ad11SSeven Lee NAU8821_JACK_INSERT_DEBOUNCE_SFT); 1844aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R0D_JACK_DET_CTRL, 1845aab1ad11SSeven Lee NAU8821_JACK_EJECT_DEBOUNCE_MASK, 1846aab1ad11SSeven Lee nau8821->jack_eject_debounce << 1847aab1ad11SSeven Lee NAU8821_JACK_EJECT_DEBOUNCE_SFT); 1848aab1ad11SSeven Lee /* Pull up IRQ pin */ 1849aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK, 1850aab1ad11SSeven Lee NAU8821_IRQ_PIN_PULL_UP | NAU8821_IRQ_PIN_PULL_EN | 1851aab1ad11SSeven Lee NAU8821_IRQ_OUTPUT_EN, NAU8821_IRQ_PIN_PULL_UP | 1852aab1ad11SSeven Lee NAU8821_IRQ_PIN_PULL_EN | NAU8821_IRQ_OUTPUT_EN); 1853aab1ad11SSeven Lee /* Disable interruption before codec initiation done */ 1854aab1ad11SSeven Lee /* Mask unneeded IRQs: 1 - disable, 0 - enable */ 1855aab1ad11SSeven Lee regmap_update_bits(regmap, NAU8821_R0F_INTERRUPT_MASK, 0x3f5, 0x3f5); 1856aab1ad11SSeven Lee 1857aab1ad11SSeven Lee return 0; 1858aab1ad11SSeven Lee } 1859aab1ad11SSeven Lee 18601bc40efdSEdson Juliano Drosdeck /* Please keep this list alphabetically sorted */ 18611bc40efdSEdson Juliano Drosdeck static const struct dmi_system_id nau8821_quirk_table[] = { 18621bc40efdSEdson Juliano Drosdeck { 18631bc40efdSEdson Juliano Drosdeck /* Positivo CW14Q01P-V2 */ 18641bc40efdSEdson Juliano Drosdeck .matches = { 18651bc40efdSEdson Juliano Drosdeck DMI_MATCH(DMI_SYS_VENDOR, "Positivo Tecnologia SA"), 18661bc40efdSEdson Juliano Drosdeck DMI_MATCH(DMI_BOARD_NAME, "CW14Q01P-V2"), 18671bc40efdSEdson Juliano Drosdeck }, 18681bc40efdSEdson Juliano Drosdeck .driver_data = (void *)(NAU8821_JD_ACTIVE_HIGH), 18691bc40efdSEdson Juliano Drosdeck }, 18701bc40efdSEdson Juliano Drosdeck {} 18711bc40efdSEdson Juliano Drosdeck }; 18721bc40efdSEdson Juliano Drosdeck 18731bc40efdSEdson Juliano Drosdeck static void nau8821_check_quirks(void) 18741bc40efdSEdson Juliano Drosdeck { 18751bc40efdSEdson Juliano Drosdeck const struct dmi_system_id *dmi_id; 18761bc40efdSEdson Juliano Drosdeck 18771bc40efdSEdson Juliano Drosdeck if (quirk_override != -1) { 18781bc40efdSEdson Juliano Drosdeck nau8821_quirk = quirk_override; 18791bc40efdSEdson Juliano Drosdeck return; 18801bc40efdSEdson Juliano Drosdeck } 18811bc40efdSEdson Juliano Drosdeck 18821bc40efdSEdson Juliano Drosdeck dmi_id = dmi_first_match(nau8821_quirk_table); 18831bc40efdSEdson Juliano Drosdeck if (dmi_id) 18841bc40efdSEdson Juliano Drosdeck nau8821_quirk = (unsigned long)dmi_id->driver_data; 18851bc40efdSEdson Juliano Drosdeck } 18861bc40efdSEdson Juliano Drosdeck 18877325ed4dSStephen Kitt static int nau8821_i2c_probe(struct i2c_client *i2c) 1888aab1ad11SSeven Lee { 1889aab1ad11SSeven Lee struct device *dev = &i2c->dev; 1890aab1ad11SSeven Lee struct nau8821 *nau8821 = dev_get_platdata(&i2c->dev); 1891aab1ad11SSeven Lee int ret, value; 1892aab1ad11SSeven Lee 1893aab1ad11SSeven Lee if (!nau8821) { 1894aab1ad11SSeven Lee nau8821 = devm_kzalloc(dev, sizeof(*nau8821), GFP_KERNEL); 1895aab1ad11SSeven Lee if (!nau8821) 1896aab1ad11SSeven Lee return -ENOMEM; 1897aab1ad11SSeven Lee nau8821_read_device_properties(dev, nau8821); 1898aab1ad11SSeven Lee } 1899aab1ad11SSeven Lee i2c_set_clientdata(i2c, nau8821); 1900aab1ad11SSeven Lee 1901aab1ad11SSeven Lee nau8821->regmap = devm_regmap_init_i2c(i2c, &nau8821_regmap_config); 1902aab1ad11SSeven Lee if (IS_ERR(nau8821->regmap)) 1903aab1ad11SSeven Lee return PTR_ERR(nau8821->regmap); 1904aab1ad11SSeven Lee 1905aab1ad11SSeven Lee nau8821->dev = dev; 1906aab1ad11SSeven Lee nau8821->irq = i2c->irq; 19071bc40efdSEdson Juliano Drosdeck 19081bc40efdSEdson Juliano Drosdeck nau8821_check_quirks(); 19091bc40efdSEdson Juliano Drosdeck 19101bc40efdSEdson Juliano Drosdeck if (nau8821_quirk & NAU8821_JD_ACTIVE_HIGH) 19111bc40efdSEdson Juliano Drosdeck nau8821->jkdet_polarity = 0; 19121bc40efdSEdson Juliano Drosdeck 1913aab1ad11SSeven Lee nau8821_print_device_properties(nau8821); 1914aab1ad11SSeven Lee 1915aab1ad11SSeven Lee nau8821_reset_chip(nau8821->regmap); 1916aab1ad11SSeven Lee ret = regmap_read(nau8821->regmap, NAU8821_R58_I2C_DEVICE_ID, &value); 1917aab1ad11SSeven Lee if (ret) { 1918aab1ad11SSeven Lee dev_err(dev, "Failed to read device id (%d)\n", ret); 1919aab1ad11SSeven Lee return ret; 1920aab1ad11SSeven Lee } 1921aab1ad11SSeven Lee nau8821_init_regs(nau8821); 1922aab1ad11SSeven Lee 1923aab1ad11SSeven Lee if (i2c->irq) 1924aab1ad11SSeven Lee nau8821_setup_irq(nau8821); 1925aab1ad11SSeven Lee 1926aab1ad11SSeven Lee ret = devm_snd_soc_register_component(&i2c->dev, 1927aab1ad11SSeven Lee &nau8821_component_driver, &nau8821_dai, 1); 1928aab1ad11SSeven Lee 1929aab1ad11SSeven Lee return ret; 1930aab1ad11SSeven Lee } 1931aab1ad11SSeven Lee 1932aab1ad11SSeven Lee static const struct i2c_device_id nau8821_i2c_ids[] = { 1933aab1ad11SSeven Lee { "nau8821", 0 }, 1934aab1ad11SSeven Lee { } 1935aab1ad11SSeven Lee }; 1936aab1ad11SSeven Lee MODULE_DEVICE_TABLE(i2c, nau8821_i2c_ids); 1937aab1ad11SSeven Lee 1938aab1ad11SSeven Lee #ifdef CONFIG_OF 1939aab1ad11SSeven Lee static const struct of_device_id nau8821_of_ids[] = { 1940aab1ad11SSeven Lee { .compatible = "nuvoton,nau8821", }, 1941aab1ad11SSeven Lee {} 1942aab1ad11SSeven Lee }; 1943aab1ad11SSeven Lee MODULE_DEVICE_TABLE(of, nau8821_of_ids); 1944aab1ad11SSeven Lee #endif 1945aab1ad11SSeven Lee 1946aab1ad11SSeven Lee #ifdef CONFIG_ACPI 1947aab1ad11SSeven Lee static const struct acpi_device_id nau8821_acpi_match[] = { 1948aab1ad11SSeven Lee { "NVTN2020", 0 }, 1949aab1ad11SSeven Lee {}, 1950aab1ad11SSeven Lee }; 1951aab1ad11SSeven Lee MODULE_DEVICE_TABLE(acpi, nau8821_acpi_match); 1952aab1ad11SSeven Lee #endif 1953aab1ad11SSeven Lee 1954aab1ad11SSeven Lee static struct i2c_driver nau8821_driver = { 1955aab1ad11SSeven Lee .driver = { 1956aab1ad11SSeven Lee .name = "nau8821", 1957aab1ad11SSeven Lee .of_match_table = of_match_ptr(nau8821_of_ids), 1958aab1ad11SSeven Lee .acpi_match_table = ACPI_PTR(nau8821_acpi_match), 1959aab1ad11SSeven Lee }, 19609abcd240SUwe Kleine-König .probe = nau8821_i2c_probe, 1961aab1ad11SSeven Lee .id_table = nau8821_i2c_ids, 1962aab1ad11SSeven Lee }; 1963aab1ad11SSeven Lee module_i2c_driver(nau8821_driver); 1964aab1ad11SSeven Lee 1965aab1ad11SSeven Lee MODULE_DESCRIPTION("ASoC nau8821 driver"); 1966aab1ad11SSeven Lee MODULE_AUTHOR("John Hsu <kchsu0@nuvoton.com>"); 1967aab1ad11SSeven Lee MODULE_AUTHOR("Seven Lee <wtli@nuvoton.com>"); 1968aab1ad11SSeven Lee MODULE_LICENSE("GPL"); 1969