1 // SPDX-License-Identifier: GPL-2.0-only
3 // tegra210_dmic.c - Tegra210 DMIC driver
5 // Copyright (c) 2020 NVIDIA CORPORATION. All rights reserved.
8 #include <linux/device.h>
9 #include <linux/math64.h>
10 #include <linux/module.h>
11 #include <linux/of_device.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regmap.h>
15 #include <sound/core.h>
16 #include <sound/pcm_params.h>
17 #include <sound/soc.h>
18 #include "tegra210_dmic.h"
19 #include "tegra_cif.h"
21 static const struct reg_default tegra210_dmic_reg_defaults[] = {
22 { TEGRA210_DMIC_TX_INT_MASK, 0x00000001 },
23 { TEGRA210_DMIC_TX_CIF_CTRL, 0x00007700 },
24 { TEGRA210_DMIC_CG, 0x1 },
25 { TEGRA210_DMIC_CTRL, 0x00000301 },
26 /* Below enables all filters - DCR, LP and SC */
27 { TEGRA210_DMIC_DBG_CTRL, 0xe },
28 /* Below as per latest POR value */
29 { TEGRA210_DMIC_DCR_BIQUAD_0_COEF_4, 0x0 },
30 /* LP filter is configured for pass through and used to apply gain */
31 { TEGRA210_DMIC_LP_BIQUAD_0_COEF_0, 0x00800000 },
32 { TEGRA210_DMIC_LP_BIQUAD_0_COEF_1, 0x0 },
33 { TEGRA210_DMIC_LP_BIQUAD_0_COEF_2, 0x0 },
34 { TEGRA210_DMIC_LP_BIQUAD_0_COEF_3, 0x0 },
35 { TEGRA210_DMIC_LP_BIQUAD_0_COEF_4, 0x0 },
36 { TEGRA210_DMIC_LP_BIQUAD_1_COEF_0, 0x00800000 },
37 { TEGRA210_DMIC_LP_BIQUAD_1_COEF_1, 0x0 },
38 { TEGRA210_DMIC_LP_BIQUAD_1_COEF_2, 0x0 },
39 { TEGRA210_DMIC_LP_BIQUAD_1_COEF_3, 0x0 },
40 { TEGRA210_DMIC_LP_BIQUAD_1_COEF_4, 0x0 },
43 static int __maybe_unused tegra210_dmic_runtime_suspend(struct device *dev)
45 struct tegra210_dmic *dmic = dev_get_drvdata(dev);
47 regcache_cache_only(dmic->regmap, true);
48 regcache_mark_dirty(dmic->regmap);
50 clk_disable_unprepare(dmic->clk_dmic);
55 static int __maybe_unused tegra210_dmic_runtime_resume(struct device *dev)
57 struct tegra210_dmic *dmic = dev_get_drvdata(dev);
60 err = clk_prepare_enable(dmic->clk_dmic);
62 dev_err(dev, "failed to enable DMIC clock, err: %d\n", err);
66 regcache_cache_only(dmic->regmap, false);
67 regcache_sync(dmic->regmap);
72 static int tegra210_dmic_hw_params(struct snd_pcm_substream *substream,
73 struct snd_pcm_hw_params *params,
74 struct snd_soc_dai *dai)
76 struct tegra210_dmic *dmic = snd_soc_dai_get_drvdata(dai);
77 unsigned int srate, clk_rate, channels;
78 struct tegra_cif_conf cif_conf;
79 unsigned long long gain_q23 = DEFAULT_GAIN_Q23;
82 memset(&cif_conf, 0, sizeof(struct tegra_cif_conf));
84 channels = params_channels(params);
86 cif_conf.audio_ch = channels;
88 switch (dmic->ch_select) {
89 case DMIC_CH_SELECT_LEFT:
90 case DMIC_CH_SELECT_RIGHT:
91 cif_conf.client_ch = 1;
93 case DMIC_CH_SELECT_STEREO:
94 cif_conf.client_ch = 2;
97 dev_err(dai->dev, "invalid DMIC client channels\n");
101 srate = params_rate(params);
104 * DMIC clock rate is a multiple of 'Over Sampling Ratio' and
105 * 'Sample Rate'. The supported OSR values are 64, 128 and 256.
107 clk_rate = (DMIC_OSR_FACTOR << dmic->osr_val) * srate;
109 err = clk_set_rate(dmic->clk_dmic, clk_rate);
111 dev_err(dai->dev, "can't set DMIC clock rate %u, err: %d\n",
116 regmap_update_bits(dmic->regmap,
120 TEGRA210_DMIC_CTRL_LRSEL_POLARITY_MASK |
121 TEGRA210_DMIC_CTRL_OSR_MASK |
122 TEGRA210_DMIC_CTRL_CHANNEL_SELECT_MASK,
124 (dmic->lrsel << LRSEL_POL_SHIFT) |
125 (dmic->osr_val << OSR_SHIFT) |
126 ((dmic->ch_select + 1) << CH_SEL_SHIFT));
129 * Use LP filter gain register to apply boost.
130 * Boost Gain Volume control has 100x factor.
132 if (dmic->boost_gain)
133 gain_q23 = div_u64(gain_q23 * dmic->boost_gain, 100);
135 regmap_write(dmic->regmap, TEGRA210_DMIC_LP_FILTER_GAIN,
136 (unsigned int)gain_q23);
138 switch (params_format(params)) {
139 case SNDRV_PCM_FORMAT_S16_LE:
140 cif_conf.audio_bits = TEGRA_ACIF_BITS_16;
142 case SNDRV_PCM_FORMAT_S32_LE:
143 cif_conf.audio_bits = TEGRA_ACIF_BITS_32;
146 dev_err(dai->dev, "unsupported format!\n");
150 cif_conf.client_bits = TEGRA_ACIF_BITS_24;
151 cif_conf.mono_conv = dmic->mono_to_stereo;
152 cif_conf.stereo_conv = dmic->stereo_to_mono;
154 tegra_set_cif(dmic->regmap, TEGRA210_DMIC_TX_CIF_CTRL, &cif_conf);
159 static int tegra210_dmic_get_boost_gain(struct snd_kcontrol *kcontrol,
160 struct snd_ctl_elem_value *ucontrol)
162 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
163 struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
165 ucontrol->value.integer.value[0] = dmic->boost_gain;
170 static int tegra210_dmic_put_boost_gain(struct snd_kcontrol *kcontrol,
171 struct snd_ctl_elem_value *ucontrol)
173 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
174 struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
175 int value = ucontrol->value.integer.value[0];
177 if (value == dmic->boost_gain)
180 dmic->boost_gain = value;
185 static int tegra210_dmic_get_ch_select(struct snd_kcontrol *kcontrol,
186 struct snd_ctl_elem_value *ucontrol)
188 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
189 struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
191 ucontrol->value.enumerated.item[0] = dmic->ch_select;
196 static int tegra210_dmic_put_ch_select(struct snd_kcontrol *kcontrol,
197 struct snd_ctl_elem_value *ucontrol)
199 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
200 struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
201 unsigned int value = ucontrol->value.enumerated.item[0];
203 if (value == dmic->ch_select)
206 dmic->ch_select = value;
211 static int tegra210_dmic_get_mono_to_stereo(struct snd_kcontrol *kcontrol,
212 struct snd_ctl_elem_value *ucontrol)
214 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
215 struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
217 ucontrol->value.enumerated.item[0] = dmic->mono_to_stereo;
222 static int tegra210_dmic_put_mono_to_stereo(struct snd_kcontrol *kcontrol,
223 struct snd_ctl_elem_value *ucontrol)
225 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
226 struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
227 unsigned int value = ucontrol->value.enumerated.item[0];
229 if (value == dmic->mono_to_stereo)
232 dmic->mono_to_stereo = value;
237 static int tegra210_dmic_get_stereo_to_mono(struct snd_kcontrol *kcontrol,
238 struct snd_ctl_elem_value *ucontrol)
240 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
241 struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
243 ucontrol->value.enumerated.item[0] = dmic->stereo_to_mono;
248 static int tegra210_dmic_put_stereo_to_mono(struct snd_kcontrol *kcontrol,
249 struct snd_ctl_elem_value *ucontrol)
251 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
252 struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
253 unsigned int value = ucontrol->value.enumerated.item[0];
255 if (value == dmic->stereo_to_mono)
258 dmic->stereo_to_mono = value;
263 static int tegra210_dmic_get_osr_val(struct snd_kcontrol *kcontrol,
264 struct snd_ctl_elem_value *ucontrol)
266 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
267 struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
269 ucontrol->value.enumerated.item[0] = dmic->osr_val;
274 static int tegra210_dmic_put_osr_val(struct snd_kcontrol *kcontrol,
275 struct snd_ctl_elem_value *ucontrol)
277 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
278 struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
279 unsigned int value = ucontrol->value.enumerated.item[0];
281 if (value == dmic->osr_val)
284 dmic->osr_val = value;
289 static int tegra210_dmic_get_pol_sel(struct snd_kcontrol *kcontrol,
290 struct snd_ctl_elem_value *ucontrol)
292 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
293 struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
295 ucontrol->value.enumerated.item[0] = dmic->lrsel;
300 static int tegra210_dmic_put_pol_sel(struct snd_kcontrol *kcontrol,
301 struct snd_ctl_elem_value *ucontrol)
303 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
304 struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
305 unsigned int value = ucontrol->value.enumerated.item[0];
307 if (value == dmic->lrsel)
315 static const struct snd_soc_dai_ops tegra210_dmic_dai_ops = {
316 .hw_params = tegra210_dmic_hw_params,
319 static struct snd_soc_dai_driver tegra210_dmic_dais[] = {
323 .stream_name = "CIF-Capture",
326 .rates = SNDRV_PCM_RATE_8000_48000,
327 .formats = SNDRV_PCM_FMTBIT_S16_LE |
328 SNDRV_PCM_FMTBIT_S32_LE,
334 .stream_name = "DAP-Capture",
337 .rates = SNDRV_PCM_RATE_8000_48000,
338 .formats = SNDRV_PCM_FMTBIT_S16_LE |
339 SNDRV_PCM_FMTBIT_S32_LE,
341 .ops = &tegra210_dmic_dai_ops,
346 static const struct snd_soc_dapm_widget tegra210_dmic_widgets[] = {
347 SND_SOC_DAPM_AIF_OUT("TX", NULL, 0, TEGRA210_DMIC_ENABLE, 0, 0),
348 SND_SOC_DAPM_MIC("MIC", NULL),
351 static const struct snd_soc_dapm_route tegra210_dmic_routes[] = {
352 { "XBAR-RX", NULL, "XBAR-Capture" },
353 { "XBAR-Capture", NULL, "CIF-Capture" },
354 { "CIF-Capture", NULL, "TX" },
355 { "TX", NULL, "DAP-Capture" },
356 { "DAP-Capture", NULL, "MIC" },
359 static const char * const tegra210_dmic_ch_select[] = {
360 "Left", "Right", "Stereo",
363 static const struct soc_enum tegra210_dmic_ch_enum =
364 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_dmic_ch_select),
365 tegra210_dmic_ch_select);
367 static const char * const tegra210_dmic_mono_conv_text[] = {
371 static const char * const tegra210_dmic_stereo_conv_text[] = {
375 static const struct soc_enum tegra210_dmic_mono_conv_enum =
376 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_dmic_mono_conv_text),
377 tegra210_dmic_mono_conv_text);
379 static const struct soc_enum tegra210_dmic_stereo_conv_enum =
380 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_dmic_stereo_conv_text),
381 tegra210_dmic_stereo_conv_text);
383 static const char * const tegra210_dmic_osr_text[] = {
384 "OSR_64", "OSR_128", "OSR_256",
387 static const struct soc_enum tegra210_dmic_osr_enum =
388 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_dmic_osr_text),
389 tegra210_dmic_osr_text);
391 static const char * const tegra210_dmic_lrsel_text[] = {
395 static const struct soc_enum tegra210_dmic_lrsel_enum =
396 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_dmic_lrsel_text),
397 tegra210_dmic_lrsel_text);
399 static const struct snd_kcontrol_new tegra210_dmic_controls[] = {
400 SOC_SINGLE_EXT("Boost Gain Volume", 0, 0, MAX_BOOST_GAIN, 0,
401 tegra210_dmic_get_boost_gain,
402 tegra210_dmic_put_boost_gain),
403 SOC_ENUM_EXT("Channel Select", tegra210_dmic_ch_enum,
404 tegra210_dmic_get_ch_select, tegra210_dmic_put_ch_select),
405 SOC_ENUM_EXT("Mono To Stereo",
406 tegra210_dmic_mono_conv_enum,
407 tegra210_dmic_get_mono_to_stereo,
408 tegra210_dmic_put_mono_to_stereo),
409 SOC_ENUM_EXT("Stereo To Mono",
410 tegra210_dmic_stereo_conv_enum,
411 tegra210_dmic_get_stereo_to_mono,
412 tegra210_dmic_put_stereo_to_mono),
413 SOC_ENUM_EXT("OSR Value", tegra210_dmic_osr_enum,
414 tegra210_dmic_get_osr_val, tegra210_dmic_put_osr_val),
415 SOC_ENUM_EXT("LR Polarity Select", tegra210_dmic_lrsel_enum,
416 tegra210_dmic_get_pol_sel, tegra210_dmic_put_pol_sel),
419 static const struct snd_soc_component_driver tegra210_dmic_compnt = {
420 .dapm_widgets = tegra210_dmic_widgets,
421 .num_dapm_widgets = ARRAY_SIZE(tegra210_dmic_widgets),
422 .dapm_routes = tegra210_dmic_routes,
423 .num_dapm_routes = ARRAY_SIZE(tegra210_dmic_routes),
424 .controls = tegra210_dmic_controls,
425 .num_controls = ARRAY_SIZE(tegra210_dmic_controls),
428 static bool tegra210_dmic_wr_reg(struct device *dev, unsigned int reg)
431 case TEGRA210_DMIC_TX_INT_MASK ... TEGRA210_DMIC_TX_CIF_CTRL:
432 case TEGRA210_DMIC_ENABLE ... TEGRA210_DMIC_CG:
433 case TEGRA210_DMIC_CTRL:
434 case TEGRA210_DMIC_DBG_CTRL:
435 case TEGRA210_DMIC_DCR_BIQUAD_0_COEF_4 ... TEGRA210_DMIC_LP_BIQUAD_1_COEF_4:
442 static bool tegra210_dmic_rd_reg(struct device *dev, unsigned int reg)
444 if (tegra210_dmic_wr_reg(dev, reg))
448 case TEGRA210_DMIC_TX_STATUS:
449 case TEGRA210_DMIC_TX_INT_STATUS:
450 case TEGRA210_DMIC_STATUS:
451 case TEGRA210_DMIC_INT_STATUS:
458 static bool tegra210_dmic_volatile_reg(struct device *dev, unsigned int reg)
461 case TEGRA210_DMIC_TX_STATUS:
462 case TEGRA210_DMIC_TX_INT_STATUS:
463 case TEGRA210_DMIC_TX_INT_SET:
464 case TEGRA210_DMIC_SOFT_RESET:
465 case TEGRA210_DMIC_STATUS:
466 case TEGRA210_DMIC_INT_STATUS:
473 static const struct regmap_config tegra210_dmic_regmap_config = {
477 .max_register = TEGRA210_DMIC_LP_BIQUAD_1_COEF_4,
478 .writeable_reg = tegra210_dmic_wr_reg,
479 .readable_reg = tegra210_dmic_rd_reg,
480 .volatile_reg = tegra210_dmic_volatile_reg,
481 .reg_defaults = tegra210_dmic_reg_defaults,
482 .num_reg_defaults = ARRAY_SIZE(tegra210_dmic_reg_defaults),
483 .cache_type = REGCACHE_FLAT,
486 static int tegra210_dmic_probe(struct platform_device *pdev)
488 struct device *dev = &pdev->dev;
489 struct tegra210_dmic *dmic;
493 dmic = devm_kzalloc(dev, sizeof(*dmic), GFP_KERNEL);
497 dmic->osr_val = DMIC_OSR_64;
498 dmic->ch_select = DMIC_CH_SELECT_STEREO;
499 dmic->lrsel = DMIC_LRSEL_LEFT;
500 dmic->boost_gain = 0;
501 dmic->stereo_to_mono = 0; /* "CH0" */
503 dev_set_drvdata(dev, dmic);
505 dmic->clk_dmic = devm_clk_get(dev, "dmic");
506 if (IS_ERR(dmic->clk_dmic)) {
507 dev_err(dev, "can't retrieve DMIC clock\n");
508 return PTR_ERR(dmic->clk_dmic);
511 regs = devm_platform_ioremap_resource(pdev, 0);
513 return PTR_ERR(regs);
515 dmic->regmap = devm_regmap_init_mmio(dev, regs,
516 &tegra210_dmic_regmap_config);
517 if (IS_ERR(dmic->regmap)) {
518 dev_err(dev, "regmap init failed\n");
519 return PTR_ERR(dmic->regmap);
522 regcache_cache_only(dmic->regmap, true);
524 err = devm_snd_soc_register_component(dev, &tegra210_dmic_compnt,
526 ARRAY_SIZE(tegra210_dmic_dais));
528 dev_err(dev, "can't register DMIC component, err: %d\n", err);
532 pm_runtime_enable(dev);
537 static int tegra210_dmic_remove(struct platform_device *pdev)
539 pm_runtime_disable(&pdev->dev);
544 static const struct dev_pm_ops tegra210_dmic_pm_ops = {
545 SET_RUNTIME_PM_OPS(tegra210_dmic_runtime_suspend,
546 tegra210_dmic_runtime_resume, NULL)
547 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
548 pm_runtime_force_resume)
551 static const struct of_device_id tegra210_dmic_of_match[] = {
552 { .compatible = "nvidia,tegra210-dmic" },
555 MODULE_DEVICE_TABLE(of, tegra210_dmic_of_match);
557 static struct platform_driver tegra210_dmic_driver = {
559 .name = "tegra210-dmic",
560 .of_match_table = tegra210_dmic_of_match,
561 .pm = &tegra210_dmic_pm_ops,
563 .probe = tegra210_dmic_probe,
564 .remove = tegra210_dmic_remove,
566 module_platform_driver(tegra210_dmic_driver)
568 MODULE_AUTHOR("Rahul Mittal <rmittal@nvidia.com>");
569 MODULE_DESCRIPTION("Tegra210 ASoC DMIC driver");
570 MODULE_LICENSE("GPL v2");