2 * extcon-arizona.c - Extcon driver Wolfson Arizona devices
4 * Copyright (C) 2012 Wolfson Microelectronics plc
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/i2c.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/err.h>
23 #include <linux/gpio.h>
24 #include <linux/input.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/extcon.h>
30 #include <sound/soc.h>
32 #include <linux/mfd/arizona/core.h>
33 #include <linux/mfd/arizona/pdata.h>
34 #include <linux/mfd/arizona/registers.h>
36 #define ARIZONA_MAX_MICD_RANGE 8
38 #define ARIZONA_ACCDET_MODE_MIC 0
39 #define ARIZONA_ACCDET_MODE_HPL 1
40 #define ARIZONA_ACCDET_MODE_HPR 2
42 #define ARIZONA_HPDET_MAX 10000
44 #define HPDET_DEBOUNCE 500
45 #define DEFAULT_MICD_TIMEOUT 2000
47 struct arizona_extcon_info {
49 struct arizona *arizona;
51 struct regulator *micvdd;
52 struct input_dev *input;
57 const struct arizona_micd_config *micd_modes;
60 const struct arizona_micd_range *micd_ranges;
68 struct delayed_work hpdet_work;
69 struct delayed_work micd_detect_work;
70 struct delayed_work micd_timeout_work;
77 unsigned int hpdet_res[3];
85 struct extcon_dev edev;
88 static const struct arizona_micd_config micd_default_modes[] = {
89 { ARIZONA_ACCDET_SRC, 1, 0 },
93 static const struct arizona_micd_range micd_default_ranges[] = {
94 { .max = 11, .key = BTN_0 },
95 { .max = 28, .key = BTN_1 },
96 { .max = 54, .key = BTN_2 },
97 { .max = 100, .key = BTN_3 },
98 { .max = 186, .key = BTN_4 },
99 { .max = 430, .key = BTN_5 },
102 static const int arizona_micd_levels[] = {
103 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
104 49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
105 105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
106 270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
110 #define ARIZONA_CABLE_MECHANICAL 0
111 #define ARIZONA_CABLE_MICROPHONE 1
112 #define ARIZONA_CABLE_HEADPHONE 2
113 #define ARIZONA_CABLE_LINEOUT 3
115 static const char *arizona_cable[] = {
123 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info);
125 static void arizona_extcon_do_magic(struct arizona_extcon_info *info,
128 struct arizona *arizona = info->arizona;
131 mutex_lock(&arizona->dapm->card->dapm_mutex);
133 arizona->hpdet_magic = magic;
135 /* Keep the HP output stages disabled while doing the magic */
137 ret = regmap_update_bits(arizona->regmap,
138 ARIZONA_OUTPUT_ENABLES_1,
140 ARIZONA_OUT1R_ENA, 0);
142 dev_warn(arizona->dev,
143 "Failed to disable headphone outputs: %d\n",
147 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000,
150 dev_warn(arizona->dev, "Failed to do magic: %d\n",
153 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000,
156 dev_warn(arizona->dev, "Failed to do magic: %d\n",
159 /* Restore the desired state while not doing the magic */
161 ret = regmap_update_bits(arizona->regmap,
162 ARIZONA_OUTPUT_ENABLES_1,
164 ARIZONA_OUT1R_ENA, arizona->hp_ena);
166 dev_warn(arizona->dev,
167 "Failed to restore headphone outputs: %d\n",
171 mutex_unlock(&arizona->dapm->card->dapm_mutex);
174 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
176 struct arizona *arizona = info->arizona;
178 mode %= info->micd_num_modes;
180 if (arizona->pdata.micd_pol_gpio > 0)
181 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
182 info->micd_modes[mode].gpio);
183 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
184 ARIZONA_MICD_BIAS_SRC_MASK,
185 info->micd_modes[mode].bias <<
186 ARIZONA_MICD_BIAS_SRC_SHIFT);
187 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
188 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
190 info->micd_mode = mode;
192 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
195 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
197 switch (info->micd_modes[0].bias) {
209 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
211 struct arizona *arizona = info->arizona;
212 const char *widget = arizona_extcon_get_micbias(info);
213 struct snd_soc_dapm_context *dapm = arizona->dapm;
216 mutex_lock(&dapm->card->dapm_mutex);
218 ret = snd_soc_dapm_force_enable_pin(dapm, widget);
220 dev_warn(arizona->dev, "Failed to enable %s: %d\n",
223 mutex_unlock(&dapm->card->dapm_mutex);
225 snd_soc_dapm_sync(dapm);
227 if (!arizona->pdata.micd_force_micbias) {
228 mutex_lock(&dapm->card->dapm_mutex);
230 ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
232 dev_warn(arizona->dev, "Failed to disable %s: %d\n",
235 mutex_unlock(&dapm->card->dapm_mutex);
237 snd_soc_dapm_sync(dapm);
241 static void arizona_start_mic(struct arizona_extcon_info *info)
243 struct arizona *arizona = info->arizona;
247 /* Microphone detection can't use idle mode */
248 pm_runtime_get(info->dev);
250 if (info->detecting) {
251 ret = regulator_allow_bypass(info->micvdd, false);
253 dev_err(arizona->dev,
254 "Failed to regulate MICVDD: %d\n",
259 ret = regulator_enable(info->micvdd);
261 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
265 if (info->micd_reva) {
266 regmap_write(arizona->regmap, 0x80, 0x3);
267 regmap_write(arizona->regmap, 0x294, 0);
268 regmap_write(arizona->regmap, 0x80, 0x0);
271 regmap_update_bits(arizona->regmap,
272 ARIZONA_ACCESSORY_DETECT_MODE_1,
273 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
275 arizona_extcon_pulse_micbias(info);
277 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
278 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
281 regulator_disable(info->micvdd);
282 pm_runtime_put_autosuspend(info->dev);
286 static void arizona_stop_mic(struct arizona_extcon_info *info)
288 struct arizona *arizona = info->arizona;
289 const char *widget = arizona_extcon_get_micbias(info);
290 struct snd_soc_dapm_context *dapm = arizona->dapm;
294 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
298 mutex_lock(&dapm->card->dapm_mutex);
300 ret = snd_soc_dapm_disable_pin(dapm, widget);
302 dev_warn(arizona->dev,
303 "Failed to disable %s: %d\n",
306 mutex_unlock(&dapm->card->dapm_mutex);
308 snd_soc_dapm_sync(dapm);
310 if (info->micd_reva) {
311 regmap_write(arizona->regmap, 0x80, 0x3);
312 regmap_write(arizona->regmap, 0x294, 2);
313 regmap_write(arizona->regmap, 0x80, 0x0);
316 ret = regulator_allow_bypass(info->micvdd, true);
318 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
323 regulator_disable(info->micvdd);
324 pm_runtime_mark_last_busy(info->dev);
325 pm_runtime_put_autosuspend(info->dev);
330 unsigned int factor_a;
331 unsigned int factor_b;
332 } arizona_hpdet_b_ranges[] = {
341 } arizona_hpdet_c_ranges[] = {
348 static int arizona_hpdet_read(struct arizona_extcon_info *info)
350 struct arizona *arizona = info->arizona;
351 unsigned int val, range;
354 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
356 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
361 switch (info->hpdet_ip) {
363 if (!(val & ARIZONA_HP_DONE)) {
364 dev_err(arizona->dev, "HPDET did not complete: %x\n",
369 val &= ARIZONA_HP_LVL_MASK;
373 if (!(val & ARIZONA_HP_DONE_B)) {
374 dev_err(arizona->dev, "HPDET did not complete: %x\n",
379 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
381 dev_err(arizona->dev, "Failed to read HP value: %d\n",
386 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
388 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
389 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
391 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
392 (val < 100 || val >= 0x3fb)) {
394 dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
396 regmap_update_bits(arizona->regmap,
397 ARIZONA_HEADPHONE_DETECT_1,
398 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
400 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
404 /* If we go out of range report top of range */
405 if (val < 100 || val >= 0x3fb) {
406 dev_dbg(arizona->dev, "Measurement out of range\n");
407 return ARIZONA_HPDET_MAX;
410 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
413 val = arizona_hpdet_b_ranges[range].factor_b
415 arizona_hpdet_b_ranges[range].factor_a);
419 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
422 if (!(val & ARIZONA_HP_DONE_B)) {
423 dev_err(arizona->dev, "HPDET did not complete: %x\n",
428 val &= ARIZONA_HP_LVL_B_MASK;
430 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
432 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
433 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
435 /* Skip up or down a range? */
436 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
438 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
439 arizona_hpdet_c_ranges[range].min,
440 arizona_hpdet_c_ranges[range].max);
441 regmap_update_bits(arizona->regmap,
442 ARIZONA_HEADPHONE_DETECT_1,
443 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
445 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
449 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
450 (val >= arizona_hpdet_c_ranges[range].max)) {
452 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
453 arizona_hpdet_c_ranges[range].min,
454 arizona_hpdet_c_ranges[range].max);
455 regmap_update_bits(arizona->regmap,
456 ARIZONA_HEADPHONE_DETECT_1,
457 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
459 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
464 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
468 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading,
471 struct arizona *arizona = info->arizona;
472 int id_gpio = arizona->pdata.hpdet_id_gpio;
475 * If we're using HPDET for accessory identification we need
476 * to take multiple measurements, step through them in sequence.
478 if (arizona->pdata.hpdet_acc_id) {
479 info->hpdet_res[info->num_hpdet_res++] = *reading;
481 /* Only check the mic directly if we didn't already ID it */
482 if (id_gpio && info->num_hpdet_res == 1) {
483 dev_dbg(arizona->dev, "Measuring mic\n");
485 regmap_update_bits(arizona->regmap,
486 ARIZONA_ACCESSORY_DETECT_MODE_1,
487 ARIZONA_ACCDET_MODE_MASK |
489 ARIZONA_ACCDET_MODE_HPR |
490 info->micd_modes[0].src);
492 gpio_set_value_cansleep(id_gpio, 1);
494 regmap_update_bits(arizona->regmap,
495 ARIZONA_HEADPHONE_DETECT_1,
496 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
500 /* OK, got both. Now, compare... */
501 dev_dbg(arizona->dev, "HPDET measured %d %d\n",
502 info->hpdet_res[0], info->hpdet_res[1]);
504 /* Take the headphone impedance for the main report */
505 *reading = info->hpdet_res[0];
507 /* Sometimes we get false readings due to slow insert */
508 if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
509 dev_dbg(arizona->dev, "Retrying high impedance\n");
510 info->num_hpdet_res = 0;
511 info->hpdet_retried = true;
512 arizona_start_hpdet_acc_id(info);
513 pm_runtime_put(info->dev);
518 * If we measure the mic as high impedance
520 if (!id_gpio || info->hpdet_res[1] > 50) {
521 dev_dbg(arizona->dev, "Detected mic\n");
523 info->detecting = true;
525 dev_dbg(arizona->dev, "Detected headphone\n");
528 /* Make sure everything is reset back to the real polarity */
529 regmap_update_bits(arizona->regmap,
530 ARIZONA_ACCESSORY_DETECT_MODE_1,
532 info->micd_modes[0].src);
538 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
540 struct arizona_extcon_info *info = data;
541 struct arizona *arizona = info->arizona;
542 int id_gpio = arizona->pdata.hpdet_id_gpio;
543 int report = ARIZONA_CABLE_HEADPHONE;
547 mutex_lock(&info->lock);
549 /* If we got a spurious IRQ for some reason then ignore it */
550 if (!info->hpdet_active) {
551 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
552 mutex_unlock(&info->lock);
556 /* If the cable was removed while measuring ignore the result */
557 ret = extcon_get_cable_state_(&info->edev, ARIZONA_CABLE_MECHANICAL);
559 dev_err(arizona->dev, "Failed to check cable state: %d\n",
563 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
567 ret = arizona_hpdet_read(info);
574 /* Reset back to starting range */
575 regmap_update_bits(arizona->regmap,
576 ARIZONA_HEADPHONE_DETECT_1,
577 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
580 ret = arizona_hpdet_do_id(info, &reading, &mic);
586 /* Report high impedence cables as line outputs */
588 report = ARIZONA_CABLE_LINEOUT;
590 report = ARIZONA_CABLE_HEADPHONE;
592 ret = extcon_set_cable_state_(&info->edev, report, true);
594 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
597 arizona_extcon_do_magic(info, 0);
601 gpio_set_value_cansleep(id_gpio, 0);
603 /* Revert back to MICDET mode */
604 regmap_update_bits(arizona->regmap,
605 ARIZONA_ACCESSORY_DETECT_MODE_1,
606 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
608 /* If we have a mic then reenable MICDET */
609 if (mic || info->mic)
610 arizona_start_mic(info);
612 if (info->hpdet_active) {
613 pm_runtime_put_autosuspend(info->dev);
614 info->hpdet_active = false;
617 info->hpdet_done = true;
620 mutex_unlock(&info->lock);
625 static void arizona_identify_headphone(struct arizona_extcon_info *info)
627 struct arizona *arizona = info->arizona;
630 if (info->hpdet_done)
633 dev_dbg(arizona->dev, "Starting HPDET\n");
635 /* Make sure we keep the device enabled during the measurement */
636 pm_runtime_get(info->dev);
638 info->hpdet_active = true;
641 arizona_stop_mic(info);
643 arizona_extcon_do_magic(info, 0x4000);
645 ret = regmap_update_bits(arizona->regmap,
646 ARIZONA_ACCESSORY_DETECT_MODE_1,
647 ARIZONA_ACCDET_MODE_MASK,
648 ARIZONA_ACCDET_MODE_HPL);
650 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
654 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
655 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
657 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
665 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
666 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
668 /* Just report headphone */
669 ret = extcon_update_state(&info->edev,
670 1 << ARIZONA_CABLE_HEADPHONE,
671 1 << ARIZONA_CABLE_HEADPHONE);
673 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
676 arizona_start_mic(info);
678 info->hpdet_active = false;
681 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
683 struct arizona *arizona = info->arizona;
688 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
690 /* Make sure we keep the device enabled during the measurement */
691 pm_runtime_get_sync(info->dev);
693 info->hpdet_active = true;
695 arizona_extcon_do_magic(info, 0x4000);
697 ret = regmap_update_bits(arizona->regmap,
698 ARIZONA_ACCESSORY_DETECT_MODE_1,
699 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
700 info->micd_modes[0].src |
701 ARIZONA_ACCDET_MODE_HPL);
703 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
707 if (arizona->pdata.hpdet_acc_id_line) {
708 ret = regmap_update_bits(arizona->regmap,
709 ARIZONA_HEADPHONE_DETECT_1,
710 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
712 dev_err(arizona->dev,
713 "Can't start HPDETL measurement: %d\n",
718 arizona_hpdet_do_id(info, &hp_reading, &mic);
724 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
725 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
727 /* Just report headphone */
728 ret = extcon_update_state(&info->edev,
729 1 << ARIZONA_CABLE_HEADPHONE,
730 1 << ARIZONA_CABLE_HEADPHONE);
732 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
734 info->hpdet_active = false;
737 static void arizona_micd_timeout_work(struct work_struct *work)
739 struct arizona_extcon_info *info = container_of(work,
740 struct arizona_extcon_info,
741 micd_timeout_work.work);
743 mutex_lock(&info->lock);
745 dev_dbg(info->arizona->dev, "MICD timed out, reporting HP\n");
746 arizona_identify_headphone(info);
748 info->detecting = false;
750 arizona_stop_mic(info);
752 mutex_unlock(&info->lock);
755 static void arizona_micd_detect(struct work_struct *work)
757 struct arizona_extcon_info *info = container_of(work,
758 struct arizona_extcon_info,
759 micd_detect_work.work);
760 struct arizona *arizona = info->arizona;
761 unsigned int val = 0, lvl;
764 cancel_delayed_work_sync(&info->micd_timeout_work);
766 mutex_lock(&info->lock);
768 for (i = 0; i < 10 && !(val & 0x7fc); i++) {
769 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
771 dev_err(arizona->dev,
772 "Failed to read MICDET: %d\n", ret);
773 mutex_unlock(&info->lock);
777 dev_dbg(arizona->dev, "MICDET: %x\n", val);
779 if (!(val & ARIZONA_MICD_VALID)) {
780 dev_warn(arizona->dev,
781 "Microphone detection state invalid\n");
782 mutex_unlock(&info->lock);
787 if (i == 10 && !(val & 0x7fc)) {
788 dev_err(arizona->dev, "Failed to get valid MICDET value\n");
789 mutex_unlock(&info->lock);
793 /* Due to jack detect this should never happen */
794 if (!(val & ARIZONA_MICD_STS)) {
795 dev_warn(arizona->dev, "Detected open circuit\n");
796 info->detecting = false;
800 /* If we got a high impedence we should have a headset, report it. */
801 if (info->detecting && (val & 0x400)) {
802 arizona_identify_headphone(info);
804 ret = extcon_update_state(&info->edev,
805 1 << ARIZONA_CABLE_MICROPHONE,
806 1 << ARIZONA_CABLE_MICROPHONE);
809 dev_err(arizona->dev, "Headset report failed: %d\n",
812 /* Don't need to regulate for button detection */
813 ret = regulator_allow_bypass(info->micvdd, false);
815 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
820 info->detecting = false;
824 /* If we detected a lower impedence during initial startup
825 * then we probably have the wrong polarity, flip it. Don't
826 * do this for the lowest impedences to speed up detection of
827 * plain headphones. If both polarities report a low
828 * impedence then give up and report headphones.
830 if (info->detecting && (val & 0x3f8)) {
831 if (info->jack_flips >= info->micd_num_modes * 10) {
832 dev_dbg(arizona->dev, "Detected HP/line\n");
833 arizona_identify_headphone(info);
835 info->detecting = false;
837 arizona_stop_mic(info);
840 if (info->micd_mode == info->micd_num_modes)
842 arizona_extcon_set_mode(info, info->micd_mode);
851 * If we're still detecting and we detect a short then we've
852 * got a headphone. Otherwise it's a button press.
856 dev_dbg(arizona->dev, "Mic button detected\n");
858 lvl = val & ARIZONA_MICD_LVL_MASK;
859 lvl >>= ARIZONA_MICD_LVL_SHIFT;
861 for (i = 0; i < info->num_micd_ranges; i++)
862 input_report_key(info->input,
863 info->micd_ranges[i].key, 0);
866 WARN_ON(ffs(lvl) - 1 >= info->num_micd_ranges);
867 if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
868 key = info->micd_ranges[ffs(lvl) - 1].key;
869 input_report_key(info->input, key, 1);
870 input_sync(info->input);
873 } else if (info->detecting) {
874 dev_dbg(arizona->dev, "Headphone detected\n");
875 info->detecting = false;
876 arizona_stop_mic(info);
878 arizona_identify_headphone(info);
880 dev_warn(arizona->dev, "Button with no mic: %x\n",
884 dev_dbg(arizona->dev, "Mic button released\n");
885 for (i = 0; i < info->num_micd_ranges; i++)
886 input_report_key(info->input,
887 info->micd_ranges[i].key, 0);
888 input_sync(info->input);
889 arizona_extcon_pulse_micbias(info);
894 queue_delayed_work(system_power_efficient_wq,
895 &info->micd_timeout_work,
896 msecs_to_jiffies(info->micd_timeout));
898 pm_runtime_mark_last_busy(info->dev);
899 mutex_unlock(&info->lock);
902 static irqreturn_t arizona_micdet(int irq, void *data)
904 struct arizona_extcon_info *info = data;
905 struct arizona *arizona = info->arizona;
906 int debounce = arizona->pdata.micd_detect_debounce;
908 cancel_delayed_work_sync(&info->micd_detect_work);
909 cancel_delayed_work_sync(&info->micd_timeout_work);
911 mutex_lock(&info->lock);
912 if (!info->detecting)
914 mutex_unlock(&info->lock);
917 queue_delayed_work(system_power_efficient_wq,
918 &info->micd_detect_work,
919 msecs_to_jiffies(debounce));
921 arizona_micd_detect(&info->micd_detect_work.work);
926 static void arizona_hpdet_work(struct work_struct *work)
928 struct arizona_extcon_info *info = container_of(work,
929 struct arizona_extcon_info,
932 mutex_lock(&info->lock);
933 arizona_start_hpdet_acc_id(info);
934 mutex_unlock(&info->lock);
937 static irqreturn_t arizona_jackdet(int irq, void *data)
939 struct arizona_extcon_info *info = data;
940 struct arizona *arizona = info->arizona;
941 unsigned int val, present, mask;
942 bool cancelled_hp, cancelled_mic;
945 cancelled_hp = cancel_delayed_work_sync(&info->hpdet_work);
946 cancelled_mic = cancel_delayed_work_sync(&info->micd_timeout_work);
948 pm_runtime_get_sync(info->dev);
950 mutex_lock(&info->lock);
952 if (arizona->pdata.jd_gpio5) {
953 mask = ARIZONA_MICD_CLAMP_STS;
956 mask = ARIZONA_JD1_STS;
957 present = ARIZONA_JD1_STS;
960 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
962 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
964 mutex_unlock(&info->lock);
965 pm_runtime_put_autosuspend(info->dev);
970 if (val == info->last_jackdet) {
971 dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
973 queue_delayed_work(system_power_efficient_wq,
975 msecs_to_jiffies(HPDET_DEBOUNCE));
978 int micd_timeout = info->micd_timeout;
980 queue_delayed_work(system_power_efficient_wq,
981 &info->micd_timeout_work,
982 msecs_to_jiffies(micd_timeout));
987 info->last_jackdet = val;
989 if (info->last_jackdet == present) {
990 dev_dbg(arizona->dev, "Detected jack\n");
991 ret = extcon_set_cable_state_(&info->edev,
992 ARIZONA_CABLE_MECHANICAL, true);
995 dev_err(arizona->dev, "Mechanical report failed: %d\n",
998 if (!arizona->pdata.hpdet_acc_id) {
999 info->detecting = true;
1001 info->jack_flips = 0;
1003 arizona_start_mic(info);
1005 queue_delayed_work(system_power_efficient_wq,
1007 msecs_to_jiffies(HPDET_DEBOUNCE));
1010 regmap_update_bits(arizona->regmap,
1011 ARIZONA_JACK_DETECT_DEBOUNCE,
1012 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
1014 dev_dbg(arizona->dev, "Detected jack removal\n");
1016 arizona_stop_mic(info);
1018 info->num_hpdet_res = 0;
1019 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
1020 info->hpdet_res[i] = 0;
1022 info->hpdet_done = false;
1023 info->hpdet_retried = false;
1025 for (i = 0; i < info->num_micd_ranges; i++)
1026 input_report_key(info->input,
1027 info->micd_ranges[i].key, 0);
1028 input_sync(info->input);
1030 ret = extcon_update_state(&info->edev, 0xffffffff, 0);
1032 dev_err(arizona->dev, "Removal report failed: %d\n",
1035 regmap_update_bits(arizona->regmap,
1036 ARIZONA_JACK_DETECT_DEBOUNCE,
1037 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
1038 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
1041 if (arizona->pdata.micd_timeout)
1042 info->micd_timeout = arizona->pdata.micd_timeout;
1044 info->micd_timeout = DEFAULT_MICD_TIMEOUT;
1047 /* Clear trig_sts to make sure DCVDD is not forced up */
1048 regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
1049 ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
1050 ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
1051 ARIZONA_JD1_FALL_TRIG_STS |
1052 ARIZONA_JD1_RISE_TRIG_STS);
1054 mutex_unlock(&info->lock);
1056 pm_runtime_mark_last_busy(info->dev);
1057 pm_runtime_put_autosuspend(info->dev);
1062 /* Map a level onto a slot in the register bank */
1063 static void arizona_micd_set_level(struct arizona *arizona, int index,
1069 reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
1078 /* Program the level itself */
1079 regmap_update_bits(arizona->regmap, reg, mask, level);
1082 static int arizona_extcon_probe(struct platform_device *pdev)
1084 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
1085 struct arizona_pdata *pdata = &arizona->pdata;
1086 struct arizona_extcon_info *info;
1088 int jack_irq_fall, jack_irq_rise;
1089 int ret, mode, i, j;
1091 if (!arizona->dapm || !arizona->dapm->card)
1092 return -EPROBE_DEFER;
1094 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1096 dev_err(&pdev->dev, "Failed to allocate memory\n");
1101 info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
1102 if (IS_ERR(info->micvdd)) {
1103 ret = PTR_ERR(info->micvdd);
1104 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
1108 mutex_init(&info->lock);
1109 info->arizona = arizona;
1110 info->dev = &pdev->dev;
1111 info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
1112 INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
1113 INIT_DELAYED_WORK(&info->micd_detect_work, arizona_micd_detect);
1114 INIT_DELAYED_WORK(&info->micd_timeout_work, arizona_micd_timeout_work);
1115 platform_set_drvdata(pdev, info);
1117 switch (arizona->type) {
1119 switch (arizona->rev) {
1121 info->micd_reva = true;
1124 info->micd_clamp = true;
1133 info->edev.name = "Headset Jack";
1134 info->edev.dev.parent = arizona->dev;
1135 info->edev.supported_cable = arizona_cable;
1137 ret = extcon_dev_register(&info->edev);
1139 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
1144 info->input = devm_input_allocate_device(&pdev->dev);
1146 dev_err(arizona->dev, "Can't allocate input dev\n");
1151 info->input->name = "Headset";
1152 info->input->phys = "arizona/extcon";
1153 info->input->dev.parent = &pdev->dev;
1155 if (pdata->num_micd_configs) {
1156 info->micd_modes = pdata->micd_configs;
1157 info->micd_num_modes = pdata->num_micd_configs;
1159 info->micd_modes = micd_default_modes;
1160 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1163 if (arizona->pdata.micd_pol_gpio > 0) {
1164 if (info->micd_modes[0].gpio)
1165 mode = GPIOF_OUT_INIT_HIGH;
1167 mode = GPIOF_OUT_INIT_LOW;
1169 ret = devm_gpio_request_one(&pdev->dev,
1170 arizona->pdata.micd_pol_gpio,
1174 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1175 arizona->pdata.micd_pol_gpio, ret);
1180 if (arizona->pdata.hpdet_id_gpio > 0) {
1181 ret = devm_gpio_request_one(&pdev->dev,
1182 arizona->pdata.hpdet_id_gpio,
1186 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1187 arizona->pdata.hpdet_id_gpio, ret);
1192 if (arizona->pdata.micd_bias_start_time)
1193 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1194 ARIZONA_MICD_BIAS_STARTTIME_MASK,
1195 arizona->pdata.micd_bias_start_time
1196 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1198 if (arizona->pdata.micd_rate)
1199 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1200 ARIZONA_MICD_RATE_MASK,
1201 arizona->pdata.micd_rate
1202 << ARIZONA_MICD_RATE_SHIFT);
1204 if (arizona->pdata.micd_dbtime)
1205 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1206 ARIZONA_MICD_DBTIME_MASK,
1207 arizona->pdata.micd_dbtime
1208 << ARIZONA_MICD_DBTIME_SHIFT);
1210 BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) != 0x40);
1212 if (arizona->pdata.num_micd_ranges) {
1213 info->micd_ranges = pdata->micd_ranges;
1214 info->num_micd_ranges = pdata->num_micd_ranges;
1216 info->micd_ranges = micd_default_ranges;
1217 info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
1220 if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_RANGE) {
1221 dev_err(arizona->dev, "Too many MICD ranges: %d\n",
1222 arizona->pdata.num_micd_ranges);
1225 if (info->num_micd_ranges > 1) {
1226 for (i = 1; i < info->num_micd_ranges; i++) {
1227 if (info->micd_ranges[i - 1].max >
1228 info->micd_ranges[i].max) {
1229 dev_err(arizona->dev,
1230 "MICD ranges must be sorted\n");
1237 /* Disable all buttons by default */
1238 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1239 ARIZONA_MICD_LVL_SEL_MASK, 0x81);
1241 /* Set up all the buttons the user specified */
1242 for (i = 0; i < info->num_micd_ranges; i++) {
1243 for (j = 0; j < ARRAY_SIZE(arizona_micd_levels); j++)
1244 if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
1247 if (j == ARRAY_SIZE(arizona_micd_levels)) {
1248 dev_err(arizona->dev, "Unsupported MICD level %d\n",
1249 info->micd_ranges[i].max);
1254 dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
1255 arizona_micd_levels[j], i);
1257 arizona_micd_set_level(arizona, i, j);
1258 input_set_capability(info->input, EV_KEY,
1259 info->micd_ranges[i].key);
1261 /* Enable reporting of that range */
1262 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1266 /* Set all the remaining keys to a maximum */
1267 for (; i < ARIZONA_MAX_MICD_RANGE; i++)
1268 arizona_micd_set_level(arizona, i, 0x3f);
1271 * If we have a clamp use it, activating in conjunction with
1272 * GPIO5 if that is connected for jack detect operation.
1274 if (info->micd_clamp) {
1275 if (arizona->pdata.jd_gpio5) {
1276 /* Put the GPIO into input mode with optional pull */
1278 if (arizona->pdata.jd_gpio5_nopull)
1279 val &= ~ARIZONA_GPN_PU;
1281 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1284 regmap_update_bits(arizona->regmap,
1285 ARIZONA_MICD_CLAMP_CONTROL,
1286 ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
1288 regmap_update_bits(arizona->regmap,
1289 ARIZONA_MICD_CLAMP_CONTROL,
1290 ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
1293 regmap_update_bits(arizona->regmap,
1294 ARIZONA_JACK_DETECT_DEBOUNCE,
1295 ARIZONA_MICD_CLAMP_DB,
1296 ARIZONA_MICD_CLAMP_DB);
1299 arizona_extcon_set_mode(info, 0);
1301 pm_runtime_enable(&pdev->dev);
1302 pm_runtime_idle(&pdev->dev);
1303 pm_runtime_get_sync(&pdev->dev);
1305 if (arizona->pdata.jd_gpio5) {
1306 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1307 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1309 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1310 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1313 ret = arizona_request_irq(arizona, jack_irq_rise,
1314 "JACKDET rise", arizona_jackdet, info);
1316 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1321 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1323 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1328 ret = arizona_request_irq(arizona, jack_irq_fall,
1329 "JACKDET fall", arizona_jackdet, info);
1331 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1335 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1337 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1342 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1343 "MICDET", arizona_micdet, info);
1345 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1349 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1350 "HPDET", arizona_hpdet_irq, info);
1352 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1356 arizona_clk32k_enable(arizona);
1357 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1358 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1359 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1360 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1362 ret = regulator_allow_bypass(info->micvdd, true);
1364 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1367 pm_runtime_put(&pdev->dev);
1369 ret = input_register_device(info->input);
1371 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1378 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1380 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1382 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1384 arizona_free_irq(arizona, jack_irq_fall, info);
1386 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1388 arizona_free_irq(arizona, jack_irq_rise, info);
1391 pm_runtime_disable(&pdev->dev);
1392 extcon_dev_unregister(&info->edev);
1397 static int arizona_extcon_remove(struct platform_device *pdev)
1399 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1400 struct arizona *arizona = info->arizona;
1401 int jack_irq_rise, jack_irq_fall;
1403 pm_runtime_disable(&pdev->dev);
1405 regmap_update_bits(arizona->regmap,
1406 ARIZONA_MICD_CLAMP_CONTROL,
1407 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1409 if (arizona->pdata.jd_gpio5) {
1410 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1411 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1413 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1414 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1417 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1418 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1419 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1420 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1421 arizona_free_irq(arizona, jack_irq_rise, info);
1422 arizona_free_irq(arizona, jack_irq_fall, info);
1423 cancel_delayed_work_sync(&info->hpdet_work);
1424 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1425 ARIZONA_JD1_ENA, 0);
1426 arizona_clk32k_disable(arizona);
1427 extcon_dev_unregister(&info->edev);
1432 static struct platform_driver arizona_extcon_driver = {
1434 .name = "arizona-extcon",
1435 .owner = THIS_MODULE,
1437 .probe = arizona_extcon_probe,
1438 .remove = arizona_extcon_remove,
1441 module_platform_driver(arizona_extcon_driver);
1443 MODULE_DESCRIPTION("Arizona Extcon driver");
1444 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1445 MODULE_LICENSE("GPL");
1446 MODULE_ALIAS("platform:extcon-arizona");