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