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_set_mode(struct arizona_extcon_info *info, int mode)
105 struct arizona *arizona = info->arizona;
107 if (arizona->pdata.micd_pol_gpio > 0)
108 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
109 info->micd_modes[mode].gpio);
110 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
111 ARIZONA_MICD_BIAS_SRC_MASK,
112 info->micd_modes[mode].bias);
113 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
114 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
116 info->micd_mode = mode;
118 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
121 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
123 switch (info->micd_modes[0].bias >> ARIZONA_MICD_BIAS_SRC_SHIFT) {
135 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
137 struct arizona *arizona = info->arizona;
138 const char *widget = arizona_extcon_get_micbias(info);
139 struct snd_soc_dapm_context *dapm = arizona->dapm;
142 mutex_lock(&dapm->card->dapm_mutex);
144 ret = snd_soc_dapm_force_enable_pin(dapm, widget);
146 dev_warn(arizona->dev, "Failed to enable %s: %d\n",
149 mutex_unlock(&dapm->card->dapm_mutex);
151 snd_soc_dapm_sync(dapm);
153 if (!arizona->pdata.micd_force_micbias) {
154 mutex_lock(&dapm->card->dapm_mutex);
156 ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
158 dev_warn(arizona->dev, "Failed to disable %s: %d\n",
161 mutex_unlock(&dapm->card->dapm_mutex);
163 snd_soc_dapm_sync(dapm);
167 static void arizona_start_mic(struct arizona_extcon_info *info)
169 struct arizona *arizona = info->arizona;
173 /* Microphone detection can't use idle mode */
174 pm_runtime_get(info->dev);
176 if (info->detecting) {
177 ret = regulator_allow_bypass(info->micvdd, false);
179 dev_err(arizona->dev,
180 "Failed to regulate MICVDD: %d\n",
185 ret = regulator_enable(info->micvdd);
187 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
191 if (info->micd_reva) {
192 regmap_write(arizona->regmap, 0x80, 0x3);
193 regmap_write(arizona->regmap, 0x294, 0);
194 regmap_write(arizona->regmap, 0x80, 0x0);
197 regmap_update_bits(arizona->regmap,
198 ARIZONA_ACCESSORY_DETECT_MODE_1,
199 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
201 arizona_extcon_pulse_micbias(info);
203 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
204 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
207 regulator_disable(info->micvdd);
208 pm_runtime_put_autosuspend(info->dev);
212 static void arizona_stop_mic(struct arizona_extcon_info *info)
214 struct arizona *arizona = info->arizona;
215 const char *widget = arizona_extcon_get_micbias(info);
216 struct snd_soc_dapm_context *dapm = arizona->dapm;
220 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
224 mutex_lock(&dapm->card->dapm_mutex);
226 ret = snd_soc_dapm_disable_pin(dapm, widget);
228 dev_warn(arizona->dev,
229 "Failed to disable %s: %d\n",
232 mutex_unlock(&dapm->card->dapm_mutex);
234 snd_soc_dapm_sync(dapm);
236 if (info->micd_reva) {
237 regmap_write(arizona->regmap, 0x80, 0x3);
238 regmap_write(arizona->regmap, 0x294, 2);
239 regmap_write(arizona->regmap, 0x80, 0x0);
242 ret = regulator_allow_bypass(info->micvdd, true);
244 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
249 regulator_disable(info->micvdd);
250 pm_runtime_mark_last_busy(info->dev);
251 pm_runtime_put_autosuspend(info->dev);
256 unsigned int factor_a;
257 unsigned int factor_b;
258 } arizona_hpdet_b_ranges[] = {
267 } arizona_hpdet_c_ranges[] = {
274 static int arizona_hpdet_read(struct arizona_extcon_info *info)
276 struct arizona *arizona = info->arizona;
277 unsigned int val, range;
280 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
282 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
287 switch (info->hpdet_ip) {
289 if (!(val & ARIZONA_HP_DONE)) {
290 dev_err(arizona->dev, "HPDET did not complete: %x\n",
295 val &= ARIZONA_HP_LVL_MASK;
299 if (!(val & ARIZONA_HP_DONE_B)) {
300 dev_err(arizona->dev, "HPDET did not complete: %x\n",
305 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
307 dev_err(arizona->dev, "Failed to read HP value: %d\n",
312 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
314 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
315 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
317 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
318 (val < 100 || val > 0x3fb)) {
320 dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
322 regmap_update_bits(arizona->regmap,
323 ARIZONA_HEADPHONE_DETECT_1,
324 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
326 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
330 /* If we go out of range report top of range */
331 if (val < 100 || val > 0x3fb) {
332 dev_dbg(arizona->dev, "Measurement out of range\n");
336 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
339 val = arizona_hpdet_b_ranges[range].factor_b
341 arizona_hpdet_b_ranges[range].factor_a);
345 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
348 if (!(val & ARIZONA_HP_DONE_B)) {
349 dev_err(arizona->dev, "HPDET did not complete: %x\n",
354 val &= ARIZONA_HP_LVL_B_MASK;
356 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
358 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
359 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
361 /* Skip up or down a range? */
362 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
364 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
365 arizona_hpdet_c_ranges[range].min,
366 arizona_hpdet_c_ranges[range].max);
367 regmap_update_bits(arizona->regmap,
368 ARIZONA_HEADPHONE_DETECT_1,
369 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
371 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
375 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
376 (val >= arizona_hpdet_c_ranges[range].max)) {
378 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
379 arizona_hpdet_c_ranges[range].min,
380 arizona_hpdet_c_ranges[range].max);
381 regmap_update_bits(arizona->regmap,
382 ARIZONA_HEADPHONE_DETECT_1,
383 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
385 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
390 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
394 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading)
396 struct arizona *arizona = info->arizona;
397 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 info->detecting = true;
468 dev_dbg(arizona->dev, "Detected headphone\n");
471 /* Make sure everything is reset back to the real polarity */
472 regmap_update_bits(arizona->regmap,
473 ARIZONA_ACCESSORY_DETECT_MODE_1,
475 info->micd_modes[0].src);
481 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
483 struct arizona_extcon_info *info = data;
484 struct arizona *arizona = info->arizona;
485 int id_gpio = arizona->pdata.hpdet_id_gpio;
486 int report = ARIZONA_CABLE_HEADPHONE;
490 mutex_lock(&info->lock);
492 /* If we got a spurious IRQ for some reason then ignore it */
493 if (!info->hpdet_active) {
494 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
495 mutex_unlock(&info->lock);
499 /* If the cable was removed while measuring ignore the result */
500 ret = extcon_get_cable_state_(&info->edev, ARIZONA_CABLE_MECHANICAL);
502 dev_err(arizona->dev, "Failed to check cable state: %d\n",
506 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
510 ret = arizona_hpdet_read(info);
511 if (ret == -EAGAIN) {
513 } else if (ret < 0) {
518 /* Reset back to starting range */
519 regmap_update_bits(arizona->regmap,
520 ARIZONA_HEADPHONE_DETECT_1,
521 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
524 ret = arizona_hpdet_do_id(info, &reading);
525 if (ret == -EAGAIN) {
527 } else if (ret < 0) {
531 /* Report high impedence cables as line outputs */
533 report = ARIZONA_CABLE_LINEOUT;
535 report = ARIZONA_CABLE_HEADPHONE;
537 ret = extcon_set_cable_state_(&info->edev, report, true);
539 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
542 mutex_lock(&arizona->dapm->card->dapm_mutex);
544 ret = regmap_read(arizona->regmap, ARIZONA_OUTPUT_ENABLES_1, &val);
546 dev_err(arizona->dev, "Failed to read output enables: %d\n",
551 if (!(val & (ARIZONA_OUT1L_ENA | ARIZONA_OUT1R_ENA))) {
552 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0);
554 dev_warn(arizona->dev, "Failed to undo magic: %d\n",
557 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0);
559 dev_warn(arizona->dev, "Failed to undo magic: %d\n",
563 mutex_unlock(&arizona->dapm->card->dapm_mutex);
567 gpio_set_value_cansleep(id_gpio, 0);
569 /* Revert back to MICDET mode */
570 regmap_update_bits(arizona->regmap,
571 ARIZONA_ACCESSORY_DETECT_MODE_1,
572 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
574 /* If we have a mic then reenable MICDET */
576 arizona_start_mic(info);
578 if (info->hpdet_active) {
579 pm_runtime_put_autosuspend(info->dev);
580 info->hpdet_active = false;
583 info->hpdet_done = true;
586 mutex_unlock(&info->lock);
591 static void arizona_identify_headphone(struct arizona_extcon_info *info)
593 struct arizona *arizona = info->arizona;
596 if (info->hpdet_done)
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;
925 info->hpdet_done = false;
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");