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 HPDET_DEBOUNCE 250
44 struct arizona_extcon_info {
46 struct arizona *arizona;
48 struct regulator *micvdd;
49 struct input_dev *input;
54 const struct arizona_micd_config *micd_modes;
57 const struct arizona_micd_range *micd_ranges;
63 struct delayed_work hpdet_work;
69 unsigned int hpdet_res[3];
77 struct extcon_dev edev;
80 static const struct arizona_micd_config micd_default_modes[] = {
81 { ARIZONA_ACCDET_SRC, 1 << ARIZONA_MICD_BIAS_SRC_SHIFT, 0 },
82 { 0, 2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 },
85 static const struct arizona_micd_range micd_default_ranges[] = {
86 { .max = 11, .key = BTN_0 },
87 { .max = 28, .key = BTN_1 },
88 { .max = 54, .key = BTN_2 },
89 { .max = 100, .key = BTN_3 },
90 { .max = 186, .key = BTN_4 },
91 { .max = 430, .key = BTN_5 },
94 static const int arizona_micd_levels[] = {
95 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
96 49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
97 105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
98 270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
102 #define ARIZONA_CABLE_MECHANICAL 0
103 #define ARIZONA_CABLE_MICROPHONE 1
104 #define ARIZONA_CABLE_HEADPHONE 2
105 #define ARIZONA_CABLE_LINEOUT 3
107 static const char *arizona_cable[] = {
115 static void arizona_extcon_do_magic(struct arizona_extcon_info *info,
118 struct arizona *arizona = info->arizona;
121 mutex_lock(&arizona->dapm->card->dapm_mutex);
123 arizona->hpdet_magic = magic;
125 /* Keep the HP output stages disabled while doing the magic */
127 ret = regmap_update_bits(arizona->regmap,
128 ARIZONA_OUTPUT_ENABLES_1,
130 ARIZONA_OUT1R_ENA, 0);
132 dev_warn(arizona->dev,
133 "Failed to disable headphone outputs: %d\n",
137 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000,
140 dev_warn(arizona->dev, "Failed to do magic: %d\n",
143 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000,
146 dev_warn(arizona->dev, "Failed to do magic: %d\n",
149 /* Restore the desired state while not doing the magic */
151 ret = regmap_update_bits(arizona->regmap,
152 ARIZONA_OUTPUT_ENABLES_1,
154 ARIZONA_OUT1R_ENA, arizona->hp_ena);
156 dev_warn(arizona->dev,
157 "Failed to restore headphone outputs: %d\n",
161 mutex_unlock(&arizona->dapm->card->dapm_mutex);
164 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
166 struct arizona *arizona = info->arizona;
168 mode %= info->micd_num_modes;
170 if (arizona->pdata.micd_pol_gpio > 0)
171 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
172 info->micd_modes[mode].gpio);
173 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
174 ARIZONA_MICD_BIAS_SRC_MASK,
175 info->micd_modes[mode].bias);
176 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
177 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
179 info->micd_mode = mode;
181 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
184 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
186 switch (info->micd_modes[0].bias >> ARIZONA_MICD_BIAS_SRC_SHIFT) {
198 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
200 struct arizona *arizona = info->arizona;
201 const char *widget = arizona_extcon_get_micbias(info);
202 struct snd_soc_dapm_context *dapm = arizona->dapm;
205 mutex_lock(&dapm->card->dapm_mutex);
207 ret = snd_soc_dapm_force_enable_pin(dapm, widget);
209 dev_warn(arizona->dev, "Failed to enable %s: %d\n",
212 mutex_unlock(&dapm->card->dapm_mutex);
214 snd_soc_dapm_sync(dapm);
216 if (!arizona->pdata.micd_force_micbias) {
217 mutex_lock(&dapm->card->dapm_mutex);
219 ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
221 dev_warn(arizona->dev, "Failed to disable %s: %d\n",
224 mutex_unlock(&dapm->card->dapm_mutex);
226 snd_soc_dapm_sync(dapm);
230 static void arizona_start_mic(struct arizona_extcon_info *info)
232 struct arizona *arizona = info->arizona;
236 /* Microphone detection can't use idle mode */
237 pm_runtime_get(info->dev);
239 if (info->detecting) {
240 ret = regulator_allow_bypass(info->micvdd, false);
242 dev_err(arizona->dev,
243 "Failed to regulate MICVDD: %d\n",
248 ret = regulator_enable(info->micvdd);
250 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
254 if (info->micd_reva) {
255 regmap_write(arizona->regmap, 0x80, 0x3);
256 regmap_write(arizona->regmap, 0x294, 0);
257 regmap_write(arizona->regmap, 0x80, 0x0);
260 regmap_update_bits(arizona->regmap,
261 ARIZONA_ACCESSORY_DETECT_MODE_1,
262 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
264 arizona_extcon_pulse_micbias(info);
266 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
267 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
270 regulator_disable(info->micvdd);
271 pm_runtime_put_autosuspend(info->dev);
275 static void arizona_stop_mic(struct arizona_extcon_info *info)
277 struct arizona *arizona = info->arizona;
278 const char *widget = arizona_extcon_get_micbias(info);
279 struct snd_soc_dapm_context *dapm = arizona->dapm;
283 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
287 mutex_lock(&dapm->card->dapm_mutex);
289 ret = snd_soc_dapm_disable_pin(dapm, widget);
291 dev_warn(arizona->dev,
292 "Failed to disable %s: %d\n",
295 mutex_unlock(&dapm->card->dapm_mutex);
297 snd_soc_dapm_sync(dapm);
299 if (info->micd_reva) {
300 regmap_write(arizona->regmap, 0x80, 0x3);
301 regmap_write(arizona->regmap, 0x294, 2);
302 regmap_write(arizona->regmap, 0x80, 0x0);
305 ret = regulator_allow_bypass(info->micvdd, true);
307 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
312 regulator_disable(info->micvdd);
313 pm_runtime_mark_last_busy(info->dev);
314 pm_runtime_put_autosuspend(info->dev);
319 unsigned int factor_a;
320 unsigned int factor_b;
321 } arizona_hpdet_b_ranges[] = {
330 } arizona_hpdet_c_ranges[] = {
337 static int arizona_hpdet_read(struct arizona_extcon_info *info)
339 struct arizona *arizona = info->arizona;
340 unsigned int val, range;
343 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
345 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
350 switch (info->hpdet_ip) {
352 if (!(val & ARIZONA_HP_DONE)) {
353 dev_err(arizona->dev, "HPDET did not complete: %x\n",
358 val &= ARIZONA_HP_LVL_MASK;
362 if (!(val & ARIZONA_HP_DONE_B)) {
363 dev_err(arizona->dev, "HPDET did not complete: %x\n",
368 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
370 dev_err(arizona->dev, "Failed to read HP value: %d\n",
375 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
377 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
378 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
380 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
381 (val < 100 || val > 0x3fb)) {
383 dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
385 regmap_update_bits(arizona->regmap,
386 ARIZONA_HEADPHONE_DETECT_1,
387 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
389 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
393 /* If we go out of range report top of range */
394 if (val < 100 || val > 0x3fb) {
395 dev_dbg(arizona->dev, "Measurement out of range\n");
399 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
402 val = arizona_hpdet_b_ranges[range].factor_b
404 arizona_hpdet_b_ranges[range].factor_a);
408 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
411 if (!(val & ARIZONA_HP_DONE_B)) {
412 dev_err(arizona->dev, "HPDET did not complete: %x\n",
417 val &= ARIZONA_HP_LVL_B_MASK;
419 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
421 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
422 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
424 /* Skip up or down a range? */
425 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
427 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
428 arizona_hpdet_c_ranges[range].min,
429 arizona_hpdet_c_ranges[range].max);
430 regmap_update_bits(arizona->regmap,
431 ARIZONA_HEADPHONE_DETECT_1,
432 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
434 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
438 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
439 (val >= arizona_hpdet_c_ranges[range].max)) {
441 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
442 arizona_hpdet_c_ranges[range].min,
443 arizona_hpdet_c_ranges[range].max);
444 regmap_update_bits(arizona->regmap,
445 ARIZONA_HEADPHONE_DETECT_1,
446 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
448 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
453 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
457 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading)
459 struct arizona *arizona = info->arizona;
460 int id_gpio = arizona->pdata.hpdet_id_gpio;
463 * If we're using HPDET for accessory identification we need
464 * to take multiple measurements, step through them in sequence.
466 if (arizona->pdata.hpdet_acc_id) {
467 info->hpdet_res[info->num_hpdet_res++] = *reading;
470 * If the impedence is too high don't measure the
473 if (info->num_hpdet_res == 1 && *reading >= 45) {
474 dev_dbg(arizona->dev, "Skipping ground flip\n");
475 info->hpdet_res[info->num_hpdet_res++] = *reading;
478 if (info->num_hpdet_res == 1) {
479 dev_dbg(arizona->dev, "Flipping ground\n");
481 regmap_update_bits(arizona->regmap,
482 ARIZONA_ACCESSORY_DETECT_MODE_1,
484 ~info->micd_modes[0].src);
486 regmap_update_bits(arizona->regmap,
487 ARIZONA_HEADPHONE_DETECT_1,
488 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
492 /* Only check the mic directly if we didn't already ID it */
493 if (id_gpio && info->num_hpdet_res == 2 &&
494 !((info->hpdet_res[0] > info->hpdet_res[1] * 2))) {
495 dev_dbg(arizona->dev, "Measuring mic\n");
497 regmap_update_bits(arizona->regmap,
498 ARIZONA_ACCESSORY_DETECT_MODE_1,
499 ARIZONA_ACCDET_MODE_MASK |
501 ARIZONA_ACCDET_MODE_HPR |
502 info->micd_modes[0].src);
504 gpio_set_value_cansleep(id_gpio, 1);
506 regmap_update_bits(arizona->regmap,
507 ARIZONA_HEADPHONE_DETECT_1,
508 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
512 /* OK, got both. Now, compare... */
513 dev_dbg(arizona->dev, "HPDET measured %d %d %d\n",
514 info->hpdet_res[0], info->hpdet_res[1],
518 /* Take the headphone impedance for the main report */
519 *reading = info->hpdet_res[0];
522 * Either the two grounds measure differently or we
523 * measure the mic as high impedance.
525 if ((info->hpdet_res[0] > info->hpdet_res[1] * 2) ||
526 (id_gpio && info->hpdet_res[2] > 1257)) {
527 dev_dbg(arizona->dev, "Detected mic\n");
529 info->detecting = true;
531 dev_dbg(arizona->dev, "Detected headphone\n");
534 /* Make sure everything is reset back to the real polarity */
535 regmap_update_bits(arizona->regmap,
536 ARIZONA_ACCESSORY_DETECT_MODE_1,
538 info->micd_modes[0].src);
544 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
546 struct arizona_extcon_info *info = data;
547 struct arizona *arizona = info->arizona;
548 int id_gpio = arizona->pdata.hpdet_id_gpio;
549 int report = ARIZONA_CABLE_HEADPHONE;
552 mutex_lock(&info->lock);
554 /* If we got a spurious IRQ for some reason then ignore it */
555 if (!info->hpdet_active) {
556 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
557 mutex_unlock(&info->lock);
561 /* If the cable was removed while measuring ignore the result */
562 ret = extcon_get_cable_state_(&info->edev, ARIZONA_CABLE_MECHANICAL);
564 dev_err(arizona->dev, "Failed to check cable state: %d\n",
568 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
572 ret = arizona_hpdet_read(info);
573 if (ret == -EAGAIN) {
575 } else if (ret < 0) {
580 /* Reset back to starting range */
581 regmap_update_bits(arizona->regmap,
582 ARIZONA_HEADPHONE_DETECT_1,
583 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
586 ret = arizona_hpdet_do_id(info, &reading);
587 if (ret == -EAGAIN) {
589 } else if (ret < 0) {
593 /* Report high impedence cables as line outputs */
595 report = ARIZONA_CABLE_LINEOUT;
597 report = ARIZONA_CABLE_HEADPHONE;
599 ret = extcon_set_cable_state_(&info->edev, report, true);
601 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
604 arizona_extcon_do_magic(info, 0);
608 gpio_set_value_cansleep(id_gpio, 0);
610 /* Revert back to MICDET mode */
611 regmap_update_bits(arizona->regmap,
612 ARIZONA_ACCESSORY_DETECT_MODE_1,
613 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
615 /* If we have a mic then reenable MICDET */
617 arizona_start_mic(info);
619 if (info->hpdet_active) {
620 pm_runtime_put_autosuspend(info->dev);
621 info->hpdet_active = false;
624 info->hpdet_done = true;
627 mutex_unlock(&info->lock);
632 static void arizona_identify_headphone(struct arizona_extcon_info *info)
634 struct arizona *arizona = info->arizona;
637 if (info->hpdet_done)
640 dev_dbg(arizona->dev, "Starting HPDET\n");
642 /* Make sure we keep the device enabled during the measurement */
643 pm_runtime_get(info->dev);
645 info->hpdet_active = true;
648 arizona_stop_mic(info);
650 arizona_extcon_do_magic(info, 0x4000);
652 ret = regmap_update_bits(arizona->regmap,
653 ARIZONA_ACCESSORY_DETECT_MODE_1,
654 ARIZONA_ACCDET_MODE_MASK,
655 ARIZONA_ACCDET_MODE_HPL);
657 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
661 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
662 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
664 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
672 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
673 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
675 /* Just report headphone */
676 ret = extcon_update_state(&info->edev,
677 1 << ARIZONA_CABLE_HEADPHONE,
678 1 << ARIZONA_CABLE_HEADPHONE);
680 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
683 arizona_start_mic(info);
685 info->hpdet_active = false;
688 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
690 struct arizona *arizona = info->arizona;
693 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
695 /* Make sure we keep the device enabled during the measurement */
696 pm_runtime_get_sync(info->dev);
698 info->hpdet_active = true;
700 arizona_extcon_do_magic(info, 0x4000);
702 ret = regmap_update_bits(arizona->regmap,
703 ARIZONA_ACCESSORY_DETECT_MODE_1,
704 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
705 info->micd_modes[0].src |
706 ARIZONA_ACCDET_MODE_HPL);
708 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
712 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
713 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
715 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
723 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
724 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
726 /* Just report headphone */
727 ret = extcon_update_state(&info->edev,
728 1 << ARIZONA_CABLE_HEADPHONE,
729 1 << ARIZONA_CABLE_HEADPHONE);
731 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
733 info->hpdet_active = false;
736 static irqreturn_t arizona_micdet(int irq, void *data)
738 struct arizona_extcon_info *info = data;
739 struct arizona *arizona = info->arizona;
740 unsigned int val, lvl;
743 mutex_lock(&info->lock);
745 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
747 dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
748 mutex_unlock(&info->lock);
752 dev_dbg(arizona->dev, "MICDET: %x\n", val);
754 if (!(val & ARIZONA_MICD_VALID)) {
755 dev_warn(arizona->dev, "Microphone detection state invalid\n");
756 mutex_unlock(&info->lock);
760 /* Due to jack detect this should never happen */
761 if (!(val & ARIZONA_MICD_STS)) {
762 dev_warn(arizona->dev, "Detected open circuit\n");
763 info->detecting = false;
767 /* If we got a high impedence we should have a headset, report it. */
768 if (info->detecting && (val & 0x400)) {
769 arizona_identify_headphone(info);
771 ret = extcon_update_state(&info->edev,
772 1 << ARIZONA_CABLE_MICROPHONE,
773 1 << ARIZONA_CABLE_MICROPHONE);
776 dev_err(arizona->dev, "Headset report failed: %d\n",
779 /* Don't need to regulate for button detection */
780 ret = regulator_allow_bypass(info->micvdd, false);
782 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
787 info->detecting = false;
791 /* If we detected a lower impedence during initial startup
792 * then we probably have the wrong polarity, flip it. Don't
793 * do this for the lowest impedences to speed up detection of
794 * plain headphones. If both polarities report a low
795 * impedence then give up and report headphones.
797 if (info->detecting && (val & 0x3f8)) {
798 if (info->jack_flips >= info->micd_num_modes * 10) {
799 dev_dbg(arizona->dev, "Detected HP/line\n");
800 arizona_identify_headphone(info);
802 info->detecting = false;
804 arizona_stop_mic(info);
807 if (info->micd_mode == info->micd_num_modes)
809 arizona_extcon_set_mode(info, info->micd_mode);
818 * If we're still detecting and we detect a short then we've
819 * got a headphone. Otherwise it's a button press.
823 dev_dbg(arizona->dev, "Mic button detected\n");
825 lvl = val & ARIZONA_MICD_LVL_MASK;
826 lvl >>= ARIZONA_MICD_LVL_SHIFT;
829 WARN_ON(ffs(lvl) - 1 >= info->num_micd_ranges);
830 if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
831 key = info->micd_ranges[ffs(lvl) - 1].key;
832 input_report_key(info->input, key, 1);
833 input_sync(info->input);
836 } else if (info->detecting) {
837 dev_dbg(arizona->dev, "Headphone detected\n");
838 info->detecting = false;
839 arizona_stop_mic(info);
841 arizona_identify_headphone(info);
843 dev_warn(arizona->dev, "Button with no mic: %x\n",
847 dev_dbg(arizona->dev, "Mic button released\n");
848 for (i = 0; i < info->num_micd_ranges; i++)
849 input_report_key(info->input,
850 info->micd_ranges[i].key, 0);
851 input_sync(info->input);
852 arizona_extcon_pulse_micbias(info);
856 pm_runtime_mark_last_busy(info->dev);
857 mutex_unlock(&info->lock);
862 static void arizona_hpdet_work(struct work_struct *work)
864 struct arizona_extcon_info *info = container_of(work,
865 struct arizona_extcon_info,
868 mutex_lock(&info->lock);
869 arizona_start_hpdet_acc_id(info);
870 mutex_unlock(&info->lock);
873 static irqreturn_t arizona_jackdet(int irq, void *data)
875 struct arizona_extcon_info *info = data;
876 struct arizona *arizona = info->arizona;
877 unsigned int val, present, mask;
881 cancelled = cancel_delayed_work_sync(&info->hpdet_work);
883 pm_runtime_get_sync(info->dev);
885 mutex_lock(&info->lock);
887 if (arizona->pdata.jd_gpio5) {
888 mask = ARIZONA_MICD_CLAMP_STS;
891 mask = ARIZONA_JD1_STS;
892 present = ARIZONA_JD1_STS;
895 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
897 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
899 mutex_unlock(&info->lock);
900 pm_runtime_put_autosuspend(info->dev);
905 if (val == info->last_jackdet) {
906 dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
908 schedule_delayed_work(&info->hpdet_work,
909 msecs_to_jiffies(HPDET_DEBOUNCE));
913 info->last_jackdet = val;
915 if (info->last_jackdet == present) {
916 dev_dbg(arizona->dev, "Detected jack\n");
917 ret = extcon_set_cable_state_(&info->edev,
918 ARIZONA_CABLE_MECHANICAL, true);
921 dev_err(arizona->dev, "Mechanical report failed: %d\n",
924 if (!arizona->pdata.hpdet_acc_id) {
925 info->detecting = true;
927 info->jack_flips = 0;
929 arizona_start_mic(info);
931 schedule_delayed_work(&info->hpdet_work,
932 msecs_to_jiffies(HPDET_DEBOUNCE));
935 regmap_update_bits(arizona->regmap,
936 ARIZONA_JACK_DETECT_DEBOUNCE,
937 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
939 dev_dbg(arizona->dev, "Detected jack removal\n");
941 arizona_stop_mic(info);
943 info->num_hpdet_res = 0;
944 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
945 info->hpdet_res[i] = 0;
947 info->hpdet_done = false;
949 for (i = 0; i < info->num_micd_ranges; i++)
950 input_report_key(info->input,
951 info->micd_ranges[i].key, 0);
952 input_sync(info->input);
954 ret = extcon_update_state(&info->edev, 0xffffffff, 0);
956 dev_err(arizona->dev, "Removal report failed: %d\n",
959 regmap_update_bits(arizona->regmap,
960 ARIZONA_JACK_DETECT_DEBOUNCE,
961 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
962 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
965 /* Clear trig_sts to make sure DCVDD is not forced up */
966 regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
967 ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
968 ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
969 ARIZONA_JD1_FALL_TRIG_STS |
970 ARIZONA_JD1_RISE_TRIG_STS);
973 mutex_unlock(&info->lock);
975 pm_runtime_mark_last_busy(info->dev);
976 pm_runtime_put_autosuspend(info->dev);
981 /* Map a level onto a slot in the register bank */
982 static void arizona_micd_set_level(struct arizona *arizona, int index,
988 reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
997 /* Program the level itself */
998 regmap_update_bits(arizona->regmap, reg, mask, level);
1001 static int arizona_extcon_probe(struct platform_device *pdev)
1003 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
1004 struct arizona_pdata *pdata;
1005 struct arizona_extcon_info *info;
1007 int jack_irq_fall, jack_irq_rise;
1008 int ret, mode, i, j;
1010 if (!arizona->dapm || !arizona->dapm->card)
1011 return -EPROBE_DEFER;
1013 pdata = dev_get_platdata(arizona->dev);
1015 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1017 dev_err(&pdev->dev, "Failed to allocate memory\n");
1022 info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
1023 if (IS_ERR(info->micvdd)) {
1024 ret = PTR_ERR(info->micvdd);
1025 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
1029 mutex_init(&info->lock);
1030 info->arizona = arizona;
1031 info->dev = &pdev->dev;
1032 info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
1033 INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
1034 platform_set_drvdata(pdev, info);
1036 switch (arizona->type) {
1038 switch (arizona->rev) {
1040 info->micd_reva = true;
1043 info->micd_clamp = true;
1052 info->edev.name = "Headset Jack";
1053 info->edev.supported_cable = arizona_cable;
1055 ret = extcon_dev_register(&info->edev, arizona->dev);
1057 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
1062 info->input = devm_input_allocate_device(&pdev->dev);
1064 dev_err(arizona->dev, "Can't allocate input dev\n");
1069 info->input->name = "Headset";
1070 info->input->phys = "arizona/extcon";
1071 info->input->dev.parent = &pdev->dev;
1073 if (pdata->num_micd_configs) {
1074 info->micd_modes = pdata->micd_configs;
1075 info->micd_num_modes = pdata->num_micd_configs;
1077 info->micd_modes = micd_default_modes;
1078 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1081 if (arizona->pdata.micd_pol_gpio > 0) {
1082 if (info->micd_modes[0].gpio)
1083 mode = GPIOF_OUT_INIT_HIGH;
1085 mode = GPIOF_OUT_INIT_LOW;
1087 ret = devm_gpio_request_one(&pdev->dev,
1088 arizona->pdata.micd_pol_gpio,
1092 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1093 arizona->pdata.micd_pol_gpio, ret);
1098 if (arizona->pdata.hpdet_id_gpio > 0) {
1099 ret = devm_gpio_request_one(&pdev->dev,
1100 arizona->pdata.hpdet_id_gpio,
1104 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1105 arizona->pdata.hpdet_id_gpio, ret);
1110 if (arizona->pdata.micd_bias_start_time)
1111 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1112 ARIZONA_MICD_BIAS_STARTTIME_MASK,
1113 arizona->pdata.micd_bias_start_time
1114 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1116 if (arizona->pdata.micd_rate)
1117 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1118 ARIZONA_MICD_RATE_MASK,
1119 arizona->pdata.micd_rate
1120 << ARIZONA_MICD_RATE_SHIFT);
1122 if (arizona->pdata.micd_dbtime)
1123 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1124 ARIZONA_MICD_DBTIME_MASK,
1125 arizona->pdata.micd_dbtime
1126 << ARIZONA_MICD_DBTIME_SHIFT);
1128 BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) != 0x40);
1130 if (arizona->pdata.num_micd_ranges) {
1131 info->micd_ranges = pdata->micd_ranges;
1132 info->num_micd_ranges = pdata->num_micd_ranges;
1134 info->micd_ranges = micd_default_ranges;
1135 info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
1138 if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_RANGE) {
1139 dev_err(arizona->dev, "Too many MICD ranges: %d\n",
1140 arizona->pdata.num_micd_ranges);
1143 if (info->num_micd_ranges > 1) {
1144 for (i = 1; i < info->num_micd_ranges; i++) {
1145 if (info->micd_ranges[i - 1].max >
1146 info->micd_ranges[i].max) {
1147 dev_err(arizona->dev,
1148 "MICD ranges must be sorted\n");
1155 /* Disable all buttons by default */
1156 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1157 ARIZONA_MICD_LVL_SEL_MASK, 0x81);
1159 /* Set up all the buttons the user specified */
1160 for (i = 0; i < info->num_micd_ranges; i++) {
1161 for (j = 0; j < ARRAY_SIZE(arizona_micd_levels); j++)
1162 if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
1165 if (j == ARRAY_SIZE(arizona_micd_levels)) {
1166 dev_err(arizona->dev, "Unsupported MICD level %d\n",
1167 info->micd_ranges[i].max);
1172 dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
1173 arizona_micd_levels[j], i);
1175 arizona_micd_set_level(arizona, i, j);
1176 input_set_capability(info->input, EV_KEY,
1177 info->micd_ranges[i].key);
1179 /* Enable reporting of that range */
1180 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1184 /* Set all the remaining keys to a maximum */
1185 for (; i < ARIZONA_MAX_MICD_RANGE; i++)
1186 arizona_micd_set_level(arizona, i, 0x3f);
1189 * If we have a clamp use it, activating in conjunction with
1190 * GPIO5 if that is connected for jack detect operation.
1192 if (info->micd_clamp) {
1193 if (arizona->pdata.jd_gpio5) {
1194 /* Put the GPIO into input mode with optional pull */
1196 if (arizona->pdata.jd_gpio5_nopull)
1197 val &= ~ARIZONA_GPN_PU;
1199 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1202 regmap_update_bits(arizona->regmap,
1203 ARIZONA_MICD_CLAMP_CONTROL,
1204 ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
1206 regmap_update_bits(arizona->regmap,
1207 ARIZONA_MICD_CLAMP_CONTROL,
1208 ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
1211 regmap_update_bits(arizona->regmap,
1212 ARIZONA_JACK_DETECT_DEBOUNCE,
1213 ARIZONA_MICD_CLAMP_DB,
1214 ARIZONA_MICD_CLAMP_DB);
1217 arizona_extcon_set_mode(info, 0);
1219 pm_runtime_enable(&pdev->dev);
1220 pm_runtime_idle(&pdev->dev);
1221 pm_runtime_get_sync(&pdev->dev);
1223 if (arizona->pdata.jd_gpio5) {
1224 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1225 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1227 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1228 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1231 ret = arizona_request_irq(arizona, jack_irq_rise,
1232 "JACKDET rise", arizona_jackdet, info);
1234 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1239 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1241 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1246 ret = arizona_request_irq(arizona, jack_irq_fall,
1247 "JACKDET fall", arizona_jackdet, info);
1249 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1253 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1255 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1260 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1261 "MICDET", arizona_micdet, info);
1263 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1267 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1268 "HPDET", arizona_hpdet_irq, info);
1270 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1274 arizona_clk32k_enable(arizona);
1275 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1276 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1277 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1278 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1280 ret = regulator_allow_bypass(info->micvdd, true);
1282 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1285 pm_runtime_put(&pdev->dev);
1287 ret = input_register_device(info->input);
1289 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1296 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1298 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1300 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1302 arizona_free_irq(arizona, jack_irq_fall, info);
1304 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1306 arizona_free_irq(arizona, jack_irq_rise, info);
1309 pm_runtime_disable(&pdev->dev);
1310 extcon_dev_unregister(&info->edev);
1315 static int arizona_extcon_remove(struct platform_device *pdev)
1317 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1318 struct arizona *arizona = info->arizona;
1319 int jack_irq_rise, jack_irq_fall;
1321 pm_runtime_disable(&pdev->dev);
1323 regmap_update_bits(arizona->regmap,
1324 ARIZONA_MICD_CLAMP_CONTROL,
1325 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1327 if (arizona->pdata.jd_gpio5) {
1328 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1329 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1331 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1332 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1335 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1336 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1337 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1338 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1339 arizona_free_irq(arizona, jack_irq_rise, info);
1340 arizona_free_irq(arizona, jack_irq_fall, info);
1341 cancel_delayed_work_sync(&info->hpdet_work);
1342 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1343 ARIZONA_JD1_ENA, 0);
1344 arizona_clk32k_disable(arizona);
1345 extcon_dev_unregister(&info->edev);
1350 static struct platform_driver arizona_extcon_driver = {
1352 .name = "arizona-extcon",
1353 .owner = THIS_MODULE,
1355 .probe = arizona_extcon_probe,
1356 .remove = arizona_extcon_remove,
1359 module_platform_driver(arizona_extcon_driver);
1361 MODULE_DESCRIPTION("Arizona Extcon driver");
1362 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1363 MODULE_LICENSE("GPL");
1364 MODULE_ALIAS("platform:extcon-arizona");