summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/tegra2_i2s.c
blob: b129ad3146269b999b56f1f6aeaebcfb529c9afb (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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
 * arch/arm/mach-tegra/tegra2_i2s.c
 *
 * Copyright (C) 2010 Google, Inc.
 *
 * Author:
 *      Iliyan Malchev <malchev@google.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 *
 */

#include <linux/kernel.h>
#include <linux/err.h>

#include "clock.h"
#include <asm/io.h>
#include <mach/iomap.h>
#include <mach/audio.h>
#include <mach/tegra2_i2s.h>


#define NR_I2S_IFC	2

#define check_ifc(n, ...) if ((n) > NR_I2S_IFC) {			\
	pr_err("%s: invalid i2s interface %d\n", __func__, (n));	\
	return __VA_ARGS__;						\
}

static phys_addr_t i2s_phy_base[NR_I2S_IFC] = {
	TEGRA_I2S1_BASE,
	TEGRA_I2S2_BASE,
};

static void *i2s_base[NR_I2S_IFC] = {
	IO_ADDRESS(TEGRA_I2S1_BASE),
	IO_ADDRESS(TEGRA_I2S2_BASE),
};

static inline void i2s_writel(int ifc, u32 val, u32 reg)
{
	__raw_writel(val, i2s_base[ifc] + reg);
}

static inline u32 i2s_readl(int ifc, u32 reg)
{
	return __raw_readl(i2s_base[ifc] + reg);
}

void i2s_dump_registers(int ifc)
{
	check_ifc(ifc);

	pr_info("%s: CTRL   %08x\n", __func__,
			i2s_readl(ifc, I2S_I2S_CTRL_0));
	pr_info("%s: STATUS %08x\n", __func__,
			i2s_readl(ifc, I2S_I2S_STATUS_0));
	pr_info("%s: TIMING %08x\n", __func__,
			i2s_readl(ifc, I2S_I2S_TIMING_0));
	pr_info("%s: SCR    %08x\n", __func__,
			i2s_readl(ifc, I2S_I2S_FIFO_SCR_0));
	pr_info("%s: FIFO1  %08x\n", __func__,
			i2s_readl(ifc, I2S_I2S_FIFO1_0));
	pr_info("%s: FIFO2  %08x\n", __func__,
			i2s_readl(ifc, I2S_I2S_FIFO1_0));
}

void i2s_get_all_regs(int ifc, struct i2s_runtime_data* ird)
{
	check_ifc(ifc);
	ird->i2s_ctrl_0 = i2s_readl(ifc, I2S_I2S_CTRL_0);
	ird->i2s_status_0 = i2s_readl(ifc, I2S_I2S_STATUS_0);
	ird->i2s_timing_0 = i2s_readl(ifc, I2S_I2S_TIMING_0);
	ird->i2s__fifo_scr_0 = i2s_readl(ifc, I2S_I2S_FIFO_SCR_0);
	ird->i2s_pcm_ctrl_0 = i2s_readl(ifc, I2S_I2S_PCM_CTRL_0);
	ird->i2s_nw_ctrl_0 = i2s_readl(ifc, I2S_I2S_NW_CTRL_0);
	ird->i2s_tdm_ctrl_0 = i2s_readl(ifc, I2S_I2S_TDM_CTRL_0);
	ird->i2s_tdm_tx_rx_ctrl_0 = i2s_readl(ifc, I2S_I2S_TDM_TX_RX_CTRL_0);
	ird->i2s_fifo1_0 = i2s_readl(ifc, I2S_I2S_FIFO1_0);
	ird->i2s_fifo2_0 = i2s_readl(ifc, I2S_I2S_FIFO2_0);
}

void i2s_set_all_regs(int ifc, struct i2s_runtime_data* ird)
{
	check_ifc(ifc);
	i2s_writel(ifc, ird->i2s_ctrl_0, I2S_I2S_CTRL_0);
	i2s_writel(ifc, ird->i2s_status_0, I2S_I2S_STATUS_0);
	i2s_writel(ifc, ird->i2s_timing_0, I2S_I2S_TIMING_0);
	i2s_writel(ifc, ird->i2s__fifo_scr_0, I2S_I2S_FIFO_SCR_0);
	i2s_writel(ifc, ird->i2s_pcm_ctrl_0, I2S_I2S_PCM_CTRL_0);
	i2s_writel(ifc, ird->i2s_nw_ctrl_0, I2S_I2S_NW_CTRL_0);
	i2s_writel(ifc, ird->i2s_tdm_ctrl_0, I2S_I2S_TDM_CTRL_0);
	i2s_writel(ifc, ird->i2s_tdm_tx_rx_ctrl_0, I2S_I2S_TDM_TX_RX_CTRL_0);
	i2s_writel(ifc, ird->i2s_fifo1_0, I2S_I2S_FIFO1_0);
	i2s_writel(ifc, ird->i2s_fifo2_0, I2S_I2S_FIFO2_0);
}

int i2s_set_channel_bit_count(int ifc, int sampling, int bitclk)
{
	u32 val;
	int bitcnt;

	check_ifc(ifc, -EINVAL);

	bitcnt = bitclk / (2 * sampling) - 1;

	if (bitcnt < 0 || bitcnt >= 1<<11) {
		pr_err("%s: bit count %d is out of bounds\n", __func__,
			bitcnt);
		return -EINVAL;
	}

	val = bitcnt;
	if (bitclk % (2 * sampling)) {
		pr_info("%s: enabling non-symmetric mode\n", __func__);
		val |= I2S_I2S_TIMING_NON_SYM_ENABLE;
	}

	i2s_writel(ifc, val, I2S_I2S_TIMING_0);
	return 0;
}

void i2s_set_fifo_mode(int ifc, int fifo, int tx)
{
	u32 val;

	check_ifc(ifc);

	val = i2s_readl(ifc, I2S_I2S_CTRL_0);
	if (fifo == 0) {
		val &= ~I2S_I2S_CTRL_FIFO1_RX_ENABLE;
		val |= (!tx) ? I2S_I2S_CTRL_FIFO1_RX_ENABLE : 0;
	}
	else {
		val &= ~I2S_I2S_CTRL_FIFO2_TX_ENABLE;
		val |= tx ? I2S_I2S_CTRL_FIFO2_TX_ENABLE : 0;
	}
	i2s_writel(ifc, val, I2S_I2S_CTRL_0);
}

void i2s_set_loopback(int ifc, int on)
{
	u32 val;

	check_ifc(ifc);

	val = i2s_readl(ifc, I2S_I2S_CTRL_0);
	val &= ~I2S_I2S_CTRL_FIFO_LPBK_ENABLE;
	val |= on ? I2S_I2S_CTRL_FIFO_LPBK_ENABLE : 0;

	i2s_writel(ifc, val, I2S_I2S_CTRL_0);
}

int i2s_fifo_set_attention_level(int ifc, int fifo, unsigned level)
{
	u32 val;

	check_ifc(ifc, -EINVAL);

	if (level > I2S_FIFO_ATN_LVL_TWELVE_SLOTS) {
		pr_err("%s: invalid fifo level selector %d\n", __func__,
			level);
		return -EINVAL;
	}

	val = i2s_readl(ifc, I2S_I2S_FIFO_SCR_0);

	if (!fifo) {
		val &= ~I2S_I2S_FIFO_SCR_FIFO1_ATN_LVL_MASK;
		val |= level << I2S_FIFO1_ATN_LVL_SHIFT;
	}
	else {
		val &= ~I2S_I2S_FIFO_SCR_FIFO2_ATN_LVL_MASK;
		val |= level << I2S_FIFO2_ATN_LVL_SHIFT;
	}

	i2s_writel(ifc, val, I2S_I2S_FIFO_SCR_0);
	return 0;
}

void i2s_fifo_enable(int ifc, int fifo, int on)
{
	u32 val;

	check_ifc(ifc);

	val = i2s_readl(ifc, I2S_I2S_CTRL_0);
	if (!fifo) {
		val &= ~I2S_I2S_CTRL_FIFO1_ENABLE;
		val |= on ? I2S_I2S_CTRL_FIFO1_ENABLE : 0;
	}
	else {
		val &= ~I2S_I2S_CTRL_FIFO2_ENABLE;
		val |= on ? I2S_I2S_CTRL_FIFO2_ENABLE : 0;
	}

	i2s_writel(ifc, val, I2S_I2S_CTRL_0);
}

void i2s_fifo_clear(int ifc, int fifo)
{
	u32 val;

	check_ifc(ifc);

	val = i2s_readl(ifc, I2S_I2S_FIFO_SCR_0);
	if (!fifo) {
		val &= ~I2S_I2S_FIFO_SCR_FIFO1_CLR;
		val |= I2S_I2S_FIFO_SCR_FIFO1_CLR;
	}
	else {
		val &= ~I2S_I2S_FIFO_SCR_FIFO2_CLR;
		val |= I2S_I2S_FIFO_SCR_FIFO2_CLR;
	}

	i2s_writel(ifc, val, I2S_I2S_FIFO_SCR_0);
}

void i2s_set_master(int ifc, int master)
{
	u32 val;
	check_ifc(ifc);
	val = i2s_readl(ifc, I2S_I2S_CTRL_0);
	val &= ~I2S_I2S_CTRL_MASTER_ENABLE;
	val |= master ? I2S_I2S_CTRL_MASTER_ENABLE : 0;
	i2s_writel(ifc, val, I2S_I2S_CTRL_0);
}

int i2s_set_bit_format(int ifc, unsigned fmt)
{
	u32 val;

	check_ifc(ifc, -EINVAL);

	if (fmt > I2S_BIT_FORMAT_DSP) {
		pr_err("%s: invalid bit-format selector %d\n", __func__, fmt);
		return -EINVAL;
	}

	val = i2s_readl(ifc, I2S_I2S_CTRL_0);
	val &= ~I2S_I2S_CTRL_BIT_FORMAT_MASK;
	val |= fmt << I2S_BIT_FORMAT_SHIFT;

	i2s_writel(ifc, val, I2S_I2S_CTRL_0);

	if (fmt == I2S_BIT_FORMAT_DSP) {
		i2s_enable_pcm_mode(ifc, 1);
	}
	else {
		i2s_enable_pcm_mode(ifc, 0);
	}
	return 0;
}

int i2s_set_bit_size(int ifc, unsigned bit_size)
{
	u32 val;

	check_ifc(ifc, -EINVAL);

	val = i2s_readl(ifc, I2S_I2S_CTRL_0);
	val &= ~I2S_I2S_CTRL_BIT_SIZE_MASK;

	if (bit_size > I2S_BIT_SIZE_32) {
		pr_err("%s: invalid bit_size selector %d\n", __func__,
			bit_size);
		return -EINVAL;
	}

	val |= bit_size << I2S_BIT_SIZE_SHIFT;

	i2s_writel(ifc, val, I2S_I2S_CTRL_0);
	return 0;
}

int i2s_set_fifo_format(int ifc, unsigned fmt)
{
	u32 val;

	check_ifc(ifc, -EINVAL);

	val = i2s_readl(ifc, I2S_I2S_CTRL_0);
	val &= ~I2S_I2S_CTRL_FIFO_FORMAT_MASK;

	if (fmt > I2S_FIFO_32 && fmt != I2S_FIFO_PACKED) {
		pr_err("%s: invalid fmt selector %d\n", __func__, fmt);
		return -EINVAL;
	}

	val |= fmt << I2S_FIFO_SHIFT;

	i2s_writel(ifc, val, I2S_I2S_CTRL_0);
	return 0;
}

void i2s_set_left_right_control_polarity(int ifc, int high_low)
{
	u32 val;

	check_ifc(ifc);

	val = i2s_readl(ifc, I2S_I2S_CTRL_0);
	val &= ~I2S_I2S_CTRL_L_R_CTRL;
	val |= high_low ? I2S_I2S_CTRL_L_R_CTRL : 0;
	i2s_writel(ifc, val, I2S_I2S_CTRL_0);
}

void i2s_enable_pcm_mode(int ifc, int on)
{
	u32 val;

	check_ifc(ifc);

	val = i2s_readl(ifc, I2S_I2S_PCM_CTRL_0);
	val &= ~(I2S_I2S_PCM_CTRL_TRM_MODE | I2S_I2S_PCM_CTRL_RCV_MODE);
	val |= on ? (I2S_I2S_PCM_CTRL_TRM_MODE | I2S_I2S_PCM_CTRL_RCV_MODE) : 0;
	i2s_writel(ifc, val, I2S_I2S_PCM_CTRL_0);
}

int i2s_set_pcm_edge_mode(int ifc, unsigned edge_mode)
{
	u32 val;

	check_ifc(ifc, -EINVAL);

	if (edge_mode > I2S_I2S_PCM_TRM_EDGE_NEG_EDGE_HIGHZ) {
		pr_err("%s: invalid dsp edge mode \n", __func__);
		return -EINVAL;
	}

	val = i2s_readl(ifc, I2S_I2S_PCM_CTRL_0);
	val &= ~I2S_I2S_PCM_TRM_EDGE_CTRL_MASK;
	val |= edge_mode << I2S_PCM_TRM_EDGE_CTRL_SHIFT;

	i2s_writel(ifc, val, I2S_I2S_PCM_CTRL_0);
	return 0;
}

int i2s_set_pcm_mask_bits(int ifc, unsigned mask_bits, int tx)
{
	u32 val;

	check_ifc(ifc, -EINVAL);

	val = i2s_readl(ifc, I2S_I2S_PCM_CTRL_0);
	if (tx) {
		if (mask_bits > I2S_I2S_PCM_TRM_MASK_BITS_SEVEN) {
			pr_err("%s: invalid dsp mask bits \n", __func__);
			return -EINVAL;
		}
		val &= ~I2S_I2S_PCM_TRM_MASK_BITS_MASK;
		val |= mask_bits << I2S_PCM_TRM_MASK_BITS_SHIFT;
	}
	else {
		if (mask_bits > I2S_I2S_PCM_RCV_MASK_BITS_SEVEN) {
			pr_err("%s: invalid dsp mask bits \n", __func__);
			return -EINVAL;
		}
		val &= ~I2S_I2S_PCM_RCV_MASK_BITS_MASK;
		val |= mask_bits << I2S_PCM_RCV_MASK_BITS_SHIFT;
	}
	i2s_writel(ifc, val, I2S_I2S_PCM_CTRL_0);
	return 0;
}

void i2s_set_pcm_fsync_width(int ifc, int fsync_long)
{
	u32 val;

	check_ifc(ifc);

	val = i2s_readl(ifc, I2S_I2S_PCM_CTRL_0);
	val &= ~I2S_I2S_PCM_CTRL_FSYNC_PCM_CTRL;
	val |= fsync_long ? I2S_I2S_PCM_CTRL_FSYNC_PCM_CTRL : 0;

	i2s_writel(ifc, val, I2S_I2S_PCM_CTRL_0);
}

void i2s_set_fifo_irq_on_err(int ifc, int fifo, int on)
{
	u32 val;

	check_ifc(ifc);

	val = i2s_readl(ifc, I2S_I2S_CTRL_0);
	if (!fifo) {
		val &= ~I2S_I2S_IE_FIFO1_ERR;
		val |= on ? I2S_I2S_IE_FIFO1_ERR : 0;
	}
	else {
		val &= ~I2S_I2S_IE_FIFO2_ERR;
		val |= on ? I2S_I2S_IE_FIFO2_ERR : 0;
	}
	i2s_writel(ifc, val, I2S_I2S_CTRL_0);
}

void i2s_set_fifo_irq_on_qe(int ifc, int fifo, int on)
{
	u32 val;

	check_ifc(ifc);

	val = i2s_readl(ifc, I2S_I2S_CTRL_0);
	if (!fifo) {
		val &= ~I2S_I2S_QE_FIFO1;
		val |= on ? I2S_I2S_QE_FIFO1 : 0;
	}
	else {
		val &= ~I2S_I2S_QE_FIFO2;
		val |= on ? I2S_I2S_QE_FIFO2 : 0;
	}
	i2s_writel(ifc, val, I2S_I2S_CTRL_0);
}

void i2s_enable_fifos(int ifc, int on)
{
	u32 val;

	check_ifc(ifc);

	val = i2s_readl(ifc, I2S_I2S_CTRL_0);
	if (on)
		val |= I2S_I2S_QE_FIFO1 | I2S_I2S_QE_FIFO2 |
		       I2S_I2S_IE_FIFO1_ERR | I2S_I2S_IE_FIFO2_ERR;
	else
		val &= ~(I2S_I2S_QE_FIFO1 | I2S_I2S_QE_FIFO2 |
			 I2S_I2S_IE_FIFO1_ERR | I2S_I2S_IE_FIFO2_ERR);

	i2s_writel(ifc, val, I2S_I2S_CTRL_0);
}

void i2s_fifo_write(int ifc, int fifo, u32 data)
{
	check_ifc(ifc);
	i2s_writel(ifc, data, fifo ? I2S_I2S_FIFO2_0 : I2S_I2S_FIFO1_0);
}

u32 i2s_fifo_read(int ifc, int fifo)
{
	check_ifc(ifc, 0);
	return i2s_readl(ifc, fifo ? I2S_I2S_FIFO2_0 : I2S_I2S_FIFO1_0);
}

u32 i2s_get_status(int ifc)
{
	check_ifc(ifc, 0);
	return i2s_readl(ifc, I2S_I2S_STATUS_0);
}

u32 i2s_get_control(int ifc)
{
	check_ifc(ifc, 0);
	return i2s_readl(ifc, I2S_I2S_CTRL_0);
}

void i2s_ack_status(int ifc)
{
	check_ifc(ifc);
	return i2s_writel(ifc, i2s_readl(ifc, I2S_I2S_STATUS_0), I2S_I2S_STATUS_0);
}

u32 i2s_get_fifo_scr(int ifc)
{
	check_ifc(ifc, 0);
	return i2s_readl(ifc, I2S_I2S_FIFO_SCR_0);
}

phys_addr_t i2s_get_fifo_phy_base(int ifc, int fifo)
{
	check_ifc(ifc, 0);
	return i2s_phy_base[ifc] + (fifo ? I2S_I2S_FIFO2_0 : I2S_I2S_FIFO1_0);
}

u32 i2s_get_fifo_full_empty_count(int ifc, int fifo)
{
	u32 val;

	check_ifc(ifc, 0);

	val = i2s_readl(ifc, I2S_I2S_FIFO_SCR_0);

	if (!fifo)
		val = val >> I2S_I2S_FIFO_SCR_FIFO1_FULL_EMPTY_COUNT_SHIFT;
	else
		val = val >> I2S_I2S_FIFO_SCR_FIFO2_FULL_EMPTY_COUNT_SHIFT;

	return val & I2S_I2S_FIFO_SCR_FIFO_FULL_EMPTY_COUNT_MASK;
}


struct clk *i2s_get_clock_by_name(const char *name)
{
    return tegra_get_clock_by_name(name);
}

int i2s_initialize(int ifc)
{
	i2s_enable_fifos(ifc, 0);
	i2s_set_left_right_control_polarity(ifc, AUDIO_LRCK_LEFT_LOW); /* low */
	i2s_set_master(ifc, AUDIO_MASTER_MODE); /* set as master */
	i2s_set_fifo_mode(ifc, FIFO1, 1); /* FIFO1 is TX */
	i2s_set_fifo_mode(ifc, FIFO2, 0); /* FIFO2 is RX */
	i2s_set_bit_format(ifc, AUDIO_FRAME_FORMAT_I2S);
	i2s_set_bit_size(ifc, AUDIO_BIT_SIZE_16);
	i2s_set_fifo_format(ifc, I2S_FIFO_PACKED);
	return 0;
}

int i2s_get_dma_requestor(int ifc)
{
	return ((ifc)? 1 : 2); /* 1 = I2S2, 2 = I2S1 */
}