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