1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * da7219-aad.c - Dialog DA7219 ALSA SoC AAD Driver 4 * 5 * Copyright (c) 2015 Dialog Semiconductor Ltd. 6 * 7 * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com> 8 */ 9 10 #include <linux/module.h> 11 #include <linux/platform_device.h> 12 #include <linux/clk.h> 13 #include <linux/i2c.h> 14 #include <linux/property.h> 15 #include <linux/pm_wakeirq.h> 16 #include <linux/slab.h> 17 #include <linux/delay.h> 18 #include <linux/workqueue.h> 19 #include <sound/soc.h> 20 #include <sound/jack.h> 21 #include <sound/da7219.h> 22 23 #include "da7219.h" 24 #include "da7219-aad.h" 25 26 27 /* 28 * Detection control 29 */ 30 31 void da7219_aad_jack_det(struct snd_soc_component *component, struct snd_soc_jack *jack) 32 { 33 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 34 35 da7219->aad->jack = jack; 36 da7219->aad->jack_inserted = false; 37 38 /* Send an initial empty report */ 39 snd_soc_jack_report(jack, 0, DA7219_AAD_REPORT_ALL_MASK); 40 41 /* Enable/Disable jack detection */ 42 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1, 43 DA7219_ACCDET_EN_MASK, 44 (jack ? DA7219_ACCDET_EN_MASK : 0)); 45 } 46 47 /* 48 * Button/HPTest work 49 */ 50 51 static void da7219_aad_btn_det_work(struct work_struct *work) 52 { 53 struct da7219_aad_priv *da7219_aad = 54 container_of(work, struct da7219_aad_priv, btn_det_work); 55 struct snd_soc_component *component = da7219_aad->component; 56 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 57 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 58 u8 statusa, micbias_ctrl; 59 bool micbias_up = false; 60 int retries = 0; 61 62 /* Disable ground switch */ 63 snd_soc_component_update_bits(component, 0xFB, 0x01, 0x00); 64 65 /* Drive headphones/lineout */ 66 snd_soc_component_update_bits(component, DA7219_HP_L_CTRL, 67 DA7219_HP_L_AMP_OE_MASK, 68 DA7219_HP_L_AMP_OE_MASK); 69 snd_soc_component_update_bits(component, DA7219_HP_R_CTRL, 70 DA7219_HP_R_AMP_OE_MASK, 71 DA7219_HP_R_AMP_OE_MASK); 72 73 /* Make sure mic bias is up */ 74 snd_soc_dapm_force_enable_pin(dapm, "Mic Bias"); 75 snd_soc_dapm_sync(dapm); 76 77 do { 78 statusa = snd_soc_component_read(component, DA7219_ACCDET_STATUS_A); 79 if (statusa & DA7219_MICBIAS_UP_STS_MASK) 80 micbias_up = true; 81 else if (retries++ < DA7219_AAD_MICBIAS_CHK_RETRIES) 82 msleep(DA7219_AAD_MICBIAS_CHK_DELAY); 83 } while ((!micbias_up) && (retries < DA7219_AAD_MICBIAS_CHK_RETRIES)); 84 85 if (retries >= DA7219_AAD_MICBIAS_CHK_RETRIES) 86 dev_warn(component->dev, "Mic bias status check timed out"); 87 88 da7219->micbias_on_event = true; 89 90 /* 91 * Mic bias pulse required to enable mic, must be done before enabling 92 * button detection to prevent erroneous button readings. 93 */ 94 if (da7219_aad->micbias_pulse_lvl && da7219_aad->micbias_pulse_time) { 95 /* Pulse higher level voltage */ 96 micbias_ctrl = snd_soc_component_read(component, DA7219_MICBIAS_CTRL); 97 snd_soc_component_update_bits(component, DA7219_MICBIAS_CTRL, 98 DA7219_MICBIAS1_LEVEL_MASK, 99 da7219_aad->micbias_pulse_lvl); 100 msleep(da7219_aad->micbias_pulse_time); 101 snd_soc_component_write(component, DA7219_MICBIAS_CTRL, micbias_ctrl); 102 103 } 104 105 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1, 106 DA7219_BUTTON_CONFIG_MASK, 107 da7219_aad->btn_cfg); 108 } 109 110 static void da7219_aad_hptest_work(struct work_struct *work) 111 { 112 struct da7219_aad_priv *da7219_aad = 113 container_of(work, struct da7219_aad_priv, hptest_work); 114 struct snd_soc_component *component = da7219_aad->component; 115 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 116 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 117 118 __le16 tonegen_freq_hptest; 119 u8 pll_srm_sts, pll_ctrl, gain_ramp_ctrl, accdet_cfg8; 120 int report = 0, ret; 121 122 /* Lock DAPM, Kcontrols affected by this test and the PLL */ 123 snd_soc_dapm_mutex_lock(dapm); 124 mutex_lock(&da7219->ctrl_lock); 125 mutex_lock(&da7219->pll_lock); 126 127 /* Ensure MCLK is available for HP test procedure */ 128 if (da7219->mclk) { 129 ret = clk_prepare_enable(da7219->mclk); 130 if (ret) { 131 dev_err(component->dev, "Failed to enable mclk - %d\n", ret); 132 mutex_unlock(&da7219->pll_lock); 133 mutex_unlock(&da7219->ctrl_lock); 134 snd_soc_dapm_mutex_unlock(dapm); 135 return; 136 } 137 } 138 139 /* 140 * If MCLK not present, then we're using the internal oscillator and 141 * require different frequency settings to achieve the same result. 142 * 143 * If MCLK is present, but PLL is not enabled then we enable it here to 144 * ensure a consistent detection procedure. 145 */ 146 pll_srm_sts = snd_soc_component_read(component, DA7219_PLL_SRM_STS); 147 if (pll_srm_sts & DA7219_PLL_SRM_STS_MCLK) { 148 tonegen_freq_hptest = cpu_to_le16(DA7219_AAD_HPTEST_RAMP_FREQ); 149 150 pll_ctrl = snd_soc_component_read(component, DA7219_PLL_CTRL); 151 if ((pll_ctrl & DA7219_PLL_MODE_MASK) == DA7219_PLL_MODE_BYPASS) 152 da7219_set_pll(component, DA7219_SYSCLK_PLL, 153 DA7219_PLL_FREQ_OUT_98304); 154 } else { 155 tonegen_freq_hptest = cpu_to_le16(DA7219_AAD_HPTEST_RAMP_FREQ_INT_OSC); 156 } 157 158 /* Disable ground switch */ 159 snd_soc_component_update_bits(component, 0xFB, 0x01, 0x00); 160 161 /* Ensure gain ramping at fastest rate */ 162 gain_ramp_ctrl = snd_soc_component_read(component, DA7219_GAIN_RAMP_CTRL); 163 snd_soc_component_write(component, DA7219_GAIN_RAMP_CTRL, DA7219_GAIN_RAMP_RATE_X8); 164 165 /* Bypass cache so it saves current settings */ 166 regcache_cache_bypass(da7219->regmap, true); 167 168 /* Make sure Tone Generator is disabled */ 169 snd_soc_component_write(component, DA7219_TONE_GEN_CFG1, 0); 170 171 /* Enable HPTest block, 1KOhms check */ 172 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_8, 173 DA7219_HPTEST_EN_MASK | DA7219_HPTEST_RES_SEL_MASK, 174 DA7219_HPTEST_EN_MASK | 175 DA7219_HPTEST_RES_SEL_1KOHMS); 176 177 /* Set gains to 0db */ 178 snd_soc_component_write(component, DA7219_DAC_L_GAIN, DA7219_DAC_DIGITAL_GAIN_0DB); 179 snd_soc_component_write(component, DA7219_DAC_R_GAIN, DA7219_DAC_DIGITAL_GAIN_0DB); 180 snd_soc_component_write(component, DA7219_HP_L_GAIN, DA7219_HP_AMP_GAIN_0DB); 181 snd_soc_component_write(component, DA7219_HP_R_GAIN, DA7219_HP_AMP_GAIN_0DB); 182 183 /* Disable DAC filters, EQs and soft mute */ 184 snd_soc_component_update_bits(component, DA7219_DAC_FILTERS1, DA7219_HPF_MODE_MASK, 185 0); 186 snd_soc_component_update_bits(component, DA7219_DAC_FILTERS4, DA7219_DAC_EQ_EN_MASK, 187 0); 188 snd_soc_component_update_bits(component, DA7219_DAC_FILTERS5, 189 DA7219_DAC_SOFTMUTE_EN_MASK, 0); 190 191 /* Enable HP left & right paths */ 192 snd_soc_component_update_bits(component, DA7219_CP_CTRL, DA7219_CP_EN_MASK, 193 DA7219_CP_EN_MASK); 194 snd_soc_component_update_bits(component, DA7219_DIG_ROUTING_DAC, 195 DA7219_DAC_L_SRC_MASK | DA7219_DAC_R_SRC_MASK, 196 DA7219_DAC_L_SRC_TONEGEN | 197 DA7219_DAC_R_SRC_TONEGEN); 198 snd_soc_component_update_bits(component, DA7219_DAC_L_CTRL, 199 DA7219_DAC_L_EN_MASK | DA7219_DAC_L_MUTE_EN_MASK, 200 DA7219_DAC_L_EN_MASK); 201 snd_soc_component_update_bits(component, DA7219_DAC_R_CTRL, 202 DA7219_DAC_R_EN_MASK | DA7219_DAC_R_MUTE_EN_MASK, 203 DA7219_DAC_R_EN_MASK); 204 snd_soc_component_update_bits(component, DA7219_MIXOUT_L_SELECT, 205 DA7219_MIXOUT_L_MIX_SELECT_MASK, 206 DA7219_MIXOUT_L_MIX_SELECT_MASK); 207 snd_soc_component_update_bits(component, DA7219_MIXOUT_R_SELECT, 208 DA7219_MIXOUT_R_MIX_SELECT_MASK, 209 DA7219_MIXOUT_R_MIX_SELECT_MASK); 210 snd_soc_component_update_bits(component, DA7219_DROUTING_ST_OUTFILT_1L, 211 DA7219_OUTFILT_ST_1L_SRC_MASK, 212 DA7219_DMIX_ST_SRC_OUTFILT1L); 213 snd_soc_component_update_bits(component, DA7219_DROUTING_ST_OUTFILT_1R, 214 DA7219_OUTFILT_ST_1R_SRC_MASK, 215 DA7219_DMIX_ST_SRC_OUTFILT1R); 216 snd_soc_component_update_bits(component, DA7219_MIXOUT_L_CTRL, 217 DA7219_MIXOUT_L_AMP_EN_MASK, 218 DA7219_MIXOUT_L_AMP_EN_MASK); 219 snd_soc_component_update_bits(component, DA7219_MIXOUT_R_CTRL, 220 DA7219_MIXOUT_R_AMP_EN_MASK, 221 DA7219_MIXOUT_R_AMP_EN_MASK); 222 snd_soc_component_update_bits(component, DA7219_HP_L_CTRL, 223 DA7219_HP_L_AMP_OE_MASK | DA7219_HP_L_AMP_EN_MASK, 224 DA7219_HP_L_AMP_OE_MASK | DA7219_HP_L_AMP_EN_MASK); 225 snd_soc_component_update_bits(component, DA7219_HP_R_CTRL, 226 DA7219_HP_R_AMP_OE_MASK | DA7219_HP_R_AMP_EN_MASK, 227 DA7219_HP_R_AMP_OE_MASK | DA7219_HP_R_AMP_EN_MASK); 228 msleep(DA7219_SETTLING_DELAY); 229 snd_soc_component_update_bits(component, DA7219_HP_L_CTRL, 230 DA7219_HP_L_AMP_MUTE_EN_MASK | 231 DA7219_HP_L_AMP_MIN_GAIN_EN_MASK, 0); 232 snd_soc_component_update_bits(component, DA7219_HP_R_CTRL, 233 DA7219_HP_R_AMP_MUTE_EN_MASK | 234 DA7219_HP_R_AMP_MIN_GAIN_EN_MASK, 0); 235 236 /* 237 * If we're running from the internal oscillator then give audio paths 238 * time to settle before running test. 239 */ 240 if (!(pll_srm_sts & DA7219_PLL_SRM_STS_MCLK)) 241 msleep(DA7219_AAD_HPTEST_INT_OSC_PATH_DELAY); 242 243 /* Configure & start Tone Generator */ 244 snd_soc_component_write(component, DA7219_TONE_GEN_ON_PER, DA7219_BEEP_ON_PER_MASK); 245 regmap_raw_write(da7219->regmap, DA7219_TONE_GEN_FREQ1_L, 246 &tonegen_freq_hptest, sizeof(tonegen_freq_hptest)); 247 snd_soc_component_update_bits(component, DA7219_TONE_GEN_CFG2, 248 DA7219_SWG_SEL_MASK | DA7219_TONE_GEN_GAIN_MASK, 249 DA7219_SWG_SEL_SRAMP | 250 DA7219_TONE_GEN_GAIN_MINUS_15DB); 251 snd_soc_component_write(component, DA7219_TONE_GEN_CFG1, DA7219_START_STOPN_MASK); 252 253 msleep(DA7219_AAD_HPTEST_PERIOD); 254 255 /* Grab comparator reading */ 256 accdet_cfg8 = snd_soc_component_read(component, DA7219_ACCDET_CONFIG_8); 257 if (accdet_cfg8 & DA7219_HPTEST_COMP_MASK) 258 report |= SND_JACK_HEADPHONE; 259 else 260 report |= SND_JACK_LINEOUT; 261 262 /* Stop tone generator */ 263 snd_soc_component_write(component, DA7219_TONE_GEN_CFG1, 0); 264 265 msleep(DA7219_AAD_HPTEST_PERIOD); 266 267 /* Restore original settings from cache */ 268 regcache_mark_dirty(da7219->regmap); 269 regcache_sync_region(da7219->regmap, DA7219_HP_L_CTRL, 270 DA7219_HP_R_CTRL); 271 msleep(DA7219_SETTLING_DELAY); 272 regcache_sync_region(da7219->regmap, DA7219_MIXOUT_L_CTRL, 273 DA7219_MIXOUT_R_CTRL); 274 regcache_sync_region(da7219->regmap, DA7219_DROUTING_ST_OUTFILT_1L, 275 DA7219_DROUTING_ST_OUTFILT_1R); 276 regcache_sync_region(da7219->regmap, DA7219_MIXOUT_L_SELECT, 277 DA7219_MIXOUT_R_SELECT); 278 regcache_sync_region(da7219->regmap, DA7219_DAC_L_CTRL, 279 DA7219_DAC_R_CTRL); 280 regcache_sync_region(da7219->regmap, DA7219_DIG_ROUTING_DAC, 281 DA7219_DIG_ROUTING_DAC); 282 regcache_sync_region(da7219->regmap, DA7219_CP_CTRL, DA7219_CP_CTRL); 283 regcache_sync_region(da7219->regmap, DA7219_DAC_FILTERS5, 284 DA7219_DAC_FILTERS5); 285 regcache_sync_region(da7219->regmap, DA7219_DAC_FILTERS4, 286 DA7219_DAC_FILTERS1); 287 regcache_sync_region(da7219->regmap, DA7219_HP_L_GAIN, 288 DA7219_HP_R_GAIN); 289 regcache_sync_region(da7219->regmap, DA7219_DAC_L_GAIN, 290 DA7219_DAC_R_GAIN); 291 regcache_sync_region(da7219->regmap, DA7219_TONE_GEN_ON_PER, 292 DA7219_TONE_GEN_ON_PER); 293 regcache_sync_region(da7219->regmap, DA7219_TONE_GEN_FREQ1_L, 294 DA7219_TONE_GEN_FREQ1_U); 295 regcache_sync_region(da7219->regmap, DA7219_TONE_GEN_CFG1, 296 DA7219_TONE_GEN_CFG2); 297 298 regcache_cache_bypass(da7219->regmap, false); 299 300 /* Disable HPTest block */ 301 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_8, 302 DA7219_HPTEST_EN_MASK, 0); 303 304 /* 305 * If we're running from the internal oscillator then give audio paths 306 * time to settle before allowing headphones to be driven as required. 307 */ 308 if (!(pll_srm_sts & DA7219_PLL_SRM_STS_MCLK)) 309 msleep(DA7219_AAD_HPTEST_INT_OSC_PATH_DELAY); 310 311 /* Restore gain ramping rate */ 312 snd_soc_component_write(component, DA7219_GAIN_RAMP_CTRL, gain_ramp_ctrl); 313 314 /* Drive Headphones/lineout */ 315 snd_soc_component_update_bits(component, DA7219_HP_L_CTRL, DA7219_HP_L_AMP_OE_MASK, 316 DA7219_HP_L_AMP_OE_MASK); 317 snd_soc_component_update_bits(component, DA7219_HP_R_CTRL, DA7219_HP_R_AMP_OE_MASK, 318 DA7219_HP_R_AMP_OE_MASK); 319 320 /* Restore PLL to previous configuration, if re-configured */ 321 if ((pll_srm_sts & DA7219_PLL_SRM_STS_MCLK) && 322 ((pll_ctrl & DA7219_PLL_MODE_MASK) == DA7219_PLL_MODE_BYPASS)) 323 da7219_set_pll(component, DA7219_SYSCLK_MCLK, 0); 324 325 /* Remove MCLK, if previously enabled */ 326 if (da7219->mclk) 327 clk_disable_unprepare(da7219->mclk); 328 329 mutex_unlock(&da7219->pll_lock); 330 mutex_unlock(&da7219->ctrl_lock); 331 snd_soc_dapm_mutex_unlock(dapm); 332 333 /* 334 * Only send report if jack hasn't been removed during process, 335 * otherwise it's invalid and we drop it. 336 */ 337 if (da7219_aad->jack_inserted) 338 snd_soc_jack_report(da7219_aad->jack, report, 339 SND_JACK_HEADSET | SND_JACK_LINEOUT); 340 } 341 342 343 /* 344 * IRQ 345 */ 346 347 static irqreturn_t da7219_aad_irq_thread(int irq, void *data) 348 { 349 struct da7219_aad_priv *da7219_aad = data; 350 struct snd_soc_component *component = da7219_aad->component; 351 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 352 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 353 u8 events[DA7219_AAD_IRQ_REG_MAX]; 354 u8 statusa, srm_st; 355 int i, report = 0, mask = 0; 356 357 srm_st = snd_soc_component_read(component, DA7219_PLL_SRM_STS) & DA7219_PLL_SRM_STS_MCLK; 358 msleep(da7219_aad->gnd_switch_delay * ((srm_st == 0x0) ? 2 : 1) - 4); 359 /* Enable ground switch */ 360 snd_soc_component_update_bits(component, 0xFB, 0x01, 0x01); 361 362 /* Read current IRQ events */ 363 regmap_bulk_read(da7219->regmap, DA7219_ACCDET_IRQ_EVENT_A, 364 events, DA7219_AAD_IRQ_REG_MAX); 365 366 if (!events[DA7219_AAD_IRQ_REG_A] && !events[DA7219_AAD_IRQ_REG_B]) 367 return IRQ_NONE; 368 369 /* Read status register for jack insertion & type status */ 370 statusa = snd_soc_component_read(component, DA7219_ACCDET_STATUS_A); 371 372 /* Clear events */ 373 regmap_bulk_write(da7219->regmap, DA7219_ACCDET_IRQ_EVENT_A, 374 events, DA7219_AAD_IRQ_REG_MAX); 375 376 dev_dbg(component->dev, "IRQ events = 0x%x|0x%x, status = 0x%x\n", 377 events[DA7219_AAD_IRQ_REG_A], events[DA7219_AAD_IRQ_REG_B], 378 statusa); 379 380 if (statusa & DA7219_JACK_INSERTION_STS_MASK) { 381 /* Jack Insertion */ 382 if (events[DA7219_AAD_IRQ_REG_A] & 383 DA7219_E_JACK_INSERTED_MASK) { 384 report |= SND_JACK_MECHANICAL; 385 mask |= SND_JACK_MECHANICAL; 386 da7219_aad->jack_inserted = true; 387 } 388 389 /* Jack type detection */ 390 if (events[DA7219_AAD_IRQ_REG_A] & 391 DA7219_E_JACK_DETECT_COMPLETE_MASK) { 392 /* 393 * If 4-pole, then enable button detection, else perform 394 * HP impedance test to determine output type to report. 395 * 396 * We schedule work here as the tasks themselves can 397 * take time to complete, and in particular for hptest 398 * we want to be able to check if the jack was removed 399 * during the procedure as this will invalidate the 400 * result. By doing this as work, the IRQ thread can 401 * handle a removal, and we can check at the end of 402 * hptest if we have a valid result or not. 403 */ 404 if (statusa & DA7219_JACK_TYPE_STS_MASK) { 405 report |= SND_JACK_HEADSET; 406 mask |= SND_JACK_HEADSET | SND_JACK_LINEOUT; 407 schedule_work(&da7219_aad->btn_det_work); 408 } else { 409 schedule_work(&da7219_aad->hptest_work); 410 } 411 } 412 413 /* Button support for 4-pole jack */ 414 if (statusa & DA7219_JACK_TYPE_STS_MASK) { 415 for (i = 0; i < DA7219_AAD_MAX_BUTTONS; ++i) { 416 /* Button Press */ 417 if (events[DA7219_AAD_IRQ_REG_B] & 418 (DA7219_E_BUTTON_A_PRESSED_MASK << i)) { 419 report |= SND_JACK_BTN_0 >> i; 420 mask |= SND_JACK_BTN_0 >> i; 421 } 422 } 423 snd_soc_jack_report(da7219_aad->jack, report, mask); 424 425 for (i = 0; i < DA7219_AAD_MAX_BUTTONS; ++i) { 426 /* Button Release */ 427 if (events[DA7219_AAD_IRQ_REG_B] & 428 (DA7219_E_BUTTON_A_RELEASED_MASK >> i)) { 429 report &= ~(SND_JACK_BTN_0 >> i); 430 mask |= SND_JACK_BTN_0 >> i; 431 } 432 } 433 } 434 } else { 435 /* Jack removal */ 436 if (events[DA7219_AAD_IRQ_REG_A] & DA7219_E_JACK_REMOVED_MASK) { 437 report = 0; 438 mask |= DA7219_AAD_REPORT_ALL_MASK; 439 da7219_aad->jack_inserted = false; 440 441 /* Cancel any pending work */ 442 cancel_work_sync(&da7219_aad->btn_det_work); 443 cancel_work_sync(&da7219_aad->hptest_work); 444 445 /* Un-drive headphones/lineout */ 446 snd_soc_component_update_bits(component, DA7219_HP_R_CTRL, 447 DA7219_HP_R_AMP_OE_MASK, 0); 448 snd_soc_component_update_bits(component, DA7219_HP_L_CTRL, 449 DA7219_HP_L_AMP_OE_MASK, 0); 450 451 /* Ensure button detection disabled */ 452 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1, 453 DA7219_BUTTON_CONFIG_MASK, 0); 454 455 da7219->micbias_on_event = false; 456 457 /* Disable mic bias */ 458 snd_soc_dapm_disable_pin(dapm, "Mic Bias"); 459 snd_soc_dapm_sync(dapm); 460 461 /* Disable ground switch */ 462 snd_soc_component_update_bits(component, 0xFB, 0x01, 0x00); 463 } 464 } 465 466 snd_soc_jack_report(da7219_aad->jack, report, mask); 467 468 return IRQ_HANDLED; 469 } 470 471 /* 472 * DT/ACPI to pdata conversion 473 */ 474 475 static enum da7219_aad_micbias_pulse_lvl 476 da7219_aad_fw_micbias_pulse_lvl(struct device *dev, u32 val) 477 { 478 switch (val) { 479 case 2800: 480 return DA7219_AAD_MICBIAS_PULSE_LVL_2_8V; 481 case 2900: 482 return DA7219_AAD_MICBIAS_PULSE_LVL_2_9V; 483 default: 484 dev_warn(dev, "Invalid micbias pulse level"); 485 return DA7219_AAD_MICBIAS_PULSE_LVL_OFF; 486 } 487 } 488 489 static enum da7219_aad_btn_cfg 490 da7219_aad_fw_btn_cfg(struct device *dev, u32 val) 491 { 492 switch (val) { 493 case 2: 494 return DA7219_AAD_BTN_CFG_2MS; 495 case 5: 496 return DA7219_AAD_BTN_CFG_5MS; 497 case 10: 498 return DA7219_AAD_BTN_CFG_10MS; 499 case 50: 500 return DA7219_AAD_BTN_CFG_50MS; 501 case 100: 502 return DA7219_AAD_BTN_CFG_100MS; 503 case 200: 504 return DA7219_AAD_BTN_CFG_200MS; 505 case 500: 506 return DA7219_AAD_BTN_CFG_500MS; 507 default: 508 dev_warn(dev, "Invalid button config"); 509 return DA7219_AAD_BTN_CFG_10MS; 510 } 511 } 512 513 static enum da7219_aad_mic_det_thr 514 da7219_aad_fw_mic_det_thr(struct device *dev, u32 val) 515 { 516 switch (val) { 517 case 200: 518 return DA7219_AAD_MIC_DET_THR_200_OHMS; 519 case 500: 520 return DA7219_AAD_MIC_DET_THR_500_OHMS; 521 case 750: 522 return DA7219_AAD_MIC_DET_THR_750_OHMS; 523 case 1000: 524 return DA7219_AAD_MIC_DET_THR_1000_OHMS; 525 default: 526 dev_warn(dev, "Invalid mic detect threshold"); 527 return DA7219_AAD_MIC_DET_THR_500_OHMS; 528 } 529 } 530 531 static enum da7219_aad_jack_ins_deb 532 da7219_aad_fw_jack_ins_deb(struct device *dev, u32 val) 533 { 534 switch (val) { 535 case 5: 536 return DA7219_AAD_JACK_INS_DEB_5MS; 537 case 10: 538 return DA7219_AAD_JACK_INS_DEB_10MS; 539 case 20: 540 return DA7219_AAD_JACK_INS_DEB_20MS; 541 case 50: 542 return DA7219_AAD_JACK_INS_DEB_50MS; 543 case 100: 544 return DA7219_AAD_JACK_INS_DEB_100MS; 545 case 200: 546 return DA7219_AAD_JACK_INS_DEB_200MS; 547 case 500: 548 return DA7219_AAD_JACK_INS_DEB_500MS; 549 case 1000: 550 return DA7219_AAD_JACK_INS_DEB_1S; 551 default: 552 dev_warn(dev, "Invalid jack insert debounce"); 553 return DA7219_AAD_JACK_INS_DEB_20MS; 554 } 555 } 556 557 static enum da7219_aad_jack_det_rate 558 da7219_aad_fw_jack_det_rate(struct device *dev, const char *str) 559 { 560 if (!strcmp(str, "32ms_64ms")) { 561 return DA7219_AAD_JACK_DET_RATE_32_64MS; 562 } else if (!strcmp(str, "64ms_128ms")) { 563 return DA7219_AAD_JACK_DET_RATE_64_128MS; 564 } else if (!strcmp(str, "128ms_256ms")) { 565 return DA7219_AAD_JACK_DET_RATE_128_256MS; 566 } else if (!strcmp(str, "256ms_512ms")) { 567 return DA7219_AAD_JACK_DET_RATE_256_512MS; 568 } else { 569 dev_warn(dev, "Invalid jack detect rate"); 570 return DA7219_AAD_JACK_DET_RATE_256_512MS; 571 } 572 } 573 574 static enum da7219_aad_jack_rem_deb 575 da7219_aad_fw_jack_rem_deb(struct device *dev, u32 val) 576 { 577 switch (val) { 578 case 1: 579 return DA7219_AAD_JACK_REM_DEB_1MS; 580 case 5: 581 return DA7219_AAD_JACK_REM_DEB_5MS; 582 case 10: 583 return DA7219_AAD_JACK_REM_DEB_10MS; 584 case 20: 585 return DA7219_AAD_JACK_REM_DEB_20MS; 586 default: 587 dev_warn(dev, "Invalid jack removal debounce"); 588 return DA7219_AAD_JACK_REM_DEB_1MS; 589 } 590 } 591 592 static enum da7219_aad_btn_avg 593 da7219_aad_fw_btn_avg(struct device *dev, u32 val) 594 { 595 switch (val) { 596 case 1: 597 return DA7219_AAD_BTN_AVG_1; 598 case 2: 599 return DA7219_AAD_BTN_AVG_2; 600 case 4: 601 return DA7219_AAD_BTN_AVG_4; 602 case 8: 603 return DA7219_AAD_BTN_AVG_8; 604 default: 605 dev_warn(dev, "Invalid button average value"); 606 return DA7219_AAD_BTN_AVG_2; 607 } 608 } 609 610 static enum da7219_aad_adc_1bit_rpt 611 da7219_aad_fw_adc_1bit_rpt(struct device *dev, u32 val) 612 { 613 switch (val) { 614 case 1: 615 return DA7219_AAD_ADC_1BIT_RPT_1; 616 case 2: 617 return DA7219_AAD_ADC_1BIT_RPT_2; 618 case 4: 619 return DA7219_AAD_ADC_1BIT_RPT_4; 620 case 8: 621 return DA7219_AAD_ADC_1BIT_RPT_8; 622 default: 623 dev_warn(dev, "Invalid ADC 1-bit repeat value"); 624 return DA7219_AAD_ADC_1BIT_RPT_1; 625 } 626 } 627 628 static struct da7219_aad_pdata *da7219_aad_fw_to_pdata(struct device *dev) 629 { 630 struct i2c_client *i2c = to_i2c_client(dev); 631 struct fwnode_handle *aad_np; 632 struct da7219_aad_pdata *aad_pdata; 633 const char *fw_str; 634 u32 fw_val32; 635 636 aad_np = device_get_named_child_node(dev, "da7219_aad"); 637 if (!aad_np) 638 return NULL; 639 640 aad_pdata = devm_kzalloc(dev, sizeof(*aad_pdata), GFP_KERNEL); 641 if (!aad_pdata) 642 return NULL; 643 644 aad_pdata->irq = i2c->irq; 645 646 if (fwnode_property_read_u32(aad_np, "dlg,micbias-pulse-lvl", 647 &fw_val32) >= 0) 648 aad_pdata->micbias_pulse_lvl = 649 da7219_aad_fw_micbias_pulse_lvl(dev, fw_val32); 650 else 651 aad_pdata->micbias_pulse_lvl = DA7219_AAD_MICBIAS_PULSE_LVL_OFF; 652 653 if (fwnode_property_read_u32(aad_np, "dlg,micbias-pulse-time", 654 &fw_val32) >= 0) 655 aad_pdata->micbias_pulse_time = fw_val32; 656 657 if (fwnode_property_read_u32(aad_np, "dlg,btn-cfg", &fw_val32) >= 0) 658 aad_pdata->btn_cfg = da7219_aad_fw_btn_cfg(dev, fw_val32); 659 else 660 aad_pdata->btn_cfg = DA7219_AAD_BTN_CFG_10MS; 661 662 if (fwnode_property_read_u32(aad_np, "dlg,mic-det-thr", &fw_val32) >= 0) 663 aad_pdata->mic_det_thr = 664 da7219_aad_fw_mic_det_thr(dev, fw_val32); 665 else 666 aad_pdata->mic_det_thr = DA7219_AAD_MIC_DET_THR_500_OHMS; 667 668 if (fwnode_property_read_u32(aad_np, "dlg,jack-ins-deb", &fw_val32) >= 0) 669 aad_pdata->jack_ins_deb = 670 da7219_aad_fw_jack_ins_deb(dev, fw_val32); 671 else 672 aad_pdata->jack_ins_deb = DA7219_AAD_JACK_INS_DEB_20MS; 673 674 if (!fwnode_property_read_string(aad_np, "dlg,jack-det-rate", &fw_str)) 675 aad_pdata->jack_det_rate = 676 da7219_aad_fw_jack_det_rate(dev, fw_str); 677 else 678 aad_pdata->jack_det_rate = DA7219_AAD_JACK_DET_RATE_256_512MS; 679 680 if (fwnode_property_read_u32(aad_np, "dlg,jack-rem-deb", &fw_val32) >= 0) 681 aad_pdata->jack_rem_deb = 682 da7219_aad_fw_jack_rem_deb(dev, fw_val32); 683 else 684 aad_pdata->jack_rem_deb = DA7219_AAD_JACK_REM_DEB_1MS; 685 686 if (fwnode_property_read_u32(aad_np, "dlg,a-d-btn-thr", &fw_val32) >= 0) 687 aad_pdata->a_d_btn_thr = (u8) fw_val32; 688 else 689 aad_pdata->a_d_btn_thr = 0xA; 690 691 if (fwnode_property_read_u32(aad_np, "dlg,d-b-btn-thr", &fw_val32) >= 0) 692 aad_pdata->d_b_btn_thr = (u8) fw_val32; 693 else 694 aad_pdata->d_b_btn_thr = 0x16; 695 696 if (fwnode_property_read_u32(aad_np, "dlg,b-c-btn-thr", &fw_val32) >= 0) 697 aad_pdata->b_c_btn_thr = (u8) fw_val32; 698 else 699 aad_pdata->b_c_btn_thr = 0x21; 700 701 if (fwnode_property_read_u32(aad_np, "dlg,c-mic-btn-thr", &fw_val32) >= 0) 702 aad_pdata->c_mic_btn_thr = (u8) fw_val32; 703 else 704 aad_pdata->c_mic_btn_thr = 0x3E; 705 706 if (fwnode_property_read_u32(aad_np, "dlg,btn-avg", &fw_val32) >= 0) 707 aad_pdata->btn_avg = da7219_aad_fw_btn_avg(dev, fw_val32); 708 else 709 aad_pdata->btn_avg = DA7219_AAD_BTN_AVG_2; 710 711 if (fwnode_property_read_u32(aad_np, "dlg,adc-1bit-rpt", &fw_val32) >= 0) 712 aad_pdata->adc_1bit_rpt = 713 da7219_aad_fw_adc_1bit_rpt(dev, fw_val32); 714 else 715 aad_pdata->adc_1bit_rpt = DA7219_AAD_ADC_1BIT_RPT_1; 716 717 return aad_pdata; 718 } 719 720 static void da7219_aad_handle_pdata(struct snd_soc_component *component) 721 { 722 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 723 struct da7219_aad_priv *da7219_aad = da7219->aad; 724 struct da7219_pdata *pdata = da7219->pdata; 725 726 if ((pdata) && (pdata->aad_pdata)) { 727 struct da7219_aad_pdata *aad_pdata = pdata->aad_pdata; 728 u8 cfg, mask; 729 730 da7219_aad->irq = aad_pdata->irq; 731 732 switch (aad_pdata->micbias_pulse_lvl) { 733 case DA7219_AAD_MICBIAS_PULSE_LVL_2_8V: 734 case DA7219_AAD_MICBIAS_PULSE_LVL_2_9V: 735 da7219_aad->micbias_pulse_lvl = 736 (aad_pdata->micbias_pulse_lvl << 737 DA7219_MICBIAS1_LEVEL_SHIFT); 738 break; 739 default: 740 break; 741 } 742 743 da7219_aad->micbias_pulse_time = aad_pdata->micbias_pulse_time; 744 745 switch (aad_pdata->btn_cfg) { 746 case DA7219_AAD_BTN_CFG_2MS: 747 case DA7219_AAD_BTN_CFG_5MS: 748 case DA7219_AAD_BTN_CFG_10MS: 749 case DA7219_AAD_BTN_CFG_50MS: 750 case DA7219_AAD_BTN_CFG_100MS: 751 case DA7219_AAD_BTN_CFG_200MS: 752 case DA7219_AAD_BTN_CFG_500MS: 753 da7219_aad->btn_cfg = (aad_pdata->btn_cfg << 754 DA7219_BUTTON_CONFIG_SHIFT); 755 } 756 757 cfg = 0; 758 mask = 0; 759 switch (aad_pdata->mic_det_thr) { 760 case DA7219_AAD_MIC_DET_THR_200_OHMS: 761 case DA7219_AAD_MIC_DET_THR_500_OHMS: 762 case DA7219_AAD_MIC_DET_THR_750_OHMS: 763 case DA7219_AAD_MIC_DET_THR_1000_OHMS: 764 cfg |= (aad_pdata->mic_det_thr << 765 DA7219_MIC_DET_THRESH_SHIFT); 766 mask |= DA7219_MIC_DET_THRESH_MASK; 767 } 768 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1, mask, cfg); 769 770 cfg = 0; 771 mask = 0; 772 switch (aad_pdata->jack_ins_deb) { 773 case DA7219_AAD_JACK_INS_DEB_5MS: 774 case DA7219_AAD_JACK_INS_DEB_10MS: 775 case DA7219_AAD_JACK_INS_DEB_20MS: 776 case DA7219_AAD_JACK_INS_DEB_50MS: 777 case DA7219_AAD_JACK_INS_DEB_100MS: 778 case DA7219_AAD_JACK_INS_DEB_200MS: 779 case DA7219_AAD_JACK_INS_DEB_500MS: 780 case DA7219_AAD_JACK_INS_DEB_1S: 781 cfg |= (aad_pdata->jack_ins_deb << 782 DA7219_JACKDET_DEBOUNCE_SHIFT); 783 mask |= DA7219_JACKDET_DEBOUNCE_MASK; 784 } 785 switch (aad_pdata->jack_det_rate) { 786 case DA7219_AAD_JACK_DET_RATE_32_64MS: 787 case DA7219_AAD_JACK_DET_RATE_64_128MS: 788 case DA7219_AAD_JACK_DET_RATE_128_256MS: 789 case DA7219_AAD_JACK_DET_RATE_256_512MS: 790 cfg |= (aad_pdata->jack_det_rate << 791 DA7219_JACK_DETECT_RATE_SHIFT); 792 mask |= DA7219_JACK_DETECT_RATE_MASK; 793 } 794 switch (aad_pdata->jack_rem_deb) { 795 case DA7219_AAD_JACK_REM_DEB_1MS: 796 case DA7219_AAD_JACK_REM_DEB_5MS: 797 case DA7219_AAD_JACK_REM_DEB_10MS: 798 case DA7219_AAD_JACK_REM_DEB_20MS: 799 cfg |= (aad_pdata->jack_rem_deb << 800 DA7219_JACKDET_REM_DEB_SHIFT); 801 mask |= DA7219_JACKDET_REM_DEB_MASK; 802 } 803 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_2, mask, cfg); 804 805 snd_soc_component_write(component, DA7219_ACCDET_CONFIG_3, 806 aad_pdata->a_d_btn_thr); 807 snd_soc_component_write(component, DA7219_ACCDET_CONFIG_4, 808 aad_pdata->d_b_btn_thr); 809 snd_soc_component_write(component, DA7219_ACCDET_CONFIG_5, 810 aad_pdata->b_c_btn_thr); 811 snd_soc_component_write(component, DA7219_ACCDET_CONFIG_6, 812 aad_pdata->c_mic_btn_thr); 813 814 cfg = 0; 815 mask = 0; 816 switch (aad_pdata->btn_avg) { 817 case DA7219_AAD_BTN_AVG_1: 818 case DA7219_AAD_BTN_AVG_2: 819 case DA7219_AAD_BTN_AVG_4: 820 case DA7219_AAD_BTN_AVG_8: 821 cfg |= (aad_pdata->btn_avg << 822 DA7219_BUTTON_AVERAGE_SHIFT); 823 mask |= DA7219_BUTTON_AVERAGE_MASK; 824 } 825 switch (aad_pdata->adc_1bit_rpt) { 826 case DA7219_AAD_ADC_1BIT_RPT_1: 827 case DA7219_AAD_ADC_1BIT_RPT_2: 828 case DA7219_AAD_ADC_1BIT_RPT_4: 829 case DA7219_AAD_ADC_1BIT_RPT_8: 830 cfg |= (aad_pdata->adc_1bit_rpt << 831 DA7219_ADC_1_BIT_REPEAT_SHIFT); 832 mask |= DA7219_ADC_1_BIT_REPEAT_MASK; 833 } 834 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_7, mask, cfg); 835 } 836 } 837 838 static void da7219_aad_handle_gnd_switch_time(struct snd_soc_component *component) 839 { 840 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 841 struct da7219_aad_priv *da7219_aad = da7219->aad; 842 u8 jack_det; 843 844 jack_det = snd_soc_component_read(component, DA7219_ACCDET_CONFIG_2) 845 & DA7219_JACK_DETECT_RATE_MASK; 846 switch (jack_det) { 847 case 0x00: 848 da7219_aad->gnd_switch_delay = 32; 849 break; 850 case 0x10: 851 da7219_aad->gnd_switch_delay = 64; 852 break; 853 case 0x20: 854 da7219_aad->gnd_switch_delay = 128; 855 break; 856 case 0x30: 857 da7219_aad->gnd_switch_delay = 256; 858 break; 859 default: 860 da7219_aad->gnd_switch_delay = 32; 861 break; 862 } 863 } 864 865 /* 866 * Suspend/Resume 867 */ 868 869 void da7219_aad_suspend(struct snd_soc_component *component) 870 { 871 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 872 struct da7219_aad_priv *da7219_aad = da7219->aad; 873 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 874 u8 micbias_ctrl; 875 876 if (da7219_aad->jack) { 877 /* Disable jack detection during suspend */ 878 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1, 879 DA7219_ACCDET_EN_MASK, 0); 880 881 /* 882 * If we have a 4-pole jack inserted, then micbias will be 883 * enabled. We can disable micbias here, and keep a note to 884 * re-enable it on resume. If jack removal occurred during 885 * suspend then this will be dealt with through the IRQ handler. 886 */ 887 if (da7219_aad->jack_inserted) { 888 micbias_ctrl = snd_soc_component_read(component, DA7219_MICBIAS_CTRL); 889 if (micbias_ctrl & DA7219_MICBIAS1_EN_MASK) { 890 snd_soc_dapm_disable_pin(dapm, "Mic Bias"); 891 snd_soc_dapm_sync(dapm); 892 da7219_aad->micbias_resume_enable = true; 893 } 894 } 895 } 896 } 897 898 void da7219_aad_resume(struct snd_soc_component *component) 899 { 900 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 901 struct da7219_aad_priv *da7219_aad = da7219->aad; 902 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 903 904 if (da7219_aad->jack) { 905 /* Re-enable micbias if previously enabled for 4-pole jack */ 906 if (da7219_aad->jack_inserted && 907 da7219_aad->micbias_resume_enable) { 908 snd_soc_dapm_force_enable_pin(dapm, "Mic Bias"); 909 snd_soc_dapm_sync(dapm); 910 da7219_aad->micbias_resume_enable = false; 911 } 912 913 /* Re-enable jack detection */ 914 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1, 915 DA7219_ACCDET_EN_MASK, 916 DA7219_ACCDET_EN_MASK); 917 } 918 } 919 920 921 /* 922 * Init/Exit 923 */ 924 925 int da7219_aad_init(struct snd_soc_component *component) 926 { 927 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 928 struct da7219_aad_priv *da7219_aad = da7219->aad; 929 u8 mask[DA7219_AAD_IRQ_REG_MAX]; 930 int ret; 931 932 da7219_aad->component = component; 933 934 /* Handle any DT/ACPI/platform data */ 935 da7219_aad_handle_pdata(component); 936 937 /* Disable button detection */ 938 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1, 939 DA7219_BUTTON_CONFIG_MASK, 0); 940 941 INIT_WORK(&da7219_aad->btn_det_work, da7219_aad_btn_det_work); 942 INIT_WORK(&da7219_aad->hptest_work, da7219_aad_hptest_work); 943 944 ret = request_threaded_irq(da7219_aad->irq, NULL, 945 da7219_aad_irq_thread, 946 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 947 "da7219-aad", da7219_aad); 948 if (ret) { 949 dev_err(component->dev, "Failed to request IRQ: %d\n", ret); 950 return ret; 951 } 952 953 /* Unmask AAD IRQs */ 954 memset(mask, 0, DA7219_AAD_IRQ_REG_MAX); 955 regmap_bulk_write(da7219->regmap, DA7219_ACCDET_IRQ_MASK_A, 956 &mask, DA7219_AAD_IRQ_REG_MAX); 957 958 da7219_aad_handle_gnd_switch_time(component); 959 960 return 0; 961 } 962 963 void da7219_aad_exit(struct snd_soc_component *component) 964 { 965 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 966 struct da7219_aad_priv *da7219_aad = da7219->aad; 967 u8 mask[DA7219_AAD_IRQ_REG_MAX]; 968 969 /* Mask off AAD IRQs */ 970 memset(mask, DA7219_BYTE_MASK, DA7219_AAD_IRQ_REG_MAX); 971 regmap_bulk_write(da7219->regmap, DA7219_ACCDET_IRQ_MASK_A, 972 mask, DA7219_AAD_IRQ_REG_MAX); 973 974 free_irq(da7219_aad->irq, da7219_aad); 975 976 cancel_work_sync(&da7219_aad->btn_det_work); 977 cancel_work_sync(&da7219_aad->hptest_work); 978 } 979 980 /* 981 * AAD related I2C probe handling 982 */ 983 984 int da7219_aad_probe(struct i2c_client *i2c) 985 { 986 struct da7219_priv *da7219 = i2c_get_clientdata(i2c); 987 struct device *dev = &i2c->dev; 988 struct da7219_aad_priv *da7219_aad; 989 990 da7219_aad = devm_kzalloc(dev, sizeof(*da7219_aad), GFP_KERNEL); 991 if (!da7219_aad) 992 return -ENOMEM; 993 994 da7219->aad = da7219_aad; 995 996 /* Retrieve any DT/ACPI/platform data */ 997 if (da7219->pdata && !da7219->pdata->aad_pdata) 998 da7219->pdata->aad_pdata = da7219_aad_fw_to_pdata(dev); 999 1000 return 0; 1001 } 1002 1003 MODULE_DESCRIPTION("ASoC DA7219 AAD Driver"); 1004 MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>"); 1005 MODULE_LICENSE("GPL"); 1006