1 // SPDX-License-Identifier: GPL-2.0-only
3 * tegra20_spdif.c - Tegra20 SPDIF driver
5 * Author: Stephen Warren <swarren@nvidia.com>
6 * Copyright (C) 2011-2012 - NVIDIA, Inc.
10 #include <linux/delay.h>
11 #include <linux/device.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regmap.h>
18 #include <linux/reset.h>
19 #include <linux/slab.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
24 #include <sound/dmaengine_pcm.h>
26 #include "tegra20_spdif.h"
28 static __maybe_unused
int tegra20_spdif_runtime_suspend(struct device
*dev
)
30 struct tegra20_spdif
*spdif
= dev_get_drvdata(dev
);
32 regcache_cache_only(spdif
->regmap
, true);
34 clk_disable_unprepare(spdif
->clk_spdif_out
);
39 static __maybe_unused
int tegra20_spdif_runtime_resume(struct device
*dev
)
41 struct tegra20_spdif
*spdif
= dev_get_drvdata(dev
);
44 ret
= reset_control_assert(spdif
->reset
);
48 ret
= clk_prepare_enable(spdif
->clk_spdif_out
);
50 dev_err(dev
, "clk_enable failed: %d\n", ret
);
54 usleep_range(10, 100);
56 ret
= reset_control_deassert(spdif
->reset
);
60 regcache_cache_only(spdif
->regmap
, false);
61 regcache_mark_dirty(spdif
->regmap
);
63 ret
= regcache_sync(spdif
->regmap
);
70 clk_disable_unprepare(spdif
->clk_spdif_out
);
75 static int tegra20_spdif_hw_params(struct snd_pcm_substream
*substream
,
76 struct snd_pcm_hw_params
*params
,
77 struct snd_soc_dai
*dai
)
79 struct tegra20_spdif
*spdif
= dev_get_drvdata(dai
->dev
);
80 unsigned int mask
= 0, val
= 0;
84 mask
|= TEGRA20_SPDIF_CTRL_PACK
|
85 TEGRA20_SPDIF_CTRL_BIT_MODE_MASK
;
86 switch (params_format(params
)) {
87 case SNDRV_PCM_FORMAT_S16_LE
:
88 val
|= TEGRA20_SPDIF_CTRL_PACK
|
89 TEGRA20_SPDIF_CTRL_BIT_MODE_16BIT
;
95 regmap_update_bits(spdif
->regmap
, TEGRA20_SPDIF_CTRL
, mask
, val
);
98 * FIFO trigger level must be bigger than DMA burst or equal to it,
99 * otherwise data is discarded on overflow.
101 regmap_update_bits(spdif
->regmap
, TEGRA20_SPDIF_DATA_FIFO_CSR
,
102 TEGRA20_SPDIF_DATA_FIFO_CSR_TX_ATN_LVL_MASK
,
103 TEGRA20_SPDIF_DATA_FIFO_CSR_TX_ATN_LVL_TU4_WORD_FULL
);
105 switch (params_rate(params
)) {
107 spdifclock
= 4096000;
110 spdifclock
= 5644800;
113 spdifclock
= 6144000;
116 spdifclock
= 11289600;
119 spdifclock
= 12288000;
122 spdifclock
= 22579200;
125 spdifclock
= 24576000;
131 ret
= clk_set_rate(spdif
->clk_spdif_out
, spdifclock
);
133 dev_err(dai
->dev
, "Can't set SPDIF clock rate: %d\n", ret
);
137 rate
= clk_get_rate(spdif
->clk_spdif_out
);
138 if (rate
!= spdifclock
)
139 dev_warn_once(dai
->dev
,
140 "SPDIF clock rate %d doesn't match requested rate %lu\n",
146 static void tegra20_spdif_start_playback(struct tegra20_spdif
*spdif
)
148 regmap_update_bits(spdif
->regmap
, TEGRA20_SPDIF_CTRL
,
149 TEGRA20_SPDIF_CTRL_TX_EN
,
150 TEGRA20_SPDIF_CTRL_TX_EN
);
153 static void tegra20_spdif_stop_playback(struct tegra20_spdif
*spdif
)
155 regmap_update_bits(spdif
->regmap
, TEGRA20_SPDIF_CTRL
,
156 TEGRA20_SPDIF_CTRL_TX_EN
, 0);
159 static int tegra20_spdif_trigger(struct snd_pcm_substream
*substream
, int cmd
,
160 struct snd_soc_dai
*dai
)
162 struct tegra20_spdif
*spdif
= dev_get_drvdata(dai
->dev
);
165 case SNDRV_PCM_TRIGGER_START
:
166 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
167 case SNDRV_PCM_TRIGGER_RESUME
:
168 tegra20_spdif_start_playback(spdif
);
170 case SNDRV_PCM_TRIGGER_STOP
:
171 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
172 case SNDRV_PCM_TRIGGER_SUSPEND
:
173 tegra20_spdif_stop_playback(spdif
);
182 static int tegra20_spdif_filter_rates(struct snd_pcm_hw_params
*params
,
183 struct snd_pcm_hw_rule
*rule
)
185 struct snd_interval
*r
= hw_param_interval(params
, rule
->var
);
186 struct snd_soc_dai
*dai
= rule
->private;
187 struct tegra20_spdif
*spdif
= dev_get_drvdata(dai
->dev
);
188 struct clk
*parent
= clk_get_parent(spdif
->clk_spdif_out
);
189 static const unsigned int rates
[] = { 32000, 44100, 48000 };
190 unsigned long i
, parent_rate
, valid_rates
= 0;
192 parent_rate
= clk_get_rate(parent
);
194 dev_err(dai
->dev
, "Can't get parent clock rate\n");
198 for (i
= 0; i
< ARRAY_SIZE(rates
); i
++) {
199 if (parent_rate
% (rates
[i
] * 128) == 0)
200 valid_rates
|= BIT(i
);
204 * At least one rate must be valid, otherwise the parent clock isn't
205 * audio PLL. Nothing should be filtered in this case.
208 valid_rates
= BIT(ARRAY_SIZE(rates
)) - 1;
210 return snd_interval_list(r
, ARRAY_SIZE(rates
), rates
, valid_rates
);
213 static int tegra20_spdif_startup(struct snd_pcm_substream
*substream
,
214 struct snd_soc_dai
*dai
)
216 if (!device_property_read_bool(dai
->dev
, "nvidia,fixed-parent-rate"))
220 * SPDIF and I2S share audio PLL. HDMI takes audio packets from SPDIF
221 * and audio may not work on some TVs if clock rate isn't precise.
223 * PLL rate is controlled by I2S side. Filter out audio rates that
224 * don't match PLL rate at the start of stream to allow both SPDIF
225 * and I2S work simultaneously, assuming that PLL rate won't be
228 return snd_pcm_hw_rule_add(substream
->runtime
, 0,
229 SNDRV_PCM_HW_PARAM_RATE
,
230 tegra20_spdif_filter_rates
, dai
,
231 SNDRV_PCM_HW_PARAM_RATE
, -1);
234 static int tegra20_spdif_probe(struct snd_soc_dai
*dai
)
236 struct tegra20_spdif
*spdif
= dev_get_drvdata(dai
->dev
);
238 snd_soc_dai_init_dma_data(dai
, &spdif
->playback_dma_data
, NULL
);
243 static const struct snd_soc_dai_ops tegra20_spdif_dai_ops
= {
244 .probe
= tegra20_spdif_probe
,
245 .hw_params
= tegra20_spdif_hw_params
,
246 .trigger
= tegra20_spdif_trigger
,
247 .startup
= tegra20_spdif_startup
,
250 static struct snd_soc_dai_driver tegra20_spdif_dai
= {
251 .name
= "tegra20-spdif",
253 .stream_name
= "Playback",
256 .rates
= SNDRV_PCM_RATE_32000
| SNDRV_PCM_RATE_44100
|
257 SNDRV_PCM_RATE_48000
,
258 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
260 .ops
= &tegra20_spdif_dai_ops
,
263 static const struct snd_soc_component_driver tegra20_spdif_component
= {
264 .name
= "tegra20-spdif",
265 .legacy_dai_naming
= 1,
268 static bool tegra20_spdif_wr_rd_reg(struct device
*dev
, unsigned int reg
)
271 case TEGRA20_SPDIF_CTRL
:
272 case TEGRA20_SPDIF_STATUS
:
273 case TEGRA20_SPDIF_STROBE_CTRL
:
274 case TEGRA20_SPDIF_DATA_FIFO_CSR
:
275 case TEGRA20_SPDIF_DATA_OUT
:
276 case TEGRA20_SPDIF_DATA_IN
:
277 case TEGRA20_SPDIF_CH_STA_RX_A
:
278 case TEGRA20_SPDIF_CH_STA_RX_B
:
279 case TEGRA20_SPDIF_CH_STA_RX_C
:
280 case TEGRA20_SPDIF_CH_STA_RX_D
:
281 case TEGRA20_SPDIF_CH_STA_RX_E
:
282 case TEGRA20_SPDIF_CH_STA_RX_F
:
283 case TEGRA20_SPDIF_CH_STA_TX_A
:
284 case TEGRA20_SPDIF_CH_STA_TX_B
:
285 case TEGRA20_SPDIF_CH_STA_TX_C
:
286 case TEGRA20_SPDIF_CH_STA_TX_D
:
287 case TEGRA20_SPDIF_CH_STA_TX_E
:
288 case TEGRA20_SPDIF_CH_STA_TX_F
:
289 case TEGRA20_SPDIF_USR_STA_RX_A
:
290 case TEGRA20_SPDIF_USR_DAT_TX_A
:
297 static bool tegra20_spdif_volatile_reg(struct device
*dev
, unsigned int reg
)
300 case TEGRA20_SPDIF_STATUS
:
301 case TEGRA20_SPDIF_DATA_FIFO_CSR
:
302 case TEGRA20_SPDIF_DATA_OUT
:
303 case TEGRA20_SPDIF_DATA_IN
:
304 case TEGRA20_SPDIF_CH_STA_RX_A
:
305 case TEGRA20_SPDIF_CH_STA_RX_B
:
306 case TEGRA20_SPDIF_CH_STA_RX_C
:
307 case TEGRA20_SPDIF_CH_STA_RX_D
:
308 case TEGRA20_SPDIF_CH_STA_RX_E
:
309 case TEGRA20_SPDIF_CH_STA_RX_F
:
310 case TEGRA20_SPDIF_USR_STA_RX_A
:
311 case TEGRA20_SPDIF_USR_DAT_TX_A
:
318 static bool tegra20_spdif_precious_reg(struct device
*dev
, unsigned int reg
)
321 case TEGRA20_SPDIF_DATA_OUT
:
322 case TEGRA20_SPDIF_DATA_IN
:
323 case TEGRA20_SPDIF_USR_STA_RX_A
:
324 case TEGRA20_SPDIF_USR_DAT_TX_A
:
331 static const struct regmap_config tegra20_spdif_regmap_config
= {
335 .max_register
= TEGRA20_SPDIF_USR_DAT_TX_A
,
336 .writeable_reg
= tegra20_spdif_wr_rd_reg
,
337 .readable_reg
= tegra20_spdif_wr_rd_reg
,
338 .volatile_reg
= tegra20_spdif_volatile_reg
,
339 .precious_reg
= tegra20_spdif_precious_reg
,
340 .cache_type
= REGCACHE_FLAT
,
343 static int tegra20_spdif_platform_probe(struct platform_device
*pdev
)
345 struct tegra20_spdif
*spdif
;
346 struct resource
*mem
;
350 spdif
= devm_kzalloc(&pdev
->dev
, sizeof(struct tegra20_spdif
),
355 dev_set_drvdata(&pdev
->dev
, spdif
);
357 spdif
->reset
= devm_reset_control_get_exclusive(&pdev
->dev
, NULL
);
358 if (IS_ERR(spdif
->reset
)) {
359 dev_err(&pdev
->dev
, "Can't retrieve spdif reset\n");
360 return PTR_ERR(spdif
->reset
);
363 spdif
->clk_spdif_out
= devm_clk_get(&pdev
->dev
, "out");
364 if (IS_ERR(spdif
->clk_spdif_out
)) {
365 dev_err(&pdev
->dev
, "Could not retrieve spdif clock\n");
366 return PTR_ERR(spdif
->clk_spdif_out
);
369 regs
= devm_platform_get_and_ioremap_resource(pdev
, 0, &mem
);
371 return PTR_ERR(regs
);
373 spdif
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, regs
,
374 &tegra20_spdif_regmap_config
);
375 if (IS_ERR(spdif
->regmap
)) {
376 dev_err(&pdev
->dev
, "regmap init failed\n");
377 return PTR_ERR(spdif
->regmap
);
380 spdif
->playback_dma_data
.addr
= mem
->start
+ TEGRA20_SPDIF_DATA_OUT
;
381 spdif
->playback_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
382 spdif
->playback_dma_data
.maxburst
= 4;
384 ret
= devm_pm_runtime_enable(&pdev
->dev
);
388 ret
= devm_snd_soc_register_component(&pdev
->dev
,
389 &tegra20_spdif_component
,
390 &tegra20_spdif_dai
, 1);
392 dev_err(&pdev
->dev
, "Could not register DAI: %d\n", ret
);
396 ret
= devm_tegra_pcm_platform_register(&pdev
->dev
);
398 dev_err(&pdev
->dev
, "Could not register PCM: %d\n", ret
);
405 static const struct dev_pm_ops tegra20_spdif_pm_ops
= {
406 SET_RUNTIME_PM_OPS(tegra20_spdif_runtime_suspend
,
407 tegra20_spdif_runtime_resume
, NULL
)
408 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
409 pm_runtime_force_resume
)
412 static const struct of_device_id tegra20_spdif_of_match
[] = {
413 { .compatible
= "nvidia,tegra20-spdif", },
416 MODULE_DEVICE_TABLE(of
, tegra20_spdif_of_match
);
418 static struct platform_driver tegra20_spdif_driver
= {
420 .name
= "tegra20-spdif",
421 .pm
= &tegra20_spdif_pm_ops
,
422 .of_match_table
= tegra20_spdif_of_match
,
424 .probe
= tegra20_spdif_platform_probe
,
426 module_platform_driver(tegra20_spdif_driver
);
428 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
429 MODULE_DESCRIPTION("Tegra20 SPDIF ASoC driver");
430 MODULE_LICENSE("GPL");