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