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