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