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