1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/*
* tegra_spdif.c -- ALSA Soc Audio Layer
*
* Copyright (c) 2011, NVIDIA Corporation.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "tegra_soc.h"
/* spdif controller */
struct tegra_spdif_info {
struct platform_device *pdev;
struct tegra_audio_platform_data *pdata;
struct clk *spdif_clk;
unsigned long spdif_phys;
unsigned long spdif_base;
unsigned long dma_req_sel;
int irq;
int ref_count;
struct spdif_regs_cache spdif_regs;
};
void setup_spdif_dma_request(struct snd_pcm_substream *substream,
struct tegra_dma_req *req,
void (*dma_callback)(struct tegra_dma_req *req),
void *dma_data)
{
struct snd_soc_pcm_runtime *rtd = substream->private_data;
struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
struct tegra_spdif_info *info = cpu_dai->private_data;
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
req->to_memory = false;
req->dest_addr = spdif_get_fifo_phy_base(info->spdif_phys,
AUDIO_TX_MODE);
req->dest_wrap = 4;
req->source_wrap = 0;
req->dest_bus_width = 32;
req->source_bus_width = 32;
} else {
req->to_memory = true;
req->dest_addr = spdif_get_fifo_phy_base(info->spdif_phys,
AUDIO_RX_MODE);
req->dest_wrap = 0;
req->source_wrap = 4;
req->dest_bus_width = 32;
req->source_bus_width = 32;
}
req->complete = dma_callback;
req->dev = dma_data;
req->req_sel = info->dma_req_sel;
return;
}
/* playback */
static inline void start_spdif_playback(struct snd_soc_dai *dai)
{
struct tegra_spdif_info *info = dai->private_data;
spdif_fifo_set_attention_level(info->spdif_base, AUDIO_TX_MODE,
SPDIF_FIFO_ATN_LVL_FOUR_SLOTS);
spdif_fifo_enable(info->spdif_base, AUDIO_TX_MODE, true);
}
static inline void stop_spdif_playback(struct snd_soc_dai *dai)
{
struct tegra_spdif_info *info = dai->private_data;
spdif_fifo_enable(info->spdif_base, AUDIO_TX_MODE, false);
while (spdif_get_status(info->spdif_base) & SPDIF_STATUS_0_TX_BSY);
}
/* capture */
static inline void start_spdif_capture(struct snd_soc_dai *dai)
{
struct tegra_spdif_info *info = dai->private_data;
spdif_fifo_set_attention_level(info->spdif_base, AUDIO_RX_MODE,
SPDIF_FIFO_ATN_LVL_FOUR_SLOTS);
spdif_fifo_enable(info->spdif_base, AUDIO_RX_MODE, true);
}
static inline void stop_spdif_capture(struct snd_soc_dai *dai)
{
struct tegra_spdif_info *info = dai->private_data;
spdif_fifo_enable(info->spdif_base, AUDIO_RX_MODE, false);
while (spdif_get_status(info->spdif_base) & SPDIF_STATUS_0_RX_BSY);
}
static int tegra_spdif_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params,
struct snd_soc_dai *dai)
{
struct tegra_spdif_info *info = dai->private_data;
int ret = 0;
int val;
switch (params_format(params)) {
case SNDRV_PCM_FORMAT_S16_LE:
val = SPDIF_BIT_MODE_MODE16BIT;
break;
case SNDRV_PCM_FORMAT_S24_LE:
val = SPDIF_BIT_MODE_MODE24BIT;
break;
case SNDRV_PCM_FORMAT_S32_LE:
val = SPDIF_BIT_MODE_MODERAW;
break;
default:
ret =-EINVAL;
goto err;
}
spdif_set_bit_mode(info->spdif_base, val);
spdif_set_fifo_packed(info->spdif_base, 1);
switch (params_rate(params)) {
case 8000:
case 32000:
case 44100:
case 48000:
case 88200:
case 96000:
val = params_rate(params);
break;
default:
ret = -EINVAL;
goto err;
}
spdif_set_sample_rate(info->spdif_base, val);
return 0;
err:
return ret;
}
static int tegra_spdif_set_dai_fmt(struct snd_soc_dai *cpu_dai,
unsigned int fmt)
{
return 0;
}
static int tegra_spdif_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
int clk_id, unsigned int freq, int dir)
{
struct tegra_spdif_info* info = cpu_dai->private_data;
struct tegra_audio_platform_data *pdata = info->pdev->dev.platform_data;
if (info && info->spdif_clk) {
clk_set_rate(info->spdif_clk, pdata->spdif_clk_rate);
}
else {
pr_err("%s: could not get spdif clock\n", __func__);
return -EIO;
}
return 0;
}
static int tegra_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
struct snd_soc_dai *dai)
{
int ret = 0;
switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
case SNDRV_PCM_TRIGGER_RESUME:
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
start_spdif_playback(dai);
else
start_spdif_capture(dai);
break;
case SNDRV_PCM_TRIGGER_STOP:
case SNDRV_PCM_TRIGGER_SUSPEND:
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
stop_spdif_playback(dai);
else
stop_spdif_capture(dai);
break;
default:
ret = -EINVAL;
}
return ret;
}
#ifdef CONFIG_PM
int tegra_spdif_suspend(struct snd_soc_dai *cpu_dai)
{
struct tegra_spdif_info* info = cpu_dai->private_data;
spdif_get_all_regs(info->spdif_base, &info->spdif_regs);
return 0;
}
int tegra_spdif_resume(struct snd_soc_dai *cpu_dai)
{
struct tegra_spdif_info* info = cpu_dai->private_data;
spdif_set_all_regs(info->spdif_base, &info->spdif_regs);
return 0;
}
#else
#define tegra_spdif_suspend NULL
#define tegra_spdif_resume NULL
#endif
static int tegra_spdif_startup(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
struct tegra_spdif_info *info = dai->private_data;
if (!info->ref_count)
clk_enable(info->spdif_clk);
info->ref_count++;
return 0;
}
static void tegra_spdif_shutdown(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
struct tegra_spdif_info *info = dai->private_data;
if (info->ref_count > 0)
info->ref_count--;
if (!info->ref_count)
clk_disable(info->spdif_clk);
return;
}
static int tegra_spdif_probe(struct platform_device *pdev,
struct snd_soc_dai *dai)
{
return 0;
}
static struct snd_soc_dai_ops tegra_spdif_dai_ops = {
.startup = tegra_spdif_startup,
.shutdown = tegra_spdif_shutdown,
.trigger = tegra_spdif_trigger,
.hw_params = tegra_spdif_hw_params,
.set_fmt = tegra_spdif_set_dai_fmt,
.set_sysclk = tegra_spdif_set_dai_sysclk,
};
struct snd_soc_dai tegra_spdif_dai = {
.name = "tegra-spdif",
.id = 0,
.probe = tegra_spdif_probe,
.suspend = tegra_spdif_suspend,
.resume = tegra_spdif_resume,
.playback = {
.channels_min = 2,
.channels_max = 2,
.rates = TEGRA_SAMPLE_RATES,
.formats = SNDRV_PCM_FMTBIT_S16_LE,
},
.capture = {
.channels_min = 2,
.channels_max = 2,
.rates = TEGRA_SAMPLE_RATES,
.formats = SNDRV_PCM_FMTBIT_S16_LE,
},
.ops = &tegra_spdif_dai_ops,
};
EXPORT_SYMBOL_GPL(tegra_spdif_dai);
static int tegra_spdif_driver_probe(struct platform_device *pdev)
{
int err = 0;
struct resource *res, *mem;
struct tegra_spdif_info *info;
pr_info("%s\n", __func__);
info = kzalloc(sizeof(*info), GFP_KERNEL);
if (!info)
return -ENOMEM;
info->pdev = pdev;
info->pdata = pdev->dev.platform_data;
info->pdata->driver_data = info;
BUG_ON(!info->pdata);
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res) {
dev_err(&pdev->dev, "no mem resource!\n");
err = -ENODEV;
goto fail;
}
mem = request_mem_region(res->start, resource_size(res), pdev->name);
if (!mem) {
dev_err(&pdev->dev, "memory region already claimed!\n");
err = -EBUSY;
goto fail;
}
info->spdif_phys = res->start;
info->spdif_base = (unsigned long)ioremap(res->start, res->end - res->start + 1);
if (!info->spdif_base) {
dev_err(&pdev->dev, "cannot remap iomem!\n");
err = -ENOMEM;
goto fail_release_mem;
}
res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
if (!res) {
dev_err(&pdev->dev, "no dma resource!\n");
err = -ENODEV;
goto fail_unmap_mem;
}
info->dma_req_sel = res->start;
res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
if (!res) {
dev_err(&pdev->dev, "no irq resource!\n");
err = -ENODEV;
goto fail_unmap_mem;
}
info->irq = res->start;
info->spdif_clk = clk_get(&pdev->dev, NULL);
if (IS_ERR(info->spdif_clk)) {
err = PTR_ERR(info->spdif_clk);
goto fail_unmap_mem;
}
clk_set_rate(info->spdif_clk, info->pdata->spdif_clk_rate);
spdif_initialize(info->spdif_base, AUDIO_TX_MODE);
spdif_initialize(info->spdif_base, AUDIO_RX_MODE);
tegra_spdif_dai.dev = &pdev->dev;
tegra_spdif_dai.private_data = info;
err = snd_soc_register_dai(&tegra_spdif_dai);
if (err)
goto fail_unmap_mem;
return 0;
fail_unmap_mem:
iounmap((void __iomem*)info->spdif_base);
fail_release_mem:
release_mem_region(mem->start, resource_size(mem));
fail:
kfree(info);
return err;
}
static int __devexit tegra_spdif_driver_remove(struct platform_device *pdev)
{
struct tegra_spdif_info *info = tegra_spdif_dai.private_data;
if (info->spdif_base)
iounmap((void __iomem*)info->spdif_base);
if (info)
kfree(info);
snd_soc_unregister_dai(&tegra_spdif_dai);
return 0;
}
static struct platform_driver tegra_spdif_driver = {
.probe = tegra_spdif_driver_probe,
.remove = __devexit_p(tegra_spdif_driver_remove),
.driver = {
.name = "spdif_out",
.owner = THIS_MODULE,
},
};
static int __init tegra_spdif_init(void)
{
int ret = 0;
ret = platform_driver_register(&tegra_spdif_driver);
return ret;
}
module_init(tegra_spdif_init);
static void __exit tegra_spdif_exit(void)
{
platform_driver_unregister(&tegra_spdif_driver);
}
module_exit(tegra_spdif_exit);
/* Module information */
MODULE_DESCRIPTION("Tegra SPDIF SoC interface");
MODULE_LICENSE("GPL");
|