summaryrefslogtreecommitdiff
path: root/sound/soc/tegra/tegra_i2s.c
blob: 83dc452f000cd1134a0fef64d2fdd52f96cd9eea (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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
 * tegra_i2s.c  --  ALSA Soc Audio Layer
 *
 * Copyright (c) 2009-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
 * version 2 as published by the Free Software Foundation.
 *
 * 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 St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include "tegra_soc.h"

/* i2s controller */
struct tegra_i2s_info {
	struct platform_device *pdev;
	struct tegra_audio_platform_data *pdata;

	unsigned int bit_format;
	bool i2s_master;
	int ref_count;
	struct das_regs_cache das_regs;
};

void free_i2s_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;

	int fifo_mode = AUDIO_RX_MODE;

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

	i2s_free_dma_requestor(cpu_dai->id, fifo_mode);
}

void setup_i2s_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_i2s_info *info = cpu_dai->private_data;

	int fifo_mode = AUDIO_RX_MODE;

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

	req->req_sel = i2s_get_dma_requestor(cpu_dai->id, fifo_mode);

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		req->to_memory = false;
		req->dest_addr =
			i2s_get_fifo_phy_base(cpu_dai->id, fifo_mode);
		req->dest_wrap = 4;
		req->source_wrap = 0;
		if (info->bit_format == AUDIO_FRAME_FORMAT_DSP)
			req->dest_bus_width = info->pdata->dsp_bus_width;
		else
			req->dest_bus_width = info->pdata->i2s_bus_width;
		req->source_bus_width = 32;
	} else {
		req->to_memory = true;
		req->source_addr =
			i2s_get_fifo_phy_base(cpu_dai->id, fifo_mode);
		req->dest_wrap = 0;
		req->source_wrap = 4;
		if (info->bit_format == AUDIO_FRAME_FORMAT_DSP)
			req->source_bus_width = info->pdata->dsp_bus_width;
		else
			req->source_bus_width = info->pdata->i2s_bus_width;
		req->dest_bus_width = 32;
	}

	req->complete = dma_callback;
	req->dev = dma_data;

	return;
}

void set_i2s_fifo_attention(struct snd_pcm_substream *substream,
			int buffersize)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;

	i2s_set_fifo_attention(cpu_dai->id,
		buffersize,
		(substream->stream == SNDRV_PCM_STREAM_PLAYBACK)?
		AUDIO_TX_MODE: AUDIO_RX_MODE);
}

/* playback */
static inline void start_i2s_playback(struct snd_soc_dai *cpu_dai)
{
	i2s_fifo_enable(cpu_dai->id, AUDIO_TX_MODE, 1);
}

static inline void stop_i2s_playback(struct snd_soc_dai *cpu_dai)
{
	i2s_set_fifo_irq_on_err(cpu_dai->id, AUDIO_TX_MODE, 0);
	i2s_set_fifo_irq_on_qe(cpu_dai->id, AUDIO_TX_MODE, 0);
	i2s_fifo_enable(cpu_dai->id, AUDIO_TX_MODE, 0);
	while (i2s_get_status(cpu_dai->id, AUDIO_TX_MODE));
}

/* recording */
static inline void start_i2s_capture(struct snd_soc_dai *cpu_dai)
{
	i2s_fifo_enable(cpu_dai->id, AUDIO_RX_MODE, 1);
}

static inline void stop_i2s_capture(struct snd_soc_dai *cpu_dai)
{
	i2s_set_fifo_irq_on_err(cpu_dai->id, AUDIO_RX_MODE, 0);
	i2s_set_fifo_irq_on_qe(cpu_dai->id, AUDIO_RX_MODE, 0);
	i2s_fifo_enable(cpu_dai->id, AUDIO_RX_MODE, 0);
	while (i2s_get_status(cpu_dai->id, AUDIO_RX_MODE));
}


static int tegra_i2s_hw_params(struct snd_pcm_substream *substream,
				struct snd_pcm_hw_params *params,
				struct snd_soc_dai *dai)
{
	unsigned int i2s_id = dai->id;
	int val;

	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
		val = AUDIO_BIT_SIZE_16;
		break;
	case SNDRV_PCM_FORMAT_S24_LE:
		val = AUDIO_BIT_SIZE_24;
		break;
	case SNDRV_PCM_FORMAT_S32_LE:
		val = AUDIO_BIT_SIZE_32;
		break;
	default:
		return -EINVAL;
	}
	i2s_set_bit_size(i2s_id, 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;
	}

	i2s_set_samplerate(i2s_id, val);

	switch (params_channels(params)) {
	case 1: val = AUDIO_CHANNEL_1; break;
	case 2: val = AUDIO_CHANNEL_2; break;
	case 3: val = AUDIO_CHANNEL_3; break;
	case 4: val = AUDIO_CHANNEL_4; break;
	case 5: val = AUDIO_CHANNEL_5; break;
	case 6: val = AUDIO_CHANNEL_6; break;
	case 7: val = AUDIO_CHANNEL_7; break;
	case 8: val = AUDIO_CHANNEL_8; break;
	default:
		return -EINVAL;
	}

	i2s_set_channels(i2s_id, val);

	return 0;
}


static int tegra_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
					unsigned int fmt)
{
	struct tegra_i2s_info *info = cpu_dai->private_data;
	unsigned int i2s_id = cpu_dai->id;
	int val1, val2;

	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBS_CFS:
		info->i2s_master = 1;
		break;
	case SND_SOC_DAIFMT_CBM_CFM:
		info->i2s_master = 0;
		break;
	case SND_SOC_DAIFMT_CBS_CFM:
	case SND_SOC_DAIFMT_CBM_CFS:
		/* Tegra does not support different combinations of
		 * master and slave for FSYNC and BCLK */
	default:
		return -EINVAL;
	}
	i2s_set_master(i2s_id, info->i2s_master);

	val2 = AUDIO_LRCK_LEFT_LOW;

	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_DSP_A:
		val1 = AUDIO_FRAME_FORMAT_DSP;
		val2 = AUDIO_LRCK_RIGHT_LOW;
		break;
	case SND_SOC_DAIFMT_DSP_B:
		val1 = AUDIO_FRAME_FORMAT_DSP;
		val2 = AUDIO_LRCK_RIGHT_LOW;
		break;
	case SND_SOC_DAIFMT_I2S:
		val1 = AUDIO_FRAME_FORMAT_I2S;
		break;
	case SND_SOC_DAIFMT_RIGHT_J:
		val1 = AUDIO_FRAME_FORMAT_RJM;
		break;
	case SND_SOC_DAIFMT_LEFT_J:
		val1 = AUDIO_FRAME_FORMAT_LJM;
		break;
	default:
		return -EINVAL;
	}

	i2s_set_bit_format(i2s_id, val1);
	i2s_set_left_right_control_polarity(i2s_id, val2);

	return 0;
}

static int tegra_i2s_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
					int clk_id, unsigned int freq, int dir)
{
	return 0;
}

static int tegra_i2s_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_i2s_playback(dai);
		else
			start_i2s_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_i2s_playback(dai);
		else
			stop_i2s_capture(dai);
		break;
	default:
		ret = -EINVAL;
	}

	return ret;
}

static int i2s_configure(struct tegra_i2s_info *info )
{
	struct platform_device *pdev = info->pdev;
	struct tegra_audio_platform_data *pdata = pdev->dev.platform_data;
	struct tegra_i2s_property i2sprop;

	memset(&i2sprop, 0, sizeof(i2sprop));

	i2sprop.master_mode = pdata->i2s_master;
	i2sprop.audio_mode = pdata->mode;
	i2sprop.bit_size = pdata->bit_size;
	i2sprop.clk_rate = pdata->dev_clk_rate;
	i2sprop.sample_rate = pdata->i2s_master_clk;
	i2sprop.fifo_fmt = pdata->fifo_fmt;

	i2s_init(pdev->id, &i2sprop);

	return 0;
}

#ifdef CONFIG_PM
int tegra_i2s_suspend(struct snd_soc_dai *cpu_dai)
{
	struct tegra_i2s_info *info = cpu_dai->private_data;

	i2s_suspend(cpu_dai->id);
	tegra_das_get_all_regs(&info->das_regs);


	return 0;
}

int tegra_i2s_resume(struct snd_soc_dai *cpu_dai)
{
	struct tegra_i2s_info *info = cpu_dai->private_data;


	tegra_das_set_all_regs(&info->das_regs);
	i2s_resume(cpu_dai->id);
	tegra_jack_resume();


	/* disabled clock as it is being enabled back on startup */
	i2s_clock_disable(cpu_dai->id);
	return 0;
}

#else
#define tegra_i2s_suspend	NULL
#define tegra_i2s_resume	NULL
#endif

static int tegra_i2s_startup(struct snd_pcm_substream *substream,
				struct snd_soc_dai *dai)
{
	struct tegra_i2s_info *info = dai->private_data;

	if (!info->ref_count)
		i2s_clock_enable(dai->id);

	info->ref_count++;
	return 0;
}

static void tegra_i2s_shutdown(struct snd_pcm_substream *substream,
				struct snd_soc_dai *dai)
{
	struct tegra_i2s_info *info = dai->private_data;

	if (info->ref_count > 0)
	    info->ref_count--;

	if (!info->ref_count)
		i2s_clock_disable(dai->id);

	return;
}

static int tegra_i2s_probe(struct platform_device *pdev,
				struct snd_soc_dai *cpu_dai)
{
	return 0;
}

static struct snd_soc_dai_ops tegra_i2s_dai_ops = {
	.startup	= tegra_i2s_startup,
	.shutdown	= tegra_i2s_shutdown,
	.trigger	= tegra_i2s_trigger,
	.hw_params	= tegra_i2s_hw_params,
	.set_fmt	= tegra_i2s_set_dai_fmt,
	.set_sysclk	= tegra_i2s_set_dai_sysclk,
};

#define TEGRA_I2S_CREATE_DAI(link_id, ch_min, ch_max, sample_rates)	\
{							\
	.name = "tegra-i2s-"#link_id,			\
	.id = (link_id),				\
	.probe = tegra_i2s_probe,			\
	.suspend = tegra_i2s_suspend,			\
	.resume = tegra_i2s_resume,			\
	.playback = {					\
		.channels_min = (ch_min),		\
		.channels_max = (ch_max),		\
		.rates = (sample_rates),		\
		.formats = SNDRV_PCM_FMTBIT_S16_LE,	\
	},						\
	.capture = {					\
		.channels_min = (ch_min),		\
		.channels_max = (ch_max),		\
		.rates = (sample_rates),		\
		.formats = SNDRV_PCM_FMTBIT_S16_LE,	\
	},						\
	.ops = &tegra_i2s_dai_ops,			\
}

struct snd_soc_dai tegra_i2s_dai[] = {
#if defined(CONFIG_ARCH_TEGRA_2x_SOC)
	TEGRA_I2S_CREATE_DAI(0, 1, 2, TEGRA_SAMPLE_RATES),
	TEGRA_I2S_CREATE_DAI(1, 1, 2, TEGRA_SAMPLE_RATES),
#else
	TEGRA_I2S_CREATE_DAI(1, 2, 2, TEGRA_SAMPLE_RATES),
	TEGRA_I2S_CREATE_DAI(2, 1, 2, TEGRA_VOICE_SAMPLE_RATES),
	TEGRA_I2S_CREATE_DAI(3, 1, 2, TEGRA_VOICE_SAMPLE_RATES),
#endif
};

EXPORT_SYMBOL_GPL(tegra_i2s_dai);

static int tegra_i2s_driver_probe(struct platform_device *pdev)
{
	int err = 0;
	struct tegra_i2s_info *info;
	int i = 0;

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

	err = i2s_configure(info);
	if (err) {
		goto fail_clock;
	}

	info->bit_format = info->pdata->mode;


	for (i = 0; i < ARRAY_SIZE(tegra_i2s_dai); i++) {
		if (tegra_i2s_dai[i].id == pdev->id) {
			tegra_i2s_dai[i].dev = &pdev->dev;
			tegra_i2s_dai[i].private_data = info;
			err = snd_soc_register_dai(&tegra_i2s_dai[i]);
			if (err)
				goto fail_clock;
		}
	}

	return 0;

fail_clock:
	i2s_close(pdev->id);
	kfree(info);
	return err;
}


static int __devexit tegra_i2s_driver_remove(struct platform_device *pdev)
{
	struct tegra_i2s_info *info = tegra_i2s_dai[pdev->id].private_data;

	if (info)
		kfree(info);

	snd_soc_unregister_dai(&tegra_i2s_dai[pdev->id]);
	return 0;
}

static struct platform_driver tegra_i2s_driver = {
	.probe = tegra_i2s_driver_probe,
	.remove = __devexit_p(tegra_i2s_driver_remove),
	.driver = {
#if defined(CONFIG_ARCH_TEGRA_2x_SOC)
		.name = "i2s",
#else
		.name = "audio",
#endif
		.owner = THIS_MODULE,
	},
};

static int __init tegra_i2s_init(void)
{
	int ret = 0;

	ret = platform_driver_register(&tegra_i2s_driver);
	return ret;
}
module_init(tegra_i2s_init);

static void __exit tegra_i2s_exit(void)
{
	platform_driver_unregister(&tegra_i2s_driver);
}
module_exit(tegra_i2s_exit);

/* Module information */
MODULE_DESCRIPTION("Tegra I2S SoC interface");
MODULE_LICENSE("GPL");