xref: /openbmc/linux/sound/soc/codecs/da7219-aad.c (revision 806eb9e4)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * da7219-aad.c - Dialog DA7219 ALSA SoC AAD Driver
4  *
5  * Copyright (c) 2015 Dialog Semiconductor Ltd.
6  *
7  * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/clk.h>
13 #include <linux/i2c.h>
14 #include <linux/property.h>
15 #include <linux/pm_wakeirq.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/workqueue.h>
19 #include <sound/soc.h>
20 #include <sound/jack.h>
21 #include <sound/da7219.h>
22 
23 #include "da7219.h"
24 #include "da7219-aad.h"
25 
26 
27 /*
28  * Detection control
29  */
30 
31 void da7219_aad_jack_det(struct snd_soc_component *component, struct snd_soc_jack *jack)
32 {
33 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
34 
35 	da7219->aad->jack = jack;
36 	da7219->aad->jack_inserted = false;
37 
38 	/* Send an initial empty report */
39 	snd_soc_jack_report(jack, 0, DA7219_AAD_REPORT_ALL_MASK);
40 
41 	/* Enable/Disable jack detection */
42 	snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1,
43 			    DA7219_ACCDET_EN_MASK,
44 			    (jack ? DA7219_ACCDET_EN_MASK : 0));
45 }
46 
47 /*
48  * Button/HPTest work
49  */
50 
51 static void da7219_aad_btn_det_work(struct work_struct *work)
52 {
53 	struct da7219_aad_priv *da7219_aad =
54 		container_of(work, struct da7219_aad_priv, btn_det_work);
55 	struct snd_soc_component *component = da7219_aad->component;
56 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
57 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
58 	u8 statusa, micbias_ctrl;
59 	bool micbias_up = false;
60 	int retries = 0;
61 
62 	/* Disable ground switch */
63 	snd_soc_component_update_bits(component, 0xFB, 0x01, 0x00);
64 
65 	/* Drive headphones/lineout */
66 	snd_soc_component_update_bits(component, DA7219_HP_L_CTRL,
67 			    DA7219_HP_L_AMP_OE_MASK,
68 			    DA7219_HP_L_AMP_OE_MASK);
69 	snd_soc_component_update_bits(component, DA7219_HP_R_CTRL,
70 			    DA7219_HP_R_AMP_OE_MASK,
71 			    DA7219_HP_R_AMP_OE_MASK);
72 
73 	/* Make sure mic bias is up */
74 	snd_soc_dapm_force_enable_pin(dapm, "Mic Bias");
75 	snd_soc_dapm_sync(dapm);
76 
77 	do {
78 		statusa = snd_soc_component_read(component, DA7219_ACCDET_STATUS_A);
79 		if (statusa & DA7219_MICBIAS_UP_STS_MASK)
80 			micbias_up = true;
81 		else if (retries++ < DA7219_AAD_MICBIAS_CHK_RETRIES)
82 			msleep(DA7219_AAD_MICBIAS_CHK_DELAY);
83 	} while ((!micbias_up) && (retries < DA7219_AAD_MICBIAS_CHK_RETRIES));
84 
85 	if (retries >= DA7219_AAD_MICBIAS_CHK_RETRIES)
86 		dev_warn(component->dev, "Mic bias status check timed out");
87 
88 	da7219->micbias_on_event = true;
89 
90 	/*
91 	 * Mic bias pulse required to enable mic, must be done before enabling
92 	 * button detection to prevent erroneous button readings.
93 	 */
94 	if (da7219_aad->micbias_pulse_lvl && da7219_aad->micbias_pulse_time) {
95 		/* Pulse higher level voltage */
96 		micbias_ctrl = snd_soc_component_read(component, DA7219_MICBIAS_CTRL);
97 		snd_soc_component_update_bits(component, DA7219_MICBIAS_CTRL,
98 				    DA7219_MICBIAS1_LEVEL_MASK,
99 				    da7219_aad->micbias_pulse_lvl);
100 		msleep(da7219_aad->micbias_pulse_time);
101 		snd_soc_component_write(component, DA7219_MICBIAS_CTRL, micbias_ctrl);
102 
103 	}
104 
105 	snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1,
106 			    DA7219_BUTTON_CONFIG_MASK,
107 			    da7219_aad->btn_cfg);
108 }
109 
110 static void da7219_aad_hptest_work(struct work_struct *work)
111 {
112 	struct da7219_aad_priv *da7219_aad =
113 		container_of(work, struct da7219_aad_priv, hptest_work);
114 	struct snd_soc_component *component = da7219_aad->component;
115 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
116 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
117 
118 	__le16 tonegen_freq_hptest;
119 	u8 pll_srm_sts, pll_ctrl, gain_ramp_ctrl, accdet_cfg8;
120 	int report = 0, ret;
121 
122 	/* Lock DAPM, Kcontrols affected by this test and the PLL */
123 	snd_soc_dapm_mutex_lock(dapm);
124 	mutex_lock(&da7219->ctrl_lock);
125 	mutex_lock(&da7219->pll_lock);
126 
127 	/* Ensure MCLK is available for HP test procedure */
128 	if (da7219->mclk) {
129 		ret = clk_prepare_enable(da7219->mclk);
130 		if (ret) {
131 			dev_err(component->dev, "Failed to enable mclk - %d\n", ret);
132 			mutex_unlock(&da7219->pll_lock);
133 			mutex_unlock(&da7219->ctrl_lock);
134 			snd_soc_dapm_mutex_unlock(dapm);
135 			return;
136 		}
137 	}
138 
139 	/*
140 	 * If MCLK not present, then we're using the internal oscillator and
141 	 * require different frequency settings to achieve the same result.
142 	 *
143 	 * If MCLK is present, but PLL is not enabled then we enable it here to
144 	 * ensure a consistent detection procedure.
145 	 */
146 	pll_srm_sts = snd_soc_component_read(component, DA7219_PLL_SRM_STS);
147 	if (pll_srm_sts & DA7219_PLL_SRM_STS_MCLK) {
148 		tonegen_freq_hptest = cpu_to_le16(DA7219_AAD_HPTEST_RAMP_FREQ);
149 
150 		pll_ctrl = snd_soc_component_read(component, DA7219_PLL_CTRL);
151 		if ((pll_ctrl & DA7219_PLL_MODE_MASK) == DA7219_PLL_MODE_BYPASS)
152 			da7219_set_pll(component, DA7219_SYSCLK_PLL,
153 				       DA7219_PLL_FREQ_OUT_98304);
154 	} else {
155 		tonegen_freq_hptest = cpu_to_le16(DA7219_AAD_HPTEST_RAMP_FREQ_INT_OSC);
156 	}
157 
158 	/* Disable ground switch */
159 	snd_soc_component_update_bits(component, 0xFB, 0x01, 0x00);
160 
161 	/* Ensure gain ramping at fastest rate */
162 	gain_ramp_ctrl = snd_soc_component_read(component, DA7219_GAIN_RAMP_CTRL);
163 	snd_soc_component_write(component, DA7219_GAIN_RAMP_CTRL, DA7219_GAIN_RAMP_RATE_X8);
164 
165 	/* Bypass cache so it saves current settings */
166 	regcache_cache_bypass(da7219->regmap, true);
167 
168 	/* Make sure Tone Generator is disabled */
169 	snd_soc_component_write(component, DA7219_TONE_GEN_CFG1, 0);
170 
171 	/* Enable HPTest block, 1KOhms check */
172 	snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_8,
173 			    DA7219_HPTEST_EN_MASK | DA7219_HPTEST_RES_SEL_MASK,
174 			    DA7219_HPTEST_EN_MASK |
175 			    DA7219_HPTEST_RES_SEL_1KOHMS);
176 
177 	/* Set gains to 0db */
178 	snd_soc_component_write(component, DA7219_DAC_L_GAIN, DA7219_DAC_DIGITAL_GAIN_0DB);
179 	snd_soc_component_write(component, DA7219_DAC_R_GAIN, DA7219_DAC_DIGITAL_GAIN_0DB);
180 	snd_soc_component_write(component, DA7219_HP_L_GAIN, DA7219_HP_AMP_GAIN_0DB);
181 	snd_soc_component_write(component, DA7219_HP_R_GAIN, DA7219_HP_AMP_GAIN_0DB);
182 
183 	/* Disable DAC filters, EQs and soft mute */
184 	snd_soc_component_update_bits(component, DA7219_DAC_FILTERS1, DA7219_HPF_MODE_MASK,
185 			    0);
186 	snd_soc_component_update_bits(component, DA7219_DAC_FILTERS4, DA7219_DAC_EQ_EN_MASK,
187 			    0);
188 	snd_soc_component_update_bits(component, DA7219_DAC_FILTERS5,
189 			    DA7219_DAC_SOFTMUTE_EN_MASK, 0);
190 
191 	/* Enable HP left & right paths */
192 	snd_soc_component_update_bits(component, DA7219_CP_CTRL, DA7219_CP_EN_MASK,
193 			    DA7219_CP_EN_MASK);
194 	snd_soc_component_update_bits(component, DA7219_DIG_ROUTING_DAC,
195 			    DA7219_DAC_L_SRC_MASK | DA7219_DAC_R_SRC_MASK,
196 			    DA7219_DAC_L_SRC_TONEGEN |
197 			    DA7219_DAC_R_SRC_TONEGEN);
198 	snd_soc_component_update_bits(component, DA7219_DAC_L_CTRL,
199 			    DA7219_DAC_L_EN_MASK | DA7219_DAC_L_MUTE_EN_MASK,
200 			    DA7219_DAC_L_EN_MASK);
201 	snd_soc_component_update_bits(component, DA7219_DAC_R_CTRL,
202 			    DA7219_DAC_R_EN_MASK | DA7219_DAC_R_MUTE_EN_MASK,
203 			    DA7219_DAC_R_EN_MASK);
204 	snd_soc_component_update_bits(component, DA7219_MIXOUT_L_SELECT,
205 			    DA7219_MIXOUT_L_MIX_SELECT_MASK,
206 			    DA7219_MIXOUT_L_MIX_SELECT_MASK);
207 	snd_soc_component_update_bits(component, DA7219_MIXOUT_R_SELECT,
208 			    DA7219_MIXOUT_R_MIX_SELECT_MASK,
209 			    DA7219_MIXOUT_R_MIX_SELECT_MASK);
210 	snd_soc_component_update_bits(component, DA7219_DROUTING_ST_OUTFILT_1L,
211 			    DA7219_OUTFILT_ST_1L_SRC_MASK,
212 			    DA7219_DMIX_ST_SRC_OUTFILT1L);
213 	snd_soc_component_update_bits(component, DA7219_DROUTING_ST_OUTFILT_1R,
214 			    DA7219_OUTFILT_ST_1R_SRC_MASK,
215 			    DA7219_DMIX_ST_SRC_OUTFILT1R);
216 	snd_soc_component_update_bits(component, DA7219_MIXOUT_L_CTRL,
217 			    DA7219_MIXOUT_L_AMP_EN_MASK,
218 			    DA7219_MIXOUT_L_AMP_EN_MASK);
219 	snd_soc_component_update_bits(component, DA7219_MIXOUT_R_CTRL,
220 			    DA7219_MIXOUT_R_AMP_EN_MASK,
221 			    DA7219_MIXOUT_R_AMP_EN_MASK);
222 	snd_soc_component_update_bits(component, DA7219_HP_L_CTRL,
223 			    DA7219_HP_L_AMP_OE_MASK | DA7219_HP_L_AMP_EN_MASK,
224 			    DA7219_HP_L_AMP_OE_MASK | DA7219_HP_L_AMP_EN_MASK);
225 	snd_soc_component_update_bits(component, DA7219_HP_R_CTRL,
226 			    DA7219_HP_R_AMP_OE_MASK | DA7219_HP_R_AMP_EN_MASK,
227 			    DA7219_HP_R_AMP_OE_MASK | DA7219_HP_R_AMP_EN_MASK);
228 	msleep(DA7219_SETTLING_DELAY);
229 	snd_soc_component_update_bits(component, DA7219_HP_L_CTRL,
230 			    DA7219_HP_L_AMP_MUTE_EN_MASK |
231 			    DA7219_HP_L_AMP_MIN_GAIN_EN_MASK, 0);
232 	snd_soc_component_update_bits(component, DA7219_HP_R_CTRL,
233 			    DA7219_HP_R_AMP_MUTE_EN_MASK |
234 			    DA7219_HP_R_AMP_MIN_GAIN_EN_MASK, 0);
235 
236 	/*
237 	 * If we're running from the internal oscillator then give audio paths
238 	 * time to settle before running test.
239 	 */
240 	if (!(pll_srm_sts & DA7219_PLL_SRM_STS_MCLK))
241 		msleep(DA7219_AAD_HPTEST_INT_OSC_PATH_DELAY);
242 
243 	/* Configure & start Tone Generator */
244 	snd_soc_component_write(component, DA7219_TONE_GEN_ON_PER, DA7219_BEEP_ON_PER_MASK);
245 	regmap_raw_write(da7219->regmap, DA7219_TONE_GEN_FREQ1_L,
246 			 &tonegen_freq_hptest, sizeof(tonegen_freq_hptest));
247 	snd_soc_component_update_bits(component, DA7219_TONE_GEN_CFG2,
248 			    DA7219_SWG_SEL_MASK | DA7219_TONE_GEN_GAIN_MASK,
249 			    DA7219_SWG_SEL_SRAMP |
250 			    DA7219_TONE_GEN_GAIN_MINUS_15DB);
251 	snd_soc_component_write(component, DA7219_TONE_GEN_CFG1, DA7219_START_STOPN_MASK);
252 
253 	msleep(DA7219_AAD_HPTEST_PERIOD);
254 
255 	/* Grab comparator reading */
256 	accdet_cfg8 = snd_soc_component_read(component, DA7219_ACCDET_CONFIG_8);
257 	if (accdet_cfg8 & DA7219_HPTEST_COMP_MASK)
258 		report |= SND_JACK_HEADPHONE;
259 	else
260 		report |= SND_JACK_LINEOUT;
261 
262 	/* Stop tone generator */
263 	snd_soc_component_write(component, DA7219_TONE_GEN_CFG1, 0);
264 
265 	msleep(DA7219_AAD_HPTEST_PERIOD);
266 
267 	/* Restore original settings from cache */
268 	regcache_mark_dirty(da7219->regmap);
269 	regcache_sync_region(da7219->regmap, DA7219_HP_L_CTRL,
270 			     DA7219_HP_R_CTRL);
271 	msleep(DA7219_SETTLING_DELAY);
272 	regcache_sync_region(da7219->regmap, DA7219_MIXOUT_L_CTRL,
273 			     DA7219_MIXOUT_R_CTRL);
274 	regcache_sync_region(da7219->regmap, DA7219_DROUTING_ST_OUTFILT_1L,
275 			     DA7219_DROUTING_ST_OUTFILT_1R);
276 	regcache_sync_region(da7219->regmap, DA7219_MIXOUT_L_SELECT,
277 			     DA7219_MIXOUT_R_SELECT);
278 	regcache_sync_region(da7219->regmap, DA7219_DAC_L_CTRL,
279 			     DA7219_DAC_R_CTRL);
280 	regcache_sync_region(da7219->regmap, DA7219_DIG_ROUTING_DAC,
281 			     DA7219_DIG_ROUTING_DAC);
282 	regcache_sync_region(da7219->regmap, DA7219_CP_CTRL, DA7219_CP_CTRL);
283 	regcache_sync_region(da7219->regmap, DA7219_DAC_FILTERS5,
284 			     DA7219_DAC_FILTERS5);
285 	regcache_sync_region(da7219->regmap, DA7219_DAC_FILTERS4,
286 			     DA7219_DAC_FILTERS1);
287 	regcache_sync_region(da7219->regmap, DA7219_HP_L_GAIN,
288 			     DA7219_HP_R_GAIN);
289 	regcache_sync_region(da7219->regmap, DA7219_DAC_L_GAIN,
290 			     DA7219_DAC_R_GAIN);
291 	regcache_sync_region(da7219->regmap, DA7219_TONE_GEN_ON_PER,
292 			     DA7219_TONE_GEN_ON_PER);
293 	regcache_sync_region(da7219->regmap, DA7219_TONE_GEN_FREQ1_L,
294 			     DA7219_TONE_GEN_FREQ1_U);
295 	regcache_sync_region(da7219->regmap, DA7219_TONE_GEN_CFG1,
296 			     DA7219_TONE_GEN_CFG2);
297 
298 	regcache_cache_bypass(da7219->regmap, false);
299 
300 	/* Disable HPTest block */
301 	snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_8,
302 			    DA7219_HPTEST_EN_MASK, 0);
303 
304 	/*
305 	 * If we're running from the internal oscillator then give audio paths
306 	 * time to settle before allowing headphones to be driven as required.
307 	 */
308 	if (!(pll_srm_sts & DA7219_PLL_SRM_STS_MCLK))
309 		msleep(DA7219_AAD_HPTEST_INT_OSC_PATH_DELAY);
310 
311 	/* Restore gain ramping rate */
312 	snd_soc_component_write(component, DA7219_GAIN_RAMP_CTRL, gain_ramp_ctrl);
313 
314 	/* Drive Headphones/lineout */
315 	snd_soc_component_update_bits(component, DA7219_HP_L_CTRL, DA7219_HP_L_AMP_OE_MASK,
316 			    DA7219_HP_L_AMP_OE_MASK);
317 	snd_soc_component_update_bits(component, DA7219_HP_R_CTRL, DA7219_HP_R_AMP_OE_MASK,
318 			    DA7219_HP_R_AMP_OE_MASK);
319 
320 	/* Restore PLL to previous configuration, if re-configured */
321 	if ((pll_srm_sts & DA7219_PLL_SRM_STS_MCLK) &&
322 	    ((pll_ctrl & DA7219_PLL_MODE_MASK) == DA7219_PLL_MODE_BYPASS))
323 		da7219_set_pll(component, DA7219_SYSCLK_MCLK, 0);
324 
325 	/* Remove MCLK, if previously enabled */
326 	if (da7219->mclk)
327 		clk_disable_unprepare(da7219->mclk);
328 
329 	mutex_unlock(&da7219->pll_lock);
330 	mutex_unlock(&da7219->ctrl_lock);
331 	snd_soc_dapm_mutex_unlock(dapm);
332 
333 	/*
334 	 * Only send report if jack hasn't been removed during process,
335 	 * otherwise it's invalid and we drop it.
336 	 */
337 	if (da7219_aad->jack_inserted)
338 		snd_soc_jack_report(da7219_aad->jack, report,
339 				    SND_JACK_HEADSET | SND_JACK_LINEOUT);
340 }
341 
342 static void da7219_aad_jack_det_work(struct work_struct *work)
343 {
344 	struct da7219_aad_priv *da7219_aad =
345 		container_of(work, struct da7219_aad_priv, jack_det_work.work);
346 	struct snd_soc_component *component = da7219_aad->component;
347 
348 	/* Enable ground switch */
349 	snd_soc_component_update_bits(component, 0xFB, 0x01, 0x01);
350 }
351 
352 /*
353  * IRQ
354  */
355 
356 static irqreturn_t da7219_aad_irq_thread(int irq, void *data)
357 {
358 	struct da7219_aad_priv *da7219_aad = data;
359 	struct snd_soc_component *component = da7219_aad->component;
360 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
361 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
362 	u8 events[DA7219_AAD_IRQ_REG_MAX];
363 	u8 statusa;
364 	int i, ret, report = 0, mask = 0;
365 
366 	/* Read current IRQ events */
367 	ret = regmap_bulk_read(da7219->regmap, DA7219_ACCDET_IRQ_EVENT_A,
368 			       events, DA7219_AAD_IRQ_REG_MAX);
369 	if (ret) {
370 		dev_warn_ratelimited(component->dev, "Failed to read IRQ events: %d\n", ret);
371 		return IRQ_NONE;
372 	}
373 
374 	if (!events[DA7219_AAD_IRQ_REG_A] && !events[DA7219_AAD_IRQ_REG_B])
375 		return IRQ_NONE;
376 
377 	/* Read status register for jack insertion & type status */
378 	statusa = snd_soc_component_read(component, DA7219_ACCDET_STATUS_A);
379 
380 	if (events[DA7219_AAD_IRQ_REG_A] & DA7219_E_JACK_INSERTED_MASK) {
381 		u8 srm_st;
382 		int delay = 0;
383 
384 		srm_st = snd_soc_component_read(component,
385 					DA7219_PLL_SRM_STS) & DA7219_PLL_SRM_STS_MCLK;
386 		delay = (da7219_aad->gnd_switch_delay * ((srm_st == 0x0) ? 2 : 1) - 2);
387 		queue_delayed_work(da7219_aad->aad_wq,
388 							&da7219_aad->jack_det_work,
389 							msecs_to_jiffies(delay));
390 	}
391 
392 	/* Clear events */
393 	regmap_bulk_write(da7219->regmap, DA7219_ACCDET_IRQ_EVENT_A,
394 			  events, DA7219_AAD_IRQ_REG_MAX);
395 
396 	dev_dbg(component->dev, "IRQ events = 0x%x|0x%x, status = 0x%x\n",
397 		events[DA7219_AAD_IRQ_REG_A], events[DA7219_AAD_IRQ_REG_B],
398 		statusa);
399 
400 	if (statusa & DA7219_JACK_INSERTION_STS_MASK) {
401 		/* Jack Insertion */
402 		if (events[DA7219_AAD_IRQ_REG_A] &
403 		    DA7219_E_JACK_INSERTED_MASK) {
404 			report |= SND_JACK_MECHANICAL;
405 			mask |= SND_JACK_MECHANICAL;
406 			da7219_aad->jack_inserted = true;
407 		}
408 
409 		/* Jack type detection */
410 		if (events[DA7219_AAD_IRQ_REG_A] &
411 		    DA7219_E_JACK_DETECT_COMPLETE_MASK) {
412 			/*
413 			 * If 4-pole, then enable button detection, else perform
414 			 * HP impedance test to determine output type to report.
415 			 *
416 			 * We schedule work here as the tasks themselves can
417 			 * take time to complete, and in particular for hptest
418 			 * we want to be able to check if the jack was removed
419 			 * during the procedure as this will invalidate the
420 			 * result. By doing this as work, the IRQ thread can
421 			 * handle a removal, and we can check at the end of
422 			 * hptest if we have a valid result or not.
423 			 */
424 			if (statusa & DA7219_JACK_TYPE_STS_MASK) {
425 				report |= SND_JACK_HEADSET;
426 				mask |=	SND_JACK_HEADSET | SND_JACK_LINEOUT;
427 				queue_work(da7219_aad->aad_wq, &da7219_aad->btn_det_work);
428 			} else {
429 				queue_work(da7219_aad->aad_wq, &da7219_aad->hptest_work);
430 			}
431 		}
432 
433 		/* Button support for 4-pole jack */
434 		if (statusa & DA7219_JACK_TYPE_STS_MASK) {
435 			for (i = 0; i < DA7219_AAD_MAX_BUTTONS; ++i) {
436 				/* Button Press */
437 				if (events[DA7219_AAD_IRQ_REG_B] &
438 				    (DA7219_E_BUTTON_A_PRESSED_MASK << i)) {
439 					report |= SND_JACK_BTN_0 >> i;
440 					mask |= SND_JACK_BTN_0 >> i;
441 				}
442 			}
443 			snd_soc_jack_report(da7219_aad->jack, report, mask);
444 
445 			for (i = 0; i < DA7219_AAD_MAX_BUTTONS; ++i) {
446 				/* Button Release */
447 				if (events[DA7219_AAD_IRQ_REG_B] &
448 				    (DA7219_E_BUTTON_A_RELEASED_MASK >> i)) {
449 					report &= ~(SND_JACK_BTN_0 >> i);
450 					mask |= SND_JACK_BTN_0 >> i;
451 				}
452 			}
453 		}
454 	} else {
455 		/* Jack removal */
456 		if (events[DA7219_AAD_IRQ_REG_A] & DA7219_E_JACK_REMOVED_MASK) {
457 			report = 0;
458 			mask |= DA7219_AAD_REPORT_ALL_MASK;
459 			da7219_aad->jack_inserted = false;
460 
461 			/* Cancel any pending work */
462 			cancel_delayed_work_sync(&da7219_aad->jack_det_work);
463 			cancel_work_sync(&da7219_aad->btn_det_work);
464 			cancel_work_sync(&da7219_aad->hptest_work);
465 
466 			/* Un-drive headphones/lineout */
467 			snd_soc_component_update_bits(component, DA7219_HP_R_CTRL,
468 					    DA7219_HP_R_AMP_OE_MASK, 0);
469 			snd_soc_component_update_bits(component, DA7219_HP_L_CTRL,
470 					    DA7219_HP_L_AMP_OE_MASK, 0);
471 
472 			/* Ensure button detection disabled */
473 			snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1,
474 					    DA7219_BUTTON_CONFIG_MASK, 0);
475 
476 			da7219->micbias_on_event = false;
477 
478 			/* Disable mic bias */
479 			snd_soc_dapm_disable_pin(dapm, "Mic Bias");
480 			snd_soc_dapm_sync(dapm);
481 
482 			/* Disable ground switch */
483 			snd_soc_component_update_bits(component, 0xFB, 0x01, 0x00);
484 		}
485 	}
486 
487 	snd_soc_jack_report(da7219_aad->jack, report, mask);
488 
489 	return IRQ_HANDLED;
490 }
491 
492 /*
493  * DT/ACPI to pdata conversion
494  */
495 
496 static enum da7219_aad_micbias_pulse_lvl
497 	da7219_aad_fw_micbias_pulse_lvl(struct device *dev, u32 val)
498 {
499 	switch (val) {
500 	case 2800:
501 		return DA7219_AAD_MICBIAS_PULSE_LVL_2_8V;
502 	case 2900:
503 		return DA7219_AAD_MICBIAS_PULSE_LVL_2_9V;
504 	default:
505 		dev_warn(dev, "Invalid micbias pulse level");
506 		return DA7219_AAD_MICBIAS_PULSE_LVL_OFF;
507 	}
508 }
509 
510 static enum da7219_aad_btn_cfg
511 	da7219_aad_fw_btn_cfg(struct device *dev, u32 val)
512 {
513 	switch (val) {
514 	case 2:
515 		return DA7219_AAD_BTN_CFG_2MS;
516 	case 5:
517 		return DA7219_AAD_BTN_CFG_5MS;
518 	case 10:
519 		return DA7219_AAD_BTN_CFG_10MS;
520 	case 50:
521 		return DA7219_AAD_BTN_CFG_50MS;
522 	case 100:
523 		return DA7219_AAD_BTN_CFG_100MS;
524 	case 200:
525 		return DA7219_AAD_BTN_CFG_200MS;
526 	case 500:
527 		return DA7219_AAD_BTN_CFG_500MS;
528 	default:
529 		dev_warn(dev, "Invalid button config");
530 		return DA7219_AAD_BTN_CFG_10MS;
531 	}
532 }
533 
534 static enum da7219_aad_mic_det_thr
535 	da7219_aad_fw_mic_det_thr(struct device *dev, u32 val)
536 {
537 	switch (val) {
538 	case 200:
539 		return DA7219_AAD_MIC_DET_THR_200_OHMS;
540 	case 500:
541 		return DA7219_AAD_MIC_DET_THR_500_OHMS;
542 	case 750:
543 		return DA7219_AAD_MIC_DET_THR_750_OHMS;
544 	case 1000:
545 		return DA7219_AAD_MIC_DET_THR_1000_OHMS;
546 	default:
547 		dev_warn(dev, "Invalid mic detect threshold");
548 		return DA7219_AAD_MIC_DET_THR_500_OHMS;
549 	}
550 }
551 
552 static enum da7219_aad_jack_ins_deb
553 	da7219_aad_fw_jack_ins_deb(struct device *dev, u32 val)
554 {
555 	switch (val) {
556 	case 5:
557 		return DA7219_AAD_JACK_INS_DEB_5MS;
558 	case 10:
559 		return DA7219_AAD_JACK_INS_DEB_10MS;
560 	case 20:
561 		return DA7219_AAD_JACK_INS_DEB_20MS;
562 	case 50:
563 		return DA7219_AAD_JACK_INS_DEB_50MS;
564 	case 100:
565 		return DA7219_AAD_JACK_INS_DEB_100MS;
566 	case 200:
567 		return DA7219_AAD_JACK_INS_DEB_200MS;
568 	case 500:
569 		return DA7219_AAD_JACK_INS_DEB_500MS;
570 	case 1000:
571 		return DA7219_AAD_JACK_INS_DEB_1S;
572 	default:
573 		dev_warn(dev, "Invalid jack insert debounce");
574 		return DA7219_AAD_JACK_INS_DEB_20MS;
575 	}
576 }
577 
578 static enum da7219_aad_jack_ins_det_pty
579 	da7219_aad_fw_jack_ins_det_pty(struct device *dev, const char *str)
580 {
581 	if (!strcmp(str, "low")) {
582 		return DA7219_AAD_JACK_INS_DET_PTY_LOW;
583 	} else if (!strcmp(str, "high")) {
584 		return DA7219_AAD_JACK_INS_DET_PTY_HIGH;
585 	} else {
586 		dev_warn(dev, "Invalid jack insertion detection polarity");
587 		return DA7219_AAD_JACK_INS_DET_PTY_LOW;
588 	}
589 }
590 
591 static enum da7219_aad_jack_det_rate
592 	da7219_aad_fw_jack_det_rate(struct device *dev, const char *str)
593 {
594 	if (!strcmp(str, "32_64")) {
595 		return DA7219_AAD_JACK_DET_RATE_32_64MS;
596 	} else if (!strcmp(str, "64_128")) {
597 		return DA7219_AAD_JACK_DET_RATE_64_128MS;
598 	} else if (!strcmp(str, "128_256")) {
599 		return DA7219_AAD_JACK_DET_RATE_128_256MS;
600 	} else if (!strcmp(str, "256_512")) {
601 		return DA7219_AAD_JACK_DET_RATE_256_512MS;
602 	} else {
603 		dev_warn(dev, "Invalid jack detect rate");
604 		return DA7219_AAD_JACK_DET_RATE_256_512MS;
605 	}
606 }
607 
608 static enum da7219_aad_jack_rem_deb
609 	da7219_aad_fw_jack_rem_deb(struct device *dev, u32 val)
610 {
611 	switch (val) {
612 	case 1:
613 		return DA7219_AAD_JACK_REM_DEB_1MS;
614 	case 5:
615 		return DA7219_AAD_JACK_REM_DEB_5MS;
616 	case 10:
617 		return DA7219_AAD_JACK_REM_DEB_10MS;
618 	case 20:
619 		return DA7219_AAD_JACK_REM_DEB_20MS;
620 	default:
621 		dev_warn(dev, "Invalid jack removal debounce");
622 		return DA7219_AAD_JACK_REM_DEB_1MS;
623 	}
624 }
625 
626 static enum da7219_aad_btn_avg
627 	da7219_aad_fw_btn_avg(struct device *dev, u32 val)
628 {
629 	switch (val) {
630 	case 1:
631 		return DA7219_AAD_BTN_AVG_1;
632 	case 2:
633 		return DA7219_AAD_BTN_AVG_2;
634 	case 4:
635 		return DA7219_AAD_BTN_AVG_4;
636 	case 8:
637 		return DA7219_AAD_BTN_AVG_8;
638 	default:
639 		dev_warn(dev, "Invalid button average value");
640 		return DA7219_AAD_BTN_AVG_2;
641 	}
642 }
643 
644 static enum da7219_aad_adc_1bit_rpt
645 	da7219_aad_fw_adc_1bit_rpt(struct device *dev, u32 val)
646 {
647 	switch (val) {
648 	case 1:
649 		return DA7219_AAD_ADC_1BIT_RPT_1;
650 	case 2:
651 		return DA7219_AAD_ADC_1BIT_RPT_2;
652 	case 4:
653 		return DA7219_AAD_ADC_1BIT_RPT_4;
654 	case 8:
655 		return DA7219_AAD_ADC_1BIT_RPT_8;
656 	default:
657 		dev_warn(dev, "Invalid ADC 1-bit repeat value");
658 		return DA7219_AAD_ADC_1BIT_RPT_1;
659 	}
660 }
661 
662 static struct da7219_aad_pdata *da7219_aad_fw_to_pdata(struct device *dev)
663 {
664 	struct i2c_client *i2c = to_i2c_client(dev);
665 	struct fwnode_handle *aad_np;
666 	struct da7219_aad_pdata *aad_pdata;
667 	const char *fw_str;
668 	u32 fw_val32;
669 
670 	aad_np = device_get_named_child_node(dev, "da7219_aad");
671 	if (!aad_np)
672 		return NULL;
673 
674 	aad_pdata = devm_kzalloc(dev, sizeof(*aad_pdata), GFP_KERNEL);
675 	if (!aad_pdata)
676 		return NULL;
677 
678 	aad_pdata->irq = i2c->irq;
679 
680 	if (fwnode_property_read_u32(aad_np, "dlg,micbias-pulse-lvl",
681 				     &fw_val32) >= 0)
682 		aad_pdata->micbias_pulse_lvl =
683 			da7219_aad_fw_micbias_pulse_lvl(dev, fw_val32);
684 	else
685 		aad_pdata->micbias_pulse_lvl = DA7219_AAD_MICBIAS_PULSE_LVL_OFF;
686 
687 	if (fwnode_property_read_u32(aad_np, "dlg,micbias-pulse-time",
688 				     &fw_val32) >= 0)
689 		aad_pdata->micbias_pulse_time = fw_val32;
690 
691 	if (fwnode_property_read_u32(aad_np, "dlg,btn-cfg", &fw_val32) >= 0)
692 		aad_pdata->btn_cfg = da7219_aad_fw_btn_cfg(dev, fw_val32);
693 	else
694 		aad_pdata->btn_cfg = DA7219_AAD_BTN_CFG_10MS;
695 
696 	if (fwnode_property_read_u32(aad_np, "dlg,mic-det-thr", &fw_val32) >= 0)
697 		aad_pdata->mic_det_thr =
698 			da7219_aad_fw_mic_det_thr(dev, fw_val32);
699 	else
700 		aad_pdata->mic_det_thr = DA7219_AAD_MIC_DET_THR_500_OHMS;
701 
702 	if (fwnode_property_read_u32(aad_np, "dlg,jack-ins-deb", &fw_val32) >= 0)
703 		aad_pdata->jack_ins_deb =
704 			da7219_aad_fw_jack_ins_deb(dev, fw_val32);
705 	else
706 		aad_pdata->jack_ins_deb = DA7219_AAD_JACK_INS_DEB_20MS;
707 
708 	if (!fwnode_property_read_string(aad_np, "dlg,jack-ins-det-pty", &fw_str))
709 		aad_pdata->jack_ins_det_pty =
710 			da7219_aad_fw_jack_ins_det_pty(dev, fw_str);
711 	else
712 		aad_pdata->jack_ins_det_pty = DA7219_AAD_JACK_INS_DET_PTY_LOW;
713 
714 	if (!fwnode_property_read_string(aad_np, "dlg,jack-det-rate", &fw_str))
715 		aad_pdata->jack_det_rate =
716 			da7219_aad_fw_jack_det_rate(dev, fw_str);
717 	else
718 		aad_pdata->jack_det_rate = DA7219_AAD_JACK_DET_RATE_256_512MS;
719 
720 	if (fwnode_property_read_u32(aad_np, "dlg,jack-rem-deb", &fw_val32) >= 0)
721 		aad_pdata->jack_rem_deb =
722 			da7219_aad_fw_jack_rem_deb(dev, fw_val32);
723 	else
724 		aad_pdata->jack_rem_deb = DA7219_AAD_JACK_REM_DEB_1MS;
725 
726 	if (fwnode_property_read_u32(aad_np, "dlg,a-d-btn-thr", &fw_val32) >= 0)
727 		aad_pdata->a_d_btn_thr = (u8) fw_val32;
728 	else
729 		aad_pdata->a_d_btn_thr = 0xA;
730 
731 	if (fwnode_property_read_u32(aad_np, "dlg,d-b-btn-thr", &fw_val32) >= 0)
732 		aad_pdata->d_b_btn_thr = (u8) fw_val32;
733 	else
734 		aad_pdata->d_b_btn_thr = 0x16;
735 
736 	if (fwnode_property_read_u32(aad_np, "dlg,b-c-btn-thr", &fw_val32) >= 0)
737 		aad_pdata->b_c_btn_thr = (u8) fw_val32;
738 	else
739 		aad_pdata->b_c_btn_thr = 0x21;
740 
741 	if (fwnode_property_read_u32(aad_np, "dlg,c-mic-btn-thr", &fw_val32) >= 0)
742 		aad_pdata->c_mic_btn_thr = (u8) fw_val32;
743 	else
744 		aad_pdata->c_mic_btn_thr = 0x3E;
745 
746 	if (fwnode_property_read_u32(aad_np, "dlg,btn-avg", &fw_val32) >= 0)
747 		aad_pdata->btn_avg = da7219_aad_fw_btn_avg(dev, fw_val32);
748 	else
749 		aad_pdata->btn_avg = DA7219_AAD_BTN_AVG_2;
750 
751 	if (fwnode_property_read_u32(aad_np, "dlg,adc-1bit-rpt", &fw_val32) >= 0)
752 		aad_pdata->adc_1bit_rpt =
753 			da7219_aad_fw_adc_1bit_rpt(dev, fw_val32);
754 	else
755 		aad_pdata->adc_1bit_rpt = DA7219_AAD_ADC_1BIT_RPT_1;
756 
757 	return aad_pdata;
758 }
759 
760 static void da7219_aad_handle_pdata(struct snd_soc_component *component)
761 {
762 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
763 	struct da7219_aad_priv *da7219_aad = da7219->aad;
764 	struct da7219_pdata *pdata = da7219->pdata;
765 
766 	if ((pdata) && (pdata->aad_pdata)) {
767 		struct da7219_aad_pdata *aad_pdata = pdata->aad_pdata;
768 		u8 cfg, mask;
769 
770 		da7219_aad->irq = aad_pdata->irq;
771 
772 		switch (aad_pdata->micbias_pulse_lvl) {
773 		case DA7219_AAD_MICBIAS_PULSE_LVL_2_8V:
774 		case DA7219_AAD_MICBIAS_PULSE_LVL_2_9V:
775 			da7219_aad->micbias_pulse_lvl =
776 				(aad_pdata->micbias_pulse_lvl <<
777 				 DA7219_MICBIAS1_LEVEL_SHIFT);
778 			break;
779 		default:
780 			break;
781 		}
782 
783 		da7219_aad->micbias_pulse_time = aad_pdata->micbias_pulse_time;
784 
785 		switch (aad_pdata->btn_cfg) {
786 		case DA7219_AAD_BTN_CFG_2MS:
787 		case DA7219_AAD_BTN_CFG_5MS:
788 		case DA7219_AAD_BTN_CFG_10MS:
789 		case DA7219_AAD_BTN_CFG_50MS:
790 		case DA7219_AAD_BTN_CFG_100MS:
791 		case DA7219_AAD_BTN_CFG_200MS:
792 		case DA7219_AAD_BTN_CFG_500MS:
793 			da7219_aad->btn_cfg  = (aad_pdata->btn_cfg <<
794 						DA7219_BUTTON_CONFIG_SHIFT);
795 		}
796 
797 		cfg = 0;
798 		mask = 0;
799 		switch (aad_pdata->mic_det_thr) {
800 		case DA7219_AAD_MIC_DET_THR_200_OHMS:
801 		case DA7219_AAD_MIC_DET_THR_500_OHMS:
802 		case DA7219_AAD_MIC_DET_THR_750_OHMS:
803 		case DA7219_AAD_MIC_DET_THR_1000_OHMS:
804 			cfg |= (aad_pdata->mic_det_thr <<
805 				DA7219_MIC_DET_THRESH_SHIFT);
806 			mask |= DA7219_MIC_DET_THRESH_MASK;
807 		}
808 		snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1, mask, cfg);
809 
810 		cfg = 0;
811 		mask = 0;
812 		switch (aad_pdata->jack_ins_deb) {
813 		case DA7219_AAD_JACK_INS_DEB_5MS:
814 		case DA7219_AAD_JACK_INS_DEB_10MS:
815 		case DA7219_AAD_JACK_INS_DEB_20MS:
816 		case DA7219_AAD_JACK_INS_DEB_50MS:
817 		case DA7219_AAD_JACK_INS_DEB_100MS:
818 		case DA7219_AAD_JACK_INS_DEB_200MS:
819 		case DA7219_AAD_JACK_INS_DEB_500MS:
820 		case DA7219_AAD_JACK_INS_DEB_1S:
821 			cfg |= (aad_pdata->jack_ins_deb <<
822 				DA7219_JACKDET_DEBOUNCE_SHIFT);
823 			mask |= DA7219_JACKDET_DEBOUNCE_MASK;
824 		}
825 		switch (aad_pdata->jack_det_rate) {
826 		case DA7219_AAD_JACK_DET_RATE_32_64MS:
827 		case DA7219_AAD_JACK_DET_RATE_64_128MS:
828 		case DA7219_AAD_JACK_DET_RATE_128_256MS:
829 		case DA7219_AAD_JACK_DET_RATE_256_512MS:
830 			cfg |= (aad_pdata->jack_det_rate <<
831 				DA7219_JACK_DETECT_RATE_SHIFT);
832 			mask |= DA7219_JACK_DETECT_RATE_MASK;
833 		}
834 		switch (aad_pdata->jack_rem_deb) {
835 		case DA7219_AAD_JACK_REM_DEB_1MS:
836 		case DA7219_AAD_JACK_REM_DEB_5MS:
837 		case DA7219_AAD_JACK_REM_DEB_10MS:
838 		case DA7219_AAD_JACK_REM_DEB_20MS:
839 			cfg |= (aad_pdata->jack_rem_deb <<
840 				DA7219_JACKDET_REM_DEB_SHIFT);
841 			mask |= DA7219_JACKDET_REM_DEB_MASK;
842 		}
843 		snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_2, mask, cfg);
844 
845 		snd_soc_component_write(component, DA7219_ACCDET_CONFIG_3,
846 			      aad_pdata->a_d_btn_thr);
847 		snd_soc_component_write(component, DA7219_ACCDET_CONFIG_4,
848 			      aad_pdata->d_b_btn_thr);
849 		snd_soc_component_write(component, DA7219_ACCDET_CONFIG_5,
850 			      aad_pdata->b_c_btn_thr);
851 		snd_soc_component_write(component, DA7219_ACCDET_CONFIG_6,
852 			      aad_pdata->c_mic_btn_thr);
853 
854 		cfg = 0;
855 		mask = 0;
856 		switch (aad_pdata->btn_avg) {
857 		case DA7219_AAD_BTN_AVG_1:
858 		case DA7219_AAD_BTN_AVG_2:
859 		case DA7219_AAD_BTN_AVG_4:
860 		case DA7219_AAD_BTN_AVG_8:
861 			cfg |= (aad_pdata->btn_avg <<
862 				DA7219_BUTTON_AVERAGE_SHIFT);
863 			mask |= DA7219_BUTTON_AVERAGE_MASK;
864 		}
865 		switch (aad_pdata->adc_1bit_rpt) {
866 		case DA7219_AAD_ADC_1BIT_RPT_1:
867 		case DA7219_AAD_ADC_1BIT_RPT_2:
868 		case DA7219_AAD_ADC_1BIT_RPT_4:
869 		case DA7219_AAD_ADC_1BIT_RPT_8:
870 			cfg |= (aad_pdata->adc_1bit_rpt <<
871 			       DA7219_ADC_1_BIT_REPEAT_SHIFT);
872 			mask |= DA7219_ADC_1_BIT_REPEAT_MASK;
873 		}
874 		snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_7, mask, cfg);
875 
876 		switch (aad_pdata->jack_ins_det_pty) {
877 		case DA7219_AAD_JACK_INS_DET_PTY_LOW:
878 			snd_soc_component_write(component, 0xF0, 0x8B);
879 			snd_soc_component_write(component, 0x75, 0x80);
880 			snd_soc_component_write(component, 0xF0, 0x00);
881 			break;
882 		case DA7219_AAD_JACK_INS_DET_PTY_HIGH:
883 			snd_soc_component_write(component, 0xF0, 0x8B);
884 			snd_soc_component_write(component, 0x75, 0x00);
885 			snd_soc_component_write(component, 0xF0, 0x00);
886 			break;
887 		default:
888 			break;
889 		}
890 	}
891 }
892 
893 static void da7219_aad_handle_gnd_switch_time(struct snd_soc_component *component)
894 {
895 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
896 	struct da7219_aad_priv *da7219_aad = da7219->aad;
897 	u8 jack_det;
898 
899 	jack_det = snd_soc_component_read(component, DA7219_ACCDET_CONFIG_2)
900 		& DA7219_JACK_DETECT_RATE_MASK;
901 	switch (jack_det) {
902 	case 0x00:
903 		da7219_aad->gnd_switch_delay = 32;
904 		break;
905 	case 0x10:
906 		da7219_aad->gnd_switch_delay = 64;
907 		break;
908 	case 0x20:
909 		da7219_aad->gnd_switch_delay = 128;
910 		break;
911 	case 0x30:
912 		da7219_aad->gnd_switch_delay = 256;
913 		break;
914 	default:
915 		da7219_aad->gnd_switch_delay = 32;
916 		break;
917 	}
918 }
919 
920 /*
921  * Suspend/Resume
922  */
923 
924 void da7219_aad_suspend(struct snd_soc_component *component)
925 {
926 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
927 	struct da7219_aad_priv *da7219_aad = da7219->aad;
928 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
929 	u8 micbias_ctrl;
930 
931 	if (da7219_aad->jack) {
932 		/* Disable jack detection during suspend */
933 		snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1,
934 				    DA7219_ACCDET_EN_MASK, 0);
935 
936 		/*
937 		 * If we have a 4-pole jack inserted, then micbias will be
938 		 * enabled. We can disable micbias here, and keep a note to
939 		 * re-enable it on resume. If jack removal occurred during
940 		 * suspend then this will be dealt with through the IRQ handler.
941 		 */
942 		if (da7219_aad->jack_inserted) {
943 			micbias_ctrl = snd_soc_component_read(component, DA7219_MICBIAS_CTRL);
944 			if (micbias_ctrl & DA7219_MICBIAS1_EN_MASK) {
945 				snd_soc_dapm_disable_pin(dapm, "Mic Bias");
946 				snd_soc_dapm_sync(dapm);
947 				da7219_aad->micbias_resume_enable = true;
948 			}
949 		}
950 	}
951 
952 	synchronize_irq(da7219_aad->irq);
953 }
954 
955 void da7219_aad_resume(struct snd_soc_component *component)
956 {
957 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
958 	struct da7219_aad_priv *da7219_aad = da7219->aad;
959 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
960 
961 	if (da7219_aad->jack) {
962 		/* Re-enable micbias if previously enabled for 4-pole jack */
963 		if (da7219_aad->jack_inserted &&
964 		    da7219_aad->micbias_resume_enable) {
965 			snd_soc_dapm_force_enable_pin(dapm, "Mic Bias");
966 			snd_soc_dapm_sync(dapm);
967 			da7219_aad->micbias_resume_enable = false;
968 		}
969 
970 		/* Re-enable jack detection */
971 		snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1,
972 				    DA7219_ACCDET_EN_MASK,
973 				    DA7219_ACCDET_EN_MASK);
974 	}
975 }
976 
977 
978 /*
979  * Init/Exit
980  */
981 
982 int da7219_aad_init(struct snd_soc_component *component)
983 {
984 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
985 	struct da7219_aad_priv *da7219_aad = da7219->aad;
986 	u8 mask[DA7219_AAD_IRQ_REG_MAX];
987 	int ret;
988 
989 	da7219_aad->component = component;
990 
991 	/* Handle any DT/ACPI/platform data */
992 	da7219_aad_handle_pdata(component);
993 
994 	/* Disable button detection */
995 	snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1,
996 			    DA7219_BUTTON_CONFIG_MASK, 0);
997 
998 	da7219_aad_handle_gnd_switch_time(component);
999 
1000 	da7219_aad->aad_wq = create_singlethread_workqueue("da7219-aad");
1001 	if (!da7219_aad->aad_wq) {
1002 		dev_err(component->dev, "Failed to create aad workqueue\n");
1003 		return -ENOMEM;
1004 	}
1005 
1006 	INIT_DELAYED_WORK(&da7219_aad->jack_det_work, da7219_aad_jack_det_work);
1007 	INIT_WORK(&da7219_aad->btn_det_work, da7219_aad_btn_det_work);
1008 	INIT_WORK(&da7219_aad->hptest_work, da7219_aad_hptest_work);
1009 
1010 	ret = request_threaded_irq(da7219_aad->irq, NULL,
1011 				   da7219_aad_irq_thread,
1012 				   IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1013 				   "da7219-aad", da7219_aad);
1014 	if (ret) {
1015 		dev_err(component->dev, "Failed to request IRQ: %d\n", ret);
1016 		return ret;
1017 	}
1018 
1019 	/* Unmask AAD IRQs */
1020 	memset(mask, 0, DA7219_AAD_IRQ_REG_MAX);
1021 	regmap_bulk_write(da7219->regmap, DA7219_ACCDET_IRQ_MASK_A,
1022 			  &mask, DA7219_AAD_IRQ_REG_MAX);
1023 
1024 	return 0;
1025 }
1026 
1027 void da7219_aad_exit(struct snd_soc_component *component)
1028 {
1029 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1030 	struct da7219_aad_priv *da7219_aad = da7219->aad;
1031 	u8 mask[DA7219_AAD_IRQ_REG_MAX];
1032 
1033 	/* Mask off AAD IRQs */
1034 	memset(mask, DA7219_BYTE_MASK, DA7219_AAD_IRQ_REG_MAX);
1035 	regmap_bulk_write(da7219->regmap, DA7219_ACCDET_IRQ_MASK_A,
1036 			  mask, DA7219_AAD_IRQ_REG_MAX);
1037 
1038 	free_irq(da7219_aad->irq, da7219_aad);
1039 
1040 	cancel_delayed_work_sync(&da7219_aad->jack_det_work);
1041 	cancel_work_sync(&da7219_aad->btn_det_work);
1042 	cancel_work_sync(&da7219_aad->hptest_work);
1043 	destroy_workqueue(da7219_aad->aad_wq);
1044 }
1045 
1046 /*
1047  * AAD related I2C probe handling
1048  */
1049 
1050 int da7219_aad_probe(struct i2c_client *i2c)
1051 {
1052 	struct da7219_priv *da7219 = i2c_get_clientdata(i2c);
1053 	struct device *dev = &i2c->dev;
1054 	struct da7219_aad_priv *da7219_aad;
1055 
1056 	da7219_aad = devm_kzalloc(dev, sizeof(*da7219_aad), GFP_KERNEL);
1057 	if (!da7219_aad)
1058 		return -ENOMEM;
1059 
1060 	da7219->aad = da7219_aad;
1061 
1062 	/* Retrieve any DT/ACPI/platform data */
1063 	if (da7219->pdata && !da7219->pdata->aad_pdata)
1064 		da7219->pdata->aad_pdata = da7219_aad_fw_to_pdata(dev);
1065 
1066 	return 0;
1067 }
1068 
1069 MODULE_DESCRIPTION("ASoC DA7219 AAD Driver");
1070 MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
1071 MODULE_AUTHOR("David Rau <David.Rau.opensource@dm.renesas.com>");
1072 MODULE_LICENSE("GPL");
1073