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 #define ARIZONA_CABLE_MECHANICAL 0
122 #define ARIZONA_CABLE_MICROPHONE 1
123 #define ARIZONA_CABLE_HEADPHONE 2
124 #define ARIZONA_CABLE_LINEOUT 3
126 static const char *arizona_cable[] = {
134 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info);
136 static void arizona_extcon_hp_clamp(struct arizona_extcon_info *info,
139 struct arizona *arizona = info->arizona;
140 unsigned int mask = 0, val = 0;
143 switch (arizona->type) {
146 mask = ARIZONA_HP1L_SHRTO | ARIZONA_HP1L_FLWR |
149 val = ARIZONA_HP1L_SHRTO;
151 val = ARIZONA_HP1L_FLWR | ARIZONA_HP1L_SHRTI;
154 mask = ARIZONA_RMV_SHRT_HP1L;
156 val = ARIZONA_RMV_SHRT_HP1L;
160 mutex_lock(&arizona->dapm->card->dapm_mutex);
162 arizona->hpdet_clamp = clamp;
164 /* Keep the HP output stages disabled while doing the clamp */
166 ret = regmap_update_bits(arizona->regmap,
167 ARIZONA_OUTPUT_ENABLES_1,
169 ARIZONA_OUT1R_ENA, 0);
171 dev_warn(arizona->dev,
172 "Failed to disable headphone outputs: %d\n",
176 ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1L,
179 dev_warn(arizona->dev, "Failed to do clamp: %d\n",
182 ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1R,
185 dev_warn(arizona->dev, "Failed to do clamp: %d\n",
188 /* Restore the desired state while not doing the clamp */
190 ret = regmap_update_bits(arizona->regmap,
191 ARIZONA_OUTPUT_ENABLES_1,
193 ARIZONA_OUT1R_ENA, arizona->hp_ena);
195 dev_warn(arizona->dev,
196 "Failed to restore headphone outputs: %d\n",
200 mutex_unlock(&arizona->dapm->card->dapm_mutex);
203 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
205 struct arizona *arizona = info->arizona;
207 mode %= info->micd_num_modes;
209 if (arizona->pdata.micd_pol_gpio > 0)
210 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
211 info->micd_modes[mode].gpio);
212 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
213 ARIZONA_MICD_BIAS_SRC_MASK,
214 info->micd_modes[mode].bias <<
215 ARIZONA_MICD_BIAS_SRC_SHIFT);
216 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
217 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
219 info->micd_mode = mode;
221 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
224 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
226 switch (info->micd_modes[0].bias) {
238 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
240 struct arizona *arizona = info->arizona;
241 const char *widget = arizona_extcon_get_micbias(info);
242 struct snd_soc_dapm_context *dapm = arizona->dapm;
245 ret = snd_soc_dapm_force_enable_pin(dapm, widget);
247 dev_warn(arizona->dev, "Failed to enable %s: %d\n",
250 snd_soc_dapm_sync(dapm);
252 if (!arizona->pdata.micd_force_micbias) {
253 ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
255 dev_warn(arizona->dev, "Failed to disable %s: %d\n",
258 snd_soc_dapm_sync(dapm);
262 static void arizona_start_mic(struct arizona_extcon_info *info)
264 struct arizona *arizona = info->arizona;
268 /* Microphone detection can't use idle mode */
269 pm_runtime_get(info->dev);
271 if (info->detecting) {
272 ret = regulator_allow_bypass(info->micvdd, false);
274 dev_err(arizona->dev,
275 "Failed to regulate MICVDD: %d\n",
280 ret = regulator_enable(info->micvdd);
282 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
286 if (info->micd_reva) {
287 regmap_write(arizona->regmap, 0x80, 0x3);
288 regmap_write(arizona->regmap, 0x294, 0);
289 regmap_write(arizona->regmap, 0x80, 0x0);
292 regmap_update_bits(arizona->regmap,
293 ARIZONA_ACCESSORY_DETECT_MODE_1,
294 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
296 arizona_extcon_pulse_micbias(info);
298 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
299 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
302 regulator_disable(info->micvdd);
303 pm_runtime_put_autosuspend(info->dev);
307 static void arizona_stop_mic(struct arizona_extcon_info *info)
309 struct arizona *arizona = info->arizona;
310 const char *widget = arizona_extcon_get_micbias(info);
311 struct snd_soc_dapm_context *dapm = arizona->dapm;
315 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
319 ret = snd_soc_dapm_disable_pin(dapm, widget);
321 dev_warn(arizona->dev,
322 "Failed to disable %s: %d\n",
325 snd_soc_dapm_sync(dapm);
327 if (info->micd_reva) {
328 regmap_write(arizona->regmap, 0x80, 0x3);
329 regmap_write(arizona->regmap, 0x294, 2);
330 regmap_write(arizona->regmap, 0x80, 0x0);
333 ret = regulator_allow_bypass(info->micvdd, true);
335 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
340 regulator_disable(info->micvdd);
341 pm_runtime_mark_last_busy(info->dev);
342 pm_runtime_put_autosuspend(info->dev);
347 unsigned int threshold;
348 unsigned int factor_a;
349 unsigned int factor_b;
350 } arizona_hpdet_b_ranges[] = {
351 { 100, 5528, 362464 },
352 { 169, 11084, 6186851 },
353 { 169, 11065, 65460395 },
356 #define ARIZONA_HPDET_B_RANGE_MAX 0x3fb
361 } arizona_hpdet_c_ranges[] = {
368 static int arizona_hpdet_read(struct arizona_extcon_info *info)
370 struct arizona *arizona = info->arizona;
371 unsigned int val, range;
374 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
376 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
381 switch (info->hpdet_ip_version) {
383 if (!(val & ARIZONA_HP_DONE)) {
384 dev_err(arizona->dev, "HPDET did not complete: %x\n",
389 val &= ARIZONA_HP_LVL_MASK;
393 if (!(val & ARIZONA_HP_DONE_B)) {
394 dev_err(arizona->dev, "HPDET did not complete: %x\n",
399 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
401 dev_err(arizona->dev, "Failed to read HP value: %d\n",
406 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
408 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
409 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
411 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
412 (val < arizona_hpdet_b_ranges[range].threshold ||
413 val >= ARIZONA_HPDET_B_RANGE_MAX)) {
415 dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
417 regmap_update_bits(arizona->regmap,
418 ARIZONA_HEADPHONE_DETECT_1,
419 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
421 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
425 /* If we go out of range report top of range */
426 if (val < arizona_hpdet_b_ranges[range].threshold ||
427 val >= ARIZONA_HPDET_B_RANGE_MAX) {
428 dev_dbg(arizona->dev, "Measurement out of range\n");
429 return ARIZONA_HPDET_MAX;
432 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
435 val = arizona_hpdet_b_ranges[range].factor_b
437 arizona_hpdet_b_ranges[range].factor_a);
441 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
442 info->hpdet_ip_version);
444 if (!(val & ARIZONA_HP_DONE_B)) {
445 dev_err(arizona->dev, "HPDET did not complete: %x\n",
450 val &= ARIZONA_HP_LVL_B_MASK;
451 /* Convert to ohms, the value is in 0.5 ohm increments */
454 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
456 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
457 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
459 /* Skip up a range, or report? */
460 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
461 (val >= arizona_hpdet_c_ranges[range].max)) {
463 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
464 arizona_hpdet_c_ranges[range].min,
465 arizona_hpdet_c_ranges[range].max);
466 regmap_update_bits(arizona->regmap,
467 ARIZONA_HEADPHONE_DETECT_1,
468 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
470 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
474 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
475 dev_dbg(arizona->dev, "Reporting range boundary %d\n",
476 arizona_hpdet_c_ranges[range].min);
477 val = arizona_hpdet_c_ranges[range].min;
481 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
485 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading,
488 struct arizona *arizona = info->arizona;
489 int id_gpio = arizona->pdata.hpdet_id_gpio;
492 * If we're using HPDET for accessory identification we need
493 * to take multiple measurements, step through them in sequence.
495 if (arizona->pdata.hpdet_acc_id) {
496 info->hpdet_res[info->num_hpdet_res++] = *reading;
498 /* Only check the mic directly if we didn't already ID it */
499 if (id_gpio && info->num_hpdet_res == 1) {
500 dev_dbg(arizona->dev, "Measuring mic\n");
502 regmap_update_bits(arizona->regmap,
503 ARIZONA_ACCESSORY_DETECT_MODE_1,
504 ARIZONA_ACCDET_MODE_MASK |
506 ARIZONA_ACCDET_MODE_HPR |
507 info->micd_modes[0].src);
509 gpio_set_value_cansleep(id_gpio, 1);
511 regmap_update_bits(arizona->regmap,
512 ARIZONA_HEADPHONE_DETECT_1,
513 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
517 /* OK, got both. Now, compare... */
518 dev_dbg(arizona->dev, "HPDET measured %d %d\n",
519 info->hpdet_res[0], info->hpdet_res[1]);
521 /* Take the headphone impedance for the main report */
522 *reading = info->hpdet_res[0];
524 /* Sometimes we get false readings due to slow insert */
525 if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
526 dev_dbg(arizona->dev, "Retrying high impedance\n");
527 info->num_hpdet_res = 0;
528 info->hpdet_retried = true;
529 arizona_start_hpdet_acc_id(info);
530 pm_runtime_put(info->dev);
535 * If we measure the mic as high impedance
537 if (!id_gpio || info->hpdet_res[1] > 50) {
538 dev_dbg(arizona->dev, "Detected mic\n");
540 info->detecting = true;
542 dev_dbg(arizona->dev, "Detected headphone\n");
545 /* Make sure everything is reset back to the real polarity */
546 regmap_update_bits(arizona->regmap,
547 ARIZONA_ACCESSORY_DETECT_MODE_1,
549 info->micd_modes[0].src);
555 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
557 struct arizona_extcon_info *info = data;
558 struct arizona *arizona = info->arizona;
559 int id_gpio = arizona->pdata.hpdet_id_gpio;
560 int report = ARIZONA_CABLE_HEADPHONE;
564 mutex_lock(&info->lock);
566 /* If we got a spurious IRQ for some reason then ignore it */
567 if (!info->hpdet_active) {
568 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
569 mutex_unlock(&info->lock);
573 /* If the cable was removed while measuring ignore the result */
574 ret = extcon_get_cable_state_(info->edev, ARIZONA_CABLE_MECHANICAL);
576 dev_err(arizona->dev, "Failed to check cable state: %d\n",
580 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
584 ret = arizona_hpdet_read(info);
591 /* Reset back to starting range */
592 regmap_update_bits(arizona->regmap,
593 ARIZONA_HEADPHONE_DETECT_1,
594 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
597 ret = arizona_hpdet_do_id(info, &reading, &mic);
603 /* Report high impedence cables as line outputs */
605 report = ARIZONA_CABLE_LINEOUT;
607 report = ARIZONA_CABLE_HEADPHONE;
609 ret = extcon_set_cable_state_(info->edev, report, true);
611 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
615 /* Reset back to starting range */
616 regmap_update_bits(arizona->regmap,
617 ARIZONA_HEADPHONE_DETECT_1,
618 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
621 arizona_extcon_hp_clamp(info, false);
624 gpio_set_value_cansleep(id_gpio, 0);
626 /* Revert back to MICDET mode */
627 regmap_update_bits(arizona->regmap,
628 ARIZONA_ACCESSORY_DETECT_MODE_1,
629 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
631 /* If we have a mic then reenable MICDET */
632 if (mic || info->mic)
633 arizona_start_mic(info);
635 if (info->hpdet_active) {
636 pm_runtime_put_autosuspend(info->dev);
637 info->hpdet_active = false;
640 info->hpdet_done = true;
643 mutex_unlock(&info->lock);
648 static void arizona_identify_headphone(struct arizona_extcon_info *info)
650 struct arizona *arizona = info->arizona;
653 if (info->hpdet_done)
656 dev_dbg(arizona->dev, "Starting HPDET\n");
658 /* Make sure we keep the device enabled during the measurement */
659 pm_runtime_get(info->dev);
661 info->hpdet_active = true;
664 arizona_stop_mic(info);
666 arizona_extcon_hp_clamp(info, true);
668 ret = regmap_update_bits(arizona->regmap,
669 ARIZONA_ACCESSORY_DETECT_MODE_1,
670 ARIZONA_ACCDET_MODE_MASK,
671 arizona->pdata.hpdet_channel);
673 dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
677 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
678 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
680 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
688 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
689 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
691 /* Just report headphone */
692 ret = extcon_set_cable_state_(info->edev,
693 ARIZONA_CABLE_HEADPHONE, true);
695 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
698 arizona_start_mic(info);
700 info->hpdet_active = false;
703 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
705 struct arizona *arizona = info->arizona;
710 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
712 /* Make sure we keep the device enabled during the measurement */
713 pm_runtime_get_sync(info->dev);
715 info->hpdet_active = true;
717 arizona_extcon_hp_clamp(info, true);
719 ret = regmap_update_bits(arizona->regmap,
720 ARIZONA_ACCESSORY_DETECT_MODE_1,
721 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
722 info->micd_modes[0].src |
723 arizona->pdata.hpdet_channel);
725 dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
729 if (arizona->pdata.hpdet_acc_id_line) {
730 ret = regmap_update_bits(arizona->regmap,
731 ARIZONA_HEADPHONE_DETECT_1,
732 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
734 dev_err(arizona->dev,
735 "Can't start HPDETL measurement: %d\n",
740 arizona_hpdet_do_id(info, &hp_reading, &mic);
746 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
747 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
749 /* Just report headphone */
750 ret = extcon_set_cable_state_(info->edev,
751 ARIZONA_CABLE_HEADPHONE, true);
753 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
755 info->hpdet_active = false;
758 static void arizona_micd_timeout_work(struct work_struct *work)
760 struct arizona_extcon_info *info = container_of(work,
761 struct arizona_extcon_info,
762 micd_timeout_work.work);
764 mutex_lock(&info->lock);
766 dev_dbg(info->arizona->dev, "MICD timed out, reporting HP\n");
767 arizona_identify_headphone(info);
769 info->detecting = false;
771 arizona_stop_mic(info);
773 mutex_unlock(&info->lock);
776 static void arizona_micd_detect(struct work_struct *work)
778 struct arizona_extcon_info *info = container_of(work,
779 struct arizona_extcon_info,
780 micd_detect_work.work);
781 struct arizona *arizona = info->arizona;
782 unsigned int val = 0, lvl;
785 cancel_delayed_work_sync(&info->micd_timeout_work);
787 mutex_lock(&info->lock);
789 /* If the cable was removed while measuring ignore the result */
790 ret = extcon_get_cable_state_(info->edev, ARIZONA_CABLE_MECHANICAL);
792 dev_err(arizona->dev, "Failed to check cable state: %d\n",
794 mutex_unlock(&info->lock);
797 dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n");
798 mutex_unlock(&info->lock);
802 for (i = 0; i < 10 && !(val & MICD_LVL_0_TO_8); i++) {
803 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
805 dev_err(arizona->dev,
806 "Failed to read MICDET: %d\n", ret);
807 mutex_unlock(&info->lock);
811 dev_dbg(arizona->dev, "MICDET: %x\n", val);
813 if (!(val & ARIZONA_MICD_VALID)) {
814 dev_warn(arizona->dev,
815 "Microphone detection state invalid\n");
816 mutex_unlock(&info->lock);
821 if (i == 10 && !(val & MICD_LVL_0_TO_8)) {
822 dev_err(arizona->dev, "Failed to get valid MICDET value\n");
823 mutex_unlock(&info->lock);
827 /* Due to jack detect this should never happen */
828 if (!(val & ARIZONA_MICD_STS)) {
829 dev_warn(arizona->dev, "Detected open circuit\n");
830 info->detecting = false;
834 /* If we got a high impedence we should have a headset, report it. */
835 if (info->detecting && (val & ARIZONA_MICD_LVL_8)) {
836 arizona_identify_headphone(info);
838 ret = extcon_set_cable_state_(info->edev,
839 ARIZONA_CABLE_MICROPHONE, true);
842 dev_err(arizona->dev, "Headset report failed: %d\n",
845 /* Don't need to regulate for button detection */
846 ret = regulator_allow_bypass(info->micvdd, true);
848 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
853 info->detecting = false;
857 /* If we detected a lower impedence during initial startup
858 * then we probably have the wrong polarity, flip it. Don't
859 * do this for the lowest impedences to speed up detection of
860 * plain headphones. If both polarities report a low
861 * impedence then give up and report headphones.
863 if (info->detecting && (val & MICD_LVL_1_TO_7)) {
864 if (info->jack_flips >= info->micd_num_modes * 10) {
865 dev_dbg(arizona->dev, "Detected HP/line\n");
866 arizona_identify_headphone(info);
868 info->detecting = false;
870 arizona_stop_mic(info);
873 if (info->micd_mode == info->micd_num_modes)
875 arizona_extcon_set_mode(info, info->micd_mode);
884 * If we're still detecting and we detect a short then we've
885 * got a headphone. Otherwise it's a button press.
887 if (val & MICD_LVL_0_TO_7) {
889 dev_dbg(arizona->dev, "Mic button detected\n");
891 lvl = val & ARIZONA_MICD_LVL_MASK;
892 lvl >>= ARIZONA_MICD_LVL_SHIFT;
894 for (i = 0; i < info->num_micd_ranges; i++)
895 input_report_key(info->input,
896 info->micd_ranges[i].key, 0);
899 WARN_ON(ffs(lvl) - 1 >= info->num_micd_ranges);
900 if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
901 key = info->micd_ranges[ffs(lvl) - 1].key;
902 input_report_key(info->input, key, 1);
903 input_sync(info->input);
906 } else if (info->detecting) {
907 dev_dbg(arizona->dev, "Headphone detected\n");
908 info->detecting = false;
909 arizona_stop_mic(info);
911 arizona_identify_headphone(info);
913 dev_warn(arizona->dev, "Button with no mic: %x\n",
917 dev_dbg(arizona->dev, "Mic button released\n");
918 for (i = 0; i < info->num_micd_ranges; i++)
919 input_report_key(info->input,
920 info->micd_ranges[i].key, 0);
921 input_sync(info->input);
922 arizona_extcon_pulse_micbias(info);
927 queue_delayed_work(system_power_efficient_wq,
928 &info->micd_timeout_work,
929 msecs_to_jiffies(info->micd_timeout));
931 pm_runtime_mark_last_busy(info->dev);
932 mutex_unlock(&info->lock);
935 static irqreturn_t arizona_micdet(int irq, void *data)
937 struct arizona_extcon_info *info = data;
938 struct arizona *arizona = info->arizona;
939 int debounce = arizona->pdata.micd_detect_debounce;
941 cancel_delayed_work_sync(&info->micd_detect_work);
942 cancel_delayed_work_sync(&info->micd_timeout_work);
944 mutex_lock(&info->lock);
945 if (!info->detecting)
947 mutex_unlock(&info->lock);
950 queue_delayed_work(system_power_efficient_wq,
951 &info->micd_detect_work,
952 msecs_to_jiffies(debounce));
954 arizona_micd_detect(&info->micd_detect_work.work);
959 static void arizona_hpdet_work(struct work_struct *work)
961 struct arizona_extcon_info *info = container_of(work,
962 struct arizona_extcon_info,
965 mutex_lock(&info->lock);
966 arizona_start_hpdet_acc_id(info);
967 mutex_unlock(&info->lock);
970 static irqreturn_t arizona_jackdet(int irq, void *data)
972 struct arizona_extcon_info *info = data;
973 struct arizona *arizona = info->arizona;
974 unsigned int val, present, mask;
975 bool cancelled_hp, cancelled_mic;
978 cancelled_hp = cancel_delayed_work_sync(&info->hpdet_work);
979 cancelled_mic = cancel_delayed_work_sync(&info->micd_timeout_work);
981 pm_runtime_get_sync(info->dev);
983 mutex_lock(&info->lock);
985 if (arizona->pdata.jd_gpio5) {
986 mask = ARIZONA_MICD_CLAMP_STS;
987 if (arizona->pdata.jd_invert)
988 present = ARIZONA_MICD_CLAMP_STS;
992 mask = ARIZONA_JD1_STS;
993 if (arizona->pdata.jd_invert)
996 present = ARIZONA_JD1_STS;
999 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
1001 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
1003 mutex_unlock(&info->lock);
1004 pm_runtime_put_autosuspend(info->dev);
1009 if (val == info->last_jackdet) {
1010 dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
1012 queue_delayed_work(system_power_efficient_wq,
1014 msecs_to_jiffies(HPDET_DEBOUNCE));
1016 if (cancelled_mic) {
1017 int micd_timeout = info->micd_timeout;
1019 queue_delayed_work(system_power_efficient_wq,
1020 &info->micd_timeout_work,
1021 msecs_to_jiffies(micd_timeout));
1026 info->last_jackdet = val;
1028 if (info->last_jackdet == present) {
1029 dev_dbg(arizona->dev, "Detected jack\n");
1030 ret = extcon_set_cable_state_(info->edev,
1031 ARIZONA_CABLE_MECHANICAL, true);
1034 dev_err(arizona->dev, "Mechanical report failed: %d\n",
1037 if (!arizona->pdata.hpdet_acc_id) {
1038 info->detecting = true;
1040 info->jack_flips = 0;
1042 arizona_start_mic(info);
1044 queue_delayed_work(system_power_efficient_wq,
1046 msecs_to_jiffies(HPDET_DEBOUNCE));
1049 regmap_update_bits(arizona->regmap,
1050 ARIZONA_JACK_DETECT_DEBOUNCE,
1051 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
1053 dev_dbg(arizona->dev, "Detected jack removal\n");
1055 arizona_stop_mic(info);
1057 info->num_hpdet_res = 0;
1058 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
1059 info->hpdet_res[i] = 0;
1061 info->hpdet_done = false;
1062 info->hpdet_retried = false;
1064 for (i = 0; i < info->num_micd_ranges; i++)
1065 input_report_key(info->input,
1066 info->micd_ranges[i].key, 0);
1067 input_sync(info->input);
1069 ret = extcon_update_state(info->edev, 0xffffffff, 0);
1071 dev_err(arizona->dev, "Removal report failed: %d\n",
1074 regmap_update_bits(arizona->regmap,
1075 ARIZONA_JACK_DETECT_DEBOUNCE,
1076 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
1077 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
1080 if (arizona->pdata.micd_timeout)
1081 info->micd_timeout = arizona->pdata.micd_timeout;
1083 info->micd_timeout = DEFAULT_MICD_TIMEOUT;
1086 /* Clear trig_sts to make sure DCVDD is not forced up */
1087 regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
1088 ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
1089 ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
1090 ARIZONA_JD1_FALL_TRIG_STS |
1091 ARIZONA_JD1_RISE_TRIG_STS);
1093 mutex_unlock(&info->lock);
1095 pm_runtime_mark_last_busy(info->dev);
1096 pm_runtime_put_autosuspend(info->dev);
1101 /* Map a level onto a slot in the register bank */
1102 static void arizona_micd_set_level(struct arizona *arizona, int index,
1108 reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
1117 /* Program the level itself */
1118 regmap_update_bits(arizona->regmap, reg, mask, level);
1121 static int arizona_extcon_of_get_pdata(struct arizona *arizona)
1123 struct arizona_pdata *pdata = &arizona->pdata;
1124 unsigned int val = ARIZONA_ACCDET_MODE_HPL;
1126 of_property_read_u32(arizona->dev->of_node, "wlf,hpdet-channel", &val);
1128 case ARIZONA_ACCDET_MODE_HPL:
1129 case ARIZONA_ACCDET_MODE_HPR:
1130 pdata->hpdet_channel = val;
1133 dev_err(arizona->dev,
1134 "Wrong wlf,hpdet-channel DT value %d\n", val);
1135 pdata->hpdet_channel = ARIZONA_ACCDET_MODE_HPL;
1141 static int arizona_extcon_probe(struct platform_device *pdev)
1143 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
1144 struct arizona_pdata *pdata = &arizona->pdata;
1145 struct arizona_extcon_info *info;
1147 unsigned int clamp_mode;
1148 int jack_irq_fall, jack_irq_rise;
1149 int ret, mode, i, j;
1151 if (!arizona->dapm || !arizona->dapm->card)
1152 return -EPROBE_DEFER;
1154 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1158 if (IS_ENABLED(CONFIG_OF)) {
1159 if (!dev_get_platdata(arizona->dev))
1160 arizona_extcon_of_get_pdata(arizona);
1163 info->micvdd = devm_regulator_get(&pdev->dev, "MICVDD");
1164 if (IS_ERR(info->micvdd)) {
1165 ret = PTR_ERR(info->micvdd);
1166 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
1170 mutex_init(&info->lock);
1171 info->arizona = arizona;
1172 info->dev = &pdev->dev;
1173 info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
1174 INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
1175 INIT_DELAYED_WORK(&info->micd_detect_work, arizona_micd_detect);
1176 INIT_DELAYED_WORK(&info->micd_timeout_work, arizona_micd_timeout_work);
1177 platform_set_drvdata(pdev, info);
1179 switch (arizona->type) {
1181 switch (arizona->rev) {
1183 info->micd_reva = true;
1186 info->micd_clamp = true;
1187 info->hpdet_ip_version = 1;
1193 switch (arizona->rev) {
1197 info->micd_clamp = true;
1198 info->hpdet_ip_version = 2;
1206 info->edev = devm_extcon_dev_allocate(&pdev->dev, arizona_cable);
1207 if (IS_ERR(info->edev)) {
1208 dev_err(&pdev->dev, "failed to allocate extcon device\n");
1212 ret = devm_extcon_dev_register(&pdev->dev, info->edev);
1214 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
1219 info->input = devm_input_allocate_device(&pdev->dev);
1221 dev_err(arizona->dev, "Can't allocate input dev\n");
1226 info->input->name = "Headset";
1227 info->input->phys = "arizona/extcon";
1229 if (pdata->num_micd_configs) {
1230 info->micd_modes = pdata->micd_configs;
1231 info->micd_num_modes = pdata->num_micd_configs;
1233 info->micd_modes = micd_default_modes;
1234 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1237 if (arizona->pdata.micd_pol_gpio > 0) {
1238 if (info->micd_modes[0].gpio)
1239 mode = GPIOF_OUT_INIT_HIGH;
1241 mode = GPIOF_OUT_INIT_LOW;
1243 ret = devm_gpio_request_one(&pdev->dev,
1244 arizona->pdata.micd_pol_gpio,
1248 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1249 arizona->pdata.micd_pol_gpio, ret);
1254 if (arizona->pdata.hpdet_id_gpio > 0) {
1255 ret = devm_gpio_request_one(&pdev->dev,
1256 arizona->pdata.hpdet_id_gpio,
1260 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1261 arizona->pdata.hpdet_id_gpio, ret);
1266 if (arizona->pdata.micd_bias_start_time)
1267 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1268 ARIZONA_MICD_BIAS_STARTTIME_MASK,
1269 arizona->pdata.micd_bias_start_time
1270 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1272 if (arizona->pdata.micd_rate)
1273 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1274 ARIZONA_MICD_RATE_MASK,
1275 arizona->pdata.micd_rate
1276 << ARIZONA_MICD_RATE_SHIFT);
1278 if (arizona->pdata.micd_dbtime)
1279 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1280 ARIZONA_MICD_DBTIME_MASK,
1281 arizona->pdata.micd_dbtime
1282 << ARIZONA_MICD_DBTIME_SHIFT);
1284 BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) != 0x40);
1286 if (arizona->pdata.num_micd_ranges) {
1287 info->micd_ranges = pdata->micd_ranges;
1288 info->num_micd_ranges = pdata->num_micd_ranges;
1290 info->micd_ranges = micd_default_ranges;
1291 info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
1294 if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_RANGE) {
1295 dev_err(arizona->dev, "Too many MICD ranges: %d\n",
1296 arizona->pdata.num_micd_ranges);
1299 if (info->num_micd_ranges > 1) {
1300 for (i = 1; i < info->num_micd_ranges; i++) {
1301 if (info->micd_ranges[i - 1].max >
1302 info->micd_ranges[i].max) {
1303 dev_err(arizona->dev,
1304 "MICD ranges must be sorted\n");
1311 /* Disable all buttons by default */
1312 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1313 ARIZONA_MICD_LVL_SEL_MASK, 0x81);
1315 /* Set up all the buttons the user specified */
1316 for (i = 0; i < info->num_micd_ranges; i++) {
1317 for (j = 0; j < ARRAY_SIZE(arizona_micd_levels); j++)
1318 if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
1321 if (j == ARRAY_SIZE(arizona_micd_levels)) {
1322 dev_err(arizona->dev, "Unsupported MICD level %d\n",
1323 info->micd_ranges[i].max);
1328 dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
1329 arizona_micd_levels[j], i);
1331 arizona_micd_set_level(arizona, i, j);
1332 input_set_capability(info->input, EV_KEY,
1333 info->micd_ranges[i].key);
1335 /* Enable reporting of that range */
1336 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1340 /* Set all the remaining keys to a maximum */
1341 for (; i < ARIZONA_MAX_MICD_RANGE; i++)
1342 arizona_micd_set_level(arizona, i, 0x3f);
1345 * If we have a clamp use it, activating in conjunction with
1346 * GPIO5 if that is connected for jack detect operation.
1348 if (info->micd_clamp) {
1349 if (arizona->pdata.jd_gpio5) {
1350 /* Put the GPIO into input mode with optional pull */
1352 if (arizona->pdata.jd_gpio5_nopull)
1353 val &= ~ARIZONA_GPN_PU;
1355 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1358 if (arizona->pdata.jd_invert)
1359 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH_GP5H;
1361 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL_GP5H;
1363 if (arizona->pdata.jd_invert)
1364 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH;
1366 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL;
1369 regmap_update_bits(arizona->regmap,
1370 ARIZONA_MICD_CLAMP_CONTROL,
1371 ARIZONA_MICD_CLAMP_MODE_MASK, clamp_mode);
1373 regmap_update_bits(arizona->regmap,
1374 ARIZONA_JACK_DETECT_DEBOUNCE,
1375 ARIZONA_MICD_CLAMP_DB,
1376 ARIZONA_MICD_CLAMP_DB);
1379 arizona_extcon_set_mode(info, 0);
1381 pm_runtime_enable(&pdev->dev);
1382 pm_runtime_idle(&pdev->dev);
1383 pm_runtime_get_sync(&pdev->dev);
1385 if (arizona->pdata.jd_gpio5) {
1386 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1387 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1389 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1390 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1393 ret = arizona_request_irq(arizona, jack_irq_rise,
1394 "JACKDET rise", arizona_jackdet, info);
1396 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1401 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1403 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1408 ret = arizona_request_irq(arizona, jack_irq_fall,
1409 "JACKDET fall", arizona_jackdet, info);
1411 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1415 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1417 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1422 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1423 "MICDET", arizona_micdet, info);
1425 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1429 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1430 "HPDET", arizona_hpdet_irq, info);
1432 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1436 arizona_clk32k_enable(arizona);
1437 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1438 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1439 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1440 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1442 ret = regulator_allow_bypass(info->micvdd, true);
1444 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1447 pm_runtime_put(&pdev->dev);
1449 ret = input_register_device(info->input);
1451 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1458 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1460 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1462 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1464 arizona_free_irq(arizona, jack_irq_fall, info);
1466 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1468 arizona_free_irq(arizona, jack_irq_rise, info);
1471 pm_runtime_disable(&pdev->dev);
1475 static int arizona_extcon_remove(struct platform_device *pdev)
1477 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1478 struct arizona *arizona = info->arizona;
1479 int jack_irq_rise, jack_irq_fall;
1481 pm_runtime_disable(&pdev->dev);
1483 regmap_update_bits(arizona->regmap,
1484 ARIZONA_MICD_CLAMP_CONTROL,
1485 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1487 if (arizona->pdata.jd_gpio5) {
1488 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1489 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1491 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1492 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1495 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1496 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1497 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1498 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1499 arizona_free_irq(arizona, jack_irq_rise, info);
1500 arizona_free_irq(arizona, jack_irq_fall, info);
1501 cancel_delayed_work_sync(&info->hpdet_work);
1502 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1503 ARIZONA_JD1_ENA, 0);
1504 arizona_clk32k_disable(arizona);
1509 static struct platform_driver arizona_extcon_driver = {
1511 .name = "arizona-extcon",
1513 .probe = arizona_extcon_probe,
1514 .remove = arizona_extcon_remove,
1517 module_platform_driver(arizona_extcon_driver);
1519 MODULE_DESCRIPTION("Arizona Extcon driver");
1520 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1521 MODULE_LICENSE("GPL");
1522 MODULE_ALIAS("platform:extcon-arizona");