iPod Classic: capture support for CS42L55 codec

There are a couple of power saving options that can be selected using
defines, they configure the CODEC in a different way than OF does:

 MONO_MIC: jack microphone is connected to left channel, disabling
 right channel saves ~1 mW, there is no reason to not to do it.

 BYPASS_PGA: this option only applies to the line-in, OF does not
 bypass the PGA and configures it to 0 dB gain. At the beginning,
 this patch was written based on CODEC datasheet, bypassing PGA
 because it saves power and incrementes dinamic range ~1dB, i have
 used this setup for a while without problems. Finally this option
 was disabled at the last minute, i decided to do it after reviewing
 the OF and realizing that CS42L55 datasheet recommends to bypass the
 PGA only if the HW includes a couple of capacitors (see Typical
 Connection Diagram, Note 4), at this moment i don't know if Classic
 includes these capacitors (probably not). Anyway, i am not able to
 tell the difference listening to voice recodings.

TODO:
- Use variable PGA gain for jack microphone (it is fixed to +12 dB.
  as OF does).
- I am not a fan of having lots of unused #define options, these could
  be useful for a generic driver but actually this driver is Classic
  oriented, i am not sure if it could be considered disirable to
  eliminate them in the final version.

Change-Id: I3dadf2341f44d5e13f3847e6c9de4a76cd6f0918
This commit is contained in:
Cástor Muñoz 2014-12-06 18:58:25 +01:00
parent 67b4e7f958
commit 42abc6a496
2 changed files with 141 additions and 7 deletions

View File

@ -32,6 +32,7 @@
#include "cs42l55.h"
static int bass, treble;
static int active_dsp_modules; /* powered DSP modules mask */
/* convert tenth of dB volume (-600..120) to volume register value */
static int vol_tenthdb2hw(int db)
@ -66,6 +67,7 @@ void audiohw_preinit(void)
bass = 0;
treble = 0;
active_dsp_modules = 0;
/* Ask Cirrus or maybe Apple what the hell this means */
cscodec_write(HIDDENCTL, HIDDENCTL_UNLOCK);
@ -143,24 +145,40 @@ void audiohw_enable_lineout(bool enable)
PWRCTL2_PDN_LINA_ALWAYS | PWRCTL2_PDN_LINB_ALWAYS);
}
static void handle_dsp_power(void)
static void handle_dsp_power(int dsp_module, bool onoff)
{
if (onoff)
{
if (!active_dsp_modules)
cscodec_setbits(PLAYCTL, PLAYCTL_PDN_DSP, 0);
active_dsp_modules |= dsp_module;
}
else
{
active_dsp_modules &= ~dsp_module;
if (!active_dsp_modules)
cscodec_setbits(PLAYCTL, 0, PLAYCTL_PDN_DSP);
}
}
static void handle_tone_onoff(void)
{
if (bass || treble)
{
cscodec_setbits(PLAYCTL, PLAYCTL_PDN_DSP, 0);
handle_dsp_power(DSP_MODULE_TONE, 1);
cscodec_setbits(BTCTL, 0, BTCTL_TCEN);
}
else
{
cscodec_setbits(BTCTL, BTCTL_TCEN, 0);
cscodec_setbits(PLAYCTL, 0, PLAYCTL_PDN_DSP);
handle_dsp_power(DSP_MODULE_TONE, 0);
}
}
void audiohw_set_bass(int value)
{
bass = value;
handle_dsp_power();
handle_tone_onoff();
if (value >= -105 && value <= 120)
cscodec_setbits(TONECTL, TONECTL_BASS_MASK,
(8 - value / 15) << TONECTL_BASS_SHIFT);
@ -169,7 +187,7 @@ void audiohw_set_bass(int value)
void audiohw_set_treble(int value)
{
treble = value;
handle_dsp_power();
handle_tone_onoff();
if (value >= -105 && value <= 120)
cscodec_setbits(TONECTL, TONECTL_TREB_MASK,
(8 - value / 15) << TONECTL_TREB_SHIFT);
@ -225,5 +243,110 @@ void audiohw_set_frequency(int fsel)
}
#ifdef HAVE_RECORDING
//TODO: Implement
/* Saves power for targets supporting only mono microphone. */
#define MONO_MIC
/* Classic OF does not bypass PGA when line-in is selected. It can be
* bypassed for power saving instead of using it at fixed 0 dB gain.
* See CS42L55 DS, Figure 1 (Typical Connection Diagram), Note 4.
*/
/*#define BYPASS_PGA*/
void audiohw_enable_recording(bool source_mic)
{
/* mute ADCs */
cscodec_write(ADCCTL, ADCCTL_ADCAMUTE | ADCCTL_ADCBMUTE);
/* from cs42l55 DS:
* PWRCTL1[3]: ADC charge pump, for optimal ADC performance
* and power consumption, set to 1b when VA > 2.1 V and
* set to 0b when VA < 2.1 V.
*/
if (source_mic)
{
#ifdef MONO_MIC
/* power-up CODEC, ADCA and ADC pump */
cscodec_write(PWRCTL1, PWRCTL1_PDN_ADCB);
#else
/* power-up CODEC, ADCA, ADCB and ADC pump */
cscodec_write(PWRCTL1, 0);
#endif
#ifdef BYPASS_PGA
/* select PGA as input */
cscodec_setbits(ALHMUX, ALHMUX_ADCAMUX_MASK | ALHMUX_ADCBMUX_MASK,
ALHMUX_ADCAMUX_PGAA | ALHMUX_ADCBMUX_PGAB);
#endif
/* configure PGA: select AIN2 and set gain */
cscodec_write(PGAACTL, PGAACTL_MUX_AIN2A |
((PGA_GAIN_DB << 1) & PGAACTL_VOLUME_MASK));
#ifndef MONO_MIC
cscodec_write(PGABCTL, PGABCTL_MUX_AIN2B |
((PGA_GAIN_DB << 1) & PGABCTL_VOLUME_MASK));
#endif
}
else /* line-in */
{
/* power-up CODEC, ADCA, ADCB and ADC pump */
cscodec_write(PWRCTL1, 0);
#ifdef BYPASS_PGA
/* selects AIN1 as input (PGA is bypassed) */
cscodec_setbits(ALHMUX, ALHMUX_ADCAMUX_MASK | ALHMUX_ADCBMUX_MASK,
ALHMUX_ADCAMUX_AIN1A | ALHMUX_ADCBMUX_AIN1B);
#else
/* configure PGA: select AIN1 and set gain to 0dB */
cscodec_write(PGAACTL, PGAACTL_MUX_AIN1A);
cscodec_write(PGABCTL, PGABCTL_MUX_AIN1B);
#endif
}
cscodec_write(ADCCTL, 0); /* unmute ADCs */
}
void audiohw_disable_recording(void)
{
/* reset used registers to default values */
cscodec_write(PGAACTL, 0);
cscodec_write(PGABCTL, 0);
#ifdef BYPASS_PGA
cscodec_setbits(ALHMUX, ALHMUX_ADCAMUX_MASK | ALHMUX_ADCBMUX_MASK,
ALHMUX_ADCAMUX_PGAA | ALHMUX_ADCBMUX_PGAB);
#endif
/* power-down ADC section */
cscodec_write(PWRCTL1, PWRCTL1_PDN_CHRG |
PWRCTL1_PDN_ADCA | PWRCTL1_PDN_ADCB);
}
void audiohw_set_recvol(int left, int right, int type)
{
#ifndef MONO_MIC
(void) type;
#else
if (type == AUDIO_GAIN_LINEIN)
#endif
cscodec_write(ADCBATT, right);
cscodec_write(ADCAATT, left);
}
void audiohw_set_monitor(bool enable)
{
if (enable)
{
/* enable DSP power if it is actually disabled */
handle_dsp_power(DSP_MODULE_MONITOR, 1);
/* unmute ADC mixer */
cscodec_write(AMIXACTL, 0);
cscodec_write(AMIXBCTL, 0);
}
else
{
/* mute ADC mixer */
cscodec_write(AMIXACTL, AMIXACTL_AMIXAMUTE);
cscodec_write(AMIXBCTL, AMIXBCTL_AMIXBMUTE);
/* disable DSP power if it is not being used by other modules */
handle_dsp_power(DSP_MODULE_MONITOR, 0);
}
}
#endif /* HAVE_RECORDING */

View File

@ -23,13 +23,24 @@
#define __CS42L55_H__
#define AUDIOHW_CAPS (BASS_CAP | TREBLE_CAP | BASS_CUTOFF_CAP | \
TREBLE_CUTOFF_CAP | PRESCALER_CAP | LINEOUT_CAP)
TREBLE_CUTOFF_CAP | PRESCALER_CAP | LINEOUT_CAP | \
LIN_GAIN_CAP | MIC_GAIN_CAP)
AUDIOHW_SETTING(VOLUME, "dB", 0, 1, -60, 12, -25)
AUDIOHW_SETTING(BASS, "dB", 1, 15,-105, 120, 0)
AUDIOHW_SETTING(TREBLE, "dB", 1, 15,-105, 120, 0)
AUDIOHW_SETTING(BASS_CUTOFF, "", 0, 1, 1, 4, 2)
AUDIOHW_SETTING(TREBLE_CUTOFF, "", 0, 1, 1, 4, 1)
#ifdef HAVE_RECORDING
#define PGA_GAIN_DB 12 /* PGA fixed gain, range: -6 to +12 dB */
AUDIOHW_SETTING(LEFT_GAIN, "dB", 0, 1, -96, 0, 0)
AUDIOHW_SETTING(RIGHT_GAIN, "dB", 0, 1, -96, 0, 0)
AUDIOHW_SETTING(MIC_GAIN, "dB", 0, 1, -96, 0, 0, val + PGA_GAIN_DB)
#endif /* HAVE_RECORDING */
/* powered DSP modules */
#define DSP_MODULE_TONE (1 << 0)
#define DSP_MODULE_MONITOR (1 << 1)
void audiohw_enable_lineout(bool enable);