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>
35 #include <dt-bindings/mfd/arizona.h>
37 #define ARIZONA_MAX_MICD_RANGE 8
39 #define ARIZONA_MICD_CLAMP_MODE_JDL 0x4
40 #define ARIZONA_MICD_CLAMP_MODE_JDH 0x5
41 #define ARIZONA_MICD_CLAMP_MODE_JDL_GP5H 0x9
42 #define ARIZONA_MICD_CLAMP_MODE_JDH_GP5H 0xb
44 #define ARIZONA_HPDET_MAX 10000
46 #define HPDET_DEBOUNCE 500
47 #define DEFAULT_MICD_TIMEOUT 2000
49 #define MICD_LVL_1_TO_7 (ARIZONA_MICD_LVL_1 | ARIZONA_MICD_LVL_2 | \
50 ARIZONA_MICD_LVL_3 | ARIZONA_MICD_LVL_4 | \
51 ARIZONA_MICD_LVL_5 | ARIZONA_MICD_LVL_6 | \
54 #define MICD_LVL_0_TO_7 (ARIZONA_MICD_LVL_0 | MICD_LVL_1_TO_7)
56 #define MICD_LVL_0_TO_8 (MICD_LVL_0_TO_7 | ARIZONA_MICD_LVL_8)
58 struct arizona_extcon_info {
60 struct arizona *arizona;
62 struct regulator *micvdd;
63 struct input_dev *input;
68 const struct arizona_micd_config *micd_modes;
71 const struct arizona_micd_range *micd_ranges;
79 struct delayed_work hpdet_work;
80 struct delayed_work micd_detect_work;
81 struct delayed_work micd_timeout_work;
88 unsigned int hpdet_res[3];
96 struct extcon_dev *edev;
99 static const struct arizona_micd_config micd_default_modes[] = {
100 { ARIZONA_ACCDET_SRC, 1, 0 },
104 static const struct arizona_micd_range micd_default_ranges[] = {
105 { .max = 11, .key = BTN_0 },
106 { .max = 28, .key = BTN_1 },
107 { .max = 54, .key = BTN_2 },
108 { .max = 100, .key = BTN_3 },
109 { .max = 186, .key = BTN_4 },
110 { .max = 430, .key = BTN_5 },
113 static const int arizona_micd_levels[] = {
114 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
115 49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
116 105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
117 270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
121 static const unsigned int arizona_cable[] = {
129 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info);
131 static void arizona_extcon_hp_clamp(struct arizona_extcon_info *info,
134 struct arizona *arizona = info->arizona;
135 unsigned int mask = 0, val = 0;
138 switch (arizona->type) {
141 mask = ARIZONA_HP1L_SHRTO | ARIZONA_HP1L_FLWR |
144 val = ARIZONA_HP1L_SHRTO;
146 val = ARIZONA_HP1L_FLWR | ARIZONA_HP1L_SHRTI;
149 mask = ARIZONA_RMV_SHRT_HP1L;
151 val = ARIZONA_RMV_SHRT_HP1L;
155 mutex_lock(&arizona->dapm->card->dapm_mutex);
157 arizona->hpdet_clamp = clamp;
159 /* Keep the HP output stages disabled while doing the clamp */
161 ret = regmap_update_bits(arizona->regmap,
162 ARIZONA_OUTPUT_ENABLES_1,
164 ARIZONA_OUT1R_ENA, 0);
166 dev_warn(arizona->dev,
167 "Failed to disable headphone outputs: %d\n",
171 ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1L,
174 dev_warn(arizona->dev, "Failed to do clamp: %d\n",
177 ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1R,
180 dev_warn(arizona->dev, "Failed to do clamp: %d\n",
183 /* Restore the desired state while not doing the clamp */
185 ret = regmap_update_bits(arizona->regmap,
186 ARIZONA_OUTPUT_ENABLES_1,
188 ARIZONA_OUT1R_ENA, arizona->hp_ena);
190 dev_warn(arizona->dev,
191 "Failed to restore headphone outputs: %d\n",
195 mutex_unlock(&arizona->dapm->card->dapm_mutex);
198 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
200 struct arizona *arizona = info->arizona;
202 mode %= info->micd_num_modes;
204 if (arizona->pdata.micd_pol_gpio > 0)
205 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
206 info->micd_modes[mode].gpio);
207 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
208 ARIZONA_MICD_BIAS_SRC_MASK,
209 info->micd_modes[mode].bias <<
210 ARIZONA_MICD_BIAS_SRC_SHIFT);
211 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
212 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
214 info->micd_mode = mode;
216 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
219 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
221 switch (info->micd_modes[0].bias) {
233 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
235 struct arizona *arizona = info->arizona;
236 const char *widget = arizona_extcon_get_micbias(info);
237 struct snd_soc_dapm_context *dapm = arizona->dapm;
240 ret = snd_soc_dapm_force_enable_pin(dapm, widget);
242 dev_warn(arizona->dev, "Failed to enable %s: %d\n",
245 snd_soc_dapm_sync(dapm);
247 if (!arizona->pdata.micd_force_micbias) {
248 ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
250 dev_warn(arizona->dev, "Failed to disable %s: %d\n",
253 snd_soc_dapm_sync(dapm);
257 static void arizona_start_mic(struct arizona_extcon_info *info)
259 struct arizona *arizona = info->arizona;
263 /* Microphone detection can't use idle mode */
264 pm_runtime_get(info->dev);
266 if (info->detecting) {
267 ret = regulator_allow_bypass(info->micvdd, false);
269 dev_err(arizona->dev,
270 "Failed to regulate MICVDD: %d\n",
275 ret = regulator_enable(info->micvdd);
277 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
281 if (info->micd_reva) {
282 regmap_write(arizona->regmap, 0x80, 0x3);
283 regmap_write(arizona->regmap, 0x294, 0);
284 regmap_write(arizona->regmap, 0x80, 0x0);
287 regmap_update_bits(arizona->regmap,
288 ARIZONA_ACCESSORY_DETECT_MODE_1,
289 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
291 arizona_extcon_pulse_micbias(info);
293 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
294 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
297 regulator_disable(info->micvdd);
298 pm_runtime_put_autosuspend(info->dev);
302 static void arizona_stop_mic(struct arizona_extcon_info *info)
304 struct arizona *arizona = info->arizona;
305 const char *widget = arizona_extcon_get_micbias(info);
306 struct snd_soc_dapm_context *dapm = arizona->dapm;
310 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
314 ret = snd_soc_dapm_disable_pin(dapm, widget);
316 dev_warn(arizona->dev,
317 "Failed to disable %s: %d\n",
320 snd_soc_dapm_sync(dapm);
322 if (info->micd_reva) {
323 regmap_write(arizona->regmap, 0x80, 0x3);
324 regmap_write(arizona->regmap, 0x294, 2);
325 regmap_write(arizona->regmap, 0x80, 0x0);
328 ret = regulator_allow_bypass(info->micvdd, true);
330 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
335 regulator_disable(info->micvdd);
336 pm_runtime_mark_last_busy(info->dev);
337 pm_runtime_put_autosuspend(info->dev);
342 unsigned int threshold;
343 unsigned int factor_a;
344 unsigned int factor_b;
345 } arizona_hpdet_b_ranges[] = {
346 { 100, 5528, 362464 },
347 { 169, 11084, 6186851 },
348 { 169, 11065, 65460395 },
351 #define ARIZONA_HPDET_B_RANGE_MAX 0x3fb
356 } arizona_hpdet_c_ranges[] = {
363 static int arizona_hpdet_read(struct arizona_extcon_info *info)
365 struct arizona *arizona = info->arizona;
366 unsigned int val, range;
369 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
371 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
376 switch (info->hpdet_ip_version) {
378 if (!(val & ARIZONA_HP_DONE)) {
379 dev_err(arizona->dev, "HPDET did not complete: %x\n",
384 val &= ARIZONA_HP_LVL_MASK;
388 if (!(val & ARIZONA_HP_DONE_B)) {
389 dev_err(arizona->dev, "HPDET did not complete: %x\n",
394 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
396 dev_err(arizona->dev, "Failed to read HP value: %d\n",
401 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
403 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
404 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
406 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
407 (val < arizona_hpdet_b_ranges[range].threshold ||
408 val >= ARIZONA_HPDET_B_RANGE_MAX)) {
410 dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
412 regmap_update_bits(arizona->regmap,
413 ARIZONA_HEADPHONE_DETECT_1,
414 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
416 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
420 /* If we go out of range report top of range */
421 if (val < arizona_hpdet_b_ranges[range].threshold ||
422 val >= ARIZONA_HPDET_B_RANGE_MAX) {
423 dev_dbg(arizona->dev, "Measurement out of range\n");
424 return ARIZONA_HPDET_MAX;
427 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
430 val = arizona_hpdet_b_ranges[range].factor_b
432 arizona_hpdet_b_ranges[range].factor_a);
436 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
437 info->hpdet_ip_version);
439 if (!(val & ARIZONA_HP_DONE_B)) {
440 dev_err(arizona->dev, "HPDET did not complete: %x\n",
445 val &= ARIZONA_HP_LVL_B_MASK;
446 /* Convert to ohms, the value is in 0.5 ohm increments */
449 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
451 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
452 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
454 /* Skip up a range, or report? */
455 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
456 (val >= arizona_hpdet_c_ranges[range].max)) {
458 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
459 arizona_hpdet_c_ranges[range].min,
460 arizona_hpdet_c_ranges[range].max);
461 regmap_update_bits(arizona->regmap,
462 ARIZONA_HEADPHONE_DETECT_1,
463 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
465 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
469 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
470 dev_dbg(arizona->dev, "Reporting range boundary %d\n",
471 arizona_hpdet_c_ranges[range].min);
472 val = arizona_hpdet_c_ranges[range].min;
476 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
480 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading,
483 struct arizona *arizona = info->arizona;
484 int id_gpio = arizona->pdata.hpdet_id_gpio;
487 * If we're using HPDET for accessory identification we need
488 * to take multiple measurements, step through them in sequence.
490 if (arizona->pdata.hpdet_acc_id) {
491 info->hpdet_res[info->num_hpdet_res++] = *reading;
493 /* Only check the mic directly if we didn't already ID it */
494 if (id_gpio && info->num_hpdet_res == 1) {
495 dev_dbg(arizona->dev, "Measuring mic\n");
497 regmap_update_bits(arizona->regmap,
498 ARIZONA_ACCESSORY_DETECT_MODE_1,
499 ARIZONA_ACCDET_MODE_MASK |
501 ARIZONA_ACCDET_MODE_HPR |
502 info->micd_modes[0].src);
504 gpio_set_value_cansleep(id_gpio, 1);
506 regmap_update_bits(arizona->regmap,
507 ARIZONA_HEADPHONE_DETECT_1,
508 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
512 /* OK, got both. Now, compare... */
513 dev_dbg(arizona->dev, "HPDET measured %d %d\n",
514 info->hpdet_res[0], info->hpdet_res[1]);
516 /* Take the headphone impedance for the main report */
517 *reading = info->hpdet_res[0];
519 /* Sometimes we get false readings due to slow insert */
520 if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
521 dev_dbg(arizona->dev, "Retrying high impedance\n");
522 info->num_hpdet_res = 0;
523 info->hpdet_retried = true;
524 arizona_start_hpdet_acc_id(info);
525 pm_runtime_put(info->dev);
530 * If we measure the mic as high impedance
532 if (!id_gpio || info->hpdet_res[1] > 50) {
533 dev_dbg(arizona->dev, "Detected mic\n");
535 info->detecting = true;
537 dev_dbg(arizona->dev, "Detected headphone\n");
540 /* Make sure everything is reset back to the real polarity */
541 regmap_update_bits(arizona->regmap,
542 ARIZONA_ACCESSORY_DETECT_MODE_1,
544 info->micd_modes[0].src);
550 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
552 struct arizona_extcon_info *info = data;
553 struct arizona *arizona = info->arizona;
554 int id_gpio = arizona->pdata.hpdet_id_gpio;
555 unsigned int report = EXTCON_HEADPHONE;
559 mutex_lock(&info->lock);
561 /* If we got a spurious IRQ for some reason then ignore it */
562 if (!info->hpdet_active) {
563 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
564 mutex_unlock(&info->lock);
568 /* If the cable was removed while measuring ignore the result */
569 ret = extcon_get_cable_state_(info->edev, EXTCON_MECHANICAL);
571 dev_err(arizona->dev, "Failed to check cable state: %d\n",
575 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
579 ret = arizona_hpdet_read(info);
586 /* Reset back to starting range */
587 regmap_update_bits(arizona->regmap,
588 ARIZONA_HEADPHONE_DETECT_1,
589 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
592 ret = arizona_hpdet_do_id(info, &reading, &mic);
598 /* Report high impedence cables as line outputs */
600 report = EXTCON_LINE_OUT;
602 report = EXTCON_HEADPHONE;
604 ret = extcon_set_cable_state_(info->edev, report, true);
606 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
610 /* Reset back to starting range */
611 regmap_update_bits(arizona->regmap,
612 ARIZONA_HEADPHONE_DETECT_1,
613 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
616 arizona_extcon_hp_clamp(info, false);
619 gpio_set_value_cansleep(id_gpio, 0);
621 /* Revert back to MICDET mode */
622 regmap_update_bits(arizona->regmap,
623 ARIZONA_ACCESSORY_DETECT_MODE_1,
624 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
626 /* If we have a mic then reenable MICDET */
627 if (mic || info->mic)
628 arizona_start_mic(info);
630 if (info->hpdet_active) {
631 pm_runtime_put_autosuspend(info->dev);
632 info->hpdet_active = false;
635 info->hpdet_done = true;
638 mutex_unlock(&info->lock);
643 static void arizona_identify_headphone(struct arizona_extcon_info *info)
645 struct arizona *arizona = info->arizona;
648 if (info->hpdet_done)
651 dev_dbg(arizona->dev, "Starting HPDET\n");
653 /* Make sure we keep the device enabled during the measurement */
654 pm_runtime_get(info->dev);
656 info->hpdet_active = true;
659 arizona_stop_mic(info);
661 arizona_extcon_hp_clamp(info, true);
663 ret = regmap_update_bits(arizona->regmap,
664 ARIZONA_ACCESSORY_DETECT_MODE_1,
665 ARIZONA_ACCDET_MODE_MASK,
666 arizona->pdata.hpdet_channel);
668 dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
672 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
673 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
675 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
683 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
684 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
686 /* Just report headphone */
687 ret = extcon_set_cable_state_(info->edev, EXTCON_HEADPHONE, true);
689 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
692 arizona_start_mic(info);
694 info->hpdet_active = false;
697 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
699 struct arizona *arizona = info->arizona;
704 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
706 /* Make sure we keep the device enabled during the measurement */
707 pm_runtime_get_sync(info->dev);
709 info->hpdet_active = true;
711 arizona_extcon_hp_clamp(info, true);
713 ret = regmap_update_bits(arizona->regmap,
714 ARIZONA_ACCESSORY_DETECT_MODE_1,
715 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
716 info->micd_modes[0].src |
717 arizona->pdata.hpdet_channel);
719 dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
723 if (arizona->pdata.hpdet_acc_id_line) {
724 ret = regmap_update_bits(arizona->regmap,
725 ARIZONA_HEADPHONE_DETECT_1,
726 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
728 dev_err(arizona->dev,
729 "Can't start HPDETL measurement: %d\n",
734 arizona_hpdet_do_id(info, &hp_reading, &mic);
740 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
741 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
743 /* Just report headphone */
744 ret = extcon_set_cable_state_(info->edev, EXTCON_HEADPHONE, true);
746 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
748 info->hpdet_active = false;
751 static void arizona_micd_timeout_work(struct work_struct *work)
753 struct arizona_extcon_info *info = container_of(work,
754 struct arizona_extcon_info,
755 micd_timeout_work.work);
757 mutex_lock(&info->lock);
759 dev_dbg(info->arizona->dev, "MICD timed out, reporting HP\n");
760 arizona_identify_headphone(info);
762 info->detecting = false;
764 arizona_stop_mic(info);
766 mutex_unlock(&info->lock);
769 static void arizona_micd_detect(struct work_struct *work)
771 struct arizona_extcon_info *info = container_of(work,
772 struct arizona_extcon_info,
773 micd_detect_work.work);
774 struct arizona *arizona = info->arizona;
775 unsigned int val = 0, lvl;
778 cancel_delayed_work_sync(&info->micd_timeout_work);
780 mutex_lock(&info->lock);
782 /* If the cable was removed while measuring ignore the result */
783 ret = extcon_get_cable_state_(info->edev, EXTCON_MECHANICAL);
785 dev_err(arizona->dev, "Failed to check cable state: %d\n",
787 mutex_unlock(&info->lock);
790 dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n");
791 mutex_unlock(&info->lock);
795 for (i = 0; i < 10 && !(val & MICD_LVL_0_TO_8); i++) {
796 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
798 dev_err(arizona->dev,
799 "Failed to read MICDET: %d\n", ret);
800 mutex_unlock(&info->lock);
804 dev_dbg(arizona->dev, "MICDET: %x\n", val);
806 if (!(val & ARIZONA_MICD_VALID)) {
807 dev_warn(arizona->dev,
808 "Microphone detection state invalid\n");
809 mutex_unlock(&info->lock);
814 if (i == 10 && !(val & MICD_LVL_0_TO_8)) {
815 dev_err(arizona->dev, "Failed to get valid MICDET value\n");
816 mutex_unlock(&info->lock);
820 /* Due to jack detect this should never happen */
821 if (!(val & ARIZONA_MICD_STS)) {
822 dev_warn(arizona->dev, "Detected open circuit\n");
823 info->detecting = false;
827 /* If we got a high impedence we should have a headset, report it. */
828 if (info->detecting && (val & ARIZONA_MICD_LVL_8)) {
829 arizona_identify_headphone(info);
831 ret = extcon_set_cable_state_(info->edev,
832 EXTCON_MICROPHONE, true);
834 dev_err(arizona->dev, "Headset report failed: %d\n",
837 /* Don't need to regulate for button detection */
838 ret = regulator_allow_bypass(info->micvdd, true);
840 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
845 info->detecting = false;
849 /* If we detected a lower impedence during initial startup
850 * then we probably have the wrong polarity, flip it. Don't
851 * do this for the lowest impedences to speed up detection of
852 * plain headphones. If both polarities report a low
853 * impedence then give up and report headphones.
855 if (info->detecting && (val & MICD_LVL_1_TO_7)) {
856 if (info->jack_flips >= info->micd_num_modes * 10) {
857 dev_dbg(arizona->dev, "Detected HP/line\n");
858 arizona_identify_headphone(info);
860 info->detecting = false;
862 arizona_stop_mic(info);
865 if (info->micd_mode == info->micd_num_modes)
867 arizona_extcon_set_mode(info, info->micd_mode);
876 * If we're still detecting and we detect a short then we've
877 * got a headphone. Otherwise it's a button press.
879 if (val & MICD_LVL_0_TO_7) {
881 dev_dbg(arizona->dev, "Mic button detected\n");
883 lvl = val & ARIZONA_MICD_LVL_MASK;
884 lvl >>= ARIZONA_MICD_LVL_SHIFT;
886 for (i = 0; i < info->num_micd_ranges; i++)
887 input_report_key(info->input,
888 info->micd_ranges[i].key, 0);
891 WARN_ON(ffs(lvl) - 1 >= info->num_micd_ranges);
892 if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
893 key = info->micd_ranges[ffs(lvl) - 1].key;
894 input_report_key(info->input, key, 1);
895 input_sync(info->input);
898 } else if (info->detecting) {
899 dev_dbg(arizona->dev, "Headphone detected\n");
900 info->detecting = false;
901 arizona_stop_mic(info);
903 arizona_identify_headphone(info);
905 dev_warn(arizona->dev, "Button with no mic: %x\n",
909 dev_dbg(arizona->dev, "Mic button released\n");
910 for (i = 0; i < info->num_micd_ranges; i++)
911 input_report_key(info->input,
912 info->micd_ranges[i].key, 0);
913 input_sync(info->input);
914 arizona_extcon_pulse_micbias(info);
919 queue_delayed_work(system_power_efficient_wq,
920 &info->micd_timeout_work,
921 msecs_to_jiffies(info->micd_timeout));
923 pm_runtime_mark_last_busy(info->dev);
924 mutex_unlock(&info->lock);
927 static irqreturn_t arizona_micdet(int irq, void *data)
929 struct arizona_extcon_info *info = data;
930 struct arizona *arizona = info->arizona;
931 int debounce = arizona->pdata.micd_detect_debounce;
933 cancel_delayed_work_sync(&info->micd_detect_work);
934 cancel_delayed_work_sync(&info->micd_timeout_work);
936 mutex_lock(&info->lock);
937 if (!info->detecting)
939 mutex_unlock(&info->lock);
942 queue_delayed_work(system_power_efficient_wq,
943 &info->micd_detect_work,
944 msecs_to_jiffies(debounce));
946 arizona_micd_detect(&info->micd_detect_work.work);
951 static void arizona_hpdet_work(struct work_struct *work)
953 struct arizona_extcon_info *info = container_of(work,
954 struct arizona_extcon_info,
957 mutex_lock(&info->lock);
958 arizona_start_hpdet_acc_id(info);
959 mutex_unlock(&info->lock);
962 static irqreturn_t arizona_jackdet(int irq, void *data)
964 struct arizona_extcon_info *info = data;
965 struct arizona *arizona = info->arizona;
966 unsigned int val, present, mask;
967 bool cancelled_hp, cancelled_mic;
970 cancelled_hp = cancel_delayed_work_sync(&info->hpdet_work);
971 cancelled_mic = cancel_delayed_work_sync(&info->micd_timeout_work);
973 pm_runtime_get_sync(info->dev);
975 mutex_lock(&info->lock);
977 if (arizona->pdata.jd_gpio5) {
978 mask = ARIZONA_MICD_CLAMP_STS;
979 if (arizona->pdata.jd_invert)
980 present = ARIZONA_MICD_CLAMP_STS;
984 mask = ARIZONA_JD1_STS;
985 if (arizona->pdata.jd_invert)
988 present = ARIZONA_JD1_STS;
991 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
993 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
995 mutex_unlock(&info->lock);
996 pm_runtime_put_autosuspend(info->dev);
1001 if (val == info->last_jackdet) {
1002 dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
1004 queue_delayed_work(system_power_efficient_wq,
1006 msecs_to_jiffies(HPDET_DEBOUNCE));
1008 if (cancelled_mic) {
1009 int micd_timeout = info->micd_timeout;
1011 queue_delayed_work(system_power_efficient_wq,
1012 &info->micd_timeout_work,
1013 msecs_to_jiffies(micd_timeout));
1018 info->last_jackdet = val;
1020 if (info->last_jackdet == present) {
1021 dev_dbg(arizona->dev, "Detected jack\n");
1022 ret = extcon_set_cable_state_(info->edev,
1023 EXTCON_MECHANICAL, true);
1026 dev_err(arizona->dev, "Mechanical report failed: %d\n",
1029 if (!arizona->pdata.hpdet_acc_id) {
1030 info->detecting = true;
1032 info->jack_flips = 0;
1034 arizona_start_mic(info);
1036 queue_delayed_work(system_power_efficient_wq,
1038 msecs_to_jiffies(HPDET_DEBOUNCE));
1041 regmap_update_bits(arizona->regmap,
1042 ARIZONA_JACK_DETECT_DEBOUNCE,
1043 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
1045 dev_dbg(arizona->dev, "Detected jack removal\n");
1047 arizona_stop_mic(info);
1049 info->num_hpdet_res = 0;
1050 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
1051 info->hpdet_res[i] = 0;
1053 info->hpdet_done = false;
1054 info->hpdet_retried = false;
1056 for (i = 0; i < info->num_micd_ranges; i++)
1057 input_report_key(info->input,
1058 info->micd_ranges[i].key, 0);
1059 input_sync(info->input);
1061 ret = extcon_update_state(info->edev, 0xffffffff, 0);
1063 dev_err(arizona->dev, "Removal report failed: %d\n",
1066 regmap_update_bits(arizona->regmap,
1067 ARIZONA_JACK_DETECT_DEBOUNCE,
1068 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
1069 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
1072 if (arizona->pdata.micd_timeout)
1073 info->micd_timeout = arizona->pdata.micd_timeout;
1075 info->micd_timeout = DEFAULT_MICD_TIMEOUT;
1078 /* Clear trig_sts to make sure DCVDD is not forced up */
1079 regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
1080 ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
1081 ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
1082 ARIZONA_JD1_FALL_TRIG_STS |
1083 ARIZONA_JD1_RISE_TRIG_STS);
1085 mutex_unlock(&info->lock);
1087 pm_runtime_mark_last_busy(info->dev);
1088 pm_runtime_put_autosuspend(info->dev);
1093 /* Map a level onto a slot in the register bank */
1094 static void arizona_micd_set_level(struct arizona *arizona, int index,
1100 reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
1109 /* Program the level itself */
1110 regmap_update_bits(arizona->regmap, reg, mask, level);
1113 static int arizona_extcon_of_get_pdata(struct arizona *arizona)
1115 struct arizona_pdata *pdata = &arizona->pdata;
1116 unsigned int val = ARIZONA_ACCDET_MODE_HPL;
1118 of_property_read_u32(arizona->dev->of_node, "wlf,hpdet-channel", &val);
1120 case ARIZONA_ACCDET_MODE_HPL:
1121 case ARIZONA_ACCDET_MODE_HPR:
1122 pdata->hpdet_channel = val;
1125 dev_err(arizona->dev,
1126 "Wrong wlf,hpdet-channel DT value %d\n", val);
1127 pdata->hpdet_channel = ARIZONA_ACCDET_MODE_HPL;
1133 static int arizona_extcon_probe(struct platform_device *pdev)
1135 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
1136 struct arizona_pdata *pdata = &arizona->pdata;
1137 struct arizona_extcon_info *info;
1139 unsigned int clamp_mode;
1140 int jack_irq_fall, jack_irq_rise;
1141 int ret, mode, i, j;
1143 if (!arizona->dapm || !arizona->dapm->card)
1144 return -EPROBE_DEFER;
1146 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1150 if (IS_ENABLED(CONFIG_OF)) {
1151 if (!dev_get_platdata(arizona->dev))
1152 arizona_extcon_of_get_pdata(arizona);
1155 info->micvdd = devm_regulator_get(&pdev->dev, "MICVDD");
1156 if (IS_ERR(info->micvdd)) {
1157 ret = PTR_ERR(info->micvdd);
1158 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
1162 mutex_init(&info->lock);
1163 info->arizona = arizona;
1164 info->dev = &pdev->dev;
1165 info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
1166 INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
1167 INIT_DELAYED_WORK(&info->micd_detect_work, arizona_micd_detect);
1168 INIT_DELAYED_WORK(&info->micd_timeout_work, arizona_micd_timeout_work);
1169 platform_set_drvdata(pdev, info);
1171 switch (arizona->type) {
1173 switch (arizona->rev) {
1175 info->micd_reva = true;
1178 info->micd_clamp = true;
1179 info->hpdet_ip_version = 1;
1185 switch (arizona->rev) {
1189 info->micd_clamp = true;
1190 info->hpdet_ip_version = 2;
1198 info->edev = devm_extcon_dev_allocate(&pdev->dev, arizona_cable);
1199 if (IS_ERR(info->edev)) {
1200 dev_err(&pdev->dev, "failed to allocate extcon device\n");
1204 ret = devm_extcon_dev_register(&pdev->dev, info->edev);
1206 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
1211 info->input = devm_input_allocate_device(&pdev->dev);
1213 dev_err(arizona->dev, "Can't allocate input dev\n");
1218 info->input->name = "Headset";
1219 info->input->phys = "arizona/extcon";
1221 if (pdata->num_micd_configs) {
1222 info->micd_modes = pdata->micd_configs;
1223 info->micd_num_modes = pdata->num_micd_configs;
1225 info->micd_modes = micd_default_modes;
1226 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1229 if (arizona->pdata.micd_pol_gpio > 0) {
1230 if (info->micd_modes[0].gpio)
1231 mode = GPIOF_OUT_INIT_HIGH;
1233 mode = GPIOF_OUT_INIT_LOW;
1235 ret = devm_gpio_request_one(&pdev->dev,
1236 arizona->pdata.micd_pol_gpio,
1240 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1241 arizona->pdata.micd_pol_gpio, ret);
1246 if (arizona->pdata.hpdet_id_gpio > 0) {
1247 ret = devm_gpio_request_one(&pdev->dev,
1248 arizona->pdata.hpdet_id_gpio,
1252 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1253 arizona->pdata.hpdet_id_gpio, ret);
1258 if (arizona->pdata.micd_bias_start_time)
1259 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1260 ARIZONA_MICD_BIAS_STARTTIME_MASK,
1261 arizona->pdata.micd_bias_start_time
1262 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1264 if (arizona->pdata.micd_rate)
1265 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1266 ARIZONA_MICD_RATE_MASK,
1267 arizona->pdata.micd_rate
1268 << ARIZONA_MICD_RATE_SHIFT);
1270 if (arizona->pdata.micd_dbtime)
1271 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1272 ARIZONA_MICD_DBTIME_MASK,
1273 arizona->pdata.micd_dbtime
1274 << ARIZONA_MICD_DBTIME_SHIFT);
1276 BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) != 0x40);
1278 if (arizona->pdata.num_micd_ranges) {
1279 info->micd_ranges = pdata->micd_ranges;
1280 info->num_micd_ranges = pdata->num_micd_ranges;
1282 info->micd_ranges = micd_default_ranges;
1283 info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
1286 if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_RANGE) {
1287 dev_err(arizona->dev, "Too many MICD ranges: %d\n",
1288 arizona->pdata.num_micd_ranges);
1291 if (info->num_micd_ranges > 1) {
1292 for (i = 1; i < info->num_micd_ranges; i++) {
1293 if (info->micd_ranges[i - 1].max >
1294 info->micd_ranges[i].max) {
1295 dev_err(arizona->dev,
1296 "MICD ranges must be sorted\n");
1303 /* Disable all buttons by default */
1304 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1305 ARIZONA_MICD_LVL_SEL_MASK, 0x81);
1307 /* Set up all the buttons the user specified */
1308 for (i = 0; i < info->num_micd_ranges; i++) {
1309 for (j = 0; j < ARRAY_SIZE(arizona_micd_levels); j++)
1310 if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
1313 if (j == ARRAY_SIZE(arizona_micd_levels)) {
1314 dev_err(arizona->dev, "Unsupported MICD level %d\n",
1315 info->micd_ranges[i].max);
1320 dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
1321 arizona_micd_levels[j], i);
1323 arizona_micd_set_level(arizona, i, j);
1324 input_set_capability(info->input, EV_KEY,
1325 info->micd_ranges[i].key);
1327 /* Enable reporting of that range */
1328 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1332 /* Set all the remaining keys to a maximum */
1333 for (; i < ARIZONA_MAX_MICD_RANGE; i++)
1334 arizona_micd_set_level(arizona, i, 0x3f);
1337 * If we have a clamp use it, activating in conjunction with
1338 * GPIO5 if that is connected for jack detect operation.
1340 if (info->micd_clamp) {
1341 if (arizona->pdata.jd_gpio5) {
1342 /* Put the GPIO into input mode with optional pull */
1344 if (arizona->pdata.jd_gpio5_nopull)
1345 val &= ~ARIZONA_GPN_PU;
1347 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1350 if (arizona->pdata.jd_invert)
1351 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH_GP5H;
1353 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL_GP5H;
1355 if (arizona->pdata.jd_invert)
1356 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH;
1358 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL;
1361 regmap_update_bits(arizona->regmap,
1362 ARIZONA_MICD_CLAMP_CONTROL,
1363 ARIZONA_MICD_CLAMP_MODE_MASK, clamp_mode);
1365 regmap_update_bits(arizona->regmap,
1366 ARIZONA_JACK_DETECT_DEBOUNCE,
1367 ARIZONA_MICD_CLAMP_DB,
1368 ARIZONA_MICD_CLAMP_DB);
1371 arizona_extcon_set_mode(info, 0);
1373 pm_runtime_enable(&pdev->dev);
1374 pm_runtime_idle(&pdev->dev);
1375 pm_runtime_get_sync(&pdev->dev);
1377 if (arizona->pdata.jd_gpio5) {
1378 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1379 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1381 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1382 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1385 ret = arizona_request_irq(arizona, jack_irq_rise,
1386 "JACKDET rise", arizona_jackdet, info);
1388 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1393 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1395 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1400 ret = arizona_request_irq(arizona, jack_irq_fall,
1401 "JACKDET fall", arizona_jackdet, info);
1403 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1407 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1409 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1414 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1415 "MICDET", arizona_micdet, info);
1417 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1421 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1422 "HPDET", arizona_hpdet_irq, info);
1424 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1428 arizona_clk32k_enable(arizona);
1429 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1430 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1431 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1432 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1434 ret = regulator_allow_bypass(info->micvdd, true);
1436 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1439 pm_runtime_put(&pdev->dev);
1441 ret = input_register_device(info->input);
1443 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1450 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1452 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1454 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1456 arizona_free_irq(arizona, jack_irq_fall, info);
1458 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1460 arizona_free_irq(arizona, jack_irq_rise, info);
1463 pm_runtime_disable(&pdev->dev);
1467 static int arizona_extcon_remove(struct platform_device *pdev)
1469 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1470 struct arizona *arizona = info->arizona;
1471 int jack_irq_rise, jack_irq_fall;
1473 pm_runtime_disable(&pdev->dev);
1475 regmap_update_bits(arizona->regmap,
1476 ARIZONA_MICD_CLAMP_CONTROL,
1477 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1479 if (arizona->pdata.jd_gpio5) {
1480 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1481 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1483 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1484 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1487 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1488 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1489 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1490 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1491 arizona_free_irq(arizona, jack_irq_rise, info);
1492 arizona_free_irq(arizona, jack_irq_fall, info);
1493 cancel_delayed_work_sync(&info->hpdet_work);
1494 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1495 ARIZONA_JD1_ENA, 0);
1496 arizona_clk32k_disable(arizona);
1501 static struct platform_driver arizona_extcon_driver = {
1503 .name = "arizona-extcon",
1505 .probe = arizona_extcon_probe,
1506 .remove = arizona_extcon_remove,
1509 module_platform_driver(arizona_extcon_driver);
1511 MODULE_DESCRIPTION("Arizona Extcon driver");
1512 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1513 MODULE_LICENSE("GPL");
1514 MODULE_ALIAS("platform:extcon-arizona");