xref: /openbmc/linux/sound/soc/codecs/da7219-aad.c (revision e021ae7f)
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, report = 0, mask = 0;
365 
366 	/* Read current IRQ events */
367 	regmap_bulk_read(da7219->regmap, DA7219_ACCDET_IRQ_EVENT_A,
368 			 events, DA7219_AAD_IRQ_REG_MAX);
369 
370 	if (!events[DA7219_AAD_IRQ_REG_A] && !events[DA7219_AAD_IRQ_REG_B])
371 		return IRQ_NONE;
372 
373 	/* Read status register for jack insertion & type status */
374 	statusa = snd_soc_component_read(component, DA7219_ACCDET_STATUS_A);
375 
376 	if (events[DA7219_AAD_IRQ_REG_A] & DA7219_E_JACK_INSERTED_MASK) {
377 		u8 srm_st;
378 		int delay = 0;
379 
380 		srm_st = snd_soc_component_read(component,
381 					DA7219_PLL_SRM_STS) & DA7219_PLL_SRM_STS_MCLK;
382 		delay = (da7219_aad->gnd_switch_delay * ((srm_st == 0x0) ? 2 : 1) - 2);
383 		queue_delayed_work(da7219_aad->aad_wq,
384 							&da7219_aad->jack_det_work,
385 							msecs_to_jiffies(delay));
386 	}
387 
388 	/* Clear events */
389 	regmap_bulk_write(da7219->regmap, DA7219_ACCDET_IRQ_EVENT_A,
390 			  events, DA7219_AAD_IRQ_REG_MAX);
391 
392 	dev_dbg(component->dev, "IRQ events = 0x%x|0x%x, status = 0x%x\n",
393 		events[DA7219_AAD_IRQ_REG_A], events[DA7219_AAD_IRQ_REG_B],
394 		statusa);
395 
396 	if (statusa & DA7219_JACK_INSERTION_STS_MASK) {
397 		/* Jack Insertion */
398 		if (events[DA7219_AAD_IRQ_REG_A] &
399 		    DA7219_E_JACK_INSERTED_MASK) {
400 			report |= SND_JACK_MECHANICAL;
401 			mask |= SND_JACK_MECHANICAL;
402 			da7219_aad->jack_inserted = true;
403 		}
404 
405 		/* Jack type detection */
406 		if (events[DA7219_AAD_IRQ_REG_A] &
407 		    DA7219_E_JACK_DETECT_COMPLETE_MASK) {
408 			/*
409 			 * If 4-pole, then enable button detection, else perform
410 			 * HP impedance test to determine output type to report.
411 			 *
412 			 * We schedule work here as the tasks themselves can
413 			 * take time to complete, and in particular for hptest
414 			 * we want to be able to check if the jack was removed
415 			 * during the procedure as this will invalidate the
416 			 * result. By doing this as work, the IRQ thread can
417 			 * handle a removal, and we can check at the end of
418 			 * hptest if we have a valid result or not.
419 			 */
420 			if (statusa & DA7219_JACK_TYPE_STS_MASK) {
421 				report |= SND_JACK_HEADSET;
422 				mask |=	SND_JACK_HEADSET | SND_JACK_LINEOUT;
423 				queue_work(da7219_aad->aad_wq, &da7219_aad->btn_det_work);
424 			} else {
425 				queue_work(da7219_aad->aad_wq, &da7219_aad->hptest_work);
426 			}
427 		}
428 
429 		/* Button support for 4-pole jack */
430 		if (statusa & DA7219_JACK_TYPE_STS_MASK) {
431 			for (i = 0; i < DA7219_AAD_MAX_BUTTONS; ++i) {
432 				/* Button Press */
433 				if (events[DA7219_AAD_IRQ_REG_B] &
434 				    (DA7219_E_BUTTON_A_PRESSED_MASK << i)) {
435 					report |= SND_JACK_BTN_0 >> i;
436 					mask |= SND_JACK_BTN_0 >> i;
437 				}
438 			}
439 			snd_soc_jack_report(da7219_aad->jack, report, mask);
440 
441 			for (i = 0; i < DA7219_AAD_MAX_BUTTONS; ++i) {
442 				/* Button Release */
443 				if (events[DA7219_AAD_IRQ_REG_B] &
444 				    (DA7219_E_BUTTON_A_RELEASED_MASK >> i)) {
445 					report &= ~(SND_JACK_BTN_0 >> i);
446 					mask |= SND_JACK_BTN_0 >> i;
447 				}
448 			}
449 		}
450 	} else {
451 		/* Jack removal */
452 		if (events[DA7219_AAD_IRQ_REG_A] & DA7219_E_JACK_REMOVED_MASK) {
453 			report = 0;
454 			mask |= DA7219_AAD_REPORT_ALL_MASK;
455 			da7219_aad->jack_inserted = false;
456 
457 			/* Cancel any pending work */
458 			cancel_delayed_work_sync(&da7219_aad->jack_det_work);
459 			cancel_work_sync(&da7219_aad->btn_det_work);
460 			cancel_work_sync(&da7219_aad->hptest_work);
461 
462 			/* Un-drive headphones/lineout */
463 			snd_soc_component_update_bits(component, DA7219_HP_R_CTRL,
464 					    DA7219_HP_R_AMP_OE_MASK, 0);
465 			snd_soc_component_update_bits(component, DA7219_HP_L_CTRL,
466 					    DA7219_HP_L_AMP_OE_MASK, 0);
467 
468 			/* Ensure button detection disabled */
469 			snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1,
470 					    DA7219_BUTTON_CONFIG_MASK, 0);
471 
472 			da7219->micbias_on_event = false;
473 
474 			/* Disable mic bias */
475 			snd_soc_dapm_disable_pin(dapm, "Mic Bias");
476 			snd_soc_dapm_sync(dapm);
477 
478 			/* Disable ground switch */
479 			snd_soc_component_update_bits(component, 0xFB, 0x01, 0x00);
480 		}
481 	}
482 
483 	snd_soc_jack_report(da7219_aad->jack, report, mask);
484 
485 	return IRQ_HANDLED;
486 }
487 
488 /*
489  * DT/ACPI to pdata conversion
490  */
491 
492 static enum da7219_aad_micbias_pulse_lvl
493 	da7219_aad_fw_micbias_pulse_lvl(struct device *dev, u32 val)
494 {
495 	switch (val) {
496 	case 2800:
497 		return DA7219_AAD_MICBIAS_PULSE_LVL_2_8V;
498 	case 2900:
499 		return DA7219_AAD_MICBIAS_PULSE_LVL_2_9V;
500 	default:
501 		dev_warn(dev, "Invalid micbias pulse level");
502 		return DA7219_AAD_MICBIAS_PULSE_LVL_OFF;
503 	}
504 }
505 
506 static enum da7219_aad_btn_cfg
507 	da7219_aad_fw_btn_cfg(struct device *dev, u32 val)
508 {
509 	switch (val) {
510 	case 2:
511 		return DA7219_AAD_BTN_CFG_2MS;
512 	case 5:
513 		return DA7219_AAD_BTN_CFG_5MS;
514 	case 10:
515 		return DA7219_AAD_BTN_CFG_10MS;
516 	case 50:
517 		return DA7219_AAD_BTN_CFG_50MS;
518 	case 100:
519 		return DA7219_AAD_BTN_CFG_100MS;
520 	case 200:
521 		return DA7219_AAD_BTN_CFG_200MS;
522 	case 500:
523 		return DA7219_AAD_BTN_CFG_500MS;
524 	default:
525 		dev_warn(dev, "Invalid button config");
526 		return DA7219_AAD_BTN_CFG_10MS;
527 	}
528 }
529 
530 static enum da7219_aad_mic_det_thr
531 	da7219_aad_fw_mic_det_thr(struct device *dev, u32 val)
532 {
533 	switch (val) {
534 	case 200:
535 		return DA7219_AAD_MIC_DET_THR_200_OHMS;
536 	case 500:
537 		return DA7219_AAD_MIC_DET_THR_500_OHMS;
538 	case 750:
539 		return DA7219_AAD_MIC_DET_THR_750_OHMS;
540 	case 1000:
541 		return DA7219_AAD_MIC_DET_THR_1000_OHMS;
542 	default:
543 		dev_warn(dev, "Invalid mic detect threshold");
544 		return DA7219_AAD_MIC_DET_THR_500_OHMS;
545 	}
546 }
547 
548 static enum da7219_aad_jack_ins_deb
549 	da7219_aad_fw_jack_ins_deb(struct device *dev, u32 val)
550 {
551 	switch (val) {
552 	case 5:
553 		return DA7219_AAD_JACK_INS_DEB_5MS;
554 	case 10:
555 		return DA7219_AAD_JACK_INS_DEB_10MS;
556 	case 20:
557 		return DA7219_AAD_JACK_INS_DEB_20MS;
558 	case 50:
559 		return DA7219_AAD_JACK_INS_DEB_50MS;
560 	case 100:
561 		return DA7219_AAD_JACK_INS_DEB_100MS;
562 	case 200:
563 		return DA7219_AAD_JACK_INS_DEB_200MS;
564 	case 500:
565 		return DA7219_AAD_JACK_INS_DEB_500MS;
566 	case 1000:
567 		return DA7219_AAD_JACK_INS_DEB_1S;
568 	default:
569 		dev_warn(dev, "Invalid jack insert debounce");
570 		return DA7219_AAD_JACK_INS_DEB_20MS;
571 	}
572 }
573 
574 static enum da7219_aad_jack_ins_det_pty
575 	da7219_aad_fw_jack_ins_det_pty(struct device *dev, const char *str)
576 {
577 	if (!strcmp(str, "low")) {
578 		return DA7219_AAD_JACK_INS_DET_PTY_LOW;
579 	} else if (!strcmp(str, "high")) {
580 		return DA7219_AAD_JACK_INS_DET_PTY_HIGH;
581 	} else {
582 		dev_warn(dev, "Invalid jack insertion detection polarity");
583 		return DA7219_AAD_JACK_INS_DET_PTY_LOW;
584 	}
585 }
586 
587 static enum da7219_aad_jack_det_rate
588 	da7219_aad_fw_jack_det_rate(struct device *dev, const char *str)
589 {
590 	if (!strcmp(str, "32_64")) {
591 		return DA7219_AAD_JACK_DET_RATE_32_64MS;
592 	} else if (!strcmp(str, "64_128")) {
593 		return DA7219_AAD_JACK_DET_RATE_64_128MS;
594 	} else if (!strcmp(str, "128_256")) {
595 		return DA7219_AAD_JACK_DET_RATE_128_256MS;
596 	} else if (!strcmp(str, "256_512")) {
597 		return DA7219_AAD_JACK_DET_RATE_256_512MS;
598 	} else {
599 		dev_warn(dev, "Invalid jack detect rate");
600 		return DA7219_AAD_JACK_DET_RATE_256_512MS;
601 	}
602 }
603 
604 static enum da7219_aad_jack_rem_deb
605 	da7219_aad_fw_jack_rem_deb(struct device *dev, u32 val)
606 {
607 	switch (val) {
608 	case 1:
609 		return DA7219_AAD_JACK_REM_DEB_1MS;
610 	case 5:
611 		return DA7219_AAD_JACK_REM_DEB_5MS;
612 	case 10:
613 		return DA7219_AAD_JACK_REM_DEB_10MS;
614 	case 20:
615 		return DA7219_AAD_JACK_REM_DEB_20MS;
616 	default:
617 		dev_warn(dev, "Invalid jack removal debounce");
618 		return DA7219_AAD_JACK_REM_DEB_1MS;
619 	}
620 }
621 
622 static enum da7219_aad_btn_avg
623 	da7219_aad_fw_btn_avg(struct device *dev, u32 val)
624 {
625 	switch (val) {
626 	case 1:
627 		return DA7219_AAD_BTN_AVG_1;
628 	case 2:
629 		return DA7219_AAD_BTN_AVG_2;
630 	case 4:
631 		return DA7219_AAD_BTN_AVG_4;
632 	case 8:
633 		return DA7219_AAD_BTN_AVG_8;
634 	default:
635 		dev_warn(dev, "Invalid button average value");
636 		return DA7219_AAD_BTN_AVG_2;
637 	}
638 }
639 
640 static enum da7219_aad_adc_1bit_rpt
641 	da7219_aad_fw_adc_1bit_rpt(struct device *dev, u32 val)
642 {
643 	switch (val) {
644 	case 1:
645 		return DA7219_AAD_ADC_1BIT_RPT_1;
646 	case 2:
647 		return DA7219_AAD_ADC_1BIT_RPT_2;
648 	case 4:
649 		return DA7219_AAD_ADC_1BIT_RPT_4;
650 	case 8:
651 		return DA7219_AAD_ADC_1BIT_RPT_8;
652 	default:
653 		dev_warn(dev, "Invalid ADC 1-bit repeat value");
654 		return DA7219_AAD_ADC_1BIT_RPT_1;
655 	}
656 }
657 
658 static struct da7219_aad_pdata *da7219_aad_fw_to_pdata(struct device *dev)
659 {
660 	struct i2c_client *i2c = to_i2c_client(dev);
661 	struct fwnode_handle *aad_np;
662 	struct da7219_aad_pdata *aad_pdata;
663 	const char *fw_str;
664 	u32 fw_val32;
665 
666 	aad_np = device_get_named_child_node(dev, "da7219_aad");
667 	if (!aad_np)
668 		return NULL;
669 
670 	aad_pdata = devm_kzalloc(dev, sizeof(*aad_pdata), GFP_KERNEL);
671 	if (!aad_pdata)
672 		return NULL;
673 
674 	aad_pdata->irq = i2c->irq;
675 
676 	if (fwnode_property_read_u32(aad_np, "dlg,micbias-pulse-lvl",
677 				     &fw_val32) >= 0)
678 		aad_pdata->micbias_pulse_lvl =
679 			da7219_aad_fw_micbias_pulse_lvl(dev, fw_val32);
680 	else
681 		aad_pdata->micbias_pulse_lvl = DA7219_AAD_MICBIAS_PULSE_LVL_OFF;
682 
683 	if (fwnode_property_read_u32(aad_np, "dlg,micbias-pulse-time",
684 				     &fw_val32) >= 0)
685 		aad_pdata->micbias_pulse_time = fw_val32;
686 
687 	if (fwnode_property_read_u32(aad_np, "dlg,btn-cfg", &fw_val32) >= 0)
688 		aad_pdata->btn_cfg = da7219_aad_fw_btn_cfg(dev, fw_val32);
689 	else
690 		aad_pdata->btn_cfg = DA7219_AAD_BTN_CFG_10MS;
691 
692 	if (fwnode_property_read_u32(aad_np, "dlg,mic-det-thr", &fw_val32) >= 0)
693 		aad_pdata->mic_det_thr =
694 			da7219_aad_fw_mic_det_thr(dev, fw_val32);
695 	else
696 		aad_pdata->mic_det_thr = DA7219_AAD_MIC_DET_THR_500_OHMS;
697 
698 	if (fwnode_property_read_u32(aad_np, "dlg,jack-ins-deb", &fw_val32) >= 0)
699 		aad_pdata->jack_ins_deb =
700 			da7219_aad_fw_jack_ins_deb(dev, fw_val32);
701 	else
702 		aad_pdata->jack_ins_deb = DA7219_AAD_JACK_INS_DEB_20MS;
703 
704 	if (!fwnode_property_read_string(aad_np, "dlg,jack-ins-det-pty", &fw_str))
705 		aad_pdata->jack_ins_det_pty =
706 			da7219_aad_fw_jack_ins_det_pty(dev, fw_str);
707 	else
708 		aad_pdata->jack_ins_det_pty = DA7219_AAD_JACK_INS_DET_PTY_LOW;
709 
710 	if (!fwnode_property_read_string(aad_np, "dlg,jack-det-rate", &fw_str))
711 		aad_pdata->jack_det_rate =
712 			da7219_aad_fw_jack_det_rate(dev, fw_str);
713 	else
714 		aad_pdata->jack_det_rate = DA7219_AAD_JACK_DET_RATE_256_512MS;
715 
716 	if (fwnode_property_read_u32(aad_np, "dlg,jack-rem-deb", &fw_val32) >= 0)
717 		aad_pdata->jack_rem_deb =
718 			da7219_aad_fw_jack_rem_deb(dev, fw_val32);
719 	else
720 		aad_pdata->jack_rem_deb = DA7219_AAD_JACK_REM_DEB_1MS;
721 
722 	if (fwnode_property_read_u32(aad_np, "dlg,a-d-btn-thr", &fw_val32) >= 0)
723 		aad_pdata->a_d_btn_thr = (u8) fw_val32;
724 	else
725 		aad_pdata->a_d_btn_thr = 0xA;
726 
727 	if (fwnode_property_read_u32(aad_np, "dlg,d-b-btn-thr", &fw_val32) >= 0)
728 		aad_pdata->d_b_btn_thr = (u8) fw_val32;
729 	else
730 		aad_pdata->d_b_btn_thr = 0x16;
731 
732 	if (fwnode_property_read_u32(aad_np, "dlg,b-c-btn-thr", &fw_val32) >= 0)
733 		aad_pdata->b_c_btn_thr = (u8) fw_val32;
734 	else
735 		aad_pdata->b_c_btn_thr = 0x21;
736 
737 	if (fwnode_property_read_u32(aad_np, "dlg,c-mic-btn-thr", &fw_val32) >= 0)
738 		aad_pdata->c_mic_btn_thr = (u8) fw_val32;
739 	else
740 		aad_pdata->c_mic_btn_thr = 0x3E;
741 
742 	if (fwnode_property_read_u32(aad_np, "dlg,btn-avg", &fw_val32) >= 0)
743 		aad_pdata->btn_avg = da7219_aad_fw_btn_avg(dev, fw_val32);
744 	else
745 		aad_pdata->btn_avg = DA7219_AAD_BTN_AVG_2;
746 
747 	if (fwnode_property_read_u32(aad_np, "dlg,adc-1bit-rpt", &fw_val32) >= 0)
748 		aad_pdata->adc_1bit_rpt =
749 			da7219_aad_fw_adc_1bit_rpt(dev, fw_val32);
750 	else
751 		aad_pdata->adc_1bit_rpt = DA7219_AAD_ADC_1BIT_RPT_1;
752 
753 	return aad_pdata;
754 }
755 
756 static void da7219_aad_handle_pdata(struct snd_soc_component *component)
757 {
758 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
759 	struct da7219_aad_priv *da7219_aad = da7219->aad;
760 	struct da7219_pdata *pdata = da7219->pdata;
761 
762 	if ((pdata) && (pdata->aad_pdata)) {
763 		struct da7219_aad_pdata *aad_pdata = pdata->aad_pdata;
764 		u8 cfg, mask;
765 
766 		da7219_aad->irq = aad_pdata->irq;
767 
768 		switch (aad_pdata->micbias_pulse_lvl) {
769 		case DA7219_AAD_MICBIAS_PULSE_LVL_2_8V:
770 		case DA7219_AAD_MICBIAS_PULSE_LVL_2_9V:
771 			da7219_aad->micbias_pulse_lvl =
772 				(aad_pdata->micbias_pulse_lvl <<
773 				 DA7219_MICBIAS1_LEVEL_SHIFT);
774 			break;
775 		default:
776 			break;
777 		}
778 
779 		da7219_aad->micbias_pulse_time = aad_pdata->micbias_pulse_time;
780 
781 		switch (aad_pdata->btn_cfg) {
782 		case DA7219_AAD_BTN_CFG_2MS:
783 		case DA7219_AAD_BTN_CFG_5MS:
784 		case DA7219_AAD_BTN_CFG_10MS:
785 		case DA7219_AAD_BTN_CFG_50MS:
786 		case DA7219_AAD_BTN_CFG_100MS:
787 		case DA7219_AAD_BTN_CFG_200MS:
788 		case DA7219_AAD_BTN_CFG_500MS:
789 			da7219_aad->btn_cfg  = (aad_pdata->btn_cfg <<
790 						DA7219_BUTTON_CONFIG_SHIFT);
791 		}
792 
793 		cfg = 0;
794 		mask = 0;
795 		switch (aad_pdata->mic_det_thr) {
796 		case DA7219_AAD_MIC_DET_THR_200_OHMS:
797 		case DA7219_AAD_MIC_DET_THR_500_OHMS:
798 		case DA7219_AAD_MIC_DET_THR_750_OHMS:
799 		case DA7219_AAD_MIC_DET_THR_1000_OHMS:
800 			cfg |= (aad_pdata->mic_det_thr <<
801 				DA7219_MIC_DET_THRESH_SHIFT);
802 			mask |= DA7219_MIC_DET_THRESH_MASK;
803 		}
804 		snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1, mask, cfg);
805 
806 		cfg = 0;
807 		mask = 0;
808 		switch (aad_pdata->jack_ins_deb) {
809 		case DA7219_AAD_JACK_INS_DEB_5MS:
810 		case DA7219_AAD_JACK_INS_DEB_10MS:
811 		case DA7219_AAD_JACK_INS_DEB_20MS:
812 		case DA7219_AAD_JACK_INS_DEB_50MS:
813 		case DA7219_AAD_JACK_INS_DEB_100MS:
814 		case DA7219_AAD_JACK_INS_DEB_200MS:
815 		case DA7219_AAD_JACK_INS_DEB_500MS:
816 		case DA7219_AAD_JACK_INS_DEB_1S:
817 			cfg |= (aad_pdata->jack_ins_deb <<
818 				DA7219_JACKDET_DEBOUNCE_SHIFT);
819 			mask |= DA7219_JACKDET_DEBOUNCE_MASK;
820 		}
821 		switch (aad_pdata->jack_det_rate) {
822 		case DA7219_AAD_JACK_DET_RATE_32_64MS:
823 		case DA7219_AAD_JACK_DET_RATE_64_128MS:
824 		case DA7219_AAD_JACK_DET_RATE_128_256MS:
825 		case DA7219_AAD_JACK_DET_RATE_256_512MS:
826 			cfg |= (aad_pdata->jack_det_rate <<
827 				DA7219_JACK_DETECT_RATE_SHIFT);
828 			mask |= DA7219_JACK_DETECT_RATE_MASK;
829 		}
830 		switch (aad_pdata->jack_rem_deb) {
831 		case DA7219_AAD_JACK_REM_DEB_1MS:
832 		case DA7219_AAD_JACK_REM_DEB_5MS:
833 		case DA7219_AAD_JACK_REM_DEB_10MS:
834 		case DA7219_AAD_JACK_REM_DEB_20MS:
835 			cfg |= (aad_pdata->jack_rem_deb <<
836 				DA7219_JACKDET_REM_DEB_SHIFT);
837 			mask |= DA7219_JACKDET_REM_DEB_MASK;
838 		}
839 		snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_2, mask, cfg);
840 
841 		snd_soc_component_write(component, DA7219_ACCDET_CONFIG_3,
842 			      aad_pdata->a_d_btn_thr);
843 		snd_soc_component_write(component, DA7219_ACCDET_CONFIG_4,
844 			      aad_pdata->d_b_btn_thr);
845 		snd_soc_component_write(component, DA7219_ACCDET_CONFIG_5,
846 			      aad_pdata->b_c_btn_thr);
847 		snd_soc_component_write(component, DA7219_ACCDET_CONFIG_6,
848 			      aad_pdata->c_mic_btn_thr);
849 
850 		cfg = 0;
851 		mask = 0;
852 		switch (aad_pdata->btn_avg) {
853 		case DA7219_AAD_BTN_AVG_1:
854 		case DA7219_AAD_BTN_AVG_2:
855 		case DA7219_AAD_BTN_AVG_4:
856 		case DA7219_AAD_BTN_AVG_8:
857 			cfg |= (aad_pdata->btn_avg <<
858 				DA7219_BUTTON_AVERAGE_SHIFT);
859 			mask |= DA7219_BUTTON_AVERAGE_MASK;
860 		}
861 		switch (aad_pdata->adc_1bit_rpt) {
862 		case DA7219_AAD_ADC_1BIT_RPT_1:
863 		case DA7219_AAD_ADC_1BIT_RPT_2:
864 		case DA7219_AAD_ADC_1BIT_RPT_4:
865 		case DA7219_AAD_ADC_1BIT_RPT_8:
866 			cfg |= (aad_pdata->adc_1bit_rpt <<
867 			       DA7219_ADC_1_BIT_REPEAT_SHIFT);
868 			mask |= DA7219_ADC_1_BIT_REPEAT_MASK;
869 		}
870 		snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_7, mask, cfg);
871 
872 		switch (aad_pdata->jack_ins_det_pty) {
873 		case DA7219_AAD_JACK_INS_DET_PTY_LOW:
874 			snd_soc_component_write(component, 0xF0, 0x8B);
875 			snd_soc_component_write(component, 0x75, 0x80);
876 			snd_soc_component_write(component, 0xF0, 0x00);
877 			break;
878 		case DA7219_AAD_JACK_INS_DET_PTY_HIGH:
879 			snd_soc_component_write(component, 0xF0, 0x8B);
880 			snd_soc_component_write(component, 0x75, 0x00);
881 			snd_soc_component_write(component, 0xF0, 0x00);
882 			break;
883 		default:
884 			break;
885 		}
886 	}
887 }
888 
889 static void da7219_aad_handle_gnd_switch_time(struct snd_soc_component *component)
890 {
891 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
892 	struct da7219_aad_priv *da7219_aad = da7219->aad;
893 	u8 jack_det;
894 
895 	jack_det = snd_soc_component_read(component, DA7219_ACCDET_CONFIG_2)
896 		& DA7219_JACK_DETECT_RATE_MASK;
897 	switch (jack_det) {
898 	case 0x00:
899 		da7219_aad->gnd_switch_delay = 32;
900 		break;
901 	case 0x10:
902 		da7219_aad->gnd_switch_delay = 64;
903 		break;
904 	case 0x20:
905 		da7219_aad->gnd_switch_delay = 128;
906 		break;
907 	case 0x30:
908 		da7219_aad->gnd_switch_delay = 256;
909 		break;
910 	default:
911 		da7219_aad->gnd_switch_delay = 32;
912 		break;
913 	}
914 }
915 
916 /*
917  * Suspend/Resume
918  */
919 
920 void da7219_aad_suspend(struct snd_soc_component *component)
921 {
922 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
923 	struct da7219_aad_priv *da7219_aad = da7219->aad;
924 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
925 	u8 micbias_ctrl;
926 
927 	if (da7219_aad->jack) {
928 		/* Disable jack detection during suspend */
929 		snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1,
930 				    DA7219_ACCDET_EN_MASK, 0);
931 
932 		/*
933 		 * If we have a 4-pole jack inserted, then micbias will be
934 		 * enabled. We can disable micbias here, and keep a note to
935 		 * re-enable it on resume. If jack removal occurred during
936 		 * suspend then this will be dealt with through the IRQ handler.
937 		 */
938 		if (da7219_aad->jack_inserted) {
939 			micbias_ctrl = snd_soc_component_read(component, DA7219_MICBIAS_CTRL);
940 			if (micbias_ctrl & DA7219_MICBIAS1_EN_MASK) {
941 				snd_soc_dapm_disable_pin(dapm, "Mic Bias");
942 				snd_soc_dapm_sync(dapm);
943 				da7219_aad->micbias_resume_enable = true;
944 			}
945 		}
946 	}
947 }
948 
949 void da7219_aad_resume(struct snd_soc_component *component)
950 {
951 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
952 	struct da7219_aad_priv *da7219_aad = da7219->aad;
953 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
954 
955 	if (da7219_aad->jack) {
956 		/* Re-enable micbias if previously enabled for 4-pole jack */
957 		if (da7219_aad->jack_inserted &&
958 		    da7219_aad->micbias_resume_enable) {
959 			snd_soc_dapm_force_enable_pin(dapm, "Mic Bias");
960 			snd_soc_dapm_sync(dapm);
961 			da7219_aad->micbias_resume_enable = false;
962 		}
963 
964 		/* Re-enable jack detection */
965 		snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1,
966 				    DA7219_ACCDET_EN_MASK,
967 				    DA7219_ACCDET_EN_MASK);
968 	}
969 }
970 
971 
972 /*
973  * Init/Exit
974  */
975 
976 int da7219_aad_init(struct snd_soc_component *component)
977 {
978 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
979 	struct da7219_aad_priv *da7219_aad = da7219->aad;
980 	u8 mask[DA7219_AAD_IRQ_REG_MAX];
981 	int ret;
982 
983 	da7219_aad->component = component;
984 
985 	/* Handle any DT/ACPI/platform data */
986 	da7219_aad_handle_pdata(component);
987 
988 	/* Disable button detection */
989 	snd_soc_component_update_bits(component, DA7219_ACCDET_CONFIG_1,
990 			    DA7219_BUTTON_CONFIG_MASK, 0);
991 
992 	da7219_aad_handle_gnd_switch_time(component);
993 
994 	da7219_aad->aad_wq = create_singlethread_workqueue("da7219-aad");
995 	if (!da7219_aad->aad_wq) {
996 		dev_err(component->dev, "Failed to create aad workqueue\n");
997 		return -ENOMEM;
998 	}
999 
1000 	INIT_DELAYED_WORK(&da7219_aad->jack_det_work, da7219_aad_jack_det_work);
1001 	INIT_WORK(&da7219_aad->btn_det_work, da7219_aad_btn_det_work);
1002 	INIT_WORK(&da7219_aad->hptest_work, da7219_aad_hptest_work);
1003 
1004 	ret = request_threaded_irq(da7219_aad->irq, NULL,
1005 				   da7219_aad_irq_thread,
1006 				   IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1007 				   "da7219-aad", da7219_aad);
1008 	if (ret) {
1009 		dev_err(component->dev, "Failed to request IRQ: %d\n", ret);
1010 		return ret;
1011 	}
1012 
1013 	/* Unmask AAD IRQs */
1014 	memset(mask, 0, DA7219_AAD_IRQ_REG_MAX);
1015 	regmap_bulk_write(da7219->regmap, DA7219_ACCDET_IRQ_MASK_A,
1016 			  &mask, DA7219_AAD_IRQ_REG_MAX);
1017 
1018 	return 0;
1019 }
1020 
1021 void da7219_aad_exit(struct snd_soc_component *component)
1022 {
1023 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
1024 	struct da7219_aad_priv *da7219_aad = da7219->aad;
1025 	u8 mask[DA7219_AAD_IRQ_REG_MAX];
1026 
1027 	/* Mask off AAD IRQs */
1028 	memset(mask, DA7219_BYTE_MASK, DA7219_AAD_IRQ_REG_MAX);
1029 	regmap_bulk_write(da7219->regmap, DA7219_ACCDET_IRQ_MASK_A,
1030 			  mask, DA7219_AAD_IRQ_REG_MAX);
1031 
1032 	free_irq(da7219_aad->irq, da7219_aad);
1033 
1034 	cancel_delayed_work_sync(&da7219_aad->jack_det_work);
1035 	cancel_work_sync(&da7219_aad->btn_det_work);
1036 	cancel_work_sync(&da7219_aad->hptest_work);
1037 	destroy_workqueue(da7219_aad->aad_wq);
1038 }
1039 
1040 /*
1041  * AAD related I2C probe handling
1042  */
1043 
1044 int da7219_aad_probe(struct i2c_client *i2c)
1045 {
1046 	struct da7219_priv *da7219 = i2c_get_clientdata(i2c);
1047 	struct device *dev = &i2c->dev;
1048 	struct da7219_aad_priv *da7219_aad;
1049 
1050 	da7219_aad = devm_kzalloc(dev, sizeof(*da7219_aad), GFP_KERNEL);
1051 	if (!da7219_aad)
1052 		return -ENOMEM;
1053 
1054 	da7219->aad = da7219_aad;
1055 
1056 	/* Retrieve any DT/ACPI/platform data */
1057 	if (da7219->pdata && !da7219->pdata->aad_pdata)
1058 		da7219->pdata->aad_pdata = da7219_aad_fw_to_pdata(dev);
1059 
1060 	return 0;
1061 }
1062 
1063 MODULE_DESCRIPTION("ASoC DA7219 AAD Driver");
1064 MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
1065 MODULE_AUTHOR("David Rau <David.Rau.opensource@dm.renesas.com>");
1066 MODULE_LICENSE("GPL");
1067