1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Christian Gmeiner
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
33 #include "i2c-coldfire.h"
36 /* local functions and definations */
37 #define TLV320_ADDR 0x34
45 /* Shadow registers */
46 unsigned tlv320_regs
[0xf];
48 void tlv320_write_reg(unsigned reg
, unsigned value
)
50 unsigned char data
[2];
52 /* The register address is the high 7 bits and the data the low 9 bits */
53 data
[0] = (reg
<< 1) | ((value
>> 8) & 1);
56 if (i2c_write(I2C_IFACE_0
, TLV320_ADDR
, data
, 2) != 2)
58 logf("tlv320 error reg=0x%x", reg
);
62 tlv320_regs
[reg
] = value
;
65 /* public functions */
68 * Init our tlv with default values
70 void tlv320_init(void)
72 memset(tlv320_regs
, 0, sizeof(tlv320_regs
));
74 /* Initialize all registers */
76 /* All ON except OUT, ADC, MIC and LINE */
77 tlv320_write_reg(REG_PC
, PC_OUT
| PC_ADC
| PC_MIC
| PC_LINE
);
78 tlv320_set_recvol(0, 0, AUDIO_GAIN_MIC
);
79 tlv320_set_recvol(0, 0, AUDIO_GAIN_LINEIN
);
81 tlv320_write_reg(REG_AAP
, AAP_DAC
| AAP_MICM
);
82 tlv320_write_reg(REG_DAP
, 0x00); /* No deemphasis */
83 tlv320_write_reg(REG_DAIF
, DAIF_IWL_16
| DAIF_FOR_I2S
);
84 tlv320_write_reg(REG_DIA
, DIA_ACT
);
85 tlv320_write_reg(REG_SRC
, (1 << 5)); /* 44.1kHz */
86 /* All ON except ADC, MIC and LINE */
87 tlv320_write_reg(REG_PC
, PC_ADC
| PC_MIC
| PC_LINE
);
91 * Resets tlv320 to default values
93 void tlv320_reset(void)
95 tlv320_write_reg(REG_RR
, RR_RESET
);
99 * Sets left and right headphone volume
101 * Left & Right: 48 .. 121 .. 127 => Volume -73dB (mute) .. +0 dB .. +6 dB
103 void tlv320_set_headphone_vol(int vol_l
, int vol_r
)
105 unsigned value_dap
= tlv320_regs
[REG_DAP
];
106 unsigned value_dap_last
= value_dap
;
107 unsigned value_l
= LHV_LHV(vol_l
);
108 unsigned value_r
= RHV_RHV(vol_r
);
110 /* keep track of current setting */
111 tlv320
.vol_l
= vol_l
;
112 tlv320
.vol_r
= vol_r
;
114 if (value_l
> HEADPHONE_MUTE
|| value_r
> HEADPHONE_MUTE
)
115 value_dap
&= ~DAP_DACM
;
117 value_dap
|= DAP_DACM
;
120 tlv320_write_reg(REG_LHV
, LHV_LZC
| value_l
);
121 tlv320_write_reg(REG_RHV
, RHV_RZC
| value_r
);
122 if (value_dap
!= value_dap_last
)
123 tlv320_write_reg(REG_DAP
, value_dap
);
127 * Set recording volume
129 * Line in : 0 .. 31 => Volume -34.5 .. +12 dB
130 * Mic (left): 0 .. 1 => Volume +0, +20 dB
133 void tlv320_set_recvol(int left
, int right
, int type
)
135 if (type
== AUDIO_GAIN_MIC
)
137 unsigned value_aap
= tlv320_regs
[REG_AAP
];
140 value_aap
|= AAP_MICB
; /* Enable mic boost (20dB) */
142 value_aap
&= ~AAP_MICB
;
144 tlv320_write_reg(REG_AAP
, value_aap
);
147 else if (type
== AUDIO_GAIN_LINEIN
)
149 tlv320_write_reg(REG_LLIV
, LLIV_LIV(left
));
150 tlv320_write_reg(REG_RLIV
, RLIV_RIV(right
));
155 * Mute (mute=true) or enable sound (mute=false)
158 void tlv320_mute(bool mute
)
160 unsigned value_dap
= tlv320_regs
[REG_DAP
];
161 unsigned value_l
, value_r
;
165 value_l
= LHV_LHV(HEADPHONE_MUTE
);
166 value_r
= RHV_RHV(HEADPHONE_MUTE
);
167 value_dap
|= DAP_DACM
;
171 value_l
= LHV_LHV(tlv320
.vol_l
);
172 value_r
= RHV_RHV(tlv320
.vol_r
);
173 if (value_l
> HEADPHONE_MUTE
|| value_r
> HEADPHONE_MUTE
)
174 value_dap
&= ~DAP_DACM
;
177 tlv320_write_reg(REG_LHV
, LHV_LZC
| value_l
);
178 tlv320_write_reg(REG_RHV
, RHV_RZC
| value_r
);
179 tlv320_write_reg(REG_DAP
, value_dap
);
182 /* Nice shutdown of TLV320 codec */
185 tlv320_write_reg(REG_PC
, PC_OFF
| PC_CLK
| PC_OSC
| PC_OUT
|
186 PC_DAC
| PC_ADC
| PC_MIC
| PC_LINE
); /* All OFF */
189 void tlv320_enable_recording(bool source_mic
)
191 unsigned value_daif
= tlv320_regs
[REG_DAIF
];
192 unsigned value_aap
, value_pc
;
196 /* select MIC and enable mic boost (20 dB) */
197 value_aap
= AAP_DAC
| AAP_INSEL
| AAP_MICB
;
198 value_pc
= PC_LINE
; /* power down LINE */
202 value_aap
= AAP_DAC
| AAP_MICM
;
203 value_pc
= PC_MIC
; /* power down MIC */
206 tlv320_write_reg(REG_PC
, value_pc
);
207 tlv320_write_reg(REG_AAP
, value_aap
);
209 /* Enable MASTER mode (start sending I2S data to the CPU) */
210 value_daif
|= DAIF_MS
;
211 tlv320_write_reg(REG_DAIF
, value_daif
);
214 void tlv320_disable_recording()
216 unsigned value_pc
= tlv320_regs
[REG_PC
];
217 unsigned value_aap
= tlv320_regs
[REG_AAP
];
218 unsigned value_daif
= tlv320_regs
[REG_DAIF
];
220 value_daif
&= ~DAIF_MS
; /* disable MASTER mode */
221 tlv320_write_reg(REG_DAIF
, value_daif
);
223 value_aap
|= AAP_MICM
; /* mute MIC */
224 tlv320_write_reg(REG_PC
, value_aap
);
226 value_pc
|= PC_ADC
| PC_MIC
| PC_LINE
; /* ADC, MIC and LINE off */
227 tlv320_write_reg(REG_PC
, value_pc
);
230 void tlv320_set_monitor(bool enable
)
232 unsigned value_aap
, value_pc
;
236 value_aap
= AAP_BYPASS
| AAP_MICM
;
237 value_pc
= PC_DAC
| PC_ADC
| PC_MIC
; /* DAC, ADC and MIC off */
241 value_aap
= AAP_DAC
| AAP_MICM
;
242 value_pc
= PC_ADC
| PC_MIC
| PC_LINE
; /* ADC, MIC and LINE off */
245 tlv320_write_reg(REG_AAP
, value_aap
);
246 tlv320_write_reg(REG_PC
, value_pc
);