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/device.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <sound/dmaengine_pcm.h>
23 #include "tegra20_spdif.h"
25 #define DRV_NAME "tegra20-spdif"
27 static int tegra20_spdif_runtime_suspend(struct device
*dev
)
29 struct tegra20_spdif
*spdif
= dev_get_drvdata(dev
);
31 clk_disable_unprepare(spdif
->clk_spdif_out
);
36 static int tegra20_spdif_runtime_resume(struct device
*dev
)
38 struct tegra20_spdif
*spdif
= dev_get_drvdata(dev
);
41 ret
= clk_prepare_enable(spdif
->clk_spdif_out
);
43 dev_err(dev
, "clk_enable failed: %d\n", ret
);
50 static int tegra20_spdif_hw_params(struct snd_pcm_substream
*substream
,
51 struct snd_pcm_hw_params
*params
,
52 struct snd_soc_dai
*dai
)
54 struct device
*dev
= dai
->dev
;
55 struct tegra20_spdif
*spdif
= snd_soc_dai_get_drvdata(dai
);
56 unsigned int mask
= 0, val
= 0;
59 mask
|= TEGRA20_SPDIF_CTRL_PACK
|
60 TEGRA20_SPDIF_CTRL_BIT_MODE_MASK
;
61 switch (params_format(params
)) {
62 case SNDRV_PCM_FORMAT_S16_LE
:
63 val
|= TEGRA20_SPDIF_CTRL_PACK
|
64 TEGRA20_SPDIF_CTRL_BIT_MODE_16BIT
;
70 regmap_update_bits(spdif
->regmap
, TEGRA20_SPDIF_CTRL
, mask
, val
);
72 switch (params_rate(params
)) {
83 spdifclock
= 11289600;
86 spdifclock
= 12288000;
89 spdifclock
= 22579200;
92 spdifclock
= 24576000;
98 ret
= clk_set_rate(spdif
->clk_spdif_out
, spdifclock
);
100 dev_err(dev
, "Can't set SPDIF clock rate: %d\n", ret
);
107 static void tegra20_spdif_start_playback(struct tegra20_spdif
*spdif
)
109 regmap_update_bits(spdif
->regmap
, TEGRA20_SPDIF_CTRL
,
110 TEGRA20_SPDIF_CTRL_TX_EN
,
111 TEGRA20_SPDIF_CTRL_TX_EN
);
114 static void tegra20_spdif_stop_playback(struct tegra20_spdif
*spdif
)
116 regmap_update_bits(spdif
->regmap
, TEGRA20_SPDIF_CTRL
,
117 TEGRA20_SPDIF_CTRL_TX_EN
, 0);
120 static int tegra20_spdif_trigger(struct snd_pcm_substream
*substream
, int cmd
,
121 struct snd_soc_dai
*dai
)
123 struct tegra20_spdif
*spdif
= snd_soc_dai_get_drvdata(dai
);
126 case SNDRV_PCM_TRIGGER_START
:
127 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
128 case SNDRV_PCM_TRIGGER_RESUME
:
129 tegra20_spdif_start_playback(spdif
);
131 case SNDRV_PCM_TRIGGER_STOP
:
132 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
133 case SNDRV_PCM_TRIGGER_SUSPEND
:
134 tegra20_spdif_stop_playback(spdif
);
143 static int tegra20_spdif_probe(struct snd_soc_dai
*dai
)
145 struct tegra20_spdif
*spdif
= snd_soc_dai_get_drvdata(dai
);
147 dai
->capture_dma_data
= NULL
;
148 dai
->playback_dma_data
= &spdif
->playback_dma_data
;
153 static const struct snd_soc_dai_ops tegra20_spdif_dai_ops
= {
154 .hw_params
= tegra20_spdif_hw_params
,
155 .trigger
= tegra20_spdif_trigger
,
158 static struct snd_soc_dai_driver tegra20_spdif_dai
= {
160 .probe
= tegra20_spdif_probe
,
162 .stream_name
= "Playback",
165 .rates
= SNDRV_PCM_RATE_32000
| SNDRV_PCM_RATE_44100
|
166 SNDRV_PCM_RATE_48000
,
167 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
169 .ops
= &tegra20_spdif_dai_ops
,
172 static const struct snd_soc_component_driver tegra20_spdif_component
= {
176 static bool tegra20_spdif_wr_rd_reg(struct device
*dev
, unsigned int reg
)
179 case TEGRA20_SPDIF_CTRL
:
180 case TEGRA20_SPDIF_STATUS
:
181 case TEGRA20_SPDIF_STROBE_CTRL
:
182 case TEGRA20_SPDIF_DATA_FIFO_CSR
:
183 case TEGRA20_SPDIF_DATA_OUT
:
184 case TEGRA20_SPDIF_DATA_IN
:
185 case TEGRA20_SPDIF_CH_STA_RX_A
:
186 case TEGRA20_SPDIF_CH_STA_RX_B
:
187 case TEGRA20_SPDIF_CH_STA_RX_C
:
188 case TEGRA20_SPDIF_CH_STA_RX_D
:
189 case TEGRA20_SPDIF_CH_STA_RX_E
:
190 case TEGRA20_SPDIF_CH_STA_RX_F
:
191 case TEGRA20_SPDIF_CH_STA_TX_A
:
192 case TEGRA20_SPDIF_CH_STA_TX_B
:
193 case TEGRA20_SPDIF_CH_STA_TX_C
:
194 case TEGRA20_SPDIF_CH_STA_TX_D
:
195 case TEGRA20_SPDIF_CH_STA_TX_E
:
196 case TEGRA20_SPDIF_CH_STA_TX_F
:
197 case TEGRA20_SPDIF_USR_STA_RX_A
:
198 case TEGRA20_SPDIF_USR_DAT_TX_A
:
205 static bool tegra20_spdif_volatile_reg(struct device
*dev
, unsigned int reg
)
208 case TEGRA20_SPDIF_STATUS
:
209 case TEGRA20_SPDIF_DATA_FIFO_CSR
:
210 case TEGRA20_SPDIF_DATA_OUT
:
211 case TEGRA20_SPDIF_DATA_IN
:
212 case TEGRA20_SPDIF_CH_STA_RX_A
:
213 case TEGRA20_SPDIF_CH_STA_RX_B
:
214 case TEGRA20_SPDIF_CH_STA_RX_C
:
215 case TEGRA20_SPDIF_CH_STA_RX_D
:
216 case TEGRA20_SPDIF_CH_STA_RX_E
:
217 case TEGRA20_SPDIF_CH_STA_RX_F
:
218 case TEGRA20_SPDIF_USR_STA_RX_A
:
219 case TEGRA20_SPDIF_USR_DAT_TX_A
:
226 static bool tegra20_spdif_precious_reg(struct device
*dev
, unsigned int reg
)
229 case TEGRA20_SPDIF_DATA_OUT
:
230 case TEGRA20_SPDIF_DATA_IN
:
231 case TEGRA20_SPDIF_USR_STA_RX_A
:
232 case TEGRA20_SPDIF_USR_DAT_TX_A
:
239 static const struct regmap_config tegra20_spdif_regmap_config
= {
243 .max_register
= TEGRA20_SPDIF_USR_DAT_TX_A
,
244 .writeable_reg
= tegra20_spdif_wr_rd_reg
,
245 .readable_reg
= tegra20_spdif_wr_rd_reg
,
246 .volatile_reg
= tegra20_spdif_volatile_reg
,
247 .precious_reg
= tegra20_spdif_precious_reg
,
248 .cache_type
= REGCACHE_FLAT
,
251 static int tegra20_spdif_platform_probe(struct platform_device
*pdev
)
253 struct tegra20_spdif
*spdif
;
254 struct resource
*mem
, *dmareq
;
258 spdif
= devm_kzalloc(&pdev
->dev
, sizeof(struct tegra20_spdif
),
263 dev_set_drvdata(&pdev
->dev
, spdif
);
265 spdif
->clk_spdif_out
= devm_clk_get(&pdev
->dev
, "spdif_out");
266 if (IS_ERR(spdif
->clk_spdif_out
)) {
267 pr_err("Can't retrieve spdif clock\n");
268 ret
= PTR_ERR(spdif
->clk_spdif_out
);
272 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
273 regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
275 return PTR_ERR(regs
);
277 dmareq
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
279 dev_err(&pdev
->dev
, "No DMA resource\n");
283 spdif
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, regs
,
284 &tegra20_spdif_regmap_config
);
285 if (IS_ERR(spdif
->regmap
)) {
286 dev_err(&pdev
->dev
, "regmap init failed\n");
287 ret
= PTR_ERR(spdif
->regmap
);
291 spdif
->playback_dma_data
.addr
= mem
->start
+ TEGRA20_SPDIF_DATA_OUT
;
292 spdif
->playback_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
293 spdif
->playback_dma_data
.maxburst
= 4;
294 spdif
->playback_dma_data
.slave_id
= dmareq
->start
;
296 pm_runtime_enable(&pdev
->dev
);
297 if (!pm_runtime_enabled(&pdev
->dev
)) {
298 ret
= tegra20_spdif_runtime_resume(&pdev
->dev
);
303 ret
= snd_soc_register_component(&pdev
->dev
, &tegra20_spdif_component
,
304 &tegra20_spdif_dai
, 1);
306 dev_err(&pdev
->dev
, "Could not register DAI: %d\n", ret
);
311 ret
= tegra_pcm_platform_register(&pdev
->dev
);
313 dev_err(&pdev
->dev
, "Could not register PCM: %d\n", ret
);
314 goto err_unregister_component
;
319 err_unregister_component
:
320 snd_soc_unregister_component(&pdev
->dev
);
322 if (!pm_runtime_status_suspended(&pdev
->dev
))
323 tegra20_spdif_runtime_suspend(&pdev
->dev
);
325 pm_runtime_disable(&pdev
->dev
);
330 static int tegra20_spdif_platform_remove(struct platform_device
*pdev
)
332 pm_runtime_disable(&pdev
->dev
);
333 if (!pm_runtime_status_suspended(&pdev
->dev
))
334 tegra20_spdif_runtime_suspend(&pdev
->dev
);
336 tegra_pcm_platform_unregister(&pdev
->dev
);
337 snd_soc_unregister_component(&pdev
->dev
);
342 static const struct dev_pm_ops tegra20_spdif_pm_ops
= {
343 SET_RUNTIME_PM_OPS(tegra20_spdif_runtime_suspend
,
344 tegra20_spdif_runtime_resume
, NULL
)
347 static struct platform_driver tegra20_spdif_driver
= {
350 .pm
= &tegra20_spdif_pm_ops
,
352 .probe
= tegra20_spdif_platform_probe
,
353 .remove
= tegra20_spdif_platform_remove
,
356 module_platform_driver(tegra20_spdif_driver
);
358 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
359 MODULE_DESCRIPTION("Tegra20 SPDIF ASoC driver");
360 MODULE_LICENSE("GPL");
361 MODULE_ALIAS("platform:" DRV_NAME
);