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_NUM_BUTTONS 6
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;
59 unsigned int hpdet_res[3];
67 struct extcon_dev edev;
70 static const struct arizona_micd_config micd_default_modes[] = {
71 { 0, 2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 },
72 { ARIZONA_ACCDET_SRC, 1 << ARIZONA_MICD_BIAS_SRC_SHIFT, 0 },
78 } arizona_lvl_to_key[ARIZONA_NUM_BUTTONS] = {
87 #define ARIZONA_CABLE_MECHANICAL 0
88 #define ARIZONA_CABLE_MICROPHONE 1
89 #define ARIZONA_CABLE_HEADPHONE 2
90 #define ARIZONA_CABLE_LINEOUT 3
92 static const char *arizona_cable[] = {
100 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
102 struct arizona *arizona = info->arizona;
104 if (arizona->pdata.micd_pol_gpio > 0)
105 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
106 info->micd_modes[mode].gpio);
107 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
108 ARIZONA_MICD_BIAS_SRC_MASK,
109 info->micd_modes[mode].bias);
110 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
111 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
113 info->micd_mode = mode;
115 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
118 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
120 switch (info->micd_modes[0].bias >> ARIZONA_MICD_BIAS_SRC_SHIFT) {
132 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
134 struct arizona *arizona = info->arizona;
135 const char *widget = arizona_extcon_get_micbias(info);
136 struct snd_soc_dapm_context *dapm = arizona->dapm;
139 mutex_lock(&dapm->card->dapm_mutex);
141 ret = snd_soc_dapm_force_enable_pin(dapm, widget);
143 dev_warn(arizona->dev, "Failed to enable %s: %d\n",
146 mutex_unlock(&dapm->card->dapm_mutex);
148 snd_soc_dapm_sync(dapm);
150 if (!arizona->pdata.micd_force_micbias) {
151 mutex_lock(&dapm->card->dapm_mutex);
153 ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
155 dev_warn(arizona->dev, "Failed to disable %s: %d\n",
158 mutex_unlock(&dapm->card->dapm_mutex);
160 snd_soc_dapm_sync(dapm);
164 static void arizona_start_mic(struct arizona_extcon_info *info)
166 struct arizona *arizona = info->arizona;
170 /* Microphone detection can't use idle mode */
171 pm_runtime_get(info->dev);
173 if (info->detecting) {
174 ret = regulator_allow_bypass(info->micvdd, false);
176 dev_err(arizona->dev,
177 "Failed to regulate MICVDD: %d\n",
182 ret = regulator_enable(info->micvdd);
184 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
188 if (info->micd_reva) {
189 regmap_write(arizona->regmap, 0x80, 0x3);
190 regmap_write(arizona->regmap, 0x294, 0);
191 regmap_write(arizona->regmap, 0x80, 0x0);
194 regmap_update_bits(arizona->regmap,
195 ARIZONA_ACCESSORY_DETECT_MODE_1,
196 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
198 arizona_extcon_pulse_micbias(info);
200 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
201 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
204 regulator_disable(info->micvdd);
205 pm_runtime_put_autosuspend(info->dev);
209 static void arizona_stop_mic(struct arizona_extcon_info *info)
211 struct arizona *arizona = info->arizona;
212 const char *widget = arizona_extcon_get_micbias(info);
213 struct snd_soc_dapm_context *dapm = arizona->dapm;
217 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
221 mutex_lock(&dapm->card->dapm_mutex);
223 ret = snd_soc_dapm_disable_pin(dapm, widget);
225 dev_warn(arizona->dev,
226 "Failed to disable %s: %d\n",
229 mutex_unlock(&dapm->card->dapm_mutex);
231 snd_soc_dapm_sync(dapm);
233 if (info->micd_reva) {
234 regmap_write(arizona->regmap, 0x80, 0x3);
235 regmap_write(arizona->regmap, 0x294, 2);
236 regmap_write(arizona->regmap, 0x80, 0x0);
239 ret = regulator_allow_bypass(info->micvdd, true);
241 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
246 regulator_disable(info->micvdd);
247 pm_runtime_mark_last_busy(info->dev);
248 pm_runtime_put_autosuspend(info->dev);
253 unsigned int factor_a;
254 unsigned int factor_b;
255 } arizona_hpdet_b_ranges[] = {
264 } arizona_hpdet_c_ranges[] = {
271 static int arizona_hpdet_read(struct arizona_extcon_info *info)
273 struct arizona *arizona = info->arizona;
274 unsigned int val, range;
277 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
279 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
284 switch (info->hpdet_ip) {
286 if (!(val & ARIZONA_HP_DONE)) {
287 dev_err(arizona->dev, "HPDET did not complete: %x\n",
292 val &= ARIZONA_HP_LVL_MASK;
296 if (!(val & ARIZONA_HP_DONE_B)) {
297 dev_err(arizona->dev, "HPDET did not complete: %x\n",
302 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
304 dev_err(arizona->dev, "Failed to read HP value: %d\n",
309 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
311 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
312 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
314 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
315 (val < 100 || val > 0x3fb)) {
317 dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
319 regmap_update_bits(arizona->regmap,
320 ARIZONA_HEADPHONE_DETECT_1,
321 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
323 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
327 /* If we go out of range report top of range */
328 if (val < 100 || val > 0x3fb) {
329 dev_dbg(arizona->dev, "Measurement out of range\n");
333 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
336 val = arizona_hpdet_b_ranges[range].factor_b
338 arizona_hpdet_b_ranges[range].factor_a);
342 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
345 if (!(val & ARIZONA_HP_DONE_B)) {
346 dev_err(arizona->dev, "HPDET did not complete: %x\n",
351 val &= ARIZONA_HP_LVL_B_MASK;
353 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
355 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
356 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
358 /* Skip up or down a range? */
359 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
361 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
362 arizona_hpdet_c_ranges[range].min,
363 arizona_hpdet_c_ranges[range].max);
364 regmap_update_bits(arizona->regmap,
365 ARIZONA_HEADPHONE_DETECT_1,
366 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
368 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
372 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
373 (val >= arizona_hpdet_c_ranges[range].max)) {
375 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
376 arizona_hpdet_c_ranges[range].min,
377 arizona_hpdet_c_ranges[range].max);
378 regmap_update_bits(arizona->regmap,
379 ARIZONA_HEADPHONE_DETECT_1,
380 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
382 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
387 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
391 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading)
393 struct arizona *arizona = info->arizona;
394 int id_gpio = arizona->pdata.hpdet_id_gpio;
398 * If we're using HPDET for accessory identification we need
399 * to take multiple measurements, step through them in sequence.
401 if (arizona->pdata.hpdet_acc_id) {
402 info->hpdet_res[info->num_hpdet_res++] = *reading;
405 * If the impedence is too high don't measure the
408 if (info->num_hpdet_res == 1 && *reading >= 45) {
409 dev_dbg(arizona->dev, "Skipping ground flip\n");
410 info->hpdet_res[info->num_hpdet_res++] = *reading;
413 if (info->num_hpdet_res == 1) {
414 dev_dbg(arizona->dev, "Flipping ground\n");
416 regmap_update_bits(arizona->regmap,
417 ARIZONA_ACCESSORY_DETECT_MODE_1,
419 ~info->micd_modes[0].src);
421 regmap_update_bits(arizona->regmap,
422 ARIZONA_HEADPHONE_DETECT_1,
423 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
427 /* Only check the mic directly if we didn't already ID it */
428 if (id_gpio && info->num_hpdet_res == 2 &&
429 !((info->hpdet_res[0] > info->hpdet_res[1] * 2))) {
430 dev_dbg(arizona->dev, "Measuring mic\n");
432 regmap_update_bits(arizona->regmap,
433 ARIZONA_ACCESSORY_DETECT_MODE_1,
434 ARIZONA_ACCDET_MODE_MASK |
436 ARIZONA_ACCDET_MODE_HPR |
437 info->micd_modes[0].src);
439 gpio_set_value_cansleep(id_gpio, 1);
441 regmap_update_bits(arizona->regmap,
442 ARIZONA_HEADPHONE_DETECT_1,
443 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
447 /* OK, got both. Now, compare... */
448 dev_dbg(arizona->dev, "HPDET measured %d %d %d\n",
449 info->hpdet_res[0], info->hpdet_res[1],
453 * Either the two grounds measure differently or we
454 * measure the mic as high impedance.
456 if ((info->hpdet_res[0] > info->hpdet_res[1] * 2) ||
457 (id_gpio && info->hpdet_res[2] > 10)) {
458 dev_dbg(arizona->dev, "Detected mic\n");
460 ret = extcon_set_cable_state_(&info->edev,
461 ARIZONA_CABLE_MICROPHONE,
464 dev_err(arizona->dev,
465 "Failed to report mic: %d\n", ret);
468 /* Take the headphone impedance for the main report */
469 *reading = info->hpdet_res[1];
471 dev_dbg(arizona->dev, "Detected headphone\n");
474 /* Make sure everything is reset back to the real polarity */
475 regmap_update_bits(arizona->regmap,
476 ARIZONA_ACCESSORY_DETECT_MODE_1,
478 info->micd_modes[0].src);
484 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
486 struct arizona_extcon_info *info = data;
487 struct arizona *arizona = info->arizona;
488 int id_gpio = arizona->pdata.hpdet_id_gpio;
489 int report = ARIZONA_CABLE_HEADPHONE;
492 mutex_lock(&info->lock);
494 /* If we got a spurious IRQ for some reason then ignore it */
495 if (!info->hpdet_active) {
496 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
497 mutex_unlock(&info->lock);
501 /* If the cable was removed while measuring ignore the result */
502 ret = extcon_get_cable_state_(&info->edev, ARIZONA_CABLE_MECHANICAL);
504 dev_err(arizona->dev, "Failed to check cable state: %d\n",
508 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
512 ret = arizona_hpdet_read(info);
513 if (ret == -EAGAIN) {
515 } else if (ret < 0) {
520 /* Reset back to starting range */
521 regmap_update_bits(arizona->regmap,
522 ARIZONA_HEADPHONE_DETECT_1,
523 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
526 ret = arizona_hpdet_do_id(info, &reading);
527 if (ret == -EAGAIN) {
529 } else if (ret < 0) {
533 /* Report high impedence cables as line outputs */
535 report = ARIZONA_CABLE_LINEOUT;
537 report = ARIZONA_CABLE_HEADPHONE;
539 ret = extcon_set_cable_state_(&info->edev, report, true);
541 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
544 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0);
546 dev_warn(arizona->dev, "Failed to undo magic: %d\n", ret);
548 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0);
550 dev_warn(arizona->dev, "Failed to undo magic: %d\n", ret);
554 gpio_set_value_cansleep(id_gpio, 0);
556 /* Revert back to MICDET mode */
557 regmap_update_bits(arizona->regmap,
558 ARIZONA_ACCESSORY_DETECT_MODE_1,
559 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
561 /* If we have a mic then reenable MICDET */
563 arizona_start_mic(info);
565 if (info->hpdet_active) {
566 pm_runtime_put_autosuspend(info->dev);
567 info->hpdet_active = false;
571 mutex_unlock(&info->lock);
576 static void arizona_identify_headphone(struct arizona_extcon_info *info)
578 struct arizona *arizona = info->arizona;
581 dev_dbg(arizona->dev, "Starting HPDET\n");
583 /* Make sure we keep the device enabled during the measurement */
584 pm_runtime_get(info->dev);
586 info->hpdet_active = true;
589 arizona_stop_mic(info);
591 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0x4000);
593 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
595 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0x4000);
597 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
599 ret = regmap_update_bits(arizona->regmap,
600 ARIZONA_ACCESSORY_DETECT_MODE_1,
601 ARIZONA_ACCDET_MODE_MASK,
602 ARIZONA_ACCDET_MODE_HPL);
604 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
608 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
609 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
611 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
619 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
620 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
622 /* Just report headphone */
623 ret = extcon_update_state(&info->edev,
624 1 << ARIZONA_CABLE_HEADPHONE,
625 1 << ARIZONA_CABLE_HEADPHONE);
627 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
630 arizona_start_mic(info);
632 info->hpdet_active = false;
635 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
637 struct arizona *arizona = info->arizona;
640 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
642 /* Make sure we keep the device enabled during the measurement */
643 pm_runtime_get(info->dev);
645 info->hpdet_active = true;
647 arizona_extcon_pulse_micbias(info);
649 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0x4000);
651 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
653 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0x4000);
655 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
657 ret = regmap_update_bits(arizona->regmap,
658 ARIZONA_ACCESSORY_DETECT_MODE_1,
659 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
660 info->micd_modes[0].src |
661 ARIZONA_ACCDET_MODE_HPL);
663 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
667 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
668 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
670 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
678 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
679 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
681 /* Just report headphone */
682 ret = extcon_update_state(&info->edev,
683 1 << ARIZONA_CABLE_HEADPHONE,
684 1 << ARIZONA_CABLE_HEADPHONE);
686 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
688 info->hpdet_active = false;
691 static irqreturn_t arizona_micdet(int irq, void *data)
693 struct arizona_extcon_info *info = data;
694 struct arizona *arizona = info->arizona;
695 unsigned int val, lvl;
698 mutex_lock(&info->lock);
700 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
702 dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
703 mutex_unlock(&info->lock);
707 dev_dbg(arizona->dev, "MICDET: %x\n", val);
709 if (!(val & ARIZONA_MICD_VALID)) {
710 dev_warn(arizona->dev, "Microphone detection state invalid\n");
711 mutex_unlock(&info->lock);
715 /* Due to jack detect this should never happen */
716 if (!(val & ARIZONA_MICD_STS)) {
717 dev_warn(arizona->dev, "Detected open circuit\n");
718 info->detecting = false;
722 /* If we got a high impedence we should have a headset, report it. */
723 if (info->detecting && (val & 0x400)) {
724 arizona_identify_headphone(info);
726 ret = extcon_update_state(&info->edev,
727 1 << ARIZONA_CABLE_MICROPHONE,
728 1 << ARIZONA_CABLE_MICROPHONE);
731 dev_err(arizona->dev, "Headset report failed: %d\n",
734 /* Don't need to regulate for button detection */
735 ret = regulator_allow_bypass(info->micvdd, false);
737 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
742 info->detecting = false;
746 /* If we detected a lower impedence during initial startup
747 * then we probably have the wrong polarity, flip it. Don't
748 * do this for the lowest impedences to speed up detection of
749 * plain headphones. If both polarities report a low
750 * impedence then give up and report headphones.
752 if (info->detecting && (val & 0x3f8)) {
753 if (info->jack_flips >= info->micd_num_modes) {
754 dev_dbg(arizona->dev, "Detected HP/line\n");
755 arizona_identify_headphone(info);
757 info->detecting = false;
759 arizona_stop_mic(info);
762 if (info->micd_mode == info->micd_num_modes)
764 arizona_extcon_set_mode(info, info->micd_mode);
773 * If we're still detecting and we detect a short then we've
774 * got a headphone. Otherwise it's a button press.
778 dev_dbg(arizona->dev, "Mic button detected\n");
780 lvl = val & ARIZONA_MICD_LVL_MASK;
781 lvl >>= ARIZONA_MICD_LVL_SHIFT;
783 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
784 if (lvl & arizona_lvl_to_key[i].status)
785 input_report_key(info->input,
786 arizona_lvl_to_key[i].report,
788 input_sync(info->input);
790 } else if (info->detecting) {
791 dev_dbg(arizona->dev, "Headphone detected\n");
792 info->detecting = false;
793 arizona_stop_mic(info);
795 arizona_identify_headphone(info);
797 dev_warn(arizona->dev, "Button with no mic: %x\n",
801 dev_dbg(arizona->dev, "Mic button released\n");
802 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
803 input_report_key(info->input,
804 arizona_lvl_to_key[i].report, 0);
805 input_sync(info->input);
806 arizona_extcon_pulse_micbias(info);
810 pm_runtime_mark_last_busy(info->dev);
811 mutex_unlock(&info->lock);
816 static irqreturn_t arizona_jackdet(int irq, void *data)
818 struct arizona_extcon_info *info = data;
819 struct arizona *arizona = info->arizona;
820 unsigned int val, present, mask;
823 pm_runtime_get_sync(info->dev);
825 mutex_lock(&info->lock);
827 if (arizona->pdata.jd_gpio5) {
828 mask = ARIZONA_MICD_CLAMP_STS;
831 mask = ARIZONA_JD1_STS;
832 present = ARIZONA_JD1_STS;
835 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
837 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
839 mutex_unlock(&info->lock);
840 pm_runtime_put_autosuspend(info->dev);
844 if ((val & mask) == present) {
845 dev_dbg(arizona->dev, "Detected jack\n");
846 ret = extcon_set_cable_state_(&info->edev,
847 ARIZONA_CABLE_MECHANICAL, true);
850 dev_err(arizona->dev, "Mechanical report failed: %d\n",
853 if (!arizona->pdata.hpdet_acc_id) {
854 info->detecting = true;
856 info->jack_flips = 0;
858 arizona_start_mic(info);
860 arizona_start_hpdet_acc_id(info);
863 regmap_update_bits(arizona->regmap,
864 ARIZONA_JACK_DETECT_DEBOUNCE,
865 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
867 dev_dbg(arizona->dev, "Detected jack removal\n");
869 arizona_stop_mic(info);
871 info->num_hpdet_res = 0;
872 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
873 info->hpdet_res[i] = 0;
876 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
877 input_report_key(info->input,
878 arizona_lvl_to_key[i].report, 0);
879 input_sync(info->input);
881 ret = extcon_update_state(&info->edev, 0xffffffff, 0);
883 dev_err(arizona->dev, "Removal report failed: %d\n",
886 regmap_update_bits(arizona->regmap,
887 ARIZONA_JACK_DETECT_DEBOUNCE,
888 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
889 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
892 mutex_unlock(&info->lock);
894 pm_runtime_mark_last_busy(info->dev);
895 pm_runtime_put_autosuspend(info->dev);
900 static int arizona_extcon_probe(struct platform_device *pdev)
902 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
903 struct arizona_pdata *pdata;
904 struct arizona_extcon_info *info;
905 int jack_irq_fall, jack_irq_rise;
908 if (!arizona->dapm || !arizona->dapm->card)
909 return -EPROBE_DEFER;
911 pdata = dev_get_platdata(arizona->dev);
913 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
915 dev_err(&pdev->dev, "Failed to allocate memory\n");
920 info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
921 if (IS_ERR(info->micvdd)) {
922 ret = PTR_ERR(info->micvdd);
923 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
927 mutex_init(&info->lock);
928 info->arizona = arizona;
929 info->dev = &pdev->dev;
930 platform_set_drvdata(pdev, info);
932 switch (arizona->type) {
934 switch (arizona->rev) {
936 info->micd_reva = true;
939 info->micd_clamp = true;
948 info->edev.name = "Headset Jack";
949 info->edev.supported_cable = arizona_cable;
951 ret = extcon_dev_register(&info->edev, arizona->dev);
953 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
958 if (pdata->num_micd_configs) {
959 info->micd_modes = pdata->micd_configs;
960 info->micd_num_modes = pdata->num_micd_configs;
962 info->micd_modes = micd_default_modes;
963 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
966 if (arizona->pdata.micd_pol_gpio > 0) {
967 if (info->micd_modes[0].gpio)
968 mode = GPIOF_OUT_INIT_HIGH;
970 mode = GPIOF_OUT_INIT_LOW;
972 ret = devm_gpio_request_one(&pdev->dev,
973 arizona->pdata.micd_pol_gpio,
977 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
978 arizona->pdata.micd_pol_gpio, ret);
983 if (arizona->pdata.hpdet_id_gpio > 0) {
984 ret = devm_gpio_request_one(&pdev->dev,
985 arizona->pdata.hpdet_id_gpio,
989 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
990 arizona->pdata.hpdet_id_gpio, ret);
995 if (arizona->pdata.micd_bias_start_time)
996 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
997 ARIZONA_MICD_BIAS_STARTTIME_MASK,
998 arizona->pdata.micd_bias_start_time
999 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1001 if (arizona->pdata.micd_rate)
1002 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1003 ARIZONA_MICD_RATE_MASK,
1004 arizona->pdata.micd_rate
1005 << ARIZONA_MICD_RATE_SHIFT);
1007 if (arizona->pdata.micd_dbtime)
1008 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1009 ARIZONA_MICD_DBTIME_MASK,
1010 arizona->pdata.micd_dbtime
1011 << ARIZONA_MICD_DBTIME_SHIFT);
1014 * If we have a clamp use it, activating in conjunction with
1015 * GPIO5 if that is connected for jack detect operation.
1017 if (info->micd_clamp) {
1018 if (arizona->pdata.jd_gpio5) {
1019 /* Put the GPIO into input mode */
1020 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1023 regmap_update_bits(arizona->regmap,
1024 ARIZONA_MICD_CLAMP_CONTROL,
1025 ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
1027 regmap_update_bits(arizona->regmap,
1028 ARIZONA_MICD_CLAMP_CONTROL,
1029 ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
1032 regmap_update_bits(arizona->regmap,
1033 ARIZONA_JACK_DETECT_DEBOUNCE,
1034 ARIZONA_MICD_CLAMP_DB,
1035 ARIZONA_MICD_CLAMP_DB);
1038 arizona_extcon_set_mode(info, 0);
1040 info->input = devm_input_allocate_device(&pdev->dev);
1042 dev_err(arizona->dev, "Can't allocate input dev\n");
1047 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
1048 input_set_capability(info->input, EV_KEY,
1049 arizona_lvl_to_key[i].report);
1050 info->input->name = "Headset";
1051 info->input->phys = "arizona/extcon";
1052 info->input->dev.parent = &pdev->dev;
1054 pm_runtime_enable(&pdev->dev);
1055 pm_runtime_idle(&pdev->dev);
1056 pm_runtime_get_sync(&pdev->dev);
1058 if (arizona->pdata.jd_gpio5) {
1059 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1060 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1062 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1063 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1066 ret = arizona_request_irq(arizona, jack_irq_rise,
1067 "JACKDET rise", arizona_jackdet, info);
1069 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1074 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1076 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1081 ret = arizona_request_irq(arizona, jack_irq_fall,
1082 "JACKDET fall", arizona_jackdet, info);
1084 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1088 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1090 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1095 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1096 "MICDET", arizona_micdet, info);
1098 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1102 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1103 "HPDET", arizona_hpdet_irq, info);
1105 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1109 arizona_clk32k_enable(arizona);
1110 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1111 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1112 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1113 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1115 ret = regulator_allow_bypass(info->micvdd, true);
1117 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1120 pm_runtime_put(&pdev->dev);
1122 ret = input_register_device(info->input);
1124 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1131 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1133 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1135 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1137 arizona_free_irq(arizona, jack_irq_fall, info);
1139 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1141 arizona_free_irq(arizona, jack_irq_rise, info);
1144 pm_runtime_disable(&pdev->dev);
1145 extcon_dev_unregister(&info->edev);
1150 static int arizona_extcon_remove(struct platform_device *pdev)
1152 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1153 struct arizona *arizona = info->arizona;
1154 int jack_irq_rise, jack_irq_fall;
1156 pm_runtime_disable(&pdev->dev);
1158 regmap_update_bits(arizona->regmap,
1159 ARIZONA_MICD_CLAMP_CONTROL,
1160 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1162 if (arizona->pdata.jd_gpio5) {
1163 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1164 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1166 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1167 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1170 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1171 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1172 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1173 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1174 arizona_free_irq(arizona, jack_irq_rise, info);
1175 arizona_free_irq(arizona, jack_irq_fall, info);
1176 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1177 ARIZONA_JD1_ENA, 0);
1178 arizona_clk32k_disable(arizona);
1179 extcon_dev_unregister(&info->edev);
1184 static struct platform_driver arizona_extcon_driver = {
1186 .name = "arizona-extcon",
1187 .owner = THIS_MODULE,
1189 .probe = arizona_extcon_probe,
1190 .remove = arizona_extcon_remove,
1193 module_platform_driver(arizona_extcon_driver);
1195 MODULE_DESCRIPTION("Arizona Extcon driver");
1196 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1197 MODULE_LICENSE("GPL");
1198 MODULE_ALIAS("platform:extcon-arizona");