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 static void da7219_aad_jack_det_work(struct work_struct *work) 343 { 344 struct da7219_aad_priv *da7219_aad = 345 container_of(work, struct da7219_aad_priv, jack_det_work); 346 struct snd_soc_component *component = da7219_aad->component; 347 u8 srm_st; 348 349 mutex_lock(&da7219_aad->jack_det_mutex); 350 351 srm_st = snd_soc_component_read(component, DA7219_PLL_SRM_STS) & DA7219_PLL_SRM_STS_MCLK; 352 msleep(da7219_aad->gnd_switch_delay * ((srm_st == 0x0) ? 2 : 1) - 4); 353 /* Enable ground switch */ 354 snd_soc_component_update_bits(component, 0xFB, 0x01, 0x01); 355 356 mutex_unlock(&da7219_aad->jack_det_mutex); 357 } 358 359 360 /* 361 * IRQ 362 */ 363 364 static irqreturn_t da7219_aad_pre_irq_thread(int irq, void *data) 365 { 366 367 struct da7219_aad_priv *da7219_aad = data; 368 369 if (!da7219_aad->jack_inserted) 370 schedule_work(&da7219_aad->jack_det_work); 371 372 return IRQ_WAKE_THREAD; 373 } 374 375 static irqreturn_t da7219_aad_irq_thread(int irq, void *data) 376 { 377 struct da7219_aad_priv *da7219_aad = data; 378 struct snd_soc_component *component = da7219_aad->component; 379 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 380 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 381 u8 events[DA7219_AAD_IRQ_REG_MAX]; 382 u8 statusa; 383 int i, report = 0, mask = 0; 384 385 /* Read current IRQ events */ 386 regmap_bulk_read(da7219->regmap, DA7219_ACCDET_IRQ_EVENT_A, 387 events, DA7219_AAD_IRQ_REG_MAX); 388 389 if (!events[DA7219_AAD_IRQ_REG_A] && !events[DA7219_AAD_IRQ_REG_B]) 390 return IRQ_NONE; 391 392 /* Read status register for jack insertion & type status */ 393 statusa = snd_soc_component_read(component, DA7219_ACCDET_STATUS_A); 394 395 /* Clear events */ 396 regmap_bulk_write(da7219->regmap, DA7219_ACCDET_IRQ_EVENT_A, 397 events, DA7219_AAD_IRQ_REG_MAX); 398 399 dev_dbg(component->dev, "IRQ events = 0x%x|0x%x, status = 0x%x\n", 400 events[DA7219_AAD_IRQ_REG_A], events[DA7219_AAD_IRQ_REG_B], 401 statusa); 402 403 if (!da7219_aad->jack_inserted) 404 cancel_work_sync(&da7219_aad->jack_det_work); 405 406 if (statusa & DA7219_JACK_INSERTION_STS_MASK) { 407 /* Jack Insertion */ 408 if (events[DA7219_AAD_IRQ_REG_A] & 409 DA7219_E_JACK_INSERTED_MASK) { 410 report |= SND_JACK_MECHANICAL; 411 mask |= SND_JACK_MECHANICAL; 412 da7219_aad->jack_inserted = true; 413 } 414 415 /* Jack type detection */ 416 if (events[DA7219_AAD_IRQ_REG_A] & 417 DA7219_E_JACK_DETECT_COMPLETE_MASK) { 418 /* 419 * If 4-pole, then enable button detection, else perform 420 * HP impedance test to determine output type to report. 421 * 422 * We schedule work here as the tasks themselves can 423 * take time to complete, and in particular for hptest 424 * we want to be able to check if the jack was removed 425 * during the procedure as this will invalidate the 426 * result. By doing this as work, the IRQ thread can 427 * handle a removal, and we can check at the end of 428 * hptest if we have a valid result or not. 429 */ 430 if (statusa & DA7219_JACK_TYPE_STS_MASK) { 431 report |= SND_JACK_HEADSET; 432 mask |= SND_JACK_HEADSET | SND_JACK_LINEOUT; 433 schedule_work(&da7219_aad->btn_det_work); 434 } else { 435 schedule_work(&da7219_aad->hptest_work); 436 } 437 } 438 439 /* Button support for 4-pole jack */ 440 if (statusa & DA7219_JACK_TYPE_STS_MASK) { 441 for (i = 0; i < DA7219_AAD_MAX_BUTTONS; ++i) { 442 /* Button Press */ 443 if (events[DA7219_AAD_IRQ_REG_B] & 444 (DA7219_E_BUTTON_A_PRESSED_MASK << i)) { 445 report |= SND_JACK_BTN_0 >> i; 446 mask |= SND_JACK_BTN_0 >> i; 447 } 448 } 449 snd_soc_jack_report(da7219_aad->jack, report, mask); 450 451 for (i = 0; i < DA7219_AAD_MAX_BUTTONS; ++i) { 452 /* Button Release */ 453 if (events[DA7219_AAD_IRQ_REG_B] & 454 (DA7219_E_BUTTON_A_RELEASED_MASK >> i)) { 455 report &= ~(SND_JACK_BTN_0 >> i); 456 mask |= SND_JACK_BTN_0 >> i; 457 } 458 } 459 } 460 } else { 461 /* Jack removal */ 462 if (events[DA7219_AAD_IRQ_REG_A] & DA7219_E_JACK_REMOVED_MASK) { 463 report = 0; 464 mask |= DA7219_AAD_REPORT_ALL_MASK; 465 da7219_aad->jack_inserted = false; 466 467 /* Cancel any pending work */ 468 cancel_work_sync(&da7219_aad->btn_det_work); 469 cancel_work_sync(&da7219_aad->hptest_work); 470 471 /* Un-drive headphones/lineout */ 472 snd_soc_component_update_bits(component, DA7219_HP_R_CTRL, 473 DA7219_HP_R_AMP_OE_MASK, 0); 474 snd_soc_component_update_bits(component, DA7219_HP_L_CTRL, 475 DA7219_HP_L_AMP_OE_MASK, 0); 476 477 /* Ensure button detection disabled */ 478 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1, 479 DA7219_BUTTON_CONFIG_MASK, 0); 480 481 da7219->micbias_on_event = false; 482 483 /* Disable mic bias */ 484 snd_soc_dapm_disable_pin(dapm, "Mic Bias"); 485 snd_soc_dapm_sync(dapm); 486 487 /* Disable ground switch */ 488 snd_soc_component_update_bits(component, 0xFB, 0x01, 0x00); 489 } 490 } 491 492 snd_soc_jack_report(da7219_aad->jack, report, mask); 493 494 return IRQ_HANDLED; 495 } 496 497 /* 498 * DT/ACPI to pdata conversion 499 */ 500 501 static enum da7219_aad_micbias_pulse_lvl 502 da7219_aad_fw_micbias_pulse_lvl(struct device *dev, u32 val) 503 { 504 switch (val) { 505 case 2800: 506 return DA7219_AAD_MICBIAS_PULSE_LVL_2_8V; 507 case 2900: 508 return DA7219_AAD_MICBIAS_PULSE_LVL_2_9V; 509 default: 510 dev_warn(dev, "Invalid micbias pulse level"); 511 return DA7219_AAD_MICBIAS_PULSE_LVL_OFF; 512 } 513 } 514 515 static enum da7219_aad_btn_cfg 516 da7219_aad_fw_btn_cfg(struct device *dev, u32 val) 517 { 518 switch (val) { 519 case 2: 520 return DA7219_AAD_BTN_CFG_2MS; 521 case 5: 522 return DA7219_AAD_BTN_CFG_5MS; 523 case 10: 524 return DA7219_AAD_BTN_CFG_10MS; 525 case 50: 526 return DA7219_AAD_BTN_CFG_50MS; 527 case 100: 528 return DA7219_AAD_BTN_CFG_100MS; 529 case 200: 530 return DA7219_AAD_BTN_CFG_200MS; 531 case 500: 532 return DA7219_AAD_BTN_CFG_500MS; 533 default: 534 dev_warn(dev, "Invalid button config"); 535 return DA7219_AAD_BTN_CFG_10MS; 536 } 537 } 538 539 static enum da7219_aad_mic_det_thr 540 da7219_aad_fw_mic_det_thr(struct device *dev, u32 val) 541 { 542 switch (val) { 543 case 200: 544 return DA7219_AAD_MIC_DET_THR_200_OHMS; 545 case 500: 546 return DA7219_AAD_MIC_DET_THR_500_OHMS; 547 case 750: 548 return DA7219_AAD_MIC_DET_THR_750_OHMS; 549 case 1000: 550 return DA7219_AAD_MIC_DET_THR_1000_OHMS; 551 default: 552 dev_warn(dev, "Invalid mic detect threshold"); 553 return DA7219_AAD_MIC_DET_THR_500_OHMS; 554 } 555 } 556 557 static enum da7219_aad_jack_ins_deb 558 da7219_aad_fw_jack_ins_deb(struct device *dev, u32 val) 559 { 560 switch (val) { 561 case 5: 562 return DA7219_AAD_JACK_INS_DEB_5MS; 563 case 10: 564 return DA7219_AAD_JACK_INS_DEB_10MS; 565 case 20: 566 return DA7219_AAD_JACK_INS_DEB_20MS; 567 case 50: 568 return DA7219_AAD_JACK_INS_DEB_50MS; 569 case 100: 570 return DA7219_AAD_JACK_INS_DEB_100MS; 571 case 200: 572 return DA7219_AAD_JACK_INS_DEB_200MS; 573 case 500: 574 return DA7219_AAD_JACK_INS_DEB_500MS; 575 case 1000: 576 return DA7219_AAD_JACK_INS_DEB_1S; 577 default: 578 dev_warn(dev, "Invalid jack insert debounce"); 579 return DA7219_AAD_JACK_INS_DEB_20MS; 580 } 581 } 582 583 static enum da7219_aad_jack_det_rate 584 da7219_aad_fw_jack_det_rate(struct device *dev, const char *str) 585 { 586 if (!strcmp(str, "32ms_64ms")) { 587 return DA7219_AAD_JACK_DET_RATE_32_64MS; 588 } else if (!strcmp(str, "64ms_128ms")) { 589 return DA7219_AAD_JACK_DET_RATE_64_128MS; 590 } else if (!strcmp(str, "128ms_256ms")) { 591 return DA7219_AAD_JACK_DET_RATE_128_256MS; 592 } else if (!strcmp(str, "256ms_512ms")) { 593 return DA7219_AAD_JACK_DET_RATE_256_512MS; 594 } else { 595 dev_warn(dev, "Invalid jack detect rate"); 596 return DA7219_AAD_JACK_DET_RATE_256_512MS; 597 } 598 } 599 600 static enum da7219_aad_jack_rem_deb 601 da7219_aad_fw_jack_rem_deb(struct device *dev, u32 val) 602 { 603 switch (val) { 604 case 1: 605 return DA7219_AAD_JACK_REM_DEB_1MS; 606 case 5: 607 return DA7219_AAD_JACK_REM_DEB_5MS; 608 case 10: 609 return DA7219_AAD_JACK_REM_DEB_10MS; 610 case 20: 611 return DA7219_AAD_JACK_REM_DEB_20MS; 612 default: 613 dev_warn(dev, "Invalid jack removal debounce"); 614 return DA7219_AAD_JACK_REM_DEB_1MS; 615 } 616 } 617 618 static enum da7219_aad_btn_avg 619 da7219_aad_fw_btn_avg(struct device *dev, u32 val) 620 { 621 switch (val) { 622 case 1: 623 return DA7219_AAD_BTN_AVG_1; 624 case 2: 625 return DA7219_AAD_BTN_AVG_2; 626 case 4: 627 return DA7219_AAD_BTN_AVG_4; 628 case 8: 629 return DA7219_AAD_BTN_AVG_8; 630 default: 631 dev_warn(dev, "Invalid button average value"); 632 return DA7219_AAD_BTN_AVG_2; 633 } 634 } 635 636 static enum da7219_aad_adc_1bit_rpt 637 da7219_aad_fw_adc_1bit_rpt(struct device *dev, u32 val) 638 { 639 switch (val) { 640 case 1: 641 return DA7219_AAD_ADC_1BIT_RPT_1; 642 case 2: 643 return DA7219_AAD_ADC_1BIT_RPT_2; 644 case 4: 645 return DA7219_AAD_ADC_1BIT_RPT_4; 646 case 8: 647 return DA7219_AAD_ADC_1BIT_RPT_8; 648 default: 649 dev_warn(dev, "Invalid ADC 1-bit repeat value"); 650 return DA7219_AAD_ADC_1BIT_RPT_1; 651 } 652 } 653 654 static struct da7219_aad_pdata *da7219_aad_fw_to_pdata(struct device *dev) 655 { 656 struct i2c_client *i2c = to_i2c_client(dev); 657 struct fwnode_handle *aad_np; 658 struct da7219_aad_pdata *aad_pdata; 659 const char *fw_str; 660 u32 fw_val32; 661 662 aad_np = device_get_named_child_node(dev, "da7219_aad"); 663 if (!aad_np) 664 return NULL; 665 666 aad_pdata = devm_kzalloc(dev, sizeof(*aad_pdata), GFP_KERNEL); 667 if (!aad_pdata) 668 return NULL; 669 670 aad_pdata->irq = i2c->irq; 671 672 if (fwnode_property_read_u32(aad_np, "dlg,micbias-pulse-lvl", 673 &fw_val32) >= 0) 674 aad_pdata->micbias_pulse_lvl = 675 da7219_aad_fw_micbias_pulse_lvl(dev, fw_val32); 676 else 677 aad_pdata->micbias_pulse_lvl = DA7219_AAD_MICBIAS_PULSE_LVL_OFF; 678 679 if (fwnode_property_read_u32(aad_np, "dlg,micbias-pulse-time", 680 &fw_val32) >= 0) 681 aad_pdata->micbias_pulse_time = fw_val32; 682 683 if (fwnode_property_read_u32(aad_np, "dlg,btn-cfg", &fw_val32) >= 0) 684 aad_pdata->btn_cfg = da7219_aad_fw_btn_cfg(dev, fw_val32); 685 else 686 aad_pdata->btn_cfg = DA7219_AAD_BTN_CFG_10MS; 687 688 if (fwnode_property_read_u32(aad_np, "dlg,mic-det-thr", &fw_val32) >= 0) 689 aad_pdata->mic_det_thr = 690 da7219_aad_fw_mic_det_thr(dev, fw_val32); 691 else 692 aad_pdata->mic_det_thr = DA7219_AAD_MIC_DET_THR_500_OHMS; 693 694 if (fwnode_property_read_u32(aad_np, "dlg,jack-ins-deb", &fw_val32) >= 0) 695 aad_pdata->jack_ins_deb = 696 da7219_aad_fw_jack_ins_deb(dev, fw_val32); 697 else 698 aad_pdata->jack_ins_deb = DA7219_AAD_JACK_INS_DEB_20MS; 699 700 if (!fwnode_property_read_string(aad_np, "dlg,jack-det-rate", &fw_str)) 701 aad_pdata->jack_det_rate = 702 da7219_aad_fw_jack_det_rate(dev, fw_str); 703 else 704 aad_pdata->jack_det_rate = DA7219_AAD_JACK_DET_RATE_256_512MS; 705 706 if (fwnode_property_read_u32(aad_np, "dlg,jack-rem-deb", &fw_val32) >= 0) 707 aad_pdata->jack_rem_deb = 708 da7219_aad_fw_jack_rem_deb(dev, fw_val32); 709 else 710 aad_pdata->jack_rem_deb = DA7219_AAD_JACK_REM_DEB_1MS; 711 712 if (fwnode_property_read_u32(aad_np, "dlg,a-d-btn-thr", &fw_val32) >= 0) 713 aad_pdata->a_d_btn_thr = (u8) fw_val32; 714 else 715 aad_pdata->a_d_btn_thr = 0xA; 716 717 if (fwnode_property_read_u32(aad_np, "dlg,d-b-btn-thr", &fw_val32) >= 0) 718 aad_pdata->d_b_btn_thr = (u8) fw_val32; 719 else 720 aad_pdata->d_b_btn_thr = 0x16; 721 722 if (fwnode_property_read_u32(aad_np, "dlg,b-c-btn-thr", &fw_val32) >= 0) 723 aad_pdata->b_c_btn_thr = (u8) fw_val32; 724 else 725 aad_pdata->b_c_btn_thr = 0x21; 726 727 if (fwnode_property_read_u32(aad_np, "dlg,c-mic-btn-thr", &fw_val32) >= 0) 728 aad_pdata->c_mic_btn_thr = (u8) fw_val32; 729 else 730 aad_pdata->c_mic_btn_thr = 0x3E; 731 732 if (fwnode_property_read_u32(aad_np, "dlg,btn-avg", &fw_val32) >= 0) 733 aad_pdata->btn_avg = da7219_aad_fw_btn_avg(dev, fw_val32); 734 else 735 aad_pdata->btn_avg = DA7219_AAD_BTN_AVG_2; 736 737 if (fwnode_property_read_u32(aad_np, "dlg,adc-1bit-rpt", &fw_val32) >= 0) 738 aad_pdata->adc_1bit_rpt = 739 da7219_aad_fw_adc_1bit_rpt(dev, fw_val32); 740 else 741 aad_pdata->adc_1bit_rpt = DA7219_AAD_ADC_1BIT_RPT_1; 742 743 return aad_pdata; 744 } 745 746 static void da7219_aad_handle_pdata(struct snd_soc_component *component) 747 { 748 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 749 struct da7219_aad_priv *da7219_aad = da7219->aad; 750 struct da7219_pdata *pdata = da7219->pdata; 751 752 if ((pdata) && (pdata->aad_pdata)) { 753 struct da7219_aad_pdata *aad_pdata = pdata->aad_pdata; 754 u8 cfg, mask; 755 756 da7219_aad->irq = aad_pdata->irq; 757 758 switch (aad_pdata->micbias_pulse_lvl) { 759 case DA7219_AAD_MICBIAS_PULSE_LVL_2_8V: 760 case DA7219_AAD_MICBIAS_PULSE_LVL_2_9V: 761 da7219_aad->micbias_pulse_lvl = 762 (aad_pdata->micbias_pulse_lvl << 763 DA7219_MICBIAS1_LEVEL_SHIFT); 764 break; 765 default: 766 break; 767 } 768 769 da7219_aad->micbias_pulse_time = aad_pdata->micbias_pulse_time; 770 771 switch (aad_pdata->btn_cfg) { 772 case DA7219_AAD_BTN_CFG_2MS: 773 case DA7219_AAD_BTN_CFG_5MS: 774 case DA7219_AAD_BTN_CFG_10MS: 775 case DA7219_AAD_BTN_CFG_50MS: 776 case DA7219_AAD_BTN_CFG_100MS: 777 case DA7219_AAD_BTN_CFG_200MS: 778 case DA7219_AAD_BTN_CFG_500MS: 779 da7219_aad->btn_cfg = (aad_pdata->btn_cfg << 780 DA7219_BUTTON_CONFIG_SHIFT); 781 } 782 783 cfg = 0; 784 mask = 0; 785 switch (aad_pdata->mic_det_thr) { 786 case DA7219_AAD_MIC_DET_THR_200_OHMS: 787 case DA7219_AAD_MIC_DET_THR_500_OHMS: 788 case DA7219_AAD_MIC_DET_THR_750_OHMS: 789 case DA7219_AAD_MIC_DET_THR_1000_OHMS: 790 cfg |= (aad_pdata->mic_det_thr << 791 DA7219_MIC_DET_THRESH_SHIFT); 792 mask |= DA7219_MIC_DET_THRESH_MASK; 793 } 794 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1, mask, cfg); 795 796 cfg = 0; 797 mask = 0; 798 switch (aad_pdata->jack_ins_deb) { 799 case DA7219_AAD_JACK_INS_DEB_5MS: 800 case DA7219_AAD_JACK_INS_DEB_10MS: 801 case DA7219_AAD_JACK_INS_DEB_20MS: 802 case DA7219_AAD_JACK_INS_DEB_50MS: 803 case DA7219_AAD_JACK_INS_DEB_100MS: 804 case DA7219_AAD_JACK_INS_DEB_200MS: 805 case DA7219_AAD_JACK_INS_DEB_500MS: 806 case DA7219_AAD_JACK_INS_DEB_1S: 807 cfg |= (aad_pdata->jack_ins_deb << 808 DA7219_JACKDET_DEBOUNCE_SHIFT); 809 mask |= DA7219_JACKDET_DEBOUNCE_MASK; 810 } 811 switch (aad_pdata->jack_det_rate) { 812 case DA7219_AAD_JACK_DET_RATE_32_64MS: 813 case DA7219_AAD_JACK_DET_RATE_64_128MS: 814 case DA7219_AAD_JACK_DET_RATE_128_256MS: 815 case DA7219_AAD_JACK_DET_RATE_256_512MS: 816 cfg |= (aad_pdata->jack_det_rate << 817 DA7219_JACK_DETECT_RATE_SHIFT); 818 mask |= DA7219_JACK_DETECT_RATE_MASK; 819 } 820 switch (aad_pdata->jack_rem_deb) { 821 case DA7219_AAD_JACK_REM_DEB_1MS: 822 case DA7219_AAD_JACK_REM_DEB_5MS: 823 case DA7219_AAD_JACK_REM_DEB_10MS: 824 case DA7219_AAD_JACK_REM_DEB_20MS: 825 cfg |= (aad_pdata->jack_rem_deb << 826 DA7219_JACKDET_REM_DEB_SHIFT); 827 mask |= DA7219_JACKDET_REM_DEB_MASK; 828 } 829 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_2, mask, cfg); 830 831 snd_soc_component_write(component, DA7219_ACCDET_CONFIG_3, 832 aad_pdata->a_d_btn_thr); 833 snd_soc_component_write(component, DA7219_ACCDET_CONFIG_4, 834 aad_pdata->d_b_btn_thr); 835 snd_soc_component_write(component, DA7219_ACCDET_CONFIG_5, 836 aad_pdata->b_c_btn_thr); 837 snd_soc_component_write(component, DA7219_ACCDET_CONFIG_6, 838 aad_pdata->c_mic_btn_thr); 839 840 cfg = 0; 841 mask = 0; 842 switch (aad_pdata->btn_avg) { 843 case DA7219_AAD_BTN_AVG_1: 844 case DA7219_AAD_BTN_AVG_2: 845 case DA7219_AAD_BTN_AVG_4: 846 case DA7219_AAD_BTN_AVG_8: 847 cfg |= (aad_pdata->btn_avg << 848 DA7219_BUTTON_AVERAGE_SHIFT); 849 mask |= DA7219_BUTTON_AVERAGE_MASK; 850 } 851 switch (aad_pdata->adc_1bit_rpt) { 852 case DA7219_AAD_ADC_1BIT_RPT_1: 853 case DA7219_AAD_ADC_1BIT_RPT_2: 854 case DA7219_AAD_ADC_1BIT_RPT_4: 855 case DA7219_AAD_ADC_1BIT_RPT_8: 856 cfg |= (aad_pdata->adc_1bit_rpt << 857 DA7219_ADC_1_BIT_REPEAT_SHIFT); 858 mask |= DA7219_ADC_1_BIT_REPEAT_MASK; 859 } 860 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_7, mask, cfg); 861 } 862 } 863 864 static void da7219_aad_handle_gnd_switch_time(struct snd_soc_component *component) 865 { 866 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 867 struct da7219_aad_priv *da7219_aad = da7219->aad; 868 u8 jack_det; 869 870 jack_det = snd_soc_component_read(component, DA7219_ACCDET_CONFIG_2) 871 & DA7219_JACK_DETECT_RATE_MASK; 872 switch (jack_det) { 873 case 0x00: 874 da7219_aad->gnd_switch_delay = 32; 875 break; 876 case 0x10: 877 da7219_aad->gnd_switch_delay = 64; 878 break; 879 case 0x20: 880 da7219_aad->gnd_switch_delay = 128; 881 break; 882 case 0x30: 883 da7219_aad->gnd_switch_delay = 256; 884 break; 885 default: 886 da7219_aad->gnd_switch_delay = 32; 887 break; 888 } 889 } 890 891 /* 892 * Suspend/Resume 893 */ 894 895 void da7219_aad_suspend(struct snd_soc_component *component) 896 { 897 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 898 struct da7219_aad_priv *da7219_aad = da7219->aad; 899 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 900 u8 micbias_ctrl; 901 902 if (da7219_aad->jack) { 903 /* Disable jack detection during suspend */ 904 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1, 905 DA7219_ACCDET_EN_MASK, 0); 906 907 /* 908 * If we have a 4-pole jack inserted, then micbias will be 909 * enabled. We can disable micbias here, and keep a note to 910 * re-enable it on resume. If jack removal occurred during 911 * suspend then this will be dealt with through the IRQ handler. 912 */ 913 if (da7219_aad->jack_inserted) { 914 micbias_ctrl = snd_soc_component_read(component, DA7219_MICBIAS_CTRL); 915 if (micbias_ctrl & DA7219_MICBIAS1_EN_MASK) { 916 snd_soc_dapm_disable_pin(dapm, "Mic Bias"); 917 snd_soc_dapm_sync(dapm); 918 da7219_aad->micbias_resume_enable = true; 919 } 920 } 921 } 922 } 923 924 void da7219_aad_resume(struct snd_soc_component *component) 925 { 926 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 927 struct da7219_aad_priv *da7219_aad = da7219->aad; 928 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 929 930 if (da7219_aad->jack) { 931 /* Re-enable micbias if previously enabled for 4-pole jack */ 932 if (da7219_aad->jack_inserted && 933 da7219_aad->micbias_resume_enable) { 934 snd_soc_dapm_force_enable_pin(dapm, "Mic Bias"); 935 snd_soc_dapm_sync(dapm); 936 da7219_aad->micbias_resume_enable = false; 937 } 938 939 /* Re-enable jack detection */ 940 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1, 941 DA7219_ACCDET_EN_MASK, 942 DA7219_ACCDET_EN_MASK); 943 } 944 } 945 946 947 /* 948 * Init/Exit 949 */ 950 951 int da7219_aad_init(struct snd_soc_component *component) 952 { 953 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 954 struct da7219_aad_priv *da7219_aad = da7219->aad; 955 u8 mask[DA7219_AAD_IRQ_REG_MAX]; 956 int ret; 957 958 da7219_aad->component = component; 959 960 /* Handle any DT/ACPI/platform data */ 961 da7219_aad_handle_pdata(component); 962 963 /* Disable button detection */ 964 snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1, 965 DA7219_BUTTON_CONFIG_MASK, 0); 966 967 INIT_WORK(&da7219_aad->btn_det_work, da7219_aad_btn_det_work); 968 INIT_WORK(&da7219_aad->hptest_work, da7219_aad_hptest_work); 969 INIT_WORK(&da7219_aad->jack_det_work, da7219_aad_jack_det_work); 970 971 mutex_init(&da7219_aad->jack_det_mutex); 972 973 ret = request_threaded_irq(da7219_aad->irq, da7219_aad_pre_irq_thread, 974 da7219_aad_irq_thread, 975 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 976 "da7219-aad", da7219_aad); 977 if (ret) { 978 dev_err(component->dev, "Failed to request IRQ: %d\n", ret); 979 return ret; 980 } 981 982 /* Unmask AAD IRQs */ 983 memset(mask, 0, DA7219_AAD_IRQ_REG_MAX); 984 regmap_bulk_write(da7219->regmap, DA7219_ACCDET_IRQ_MASK_A, 985 &mask, DA7219_AAD_IRQ_REG_MAX); 986 987 da7219_aad_handle_gnd_switch_time(component); 988 989 return 0; 990 } 991 992 void da7219_aad_exit(struct snd_soc_component *component) 993 { 994 struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component); 995 struct da7219_aad_priv *da7219_aad = da7219->aad; 996 u8 mask[DA7219_AAD_IRQ_REG_MAX]; 997 998 /* Mask off AAD IRQs */ 999 memset(mask, DA7219_BYTE_MASK, DA7219_AAD_IRQ_REG_MAX); 1000 regmap_bulk_write(da7219->regmap, DA7219_ACCDET_IRQ_MASK_A, 1001 mask, DA7219_AAD_IRQ_REG_MAX); 1002 1003 free_irq(da7219_aad->irq, da7219_aad); 1004 1005 cancel_work_sync(&da7219_aad->btn_det_work); 1006 cancel_work_sync(&da7219_aad->hptest_work); 1007 } 1008 1009 /* 1010 * AAD related I2C probe handling 1011 */ 1012 1013 int da7219_aad_probe(struct i2c_client *i2c) 1014 { 1015 struct da7219_priv *da7219 = i2c_get_clientdata(i2c); 1016 struct device *dev = &i2c->dev; 1017 struct da7219_aad_priv *da7219_aad; 1018 1019 da7219_aad = devm_kzalloc(dev, sizeof(*da7219_aad), GFP_KERNEL); 1020 if (!da7219_aad) 1021 return -ENOMEM; 1022 1023 da7219->aad = da7219_aad; 1024 1025 /* Retrieve any DT/ACPI/platform data */ 1026 if (da7219->pdata && !da7219->pdata->aad_pdata) 1027 da7219->pdata->aad_pdata = da7219_aad_fw_to_pdata(dev); 1028 1029 return 0; 1030 } 1031 1032 MODULE_DESCRIPTION("ASoC DA7219 AAD Driver"); 1033 MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>"); 1034 MODULE_LICENSE("GPL"); 1035