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/consumer.h>
24 #include <linux/gpio.h>
25 #include <linux/input.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/property.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/extcon.h>
32 #include <sound/soc.h>
34 #include <linux/mfd/arizona/core.h>
35 #include <linux/mfd/arizona/pdata.h>
36 #include <linux/mfd/arizona/registers.h>
37 #include <dt-bindings/mfd/arizona.h>
39 #define ARIZONA_MAX_MICD_RANGE 8
41 #define ARIZONA_MICD_CLAMP_MODE_JDL 0x4
42 #define ARIZONA_MICD_CLAMP_MODE_JDH 0x5
43 #define ARIZONA_MICD_CLAMP_MODE_JDL_GP5H 0x9
44 #define ARIZONA_MICD_CLAMP_MODE_JDH_GP5H 0xb
46 #define ARIZONA_HPDET_MAX 10000
48 #define HPDET_DEBOUNCE 500
49 #define DEFAULT_MICD_TIMEOUT 2000
51 #define MICD_LVL_1_TO_7 (ARIZONA_MICD_LVL_1 | ARIZONA_MICD_LVL_2 | \
52 ARIZONA_MICD_LVL_3 | ARIZONA_MICD_LVL_4 | \
53 ARIZONA_MICD_LVL_5 | ARIZONA_MICD_LVL_6 | \
56 #define MICD_LVL_0_TO_7 (ARIZONA_MICD_LVL_0 | MICD_LVL_1_TO_7)
58 #define MICD_LVL_0_TO_8 (MICD_LVL_0_TO_7 | ARIZONA_MICD_LVL_8)
60 struct arizona_extcon_info {
62 struct arizona *arizona;
64 struct regulator *micvdd;
65 struct input_dev *input;
70 const struct arizona_micd_config *micd_modes;
73 const struct arizona_micd_range *micd_ranges;
81 struct delayed_work hpdet_work;
82 struct delayed_work micd_detect_work;
83 struct delayed_work micd_timeout_work;
90 unsigned int hpdet_res[3];
98 struct extcon_dev *edev;
100 struct gpio_desc *micd_pol_gpio;
103 static const struct arizona_micd_config micd_default_modes[] = {
104 { ARIZONA_ACCDET_SRC, 1, 0 },
108 static const struct arizona_micd_range micd_default_ranges[] = {
109 { .max = 11, .key = BTN_0 },
110 { .max = 28, .key = BTN_1 },
111 { .max = 54, .key = BTN_2 },
112 { .max = 100, .key = BTN_3 },
113 { .max = 186, .key = BTN_4 },
114 { .max = 430, .key = BTN_5 },
117 static const int arizona_micd_levels[] = {
118 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
119 49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
120 105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
121 270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
125 static const unsigned int arizona_cable[] = {
133 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info);
135 static void arizona_extcon_hp_clamp(struct arizona_extcon_info *info,
138 struct arizona *arizona = info->arizona;
139 unsigned int mask = 0, val = 0;
142 switch (arizona->type) {
145 mask = ARIZONA_HP1L_SHRTO | ARIZONA_HP1L_FLWR |
148 val = ARIZONA_HP1L_SHRTO;
150 val = ARIZONA_HP1L_FLWR | ARIZONA_HP1L_SHRTI;
153 mask = ARIZONA_RMV_SHRT_HP1L;
155 val = ARIZONA_RMV_SHRT_HP1L;
159 mutex_lock(&arizona->dapm->card->dapm_mutex);
161 arizona->hpdet_clamp = clamp;
163 /* Keep the HP output stages disabled while doing the clamp */
165 ret = regmap_update_bits(arizona->regmap,
166 ARIZONA_OUTPUT_ENABLES_1,
168 ARIZONA_OUT1R_ENA, 0);
170 dev_warn(arizona->dev,
171 "Failed to disable headphone outputs: %d\n",
175 ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1L,
178 dev_warn(arizona->dev, "Failed to do clamp: %d\n",
181 ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1R,
184 dev_warn(arizona->dev, "Failed to do clamp: %d\n",
187 /* Restore the desired state while not doing the clamp */
189 ret = regmap_update_bits(arizona->regmap,
190 ARIZONA_OUTPUT_ENABLES_1,
192 ARIZONA_OUT1R_ENA, arizona->hp_ena);
194 dev_warn(arizona->dev,
195 "Failed to restore headphone outputs: %d\n",
199 mutex_unlock(&arizona->dapm->card->dapm_mutex);
202 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
204 struct arizona *arizona = info->arizona;
206 mode %= info->micd_num_modes;
208 if (arizona->pdata.micd_pol_gpio > 0)
209 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
210 info->micd_modes[mode].gpio);
212 gpiod_set_value_cansleep(info->micd_pol_gpio,
213 info->micd_modes[mode].gpio);
215 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
216 ARIZONA_MICD_BIAS_SRC_MASK,
217 info->micd_modes[mode].bias <<
218 ARIZONA_MICD_BIAS_SRC_SHIFT);
219 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
220 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
222 info->micd_mode = mode;
224 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
227 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
229 switch (info->micd_modes[0].bias) {
241 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
243 struct arizona *arizona = info->arizona;
244 const char *widget = arizona_extcon_get_micbias(info);
245 struct snd_soc_dapm_context *dapm = arizona->dapm;
248 ret = snd_soc_dapm_force_enable_pin(dapm, widget);
250 dev_warn(arizona->dev, "Failed to enable %s: %d\n",
253 snd_soc_dapm_sync(dapm);
255 if (!arizona->pdata.micd_force_micbias) {
256 ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
258 dev_warn(arizona->dev, "Failed to disable %s: %d\n",
261 snd_soc_dapm_sync(dapm);
265 static void arizona_start_mic(struct arizona_extcon_info *info)
267 struct arizona *arizona = info->arizona;
271 /* Microphone detection can't use idle mode */
272 pm_runtime_get(info->dev);
274 if (info->detecting) {
275 ret = regulator_allow_bypass(info->micvdd, false);
277 dev_err(arizona->dev,
278 "Failed to regulate MICVDD: %d\n",
283 ret = regulator_enable(info->micvdd);
285 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
289 if (info->micd_reva) {
290 regmap_write(arizona->regmap, 0x80, 0x3);
291 regmap_write(arizona->regmap, 0x294, 0);
292 regmap_write(arizona->regmap, 0x80, 0x0);
295 regmap_update_bits(arizona->regmap,
296 ARIZONA_ACCESSORY_DETECT_MODE_1,
297 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
299 arizona_extcon_pulse_micbias(info);
301 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
302 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
305 regulator_disable(info->micvdd);
306 pm_runtime_put_autosuspend(info->dev);
310 static void arizona_stop_mic(struct arizona_extcon_info *info)
312 struct arizona *arizona = info->arizona;
313 const char *widget = arizona_extcon_get_micbias(info);
314 struct snd_soc_dapm_context *dapm = arizona->dapm;
318 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
322 ret = snd_soc_dapm_disable_pin(dapm, widget);
324 dev_warn(arizona->dev,
325 "Failed to disable %s: %d\n",
328 snd_soc_dapm_sync(dapm);
330 if (info->micd_reva) {
331 regmap_write(arizona->regmap, 0x80, 0x3);
332 regmap_write(arizona->regmap, 0x294, 2);
333 regmap_write(arizona->regmap, 0x80, 0x0);
336 ret = regulator_allow_bypass(info->micvdd, true);
338 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
343 regulator_disable(info->micvdd);
344 pm_runtime_mark_last_busy(info->dev);
345 pm_runtime_put_autosuspend(info->dev);
350 unsigned int threshold;
351 unsigned int factor_a;
352 unsigned int factor_b;
353 } arizona_hpdet_b_ranges[] = {
354 { 100, 5528, 362464 },
355 { 169, 11084, 6186851 },
356 { 169, 11065, 65460395 },
359 #define ARIZONA_HPDET_B_RANGE_MAX 0x3fb
364 } arizona_hpdet_c_ranges[] = {
371 static int arizona_hpdet_read(struct arizona_extcon_info *info)
373 struct arizona *arizona = info->arizona;
374 unsigned int val, range;
377 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
379 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
384 switch (info->hpdet_ip_version) {
386 if (!(val & ARIZONA_HP_DONE)) {
387 dev_err(arizona->dev, "HPDET did not complete: %x\n",
392 val &= ARIZONA_HP_LVL_MASK;
396 if (!(val & ARIZONA_HP_DONE_B)) {
397 dev_err(arizona->dev, "HPDET did not complete: %x\n",
402 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
404 dev_err(arizona->dev, "Failed to read HP value: %d\n",
409 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
411 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
412 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
414 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
415 (val < arizona_hpdet_b_ranges[range].threshold ||
416 val >= ARIZONA_HPDET_B_RANGE_MAX)) {
418 dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
420 regmap_update_bits(arizona->regmap,
421 ARIZONA_HEADPHONE_DETECT_1,
422 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
424 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
428 /* If we go out of range report top of range */
429 if (val < arizona_hpdet_b_ranges[range].threshold ||
430 val >= ARIZONA_HPDET_B_RANGE_MAX) {
431 dev_dbg(arizona->dev, "Measurement out of range\n");
432 return ARIZONA_HPDET_MAX;
435 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
438 val = arizona_hpdet_b_ranges[range].factor_b
440 arizona_hpdet_b_ranges[range].factor_a);
444 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
445 info->hpdet_ip_version);
447 if (!(val & ARIZONA_HP_DONE_B)) {
448 dev_err(arizona->dev, "HPDET did not complete: %x\n",
453 val &= ARIZONA_HP_LVL_B_MASK;
454 /* Convert to ohms, the value is in 0.5 ohm increments */
457 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
459 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
460 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
462 /* Skip up a range, or report? */
463 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
464 (val >= arizona_hpdet_c_ranges[range].max)) {
466 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
467 arizona_hpdet_c_ranges[range].min,
468 arizona_hpdet_c_ranges[range].max);
469 regmap_update_bits(arizona->regmap,
470 ARIZONA_HEADPHONE_DETECT_1,
471 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
473 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
477 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
478 dev_dbg(arizona->dev, "Reporting range boundary %d\n",
479 arizona_hpdet_c_ranges[range].min);
480 val = arizona_hpdet_c_ranges[range].min;
484 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
488 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading,
491 struct arizona *arizona = info->arizona;
492 int id_gpio = arizona->pdata.hpdet_id_gpio;
495 * If we're using HPDET for accessory identification we need
496 * to take multiple measurements, step through them in sequence.
498 if (arizona->pdata.hpdet_acc_id) {
499 info->hpdet_res[info->num_hpdet_res++] = *reading;
501 /* Only check the mic directly if we didn't already ID it */
502 if (id_gpio && info->num_hpdet_res == 1) {
503 dev_dbg(arizona->dev, "Measuring mic\n");
505 regmap_update_bits(arizona->regmap,
506 ARIZONA_ACCESSORY_DETECT_MODE_1,
507 ARIZONA_ACCDET_MODE_MASK |
509 ARIZONA_ACCDET_MODE_HPR |
510 info->micd_modes[0].src);
512 gpio_set_value_cansleep(id_gpio, 1);
514 regmap_update_bits(arizona->regmap,
515 ARIZONA_HEADPHONE_DETECT_1,
516 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
520 /* OK, got both. Now, compare... */
521 dev_dbg(arizona->dev, "HPDET measured %d %d\n",
522 info->hpdet_res[0], info->hpdet_res[1]);
524 /* Take the headphone impedance for the main report */
525 *reading = info->hpdet_res[0];
527 /* Sometimes we get false readings due to slow insert */
528 if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
529 dev_dbg(arizona->dev, "Retrying high impedance\n");
530 info->num_hpdet_res = 0;
531 info->hpdet_retried = true;
532 arizona_start_hpdet_acc_id(info);
533 pm_runtime_put(info->dev);
538 * If we measure the mic as high impedance
540 if (!id_gpio || info->hpdet_res[1] > 50) {
541 dev_dbg(arizona->dev, "Detected mic\n");
543 info->detecting = true;
545 dev_dbg(arizona->dev, "Detected headphone\n");
548 /* Make sure everything is reset back to the real polarity */
549 regmap_update_bits(arizona->regmap,
550 ARIZONA_ACCESSORY_DETECT_MODE_1,
552 info->micd_modes[0].src);
558 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
560 struct arizona_extcon_info *info = data;
561 struct arizona *arizona = info->arizona;
562 int id_gpio = arizona->pdata.hpdet_id_gpio;
563 unsigned int report = EXTCON_HEADPHONE;
567 mutex_lock(&info->lock);
569 /* If we got a spurious IRQ for some reason then ignore it */
570 if (!info->hpdet_active) {
571 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
572 mutex_unlock(&info->lock);
576 /* If the cable was removed while measuring ignore the result */
577 ret = extcon_get_cable_state_(info->edev, EXTCON_MECHANICAL);
579 dev_err(arizona->dev, "Failed to check cable state: %d\n",
583 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
587 ret = arizona_hpdet_read(info);
594 /* Reset back to starting range */
595 regmap_update_bits(arizona->regmap,
596 ARIZONA_HEADPHONE_DETECT_1,
597 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
600 ret = arizona_hpdet_do_id(info, &reading, &mic);
606 /* Report high impedence cables as line outputs */
608 report = EXTCON_LINE_OUT;
610 report = EXTCON_HEADPHONE;
612 ret = extcon_set_cable_state_(info->edev, report, true);
614 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
618 /* Reset back to starting range */
619 regmap_update_bits(arizona->regmap,
620 ARIZONA_HEADPHONE_DETECT_1,
621 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
624 arizona_extcon_hp_clamp(info, false);
627 gpio_set_value_cansleep(id_gpio, 0);
629 /* Revert back to MICDET mode */
630 regmap_update_bits(arizona->regmap,
631 ARIZONA_ACCESSORY_DETECT_MODE_1,
632 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
634 /* If we have a mic then reenable MICDET */
635 if (mic || info->mic)
636 arizona_start_mic(info);
638 if (info->hpdet_active) {
639 pm_runtime_put_autosuspend(info->dev);
640 info->hpdet_active = false;
643 info->hpdet_done = true;
646 mutex_unlock(&info->lock);
651 static void arizona_identify_headphone(struct arizona_extcon_info *info)
653 struct arizona *arizona = info->arizona;
656 if (info->hpdet_done)
659 dev_dbg(arizona->dev, "Starting HPDET\n");
661 /* Make sure we keep the device enabled during the measurement */
662 pm_runtime_get(info->dev);
664 info->hpdet_active = true;
667 arizona_stop_mic(info);
669 arizona_extcon_hp_clamp(info, true);
671 ret = regmap_update_bits(arizona->regmap,
672 ARIZONA_ACCESSORY_DETECT_MODE_1,
673 ARIZONA_ACCDET_MODE_MASK,
674 arizona->pdata.hpdet_channel);
676 dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
680 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
681 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
683 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
691 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
692 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
694 /* Just report headphone */
695 ret = extcon_set_cable_state_(info->edev, EXTCON_HEADPHONE, true);
697 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
700 arizona_start_mic(info);
702 info->hpdet_active = false;
705 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
707 struct arizona *arizona = info->arizona;
712 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
714 /* Make sure we keep the device enabled during the measurement */
715 pm_runtime_get_sync(info->dev);
717 info->hpdet_active = true;
719 arizona_extcon_hp_clamp(info, true);
721 ret = regmap_update_bits(arizona->regmap,
722 ARIZONA_ACCESSORY_DETECT_MODE_1,
723 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
724 info->micd_modes[0].src |
725 arizona->pdata.hpdet_channel);
727 dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
731 if (arizona->pdata.hpdet_acc_id_line) {
732 ret = regmap_update_bits(arizona->regmap,
733 ARIZONA_HEADPHONE_DETECT_1,
734 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
736 dev_err(arizona->dev,
737 "Can't start HPDETL measurement: %d\n",
742 arizona_hpdet_do_id(info, &hp_reading, &mic);
748 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
749 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
751 /* Just report headphone */
752 ret = extcon_set_cable_state_(info->edev, EXTCON_HEADPHONE, true);
754 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
756 info->hpdet_active = false;
759 static void arizona_micd_timeout_work(struct work_struct *work)
761 struct arizona_extcon_info *info = container_of(work,
762 struct arizona_extcon_info,
763 micd_timeout_work.work);
765 mutex_lock(&info->lock);
767 dev_dbg(info->arizona->dev, "MICD timed out, reporting HP\n");
769 info->detecting = false;
771 arizona_identify_headphone(info);
773 arizona_stop_mic(info);
775 mutex_unlock(&info->lock);
778 static void arizona_micd_detect(struct work_struct *work)
780 struct arizona_extcon_info *info = container_of(work,
781 struct arizona_extcon_info,
782 micd_detect_work.work);
783 struct arizona *arizona = info->arizona;
784 unsigned int val = 0, lvl;
787 cancel_delayed_work_sync(&info->micd_timeout_work);
789 mutex_lock(&info->lock);
791 /* If the cable was removed while measuring ignore the result */
792 ret = extcon_get_cable_state_(info->edev, EXTCON_MECHANICAL);
794 dev_err(arizona->dev, "Failed to check cable state: %d\n",
796 mutex_unlock(&info->lock);
799 dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n");
800 mutex_unlock(&info->lock);
804 for (i = 0; i < 10 && !(val & MICD_LVL_0_TO_8); i++) {
805 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
807 dev_err(arizona->dev,
808 "Failed to read MICDET: %d\n", ret);
809 mutex_unlock(&info->lock);
813 dev_dbg(arizona->dev, "MICDET: %x\n", val);
815 if (!(val & ARIZONA_MICD_VALID)) {
816 dev_warn(arizona->dev,
817 "Microphone detection state invalid\n");
818 mutex_unlock(&info->lock);
823 if (i == 10 && !(val & MICD_LVL_0_TO_8)) {
824 dev_err(arizona->dev, "Failed to get valid MICDET value\n");
825 mutex_unlock(&info->lock);
829 /* Due to jack detect this should never happen */
830 if (!(val & ARIZONA_MICD_STS)) {
831 dev_warn(arizona->dev, "Detected open circuit\n");
832 info->detecting = false;
836 /* If we got a high impedence we should have a headset, report it. */
837 if (info->detecting && (val & ARIZONA_MICD_LVL_8)) {
839 info->detecting = false;
841 arizona_identify_headphone(info);
843 ret = extcon_set_cable_state_(info->edev,
844 EXTCON_MICROPHONE, true);
846 dev_err(arizona->dev, "Headset report failed: %d\n",
849 /* Don't need to regulate for button detection */
850 ret = regulator_allow_bypass(info->micvdd, true);
852 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
859 /* If we detected a lower impedence during initial startup
860 * then we probably have the wrong polarity, flip it. Don't
861 * do this for the lowest impedences to speed up detection of
862 * plain headphones. If both polarities report a low
863 * impedence then give up and report headphones.
865 if (info->detecting && (val & MICD_LVL_1_TO_7)) {
866 if (info->jack_flips >= info->micd_num_modes * 10) {
867 dev_dbg(arizona->dev, "Detected HP/line\n");
869 info->detecting = false;
871 arizona_identify_headphone(info);
873 arizona_stop_mic(info);
876 if (info->micd_mode == info->micd_num_modes)
878 arizona_extcon_set_mode(info, info->micd_mode);
887 * If we're still detecting and we detect a short then we've
888 * got a headphone. Otherwise it's a button press.
890 if (val & MICD_LVL_0_TO_7) {
892 dev_dbg(arizona->dev, "Mic button detected\n");
894 lvl = val & ARIZONA_MICD_LVL_MASK;
895 lvl >>= ARIZONA_MICD_LVL_SHIFT;
897 for (i = 0; i < info->num_micd_ranges; i++)
898 input_report_key(info->input,
899 info->micd_ranges[i].key, 0);
902 WARN_ON(ffs(lvl) - 1 >= info->num_micd_ranges);
903 if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
904 key = info->micd_ranges[ffs(lvl) - 1].key;
905 input_report_key(info->input, key, 1);
906 input_sync(info->input);
909 } else if (info->detecting) {
910 dev_dbg(arizona->dev, "Headphone detected\n");
911 info->detecting = false;
912 arizona_stop_mic(info);
914 arizona_identify_headphone(info);
916 dev_warn(arizona->dev, "Button with no mic: %x\n",
920 dev_dbg(arizona->dev, "Mic button released\n");
921 for (i = 0; i < info->num_micd_ranges; i++)
922 input_report_key(info->input,
923 info->micd_ranges[i].key, 0);
924 input_sync(info->input);
925 arizona_extcon_pulse_micbias(info);
930 queue_delayed_work(system_power_efficient_wq,
931 &info->micd_timeout_work,
932 msecs_to_jiffies(info->micd_timeout));
934 pm_runtime_mark_last_busy(info->dev);
935 mutex_unlock(&info->lock);
938 static irqreturn_t arizona_micdet(int irq, void *data)
940 struct arizona_extcon_info *info = data;
941 struct arizona *arizona = info->arizona;
942 int debounce = arizona->pdata.micd_detect_debounce;
944 cancel_delayed_work_sync(&info->micd_detect_work);
945 cancel_delayed_work_sync(&info->micd_timeout_work);
947 mutex_lock(&info->lock);
948 if (!info->detecting)
950 mutex_unlock(&info->lock);
953 queue_delayed_work(system_power_efficient_wq,
954 &info->micd_detect_work,
955 msecs_to_jiffies(debounce));
957 arizona_micd_detect(&info->micd_detect_work.work);
962 static void arizona_hpdet_work(struct work_struct *work)
964 struct arizona_extcon_info *info = container_of(work,
965 struct arizona_extcon_info,
968 mutex_lock(&info->lock);
969 arizona_start_hpdet_acc_id(info);
970 mutex_unlock(&info->lock);
973 static irqreturn_t arizona_jackdet(int irq, void *data)
975 struct arizona_extcon_info *info = data;
976 struct arizona *arizona = info->arizona;
977 unsigned int val, present, mask;
978 bool cancelled_hp, cancelled_mic;
981 cancelled_hp = cancel_delayed_work_sync(&info->hpdet_work);
982 cancelled_mic = cancel_delayed_work_sync(&info->micd_timeout_work);
984 pm_runtime_get_sync(info->dev);
986 mutex_lock(&info->lock);
988 if (arizona->pdata.jd_gpio5) {
989 mask = ARIZONA_MICD_CLAMP_STS;
990 if (arizona->pdata.jd_invert)
991 present = ARIZONA_MICD_CLAMP_STS;
995 mask = ARIZONA_JD1_STS;
996 if (arizona->pdata.jd_invert)
999 present = ARIZONA_JD1_STS;
1002 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
1004 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
1006 mutex_unlock(&info->lock);
1007 pm_runtime_put_autosuspend(info->dev);
1012 if (val == info->last_jackdet) {
1013 dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
1015 queue_delayed_work(system_power_efficient_wq,
1017 msecs_to_jiffies(HPDET_DEBOUNCE));
1019 if (cancelled_mic) {
1020 int micd_timeout = info->micd_timeout;
1022 queue_delayed_work(system_power_efficient_wq,
1023 &info->micd_timeout_work,
1024 msecs_to_jiffies(micd_timeout));
1029 info->last_jackdet = val;
1031 if (info->last_jackdet == present) {
1032 dev_dbg(arizona->dev, "Detected jack\n");
1033 ret = extcon_set_cable_state_(info->edev,
1034 EXTCON_MECHANICAL, true);
1037 dev_err(arizona->dev, "Mechanical report failed: %d\n",
1040 if (!arizona->pdata.hpdet_acc_id) {
1041 info->detecting = true;
1043 info->jack_flips = 0;
1045 arizona_start_mic(info);
1047 queue_delayed_work(system_power_efficient_wq,
1049 msecs_to_jiffies(HPDET_DEBOUNCE));
1052 regmap_update_bits(arizona->regmap,
1053 ARIZONA_JACK_DETECT_DEBOUNCE,
1054 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
1056 dev_dbg(arizona->dev, "Detected jack removal\n");
1058 arizona_stop_mic(info);
1060 info->num_hpdet_res = 0;
1061 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
1062 info->hpdet_res[i] = 0;
1064 info->hpdet_done = false;
1065 info->hpdet_retried = false;
1067 for (i = 0; i < info->num_micd_ranges; i++)
1068 input_report_key(info->input,
1069 info->micd_ranges[i].key, 0);
1070 input_sync(info->input);
1072 ret = extcon_update_state(info->edev, 0xffffffff, 0);
1074 dev_err(arizona->dev, "Removal report failed: %d\n",
1077 regmap_update_bits(arizona->regmap,
1078 ARIZONA_JACK_DETECT_DEBOUNCE,
1079 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
1080 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
1083 if (arizona->pdata.micd_timeout)
1084 info->micd_timeout = arizona->pdata.micd_timeout;
1086 info->micd_timeout = DEFAULT_MICD_TIMEOUT;
1089 /* Clear trig_sts to make sure DCVDD is not forced up */
1090 regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
1091 ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
1092 ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
1093 ARIZONA_JD1_FALL_TRIG_STS |
1094 ARIZONA_JD1_RISE_TRIG_STS);
1096 mutex_unlock(&info->lock);
1098 pm_runtime_mark_last_busy(info->dev);
1099 pm_runtime_put_autosuspend(info->dev);
1104 /* Map a level onto a slot in the register bank */
1105 static void arizona_micd_set_level(struct arizona *arizona, int index,
1111 reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
1120 /* Program the level itself */
1121 regmap_update_bits(arizona->regmap, reg, mask, level);
1124 static int arizona_extcon_device_get_pdata(struct arizona *arizona)
1126 struct arizona_pdata *pdata = &arizona->pdata;
1127 unsigned int val = ARIZONA_ACCDET_MODE_HPL;
1129 device_property_read_u32(arizona->dev, "wlf,hpdet-channel", &val);
1131 case ARIZONA_ACCDET_MODE_HPL:
1132 case ARIZONA_ACCDET_MODE_HPR:
1133 pdata->hpdet_channel = val;
1136 dev_err(arizona->dev,
1137 "Wrong wlf,hpdet-channel DT value %d\n", val);
1138 pdata->hpdet_channel = ARIZONA_ACCDET_MODE_HPL;
1141 device_property_read_u32(arizona->dev, "wlf,micd-detect-debounce",
1142 &pdata->micd_detect_debounce);
1144 device_property_read_u32(arizona->dev, "wlf,micd-bias-start-time",
1145 &pdata->micd_bias_start_time);
1147 device_property_read_u32(arizona->dev, "wlf,micd-rate",
1150 device_property_read_u32(arizona->dev, "wlf,micd-dbtime",
1151 &pdata->micd_dbtime);
1153 device_property_read_u32(arizona->dev, "wlf,micd-timeout",
1154 &pdata->micd_timeout);
1156 pdata->micd_force_micbias = device_property_read_bool(arizona->dev,
1157 "wlf,micd-force-micbias");
1162 static int arizona_extcon_probe(struct platform_device *pdev)
1164 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
1165 struct arizona_pdata *pdata = &arizona->pdata;
1166 struct arizona_extcon_info *info;
1168 unsigned int clamp_mode;
1169 int jack_irq_fall, jack_irq_rise;
1170 int ret, mode, i, j;
1172 if (!arizona->dapm || !arizona->dapm->card)
1173 return -EPROBE_DEFER;
1175 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1179 if (!dev_get_platdata(arizona->dev))
1180 arizona_extcon_device_get_pdata(arizona);
1182 info->micvdd = devm_regulator_get(&pdev->dev, "MICVDD");
1183 if (IS_ERR(info->micvdd)) {
1184 ret = PTR_ERR(info->micvdd);
1185 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
1189 mutex_init(&info->lock);
1190 info->arizona = arizona;
1191 info->dev = &pdev->dev;
1192 info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
1193 INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
1194 INIT_DELAYED_WORK(&info->micd_detect_work, arizona_micd_detect);
1195 INIT_DELAYED_WORK(&info->micd_timeout_work, arizona_micd_timeout_work);
1196 platform_set_drvdata(pdev, info);
1198 switch (arizona->type) {
1200 switch (arizona->rev) {
1202 info->micd_reva = true;
1205 info->micd_clamp = true;
1206 info->hpdet_ip_version = 1;
1212 switch (arizona->rev) {
1216 info->micd_clamp = true;
1217 info->hpdet_ip_version = 2;
1225 info->edev = devm_extcon_dev_allocate(&pdev->dev, arizona_cable);
1226 if (IS_ERR(info->edev)) {
1227 dev_err(&pdev->dev, "failed to allocate extcon device\n");
1231 ret = devm_extcon_dev_register(&pdev->dev, info->edev);
1233 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
1238 info->input = devm_input_allocate_device(&pdev->dev);
1240 dev_err(arizona->dev, "Can't allocate input dev\n");
1245 info->input->name = "Headset";
1246 info->input->phys = "arizona/extcon";
1248 if (pdata->num_micd_configs) {
1249 info->micd_modes = pdata->micd_configs;
1250 info->micd_num_modes = pdata->num_micd_configs;
1252 info->micd_modes = micd_default_modes;
1253 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1256 if (arizona->pdata.micd_pol_gpio > 0) {
1257 if (info->micd_modes[0].gpio)
1258 mode = GPIOF_OUT_INIT_HIGH;
1260 mode = GPIOF_OUT_INIT_LOW;
1262 ret = devm_gpio_request_one(&pdev->dev,
1263 arizona->pdata.micd_pol_gpio,
1267 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1268 arizona->pdata.micd_pol_gpio, ret);
1272 if (info->micd_modes[0].gpio)
1273 mode = GPIOD_OUT_HIGH;
1275 mode = GPIOD_OUT_LOW;
1277 /* We can't use devm here because we need to do the get
1278 * against the MFD device, as that is where the of_node
1279 * will reside, but if we devm against that the GPIO
1280 * will not be freed if the extcon driver is unloaded.
1282 info->micd_pol_gpio = gpiod_get_optional(arizona->dev,
1285 if (IS_ERR(info->micd_pol_gpio)) {
1286 ret = PTR_ERR(info->micd_pol_gpio);
1287 dev_err(arizona->dev,
1288 "Failed to get microphone polarity GPIO: %d\n",
1294 if (arizona->pdata.hpdet_id_gpio > 0) {
1295 ret = devm_gpio_request_one(&pdev->dev,
1296 arizona->pdata.hpdet_id_gpio,
1300 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1301 arizona->pdata.hpdet_id_gpio, ret);
1306 if (arizona->pdata.micd_bias_start_time)
1307 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1308 ARIZONA_MICD_BIAS_STARTTIME_MASK,
1309 arizona->pdata.micd_bias_start_time
1310 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1312 if (arizona->pdata.micd_rate)
1313 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1314 ARIZONA_MICD_RATE_MASK,
1315 arizona->pdata.micd_rate
1316 << ARIZONA_MICD_RATE_SHIFT);
1318 if (arizona->pdata.micd_dbtime)
1319 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1320 ARIZONA_MICD_DBTIME_MASK,
1321 arizona->pdata.micd_dbtime
1322 << ARIZONA_MICD_DBTIME_SHIFT);
1324 BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) != 0x40);
1326 if (arizona->pdata.num_micd_ranges) {
1327 info->micd_ranges = pdata->micd_ranges;
1328 info->num_micd_ranges = pdata->num_micd_ranges;
1330 info->micd_ranges = micd_default_ranges;
1331 info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
1334 if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_RANGE) {
1335 dev_err(arizona->dev, "Too many MICD ranges: %d\n",
1336 arizona->pdata.num_micd_ranges);
1339 if (info->num_micd_ranges > 1) {
1340 for (i = 1; i < info->num_micd_ranges; i++) {
1341 if (info->micd_ranges[i - 1].max >
1342 info->micd_ranges[i].max) {
1343 dev_err(arizona->dev,
1344 "MICD ranges must be sorted\n");
1351 /* Disable all buttons by default */
1352 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1353 ARIZONA_MICD_LVL_SEL_MASK, 0x81);
1355 /* Set up all the buttons the user specified */
1356 for (i = 0; i < info->num_micd_ranges; i++) {
1357 for (j = 0; j < ARRAY_SIZE(arizona_micd_levels); j++)
1358 if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
1361 if (j == ARRAY_SIZE(arizona_micd_levels)) {
1362 dev_err(arizona->dev, "Unsupported MICD level %d\n",
1363 info->micd_ranges[i].max);
1368 dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
1369 arizona_micd_levels[j], i);
1371 arizona_micd_set_level(arizona, i, j);
1372 input_set_capability(info->input, EV_KEY,
1373 info->micd_ranges[i].key);
1375 /* Enable reporting of that range */
1376 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1380 /* Set all the remaining keys to a maximum */
1381 for (; i < ARIZONA_MAX_MICD_RANGE; i++)
1382 arizona_micd_set_level(arizona, i, 0x3f);
1385 * If we have a clamp use it, activating in conjunction with
1386 * GPIO5 if that is connected for jack detect operation.
1388 if (info->micd_clamp) {
1389 if (arizona->pdata.jd_gpio5) {
1390 /* Put the GPIO into input mode with optional pull */
1392 if (arizona->pdata.jd_gpio5_nopull)
1393 val &= ~ARIZONA_GPN_PU;
1395 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1398 if (arizona->pdata.jd_invert)
1399 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH_GP5H;
1401 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL_GP5H;
1403 if (arizona->pdata.jd_invert)
1404 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH;
1406 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL;
1409 regmap_update_bits(arizona->regmap,
1410 ARIZONA_MICD_CLAMP_CONTROL,
1411 ARIZONA_MICD_CLAMP_MODE_MASK, clamp_mode);
1413 regmap_update_bits(arizona->regmap,
1414 ARIZONA_JACK_DETECT_DEBOUNCE,
1415 ARIZONA_MICD_CLAMP_DB,
1416 ARIZONA_MICD_CLAMP_DB);
1419 arizona_extcon_set_mode(info, 0);
1421 pm_runtime_enable(&pdev->dev);
1422 pm_runtime_idle(&pdev->dev);
1423 pm_runtime_get_sync(&pdev->dev);
1425 if (arizona->pdata.jd_gpio5) {
1426 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1427 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1429 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1430 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1433 ret = arizona_request_irq(arizona, jack_irq_rise,
1434 "JACKDET rise", arizona_jackdet, info);
1436 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1441 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1443 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1448 ret = arizona_request_irq(arizona, jack_irq_fall,
1449 "JACKDET fall", arizona_jackdet, info);
1451 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1455 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1457 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1462 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1463 "MICDET", arizona_micdet, info);
1465 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1469 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1470 "HPDET", arizona_hpdet_irq, info);
1472 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1476 arizona_clk32k_enable(arizona);
1477 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1478 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1479 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1480 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1482 ret = regulator_allow_bypass(info->micvdd, true);
1484 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1487 pm_runtime_put(&pdev->dev);
1489 ret = input_register_device(info->input);
1491 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1498 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1500 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1502 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1504 arizona_free_irq(arizona, jack_irq_fall, info);
1506 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1508 arizona_free_irq(arizona, jack_irq_rise, info);
1510 gpiod_put(info->micd_pol_gpio);
1512 pm_runtime_disable(&pdev->dev);
1516 static int arizona_extcon_remove(struct platform_device *pdev)
1518 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1519 struct arizona *arizona = info->arizona;
1520 int jack_irq_rise, jack_irq_fall;
1522 gpiod_put(info->micd_pol_gpio);
1524 pm_runtime_disable(&pdev->dev);
1526 regmap_update_bits(arizona->regmap,
1527 ARIZONA_MICD_CLAMP_CONTROL,
1528 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1530 if (arizona->pdata.jd_gpio5) {
1531 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1532 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1534 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1535 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1538 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1539 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1540 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1541 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1542 arizona_free_irq(arizona, jack_irq_rise, info);
1543 arizona_free_irq(arizona, jack_irq_fall, info);
1544 cancel_delayed_work_sync(&info->hpdet_work);
1545 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1546 ARIZONA_JD1_ENA, 0);
1547 arizona_clk32k_disable(arizona);
1552 static struct platform_driver arizona_extcon_driver = {
1554 .name = "arizona-extcon",
1556 .probe = arizona_extcon_probe,
1557 .remove = arizona_extcon_remove,
1560 module_platform_driver(arizona_extcon_driver);
1562 MODULE_DESCRIPTION("Arizona Extcon driver");
1563 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1564 MODULE_LICENSE("GPL");
1565 MODULE_ALIAS("platform:extcon-arizona");