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
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2005 by Christian Gmeiner
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "lcd.h"
#include "cpu.h"
#include "kernel.h"
#include "thread.h"
#include "power.h"
#include "logf.h"
#include "system.h"
#include "sprintf.h"
#include "button.h"
#include "string.h"
#include "file.h"
#include "buffer.h"
#include "audio.h"
#include "i2c-coldfire.h"
#include "tlv320.h"
/* local functions and definations */
#define TLV320_ADDR 0x34
struct tlv320_info
{
int vol_l;
int vol_r;
} tlv320;
/* Shadow registers */
unsigned tlv320_regs[0xf];
void tlv320_write_reg(unsigned reg, unsigned value)
{
unsigned char data[2];
/* The register address is the high 7 bits and the data the low 9 bits */
data[0] = (reg << 1) | ((value >> 8) & 1);
data[1] = value;
if (i2c_write(I2C_IFACE_0, TLV320_ADDR, data, 2) != 2)
{
logf("tlv320 error reg=0x%x", reg);
return;
}
tlv320_regs[reg] = value;
}
/* public functions */
/**
* Init our tlv with default values
*/
void tlv320_init(void)
{
memset(tlv320_regs, 0, sizeof(tlv320_regs));
/* Initialize all registers */
/* All ON except OUT, ADC, MIC and LINE */
tlv320_write_reg(REG_PC, PC_OUT | PC_ADC | PC_MIC | PC_LINE);
tlv320_set_recvol(0, 0, AUDIO_GAIN_MIC);
tlv320_set_recvol(0, 0, AUDIO_GAIN_LINEIN);
tlv320_mute(true);
tlv320_write_reg(REG_AAP, AAP_DAC | AAP_MICM);
tlv320_write_reg(REG_DAP, 0x00); /* No deemphasis */
tlv320_write_reg(REG_DAIF, DAIF_IWL_16 | DAIF_FOR_I2S);
tlv320_write_reg(REG_DIA, DIA_ACT);
tlv320_set_frequency(-1); /* default */
/* All ON except ADC, MIC and LINE */
tlv320_write_reg(REG_PC, PC_ADC | PC_MIC | PC_LINE);
}
/**
* Resets tlv320 to default values
*/
void tlv320_reset(void)
{
tlv320_write_reg(REG_RR, RR_RESET);
}
/**
* Sets internal sample rate for DAC and ADC relative to MCLK
* Selection for frequency:
* Fs: tlv: with:
* 11025: 0 = MCLK/2 MCLK/2 SCLK, LRCK: Audio Clk / 16
* 22050: 0 = MCLK/2 MCLK SCLK, LRCK: Audio Clk / 8
* 44100: 1 = MCLK MCLK SCLK, LRCK: Audio Clk / 4 (default)
* 88200: 2 = MCLK*2 MCLK SCLK, LRCK: Audio Clk / 2
*/
void tlv320_set_frequency(unsigned fsel)
{
/* All rates available for 11.2896MHz besides 8.021 */
unsigned char values_src[3] =
{
/* Fs: */
(0x8 << 2) | SRC_CLKIN, /* 11025, 22050 */
(0x8 << 2), /* 44100 */
(0xf << 2), /* 88200 */
};
if (fsel >= ARRAYLEN(values_src))
fsel = 1;
tlv320_write_reg(REG_SRC, values_src[fsel]);
}
/**
* Sets left and right headphone volume
*
* Left & Right: 48 .. 121 .. 127 => Volume -73dB (mute) .. +0 dB .. +6 dB
*/
void tlv320_set_headphone_vol(int vol_l, int vol_r)
{
unsigned value_dap = tlv320_regs[REG_DAP];
unsigned value_dap_last = value_dap;
unsigned value_l = LHV_LHV(vol_l);
unsigned value_r = RHV_RHV(vol_r);
/* keep track of current setting */
tlv320.vol_l = vol_l;
tlv320.vol_r = vol_r;
if (value_l > HEADPHONE_MUTE || value_r > HEADPHONE_MUTE)
value_dap &= ~DAP_DACM;
else
value_dap |= DAP_DACM;
/* update */
tlv320_write_reg(REG_LHV, LHV_LZC | value_l);
tlv320_write_reg(REG_RHV, RHV_RZC | value_r);
if (value_dap != value_dap_last)
tlv320_write_reg(REG_DAP, value_dap);
}
/**
* Set recording volume
*
* Line in : 0 .. 31 => Volume -34.5 .. +12 dB
* Mic (left): 0 .. 1 => Volume +0, +20 dB
*
*/
void tlv320_set_recvol(int left, int right, int type)
{
if (type == AUDIO_GAIN_MIC)
{
unsigned value_aap = tlv320_regs[REG_AAP];
if (left)
value_aap |= AAP_MICB; /* Enable mic boost (20dB) */
else
value_aap &= ~AAP_MICB;
tlv320_write_reg(REG_AAP, value_aap);
}
else if (type == AUDIO_GAIN_LINEIN)
{
tlv320_write_reg(REG_LLIV, LLIV_LIV(left));
tlv320_write_reg(REG_RLIV, RLIV_RIV(right));
}
}
/**
* Mute (mute=true) or enable sound (mute=false)
*
*/
void tlv320_mute(bool mute)
{
unsigned value_dap = tlv320_regs[REG_DAP];
unsigned value_l, value_r;
if (mute)
{
value_l = LHV_LHV(HEADPHONE_MUTE);
value_r = RHV_RHV(HEADPHONE_MUTE);
value_dap |= DAP_DACM;
}
else
{
value_l = LHV_LHV(tlv320.vol_l);
value_r = RHV_RHV(tlv320.vol_r);
if (value_l > HEADPHONE_MUTE || value_r > HEADPHONE_MUTE)
value_dap &= ~DAP_DACM;
}
tlv320_write_reg(REG_LHV, LHV_LZC | value_l);
tlv320_write_reg(REG_RHV, RHV_RZC | value_r);
tlv320_write_reg(REG_DAP, value_dap);
}
/* Nice shutdown of TLV320 codec */
void tlv320_close(void)
{
tlv320_mute(true);
sleep(HZ/8);
tlv320_write_reg(REG_PC, PC_OFF | PC_CLK | PC_OSC | PC_OUT |
PC_DAC | PC_ADC | PC_MIC | PC_LINE); /* All OFF */
}
void tlv320_enable_recording(bool source_mic)
{
unsigned value_aap, value_pc;
if (source_mic)
{
/* select MIC and enable mic boost (20 dB) */
value_aap = AAP_DAC | AAP_INSEL | AAP_MICB;
value_pc = PC_LINE; /* power down LINE */
}
else
{
value_aap = AAP_DAC | AAP_MICM;
value_pc = PC_MIC; /* power down MIC */
}
tlv320_write_reg(REG_PC, value_pc);
tlv320_write_reg(REG_AAP, value_aap);
}
void tlv320_disable_recording(void)
{
unsigned value_pc = tlv320_regs[REG_PC];
unsigned value_aap = tlv320_regs[REG_AAP];
value_aap |= AAP_MICM; /* mute MIC */
tlv320_write_reg(REG_PC, value_aap);
value_pc |= PC_ADC | PC_MIC | PC_LINE; /* ADC, MIC and LINE off */
tlv320_write_reg(REG_PC, value_pc);
}
void tlv320_set_monitor(bool enable)
{
unsigned value_aap, value_pc;
if (enable)
{
/* Keep DAC on to allow mixing of voice with analog audio */
value_aap = AAP_DAC | AAP_BYPASS | AAP_MICM;
value_pc = PC_ADC | PC_MIC; /* ADC and MIC off */
}
else
{
value_aap = AAP_DAC | AAP_MICM;
value_pc = PC_ADC | PC_MIC | PC_LINE; /* ADC, MIC and LINE off */
}
tlv320_write_reg(REG_AAP, value_aap);
tlv320_write_reg(REG_PC, value_pc);
}
|