1 // SPDX-License-Identifier: GPL-2.0-only
3 // tegra186_dspk.c - Tegra186 DSPK driver
5 // Copyright (c) 2020 NVIDIA CORPORATION. All rights reserved.
8 #include <linux/device.h>
9 #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 "tegra186_dspk.h"
19 #include "tegra_cif.h"
21 static const struct reg_default tegra186_dspk_reg_defaults[] = {
22 { TEGRA186_DSPK_RX_INT_MASK, 0x00000007 },
23 { TEGRA186_DSPK_RX_CIF_CTRL, 0x00007700 },
24 { TEGRA186_DSPK_CG, 0x00000001 },
25 { TEGRA186_DSPK_CORE_CTRL, 0x00000310 },
26 { TEGRA186_DSPK_CODEC_CTRL, 0x03000000 },
29 static int tegra186_dspk_get_control(struct snd_kcontrol *kcontrol,
30 struct snd_ctl_elem_value *ucontrol)
32 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
33 struct tegra186_dspk *dspk = snd_soc_component_get_drvdata(codec);
35 if (strstr(kcontrol->id.name, "FIFO Threshold"))
36 ucontrol->value.integer.value[0] = dspk->rx_fifo_th;
37 else if (strstr(kcontrol->id.name, "OSR Value"))
38 ucontrol->value.integer.value[0] = dspk->osr_val;
39 else if (strstr(kcontrol->id.name, "LR Polarity Select"))
40 ucontrol->value.integer.value[0] = dspk->lrsel;
41 else if (strstr(kcontrol->id.name, "Channel Select"))
42 ucontrol->value.integer.value[0] = dspk->ch_sel;
43 else if (strstr(kcontrol->id.name, "Mono To Stereo"))
44 ucontrol->value.integer.value[0] = dspk->mono_to_stereo;
45 else if (strstr(kcontrol->id.name, "Stereo To Mono"))
46 ucontrol->value.integer.value[0] = dspk->stereo_to_mono;
51 static int tegra186_dspk_put_control(struct snd_kcontrol *kcontrol,
52 struct snd_ctl_elem_value *ucontrol)
54 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
55 struct tegra186_dspk *dspk = snd_soc_component_get_drvdata(codec);
56 int val = ucontrol->value.integer.value[0];
58 if (strstr(kcontrol->id.name, "FIFO Threshold"))
59 dspk->rx_fifo_th = val;
60 else if (strstr(kcontrol->id.name, "OSR Value"))
62 else if (strstr(kcontrol->id.name, "LR Polarity Select"))
64 else if (strstr(kcontrol->id.name, "Channel Select"))
66 else if (strstr(kcontrol->id.name, "Mono To Stereo"))
67 dspk->mono_to_stereo = val;
68 else if (strstr(kcontrol->id.name, "Stereo To Mono"))
69 dspk->stereo_to_mono = val;
74 static int __maybe_unused tegra186_dspk_runtime_suspend(struct device *dev)
76 struct tegra186_dspk *dspk = dev_get_drvdata(dev);
78 regcache_cache_only(dspk->regmap, true);
79 regcache_mark_dirty(dspk->regmap);
81 clk_disable_unprepare(dspk->clk_dspk);
86 static int __maybe_unused tegra186_dspk_runtime_resume(struct device *dev)
88 struct tegra186_dspk *dspk = dev_get_drvdata(dev);
91 err = clk_prepare_enable(dspk->clk_dspk);
93 dev_err(dev, "failed to enable DSPK clock, err: %d\n", err);
97 regcache_cache_only(dspk->regmap, false);
98 regcache_sync(dspk->regmap);
103 static int tegra186_dspk_hw_params(struct snd_pcm_substream *substream,
104 struct snd_pcm_hw_params *params,
105 struct snd_soc_dai *dai)
107 struct tegra186_dspk *dspk = snd_soc_dai_get_drvdata(dai);
108 unsigned int channels, srate, dspk_clk;
109 struct device *dev = dai->dev;
110 struct tegra_cif_conf cif_conf;
114 memset(&cif_conf, 0, sizeof(struct tegra_cif_conf));
116 channels = params_channels(params);
117 cif_conf.audio_ch = channels;
120 switch (dspk->ch_sel) {
121 case DSPK_CH_SELECT_LEFT:
122 case DSPK_CH_SELECT_RIGHT:
123 cif_conf.client_ch = 1;
125 case DSPK_CH_SELECT_STEREO:
126 cif_conf.client_ch = 2;
129 dev_err(dev, "Invalid DSPK client channels\n");
133 cif_conf.client_bits = TEGRA_ACIF_BITS_24;
135 switch (params_format(params)) {
136 case SNDRV_PCM_FORMAT_S16_LE:
137 cif_conf.audio_bits = TEGRA_ACIF_BITS_16;
139 case SNDRV_PCM_FORMAT_S32_LE:
140 cif_conf.audio_bits = TEGRA_ACIF_BITS_32;
143 dev_err(dev, "unsupported format!\n");
147 srate = params_rate(params);
149 /* RX FIFO threshold in terms of frames */
150 max_th = (TEGRA186_DSPK_RX_FIFO_DEPTH / cif_conf.audio_ch) - 1;
152 if (dspk->rx_fifo_th > max_th)
153 dspk->rx_fifo_th = max_th;
155 cif_conf.threshold = dspk->rx_fifo_th;
156 cif_conf.mono_conv = dspk->mono_to_stereo;
157 cif_conf.stereo_conv = dspk->stereo_to_mono;
159 tegra_set_cif(dspk->regmap, TEGRA186_DSPK_RX_CIF_CTRL,
163 * DSPK clock and PDM codec clock should be synchronous with 4:1 ratio,
164 * this is because it takes 4 clock cycles to send out one sample to
165 * codec by sigma delta modulator. Finally the clock rate is a multiple
166 * of 'Over Sampling Ratio', 'Sample Rate' and 'Interface Clock Ratio'.
168 dspk_clk = (DSPK_OSR_FACTOR << dspk->osr_val) * srate * DSPK_CLK_RATIO;
170 err = clk_set_rate(dspk->clk_dspk, dspk_clk);
172 dev_err(dev, "can't set DSPK clock rate %u, err: %d\n",
178 regmap_update_bits(dspk->regmap,
180 TEGRA186_DSPK_CORE_CTRL,
182 TEGRA186_DSPK_OSR_MASK |
183 TEGRA186_DSPK_CHANNEL_SELECT_MASK |
184 TEGRA186_DSPK_CTRL_LRSEL_POLARITY_MASK,
186 (dspk->osr_val << DSPK_OSR_SHIFT) |
187 ((dspk->ch_sel + 1) << CH_SEL_SHIFT) |
188 (dspk->lrsel << LRSEL_POL_SHIFT));
193 static const struct snd_soc_dai_ops tegra186_dspk_dai_ops = {
194 .hw_params = tegra186_dspk_hw_params,
197 static struct snd_soc_dai_driver tegra186_dspk_dais[] = {
201 .stream_name = "CIF-Playback",
204 .rates = SNDRV_PCM_RATE_8000_48000,
205 .formats = SNDRV_PCM_FMTBIT_S16_LE |
206 SNDRV_PCM_FMTBIT_S32_LE,
212 .stream_name = "DAP-Playback",
215 .rates = SNDRV_PCM_RATE_8000_48000,
216 .formats = SNDRV_PCM_FMTBIT_S16_LE |
217 SNDRV_PCM_FMTBIT_S32_LE,
219 .ops = &tegra186_dspk_dai_ops,
220 .symmetric_rates = 1,
224 static const struct snd_soc_dapm_widget tegra186_dspk_widgets[] = {
225 SND_SOC_DAPM_AIF_IN("RX", NULL, 0, TEGRA186_DSPK_ENABLE, 0, 0),
226 SND_SOC_DAPM_SPK("SPK", NULL),
229 static const struct snd_soc_dapm_route tegra186_dspk_routes[] = {
230 { "XBAR-Playback", NULL, "XBAR-TX" },
231 { "CIF-Playback", NULL, "XBAR-Playback" },
232 { "RX", NULL, "CIF-Playback" },
233 { "DAP-Playback", NULL, "RX" },
234 { "SPK", NULL, "DAP-Playback" },
237 static const char * const tegra186_dspk_ch_sel_text[] = {
238 "Left", "Right", "Stereo",
241 static const struct soc_enum tegra186_dspk_ch_sel_enum =
242 SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(tegra186_dspk_ch_sel_text),
243 tegra186_dspk_ch_sel_text);
245 static const char * const tegra186_dspk_osr_text[] = {
246 "OSR_32", "OSR_64", "OSR_128", "OSR_256",
249 static const struct soc_enum tegra186_dspk_osr_enum =
250 SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(tegra186_dspk_osr_text),
251 tegra186_dspk_osr_text);
253 static const char * const tegra186_dspk_lrsel_text[] = {
257 static const char * const tegra186_dspk_mono_conv_text[] = {
261 static const struct soc_enum tegra186_dspk_mono_conv_enum =
262 SOC_ENUM_SINGLE(SND_SOC_NOPM, 0,
263 ARRAY_SIZE(tegra186_dspk_mono_conv_text),
264 tegra186_dspk_mono_conv_text);
266 static const char * const tegra186_dspk_stereo_conv_text[] = {
270 static const struct soc_enum tegra186_dspk_stereo_conv_enum =
271 SOC_ENUM_SINGLE(SND_SOC_NOPM, 0,
272 ARRAY_SIZE(tegra186_dspk_stereo_conv_text),
273 tegra186_dspk_stereo_conv_text);
275 static const struct soc_enum tegra186_dspk_lrsel_enum =
276 SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(tegra186_dspk_lrsel_text),
277 tegra186_dspk_lrsel_text);
279 static const struct snd_kcontrol_new tegrat186_dspk_controls[] = {
280 SOC_SINGLE_EXT("FIFO Threshold", SND_SOC_NOPM, 0,
281 TEGRA186_DSPK_RX_FIFO_DEPTH - 1, 0,
282 tegra186_dspk_get_control, tegra186_dspk_put_control),
283 SOC_ENUM_EXT("OSR Value", tegra186_dspk_osr_enum,
284 tegra186_dspk_get_control, tegra186_dspk_put_control),
285 SOC_ENUM_EXT("LR Polarity Select", tegra186_dspk_lrsel_enum,
286 tegra186_dspk_get_control, tegra186_dspk_put_control),
287 SOC_ENUM_EXT("Channel Select", tegra186_dspk_ch_sel_enum,
288 tegra186_dspk_get_control, tegra186_dspk_put_control),
289 SOC_ENUM_EXT("Mono To Stereo", tegra186_dspk_mono_conv_enum,
290 tegra186_dspk_get_control, tegra186_dspk_put_control),
291 SOC_ENUM_EXT("Stereo To Mono", tegra186_dspk_stereo_conv_enum,
292 tegra186_dspk_get_control, tegra186_dspk_put_control),
295 static const struct snd_soc_component_driver tegra186_dspk_cmpnt = {
296 .dapm_widgets = tegra186_dspk_widgets,
297 .num_dapm_widgets = ARRAY_SIZE(tegra186_dspk_widgets),
298 .dapm_routes = tegra186_dspk_routes,
299 .num_dapm_routes = ARRAY_SIZE(tegra186_dspk_routes),
300 .controls = tegrat186_dspk_controls,
301 .num_controls = ARRAY_SIZE(tegrat186_dspk_controls),
304 static bool tegra186_dspk_wr_reg(struct device *dev, unsigned int reg)
307 case TEGRA186_DSPK_RX_INT_MASK ... TEGRA186_DSPK_RX_CIF_CTRL:
308 case TEGRA186_DSPK_ENABLE ... TEGRA186_DSPK_CG:
309 case TEGRA186_DSPK_CORE_CTRL ... TEGRA186_DSPK_CODEC_CTRL:
316 static bool tegra186_dspk_rd_reg(struct device *dev, unsigned int reg)
318 if (tegra186_dspk_wr_reg(dev, reg))
322 case TEGRA186_DSPK_RX_STATUS:
323 case TEGRA186_DSPK_RX_INT_STATUS:
324 case TEGRA186_DSPK_STATUS:
325 case TEGRA186_DSPK_INT_STATUS:
332 static bool tegra186_dspk_volatile_reg(struct device *dev, unsigned int reg)
335 case TEGRA186_DSPK_RX_STATUS:
336 case TEGRA186_DSPK_RX_INT_STATUS:
337 case TEGRA186_DSPK_STATUS:
338 case TEGRA186_DSPK_INT_STATUS:
345 static const struct regmap_config tegra186_dspk_regmap = {
349 .max_register = TEGRA186_DSPK_CODEC_CTRL,
350 .writeable_reg = tegra186_dspk_wr_reg,
351 .readable_reg = tegra186_dspk_rd_reg,
352 .volatile_reg = tegra186_dspk_volatile_reg,
353 .reg_defaults = tegra186_dspk_reg_defaults,
354 .num_reg_defaults = ARRAY_SIZE(tegra186_dspk_reg_defaults),
355 .cache_type = REGCACHE_FLAT,
358 static const struct of_device_id tegra186_dspk_of_match[] = {
359 { .compatible = "nvidia,tegra186-dspk" },
362 MODULE_DEVICE_TABLE(of, tegra186_dspk_of_match);
364 static int tegra186_dspk_platform_probe(struct platform_device *pdev)
366 struct device *dev = &pdev->dev;
367 struct tegra186_dspk *dspk;
371 dspk = devm_kzalloc(dev, sizeof(*dspk), GFP_KERNEL);
375 dspk->osr_val = DSPK_OSR_64;
376 dspk->lrsel = DSPK_LRSEL_LEFT;
377 dspk->ch_sel = DSPK_CH_SELECT_STEREO;
378 dspk->mono_to_stereo = 0; /* "Zero" */
380 dev_set_drvdata(dev, dspk);
382 dspk->clk_dspk = devm_clk_get(dev, "dspk");
383 if (IS_ERR(dspk->clk_dspk)) {
384 dev_err(dev, "can't retrieve DSPK clock\n");
385 return PTR_ERR(dspk->clk_dspk);
388 regs = devm_platform_ioremap_resource(pdev, 0);
390 return PTR_ERR(regs);
392 dspk->regmap = devm_regmap_init_mmio(dev, regs, &tegra186_dspk_regmap);
393 if (IS_ERR(dspk->regmap)) {
394 dev_err(dev, "regmap init failed\n");
395 return PTR_ERR(dspk->regmap);
398 regcache_cache_only(dspk->regmap, true);
400 err = devm_snd_soc_register_component(dev, &tegra186_dspk_cmpnt,
402 ARRAY_SIZE(tegra186_dspk_dais));
404 dev_err(dev, "can't register DSPK component, err: %d\n",
409 pm_runtime_enable(dev);
414 static int tegra186_dspk_platform_remove(struct platform_device *pdev)
416 pm_runtime_disable(&pdev->dev);
421 static const struct dev_pm_ops tegra186_dspk_pm_ops = {
422 SET_RUNTIME_PM_OPS(tegra186_dspk_runtime_suspend,
423 tegra186_dspk_runtime_resume, NULL)
424 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
425 pm_runtime_force_resume)
428 static struct platform_driver tegra186_dspk_driver = {
430 .name = "tegra186-dspk",
431 .of_match_table = tegra186_dspk_of_match,
432 .pm = &tegra186_dspk_pm_ops,
434 .probe = tegra186_dspk_platform_probe,
435 .remove = tegra186_dspk_platform_remove,
437 module_platform_driver(tegra186_dspk_driver);
439 MODULE_AUTHOR("Mohan Kumar <mkumard@nvidia.com>");
440 MODULE_AUTHOR("Sameer Pujar <spujar@nvidia.com>");
441 MODULE_DESCRIPTION("Tegra186 ASoC DSPK driver");
442 MODULE_LICENSE("GPL v2");