blob: 00a2d33352f4b3d97d14031610d3821fb76cbf91 [file] [log] [blame]
Stephen Warren774fec32011-07-05 10:55:27 -06001/*
Stephen Warrenef280d32012-04-05 15:54:53 -06002 * tegra20_spdif.c - Tegra20 SPDIF driver
Stephen Warren774fec32011-07-05 10:55:27 -06003 *
4 * Author: Stephen Warren <swarren@nvidia.com>
Stephen Warren518de862012-03-20 14:55:49 -06005 * Copyright (C) 2011-2012 - NVIDIA, Inc.
Stephen Warren774fec32011-07-05 10:55:27 -06006 *
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.
10 *
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.
15 *
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
19 * 02110-1301 USA
20 *
21 */
22
23#include <linux/clk.h>
Stephen Warren774fec32011-07-05 10:55:27 -060024#include <linux/debugfs.h>
25#include <linux/device.h>
Stephen Warren7613c502012-04-06 11:12:25 -060026#include <linux/io.h>
27#include <linux/module.h>
Stephen Warren774fec32011-07-05 10:55:27 -060028#include <linux/platform_device.h>
29#include <linux/seq_file.h>
30#include <linux/slab.h>
Stephen Warren774fec32011-07-05 10:55:27 -060031#include <sound/core.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/soc.h>
35
Stephen Warrenef280d32012-04-05 15:54:53 -060036#include "tegra20_spdif.h"
Stephen Warren774fec32011-07-05 10:55:27 -060037
Stephen Warren896637a2012-04-06 10:30:52 -060038#define DRV_NAME "tegra20-spdif"
Stephen Warren774fec32011-07-05 10:55:27 -060039
Stephen Warren896637a2012-04-06 10:30:52 -060040static inline void tegra20_spdif_write(struct tegra20_spdif *spdif, u32 reg,
Stephen Warren774fec32011-07-05 10:55:27 -060041 u32 val)
42{
43 __raw_writel(val, spdif->regs + reg);
44}
45
Stephen Warren896637a2012-04-06 10:30:52 -060046static inline u32 tegra20_spdif_read(struct tegra20_spdif *spdif, u32 reg)
Stephen Warren774fec32011-07-05 10:55:27 -060047{
48 return __raw_readl(spdif->regs + reg);
49}
50
51#ifdef CONFIG_DEBUG_FS
Stephen Warren896637a2012-04-06 10:30:52 -060052static int tegra20_spdif_show(struct seq_file *s, void *unused)
Stephen Warren774fec32011-07-05 10:55:27 -060053{
54#define REG(r) { r, #r }
55 static const struct {
56 int offset;
57 const char *name;
58 } regs[] = {
Stephen Warren896637a2012-04-06 10:30:52 -060059 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),
Stephen Warren774fec32011-07-05 10:55:27 -060075 };
76#undef REG
77
Stephen Warren896637a2012-04-06 10:30:52 -060078 struct tegra20_spdif *spdif = s->private;
Stephen Warren774fec32011-07-05 10:55:27 -060079 int i;
80
81 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Stephen Warren896637a2012-04-06 10:30:52 -060082 u32 val = tegra20_spdif_read(spdif, regs[i].offset);
Stephen Warren774fec32011-07-05 10:55:27 -060083 seq_printf(s, "%s = %08x\n", regs[i].name, val);
84 }
85
86 return 0;
87}
88
Stephen Warren896637a2012-04-06 10:30:52 -060089static int tegra20_spdif_debug_open(struct inode *inode, struct file *file)
Stephen Warren774fec32011-07-05 10:55:27 -060090{
Stephen Warren896637a2012-04-06 10:30:52 -060091 return single_open(file, tegra20_spdif_show, inode->i_private);
Stephen Warren774fec32011-07-05 10:55:27 -060092}
93
Stephen Warren896637a2012-04-06 10:30:52 -060094static const struct file_operations tegra20_spdif_debug_fops = {
95 .open = tegra20_spdif_debug_open,
Stephen Warren774fec32011-07-05 10:55:27 -060096 .read = seq_read,
97 .llseek = seq_lseek,
98 .release = single_release,
99};
100
Stephen Warren896637a2012-04-06 10:30:52 -0600101static void tegra20_spdif_debug_add(struct tegra20_spdif *spdif)
Stephen Warren774fec32011-07-05 10:55:27 -0600102{
103 spdif->debug = debugfs_create_file(DRV_NAME, S_IRUGO,
104 snd_soc_debugfs_root, spdif,
Stephen Warren896637a2012-04-06 10:30:52 -0600105 &tegra20_spdif_debug_fops);
Stephen Warren774fec32011-07-05 10:55:27 -0600106}
107
Stephen Warren896637a2012-04-06 10:30:52 -0600108static void tegra20_spdif_debug_remove(struct tegra20_spdif *spdif)
Stephen Warren774fec32011-07-05 10:55:27 -0600109{
110 if (spdif->debug)
111 debugfs_remove(spdif->debug);
112}
113#else
Stephen Warren896637a2012-04-06 10:30:52 -0600114static inline void tegra20_spdif_debug_add(struct tegra20_spdif *spdif)
Stephen Warren774fec32011-07-05 10:55:27 -0600115{
116}
117
Stephen Warren896637a2012-04-06 10:30:52 -0600118static inline void tegra20_spdif_debug_remove(struct tegra20_spdif *spdif)
Stephen Warren774fec32011-07-05 10:55:27 -0600119{
120}
121#endif
122
Stephen Warren896637a2012-04-06 10:30:52 -0600123static int tegra20_spdif_hw_params(struct snd_pcm_substream *substream,
Stephen Warren774fec32011-07-05 10:55:27 -0600124 struct snd_pcm_hw_params *params,
125 struct snd_soc_dai *dai)
126{
127 struct device *dev = substream->pcm->card->dev;
Stephen Warren896637a2012-04-06 10:30:52 -0600128 struct tegra20_spdif *spdif = snd_soc_dai_get_drvdata(dai);
Axel Lin4b8713f2011-10-02 21:07:02 +0800129 int ret, spdifclock;
Stephen Warren774fec32011-07-05 10:55:27 -0600130
Stephen Warren896637a2012-04-06 10:30:52 -0600131 spdif->reg_ctrl &= ~TEGRA20_SPDIF_CTRL_PACK;
132 spdif->reg_ctrl &= ~TEGRA20_SPDIF_CTRL_BIT_MODE_MASK;
Stephen Warren774fec32011-07-05 10:55:27 -0600133 switch (params_format(params)) {
134 case SNDRV_PCM_FORMAT_S16_LE:
Stephen Warren896637a2012-04-06 10:30:52 -0600135 spdif->reg_ctrl |= TEGRA20_SPDIF_CTRL_PACK;
136 spdif->reg_ctrl |= TEGRA20_SPDIF_CTRL_BIT_MODE_16BIT;
Stephen Warren774fec32011-07-05 10:55:27 -0600137 break;
138 default:
139 return -EINVAL;
140 }
141
Stephen Warren774fec32011-07-05 10:55:27 -0600142 switch (params_rate(params)) {
143 case 32000:
144 spdifclock = 4096000;
145 break;
146 case 44100:
147 spdifclock = 5644800;
148 break;
149 case 48000:
150 spdifclock = 6144000;
151 break;
152 case 88200:
153 spdifclock = 11289600;
154 break;
155 case 96000:
156 spdifclock = 12288000;
157 break;
158 case 176400:
159 spdifclock = 22579200;
160 break;
161 case 192000:
162 spdifclock = 24576000;
163 break;
164 default:
165 return -EINVAL;
166 }
167
168 ret = clk_set_rate(spdif->clk_spdif_out, spdifclock);
169 if (ret) {
170 dev_err(dev, "Can't set SPDIF clock rate: %d\n", ret);
171 return ret;
172 }
173
174 return 0;
175}
176
Stephen Warren896637a2012-04-06 10:30:52 -0600177static void tegra20_spdif_start_playback(struct tegra20_spdif *spdif)
Stephen Warren774fec32011-07-05 10:55:27 -0600178{
Stephen Warren896637a2012-04-06 10:30:52 -0600179 spdif->reg_ctrl |= TEGRA20_SPDIF_CTRL_TX_EN;
180 tegra20_spdif_write(spdif, TEGRA20_SPDIF_CTRL, spdif->reg_ctrl);
Stephen Warren774fec32011-07-05 10:55:27 -0600181}
182
Stephen Warren896637a2012-04-06 10:30:52 -0600183static void tegra20_spdif_stop_playback(struct tegra20_spdif *spdif)
Stephen Warren774fec32011-07-05 10:55:27 -0600184{
Stephen Warren896637a2012-04-06 10:30:52 -0600185 spdif->reg_ctrl &= ~TEGRA20_SPDIF_CTRL_TX_EN;
186 tegra20_spdif_write(spdif, TEGRA20_SPDIF_CTRL, spdif->reg_ctrl);
Stephen Warren774fec32011-07-05 10:55:27 -0600187}
188
Stephen Warren896637a2012-04-06 10:30:52 -0600189static int tegra20_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
Stephen Warren774fec32011-07-05 10:55:27 -0600190 struct snd_soc_dai *dai)
191{
Stephen Warren896637a2012-04-06 10:30:52 -0600192 struct tegra20_spdif *spdif = snd_soc_dai_get_drvdata(dai);
Stephen Warren774fec32011-07-05 10:55:27 -0600193
194 switch (cmd) {
195 case SNDRV_PCM_TRIGGER_START:
196 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
197 case SNDRV_PCM_TRIGGER_RESUME:
Stephen Warren30d436a2012-03-30 17:07:16 -0600198 clk_enable(spdif->clk_spdif_out);
Stephen Warren896637a2012-04-06 10:30:52 -0600199 tegra20_spdif_start_playback(spdif);
Stephen Warren774fec32011-07-05 10:55:27 -0600200 break;
201 case SNDRV_PCM_TRIGGER_STOP:
202 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
203 case SNDRV_PCM_TRIGGER_SUSPEND:
Stephen Warren896637a2012-04-06 10:30:52 -0600204 tegra20_spdif_stop_playback(spdif);
Stephen Warren30d436a2012-03-30 17:07:16 -0600205 clk_disable(spdif->clk_spdif_out);
Stephen Warren774fec32011-07-05 10:55:27 -0600206 break;
207 default:
208 return -EINVAL;
209 }
210
211 return 0;
212}
213
Stephen Warren896637a2012-04-06 10:30:52 -0600214static int tegra20_spdif_probe(struct snd_soc_dai *dai)
Stephen Warren774fec32011-07-05 10:55:27 -0600215{
Stephen Warren896637a2012-04-06 10:30:52 -0600216 struct tegra20_spdif *spdif = snd_soc_dai_get_drvdata(dai);
Stephen Warren774fec32011-07-05 10:55:27 -0600217
218 dai->capture_dma_data = NULL;
219 dai->playback_dma_data = &spdif->playback_dma_data;
220
221 return 0;
222}
223
Stephen Warren896637a2012-04-06 10:30:52 -0600224static const struct snd_soc_dai_ops tegra20_spdif_dai_ops = {
225 .hw_params = tegra20_spdif_hw_params,
226 .trigger = tegra20_spdif_trigger,
Stephen Warren774fec32011-07-05 10:55:27 -0600227};
228
Stephen Warren896637a2012-04-06 10:30:52 -0600229static struct snd_soc_dai_driver tegra20_spdif_dai = {
Stephen Warren774fec32011-07-05 10:55:27 -0600230 .name = DRV_NAME,
Stephen Warren896637a2012-04-06 10:30:52 -0600231 .probe = tegra20_spdif_probe,
Stephen Warren774fec32011-07-05 10:55:27 -0600232 .playback = {
233 .channels_min = 2,
234 .channels_max = 2,
235 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
236 SNDRV_PCM_RATE_48000,
237 .formats = SNDRV_PCM_FMTBIT_S16_LE,
238 },
Stephen Warren896637a2012-04-06 10:30:52 -0600239 .ops = &tegra20_spdif_dai_ops,
Stephen Warren774fec32011-07-05 10:55:27 -0600240};
241
Stephen Warren896637a2012-04-06 10:30:52 -0600242static __devinit int tegra20_spdif_platform_probe(struct platform_device *pdev)
Stephen Warren774fec32011-07-05 10:55:27 -0600243{
Stephen Warren896637a2012-04-06 10:30:52 -0600244 struct tegra20_spdif *spdif;
Stephen Warren774fec32011-07-05 10:55:27 -0600245 struct resource *mem, *memregion, *dmareq;
246 int ret;
247
Stephen Warren896637a2012-04-06 10:30:52 -0600248 spdif = kzalloc(sizeof(struct tegra20_spdif), GFP_KERNEL);
Stephen Warren774fec32011-07-05 10:55:27 -0600249 if (!spdif) {
Stephen Warren896637a2012-04-06 10:30:52 -0600250 dev_err(&pdev->dev, "Can't allocate tegra20_spdif\n");
Stephen Warren774fec32011-07-05 10:55:27 -0600251 ret = -ENOMEM;
252 goto exit;
253 }
254 dev_set_drvdata(&pdev->dev, spdif);
255
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);
260 goto err_free;
261 }
262
263 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
264 if (!mem) {
265 dev_err(&pdev->dev, "No memory resource\n");
266 ret = -ENODEV;
267 goto err_clk_put;
268 }
269
270 dmareq = platform_get_resource(pdev, IORESOURCE_DMA, 0);
271 if (!dmareq) {
272 dev_err(&pdev->dev, "No DMA resource\n");
273 ret = -ENODEV;
274 goto err_clk_put;
275 }
276
277 memregion = request_mem_region(mem->start, resource_size(mem),
278 DRV_NAME);
279 if (!memregion) {
280 dev_err(&pdev->dev, "Memory region already claimed\n");
281 ret = -EBUSY;
282 goto err_clk_put;
283 }
284
285 spdif->regs = ioremap(mem->start, resource_size(mem));
286 if (!spdif->regs) {
287 dev_err(&pdev->dev, "ioremap failed\n");
288 ret = -ENOMEM;
289 goto err_release;
290 }
291
Stephen Warren896637a2012-04-06 10:30:52 -0600292 spdif->playback_dma_data.addr = mem->start + TEGRA20_SPDIF_DATA_OUT;
Stephen Warren774fec32011-07-05 10:55:27 -0600293 spdif->playback_dma_data.wrap = 4;
294 spdif->playback_dma_data.width = 32;
295 spdif->playback_dma_data.req_sel = dmareq->start;
296
Stephen Warren896637a2012-04-06 10:30:52 -0600297 ret = snd_soc_register_dai(&pdev->dev, &tegra20_spdif_dai);
Stephen Warren774fec32011-07-05 10:55:27 -0600298 if (ret) {
299 dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
300 ret = -ENOMEM;
301 goto err_unmap;
302 }
303
Stephen Warren518de862012-03-20 14:55:49 -0600304 ret = tegra_pcm_platform_register(&pdev->dev);
305 if (ret) {
306 dev_err(&pdev->dev, "Could not register PCM: %d\n", ret);
307 goto err_unregister_dai;
308 }
309
Stephen Warren896637a2012-04-06 10:30:52 -0600310 tegra20_spdif_debug_add(spdif);
Stephen Warren774fec32011-07-05 10:55:27 -0600311
312 return 0;
313
Stephen Warren518de862012-03-20 14:55:49 -0600314err_unregister_dai:
315 snd_soc_unregister_dai(&pdev->dev);
Stephen Warren774fec32011-07-05 10:55:27 -0600316err_unmap:
317 iounmap(spdif->regs);
318err_release:
319 release_mem_region(mem->start, resource_size(mem));
320err_clk_put:
321 clk_put(spdif->clk_spdif_out);
322err_free:
323 kfree(spdif);
324exit:
325 return ret;
326}
327
Stephen Warren896637a2012-04-06 10:30:52 -0600328static int __devexit tegra20_spdif_platform_remove(struct platform_device *pdev)
Stephen Warren774fec32011-07-05 10:55:27 -0600329{
Stephen Warren896637a2012-04-06 10:30:52 -0600330 struct tegra20_spdif *spdif = dev_get_drvdata(&pdev->dev);
Stephen Warren774fec32011-07-05 10:55:27 -0600331 struct resource *res;
332
Stephen Warren518de862012-03-20 14:55:49 -0600333 tegra_pcm_platform_unregister(&pdev->dev);
Stephen Warren774fec32011-07-05 10:55:27 -0600334 snd_soc_unregister_dai(&pdev->dev);
335
Stephen Warren896637a2012-04-06 10:30:52 -0600336 tegra20_spdif_debug_remove(spdif);
Stephen Warren774fec32011-07-05 10:55:27 -0600337
338 iounmap(spdif->regs);
339
340 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
341 release_mem_region(res->start, resource_size(res));
342
343 clk_put(spdif->clk_spdif_out);
344
345 kfree(spdif);
346
347 return 0;
348}
349
Stephen Warren896637a2012-04-06 10:30:52 -0600350static struct platform_driver tegra20_spdif_driver = {
Stephen Warren774fec32011-07-05 10:55:27 -0600351 .driver = {
352 .name = DRV_NAME,
353 .owner = THIS_MODULE,
354 },
Stephen Warren896637a2012-04-06 10:30:52 -0600355 .probe = tegra20_spdif_platform_probe,
356 .remove = __devexit_p(tegra20_spdif_platform_remove),
Stephen Warren774fec32011-07-05 10:55:27 -0600357};
358
Stephen Warren896637a2012-04-06 10:30:52 -0600359module_platform_driver(tegra20_spdif_driver);
Stephen Warren774fec32011-07-05 10:55:27 -0600360
361MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
Stephen Warren896637a2012-04-06 10:30:52 -0600362MODULE_DESCRIPTION("Tegra20 SPDIF ASoC driver");
Stephen Warren774fec32011-07-05 10:55:27 -0600363MODULE_LICENSE("GPL");
364MODULE_ALIAS("platform:" DRV_NAME);