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