]> Pileus Git - ~andy/linux/blob - drivers/extcon/extcon-arizona.c
extcon: arizona: Remove extra jack flip increment
[~andy/linux] / 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
36 #define ARIZONA_NUM_BUTTONS 6
37
38 #define ARIZONA_ACCDET_MODE_MIC 0
39 #define ARIZONA_ACCDET_MODE_HPL 1
40 #define ARIZONA_ACCDET_MODE_HPR 2
41
42 struct arizona_extcon_info {
43         struct device *dev;
44         struct arizona *arizona;
45         struct mutex lock;
46         struct regulator *micvdd;
47         struct input_dev *input;
48
49         int micd_mode;
50         const struct arizona_micd_config *micd_modes;
51         int micd_num_modes;
52
53         bool micd_reva;
54         bool micd_clamp;
55
56         bool hpdet_active;
57
58         int num_hpdet_res;
59         unsigned int hpdet_res[3];
60
61         bool mic;
62         bool detecting;
63         int jack_flips;
64
65         int hpdet_ip;
66
67         struct extcon_dev edev;
68 };
69
70 static const struct arizona_micd_config micd_default_modes[] = {
71         { 0,                  2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 },
72         { ARIZONA_ACCDET_SRC, 1 << ARIZONA_MICD_BIAS_SRC_SHIFT, 0 },
73 };
74
75 static struct {
76         u16 status;
77         int report;
78 } arizona_lvl_to_key[ARIZONA_NUM_BUTTONS] = {
79         {  0x1, BTN_0 },
80         {  0x2, BTN_1 },
81         {  0x4, BTN_2 },
82         {  0x8, BTN_3 },
83         { 0x10, BTN_4 },
84         { 0x20, BTN_5 },
85 };
86
87 #define ARIZONA_CABLE_MECHANICAL 0
88 #define ARIZONA_CABLE_MICROPHONE 1
89 #define ARIZONA_CABLE_HEADPHONE  2
90 #define ARIZONA_CABLE_LINEOUT    3
91
92 static const char *arizona_cable[] = {
93         "Mechanical",
94         "Microphone",
95         "Headphone",
96         "Line-out",
97         NULL,
98 };
99
100 static void arizona_extcon_set_mode(struct arizona_extcon_info *info, int mode)
101 {
102         struct arizona *arizona = info->arizona;
103
104         if (arizona->pdata.micd_pol_gpio > 0)
105                 gpio_set_value_cansleep(arizona->pdata.micd_pol_gpio,
106                                         info->micd_modes[mode].gpio);
107         regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
108                            ARIZONA_MICD_BIAS_SRC_MASK,
109                            info->micd_modes[mode].bias);
110         regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
111                            ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
112
113         info->micd_mode = mode;
114
115         dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
116 }
117
118 static const char *arizona_extcon_get_micbias(struct arizona_extcon_info *info)
119 {
120         switch (info->micd_modes[0].bias >> ARIZONA_MICD_BIAS_SRC_SHIFT) {
121         case 1:
122                 return "MICBIAS1";
123         case 2:
124                 return "MICBIAS2";
125         case 3:
126                 return "MICBIAS3";
127         default:
128                 return "MICVDD";
129         }
130 }
131
132 static void arizona_extcon_pulse_micbias(struct arizona_extcon_info *info)
133 {
134         struct arizona *arizona = info->arizona;
135         const char *widget = arizona_extcon_get_micbias(info);
136         struct snd_soc_dapm_context *dapm = arizona->dapm;
137         int ret;
138
139         mutex_lock(&dapm->card->dapm_mutex);
140
141         ret = snd_soc_dapm_force_enable_pin(dapm, widget);
142         if (ret != 0)
143                 dev_warn(arizona->dev, "Failed to enable %s: %d\n",
144                          widget, ret);
145
146         mutex_unlock(&dapm->card->dapm_mutex);
147
148         snd_soc_dapm_sync(dapm);
149
150         if (!arizona->pdata.micd_force_micbias) {
151                 mutex_lock(&dapm->card->dapm_mutex);
152
153                 ret = snd_soc_dapm_disable_pin(arizona->dapm, widget);
154                 if (ret != 0)
155                         dev_warn(arizona->dev, "Failed to disable %s: %d\n",
156                                  widget, ret);
157
158                 mutex_unlock(&dapm->card->dapm_mutex);
159
160                 snd_soc_dapm_sync(dapm);
161         }
162 }
163
164 static void arizona_start_mic(struct arizona_extcon_info *info)
165 {
166         struct arizona *arizona = info->arizona;
167         bool change;
168         int ret;
169
170         /* Microphone detection can't use idle mode */
171         pm_runtime_get(info->dev);
172
173         if (info->detecting) {
174                 ret = regulator_allow_bypass(info->micvdd, false);
175                 if (ret != 0) {
176                         dev_err(arizona->dev,
177                                 "Failed to regulate MICVDD: %d\n",
178                                 ret);
179                 }
180         }
181
182         ret = regulator_enable(info->micvdd);
183         if (ret != 0) {
184                 dev_err(arizona->dev, "Failed to enable MICVDD: %d\n",
185                         ret);
186         }
187
188         if (info->micd_reva) {
189                 regmap_write(arizona->regmap, 0x80, 0x3);
190                 regmap_write(arizona->regmap, 0x294, 0);
191                 regmap_write(arizona->regmap, 0x80, 0x0);
192         }
193
194         regmap_update_bits(arizona->regmap,
195                            ARIZONA_ACCESSORY_DETECT_MODE_1,
196                            ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
197
198         arizona_extcon_pulse_micbias(info);
199
200         regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
201                                  ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
202                                  &change);
203         if (!change) {
204                 regulator_disable(info->micvdd);
205                 pm_runtime_put_autosuspend(info->dev);
206         }
207 }
208
209 static void arizona_stop_mic(struct arizona_extcon_info *info)
210 {
211         struct arizona *arizona = info->arizona;
212         const char *widget = arizona_extcon_get_micbias(info);
213         struct snd_soc_dapm_context *dapm = arizona->dapm;
214         bool change;
215         int ret;
216
217         regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
218                                  ARIZONA_MICD_ENA, 0,
219                                  &change);
220
221         mutex_lock(&dapm->card->dapm_mutex);
222
223         ret = snd_soc_dapm_disable_pin(dapm, widget);
224         if (ret != 0)
225                 dev_warn(arizona->dev,
226                          "Failed to disable %s: %d\n",
227                          widget, ret);
228
229         mutex_unlock(&dapm->card->dapm_mutex);
230
231         snd_soc_dapm_sync(dapm);
232
233         if (info->micd_reva) {
234                 regmap_write(arizona->regmap, 0x80, 0x3);
235                 regmap_write(arizona->regmap, 0x294, 2);
236                 regmap_write(arizona->regmap, 0x80, 0x0);
237         }
238
239         ret = regulator_allow_bypass(info->micvdd, true);
240         if (ret != 0) {
241                 dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
242                         ret);
243         }
244
245         if (change) {
246                 regulator_disable(info->micvdd);
247                 pm_runtime_mark_last_busy(info->dev);
248                 pm_runtime_put_autosuspend(info->dev);
249         }
250 }
251
252 static struct {
253         unsigned int factor_a;
254         unsigned int factor_b;
255 } arizona_hpdet_b_ranges[] = {
256         {  5528,   362464 },
257         { 11084,  6186851 },
258         { 11065, 65460395 },
259 };
260
261 static struct {
262         int min;
263         int max;
264 } arizona_hpdet_c_ranges[] = {
265         { 0,       30 },
266         { 8,      100 },
267         { 100,   1000 },
268         { 1000, 10000 },
269 };
270
271 static int arizona_hpdet_read(struct arizona_extcon_info *info)
272 {
273         struct arizona *arizona = info->arizona;
274         unsigned int val, range;
275         int ret;
276
277         ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
278         if (ret != 0) {
279                 dev_err(arizona->dev, "Failed to read HPDET status: %d\n",
280                         ret);
281                 return ret;
282         }
283
284         switch (info->hpdet_ip) {
285         case 0:
286                 if (!(val & ARIZONA_HP_DONE)) {
287                         dev_err(arizona->dev, "HPDET did not complete: %x\n",
288                                 val);
289                         return -EAGAIN;
290                 }
291
292                 val &= ARIZONA_HP_LVL_MASK;
293                 break;
294
295         case 1:
296                 if (!(val & ARIZONA_HP_DONE_B)) {
297                         dev_err(arizona->dev, "HPDET did not complete: %x\n",
298                                 val);
299                         return -EAGAIN;
300                 }
301
302                 ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
303                 if (ret != 0) {
304                         dev_err(arizona->dev, "Failed to read HP value: %d\n",
305                                 ret);
306                         return -EAGAIN;
307                 }
308
309                 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
310                             &range);
311                 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
312                            >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
313
314                 if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
315                     (val < 100 || val > 0x3fb)) {
316                         range++;
317                         dev_dbg(arizona->dev, "Moving to HPDET range %d\n",
318                                 range);
319                         regmap_update_bits(arizona->regmap,
320                                            ARIZONA_HEADPHONE_DETECT_1,
321                                            ARIZONA_HP_IMPEDANCE_RANGE_MASK,
322                                            range <<
323                                            ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
324                         return -EAGAIN;
325                 }
326
327                 /* If we go out of range report top of range */
328                 if (val < 100 || val > 0x3fb) {
329                         dev_dbg(arizona->dev, "Measurement out of range\n");
330                         return 10000;
331                 }
332
333                 dev_dbg(arizona->dev, "HPDET read %d in range %d\n",
334                         val, range);
335
336                 val = arizona_hpdet_b_ranges[range].factor_b
337                         / ((val * 100) -
338                            arizona_hpdet_b_ranges[range].factor_a);
339                 break;
340
341         default:
342                 dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n",
343                          info->hpdet_ip);
344         case 2:
345                 if (!(val & ARIZONA_HP_DONE_B)) {
346                         dev_err(arizona->dev, "HPDET did not complete: %x\n",
347                                 val);
348                         return -EAGAIN;
349                 }
350
351                 val &= ARIZONA_HP_LVL_B_MASK;
352
353                 regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
354                             &range);
355                 range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
356                            >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
357
358                 /* Skip up or down a range? */
359                 if (range && (val < arizona_hpdet_c_ranges[range].min)) {
360                         range--;
361                         dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
362                                 arizona_hpdet_c_ranges[range].min,
363                                 arizona_hpdet_c_ranges[range].max);
364                         regmap_update_bits(arizona->regmap,
365                                            ARIZONA_HEADPHONE_DETECT_1,
366                                            ARIZONA_HP_IMPEDANCE_RANGE_MASK,
367                                            range <<
368                                            ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
369                         return -EAGAIN;
370                 }
371
372                 if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
373                     (val >= arizona_hpdet_c_ranges[range].max)) {
374                         range++;
375                         dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
376                                 arizona_hpdet_c_ranges[range].min,
377                                 arizona_hpdet_c_ranges[range].max);
378                         regmap_update_bits(arizona->regmap,
379                                            ARIZONA_HEADPHONE_DETECT_1,
380                                            ARIZONA_HP_IMPEDANCE_RANGE_MASK,
381                                            range <<
382                                            ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
383                         return -EAGAIN;
384                 }
385         }
386
387         dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
388         return val;
389 }
390
391 static int arizona_hpdet_do_id(struct arizona_extcon_info *info, int *reading)
392 {
393         struct arizona *arizona = info->arizona;
394         int id_gpio = arizona->pdata.hpdet_id_gpio;
395         int ret;
396
397         /*
398          * If we're using HPDET for accessory identification we need
399          * to take multiple measurements, step through them in sequence.
400          */
401         if (arizona->pdata.hpdet_acc_id) {
402                 info->hpdet_res[info->num_hpdet_res++] = *reading;
403
404                 /*
405                  * If the impedence is too high don't measure the
406                  * second ground.
407                  */
408                 if (info->num_hpdet_res == 1 && *reading >= 45) {
409                         dev_dbg(arizona->dev, "Skipping ground flip\n");
410                         info->hpdet_res[info->num_hpdet_res++] = *reading;
411                 }
412
413                 if (info->num_hpdet_res == 1) {
414                         dev_dbg(arizona->dev, "Flipping ground\n");
415
416                         regmap_update_bits(arizona->regmap,
417                                            ARIZONA_ACCESSORY_DETECT_MODE_1,
418                                            ARIZONA_ACCDET_SRC,
419                                            ~info->micd_modes[0].src);
420
421                         regmap_update_bits(arizona->regmap,
422                                            ARIZONA_HEADPHONE_DETECT_1,
423                                            ARIZONA_HP_POLL, ARIZONA_HP_POLL);
424                         return -EAGAIN;
425                 }
426
427                 /* Only check the mic directly if we didn't already ID it */
428                 if (id_gpio && info->num_hpdet_res == 2 &&
429                     !((info->hpdet_res[0] > info->hpdet_res[1] * 2))) {
430                         dev_dbg(arizona->dev, "Measuring mic\n");
431
432                         regmap_update_bits(arizona->regmap,
433                                            ARIZONA_ACCESSORY_DETECT_MODE_1,
434                                            ARIZONA_ACCDET_MODE_MASK |
435                                            ARIZONA_ACCDET_SRC,
436                                            ARIZONA_ACCDET_MODE_HPR |
437                                            info->micd_modes[0].src);
438
439                         gpio_set_value_cansleep(id_gpio, 1);
440
441                         regmap_update_bits(arizona->regmap,
442                                            ARIZONA_HEADPHONE_DETECT_1,
443                                            ARIZONA_HP_POLL, ARIZONA_HP_POLL);
444                         return -EAGAIN;
445                 }
446
447                 /* OK, got both.  Now, compare... */
448                 dev_dbg(arizona->dev, "HPDET measured %d %d %d\n",
449                         info->hpdet_res[0], info->hpdet_res[1],
450                         info->hpdet_res[2]);
451
452                 /*
453                  * Either the two grounds measure differently or we
454                  * measure the mic as high impedance.
455                  */
456                 if ((info->hpdet_res[0] > info->hpdet_res[1] * 2) ||
457                     (id_gpio && info->hpdet_res[2] > 10)) {
458                         dev_dbg(arizona->dev, "Detected mic\n");
459                         info->mic = true;
460                         ret = extcon_set_cable_state_(&info->edev,
461                                                       ARIZONA_CABLE_MICROPHONE,
462                                                       true);
463                         if (ret != 0) {
464                                 dev_err(arizona->dev,
465                                         "Failed to report mic: %d\n", ret);
466                         }
467
468                         /* Take the headphone impedance for the main report */
469                         *reading = info->hpdet_res[1];
470                 } else {
471                         dev_dbg(arizona->dev, "Detected headphone\n");
472                 }
473
474                 /* Make sure everything is reset back to the real polarity */
475                 regmap_update_bits(arizona->regmap,
476                                    ARIZONA_ACCESSORY_DETECT_MODE_1,
477                                    ARIZONA_ACCDET_SRC,
478                                    info->micd_modes[0].src);
479         }
480
481         return 0;
482 }
483
484 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
485 {
486         struct arizona_extcon_info *info = data;
487         struct arizona *arizona = info->arizona;
488         int id_gpio = arizona->pdata.hpdet_id_gpio;
489         int report = ARIZONA_CABLE_HEADPHONE;
490         int ret, reading;
491
492         mutex_lock(&info->lock);
493
494         /* If we got a spurious IRQ for some reason then ignore it */
495         if (!info->hpdet_active) {
496                 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
497                 mutex_unlock(&info->lock);
498                 return IRQ_NONE;
499         }
500
501         /* If the cable was removed while measuring ignore the result */
502         ret = extcon_get_cable_state_(&info->edev, ARIZONA_CABLE_MECHANICAL);
503         if (ret < 0) {
504                 dev_err(arizona->dev, "Failed to check cable state: %d\n",
505                         ret);
506                 goto out;
507         } else if (!ret) {
508                 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
509                 goto done;
510         }
511
512         ret = arizona_hpdet_read(info);
513         if (ret == -EAGAIN) {
514                 goto out;
515         } else if (ret < 0) {
516                 goto done;
517         }
518         reading = ret;
519
520         /* Reset back to starting range */
521         regmap_update_bits(arizona->regmap,
522                            ARIZONA_HEADPHONE_DETECT_1,
523                            ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
524                            0);
525
526         ret = arizona_hpdet_do_id(info, &reading);
527         if (ret == -EAGAIN) {
528                 goto out;
529         } else if (ret < 0) {
530                 goto done;
531         }
532
533         /* Report high impedence cables as line outputs */
534         if (reading >= 5000)
535                 report = ARIZONA_CABLE_LINEOUT;
536         else
537                 report = ARIZONA_CABLE_HEADPHONE;
538
539         ret = extcon_set_cable_state_(&info->edev, report, true);
540         if (ret != 0)
541                 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
542                         ret);
543
544         ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0);
545         if (ret != 0)
546                 dev_warn(arizona->dev, "Failed to undo magic: %d\n", ret);
547
548         ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0);
549         if (ret != 0)
550                 dev_warn(arizona->dev, "Failed to undo magic: %d\n", ret);
551
552 done:
553         if (id_gpio)
554                 gpio_set_value_cansleep(id_gpio, 0);
555
556         /* Revert back to MICDET mode */
557         regmap_update_bits(arizona->regmap,
558                            ARIZONA_ACCESSORY_DETECT_MODE_1,
559                            ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
560
561         /* If we have a mic then reenable MICDET */
562         if (info->mic)
563                 arizona_start_mic(info);
564
565         if (info->hpdet_active) {
566                 pm_runtime_put_autosuspend(info->dev);
567                 info->hpdet_active = false;
568         }
569
570 out:
571         mutex_unlock(&info->lock);
572
573         return IRQ_HANDLED;
574 }
575
576 static void arizona_identify_headphone(struct arizona_extcon_info *info)
577 {
578         struct arizona *arizona = info->arizona;
579         int ret;
580
581         dev_dbg(arizona->dev, "Starting HPDET\n");
582
583         /* Make sure we keep the device enabled during the measurement */
584         pm_runtime_get(info->dev);
585
586         info->hpdet_active = true;
587
588         if (info->mic)
589                 arizona_stop_mic(info);
590
591         ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0x4000);
592         if (ret != 0)
593                 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
594
595         ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0x4000);
596         if (ret != 0)
597                 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
598
599         ret = regmap_update_bits(arizona->regmap,
600                                  ARIZONA_ACCESSORY_DETECT_MODE_1,
601                                  ARIZONA_ACCDET_MODE_MASK,
602                                  ARIZONA_ACCDET_MODE_HPL);
603         if (ret != 0) {
604                 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
605                 goto err;
606         }
607
608         ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
609                                  ARIZONA_HP_POLL, ARIZONA_HP_POLL);
610         if (ret != 0) {
611                 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
612                         ret);
613                 goto err;
614         }
615
616         return;
617
618 err:
619         regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
620                            ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
621
622         /* Just report headphone */
623         ret = extcon_update_state(&info->edev,
624                                   1 << ARIZONA_CABLE_HEADPHONE,
625                                   1 << ARIZONA_CABLE_HEADPHONE);
626         if (ret != 0)
627                 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
628
629         if (info->mic)
630                 arizona_start_mic(info);
631
632         info->hpdet_active = false;
633 }
634
635 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
636 {
637         struct arizona *arizona = info->arizona;
638         int ret;
639
640         dev_dbg(arizona->dev, "Starting identification via HPDET\n");
641
642         /* Make sure we keep the device enabled during the measurement */
643         pm_runtime_get(info->dev);
644
645         info->hpdet_active = true;
646
647         arizona_extcon_pulse_micbias(info);
648
649         ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0x4000);
650         if (ret != 0)
651                 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
652
653         ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0x4000);
654         if (ret != 0)
655                 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
656
657         ret = regmap_update_bits(arizona->regmap,
658                                  ARIZONA_ACCESSORY_DETECT_MODE_1,
659                                  ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
660                                  info->micd_modes[0].src |
661                                  ARIZONA_ACCDET_MODE_HPL);
662         if (ret != 0) {
663                 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
664                 goto err;
665         }
666
667         ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
668                                  ARIZONA_HP_POLL, ARIZONA_HP_POLL);
669         if (ret != 0) {
670                 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
671                         ret);
672                 goto err;
673         }
674
675         return;
676
677 err:
678         regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
679                            ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
680
681         /* Just report headphone */
682         ret = extcon_update_state(&info->edev,
683                                   1 << ARIZONA_CABLE_HEADPHONE,
684                                   1 << ARIZONA_CABLE_HEADPHONE);
685         if (ret != 0)
686                 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
687
688         info->hpdet_active = false;
689 }
690
691 static irqreturn_t arizona_micdet(int irq, void *data)
692 {
693         struct arizona_extcon_info *info = data;
694         struct arizona *arizona = info->arizona;
695         unsigned int val, lvl;
696         int ret, i;
697
698         mutex_lock(&info->lock);
699
700         ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
701         if (ret != 0) {
702                 dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
703                 mutex_unlock(&info->lock);
704                 return IRQ_NONE;
705         }
706
707         dev_dbg(arizona->dev, "MICDET: %x\n", val);
708
709         if (!(val & ARIZONA_MICD_VALID)) {
710                 dev_warn(arizona->dev, "Microphone detection state invalid\n");
711                 mutex_unlock(&info->lock);
712                 return IRQ_NONE;
713         }
714
715         /* Due to jack detect this should never happen */
716         if (!(val & ARIZONA_MICD_STS)) {
717                 dev_warn(arizona->dev, "Detected open circuit\n");
718                 info->detecting = false;
719                 goto handled;
720         }
721
722         /* If we got a high impedence we should have a headset, report it. */
723         if (info->detecting && (val & 0x400)) {
724                 arizona_identify_headphone(info);
725
726                 ret = extcon_update_state(&info->edev,
727                                           1 << ARIZONA_CABLE_MICROPHONE,
728                                           1 << ARIZONA_CABLE_MICROPHONE);
729
730                 if (ret != 0)
731                         dev_err(arizona->dev, "Headset report failed: %d\n",
732                                 ret);
733
734                 /* Don't need to regulate for button detection */
735                 ret = regulator_allow_bypass(info->micvdd, false);
736                 if (ret != 0) {
737                         dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
738                                 ret);
739                 }
740
741                 info->mic = true;
742                 info->detecting = false;
743                 goto handled;
744         }
745
746         /* If we detected a lower impedence during initial startup
747          * then we probably have the wrong polarity, flip it.  Don't
748          * do this for the lowest impedences to speed up detection of
749          * plain headphones.  If both polarities report a low
750          * impedence then give up and report headphones.
751          */
752         if (info->detecting && (val & 0x3f8)) {
753                 if (info->jack_flips >= info->micd_num_modes) {
754                         dev_dbg(arizona->dev, "Detected HP/line\n");
755                         arizona_identify_headphone(info);
756
757                         info->detecting = false;
758
759                         arizona_stop_mic(info);
760                 } else {
761                         info->micd_mode++;
762                         if (info->micd_mode == info->micd_num_modes)
763                                 info->micd_mode = 0;
764                         arizona_extcon_set_mode(info, info->micd_mode);
765
766                         info->jack_flips++;
767                 }
768
769                 goto handled;
770         }
771
772         /*
773          * If we're still detecting and we detect a short then we've
774          * got a headphone.  Otherwise it's a button press.
775          */
776         if (val & 0x3fc) {
777                 if (info->mic) {
778                         dev_dbg(arizona->dev, "Mic button detected\n");
779
780                         lvl = val & ARIZONA_MICD_LVL_MASK;
781                         lvl >>= ARIZONA_MICD_LVL_SHIFT;
782
783                         for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
784                                 if (lvl & arizona_lvl_to_key[i].status)
785                                         input_report_key(info->input,
786                                                          arizona_lvl_to_key[i].report,
787                                                          1);
788                         input_sync(info->input);
789
790                 } else if (info->detecting) {
791                         dev_dbg(arizona->dev, "Headphone detected\n");
792                         info->detecting = false;
793                         arizona_stop_mic(info);
794
795                         arizona_identify_headphone(info);
796                 } else {
797                         dev_warn(arizona->dev, "Button with no mic: %x\n",
798                                  val);
799                 }
800         } else {
801                 dev_dbg(arizona->dev, "Mic button released\n");
802                 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
803                         input_report_key(info->input,
804                                          arizona_lvl_to_key[i].report, 0);
805                 input_sync(info->input);
806                 arizona_extcon_pulse_micbias(info);
807         }
808
809 handled:
810         pm_runtime_mark_last_busy(info->dev);
811         mutex_unlock(&info->lock);
812
813         return IRQ_HANDLED;
814 }
815
816 static irqreturn_t arizona_jackdet(int irq, void *data)
817 {
818         struct arizona_extcon_info *info = data;
819         struct arizona *arizona = info->arizona;
820         unsigned int val, present, mask;
821         int ret, i;
822
823         pm_runtime_get_sync(info->dev);
824
825         mutex_lock(&info->lock);
826
827         if (arizona->pdata.jd_gpio5) {
828                 mask = ARIZONA_MICD_CLAMP_STS;
829                 present = 0;
830         } else {
831                 mask = ARIZONA_JD1_STS;
832                 present = ARIZONA_JD1_STS;
833         }
834
835         ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
836         if (ret != 0) {
837                 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
838                         ret);
839                 mutex_unlock(&info->lock);
840                 pm_runtime_put_autosuspend(info->dev);
841                 return IRQ_NONE;
842         }
843
844         if ((val & mask) == present) {
845                 dev_dbg(arizona->dev, "Detected jack\n");
846                 ret = extcon_set_cable_state_(&info->edev,
847                                               ARIZONA_CABLE_MECHANICAL, true);
848
849                 if (ret != 0)
850                         dev_err(arizona->dev, "Mechanical report failed: %d\n",
851                                 ret);
852
853                 if (!arizona->pdata.hpdet_acc_id) {
854                         info->detecting = true;
855                         info->mic = false;
856                         info->jack_flips = 0;
857
858                         arizona_start_mic(info);
859                 } else {
860                         arizona_start_hpdet_acc_id(info);
861                 }
862
863                 regmap_update_bits(arizona->regmap,
864                                    ARIZONA_JACK_DETECT_DEBOUNCE,
865                                    ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
866         } else {
867                 dev_dbg(arizona->dev, "Detected jack removal\n");
868
869                 arizona_stop_mic(info);
870
871                 info->num_hpdet_res = 0;
872                 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
873                         info->hpdet_res[i] = 0;
874                 info->mic = false;
875
876                 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
877                         input_report_key(info->input,
878                                          arizona_lvl_to_key[i].report, 0);
879                 input_sync(info->input);
880
881                 ret = extcon_update_state(&info->edev, 0xffffffff, 0);
882                 if (ret != 0)
883                         dev_err(arizona->dev, "Removal report failed: %d\n",
884                                 ret);
885
886                 regmap_update_bits(arizona->regmap,
887                                    ARIZONA_JACK_DETECT_DEBOUNCE,
888                                    ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
889                                    ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
890         }
891
892         mutex_unlock(&info->lock);
893
894         pm_runtime_mark_last_busy(info->dev);
895         pm_runtime_put_autosuspend(info->dev);
896
897         return IRQ_HANDLED;
898 }
899
900 static int arizona_extcon_probe(struct platform_device *pdev)
901 {
902         struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
903         struct arizona_pdata *pdata;
904         struct arizona_extcon_info *info;
905         int jack_irq_fall, jack_irq_rise;
906         int ret, mode, i;
907
908         if (!arizona->dapm || !arizona->dapm->card)
909                 return -EPROBE_DEFER;
910
911         pdata = dev_get_platdata(arizona->dev);
912
913         info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
914         if (!info) {
915                 dev_err(&pdev->dev, "Failed to allocate memory\n");
916                 ret = -ENOMEM;
917                 goto err;
918         }
919
920         info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
921         if (IS_ERR(info->micvdd)) {
922                 ret = PTR_ERR(info->micvdd);
923                 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
924                 goto err;
925         }
926
927         mutex_init(&info->lock);
928         info->arizona = arizona;
929         info->dev = &pdev->dev;
930         platform_set_drvdata(pdev, info);
931
932         switch (arizona->type) {
933         case WM5102:
934                 switch (arizona->rev) {
935                 case 0:
936                         info->micd_reva = true;
937                         break;
938                 default:
939                         info->micd_clamp = true;
940                         info->hpdet_ip = 1;
941                         break;
942                 }
943                 break;
944         default:
945                 break;
946         }
947
948         info->edev.name = "Headset Jack";
949         info->edev.supported_cable = arizona_cable;
950
951         ret = extcon_dev_register(&info->edev, arizona->dev);
952         if (ret < 0) {
953                 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
954                         ret);
955                 goto err;
956         }
957
958         if (pdata->num_micd_configs) {
959                 info->micd_modes = pdata->micd_configs;
960                 info->micd_num_modes = pdata->num_micd_configs;
961         } else {
962                 info->micd_modes = micd_default_modes;
963                 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
964         }
965
966         if (arizona->pdata.micd_pol_gpio > 0) {
967                 if (info->micd_modes[0].gpio)
968                         mode = GPIOF_OUT_INIT_HIGH;
969                 else
970                         mode = GPIOF_OUT_INIT_LOW;
971
972                 ret = devm_gpio_request_one(&pdev->dev,
973                                             arizona->pdata.micd_pol_gpio,
974                                             mode,
975                                             "MICD polarity");
976                 if (ret != 0) {
977                         dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
978                                 arizona->pdata.micd_pol_gpio, ret);
979                         goto err_register;
980                 }
981         }
982
983         if (arizona->pdata.hpdet_id_gpio > 0) {
984                 ret = devm_gpio_request_one(&pdev->dev,
985                                             arizona->pdata.hpdet_id_gpio,
986                                             GPIOF_OUT_INIT_LOW,
987                                             "HPDET");
988                 if (ret != 0) {
989                         dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
990                                 arizona->pdata.hpdet_id_gpio, ret);
991                         goto err_register;
992                 }
993         }
994
995         if (arizona->pdata.micd_bias_start_time)
996                 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
997                                    ARIZONA_MICD_BIAS_STARTTIME_MASK,
998                                    arizona->pdata.micd_bias_start_time
999                                    << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1000
1001         if (arizona->pdata.micd_rate)
1002                 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1003                                    ARIZONA_MICD_RATE_MASK,
1004                                    arizona->pdata.micd_rate
1005                                    << ARIZONA_MICD_RATE_SHIFT);
1006
1007         if (arizona->pdata.micd_dbtime)
1008                 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1009                                    ARIZONA_MICD_DBTIME_MASK,
1010                                    arizona->pdata.micd_dbtime
1011                                    << ARIZONA_MICD_DBTIME_SHIFT);
1012
1013         /*
1014          * If we have a clamp use it, activating in conjunction with
1015          * GPIO5 if that is connected for jack detect operation.
1016          */
1017         if (info->micd_clamp) {
1018                 if (arizona->pdata.jd_gpio5) {
1019                         /* Put the GPIO into input mode */
1020                         regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1021                                      0xc101);
1022
1023                         regmap_update_bits(arizona->regmap,
1024                                            ARIZONA_MICD_CLAMP_CONTROL,
1025                                            ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
1026                 } else {
1027                         regmap_update_bits(arizona->regmap,
1028                                            ARIZONA_MICD_CLAMP_CONTROL,
1029                                            ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
1030                 }
1031
1032                 regmap_update_bits(arizona->regmap,
1033                                    ARIZONA_JACK_DETECT_DEBOUNCE,
1034                                    ARIZONA_MICD_CLAMP_DB,
1035                                    ARIZONA_MICD_CLAMP_DB);
1036         }
1037
1038         arizona_extcon_set_mode(info, 0);
1039
1040         info->input = devm_input_allocate_device(&pdev->dev);
1041         if (!info->input) {
1042                 dev_err(arizona->dev, "Can't allocate input dev\n");
1043                 ret = -ENOMEM;
1044                 goto err_register;
1045         }
1046
1047         for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
1048                 input_set_capability(info->input, EV_KEY,
1049                                      arizona_lvl_to_key[i].report);
1050         info->input->name = "Headset";
1051         info->input->phys = "arizona/extcon";
1052         info->input->dev.parent = &pdev->dev;
1053
1054         pm_runtime_enable(&pdev->dev);
1055         pm_runtime_idle(&pdev->dev);
1056         pm_runtime_get_sync(&pdev->dev);
1057
1058         if (arizona->pdata.jd_gpio5) {
1059                 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1060                 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1061         } else {
1062                 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1063                 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1064         }
1065
1066         ret = arizona_request_irq(arizona, jack_irq_rise,
1067                                   "JACKDET rise", arizona_jackdet, info);
1068         if (ret != 0) {
1069                 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1070                         ret);
1071                 goto err_input;
1072         }
1073
1074         ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1075         if (ret != 0) {
1076                 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1077                         ret);
1078                 goto err_rise;
1079         }
1080
1081         ret = arizona_request_irq(arizona, jack_irq_fall,
1082                                   "JACKDET fall", arizona_jackdet, info);
1083         if (ret != 0) {
1084                 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1085                 goto err_rise_wake;
1086         }
1087
1088         ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1089         if (ret != 0) {
1090                 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1091                         ret);
1092                 goto err_fall;
1093         }
1094
1095         ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1096                                   "MICDET", arizona_micdet, info);
1097         if (ret != 0) {
1098                 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1099                 goto err_fall_wake;
1100         }
1101
1102         ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1103                                   "HPDET", arizona_hpdet_irq, info);
1104         if (ret != 0) {
1105                 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1106                 goto err_micdet;
1107         }
1108
1109         arizona_clk32k_enable(arizona);
1110         regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1111                            ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1112         regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1113                            ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1114
1115         ret = regulator_allow_bypass(info->micvdd, true);
1116         if (ret != 0)
1117                 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1118                          ret);
1119
1120         pm_runtime_put(&pdev->dev);
1121
1122         ret = input_register_device(info->input);
1123         if (ret) {
1124                 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1125                 goto err_hpdet;
1126         }
1127
1128         return 0;
1129
1130 err_hpdet:
1131         arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1132 err_micdet:
1133         arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1134 err_fall_wake:
1135         arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1136 err_fall:
1137         arizona_free_irq(arizona, jack_irq_fall, info);
1138 err_rise_wake:
1139         arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1140 err_rise:
1141         arizona_free_irq(arizona, jack_irq_rise, info);
1142 err_input:
1143 err_register:
1144         pm_runtime_disable(&pdev->dev);
1145         extcon_dev_unregister(&info->edev);
1146 err:
1147         return ret;
1148 }
1149
1150 static int arizona_extcon_remove(struct platform_device *pdev)
1151 {
1152         struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1153         struct arizona *arizona = info->arizona;
1154         int jack_irq_rise, jack_irq_fall;
1155
1156         pm_runtime_disable(&pdev->dev);
1157
1158         regmap_update_bits(arizona->regmap,
1159                            ARIZONA_MICD_CLAMP_CONTROL,
1160                            ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1161
1162         if (arizona->pdata.jd_gpio5) {
1163                 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1164                 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1165         } else {
1166                 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1167                 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1168         }
1169
1170         arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1171         arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1172         arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1173         arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1174         arizona_free_irq(arizona, jack_irq_rise, info);
1175         arizona_free_irq(arizona, jack_irq_fall, info);
1176         regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1177                            ARIZONA_JD1_ENA, 0);
1178         arizona_clk32k_disable(arizona);
1179         extcon_dev_unregister(&info->edev);
1180
1181         return 0;
1182 }
1183
1184 static struct platform_driver arizona_extcon_driver = {
1185         .driver         = {
1186                 .name   = "arizona-extcon",
1187                 .owner  = THIS_MODULE,
1188         },
1189         .probe          = arizona_extcon_probe,
1190         .remove         = arizona_extcon_remove,
1191 };
1192
1193 module_platform_driver(arizona_extcon_driver);
1194
1195 MODULE_DESCRIPTION("Arizona Extcon driver");
1196 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1197 MODULE_LICENSE("GPL");
1198 MODULE_ALIAS("platform:extcon-arizona");