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