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 regmap_update_bits_check(arizona
->regmap
, ARIZONA_MIC_DETECT_1
,
330 ARIZONA_MICD_ENA
, ARIZONA_MICD_ENA
,
333 regulator_disable(info
->micvdd
);
334 pm_runtime_put_autosuspend(info
->dev
);
338 static void arizona_stop_mic(struct arizona_extcon_info
*info
)
340 struct arizona
*arizona
= info
->arizona
;
341 const char *widget
= arizona_extcon_get_micbias(info
);
342 struct snd_soc_dapm_context
*dapm
= arizona
->dapm
;
343 struct snd_soc_component
*component
= snd_soc_dapm_to_component(dapm
);
347 regmap_update_bits_check(arizona
->regmap
, ARIZONA_MIC_DETECT_1
,
351 ret
= snd_soc_component_disable_pin(component
, widget
);
353 dev_warn(arizona
->dev
,
354 "Failed to disable %s: %d\n",
357 snd_soc_dapm_sync(dapm
);
359 if (info
->micd_reva
) {
360 regmap_write(arizona
->regmap
, 0x80, 0x3);
361 regmap_write(arizona
->regmap
, 0x294, 2);
362 regmap_write(arizona
->regmap
, 0x80, 0x0);
365 ret
= regulator_allow_bypass(info
->micvdd
, true);
367 dev_err(arizona
->dev
, "Failed to bypass MICVDD: %d\n",
372 regulator_disable(info
->micvdd
);
373 pm_runtime_mark_last_busy(info
->dev
);
374 pm_runtime_put_autosuspend(info
->dev
);
379 unsigned int threshold
;
380 unsigned int factor_a
;
381 unsigned int factor_b
;
382 } arizona_hpdet_b_ranges
[] = {
383 { 100, 5528, 362464 },
384 { 169, 11084, 6186851 },
385 { 169, 11065, 65460395 },
388 #define ARIZONA_HPDET_B_RANGE_MAX 0x3fb
393 } arizona_hpdet_c_ranges
[] = {
400 static int arizona_hpdet_read(struct arizona_extcon_info
*info
)
402 struct arizona
*arizona
= info
->arizona
;
403 unsigned int val
, range
;
406 ret
= regmap_read(arizona
->regmap
, ARIZONA_HEADPHONE_DETECT_2
, &val
);
408 dev_err(arizona
->dev
, "Failed to read HPDET status: %d\n",
413 switch (info
->hpdet_ip_version
) {
415 if (!(val
& ARIZONA_HP_DONE
)) {
416 dev_err(arizona
->dev
, "HPDET did not complete: %x\n",
421 val
&= ARIZONA_HP_LVL_MASK
;
425 if (!(val
& ARIZONA_HP_DONE_B
)) {
426 dev_err(arizona
->dev
, "HPDET did not complete: %x\n",
431 ret
= regmap_read(arizona
->regmap
, ARIZONA_HP_DACVAL
, &val
);
433 dev_err(arizona
->dev
, "Failed to read HP value: %d\n",
438 regmap_read(arizona
->regmap
, ARIZONA_HEADPHONE_DETECT_1
,
440 range
= (range
& ARIZONA_HP_IMPEDANCE_RANGE_MASK
)
441 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT
;
443 if (range
< ARRAY_SIZE(arizona_hpdet_b_ranges
) - 1 &&
444 (val
< arizona_hpdet_b_ranges
[range
].threshold
||
445 val
>= ARIZONA_HPDET_B_RANGE_MAX
)) {
447 dev_dbg(arizona
->dev
, "Moving to HPDET range %d\n",
449 regmap_update_bits(arizona
->regmap
,
450 ARIZONA_HEADPHONE_DETECT_1
,
451 ARIZONA_HP_IMPEDANCE_RANGE_MASK
,
453 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT
);
457 /* If we go out of range report top of range */
458 if (val
< arizona_hpdet_b_ranges
[range
].threshold
||
459 val
>= ARIZONA_HPDET_B_RANGE_MAX
) {
460 dev_dbg(arizona
->dev
, "Measurement out of range\n");
461 return ARIZONA_HPDET_MAX
;
464 dev_dbg(arizona
->dev
, "HPDET read %d in range %d\n",
467 val
= arizona_hpdet_b_ranges
[range
].factor_b
469 arizona_hpdet_b_ranges
[range
].factor_a
);
473 if (!(val
& ARIZONA_HP_DONE_B
)) {
474 dev_err(arizona
->dev
, "HPDET did not complete: %x\n",
479 val
&= ARIZONA_HP_LVL_B_MASK
;
480 /* Convert to ohms, the value is in 0.5 ohm increments */
483 regmap_read(arizona
->regmap
, ARIZONA_HEADPHONE_DETECT_1
,
485 range
= (range
& ARIZONA_HP_IMPEDANCE_RANGE_MASK
)
486 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT
;
488 /* Skip up a range, or report? */
489 if (range
< ARRAY_SIZE(arizona_hpdet_c_ranges
) - 1 &&
490 (val
>= arizona_hpdet_c_ranges
[range
].max
)) {
492 dev_dbg(arizona
->dev
, "Moving to HPDET range %d-%d\n",
493 arizona_hpdet_c_ranges
[range
].min
,
494 arizona_hpdet_c_ranges
[range
].max
);
495 regmap_update_bits(arizona
->regmap
,
496 ARIZONA_HEADPHONE_DETECT_1
,
497 ARIZONA_HP_IMPEDANCE_RANGE_MASK
,
499 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT
);
503 if (range
&& (val
< arizona_hpdet_c_ranges
[range
].min
)) {
504 dev_dbg(arizona
->dev
, "Reporting range boundary %d\n",
505 arizona_hpdet_c_ranges
[range
].min
);
506 val
= arizona_hpdet_c_ranges
[range
].min
;
511 dev_warn(arizona
->dev
, "Unknown HPDET IP revision %d\n",
512 info
->hpdet_ip_version
);
516 dev_dbg(arizona
->dev
, "HP impedance %d ohms\n", val
);
520 static int arizona_hpdet_do_id(struct arizona_extcon_info
*info
, int *reading
,
523 struct arizona
*arizona
= info
->arizona
;
524 int id_gpio
= arizona
->pdata
.hpdet_id_gpio
;
527 * If we're using HPDET for accessory identification we need
528 * to take multiple measurements, step through them in sequence.
530 if (arizona
->pdata
.hpdet_acc_id
) {
531 info
->hpdet_res
[info
->num_hpdet_res
++] = *reading
;
533 /* Only check the mic directly if we didn't already ID it */
534 if (id_gpio
&& info
->num_hpdet_res
== 1) {
535 dev_dbg(arizona
->dev
, "Measuring mic\n");
537 regmap_update_bits(arizona
->regmap
,
538 ARIZONA_ACCESSORY_DETECT_MODE_1
,
539 ARIZONA_ACCDET_MODE_MASK
|
541 ARIZONA_ACCDET_MODE_HPR
|
542 info
->micd_modes
[0].src
);
544 gpio_set_value_cansleep(id_gpio
, 1);
546 regmap_update_bits(arizona
->regmap
,
547 ARIZONA_HEADPHONE_DETECT_1
,
548 ARIZONA_HP_POLL
, ARIZONA_HP_POLL
);
552 /* OK, got both. Now, compare... */
553 dev_dbg(arizona
->dev
, "HPDET measured %d %d\n",
554 info
->hpdet_res
[0], info
->hpdet_res
[1]);
556 /* Take the headphone impedance for the main report */
557 *reading
= info
->hpdet_res
[0];
559 /* Sometimes we get false readings due to slow insert */
560 if (*reading
>= ARIZONA_HPDET_MAX
&& !info
->hpdet_retried
) {
561 dev_dbg(arizona
->dev
, "Retrying high impedance\n");
562 info
->num_hpdet_res
= 0;
563 info
->hpdet_retried
= true;
564 arizona_start_hpdet_acc_id(info
);
565 pm_runtime_put(info
->dev
);
570 * If we measure the mic as high impedance
572 if (!id_gpio
|| info
->hpdet_res
[1] > 50) {
573 dev_dbg(arizona
->dev
, "Detected mic\n");
575 info
->detecting
= true;
577 dev_dbg(arizona
->dev
, "Detected headphone\n");
580 /* Make sure everything is reset back to the real polarity */
581 regmap_update_bits(arizona
->regmap
,
582 ARIZONA_ACCESSORY_DETECT_MODE_1
,
584 info
->micd_modes
[0].src
);
590 static irqreturn_t
arizona_hpdet_irq(int irq
, void *data
)
592 struct arizona_extcon_info
*info
= data
;
593 struct arizona
*arizona
= info
->arizona
;
594 int id_gpio
= arizona
->pdata
.hpdet_id_gpio
;
595 unsigned int report
= EXTCON_JACK_HEADPHONE
;
599 mutex_lock(&info
->lock
);
601 /* If we got a spurious IRQ for some reason then ignore it */
602 if (!info
->hpdet_active
) {
603 dev_warn(arizona
->dev
, "Spurious HPDET IRQ\n");
604 mutex_unlock(&info
->lock
);
608 /* If the cable was removed while measuring ignore the result */
609 ret
= extcon_get_state(info
->edev
, EXTCON_MECHANICAL
);
611 dev_err(arizona
->dev
, "Failed to check cable state: %d\n",
615 dev_dbg(arizona
->dev
, "Ignoring HPDET for removed cable\n");
619 ret
= arizona_hpdet_read(info
);
626 /* Reset back to starting range */
627 regmap_update_bits(arizona
->regmap
,
628 ARIZONA_HEADPHONE_DETECT_1
,
629 ARIZONA_HP_IMPEDANCE_RANGE_MASK
| ARIZONA_HP_POLL
,
632 ret
= arizona_hpdet_do_id(info
, &reading
, &mic
);
638 /* Report high impedence cables as line outputs */
640 report
= EXTCON_JACK_LINE_OUT
;
642 report
= EXTCON_JACK_HEADPHONE
;
644 ret
= extcon_set_state_sync(info
->edev
, report
, true);
646 dev_err(arizona
->dev
, "Failed to report HP/line: %d\n",
650 /* Reset back to starting range */
651 regmap_update_bits(arizona
->regmap
,
652 ARIZONA_HEADPHONE_DETECT_1
,
653 ARIZONA_HP_IMPEDANCE_RANGE_MASK
| ARIZONA_HP_POLL
,
656 arizona_extcon_hp_clamp(info
, false);
659 gpio_set_value_cansleep(id_gpio
, 0);
661 /* Revert back to MICDET mode */
662 regmap_update_bits(arizona
->regmap
,
663 ARIZONA_ACCESSORY_DETECT_MODE_1
,
664 ARIZONA_ACCDET_MODE_MASK
, ARIZONA_ACCDET_MODE_MIC
);
666 /* If we have a mic then reenable MICDET */
667 if (mic
|| info
->mic
)
668 arizona_start_mic(info
);
670 if (info
->hpdet_active
) {
671 pm_runtime_put_autosuspend(info
->dev
);
672 info
->hpdet_active
= false;
675 info
->hpdet_done
= true;
678 mutex_unlock(&info
->lock
);
683 static void arizona_identify_headphone(struct arizona_extcon_info
*info
)
685 struct arizona
*arizona
= info
->arizona
;
688 if (info
->hpdet_done
)
691 dev_dbg(arizona
->dev
, "Starting HPDET\n");
693 /* Make sure we keep the device enabled during the measurement */
694 pm_runtime_get(info
->dev
);
696 info
->hpdet_active
= true;
699 arizona_stop_mic(info
);
701 arizona_extcon_hp_clamp(info
, true);
703 ret
= regmap_update_bits(arizona
->regmap
,
704 ARIZONA_ACCESSORY_DETECT_MODE_1
,
705 ARIZONA_ACCDET_MODE_MASK
,
706 arizona
->pdata
.hpdet_channel
);
708 dev_err(arizona
->dev
, "Failed to set HPDET mode: %d\n", ret
);
712 ret
= regmap_update_bits(arizona
->regmap
, ARIZONA_HEADPHONE_DETECT_1
,
713 ARIZONA_HP_POLL
, ARIZONA_HP_POLL
);
715 dev_err(arizona
->dev
, "Can't start HPDETL measurement: %d\n",
723 regmap_update_bits(arizona
->regmap
, ARIZONA_ACCESSORY_DETECT_MODE_1
,
724 ARIZONA_ACCDET_MODE_MASK
, ARIZONA_ACCDET_MODE_MIC
);
726 /* Just report headphone */
727 ret
= extcon_set_state_sync(info
->edev
, EXTCON_JACK_HEADPHONE
, true);
729 dev_err(arizona
->dev
, "Failed to report headphone: %d\n", ret
);
732 arizona_start_mic(info
);
734 info
->hpdet_active
= false;
737 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info
*info
)
739 struct arizona
*arizona
= info
->arizona
;
744 dev_dbg(arizona
->dev
, "Starting identification via HPDET\n");
746 /* Make sure we keep the device enabled during the measurement */
747 pm_runtime_get_sync(info
->dev
);
749 info
->hpdet_active
= true;
751 arizona_extcon_hp_clamp(info
, true);
753 ret
= regmap_update_bits(arizona
->regmap
,
754 ARIZONA_ACCESSORY_DETECT_MODE_1
,
755 ARIZONA_ACCDET_SRC
| ARIZONA_ACCDET_MODE_MASK
,
756 info
->micd_modes
[0].src
|
757 arizona
->pdata
.hpdet_channel
);
759 dev_err(arizona
->dev
, "Failed to set HPDET mode: %d\n", ret
);
763 if (arizona
->pdata
.hpdet_acc_id_line
) {
764 ret
= regmap_update_bits(arizona
->regmap
,
765 ARIZONA_HEADPHONE_DETECT_1
,
766 ARIZONA_HP_POLL
, ARIZONA_HP_POLL
);
768 dev_err(arizona
->dev
,
769 "Can't start HPDETL measurement: %d\n",
774 arizona_hpdet_do_id(info
, &hp_reading
, &mic
);
780 regmap_update_bits(arizona
->regmap
, ARIZONA_ACCESSORY_DETECT_MODE_1
,
781 ARIZONA_ACCDET_MODE_MASK
, ARIZONA_ACCDET_MODE_MIC
);
783 /* Just report headphone */
784 ret
= extcon_set_state_sync(info
->edev
, EXTCON_JACK_HEADPHONE
, true);
786 dev_err(arizona
->dev
, "Failed to report headphone: %d\n", ret
);
788 info
->hpdet_active
= false;
791 static void arizona_micd_timeout_work(struct work_struct
*work
)
793 struct arizona_extcon_info
*info
= container_of(work
,
794 struct arizona_extcon_info
,
795 micd_timeout_work
.work
);
797 mutex_lock(&info
->lock
);
799 dev_dbg(info
->arizona
->dev
, "MICD timed out, reporting HP\n");
801 info
->detecting
= false;
803 arizona_identify_headphone(info
);
805 arizona_stop_mic(info
);
807 mutex_unlock(&info
->lock
);
810 static void arizona_micd_detect(struct work_struct
*work
)
812 struct arizona_extcon_info
*info
= container_of(work
,
813 struct arizona_extcon_info
,
814 micd_detect_work
.work
);
815 struct arizona
*arizona
= info
->arizona
;
816 unsigned int val
= 0, lvl
;
819 cancel_delayed_work_sync(&info
->micd_timeout_work
);
821 mutex_lock(&info
->lock
);
823 /* If the cable was removed while measuring ignore the result */
824 ret
= extcon_get_state(info
->edev
, EXTCON_MECHANICAL
);
826 dev_err(arizona
->dev
, "Failed to check cable state: %d\n",
828 mutex_unlock(&info
->lock
);
831 dev_dbg(arizona
->dev
, "Ignoring MICDET for removed cable\n");
832 mutex_unlock(&info
->lock
);
836 if (info
->detecting
&& arizona
->pdata
.micd_software_compare
) {
837 /* Must disable MICD before we read the ADCVAL */
838 regmap_update_bits(arizona
->regmap
, ARIZONA_MIC_DETECT_1
,
839 ARIZONA_MICD_ENA
, 0);
840 ret
= regmap_read(arizona
->regmap
, ARIZONA_MIC_DETECT_4
, &val
);
842 dev_err(arizona
->dev
,
843 "Failed to read MICDET_ADCVAL: %d\n",
845 mutex_unlock(&info
->lock
);
849 dev_dbg(arizona
->dev
, "MICDET_ADCVAL: %x\n", val
);
851 val
&= ARIZONA_MICDET_ADCVAL_MASK
;
852 if (val
< ARRAY_SIZE(arizona_micd_levels
))
853 val
= arizona_micd_levels
[val
];
857 if (val
<= QUICK_HEADPHONE_MAX_OHM
)
858 val
= ARIZONA_MICD_STS
| ARIZONA_MICD_LVL_0
;
859 else if (val
<= MICROPHONE_MIN_OHM
)
860 val
= ARIZONA_MICD_STS
| ARIZONA_MICD_LVL_1
;
861 else if (val
<= MICROPHONE_MAX_OHM
)
862 val
= ARIZONA_MICD_STS
| ARIZONA_MICD_LVL_8
;
864 val
= ARIZONA_MICD_LVL_8
;
867 for (i
= 0; i
< 10 && !(val
& MICD_LVL_0_TO_8
); i
++) {
868 ret
= regmap_read(arizona
->regmap
, ARIZONA_MIC_DETECT_3
, &val
);
870 dev_err(arizona
->dev
,
871 "Failed to read MICDET: %d\n", ret
);
872 mutex_unlock(&info
->lock
);
876 dev_dbg(arizona
->dev
, "MICDET: %x\n", val
);
878 if (!(val
& ARIZONA_MICD_VALID
)) {
879 dev_warn(arizona
->dev
,
880 "Microphone detection state invalid\n");
881 mutex_unlock(&info
->lock
);
886 if (i
== 10 && !(val
& MICD_LVL_0_TO_8
)) {
887 dev_err(arizona
->dev
, "Failed to get valid MICDET value\n");
888 mutex_unlock(&info
->lock
);
892 /* Due to jack detect this should never happen */
893 if (!(val
& ARIZONA_MICD_STS
)) {
894 dev_warn(arizona
->dev
, "Detected open circuit\n");
896 arizona_stop_mic(info
);
897 info
->detecting
= false;
898 arizona_identify_headphone(info
);
902 /* If we got a high impedence we should have a headset, report it. */
903 if (info
->detecting
&& (val
& ARIZONA_MICD_LVL_8
)) {
905 info
->detecting
= false;
907 arizona_identify_headphone(info
);
909 ret
= extcon_set_state_sync(info
->edev
,
910 EXTCON_JACK_MICROPHONE
, true);
912 dev_err(arizona
->dev
, "Headset report failed: %d\n",
915 /* Don't need to regulate for button detection */
916 ret
= regulator_allow_bypass(info
->micvdd
, true);
918 dev_err(arizona
->dev
, "Failed to bypass MICVDD: %d\n",
925 /* If we detected a lower impedence during initial startup
926 * then we probably have the wrong polarity, flip it. Don't
927 * do this for the lowest impedences to speed up detection of
928 * plain headphones. If both polarities report a low
929 * impedence then give up and report headphones.
931 if (info
->detecting
&& (val
& MICD_LVL_1_TO_7
)) {
932 if (info
->jack_flips
>= info
->micd_num_modes
* 10) {
933 dev_dbg(arizona
->dev
, "Detected HP/line\n");
935 info
->detecting
= false;
937 arizona_identify_headphone(info
);
939 arizona_stop_mic(info
);
942 if (info
->micd_mode
== info
->micd_num_modes
)
944 arizona_extcon_set_mode(info
, info
->micd_mode
);
953 * If we're still detecting and we detect a short then we've
954 * got a headphone. Otherwise it's a button press.
956 if (val
& MICD_LVL_0_TO_7
) {
958 dev_dbg(arizona
->dev
, "Mic button detected\n");
960 lvl
= val
& ARIZONA_MICD_LVL_MASK
;
961 lvl
>>= ARIZONA_MICD_LVL_SHIFT
;
963 for (i
= 0; i
< info
->num_micd_ranges
; i
++)
964 input_report_key(info
->input
,
965 info
->micd_ranges
[i
].key
, 0);
968 WARN_ON(ffs(lvl
) - 1 >= info
->num_micd_ranges
);
969 if (lvl
&& ffs(lvl
) - 1 < info
->num_micd_ranges
) {
970 key
= info
->micd_ranges
[ffs(lvl
) - 1].key
;
971 input_report_key(info
->input
, key
, 1);
972 input_sync(info
->input
);
975 } else if (info
->detecting
) {
976 dev_dbg(arizona
->dev
, "Headphone detected\n");
977 info
->detecting
= false;
978 arizona_stop_mic(info
);
980 arizona_identify_headphone(info
);
982 dev_warn(arizona
->dev
, "Button with no mic: %x\n",
986 dev_dbg(arizona
->dev
, "Mic button released\n");
987 for (i
= 0; i
< info
->num_micd_ranges
; i
++)
988 input_report_key(info
->input
,
989 info
->micd_ranges
[i
].key
, 0);
990 input_sync(info
->input
);
991 arizona_extcon_pulse_micbias(info
);
995 if (info
->detecting
) {
996 if (arizona
->pdata
.micd_software_compare
)
997 regmap_update_bits(arizona
->regmap
,
998 ARIZONA_MIC_DETECT_1
,
1002 queue_delayed_work(system_power_efficient_wq
,
1003 &info
->micd_timeout_work
,
1004 msecs_to_jiffies(info
->micd_timeout
));
1007 pm_runtime_mark_last_busy(info
->dev
);
1008 mutex_unlock(&info
->lock
);
1011 static irqreturn_t
arizona_micdet(int irq
, void *data
)
1013 struct arizona_extcon_info
*info
= data
;
1014 struct arizona
*arizona
= info
->arizona
;
1015 int debounce
= arizona
->pdata
.micd_detect_debounce
;
1017 cancel_delayed_work_sync(&info
->micd_detect_work
);
1018 cancel_delayed_work_sync(&info
->micd_timeout_work
);
1020 mutex_lock(&info
->lock
);
1021 if (!info
->detecting
)
1023 mutex_unlock(&info
->lock
);
1026 queue_delayed_work(system_power_efficient_wq
,
1027 &info
->micd_detect_work
,
1028 msecs_to_jiffies(debounce
));
1030 arizona_micd_detect(&info
->micd_detect_work
.work
);
1035 static void arizona_hpdet_work(struct work_struct
*work
)
1037 struct arizona_extcon_info
*info
= container_of(work
,
1038 struct arizona_extcon_info
,
1041 mutex_lock(&info
->lock
);
1042 arizona_start_hpdet_acc_id(info
);
1043 mutex_unlock(&info
->lock
);
1046 static int arizona_hpdet_wait(struct arizona_extcon_info
*info
)
1048 struct arizona
*arizona
= info
->arizona
;
1052 for (i
= 0; i
< ARIZONA_HPDET_WAIT_COUNT
; i
++) {
1053 ret
= regmap_read(arizona
->regmap
, ARIZONA_HEADPHONE_DETECT_2
,
1056 dev_err(arizona
->dev
,
1057 "Failed to read HPDET state: %d\n", ret
);
1061 switch (info
->hpdet_ip_version
) {
1063 if (val
& ARIZONA_HP_DONE
)
1067 if (val
& ARIZONA_HP_DONE_B
)
1072 msleep(ARIZONA_HPDET_WAIT_DELAY_MS
);
1075 dev_warn(arizona
->dev
, "HPDET did not appear to complete\n");
1080 static irqreturn_t
arizona_jackdet(int irq
, void *data
)
1082 struct arizona_extcon_info
*info
= data
;
1083 struct arizona
*arizona
= info
->arizona
;
1084 unsigned int val
, present
, mask
;
1085 bool cancelled_hp
, cancelled_mic
;
1088 cancelled_hp
= cancel_delayed_work_sync(&info
->hpdet_work
);
1089 cancelled_mic
= cancel_delayed_work_sync(&info
->micd_timeout_work
);
1091 pm_runtime_get_sync(info
->dev
);
1093 mutex_lock(&info
->lock
);
1095 if (info
->micd_clamp
) {
1096 mask
= ARIZONA_MICD_CLAMP_STS
;
1099 mask
= ARIZONA_JD1_STS
;
1100 if (arizona
->pdata
.jd_invert
)
1103 present
= ARIZONA_JD1_STS
;
1106 ret
= regmap_read(arizona
->regmap
, ARIZONA_AOD_IRQ_RAW_STATUS
, &val
);
1108 dev_err(arizona
->dev
, "Failed to read jackdet status: %d\n",
1110 mutex_unlock(&info
->lock
);
1111 pm_runtime_put_autosuspend(info
->dev
);
1116 if (val
== info
->last_jackdet
) {
1117 dev_dbg(arizona
->dev
, "Suppressing duplicate JACKDET\n");
1119 queue_delayed_work(system_power_efficient_wq
,
1121 msecs_to_jiffies(HPDET_DEBOUNCE
));
1123 if (cancelled_mic
) {
1124 int micd_timeout
= info
->micd_timeout
;
1126 queue_delayed_work(system_power_efficient_wq
,
1127 &info
->micd_timeout_work
,
1128 msecs_to_jiffies(micd_timeout
));
1133 info
->last_jackdet
= val
;
1135 if (info
->last_jackdet
== present
) {
1136 dev_dbg(arizona
->dev
, "Detected jack\n");
1137 ret
= extcon_set_state_sync(info
->edev
,
1138 EXTCON_MECHANICAL
, true);
1141 dev_err(arizona
->dev
, "Mechanical report failed: %d\n",
1144 if (!arizona
->pdata
.hpdet_acc_id
) {
1145 info
->detecting
= true;
1147 info
->jack_flips
= 0;
1149 arizona_start_mic(info
);
1151 queue_delayed_work(system_power_efficient_wq
,
1153 msecs_to_jiffies(HPDET_DEBOUNCE
));
1156 if (info
->micd_clamp
|| !arizona
->pdata
.jd_invert
)
1157 regmap_update_bits(arizona
->regmap
,
1158 ARIZONA_JACK_DETECT_DEBOUNCE
,
1159 ARIZONA_MICD_CLAMP_DB
|
1162 dev_dbg(arizona
->dev
, "Detected jack removal\n");
1164 arizona_stop_mic(info
);
1166 info
->num_hpdet_res
= 0;
1167 for (i
= 0; i
< ARRAY_SIZE(info
->hpdet_res
); i
++)
1168 info
->hpdet_res
[i
] = 0;
1170 info
->hpdet_done
= false;
1171 info
->hpdet_retried
= false;
1173 for (i
= 0; i
< info
->num_micd_ranges
; i
++)
1174 input_report_key(info
->input
,
1175 info
->micd_ranges
[i
].key
, 0);
1176 input_sync(info
->input
);
1178 for (i
= 0; i
< ARRAY_SIZE(arizona_cable
) - 1; i
++) {
1179 ret
= extcon_set_state_sync(info
->edev
,
1180 arizona_cable
[i
], false);
1182 dev_err(arizona
->dev
,
1183 "Removal report failed: %d\n", ret
);
1187 * If the jack was removed during a headphone detection we
1188 * need to wait for the headphone detection to finish, as
1189 * it can not be aborted. We don't want to be able to start
1190 * a new headphone detection from a fresh insert until this
1193 arizona_hpdet_wait(info
);
1195 regmap_update_bits(arizona
->regmap
,
1196 ARIZONA_JACK_DETECT_DEBOUNCE
,
1197 ARIZONA_MICD_CLAMP_DB
| ARIZONA_JD1_DB
,
1198 ARIZONA_MICD_CLAMP_DB
| ARIZONA_JD1_DB
);
1201 if (arizona
->pdata
.micd_timeout
)
1202 info
->micd_timeout
= arizona
->pdata
.micd_timeout
;
1204 info
->micd_timeout
= DEFAULT_MICD_TIMEOUT
;
1207 /* Clear trig_sts to make sure DCVDD is not forced up */
1208 regmap_write(arizona
->regmap
, ARIZONA_AOD_WKUP_AND_TRIG
,
1209 ARIZONA_MICD_CLAMP_FALL_TRIG_STS
|
1210 ARIZONA_MICD_CLAMP_RISE_TRIG_STS
|
1211 ARIZONA_JD1_FALL_TRIG_STS
|
1212 ARIZONA_JD1_RISE_TRIG_STS
);
1214 mutex_unlock(&info
->lock
);
1216 pm_runtime_mark_last_busy(info
->dev
);
1217 pm_runtime_put_autosuspend(info
->dev
);
1222 /* Map a level onto a slot in the register bank */
1223 static void arizona_micd_set_level(struct arizona
*arizona
, int index
,
1229 reg
= ARIZONA_MIC_DETECT_LEVEL_4
- (index
/ 2);
1238 /* Program the level itself */
1239 regmap_update_bits(arizona
->regmap
, reg
, mask
, level
);
1242 static int arizona_extcon_get_micd_configs(struct device
*dev
,
1243 struct arizona
*arizona
)
1245 const char * const prop
= "wlf,micd-configs";
1246 const int entries_per_config
= 3;
1247 struct arizona_micd_config
*micd_configs
;
1252 nconfs
= device_property_read_u32_array(arizona
->dev
, prop
, NULL
, 0);
1256 vals
= kcalloc(nconfs
, sizeof(u32
), GFP_KERNEL
);
1260 ret
= device_property_read_u32_array(arizona
->dev
, prop
, vals
, nconfs
);
1264 nconfs
/= entries_per_config
;
1265 micd_configs
= devm_kcalloc(dev
, nconfs
, sizeof(*micd_configs
),
1267 if (!micd_configs
) {
1272 for (i
= 0, j
= 0; i
< nconfs
; ++i
) {
1273 micd_configs
[i
].src
= vals
[j
++] ? ARIZONA_ACCDET_SRC
: 0;
1274 micd_configs
[i
].bias
= vals
[j
++];
1275 micd_configs
[i
].gpio
= vals
[j
++];
1278 arizona
->pdata
.micd_configs
= micd_configs
;
1279 arizona
->pdata
.num_micd_configs
= nconfs
;
1286 static int arizona_extcon_device_get_pdata(struct device
*dev
,
1287 struct arizona
*arizona
)
1289 struct arizona_pdata
*pdata
= &arizona
->pdata
;
1290 unsigned int val
= ARIZONA_ACCDET_MODE_HPL
;
1293 device_property_read_u32(arizona
->dev
, "wlf,hpdet-channel", &val
);
1295 case ARIZONA_ACCDET_MODE_HPL
:
1296 case ARIZONA_ACCDET_MODE_HPR
:
1297 pdata
->hpdet_channel
= val
;
1300 dev_err(arizona
->dev
,
1301 "Wrong wlf,hpdet-channel DT value %d\n", val
);
1302 pdata
->hpdet_channel
= ARIZONA_ACCDET_MODE_HPL
;
1305 device_property_read_u32(arizona
->dev
, "wlf,micd-detect-debounce",
1306 &pdata
->micd_detect_debounce
);
1308 device_property_read_u32(arizona
->dev
, "wlf,micd-bias-start-time",
1309 &pdata
->micd_bias_start_time
);
1311 device_property_read_u32(arizona
->dev
, "wlf,micd-rate",
1314 device_property_read_u32(arizona
->dev
, "wlf,micd-dbtime",
1315 &pdata
->micd_dbtime
);
1317 device_property_read_u32(arizona
->dev
, "wlf,micd-timeout-ms",
1318 &pdata
->micd_timeout
);
1320 pdata
->micd_force_micbias
= device_property_read_bool(arizona
->dev
,
1321 "wlf,micd-force-micbias");
1323 pdata
->micd_software_compare
= device_property_read_bool(arizona
->dev
,
1324 "wlf,micd-software-compare");
1326 pdata
->jd_invert
= device_property_read_bool(arizona
->dev
,
1329 device_property_read_u32(arizona
->dev
, "wlf,gpsw", &pdata
->gpsw
);
1331 pdata
->jd_gpio5
= device_property_read_bool(arizona
->dev
,
1333 pdata
->jd_gpio5_nopull
= device_property_read_bool(arizona
->dev
,
1334 "wlf,use-jd2-nopull");
1336 ret
= arizona_extcon_get_micd_configs(dev
, arizona
);
1338 dev_err(arizona
->dev
, "Failed to read micd configs: %d\n", ret
);
1343 static int arizona_extcon_probe(struct platform_device
*pdev
)
1345 struct arizona
*arizona
= dev_get_drvdata(pdev
->dev
.parent
);
1346 struct arizona_pdata
*pdata
= &arizona
->pdata
;
1347 struct arizona_extcon_info
*info
;
1349 unsigned int clamp_mode
;
1350 int jack_irq_fall
, jack_irq_rise
;
1351 int ret
, mode
, i
, j
;
1353 if (!arizona
->dapm
|| !arizona
->dapm
->card
)
1354 return -EPROBE_DEFER
;
1356 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
1360 if (!dev_get_platdata(arizona
->dev
))
1361 arizona_extcon_device_get_pdata(&pdev
->dev
, arizona
);
1363 info
->micvdd
= devm_regulator_get(&pdev
->dev
, "MICVDD");
1364 if (IS_ERR(info
->micvdd
)) {
1365 ret
= PTR_ERR(info
->micvdd
);
1366 dev_err(arizona
->dev
, "Failed to get MICVDD: %d\n", ret
);
1370 mutex_init(&info
->lock
);
1371 info
->arizona
= arizona
;
1372 info
->dev
= &pdev
->dev
;
1373 info
->last_jackdet
= ~(ARIZONA_MICD_CLAMP_STS
| ARIZONA_JD1_STS
);
1374 INIT_DELAYED_WORK(&info
->hpdet_work
, arizona_hpdet_work
);
1375 INIT_DELAYED_WORK(&info
->micd_detect_work
, arizona_micd_detect
);
1376 INIT_DELAYED_WORK(&info
->micd_timeout_work
, arizona_micd_timeout_work
);
1377 platform_set_drvdata(pdev
, info
);
1379 switch (arizona
->type
) {
1381 switch (arizona
->rev
) {
1383 info
->micd_reva
= true;
1386 info
->micd_clamp
= true;
1387 info
->hpdet_ip_version
= 1;
1393 switch (arizona
->rev
) {
1397 info
->micd_clamp
= true;
1398 info
->hpdet_ip_version
= 2;
1404 info
->micd_clamp
= true;
1405 info
->hpdet_ip_version
= 2;
1411 info
->edev
= devm_extcon_dev_allocate(&pdev
->dev
, arizona_cable
);
1412 if (IS_ERR(info
->edev
)) {
1413 dev_err(&pdev
->dev
, "failed to allocate extcon device\n");
1417 ret
= devm_extcon_dev_register(&pdev
->dev
, info
->edev
);
1419 dev_err(arizona
->dev
, "extcon_dev_register() failed: %d\n",
1424 info
->input
= devm_input_allocate_device(&pdev
->dev
);
1426 dev_err(arizona
->dev
, "Can't allocate input dev\n");
1431 info
->input
->name
= "Headset";
1432 info
->input
->phys
= "arizona/extcon";
1434 if (pdata
->num_micd_configs
) {
1435 info
->micd_modes
= pdata
->micd_configs
;
1436 info
->micd_num_modes
= pdata
->num_micd_configs
;
1438 info
->micd_modes
= micd_default_modes
;
1439 info
->micd_num_modes
= ARRAY_SIZE(micd_default_modes
);
1442 if (arizona
->pdata
.gpsw
> 0)
1443 regmap_update_bits(arizona
->regmap
, ARIZONA_GP_SWITCH_1
,
1444 ARIZONA_SW1_MODE_MASK
, arizona
->pdata
.gpsw
);
1446 if (pdata
->micd_pol_gpio
> 0) {
1447 if (info
->micd_modes
[0].gpio
)
1448 mode
= GPIOF_OUT_INIT_HIGH
;
1450 mode
= GPIOF_OUT_INIT_LOW
;
1452 ret
= devm_gpio_request_one(&pdev
->dev
, pdata
->micd_pol_gpio
,
1453 mode
, "MICD polarity");
1455 dev_err(arizona
->dev
, "Failed to request GPIO%d: %d\n",
1456 pdata
->micd_pol_gpio
, ret
);
1460 info
->micd_pol_gpio
= gpio_to_desc(pdata
->micd_pol_gpio
);
1462 if (info
->micd_modes
[0].gpio
)
1463 mode
= GPIOD_OUT_HIGH
;
1465 mode
= GPIOD_OUT_LOW
;
1467 /* We can't use devm here because we need to do the get
1468 * against the MFD device, as that is where the of_node
1469 * will reside, but if we devm against that the GPIO
1470 * will not be freed if the extcon driver is unloaded.
1472 info
->micd_pol_gpio
= gpiod_get_optional(arizona
->dev
,
1475 if (IS_ERR(info
->micd_pol_gpio
)) {
1476 ret
= PTR_ERR(info
->micd_pol_gpio
);
1477 dev_err(arizona
->dev
,
1478 "Failed to get microphone polarity GPIO: %d\n",
1484 if (arizona
->pdata
.hpdet_id_gpio
> 0) {
1485 ret
= devm_gpio_request_one(&pdev
->dev
,
1486 arizona
->pdata
.hpdet_id_gpio
,
1490 dev_err(arizona
->dev
, "Failed to request GPIO%d: %d\n",
1491 arizona
->pdata
.hpdet_id_gpio
, ret
);
1496 if (arizona
->pdata
.micd_bias_start_time
)
1497 regmap_update_bits(arizona
->regmap
, ARIZONA_MIC_DETECT_1
,
1498 ARIZONA_MICD_BIAS_STARTTIME_MASK
,
1499 arizona
->pdata
.micd_bias_start_time
1500 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT
);
1502 if (arizona
->pdata
.micd_rate
)
1503 regmap_update_bits(arizona
->regmap
, ARIZONA_MIC_DETECT_1
,
1504 ARIZONA_MICD_RATE_MASK
,
1505 arizona
->pdata
.micd_rate
1506 << ARIZONA_MICD_RATE_SHIFT
);
1508 switch (arizona
->pdata
.micd_dbtime
) {
1509 case MICD_DBTIME_FOUR_READINGS
:
1510 regmap_update_bits(arizona
->regmap
, ARIZONA_MIC_DETECT_1
,
1511 ARIZONA_MICD_DBTIME_MASK
,
1512 ARIZONA_MICD_DBTIME
);
1514 case MICD_DBTIME_TWO_READINGS
:
1515 regmap_update_bits(arizona
->regmap
, ARIZONA_MIC_DETECT_1
,
1516 ARIZONA_MICD_DBTIME_MASK
, 0);
1522 BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels
) <
1523 ARIZONA_NUM_MICD_BUTTON_LEVELS
);
1525 if (arizona
->pdata
.num_micd_ranges
) {
1526 info
->micd_ranges
= pdata
->micd_ranges
;
1527 info
->num_micd_ranges
= pdata
->num_micd_ranges
;
1529 info
->micd_ranges
= micd_default_ranges
;
1530 info
->num_micd_ranges
= ARRAY_SIZE(micd_default_ranges
);
1533 if (arizona
->pdata
.num_micd_ranges
> ARIZONA_MAX_MICD_RANGE
) {
1534 dev_err(arizona
->dev
, "Too many MICD ranges: %d\n",
1535 arizona
->pdata
.num_micd_ranges
);
1538 if (info
->num_micd_ranges
> 1) {
1539 for (i
= 1; i
< info
->num_micd_ranges
; i
++) {
1540 if (info
->micd_ranges
[i
- 1].max
>
1541 info
->micd_ranges
[i
].max
) {
1542 dev_err(arizona
->dev
,
1543 "MICD ranges must be sorted\n");
1550 /* Disable all buttons by default */
1551 regmap_update_bits(arizona
->regmap
, ARIZONA_MIC_DETECT_2
,
1552 ARIZONA_MICD_LVL_SEL_MASK
, 0x81);
1554 /* Set up all the buttons the user specified */
1555 for (i
= 0; i
< info
->num_micd_ranges
; i
++) {
1556 for (j
= 0; j
< ARIZONA_NUM_MICD_BUTTON_LEVELS
; j
++)
1557 if (arizona_micd_levels
[j
] >= info
->micd_ranges
[i
].max
)
1560 if (j
== ARIZONA_NUM_MICD_BUTTON_LEVELS
) {
1561 dev_err(arizona
->dev
, "Unsupported MICD level %d\n",
1562 info
->micd_ranges
[i
].max
);
1567 dev_dbg(arizona
->dev
, "%d ohms for MICD threshold %d\n",
1568 arizona_micd_levels
[j
], i
);
1570 arizona_micd_set_level(arizona
, i
, j
);
1571 input_set_capability(info
->input
, EV_KEY
,
1572 info
->micd_ranges
[i
].key
);
1574 /* Enable reporting of that range */
1575 regmap_update_bits(arizona
->regmap
, ARIZONA_MIC_DETECT_2
,
1579 /* Set all the remaining keys to a maximum */
1580 for (; i
< ARIZONA_MAX_MICD_RANGE
; i
++)
1581 arizona_micd_set_level(arizona
, i
, 0x3f);
1584 * If we have a clamp use it, activating in conjunction with
1585 * GPIO5 if that is connected for jack detect operation.
1587 if (info
->micd_clamp
) {
1588 if (arizona
->pdata
.jd_gpio5
) {
1589 /* Put the GPIO into input mode with optional pull */
1591 if (arizona
->pdata
.jd_gpio5_nopull
)
1592 val
&= ~ARIZONA_GPN_PU
;
1594 regmap_write(arizona
->regmap
, ARIZONA_GPIO5_CTRL
,
1597 if (arizona
->pdata
.jd_invert
)
1598 clamp_mode
= ARIZONA_MICD_CLAMP_MODE_JDH_GP5H
;
1600 clamp_mode
= ARIZONA_MICD_CLAMP_MODE_JDL_GP5H
;
1602 if (arizona
->pdata
.jd_invert
)
1603 clamp_mode
= ARIZONA_MICD_CLAMP_MODE_JDH
;
1605 clamp_mode
= ARIZONA_MICD_CLAMP_MODE_JDL
;
1608 regmap_update_bits(arizona
->regmap
,
1609 ARIZONA_MICD_CLAMP_CONTROL
,
1610 ARIZONA_MICD_CLAMP_MODE_MASK
, clamp_mode
);
1612 regmap_update_bits(arizona
->regmap
,
1613 ARIZONA_JACK_DETECT_DEBOUNCE
,
1614 ARIZONA_MICD_CLAMP_DB
,
1615 ARIZONA_MICD_CLAMP_DB
);
1618 arizona_extcon_set_mode(info
, 0);
1620 pm_runtime_enable(&pdev
->dev
);
1621 pm_runtime_idle(&pdev
->dev
);
1622 pm_runtime_get_sync(&pdev
->dev
);
1624 if (info
->micd_clamp
) {
1625 jack_irq_rise
= ARIZONA_IRQ_MICD_CLAMP_RISE
;
1626 jack_irq_fall
= ARIZONA_IRQ_MICD_CLAMP_FALL
;
1628 jack_irq_rise
= ARIZONA_IRQ_JD_RISE
;
1629 jack_irq_fall
= ARIZONA_IRQ_JD_FALL
;
1632 ret
= arizona_request_irq(arizona
, jack_irq_rise
,
1633 "JACKDET rise", arizona_jackdet
, info
);
1635 dev_err(&pdev
->dev
, "Failed to get JACKDET rise IRQ: %d\n",
1640 ret
= arizona_set_irq_wake(arizona
, jack_irq_rise
, 1);
1642 dev_err(&pdev
->dev
, "Failed to set JD rise IRQ wake: %d\n",
1647 ret
= arizona_request_irq(arizona
, jack_irq_fall
,
1648 "JACKDET fall", arizona_jackdet
, info
);
1650 dev_err(&pdev
->dev
, "Failed to get JD fall IRQ: %d\n", ret
);
1654 ret
= arizona_set_irq_wake(arizona
, jack_irq_fall
, 1);
1656 dev_err(&pdev
->dev
, "Failed to set JD fall IRQ wake: %d\n",
1661 ret
= arizona_request_irq(arizona
, ARIZONA_IRQ_MICDET
,
1662 "MICDET", arizona_micdet
, info
);
1664 dev_err(&pdev
->dev
, "Failed to get MICDET IRQ: %d\n", ret
);
1668 ret
= arizona_request_irq(arizona
, ARIZONA_IRQ_HPDET
,
1669 "HPDET", arizona_hpdet_irq
, info
);
1671 dev_err(&pdev
->dev
, "Failed to get HPDET IRQ: %d\n", ret
);
1675 arizona_clk32k_enable(arizona
);
1676 regmap_update_bits(arizona
->regmap
, ARIZONA_JACK_DETECT_DEBOUNCE
,
1677 ARIZONA_JD1_DB
, ARIZONA_JD1_DB
);
1678 regmap_update_bits(arizona
->regmap
, ARIZONA_JACK_DETECT_ANALOGUE
,
1679 ARIZONA_JD1_ENA
, ARIZONA_JD1_ENA
);
1681 ret
= regulator_allow_bypass(info
->micvdd
, true);
1683 dev_warn(arizona
->dev
, "Failed to set MICVDD to bypass: %d\n",
1686 pm_runtime_put(&pdev
->dev
);
1688 ret
= input_register_device(info
->input
);
1690 dev_err(&pdev
->dev
, "Can't register input device: %d\n", ret
);
1697 arizona_free_irq(arizona
, ARIZONA_IRQ_HPDET
, info
);
1699 arizona_free_irq(arizona
, ARIZONA_IRQ_MICDET
, info
);
1701 arizona_set_irq_wake(arizona
, jack_irq_fall
, 0);
1703 arizona_free_irq(arizona
, jack_irq_fall
, info
);
1705 arizona_set_irq_wake(arizona
, jack_irq_rise
, 0);
1707 arizona_free_irq(arizona
, jack_irq_rise
, info
);
1709 gpiod_put(info
->micd_pol_gpio
);
1711 pm_runtime_disable(&pdev
->dev
);
1715 static int arizona_extcon_remove(struct platform_device
*pdev
)
1717 struct arizona_extcon_info
*info
= platform_get_drvdata(pdev
);
1718 struct arizona
*arizona
= info
->arizona
;
1719 int jack_irq_rise
, jack_irq_fall
;
1722 regmap_update_bits_check(arizona
->regmap
, ARIZONA_MIC_DETECT_1
,
1723 ARIZONA_MICD_ENA
, 0,
1727 regulator_disable(info
->micvdd
);
1728 pm_runtime_put(info
->dev
);
1731 gpiod_put(info
->micd_pol_gpio
);
1733 pm_runtime_disable(&pdev
->dev
);
1735 regmap_update_bits(arizona
->regmap
,
1736 ARIZONA_MICD_CLAMP_CONTROL
,
1737 ARIZONA_MICD_CLAMP_MODE_MASK
, 0);
1739 if (info
->micd_clamp
) {
1740 jack_irq_rise
= ARIZONA_IRQ_MICD_CLAMP_RISE
;
1741 jack_irq_fall
= ARIZONA_IRQ_MICD_CLAMP_FALL
;
1743 jack_irq_rise
= ARIZONA_IRQ_JD_RISE
;
1744 jack_irq_fall
= ARIZONA_IRQ_JD_FALL
;
1747 arizona_set_irq_wake(arizona
, jack_irq_rise
, 0);
1748 arizona_set_irq_wake(arizona
, jack_irq_fall
, 0);
1749 arizona_free_irq(arizona
, ARIZONA_IRQ_HPDET
, info
);
1750 arizona_free_irq(arizona
, ARIZONA_IRQ_MICDET
, info
);
1751 arizona_free_irq(arizona
, jack_irq_rise
, info
);
1752 arizona_free_irq(arizona
, jack_irq_fall
, info
);
1753 cancel_delayed_work_sync(&info
->hpdet_work
);
1754 regmap_update_bits(arizona
->regmap
, ARIZONA_JACK_DETECT_ANALOGUE
,
1755 ARIZONA_JD1_ENA
, 0);
1756 arizona_clk32k_disable(arizona
);
1761 static struct platform_driver arizona_extcon_driver
= {
1763 .name
= "arizona-extcon",
1765 .probe
= arizona_extcon_probe
,
1766 .remove
= arizona_extcon_remove
,
1769 module_platform_driver(arizona_extcon_driver
);
1771 MODULE_DESCRIPTION("Arizona Extcon driver");
1772 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1773 MODULE_LICENSE("GPL");
1774 MODULE_ALIAS("platform:extcon-arizona");