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