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