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/consumer.h>
24 #include <linux/gpio.h>
25 #include <linux/input.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/property.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/extcon.h>
32 #include <sound/soc.h>
34 #include <linux/mfd/arizona/core.h>
35 #include <linux/mfd/arizona/pdata.h>
36 #include <linux/mfd/arizona/registers.h>
37 #include <dt-bindings/mfd/arizona.h>
39 #define ARIZONA_MAX_MICD_RANGE 8
41 #define ARIZONA_MICD_CLAMP_MODE_JDL 0x4
42 #define ARIZONA_MICD_CLAMP_MODE_JDH 0x5
43 #define ARIZONA_MICD_CLAMP_MODE_JDL_GP5H 0x9
44 #define ARIZONA_MICD_CLAMP_MODE_JDH_GP5H 0xb
46 #define ARIZONA_HPDET_MAX 10000
48 #define HPDET_DEBOUNCE 500
49 #define DEFAULT_MICD_TIMEOUT 2000
51 #define MICD_DBTIME_TWO_READINGS 2
52 #define MICD_DBTIME_FOUR_READINGS 4
54 #define MICD_LVL_1_TO_7 (ARIZONA_MICD_LVL_1 | ARIZONA_MICD_LVL_2 | \
55 ARIZONA_MICD_LVL_3 | ARIZONA_MICD_LVL_4 | \
56 ARIZONA_MICD_LVL_5 | ARIZONA_MICD_LVL_6 | \
59 #define MICD_LVL_0_TO_7 (ARIZONA_MICD_LVL_0 | MICD_LVL_1_TO_7)
61 #define MICD_LVL_0_TO_8 (MICD_LVL_0_TO_7 | ARIZONA_MICD_LVL_8)
63 struct arizona_extcon_info {
65 struct arizona *arizona;
67 struct regulator *micvdd;
68 struct input_dev *input;
73 const struct arizona_micd_config *micd_modes;
76 const struct arizona_micd_range *micd_ranges;
84 struct delayed_work hpdet_work;
85 struct delayed_work micd_detect_work;
86 struct delayed_work micd_timeout_work;
93 unsigned int hpdet_res[3];
101 struct extcon_dev *edev;
103 struct gpio_desc *micd_pol_gpio;
106 static const struct arizona_micd_config micd_default_modes[] = {
107 { ARIZONA_ACCDET_SRC, 1, 0 },
111 static const struct arizona_micd_range micd_default_ranges[] = {
112 { .max = 11, .key = BTN_0 },
113 { .max = 28, .key = BTN_1 },
114 { .max = 54, .key = BTN_2 },
115 { .max = 100, .key = BTN_3 },
116 { .max = 186, .key = BTN_4 },
117 { .max = 430, .key = BTN_5 },
120 static const int arizona_micd_levels[] = {
121 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
122 49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
123 105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
124 270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
128 static const unsigned int arizona_cable[] = {
136 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info);
138 static void arizona_extcon_hp_clamp(struct arizona_extcon_info *info,
141 struct arizona *arizona = info->arizona;
142 unsigned int mask = 0, val = 0;
145 switch (arizona->type) {
148 mask = ARIZONA_HP1L_SHRTO | ARIZONA_HP1L_FLWR |
151 val = ARIZONA_HP1L_SHRTO;
153 val = ARIZONA_HP1L_FLWR | ARIZONA_HP1L_SHRTI;
156 mask = ARIZONA_RMV_SHRT_HP1L;
158 val = ARIZONA_RMV_SHRT_HP1L;
162 mutex_lock(&arizona->dapm->card->dapm_mutex);
164 arizona->hpdet_clamp = clamp;
166 /* Keep the HP output stages disabled while doing the clamp */
168 ret = regmap_update_bits(arizona->regmap,
169 ARIZONA_OUTPUT_ENABLES_1,
171 ARIZONA_OUT1R_ENA, 0);
173 dev_warn(arizona->dev,
174 "Failed to disable headphone outputs: %d\n",
178 ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1L,
181 dev_warn(arizona->dev, "Failed to do clamp: %d\n",
184 ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1R,
187 dev_warn(arizona->dev, "Failed to do clamp: %d\n",
190 /* Restore the desired state while not doing the clamp */
192 ret = regmap_update_bits(arizona->regmap,
193 ARIZONA_OUTPUT_ENABLES_1,
195 ARIZONA_OUT1R_ENA, arizona->hp_ena);
197 dev_warn(arizona->dev,
198 "Failed to restore headphone outputs: %d\n",
202 mutex_unlock(&arizona->dapm->card->dapm_mutex);
205 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
207 struct arizona *arizona = info->arizona;
209 mode %= info->micd_num_modes;
211 if (arizona->pdata.micd_pol_gpio > 0)
212 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
213 info->micd_modes[mode].gpio);
215 gpiod_set_value_cansleep(info->micd_pol_gpio,
216 info->micd_modes[mode].gpio);
218 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
219 ARIZONA_MICD_BIAS_SRC_MASK,
220 info->micd_modes[mode].bias <<
221 ARIZONA_MICD_BIAS_SRC_SHIFT);
222 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
223 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
225 info->micd_mode = mode;
227 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
230 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
232 switch (info->micd_modes[0].bias) {
244 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
246 struct arizona *arizona = info->arizona;
247 const char *widget = arizona_extcon_get_micbias(info);
248 struct snd_soc_dapm_context *dapm = arizona->dapm;
251 ret = snd_soc_dapm_force_enable_pin(dapm, widget);
253 dev_warn(arizona->dev, "Failed to enable %s: %d\n",
256 snd_soc_dapm_sync(dapm);
258 if (!arizona->pdata.micd_force_micbias) {
259 ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
261 dev_warn(arizona->dev, "Failed to disable %s: %d\n",
264 snd_soc_dapm_sync(dapm);
268 static void arizona_start_mic(struct arizona_extcon_info *info)
270 struct arizona *arizona = info->arizona;
274 /* Microphone detection can't use idle mode */
275 pm_runtime_get(info->dev);
277 if (info->detecting) {
278 ret = regulator_allow_bypass(info->micvdd, false);
280 dev_err(arizona->dev,
281 "Failed to regulate MICVDD: %d\n",
286 ret = regulator_enable(info->micvdd);
288 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
292 if (info->micd_reva) {
293 regmap_write(arizona->regmap, 0x80, 0x3);
294 regmap_write(arizona->regmap, 0x294, 0);
295 regmap_write(arizona->regmap, 0x80, 0x0);
298 regmap_update_bits(arizona->regmap,
299 ARIZONA_ACCESSORY_DETECT_MODE_1,
300 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
302 arizona_extcon_pulse_micbias(info);
304 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
305 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
308 regulator_disable(info->micvdd);
309 pm_runtime_put_autosuspend(info->dev);
313 static void arizona_stop_mic(struct arizona_extcon_info *info)
315 struct arizona *arizona = info->arizona;
316 const char *widget = arizona_extcon_get_micbias(info);
317 struct snd_soc_dapm_context *dapm = arizona->dapm;
321 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
325 ret = snd_soc_dapm_disable_pin(dapm, widget);
327 dev_warn(arizona->dev,
328 "Failed to disable %s: %d\n",
331 snd_soc_dapm_sync(dapm);
333 if (info->micd_reva) {
334 regmap_write(arizona->regmap, 0x80, 0x3);
335 regmap_write(arizona->regmap, 0x294, 2);
336 regmap_write(arizona->regmap, 0x80, 0x0);
339 ret = regulator_allow_bypass(info->micvdd, true);
341 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
346 regulator_disable(info->micvdd);
347 pm_runtime_mark_last_busy(info->dev);
348 pm_runtime_put_autosuspend(info->dev);
353 unsigned int threshold;
354 unsigned int factor_a;
355 unsigned int factor_b;
356 } arizona_hpdet_b_ranges[] = {
357 { 100, 5528, 362464 },
358 { 169, 11084, 6186851 },
359 { 169, 11065, 65460395 },
362 #define ARIZONA_HPDET_B_RANGE_MAX 0x3fb
367 } arizona_hpdet_c_ranges[] = {
374 static int arizona_hpdet_read(struct arizona_extcon_info *info)
376 struct arizona *arizona = info->arizona;
377 unsigned int val, range;
380 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
382 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
387 switch (info->hpdet_ip_version) {
389 if (!(val & ARIZONA_HP_DONE)) {
390 dev_err(arizona->dev, "HPDET did not complete: %x\n",
395 val &= ARIZONA_HP_LVL_MASK;
399 if (!(val & ARIZONA_HP_DONE_B)) {
400 dev_err(arizona->dev, "HPDET did not complete: %x\n",
405 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
407 dev_err(arizona->dev, "Failed to read HP value: %d\n",
412 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
414 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
415 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
417 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
418 (val < arizona_hpdet_b_ranges[range].threshold ||
419 val >= ARIZONA_HPDET_B_RANGE_MAX)) {
421 dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
423 regmap_update_bits(arizona->regmap,
424 ARIZONA_HEADPHONE_DETECT_1,
425 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
427 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
431 /* If we go out of range report top of range */
432 if (val < arizona_hpdet_b_ranges[range].threshold ||
433 val >= ARIZONA_HPDET_B_RANGE_MAX) {
434 dev_dbg(arizona->dev, "Measurement out of range\n");
435 return ARIZONA_HPDET_MAX;
438 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
441 val = arizona_hpdet_b_ranges[range].factor_b
443 arizona_hpdet_b_ranges[range].factor_a);
447 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
448 info->hpdet_ip_version);
450 if (!(val & ARIZONA_HP_DONE_B)) {
451 dev_err(arizona->dev, "HPDET did not complete: %x\n",
456 val &= ARIZONA_HP_LVL_B_MASK;
457 /* Convert to ohms, the value is in 0.5 ohm increments */
460 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
462 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
463 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
465 /* Skip up a range, or report? */
466 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
467 (val >= arizona_hpdet_c_ranges[range].max)) {
469 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
470 arizona_hpdet_c_ranges[range].min,
471 arizona_hpdet_c_ranges[range].max);
472 regmap_update_bits(arizona->regmap,
473 ARIZONA_HEADPHONE_DETECT_1,
474 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
476 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
480 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
481 dev_dbg(arizona->dev, "Reporting range boundary %d\n",
482 arizona_hpdet_c_ranges[range].min);
483 val = arizona_hpdet_c_ranges[range].min;
487 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
491 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading,
494 struct arizona *arizona = info->arizona;
495 int id_gpio = arizona->pdata.hpdet_id_gpio;
498 * If we're using HPDET for accessory identification we need
499 * to take multiple measurements, step through them in sequence.
501 if (arizona->pdata.hpdet_acc_id) {
502 info->hpdet_res[info->num_hpdet_res++] = *reading;
504 /* Only check the mic directly if we didn't already ID it */
505 if (id_gpio && info->num_hpdet_res == 1) {
506 dev_dbg(arizona->dev, "Measuring mic\n");
508 regmap_update_bits(arizona->regmap,
509 ARIZONA_ACCESSORY_DETECT_MODE_1,
510 ARIZONA_ACCDET_MODE_MASK |
512 ARIZONA_ACCDET_MODE_HPR |
513 info->micd_modes[0].src);
515 gpio_set_value_cansleep(id_gpio, 1);
517 regmap_update_bits(arizona->regmap,
518 ARIZONA_HEADPHONE_DETECT_1,
519 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
523 /* OK, got both. Now, compare... */
524 dev_dbg(arizona->dev, "HPDET measured %d %d\n",
525 info->hpdet_res[0], info->hpdet_res[1]);
527 /* Take the headphone impedance for the main report */
528 *reading = info->hpdet_res[0];
530 /* Sometimes we get false readings due to slow insert */
531 if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
532 dev_dbg(arizona->dev, "Retrying high impedance\n");
533 info->num_hpdet_res = 0;
534 info->hpdet_retried = true;
535 arizona_start_hpdet_acc_id(info);
536 pm_runtime_put(info->dev);
541 * If we measure the mic as high impedance
543 if (!id_gpio || info->hpdet_res[1] > 50) {
544 dev_dbg(arizona->dev, "Detected mic\n");
546 info->detecting = true;
548 dev_dbg(arizona->dev, "Detected headphone\n");
551 /* Make sure everything is reset back to the real polarity */
552 regmap_update_bits(arizona->regmap,
553 ARIZONA_ACCESSORY_DETECT_MODE_1,
555 info->micd_modes[0].src);
561 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
563 struct arizona_extcon_info *info = data;
564 struct arizona *arizona = info->arizona;
565 int id_gpio = arizona->pdata.hpdet_id_gpio;
566 unsigned int report = EXTCON_HEADPHONE;
570 mutex_lock(&info->lock);
572 /* If we got a spurious IRQ for some reason then ignore it */
573 if (!info->hpdet_active) {
574 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
575 mutex_unlock(&info->lock);
579 /* If the cable was removed while measuring ignore the result */
580 ret = extcon_get_cable_state_(info->edev, EXTCON_MECHANICAL);
582 dev_err(arizona->dev, "Failed to check cable state: %d\n",
586 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
590 ret = arizona_hpdet_read(info);
597 /* Reset back to starting range */
598 regmap_update_bits(arizona->regmap,
599 ARIZONA_HEADPHONE_DETECT_1,
600 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
603 ret = arizona_hpdet_do_id(info, &reading, &mic);
609 /* Report high impedence cables as line outputs */
611 report = EXTCON_LINE_OUT;
613 report = EXTCON_HEADPHONE;
615 ret = extcon_set_cable_state_(info->edev, report, true);
617 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
621 /* Reset back to starting range */
622 regmap_update_bits(arizona->regmap,
623 ARIZONA_HEADPHONE_DETECT_1,
624 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
627 arizona_extcon_hp_clamp(info, false);
630 gpio_set_value_cansleep(id_gpio, 0);
632 /* Revert back to MICDET mode */
633 regmap_update_bits(arizona->regmap,
634 ARIZONA_ACCESSORY_DETECT_MODE_1,
635 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
637 /* If we have a mic then reenable MICDET */
638 if (mic || info->mic)
639 arizona_start_mic(info);
641 if (info->hpdet_active) {
642 pm_runtime_put_autosuspend(info->dev);
643 info->hpdet_active = false;
646 info->hpdet_done = true;
649 mutex_unlock(&info->lock);
654 static void arizona_identify_headphone(struct arizona_extcon_info *info)
656 struct arizona *arizona = info->arizona;
659 if (info->hpdet_done)
662 dev_dbg(arizona->dev, "Starting HPDET\n");
664 /* Make sure we keep the device enabled during the measurement */
665 pm_runtime_get(info->dev);
667 info->hpdet_active = true;
670 arizona_stop_mic(info);
672 arizona_extcon_hp_clamp(info, true);
674 ret = regmap_update_bits(arizona->regmap,
675 ARIZONA_ACCESSORY_DETECT_MODE_1,
676 ARIZONA_ACCDET_MODE_MASK,
677 arizona->pdata.hpdet_channel);
679 dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
683 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
684 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
686 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
694 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
695 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
697 /* Just report headphone */
698 ret = extcon_set_cable_state_(info->edev, EXTCON_HEADPHONE, true);
700 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
703 arizona_start_mic(info);
705 info->hpdet_active = false;
708 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
710 struct arizona *arizona = info->arizona;
715 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
717 /* Make sure we keep the device enabled during the measurement */
718 pm_runtime_get_sync(info->dev);
720 info->hpdet_active = true;
722 arizona_extcon_hp_clamp(info, true);
724 ret = regmap_update_bits(arizona->regmap,
725 ARIZONA_ACCESSORY_DETECT_MODE_1,
726 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
727 info->micd_modes[0].src |
728 arizona->pdata.hpdet_channel);
730 dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
734 if (arizona->pdata.hpdet_acc_id_line) {
735 ret = regmap_update_bits(arizona->regmap,
736 ARIZONA_HEADPHONE_DETECT_1,
737 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
739 dev_err(arizona->dev,
740 "Can't start HPDETL measurement: %d\n",
745 arizona_hpdet_do_id(info, &hp_reading, &mic);
751 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
752 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
754 /* Just report headphone */
755 ret = extcon_set_cable_state_(info->edev, EXTCON_HEADPHONE, true);
757 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
759 info->hpdet_active = false;
762 static void arizona_micd_timeout_work(struct work_struct *work)
764 struct arizona_extcon_info *info = container_of(work,
765 struct arizona_extcon_info,
766 micd_timeout_work.work);
768 mutex_lock(&info->lock);
770 dev_dbg(info->arizona->dev, "MICD timed out, reporting HP\n");
772 info->detecting = false;
774 arizona_identify_headphone(info);
776 arizona_stop_mic(info);
778 mutex_unlock(&info->lock);
781 static void arizona_micd_detect(struct work_struct *work)
783 struct arizona_extcon_info *info = container_of(work,
784 struct arizona_extcon_info,
785 micd_detect_work.work);
786 struct arizona *arizona = info->arizona;
787 unsigned int val = 0, lvl;
790 cancel_delayed_work_sync(&info->micd_timeout_work);
792 mutex_lock(&info->lock);
794 /* If the cable was removed while measuring ignore the result */
795 ret = extcon_get_cable_state_(info->edev, EXTCON_MECHANICAL);
797 dev_err(arizona->dev, "Failed to check cable state: %d\n",
799 mutex_unlock(&info->lock);
802 dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n");
803 mutex_unlock(&info->lock);
807 for (i = 0; i < 10 && !(val & MICD_LVL_0_TO_8); i++) {
808 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
810 dev_err(arizona->dev,
811 "Failed to read MICDET: %d\n", ret);
812 mutex_unlock(&info->lock);
816 dev_dbg(arizona->dev, "MICDET: %x\n", val);
818 if (!(val & ARIZONA_MICD_VALID)) {
819 dev_warn(arizona->dev,
820 "Microphone detection state invalid\n");
821 mutex_unlock(&info->lock);
826 if (i == 10 && !(val & MICD_LVL_0_TO_8)) {
827 dev_err(arizona->dev, "Failed to get valid MICDET value\n");
828 mutex_unlock(&info->lock);
832 /* Due to jack detect this should never happen */
833 if (!(val & ARIZONA_MICD_STS)) {
834 dev_warn(arizona->dev, "Detected open circuit\n");
836 arizona_stop_mic(info);
837 info->detecting = false;
838 arizona_identify_headphone(info);
842 /* If we got a high impedence we should have a headset, report it. */
843 if (info->detecting && (val & ARIZONA_MICD_LVL_8)) {
845 info->detecting = false;
847 arizona_identify_headphone(info);
849 ret = extcon_set_cable_state_(info->edev,
850 EXTCON_MICROPHONE, true);
852 dev_err(arizona->dev, "Headset report failed: %d\n",
855 /* Don't need to regulate for button detection */
856 ret = regulator_allow_bypass(info->micvdd, true);
858 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
865 /* If we detected a lower impedence during initial startup
866 * then we probably have the wrong polarity, flip it. Don't
867 * do this for the lowest impedences to speed up detection of
868 * plain headphones. If both polarities report a low
869 * impedence then give up and report headphones.
871 if (info->detecting && (val & MICD_LVL_1_TO_7)) {
872 if (info->jack_flips >= info->micd_num_modes * 10) {
873 dev_dbg(arizona->dev, "Detected HP/line\n");
875 info->detecting = false;
877 arizona_identify_headphone(info);
879 arizona_stop_mic(info);
882 if (info->micd_mode == info->micd_num_modes)
884 arizona_extcon_set_mode(info, info->micd_mode);
893 * If we're still detecting and we detect a short then we've
894 * got a headphone. Otherwise it's a button press.
896 if (val & MICD_LVL_0_TO_7) {
898 dev_dbg(arizona->dev, "Mic button detected\n");
900 lvl = val & ARIZONA_MICD_LVL_MASK;
901 lvl >>= ARIZONA_MICD_LVL_SHIFT;
903 for (i = 0; i < info->num_micd_ranges; i++)
904 input_report_key(info->input,
905 info->micd_ranges[i].key, 0);
908 WARN_ON(ffs(lvl) - 1 >= info->num_micd_ranges);
909 if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
910 key = info->micd_ranges[ffs(lvl) - 1].key;
911 input_report_key(info->input, key, 1);
912 input_sync(info->input);
915 } else if (info->detecting) {
916 dev_dbg(arizona->dev, "Headphone detected\n");
917 info->detecting = false;
918 arizona_stop_mic(info);
920 arizona_identify_headphone(info);
922 dev_warn(arizona->dev, "Button with no mic: %x\n",
926 dev_dbg(arizona->dev, "Mic button released\n");
927 for (i = 0; i < info->num_micd_ranges; i++)
928 input_report_key(info->input,
929 info->micd_ranges[i].key, 0);
930 input_sync(info->input);
931 arizona_extcon_pulse_micbias(info);
936 queue_delayed_work(system_power_efficient_wq,
937 &info->micd_timeout_work,
938 msecs_to_jiffies(info->micd_timeout));
940 pm_runtime_mark_last_busy(info->dev);
941 mutex_unlock(&info->lock);
944 static irqreturn_t arizona_micdet(int irq, void *data)
946 struct arizona_extcon_info *info = data;
947 struct arizona *arizona = info->arizona;
948 int debounce = arizona->pdata.micd_detect_debounce;
950 cancel_delayed_work_sync(&info->micd_detect_work);
951 cancel_delayed_work_sync(&info->micd_timeout_work);
953 mutex_lock(&info->lock);
954 if (!info->detecting)
956 mutex_unlock(&info->lock);
959 queue_delayed_work(system_power_efficient_wq,
960 &info->micd_detect_work,
961 msecs_to_jiffies(debounce));
963 arizona_micd_detect(&info->micd_detect_work.work);
968 static void arizona_hpdet_work(struct work_struct *work)
970 struct arizona_extcon_info *info = container_of(work,
971 struct arizona_extcon_info,
974 mutex_lock(&info->lock);
975 arizona_start_hpdet_acc_id(info);
976 mutex_unlock(&info->lock);
979 static irqreturn_t arizona_jackdet(int irq, void *data)
981 struct arizona_extcon_info *info = data;
982 struct arizona *arizona = info->arizona;
983 unsigned int val, present, mask;
984 bool cancelled_hp, cancelled_mic;
987 cancelled_hp = cancel_delayed_work_sync(&info->hpdet_work);
988 cancelled_mic = cancel_delayed_work_sync(&info->micd_timeout_work);
990 pm_runtime_get_sync(info->dev);
992 mutex_lock(&info->lock);
994 if (arizona->pdata.jd_gpio5) {
995 mask = ARIZONA_MICD_CLAMP_STS;
996 if (arizona->pdata.jd_invert)
997 present = ARIZONA_MICD_CLAMP_STS;
1001 mask = ARIZONA_JD1_STS;
1002 if (arizona->pdata.jd_invert)
1005 present = ARIZONA_JD1_STS;
1008 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
1010 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
1012 mutex_unlock(&info->lock);
1013 pm_runtime_put_autosuspend(info->dev);
1018 if (val == info->last_jackdet) {
1019 dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
1021 queue_delayed_work(system_power_efficient_wq,
1023 msecs_to_jiffies(HPDET_DEBOUNCE));
1025 if (cancelled_mic) {
1026 int micd_timeout = info->micd_timeout;
1028 queue_delayed_work(system_power_efficient_wq,
1029 &info->micd_timeout_work,
1030 msecs_to_jiffies(micd_timeout));
1035 info->last_jackdet = val;
1037 if (info->last_jackdet == present) {
1038 dev_dbg(arizona->dev, "Detected jack\n");
1039 ret = extcon_set_cable_state_(info->edev,
1040 EXTCON_MECHANICAL, true);
1043 dev_err(arizona->dev, "Mechanical report failed: %d\n",
1046 if (!arizona->pdata.hpdet_acc_id) {
1047 info->detecting = true;
1049 info->jack_flips = 0;
1051 arizona_start_mic(info);
1053 queue_delayed_work(system_power_efficient_wq,
1055 msecs_to_jiffies(HPDET_DEBOUNCE));
1058 regmap_update_bits(arizona->regmap,
1059 ARIZONA_JACK_DETECT_DEBOUNCE,
1060 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
1062 dev_dbg(arizona->dev, "Detected jack removal\n");
1064 arizona_stop_mic(info);
1066 info->num_hpdet_res = 0;
1067 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
1068 info->hpdet_res[i] = 0;
1070 info->hpdet_done = false;
1071 info->hpdet_retried = false;
1073 for (i = 0; i < info->num_micd_ranges; i++)
1074 input_report_key(info->input,
1075 info->micd_ranges[i].key, 0);
1076 input_sync(info->input);
1078 ret = extcon_update_state(info->edev, 0xffffffff, 0);
1080 dev_err(arizona->dev, "Removal report failed: %d\n",
1083 regmap_update_bits(arizona->regmap,
1084 ARIZONA_JACK_DETECT_DEBOUNCE,
1085 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
1086 ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
1089 if (arizona->pdata.micd_timeout)
1090 info->micd_timeout = arizona->pdata.micd_timeout;
1092 info->micd_timeout = DEFAULT_MICD_TIMEOUT;
1095 /* Clear trig_sts to make sure DCVDD is not forced up */
1096 regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
1097 ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
1098 ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
1099 ARIZONA_JD1_FALL_TRIG_STS |
1100 ARIZONA_JD1_RISE_TRIG_STS);
1102 mutex_unlock(&info->lock);
1104 pm_runtime_mark_last_busy(info->dev);
1105 pm_runtime_put_autosuspend(info->dev);
1110 /* Map a level onto a slot in the register bank */
1111 static void arizona_micd_set_level(struct arizona *arizona, int index,
1117 reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
1126 /* Program the level itself */
1127 regmap_update_bits(arizona->regmap, reg, mask, level);
1130 static int arizona_extcon_device_get_pdata(struct arizona *arizona)
1132 struct arizona_pdata *pdata = &arizona->pdata;
1133 unsigned int val = ARIZONA_ACCDET_MODE_HPL;
1135 device_property_read_u32(arizona->dev, "wlf,hpdet-channel", &val);
1137 case ARIZONA_ACCDET_MODE_HPL:
1138 case ARIZONA_ACCDET_MODE_HPR:
1139 pdata->hpdet_channel = val;
1142 dev_err(arizona->dev,
1143 "Wrong wlf,hpdet-channel DT value %d\n", val);
1144 pdata->hpdet_channel = ARIZONA_ACCDET_MODE_HPL;
1147 device_property_read_u32(arizona->dev, "wlf,micd-detect-debounce",
1148 &pdata->micd_detect_debounce);
1150 device_property_read_u32(arizona->dev, "wlf,micd-bias-start-time",
1151 &pdata->micd_bias_start_time);
1153 device_property_read_u32(arizona->dev, "wlf,micd-rate",
1156 device_property_read_u32(arizona->dev, "wlf,micd-dbtime",
1157 &pdata->micd_dbtime);
1159 device_property_read_u32(arizona->dev, "wlf,micd-timeout",
1160 &pdata->micd_timeout);
1162 pdata->micd_force_micbias = device_property_read_bool(arizona->dev,
1163 "wlf,micd-force-micbias");
1168 static int arizona_extcon_probe(struct platform_device *pdev)
1170 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
1171 struct arizona_pdata *pdata = &arizona->pdata;
1172 struct arizona_extcon_info *info;
1174 unsigned int clamp_mode;
1175 int jack_irq_fall, jack_irq_rise;
1176 int ret, mode, i, j;
1178 if (!arizona->dapm || !arizona->dapm->card)
1179 return -EPROBE_DEFER;
1181 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1185 if (!dev_get_platdata(arizona->dev))
1186 arizona_extcon_device_get_pdata(arizona);
1188 info->micvdd = devm_regulator_get(&pdev->dev, "MICVDD");
1189 if (IS_ERR(info->micvdd)) {
1190 ret = PTR_ERR(info->micvdd);
1191 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
1195 mutex_init(&info->lock);
1196 info->arizona = arizona;
1197 info->dev = &pdev->dev;
1198 info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
1199 INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
1200 INIT_DELAYED_WORK(&info->micd_detect_work, arizona_micd_detect);
1201 INIT_DELAYED_WORK(&info->micd_timeout_work, arizona_micd_timeout_work);
1202 platform_set_drvdata(pdev, info);
1204 switch (arizona->type) {
1206 switch (arizona->rev) {
1208 info->micd_reva = true;
1211 info->micd_clamp = true;
1212 info->hpdet_ip_version = 1;
1218 switch (arizona->rev) {
1222 info->micd_clamp = true;
1223 info->hpdet_ip_version = 2;
1231 info->edev = devm_extcon_dev_allocate(&pdev->dev, arizona_cable);
1232 if (IS_ERR(info->edev)) {
1233 dev_err(&pdev->dev, "failed to allocate extcon device\n");
1237 ret = devm_extcon_dev_register(&pdev->dev, info->edev);
1239 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
1244 info->input = devm_input_allocate_device(&pdev->dev);
1246 dev_err(arizona->dev, "Can't allocate input dev\n");
1251 info->input->name = "Headset";
1252 info->input->phys = "arizona/extcon";
1254 if (pdata->num_micd_configs) {
1255 info->micd_modes = pdata->micd_configs;
1256 info->micd_num_modes = pdata->num_micd_configs;
1258 info->micd_modes = micd_default_modes;
1259 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1262 if (arizona->pdata.micd_pol_gpio > 0) {
1263 if (info->micd_modes[0].gpio)
1264 mode = GPIOF_OUT_INIT_HIGH;
1266 mode = GPIOF_OUT_INIT_LOW;
1268 ret = devm_gpio_request_one(&pdev->dev,
1269 arizona->pdata.micd_pol_gpio,
1273 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1274 arizona->pdata.micd_pol_gpio, ret);
1278 if (info->micd_modes[0].gpio)
1279 mode = GPIOD_OUT_HIGH;
1281 mode = GPIOD_OUT_LOW;
1283 /* We can't use devm here because we need to do the get
1284 * against the MFD device, as that is where the of_node
1285 * will reside, but if we devm against that the GPIO
1286 * will not be freed if the extcon driver is unloaded.
1288 info->micd_pol_gpio = gpiod_get_optional(arizona->dev,
1291 if (IS_ERR(info->micd_pol_gpio)) {
1292 ret = PTR_ERR(info->micd_pol_gpio);
1293 dev_err(arizona->dev,
1294 "Failed to get microphone polarity GPIO: %d\n",
1300 if (arizona->pdata.hpdet_id_gpio > 0) {
1301 ret = devm_gpio_request_one(&pdev->dev,
1302 arizona->pdata.hpdet_id_gpio,
1306 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1307 arizona->pdata.hpdet_id_gpio, ret);
1312 if (arizona->pdata.micd_bias_start_time)
1313 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1314 ARIZONA_MICD_BIAS_STARTTIME_MASK,
1315 arizona->pdata.micd_bias_start_time
1316 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1318 if (arizona->pdata.micd_rate)
1319 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1320 ARIZONA_MICD_RATE_MASK,
1321 arizona->pdata.micd_rate
1322 << ARIZONA_MICD_RATE_SHIFT);
1324 switch (arizona->pdata.micd_dbtime) {
1325 case MICD_DBTIME_FOUR_READINGS:
1326 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1327 ARIZONA_MICD_DBTIME_MASK,
1328 ARIZONA_MICD_DBTIME);
1330 case MICD_DBTIME_TWO_READINGS:
1331 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1332 ARIZONA_MICD_DBTIME_MASK, 0);
1338 BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) != 0x40);
1340 if (arizona->pdata.num_micd_ranges) {
1341 info->micd_ranges = pdata->micd_ranges;
1342 info->num_micd_ranges = pdata->num_micd_ranges;
1344 info->micd_ranges = micd_default_ranges;
1345 info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
1348 if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_RANGE) {
1349 dev_err(arizona->dev, "Too many MICD ranges: %d\n",
1350 arizona->pdata.num_micd_ranges);
1353 if (info->num_micd_ranges > 1) {
1354 for (i = 1; i < info->num_micd_ranges; i++) {
1355 if (info->micd_ranges[i - 1].max >
1356 info->micd_ranges[i].max) {
1357 dev_err(arizona->dev,
1358 "MICD ranges must be sorted\n");
1365 /* Disable all buttons by default */
1366 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1367 ARIZONA_MICD_LVL_SEL_MASK, 0x81);
1369 /* Set up all the buttons the user specified */
1370 for (i = 0; i < info->num_micd_ranges; i++) {
1371 for (j = 0; j < ARRAY_SIZE(arizona_micd_levels); j++)
1372 if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
1375 if (j == ARRAY_SIZE(arizona_micd_levels)) {
1376 dev_err(arizona->dev, "Unsupported MICD level %d\n",
1377 info->micd_ranges[i].max);
1382 dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
1383 arizona_micd_levels[j], i);
1385 arizona_micd_set_level(arizona, i, j);
1386 input_set_capability(info->input, EV_KEY,
1387 info->micd_ranges[i].key);
1389 /* Enable reporting of that range */
1390 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1394 /* Set all the remaining keys to a maximum */
1395 for (; i < ARIZONA_MAX_MICD_RANGE; i++)
1396 arizona_micd_set_level(arizona, i, 0x3f);
1399 * If we have a clamp use it, activating in conjunction with
1400 * GPIO5 if that is connected for jack detect operation.
1402 if (info->micd_clamp) {
1403 if (arizona->pdata.jd_gpio5) {
1404 /* Put the GPIO into input mode with optional pull */
1406 if (arizona->pdata.jd_gpio5_nopull)
1407 val &= ~ARIZONA_GPN_PU;
1409 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1412 if (arizona->pdata.jd_invert)
1413 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH_GP5H;
1415 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL_GP5H;
1417 if (arizona->pdata.jd_invert)
1418 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH;
1420 clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL;
1423 regmap_update_bits(arizona->regmap,
1424 ARIZONA_MICD_CLAMP_CONTROL,
1425 ARIZONA_MICD_CLAMP_MODE_MASK, clamp_mode);
1427 regmap_update_bits(arizona->regmap,
1428 ARIZONA_JACK_DETECT_DEBOUNCE,
1429 ARIZONA_MICD_CLAMP_DB,
1430 ARIZONA_MICD_CLAMP_DB);
1433 arizona_extcon_set_mode(info, 0);
1435 pm_runtime_enable(&pdev->dev);
1436 pm_runtime_idle(&pdev->dev);
1437 pm_runtime_get_sync(&pdev->dev);
1439 if (arizona->pdata.jd_gpio5) {
1440 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1441 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1443 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1444 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1447 ret = arizona_request_irq(arizona, jack_irq_rise,
1448 "JACKDET rise", arizona_jackdet, info);
1450 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1455 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1457 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1462 ret = arizona_request_irq(arizona, jack_irq_fall,
1463 "JACKDET fall", arizona_jackdet, info);
1465 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1469 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1471 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1476 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1477 "MICDET", arizona_micdet, info);
1479 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1483 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1484 "HPDET", arizona_hpdet_irq, info);
1486 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1490 arizona_clk32k_enable(arizona);
1491 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1492 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1493 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1494 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1496 ret = regulator_allow_bypass(info->micvdd, true);
1498 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1501 pm_runtime_put(&pdev->dev);
1503 ret = input_register_device(info->input);
1505 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1512 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1514 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1516 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1518 arizona_free_irq(arizona, jack_irq_fall, info);
1520 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1522 arizona_free_irq(arizona, jack_irq_rise, info);
1524 gpiod_put(info->micd_pol_gpio);
1526 pm_runtime_disable(&pdev->dev);
1530 static int arizona_extcon_remove(struct platform_device *pdev)
1532 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1533 struct arizona *arizona = info->arizona;
1534 int jack_irq_rise, jack_irq_fall;
1536 gpiod_put(info->micd_pol_gpio);
1538 pm_runtime_disable(&pdev->dev);
1540 regmap_update_bits(arizona->regmap,
1541 ARIZONA_MICD_CLAMP_CONTROL,
1542 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1544 if (arizona->pdata.jd_gpio5) {
1545 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1546 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1548 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1549 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1552 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1553 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1554 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1555 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1556 arizona_free_irq(arizona, jack_irq_rise, info);
1557 arizona_free_irq(arizona, jack_irq_fall, info);
1558 cancel_delayed_work_sync(&info->hpdet_work);
1559 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1560 ARIZONA_JD1_ENA, 0);
1561 arizona_clk32k_disable(arizona);
1566 static struct platform_driver arizona_extcon_driver = {
1568 .name = "arizona-extcon",
1570 .probe = arizona_extcon_probe,
1571 .remove = arizona_extcon_remove,
1574 module_platform_driver(arizona_extcon_driver);
1576 MODULE_DESCRIPTION("Arizona Extcon driver");
1577 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1578 MODULE_LICENSE("GPL");
1579 MODULE_ALIAS("platform:extcon-arizona");