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/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>
28 #include "da7219-aad.h"
35 void da7219_aad_jack_det(struct snd_soc_codec
*codec
, struct snd_soc_jack
*jack
)
37 struct da7219_priv
*da7219
= snd_soc_codec_get_drvdata(codec
);
39 da7219
->aad
->jack
= jack
;
40 da7219
->aad
->jack_inserted
= false;
42 /* Send an initial empty report */
43 snd_soc_jack_report(jack
, 0, DA7219_AAD_REPORT_ALL_MASK
);
45 /* Enable/Disable jack detection */
46 snd_soc_update_bits(codec
, DA7219_ACCDET_CONFIG_1
,
47 DA7219_ACCDET_EN_MASK
,
48 (jack
? DA7219_ACCDET_EN_MASK
: 0));
50 EXPORT_SYMBOL_GPL(da7219_aad_jack_det
);
56 static void da7219_aad_btn_det_work(struct work_struct
*work
)
58 struct da7219_aad_priv
*da7219_aad
=
59 container_of(work
, struct da7219_aad_priv
, btn_det_work
);
60 struct snd_soc_codec
*codec
= da7219_aad
->codec
;
61 struct snd_soc_dapm_context
*dapm
= snd_soc_codec_get_dapm(codec
);
62 u8 statusa
, micbias_ctrl
;
63 bool micbias_up
= false;
66 /* Drive headphones/lineout */
67 snd_soc_update_bits(codec
, DA7219_HP_L_CTRL
,
68 DA7219_HP_L_AMP_OE_MASK
,
69 DA7219_HP_L_AMP_OE_MASK
);
70 snd_soc_update_bits(codec
, DA7219_HP_R_CTRL
,
71 DA7219_HP_R_AMP_OE_MASK
,
72 DA7219_HP_R_AMP_OE_MASK
);
74 /* Make sure mic bias is up */
75 snd_soc_dapm_force_enable_pin(dapm
, "Mic Bias");
76 snd_soc_dapm_sync(dapm
);
79 statusa
= snd_soc_read(codec
, DA7219_ACCDET_STATUS_A
);
80 if (statusa
& DA7219_MICBIAS_UP_STS_MASK
)
82 else if (retries
++ < DA7219_AAD_MICBIAS_CHK_RETRIES
)
83 msleep(DA7219_AAD_MICBIAS_CHK_DELAY
);
84 } while ((!micbias_up
) && (retries
< DA7219_AAD_MICBIAS_CHK_RETRIES
));
86 if (retries
>= DA7219_AAD_MICBIAS_CHK_RETRIES
)
87 dev_warn(codec
->dev
, "Mic bias status check timed out");
90 * Mic bias pulse required to enable mic, must be done before enabling
91 * button detection to prevent erroneous button readings.
93 if (da7219_aad
->micbias_pulse_lvl
&& da7219_aad
->micbias_pulse_time
) {
94 /* Pulse higher level voltage */
95 micbias_ctrl
= snd_soc_read(codec
, DA7219_MICBIAS_CTRL
);
96 snd_soc_update_bits(codec
, DA7219_MICBIAS_CTRL
,
97 DA7219_MICBIAS1_LEVEL_MASK
,
98 da7219_aad
->micbias_pulse_lvl
);
99 msleep(da7219_aad
->micbias_pulse_time
);
100 snd_soc_write(codec
, DA7219_MICBIAS_CTRL
, micbias_ctrl
);
104 snd_soc_update_bits(codec
, DA7219_ACCDET_CONFIG_1
,
105 DA7219_BUTTON_CONFIG_MASK
,
106 da7219_aad
->btn_cfg
);
109 static void da7219_aad_hptest_work(struct work_struct
*work
)
111 struct da7219_aad_priv
*da7219_aad
=
112 container_of(work
, struct da7219_aad_priv
, hptest_work
);
113 struct snd_soc_codec
*codec
= da7219_aad
->codec
;
114 struct snd_soc_dapm_context
*dapm
= snd_soc_codec_get_dapm(codec
);
115 struct da7219_priv
*da7219
= snd_soc_codec_get_drvdata(codec
);
117 u16 tonegen_freq_hptest
;
118 u8 pll_srm_sts
, gain_ramp_ctrl
, accdet_cfg8
;
119 int report
= 0, ret
= 0;
121 /* Lock DAPM and any Kcontrols that are affected by this test */
122 snd_soc_dapm_mutex_lock(dapm
);
123 mutex_lock(&da7219
->lock
);
125 /* Ensure MCLK is available for HP test procedure */
127 ret
= clk_prepare_enable(da7219
->mclk
);
129 dev_err(codec
->dev
, "Failed to enable mclk - %d\n", ret
);
130 mutex_unlock(&da7219
->lock
);
131 snd_soc_dapm_mutex_unlock(dapm
);
137 * If MCLK not present, then we're using the internal oscillator and
138 * require different frequency settings to achieve the same result.
140 pll_srm_sts
= snd_soc_read(codec
, DA7219_PLL_SRM_STS
);
141 if (pll_srm_sts
& DA7219_PLL_SRM_STS_MCLK
)
142 tonegen_freq_hptest
= cpu_to_le16(DA7219_AAD_HPTEST_RAMP_FREQ
);
144 tonegen_freq_hptest
= cpu_to_le16(DA7219_AAD_HPTEST_RAMP_FREQ_INT_OSC
);
146 /* Ensure gain ramping at fastest rate */
147 gain_ramp_ctrl
= snd_soc_read(codec
, DA7219_GAIN_RAMP_CTRL
);
148 snd_soc_write(codec
, DA7219_GAIN_RAMP_CTRL
, DA7219_GAIN_RAMP_RATE_X8
);
150 /* Bypass cache so it saves current settings */
151 regcache_cache_bypass(da7219
->regmap
, true);
153 /* Make sure Tone Generator is disabled */
154 snd_soc_write(codec
, DA7219_TONE_GEN_CFG1
, 0);
156 /* Enable HPTest block, 1KOhms check */
157 snd_soc_update_bits(codec
, DA7219_ACCDET_CONFIG_8
,
158 DA7219_HPTEST_EN_MASK
| DA7219_HPTEST_RES_SEL_MASK
,
159 DA7219_HPTEST_EN_MASK
|
160 DA7219_HPTEST_RES_SEL_1KOHMS
);
162 /* Set gains to 0db */
163 snd_soc_write(codec
, DA7219_DAC_L_GAIN
, DA7219_DAC_DIGITAL_GAIN_0DB
);
164 snd_soc_write(codec
, DA7219_DAC_R_GAIN
, DA7219_DAC_DIGITAL_GAIN_0DB
);
165 snd_soc_write(codec
, DA7219_HP_L_GAIN
, DA7219_HP_AMP_GAIN_0DB
);
166 snd_soc_write(codec
, DA7219_HP_R_GAIN
, DA7219_HP_AMP_GAIN_0DB
);
168 /* Disable DAC filters, EQs and soft mute */
169 snd_soc_update_bits(codec
, DA7219_DAC_FILTERS1
, DA7219_HPF_MODE_MASK
,
171 snd_soc_update_bits(codec
, DA7219_DAC_FILTERS4
, DA7219_DAC_EQ_EN_MASK
,
173 snd_soc_update_bits(codec
, DA7219_DAC_FILTERS5
,
174 DA7219_DAC_SOFTMUTE_EN_MASK
, 0);
176 /* Enable HP left & right paths */
177 snd_soc_update_bits(codec
, DA7219_CP_CTRL
, DA7219_CP_EN_MASK
,
179 snd_soc_update_bits(codec
, DA7219_DIG_ROUTING_DAC
,
180 DA7219_DAC_L_SRC_MASK
| DA7219_DAC_R_SRC_MASK
,
181 DA7219_DAC_L_SRC_TONEGEN
|
182 DA7219_DAC_R_SRC_TONEGEN
);
183 snd_soc_update_bits(codec
, DA7219_DAC_L_CTRL
,
184 DA7219_DAC_L_EN_MASK
| DA7219_DAC_L_MUTE_EN_MASK
,
185 DA7219_DAC_L_EN_MASK
);
186 snd_soc_update_bits(codec
, DA7219_DAC_R_CTRL
,
187 DA7219_DAC_R_EN_MASK
| DA7219_DAC_R_MUTE_EN_MASK
,
188 DA7219_DAC_R_EN_MASK
);
189 snd_soc_update_bits(codec
, DA7219_MIXOUT_L_SELECT
,
190 DA7219_MIXOUT_L_MIX_SELECT_MASK
,
191 DA7219_MIXOUT_L_MIX_SELECT_MASK
);
192 snd_soc_update_bits(codec
, DA7219_MIXOUT_R_SELECT
,
193 DA7219_MIXOUT_R_MIX_SELECT_MASK
,
194 DA7219_MIXOUT_R_MIX_SELECT_MASK
);
195 snd_soc_update_bits(codec
, DA7219_DROUTING_ST_OUTFILT_1L
,
196 DA7219_OUTFILT_ST_1L_SRC_MASK
,
197 DA7219_DMIX_ST_SRC_OUTFILT1L
);
198 snd_soc_update_bits(codec
, DA7219_DROUTING_ST_OUTFILT_1R
,
199 DA7219_OUTFILT_ST_1R_SRC_MASK
,
200 DA7219_DMIX_ST_SRC_OUTFILT1R
);
201 snd_soc_update_bits(codec
, DA7219_MIXOUT_L_CTRL
,
202 DA7219_MIXOUT_L_AMP_EN_MASK
,
203 DA7219_MIXOUT_L_AMP_EN_MASK
);
204 snd_soc_update_bits(codec
, DA7219_MIXOUT_R_CTRL
,
205 DA7219_MIXOUT_R_AMP_EN_MASK
,
206 DA7219_MIXOUT_R_AMP_EN_MASK
);
207 snd_soc_update_bits(codec
, DA7219_HP_L_CTRL
,
208 DA7219_HP_L_AMP_OE_MASK
| DA7219_HP_L_AMP_EN_MASK
,
209 DA7219_HP_L_AMP_OE_MASK
| DA7219_HP_L_AMP_EN_MASK
);
210 snd_soc_update_bits(codec
, DA7219_HP_R_CTRL
,
211 DA7219_HP_R_AMP_OE_MASK
| DA7219_HP_R_AMP_EN_MASK
,
212 DA7219_HP_R_AMP_OE_MASK
| DA7219_HP_R_AMP_EN_MASK
);
213 msleep(DA7219_SETTLING_DELAY
);
214 snd_soc_update_bits(codec
, DA7219_HP_L_CTRL
,
215 DA7219_HP_L_AMP_MUTE_EN_MASK
|
216 DA7219_HP_L_AMP_MIN_GAIN_EN_MASK
, 0);
217 snd_soc_update_bits(codec
, DA7219_HP_R_CTRL
,
218 DA7219_HP_R_AMP_MUTE_EN_MASK
|
219 DA7219_HP_R_AMP_MIN_GAIN_EN_MASK
, 0);
222 * If we're running from the internal oscillator then give audio paths
223 * time to settle before running test.
225 if (!(pll_srm_sts
& DA7219_PLL_SRM_STS_MCLK
))
226 msleep(DA7219_AAD_HPTEST_INT_OSC_PATH_DELAY
);
228 /* Configure & start Tone Generator */
229 snd_soc_write(codec
, DA7219_TONE_GEN_ON_PER
, DA7219_BEEP_ON_PER_MASK
);
230 regmap_raw_write(da7219
->regmap
, DA7219_TONE_GEN_FREQ1_L
,
231 &tonegen_freq_hptest
, sizeof(tonegen_freq_hptest
));
232 snd_soc_update_bits(codec
, DA7219_TONE_GEN_CFG2
,
233 DA7219_SWG_SEL_MASK
| DA7219_TONE_GEN_GAIN_MASK
,
234 DA7219_SWG_SEL_SRAMP
|
235 DA7219_TONE_GEN_GAIN_MINUS_15DB
);
236 snd_soc_write(codec
, DA7219_TONE_GEN_CFG1
, DA7219_START_STOPN_MASK
);
238 msleep(DA7219_AAD_HPTEST_PERIOD
);
240 /* Grab comparator reading */
241 accdet_cfg8
= snd_soc_read(codec
, DA7219_ACCDET_CONFIG_8
);
242 if (accdet_cfg8
& DA7219_HPTEST_COMP_MASK
)
243 report
|= SND_JACK_HEADPHONE
;
245 report
|= SND_JACK_LINEOUT
;
247 /* Stop tone generator */
248 snd_soc_write(codec
, DA7219_TONE_GEN_CFG1
, 0);
250 msleep(DA7219_AAD_HPTEST_PERIOD
);
252 /* Restore original settings from cache */
253 regcache_mark_dirty(da7219
->regmap
);
254 regcache_sync_region(da7219
->regmap
, DA7219_HP_L_CTRL
,
256 msleep(DA7219_SETTLING_DELAY
);
257 regcache_sync_region(da7219
->regmap
, DA7219_MIXOUT_L_CTRL
,
258 DA7219_MIXOUT_R_CTRL
);
259 regcache_sync_region(da7219
->regmap
, DA7219_DROUTING_ST_OUTFILT_1L
,
260 DA7219_DROUTING_ST_OUTFILT_1R
);
261 regcache_sync_region(da7219
->regmap
, DA7219_MIXOUT_L_SELECT
,
262 DA7219_MIXOUT_R_SELECT
);
263 regcache_sync_region(da7219
->regmap
, DA7219_DAC_L_CTRL
,
265 regcache_sync_region(da7219
->regmap
, DA7219_DIG_ROUTING_DAC
,
266 DA7219_DIG_ROUTING_DAC
);
267 regcache_sync_region(da7219
->regmap
, DA7219_CP_CTRL
, DA7219_CP_CTRL
);
268 regcache_sync_region(da7219
->regmap
, DA7219_DAC_FILTERS5
,
269 DA7219_DAC_FILTERS5
);
270 regcache_sync_region(da7219
->regmap
, DA7219_DAC_FILTERS4
,
271 DA7219_DAC_FILTERS1
);
272 regcache_sync_region(da7219
->regmap
, DA7219_HP_L_GAIN
,
274 regcache_sync_region(da7219
->regmap
, DA7219_DAC_L_GAIN
,
276 regcache_sync_region(da7219
->regmap
, DA7219_TONE_GEN_ON_PER
,
277 DA7219_TONE_GEN_ON_PER
);
278 regcache_sync_region(da7219
->regmap
, DA7219_TONE_GEN_FREQ1_L
,
279 DA7219_TONE_GEN_FREQ1_U
);
280 regcache_sync_region(da7219
->regmap
, DA7219_TONE_GEN_CFG1
,
281 DA7219_TONE_GEN_CFG2
);
283 regcache_cache_bypass(da7219
->regmap
, false);
285 /* Disable HPTest block */
286 snd_soc_update_bits(codec
, DA7219_ACCDET_CONFIG_8
,
287 DA7219_HPTEST_EN_MASK
, 0);
290 * If we're running from the internal oscillator then give audio paths
291 * time to settle before allowing headphones to be driven as required.
293 if (!(pll_srm_sts
& DA7219_PLL_SRM_STS_MCLK
))
294 msleep(DA7219_AAD_HPTEST_INT_OSC_PATH_DELAY
);
296 /* Restore gain ramping rate */
297 snd_soc_write(codec
, DA7219_GAIN_RAMP_CTRL
, gain_ramp_ctrl
);
299 /* Drive Headphones/lineout */
300 snd_soc_update_bits(codec
, DA7219_HP_L_CTRL
, DA7219_HP_L_AMP_OE_MASK
,
301 DA7219_HP_L_AMP_OE_MASK
);
302 snd_soc_update_bits(codec
, DA7219_HP_R_CTRL
, DA7219_HP_R_AMP_OE_MASK
,
303 DA7219_HP_R_AMP_OE_MASK
);
305 /* Remove MCLK, if previously enabled */
307 clk_disable_unprepare(da7219
->mclk
);
309 mutex_unlock(&da7219
->lock
);
310 snd_soc_dapm_mutex_unlock(dapm
);
313 * Only send report if jack hasn't been removed during process,
314 * otherwise it's invalid and we drop it.
316 if (da7219_aad
->jack_inserted
)
317 snd_soc_jack_report(da7219_aad
->jack
, report
,
318 SND_JACK_HEADSET
| SND_JACK_LINEOUT
);
326 static irqreturn_t
da7219_aad_irq_thread(int irq
, void *data
)
328 struct da7219_aad_priv
*da7219_aad
= data
;
329 struct snd_soc_codec
*codec
= da7219_aad
->codec
;
330 struct snd_soc_dapm_context
*dapm
= snd_soc_codec_get_dapm(codec
);
331 struct da7219_priv
*da7219
= snd_soc_codec_get_drvdata(codec
);
332 u8 events
[DA7219_AAD_IRQ_REG_MAX
];
334 int i
, report
= 0, mask
= 0;
336 /* Read current IRQ events */
337 regmap_bulk_read(da7219
->regmap
, DA7219_ACCDET_IRQ_EVENT_A
,
338 events
, DA7219_AAD_IRQ_REG_MAX
);
340 if (!events
[DA7219_AAD_IRQ_REG_A
] && !events
[DA7219_AAD_IRQ_REG_B
])
343 /* Read status register for jack insertion & type status */
344 statusa
= snd_soc_read(codec
, DA7219_ACCDET_STATUS_A
);
347 regmap_bulk_write(da7219
->regmap
, DA7219_ACCDET_IRQ_EVENT_A
,
348 events
, DA7219_AAD_IRQ_REG_MAX
);
350 dev_dbg(codec
->dev
, "IRQ events = 0x%x|0x%x, status = 0x%x\n",
351 events
[DA7219_AAD_IRQ_REG_A
], events
[DA7219_AAD_IRQ_REG_B
],
354 if (statusa
& DA7219_JACK_INSERTION_STS_MASK
) {
356 if (events
[DA7219_AAD_IRQ_REG_A
] &
357 DA7219_E_JACK_INSERTED_MASK
) {
358 report
|= SND_JACK_MECHANICAL
;
359 mask
|= SND_JACK_MECHANICAL
;
360 da7219_aad
->jack_inserted
= true;
363 /* Jack type detection */
364 if (events
[DA7219_AAD_IRQ_REG_A
] &
365 DA7219_E_JACK_DETECT_COMPLETE_MASK
) {
367 * If 4-pole, then enable button detection, else perform
368 * HP impedance test to determine output type to report.
370 * We schedule work here as the tasks themselves can
371 * take time to complete, and in particular for hptest
372 * we want to be able to check if the jack was removed
373 * during the procedure as this will invalidate the
374 * result. By doing this as work, the IRQ thread can
375 * handle a removal, and we can check at the end of
376 * hptest if we have a valid result or not.
378 if (statusa
& DA7219_JACK_TYPE_STS_MASK
) {
379 report
|= SND_JACK_HEADSET
;
380 mask
|= SND_JACK_HEADSET
| SND_JACK_LINEOUT
;
381 schedule_work(&da7219_aad
->btn_det_work
);
383 schedule_work(&da7219_aad
->hptest_work
);
387 /* Button support for 4-pole jack */
388 if (statusa
& DA7219_JACK_TYPE_STS_MASK
) {
389 for (i
= 0; i
< DA7219_AAD_MAX_BUTTONS
; ++i
) {
391 if (events
[DA7219_AAD_IRQ_REG_B
] &
392 (DA7219_E_BUTTON_A_PRESSED_MASK
<< i
)) {
393 report
|= SND_JACK_BTN_0
>> i
;
394 mask
|= SND_JACK_BTN_0
>> i
;
397 snd_soc_jack_report(da7219_aad
->jack
, report
, mask
);
399 for (i
= 0; i
< DA7219_AAD_MAX_BUTTONS
; ++i
) {
401 if (events
[DA7219_AAD_IRQ_REG_B
] &
402 (DA7219_E_BUTTON_A_RELEASED_MASK
>> i
)) {
403 report
&= ~(SND_JACK_BTN_0
>> i
);
404 mask
|= SND_JACK_BTN_0
>> i
;
410 if (events
[DA7219_AAD_IRQ_REG_A
] & DA7219_E_JACK_REMOVED_MASK
) {
412 mask
|= DA7219_AAD_REPORT_ALL_MASK
;
413 da7219_aad
->jack_inserted
= false;
415 /* Un-drive headphones/lineout */
416 snd_soc_update_bits(codec
, DA7219_HP_R_CTRL
,
417 DA7219_HP_R_AMP_OE_MASK
, 0);
418 snd_soc_update_bits(codec
, DA7219_HP_L_CTRL
,
419 DA7219_HP_L_AMP_OE_MASK
, 0);
421 /* Ensure button detection disabled */
422 snd_soc_update_bits(codec
, DA7219_ACCDET_CONFIG_1
,
423 DA7219_BUTTON_CONFIG_MASK
, 0);
425 /* Disable mic bias */
426 snd_soc_dapm_disable_pin(dapm
, "Mic Bias");
427 snd_soc_dapm_sync(dapm
);
429 /* Cancel any pending work */
430 cancel_work_sync(&da7219_aad
->btn_det_work
);
431 cancel_work_sync(&da7219_aad
->hptest_work
);
435 snd_soc_jack_report(da7219_aad
->jack
, report
, mask
);
441 * DT/ACPI to pdata conversion
444 static enum da7219_aad_micbias_pulse_lvl
445 da7219_aad_fw_micbias_pulse_lvl(struct snd_soc_codec
*codec
, u32 val
)
449 return DA7219_AAD_MICBIAS_PULSE_LVL_2_8V
;
451 return DA7219_AAD_MICBIAS_PULSE_LVL_2_9V
;
453 dev_warn(codec
->dev
, "Invalid micbias pulse level");
454 return DA7219_AAD_MICBIAS_PULSE_LVL_OFF
;
458 static enum da7219_aad_btn_cfg
459 da7219_aad_fw_btn_cfg(struct snd_soc_codec
*codec
, u32 val
)
463 return DA7219_AAD_BTN_CFG_2MS
;
465 return DA7219_AAD_BTN_CFG_5MS
;
467 return DA7219_AAD_BTN_CFG_10MS
;
469 return DA7219_AAD_BTN_CFG_50MS
;
471 return DA7219_AAD_BTN_CFG_100MS
;
473 return DA7219_AAD_BTN_CFG_200MS
;
475 return DA7219_AAD_BTN_CFG_500MS
;
477 dev_warn(codec
->dev
, "Invalid button config");
478 return DA7219_AAD_BTN_CFG_10MS
;
482 static enum da7219_aad_mic_det_thr
483 da7219_aad_fw_mic_det_thr(struct snd_soc_codec
*codec
, u32 val
)
487 return DA7219_AAD_MIC_DET_THR_200_OHMS
;
489 return DA7219_AAD_MIC_DET_THR_500_OHMS
;
491 return DA7219_AAD_MIC_DET_THR_750_OHMS
;
493 return DA7219_AAD_MIC_DET_THR_1000_OHMS
;
495 dev_warn(codec
->dev
, "Invalid mic detect threshold");
496 return DA7219_AAD_MIC_DET_THR_500_OHMS
;
500 static enum da7219_aad_jack_ins_deb
501 da7219_aad_fw_jack_ins_deb(struct snd_soc_codec
*codec
, u32 val
)
505 return DA7219_AAD_JACK_INS_DEB_5MS
;
507 return DA7219_AAD_JACK_INS_DEB_10MS
;
509 return DA7219_AAD_JACK_INS_DEB_20MS
;
511 return DA7219_AAD_JACK_INS_DEB_50MS
;
513 return DA7219_AAD_JACK_INS_DEB_100MS
;
515 return DA7219_AAD_JACK_INS_DEB_200MS
;
517 return DA7219_AAD_JACK_INS_DEB_500MS
;
519 return DA7219_AAD_JACK_INS_DEB_1S
;
521 dev_warn(codec
->dev
, "Invalid jack insert debounce");
522 return DA7219_AAD_JACK_INS_DEB_20MS
;
526 static enum da7219_aad_jack_det_rate
527 da7219_aad_fw_jack_det_rate(struct snd_soc_codec
*codec
, const char *str
)
529 if (!strcmp(str
, "32ms_64ms")) {
530 return DA7219_AAD_JACK_DET_RATE_32_64MS
;
531 } else if (!strcmp(str
, "64ms_128ms")) {
532 return DA7219_AAD_JACK_DET_RATE_64_128MS
;
533 } else if (!strcmp(str
, "128ms_256ms")) {
534 return DA7219_AAD_JACK_DET_RATE_128_256MS
;
535 } else if (!strcmp(str
, "256ms_512ms")) {
536 return DA7219_AAD_JACK_DET_RATE_256_512MS
;
538 dev_warn(codec
->dev
, "Invalid jack detect rate");
539 return DA7219_AAD_JACK_DET_RATE_256_512MS
;
543 static enum da7219_aad_jack_rem_deb
544 da7219_aad_fw_jack_rem_deb(struct snd_soc_codec
*codec
, u32 val
)
548 return DA7219_AAD_JACK_REM_DEB_1MS
;
550 return DA7219_AAD_JACK_REM_DEB_5MS
;
552 return DA7219_AAD_JACK_REM_DEB_10MS
;
554 return DA7219_AAD_JACK_REM_DEB_20MS
;
556 dev_warn(codec
->dev
, "Invalid jack removal debounce");
557 return DA7219_AAD_JACK_REM_DEB_1MS
;
561 static enum da7219_aad_btn_avg
562 da7219_aad_fw_btn_avg(struct snd_soc_codec
*codec
, u32 val
)
566 return DA7219_AAD_BTN_AVG_1
;
568 return DA7219_AAD_BTN_AVG_2
;
570 return DA7219_AAD_BTN_AVG_4
;
572 return DA7219_AAD_BTN_AVG_8
;
574 dev_warn(codec
->dev
, "Invalid button average value");
575 return DA7219_AAD_BTN_AVG_2
;
579 static enum da7219_aad_adc_1bit_rpt
580 da7219_aad_fw_adc_1bit_rpt(struct snd_soc_codec
*codec
, u32 val
)
584 return DA7219_AAD_ADC_1BIT_RPT_1
;
586 return DA7219_AAD_ADC_1BIT_RPT_2
;
588 return DA7219_AAD_ADC_1BIT_RPT_4
;
590 return DA7219_AAD_ADC_1BIT_RPT_8
;
592 dev_warn(codec
->dev
, "Invalid ADC 1-bit repeat value");
593 return DA7219_AAD_ADC_1BIT_RPT_1
;
597 static struct da7219_aad_pdata
*da7219_aad_fw_to_pdata(struct snd_soc_codec
*codec
)
599 struct device
*dev
= codec
->dev
;
600 struct i2c_client
*i2c
= to_i2c_client(dev
);
601 struct fwnode_handle
*aad_np
;
602 struct da7219_aad_pdata
*aad_pdata
;
606 aad_np
= device_get_named_child_node(dev
, "da7219_aad");
610 aad_pdata
= devm_kzalloc(codec
->dev
, sizeof(*aad_pdata
), GFP_KERNEL
);
614 aad_pdata
->irq
= i2c
->irq
;
616 if (fwnode_property_read_u32(aad_np
, "dlg,micbias-pulse-lvl",
618 aad_pdata
->micbias_pulse_lvl
=
619 da7219_aad_fw_micbias_pulse_lvl(codec
, fw_val32
);
621 aad_pdata
->micbias_pulse_lvl
= DA7219_AAD_MICBIAS_PULSE_LVL_OFF
;
623 if (fwnode_property_read_u32(aad_np
, "dlg,micbias-pulse-time",
625 aad_pdata
->micbias_pulse_time
= fw_val32
;
627 if (fwnode_property_read_u32(aad_np
, "dlg,btn-cfg", &fw_val32
) >= 0)
628 aad_pdata
->btn_cfg
= da7219_aad_fw_btn_cfg(codec
, fw_val32
);
630 aad_pdata
->btn_cfg
= DA7219_AAD_BTN_CFG_10MS
;
632 if (fwnode_property_read_u32(aad_np
, "dlg,mic-det-thr", &fw_val32
) >= 0)
633 aad_pdata
->mic_det_thr
=
634 da7219_aad_fw_mic_det_thr(codec
, fw_val32
);
636 aad_pdata
->mic_det_thr
= DA7219_AAD_MIC_DET_THR_500_OHMS
;
638 if (fwnode_property_read_u32(aad_np
, "dlg,jack-ins-deb", &fw_val32
) >= 0)
639 aad_pdata
->jack_ins_deb
=
640 da7219_aad_fw_jack_ins_deb(codec
, fw_val32
);
642 aad_pdata
->jack_ins_deb
= DA7219_AAD_JACK_INS_DEB_20MS
;
644 if (!fwnode_property_read_string(aad_np
, "dlg,jack-det-rate", &fw_str
))
645 aad_pdata
->jack_det_rate
=
646 da7219_aad_fw_jack_det_rate(codec
, fw_str
);
648 aad_pdata
->jack_det_rate
= DA7219_AAD_JACK_DET_RATE_256_512MS
;
650 if (fwnode_property_read_u32(aad_np
, "dlg,jack-rem-deb", &fw_val32
) >= 0)
651 aad_pdata
->jack_rem_deb
=
652 da7219_aad_fw_jack_rem_deb(codec
, fw_val32
);
654 aad_pdata
->jack_rem_deb
= DA7219_AAD_JACK_REM_DEB_1MS
;
656 if (fwnode_property_read_u32(aad_np
, "dlg,a-d-btn-thr", &fw_val32
) >= 0)
657 aad_pdata
->a_d_btn_thr
= (u8
) fw_val32
;
659 aad_pdata
->a_d_btn_thr
= 0xA;
661 if (fwnode_property_read_u32(aad_np
, "dlg,d-b-btn-thr", &fw_val32
) >= 0)
662 aad_pdata
->d_b_btn_thr
= (u8
) fw_val32
;
664 aad_pdata
->d_b_btn_thr
= 0x16;
666 if (fwnode_property_read_u32(aad_np
, "dlg,b-c-btn-thr", &fw_val32
) >= 0)
667 aad_pdata
->b_c_btn_thr
= (u8
) fw_val32
;
669 aad_pdata
->b_c_btn_thr
= 0x21;
671 if (fwnode_property_read_u32(aad_np
, "dlg,c-mic-btn-thr", &fw_val32
) >= 0)
672 aad_pdata
->c_mic_btn_thr
= (u8
) fw_val32
;
674 aad_pdata
->c_mic_btn_thr
= 0x3E;
676 if (fwnode_property_read_u32(aad_np
, "dlg,btn-avg", &fw_val32
) >= 0)
677 aad_pdata
->btn_avg
= da7219_aad_fw_btn_avg(codec
, fw_val32
);
679 aad_pdata
->btn_avg
= DA7219_AAD_BTN_AVG_2
;
681 if (fwnode_property_read_u32(aad_np
, "dlg,adc-1bit-rpt", &fw_val32
) >= 0)
682 aad_pdata
->adc_1bit_rpt
=
683 da7219_aad_fw_adc_1bit_rpt(codec
, fw_val32
);
685 aad_pdata
->adc_1bit_rpt
= DA7219_AAD_ADC_1BIT_RPT_1
;
690 static void da7219_aad_handle_pdata(struct snd_soc_codec
*codec
)
692 struct da7219_priv
*da7219
= snd_soc_codec_get_drvdata(codec
);
693 struct da7219_aad_priv
*da7219_aad
= da7219
->aad
;
694 struct da7219_pdata
*pdata
= da7219
->pdata
;
696 if ((pdata
) && (pdata
->aad_pdata
)) {
697 struct da7219_aad_pdata
*aad_pdata
= pdata
->aad_pdata
;
700 da7219_aad
->irq
= aad_pdata
->irq
;
702 switch (aad_pdata
->micbias_pulse_lvl
) {
703 case DA7219_AAD_MICBIAS_PULSE_LVL_2_8V
:
704 case DA7219_AAD_MICBIAS_PULSE_LVL_2_9V
:
705 da7219_aad
->micbias_pulse_lvl
=
706 (aad_pdata
->micbias_pulse_lvl
<<
707 DA7219_MICBIAS1_LEVEL_SHIFT
);
713 da7219_aad
->micbias_pulse_time
= aad_pdata
->micbias_pulse_time
;
715 switch (aad_pdata
->btn_cfg
) {
716 case DA7219_AAD_BTN_CFG_2MS
:
717 case DA7219_AAD_BTN_CFG_5MS
:
718 case DA7219_AAD_BTN_CFG_10MS
:
719 case DA7219_AAD_BTN_CFG_50MS
:
720 case DA7219_AAD_BTN_CFG_100MS
:
721 case DA7219_AAD_BTN_CFG_200MS
:
722 case DA7219_AAD_BTN_CFG_500MS
:
723 da7219_aad
->btn_cfg
= (aad_pdata
->btn_cfg
<<
724 DA7219_BUTTON_CONFIG_SHIFT
);
729 switch (aad_pdata
->mic_det_thr
) {
730 case DA7219_AAD_MIC_DET_THR_200_OHMS
:
731 case DA7219_AAD_MIC_DET_THR_500_OHMS
:
732 case DA7219_AAD_MIC_DET_THR_750_OHMS
:
733 case DA7219_AAD_MIC_DET_THR_1000_OHMS
:
734 cfg
|= (aad_pdata
->mic_det_thr
<<
735 DA7219_MIC_DET_THRESH_SHIFT
);
736 mask
|= DA7219_MIC_DET_THRESH_MASK
;
738 snd_soc_update_bits(codec
, DA7219_ACCDET_CONFIG_1
, mask
, cfg
);
742 switch (aad_pdata
->jack_ins_deb
) {
743 case DA7219_AAD_JACK_INS_DEB_5MS
:
744 case DA7219_AAD_JACK_INS_DEB_10MS
:
745 case DA7219_AAD_JACK_INS_DEB_20MS
:
746 case DA7219_AAD_JACK_INS_DEB_50MS
:
747 case DA7219_AAD_JACK_INS_DEB_100MS
:
748 case DA7219_AAD_JACK_INS_DEB_200MS
:
749 case DA7219_AAD_JACK_INS_DEB_500MS
:
750 case DA7219_AAD_JACK_INS_DEB_1S
:
751 cfg
|= (aad_pdata
->jack_ins_deb
<<
752 DA7219_JACKDET_DEBOUNCE_SHIFT
);
753 mask
|= DA7219_JACKDET_DEBOUNCE_MASK
;
755 switch (aad_pdata
->jack_det_rate
) {
756 case DA7219_AAD_JACK_DET_RATE_32_64MS
:
757 case DA7219_AAD_JACK_DET_RATE_64_128MS
:
758 case DA7219_AAD_JACK_DET_RATE_128_256MS
:
759 case DA7219_AAD_JACK_DET_RATE_256_512MS
:
760 cfg
|= (aad_pdata
->jack_det_rate
<<
761 DA7219_JACK_DETECT_RATE_SHIFT
);
762 mask
|= DA7219_JACK_DETECT_RATE_MASK
;
764 switch (aad_pdata
->jack_rem_deb
) {
765 case DA7219_AAD_JACK_REM_DEB_1MS
:
766 case DA7219_AAD_JACK_REM_DEB_5MS
:
767 case DA7219_AAD_JACK_REM_DEB_10MS
:
768 case DA7219_AAD_JACK_REM_DEB_20MS
:
769 cfg
|= (aad_pdata
->jack_rem_deb
<<
770 DA7219_JACKDET_REM_DEB_SHIFT
);
771 mask
|= DA7219_JACKDET_REM_DEB_MASK
;
773 snd_soc_update_bits(codec
, DA7219_ACCDET_CONFIG_2
, mask
, cfg
);
775 snd_soc_write(codec
, DA7219_ACCDET_CONFIG_3
,
776 aad_pdata
->a_d_btn_thr
);
777 snd_soc_write(codec
, DA7219_ACCDET_CONFIG_4
,
778 aad_pdata
->d_b_btn_thr
);
779 snd_soc_write(codec
, DA7219_ACCDET_CONFIG_5
,
780 aad_pdata
->b_c_btn_thr
);
781 snd_soc_write(codec
, DA7219_ACCDET_CONFIG_6
,
782 aad_pdata
->c_mic_btn_thr
);
786 switch (aad_pdata
->btn_avg
) {
787 case DA7219_AAD_BTN_AVG_1
:
788 case DA7219_AAD_BTN_AVG_2
:
789 case DA7219_AAD_BTN_AVG_4
:
790 case DA7219_AAD_BTN_AVG_8
:
791 cfg
|= (aad_pdata
->btn_avg
<<
792 DA7219_BUTTON_AVERAGE_SHIFT
);
793 mask
|= DA7219_BUTTON_AVERAGE_MASK
;
795 switch (aad_pdata
->adc_1bit_rpt
) {
796 case DA7219_AAD_ADC_1BIT_RPT_1
:
797 case DA7219_AAD_ADC_1BIT_RPT_2
:
798 case DA7219_AAD_ADC_1BIT_RPT_4
:
799 case DA7219_AAD_ADC_1BIT_RPT_8
:
800 cfg
|= (aad_pdata
->adc_1bit_rpt
<<
801 DA7219_ADC_1_BIT_REPEAT_SHIFT
);
802 mask
|= DA7219_ADC_1_BIT_REPEAT_MASK
;
804 snd_soc_update_bits(codec
, DA7219_ACCDET_CONFIG_7
, mask
, cfg
);
813 void da7219_aad_suspend(struct snd_soc_codec
*codec
)
815 struct da7219_priv
*da7219
= snd_soc_codec_get_drvdata(codec
);
816 struct da7219_aad_priv
*da7219_aad
= da7219
->aad
;
817 struct snd_soc_dapm_context
*dapm
= snd_soc_codec_get_dapm(codec
);
820 if (da7219_aad
->jack
) {
821 /* Disable jack detection during suspend */
822 snd_soc_update_bits(codec
, DA7219_ACCDET_CONFIG_1
,
823 DA7219_ACCDET_EN_MASK
, 0);
826 * If we have a 4-pole jack inserted, then micbias will be
827 * enabled. We can disable micbias here, and keep a note to
828 * re-enable it on resume. If jack removal occurred during
829 * suspend then this will be dealt with through the IRQ handler.
831 if (da7219_aad
->jack_inserted
) {
832 micbias_ctrl
= snd_soc_read(codec
, DA7219_MICBIAS_CTRL
);
833 if (micbias_ctrl
& DA7219_MICBIAS1_EN_MASK
) {
834 snd_soc_dapm_disable_pin(dapm
, "Mic Bias");
835 snd_soc_dapm_sync(dapm
);
836 da7219_aad
->micbias_resume_enable
= true;
842 void da7219_aad_resume(struct snd_soc_codec
*codec
)
844 struct da7219_priv
*da7219
= snd_soc_codec_get_drvdata(codec
);
845 struct da7219_aad_priv
*da7219_aad
= da7219
->aad
;
846 struct snd_soc_dapm_context
*dapm
= snd_soc_codec_get_dapm(codec
);
848 if (da7219_aad
->jack
) {
849 /* Re-enable micbias if previously enabled for 4-pole jack */
850 if (da7219_aad
->jack_inserted
&&
851 da7219_aad
->micbias_resume_enable
) {
852 snd_soc_dapm_force_enable_pin(dapm
, "Mic Bias");
853 snd_soc_dapm_sync(dapm
);
854 da7219_aad
->micbias_resume_enable
= false;
857 /* Re-enable jack detection */
858 snd_soc_update_bits(codec
, DA7219_ACCDET_CONFIG_1
,
859 DA7219_ACCDET_EN_MASK
,
860 DA7219_ACCDET_EN_MASK
);
869 int da7219_aad_init(struct snd_soc_codec
*codec
)
871 struct da7219_priv
*da7219
= snd_soc_codec_get_drvdata(codec
);
872 struct da7219_aad_priv
*da7219_aad
;
873 u8 mask
[DA7219_AAD_IRQ_REG_MAX
];
876 da7219_aad
= devm_kzalloc(codec
->dev
, sizeof(*da7219_aad
), GFP_KERNEL
);
880 da7219
->aad
= da7219_aad
;
881 da7219_aad
->codec
= codec
;
883 /* Handle any DT/ACPI/platform data */
884 if (da7219
->pdata
&& !da7219
->pdata
->aad_pdata
)
885 da7219
->pdata
->aad_pdata
= da7219_aad_fw_to_pdata(codec
);
887 da7219_aad_handle_pdata(codec
);
889 /* Disable button detection */
890 snd_soc_update_bits(codec
, DA7219_ACCDET_CONFIG_1
,
891 DA7219_BUTTON_CONFIG_MASK
, 0);
893 INIT_WORK(&da7219_aad
->btn_det_work
, da7219_aad_btn_det_work
);
894 INIT_WORK(&da7219_aad
->hptest_work
, da7219_aad_hptest_work
);
896 ret
= request_threaded_irq(da7219_aad
->irq
, NULL
,
897 da7219_aad_irq_thread
,
898 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
899 "da7219-aad", da7219_aad
);
901 dev_err(codec
->dev
, "Failed to request IRQ: %d\n", ret
);
905 /* Unmask AAD IRQs */
906 memset(mask
, 0, DA7219_AAD_IRQ_REG_MAX
);
907 regmap_bulk_write(da7219
->regmap
, DA7219_ACCDET_IRQ_MASK_A
,
908 &mask
, DA7219_AAD_IRQ_REG_MAX
);
912 EXPORT_SYMBOL_GPL(da7219_aad_init
);
914 void da7219_aad_exit(struct snd_soc_codec
*codec
)
916 struct da7219_priv
*da7219
= snd_soc_codec_get_drvdata(codec
);
917 struct da7219_aad_priv
*da7219_aad
= da7219
->aad
;
918 u8 mask
[DA7219_AAD_IRQ_REG_MAX
];
920 /* Mask off AAD IRQs */
921 memset(mask
, DA7219_BYTE_MASK
, DA7219_AAD_IRQ_REG_MAX
);
922 regmap_bulk_write(da7219
->regmap
, DA7219_ACCDET_IRQ_MASK_A
,
923 mask
, DA7219_AAD_IRQ_REG_MAX
);
925 free_irq(da7219_aad
->irq
, da7219_aad
);
927 cancel_work_sync(&da7219_aad
->btn_det_work
);
928 cancel_work_sync(&da7219_aad
->hptest_work
);
930 EXPORT_SYMBOL_GPL(da7219_aad_exit
);
932 MODULE_DESCRIPTION("ASoC DA7219 AAD Driver");
933 MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
934 MODULE_LICENSE("GPL");