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;
61 unsigned int hpdet_res[3];
69 struct extcon_dev edev;
72 static const struct arizona_micd_config micd_default_modes[] = {
73 { 0, 2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 },
74 { ARIZONA_ACCDET_SRC, 1 << ARIZONA_MICD_BIAS_SRC_SHIFT, 0 },
80 } arizona_lvl_to_key[ARIZONA_NUM_BUTTONS] = {
89 #define ARIZONA_CABLE_MECHANICAL 0
90 #define ARIZONA_CABLE_MICROPHONE 1
91 #define ARIZONA_CABLE_HEADPHONE 2
92 #define ARIZONA_CABLE_LINEOUT 3
94 static const char *arizona_cable[] = {
102 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
104 struct arizona *arizona = info->arizona;
106 if (arizona->pdata.micd_pol_gpio > 0)
107 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
108 info->micd_modes[mode].gpio);
109 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
110 ARIZONA_MICD_BIAS_SRC_MASK,
111 info->micd_modes[mode].bias);
112 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
113 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
115 info->micd_mode = mode;
117 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
120 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
122 switch (info->micd_modes[0].bias >> ARIZONA_MICD_BIAS_SRC_SHIFT) {
134 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
136 struct arizona *arizona = info->arizona;
137 const char *widget = arizona_extcon_get_micbias(info);
138 struct snd_soc_dapm_context *dapm = arizona->dapm;
141 mutex_lock(&dapm->card->dapm_mutex);
143 ret = snd_soc_dapm_force_enable_pin(dapm, widget);
145 dev_warn(arizona->dev, "Failed to enable %s: %d\n",
148 mutex_unlock(&dapm->card->dapm_mutex);
150 snd_soc_dapm_sync(dapm);
152 if (!arizona->pdata.micd_force_micbias) {
153 mutex_lock(&dapm->card->dapm_mutex);
155 ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
157 dev_warn(arizona->dev, "Failed to disable %s: %d\n",
160 mutex_unlock(&dapm->card->dapm_mutex);
162 snd_soc_dapm_sync(dapm);
166 static void arizona_start_mic(struct arizona_extcon_info *info)
168 struct arizona *arizona = info->arizona;
172 /* Microphone detection can't use idle mode */
173 pm_runtime_get(info->dev);
175 if (info->detecting) {
176 ret = regulator_allow_bypass(info->micvdd, false);
178 dev_err(arizona->dev,
179 "Failed to regulate MICVDD: %d\n",
184 ret = regulator_enable(info->micvdd);
186 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
190 if (info->micd_reva) {
191 regmap_write(arizona->regmap, 0x80, 0x3);
192 regmap_write(arizona->regmap, 0x294, 0);
193 regmap_write(arizona->regmap, 0x80, 0x0);
196 regmap_update_bits(arizona->regmap,
197 ARIZONA_ACCESSORY_DETECT_MODE_1,
198 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
200 arizona_extcon_pulse_micbias(info);
202 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
203 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
206 regulator_disable(info->micvdd);
207 pm_runtime_put_autosuspend(info->dev);
211 static void arizona_stop_mic(struct arizona_extcon_info *info)
213 struct arizona *arizona = info->arizona;
214 const char *widget = arizona_extcon_get_micbias(info);
215 struct snd_soc_dapm_context *dapm = arizona->dapm;
219 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
223 mutex_lock(&dapm->card->dapm_mutex);
225 ret = snd_soc_dapm_disable_pin(dapm, widget);
227 dev_warn(arizona->dev,
228 "Failed to disable %s: %d\n",
231 mutex_unlock(&dapm->card->dapm_mutex);
233 snd_soc_dapm_sync(dapm);
235 if (info->micd_reva) {
236 regmap_write(arizona->regmap, 0x80, 0x3);
237 regmap_write(arizona->regmap, 0x294, 2);
238 regmap_write(arizona->regmap, 0x80, 0x0);
241 ret = regulator_allow_bypass(info->micvdd, true);
243 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
248 regulator_disable(info->micvdd);
249 pm_runtime_mark_last_busy(info->dev);
250 pm_runtime_put_autosuspend(info->dev);
255 unsigned int factor_a;
256 unsigned int factor_b;
257 } arizona_hpdet_b_ranges[] = {
266 } arizona_hpdet_c_ranges[] = {
273 static int arizona_hpdet_read(struct arizona_extcon_info *info)
275 struct arizona *arizona = info->arizona;
276 unsigned int val, range;
279 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
281 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
286 switch (info->hpdet_ip) {
288 if (!(val & ARIZONA_HP_DONE)) {
289 dev_err(arizona->dev, "HPDET did not complete: %x\n",
294 val &= ARIZONA_HP_LVL_MASK;
298 if (!(val & ARIZONA_HP_DONE_B)) {
299 dev_err(arizona->dev, "HPDET did not complete: %x\n",
304 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
306 dev_err(arizona->dev, "Failed to read HP value: %d\n",
311 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
313 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
314 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
316 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
317 (val < 100 || val > 0x3fb)) {
319 dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
321 regmap_update_bits(arizona->regmap,
322 ARIZONA_HEADPHONE_DETECT_1,
323 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
325 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
329 /* If we go out of range report top of range */
330 if (val < 100 || val > 0x3fb) {
331 dev_dbg(arizona->dev, "Measurement out of range\n");
335 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
338 val = arizona_hpdet_b_ranges[range].factor_b
340 arizona_hpdet_b_ranges[range].factor_a);
344 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
347 if (!(val & ARIZONA_HP_DONE_B)) {
348 dev_err(arizona->dev, "HPDET did not complete: %x\n",
353 val &= ARIZONA_HP_LVL_B_MASK;
355 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
357 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
358 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
360 /* Skip up or down a range? */
361 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
363 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
364 arizona_hpdet_c_ranges[range].min,
365 arizona_hpdet_c_ranges[range].max);
366 regmap_update_bits(arizona->regmap,
367 ARIZONA_HEADPHONE_DETECT_1,
368 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
370 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
374 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
375 (val >= arizona_hpdet_c_ranges[range].max)) {
377 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
378 arizona_hpdet_c_ranges[range].min,
379 arizona_hpdet_c_ranges[range].max);
380 regmap_update_bits(arizona->regmap,
381 ARIZONA_HEADPHONE_DETECT_1,
382 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
384 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
389 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
393 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading)
395 struct arizona *arizona = info->arizona;
396 int id_gpio = arizona->pdata.hpdet_id_gpio;
400 * If we're using HPDET for accessory identification we need
401 * to take multiple measurements, step through them in sequence.
403 if (arizona->pdata.hpdet_acc_id) {
404 info->hpdet_res[info->num_hpdet_res++] = *reading;
407 * If the impedence is too high don't measure the
410 if (info->num_hpdet_res == 1 && *reading >= 45) {
411 dev_dbg(arizona->dev, "Skipping ground flip\n");
412 info->hpdet_res[info->num_hpdet_res++] = *reading;
415 if (info->num_hpdet_res == 1) {
416 dev_dbg(arizona->dev, "Flipping ground\n");
418 regmap_update_bits(arizona->regmap,
419 ARIZONA_ACCESSORY_DETECT_MODE_1,
421 ~info->micd_modes[0].src);
423 regmap_update_bits(arizona->regmap,
424 ARIZONA_HEADPHONE_DETECT_1,
425 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
429 /* Only check the mic directly if we didn't already ID it */
430 if (id_gpio && info->num_hpdet_res == 2 &&
431 !((info->hpdet_res[0] > info->hpdet_res[1] * 2))) {
432 dev_dbg(arizona->dev, "Measuring mic\n");
434 regmap_update_bits(arizona->regmap,
435 ARIZONA_ACCESSORY_DETECT_MODE_1,
436 ARIZONA_ACCDET_MODE_MASK |
438 ARIZONA_ACCDET_MODE_HPR |
439 info->micd_modes[0].src);
441 gpio_set_value_cansleep(id_gpio, 1);
443 regmap_update_bits(arizona->regmap,
444 ARIZONA_HEADPHONE_DETECT_1,
445 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
449 /* OK, got both. Now, compare... */
450 dev_dbg(arizona->dev, "HPDET measured %d %d %d\n",
451 info->hpdet_res[0], info->hpdet_res[1],
455 * Either the two grounds measure differently or we
456 * measure the mic as high impedance.
458 if ((info->hpdet_res[0] > info->hpdet_res[1] * 2) ||
459 (id_gpio && info->hpdet_res[2] > 10)) {
460 dev_dbg(arizona->dev, "Detected mic\n");
462 ret = extcon_set_cable_state_(&info->edev,
463 ARIZONA_CABLE_MICROPHONE,
466 dev_err(arizona->dev,
467 "Failed to report mic: %d\n", ret);
470 /* Take the headphone impedance for the main report */
471 *reading = info->hpdet_res[1];
473 dev_dbg(arizona->dev, "Detected headphone\n");
476 /* Make sure everything is reset back to the real polarity */
477 regmap_update_bits(arizona->regmap,
478 ARIZONA_ACCESSORY_DETECT_MODE_1,
480 info->micd_modes[0].src);
486 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
488 struct arizona_extcon_info *info = data;
489 struct arizona *arizona = info->arizona;
490 int id_gpio = arizona->pdata.hpdet_id_gpio;
491 int report = ARIZONA_CABLE_HEADPHONE;
495 mutex_lock(&info->lock);
497 /* If we got a spurious IRQ for some reason then ignore it */
498 if (!info->hpdet_active) {
499 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
500 mutex_unlock(&info->lock);
504 /* If the cable was removed while measuring ignore the result */
505 ret = extcon_get_cable_state_(&info->edev, ARIZONA_CABLE_MECHANICAL);
507 dev_err(arizona->dev, "Failed to check cable state: %d\n",
511 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
515 ret = arizona_hpdet_read(info);
516 if (ret == -EAGAIN) {
518 } else if (ret < 0) {
523 /* Reset back to starting range */
524 regmap_update_bits(arizona->regmap,
525 ARIZONA_HEADPHONE_DETECT_1,
526 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
529 ret = arizona_hpdet_do_id(info, &reading);
530 if (ret == -EAGAIN) {
532 } else if (ret < 0) {
536 /* Report high impedence cables as line outputs */
538 report = ARIZONA_CABLE_LINEOUT;
540 report = ARIZONA_CABLE_HEADPHONE;
542 ret = extcon_set_cable_state_(&info->edev, report, true);
544 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
547 mutex_lock(&arizona->dapm->card->dapm_mutex);
549 ret = regmap_read(arizona->regmap, ARIZONA_OUTPUT_ENABLES_1, &val);
551 dev_err(arizona->dev, "Failed to read output enables: %d\n",
556 if (!(val & (ARIZONA_OUT1L_ENA | ARIZONA_OUT1R_ENA))) {
557 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0);
559 dev_warn(arizona->dev, "Failed to undo magic: %d\n",
562 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0);
564 dev_warn(arizona->dev, "Failed to undo magic: %d\n",
568 mutex_unlock(&arizona->dapm->card->dapm_mutex);
572 gpio_set_value_cansleep(id_gpio, 0);
574 /* Revert back to MICDET mode */
575 regmap_update_bits(arizona->regmap,
576 ARIZONA_ACCESSORY_DETECT_MODE_1,
577 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
579 /* If we have a mic then reenable MICDET */
581 arizona_start_mic(info);
583 if (info->hpdet_active) {
584 pm_runtime_put_autosuspend(info->dev);
585 info->hpdet_active = false;
589 mutex_unlock(&info->lock);
594 static void arizona_identify_headphone(struct arizona_extcon_info *info)
596 struct arizona *arizona = info->arizona;
599 dev_dbg(arizona->dev, "Starting HPDET\n");
601 /* Make sure we keep the device enabled during the measurement */
602 pm_runtime_get(info->dev);
604 info->hpdet_active = true;
607 arizona_stop_mic(info);
609 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0x4000);
611 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
613 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0x4000);
615 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
617 ret = regmap_update_bits(arizona->regmap,
618 ARIZONA_ACCESSORY_DETECT_MODE_1,
619 ARIZONA_ACCDET_MODE_MASK,
620 ARIZONA_ACCDET_MODE_HPL);
622 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
626 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
627 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
629 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
637 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
638 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
640 /* Just report headphone */
641 ret = extcon_update_state(&info->edev,
642 1 << ARIZONA_CABLE_HEADPHONE,
643 1 << ARIZONA_CABLE_HEADPHONE);
645 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
648 arizona_start_mic(info);
650 info->hpdet_active = false;
653 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
655 struct arizona *arizona = info->arizona;
659 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
661 /* Make sure we keep the device enabled during the measurement */
662 pm_runtime_get_sync(info->dev);
664 info->hpdet_active = true;
666 arizona_extcon_pulse_micbias(info);
668 mutex_lock(&arizona->dapm->card->dapm_mutex);
670 ret = regmap_read(arizona->regmap, ARIZONA_OUTPUT_ENABLES_1, &val);
672 dev_err(arizona->dev, "Failed to read output enables: %d\n",
677 if (!(val & (ARIZONA_OUT1L_ENA | ARIZONA_OUT1R_ENA))) {
678 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000,
681 dev_warn(arizona->dev, "Failed to do magic: %d\n",
684 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000,
687 dev_warn(arizona->dev, "Failed to do magic: %d\n",
691 mutex_unlock(&arizona->dapm->card->dapm_mutex);
693 ret = regmap_update_bits(arizona->regmap,
694 ARIZONA_ACCESSORY_DETECT_MODE_1,
695 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
696 info->micd_modes[0].src |
697 ARIZONA_ACCDET_MODE_HPL);
699 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
703 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
704 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
706 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
714 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
715 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
717 /* Just report headphone */
718 ret = extcon_update_state(&info->edev,
719 1 << ARIZONA_CABLE_HEADPHONE,
720 1 << ARIZONA_CABLE_HEADPHONE);
722 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
724 info->hpdet_active = false;
727 static irqreturn_t arizona_micdet(int irq, void *data)
729 struct arizona_extcon_info *info = data;
730 struct arizona *arizona = info->arizona;
731 unsigned int val, lvl;
734 mutex_lock(&info->lock);
736 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
738 dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
739 mutex_unlock(&info->lock);
743 dev_dbg(arizona->dev, "MICDET: %x\n", val);
745 if (!(val & ARIZONA_MICD_VALID)) {
746 dev_warn(arizona->dev, "Microphone detection state invalid\n");
747 mutex_unlock(&info->lock);
751 /* Due to jack detect this should never happen */
752 if (!(val & ARIZONA_MICD_STS)) {
753 dev_warn(arizona->dev, "Detected open circuit\n");
754 info->detecting = false;
758 /* If we got a high impedence we should have a headset, report it. */
759 if (info->detecting && (val & 0x400)) {
760 arizona_identify_headphone(info);
762 ret = extcon_update_state(&info->edev,
763 1 << ARIZONA_CABLE_MICROPHONE,
764 1 << ARIZONA_CABLE_MICROPHONE);
767 dev_err(arizona->dev, "Headset report failed: %d\n",
770 /* Don't need to regulate for button detection */
771 ret = regulator_allow_bypass(info->micvdd, false);
773 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
778 info->detecting = false;
782 /* If we detected a lower impedence during initial startup
783 * then we probably have the wrong polarity, flip it. Don't
784 * do this for the lowest impedences to speed up detection of
785 * plain headphones. If both polarities report a low
786 * impedence then give up and report headphones.
788 if (info->detecting && (val & 0x3f8)) {
789 if (info->jack_flips >= info->micd_num_modes) {
790 dev_dbg(arizona->dev, "Detected HP/line\n");
791 arizona_identify_headphone(info);
793 info->detecting = false;
795 arizona_stop_mic(info);
798 if (info->micd_mode == info->micd_num_modes)
800 arizona_extcon_set_mode(info, info->micd_mode);
809 * If we're still detecting and we detect a short then we've
810 * got a headphone. Otherwise it's a button press.
814 dev_dbg(arizona->dev, "Mic button detected\n");
816 lvl = val & ARIZONA_MICD_LVL_MASK;
817 lvl >>= ARIZONA_MICD_LVL_SHIFT;
819 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
820 if (lvl & arizona_lvl_to_key[i].status)
821 input_report_key(info->input,
822 arizona_lvl_to_key[i].report,
824 input_sync(info->input);
826 } else if (info->detecting) {
827 dev_dbg(arizona->dev, "Headphone detected\n");
828 info->detecting = false;
829 arizona_stop_mic(info);
831 arizona_identify_headphone(info);
833 dev_warn(arizona->dev, "Button with no mic: %x\n",
837 dev_dbg(arizona->dev, "Mic button released\n");
838 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
839 input_report_key(info->input,
840 arizona_lvl_to_key[i].report, 0);
841 input_sync(info->input);
842 arizona_extcon_pulse_micbias(info);
846 pm_runtime_mark_last_busy(info->dev);
847 mutex_unlock(&info->lock);
852 static void arizona_hpdet_work(struct work_struct *work)
854 struct arizona_extcon_info *info = container_of(work,
855 struct arizona_extcon_info,
858 mutex_lock(&info->lock);
859 arizona_start_hpdet_acc_id(info);
860 mutex_unlock(&info->lock);
863 static irqreturn_t arizona_jackdet(int irq, void *data)
865 struct arizona_extcon_info *info = data;
866 struct arizona *arizona = info->arizona;
867 unsigned int val, present, mask;
870 pm_runtime_get_sync(info->dev);
872 cancel_delayed_work_sync(&info->hpdet_work);
874 mutex_lock(&info->lock);
876 if (arizona->pdata.jd_gpio5) {
877 mask = ARIZONA_MICD_CLAMP_STS;
880 mask = ARIZONA_JD1_STS;
881 present = ARIZONA_JD1_STS;
884 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
886 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
888 mutex_unlock(&info->lock);
889 pm_runtime_put_autosuspend(info->dev);
893 if ((val & mask) == present) {
894 dev_dbg(arizona->dev, "Detected jack\n");
895 ret = extcon_set_cable_state_(&info->edev,
896 ARIZONA_CABLE_MECHANICAL, true);
899 dev_err(arizona->dev, "Mechanical report failed: %d\n",
902 if (!arizona->pdata.hpdet_acc_id) {
903 info->detecting = true;
905 info->jack_flips = 0;
907 arizona_start_mic(info);
909 schedule_delayed_work(&info->hpdet_work,
910 msecs_to_jiffies(250));
913 regmap_update_bits(arizona->regmap,
914 ARIZONA_JACK_DETECT_DEBOUNCE,
915 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
917 dev_dbg(arizona->dev, "Detected jack removal\n");
919 arizona_stop_mic(info);
921 info->num_hpdet_res = 0;
922 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
923 info->hpdet_res[i] = 0;
926 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
927 input_report_key(info->input,
928 arizona_lvl_to_key[i].report, 0);
929 input_sync(info->input);
931 ret = extcon_update_state(&info->edev, 0xffffffff, 0);
933 dev_err(arizona->dev, "Removal report failed: %d\n",
936 regmap_update_bits(arizona->regmap,
937 ARIZONA_JACK_DETECT_DEBOUNCE,
938 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
939 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
942 mutex_unlock(&info->lock);
944 pm_runtime_mark_last_busy(info->dev);
945 pm_runtime_put_autosuspend(info->dev);
950 static int arizona_extcon_probe(struct platform_device *pdev)
952 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
953 struct arizona_pdata *pdata;
954 struct arizona_extcon_info *info;
955 int jack_irq_fall, jack_irq_rise;
958 if (!arizona->dapm || !arizona->dapm->card)
959 return -EPROBE_DEFER;
961 pdata = dev_get_platdata(arizona->dev);
963 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
965 dev_err(&pdev->dev, "Failed to allocate memory\n");
970 info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
971 if (IS_ERR(info->micvdd)) {
972 ret = PTR_ERR(info->micvdd);
973 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
977 mutex_init(&info->lock);
978 info->arizona = arizona;
979 info->dev = &pdev->dev;
980 INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
981 platform_set_drvdata(pdev, info);
983 switch (arizona->type) {
985 switch (arizona->rev) {
987 info->micd_reva = true;
990 info->micd_clamp = true;
999 info->edev.name = "Headset Jack";
1000 info->edev.supported_cable = arizona_cable;
1002 ret = extcon_dev_register(&info->edev, arizona->dev);
1004 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
1009 if (pdata->num_micd_configs) {
1010 info->micd_modes = pdata->micd_configs;
1011 info->micd_num_modes = pdata->num_micd_configs;
1013 info->micd_modes = micd_default_modes;
1014 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1017 if (arizona->pdata.micd_pol_gpio > 0) {
1018 if (info->micd_modes[0].gpio)
1019 mode = GPIOF_OUT_INIT_HIGH;
1021 mode = GPIOF_OUT_INIT_LOW;
1023 ret = devm_gpio_request_one(&pdev->dev,
1024 arizona->pdata.micd_pol_gpio,
1028 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1029 arizona->pdata.micd_pol_gpio, ret);
1034 if (arizona->pdata.hpdet_id_gpio > 0) {
1035 ret = devm_gpio_request_one(&pdev->dev,
1036 arizona->pdata.hpdet_id_gpio,
1040 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1041 arizona->pdata.hpdet_id_gpio, ret);
1046 if (arizona->pdata.micd_bias_start_time)
1047 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1048 ARIZONA_MICD_BIAS_STARTTIME_MASK,
1049 arizona->pdata.micd_bias_start_time
1050 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1052 if (arizona->pdata.micd_rate)
1053 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1054 ARIZONA_MICD_RATE_MASK,
1055 arizona->pdata.micd_rate
1056 << ARIZONA_MICD_RATE_SHIFT);
1058 if (arizona->pdata.micd_dbtime)
1059 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1060 ARIZONA_MICD_DBTIME_MASK,
1061 arizona->pdata.micd_dbtime
1062 << ARIZONA_MICD_DBTIME_SHIFT);
1065 * If we have a clamp use it, activating in conjunction with
1066 * GPIO5 if that is connected for jack detect operation.
1068 if (info->micd_clamp) {
1069 if (arizona->pdata.jd_gpio5) {
1070 /* Put the GPIO into input mode */
1071 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1074 regmap_update_bits(arizona->regmap,
1075 ARIZONA_MICD_CLAMP_CONTROL,
1076 ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
1078 regmap_update_bits(arizona->regmap,
1079 ARIZONA_MICD_CLAMP_CONTROL,
1080 ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
1083 regmap_update_bits(arizona->regmap,
1084 ARIZONA_JACK_DETECT_DEBOUNCE,
1085 ARIZONA_MICD_CLAMP_DB,
1086 ARIZONA_MICD_CLAMP_DB);
1089 arizona_extcon_set_mode(info, 0);
1091 info->input = devm_input_allocate_device(&pdev->dev);
1093 dev_err(arizona->dev, "Can't allocate input dev\n");
1098 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
1099 input_set_capability(info->input, EV_KEY,
1100 arizona_lvl_to_key[i].report);
1101 info->input->name = "Headset";
1102 info->input->phys = "arizona/extcon";
1103 info->input->dev.parent = &pdev->dev;
1105 pm_runtime_enable(&pdev->dev);
1106 pm_runtime_idle(&pdev->dev);
1107 pm_runtime_get_sync(&pdev->dev);
1109 if (arizona->pdata.jd_gpio5) {
1110 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1111 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1113 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1114 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1117 ret = arizona_request_irq(arizona, jack_irq_rise,
1118 "JACKDET rise", arizona_jackdet, info);
1120 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1125 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1127 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1132 ret = arizona_request_irq(arizona, jack_irq_fall,
1133 "JACKDET fall", arizona_jackdet, info);
1135 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1139 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1141 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1146 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1147 "MICDET", arizona_micdet, info);
1149 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1153 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1154 "HPDET", arizona_hpdet_irq, info);
1156 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1160 arizona_clk32k_enable(arizona);
1161 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1162 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1163 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1164 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1166 ret = regulator_allow_bypass(info->micvdd, true);
1168 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1171 pm_runtime_put(&pdev->dev);
1173 ret = input_register_device(info->input);
1175 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1182 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1184 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1186 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1188 arizona_free_irq(arizona, jack_irq_fall, info);
1190 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1192 arizona_free_irq(arizona, jack_irq_rise, info);
1195 pm_runtime_disable(&pdev->dev);
1196 extcon_dev_unregister(&info->edev);
1201 static int arizona_extcon_remove(struct platform_device *pdev)
1203 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1204 struct arizona *arizona = info->arizona;
1205 int jack_irq_rise, jack_irq_fall;
1207 pm_runtime_disable(&pdev->dev);
1209 regmap_update_bits(arizona->regmap,
1210 ARIZONA_MICD_CLAMP_CONTROL,
1211 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1213 if (arizona->pdata.jd_gpio5) {
1214 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1215 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1217 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1218 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1221 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1222 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1223 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1224 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1225 arizona_free_irq(arizona, jack_irq_rise, info);
1226 arizona_free_irq(arizona, jack_irq_fall, info);
1227 cancel_delayed_work_sync(&info->hpdet_work);
1228 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1229 ARIZONA_JD1_ENA, 0);
1230 arizona_clk32k_disable(arizona);
1231 extcon_dev_unregister(&info->edev);
1236 static struct platform_driver arizona_extcon_driver = {
1238 .name = "arizona-extcon",
1239 .owner = THIS_MODULE,
1241 .probe = arizona_extcon_probe,
1242 .remove = arizona_extcon_remove,
1245 module_platform_driver(arizona_extcon_driver);
1247 MODULE_DESCRIPTION("Arizona Extcon driver");
1248 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1249 MODULE_LICENSE("GPL");
1250 MODULE_ALIAS("platform:extcon-arizona");