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