blob: ed1fd502ef8dbe1d732ff9efd7c6eede67d3411b [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>
24#include <linux/module.h>
25#include <linux/debugfs.h>
26#include <linux/device.h>
27#include <linux/platform_device.h>
28#include <linux/seq_file.h>
29#include <linux/slab.h>
30#include <linux/io.h>
31#include <mach/iomap.h>
32#include <sound/core.h>
33#include <sound/pcm.h>
34#include <sound/pcm_params.h>
35#include <sound/soc.h>
36
Stephen Warrenef280d32012-04-05 15:54:53 -060037#include "tegra20_spdif.h"
Stephen Warren774fec32011-07-05 10:55:27 -060038
39#define DRV_NAME "tegra-spdif"
40
41static inline void tegra_spdif_write(struct tegra_spdif *spdif, u32 reg,
42 u32 val)
43{
44 __raw_writel(val, spdif->regs + reg);
45}
46
47static inline u32 tegra_spdif_read(struct tegra_spdif *spdif, u32 reg)
48{
49 return __raw_readl(spdif->regs + reg);
50}
51
52#ifdef CONFIG_DEBUG_FS
53static int tegra_spdif_show(struct seq_file *s, void *unused)
54{
55#define REG(r) { r, #r }
56 static const struct {
57 int offset;
58 const char *name;
59 } regs[] = {
60 REG(TEGRA_SPDIF_CTRL),
61 REG(TEGRA_SPDIF_STATUS),
62 REG(TEGRA_SPDIF_STROBE_CTRL),
63 REG(TEGRA_SPDIF_DATA_FIFO_CSR),
64 REG(TEGRA_SPDIF_CH_STA_RX_A),
65 REG(TEGRA_SPDIF_CH_STA_RX_B),
66 REG(TEGRA_SPDIF_CH_STA_RX_C),
67 REG(TEGRA_SPDIF_CH_STA_RX_D),
68 REG(TEGRA_SPDIF_CH_STA_RX_E),
69 REG(TEGRA_SPDIF_CH_STA_RX_F),
70 REG(TEGRA_SPDIF_CH_STA_TX_A),
71 REG(TEGRA_SPDIF_CH_STA_TX_B),
72 REG(TEGRA_SPDIF_CH_STA_TX_C),
73 REG(TEGRA_SPDIF_CH_STA_TX_D),
74 REG(TEGRA_SPDIF_CH_STA_TX_E),
75 REG(TEGRA_SPDIF_CH_STA_TX_F),
76 };
77#undef REG
78
79 struct tegra_spdif *spdif = s->private;
80 int i;
81
82 for (i = 0; i < ARRAY_SIZE(regs); i++) {
83 u32 val = tegra_spdif_read(spdif, regs[i].offset);
84 seq_printf(s, "%s = %08x\n", regs[i].name, val);
85 }
86
87 return 0;
88}
89
90static int tegra_spdif_debug_open(struct inode *inode, struct file *file)
91{
92 return single_open(file, tegra_spdif_show, inode->i_private);
93}
94
95static const struct file_operations tegra_spdif_debug_fops = {
96 .open = tegra_spdif_debug_open,
97 .read = seq_read,
98 .llseek = seq_lseek,
99 .release = single_release,
100};
101
102static void tegra_spdif_debug_add(struct tegra_spdif *spdif)
103{
104 spdif->debug = debugfs_create_file(DRV_NAME, S_IRUGO,
105 snd_soc_debugfs_root, spdif,
106 &tegra_spdif_debug_fops);
107}
108
109static void tegra_spdif_debug_remove(struct tegra_spdif *spdif)
110{
111 if (spdif->debug)
112 debugfs_remove(spdif->debug);
113}
114#else
115static inline void tegra_spdif_debug_add(struct tegra_spdif *spdif)
116{
117}
118
119static inline void tegra_spdif_debug_remove(struct tegra_spdif *spdif)
120{
121}
122#endif
123
124static int tegra_spdif_hw_params(struct snd_pcm_substream *substream,
125 struct snd_pcm_hw_params *params,
126 struct snd_soc_dai *dai)
127{
128 struct device *dev = substream->pcm->card->dev;
129 struct tegra_spdif *spdif = snd_soc_dai_get_drvdata(dai);
Axel Lin4b8713f2011-10-02 21:07:02 +0800130 int ret, spdifclock;
Stephen Warren774fec32011-07-05 10:55:27 -0600131
132 spdif->reg_ctrl &= ~TEGRA_SPDIF_CTRL_PACK;
133 spdif->reg_ctrl &= ~TEGRA_SPDIF_CTRL_BIT_MODE_MASK;
134 switch (params_format(params)) {
135 case SNDRV_PCM_FORMAT_S16_LE:
136 spdif->reg_ctrl |= TEGRA_SPDIF_CTRL_PACK;
137 spdif->reg_ctrl |= TEGRA_SPDIF_CTRL_BIT_MODE_16BIT;
138 break;
139 default:
140 return -EINVAL;
141 }
142
Stephen Warren774fec32011-07-05 10:55:27 -0600143 switch (params_rate(params)) {
144 case 32000:
145 spdifclock = 4096000;
146 break;
147 case 44100:
148 spdifclock = 5644800;
149 break;
150 case 48000:
151 spdifclock = 6144000;
152 break;
153 case 88200:
154 spdifclock = 11289600;
155 break;
156 case 96000:
157 spdifclock = 12288000;
158 break;
159 case 176400:
160 spdifclock = 22579200;
161 break;
162 case 192000:
163 spdifclock = 24576000;
164 break;
165 default:
166 return -EINVAL;
167 }
168
169 ret = clk_set_rate(spdif->clk_spdif_out, spdifclock);
170 if (ret) {
171 dev_err(dev, "Can't set SPDIF clock rate: %d\n", ret);
172 return ret;
173 }
174
175 return 0;
176}
177
178static void tegra_spdif_start_playback(struct tegra_spdif *spdif)
179{
180 spdif->reg_ctrl |= TEGRA_SPDIF_CTRL_TX_EN;
181 tegra_spdif_write(spdif, TEGRA_SPDIF_CTRL, spdif->reg_ctrl);
182}
183
184static void tegra_spdif_stop_playback(struct tegra_spdif *spdif)
185{
186 spdif->reg_ctrl &= ~TEGRA_SPDIF_CTRL_TX_EN;
187 tegra_spdif_write(spdif, TEGRA_SPDIF_CTRL, spdif->reg_ctrl);
188}
189
190static int tegra_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
191 struct snd_soc_dai *dai)
192{
193 struct tegra_spdif *spdif = snd_soc_dai_get_drvdata(dai);
194
195 switch (cmd) {
196 case SNDRV_PCM_TRIGGER_START:
197 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
198 case SNDRV_PCM_TRIGGER_RESUME:
Stephen Warren30d436a2012-03-30 17:07:16 -0600199 clk_enable(spdif->clk_spdif_out);
Stephen Warren774fec32011-07-05 10:55:27 -0600200 tegra_spdif_start_playback(spdif);
201 break;
202 case SNDRV_PCM_TRIGGER_STOP:
203 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
204 case SNDRV_PCM_TRIGGER_SUSPEND:
205 tegra_spdif_stop_playback(spdif);
Stephen Warren30d436a2012-03-30 17:07:16 -0600206 clk_disable(spdif->clk_spdif_out);
Stephen Warren774fec32011-07-05 10:55:27 -0600207 break;
208 default:
209 return -EINVAL;
210 }
211
212 return 0;
213}
214
215static int tegra_spdif_probe(struct snd_soc_dai *dai)
216{
217 struct tegra_spdif *spdif = snd_soc_dai_get_drvdata(dai);
218
219 dai->capture_dma_data = NULL;
220 dai->playback_dma_data = &spdif->playback_dma_data;
221
222 return 0;
223}
224
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100225static const struct snd_soc_dai_ops tegra_spdif_dai_ops = {
Stephen Warren774fec32011-07-05 10:55:27 -0600226 .hw_params = tegra_spdif_hw_params,
227 .trigger = tegra_spdif_trigger,
228};
229
Axel Lin019cd3b2011-10-02 17:41:40 +0800230static struct snd_soc_dai_driver tegra_spdif_dai = {
Stephen Warren774fec32011-07-05 10:55:27 -0600231 .name = DRV_NAME,
232 .probe = tegra_spdif_probe,
233 .playback = {
234 .channels_min = 2,
235 .channels_max = 2,
236 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
237 SNDRV_PCM_RATE_48000,
238 .formats = SNDRV_PCM_FMTBIT_S16_LE,
239 },
240 .ops = &tegra_spdif_dai_ops,
241};
242
243static __devinit int tegra_spdif_platform_probe(struct platform_device *pdev)
244{
245 struct tegra_spdif *spdif;
246 struct resource *mem, *memregion, *dmareq;
247 int ret;
248
249 spdif = kzalloc(sizeof(struct tegra_spdif), GFP_KERNEL);
250 if (!spdif) {
251 dev_err(&pdev->dev, "Can't allocate tegra_spdif\n");
252 ret = -ENOMEM;
253 goto exit;
254 }
255 dev_set_drvdata(&pdev->dev, spdif);
256
257 spdif->clk_spdif_out = clk_get(&pdev->dev, "spdif_out");
258 if (IS_ERR(spdif->clk_spdif_out)) {
259 pr_err("Can't retrieve spdif clock\n");
260 ret = PTR_ERR(spdif->clk_spdif_out);
261 goto err_free;
262 }
263
264 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
265 if (!mem) {
266 dev_err(&pdev->dev, "No memory resource\n");
267 ret = -ENODEV;
268 goto err_clk_put;
269 }
270
271 dmareq = platform_get_resource(pdev, IORESOURCE_DMA, 0);
272 if (!dmareq) {
273 dev_err(&pdev->dev, "No DMA resource\n");
274 ret = -ENODEV;
275 goto err_clk_put;
276 }
277
278 memregion = request_mem_region(mem->start, resource_size(mem),
279 DRV_NAME);
280 if (!memregion) {
281 dev_err(&pdev->dev, "Memory region already claimed\n");
282 ret = -EBUSY;
283 goto err_clk_put;
284 }
285
286 spdif->regs = ioremap(mem->start, resource_size(mem));
287 if (!spdif->regs) {
288 dev_err(&pdev->dev, "ioremap failed\n");
289 ret = -ENOMEM;
290 goto err_release;
291 }
292
293 spdif->playback_dma_data.addr = mem->start + TEGRA_SPDIF_DATA_OUT;
294 spdif->playback_dma_data.wrap = 4;
295 spdif->playback_dma_data.width = 32;
296 spdif->playback_dma_data.req_sel = dmareq->start;
297
298 ret = snd_soc_register_dai(&pdev->dev, &tegra_spdif_dai);
299 if (ret) {
300 dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
301 ret = -ENOMEM;
302 goto err_unmap;
303 }
304
Stephen Warren518de862012-03-20 14:55:49 -0600305 ret = tegra_pcm_platform_register(&pdev->dev);
306 if (ret) {
307 dev_err(&pdev->dev, "Could not register PCM: %d\n", ret);
308 goto err_unregister_dai;
309 }
310
Stephen Warren774fec32011-07-05 10:55:27 -0600311 tegra_spdif_debug_add(spdif);
312
313 return 0;
314
Stephen Warren518de862012-03-20 14:55:49 -0600315err_unregister_dai:
316 snd_soc_unregister_dai(&pdev->dev);
Stephen Warren774fec32011-07-05 10:55:27 -0600317err_unmap:
318 iounmap(spdif->regs);
319err_release:
320 release_mem_region(mem->start, resource_size(mem));
321err_clk_put:
322 clk_put(spdif->clk_spdif_out);
323err_free:
324 kfree(spdif);
325exit:
326 return ret;
327}
328
329static int __devexit tegra_spdif_platform_remove(struct platform_device *pdev)
330{
331 struct tegra_spdif *spdif = dev_get_drvdata(&pdev->dev);
332 struct resource *res;
333
Stephen Warren518de862012-03-20 14:55:49 -0600334 tegra_pcm_platform_unregister(&pdev->dev);
Stephen Warren774fec32011-07-05 10:55:27 -0600335 snd_soc_unregister_dai(&pdev->dev);
336
337 tegra_spdif_debug_remove(spdif);
338
339 iounmap(spdif->regs);
340
341 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
342 release_mem_region(res->start, resource_size(res));
343
344 clk_put(spdif->clk_spdif_out);
345
346 kfree(spdif);
347
348 return 0;
349}
350
351static struct platform_driver tegra_spdif_driver = {
352 .driver = {
353 .name = DRV_NAME,
354 .owner = THIS_MODULE,
355 },
356 .probe = tegra_spdif_platform_probe,
357 .remove = __devexit_p(tegra_spdif_platform_remove),
358};
359
Axel Linaf3c2622011-11-28 18:55:03 +0800360module_platform_driver(tegra_spdif_driver);
Stephen Warren774fec32011-07-05 10:55:27 -0600361
362MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
363MODULE_DESCRIPTION("Tegra SPDIF ASoC driver");
364MODULE_LICENSE("GPL");
365MODULE_ALIAS("platform:" DRV_NAME);