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