audio: Move hosted audio "codec" drivers into their respective target dirs

They are nearly entirely generic wrappers around ALSA controls, unique
per target, and are ripe for further consolidation.

Change-Id: I05e4a450e3e89e03616906601c4f8fa46200dff5
This commit is contained in:
Solomon Peachy 2025-11-30 11:21:37 -05:00
parent d376e0afb7
commit a79bdaf462
20 changed files with 62 additions and 43 deletions

View file

@ -0,0 +1,93 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
*
* Copyright (c) 2018 Marcin Bukat
*
* 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.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "config.h"
#include "audio.h"
#include "audiohw.h"
#include "system.h"
#include "panic.h"
#include "alsa-controls.h"
static int hw_init = 0;
static long int vol_l_hw = 255;
static long int vol_r_hw = 255;
static int muted = -1;
void audiohw_mute(int mute)
{
if (!hw_init || muted == mute)
return;
muted = mute;
if(mute)
{
long int ps0 = 0;
alsa_controls_set_ints("Output Port Switch", 1, &ps0);
}
else
{
long int ps2 = 2;
alsa_controls_set_ints("Output Port Switch", 1, &ps2);
}
}
void audiohw_preinit(void)
{
alsa_controls_init("default");
hw_init = 1;
#if defined(AUDIOHW_MUTE_ON_STOP) || defined(AUDIOHW_NEEDS_INITIAL_UNMUTE)
audiohw_mute(true); /* Start muted to avoid the POP */
#else
audiohw_mute(false);
#endif
}
void audiohw_postinit(void)
{
}
void audiohw_close(void)
{
hw_init = 0;
muted = -1;
alsa_controls_close();
}
void audiohw_set_frequency(int fsel)
{
(void)fsel;
}
void audiohw_set_volume(int vol_l, int vol_r)
{
vol_l_hw = -vol_l/5;
vol_r_hw = -vol_r/5;
if (!hw_init)
return;
alsa_controls_set_ints("Left Playback Volume", 1, &vol_l_hw);
alsa_controls_set_ints("Right Playback Volume", 1, &vol_r_hw);
}

View file

@ -0,0 +1,13 @@
#ifndef __ROCKER_CODEC__
#define __ROCKER_CODEC__
#define AUDIOHW_CAPS 0
AUDIOHW_SETTING(VOLUME, "dB", 1, 5, -115*10, 0, -30*10)
#endif
//#define AUDIOHW_MUTE_ON_STOP
//#define AUDIOHW_NEEDS_INITIAL_UNMUTE
/* Note: Due to Kernel bug, we can't use MUTE_ON_PAUSE with backlight fading */
void audiohw_mute(int mute);

View file

@ -0,0 +1,281 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
*
* Copyright (c) 2018 Marcin Bukat
* Copyright (c) 2020 Solomon Peachy
*
* 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.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
// #define LOGF_ENABLE
#include "config.h"
#include "audio.h"
#include "audiohw.h"
#include "system.h"
#include "panic.h"
#include "sysfs.h"
#include "alsa-controls.h"
#include "pcm-alsa.h"
#include "settings.h"
#include "logf.h"
/*
PCM device hw:0,0
ACCESS: MMAP_INTERLEAVED RW_INTERLEAVED
FORMAT: S16_LE S24_LE
SUBFORMAT: STD
SAMPLE_BITS: [16 32]
FRAME_BITS: [16 64]
CHANNELS: [1 2]
RATE: [8000 192000]
PERIOD_TIME: (2666 8192000]
PERIOD_SIZE: [512 65536]
PERIOD_BYTES: [4096 131072]
PERIODS: [4 128]
BUFFER_TIME: (10666 32768000]
BUFFER_SIZE: [2048 262144]
BUFFER_BYTES: [4096 524288]
TICK_TIME: ALL
Mixer controls (v1):
numid=1,iface=MIXER,name='Output Port Switch'
; type=INTEGER,access=rw------,values=1,min=0,max=5,step=0
: values=4
Mixer controls (v2+):
numid=3,iface=MIXER,name='ES9018_K2M Digital Filter'
; type=INTEGER,access=rw------,values=1,min=0,max=4,step=0
: values=0
numid=1,iface=MIXER,name='Left Playback Volume'
; type=INTEGER,access=rw------,values=1,min=0,max=255,step=0
: values=0
numid=4,iface=MIXER,name='Output Port Switch'
; type=INTEGER,access=rw------,values=1,min=0,max=5,step=0
: values=0
numid=2,iface=MIXER,name='Right Playback Volume'
; type=INTEGER,access=rw------,values=1,min=0,max=255,step=0
: values=0
numid=5,iface=MIXER,name='isDSD'
; type=BOOLEAN,access=rw------,values=1
: values=off
*/
static int hw_init = 0;
static long int vol_l_hw = 255;
static long int vol_r_hw = 255;
static long int last_ps = -1;
static int muted = -1;
extern int hwver;
void audiohw_mute(int mute)
{
logf("mute %d", mute);
if (hw_init < 0 || muted == mute)
return;
muted = mute;
if(mute)
{
long int ps0 = 0;
alsa_controls_set_ints("Output Port Switch", 1, &ps0);
}
else
{
last_ps = 0;
erosq_get_outputs();
}
}
int erosq_get_outputs(void) {
long int ps = 0; // Muted, if nothing is plugged in!
int status = 0;
if (!hw_init) return ps;
const char * const sysfs_lo_switch = "/sys/class/switch/lineout/state";
const char * const sysfs_hs_switch = "/sys/class/switch/headset/state";
sysfs_get_int(sysfs_lo_switch, &status);
if (status) ps = 1; // lineout
sysfs_get_int(sysfs_hs_switch, &status);
if (status) ps = 2; // headset
erosq_set_output(ps);
return ps;
}
void erosq_set_output(int ps)
{
if (!hw_init || muted) return;
if (last_ps != ps)
{
logf("set out %d/%ld", ps, last_ps);
/* Output port switch */
last_ps = ps;
alsa_controls_set_ints("Output Port Switch", 1, &last_ps);
audiohw_set_volume(vol_l_hw, vol_r_hw);
}
}
void audiohw_preinit(void)
{
logf("hw preinit");
alsa_controls_init("default");
hw_init = 1;
/* See if we have hw2 or later */
if (alsa_controls_find("Left Playback Volume") == -1)
hwver = 1;
else if (hwver == 1)
hwver = 23;
audiohw_mute(false); /* No need to stay muted */
}
void audiohw_postinit(void)
{
logf("hw postinit");
}
void audiohw_close(void)
{
logf("hw close");
hw_init = 0;
muted = -1;
alsa_controls_close();
}
void audiohw_set_frequency(int fsel)
{
(void)fsel;
}
/* min/max for pcm volume */
const int min_pcm = -740;
const int max_pcm = 0;
static void audiohw_set_volume_v1(int vol_l, int vol_r)
{
long l,r;
vol_l_hw = vol_l;
vol_r_hw = vol_r;
logf("set_volume_v1 %d, %d", vol_l, vol_r);
if (lineout_inserted() && !headphones_inserted()) {
/* On the EROS Q/K hardware, full scale line out is _very_ hot
at ~5.8Vpp. As the hardware provides no way to reduce
output gain, we have to back off on the PCM signal
to avoid blowing out the signal.
*/
l = r = global_settings.volume_limit * 10;
} else {
l = vol_l_hw;
r = vol_r_hw;
}
int sw_volume_l = l <= min_pcm ? min_pcm : MIN(l, max_pcm);
int sw_volume_r = r <= min_pcm ? min_pcm : MIN(r, max_pcm);
pcm_set_mixer_volume(sw_volume_l / 20, sw_volume_r / 20);
}
static void audiohw_set_volume_v2(int vol_l, int vol_r)
{
long l,r;
vol_l_hw = vol_l;
vol_r_hw = vol_r;
logf("set_volume_v2 %d, %d", vol_l, vol_r);
if (lineout_inserted() && !headphones_inserted()) {
// was l = r = ... syntax.
// for some reason r channel was not getting set
// and was quiet...?
l = -1 * (global_settings.volume_limit * 10) / 5;
r = l;
} else {
// never save volume as positive, we will
// just oscillate between positive and negative then
l = -1 * vol_l_hw / 5;
r = -1 * vol_r_hw / 5;
}
if (!hw_init)
return;
alsa_controls_set_ints("Left Playback Volume", 1, &l);
alsa_controls_set_ints("Right Playback Volume", 1, &r);
/* Dial back PCM mixer to avoid compression */
pcm_set_mixer_volume(global_settings.volume_limit / 2, global_settings.volume_limit / 2);
}
void audiohw_set_volume(int vol_l, int vol_r)
{
if (hwver >= 2) {
audiohw_set_volume_v2(vol_l, vol_r);
} else {
audiohw_set_volume_v1(vol_l, vol_r);
}
}
void audiohw_set_lineout_volume(int vol_l, int vol_r)
{
long l,r;
(void)vol_l;
(void)vol_r;
if (lineout_inserted() && !headphones_inserted()) {
// was l = r = ... syntax.
// for some reason r channel was not getting set
// and was quiet...?
l = global_settings.volume_limit * 10;
r = l;
logf("lo vol %ld %ld", l, r);
if (hw_init){
if (hwver >= 2) {
l /= 5;
l = l * -1;
r /= 5;
r = r * -1;
alsa_controls_set_ints("Left Playback Volume", 1, &l);
alsa_controls_set_ints("Right Playback Volume", 1, &r);
} else {
int sw_volume_l = l <= min_pcm ? min_pcm : MIN(l, max_pcm);
int sw_volume_r = r <= min_pcm ? min_pcm : MIN(r, max_pcm);
pcm_set_mixer_volume(sw_volume_l / 20, sw_volume_r / 20);
}
}
}
}

View file

@ -0,0 +1,23 @@
#ifndef __EROSQLINUX_CODEC__
#define __EROSQLINUX_CODEC__
#define AUDIOHW_CAPS (LINEOUT_CAP)
/* a small DC offset prevents play/pause clicking due to the DAC auto-muting */
#define PCM_DC_OFFSET_VALUE -1
/*
* Note: Maximum volume is set one step below unity in order to
* avoid overflowing pcm samples due to our DC Offset.
*
* The DAC's output is hot enough this should not be an issue.
*/
AUDIOHW_SETTING(VOLUME, "dB", 0, 2, -74, -2, -40)
//#define AUDIOHW_NEEDS_INITIAL_UNMUTE
void audiohw_mute(int mute);
void erosq_set_output(int ps);
int erosq_get_outputs(void);
#endif

View file

@ -0,0 +1,40 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright © 2010 Thomas Martitz
*
* 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.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "config.h"
#include "audiohw.h"
#include "pcm-android.h"
void audiohw_set_volume(int volume)
{
pcm_set_mixer_volume(volume);
}
void audiohw_set_balance(int balance)
{
(void)balance;
}
void audiohw_close(void)
{
pcm_shutdown();
}

View file

@ -0,0 +1,43 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2010 by Thomas Martitz
*
* 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.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef __ANDROID_CODEC_H
#define __ANDROID_CODEC_H
/* Bass and treble tone controls */
#ifdef AUDIOHW_HAVE_BASS
AUDIOHW_SETTING(BASS, "dB", 0, 1, -24, 24, 0)
#endif
#ifdef AUDIOHW_HAVE_TREBLE
AUDIOHW_SETTING(TREBLE, "dB", 0, 1, -24, 24, 0)
#endif
#if defined(HAVE_RECORDING)
AUDIOHW_SETTING(LEFT_GAIN, "dB", 1, 1,-128, 96, 0)
AUDIOHW_SETTING(RIGHT_GAIN, "dB", 1, 1,-128, 96, 0)
AUDIOHW_SETTING(MIC_GAIN, "dB", 1, 1,-128, 108, 16)
#endif
#if defined(AUDIOHW_HAVE_BASS_CUTOFF)
AUDIOHW_SETTING(BASS_CUTOFF, "", 0, 1, 1, 4, 1)
#endif
#if defined(AUDIOHW_HAVE_TREBLE_CUTOFF)
AUDIOHW_SETTING(TREBLE_CUTOFF, "", 0, 1, 1, 4, 1)
#endif
#endif /* __ANDROID_CODEC_H */

View file

@ -0,0 +1,7 @@
#ifndef __PCM_ANDROID_H
#define __PCM_ANDROID_H
void pcm_set_mixer_volume(int volume);
void pcm_shutdown(void);
#endif /* __PCM_ANDROID_H */

View file

@ -0,0 +1,169 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
*
* Copyright (c) 2018 Marcin Bukat
* Copyright (c) 2019 Roman Stolyarov
*
* 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.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "config.h"
#include "audio.h"
#include "audiohw.h"
#include "system.h"
#include "kernel.h"
#include "panic.h"
#include "sysfs.h"
#include "alsa-controls.h"
#include "pcm-alsa.h"
#include <sys/ioctl.h>
static int ak_hw = -1;
static int vol_sw[2] = {0};
static long int vol_hw[2] = {0};
static void hw_open(void)
{
ak_hw = open("/dev/ak4376", O_RDWR);
if(ak_hw < 0)
panicf("Cannot open '/dev/ak4376'");
if(ioctl(ak_hw, 0x20003424, 0) < 0)
{
panicf("Call cmd AK4376_POWER_ON fail");
}
}
static void hw_close(void)
{
if(ioctl(ak_hw, 0x20003425, 0) < 0)
{
panicf("Call cmd AK4376_POWER_OFF fail");
}
close(ak_hw);
ak_hw = -1;
}
void audiohw_preinit(void)
{
alsa_controls_init("default");
hw_open();
// NOTE:
// Of the exported controls, only these do anything:
// 10 DACL Playback Volume
// 11 DACR Playback Volume
// 12 Low Mode Switch (see table 25 in datasheet, not simple..)
audiohw_mute(false);
}
void audiohw_postinit(void)
{
}
void audiohw_close(void)
{
hw_close();
alsa_controls_close();
}
void audiohw_set_frequency(int fsel)
{
(void)fsel;
}
static int muted = -1;
void audiohw_set_volume(int vol_l, int vol_r)
{
int vol[2];
if (ak_hw < 0)
return;
vol[0] = vol_l / 20;
vol[1] = vol_r / 20;
for (int i = 0; i < 2; i++)
{
if (vol[i] > -56)
{
if (vol[i] < -12)
{
vol_hw[i] = 1;
vol_sw[i] = vol[i] + 12;
}
else
{
vol_hw[i] = 25 - (-vol[i] * 2);
vol_sw[i] = 0;
}
}
else
{
// Mute
vol_hw[i] = 0;
vol_sw[i] = 0;
}
}
if (!muted) {
alsa_controls_set_ints("DACL Playback Volume", 1, &vol_hw[0]);
alsa_controls_set_ints("DACR Playback Volume", 1, &vol_hw[1]);
pcm_set_mixer_volume(vol_sw[0], vol_sw[1]);
}
}
void audiohw_mute(int mute)
{
long int vol0 = 0;
if (ak_hw < 0 || muted == mute)
return;
muted = mute;
if(mute)
{
alsa_controls_set_ints("DACL Playback Volume", 1, &vol0);
alsa_controls_set_ints("DACR Playback Volume", 1, &vol0);
pcm_set_mixer_volume(0, 0);
}
else
{
alsa_controls_set_ints("DACL Playback Volume", 1, &vol_hw[0]);
alsa_controls_set_ints("DACR Playback Volume", 1, &vol_hw[1]);
pcm_set_mixer_volume(vol_sw[0], vol_sw[1]);
}
}
void audiohw_set_filter_roll_off(int value)
{
#if 0 // defined(FIIO_M3K_LINUX)
if (ak_hw < 0)
return;
/* 0 = Sharp;
1 = Slow;
2 = Short Sharp
3 = Short Slow */
// AK4376 supports this but the control isn't wired into ALSA!
long int value_hw = value;
alsa_controls_set_ints("AK4376 Digital Filter", 1, &value_hw);
#endif
(void)value;
}

View file

@ -0,0 +1,12 @@
#ifndef __FIIOLINUX_CODEC__
#define __FIIOLINUX_CODEC__
#define AUDIOHW_CAPS (FILTER_ROLL_OFF_CAP)
#define AUDIOHW_HAVE_SHORT2_ROLL_OFF
AUDIOHW_SETTING(VOLUME, "dB", 0, 1, -100, 0, -30)
AUDIOHW_SETTING(FILTER_ROLL_OFF, "", 0, 1, 0, 4, 0)
#endif
#define AUDIOHW_MUTE_ON_STOP
void audiohw_mute(int mute);

View file

@ -0,0 +1,55 @@
/***************************************************************************
* __________ __ ___
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2014 by Ilia Sergachev: Initial Rockbox port to iBasso DX50
* Copyright (C) 2014 by Mario Basister: iBasso DX90 port
* Copyright (C) 2014 by Simon Rothen: Initial Rockbox repository submission, additional features
* Copyright (C) 2014 by Udo Schläpfer: Code clean up, additional features
*
* 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.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef _CODEC_DX50_H_
#define _CODEC_DX50_H_
#define AUDIOHW_CAPS (MONO_VOL_CAP | FILTER_ROLL_OFF_CAP)
/*
http://www.wolfsonmicro.com/media/76425/WM8740.pdf
0.5 * ( x - 255 ) = ydB 1 <= x <= 255
mute x = 0
x = 255 -> 0dB
.
.
.
x = 2 -> -126.5dB
x = 1 -> -127dB
x = 0 -> -128dB
See audiohw.h, sound.c.
*/
AUDIOHW_SETTING(VOLUME, "dB", 0, 1, -128, 0, -30)
/* 1: slow roll off, 0: sharp roll off, sharp roll off default */
AUDIOHW_SETTING(FILTER_ROLL_OFF, "", 0, 1, 0, 1, 0)
#endif

View file

@ -0,0 +1,35 @@
/***************************************************************************
* __________ __ ___
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2014 by Ilia Sergachev: Initial Rockbox port to iBasso DX50
* Copyright (C) 2014 by Mario Basister: iBasso DX90 port
* Copyright (C) 2014 by Simon Rothen: Initial Rockbox repository submission, additional features
* Copyright (C) 2014 by Udo Schläpfer: Code clean up, additional features
*
* 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.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef _CODEC_DX90_H_
#define _CODEC_DX90_H_
#define AUDIOHW_CAPS MONO_VOL_CAP
AUDIOHW_SETTING(VOLUME, "", 0, 1, -255, 0, -128)
#endif

View file

@ -0,0 +1,33 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2010 by Thomas Martitz
*
* 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.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef _SDL_CODEC_H
#define _SDL_CODEC_H
#if (defined(HAVE_SDL_AUDIO) \
&& !(CONFIG_PLATFORM & PLATFORM_MAEMO5)) \
|| (CONFIG_PLATFORM & PLATFORM_CTRU)
AUDIOHW_SETTING(VOLUME, "dB", 0, 1, -80, 0, 0)
#else
#define AUDIOHW_CAPS (MONO_VOL_CAP)
AUDIOHW_SETTING(VOLUME, "dB", 0, 1, -99, 0, 0)
#endif /* CONFIG_PLATFORM & PLATFORM_SDL */
#endif /* _SDL_CODEC_H */

View file

@ -0,0 +1,437 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (c) 2016 Amaury Pouly
*
* 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.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "logf.h"
#include "system.h"
#include "kernel.h"
#include "string.h"
#include "stdio.h"
#include "audio.h"
#include "sound.h"
#include "audiohw.h"
#include "nwzlinux_codec.h"
#include "stdlib.h"
#include "panic.h"
#include <sys/ioctl.h>
#include "nwz_audio.h"
#include "pcm-alsa.h"
#include "alsa-controls.h"
/* This driver handle the Sony linux audio drivers: despite using many differents
* codecs, it appears that they all share a common interface and common controls. */
/* This is the alsa mixer interface exposed by Sony:
numid=3,iface=MIXER,name='Capture Src Switch'
; type=ENUMERATED,access=rw------,values=1,items=4
; Item #0 'None'
; Item #1 'Line'
; Item #2 'Fm'
; Item #3 'Mic'
: values=0
numid=2,iface=MIXER,name='Playback Src Switch'
; type=ENUMERATED,access=rw------,values=1,items=7
; Item #0 'None'
; Item #1 'Music'
; Item #2 'Video'
; Item #3 'Tv'
; Item #4 'Fm'
; Item #5 'Line'
; Item #6 'Mic'
: values=1
numid=1,iface=MIXER,name='Playback Volume'
; type=INTEGER,access=rw------,values=2,min=0,max=100,step=1
: values=5,5
numid=7,iface=MIXER,name='CODEC Acoustic Switch'
; type=BOOLEAN,access=rw------,values=1
: values=on
numid=8,iface=MIXER,name='CODEC Cue/Rev Switch'
; type=BOOLEAN,access=rw------,values=1
: values=off
numid=9,iface=MIXER,name='CODEC Fade In Switch'
; type=BOOLEAN,access=rw------,values=1
: values=off
numid=6,iface=MIXER,name='CODEC Mute Switch'
; type=BOOLEAN,access=rw------,values=1
: values=off
numid=5,iface=MIXER,name='CODEC Power Switch'
; type=BOOLEAN,access=rw------,values=1
: values=on
numid=10,iface=MIXER,name='CODEC Stanby Switch'
; type=BOOLEAN,access=rw------,values=1
: values=off
numid=4,iface=MIXER,name='Output Switch'
; type=ENUMERATED,access=rw------,values=1,items=4
; Item #0 'Headphone'
; Item #1 'LineVariable'
; Item #2 'LineFixed'
; Item #3 'Speaker'
: values=0
numid=6,iface=MIXER,name='Sampling Rate'
; type=ENUMERATED,access=rw------,values=1,items=6
; Item #0 '44100'
; Item #1 '48000'
; Item #2 '88200'
; Item #3 '96000'
; Item #4 '176400'
; Item #5 '192000'
: values=0
*/
/* List of various codecs used by Sony */
enum nwz_codec_t
{
NWZ_CS42L56,
NWZ_R2A15602LG_D,
NWZ_CS47L01_A,
NWZ_CS47L01_D,
NWZ_CXD3774GF_D,
NWZ_UNK_CODEC,
};
#define NWZ_LEVEL_MUTE -1000
/* Description of the volume curve implemented by the kernel driver */
struct nwz_vol_curve_t
{
int count; /* number of levels */
int level[]; /* levels in tenth-dB, level[0] is always mute */
};
/* file descriptor of the icx_noican device */
static int fd_noican;
/* file descriptor of the hardware sound device */
static int fd_hw;
/* Codec */
static enum nwz_codec_t nwz_codec;
/* does the code support setting the sample rate? */
static bool has_sample_rate;
static enum nwz_codec_t find_codec(void)
{
if(nwz_is_kernel_module_loaded("cs42L56_d"))
return NWZ_CS42L56;
if(nwz_is_kernel_module_loaded("r2A15602LG_d"))
return NWZ_R2A15602LG_D;
if(nwz_is_kernel_module_loaded("cs47L01_d"))
return NWZ_CS47L01_D;
if(nwz_is_kernel_module_loaded("cs47L01_a"))
return NWZ_CS47L01_A;
if(nwz_is_kernel_module_loaded("cxd3774gf_d"))
return NWZ_CXD3774GF_D;
return NWZ_UNK_CODEC;
}
const char *nwz_get_codec_name(void)
{
switch(nwz_codec)
{
case NWZ_CS42L56: return "cs42L56_d";
case NWZ_R2A15602LG_D: return "r2A15602LG_d";
case NWZ_CS47L01_D: return "cs47L01_d";
case NWZ_CS47L01_A: return "cs47L01_a";
case NWZ_CXD3774GF_D: return "cxd3774gf_d";
default: return "Unknown";
}
}
static struct nwz_vol_curve_t cxd3774gf_vol_curve =
{
.count = 31,
/* Most Sonys seem to follow the convention of 3dB/step then 2dB/step then 1dB/step */
.level = {NWZ_LEVEL_MUTE,
-550, -520, -490, -460, -430, -400, -370, -340, -310, -280, -250, /* 3dB/step */
-230, -210, -190, -170, -150, -130, -110, -90, /* 2dB/step */
-80, -70, -60, -50, -40, -30, -20, -10, 0, /* 1dB/step */
15, 35, /* 1.5dB then 2dB */
}
};
struct nwz_vol_curve_t *nwz_get_codec_vol_curve(void)
{
switch(nwz_codec)
{
case NWZ_CS47L01_A:
case NWZ_CS47L01_D:
/* there are 32 levels but the last two are the same so in fact it
* is the same curve as the cxd3774gf_d */
case NWZ_CXD3774GF_D:
return &cxd3774gf_vol_curve;
default:
/* return the safest curve (only 31 levels) */
return &cxd3774gf_vol_curve;
}
}
static void noican_init(void)
{
fd_noican = open(NWZ_NC_DEV, O_RDWR);
/* some targets don't have noise cancelling so silently fail */
}
static void noican_close(void)
{
if(fd_noican >= 0)
close(fd_noican);
}
/* Set NC switch */
static void noican_set_switch(int sw)
{
if(ioctl(fd_noican, NWZ_NC_SET_SWITCH, &sw) < 0)
panicf("ioctl(NWZ_NC_SET_SWITCH) failed");
}
/* Get NC switch */
static int noican_get_switch(void)
{
int val;
if(ioctl(fd_noican, NWZ_NC_GET_SWITCH, &val) < 0)
panicf("ioctl(NWZ_NC_GET_SWITCH) failed");
return val;
}
/* Get HP status */
static int noican_get_hp_status(void)
{
int val;
if(ioctl(fd_noican, NWZ_NC_GET_HP_STATUS, &val) < 0)
panicf("ioctl(NWZ_NC_GET_HP_STATUS) failed");
return val;
}
/* Set HP type */
static void noican_set_hp_type(int type)
{
if(ioctl(fd_noican, NWZ_NC_SET_HP_TYPE, &type) < 0)
panicf("ioctl(NWZ_NC_SET_HP_TYPE) failed");
}
/* Get HP type */
static int noican_get_hp_type(void)
{
int val;
if(ioctl(fd_noican, NWZ_NC_GET_HP_TYPE, &val) < 0)
panicf("ioctl(NWZ_NC_GET_HP_TYPE) failed");
return val;
}
/* Set gain */
static void noican_set_gain(int gain)
{
if(ioctl(fd_noican, NWZ_NC_SET_GAIN, &gain) < 0)
panicf("ioctl(NWZ_NC_SET_GAIN) failed");
}
/* Get gain */
static int noican_get_gain(void)
{
int val;
if(ioctl(fd_noican, NWZ_NC_GET_GAIN, &val) < 0)
panicf("ioctl(NWZ_NC_GET_GAIN) failed");
return val;
}
/* Set filter */
static void noican_set_filter(int filter)
{
if(ioctl(fd_noican, NWZ_NC_SET_FILTER, &filter) < 0)
panicf("ioctl(NWZ_NC_SET_FILTER) failed");
}
/* Get filter */
static int noican_get_filter(void)
{
int val;
if(ioctl(fd_noican, NWZ_NC_GET_FILTER, &val) < 0)
panicf("ioctl(NWZ_NC_GET_FILTER) failed");
return val;
}
static void hw_open(void)
{
fd_hw = open("/dev/snd/hwC0D0", O_RDWR);
if(fd_hw < 0)
panicf("Cannot open '/dev/snd/hwC0D0'");
}
static void hw_close(void)
{
close(fd_hw);
}
/* Acoustic and Cue/Rev control how the volume curve, but it is not clear
* what the intention of these modes are and the OF does not seem to use
* them by default */
bool audiohw_acoustic_enabled(void)
{
return alsa_controls_get_bool("CODEC Acoustic Switch");
}
void audiohw_enable_acoustic(bool en)
{
alsa_controls_set_bool("CODEC Acoustic Switch", en);
}
bool audiohw_cuerev_enabled(void)
{
return alsa_controls_get_bool("CODEC Cue/Rev Switch");
}
void audiohw_enable_cuerev(bool en)
{
alsa_controls_set_bool("CODEC Cue/Rev Switch", en);
}
void audiohw_set_playback_src(enum nwz_src_t src)
{
switch(src)
{
case NWZ_RADIO: alsa_controls_set_enum("Playback Src Switch", "Fm"); break;
case NWZ_MIC: alsa_controls_set_enum("Playback Src Switch", "Mic"); break;
case NWZ_PLAYBACK:
default: alsa_controls_set_enum("Playback Src Switch", "Music"); break;
}
}
void audiohw_preinit(void)
{
alsa_controls_init("default");
/* turn on codec */
alsa_controls_set_bool("CODEC Power Switch", true);
/* mute */
alsa_controls_set_bool("CODEC Mute Switch", true);
/* Acoustic and Cue/Rev control how the volume curve, but it is not clear
* what the intention of these modes are and the OF does not seem to use
* them by default */
audiohw_enable_acoustic(false);
audiohw_enable_cuerev(false);
/* select playback source */
audiohw_set_playback_src(NWZ_PLAYBACK);
/* use headphone output */
alsa_controls_set_enum("Output Switch", "Headphone");
/* unmute */
alsa_controls_set_bool("CODEC Mute Switch", false);
/* sample rate */
has_sample_rate = alsa_has_control("Sampling Rate");
/* init noican */
noican_init();
if(fd_noican >= 0)
{
/* dump configuration, for debug purposes */
printf("nc hp status: %d\n", noican_get_hp_status());
printf("nc type: %d\n", noican_get_hp_type());
printf("nc switch: %d\n", noican_get_switch());
printf("nc gain: %d\n", noican_get_gain());
printf("nc filter: %d\n", noican_get_filter());
/* make sure we start in a clean state */
noican_set_switch(NWZ_NC_SWITCH_OFF);
noican_set_hp_type(NC_HP_TYPE_DEFAULT);
noican_set_filter(NWZ_NC_FILTER_INDEX_0);
noican_set_gain(NWZ_NC_GAIN_CENTER);
}
/* init hw */
hw_open();
nwz_codec = find_codec();
printf("Codec: %s\n", nwz_get_codec_name());
}
void audiohw_postinit(void)
{
}
/* volume must be driver unit */
static void nwz_set_driver_vol(int vol)
{
long vols[2];
/* the driver expects percent, convert from centibel in range 0...x */
vols[0] = vols[1] = vol;
/* on some recent players like A10, Sony decided to merge left/right volume
* into one, thus we need to make sure we write the correct number of values */
int vol_cnt;
alsa_controls_get_info("Playback Volume", &vol_cnt);
alsa_controls_set_ints("Playback Volume", vol_cnt, vols);
}
/* volume is in tenth-dB */
void audiohw_set_volume(int vol_l, int vol_r)
{
/* FIXME at the moment we don't support balance and just average left and right.
* But this could be implemented using pcm alsa digital volume */
/* the Sony drivers expect vol_l = vol_r */
int vol = (vol_l + vol_r) / 2;
printf("request volume %d dB\n", vol / 10);
struct nwz_vol_curve_t *curve = nwz_get_codec_vol_curve();
/* min/max for pcm volume */
int min_pcm = -430;
int max_pcm = 0;
/* On some codecs (like cs47L01), Sony clear overdrives the DAC which produces
* massive clipping at any level (since they fix the DAC volume at around +6dB
* and then adjust HP volume in negative at the top of range !!). The only
* solution around this problem is to use the digital volume first so that
* very quickly the digital volume compensate for the DAC overdrive and we
* avoid clipping. */
int sony_clip_level = -80; /* any volume above this will cause massive clipping the DAC */
/* to avoid the clipping problem, virtually decrease requested volume by the
* clipping threshold, so that we will compensate in digital later by
* at least this amount if possibly */
vol -= sony_clip_level;
int drv_vol = curve->count - 1;
/* pick driver level just above request volume */
while(drv_vol > 0 && curve->level[drv_vol - 1] >= vol)
drv_vol--;
/* now remove the artifical volume change */
vol += sony_clip_level;
/* now adjust digital volume */
vol -= curve->level[drv_vol];
if(vol < min_pcm)
{
vol = min_pcm; /* digital cannot do <43dB */
drv_vol = 0; /* mute */
}
else if(vol > max_pcm)
vol = max_pcm; /* digital cannot do >0dB */
printf(" set driver volume %d (%d dB)\n", drv_vol, curve->level[drv_vol] / 10);
nwz_set_driver_vol(drv_vol);
printf(" set digital volume %d dB\n", vol / 10);
pcm_set_mixer_volume(vol / 10, vol / 10);
}
void audiohw_close(void)
{
hw_close();
alsa_controls_close();
noican_close();
}
void audiohw_set_frequency(int fsel)
{
if(has_sample_rate)
{
/* it's slightly annoying that Sony put the value in an enum with strings... */
char freq_str[16];
sprintf(freq_str, "%d", fsel);
alsa_controls_set_enum("Sampling Rate", freq_str);
}
}

View file

@ -0,0 +1,45 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2016 by Amaury Pouly
*
* 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.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef __NWZLINUX_CODEC_H__
#define __NWZLINUX_CODEC_H__
#define AUDIOHW_CAPS 0
/* Ranges from -100dB to 4dB */
AUDIOHW_SETTING(VOLUME, "dB", 0, 1, -100, 4, -10)
enum nwz_src_t
{
NWZ_PLAYBACK,
NWZ_RADIO,
NWZ_MIC,
};
/* enable/disable Sony's "acoustic" mode */
bool audiohw_acoustic_enabled(void);
void audiohw_enable_acoustic(bool en);
/* enable/disable Sony's "cuerev" mode */
bool audiohw_cuerev_enabled(void);
void audiohw_enable_cuerev(bool en);
/* select playback source */
void audiohw_set_playback_src(enum nwz_src_t src);
#endif /* __NWZLINUX_CODEC_H__ */

View file

@ -0,0 +1,190 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
*
* Copyright (c) 2018 Marcin Bukat
* Copyright (c) 2025 Solomon Peachy
*
* 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.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
//#define LOGF_ENABLE
#include "config.h"
#include "audio.h"
#include "audiohw.h"
#include "button.h"
#include "system.h"
#include "kernel.h"
#include "panic.h"
#include "sysfs.h"
#include "alsa-controls.h"
#include "pcm-alsa.h"
#include "logf.h"
/*
f28:
**** List of PLAYBACK Hardware Devices ****
card 0: tyinx1 [tyin_x1], device 0: tyin_x1-es9018_k2m-i2s es9018_k2m-hifi-0 []
Subdevices: 1/1
Subdevice #0: subdevice #0
card 0: tyinx1 [tyin_x1], device 1: tyin_x1-es9018_k2m-pcm es9018_k2m-hifi-1 []
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: saspdif [sa_spdif], device 0: SA SPDIF Dummy spdif dump dai-0 []
Subdevices: 1/1
Subdevice #0: subdevice #0
Mixer controls:
numid=3,iface=MIXER,name='ES9018_K2M Digital Filter'
; type=INTEGER,access=rw------,values=1,min=0,max=4,step=0
: values=0
numid=5,iface=MIXER,name='Hardware Mute'
; type=BOOLEAN,access=rw------,values=1
: values=off
numid=1,iface=MIXER,name='Left Playback Volume'
; type=INTEGER,access=rw------,values=1,min=0,max=255,step=0
: values=0
numid=4,iface=MIXER,name='Output Port Switch'
; type=INTEGER,access=rw------,values=1,min=0,max=5,step=0
: values=0
numid=2,iface=MIXER,name='Right Playback Volume'
; type=INTEGER,access=rw------,values=1,min=0,max=255,step=0
: values=0
numid=6,iface=MIXER,name='isDSD'
; type=BOOLEAN,access=rw------,values=1
: values=off
*/
static int hw_init = 0;
static long int vol_l_hw = 255;
static long int vol_r_hw = 255;
static long int last_ps = -1;
static int muted = -1;
void audiohw_mute(int mute)
{
if (hw_init < 0 || muted == mute)
return;
muted = mute;
alsa_controls_set_bool("Hardware Mute", !!mute);
}
int surfans_get_outputs(void){
long int ps = 0; // Muted, if nothing is plugged in!
int status = 0;
if (!hw_init) return ps;
const char * const sysfs_hs_switch = "/sys/class/switch/headset/state";
const char * const sysfs_bal_switch = "/sys/class/switch/balance/state";
sysfs_get_int(sysfs_hs_switch, &status);
if (status) ps = 2; // headset
sysfs_get_int(sysfs_bal_switch, &status);
if (status) ps = 3; // balanced output
surfans_set_output(ps);
return ps;
}
void surfans_set_output(int ps)
{
if (!hw_init || muted) return;
if (last_ps != ps)
{
logf("set out %d/%d", ps, last_ps);
/* Output port switch */
last_ps = ps;
alsa_controls_set_ints("Output Port Switch", 1, &last_ps);
audiohw_set_volume(vol_l_hw, vol_r_hw);
}
}
void audiohw_preinit(void)
{
logf("hw preinit");
alsa_controls_init("default");
hw_init = 1;
audiohw_mute(false); /* No need ? */
alsa_controls_set_bool("isDSD", 0);
}
void audiohw_postinit(void)
{
logf("hw postinit");
}
void audiohw_close(void)
{
logf("hw close");
hw_init = 0;
alsa_controls_close();
}
void audiohw_set_frequency(int fsel)
{
(void)fsel;
}
void audiohw_set_volume(int vol_l, int vol_r)
{
logf("hw vol %d %d", vol_l, vol_r);
long l,r;
vol_l_hw = vol_l;
vol_r_hw = vol_r;
l = -vol_l/5;
r = -vol_r/5;
if (!hw_init)
return;
alsa_controls_set_ints("Left Playback Volume", 1, &l);
alsa_controls_set_ints("Right Playback Volume", 1, &r);
}
void audiohw_set_filter_roll_off(int value)
{
logf("rolloff %d", value);
/* 0 = Sharp;
1 = Slow;
2 = Short Sharp
3 = Short Slow
4 = Super Slow */
#if defined(XDUOO_X20)
long int value_hw = value;
alsa_controls_set_ints("ES9018_K2M Digital Filter", 1, &value_hw);
#else
(void)value;
#endif
}

View file

@ -0,0 +1,17 @@
#ifndef __SURFANSLINUX_CODEC__
#define __SURFANSLINUX_CODEC__
#define AUDIOHW_CAPS (FILTER_ROLL_OFF_CAP)
AUDIOHW_SETTING(VOLUME, "dB", 1, 5, -102*10, 0, -30*10)
#endif
//#define AUDIOHW_MUTE_ON_STOP
#define AUDIOHW_MUTE_ON_SRATE_CHANGE
//#define AUDIOHW_NEEDS_INITIAL_UNMUTE
AUDIOHW_SETTING(FILTER_ROLL_OFF, "", 0, 1, 0, 4, 0)
#define AUDIOHW_HAVE_SHORT2_ROLL_OFF
void audiohw_mute(int mute);
void surfans_set_output(int ps);
int surfans_get_outputs(void);

View file

@ -0,0 +1,252 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
*
* Copyright (c) 2018 Marcin Bukat
* Copyright (c) 2018 Roman Stolyarov
*
* 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.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
//#define LOGF_ENABLE
#include "config.h"
#include "audio.h"
#include "audiohw.h"
#include "button.h"
#include "system.h"
#include "kernel.h"
#include "panic.h"
#include "sysfs.h"
#include "alsa-controls.h"
#include "pcm-alsa.h"
#include "logf.h"
/*
X3ii:
PCM device hw:0,0
ACCESS: MMAP_INTERLEAVED RW_INTERLEAVED
FORMAT: S16_LE S24_LE
SUBFORMAT: STD
SAMPLE_BITS: [16 32]
FRAME_BITS: [16 64]
CHANNELS: [1 2]
RATE: [8000 384000]
PERIOD_TIME: (1333 16384000]
PERIOD_SIZE: [512 131072]
PERIOD_BYTES: [4096 262144]
PERIODS: [4 256]
BUFFER_TIME: (5333 65536000]
BUFFER_SIZE: [2048 524288]
BUFFER_BYTES: [4096 1048576]
TICK_TIME: ALL
Mixer controls:
numid=1,iface=MIXER,name='Left Playback Volume'
; type=INTEGER,access=rw------,values=1,min=0,max=255,step=0
: values=0
numid=2,iface=MIXER,name='Right Playback Volume'
; type=INTEGER,access=rw------,values=1,min=0,max=255,step=0
: values=0
numid=3,iface=MIXER,name='AK4490 Digital Filter'
; type=INTEGER,access=rw------,values=1,min=0,max=4,step=0
: values=0
numid=4,iface=MIXER,name='AK4490 Soft Mute'
; type=BOOLEAN,access=rw------,values=1
: values=off
numid=5,iface=MIXER,name='Output Port Switch'
; type=INTEGER,access=rw------,values=1,min=0,max=5,step=0
: values=0
*/
static int hw_init = 0;
static long int vol_l_hw = 255;
static long int vol_r_hw = 255;
static long int last_ps = -1;
static int muted = -1;
void audiohw_mute(int mute)
{
logf("mute %d", mute);
if (!hw_init || muted == mute)
return;
muted = mute;
if(mute)
{
long int ps0 = 0;
alsa_controls_set_ints("Output Port Switch", 1, &ps0);
}
else
{
last_ps = 0;
xduoo_get_outputs();
}
}
int xduoo_get_outputs(void){
long int ps = 0; // Muted, if nothing is plugged in!
int status = 0;
if (!hw_init) return ps;
const char * const sysfs_lo_switch = "/sys/class/switch/lineout/state";
const char * const sysfs_hs_switch = "/sys/class/switch/headset/state";
#if defined(XDUOO_X20)
const char * const sysfs_bal_switch = "/sys/class/switch/balance/state";
#endif
sysfs_get_int(sysfs_lo_switch, &status);
if (status) ps = 1; // lineout
sysfs_get_int(sysfs_hs_switch, &status);
if (status) ps = 2; // headset
#if defined(XDUOO_X20)
sysfs_get_int(sysfs_bal_switch, &status);
if (status) ps = 3; // balanced output
#endif
xduoo_set_output(ps);
return ps;
}
void xduoo_set_output(int ps)
{
if (!hw_init || muted) return;
if (last_ps != ps)
{
logf("set out %d/%d", ps, last_ps);
/* Output port switch */
last_ps = ps;
alsa_controls_set_ints("Output Port Switch", 1, &last_ps);
audiohw_set_volume(vol_l_hw, vol_r_hw);
#if defined(XDUOO_X3II)
/* Enable/disable headphone remote ADC */
sysfs_set_string("/sys/devices/platform/earpods_adc.0/earpods_adc/earpods_adc_sw", (ps == 2) ? "on" : "off");
#endif
}
}
void audiohw_preinit(void)
{
logf("hw preinit");
alsa_controls_init("default");
hw_init = 1;
#if defined(XDUOO_X3II)
audiohw_mute(true); /* Start muted to avoid the POP */
#else
audiohw_mute(false); /* No need */
#endif
// const char * const codec_pmdown = "/sys/devices/platform/ingenic-x3ii.0/x3ii-ak4490-i2s/pmdown_time"; // in ms, defaults 5000
}
void audiohw_postinit(void)
{
logf("hw postinit");
}
void audiohw_close(void)
{
logf("hw close");
hw_init = 0;
alsa_controls_close();
}
void audiohw_set_frequency(int fsel)
{
(void)fsel;
}
void audiohw_set_volume(int vol_l, int vol_r)
{
logf("hw vol %d %d", vol_l, vol_r);
long l,r;
vol_l_hw = vol_l;
vol_r_hw = vol_r;
if (lineout_inserted()) {
l = 0;
r = 0;
} else {
l = -vol_l/5;
r = -vol_r/5;
}
if (!hw_init)
return;
alsa_controls_set_ints("Left Playback Volume", 1, &l);
alsa_controls_set_ints("Right Playback Volume", 1, &r);
}
void audiohw_set_lineout_volume(int vol_l, int vol_r)
{
long l,r;
logf("lo vol %d %d", vol_l, vol_r);
(void)vol_l;
(void)vol_r;
if (lineout_inserted()) {
l = 0;
r = 0;
} else {
l = -vol_l_hw/5;
r = -vol_r_hw/5;
}
alsa_controls_set_ints("Left Playback Volume", 1, &l);
alsa_controls_set_ints("Right Playback Volume", 1, &r);
}
void audiohw_set_filter_roll_off(int value)
{
logf("rolloff %d", value);
/* 0 = Sharp;
1 = Slow;
2 = Short Sharp
3 = Short Slow
4 = Super Slow */
#if defined(XDUOO_X3II)
long int value_hw = value;
alsa_controls_set_ints("AK4490 Digital Filter", 1, &value_hw);
#elif defined(XDUOO_X20)
long int value_hw = value;
alsa_controls_set_ints("ES9018_K2M Digital Filter", 1, &value_hw);
#else
(void)value;
#endif
}

View file

@ -0,0 +1,26 @@
#ifndef __XDUOOLINUX_CODEC__
#define __XDUOOLINUX_CODEC__
#define AUDIOHW_CAPS (LINEOUT_CAP | FILTER_ROLL_OFF_CAP)
AUDIOHW_SETTING(VOLUME, "dB", 1, 5, -102*10, 0, -30*10)
#endif
// We want this, but the codec takes over a second to unmute!
//#define AUDIOHW_MUTE_ON_STOP
#if defined(XDUOO_X3II)
/* The AK4490 glitches when switching sample rates */
#define AUDIOHW_MUTE_ON_SRATE_CHANGE
AUDIOHW_SETTING(FILTER_ROLL_OFF, "", 0, 1, 0, 5, 0)
#define AUDIOHW_HAVE_SS_ROLL_OFF
#endif
#if defined(XDUOO_X20)
//#define AUDIOHW_NEEDS_INITIAL_UNMUTE
AUDIOHW_SETTING(FILTER_ROLL_OFF, "", 0, 1, 0, 4, 0)
#define AUDIOHW_HAVE_SHORT2_ROLL_OFF
#endif
void audiohw_mute(int mute);
void xduoo_set_output(int ps);
int xduoo_get_outputs(void);