]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/extcon/extcon-arizona.c
extcon: arizona: Retry HPDET identification for high impedance
[karo-tx-linux.git] / drivers / extcon / extcon-arizona.c
1 /*
2  * extcon-arizona.c - Extcon driver Wolfson Arizona devices
3  *
4  *  Copyright (C) 2012 Wolfson Microelectronics plc
5  *
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.
10  *
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.
15  */
16
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>
29
30 #include <sound/soc.h>
31
32 #include <linux/mfd/arizona/core.h>
33 #include <linux/mfd/arizona/pdata.h>
34 #include <linux/mfd/arizona/registers.h>
35
36 #define ARIZONA_MAX_MICD_RANGE 8
37
38 #define ARIZONA_ACCDET_MODE_MIC 0
39 #define ARIZONA_ACCDET_MODE_HPL 1
40 #define ARIZONA_ACCDET_MODE_HPR 2
41
42 #define ARIZONA_HPDET_MAX 10000
43
44 #define HPDET_DEBOUNCE 500
45
46 struct arizona_extcon_info {
47         struct device *dev;
48         struct arizona *arizona;
49         struct mutex lock;
50         struct regulator *micvdd;
51         struct input_dev *input;
52
53         u16 last_jackdet;
54
55         int micd_mode;
56         const struct arizona_micd_config *micd_modes;
57         int micd_num_modes;
58
59         const struct arizona_micd_range *micd_ranges;
60         int num_micd_ranges;
61
62         bool micd_reva;
63         bool micd_clamp;
64
65         struct delayed_work hpdet_work;
66
67         bool hpdet_active;
68         bool hpdet_done;
69         bool hpdet_retried;
70
71         int num_hpdet_res;
72         unsigned int hpdet_res[3];
73
74         bool mic;
75         bool detecting;
76         int jack_flips;
77
78         int hpdet_ip;
79
80         struct extcon_dev edev;
81 };
82
83 static const struct arizona_micd_config micd_default_modes[] = {
84         { ARIZONA_ACCDET_SRC, 1 << ARIZONA_MICD_BIAS_SRC_SHIFT, 0 },
85         { 0,                  2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 },
86 };
87
88 static const struct arizona_micd_range micd_default_ranges[] = {
89         { .max =  11, .key = BTN_0 },
90         { .max =  28, .key = BTN_1 },
91         { .max =  54, .key = BTN_2 },
92         { .max = 100, .key = BTN_3 },
93         { .max = 186, .key = BTN_4 },
94         { .max = 430, .key = BTN_5 },
95 };
96
97 static const int arizona_micd_levels[] = {
98         3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
99         49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
100         105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
101         270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
102         1257,
103 };
104
105 #define ARIZONA_CABLE_MECHANICAL 0
106 #define ARIZONA_CABLE_MICROPHONE 1
107 #define ARIZONA_CABLE_HEADPHONE  2
108 #define ARIZONA_CABLE_LINEOUT    3
109
110 static const char *arizona_cable[] = {
111         "Mechanical",
112         "Microphone",
113         "Headphone",
114         "Line-out",
115         NULL,
116 };
117
118 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info);
119
120 static void arizona_extcon_do_magic(struct arizona_extcon_info *info,
121                                     unsigned int magic)
122 {
123         struct arizona *arizona = info->arizona;
124         int ret;
125
126         mutex_lock(&arizona->dapm->card->dapm_mutex);
127
128         arizona->hpdet_magic = magic;
129
130         /* Keep the HP output stages disabled while doing the magic */
131         if (magic) {
132                 ret = regmap_update_bits(arizona->regmap,
133                                          ARIZONA_OUTPUT_ENABLES_1,
134                                          ARIZONA_OUT1L_ENA |
135                                          ARIZONA_OUT1R_ENA, 0);
136                 if (ret != 0)
137                         dev_warn(arizona->dev,
138                                 "Failed to disable headphone outputs: %d\n",
139                                  ret);
140         }
141
142         ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000,
143                                  magic);
144         if (ret != 0)
145                 dev_warn(arizona->dev, "Failed to do magic: %d\n",
146                                  ret);
147
148         ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000,
149                                  magic);
150         if (ret != 0)
151                 dev_warn(arizona->dev, "Failed to do magic: %d\n",
152                          ret);
153
154         /* Restore the desired state while not doing the magic */
155         if (!magic) {
156                 ret = regmap_update_bits(arizona->regmap,
157                                          ARIZONA_OUTPUT_ENABLES_1,
158                                          ARIZONA_OUT1L_ENA |
159                                          ARIZONA_OUT1R_ENA, arizona->hp_ena);
160                 if (ret != 0)
161                         dev_warn(arizona->dev,
162                                  "Failed to restore headphone outputs: %d\n",
163                                  ret);
164         }
165
166         mutex_unlock(&arizona->dapm->card->dapm_mutex);
167 }
168
169 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
170 {
171         struct arizona *arizona = info->arizona;
172
173         mode %= info->micd_num_modes;
174
175         if (arizona->pdata.micd_pol_gpio > 0)
176                 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
177                                         info->micd_modes[mode].gpio);
178         regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
179                            ARIZONA_MICD_BIAS_SRC_MASK,
180                            info->micd_modes[mode].bias);
181         regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
182                            ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
183
184         info->micd_mode = mode;
185
186         dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
187 }
188
189 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
190 {
191         switch (info->micd_modes[0].bias >> ARIZONA_MICD_BIAS_SRC_SHIFT) {
192         case 1:
193                 return "MICBIAS1";
194         case 2:
195                 return "MICBIAS2";
196         case 3:
197                 return "MICBIAS3";
198         default:
199                 return "MICVDD";
200         }
201 }
202
203 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
204 {
205         struct arizona *arizona = info->arizona;
206         const char *widget = arizona_extcon_get_micbias(info);
207         struct snd_soc_dapm_context *dapm = arizona->dapm;
208         int ret;
209
210         mutex_lock(&dapm->card->dapm_mutex);
211
212         ret = snd_soc_dapm_force_enable_pin(dapm, widget);
213         if (ret != 0)
214                 dev_warn(arizona->dev, "Failed to enable %s: %d\n",
215                          widget, ret);
216
217         mutex_unlock(&dapm->card->dapm_mutex);
218
219         snd_soc_dapm_sync(dapm);
220
221         if (!arizona->pdata.micd_force_micbias) {
222                 mutex_lock(&dapm->card->dapm_mutex);
223
224                 ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
225                 if (ret != 0)
226                         dev_warn(arizona->dev, "Failed to disable %s: %d\n",
227                                  widget, ret);
228
229                 mutex_unlock(&dapm->card->dapm_mutex);
230
231                 snd_soc_dapm_sync(dapm);
232         }
233 }
234
235 static void arizona_start_mic(struct arizona_extcon_info *info)
236 {
237         struct arizona *arizona = info->arizona;
238         bool change;
239         int ret;
240
241         /* Microphone detection can't use idle mode */
242         pm_runtime_get(info->dev);
243
244         if (info->detecting) {
245                 ret = regulator_allow_bypass(info->micvdd, false);
246                 if (ret != 0) {
247                         dev_err(arizona->dev,
248                                 "Failed to regulate MICVDD: %d\n",
249                                 ret);
250                 }
251         }
252
253         ret = regulator_enable(info->micvdd);
254         if (ret != 0) {
255                 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
256                         ret);
257         }
258
259         if (info->micd_reva) {
260                 regmap_write(arizona->regmap, 0x80, 0x3);
261                 regmap_write(arizona->regmap, 0x294, 0);
262                 regmap_write(arizona->regmap, 0x80, 0x0);
263         }
264
265         regmap_update_bits(arizona->regmap,
266                            ARIZONA_ACCESSORY_DETECT_MODE_1,
267                            ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
268
269         arizona_extcon_pulse_micbias(info);
270
271         regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
272                                  ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
273                                  &change);
274         if (!change) {
275                 regulator_disable(info->micvdd);
276                 pm_runtime_put_autosuspend(info->dev);
277         }
278 }
279
280 static void arizona_stop_mic(struct arizona_extcon_info *info)
281 {
282         struct arizona *arizona = info->arizona;
283         const char *widget = arizona_extcon_get_micbias(info);
284         struct snd_soc_dapm_context *dapm = arizona->dapm;
285         bool change;
286         int ret;
287
288         regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
289                                  ARIZONA_MICD_ENA, 0,
290                                  &change);
291
292         mutex_lock(&dapm->card->dapm_mutex);
293
294         ret = snd_soc_dapm_disable_pin(dapm, widget);
295         if (ret != 0)
296                 dev_warn(arizona->dev,
297                          "Failed to disable %s: %d\n",
298                          widget, ret);
299
300         mutex_unlock(&dapm->card->dapm_mutex);
301
302         snd_soc_dapm_sync(dapm);
303
304         if (info->micd_reva) {
305                 regmap_write(arizona->regmap, 0x80, 0x3);
306                 regmap_write(arizona->regmap, 0x294, 2);
307                 regmap_write(arizona->regmap, 0x80, 0x0);
308         }
309
310         ret = regulator_allow_bypass(info->micvdd, true);
311         if (ret != 0) {
312                 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
313                         ret);
314         }
315
316         if (change) {
317                 regulator_disable(info->micvdd);
318                 pm_runtime_mark_last_busy(info->dev);
319                 pm_runtime_put_autosuspend(info->dev);
320         }
321 }
322
323 static struct {
324         unsigned int factor_a;
325         unsigned int factor_b;
326 } arizona_hpdet_b_ranges[] = {
327         {  5528,   362464 },
328         { 11084,  6186851 },
329         { 11065, 65460395 },
330 };
331
332 static struct {
333         int min;
334         int max;
335 } arizona_hpdet_c_ranges[] = {
336         { 0,       30 },
337         { 8,      100 },
338         { 100,   1000 },
339         { 1000, 10000 },
340 };
341
342 static int arizona_hpdet_read(struct arizona_extcon_info *info)
343 {
344         struct arizona *arizona = info->arizona;
345         unsigned int val, range;
346         int ret;
347
348         ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
349         if (ret != 0) {
350                 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
351                         ret);
352                 return ret;
353         }
354
355         switch (info->hpdet_ip) {
356         case 0:
357                 if (!(val & ARIZONA_HP_DONE)) {
358                         dev_err(arizona->dev, "HPDET did not complete: %x\n",
359                                 val);
360                         return -EAGAIN;
361                 }
362
363                 val &= ARIZONA_HP_LVL_MASK;
364                 break;
365
366         case 1:
367                 if (!(val & ARIZONA_HP_DONE_B)) {
368                         dev_err(arizona->dev, "HPDET did not complete: %x\n",
369                                 val);
370                         return -EAGAIN;
371                 }
372
373                 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
374                 if (ret != 0) {
375                         dev_err(arizona->dev, "Failed to read HP value: %d\n",
376                                 ret);
377                         return -EAGAIN;
378                 }
379
380                 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
381                             &range);
382                 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
383                            >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
384
385                 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
386                     (val < 100 || val > 0x3fb)) {
387                         range++;
388                         dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
389                                 range);
390                         regmap_update_bits(arizona->regmap,
391                                            ARIZONA_HEADPHONE_DETECT_1,
392                                            ARIZONA_HP_IMPEDANCE_RANGE_MASK,
393                                            range <<
394                                            ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
395                         return -EAGAIN;
396                 }
397
398                 /* If we go out of range report top of range */
399                 if (val < 100 || val > 0x3fb) {
400                         dev_dbg(arizona->dev, "Measurement out of range\n");
401                         return ARIZONA_HPDET_MAX;
402                 }
403
404                 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
405                         val, range);
406
407                 val = arizona_hpdet_b_ranges[range].factor_b
408                         / ((val * 100) -
409                            arizona_hpdet_b_ranges[range].factor_a);
410                 break;
411
412         default:
413                 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
414                          info->hpdet_ip);
415         case 2:
416                 if (!(val & ARIZONA_HP_DONE_B)) {
417                         dev_err(arizona->dev, "HPDET did not complete: %x\n",
418                                 val);
419                         return -EAGAIN;
420                 }
421
422                 val &= ARIZONA_HP_LVL_B_MASK;
423
424                 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
425                             &range);
426                 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
427                            >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
428
429                 /* Skip up or down a range? */
430                 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
431                         range--;
432                         dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
433                                 arizona_hpdet_c_ranges[range].min,
434                                 arizona_hpdet_c_ranges[range].max);
435                         regmap_update_bits(arizona->regmap,
436                                            ARIZONA_HEADPHONE_DETECT_1,
437                                            ARIZONA_HP_IMPEDANCE_RANGE_MASK,
438                                            range <<
439                                            ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
440                         return -EAGAIN;
441                 }
442
443                 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
444                     (val >= arizona_hpdet_c_ranges[range].max)) {
445                         range++;
446                         dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
447                                 arizona_hpdet_c_ranges[range].min,
448                                 arizona_hpdet_c_ranges[range].max);
449                         regmap_update_bits(arizona->regmap,
450                                            ARIZONA_HEADPHONE_DETECT_1,
451                                            ARIZONA_HP_IMPEDANCE_RANGE_MASK,
452                                            range <<
453                                            ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
454                         return -EAGAIN;
455                 }
456         }
457
458         dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
459         return val;
460 }
461
462 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading)
463 {
464         struct arizona *arizona = info->arizona;
465         int id_gpio = arizona->pdata.hpdet_id_gpio;
466
467         /*
468          * If we're using HPDET for accessory identification we need
469          * to take multiple measurements, step through them in sequence.
470          */
471         if (arizona->pdata.hpdet_acc_id) {
472                 info->hpdet_res[info->num_hpdet_res++] = *reading;
473
474                 /*
475                  * If the impedence is too high don't measure the
476                  * second ground.
477                  */
478                 if (info->num_hpdet_res == 1 && *reading >= 45) {
479                         dev_dbg(arizona->dev, "Skipping ground flip\n");
480                         info->hpdet_res[info->num_hpdet_res++] = *reading;
481                 }
482
483                 if (info->num_hpdet_res == 1) {
484                         dev_dbg(arizona->dev, "Flipping ground\n");
485
486                         regmap_update_bits(arizona->regmap,
487                                            ARIZONA_ACCESSORY_DETECT_MODE_1,
488                                            ARIZONA_ACCDET_SRC,
489                                            ~info->micd_modes[0].src);
490
491                         regmap_update_bits(arizona->regmap,
492                                            ARIZONA_HEADPHONE_DETECT_1,
493                                            ARIZONA_HP_POLL, ARIZONA_HP_POLL);
494                         return -EAGAIN;
495                 }
496
497                 /* Only check the mic directly if we didn't already ID it */
498                 if (id_gpio && info->num_hpdet_res == 2 &&
499                     !((info->hpdet_res[0] > info->hpdet_res[1] * 2))) {
500                         dev_dbg(arizona->dev, "Measuring mic\n");
501
502                         regmap_update_bits(arizona->regmap,
503                                            ARIZONA_ACCESSORY_DETECT_MODE_1,
504                                            ARIZONA_ACCDET_MODE_MASK |
505                                            ARIZONA_ACCDET_SRC,
506                                            ARIZONA_ACCDET_MODE_HPR |
507                                            info->micd_modes[0].src);
508
509                         gpio_set_value_cansleep(id_gpio, 1);
510
511                         regmap_update_bits(arizona->regmap,
512                                            ARIZONA_HEADPHONE_DETECT_1,
513                                            ARIZONA_HP_POLL, ARIZONA_HP_POLL);
514                         return -EAGAIN;
515                 }
516
517                 /* OK, got both.  Now, compare... */
518                 dev_dbg(arizona->dev, "HPDET measured %d %d %d\n",
519                         info->hpdet_res[0], info->hpdet_res[1],
520                         info->hpdet_res[2]);
521
522
523                 /* Take the headphone impedance for the main report */
524                 *reading = info->hpdet_res[0];
525
526                 /* Sometimes we get false readings due to slow insert */
527                 if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
528                         dev_dbg(arizona->dev, "Retrying high impedance\n");
529                         info->num_hpdet_res = 0;
530                         info->hpdet_retried = true;
531                         arizona_start_hpdet_acc_id(info);
532                         pm_runtime_put(info->dev);
533                         return -EAGAIN;
534                 }
535
536                 /*
537                  * Either the two grounds measure differently or we
538                  * measure the mic as high impedance.
539                  */
540                 if ((info->hpdet_res[0] > info->hpdet_res[1] * 2) ||
541                     (id_gpio && info->hpdet_res[2] > 1257)) {
542                         dev_dbg(arizona->dev, "Detected mic\n");
543                         info->mic = true;
544                         info->detecting = true;
545                 } else {
546                         dev_dbg(arizona->dev, "Detected headphone\n");
547                 }
548
549                 /* Make sure everything is reset back to the real polarity */
550                 regmap_update_bits(arizona->regmap,
551                                    ARIZONA_ACCESSORY_DETECT_MODE_1,
552                                    ARIZONA_ACCDET_SRC,
553                                    info->micd_modes[0].src);
554         }
555
556         return 0;
557 }
558
559 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
560 {
561         struct arizona_extcon_info *info = data;
562         struct arizona *arizona = info->arizona;
563         int id_gpio = arizona->pdata.hpdet_id_gpio;
564         int report = ARIZONA_CABLE_HEADPHONE;
565         int ret, reading;
566
567         mutex_lock(&info->lock);
568
569         /* If we got a spurious IRQ for some reason then ignore it */
570         if (!info->hpdet_active) {
571                 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
572                 mutex_unlock(&info->lock);
573                 return IRQ_NONE;
574         }
575
576         /* If the cable was removed while measuring ignore the result */
577         ret = extcon_get_cable_state_(&info->edev, ARIZONA_CABLE_MECHANICAL);
578         if (ret < 0) {
579                 dev_err(arizona->dev, "Failed to check cable state: %d\n",
580                         ret);
581                 goto out;
582         } else if (!ret) {
583                 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
584                 goto done;
585         }
586
587         ret = arizona_hpdet_read(info);
588         if (ret == -EAGAIN) {
589                 goto out;
590         } else if (ret < 0) {
591                 goto done;
592         }
593         reading = ret;
594
595         /* Reset back to starting range */
596         regmap_update_bits(arizona->regmap,
597                            ARIZONA_HEADPHONE_DETECT_1,
598                            ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
599                            0);
600
601         ret = arizona_hpdet_do_id(info, &reading);
602         if (ret == -EAGAIN) {
603                 goto out;
604         } else if (ret < 0) {
605                 goto done;
606         }
607
608         /* Report high impedence cables as line outputs */
609         if (reading >= 5000)
610                 report = ARIZONA_CABLE_LINEOUT;
611         else
612                 report = ARIZONA_CABLE_HEADPHONE;
613
614         ret = extcon_set_cable_state_(&info->edev, report, true);
615         if (ret != 0)
616                 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
617                         ret);
618
619         arizona_extcon_do_magic(info, 0);
620
621 done:
622         if (id_gpio)
623                 gpio_set_value_cansleep(id_gpio, 0);
624
625         /* Revert back to MICDET mode */
626         regmap_update_bits(arizona->regmap,
627                            ARIZONA_ACCESSORY_DETECT_MODE_1,
628                            ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
629
630         /* If we have a mic then reenable MICDET */
631         if (info->mic)
632                 arizona_start_mic(info);
633
634         if (info->hpdet_active) {
635                 pm_runtime_put_autosuspend(info->dev);
636                 info->hpdet_active = false;
637         }
638
639         info->hpdet_done = true;
640
641 out:
642         mutex_unlock(&info->lock);
643
644         return IRQ_HANDLED;
645 }
646
647 static void arizona_identify_headphone(struct arizona_extcon_info *info)
648 {
649         struct arizona *arizona = info->arizona;
650         int ret;
651
652         if (info->hpdet_done)
653                 return;
654
655         dev_dbg(arizona->dev, "Starting HPDET\n");
656
657         /* Make sure we keep the device enabled during the measurement */
658         pm_runtime_get(info->dev);
659
660         info->hpdet_active = true;
661
662         if (info->mic)
663                 arizona_stop_mic(info);
664
665         arizona_extcon_do_magic(info, 0x4000);
666
667         ret = regmap_update_bits(arizona->regmap,
668                                  ARIZONA_ACCESSORY_DETECT_MODE_1,
669                                  ARIZONA_ACCDET_MODE_MASK,
670                                  ARIZONA_ACCDET_MODE_HPL);
671         if (ret != 0) {
672                 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
673                 goto err;
674         }
675
676         ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
677                                  ARIZONA_HP_POLL, ARIZONA_HP_POLL);
678         if (ret != 0) {
679                 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
680                         ret);
681                 goto err;
682         }
683
684         return;
685
686 err:
687         regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
688                            ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
689
690         /* Just report headphone */
691         ret = extcon_update_state(&info->edev,
692                                   1 << ARIZONA_CABLE_HEADPHONE,
693                                   1 << ARIZONA_CABLE_HEADPHONE);
694         if (ret != 0)
695                 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
696
697         if (info->mic)
698                 arizona_start_mic(info);
699
700         info->hpdet_active = false;
701 }
702
703 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
704 {
705         struct arizona *arizona = info->arizona;
706         int ret;
707
708         dev_dbg(arizona->dev, "Starting identification via HPDET\n");
709
710         /* Make sure we keep the device enabled during the measurement */
711         pm_runtime_get_sync(info->dev);
712
713         info->hpdet_active = true;
714
715         arizona_extcon_do_magic(info, 0x4000);
716
717         ret = regmap_update_bits(arizona->regmap,
718                                  ARIZONA_ACCESSORY_DETECT_MODE_1,
719                                  ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
720                                  info->micd_modes[0].src |
721                                  ARIZONA_ACCDET_MODE_HPL);
722         if (ret != 0) {
723                 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
724                 goto err;
725         }
726
727         ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
728                                  ARIZONA_HP_POLL, ARIZONA_HP_POLL);
729         if (ret != 0) {
730                 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
731                         ret);
732                 goto err;
733         }
734
735         return;
736
737 err:
738         regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
739                            ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
740
741         /* Just report headphone */
742         ret = extcon_update_state(&info->edev,
743                                   1 << ARIZONA_CABLE_HEADPHONE,
744                                   1 << ARIZONA_CABLE_HEADPHONE);
745         if (ret != 0)
746                 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
747
748         info->hpdet_active = false;
749 }
750
751 static irqreturn_t arizona_micdet(int irq, void *data)
752 {
753         struct arizona_extcon_info *info = data;
754         struct arizona *arizona = info->arizona;
755         unsigned int val = 0, lvl;
756         int ret, i, key;
757
758         mutex_lock(&info->lock);
759
760         for (i = 0; i < 10 && !(val & 0x7fc); i++) {
761                 ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
762                 if (ret != 0) {
763                         dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
764                         mutex_unlock(&info->lock);
765                         return IRQ_NONE;
766                 }
767
768                 dev_dbg(arizona->dev, "MICDET: %x\n", val);
769
770                 if (!(val & ARIZONA_MICD_VALID)) {
771                         dev_warn(arizona->dev, "Microphone detection state invalid\n");
772                         mutex_unlock(&info->lock);
773                         return IRQ_NONE;
774                 }
775         }
776
777         if (i == 10 && !(val & 0x7fc)) {
778                 dev_err(arizona->dev, "Failed to get valid MICDET value\n");
779                 mutex_unlock(&info->lock);
780                 return IRQ_NONE;
781         }
782
783         /* Due to jack detect this should never happen */
784         if (!(val & ARIZONA_MICD_STS)) {
785                 dev_warn(arizona->dev, "Detected open circuit\n");
786                 info->detecting = false;
787                 goto handled;
788         }
789
790         /* If we got a high impedence we should have a headset, report it. */
791         if (info->detecting && (val & 0x400)) {
792                 arizona_identify_headphone(info);
793
794                 ret = extcon_update_state(&info->edev,
795                                           1 << ARIZONA_CABLE_MICROPHONE,
796                                           1 << ARIZONA_CABLE_MICROPHONE);
797
798                 if (ret != 0)
799                         dev_err(arizona->dev, "Headset report failed: %d\n",
800                                 ret);
801
802                 /* Don't need to regulate for button detection */
803                 ret = regulator_allow_bypass(info->micvdd, false);
804                 if (ret != 0) {
805                         dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
806                                 ret);
807                 }
808
809                 info->mic = true;
810                 info->detecting = false;
811                 goto handled;
812         }
813
814         /* If we detected a lower impedence during initial startup
815          * then we probably have the wrong polarity, flip it.  Don't
816          * do this for the lowest impedences to speed up detection of
817          * plain headphones.  If both polarities report a low
818          * impedence then give up and report headphones.
819          */
820         if (info->detecting && (val & 0x3f8)) {
821                 if (info->jack_flips >= info->micd_num_modes * 10) {
822                         dev_dbg(arizona->dev, "Detected HP/line\n");
823                         arizona_identify_headphone(info);
824
825                         info->detecting = false;
826
827                         arizona_stop_mic(info);
828                 } else {
829                         info->micd_mode++;
830                         if (info->micd_mode == info->micd_num_modes)
831                                 info->micd_mode = 0;
832                         arizona_extcon_set_mode(info, info->micd_mode);
833
834                         info->jack_flips++;
835                 }
836
837                 goto handled;
838         }
839
840         /*
841          * If we're still detecting and we detect a short then we've
842          * got a headphone.  Otherwise it's a button press.
843          */
844         if (val & 0x3fc) {
845                 if (info->mic) {
846                         dev_dbg(arizona->dev, "Mic button detected\n");
847
848                         lvl = val & ARIZONA_MICD_LVL_MASK;
849                         lvl >>= ARIZONA_MICD_LVL_SHIFT;
850
851                         WARN_ON(!lvl);
852                         WARN_ON(ffs(lvl) - 1 >= info->num_micd_ranges);
853                         if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
854                                 key = info->micd_ranges[ffs(lvl) - 1].key;
855                                 input_report_key(info->input, key, 1);
856                                 input_sync(info->input);
857                         }
858
859                 } else if (info->detecting) {
860                         dev_dbg(arizona->dev, "Headphone detected\n");
861                         info->detecting = false;
862                         arizona_stop_mic(info);
863
864                         arizona_identify_headphone(info);
865                 } else {
866                         dev_warn(arizona->dev, "Button with no mic: %x\n",
867                                  val);
868                 }
869         } else {
870                 dev_dbg(arizona->dev, "Mic button released\n");
871                 for (i = 0; i < info->num_micd_ranges; i++)
872                         input_report_key(info->input,
873                                          info->micd_ranges[i].key, 0);
874                 input_sync(info->input);
875                 arizona_extcon_pulse_micbias(info);
876         }
877
878 handled:
879         pm_runtime_mark_last_busy(info->dev);
880         mutex_unlock(&info->lock);
881
882         return IRQ_HANDLED;
883 }
884
885 static void arizona_hpdet_work(struct work_struct *work)
886 {
887         struct arizona_extcon_info *info = container_of(work,
888                                                         struct arizona_extcon_info,
889                                                         hpdet_work.work);
890
891         mutex_lock(&info->lock);
892         arizona_start_hpdet_acc_id(info);
893         mutex_unlock(&info->lock);
894 }
895
896 static irqreturn_t arizona_jackdet(int irq, void *data)
897 {
898         struct arizona_extcon_info *info = data;
899         struct arizona *arizona = info->arizona;
900         unsigned int val, present, mask;
901         bool cancelled;
902         int ret, i;
903
904         cancelled = cancel_delayed_work_sync(&info->hpdet_work);
905
906         pm_runtime_get_sync(info->dev);
907
908         mutex_lock(&info->lock);
909
910         if (arizona->pdata.jd_gpio5) {
911                 mask = ARIZONA_MICD_CLAMP_STS;
912                 present = 0;
913         } else {
914                 mask = ARIZONA_JD1_STS;
915                 present = ARIZONA_JD1_STS;
916         }
917
918         ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
919         if (ret != 0) {
920                 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
921                         ret);
922                 mutex_unlock(&info->lock);
923                 pm_runtime_put_autosuspend(info->dev);
924                 return IRQ_NONE;
925         }
926
927         val &= mask;
928         if (val == info->last_jackdet) {
929                 dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
930                 if (cancelled)
931                         schedule_delayed_work(&info->hpdet_work,
932                                               msecs_to_jiffies(HPDET_DEBOUNCE));
933
934                 goto out;
935         }
936         info->last_jackdet = val;
937
938         if (info->last_jackdet == present) {
939                 dev_dbg(arizona->dev, "Detected jack\n");
940                 ret = extcon_set_cable_state_(&info->edev,
941                                               ARIZONA_CABLE_MECHANICAL, true);
942
943                 if (ret != 0)
944                         dev_err(arizona->dev, "Mechanical report failed: %d\n",
945                                 ret);
946
947                 if (!arizona->pdata.hpdet_acc_id) {
948                         info->detecting = true;
949                         info->mic = false;
950                         info->jack_flips = 0;
951
952                         arizona_start_mic(info);
953                 } else {
954                         schedule_delayed_work(&info->hpdet_work,
955                                               msecs_to_jiffies(HPDET_DEBOUNCE));
956                 }
957
958                 regmap_update_bits(arizona->regmap,
959                                    ARIZONA_JACK_DETECT_DEBOUNCE,
960                                    ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
961         } else {
962                 dev_dbg(arizona->dev, "Detected jack removal\n");
963
964                 arizona_stop_mic(info);
965
966                 info->num_hpdet_res = 0;
967                 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
968                         info->hpdet_res[i] = 0;
969                 info->mic = false;
970                 info->hpdet_done = false;
971                 info->hpdet_retried = false;
972
973                 for (i = 0; i < info->num_micd_ranges; i++)
974                         input_report_key(info->input,
975                                          info->micd_ranges[i].key, 0);
976                 input_sync(info->input);
977
978                 ret = extcon_update_state(&info->edev, 0xffffffff, 0);
979                 if (ret != 0)
980                         dev_err(arizona->dev, "Removal report failed: %d\n",
981                                 ret);
982
983                 regmap_update_bits(arizona->regmap,
984                                    ARIZONA_JACK_DETECT_DEBOUNCE,
985                                    ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
986                                    ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
987         }
988
989         /* Clear trig_sts to make sure DCVDD is not forced up */
990         regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
991                      ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
992                      ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
993                      ARIZONA_JD1_FALL_TRIG_STS |
994                      ARIZONA_JD1_RISE_TRIG_STS);
995
996 out:
997         mutex_unlock(&info->lock);
998
999         pm_runtime_mark_last_busy(info->dev);
1000         pm_runtime_put_autosuspend(info->dev);
1001
1002         return IRQ_HANDLED;
1003 }
1004
1005 /* Map a level onto a slot in the register bank */
1006 static void arizona_micd_set_level(struct arizona *arizona, int index,
1007                                    unsigned int level)
1008 {
1009         int reg;
1010         unsigned int mask;
1011
1012         reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
1013
1014         if (!(index % 2)) {
1015                 mask = 0x3f00;
1016                 level <<= 8;
1017         } else {
1018                 mask = 0x3f;
1019         }
1020
1021         /* Program the level itself */
1022         regmap_update_bits(arizona->regmap, reg, mask, level);
1023 }
1024
1025 static int arizona_extcon_probe(struct platform_device *pdev)
1026 {
1027         struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
1028         struct arizona_pdata *pdata;
1029         struct arizona_extcon_info *info;
1030         unsigned int val;
1031         int jack_irq_fall, jack_irq_rise;
1032         int ret, mode, i, j;
1033
1034         if (!arizona->dapm || !arizona->dapm->card)
1035                 return -EPROBE_DEFER;
1036
1037         pdata = dev_get_platdata(arizona->dev);
1038
1039         info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1040         if (!info) {
1041                 dev_err(&pdev->dev, "Failed to allocate memory\n");
1042                 ret = -ENOMEM;
1043                 goto err;
1044         }
1045
1046         info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
1047         if (IS_ERR(info->micvdd)) {
1048                 ret = PTR_ERR(info->micvdd);
1049                 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
1050                 goto err;
1051         }
1052
1053         mutex_init(&info->lock);
1054         info->arizona = arizona;
1055         info->dev = &pdev->dev;
1056         info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
1057         INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
1058         platform_set_drvdata(pdev, info);
1059
1060         switch (arizona->type) {
1061         case WM5102:
1062                 switch (arizona->rev) {
1063                 case 0:
1064                         info->micd_reva = true;
1065                         break;
1066                 default:
1067                         info->micd_clamp = true;
1068                         info->hpdet_ip = 1;
1069                         break;
1070                 }
1071                 break;
1072         default:
1073                 break;
1074         }
1075
1076         info->edev.name = "Headset Jack";
1077         info->edev.supported_cable = arizona_cable;
1078
1079         ret = extcon_dev_register(&info->edev, arizona->dev);
1080         if (ret < 0) {
1081                 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
1082                         ret);
1083                 goto err;
1084         }
1085
1086         info->input = devm_input_allocate_device(&pdev->dev);
1087         if (!info->input) {
1088                 dev_err(arizona->dev, "Can't allocate input dev\n");
1089                 ret = -ENOMEM;
1090                 goto err_register;
1091         }
1092
1093         info->input->name = "Headset";
1094         info->input->phys = "arizona/extcon";
1095         info->input->dev.parent = &pdev->dev;
1096
1097         if (pdata->num_micd_configs) {
1098                 info->micd_modes = pdata->micd_configs;
1099                 info->micd_num_modes = pdata->num_micd_configs;
1100         } else {
1101                 info->micd_modes = micd_default_modes;
1102                 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1103         }
1104
1105         if (arizona->pdata.micd_pol_gpio > 0) {
1106                 if (info->micd_modes[0].gpio)
1107                         mode = GPIOF_OUT_INIT_HIGH;
1108                 else
1109                         mode = GPIOF_OUT_INIT_LOW;
1110
1111                 ret = devm_gpio_request_one(&pdev->dev,
1112                                             arizona->pdata.micd_pol_gpio,
1113                                             mode,
1114                                             "MICD polarity");
1115                 if (ret != 0) {
1116                         dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1117                                 arizona->pdata.micd_pol_gpio, ret);
1118                         goto err_register;
1119                 }
1120         }
1121
1122         if (arizona->pdata.hpdet_id_gpio > 0) {
1123                 ret = devm_gpio_request_one(&pdev->dev,
1124                                             arizona->pdata.hpdet_id_gpio,
1125                                             GPIOF_OUT_INIT_LOW,
1126                                             "HPDET");
1127                 if (ret != 0) {
1128                         dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1129                                 arizona->pdata.hpdet_id_gpio, ret);
1130                         goto err_register;
1131                 }
1132         }
1133
1134         if (arizona->pdata.micd_bias_start_time)
1135                 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1136                                    ARIZONA_MICD_BIAS_STARTTIME_MASK,
1137                                    arizona->pdata.micd_bias_start_time
1138                                    << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1139
1140         if (arizona->pdata.micd_rate)
1141                 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1142                                    ARIZONA_MICD_RATE_MASK,
1143                                    arizona->pdata.micd_rate
1144                                    << ARIZONA_MICD_RATE_SHIFT);
1145
1146         if (arizona->pdata.micd_dbtime)
1147                 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1148                                    ARIZONA_MICD_DBTIME_MASK,
1149                                    arizona->pdata.micd_dbtime
1150                                    << ARIZONA_MICD_DBTIME_SHIFT);
1151
1152         BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) != 0x40);
1153
1154         if (arizona->pdata.num_micd_ranges) {
1155                 info->micd_ranges = pdata->micd_ranges;
1156                 info->num_micd_ranges = pdata->num_micd_ranges;
1157         } else {
1158                 info->micd_ranges = micd_default_ranges;
1159                 info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
1160         }
1161
1162         if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_RANGE) {
1163                 dev_err(arizona->dev, "Too many MICD ranges: %d\n",
1164                         arizona->pdata.num_micd_ranges);
1165         }
1166
1167         if (info->num_micd_ranges > 1) {
1168                 for (i = 1; i < info->num_micd_ranges; i++) {
1169                         if (info->micd_ranges[i - 1].max >
1170                             info->micd_ranges[i].max) {
1171                                 dev_err(arizona->dev,
1172                                         "MICD ranges must be sorted\n");
1173                                 ret = -EINVAL;
1174                                 goto err_input;
1175                         }
1176                 }
1177         }
1178
1179         /* Disable all buttons by default */
1180         regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1181                            ARIZONA_MICD_LVL_SEL_MASK, 0x81);
1182
1183         /* Set up all the buttons the user specified */
1184         for (i = 0; i < info->num_micd_ranges; i++) {
1185                 for (j = 0; j < ARRAY_SIZE(arizona_micd_levels); j++)
1186                         if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
1187                                 break;
1188
1189                 if (j == ARRAY_SIZE(arizona_micd_levels)) {
1190                         dev_err(arizona->dev, "Unsupported MICD level %d\n",
1191                                 info->micd_ranges[i].max);
1192                         ret = -EINVAL;
1193                         goto err_input;
1194                 }
1195
1196                 dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
1197                         arizona_micd_levels[j], i);
1198
1199                 arizona_micd_set_level(arizona, i, j);
1200                 input_set_capability(info->input, EV_KEY,
1201                                      info->micd_ranges[i].key);
1202
1203                 /* Enable reporting of that range */
1204                 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1205                                    1 << i, 1 << i);
1206         }
1207
1208         /* Set all the remaining keys to a maximum */
1209         for (; i < ARIZONA_MAX_MICD_RANGE; i++)
1210                 arizona_micd_set_level(arizona, i, 0x3f);
1211
1212         /*
1213          * If we have a clamp use it, activating in conjunction with
1214          * GPIO5 if that is connected for jack detect operation.
1215          */
1216         if (info->micd_clamp) {
1217                 if (arizona->pdata.jd_gpio5) {
1218                         /* Put the GPIO into input mode with optional pull */
1219                         val = 0xc101;
1220                         if (arizona->pdata.jd_gpio5_nopull)
1221                                 val &= ~ARIZONA_GPN_PU;
1222
1223                         regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1224                                      val);
1225
1226                         regmap_update_bits(arizona->regmap,
1227                                            ARIZONA_MICD_CLAMP_CONTROL,
1228                                            ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
1229                 } else {
1230                         regmap_update_bits(arizona->regmap,
1231                                            ARIZONA_MICD_CLAMP_CONTROL,
1232                                            ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
1233                 }
1234
1235                 regmap_update_bits(arizona->regmap,
1236                                    ARIZONA_JACK_DETECT_DEBOUNCE,
1237                                    ARIZONA_MICD_CLAMP_DB,
1238                                    ARIZONA_MICD_CLAMP_DB);
1239         }
1240
1241         arizona_extcon_set_mode(info, 0);
1242
1243         pm_runtime_enable(&pdev->dev);
1244         pm_runtime_idle(&pdev->dev);
1245         pm_runtime_get_sync(&pdev->dev);
1246
1247         if (arizona->pdata.jd_gpio5) {
1248                 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1249                 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1250         } else {
1251                 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1252                 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1253         }
1254
1255         ret = arizona_request_irq(arizona, jack_irq_rise,
1256                                   "JACKDET rise", arizona_jackdet, info);
1257         if (ret != 0) {
1258                 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1259                         ret);
1260                 goto err_input;
1261         }
1262
1263         ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1264         if (ret != 0) {
1265                 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1266                         ret);
1267                 goto err_rise;
1268         }
1269
1270         ret = arizona_request_irq(arizona, jack_irq_fall,
1271                                   "JACKDET fall", arizona_jackdet, info);
1272         if (ret != 0) {
1273                 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1274                 goto err_rise_wake;
1275         }
1276
1277         ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1278         if (ret != 0) {
1279                 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1280                         ret);
1281                 goto err_fall;
1282         }
1283
1284         ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1285                                   "MICDET", arizona_micdet, info);
1286         if (ret != 0) {
1287                 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1288                 goto err_fall_wake;
1289         }
1290
1291         ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1292                                   "HPDET", arizona_hpdet_irq, info);
1293         if (ret != 0) {
1294                 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1295                 goto err_micdet;
1296         }
1297
1298         arizona_clk32k_enable(arizona);
1299         regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1300                            ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1301         regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1302                            ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1303
1304         ret = regulator_allow_bypass(info->micvdd, true);
1305         if (ret != 0)
1306                 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1307                          ret);
1308
1309         pm_runtime_put(&pdev->dev);
1310
1311         ret = input_register_device(info->input);
1312         if (ret) {
1313                 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1314                 goto err_hpdet;
1315         }
1316
1317         return 0;
1318
1319 err_hpdet:
1320         arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1321 err_micdet:
1322         arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1323 err_fall_wake:
1324         arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1325 err_fall:
1326         arizona_free_irq(arizona, jack_irq_fall, info);
1327 err_rise_wake:
1328         arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1329 err_rise:
1330         arizona_free_irq(arizona, jack_irq_rise, info);
1331 err_input:
1332 err_register:
1333         pm_runtime_disable(&pdev->dev);
1334         extcon_dev_unregister(&info->edev);
1335 err:
1336         return ret;
1337 }
1338
1339 static int arizona_extcon_remove(struct platform_device *pdev)
1340 {
1341         struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1342         struct arizona *arizona = info->arizona;
1343         int jack_irq_rise, jack_irq_fall;
1344
1345         pm_runtime_disable(&pdev->dev);
1346
1347         regmap_update_bits(arizona->regmap,
1348                            ARIZONA_MICD_CLAMP_CONTROL,
1349                            ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1350
1351         if (arizona->pdata.jd_gpio5) {
1352                 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1353                 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1354         } else {
1355                 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1356                 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1357         }
1358
1359         arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1360         arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1361         arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1362         arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1363         arizona_free_irq(arizona, jack_irq_rise, info);
1364         arizona_free_irq(arizona, jack_irq_fall, info);
1365         cancel_delayed_work_sync(&info->hpdet_work);
1366         regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1367                            ARIZONA_JD1_ENA, 0);
1368         arizona_clk32k_disable(arizona);
1369         extcon_dev_unregister(&info->edev);
1370
1371         return 0;
1372 }
1373
1374 static struct platform_driver arizona_extcon_driver = {
1375         .driver         = {
1376                 .name   = "arizona-extcon",
1377                 .owner  = THIS_MODULE,
1378         },
1379         .probe          = arizona_extcon_probe,
1380         .remove         = arizona_extcon_remove,
1381 };
1382
1383 module_platform_driver(arizona_extcon_driver);
1384
1385 MODULE_DESCRIPTION("Arizona Extcon driver");
1386 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1387 MODULE_LICENSE("GPL");
1388 MODULE_ALIAS("platform:extcon-arizona");