1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // tegra210_dmic.c - Tegra210 DMIC driver
4*4882a593Smuzhiyun //
5*4882a593Smuzhiyun // Copyright (c) 2020 NVIDIA CORPORATION. All rights reserved.
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/clk.h>
8*4882a593Smuzhiyun #include <linux/device.h>
9*4882a593Smuzhiyun #include <linux/math64.h>
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/of_device.h>
12*4882a593Smuzhiyun #include <linux/platform_device.h>
13*4882a593Smuzhiyun #include <linux/pm_runtime.h>
14*4882a593Smuzhiyun #include <linux/regmap.h>
15*4882a593Smuzhiyun #include <sound/core.h>
16*4882a593Smuzhiyun #include <sound/pcm_params.h>
17*4882a593Smuzhiyun #include <sound/soc.h>
18*4882a593Smuzhiyun #include "tegra210_dmic.h"
19*4882a593Smuzhiyun #include "tegra_cif.h"
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun static const struct reg_default tegra210_dmic_reg_defaults[] = {
22*4882a593Smuzhiyun { TEGRA210_DMIC_TX_INT_MASK, 0x00000001 },
23*4882a593Smuzhiyun { TEGRA210_DMIC_TX_CIF_CTRL, 0x00007700 },
24*4882a593Smuzhiyun { TEGRA210_DMIC_CG, 0x1 },
25*4882a593Smuzhiyun { TEGRA210_DMIC_CTRL, 0x00000301 },
26*4882a593Smuzhiyun /* Below enables all filters - DCR, LP and SC */
27*4882a593Smuzhiyun { TEGRA210_DMIC_DBG_CTRL, 0xe },
28*4882a593Smuzhiyun /* Below as per latest POR value */
29*4882a593Smuzhiyun { TEGRA210_DMIC_DCR_BIQUAD_0_COEF_4, 0x0 },
30*4882a593Smuzhiyun /* LP filter is configured for pass through and used to apply gain */
31*4882a593Smuzhiyun { TEGRA210_DMIC_LP_BIQUAD_0_COEF_0, 0x00800000 },
32*4882a593Smuzhiyun { TEGRA210_DMIC_LP_BIQUAD_0_COEF_1, 0x0 },
33*4882a593Smuzhiyun { TEGRA210_DMIC_LP_BIQUAD_0_COEF_2, 0x0 },
34*4882a593Smuzhiyun { TEGRA210_DMIC_LP_BIQUAD_0_COEF_3, 0x0 },
35*4882a593Smuzhiyun { TEGRA210_DMIC_LP_BIQUAD_0_COEF_4, 0x0 },
36*4882a593Smuzhiyun { TEGRA210_DMIC_LP_BIQUAD_1_COEF_0, 0x00800000 },
37*4882a593Smuzhiyun { TEGRA210_DMIC_LP_BIQUAD_1_COEF_1, 0x0 },
38*4882a593Smuzhiyun { TEGRA210_DMIC_LP_BIQUAD_1_COEF_2, 0x0 },
39*4882a593Smuzhiyun { TEGRA210_DMIC_LP_BIQUAD_1_COEF_3, 0x0 },
40*4882a593Smuzhiyun { TEGRA210_DMIC_LP_BIQUAD_1_COEF_4, 0x0 },
41*4882a593Smuzhiyun };
42*4882a593Smuzhiyun
tegra210_dmic_runtime_suspend(struct device * dev)43*4882a593Smuzhiyun static int __maybe_unused tegra210_dmic_runtime_suspend(struct device *dev)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun struct tegra210_dmic *dmic = dev_get_drvdata(dev);
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun regcache_cache_only(dmic->regmap, true);
48*4882a593Smuzhiyun regcache_mark_dirty(dmic->regmap);
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun clk_disable_unprepare(dmic->clk_dmic);
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun return 0;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
tegra210_dmic_runtime_resume(struct device * dev)55*4882a593Smuzhiyun static int __maybe_unused tegra210_dmic_runtime_resume(struct device *dev)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun struct tegra210_dmic *dmic = dev_get_drvdata(dev);
58*4882a593Smuzhiyun int err;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun err = clk_prepare_enable(dmic->clk_dmic);
61*4882a593Smuzhiyun if (err) {
62*4882a593Smuzhiyun dev_err(dev, "failed to enable DMIC clock, err: %d\n", err);
63*4882a593Smuzhiyun return err;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun regcache_cache_only(dmic->regmap, false);
67*4882a593Smuzhiyun regcache_sync(dmic->regmap);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun return 0;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
tegra210_dmic_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)72*4882a593Smuzhiyun static int tegra210_dmic_hw_params(struct snd_pcm_substream *substream,
73*4882a593Smuzhiyun struct snd_pcm_hw_params *params,
74*4882a593Smuzhiyun struct snd_soc_dai *dai)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun struct tegra210_dmic *dmic = snd_soc_dai_get_drvdata(dai);
77*4882a593Smuzhiyun unsigned int srate, clk_rate, channels;
78*4882a593Smuzhiyun struct tegra_cif_conf cif_conf;
79*4882a593Smuzhiyun unsigned long long gain_q23 = DEFAULT_GAIN_Q23;
80*4882a593Smuzhiyun int err;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun memset(&cif_conf, 0, sizeof(struct tegra_cif_conf));
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun channels = params_channels(params);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun cif_conf.audio_ch = channels;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun switch (dmic->ch_select) {
89*4882a593Smuzhiyun case DMIC_CH_SELECT_LEFT:
90*4882a593Smuzhiyun case DMIC_CH_SELECT_RIGHT:
91*4882a593Smuzhiyun cif_conf.client_ch = 1;
92*4882a593Smuzhiyun break;
93*4882a593Smuzhiyun case DMIC_CH_SELECT_STEREO:
94*4882a593Smuzhiyun cif_conf.client_ch = 2;
95*4882a593Smuzhiyun break;
96*4882a593Smuzhiyun default:
97*4882a593Smuzhiyun dev_err(dai->dev, "invalid DMIC client channels\n");
98*4882a593Smuzhiyun return -EINVAL;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun srate = params_rate(params);
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun * DMIC clock rate is a multiple of 'Over Sampling Ratio' and
105*4882a593Smuzhiyun * 'Sample Rate'. The supported OSR values are 64, 128 and 256.
106*4882a593Smuzhiyun */
107*4882a593Smuzhiyun clk_rate = (DMIC_OSR_FACTOR << dmic->osr_val) * srate;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun err = clk_set_rate(dmic->clk_dmic, clk_rate);
110*4882a593Smuzhiyun if (err) {
111*4882a593Smuzhiyun dev_err(dai->dev, "can't set DMIC clock rate %u, err: %d\n",
112*4882a593Smuzhiyun clk_rate, err);
113*4882a593Smuzhiyun return err;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun regmap_update_bits(dmic->regmap,
117*4882a593Smuzhiyun /* Reg */
118*4882a593Smuzhiyun TEGRA210_DMIC_CTRL,
119*4882a593Smuzhiyun /* Mask */
120*4882a593Smuzhiyun TEGRA210_DMIC_CTRL_LRSEL_POLARITY_MASK |
121*4882a593Smuzhiyun TEGRA210_DMIC_CTRL_OSR_MASK |
122*4882a593Smuzhiyun TEGRA210_DMIC_CTRL_CHANNEL_SELECT_MASK,
123*4882a593Smuzhiyun /* Value */
124*4882a593Smuzhiyun (dmic->lrsel << LRSEL_POL_SHIFT) |
125*4882a593Smuzhiyun (dmic->osr_val << OSR_SHIFT) |
126*4882a593Smuzhiyun ((dmic->ch_select + 1) << CH_SEL_SHIFT));
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun * Use LP filter gain register to apply boost.
130*4882a593Smuzhiyun * Boost Gain Volume control has 100x factor.
131*4882a593Smuzhiyun */
132*4882a593Smuzhiyun if (dmic->boost_gain)
133*4882a593Smuzhiyun gain_q23 = div_u64(gain_q23 * dmic->boost_gain, 100);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun regmap_write(dmic->regmap, TEGRA210_DMIC_LP_FILTER_GAIN,
136*4882a593Smuzhiyun (unsigned int)gain_q23);
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun switch (params_format(params)) {
139*4882a593Smuzhiyun case SNDRV_PCM_FORMAT_S16_LE:
140*4882a593Smuzhiyun cif_conf.audio_bits = TEGRA_ACIF_BITS_16;
141*4882a593Smuzhiyun break;
142*4882a593Smuzhiyun case SNDRV_PCM_FORMAT_S32_LE:
143*4882a593Smuzhiyun cif_conf.audio_bits = TEGRA_ACIF_BITS_32;
144*4882a593Smuzhiyun break;
145*4882a593Smuzhiyun default:
146*4882a593Smuzhiyun dev_err(dai->dev, "unsupported format!\n");
147*4882a593Smuzhiyun return -EOPNOTSUPP;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun cif_conf.client_bits = TEGRA_ACIF_BITS_24;
151*4882a593Smuzhiyun cif_conf.mono_conv = dmic->mono_to_stereo;
152*4882a593Smuzhiyun cif_conf.stereo_conv = dmic->stereo_to_mono;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun tegra_set_cif(dmic->regmap, TEGRA210_DMIC_TX_CIF_CTRL, &cif_conf);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun return 0;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
tegra210_dmic_get_boost_gain(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)159*4882a593Smuzhiyun static int tegra210_dmic_get_boost_gain(struct snd_kcontrol *kcontrol,
160*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
163*4882a593Smuzhiyun struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun ucontrol->value.integer.value[0] = dmic->boost_gain;
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun return 0;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
tegra210_dmic_put_boost_gain(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)170*4882a593Smuzhiyun static int tegra210_dmic_put_boost_gain(struct snd_kcontrol *kcontrol,
171*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
174*4882a593Smuzhiyun struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
175*4882a593Smuzhiyun int value = ucontrol->value.integer.value[0];
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun if (value == dmic->boost_gain)
178*4882a593Smuzhiyun return 0;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun dmic->boost_gain = value;
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun return 1;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
tegra210_dmic_get_ch_select(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)185*4882a593Smuzhiyun static int tegra210_dmic_get_ch_select(struct snd_kcontrol *kcontrol,
186*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
189*4882a593Smuzhiyun struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun ucontrol->value.enumerated.item[0] = dmic->ch_select;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun return 0;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
tegra210_dmic_put_ch_select(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)196*4882a593Smuzhiyun static int tegra210_dmic_put_ch_select(struct snd_kcontrol *kcontrol,
197*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
200*4882a593Smuzhiyun struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
201*4882a593Smuzhiyun unsigned int value = ucontrol->value.enumerated.item[0];
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun if (value == dmic->ch_select)
204*4882a593Smuzhiyun return 0;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun dmic->ch_select = value;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun return 1;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun
tegra210_dmic_get_mono_to_stereo(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)211*4882a593Smuzhiyun static int tegra210_dmic_get_mono_to_stereo(struct snd_kcontrol *kcontrol,
212*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
215*4882a593Smuzhiyun struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun ucontrol->value.enumerated.item[0] = dmic->mono_to_stereo;
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun return 0;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun
tegra210_dmic_put_mono_to_stereo(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)222*4882a593Smuzhiyun static int tegra210_dmic_put_mono_to_stereo(struct snd_kcontrol *kcontrol,
223*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
226*4882a593Smuzhiyun struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
227*4882a593Smuzhiyun unsigned int value = ucontrol->value.enumerated.item[0];
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun if (value == dmic->mono_to_stereo)
230*4882a593Smuzhiyun return 0;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun dmic->mono_to_stereo = value;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun return 1;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun
tegra210_dmic_get_stereo_to_mono(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)237*4882a593Smuzhiyun static int tegra210_dmic_get_stereo_to_mono(struct snd_kcontrol *kcontrol,
238*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
241*4882a593Smuzhiyun struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun ucontrol->value.enumerated.item[0] = dmic->stereo_to_mono;
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun return 0;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
tegra210_dmic_put_stereo_to_mono(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)248*4882a593Smuzhiyun static int tegra210_dmic_put_stereo_to_mono(struct snd_kcontrol *kcontrol,
249*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
252*4882a593Smuzhiyun struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
253*4882a593Smuzhiyun unsigned int value = ucontrol->value.enumerated.item[0];
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun if (value == dmic->stereo_to_mono)
256*4882a593Smuzhiyun return 0;
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun dmic->stereo_to_mono = value;
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun return 1;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
tegra210_dmic_get_osr_val(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)263*4882a593Smuzhiyun static int tegra210_dmic_get_osr_val(struct snd_kcontrol *kcontrol,
264*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
267*4882a593Smuzhiyun struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun ucontrol->value.enumerated.item[0] = dmic->osr_val;
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun return 0;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
tegra210_dmic_put_osr_val(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)274*4882a593Smuzhiyun static int tegra210_dmic_put_osr_val(struct snd_kcontrol *kcontrol,
275*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
278*4882a593Smuzhiyun struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
279*4882a593Smuzhiyun unsigned int value = ucontrol->value.enumerated.item[0];
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun if (value == dmic->osr_val)
282*4882a593Smuzhiyun return 0;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun dmic->osr_val = value;
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun return 1;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun
tegra210_dmic_get_pol_sel(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)289*4882a593Smuzhiyun static int tegra210_dmic_get_pol_sel(struct snd_kcontrol *kcontrol,
290*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
293*4882a593Smuzhiyun struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun ucontrol->value.enumerated.item[0] = dmic->lrsel;
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun return 0;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
tegra210_dmic_put_pol_sel(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)300*4882a593Smuzhiyun static int tegra210_dmic_put_pol_sel(struct snd_kcontrol *kcontrol,
301*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
302*4882a593Smuzhiyun {
303*4882a593Smuzhiyun struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
304*4882a593Smuzhiyun struct tegra210_dmic *dmic = snd_soc_component_get_drvdata(comp);
305*4882a593Smuzhiyun unsigned int value = ucontrol->value.enumerated.item[0];
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun if (value == dmic->lrsel)
308*4882a593Smuzhiyun return 0;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun dmic->lrsel = value;
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun return 1;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun static const struct snd_soc_dai_ops tegra210_dmic_dai_ops = {
316*4882a593Smuzhiyun .hw_params = tegra210_dmic_hw_params,
317*4882a593Smuzhiyun };
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun static struct snd_soc_dai_driver tegra210_dmic_dais[] = {
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun .name = "DMIC-CIF",
322*4882a593Smuzhiyun .capture = {
323*4882a593Smuzhiyun .stream_name = "CIF-Capture",
324*4882a593Smuzhiyun .channels_min = 1,
325*4882a593Smuzhiyun .channels_max = 2,
326*4882a593Smuzhiyun .rates = SNDRV_PCM_RATE_8000_48000,
327*4882a593Smuzhiyun .formats = SNDRV_PCM_FMTBIT_S16_LE |
328*4882a593Smuzhiyun SNDRV_PCM_FMTBIT_S32_LE,
329*4882a593Smuzhiyun },
330*4882a593Smuzhiyun },
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun .name = "DMIC-DAP",
333*4882a593Smuzhiyun .capture = {
334*4882a593Smuzhiyun .stream_name = "DAP-Capture",
335*4882a593Smuzhiyun .channels_min = 1,
336*4882a593Smuzhiyun .channels_max = 2,
337*4882a593Smuzhiyun .rates = SNDRV_PCM_RATE_8000_48000,
338*4882a593Smuzhiyun .formats = SNDRV_PCM_FMTBIT_S16_LE |
339*4882a593Smuzhiyun SNDRV_PCM_FMTBIT_S32_LE,
340*4882a593Smuzhiyun },
341*4882a593Smuzhiyun .ops = &tegra210_dmic_dai_ops,
342*4882a593Smuzhiyun .symmetric_rates = 1,
343*4882a593Smuzhiyun },
344*4882a593Smuzhiyun };
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun static const struct snd_soc_dapm_widget tegra210_dmic_widgets[] = {
347*4882a593Smuzhiyun SND_SOC_DAPM_AIF_OUT("TX", NULL, 0, TEGRA210_DMIC_ENABLE, 0, 0),
348*4882a593Smuzhiyun SND_SOC_DAPM_MIC("MIC", NULL),
349*4882a593Smuzhiyun };
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun static const struct snd_soc_dapm_route tegra210_dmic_routes[] = {
352*4882a593Smuzhiyun { "XBAR-RX", NULL, "XBAR-Capture" },
353*4882a593Smuzhiyun { "XBAR-Capture", NULL, "CIF-Capture" },
354*4882a593Smuzhiyun { "CIF-Capture", NULL, "TX" },
355*4882a593Smuzhiyun { "TX", NULL, "DAP-Capture" },
356*4882a593Smuzhiyun { "DAP-Capture", NULL, "MIC" },
357*4882a593Smuzhiyun };
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun static const char * const tegra210_dmic_ch_select[] = {
360*4882a593Smuzhiyun "Left", "Right", "Stereo",
361*4882a593Smuzhiyun };
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun static const struct soc_enum tegra210_dmic_ch_enum =
364*4882a593Smuzhiyun SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_dmic_ch_select),
365*4882a593Smuzhiyun tegra210_dmic_ch_select);
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun static const char * const tegra210_dmic_mono_conv_text[] = {
368*4882a593Smuzhiyun "Zero", "Copy",
369*4882a593Smuzhiyun };
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun static const char * const tegra210_dmic_stereo_conv_text[] = {
372*4882a593Smuzhiyun "CH0", "CH1", "AVG",
373*4882a593Smuzhiyun };
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun static const struct soc_enum tegra210_dmic_mono_conv_enum =
376*4882a593Smuzhiyun SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_dmic_mono_conv_text),
377*4882a593Smuzhiyun tegra210_dmic_mono_conv_text);
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun static const struct soc_enum tegra210_dmic_stereo_conv_enum =
380*4882a593Smuzhiyun SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_dmic_stereo_conv_text),
381*4882a593Smuzhiyun tegra210_dmic_stereo_conv_text);
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun static const char * const tegra210_dmic_osr_text[] = {
384*4882a593Smuzhiyun "OSR_64", "OSR_128", "OSR_256",
385*4882a593Smuzhiyun };
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun static const struct soc_enum tegra210_dmic_osr_enum =
388*4882a593Smuzhiyun SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_dmic_osr_text),
389*4882a593Smuzhiyun tegra210_dmic_osr_text);
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun static const char * const tegra210_dmic_lrsel_text[] = {
392*4882a593Smuzhiyun "Left", "Right",
393*4882a593Smuzhiyun };
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun static const struct soc_enum tegra210_dmic_lrsel_enum =
396*4882a593Smuzhiyun SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_dmic_lrsel_text),
397*4882a593Smuzhiyun tegra210_dmic_lrsel_text);
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun static const struct snd_kcontrol_new tegra210_dmic_controls[] = {
400*4882a593Smuzhiyun SOC_SINGLE_EXT("Boost Gain Volume", 0, 0, MAX_BOOST_GAIN, 0,
401*4882a593Smuzhiyun tegra210_dmic_get_boost_gain,
402*4882a593Smuzhiyun tegra210_dmic_put_boost_gain),
403*4882a593Smuzhiyun SOC_ENUM_EXT("Channel Select", tegra210_dmic_ch_enum,
404*4882a593Smuzhiyun tegra210_dmic_get_ch_select, tegra210_dmic_put_ch_select),
405*4882a593Smuzhiyun SOC_ENUM_EXT("Mono To Stereo",
406*4882a593Smuzhiyun tegra210_dmic_mono_conv_enum,
407*4882a593Smuzhiyun tegra210_dmic_get_mono_to_stereo,
408*4882a593Smuzhiyun tegra210_dmic_put_mono_to_stereo),
409*4882a593Smuzhiyun SOC_ENUM_EXT("Stereo To Mono",
410*4882a593Smuzhiyun tegra210_dmic_stereo_conv_enum,
411*4882a593Smuzhiyun tegra210_dmic_get_stereo_to_mono,
412*4882a593Smuzhiyun tegra210_dmic_put_stereo_to_mono),
413*4882a593Smuzhiyun SOC_ENUM_EXT("OSR Value", tegra210_dmic_osr_enum,
414*4882a593Smuzhiyun tegra210_dmic_get_osr_val, tegra210_dmic_put_osr_val),
415*4882a593Smuzhiyun SOC_ENUM_EXT("LR Polarity Select", tegra210_dmic_lrsel_enum,
416*4882a593Smuzhiyun tegra210_dmic_get_pol_sel, tegra210_dmic_put_pol_sel),
417*4882a593Smuzhiyun };
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun static const struct snd_soc_component_driver tegra210_dmic_compnt = {
420*4882a593Smuzhiyun .dapm_widgets = tegra210_dmic_widgets,
421*4882a593Smuzhiyun .num_dapm_widgets = ARRAY_SIZE(tegra210_dmic_widgets),
422*4882a593Smuzhiyun .dapm_routes = tegra210_dmic_routes,
423*4882a593Smuzhiyun .num_dapm_routes = ARRAY_SIZE(tegra210_dmic_routes),
424*4882a593Smuzhiyun .controls = tegra210_dmic_controls,
425*4882a593Smuzhiyun .num_controls = ARRAY_SIZE(tegra210_dmic_controls),
426*4882a593Smuzhiyun };
427*4882a593Smuzhiyun
tegra210_dmic_wr_reg(struct device * dev,unsigned int reg)428*4882a593Smuzhiyun static bool tegra210_dmic_wr_reg(struct device *dev, unsigned int reg)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun switch (reg) {
431*4882a593Smuzhiyun case TEGRA210_DMIC_TX_INT_MASK ... TEGRA210_DMIC_TX_CIF_CTRL:
432*4882a593Smuzhiyun case TEGRA210_DMIC_ENABLE ... TEGRA210_DMIC_CG:
433*4882a593Smuzhiyun case TEGRA210_DMIC_CTRL:
434*4882a593Smuzhiyun case TEGRA210_DMIC_DBG_CTRL:
435*4882a593Smuzhiyun case TEGRA210_DMIC_DCR_BIQUAD_0_COEF_4 ... TEGRA210_DMIC_LP_BIQUAD_1_COEF_4:
436*4882a593Smuzhiyun return true;
437*4882a593Smuzhiyun default:
438*4882a593Smuzhiyun return false;
439*4882a593Smuzhiyun };
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun
tegra210_dmic_rd_reg(struct device * dev,unsigned int reg)442*4882a593Smuzhiyun static bool tegra210_dmic_rd_reg(struct device *dev, unsigned int reg)
443*4882a593Smuzhiyun {
444*4882a593Smuzhiyun if (tegra210_dmic_wr_reg(dev, reg))
445*4882a593Smuzhiyun return true;
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun switch (reg) {
448*4882a593Smuzhiyun case TEGRA210_DMIC_TX_STATUS:
449*4882a593Smuzhiyun case TEGRA210_DMIC_TX_INT_STATUS:
450*4882a593Smuzhiyun case TEGRA210_DMIC_STATUS:
451*4882a593Smuzhiyun case TEGRA210_DMIC_INT_STATUS:
452*4882a593Smuzhiyun return true;
453*4882a593Smuzhiyun default:
454*4882a593Smuzhiyun return false;
455*4882a593Smuzhiyun };
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun
tegra210_dmic_volatile_reg(struct device * dev,unsigned int reg)458*4882a593Smuzhiyun static bool tegra210_dmic_volatile_reg(struct device *dev, unsigned int reg)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun switch (reg) {
461*4882a593Smuzhiyun case TEGRA210_DMIC_TX_STATUS:
462*4882a593Smuzhiyun case TEGRA210_DMIC_TX_INT_STATUS:
463*4882a593Smuzhiyun case TEGRA210_DMIC_TX_INT_SET:
464*4882a593Smuzhiyun case TEGRA210_DMIC_SOFT_RESET:
465*4882a593Smuzhiyun case TEGRA210_DMIC_STATUS:
466*4882a593Smuzhiyun case TEGRA210_DMIC_INT_STATUS:
467*4882a593Smuzhiyun return true;
468*4882a593Smuzhiyun default:
469*4882a593Smuzhiyun return false;
470*4882a593Smuzhiyun };
471*4882a593Smuzhiyun }
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun static const struct regmap_config tegra210_dmic_regmap_config = {
474*4882a593Smuzhiyun .reg_bits = 32,
475*4882a593Smuzhiyun .reg_stride = 4,
476*4882a593Smuzhiyun .val_bits = 32,
477*4882a593Smuzhiyun .max_register = TEGRA210_DMIC_LP_BIQUAD_1_COEF_4,
478*4882a593Smuzhiyun .writeable_reg = tegra210_dmic_wr_reg,
479*4882a593Smuzhiyun .readable_reg = tegra210_dmic_rd_reg,
480*4882a593Smuzhiyun .volatile_reg = tegra210_dmic_volatile_reg,
481*4882a593Smuzhiyun .reg_defaults = tegra210_dmic_reg_defaults,
482*4882a593Smuzhiyun .num_reg_defaults = ARRAY_SIZE(tegra210_dmic_reg_defaults),
483*4882a593Smuzhiyun .cache_type = REGCACHE_FLAT,
484*4882a593Smuzhiyun };
485*4882a593Smuzhiyun
tegra210_dmic_probe(struct platform_device * pdev)486*4882a593Smuzhiyun static int tegra210_dmic_probe(struct platform_device *pdev)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun struct device *dev = &pdev->dev;
489*4882a593Smuzhiyun struct tegra210_dmic *dmic;
490*4882a593Smuzhiyun void __iomem *regs;
491*4882a593Smuzhiyun int err;
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun dmic = devm_kzalloc(dev, sizeof(*dmic), GFP_KERNEL);
494*4882a593Smuzhiyun if (!dmic)
495*4882a593Smuzhiyun return -ENOMEM;
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun dmic->osr_val = DMIC_OSR_64;
498*4882a593Smuzhiyun dmic->ch_select = DMIC_CH_SELECT_STEREO;
499*4882a593Smuzhiyun dmic->lrsel = DMIC_LRSEL_LEFT;
500*4882a593Smuzhiyun dmic->boost_gain = 0;
501*4882a593Smuzhiyun dmic->stereo_to_mono = 0; /* "CH0" */
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun dev_set_drvdata(dev, dmic);
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun dmic->clk_dmic = devm_clk_get(dev, "dmic");
506*4882a593Smuzhiyun if (IS_ERR(dmic->clk_dmic)) {
507*4882a593Smuzhiyun dev_err(dev, "can't retrieve DMIC clock\n");
508*4882a593Smuzhiyun return PTR_ERR(dmic->clk_dmic);
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun regs = devm_platform_ioremap_resource(pdev, 0);
512*4882a593Smuzhiyun if (IS_ERR(regs))
513*4882a593Smuzhiyun return PTR_ERR(regs);
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun dmic->regmap = devm_regmap_init_mmio(dev, regs,
516*4882a593Smuzhiyun &tegra210_dmic_regmap_config);
517*4882a593Smuzhiyun if (IS_ERR(dmic->regmap)) {
518*4882a593Smuzhiyun dev_err(dev, "regmap init failed\n");
519*4882a593Smuzhiyun return PTR_ERR(dmic->regmap);
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun regcache_cache_only(dmic->regmap, true);
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun err = devm_snd_soc_register_component(dev, &tegra210_dmic_compnt,
525*4882a593Smuzhiyun tegra210_dmic_dais,
526*4882a593Smuzhiyun ARRAY_SIZE(tegra210_dmic_dais));
527*4882a593Smuzhiyun if (err) {
528*4882a593Smuzhiyun dev_err(dev, "can't register DMIC component, err: %d\n", err);
529*4882a593Smuzhiyun return err;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun pm_runtime_enable(dev);
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun return 0;
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun
tegra210_dmic_remove(struct platform_device * pdev)537*4882a593Smuzhiyun static int tegra210_dmic_remove(struct platform_device *pdev)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun pm_runtime_disable(&pdev->dev);
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun return 0;
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun static const struct dev_pm_ops tegra210_dmic_pm_ops = {
545*4882a593Smuzhiyun SET_RUNTIME_PM_OPS(tegra210_dmic_runtime_suspend,
546*4882a593Smuzhiyun tegra210_dmic_runtime_resume, NULL)
547*4882a593Smuzhiyun SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
548*4882a593Smuzhiyun pm_runtime_force_resume)
549*4882a593Smuzhiyun };
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun static const struct of_device_id tegra210_dmic_of_match[] = {
552*4882a593Smuzhiyun { .compatible = "nvidia,tegra210-dmic" },
553*4882a593Smuzhiyun {},
554*4882a593Smuzhiyun };
555*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, tegra210_dmic_of_match);
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun static struct platform_driver tegra210_dmic_driver = {
558*4882a593Smuzhiyun .driver = {
559*4882a593Smuzhiyun .name = "tegra210-dmic",
560*4882a593Smuzhiyun .of_match_table = tegra210_dmic_of_match,
561*4882a593Smuzhiyun .pm = &tegra210_dmic_pm_ops,
562*4882a593Smuzhiyun },
563*4882a593Smuzhiyun .probe = tegra210_dmic_probe,
564*4882a593Smuzhiyun .remove = tegra210_dmic_remove,
565*4882a593Smuzhiyun };
566*4882a593Smuzhiyun module_platform_driver(tegra210_dmic_driver)
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun MODULE_AUTHOR("Rahul Mittal <rmittal@nvidia.com>");
569*4882a593Smuzhiyun MODULE_DESCRIPTION("Tegra210 ASoC DMIC driver");
570*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
571