2 * extcon-arizona.c - Extcon driver Wolfson Arizona devices
4 * Copyright (C) 2012 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.h>
24 #include <linux/input.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/extcon.h>
30 #include <sound/soc.h>
32 #include <linux/mfd/arizona/core.h>
33 #include <linux/mfd/arizona/pdata.h>
34 #include <linux/mfd/arizona/registers.h>
36 #define ARIZONA_MAX_MICD_RANGE 8
38 #define ARIZONA_ACCDET_MODE_MIC 0
39 #define ARIZONA_ACCDET_MODE_HPL 1
40 #define ARIZONA_ACCDET_MODE_HPR 2
42 #define ARIZONA_MICD_CLAMP_MODE_JDL 0x4
43 #define ARIZONA_MICD_CLAMP_MODE_JDH 0x5
44 #define ARIZONA_MICD_CLAMP_MODE_JDL_GP5H 0x9
45 #define ARIZONA_MICD_CLAMP_MODE_JDH_GP5H 0xb
47 #define ARIZONA_HPDET_MAX 10000
49 #define HPDET_DEBOUNCE 500
50 #define DEFAULT_MICD_TIMEOUT 2000
52 #define MICD_LVL_1_TO_7 (ARIZONA_MICD_LVL_1 | ARIZONA_MICD_LVL_2 | \
53 ARIZONA_MICD_LVL_3 | ARIZONA_MICD_LVL_4 | \
54 ARIZONA_MICD_LVL_5 | ARIZONA_MICD_LVL_6 | \
57 #define MICD_LVL_0_TO_7 (ARIZONA_MICD_LVL_0 | MICD_LVL_1_TO_7)
59 #define MICD_LVL_0_TO_8 (MICD_LVL_0_TO_7 | ARIZONA_MICD_LVL_8)
61 struct arizona_extcon_info {
63 struct arizona *arizona;
65 struct regulator *micvdd;
66 struct input_dev *input;
71 const struct arizona_micd_config *micd_modes;
74 const struct arizona_micd_range *micd_ranges;
82 struct delayed_work hpdet_work;
83 struct delayed_work micd_detect_work;
84 struct delayed_work micd_timeout_work;
91 unsigned int hpdet_res[3];
99 struct extcon_dev *edev;
102 static const struct arizona_micd_config micd_default_modes[] = {
103 { ARIZONA_ACCDET_SRC, 1, 0 },
107 static const struct arizona_micd_range micd_default_ranges[] = {
108 { .max = 11, .key = BTN_0 },
109 { .max = 28, .key = BTN_1 },
110 { .max = 54, .key = BTN_2 },
111 { .max = 100, .key = BTN_3 },
112 { .max = 186, .key = BTN_4 },
113 { .max = 430, .key = BTN_5 },
116 static const int arizona_micd_levels[] = {
117 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
118 49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
119 105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
120 270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
124 #define ARIZONA_CABLE_MECHANICAL 0
125 #define ARIZONA_CABLE_MICROPHONE 1
126 #define ARIZONA_CABLE_HEADPHONE 2
127 #define ARIZONA_CABLE_LINEOUT 3
129 static const char *arizona_cable[] = {
137 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info);
139 static void arizona_extcon_do_magic(struct arizona_extcon_info *info,
142 struct arizona *arizona = info->arizona;
145 mutex_lock(&arizona->dapm->card->dapm_mutex);
147 arizona->hpdet_magic = magic;
149 /* Keep the HP output stages disabled while doing the magic */
151 ret = regmap_update_bits(arizona->regmap,
152 ARIZONA_OUTPUT_ENABLES_1,
154 ARIZONA_OUT1R_ENA, 0);
156 dev_warn(arizona->dev,
157 "Failed to disable headphone outputs: %d\n",
161 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000,
164 dev_warn(arizona->dev, "Failed to do magic: %d\n",
167 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000,
170 dev_warn(arizona->dev, "Failed to do magic: %d\n",
173 /* Restore the desired state while not doing the magic */
175 ret = regmap_update_bits(arizona->regmap,
176 ARIZONA_OUTPUT_ENABLES_1,
178 ARIZONA_OUT1R_ENA, arizona->hp_ena);
180 dev_warn(arizona->dev,
181 "Failed to restore headphone outputs: %d\n",
185 mutex_unlock(&arizona->dapm->card->dapm_mutex);
188 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
190 struct arizona *arizona = info->arizona;
192 mode %= info->micd_num_modes;
194 if (arizona->pdata.micd_pol_gpio > 0)
195 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
196 info->micd_modes[mode].gpio);
197 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
198 ARIZONA_MICD_BIAS_SRC_MASK,
199 info->micd_modes[mode].bias <<
200 ARIZONA_MICD_BIAS_SRC_SHIFT);
201 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
202 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
204 info->micd_mode = mode;
206 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
209 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
211 switch (info->micd_modes[0].bias) {
223 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
225 struct arizona *arizona = info->arizona;
226 const char *widget = arizona_extcon_get_micbias(info);
227 struct snd_soc_dapm_context *dapm = arizona->dapm;
230 ret = snd_soc_dapm_force_enable_pin(dapm, widget);
232 dev_warn(arizona->dev, "Failed to enable %s: %d\n",
235 snd_soc_dapm_sync(dapm);
237 if (!arizona->pdata.micd_force_micbias) {
238 ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
240 dev_warn(arizona->dev, "Failed to disable %s: %d\n",
243 snd_soc_dapm_sync(dapm);
247 static void arizona_start_mic(struct arizona_extcon_info *info)
249 struct arizona *arizona = info->arizona;
253 /* Microphone detection can't use idle mode */
254 pm_runtime_get(info->dev);
256 if (info->detecting) {
257 ret = regulator_allow_bypass(info->micvdd, false);
259 dev_err(arizona->dev,
260 "Failed to regulate MICVDD: %d\n",
265 ret = regulator_enable(info->micvdd);
267 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
271 if (info->micd_reva) {
272 regmap_write(arizona->regmap, 0x80, 0x3);
273 regmap_write(arizona->regmap, 0x294, 0);
274 regmap_write(arizona->regmap, 0x80, 0x0);
277 regmap_update_bits(arizona->regmap,
278 ARIZONA_ACCESSORY_DETECT_MODE_1,
279 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
281 arizona_extcon_pulse_micbias(info);
283 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
284 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
287 regulator_disable(info->micvdd);
288 pm_runtime_put_autosuspend(info->dev);
292 static void arizona_stop_mic(struct arizona_extcon_info *info)
294 struct arizona *arizona = info->arizona;
295 const char *widget = arizona_extcon_get_micbias(info);
296 struct snd_soc_dapm_context *dapm = arizona->dapm;
300 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
304 ret = snd_soc_dapm_disable_pin(dapm, widget);
306 dev_warn(arizona->dev,
307 "Failed to disable %s: %d\n",
310 snd_soc_dapm_sync(dapm);
312 if (info->micd_reva) {
313 regmap_write(arizona->regmap, 0x80, 0x3);
314 regmap_write(arizona->regmap, 0x294, 2);
315 regmap_write(arizona->regmap, 0x80, 0x0);
318 ret = regulator_allow_bypass(info->micvdd, true);
320 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
325 regulator_disable(info->micvdd);
326 pm_runtime_mark_last_busy(info->dev);
327 pm_runtime_put_autosuspend(info->dev);
332 unsigned int factor_a;
333 unsigned int factor_b;
334 } arizona_hpdet_b_ranges[] = {
343 } arizona_hpdet_c_ranges[] = {
350 static int arizona_hpdet_read(struct arizona_extcon_info *info)
352 struct arizona *arizona = info->arizona;
353 unsigned int val, range;
356 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
358 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
363 switch (info->hpdet_ip) {
365 if (!(val & ARIZONA_HP_DONE)) {
366 dev_err(arizona->dev, "HPDET did not complete: %x\n",
371 val &= ARIZONA_HP_LVL_MASK;
375 if (!(val & ARIZONA_HP_DONE_B)) {
376 dev_err(arizona->dev, "HPDET did not complete: %x\n",
381 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
383 dev_err(arizona->dev, "Failed to read HP value: %d\n",
388 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
390 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
391 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
393 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
394 (val < 100 || val >= 0x3fb)) {
396 dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
398 regmap_update_bits(arizona->regmap,
399 ARIZONA_HEADPHONE_DETECT_1,
400 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
402 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
406 /* If we go out of range report top of range */
407 if (val < 100 || val >= 0x3fb) {
408 dev_dbg(arizona->dev, "Measurement out of range\n");
409 return ARIZONA_HPDET_MAX;
412 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
415 val = arizona_hpdet_b_ranges[range].factor_b
417 arizona_hpdet_b_ranges[range].factor_a);
421 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
424 if (!(val & ARIZONA_HP_DONE_B)) {
425 dev_err(arizona->dev, "HPDET did not complete: %x\n",
430 val &= ARIZONA_HP_LVL_B_MASK;
431 /* Convert to ohms, the value is in 0.5 ohm increments */
434 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
436 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
437 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
439 /* Skip up a range, or report? */
440 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
441 (val >= arizona_hpdet_c_ranges[range].max)) {
443 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
444 arizona_hpdet_c_ranges[range].min,
445 arizona_hpdet_c_ranges[range].max);
446 regmap_update_bits(arizona->regmap,
447 ARIZONA_HEADPHONE_DETECT_1,
448 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
450 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
454 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
455 dev_dbg(arizona->dev, "Reporting range boundary %d\n",
456 arizona_hpdet_c_ranges[range].min);
457 val = arizona_hpdet_c_ranges[range].min;
461 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
465 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading,
468 struct arizona *arizona = info->arizona;
469 int id_gpio = arizona->pdata.hpdet_id_gpio;
472 * If we're using HPDET for accessory identification we need
473 * to take multiple measurements, step through them in sequence.
475 if (arizona->pdata.hpdet_acc_id) {
476 info->hpdet_res[info->num_hpdet_res++] = *reading;
478 /* Only check the mic directly if we didn't already ID it */
479 if (id_gpio && info->num_hpdet_res == 1) {
480 dev_dbg(arizona->dev, "Measuring mic\n");
482 regmap_update_bits(arizona->regmap,
483 ARIZONA_ACCESSORY_DETECT_MODE_1,
484 ARIZONA_ACCDET_MODE_MASK |
486 ARIZONA_ACCDET_MODE_HPR |
487 info->micd_modes[0].src);
489 gpio_set_value_cansleep(id_gpio, 1);
491 regmap_update_bits(arizona->regmap,
492 ARIZONA_HEADPHONE_DETECT_1,
493 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
497 /* OK, got both. Now, compare... */
498 dev_dbg(arizona->dev, "HPDET measured %d %d\n",
499 info->hpdet_res[0], info->hpdet_res[1]);
501 /* Take the headphone impedance for the main report */
502 *reading = info->hpdet_res[0];
504 /* Sometimes we get false readings due to slow insert */
505 if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
506 dev_dbg(arizona->dev, "Retrying high impedance\n");
507 info->num_hpdet_res = 0;
508 info->hpdet_retried = true;
509 arizona_start_hpdet_acc_id(info);
510 pm_runtime_put(info->dev);
515 * If we measure the mic as high impedance
517 if (!id_gpio || info->hpdet_res[1] > 50) {
518 dev_dbg(arizona->dev, "Detected mic\n");
520 info->detecting = true;
522 dev_dbg(arizona->dev, "Detected headphone\n");
525 /* Make sure everything is reset back to the real polarity */
526 regmap_update_bits(arizona->regmap,
527 ARIZONA_ACCESSORY_DETECT_MODE_1,
529 info->micd_modes[0].src);
535 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
537 struct arizona_extcon_info *info = data;
538 struct arizona *arizona = info->arizona;
539 int id_gpio = arizona->pdata.hpdet_id_gpio;
540 int report = ARIZONA_CABLE_HEADPHONE;
544 mutex_lock(&info->lock);
546 /* If we got a spurious IRQ for some reason then ignore it */
547 if (!info->hpdet_active) {
548 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
549 mutex_unlock(&info->lock);
553 /* If the cable was removed while measuring ignore the result */
554 ret = extcon_get_cable_state_(info->edev, ARIZONA_CABLE_MECHANICAL);
556 dev_err(arizona->dev, "Failed to check cable state: %d\n",
560 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
564 ret = arizona_hpdet_read(info);
571 /* Reset back to starting range */
572 regmap_update_bits(arizona->regmap,
573 ARIZONA_HEADPHONE_DETECT_1,
574 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
577 ret = arizona_hpdet_do_id(info, &reading, &mic);
583 /* Report high impedence cables as line outputs */
585 report = ARIZONA_CABLE_LINEOUT;
587 report = ARIZONA_CABLE_HEADPHONE;
589 ret = extcon_set_cable_state_(info->edev, report, true);
591 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
595 /* Reset back to starting range */
596 regmap_update_bits(arizona->regmap,
597 ARIZONA_HEADPHONE_DETECT_1,
598 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
601 arizona_extcon_do_magic(info, 0);
604 gpio_set_value_cansleep(id_gpio, 0);
606 /* Revert back to MICDET mode */
607 regmap_update_bits(arizona->regmap,
608 ARIZONA_ACCESSORY_DETECT_MODE_1,
609 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
611 /* If we have a mic then reenable MICDET */
612 if (mic || info->mic)
613 arizona_start_mic(info);
615 if (info->hpdet_active) {
616 pm_runtime_put_autosuspend(info->dev);
617 info->hpdet_active = false;
620 info->hpdet_done = true;
623 mutex_unlock(&info->lock);
628 static void arizona_identify_headphone(struct arizona_extcon_info *info)
630 struct arizona *arizona = info->arizona;
633 if (info->hpdet_done)
636 dev_dbg(arizona->dev, "Starting HPDET\n");
638 /* Make sure we keep the device enabled during the measurement */
639 pm_runtime_get(info->dev);
641 info->hpdet_active = true;
644 arizona_stop_mic(info);
646 arizona_extcon_do_magic(info, 0x4000);
648 ret = regmap_update_bits(arizona->regmap,
649 ARIZONA_ACCESSORY_DETECT_MODE_1,
650 ARIZONA_ACCDET_MODE_MASK,
651 ARIZONA_ACCDET_MODE_HPL);
653 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
657 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
658 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
660 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
668 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
669 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
671 /* Just report headphone */
672 ret = extcon_update_state(info->edev,
673 1 << ARIZONA_CABLE_HEADPHONE,
674 1 << ARIZONA_CABLE_HEADPHONE);
676 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
679 arizona_start_mic(info);
681 info->hpdet_active = false;
684 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
686 struct arizona *arizona = info->arizona;
691 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
693 /* Make sure we keep the device enabled during the measurement */
694 pm_runtime_get_sync(info->dev);
696 info->hpdet_active = true;
698 arizona_extcon_do_magic(info, 0x4000);
700 ret = regmap_update_bits(arizona->regmap,
701 ARIZONA_ACCESSORY_DETECT_MODE_1,
702 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
703 info->micd_modes[0].src |
704 ARIZONA_ACCDET_MODE_HPL);
706 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
710 if (arizona->pdata.hpdet_acc_id_line) {
711 ret = regmap_update_bits(arizona->regmap,
712 ARIZONA_HEADPHONE_DETECT_1,
713 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
715 dev_err(arizona->dev,
716 "Can't start HPDETL measurement: %d\n",
721 arizona_hpdet_do_id(info, &hp_reading, &mic);
727 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
728 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
730 /* Just report headphone */
731 ret = extcon_update_state(info->edev,
732 1 << ARIZONA_CABLE_HEADPHONE,
733 1 << ARIZONA_CABLE_HEADPHONE);
735 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
737 info->hpdet_active = false;
740 static void arizona_micd_timeout_work(struct work_struct *work)
742 struct arizona_extcon_info *info = container_of(work,
743 struct arizona_extcon_info,
744 micd_timeout_work.work);
746 mutex_lock(&info->lock);
748 dev_dbg(info->arizona->dev, "MICD timed out, reporting HP\n");
749 arizona_identify_headphone(info);
751 info->detecting = false;
753 arizona_stop_mic(info);
755 mutex_unlock(&info->lock);
758 static void arizona_micd_detect(struct work_struct *work)
760 struct arizona_extcon_info *info = container_of(work,
761 struct arizona_extcon_info,
762 micd_detect_work.work);
763 struct arizona *arizona = info->arizona;
764 unsigned int val = 0, lvl;
767 cancel_delayed_work_sync(&info->micd_timeout_work);
769 mutex_lock(&info->lock);
771 /* If the cable was removed while measuring ignore the result */
772 ret = extcon_get_cable_state_(info->edev, ARIZONA_CABLE_MECHANICAL);
774 dev_err(arizona->dev, "Failed to check cable state: %d\n",
776 mutex_unlock(&info->lock);
779 dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n");
780 mutex_unlock(&info->lock);
784 for (i = 0; i < 10 && !(val & MICD_LVL_0_TO_8); i++) {
785 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
787 dev_err(arizona->dev,
788 "Failed to read MICDET: %d\n", ret);
789 mutex_unlock(&info->lock);
793 dev_dbg(arizona->dev, "MICDET: %x\n", val);
795 if (!(val & ARIZONA_MICD_VALID)) {
796 dev_warn(arizona->dev,
797 "Microphone detection state invalid\n");
798 mutex_unlock(&info->lock);
803 if (i == 10 && !(val & MICD_LVL_0_TO_8)) {
804 dev_err(arizona->dev, "Failed to get valid MICDET value\n");
805 mutex_unlock(&info->lock);
809 /* Due to jack detect this should never happen */
810 if (!(val & ARIZONA_MICD_STS)) {
811 dev_warn(arizona->dev, "Detected open circuit\n");
812 info->detecting = false;
816 /* If we got a high impedence we should have a headset, report it. */
817 if (info->detecting && (val & ARIZONA_MICD_LVL_8)) {
818 arizona_identify_headphone(info);
820 ret = extcon_update_state(info->edev,
821 1 << ARIZONA_CABLE_MICROPHONE,
822 1 << ARIZONA_CABLE_MICROPHONE);
825 dev_err(arizona->dev, "Headset report failed: %d\n",
828 /* Don't need to regulate for button detection */
829 ret = regulator_allow_bypass(info->micvdd, false);
831 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
836 info->detecting = false;
840 /* If we detected a lower impedence during initial startup
841 * then we probably have the wrong polarity, flip it. Don't
842 * do this for the lowest impedences to speed up detection of
843 * plain headphones. If both polarities report a low
844 * impedence then give up and report headphones.
846 if (info->detecting && (val & MICD_LVL_1_TO_7)) {
847 if (info->jack_flips >= info->micd_num_modes * 10) {
848 dev_dbg(arizona->dev, "Detected HP/line\n");
849 arizona_identify_headphone(info);
851 info->detecting = false;
853 arizona_stop_mic(info);
856 if (info->micd_mode == info->micd_num_modes)
858 arizona_extcon_set_mode(info, info->micd_mode);
867 * If we're still detecting and we detect a short then we've
868 * got a headphone. Otherwise it's a button press.
870 if (val & MICD_LVL_0_TO_7) {
872 dev_dbg(arizona->dev, "Mic button detected\n");
874 lvl = val & ARIZONA_MICD_LVL_MASK;
875 lvl >>= ARIZONA_MICD_LVL_SHIFT;
877 for (i = 0; i < info->num_micd_ranges; i++)
878 input_report_key(info->input,
879 info->micd_ranges[i].key, 0);
882 WARN_ON(ffs(lvl) - 1 >= info->num_micd_ranges);
883 if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
884 key = info->micd_ranges[ffs(lvl) - 1].key;
885 input_report_key(info->input, key, 1);
886 input_sync(info->input);
889 } else if (info->detecting) {
890 dev_dbg(arizona->dev, "Headphone detected\n");
891 info->detecting = false;
892 arizona_stop_mic(info);
894 arizona_identify_headphone(info);
896 dev_warn(arizona->dev, "Button with no mic: %x\n",
900 dev_dbg(arizona->dev, "Mic button released\n");
901 for (i = 0; i < info->num_micd_ranges; i++)
902 input_report_key(info->input,
903 info->micd_ranges[i].key, 0);
904 input_sync(info->input);
905 arizona_extcon_pulse_micbias(info);
910 queue_delayed_work(system_power_efficient_wq,
911 &info->micd_timeout_work,
912 msecs_to_jiffies(info->micd_timeout));
914 pm_runtime_mark_last_busy(info->dev);
915 mutex_unlock(&info->lock);
918 static irqreturn_t arizona_micdet(int irq, void *data)
920 struct arizona_extcon_info *info = data;
921 struct arizona *arizona = info->arizona;
922 int debounce = arizona->pdata.micd_detect_debounce;
924 cancel_delayed_work_sync(&info->micd_detect_work);
925 cancel_delayed_work_sync(&info->micd_timeout_work);
927 mutex_lock(&info->lock);
928 if (!info->detecting)
930 mutex_unlock(&info->lock);
933 queue_delayed_work(system_power_efficient_wq,
934 &info->micd_detect_work,
935 msecs_to_jiffies(debounce));
937 arizona_micd_detect(&info->micd_detect_work.work);
942 static void arizona_hpdet_work(struct work_struct *work)
944 struct arizona_extcon_info *info = container_of(work,
945 struct arizona_extcon_info,
948 mutex_lock(&info->lock);
949 arizona_start_hpdet_acc_id(info);
950 mutex_unlock(&info->lock);
953 static irqreturn_t arizona_jackdet(int irq, void *data)
955 struct arizona_extcon_info *info = data;
956 struct arizona *arizona = info->arizona;
957 unsigned int val, present, mask;
958 bool cancelled_hp, cancelled_mic;
961 cancelled_hp = cancel_delayed_work_sync(&info->hpdet_work);
962 cancelled_mic = cancel_delayed_work_sync(&info->micd_timeout_work);
964 pm_runtime_get_sync(info->dev);
966 mutex_lock(&info->lock);
968 if (arizona->pdata.jd_gpio5) {
969 mask = ARIZONA_MICD_CLAMP_STS;
970 if (arizona->pdata.jd_invert)
971 present = ARIZONA_MICD_CLAMP_STS;
975 mask = ARIZONA_JD1_STS;
976 if (arizona->pdata.jd_invert)
979 present = ARIZONA_JD1_STS;
982 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
984 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
986 mutex_unlock(&info->lock);
987 pm_runtime_put_autosuspend(info->dev);
992 if (val == info->last_jackdet) {
993 dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
995 queue_delayed_work(system_power_efficient_wq,
997 msecs_to_jiffies(HPDET_DEBOUNCE));
1000 int micd_timeout = info->micd_timeout;
1002 queue_delayed_work(system_power_efficient_wq,
1003 &info->micd_timeout_work,
1004 msecs_to_jiffies(micd_timeout));
1009 info->last_jackdet = val;
1011 if (info->last_jackdet == present) {
1012 dev_dbg(arizona->dev, "Detected jack\n");
1013 ret = extcon_set_cable_state_(info->edev,
1014 ARIZONA_CABLE_MECHANICAL, true);
1017 dev_err(arizona->dev, "Mechanical report failed: %d\n",
1020 if (!arizona->pdata.hpdet_acc_id) {
1021 info->detecting = true;
1023 info->jack_flips = 0;
1025 arizona_start_mic(info);
1027 queue_delayed_work(system_power_efficient_wq,
1029 msecs_to_jiffies(HPDET_DEBOUNCE));
1032 regmap_update_bits(arizona->regmap,
1033 ARIZONA_JACK_DETECT_DEBOUNCE,
1034 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
1036 dev_dbg(arizona->dev, "Detected jack removal\n");
1038 arizona_stop_mic(info);
1040 info->num_hpdet_res = 0;
1041 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
1042 info->hpdet_res[i] = 0;
1044 info->hpdet_done = false;
1045 info->hpdet_retried = false;
1047 for (i = 0; i < info->num_micd_ranges; i++)
1048 input_report_key(info->input,
1049 info->micd_ranges[i].key, 0);
1050 input_sync(info->input);
1052 ret = extcon_update_state(info->edev, 0xffffffff, 0);
1054 dev_err(arizona->dev, "Removal report failed: %d\n",
1057 regmap_update_bits(arizona->regmap,
1058 ARIZONA_JACK_DETECT_DEBOUNCE,
1059 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
1060 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
1063 if (arizona->pdata.micd_timeout)
1064 info->micd_timeout = arizona->pdata.micd_timeout;
1066 info->micd_timeout = DEFAULT_MICD_TIMEOUT;
1069 /* Clear trig_sts to make sure DCVDD is not forced up */
1070 regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
1071 ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
1072 ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
1073 ARIZONA_JD1_FALL_TRIG_STS |
1074 ARIZONA_JD1_RISE_TRIG_STS);
1076 mutex_unlock(&info->lock);
1078 pm_runtime_mark_last_busy(info->dev);
1079 pm_runtime_put_autosuspend(info->dev);
1084 /* Map a level onto a slot in the register bank */
1085 static void arizona_micd_set_level(struct arizona *arizona, int index,
1091 reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
1100 /* Program the level itself */
1101 regmap_update_bits(arizona->regmap, reg, mask, level);
1104 static int arizona_extcon_probe(struct platform_device *pdev)
1106 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
1107 struct arizona_pdata *pdata = &arizona->pdata;
1108 struct arizona_extcon_info *info;
1110 unsigned int clamp_mode;
1111 int jack_irq_fall, jack_irq_rise;
1112 int ret, mode, i, j;
1114 if (!arizona->dapm || !arizona->dapm->card)
1115 return -EPROBE_DEFER;
1117 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1119 dev_err(&pdev->dev, "Failed to allocate memory\n");
1123 info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
1124 if (IS_ERR(info->micvdd)) {
1125 ret = PTR_ERR(info->micvdd);
1126 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
1130 mutex_init(&info->lock);
1131 info->arizona = arizona;
1132 info->dev = &pdev->dev;
1133 info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
1134 INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
1135 INIT_DELAYED_WORK(&info->micd_detect_work, arizona_micd_detect);
1136 INIT_DELAYED_WORK(&info->micd_timeout_work, arizona_micd_timeout_work);
1137 platform_set_drvdata(pdev, info);
1139 switch (arizona->type) {
1141 switch (arizona->rev) {
1143 info->micd_reva = true;
1146 info->micd_clamp = true;
1152 switch (arizona->rev) {
1156 info->micd_clamp = true;
1165 info->edev = devm_extcon_dev_allocate(&pdev->dev, arizona_cable);
1166 if (IS_ERR(info->edev)) {
1167 dev_err(&pdev->dev, "failed to allocate extcon device\n");
1170 info->edev->name = "Headset Jack";
1171 info->edev->dev.parent = arizona->dev;
1173 ret = devm_extcon_dev_register(&pdev->dev, info->edev);
1175 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
1180 info->input = devm_input_allocate_device(&pdev->dev);
1182 dev_err(arizona->dev, "Can't allocate input dev\n");
1187 info->input->name = "Headset";
1188 info->input->phys = "arizona/extcon";
1189 info->input->dev.parent = &pdev->dev;
1191 if (pdata->num_micd_configs) {
1192 info->micd_modes = pdata->micd_configs;
1193 info->micd_num_modes = pdata->num_micd_configs;
1195 info->micd_modes = micd_default_modes;
1196 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1199 if (arizona->pdata.micd_pol_gpio > 0) {
1200 if (info->micd_modes[0].gpio)
1201 mode = GPIOF_OUT_INIT_HIGH;
1203 mode = GPIOF_OUT_INIT_LOW;
1205 ret = devm_gpio_request_one(&pdev->dev,
1206 arizona->pdata.micd_pol_gpio,
1210 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1211 arizona->pdata.micd_pol_gpio, ret);
1216 if (arizona->pdata.hpdet_id_gpio > 0) {
1217 ret = devm_gpio_request_one(&pdev->dev,
1218 arizona->pdata.hpdet_id_gpio,
1222 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1223 arizona->pdata.hpdet_id_gpio, ret);
1228 if (arizona->pdata.micd_bias_start_time)
1229 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1230 ARIZONA_MICD_BIAS_STARTTIME_MASK,
1231 arizona->pdata.micd_bias_start_time
1232 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1234 if (arizona->pdata.micd_rate)
1235 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1236 ARIZONA_MICD_RATE_MASK,
1237 arizona->pdata.micd_rate
1238 << ARIZONA_MICD_RATE_SHIFT);
1240 if (arizona->pdata.micd_dbtime)
1241 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1242 ARIZONA_MICD_DBTIME_MASK,
1243 arizona->pdata.micd_dbtime
1244 << ARIZONA_MICD_DBTIME_SHIFT);
1246 BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) != 0x40);
1248 if (arizona->pdata.num_micd_ranges) {
1249 info->micd_ranges = pdata->micd_ranges;
1250 info->num_micd_ranges = pdata->num_micd_ranges;
1252 info->micd_ranges = micd_default_ranges;
1253 info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
1256 if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_RANGE) {
1257 dev_err(arizona->dev, "Too many MICD ranges: %d\n",
1258 arizona->pdata.num_micd_ranges);
1261 if (info->num_micd_ranges > 1) {
1262 for (i = 1; i < info->num_micd_ranges; i++) {
1263 if (info->micd_ranges[i - 1].max >
1264 info->micd_ranges[i].max) {
1265 dev_err(arizona->dev,
1266 "MICD ranges must be sorted\n");
1273 /* Disable all buttons by default */
1274 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1275 ARIZONA_MICD_LVL_SEL_MASK, 0x81);
1277 /* Set up all the buttons the user specified */
1278 for (i = 0; i < info->num_micd_ranges; i++) {
1279 for (j = 0; j < ARRAY_SIZE(arizona_micd_levels); j++)
1280 if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
1283 if (j == ARRAY_SIZE(arizona_micd_levels)) {
1284 dev_err(arizona->dev, "Unsupported MICD level %d\n",
1285 info->micd_ranges[i].max);
1290 dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
1291 arizona_micd_levels[j], i);
1293 arizona_micd_set_level(arizona, i, j);
1294 input_set_capability(info->input, EV_KEY,
1295 info->micd_ranges[i].key);
1297 /* Enable reporting of that range */
1298 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1302 /* Set all the remaining keys to a maximum */
1303 for (; i < ARIZONA_MAX_MICD_RANGE; i++)
1304 arizona_micd_set_level(arizona, i, 0x3f);
1307 * If we have a clamp use it, activating in conjunction with
1308 * GPIO5 if that is connected for jack detect operation.
1310 if (info->micd_clamp) {
1311 if (arizona->pdata.jd_gpio5) {
1312 /* Put the GPIO into input mode with optional pull */
1314 if (arizona->pdata.jd_gpio5_nopull)
1315 val &= ~ARIZONA_GPN_PU;
1317 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1320 if (arizona->pdata.jd_invert)
1321 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH_GP5H;
1323 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL_GP5H;
1325 if (arizona->pdata.jd_invert)
1326 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH;
1328 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL;
1331 regmap_update_bits(arizona->regmap,
1332 ARIZONA_MICD_CLAMP_CONTROL,
1333 ARIZONA_MICD_CLAMP_MODE_MASK, clamp_mode);
1335 regmap_update_bits(arizona->regmap,
1336 ARIZONA_JACK_DETECT_DEBOUNCE,
1337 ARIZONA_MICD_CLAMP_DB,
1338 ARIZONA_MICD_CLAMP_DB);
1341 arizona_extcon_set_mode(info, 0);
1343 pm_runtime_enable(&pdev->dev);
1344 pm_runtime_idle(&pdev->dev);
1345 pm_runtime_get_sync(&pdev->dev);
1347 if (arizona->pdata.jd_gpio5) {
1348 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1349 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1351 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1352 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1355 ret = arizona_request_irq(arizona, jack_irq_rise,
1356 "JACKDET rise", arizona_jackdet, info);
1358 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1363 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1365 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1370 ret = arizona_request_irq(arizona, jack_irq_fall,
1371 "JACKDET fall", arizona_jackdet, info);
1373 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1377 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1379 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1384 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1385 "MICDET", arizona_micdet, info);
1387 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1391 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1392 "HPDET", arizona_hpdet_irq, info);
1394 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1398 arizona_clk32k_enable(arizona);
1399 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1400 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1401 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1402 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1404 ret = regulator_allow_bypass(info->micvdd, true);
1406 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1409 pm_runtime_put(&pdev->dev);
1411 ret = input_register_device(info->input);
1413 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1420 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1422 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1424 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1426 arizona_free_irq(arizona, jack_irq_fall, info);
1428 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1430 arizona_free_irq(arizona, jack_irq_rise, info);
1433 pm_runtime_disable(&pdev->dev);
1437 static int arizona_extcon_remove(struct platform_device *pdev)
1439 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1440 struct arizona *arizona = info->arizona;
1441 int jack_irq_rise, jack_irq_fall;
1443 pm_runtime_disable(&pdev->dev);
1445 regmap_update_bits(arizona->regmap,
1446 ARIZONA_MICD_CLAMP_CONTROL,
1447 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1449 if (arizona->pdata.jd_gpio5) {
1450 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1451 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1453 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1454 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1457 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1458 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1459 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1460 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1461 arizona_free_irq(arizona, jack_irq_rise, info);
1462 arizona_free_irq(arizona, jack_irq_fall, info);
1463 cancel_delayed_work_sync(&info->hpdet_work);
1464 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1465 ARIZONA_JD1_ENA, 0);
1466 arizona_clk32k_disable(arizona);
1471 static struct platform_driver arizona_extcon_driver = {
1473 .name = "arizona-extcon",
1474 .owner = THIS_MODULE,
1476 .probe = arizona_extcon_probe,
1477 .remove = arizona_extcon_remove,
1480 module_platform_driver(arizona_extcon_driver);
1482 MODULE_DESCRIPTION("Arizona Extcon driver");
1483 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1484 MODULE_LICENSE("GPL");
1485 MODULE_ALIAS("platform:extcon-arizona");