1 // SPDX-License-Identifier: GPL-2.0-only
2 // SPDX-FileCopyrightText: Copyright (c) 2020-2024 NVIDIA CORPORATION & AFFILIATES.
3 // All rights reserved.
5 // tegra210_dmic.c - Tegra210 DMIC driver
8 #include <linux/device.h>
9 #include <linux/math64.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.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_S24_LE
:
143 case SNDRV_PCM_FORMAT_S32_LE
:
144 cif_conf
.audio_bits
= TEGRA_ACIF_BITS_32
;
147 dev_err(dai
->dev
, "unsupported format!\n");
151 cif_conf
.client_bits
= TEGRA_ACIF_BITS_24
;
152 cif_conf
.mono_conv
= dmic
->mono_to_stereo
;
153 cif_conf
.stereo_conv
= dmic
->stereo_to_mono
;
155 tegra_set_cif(dmic
->regmap
, TEGRA210_DMIC_TX_CIF_CTRL
, &cif_conf
);
160 static int tegra210_dmic_get_boost_gain(struct snd_kcontrol
*kcontrol
,
161 struct snd_ctl_elem_value
*ucontrol
)
163 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
164 struct tegra210_dmic
*dmic
= snd_soc_component_get_drvdata(comp
);
166 ucontrol
->value
.integer
.value
[0] = dmic
->boost_gain
;
171 static int tegra210_dmic_put_boost_gain(struct snd_kcontrol
*kcontrol
,
172 struct snd_ctl_elem_value
*ucontrol
)
174 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
175 struct tegra210_dmic
*dmic
= snd_soc_component_get_drvdata(comp
);
176 int value
= ucontrol
->value
.integer
.value
[0];
178 if (value
== dmic
->boost_gain
)
181 dmic
->boost_gain
= value
;
186 static int tegra210_dmic_get_ch_select(struct snd_kcontrol
*kcontrol
,
187 struct snd_ctl_elem_value
*ucontrol
)
189 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
190 struct tegra210_dmic
*dmic
= snd_soc_component_get_drvdata(comp
);
192 ucontrol
->value
.enumerated
.item
[0] = dmic
->ch_select
;
197 static int tegra210_dmic_put_ch_select(struct snd_kcontrol
*kcontrol
,
198 struct snd_ctl_elem_value
*ucontrol
)
200 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
201 struct tegra210_dmic
*dmic
= snd_soc_component_get_drvdata(comp
);
202 unsigned int value
= ucontrol
->value
.enumerated
.item
[0];
204 if (value
== dmic
->ch_select
)
207 dmic
->ch_select
= value
;
212 static int tegra210_dmic_get_mono_to_stereo(struct snd_kcontrol
*kcontrol
,
213 struct snd_ctl_elem_value
*ucontrol
)
215 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
216 struct tegra210_dmic
*dmic
= snd_soc_component_get_drvdata(comp
);
218 ucontrol
->value
.enumerated
.item
[0] = dmic
->mono_to_stereo
;
223 static int tegra210_dmic_put_mono_to_stereo(struct snd_kcontrol
*kcontrol
,
224 struct snd_ctl_elem_value
*ucontrol
)
226 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
227 struct tegra210_dmic
*dmic
= snd_soc_component_get_drvdata(comp
);
228 unsigned int value
= ucontrol
->value
.enumerated
.item
[0];
230 if (value
== dmic
->mono_to_stereo
)
233 dmic
->mono_to_stereo
= value
;
238 static int tegra210_dmic_get_stereo_to_mono(struct snd_kcontrol
*kcontrol
,
239 struct snd_ctl_elem_value
*ucontrol
)
241 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
242 struct tegra210_dmic
*dmic
= snd_soc_component_get_drvdata(comp
);
244 ucontrol
->value
.enumerated
.item
[0] = dmic
->stereo_to_mono
;
249 static int tegra210_dmic_put_stereo_to_mono(struct snd_kcontrol
*kcontrol
,
250 struct snd_ctl_elem_value
*ucontrol
)
252 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
253 struct tegra210_dmic
*dmic
= snd_soc_component_get_drvdata(comp
);
254 unsigned int value
= ucontrol
->value
.enumerated
.item
[0];
256 if (value
== dmic
->stereo_to_mono
)
259 dmic
->stereo_to_mono
= value
;
264 static int tegra210_dmic_get_osr_val(struct snd_kcontrol
*kcontrol
,
265 struct snd_ctl_elem_value
*ucontrol
)
267 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
268 struct tegra210_dmic
*dmic
= snd_soc_component_get_drvdata(comp
);
270 ucontrol
->value
.enumerated
.item
[0] = dmic
->osr_val
;
275 static int tegra210_dmic_put_osr_val(struct snd_kcontrol
*kcontrol
,
276 struct snd_ctl_elem_value
*ucontrol
)
278 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
279 struct tegra210_dmic
*dmic
= snd_soc_component_get_drvdata(comp
);
280 unsigned int value
= ucontrol
->value
.enumerated
.item
[0];
282 if (value
== dmic
->osr_val
)
285 dmic
->osr_val
= value
;
290 static int tegra210_dmic_get_pol_sel(struct snd_kcontrol
*kcontrol
,
291 struct snd_ctl_elem_value
*ucontrol
)
293 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
294 struct tegra210_dmic
*dmic
= snd_soc_component_get_drvdata(comp
);
296 ucontrol
->value
.enumerated
.item
[0] = dmic
->lrsel
;
301 static int tegra210_dmic_put_pol_sel(struct snd_kcontrol
*kcontrol
,
302 struct snd_ctl_elem_value
*ucontrol
)
304 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
305 struct tegra210_dmic
*dmic
= snd_soc_component_get_drvdata(comp
);
306 unsigned int value
= ucontrol
->value
.enumerated
.item
[0];
308 if (value
== dmic
->lrsel
)
316 static const struct snd_soc_dai_ops tegra210_dmic_dai_ops
= {
317 .hw_params
= tegra210_dmic_hw_params
,
320 static struct snd_soc_dai_driver tegra210_dmic_dais
[] = {
324 .stream_name
= "CIF-Capture",
327 .rates
= SNDRV_PCM_RATE_8000_48000
,
328 .formats
= SNDRV_PCM_FMTBIT_S16_LE
|
329 SNDRV_PCM_FMTBIT_S24_LE
|
330 SNDRV_PCM_FMTBIT_S32_LE
,
336 .stream_name
= "DAP-Capture",
339 .rates
= SNDRV_PCM_RATE_8000_48000
,
340 .formats
= SNDRV_PCM_FMTBIT_S16_LE
|
341 SNDRV_PCM_FMTBIT_S24_LE
|
342 SNDRV_PCM_FMTBIT_S32_LE
,
344 .ops
= &tegra210_dmic_dai_ops
,
349 static const struct snd_soc_dapm_widget tegra210_dmic_widgets
[] = {
350 SND_SOC_DAPM_AIF_OUT("TX", NULL
, 0, TEGRA210_DMIC_ENABLE
, 0, 0),
351 SND_SOC_DAPM_MIC("MIC", NULL
),
354 static const struct snd_soc_dapm_route tegra210_dmic_routes
[] = {
355 { "XBAR-RX", NULL
, "XBAR-Capture" },
356 { "XBAR-Capture", NULL
, "CIF-Capture" },
357 { "CIF-Capture", NULL
, "TX" },
358 { "TX", NULL
, "DAP-Capture" },
359 { "DAP-Capture", NULL
, "MIC" },
362 static const char * const tegra210_dmic_ch_select
[] = {
363 "Left", "Right", "Stereo",
366 static const struct soc_enum tegra210_dmic_ch_enum
=
367 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_dmic_ch_select
),
368 tegra210_dmic_ch_select
);
370 static const char * const tegra210_dmic_mono_conv_text
[] = {
374 static const char * const tegra210_dmic_stereo_conv_text
[] = {
378 static const struct soc_enum tegra210_dmic_mono_conv_enum
=
379 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_dmic_mono_conv_text
),
380 tegra210_dmic_mono_conv_text
);
382 static const struct soc_enum tegra210_dmic_stereo_conv_enum
=
383 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_dmic_stereo_conv_text
),
384 tegra210_dmic_stereo_conv_text
);
386 static const char * const tegra210_dmic_osr_text
[] = {
387 "OSR_64", "OSR_128", "OSR_256",
390 static const struct soc_enum tegra210_dmic_osr_enum
=
391 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_dmic_osr_text
),
392 tegra210_dmic_osr_text
);
394 static const char * const tegra210_dmic_lrsel_text
[] = {
398 static const struct soc_enum tegra210_dmic_lrsel_enum
=
399 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_dmic_lrsel_text
),
400 tegra210_dmic_lrsel_text
);
402 static const struct snd_kcontrol_new tegra210_dmic_controls
[] = {
403 SOC_SINGLE_EXT("Boost Gain Volume", 0, 0, MAX_BOOST_GAIN
, 0,
404 tegra210_dmic_get_boost_gain
,
405 tegra210_dmic_put_boost_gain
),
406 SOC_ENUM_EXT("Channel Select", tegra210_dmic_ch_enum
,
407 tegra210_dmic_get_ch_select
, tegra210_dmic_put_ch_select
),
408 SOC_ENUM_EXT("Mono To Stereo",
409 tegra210_dmic_mono_conv_enum
,
410 tegra210_dmic_get_mono_to_stereo
,
411 tegra210_dmic_put_mono_to_stereo
),
412 SOC_ENUM_EXT("Stereo To Mono",
413 tegra210_dmic_stereo_conv_enum
,
414 tegra210_dmic_get_stereo_to_mono
,
415 tegra210_dmic_put_stereo_to_mono
),
416 SOC_ENUM_EXT("OSR Value", tegra210_dmic_osr_enum
,
417 tegra210_dmic_get_osr_val
, tegra210_dmic_put_osr_val
),
418 SOC_ENUM_EXT("LR Polarity Select", tegra210_dmic_lrsel_enum
,
419 tegra210_dmic_get_pol_sel
, tegra210_dmic_put_pol_sel
),
422 static const struct snd_soc_component_driver tegra210_dmic_compnt
= {
423 .dapm_widgets
= tegra210_dmic_widgets
,
424 .num_dapm_widgets
= ARRAY_SIZE(tegra210_dmic_widgets
),
425 .dapm_routes
= tegra210_dmic_routes
,
426 .num_dapm_routes
= ARRAY_SIZE(tegra210_dmic_routes
),
427 .controls
= tegra210_dmic_controls
,
428 .num_controls
= ARRAY_SIZE(tegra210_dmic_controls
),
431 static bool tegra210_dmic_wr_reg(struct device
*dev
, unsigned int reg
)
434 case TEGRA210_DMIC_TX_INT_MASK
... TEGRA210_DMIC_TX_CIF_CTRL
:
435 case TEGRA210_DMIC_ENABLE
... TEGRA210_DMIC_CG
:
436 case TEGRA210_DMIC_CTRL
:
437 case TEGRA210_DMIC_DBG_CTRL
:
438 case TEGRA210_DMIC_DCR_BIQUAD_0_COEF_4
... TEGRA210_DMIC_LP_BIQUAD_1_COEF_4
:
445 static bool tegra210_dmic_rd_reg(struct device
*dev
, unsigned int reg
)
447 if (tegra210_dmic_wr_reg(dev
, reg
))
451 case TEGRA210_DMIC_TX_STATUS
:
452 case TEGRA210_DMIC_TX_INT_STATUS
:
453 case TEGRA210_DMIC_STATUS
:
454 case TEGRA210_DMIC_INT_STATUS
:
461 static bool tegra210_dmic_volatile_reg(struct device
*dev
, unsigned int reg
)
464 case TEGRA210_DMIC_TX_STATUS
:
465 case TEGRA210_DMIC_TX_INT_STATUS
:
466 case TEGRA210_DMIC_TX_INT_SET
:
467 case TEGRA210_DMIC_SOFT_RESET
:
468 case TEGRA210_DMIC_STATUS
:
469 case TEGRA210_DMIC_INT_STATUS
:
476 static const struct regmap_config tegra210_dmic_regmap_config
= {
480 .max_register
= TEGRA210_DMIC_LP_BIQUAD_1_COEF_4
,
481 .writeable_reg
= tegra210_dmic_wr_reg
,
482 .readable_reg
= tegra210_dmic_rd_reg
,
483 .volatile_reg
= tegra210_dmic_volatile_reg
,
484 .reg_defaults
= tegra210_dmic_reg_defaults
,
485 .num_reg_defaults
= ARRAY_SIZE(tegra210_dmic_reg_defaults
),
486 .cache_type
= REGCACHE_FLAT
,
489 static int tegra210_dmic_probe(struct platform_device
*pdev
)
491 struct device
*dev
= &pdev
->dev
;
492 struct tegra210_dmic
*dmic
;
496 dmic
= devm_kzalloc(dev
, sizeof(*dmic
), GFP_KERNEL
);
500 dmic
->osr_val
= DMIC_OSR_64
;
501 dmic
->ch_select
= DMIC_CH_SELECT_STEREO
;
502 dmic
->lrsel
= DMIC_LRSEL_LEFT
;
503 dmic
->boost_gain
= 0;
504 dmic
->stereo_to_mono
= 0; /* "CH0" */
506 dev_set_drvdata(dev
, dmic
);
508 dmic
->clk_dmic
= devm_clk_get(dev
, "dmic");
509 if (IS_ERR(dmic
->clk_dmic
)) {
510 dev_err(dev
, "can't retrieve DMIC clock\n");
511 return PTR_ERR(dmic
->clk_dmic
);
514 regs
= devm_platform_ioremap_resource(pdev
, 0);
516 return PTR_ERR(regs
);
518 dmic
->regmap
= devm_regmap_init_mmio(dev
, regs
,
519 &tegra210_dmic_regmap_config
);
520 if (IS_ERR(dmic
->regmap
)) {
521 dev_err(dev
, "regmap init failed\n");
522 return PTR_ERR(dmic
->regmap
);
525 regcache_cache_only(dmic
->regmap
, true);
527 err
= devm_snd_soc_register_component(dev
, &tegra210_dmic_compnt
,
529 ARRAY_SIZE(tegra210_dmic_dais
));
531 dev_err(dev
, "can't register DMIC component, err: %d\n", err
);
535 pm_runtime_enable(dev
);
540 static void tegra210_dmic_remove(struct platform_device
*pdev
)
542 pm_runtime_disable(&pdev
->dev
);
545 static const struct dev_pm_ops tegra210_dmic_pm_ops
= {
546 SET_RUNTIME_PM_OPS(tegra210_dmic_runtime_suspend
,
547 tegra210_dmic_runtime_resume
, NULL
)
548 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
549 pm_runtime_force_resume
)
552 static const struct of_device_id tegra210_dmic_of_match
[] = {
553 { .compatible
= "nvidia,tegra210-dmic" },
556 MODULE_DEVICE_TABLE(of
, tegra210_dmic_of_match
);
558 static struct platform_driver tegra210_dmic_driver
= {
560 .name
= "tegra210-dmic",
561 .of_match_table
= tegra210_dmic_of_match
,
562 .pm
= &tegra210_dmic_pm_ops
,
564 .probe
= tegra210_dmic_probe
,
565 .remove
= tegra210_dmic_remove
,
567 module_platform_driver(tegra210_dmic_driver
)
569 MODULE_AUTHOR("Rahul Mittal <rmittal@nvidia.com>");
570 MODULE_DESCRIPTION("Tegra210 ASoC DMIC driver");
571 MODULE_LICENSE("GPL v2");