extcon: arizona: Don't HPDET magic when headphones are enabled
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / extcon / extcon-arizona.c
1 /*
2  * extcon-arizona.c - Extcon driver Wolfson Arizona devices
3  *
4  *  Copyright (C) 2012 Wolfson Microelectronics plc
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/i2c.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/err.h>
23 #include <linux/gpio.h>
24 #include <linux/input.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/extcon.h>
29
30 #include <sound/soc.h>
31
32 #include <linux/mfd/arizona/core.h>
33 #include <linux/mfd/arizona/pdata.h>
34 #include <linux/mfd/arizona/registers.h>
35
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                  * Either the two grounds measure differently or we
456                  * measure the mic as high impedance.
457                  */
458                 if ((info->hpdet_res[0] > info->hpdet_res[1] * 2) ||
459                     (id_gpio && info->hpdet_res[2] > 10)) {
460                         dev_dbg(arizona->dev, "Detected mic\n");
461                         info->mic = true;
462                         ret = extcon_set_cable_state_(&info->edev,
463                                                       ARIZONA_CABLE_MICROPHONE,
464                                                       true);
465                         if (ret != 0) {
466                                 dev_err(arizona->dev,
467                                         "Failed to report mic: %d\n", ret);
468                         }
469
470                         /* Take the headphone impedance for the main report */
471                         *reading = info->hpdet_res[1];
472                 } else {
473                         dev_dbg(arizona->dev, "Detected headphone\n");
474                 }
475
476                 /* Make sure everything is reset back to the real polarity */
477                 regmap_update_bits(arizona->regmap,
478                                    ARIZONA_ACCESSORY_DETECT_MODE_1,
479                                    ARIZONA_ACCDET_SRC,
480                                    info->micd_modes[0].src);
481         }
482
483         return 0;
484 }
485
486 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
487 {
488         struct arizona_extcon_info *info = data;
489         struct arizona *arizona = info->arizona;
490         int id_gpio = arizona->pdata.hpdet_id_gpio;
491         int report = ARIZONA_CABLE_HEADPHONE;
492         unsigned int val;
493         int ret, reading;
494
495         mutex_lock(&info->lock);
496
497         /* If we got a spurious IRQ for some reason then ignore it */
498         if (!info->hpdet_active) {
499                 dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
500                 mutex_unlock(&info->lock);
501                 return IRQ_NONE;
502         }
503
504         /* If the cable was removed while measuring ignore the result */
505         ret = extcon_get_cable_state_(&info->edev, ARIZONA_CABLE_MECHANICAL);
506         if (ret < 0) {
507                 dev_err(arizona->dev, "Failed to check cable state: %d\n",
508                         ret);
509                 goto out;
510         } else if (!ret) {
511                 dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
512                 goto done;
513         }
514
515         ret = arizona_hpdet_read(info);
516         if (ret == -EAGAIN) {
517                 goto out;
518         } else if (ret < 0) {
519                 goto done;
520         }
521         reading = ret;
522
523         /* Reset back to starting range */
524         regmap_update_bits(arizona->regmap,
525                            ARIZONA_HEADPHONE_DETECT_1,
526                            ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
527                            0);
528
529         ret = arizona_hpdet_do_id(info, &reading);
530         if (ret == -EAGAIN) {
531                 goto out;
532         } else if (ret < 0) {
533                 goto done;
534         }
535
536         /* Report high impedence cables as line outputs */
537         if (reading >= 5000)
538                 report = ARIZONA_CABLE_LINEOUT;
539         else
540                 report = ARIZONA_CABLE_HEADPHONE;
541
542         ret = extcon_set_cable_state_(&info->edev, report, true);
543         if (ret != 0)
544                 dev_err(arizona->dev, "Failed to report HP/line: %d\n",
545                         ret);
546
547         mutex_lock(&arizona->dapm->card->dapm_mutex);
548
549         ret = regmap_read(arizona->regmap, ARIZONA_OUTPUT_ENABLES_1, &val);
550         if (ret != 0) {
551                 dev_err(arizona->dev, "Failed to read output enables: %d\n",
552                         ret);
553                 val = 0;
554         }
555
556         if (!(val & (ARIZONA_OUT1L_ENA | ARIZONA_OUT1R_ENA))) {
557                 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0);
558                 if (ret != 0)
559                         dev_warn(arizona->dev, "Failed to undo magic: %d\n",
560                                  ret);
561
562                 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0);
563                 if (ret != 0)
564                         dev_warn(arizona->dev, "Failed to undo magic: %d\n",
565                                  ret);
566         }
567
568         mutex_unlock(&arizona->dapm->card->dapm_mutex);
569
570 done:
571         if (id_gpio)
572                 gpio_set_value_cansleep(id_gpio, 0);
573
574         /* Revert back to MICDET mode */
575         regmap_update_bits(arizona->regmap,
576                            ARIZONA_ACCESSORY_DETECT_MODE_1,
577                            ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
578
579         /* If we have a mic then reenable MICDET */
580         if (info->mic)
581                 arizona_start_mic(info);
582
583         if (info->hpdet_active) {
584                 pm_runtime_put_autosuspend(info->dev);
585                 info->hpdet_active = false;
586         }
587
588 out:
589         mutex_unlock(&info->lock);
590
591         return IRQ_HANDLED;
592 }
593
594 static void arizona_identify_headphone(struct arizona_extcon_info *info)
595 {
596         struct arizona *arizona = info->arizona;
597         int ret;
598
599         dev_dbg(arizona->dev, "Starting HPDET\n");
600
601         /* Make sure we keep the device enabled during the measurement */
602         pm_runtime_get(info->dev);
603
604         info->hpdet_active = true;
605
606         if (info->mic)
607                 arizona_stop_mic(info);
608
609         ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000, 0x4000);
610         if (ret != 0)
611                 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
612
613         ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000, 0x4000);
614         if (ret != 0)
615                 dev_warn(arizona->dev, "Failed to do magic: %d\n", ret);
616
617         ret = regmap_update_bits(arizona->regmap,
618                                  ARIZONA_ACCESSORY_DETECT_MODE_1,
619                                  ARIZONA_ACCDET_MODE_MASK,
620                                  ARIZONA_ACCDET_MODE_HPL);
621         if (ret != 0) {
622                 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
623                 goto err;
624         }
625
626         ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
627                                  ARIZONA_HP_POLL, ARIZONA_HP_POLL);
628         if (ret != 0) {
629                 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
630                         ret);
631                 goto err;
632         }
633
634         return;
635
636 err:
637         regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
638                            ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
639
640         /* Just report headphone */
641         ret = extcon_update_state(&info->edev,
642                                   1 << ARIZONA_CABLE_HEADPHONE,
643                                   1 << ARIZONA_CABLE_HEADPHONE);
644         if (ret != 0)
645                 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
646
647         if (info->mic)
648                 arizona_start_mic(info);
649
650         info->hpdet_active = false;
651 }
652
653 static void arizona_start_hpdet_acc_id(struct arizona_extcon_info *info)
654 {
655         struct arizona *arizona = info->arizona;
656         unsigned int val;
657         int ret;
658
659         dev_dbg(arizona->dev, "Starting identification via HPDET\n");
660
661         /* Make sure we keep the device enabled during the measurement */
662         pm_runtime_get_sync(info->dev);
663
664         info->hpdet_active = true;
665
666         arizona_extcon_pulse_micbias(info);
667
668         mutex_lock(&arizona->dapm->card->dapm_mutex);
669
670         ret = regmap_read(arizona->regmap, ARIZONA_OUTPUT_ENABLES_1, &val);
671         if (ret != 0) {
672                 dev_err(arizona->dev, "Failed to read output enables: %d\n",
673                         ret);
674                 val = 0;
675         }
676
677         if (!(val & (ARIZONA_OUT1L_ENA | ARIZONA_OUT1R_ENA))) {
678                 ret = regmap_update_bits(arizona->regmap, 0x225, 0x4000,
679                                          0x4000);
680                 if (ret != 0)
681                         dev_warn(arizona->dev, "Failed to do magic: %d\n",
682                                  ret);
683
684                 ret = regmap_update_bits(arizona->regmap, 0x226, 0x4000,
685                                          0x4000);
686                 if (ret != 0)
687                         dev_warn(arizona->dev, "Failed to do magic: %d\n",
688                                  ret);
689         }
690
691         mutex_unlock(&arizona->dapm->card->dapm_mutex);
692
693         ret = regmap_update_bits(arizona->regmap,
694                                  ARIZONA_ACCESSORY_DETECT_MODE_1,
695                                  ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
696                                  info->micd_modes[0].src |
697                                  ARIZONA_ACCDET_MODE_HPL);
698         if (ret != 0) {
699                 dev_err(arizona->dev, "Failed to set HPDETL mode: %d\n", ret);
700                 goto err;
701         }
702
703         ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
704                                  ARIZONA_HP_POLL, ARIZONA_HP_POLL);
705         if (ret != 0) {
706                 dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n",
707                         ret);
708                 goto err;
709         }
710
711         return;
712
713 err:
714         regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
715                            ARIZONA_ACCDET_MODE_MASK, ARIZONA_ACCDET_MODE_MIC);
716
717         /* Just report headphone */
718         ret = extcon_update_state(&info->edev,
719                                   1 << ARIZONA_CABLE_HEADPHONE,
720                                   1 << ARIZONA_CABLE_HEADPHONE);
721         if (ret != 0)
722                 dev_err(arizona->dev, "Failed to report headphone: %d\n", ret);
723
724         info->hpdet_active = false;
725 }
726
727 static irqreturn_t arizona_micdet(int irq, void *data)
728 {
729         struct arizona_extcon_info *info = data;
730         struct arizona *arizona = info->arizona;
731         unsigned int val, lvl;
732         int ret, i;
733
734         mutex_lock(&info->lock);
735
736         ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
737         if (ret != 0) {
738                 dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
739                 mutex_unlock(&info->lock);
740                 return IRQ_NONE;
741         }
742
743         dev_dbg(arizona->dev, "MICDET: %x\n", val);
744
745         if (!(val & ARIZONA_MICD_VALID)) {
746                 dev_warn(arizona->dev, "Microphone detection state invalid\n");
747                 mutex_unlock(&info->lock);
748                 return IRQ_NONE;
749         }
750
751         /* Due to jack detect this should never happen */
752         if (!(val & ARIZONA_MICD_STS)) {
753                 dev_warn(arizona->dev, "Detected open circuit\n");
754                 info->detecting = false;
755                 goto handled;
756         }
757
758         /* If we got a high impedence we should have a headset, report it. */
759         if (info->detecting && (val & 0x400)) {
760                 arizona_identify_headphone(info);
761
762                 ret = extcon_update_state(&info->edev,
763                                           1 << ARIZONA_CABLE_MICROPHONE,
764                                           1 << ARIZONA_CABLE_MICROPHONE);
765
766                 if (ret != 0)
767                         dev_err(arizona->dev, "Headset report failed: %d\n",
768                                 ret);
769
770                 /* Don't need to regulate for button detection */
771                 ret = regulator_allow_bypass(info->micvdd, false);
772                 if (ret != 0) {
773                         dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n",
774                                 ret);
775                 }
776
777                 info->mic = true;
778                 info->detecting = false;
779                 goto handled;
780         }
781
782         /* If we detected a lower impedence during initial startup
783          * then we probably have the wrong polarity, flip it.  Don't
784          * do this for the lowest impedences to speed up detection of
785          * plain headphones.  If both polarities report a low
786          * impedence then give up and report headphones.
787          */
788         if (info->detecting && (val & 0x3f8)) {
789                 if (info->jack_flips >= info->micd_num_modes) {
790                         dev_dbg(arizona->dev, "Detected HP/line\n");
791                         arizona_identify_headphone(info);
792
793                         info->detecting = false;
794
795                         arizona_stop_mic(info);
796                 } else {
797                         info->micd_mode++;
798                         if (info->micd_mode == info->micd_num_modes)
799                                 info->micd_mode = 0;
800                         arizona_extcon_set_mode(info, info->micd_mode);
801
802                         info->jack_flips++;
803                 }
804
805                 goto handled;
806         }
807
808         /*
809          * If we're still detecting and we detect a short then we've
810          * got a headphone.  Otherwise it's a button press.
811          */
812         if (val & 0x3fc) {
813                 if (info->mic) {
814                         dev_dbg(arizona->dev, "Mic button detected\n");
815
816                         lvl = val & ARIZONA_MICD_LVL_MASK;
817                         lvl >>= ARIZONA_MICD_LVL_SHIFT;
818
819                         for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
820                                 if (lvl & arizona_lvl_to_key[i].status)
821                                         input_report_key(info->input,
822                                                          arizona_lvl_to_key[i].report,
823                                                          1);
824                         input_sync(info->input);
825
826                 } else if (info->detecting) {
827                         dev_dbg(arizona->dev, "Headphone detected\n");
828                         info->detecting = false;
829                         arizona_stop_mic(info);
830
831                         arizona_identify_headphone(info);
832                 } else {
833                         dev_warn(arizona->dev, "Button with no mic: %x\n",
834                                  val);
835                 }
836         } else {
837                 dev_dbg(arizona->dev, "Mic button released\n");
838                 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
839                         input_report_key(info->input,
840                                          arizona_lvl_to_key[i].report, 0);
841                 input_sync(info->input);
842                 arizona_extcon_pulse_micbias(info);
843         }
844
845 handled:
846         pm_runtime_mark_last_busy(info->dev);
847         mutex_unlock(&info->lock);
848
849         return IRQ_HANDLED;
850 }
851
852 static void arizona_hpdet_work(struct work_struct *work)
853 {
854         struct arizona_extcon_info *info = container_of(work,
855                                                         struct arizona_extcon_info,
856                                                         hpdet_work.work);
857
858         mutex_lock(&info->lock);
859         arizona_start_hpdet_acc_id(info);
860         mutex_unlock(&info->lock);
861 }
862
863 static irqreturn_t arizona_jackdet(int irq, void *data)
864 {
865         struct arizona_extcon_info *info = data;
866         struct arizona *arizona = info->arizona;
867         unsigned int val, present, mask;
868         int ret, i;
869
870         pm_runtime_get_sync(info->dev);
871
872         cancel_delayed_work_sync(&info->hpdet_work);
873
874         mutex_lock(&info->lock);
875
876         if (arizona->pdata.jd_gpio5) {
877                 mask = ARIZONA_MICD_CLAMP_STS;
878                 present = 0;
879         } else {
880                 mask = ARIZONA_JD1_STS;
881                 present = ARIZONA_JD1_STS;
882         }
883
884         ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
885         if (ret != 0) {
886                 dev_err(arizona->dev, "Failed to read jackdet status: %d\n",
887                         ret);
888                 mutex_unlock(&info->lock);
889                 pm_runtime_put_autosuspend(info->dev);
890                 return IRQ_NONE;
891         }
892
893         if ((val & mask) == present) {
894                 dev_dbg(arizona->dev, "Detected jack\n");
895                 ret = extcon_set_cable_state_(&info->edev,
896                                               ARIZONA_CABLE_MECHANICAL, true);
897
898                 if (ret != 0)
899                         dev_err(arizona->dev, "Mechanical report failed: %d\n",
900                                 ret);
901
902                 if (!arizona->pdata.hpdet_acc_id) {
903                         info->detecting = true;
904                         info->mic = false;
905                         info->jack_flips = 0;
906
907                         arizona_start_mic(info);
908                 } else {
909                         schedule_delayed_work(&info->hpdet_work,
910                                               msecs_to_jiffies(250));
911                 }
912
913                 regmap_update_bits(arizona->regmap,
914                                    ARIZONA_JACK_DETECT_DEBOUNCE,
915                                    ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB, 0);
916         } else {
917                 dev_dbg(arizona->dev, "Detected jack removal\n");
918
919                 arizona_stop_mic(info);
920
921                 info->num_hpdet_res = 0;
922                 for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
923                         info->hpdet_res[i] = 0;
924                 info->mic = false;
925
926                 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
927                         input_report_key(info->input,
928                                          arizona_lvl_to_key[i].report, 0);
929                 input_sync(info->input);
930
931                 ret = extcon_update_state(&info->edev, 0xffffffff, 0);
932                 if (ret != 0)
933                         dev_err(arizona->dev, "Removal report failed: %d\n",
934                                 ret);
935
936                 regmap_update_bits(arizona->regmap,
937                                    ARIZONA_JACK_DETECT_DEBOUNCE,
938                                    ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
939                                    ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
940         }
941
942         mutex_unlock(&info->lock);
943
944         pm_runtime_mark_last_busy(info->dev);
945         pm_runtime_put_autosuspend(info->dev);
946
947         return IRQ_HANDLED;
948 }
949
950 static int arizona_extcon_probe(struct platform_device *pdev)
951 {
952         struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
953         struct arizona_pdata *pdata;
954         struct arizona_extcon_info *info;
955         int jack_irq_fall, jack_irq_rise;
956         int ret, mode, i;
957
958         if (!arizona->dapm || !arizona->dapm->card)
959                 return -EPROBE_DEFER;
960
961         pdata = dev_get_platdata(arizona->dev);
962
963         info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
964         if (!info) {
965                 dev_err(&pdev->dev, "Failed to allocate memory\n");
966                 ret = -ENOMEM;
967                 goto err;
968         }
969
970         info->micvdd = devm_regulator_get(arizona->dev, "MICVDD");
971         if (IS_ERR(info->micvdd)) {
972                 ret = PTR_ERR(info->micvdd);
973                 dev_err(arizona->dev, "Failed to get MICVDD: %d\n", ret);
974                 goto err;
975         }
976
977         mutex_init(&info->lock);
978         info->arizona = arizona;
979         info->dev = &pdev->dev;
980         INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
981         platform_set_drvdata(pdev, info);
982
983         switch (arizona->type) {
984         case WM5102:
985                 switch (arizona->rev) {
986                 case 0:
987                         info->micd_reva = true;
988                         break;
989                 default:
990                         info->micd_clamp = true;
991                         info->hpdet_ip = 1;
992                         break;
993                 }
994                 break;
995         default:
996                 break;
997         }
998
999         info->edev.name = "Headset Jack";
1000         info->edev.supported_cable = arizona_cable;
1001
1002         ret = extcon_dev_register(&info->edev, arizona->dev);
1003         if (ret < 0) {
1004                 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
1005                         ret);
1006                 goto err;
1007         }
1008
1009         if (pdata->num_micd_configs) {
1010                 info->micd_modes = pdata->micd_configs;
1011                 info->micd_num_modes = pdata->num_micd_configs;
1012         } else {
1013                 info->micd_modes = micd_default_modes;
1014                 info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1015         }
1016
1017         if (arizona->pdata.micd_pol_gpio > 0) {
1018                 if (info->micd_modes[0].gpio)
1019                         mode = GPIOF_OUT_INIT_HIGH;
1020                 else
1021                         mode = GPIOF_OUT_INIT_LOW;
1022
1023                 ret = devm_gpio_request_one(&pdev->dev,
1024                                             arizona->pdata.micd_pol_gpio,
1025                                             mode,
1026                                             "MICD polarity");
1027                 if (ret != 0) {
1028                         dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1029                                 arizona->pdata.micd_pol_gpio, ret);
1030                         goto err_register;
1031                 }
1032         }
1033
1034         if (arizona->pdata.hpdet_id_gpio > 0) {
1035                 ret = devm_gpio_request_one(&pdev->dev,
1036                                             arizona->pdata.hpdet_id_gpio,
1037                                             GPIOF_OUT_INIT_LOW,
1038                                             "HPDET");
1039                 if (ret != 0) {
1040                         dev_err(arizona->dev, "Failed to request GPIO%d: %d\n",
1041                                 arizona->pdata.hpdet_id_gpio, ret);
1042                         goto err_register;
1043                 }
1044         }
1045
1046         if (arizona->pdata.micd_bias_start_time)
1047                 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1048                                    ARIZONA_MICD_BIAS_STARTTIME_MASK,
1049                                    arizona->pdata.micd_bias_start_time
1050                                    << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1051
1052         if (arizona->pdata.micd_rate)
1053                 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1054                                    ARIZONA_MICD_RATE_MASK,
1055                                    arizona->pdata.micd_rate
1056                                    << ARIZONA_MICD_RATE_SHIFT);
1057
1058         if (arizona->pdata.micd_dbtime)
1059                 regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1060                                    ARIZONA_MICD_DBTIME_MASK,
1061                                    arizona->pdata.micd_dbtime
1062                                    << ARIZONA_MICD_DBTIME_SHIFT);
1063
1064         /*
1065          * If we have a clamp use it, activating in conjunction with
1066          * GPIO5 if that is connected for jack detect operation.
1067          */
1068         if (info->micd_clamp) {
1069                 if (arizona->pdata.jd_gpio5) {
1070                         /* Put the GPIO into input mode */
1071                         regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1072                                      0xc101);
1073
1074                         regmap_update_bits(arizona->regmap,
1075                                            ARIZONA_MICD_CLAMP_CONTROL,
1076                                            ARIZONA_MICD_CLAMP_MODE_MASK, 0x9);
1077                 } else {
1078                         regmap_update_bits(arizona->regmap,
1079                                            ARIZONA_MICD_CLAMP_CONTROL,
1080                                            ARIZONA_MICD_CLAMP_MODE_MASK, 0x4);
1081                 }
1082
1083                 regmap_update_bits(arizona->regmap,
1084                                    ARIZONA_JACK_DETECT_DEBOUNCE,
1085                                    ARIZONA_MICD_CLAMP_DB,
1086                                    ARIZONA_MICD_CLAMP_DB);
1087         }
1088
1089         arizona_extcon_set_mode(info, 0);
1090
1091         info->input = devm_input_allocate_device(&pdev->dev);
1092         if (!info->input) {
1093                 dev_err(arizona->dev, "Can't allocate input dev\n");
1094                 ret = -ENOMEM;
1095                 goto err_register;
1096         }
1097
1098         for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
1099                 input_set_capability(info->input, EV_KEY,
1100                                      arizona_lvl_to_key[i].report);
1101         info->input->name = "Headset";
1102         info->input->phys = "arizona/extcon";
1103         info->input->dev.parent = &pdev->dev;
1104
1105         pm_runtime_enable(&pdev->dev);
1106         pm_runtime_idle(&pdev->dev);
1107         pm_runtime_get_sync(&pdev->dev);
1108
1109         if (arizona->pdata.jd_gpio5) {
1110                 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1111                 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1112         } else {
1113                 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1114                 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1115         }
1116
1117         ret = arizona_request_irq(arizona, jack_irq_rise,
1118                                   "JACKDET rise", arizona_jackdet, info);
1119         if (ret != 0) {
1120                 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
1121                         ret);
1122                 goto err_input;
1123         }
1124
1125         ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1126         if (ret != 0) {
1127                 dev_err(&pdev->dev, "Failed to set JD rise IRQ wake: %d\n",
1128                         ret);
1129                 goto err_rise;
1130         }
1131
1132         ret = arizona_request_irq(arizona, jack_irq_fall,
1133                                   "JACKDET fall", arizona_jackdet, info);
1134         if (ret != 0) {
1135                 dev_err(&pdev->dev, "Failed to get JD fall IRQ: %d\n", ret);
1136                 goto err_rise_wake;
1137         }
1138
1139         ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1140         if (ret != 0) {
1141                 dev_err(&pdev->dev, "Failed to set JD fall IRQ wake: %d\n",
1142                         ret);
1143                 goto err_fall;
1144         }
1145
1146         ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1147                                   "MICDET", arizona_micdet, info);
1148         if (ret != 0) {
1149                 dev_err(&pdev->dev, "Failed to get MICDET IRQ: %d\n", ret);
1150                 goto err_fall_wake;
1151         }
1152
1153         ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1154                                   "HPDET", arizona_hpdet_irq, info);
1155         if (ret != 0) {
1156                 dev_err(&pdev->dev, "Failed to get HPDET IRQ: %d\n", ret);
1157                 goto err_micdet;
1158         }
1159
1160         arizona_clk32k_enable(arizona);
1161         regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1162                            ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1163         regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1164                            ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1165
1166         ret = regulator_allow_bypass(info->micvdd, true);
1167         if (ret != 0)
1168                 dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n",
1169                          ret);
1170
1171         pm_runtime_put(&pdev->dev);
1172
1173         ret = input_register_device(info->input);
1174         if (ret) {
1175                 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
1176                 goto err_hpdet;
1177         }
1178
1179         return 0;
1180
1181 err_hpdet:
1182         arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1183 err_micdet:
1184         arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1185 err_fall_wake:
1186         arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1187 err_fall:
1188         arizona_free_irq(arizona, jack_irq_fall, info);
1189 err_rise_wake:
1190         arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1191 err_rise:
1192         arizona_free_irq(arizona, jack_irq_rise, info);
1193 err_input:
1194 err_register:
1195         pm_runtime_disable(&pdev->dev);
1196         extcon_dev_unregister(&info->edev);
1197 err:
1198         return ret;
1199 }
1200
1201 static int arizona_extcon_remove(struct platform_device *pdev)
1202 {
1203         struct arizona_extcon_info *info = platform_get_drvdata(pdev);
1204         struct arizona *arizona = info->arizona;
1205         int jack_irq_rise, jack_irq_fall;
1206
1207         pm_runtime_disable(&pdev->dev);
1208
1209         regmap_update_bits(arizona->regmap,
1210                            ARIZONA_MICD_CLAMP_CONTROL,
1211                            ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1212
1213         if (arizona->pdata.jd_gpio5) {
1214                 jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1215                 jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1216         } else {
1217                 jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1218                 jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1219         }
1220
1221         arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1222         arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1223         arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1224         arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1225         arizona_free_irq(arizona, jack_irq_rise, info);
1226         arizona_free_irq(arizona, jack_irq_fall, info);
1227         cancel_delayed_work_sync(&info->hpdet_work);
1228         regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1229                            ARIZONA_JD1_ENA, 0);
1230         arizona_clk32k_disable(arizona);
1231         extcon_dev_unregister(&info->edev);
1232
1233         return 0;
1234 }
1235
1236 static struct platform_driver arizona_extcon_driver = {
1237         .driver         = {
1238                 .name   = "arizona-extcon",
1239                 .owner  = THIS_MODULE,
1240         },
1241         .probe          = arizona_extcon_probe,
1242         .remove         = arizona_extcon_remove,
1243 };
1244
1245 module_platform_driver(arizona_extcon_driver);
1246
1247 MODULE_DESCRIPTION("Arizona Extcon driver");
1248 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1249 MODULE_LICENSE("GPL");
1250 MODULE_ALIAS("platform:extcon-arizona");