1 /* 2 * da7219.c - DA7219 ALSA SoC Codec Driver 3 * 4 * Copyright (c) 2015 Dialog Semiconductor 5 * 6 * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com> 7 * 8 * This program is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License as published by the 10 * Free Software Foundation; either version 2 of the License, or (at your 11 * option) any later version. 12 */ 13 14 #include <linux/acpi.h> 15 #include <linux/clk.h> 16 #include <linux/clkdev.h> 17 #include <linux/clk-provider.h> 18 #include <linux/i2c.h> 19 #include <linux/of_device.h> 20 #include <linux/property.h> 21 #include <linux/regmap.h> 22 #include <linux/slab.h> 23 #include <linux/pm.h> 24 #include <linux/module.h> 25 #include <linux/delay.h> 26 #include <linux/regulator/consumer.h> 27 #include <sound/pcm.h> 28 #include <sound/pcm_params.h> 29 #include <sound/soc.h> 30 #include <sound/soc-dapm.h> 31 #include <sound/initval.h> 32 #include <sound/tlv.h> 33 #include <asm/div64.h> 34 35 #include <sound/da7219.h> 36 #include "da7219.h" 37 #include "da7219-aad.h" 38 39 40 /* 41 * TLVs and Enums 42 */ 43 44 /* Input TLVs */ 45 static const DECLARE_TLV_DB_SCALE(da7219_mic_gain_tlv, -600, 600, 0); 46 static const DECLARE_TLV_DB_SCALE(da7219_mixin_gain_tlv, -450, 150, 0); 47 static const DECLARE_TLV_DB_SCALE(da7219_adc_dig_gain_tlv, -8325, 75, 0); 48 static const DECLARE_TLV_DB_SCALE(da7219_alc_threshold_tlv, -9450, 150, 0); 49 static const DECLARE_TLV_DB_SCALE(da7219_alc_gain_tlv, 0, 600, 0); 50 static const DECLARE_TLV_DB_SCALE(da7219_alc_ana_gain_tlv, 0, 600, 0); 51 static const DECLARE_TLV_DB_SCALE(da7219_sidetone_gain_tlv, -4200, 300, 0); 52 static const DECLARE_TLV_DB_SCALE(da7219_tonegen_gain_tlv, -4500, 300, 0); 53 54 /* Output TLVs */ 55 static const DECLARE_TLV_DB_SCALE(da7219_dac_eq_band_tlv, -1050, 150, 0); 56 57 static const DECLARE_TLV_DB_RANGE(da7219_dac_dig_gain_tlv, 58 0x0, 0x07, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1), 59 /* -77.25dB to 12dB */ 60 0x08, 0x7f, TLV_DB_SCALE_ITEM(-7725, 75, 0) 61 ); 62 63 static const DECLARE_TLV_DB_SCALE(da7219_dac_ng_threshold_tlv, -10200, 600, 0); 64 static const DECLARE_TLV_DB_SCALE(da7219_hp_gain_tlv, -5700, 100, 0); 65 66 /* Input Enums */ 67 static const char * const da7219_alc_attack_rate_txt[] = { 68 "7.33/fs", "14.66/fs", "29.32/fs", "58.64/fs", "117.3/fs", "234.6/fs", 69 "469.1/fs", "938.2/fs", "1876/fs", "3753/fs", "7506/fs", "15012/fs", 70 "30024/fs" 71 }; 72 73 static const struct soc_enum da7219_alc_attack_rate = 74 SOC_ENUM_SINGLE(DA7219_ALC_CTRL2, DA7219_ALC_ATTACK_SHIFT, 75 DA7219_ALC_ATTACK_MAX, da7219_alc_attack_rate_txt); 76 77 static const char * const da7219_alc_release_rate_txt[] = { 78 "28.66/fs", "57.33/fs", "114.6/fs", "229.3/fs", "458.6/fs", "917.1/fs", 79 "1834/fs", "3668/fs", "7337/fs", "14674/fs", "29348/fs" 80 }; 81 82 static const struct soc_enum da7219_alc_release_rate = 83 SOC_ENUM_SINGLE(DA7219_ALC_CTRL2, DA7219_ALC_RELEASE_SHIFT, 84 DA7219_ALC_RELEASE_MAX, da7219_alc_release_rate_txt); 85 86 static const char * const da7219_alc_hold_time_txt[] = { 87 "62/fs", "124/fs", "248/fs", "496/fs", "992/fs", "1984/fs", "3968/fs", 88 "7936/fs", "15872/fs", "31744/fs", "63488/fs", "126976/fs", 89 "253952/fs", "507904/fs", "1015808/fs", "2031616/fs" 90 }; 91 92 static const struct soc_enum da7219_alc_hold_time = 93 SOC_ENUM_SINGLE(DA7219_ALC_CTRL3, DA7219_ALC_HOLD_SHIFT, 94 DA7219_ALC_HOLD_MAX, da7219_alc_hold_time_txt); 95 96 static const char * const da7219_alc_env_rate_txt[] = { 97 "1/4", "1/16", "1/256", "1/65536" 98 }; 99 100 static const struct soc_enum da7219_alc_env_attack_rate = 101 SOC_ENUM_SINGLE(DA7219_ALC_CTRL3, DA7219_ALC_INTEG_ATTACK_SHIFT, 102 DA7219_ALC_INTEG_MAX, da7219_alc_env_rate_txt); 103 104 static const struct soc_enum da7219_alc_env_release_rate = 105 SOC_ENUM_SINGLE(DA7219_ALC_CTRL3, DA7219_ALC_INTEG_RELEASE_SHIFT, 106 DA7219_ALC_INTEG_MAX, da7219_alc_env_rate_txt); 107 108 static const char * const da7219_alc_anticlip_step_txt[] = { 109 "0.034dB/fs", "0.068dB/fs", "0.136dB/fs", "0.272dB/fs" 110 }; 111 112 static const struct soc_enum da7219_alc_anticlip_step = 113 SOC_ENUM_SINGLE(DA7219_ALC_ANTICLIP_CTRL, 114 DA7219_ALC_ANTICLIP_STEP_SHIFT, 115 DA7219_ALC_ANTICLIP_STEP_MAX, 116 da7219_alc_anticlip_step_txt); 117 118 /* Input/Output Enums */ 119 static const char * const da7219_gain_ramp_rate_txt[] = { 120 "Nominal Rate * 8", "Nominal Rate", "Nominal Rate / 8", 121 "Nominal Rate / 16" 122 }; 123 124 static const struct soc_enum da7219_gain_ramp_rate = 125 SOC_ENUM_SINGLE(DA7219_GAIN_RAMP_CTRL, DA7219_GAIN_RAMP_RATE_SHIFT, 126 DA7219_GAIN_RAMP_RATE_MAX, da7219_gain_ramp_rate_txt); 127 128 static const char * const da7219_hpf_mode_txt[] = { 129 "Disabled", "Audio", "Voice" 130 }; 131 132 static const unsigned int da7219_hpf_mode_val[] = { 133 DA7219_HPF_DISABLED, DA7219_HPF_AUDIO_EN, DA7219_HPF_VOICE_EN, 134 }; 135 136 static const struct soc_enum da7219_adc_hpf_mode = 137 SOC_VALUE_ENUM_SINGLE(DA7219_ADC_FILTERS1, DA7219_HPF_MODE_SHIFT, 138 DA7219_HPF_MODE_MASK, DA7219_HPF_MODE_MAX, 139 da7219_hpf_mode_txt, da7219_hpf_mode_val); 140 141 static const struct soc_enum da7219_dac_hpf_mode = 142 SOC_VALUE_ENUM_SINGLE(DA7219_DAC_FILTERS1, DA7219_HPF_MODE_SHIFT, 143 DA7219_HPF_MODE_MASK, DA7219_HPF_MODE_MAX, 144 da7219_hpf_mode_txt, da7219_hpf_mode_val); 145 146 static const char * const da7219_audio_hpf_corner_txt[] = { 147 "2Hz", "4Hz", "8Hz", "16Hz" 148 }; 149 150 static const struct soc_enum da7219_adc_audio_hpf_corner = 151 SOC_ENUM_SINGLE(DA7219_ADC_FILTERS1, 152 DA7219_ADC_AUDIO_HPF_CORNER_SHIFT, 153 DA7219_AUDIO_HPF_CORNER_MAX, 154 da7219_audio_hpf_corner_txt); 155 156 static const struct soc_enum da7219_dac_audio_hpf_corner = 157 SOC_ENUM_SINGLE(DA7219_DAC_FILTERS1, 158 DA7219_DAC_AUDIO_HPF_CORNER_SHIFT, 159 DA7219_AUDIO_HPF_CORNER_MAX, 160 da7219_audio_hpf_corner_txt); 161 162 static const char * const da7219_voice_hpf_corner_txt[] = { 163 "2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz" 164 }; 165 166 static const struct soc_enum da7219_adc_voice_hpf_corner = 167 SOC_ENUM_SINGLE(DA7219_ADC_FILTERS1, 168 DA7219_ADC_VOICE_HPF_CORNER_SHIFT, 169 DA7219_VOICE_HPF_CORNER_MAX, 170 da7219_voice_hpf_corner_txt); 171 172 static const struct soc_enum da7219_dac_voice_hpf_corner = 173 SOC_ENUM_SINGLE(DA7219_DAC_FILTERS1, 174 DA7219_DAC_VOICE_HPF_CORNER_SHIFT, 175 DA7219_VOICE_HPF_CORNER_MAX, 176 da7219_voice_hpf_corner_txt); 177 178 static const char * const da7219_tonegen_dtmf_key_txt[] = { 179 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", 180 "*", "#" 181 }; 182 183 static const struct soc_enum da7219_tonegen_dtmf_key = 184 SOC_ENUM_SINGLE(DA7219_TONE_GEN_CFG1, DA7219_DTMF_REG_SHIFT, 185 DA7219_DTMF_REG_MAX, da7219_tonegen_dtmf_key_txt); 186 187 static const char * const da7219_tonegen_swg_sel_txt[] = { 188 "Sum", "SWG1", "SWG2", "SWG1_1-Cos" 189 }; 190 191 static const struct soc_enum da7219_tonegen_swg_sel = 192 SOC_ENUM_SINGLE(DA7219_TONE_GEN_CFG2, DA7219_SWG_SEL_SHIFT, 193 DA7219_SWG_SEL_MAX, da7219_tonegen_swg_sel_txt); 194 195 /* Output Enums */ 196 static const char * const da7219_dac_softmute_rate_txt[] = { 197 "1 Sample", "2 Samples", "4 Samples", "8 Samples", "16 Samples", 198 "32 Samples", "64 Samples" 199 }; 200 201 static const struct soc_enum da7219_dac_softmute_rate = 202 SOC_ENUM_SINGLE(DA7219_DAC_FILTERS5, DA7219_DAC_SOFTMUTE_RATE_SHIFT, 203 DA7219_DAC_SOFTMUTE_RATE_MAX, 204 da7219_dac_softmute_rate_txt); 205 206 static const char * const da7219_dac_ng_setup_time_txt[] = { 207 "256 Samples", "512 Samples", "1024 Samples", "2048 Samples" 208 }; 209 210 static const struct soc_enum da7219_dac_ng_setup_time = 211 SOC_ENUM_SINGLE(DA7219_DAC_NG_SETUP_TIME, 212 DA7219_DAC_NG_SETUP_TIME_SHIFT, 213 DA7219_DAC_NG_SETUP_TIME_MAX, 214 da7219_dac_ng_setup_time_txt); 215 216 static const char * const da7219_dac_ng_rampup_txt[] = { 217 "0.22ms/dB", "0.0138ms/dB" 218 }; 219 220 static const struct soc_enum da7219_dac_ng_rampup_rate = 221 SOC_ENUM_SINGLE(DA7219_DAC_NG_SETUP_TIME, 222 DA7219_DAC_NG_RAMPUP_RATE_SHIFT, 223 DA7219_DAC_NG_RAMP_RATE_MAX, 224 da7219_dac_ng_rampup_txt); 225 226 static const char * const da7219_dac_ng_rampdown_txt[] = { 227 "0.88ms/dB", "14.08ms/dB" 228 }; 229 230 static const struct soc_enum da7219_dac_ng_rampdown_rate = 231 SOC_ENUM_SINGLE(DA7219_DAC_NG_SETUP_TIME, 232 DA7219_DAC_NG_RAMPDN_RATE_SHIFT, 233 DA7219_DAC_NG_RAMP_RATE_MAX, 234 da7219_dac_ng_rampdown_txt); 235 236 237 static const char * const da7219_cp_track_mode_txt[] = { 238 "Largest Volume", "DAC Volume", "Signal Magnitude" 239 }; 240 241 static const unsigned int da7219_cp_track_mode_val[] = { 242 DA7219_CP_MCHANGE_LARGEST_VOL, DA7219_CP_MCHANGE_DAC_VOL, 243 DA7219_CP_MCHANGE_SIG_MAG 244 }; 245 246 static const struct soc_enum da7219_cp_track_mode = 247 SOC_VALUE_ENUM_SINGLE(DA7219_CP_CTRL, DA7219_CP_MCHANGE_SHIFT, 248 DA7219_CP_MCHANGE_REL_MASK, DA7219_CP_MCHANGE_MAX, 249 da7219_cp_track_mode_txt, 250 da7219_cp_track_mode_val); 251 252 253 /* 254 * Control Functions 255 */ 256 257 /* Locked Kcontrol calls */ 258 static int da7219_volsw_locked_get(struct snd_kcontrol *kcontrol, 259 struct snd_ctl_elem_value *ucontrol) 260 { 261 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 262 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 263 int ret; 264 265 mutex_lock(&da7219->ctrl_lock); 266 ret = snd_soc_get_volsw(kcontrol, ucontrol); 267 mutex_unlock(&da7219->ctrl_lock); 268 269 return ret; 270 } 271 272 static int da7219_volsw_locked_put(struct snd_kcontrol *kcontrol, 273 struct snd_ctl_elem_value *ucontrol) 274 { 275 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 276 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 277 int ret; 278 279 mutex_lock(&da7219->ctrl_lock); 280 ret = snd_soc_put_volsw(kcontrol, ucontrol); 281 mutex_unlock(&da7219->ctrl_lock); 282 283 return ret; 284 } 285 286 static int da7219_enum_locked_get(struct snd_kcontrol *kcontrol, 287 struct snd_ctl_elem_value *ucontrol) 288 { 289 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 290 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 291 int ret; 292 293 mutex_lock(&da7219->ctrl_lock); 294 ret = snd_soc_get_enum_double(kcontrol, ucontrol); 295 mutex_unlock(&da7219->ctrl_lock); 296 297 return ret; 298 } 299 300 static int da7219_enum_locked_put(struct snd_kcontrol *kcontrol, 301 struct snd_ctl_elem_value *ucontrol) 302 { 303 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 304 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 305 int ret; 306 307 mutex_lock(&da7219->ctrl_lock); 308 ret = snd_soc_put_enum_double(kcontrol, ucontrol); 309 mutex_unlock(&da7219->ctrl_lock); 310 311 return ret; 312 } 313 314 /* ALC */ 315 static void da7219_alc_calib(struct snd_soc_component *component) 316 { 317 u8 mic_ctrl, mixin_ctrl, adc_ctrl, calib_ctrl; 318 319 /* Save current state of mic control register */ 320 mic_ctrl = snd_soc_component_read32(component, DA7219_MIC_1_CTRL); 321 322 /* Save current state of input mixer control register */ 323 mixin_ctrl = snd_soc_component_read32(component, DA7219_MIXIN_L_CTRL); 324 325 /* Save current state of input ADC control register */ 326 adc_ctrl = snd_soc_component_read32(component, DA7219_ADC_L_CTRL); 327 328 /* Enable then Mute MIC PGAs */ 329 snd_soc_component_update_bits(component, DA7219_MIC_1_CTRL, DA7219_MIC_1_AMP_EN_MASK, 330 DA7219_MIC_1_AMP_EN_MASK); 331 snd_soc_component_update_bits(component, DA7219_MIC_1_CTRL, 332 DA7219_MIC_1_AMP_MUTE_EN_MASK, 333 DA7219_MIC_1_AMP_MUTE_EN_MASK); 334 335 /* Enable input mixers unmuted */ 336 snd_soc_component_update_bits(component, DA7219_MIXIN_L_CTRL, 337 DA7219_MIXIN_L_AMP_EN_MASK | 338 DA7219_MIXIN_L_AMP_MUTE_EN_MASK, 339 DA7219_MIXIN_L_AMP_EN_MASK); 340 341 /* Enable input filters unmuted */ 342 snd_soc_component_update_bits(component, DA7219_ADC_L_CTRL, 343 DA7219_ADC_L_MUTE_EN_MASK | DA7219_ADC_L_EN_MASK, 344 DA7219_ADC_L_EN_MASK); 345 346 /* Perform auto calibration */ 347 snd_soc_component_update_bits(component, DA7219_ALC_CTRL1, 348 DA7219_ALC_AUTO_CALIB_EN_MASK, 349 DA7219_ALC_AUTO_CALIB_EN_MASK); 350 do { 351 calib_ctrl = snd_soc_component_read32(component, DA7219_ALC_CTRL1); 352 } while (calib_ctrl & DA7219_ALC_AUTO_CALIB_EN_MASK); 353 354 /* If auto calibration fails, disable DC offset, hybrid ALC */ 355 if (calib_ctrl & DA7219_ALC_CALIB_OVERFLOW_MASK) { 356 dev_warn(component->dev, 357 "ALC auto calibration failed with overflow\n"); 358 snd_soc_component_update_bits(component, DA7219_ALC_CTRL1, 359 DA7219_ALC_OFFSET_EN_MASK | 360 DA7219_ALC_SYNC_MODE_MASK, 0); 361 } else { 362 /* Enable DC offset cancellation, hybrid mode */ 363 snd_soc_component_update_bits(component, DA7219_ALC_CTRL1, 364 DA7219_ALC_OFFSET_EN_MASK | 365 DA7219_ALC_SYNC_MODE_MASK, 366 DA7219_ALC_OFFSET_EN_MASK | 367 DA7219_ALC_SYNC_MODE_MASK); 368 } 369 370 /* Restore input filter control register to original state */ 371 snd_soc_component_write(component, DA7219_ADC_L_CTRL, adc_ctrl); 372 373 /* Restore input mixer control registers to original state */ 374 snd_soc_component_write(component, DA7219_MIXIN_L_CTRL, mixin_ctrl); 375 376 /* Restore MIC control registers to original states */ 377 snd_soc_component_write(component, DA7219_MIC_1_CTRL, mic_ctrl); 378 } 379 380 static int da7219_mixin_gain_put(struct snd_kcontrol *kcontrol, 381 struct snd_ctl_elem_value *ucontrol) 382 { 383 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 384 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 385 int ret; 386 387 ret = snd_soc_put_volsw(kcontrol, ucontrol); 388 389 /* 390 * If ALC in operation and value of control has been updated, 391 * make sure calibrated offsets are updated. 392 */ 393 if ((ret == 1) && (da7219->alc_en)) 394 da7219_alc_calib(component); 395 396 return ret; 397 } 398 399 static int da7219_alc_sw_put(struct snd_kcontrol *kcontrol, 400 struct snd_ctl_elem_value *ucontrol) 401 { 402 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 403 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 404 405 406 /* Force ALC offset calibration if enabling ALC */ 407 if ((ucontrol->value.integer.value[0]) && (!da7219->alc_en)) { 408 da7219_alc_calib(component); 409 da7219->alc_en = true; 410 } else { 411 da7219->alc_en = false; 412 } 413 414 return snd_soc_put_volsw(kcontrol, ucontrol); 415 } 416 417 /* ToneGen */ 418 static int da7219_tonegen_freq_get(struct snd_kcontrol *kcontrol, 419 struct snd_ctl_elem_value *ucontrol) 420 { 421 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 422 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 423 struct soc_mixer_control *mixer_ctrl = 424 (struct soc_mixer_control *) kcontrol->private_value; 425 unsigned int reg = mixer_ctrl->reg; 426 __le16 val; 427 int ret; 428 429 mutex_lock(&da7219->ctrl_lock); 430 ret = regmap_raw_read(da7219->regmap, reg, &val, sizeof(val)); 431 mutex_unlock(&da7219->ctrl_lock); 432 433 if (ret) 434 return ret; 435 436 /* 437 * Frequency value spans two 8-bit registers, lower then upper byte. 438 * Therefore we need to convert to host endianness here. 439 */ 440 ucontrol->value.integer.value[0] = le16_to_cpu(val); 441 442 return 0; 443 } 444 445 static int da7219_tonegen_freq_put(struct snd_kcontrol *kcontrol, 446 struct snd_ctl_elem_value *ucontrol) 447 { 448 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); 449 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 450 struct soc_mixer_control *mixer_ctrl = 451 (struct soc_mixer_control *) kcontrol->private_value; 452 unsigned int reg = mixer_ctrl->reg; 453 __le16 val; 454 int ret; 455 456 /* 457 * Frequency value spans two 8-bit registers, lower then upper byte. 458 * Therefore we need to convert to little endian here to align with 459 * HW registers. 460 */ 461 val = cpu_to_le16(ucontrol->value.integer.value[0]); 462 463 mutex_lock(&da7219->ctrl_lock); 464 ret = regmap_raw_write(da7219->regmap, reg, &val, sizeof(val)); 465 mutex_unlock(&da7219->ctrl_lock); 466 467 return ret; 468 } 469 470 471 /* 472 * KControls 473 */ 474 475 static const struct snd_kcontrol_new da7219_snd_controls[] = { 476 /* Mics */ 477 SOC_SINGLE_TLV("Mic Volume", DA7219_MIC_1_GAIN, 478 DA7219_MIC_1_AMP_GAIN_SHIFT, DA7219_MIC_1_AMP_GAIN_MAX, 479 DA7219_NO_INVERT, da7219_mic_gain_tlv), 480 SOC_SINGLE("Mic Switch", DA7219_MIC_1_CTRL, 481 DA7219_MIC_1_AMP_MUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX, 482 DA7219_INVERT), 483 484 /* Mixer Input */ 485 SOC_SINGLE_EXT_TLV("Mixin Volume", DA7219_MIXIN_L_GAIN, 486 DA7219_MIXIN_L_AMP_GAIN_SHIFT, 487 DA7219_MIXIN_L_AMP_GAIN_MAX, DA7219_NO_INVERT, 488 snd_soc_get_volsw, da7219_mixin_gain_put, 489 da7219_mixin_gain_tlv), 490 SOC_SINGLE("Mixin Switch", DA7219_MIXIN_L_CTRL, 491 DA7219_MIXIN_L_AMP_MUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX, 492 DA7219_INVERT), 493 SOC_SINGLE("Mixin Gain Ramp Switch", DA7219_MIXIN_L_CTRL, 494 DA7219_MIXIN_L_AMP_RAMP_EN_SHIFT, DA7219_SWITCH_EN_MAX, 495 DA7219_NO_INVERT), 496 SOC_SINGLE("Mixin ZC Gain Switch", DA7219_MIXIN_L_CTRL, 497 DA7219_MIXIN_L_AMP_ZC_EN_SHIFT, DA7219_SWITCH_EN_MAX, 498 DA7219_NO_INVERT), 499 500 /* ADC */ 501 SOC_SINGLE_TLV("Capture Digital Volume", DA7219_ADC_L_GAIN, 502 DA7219_ADC_L_DIGITAL_GAIN_SHIFT, 503 DA7219_ADC_L_DIGITAL_GAIN_MAX, DA7219_NO_INVERT, 504 da7219_adc_dig_gain_tlv), 505 SOC_SINGLE("Capture Digital Switch", DA7219_ADC_L_CTRL, 506 DA7219_ADC_L_MUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX, 507 DA7219_INVERT), 508 SOC_SINGLE("Capture Digital Gain Ramp Switch", DA7219_ADC_L_CTRL, 509 DA7219_ADC_L_RAMP_EN_SHIFT, DA7219_SWITCH_EN_MAX, 510 DA7219_NO_INVERT), 511 512 /* ALC */ 513 SOC_ENUM("ALC Attack Rate", da7219_alc_attack_rate), 514 SOC_ENUM("ALC Release Rate", da7219_alc_release_rate), 515 SOC_ENUM("ALC Hold Time", da7219_alc_hold_time), 516 SOC_ENUM("ALC Envelope Attack Rate", da7219_alc_env_attack_rate), 517 SOC_ENUM("ALC Envelope Release Rate", da7219_alc_env_release_rate), 518 SOC_SINGLE_TLV("ALC Noise Threshold", DA7219_ALC_NOISE, 519 DA7219_ALC_NOISE_SHIFT, DA7219_ALC_THRESHOLD_MAX, 520 DA7219_INVERT, da7219_alc_threshold_tlv), 521 SOC_SINGLE_TLV("ALC Min Threshold", DA7219_ALC_TARGET_MIN, 522 DA7219_ALC_THRESHOLD_MIN_SHIFT, DA7219_ALC_THRESHOLD_MAX, 523 DA7219_INVERT, da7219_alc_threshold_tlv), 524 SOC_SINGLE_TLV("ALC Max Threshold", DA7219_ALC_TARGET_MAX, 525 DA7219_ALC_THRESHOLD_MAX_SHIFT, DA7219_ALC_THRESHOLD_MAX, 526 DA7219_INVERT, da7219_alc_threshold_tlv), 527 SOC_SINGLE_TLV("ALC Max Attenuation", DA7219_ALC_GAIN_LIMITS, 528 DA7219_ALC_ATTEN_MAX_SHIFT, DA7219_ALC_ATTEN_GAIN_MAX, 529 DA7219_NO_INVERT, da7219_alc_gain_tlv), 530 SOC_SINGLE_TLV("ALC Max Volume", DA7219_ALC_GAIN_LIMITS, 531 DA7219_ALC_GAIN_MAX_SHIFT, DA7219_ALC_ATTEN_GAIN_MAX, 532 DA7219_NO_INVERT, da7219_alc_gain_tlv), 533 SOC_SINGLE_RANGE_TLV("ALC Min Analog Volume", DA7219_ALC_ANA_GAIN_LIMITS, 534 DA7219_ALC_ANA_GAIN_MIN_SHIFT, 535 DA7219_ALC_ANA_GAIN_MIN, DA7219_ALC_ANA_GAIN_MAX, 536 DA7219_NO_INVERT, da7219_alc_ana_gain_tlv), 537 SOC_SINGLE_RANGE_TLV("ALC Max Analog Volume", DA7219_ALC_ANA_GAIN_LIMITS, 538 DA7219_ALC_ANA_GAIN_MAX_SHIFT, 539 DA7219_ALC_ANA_GAIN_MIN, DA7219_ALC_ANA_GAIN_MAX, 540 DA7219_NO_INVERT, da7219_alc_ana_gain_tlv), 541 SOC_ENUM("ALC Anticlip Step", da7219_alc_anticlip_step), 542 SOC_SINGLE("ALC Anticlip Switch", DA7219_ALC_ANTICLIP_CTRL, 543 DA7219_ALC_ANTIPCLIP_EN_SHIFT, DA7219_SWITCH_EN_MAX, 544 DA7219_NO_INVERT), 545 SOC_SINGLE_EXT("ALC Switch", DA7219_ALC_CTRL1, DA7219_ALC_EN_SHIFT, 546 DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT, 547 snd_soc_get_volsw, da7219_alc_sw_put), 548 549 /* Input High-Pass Filters */ 550 SOC_ENUM("ADC HPF Mode", da7219_adc_hpf_mode), 551 SOC_ENUM("ADC HPF Corner Audio", da7219_adc_audio_hpf_corner), 552 SOC_ENUM("ADC HPF Corner Voice", da7219_adc_voice_hpf_corner), 553 554 /* Sidetone Filter */ 555 SOC_SINGLE_TLV("Sidetone Volume", DA7219_SIDETONE_GAIN, 556 DA7219_SIDETONE_GAIN_SHIFT, DA7219_SIDETONE_GAIN_MAX, 557 DA7219_NO_INVERT, da7219_sidetone_gain_tlv), 558 SOC_SINGLE("Sidetone Switch", DA7219_SIDETONE_CTRL, 559 DA7219_SIDETONE_MUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX, 560 DA7219_INVERT), 561 562 /* Tone Generator */ 563 SOC_SINGLE_EXT_TLV("ToneGen Volume", DA7219_TONE_GEN_CFG2, 564 DA7219_TONE_GEN_GAIN_SHIFT, DA7219_TONE_GEN_GAIN_MAX, 565 DA7219_NO_INVERT, da7219_volsw_locked_get, 566 da7219_volsw_locked_put, da7219_tonegen_gain_tlv), 567 SOC_ENUM_EXT("ToneGen DTMF Key", da7219_tonegen_dtmf_key, 568 da7219_enum_locked_get, da7219_enum_locked_put), 569 SOC_SINGLE_EXT("ToneGen DTMF Switch", DA7219_TONE_GEN_CFG1, 570 DA7219_DTMF_EN_SHIFT, DA7219_SWITCH_EN_MAX, 571 DA7219_NO_INVERT, da7219_volsw_locked_get, 572 da7219_volsw_locked_put), 573 SOC_ENUM_EXT("ToneGen Sinewave Gen Type", da7219_tonegen_swg_sel, 574 da7219_enum_locked_get, da7219_enum_locked_put), 575 SOC_SINGLE_EXT("ToneGen Sinewave1 Freq", DA7219_TONE_GEN_FREQ1_L, 576 DA7219_FREQ1_L_SHIFT, DA7219_FREQ_MAX, DA7219_NO_INVERT, 577 da7219_tonegen_freq_get, da7219_tonegen_freq_put), 578 SOC_SINGLE_EXT("ToneGen Sinewave2 Freq", DA7219_TONE_GEN_FREQ2_L, 579 DA7219_FREQ2_L_SHIFT, DA7219_FREQ_MAX, DA7219_NO_INVERT, 580 da7219_tonegen_freq_get, da7219_tonegen_freq_put), 581 SOC_SINGLE_EXT("ToneGen On Time", DA7219_TONE_GEN_ON_PER, 582 DA7219_BEEP_ON_PER_SHIFT, DA7219_BEEP_ON_OFF_MAX, 583 DA7219_NO_INVERT, da7219_volsw_locked_get, 584 da7219_volsw_locked_put), 585 SOC_SINGLE("ToneGen Off Time", DA7219_TONE_GEN_OFF_PER, 586 DA7219_BEEP_OFF_PER_SHIFT, DA7219_BEEP_ON_OFF_MAX, 587 DA7219_NO_INVERT), 588 589 /* Gain ramping */ 590 SOC_ENUM("Gain Ramp Rate", da7219_gain_ramp_rate), 591 592 /* DAC High-Pass Filter */ 593 SOC_ENUM_EXT("DAC HPF Mode", da7219_dac_hpf_mode, 594 da7219_enum_locked_get, da7219_enum_locked_put), 595 SOC_ENUM("DAC HPF Corner Audio", da7219_dac_audio_hpf_corner), 596 SOC_ENUM("DAC HPF Corner Voice", da7219_dac_voice_hpf_corner), 597 598 /* DAC 5-Band Equaliser */ 599 SOC_SINGLE_TLV("DAC EQ Band1 Volume", DA7219_DAC_FILTERS2, 600 DA7219_DAC_EQ_BAND1_SHIFT, DA7219_DAC_EQ_BAND_MAX, 601 DA7219_NO_INVERT, da7219_dac_eq_band_tlv), 602 SOC_SINGLE_TLV("DAC EQ Band2 Volume", DA7219_DAC_FILTERS2, 603 DA7219_DAC_EQ_BAND2_SHIFT, DA7219_DAC_EQ_BAND_MAX, 604 DA7219_NO_INVERT, da7219_dac_eq_band_tlv), 605 SOC_SINGLE_TLV("DAC EQ Band3 Volume", DA7219_DAC_FILTERS3, 606 DA7219_DAC_EQ_BAND3_SHIFT, DA7219_DAC_EQ_BAND_MAX, 607 DA7219_NO_INVERT, da7219_dac_eq_band_tlv), 608 SOC_SINGLE_TLV("DAC EQ Band4 Volume", DA7219_DAC_FILTERS3, 609 DA7219_DAC_EQ_BAND4_SHIFT, DA7219_DAC_EQ_BAND_MAX, 610 DA7219_NO_INVERT, da7219_dac_eq_band_tlv), 611 SOC_SINGLE_TLV("DAC EQ Band5 Volume", DA7219_DAC_FILTERS4, 612 DA7219_DAC_EQ_BAND5_SHIFT, DA7219_DAC_EQ_BAND_MAX, 613 DA7219_NO_INVERT, da7219_dac_eq_band_tlv), 614 SOC_SINGLE_EXT("DAC EQ Switch", DA7219_DAC_FILTERS4, 615 DA7219_DAC_EQ_EN_SHIFT, DA7219_SWITCH_EN_MAX, 616 DA7219_NO_INVERT, da7219_volsw_locked_get, 617 da7219_volsw_locked_put), 618 619 /* DAC Softmute */ 620 SOC_ENUM("DAC Soft Mute Rate", da7219_dac_softmute_rate), 621 SOC_SINGLE_EXT("DAC Soft Mute Switch", DA7219_DAC_FILTERS5, 622 DA7219_DAC_SOFTMUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX, 623 DA7219_NO_INVERT, da7219_volsw_locked_get, 624 da7219_volsw_locked_put), 625 626 /* DAC Noise Gate */ 627 SOC_ENUM("DAC NG Setup Time", da7219_dac_ng_setup_time), 628 SOC_ENUM("DAC NG Rampup Rate", da7219_dac_ng_rampup_rate), 629 SOC_ENUM("DAC NG Rampdown Rate", da7219_dac_ng_rampdown_rate), 630 SOC_SINGLE_TLV("DAC NG Off Threshold", DA7219_DAC_NG_OFF_THRESH, 631 DA7219_DAC_NG_OFF_THRESHOLD_SHIFT, 632 DA7219_DAC_NG_THRESHOLD_MAX, DA7219_NO_INVERT, 633 da7219_dac_ng_threshold_tlv), 634 SOC_SINGLE_TLV("DAC NG On Threshold", DA7219_DAC_NG_ON_THRESH, 635 DA7219_DAC_NG_ON_THRESHOLD_SHIFT, 636 DA7219_DAC_NG_THRESHOLD_MAX, DA7219_NO_INVERT, 637 da7219_dac_ng_threshold_tlv), 638 SOC_SINGLE("DAC NG Switch", DA7219_DAC_NG_CTRL, DA7219_DAC_NG_EN_SHIFT, 639 DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT), 640 641 /* DACs */ 642 SOC_DOUBLE_R_EXT_TLV("Playback Digital Volume", DA7219_DAC_L_GAIN, 643 DA7219_DAC_R_GAIN, DA7219_DAC_L_DIGITAL_GAIN_SHIFT, 644 DA7219_DAC_DIGITAL_GAIN_MAX, DA7219_NO_INVERT, 645 da7219_volsw_locked_get, da7219_volsw_locked_put, 646 da7219_dac_dig_gain_tlv), 647 SOC_DOUBLE_R_EXT("Playback Digital Switch", DA7219_DAC_L_CTRL, 648 DA7219_DAC_R_CTRL, DA7219_DAC_L_MUTE_EN_SHIFT, 649 DA7219_SWITCH_EN_MAX, DA7219_INVERT, 650 da7219_volsw_locked_get, da7219_volsw_locked_put), 651 SOC_DOUBLE_R("Playback Digital Gain Ramp Switch", DA7219_DAC_L_CTRL, 652 DA7219_DAC_R_CTRL, DA7219_DAC_L_RAMP_EN_SHIFT, 653 DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT), 654 655 /* CP */ 656 SOC_ENUM("Charge Pump Track Mode", da7219_cp_track_mode), 657 SOC_SINGLE("Charge Pump Threshold", DA7219_CP_VOL_THRESHOLD1, 658 DA7219_CP_THRESH_VDD2_SHIFT, DA7219_CP_THRESH_VDD2_MAX, 659 DA7219_NO_INVERT), 660 661 /* Headphones */ 662 SOC_DOUBLE_R_EXT_TLV("Headphone Volume", DA7219_HP_L_GAIN, 663 DA7219_HP_R_GAIN, DA7219_HP_L_AMP_GAIN_SHIFT, 664 DA7219_HP_AMP_GAIN_MAX, DA7219_NO_INVERT, 665 da7219_volsw_locked_get, da7219_volsw_locked_put, 666 da7219_hp_gain_tlv), 667 SOC_DOUBLE_R_EXT("Headphone Switch", DA7219_HP_L_CTRL, DA7219_HP_R_CTRL, 668 DA7219_HP_L_AMP_MUTE_EN_SHIFT, DA7219_SWITCH_EN_MAX, 669 DA7219_INVERT, da7219_volsw_locked_get, 670 da7219_volsw_locked_put), 671 SOC_DOUBLE_R("Headphone Gain Ramp Switch", DA7219_HP_L_CTRL, 672 DA7219_HP_R_CTRL, DA7219_HP_L_AMP_RAMP_EN_SHIFT, 673 DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT), 674 SOC_DOUBLE_R("Headphone ZC Gain Switch", DA7219_HP_L_CTRL, 675 DA7219_HP_R_CTRL, DA7219_HP_L_AMP_ZC_EN_SHIFT, 676 DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT), 677 }; 678 679 680 /* 681 * DAPM Mux Controls 682 */ 683 684 static const char * const da7219_out_sel_txt[] = { 685 "ADC", "Tone Generator", "DAIL", "DAIR" 686 }; 687 688 static const struct soc_enum da7219_out_dail_sel = 689 SOC_ENUM_SINGLE(DA7219_DIG_ROUTING_DAI, 690 DA7219_DAI_L_SRC_SHIFT, 691 DA7219_OUT_SRC_MAX, 692 da7219_out_sel_txt); 693 694 static const struct snd_kcontrol_new da7219_out_dail_sel_mux = 695 SOC_DAPM_ENUM("Out DAIL Mux", da7219_out_dail_sel); 696 697 static const struct soc_enum da7219_out_dair_sel = 698 SOC_ENUM_SINGLE(DA7219_DIG_ROUTING_DAI, 699 DA7219_DAI_R_SRC_SHIFT, 700 DA7219_OUT_SRC_MAX, 701 da7219_out_sel_txt); 702 703 static const struct snd_kcontrol_new da7219_out_dair_sel_mux = 704 SOC_DAPM_ENUM("Out DAIR Mux", da7219_out_dair_sel); 705 706 static const struct soc_enum da7219_out_dacl_sel = 707 SOC_ENUM_SINGLE(DA7219_DIG_ROUTING_DAC, 708 DA7219_DAC_L_SRC_SHIFT, 709 DA7219_OUT_SRC_MAX, 710 da7219_out_sel_txt); 711 712 static const struct snd_kcontrol_new da7219_out_dacl_sel_mux = 713 SOC_DAPM_ENUM("Out DACL Mux", da7219_out_dacl_sel); 714 715 static const struct soc_enum da7219_out_dacr_sel = 716 SOC_ENUM_SINGLE(DA7219_DIG_ROUTING_DAC, 717 DA7219_DAC_R_SRC_SHIFT, 718 DA7219_OUT_SRC_MAX, 719 da7219_out_sel_txt); 720 721 static const struct snd_kcontrol_new da7219_out_dacr_sel_mux = 722 SOC_DAPM_ENUM("Out DACR Mux", da7219_out_dacr_sel); 723 724 725 /* 726 * DAPM Mixer Controls 727 */ 728 729 static const struct snd_kcontrol_new da7219_mixin_controls[] = { 730 SOC_DAPM_SINGLE("Mic Switch", DA7219_MIXIN_L_SELECT, 731 DA7219_MIXIN_L_MIX_SELECT_SHIFT, 732 DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT), 733 }; 734 735 static const struct snd_kcontrol_new da7219_mixout_l_controls[] = { 736 SOC_DAPM_SINGLE("DACL Switch", DA7219_MIXOUT_L_SELECT, 737 DA7219_MIXOUT_L_MIX_SELECT_SHIFT, 738 DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT), 739 }; 740 741 static const struct snd_kcontrol_new da7219_mixout_r_controls[] = { 742 SOC_DAPM_SINGLE("DACR Switch", DA7219_MIXOUT_R_SELECT, 743 DA7219_MIXOUT_R_MIX_SELECT_SHIFT, 744 DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT), 745 }; 746 747 #define DA7219_DMIX_ST_CTRLS(reg) \ 748 SOC_DAPM_SINGLE("Out FilterL Switch", reg, \ 749 DA7219_DMIX_ST_SRC_OUTFILT1L_SHIFT, \ 750 DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT), \ 751 SOC_DAPM_SINGLE("Out FilterR Switch", reg, \ 752 DA7219_DMIX_ST_SRC_OUTFILT1R_SHIFT, \ 753 DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT), \ 754 SOC_DAPM_SINGLE("Sidetone Switch", reg, \ 755 DA7219_DMIX_ST_SRC_SIDETONE_SHIFT, \ 756 DA7219_SWITCH_EN_MAX, DA7219_NO_INVERT) \ 757 758 static const struct snd_kcontrol_new da7219_st_out_filtl_mix_controls[] = { 759 DA7219_DMIX_ST_CTRLS(DA7219_DROUTING_ST_OUTFILT_1L), 760 }; 761 762 static const struct snd_kcontrol_new da7219_st_out_filtr_mix_controls[] = { 763 DA7219_DMIX_ST_CTRLS(DA7219_DROUTING_ST_OUTFILT_1R), 764 }; 765 766 767 /* 768 * DAPM Events 769 */ 770 771 static int da7219_mic_pga_event(struct snd_soc_dapm_widget *w, 772 struct snd_kcontrol *kcontrol, int event) 773 { 774 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 775 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 776 777 switch (event) { 778 case SND_SOC_DAPM_POST_PMU: 779 if (da7219->micbias_on_event) { 780 /* 781 * Delay only for first capture after bias enabled to 782 * avoid possible DC offset related noise. 783 */ 784 da7219->micbias_on_event = false; 785 msleep(da7219->mic_pga_delay); 786 } 787 break; 788 default: 789 break; 790 } 791 792 return 0; 793 } 794 795 static int da7219_dai_event(struct snd_soc_dapm_widget *w, 796 struct snd_kcontrol *kcontrol, int event) 797 { 798 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 799 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 800 u8 pll_ctrl, pll_status; 801 int i = 0, ret; 802 bool srm_lock = false; 803 804 switch (event) { 805 case SND_SOC_DAPM_PRE_PMU: 806 if (da7219->master) { 807 /* Enable DAI clks for master mode */ 808 if (da7219->dai_clks) { 809 ret = clk_prepare_enable(da7219->dai_clks); 810 if (ret) { 811 dev_err(component->dev, 812 "Failed to enable dai_clks\n"); 813 return ret; 814 } 815 } else { 816 snd_soc_component_update_bits(component, 817 DA7219_DAI_CLK_MODE, 818 DA7219_DAI_CLK_EN_MASK, 819 DA7219_DAI_CLK_EN_MASK); 820 } 821 } 822 823 /* PC synchronised to DAI */ 824 snd_soc_component_update_bits(component, DA7219_PC_COUNT, 825 DA7219_PC_FREERUN_MASK, 0); 826 827 /* Slave mode, if SRM not enabled no need for status checks */ 828 pll_ctrl = snd_soc_component_read32(component, DA7219_PLL_CTRL); 829 if ((pll_ctrl & DA7219_PLL_MODE_MASK) != DA7219_PLL_MODE_SRM) 830 return 0; 831 832 /* Check SRM has locked */ 833 do { 834 pll_status = snd_soc_component_read32(component, DA7219_PLL_SRM_STS); 835 if (pll_status & DA7219_PLL_SRM_STS_SRM_LOCK) { 836 srm_lock = true; 837 } else { 838 ++i; 839 msleep(50); 840 } 841 } while ((i < DA7219_SRM_CHECK_RETRIES) && (!srm_lock)); 842 843 if (!srm_lock) 844 dev_warn(component->dev, "SRM failed to lock\n"); 845 846 return 0; 847 case SND_SOC_DAPM_POST_PMD: 848 /* PC free-running */ 849 snd_soc_component_update_bits(component, DA7219_PC_COUNT, 850 DA7219_PC_FREERUN_MASK, 851 DA7219_PC_FREERUN_MASK); 852 853 /* Disable DAI clks if in master mode */ 854 if (da7219->master) { 855 if (da7219->dai_clks) 856 clk_disable_unprepare(da7219->dai_clks); 857 else 858 snd_soc_component_update_bits(component, 859 DA7219_DAI_CLK_MODE, 860 DA7219_DAI_CLK_EN_MASK, 861 0); 862 } 863 864 return 0; 865 default: 866 return -EINVAL; 867 } 868 } 869 870 static int da7219_settling_event(struct snd_soc_dapm_widget *w, 871 struct snd_kcontrol *kcontrol, int event) 872 { 873 switch (event) { 874 case SND_SOC_DAPM_POST_PMU: 875 case SND_SOC_DAPM_POST_PMD: 876 msleep(DA7219_SETTLING_DELAY); 877 break; 878 default: 879 break; 880 } 881 882 return 0; 883 } 884 885 static int da7219_mixout_event(struct snd_soc_dapm_widget *w, 886 struct snd_kcontrol *kcontrol, int event) 887 { 888 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 889 u8 hp_ctrl, min_gain_mask; 890 891 switch (w->reg) { 892 case DA7219_MIXOUT_L_CTRL: 893 hp_ctrl = DA7219_HP_L_CTRL; 894 min_gain_mask = DA7219_HP_L_AMP_MIN_GAIN_EN_MASK; 895 break; 896 case DA7219_MIXOUT_R_CTRL: 897 hp_ctrl = DA7219_HP_R_CTRL; 898 min_gain_mask = DA7219_HP_R_AMP_MIN_GAIN_EN_MASK; 899 break; 900 default: 901 return -EINVAL; 902 } 903 904 switch (event) { 905 case SND_SOC_DAPM_PRE_PMD: 906 /* Enable minimum gain on HP to avoid pops */ 907 snd_soc_component_update_bits(component, hp_ctrl, min_gain_mask, 908 min_gain_mask); 909 910 msleep(DA7219_MIN_GAIN_DELAY); 911 912 break; 913 case SND_SOC_DAPM_POST_PMU: 914 /* Remove minimum gain on HP */ 915 snd_soc_component_update_bits(component, hp_ctrl, min_gain_mask, 0); 916 917 break; 918 } 919 920 return 0; 921 } 922 923 static int da7219_gain_ramp_event(struct snd_soc_dapm_widget *w, 924 struct snd_kcontrol *kcontrol, int event) 925 { 926 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 927 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 928 929 switch (event) { 930 case SND_SOC_DAPM_PRE_PMU: 931 case SND_SOC_DAPM_PRE_PMD: 932 /* Ensure nominal gain ramping for DAPM sequence */ 933 da7219->gain_ramp_ctrl = 934 snd_soc_component_read32(component, DA7219_GAIN_RAMP_CTRL); 935 snd_soc_component_write(component, DA7219_GAIN_RAMP_CTRL, 936 DA7219_GAIN_RAMP_RATE_NOMINAL); 937 break; 938 case SND_SOC_DAPM_POST_PMU: 939 case SND_SOC_DAPM_POST_PMD: 940 /* Restore previous gain ramp settings */ 941 snd_soc_component_write(component, DA7219_GAIN_RAMP_CTRL, 942 da7219->gain_ramp_ctrl); 943 break; 944 } 945 946 return 0; 947 } 948 949 950 /* 951 * DAPM Widgets 952 */ 953 954 static const struct snd_soc_dapm_widget da7219_dapm_widgets[] = { 955 /* Input Supplies */ 956 SND_SOC_DAPM_SUPPLY("Mic Bias", DA7219_MICBIAS_CTRL, 957 DA7219_MICBIAS1_EN_SHIFT, DA7219_NO_INVERT, 958 NULL, 0), 959 960 /* Inputs */ 961 SND_SOC_DAPM_INPUT("MIC"), 962 963 /* Input PGAs */ 964 SND_SOC_DAPM_PGA_E("Mic PGA", DA7219_MIC_1_CTRL, 965 DA7219_MIC_1_AMP_EN_SHIFT, DA7219_NO_INVERT, 966 NULL, 0, da7219_mic_pga_event, SND_SOC_DAPM_POST_PMU), 967 SND_SOC_DAPM_PGA_E("Mixin PGA", DA7219_MIXIN_L_CTRL, 968 DA7219_MIXIN_L_AMP_EN_SHIFT, DA7219_NO_INVERT, 969 NULL, 0, da7219_settling_event, SND_SOC_DAPM_POST_PMU), 970 971 /* Input Filters */ 972 SND_SOC_DAPM_ADC("ADC", NULL, DA7219_ADC_L_CTRL, DA7219_ADC_L_EN_SHIFT, 973 DA7219_NO_INVERT), 974 975 /* Tone Generator */ 976 SND_SOC_DAPM_SIGGEN("TONE"), 977 SND_SOC_DAPM_PGA("Tone Generator", DA7219_TONE_GEN_CFG1, 978 DA7219_START_STOPN_SHIFT, DA7219_NO_INVERT, NULL, 0), 979 980 /* Sidetone Input */ 981 SND_SOC_DAPM_ADC("Sidetone Filter", NULL, DA7219_SIDETONE_CTRL, 982 DA7219_SIDETONE_EN_SHIFT, DA7219_NO_INVERT), 983 984 /* Input Mixer Supply */ 985 SND_SOC_DAPM_SUPPLY("Mixer In Supply", DA7219_MIXIN_L_CTRL, 986 DA7219_MIXIN_L_MIX_EN_SHIFT, DA7219_NO_INVERT, 987 NULL, 0), 988 989 /* Input Mixer */ 990 SND_SOC_DAPM_MIXER("Mixer In", SND_SOC_NOPM, 0, 0, 991 da7219_mixin_controls, 992 ARRAY_SIZE(da7219_mixin_controls)), 993 994 /* Input Muxes */ 995 SND_SOC_DAPM_MUX("Out DAIL Mux", SND_SOC_NOPM, 0, 0, 996 &da7219_out_dail_sel_mux), 997 SND_SOC_DAPM_MUX("Out DAIR Mux", SND_SOC_NOPM, 0, 0, 998 &da7219_out_dair_sel_mux), 999 1000 /* DAI Supply */ 1001 SND_SOC_DAPM_SUPPLY("DAI", DA7219_DAI_CTRL, DA7219_DAI_EN_SHIFT, 1002 DA7219_NO_INVERT, da7219_dai_event, 1003 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), 1004 1005 /* DAI */ 1006 SND_SOC_DAPM_AIF_OUT("DAIOUT", "Capture", 0, DA7219_DAI_TDM_CTRL, 1007 DA7219_DAI_OE_SHIFT, DA7219_NO_INVERT), 1008 SND_SOC_DAPM_AIF_IN("DAIIN", "Playback", 0, SND_SOC_NOPM, 0, 0), 1009 1010 /* Output Muxes */ 1011 SND_SOC_DAPM_MUX("Out DACL Mux", SND_SOC_NOPM, 0, 0, 1012 &da7219_out_dacl_sel_mux), 1013 SND_SOC_DAPM_MUX("Out DACR Mux", SND_SOC_NOPM, 0, 0, 1014 &da7219_out_dacr_sel_mux), 1015 1016 /* Output Mixers */ 1017 SND_SOC_DAPM_MIXER("Mixer Out FilterL", SND_SOC_NOPM, 0, 0, 1018 da7219_mixout_l_controls, 1019 ARRAY_SIZE(da7219_mixout_l_controls)), 1020 SND_SOC_DAPM_MIXER("Mixer Out FilterR", SND_SOC_NOPM, 0, 0, 1021 da7219_mixout_r_controls, 1022 ARRAY_SIZE(da7219_mixout_r_controls)), 1023 1024 /* Sidetone Mixers */ 1025 SND_SOC_DAPM_MIXER("ST Mixer Out FilterL", SND_SOC_NOPM, 0, 0, 1026 da7219_st_out_filtl_mix_controls, 1027 ARRAY_SIZE(da7219_st_out_filtl_mix_controls)), 1028 SND_SOC_DAPM_MIXER("ST Mixer Out FilterR", SND_SOC_NOPM, 0, 1029 0, da7219_st_out_filtr_mix_controls, 1030 ARRAY_SIZE(da7219_st_out_filtr_mix_controls)), 1031 1032 /* DACs */ 1033 SND_SOC_DAPM_DAC_E("DACL", NULL, DA7219_DAC_L_CTRL, 1034 DA7219_DAC_L_EN_SHIFT, DA7219_NO_INVERT, 1035 da7219_settling_event, 1036 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), 1037 SND_SOC_DAPM_DAC_E("DACR", NULL, DA7219_DAC_R_CTRL, 1038 DA7219_DAC_R_EN_SHIFT, DA7219_NO_INVERT, 1039 da7219_settling_event, 1040 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), 1041 1042 /* Output PGAs */ 1043 SND_SOC_DAPM_PGA_E("Mixout Left PGA", DA7219_MIXOUT_L_CTRL, 1044 DA7219_MIXOUT_L_AMP_EN_SHIFT, DA7219_NO_INVERT, 1045 NULL, 0, da7219_mixout_event, 1046 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1047 SND_SOC_DAPM_PGA_E("Mixout Right PGA", DA7219_MIXOUT_R_CTRL, 1048 DA7219_MIXOUT_R_AMP_EN_SHIFT, DA7219_NO_INVERT, 1049 NULL, 0, da7219_mixout_event, 1050 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1051 SND_SOC_DAPM_SUPPLY_S("Headphone Left PGA", 1, DA7219_HP_L_CTRL, 1052 DA7219_HP_L_AMP_EN_SHIFT, DA7219_NO_INVERT, 1053 da7219_settling_event, 1054 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), 1055 SND_SOC_DAPM_SUPPLY_S("Headphone Right PGA", 1, DA7219_HP_R_CTRL, 1056 DA7219_HP_R_AMP_EN_SHIFT, DA7219_NO_INVERT, 1057 da7219_settling_event, 1058 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), 1059 1060 /* Output Supplies */ 1061 SND_SOC_DAPM_SUPPLY_S("Charge Pump", 0, DA7219_CP_CTRL, 1062 DA7219_CP_EN_SHIFT, DA7219_NO_INVERT, 1063 da7219_settling_event, 1064 SND_SOC_DAPM_POST_PMU), 1065 1066 /* Outputs */ 1067 SND_SOC_DAPM_OUTPUT("HPL"), 1068 SND_SOC_DAPM_OUTPUT("HPR"), 1069 1070 /* Pre/Post Power */ 1071 SND_SOC_DAPM_PRE("Pre Power Gain Ramp", da7219_gain_ramp_event), 1072 SND_SOC_DAPM_POST("Post Power Gain Ramp", da7219_gain_ramp_event), 1073 }; 1074 1075 1076 /* 1077 * DAPM Mux Routes 1078 */ 1079 1080 #define DA7219_OUT_DAI_MUX_ROUTES(name) \ 1081 {name, "ADC", "Mixer In"}, \ 1082 {name, "Tone Generator", "Tone Generator"}, \ 1083 {name, "DAIL", "DAIOUT"}, \ 1084 {name, "DAIR", "DAIOUT"} 1085 1086 #define DA7219_OUT_DAC_MUX_ROUTES(name) \ 1087 {name, "ADC", "Mixer In"}, \ 1088 {name, "Tone Generator", "Tone Generator"}, \ 1089 {name, "DAIL", "DAIIN"}, \ 1090 {name, "DAIR", "DAIIN"} 1091 1092 /* 1093 * DAPM Mixer Routes 1094 */ 1095 1096 #define DA7219_DMIX_ST_ROUTES(name) \ 1097 {name, "Out FilterL Switch", "Mixer Out FilterL"}, \ 1098 {name, "Out FilterR Switch", "Mixer Out FilterR"}, \ 1099 {name, "Sidetone Switch", "Sidetone Filter"} 1100 1101 1102 /* 1103 * DAPM audio route definition 1104 */ 1105 1106 static const struct snd_soc_dapm_route da7219_audio_map[] = { 1107 /* Input paths */ 1108 {"MIC", NULL, "Mic Bias"}, 1109 {"Mic PGA", NULL, "MIC"}, 1110 {"Mixin PGA", NULL, "Mic PGA"}, 1111 {"ADC", NULL, "Mixin PGA"}, 1112 1113 {"Mixer In", NULL, "Mixer In Supply"}, 1114 {"Mixer In", "Mic Switch", "ADC"}, 1115 1116 {"Sidetone Filter", NULL, "Mixer In"}, 1117 1118 {"Tone Generator", NULL, "TONE"}, 1119 1120 DA7219_OUT_DAI_MUX_ROUTES("Out DAIL Mux"), 1121 DA7219_OUT_DAI_MUX_ROUTES("Out DAIR Mux"), 1122 1123 {"DAIOUT", NULL, "Out DAIL Mux"}, 1124 {"DAIOUT", NULL, "Out DAIR Mux"}, 1125 {"DAIOUT", NULL, "DAI"}, 1126 1127 /* Output paths */ 1128 {"DAIIN", NULL, "DAI"}, 1129 1130 DA7219_OUT_DAC_MUX_ROUTES("Out DACL Mux"), 1131 DA7219_OUT_DAC_MUX_ROUTES("Out DACR Mux"), 1132 1133 {"Mixer Out FilterL", "DACL Switch", "Out DACL Mux"}, 1134 {"Mixer Out FilterR", "DACR Switch", "Out DACR Mux"}, 1135 1136 DA7219_DMIX_ST_ROUTES("ST Mixer Out FilterL"), 1137 DA7219_DMIX_ST_ROUTES("ST Mixer Out FilterR"), 1138 1139 {"DACL", NULL, "ST Mixer Out FilterL"}, 1140 {"DACR", NULL, "ST Mixer Out FilterR"}, 1141 1142 {"Mixout Left PGA", NULL, "DACL"}, 1143 {"Mixout Right PGA", NULL, "DACR"}, 1144 1145 {"HPL", NULL, "Mixout Left PGA"}, 1146 {"HPR", NULL, "Mixout Right PGA"}, 1147 1148 {"HPL", NULL, "Headphone Left PGA"}, 1149 {"HPR", NULL, "Headphone Right PGA"}, 1150 1151 {"HPL", NULL, "Charge Pump"}, 1152 {"HPR", NULL, "Charge Pump"}, 1153 }; 1154 1155 1156 /* 1157 * DAI operations 1158 */ 1159 1160 static int da7219_set_dai_sysclk(struct snd_soc_dai *codec_dai, 1161 int clk_id, unsigned int freq, int dir) 1162 { 1163 struct snd_soc_component *component = codec_dai->component; 1164 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 1165 int ret = 0; 1166 1167 if ((da7219->clk_src == clk_id) && (da7219->mclk_rate == freq)) 1168 return 0; 1169 1170 if ((freq < 2000000) || (freq > 54000000)) { 1171 dev_err(codec_dai->dev, "Unsupported MCLK value %d\n", 1172 freq); 1173 return -EINVAL; 1174 } 1175 1176 mutex_lock(&da7219->pll_lock); 1177 1178 switch (clk_id) { 1179 case DA7219_CLKSRC_MCLK_SQR: 1180 snd_soc_component_update_bits(component, DA7219_PLL_CTRL, 1181 DA7219_PLL_MCLK_SQR_EN_MASK, 1182 DA7219_PLL_MCLK_SQR_EN_MASK); 1183 break; 1184 case DA7219_CLKSRC_MCLK: 1185 snd_soc_component_update_bits(component, DA7219_PLL_CTRL, 1186 DA7219_PLL_MCLK_SQR_EN_MASK, 0); 1187 break; 1188 default: 1189 dev_err(codec_dai->dev, "Unknown clock source %d\n", clk_id); 1190 mutex_unlock(&da7219->pll_lock); 1191 return -EINVAL; 1192 } 1193 1194 da7219->clk_src = clk_id; 1195 1196 if (da7219->mclk) { 1197 freq = clk_round_rate(da7219->mclk, freq); 1198 ret = clk_set_rate(da7219->mclk, freq); 1199 if (ret) { 1200 dev_err(codec_dai->dev, "Failed to set clock rate %d\n", 1201 freq); 1202 mutex_unlock(&da7219->pll_lock); 1203 return ret; 1204 } 1205 } 1206 1207 da7219->mclk_rate = freq; 1208 1209 mutex_unlock(&da7219->pll_lock); 1210 1211 return 0; 1212 } 1213 1214 int da7219_set_pll(struct snd_soc_component *component, int source, unsigned int fout) 1215 { 1216 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 1217 1218 u8 pll_ctrl, indiv_bits, indiv; 1219 u8 pll_frac_top, pll_frac_bot, pll_integer; 1220 u32 freq_ref; 1221 u64 frac_div; 1222 1223 /* Verify 2MHz - 54MHz MCLK provided, and set input divider */ 1224 if (da7219->mclk_rate < 2000000) { 1225 dev_err(component->dev, "PLL input clock %d below valid range\n", 1226 da7219->mclk_rate); 1227 return -EINVAL; 1228 } else if (da7219->mclk_rate <= 4500000) { 1229 indiv_bits = DA7219_PLL_INDIV_2_TO_4_5_MHZ; 1230 indiv = DA7219_PLL_INDIV_2_TO_4_5_MHZ_VAL; 1231 } else if (da7219->mclk_rate <= 9000000) { 1232 indiv_bits = DA7219_PLL_INDIV_4_5_TO_9_MHZ; 1233 indiv = DA7219_PLL_INDIV_4_5_TO_9_MHZ_VAL; 1234 } else if (da7219->mclk_rate <= 18000000) { 1235 indiv_bits = DA7219_PLL_INDIV_9_TO_18_MHZ; 1236 indiv = DA7219_PLL_INDIV_9_TO_18_MHZ_VAL; 1237 } else if (da7219->mclk_rate <= 36000000) { 1238 indiv_bits = DA7219_PLL_INDIV_18_TO_36_MHZ; 1239 indiv = DA7219_PLL_INDIV_18_TO_36_MHZ_VAL; 1240 } else if (da7219->mclk_rate <= 54000000) { 1241 indiv_bits = DA7219_PLL_INDIV_36_TO_54_MHZ; 1242 indiv = DA7219_PLL_INDIV_36_TO_54_MHZ_VAL; 1243 } else { 1244 dev_err(component->dev, "PLL input clock %d above valid range\n", 1245 da7219->mclk_rate); 1246 return -EINVAL; 1247 } 1248 freq_ref = (da7219->mclk_rate / indiv); 1249 pll_ctrl = indiv_bits; 1250 1251 /* Configure PLL */ 1252 switch (source) { 1253 case DA7219_SYSCLK_MCLK: 1254 pll_ctrl |= DA7219_PLL_MODE_BYPASS; 1255 snd_soc_component_update_bits(component, DA7219_PLL_CTRL, 1256 DA7219_PLL_INDIV_MASK | 1257 DA7219_PLL_MODE_MASK, pll_ctrl); 1258 return 0; 1259 case DA7219_SYSCLK_PLL: 1260 pll_ctrl |= DA7219_PLL_MODE_NORMAL; 1261 break; 1262 case DA7219_SYSCLK_PLL_SRM: 1263 pll_ctrl |= DA7219_PLL_MODE_SRM; 1264 break; 1265 default: 1266 dev_err(component->dev, "Invalid PLL config\n"); 1267 return -EINVAL; 1268 } 1269 1270 /* Calculate dividers for PLL */ 1271 pll_integer = fout / freq_ref; 1272 frac_div = (u64)(fout % freq_ref) * 8192ULL; 1273 do_div(frac_div, freq_ref); 1274 pll_frac_top = (frac_div >> DA7219_BYTE_SHIFT) & DA7219_BYTE_MASK; 1275 pll_frac_bot = (frac_div) & DA7219_BYTE_MASK; 1276 1277 /* Write PLL config & dividers */ 1278 snd_soc_component_write(component, DA7219_PLL_FRAC_TOP, pll_frac_top); 1279 snd_soc_component_write(component, DA7219_PLL_FRAC_BOT, pll_frac_bot); 1280 snd_soc_component_write(component, DA7219_PLL_INTEGER, pll_integer); 1281 snd_soc_component_update_bits(component, DA7219_PLL_CTRL, 1282 DA7219_PLL_INDIV_MASK | DA7219_PLL_MODE_MASK, 1283 pll_ctrl); 1284 1285 return 0; 1286 } 1287 1288 static int da7219_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id, 1289 int source, unsigned int fref, unsigned int fout) 1290 { 1291 struct snd_soc_component *component = codec_dai->component; 1292 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 1293 int ret; 1294 1295 mutex_lock(&da7219->pll_lock); 1296 ret = da7219_set_pll(component, source, fout); 1297 mutex_unlock(&da7219->pll_lock); 1298 1299 return ret; 1300 } 1301 1302 static int da7219_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt) 1303 { 1304 struct snd_soc_component *component = codec_dai->component; 1305 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 1306 u8 dai_clk_mode = 0, dai_ctrl = 0; 1307 1308 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 1309 case SND_SOC_DAIFMT_CBM_CFM: 1310 da7219->master = true; 1311 break; 1312 case SND_SOC_DAIFMT_CBS_CFS: 1313 da7219->master = false; 1314 break; 1315 default: 1316 return -EINVAL; 1317 } 1318 1319 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 1320 case SND_SOC_DAIFMT_I2S: 1321 case SND_SOC_DAIFMT_LEFT_J: 1322 case SND_SOC_DAIFMT_RIGHT_J: 1323 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 1324 case SND_SOC_DAIFMT_NB_NF: 1325 break; 1326 case SND_SOC_DAIFMT_NB_IF: 1327 dai_clk_mode |= DA7219_DAI_WCLK_POL_INV; 1328 break; 1329 case SND_SOC_DAIFMT_IB_NF: 1330 dai_clk_mode |= DA7219_DAI_CLK_POL_INV; 1331 break; 1332 case SND_SOC_DAIFMT_IB_IF: 1333 dai_clk_mode |= DA7219_DAI_WCLK_POL_INV | 1334 DA7219_DAI_CLK_POL_INV; 1335 break; 1336 default: 1337 return -EINVAL; 1338 } 1339 break; 1340 case SND_SOC_DAIFMT_DSP_B: 1341 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 1342 case SND_SOC_DAIFMT_NB_NF: 1343 dai_clk_mode |= DA7219_DAI_CLK_POL_INV; 1344 break; 1345 case SND_SOC_DAIFMT_NB_IF: 1346 dai_clk_mode |= DA7219_DAI_WCLK_POL_INV | 1347 DA7219_DAI_CLK_POL_INV; 1348 break; 1349 case SND_SOC_DAIFMT_IB_NF: 1350 break; 1351 case SND_SOC_DAIFMT_IB_IF: 1352 dai_clk_mode |= DA7219_DAI_WCLK_POL_INV; 1353 break; 1354 default: 1355 return -EINVAL; 1356 } 1357 break; 1358 default: 1359 return -EINVAL; 1360 } 1361 1362 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 1363 case SND_SOC_DAIFMT_I2S: 1364 dai_ctrl |= DA7219_DAI_FORMAT_I2S; 1365 break; 1366 case SND_SOC_DAIFMT_LEFT_J: 1367 dai_ctrl |= DA7219_DAI_FORMAT_LEFT_J; 1368 break; 1369 case SND_SOC_DAIFMT_RIGHT_J: 1370 dai_ctrl |= DA7219_DAI_FORMAT_RIGHT_J; 1371 break; 1372 case SND_SOC_DAIFMT_DSP_B: 1373 dai_ctrl |= DA7219_DAI_FORMAT_DSP; 1374 break; 1375 default: 1376 return -EINVAL; 1377 } 1378 1379 snd_soc_component_update_bits(component, DA7219_DAI_CLK_MODE, 1380 DA7219_DAI_CLK_POL_MASK | DA7219_DAI_WCLK_POL_MASK, 1381 dai_clk_mode); 1382 snd_soc_component_update_bits(component, DA7219_DAI_CTRL, DA7219_DAI_FORMAT_MASK, 1383 dai_ctrl); 1384 1385 return 0; 1386 } 1387 1388 static int da7219_set_dai_tdm_slot(struct snd_soc_dai *dai, 1389 unsigned int tx_mask, unsigned int rx_mask, 1390 int slots, int slot_width) 1391 { 1392 struct snd_soc_component *component = dai->component; 1393 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 1394 unsigned int ch_mask; 1395 u8 dai_bclks_per_wclk, slot_offset; 1396 u16 offset; 1397 __le16 dai_offset; 1398 u32 frame_size; 1399 1400 /* No channels enabled so disable TDM */ 1401 if (!tx_mask) { 1402 snd_soc_component_update_bits(component, DA7219_DAI_TDM_CTRL, 1403 DA7219_DAI_TDM_CH_EN_MASK | 1404 DA7219_DAI_TDM_MODE_EN_MASK, 0); 1405 da7219->tdm_en = false; 1406 return 0; 1407 } 1408 1409 /* Check we have valid slots */ 1410 slot_offset = ffs(tx_mask) - 1; 1411 ch_mask = (tx_mask >> slot_offset); 1412 if (fls(ch_mask) > DA7219_DAI_TDM_MAX_SLOTS) { 1413 dev_err(component->dev, 1414 "Invalid number of slots, max = %d\n", 1415 DA7219_DAI_TDM_MAX_SLOTS); 1416 return -EINVAL; 1417 } 1418 1419 /* 1420 * Ensure we have a valid offset into the frame, based on slot width 1421 * and slot offset of first slot we're interested in. 1422 */ 1423 offset = slot_offset * slot_width; 1424 if (offset > DA7219_DAI_OFFSET_MAX) { 1425 dev_err(component->dev, "Invalid frame offset %d\n", offset); 1426 return -EINVAL; 1427 } 1428 1429 /* 1430 * If we're master, calculate & validate frame size based on slot info 1431 * provided as we have a limited set of rates available. 1432 */ 1433 if (da7219->master) { 1434 frame_size = slots * slot_width; 1435 switch (frame_size) { 1436 case 32: 1437 dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_32; 1438 break; 1439 case 64: 1440 dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_64; 1441 break; 1442 case 128: 1443 dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_128; 1444 break; 1445 case 256: 1446 dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_256; 1447 break; 1448 default: 1449 dev_err(component->dev, "Invalid frame size %d\n", 1450 frame_size); 1451 return -EINVAL; 1452 } 1453 1454 snd_soc_component_update_bits(component, DA7219_DAI_CLK_MODE, 1455 DA7219_DAI_BCLKS_PER_WCLK_MASK, 1456 dai_bclks_per_wclk); 1457 } 1458 1459 dai_offset = cpu_to_le16(offset); 1460 regmap_bulk_write(da7219->regmap, DA7219_DAI_OFFSET_LOWER, 1461 &dai_offset, sizeof(dai_offset)); 1462 1463 snd_soc_component_update_bits(component, DA7219_DAI_TDM_CTRL, 1464 DA7219_DAI_TDM_CH_EN_MASK | 1465 DA7219_DAI_TDM_MODE_EN_MASK, 1466 (ch_mask << DA7219_DAI_TDM_CH_EN_SHIFT) | 1467 DA7219_DAI_TDM_MODE_EN_MASK); 1468 1469 da7219->tdm_en = true; 1470 1471 return 0; 1472 } 1473 1474 static int da7219_hw_params(struct snd_pcm_substream *substream, 1475 struct snd_pcm_hw_params *params, 1476 struct snd_soc_dai *dai) 1477 { 1478 struct snd_soc_component *component = dai->component; 1479 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 1480 u8 dai_ctrl = 0, dai_bclks_per_wclk = 0, fs; 1481 unsigned int channels; 1482 int word_len = params_width(params); 1483 int frame_size; 1484 1485 switch (word_len) { 1486 case 16: 1487 dai_ctrl |= DA7219_DAI_WORD_LENGTH_S16_LE; 1488 break; 1489 case 20: 1490 dai_ctrl |= DA7219_DAI_WORD_LENGTH_S20_LE; 1491 break; 1492 case 24: 1493 dai_ctrl |= DA7219_DAI_WORD_LENGTH_S24_LE; 1494 break; 1495 case 32: 1496 dai_ctrl |= DA7219_DAI_WORD_LENGTH_S32_LE; 1497 break; 1498 default: 1499 return -EINVAL; 1500 } 1501 1502 channels = params_channels(params); 1503 if ((channels < 1) || (channels > DA7219_DAI_CH_NUM_MAX)) { 1504 dev_err(component->dev, 1505 "Invalid number of channels, only 1 to %d supported\n", 1506 DA7219_DAI_CH_NUM_MAX); 1507 return -EINVAL; 1508 } 1509 dai_ctrl |= channels << DA7219_DAI_CH_NUM_SHIFT; 1510 1511 switch (params_rate(params)) { 1512 case 8000: 1513 fs = DA7219_SR_8000; 1514 break; 1515 case 11025: 1516 fs = DA7219_SR_11025; 1517 break; 1518 case 12000: 1519 fs = DA7219_SR_12000; 1520 break; 1521 case 16000: 1522 fs = DA7219_SR_16000; 1523 break; 1524 case 22050: 1525 fs = DA7219_SR_22050; 1526 break; 1527 case 24000: 1528 fs = DA7219_SR_24000; 1529 break; 1530 case 32000: 1531 fs = DA7219_SR_32000; 1532 break; 1533 case 44100: 1534 fs = DA7219_SR_44100; 1535 break; 1536 case 48000: 1537 fs = DA7219_SR_48000; 1538 break; 1539 case 88200: 1540 fs = DA7219_SR_88200; 1541 break; 1542 case 96000: 1543 fs = DA7219_SR_96000; 1544 break; 1545 default: 1546 return -EINVAL; 1547 } 1548 1549 /* 1550 * If we're master, then we have a limited set of BCLK rates we 1551 * support. For slave mode this isn't the case and the codec can detect 1552 * the BCLK rate automatically. 1553 */ 1554 if (da7219->master && !da7219->tdm_en) { 1555 frame_size = word_len * 2; 1556 if (frame_size <= 32) 1557 dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_32; 1558 else 1559 dai_bclks_per_wclk = DA7219_DAI_BCLKS_PER_WCLK_64; 1560 1561 snd_soc_component_update_bits(component, DA7219_DAI_CLK_MODE, 1562 DA7219_DAI_BCLKS_PER_WCLK_MASK, 1563 dai_bclks_per_wclk); 1564 } 1565 1566 snd_soc_component_update_bits(component, DA7219_DAI_CTRL, 1567 DA7219_DAI_WORD_LENGTH_MASK | 1568 DA7219_DAI_CH_NUM_MASK, 1569 dai_ctrl); 1570 snd_soc_component_write(component, DA7219_SR, fs); 1571 1572 return 0; 1573 } 1574 1575 static const struct snd_soc_dai_ops da7219_dai_ops = { 1576 .hw_params = da7219_hw_params, 1577 .set_sysclk = da7219_set_dai_sysclk, 1578 .set_pll = da7219_set_dai_pll, 1579 .set_fmt = da7219_set_dai_fmt, 1580 .set_tdm_slot = da7219_set_dai_tdm_slot, 1581 }; 1582 1583 #define DA7219_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ 1584 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) 1585 1586 static struct snd_soc_dai_driver da7219_dai = { 1587 .name = "da7219-hifi", 1588 .playback = { 1589 .stream_name = "Playback", 1590 .channels_min = 1, 1591 .channels_max = DA7219_DAI_CH_NUM_MAX, 1592 .rates = SNDRV_PCM_RATE_8000_96000, 1593 .formats = DA7219_FORMATS, 1594 }, 1595 .capture = { 1596 .stream_name = "Capture", 1597 .channels_min = 1, 1598 .channels_max = DA7219_DAI_CH_NUM_MAX, 1599 .rates = SNDRV_PCM_RATE_8000_96000, 1600 .formats = DA7219_FORMATS, 1601 }, 1602 .ops = &da7219_dai_ops, 1603 .symmetric_rates = 1, 1604 .symmetric_channels = 1, 1605 .symmetric_samplebits = 1, 1606 }; 1607 1608 1609 /* 1610 * DT/ACPI 1611 */ 1612 1613 static const struct of_device_id da7219_of_match[] = { 1614 { .compatible = "dlg,da7219", }, 1615 { } 1616 }; 1617 MODULE_DEVICE_TABLE(of, da7219_of_match); 1618 1619 static const struct acpi_device_id da7219_acpi_match[] = { 1620 { .id = "DLGS7219", }, 1621 { } 1622 }; 1623 MODULE_DEVICE_TABLE(acpi, da7219_acpi_match); 1624 1625 static enum da7219_micbias_voltage 1626 da7219_fw_micbias_lvl(struct device *dev, u32 val) 1627 { 1628 switch (val) { 1629 case 1600: 1630 return DA7219_MICBIAS_1_6V; 1631 case 1800: 1632 return DA7219_MICBIAS_1_8V; 1633 case 2000: 1634 return DA7219_MICBIAS_2_0V; 1635 case 2200: 1636 return DA7219_MICBIAS_2_2V; 1637 case 2400: 1638 return DA7219_MICBIAS_2_4V; 1639 case 2600: 1640 return DA7219_MICBIAS_2_6V; 1641 default: 1642 dev_warn(dev, "Invalid micbias level"); 1643 return DA7219_MICBIAS_2_2V; 1644 } 1645 } 1646 1647 static enum da7219_mic_amp_in_sel 1648 da7219_fw_mic_amp_in_sel(struct device *dev, const char *str) 1649 { 1650 if (!strcmp(str, "diff")) { 1651 return DA7219_MIC_AMP_IN_SEL_DIFF; 1652 } else if (!strcmp(str, "se_p")) { 1653 return DA7219_MIC_AMP_IN_SEL_SE_P; 1654 } else if (!strcmp(str, "se_n")) { 1655 return DA7219_MIC_AMP_IN_SEL_SE_N; 1656 } else { 1657 dev_warn(dev, "Invalid mic input type selection"); 1658 return DA7219_MIC_AMP_IN_SEL_DIFF; 1659 } 1660 } 1661 1662 static struct da7219_pdata *da7219_fw_to_pdata(struct snd_soc_component *component) 1663 { 1664 struct device *dev = component->dev; 1665 struct da7219_pdata *pdata; 1666 const char *of_str; 1667 u32 of_val32; 1668 1669 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 1670 if (!pdata) 1671 return NULL; 1672 1673 pdata->wakeup_source = device_property_read_bool(dev, "wakeup-source"); 1674 1675 pdata->dai_clks_name = "da7219-dai-clks"; 1676 if (device_property_read_string(dev, "clock-output-names", 1677 &pdata->dai_clks_name)) 1678 dev_warn(dev, "Using default clk name: %s\n", 1679 pdata->dai_clks_name); 1680 1681 if (device_property_read_u32(dev, "dlg,micbias-lvl", &of_val32) >= 0) 1682 pdata->micbias_lvl = da7219_fw_micbias_lvl(dev, of_val32); 1683 else 1684 pdata->micbias_lvl = DA7219_MICBIAS_2_2V; 1685 1686 if (!device_property_read_string(dev, "dlg,mic-amp-in-sel", &of_str)) 1687 pdata->mic_amp_in_sel = da7219_fw_mic_amp_in_sel(dev, of_str); 1688 else 1689 pdata->mic_amp_in_sel = DA7219_MIC_AMP_IN_SEL_DIFF; 1690 1691 return pdata; 1692 } 1693 1694 1695 /* 1696 * Codec driver functions 1697 */ 1698 1699 static int da7219_set_bias_level(struct snd_soc_component *component, 1700 enum snd_soc_bias_level level) 1701 { 1702 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 1703 int ret; 1704 1705 switch (level) { 1706 case SND_SOC_BIAS_ON: 1707 break; 1708 case SND_SOC_BIAS_PREPARE: 1709 /* Enable MCLK for transition to ON state */ 1710 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_STANDBY) { 1711 if (da7219->mclk) { 1712 ret = clk_prepare_enable(da7219->mclk); 1713 if (ret) { 1714 dev_err(component->dev, 1715 "Failed to enable mclk\n"); 1716 return ret; 1717 } 1718 } 1719 } 1720 1721 break; 1722 case SND_SOC_BIAS_STANDBY: 1723 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) 1724 /* Master bias */ 1725 snd_soc_component_update_bits(component, DA7219_REFERENCES, 1726 DA7219_BIAS_EN_MASK, 1727 DA7219_BIAS_EN_MASK); 1728 1729 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_PREPARE) { 1730 /* Remove MCLK */ 1731 if (da7219->mclk) 1732 clk_disable_unprepare(da7219->mclk); 1733 } 1734 break; 1735 case SND_SOC_BIAS_OFF: 1736 /* Only disable master bias if we're not a wake-up source */ 1737 if (!da7219->wakeup_source) 1738 snd_soc_component_update_bits(component, DA7219_REFERENCES, 1739 DA7219_BIAS_EN_MASK, 0); 1740 1741 break; 1742 } 1743 1744 return 0; 1745 } 1746 1747 static const char *da7219_supply_names[DA7219_NUM_SUPPLIES] = { 1748 [DA7219_SUPPLY_VDD] = "VDD", 1749 [DA7219_SUPPLY_VDDMIC] = "VDDMIC", 1750 [DA7219_SUPPLY_VDDIO] = "VDDIO", 1751 }; 1752 1753 static int da7219_handle_supplies(struct snd_soc_component *component) 1754 { 1755 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 1756 struct regulator *vddio; 1757 u8 io_voltage_lvl = DA7219_IO_VOLTAGE_LEVEL_2_5V_3_6V; 1758 int i, ret; 1759 1760 /* Get required supplies */ 1761 for (i = 0; i < DA7219_NUM_SUPPLIES; ++i) 1762 da7219->supplies[i].supply = da7219_supply_names[i]; 1763 1764 ret = devm_regulator_bulk_get(component->dev, DA7219_NUM_SUPPLIES, 1765 da7219->supplies); 1766 if (ret) { 1767 dev_err(component->dev, "Failed to get supplies"); 1768 return ret; 1769 } 1770 1771 /* Determine VDDIO voltage provided */ 1772 vddio = da7219->supplies[DA7219_SUPPLY_VDDIO].consumer; 1773 ret = regulator_get_voltage(vddio); 1774 if (ret < 1200000) 1775 dev_warn(component->dev, "Invalid VDDIO voltage\n"); 1776 else if (ret < 2800000) 1777 io_voltage_lvl = DA7219_IO_VOLTAGE_LEVEL_1_2V_2_8V; 1778 1779 /* Enable main supplies */ 1780 ret = regulator_bulk_enable(DA7219_NUM_SUPPLIES, da7219->supplies); 1781 if (ret) { 1782 dev_err(component->dev, "Failed to enable supplies"); 1783 return ret; 1784 } 1785 1786 /* Ensure device in active mode */ 1787 snd_soc_component_write(component, DA7219_SYSTEM_ACTIVE, DA7219_SYSTEM_ACTIVE_MASK); 1788 1789 /* Update IO voltage level range */ 1790 snd_soc_component_write(component, DA7219_IO_CTRL, io_voltage_lvl); 1791 1792 return 0; 1793 } 1794 1795 #ifdef CONFIG_COMMON_CLK 1796 static int da7219_dai_clks_prepare(struct clk_hw *hw) 1797 { 1798 struct da7219_priv *da7219 = 1799 container_of(hw, struct da7219_priv, dai_clks_hw); 1800 struct snd_soc_component *component = da7219->component; 1801 1802 snd_soc_component_update_bits(component, DA7219_DAI_CLK_MODE, 1803 DA7219_DAI_CLK_EN_MASK, 1804 DA7219_DAI_CLK_EN_MASK); 1805 1806 return 0; 1807 } 1808 1809 static void da7219_dai_clks_unprepare(struct clk_hw *hw) 1810 { 1811 struct da7219_priv *da7219 = 1812 container_of(hw, struct da7219_priv, dai_clks_hw); 1813 struct snd_soc_component *component = da7219->component; 1814 1815 snd_soc_component_update_bits(component, DA7219_DAI_CLK_MODE, 1816 DA7219_DAI_CLK_EN_MASK, 0); 1817 } 1818 1819 static int da7219_dai_clks_is_prepared(struct clk_hw *hw) 1820 { 1821 struct da7219_priv *da7219 = 1822 container_of(hw, struct da7219_priv, dai_clks_hw); 1823 struct snd_soc_component *component = da7219->component; 1824 u8 clk_reg; 1825 1826 clk_reg = snd_soc_component_read32(component, DA7219_DAI_CLK_MODE); 1827 1828 return !!(clk_reg & DA7219_DAI_CLK_EN_MASK); 1829 } 1830 1831 static unsigned long da7219_dai_clks_recalc_rate(struct clk_hw *hw, 1832 unsigned long parent_rate) 1833 { 1834 struct da7219_priv *da7219 = 1835 container_of(hw, struct da7219_priv, dai_clks_hw); 1836 struct snd_soc_component *component = da7219->component; 1837 u8 fs = snd_soc_component_read32(component, DA7219_SR); 1838 1839 switch (fs & DA7219_SR_MASK) { 1840 case DA7219_SR_8000: 1841 return 8000; 1842 case DA7219_SR_11025: 1843 return 11025; 1844 case DA7219_SR_12000: 1845 return 12000; 1846 case DA7219_SR_16000: 1847 return 16000; 1848 case DA7219_SR_22050: 1849 return 22050; 1850 case DA7219_SR_24000: 1851 return 24000; 1852 case DA7219_SR_32000: 1853 return 32000; 1854 case DA7219_SR_44100: 1855 return 44100; 1856 case DA7219_SR_48000: 1857 return 48000; 1858 case DA7219_SR_88200: 1859 return 88200; 1860 case DA7219_SR_96000: 1861 return 96000; 1862 default: 1863 return 0; 1864 } 1865 } 1866 1867 static const struct clk_ops da7219_dai_clks_ops = { 1868 .prepare = da7219_dai_clks_prepare, 1869 .unprepare = da7219_dai_clks_unprepare, 1870 .is_prepared = da7219_dai_clks_is_prepared, 1871 .recalc_rate = da7219_dai_clks_recalc_rate, 1872 }; 1873 1874 static int da7219_register_dai_clks(struct snd_soc_component *component) 1875 { 1876 struct device *dev = component->dev; 1877 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 1878 struct da7219_pdata *pdata = da7219->pdata; 1879 struct clk_init_data init = {}; 1880 struct clk *dai_clks; 1881 struct clk_lookup *dai_clks_lookup; 1882 const char *parent_name; 1883 1884 if (da7219->mclk) { 1885 parent_name = __clk_get_name(da7219->mclk); 1886 init.parent_names = &parent_name; 1887 init.num_parents = 1; 1888 } else { 1889 init.parent_names = NULL; 1890 init.num_parents = 0; 1891 } 1892 1893 init.name = pdata->dai_clks_name; 1894 init.ops = &da7219_dai_clks_ops; 1895 init.flags = CLK_GET_RATE_NOCACHE; 1896 da7219->dai_clks_hw.init = &init; 1897 1898 dai_clks = devm_clk_register(dev, &da7219->dai_clks_hw); 1899 if (IS_ERR(dai_clks)) { 1900 dev_warn(dev, "Failed to register DAI clocks: %ld\n", 1901 PTR_ERR(dai_clks)); 1902 return PTR_ERR(dai_clks); 1903 } 1904 da7219->dai_clks = dai_clks; 1905 1906 /* If we're using DT, then register as provider accordingly */ 1907 if (dev->of_node) { 1908 devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, 1909 &da7219->dai_clks_hw); 1910 } else { 1911 dai_clks_lookup = clkdev_create(dai_clks, pdata->dai_clks_name, 1912 "%s", dev_name(dev)); 1913 if (!dai_clks_lookup) 1914 return -ENOMEM; 1915 else 1916 da7219->dai_clks_lookup = dai_clks_lookup; 1917 } 1918 1919 return 0; 1920 } 1921 #else 1922 static inline int da7219_register_dai_clks(struct snd_soc_component *component) 1923 { 1924 return 0; 1925 } 1926 #endif /* CONFIG_COMMON_CLK */ 1927 1928 static void da7219_handle_pdata(struct snd_soc_component *component) 1929 { 1930 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 1931 struct da7219_pdata *pdata = da7219->pdata; 1932 1933 if (pdata) { 1934 u8 micbias_lvl = 0; 1935 1936 da7219->wakeup_source = pdata->wakeup_source; 1937 1938 /* Mic Bias voltages */ 1939 switch (pdata->micbias_lvl) { 1940 case DA7219_MICBIAS_1_6V: 1941 case DA7219_MICBIAS_1_8V: 1942 case DA7219_MICBIAS_2_0V: 1943 case DA7219_MICBIAS_2_2V: 1944 case DA7219_MICBIAS_2_4V: 1945 case DA7219_MICBIAS_2_6V: 1946 micbias_lvl |= (pdata->micbias_lvl << 1947 DA7219_MICBIAS1_LEVEL_SHIFT); 1948 break; 1949 } 1950 1951 snd_soc_component_write(component, DA7219_MICBIAS_CTRL, micbias_lvl); 1952 1953 /* 1954 * Calculate delay required to compensate for DC offset in 1955 * Mic PGA, based on Mic Bias voltage. 1956 */ 1957 da7219->mic_pga_delay = DA7219_MIC_PGA_BASE_DELAY + 1958 (pdata->micbias_lvl * 1959 DA7219_MIC_PGA_OFFSET_DELAY); 1960 1961 /* Mic */ 1962 switch (pdata->mic_amp_in_sel) { 1963 case DA7219_MIC_AMP_IN_SEL_DIFF: 1964 case DA7219_MIC_AMP_IN_SEL_SE_P: 1965 case DA7219_MIC_AMP_IN_SEL_SE_N: 1966 snd_soc_component_write(component, DA7219_MIC_1_SELECT, 1967 pdata->mic_amp_in_sel); 1968 break; 1969 } 1970 } 1971 } 1972 1973 static struct reg_sequence da7219_rev_aa_patch[] = { 1974 { DA7219_REFERENCES, 0x08 }, 1975 }; 1976 1977 static int da7219_probe(struct snd_soc_component *component) 1978 { 1979 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 1980 unsigned int rev; 1981 int ret; 1982 1983 da7219->component = component; 1984 mutex_init(&da7219->ctrl_lock); 1985 mutex_init(&da7219->pll_lock); 1986 1987 /* Regulator configuration */ 1988 ret = da7219_handle_supplies(component); 1989 if (ret) 1990 return ret; 1991 1992 ret = regmap_read(da7219->regmap, DA7219_CHIP_REVISION, &rev); 1993 if (ret) { 1994 dev_err(component->dev, "Failed to read chip revision: %d\n", ret); 1995 goto err_disable_reg; 1996 } 1997 1998 switch (rev & DA7219_CHIP_MINOR_MASK) { 1999 case 0: 2000 ret = regmap_register_patch(da7219->regmap, da7219_rev_aa_patch, 2001 ARRAY_SIZE(da7219_rev_aa_patch)); 2002 if (ret) { 2003 dev_err(component->dev, "Failed to register AA patch: %d\n", 2004 ret); 2005 goto err_disable_reg; 2006 } 2007 break; 2008 default: 2009 break; 2010 } 2011 2012 /* Handle DT/ACPI/Platform data */ 2013 da7219->pdata = dev_get_platdata(component->dev); 2014 if (!da7219->pdata) 2015 da7219->pdata = da7219_fw_to_pdata(component); 2016 2017 da7219_handle_pdata(component); 2018 2019 /* Check if MCLK provided */ 2020 da7219->mclk = devm_clk_get(component->dev, "mclk"); 2021 if (IS_ERR(da7219->mclk)) { 2022 if (PTR_ERR(da7219->mclk) != -ENOENT) { 2023 ret = PTR_ERR(da7219->mclk); 2024 goto err_disable_reg; 2025 } else { 2026 da7219->mclk = NULL; 2027 } 2028 } 2029 2030 /* Register CCF DAI clock control */ 2031 ret = da7219_register_dai_clks(component); 2032 if (ret) 2033 return ret; 2034 2035 /* Default PC counter to free-running */ 2036 snd_soc_component_update_bits(component, DA7219_PC_COUNT, DA7219_PC_FREERUN_MASK, 2037 DA7219_PC_FREERUN_MASK); 2038 2039 /* Default gain ramping */ 2040 snd_soc_component_update_bits(component, DA7219_MIXIN_L_CTRL, 2041 DA7219_MIXIN_L_AMP_RAMP_EN_MASK, 2042 DA7219_MIXIN_L_AMP_RAMP_EN_MASK); 2043 snd_soc_component_update_bits(component, DA7219_ADC_L_CTRL, DA7219_ADC_L_RAMP_EN_MASK, 2044 DA7219_ADC_L_RAMP_EN_MASK); 2045 snd_soc_component_update_bits(component, DA7219_DAC_L_CTRL, DA7219_DAC_L_RAMP_EN_MASK, 2046 DA7219_DAC_L_RAMP_EN_MASK); 2047 snd_soc_component_update_bits(component, DA7219_DAC_R_CTRL, DA7219_DAC_R_RAMP_EN_MASK, 2048 DA7219_DAC_R_RAMP_EN_MASK); 2049 snd_soc_component_update_bits(component, DA7219_HP_L_CTRL, 2050 DA7219_HP_L_AMP_RAMP_EN_MASK, 2051 DA7219_HP_L_AMP_RAMP_EN_MASK); 2052 snd_soc_component_update_bits(component, DA7219_HP_R_CTRL, 2053 DA7219_HP_R_AMP_RAMP_EN_MASK, 2054 DA7219_HP_R_AMP_RAMP_EN_MASK); 2055 2056 /* Default minimum gain on HP to avoid pops during DAPM sequencing */ 2057 snd_soc_component_update_bits(component, DA7219_HP_L_CTRL, 2058 DA7219_HP_L_AMP_MIN_GAIN_EN_MASK, 2059 DA7219_HP_L_AMP_MIN_GAIN_EN_MASK); 2060 snd_soc_component_update_bits(component, DA7219_HP_R_CTRL, 2061 DA7219_HP_R_AMP_MIN_GAIN_EN_MASK, 2062 DA7219_HP_R_AMP_MIN_GAIN_EN_MASK); 2063 2064 /* Default infinite tone gen, start/stop by Kcontrol */ 2065 snd_soc_component_write(component, DA7219_TONE_GEN_CYCLES, DA7219_BEEP_CYCLES_MASK); 2066 2067 /* Initialise AAD block */ 2068 ret = da7219_aad_init(component); 2069 if (ret) 2070 goto err_disable_reg; 2071 2072 return 0; 2073 2074 err_disable_reg: 2075 regulator_bulk_disable(DA7219_NUM_SUPPLIES, da7219->supplies); 2076 2077 return ret; 2078 } 2079 2080 static void da7219_remove(struct snd_soc_component *component) 2081 { 2082 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 2083 2084 da7219_aad_exit(component); 2085 2086 #ifdef CONFIG_COMMON_CLK 2087 if (da7219->dai_clks_lookup) 2088 clkdev_drop(da7219->dai_clks_lookup); 2089 #endif 2090 2091 /* Supplies */ 2092 regulator_bulk_disable(DA7219_NUM_SUPPLIES, da7219->supplies); 2093 } 2094 2095 #ifdef CONFIG_PM 2096 static int da7219_suspend(struct snd_soc_component *component) 2097 { 2098 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 2099 2100 /* Suspend AAD if we're not a wake-up source */ 2101 if (!da7219->wakeup_source) 2102 da7219_aad_suspend(component); 2103 2104 snd_soc_component_force_bias_level(component, SND_SOC_BIAS_OFF); 2105 2106 return 0; 2107 } 2108 2109 static int da7219_resume(struct snd_soc_component *component) 2110 { 2111 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 2112 2113 snd_soc_component_force_bias_level(component, SND_SOC_BIAS_STANDBY); 2114 2115 /* Resume AAD if previously suspended */ 2116 if (!da7219->wakeup_source) 2117 da7219_aad_resume(component); 2118 2119 return 0; 2120 } 2121 #else 2122 #define da7219_suspend NULL 2123 #define da7219_resume NULL 2124 #endif 2125 2126 static const struct snd_soc_component_driver soc_component_dev_da7219 = { 2127 .probe = da7219_probe, 2128 .remove = da7219_remove, 2129 .suspend = da7219_suspend, 2130 .resume = da7219_resume, 2131 .set_bias_level = da7219_set_bias_level, 2132 .controls = da7219_snd_controls, 2133 .num_controls = ARRAY_SIZE(da7219_snd_controls), 2134 .dapm_widgets = da7219_dapm_widgets, 2135 .num_dapm_widgets = ARRAY_SIZE(da7219_dapm_widgets), 2136 .dapm_routes = da7219_audio_map, 2137 .num_dapm_routes = ARRAY_SIZE(da7219_audio_map), 2138 .idle_bias_on = 1, 2139 .use_pmdown_time = 1, 2140 .endianness = 1, 2141 .non_legacy_dai_naming = 1, 2142 }; 2143 2144 2145 /* 2146 * Regmap configs 2147 */ 2148 2149 static struct reg_default da7219_reg_defaults[] = { 2150 { DA7219_MIC_1_SELECT, 0x00 }, 2151 { DA7219_CIF_TIMEOUT_CTRL, 0x01 }, 2152 { DA7219_SR_24_48, 0x00 }, 2153 { DA7219_SR, 0x0A }, 2154 { DA7219_CIF_I2C_ADDR_CFG, 0x02 }, 2155 { DA7219_PLL_CTRL, 0x10 }, 2156 { DA7219_PLL_FRAC_TOP, 0x00 }, 2157 { DA7219_PLL_FRAC_BOT, 0x00 }, 2158 { DA7219_PLL_INTEGER, 0x20 }, 2159 { DA7219_DIG_ROUTING_DAI, 0x10 }, 2160 { DA7219_DAI_CLK_MODE, 0x01 }, 2161 { DA7219_DAI_CTRL, 0x28 }, 2162 { DA7219_DAI_TDM_CTRL, 0x40 }, 2163 { DA7219_DIG_ROUTING_DAC, 0x32 }, 2164 { DA7219_DAI_OFFSET_LOWER, 0x00 }, 2165 { DA7219_DAI_OFFSET_UPPER, 0x00 }, 2166 { DA7219_REFERENCES, 0x08 }, 2167 { DA7219_MIXIN_L_SELECT, 0x00 }, 2168 { DA7219_MIXIN_L_GAIN, 0x03 }, 2169 { DA7219_ADC_L_GAIN, 0x6F }, 2170 { DA7219_ADC_FILTERS1, 0x80 }, 2171 { DA7219_MIC_1_GAIN, 0x01 }, 2172 { DA7219_SIDETONE_CTRL, 0x40 }, 2173 { DA7219_SIDETONE_GAIN, 0x0E }, 2174 { DA7219_DROUTING_ST_OUTFILT_1L, 0x01 }, 2175 { DA7219_DROUTING_ST_OUTFILT_1R, 0x02 }, 2176 { DA7219_DAC_FILTERS5, 0x00 }, 2177 { DA7219_DAC_FILTERS2, 0x88 }, 2178 { DA7219_DAC_FILTERS3, 0x88 }, 2179 { DA7219_DAC_FILTERS4, 0x08 }, 2180 { DA7219_DAC_FILTERS1, 0x80 }, 2181 { DA7219_DAC_L_GAIN, 0x6F }, 2182 { DA7219_DAC_R_GAIN, 0x6F }, 2183 { DA7219_CP_CTRL, 0x20 }, 2184 { DA7219_HP_L_GAIN, 0x39 }, 2185 { DA7219_HP_R_GAIN, 0x39 }, 2186 { DA7219_MIXOUT_L_SELECT, 0x00 }, 2187 { DA7219_MIXOUT_R_SELECT, 0x00 }, 2188 { DA7219_MICBIAS_CTRL, 0x03 }, 2189 { DA7219_MIC_1_CTRL, 0x40 }, 2190 { DA7219_MIXIN_L_CTRL, 0x40 }, 2191 { DA7219_ADC_L_CTRL, 0x40 }, 2192 { DA7219_DAC_L_CTRL, 0x40 }, 2193 { DA7219_DAC_R_CTRL, 0x40 }, 2194 { DA7219_HP_L_CTRL, 0x40 }, 2195 { DA7219_HP_R_CTRL, 0x40 }, 2196 { DA7219_MIXOUT_L_CTRL, 0x10 }, 2197 { DA7219_MIXOUT_R_CTRL, 0x10 }, 2198 { DA7219_CHIP_ID1, 0x23 }, 2199 { DA7219_CHIP_ID2, 0x93 }, 2200 { DA7219_IO_CTRL, 0x00 }, 2201 { DA7219_GAIN_RAMP_CTRL, 0x00 }, 2202 { DA7219_PC_COUNT, 0x02 }, 2203 { DA7219_CP_VOL_THRESHOLD1, 0x0E }, 2204 { DA7219_DIG_CTRL, 0x00 }, 2205 { DA7219_ALC_CTRL2, 0x00 }, 2206 { DA7219_ALC_CTRL3, 0x00 }, 2207 { DA7219_ALC_NOISE, 0x3F }, 2208 { DA7219_ALC_TARGET_MIN, 0x3F }, 2209 { DA7219_ALC_TARGET_MAX, 0x00 }, 2210 { DA7219_ALC_GAIN_LIMITS, 0xFF }, 2211 { DA7219_ALC_ANA_GAIN_LIMITS, 0x71 }, 2212 { DA7219_ALC_ANTICLIP_CTRL, 0x00 }, 2213 { DA7219_ALC_ANTICLIP_LEVEL, 0x00 }, 2214 { DA7219_DAC_NG_SETUP_TIME, 0x00 }, 2215 { DA7219_DAC_NG_OFF_THRESH, 0x00 }, 2216 { DA7219_DAC_NG_ON_THRESH, 0x00 }, 2217 { DA7219_DAC_NG_CTRL, 0x00 }, 2218 { DA7219_TONE_GEN_CFG1, 0x00 }, 2219 { DA7219_TONE_GEN_CFG2, 0x00 }, 2220 { DA7219_TONE_GEN_CYCLES, 0x00 }, 2221 { DA7219_TONE_GEN_FREQ1_L, 0x55 }, 2222 { DA7219_TONE_GEN_FREQ1_U, 0x15 }, 2223 { DA7219_TONE_GEN_FREQ2_L, 0x00 }, 2224 { DA7219_TONE_GEN_FREQ2_U, 0x40 }, 2225 { DA7219_TONE_GEN_ON_PER, 0x02 }, 2226 { DA7219_TONE_GEN_OFF_PER, 0x01 }, 2227 { DA7219_ACCDET_IRQ_MASK_A, 0x00 }, 2228 { DA7219_ACCDET_IRQ_MASK_B, 0x00 }, 2229 { DA7219_ACCDET_CONFIG_1, 0xD6 }, 2230 { DA7219_ACCDET_CONFIG_2, 0x34 }, 2231 { DA7219_ACCDET_CONFIG_3, 0x0A }, 2232 { DA7219_ACCDET_CONFIG_4, 0x16 }, 2233 { DA7219_ACCDET_CONFIG_5, 0x21 }, 2234 { DA7219_ACCDET_CONFIG_6, 0x3E }, 2235 { DA7219_ACCDET_CONFIG_7, 0x01 }, 2236 { DA7219_SYSTEM_ACTIVE, 0x00 }, 2237 }; 2238 2239 static bool da7219_volatile_register(struct device *dev, unsigned int reg) 2240 { 2241 switch (reg) { 2242 case DA7219_MIC_1_GAIN_STATUS: 2243 case DA7219_MIXIN_L_GAIN_STATUS: 2244 case DA7219_ADC_L_GAIN_STATUS: 2245 case DA7219_DAC_L_GAIN_STATUS: 2246 case DA7219_DAC_R_GAIN_STATUS: 2247 case DA7219_HP_L_GAIN_STATUS: 2248 case DA7219_HP_R_GAIN_STATUS: 2249 case DA7219_CIF_CTRL: 2250 case DA7219_PLL_SRM_STS: 2251 case DA7219_ALC_CTRL1: 2252 case DA7219_SYSTEM_MODES_INPUT: 2253 case DA7219_SYSTEM_MODES_OUTPUT: 2254 case DA7219_ALC_OFFSET_AUTO_M_L: 2255 case DA7219_ALC_OFFSET_AUTO_U_L: 2256 case DA7219_TONE_GEN_CFG1: 2257 case DA7219_ACCDET_STATUS_A: 2258 case DA7219_ACCDET_STATUS_B: 2259 case DA7219_ACCDET_IRQ_EVENT_A: 2260 case DA7219_ACCDET_IRQ_EVENT_B: 2261 case DA7219_ACCDET_CONFIG_8: 2262 case DA7219_SYSTEM_STATUS: 2263 return true; 2264 default: 2265 return false; 2266 } 2267 } 2268 2269 static const struct regmap_config da7219_regmap_config = { 2270 .reg_bits = 8, 2271 .val_bits = 8, 2272 2273 .max_register = DA7219_SYSTEM_ACTIVE, 2274 .reg_defaults = da7219_reg_defaults, 2275 .num_reg_defaults = ARRAY_SIZE(da7219_reg_defaults), 2276 .volatile_reg = da7219_volatile_register, 2277 .cache_type = REGCACHE_RBTREE, 2278 }; 2279 2280 2281 /* 2282 * I2C layer 2283 */ 2284 2285 static int da7219_i2c_probe(struct i2c_client *i2c, 2286 const struct i2c_device_id *id) 2287 { 2288 struct da7219_priv *da7219; 2289 unsigned int system_active, system_status; 2290 int i, ret; 2291 2292 da7219 = devm_kzalloc(&i2c->dev, sizeof(struct da7219_priv), 2293 GFP_KERNEL); 2294 if (!da7219) 2295 return -ENOMEM; 2296 2297 i2c_set_clientdata(i2c, da7219); 2298 2299 da7219->regmap = devm_regmap_init_i2c(i2c, &da7219_regmap_config); 2300 if (IS_ERR(da7219->regmap)) { 2301 ret = PTR_ERR(da7219->regmap); 2302 dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret); 2303 return ret; 2304 } 2305 2306 regcache_cache_bypass(da7219->regmap, true); 2307 2308 /* Disable audio paths if still active from previous start */ 2309 regmap_read(da7219->regmap, DA7219_SYSTEM_ACTIVE, &system_active); 2310 if (system_active) { 2311 regmap_write(da7219->regmap, DA7219_GAIN_RAMP_CTRL, 2312 DA7219_GAIN_RAMP_RATE_NOMINAL); 2313 regmap_write(da7219->regmap, DA7219_SYSTEM_MODES_INPUT, 0x00); 2314 regmap_write(da7219->regmap, DA7219_SYSTEM_MODES_OUTPUT, 0x01); 2315 2316 for (i = 0; i < DA7219_SYS_STAT_CHECK_RETRIES; ++i) { 2317 regmap_read(da7219->regmap, DA7219_SYSTEM_STATUS, 2318 &system_status); 2319 if (!system_status) 2320 break; 2321 2322 msleep(DA7219_SYS_STAT_CHECK_DELAY); 2323 } 2324 } 2325 2326 /* Soft reset component */ 2327 regmap_write_bits(da7219->regmap, DA7219_ACCDET_CONFIG_1, 2328 DA7219_ACCDET_EN_MASK, 0); 2329 regmap_write_bits(da7219->regmap, DA7219_CIF_CTRL, 2330 DA7219_CIF_REG_SOFT_RESET_MASK, 2331 DA7219_CIF_REG_SOFT_RESET_MASK); 2332 regmap_write_bits(da7219->regmap, DA7219_SYSTEM_ACTIVE, 2333 DA7219_SYSTEM_ACTIVE_MASK, 0); 2334 2335 regcache_cache_bypass(da7219->regmap, false); 2336 2337 ret = devm_snd_soc_register_component(&i2c->dev, 2338 &soc_component_dev_da7219, 2339 &da7219_dai, 1); 2340 if (ret < 0) { 2341 dev_err(&i2c->dev, "Failed to register da7219 component: %d\n", 2342 ret); 2343 } 2344 return ret; 2345 } 2346 2347 static int da7219_i2c_remove(struct i2c_client *client) 2348 { 2349 return 0; 2350 } 2351 2352 static const struct i2c_device_id da7219_i2c_id[] = { 2353 { "da7219", }, 2354 { } 2355 }; 2356 MODULE_DEVICE_TABLE(i2c, da7219_i2c_id); 2357 2358 static struct i2c_driver da7219_i2c_driver = { 2359 .driver = { 2360 .name = "da7219", 2361 .of_match_table = of_match_ptr(da7219_of_match), 2362 .acpi_match_table = ACPI_PTR(da7219_acpi_match), 2363 }, 2364 .probe = da7219_i2c_probe, 2365 .remove = da7219_i2c_remove, 2366 .id_table = da7219_i2c_id, 2367 }; 2368 2369 module_i2c_driver(da7219_i2c_driver); 2370 2371 MODULE_DESCRIPTION("ASoC DA7219 Codec Driver"); 2372 MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>"); 2373 MODULE_LICENSE("GPL"); 2374