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