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