2005-08-06 10:12:19 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2006-03-23 14:02:43 +00:00
|
|
|
#include "logf.h"
|
2005-08-06 10:12:19 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "string.h"
|
2006-07-21 08:42:28 +00:00
|
|
|
#include "audio.h"
|
2005-08-06 10:12:19 +00:00
|
|
|
|
|
|
|
#include "i2c-coldfire.h"
|
2007-05-22 20:39:50 +00:00
|
|
|
#include "audiohw.h"
|
2005-08-06 10:12:19 +00:00
|
|
|
|
2007-05-22 15:56:05 +00:00
|
|
|
const struct sound_settings_info audiohw_settings[] = {
|
|
|
|
[SOUND_VOLUME] = {"dB", 0, 1, -73, 6, -20},
|
|
|
|
/* HAVE_SW_TONE_CONTROLS */
|
|
|
|
[SOUND_BASS] = {"dB", 0, 1, -24, 24, 0},
|
|
|
|
[SOUND_TREBLE] = {"dB", 0, 1, -24, 24, 0},
|
|
|
|
[SOUND_BALANCE] = {"%", 0, 1,-100, 100, 0},
|
|
|
|
[SOUND_CHANNELS] = {"", 0, 1, 0, 5, 0},
|
|
|
|
[SOUND_STEREO_WIDTH] = {"%", 0, 1, 0, 255, 100},
|
|
|
|
[SOUND_LEFT_GAIN] = {"dB", 1, 1, 0, 31, 23},
|
|
|
|
[SOUND_RIGHT_GAIN] = {"dB", 1, 1, 0, 31, 23},
|
|
|
|
[SOUND_MIC_GAIN] = {"dB", 1, 1, 0, 1, 1},
|
|
|
|
};
|
|
|
|
|
2006-12-06 13:34:15 +00:00
|
|
|
/* convert tenth of dB volume (-840..0) to master volume register value */
|
|
|
|
int tenthdb2master(int db)
|
|
|
|
{
|
|
|
|
/* +6 to -73dB 1dB steps (plus mute == 80levels) 7bits */
|
|
|
|
/* 1111111 == +6dB (0x7f) */
|
|
|
|
/* 1111001 == 0dB (0x79) */
|
|
|
|
/* 0110000 == -73dB (0x30 */
|
|
|
|
/* 0101111 == mute (0x2f) */
|
|
|
|
|
|
|
|
if (db < VOLUME_MIN) {
|
|
|
|
return 0x2f;
|
|
|
|
} else {
|
|
|
|
return((db/10)+73+0x30);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-06 10:12:19 +00:00
|
|
|
/* local functions and definations */
|
|
|
|
#define TLV320_ADDR 0x34
|
|
|
|
|
|
|
|
struct tlv320_info
|
|
|
|
{
|
|
|
|
int vol_l;
|
|
|
|
int vol_r;
|
|
|
|
} tlv320;
|
|
|
|
|
2006-03-28 20:36:59 +00:00
|
|
|
/* Shadow registers */
|
2005-08-06 10:12:19 +00:00
|
|
|
unsigned tlv320_regs[0xf];
|
|
|
|
|
|
|
|
void tlv320_write_reg(unsigned reg, unsigned value)
|
|
|
|
{
|
2006-07-21 08:42:28 +00:00
|
|
|
unsigned char data[2];
|
2005-08-06 10:12:19 +00:00
|
|
|
|
2006-03-28 20:36:59 +00:00
|
|
|
/* The register address is the high 7 bits and the data the low 9 bits */
|
2006-07-21 08:42:28 +00:00
|
|
|
data[0] = (reg << 1) | ((value >> 8) & 1);
|
2006-10-27 11:24:47 +00:00
|
|
|
data[1] = value;
|
2005-08-06 10:12:19 +00:00
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
if (i2c_write(I2C_IFACE_0, TLV320_ADDR, data, 2) != 2)
|
2005-08-06 10:12:19 +00:00
|
|
|
{
|
2006-03-23 14:02:43 +00:00
|
|
|
logf("tlv320 error reg=0x%x", reg);
|
2005-08-06 10:12:19 +00:00
|
|
|
return;
|
2006-10-27 11:24:47 +00:00
|
|
|
}
|
2005-08-06 10:12:19 +00:00
|
|
|
|
|
|
|
tlv320_regs[reg] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* public functions */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init our tlv with default values
|
|
|
|
*/
|
2006-12-06 10:24:59 +00:00
|
|
|
void audiohw_init(void)
|
2005-08-06 10:12:19 +00:00
|
|
|
{
|
2006-03-28 20:36:59 +00:00
|
|
|
memset(tlv320_regs, 0, sizeof(tlv320_regs));
|
|
|
|
|
|
|
|
/* Initialize all registers */
|
|
|
|
|
2006-08-26 21:00:18 +00:00
|
|
|
/* All ON except OUT, ADC, MIC and LINE */
|
|
|
|
tlv320_write_reg(REG_PC, PC_OUT | PC_ADC | PC_MIC | PC_LINE);
|
2006-12-06 10:24:59 +00:00
|
|
|
audiohw_set_recvol(0, 0, AUDIO_GAIN_MIC);
|
|
|
|
audiohw_set_recvol(0, 0, AUDIO_GAIN_LINEIN);
|
|
|
|
audiohw_mute(true);
|
2006-08-26 21:00:18 +00:00
|
|
|
tlv320_write_reg(REG_AAP, AAP_DAC | AAP_MICM);
|
2006-03-28 20:36:59 +00:00
|
|
|
tlv320_write_reg(REG_DAP, 0x00); /* No deemphasis */
|
2006-08-26 21:00:18 +00:00
|
|
|
tlv320_write_reg(REG_DAIF, DAIF_IWL_16 | DAIF_FOR_I2S);
|
2006-03-28 20:36:59 +00:00
|
|
|
tlv320_write_reg(REG_DIA, DIA_ACT);
|
2006-12-06 10:24:59 +00:00
|
|
|
audiohw_set_frequency(-1); /* default */
|
2007-03-11 05:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Switch outputs ON
|
|
|
|
*/
|
|
|
|
void audiohw_postinit(void)
|
|
|
|
{
|
2006-08-26 21:00:18 +00:00
|
|
|
/* All ON except ADC, MIC and LINE */
|
2007-03-11 05:04:48 +00:00
|
|
|
sleep(HZ);
|
2006-08-26 21:00:18 +00:00
|
|
|
tlv320_write_reg(REG_PC, PC_ADC | PC_MIC | PC_LINE);
|
2007-03-11 05:04:48 +00:00
|
|
|
sleep(HZ/4);
|
|
|
|
audiohw_mute(false);
|
2005-08-06 10:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets tlv320 to default values
|
|
|
|
*/
|
2006-12-06 10:24:59 +00:00
|
|
|
void audiohw_reset(void)
|
2005-08-06 10:12:19 +00:00
|
|
|
{
|
2006-02-03 23:59:41 +00:00
|
|
|
tlv320_write_reg(REG_RR, RR_RESET);
|
2005-08-06 10:12:19 +00:00
|
|
|
}
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2006-12-06 10:24:59 +00:00
|
|
|
void audiohw_set_frequency(unsigned fsel)
|
2006-11-06 18:07:30 +00:00
|
|
|
{
|
|
|
|
/* 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 */
|
|
|
|
};
|
|
|
|
|
2007-03-14 10:06:57 +00:00
|
|
|
unsigned value_dap, value_pc;
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
if (fsel >= ARRAYLEN(values_src))
|
|
|
|
fsel = 1;
|
|
|
|
|
2007-03-14 10:06:57 +00:00
|
|
|
/* Temporarily turn off the DAC and ADC before switching sample
|
|
|
|
rates or they don't choose their filters correctly */
|
|
|
|
value_dap = tlv320_regs[REG_DAP];
|
|
|
|
value_pc = tlv320_regs[REG_PC];
|
|
|
|
|
|
|
|
tlv320_write_reg(REG_DAP, value_dap | DAP_DACM);
|
|
|
|
tlv320_write_reg(REG_PC, value_pc | PC_DAC | PC_ADC);
|
2006-11-06 18:07:30 +00:00
|
|
|
tlv320_write_reg(REG_SRC, values_src[fsel]);
|
2007-03-14 10:06:57 +00:00
|
|
|
tlv320_write_reg(REG_PC, value_pc | PC_DAC);
|
|
|
|
tlv320_write_reg(REG_PC, value_pc);
|
|
|
|
tlv320_write_reg(REG_DAP, value_dap);
|
2006-11-06 18:07:30 +00:00
|
|
|
}
|
|
|
|
|
2005-08-06 10:12:19 +00:00
|
|
|
/**
|
2006-08-26 21:00:18 +00:00
|
|
|
* Sets left and right headphone volume
|
|
|
|
*
|
|
|
|
* Left & Right: 48 .. 121 .. 127 => Volume -73dB (mute) .. +0 dB .. +6 dB
|
2005-08-06 10:12:19 +00:00
|
|
|
*/
|
2006-12-06 10:24:59 +00:00
|
|
|
void audiohw_set_headphone_vol(int vol_l, int vol_r)
|
2005-08-06 10:12:19 +00:00
|
|
|
{
|
2006-09-26 18:57:31 +00:00
|
|
|
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);
|
2005-08-06 10:12:19 +00:00
|
|
|
|
|
|
|
/* keep track of current setting */
|
|
|
|
tlv320.vol_l = vol_l;
|
|
|
|
tlv320.vol_r = vol_r;
|
|
|
|
|
2006-09-26 18:57:31 +00:00
|
|
|
if (value_l > HEADPHONE_MUTE || value_r > HEADPHONE_MUTE)
|
|
|
|
value_dap &= ~DAP_DACM;
|
|
|
|
else
|
|
|
|
value_dap |= DAP_DACM;
|
2005-08-06 10:12:19 +00:00
|
|
|
|
|
|
|
/* update */
|
2006-08-26 21:00:18 +00:00
|
|
|
tlv320_write_reg(REG_LHV, LHV_LZC | value_l);
|
|
|
|
tlv320_write_reg(REG_RHV, RHV_RZC | value_r);
|
2006-09-26 18:57:31 +00:00
|
|
|
if (value_dap != value_dap_last)
|
|
|
|
tlv320_write_reg(REG_DAP, value_dap);
|
2005-08-06 10:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-07-21 08:42:28 +00:00
|
|
|
* Set recording volume
|
|
|
|
*
|
2006-08-26 21:00:18 +00:00
|
|
|
* Line in : 0 .. 31 => Volume -34.5 .. +12 dB
|
|
|
|
* Mic (left): 0 .. 1 => Volume +0, +20 dB
|
2006-07-21 08:42:28 +00:00
|
|
|
*
|
2005-08-06 10:12:19 +00:00
|
|
|
*/
|
2006-12-06 10:24:59 +00:00
|
|
|
void audiohw_set_recvol(int left, int right, int type)
|
2005-08-06 10:12:19 +00:00
|
|
|
{
|
2006-07-21 08:42:28 +00:00
|
|
|
if (type == AUDIO_GAIN_MIC)
|
|
|
|
{
|
|
|
|
unsigned value_aap = tlv320_regs[REG_AAP];
|
2006-10-27 11:24:47 +00:00
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
if (left)
|
|
|
|
value_aap |= AAP_MICB; /* Enable mic boost (20dB) */
|
|
|
|
else
|
|
|
|
value_aap &= ~AAP_MICB;
|
|
|
|
|
2006-10-27 11:24:47 +00:00
|
|
|
tlv320_write_reg(REG_AAP, value_aap);
|
2006-08-26 21:00:18 +00:00
|
|
|
}
|
|
|
|
else if (type == AUDIO_GAIN_LINEIN)
|
2006-07-21 08:42:28 +00:00
|
|
|
{
|
|
|
|
tlv320_write_reg(REG_LLIV, LLIV_LIV(left));
|
2006-10-27 11:24:47 +00:00
|
|
|
tlv320_write_reg(REG_RLIV, RLIV_RIV(right));
|
2006-07-21 08:42:28 +00:00
|
|
|
}
|
2005-08-06 10:12:19 +00:00
|
|
|
}
|
|
|
|
|
2007-06-13 06:33:40 +00:00
|
|
|
void audiohw_mute(bool mute)
|
2005-08-06 10:12:19 +00:00
|
|
|
{
|
2006-09-26 18:57:31 +00:00
|
|
|
unsigned value_dap = tlv320_regs[REG_DAP];
|
|
|
|
unsigned value_l, value_r;
|
2005-08-06 10:12:19 +00:00
|
|
|
|
|
|
|
if (mute)
|
|
|
|
{
|
2006-03-28 20:36:59 +00:00
|
|
|
value_l = LHV_LHV(HEADPHONE_MUTE);
|
|
|
|
value_r = RHV_RHV(HEADPHONE_MUTE);
|
2006-09-26 18:57:31 +00:00
|
|
|
value_dap |= DAP_DACM;
|
2005-08-06 10:12:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-03-28 20:36:59 +00:00
|
|
|
value_l = LHV_LHV(tlv320.vol_l);
|
|
|
|
value_r = RHV_RHV(tlv320.vol_r);
|
2006-09-26 18:57:31 +00:00
|
|
|
if (value_l > HEADPHONE_MUTE || value_r > HEADPHONE_MUTE)
|
|
|
|
value_dap &= ~DAP_DACM;
|
2005-08-06 10:12:19 +00:00
|
|
|
}
|
|
|
|
|
2006-08-26 21:00:18 +00:00
|
|
|
tlv320_write_reg(REG_LHV, LHV_LZC | value_l);
|
|
|
|
tlv320_write_reg(REG_RHV, RHV_RZC | value_r);
|
2006-09-26 18:57:31 +00:00
|
|
|
tlv320_write_reg(REG_DAP, value_dap);
|
2005-08-06 10:12:19 +00:00
|
|
|
}
|
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
/* Nice shutdown of TLV320 codec */
|
2006-12-06 10:24:59 +00:00
|
|
|
void audiohw_close(void)
|
2005-08-06 10:12:19 +00:00
|
|
|
{
|
2006-12-06 10:24:59 +00:00
|
|
|
audiohw_mute(true);
|
2006-11-06 18:07:30 +00:00
|
|
|
sleep(HZ/8);
|
|
|
|
|
2006-08-26 21:00:18 +00:00
|
|
|
tlv320_write_reg(REG_PC, PC_OFF | PC_CLK | PC_OSC | PC_OUT |
|
|
|
|
PC_DAC | PC_ADC | PC_MIC | PC_LINE); /* All OFF */
|
2005-08-06 10:12:19 +00:00
|
|
|
}
|
|
|
|
|
2006-12-06 10:24:59 +00:00
|
|
|
void audiohw_enable_recording(bool source_mic)
|
2005-08-06 10:12:19 +00:00
|
|
|
{
|
2006-07-21 08:42:28 +00:00
|
|
|
unsigned value_aap, value_pc;
|
2006-10-27 11:24:47 +00:00
|
|
|
|
2005-08-06 10:12:19 +00:00
|
|
|
if (source_mic)
|
|
|
|
{
|
2006-08-26 21:00:18 +00:00
|
|
|
/* select MIC and enable mic boost (20 dB) */
|
2006-10-27 11:24:47 +00:00
|
|
|
value_aap = AAP_DAC | AAP_INSEL | AAP_MICB;
|
2006-08-26 21:00:18 +00:00
|
|
|
value_pc = PC_LINE; /* power down LINE */
|
2005-08-06 10:12:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-07-21 08:42:28 +00:00
|
|
|
value_aap = AAP_DAC | AAP_MICM;
|
2006-08-26 21:00:18 +00:00
|
|
|
value_pc = PC_MIC; /* power down MIC */
|
2005-08-06 10:12:19 +00:00
|
|
|
}
|
|
|
|
|
2006-07-21 08:42:28 +00:00
|
|
|
tlv320_write_reg(REG_PC, value_pc);
|
2005-08-06 10:12:19 +00:00
|
|
|
tlv320_write_reg(REG_AAP, value_aap);
|
|
|
|
}
|
2006-10-27 11:24:47 +00:00
|
|
|
|
2006-12-06 10:24:59 +00:00
|
|
|
void audiohw_disable_recording(void)
|
2005-08-06 10:12:19 +00:00
|
|
|
{
|
2006-07-21 08:42:28 +00:00
|
|
|
unsigned value_pc = tlv320_regs[REG_PC];
|
|
|
|
unsigned value_aap = tlv320_regs[REG_AAP];
|
2006-10-27 11:24:47 +00:00
|
|
|
|
2006-08-26 21:00:18 +00:00
|
|
|
value_aap |= AAP_MICM; /* mute MIC */
|
2006-07-21 08:42:28 +00:00
|
|
|
tlv320_write_reg(REG_PC, value_aap);
|
|
|
|
|
2006-08-26 21:00:18 +00:00
|
|
|
value_pc |= PC_ADC | PC_MIC | PC_LINE; /* ADC, MIC and LINE off */
|
2006-10-27 11:24:47 +00:00
|
|
|
tlv320_write_reg(REG_PC, value_pc);
|
2006-07-21 08:42:28 +00:00
|
|
|
}
|
2005-08-06 10:12:19 +00:00
|
|
|
|
2006-12-06 10:24:59 +00:00
|
|
|
void audiohw_set_monitor(bool enable)
|
2006-07-21 08:42:28 +00:00
|
|
|
{
|
2006-08-26 21:00:18 +00:00
|
|
|
unsigned value_aap, value_pc;
|
2006-07-21 08:42:28 +00:00
|
|
|
|
2006-08-26 21:00:18 +00:00
|
|
|
if (enable)
|
|
|
|
{
|
2006-10-27 11:24:47 +00:00
|
|
|
/* 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 */
|
2006-08-26 21:00:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value_aap = AAP_DAC | AAP_MICM;
|
2006-10-27 11:24:47 +00:00
|
|
|
value_pc = PC_ADC | PC_MIC | PC_LINE; /* ADC, MIC and LINE off */
|
2006-08-26 21:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tlv320_write_reg(REG_AAP, value_aap);
|
|
|
|
tlv320_write_reg(REG_PC, value_pc);
|
|
|
|
}
|