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/debugfs.h>
25 #include <linux/device.h>
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/seq_file.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>
36 #include "tegra20_spdif.h"
38 #define DRV_NAME "tegra20-spdif"
40 static inline void tegra20_spdif_write(struct tegra20_spdif *spdif, u32 reg,
43 __raw_writel(val, spdif->regs + reg);
46 static inline u32 tegra20_spdif_read(struct tegra20_spdif *spdif, u32 reg)
48 return __raw_readl(spdif->regs + reg);
51 #ifdef CONFIG_DEBUG_FS
52 static int tegra20_spdif_show(struct seq_file *s, void *unused)
54 #define REG(r) { r, #r }
59 REG(TEGRA20_SPDIF_CTRL),
60 REG(TEGRA20_SPDIF_STATUS),
61 REG(TEGRA20_SPDIF_STROBE_CTRL),
62 REG(TEGRA20_SPDIF_DATA_FIFO_CSR),
63 REG(TEGRA20_SPDIF_CH_STA_RX_A),
64 REG(TEGRA20_SPDIF_CH_STA_RX_B),
65 REG(TEGRA20_SPDIF_CH_STA_RX_C),
66 REG(TEGRA20_SPDIF_CH_STA_RX_D),
67 REG(TEGRA20_SPDIF_CH_STA_RX_E),
68 REG(TEGRA20_SPDIF_CH_STA_RX_F),
69 REG(TEGRA20_SPDIF_CH_STA_TX_A),
70 REG(TEGRA20_SPDIF_CH_STA_TX_B),
71 REG(TEGRA20_SPDIF_CH_STA_TX_C),
72 REG(TEGRA20_SPDIF_CH_STA_TX_D),
73 REG(TEGRA20_SPDIF_CH_STA_TX_E),
74 REG(TEGRA20_SPDIF_CH_STA_TX_F),
78 struct tegra20_spdif *spdif = s->private;
81 for (i = 0; i < ARRAY_SIZE(regs); i++) {
82 u32 val = tegra20_spdif_read(spdif, regs[i].offset);
83 seq_printf(s, "%s = %08x\n", regs[i].name, val);
89 static int tegra20_spdif_debug_open(struct inode *inode, struct file *file)
91 return single_open(file, tegra20_spdif_show, inode->i_private);
94 static const struct file_operations tegra20_spdif_debug_fops = {
95 .open = tegra20_spdif_debug_open,
98 .release = single_release,
101 static void tegra20_spdif_debug_add(struct tegra20_spdif *spdif)
103 spdif->debug = debugfs_create_file(DRV_NAME, S_IRUGO,
104 snd_soc_debugfs_root, spdif,
105 &tegra20_spdif_debug_fops);
108 static void tegra20_spdif_debug_remove(struct tegra20_spdif *spdif)
111 debugfs_remove(spdif->debug);
114 static inline void tegra20_spdif_debug_add(struct tegra20_spdif *spdif)
118 static inline void tegra20_spdif_debug_remove(struct tegra20_spdif *spdif)
123 static int tegra20_spdif_hw_params(struct snd_pcm_substream *substream,
124 struct snd_pcm_hw_params *params,
125 struct snd_soc_dai *dai)
127 struct device *dev = substream->pcm->card->dev;
128 struct tegra20_spdif *spdif = snd_soc_dai_get_drvdata(dai);
131 spdif->reg_ctrl &= ~TEGRA20_SPDIF_CTRL_PACK;
132 spdif->reg_ctrl &= ~TEGRA20_SPDIF_CTRL_BIT_MODE_MASK;
133 switch (params_format(params)) {
134 case SNDRV_PCM_FORMAT_S16_LE:
135 spdif->reg_ctrl |= TEGRA20_SPDIF_CTRL_PACK;
136 spdif->reg_ctrl |= TEGRA20_SPDIF_CTRL_BIT_MODE_16BIT;
142 switch (params_rate(params)) {
144 spdifclock = 4096000;
147 spdifclock = 5644800;
150 spdifclock = 6144000;
153 spdifclock = 11289600;
156 spdifclock = 12288000;
159 spdifclock = 22579200;
162 spdifclock = 24576000;
168 ret = clk_set_rate(spdif->clk_spdif_out, spdifclock);
170 dev_err(dev, "Can't set SPDIF clock rate: %d\n", ret);
177 static void tegra20_spdif_start_playback(struct tegra20_spdif *spdif)
179 spdif->reg_ctrl |= TEGRA20_SPDIF_CTRL_TX_EN;
180 tegra20_spdif_write(spdif, TEGRA20_SPDIF_CTRL, spdif->reg_ctrl);
183 static void tegra20_spdif_stop_playback(struct tegra20_spdif *spdif)
185 spdif->reg_ctrl &= ~TEGRA20_SPDIF_CTRL_TX_EN;
186 tegra20_spdif_write(spdif, TEGRA20_SPDIF_CTRL, spdif->reg_ctrl);
189 static int tegra20_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
190 struct snd_soc_dai *dai)
192 struct tegra20_spdif *spdif = snd_soc_dai_get_drvdata(dai);
195 case SNDRV_PCM_TRIGGER_START:
196 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
197 case SNDRV_PCM_TRIGGER_RESUME:
198 clk_enable(spdif->clk_spdif_out);
199 tegra20_spdif_start_playback(spdif);
201 case SNDRV_PCM_TRIGGER_STOP:
202 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
203 case SNDRV_PCM_TRIGGER_SUSPEND:
204 tegra20_spdif_stop_playback(spdif);
205 clk_disable(spdif->clk_spdif_out);
214 static int tegra20_spdif_probe(struct snd_soc_dai *dai)
216 struct tegra20_spdif *spdif = snd_soc_dai_get_drvdata(dai);
218 dai->capture_dma_data = NULL;
219 dai->playback_dma_data = &spdif->playback_dma_data;
224 static const struct snd_soc_dai_ops tegra20_spdif_dai_ops = {
225 .hw_params = tegra20_spdif_hw_params,
226 .trigger = tegra20_spdif_trigger,
229 static struct snd_soc_dai_driver tegra20_spdif_dai = {
231 .probe = tegra20_spdif_probe,
235 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
236 SNDRV_PCM_RATE_48000,
237 .formats = SNDRV_PCM_FMTBIT_S16_LE,
239 .ops = &tegra20_spdif_dai_ops,
242 static __devinit int tegra20_spdif_platform_probe(struct platform_device *pdev)
244 struct tegra20_spdif *spdif;
245 struct resource *mem, *memregion, *dmareq;
248 spdif = kzalloc(sizeof(struct tegra20_spdif), GFP_KERNEL);
250 dev_err(&pdev->dev, "Can't allocate tegra20_spdif\n");
254 dev_set_drvdata(&pdev->dev, spdif);
256 spdif->clk_spdif_out = clk_get(&pdev->dev, "spdif_out");
257 if (IS_ERR(spdif->clk_spdif_out)) {
258 pr_err("Can't retrieve spdif clock\n");
259 ret = PTR_ERR(spdif->clk_spdif_out);
263 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
265 dev_err(&pdev->dev, "No memory resource\n");
270 dmareq = platform_get_resource(pdev, IORESOURCE_DMA, 0);
272 dev_err(&pdev->dev, "No DMA resource\n");
277 memregion = request_mem_region(mem->start, resource_size(mem),
280 dev_err(&pdev->dev, "Memory region already claimed\n");
285 spdif->regs = ioremap(mem->start, resource_size(mem));
287 dev_err(&pdev->dev, "ioremap failed\n");
292 spdif->playback_dma_data.addr = mem->start + TEGRA20_SPDIF_DATA_OUT;
293 spdif->playback_dma_data.wrap = 4;
294 spdif->playback_dma_data.width = 32;
295 spdif->playback_dma_data.req_sel = dmareq->start;
297 ret = snd_soc_register_dai(&pdev->dev, &tegra20_spdif_dai);
299 dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
304 ret = tegra_pcm_platform_register(&pdev->dev);
306 dev_err(&pdev->dev, "Could not register PCM: %d\n", ret);
307 goto err_unregister_dai;
310 tegra20_spdif_debug_add(spdif);
315 snd_soc_unregister_dai(&pdev->dev);
317 iounmap(spdif->regs);
319 release_mem_region(mem->start, resource_size(mem));
321 clk_put(spdif->clk_spdif_out);
328 static int __devexit tegra20_spdif_platform_remove(struct platform_device *pdev)
330 struct tegra20_spdif *spdif = dev_get_drvdata(&pdev->dev);
331 struct resource *res;
333 tegra_pcm_platform_unregister(&pdev->dev);
334 snd_soc_unregister_dai(&pdev->dev);
336 tegra20_spdif_debug_remove(spdif);
338 iounmap(spdif->regs);
340 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
341 release_mem_region(res->start, resource_size(res));
343 clk_put(spdif->clk_spdif_out);
350 static struct platform_driver tegra20_spdif_driver = {
353 .owner = THIS_MODULE,
355 .probe = tegra20_spdif_platform_probe,
356 .remove = __devexit_p(tegra20_spdif_platform_remove),
359 module_platform_driver(tegra20_spdif_driver);
361 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
362 MODULE_DESCRIPTION("Tegra20 SPDIF ASoC driver");
363 MODULE_LICENSE("GPL");
364 MODULE_ALIAS("platform:" DRV_NAME);