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 <linux/mfd/arizona/core.h>
31 #include <linux/mfd/arizona/pdata.h>
32 #include <linux/mfd/arizona/registers.h>
34 #define ARIZONA_DEFAULT_HP 32
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;
59 unsigned int hpdet_res[3];
67 struct extcon_dev edev;
70 static const struct arizona_micd_config micd_default_modes[] = {
71 { 0, 2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 },
72 { ARIZONA_ACCDET_SRC, 1 << ARIZONA_MICD_BIAS_SRC_SHIFT, 0 },
78 } arizona_lvl_to_key[ARIZONA_NUM_BUTTONS] = {
87 #define ARIZONA_CABLE_MECHANICAL 0
88 #define ARIZONA_CABLE_MICROPHONE 1
89 #define ARIZONA_CABLE_HEADPHONE 2
90 #define ARIZONA_CABLE_LINEOUT 3
92 static const char *arizona_cable[] = {
100 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
102 struct arizona *arizona = info->arizona;
104 if (arizona->pdata.micd_pol_gpio > 0)
105 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
106 info->micd_modes[mode].gpio);
107 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
108 ARIZONA_MICD_BIAS_SRC_MASK,
109 info->micd_modes[mode].bias);
110 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
111 ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
113 info->micd_mode = mode;
115 dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
118 static void arizona_start_mic(struct arizona_extcon_info *info)
120 struct arizona *arizona = info->arizona;
124 /* Microphone detection can't use idle mode */
125 pm_runtime_get(info->dev);
127 ret = regulator_enable(info->micvdd);
129 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
133 if (info->micd_reva) {
134 regmap_write(arizona->regmap, 0x80, 0x3);
135 regmap_write(arizona->regmap, 0x294, 0);
136 regmap_write(arizona->regmap, 0x80, 0x0);
139 regmap_update_bits(arizona->regmap,
140 ARIZONA_ACCESSORY_DETECT_MODE_1,
141 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
143 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
144 ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
147 regulator_disable(info->micvdd);
148 pm_runtime_put_autosuspend(info->dev);
152 static void arizona_stop_mic(struct arizona_extcon_info *info)
154 struct arizona *arizona = info->arizona;
157 regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
161 if (info->micd_reva) {
162 regmap_write(arizona->regmap, 0x80, 0x3);
163 regmap_write(arizona->regmap, 0x294, 2);
164 regmap_write(arizona->regmap, 0x80, 0x0);
168 regulator_disable(info->micvdd);
169 pm_runtime_mark_last_busy(info->dev);
170 pm_runtime_put_autosuspend(info->dev);
175 unsigned int factor_a;
176 unsigned int factor_b;
177 } arizona_hpdet_b_ranges[] = {
186 } arizona_hpdet_c_ranges[] = {
193 static int arizona_hpdet_read(struct arizona_extcon_info *info)
195 struct arizona *arizona = info->arizona;
196 unsigned int val, range;
199 ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
201 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
206 switch (info->hpdet_ip) {
208 if (!(val & ARIZONA_HP_DONE)) {
209 dev_err(arizona->dev, "HPDET did not complete: %x\n",
211 val = ARIZONA_DEFAULT_HP;
214 val &= ARIZONA_HP_LVL_MASK;
218 if (!(val & ARIZONA_HP_DONE_B)) {
219 dev_err(arizona->dev, "HPDET did not complete: %x\n",
221 return ARIZONA_DEFAULT_HP;
224 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
226 dev_err(arizona->dev, "Failed to read HP value: %d\n",
228 return ARIZONA_DEFAULT_HP;
231 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
233 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
234 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
236 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
237 (val < 100 || val > 0x3fb)) {
239 dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
241 regmap_update_bits(arizona->regmap,
242 ARIZONA_HEADPHONE_DETECT_1,
243 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
245 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
249 /* If we go out of range report top of range */
250 if (val < 100 || val > 0x3fb) {
251 dev_dbg(arizona->dev, "Measurement out of range\n");
255 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
258 val = arizona_hpdet_b_ranges[range].factor_b
260 arizona_hpdet_b_ranges[range].factor_a);
264 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
267 if (!(val & ARIZONA_HP_DONE_B)) {
268 dev_err(arizona->dev, "HPDET did not complete: %x\n",
270 return ARIZONA_DEFAULT_HP;
273 val &= ARIZONA_HP_LVL_B_MASK;
275 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
277 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
278 >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
280 /* Skip up or down a range? */
281 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
283 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
284 arizona_hpdet_c_ranges[range].min,
285 arizona_hpdet_c_ranges[range].max);
286 regmap_update_bits(arizona->regmap,
287 ARIZONA_HEADPHONE_DETECT_1,
288 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
290 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
294 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
295 (val >= arizona_hpdet_c_ranges[range].max)) {
297 dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
298 arizona_hpdet_c_ranges[range].min,
299 arizona_hpdet_c_ranges[range].max);
300 regmap_update_bits(arizona->regmap,
301 ARIZONA_HEADPHONE_DETECT_1,
302 ARIZONA_HP_IMPEDANCE_RANGE_MASK,
304 ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
309 dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
313 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading)
315 struct arizona *arizona = info->arizona;
316 int id_gpio = arizona->pdata.hpdet_id_gpio;
320 * If we're using HPDET for accessory identification we need
321 * to take multiple measurements, step through them in sequence.
323 if (arizona->pdata.hpdet_acc_id) {
324 info->hpdet_res[info->num_hpdet_res++] = *reading;
327 * If the impedence is too high don't measure the
330 if (info->num_hpdet_res == 1 && *reading >= 45) {
331 dev_dbg(arizona->dev, "Skipping ground flip\n");
332 info->hpdet_res[info->num_hpdet_res++] = *reading;
335 if (info->num_hpdet_res == 1) {
336 dev_dbg(arizona->dev, "Flipping ground\n");
338 regmap_update_bits(arizona->regmap,
339 ARIZONA_ACCESSORY_DETECT_MODE_1,
341 ~info->micd_modes[0].src);
343 regmap_update_bits(arizona->regmap,
344 ARIZONA_HEADPHONE_DETECT_1,
345 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
349 /* Only check the mic directly if we didn't already ID it */
350 if (id_gpio && info->num_hpdet_res == 2 &&
351 !((info->hpdet_res[0] > info->hpdet_res[1] * 2))) {
352 dev_dbg(arizona->dev, "Measuring mic\n");
354 regmap_update_bits(arizona->regmap,
355 ARIZONA_ACCESSORY_DETECT_MODE_1,
356 ARIZONA_ACCDET_MODE_MASK |
358 ARIZONA_ACCDET_MODE_HPR |
359 info->micd_modes[0].src);
361 gpio_set_value_cansleep(id_gpio, 1);
363 regmap_update_bits(arizona->regmap,
364 ARIZONA_HEADPHONE_DETECT_1,
365 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
369 /* OK, got both. Now, compare... */
370 dev_dbg(arizona->dev, "HPDET measured %d %d %d\n",
371 info->hpdet_res[0], info->hpdet_res[1],
375 * Either the two grounds measure differently or we
376 * measure the mic as high impedance.
378 if ((info->hpdet_res[0] > info->hpdet_res[1] * 2) ||
379 (id_gpio && info->hpdet_res[2] > 10)) {
380 dev_dbg(arizona->dev, "Detected mic\n");
382 ret = extcon_set_cable_state_(&info->edev,
383 ARIZONA_CABLE_MICROPHONE,
386 dev_err(arizona->dev,
387 "Failed to report mic: %d\n", ret);
390 /* Take the headphone impedance for the main report */
391 *reading = info->hpdet_res[1];
393 dev_dbg(arizona->dev, "Detected headphone\n");
396 /* Make sure everything is reset back to the real polarity */
397 regmap_update_bits(arizona->regmap,
398 ARIZONA_ACCESSORY_DETECT_MODE_1,
400 info->micd_modes[0].src);
406 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
408 struct arizona_extcon_info *info = data;
409 struct arizona *arizona = info->arizona;
410 int id_gpio = arizona->pdata.hpdet_id_gpio;
411 int report = ARIZONA_CABLE_HEADPHONE;
414 mutex_lock(&info->lock);
416 /* If we got a spurious IRQ for some reason then ignore it */
417 if (!info->hpdet_active) {
418 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
419 mutex_unlock(&info->lock);
423 /* If the cable was removed while measuring ignore the result */
424 ret = extcon_get_cable_state_(&info->edev, ARIZONA_CABLE_MECHANICAL);
426 dev_err(arizona->dev, "Failed to check cable state: %d\n",
430 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
434 ret = arizona_hpdet_read(info);
435 if (ret == -EAGAIN) {
437 } else if (ret < 0) {
442 /* Reset back to starting range */
443 regmap_update_bits(arizona->regmap,
444 ARIZONA_HEADPHONE_DETECT_1,
445 ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
448 ret = arizona_hpdet_do_id(info, &reading);
449 if (ret == -EAGAIN) {
451 } else if (ret < 0) {
455 /* Report high impedence cables as line outputs */
457 report = ARIZONA_CABLE_LINEOUT;
459 report = ARIZONA_CABLE_HEADPHONE;
461 ret = extcon_set_cable_state_(&info->edev, report, true);
463 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
466 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0);
468 dev_warn(arizona->dev, "Failed to undo magic: %d\n", ret);
470 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0);
472 dev_warn(arizona->dev, "Failed to undo magic: %d\n", ret);
476 gpio_set_value_cansleep(id_gpio, 0);
478 /* Revert back to MICDET mode */
479 regmap_update_bits(arizona->regmap,
480 ARIZONA_ACCESSORY_DETECT_MODE_1,
481 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
483 /* If we have a mic then reenable MICDET */
485 arizona_start_mic(info);
487 if (info->hpdet_active) {
488 pm_runtime_put_autosuspend(info->dev);
489 info->hpdet_active = false;
493 mutex_unlock(&info->lock);
498 static void arizona_identify_headphone(struct arizona_extcon_info *info)
500 struct arizona *arizona = info->arizona;
503 dev_dbg(arizona->dev, "Starting HPDET\n");
505 /* Make sure we keep the device enabled during the measurement */
506 pm_runtime_get(info->dev);
508 info->hpdet_active = true;
511 arizona_stop_mic(info);
513 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0x4000);
515 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
517 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0x4000);
519 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
521 ret = regmap_update_bits(arizona->regmap,
522 ARIZONA_ACCESSORY_DETECT_MODE_1,
523 ARIZONA_ACCDET_MODE_MASK,
524 ARIZONA_ACCDET_MODE_HPL);
526 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
530 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
531 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
533 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
541 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
542 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
544 /* Just report headphone */
545 ret = extcon_update_state(&info->edev,
546 1 << ARIZONA_CABLE_HEADPHONE,
547 1 << ARIZONA_CABLE_HEADPHONE);
549 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
552 arizona_start_mic(info);
554 info->hpdet_active = false;
557 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
559 struct arizona *arizona = info->arizona;
562 dev_dbg(arizona->dev, "Starting identification via HPDET\n");
564 /* Make sure we keep the device enabled during the measurement */
565 pm_runtime_get(info->dev);
567 info->hpdet_active = true;
569 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0x4000);
571 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
573 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0x4000);
575 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
577 ret = regmap_update_bits(arizona->regmap,
578 ARIZONA_ACCESSORY_DETECT_MODE_1,
579 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
580 info->micd_modes[0].src |
581 ARIZONA_ACCDET_MODE_HPL);
583 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
587 ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
588 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
590 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
598 regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
599 ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
601 /* Just report headphone */
602 ret = extcon_update_state(&info->edev,
603 1 << ARIZONA_CABLE_HEADPHONE,
604 1 << ARIZONA_CABLE_HEADPHONE);
606 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
608 info->hpdet_active = false;
611 static irqreturn_t arizona_micdet(int irq, void *data)
613 struct arizona_extcon_info *info = data;
614 struct arizona *arizona = info->arizona;
615 unsigned int val, lvl;
618 mutex_lock(&info->lock);
620 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
622 dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
623 mutex_unlock(&info->lock);
627 dev_dbg(arizona->dev, "MICDET: %x\n", val);
629 if (!(val & ARIZONA_MICD_VALID)) {
630 dev_warn(arizona->dev, "Microphone detection state invalid\n");
631 mutex_unlock(&info->lock);
635 /* Due to jack detect this should never happen */
636 if (!(val & ARIZONA_MICD_STS)) {
637 dev_warn(arizona->dev, "Detected open circuit\n");
638 info->detecting = false;
642 /* If we got a high impedence we should have a headset, report it. */
643 if (info->detecting && (val & 0x400)) {
644 arizona_identify_headphone(info);
646 ret = extcon_update_state(&info->edev,
647 1 << ARIZONA_CABLE_MICROPHONE,
648 1 << ARIZONA_CABLE_MICROPHONE);
651 dev_err(arizona->dev, "Headset report failed: %d\n",
655 info->detecting = false;
659 /* If we detected a lower impedence during initial startup
660 * then we probably have the wrong polarity, flip it. Don't
661 * do this for the lowest impedences to speed up detection of
662 * plain headphones. If both polarities report a low
663 * impedence then give up and report headphones.
665 if (info->detecting && (val & 0x3f8)) {
668 if (info->jack_flips >= info->micd_num_modes) {
669 dev_dbg(arizona->dev, "Detected HP/line\n");
670 arizona_identify_headphone(info);
672 info->detecting = false;
674 arizona_stop_mic(info);
677 if (info->micd_mode == info->micd_num_modes)
679 arizona_extcon_set_mode(info, info->micd_mode);
688 * If we're still detecting and we detect a short then we've
689 * got a headphone. Otherwise it's a button press.
693 dev_dbg(arizona->dev, "Mic button detected\n");
695 lvl = val & ARIZONA_MICD_LVL_MASK;
696 lvl >>= ARIZONA_MICD_LVL_SHIFT;
698 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
699 if (lvl & arizona_lvl_to_key[i].status)
700 input_report_key(info->input,
701 arizona_lvl_to_key[i].report,
703 input_sync(info->input);
705 } else if (info->detecting) {
706 dev_dbg(arizona->dev, "Headphone detected\n");
707 info->detecting = false;
708 arizona_stop_mic(info);
710 arizona_identify_headphone(info);
712 dev_warn(arizona->dev, "Button with no mic: %x\n",
716 dev_dbg(arizona->dev, "Mic button released\n");
717 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
718 input_report_key(info->input,
719 arizona_lvl_to_key[i].report, 0);
720 input_sync(info->input);
724 pm_runtime_mark_last_busy(info->dev);
725 mutex_unlock(&info->lock);
730 static irqreturn_t arizona_jackdet(int irq, void *data)
732 struct arizona_extcon_info *info = data;
733 struct arizona *arizona = info->arizona;
734 unsigned int val, present, mask;
737 pm_runtime_get_sync(info->dev);
739 mutex_lock(&info->lock);
741 if (arizona->pdata.jd_gpio5) {
742 mask = ARIZONA_MICD_CLAMP_STS;
745 mask = ARIZONA_JD1_STS;
746 present = ARIZONA_JD1_STS;
749 ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
751 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
753 mutex_unlock(&info->lock);
754 pm_runtime_put_autosuspend(info->dev);
758 if ((val & mask) == present) {
759 dev_dbg(arizona->dev, "Detected jack\n");
760 ret = extcon_set_cable_state_(&info->edev,
761 ARIZONA_CABLE_MECHANICAL, true);
764 dev_err(arizona->dev, "Mechanical report failed: %d\n",
767 if (!arizona->pdata.hpdet_acc_id) {
768 info->detecting = true;
770 info->jack_flips = 0;
772 arizona_start_mic(info);
774 arizona_start_hpdet_acc_id(info);
777 dev_dbg(arizona->dev, "Detected jack removal\n");
779 arizona_stop_mic(info);
781 info->num_hpdet_res = 0;
782 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
783 info->hpdet_res[i] = 0;
786 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
787 input_report_key(info->input,
788 arizona_lvl_to_key[i].report, 0);
789 input_sync(info->input);
791 ret = extcon_update_state(&info->edev, 0xffffffff, 0);
793 dev_err(arizona->dev, "Removal report failed: %d\n",
797 mutex_unlock(&info->lock);
799 pm_runtime_mark_last_busy(info->dev);
800 pm_runtime_put_autosuspend(info->dev);
805 static int arizona_extcon_probe(struct platform_device *pdev)
807 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
808 struct arizona_pdata *pdata;
809 struct arizona_extcon_info *info;
810 int jack_irq_fall, jack_irq_rise;
813 pdata = dev_get_platdata(arizona->dev);
815 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
817 dev_err(&pdev->dev, "Failed to allocate memory\n");
822 info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
823 if (IS_ERR(info->micvdd)) {
824 ret = PTR_ERR(info->micvdd);
825 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
829 mutex_init(&info->lock);
830 info->arizona = arizona;
831 info->dev = &pdev->dev;
832 platform_set_drvdata(pdev, info);
834 switch (arizona->type) {
836 switch (arizona->rev) {
838 info->micd_reva = true;
841 info->micd_clamp = true;
850 info->edev.name = "Headset Jack";
851 info->edev.supported_cable = arizona_cable;
853 ret = extcon_dev_register(&info->edev, arizona->dev);
855 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
860 if (pdata->num_micd_configs) {
861 info->micd_modes = pdata->micd_configs;
862 info->micd_num_modes = pdata->num_micd_configs;
864 info->micd_modes = micd_default_modes;
865 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
868 if (arizona->pdata.micd_pol_gpio > 0) {
869 if (info->micd_modes[0].gpio)
870 mode = GPIOF_OUT_INIT_HIGH;
872 mode = GPIOF_OUT_INIT_LOW;
874 ret = devm_gpio_request_one(&pdev->dev,
875 arizona->pdata.micd_pol_gpio,
879 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
880 arizona->pdata.micd_pol_gpio, ret);
885 if (arizona->pdata.hpdet_id_gpio > 0) {
886 ret = devm_gpio_request_one(&pdev->dev,
887 arizona->pdata.hpdet_id_gpio,
891 dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
892 arizona->pdata.hpdet_id_gpio, ret);
897 if (arizona->pdata.micd_bias_start_time)
898 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
899 ARIZONA_MICD_BIAS_STARTTIME_MASK,
900 arizona->pdata.micd_bias_start_time
901 << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
904 * If we have a clamp use it, activating in conjunction with
905 * GPIO5 if that is connected for jack detect operation.
907 if (info->micd_clamp) {
908 if (arizona->pdata.jd_gpio5) {
909 /* Put the GPIO into input mode */
910 regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
913 regmap_update_bits(arizona->regmap,
914 ARIZONA_MICD_CLAMP_CONTROL,
915 ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
917 regmap_update_bits(arizona->regmap,
918 ARIZONA_MICD_CLAMP_CONTROL,
919 ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
922 regmap_update_bits(arizona->regmap,
923 ARIZONA_JACK_DETECT_DEBOUNCE,
924 ARIZONA_MICD_CLAMP_DB,
925 ARIZONA_MICD_CLAMP_DB);
928 arizona_extcon_set_mode(info, 0);
930 info->input = devm_input_allocate_device(&pdev->dev);
932 dev_err(arizona->dev, "Can't allocate input dev\n");
937 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
938 input_set_capability(info->input, EV_KEY,
939 arizona_lvl_to_key[i].report);
940 info->input->name = "Headset";
941 info->input->phys = "arizona/extcon";
942 info->input->dev.parent = &pdev->dev;
944 pm_runtime_enable(&pdev->dev);
945 pm_runtime_idle(&pdev->dev);
946 pm_runtime_get_sync(&pdev->dev);
948 if (arizona->pdata.jd_gpio5) {
949 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
950 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
952 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
953 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
956 ret = arizona_request_irq(arizona, jack_irq_rise,
957 "JACKDET rise", arizona_jackdet, info);
959 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
964 ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
966 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
971 ret = arizona_request_irq(arizona, jack_irq_fall,
972 "JACKDET fall", arizona_jackdet, info);
974 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
978 ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
980 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
985 ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
986 "MICDET", arizona_micdet, info);
988 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
992 ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
993 "HPDET", arizona_hpdet_irq, info);
995 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
999 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1000 ARIZONA_MICD_RATE_MASK,
1001 8 << ARIZONA_MICD_RATE_SHIFT);
1003 arizona_clk32k_enable(arizona);
1004 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1005 ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1006 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1007 ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1009 ret = regulator_allow_bypass(info->micvdd, true);
1011 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1014 pm_runtime_put(&pdev->dev);
1016 ret = input_register_device(info->input);
1018 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1025 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1027 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1029 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1031 arizona_free_irq(arizona, jack_irq_fall, info);
1033 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1035 arizona_free_irq(arizona, jack_irq_rise, info);
1038 pm_runtime_disable(&pdev->dev);
1039 extcon_dev_unregister(&info->edev);
1044 static int arizona_extcon_remove(struct platform_device *pdev)
1046 struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1047 struct arizona *arizona = info->arizona;
1048 int jack_irq_rise, jack_irq_fall;
1050 pm_runtime_disable(&pdev->dev);
1052 regmap_update_bits(arizona->regmap,
1053 ARIZONA_MICD_CLAMP_CONTROL,
1054 ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1056 if (arizona->pdata.jd_gpio5) {
1057 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1058 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1060 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1061 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1064 arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1065 arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1066 arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1067 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1068 arizona_free_irq(arizona, jack_irq_rise, info);
1069 arizona_free_irq(arizona, jack_irq_fall, info);
1070 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1071 ARIZONA_JD1_ENA, 0);
1072 arizona_clk32k_disable(arizona);
1073 extcon_dev_unregister(&info->edev);
1078 static struct platform_driver arizona_extcon_driver = {
1080 .name = "arizona-extcon",
1081 .owner = THIS_MODULE,
1083 .probe = arizona_extcon_probe,
1084 .remove = arizona_extcon_remove,
1087 module_platform_driver(arizona_extcon_driver);
1089 MODULE_DESCRIPTION("Arizona Extcon driver");
1090 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1091 MODULE_LICENSE("GPL");
1092 MODULE_ALIAS("platform:extcon-arizona");