Загрузка данных


static int sgtl5000_route_input(const struct device *dev, audio_channel_t channel, uint32_t input)
{
	/* Verify that the requested channel type is valid for an input stream */
	if (channel != AUDIO_CHANNEL_SIDE_LEFT && 
	    channel != AUDIO_CHANNEL_SIDE_RIGHT && 
	    channel != AUDIO_CHANNEL_ALL) {
		return -EINVAL;
	}

	/* 
	 * The SGTL5000 hardware features a single Capture Mux shared between both channels.
	 * Switching is performed in the SGTL5000_CHIP_ANA_CTRL register, bit 2 (SEL_ADC).
	 * 0 = MIC_IN, 1 = LINE_IN
	 */
	switch ((sgtl5000_input_t)input) {
	case SGTL5000_INPUT_MIC:
		LOG_INF("Routing Input (Channel %d): MIC_IN", channel);
		/* Clear bit 2 to select MIC_IN */
		return sgtl5000_update_reg(dev, SGTL5000_CHIP_ANA_CTRL, 0x0004, 0);

	case SGTL5000_INPUT_LINE:
		LOG_INF("Routing Input (Channel %d): LINE_IN", channel);
		/* Set bit 2 (0x0004) to select LINE_IN */
		return sgtl5000_update_reg(dev, SGTL5000_CHIP_ANA_CTRL, 0x0004, 0x0004);

	default:
		LOG_ERR("Invalid input source ID: %d", input);
		return -EINVAL;
	}
}

static int sgtl5000_route_output(const struct device *dev, audio_channel_t channel, uint32_t output)
{
	/* Verify that the requested channel type is valid for an output stream */
	if (channel != AUDIO_CHANNEL_FRONT_LEFT && 
	    channel != AUDIO_CHANNEL_FRONT_RIGHT && 
	    channel != AUDIO_CHANNEL_HEADPHONE_LEFT &&
	    channel != AUDIO_CHANNEL_HEADPHONE_RIGHT &&
	    channel != AUDIO_CHANNEL_ALL) {
		return -EINVAL;
	}

	/* 
	 * Output stage management on SGTL5000:
	 * 1. Headphone Source Selection: ANA_CTRL register, bit 6 (HP_SEL): 0 = DAC, 1 = LINE_IN.
	 *    We hardcode this to DAC (0) as the standard digital playback configuration.
	 * 2. Power state control for HP and LINE_OUT blocks inside the CHIP_ANA_POWER register.
	 */
	switch ((sgtl5000_output_t)output) {
	case SGTL5000_OUTPUT_HEADPHONE:
		LOG_INF("Routing Output: Headphones Only (DAC -> HP)");
		/* Route DAC to Headphones (HP_SEL = 0) */
		sgtl5000_update_reg(dev, SGTL5000_CHIP_ANA_CTRL, SGTL5000_HP_SEL_MASK, 0);
		/* Power up HP stage and VAG reference, power down LINE_OUT stage */
		return sgtl5000_update_reg(dev, SGTL5000_CHIP_ANA_POWER, 
					   SGTL5000_HP_POWERUP | SGTL5000_LINE_OUT_POWERUP | SGTL5000_VAG_POWERUP, 
					   SGTL5000_HP_POWERUP | SGTL5000_VAG_POWERUP);

	case SGTL5000_OUTPUT_LINE:
		LOG_INF("Routing Output: Line-Out Only (DAC -> LO)");
		/* Power up LINE_OUT stage and VAG reference, power down HP stage */
		return sgtl5000_update_reg(dev, SGTL5000_CHIP_ANA_POWER, 
					   SGTL5000_HP_POWERUP | SGTL5000_LINE_OUT_POWERUP | SGTL5000_VAG_POWERUP, 
					   SGTL5000_LINE_OUT_POWERUP | SGTL5000_VAG_POWERUP);

	case SGTL5000_OUTPUT_ALL:
		LOG_INF("Routing Output: Simultaneous (HP & LO Enabled)");
		/* Route DAC to Headphones (HP_SEL = 0) */
		sgtl5000_update_reg(dev, SGTL5000_CHIP_ANA_CTRL, SGTL5000_HP_SEL_MASK, 0);
		/* Power up all available output stages and VAG reference */
		return sgtl5000_update_reg(dev, SGTL5000_CHIP_ANA_POWER, 
					   SGTL5000_HP_POWERUP | SGTL5000_LINE_OUT_POWERUP | SGTL5000_VAG_POWERUP, 
					   SGTL5000_HP_POWERUP | SGTL5000_LINE_OUT_POWERUP | SGTL5000_VAG_POWERUP);

	default:
		LOG_ERR("Invalid output destination ID: %d", output);
		return -EINVAL;
	}
}