1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * NAU88L24 ALSA SoC audio driver 4 * 5 * Copyright 2016 Nuvoton Technology Corp. 6 * Author: John Hsu <KCHSU0@nuvoton.com> 7 */ 8 9 #include <linux/module.h> 10 #include <linux/delay.h> 11 #include <linux/dmi.h> 12 #include <linux/init.h> 13 #include <linux/i2c.h> 14 #include <linux/regmap.h> 15 #include <linux/slab.h> 16 #include <linux/clk.h> 17 #include <linux/acpi.h> 18 #include <linux/math64.h> 19 #include <linux/semaphore.h> 20 21 #include <sound/initval.h> 22 #include <sound/tlv.h> 23 #include <sound/core.h> 24 #include <sound/pcm.h> 25 #include <sound/pcm_params.h> 26 #include <sound/soc.h> 27 #include <sound/jack.h> 28 29 #include "nau8824.h" 30 31 #define NAU8824_JD_ACTIVE_HIGH BIT(0) 32 #define NAU8824_MONO_SPEAKER BIT(1) 33 34 static int nau8824_quirk; 35 static int quirk_override = -1; 36 module_param_named(quirk, quirk_override, uint, 0444); 37 MODULE_PARM_DESC(quirk, "Board-specific quirk override"); 38 39 static int nau8824_config_sysclk(struct nau8824 *nau8824, 40 int clk_id, unsigned int freq); 41 static bool nau8824_is_jack_inserted(struct nau8824 *nau8824); 42 43 /* the ADC threshold of headset */ 44 #define DMIC_CLK 3072000 45 46 /* the ADC threshold of headset */ 47 #define HEADSET_SARADC_THD 0x80 48 49 /* the parameter threshold of FLL */ 50 #define NAU_FREF_MAX 13500000 51 #define NAU_FVCO_MAX 100000000 52 #define NAU_FVCO_MIN 90000000 53 54 /* scaling for mclk from sysclk_src output */ 55 static const struct nau8824_fll_attr mclk_src_scaling[] = { 56 { 1, 0x0 }, 57 { 2, 0x2 }, 58 { 4, 0x3 }, 59 { 8, 0x4 }, 60 { 16, 0x5 }, 61 { 32, 0x6 }, 62 { 3, 0x7 }, 63 { 6, 0xa }, 64 { 12, 0xb }, 65 { 24, 0xc }, 66 }; 67 68 /* ratio for input clk freq */ 69 static const struct nau8824_fll_attr fll_ratio[] = { 70 { 512000, 0x01 }, 71 { 256000, 0x02 }, 72 { 128000, 0x04 }, 73 { 64000, 0x08 }, 74 { 32000, 0x10 }, 75 { 8000, 0x20 }, 76 { 4000, 0x40 }, 77 }; 78 79 static const struct nau8824_fll_attr fll_pre_scalar[] = { 80 { 1, 0x0 }, 81 { 2, 0x1 }, 82 { 4, 0x2 }, 83 { 8, 0x3 }, 84 }; 85 86 /* the maximum frequency of CLK_ADC and CLK_DAC */ 87 #define CLK_DA_AD_MAX 6144000 88 89 /* over sampling rate */ 90 static const struct nau8824_osr_attr osr_dac_sel[] = { 91 { 64, 2 }, /* OSR 64, SRC 1/4 */ 92 { 256, 0 }, /* OSR 256, SRC 1 */ 93 { 128, 1 }, /* OSR 128, SRC 1/2 */ 94 { 0, 0 }, 95 { 32, 3 }, /* OSR 32, SRC 1/8 */ 96 }; 97 98 static const struct nau8824_osr_attr osr_adc_sel[] = { 99 { 32, 3 }, /* OSR 32, SRC 1/8 */ 100 { 64, 2 }, /* OSR 64, SRC 1/4 */ 101 { 128, 1 }, /* OSR 128, SRC 1/2 */ 102 { 256, 0 }, /* OSR 256, SRC 1 */ 103 }; 104 105 static const struct reg_default nau8824_reg_defaults[] = { 106 { NAU8824_REG_ENA_CTRL, 0x0000 }, 107 { NAU8824_REG_CLK_GATING_ENA, 0x0000 }, 108 { NAU8824_REG_CLK_DIVIDER, 0x0000 }, 109 { NAU8824_REG_FLL1, 0x0000 }, 110 { NAU8824_REG_FLL2, 0x3126 }, 111 { NAU8824_REG_FLL3, 0x0008 }, 112 { NAU8824_REG_FLL4, 0x0010 }, 113 { NAU8824_REG_FLL5, 0xC000 }, 114 { NAU8824_REG_FLL6, 0x6000 }, 115 { NAU8824_REG_FLL_VCO_RSV, 0xF13C }, 116 { NAU8824_REG_JACK_DET_CTRL, 0x0000 }, 117 { NAU8824_REG_INTERRUPT_SETTING_1, 0x0000 }, 118 { NAU8824_REG_IRQ, 0x0000 }, 119 { NAU8824_REG_CLEAR_INT_REG, 0x0000 }, 120 { NAU8824_REG_INTERRUPT_SETTING, 0x1000 }, 121 { NAU8824_REG_SAR_ADC, 0x0015 }, 122 { NAU8824_REG_VDET_COEFFICIENT, 0x0110 }, 123 { NAU8824_REG_VDET_THRESHOLD_1, 0x0000 }, 124 { NAU8824_REG_VDET_THRESHOLD_2, 0x0000 }, 125 { NAU8824_REG_VDET_THRESHOLD_3, 0x0000 }, 126 { NAU8824_REG_VDET_THRESHOLD_4, 0x0000 }, 127 { NAU8824_REG_GPIO_SEL, 0x0000 }, 128 { NAU8824_REG_PORT0_I2S_PCM_CTRL_1, 0x000B }, 129 { NAU8824_REG_PORT0_I2S_PCM_CTRL_2, 0x0010 }, 130 { NAU8824_REG_PORT0_LEFT_TIME_SLOT, 0x0000 }, 131 { NAU8824_REG_PORT0_RIGHT_TIME_SLOT, 0x0000 }, 132 { NAU8824_REG_TDM_CTRL, 0x0000 }, 133 { NAU8824_REG_ADC_HPF_FILTER, 0x0000 }, 134 { NAU8824_REG_ADC_FILTER_CTRL, 0x0002 }, 135 { NAU8824_REG_DAC_FILTER_CTRL_1, 0x0000 }, 136 { NAU8824_REG_DAC_FILTER_CTRL_2, 0x0000 }, 137 { NAU8824_REG_NOTCH_FILTER_1, 0x0000 }, 138 { NAU8824_REG_NOTCH_FILTER_2, 0x0000 }, 139 { NAU8824_REG_EQ1_LOW, 0x112C }, 140 { NAU8824_REG_EQ2_EQ3, 0x2C2C }, 141 { NAU8824_REG_EQ4_EQ5, 0x2C2C }, 142 { NAU8824_REG_ADC_CH0_DGAIN_CTRL, 0x0100 }, 143 { NAU8824_REG_ADC_CH1_DGAIN_CTRL, 0x0100 }, 144 { NAU8824_REG_ADC_CH2_DGAIN_CTRL, 0x0100 }, 145 { NAU8824_REG_ADC_CH3_DGAIN_CTRL, 0x0100 }, 146 { NAU8824_REG_DAC_MUTE_CTRL, 0x0000 }, 147 { NAU8824_REG_DAC_CH0_DGAIN_CTRL, 0x0100 }, 148 { NAU8824_REG_DAC_CH1_DGAIN_CTRL, 0x0100 }, 149 { NAU8824_REG_ADC_TO_DAC_ST, 0x0000 }, 150 { NAU8824_REG_DRC_KNEE_IP12_ADC_CH01, 0x1486 }, 151 { NAU8824_REG_DRC_KNEE_IP34_ADC_CH01, 0x0F12 }, 152 { NAU8824_REG_DRC_SLOPE_ADC_CH01, 0x25FF }, 153 { NAU8824_REG_DRC_ATKDCY_ADC_CH01, 0x3457 }, 154 { NAU8824_REG_DRC_KNEE_IP12_ADC_CH23, 0x1486 }, 155 { NAU8824_REG_DRC_KNEE_IP34_ADC_CH23, 0x0F12 }, 156 { NAU8824_REG_DRC_SLOPE_ADC_CH23, 0x25FF }, 157 { NAU8824_REG_DRC_ATKDCY_ADC_CH23, 0x3457 }, 158 { NAU8824_REG_DRC_GAINL_ADC0, 0x0200 }, 159 { NAU8824_REG_DRC_GAINL_ADC1, 0x0200 }, 160 { NAU8824_REG_DRC_GAINL_ADC2, 0x0200 }, 161 { NAU8824_REG_DRC_GAINL_ADC3, 0x0200 }, 162 { NAU8824_REG_DRC_KNEE_IP12_DAC, 0x1486 }, 163 { NAU8824_REG_DRC_KNEE_IP34_DAC, 0x0F12 }, 164 { NAU8824_REG_DRC_SLOPE_DAC, 0x25F9 }, 165 { NAU8824_REG_DRC_ATKDCY_DAC, 0x3457 }, 166 { NAU8824_REG_DRC_GAIN_DAC_CH0, 0x0200 }, 167 { NAU8824_REG_DRC_GAIN_DAC_CH1, 0x0200 }, 168 { NAU8824_REG_MODE, 0x0000 }, 169 { NAU8824_REG_MODE1, 0x0000 }, 170 { NAU8824_REG_MODE2, 0x0000 }, 171 { NAU8824_REG_CLASSG, 0x0000 }, 172 { NAU8824_REG_OTP_EFUSE, 0x0000 }, 173 { NAU8824_REG_OTPDOUT_1, 0x0000 }, 174 { NAU8824_REG_OTPDOUT_2, 0x0000 }, 175 { NAU8824_REG_MISC_CTRL, 0x0000 }, 176 { NAU8824_REG_I2C_TIMEOUT, 0xEFFF }, 177 { NAU8824_REG_TEST_MODE, 0x0000 }, 178 { NAU8824_REG_I2C_DEVICE_ID, 0x1AF1 }, 179 { NAU8824_REG_SAR_ADC_DATA_OUT, 0x00FF }, 180 { NAU8824_REG_BIAS_ADJ, 0x0000 }, 181 { NAU8824_REG_PGA_GAIN, 0x0000 }, 182 { NAU8824_REG_TRIM_SETTINGS, 0x0000 }, 183 { NAU8824_REG_ANALOG_CONTROL_1, 0x0000 }, 184 { NAU8824_REG_ANALOG_CONTROL_2, 0x0000 }, 185 { NAU8824_REG_ENABLE_LO, 0x0000 }, 186 { NAU8824_REG_GAIN_LO, 0x0000 }, 187 { NAU8824_REG_CLASSD_GAIN_1, 0x0000 }, 188 { NAU8824_REG_CLASSD_GAIN_2, 0x0000 }, 189 { NAU8824_REG_ANALOG_ADC_1, 0x0011 }, 190 { NAU8824_REG_ANALOG_ADC_2, 0x0020 }, 191 { NAU8824_REG_RDAC, 0x0008 }, 192 { NAU8824_REG_MIC_BIAS, 0x0006 }, 193 { NAU8824_REG_HS_VOLUME_CONTROL, 0x0000 }, 194 { NAU8824_REG_BOOST, 0x0000 }, 195 { NAU8824_REG_FEPGA, 0x0000 }, 196 { NAU8824_REG_FEPGA_II, 0x0000 }, 197 { NAU8824_REG_FEPGA_SE, 0x0000 }, 198 { NAU8824_REG_FEPGA_ATTENUATION, 0x0000 }, 199 { NAU8824_REG_ATT_PORT0, 0x0000 }, 200 { NAU8824_REG_ATT_PORT1, 0x0000 }, 201 { NAU8824_REG_POWER_UP_CONTROL, 0x0000 }, 202 { NAU8824_REG_CHARGE_PUMP_CONTROL, 0x0300 }, 203 { NAU8824_REG_CHARGE_PUMP_INPUT, 0x0013 }, 204 }; 205 206 static int nau8824_sema_acquire(struct nau8824 *nau8824, long timeout) 207 { 208 int ret; 209 210 if (timeout) { 211 ret = down_timeout(&nau8824->jd_sem, timeout); 212 if (ret < 0) 213 dev_warn(nau8824->dev, "Acquire semaphore timeout\n"); 214 } else { 215 ret = down_interruptible(&nau8824->jd_sem); 216 if (ret < 0) 217 dev_warn(nau8824->dev, "Acquire semaphore fail\n"); 218 } 219 220 return ret; 221 } 222 223 static inline void nau8824_sema_release(struct nau8824 *nau8824) 224 { 225 up(&nau8824->jd_sem); 226 } 227 228 static bool nau8824_readable_reg(struct device *dev, unsigned int reg) 229 { 230 switch (reg) { 231 case NAU8824_REG_ENA_CTRL ... NAU8824_REG_FLL_VCO_RSV: 232 case NAU8824_REG_JACK_DET_CTRL: 233 case NAU8824_REG_INTERRUPT_SETTING_1: 234 case NAU8824_REG_IRQ: 235 case NAU8824_REG_CLEAR_INT_REG ... NAU8824_REG_VDET_THRESHOLD_4: 236 case NAU8824_REG_GPIO_SEL: 237 case NAU8824_REG_PORT0_I2S_PCM_CTRL_1 ... NAU8824_REG_TDM_CTRL: 238 case NAU8824_REG_ADC_HPF_FILTER ... NAU8824_REG_EQ4_EQ5: 239 case NAU8824_REG_ADC_CH0_DGAIN_CTRL ... NAU8824_REG_ADC_TO_DAC_ST: 240 case NAU8824_REG_DRC_KNEE_IP12_ADC_CH01 ... NAU8824_REG_DRC_GAINL_ADC3: 241 case NAU8824_REG_DRC_KNEE_IP12_DAC ... NAU8824_REG_DRC_GAIN_DAC_CH1: 242 case NAU8824_REG_CLASSG ... NAU8824_REG_OTP_EFUSE: 243 case NAU8824_REG_OTPDOUT_1 ... NAU8824_REG_OTPDOUT_2: 244 case NAU8824_REG_I2C_TIMEOUT: 245 case NAU8824_REG_I2C_DEVICE_ID ... NAU8824_REG_SAR_ADC_DATA_OUT: 246 case NAU8824_REG_BIAS_ADJ ... NAU8824_REG_CLASSD_GAIN_2: 247 case NAU8824_REG_ANALOG_ADC_1 ... NAU8824_REG_ATT_PORT1: 248 case NAU8824_REG_POWER_UP_CONTROL ... NAU8824_REG_CHARGE_PUMP_INPUT: 249 return true; 250 default: 251 return false; 252 } 253 254 } 255 256 static bool nau8824_writeable_reg(struct device *dev, unsigned int reg) 257 { 258 switch (reg) { 259 case NAU8824_REG_RESET ... NAU8824_REG_FLL_VCO_RSV: 260 case NAU8824_REG_JACK_DET_CTRL: 261 case NAU8824_REG_INTERRUPT_SETTING_1: 262 case NAU8824_REG_CLEAR_INT_REG ... NAU8824_REG_VDET_THRESHOLD_4: 263 case NAU8824_REG_GPIO_SEL: 264 case NAU8824_REG_PORT0_I2S_PCM_CTRL_1 ... NAU8824_REG_TDM_CTRL: 265 case NAU8824_REG_ADC_HPF_FILTER ... NAU8824_REG_EQ4_EQ5: 266 case NAU8824_REG_ADC_CH0_DGAIN_CTRL ... NAU8824_REG_ADC_TO_DAC_ST: 267 case NAU8824_REG_DRC_KNEE_IP12_ADC_CH01: 268 case NAU8824_REG_DRC_KNEE_IP34_ADC_CH01: 269 case NAU8824_REG_DRC_SLOPE_ADC_CH01: 270 case NAU8824_REG_DRC_ATKDCY_ADC_CH01: 271 case NAU8824_REG_DRC_KNEE_IP12_ADC_CH23: 272 case NAU8824_REG_DRC_KNEE_IP34_ADC_CH23: 273 case NAU8824_REG_DRC_SLOPE_ADC_CH23: 274 case NAU8824_REG_DRC_ATKDCY_ADC_CH23: 275 case NAU8824_REG_DRC_KNEE_IP12_DAC ... NAU8824_REG_DRC_ATKDCY_DAC: 276 case NAU8824_REG_CLASSG ... NAU8824_REG_OTP_EFUSE: 277 case NAU8824_REG_I2C_TIMEOUT: 278 case NAU8824_REG_BIAS_ADJ ... NAU8824_REG_CLASSD_GAIN_2: 279 case NAU8824_REG_ANALOG_ADC_1 ... NAU8824_REG_ATT_PORT1: 280 case NAU8824_REG_POWER_UP_CONTROL ... NAU8824_REG_CHARGE_PUMP_CONTROL: 281 return true; 282 default: 283 return false; 284 } 285 } 286 287 static bool nau8824_volatile_reg(struct device *dev, unsigned int reg) 288 { 289 switch (reg) { 290 case NAU8824_REG_RESET: 291 case NAU8824_REG_IRQ ... NAU8824_REG_CLEAR_INT_REG: 292 case NAU8824_REG_DRC_GAINL_ADC0 ... NAU8824_REG_DRC_GAINL_ADC3: 293 case NAU8824_REG_DRC_GAIN_DAC_CH0 ... NAU8824_REG_DRC_GAIN_DAC_CH1: 294 case NAU8824_REG_OTPDOUT_1 ... NAU8824_REG_OTPDOUT_2: 295 case NAU8824_REG_I2C_DEVICE_ID ... NAU8824_REG_SAR_ADC_DATA_OUT: 296 case NAU8824_REG_CHARGE_PUMP_INPUT: 297 return true; 298 default: 299 return false; 300 } 301 } 302 303 static const char * const nau8824_companding[] = { 304 "Off", "NC", "u-law", "A-law" }; 305 306 static const struct soc_enum nau8824_companding_adc_enum = 307 SOC_ENUM_SINGLE(NAU8824_REG_PORT0_I2S_PCM_CTRL_1, 12, 308 ARRAY_SIZE(nau8824_companding), nau8824_companding); 309 310 static const struct soc_enum nau8824_companding_dac_enum = 311 SOC_ENUM_SINGLE(NAU8824_REG_PORT0_I2S_PCM_CTRL_1, 14, 312 ARRAY_SIZE(nau8824_companding), nau8824_companding); 313 314 static const char * const nau8824_adc_decimation[] = { 315 "32", "64", "128", "256" }; 316 317 static const struct soc_enum nau8824_adc_decimation_enum = 318 SOC_ENUM_SINGLE(NAU8824_REG_ADC_FILTER_CTRL, 0, 319 ARRAY_SIZE(nau8824_adc_decimation), nau8824_adc_decimation); 320 321 static const char * const nau8824_dac_oversampl[] = { 322 "64", "256", "128", "", "32" }; 323 324 static const struct soc_enum nau8824_dac_oversampl_enum = 325 SOC_ENUM_SINGLE(NAU8824_REG_DAC_FILTER_CTRL_1, 0, 326 ARRAY_SIZE(nau8824_dac_oversampl), nau8824_dac_oversampl); 327 328 static const char * const nau8824_input_channel[] = { 329 "Input CH0", "Input CH1", "Input CH2", "Input CH3" }; 330 331 static const struct soc_enum nau8824_adc_ch0_enum = 332 SOC_ENUM_SINGLE(NAU8824_REG_ADC_CH0_DGAIN_CTRL, 9, 333 ARRAY_SIZE(nau8824_input_channel), nau8824_input_channel); 334 335 static const struct soc_enum nau8824_adc_ch1_enum = 336 SOC_ENUM_SINGLE(NAU8824_REG_ADC_CH1_DGAIN_CTRL, 9, 337 ARRAY_SIZE(nau8824_input_channel), nau8824_input_channel); 338 339 static const struct soc_enum nau8824_adc_ch2_enum = 340 SOC_ENUM_SINGLE(NAU8824_REG_ADC_CH2_DGAIN_CTRL, 9, 341 ARRAY_SIZE(nau8824_input_channel), nau8824_input_channel); 342 343 static const struct soc_enum nau8824_adc_ch3_enum = 344 SOC_ENUM_SINGLE(NAU8824_REG_ADC_CH3_DGAIN_CTRL, 9, 345 ARRAY_SIZE(nau8824_input_channel), nau8824_input_channel); 346 347 static const char * const nau8824_tdm_slot[] = { 348 "Slot 0", "Slot 1", "Slot 2", "Slot 3" }; 349 350 static const struct soc_enum nau8824_dac_left_sel_enum = 351 SOC_ENUM_SINGLE(NAU8824_REG_TDM_CTRL, 6, 352 ARRAY_SIZE(nau8824_tdm_slot), nau8824_tdm_slot); 353 354 static const struct soc_enum nau8824_dac_right_sel_enum = 355 SOC_ENUM_SINGLE(NAU8824_REG_TDM_CTRL, 4, 356 ARRAY_SIZE(nau8824_tdm_slot), nau8824_tdm_slot); 357 358 static const DECLARE_TLV_DB_MINMAX_MUTE(spk_vol_tlv, 0, 2400); 359 static const DECLARE_TLV_DB_MINMAX(hp_vol_tlv, -3000, 0); 360 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 200, 0); 361 static const DECLARE_TLV_DB_SCALE(dmic_vol_tlv, -12800, 50, 0); 362 363 static const struct snd_kcontrol_new nau8824_snd_controls[] = { 364 SOC_ENUM("ADC Companding", nau8824_companding_adc_enum), 365 SOC_ENUM("DAC Companding", nau8824_companding_dac_enum), 366 367 SOC_ENUM("ADC Decimation Rate", nau8824_adc_decimation_enum), 368 SOC_ENUM("DAC Oversampling Rate", nau8824_dac_oversampl_enum), 369 370 SOC_SINGLE_TLV("Speaker Right DACR Volume", 371 NAU8824_REG_CLASSD_GAIN_1, 8, 0x1f, 0, spk_vol_tlv), 372 SOC_SINGLE_TLV("Speaker Left DACL Volume", 373 NAU8824_REG_CLASSD_GAIN_2, 0, 0x1f, 0, spk_vol_tlv), 374 SOC_SINGLE_TLV("Speaker Left DACR Volume", 375 NAU8824_REG_CLASSD_GAIN_1, 0, 0x1f, 0, spk_vol_tlv), 376 SOC_SINGLE_TLV("Speaker Right DACL Volume", 377 NAU8824_REG_CLASSD_GAIN_2, 8, 0x1f, 0, spk_vol_tlv), 378 379 SOC_SINGLE_TLV("Headphone Right DACR Volume", 380 NAU8824_REG_ATT_PORT0, 8, 0x1f, 0, hp_vol_tlv), 381 SOC_SINGLE_TLV("Headphone Left DACL Volume", 382 NAU8824_REG_ATT_PORT0, 0, 0x1f, 0, hp_vol_tlv), 383 SOC_SINGLE_TLV("Headphone Right DACL Volume", 384 NAU8824_REG_ATT_PORT1, 8, 0x1f, 0, hp_vol_tlv), 385 SOC_SINGLE_TLV("Headphone Left DACR Volume", 386 NAU8824_REG_ATT_PORT1, 0, 0x1f, 0, hp_vol_tlv), 387 388 SOC_SINGLE_TLV("MIC1 Volume", NAU8824_REG_FEPGA_II, 389 NAU8824_FEPGA_GAINL_SFT, 0x12, 0, mic_vol_tlv), 390 SOC_SINGLE_TLV("MIC2 Volume", NAU8824_REG_FEPGA_II, 391 NAU8824_FEPGA_GAINR_SFT, 0x12, 0, mic_vol_tlv), 392 393 SOC_SINGLE_TLV("DMIC1 Volume", NAU8824_REG_ADC_CH0_DGAIN_CTRL, 394 0, 0x164, 0, dmic_vol_tlv), 395 SOC_SINGLE_TLV("DMIC2 Volume", NAU8824_REG_ADC_CH1_DGAIN_CTRL, 396 0, 0x164, 0, dmic_vol_tlv), 397 SOC_SINGLE_TLV("DMIC3 Volume", NAU8824_REG_ADC_CH2_DGAIN_CTRL, 398 0, 0x164, 0, dmic_vol_tlv), 399 SOC_SINGLE_TLV("DMIC4 Volume", NAU8824_REG_ADC_CH3_DGAIN_CTRL, 400 0, 0x164, 0, dmic_vol_tlv), 401 402 SOC_ENUM("ADC CH0 Select", nau8824_adc_ch0_enum), 403 SOC_ENUM("ADC CH1 Select", nau8824_adc_ch1_enum), 404 SOC_ENUM("ADC CH2 Select", nau8824_adc_ch2_enum), 405 SOC_ENUM("ADC CH3 Select", nau8824_adc_ch3_enum), 406 407 SOC_SINGLE("ADC CH0 TX Switch", NAU8824_REG_TDM_CTRL, 0, 1, 0), 408 SOC_SINGLE("ADC CH1 TX Switch", NAU8824_REG_TDM_CTRL, 1, 1, 0), 409 SOC_SINGLE("ADC CH2 TX Switch", NAU8824_REG_TDM_CTRL, 2, 1, 0), 410 SOC_SINGLE("ADC CH3 TX Switch", NAU8824_REG_TDM_CTRL, 3, 1, 0), 411 412 SOC_ENUM("DACL Channel Source", nau8824_dac_left_sel_enum), 413 SOC_ENUM("DACR Channel Source", nau8824_dac_right_sel_enum), 414 415 SOC_SINGLE("DACL LR Mix", NAU8824_REG_DAC_MUTE_CTRL, 0, 1, 0), 416 SOC_SINGLE("DACR LR Mix", NAU8824_REG_DAC_MUTE_CTRL, 1, 1, 0), 417 418 SOC_SINGLE("THD for key media", 419 NAU8824_REG_VDET_THRESHOLD_1, 8, 0xff, 0), 420 SOC_SINGLE("THD for key voice command", 421 NAU8824_REG_VDET_THRESHOLD_1, 0, 0xff, 0), 422 SOC_SINGLE("THD for key volume up", 423 NAU8824_REG_VDET_THRESHOLD_2, 8, 0xff, 0), 424 SOC_SINGLE("THD for key volume down", 425 NAU8824_REG_VDET_THRESHOLD_2, 0, 0xff, 0), 426 }; 427 428 static int nau8824_output_dac_event(struct snd_soc_dapm_widget *w, 429 struct snd_kcontrol *kcontrol, int event) 430 { 431 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 432 struct nau8824 *nau8824 = snd_soc_component_get_drvdata(component); 433 434 switch (event) { 435 case SND_SOC_DAPM_PRE_PMU: 436 /* Disables the TESTDAC to let DAC signal pass through. */ 437 regmap_update_bits(nau8824->regmap, NAU8824_REG_ENABLE_LO, 438 NAU8824_TEST_DAC_EN, 0); 439 break; 440 case SND_SOC_DAPM_POST_PMD: 441 regmap_update_bits(nau8824->regmap, NAU8824_REG_ENABLE_LO, 442 NAU8824_TEST_DAC_EN, NAU8824_TEST_DAC_EN); 443 break; 444 default: 445 return -EINVAL; 446 } 447 448 return 0; 449 } 450 451 static int nau8824_spk_event(struct snd_soc_dapm_widget *w, 452 struct snd_kcontrol *kcontrol, int event) 453 { 454 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 455 struct nau8824 *nau8824 = snd_soc_component_get_drvdata(component); 456 457 switch (event) { 458 case SND_SOC_DAPM_PRE_PMU: 459 regmap_update_bits(nau8824->regmap, 460 NAU8824_REG_ANALOG_CONTROL_2, 461 NAU8824_CLASSD_CLAMP_DIS, NAU8824_CLASSD_CLAMP_DIS); 462 break; 463 case SND_SOC_DAPM_POST_PMD: 464 regmap_update_bits(nau8824->regmap, 465 NAU8824_REG_ANALOG_CONTROL_2, 466 NAU8824_CLASSD_CLAMP_DIS, 0); 467 break; 468 default: 469 return -EINVAL; 470 } 471 472 return 0; 473 } 474 475 static int nau8824_pump_event(struct snd_soc_dapm_widget *w, 476 struct snd_kcontrol *kcontrol, int event) 477 { 478 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 479 struct nau8824 *nau8824 = snd_soc_component_get_drvdata(component); 480 481 switch (event) { 482 case SND_SOC_DAPM_POST_PMU: 483 /* Prevent startup click by letting charge pump to ramp up */ 484 msleep(10); 485 regmap_update_bits(nau8824->regmap, 486 NAU8824_REG_CHARGE_PUMP_CONTROL, 487 NAU8824_JAMNODCLOW, NAU8824_JAMNODCLOW); 488 break; 489 case SND_SOC_DAPM_PRE_PMD: 490 regmap_update_bits(nau8824->regmap, 491 NAU8824_REG_CHARGE_PUMP_CONTROL, 492 NAU8824_JAMNODCLOW, 0); 493 break; 494 default: 495 return -EINVAL; 496 } 497 498 return 0; 499 } 500 501 static int system_clock_control(struct snd_soc_dapm_widget *w, 502 struct snd_kcontrol *k, int event) 503 { 504 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 505 struct nau8824 *nau8824 = snd_soc_component_get_drvdata(component); 506 struct regmap *regmap = nau8824->regmap; 507 unsigned int value; 508 bool clk_fll, error; 509 510 if (SND_SOC_DAPM_EVENT_OFF(event)) { 511 dev_dbg(nau8824->dev, "system clock control : POWER OFF\n"); 512 /* Set clock source to disable or internal clock before the 513 * playback or capture end. Codec needs clock for Jack 514 * detection and button press if jack inserted; otherwise, 515 * the clock should be closed. 516 */ 517 if (nau8824_is_jack_inserted(nau8824)) { 518 nau8824_config_sysclk(nau8824, 519 NAU8824_CLK_INTERNAL, 0); 520 } else { 521 nau8824_config_sysclk(nau8824, NAU8824_CLK_DIS, 0); 522 } 523 } else { 524 dev_dbg(nau8824->dev, "system clock control : POWER ON\n"); 525 /* Check the clock source setting is proper or not 526 * no matter the source is from FLL or MCLK. 527 */ 528 regmap_read(regmap, NAU8824_REG_FLL1, &value); 529 clk_fll = value & NAU8824_FLL_RATIO_MASK; 530 /* It's error to use internal clock when playback */ 531 regmap_read(regmap, NAU8824_REG_FLL6, &value); 532 error = value & NAU8824_DCO_EN; 533 if (!error) { 534 /* Check error depending on source is FLL or MCLK. */ 535 regmap_read(regmap, NAU8824_REG_CLK_DIVIDER, &value); 536 if (clk_fll) 537 error = !(value & NAU8824_CLK_SRC_VCO); 538 else 539 error = value & NAU8824_CLK_SRC_VCO; 540 } 541 /* Recover the clock source setting if error. */ 542 if (error) { 543 if (clk_fll) { 544 regmap_update_bits(regmap, 545 NAU8824_REG_FLL6, NAU8824_DCO_EN, 0); 546 regmap_update_bits(regmap, 547 NAU8824_REG_CLK_DIVIDER, 548 NAU8824_CLK_SRC_MASK, 549 NAU8824_CLK_SRC_VCO); 550 } else { 551 nau8824_config_sysclk(nau8824, 552 NAU8824_CLK_MCLK, 0); 553 } 554 } 555 } 556 557 return 0; 558 } 559 560 static int dmic_clock_control(struct snd_soc_dapm_widget *w, 561 struct snd_kcontrol *k, int event) 562 { 563 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 564 struct nau8824 *nau8824 = snd_soc_component_get_drvdata(component); 565 int src; 566 567 /* The DMIC clock is gotten from system clock (256fs) divided by 568 * DMIC_SRC (1, 2, 4, 8, 16, 32). The clock has to be equal or 569 * less than 3.072 MHz. 570 */ 571 for (src = 0; src < 5; src++) { 572 if ((0x1 << (8 - src)) * nau8824->fs <= DMIC_CLK) 573 break; 574 } 575 dev_dbg(nau8824->dev, "dmic src %d for mclk %d\n", src, nau8824->fs * 256); 576 regmap_update_bits(nau8824->regmap, NAU8824_REG_CLK_DIVIDER, 577 NAU8824_CLK_DMIC_SRC_MASK, (src << NAU8824_CLK_DMIC_SRC_SFT)); 578 579 return 0; 580 } 581 582 static const struct snd_kcontrol_new nau8824_adc_ch0_dmic = 583 SOC_DAPM_SINGLE("Switch", NAU8824_REG_ENA_CTRL, 584 NAU8824_ADC_CH0_DMIC_SFT, 1, 0); 585 586 static const struct snd_kcontrol_new nau8824_adc_ch1_dmic = 587 SOC_DAPM_SINGLE("Switch", NAU8824_REG_ENA_CTRL, 588 NAU8824_ADC_CH1_DMIC_SFT, 1, 0); 589 590 static const struct snd_kcontrol_new nau8824_adc_ch2_dmic = 591 SOC_DAPM_SINGLE("Switch", NAU8824_REG_ENA_CTRL, 592 NAU8824_ADC_CH2_DMIC_SFT, 1, 0); 593 594 static const struct snd_kcontrol_new nau8824_adc_ch3_dmic = 595 SOC_DAPM_SINGLE("Switch", NAU8824_REG_ENA_CTRL, 596 NAU8824_ADC_CH3_DMIC_SFT, 1, 0); 597 598 static const struct snd_kcontrol_new nau8824_adc_left_mixer[] = { 599 SOC_DAPM_SINGLE("MIC Switch", NAU8824_REG_FEPGA, 600 NAU8824_FEPGA_MODEL_MIC1_SFT, 1, 0), 601 SOC_DAPM_SINGLE("HSMIC Switch", NAU8824_REG_FEPGA, 602 NAU8824_FEPGA_MODEL_HSMIC_SFT, 1, 0), 603 }; 604 605 static const struct snd_kcontrol_new nau8824_adc_right_mixer[] = { 606 SOC_DAPM_SINGLE("MIC Switch", NAU8824_REG_FEPGA, 607 NAU8824_FEPGA_MODER_MIC2_SFT, 1, 0), 608 SOC_DAPM_SINGLE("HSMIC Switch", NAU8824_REG_FEPGA, 609 NAU8824_FEPGA_MODER_HSMIC_SFT, 1, 0), 610 }; 611 612 static const struct snd_kcontrol_new nau8824_hp_left_mixer[] = { 613 SOC_DAPM_SINGLE("DAC Right Switch", NAU8824_REG_ENABLE_LO, 614 NAU8824_DACR_HPL_EN_SFT, 1, 0), 615 SOC_DAPM_SINGLE("DAC Left Switch", NAU8824_REG_ENABLE_LO, 616 NAU8824_DACL_HPL_EN_SFT, 1, 0), 617 }; 618 619 static const struct snd_kcontrol_new nau8824_hp_right_mixer[] = { 620 SOC_DAPM_SINGLE("DAC Left Switch", NAU8824_REG_ENABLE_LO, 621 NAU8824_DACL_HPR_EN_SFT, 1, 0), 622 SOC_DAPM_SINGLE("DAC Right Switch", NAU8824_REG_ENABLE_LO, 623 NAU8824_DACR_HPR_EN_SFT, 1, 0), 624 }; 625 626 static const char * const nau8824_dac_src[] = { "DACL", "DACR" }; 627 628 static SOC_ENUM_SINGLE_DECL( 629 nau8824_dacl_enum, NAU8824_REG_DAC_CH0_DGAIN_CTRL, 630 NAU8824_DAC_CH0_SEL_SFT, nau8824_dac_src); 631 632 static SOC_ENUM_SINGLE_DECL( 633 nau8824_dacr_enum, NAU8824_REG_DAC_CH1_DGAIN_CTRL, 634 NAU8824_DAC_CH1_SEL_SFT, nau8824_dac_src); 635 636 static const struct snd_kcontrol_new nau8824_dacl_mux = 637 SOC_DAPM_ENUM("DACL Source", nau8824_dacl_enum); 638 639 static const struct snd_kcontrol_new nau8824_dacr_mux = 640 SOC_DAPM_ENUM("DACR Source", nau8824_dacr_enum); 641 642 643 static const struct snd_soc_dapm_widget nau8824_dapm_widgets[] = { 644 SND_SOC_DAPM_SUPPLY("System Clock", SND_SOC_NOPM, 0, 0, 645 system_clock_control, SND_SOC_DAPM_POST_PMD | 646 SND_SOC_DAPM_POST_PMU), 647 648 SND_SOC_DAPM_INPUT("HSMIC1"), 649 SND_SOC_DAPM_INPUT("HSMIC2"), 650 SND_SOC_DAPM_INPUT("MIC1"), 651 SND_SOC_DAPM_INPUT("MIC2"), 652 SND_SOC_DAPM_INPUT("DMIC1"), 653 SND_SOC_DAPM_INPUT("DMIC2"), 654 SND_SOC_DAPM_INPUT("DMIC3"), 655 SND_SOC_DAPM_INPUT("DMIC4"), 656 657 SND_SOC_DAPM_SUPPLY("SAR", NAU8824_REG_SAR_ADC, 658 NAU8824_SAR_ADC_EN_SFT, 0, NULL, 0), 659 SND_SOC_DAPM_SUPPLY("MICBIAS", NAU8824_REG_MIC_BIAS, 660 NAU8824_MICBIAS_POWERUP_SFT, 0, NULL, 0), 661 SND_SOC_DAPM_SUPPLY("DMIC12 Power", NAU8824_REG_BIAS_ADJ, 662 NAU8824_DMIC1_EN_SFT, 0, NULL, 0), 663 SND_SOC_DAPM_SUPPLY("DMIC34 Power", NAU8824_REG_BIAS_ADJ, 664 NAU8824_DMIC2_EN_SFT, 0, NULL, 0), 665 SND_SOC_DAPM_SUPPLY("DMIC Clock", SND_SOC_NOPM, 0, 0, 666 dmic_clock_control, SND_SOC_DAPM_POST_PMU), 667 668 SND_SOC_DAPM_SWITCH("DMIC1 Enable", SND_SOC_NOPM, 669 0, 0, &nau8824_adc_ch0_dmic), 670 SND_SOC_DAPM_SWITCH("DMIC2 Enable", SND_SOC_NOPM, 671 0, 0, &nau8824_adc_ch1_dmic), 672 SND_SOC_DAPM_SWITCH("DMIC3 Enable", SND_SOC_NOPM, 673 0, 0, &nau8824_adc_ch2_dmic), 674 SND_SOC_DAPM_SWITCH("DMIC4 Enable", SND_SOC_NOPM, 675 0, 0, &nau8824_adc_ch3_dmic), 676 677 SND_SOC_DAPM_MIXER("Left ADC", NAU8824_REG_POWER_UP_CONTROL, 678 12, 0, nau8824_adc_left_mixer, 679 ARRAY_SIZE(nau8824_adc_left_mixer)), 680 SND_SOC_DAPM_MIXER("Right ADC", NAU8824_REG_POWER_UP_CONTROL, 681 13, 0, nau8824_adc_right_mixer, 682 ARRAY_SIZE(nau8824_adc_right_mixer)), 683 684 SND_SOC_DAPM_ADC("ADCL", NULL, NAU8824_REG_ANALOG_ADC_2, 685 NAU8824_ADCL_EN_SFT, 0), 686 SND_SOC_DAPM_ADC("ADCR", NULL, NAU8824_REG_ANALOG_ADC_2, 687 NAU8824_ADCR_EN_SFT, 0), 688 689 SND_SOC_DAPM_AIF_OUT("AIFTX", "Capture", 0, SND_SOC_NOPM, 0, 0), 690 SND_SOC_DAPM_AIF_IN("AIFRX", "Playback", 0, SND_SOC_NOPM, 0, 0), 691 692 SND_SOC_DAPM_DAC("DACL", NULL, NAU8824_REG_RDAC, 693 NAU8824_DACL_EN_SFT, 0), 694 SND_SOC_DAPM_SUPPLY("DACL Clock", NAU8824_REG_RDAC, 695 NAU8824_DACL_CLK_SFT, 0, NULL, 0), 696 SND_SOC_DAPM_DAC("DACR", NULL, NAU8824_REG_RDAC, 697 NAU8824_DACR_EN_SFT, 0), 698 SND_SOC_DAPM_SUPPLY("DACR Clock", NAU8824_REG_RDAC, 699 NAU8824_DACR_CLK_SFT, 0, NULL, 0), 700 701 SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &nau8824_dacl_mux), 702 SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &nau8824_dacr_mux), 703 704 SND_SOC_DAPM_PGA_S("Output DACL", 0, NAU8824_REG_CHARGE_PUMP_CONTROL, 705 8, 1, nau8824_output_dac_event, 706 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), 707 SND_SOC_DAPM_PGA_S("Output DACR", 0, NAU8824_REG_CHARGE_PUMP_CONTROL, 708 9, 1, nau8824_output_dac_event, 709 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), 710 711 SND_SOC_DAPM_PGA_S("ClassD", 0, NAU8824_REG_CLASSD_GAIN_1, 712 NAU8824_CLASSD_EN_SFT, 0, nau8824_spk_event, 713 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), 714 715 SND_SOC_DAPM_MIXER("Left Headphone", NAU8824_REG_CLASSG, 716 NAU8824_CLASSG_LDAC_EN_SFT, 0, nau8824_hp_left_mixer, 717 ARRAY_SIZE(nau8824_hp_left_mixer)), 718 SND_SOC_DAPM_MIXER("Right Headphone", NAU8824_REG_CLASSG, 719 NAU8824_CLASSG_RDAC_EN_SFT, 0, nau8824_hp_right_mixer, 720 ARRAY_SIZE(nau8824_hp_right_mixer)), 721 SND_SOC_DAPM_PGA_S("Charge Pump", 1, NAU8824_REG_CHARGE_PUMP_CONTROL, 722 NAU8824_CHARGE_PUMP_EN_SFT, 0, nau8824_pump_event, 723 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 724 SND_SOC_DAPM_PGA("Output Driver L", 725 NAU8824_REG_POWER_UP_CONTROL, 3, 0, NULL, 0), 726 SND_SOC_DAPM_PGA("Output Driver R", 727 NAU8824_REG_POWER_UP_CONTROL, 2, 0, NULL, 0), 728 SND_SOC_DAPM_PGA("Main Driver L", 729 NAU8824_REG_POWER_UP_CONTROL, 1, 0, NULL, 0), 730 SND_SOC_DAPM_PGA("Main Driver R", 731 NAU8824_REG_POWER_UP_CONTROL, 0, 0, NULL, 0), 732 SND_SOC_DAPM_PGA("HP Boost Driver", NAU8824_REG_BOOST, 733 NAU8824_HP_BOOST_DIS_SFT, 1, NULL, 0), 734 SND_SOC_DAPM_PGA("Class G", NAU8824_REG_CLASSG, 735 NAU8824_CLASSG_EN_SFT, 0, NULL, 0), 736 737 SND_SOC_DAPM_OUTPUT("SPKOUTL"), 738 SND_SOC_DAPM_OUTPUT("SPKOUTR"), 739 SND_SOC_DAPM_OUTPUT("HPOL"), 740 SND_SOC_DAPM_OUTPUT("HPOR"), 741 }; 742 743 static const struct snd_soc_dapm_route nau8824_dapm_routes[] = { 744 {"DMIC1 Enable", "Switch", "DMIC1"}, 745 {"DMIC2 Enable", "Switch", "DMIC2"}, 746 {"DMIC3 Enable", "Switch", "DMIC3"}, 747 {"DMIC4 Enable", "Switch", "DMIC4"}, 748 749 {"DMIC1", NULL, "DMIC12 Power"}, 750 {"DMIC2", NULL, "DMIC12 Power"}, 751 {"DMIC3", NULL, "DMIC34 Power"}, 752 {"DMIC4", NULL, "DMIC34 Power"}, 753 {"DMIC12 Power", NULL, "DMIC Clock"}, 754 {"DMIC34 Power", NULL, "DMIC Clock"}, 755 756 {"Left ADC", "MIC Switch", "MIC1"}, 757 {"Left ADC", "HSMIC Switch", "HSMIC1"}, 758 {"Right ADC", "MIC Switch", "MIC2"}, 759 {"Right ADC", "HSMIC Switch", "HSMIC2"}, 760 761 {"ADCL", NULL, "Left ADC"}, 762 {"ADCR", NULL, "Right ADC"}, 763 764 {"AIFTX", NULL, "MICBIAS"}, 765 {"AIFTX", NULL, "ADCL"}, 766 {"AIFTX", NULL, "ADCR"}, 767 {"AIFTX", NULL, "DMIC1 Enable"}, 768 {"AIFTX", NULL, "DMIC2 Enable"}, 769 {"AIFTX", NULL, "DMIC3 Enable"}, 770 {"AIFTX", NULL, "DMIC4 Enable"}, 771 772 {"AIFTX", NULL, "System Clock"}, 773 {"AIFRX", NULL, "System Clock"}, 774 775 {"DACL", NULL, "AIFRX"}, 776 {"DACL", NULL, "DACL Clock"}, 777 {"DACR", NULL, "AIFRX"}, 778 {"DACR", NULL, "DACR Clock"}, 779 780 {"DACL Mux", "DACL", "DACL"}, 781 {"DACL Mux", "DACR", "DACR"}, 782 {"DACR Mux", "DACL", "DACL"}, 783 {"DACR Mux", "DACR", "DACR"}, 784 785 {"Output DACL", NULL, "DACL Mux"}, 786 {"Output DACR", NULL, "DACR Mux"}, 787 788 {"ClassD", NULL, "Output DACL"}, 789 {"ClassD", NULL, "Output DACR"}, 790 791 {"Left Headphone", "DAC Left Switch", "Output DACL"}, 792 {"Left Headphone", "DAC Right Switch", "Output DACR"}, 793 {"Right Headphone", "DAC Left Switch", "Output DACL"}, 794 {"Right Headphone", "DAC Right Switch", "Output DACR"}, 795 796 {"Charge Pump", NULL, "Left Headphone"}, 797 {"Charge Pump", NULL, "Right Headphone"}, 798 {"Output Driver L", NULL, "Charge Pump"}, 799 {"Output Driver R", NULL, "Charge Pump"}, 800 {"Main Driver L", NULL, "Output Driver L"}, 801 {"Main Driver R", NULL, "Output Driver R"}, 802 {"Class G", NULL, "Main Driver L"}, 803 {"Class G", NULL, "Main Driver R"}, 804 {"HP Boost Driver", NULL, "Class G"}, 805 806 {"SPKOUTL", NULL, "ClassD"}, 807 {"SPKOUTR", NULL, "ClassD"}, 808 {"HPOL", NULL, "HP Boost Driver"}, 809 {"HPOR", NULL, "HP Boost Driver"}, 810 }; 811 812 static bool nau8824_is_jack_inserted(struct nau8824 *nau8824) 813 { 814 struct snd_soc_jack *jack = nau8824->jack; 815 bool insert = false; 816 817 if (nau8824->irq && jack) 818 insert = jack->status & SND_JACK_HEADPHONE; 819 820 return insert; 821 } 822 823 static void nau8824_int_status_clear_all(struct regmap *regmap) 824 { 825 int active_irq, clear_irq, i; 826 827 /* Reset the intrruption status from rightmost bit if the corres- 828 * ponding irq event occurs. 829 */ 830 regmap_read(regmap, NAU8824_REG_IRQ, &active_irq); 831 for (i = 0; i < NAU8824_REG_DATA_LEN; i++) { 832 clear_irq = (0x1 << i); 833 if (active_irq & clear_irq) 834 regmap_write(regmap, 835 NAU8824_REG_CLEAR_INT_REG, clear_irq); 836 } 837 } 838 839 static void nau8824_eject_jack(struct nau8824 *nau8824) 840 { 841 struct snd_soc_dapm_context *dapm = nau8824->dapm; 842 struct regmap *regmap = nau8824->regmap; 843 844 /* Clear all interruption status */ 845 nau8824_int_status_clear_all(regmap); 846 847 snd_soc_dapm_disable_pin(dapm, "SAR"); 848 snd_soc_dapm_disable_pin(dapm, "MICBIAS"); 849 snd_soc_dapm_sync(dapm); 850 851 /* Enable the insertion interruption, disable the ejection 852 * interruption, and then bypass de-bounce circuit. 853 */ 854 regmap_update_bits(regmap, NAU8824_REG_INTERRUPT_SETTING, 855 NAU8824_IRQ_KEY_RELEASE_DIS | NAU8824_IRQ_KEY_SHORT_PRESS_DIS | 856 NAU8824_IRQ_EJECT_DIS | NAU8824_IRQ_INSERT_DIS, 857 NAU8824_IRQ_KEY_RELEASE_DIS | NAU8824_IRQ_KEY_SHORT_PRESS_DIS | 858 NAU8824_IRQ_EJECT_DIS); 859 regmap_update_bits(regmap, NAU8824_REG_INTERRUPT_SETTING_1, 860 NAU8824_IRQ_INSERT_EN | NAU8824_IRQ_EJECT_EN, 861 NAU8824_IRQ_INSERT_EN); 862 regmap_update_bits(regmap, NAU8824_REG_ENA_CTRL, 863 NAU8824_JD_SLEEP_MODE, NAU8824_JD_SLEEP_MODE); 864 865 /* Close clock for jack type detection at manual mode */ 866 if (dapm->bias_level < SND_SOC_BIAS_PREPARE) 867 nau8824_config_sysclk(nau8824, NAU8824_CLK_DIS, 0); 868 } 869 870 static void nau8824_jdet_work(struct work_struct *work) 871 { 872 struct nau8824 *nau8824 = container_of( 873 work, struct nau8824, jdet_work); 874 struct snd_soc_dapm_context *dapm = nau8824->dapm; 875 struct regmap *regmap = nau8824->regmap; 876 int adc_value, event = 0, event_mask = 0; 877 878 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS"); 879 snd_soc_dapm_force_enable_pin(dapm, "SAR"); 880 snd_soc_dapm_sync(dapm); 881 882 msleep(100); 883 884 regmap_read(regmap, NAU8824_REG_SAR_ADC_DATA_OUT, &adc_value); 885 adc_value = adc_value & NAU8824_SAR_ADC_DATA_MASK; 886 dev_dbg(nau8824->dev, "SAR ADC data 0x%02x\n", adc_value); 887 if (adc_value < HEADSET_SARADC_THD) { 888 event |= SND_JACK_HEADPHONE; 889 890 snd_soc_dapm_disable_pin(dapm, "SAR"); 891 snd_soc_dapm_disable_pin(dapm, "MICBIAS"); 892 snd_soc_dapm_sync(dapm); 893 } else { 894 event |= SND_JACK_HEADSET; 895 } 896 event_mask |= SND_JACK_HEADSET; 897 snd_soc_jack_report(nau8824->jack, event, event_mask); 898 899 /* Enable short key press and release interruption. */ 900 regmap_update_bits(regmap, NAU8824_REG_INTERRUPT_SETTING, 901 NAU8824_IRQ_KEY_RELEASE_DIS | 902 NAU8824_IRQ_KEY_SHORT_PRESS_DIS, 0); 903 904 nau8824_sema_release(nau8824); 905 } 906 907 static void nau8824_setup_auto_irq(struct nau8824 *nau8824) 908 { 909 struct regmap *regmap = nau8824->regmap; 910 911 /* Enable jack ejection interruption. */ 912 regmap_update_bits(regmap, NAU8824_REG_INTERRUPT_SETTING_1, 913 NAU8824_IRQ_INSERT_EN | NAU8824_IRQ_EJECT_EN, 914 NAU8824_IRQ_EJECT_EN); 915 regmap_update_bits(regmap, NAU8824_REG_INTERRUPT_SETTING, 916 NAU8824_IRQ_EJECT_DIS, 0); 917 /* Enable internal VCO needed for interruptions */ 918 if (nau8824->dapm->bias_level < SND_SOC_BIAS_PREPARE) 919 nau8824_config_sysclk(nau8824, NAU8824_CLK_INTERNAL, 0); 920 regmap_update_bits(regmap, NAU8824_REG_ENA_CTRL, 921 NAU8824_JD_SLEEP_MODE, 0); 922 } 923 924 static int nau8824_button_decode(int value) 925 { 926 int buttons = 0; 927 928 /* The chip supports up to 8 buttons, but ALSA defines 929 * only 6 buttons. 930 */ 931 if (value & BIT(0)) 932 buttons |= SND_JACK_BTN_0; 933 if (value & BIT(1)) 934 buttons |= SND_JACK_BTN_1; 935 if (value & BIT(2)) 936 buttons |= SND_JACK_BTN_2; 937 if (value & BIT(3)) 938 buttons |= SND_JACK_BTN_3; 939 if (value & BIT(4)) 940 buttons |= SND_JACK_BTN_4; 941 if (value & BIT(5)) 942 buttons |= SND_JACK_BTN_5; 943 944 return buttons; 945 } 946 947 #define NAU8824_BUTTONS (SND_JACK_BTN_0 | SND_JACK_BTN_1 | \ 948 SND_JACK_BTN_2 | SND_JACK_BTN_3) 949 950 static irqreturn_t nau8824_interrupt(int irq, void *data) 951 { 952 struct nau8824 *nau8824 = (struct nau8824 *)data; 953 struct regmap *regmap = nau8824->regmap; 954 int active_irq, clear_irq = 0, event = 0, event_mask = 0; 955 956 if (regmap_read(regmap, NAU8824_REG_IRQ, &active_irq)) { 957 dev_err(nau8824->dev, "failed to read irq status\n"); 958 return IRQ_NONE; 959 } 960 dev_dbg(nau8824->dev, "IRQ %x\n", active_irq); 961 962 if (active_irq & NAU8824_JACK_EJECTION_DETECTED) { 963 nau8824_eject_jack(nau8824); 964 event_mask |= SND_JACK_HEADSET; 965 clear_irq = NAU8824_JACK_EJECTION_DETECTED; 966 /* release semaphore held after resume, 967 * and cancel jack detection 968 */ 969 nau8824_sema_release(nau8824); 970 cancel_work_sync(&nau8824->jdet_work); 971 } else if (active_irq & NAU8824_KEY_SHORT_PRESS_IRQ) { 972 int key_status, button_pressed; 973 974 regmap_read(regmap, NAU8824_REG_CLEAR_INT_REG, 975 &key_status); 976 977 /* lower 8 bits of the register are for pressed keys */ 978 button_pressed = nau8824_button_decode(key_status); 979 980 event |= button_pressed; 981 dev_dbg(nau8824->dev, "button %x pressed\n", event); 982 event_mask |= NAU8824_BUTTONS; 983 clear_irq = NAU8824_KEY_SHORT_PRESS_IRQ; 984 } else if (active_irq & NAU8824_KEY_RELEASE_IRQ) { 985 event_mask = NAU8824_BUTTONS; 986 clear_irq = NAU8824_KEY_RELEASE_IRQ; 987 } else if (active_irq & NAU8824_JACK_INSERTION_DETECTED) { 988 /* Turn off insertion interruption at manual mode */ 989 regmap_update_bits(regmap, 990 NAU8824_REG_INTERRUPT_SETTING, 991 NAU8824_IRQ_INSERT_DIS, 992 NAU8824_IRQ_INSERT_DIS); 993 regmap_update_bits(regmap, 994 NAU8824_REG_INTERRUPT_SETTING_1, 995 NAU8824_IRQ_INSERT_EN, 0); 996 /* detect microphone and jack type */ 997 cancel_work_sync(&nau8824->jdet_work); 998 schedule_work(&nau8824->jdet_work); 999 1000 /* Enable interruption for jack type detection at audo 1001 * mode which can detect microphone and jack type. 1002 */ 1003 nau8824_setup_auto_irq(nau8824); 1004 } 1005 1006 if (!clear_irq) 1007 clear_irq = active_irq; 1008 /* clears the rightmost interruption */ 1009 regmap_write(regmap, NAU8824_REG_CLEAR_INT_REG, clear_irq); 1010 1011 if (event_mask) 1012 snd_soc_jack_report(nau8824->jack, event, event_mask); 1013 1014 return IRQ_HANDLED; 1015 } 1016 1017 static const struct nau8824_osr_attr * 1018 nau8824_get_osr(struct nau8824 *nau8824, int stream) 1019 { 1020 unsigned int osr; 1021 1022 if (stream == SNDRV_PCM_STREAM_PLAYBACK) { 1023 regmap_read(nau8824->regmap, 1024 NAU8824_REG_DAC_FILTER_CTRL_1, &osr); 1025 osr &= NAU8824_DAC_OVERSAMPLE_MASK; 1026 if (osr >= ARRAY_SIZE(osr_dac_sel)) 1027 return NULL; 1028 return &osr_dac_sel[osr]; 1029 } else { 1030 regmap_read(nau8824->regmap, 1031 NAU8824_REG_ADC_FILTER_CTRL, &osr); 1032 osr &= NAU8824_ADC_SYNC_DOWN_MASK; 1033 if (osr >= ARRAY_SIZE(osr_adc_sel)) 1034 return NULL; 1035 return &osr_adc_sel[osr]; 1036 } 1037 } 1038 1039 static int nau8824_dai_startup(struct snd_pcm_substream *substream, 1040 struct snd_soc_dai *dai) 1041 { 1042 struct snd_soc_component *component = dai->component; 1043 struct nau8824 *nau8824 = snd_soc_component_get_drvdata(component); 1044 const struct nau8824_osr_attr *osr; 1045 1046 osr = nau8824_get_osr(nau8824, substream->stream); 1047 if (!osr || !osr->osr) 1048 return -EINVAL; 1049 1050 return snd_pcm_hw_constraint_minmax(substream->runtime, 1051 SNDRV_PCM_HW_PARAM_RATE, 1052 0, CLK_DA_AD_MAX / osr->osr); 1053 } 1054 1055 static int nau8824_hw_params(struct snd_pcm_substream *substream, 1056 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai) 1057 { 1058 struct snd_soc_component *component = dai->component; 1059 struct nau8824 *nau8824 = snd_soc_component_get_drvdata(component); 1060 unsigned int val_len = 0, ctrl_val, bclk_fs, bclk_div; 1061 const struct nau8824_osr_attr *osr; 1062 int err = -EINVAL; 1063 1064 nau8824_sema_acquire(nau8824, HZ); 1065 1066 /* CLK_DAC or CLK_ADC = OSR * FS 1067 * DAC or ADC clock frequency is defined as Over Sampling Rate (OSR) 1068 * multiplied by the audio sample rate (Fs). Note that the OSR and Fs 1069 * values must be selected such that the maximum frequency is less 1070 * than 6.144 MHz. 1071 */ 1072 nau8824->fs = params_rate(params); 1073 osr = nau8824_get_osr(nau8824, substream->stream); 1074 if (!osr || !osr->osr) 1075 goto error; 1076 if (nau8824->fs * osr->osr > CLK_DA_AD_MAX) 1077 goto error; 1078 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 1079 regmap_update_bits(nau8824->regmap, NAU8824_REG_CLK_DIVIDER, 1080 NAU8824_CLK_DAC_SRC_MASK, 1081 osr->clk_src << NAU8824_CLK_DAC_SRC_SFT); 1082 else 1083 regmap_update_bits(nau8824->regmap, NAU8824_REG_CLK_DIVIDER, 1084 NAU8824_CLK_ADC_SRC_MASK, 1085 osr->clk_src << NAU8824_CLK_ADC_SRC_SFT); 1086 1087 /* make BCLK and LRC divde configuration if the codec as master. */ 1088 regmap_read(nau8824->regmap, 1089 NAU8824_REG_PORT0_I2S_PCM_CTRL_2, &ctrl_val); 1090 if (ctrl_val & NAU8824_I2S_MS_MASTER) { 1091 /* get the bclk and fs ratio */ 1092 bclk_fs = snd_soc_params_to_bclk(params) / nau8824->fs; 1093 if (bclk_fs <= 32) 1094 bclk_div = 0x3; 1095 else if (bclk_fs <= 64) 1096 bclk_div = 0x2; 1097 else if (bclk_fs <= 128) 1098 bclk_div = 0x1; 1099 else if (bclk_fs <= 256) 1100 bclk_div = 0; 1101 else 1102 goto error; 1103 regmap_update_bits(nau8824->regmap, 1104 NAU8824_REG_PORT0_I2S_PCM_CTRL_2, 1105 NAU8824_I2S_LRC_DIV_MASK | NAU8824_I2S_BLK_DIV_MASK, 1106 (bclk_div << NAU8824_I2S_LRC_DIV_SFT) | bclk_div); 1107 } 1108 1109 switch (params_width(params)) { 1110 case 16: 1111 val_len |= NAU8824_I2S_DL_16; 1112 break; 1113 case 20: 1114 val_len |= NAU8824_I2S_DL_20; 1115 break; 1116 case 24: 1117 val_len |= NAU8824_I2S_DL_24; 1118 break; 1119 case 32: 1120 val_len |= NAU8824_I2S_DL_32; 1121 break; 1122 default: 1123 goto error; 1124 } 1125 1126 regmap_update_bits(nau8824->regmap, NAU8824_REG_PORT0_I2S_PCM_CTRL_1, 1127 NAU8824_I2S_DL_MASK, val_len); 1128 err = 0; 1129 1130 error: 1131 nau8824_sema_release(nau8824); 1132 1133 return err; 1134 } 1135 1136 static int nau8824_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 1137 { 1138 struct snd_soc_component *component = dai->component; 1139 struct nau8824 *nau8824 = snd_soc_component_get_drvdata(component); 1140 unsigned int ctrl1_val = 0, ctrl2_val = 0; 1141 1142 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 1143 case SND_SOC_DAIFMT_CBM_CFM: 1144 ctrl2_val |= NAU8824_I2S_MS_MASTER; 1145 break; 1146 case SND_SOC_DAIFMT_CBS_CFS: 1147 break; 1148 default: 1149 return -EINVAL; 1150 } 1151 1152 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 1153 case SND_SOC_DAIFMT_NB_NF: 1154 break; 1155 case SND_SOC_DAIFMT_IB_NF: 1156 ctrl1_val |= NAU8824_I2S_BP_INV; 1157 break; 1158 default: 1159 return -EINVAL; 1160 } 1161 1162 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 1163 case SND_SOC_DAIFMT_I2S: 1164 ctrl1_val |= NAU8824_I2S_DF_I2S; 1165 break; 1166 case SND_SOC_DAIFMT_LEFT_J: 1167 ctrl1_val |= NAU8824_I2S_DF_LEFT; 1168 break; 1169 case SND_SOC_DAIFMT_RIGHT_J: 1170 ctrl1_val |= NAU8824_I2S_DF_RIGTH; 1171 break; 1172 case SND_SOC_DAIFMT_DSP_A: 1173 ctrl1_val |= NAU8824_I2S_DF_PCM_AB; 1174 break; 1175 case SND_SOC_DAIFMT_DSP_B: 1176 ctrl1_val |= NAU8824_I2S_DF_PCM_AB; 1177 ctrl1_val |= NAU8824_I2S_PCMB_EN; 1178 break; 1179 default: 1180 return -EINVAL; 1181 } 1182 1183 nau8824_sema_acquire(nau8824, HZ); 1184 1185 regmap_update_bits(nau8824->regmap, NAU8824_REG_PORT0_I2S_PCM_CTRL_1, 1186 NAU8824_I2S_DF_MASK | NAU8824_I2S_BP_MASK | 1187 NAU8824_I2S_PCMB_EN, ctrl1_val); 1188 regmap_update_bits(nau8824->regmap, NAU8824_REG_PORT0_I2S_PCM_CTRL_2, 1189 NAU8824_I2S_MS_MASK, ctrl2_val); 1190 1191 nau8824_sema_release(nau8824); 1192 1193 return 0; 1194 } 1195 1196 /** 1197 * nau8824_set_tdm_slot - configure DAI TDM. 1198 * @dai: DAI 1199 * @tx_mask: Bitmask representing active TX slots. Ex. 1200 * 0xf for normal 4 channel TDM. 1201 * 0xf0 for shifted 4 channel TDM 1202 * @rx_mask: Bitmask [0:1] representing active DACR RX slots. 1203 * Bitmask [2:3] representing active DACL RX slots. 1204 * 00=CH0,01=CH1,10=CH2,11=CH3. Ex. 1205 * 0xf for DACL/R selecting TDM CH3. 1206 * 0xf0 for DACL/R selecting shifted TDM CH3. 1207 * @slots: Number of slots in use. 1208 * @slot_width: Width in bits for each slot. 1209 * 1210 * Configures a DAI for TDM operation. Only support 4 slots TDM. 1211 */ 1212 static int nau8824_set_tdm_slot(struct snd_soc_dai *dai, 1213 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) 1214 { 1215 struct snd_soc_component *component = dai->component; 1216 struct nau8824 *nau8824 = snd_soc_component_get_drvdata(component); 1217 unsigned int tslot_l = 0, ctrl_val = 0; 1218 1219 if (slots > 4 || ((tx_mask & 0xf0) && (tx_mask & 0xf)) || 1220 ((rx_mask & 0xf0) && (rx_mask & 0xf)) || 1221 ((rx_mask & 0xf0) && (tx_mask & 0xf)) || 1222 ((rx_mask & 0xf) && (tx_mask & 0xf0))) 1223 return -EINVAL; 1224 1225 ctrl_val |= (NAU8824_TDM_MODE | NAU8824_TDM_OFFSET_EN); 1226 if (tx_mask & 0xf0) { 1227 tslot_l = 4 * slot_width; 1228 ctrl_val |= (tx_mask >> 4); 1229 } else { 1230 ctrl_val |= tx_mask; 1231 } 1232 if (rx_mask & 0xf0) 1233 ctrl_val |= ((rx_mask >> 4) << NAU8824_TDM_DACR_RX_SFT); 1234 else 1235 ctrl_val |= (rx_mask << NAU8824_TDM_DACR_RX_SFT); 1236 1237 regmap_update_bits(nau8824->regmap, NAU8824_REG_TDM_CTRL, 1238 NAU8824_TDM_MODE | NAU8824_TDM_OFFSET_EN | 1239 NAU8824_TDM_DACL_RX_MASK | NAU8824_TDM_DACR_RX_MASK | 1240 NAU8824_TDM_TX_MASK, ctrl_val); 1241 regmap_update_bits(nau8824->regmap, NAU8824_REG_PORT0_LEFT_TIME_SLOT, 1242 NAU8824_TSLOT_L_MASK, tslot_l); 1243 1244 return 0; 1245 } 1246 1247 /** 1248 * nau8824_calc_fll_param - Calculate FLL parameters. 1249 * @fll_in: external clock provided to codec. 1250 * @fs: sampling rate. 1251 * @fll_param: Pointer to structure of FLL parameters. 1252 * 1253 * Calculate FLL parameters to configure codec. 1254 * 1255 * Returns 0 for success or negative error code. 1256 */ 1257 static int nau8824_calc_fll_param(unsigned int fll_in, 1258 unsigned int fs, struct nau8824_fll *fll_param) 1259 { 1260 u64 fvco, fvco_max; 1261 unsigned int fref, i, fvco_sel; 1262 1263 /* Ensure the reference clock frequency (FREF) is <= 13.5MHz by dividing 1264 * freq_in by 1, 2, 4, or 8 using FLL pre-scalar. 1265 * FREF = freq_in / NAU8824_FLL_REF_DIV_MASK 1266 */ 1267 for (i = 0; i < ARRAY_SIZE(fll_pre_scalar); i++) { 1268 fref = fll_in / fll_pre_scalar[i].param; 1269 if (fref <= NAU_FREF_MAX) 1270 break; 1271 } 1272 if (i == ARRAY_SIZE(fll_pre_scalar)) 1273 return -EINVAL; 1274 fll_param->clk_ref_div = fll_pre_scalar[i].val; 1275 1276 /* Choose the FLL ratio based on FREF */ 1277 for (i = 0; i < ARRAY_SIZE(fll_ratio); i++) { 1278 if (fref >= fll_ratio[i].param) 1279 break; 1280 } 1281 if (i == ARRAY_SIZE(fll_ratio)) 1282 return -EINVAL; 1283 fll_param->ratio = fll_ratio[i].val; 1284 1285 /* Calculate the frequency of DCO (FDCO) given freq_out = 256 * Fs. 1286 * FDCO must be within the 90MHz - 124MHz or the FFL cannot be 1287 * guaranteed across the full range of operation. 1288 * FDCO = freq_out * 2 * mclk_src_scaling 1289 */ 1290 fvco_max = 0; 1291 fvco_sel = ARRAY_SIZE(mclk_src_scaling); 1292 for (i = 0; i < ARRAY_SIZE(mclk_src_scaling); i++) { 1293 fvco = 256ULL * fs * 2 * mclk_src_scaling[i].param; 1294 if (fvco > NAU_FVCO_MIN && fvco < NAU_FVCO_MAX && 1295 fvco_max < fvco) { 1296 fvco_max = fvco; 1297 fvco_sel = i; 1298 } 1299 } 1300 if (ARRAY_SIZE(mclk_src_scaling) == fvco_sel) 1301 return -EINVAL; 1302 fll_param->mclk_src = mclk_src_scaling[fvco_sel].val; 1303 1304 /* Calculate the FLL 10-bit integer input and the FLL 16-bit fractional 1305 * input based on FDCO, FREF and FLL ratio. 1306 */ 1307 fvco = div_u64(fvco_max << 16, fref * fll_param->ratio); 1308 fll_param->fll_int = (fvco >> 16) & 0x3FF; 1309 fll_param->fll_frac = fvco & 0xFFFF; 1310 return 0; 1311 } 1312 1313 static void nau8824_fll_apply(struct regmap *regmap, 1314 struct nau8824_fll *fll_param) 1315 { 1316 regmap_update_bits(regmap, NAU8824_REG_CLK_DIVIDER, 1317 NAU8824_CLK_SRC_MASK | NAU8824_CLK_MCLK_SRC_MASK, 1318 NAU8824_CLK_SRC_MCLK | fll_param->mclk_src); 1319 regmap_update_bits(regmap, NAU8824_REG_FLL1, 1320 NAU8824_FLL_RATIO_MASK, fll_param->ratio); 1321 /* FLL 16-bit fractional input */ 1322 regmap_write(regmap, NAU8824_REG_FLL2, fll_param->fll_frac); 1323 /* FLL 10-bit integer input */ 1324 regmap_update_bits(regmap, NAU8824_REG_FLL3, 1325 NAU8824_FLL_INTEGER_MASK, fll_param->fll_int); 1326 /* FLL pre-scaler */ 1327 regmap_update_bits(regmap, NAU8824_REG_FLL4, 1328 NAU8824_FLL_REF_DIV_MASK, 1329 fll_param->clk_ref_div << NAU8824_FLL_REF_DIV_SFT); 1330 /* select divided VCO input */ 1331 regmap_update_bits(regmap, NAU8824_REG_FLL5, 1332 NAU8824_FLL_CLK_SW_MASK, NAU8824_FLL_CLK_SW_REF); 1333 /* Disable free-running mode */ 1334 regmap_update_bits(regmap, 1335 NAU8824_REG_FLL6, NAU8824_DCO_EN, 0); 1336 if (fll_param->fll_frac) { 1337 regmap_update_bits(regmap, NAU8824_REG_FLL5, 1338 NAU8824_FLL_PDB_DAC_EN | NAU8824_FLL_LOOP_FTR_EN | 1339 NAU8824_FLL_FTR_SW_MASK, 1340 NAU8824_FLL_PDB_DAC_EN | NAU8824_FLL_LOOP_FTR_EN | 1341 NAU8824_FLL_FTR_SW_FILTER); 1342 regmap_update_bits(regmap, NAU8824_REG_FLL6, 1343 NAU8824_SDM_EN, NAU8824_SDM_EN); 1344 } else { 1345 regmap_update_bits(regmap, NAU8824_REG_FLL5, 1346 NAU8824_FLL_PDB_DAC_EN | NAU8824_FLL_LOOP_FTR_EN | 1347 NAU8824_FLL_FTR_SW_MASK, NAU8824_FLL_FTR_SW_ACCU); 1348 regmap_update_bits(regmap, 1349 NAU8824_REG_FLL6, NAU8824_SDM_EN, 0); 1350 } 1351 } 1352 1353 /* freq_out must be 256*Fs in order to achieve the best performance */ 1354 static int nau8824_set_pll(struct snd_soc_component *component, int pll_id, int source, 1355 unsigned int freq_in, unsigned int freq_out) 1356 { 1357 struct nau8824 *nau8824 = snd_soc_component_get_drvdata(component); 1358 struct nau8824_fll fll_param; 1359 int ret, fs; 1360 1361 fs = freq_out / 256; 1362 ret = nau8824_calc_fll_param(freq_in, fs, &fll_param); 1363 if (ret < 0) { 1364 dev_err(nau8824->dev, "Unsupported input clock %d\n", freq_in); 1365 return ret; 1366 } 1367 dev_dbg(nau8824->dev, "mclk_src=%x ratio=%x fll_frac=%x fll_int=%x clk_ref_div=%x\n", 1368 fll_param.mclk_src, fll_param.ratio, fll_param.fll_frac, 1369 fll_param.fll_int, fll_param.clk_ref_div); 1370 1371 nau8824_fll_apply(nau8824->regmap, &fll_param); 1372 mdelay(2); 1373 regmap_update_bits(nau8824->regmap, NAU8824_REG_CLK_DIVIDER, 1374 NAU8824_CLK_SRC_MASK, NAU8824_CLK_SRC_VCO); 1375 1376 return 0; 1377 } 1378 1379 static int nau8824_config_sysclk(struct nau8824 *nau8824, 1380 int clk_id, unsigned int freq) 1381 { 1382 struct regmap *regmap = nau8824->regmap; 1383 1384 switch (clk_id) { 1385 case NAU8824_CLK_DIS: 1386 regmap_update_bits(regmap, NAU8824_REG_CLK_DIVIDER, 1387 NAU8824_CLK_SRC_MASK, NAU8824_CLK_SRC_MCLK); 1388 regmap_update_bits(regmap, NAU8824_REG_FLL6, 1389 NAU8824_DCO_EN, 0); 1390 break; 1391 1392 case NAU8824_CLK_MCLK: 1393 nau8824_sema_acquire(nau8824, HZ); 1394 regmap_update_bits(regmap, NAU8824_REG_CLK_DIVIDER, 1395 NAU8824_CLK_SRC_MASK, NAU8824_CLK_SRC_MCLK); 1396 regmap_update_bits(regmap, NAU8824_REG_FLL6, 1397 NAU8824_DCO_EN, 0); 1398 nau8824_sema_release(nau8824); 1399 break; 1400 1401 case NAU8824_CLK_INTERNAL: 1402 regmap_update_bits(regmap, NAU8824_REG_FLL6, 1403 NAU8824_DCO_EN, NAU8824_DCO_EN); 1404 regmap_update_bits(regmap, NAU8824_REG_CLK_DIVIDER, 1405 NAU8824_CLK_SRC_MASK, NAU8824_CLK_SRC_VCO); 1406 break; 1407 1408 case NAU8824_CLK_FLL_MCLK: 1409 nau8824_sema_acquire(nau8824, HZ); 1410 regmap_update_bits(regmap, NAU8824_REG_FLL3, 1411 NAU8824_FLL_CLK_SRC_MASK, NAU8824_FLL_CLK_SRC_MCLK); 1412 nau8824_sema_release(nau8824); 1413 break; 1414 1415 case NAU8824_CLK_FLL_BLK: 1416 nau8824_sema_acquire(nau8824, HZ); 1417 regmap_update_bits(regmap, NAU8824_REG_FLL3, 1418 NAU8824_FLL_CLK_SRC_MASK, NAU8824_FLL_CLK_SRC_BLK); 1419 nau8824_sema_release(nau8824); 1420 break; 1421 1422 case NAU8824_CLK_FLL_FS: 1423 nau8824_sema_acquire(nau8824, HZ); 1424 regmap_update_bits(regmap, NAU8824_REG_FLL3, 1425 NAU8824_FLL_CLK_SRC_MASK, NAU8824_FLL_CLK_SRC_FS); 1426 nau8824_sema_release(nau8824); 1427 break; 1428 1429 default: 1430 dev_err(nau8824->dev, "Invalid clock id (%d)\n", clk_id); 1431 return -EINVAL; 1432 } 1433 1434 dev_dbg(nau8824->dev, "Sysclk is %dHz and clock id is %d\n", freq, 1435 clk_id); 1436 1437 return 0; 1438 } 1439 1440 static int nau8824_set_sysclk(struct snd_soc_component *component, 1441 int clk_id, int source, unsigned int freq, int dir) 1442 { 1443 struct nau8824 *nau8824 = snd_soc_component_get_drvdata(component); 1444 1445 return nau8824_config_sysclk(nau8824, clk_id, freq); 1446 } 1447 1448 static void nau8824_resume_setup(struct nau8824 *nau8824) 1449 { 1450 nau8824_config_sysclk(nau8824, NAU8824_CLK_DIS, 0); 1451 if (nau8824->irq) { 1452 /* Clear all interruption status */ 1453 nau8824_int_status_clear_all(nau8824->regmap); 1454 /* Enable jack detection at sleep mode, insertion detection, 1455 * and ejection detection. 1456 */ 1457 regmap_update_bits(nau8824->regmap, NAU8824_REG_ENA_CTRL, 1458 NAU8824_JD_SLEEP_MODE, NAU8824_JD_SLEEP_MODE); 1459 regmap_update_bits(nau8824->regmap, 1460 NAU8824_REG_INTERRUPT_SETTING_1, 1461 NAU8824_IRQ_EJECT_EN | NAU8824_IRQ_INSERT_EN, 1462 NAU8824_IRQ_EJECT_EN | NAU8824_IRQ_INSERT_EN); 1463 regmap_update_bits(nau8824->regmap, 1464 NAU8824_REG_INTERRUPT_SETTING, 1465 NAU8824_IRQ_EJECT_DIS | NAU8824_IRQ_INSERT_DIS, 0); 1466 } 1467 } 1468 1469 static int nau8824_set_bias_level(struct snd_soc_component *component, 1470 enum snd_soc_bias_level level) 1471 { 1472 struct nau8824 *nau8824 = snd_soc_component_get_drvdata(component); 1473 1474 switch (level) { 1475 case SND_SOC_BIAS_ON: 1476 break; 1477 1478 case SND_SOC_BIAS_PREPARE: 1479 break; 1480 1481 case SND_SOC_BIAS_STANDBY: 1482 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) { 1483 /* Setup codec configuration after resume */ 1484 nau8824_resume_setup(nau8824); 1485 } 1486 break; 1487 1488 case SND_SOC_BIAS_OFF: 1489 regmap_update_bits(nau8824->regmap, 1490 NAU8824_REG_INTERRUPT_SETTING, 0x3ff, 0x3ff); 1491 regmap_update_bits(nau8824->regmap, 1492 NAU8824_REG_INTERRUPT_SETTING_1, 1493 NAU8824_IRQ_EJECT_EN | NAU8824_IRQ_INSERT_EN, 0); 1494 break; 1495 } 1496 1497 return 0; 1498 } 1499 1500 static int nau8824_component_probe(struct snd_soc_component *component) 1501 { 1502 struct nau8824 *nau8824 = snd_soc_component_get_drvdata(component); 1503 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 1504 1505 nau8824->dapm = dapm; 1506 1507 return 0; 1508 } 1509 1510 static int __maybe_unused nau8824_suspend(struct snd_soc_component *component) 1511 { 1512 struct nau8824 *nau8824 = snd_soc_component_get_drvdata(component); 1513 1514 if (nau8824->irq) { 1515 disable_irq(nau8824->irq); 1516 snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF); 1517 } 1518 regcache_cache_only(nau8824->regmap, true); 1519 regcache_mark_dirty(nau8824->regmap); 1520 1521 return 0; 1522 } 1523 1524 static int __maybe_unused nau8824_resume(struct snd_soc_component *component) 1525 { 1526 struct nau8824 *nau8824 = snd_soc_component_get_drvdata(component); 1527 1528 regcache_cache_only(nau8824->regmap, false); 1529 regcache_sync(nau8824->regmap); 1530 if (nau8824->irq) { 1531 /* Hold semaphore to postpone playback happening 1532 * until jack detection done. 1533 */ 1534 nau8824_sema_acquire(nau8824, 0); 1535 enable_irq(nau8824->irq); 1536 } 1537 1538 return 0; 1539 } 1540 1541 static const struct snd_soc_component_driver nau8824_component_driver = { 1542 .probe = nau8824_component_probe, 1543 .set_sysclk = nau8824_set_sysclk, 1544 .set_pll = nau8824_set_pll, 1545 .set_bias_level = nau8824_set_bias_level, 1546 .suspend = nau8824_suspend, 1547 .resume = nau8824_resume, 1548 .controls = nau8824_snd_controls, 1549 .num_controls = ARRAY_SIZE(nau8824_snd_controls), 1550 .dapm_widgets = nau8824_dapm_widgets, 1551 .num_dapm_widgets = ARRAY_SIZE(nau8824_dapm_widgets), 1552 .dapm_routes = nau8824_dapm_routes, 1553 .num_dapm_routes = ARRAY_SIZE(nau8824_dapm_routes), 1554 .suspend_bias_off = 1, 1555 .idle_bias_on = 1, 1556 .use_pmdown_time = 1, 1557 .endianness = 1, 1558 }; 1559 1560 static const struct snd_soc_dai_ops nau8824_dai_ops = { 1561 .startup = nau8824_dai_startup, 1562 .hw_params = nau8824_hw_params, 1563 .set_fmt = nau8824_set_fmt, 1564 .set_tdm_slot = nau8824_set_tdm_slot, 1565 }; 1566 1567 #define NAU8824_RATES SNDRV_PCM_RATE_8000_192000 1568 #define NAU8824_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE \ 1569 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE) 1570 1571 static struct snd_soc_dai_driver nau8824_dai = { 1572 .name = NAU8824_CODEC_DAI, 1573 .playback = { 1574 .stream_name = "Playback", 1575 .channels_min = 1, 1576 .channels_max = 2, 1577 .rates = NAU8824_RATES, 1578 .formats = NAU8824_FORMATS, 1579 }, 1580 .capture = { 1581 .stream_name = "Capture", 1582 .channels_min = 1, 1583 .channels_max = 2, 1584 .rates = NAU8824_RATES, 1585 .formats = NAU8824_FORMATS, 1586 }, 1587 .ops = &nau8824_dai_ops, 1588 }; 1589 1590 static const struct regmap_config nau8824_regmap_config = { 1591 .val_bits = NAU8824_REG_ADDR_LEN, 1592 .reg_bits = NAU8824_REG_DATA_LEN, 1593 1594 .max_register = NAU8824_REG_MAX, 1595 .readable_reg = nau8824_readable_reg, 1596 .writeable_reg = nau8824_writeable_reg, 1597 .volatile_reg = nau8824_volatile_reg, 1598 1599 .cache_type = REGCACHE_RBTREE, 1600 .reg_defaults = nau8824_reg_defaults, 1601 .num_reg_defaults = ARRAY_SIZE(nau8824_reg_defaults), 1602 }; 1603 1604 /** 1605 * nau8824_enable_jack_detect - Specify a jack for event reporting 1606 * 1607 * @component: component to register the jack with 1608 * @jack: jack to use to report headset and button events on 1609 * 1610 * After this function has been called the headset insert/remove and button 1611 * events will be routed to the given jack. Jack can be null to stop 1612 * reporting. 1613 */ 1614 int nau8824_enable_jack_detect(struct snd_soc_component *component, 1615 struct snd_soc_jack *jack) 1616 { 1617 struct nau8824 *nau8824 = snd_soc_component_get_drvdata(component); 1618 int ret; 1619 1620 nau8824->jack = jack; 1621 /* Initiate jack detection work queue */ 1622 INIT_WORK(&nau8824->jdet_work, nau8824_jdet_work); 1623 ret = devm_request_threaded_irq(nau8824->dev, nau8824->irq, NULL, 1624 nau8824_interrupt, IRQF_TRIGGER_LOW | IRQF_ONESHOT, 1625 "nau8824", nau8824); 1626 if (ret) { 1627 dev_err(nau8824->dev, "Cannot request irq %d (%d)\n", 1628 nau8824->irq, ret); 1629 } 1630 1631 return ret; 1632 } 1633 EXPORT_SYMBOL_GPL(nau8824_enable_jack_detect); 1634 1635 static void nau8824_reset_chip(struct regmap *regmap) 1636 { 1637 regmap_write(regmap, NAU8824_REG_RESET, 0x00); 1638 regmap_write(regmap, NAU8824_REG_RESET, 0x00); 1639 } 1640 1641 static void nau8824_setup_buttons(struct nau8824 *nau8824) 1642 { 1643 struct regmap *regmap = nau8824->regmap; 1644 1645 regmap_update_bits(regmap, NAU8824_REG_SAR_ADC, 1646 NAU8824_SAR_TRACKING_GAIN_MASK, 1647 nau8824->sar_voltage << NAU8824_SAR_TRACKING_GAIN_SFT); 1648 regmap_update_bits(regmap, NAU8824_REG_SAR_ADC, 1649 NAU8824_SAR_COMPARE_TIME_MASK, 1650 nau8824->sar_compare_time << NAU8824_SAR_COMPARE_TIME_SFT); 1651 regmap_update_bits(regmap, NAU8824_REG_SAR_ADC, 1652 NAU8824_SAR_SAMPLING_TIME_MASK, 1653 nau8824->sar_sampling_time << NAU8824_SAR_SAMPLING_TIME_SFT); 1654 1655 regmap_update_bits(regmap, NAU8824_REG_VDET_COEFFICIENT, 1656 NAU8824_LEVELS_NR_MASK, 1657 (nau8824->sar_threshold_num - 1) << NAU8824_LEVELS_NR_SFT); 1658 regmap_update_bits(regmap, NAU8824_REG_VDET_COEFFICIENT, 1659 NAU8824_HYSTERESIS_MASK, 1660 nau8824->sar_hysteresis << NAU8824_HYSTERESIS_SFT); 1661 regmap_update_bits(regmap, NAU8824_REG_VDET_COEFFICIENT, 1662 NAU8824_SHORTKEY_DEBOUNCE_MASK, 1663 nau8824->key_debounce << NAU8824_SHORTKEY_DEBOUNCE_SFT); 1664 1665 regmap_write(regmap, NAU8824_REG_VDET_THRESHOLD_1, 1666 (nau8824->sar_threshold[0] << 8) | nau8824->sar_threshold[1]); 1667 regmap_write(regmap, NAU8824_REG_VDET_THRESHOLD_2, 1668 (nau8824->sar_threshold[2] << 8) | nau8824->sar_threshold[3]); 1669 regmap_write(regmap, NAU8824_REG_VDET_THRESHOLD_3, 1670 (nau8824->sar_threshold[4] << 8) | nau8824->sar_threshold[5]); 1671 regmap_write(regmap, NAU8824_REG_VDET_THRESHOLD_4, 1672 (nau8824->sar_threshold[6] << 8) | nau8824->sar_threshold[7]); 1673 } 1674 1675 static void nau8824_init_regs(struct nau8824 *nau8824) 1676 { 1677 struct regmap *regmap = nau8824->regmap; 1678 1679 /* Enable Bias/VMID/VMID Tieoff */ 1680 regmap_update_bits(regmap, NAU8824_REG_BIAS_ADJ, 1681 NAU8824_VMID | NAU8824_VMID_SEL_MASK, NAU8824_VMID | 1682 (nau8824->vref_impedance << NAU8824_VMID_SEL_SFT)); 1683 regmap_update_bits(regmap, NAU8824_REG_BOOST, 1684 NAU8824_GLOBAL_BIAS_EN, NAU8824_GLOBAL_BIAS_EN); 1685 mdelay(2); 1686 regmap_update_bits(regmap, NAU8824_REG_MIC_BIAS, 1687 NAU8824_MICBIAS_VOLTAGE_MASK, nau8824->micbias_voltage); 1688 /* Disable Boost Driver, Automatic Short circuit protection enable */ 1689 regmap_update_bits(regmap, NAU8824_REG_BOOST, 1690 NAU8824_PRECHARGE_DIS | NAU8824_HP_BOOST_DIS | 1691 NAU8824_HP_BOOST_G_DIS | NAU8824_SHORT_SHUTDOWN_EN, 1692 NAU8824_PRECHARGE_DIS | NAU8824_HP_BOOST_DIS | 1693 NAU8824_HP_BOOST_G_DIS | NAU8824_SHORT_SHUTDOWN_EN); 1694 /* Scaling for ADC and DAC clock */ 1695 regmap_update_bits(regmap, NAU8824_REG_CLK_DIVIDER, 1696 NAU8824_CLK_ADC_SRC_MASK | NAU8824_CLK_DAC_SRC_MASK, 1697 (0x1 << NAU8824_CLK_ADC_SRC_SFT) | 1698 (0x1 << NAU8824_CLK_DAC_SRC_SFT)); 1699 regmap_update_bits(regmap, NAU8824_REG_DAC_MUTE_CTRL, 1700 NAU8824_DAC_ZC_EN, NAU8824_DAC_ZC_EN); 1701 regmap_update_bits(regmap, NAU8824_REG_ENA_CTRL, 1702 NAU8824_DAC_CH1_EN | NAU8824_DAC_CH0_EN | 1703 NAU8824_ADC_CH0_EN | NAU8824_ADC_CH1_EN | 1704 NAU8824_ADC_CH2_EN | NAU8824_ADC_CH3_EN, 1705 NAU8824_DAC_CH1_EN | NAU8824_DAC_CH0_EN | 1706 NAU8824_ADC_CH0_EN | NAU8824_ADC_CH1_EN | 1707 NAU8824_ADC_CH2_EN | NAU8824_ADC_CH3_EN); 1708 regmap_update_bits(regmap, NAU8824_REG_CLK_GATING_ENA, 1709 NAU8824_CLK_ADC_CH23_EN | NAU8824_CLK_ADC_CH01_EN | 1710 NAU8824_CLK_DAC_CH1_EN | NAU8824_CLK_DAC_CH0_EN | 1711 NAU8824_CLK_I2S_EN | NAU8824_CLK_GAIN_EN | 1712 NAU8824_CLK_SAR_EN | NAU8824_CLK_DMIC_CH23_EN, 1713 NAU8824_CLK_ADC_CH23_EN | NAU8824_CLK_ADC_CH01_EN | 1714 NAU8824_CLK_DAC_CH1_EN | NAU8824_CLK_DAC_CH0_EN | 1715 NAU8824_CLK_I2S_EN | NAU8824_CLK_GAIN_EN | 1716 NAU8824_CLK_SAR_EN | NAU8824_CLK_DMIC_CH23_EN); 1717 /* Class G timer 64ms */ 1718 regmap_update_bits(regmap, NAU8824_REG_CLASSG, 1719 NAU8824_CLASSG_TIMER_MASK, 1720 0x20 << NAU8824_CLASSG_TIMER_SFT); 1721 regmap_update_bits(regmap, NAU8824_REG_TRIM_SETTINGS, 1722 NAU8824_DRV_CURR_INC, NAU8824_DRV_CURR_INC); 1723 /* Disable DACR/L power */ 1724 regmap_update_bits(regmap, NAU8824_REG_CHARGE_PUMP_CONTROL, 1725 NAU8824_SPKR_PULL_DOWN | NAU8824_SPKL_PULL_DOWN | 1726 NAU8824_POWER_DOWN_DACR | NAU8824_POWER_DOWN_DACL, 1727 NAU8824_SPKR_PULL_DOWN | NAU8824_SPKL_PULL_DOWN | 1728 NAU8824_POWER_DOWN_DACR | NAU8824_POWER_DOWN_DACL); 1729 /* Enable TESTDAC. This sets the analog DAC inputs to a '0' input 1730 * signal to avoid any glitches due to power up transients in both 1731 * the analog and digital DAC circuit. 1732 */ 1733 regmap_update_bits(regmap, NAU8824_REG_ENABLE_LO, 1734 NAU8824_TEST_DAC_EN, NAU8824_TEST_DAC_EN); 1735 /* Config L/R channel */ 1736 regmap_update_bits(regmap, NAU8824_REG_DAC_CH0_DGAIN_CTRL, 1737 NAU8824_DAC_CH0_SEL_MASK, NAU8824_DAC_CH0_SEL_I2S0); 1738 regmap_update_bits(regmap, NAU8824_REG_DAC_CH1_DGAIN_CTRL, 1739 NAU8824_DAC_CH1_SEL_MASK, NAU8824_DAC_CH1_SEL_I2S1); 1740 regmap_update_bits(regmap, NAU8824_REG_ENABLE_LO, 1741 NAU8824_DACR_HPR_EN | NAU8824_DACL_HPL_EN, 1742 NAU8824_DACR_HPR_EN | NAU8824_DACL_HPL_EN); 1743 /* Default oversampling/decimations settings are unusable 1744 * (audible hiss). Set it to something better. 1745 */ 1746 regmap_update_bits(regmap, NAU8824_REG_ADC_FILTER_CTRL, 1747 NAU8824_ADC_SYNC_DOWN_MASK, NAU8824_ADC_SYNC_DOWN_64); 1748 regmap_update_bits(regmap, NAU8824_REG_DAC_FILTER_CTRL_1, 1749 NAU8824_DAC_CICCLP_OFF | NAU8824_DAC_OVERSAMPLE_MASK, 1750 NAU8824_DAC_CICCLP_OFF | NAU8824_DAC_OVERSAMPLE_64); 1751 /* DAC clock delay 2ns, VREF */ 1752 regmap_update_bits(regmap, NAU8824_REG_RDAC, 1753 NAU8824_RDAC_CLK_DELAY_MASK | NAU8824_RDAC_VREF_MASK, 1754 (0x2 << NAU8824_RDAC_CLK_DELAY_SFT) | 1755 (0x3 << NAU8824_RDAC_VREF_SFT)); 1756 /* PGA input mode selection */ 1757 regmap_update_bits(regmap, NAU8824_REG_FEPGA, 1758 NAU8824_FEPGA_MODEL_SHORT_EN | NAU8824_FEPGA_MODER_SHORT_EN, 1759 NAU8824_FEPGA_MODEL_SHORT_EN | NAU8824_FEPGA_MODER_SHORT_EN); 1760 /* Digital microphone control */ 1761 regmap_update_bits(regmap, NAU8824_REG_ANALOG_CONTROL_1, 1762 NAU8824_DMIC_CLK_DRV_STRG | NAU8824_DMIC_CLK_SLEW_FAST, 1763 NAU8824_DMIC_CLK_DRV_STRG | NAU8824_DMIC_CLK_SLEW_FAST); 1764 regmap_update_bits(regmap, NAU8824_REG_JACK_DET_CTRL, 1765 NAU8824_JACK_LOGIC, 1766 /* jkdet_polarity - 1 is for active-low */ 1767 nau8824->jkdet_polarity ? 0 : NAU8824_JACK_LOGIC); 1768 regmap_update_bits(regmap, 1769 NAU8824_REG_JACK_DET_CTRL, NAU8824_JACK_EJECT_DT_MASK, 1770 (nau8824->jack_eject_debounce << NAU8824_JACK_EJECT_DT_SFT)); 1771 if (nau8824->sar_threshold_num) 1772 nau8824_setup_buttons(nau8824); 1773 } 1774 1775 static int nau8824_setup_irq(struct nau8824 *nau8824) 1776 { 1777 /* Disable interruption before codec initiation done */ 1778 regmap_update_bits(nau8824->regmap, NAU8824_REG_ENA_CTRL, 1779 NAU8824_JD_SLEEP_MODE, NAU8824_JD_SLEEP_MODE); 1780 regmap_update_bits(nau8824->regmap, 1781 NAU8824_REG_INTERRUPT_SETTING, 0x3ff, 0x3ff); 1782 regmap_update_bits(nau8824->regmap, NAU8824_REG_INTERRUPT_SETTING_1, 1783 NAU8824_IRQ_EJECT_EN | NAU8824_IRQ_INSERT_EN, 0); 1784 1785 return 0; 1786 } 1787 1788 static void nau8824_print_device_properties(struct nau8824 *nau8824) 1789 { 1790 struct device *dev = nau8824->dev; 1791 int i; 1792 1793 dev_dbg(dev, "jkdet-polarity: %d\n", nau8824->jkdet_polarity); 1794 dev_dbg(dev, "micbias-voltage: %d\n", nau8824->micbias_voltage); 1795 dev_dbg(dev, "vref-impedance: %d\n", nau8824->vref_impedance); 1796 1797 dev_dbg(dev, "sar-threshold-num: %d\n", nau8824->sar_threshold_num); 1798 for (i = 0; i < nau8824->sar_threshold_num; i++) 1799 dev_dbg(dev, "sar-threshold[%d]=%x\n", i, 1800 nau8824->sar_threshold[i]); 1801 1802 dev_dbg(dev, "sar-hysteresis: %d\n", nau8824->sar_hysteresis); 1803 dev_dbg(dev, "sar-voltage: %d\n", nau8824->sar_voltage); 1804 dev_dbg(dev, "sar-compare-time: %d\n", nau8824->sar_compare_time); 1805 dev_dbg(dev, "sar-sampling-time: %d\n", nau8824->sar_sampling_time); 1806 dev_dbg(dev, "short-key-debounce: %d\n", nau8824->key_debounce); 1807 dev_dbg(dev, "jack-eject-debounce: %d\n", 1808 nau8824->jack_eject_debounce); 1809 } 1810 1811 static int nau8824_read_device_properties(struct device *dev, 1812 struct nau8824 *nau8824) { 1813 int ret; 1814 1815 ret = device_property_read_u32(dev, "nuvoton,jkdet-polarity", 1816 &nau8824->jkdet_polarity); 1817 if (ret) 1818 nau8824->jkdet_polarity = 1; 1819 ret = device_property_read_u32(dev, "nuvoton,micbias-voltage", 1820 &nau8824->micbias_voltage); 1821 if (ret) 1822 nau8824->micbias_voltage = 6; 1823 ret = device_property_read_u32(dev, "nuvoton,vref-impedance", 1824 &nau8824->vref_impedance); 1825 if (ret) 1826 nau8824->vref_impedance = 2; 1827 ret = device_property_read_u32(dev, "nuvoton,sar-threshold-num", 1828 &nau8824->sar_threshold_num); 1829 if (ret) 1830 nau8824->sar_threshold_num = 4; 1831 ret = device_property_read_u32_array(dev, "nuvoton,sar-threshold", 1832 nau8824->sar_threshold, nau8824->sar_threshold_num); 1833 if (ret) { 1834 nau8824->sar_threshold[0] = 0x0a; 1835 nau8824->sar_threshold[1] = 0x14; 1836 nau8824->sar_threshold[2] = 0x26; 1837 nau8824->sar_threshold[3] = 0x73; 1838 } 1839 ret = device_property_read_u32(dev, "nuvoton,sar-hysteresis", 1840 &nau8824->sar_hysteresis); 1841 if (ret) 1842 nau8824->sar_hysteresis = 0; 1843 ret = device_property_read_u32(dev, "nuvoton,sar-voltage", 1844 &nau8824->sar_voltage); 1845 if (ret) 1846 nau8824->sar_voltage = 6; 1847 ret = device_property_read_u32(dev, "nuvoton,sar-compare-time", 1848 &nau8824->sar_compare_time); 1849 if (ret) 1850 nau8824->sar_compare_time = 1; 1851 ret = device_property_read_u32(dev, "nuvoton,sar-sampling-time", 1852 &nau8824->sar_sampling_time); 1853 if (ret) 1854 nau8824->sar_sampling_time = 1; 1855 ret = device_property_read_u32(dev, "nuvoton,short-key-debounce", 1856 &nau8824->key_debounce); 1857 if (ret) 1858 nau8824->key_debounce = 0; 1859 ret = device_property_read_u32(dev, "nuvoton,jack-eject-debounce", 1860 &nau8824->jack_eject_debounce); 1861 if (ret) 1862 nau8824->jack_eject_debounce = 1; 1863 1864 return 0; 1865 } 1866 1867 /* Please keep this list alphabetically sorted */ 1868 static const struct dmi_system_id nau8824_quirk_table[] = { 1869 { 1870 /* Cyberbook T116 rugged tablet */ 1871 .matches = { 1872 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Default string"), 1873 DMI_EXACT_MATCH(DMI_BOARD_NAME, "Cherry Trail CR"), 1874 DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "20170531"), 1875 }, 1876 .driver_data = (void *)(NAU8824_JD_ACTIVE_HIGH | 1877 NAU8824_MONO_SPEAKER), 1878 }, 1879 { 1880 /* CUBE iwork8 Air */ 1881 .matches = { 1882 DMI_MATCH(DMI_SYS_VENDOR, "cube"), 1883 DMI_MATCH(DMI_PRODUCT_NAME, "i1-TF"), 1884 DMI_MATCH(DMI_BOARD_NAME, "Cherry Trail CR"), 1885 }, 1886 .driver_data = (void *)(NAU8824_MONO_SPEAKER), 1887 }, 1888 { 1889 /* Pipo W2S */ 1890 .matches = { 1891 DMI_MATCH(DMI_SYS_VENDOR, "PIPO"), 1892 DMI_MATCH(DMI_PRODUCT_NAME, "W2S"), 1893 }, 1894 .driver_data = (void *)(NAU8824_MONO_SPEAKER), 1895 }, 1896 {} 1897 }; 1898 1899 static void nau8824_check_quirks(void) 1900 { 1901 const struct dmi_system_id *dmi_id; 1902 1903 if (quirk_override != -1) { 1904 nau8824_quirk = quirk_override; 1905 return; 1906 } 1907 1908 dmi_id = dmi_first_match(nau8824_quirk_table); 1909 if (dmi_id) 1910 nau8824_quirk = (unsigned long)dmi_id->driver_data; 1911 } 1912 1913 const char *nau8824_components(void) 1914 { 1915 nau8824_check_quirks(); 1916 1917 if (nau8824_quirk & NAU8824_MONO_SPEAKER) 1918 return "cfg-spk:1"; 1919 else 1920 return "cfg-spk:2"; 1921 } 1922 EXPORT_SYMBOL_GPL(nau8824_components); 1923 1924 static int nau8824_i2c_probe(struct i2c_client *i2c) 1925 { 1926 struct device *dev = &i2c->dev; 1927 struct nau8824 *nau8824 = dev_get_platdata(dev); 1928 int ret, value; 1929 1930 if (!nau8824) { 1931 nau8824 = devm_kzalloc(dev, sizeof(*nau8824), GFP_KERNEL); 1932 if (!nau8824) 1933 return -ENOMEM; 1934 ret = nau8824_read_device_properties(dev, nau8824); 1935 if (ret) 1936 return ret; 1937 } 1938 i2c_set_clientdata(i2c, nau8824); 1939 1940 nau8824->regmap = devm_regmap_init_i2c(i2c, &nau8824_regmap_config); 1941 if (IS_ERR(nau8824->regmap)) 1942 return PTR_ERR(nau8824->regmap); 1943 nau8824->dev = dev; 1944 nau8824->irq = i2c->irq; 1945 sema_init(&nau8824->jd_sem, 1); 1946 1947 nau8824_check_quirks(); 1948 1949 if (nau8824_quirk & NAU8824_JD_ACTIVE_HIGH) 1950 nau8824->jkdet_polarity = 0; 1951 1952 nau8824_print_device_properties(nau8824); 1953 1954 ret = regmap_read(nau8824->regmap, NAU8824_REG_I2C_DEVICE_ID, &value); 1955 if (ret < 0) { 1956 dev_err(dev, "Failed to read device id from the NAU8824: %d\n", 1957 ret); 1958 return ret; 1959 } 1960 nau8824_reset_chip(nau8824->regmap); 1961 nau8824_init_regs(nau8824); 1962 1963 if (i2c->irq) 1964 nau8824_setup_irq(nau8824); 1965 1966 return devm_snd_soc_register_component(dev, 1967 &nau8824_component_driver, &nau8824_dai, 1); 1968 } 1969 1970 static const struct i2c_device_id nau8824_i2c_ids[] = { 1971 { "nau8824", 0 }, 1972 { } 1973 }; 1974 MODULE_DEVICE_TABLE(i2c, nau8824_i2c_ids); 1975 1976 #ifdef CONFIG_OF 1977 static const struct of_device_id nau8824_of_ids[] = { 1978 { .compatible = "nuvoton,nau8824", }, 1979 {} 1980 }; 1981 MODULE_DEVICE_TABLE(of, nau8824_of_ids); 1982 #endif 1983 1984 #ifdef CONFIG_ACPI 1985 static const struct acpi_device_id nau8824_acpi_match[] = { 1986 { "10508824", 0 }, 1987 {}, 1988 }; 1989 MODULE_DEVICE_TABLE(acpi, nau8824_acpi_match); 1990 #endif 1991 1992 static struct i2c_driver nau8824_i2c_driver = { 1993 .driver = { 1994 .name = "nau8824", 1995 .of_match_table = of_match_ptr(nau8824_of_ids), 1996 .acpi_match_table = ACPI_PTR(nau8824_acpi_match), 1997 }, 1998 .probe_new = nau8824_i2c_probe, 1999 .id_table = nau8824_i2c_ids, 2000 }; 2001 module_i2c_driver(nau8824_i2c_driver); 2002 2003 2004 MODULE_DESCRIPTION("ASoC NAU88L24 driver"); 2005 MODULE_AUTHOR("John Hsu <KCHSU0@nuvoton.com>"); 2006 MODULE_LICENSE("GPL v2"); 2007