2 * tegra20_spdif.c - Tegra20 SPDIF driver
4 * Author: Stephen Warren <swarren@nvidia.com>
5 * Copyright (C) 2011-2012 - NVIDIA, Inc.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 #include <linux/clk.h>
24 #include <linux/device.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/regmap.h>
30 #include <linux/slab.h>
31 #include <sound/core.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/soc.h>
35 #include <sound/dmaengine_pcm.h>
37 #include "tegra20_spdif.h"
39 #define DRV_NAME "tegra20-spdif"
41 static int tegra20_spdif_runtime_suspend(struct device *dev)
43 struct tegra20_spdif *spdif = dev_get_drvdata(dev);
45 clk_disable_unprepare(spdif->clk_spdif_out);
50 static int tegra20_spdif_runtime_resume(struct device *dev)
52 struct tegra20_spdif *spdif = dev_get_drvdata(dev);
55 ret = clk_prepare_enable(spdif->clk_spdif_out);
57 dev_err(dev, "clk_enable failed: %d\n", ret);
64 static int tegra20_spdif_hw_params(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params *params,
66 struct snd_soc_dai *dai)
68 struct device *dev = dai->dev;
69 struct tegra20_spdif *spdif = snd_soc_dai_get_drvdata(dai);
70 unsigned int mask, val;
73 mask = TEGRA20_SPDIF_CTRL_PACK |
74 TEGRA20_SPDIF_CTRL_BIT_MODE_MASK;
75 switch (params_format(params)) {
76 case SNDRV_PCM_FORMAT_S16_LE:
77 val = TEGRA20_SPDIF_CTRL_PACK |
78 TEGRA20_SPDIF_CTRL_BIT_MODE_16BIT;
84 regmap_update_bits(spdif->regmap, TEGRA20_SPDIF_CTRL, mask, val);
86 switch (params_rate(params)) {
97 spdifclock = 11289600;
100 spdifclock = 12288000;
103 spdifclock = 22579200;
106 spdifclock = 24576000;
112 ret = clk_set_rate(spdif->clk_spdif_out, spdifclock);
114 dev_err(dev, "Can't set SPDIF clock rate: %d\n", ret);
121 static void tegra20_spdif_start_playback(struct tegra20_spdif *spdif)
123 regmap_update_bits(spdif->regmap, TEGRA20_SPDIF_CTRL,
124 TEGRA20_SPDIF_CTRL_TX_EN,
125 TEGRA20_SPDIF_CTRL_TX_EN);
128 static void tegra20_spdif_stop_playback(struct tegra20_spdif *spdif)
130 regmap_update_bits(spdif->regmap, TEGRA20_SPDIF_CTRL,
131 TEGRA20_SPDIF_CTRL_TX_EN, 0);
134 static int tegra20_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
135 struct snd_soc_dai *dai)
137 struct tegra20_spdif *spdif = snd_soc_dai_get_drvdata(dai);
140 case SNDRV_PCM_TRIGGER_START:
141 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
142 case SNDRV_PCM_TRIGGER_RESUME:
143 tegra20_spdif_start_playback(spdif);
145 case SNDRV_PCM_TRIGGER_STOP:
146 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
147 case SNDRV_PCM_TRIGGER_SUSPEND:
148 tegra20_spdif_stop_playback(spdif);
157 static int tegra20_spdif_probe(struct snd_soc_dai *dai)
159 struct tegra20_spdif *spdif = snd_soc_dai_get_drvdata(dai);
161 dai->capture_dma_data = NULL;
162 dai->playback_dma_data = &spdif->playback_dma_data;
167 static const struct snd_soc_dai_ops tegra20_spdif_dai_ops = {
168 .hw_params = tegra20_spdif_hw_params,
169 .trigger = tegra20_spdif_trigger,
172 static struct snd_soc_dai_driver tegra20_spdif_dai = {
174 .probe = tegra20_spdif_probe,
176 .stream_name = "Playback",
179 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
180 SNDRV_PCM_RATE_48000,
181 .formats = SNDRV_PCM_FMTBIT_S16_LE,
183 .ops = &tegra20_spdif_dai_ops,
186 static const struct snd_soc_component_driver tegra20_spdif_component = {
190 static bool tegra20_spdif_wr_rd_reg(struct device *dev, unsigned int reg)
193 case TEGRA20_SPDIF_CTRL:
194 case TEGRA20_SPDIF_STATUS:
195 case TEGRA20_SPDIF_STROBE_CTRL:
196 case TEGRA20_SPDIF_DATA_FIFO_CSR:
197 case TEGRA20_SPDIF_DATA_OUT:
198 case TEGRA20_SPDIF_DATA_IN:
199 case TEGRA20_SPDIF_CH_STA_RX_A:
200 case TEGRA20_SPDIF_CH_STA_RX_B:
201 case TEGRA20_SPDIF_CH_STA_RX_C:
202 case TEGRA20_SPDIF_CH_STA_RX_D:
203 case TEGRA20_SPDIF_CH_STA_RX_E:
204 case TEGRA20_SPDIF_CH_STA_RX_F:
205 case TEGRA20_SPDIF_CH_STA_TX_A:
206 case TEGRA20_SPDIF_CH_STA_TX_B:
207 case TEGRA20_SPDIF_CH_STA_TX_C:
208 case TEGRA20_SPDIF_CH_STA_TX_D:
209 case TEGRA20_SPDIF_CH_STA_TX_E:
210 case TEGRA20_SPDIF_CH_STA_TX_F:
211 case TEGRA20_SPDIF_USR_STA_RX_A:
212 case TEGRA20_SPDIF_USR_DAT_TX_A:
219 static bool tegra20_spdif_volatile_reg(struct device *dev, unsigned int reg)
222 case TEGRA20_SPDIF_STATUS:
223 case TEGRA20_SPDIF_DATA_FIFO_CSR:
224 case TEGRA20_SPDIF_DATA_OUT:
225 case TEGRA20_SPDIF_DATA_IN:
226 case TEGRA20_SPDIF_CH_STA_RX_A:
227 case TEGRA20_SPDIF_CH_STA_RX_B:
228 case TEGRA20_SPDIF_CH_STA_RX_C:
229 case TEGRA20_SPDIF_CH_STA_RX_D:
230 case TEGRA20_SPDIF_CH_STA_RX_E:
231 case TEGRA20_SPDIF_CH_STA_RX_F:
232 case TEGRA20_SPDIF_USR_STA_RX_A:
233 case TEGRA20_SPDIF_USR_DAT_TX_A:
240 static bool tegra20_spdif_precious_reg(struct device *dev, unsigned int reg)
243 case TEGRA20_SPDIF_DATA_OUT:
244 case TEGRA20_SPDIF_DATA_IN:
245 case TEGRA20_SPDIF_USR_STA_RX_A:
246 case TEGRA20_SPDIF_USR_DAT_TX_A:
253 static const struct regmap_config tegra20_spdif_regmap_config = {
257 .max_register = TEGRA20_SPDIF_USR_DAT_TX_A,
258 .writeable_reg = tegra20_spdif_wr_rd_reg,
259 .readable_reg = tegra20_spdif_wr_rd_reg,
260 .volatile_reg = tegra20_spdif_volatile_reg,
261 .precious_reg = tegra20_spdif_precious_reg,
262 .cache_type = REGCACHE_RBTREE,
265 static int tegra20_spdif_platform_probe(struct platform_device *pdev)
267 struct tegra20_spdif *spdif;
268 struct resource *mem, *memregion, *dmareq;
272 spdif = devm_kzalloc(&pdev->dev, sizeof(struct tegra20_spdif),
275 dev_err(&pdev->dev, "Can't allocate tegra20_spdif\n");
279 dev_set_drvdata(&pdev->dev, spdif);
281 spdif->clk_spdif_out = clk_get(&pdev->dev, "spdif_out");
282 if (IS_ERR(spdif->clk_spdif_out)) {
283 pr_err("Can't retrieve spdif clock\n");
284 ret = PTR_ERR(spdif->clk_spdif_out);
288 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
290 dev_err(&pdev->dev, "No memory resource\n");
295 dmareq = platform_get_resource(pdev, IORESOURCE_DMA, 0);
297 dev_err(&pdev->dev, "No DMA resource\n");
302 memregion = devm_request_mem_region(&pdev->dev, mem->start,
303 resource_size(mem), DRV_NAME);
305 dev_err(&pdev->dev, "Memory region already claimed\n");
310 regs = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
312 dev_err(&pdev->dev, "ioremap failed\n");
317 spdif->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
318 &tegra20_spdif_regmap_config);
319 if (IS_ERR(spdif->regmap)) {
320 dev_err(&pdev->dev, "regmap init failed\n");
321 ret = PTR_ERR(spdif->regmap);
325 spdif->playback_dma_data.addr = mem->start + TEGRA20_SPDIF_DATA_OUT;
326 spdif->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
327 spdif->playback_dma_data.maxburst = 4;
328 spdif->playback_dma_data.slave_id = dmareq->start;
330 pm_runtime_enable(&pdev->dev);
331 if (!pm_runtime_enabled(&pdev->dev)) {
332 ret = tegra20_spdif_runtime_resume(&pdev->dev);
337 ret = snd_soc_register_component(&pdev->dev, &tegra20_spdif_component,
338 &tegra20_spdif_dai, 1);
340 dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
345 ret = tegra_pcm_platform_register(&pdev->dev);
347 dev_err(&pdev->dev, "Could not register PCM: %d\n", ret);
348 goto err_unregister_component;
353 err_unregister_component:
354 snd_soc_unregister_component(&pdev->dev);
356 if (!pm_runtime_status_suspended(&pdev->dev))
357 tegra20_spdif_runtime_suspend(&pdev->dev);
359 pm_runtime_disable(&pdev->dev);
361 clk_put(spdif->clk_spdif_out);
366 static int tegra20_spdif_platform_remove(struct platform_device *pdev)
368 struct tegra20_spdif *spdif = dev_get_drvdata(&pdev->dev);
370 pm_runtime_disable(&pdev->dev);
371 if (!pm_runtime_status_suspended(&pdev->dev))
372 tegra20_spdif_runtime_suspend(&pdev->dev);
374 tegra_pcm_platform_unregister(&pdev->dev);
375 snd_soc_unregister_component(&pdev->dev);
377 clk_put(spdif->clk_spdif_out);
382 static const struct dev_pm_ops tegra20_spdif_pm_ops = {
383 SET_RUNTIME_PM_OPS(tegra20_spdif_runtime_suspend,
384 tegra20_spdif_runtime_resume, NULL)
387 static struct platform_driver tegra20_spdif_driver = {
390 .owner = THIS_MODULE,
391 .pm = &tegra20_spdif_pm_ops,
393 .probe = tegra20_spdif_platform_probe,
394 .remove = tegra20_spdif_platform_remove,
397 module_platform_driver(tegra20_spdif_driver);
399 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
400 MODULE_DESCRIPTION("Tegra20 SPDIF ASoC driver");
401 MODULE_LICENSE("GPL");
402 MODULE_ALIAS("platform:" DRV_NAME);