User Tools

Site Tools


alsa

This is an old revision of the document!


ALSA and the speakers

The two grille speakers are on the Line-Out of a TI TLV320AIC32x4 codec. The codec is on I2C bus 0 at address 0x18, fed over SSI, and shows up as ALSA card 0, PM3 Audio. There is a capture path too (Mic PGA / ADC).

The Line-Out does not drive the speakers directly. It feeds an external power amplifier, PAmobil2, which is off at power-on. The amp is switched on by the PIC32 over /dev/ttymxc2:

  • PIC32 key 0x07E0 (ValueWrite, type 0x04): 1 = amp on, 0 = amp off.
  • One master gate for both channels.
  • It reads back with ValueRead, so the state can be queried.
  • It latches until the PIC32 resets at the next power-off. Nothing persistent is written.

With the amp off, ALSA, the codec, the mixer, and the clocks can all be perfectly configured and the speaker is still silent. That is why playback needed nx in practice: nx switches the amp on once at startup through NxPaCtrl/NxPaMobil2PicDev.

Turning the amp on

The stock tool does it with:

NxExe prox2 amp

That command toggles the amp. The state persists until the PIC32 loses power, so blindly toggling on boot can turn it off if it was already on. Read the state first, or set it explicitly.

To set the amp explicitly, write the PIC32 frame directly. These are IPP frames with sequence 1 and no payload CRC, because 0x04 and 0x02 are below 0x80:

stty -F /dev/ttymxc2 115200 cs8 -cstopb -parenb raw -echo

# amp on: ValueWrite 0x07E0 = 1
printf '\274\001\000\004\000\003\313\007\340\001' > /dev/ttymxc2

# amp off: ValueWrite 0x07E0 = 0
printf '\274\001\000\004\000\003\313\007\340\000' > /dev/ttymxc2

# read state: ValueRead 0x07E0
printf '\274\001\000\002\000\002\104\007\340' > /dev/ttymxc2
timeout -t 2 dd if=/dev/ttymxc2 bs=1 count=10 2>/dev/null | hexdump -C

The reply to the read is a 0x03 frame with the 2-byte key and the current value. The examples use sequence 1. If you write a script, increment the sequence byte and recompute the CRC-8 for each frame.

ALSA setup

alsa-restore.service restores /etc/asound.state at boot, so the routing is usually already set. The two details that still cause silence are:

Line-Out driver gain must be at the 0 dB baseline. 6 is the baseline, not a volume control.

amixer -c 0 sset 'LO Driver Gain' 6

Play a file, not a pipe. A file plays:

aplay /tmp/tone.wav

Piping into aplay from stdin is silent on this device:

cat tone.wav | aplay -q -

Volume is the PCM control. Use it as a percentage:

amixer -c 0 sset PCM 65%

The old amixer sset PCM 144 command sets a raw level. Treat it as a high level, not a percentage.

A complete manual sequence:

printf '\274\001\000\004\000\003\313\007\340\001' > /dev/ttymxc2
alsactl -f /etc/asound.state restore
amixer -c 0 sset 'LO Driver Gain' 6
amixer -c 0 sset PCM 65%
scp -O tone.wav root@<unit-ip>:/tmp/
ssh root@<unit-ip> aplay /tmp/tone.wav

Why nx seemed to be required

nx turns the speaker amp on once at startup and never turns it off. The PIC32 keeps that state until it loses power.

The codec is not the gate. Its registers, DAPM, GPIO, clocks, AUDMUX, and SSI1 can all match while the speaker is silent. The amp is a separate PIC32 value on ttymxc2, after the codec. nx does not touch any extra ALSA mixer or codec control. It writes 0x07E0 = 1 once near boot.

Set 0x07E0 to 1 and the speaker works from a cold boot with no nx. It stays on until the PIC32 resets, so it only needs setting once per power cycle.

Notes (maybe outdated)

People have had mixed results with ALSA initialization. One person only had success by running nx:

Saving and restoring ALSA device state isn't enough to initialize from boot.
The nx binary is built with miniaudio
inside it, so perhaps it does some external hardware initialization of the
speaker. I couldn't get miniaudio in a custom binary to work without
initializing by running nx. Maybe there is a way to get the miniaudio debug
logs from a running nx instance.

Another person found that it worked out of the box:

Copying an audio file (a 44.1 kHz mono WAV in my case) onto the SD card and
playing it with aplay worked even with nx disabled. alsamixer
is preinstalled and works for setting the volume.

Addendum

The older reports about mixed ALSA results are consistent with the amp latch.

If nx has run since power-on, the amp is on and aplay works even after nx is stopped. That is the “worked out of the box” case.

If the unit is cold-booted with nx never started, aplay is silent until 0x07E0 is set to 1. Saving and restoring /etc/asound.state does not touch the amp, so it cannot fix that case.

The miniaudio theory was speculation. nx does not need to stay running for audio. It only needs to leave the amp switched on.

Stock ALSA state

The stock /etc/asound.state is below. If you build a custom rootfs, keep this file and restore it at boot:

alsactl -f /etc/asound.state restore

The important parts are the Line-Out DAC switches, LO Driver Gain at 6 (0 dB), and PCM Playback Volume at 85 (about -21 dB). The rest is the input and AGC configuration.

state.Audio {
	control.1 {
		iface MIXER
		name 'PCM Playback Volume'
		value.0 85
		value.1 85
		comment {
			access 'read write'
			type INTEGER
			count 2
			range '0 - 175'
			dbmin -6350
			dbmax 2400
			dbvalue.0 -2100
			dbvalue.1 -2100
		}
	}
	control.2 {
		iface MIXER
		name 'HP Driver Gain Volume'
		value.0 6
		value.1 6
		comment {
			access 'read write'
			type INTEGER
			count 2
			range '0 - 35'
			dbmin -600
			dbmax 2900
			dbvalue.0 0
			dbvalue.1 0
		}
	}
	control.3 {
		iface MIXER
		name 'LO Driver Gain Volume'
		value.0 6
		value.1 6
		comment {
			access 'read write'
			type INTEGER
			count 2
			range '0 - 35'
			dbmin -600
			dbmax 2900
			dbvalue.0 0
			dbvalue.1 0
		}
	}
	control.4 {
		iface MIXER
		name 'HP DAC Playback Switch'
		value.0 false
		value.1 false
		comment {
			access 'read write'
			type BOOLEAN
			count 2
		}
	}
	control.5 {
		iface MIXER
		name 'LO DAC Playback Switch'
		value.0 true
		value.1 true
		comment {
			access 'read write'
			type BOOLEAN
			count 2
		}
	}
	control.6 {
		iface MIXER
		name 'Mic PGA Switch'
		value.0 false
		value.1 false
		comment {
			access 'read write'
			type BOOLEAN
			count 2
		}
	}
	control.7 {
		iface MIXER
		name 'ADCFGA Left Mute Switch'
		value false
		comment {
			access 'read write'
			type BOOLEAN
			count 1
		}
	}
	control.8 {
		iface MIXER
		name 'ADCFGA Right Mute Switch'
		value false
		comment {
			access 'read write'
			type BOOLEAN
			count 1
		}
	}
	control.9 {
		iface MIXER
		name 'ADC Level Volume'
		value.0 24
		value.1 24
		comment {
			access 'read write'
			type INTEGER
			count 2
			range '0 - 64'
			dbmin -1200
			dbmax 2000
			dbvalue.0 0
			dbvalue.1 0
		}
	}
	control.10 {
		iface MIXER
		name 'PGA Level Volume'
		value.0 0
		value.1 0
		comment {
			access 'read write'
			type INTEGER
			count 2
			range '0 - 95'
			dbmin 0
			dbmax 4750
			dbvalue.0 0
			dbvalue.1 0
		}
	}
	control.11 {
		iface MIXER
		name 'Auto-mute Switch'
		value 0
		comment {
			access 'read write'
			type INTEGER
			count 1
			range '0 - 7'
		}
	}
	control.12 {
		iface MIXER
		name 'AGC Left Switch'
		value false
		comment {
			access 'read write'
			type BOOLEAN
			count 1
		}
	}
	control.13 {
		iface MIXER
		name 'AGC Right Switch'
		value false
		comment {
			access 'read write'
			type BOOLEAN
			count 1
		}
	}
	control.14 {
		iface MIXER
		name 'AGC Target Level'
		value.0 0
		value.1 0
		comment {
			access 'read write'
			type INTEGER
			count 2
			range '0 - 7'
		}
	}
	control.15 {
		iface MIXER
		name 'AGC Gain Hysteresis'
		value.0 0
		value.1 0
		comment {
			access 'read write'
			type INTEGER
			count 2
			range '0 - 3'
		}
	}
	control.16 {
		iface MIXER
		name 'AGC Hysteresis'
		value.0 0
		value.1 0
		comment {
			access 'read write'
			type INTEGER
			count 2
			range '0 - 3'
		}
	}
	control.17 {
		iface MIXER
		name 'AGC Noise Threshold'
		value.0 0
		value.1 0
		comment {
			access 'read write'
			type INTEGER
			count 2
			range '0 - 31'
		}
	}
	control.18 {
		iface MIXER
		name 'AGC Max PGA'
		value.0 127
		value.1 127
		comment {
			access 'read write'
			type INTEGER
			count 2
			range '0 - 127'
		}
	}
	control.19 {
		iface MIXER
		name 'AGC Attack Time'
		value.0 0
		value.1 0
		comment {
			access 'read write'
			type INTEGER
			count 2
			range '0 - 31'
		}
	}
	control.20 {
		iface MIXER
		name 'AGC Decay Time'
		value.0 0
		value.1 0
		comment {
			access 'read write'
			type INTEGER
			count 2
			range '0 - 31'
		}
	}
	control.21 {
		iface MIXER
		name 'AGC Noise Debounce'
		value.0 0
		value.1 0
		comment {
			access 'read write'
			type INTEGER
			count 2
			range '0 - 31'
		}
	}
	control.22 {
		iface MIXER
		name 'AGC Signal Debounce'
		value.0 0
		value.1 0
		comment {
			access 'read write'
			type INTEGER
			count 2
			range '0 - 15'
		}
	}
	control.23 {
		iface MIXER
		name 'HPL Output Mixer L_DAC Switch'
		value false
		comment {
			access 'read write'
			type BOOLEAN
			count 1
		}
	}
	control.24 {
		iface MIXER
		name 'HPL Output Mixer IN1_L Switch'
		value false
		comment {
			access 'read write'
			type BOOLEAN
			count 1
		}
	}
	control.25 {
		iface MIXER
		name 'LOL Output Mixer L_DAC Switch'
		value true
		comment {
			access 'read write'
			type BOOLEAN
			count 1
		}
	}
	control.26 {
		iface MIXER
		name 'HPR Output Mixer R_DAC Switch'
		value false
		comment {
			access 'read write'
			type BOOLEAN
			count 1
		}
	}
	control.27 {
		iface MIXER
		name 'HPR Output Mixer IN1_R Switch'
		value false
		comment {
			access 'read write'
			type BOOLEAN
			count 1
		}
	}
	control.28 {
		iface MIXER
		name 'LOR Output Mixer R_DAC Switch'
		value true
		comment {
			access 'read write'
			type BOOLEAN
			count 1
		}
	}
	control.29 {
		iface MIXER
		name 'IN1_R to Right Mixer Positive Resistor'
		value Off
		comment {
			access 'read write'
			type ENUMERATED
			count 1
			item.0 Off
			item.1 '10 kOhm'
			item.2 '20 kOhm'
			item.3 '40 kOhm'
		}
	}
	control.30 {
		iface MIXER
		name 'IN2_R to Right Mixer Positive Resistor'
		value Off
		comment {
			access 'read write'
			type ENUMERATED
			count 1
			item.0 Off
			item.1 '10 kOhm'
			item.2 '20 kOhm'
			item.3 '40 kOhm'
		}
	}
	control.31 {
		iface MIXER
		name 'IN3_R to Right Mixer Positive Resistor'
		value Off
		comment {
			access 'read write'
			type ENUMERATED
			count 1
			item.0 Off
			item.1 '10 kOhm'
			item.2 '20 kOhm'
			item.3 '40 kOhm'
		}
	}
	control.32 {
		iface MIXER
		name 'IN2_L to Right Mixer Positive Resistor'
		value Off
		comment {
			access 'read write'
			type ENUMERATED
			count 1
			item.0 Off
			item.1 '10 kOhm'
			item.2 '20 kOhm'
			item.3 '40 kOhm'
		}
	}
	control.33 {
		iface MIXER
		name 'IN1_L to Right Mixer Negative Resistor'
		value Off
		comment {
			access 'read write'
			type ENUMERATED
			count 1
			item.0 Off
			item.1 '10 kOhm'
			item.2 '20 kOhm'
			item.3 '40 kOhm'
		}
	}
	control.34 {
		iface MIXER
		name 'IN3_L to Right Mixer Negative Resistor'
		value Off
		comment {
			access 'read write'
			type ENUMERATED
			count 1
			item.0 Off
			item.1 '10 kOhm'
			item.2 '20 kOhm'
			item.3 '40 kOhm'
		}
	}
	control.35 {
		iface MIXER
		name 'IN1_L to Left Mixer Positive Resistor'
		value Off
		comment {
			access 'read write'
			type ENUMERATED
			count 1
			item.0 Off
			item.1 '10 kOhm'
			item.2 '20 kOhm'
			item.3 '40 kOhm'
		}
	}
	control.36 {
		iface MIXER
		name 'IN2_L to Left Mixer Positive Resistor'
		value Off
		comment {
			access 'read write'
			type ENUMERATED
			count 1
			item.0 Off
			item.1 '10 kOhm'
			item.2 '20 kOhm'
			item.3 '40 kOhm'
		}
	}
	control.37 {
		iface MIXER
		name 'IN3_L to Left Mixer Positive Resistor'
		value Off
		comment {
			access 'read write'
			type ENUMERATED
			count 1
			item.0 Off
			item.1 '10 kOhm'
			item.2 '20 kOhm'
			item.3 '40 kOhm'
		}
	}
	control.38 {
		iface MIXER
		name 'IN1_R to Left Mixer Positive Resistor'
		value Off
		comment {
			access 'read write'
			type ENUMERATED
			count 1
			item.0 Off
			item.1 '10 kOhm'
			item.2 '20 kOhm'
			item.3 '40 kOhm'
		}
	}
	control.39 {
		iface MIXER
		name 'IN2_R to Left Mixer Negative Resistor'
		value Off
		comment {
			access 'read write'
			type ENUMERATED
			count 1
			item.0 Off
			item.1 '10 kOhm'
			item.2 '20 kOhm'
			item.3 '40 kOhm'
		}
	}
	control.40 {
		iface MIXER
		name 'IN3_R to Left Mixer Negative Resistor'
		value Off
		comment {
			access 'read write'
			type ENUMERATED
			count 1
			item.0 Off
			item.1 '10 kOhm'
			item.2 '20 kOhm'
			item.3 '40 kOhm'
		}
	}
}
alsa.1795384041.txt.gz · Last modified: by qkb