2 * extcon-arizona.c - Extcon driver Wolfson Arizona devices
4 * Copyright (C) 2012-2014 Wolfson Microelectronics plc
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/i2c.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/err.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/gpio.h>
25 #include <linux/input.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/property.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/extcon.h>
32 #include <sound/soc.h>
34 #include <linux/mfd/arizona/core.h>
35 #include <linux/mfd/arizona/pdata.h>
36 #include <linux/mfd/arizona/registers.h>
37 #include <dt-bindings/mfd/arizona.h>
39 #define ARIZONA_MAX_MICD_RANGE 8
41 #define ARIZONA_MICD_CLAMP_MODE_JDL 0x4
42 #define ARIZONA_MICD_CLAMP_MODE_JDH 0x5
43 #define ARIZONA_MICD_CLAMP_MODE_JDL_GP5H 0x9
44 #define ARIZONA_MICD_CLAMP_MODE_JDH_GP5H 0xb
46 #define ARIZONA_TST_CAP_DEFAULT 0x3
47 #define ARIZONA_TST_CAP_CLAMP 0x1
49 #define ARIZONA_HPDET_MAX 10000
51 #define HPDET_DEBOUNCE 500
52 #define DEFAULT_MICD_TIMEOUT 2000
54 #define QUICK_HEADPHONE_MAX_OHM 3
55 #define MICROPHONE_MIN_OHM 1257
56 #define MICROPHONE_MAX_OHM 30000
58 #define MICD_DBTIME_TWO_READINGS 2
59 #define MICD_DBTIME_FOUR_READINGS 4
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 | \
66 #define MICD_LVL_0_TO_7 (ARIZONA_MICD_LVL_0 | MICD_LVL_1_TO_7)
68 #define MICD_LVL_0_TO_8 (MICD_LVL_0_TO_7 | ARIZONA_MICD_LVL_8)
70 struct arizona_extcon_info
{
72 struct arizona
*arizona
;
74 struct regulator
*micvdd
;
75 struct input_dev
*input
;
80 const struct arizona_micd_config
*micd_modes
;
83 const struct arizona_micd_range
*micd_ranges
;
91 struct delayed_work hpdet_work
;
92 struct delayed_work micd_detect_work
;
93 struct delayed_work micd_timeout_work
;
100 unsigned int hpdet_res
[3];
106 int hpdet_ip_version
;
108 struct extcon_dev
*edev
;
110 struct gpio_desc
*micd_pol_gpio
;
113 static const struct arizona_micd_config micd_default_modes
[] = {
114 { ARIZONA_ACCDET_SRC
, 1, 0 },
118 static const struct arizona_micd_range micd_default_ranges
[] = {
119 { .max
= 11, .key
= BTN_0
},
120 { .max
= 28, .key
= BTN_1
},
121 { .max
= 54, .key
= BTN_2
},
122 { .max
= 100, .key
= BTN_3
},
123 { .max
= 186, .key
= BTN_4
},
124 { .max
= 430, .key
= BTN_5
},
127 /* The number of levels in arizona_micd_levels valid for button thresholds */
128 #define ARIZONA_NUM_MICD_BUTTON_LEVELS 64
130 static const int arizona_micd_levels
[] = {
131 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
132 49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
133 105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
134 270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
138 static const unsigned int arizona_cable
[] = {
140 EXTCON_JACK_MICROPHONE
,
141 EXTCON_JACK_HEADPHONE
,
142 EXTCON_JACK_LINE_OUT
,
146 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info
*info
);
148 static void arizona_extcon_hp_clamp(struct arizona_extcon_info
*info
,
151 struct arizona
*arizona
= info
->arizona
;
152 unsigned int mask
= 0, val
= 0;
153 unsigned int cap_sel
= 0;
156 switch (arizona
->type
) {
163 mask
= ARIZONA_HP1L_SHRTO
| ARIZONA_HP1L_FLWR
|
166 val
= ARIZONA_HP1L_SHRTO
;
167 cap_sel
= ARIZONA_TST_CAP_CLAMP
;
169 val
= ARIZONA_HP1L_FLWR
| ARIZONA_HP1L_SHRTI
;
170 cap_sel
= ARIZONA_TST_CAP_DEFAULT
;
173 ret
= regmap_update_bits(arizona
->regmap
,
174 ARIZONA_HP_TEST_CTRL_1
,
175 ARIZONA_HP1_TST_CAP_SEL_MASK
,
178 dev_warn(arizona
->dev
,
179 "Failed to set TST_CAP_SEL: %d\n", ret
);
182 mask
= ARIZONA_RMV_SHRT_HP1L
;
184 val
= ARIZONA_RMV_SHRT_HP1L
;
188 snd_soc_dapm_mutex_lock(arizona
->dapm
);
190 arizona
->hpdet_clamp
= clamp
;
192 /* Keep the HP output stages disabled while doing the clamp */
194 ret
= regmap_update_bits(arizona
->regmap
,
195 ARIZONA_OUTPUT_ENABLES_1
,
197 ARIZONA_OUT1R_ENA
, 0);
199 dev_warn(arizona
->dev
,
200 "Failed to disable headphone outputs: %d\n",
205 ret
= regmap_update_bits(arizona
->regmap
, ARIZONA_HP_CTRL_1L
,
208 dev_warn(arizona
->dev
, "Failed to do clamp: %d\n",
211 ret
= regmap_update_bits(arizona
->regmap
, ARIZONA_HP_CTRL_1R
,
214 dev_warn(arizona
->dev
, "Failed to do clamp: %d\n",
218 /* Restore the desired state while not doing the clamp */
220 ret
= regmap_update_bits(arizona
->regmap
,
221 ARIZONA_OUTPUT_ENABLES_1
,
223 ARIZONA_OUT1R_ENA
, arizona
->hp_ena
);
225 dev_warn(arizona
->dev
,
226 "Failed to restore headphone outputs: %d\n",
230 snd_soc_dapm_mutex_unlock(arizona
->dapm
);
233 static void arizona_extcon_set_mode(struct arizona_extcon_info
*info
, int mode
)
235 struct arizona
*arizona
= info
->arizona
;
237 mode
%= info
->micd_num_modes
;
239 gpiod_set_value_cansleep(info
->micd_pol_gpio
,
240 info
->micd_modes
[mode
].gpio
);
242 regmap_update_bits(arizona
->regmap
, ARIZONA_MIC_DETECT_1
,
243 ARIZONA_MICD_BIAS_SRC_MASK
,
244 info
->micd_modes
[mode
].bias
<<
245 ARIZONA_MICD_BIAS_SRC_SHIFT
);
246 regmap_update_bits(arizona
->regmap
, ARIZONA_ACCESSORY_DETECT_MODE_1
,
247 ARIZONA_ACCDET_SRC
, info
->micd_modes
[mode
].src
);
249 info
->micd_mode
= mode
;
251 dev_dbg(arizona
->dev
, "Set jack polarity to %d\n", mode
);
254 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info
*info
)
256 switch (info
->micd_modes
[0].bias
) {
268 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info
*info
)
270 struct arizona
*arizona
= info
->arizona
;
271 const char *widget
= arizona_extcon_get_micbias(info
);
272 struct snd_soc_dapm_context
*dapm
= arizona
->dapm
;
273 struct snd_soc_component
*component
= snd_soc_dapm_to_component(dapm
);
276 ret
= snd_soc_component_force_enable_pin(component
, widget
);
278 dev_warn(arizona
->dev
, "Failed to enable %s: %d\n",
281 snd_soc_dapm_sync(dapm
);
283 if (!arizona
->pdata
.micd_force_micbias
) {
284 ret
= snd_soc_component_disable_pin(component
, widget
);
286 dev_warn(arizona
->dev
, "Failed to disable %s: %d\n",
289 snd_soc_dapm_sync(dapm
);
293 static void arizona_start_mic(struct arizona_extcon_info
*info
)
295 struct arizona
*arizona
= info
->arizona
;
300 /* Microphone detection can't use idle mode */
301 pm_runtime_get(info
->dev
);
303 if (info
->detecting
) {
304 ret
= regulator_allow_bypass(info
->micvdd
, false);
306 dev_err(arizona
->dev
,
307 "Failed to regulate MICVDD: %d\n",
312 ret
= regulator_enable(info
->micvdd
);
314 dev_err(arizona
->dev
, "Failed to enable MICVDD: %d\n",
318 if (info
->micd_reva
) {
319 regmap_write(arizona
->regmap
, 0x80, 0x3);
320 regmap_write(arizona
->regmap
, 0x294, 0);
321 regmap_write(arizona
->regmap
, 0x80, 0x0);
324 if (info
->detecting
&& arizona
->pdata
.micd_software_compare
)
325 mode
= ARIZONA_ACCDET_MODE_ADC
;
327 mode
= ARIZONA_ACCDET_MODE_MIC
;
329 regmap_update_bits(arizona
->regmap
,
330 ARIZONA_ACCESSORY_DETECT_MODE_1
,
331 ARIZONA_ACCDET_MODE_MASK
, mode
);
333 arizona_extcon_pulse_micbias(info
);
335 regmap_update_bits_check(arizona
->regmap
, ARIZONA_MIC_DETECT_1
,
336 ARIZONA_MICD_ENA
, ARIZONA_MICD_ENA
,
339 regulator_disable(info
->micvdd
);
340 pm_runtime_put_autosuspend(info
->dev
);
344 static void arizona_stop_mic(struct arizona_extcon_info
*info
)
346 struct arizona
*arizona
= info
->arizona
;
347 const char *widget
= arizona_extcon_get_micbias(info
);
348 struct snd_soc_dapm_context
*dapm
= arizona
->dapm
;
349 struct snd_soc_component
*component
= snd_soc_dapm_to_component(dapm
);
353 regmap_update_bits_check(arizona
->regmap
, ARIZONA_MIC_DETECT_1
,
357 ret
= snd_soc_component_disable_pin(component
, widget
);
359 dev_warn(arizona
->dev
,
360 "Failed to disable %s: %d\n",
363 snd_soc_dapm_sync(dapm
);
365 if (info
->micd_reva
) {
366 regmap_write(arizona
->regmap
, 0x80, 0x3);
367 regmap_write(arizona
->regmap
, 0x294, 2);
368 regmap_write(arizona
->regmap
, 0x80, 0x0);
371 ret
= regulator_allow_bypass(info
->micvdd
, true);
373 dev_err(arizona
->dev
, "Failed to bypass MICVDD: %d\n",
378 regulator_disable(info
->micvdd
);
379 pm_runtime_mark_last_busy(info
->dev
);
380 pm_runtime_put_autosuspend(info
->dev
);
385 unsigned int threshold
;
386 unsigned int factor_a
;
387 unsigned int factor_b
;
388 } arizona_hpdet_b_ranges
[] = {
389 { 100, 5528, 362464 },
390 { 169, 11084, 6186851 },
391 { 169, 11065, 65460395 },
394 #define ARIZONA_HPDET_B_RANGE_MAX 0x3fb
399 } arizona_hpdet_c_ranges
[] = {
406 static int arizona_hpdet_read(struct arizona_extcon_info
*info
)
408 struct arizona
*arizona
= info
->arizona
;
409 unsigned int val
, range
;
412 ret
= regmap_read(arizona
->regmap
, ARIZONA_HEADPHONE_DETECT_2
, &val
);
414 dev_err(arizona
->dev
, "Failed to read HPDET status: %d\n",
419 switch (info
->hpdet_ip_version
) {
421 if (!(val
& ARIZONA_HP_DONE
)) {
422 dev_err(arizona
->dev
, "HPDET did not complete: %x\n",
427 val
&= ARIZONA_HP_LVL_MASK
;
431 if (!(val
& ARIZONA_HP_DONE_B
)) {
432 dev_err(arizona
->dev
, "HPDET did not complete: %x\n",
437 ret
= regmap_read(arizona
->regmap
, ARIZONA_HP_DACVAL
, &val
);
439 dev_err(arizona
->dev
, "Failed to read HP value: %d\n",
444 regmap_read(arizona
->regmap
, ARIZONA_HEADPHONE_DETECT_1
,
446 range
= (range
& ARIZONA_HP_IMPEDANCE_RANGE_MASK
)
447 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT
;
449 if (range
< ARRAY_SIZE(arizona_hpdet_b_ranges
) - 1 &&
450 (val
< arizona_hpdet_b_ranges
[range
].threshold
||
451 val
>= ARIZONA_HPDET_B_RANGE_MAX
)) {
453 dev_dbg(arizona
->dev
, "Moving to HPDET range %d\n",
455 regmap_update_bits(arizona
->regmap
,
456 ARIZONA_HEADPHONE_DETECT_1
,
457 ARIZONA_HP_IMPEDANCE_RANGE_MASK
,
459 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT
);
463 /* If we go out of range report top of range */
464 if (val
< arizona_hpdet_b_ranges
[range
].threshold
||
465 val
>= ARIZONA_HPDET_B_RANGE_MAX
) {
466 dev_dbg(arizona
->dev
, "Measurement out of range\n");
467 return ARIZONA_HPDET_MAX
;
470 dev_dbg(arizona
->dev
, "HPDET read %d in range %d\n",
473 val
= arizona_hpdet_b_ranges
[range
].factor_b
475 arizona_hpdet_b_ranges
[range
].factor_a
);
479 if (!(val
& ARIZONA_HP_DONE_B
)) {
480 dev_err(arizona
->dev
, "HPDET did not complete: %x\n",
485 val
&= ARIZONA_HP_LVL_B_MASK
;
486 /* Convert to ohms, the value is in 0.5 ohm increments */
489 regmap_read(arizona
->regmap
, ARIZONA_HEADPHONE_DETECT_1
,
491 range
= (range
& ARIZONA_HP_IMPEDANCE_RANGE_MASK
)
492 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT
;
494 /* Skip up a range, or report? */
495 if (range
< ARRAY_SIZE(arizona_hpdet_c_ranges
) - 1 &&
496 (val
>= arizona_hpdet_c_ranges
[range
].max
)) {
498 dev_dbg(arizona
->dev
, "Moving to HPDET range %d-%d\n",
499 arizona_hpdet_c_ranges
[range
].min
,
500 arizona_hpdet_c_ranges
[range
].max
);
501 regmap_update_bits(arizona
->regmap
,
502 ARIZONA_HEADPHONE_DETECT_1
,
503 ARIZONA_HP_IMPEDANCE_RANGE_MASK
,
505 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT
);
509 if (range
&& (val
< arizona_hpdet_c_ranges
[range
].min
)) {
510 dev_dbg(arizona
->dev
, "Reporting range boundary %d\n",
511 arizona_hpdet_c_ranges
[range
].min
);
512 val
= arizona_hpdet_c_ranges
[range
].min
;
517 dev_warn(arizona
->dev
, "Unknown HPDET IP revision %d\n",
518 info
->hpdet_ip_version
);
522 dev_dbg(arizona
->dev
, "HP impedance %d ohms\n", val
);
526 static int arizona_hpdet_do_id(struct arizona_extcon_info
*info
, int *reading
,
529 struct arizona
*arizona
= info
->arizona
;
530 int id_gpio
= arizona
->pdata
.hpdet_id_gpio
;
533 * If we're using HPDET for accessory identification we need
534 * to take multiple measurements, step through them in sequence.
536 if (arizona
->pdata
.hpdet_acc_id
) {
537 info
->hpdet_res
[info
->num_hpdet_res
++] = *reading
;
539 /* Only check the mic directly if we didn't already ID it */
540 if (id_gpio
&& info
->num_hpdet_res
== 1) {
541 dev_dbg(arizona
->dev
, "Measuring mic\n");
543 regmap_update_bits(arizona
->regmap
,
544 ARIZONA_ACCESSORY_DETECT_MODE_1
,
545 ARIZONA_ACCDET_MODE_MASK
|
547 ARIZONA_ACCDET_MODE_HPR
|
548 info
->micd_modes
[0].src
);
550 gpio_set_value_cansleep(id_gpio
, 1);
552 regmap_update_bits(arizona
->regmap
,
553 ARIZONA_HEADPHONE_DETECT_1
,
554 ARIZONA_HP_POLL
, ARIZONA_HP_POLL
);
558 /* OK, got both. Now, compare... */
559 dev_dbg(arizona
->dev
, "HPDET measured %d %d\n",
560 info
->hpdet_res
[0], info
->hpdet_res
[1]);
562 /* Take the headphone impedance for the main report */
563 *reading
= info
->hpdet_res
[0];
565 /* Sometimes we get false readings due to slow insert */
566 if (*reading
>= ARIZONA_HPDET_MAX
&& !info
->hpdet_retried
) {
567 dev_dbg(arizona
->dev
, "Retrying high impedance\n");
568 info
->num_hpdet_res
= 0;
569 info
->hpdet_retried
= true;
570 arizona_start_hpdet_acc_id(info
);
571 pm_runtime_put(info
->dev
);
576 * If we measure the mic as high impedance
578 if (!id_gpio
|| info
->hpdet_res
[1] > 50) {
579 dev_dbg(arizona
->dev
, "Detected mic\n");
581 info
->detecting
= true;
583 dev_dbg(arizona
->dev
, "Detected headphone\n");
586 /* Make sure everything is reset back to the real polarity */
587 regmap_update_bits(arizona
->regmap
,
588 ARIZONA_ACCESSORY_DETECT_MODE_1
,
590 info
->micd_modes
[0].src
);
596 static irqreturn_t
arizona_hpdet_irq(int irq
, void *data
)
598 struct arizona_extcon_info
*info
= data
;
599 struct arizona
*arizona
= info
->arizona
;
600 int id_gpio
= arizona
->pdata
.hpdet_id_gpio
;
601 unsigned int report
= EXTCON_JACK_HEADPHONE
;
605 mutex_lock(&info
->lock
);
607 /* If we got a spurious IRQ for some reason then ignore it */
608 if (!info
->hpdet_active
) {
609 dev_warn(arizona
->dev
, "Spurious HPDET IRQ\n");
610 mutex_unlock(&info
->lock
);
614 /* If the cable was removed while measuring ignore the result */
615 ret
= extcon_get_state(info
->edev
, EXTCON_MECHANICAL
);
617 dev_err(arizona
->dev
, "Failed to check cable state: %d\n",
621 dev_dbg(arizona
->dev
, "Ignoring HPDET for removed cable\n");
625 ret
= arizona_hpdet_read(info
);
632 /* Reset back to starting range */
633 regmap_update_bits(arizona
->regmap
,
634 ARIZONA_HEADPHONE_DETECT_1
,
635 ARIZONA_HP_IMPEDANCE_RANGE_MASK
| ARIZONA_HP_POLL
,
638 ret
= arizona_hpdet_do_id(info
, &reading
, &mic
);
644 /* Report high impedence cables as line outputs */
646 report
= EXTCON_JACK_LINE_OUT
;
648 report
= EXTCON_JACK_HEADPHONE
;
650 ret
= extcon_set_state_sync(info
->edev
, report
, true);
652 dev_err(arizona
->dev
, "Failed to report HP/line: %d\n",
656 /* Reset back to starting range */
657 regmap_update_bits(arizona
->regmap
,
658 ARIZONA_HEADPHONE_DETECT_1
,
659 ARIZONA_HP_IMPEDANCE_RANGE_MASK
| ARIZONA_HP_POLL
,
662 arizona_extcon_hp_clamp(info
, false);
665 gpio_set_value_cansleep(id_gpio
, 0);
667 /* Revert back to MICDET mode */
668 regmap_update_bits(arizona
->regmap
,
669 ARIZONA_ACCESSORY_DETECT_MODE_1
,
670 ARIZONA_ACCDET_MODE_MASK
, ARIZONA_ACCDET_MODE_MIC
);
672 /* If we have a mic then reenable MICDET */
673 if (mic
|| info
->mic
)
674 arizona_start_mic(info
);
676 if (info
->hpdet_active
) {
677 pm_runtime_put_autosuspend(info
->dev
);
678 info
->hpdet_active
= false;
681 info
->hpdet_done
= true;
684 mutex_unlock(&info
->lock
);
689 static void arizona_identify_headphone(struct arizona_extcon_info
*info
)
691 struct arizona
*arizona
= info
->arizona
;
694 if (info
->hpdet_done
)
697 dev_dbg(arizona
->dev
, "Starting HPDET\n");
699 /* Make sure we keep the device enabled during the measurement */
700 pm_runtime_get(info
->dev
);
702 info
->hpdet_active
= true;
705 arizona_stop_mic(info
);
707 arizona_extcon_hp_clamp(info
, true);
709 ret
= regmap_update_bits(arizona
->regmap
,
710 ARIZONA_ACCESSORY_DETECT_MODE_1
,
711 ARIZONA_ACCDET_MODE_MASK
,
712 arizona
->pdata
.hpdet_channel
);
714 dev_err(arizona
->dev
, "Failed to set HPDET mode: %d\n", ret
);
718 ret
= regmap_update_bits(arizona
->regmap
, ARIZONA_HEADPHONE_DETECT_1
,
719 ARIZONA_HP_POLL
, ARIZONA_HP_POLL
);
721 dev_err(arizona
->dev
, "Can't start HPDETL measurement: %d\n",
729 regmap_update_bits(arizona
->regmap
, ARIZONA_ACCESSORY_DETECT_MODE_1
,
730 ARIZONA_ACCDET_MODE_MASK
, ARIZONA_ACCDET_MODE_MIC
);
732 /* Just report headphone */
733 ret
= extcon_set_state_sync(info
->edev
, EXTCON_JACK_HEADPHONE
, true);
735 dev_err(arizona
->dev
, "Failed to report headphone: %d\n", ret
);
738 arizona_start_mic(info
);
740 info
->hpdet_active
= false;
743 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info
*info
)
745 struct arizona
*arizona
= info
->arizona
;
750 dev_dbg(arizona
->dev
, "Starting identification via HPDET\n");
752 /* Make sure we keep the device enabled during the measurement */
753 pm_runtime_get_sync(info
->dev
);
755 info
->hpdet_active
= true;
757 arizona_extcon_hp_clamp(info
, true);
759 ret
= regmap_update_bits(arizona
->regmap
,
760 ARIZONA_ACCESSORY_DETECT_MODE_1
,
761 ARIZONA_ACCDET_SRC
| ARIZONA_ACCDET_MODE_MASK
,
762 info
->micd_modes
[0].src
|
763 arizona
->pdata
.hpdet_channel
);
765 dev_err(arizona
->dev
, "Failed to set HPDET mode: %d\n", ret
);
769 if (arizona
->pdata
.hpdet_acc_id_line
) {
770 ret
= regmap_update_bits(arizona
->regmap
,
771 ARIZONA_HEADPHONE_DETECT_1
,
772 ARIZONA_HP_POLL
, ARIZONA_HP_POLL
);
774 dev_err(arizona
->dev
,
775 "Can't start HPDETL measurement: %d\n",
780 arizona_hpdet_do_id(info
, &hp_reading
, &mic
);
786 regmap_update_bits(arizona
->regmap
, ARIZONA_ACCESSORY_DETECT_MODE_1
,
787 ARIZONA_ACCDET_MODE_MASK
, ARIZONA_ACCDET_MODE_MIC
);
789 /* Just report headphone */
790 ret
= extcon_set_state_sync(info
->edev
, EXTCON_JACK_HEADPHONE
, true);
792 dev_err(arizona
->dev
, "Failed to report headphone: %d\n", ret
);
794 info
->hpdet_active
= false;
797 static void arizona_micd_timeout_work(struct work_struct
*work
)
799 struct arizona_extcon_info
*info
= container_of(work
,
800 struct arizona_extcon_info
,
801 micd_timeout_work
.work
);
803 mutex_lock(&info
->lock
);
805 dev_dbg(info
->arizona
->dev
, "MICD timed out, reporting HP\n");
807 info
->detecting
= false;
809 arizona_identify_headphone(info
);
811 arizona_stop_mic(info
);
813 mutex_unlock(&info
->lock
);
816 static void arizona_micd_detect(struct work_struct
*work
)
818 struct arizona_extcon_info
*info
= container_of(work
,
819 struct arizona_extcon_info
,
820 micd_detect_work
.work
);
821 struct arizona
*arizona
= info
->arizona
;
822 unsigned int val
= 0, lvl
;
825 cancel_delayed_work_sync(&info
->micd_timeout_work
);
827 mutex_lock(&info
->lock
);
829 /* If the cable was removed while measuring ignore the result */
830 ret
= extcon_get_state(info
->edev
, EXTCON_MECHANICAL
);
832 dev_err(arizona
->dev
, "Failed to check cable state: %d\n",
834 mutex_unlock(&info
->lock
);
837 dev_dbg(arizona
->dev
, "Ignoring MICDET for removed cable\n");
838 mutex_unlock(&info
->lock
);
842 if (info
->detecting
&& arizona
->pdata
.micd_software_compare
) {
843 /* Must disable MICD before we read the ADCVAL */
844 regmap_update_bits(arizona
->regmap
, ARIZONA_MIC_DETECT_1
,
845 ARIZONA_MICD_ENA
, 0);
846 ret
= regmap_read(arizona
->regmap
, ARIZONA_MIC_DETECT_4
, &val
);
848 dev_err(arizona
->dev
,
849 "Failed to read MICDET_ADCVAL: %d\n",
851 mutex_unlock(&info
->lock
);
855 dev_dbg(arizona
->dev
, "MICDET_ADCVAL: %x\n", val
);
857 val
&= ARIZONA_MICDET_ADCVAL_MASK
;
858 if (val
< ARRAY_SIZE(arizona_micd_levels
))
859 val
= arizona_micd_levels
[val
];
863 if (val
<= QUICK_HEADPHONE_MAX_OHM
)
864 val
= ARIZONA_MICD_STS
| ARIZONA_MICD_LVL_0
;
865 else if (val
<= MICROPHONE_MIN_OHM
)
866 val
= ARIZONA_MICD_STS
| ARIZONA_MICD_LVL_1
;
867 else if (val
<= MICROPHONE_MAX_OHM
)
868 val
= ARIZONA_MICD_STS
| ARIZONA_MICD_LVL_8
;
870 val
= ARIZONA_MICD_LVL_8
;
873 for (i
= 0; i
< 10 && !(val
& MICD_LVL_0_TO_8
); i
++) {
874 ret
= regmap_read(arizona
->regmap
, ARIZONA_MIC_DETECT_3
, &val
);
876 dev_err(arizona
->dev
,
877 "Failed to read MICDET: %d\n", ret
);
878 mutex_unlock(&info
->lock
);
882 dev_dbg(arizona
->dev
, "MICDET: %x\n", val
);
884 if (!(val
& ARIZONA_MICD_VALID
)) {
885 dev_warn(arizona
->dev
,
886 "Microphone detection state invalid\n");
887 mutex_unlock(&info
->lock
);
892 if (i
== 10 && !(val
& MICD_LVL_0_TO_8
)) {
893 dev_err(arizona
->dev
, "Failed to get valid MICDET value\n");
894 mutex_unlock(&info
->lock
);
898 /* Due to jack detect this should never happen */
899 if (!(val
& ARIZONA_MICD_STS
)) {
900 dev_warn(arizona
->dev
, "Detected open circuit\n");
902 arizona_stop_mic(info
);
903 info
->detecting
= false;
904 arizona_identify_headphone(info
);
908 /* If we got a high impedence we should have a headset, report it. */
909 if (info
->detecting
&& (val
& ARIZONA_MICD_LVL_8
)) {
911 info
->detecting
= false;
913 arizona_identify_headphone(info
);
915 ret
= extcon_set_state_sync(info
->edev
,
916 EXTCON_JACK_MICROPHONE
, true);
918 dev_err(arizona
->dev
, "Headset report failed: %d\n",
921 /* Don't need to regulate for button detection */
922 ret
= regulator_allow_bypass(info
->micvdd
, true);
924 dev_err(arizona
->dev
, "Failed to bypass MICVDD: %d\n",
931 /* If we detected a lower impedence during initial startup
932 * then we probably have the wrong polarity, flip it. Don't
933 * do this for the lowest impedences to speed up detection of
934 * plain headphones. If both polarities report a low
935 * impedence then give up and report headphones.
937 if (info
->detecting
&& (val
& MICD_LVL_1_TO_7
)) {
938 if (info
->jack_flips
>= info
->micd_num_modes
* 10) {
939 dev_dbg(arizona
->dev
, "Detected HP/line\n");
941 info
->detecting
= false;
943 arizona_identify_headphone(info
);
945 arizona_stop_mic(info
);
948 if (info
->micd_mode
== info
->micd_num_modes
)
950 arizona_extcon_set_mode(info
, info
->micd_mode
);
959 * If we're still detecting and we detect a short then we've
960 * got a headphone. Otherwise it's a button press.
962 if (val
& MICD_LVL_0_TO_7
) {
964 dev_dbg(arizona
->dev
, "Mic button detected\n");
966 lvl
= val
& ARIZONA_MICD_LVL_MASK
;
967 lvl
>>= ARIZONA_MICD_LVL_SHIFT
;
969 for (i
= 0; i
< info
->num_micd_ranges
; i
++)
970 input_report_key(info
->input
,
971 info
->micd_ranges
[i
].key
, 0);
974 WARN_ON(ffs(lvl
) - 1 >= info
->num_micd_ranges
);
975 if (lvl
&& ffs(lvl
) - 1 < info
->num_micd_ranges
) {
976 key
= info
->micd_ranges
[ffs(lvl
) - 1].key
;
977 input_report_key(info
->input
, key
, 1);
978 input_sync(info
->input
);
981 } else if (info
->detecting
) {
982 dev_dbg(arizona
->dev
, "Headphone detected\n");
983 info
->detecting
= false;
984 arizona_stop_mic(info
);
986 arizona_identify_headphone(info
);
988 dev_warn(arizona
->dev
, "Button with no mic: %x\n",
992 dev_dbg(arizona
->dev
, "Mic button released\n");
993 for (i
= 0; i
< info
->num_micd_ranges
; i
++)
994 input_report_key(info
->input
,
995 info
->micd_ranges
[i
].key
, 0);
996 input_sync(info
->input
);
997 arizona_extcon_pulse_micbias(info
);
1001 if (info
->detecting
) {
1002 if (arizona
->pdata
.micd_software_compare
)
1003 regmap_update_bits(arizona
->regmap
,
1004 ARIZONA_MIC_DETECT_1
,
1008 queue_delayed_work(system_power_efficient_wq
,
1009 &info
->micd_timeout_work
,
1010 msecs_to_jiffies(info
->micd_timeout
));
1013 pm_runtime_mark_last_busy(info
->dev
);
1014 mutex_unlock(&info
->lock
);
1017 static irqreturn_t
arizona_micdet(int irq
, void *data
)
1019 struct arizona_extcon_info
*info
= data
;
1020 struct arizona
*arizona
= info
->arizona
;
1021 int debounce
= arizona
->pdata
.micd_detect_debounce
;
1023 cancel_delayed_work_sync(&info
->micd_detect_work
);
1024 cancel_delayed_work_sync(&info
->micd_timeout_work
);
1026 mutex_lock(&info
->lock
);
1027 if (!info
->detecting
)
1029 mutex_unlock(&info
->lock
);
1032 queue_delayed_work(system_power_efficient_wq
,
1033 &info
->micd_detect_work
,
1034 msecs_to_jiffies(debounce
));
1036 arizona_micd_detect(&info
->micd_detect_work
.work
);
1041 static void arizona_hpdet_work(struct work_struct
*work
)
1043 struct arizona_extcon_info
*info
= container_of(work
,
1044 struct arizona_extcon_info
,
1047 mutex_lock(&info
->lock
);
1048 arizona_start_hpdet_acc_id(info
);
1049 mutex_unlock(&info
->lock
);
1052 static irqreturn_t
arizona_jackdet(int irq
, void *data
)
1054 struct arizona_extcon_info
*info
= data
;
1055 struct arizona
*arizona
= info
->arizona
;
1056 unsigned int val
, present
, mask
;
1057 bool cancelled_hp
, cancelled_mic
;
1060 cancelled_hp
= cancel_delayed_work_sync(&info
->hpdet_work
);
1061 cancelled_mic
= cancel_delayed_work_sync(&info
->micd_timeout_work
);
1063 pm_runtime_get_sync(info
->dev
);
1065 mutex_lock(&info
->lock
);
1067 if (info
->micd_clamp
) {
1068 mask
= ARIZONA_MICD_CLAMP_STS
;
1071 mask
= ARIZONA_JD1_STS
;
1072 if (arizona
->pdata
.jd_invert
)
1075 present
= ARIZONA_JD1_STS
;
1078 ret
= regmap_read(arizona
->regmap
, ARIZONA_AOD_IRQ_RAW_STATUS
, &val
);
1080 dev_err(arizona
->dev
, "Failed to read jackdet status: %d\n",
1082 mutex_unlock(&info
->lock
);
1083 pm_runtime_put_autosuspend(info
->dev
);
1088 if (val
== info
->last_jackdet
) {
1089 dev_dbg(arizona
->dev
, "Suppressing duplicate JACKDET\n");
1091 queue_delayed_work(system_power_efficient_wq
,
1093 msecs_to_jiffies(HPDET_DEBOUNCE
));
1095 if (cancelled_mic
) {
1096 int micd_timeout
= info
->micd_timeout
;
1098 queue_delayed_work(system_power_efficient_wq
,
1099 &info
->micd_timeout_work
,
1100 msecs_to_jiffies(micd_timeout
));
1105 info
->last_jackdet
= val
;
1107 if (info
->last_jackdet
== present
) {
1108 dev_dbg(arizona
->dev
, "Detected jack\n");
1109 ret
= extcon_set_state_sync(info
->edev
,
1110 EXTCON_MECHANICAL
, true);
1113 dev_err(arizona
->dev
, "Mechanical report failed: %d\n",
1116 if (!arizona
->pdata
.hpdet_acc_id
) {
1117 info
->detecting
= true;
1119 info
->jack_flips
= 0;
1121 arizona_start_mic(info
);
1123 queue_delayed_work(system_power_efficient_wq
,
1125 msecs_to_jiffies(HPDET_DEBOUNCE
));
1128 if (info
->micd_clamp
|| !arizona
->pdata
.jd_invert
)
1129 regmap_update_bits(arizona
->regmap
,
1130 ARIZONA_JACK_DETECT_DEBOUNCE
,
1131 ARIZONA_MICD_CLAMP_DB
|
1134 dev_dbg(arizona
->dev
, "Detected jack removal\n");
1136 arizona_stop_mic(info
);
1138 info
->num_hpdet_res
= 0;
1139 for (i
= 0; i
< ARRAY_SIZE(info
->hpdet_res
); i
++)
1140 info
->hpdet_res
[i
] = 0;
1142 info
->hpdet_done
= false;
1143 info
->hpdet_retried
= false;
1145 for (i
= 0; i
< info
->num_micd_ranges
; i
++)
1146 input_report_key(info
->input
,
1147 info
->micd_ranges
[i
].key
, 0);
1148 input_sync(info
->input
);
1150 for (i
= 0; i
< ARRAY_SIZE(arizona_cable
) - 1; i
++) {
1151 ret
= extcon_set_state_sync(info
->edev
,
1152 arizona_cable
[i
], false);
1154 dev_err(arizona
->dev
,
1155 "Removal report failed: %d\n", ret
);
1158 regmap_update_bits(arizona
->regmap
,
1159 ARIZONA_JACK_DETECT_DEBOUNCE
,
1160 ARIZONA_MICD_CLAMP_DB
| ARIZONA_JD1_DB
,
1161 ARIZONA_MICD_CLAMP_DB
| ARIZONA_JD1_DB
);
1164 if (arizona
->pdata
.micd_timeout
)
1165 info
->micd_timeout
= arizona
->pdata
.micd_timeout
;
1167 info
->micd_timeout
= DEFAULT_MICD_TIMEOUT
;
1170 /* Clear trig_sts to make sure DCVDD is not forced up */
1171 regmap_write(arizona
->regmap
, ARIZONA_AOD_WKUP_AND_TRIG
,
1172 ARIZONA_MICD_CLAMP_FALL_TRIG_STS
|
1173 ARIZONA_MICD_CLAMP_RISE_TRIG_STS
|
1174 ARIZONA_JD1_FALL_TRIG_STS
|
1175 ARIZONA_JD1_RISE_TRIG_STS
);
1177 mutex_unlock(&info
->lock
);
1179 pm_runtime_mark_last_busy(info
->dev
);
1180 pm_runtime_put_autosuspend(info
->dev
);
1185 /* Map a level onto a slot in the register bank */
1186 static void arizona_micd_set_level(struct arizona
*arizona
, int index
,
1192 reg
= ARIZONA_MIC_DETECT_LEVEL_4
- (index
/ 2);
1201 /* Program the level itself */
1202 regmap_update_bits(arizona
->regmap
, reg
, mask
, level
);
1205 static int arizona_extcon_get_micd_configs(struct device
*dev
,
1206 struct arizona
*arizona
)
1208 const char * const prop
= "wlf,micd-configs";
1209 const int entries_per_config
= 3;
1210 struct arizona_micd_config
*micd_configs
;
1215 nconfs
= device_property_read_u32_array(arizona
->dev
, prop
, NULL
, 0);
1219 vals
= kcalloc(nconfs
, sizeof(u32
), GFP_KERNEL
);
1223 ret
= device_property_read_u32_array(arizona
->dev
, prop
, vals
, nconfs
);
1227 nconfs
/= entries_per_config
;
1229 micd_configs
= devm_kzalloc(dev
,
1230 nconfs
* sizeof(struct arizona_micd_range
),
1232 if (!micd_configs
) {
1237 for (i
= 0, j
= 0; i
< nconfs
; ++i
) {
1238 micd_configs
[i
].src
= vals
[j
++] ? ARIZONA_ACCDET_SRC
: 0;
1239 micd_configs
[i
].bias
= vals
[j
++];
1240 micd_configs
[i
].gpio
= vals
[j
++];
1243 arizona
->pdata
.micd_configs
= micd_configs
;
1244 arizona
->pdata
.num_micd_configs
= nconfs
;
1251 static int arizona_extcon_device_get_pdata(struct device
*dev
,
1252 struct arizona
*arizona
)
1254 struct arizona_pdata
*pdata
= &arizona
->pdata
;
1255 unsigned int val
= ARIZONA_ACCDET_MODE_HPL
;
1258 device_property_read_u32(arizona
->dev
, "wlf,hpdet-channel", &val
);
1260 case ARIZONA_ACCDET_MODE_HPL
:
1261 case ARIZONA_ACCDET_MODE_HPR
:
1262 pdata
->hpdet_channel
= val
;
1265 dev_err(arizona
->dev
,
1266 "Wrong wlf,hpdet-channel DT value %d\n", val
);
1267 pdata
->hpdet_channel
= ARIZONA_ACCDET_MODE_HPL
;
1270 device_property_read_u32(arizona
->dev
, "wlf,micd-detect-debounce",
1271 &pdata
->micd_detect_debounce
);
1273 device_property_read_u32(arizona
->dev
, "wlf,micd-bias-start-time",
1274 &pdata
->micd_bias_start_time
);
1276 device_property_read_u32(arizona
->dev
, "wlf,micd-rate",
1279 device_property_read_u32(arizona
->dev
, "wlf,micd-dbtime",
1280 &pdata
->micd_dbtime
);
1282 device_property_read_u32(arizona
->dev
, "wlf,micd-timeout-ms",
1283 &pdata
->micd_timeout
);
1285 pdata
->micd_force_micbias
= device_property_read_bool(arizona
->dev
,
1286 "wlf,micd-force-micbias");
1288 pdata
->micd_software_compare
= device_property_read_bool(arizona
->dev
,
1289 "wlf,micd-software-compare");
1291 pdata
->jd_invert
= device_property_read_bool(arizona
->dev
,
1294 device_property_read_u32(arizona
->dev
, "wlf,gpsw", &pdata
->gpsw
);
1296 pdata
->jd_gpio5
= device_property_read_bool(arizona
->dev
,
1298 pdata
->jd_gpio5_nopull
= device_property_read_bool(arizona
->dev
,
1299 "wlf,use-jd2-nopull");
1301 ret
= arizona_extcon_get_micd_configs(dev
, arizona
);
1303 dev_err(arizona
->dev
, "Failed to read micd configs: %d\n", ret
);
1308 static int arizona_extcon_probe(struct platform_device
*pdev
)
1310 struct arizona
*arizona
= dev_get_drvdata(pdev
->dev
.parent
);
1311 struct arizona_pdata
*pdata
= &arizona
->pdata
;
1312 struct arizona_extcon_info
*info
;
1314 unsigned int clamp_mode
;
1315 int jack_irq_fall
, jack_irq_rise
;
1316 int ret
, mode
, i
, j
;
1318 if (!arizona
->dapm
|| !arizona
->dapm
->card
)
1319 return -EPROBE_DEFER
;
1321 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
1325 if (!dev_get_platdata(arizona
->dev
))
1326 arizona_extcon_device_get_pdata(&pdev
->dev
, arizona
);
1328 info
->micvdd
= devm_regulator_get(&pdev
->dev
, "MICVDD");
1329 if (IS_ERR(info
->micvdd
)) {
1330 ret
= PTR_ERR(info
->micvdd
);
1331 dev_err(arizona
->dev
, "Failed to get MICVDD: %d\n", ret
);
1335 mutex_init(&info
->lock
);
1336 info
->arizona
= arizona
;
1337 info
->dev
= &pdev
->dev
;
1338 info
->last_jackdet
= ~(ARIZONA_MICD_CLAMP_STS
| ARIZONA_JD1_STS
);
1339 INIT_DELAYED_WORK(&info
->hpdet_work
, arizona_hpdet_work
);
1340 INIT_DELAYED_WORK(&info
->micd_detect_work
, arizona_micd_detect
);
1341 INIT_DELAYED_WORK(&info
->micd_timeout_work
, arizona_micd_timeout_work
);
1342 platform_set_drvdata(pdev
, info
);
1344 switch (arizona
->type
) {
1346 switch (arizona
->rev
) {
1348 info
->micd_reva
= true;
1351 info
->micd_clamp
= true;
1352 info
->hpdet_ip_version
= 1;
1358 switch (arizona
->rev
) {
1362 info
->micd_clamp
= true;
1363 info
->hpdet_ip_version
= 2;
1369 info
->micd_clamp
= true;
1370 info
->hpdet_ip_version
= 2;
1376 info
->edev
= devm_extcon_dev_allocate(&pdev
->dev
, arizona_cable
);
1377 if (IS_ERR(info
->edev
)) {
1378 dev_err(&pdev
->dev
, "failed to allocate extcon device\n");
1382 ret
= devm_extcon_dev_register(&pdev
->dev
, info
->edev
);
1384 dev_err(arizona
->dev
, "extcon_dev_register() failed: %d\n",
1389 info
->input
= devm_input_allocate_device(&pdev
->dev
);
1391 dev_err(arizona
->dev
, "Can't allocate input dev\n");
1396 info
->input
->name
= "Headset";
1397 info
->input
->phys
= "arizona/extcon";
1399 if (pdata
->num_micd_configs
) {
1400 info
->micd_modes
= pdata
->micd_configs
;
1401 info
->micd_num_modes
= pdata
->num_micd_configs
;
1403 info
->micd_modes
= micd_default_modes
;
1404 info
->micd_num_modes
= ARRAY_SIZE(micd_default_modes
);
1407 if (arizona
->pdata
.gpsw
> 0)
1408 regmap_update_bits(arizona
->regmap
, ARIZONA_GP_SWITCH_1
,
1409 ARIZONA_SW1_MODE_MASK
, arizona
->pdata
.gpsw
);
1411 if (pdata
->micd_pol_gpio
> 0) {
1412 if (info
->micd_modes
[0].gpio
)
1413 mode
= GPIOF_OUT_INIT_HIGH
;
1415 mode
= GPIOF_OUT_INIT_LOW
;
1417 ret
= devm_gpio_request_one(&pdev
->dev
, pdata
->micd_pol_gpio
,
1418 mode
, "MICD polarity");
1420 dev_err(arizona
->dev
, "Failed to request GPIO%d: %d\n",
1421 pdata
->micd_pol_gpio
, ret
);
1425 info
->micd_pol_gpio
= gpio_to_desc(pdata
->micd_pol_gpio
);
1427 if (info
->micd_modes
[0].gpio
)
1428 mode
= GPIOD_OUT_HIGH
;
1430 mode
= GPIOD_OUT_LOW
;
1432 /* We can't use devm here because we need to do the get
1433 * against the MFD device, as that is where the of_node
1434 * will reside, but if we devm against that the GPIO
1435 * will not be freed if the extcon driver is unloaded.
1437 info
->micd_pol_gpio
= gpiod_get_optional(arizona
->dev
,
1440 if (IS_ERR(info
->micd_pol_gpio
)) {
1441 ret
= PTR_ERR(info
->micd_pol_gpio
);
1442 dev_err(arizona
->dev
,
1443 "Failed to get microphone polarity GPIO: %d\n",
1449 if (arizona
->pdata
.hpdet_id_gpio
> 0) {
1450 ret
= devm_gpio_request_one(&pdev
->dev
,
1451 arizona
->pdata
.hpdet_id_gpio
,
1455 dev_err(arizona
->dev
, "Failed to request GPIO%d: %d\n",
1456 arizona
->pdata
.hpdet_id_gpio
, ret
);
1461 if (arizona
->pdata
.micd_bias_start_time
)
1462 regmap_update_bits(arizona
->regmap
, ARIZONA_MIC_DETECT_1
,
1463 ARIZONA_MICD_BIAS_STARTTIME_MASK
,
1464 arizona
->pdata
.micd_bias_start_time
1465 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT
);
1467 if (arizona
->pdata
.micd_rate
)
1468 regmap_update_bits(arizona
->regmap
, ARIZONA_MIC_DETECT_1
,
1469 ARIZONA_MICD_RATE_MASK
,
1470 arizona
->pdata
.micd_rate
1471 << ARIZONA_MICD_RATE_SHIFT
);
1473 switch (arizona
->pdata
.micd_dbtime
) {
1474 case MICD_DBTIME_FOUR_READINGS
:
1475 regmap_update_bits(arizona
->regmap
, ARIZONA_MIC_DETECT_1
,
1476 ARIZONA_MICD_DBTIME_MASK
,
1477 ARIZONA_MICD_DBTIME
);
1479 case MICD_DBTIME_TWO_READINGS
:
1480 regmap_update_bits(arizona
->regmap
, ARIZONA_MIC_DETECT_1
,
1481 ARIZONA_MICD_DBTIME_MASK
, 0);
1487 BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels
) <
1488 ARIZONA_NUM_MICD_BUTTON_LEVELS
);
1490 if (arizona
->pdata
.num_micd_ranges
) {
1491 info
->micd_ranges
= pdata
->micd_ranges
;
1492 info
->num_micd_ranges
= pdata
->num_micd_ranges
;
1494 info
->micd_ranges
= micd_default_ranges
;
1495 info
->num_micd_ranges
= ARRAY_SIZE(micd_default_ranges
);
1498 if (arizona
->pdata
.num_micd_ranges
> ARIZONA_MAX_MICD_RANGE
) {
1499 dev_err(arizona
->dev
, "Too many MICD ranges: %d\n",
1500 arizona
->pdata
.num_micd_ranges
);
1503 if (info
->num_micd_ranges
> 1) {
1504 for (i
= 1; i
< info
->num_micd_ranges
; i
++) {
1505 if (info
->micd_ranges
[i
- 1].max
>
1506 info
->micd_ranges
[i
].max
) {
1507 dev_err(arizona
->dev
,
1508 "MICD ranges must be sorted\n");
1515 /* Disable all buttons by default */
1516 regmap_update_bits(arizona
->regmap
, ARIZONA_MIC_DETECT_2
,
1517 ARIZONA_MICD_LVL_SEL_MASK
, 0x81);
1519 /* Set up all the buttons the user specified */
1520 for (i
= 0; i
< info
->num_micd_ranges
; i
++) {
1521 for (j
= 0; j
< ARIZONA_NUM_MICD_BUTTON_LEVELS
; j
++)
1522 if (arizona_micd_levels
[j
] >= info
->micd_ranges
[i
].max
)
1525 if (j
== ARIZONA_NUM_MICD_BUTTON_LEVELS
) {
1526 dev_err(arizona
->dev
, "Unsupported MICD level %d\n",
1527 info
->micd_ranges
[i
].max
);
1532 dev_dbg(arizona
->dev
, "%d ohms for MICD threshold %d\n",
1533 arizona_micd_levels
[j
], i
);
1535 arizona_micd_set_level(arizona
, i
, j
);
1536 input_set_capability(info
->input
, EV_KEY
,
1537 info
->micd_ranges
[i
].key
);
1539 /* Enable reporting of that range */
1540 regmap_update_bits(arizona
->regmap
, ARIZONA_MIC_DETECT_2
,
1544 /* Set all the remaining keys to a maximum */
1545 for (; i
< ARIZONA_MAX_MICD_RANGE
; i
++)
1546 arizona_micd_set_level(arizona
, i
, 0x3f);
1549 * If we have a clamp use it, activating in conjunction with
1550 * GPIO5 if that is connected for jack detect operation.
1552 if (info
->micd_clamp
) {
1553 if (arizona
->pdata
.jd_gpio5
) {
1554 /* Put the GPIO into input mode with optional pull */
1556 if (arizona
->pdata
.jd_gpio5_nopull
)
1557 val
&= ~ARIZONA_GPN_PU
;
1559 regmap_write(arizona
->regmap
, ARIZONA_GPIO5_CTRL
,
1562 if (arizona
->pdata
.jd_invert
)
1563 clamp_mode
= ARIZONA_MICD_CLAMP_MODE_JDH_GP5H
;
1565 clamp_mode
= ARIZONA_MICD_CLAMP_MODE_JDL_GP5H
;
1567 if (arizona
->pdata
.jd_invert
)
1568 clamp_mode
= ARIZONA_MICD_CLAMP_MODE_JDH
;
1570 clamp_mode
= ARIZONA_MICD_CLAMP_MODE_JDL
;
1573 regmap_update_bits(arizona
->regmap
,
1574 ARIZONA_MICD_CLAMP_CONTROL
,
1575 ARIZONA_MICD_CLAMP_MODE_MASK
, clamp_mode
);
1577 regmap_update_bits(arizona
->regmap
,
1578 ARIZONA_JACK_DETECT_DEBOUNCE
,
1579 ARIZONA_MICD_CLAMP_DB
,
1580 ARIZONA_MICD_CLAMP_DB
);
1583 arizona_extcon_set_mode(info
, 0);
1585 pm_runtime_enable(&pdev
->dev
);
1586 pm_runtime_idle(&pdev
->dev
);
1587 pm_runtime_get_sync(&pdev
->dev
);
1589 if (info
->micd_clamp
) {
1590 jack_irq_rise
= ARIZONA_IRQ_MICD_CLAMP_RISE
;
1591 jack_irq_fall
= ARIZONA_IRQ_MICD_CLAMP_FALL
;
1593 jack_irq_rise
= ARIZONA_IRQ_JD_RISE
;
1594 jack_irq_fall
= ARIZONA_IRQ_JD_FALL
;
1597 ret
= arizona_request_irq(arizona
, jack_irq_rise
,
1598 "JACKDET rise", arizona_jackdet
, info
);
1600 dev_err(&pdev
->dev
, "Failed to get JACKDET rise IRQ: %d\n",
1605 ret
= arizona_set_irq_wake(arizona
, jack_irq_rise
, 1);
1607 dev_err(&pdev
->dev
, "Failed to set JD rise IRQ wake: %d\n",
1612 ret
= arizona_request_irq(arizona
, jack_irq_fall
,
1613 "JACKDET fall", arizona_jackdet
, info
);
1615 dev_err(&pdev
->dev
, "Failed to get JD fall IRQ: %d\n", ret
);
1619 ret
= arizona_set_irq_wake(arizona
, jack_irq_fall
, 1);
1621 dev_err(&pdev
->dev
, "Failed to set JD fall IRQ wake: %d\n",
1626 ret
= arizona_request_irq(arizona
, ARIZONA_IRQ_MICDET
,
1627 "MICDET", arizona_micdet
, info
);
1629 dev_err(&pdev
->dev
, "Failed to get MICDET IRQ: %d\n", ret
);
1633 ret
= arizona_request_irq(arizona
, ARIZONA_IRQ_HPDET
,
1634 "HPDET", arizona_hpdet_irq
, info
);
1636 dev_err(&pdev
->dev
, "Failed to get HPDET IRQ: %d\n", ret
);
1640 arizona_clk32k_enable(arizona
);
1641 regmap_update_bits(arizona
->regmap
, ARIZONA_JACK_DETECT_DEBOUNCE
,
1642 ARIZONA_JD1_DB
, ARIZONA_JD1_DB
);
1643 regmap_update_bits(arizona
->regmap
, ARIZONA_JACK_DETECT_ANALOGUE
,
1644 ARIZONA_JD1_ENA
, ARIZONA_JD1_ENA
);
1646 ret
= regulator_allow_bypass(info
->micvdd
, true);
1648 dev_warn(arizona
->dev
, "Failed to set MICVDD to bypass: %d\n",
1651 pm_runtime_put(&pdev
->dev
);
1653 ret
= input_register_device(info
->input
);
1655 dev_err(&pdev
->dev
, "Can't register input device: %d\n", ret
);
1662 arizona_free_irq(arizona
, ARIZONA_IRQ_HPDET
, info
);
1664 arizona_free_irq(arizona
, ARIZONA_IRQ_MICDET
, info
);
1666 arizona_set_irq_wake(arizona
, jack_irq_fall
, 0);
1668 arizona_free_irq(arizona
, jack_irq_fall
, info
);
1670 arizona_set_irq_wake(arizona
, jack_irq_rise
, 0);
1672 arizona_free_irq(arizona
, jack_irq_rise
, info
);
1674 gpiod_put(info
->micd_pol_gpio
);
1676 pm_runtime_disable(&pdev
->dev
);
1680 static int arizona_extcon_remove(struct platform_device
*pdev
)
1682 struct arizona_extcon_info
*info
= platform_get_drvdata(pdev
);
1683 struct arizona
*arizona
= info
->arizona
;
1684 int jack_irq_rise
, jack_irq_fall
;
1686 gpiod_put(info
->micd_pol_gpio
);
1688 pm_runtime_disable(&pdev
->dev
);
1690 regmap_update_bits(arizona
->regmap
,
1691 ARIZONA_MICD_CLAMP_CONTROL
,
1692 ARIZONA_MICD_CLAMP_MODE_MASK
, 0);
1694 if (info
->micd_clamp
) {
1695 jack_irq_rise
= ARIZONA_IRQ_MICD_CLAMP_RISE
;
1696 jack_irq_fall
= ARIZONA_IRQ_MICD_CLAMP_FALL
;
1698 jack_irq_rise
= ARIZONA_IRQ_JD_RISE
;
1699 jack_irq_fall
= ARIZONA_IRQ_JD_FALL
;
1702 arizona_set_irq_wake(arizona
, jack_irq_rise
, 0);
1703 arizona_set_irq_wake(arizona
, jack_irq_fall
, 0);
1704 arizona_free_irq(arizona
, ARIZONA_IRQ_HPDET
, info
);
1705 arizona_free_irq(arizona
, ARIZONA_IRQ_MICDET
, info
);
1706 arizona_free_irq(arizona
, jack_irq_rise
, info
);
1707 arizona_free_irq(arizona
, jack_irq_fall
, info
);
1708 cancel_delayed_work_sync(&info
->hpdet_work
);
1709 regmap_update_bits(arizona
->regmap
, ARIZONA_JACK_DETECT_ANALOGUE
,
1710 ARIZONA_JD1_ENA
, 0);
1711 arizona_clk32k_disable(arizona
);
1716 static struct platform_driver arizona_extcon_driver
= {
1718 .name
= "arizona-extcon",
1720 .probe
= arizona_extcon_probe
,
1721 .remove
= arizona_extcon_remove
,
1724 module_platform_driver(arizona_extcon_driver
);
1726 MODULE_DESCRIPTION("Arizona Extcon driver");
1727 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1728 MODULE_LICENSE("GPL");
1729 MODULE_ALIAS("platform:extcon-arizona");