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 /* Take the headphone impedance for the main report */
456 *reading = info->hpdet_res[0];
459 * Either the two grounds measure differently or we
460 * measure the mic as high impedance.
462 if ((info->hpdet_res[0] > info->hpdet_res[1] * 2) ||
463 (id_gpio && info->hpdet_res[2] > 10)) {
464 dev_dbg(arizona->dev, "Detected mic\n");
466 ret = extcon_set_cable_state_(&info->edev,
467 ARIZONA_CABLE_MICROPHONE,
470 dev_err(arizona->dev,
471 "Failed to report mic: %d\n", ret);
474 dev_dbg(arizona->dev, "Detected headphone\n");
477 /* Make sure everything is reset back to the real polarity */
478 regmap_update_bits(arizona->regmap,
479 ARIZONA_ACCESSORY_DETECT_MODE_1,
481 info->micd_modes[0].src);
487 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
489 struct arizona_extcon_info *info = data;
490 struct arizona *arizona = info->arizona;
491 int id_gpio = arizona->pdata.hpdet_id_gpio;
492 int report = ARIZONA_CABLE_HEADPHONE;
496 mutex_lock(&info->lock);
498 /* If we got a spurious IRQ for some reason then ignore it */
499 if (!info->hpdet_active) {
500 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
501 mutex_unlock(&info->lock);
505 /* If the cable was removed while measuring ignore the result */
506 ret = extcon_get_cable_state_(&info->edev, ARIZONA_CABLE_MECHANICAL);
508 dev_err(arizona->dev, "Failed to check cable state: %d\n",
512 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
516 ret = arizona_hpdet_read(info);
517 if (ret == -EAGAIN) {
519 } else if (ret < 0) {
524 /* Reset back to starting range */
525 regmap_update_bits(arizona->regmap,
526 ARIZONA_HEADPHONE_DETECT_1,
527 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
530 ret = arizona_hpdet_do_id(info, &reading);
531 if (ret == -EAGAIN) {
533 } else if (ret < 0) {
537 /* Report high impedence cables as line outputs */
539 report = ARIZONA_CABLE_LINEOUT;
541 report = ARIZONA_CABLE_HEADPHONE;
543 ret = extcon_set_cable_state_(&info->edev, report, true);
545 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
548 mutex_lock(&arizona->dapm->card->dapm_mutex);
550 ret = regmap_read(arizona->regmap, ARIZONA_OUTPUT_ENABLES_1, &val);
552 dev_err(arizona->dev, "Failed to read output enables: %d\n",
557 if (!(val & (ARIZONA_OUT1L_ENA | ARIZONA_OUT1R_ENA))) {
558 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0);
560 dev_warn(arizona->dev, "Failed to undo magic: %d\n",
563 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0);
565 dev_warn(arizona->dev, "Failed to undo magic: %d\n",
569 mutex_unlock(&arizona->dapm->card->dapm_mutex);
573 gpio_set_value_cansleep(id_gpio, 0);
575 /* Revert back to MICDET mode */
576 regmap_update_bits(arizona->regmap,
577 ARIZONA_ACCESSORY_DETECT_MODE_1,
578 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
580 /* If we have a mic then reenable MICDET */
582 arizona_start_mic(info);
584 if (info->hpdet_active) {
585 pm_runtime_put_autosuspend(info->dev);
586 info->hpdet_active = false;
590 mutex_unlock(&info->lock);
595 static void arizona_identify_headphone(struct arizona_extcon_info *info)
597 struct arizona *arizona = info->arizona;
600 dev_dbg(arizona->dev, "Starting HPDET\n");
602 /* Make sure we keep the device enabled during the measurement */
603 pm_runtime_get(info->dev);
605 info->hpdet_active = true;
608 arizona_stop_mic(info);
610 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0x4000);
612 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
614 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0x4000);
616 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
618 ret = regmap_update_bits(arizona->regmap,
619 ARIZONA_ACCESSORY_DETECT_MODE_1,
620 ARIZONA_ACCDET_MODE_MASK,
621 ARIZONA_ACCDET_MODE_HPL);
623 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
627 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
628 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
630 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
638 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
639 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
641 /* Just report headphone */
642 ret = extcon_update_state(&info->edev,
643 1 << ARIZONA_CABLE_HEADPHONE,
644 1 << ARIZONA_CABLE_HEADPHONE);
646 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
649 arizona_start_mic(info);
651 info->hpdet_active = false;
654 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
656 struct arizona *arizona = info->arizona;
660 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
662 /* Make sure we keep the device enabled during the measurement */
663 pm_runtime_get_sync(info->dev);
665 info->hpdet_active = true;
667 arizona_extcon_pulse_micbias(info);
669 mutex_lock(&arizona->dapm->card->dapm_mutex);
671 ret = regmap_read(arizona->regmap, ARIZONA_OUTPUT_ENABLES_1, &val);
673 dev_err(arizona->dev, "Failed to read output enables: %d\n",
678 if (!(val & (ARIZONA_OUT1L_ENA | ARIZONA_OUT1R_ENA))) {
679 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000,
682 dev_warn(arizona->dev, "Failed to do magic: %d\n",
685 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000,
688 dev_warn(arizona->dev, "Failed to do magic: %d\n",
692 mutex_unlock(&arizona->dapm->card->dapm_mutex);
694 ret = regmap_update_bits(arizona->regmap,
695 ARIZONA_ACCESSORY_DETECT_MODE_1,
696 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
697 info->micd_modes[0].src |
698 ARIZONA_ACCDET_MODE_HPL);
700 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
704 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
705 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
707 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
715 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
716 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
718 /* Just report headphone */
719 ret = extcon_update_state(&info->edev,
720 1 << ARIZONA_CABLE_HEADPHONE,
721 1 << ARIZONA_CABLE_HEADPHONE);
723 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
725 info->hpdet_active = false;
728 static irqreturn_t arizona_micdet(int irq, void *data)
730 struct arizona_extcon_info *info = data;
731 struct arizona *arizona = info->arizona;
732 unsigned int val, lvl;
735 mutex_lock(&info->lock);
737 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
739 dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
740 mutex_unlock(&info->lock);
744 dev_dbg(arizona->dev, "MICDET: %x\n", val);
746 if (!(val & ARIZONA_MICD_VALID)) {
747 dev_warn(arizona->dev, "Microphone detection state invalid\n");
748 mutex_unlock(&info->lock);
752 /* Due to jack detect this should never happen */
753 if (!(val & ARIZONA_MICD_STS)) {
754 dev_warn(arizona->dev, "Detected open circuit\n");
755 info->detecting = false;
759 /* If we got a high impedence we should have a headset, report it. */
760 if (info->detecting && (val & 0x400)) {
761 arizona_identify_headphone(info);
763 ret = extcon_update_state(&info->edev,
764 1 << ARIZONA_CABLE_MICROPHONE,
765 1 << ARIZONA_CABLE_MICROPHONE);
768 dev_err(arizona->dev, "Headset report failed: %d\n",
771 /* Don't need to regulate for button detection */
772 ret = regulator_allow_bypass(info->micvdd, false);
774 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
779 info->detecting = false;
783 /* If we detected a lower impedence during initial startup
784 * then we probably have the wrong polarity, flip it. Don't
785 * do this for the lowest impedences to speed up detection of
786 * plain headphones. If both polarities report a low
787 * impedence then give up and report headphones.
789 if (info->detecting && (val & 0x3f8)) {
790 if (info->jack_flips >= info->micd_num_modes) {
791 dev_dbg(arizona->dev, "Detected HP/line\n");
792 arizona_identify_headphone(info);
794 info->detecting = false;
796 arizona_stop_mic(info);
799 if (info->micd_mode == info->micd_num_modes)
801 arizona_extcon_set_mode(info, info->micd_mode);
810 * If we're still detecting and we detect a short then we've
811 * got a headphone. Otherwise it's a button press.
815 dev_dbg(arizona->dev, "Mic button detected\n");
817 lvl = val & ARIZONA_MICD_LVL_MASK;
818 lvl >>= ARIZONA_MICD_LVL_SHIFT;
820 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
821 if (lvl & arizona_lvl_to_key[i].status)
822 input_report_key(info->input,
823 arizona_lvl_to_key[i].report,
825 input_sync(info->input);
827 } else if (info->detecting) {
828 dev_dbg(arizona->dev, "Headphone detected\n");
829 info->detecting = false;
830 arizona_stop_mic(info);
832 arizona_identify_headphone(info);
834 dev_warn(arizona->dev, "Button with no mic: %x\n",
838 dev_dbg(arizona->dev, "Mic button released\n");
839 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
840 input_report_key(info->input,
841 arizona_lvl_to_key[i].report, 0);
842 input_sync(info->input);
843 arizona_extcon_pulse_micbias(info);
847 pm_runtime_mark_last_busy(info->dev);
848 mutex_unlock(&info->lock);
853 static void arizona_hpdet_work(struct work_struct *work)
855 struct arizona_extcon_info *info = container_of(work,
856 struct arizona_extcon_info,
859 mutex_lock(&info->lock);
860 arizona_start_hpdet_acc_id(info);
861 mutex_unlock(&info->lock);
864 static irqreturn_t arizona_jackdet(int irq, void *data)
866 struct arizona_extcon_info *info = data;
867 struct arizona *arizona = info->arizona;
868 unsigned int val, present, mask;
871 pm_runtime_get_sync(info->dev);
873 cancel_delayed_work_sync(&info->hpdet_work);
875 mutex_lock(&info->lock);
877 if (arizona->pdata.jd_gpio5) {
878 mask = ARIZONA_MICD_CLAMP_STS;
881 mask = ARIZONA_JD1_STS;
882 present = ARIZONA_JD1_STS;
885 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
887 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
889 mutex_unlock(&info->lock);
890 pm_runtime_put_autosuspend(info->dev);
894 if ((val & mask) == present) {
895 dev_dbg(arizona->dev, "Detected jack\n");
896 ret = extcon_set_cable_state_(&info->edev,
897 ARIZONA_CABLE_MECHANICAL, true);
900 dev_err(arizona->dev, "Mechanical report failed: %d\n",
903 if (!arizona->pdata.hpdet_acc_id) {
904 info->detecting = true;
906 info->jack_flips = 0;
908 arizona_start_mic(info);
910 schedule_delayed_work(&info->hpdet_work,
911 msecs_to_jiffies(250));
914 regmap_update_bits(arizona->regmap,
915 ARIZONA_JACK_DETECT_DEBOUNCE,
916 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
918 dev_dbg(arizona->dev, "Detected jack removal\n");
920 arizona_stop_mic(info);
922 info->num_hpdet_res = 0;
923 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
924 info->hpdet_res[i] = 0;
927 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
928 input_report_key(info->input,
929 arizona_lvl_to_key[i].report, 0);
930 input_sync(info->input);
932 ret = extcon_update_state(&info->edev, 0xffffffff, 0);
934 dev_err(arizona->dev, "Removal report failed: %d\n",
937 regmap_update_bits(arizona->regmap,
938 ARIZONA_JACK_DETECT_DEBOUNCE,
939 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
940 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
943 /* Clear trig_sts to make sure DCVDD is not forced up */
944 regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
945 ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
946 ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
947 ARIZONA_JD1_FALL_TRIG_STS |
948 ARIZONA_JD1_RISE_TRIG_STS);
950 mutex_unlock(&info->lock);
952 pm_runtime_mark_last_busy(info->dev);
953 pm_runtime_put_autosuspend(info->dev);
958 static int arizona_extcon_probe(struct platform_device *pdev)
960 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
961 struct arizona_pdata *pdata;
962 struct arizona_extcon_info *info;
963 int jack_irq_fall, jack_irq_rise;
966 if (!arizona->dapm || !arizona->dapm->card)
967 return -EPROBE_DEFER;
969 pdata = dev_get_platdata(arizona->dev);
971 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
973 dev_err(&pdev->dev, "Failed to allocate memory\n");
978 info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
979 if (IS_ERR(info->micvdd)) {
980 ret = PTR_ERR(info->micvdd);
981 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
985 mutex_init(&info->lock);
986 info->arizona = arizona;
987 info->dev = &pdev->dev;
988 INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
989 platform_set_drvdata(pdev, info);
991 switch (arizona->type) {
993 switch (arizona->rev) {
995 info->micd_reva = true;
998 info->micd_clamp = true;
1007 info->edev.name = "Headset Jack";
1008 info->edev.supported_cable = arizona_cable;
1010 ret = extcon_dev_register(&info->edev, arizona->dev);
1012 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
1017 if (pdata->num_micd_configs) {
1018 info->micd_modes = pdata->micd_configs;
1019 info->micd_num_modes = pdata->num_micd_configs;
1021 info->micd_modes = micd_default_modes;
1022 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1025 if (arizona->pdata.micd_pol_gpio > 0) {
1026 if (info->micd_modes[0].gpio)
1027 mode = GPIOF_OUT_INIT_HIGH;
1029 mode = GPIOF_OUT_INIT_LOW;
1031 ret = devm_gpio_request_one(&pdev->dev,
1032 arizona->pdata.micd_pol_gpio,
1036 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1037 arizona->pdata.micd_pol_gpio, ret);
1042 if (arizona->pdata.hpdet_id_gpio > 0) {
1043 ret = devm_gpio_request_one(&pdev->dev,
1044 arizona->pdata.hpdet_id_gpio,
1048 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1049 arizona->pdata.hpdet_id_gpio, ret);
1054 if (arizona->pdata.micd_bias_start_time)
1055 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1056 ARIZONA_MICD_BIAS_STARTTIME_MASK,
1057 arizona->pdata.micd_bias_start_time
1058 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1060 if (arizona->pdata.micd_rate)
1061 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1062 ARIZONA_MICD_RATE_MASK,
1063 arizona->pdata.micd_rate
1064 << ARIZONA_MICD_RATE_SHIFT);
1066 if (arizona->pdata.micd_dbtime)
1067 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1068 ARIZONA_MICD_DBTIME_MASK,
1069 arizona->pdata.micd_dbtime
1070 << ARIZONA_MICD_DBTIME_SHIFT);
1073 * If we have a clamp use it, activating in conjunction with
1074 * GPIO5 if that is connected for jack detect operation.
1076 if (info->micd_clamp) {
1077 if (arizona->pdata.jd_gpio5) {
1078 /* Put the GPIO into input mode */
1079 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1082 regmap_update_bits(arizona->regmap,
1083 ARIZONA_MICD_CLAMP_CONTROL,
1084 ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
1086 regmap_update_bits(arizona->regmap,
1087 ARIZONA_MICD_CLAMP_CONTROL,
1088 ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
1091 regmap_update_bits(arizona->regmap,
1092 ARIZONA_JACK_DETECT_DEBOUNCE,
1093 ARIZONA_MICD_CLAMP_DB,
1094 ARIZONA_MICD_CLAMP_DB);
1097 arizona_extcon_set_mode(info, 0);
1099 info->input = devm_input_allocate_device(&pdev->dev);
1101 dev_err(arizona->dev, "Can't allocate input dev\n");
1106 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
1107 input_set_capability(info->input, EV_KEY,
1108 arizona_lvl_to_key[i].report);
1109 info->input->name = "Headset";
1110 info->input->phys = "arizona/extcon";
1111 info->input->dev.parent = &pdev->dev;
1113 pm_runtime_enable(&pdev->dev);
1114 pm_runtime_idle(&pdev->dev);
1115 pm_runtime_get_sync(&pdev->dev);
1117 if (arizona->pdata.jd_gpio5) {
1118 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1119 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1121 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1122 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1125 ret = arizona_request_irq(arizona, jack_irq_rise,
1126 "JACKDET rise", arizona_jackdet, info);
1128 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1133 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1135 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1140 ret = arizona_request_irq(arizona, jack_irq_fall,
1141 "JACKDET fall", arizona_jackdet, info);
1143 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1147 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1149 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1154 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1155 "MICDET", arizona_micdet, info);
1157 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1161 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1162 "HPDET", arizona_hpdet_irq, info);
1164 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1168 arizona_clk32k_enable(arizona);
1169 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1170 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1171 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1172 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1174 ret = regulator_allow_bypass(info->micvdd, true);
1176 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1179 pm_runtime_put(&pdev->dev);
1181 ret = input_register_device(info->input);
1183 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1190 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1192 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1194 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1196 arizona_free_irq(arizona, jack_irq_fall, info);
1198 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1200 arizona_free_irq(arizona, jack_irq_rise, info);
1203 pm_runtime_disable(&pdev->dev);
1204 extcon_dev_unregister(&info->edev);
1209 static int arizona_extcon_remove(struct platform_device *pdev)
1211 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1212 struct arizona *arizona = info->arizona;
1213 int jack_irq_rise, jack_irq_fall;
1215 pm_runtime_disable(&pdev->dev);
1217 regmap_update_bits(arizona->regmap,
1218 ARIZONA_MICD_CLAMP_CONTROL,
1219 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1221 if (arizona->pdata.jd_gpio5) {
1222 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1223 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1225 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1226 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1229 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1230 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1231 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1232 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1233 arizona_free_irq(arizona, jack_irq_rise, info);
1234 arizona_free_irq(arizona, jack_irq_fall, info);
1235 cancel_delayed_work_sync(&info->hpdet_work);
1236 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1237 ARIZONA_JD1_ENA, 0);
1238 arizona_clk32k_disable(arizona);
1239 extcon_dev_unregister(&info->edev);
1244 static struct platform_driver arizona_extcon_driver = {
1246 .name = "arizona-extcon",
1247 .owner = THIS_MODULE,
1249 .probe = arizona_extcon_probe,
1250 .remove = arizona_extcon_remove,
1253 module_platform_driver(arizona_extcon_driver);
1255 MODULE_DESCRIPTION("Arizona Extcon driver");
1256 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1257 MODULE_LICENSE("GPL");
1258 MODULE_ALIAS("platform:extcon-arizona");