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