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