1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * extcon-arizona.c - Extcon driver Wolfson Arizona devices
5 * Copyright (C) 2012-2014 Wolfson Microelectronics plc
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/i2c.h>
11 #include <linux/slab.h>
12 #include <linux/interrupt.h>
13 #include <linux/err.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/gpio.h>
16 #include <linux/input.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/property.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/extcon-provider.h>
23 #include <sound/soc.h>
25 #include <linux/mfd/arizona/core.h>
26 #include <linux/mfd/arizona/pdata.h>
27 #include <linux/mfd/arizona/registers.h>
28 #include <dt-bindings/mfd/arizona.h>
30 #define ARIZONA_MAX_MICD_RANGE 8
32 #define ARIZONA_MICD_CLAMP_MODE_JDL 0x4
33 #define ARIZONA_MICD_CLAMP_MODE_JDH 0x5
34 #define ARIZONA_MICD_CLAMP_MODE_JDL_GP5H 0x9
35 #define ARIZONA_MICD_CLAMP_MODE_JDH_GP5H 0xb
37 #define ARIZONA_TST_CAP_DEFAULT 0x3
38 #define ARIZONA_TST_CAP_CLAMP 0x1
40 #define ARIZONA_HPDET_MAX 10000
42 #define HPDET_DEBOUNCE 500
43 #define DEFAULT_MICD_TIMEOUT 2000
45 #define ARIZONA_HPDET_WAIT_COUNT 15
46 #define ARIZONA_HPDET_WAIT_DELAY_MS 20
48 #define QUICK_HEADPHONE_MAX_OHM 3
49 #define MICROPHONE_MIN_OHM 1257
50 #define MICROPHONE_MAX_OHM 30000
52 #define MICD_DBTIME_TWO_READINGS 2
53 #define MICD_DBTIME_FOUR_READINGS 4
55 #define MICD_LVL_1_TO_7 (ARIZONA_MICD_LVL_1 | ARIZONA_MICD_LVL_2 | \
56 ARIZONA_MICD_LVL_3 | ARIZONA_MICD_LVL_4 | \
57 ARIZONA_MICD_LVL_5 | ARIZONA_MICD_LVL_6 | \
60 #define MICD_LVL_0_TO_7 (ARIZONA_MICD_LVL_0 | MICD_LVL_1_TO_7)
62 #define MICD_LVL_0_TO_8 (MICD_LVL_0_TO_7 | ARIZONA_MICD_LVL_8)
64 struct arizona_extcon_info {
66 struct arizona *arizona;
68 struct regulator *micvdd;
69 struct input_dev *input;
74 const struct arizona_micd_config *micd_modes;
77 const struct arizona_micd_range *micd_ranges;
85 struct delayed_work hpdet_work;
86 struct delayed_work micd_detect_work;
87 struct delayed_work micd_timeout_work;
94 unsigned int hpdet_res[3];
100 int hpdet_ip_version;
102 struct extcon_dev *edev;
104 struct gpio_desc *micd_pol_gpio;
107 static const struct arizona_micd_config micd_default_modes[] = {
108 { ARIZONA_ACCDET_SRC, 1, 0 },
112 static const struct arizona_micd_range micd_default_ranges[] = {
113 { .max = 11, .key = BTN_0 },
114 { .max = 28, .key = BTN_1 },
115 { .max = 54, .key = BTN_2 },
116 { .max = 100, .key = BTN_3 },
117 { .max = 186, .key = BTN_4 },
118 { .max = 430, .key = BTN_5 },
121 /* The number of levels in arizona_micd_levels valid for button thresholds */
122 #define ARIZONA_NUM_MICD_BUTTON_LEVELS 64
124 static const int arizona_micd_levels[] = {
125 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
126 49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
127 105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
128 270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
132 static const unsigned int arizona_cable[] = {
134 EXTCON_JACK_MICROPHONE,
135 EXTCON_JACK_HEADPHONE,
136 EXTCON_JACK_LINE_OUT,
140 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info);
142 static void arizona_extcon_hp_clamp(struct arizona_extcon_info *info,
145 struct arizona *arizona = info->arizona;
146 unsigned int mask = 0, val = 0;
147 unsigned int cap_sel = 0;
150 switch (arizona->type) {
157 mask = ARIZONA_HP1L_SHRTO | ARIZONA_HP1L_FLWR |
160 val = ARIZONA_HP1L_SHRTO;
161 cap_sel = ARIZONA_TST_CAP_CLAMP;
163 val = ARIZONA_HP1L_FLWR | ARIZONA_HP1L_SHRTI;
164 cap_sel = ARIZONA_TST_CAP_DEFAULT;
167 ret = regmap_update_bits(arizona->regmap,
168 ARIZONA_HP_TEST_CTRL_1,
169 ARIZONA_HP1_TST_CAP_SEL_MASK,
172 dev_warn(arizona->dev,
173 "Failed to set TST_CAP_SEL: %d\n", ret);
176 mask = ARIZONA_RMV_SHRT_HP1L;
178 val = ARIZONA_RMV_SHRT_HP1L;
182 snd_soc_dapm_mutex_lock(arizona->dapm);
184 arizona->hpdet_clamp = clamp;
186 /* Keep the HP output stages disabled while doing the clamp */
188 ret = regmap_update_bits(arizona->regmap,
189 ARIZONA_OUTPUT_ENABLES_1,
191 ARIZONA_OUT1R_ENA, 0);
193 dev_warn(arizona->dev,
194 "Failed to disable headphone outputs: %d\n",
199 ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1L,
202 dev_warn(arizona->dev, "Failed to do clamp: %d\n",
205 ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1R,
208 dev_warn(arizona->dev, "Failed to do clamp: %d\n",
212 /* Restore the desired state while not doing the clamp */
214 ret = regmap_update_bits(arizona->regmap,
215 ARIZONA_OUTPUT_ENABLES_1,
217 ARIZONA_OUT1R_ENA, arizona->hp_ena);
219 dev_warn(arizona->dev,
220 "Failed to restore headphone outputs: %d\n",
224 snd_soc_dapm_mutex_unlock(arizona->dapm);
227 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
229 struct arizona *arizona = info->arizona;
231 mode %= info->micd_num_modes;
233 gpiod_set_value_cansleep(info->micd_pol_gpio,
234 info->micd_modes[mode].gpio);
236 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
237 ARIZONA_MICD_BIAS_SRC_MASK,
238 info->micd_modes[mode].bias <<
239 ARIZONA_MICD_BIAS_SRC_SHIFT);
240 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
241 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
243 info->micd_mode = mode;
245 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
248 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
250 switch (info->micd_modes[0].bias) {
262 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
264 struct arizona *arizona = info->arizona;
265 const char *widget = arizona_extcon_get_micbias(info);
266 struct snd_soc_dapm_context *dapm = arizona->dapm;
267 struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
270 ret = snd_soc_component_force_enable_pin(component, widget);
272 dev_warn(arizona->dev, "Failed to enable %s: %d\n",
275 snd_soc_dapm_sync(dapm);
277 if (!arizona->pdata.micd_force_micbias) {
278 ret = snd_soc_component_disable_pin(component, widget);
280 dev_warn(arizona->dev, "Failed to disable %s: %d\n",
283 snd_soc_dapm_sync(dapm);
287 static void arizona_start_mic(struct arizona_extcon_info *info)
289 struct arizona *arizona = info->arizona;
294 /* Microphone detection can't use idle mode */
295 pm_runtime_get(info->dev);
297 if (info->detecting) {
298 ret = regulator_allow_bypass(info->micvdd, false);
300 dev_err(arizona->dev,
301 "Failed to regulate MICVDD: %d\n",
306 ret = regulator_enable(info->micvdd);
308 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
312 if (info->micd_reva) {
313 regmap_write(arizona->regmap, 0x80, 0x3);
314 regmap_write(arizona->regmap, 0x294, 0);
315 regmap_write(arizona->regmap, 0x80, 0x0);
318 if (info->detecting && arizona->pdata.micd_software_compare)
319 mode = ARIZONA_ACCDET_MODE_ADC;
321 mode = ARIZONA_ACCDET_MODE_MIC;
323 regmap_update_bits(arizona->regmap,
324 ARIZONA_ACCESSORY_DETECT_MODE_1,
325 ARIZONA_ACCDET_MODE_MASK, mode);
327 arizona_extcon_pulse_micbias(info);
329 ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
330 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
333 dev_err(arizona->dev, "Failed to enable micd: %d\n", ret);
334 } else if (!change) {
335 regulator_disable(info->micvdd);
336 pm_runtime_put_autosuspend(info->dev);
340 static void arizona_stop_mic(struct arizona_extcon_info *info)
342 struct arizona *arizona = info->arizona;
343 const char *widget = arizona_extcon_get_micbias(info);
344 struct snd_soc_dapm_context *dapm = arizona->dapm;
345 struct snd_soc_component *component = snd_soc_dapm_to_component(dapm);
349 ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
353 dev_err(arizona->dev, "Failed to disable micd: %d\n", ret);
355 ret = snd_soc_component_disable_pin(component, widget);
357 dev_warn(arizona->dev,
358 "Failed to disable %s: %d\n",
361 snd_soc_dapm_sync(dapm);
363 if (info->micd_reva) {
364 regmap_write(arizona->regmap, 0x80, 0x3);
365 regmap_write(arizona->regmap, 0x294, 2);
366 regmap_write(arizona->regmap, 0x80, 0x0);
369 ret = regulator_allow_bypass(info->micvdd, true);
371 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
376 regulator_disable(info->micvdd);
377 pm_runtime_mark_last_busy(info->dev);
378 pm_runtime_put_autosuspend(info->dev);
383 unsigned int threshold;
384 unsigned int factor_a;
385 unsigned int factor_b;
386 } arizona_hpdet_b_ranges[] = {
387 { 100, 5528, 362464 },
388 { 169, 11084, 6186851 },
389 { 169, 11065, 65460395 },
392 #define ARIZONA_HPDET_B_RANGE_MAX 0x3fb
397 } arizona_hpdet_c_ranges[] = {
404 static int arizona_hpdet_read(struct arizona_extcon_info *info)
406 struct arizona *arizona = info->arizona;
407 unsigned int val, range;
410 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
412 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
417 switch (info->hpdet_ip_version) {
419 if (!(val & ARIZONA_HP_DONE)) {
420 dev_err(arizona->dev, "HPDET did not complete: %x\n",
425 val &= ARIZONA_HP_LVL_MASK;
429 if (!(val & ARIZONA_HP_DONE_B)) {
430 dev_err(arizona->dev, "HPDET did not complete: %x\n",
435 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
437 dev_err(arizona->dev, "Failed to read HP value: %d\n",
442 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
444 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
445 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
447 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
448 (val < arizona_hpdet_b_ranges[range].threshold ||
449 val >= ARIZONA_HPDET_B_RANGE_MAX)) {
451 dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
453 regmap_update_bits(arizona->regmap,
454 ARIZONA_HEADPHONE_DETECT_1,
455 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
457 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
461 /* If we go out of range report top of range */
462 if (val < arizona_hpdet_b_ranges[range].threshold ||
463 val >= ARIZONA_HPDET_B_RANGE_MAX) {
464 dev_dbg(arizona->dev, "Measurement out of range\n");
465 return ARIZONA_HPDET_MAX;
468 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
471 val = arizona_hpdet_b_ranges[range].factor_b
473 arizona_hpdet_b_ranges[range].factor_a);
477 if (!(val & ARIZONA_HP_DONE_B)) {
478 dev_err(arizona->dev, "HPDET did not complete: %x\n",
483 val &= ARIZONA_HP_LVL_B_MASK;
484 /* Convert to ohms, the value is in 0.5 ohm increments */
487 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
489 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
490 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
492 /* Skip up a range, or report? */
493 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
494 (val >= arizona_hpdet_c_ranges[range].max)) {
496 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
497 arizona_hpdet_c_ranges[range].min,
498 arizona_hpdet_c_ranges[range].max);
499 regmap_update_bits(arizona->regmap,
500 ARIZONA_HEADPHONE_DETECT_1,
501 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
503 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
507 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
508 dev_dbg(arizona->dev, "Reporting range boundary %d\n",
509 arizona_hpdet_c_ranges[range].min);
510 val = arizona_hpdet_c_ranges[range].min;
515 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
516 info->hpdet_ip_version);
520 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
524 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading,
527 struct arizona *arizona = info->arizona;
528 int id_gpio = arizona->pdata.hpdet_id_gpio;
531 * If we're using HPDET for accessory identification we need
532 * to take multiple measurements, step through them in sequence.
534 if (arizona->pdata.hpdet_acc_id) {
535 info->hpdet_res[info->num_hpdet_res++] = *reading;
537 /* Only check the mic directly if we didn't already ID it */
538 if (id_gpio && info->num_hpdet_res == 1) {
539 dev_dbg(arizona->dev, "Measuring mic\n");
541 regmap_update_bits(arizona->regmap,
542 ARIZONA_ACCESSORY_DETECT_MODE_1,
543 ARIZONA_ACCDET_MODE_MASK |
545 ARIZONA_ACCDET_MODE_HPR |
546 info->micd_modes[0].src);
548 gpio_set_value_cansleep(id_gpio, 1);
550 regmap_update_bits(arizona->regmap,
551 ARIZONA_HEADPHONE_DETECT_1,
552 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
556 /* OK, got both. Now, compare... */
557 dev_dbg(arizona->dev, "HPDET measured %d %d\n",
558 info->hpdet_res[0], info->hpdet_res[1]);
560 /* Take the headphone impedance for the main report */
561 *reading = info->hpdet_res[0];
563 /* Sometimes we get false readings due to slow insert */
564 if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
565 dev_dbg(arizona->dev, "Retrying high impedance\n");
566 info->num_hpdet_res = 0;
567 info->hpdet_retried = true;
568 arizona_start_hpdet_acc_id(info);
569 pm_runtime_put(info->dev);
574 * If we measure the mic as high impedance
576 if (!id_gpio || info->hpdet_res[1] > 50) {
577 dev_dbg(arizona->dev, "Detected mic\n");
579 info->detecting = true;
581 dev_dbg(arizona->dev, "Detected headphone\n");
584 /* Make sure everything is reset back to the real polarity */
585 regmap_update_bits(arizona->regmap,
586 ARIZONA_ACCESSORY_DETECT_MODE_1,
588 info->micd_modes[0].src);
594 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
596 struct arizona_extcon_info *info = data;
597 struct arizona *arizona = info->arizona;
598 int id_gpio = arizona->pdata.hpdet_id_gpio;
599 unsigned int report = EXTCON_JACK_HEADPHONE;
600 int ret, reading, state;
603 mutex_lock(&info->lock);
605 /* If we got a spurious IRQ for some reason then ignore it */
606 if (!info->hpdet_active) {
607 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
608 mutex_unlock(&info->lock);
612 /* If the cable was removed while measuring ignore the result */
613 state = extcon_get_state(info->edev, EXTCON_MECHANICAL);
615 dev_err(arizona->dev, "Failed to check cable state: %d\n", state);
618 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
622 ret = arizona_hpdet_read(info);
629 /* Reset back to starting range */
630 regmap_update_bits(arizona->regmap,
631 ARIZONA_HEADPHONE_DETECT_1,
632 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
635 ret = arizona_hpdet_do_id(info, &reading, &mic);
641 /* Report high impedence cables as line outputs */
643 report = EXTCON_JACK_LINE_OUT;
645 report = EXTCON_JACK_HEADPHONE;
647 ret = extcon_set_state_sync(info->edev, report, true);
649 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
653 /* Reset back to starting range */
654 regmap_update_bits(arizona->regmap,
655 ARIZONA_HEADPHONE_DETECT_1,
656 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
659 arizona_extcon_hp_clamp(info, false);
662 gpio_set_value_cansleep(id_gpio, 0);
664 /* Revert back to MICDET mode */
665 regmap_update_bits(arizona->regmap,
666 ARIZONA_ACCESSORY_DETECT_MODE_1,
667 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
669 /* If we have a mic then reenable MICDET */
670 if (state && (mic || info->mic))
671 arizona_start_mic(info);
673 if (info->hpdet_active) {
674 pm_runtime_put_autosuspend(info->dev);
675 info->hpdet_active = false;
678 /* Do not set hp_det done when the cable has been unplugged */
680 info->hpdet_done = true;
683 mutex_unlock(&info->lock);
688 static void arizona_identify_headphone(struct arizona_extcon_info *info)
690 struct arizona *arizona = info->arizona;
693 if (info->hpdet_done)
696 dev_dbg(arizona->dev, "Starting HPDET\n");
698 /* Make sure we keep the device enabled during the measurement */
699 pm_runtime_get(info->dev);
701 info->hpdet_active = true;
704 arizona_stop_mic(info);
706 arizona_extcon_hp_clamp(info, true);
708 ret = regmap_update_bits(arizona->regmap,
709 ARIZONA_ACCESSORY_DETECT_MODE_1,
710 ARIZONA_ACCDET_MODE_MASK,
711 arizona->pdata.hpdet_channel);
713 dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
717 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
718 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
720 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
728 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
729 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
731 /* Just report headphone */
732 ret = extcon_set_state_sync(info->edev, EXTCON_JACK_HEADPHONE, true);
734 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
737 arizona_start_mic(info);
739 info->hpdet_active = false;
742 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
744 struct arizona *arizona = info->arizona;
749 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
751 /* Make sure we keep the device enabled during the measurement */
752 pm_runtime_get_sync(info->dev);
754 info->hpdet_active = true;
756 arizona_extcon_hp_clamp(info, true);
758 ret = regmap_update_bits(arizona->regmap,
759 ARIZONA_ACCESSORY_DETECT_MODE_1,
760 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
761 info->micd_modes[0].src |
762 arizona->pdata.hpdet_channel);
764 dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
768 if (arizona->pdata.hpdet_acc_id_line) {
769 ret = regmap_update_bits(arizona->regmap,
770 ARIZONA_HEADPHONE_DETECT_1,
771 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
773 dev_err(arizona->dev,
774 "Can't start HPDETL measurement: %d\n",
779 arizona_hpdet_do_id(info, &hp_reading, &mic);
785 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
786 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
788 /* Just report headphone */
789 ret = extcon_set_state_sync(info->edev, EXTCON_JACK_HEADPHONE, true);
791 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
793 info->hpdet_active = false;
796 static void arizona_micd_timeout_work(struct work_struct *work)
798 struct arizona_extcon_info *info = container_of(work,
799 struct arizona_extcon_info,
800 micd_timeout_work.work);
802 mutex_lock(&info->lock);
804 dev_dbg(info->arizona->dev, "MICD timed out, reporting HP\n");
806 info->detecting = false;
808 arizona_identify_headphone(info);
810 arizona_stop_mic(info);
812 mutex_unlock(&info->lock);
815 static void arizona_micd_detect(struct work_struct *work)
817 struct arizona_extcon_info *info = container_of(work,
818 struct arizona_extcon_info,
819 micd_detect_work.work);
820 struct arizona *arizona = info->arizona;
821 unsigned int val = 0, lvl;
824 cancel_delayed_work_sync(&info->micd_timeout_work);
826 mutex_lock(&info->lock);
828 /* If the cable was removed while measuring ignore the result */
829 ret = extcon_get_state(info->edev, EXTCON_MECHANICAL);
831 dev_err(arizona->dev, "Failed to check cable state: %d\n",
833 mutex_unlock(&info->lock);
836 dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n");
837 mutex_unlock(&info->lock);
841 if (info->detecting && arizona->pdata.micd_software_compare) {
842 /* Must disable MICD before we read the ADCVAL */
843 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
844 ARIZONA_MICD_ENA, 0);
845 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_4, &val);
847 dev_err(arizona->dev,
848 "Failed to read MICDET_ADCVAL: %d\n",
850 mutex_unlock(&info->lock);
854 dev_dbg(arizona->dev, "MICDET_ADCVAL: %x\n", val);
856 val &= ARIZONA_MICDET_ADCVAL_MASK;
857 if (val < ARRAY_SIZE(arizona_micd_levels))
858 val = arizona_micd_levels[val];
862 if (val <= QUICK_HEADPHONE_MAX_OHM)
863 val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_0;
864 else if (val <= MICROPHONE_MIN_OHM)
865 val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_1;
866 else if (val <= MICROPHONE_MAX_OHM)
867 val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_8;
869 val = ARIZONA_MICD_LVL_8;
872 for (i = 0; i < 10 && !(val & MICD_LVL_0_TO_8); i++) {
873 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
875 dev_err(arizona->dev,
876 "Failed to read MICDET: %d\n", ret);
877 mutex_unlock(&info->lock);
881 dev_dbg(arizona->dev, "MICDET: %x\n", val);
883 if (!(val & ARIZONA_MICD_VALID)) {
884 dev_warn(arizona->dev,
885 "Microphone detection state invalid\n");
886 mutex_unlock(&info->lock);
891 if (i == 10 && !(val & MICD_LVL_0_TO_8)) {
892 dev_err(arizona->dev, "Failed to get valid MICDET value\n");
893 mutex_unlock(&info->lock);
897 /* Due to jack detect this should never happen */
898 if (!(val & ARIZONA_MICD_STS)) {
899 dev_warn(arizona->dev, "Detected open circuit\n");
901 arizona_stop_mic(info);
902 info->detecting = false;
903 arizona_identify_headphone(info);
907 /* If we got a high impedence we should have a headset, report it. */
908 if (info->detecting && (val & ARIZONA_MICD_LVL_8)) {
910 info->detecting = false;
912 arizona_identify_headphone(info);
914 ret = extcon_set_state_sync(info->edev,
915 EXTCON_JACK_MICROPHONE, true);
917 dev_err(arizona->dev, "Headset report failed: %d\n",
920 /* Don't need to regulate for button detection */
921 ret = regulator_allow_bypass(info->micvdd, true);
923 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
930 /* If we detected a lower impedence during initial startup
931 * then we probably have the wrong polarity, flip it. Don't
932 * do this for the lowest impedences to speed up detection of
933 * plain headphones. If both polarities report a low
934 * impedence then give up and report headphones.
936 if (info->detecting && (val & MICD_LVL_1_TO_7)) {
937 if (info->jack_flips >= info->micd_num_modes * 10) {
938 dev_dbg(arizona->dev, "Detected HP/line\n");
940 info->detecting = false;
942 arizona_identify_headphone(info);
944 arizona_stop_mic(info);
947 if (info->micd_mode == info->micd_num_modes)
949 arizona_extcon_set_mode(info, info->micd_mode);
958 * If we're still detecting and we detect a short then we've
959 * got a headphone. Otherwise it's a button press.
961 if (val & MICD_LVL_0_TO_7) {
963 dev_dbg(arizona->dev, "Mic button detected\n");
965 lvl = val & ARIZONA_MICD_LVL_MASK;
966 lvl >>= ARIZONA_MICD_LVL_SHIFT;
968 for (i = 0; i < info->num_micd_ranges; i++)
969 input_report_key(info->input,
970 info->micd_ranges[i].key, 0);
973 WARN_ON(ffs(lvl) - 1 >= info->num_micd_ranges);
974 if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
975 key = info->micd_ranges[ffs(lvl) - 1].key;
976 input_report_key(info->input, key, 1);
977 input_sync(info->input);
980 } else if (info->detecting) {
981 dev_dbg(arizona->dev, "Headphone detected\n");
982 info->detecting = false;
983 arizona_stop_mic(info);
985 arizona_identify_headphone(info);
987 dev_warn(arizona->dev, "Button with no mic: %x\n",
991 dev_dbg(arizona->dev, "Mic button released\n");
992 for (i = 0; i < info->num_micd_ranges; i++)
993 input_report_key(info->input,
994 info->micd_ranges[i].key, 0);
995 input_sync(info->input);
996 arizona_extcon_pulse_micbias(info);
1000 if (info->detecting) {
1001 if (arizona->pdata.micd_software_compare)
1002 regmap_update_bits(arizona->regmap,
1003 ARIZONA_MIC_DETECT_1,
1007 queue_delayed_work(system_power_efficient_wq,
1008 &info->micd_timeout_work,
1009 msecs_to_jiffies(info->micd_timeout));
1012 pm_runtime_mark_last_busy(info->dev);
1013 mutex_unlock(&info->lock);
1016 static irqreturn_t arizona_micdet(int irq, void *data)
1018 struct arizona_extcon_info *info = data;
1019 struct arizona *arizona = info->arizona;
1020 int debounce = arizona->pdata.micd_detect_debounce;
1022 cancel_delayed_work_sync(&info->micd_detect_work);
1023 cancel_delayed_work_sync(&info->micd_timeout_work);
1025 mutex_lock(&info->lock);
1026 if (!info->detecting)
1028 mutex_unlock(&info->lock);
1031 queue_delayed_work(system_power_efficient_wq,
1032 &info->micd_detect_work,
1033 msecs_to_jiffies(debounce));
1035 arizona_micd_detect(&info->micd_detect_work.work);
1040 static void arizona_hpdet_work(struct work_struct *work)
1042 struct arizona_extcon_info *info = container_of(work,
1043 struct arizona_extcon_info,
1046 mutex_lock(&info->lock);
1047 arizona_start_hpdet_acc_id(info);
1048 mutex_unlock(&info->lock);
1051 static int arizona_hpdet_wait(struct arizona_extcon_info *info)
1053 struct arizona *arizona = info->arizona;
1057 for (i = 0; i < ARIZONA_HPDET_WAIT_COUNT; i++) {
1058 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2,
1061 dev_err(arizona->dev,
1062 "Failed to read HPDET state: %d\n", ret);
1066 switch (info->hpdet_ip_version) {
1068 if (val & ARIZONA_HP_DONE)
1072 if (val & ARIZONA_HP_DONE_B)
1077 msleep(ARIZONA_HPDET_WAIT_DELAY_MS);
1080 dev_warn(arizona->dev, "HPDET did not appear to complete\n");
1085 static irqreturn_t arizona_jackdet(int irq, void *data)
1087 struct arizona_extcon_info *info = data;
1088 struct arizona *arizona = info->arizona;
1089 unsigned int val, present, mask;
1090 bool cancelled_hp, cancelled_mic;
1093 cancelled_hp = cancel_delayed_work_sync(&info->hpdet_work);
1094 cancelled_mic = cancel_delayed_work_sync(&info->micd_timeout_work);
1096 pm_runtime_get_sync(info->dev);
1098 mutex_lock(&info->lock);
1100 if (info->micd_clamp) {
1101 mask = ARIZONA_MICD_CLAMP_STS;
1104 mask = ARIZONA_JD1_STS;
1105 if (arizona->pdata.jd_invert)
1108 present = ARIZONA_JD1_STS;
1111 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
1113 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
1115 mutex_unlock(&info->lock);
1116 pm_runtime_put_autosuspend(info->dev);
1121 if (val == info->last_jackdet) {
1122 dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
1124 queue_delayed_work(system_power_efficient_wq,
1126 msecs_to_jiffies(HPDET_DEBOUNCE));
1128 if (cancelled_mic) {
1129 int micd_timeout = info->micd_timeout;
1131 queue_delayed_work(system_power_efficient_wq,
1132 &info->micd_timeout_work,
1133 msecs_to_jiffies(micd_timeout));
1138 info->last_jackdet = val;
1140 if (info->last_jackdet == present) {
1141 dev_dbg(arizona->dev, "Detected jack\n");
1142 ret = extcon_set_state_sync(info->edev,
1143 EXTCON_MECHANICAL, true);
1146 dev_err(arizona->dev, "Mechanical report failed: %d\n",
1149 if (!arizona->pdata.hpdet_acc_id) {
1150 info->detecting = true;
1152 info->jack_flips = 0;
1154 arizona_start_mic(info);
1156 queue_delayed_work(system_power_efficient_wq,
1158 msecs_to_jiffies(HPDET_DEBOUNCE));
1161 if (info->micd_clamp || !arizona->pdata.jd_invert)
1162 regmap_update_bits(arizona->regmap,
1163 ARIZONA_JACK_DETECT_DEBOUNCE,
1164 ARIZONA_MICD_CLAMP_DB |
1167 dev_dbg(arizona->dev, "Detected jack removal\n");
1169 arizona_stop_mic(info);
1171 info->num_hpdet_res = 0;
1172 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
1173 info->hpdet_res[i] = 0;
1175 info->hpdet_done = false;
1176 info->hpdet_retried = false;
1178 for (i = 0; i < info->num_micd_ranges; i++)
1179 input_report_key(info->input,
1180 info->micd_ranges[i].key, 0);
1181 input_sync(info->input);
1183 for (i = 0; i < ARRAY_SIZE(arizona_cable) - 1; i++) {
1184 ret = extcon_set_state_sync(info->edev,
1185 arizona_cable[i], false);
1187 dev_err(arizona->dev,
1188 "Removal report failed: %d\n", ret);
1192 * If the jack was removed during a headphone detection we
1193 * need to wait for the headphone detection to finish, as
1194 * it can not be aborted. We don't want to be able to start
1195 * a new headphone detection from a fresh insert until this
1198 arizona_hpdet_wait(info);
1200 regmap_update_bits(arizona->regmap,
1201 ARIZONA_JACK_DETECT_DEBOUNCE,
1202 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
1203 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
1206 if (arizona->pdata.micd_timeout)
1207 info->micd_timeout = arizona->pdata.micd_timeout;
1209 info->micd_timeout = DEFAULT_MICD_TIMEOUT;
1212 /* Clear trig_sts to make sure DCVDD is not forced up */
1213 regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
1214 ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
1215 ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
1216 ARIZONA_JD1_FALL_TRIG_STS |
1217 ARIZONA_JD1_RISE_TRIG_STS);
1219 mutex_unlock(&info->lock);
1221 pm_runtime_mark_last_busy(info->dev);
1222 pm_runtime_put_autosuspend(info->dev);
1227 /* Map a level onto a slot in the register bank */
1228 static void arizona_micd_set_level(struct arizona *arizona, int index,
1234 reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
1243 /* Program the level itself */
1244 regmap_update_bits(arizona->regmap, reg, mask, level);
1247 static int arizona_extcon_get_micd_configs(struct device *dev,
1248 struct arizona *arizona)
1250 const char * const prop = "wlf,micd-configs";
1251 const int entries_per_config = 3;
1252 struct arizona_micd_config *micd_configs;
1257 nconfs = device_property_count_u32(arizona->dev, prop);
1261 vals = kcalloc(nconfs, sizeof(u32), GFP_KERNEL);
1265 ret = device_property_read_u32_array(arizona->dev, prop, vals, nconfs);
1269 nconfs /= entries_per_config;
1270 micd_configs = devm_kcalloc(dev, nconfs, sizeof(*micd_configs),
1272 if (!micd_configs) {
1277 for (i = 0, j = 0; i < nconfs; ++i) {
1278 micd_configs[i].src = vals[j++] ? ARIZONA_ACCDET_SRC : 0;
1279 micd_configs[i].bias = vals[j++];
1280 micd_configs[i].gpio = vals[j++];
1283 arizona->pdata.micd_configs = micd_configs;
1284 arizona->pdata.num_micd_configs = nconfs;
1291 static int arizona_extcon_device_get_pdata(struct device *dev,
1292 struct arizona *arizona)
1294 struct arizona_pdata *pdata = &arizona->pdata;
1295 unsigned int val = ARIZONA_ACCDET_MODE_HPL;
1298 device_property_read_u32(arizona->dev, "wlf,hpdet-channel", &val);
1300 case ARIZONA_ACCDET_MODE_HPL:
1301 case ARIZONA_ACCDET_MODE_HPR:
1302 pdata->hpdet_channel = val;
1305 dev_err(arizona->dev,
1306 "Wrong wlf,hpdet-channel DT value %d\n", val);
1307 pdata->hpdet_channel = ARIZONA_ACCDET_MODE_HPL;
1310 device_property_read_u32(arizona->dev, "wlf,micd-detect-debounce",
1311 &pdata->micd_detect_debounce);
1313 device_property_read_u32(arizona->dev, "wlf,micd-bias-start-time",
1314 &pdata->micd_bias_start_time);
1316 device_property_read_u32(arizona->dev, "wlf,micd-rate",
1319 device_property_read_u32(arizona->dev, "wlf,micd-dbtime",
1320 &pdata->micd_dbtime);
1322 device_property_read_u32(arizona->dev, "wlf,micd-timeout-ms",
1323 &pdata->micd_timeout);
1325 pdata->micd_force_micbias = device_property_read_bool(arizona->dev,
1326 "wlf,micd-force-micbias");
1328 pdata->micd_software_compare = device_property_read_bool(arizona->dev,
1329 "wlf,micd-software-compare");
1331 pdata->jd_invert = device_property_read_bool(arizona->dev,
1334 device_property_read_u32(arizona->dev, "wlf,gpsw", &pdata->gpsw);
1336 pdata->jd_gpio5 = device_property_read_bool(arizona->dev,
1338 pdata->jd_gpio5_nopull = device_property_read_bool(arizona->dev,
1339 "wlf,use-jd2-nopull");
1341 ret = arizona_extcon_get_micd_configs(dev, arizona);
1343 dev_err(arizona->dev, "Failed to read micd configs: %d\n", ret);
1348 static int arizona_extcon_probe(struct platform_device *pdev)
1350 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
1351 struct arizona_pdata *pdata = &arizona->pdata;
1352 struct arizona_extcon_info *info;
1354 unsigned int clamp_mode;
1355 int jack_irq_fall, jack_irq_rise;
1356 int ret, mode, i, j;
1358 if (!arizona->dapm || !arizona->dapm->card)
1359 return -EPROBE_DEFER;
1361 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1365 if (!dev_get_platdata(arizona->dev))
1366 arizona_extcon_device_get_pdata(&pdev->dev, arizona);
1368 info->micvdd = devm_regulator_get(&pdev->dev, "MICVDD");
1369 if (IS_ERR(info->micvdd)) {
1370 ret = PTR_ERR(info->micvdd);
1371 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
1375 mutex_init(&info->lock);
1376 info->arizona = arizona;
1377 info->dev = &pdev->dev;
1378 info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
1379 INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
1380 INIT_DELAYED_WORK(&info->micd_detect_work, arizona_micd_detect);
1381 INIT_DELAYED_WORK(&info->micd_timeout_work, arizona_micd_timeout_work);
1382 platform_set_drvdata(pdev, info);
1384 switch (arizona->type) {
1386 switch (arizona->rev) {
1388 info->micd_reva = true;
1391 info->micd_clamp = true;
1392 info->hpdet_ip_version = 1;
1398 switch (arizona->rev) {
1402 info->micd_clamp = true;
1403 info->hpdet_ip_version = 2;
1409 info->micd_clamp = true;
1410 info->hpdet_ip_version = 2;
1416 info->edev = devm_extcon_dev_allocate(&pdev->dev, arizona_cable);
1417 if (IS_ERR(info->edev)) {
1418 dev_err(&pdev->dev, "failed to allocate extcon device\n");
1422 ret = devm_extcon_dev_register(&pdev->dev, info->edev);
1424 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
1429 info->input = devm_input_allocate_device(&pdev->dev);
1431 dev_err(arizona->dev, "Can't allocate input dev\n");
1436 info->input->name = "Headset";
1437 info->input->phys = "arizona/extcon";
1439 if (pdata->num_micd_configs) {
1440 info->micd_modes = pdata->micd_configs;
1441 info->micd_num_modes = pdata->num_micd_configs;
1443 info->micd_modes = micd_default_modes;
1444 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1447 if (arizona->pdata.gpsw > 0)
1448 regmap_update_bits(arizona->regmap, ARIZONA_GP_SWITCH_1,
1449 ARIZONA_SW1_MODE_MASK, arizona->pdata.gpsw);
1451 if (pdata->micd_pol_gpio > 0) {
1452 if (info->micd_modes[0].gpio)
1453 mode = GPIOF_OUT_INIT_HIGH;
1455 mode = GPIOF_OUT_INIT_LOW;
1457 ret = devm_gpio_request_one(&pdev->dev, pdata->micd_pol_gpio,
1458 mode, "MICD polarity");
1460 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1461 pdata->micd_pol_gpio, ret);
1465 info->micd_pol_gpio = gpio_to_desc(pdata->micd_pol_gpio);
1467 if (info->micd_modes[0].gpio)
1468 mode = GPIOD_OUT_HIGH;
1470 mode = GPIOD_OUT_LOW;
1472 /* We can't use devm here because we need to do the get
1473 * against the MFD device, as that is where the of_node
1474 * will reside, but if we devm against that the GPIO
1475 * will not be freed if the extcon driver is unloaded.
1477 info->micd_pol_gpio = gpiod_get_optional(arizona->dev,
1480 if (IS_ERR(info->micd_pol_gpio)) {
1481 ret = PTR_ERR(info->micd_pol_gpio);
1482 dev_err(arizona->dev,
1483 "Failed to get microphone polarity GPIO: %d\n",
1489 if (arizona->pdata.hpdet_id_gpio > 0) {
1490 ret = devm_gpio_request_one(&pdev->dev,
1491 arizona->pdata.hpdet_id_gpio,
1495 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1496 arizona->pdata.hpdet_id_gpio, ret);
1501 if (arizona->pdata.micd_bias_start_time)
1502 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1503 ARIZONA_MICD_BIAS_STARTTIME_MASK,
1504 arizona->pdata.micd_bias_start_time
1505 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1507 if (arizona->pdata.micd_rate)
1508 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1509 ARIZONA_MICD_RATE_MASK,
1510 arizona->pdata.micd_rate
1511 << ARIZONA_MICD_RATE_SHIFT);
1513 switch (arizona->pdata.micd_dbtime) {
1514 case MICD_DBTIME_FOUR_READINGS:
1515 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1516 ARIZONA_MICD_DBTIME_MASK,
1517 ARIZONA_MICD_DBTIME);
1519 case MICD_DBTIME_TWO_READINGS:
1520 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1521 ARIZONA_MICD_DBTIME_MASK, 0);
1527 BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) <
1528 ARIZONA_NUM_MICD_BUTTON_LEVELS);
1530 if (arizona->pdata.num_micd_ranges) {
1531 info->micd_ranges = pdata->micd_ranges;
1532 info->num_micd_ranges = pdata->num_micd_ranges;
1534 info->micd_ranges = micd_default_ranges;
1535 info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
1538 if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_RANGE) {
1539 dev_err(arizona->dev, "Too many MICD ranges: %d\n",
1540 arizona->pdata.num_micd_ranges);
1543 if (info->num_micd_ranges > 1) {
1544 for (i = 1; i < info->num_micd_ranges; i++) {
1545 if (info->micd_ranges[i - 1].max >
1546 info->micd_ranges[i].max) {
1547 dev_err(arizona->dev,
1548 "MICD ranges must be sorted\n");
1555 /* Disable all buttons by default */
1556 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1557 ARIZONA_MICD_LVL_SEL_MASK, 0x81);
1559 /* Set up all the buttons the user specified */
1560 for (i = 0; i < info->num_micd_ranges; i++) {
1561 for (j = 0; j < ARIZONA_NUM_MICD_BUTTON_LEVELS; j++)
1562 if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
1565 if (j == ARIZONA_NUM_MICD_BUTTON_LEVELS) {
1566 dev_err(arizona->dev, "Unsupported MICD level %d\n",
1567 info->micd_ranges[i].max);
1572 dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
1573 arizona_micd_levels[j], i);
1575 arizona_micd_set_level(arizona, i, j);
1576 input_set_capability(info->input, EV_KEY,
1577 info->micd_ranges[i].key);
1579 /* Enable reporting of that range */
1580 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1584 /* Set all the remaining keys to a maximum */
1585 for (; i < ARIZONA_MAX_MICD_RANGE; i++)
1586 arizona_micd_set_level(arizona, i, 0x3f);
1589 * If we have a clamp use it, activating in conjunction with
1590 * GPIO5 if that is connected for jack detect operation.
1592 if (info->micd_clamp) {
1593 if (arizona->pdata.jd_gpio5) {
1594 /* Put the GPIO into input mode with optional pull */
1596 if (arizona->pdata.jd_gpio5_nopull)
1597 val &= ~ARIZONA_GPN_PU;
1599 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1602 if (arizona->pdata.jd_invert)
1603 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH_GP5H;
1605 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL_GP5H;
1607 if (arizona->pdata.jd_invert)
1608 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH;
1610 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL;
1613 regmap_update_bits(arizona->regmap,
1614 ARIZONA_MICD_CLAMP_CONTROL,
1615 ARIZONA_MICD_CLAMP_MODE_MASK, clamp_mode);
1617 regmap_update_bits(arizona->regmap,
1618 ARIZONA_JACK_DETECT_DEBOUNCE,
1619 ARIZONA_MICD_CLAMP_DB,
1620 ARIZONA_MICD_CLAMP_DB);
1623 arizona_extcon_set_mode(info, 0);
1625 pm_runtime_enable(&pdev->dev);
1626 pm_runtime_idle(&pdev->dev);
1627 pm_runtime_get_sync(&pdev->dev);
1629 if (info->micd_clamp) {
1630 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1631 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1633 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1634 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1637 ret = arizona_request_irq(arizona, jack_irq_rise,
1638 "JACKDET rise", arizona_jackdet, info);
1640 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1645 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1647 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1652 ret = arizona_request_irq(arizona, jack_irq_fall,
1653 "JACKDET fall", arizona_jackdet, info);
1655 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1659 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1661 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1666 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1667 "MICDET", arizona_micdet, info);
1669 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1673 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1674 "HPDET", arizona_hpdet_irq, info);
1676 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1680 arizona_clk32k_enable(arizona);
1681 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1682 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1683 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1684 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1686 ret = regulator_allow_bypass(info->micvdd, true);
1688 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1691 pm_runtime_put(&pdev->dev);
1693 ret = input_register_device(info->input);
1695 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1702 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1704 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1706 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1708 arizona_free_irq(arizona, jack_irq_fall, info);
1710 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1712 arizona_free_irq(arizona, jack_irq_rise, info);
1714 gpiod_put(info->micd_pol_gpio);
1716 pm_runtime_disable(&pdev->dev);
1720 static int arizona_extcon_remove(struct platform_device *pdev)
1722 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1723 struct arizona *arizona = info->arizona;
1724 int jack_irq_rise, jack_irq_fall;
1728 if (info->micd_clamp) {
1729 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1730 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1732 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1733 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1736 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1737 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1738 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1739 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1740 arizona_free_irq(arizona, jack_irq_rise, info);
1741 arizona_free_irq(arizona, jack_irq_fall, info);
1742 cancel_delayed_work_sync(&info->hpdet_work);
1743 cancel_delayed_work_sync(&info->micd_detect_work);
1744 cancel_delayed_work_sync(&info->micd_timeout_work);
1746 ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
1747 ARIZONA_MICD_ENA, 0,
1750 dev_err(&pdev->dev, "Failed to disable micd on remove: %d\n",
1752 } else if (change) {
1753 regulator_disable(info->micvdd);
1754 pm_runtime_put(info->dev);
1757 regmap_update_bits(arizona->regmap,
1758 ARIZONA_MICD_CLAMP_CONTROL,
1759 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1760 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1761 ARIZONA_JD1_ENA, 0);
1762 arizona_clk32k_disable(arizona);
1764 gpiod_put(info->micd_pol_gpio);
1766 pm_runtime_disable(&pdev->dev);
1771 static struct platform_driver arizona_extcon_driver = {
1773 .name = "arizona-extcon",
1775 .probe = arizona_extcon_probe,
1776 .remove = arizona_extcon_remove,
1779 module_platform_driver(arizona_extcon_driver);
1781 MODULE_DESCRIPTION("Arizona Extcon driver");
1782 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1783 MODULE_LICENSE("GPL");
1784 MODULE_ALIAS("platform:extcon-arizona");