#!/bin/sh

set -e

ODROID_PA_CONFIG="/etc/pulse/default.pa.d/odroid-alsa.pa"

. /usr/share/odroid-base/library.sh

sound_card_model() {
	if [ -f /proc/device-tree/sound/model 2>/dev/null ]; then
		echo $(tr -d '\0' < /proc/device-tree/sound/model)
	fi
}

sound_card_driver() {
	local uevent_file="/sys/class/sound/card${1:-0}/device/uevent"

	[ -f "$uevent_file" ] && \
		cat "$uevent_file" | grep DRIVER | cut -d'=' -f2
}

sound_card_id() {
	local uevent_file="/sys/class/sound/card${1:-0}/id"
	[ -f "$uevent_file" ] && cat "$uevent_file"
}

alsa_setup() {
	local card_id=$(sound_card_id)
	[ -n "${card_id}" ] && option="-c ${card_id}"

	case "$(odroid_model_simple)" in
		odroidn2 | odroidn2_plus)
			major=$(uname -r | cut -d'.' -f1)
			[ "x$major" = "x4" ] && return

			amixer ${option} -q sset 'FRDDR_A SINK 1 SEL' 'OUT 1' || true
			amixer ${option} -q sset 'FRDDR_A SRC 1 EN' 'on' || true
			amixer ${option} -q sset 'TDMOUT_B SRC SEL' 'IN 0' || true
			amixer ${option} -q sset 'TOHDMITX I2S SRC' 'I2S B' || true
			amixer ${option} -q sset 'TOHDMITX' 'on' || true

			amixer ${option} -q sset 'FRDDR_B SINK 1 SEL' 'OUT 2' || true
			amixer ${option} -q sset 'FRDDR_B SRC 1 EN' 'on' || true
			amixer ${option} -q sset 'TDMOUT_C SRC SEL' 'IN 1' || true
			amixer ${option} -q sset 'TOACODEC SRC' 'I2S C' || true
			amixer ${option} -q sset 'TOACODEC OUT EN' 'on' || true
			amixer ${option} -q sset 'TOACODEC Lane Select' '0' || true
			amixer ${option} -q sset 'ACODEC' '255'

			amixer ${option} -q sset 'FRDDR_C SINK 1 SEL' 'OUT 3' || true
			amixer ${option} -q sset 'FRDDR_C SRC 1 EN' 'on' || true
			amixer ${option} -q sset 'SPDIFOUT SRC SEL' 'IN 2' || true
			;;

		odroidn2l)
			major=$(uname -r | cut -d'.' -f1)
			[ "x$major" = "x4" ] && return

			amixer ${option} -q sset 'FRDDR_A SINK 1 SEL' 'OUT 1' || true
			amixer ${option} -q sset 'FRDDR_A SRC 1 EN' 'on' || true
			amixer ${option} -q sset 'TDMOUT_B SRC SEL' 'IN 0' || true
			amixer ${option} -q sset 'TOHDMITX I2S SRC' 'I2S B' || true
			amixer ${option} -q sset 'TOHDMITX' 'on' || true

			amixer ${option} -q sset 'FRDDR_B SINK 1 SEL' 'OUT 2' || true
			amixer ${option} -q sset 'FRDDR_B SRC 1 EN' 'on' || true
			amixer ${option} -q sset 'TDMOUT_C SRC SEL' 'IN 1' || true
			amixer ${option} -q sset 'TOACODEC SRC' 'I2S C' || true
			amixer ${option} -q sset 'TOACODEC OUT EN' 'on' || true
			amixer ${option} -q sset 'TOACODEC Lane Select' '0' || true
			amixer ${option} -q sset 'ACODEC' '255'
			;;

		odroidc4 | odroidhc4)
			major=$(uname -r | cut -d'.' -f1)
			[ "x$major" = "x4" ] && return

			amixer ${option} -q sset 'FRDDR_A SINK 1 SEL' 'OUT 1' || true
			amixer ${option} -q sset 'FRDDR_A SRC 1 EN' 'on' || true
			amixer ${option} -q sset 'TDMOUT_B SRC SEL' 'IN 0' || true
			amixer ${option} -q sset 'TOHDMITX I2S SRC' 'I2S B' || true
			amixer ${option} -q sset 'TOHDMITX' 'on' || true
			;;

		odroidc5)
			major=$(uname -r | cut -d'.' -f1)
			[ "x$major" = "x4" ] && return

			amixer -q sset 'HDMITX Audio Source Select' 'Tdm_B' || true
			amixer -q sset 'AMP DAC Digital' '254' || true
			;;
		*)
			;;
	esac
}

pulseaudio_setup() {
	[ -f "${ODROID_PA_CONFIG}" ] && return

	mkdir -p "$(dirname ${ODROID_PA_CONFIG})"

	case "$(odroid_model_simple)" in
		odroidc5)
			cat>"${ODROID_PA_CONFIG}"<<__EOF
			load-module module-alsa-sink device=hw:0,1 sink_name=spdif2hdmi sink_properties=device.description=HDMI-SPDIF
			load-module module-alsa-sink device=hw:0,2 sink_name=line sink_properties=device.description=Line-out
			load-module module-alsa-sink device=hw:0,3 sink_name=spdif sink_properties=device.description=SPDIF-out
			set-default-sink alsa_output.platform-auge_sound.stereo-fallback
__EOF
			;;
		odroidm1 | odroidm1s)
			cat>"${ODROID_PA_CONFIG}"<<__EOF
			set-card-profile 0 output:hdmi-stereo
			set-default-sink alsa_output.platform-hdmi-sound.hdmi-stereo
__EOF
			;;
		odroidm2)
			cat>"${ODROID_PA_CONFIG}"<<__EOF
			set-card-profile 0 output:hdmi-stereo
			set-default-sink alsa_output.platform-hdmi0-sound.hdmi-stereo
__EOF
			;;
		*)
			cat>"${ODROID_PA_CONFIG}"<<__EOF
			set-card-profile 0 output:hdmi-stereo
__EOF
			;;
	esac

	if [ -f /usr/share/pulseaudio/alsa-mixer/paths/hdmi-output-0.conf ]; then
		sed -i "s/^priority =.*/priority = 100/g" \
			/usr/share/pulseaudio/alsa-mixer/paths/hdmi-output-0.conf
	fi
}

alsa_ucm_setup() {
	local model=$(sound_card_model)		# ex) ODROID-N2
	local cardid=$(sound_card_id)		# ex) ODROIDN2
	local driver=$(sound_card_driver)
	local umc_source="/usr/share/alsa/ucm2/Hardkernel/${model}/${model}.conf"
	local conf_dir="/usr/share/alsa/ucm2/conf.d/${driver}"

	mkdir -p "$(dirname "${umc_source}")"
	mkdir -p "${conf_dir}"

	rm -f "${conf_dir}/${model}.conf"
	rm -f "${conf_dir}/${cardid}.conf"

	ln -s "${umc_source}" "${conf_dir}/${model}.conf"
	ln -s "${umc_source}" "${conf_dir}/${cardid}.conf"
}
