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
46 struct arizona_extcon_info {
48 struct arizona *arizona;
50 struct regulator *micvdd;
51 struct input_dev *input;
56 const struct arizona_micd_config *micd_modes;
59 const struct arizona_micd_range *micd_ranges;
65 struct delayed_work hpdet_work;
72 unsigned int hpdet_res[3];
80 struct extcon_dev edev;
83 static const struct arizona_micd_config micd_default_modes[] = {
84 { ARIZONA_ACCDET_SRC, 1 << ARIZONA_MICD_BIAS_SRC_SHIFT, 0 },
85 { 0, 2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 },
88 static const struct arizona_micd_range micd_default_ranges[] = {
89 { .max = 11, .key = BTN_0 },
90 { .max = 28, .key = BTN_1 },
91 { .max = 54, .key = BTN_2 },
92 { .max = 100, .key = BTN_3 },
93 { .max = 186, .key = BTN_4 },
94 { .max = 430, .key = BTN_5 },
97 static const int arizona_micd_levels[] = {
98 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
99 49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
100 105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
101 270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
105 #define ARIZONA_CABLE_MECHANICAL 0
106 #define ARIZONA_CABLE_MICROPHONE 1
107 #define ARIZONA_CABLE_HEADPHONE 2
108 #define ARIZONA_CABLE_LINEOUT 3
110 static const char *arizona_cable[] = {
118 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info);
120 static void arizona_extcon_do_magic(struct arizona_extcon_info *info,
123 struct arizona *arizona = info->arizona;
126 mutex_lock(&arizona->dapm->card->dapm_mutex);
128 arizona->hpdet_magic = magic;
130 /* Keep the HP output stages disabled while doing the magic */
132 ret = regmap_update_bits(arizona->regmap,
133 ARIZONA_OUTPUT_ENABLES_1,
135 ARIZONA_OUT1R_ENA, 0);
137 dev_warn(arizona->dev,
138 "Failed to disable headphone outputs: %d\n",
142 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000,
145 dev_warn(arizona->dev, "Failed to do magic: %d\n",
148 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000,
151 dev_warn(arizona->dev, "Failed to do magic: %d\n",
154 /* Restore the desired state while not doing the magic */
156 ret = regmap_update_bits(arizona->regmap,
157 ARIZONA_OUTPUT_ENABLES_1,
159 ARIZONA_OUT1R_ENA, arizona->hp_ena);
161 dev_warn(arizona->dev,
162 "Failed to restore headphone outputs: %d\n",
166 mutex_unlock(&arizona->dapm->card->dapm_mutex);
169 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
171 struct arizona *arizona = info->arizona;
173 mode %= info->micd_num_modes;
175 if (arizona->pdata.micd_pol_gpio > 0)
176 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
177 info->micd_modes[mode].gpio);
178 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
179 ARIZONA_MICD_BIAS_SRC_MASK,
180 info->micd_modes[mode].bias);
181 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
182 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
184 info->micd_mode = mode;
186 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
189 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
191 switch (info->micd_modes[0].bias >> ARIZONA_MICD_BIAS_SRC_SHIFT) {
203 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
205 struct arizona *arizona = info->arizona;
206 const char *widget = arizona_extcon_get_micbias(info);
207 struct snd_soc_dapm_context *dapm = arizona->dapm;
210 mutex_lock(&dapm->card->dapm_mutex);
212 ret = snd_soc_dapm_force_enable_pin(dapm, widget);
214 dev_warn(arizona->dev, "Failed to enable %s: %d\n",
217 mutex_unlock(&dapm->card->dapm_mutex);
219 snd_soc_dapm_sync(dapm);
221 if (!arizona->pdata.micd_force_micbias) {
222 mutex_lock(&dapm->card->dapm_mutex);
224 ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
226 dev_warn(arizona->dev, "Failed to disable %s: %d\n",
229 mutex_unlock(&dapm->card->dapm_mutex);
231 snd_soc_dapm_sync(dapm);
235 static void arizona_start_mic(struct arizona_extcon_info *info)
237 struct arizona *arizona = info->arizona;
241 /* Microphone detection can't use idle mode */
242 pm_runtime_get(info->dev);
244 if (info->detecting) {
245 ret = regulator_allow_bypass(info->micvdd, false);
247 dev_err(arizona->dev,
248 "Failed to regulate MICVDD: %d\n",
253 ret = regulator_enable(info->micvdd);
255 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
259 if (info->micd_reva) {
260 regmap_write(arizona->regmap, 0x80, 0x3);
261 regmap_write(arizona->regmap, 0x294, 0);
262 regmap_write(arizona->regmap, 0x80, 0x0);
265 regmap_update_bits(arizona->regmap,
266 ARIZONA_ACCESSORY_DETECT_MODE_1,
267 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
269 arizona_extcon_pulse_micbias(info);
271 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
272 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
275 regulator_disable(info->micvdd);
276 pm_runtime_put_autosuspend(info->dev);
280 static void arizona_stop_mic(struct arizona_extcon_info *info)
282 struct arizona *arizona = info->arizona;
283 const char *widget = arizona_extcon_get_micbias(info);
284 struct snd_soc_dapm_context *dapm = arizona->dapm;
288 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
292 mutex_lock(&dapm->card->dapm_mutex);
294 ret = snd_soc_dapm_disable_pin(dapm, widget);
296 dev_warn(arizona->dev,
297 "Failed to disable %s: %d\n",
300 mutex_unlock(&dapm->card->dapm_mutex);
302 snd_soc_dapm_sync(dapm);
304 if (info->micd_reva) {
305 regmap_write(arizona->regmap, 0x80, 0x3);
306 regmap_write(arizona->regmap, 0x294, 2);
307 regmap_write(arizona->regmap, 0x80, 0x0);
310 ret = regulator_allow_bypass(info->micvdd, true);
312 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
317 regulator_disable(info->micvdd);
318 pm_runtime_mark_last_busy(info->dev);
319 pm_runtime_put_autosuspend(info->dev);
324 unsigned int factor_a;
325 unsigned int factor_b;
326 } arizona_hpdet_b_ranges[] = {
335 } arizona_hpdet_c_ranges[] = {
342 static int arizona_hpdet_read(struct arizona_extcon_info *info)
344 struct arizona *arizona = info->arizona;
345 unsigned int val, range;
348 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
350 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
355 switch (info->hpdet_ip) {
357 if (!(val & ARIZONA_HP_DONE)) {
358 dev_err(arizona->dev, "HPDET did not complete: %x\n",
363 val &= ARIZONA_HP_LVL_MASK;
367 if (!(val & ARIZONA_HP_DONE_B)) {
368 dev_err(arizona->dev, "HPDET did not complete: %x\n",
373 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
375 dev_err(arizona->dev, "Failed to read HP value: %d\n",
380 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
382 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
383 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
385 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
386 (val < 100 || val > 0x3fb)) {
388 dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
390 regmap_update_bits(arizona->regmap,
391 ARIZONA_HEADPHONE_DETECT_1,
392 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
394 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
398 /* If we go out of range report top of range */
399 if (val < 100 || val > 0x3fb) {
400 dev_dbg(arizona->dev, "Measurement out of range\n");
401 return ARIZONA_HPDET_MAX;
404 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
407 val = arizona_hpdet_b_ranges[range].factor_b
409 arizona_hpdet_b_ranges[range].factor_a);
413 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
416 if (!(val & ARIZONA_HP_DONE_B)) {
417 dev_err(arizona->dev, "HPDET did not complete: %x\n",
422 val &= ARIZONA_HP_LVL_B_MASK;
424 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
426 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
427 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
429 /* Skip up or down a range? */
430 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
432 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
433 arizona_hpdet_c_ranges[range].min,
434 arizona_hpdet_c_ranges[range].max);
435 regmap_update_bits(arizona->regmap,
436 ARIZONA_HEADPHONE_DETECT_1,
437 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
439 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
443 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
444 (val >= arizona_hpdet_c_ranges[range].max)) {
446 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
447 arizona_hpdet_c_ranges[range].min,
448 arizona_hpdet_c_ranges[range].max);
449 regmap_update_bits(arizona->regmap,
450 ARIZONA_HEADPHONE_DETECT_1,
451 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
453 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
458 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
462 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading)
464 struct arizona *arizona = info->arizona;
465 int id_gpio = arizona->pdata.hpdet_id_gpio;
468 * If we're using HPDET for accessory identification we need
469 * to take multiple measurements, step through them in sequence.
471 if (arizona->pdata.hpdet_acc_id) {
472 info->hpdet_res[info->num_hpdet_res++] = *reading;
475 * If the impedence is too high don't measure the
478 if (info->num_hpdet_res == 1 && *reading >= 45) {
479 dev_dbg(arizona->dev, "Skipping ground flip\n");
480 info->hpdet_res[info->num_hpdet_res++] = *reading;
483 if (info->num_hpdet_res == 1) {
484 dev_dbg(arizona->dev, "Flipping ground\n");
486 regmap_update_bits(arizona->regmap,
487 ARIZONA_ACCESSORY_DETECT_MODE_1,
489 ~info->micd_modes[0].src);
491 regmap_update_bits(arizona->regmap,
492 ARIZONA_HEADPHONE_DETECT_1,
493 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
497 /* Only check the mic directly if we didn't already ID it */
498 if (id_gpio && info->num_hpdet_res == 2 &&
499 !((info->hpdet_res[0] > info->hpdet_res[1] * 2))) {
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 %d\n",
519 info->hpdet_res[0], info->hpdet_res[1],
523 /* Take the headphone impedance for the main report */
524 *reading = info->hpdet_res[0];
526 /* Sometimes we get false readings due to slow insert */
527 if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
528 dev_dbg(arizona->dev, "Retrying high impedance\n");
529 info->num_hpdet_res = 0;
530 info->hpdet_retried = true;
531 arizona_start_hpdet_acc_id(info);
532 pm_runtime_put(info->dev);
537 * Either the two grounds measure differently or we
538 * measure the mic as high impedance.
540 if ((info->hpdet_res[0] > info->hpdet_res[1] * 2) ||
541 (id_gpio && info->hpdet_res[2] > 1257)) {
542 dev_dbg(arizona->dev, "Detected mic\n");
544 info->detecting = true;
546 dev_dbg(arizona->dev, "Detected headphone\n");
549 /* Make sure everything is reset back to the real polarity */
550 regmap_update_bits(arizona->regmap,
551 ARIZONA_ACCESSORY_DETECT_MODE_1,
553 info->micd_modes[0].src);
559 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
561 struct arizona_extcon_info *info = data;
562 struct arizona *arizona = info->arizona;
563 int id_gpio = arizona->pdata.hpdet_id_gpio;
564 int report = ARIZONA_CABLE_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, ARIZONA_CABLE_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);
588 if (ret == -EAGAIN) {
590 } else if (ret < 0) {
595 /* Reset back to starting range */
596 regmap_update_bits(arizona->regmap,
597 ARIZONA_HEADPHONE_DETECT_1,
598 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
601 ret = arizona_hpdet_do_id(info, &reading);
602 if (ret == -EAGAIN) {
604 } else if (ret < 0) {
608 /* Report high impedence cables as line outputs */
610 report = ARIZONA_CABLE_LINEOUT;
612 report = ARIZONA_CABLE_HEADPHONE;
614 ret = extcon_set_cable_state_(&info->edev, report, true);
616 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
619 arizona_extcon_do_magic(info, 0);
623 gpio_set_value_cansleep(id_gpio, 0);
625 /* Revert back to MICDET mode */
626 regmap_update_bits(arizona->regmap,
627 ARIZONA_ACCESSORY_DETECT_MODE_1,
628 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
630 /* If we have a mic then reenable MICDET */
632 arizona_start_mic(info);
634 if (info->hpdet_active) {
635 pm_runtime_put_autosuspend(info->dev);
636 info->hpdet_active = false;
639 info->hpdet_done = true;
642 mutex_unlock(&info->lock);
647 static void arizona_identify_headphone(struct arizona_extcon_info *info)
649 struct arizona *arizona = info->arizona;
652 if (info->hpdet_done)
655 dev_dbg(arizona->dev, "Starting HPDET\n");
657 /* Make sure we keep the device enabled during the measurement */
658 pm_runtime_get(info->dev);
660 info->hpdet_active = true;
663 arizona_stop_mic(info);
665 arizona_extcon_do_magic(info, 0x4000);
667 ret = regmap_update_bits(arizona->regmap,
668 ARIZONA_ACCESSORY_DETECT_MODE_1,
669 ARIZONA_ACCDET_MODE_MASK,
670 ARIZONA_ACCDET_MODE_HPL);
672 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
676 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
677 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
679 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
687 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
688 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
690 /* Just report headphone */
691 ret = extcon_update_state(&info->edev,
692 1 << ARIZONA_CABLE_HEADPHONE,
693 1 << ARIZONA_CABLE_HEADPHONE);
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;
708 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
710 /* Make sure we keep the device enabled during the measurement */
711 pm_runtime_get_sync(info->dev);
713 info->hpdet_active = true;
715 arizona_extcon_do_magic(info, 0x4000);
717 ret = regmap_update_bits(arizona->regmap,
718 ARIZONA_ACCESSORY_DETECT_MODE_1,
719 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
720 info->micd_modes[0].src |
721 ARIZONA_ACCDET_MODE_HPL);
723 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
727 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
728 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
730 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
738 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
739 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
741 /* Just report headphone */
742 ret = extcon_update_state(&info->edev,
743 1 << ARIZONA_CABLE_HEADPHONE,
744 1 << ARIZONA_CABLE_HEADPHONE);
746 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
748 info->hpdet_active = false;
751 static irqreturn_t arizona_micdet(int irq, void *data)
753 struct arizona_extcon_info *info = data;
754 struct arizona *arizona = info->arizona;
755 unsigned int val = 0, lvl;
758 mutex_lock(&info->lock);
760 for (i = 0; i < 10 && !(val & 0x7fc); i++) {
761 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
763 dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
764 mutex_unlock(&info->lock);
768 dev_dbg(arizona->dev, "MICDET: %x\n", val);
770 if (!(val & ARIZONA_MICD_VALID)) {
771 dev_warn(arizona->dev, "Microphone detection state invalid\n");
772 mutex_unlock(&info->lock);
777 if (i == 10 && !(val & 0x7fc)) {
778 dev_err(arizona->dev, "Failed to get valid MICDET value\n");
779 mutex_unlock(&info->lock);
783 /* Due to jack detect this should never happen */
784 if (!(val & ARIZONA_MICD_STS)) {
785 dev_warn(arizona->dev, "Detected open circuit\n");
786 info->detecting = false;
790 /* If we got a high impedence we should have a headset, report it. */
791 if (info->detecting && (val & 0x400)) {
792 arizona_identify_headphone(info);
794 ret = extcon_update_state(&info->edev,
795 1 << ARIZONA_CABLE_MICROPHONE,
796 1 << ARIZONA_CABLE_MICROPHONE);
799 dev_err(arizona->dev, "Headset report failed: %d\n",
802 /* Don't need to regulate for button detection */
803 ret = regulator_allow_bypass(info->micvdd, false);
805 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
810 info->detecting = false;
814 /* If we detected a lower impedence during initial startup
815 * then we probably have the wrong polarity, flip it. Don't
816 * do this for the lowest impedences to speed up detection of
817 * plain headphones. If both polarities report a low
818 * impedence then give up and report headphones.
820 if (info->detecting && (val & 0x3f8)) {
821 if (info->jack_flips >= info->micd_num_modes * 10) {
822 dev_dbg(arizona->dev, "Detected HP/line\n");
823 arizona_identify_headphone(info);
825 info->detecting = false;
827 arizona_stop_mic(info);
830 if (info->micd_mode == info->micd_num_modes)
832 arizona_extcon_set_mode(info, info->micd_mode);
841 * If we're still detecting and we detect a short then we've
842 * got a headphone. Otherwise it's a button press.
846 dev_dbg(arizona->dev, "Mic button detected\n");
848 lvl = val & ARIZONA_MICD_LVL_MASK;
849 lvl >>= ARIZONA_MICD_LVL_SHIFT;
852 WARN_ON(ffs(lvl) - 1 >= info->num_micd_ranges);
853 if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
854 key = info->micd_ranges[ffs(lvl) - 1].key;
855 input_report_key(info->input, key, 1);
856 input_sync(info->input);
859 } else if (info->detecting) {
860 dev_dbg(arizona->dev, "Headphone detected\n");
861 info->detecting = false;
862 arizona_stop_mic(info);
864 arizona_identify_headphone(info);
866 dev_warn(arizona->dev, "Button with no mic: %x\n",
870 dev_dbg(arizona->dev, "Mic button released\n");
871 for (i = 0; i < info->num_micd_ranges; i++)
872 input_report_key(info->input,
873 info->micd_ranges[i].key, 0);
874 input_sync(info->input);
875 arizona_extcon_pulse_micbias(info);
879 pm_runtime_mark_last_busy(info->dev);
880 mutex_unlock(&info->lock);
885 static void arizona_hpdet_work(struct work_struct *work)
887 struct arizona_extcon_info *info = container_of(work,
888 struct arizona_extcon_info,
891 mutex_lock(&info->lock);
892 arizona_start_hpdet_acc_id(info);
893 mutex_unlock(&info->lock);
896 static irqreturn_t arizona_jackdet(int irq, void *data)
898 struct arizona_extcon_info *info = data;
899 struct arizona *arizona = info->arizona;
900 unsigned int val, present, mask;
904 cancelled = cancel_delayed_work_sync(&info->hpdet_work);
906 pm_runtime_get_sync(info->dev);
908 mutex_lock(&info->lock);
910 if (arizona->pdata.jd_gpio5) {
911 mask = ARIZONA_MICD_CLAMP_STS;
914 mask = ARIZONA_JD1_STS;
915 present = ARIZONA_JD1_STS;
918 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
920 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
922 mutex_unlock(&info->lock);
923 pm_runtime_put_autosuspend(info->dev);
928 if (val == info->last_jackdet) {
929 dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
931 schedule_delayed_work(&info->hpdet_work,
932 msecs_to_jiffies(HPDET_DEBOUNCE));
936 info->last_jackdet = val;
938 if (info->last_jackdet == present) {
939 dev_dbg(arizona->dev, "Detected jack\n");
940 ret = extcon_set_cable_state_(&info->edev,
941 ARIZONA_CABLE_MECHANICAL, true);
944 dev_err(arizona->dev, "Mechanical report failed: %d\n",
947 if (!arizona->pdata.hpdet_acc_id) {
948 info->detecting = true;
950 info->jack_flips = 0;
952 arizona_start_mic(info);
954 schedule_delayed_work(&info->hpdet_work,
955 msecs_to_jiffies(HPDET_DEBOUNCE));
958 regmap_update_bits(arizona->regmap,
959 ARIZONA_JACK_DETECT_DEBOUNCE,
960 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
962 dev_dbg(arizona->dev, "Detected jack removal\n");
964 arizona_stop_mic(info);
966 info->num_hpdet_res = 0;
967 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
968 info->hpdet_res[i] = 0;
970 info->hpdet_done = false;
971 info->hpdet_retried = false;
973 for (i = 0; i < info->num_micd_ranges; i++)
974 input_report_key(info->input,
975 info->micd_ranges[i].key, 0);
976 input_sync(info->input);
978 ret = extcon_update_state(&info->edev, 0xffffffff, 0);
980 dev_err(arizona->dev, "Removal report failed: %d\n",
983 regmap_update_bits(arizona->regmap,
984 ARIZONA_JACK_DETECT_DEBOUNCE,
985 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
986 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
989 /* Clear trig_sts to make sure DCVDD is not forced up */
990 regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
991 ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
992 ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
993 ARIZONA_JD1_FALL_TRIG_STS |
994 ARIZONA_JD1_RISE_TRIG_STS);
997 mutex_unlock(&info->lock);
999 pm_runtime_mark_last_busy(info->dev);
1000 pm_runtime_put_autosuspend(info->dev);
1005 /* Map a level onto a slot in the register bank */
1006 static void arizona_micd_set_level(struct arizona *arizona, int index,
1012 reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
1021 /* Program the level itself */
1022 regmap_update_bits(arizona->regmap, reg, mask, level);
1025 static int arizona_extcon_probe(struct platform_device *pdev)
1027 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
1028 struct arizona_pdata *pdata;
1029 struct arizona_extcon_info *info;
1031 int jack_irq_fall, jack_irq_rise;
1032 int ret, mode, i, j;
1034 if (!arizona->dapm || !arizona->dapm->card)
1035 return -EPROBE_DEFER;
1037 pdata = dev_get_platdata(arizona->dev);
1039 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1041 dev_err(&pdev->dev, "Failed to allocate memory\n");
1046 info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
1047 if (IS_ERR(info->micvdd)) {
1048 ret = PTR_ERR(info->micvdd);
1049 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
1053 mutex_init(&info->lock);
1054 info->arizona = arizona;
1055 info->dev = &pdev->dev;
1056 info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
1057 INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
1058 platform_set_drvdata(pdev, info);
1060 switch (arizona->type) {
1062 switch (arizona->rev) {
1064 info->micd_reva = true;
1067 info->micd_clamp = true;
1076 info->edev.name = "Headset Jack";
1077 info->edev.supported_cable = arizona_cable;
1079 ret = extcon_dev_register(&info->edev, arizona->dev);
1081 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
1086 info->input = devm_input_allocate_device(&pdev->dev);
1088 dev_err(arizona->dev, "Can't allocate input dev\n");
1093 info->input->name = "Headset";
1094 info->input->phys = "arizona/extcon";
1095 info->input->dev.parent = &pdev->dev;
1097 if (pdata->num_micd_configs) {
1098 info->micd_modes = pdata->micd_configs;
1099 info->micd_num_modes = pdata->num_micd_configs;
1101 info->micd_modes = micd_default_modes;
1102 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1105 if (arizona->pdata.micd_pol_gpio > 0) {
1106 if (info->micd_modes[0].gpio)
1107 mode = GPIOF_OUT_INIT_HIGH;
1109 mode = GPIOF_OUT_INIT_LOW;
1111 ret = devm_gpio_request_one(&pdev->dev,
1112 arizona->pdata.micd_pol_gpio,
1116 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1117 arizona->pdata.micd_pol_gpio, ret);
1122 if (arizona->pdata.hpdet_id_gpio > 0) {
1123 ret = devm_gpio_request_one(&pdev->dev,
1124 arizona->pdata.hpdet_id_gpio,
1128 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1129 arizona->pdata.hpdet_id_gpio, ret);
1134 if (arizona->pdata.micd_bias_start_time)
1135 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1136 ARIZONA_MICD_BIAS_STARTTIME_MASK,
1137 arizona->pdata.micd_bias_start_time
1138 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1140 if (arizona->pdata.micd_rate)
1141 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1142 ARIZONA_MICD_RATE_MASK,
1143 arizona->pdata.micd_rate
1144 << ARIZONA_MICD_RATE_SHIFT);
1146 if (arizona->pdata.micd_dbtime)
1147 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1148 ARIZONA_MICD_DBTIME_MASK,
1149 arizona->pdata.micd_dbtime
1150 << ARIZONA_MICD_DBTIME_SHIFT);
1152 BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) != 0x40);
1154 if (arizona->pdata.num_micd_ranges) {
1155 info->micd_ranges = pdata->micd_ranges;
1156 info->num_micd_ranges = pdata->num_micd_ranges;
1158 info->micd_ranges = micd_default_ranges;
1159 info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
1162 if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_RANGE) {
1163 dev_err(arizona->dev, "Too many MICD ranges: %d\n",
1164 arizona->pdata.num_micd_ranges);
1167 if (info->num_micd_ranges > 1) {
1168 for (i = 1; i < info->num_micd_ranges; i++) {
1169 if (info->micd_ranges[i - 1].max >
1170 info->micd_ranges[i].max) {
1171 dev_err(arizona->dev,
1172 "MICD ranges must be sorted\n");
1179 /* Disable all buttons by default */
1180 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1181 ARIZONA_MICD_LVL_SEL_MASK, 0x81);
1183 /* Set up all the buttons the user specified */
1184 for (i = 0; i < info->num_micd_ranges; i++) {
1185 for (j = 0; j < ARRAY_SIZE(arizona_micd_levels); j++)
1186 if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
1189 if (j == ARRAY_SIZE(arizona_micd_levels)) {
1190 dev_err(arizona->dev, "Unsupported MICD level %d\n",
1191 info->micd_ranges[i].max);
1196 dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
1197 arizona_micd_levels[j], i);
1199 arizona_micd_set_level(arizona, i, j);
1200 input_set_capability(info->input, EV_KEY,
1201 info->micd_ranges[i].key);
1203 /* Enable reporting of that range */
1204 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1208 /* Set all the remaining keys to a maximum */
1209 for (; i < ARIZONA_MAX_MICD_RANGE; i++)
1210 arizona_micd_set_level(arizona, i, 0x3f);
1213 * If we have a clamp use it, activating in conjunction with
1214 * GPIO5 if that is connected for jack detect operation.
1216 if (info->micd_clamp) {
1217 if (arizona->pdata.jd_gpio5) {
1218 /* Put the GPIO into input mode with optional pull */
1220 if (arizona->pdata.jd_gpio5_nopull)
1221 val &= ~ARIZONA_GPN_PU;
1223 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1226 regmap_update_bits(arizona->regmap,
1227 ARIZONA_MICD_CLAMP_CONTROL,
1228 ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
1230 regmap_update_bits(arizona->regmap,
1231 ARIZONA_MICD_CLAMP_CONTROL,
1232 ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
1235 regmap_update_bits(arizona->regmap,
1236 ARIZONA_JACK_DETECT_DEBOUNCE,
1237 ARIZONA_MICD_CLAMP_DB,
1238 ARIZONA_MICD_CLAMP_DB);
1241 arizona_extcon_set_mode(info, 0);
1243 pm_runtime_enable(&pdev->dev);
1244 pm_runtime_idle(&pdev->dev);
1245 pm_runtime_get_sync(&pdev->dev);
1247 if (arizona->pdata.jd_gpio5) {
1248 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1249 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1251 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1252 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1255 ret = arizona_request_irq(arizona, jack_irq_rise,
1256 "JACKDET rise", arizona_jackdet, info);
1258 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1263 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1265 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1270 ret = arizona_request_irq(arizona, jack_irq_fall,
1271 "JACKDET fall", arizona_jackdet, info);
1273 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1277 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1279 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1284 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1285 "MICDET", arizona_micdet, info);
1287 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1291 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1292 "HPDET", arizona_hpdet_irq, info);
1294 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1298 arizona_clk32k_enable(arizona);
1299 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1300 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1301 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1302 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1304 ret = regulator_allow_bypass(info->micvdd, true);
1306 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1309 pm_runtime_put(&pdev->dev);
1311 ret = input_register_device(info->input);
1313 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1320 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1322 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1324 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1326 arizona_free_irq(arizona, jack_irq_fall, info);
1328 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1330 arizona_free_irq(arizona, jack_irq_rise, info);
1333 pm_runtime_disable(&pdev->dev);
1334 extcon_dev_unregister(&info->edev);
1339 static int arizona_extcon_remove(struct platform_device *pdev)
1341 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1342 struct arizona *arizona = info->arizona;
1343 int jack_irq_rise, jack_irq_fall;
1345 pm_runtime_disable(&pdev->dev);
1347 regmap_update_bits(arizona->regmap,
1348 ARIZONA_MICD_CLAMP_CONTROL,
1349 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1351 if (arizona->pdata.jd_gpio5) {
1352 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1353 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1355 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1356 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1359 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1360 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1361 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1362 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1363 arizona_free_irq(arizona, jack_irq_rise, info);
1364 arizona_free_irq(arizona, jack_irq_fall, info);
1365 cancel_delayed_work_sync(&info->hpdet_work);
1366 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1367 ARIZONA_JD1_ENA, 0);
1368 arizona_clk32k_disable(arizona);
1369 extcon_dev_unregister(&info->edev);
1374 static struct platform_driver arizona_extcon_driver = {
1376 .name = "arizona-extcon",
1377 .owner = THIS_MODULE,
1379 .probe = arizona_extcon_probe,
1380 .remove = arizona_extcon_remove,
1383 module_platform_driver(arizona_extcon_driver);
1385 MODULE_DESCRIPTION("Arizona Extcon driver");
1386 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1387 MODULE_LICENSE("GPL");
1388 MODULE_ALIAS("platform:extcon-arizona");