summaryrefslogtreecommitdiff
path: root/sound/soc/tegra/tegra_spdif.c
blob: 10a8abe630dc445523b24fd3fc79ada6eca89d17 (plain)
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
417
/*
 * tegra_spdif.c  --  ALSA Soc Audio Layer
 *
 * Copyright (c) 2010-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"
#include <mach/spdif.h>

/* spdif controller */
struct tegra_spdif_info {
	struct platform_device *pdev;
	struct tegra_audio_platform_data *pdata;
	unsigned long spdif_phys;
	unsigned long spdif_base;
	aud_dev_info  spdev_info;
};

void free_spdif_dma_request(struct snd_pcm_substream *substream)
{
	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;

	info->spdev_info.fifo_mode = AUDIO_RX_MODE;

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		info->spdev_info.fifo_mode = AUDIO_TX_MODE;

	am_free_dma_requestor(&info->spdev_info);
}

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;

	info->spdev_info.fifo_mode = AUDIO_RX_MODE;

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		info->spdev_info.fifo_mode = AUDIO_TX_MODE;

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		req->to_memory = false;
		req->dest_addr = am_get_fifo_phy_base(&info->spdev_info);
		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 = am_get_fifo_phy_base(&info->spdev_info);
		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 = am_get_dma_requestor(&info->spdev_info);

	return;
}

/* playback */
static inline void start_spdif_playback(struct snd_soc_dai *dai)
{
	struct tegra_spdif_info *info = dai->private_data;

	info->spdev_info.fifo_mode = AUDIO_TX_MODE;

	am_set_stream_state(&info->spdev_info, true);
}

static inline void stop_spdif_playback(struct snd_soc_dai *dai)
{
	struct tegra_spdif_info *info = dai->private_data;

	info->spdev_info.fifo_mode = AUDIO_TX_MODE;

	am_set_stream_state(&info->spdev_info, false);
	while (am_get_status(&info->spdev_info) & 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;

	info->spdev_info.fifo_mode = AUDIO_RX_MODE;
	am_set_stream_state(&info->spdev_info, true);
}

static inline void stop_spdif_capture(struct snd_soc_dai *dai)
{
	struct tegra_spdif_info *info = dai->private_data;

	info->spdev_info.fifo_mode = AUDIO_RX_MODE;
	am_set_stream_state(&info->spdev_info, false);
	while (am_get_status(&info->spdev_info) & 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)
{
	int val;
	am_stream_format_info fmt;
	struct tegra_spdif_info *info = dai->private_data;

	info->spdev_info.fifo_mode = AUDIO_RX_MODE;

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		info->spdev_info.fifo_mode = AUDIO_TX_MODE;

	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:
		return -EINVAL;
	}

	fmt.bitsize = val;

	switch (params_rate(params)) {
	case 8000:
	case 32000:
	case 44100:
	case 48000:
	case 88200:
	case 96000:
		val = params_rate(params);
		break;
	default:
		return -EINVAL;
	}

	fmt.channels = val;
	fmt.buffersize = params_period_bytes(params);

	am_set_stream_format(&info->spdev_info, &fmt);

	return 0;
}


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)
{
	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;
	am_suspend(&info->spdev_info);
	return 0;
}

int tegra_spdif_resume(struct snd_soc_dai *cpu_dai)
{
	struct tegra_spdif_info *info = cpu_dai->private_data;
	am_resume(&info->spdev_info);

	/* disabled clock as startup code enable the clock */
	am_clock_disable(&info->spdev_info);
	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;

	info->spdev_info.fifo_mode = AUDIO_RX_MODE;

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
	info->spdev_info.fifo_mode = AUDIO_TX_MODE;

	am_clock_enable(&info->spdev_info);

	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;

	info->spdev_info.fifo_mode = AUDIO_RX_MODE;

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		info->spdev_info.fifo_mode = AUDIO_TX_MODE;

	am_clock_disable(&info->spdev_info);

	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;
	am_dev_format_info dev_fmt;

	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;
	}

	memset(&dev_fmt, 0, sizeof(dev_fmt));
	dev_fmt.clkrate = info->pdata->dev_clk_rate;
	info->spdev_info.base = info->spdif_base;
	info->spdev_info.phy_base = info->spdif_phys;

	info->spdev_info.dev_type = AUDIO_SPDIF_DEVICE;
	info->spdev_info.dev_id = pdev->id;

	info->spdev_info.fifo_mode = AUDIO_RX_MODE;
	am_device_init(&info->spdev_info, (void *)&dev_fmt, 0);

	info->spdev_info.fifo_mode = AUDIO_TX_MODE;
	am_device_init(&info->spdev_info, (void *)&dev_fmt, 0);

	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");