xref: /linux/sound/soc/codecs/arizona-jack.c (revision a8e7ef3cec99ba2487110e01d77a8a278593b3e9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * extcon-arizona.c - Extcon driver Wolfson Arizona devices
4  *
5  *  Copyright (C) 2012-2014 Wolfson Microelectronics plc
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/interrupt.h>
12 #include <linux/err.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/input.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/property.h>
17 #include <linux/regulator/consumer.h>
18 
19 #include <sound/jack.h>
20 #include <sound/soc.h>
21 
22 #include <linux/mfd/arizona/core.h>
23 #include <linux/mfd/arizona/pdata.h>
24 #include <linux/mfd/arizona/registers.h>
25 #include <dt-bindings/mfd/arizona.h>
26 
27 #include "arizona.h"
28 
29 #define ARIZONA_MAX_MICD_RANGE 8
30 
31 /*
32  * The hardware supports 8 ranges / buttons, but the snd-jack interface
33  * only supports 6 buttons (button 0-5).
34  */
35 #define ARIZONA_MAX_MICD_BUTTONS 6
36 
37 #define ARIZONA_MICD_CLAMP_MODE_JDL      0x4
38 #define ARIZONA_MICD_CLAMP_MODE_JDH      0x5
39 #define ARIZONA_MICD_CLAMP_MODE_JDL_GP5H 0x9
40 #define ARIZONA_MICD_CLAMP_MODE_JDH_GP5H 0xb
41 
42 #define ARIZONA_TST_CAP_DEFAULT 0x3
43 #define ARIZONA_TST_CAP_CLAMP   0x1
44 
45 #define ARIZONA_HPDET_MAX 10000
46 
47 #define HPDET_DEBOUNCE 500
48 #define DEFAULT_MICD_TIMEOUT 2000
49 
50 #define ARIZONA_HPDET_WAIT_COUNT 15
51 #define ARIZONA_HPDET_WAIT_DELAY_MS 20
52 
53 #define QUICK_HEADPHONE_MAX_OHM 3
54 #define MICROPHONE_MIN_OHM      1257
55 #define MICROPHONE_MAX_OHM      30000
56 
57 #define MICD_DBTIME_TWO_READINGS 2
58 #define MICD_DBTIME_FOUR_READINGS 4
59 
60 #define MICD_LVL_1_TO_7 (ARIZONA_MICD_LVL_1 | ARIZONA_MICD_LVL_2 | \
61 			 ARIZONA_MICD_LVL_3 | ARIZONA_MICD_LVL_4 | \
62 			 ARIZONA_MICD_LVL_5 | ARIZONA_MICD_LVL_6 | \
63 			 ARIZONA_MICD_LVL_7)
64 
65 #define MICD_LVL_0_TO_7 (ARIZONA_MICD_LVL_0 | MICD_LVL_1_TO_7)
66 
67 #define MICD_LVL_0_TO_8 (MICD_LVL_0_TO_7 | ARIZONA_MICD_LVL_8)
68 
69 static const struct arizona_micd_config micd_default_modes[] = {
70 	{ ARIZONA_ACCDET_SRC, 1, 0 },
71 	{ 0,                  2, 1 },
72 };
73 
74 static const struct arizona_micd_range micd_default_ranges[] = {
75 	{ .max =  11, .key = BTN_0 },
76 	{ .max =  28, .key = BTN_1 },
77 	{ .max =  54, .key = BTN_2 },
78 	{ .max = 100, .key = BTN_3 },
79 	{ .max = 186, .key = BTN_4 },
80 	{ .max = 430, .key = BTN_5 },
81 };
82 
83 /* The number of levels in arizona_micd_levels valid for button thresholds */
84 #define ARIZONA_NUM_MICD_BUTTON_LEVELS 64
85 
86 static const int arizona_micd_levels[] = {
87 	3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 34, 36, 39, 41, 44, 46,
88 	49, 52, 54, 57, 60, 62, 65, 67, 70, 73, 75, 78, 81, 83, 89, 94, 100,
89 	105, 111, 116, 122, 127, 139, 150, 161, 173, 186, 196, 209, 220, 245,
90 	270, 295, 321, 348, 375, 402, 430, 489, 550, 614, 681, 752, 903, 1071,
91 	1257, 30000,
92 };
93 
94 static void arizona_start_hpdet_acc_id(struct arizona_priv *info);
95 
96 static void arizona_extcon_hp_clamp(struct arizona_priv *info,
97 				    bool clamp)
98 {
99 	struct arizona *arizona = info->arizona;
100 	unsigned int mask = 0, val = 0;
101 	unsigned int cap_sel = 0;
102 	int ret;
103 
104 	switch (arizona->type) {
105 	case WM8998:
106 	case WM1814:
107 		mask = 0;
108 		break;
109 	case WM5110:
110 	case WM8280:
111 		mask = ARIZONA_HP1L_SHRTO | ARIZONA_HP1L_FLWR |
112 		       ARIZONA_HP1L_SHRTI;
113 		if (clamp) {
114 			val = ARIZONA_HP1L_SHRTO;
115 			cap_sel = ARIZONA_TST_CAP_CLAMP;
116 		} else {
117 			val = ARIZONA_HP1L_FLWR | ARIZONA_HP1L_SHRTI;
118 			cap_sel = ARIZONA_TST_CAP_DEFAULT;
119 		}
120 
121 		ret = regmap_update_bits(arizona->regmap,
122 					 ARIZONA_HP_TEST_CTRL_1,
123 					 ARIZONA_HP1_TST_CAP_SEL_MASK,
124 					 cap_sel);
125 		if (ret)
126 			dev_warn(arizona->dev, "Failed to set TST_CAP_SEL: %d\n", ret);
127 		break;
128 	default:
129 		mask = ARIZONA_RMV_SHRT_HP1L;
130 		if (clamp)
131 			val = ARIZONA_RMV_SHRT_HP1L;
132 		break;
133 	}
134 
135 	snd_soc_dapm_mutex_lock(arizona->dapm);
136 
137 	arizona->hpdet_clamp = clamp;
138 
139 	/* Keep the HP output stages disabled while doing the clamp */
140 	if (clamp) {
141 		ret = regmap_update_bits(arizona->regmap,
142 					 ARIZONA_OUTPUT_ENABLES_1,
143 					 ARIZONA_OUT1L_ENA |
144 					 ARIZONA_OUT1R_ENA, 0);
145 		if (ret)
146 			dev_warn(arizona->dev, "Failed to disable headphone outputs: %d\n", ret);
147 	}
148 
149 	if (mask) {
150 		ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1L,
151 					 mask, val);
152 		if (ret)
153 			dev_warn(arizona->dev, "Failed to do clamp: %d\n", ret);
154 
155 		ret = regmap_update_bits(arizona->regmap, ARIZONA_HP_CTRL_1R,
156 					 mask, val);
157 		if (ret)
158 			dev_warn(arizona->dev, "Failed to do clamp: %d\n", ret);
159 	}
160 
161 	/* Restore the desired state while not doing the clamp */
162 	if (!clamp) {
163 		ret = regmap_update_bits(arizona->regmap,
164 					 ARIZONA_OUTPUT_ENABLES_1,
165 					 ARIZONA_OUT1L_ENA |
166 					 ARIZONA_OUT1R_ENA, arizona->hp_ena);
167 		if (ret)
168 			dev_warn(arizona->dev, "Failed to restore headphone outputs: %d\n", ret);
169 	}
170 
171 	snd_soc_dapm_mutex_unlock(arizona->dapm);
172 }
173 
174 static void arizona_extcon_set_mode(struct arizona_priv *info, int mode)
175 {
176 	struct arizona *arizona = info->arizona;
177 
178 	mode %= info->micd_num_modes;
179 
180 	gpiod_set_value_cansleep(info->micd_pol_gpio,
181 				 info->micd_modes[mode].gpio);
182 
183 	regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
184 			   ARIZONA_MICD_BIAS_SRC_MASK,
185 			   info->micd_modes[mode].bias <<
186 			   ARIZONA_MICD_BIAS_SRC_SHIFT);
187 	regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
188 			   ARIZONA_ACCDET_SRC, info->micd_modes[mode].src);
189 
190 	info->micd_mode = mode;
191 
192 	dev_dbg(arizona->dev, "Set jack polarity to %d\n", mode);
193 }
194 
195 static const char *arizona_extcon_get_micbias(struct arizona_priv *info)
196 {
197 	switch (info->micd_modes[0].bias) {
198 	case 1:
199 		return "MICBIAS1";
200 	case 2:
201 		return "MICBIAS2";
202 	case 3:
203 		return "MICBIAS3";
204 	default:
205 		return "MICVDD";
206 	}
207 }
208 
209 static void arizona_extcon_pulse_micbias(struct arizona_priv *info)
210 {
211 	struct arizona *arizona = info->arizona;
212 	const char *widget = arizona_extcon_get_micbias(info);
213 	struct snd_soc_dapm_context *dapm = arizona->dapm;
214 	int ret;
215 
216 	ret = snd_soc_dapm_force_enable_pin(dapm, widget);
217 	if (ret)
218 		dev_warn(arizona->dev, "Failed to enable %s: %d\n", widget, ret);
219 
220 	snd_soc_dapm_sync(dapm);
221 
222 	if (!arizona->pdata.micd_force_micbias) {
223 		ret = snd_soc_dapm_disable_pin(dapm, widget);
224 		if (ret)
225 			dev_warn(arizona->dev, "Failed to disable %s: %d\n", widget, ret);
226 
227 		snd_soc_dapm_sync(dapm);
228 	}
229 }
230 
231 static void arizona_start_mic(struct arizona_priv *info)
232 {
233 	struct arizona *arizona = info->arizona;
234 	bool change;
235 	int ret;
236 	unsigned int mode;
237 
238 	/* Microphone detection can't use idle mode */
239 	pm_runtime_get_sync(arizona->dev);
240 
241 	if (info->detecting) {
242 		ret = regulator_allow_bypass(info->micvdd, false);
243 		if (ret)
244 			dev_err(arizona->dev, "Failed to regulate MICVDD: %d\n", ret);
245 	}
246 
247 	ret = regulator_enable(info->micvdd);
248 	if (ret)
249 		dev_err(arizona->dev, "Failed to enable MICVDD: %d\n", ret);
250 
251 	if (info->micd_reva) {
252 		const struct reg_sequence reva[] = {
253 			{ 0x80,  0x3 },
254 			{ 0x294, 0x0 },
255 			{ 0x80,  0x0 },
256 		};
257 
258 		regmap_multi_reg_write(arizona->regmap, reva, ARRAY_SIZE(reva));
259 	}
260 
261 	if (info->detecting && arizona->pdata.micd_software_compare)
262 		mode = ARIZONA_ACCDET_MODE_ADC;
263 	else
264 		mode = ARIZONA_ACCDET_MODE_MIC;
265 
266 	regmap_update_bits(arizona->regmap,
267 			   ARIZONA_ACCESSORY_DETECT_MODE_1,
268 			   ARIZONA_ACCDET_MODE_MASK, mode);
269 
270 	arizona_extcon_pulse_micbias(info);
271 
272 	ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
273 				       ARIZONA_MICD_ENA, ARIZONA_MICD_ENA,
274 				       &change);
275 	if (ret < 0) {
276 		dev_err(arizona->dev, "Failed to enable micd: %d\n", ret);
277 	} else if (!change) {
278 		regulator_disable(info->micvdd);
279 		pm_runtime_put_autosuspend(arizona->dev);
280 	}
281 }
282 
283 static void arizona_stop_mic(struct arizona_priv *info)
284 {
285 	struct arizona *arizona = info->arizona;
286 	const char *widget = arizona_extcon_get_micbias(info);
287 	struct snd_soc_dapm_context *dapm = arizona->dapm;
288 	bool change = false;
289 	int ret;
290 
291 	ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
292 				       ARIZONA_MICD_ENA, 0,
293 				       &change);
294 	if (ret < 0)
295 		dev_err(arizona->dev, "Failed to disable micd: %d\n", ret);
296 
297 	ret = snd_soc_dapm_disable_pin(dapm, widget);
298 	if (ret)
299 		dev_warn(arizona->dev, "Failed to disable %s: %d\n", widget, ret);
300 
301 	snd_soc_dapm_sync(dapm);
302 
303 	if (info->micd_reva) {
304 		const struct reg_sequence reva[] = {
305 			{ 0x80,  0x3 },
306 			{ 0x294, 0x2 },
307 			{ 0x80,  0x0 },
308 		};
309 
310 		regmap_multi_reg_write(arizona->regmap, reva, ARRAY_SIZE(reva));
311 	}
312 
313 	ret = regulator_allow_bypass(info->micvdd, true);
314 	if (ret)
315 		dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n", ret);
316 
317 	if (change) {
318 		regulator_disable(info->micvdd);
319 		pm_runtime_put_autosuspend(arizona->dev);
320 	}
321 }
322 
323 static struct {
324 	unsigned int threshold;
325 	unsigned int factor_a;
326 	unsigned int factor_b;
327 } arizona_hpdet_b_ranges[] = {
328 	{ 100,  5528,   362464 },
329 	{ 169, 11084,  6186851 },
330 	{ 169, 11065, 65460395 },
331 };
332 
333 #define ARIZONA_HPDET_B_RANGE_MAX 0x3fb
334 
335 static struct {
336 	int min;
337 	int max;
338 } arizona_hpdet_c_ranges[] = {
339 	{ 0,       30 },
340 	{ 8,      100 },
341 	{ 100,   1000 },
342 	{ 1000, 10000 },
343 };
344 
345 static int arizona_hpdet_read(struct arizona_priv *info)
346 {
347 	struct arizona *arizona = info->arizona;
348 	unsigned int val, range;
349 	int ret;
350 
351 	ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2, &val);
352 	if (ret) {
353 		dev_err(arizona->dev, "Failed to read HPDET status: %d\n", ret);
354 		return ret;
355 	}
356 
357 	switch (info->hpdet_ip_version) {
358 	case 0:
359 		if (!(val & ARIZONA_HP_DONE)) {
360 			dev_err(arizona->dev, "HPDET did not complete: %x\n", val);
361 			return -EAGAIN;
362 		}
363 
364 		val &= ARIZONA_HP_LVL_MASK;
365 		break;
366 
367 	case 1:
368 		if (!(val & ARIZONA_HP_DONE_B)) {
369 			dev_err(arizona->dev, "HPDET did not complete: %x\n", val);
370 			return -EAGAIN;
371 		}
372 
373 		ret = regmap_read(arizona->regmap, ARIZONA_HP_DACVAL, &val);
374 		if (ret) {
375 			dev_err(arizona->dev, "Failed to read HP value: %d\n", ret);
376 			return -EAGAIN;
377 		}
378 
379 		regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
380 			    &range);
381 		range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
382 			   >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
383 
384 		if (range < ARRAY_SIZE(arizona_hpdet_b_ranges) - 1 &&
385 		    (val < arizona_hpdet_b_ranges[range].threshold ||
386 		     val >= ARIZONA_HPDET_B_RANGE_MAX)) {
387 			range++;
388 			dev_dbg(arizona->dev, "Moving to HPDET range %d\n", range);
389 			regmap_update_bits(arizona->regmap,
390 					   ARIZONA_HEADPHONE_DETECT_1,
391 					   ARIZONA_HP_IMPEDANCE_RANGE_MASK,
392 					   range <<
393 					   ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
394 			return -EAGAIN;
395 		}
396 
397 		/* If we go out of range report top of range */
398 		if (val < arizona_hpdet_b_ranges[range].threshold ||
399 		    val >= ARIZONA_HPDET_B_RANGE_MAX) {
400 			dev_dbg(arizona->dev, "Measurement out of range\n");
401 			return ARIZONA_HPDET_MAX;
402 		}
403 
404 		dev_dbg(arizona->dev, "HPDET read %d in range %d\n", val, range);
405 
406 		val = arizona_hpdet_b_ranges[range].factor_b
407 			/ ((val * 100) -
408 			   arizona_hpdet_b_ranges[range].factor_a);
409 		break;
410 
411 	case 2:
412 		if (!(val & ARIZONA_HP_DONE_B)) {
413 			dev_err(arizona->dev, "HPDET did not complete: %x\n", val);
414 			return -EAGAIN;
415 		}
416 
417 		val &= ARIZONA_HP_LVL_B_MASK;
418 		/* Convert to ohms, the value is in 0.5 ohm increments */
419 		val /= 2;
420 
421 		regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
422 			    &range);
423 		range = (range & ARIZONA_HP_IMPEDANCE_RANGE_MASK)
424 			   >> ARIZONA_HP_IMPEDANCE_RANGE_SHIFT;
425 
426 		/* Skip up a range, or report? */
427 		if (range < ARRAY_SIZE(arizona_hpdet_c_ranges) - 1 &&
428 		    (val >= arizona_hpdet_c_ranges[range].max)) {
429 			range++;
430 			dev_dbg(arizona->dev, "Moving to HPDET range %d-%d\n",
431 				arizona_hpdet_c_ranges[range].min,
432 				arizona_hpdet_c_ranges[range].max);
433 			regmap_update_bits(arizona->regmap,
434 					   ARIZONA_HEADPHONE_DETECT_1,
435 					   ARIZONA_HP_IMPEDANCE_RANGE_MASK,
436 					   range <<
437 					   ARIZONA_HP_IMPEDANCE_RANGE_SHIFT);
438 			return -EAGAIN;
439 		}
440 
441 		if (range && (val < arizona_hpdet_c_ranges[range].min)) {
442 			dev_dbg(arizona->dev, "Reporting range boundary %d\n",
443 				arizona_hpdet_c_ranges[range].min);
444 			val = arizona_hpdet_c_ranges[range].min;
445 		}
446 		break;
447 
448 	default:
449 		dev_warn(arizona->dev, "Unknown HPDET IP revision %d\n", info->hpdet_ip_version);
450 		return -EINVAL;
451 	}
452 
453 	dev_dbg(arizona->dev, "HP impedance %d ohms\n", val);
454 	return val;
455 }
456 
457 static int arizona_hpdet_do_id(struct arizona_priv *info, int *reading,
458 			       bool *mic)
459 {
460 	struct arizona *arizona = info->arizona;
461 
462 	if (!arizona->pdata.hpdet_acc_id)
463 		return 0;
464 
465 	/*
466 	 * If we're using HPDET for accessory identification we need
467 	 * to take multiple measurements, step through them in sequence.
468 	 */
469 	info->hpdet_res[info->num_hpdet_res++] = *reading;
470 
471 	/* Only check the mic directly if we didn't already ID it */
472 	if (info->hpdet_id_gpio && info->num_hpdet_res == 1) {
473 		dev_dbg(arizona->dev, "Measuring mic\n");
474 
475 		regmap_update_bits(arizona->regmap,
476 				   ARIZONA_ACCESSORY_DETECT_MODE_1,
477 				   ARIZONA_ACCDET_MODE_MASK |
478 				   ARIZONA_ACCDET_SRC,
479 				   ARIZONA_ACCDET_MODE_HPR |
480 				   info->micd_modes[0].src);
481 
482 		gpiod_set_value_cansleep(info->hpdet_id_gpio, 1);
483 
484 		regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
485 				   ARIZONA_HP_POLL, ARIZONA_HP_POLL);
486 		return -EAGAIN;
487 	}
488 
489 	/* OK, got both.  Now, compare... */
490 	dev_dbg(arizona->dev, "HPDET measured %d %d\n",
491 		info->hpdet_res[0], info->hpdet_res[1]);
492 
493 	/* Take the headphone impedance for the main report */
494 	*reading = info->hpdet_res[0];
495 
496 	/* Sometimes we get false readings due to slow insert */
497 	if (*reading >= ARIZONA_HPDET_MAX && !info->hpdet_retried) {
498 		dev_dbg(arizona->dev, "Retrying high impedance\n");
499 		info->num_hpdet_res = 0;
500 		info->hpdet_retried = true;
501 		arizona_start_hpdet_acc_id(info);
502 		pm_runtime_put(arizona->dev);
503 		return -EAGAIN;
504 	}
505 
506 	/*
507 	 * If we measure the mic as high impedance
508 	 */
509 	if (!info->hpdet_id_gpio || info->hpdet_res[1] > 50) {
510 		dev_dbg(arizona->dev, "Detected mic\n");
511 		*mic = true;
512 		info->detecting = true;
513 	} else {
514 		dev_dbg(arizona->dev, "Detected headphone\n");
515 	}
516 
517 	/* Make sure everything is reset back to the real polarity */
518 	regmap_update_bits(arizona->regmap, ARIZONA_ACCESSORY_DETECT_MODE_1,
519 			   ARIZONA_ACCDET_SRC, info->micd_modes[0].src);
520 
521 	return 0;
522 }
523 
524 static irqreturn_t arizona_hpdet_irq(int irq, void *data)
525 {
526 	struct arizona_priv *info = data;
527 	struct arizona *arizona = info->arizona;
528 	int ret, reading, state, report;
529 	bool mic = false;
530 
531 	mutex_lock(&info->lock);
532 
533 	/* If we got a spurious IRQ for some reason then ignore it */
534 	if (!info->hpdet_active) {
535 		dev_warn(arizona->dev, "Spurious HPDET IRQ\n");
536 		mutex_unlock(&info->lock);
537 		return IRQ_NONE;
538 	}
539 
540 	/* If the cable was removed while measuring ignore the result */
541 	state = info->jack->status & SND_JACK_MECHANICAL;
542 	if (!state) {
543 		dev_dbg(arizona->dev, "Ignoring HPDET for removed cable\n");
544 		goto done;
545 	}
546 
547 	ret = arizona_hpdet_read(info);
548 	if (ret == -EAGAIN)
549 		goto out;
550 	else if (ret < 0)
551 		goto done;
552 	reading = ret;
553 
554 	/* Reset back to starting range */
555 	regmap_update_bits(arizona->regmap,
556 			   ARIZONA_HEADPHONE_DETECT_1,
557 			   ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
558 			   0);
559 
560 	ret = arizona_hpdet_do_id(info, &reading, &mic);
561 	if (ret == -EAGAIN)
562 		goto out;
563 	else if (ret < 0)
564 		goto done;
565 
566 	/* Report high impedence cables as line outputs */
567 	if (reading >= 5000)
568 		report = SND_JACK_LINEOUT;
569 	else
570 		report = SND_JACK_HEADPHONE;
571 
572 	snd_soc_jack_report(info->jack, report, SND_JACK_LINEOUT | SND_JACK_HEADPHONE);
573 
574 done:
575 	/* Reset back to starting range */
576 	regmap_update_bits(arizona->regmap,
577 			   ARIZONA_HEADPHONE_DETECT_1,
578 			   ARIZONA_HP_IMPEDANCE_RANGE_MASK | ARIZONA_HP_POLL,
579 			   0);
580 
581 	arizona_extcon_hp_clamp(info, false);
582 
583 	if (info->hpdet_id_gpio)
584 		gpiod_set_value_cansleep(info->hpdet_id_gpio, 0);
585 
586 	/* If we have a mic then reenable MICDET */
587 	if (state && (mic || info->mic))
588 		arizona_start_mic(info);
589 
590 	if (info->hpdet_active) {
591 		pm_runtime_put_autosuspend(arizona->dev);
592 		info->hpdet_active = false;
593 	}
594 
595 	/* Do not set hp_det done when the cable has been unplugged */
596 	if (state)
597 		info->hpdet_done = true;
598 
599 out:
600 	mutex_unlock(&info->lock);
601 
602 	return IRQ_HANDLED;
603 }
604 
605 static void arizona_identify_headphone(struct arizona_priv *info)
606 {
607 	struct arizona *arizona = info->arizona;
608 	int ret;
609 
610 	if (info->hpdet_done)
611 		return;
612 
613 	dev_dbg(arizona->dev, "Starting HPDET\n");
614 
615 	/* Make sure we keep the device enabled during the measurement */
616 	pm_runtime_get_sync(arizona->dev);
617 
618 	info->hpdet_active = true;
619 
620 	arizona_stop_mic(info);
621 
622 	arizona_extcon_hp_clamp(info, true);
623 
624 	ret = regmap_update_bits(arizona->regmap,
625 				 ARIZONA_ACCESSORY_DETECT_MODE_1,
626 				 ARIZONA_ACCDET_MODE_MASK,
627 				 arizona->pdata.hpdet_channel);
628 	if (ret != 0) {
629 		dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
630 		goto err;
631 	}
632 
633 	ret = regmap_update_bits(arizona->regmap, ARIZONA_HEADPHONE_DETECT_1,
634 				 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
635 	if (ret) {
636 		dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n", ret);
637 		goto err;
638 	}
639 
640 	return;
641 
642 err:
643 	arizona_extcon_hp_clamp(info, false);
644 	pm_runtime_put_autosuspend(arizona->dev);
645 
646 	/* Just report headphone */
647 	snd_soc_jack_report(info->jack, SND_JACK_HEADPHONE,
648 			    SND_JACK_LINEOUT | SND_JACK_HEADPHONE);
649 
650 	if (info->mic)
651 		arizona_start_mic(info);
652 
653 	info->hpdet_active = false;
654 }
655 
656 static void arizona_start_hpdet_acc_id(struct arizona_priv *info)
657 {
658 	struct arizona *arizona = info->arizona;
659 	int hp_reading = 32;
660 	bool mic;
661 	int ret;
662 
663 	dev_dbg(arizona->dev, "Starting identification via HPDET\n");
664 
665 	/* Make sure we keep the device enabled during the measurement */
666 	pm_runtime_get_sync(arizona->dev);
667 
668 	info->hpdet_active = true;
669 
670 	arizona_extcon_hp_clamp(info, true);
671 
672 	ret = regmap_update_bits(arizona->regmap,
673 				 ARIZONA_ACCESSORY_DETECT_MODE_1,
674 				 ARIZONA_ACCDET_SRC | ARIZONA_ACCDET_MODE_MASK,
675 				 info->micd_modes[0].src |
676 				 arizona->pdata.hpdet_channel);
677 	if (ret != 0) {
678 		dev_err(arizona->dev, "Failed to set HPDET mode: %d\n", ret);
679 		goto err;
680 	}
681 
682 	if (arizona->pdata.hpdet_acc_id_line) {
683 		ret = regmap_update_bits(arizona->regmap,
684 					 ARIZONA_HEADPHONE_DETECT_1,
685 					 ARIZONA_HP_POLL, ARIZONA_HP_POLL);
686 		if (ret) {
687 			dev_err(arizona->dev, "Can't start HPDETL measurement: %d\n", ret);
688 			goto err;
689 		}
690 	} else {
691 		arizona_hpdet_do_id(info, &hp_reading, &mic);
692 	}
693 
694 	return;
695 
696 err:
697 	/* Just report headphone */
698 	snd_soc_jack_report(info->jack, SND_JACK_HEADPHONE,
699 			    SND_JACK_LINEOUT | SND_JACK_HEADPHONE);
700 
701 	info->hpdet_active = false;
702 }
703 
704 static void arizona_micd_timeout_work(struct work_struct *work)
705 {
706 	struct arizona_priv *info = container_of(work,
707 						struct arizona_priv,
708 						micd_timeout_work.work);
709 
710 	mutex_lock(&info->lock);
711 
712 	dev_dbg(info->arizona->dev, "MICD timed out, reporting HP\n");
713 
714 	info->detecting = false;
715 
716 	arizona_identify_headphone(info);
717 
718 	mutex_unlock(&info->lock);
719 }
720 
721 static int arizona_micd_adc_read(struct arizona_priv *info)
722 {
723 	struct arizona *arizona = info->arizona;
724 	unsigned int val;
725 	int ret;
726 
727 	/* Must disable MICD before we read the ADCVAL */
728 	regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
729 			   ARIZONA_MICD_ENA, 0);
730 
731 	ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_4, &val);
732 	if (ret) {
733 		dev_err(arizona->dev, "Failed to read MICDET_ADCVAL: %d\n", ret);
734 		return ret;
735 	}
736 
737 	dev_dbg(arizona->dev, "MICDET_ADCVAL: %x\n", val);
738 
739 	val &= ARIZONA_MICDET_ADCVAL_MASK;
740 	if (val < ARRAY_SIZE(arizona_micd_levels))
741 		val = arizona_micd_levels[val];
742 	else
743 		val = INT_MAX;
744 
745 	if (val <= QUICK_HEADPHONE_MAX_OHM)
746 		val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_0;
747 	else if (val <= MICROPHONE_MIN_OHM)
748 		val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_1;
749 	else if (val <= MICROPHONE_MAX_OHM)
750 		val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_8;
751 	else
752 		val = ARIZONA_MICD_LVL_8;
753 
754 	return val;
755 }
756 
757 static int arizona_micd_read(struct arizona_priv *info)
758 {
759 	struct arizona *arizona = info->arizona;
760 	unsigned int val = 0;
761 	int ret, i;
762 
763 	for (i = 0; i < 10 && !(val & MICD_LVL_0_TO_8); i++) {
764 		ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val);
765 		if (ret) {
766 			dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret);
767 			return ret;
768 		}
769 
770 		dev_dbg(arizona->dev, "MICDET: %x\n", val);
771 
772 		if (!(val & ARIZONA_MICD_VALID)) {
773 			dev_warn(arizona->dev, "Microphone detection state invalid\n");
774 			return -EINVAL;
775 		}
776 	}
777 
778 	if (i == 10 && !(val & MICD_LVL_0_TO_8)) {
779 		dev_err(arizona->dev, "Failed to get valid MICDET value\n");
780 		return -EINVAL;
781 	}
782 
783 	return val;
784 }
785 
786 static int arizona_micdet_reading(void *priv)
787 {
788 	struct arizona_priv *info = priv;
789 	struct arizona *arizona = info->arizona;
790 	int ret, val;
791 
792 	if (info->detecting && arizona->pdata.micd_software_compare)
793 		ret = arizona_micd_adc_read(info);
794 	else
795 		ret = arizona_micd_read(info);
796 	if (ret < 0)
797 		return ret;
798 
799 	val = ret;
800 
801 	/* Due to jack detect this should never happen */
802 	if (!(val & ARIZONA_MICD_STS)) {
803 		dev_warn(arizona->dev, "Detected open circuit\n");
804 		info->mic = false;
805 		info->detecting = false;
806 		arizona_identify_headphone(info);
807 		return 0;
808 	}
809 
810 	/* If we got a high impedence we should have a headset, report it. */
811 	if (val & ARIZONA_MICD_LVL_8) {
812 		info->mic = true;
813 		info->detecting = false;
814 
815 		arizona_identify_headphone(info);
816 
817 		snd_soc_jack_report(info->jack, SND_JACK_MICROPHONE, SND_JACK_MICROPHONE);
818 
819 		/* Don't need to regulate for button detection */
820 		ret = regulator_allow_bypass(info->micvdd, true);
821 		if (ret)
822 			dev_err(arizona->dev, "Failed to bypass MICVDD: %d\n", ret);
823 
824 		return 0;
825 	}
826 
827 	/* If we detected a lower impedence during initial startup
828 	 * then we probably have the wrong polarity, flip it.  Don't
829 	 * do this for the lowest impedences to speed up detection of
830 	 * plain headphones.  If both polarities report a low
831 	 * impedence then give up and report headphones.
832 	 */
833 	if (val & MICD_LVL_1_TO_7) {
834 		if (info->jack_flips >= info->micd_num_modes * 10) {
835 			dev_dbg(arizona->dev, "Detected HP/line\n");
836 
837 			info->detecting = false;
838 
839 			arizona_identify_headphone(info);
840 		} else {
841 			info->micd_mode++;
842 			if (info->micd_mode == info->micd_num_modes)
843 				info->micd_mode = 0;
844 			arizona_extcon_set_mode(info, info->micd_mode);
845 
846 			info->jack_flips++;
847 
848 			if (arizona->pdata.micd_software_compare)
849 				regmap_update_bits(arizona->regmap,
850 						   ARIZONA_MIC_DETECT_1,
851 						   ARIZONA_MICD_ENA,
852 						   ARIZONA_MICD_ENA);
853 
854 			queue_delayed_work(system_power_efficient_wq,
855 					   &info->micd_timeout_work,
856 					   msecs_to_jiffies(arizona->pdata.micd_timeout));
857 		}
858 
859 		return 0;
860 	}
861 
862 	/*
863 	 * If we're still detecting and we detect a short then we've
864 	 * got a headphone.
865 	 */
866 	dev_dbg(arizona->dev, "Headphone detected\n");
867 	info->detecting = false;
868 
869 	arizona_identify_headphone(info);
870 
871 	return 0;
872 }
873 
874 static int arizona_button_reading(void *priv)
875 {
876 	struct arizona_priv *info = priv;
877 	struct arizona *arizona = info->arizona;
878 	int val, key, lvl;
879 
880 	val = arizona_micd_read(info);
881 	if (val < 0)
882 		return val;
883 
884 	/*
885 	 * If we're still detecting and we detect a short then we've
886 	 * got a headphone.  Otherwise it's a button press.
887 	 */
888 	if (val & MICD_LVL_0_TO_7) {
889 		if (info->mic) {
890 			dev_dbg(arizona->dev, "Mic button detected\n");
891 
892 			lvl = val & ARIZONA_MICD_LVL_MASK;
893 			lvl >>= ARIZONA_MICD_LVL_SHIFT;
894 
895 			if (lvl && ffs(lvl) - 1 < info->num_micd_ranges) {
896 				key = ffs(lvl) - 1;
897 				snd_soc_jack_report(info->jack,
898 						    SND_JACK_BTN_0 >> key,
899 						    info->micd_button_mask);
900 			} else {
901 				dev_err(arizona->dev, "Button out of range\n");
902 			}
903 		} else {
904 			dev_warn(arizona->dev, "Button with no mic: %x\n", val);
905 		}
906 	} else {
907 		dev_dbg(arizona->dev, "Mic button released\n");
908 		snd_soc_jack_report(info->jack, 0, info->micd_button_mask);
909 		arizona_extcon_pulse_micbias(info);
910 	}
911 
912 	return 0;
913 }
914 
915 static void arizona_micd_detect(struct work_struct *work)
916 {
917 	struct arizona_priv *info = container_of(work,
918 						struct arizona_priv,
919 						micd_detect_work.work);
920 	struct arizona *arizona = info->arizona;
921 
922 	cancel_delayed_work_sync(&info->micd_timeout_work);
923 
924 	mutex_lock(&info->lock);
925 
926 	/* If the cable was removed while measuring ignore the result */
927 	if (!(info->jack->status & SND_JACK_MECHANICAL)) {
928 		dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n");
929 		mutex_unlock(&info->lock);
930 		return;
931 	}
932 
933 	if (info->detecting)
934 		arizona_micdet_reading(info);
935 	else
936 		arizona_button_reading(info);
937 
938 	pm_runtime_mark_last_busy(arizona->dev);
939 	mutex_unlock(&info->lock);
940 }
941 
942 static irqreturn_t arizona_micdet(int irq, void *data)
943 {
944 	struct arizona_priv *info = data;
945 	struct arizona *arizona = info->arizona;
946 	int debounce = arizona->pdata.micd_detect_debounce;
947 
948 	cancel_delayed_work_sync(&info->micd_detect_work);
949 	cancel_delayed_work_sync(&info->micd_timeout_work);
950 
951 	mutex_lock(&info->lock);
952 	if (!info->detecting)
953 		debounce = 0;
954 	mutex_unlock(&info->lock);
955 
956 	if (debounce)
957 		queue_delayed_work(system_power_efficient_wq,
958 				   &info->micd_detect_work,
959 				   msecs_to_jiffies(debounce));
960 	else
961 		arizona_micd_detect(&info->micd_detect_work.work);
962 
963 	return IRQ_HANDLED;
964 }
965 
966 static void arizona_hpdet_work(struct work_struct *work)
967 {
968 	struct arizona_priv *info = container_of(work,
969 						struct arizona_priv,
970 						hpdet_work.work);
971 
972 	mutex_lock(&info->lock);
973 	arizona_start_hpdet_acc_id(info);
974 	mutex_unlock(&info->lock);
975 }
976 
977 static int arizona_hpdet_wait(struct arizona_priv *info)
978 {
979 	struct arizona *arizona = info->arizona;
980 	unsigned int val;
981 	int i, ret;
982 
983 	for (i = 0; i < ARIZONA_HPDET_WAIT_COUNT; i++) {
984 		ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2,
985 				&val);
986 		if (ret) {
987 			dev_err(arizona->dev, "Failed to read HPDET state: %d\n", ret);
988 			return ret;
989 		}
990 
991 		switch (info->hpdet_ip_version) {
992 		case 0:
993 			if (val & ARIZONA_HP_DONE)
994 				return 0;
995 			break;
996 		default:
997 			if (val & ARIZONA_HP_DONE_B)
998 				return 0;
999 			break;
1000 		}
1001 
1002 		msleep(ARIZONA_HPDET_WAIT_DELAY_MS);
1003 	}
1004 
1005 	dev_warn(arizona->dev, "HPDET did not appear to complete\n");
1006 
1007 	return -ETIMEDOUT;
1008 }
1009 
1010 static irqreturn_t arizona_jackdet(int irq, void *data)
1011 {
1012 	struct arizona_priv *info = data;
1013 	struct arizona *arizona = info->arizona;
1014 	unsigned int val, present, mask;
1015 	bool cancelled_hp, cancelled_mic;
1016 	int ret, i;
1017 
1018 	cancelled_hp = cancel_delayed_work_sync(&info->hpdet_work);
1019 	cancelled_mic = cancel_delayed_work_sync(&info->micd_timeout_work);
1020 
1021 	pm_runtime_get_sync(arizona->dev);
1022 
1023 	mutex_lock(&info->lock);
1024 
1025 	if (info->micd_clamp) {
1026 		mask = ARIZONA_MICD_CLAMP_STS;
1027 		present = 0;
1028 	} else {
1029 		mask = ARIZONA_JD1_STS;
1030 		if (arizona->pdata.jd_invert)
1031 			present = 0;
1032 		else
1033 			present = ARIZONA_JD1_STS;
1034 	}
1035 
1036 	ret = regmap_read(arizona->regmap, ARIZONA_AOD_IRQ_RAW_STATUS, &val);
1037 	if (ret) {
1038 		dev_err(arizona->dev, "Failed to read jackdet status: %d\n", ret);
1039 		mutex_unlock(&info->lock);
1040 		pm_runtime_put_autosuspend(arizona->dev);
1041 		return IRQ_NONE;
1042 	}
1043 
1044 	val &= mask;
1045 	if (val == info->last_jackdet) {
1046 		dev_dbg(arizona->dev, "Suppressing duplicate JACKDET\n");
1047 		if (cancelled_hp)
1048 			queue_delayed_work(system_power_efficient_wq,
1049 					   &info->hpdet_work,
1050 					   msecs_to_jiffies(HPDET_DEBOUNCE));
1051 
1052 		if (cancelled_mic) {
1053 			int micd_timeout = arizona->pdata.micd_timeout;
1054 
1055 			queue_delayed_work(system_power_efficient_wq,
1056 					   &info->micd_timeout_work,
1057 					   msecs_to_jiffies(micd_timeout));
1058 		}
1059 
1060 		goto out;
1061 	}
1062 	info->last_jackdet = val;
1063 
1064 	if (info->last_jackdet == present) {
1065 		dev_dbg(arizona->dev, "Detected jack\n");
1066 		snd_soc_jack_report(info->jack, SND_JACK_MECHANICAL, SND_JACK_MECHANICAL);
1067 
1068 		info->detecting = true;
1069 		info->mic = false;
1070 		info->jack_flips = 0;
1071 
1072 		if (!arizona->pdata.hpdet_acc_id) {
1073 			arizona_start_mic(info);
1074 		} else {
1075 			queue_delayed_work(system_power_efficient_wq,
1076 					   &info->hpdet_work,
1077 					   msecs_to_jiffies(HPDET_DEBOUNCE));
1078 		}
1079 
1080 		if (info->micd_clamp || !arizona->pdata.jd_invert)
1081 			regmap_update_bits(arizona->regmap,
1082 					   ARIZONA_JACK_DETECT_DEBOUNCE,
1083 					   ARIZONA_MICD_CLAMP_DB |
1084 					   ARIZONA_JD1_DB, 0);
1085 	} else {
1086 		dev_dbg(arizona->dev, "Detected jack removal\n");
1087 
1088 		arizona_stop_mic(info);
1089 
1090 		info->num_hpdet_res = 0;
1091 		for (i = 0; i < ARRAY_SIZE(info->hpdet_res); i++)
1092 			info->hpdet_res[i] = 0;
1093 		info->mic = false;
1094 		info->hpdet_done = false;
1095 		info->hpdet_retried = false;
1096 
1097 		snd_soc_jack_report(info->jack, 0, ARIZONA_JACK_MASK | info->micd_button_mask);
1098 
1099 		/*
1100 		 * If the jack was removed during a headphone detection we
1101 		 * need to wait for the headphone detection to finish, as
1102 		 * it can not be aborted. We don't want to be able to start
1103 		 * a new headphone detection from a fresh insert until this
1104 		 * one is finished.
1105 		 */
1106 		arizona_hpdet_wait(info);
1107 
1108 		regmap_update_bits(arizona->regmap,
1109 				   ARIZONA_JACK_DETECT_DEBOUNCE,
1110 				   ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
1111 				   ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB);
1112 	}
1113 
1114 out:
1115 	/* Clear trig_sts to make sure DCVDD is not forced up */
1116 	regmap_write(arizona->regmap, ARIZONA_AOD_WKUP_AND_TRIG,
1117 		     ARIZONA_MICD_CLAMP_FALL_TRIG_STS |
1118 		     ARIZONA_MICD_CLAMP_RISE_TRIG_STS |
1119 		     ARIZONA_JD1_FALL_TRIG_STS |
1120 		     ARIZONA_JD1_RISE_TRIG_STS);
1121 
1122 	mutex_unlock(&info->lock);
1123 
1124 	pm_runtime_put_autosuspend(arizona->dev);
1125 
1126 	return IRQ_HANDLED;
1127 }
1128 
1129 /* Map a level onto a slot in the register bank */
1130 static void arizona_micd_set_level(struct arizona *arizona, int index,
1131 				   unsigned int level)
1132 {
1133 	int reg;
1134 	unsigned int mask;
1135 
1136 	reg = ARIZONA_MIC_DETECT_LEVEL_4 - (index / 2);
1137 
1138 	if (!(index % 2)) {
1139 		mask = 0x3f00;
1140 		level <<= 8;
1141 	} else {
1142 		mask = 0x3f;
1143 	}
1144 
1145 	/* Program the level itself */
1146 	regmap_update_bits(arizona->regmap, reg, mask, level);
1147 }
1148 
1149 static int arizona_extcon_get_micd_configs(struct device *dev,
1150 					   struct arizona *arizona)
1151 {
1152 	const char * const prop = "wlf,micd-configs";
1153 	const int entries_per_config = 3;
1154 	struct arizona_micd_config *micd_configs;
1155 	int nconfs, ret;
1156 	int i, j;
1157 	u32 *vals;
1158 
1159 	nconfs = device_property_count_u32(arizona->dev, prop);
1160 	if (nconfs <= 0)
1161 		return 0;
1162 
1163 	vals = kcalloc(nconfs, sizeof(u32), GFP_KERNEL);
1164 	if (!vals)
1165 		return -ENOMEM;
1166 
1167 	ret = device_property_read_u32_array(arizona->dev, prop, vals, nconfs);
1168 	if (ret < 0)
1169 		goto out;
1170 
1171 	nconfs /= entries_per_config;
1172 	micd_configs = devm_kcalloc(dev, nconfs, sizeof(*micd_configs),
1173 				    GFP_KERNEL);
1174 	if (!micd_configs) {
1175 		ret = -ENOMEM;
1176 		goto out;
1177 	}
1178 
1179 	for (i = 0, j = 0; i < nconfs; ++i) {
1180 		micd_configs[i].src = vals[j++] ? ARIZONA_ACCDET_SRC : 0;
1181 		micd_configs[i].bias = vals[j++];
1182 		micd_configs[i].gpio = vals[j++];
1183 	}
1184 
1185 	arizona->pdata.micd_configs = micd_configs;
1186 	arizona->pdata.num_micd_configs = nconfs;
1187 
1188 out:
1189 	kfree(vals);
1190 	return ret;
1191 }
1192 
1193 static int arizona_extcon_device_get_pdata(struct device *dev,
1194 					   struct arizona *arizona)
1195 {
1196 	struct arizona_pdata *pdata = &arizona->pdata;
1197 	unsigned int val = ARIZONA_ACCDET_MODE_HPL;
1198 	int ret;
1199 
1200 	device_property_read_u32(arizona->dev, "wlf,hpdet-channel", &val);
1201 	switch (val) {
1202 	case ARIZONA_ACCDET_MODE_HPL:
1203 	case ARIZONA_ACCDET_MODE_HPR:
1204 		pdata->hpdet_channel = val;
1205 		break;
1206 	default:
1207 		dev_err(arizona->dev, "Wrong wlf,hpdet-channel DT value %d\n", val);
1208 		pdata->hpdet_channel = ARIZONA_ACCDET_MODE_HPL;
1209 	}
1210 
1211 	device_property_read_u32(arizona->dev, "wlf,micd-detect-debounce",
1212 				 &pdata->micd_detect_debounce);
1213 
1214 	device_property_read_u32(arizona->dev, "wlf,micd-bias-start-time",
1215 				 &pdata->micd_bias_start_time);
1216 
1217 	device_property_read_u32(arizona->dev, "wlf,micd-rate",
1218 				 &pdata->micd_rate);
1219 
1220 	device_property_read_u32(arizona->dev, "wlf,micd-dbtime",
1221 				 &pdata->micd_dbtime);
1222 
1223 	device_property_read_u32(arizona->dev, "wlf,micd-timeout-ms",
1224 				 &pdata->micd_timeout);
1225 
1226 	pdata->micd_force_micbias = device_property_read_bool(arizona->dev,
1227 						"wlf,micd-force-micbias");
1228 
1229 	pdata->micd_software_compare = device_property_read_bool(arizona->dev,
1230 						"wlf,micd-software-compare");
1231 
1232 	pdata->jd_invert = device_property_read_bool(arizona->dev,
1233 						     "wlf,jd-invert");
1234 
1235 	device_property_read_u32(arizona->dev, "wlf,gpsw", &pdata->gpsw);
1236 
1237 	pdata->jd_gpio5 = device_property_read_bool(arizona->dev,
1238 						    "wlf,use-jd2");
1239 	pdata->jd_gpio5_nopull = device_property_read_bool(arizona->dev,
1240 						"wlf,use-jd2-nopull");
1241 
1242 	ret = arizona_extcon_get_micd_configs(dev, arizona);
1243 	if (ret < 0)
1244 		dev_err(arizona->dev, "Failed to read micd configs: %d\n", ret);
1245 
1246 	return 0;
1247 }
1248 
1249 int arizona_jack_codec_dev_probe(struct arizona_priv *info, struct device *dev)
1250 {
1251 	struct arizona *arizona = info->arizona;
1252 	struct arizona_pdata *pdata = &arizona->pdata;
1253 	int ret, mode;
1254 
1255 	if (!dev_get_platdata(arizona->dev))
1256 		arizona_extcon_device_get_pdata(dev, arizona);
1257 
1258 	info->micvdd = devm_regulator_get(dev, "MICVDD");
1259 	if (IS_ERR(info->micvdd))
1260 		return dev_err_probe(arizona->dev, PTR_ERR(info->micvdd), "getting MICVDD\n");
1261 
1262 	mutex_init(&info->lock);
1263 	info->last_jackdet = ~(ARIZONA_MICD_CLAMP_STS | ARIZONA_JD1_STS);
1264 	INIT_DELAYED_WORK(&info->hpdet_work, arizona_hpdet_work);
1265 	INIT_DELAYED_WORK(&info->micd_detect_work, arizona_micd_detect);
1266 	INIT_DELAYED_WORK(&info->micd_timeout_work, arizona_micd_timeout_work);
1267 
1268 	switch (arizona->type) {
1269 	case WM5102:
1270 		switch (arizona->rev) {
1271 		case 0:
1272 			info->micd_reva = true;
1273 			break;
1274 		default:
1275 			info->micd_clamp = true;
1276 			info->hpdet_ip_version = 1;
1277 			break;
1278 		}
1279 		break;
1280 	case WM5110:
1281 	case WM8280:
1282 		switch (arizona->rev) {
1283 		case 0 ... 2:
1284 			break;
1285 		default:
1286 			info->micd_clamp = true;
1287 			info->hpdet_ip_version = 2;
1288 			break;
1289 		}
1290 		break;
1291 	case WM8998:
1292 	case WM1814:
1293 		info->micd_clamp = true;
1294 		info->hpdet_ip_version = 2;
1295 		break;
1296 	default:
1297 		break;
1298 	}
1299 
1300 	if (!pdata->micd_timeout)
1301 		pdata->micd_timeout = DEFAULT_MICD_TIMEOUT;
1302 
1303 	if (pdata->num_micd_configs) {
1304 		info->micd_modes = pdata->micd_configs;
1305 		info->micd_num_modes = pdata->num_micd_configs;
1306 	} else {
1307 		info->micd_modes = micd_default_modes;
1308 		info->micd_num_modes = ARRAY_SIZE(micd_default_modes);
1309 	}
1310 
1311 	if (arizona->pdata.gpsw > 0)
1312 		regmap_update_bits(arizona->regmap, ARIZONA_GP_SWITCH_1,
1313 				ARIZONA_SW1_MODE_MASK, arizona->pdata.gpsw);
1314 
1315 	if (info->micd_modes[0].gpio)
1316 		mode = GPIOD_OUT_HIGH;
1317 	else
1318 		mode = GPIOD_OUT_LOW;
1319 
1320 	/* We can't use devm here because we need to do the get
1321 	 * against the MFD device, as that is where the of_node
1322 	 * will reside, but if we devm against that the GPIO
1323 	 * will not be freed if the extcon driver is unloaded.
1324 	 */
1325 	info->micd_pol_gpio = gpiod_get_optional(arizona->dev,
1326 						 "wlf,micd-pol",
1327 						 mode);
1328 	if (IS_ERR(info->micd_pol_gpio)) {
1329 		ret = PTR_ERR(info->micd_pol_gpio);
1330 		dev_err_probe(arizona->dev, ret, "getting microphone polarity GPIO\n");
1331 		return ret;
1332 	}
1333 
1334 	info->hpdet_id_gpio = gpiod_get_optional(arizona->dev,
1335 						 "wlf,hpdet-id-gpio",
1336 						 mode);
1337 	if (IS_ERR(info->hpdet_id_gpio)) {
1338 		ret = PTR_ERR(info->hpdet_id_gpio);
1339 		dev_err_probe(arizona->dev, ret, "getting headphone detect ID GPIO\n");
1340 		return ret;
1341 	}
1342 
1343 	return 0;
1344 }
1345 EXPORT_SYMBOL_GPL(arizona_jack_codec_dev_probe);
1346 
1347 int arizona_jack_codec_dev_remove(struct arizona_priv *info)
1348 {
1349 	gpiod_put(info->micd_pol_gpio);
1350 	gpiod_put(info->hpdet_id_gpio);
1351 	return 0;
1352 }
1353 EXPORT_SYMBOL_GPL(arizona_jack_codec_dev_remove);
1354 
1355 static int arizona_jack_enable_jack_detect(struct arizona_priv *info,
1356 					   struct snd_soc_jack *jack)
1357 {
1358 	struct arizona *arizona = info->arizona;
1359 	struct arizona_pdata *pdata = &arizona->pdata;
1360 	unsigned int val;
1361 	unsigned int clamp_mode;
1362 	int jack_irq_fall, jack_irq_rise;
1363 	int ret, i, j;
1364 
1365 	if (arizona->pdata.micd_bias_start_time)
1366 		regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1367 				   ARIZONA_MICD_BIAS_STARTTIME_MASK,
1368 				   arizona->pdata.micd_bias_start_time
1369 				   << ARIZONA_MICD_BIAS_STARTTIME_SHIFT);
1370 
1371 	if (arizona->pdata.micd_rate)
1372 		regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1373 				   ARIZONA_MICD_RATE_MASK,
1374 				   arizona->pdata.micd_rate
1375 				   << ARIZONA_MICD_RATE_SHIFT);
1376 
1377 	switch (arizona->pdata.micd_dbtime) {
1378 	case MICD_DBTIME_FOUR_READINGS:
1379 		regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1380 				   ARIZONA_MICD_DBTIME_MASK,
1381 				   ARIZONA_MICD_DBTIME);
1382 		break;
1383 	case MICD_DBTIME_TWO_READINGS:
1384 		regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1,
1385 				   ARIZONA_MICD_DBTIME_MASK, 0);
1386 		break;
1387 	default:
1388 		break;
1389 	}
1390 
1391 	BUILD_BUG_ON(ARRAY_SIZE(arizona_micd_levels) <
1392 		     ARIZONA_NUM_MICD_BUTTON_LEVELS);
1393 
1394 	if (arizona->pdata.num_micd_ranges) {
1395 		info->micd_ranges = pdata->micd_ranges;
1396 		info->num_micd_ranges = pdata->num_micd_ranges;
1397 	} else {
1398 		info->micd_ranges = micd_default_ranges;
1399 		info->num_micd_ranges = ARRAY_SIZE(micd_default_ranges);
1400 	}
1401 
1402 	if (arizona->pdata.num_micd_ranges > ARIZONA_MAX_MICD_BUTTONS) {
1403 		dev_err(arizona->dev, "Too many MICD ranges: %d > %d\n",
1404 			arizona->pdata.num_micd_ranges, ARIZONA_MAX_MICD_BUTTONS);
1405 		return -EINVAL;
1406 	}
1407 
1408 	if (info->num_micd_ranges > 1) {
1409 		for (i = 1; i < info->num_micd_ranges; i++) {
1410 			if (info->micd_ranges[i - 1].max >
1411 			    info->micd_ranges[i].max) {
1412 				dev_err(arizona->dev, "MICD ranges must be sorted\n");
1413 				return -EINVAL;
1414 			}
1415 		}
1416 	}
1417 
1418 	/* Disable all buttons by default */
1419 	regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1420 			   ARIZONA_MICD_LVL_SEL_MASK, 0x81);
1421 
1422 	/* Set up all the buttons the user specified */
1423 	for (i = 0; i < info->num_micd_ranges; i++) {
1424 		for (j = 0; j < ARIZONA_NUM_MICD_BUTTON_LEVELS; j++)
1425 			if (arizona_micd_levels[j] >= info->micd_ranges[i].max)
1426 				break;
1427 
1428 		if (j == ARIZONA_NUM_MICD_BUTTON_LEVELS) {
1429 			dev_err(arizona->dev, "Unsupported MICD level %d\n",
1430 				info->micd_ranges[i].max);
1431 			return -EINVAL;
1432 		}
1433 
1434 		dev_dbg(arizona->dev, "%d ohms for MICD threshold %d\n",
1435 			arizona_micd_levels[j], i);
1436 
1437 		arizona_micd_set_level(arizona, i, j);
1438 
1439 		/* SND_JACK_BTN_# masks start with the most significant bit */
1440 		info->micd_button_mask |= SND_JACK_BTN_0 >> i;
1441 		snd_jack_set_key(jack->jack, SND_JACK_BTN_0 >> i,
1442 				 info->micd_ranges[i].key);
1443 
1444 		/* Enable reporting of that range */
1445 		regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_2,
1446 				   1 << i, 1 << i);
1447 	}
1448 
1449 	/* Set all the remaining keys to a maximum */
1450 	for (; i < ARIZONA_MAX_MICD_RANGE; i++)
1451 		arizona_micd_set_level(arizona, i, 0x3f);
1452 
1453 	/*
1454 	 * If we have a clamp use it, activating in conjunction with
1455 	 * GPIO5 if that is connected for jack detect operation.
1456 	 */
1457 	if (info->micd_clamp) {
1458 		if (arizona->pdata.jd_gpio5) {
1459 			/* Put the GPIO into input mode with optional pull */
1460 			val = 0xc101;
1461 			if (arizona->pdata.jd_gpio5_nopull)
1462 				val &= ~ARIZONA_GPN_PU;
1463 
1464 			regmap_write(arizona->regmap, ARIZONA_GPIO5_CTRL,
1465 				     val);
1466 
1467 			if (arizona->pdata.jd_invert)
1468 				clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH_GP5H;
1469 			else
1470 				clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL_GP5H;
1471 		} else {
1472 			if (arizona->pdata.jd_invert)
1473 				clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDH;
1474 			else
1475 				clamp_mode = ARIZONA_MICD_CLAMP_MODE_JDL;
1476 		}
1477 
1478 		regmap_update_bits(arizona->regmap,
1479 				   ARIZONA_MICD_CLAMP_CONTROL,
1480 				   ARIZONA_MICD_CLAMP_MODE_MASK, clamp_mode);
1481 
1482 		regmap_update_bits(arizona->regmap,
1483 				   ARIZONA_JACK_DETECT_DEBOUNCE,
1484 				   ARIZONA_MICD_CLAMP_DB,
1485 				   ARIZONA_MICD_CLAMP_DB);
1486 	}
1487 
1488 	arizona_extcon_set_mode(info, 0);
1489 
1490 	info->jack = jack;
1491 
1492 	pm_runtime_get_sync(arizona->dev);
1493 
1494 	if (info->micd_clamp) {
1495 		jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1496 		jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1497 	} else {
1498 		jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1499 		jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1500 	}
1501 
1502 	ret = arizona_request_irq(arizona, jack_irq_rise,
1503 				  "JACKDET rise", arizona_jackdet, info);
1504 	if (ret != 0) {
1505 		dev_err(arizona->dev, "Failed to get JACKDET rise IRQ: %d\n", ret);
1506 		goto err_pm;
1507 	}
1508 
1509 	ret = arizona_set_irq_wake(arizona, jack_irq_rise, 1);
1510 	if (ret != 0) {
1511 		dev_err(arizona->dev, "Failed to set JD rise IRQ wake: %d\n", ret);
1512 		goto err_rise;
1513 	}
1514 
1515 	ret = arizona_request_irq(arizona, jack_irq_fall,
1516 				  "JACKDET fall", arizona_jackdet, info);
1517 	if (ret != 0) {
1518 		dev_err(arizona->dev, "Failed to get JD fall IRQ: %d\n", ret);
1519 		goto err_rise_wake;
1520 	}
1521 
1522 	ret = arizona_set_irq_wake(arizona, jack_irq_fall, 1);
1523 	if (ret != 0) {
1524 		dev_err(arizona->dev, "Failed to set JD fall IRQ wake: %d\n", ret);
1525 		goto err_fall;
1526 	}
1527 
1528 	ret = arizona_request_irq(arizona, ARIZONA_IRQ_MICDET,
1529 				  "MICDET", arizona_micdet, info);
1530 	if (ret != 0) {
1531 		dev_err(arizona->dev, "Failed to get MICDET IRQ: %d\n", ret);
1532 		goto err_fall_wake;
1533 	}
1534 
1535 	ret = arizona_request_irq(arizona, ARIZONA_IRQ_HPDET,
1536 				  "HPDET", arizona_hpdet_irq, info);
1537 	if (ret != 0) {
1538 		dev_err(arizona->dev, "Failed to get HPDET IRQ: %d\n", ret);
1539 		goto err_micdet;
1540 	}
1541 
1542 	arizona_clk32k_enable(arizona);
1543 	regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_DEBOUNCE,
1544 			   ARIZONA_JD1_DB, ARIZONA_JD1_DB);
1545 	regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1546 			   ARIZONA_JD1_ENA, ARIZONA_JD1_ENA);
1547 
1548 	ret = regulator_allow_bypass(info->micvdd, true);
1549 	if (ret != 0)
1550 		dev_warn(arizona->dev, "Failed to set MICVDD to bypass: %d\n", ret);
1551 
1552 	pm_runtime_put(arizona->dev);
1553 
1554 	return 0;
1555 
1556 err_micdet:
1557 	arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1558 err_fall_wake:
1559 	arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1560 err_fall:
1561 	arizona_free_irq(arizona, jack_irq_fall, info);
1562 err_rise_wake:
1563 	arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1564 err_rise:
1565 	arizona_free_irq(arizona, jack_irq_rise, info);
1566 err_pm:
1567 	pm_runtime_put(arizona->dev);
1568 	info->jack = NULL;
1569 	return ret;
1570 }
1571 
1572 static int arizona_jack_disable_jack_detect(struct arizona_priv *info)
1573 {
1574 	struct arizona *arizona = info->arizona;
1575 	int jack_irq_rise, jack_irq_fall;
1576 	bool change;
1577 	int ret;
1578 
1579 	if (!info->jack)
1580 		return 0;
1581 
1582 	if (info->micd_clamp) {
1583 		jack_irq_rise = ARIZONA_IRQ_MICD_CLAMP_RISE;
1584 		jack_irq_fall = ARIZONA_IRQ_MICD_CLAMP_FALL;
1585 	} else {
1586 		jack_irq_rise = ARIZONA_IRQ_JD_RISE;
1587 		jack_irq_fall = ARIZONA_IRQ_JD_FALL;
1588 	}
1589 
1590 	arizona_set_irq_wake(arizona, jack_irq_rise, 0);
1591 	arizona_set_irq_wake(arizona, jack_irq_fall, 0);
1592 	arizona_free_irq(arizona, ARIZONA_IRQ_HPDET, info);
1593 	arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
1594 	arizona_free_irq(arizona, jack_irq_rise, info);
1595 	arizona_free_irq(arizona, jack_irq_fall, info);
1596 	cancel_delayed_work_sync(&info->hpdet_work);
1597 	cancel_delayed_work_sync(&info->micd_detect_work);
1598 	cancel_delayed_work_sync(&info->micd_timeout_work);
1599 
1600 	ret = regmap_update_bits_check(arizona->regmap, ARIZONA_MIC_DETECT_1,
1601 				       ARIZONA_MICD_ENA, 0,
1602 				       &change);
1603 	if (ret < 0) {
1604 		dev_err(arizona->dev, "Failed to disable micd on remove: %d\n", ret);
1605 	} else if (change) {
1606 		regulator_disable(info->micvdd);
1607 		pm_runtime_put(arizona->dev);
1608 	}
1609 
1610 	regmap_update_bits(arizona->regmap,
1611 			   ARIZONA_MICD_CLAMP_CONTROL,
1612 			   ARIZONA_MICD_CLAMP_MODE_MASK, 0);
1613 	regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
1614 			   ARIZONA_JD1_ENA, 0);
1615 	arizona_clk32k_disable(arizona);
1616 	info->jack = NULL;
1617 
1618 	return 0;
1619 }
1620 
1621 int arizona_jack_set_jack(struct snd_soc_component *component,
1622 			  struct snd_soc_jack *jack, void *data)
1623 {
1624 	struct arizona_priv *info = snd_soc_component_get_drvdata(component);
1625 
1626 	if (jack)
1627 		return arizona_jack_enable_jack_detect(info, jack);
1628 	else
1629 		return arizona_jack_disable_jack_detect(info);
1630 }
1631 EXPORT_SYMBOL_GPL(arizona_jack_set_jack);
1632