1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * extcon-arizona.c - Extcon driver Wolfson Arizona devices 4 * 5 * Copyright (C) 2012-2014 Wolfson Microelectronics plc 6 */ 7 8 #include <linux/kernel.h> 9 #include <linux/module.h> 10 #include <linux/slab.h> 11 #include <linux/interrupt.h> 12 #include <linux/err.h> 13 #include <linux/gpio/consumer.h> 14 #include <linux/gpio.h> 15 #include <linux/input.h> 16 #include <linux/pm_runtime.h> 17 #include <linux/property.h> 18 #include <linux/regulator/consumer.h> 19 20 #include <sound/jack.h> 21 #include <sound/soc.h> 22 23 #include <linux/mfd/arizona/core.h> 24 #include <linux/mfd/arizona/pdata.h> 25 #include <linux/mfd/arizona/registers.h> 26 #include <dt-bindings/mfd/arizona.h> 27 28 #include "arizona.h" 29 30 #define ARIZONA_MAX_MICD_RANGE 8 31 32 /* 33 * The hardware supports 8 ranges / buttons, but the snd-jack interface 34 * only supports 6 buttons (button 0-5). 35 */ 36 #define ARIZONA_MAX_MICD_BUTTONS 6 37 38 #define ARIZONA_MICD_CLAMP_MODE_JDL 0x4 39 #define ARIZONA_MICD_CLAMP_MODE_JDH 0x5 40 #define ARIZONA_MICD_CLAMP_MODE_JDL_GP5H 0x9 41 #define ARIZONA_MICD_CLAMP_MODE_JDH_GP5H 0xb 42 43 #define ARIZONA_TST_CAP_DEFAULT 0x3 44 #define ARIZONA_TST_CAP_CLAMP 0x1 45 46 #define ARIZONA_HPDET_MAX 10000 47 48 #define HPDET_DEBOUNCE 500 49 #define DEFAULT_MICD_TIMEOUT 2000 50 51 #define ARIZONA_HPDET_WAIT_COUNT 15 52 #define ARIZONA_HPDET_WAIT_DELAY_MS 20 53 54 #define QUICK_HEADPHONE_MAX_OHM 3 55 #define MICROPHONE_MIN_OHM 1257 56 #define MICROPHONE_MAX_OHM 30000 57 58 #define MICD_DBTIME_TWO_READINGS 2 59 #define MICD_DBTIME_FOUR_READINGS 4 60 61 #define MICD_LVL_1_TO_7 (ARIZONA_MICD_LVL_1 | ARIZONA_MICD_LVL_2 | \ 62 ARIZONA_MICD_LVL_3 | ARIZONA_MICD_LVL_4 | \ 63 ARIZONA_MICD_LVL_5 | ARIZONA_MICD_LVL_6 | \ 64 ARIZONA_MICD_LVL_7) 65 66 #define MICD_LVL_0_TO_7 (ARIZONA_MICD_LVL_0 | MICD_LVL_1_TO_7) 67 68 #define MICD_LVL_0_TO_8 (MICD_LVL_0_TO_7 | ARIZONA_MICD_LVL_8) 69 70 static const struct arizona_micd_config micd_default_modes[] = { 71 { ARIZONA_ACCDET_SRC, 1, 0 }, 72 { 0, 2, 1 }, 73 }; 74 75 static const struct arizona_micd_range micd_default_ranges[] = { 76 { .max = 11, .key = BTN_0 }, 77 { .max = 28, .key = BTN_1 }, 78 { .max = 54, .key = BTN_2 }, 79 { .max = 100, .key = BTN_3 }, 80 { .max = 186, .key = BTN_4 }, 81 { .max = 430, .key = BTN_5 }, 82 }; 83 84 /* The number of levels in arizona_micd_levels valid for button thresholds */ 85 #define ARIZONA_NUM_MICD_BUTTON_LEVELS 64 86 87 static const int arizona_micd_levels[] = { 88 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46, 89 49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100, 90 105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245, 91 270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071, 92 1257, 30000, 93 }; 94 95 static void arizona_start_hpdet_acc_id(struct arizona_priv *info); 96 97 static void arizona_extcon_hp_clamp(struct arizona_priv *info, 98 bool clamp) 99 { 100 struct arizona *arizona = info->arizona; 101 unsigned int mask = 0, val = 0; 102 unsigned int cap_sel = 0; 103 int ret; 104 105 switch (arizona->type) { 106 case WM8998: 107 case WM1814: 108 mask = 0; 109 break; 110 case WM5110: 111 case WM8280: 112 mask = ARIZONA_HP1L_SHRTO | ARIZONA_HP1L_FLWR | 113 ARIZONA_HP1L_SHRTI; 114 if (clamp) { 115 val = ARIZONA_HP1L_SHRTO; 116 cap_sel = ARIZONA_TST_CAP_CLAMP; 117 } else { 118 val = ARIZONA_HP1L_FLWR | ARIZONA_HP1L_SHRTI; 119 cap_sel = ARIZONA_TST_CAP_DEFAULT; 120 } 121 122 ret = regmap_update_bits(arizona->regmap, 123 ARIZONA_HP_TEST_CTRL_1, 124 ARIZONA_HP1_TST_CAP_SEL_MASK, 125 cap_sel); 126 if (ret) 127 dev_warn(arizona->dev, "Failed to set TST_CAP_SEL: %d\n", ret); 128 break; 129 default: 130 mask = ARIZONA_RMV_SHRT_HP1L; 131 if (clamp) 132 val = ARIZONA_RMV_SHRT_HP1L; 133 break; 134 } 135 136 snd_soc_dapm_mutex_lock(arizona->dapm); 137 138 arizona->hpdet_clamp = clamp; 139 140 /* Keep the HP output stages disabled while doing the clamp */ 141 if (clamp) { 142 ret = regmap_update_bits(arizona->regmap, 143 ARIZONA_OUTPUT_ENABLES_1, 144 ARIZONA_OUT1L_ENA | 145 ARIZONA_OUT1R_ENA, 0); 146 if (ret) 147 dev_warn(arizona->dev, "Failed to disable headphone outputs: %d\n", ret); 148 } 149 150 if (mask) { 151 ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1L, 152 mask, val); 153 if (ret) 154 dev_warn(arizona->dev, "Failed to do clamp: %d\n", ret); 155 156 ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1R, 157 mask, val); 158 if (ret) 159 dev_warn(arizona->dev, "Failed to do clamp: %d\n", ret); 160 } 161 162 /* Restore the desired state while not doing the clamp */ 163 if (!clamp) { 164 ret = regmap_update_bits(arizona->regmap, 165 ARIZONA_OUTPUT_ENABLES_1, 166 ARIZONA_OUT1L_ENA | 167 ARIZONA_OUT1R_ENA, arizona->hp_ena); 168 if (ret) 169 dev_warn(arizona->dev, "Failed to restore headphone outputs: %d\n", ret); 170 } 171 172 snd_soc_dapm_mutex_unlock(arizona->dapm); 173 } 174 175 static void arizona_extcon_set_mode(struct arizona_priv *info, int mode) 176 { 177 struct arizona *arizona = info->arizona; 178 179 mode %= info->micd_num_modes; 180 181 gpiod_set_value_cansleep(info->micd_pol_gpio, 182 info->micd_modes[mode].gpio); 183 184 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1, 185 ARIZONA_MICD_BIAS_SRC_MASK, 186 info->micd_modes[mode].bias << 187 ARIZONA_MICD_BIAS_SRC_SHIFT); 188 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1, 189 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src); 190 191 info->micd_mode = mode; 192 193 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode); 194 } 195 196 static const char *arizona_extcon_get_micbias(struct arizona_priv *info) 197 { 198 switch (info->micd_modes[0].bias) { 199 case 1: 200 return "MICBIAS1"; 201 case 2: 202 return "MICBIAS2"; 203 case 3: 204 return "MICBIAS3"; 205 default: 206 return "MICVDD"; 207 } 208 } 209 210 static void arizona_extcon_pulse_micbias(struct arizona_priv *info) 211 { 212 struct arizona *arizona = info->arizona; 213 const char *widget = arizona_extcon_get_micbias(info); 214 struct snd_soc_dapm_context *dapm = arizona->dapm; 215 struct snd_soc_component *component = snd_soc_dapm_to_component(dapm); 216 int ret; 217 218 ret = snd_soc_component_force_enable_pin(component, widget); 219 if (ret) 220 dev_warn(arizona->dev, "Failed to enable %s: %d\n", widget, ret); 221 222 snd_soc_dapm_sync(dapm); 223 224 if (!arizona->pdata.micd_force_micbias) { 225 ret = snd_soc_component_disable_pin(component, widget); 226 if (ret) 227 dev_warn(arizona->dev, "Failed to disable %s: %d\n", widget, ret); 228 229 snd_soc_dapm_sync(dapm); 230 } 231 } 232 233 static void arizona_start_mic(struct arizona_priv *info) 234 { 235 struct arizona *arizona = info->arizona; 236 bool change; 237 int ret; 238 unsigned int mode; 239 240 /* Microphone detection can't use idle mode */ 241 pm_runtime_get_sync(arizona->dev); 242 243 if (info->detecting) { 244 ret = regulator_allow_bypass(info->micvdd, false); 245 if (ret) 246 dev_err(arizona->dev, "Failed to regulate MICVDD: %d\n", ret); 247 } 248 249 ret = regulator_enable(info->micvdd); 250 if (ret) 251 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n", ret); 252 253 if (info->micd_reva) { 254 const struct reg_sequence reva[] = { 255 { 0x80, 0x3 }, 256 { 0x294, 0x0 }, 257 { 0x80, 0x0 }, 258 }; 259 260 regmap_multi_reg_write(arizona->regmap, reva, ARRAY_SIZE(reva)); 261 } 262 263 if (info->detecting && arizona->pdata.micd_software_compare) 264 mode = ARIZONA_ACCDET_MODE_ADC; 265 else 266 mode = ARIZONA_ACCDET_MODE_MIC; 267 268 regmap_update_bits(arizona->regmap, 269 ARIZONA_ACCESSORY_DETECT_MODE_1, 270 ARIZONA_ACCDET_MODE_MASK, mode); 271 272 arizona_extcon_pulse_micbias(info); 273 274 ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1, 275 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA, 276 &change); 277 if (ret < 0) { 278 dev_err(arizona->dev, "Failed to enable micd: %d\n", ret); 279 } else if (!change) { 280 regulator_disable(info->micvdd); 281 pm_runtime_put_autosuspend(arizona->dev); 282 } 283 } 284 285 static void arizona_stop_mic(struct arizona_priv *info) 286 { 287 struct arizona *arizona = info->arizona; 288 const char *widget = arizona_extcon_get_micbias(info); 289 struct snd_soc_dapm_context *dapm = arizona->dapm; 290 struct snd_soc_component *component = snd_soc_dapm_to_component(dapm); 291 bool change = false; 292 int ret; 293 294 ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1, 295 ARIZONA_MICD_ENA, 0, 296 &change); 297 if (ret < 0) 298 dev_err(arizona->dev, "Failed to disable micd: %d\n", ret); 299 300 ret = snd_soc_component_disable_pin(component, widget); 301 if (ret) 302 dev_warn(arizona->dev, "Failed to disable %s: %d\n", widget, ret); 303 304 snd_soc_dapm_sync(dapm); 305 306 if (info->micd_reva) { 307 const struct reg_sequence reva[] = { 308 { 0x80, 0x3 }, 309 { 0x294, 0x2 }, 310 { 0x80, 0x0 }, 311 }; 312 313 regmap_multi_reg_write(arizona->regmap, reva, ARRAY_SIZE(reva)); 314 } 315 316 ret = regulator_allow_bypass(info->micvdd, true); 317 if (ret) 318 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n", ret); 319 320 if (change) { 321 regulator_disable(info->micvdd); 322 pm_runtime_put_autosuspend(arizona->dev); 323 } 324 } 325 326 static struct { 327 unsigned int threshold; 328 unsigned int factor_a; 329 unsigned int factor_b; 330 } arizona_hpdet_b_ranges[] = { 331 { 100, 5528, 362464 }, 332 { 169, 11084, 6186851 }, 333 { 169, 11065, 65460395 }, 334 }; 335 336 #define ARIZONA_HPDET_B_RANGE_MAX 0x3fb 337 338 static struct { 339 int min; 340 int max; 341 } arizona_hpdet_c_ranges[] = { 342 { 0, 30 }, 343 { 8, 100 }, 344 { 100, 1000 }, 345 { 1000, 10000 }, 346 }; 347 348 static int arizona_hpdet_read(struct arizona_priv *info) 349 { 350 struct arizona *arizona = info->arizona; 351 unsigned int val, range; 352 int ret; 353 354 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val); 355 if (ret) { 356 dev_err(arizona->dev, "Failed to read HPDET status: %d\n", ret); 357 return ret; 358 } 359 360 switch (info->hpdet_ip_version) { 361 case 0: 362 if (!(val & ARIZONA_HP_DONE)) { 363 dev_err(arizona->dev, "HPDET did not complete: %x\n", val); 364 return -EAGAIN; 365 } 366 367 val &= ARIZONA_HP_LVL_MASK; 368 break; 369 370 case 1: 371 if (!(val & ARIZONA_HP_DONE_B)) { 372 dev_err(arizona->dev, "HPDET did not complete: %x\n", val); 373 return -EAGAIN; 374 } 375 376 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val); 377 if (ret) { 378 dev_err(arizona->dev, "Failed to read HP value: %d\n", ret); 379 return -EAGAIN; 380 } 381 382 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1, 383 &range); 384 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK) 385 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT; 386 387 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 && 388 (val < arizona_hpdet_b_ranges[range].threshold || 389 val >= ARIZONA_HPDET_B_RANGE_MAX)) { 390 range++; 391 dev_dbg(arizona->dev, "Moving to HPDET range %d\n", range); 392 regmap_update_bits(arizona->regmap, 393 ARIZONA_HEADPHONE_DETECT_1, 394 ARIZONA_HP_IMPEDANCE_RANGE_MASK, 395 range << 396 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT); 397 return -EAGAIN; 398 } 399 400 /* If we go out of range report top of range */ 401 if (val < arizona_hpdet_b_ranges[range].threshold || 402 val >= ARIZONA_HPDET_B_RANGE_MAX) { 403 dev_dbg(arizona->dev, "Measurement out of range\n"); 404 return ARIZONA_HPDET_MAX; 405 } 406 407 dev_dbg(arizona->dev, "HPDET read %d in range %d\n", val, range); 408 409 val = arizona_hpdet_b_ranges[range].factor_b 410 / ((val * 100) - 411 arizona_hpdet_b_ranges[range].factor_a); 412 break; 413 414 case 2: 415 if (!(val & ARIZONA_HP_DONE_B)) { 416 dev_err(arizona->dev, "HPDET did not complete: %x\n", val); 417 return -EAGAIN; 418 } 419 420 val &= ARIZONA_HP_LVL_B_MASK; 421 /* Convert to ohms, the value is in 0.5 ohm increments */ 422 val /= 2; 423 424 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1, 425 &range); 426 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK) 427 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT; 428 429 /* Skip up a range, or report? */ 430 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 && 431 (val >= arizona_hpdet_c_ranges[range].max)) { 432 range++; 433 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n", 434 arizona_hpdet_c_ranges[range].min, 435 arizona_hpdet_c_ranges[range].max); 436 regmap_update_bits(arizona->regmap, 437 ARIZONA_HEADPHONE_DETECT_1, 438 ARIZONA_HP_IMPEDANCE_RANGE_MASK, 439 range << 440 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT); 441 return -EAGAIN; 442 } 443 444 if (range && (val < arizona_hpdet_c_ranges[range].min)) { 445 dev_dbg(arizona->dev, "Reporting range boundary %d\n", 446 arizona_hpdet_c_ranges[range].min); 447 val = arizona_hpdet_c_ranges[range].min; 448 } 449 break; 450 451 default: 452 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n", info->hpdet_ip_version); 453 return -EINVAL; 454 } 455 456 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val); 457 return val; 458 } 459 460 static int arizona_hpdet_do_id(struct arizona_priv *info, int *reading, 461 bool *mic) 462 { 463 struct arizona *arizona = info->arizona; 464 #ifdef CONFIG_GPIOLIB_LEGACY 465 int id_gpio = arizona->pdata.hpdet_id_gpio; 466 #else 467 int id_gpio = 0; 468 #endif 469 470 if (!arizona->pdata.hpdet_acc_id) 471 return 0; 472 473 /* 474 * If we're using HPDET for accessory identification we need 475 * to take multiple measurements, step through them in sequence. 476 */ 477 info->hpdet_res[info->num_hpdet_res++] = *reading; 478 479 #ifdef CONFIG_GPIOLIB_LEGACY 480 /* Only check the mic directly if we didn't already ID it */ 481 if (id_gpio && info->num_hpdet_res == 1) { 482 dev_dbg(arizona->dev, "Measuring mic\n"); 483 484 regmap_update_bits(arizona->regmap, 485 ARIZONA_ACCESSORY_DETECT_MODE_1, 486 ARIZONA_ACCDET_MODE_MASK | 487 ARIZONA_ACCDET_SRC, 488 ARIZONA_ACCDET_MODE_HPR | 489 info->micd_modes[0].src); 490 491 gpio_set_value_cansleep(id_gpio, 1); 492 493 regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1, 494 ARIZONA_HP_POLL, ARIZONA_HP_POLL); 495 return -EAGAIN; 496 } 497 #endif 498 499 /* OK, got both. Now, compare... */ 500 dev_dbg(arizona->dev, "HPDET measured %d %d\n", 501 info->hpdet_res[0], info->hpdet_res[1]); 502 503 /* Take the headphone impedance for the main report */ 504 *reading = info->hpdet_res[0]; 505 506 /* Sometimes we get false readings due to slow insert */ 507 if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) { 508 dev_dbg(arizona->dev, "Retrying high impedance\n"); 509 info->num_hpdet_res = 0; 510 info->hpdet_retried = true; 511 arizona_start_hpdet_acc_id(info); 512 pm_runtime_put(arizona->dev); 513 return -EAGAIN; 514 } 515 516 /* 517 * If we measure the mic as high impedance 518 */ 519 if (!id_gpio || info->hpdet_res[1] > 50) { 520 dev_dbg(arizona->dev, "Detected mic\n"); 521 *mic = true; 522 info->detecting = true; 523 } else { 524 dev_dbg(arizona->dev, "Detected headphone\n"); 525 } 526 527 /* Make sure everything is reset back to the real polarity */ 528 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1, 529 ARIZONA_ACCDET_SRC, info->micd_modes[0].src); 530 531 return 0; 532 } 533 534 static irqreturn_t arizona_hpdet_irq(int irq, void *data) 535 { 536 struct arizona_priv *info = data; 537 struct arizona *arizona = info->arizona; 538 #ifdef CONFIG_GPIOLIB_LEGACY 539 int id_gpio = arizona->pdata.hpdet_id_gpio; 540 #endif 541 int ret, reading, state, report; 542 bool mic = false; 543 544 mutex_lock(&info->lock); 545 546 /* If we got a spurious IRQ for some reason then ignore it */ 547 if (!info->hpdet_active) { 548 dev_warn(arizona->dev, "Spurious HPDET IRQ\n"); 549 mutex_unlock(&info->lock); 550 return IRQ_NONE; 551 } 552 553 /* If the cable was removed while measuring ignore the result */ 554 state = info->jack->status & SND_JACK_MECHANICAL; 555 if (!state) { 556 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n"); 557 goto done; 558 } 559 560 ret = arizona_hpdet_read(info); 561 if (ret == -EAGAIN) 562 goto out; 563 else if (ret < 0) 564 goto done; 565 reading = ret; 566 567 /* Reset back to starting range */ 568 regmap_update_bits(arizona->regmap, 569 ARIZONA_HEADPHONE_DETECT_1, 570 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL, 571 0); 572 573 ret = arizona_hpdet_do_id(info, &reading, &mic); 574 if (ret == -EAGAIN) 575 goto out; 576 else if (ret < 0) 577 goto done; 578 579 /* Report high impedence cables as line outputs */ 580 if (reading >= 5000) 581 report = SND_JACK_LINEOUT; 582 else 583 report = SND_JACK_HEADPHONE; 584 585 snd_soc_jack_report(info->jack, report, SND_JACK_LINEOUT | SND_JACK_HEADPHONE); 586 587 done: 588 /* Reset back to starting range */ 589 regmap_update_bits(arizona->regmap, 590 ARIZONA_HEADPHONE_DETECT_1, 591 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL, 592 0); 593 594 arizona_extcon_hp_clamp(info, false); 595 596 #ifdef CONFIG_GPIOLIB_LEGACY 597 if (id_gpio) 598 gpio_set_value_cansleep(id_gpio, 0); 599 #endif 600 601 /* If we have a mic then reenable MICDET */ 602 if (state && (mic || info->mic)) 603 arizona_start_mic(info); 604 605 if (info->hpdet_active) { 606 pm_runtime_put_autosuspend(arizona->dev); 607 info->hpdet_active = false; 608 } 609 610 /* Do not set hp_det done when the cable has been unplugged */ 611 if (state) 612 info->hpdet_done = true; 613 614 out: 615 mutex_unlock(&info->lock); 616 617 return IRQ_HANDLED; 618 } 619 620 static void arizona_identify_headphone(struct arizona_priv *info) 621 { 622 struct arizona *arizona = info->arizona; 623 int ret; 624 625 if (info->hpdet_done) 626 return; 627 628 dev_dbg(arizona->dev, "Starting HPDET\n"); 629 630 /* Make sure we keep the device enabled during the measurement */ 631 pm_runtime_get_sync(arizona->dev); 632 633 info->hpdet_active = true; 634 635 arizona_stop_mic(info); 636 637 arizona_extcon_hp_clamp(info, true); 638 639 ret = regmap_update_bits(arizona->regmap, 640 ARIZONA_ACCESSORY_DETECT_MODE_1, 641 ARIZONA_ACCDET_MODE_MASK, 642 arizona->pdata.hpdet_channel); 643 if (ret != 0) { 644 dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret); 645 goto err; 646 } 647 648 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1, 649 ARIZONA_HP_POLL, ARIZONA_HP_POLL); 650 if (ret) { 651 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n", ret); 652 goto err; 653 } 654 655 return; 656 657 err: 658 arizona_extcon_hp_clamp(info, false); 659 pm_runtime_put_autosuspend(arizona->dev); 660 661 /* Just report headphone */ 662 snd_soc_jack_report(info->jack, SND_JACK_HEADPHONE, 663 SND_JACK_LINEOUT | SND_JACK_HEADPHONE); 664 665 if (info->mic) 666 arizona_start_mic(info); 667 668 info->hpdet_active = false; 669 } 670 671 static void arizona_start_hpdet_acc_id(struct arizona_priv *info) 672 { 673 struct arizona *arizona = info->arizona; 674 int hp_reading = 32; 675 bool mic; 676 int ret; 677 678 dev_dbg(arizona->dev, "Starting identification via HPDET\n"); 679 680 /* Make sure we keep the device enabled during the measurement */ 681 pm_runtime_get_sync(arizona->dev); 682 683 info->hpdet_active = true; 684 685 arizona_extcon_hp_clamp(info, true); 686 687 ret = regmap_update_bits(arizona->regmap, 688 ARIZONA_ACCESSORY_DETECT_MODE_1, 689 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK, 690 info->micd_modes[0].src | 691 arizona->pdata.hpdet_channel); 692 if (ret != 0) { 693 dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret); 694 goto err; 695 } 696 697 if (arizona->pdata.hpdet_acc_id_line) { 698 ret = regmap_update_bits(arizona->regmap, 699 ARIZONA_HEADPHONE_DETECT_1, 700 ARIZONA_HP_POLL, ARIZONA_HP_POLL); 701 if (ret) { 702 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n", ret); 703 goto err; 704 } 705 } else { 706 arizona_hpdet_do_id(info, &hp_reading, &mic); 707 } 708 709 return; 710 711 err: 712 /* Just report headphone */ 713 snd_soc_jack_report(info->jack, SND_JACK_HEADPHONE, 714 SND_JACK_LINEOUT | SND_JACK_HEADPHONE); 715 716 info->hpdet_active = false; 717 } 718 719 static void arizona_micd_timeout_work(struct work_struct *work) 720 { 721 struct arizona_priv *info = container_of(work, 722 struct arizona_priv, 723 micd_timeout_work.work); 724 725 mutex_lock(&info->lock); 726 727 dev_dbg(info->arizona->dev, "MICD timed out, reporting HP\n"); 728 729 info->detecting = false; 730 731 arizona_identify_headphone(info); 732 733 mutex_unlock(&info->lock); 734 } 735 736 static int arizona_micd_adc_read(struct arizona_priv *info) 737 { 738 struct arizona *arizona = info->arizona; 739 unsigned int val; 740 int ret; 741 742 /* Must disable MICD before we read the ADCVAL */ 743 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1, 744 ARIZONA_MICD_ENA, 0); 745 746 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_4, &val); 747 if (ret) { 748 dev_err(arizona->dev, "Failed to read MICDET_ADCVAL: %d\n", ret); 749 return ret; 750 } 751 752 dev_dbg(arizona->dev, "MICDET_ADCVAL: %x\n", val); 753 754 val &= ARIZONA_MICDET_ADCVAL_MASK; 755 if (val < ARRAY_SIZE(arizona_micd_levels)) 756 val = arizona_micd_levels[val]; 757 else 758 val = INT_MAX; 759 760 if (val <= QUICK_HEADPHONE_MAX_OHM) 761 val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_0; 762 else if (val <= MICROPHONE_MIN_OHM) 763 val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_1; 764 else if (val <= MICROPHONE_MAX_OHM) 765 val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_8; 766 else 767 val = ARIZONA_MICD_LVL_8; 768 769 return val; 770 } 771 772 static int arizona_micd_read(struct arizona_priv *info) 773 { 774 struct arizona *arizona = info->arizona; 775 unsigned int val = 0; 776 int ret, i; 777 778 for (i = 0; i < 10 && !(val & MICD_LVL_0_TO_8); i++) { 779 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val); 780 if (ret) { 781 dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret); 782 return ret; 783 } 784 785 dev_dbg(arizona->dev, "MICDET: %x\n", val); 786 787 if (!(val & ARIZONA_MICD_VALID)) { 788 dev_warn(arizona->dev, "Microphone detection state invalid\n"); 789 return -EINVAL; 790 } 791 } 792 793 if (i == 10 && !(val & MICD_LVL_0_TO_8)) { 794 dev_err(arizona->dev, "Failed to get valid MICDET value\n"); 795 return -EINVAL; 796 } 797 798 return val; 799 } 800 801 static int arizona_micdet_reading(void *priv) 802 { 803 struct arizona_priv *info = priv; 804 struct arizona *arizona = info->arizona; 805 int ret, val; 806 807 if (info->detecting && arizona->pdata.micd_software_compare) 808 ret = arizona_micd_adc_read(info); 809 else 810 ret = arizona_micd_read(info); 811 if (ret < 0) 812 return ret; 813 814 val = ret; 815 816 /* Due to jack detect this should never happen */ 817 if (!(val & ARIZONA_MICD_STS)) { 818 dev_warn(arizona->dev, "Detected open circuit\n"); 819 info->mic = false; 820 info->detecting = false; 821 arizona_identify_headphone(info); 822 return 0; 823 } 824 825 /* If we got a high impedence we should have a headset, report it. */ 826 if (val & ARIZONA_MICD_LVL_8) { 827 info->mic = true; 828 info->detecting = false; 829 830 arizona_identify_headphone(info); 831 832 snd_soc_jack_report(info->jack, SND_JACK_MICROPHONE, SND_JACK_MICROPHONE); 833 834 /* Don't need to regulate for button detection */ 835 ret = regulator_allow_bypass(info->micvdd, true); 836 if (ret) 837 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n", ret); 838 839 return 0; 840 } 841 842 /* If we detected a lower impedence during initial startup 843 * then we probably have the wrong polarity, flip it. Don't 844 * do this for the lowest impedences to speed up detection of 845 * plain headphones. If both polarities report a low 846 * impedence then give up and report headphones. 847 */ 848 if (val & MICD_LVL_1_TO_7) { 849 if (info->jack_flips >= info->micd_num_modes * 10) { 850 dev_dbg(arizona->dev, "Detected HP/line\n"); 851 852 info->detecting = false; 853 854 arizona_identify_headphone(info); 855 } else { 856 info->micd_mode++; 857 if (info->micd_mode == info->micd_num_modes) 858 info->micd_mode = 0; 859 arizona_extcon_set_mode(info, info->micd_mode); 860 861 info->jack_flips++; 862 863 if (arizona->pdata.micd_software_compare) 864 regmap_update_bits(arizona->regmap, 865 ARIZONA_MIC_DETECT_1, 866 ARIZONA_MICD_ENA, 867 ARIZONA_MICD_ENA); 868 869 queue_delayed_work(system_power_efficient_wq, 870 &info->micd_timeout_work, 871 msecs_to_jiffies(arizona->pdata.micd_timeout)); 872 } 873 874 return 0; 875 } 876 877 /* 878 * If we're still detecting and we detect a short then we've 879 * got a headphone. 880 */ 881 dev_dbg(arizona->dev, "Headphone detected\n"); 882 info->detecting = false; 883 884 arizona_identify_headphone(info); 885 886 return 0; 887 } 888 889 static int arizona_button_reading(void *priv) 890 { 891 struct arizona_priv *info = priv; 892 struct arizona *arizona = info->arizona; 893 int val, key, lvl; 894 895 val = arizona_micd_read(info); 896 if (val < 0) 897 return val; 898 899 /* 900 * If we're still detecting and we detect a short then we've 901 * got a headphone. Otherwise it's a button press. 902 */ 903 if (val & MICD_LVL_0_TO_7) { 904 if (info->mic) { 905 dev_dbg(arizona->dev, "Mic button detected\n"); 906 907 lvl = val & ARIZONA_MICD_LVL_MASK; 908 lvl >>= ARIZONA_MICD_LVL_SHIFT; 909 910 if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) { 911 key = ffs(lvl) - 1; 912 snd_soc_jack_report(info->jack, 913 SND_JACK_BTN_0 >> key, 914 info->micd_button_mask); 915 } else { 916 dev_err(arizona->dev, "Button out of range\n"); 917 } 918 } else { 919 dev_warn(arizona->dev, "Button with no mic: %x\n", val); 920 } 921 } else { 922 dev_dbg(arizona->dev, "Mic button released\n"); 923 snd_soc_jack_report(info->jack, 0, info->micd_button_mask); 924 arizona_extcon_pulse_micbias(info); 925 } 926 927 return 0; 928 } 929 930 static void arizona_micd_detect(struct work_struct *work) 931 { 932 struct arizona_priv *info = container_of(work, 933 struct arizona_priv, 934 micd_detect_work.work); 935 struct arizona *arizona = info->arizona; 936 937 cancel_delayed_work_sync(&info->micd_timeout_work); 938 939 mutex_lock(&info->lock); 940 941 /* If the cable was removed while measuring ignore the result */ 942 if (!(info->jack->status & SND_JACK_MECHANICAL)) { 943 dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n"); 944 mutex_unlock(&info->lock); 945 return; 946 } 947 948 if (info->detecting) 949 arizona_micdet_reading(info); 950 else 951 arizona_button_reading(info); 952 953 pm_runtime_mark_last_busy(arizona->dev); 954 mutex_unlock(&info->lock); 955 } 956 957 static irqreturn_t arizona_micdet(int irq, void *data) 958 { 959 struct arizona_priv *info = data; 960 struct arizona *arizona = info->arizona; 961 int debounce = arizona->pdata.micd_detect_debounce; 962 963 cancel_delayed_work_sync(&info->micd_detect_work); 964 cancel_delayed_work_sync(&info->micd_timeout_work); 965 966 mutex_lock(&info->lock); 967 if (!info->detecting) 968 debounce = 0; 969 mutex_unlock(&info->lock); 970 971 if (debounce) 972 queue_delayed_work(system_power_efficient_wq, 973 &info->micd_detect_work, 974 msecs_to_jiffies(debounce)); 975 else 976 arizona_micd_detect(&info->micd_detect_work.work); 977 978 return IRQ_HANDLED; 979 } 980 981 static void arizona_hpdet_work(struct work_struct *work) 982 { 983 struct arizona_priv *info = container_of(work, 984 struct arizona_priv, 985 hpdet_work.work); 986 987 mutex_lock(&info->lock); 988 arizona_start_hpdet_acc_id(info); 989 mutex_unlock(&info->lock); 990 } 991 992 static int arizona_hpdet_wait(struct arizona_priv *info) 993 { 994 struct arizona *arizona = info->arizona; 995 unsigned int val; 996 int i, ret; 997 998 for (i = 0; i < ARIZONA_HPDET_WAIT_COUNT; i++) { 999 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, 1000 &val); 1001 if (ret) { 1002 dev_err(arizona->dev, "Failed to read HPDET state: %d\n", ret); 1003 return ret; 1004 } 1005 1006 switch (info->hpdet_ip_version) { 1007 case 0: 1008 if (val & ARIZONA_HP_DONE) 1009 return 0; 1010 break; 1011 default: 1012 if (val & ARIZONA_HP_DONE_B) 1013 return 0; 1014 break; 1015 } 1016 1017 msleep(ARIZONA_HPDET_WAIT_DELAY_MS); 1018 } 1019 1020 dev_warn(arizona->dev, "HPDET did not appear to complete\n"); 1021 1022 return -ETIMEDOUT; 1023 } 1024 1025 static irqreturn_t arizona_jackdet(int irq, void *data) 1026 { 1027 struct arizona_priv *info = data; 1028 struct arizona *arizona = info->arizona; 1029 unsigned int val, present, mask; 1030 bool cancelled_hp, cancelled_mic; 1031 int ret, i; 1032 1033 cancelled_hp = cancel_delayed_work_sync(&info->hpdet_work); 1034 cancelled_mic = cancel_delayed_work_sync(&info->micd_timeout_work); 1035 1036 pm_runtime_get_sync(arizona->dev); 1037 1038 mutex_lock(&info->lock); 1039 1040 if (info->micd_clamp) { 1041 mask = ARIZONA_MICD_CLAMP_STS; 1042 present = 0; 1043 } else { 1044 mask = ARIZONA_JD1_STS; 1045 if (arizona->pdata.jd_invert) 1046 present = 0; 1047 else 1048 present = ARIZONA_JD1_STS; 1049 } 1050 1051 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val); 1052 if (ret) { 1053 dev_err(arizona->dev, "Failed to read jackdet status: %d\n", ret); 1054 mutex_unlock(&info->lock); 1055 pm_runtime_put_autosuspend(arizona->dev); 1056 return IRQ_NONE; 1057 } 1058 1059 val &= mask; 1060 if (val == info->last_jackdet) { 1061 dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n"); 1062 if (cancelled_hp) 1063 queue_delayed_work(system_power_efficient_wq, 1064 &info->hpdet_work, 1065 msecs_to_jiffies(HPDET_DEBOUNCE)); 1066 1067 if (cancelled_mic) { 1068 int micd_timeout = arizona->pdata.micd_timeout; 1069 1070 queue_delayed_work(system_power_efficient_wq, 1071 &info->micd_timeout_work, 1072 msecs_to_jiffies(micd_timeout)); 1073 } 1074 1075 goto out; 1076 } 1077 info->last_jackdet = val; 1078 1079 if (info->last_jackdet == present) { 1080 dev_dbg(arizona->dev, "Detected jack\n"); 1081 snd_soc_jack_report(info->jack, SND_JACK_MECHANICAL, SND_JACK_MECHANICAL); 1082 1083 info->detecting = true; 1084 info->mic = false; 1085 info->jack_flips = 0; 1086 1087 if (!arizona->pdata.hpdet_acc_id) { 1088 arizona_start_mic(info); 1089 } else { 1090 queue_delayed_work(system_power_efficient_wq, 1091 &info->hpdet_work, 1092 msecs_to_jiffies(HPDET_DEBOUNCE)); 1093 } 1094 1095 if (info->micd_clamp || !arizona->pdata.jd_invert) 1096 regmap_update_bits(arizona->regmap, 1097 ARIZONA_JACK_DETECT_DEBOUNCE, 1098 ARIZONA_MICD_CLAMP_DB | 1099 ARIZONA_JD1_DB, 0); 1100 } else { 1101 dev_dbg(arizona->dev, "Detected jack removal\n"); 1102 1103 arizona_stop_mic(info); 1104 1105 info->num_hpdet_res = 0; 1106 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++) 1107 info->hpdet_res[i] = 0; 1108 info->mic = false; 1109 info->hpdet_done = false; 1110 info->hpdet_retried = false; 1111 1112 snd_soc_jack_report(info->jack, 0, ARIZONA_JACK_MASK | info->micd_button_mask); 1113 1114 /* 1115 * If the jack was removed during a headphone detection we 1116 * need to wait for the headphone detection to finish, as 1117 * it can not be aborted. We don't want to be able to start 1118 * a new headphone detection from a fresh insert until this 1119 * one is finished. 1120 */ 1121 arizona_hpdet_wait(info); 1122 1123 regmap_update_bits(arizona->regmap, 1124 ARIZONA_JACK_DETECT_DEBOUNCE, 1125 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 1126 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB); 1127 } 1128 1129 out: 1130 /* Clear trig_sts to make sure DCVDD is not forced up */ 1131 regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG, 1132 ARIZONA_MICD_CLAMP_FALL_TRIG_STS | 1133 ARIZONA_MICD_CLAMP_RISE_TRIG_STS | 1134 ARIZONA_JD1_FALL_TRIG_STS | 1135 ARIZONA_JD1_RISE_TRIG_STS); 1136 1137 mutex_unlock(&info->lock); 1138 1139 pm_runtime_put_autosuspend(arizona->dev); 1140 1141 return IRQ_HANDLED; 1142 } 1143 1144 /* Map a level onto a slot in the register bank */ 1145 static void arizona_micd_set_level(struct arizona *arizona, int index, 1146 unsigned int level) 1147 { 1148 int reg; 1149 unsigned int mask; 1150 1151 reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2); 1152 1153 if (!(index % 2)) { 1154 mask = 0x3f00; 1155 level <<= 8; 1156 } else { 1157 mask = 0x3f; 1158 } 1159 1160 /* Program the level itself */ 1161 regmap_update_bits(arizona->regmap, reg, mask, level); 1162 } 1163 1164 static int arizona_extcon_get_micd_configs(struct device *dev, 1165 struct arizona *arizona) 1166 { 1167 const char * const prop = "wlf,micd-configs"; 1168 const int entries_per_config = 3; 1169 struct arizona_micd_config *micd_configs; 1170 int nconfs, ret; 1171 int i, j; 1172 u32 *vals; 1173 1174 nconfs = device_property_count_u32(arizona->dev, prop); 1175 if (nconfs <= 0) 1176 return 0; 1177 1178 vals = kcalloc(nconfs, sizeof(u32), GFP_KERNEL); 1179 if (!vals) 1180 return -ENOMEM; 1181 1182 ret = device_property_read_u32_array(arizona->dev, prop, vals, nconfs); 1183 if (ret < 0) 1184 goto out; 1185 1186 nconfs /= entries_per_config; 1187 micd_configs = devm_kcalloc(dev, nconfs, sizeof(*micd_configs), 1188 GFP_KERNEL); 1189 if (!micd_configs) { 1190 ret = -ENOMEM; 1191 goto out; 1192 } 1193 1194 for (i = 0, j = 0; i < nconfs; ++i) { 1195 micd_configs[i].src = vals[j++] ? ARIZONA_ACCDET_SRC : 0; 1196 micd_configs[i].bias = vals[j++]; 1197 micd_configs[i].gpio = vals[j++]; 1198 } 1199 1200 arizona->pdata.micd_configs = micd_configs; 1201 arizona->pdata.num_micd_configs = nconfs; 1202 1203 out: 1204 kfree(vals); 1205 return ret; 1206 } 1207 1208 static int arizona_extcon_device_get_pdata(struct device *dev, 1209 struct arizona *arizona) 1210 { 1211 struct arizona_pdata *pdata = &arizona->pdata; 1212 unsigned int val = ARIZONA_ACCDET_MODE_HPL; 1213 int ret; 1214 1215 device_property_read_u32(arizona->dev, "wlf,hpdet-channel", &val); 1216 switch (val) { 1217 case ARIZONA_ACCDET_MODE_HPL: 1218 case ARIZONA_ACCDET_MODE_HPR: 1219 pdata->hpdet_channel = val; 1220 break; 1221 default: 1222 dev_err(arizona->dev, "Wrong wlf,hpdet-channel DT value %d\n", val); 1223 pdata->hpdet_channel = ARIZONA_ACCDET_MODE_HPL; 1224 } 1225 1226 device_property_read_u32(arizona->dev, "wlf,micd-detect-debounce", 1227 &pdata->micd_detect_debounce); 1228 1229 device_property_read_u32(arizona->dev, "wlf,micd-bias-start-time", 1230 &pdata->micd_bias_start_time); 1231 1232 device_property_read_u32(arizona->dev, "wlf,micd-rate", 1233 &pdata->micd_rate); 1234 1235 device_property_read_u32(arizona->dev, "wlf,micd-dbtime", 1236 &pdata->micd_dbtime); 1237 1238 device_property_read_u32(arizona->dev, "wlf,micd-timeout-ms", 1239 &pdata->micd_timeout); 1240 1241 pdata->micd_force_micbias = device_property_read_bool(arizona->dev, 1242 "wlf,micd-force-micbias"); 1243 1244 pdata->micd_software_compare = device_property_read_bool(arizona->dev, 1245 "wlf,micd-software-compare"); 1246 1247 pdata->jd_invert = device_property_read_bool(arizona->dev, 1248 "wlf,jd-invert"); 1249 1250 device_property_read_u32(arizona->dev, "wlf,gpsw", &pdata->gpsw); 1251 1252 pdata->jd_gpio5 = device_property_read_bool(arizona->dev, 1253 "wlf,use-jd2"); 1254 pdata->jd_gpio5_nopull = device_property_read_bool(arizona->dev, 1255 "wlf,use-jd2-nopull"); 1256 1257 ret = arizona_extcon_get_micd_configs(dev, arizona); 1258 if (ret < 0) 1259 dev_err(arizona->dev, "Failed to read micd configs: %d\n", ret); 1260 1261 return 0; 1262 } 1263 1264 int arizona_jack_codec_dev_probe(struct arizona_priv *info, struct device *dev) 1265 { 1266 struct arizona *arizona = info->arizona; 1267 struct arizona_pdata *pdata = &arizona->pdata; 1268 int ret, mode; 1269 1270 if (!dev_get_platdata(arizona->dev)) 1271 arizona_extcon_device_get_pdata(dev, arizona); 1272 1273 info->micvdd = devm_regulator_get(dev, "MICVDD"); 1274 if (IS_ERR(info->micvdd)) 1275 return dev_err_probe(arizona->dev, PTR_ERR(info->micvdd), "getting MICVDD\n"); 1276 1277 mutex_init(&info->lock); 1278 info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS); 1279 INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work); 1280 INIT_DELAYED_WORK(&info->micd_detect_work, arizona_micd_detect); 1281 INIT_DELAYED_WORK(&info->micd_timeout_work, arizona_micd_timeout_work); 1282 1283 switch (arizona->type) { 1284 case WM5102: 1285 switch (arizona->rev) { 1286 case 0: 1287 info->micd_reva = true; 1288 break; 1289 default: 1290 info->micd_clamp = true; 1291 info->hpdet_ip_version = 1; 1292 break; 1293 } 1294 break; 1295 case WM5110: 1296 case WM8280: 1297 switch (arizona->rev) { 1298 case 0 ... 2: 1299 break; 1300 default: 1301 info->micd_clamp = true; 1302 info->hpdet_ip_version = 2; 1303 break; 1304 } 1305 break; 1306 case WM8998: 1307 case WM1814: 1308 info->micd_clamp = true; 1309 info->hpdet_ip_version = 2; 1310 break; 1311 default: 1312 break; 1313 } 1314 1315 if (!pdata->micd_timeout) 1316 pdata->micd_timeout = DEFAULT_MICD_TIMEOUT; 1317 1318 if (pdata->num_micd_configs) { 1319 info->micd_modes = pdata->micd_configs; 1320 info->micd_num_modes = pdata->num_micd_configs; 1321 } else { 1322 info->micd_modes = micd_default_modes; 1323 info->micd_num_modes = ARRAY_SIZE(micd_default_modes); 1324 } 1325 1326 if (arizona->pdata.gpsw > 0) 1327 regmap_update_bits(arizona->regmap, ARIZONA_GP_SWITCH_1, 1328 ARIZONA_SW1_MODE_MASK, arizona->pdata.gpsw); 1329 1330 #ifdef CONFIG_GPIOLIB_LEGACY 1331 if (pdata->micd_pol_gpio > 0) { 1332 if (info->micd_modes[0].gpio) 1333 mode = GPIOF_OUT_INIT_HIGH; 1334 else 1335 mode = GPIOF_OUT_INIT_LOW; 1336 1337 ret = devm_gpio_request_one(dev, pdata->micd_pol_gpio, 1338 mode, "MICD polarity"); 1339 if (ret != 0) { 1340 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n", 1341 pdata->micd_pol_gpio, ret); 1342 return ret; 1343 } 1344 1345 info->micd_pol_gpio = gpio_to_desc(pdata->micd_pol_gpio); 1346 } else 1347 #endif 1348 { 1349 if (info->micd_modes[0].gpio) 1350 mode = GPIOD_OUT_HIGH; 1351 else 1352 mode = GPIOD_OUT_LOW; 1353 1354 /* We can't use devm here because we need to do the get 1355 * against the MFD device, as that is where the of_node 1356 * will reside, but if we devm against that the GPIO 1357 * will not be freed if the extcon driver is unloaded. 1358 */ 1359 info->micd_pol_gpio = gpiod_get_optional(arizona->dev, 1360 "wlf,micd-pol", 1361 mode); 1362 if (IS_ERR(info->micd_pol_gpio)) { 1363 ret = PTR_ERR(info->micd_pol_gpio); 1364 dev_err_probe(arizona->dev, ret, "getting microphone polarity GPIO\n"); 1365 return ret; 1366 } 1367 } 1368 1369 #ifdef CONFIG_GPIOLIB_LEGACY 1370 if (arizona->pdata.hpdet_id_gpio > 0) { 1371 ret = devm_gpio_request_one(dev, arizona->pdata.hpdet_id_gpio, 1372 GPIOF_OUT_INIT_LOW, 1373 "HPDET"); 1374 if (ret != 0) { 1375 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n", 1376 arizona->pdata.hpdet_id_gpio, ret); 1377 gpiod_put(info->micd_pol_gpio); 1378 return ret; 1379 } 1380 } 1381 #endif 1382 1383 return 0; 1384 } 1385 EXPORT_SYMBOL_GPL(arizona_jack_codec_dev_probe); 1386 1387 int arizona_jack_codec_dev_remove(struct arizona_priv *info) 1388 { 1389 gpiod_put(info->micd_pol_gpio); 1390 return 0; 1391 } 1392 EXPORT_SYMBOL_GPL(arizona_jack_codec_dev_remove); 1393 1394 static int arizona_jack_enable_jack_detect(struct arizona_priv *info, 1395 struct snd_soc_jack *jack) 1396 { 1397 struct arizona *arizona = info->arizona; 1398 struct arizona_pdata *pdata = &arizona->pdata; 1399 unsigned int val; 1400 unsigned int clamp_mode; 1401 int jack_irq_fall, jack_irq_rise; 1402 int ret, i, j; 1403 1404 if (arizona->pdata.micd_bias_start_time) 1405 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1, 1406 ARIZONA_MICD_BIAS_STARTTIME_MASK, 1407 arizona->pdata.micd_bias_start_time 1408 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT); 1409 1410 if (arizona->pdata.micd_rate) 1411 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1, 1412 ARIZONA_MICD_RATE_MASK, 1413 arizona->pdata.micd_rate 1414 << ARIZONA_MICD_RATE_SHIFT); 1415 1416 switch (arizona->pdata.micd_dbtime) { 1417 case MICD_DBTIME_FOUR_READINGS: 1418 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1, 1419 ARIZONA_MICD_DBTIME_MASK, 1420 ARIZONA_MICD_DBTIME); 1421 break; 1422 case MICD_DBTIME_TWO_READINGS: 1423 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1, 1424 ARIZONA_MICD_DBTIME_MASK, 0); 1425 break; 1426 default: 1427 break; 1428 } 1429 1430 BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) < 1431 ARIZONA_NUM_MICD_BUTTON_LEVELS); 1432 1433 if (arizona->pdata.num_micd_ranges) { 1434 info->micd_ranges = pdata->micd_ranges; 1435 info->num_micd_ranges = pdata->num_micd_ranges; 1436 } else { 1437 info->micd_ranges = micd_default_ranges; 1438 info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges); 1439 } 1440 1441 if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_BUTTONS) { 1442 dev_err(arizona->dev, "Too many MICD ranges: %d > %d\n", 1443 arizona->pdata.num_micd_ranges, ARIZONA_MAX_MICD_BUTTONS); 1444 return -EINVAL; 1445 } 1446 1447 if (info->num_micd_ranges > 1) { 1448 for (i = 1; i < info->num_micd_ranges; i++) { 1449 if (info->micd_ranges[i - 1].max > 1450 info->micd_ranges[i].max) { 1451 dev_err(arizona->dev, "MICD ranges must be sorted\n"); 1452 return -EINVAL; 1453 } 1454 } 1455 } 1456 1457 /* Disable all buttons by default */ 1458 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2, 1459 ARIZONA_MICD_LVL_SEL_MASK, 0x81); 1460 1461 /* Set up all the buttons the user specified */ 1462 for (i = 0; i < info->num_micd_ranges; i++) { 1463 for (j = 0; j < ARIZONA_NUM_MICD_BUTTON_LEVELS; j++) 1464 if (arizona_micd_levels[j] >= info->micd_ranges[i].max) 1465 break; 1466 1467 if (j == ARIZONA_NUM_MICD_BUTTON_LEVELS) { 1468 dev_err(arizona->dev, "Unsupported MICD level %d\n", 1469 info->micd_ranges[i].max); 1470 return -EINVAL; 1471 } 1472 1473 dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n", 1474 arizona_micd_levels[j], i); 1475 1476 arizona_micd_set_level(arizona, i, j); 1477 1478 /* SND_JACK_BTN_# masks start with the most significant bit */ 1479 info->micd_button_mask |= SND_JACK_BTN_0 >> i; 1480 snd_jack_set_key(jack->jack, SND_JACK_BTN_0 >> i, 1481 info->micd_ranges[i].key); 1482 1483 /* Enable reporting of that range */ 1484 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2, 1485 1 << i, 1 << i); 1486 } 1487 1488 /* Set all the remaining keys to a maximum */ 1489 for (; i < ARIZONA_MAX_MICD_RANGE; i++) 1490 arizona_micd_set_level(arizona, i, 0x3f); 1491 1492 /* 1493 * If we have a clamp use it, activating in conjunction with 1494 * GPIO5 if that is connected for jack detect operation. 1495 */ 1496 if (info->micd_clamp) { 1497 if (arizona->pdata.jd_gpio5) { 1498 /* Put the GPIO into input mode with optional pull */ 1499 val = 0xc101; 1500 if (arizona->pdata.jd_gpio5_nopull) 1501 val &= ~ARIZONA_GPN_PU; 1502 1503 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL, 1504 val); 1505 1506 if (arizona->pdata.jd_invert) 1507 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH_GP5H; 1508 else 1509 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL_GP5H; 1510 } else { 1511 if (arizona->pdata.jd_invert) 1512 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH; 1513 else 1514 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL; 1515 } 1516 1517 regmap_update_bits(arizona->regmap, 1518 ARIZONA_MICD_CLAMP_CONTROL, 1519 ARIZONA_MICD_CLAMP_MODE_MASK, clamp_mode); 1520 1521 regmap_update_bits(arizona->regmap, 1522 ARIZONA_JACK_DETECT_DEBOUNCE, 1523 ARIZONA_MICD_CLAMP_DB, 1524 ARIZONA_MICD_CLAMP_DB); 1525 } 1526 1527 arizona_extcon_set_mode(info, 0); 1528 1529 info->jack = jack; 1530 1531 pm_runtime_get_sync(arizona->dev); 1532 1533 if (info->micd_clamp) { 1534 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE; 1535 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL; 1536 } else { 1537 jack_irq_rise = ARIZONA_IRQ_JD_RISE; 1538 jack_irq_fall = ARIZONA_IRQ_JD_FALL; 1539 } 1540 1541 ret = arizona_request_irq(arizona, jack_irq_rise, 1542 "JACKDET rise", arizona_jackdet, info); 1543 if (ret != 0) { 1544 dev_err(arizona->dev, "Failed to get JACKDET rise IRQ: %d\n", ret); 1545 goto err_pm; 1546 } 1547 1548 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1); 1549 if (ret != 0) { 1550 dev_err(arizona->dev, "Failed to set JD rise IRQ wake: %d\n", ret); 1551 goto err_rise; 1552 } 1553 1554 ret = arizona_request_irq(arizona, jack_irq_fall, 1555 "JACKDET fall", arizona_jackdet, info); 1556 if (ret != 0) { 1557 dev_err(arizona->dev, "Failed to get JD fall IRQ: %d\n", ret); 1558 goto err_rise_wake; 1559 } 1560 1561 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1); 1562 if (ret != 0) { 1563 dev_err(arizona->dev, "Failed to set JD fall IRQ wake: %d\n", ret); 1564 goto err_fall; 1565 } 1566 1567 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET, 1568 "MICDET", arizona_micdet, info); 1569 if (ret != 0) { 1570 dev_err(arizona->dev, "Failed to get MICDET IRQ: %d\n", ret); 1571 goto err_fall_wake; 1572 } 1573 1574 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET, 1575 "HPDET", arizona_hpdet_irq, info); 1576 if (ret != 0) { 1577 dev_err(arizona->dev, "Failed to get HPDET IRQ: %d\n", ret); 1578 goto err_micdet; 1579 } 1580 1581 arizona_clk32k_enable(arizona); 1582 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE, 1583 ARIZONA_JD1_DB, ARIZONA_JD1_DB); 1584 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE, 1585 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA); 1586 1587 ret = regulator_allow_bypass(info->micvdd, true); 1588 if (ret != 0) 1589 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n", ret); 1590 1591 pm_runtime_put(arizona->dev); 1592 1593 return 0; 1594 1595 err_micdet: 1596 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info); 1597 err_fall_wake: 1598 arizona_set_irq_wake(arizona, jack_irq_fall, 0); 1599 err_fall: 1600 arizona_free_irq(arizona, jack_irq_fall, info); 1601 err_rise_wake: 1602 arizona_set_irq_wake(arizona, jack_irq_rise, 0); 1603 err_rise: 1604 arizona_free_irq(arizona, jack_irq_rise, info); 1605 err_pm: 1606 pm_runtime_put(arizona->dev); 1607 info->jack = NULL; 1608 return ret; 1609 } 1610 1611 static int arizona_jack_disable_jack_detect(struct arizona_priv *info) 1612 { 1613 struct arizona *arizona = info->arizona; 1614 int jack_irq_rise, jack_irq_fall; 1615 bool change; 1616 int ret; 1617 1618 if (!info->jack) 1619 return 0; 1620 1621 if (info->micd_clamp) { 1622 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE; 1623 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL; 1624 } else { 1625 jack_irq_rise = ARIZONA_IRQ_JD_RISE; 1626 jack_irq_fall = ARIZONA_IRQ_JD_FALL; 1627 } 1628 1629 arizona_set_irq_wake(arizona, jack_irq_rise, 0); 1630 arizona_set_irq_wake(arizona, jack_irq_fall, 0); 1631 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info); 1632 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info); 1633 arizona_free_irq(arizona, jack_irq_rise, info); 1634 arizona_free_irq(arizona, jack_irq_fall, info); 1635 cancel_delayed_work_sync(&info->hpdet_work); 1636 cancel_delayed_work_sync(&info->micd_detect_work); 1637 cancel_delayed_work_sync(&info->micd_timeout_work); 1638 1639 ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1, 1640 ARIZONA_MICD_ENA, 0, 1641 &change); 1642 if (ret < 0) { 1643 dev_err(arizona->dev, "Failed to disable micd on remove: %d\n", ret); 1644 } else if (change) { 1645 regulator_disable(info->micvdd); 1646 pm_runtime_put(arizona->dev); 1647 } 1648 1649 regmap_update_bits(arizona->regmap, 1650 ARIZONA_MICD_CLAMP_CONTROL, 1651 ARIZONA_MICD_CLAMP_MODE_MASK, 0); 1652 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE, 1653 ARIZONA_JD1_ENA, 0); 1654 arizona_clk32k_disable(arizona); 1655 info->jack = NULL; 1656 1657 return 0; 1658 } 1659 1660 int arizona_jack_set_jack(struct snd_soc_component *component, 1661 struct snd_soc_jack *jack, void *data) 1662 { 1663 struct arizona_priv *info = snd_soc_component_get_drvdata(component); 1664 1665 if (jack) 1666 return arizona_jack_enable_jack_detect(info, jack); 1667 else 1668 return arizona_jack_disable_jack_detect(info); 1669 } 1670 EXPORT_SYMBOL_GPL(arizona_jack_set_jack); 1671