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