ASoC: arizona: Free speaker thermal IRQs in CODEC remove
[linux/fpc-iii.git] / sound / soc / codecs / da7219-aad.c
blob9459593eef13d5eb0e56c88a66fa390c0cdf9a53
1 /*
2 * da7219-aad.c - Dialog DA7219 ALSA SoC AAD Driver
4 * Copyright (c) 2015 Dialog Semiconductor Ltd.
6 * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
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.
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/of_device.h>
17 #include <linux/of_irq.h>
18 #include <linux/pm_wakeirq.h>
19 #include <linux/slab.h>
20 #include <linux/delay.h>
21 #include <linux/workqueue.h>
22 #include <sound/soc.h>
23 #include <sound/jack.h>
24 #include <sound/da7219.h>
26 #include "da7219.h"
27 #include "da7219-aad.h"
31 * Detection control
34 void da7219_aad_jack_det(struct snd_soc_codec *codec, struct snd_soc_jack *jack)
36 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
38 da7219->aad->jack = jack;
39 da7219->aad->jack_inserted = false;
41 /* Send an initial empty report */
42 snd_soc_jack_report(jack, 0, DA7219_AAD_REPORT_ALL_MASK);
44 /* Enable/Disable jack detection */
45 snd_soc_update_bits(codec, DA7219_ACCDET_CONFIG_1,
46 DA7219_ACCDET_EN_MASK,
47 (jack ? DA7219_ACCDET_EN_MASK : 0));
49 EXPORT_SYMBOL_GPL(da7219_aad_jack_det);
52 * Button/HPTest work
55 static void da7219_aad_btn_det_work(struct work_struct *work)
57 struct da7219_aad_priv *da7219_aad =
58 container_of(work, struct da7219_aad_priv, btn_det_work);
59 struct snd_soc_codec *codec = da7219_aad->codec;
60 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
61 u8 statusa, micbias_ctrl;
62 bool micbias_up = false;
63 int retries = 0;
65 /* Drive headphones/lineout */
66 snd_soc_update_bits(codec, DA7219_HP_L_CTRL,
67 DA7219_HP_L_AMP_OE_MASK,
68 DA7219_HP_L_AMP_OE_MASK);
69 snd_soc_update_bits(codec, DA7219_HP_R_CTRL,
70 DA7219_HP_R_AMP_OE_MASK,
71 DA7219_HP_R_AMP_OE_MASK);
73 /* Make sure mic bias is up */
74 snd_soc_dapm_force_enable_pin(dapm, "Mic Bias");
75 snd_soc_dapm_sync(dapm);
77 do {
78 statusa = snd_soc_read(codec, 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));
85 if (retries >= DA7219_AAD_MICBIAS_CHK_RETRIES)
86 dev_warn(codec->dev, "Mic bias status check timed out");
89 * Mic bias pulse required to enable mic, must be done before enabling
90 * button detection to prevent erroneous button readings.
92 if (da7219_aad->micbias_pulse_lvl && da7219_aad->micbias_pulse_time) {
93 /* Pulse higher level voltage */
94 micbias_ctrl = snd_soc_read(codec, DA7219_MICBIAS_CTRL);
95 snd_soc_update_bits(codec, DA7219_MICBIAS_CTRL,
96 DA7219_MICBIAS1_LEVEL_MASK,
97 da7219_aad->micbias_pulse_lvl);
98 msleep(da7219_aad->micbias_pulse_time);
99 snd_soc_write(codec, DA7219_MICBIAS_CTRL, micbias_ctrl);
103 snd_soc_update_bits(codec, DA7219_ACCDET_CONFIG_1,
104 DA7219_BUTTON_CONFIG_MASK,
105 da7219_aad->btn_cfg);
108 static void da7219_aad_hptest_work(struct work_struct *work)
110 struct da7219_aad_priv *da7219_aad =
111 container_of(work, struct da7219_aad_priv, hptest_work);
112 struct snd_soc_codec *codec = da7219_aad->codec;
113 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
114 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
116 u16 tonegen_freq_hptest;
117 u8 accdet_cfg8;
118 int report = 0;
120 /* Lock DAPM and any Kcontrols that are affected by this test */
121 snd_soc_dapm_mutex_lock(dapm);
122 mutex_lock(&da7219->lock);
124 /* Bypass cache so it saves current settings */
125 regcache_cache_bypass(da7219->regmap, true);
127 /* Make sure Tone Generator is disabled */
128 snd_soc_write(codec, DA7219_TONE_GEN_CFG1, 0);
130 /* Enable HPTest block, 1KOhms check */
131 snd_soc_update_bits(codec, DA7219_ACCDET_CONFIG_8,
132 DA7219_HPTEST_EN_MASK | DA7219_HPTEST_RES_SEL_MASK,
133 DA7219_HPTEST_EN_MASK |
134 DA7219_HPTEST_RES_SEL_1KOHMS);
136 /* Set gains to 0db */
137 snd_soc_write(codec, DA7219_DAC_L_GAIN, DA7219_DAC_DIGITAL_GAIN_0DB);
138 snd_soc_write(codec, DA7219_DAC_R_GAIN, DA7219_DAC_DIGITAL_GAIN_0DB);
139 snd_soc_write(codec, DA7219_HP_L_GAIN, DA7219_HP_AMP_GAIN_0DB);
140 snd_soc_write(codec, DA7219_HP_R_GAIN, DA7219_HP_AMP_GAIN_0DB);
142 /* Disable DAC filters, EQs and soft mute */
143 snd_soc_update_bits(codec, DA7219_DAC_FILTERS1, DA7219_HPF_MODE_MASK,
145 snd_soc_update_bits(codec, DA7219_DAC_FILTERS4, DA7219_DAC_EQ_EN_MASK,
147 snd_soc_update_bits(codec, DA7219_DAC_FILTERS5,
148 DA7219_DAC_SOFTMUTE_EN_MASK, 0);
150 /* Enable HP left & right paths */
151 snd_soc_update_bits(codec, DA7219_CP_CTRL, DA7219_CP_EN_MASK,
152 DA7219_CP_EN_MASK);
153 snd_soc_update_bits(codec, DA7219_DIG_ROUTING_DAC,
154 DA7219_DAC_L_SRC_MASK | DA7219_DAC_R_SRC_MASK,
155 DA7219_DAC_L_SRC_TONEGEN |
156 DA7219_DAC_R_SRC_TONEGEN);
157 snd_soc_update_bits(codec, DA7219_DAC_L_CTRL,
158 DA7219_DAC_L_EN_MASK | DA7219_DAC_L_MUTE_EN_MASK,
159 DA7219_DAC_L_EN_MASK);
160 snd_soc_update_bits(codec, DA7219_DAC_R_CTRL,
161 DA7219_DAC_R_EN_MASK | DA7219_DAC_R_MUTE_EN_MASK,
162 DA7219_DAC_R_EN_MASK);
163 snd_soc_update_bits(codec, DA7219_MIXOUT_L_SELECT,
164 DA7219_MIXOUT_L_MIX_SELECT_MASK,
165 DA7219_MIXOUT_L_MIX_SELECT_MASK);
166 snd_soc_update_bits(codec, DA7219_MIXOUT_R_SELECT,
167 DA7219_MIXOUT_R_MIX_SELECT_MASK,
168 DA7219_MIXOUT_R_MIX_SELECT_MASK);
169 snd_soc_update_bits(codec, DA7219_DROUTING_ST_OUTFILT_1L,
170 DA7219_OUTFILT_ST_1L_SRC_MASK,
171 DA7219_DMIX_ST_SRC_OUTFILT1L);
172 snd_soc_update_bits(codec, DA7219_DROUTING_ST_OUTFILT_1R,
173 DA7219_OUTFILT_ST_1R_SRC_MASK,
174 DA7219_DMIX_ST_SRC_OUTFILT1R);
175 snd_soc_update_bits(codec, DA7219_MIXOUT_L_CTRL,
176 DA7219_MIXOUT_L_AMP_EN_MASK,
177 DA7219_MIXOUT_L_AMP_EN_MASK);
178 snd_soc_update_bits(codec, DA7219_MIXOUT_R_CTRL,
179 DA7219_MIXOUT_R_AMP_EN_MASK,
180 DA7219_MIXOUT_R_AMP_EN_MASK);
181 snd_soc_write(codec, DA7219_HP_L_CTRL,
182 DA7219_HP_L_AMP_OE_MASK | DA7219_HP_L_AMP_EN_MASK);
183 snd_soc_write(codec, DA7219_HP_R_CTRL,
184 DA7219_HP_R_AMP_OE_MASK | DA7219_HP_R_AMP_EN_MASK);
186 /* Configure & start Tone Generator */
187 snd_soc_write(codec, DA7219_TONE_GEN_ON_PER, DA7219_BEEP_ON_PER_MASK);
188 tonegen_freq_hptest = cpu_to_le16(DA7219_AAD_HPTEST_RAMP_FREQ);
189 regmap_raw_write(da7219->regmap, DA7219_TONE_GEN_FREQ1_L,
190 &tonegen_freq_hptest, sizeof(tonegen_freq_hptest));
191 snd_soc_update_bits(codec, DA7219_TONE_GEN_CFG2,
192 DA7219_SWG_SEL_MASK | DA7219_TONE_GEN_GAIN_MASK,
193 DA7219_SWG_SEL_SRAMP |
194 DA7219_TONE_GEN_GAIN_MINUS_15DB);
195 snd_soc_write(codec, DA7219_TONE_GEN_CFG1, DA7219_START_STOPN_MASK);
197 msleep(DA7219_AAD_HPTEST_PERIOD);
199 /* Grab comparator reading */
200 accdet_cfg8 = snd_soc_read(codec, DA7219_ACCDET_CONFIG_8);
201 if (accdet_cfg8 & DA7219_HPTEST_COMP_MASK)
202 report |= SND_JACK_HEADPHONE;
203 else
204 report |= SND_JACK_LINEOUT;
206 /* Stop tone generator */
207 snd_soc_write(codec, DA7219_TONE_GEN_CFG1, 0);
209 msleep(DA7219_AAD_HPTEST_PERIOD);
211 /* Restore original settings from cache */
212 regcache_mark_dirty(da7219->regmap);
213 regcache_sync_region(da7219->regmap, DA7219_HP_L_CTRL,
214 DA7219_HP_R_CTRL);
215 regcache_sync_region(da7219->regmap, DA7219_MIXOUT_L_CTRL,
216 DA7219_MIXOUT_R_CTRL);
217 regcache_sync_region(da7219->regmap, DA7219_DROUTING_ST_OUTFILT_1L,
218 DA7219_DROUTING_ST_OUTFILT_1R);
219 regcache_sync_region(da7219->regmap, DA7219_MIXOUT_L_SELECT,
220 DA7219_MIXOUT_R_SELECT);
221 regcache_sync_region(da7219->regmap, DA7219_DAC_L_CTRL,
222 DA7219_DAC_R_CTRL);
223 regcache_sync_region(da7219->regmap, DA7219_DIG_ROUTING_DAC,
224 DA7219_DIG_ROUTING_DAC);
225 regcache_sync_region(da7219->regmap, DA7219_CP_CTRL, DA7219_CP_CTRL);
226 regcache_sync_region(da7219->regmap, DA7219_DAC_FILTERS5,
227 DA7219_DAC_FILTERS5);
228 regcache_sync_region(da7219->regmap, DA7219_DAC_FILTERS4,
229 DA7219_DAC_FILTERS1);
230 regcache_sync_region(da7219->regmap, DA7219_HP_L_GAIN,
231 DA7219_HP_R_GAIN);
232 regcache_sync_region(da7219->regmap, DA7219_DAC_L_GAIN,
233 DA7219_DAC_R_GAIN);
234 regcache_sync_region(da7219->regmap, DA7219_TONE_GEN_ON_PER,
235 DA7219_TONE_GEN_ON_PER);
236 regcache_sync_region(da7219->regmap, DA7219_TONE_GEN_FREQ1_L,
237 DA7219_TONE_GEN_FREQ1_U);
238 regcache_sync_region(da7219->regmap, DA7219_TONE_GEN_CFG1,
239 DA7219_TONE_GEN_CFG2);
241 regcache_cache_bypass(da7219->regmap, false);
243 /* Disable HPTest block */
244 snd_soc_update_bits(codec, DA7219_ACCDET_CONFIG_8,
245 DA7219_HPTEST_EN_MASK, 0);
247 /* Drive Headphones/lineout */
248 snd_soc_update_bits(codec, DA7219_HP_L_CTRL, DA7219_HP_L_AMP_OE_MASK,
249 DA7219_HP_L_AMP_OE_MASK);
250 snd_soc_update_bits(codec, DA7219_HP_R_CTRL, DA7219_HP_R_AMP_OE_MASK,
251 DA7219_HP_R_AMP_OE_MASK);
253 mutex_unlock(&da7219->lock);
254 snd_soc_dapm_mutex_unlock(dapm);
257 * Only send report if jack hasn't been removed during process,
258 * otherwise it's invalid and we drop it.
260 if (da7219_aad->jack_inserted)
261 snd_soc_jack_report(da7219_aad->jack, report,
262 SND_JACK_HEADSET | SND_JACK_LINEOUT);
267 * IRQ
270 static irqreturn_t da7219_aad_irq_thread(int irq, void *data)
272 struct da7219_aad_priv *da7219_aad = data;
273 struct snd_soc_codec *codec = da7219_aad->codec;
274 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
275 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
276 u8 events[DA7219_AAD_IRQ_REG_MAX];
277 u8 statusa;
278 int i, report = 0, mask = 0;
280 /* Read current IRQ events */
281 regmap_bulk_read(da7219->regmap, DA7219_ACCDET_IRQ_EVENT_A,
282 events, DA7219_AAD_IRQ_REG_MAX);
284 if (!events[DA7219_AAD_IRQ_REG_A] && !events[DA7219_AAD_IRQ_REG_B])
285 return IRQ_NONE;
287 /* Read status register for jack insertion & type status */
288 statusa = snd_soc_read(codec, DA7219_ACCDET_STATUS_A);
290 /* Clear events */
291 regmap_bulk_write(da7219->regmap, DA7219_ACCDET_IRQ_EVENT_A,
292 events, DA7219_AAD_IRQ_REG_MAX);
294 dev_dbg(codec->dev, "IRQ events = 0x%x|0x%x, status = 0x%x\n",
295 events[DA7219_AAD_IRQ_REG_A], events[DA7219_AAD_IRQ_REG_B],
296 statusa);
298 if (statusa & DA7219_JACK_INSERTION_STS_MASK) {
299 /* Jack Insertion */
300 if (events[DA7219_AAD_IRQ_REG_A] &
301 DA7219_E_JACK_INSERTED_MASK) {
302 report |= SND_JACK_MECHANICAL;
303 mask |= SND_JACK_MECHANICAL;
304 da7219_aad->jack_inserted = true;
307 /* Jack type detection */
308 if (events[DA7219_AAD_IRQ_REG_A] &
309 DA7219_E_JACK_DETECT_COMPLETE_MASK) {
311 * If 4-pole, then enable button detection, else perform
312 * HP impedance test to determine output type to report.
314 * We schedule work here as the tasks themselves can
315 * take time to complete, and in particular for hptest
316 * we want to be able to check if the jack was removed
317 * during the procedure as this will invalidate the
318 * result. By doing this as work, the IRQ thread can
319 * handle a removal, and we can check at the end of
320 * hptest if we have a valid result or not.
322 if (statusa & DA7219_JACK_TYPE_STS_MASK) {
323 report |= SND_JACK_HEADSET;
324 mask |= SND_JACK_HEADSET | SND_JACK_LINEOUT;
325 schedule_work(&da7219_aad->btn_det_work);
326 } else {
327 schedule_work(&da7219_aad->hptest_work);
331 /* Button support for 4-pole jack */
332 if (statusa & DA7219_JACK_TYPE_STS_MASK) {
333 for (i = 0; i < DA7219_AAD_MAX_BUTTONS; ++i) {
334 /* Button Press */
335 if (events[DA7219_AAD_IRQ_REG_B] &
336 (DA7219_E_BUTTON_A_PRESSED_MASK << i)) {
337 report |= SND_JACK_BTN_0 >> i;
338 mask |= SND_JACK_BTN_0 >> i;
341 snd_soc_jack_report(da7219_aad->jack, report, mask);
343 for (i = 0; i < DA7219_AAD_MAX_BUTTONS; ++i) {
344 /* Button Release */
345 if (events[DA7219_AAD_IRQ_REG_B] &
346 (DA7219_E_BUTTON_A_RELEASED_MASK >> i)) {
347 report &= ~(SND_JACK_BTN_0 >> i);
348 mask |= SND_JACK_BTN_0 >> i;
352 } else {
353 /* Jack removal */
354 if (events[DA7219_AAD_IRQ_REG_A] & DA7219_E_JACK_REMOVED_MASK) {
355 report = 0;
356 mask |= DA7219_AAD_REPORT_ALL_MASK;
357 da7219_aad->jack_inserted = false;
359 /* Un-drive headphones/lineout */
360 snd_soc_update_bits(codec, DA7219_HP_R_CTRL,
361 DA7219_HP_R_AMP_OE_MASK, 0);
362 snd_soc_update_bits(codec, DA7219_HP_L_CTRL,
363 DA7219_HP_L_AMP_OE_MASK, 0);
365 /* Ensure button detection disabled */
366 snd_soc_update_bits(codec, DA7219_ACCDET_CONFIG_1,
367 DA7219_BUTTON_CONFIG_MASK, 0);
369 /* Disable mic bias */
370 snd_soc_dapm_disable_pin(dapm, "Mic Bias");
371 snd_soc_dapm_sync(dapm);
373 /* Cancel any pending work */
374 cancel_work_sync(&da7219_aad->btn_det_work);
375 cancel_work_sync(&da7219_aad->hptest_work);
379 snd_soc_jack_report(da7219_aad->jack, report, mask);
381 return IRQ_HANDLED;
385 * DT to pdata conversion
388 static enum da7219_aad_micbias_pulse_lvl
389 da7219_aad_of_micbias_pulse_lvl(struct snd_soc_codec *codec, u32 val)
391 switch (val) {
392 case 2800:
393 return DA7219_AAD_MICBIAS_PULSE_LVL_2_8V;
394 case 2900:
395 return DA7219_AAD_MICBIAS_PULSE_LVL_2_9V;
396 default:
397 dev_warn(codec->dev, "Invalid micbias pulse level");
398 return DA7219_AAD_MICBIAS_PULSE_LVL_OFF;
402 static enum da7219_aad_btn_cfg
403 da7219_aad_of_btn_cfg(struct snd_soc_codec *codec, u32 val)
405 switch (val) {
406 case 2:
407 return DA7219_AAD_BTN_CFG_2MS;
408 case 5:
409 return DA7219_AAD_BTN_CFG_5MS;
410 case 10:
411 return DA7219_AAD_BTN_CFG_10MS;
412 case 50:
413 return DA7219_AAD_BTN_CFG_50MS;
414 case 100:
415 return DA7219_AAD_BTN_CFG_100MS;
416 case 200:
417 return DA7219_AAD_BTN_CFG_200MS;
418 case 500:
419 return DA7219_AAD_BTN_CFG_500MS;
420 default:
421 dev_warn(codec->dev, "Invalid button config");
422 return DA7219_AAD_BTN_CFG_10MS;
426 static enum da7219_aad_mic_det_thr
427 da7219_aad_of_mic_det_thr(struct snd_soc_codec *codec, u32 val)
429 switch (val) {
430 case 200:
431 return DA7219_AAD_MIC_DET_THR_200_OHMS;
432 case 500:
433 return DA7219_AAD_MIC_DET_THR_500_OHMS;
434 case 750:
435 return DA7219_AAD_MIC_DET_THR_750_OHMS;
436 case 1000:
437 return DA7219_AAD_MIC_DET_THR_1000_OHMS;
438 default:
439 dev_warn(codec->dev, "Invalid mic detect threshold");
440 return DA7219_AAD_MIC_DET_THR_500_OHMS;
444 static enum da7219_aad_jack_ins_deb
445 da7219_aad_of_jack_ins_deb(struct snd_soc_codec *codec, u32 val)
447 switch (val) {
448 case 5:
449 return DA7219_AAD_JACK_INS_DEB_5MS;
450 case 10:
451 return DA7219_AAD_JACK_INS_DEB_10MS;
452 case 20:
453 return DA7219_AAD_JACK_INS_DEB_20MS;
454 case 50:
455 return DA7219_AAD_JACK_INS_DEB_50MS;
456 case 100:
457 return DA7219_AAD_JACK_INS_DEB_100MS;
458 case 200:
459 return DA7219_AAD_JACK_INS_DEB_200MS;
460 case 500:
461 return DA7219_AAD_JACK_INS_DEB_500MS;
462 case 1000:
463 return DA7219_AAD_JACK_INS_DEB_1S;
464 default:
465 dev_warn(codec->dev, "Invalid jack insert debounce");
466 return DA7219_AAD_JACK_INS_DEB_20MS;
470 static enum da7219_aad_jack_det_rate
471 da7219_aad_of_jack_det_rate(struct snd_soc_codec *codec, const char *str)
473 if (!strcmp(str, "32ms_64ms")) {
474 return DA7219_AAD_JACK_DET_RATE_32_64MS;
475 } else if (!strcmp(str, "64ms_128ms")) {
476 return DA7219_AAD_JACK_DET_RATE_64_128MS;
477 } else if (!strcmp(str, "128ms_256ms")) {
478 return DA7219_AAD_JACK_DET_RATE_128_256MS;
479 } else if (!strcmp(str, "256ms_512ms")) {
480 return DA7219_AAD_JACK_DET_RATE_256_512MS;
481 } else {
482 dev_warn(codec->dev, "Invalid jack detect rate");
483 return DA7219_AAD_JACK_DET_RATE_256_512MS;
487 static enum da7219_aad_jack_rem_deb
488 da7219_aad_of_jack_rem_deb(struct snd_soc_codec *codec, u32 val)
490 switch (val) {
491 case 1:
492 return DA7219_AAD_JACK_REM_DEB_1MS;
493 case 5:
494 return DA7219_AAD_JACK_REM_DEB_5MS;
495 case 10:
496 return DA7219_AAD_JACK_REM_DEB_10MS;
497 case 20:
498 return DA7219_AAD_JACK_REM_DEB_20MS;
499 default:
500 dev_warn(codec->dev, "Invalid jack removal debounce");
501 return DA7219_AAD_JACK_REM_DEB_1MS;
505 static enum da7219_aad_btn_avg
506 da7219_aad_of_btn_avg(struct snd_soc_codec *codec, u32 val)
508 switch (val) {
509 case 1:
510 return DA7219_AAD_BTN_AVG_1;
511 case 2:
512 return DA7219_AAD_BTN_AVG_2;
513 case 4:
514 return DA7219_AAD_BTN_AVG_4;
515 case 8:
516 return DA7219_AAD_BTN_AVG_8;
517 default:
518 dev_warn(codec->dev, "Invalid button average value");
519 return DA7219_AAD_BTN_AVG_2;
523 static enum da7219_aad_adc_1bit_rpt
524 da7219_aad_of_adc_1bit_rpt(struct snd_soc_codec *codec, u32 val)
526 switch (val) {
527 case 1:
528 return DA7219_AAD_ADC_1BIT_RPT_1;
529 case 2:
530 return DA7219_AAD_ADC_1BIT_RPT_2;
531 case 4:
532 return DA7219_AAD_ADC_1BIT_RPT_4;
533 case 8:
534 return DA7219_AAD_ADC_1BIT_RPT_8;
535 default:
536 dev_warn(codec->dev, "Invalid ADC 1-bit repeat value");
537 return DA7219_AAD_ADC_1BIT_RPT_1;
541 static struct da7219_aad_pdata *da7219_aad_of_to_pdata(struct snd_soc_codec *codec)
543 struct device_node *np = codec->dev->of_node;
544 struct device_node *aad_np = of_find_node_by_name(np, "da7219_aad");
545 struct da7219_aad_pdata *aad_pdata;
546 const char *of_str;
547 u32 of_val32;
549 if (!aad_np)
550 return NULL;
552 aad_pdata = devm_kzalloc(codec->dev, sizeof(*aad_pdata), GFP_KERNEL);
553 if (!aad_pdata)
554 goto out;
556 aad_pdata->irq = irq_of_parse_and_map(np, 0);
558 if (of_property_read_u32(aad_np, "dlg,micbias-pulse-lvl",
559 &of_val32) >= 0)
560 aad_pdata->micbias_pulse_lvl =
561 da7219_aad_of_micbias_pulse_lvl(codec, of_val32);
562 else
563 aad_pdata->micbias_pulse_lvl = DA7219_AAD_MICBIAS_PULSE_LVL_OFF;
565 if (of_property_read_u32(aad_np, "dlg,micbias-pulse-time",
566 &of_val32) >= 0)
567 aad_pdata->micbias_pulse_time = of_val32;
569 if (of_property_read_u32(aad_np, "dlg,btn-cfg", &of_val32) >= 0)
570 aad_pdata->btn_cfg = da7219_aad_of_btn_cfg(codec, of_val32);
571 else
572 aad_pdata->btn_cfg = DA7219_AAD_BTN_CFG_10MS;
574 if (of_property_read_u32(aad_np, "dlg,mic-det-thr", &of_val32) >= 0)
575 aad_pdata->mic_det_thr =
576 da7219_aad_of_mic_det_thr(codec, of_val32);
577 else
578 aad_pdata->mic_det_thr = DA7219_AAD_MIC_DET_THR_500_OHMS;
580 if (of_property_read_u32(aad_np, "dlg,jack-ins-deb", &of_val32) >= 0)
581 aad_pdata->jack_ins_deb =
582 da7219_aad_of_jack_ins_deb(codec, of_val32);
583 else
584 aad_pdata->jack_ins_deb = DA7219_AAD_JACK_INS_DEB_20MS;
586 if (!of_property_read_string(aad_np, "dlg,jack-det-rate", &of_str))
587 aad_pdata->jack_det_rate =
588 da7219_aad_of_jack_det_rate(codec, of_str);
589 else
590 aad_pdata->jack_det_rate = DA7219_AAD_JACK_DET_RATE_256_512MS;
592 if (of_property_read_u32(aad_np, "dlg,jack-rem-deb", &of_val32) >= 0)
593 aad_pdata->jack_rem_deb =
594 da7219_aad_of_jack_rem_deb(codec, of_val32);
595 else
596 aad_pdata->jack_rem_deb = DA7219_AAD_JACK_REM_DEB_1MS;
598 if (of_property_read_u32(aad_np, "dlg,a-d-btn-thr", &of_val32) >= 0)
599 aad_pdata->a_d_btn_thr = (u8) of_val32;
600 else
601 aad_pdata->a_d_btn_thr = 0xA;
603 if (of_property_read_u32(aad_np, "dlg,d-b-btn-thr", &of_val32) >= 0)
604 aad_pdata->d_b_btn_thr = (u8) of_val32;
605 else
606 aad_pdata->d_b_btn_thr = 0x16;
608 if (of_property_read_u32(aad_np, "dlg,b-c-btn-thr", &of_val32) >= 0)
609 aad_pdata->b_c_btn_thr = (u8) of_val32;
610 else
611 aad_pdata->b_c_btn_thr = 0x21;
613 if (of_property_read_u32(aad_np, "dlg,c-mic-btn-thr", &of_val32) >= 0)
614 aad_pdata->c_mic_btn_thr = (u8) of_val32;
615 else
616 aad_pdata->c_mic_btn_thr = 0x3E;
618 if (of_property_read_u32(aad_np, "dlg,btn-avg", &of_val32) >= 0)
619 aad_pdata->btn_avg = da7219_aad_of_btn_avg(codec, of_val32);
620 else
621 aad_pdata->btn_avg = DA7219_AAD_BTN_AVG_2;
623 if (of_property_read_u32(aad_np, "dlg,adc-1bit-rpt", &of_val32) >= 0)
624 aad_pdata->adc_1bit_rpt =
625 da7219_aad_of_adc_1bit_rpt(codec, of_val32);
626 else
627 aad_pdata->adc_1bit_rpt = DA7219_AAD_ADC_1BIT_RPT_1;
629 out:
630 of_node_put(aad_np);
632 return aad_pdata;
635 static void da7219_aad_handle_pdata(struct snd_soc_codec *codec)
637 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
638 struct da7219_aad_priv *da7219_aad = da7219->aad;
639 struct da7219_pdata *pdata = da7219->pdata;
641 if ((pdata) && (pdata->aad_pdata)) {
642 struct da7219_aad_pdata *aad_pdata = pdata->aad_pdata;
643 u8 cfg, mask;
645 da7219_aad->irq = aad_pdata->irq;
647 switch (aad_pdata->micbias_pulse_lvl) {
648 case DA7219_AAD_MICBIAS_PULSE_LVL_2_8V:
649 case DA7219_AAD_MICBIAS_PULSE_LVL_2_9V:
650 da7219_aad->micbias_pulse_lvl =
651 (aad_pdata->micbias_pulse_lvl <<
652 DA7219_MICBIAS1_LEVEL_SHIFT);
653 break;
654 default:
655 break;
658 da7219_aad->micbias_pulse_time = aad_pdata->micbias_pulse_time;
660 switch (aad_pdata->btn_cfg) {
661 case DA7219_AAD_BTN_CFG_2MS:
662 case DA7219_AAD_BTN_CFG_5MS:
663 case DA7219_AAD_BTN_CFG_10MS:
664 case DA7219_AAD_BTN_CFG_50MS:
665 case DA7219_AAD_BTN_CFG_100MS:
666 case DA7219_AAD_BTN_CFG_200MS:
667 case DA7219_AAD_BTN_CFG_500MS:
668 da7219_aad->btn_cfg = (aad_pdata->btn_cfg <<
669 DA7219_BUTTON_CONFIG_SHIFT);
672 cfg = 0;
673 mask = 0;
674 switch (aad_pdata->mic_det_thr) {
675 case DA7219_AAD_MIC_DET_THR_200_OHMS:
676 case DA7219_AAD_MIC_DET_THR_500_OHMS:
677 case DA7219_AAD_MIC_DET_THR_750_OHMS:
678 case DA7219_AAD_MIC_DET_THR_1000_OHMS:
679 cfg |= (aad_pdata->mic_det_thr <<
680 DA7219_MIC_DET_THRESH_SHIFT);
681 mask |= DA7219_MIC_DET_THRESH_MASK;
683 snd_soc_update_bits(codec, DA7219_ACCDET_CONFIG_1, mask, cfg);
685 cfg = 0;
686 mask = 0;
687 switch (aad_pdata->jack_ins_deb) {
688 case DA7219_AAD_JACK_INS_DEB_5MS:
689 case DA7219_AAD_JACK_INS_DEB_10MS:
690 case DA7219_AAD_JACK_INS_DEB_20MS:
691 case DA7219_AAD_JACK_INS_DEB_50MS:
692 case DA7219_AAD_JACK_INS_DEB_100MS:
693 case DA7219_AAD_JACK_INS_DEB_200MS:
694 case DA7219_AAD_JACK_INS_DEB_500MS:
695 case DA7219_AAD_JACK_INS_DEB_1S:
696 cfg |= (aad_pdata->jack_ins_deb <<
697 DA7219_JACKDET_DEBOUNCE_SHIFT);
698 mask |= DA7219_JACKDET_DEBOUNCE_MASK;
700 switch (aad_pdata->jack_det_rate) {
701 case DA7219_AAD_JACK_DET_RATE_32_64MS:
702 case DA7219_AAD_JACK_DET_RATE_64_128MS:
703 case DA7219_AAD_JACK_DET_RATE_128_256MS:
704 case DA7219_AAD_JACK_DET_RATE_256_512MS:
705 cfg |= (aad_pdata->jack_det_rate <<
706 DA7219_JACK_DETECT_RATE_SHIFT);
707 mask |= DA7219_JACK_DETECT_RATE_MASK;
709 switch (aad_pdata->jack_rem_deb) {
710 case DA7219_AAD_JACK_REM_DEB_1MS:
711 case DA7219_AAD_JACK_REM_DEB_5MS:
712 case DA7219_AAD_JACK_REM_DEB_10MS:
713 case DA7219_AAD_JACK_REM_DEB_20MS:
714 cfg |= (aad_pdata->jack_rem_deb <<
715 DA7219_JACKDET_REM_DEB_SHIFT);
716 mask |= DA7219_JACKDET_REM_DEB_MASK;
718 snd_soc_update_bits(codec, DA7219_ACCDET_CONFIG_2, mask, cfg);
720 snd_soc_write(codec, DA7219_ACCDET_CONFIG_3,
721 aad_pdata->a_d_btn_thr);
722 snd_soc_write(codec, DA7219_ACCDET_CONFIG_4,
723 aad_pdata->d_b_btn_thr);
724 snd_soc_write(codec, DA7219_ACCDET_CONFIG_5,
725 aad_pdata->b_c_btn_thr);
726 snd_soc_write(codec, DA7219_ACCDET_CONFIG_6,
727 aad_pdata->c_mic_btn_thr);
729 cfg = 0;
730 mask = 0;
731 switch (aad_pdata->btn_avg) {
732 case DA7219_AAD_BTN_AVG_1:
733 case DA7219_AAD_BTN_AVG_2:
734 case DA7219_AAD_BTN_AVG_4:
735 case DA7219_AAD_BTN_AVG_8:
736 cfg |= (aad_pdata->btn_avg <<
737 DA7219_BUTTON_AVERAGE_SHIFT);
738 mask |= DA7219_BUTTON_AVERAGE_MASK;
740 switch (aad_pdata->adc_1bit_rpt) {
741 case DA7219_AAD_ADC_1BIT_RPT_1:
742 case DA7219_AAD_ADC_1BIT_RPT_2:
743 case DA7219_AAD_ADC_1BIT_RPT_4:
744 case DA7219_AAD_ADC_1BIT_RPT_8:
745 cfg |= (aad_pdata->adc_1bit_rpt <<
746 DA7219_ADC_1_BIT_REPEAT_SHIFT);
747 mask |= DA7219_ADC_1_BIT_REPEAT_MASK;
749 snd_soc_update_bits(codec, DA7219_ACCDET_CONFIG_7, mask, cfg);
755 * Init/Exit
758 int da7219_aad_init(struct snd_soc_codec *codec)
760 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
761 struct da7219_aad_priv *da7219_aad;
762 u8 mask[DA7219_AAD_IRQ_REG_MAX];
763 int ret;
765 da7219_aad = devm_kzalloc(codec->dev, sizeof(*da7219_aad), GFP_KERNEL);
766 if (!da7219_aad)
767 return -ENOMEM;
769 da7219->aad = da7219_aad;
770 da7219_aad->codec = codec;
772 /* Handle any DT/platform data */
773 if ((codec->dev->of_node) && (da7219->pdata))
774 da7219->pdata->aad_pdata = da7219_aad_of_to_pdata(codec);
776 da7219_aad_handle_pdata(codec);
778 /* Disable button detection */
779 snd_soc_update_bits(codec, DA7219_ACCDET_CONFIG_1,
780 DA7219_BUTTON_CONFIG_MASK, 0);
782 INIT_WORK(&da7219_aad->btn_det_work, da7219_aad_btn_det_work);
783 INIT_WORK(&da7219_aad->hptest_work, da7219_aad_hptest_work);
785 ret = request_threaded_irq(da7219_aad->irq, NULL,
786 da7219_aad_irq_thread,
787 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
788 "da7219-aad", da7219_aad);
789 if (ret) {
790 dev_err(codec->dev, "Failed to request IRQ: %d\n", ret);
791 return ret;
794 /* Unmask AAD IRQs */
795 memset(mask, 0, DA7219_AAD_IRQ_REG_MAX);
796 regmap_bulk_write(da7219->regmap, DA7219_ACCDET_IRQ_MASK_A,
797 &mask, DA7219_AAD_IRQ_REG_MAX);
799 return 0;
801 EXPORT_SYMBOL_GPL(da7219_aad_init);
803 void da7219_aad_exit(struct snd_soc_codec *codec)
805 struct da7219_priv *da7219 = snd_soc_codec_get_drvdata(codec);
806 struct da7219_aad_priv *da7219_aad = da7219->aad;
807 u8 mask[DA7219_AAD_IRQ_REG_MAX];
809 /* Mask off AAD IRQs */
810 memset(mask, DA7219_BYTE_MASK, DA7219_AAD_IRQ_REG_MAX);
811 regmap_bulk_write(da7219->regmap, DA7219_ACCDET_IRQ_MASK_A,
812 mask, DA7219_AAD_IRQ_REG_MAX);
814 free_irq(da7219_aad->irq, da7219_aad);
816 cancel_work_sync(&da7219_aad->btn_det_work);
817 cancel_work_sync(&da7219_aad->hptest_work);
819 EXPORT_SYMBOL_GPL(da7219_aad_exit);
821 MODULE_DESCRIPTION("ASoC DA7219 AAD Driver");
822 MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
823 MODULE_LICENSE("GPL");