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