2005-12-16 11:00:44 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2005-12-19 14:30:52 +00:00
|
|
|
* Driver for WM8975 audio codec
|
2005-12-16 11:00:44 +00:00
|
|
|
*
|
2005-12-19 14:30:52 +00:00
|
|
|
* Based on code from the ipodlinux project - http://ipodlinux.org/
|
|
|
|
* Adapted for Rockbox in December 2005
|
|
|
|
*
|
|
|
|
* Original file: linux/arch/armnommu/mach-ipod/audio.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2005 Bernard Leach (leachbj@bouncycastle.org)
|
2005-12-16 11:00:44 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2005-12-16 11:00:44 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-05-20 23:10:15 +00:00
|
|
|
#include "logf.h"
|
2005-12-16 11:00:44 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "audio.h"
|
2008-09-15 19:50:55 +00:00
|
|
|
#include "sound.h"
|
2005-12-16 11:00:44 +00:00
|
|
|
|
2006-10-20 17:12:42 +00:00
|
|
|
#include "wmcodec.h"
|
2007-05-22 20:39:50 +00:00
|
|
|
#include "audiohw.h"
|
2005-12-16 11:00:44 +00:00
|
|
|
|
2007-05-22 15:56:05 +00:00
|
|
|
const struct sound_settings_info audiohw_settings[] = {
|
|
|
|
[SOUND_VOLUME] = {"dB", 0, 1, -74, 6, -25},
|
|
|
|
[SOUND_BASS] = {"dB", 0, 1, -6, 9, 0},
|
|
|
|
[SOUND_TREBLE] = {"dB", 0, 1, -6, 9, 0},
|
2007-12-10 11:14:28 +00:00
|
|
|
[SOUND_BALANCE] = {"%", 0, 1,-100, 100, 0},
|
2007-05-22 15:56:05 +00:00
|
|
|
[SOUND_CHANNELS] = {"", 0, 1, 0, 5, 0},
|
2007-10-09 21:29:20 +00:00
|
|
|
[SOUND_STEREO_WIDTH] = {"%", 0, 5, 0, 250, 100},
|
2008-05-25 21:19:07 +00:00
|
|
|
#ifdef HAVE_RECORDING
|
2008-09-10 23:50:26 +00:00
|
|
|
[SOUND_LEFT_GAIN] = {"dB", 1, 1, 0, 63, 23},
|
|
|
|
[SOUND_RIGHT_GAIN] = {"dB", 1, 1, 0, 63, 23},
|
|
|
|
[SOUND_MIC_GAIN] = {"dB", 1, 1, 0, 63, 0},
|
2008-05-25 21:19:07 +00:00
|
|
|
#endif
|
2007-05-22 15:56:05 +00:00
|
|
|
};
|
2007-01-13 23:57:19 +00:00
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
static unsigned short wm8975_regs[] =
|
|
|
|
{
|
|
|
|
[LINVOL] = LINVOL_LZCEN | 23, /* 0dB */
|
|
|
|
[RINVOL] = RINVOL_RIVU | RINVOL_RZCEN | 23, /* 0dB */
|
|
|
|
[DAPCTRL] = DAPCTRL_DACMU,
|
|
|
|
[PWRMGMT1] = PWRMGMT1_VMIDSEL_5K | PWRMGMT1_VREF,
|
|
|
|
[PWRMGMT2] = PWRMGMT2_DACL | PWRMGMT2_DACR | PWRMGMT2_LOUT1
|
|
|
|
| PWRMGMT2_ROUT1 | PWRMGMT2_LOUT2 | PWRMGMT2_ROUT2,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void wm8975_write(int reg, unsigned val)
|
|
|
|
{
|
|
|
|
wm8975_regs[reg] = val;
|
|
|
|
wmcodec_write(reg, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wm8975_write_and(int reg, unsigned bits)
|
|
|
|
{
|
|
|
|
wm8975_write(reg, wm8975_regs[reg] & bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wm8975_write_or(int reg, unsigned bits)
|
|
|
|
{
|
|
|
|
wm8975_write(reg, wm8975_regs[reg] | bits);
|
|
|
|
}
|
|
|
|
|
2006-12-06 13:34:15 +00:00
|
|
|
/* convert tenth of dB volume (-730..60) 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 */
|
2007-03-07 15:00:29 +00:00
|
|
|
/* 0101111..0000000 == mute (0x2f) */
|
2006-12-06 13:34:15 +00:00
|
|
|
|
|
|
|
if (db < VOLUME_MIN) {
|
|
|
|
return 0x0;
|
|
|
|
} else {
|
|
|
|
return((db/10)+73+0x30);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
int sound_val2phys(int setting, int value)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
switch(setting)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_RECORDING
|
|
|
|
case SOUND_LEFT_GAIN:
|
|
|
|
case SOUND_RIGHT_GAIN:
|
|
|
|
result = ((value - 23) * 15) / 2;
|
|
|
|
break;
|
|
|
|
case SOUND_MIC_GAIN:
|
|
|
|
result = ((value - 23) * 15) / 2 + 200;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
result = value;
|
|
|
|
break;
|
|
|
|
}
|
2005-12-16 11:00:44 +00:00
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
return result;
|
|
|
|
}
|
2005-12-16 11:00:44 +00:00
|
|
|
|
2010-04-27 00:05:02 +00:00
|
|
|
static void audiohw_mute(bool mute)
|
2005-12-16 11:00:44 +00:00
|
|
|
{
|
2008-09-10 23:50:26 +00:00
|
|
|
if (mute) {
|
|
|
|
/* Set DACMU = 1 to soft-mute the audio DACs. */
|
|
|
|
wm8975_write_or(DAPCTRL, DAPCTRL_DACMU);
|
|
|
|
} else {
|
|
|
|
/* Set DACMU = 0 to soft-un-mute the audio DACs. */
|
|
|
|
wm8975_write_and(DAPCTRL, ~DAPCTRL_DACMU);
|
|
|
|
}
|
|
|
|
}
|
2007-01-13 23:57:19 +00:00
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
#define IPOD_PCM_LEVEL 0x65 /* -6dB */
|
|
|
|
|
|
|
|
void audiohw_preinit(void)
|
|
|
|
{
|
|
|
|
/* POWER UP SEQUENCE */
|
|
|
|
wmcodec_write(RESET, RESET_RESET);
|
|
|
|
|
|
|
|
/* 2. Enable Vmid and VREF, quick startup. */
|
|
|
|
wm8975_write(PWRMGMT1, wm8975_regs[PWRMGMT1]);
|
|
|
|
sleep(HZ/50);
|
|
|
|
wm8975_regs[PWRMGMT1] &= ~PWRMGMT1_VMIDSEL_MASK;
|
|
|
|
wm8975_write(PWRMGMT1, wm8975_regs[PWRMGMT1] | PWRMGMT1_VMIDSEL_50K);
|
2005-12-16 11:00:44 +00:00
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
/* 4. Enable DACs, line and headphone output buffers as required. */
|
|
|
|
wm8975_write(PWRMGMT2, wm8975_regs[PWRMGMT2]);
|
2005-12-16 11:00:44 +00:00
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
wmcodec_write(AINTFCE, AINTFCE_MS | AINTFCE_LRP_I2S_RLO
|
|
|
|
| AINTFCE_IWL_16BIT | AINTFCE_FORMAT_I2S);
|
|
|
|
|
|
|
|
wm8975_write(DAPCTRL, wm8975_regs[DAPCTRL] );
|
|
|
|
|
2009-10-05 12:29:21 +00:00
|
|
|
/* Still need to find out why this is neccessary */
|
|
|
|
#ifdef IPOD_NANO2G
|
|
|
|
wmcodec_write(SAMPCTRL, 0);
|
|
|
|
#else
|
2008-12-12 11:01:07 +00:00
|
|
|
wmcodec_write(SAMPCTRL, WM8975_44100HZ);
|
2009-10-05 12:29:21 +00:00
|
|
|
#endif
|
2006-02-12 17:18:47 +00:00
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
/* set the volume to -6dB */
|
|
|
|
wmcodec_write(LOUT1VOL, LOUT1VOL_LO1ZC | IPOD_PCM_LEVEL);
|
|
|
|
wmcodec_write(ROUT1VOL, ROUT1VOL_RO1VU | ROUT1VOL_RO1ZC | IPOD_PCM_LEVEL);
|
|
|
|
|
|
|
|
wmcodec_write(LOUTMIX1, LOUTMIX1_LD2LO| LOUTMIX1_LI2LOVOL(5));
|
|
|
|
wmcodec_write(LOUTMIX2, LOUTMIX2_RI2LOVOL(5));
|
2005-12-16 11:00:44 +00:00
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
wmcodec_write(ROUTMIX1, ROUTMIX1_LI2ROVOL(5));
|
|
|
|
wmcodec_write(ROUTMIX2, ROUTMIX2_RD2RO| ROUTMIX2_RI2ROVOL(5));
|
2005-12-16 11:00:44 +00:00
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
wmcodec_write(MOUTMIX1, 0);
|
|
|
|
wmcodec_write(MOUTMIX2, 0);
|
2005-12-16 11:00:44 +00:00
|
|
|
}
|
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
void audiohw_postinit(void)
|
|
|
|
{
|
|
|
|
audiohw_mute(false);
|
|
|
|
}
|
2007-01-13 23:57:19 +00:00
|
|
|
|
|
|
|
|
2008-02-13 11:19:23 +00:00
|
|
|
void audiohw_set_master_vol(int vol_l, int vol_r)
|
2005-12-16 11:00:44 +00:00
|
|
|
{
|
|
|
|
/* +6 to -73dB 1dB steps (plus mute == 80levels) 7bits */
|
|
|
|
/* 1111111 == +6dB */
|
|
|
|
/* 1111001 == 0dB */
|
|
|
|
/* 0110000 == -73dB */
|
|
|
|
/* 0101111 == mute (0x2f) */
|
|
|
|
|
|
|
|
/* OUT1 */
|
2008-09-10 23:50:26 +00:00
|
|
|
wmcodec_write(LOUT1VOL, LOUT1VOL_LO1ZC | vol_l);
|
|
|
|
wmcodec_write(ROUT1VOL, ROUT1VOL_RO1VU | ROUT1VOL_RO1ZC | vol_r);
|
2006-03-22 13:04:49 +00:00
|
|
|
}
|
|
|
|
|
2008-02-13 11:19:23 +00:00
|
|
|
void audiohw_set_lineout_vol(int vol_l, int vol_r)
|
2006-03-22 13:04:49 +00:00
|
|
|
{
|
2005-12-16 11:00:44 +00:00
|
|
|
/* OUT2 */
|
2008-09-10 23:50:26 +00:00
|
|
|
wmcodec_write(LOUT2VOL, LOUT2VOL_LO2ZC | vol_l);
|
|
|
|
wmcodec_write(ROUT2VOL, ROUT2VOL_RO2VU | ROUT2VOL_RO2ZC | vol_r);
|
2005-12-16 11:00:44 +00:00
|
|
|
}
|
|
|
|
|
2006-12-06 10:24:59 +00:00
|
|
|
void audiohw_set_bass(int value)
|
2005-12-16 11:00:44 +00:00
|
|
|
{
|
2007-03-07 15:00:29 +00:00
|
|
|
const int regvalues[] = {
|
2008-09-10 23:50:26 +00:00
|
|
|
11, 10, 10, 9, 8, 8, 0xf, 6, 6, 5, 4, 4, 3, 2, 2, 1
|
2007-03-07 15:00:29 +00:00
|
|
|
};
|
2006-01-30 21:12:31 +00:00
|
|
|
|
2007-03-07 15:00:29 +00:00
|
|
|
if ((value >= -6) && (value <= 9)) {
|
|
|
|
/* We use linear bass control with 200 Hz cutoff */
|
2008-09-10 23:50:26 +00:00
|
|
|
wmcodec_write(BASSCTRL, regvalues[value + 6] | BASSCTRL_BC);
|
2007-03-07 15:00:29 +00:00
|
|
|
}
|
2005-12-16 11:00:44 +00:00
|
|
|
}
|
|
|
|
|
2006-12-06 10:24:59 +00:00
|
|
|
void audiohw_set_treble(int value)
|
2005-12-16 11:00:44 +00:00
|
|
|
{
|
2007-03-07 15:00:29 +00:00
|
|
|
const int regvalues[] = {
|
2008-09-10 23:50:26 +00:00
|
|
|
11, 10, 10, 9, 8, 8, 0xf, 6, 6, 5, 4, 4, 3, 2, 2, 1
|
2007-03-07 15:00:29 +00:00
|
|
|
};
|
2006-01-30 21:12:31 +00:00
|
|
|
|
2007-03-07 15:00:29 +00:00
|
|
|
if ((value >= -6) && (value <= 9)) {
|
|
|
|
/* We use linear treble control with 4 kHz cutoff */
|
2008-09-10 23:50:26 +00:00
|
|
|
wmcodec_write(TREBCTRL, regvalues[value + 6] | TREBCTRL_TC);
|
2005-12-16 11:00:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Nice shutdown of WM8975 codec */
|
2006-12-06 10:24:59 +00:00
|
|
|
void audiohw_close(void)
|
2005-12-16 11:00:44 +00:00
|
|
|
{
|
2008-09-10 23:50:26 +00:00
|
|
|
audiohw_mute(true);
|
2005-12-16 11:00:44 +00:00
|
|
|
|
|
|
|
/* 2. Disable all output buffers. */
|
2008-09-10 23:50:26 +00:00
|
|
|
wmcodec_write(PWRMGMT2, 0x0);
|
2005-12-16 11:00:44 +00:00
|
|
|
|
|
|
|
/* 3. Switch off the power supplies. */
|
2008-09-10 23:50:26 +00:00
|
|
|
wmcodec_write(PWRMGMT1, 0x0);
|
2005-12-16 11:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Note: Disable output before calling this function */
|
2008-12-12 11:01:07 +00:00
|
|
|
void audiohw_set_frequency(int fsel)
|
2008-09-10 23:50:26 +00:00
|
|
|
{
|
2008-12-12 11:01:07 +00:00
|
|
|
(void)fsel;
|
2005-12-16 11:00:44 +00:00
|
|
|
}
|
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
#ifdef HAVE_RECORDING
|
2006-12-18 01:52:21 +00:00
|
|
|
void audiohw_enable_recording(bool source_mic)
|
|
|
|
{
|
2008-09-10 23:50:26 +00:00
|
|
|
wm8975_regs[PWRMGMT1] |= PWRMGMT1_AINL | PWRMGMT1_AINR
|
|
|
|
| PWRMGMT1_ADCL | PWRMGMT1_ADCR;
|
|
|
|
wm8975_write(PWRMGMT1, wm8975_regs[PWRMGMT1]);
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
/* NOTE: When switching to digital monitoring we will not want
|
|
|
|
* the DACs disabled. Also the outputs shouldn't be disabled
|
|
|
|
* when recording from line in (dock connector) - needs testing. */
|
|
|
|
wm8975_regs[PWRMGMT2] &= ~(PWRMGMT2_LOUT1 | PWRMGMT2_ROUT1
|
|
|
|
| PWRMGMT2_LOUT2 | PWRMGMT2_ROUT2);
|
|
|
|
wm8975_write(PWRMGMT2, wm8975_regs[PWRMGMT2]);
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
wm8975_write_or(LINVOL, LINVOL_LINMUTE);
|
|
|
|
wm8975_write_or(RINVOL, RINVOL_RINMUTE);
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
wmcodec_write(ADDCTRL3, ADDCTRL3_VROI);
|
2006-12-18 01:52:21 +00:00
|
|
|
|
|
|
|
if (source_mic) {
|
2008-09-10 23:50:26 +00:00
|
|
|
wmcodec_write(ADDCTRL1, ADDCTRL1_VSEL_LOWBIAS | ADDCTRL1_DATSEL_RADC
|
|
|
|
| ADDCTRL1_TOEN);
|
|
|
|
wmcodec_write(ADCLPATH, 0);
|
|
|
|
wmcodec_write(ADCRPATH, ADCRPATH_RINSEL_RIN2 | ADCRPATH_RMICBOOST_20dB);
|
2006-12-18 01:52:21 +00:00
|
|
|
} else {
|
2008-09-10 23:50:26 +00:00
|
|
|
wmcodec_write(ADDCTRL1, ADDCTRL1_VSEL_LOWBIAS | ADDCTRL1_DATSEL_NORMAL
|
|
|
|
| ADDCTRL1_TOEN);
|
|
|
|
wmcodec_write(ADCLPATH, ADCLPATH_LINSEL_LIN1 | ADCLPATH_LMICBOOST_OFF);
|
|
|
|
wmcodec_write(ADCRPATH, ADCRPATH_RINSEL_RIN1 | ADCRPATH_RMICBOOST_OFF);
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
2008-09-10 23:50:26 +00:00
|
|
|
wm8975_write_and(LINVOL, ~LINVOL_LINMUTE);
|
|
|
|
wm8975_write_and(RINVOL, ~RINVOL_RINMUTE);
|
2006-12-18 01:52:21 +00:00
|
|
|
}
|
2005-12-16 11:00:44 +00:00
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
void audiohw_disable_recording(void)
|
|
|
|
{
|
|
|
|
/* mute inputs */
|
|
|
|
wm8975_write_or(LINVOL, LINVOL_LINMUTE);
|
|
|
|
wm8975_write_or(RINVOL, RINVOL_RINMUTE);
|
2006-12-18 01:52:21 +00:00
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
wmcodec_write(ADDCTRL3, 0);
|
2005-12-16 11:00:44 +00:00
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
wm8975_regs[PWRMGMT2] |= PWRMGMT2_DACL | PWRMGMT2_DACR
|
|
|
|
| PWRMGMT2_LOUT1 | PWRMGMT2_ROUT1
|
|
|
|
| PWRMGMT2_LOUT2 | PWRMGMT2_ROUT2;
|
|
|
|
wm8975_write(PWRMGMT2, wm8975_regs[PWRMGMT2]);
|
2005-12-16 11:00:44 +00:00
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
wm8975_regs[PWRMGMT1] &= ~(PWRMGMT1_AINL | PWRMGMT1_AINR
|
|
|
|
| PWRMGMT1_ADCL | PWRMGMT1_ADCR);
|
|
|
|
wm8975_write(PWRMGMT1, wm8975_regs[PWRMGMT1]);
|
2005-12-16 11:00:44 +00:00
|
|
|
}
|
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
void audiohw_set_recvol(int left, int right, int type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case AUDIO_GAIN_MIC: /* Mic uses right ADC */
|
|
|
|
wm8975_regs[RINVOL] &= ~RINVOL_MASK;
|
|
|
|
wm8975_write_or(RINVOL, left & RINVOL_MASK);
|
|
|
|
break;
|
|
|
|
case AUDIO_GAIN_LINEIN:
|
|
|
|
wm8975_regs[LINVOL] &= ~LINVOL_MASK;
|
|
|
|
wm8975_write_or(LINVOL, left & LINVOL_MASK);
|
|
|
|
wm8975_regs[RINVOL] &= ~RINVOL_MASK;
|
|
|
|
wm8975_write_or(RINVOL, right & RINVOL_MASK);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2005-12-16 11:00:44 +00:00
|
|
|
|
2008-09-10 23:50:26 +00:00
|
|
|
void audiohw_set_monitor(bool enable)
|
|
|
|
{
|
2005-12-16 11:18:04 +00:00
|
|
|
(void)enable;
|
2005-12-16 11:00:44 +00:00
|
|
|
}
|
2008-09-10 23:50:26 +00:00
|
|
|
#endif /* HAVE_RECORDING */
|