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;
56 struct delayed_work hpdet_work;
62 unsigned int hpdet_res[3];
70 struct extcon_dev edev;
73 static const struct arizona_micd_config micd_default_modes[] = {
74 { 0, 2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 },
75 { ARIZONA_ACCDET_SRC, 1 << ARIZONA_MICD_BIAS_SRC_SHIFT, 0 },
81 } arizona_lvl_to_key[ARIZONA_NUM_BUTTONS] = {
90 #define ARIZONA_CABLE_MECHANICAL 0
91 #define ARIZONA_CABLE_MICROPHONE 1
92 #define ARIZONA_CABLE_HEADPHONE 2
93 #define ARIZONA_CABLE_LINEOUT 3
95 static const char *arizona_cable[] = {
103 static void arizona_extcon_do_magic(struct arizona_extcon_info *info,
106 struct arizona *arizona = info->arizona;
109 mutex_lock(&arizona->dapm->card->dapm_mutex);
111 arizona->hpdet_magic = magic;
113 /* Keep the HP output stages disabled while doing the magic */
115 ret = regmap_update_bits(arizona->regmap,
116 ARIZONA_OUTPUT_ENABLES_1,
118 ARIZONA_OUT1R_ENA, 0);
120 dev_warn(arizona->dev,
121 "Failed to disable headphone outputs: %d\n",
125 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000,
128 dev_warn(arizona->dev, "Failed to do magic: %d\n",
131 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000,
134 dev_warn(arizona->dev, "Failed to do magic: %d\n",
137 /* Restore the desired state while not doing the magic */
139 ret = regmap_update_bits(arizona->regmap,
140 ARIZONA_OUTPUT_ENABLES_1,
142 ARIZONA_OUT1R_ENA, arizona->hp_ena);
144 dev_warn(arizona->dev,
145 "Failed to restore headphone outputs: %d\n",
149 mutex_unlock(&arizona->dapm->card->dapm_mutex);
152 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
154 struct arizona *arizona = info->arizona;
156 if (arizona->pdata.micd_pol_gpio > 0)
157 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
158 info->micd_modes[mode].gpio);
159 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
160 ARIZONA_MICD_BIAS_SRC_MASK,
161 info->micd_modes[mode].bias);
162 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
163 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
165 info->micd_mode = mode;
167 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
170 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
172 switch (info->micd_modes[0].bias >> ARIZONA_MICD_BIAS_SRC_SHIFT) {
184 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
186 struct arizona *arizona = info->arizona;
187 const char *widget = arizona_extcon_get_micbias(info);
188 struct snd_soc_dapm_context *dapm = arizona->dapm;
191 mutex_lock(&dapm->card->dapm_mutex);
193 ret = snd_soc_dapm_force_enable_pin(dapm, widget);
195 dev_warn(arizona->dev, "Failed to enable %s: %d\n",
198 mutex_unlock(&dapm->card->dapm_mutex);
200 snd_soc_dapm_sync(dapm);
202 if (!arizona->pdata.micd_force_micbias) {
203 mutex_lock(&dapm->card->dapm_mutex);
205 ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
207 dev_warn(arizona->dev, "Failed to disable %s: %d\n",
210 mutex_unlock(&dapm->card->dapm_mutex);
212 snd_soc_dapm_sync(dapm);
216 static void arizona_start_mic(struct arizona_extcon_info *info)
218 struct arizona *arizona = info->arizona;
222 /* Microphone detection can't use idle mode */
223 pm_runtime_get(info->dev);
225 if (info->detecting) {
226 ret = regulator_allow_bypass(info->micvdd, false);
228 dev_err(arizona->dev,
229 "Failed to regulate MICVDD: %d\n",
234 ret = regulator_enable(info->micvdd);
236 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
240 if (info->micd_reva) {
241 regmap_write(arizona->regmap, 0x80, 0x3);
242 regmap_write(arizona->regmap, 0x294, 0);
243 regmap_write(arizona->regmap, 0x80, 0x0);
246 regmap_update_bits(arizona->regmap,
247 ARIZONA_ACCESSORY_DETECT_MODE_1,
248 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
250 arizona_extcon_pulse_micbias(info);
252 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
253 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
256 regulator_disable(info->micvdd);
257 pm_runtime_put_autosuspend(info->dev);
261 static void arizona_stop_mic(struct arizona_extcon_info *info)
263 struct arizona *arizona = info->arizona;
264 const char *widget = arizona_extcon_get_micbias(info);
265 struct snd_soc_dapm_context *dapm = arizona->dapm;
269 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
273 mutex_lock(&dapm->card->dapm_mutex);
275 ret = snd_soc_dapm_disable_pin(dapm, widget);
277 dev_warn(arizona->dev,
278 "Failed to disable %s: %d\n",
281 mutex_unlock(&dapm->card->dapm_mutex);
283 snd_soc_dapm_sync(dapm);
285 if (info->micd_reva) {
286 regmap_write(arizona->regmap, 0x80, 0x3);
287 regmap_write(arizona->regmap, 0x294, 2);
288 regmap_write(arizona->regmap, 0x80, 0x0);
291 ret = regulator_allow_bypass(info->micvdd, true);
293 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
298 regulator_disable(info->micvdd);
299 pm_runtime_mark_last_busy(info->dev);
300 pm_runtime_put_autosuspend(info->dev);
305 unsigned int factor_a;
306 unsigned int factor_b;
307 } arizona_hpdet_b_ranges[] = {
316 } arizona_hpdet_c_ranges[] = {
323 static int arizona_hpdet_read(struct arizona_extcon_info *info)
325 struct arizona *arizona = info->arizona;
326 unsigned int val, range;
329 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
331 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
336 switch (info->hpdet_ip) {
338 if (!(val & ARIZONA_HP_DONE)) {
339 dev_err(arizona->dev, "HPDET did not complete: %x\n",
344 val &= ARIZONA_HP_LVL_MASK;
348 if (!(val & ARIZONA_HP_DONE_B)) {
349 dev_err(arizona->dev, "HPDET did not complete: %x\n",
354 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
356 dev_err(arizona->dev, "Failed to read HP value: %d\n",
361 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
363 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
364 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
366 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
367 (val < 100 || val > 0x3fb)) {
369 dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
371 regmap_update_bits(arizona->regmap,
372 ARIZONA_HEADPHONE_DETECT_1,
373 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
375 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
379 /* If we go out of range report top of range */
380 if (val < 100 || val > 0x3fb) {
381 dev_dbg(arizona->dev, "Measurement out of range\n");
385 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
388 val = arizona_hpdet_b_ranges[range].factor_b
390 arizona_hpdet_b_ranges[range].factor_a);
394 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
397 if (!(val & ARIZONA_HP_DONE_B)) {
398 dev_err(arizona->dev, "HPDET did not complete: %x\n",
403 val &= ARIZONA_HP_LVL_B_MASK;
405 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
407 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
408 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
410 /* Skip up or down a range? */
411 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
413 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
414 arizona_hpdet_c_ranges[range].min,
415 arizona_hpdet_c_ranges[range].max);
416 regmap_update_bits(arizona->regmap,
417 ARIZONA_HEADPHONE_DETECT_1,
418 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
420 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
424 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
425 (val >= arizona_hpdet_c_ranges[range].max)) {
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);
439 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
443 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading)
445 struct arizona *arizona = info->arizona;
446 int id_gpio = arizona->pdata.hpdet_id_gpio;
449 * If we're using HPDET for accessory identification we need
450 * to take multiple measurements, step through them in sequence.
452 if (arizona->pdata.hpdet_acc_id) {
453 info->hpdet_res[info->num_hpdet_res++] = *reading;
456 * If the impedence is too high don't measure the
459 if (info->num_hpdet_res == 1 && *reading >= 45) {
460 dev_dbg(arizona->dev, "Skipping ground flip\n");
461 info->hpdet_res[info->num_hpdet_res++] = *reading;
464 if (info->num_hpdet_res == 1) {
465 dev_dbg(arizona->dev, "Flipping ground\n");
467 regmap_update_bits(arizona->regmap,
468 ARIZONA_ACCESSORY_DETECT_MODE_1,
470 ~info->micd_modes[0].src);
472 regmap_update_bits(arizona->regmap,
473 ARIZONA_HEADPHONE_DETECT_1,
474 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
478 /* Only check the mic directly if we didn't already ID it */
479 if (id_gpio && info->num_hpdet_res == 2 &&
480 !((info->hpdet_res[0] > info->hpdet_res[1] * 2))) {
481 dev_dbg(arizona->dev, "Measuring mic\n");
483 regmap_update_bits(arizona->regmap,
484 ARIZONA_ACCESSORY_DETECT_MODE_1,
485 ARIZONA_ACCDET_MODE_MASK |
487 ARIZONA_ACCDET_MODE_HPR |
488 info->micd_modes[0].src);
490 gpio_set_value_cansleep(id_gpio, 1);
492 regmap_update_bits(arizona->regmap,
493 ARIZONA_HEADPHONE_DETECT_1,
494 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
498 /* OK, got both. Now, compare... */
499 dev_dbg(arizona->dev, "HPDET measured %d %d %d\n",
500 info->hpdet_res[0], info->hpdet_res[1],
504 /* Take the headphone impedance for the main report */
505 *reading = info->hpdet_res[0];
508 * Either the two grounds measure differently or we
509 * measure the mic as high impedance.
511 if ((info->hpdet_res[0] > info->hpdet_res[1] * 2) ||
512 (id_gpio && info->hpdet_res[2] > 10)) {
513 dev_dbg(arizona->dev, "Detected mic\n");
515 info->detecting = true;
517 dev_dbg(arizona->dev, "Detected headphone\n");
520 /* Make sure everything is reset back to the real polarity */
521 regmap_update_bits(arizona->regmap,
522 ARIZONA_ACCESSORY_DETECT_MODE_1,
524 info->micd_modes[0].src);
530 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
532 struct arizona_extcon_info *info = data;
533 struct arizona *arizona = info->arizona;
534 int id_gpio = arizona->pdata.hpdet_id_gpio;
535 int report = ARIZONA_CABLE_HEADPHONE;
538 mutex_lock(&info->lock);
540 /* If we got a spurious IRQ for some reason then ignore it */
541 if (!info->hpdet_active) {
542 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
543 mutex_unlock(&info->lock);
547 /* If the cable was removed while measuring ignore the result */
548 ret = extcon_get_cable_state_(&info->edev, ARIZONA_CABLE_MECHANICAL);
550 dev_err(arizona->dev, "Failed to check cable state: %d\n",
554 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
558 ret = arizona_hpdet_read(info);
559 if (ret == -EAGAIN) {
561 } else if (ret < 0) {
566 /* Reset back to starting range */
567 regmap_update_bits(arizona->regmap,
568 ARIZONA_HEADPHONE_DETECT_1,
569 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
572 ret = arizona_hpdet_do_id(info, &reading);
573 if (ret == -EAGAIN) {
575 } else if (ret < 0) {
579 /* Report high impedence cables as line outputs */
581 report = ARIZONA_CABLE_LINEOUT;
583 report = ARIZONA_CABLE_HEADPHONE;
585 ret = extcon_set_cable_state_(&info->edev, report, true);
587 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
590 arizona_extcon_do_magic(info, 0);
594 gpio_set_value_cansleep(id_gpio, 0);
596 /* Revert back to MICDET mode */
597 regmap_update_bits(arizona->regmap,
598 ARIZONA_ACCESSORY_DETECT_MODE_1,
599 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
601 /* If we have a mic then reenable MICDET */
603 arizona_start_mic(info);
605 if (info->hpdet_active) {
606 pm_runtime_put_autosuspend(info->dev);
607 info->hpdet_active = false;
610 info->hpdet_done = true;
613 mutex_unlock(&info->lock);
618 static void arizona_identify_headphone(struct arizona_extcon_info *info)
620 struct arizona *arizona = info->arizona;
623 if (info->hpdet_done)
626 dev_dbg(arizona->dev, "Starting HPDET\n");
628 /* Make sure we keep the device enabled during the measurement */
629 pm_runtime_get(info->dev);
631 info->hpdet_active = true;
634 arizona_stop_mic(info);
636 arizona_extcon_do_magic(info, 0x4000);
638 ret = regmap_update_bits(arizona->regmap,
639 ARIZONA_ACCESSORY_DETECT_MODE_1,
640 ARIZONA_ACCDET_MODE_MASK,
641 ARIZONA_ACCDET_MODE_HPL);
643 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
647 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
648 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
650 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
658 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
659 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
661 /* Just report headphone */
662 ret = extcon_update_state(&info->edev,
663 1 << ARIZONA_CABLE_HEADPHONE,
664 1 << ARIZONA_CABLE_HEADPHONE);
666 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
669 arizona_start_mic(info);
671 info->hpdet_active = false;
674 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
676 struct arizona *arizona = info->arizona;
679 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
681 /* Make sure we keep the device enabled during the measurement */
682 pm_runtime_get_sync(info->dev);
684 info->hpdet_active = true;
686 arizona_extcon_pulse_micbias(info);
688 arizona_extcon_do_magic(info, 0x4000);
690 ret = regmap_update_bits(arizona->regmap,
691 ARIZONA_ACCESSORY_DETECT_MODE_1,
692 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
693 info->micd_modes[0].src |
694 ARIZONA_ACCDET_MODE_HPL);
696 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
700 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
701 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
703 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
711 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
712 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
714 /* Just report headphone */
715 ret = extcon_update_state(&info->edev,
716 1 << ARIZONA_CABLE_HEADPHONE,
717 1 << ARIZONA_CABLE_HEADPHONE);
719 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
721 info->hpdet_active = false;
724 static irqreturn_t arizona_micdet(int irq, void *data)
726 struct arizona_extcon_info *info = data;
727 struct arizona *arizona = info->arizona;
728 unsigned int val, lvl;
731 mutex_lock(&info->lock);
733 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
735 dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
736 mutex_unlock(&info->lock);
740 dev_dbg(arizona->dev, "MICDET: %x\n", val);
742 if (!(val & ARIZONA_MICD_VALID)) {
743 dev_warn(arizona->dev, "Microphone detection state invalid\n");
744 mutex_unlock(&info->lock);
748 /* Due to jack detect this should never happen */
749 if (!(val & ARIZONA_MICD_STS)) {
750 dev_warn(arizona->dev, "Detected open circuit\n");
751 info->detecting = false;
755 /* If we got a high impedence we should have a headset, report it. */
756 if (info->detecting && (val & 0x400)) {
757 arizona_identify_headphone(info);
759 ret = extcon_update_state(&info->edev,
760 1 << ARIZONA_CABLE_MICROPHONE,
761 1 << ARIZONA_CABLE_MICROPHONE);
764 dev_err(arizona->dev, "Headset report failed: %d\n",
767 /* Don't need to regulate for button detection */
768 ret = regulator_allow_bypass(info->micvdd, false);
770 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
775 info->detecting = false;
779 /* If we detected a lower impedence during initial startup
780 * then we probably have the wrong polarity, flip it. Don't
781 * do this for the lowest impedences to speed up detection of
782 * plain headphones. If both polarities report a low
783 * impedence then give up and report headphones.
785 if (info->detecting && (val & 0x3f8)) {
786 if (info->jack_flips >= info->micd_num_modes) {
787 dev_dbg(arizona->dev, "Detected HP/line\n");
788 arizona_identify_headphone(info);
790 info->detecting = false;
792 arizona_stop_mic(info);
795 if (info->micd_mode == info->micd_num_modes)
797 arizona_extcon_set_mode(info, info->micd_mode);
806 * If we're still detecting and we detect a short then we've
807 * got a headphone. Otherwise it's a button press.
811 dev_dbg(arizona->dev, "Mic button detected\n");
813 lvl = val & ARIZONA_MICD_LVL_MASK;
814 lvl >>= ARIZONA_MICD_LVL_SHIFT;
816 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
817 if (lvl & arizona_lvl_to_key[i].status)
818 input_report_key(info->input,
819 arizona_lvl_to_key[i].report,
821 input_sync(info->input);
823 } else if (info->detecting) {
824 dev_dbg(arizona->dev, "Headphone detected\n");
825 info->detecting = false;
826 arizona_stop_mic(info);
828 arizona_identify_headphone(info);
830 dev_warn(arizona->dev, "Button with no mic: %x\n",
834 dev_dbg(arizona->dev, "Mic button released\n");
835 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
836 input_report_key(info->input,
837 arizona_lvl_to_key[i].report, 0);
838 input_sync(info->input);
839 arizona_extcon_pulse_micbias(info);
843 pm_runtime_mark_last_busy(info->dev);
844 mutex_unlock(&info->lock);
849 static void arizona_hpdet_work(struct work_struct *work)
851 struct arizona_extcon_info *info = container_of(work,
852 struct arizona_extcon_info,
855 mutex_lock(&info->lock);
856 arizona_start_hpdet_acc_id(info);
857 mutex_unlock(&info->lock);
860 static irqreturn_t arizona_jackdet(int irq, void *data)
862 struct arizona_extcon_info *info = data;
863 struct arizona *arizona = info->arizona;
864 unsigned int val, present, mask;
867 pm_runtime_get_sync(info->dev);
869 cancel_delayed_work_sync(&info->hpdet_work);
871 mutex_lock(&info->lock);
873 if (arizona->pdata.jd_gpio5) {
874 mask = ARIZONA_MICD_CLAMP_STS;
877 mask = ARIZONA_JD1_STS;
878 present = ARIZONA_JD1_STS;
881 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
883 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
885 mutex_unlock(&info->lock);
886 pm_runtime_put_autosuspend(info->dev);
890 if ((val & mask) == present) {
891 dev_dbg(arizona->dev, "Detected jack\n");
892 ret = extcon_set_cable_state_(&info->edev,
893 ARIZONA_CABLE_MECHANICAL, true);
896 dev_err(arizona->dev, "Mechanical report failed: %d\n",
899 if (!arizona->pdata.hpdet_acc_id) {
900 info->detecting = true;
902 info->jack_flips = 0;
904 arizona_start_mic(info);
906 schedule_delayed_work(&info->hpdet_work,
907 msecs_to_jiffies(250));
910 regmap_update_bits(arizona->regmap,
911 ARIZONA_JACK_DETECT_DEBOUNCE,
912 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
914 dev_dbg(arizona->dev, "Detected jack removal\n");
916 arizona_stop_mic(info);
918 info->num_hpdet_res = 0;
919 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
920 info->hpdet_res[i] = 0;
922 info->hpdet_done = false;
924 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
925 input_report_key(info->input,
926 arizona_lvl_to_key[i].report, 0);
927 input_sync(info->input);
929 ret = extcon_update_state(&info->edev, 0xffffffff, 0);
931 dev_err(arizona->dev, "Removal report failed: %d\n",
934 regmap_update_bits(arizona->regmap,
935 ARIZONA_JACK_DETECT_DEBOUNCE,
936 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
937 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
940 /* Clear trig_sts to make sure DCVDD is not forced up */
941 regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
942 ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
943 ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
944 ARIZONA_JD1_FALL_TRIG_STS |
945 ARIZONA_JD1_RISE_TRIG_STS);
947 mutex_unlock(&info->lock);
949 pm_runtime_mark_last_busy(info->dev);
950 pm_runtime_put_autosuspend(info->dev);
955 static int arizona_extcon_probe(struct platform_device *pdev)
957 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
958 struct arizona_pdata *pdata;
959 struct arizona_extcon_info *info;
960 int jack_irq_fall, jack_irq_rise;
963 if (!arizona->dapm || !arizona->dapm->card)
964 return -EPROBE_DEFER;
966 pdata = dev_get_platdata(arizona->dev);
968 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
970 dev_err(&pdev->dev, "Failed to allocate memory\n");
975 info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
976 if (IS_ERR(info->micvdd)) {
977 ret = PTR_ERR(info->micvdd);
978 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
982 mutex_init(&info->lock);
983 info->arizona = arizona;
984 info->dev = &pdev->dev;
985 INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
986 platform_set_drvdata(pdev, info);
988 switch (arizona->type) {
990 switch (arizona->rev) {
992 info->micd_reva = true;
995 info->micd_clamp = true;
1004 info->edev.name = "Headset Jack";
1005 info->edev.supported_cable = arizona_cable;
1007 ret = extcon_dev_register(&info->edev, arizona->dev);
1009 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
1014 if (pdata->num_micd_configs) {
1015 info->micd_modes = pdata->micd_configs;
1016 info->micd_num_modes = pdata->num_micd_configs;
1018 info->micd_modes = micd_default_modes;
1019 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1022 if (arizona->pdata.micd_pol_gpio > 0) {
1023 if (info->micd_modes[0].gpio)
1024 mode = GPIOF_OUT_INIT_HIGH;
1026 mode = GPIOF_OUT_INIT_LOW;
1028 ret = devm_gpio_request_one(&pdev->dev,
1029 arizona->pdata.micd_pol_gpio,
1033 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1034 arizona->pdata.micd_pol_gpio, ret);
1039 if (arizona->pdata.hpdet_id_gpio > 0) {
1040 ret = devm_gpio_request_one(&pdev->dev,
1041 arizona->pdata.hpdet_id_gpio,
1045 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1046 arizona->pdata.hpdet_id_gpio, ret);
1051 if (arizona->pdata.micd_bias_start_time)
1052 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1053 ARIZONA_MICD_BIAS_STARTTIME_MASK,
1054 arizona->pdata.micd_bias_start_time
1055 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1057 if (arizona->pdata.micd_rate)
1058 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1059 ARIZONA_MICD_RATE_MASK,
1060 arizona->pdata.micd_rate
1061 << ARIZONA_MICD_RATE_SHIFT);
1063 if (arizona->pdata.micd_dbtime)
1064 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1065 ARIZONA_MICD_DBTIME_MASK,
1066 arizona->pdata.micd_dbtime
1067 << ARIZONA_MICD_DBTIME_SHIFT);
1070 * If we have a clamp use it, activating in conjunction with
1071 * GPIO5 if that is connected for jack detect operation.
1073 if (info->micd_clamp) {
1074 if (arizona->pdata.jd_gpio5) {
1075 /* Put the GPIO into input mode */
1076 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1079 regmap_update_bits(arizona->regmap,
1080 ARIZONA_MICD_CLAMP_CONTROL,
1081 ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
1083 regmap_update_bits(arizona->regmap,
1084 ARIZONA_MICD_CLAMP_CONTROL,
1085 ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
1088 regmap_update_bits(arizona->regmap,
1089 ARIZONA_JACK_DETECT_DEBOUNCE,
1090 ARIZONA_MICD_CLAMP_DB,
1091 ARIZONA_MICD_CLAMP_DB);
1094 arizona_extcon_set_mode(info, 0);
1096 info->input = devm_input_allocate_device(&pdev->dev);
1098 dev_err(arizona->dev, "Can't allocate input dev\n");
1103 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
1104 input_set_capability(info->input, EV_KEY,
1105 arizona_lvl_to_key[i].report);
1106 info->input->name = "Headset";
1107 info->input->phys = "arizona/extcon";
1108 info->input->dev.parent = &pdev->dev;
1110 pm_runtime_enable(&pdev->dev);
1111 pm_runtime_idle(&pdev->dev);
1112 pm_runtime_get_sync(&pdev->dev);
1114 if (arizona->pdata.jd_gpio5) {
1115 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1116 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1118 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1119 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1122 ret = arizona_request_irq(arizona, jack_irq_rise,
1123 "JACKDET rise", arizona_jackdet, info);
1125 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1130 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1132 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1137 ret = arizona_request_irq(arizona, jack_irq_fall,
1138 "JACKDET fall", arizona_jackdet, info);
1140 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1144 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1146 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1151 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1152 "MICDET", arizona_micdet, info);
1154 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1158 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1159 "HPDET", arizona_hpdet_irq, info);
1161 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1165 arizona_clk32k_enable(arizona);
1166 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1167 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1168 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1169 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1171 ret = regulator_allow_bypass(info->micvdd, true);
1173 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1176 pm_runtime_put(&pdev->dev);
1178 ret = input_register_device(info->input);
1180 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1187 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1189 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1191 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1193 arizona_free_irq(arizona, jack_irq_fall, info);
1195 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1197 arizona_free_irq(arizona, jack_irq_rise, info);
1200 pm_runtime_disable(&pdev->dev);
1201 extcon_dev_unregister(&info->edev);
1206 static int arizona_extcon_remove(struct platform_device *pdev)
1208 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1209 struct arizona *arizona = info->arizona;
1210 int jack_irq_rise, jack_irq_fall;
1212 pm_runtime_disable(&pdev->dev);
1214 regmap_update_bits(arizona->regmap,
1215 ARIZONA_MICD_CLAMP_CONTROL,
1216 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1218 if (arizona->pdata.jd_gpio5) {
1219 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1220 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1222 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1223 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1226 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1227 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1228 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1229 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1230 arizona_free_irq(arizona, jack_irq_rise, info);
1231 arizona_free_irq(arizona, jack_irq_fall, info);
1232 cancel_delayed_work_sync(&info->hpdet_work);
1233 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1234 ARIZONA_JD1_ENA, 0);
1235 arizona_clk32k_disable(arizona);
1236 extcon_dev_unregister(&info->edev);
1241 static struct platform_driver arizona_extcon_driver = {
1243 .name = "arizona-extcon",
1244 .owner = THIS_MODULE,
1246 .probe = arizona_extcon_probe,
1247 .remove = arizona_extcon_remove,
1250 module_platform_driver(arizona_extcon_driver);
1252 MODULE_DESCRIPTION("Arizona Extcon driver");
1253 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1254 MODULE_LICENSE("GPL");
1255 MODULE_ALIAS("platform:extcon-arizona");