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 struct arizona_extcon_info {
44 struct arizona *arizona;
46 struct regulator *micvdd;
47 struct input_dev *input;
50 const struct arizona_micd_config *micd_modes;
53 const struct arizona_micd_range *micd_ranges;
59 struct delayed_work hpdet_work;
65 unsigned int hpdet_res[3];
73 struct extcon_dev edev;
76 static const struct arizona_micd_config micd_default_modes[] = {
77 { ARIZONA_ACCDET_SRC, 1 << ARIZONA_MICD_BIAS_SRC_SHIFT, 0 },
78 { 0, 2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 },
81 static const struct arizona_micd_range micd_default_ranges[] = {
82 { .max = 11, .key = BTN_0 },
83 { .max = 28, .key = BTN_1 },
84 { .max = 54, .key = BTN_2 },
85 { .max = 100, .key = BTN_3 },
86 { .max = 186, .key = BTN_4 },
87 { .max = 430, .key = BTN_5 },
90 static const int arizona_micd_levels[] = {
91 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
92 49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
93 105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
94 270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
98 #define ARIZONA_CABLE_MECHANICAL 0
99 #define ARIZONA_CABLE_MICROPHONE 1
100 #define ARIZONA_CABLE_HEADPHONE 2
101 #define ARIZONA_CABLE_LINEOUT 3
103 static const char *arizona_cable[] = {
111 static void arizona_extcon_do_magic(struct arizona_extcon_info *info,
114 struct arizona *arizona = info->arizona;
117 mutex_lock(&arizona->dapm->card->dapm_mutex);
119 arizona->hpdet_magic = magic;
121 /* Keep the HP output stages disabled while doing the magic */
123 ret = regmap_update_bits(arizona->regmap,
124 ARIZONA_OUTPUT_ENABLES_1,
126 ARIZONA_OUT1R_ENA, 0);
128 dev_warn(arizona->dev,
129 "Failed to disable headphone outputs: %d\n",
133 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000,
136 dev_warn(arizona->dev, "Failed to do magic: %d\n",
139 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000,
142 dev_warn(arizona->dev, "Failed to do magic: %d\n",
145 /* Restore the desired state while not doing the magic */
147 ret = regmap_update_bits(arizona->regmap,
148 ARIZONA_OUTPUT_ENABLES_1,
150 ARIZONA_OUT1R_ENA, arizona->hp_ena);
152 dev_warn(arizona->dev,
153 "Failed to restore headphone outputs: %d\n",
157 mutex_unlock(&arizona->dapm->card->dapm_mutex);
160 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
162 struct arizona *arizona = info->arizona;
164 mode %= info->micd_num_modes;
166 if (arizona->pdata.micd_pol_gpio > 0)
167 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
168 info->micd_modes[mode].gpio);
169 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
170 ARIZONA_MICD_BIAS_SRC_MASK,
171 info->micd_modes[mode].bias);
172 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
173 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
175 info->micd_mode = mode;
177 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
180 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
182 switch (info->micd_modes[0].bias >> ARIZONA_MICD_BIAS_SRC_SHIFT) {
194 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
196 struct arizona *arizona = info->arizona;
197 const char *widget = arizona_extcon_get_micbias(info);
198 struct snd_soc_dapm_context *dapm = arizona->dapm;
201 mutex_lock(&dapm->card->dapm_mutex);
203 ret = snd_soc_dapm_force_enable_pin(dapm, widget);
205 dev_warn(arizona->dev, "Failed to enable %s: %d\n",
208 mutex_unlock(&dapm->card->dapm_mutex);
210 snd_soc_dapm_sync(dapm);
212 if (!arizona->pdata.micd_force_micbias) {
213 mutex_lock(&dapm->card->dapm_mutex);
215 ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
217 dev_warn(arizona->dev, "Failed to disable %s: %d\n",
220 mutex_unlock(&dapm->card->dapm_mutex);
222 snd_soc_dapm_sync(dapm);
226 static void arizona_start_mic(struct arizona_extcon_info *info)
228 struct arizona *arizona = info->arizona;
232 /* Microphone detection can't use idle mode */
233 pm_runtime_get(info->dev);
235 if (info->detecting) {
236 ret = regulator_allow_bypass(info->micvdd, false);
238 dev_err(arizona->dev,
239 "Failed to regulate MICVDD: %d\n",
244 ret = regulator_enable(info->micvdd);
246 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
250 if (info->micd_reva) {
251 regmap_write(arizona->regmap, 0x80, 0x3);
252 regmap_write(arizona->regmap, 0x294, 0);
253 regmap_write(arizona->regmap, 0x80, 0x0);
256 regmap_update_bits(arizona->regmap,
257 ARIZONA_ACCESSORY_DETECT_MODE_1,
258 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
260 arizona_extcon_pulse_micbias(info);
262 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
263 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
266 regulator_disable(info->micvdd);
267 pm_runtime_put_autosuspend(info->dev);
271 static void arizona_stop_mic(struct arizona_extcon_info *info)
273 struct arizona *arizona = info->arizona;
274 const char *widget = arizona_extcon_get_micbias(info);
275 struct snd_soc_dapm_context *dapm = arizona->dapm;
279 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
283 mutex_lock(&dapm->card->dapm_mutex);
285 ret = snd_soc_dapm_disable_pin(dapm, widget);
287 dev_warn(arizona->dev,
288 "Failed to disable %s: %d\n",
291 mutex_unlock(&dapm->card->dapm_mutex);
293 snd_soc_dapm_sync(dapm);
295 if (info->micd_reva) {
296 regmap_write(arizona->regmap, 0x80, 0x3);
297 regmap_write(arizona->regmap, 0x294, 2);
298 regmap_write(arizona->regmap, 0x80, 0x0);
301 ret = regulator_allow_bypass(info->micvdd, true);
303 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
308 regulator_disable(info->micvdd);
309 pm_runtime_mark_last_busy(info->dev);
310 pm_runtime_put_autosuspend(info->dev);
315 unsigned int factor_a;
316 unsigned int factor_b;
317 } arizona_hpdet_b_ranges[] = {
326 } arizona_hpdet_c_ranges[] = {
333 static int arizona_hpdet_read(struct arizona_extcon_info *info)
335 struct arizona *arizona = info->arizona;
336 unsigned int val, range;
339 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
341 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
346 switch (info->hpdet_ip) {
348 if (!(val & ARIZONA_HP_DONE)) {
349 dev_err(arizona->dev, "HPDET did not complete: %x\n",
354 val &= ARIZONA_HP_LVL_MASK;
358 if (!(val & ARIZONA_HP_DONE_B)) {
359 dev_err(arizona->dev, "HPDET did not complete: %x\n",
364 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
366 dev_err(arizona->dev, "Failed to read HP value: %d\n",
371 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
373 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
374 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
376 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
377 (val < 100 || val > 0x3fb)) {
379 dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
381 regmap_update_bits(arizona->regmap,
382 ARIZONA_HEADPHONE_DETECT_1,
383 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
385 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
389 /* If we go out of range report top of range */
390 if (val < 100 || val > 0x3fb) {
391 dev_dbg(arizona->dev, "Measurement out of range\n");
395 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
398 val = arizona_hpdet_b_ranges[range].factor_b
400 arizona_hpdet_b_ranges[range].factor_a);
404 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
407 if (!(val & ARIZONA_HP_DONE_B)) {
408 dev_err(arizona->dev, "HPDET did not complete: %x\n",
413 val &= ARIZONA_HP_LVL_B_MASK;
415 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
417 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
418 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
420 /* Skip up or down a range? */
421 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
423 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
424 arizona_hpdet_c_ranges[range].min,
425 arizona_hpdet_c_ranges[range].max);
426 regmap_update_bits(arizona->regmap,
427 ARIZONA_HEADPHONE_DETECT_1,
428 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
430 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
434 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
435 (val >= arizona_hpdet_c_ranges[range].max)) {
437 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
438 arizona_hpdet_c_ranges[range].min,
439 arizona_hpdet_c_ranges[range].max);
440 regmap_update_bits(arizona->regmap,
441 ARIZONA_HEADPHONE_DETECT_1,
442 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
444 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
449 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
453 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading)
455 struct arizona *arizona = info->arizona;
456 int id_gpio = arizona->pdata.hpdet_id_gpio;
459 * If we're using HPDET for accessory identification we need
460 * to take multiple measurements, step through them in sequence.
462 if (arizona->pdata.hpdet_acc_id) {
463 info->hpdet_res[info->num_hpdet_res++] = *reading;
466 * If the impedence is too high don't measure the
469 if (info->num_hpdet_res == 1 && *reading >= 45) {
470 dev_dbg(arizona->dev, "Skipping ground flip\n");
471 info->hpdet_res[info->num_hpdet_res++] = *reading;
474 if (info->num_hpdet_res == 1) {
475 dev_dbg(arizona->dev, "Flipping ground\n");
477 regmap_update_bits(arizona->regmap,
478 ARIZONA_ACCESSORY_DETECT_MODE_1,
480 ~info->micd_modes[0].src);
482 regmap_update_bits(arizona->regmap,
483 ARIZONA_HEADPHONE_DETECT_1,
484 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
488 /* Only check the mic directly if we didn't already ID it */
489 if (id_gpio && info->num_hpdet_res == 2 &&
490 !((info->hpdet_res[0] > info->hpdet_res[1] * 2))) {
491 dev_dbg(arizona->dev, "Measuring mic\n");
493 regmap_update_bits(arizona->regmap,
494 ARIZONA_ACCESSORY_DETECT_MODE_1,
495 ARIZONA_ACCDET_MODE_MASK |
497 ARIZONA_ACCDET_MODE_HPR |
498 info->micd_modes[0].src);
500 gpio_set_value_cansleep(id_gpio, 1);
502 regmap_update_bits(arizona->regmap,
503 ARIZONA_HEADPHONE_DETECT_1,
504 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
508 /* OK, got both. Now, compare... */
509 dev_dbg(arizona->dev, "HPDET measured %d %d %d\n",
510 info->hpdet_res[0], info->hpdet_res[1],
514 /* Take the headphone impedance for the main report */
515 *reading = info->hpdet_res[0];
518 * Either the two grounds measure differently or we
519 * measure the mic as high impedance.
521 if ((info->hpdet_res[0] > info->hpdet_res[1] * 2) ||
522 (id_gpio && info->hpdet_res[2] > 10)) {
523 dev_dbg(arizona->dev, "Detected mic\n");
525 info->detecting = true;
527 dev_dbg(arizona->dev, "Detected headphone\n");
530 /* Make sure everything is reset back to the real polarity */
531 regmap_update_bits(arizona->regmap,
532 ARIZONA_ACCESSORY_DETECT_MODE_1,
534 info->micd_modes[0].src);
540 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
542 struct arizona_extcon_info *info = data;
543 struct arizona *arizona = info->arizona;
544 int id_gpio = arizona->pdata.hpdet_id_gpio;
545 int report = ARIZONA_CABLE_HEADPHONE;
548 mutex_lock(&info->lock);
550 /* If we got a spurious IRQ for some reason then ignore it */
551 if (!info->hpdet_active) {
552 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
553 mutex_unlock(&info->lock);
557 /* If the cable was removed while measuring ignore the result */
558 ret = extcon_get_cable_state_(&info->edev, ARIZONA_CABLE_MECHANICAL);
560 dev_err(arizona->dev, "Failed to check cable state: %d\n",
564 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
568 ret = arizona_hpdet_read(info);
569 if (ret == -EAGAIN) {
571 } else if (ret < 0) {
576 /* Reset back to starting range */
577 regmap_update_bits(arizona->regmap,
578 ARIZONA_HEADPHONE_DETECT_1,
579 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
582 ret = arizona_hpdet_do_id(info, &reading);
583 if (ret == -EAGAIN) {
585 } else if (ret < 0) {
589 /* Report high impedence cables as line outputs */
591 report = ARIZONA_CABLE_LINEOUT;
593 report = ARIZONA_CABLE_HEADPHONE;
595 ret = extcon_set_cable_state_(&info->edev, report, true);
597 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
600 arizona_extcon_do_magic(info, 0);
604 gpio_set_value_cansleep(id_gpio, 0);
606 /* Revert back to MICDET mode */
607 regmap_update_bits(arizona->regmap,
608 ARIZONA_ACCESSORY_DETECT_MODE_1,
609 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
611 /* If we have a mic then reenable MICDET */
613 arizona_start_mic(info);
615 if (info->hpdet_active) {
616 pm_runtime_put_autosuspend(info->dev);
617 info->hpdet_active = false;
620 info->hpdet_done = true;
623 mutex_unlock(&info->lock);
628 static void arizona_identify_headphone(struct arizona_extcon_info *info)
630 struct arizona *arizona = info->arizona;
633 if (info->hpdet_done)
636 dev_dbg(arizona->dev, "Starting HPDET\n");
638 /* Make sure we keep the device enabled during the measurement */
639 pm_runtime_get(info->dev);
641 info->hpdet_active = true;
644 arizona_stop_mic(info);
646 arizona_extcon_do_magic(info, 0x4000);
648 ret = regmap_update_bits(arizona->regmap,
649 ARIZONA_ACCESSORY_DETECT_MODE_1,
650 ARIZONA_ACCDET_MODE_MASK,
651 ARIZONA_ACCDET_MODE_HPL);
653 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
657 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
658 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
660 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
668 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
669 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
671 /* Just report headphone */
672 ret = extcon_update_state(&info->edev,
673 1 << ARIZONA_CABLE_HEADPHONE,
674 1 << ARIZONA_CABLE_HEADPHONE);
676 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
679 arizona_start_mic(info);
681 info->hpdet_active = false;
684 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
686 struct arizona *arizona = info->arizona;
689 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
691 /* Make sure we keep the device enabled during the measurement */
692 pm_runtime_get_sync(info->dev);
694 info->hpdet_active = true;
696 arizona_extcon_do_magic(info, 0x4000);
698 ret = regmap_update_bits(arizona->regmap,
699 ARIZONA_ACCESSORY_DETECT_MODE_1,
700 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
701 info->micd_modes[0].src |
702 ARIZONA_ACCDET_MODE_HPL);
704 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
708 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
709 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
711 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
719 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
720 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
722 /* Just report headphone */
723 ret = extcon_update_state(&info->edev,
724 1 << ARIZONA_CABLE_HEADPHONE,
725 1 << ARIZONA_CABLE_HEADPHONE);
727 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
729 info->hpdet_active = false;
732 static irqreturn_t arizona_micdet(int irq, void *data)
734 struct arizona_extcon_info *info = data;
735 struct arizona *arizona = info->arizona;
736 unsigned int val, lvl;
739 mutex_lock(&info->lock);
741 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
743 dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
744 mutex_unlock(&info->lock);
748 dev_dbg(arizona->dev, "MICDET: %x\n", val);
750 if (!(val & ARIZONA_MICD_VALID)) {
751 dev_warn(arizona->dev, "Microphone detection state invalid\n");
752 mutex_unlock(&info->lock);
756 /* Due to jack detect this should never happen */
757 if (!(val & ARIZONA_MICD_STS)) {
758 dev_warn(arizona->dev, "Detected open circuit\n");
759 info->detecting = false;
763 /* If we got a high impedence we should have a headset, report it. */
764 if (info->detecting && (val & 0x400)) {
765 arizona_identify_headphone(info);
767 ret = extcon_update_state(&info->edev,
768 1 << ARIZONA_CABLE_MICROPHONE,
769 1 << ARIZONA_CABLE_MICROPHONE);
772 dev_err(arizona->dev, "Headset report failed: %d\n",
775 /* Don't need to regulate for button detection */
776 ret = regulator_allow_bypass(info->micvdd, false);
778 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
783 info->detecting = false;
787 /* If we detected a lower impedence during initial startup
788 * then we probably have the wrong polarity, flip it. Don't
789 * do this for the lowest impedences to speed up detection of
790 * plain headphones. If both polarities report a low
791 * impedence then give up and report headphones.
793 if (info->detecting && (val & 0x3f8)) {
794 if (info->jack_flips >= info->micd_num_modes * 10) {
795 dev_dbg(arizona->dev, "Detected HP/line\n");
796 arizona_identify_headphone(info);
798 info->detecting = false;
800 arizona_stop_mic(info);
803 if (info->micd_mode == info->micd_num_modes)
805 arizona_extcon_set_mode(info, info->micd_mode);
814 * If we're still detecting and we detect a short then we've
815 * got a headphone. Otherwise it's a button press.
819 dev_dbg(arizona->dev, "Mic button detected\n");
821 lvl = val & ARIZONA_MICD_LVL_MASK;
822 lvl >>= ARIZONA_MICD_LVL_SHIFT;
825 WARN_ON(ffs(lvl) - 1 >= info->num_micd_ranges);
826 if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
827 key = info->micd_ranges[ffs(lvl) - 1].key;
828 input_report_key(info->input, key, 1);
829 input_sync(info->input);
832 } else if (info->detecting) {
833 dev_dbg(arizona->dev, "Headphone detected\n");
834 info->detecting = false;
835 arizona_stop_mic(info);
837 arizona_identify_headphone(info);
839 dev_warn(arizona->dev, "Button with no mic: %x\n",
843 dev_dbg(arizona->dev, "Mic button released\n");
844 for (i = 0; i < info->num_micd_ranges; i++)
845 input_report_key(info->input,
846 info->micd_ranges[i].key, 0);
847 input_sync(info->input);
848 arizona_extcon_pulse_micbias(info);
852 pm_runtime_mark_last_busy(info->dev);
853 mutex_unlock(&info->lock);
858 static void arizona_hpdet_work(struct work_struct *work)
860 struct arizona_extcon_info *info = container_of(work,
861 struct arizona_extcon_info,
864 mutex_lock(&info->lock);
865 arizona_start_hpdet_acc_id(info);
866 mutex_unlock(&info->lock);
869 static irqreturn_t arizona_jackdet(int irq, void *data)
871 struct arizona_extcon_info *info = data;
872 struct arizona *arizona = info->arizona;
873 unsigned int val, present, mask;
876 pm_runtime_get_sync(info->dev);
878 cancel_delayed_work_sync(&info->hpdet_work);
880 mutex_lock(&info->lock);
882 if (arizona->pdata.jd_gpio5) {
883 mask = ARIZONA_MICD_CLAMP_STS;
886 mask = ARIZONA_JD1_STS;
887 present = ARIZONA_JD1_STS;
890 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
892 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
894 mutex_unlock(&info->lock);
895 pm_runtime_put_autosuspend(info->dev);
899 if ((val & mask) == present) {
900 dev_dbg(arizona->dev, "Detected jack\n");
901 ret = extcon_set_cable_state_(&info->edev,
902 ARIZONA_CABLE_MECHANICAL, true);
905 dev_err(arizona->dev, "Mechanical report failed: %d\n",
908 if (!arizona->pdata.hpdet_acc_id) {
909 info->detecting = true;
911 info->jack_flips = 0;
913 arizona_start_mic(info);
915 schedule_delayed_work(&info->hpdet_work,
916 msecs_to_jiffies(250));
919 regmap_update_bits(arizona->regmap,
920 ARIZONA_JACK_DETECT_DEBOUNCE,
921 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
923 dev_dbg(arizona->dev, "Detected jack removal\n");
925 arizona_stop_mic(info);
927 info->num_hpdet_res = 0;
928 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
929 info->hpdet_res[i] = 0;
931 info->hpdet_done = false;
933 for (i = 0; i < info->num_micd_ranges; i++)
934 input_report_key(info->input,
935 info->micd_ranges[i].key, 0);
936 input_sync(info->input);
938 ret = extcon_update_state(&info->edev, 0xffffffff, 0);
940 dev_err(arizona->dev, "Removal report failed: %d\n",
943 regmap_update_bits(arizona->regmap,
944 ARIZONA_JACK_DETECT_DEBOUNCE,
945 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
946 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
949 /* Clear trig_sts to make sure DCVDD is not forced up */
950 regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
951 ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
952 ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
953 ARIZONA_JD1_FALL_TRIG_STS |
954 ARIZONA_JD1_RISE_TRIG_STS);
956 mutex_unlock(&info->lock);
958 pm_runtime_mark_last_busy(info->dev);
959 pm_runtime_put_autosuspend(info->dev);
964 /* Map a level onto a slot in the register bank */
965 static void arizona_micd_set_level(struct arizona *arizona, int index,
971 reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
980 /* Program the level itself */
981 regmap_update_bits(arizona->regmap, reg, mask, level);
984 static int arizona_extcon_probe(struct platform_device *pdev)
986 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
987 struct arizona_pdata *pdata;
988 struct arizona_extcon_info *info;
989 int jack_irq_fall, jack_irq_rise;
992 if (!arizona->dapm || !arizona->dapm->card)
993 return -EPROBE_DEFER;
995 pdata = dev_get_platdata(arizona->dev);
997 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
999 dev_err(&pdev->dev, "Failed to allocate memory\n");
1004 info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
1005 if (IS_ERR(info->micvdd)) {
1006 ret = PTR_ERR(info->micvdd);
1007 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
1011 mutex_init(&info->lock);
1012 info->arizona = arizona;
1013 info->dev = &pdev->dev;
1014 INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
1015 platform_set_drvdata(pdev, info);
1017 switch (arizona->type) {
1019 switch (arizona->rev) {
1021 info->micd_reva = true;
1024 info->micd_clamp = true;
1033 info->edev.name = "Headset Jack";
1034 info->edev.supported_cable = arizona_cable;
1036 ret = extcon_dev_register(&info->edev, arizona->dev);
1038 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
1043 info->input = devm_input_allocate_device(&pdev->dev);
1045 dev_err(arizona->dev, "Can't allocate input dev\n");
1050 info->input->name = "Headset";
1051 info->input->phys = "arizona/extcon";
1052 info->input->dev.parent = &pdev->dev;
1054 if (pdata->num_micd_configs) {
1055 info->micd_modes = pdata->micd_configs;
1056 info->micd_num_modes = pdata->num_micd_configs;
1058 info->micd_modes = micd_default_modes;
1059 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1062 if (arizona->pdata.micd_pol_gpio > 0) {
1063 if (info->micd_modes[0].gpio)
1064 mode = GPIOF_OUT_INIT_HIGH;
1066 mode = GPIOF_OUT_INIT_LOW;
1068 ret = devm_gpio_request_one(&pdev->dev,
1069 arizona->pdata.micd_pol_gpio,
1073 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1074 arizona->pdata.micd_pol_gpio, ret);
1079 if (arizona->pdata.hpdet_id_gpio > 0) {
1080 ret = devm_gpio_request_one(&pdev->dev,
1081 arizona->pdata.hpdet_id_gpio,
1085 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1086 arizona->pdata.hpdet_id_gpio, ret);
1091 if (arizona->pdata.micd_bias_start_time)
1092 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1093 ARIZONA_MICD_BIAS_STARTTIME_MASK,
1094 arizona->pdata.micd_bias_start_time
1095 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1097 if (arizona->pdata.micd_rate)
1098 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1099 ARIZONA_MICD_RATE_MASK,
1100 arizona->pdata.micd_rate
1101 << ARIZONA_MICD_RATE_SHIFT);
1103 if (arizona->pdata.micd_dbtime)
1104 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1105 ARIZONA_MICD_DBTIME_MASK,
1106 arizona->pdata.micd_dbtime
1107 << ARIZONA_MICD_DBTIME_SHIFT);
1109 BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) != 0x40);
1111 if (arizona->pdata.num_micd_ranges) {
1112 info->micd_ranges = pdata->micd_ranges;
1113 info->num_micd_ranges = pdata->num_micd_ranges;
1115 info->micd_ranges = micd_default_ranges;
1116 info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
1119 if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_RANGE) {
1120 dev_err(arizona->dev, "Too many MICD ranges: %d\n",
1121 arizona->pdata.num_micd_ranges);
1124 if (info->num_micd_ranges > 1) {
1125 for (i = 1; i < info->num_micd_ranges; i++) {
1126 if (info->micd_ranges[i - 1].max >
1127 info->micd_ranges[i].max) {
1128 dev_err(arizona->dev,
1129 "MICD ranges must be sorted\n");
1136 /* Disable all buttons by default */
1137 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1138 ARIZONA_MICD_LVL_SEL_MASK, 0x81);
1140 /* Set up all the buttons the user specified */
1141 for (i = 0; i < info->num_micd_ranges; i++) {
1142 for (j = 0; j < ARRAY_SIZE(arizona_micd_levels); j++)
1143 if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
1146 if (j == ARRAY_SIZE(arizona_micd_levels)) {
1147 dev_err(arizona->dev, "Unsupported MICD level %d\n",
1148 info->micd_ranges[i].max);
1153 dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
1154 arizona_micd_levels[j], i);
1156 arizona_micd_set_level(arizona, i, j);
1157 input_set_capability(info->input, EV_KEY,
1158 info->micd_ranges[i].key);
1160 /* Enable reporting of that range */
1161 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1165 /* Set all the remaining keys to a maximum */
1166 for (; i < ARIZONA_MAX_MICD_RANGE; i++)
1167 arizona_micd_set_level(arizona, i, 0x3f);
1170 * If we have a clamp use it, activating in conjunction with
1171 * GPIO5 if that is connected for jack detect operation.
1173 if (info->micd_clamp) {
1174 if (arizona->pdata.jd_gpio5) {
1175 /* Put the GPIO into input mode */
1176 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1179 regmap_update_bits(arizona->regmap,
1180 ARIZONA_MICD_CLAMP_CONTROL,
1181 ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
1183 regmap_update_bits(arizona->regmap,
1184 ARIZONA_MICD_CLAMP_CONTROL,
1185 ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
1188 regmap_update_bits(arizona->regmap,
1189 ARIZONA_JACK_DETECT_DEBOUNCE,
1190 ARIZONA_MICD_CLAMP_DB,
1191 ARIZONA_MICD_CLAMP_DB);
1194 arizona_extcon_set_mode(info, 0);
1196 pm_runtime_enable(&pdev->dev);
1197 pm_runtime_idle(&pdev->dev);
1198 pm_runtime_get_sync(&pdev->dev);
1200 if (arizona->pdata.jd_gpio5) {
1201 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1202 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1204 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1205 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1208 ret = arizona_request_irq(arizona, jack_irq_rise,
1209 "JACKDET rise", arizona_jackdet, info);
1211 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1216 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1218 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1223 ret = arizona_request_irq(arizona, jack_irq_fall,
1224 "JACKDET fall", arizona_jackdet, info);
1226 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1230 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1232 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1237 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1238 "MICDET", arizona_micdet, info);
1240 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1244 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1245 "HPDET", arizona_hpdet_irq, info);
1247 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1251 arizona_clk32k_enable(arizona);
1252 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1253 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1254 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1255 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1257 ret = regulator_allow_bypass(info->micvdd, true);
1259 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1262 pm_runtime_put(&pdev->dev);
1264 ret = input_register_device(info->input);
1266 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1273 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1275 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1277 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1279 arizona_free_irq(arizona, jack_irq_fall, info);
1281 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1283 arizona_free_irq(arizona, jack_irq_rise, info);
1286 pm_runtime_disable(&pdev->dev);
1287 extcon_dev_unregister(&info->edev);
1292 static int arizona_extcon_remove(struct platform_device *pdev)
1294 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1295 struct arizona *arizona = info->arizona;
1296 int jack_irq_rise, jack_irq_fall;
1298 pm_runtime_disable(&pdev->dev);
1300 regmap_update_bits(arizona->regmap,
1301 ARIZONA_MICD_CLAMP_CONTROL,
1302 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1304 if (arizona->pdata.jd_gpio5) {
1305 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1306 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1308 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1309 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1312 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1313 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1314 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1315 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1316 arizona_free_irq(arizona, jack_irq_rise, info);
1317 arizona_free_irq(arizona, jack_irq_fall, info);
1318 cancel_delayed_work_sync(&info->hpdet_work);
1319 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1320 ARIZONA_JD1_ENA, 0);
1321 arizona_clk32k_disable(arizona);
1322 extcon_dev_unregister(&info->edev);
1327 static struct platform_driver arizona_extcon_driver = {
1329 .name = "arizona-extcon",
1330 .owner = THIS_MODULE,
1332 .probe = arizona_extcon_probe,
1333 .remove = arizona_extcon_remove,
1336 module_platform_driver(arizona_extcon_driver);
1338 MODULE_DESCRIPTION("Arizona Extcon driver");
1339 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1340 MODULE_LICENSE("GPL");
1341 MODULE_ALIAS("platform:extcon-arizona");