// SPDX-License-Identifier: GPL-2.0-only
//
// rt1320-sdw.c -- rt1320 SDCA ALSA SoC amplifier audio driver
//
// Copyright(c) 2024 Realtek Semiconductor Corp.
//
//
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/pm_runtime.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include <linux/dmi.h>
#include <linux/firmware.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc-dapm.h>
#include <sound/initval.h>
#include <sound/tlv.h>
#include <sound/sdw.h>
#include "rt1320-sdw.h"
#include "rt-sdw-common.h"
/*
* The 'blind writes' is an SDCA term to deal with platform-specific initialization.
* It might include vendor-specific or SDCA control registers.
*/
static const struct reg_sequence rt1320_blind_write[] = {
{ 0 xc003, 0 xe0 },
{ 0 xc01b, 0 xfc },
{ 0 xc5c3, 0 xf2 },
{ 0 xc5c2, 0 x00 },
{ 0 xc5c6, 0 x10 },
{ 0 xc5c4, 0 x12 },
{ 0 xc5c8, 0 x03 },
{ 0 xc5d8, 0 x0a },
{ 0 xc5f7, 0 x22 },
{ 0 xc5f6, 0 x22 },
{ 0 xc5d0, 0 x0f },
{ 0 xc5d1, 0 x89 },
{ 0 xc057, 0 x51 },
{ 0 xc054, 0 x35 },
{ 0 xc053, 0 x55 },
{ 0 xc052, 0 x55 },
{ 0 xc051, 0 x13 },
{ 0 xc050, 0 x15 },
{ 0 xc060, 0 x77 },
{ 0 xc061, 0 x55 },
{ 0 xc063, 0 x55 },
{ 0 xc065, 0 xa5 },
{ 0 xc06b, 0 x0a },
{ 0 xca05, 0 xd6 },
{ 0 xca25, 0 xd6 },
{ 0 xcd00, 0 x05 },
{ 0 xc604, 0 x40 },
{ 0 xc609, 0 x40 },
{ 0 xc046, 0 xff },
{ 0 xc045, 0 xff },
{ 0 xc044, 0 xff },
{ 0 xc043, 0 xff },
{ 0 xc042, 0 xff },
{ 0 xc041, 0 xff },
{ 0 xc040, 0 xff },
{ 0 xcc10, 0 x01 },
{ 0 xc700, 0 xf0 },
{ 0 xc701, 0 x13 },
{ 0 xc901, 0 x04 },
{ 0 xc900, 0 x73 },
{ 0 xde03, 0 x05 },
{ 0 xdd0b, 0 x0d },
{ 0 xdd0a, 0 xff },
{ 0 xdd09, 0 x0d },
{ 0 xdd08, 0 xff },
{ 0 xc570, 0 x08 },
{ 0 xe803, 0 xbe },
{ 0 xc003, 0 xc0 },
{ 0 xc081, 0 xfe },
{ 0 xce31, 0 x0d },
{ 0 xce30, 0 xae },
{ 0 xce37, 0 x0b },
{ 0 xce36, 0 xd2 },
{ 0 xce39, 0 x04 },
{ 0 xce38, 0 x80 },
{ 0 xce3f, 0 x00 },
{ 0 xce3e, 0 x00 },
{ 0 xd470, 0 x8b },
{ 0 xd471, 0 x18 },
{ 0 xc019, 0 x10 },
{ 0 xd487, 0 x3f },
{ 0 xd486, 0 xc3 },
{ 0 x3fc2bfc7, 0 x00 },
{ 0 x3fc2bfc6, 0 x00 },
{ 0 x3fc2bfc5, 0 x00 },
{ 0 x3fc2bfc4, 0 x01 },
{ 0 x0000d486, 0 x43 },
{ 0 x1000db00, 0 x02 },
{ 0 x1000db01, 0 x00 },
{ 0 x1000db02, 0 x11 },
{ 0 x1000db03, 0 x00 },
{ 0 x1000db04, 0 x00 },
{ 0 x1000db05, 0 x82 },
{ 0 x1000db06, 0 x04 },
{ 0 x1000db07, 0 xf1 },
{ 0 x1000db08, 0 x00 },
{ 0 x1000db09, 0 x00 },
{ 0 x1000db0a, 0 x40 },
{ 0 x0000d540, 0 x01 },
{ 0 xd172, 0 x2a },
{ 0 xc5d6, 0 x01 },
{ 0 xd478, 0 xff },
};
static const struct reg_sequence rt1320_vc_blind_write[] = {
{ 0 xc003, 0 xe0 },
{ 0 xe80a, 0 x01 },
{ 0 xc5c3, 0 xf3 },
{ 0 xc057, 0 x51 },
{ 0 xc054, 0 x35 },
{ 0 xca05, 0 xd6 },
{ 0 xca07, 0 x07 },
{ 0 xca25, 0 xd6 },
{ 0 xca27, 0 x07 },
{ 0 xc604, 0 x40 },
{ 0 xc609, 0 x40 },
{ 0 xc046, 0 xff },
{ 0 xc045, 0 xff },
{ 0 xda81, 0 x14 },
{ 0 xda8d, 0 x14 },
{ 0 xc044, 0 xff },
{ 0 xc043, 0 xff },
{ 0 xc042, 0 xff },
{ 0 xc041, 0 x7f },
{ 0 xc040, 0 xff },
{ 0 xcc10, 0 x01 },
{ 0 xc700, 0 xf0 },
{ 0 xc701, 0 x13 },
{ 0 xc901, 0 x09 },
{ 0 xc900, 0 xd0 },
{ 0 xde03, 0 x05 },
{ 0 xdd0b, 0 x0d },
{ 0 xdd0a, 0 xff },
{ 0 xdd09, 0 x0d },
{ 0 xdd08, 0 xff },
{ 0 xc570, 0 x08 },
{ 0 xc086, 0 x02 },
{ 0 xc085, 0 x7f },
{ 0 xc084, 0 x00 },
{ 0 xc081, 0 xfe },
{ 0 xf084, 0 x0f },
{ 0 xf083, 0 xff },
{ 0 xf082, 0 xff },
{ 0 xf081, 0 xff },
{ 0 xf080, 0 xff },
{ 0 xe802, 0 xf8 },
{ 0 xe803, 0 xbe },
{ 0 xc003, 0 xc0 },
{ 0 xd470, 0 xec },
{ 0 xd471, 0 x3a },
{ 0 xd474, 0 x11 },
{ 0 xd475, 0 x32 },
{ 0 xd478, 0 xff },
{ 0 xd479, 0 x20 },
{ 0 xd47a, 0 x10 },
{ 0 xd47c, 0 xff },
{ 0 xc019, 0 x10 },
{ 0 xd487, 0 x0b },
{ 0 xd487, 0 x3b },
{ 0 xd486, 0 xc3 },
{ 0 xc598, 0 x04 },
{ 0 xdb03, 0 xf0 },
{ 0 xdb09, 0 x00 },
{ 0 xdb08, 0 x7a },
{ 0 xdb19, 0 x02 },
{ 0 xdb07, 0 x5a },
{ 0 xdb05, 0 x45 },
{ 0 xd500, 0 x00 },
{ 0 xd500, 0 x17 },
{ 0 xd600, 0 x01 },
{ 0 xd601, 0 x02 },
{ 0 xd602, 0 x03 },
{ 0 xd603, 0 x04 },
{ 0 xd64c, 0 x03 },
{ 0 xd64d, 0 x03 },
{ 0 xd64e, 0 x03 },
{ 0 xd64f, 0 x03 },
{ 0 xd650, 0 x03 },
{ 0 xd651, 0 x03 },
{ 0 xd652, 0 x03 },
{ 0 xd610, 0 x01 },
{ 0 xd608, 0 x03 },
{ 0 xd609, 0 x00 },
{ 0 x3fc2bf83, 0 x00 },
{ 0 x3fc2bf82, 0 x00 },
{ 0 x3fc2bf81, 0 x00 },
{ 0 x3fc2bf80, 0 x00 },
{ 0 x3fc2bfc7, 0 x00 },
{ 0 x3fc2bfc6, 0 x00 },
{ 0 x3fc2bfc5, 0 x00 },
{ 0 x3fc2bfc4, 0 x00 },
{ 0 x3fc2bfc3, 0 x00 },
{ 0 x3fc2bfc2, 0 x00 },
{ 0 x3fc2bfc1, 0 x00 },
{ 0 x3fc2bfc0, 0 x03 },
{ 0 x0000d486, 0 x43 },
{ SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE23, RT1320_SDCA_CTL_REQ_POWER_STATE, 0 ), 0 x00 },
{ 0 x1000db00, 0 x07 },
{ 0 x1000db01, 0 x00 },
{ 0 x1000db02, 0 x11 },
{ 0 x1000db03, 0 x00 },
{ 0 x1000db04, 0 x00 },
{ 0 x1000db05, 0 x82 },
{ 0 x1000db06, 0 x04 },
{ 0 x1000db07, 0 xf1 },
{ 0 x1000db08, 0 x00 },
{ 0 x1000db09, 0 x00 },
{ 0 x1000db0a, 0 x40 },
{ 0 x1000db0b, 0 x02 },
{ 0 x1000db0c, 0 xf2 },
{ 0 x1000db0d, 0 x00 },
{ 0 x1000db0e, 0 x00 },
{ 0 x1000db0f, 0 xe0 },
{ 0 x1000db10, 0 x00 },
{ 0 x1000db11, 0 x10 },
{ 0 x1000db12, 0 x00 },
{ 0 x1000db13, 0 x00 },
{ 0 x1000db14, 0 x45 },
{ 0 x1000db15, 0 x0d },
{ 0 x1000db16, 0 x01 },
{ 0 x1000db17, 0 x00 },
{ 0 x1000db18, 0 x00 },
{ 0 x1000db19, 0 xbf },
{ 0 x1000db1a, 0 x13 },
{ 0 x1000db1b, 0 x09 },
{ 0 x1000db1c, 0 x00 },
{ 0 x1000db1d, 0 x00 },
{ 0 x1000db1e, 0 x00 },
{ 0 x1000db1f, 0 x12 },
{ 0 x1000db20, 0 x09 },
{ 0 x1000db21, 0 x00 },
{ 0 x1000db22, 0 x00 },
{ 0 x1000db23, 0 x00 },
{ 0 x0000d540, 0 x01 },
{ 0 x0000c081, 0 xfc },
{ 0 x0000f01e, 0 x80 },
{ 0 xc01b, 0 xfc },
{ 0 xc5d1, 0 x89 },
{ 0 xc5d8, 0 x0a },
{ 0 xc5f7, 0 x22 },
{ 0 xc5f6, 0 x22 },
{ 0 xc065, 0 xa5 },
{ 0 xc06b, 0 x0a },
{ 0 xd172, 0 x2a },
{ 0 xc5d6, 0 x01 },
{ SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE23, RT1320_SDCA_CTL_REQ_POWER_STATE, 0 ), 0 x03 },
};
static const struct reg_default rt1320_reg_defaults[] = {
{ SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_PDE11, RT1320_SDCA_CTL_REQ_POWER_STATE, 0 ), 0 x03 },
{ SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU113, RT1320_SDCA_CTL_FU_MUTE, CH_01), 0 x01 },
{ SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU113, RT1320_SDCA_CTL_FU_MUTE, CH_02), 0 x01 },
{ SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU14, RT1320_SDCA_CTL_FU_MUTE, CH_01), 0 x01 },
{ SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU14, RT1320_SDCA_CTL_FU_MUTE, CH_02), 0 x01 },
{ SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_CS113, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX, 0 ), 0 x09 },
{ SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_CS14, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX, 0 ), 0 x0b },
{ SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_PDE11, RT1320_SDCA_CTL_ACTUAL_POWER_STATE, 0 ), 0 x03 },
{ SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_FU21, RT1320_SDCA_CTL_FU_MUTE, CH_01), 0 x01 },
{ SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_FU21, RT1320_SDCA_CTL_FU_MUTE, CH_02), 0 x01 },
{ SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE27, RT1320_SDCA_CTL_REQ_POWER_STATE, 0 ), 0 x03 },
{ SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE23, RT1320_SDCA_CTL_REQ_POWER_STATE, 0 ), 0 x03 },
{ SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PPU21, RT1320_SDCA_CTL_POSTURE_NUMBER, 0 ), 0 x00 },
{ SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_CS21, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX, 0 ), 0 x09 },
{ SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE23, RT1320_SDCA_CTL_ACTUAL_POWER_STATE, 0 ), 0 x03 },
};
static const struct reg_default rt1320_mbq_defaults[] = {
{ SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU113, RT1320_SDCA_CTL_FU_VOLUME, CH_01), 0 x0000 },
{ SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU113, RT1320_SDCA_CTL_FU_VOLUME, CH_02), 0 x0000 },
{ SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU14, RT1320_SDCA_CTL_FU_VOLUME, CH_01), 0 x0000 },
{ SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU14, RT1320_SDCA_CTL_FU_VOLUME, CH_02), 0 x0000 },
{ SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_FU21, RT1320_SDCA_CTL_FU_VOLUME, CH_01), 0 x0000 },
{ SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_FU21, RT1320_SDCA_CTL_FU_VOLUME, CH_02), 0 x0000 },
};
static bool rt1320_readable_register(struct device *dev, unsigned int reg)
{
switch (reg) {
case 0 xc000 ... 0 xc086:
case 0 xc400 ... 0 xc409:
case 0 xc480 ... 0 xc48f:
case 0 xc4c0 ... 0 xc4c4:
case 0 xc4e0 ... 0 xc4e7:
case 0 xc500:
case 0 xc560 ... 0 xc56b:
case 0 xc570:
case 0 xc580 ... 0 xc59a:
case 0 xc5b0 ... 0 xc60f:
case 0 xc640 ... 0 xc64f:
case 0 xc670:
case 0 xc680 ... 0 xc683:
case 0 xc700 ... 0 xc76f:
case 0 xc800 ... 0 xc801:
case 0 xc820:
case 0 xc900 ... 0 xc901:
case 0 xc920 ... 0 xc921:
case 0 xca00 ... 0 xca07:
case 0 xca20 ... 0 xca27:
case 0 xca40 ... 0 xca4b:
case 0 xca60 ... 0 xca68:
case 0 xca80 ... 0 xca88:
case 0 xcb00 ... 0 xcb0c:
case 0 xcc00 ... 0 xcc12:
case 0 xcc80 ... 0 xcc81:
case 0 xcd00:
case 0 xcd80 ... 0 xcd82:
case 0 xce00 ... 0 xce4d:
case 0 xcf00 ... 0 xcf25:
case 0 xd000 ... 0 xd0ff:
case 0 xd100 ... 0 xd1ff:
case 0 xd200 ... 0 xd2ff:
case 0 xd300 ... 0 xd3ff:
case 0 xd400 ... 0 xd403:
case 0 xd410 ... 0 xd417:
case 0 xd470 ... 0 xd497:
case 0 xd4dc ... 0 xd50f:
case 0 xd520 ... 0 xd543:
case 0 xd560 ... 0 xd5ef:
case 0 xd600 ... 0 xd663:
case 0 xda00 ... 0 xda6e:
case 0 xda80 ... 0 xda9e:
case 0 xdb00 ... 0 xdb7f:
case 0 xdc00:
case 0 xdc20 ... 0 xdc21:
case 0 xdd00 ... 0 xdd17:
case 0 xde00 ... 0 xde09:
case 0 xdf00 ... 0 xdf1b:
case 0 xe000 ... 0 xe847:
case 0 xf01e:
case 0 xf717 ... 0 xf719:
case 0 xf720 ... 0 xf723:
case 0 x1000cd91 ... 0 x1000cd96:
case 0 x1000f008:
case 0 x1000f021:
case 0 x3fe2e000 ... 0 x3fe2e003:
case 0 x3fc2ab80 ... 0 x3fc2abd4:
/* 0x40801508/0x40801809/0x4080180a/0x40801909/0x4080190a */
case SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_PDE11, RT1320_SDCA_CTL_REQ_POWER_STATE, 0 ):
case SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU113, RT1320_SDCA_CTL_FU_MUTE, CH_01):
case SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU113, RT1320_SDCA_CTL_FU_MUTE, CH_02):
case SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU14, RT1320_SDCA_CTL_FU_MUTE, CH_01):
case SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU14, RT1320_SDCA_CTL_FU_MUTE, CH_02):
/* 0x40880900/0x40880980 */
case SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_CS113, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX, 0 ):
case SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_CS14, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX, 0 ):
/* 0x40881500 */
case SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_PDE11, RT1320_SDCA_CTL_ACTUAL_POWER_STATE, 0 ):
/* 0x41000189/0x4100018a */
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_FU21, RT1320_SDCA_CTL_FU_MUTE, CH_01):
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_FU21, RT1320_SDCA_CTL_FU_MUTE, CH_02):
/* 0x41001388 */
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE27, RT1320_SDCA_CTL_REQ_POWER_STATE, 0 ):
/* 0x41001988 */
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE23, RT1320_SDCA_CTL_REQ_POWER_STATE, 0 ):
/* 0x41080000 */
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT0, RT1320_SDCA_CTL_FUNC_STATUS, 0 ):
/* 0x41080200 */
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PPU21, RT1320_SDCA_CTL_POSTURE_NUMBER, 0 ):
/* 0x41080900 */
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_CS113, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX, 0 ):
/* 0x41080980 */
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_CS14, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX, 0 ):
/* 0x41081080 */
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_CS21, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX, 0 ):
/* 0x41081480/0x41081488 */
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_SAPU, RT1320_SDCA_CTL_SAPU_PROTECTION_MODE, 0 ):
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_SAPU, RT1320_SDCA_CTL_SAPU_PROTECTION_STATUS, 0 ):
/* 0x41081980 */
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE23, RT1320_SDCA_CTL_ACTUAL_POWER_STATE, 0 ):
return true ;
default :
return false ;
}
}
static bool rt1320_volatile_register(struct device *dev, unsigned int reg)
{
switch (reg) {
case 0 xc000:
case 0 xc003:
case 0 xc081:
case 0 xc402 ... 0 xc406:
case 0 xc48c ... 0 xc48f:
case 0 xc560:
case 0 xc5b5 ... 0 xc5b7:
case 0 xc5fc ... 0 xc5ff:
case 0 xc820:
case 0 xc900:
case 0 xc920:
case 0 xca42:
case 0 xca62:
case 0 xca82:
case 0 xcd00:
case 0 xce03:
case 0 xce10:
case 0 xce14 ... 0 xce17:
case 0 xce44 ... 0 xce49:
case 0 xce4c ... 0 xce4d:
case 0 xcf0c:
case 0 xcf10 ... 0 xcf25:
case 0 xd486 ... 0 xd487:
case 0 xd4e5 ... 0 xd4e6:
case 0 xd4e8 ... 0 xd4ff:
case 0 xd530:
case 0 xd540:
case 0 xd543:
case 0 xdb58 ... 0 xdb5f:
case 0 xdb60 ... 0 xdb63:
case 0 xdb68 ... 0 xdb69:
case 0 xdb6d:
case 0 xdb70 ... 0 xdb71:
case 0 xdb76:
case 0 xdb7a:
case 0 xdb7c ... 0 xdb7f:
case 0 xdd0c ... 0 xdd13:
case 0 xde02:
case 0 xdf14 ... 0 xdf1b:
case 0 xe83c ... 0 xe847:
case 0 xf01e:
case 0 xf717 ... 0 xf719:
case 0 xf720 ... 0 xf723:
case 0 x10000000 ... 0 x10007fff:
case 0 x1000c000 ... 0 x1000dfff:
case 0 x1000f008:
case 0 x1000f021:
case 0 x3fc2ab80 ... 0 x3fc2abd4:
case 0 x3fc2bf80 ... 0 x3fc2bf83:
case 0 x3fc2bfc0 ... 0 x3fc2bfc7:
case 0 x3fe2e000 ... 0 x3fe2e003:
case SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_PDE11, RT1320_SDCA_CTL_ACTUAL_POWER_STATE, 0 ):
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT0, RT1320_SDCA_CTL_FUNC_STATUS, 0 ):
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_SAPU, RT1320_SDCA_CTL_SAPU_PROTECTION_MODE, 0 ):
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_SAPU, RT1320_SDCA_CTL_SAPU_PROTECTION_STATUS, 0 ):
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE23, RT1320_SDCA_CTL_ACTUAL_POWER_STATE, 0 ):
return true ;
default :
return false ;
}
}
static bool rt1320_mbq_readable_register(struct device *dev, unsigned int reg)
{
switch (reg) {
case SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU113, RT1320_SDCA_CTL_FU_VOLUME, CH_01):
case SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU113, RT1320_SDCA_CTL_FU_VOLUME, CH_02):
case SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU14, RT1320_SDCA_CTL_FU_VOLUME, CH_01):
case SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU14, RT1320_SDCA_CTL_FU_VOLUME, CH_02):
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_FU21, RT1320_SDCA_CTL_FU_VOLUME, CH_01):
case SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_FU21, RT1320_SDCA_CTL_FU_VOLUME, CH_02):
return true ;
default :
return false ;
}
}
static const struct regmap_config rt1320_sdw_regmap = {
.reg_bits = 32 ,
.val_bits = 8 ,
.readable_reg = rt1320_readable_register,
.volatile_reg = rt1320_volatile_register,
.max_register = 0 x41081980,
.reg_defaults = rt1320_reg_defaults,
.num_reg_defaults = ARRAY_SIZE(rt1320_reg_defaults),
.cache_type = REGCACHE_MAPLE,
.use_single_read = true ,
.use_single_write = true ,
};
static const struct regmap_config rt1320_mbq_regmap = {
.name = "sdw-mbq" ,
.reg_bits = 32 ,
.val_bits = 16 ,
.readable_reg = rt1320_mbq_readable_register,
.max_register = 0 x41000192,
.reg_defaults = rt1320_mbq_defaults,
.num_reg_defaults = ARRAY_SIZE(rt1320_mbq_defaults),
.cache_type = REGCACHE_MAPLE,
.use_single_read = true ,
.use_single_write = true ,
};
static int rt1320_read_prop(struct sdw_slave *slave)
{
struct sdw_slave_prop *prop = &slave->prop;
int nval;
int i, j;
u32 bit;
unsigned long addr;
struct sdw_dpn_prop *dpn;
/*
* Due to support the multi-lane, we call 'sdw_slave_read_prop' to get the lane mapping
*/
sdw_slave_read_prop(slave);
prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;
prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
prop->paging_support = true ;
prop->lane_control_support = true ;
/* first we need to allocate memory for set bits in port lists */
prop->source_ports = BIT(4 ) | BIT(8 ) | BIT(10 );
prop->sink_ports = BIT(1 );
nval = hweight32(prop->source_ports);
prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
sizeof (*prop->src_dpn_prop), GFP_KERNEL);
if (!prop->src_dpn_prop)
return -ENOMEM;
i = 0 ;
dpn = prop->src_dpn_prop;
addr = prop->source_ports;
for_each_set_bit(bit, &addr, 32 ) {
dpn[i].num = bit;
dpn[i].type = SDW_DPN_FULL;
dpn[i].simple_ch_prep_sm = true ;
dpn[i].ch_prep_timeout = 10 ;
i++;
}
/* do this again for sink now */
nval = hweight32(prop->sink_ports);
prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
sizeof (*prop->sink_dpn_prop), GFP_KERNEL);
if (!prop->sink_dpn_prop)
return -ENOMEM;
j = 0 ;
dpn = prop->sink_dpn_prop;
addr = prop->sink_ports;
for_each_set_bit(bit, &addr, 32 ) {
dpn[j].num = bit;
dpn[j].type = SDW_DPN_FULL;
dpn[j].simple_ch_prep_sm = true ;
dpn[j].ch_prep_timeout = 10 ;
j++;
}
/* set the timeout values */
prop->clk_stop_timeout = 64 ;
/* BIOS may set wake_capable. Make sure it is 0 as wake events are disabled. */
prop->wake_capable = 0 ;
return 0 ;
}
static int rt1320_pde_transition_delay(struct rt1320_sdw_priv *rt1320, unsigned char func,
unsigned char entity, unsigned char ps)
{
unsigned int delay = 1000 , val;
pm_runtime_mark_last_busy(&rt1320->sdw_slave->dev);
/* waiting for Actual PDE becomes to PS0/PS3 */
while (delay) {
regmap_read(rt1320->regmap,
SDW_SDCA_CTL(func, entity, RT1320_SDCA_CTL_ACTUAL_POWER_STATE, 0 ), &val);
if (val == ps)
break ;
usleep_range(1000 , 1500 );
delay--;
}
if (!delay) {
dev_warn(&rt1320->sdw_slave->dev, "%s PDE to %s is NOT ready" , __func__, ps?"PS3" :"PS0" );
return -ETIMEDOUT;
}
return 0 ;
}
/*
* The 'patch code' is written to the patch code area.
* The patch code area is used for SDCA register expansion flexibility.
*/
static void rt1320_load_mcu_patch(struct rt1320_sdw_priv *rt1320)
{
struct sdw_slave *slave = rt1320->sdw_slave;
const struct firmware *patch;
const char *filename;
unsigned int addr, val;
const unsigned char *ptr;
int ret, i;
if (rt1320->version_id <= RT1320_VB)
filename = RT1320_VAB_MCU_PATCH;
else
filename = RT1320_VC_MCU_PATCH;
/* load the patch code here */
ret = request_firmware(&patch, filename, &slave->dev);
if (ret) {
dev_err(&slave->dev, "%s: Failed to load %s firmware" , __func__, filename);
regmap_write(rt1320->regmap, 0 xc598, 0 x00);
regmap_write(rt1320->regmap, 0 x10007000, 0 x67);
regmap_write(rt1320->regmap, 0 x10007001, 0 x80);
regmap_write(rt1320->regmap, 0 x10007002, 0 x00);
regmap_write(rt1320->regmap, 0 x10007003, 0 x00);
} else {
ptr = (const unsigned char *)patch->data;
if ((patch->size % 8 ) == 0 ) {
for (i = 0 ; i < patch->size; i += 8 ) {
addr = (ptr[i] & 0 xff) | (ptr[i + 1 ] & 0 xff) << 8 |
(ptr[i + 2 ] & 0 xff) << 16 | (ptr[i + 3 ] & 0 xff) << 24 ;
val = (ptr[i + 4 ] & 0 xff) | (ptr[i + 5 ] & 0 xff) << 8 |
(ptr[i + 6 ] & 0 xff) << 16 | (ptr[i + 7 ] & 0 xff) << 24 ;
if (addr > 0 x10007fff || addr < 0 x10007000) {
dev_err(&slave->dev, "%s: the address 0x%x is wrong" , __func__, addr);
goto _exit_;
}
if (val > 0 xff) {
dev_err(&slave->dev, "%s: the value 0x%x is wrong" , __func__, val);
goto _exit_;
}
regmap_write(rt1320->regmap, addr, val);
}
}
_exit_:
release_firmware(patch);
}
}
static void rt1320_vab_preset(struct rt1320_sdw_priv *rt1320)
{
unsigned int i, reg, val, delay;
for (i = 0 ; i < ARRAY_SIZE(rt1320_blind_write); i++) {
reg = rt1320_blind_write[i].reg;
val = rt1320_blind_write[i].def;
delay = rt1320_blind_write[i].delay_us;
if (reg == 0 x3fc2bfc7)
rt1320_load_mcu_patch(rt1320);
regmap_write(rt1320->regmap, reg, val);
if (delay)
usleep_range(delay, delay + 1000 );
}
}
static void rt1320_vc_preset(struct rt1320_sdw_priv *rt1320)
{
struct sdw_slave *slave = rt1320->sdw_slave;
unsigned int i, reg, val, delay, retry, tmp;
for (i = 0 ; i < ARRAY_SIZE(rt1320_vc_blind_write); i++) {
reg = rt1320_vc_blind_write[i].reg;
val = rt1320_vc_blind_write[i].def;
delay = rt1320_vc_blind_write[i].delay_us;
if (reg == 0 x3fc2bf83)
rt1320_load_mcu_patch(rt1320);
if ((reg == SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE23, RT1320_SDCA_CTL_REQ_POWER_STATE, 0 )) &&
(val == 0 x00)) {
retry = 200 ;
while (retry) {
regmap_read(rt1320->regmap, RT1320_KR0_INT_READY, &tmp);
dev_dbg(&slave->dev, "%s, RT1320_KR0_INT_READY=0x%x\n" , __func__, tmp);
if (tmp == 0 x1f)
break ;
usleep_range(1000 , 1500 );
retry--;
}
if (!retry)
dev_warn(&slave->dev, "%s MCU is NOT ready!" , __func__);
}
regmap_write(rt1320->regmap, reg, val);
if (delay)
usleep_range(delay, delay + 1000 );
if (reg == SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE23, RT1320_SDCA_CTL_REQ_POWER_STATE, 0 ))
rt1320_pde_transition_delay(rt1320, FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE23, val);
}
}
static int rt1320_io_init(struct device *dev, struct sdw_slave *slave)
{
struct rt1320_sdw_priv *rt1320 = dev_get_drvdata(dev);
unsigned int amp_func_status, val, tmp;
if (rt1320->hw_init)
return 0 ;
regcache_cache_only(rt1320->regmap, false );
regcache_cache_only(rt1320->mbq_regmap, false );
if (rt1320->first_hw_init) {
regcache_cache_bypass(rt1320->regmap, true );
regcache_cache_bypass(rt1320->mbq_regmap, true );
} else {
/*
* PM runtime status is marked as 'active' only when a Slave reports as Attached
*/
/* update count of parent 'active' children */
pm_runtime_set_active(&slave->dev);
}
pm_runtime_get_noresume(&slave->dev);
if (rt1320->version_id < 0 ) {
regmap_read(rt1320->regmap, RT1320_DEV_VERSION_ID_1, &val);
rt1320->version_id = val;
}
regmap_read(rt1320->regmap,
SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT0, RT1320_SDCA_CTL_FUNC_STATUS, 0 ), &_func_status);
dev_dbg(dev, "%s amp func_status=0x%x\n" , __func__, amp_func_status);
/* initialization write */
if ((amp_func_status & FUNCTION_NEEDS_INITIALIZATION)) {
if (rt1320->version_id < RT1320_VC)
rt1320_vab_preset(rt1320);
else
rt1320_vc_preset(rt1320);
regmap_write(rt1320->regmap,
SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT0, RT1320_SDCA_CTL_FUNC_STATUS, 0 ),
FUNCTION_NEEDS_INITIALIZATION);
}
if (!rt1320->first_hw_init && rt1320->version_id == RT1320_VA) {
regmap_write(rt1320->regmap, SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE23,
RT1320_SDCA_CTL_REQ_POWER_STATE, 0 ), 0 );
regmap_read(rt1320->regmap, RT1320_HIFI_VER_0, &val);
regmap_read(rt1320->regmap, RT1320_HIFI_VER_1, &tmp);
val = (tmp << 8 ) | val;
regmap_read(rt1320->regmap, RT1320_HIFI_VER_2, &tmp);
val = (tmp << 16 ) | val;
regmap_read(rt1320->regmap, RT1320_HIFI_VER_3, &tmp);
val = (tmp << 24 ) | val;
dev_dbg(dev, "%s ROM version=0x%x\n" , __func__, val);
/*
* We call the version b which has the new DSP ROM code against version a.
* Therefore, we read the DSP address to check the ID.
*/
if (val == RT1320_VER_B_ID)
rt1320->version_id = RT1320_VB;
regmap_write(rt1320->regmap, SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE23,
RT1320_SDCA_CTL_REQ_POWER_STATE, 0 ), 3 );
}
dev_dbg(dev, "%s version_id=%d\n" , __func__, rt1320->version_id);
if (rt1320->first_hw_init) {
regcache_cache_bypass(rt1320->regmap, false );
regcache_cache_bypass(rt1320->mbq_regmap, false );
regcache_mark_dirty(rt1320->regmap);
regcache_mark_dirty(rt1320->mbq_regmap);
}
/* Mark Slave initialization complete */
rt1320->first_hw_init = true ;
rt1320->hw_init = true ;
pm_runtime_put_autosuspend(&slave->dev);
dev_dbg(&slave->dev, "%s hw_init complete\n" , __func__);
return 0 ;
}
static int rt1320_update_status(struct sdw_slave *slave,
enum sdw_slave_status status)
{
struct rt1320_sdw_priv *rt1320 = dev_get_drvdata(&slave->dev);
if (status == SDW_SLAVE_UNATTACHED)
rt1320->hw_init = false ;
/*
* Perform initialization only if slave status is present and
* hw_init flag is false
*/
if (rt1320->hw_init || status != SDW_SLAVE_ATTACHED)
return 0 ;
/* perform I/O transfers required for Slave initialization */
return rt1320_io_init(&slave->dev, slave);
}
static int rt1320_pde11_event(struct snd_soc_dapm_widget *w,
struct snd_kcontrol *kcontrol, int event)
{
struct snd_soc_component *component =
snd_soc_dapm_to_component(w->dapm);
struct rt1320_sdw_priv *rt1320 = snd_soc_component_get_drvdata(component);
unsigned char ps0 = 0 x0, ps3 = 0 x3;
switch (event) {
case SND_SOC_DAPM_POST_PMU:
regmap_write(rt1320->regmap,
SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_PDE11,
RT1320_SDCA_CTL_REQ_POWER_STATE, 0 ), ps0);
rt1320_pde_transition_delay(rt1320, FUNC_NUM_MIC, RT1320_SDCA_ENT_PDE11, ps0);
break ;
case SND_SOC_DAPM_PRE_PMD:
regmap_write(rt1320->regmap,
SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_PDE11,
RT1320_SDCA_CTL_REQ_POWER_STATE, 0 ), ps3);
rt1320_pde_transition_delay(rt1320, FUNC_NUM_MIC, RT1320_SDCA_ENT_PDE11, ps3);
break ;
default :
break ;
}
return 0 ;
}
static int rt1320_pde23_event(struct snd_soc_dapm_widget *w,
struct snd_kcontrol *kcontrol, int event)
{
struct snd_soc_component *component =
snd_soc_dapm_to_component(w->dapm);
struct rt1320_sdw_priv *rt1320 = snd_soc_component_get_drvdata(component);
unsigned char ps0 = 0 x0, ps3 = 0 x3;
switch (event) {
case SND_SOC_DAPM_POST_PMU:
regmap_write(rt1320->regmap,
SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE23,
RT1320_SDCA_CTL_REQ_POWER_STATE, 0 ), ps0);
rt1320_pde_transition_delay(rt1320, FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE23, ps0);
break ;
case SND_SOC_DAPM_PRE_PMD:
regmap_write(rt1320->regmap,
SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE23,
RT1320_SDCA_CTL_REQ_POWER_STATE, 0 ), ps3);
rt1320_pde_transition_delay(rt1320, FUNC_NUM_AMP, RT1320_SDCA_ENT_PDE23, ps3);
break ;
default :
break ;
}
return 0 ;
}
static int rt1320_set_gain_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
struct soc_mixer_control *mc =
(struct soc_mixer_control *)kcontrol->private_value;
struct rt1320_sdw_priv *rt1320 = snd_soc_component_get_drvdata(component);
unsigned int gain_l_val, gain_r_val;
unsigned int lvalue, rvalue;
const unsigned int interval_offset = 0 xc0;
unsigned int changed = 0 , reg_base;
struct rt_sdca_dmic_kctrl_priv *p;
unsigned int regvalue[4 ], gain_val[4 ], i;
int err;
if (strstr(ucontrol->id.name, "FU Capture Volume" ))
goto _dmic_vol_;
regmap_read(rt1320->mbq_regmap, mc->reg, &lvalue);
regmap_read(rt1320->mbq_regmap, mc->rreg, &rvalue);
/* L Channel */
gain_l_val = ucontrol->value.integer.value[0 ];
if (gain_l_val > mc->max)
gain_l_val = mc->max;
gain_l_val = 0 - ((mc->max - gain_l_val) * interval_offset);
gain_l_val &= 0 xffff;
/* R Channel */
gain_r_val = ucontrol->value.integer.value[1 ];
if (gain_r_val > mc->max)
gain_r_val = mc->max;
gain_r_val = 0 - ((mc->max - gain_r_val) * interval_offset);
gain_r_val &= 0 xffff;
if (lvalue == gain_l_val && rvalue == gain_r_val)
return 0 ;
/* Lch*/
regmap_write(rt1320->mbq_regmap, mc->reg, gain_l_val);
/* Rch */
regmap_write(rt1320->mbq_regmap, mc->rreg, gain_r_val);
goto _done_;
_dmic_vol_:
p = (struct rt_sdca_dmic_kctrl_priv *)kcontrol->private_value;
/* check all channels */
for (i = 0 ; i < p->count; i++) {
if (i < 2 ) {
reg_base = SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU113, RT1320_SDCA_CTL_FU_VOLUME, CH_01);
regmap_read(rt1320->mbq_regmap, reg_base + i, ®value[i]);
} else {
reg_base = SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU14, RT1320_SDCA_CTL_FU_VOLUME, CH_01);
regmap_read(rt1320->mbq_regmap, reg_base + i - 2 , ®value[i]);
}
gain_val[i] = ucontrol->value.integer.value[i];
if (gain_val[i] > p->max)
gain_val[i] = p->max;
gain_val[i] = 0 x1e00 - ((p->max - gain_val[i]) * interval_offset);
gain_val[i] &= 0 xffff;
if (regvalue[i] != gain_val[i])
changed = 1 ;
}
if (!changed)
return 0 ;
for (i = 0 ; i < p->count; i++) {
if (i < 2 ) {
reg_base = SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU113, RT1320_SDCA_CTL_FU_VOLUME, CH_01);
err = regmap_write(rt1320->mbq_regmap, reg_base + i, gain_val[i]);
} else {
reg_base = SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU14, RT1320_SDCA_CTL_FU_VOLUME, CH_01);
err = regmap_write(rt1320->mbq_regmap, reg_base + i - 2 , gain_val[i]);
}
if (err < 0 )
dev_err(&rt1320->sdw_slave->dev, "0x%08x can't be set\n" , reg_base + i);
}
_done_:
return 1 ;
}
static int rt1320_set_gain_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
struct rt1320_sdw_priv *rt1320 = snd_soc_component_get_drvdata(component);
struct soc_mixer_control *mc =
(struct soc_mixer_control *)kcontrol->private_value;
unsigned int read_l, read_r, ctl_l = 0 , ctl_r = 0 ;
const unsigned int interval_offset = 0 xc0;
unsigned int reg_base, regvalue, ctl, i;
struct rt_sdca_dmic_kctrl_priv *p;
if (strstr(ucontrol->id.name, "FU Capture Volume" ))
goto _dmic_vol_;
regmap_read(rt1320->mbq_regmap, mc->reg, &read_l);
regmap_read(rt1320->mbq_regmap, mc->rreg, &read_r);
ctl_l = mc->max - (((0 - read_l) & 0 xffff) / interval_offset);
if (read_l != read_r)
ctl_r = mc->max - (((0 - read_r) & 0 xffff) / interval_offset);
else
ctl_r = ctl_l;
ucontrol->value.integer.value[0 ] = ctl_l;
ucontrol->value.integer.value[1 ] = ctl_r;
goto _done_;
_dmic_vol_:
p = (struct rt_sdca_dmic_kctrl_priv *)kcontrol->private_value;
/* check all channels */
for (i = 0 ; i < p->count; i++) {
if (i < 2 ) {
reg_base = SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU113, RT1320_SDCA_CTL_FU_VOLUME, CH_01);
regmap_read(rt1320->mbq_regmap, reg_base + i, ®value);
} else {
reg_base = SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU14, RT1320_SDCA_CTL_FU_VOLUME, CH_01);
regmap_read(rt1320->mbq_regmap, reg_base + i - 2 , ®value);
}
ctl = p->max - (((0 x1e00 - regvalue) & 0 xffff) / interval_offset);
ucontrol->value.integer.value[i] = ctl;
}
_done_:
return 0 ;
}
static int rt1320_set_fu_capture_ctl(struct rt1320_sdw_priv *rt1320)
{
int err, i;
unsigned int ch_mute;
for (i = 0 ; i < ARRAY_SIZE(rt1320->fu_mixer_mute); i++) {
ch_mute = (rt1320->fu_dapm_mute || rt1320->fu_mixer_mute[i]) ? 0 x01 : 0 x00;
if (i < 2 )
err = regmap_write(rt1320->regmap,
SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU113,
RT1320_SDCA_CTL_FU_MUTE, CH_01) + i, ch_mute);
else
err = regmap_write(rt1320->regmap,
SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU14,
RT1320_SDCA_CTL_FU_MUTE, CH_01) + i - 2 , ch_mute);
if (err < 0 )
return err;
}
return 0 ;
}
static int rt1320_dmic_fu_capture_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
struct rt1320_sdw_priv *rt1320 = snd_soc_component_get_drvdata(component);
struct rt_sdca_dmic_kctrl_priv *p =
(struct rt_sdca_dmic_kctrl_priv *)kcontrol->private_value;
unsigned int i;
for (i = 0 ; i < p->count; i++)
ucontrol->value.integer.value[i] = !rt1320->fu_mixer_mute[i];
return 0 ;
}
static int rt1320_dmic_fu_capture_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
struct rt1320_sdw_priv *rt1320 = snd_soc_component_get_drvdata(component);
struct rt_sdca_dmic_kctrl_priv *p =
(struct rt_sdca_dmic_kctrl_priv *)kcontrol->private_value;
int err, changed = 0 , i;
for (i = 0 ; i < p->count; i++) {
if (rt1320->fu_mixer_mute[i] != !ucontrol->value.integer.value[i])
changed = 1 ;
rt1320->fu_mixer_mute[i] = !ucontrol->value.integer.value[i];
}
err = rt1320_set_fu_capture_ctl(rt1320);
if (err < 0 )
return err;
return changed;
}
static int rt1320_dmic_fu_info(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_info *uinfo)
{
struct rt_sdca_dmic_kctrl_priv *p =
(struct rt_sdca_dmic_kctrl_priv *)kcontrol->private_value;
if (p->max == 1 )
uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
else
uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
uinfo->count = p->count;
uinfo->value.integer.min = 0 ;
uinfo->value.integer.max = p->max;
return 0 ;
}
static int rt1320_dmic_fu_event(struct snd_soc_dapm_widget *w,
struct snd_kcontrol *kcontrol, int event)
{
struct snd_soc_component *component =
snd_soc_dapm_to_component(w->dapm);
struct rt1320_sdw_priv *rt1320 = snd_soc_component_get_drvdata(component);
switch (event) {
case SND_SOC_DAPM_POST_PMU:
rt1320->fu_dapm_mute = false ;
rt1320_set_fu_capture_ctl(rt1320);
break ;
case SND_SOC_DAPM_PRE_PMD:
rt1320->fu_dapm_mute = true ;
rt1320_set_fu_capture_ctl(rt1320);
break ;
}
return 0 ;
}
static const char * const rt1320_rx_data_ch_select[] = {
"L,R" ,
"R,L" ,
"L,L" ,
"R,R" ,
"L,L+R" ,
"R,L+R" ,
"L+R,L" ,
"L+R,R" ,
"L+R,L+R" ,
};
static SOC_ENUM_SINGLE_DECL(rt1320_rx_data_ch_enum,
SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_PPU21, RT1320_SDCA_CTL_POSTURE_NUMBER, 0 ), 0 ,
rt1320_rx_data_ch_select);
static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525 , 75 , 0 );
static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -1725 , 75 , 0 );
static const struct snd_kcontrol_new rt1320_snd_controls[] = {
SOC_DOUBLE_R_EXT_TLV("FU21 Playback Volume" ,
SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_FU21, RT1320_SDCA_CTL_FU_VOLUME, CH_01),
SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_FU21, RT1320_SDCA_CTL_FU_VOLUME, CH_02),
0 , 0 x57, 0 , rt1320_set_gain_get, rt1320_set_gain_put, out_vol_tlv),
SOC_ENUM("RX Channel Select" , rt1320_rx_data_ch_enum),
RT_SDCA_FU_CTRL("FU Capture Switch" ,
SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU113, RT1320_SDCA_CTL_FU_MUTE, CH_01),
1 , 1 , 4 , rt1320_dmic_fu_info, rt1320_dmic_fu_capture_get, rt1320_dmic_fu_capture_put),
RT_SDCA_EXT_TLV("FU Capture Volume" ,
SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_FU113, RT1320_SDCA_CTL_FU_VOLUME, CH_01),
rt1320_set_gain_get, rt1320_set_gain_put, 4 , 0 x3f, in_vol_tlv, rt1320_dmic_fu_info),
};
static const struct snd_kcontrol_new rt1320_spk_l_dac =
SOC_DAPM_SINGLE_AUTODISABLE("Switch" ,
SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_FU21, RT1320_SDCA_CTL_FU_MUTE, CH_01),
0 , 1 , 1 );
static const struct snd_kcontrol_new rt1320_spk_r_dac =
SOC_DAPM_SINGLE_AUTODISABLE("Switch" ,
SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_FU21, RT1320_SDCA_CTL_FU_MUTE, CH_02),
0 , 1 , 1 );
static const struct snd_soc_dapm_widget rt1320_dapm_widgets[] = {
/* Audio Interface */
SND_SOC_DAPM_AIF_IN("DP1RX" , "DP1 Playback" , 0 , SND_SOC_NOPM, 0 , 0 ),
SND_SOC_DAPM_AIF_OUT("DP4TX" , "DP4 Capture" , 0 , SND_SOC_NOPM, 0 , 0 ),
SND_SOC_DAPM_AIF_OUT("DP8-10TX" , "DP8-10 Capture" , 0 , SND_SOC_NOPM, 0 , 0 ),
/* Digital Interface */
SND_SOC_DAPM_PGA("FU21" , SND_SOC_NOPM, 0 , 0 , NULL, 0 ),
SND_SOC_DAPM_SUPPLY("PDE 23" , SND_SOC_NOPM, 0 , 0 ,
rt1320_pde23_event,
SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
SND_SOC_DAPM_SUPPLY("PDE 11" , SND_SOC_NOPM, 0 , 0 ,
rt1320_pde11_event, SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
SND_SOC_DAPM_ADC("FU 113" , NULL, SND_SOC_NOPM, 0 , 0 ),
SND_SOC_DAPM_ADC("FU 14" , NULL, SND_SOC_NOPM, 0 , 0 ),
SND_SOC_DAPM_PGA_E("FU" , SND_SOC_NOPM, 0 , 0 , NULL, 0 ,
rt1320_dmic_fu_event, SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
/* Output */
SND_SOC_DAPM_SWITCH("OT23 L" , SND_SOC_NOPM, 0 , 0 , &rt1320_spk_l_dac),
SND_SOC_DAPM_SWITCH("OT23 R" , SND_SOC_NOPM, 0 , 0 , &rt1320_spk_r_dac),
SND_SOC_DAPM_OUTPUT("SPOL" ),
SND_SOC_DAPM_OUTPUT("SPOR" ),
/* Input */
SND_SOC_DAPM_PGA("AEC Data" , SND_SOC_NOPM, 0 , 0 , NULL, 0 ),
SND_SOC_DAPM_SIGGEN("AEC Gen" ),
SND_SOC_DAPM_INPUT("DMIC1" ),
SND_SOC_DAPM_INPUT("DMIC2" ),
};
static const struct snd_soc_dapm_route rt1320_dapm_routes[] = {
{ "FU21" , NULL, "DP1RX" },
{ "FU21" , NULL, "PDE 23" },
{ "OT23 L" , "Switch" , "FU21" },
{ "OT23 R" , "Switch" , "FU21" },
{ "SPOL" , NULL, "OT23 L" },
{ "SPOR" , NULL, "OT23 R" },
{ "AEC Data" , NULL, "AEC Gen" },
{ "DP4TX" , NULL, "AEC Data" },
{"DP8-10TX" , NULL, "FU" },
{"FU" , NULL, "PDE 11" },
{"FU" , NULL, "FU 113" },
{"FU" , NULL, "FU 14" },
{"FU 113" , NULL, "DMIC1" },
{"FU 14" , NULL, "DMIC2" },
};
static int rt1320_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
int direction)
{
snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
return 0 ;
}
static void rt1320_sdw_shutdown(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
snd_soc_dai_set_dma_data(dai, substream, NULL);
}
static int rt1320_sdw_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
{
struct snd_soc_component *component = dai->component;
struct rt1320_sdw_priv *rt1320 =
snd_soc_component_get_drvdata(component);
struct sdw_stream_config stream_config;
struct sdw_port_config port_config;
struct sdw_port_config dmic_port_config[2 ];
struct sdw_stream_runtime *sdw_stream;
int retval;
unsigned int sampling_rate;
dev_dbg(dai->dev, "%s %s" , __func__, dai->name);
sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
if (!sdw_stream)
return -EINVAL;
if (!rt1320->sdw_slave)
return -EINVAL;
/* SoundWire specific configuration */
snd_sdw_params_to_config(substream, params, &stream_config, &port_config);
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
if (dai->id == RT1320_AIF1)
port_config.num = 1 ;
else
return -EINVAL;
} else {
if (dai->id == RT1320_AIF1)
port_config.num = 4 ;
else if (dai->id == RT1320_AIF2) {
dmic_port_config[0 ].ch_mask = BIT(0 ) | BIT(1 );
dmic_port_config[0 ].num = 8 ;
dmic_port_config[1 ].ch_mask = BIT(0 ) | BIT(1 );
dmic_port_config[1 ].num = 10 ;
} else
return -EINVAL;
}
if (dai->id == RT1320_AIF1)
retval = sdw_stream_add_slave(rt1320->sdw_slave, &stream_config,
&port_config, 1 , sdw_stream);
else if (dai->id == RT1320_AIF2)
retval = sdw_stream_add_slave(rt1320->sdw_slave, &stream_config,
dmic_port_config, 2 , sdw_stream);
else
return -EINVAL;
if (retval) {
dev_err(dai->dev, "%s: Unable to configure port\n" , __func__);
return retval;
}
/* sampling rate configuration */
switch (params_rate(params)) {
case 16000 :
sampling_rate = RT1320_SDCA_RATE_16000HZ;
break ;
case 32000 :
sampling_rate = RT1320_SDCA_RATE_32000HZ;
break ;
case 44100 :
sampling_rate = RT1320_SDCA_RATE_44100HZ;
break ;
case 48000 :
sampling_rate = RT1320_SDCA_RATE_48000HZ;
break ;
case 96000 :
sampling_rate = RT1320_SDCA_RATE_96000HZ;
break ;
case 192000 :
sampling_rate = RT1320_SDCA_RATE_192000HZ;
break ;
default :
dev_err(component->dev, "%s: Rate %d is not supported\n" ,
__func__, params_rate(params));
return -EINVAL;
}
/* set sampling frequency */
if (dai->id == RT1320_AIF1)
regmap_write(rt1320->regmap,
SDW_SDCA_CTL(FUNC_NUM_AMP, RT1320_SDCA_ENT_CS21, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX, 0 ),
sampling_rate);
else {
regmap_write(rt1320->regmap,
SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_CS113, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX, 0 ),
sampling_rate);
regmap_write(rt1320->regmap,
SDW_SDCA_CTL(FUNC_NUM_MIC, RT1320_SDCA_ENT_CS14, RT1320_SDCA_CTL_SAMPLE_FREQ_INDEX, 0 ),
sampling_rate);
}
return 0 ;
}
static int rt1320_sdw_pcm_hw_free(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
struct snd_soc_component *component = dai->component;
struct rt1320_sdw_priv *rt1320 =
snd_soc_component_get_drvdata(component);
struct sdw_stream_runtime *sdw_stream =
snd_soc_dai_get_dma_data(dai, substream);
if (!rt1320->sdw_slave)
return -EINVAL;
sdw_stream_remove_slave(rt1320->sdw_slave, sdw_stream);
return 0 ;
}
/*
* slave_ops: callbacks for get_clock_stop_mode, clock_stop and
* port_prep are not defined for now
*/
static const struct sdw_slave_ops rt1320_slave_ops = {
.read_prop = rt1320_read_prop,
.update_status = rt1320_update_status,
};
static int rt1320_sdw_component_probe(struct snd_soc_component *component)
{
int ret;
struct rt1320_sdw_priv *rt1320 = snd_soc_component_get_drvdata(component);
rt1320->component = component;
if (!rt1320->first_hw_init)
return 0 ;
ret = pm_runtime_resume(component->dev);
dev_dbg(&rt1320->sdw_slave->dev, "%s pm_runtime_resume, ret=%d" , __func__, ret);
if (ret < 0 && ret != -EACCES)
return ret;
return 0 ;
}
static const struct snd_soc_component_driver soc_component_sdw_rt1320 = {
.probe = rt1320_sdw_component_probe,
.controls = rt1320_snd_controls,
.num_controls = ARRAY_SIZE(rt1320_snd_controls),
.dapm_widgets = rt1320_dapm_widgets,
.num_dapm_widgets = ARRAY_SIZE(rt1320_dapm_widgets),
.dapm_routes = rt1320_dapm_routes,
.num_dapm_routes = ARRAY_SIZE(rt1320_dapm_routes),
.endianness = 1 ,
};
static const struct snd_soc_dai_ops rt1320_aif_dai_ops = {
.hw_params = rt1320_sdw_hw_params,
.hw_free = rt1320_sdw_pcm_hw_free,
.set_stream = rt1320_set_sdw_stream,
.shutdown = rt1320_sdw_shutdown,
};
#define RT1320_STEREO_RATES (SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000)
#define RT1320_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | \
SNDRV_PCM_FMTBIT_S32_LE)
static struct snd_soc_dai_driver rt1320_sdw_dai[] = {
{
.name = "rt1320-aif1" ,
.id = RT1320_AIF1,
.playback = {
.stream_name = "DP1 Playback" ,
.channels_min = 1 ,
.channels_max = 2 ,
.rates = RT1320_STEREO_RATES,
.formats = RT1320_FORMATS,
},
.capture = {
.stream_name = "DP4 Capture" ,
.channels_min = 1 ,
.channels_max = 2 ,
.rates = RT1320_STEREO_RATES,
.formats = RT1320_FORMATS,
},
.ops = &rt1320_aif_dai_ops,
},
/* DMIC: DP8 2ch + DP10 2ch */
{
.name = "rt1320-aif2" ,
.id = RT1320_AIF2,
.capture = {
.stream_name = "DP8-10 Capture" ,
.channels_min = 1 ,
.channels_max = 4 ,
.rates = RT1320_STEREO_RATES,
.formats = RT1320_FORMATS,
},
.ops = &rt1320_aif_dai_ops,
},
};
static int rt1320_sdw_init(struct device *dev, struct regmap *regmap,
struct regmap *mbq_regmap, struct sdw_slave *slave)
{
struct rt1320_sdw_priv *rt1320;
int ret;
rt1320 = devm_kzalloc(dev, sizeof (*rt1320), GFP_KERNEL);
if (!rt1320)
return -ENOMEM;
dev_set_drvdata(dev, rt1320);
rt1320->sdw_slave = slave;
rt1320->mbq_regmap = mbq_regmap;
rt1320->regmap = regmap;
regcache_cache_only(rt1320->regmap, true );
regcache_cache_only(rt1320->mbq_regmap, true );
/*
* Mark hw_init to false
* HW init will be performed when device reports present
*/
rt1320->hw_init = false ;
rt1320->first_hw_init = false ;
rt1320->version_id = -1 ;
rt1320->fu_dapm_mute = true ;
rt1320->fu_mixer_mute[0 ] = rt1320->fu_mixer_mute[1 ] =
rt1320->fu_mixer_mute[2 ] = rt1320->fu_mixer_mute[3 ] = true ;
ret = devm_snd_soc_register_component(dev,
&soc_component_sdw_rt1320,
rt1320_sdw_dai,
ARRAY_SIZE(rt1320_sdw_dai));
if (ret < 0 )
return ret;
/* set autosuspend parameters */
pm_runtime_set_autosuspend_delay(dev, 3000 );
pm_runtime_use_autosuspend(dev);
/* make sure the device does not suspend immediately */
pm_runtime_mark_last_busy(dev);
pm_runtime_enable(dev);
/* important note: the device is NOT tagged as 'active' and will remain
* 'suspended' until the hardware is enumerated/initialized. This is required
* to make sure the ASoC framework use of pm_runtime_get_sync() does not silently
* fail with -EACCESS because of race conditions between card creation and enumeration
*/
dev_dbg(dev, "%s\n" , __func__);
return ret;
}
static int rt1320_sdw_probe(struct sdw_slave *slave,
const struct sdw_device_id *id)
{
struct regmap *regmap, *mbq_regmap;
/* Regmap Initialization */
mbq_regmap = devm_regmap_init_sdw_mbq(slave, &rt1320_mbq_regmap);
if (IS_ERR(mbq_regmap))
return PTR_ERR(mbq_regmap);
regmap = devm_regmap_init_sdw(slave, &rt1320_sdw_regmap);
if (IS_ERR(regmap))
return PTR_ERR(regmap);
return rt1320_sdw_init(&slave->dev, regmap, mbq_regmap, slave);
}
static int rt1320_sdw_remove(struct sdw_slave *slave)
{
pm_runtime_disable(&slave->dev);
return 0 ;
}
/*
* Version A/B will use the class id 0
* The newer version than A/B will use the class id 1, so add it in advance
*/
static const struct sdw_device_id rt1320_id[] = {
SDW_SLAVE_ENTRY_EXT(0 x025d, 0 x1320, 0 x3, 0 x0, 0 ),
SDW_SLAVE_ENTRY_EXT(0 x025d, 0 x1320, 0 x3, 0 x1, 0 ),
{},
};
MODULE_DEVICE_TABLE(sdw, rt1320_id);
static int rt1320_dev_suspend(struct device *dev)
{
struct rt1320_sdw_priv *rt1320 = dev_get_drvdata(dev);
if (!rt1320->hw_init)
return 0 ;
regcache_cache_only(rt1320->regmap, true );
regcache_cache_only(rt1320->mbq_regmap, true );
return 0 ;
}
#define RT1320_PROBE_TIMEOUT 5000
static int rt1320_dev_resume(struct device *dev)
{
struct sdw_slave *slave = dev_to_sdw_dev(dev);
struct rt1320_sdw_priv *rt1320 = dev_get_drvdata(dev);
unsigned long time;
if (!rt1320->first_hw_init)
return 0 ;
if (!slave->unattach_request)
goto regmap_sync;
time = wait_for_completion_timeout(&slave->initialization_complete,
msecs_to_jiffies(RT1320_PROBE_TIMEOUT));
if (!time) {
dev_err(&slave->dev, "%s: Initialization not complete, timed out\n" , __func__);
return -ETIMEDOUT;
}
regmap_sync:
slave->unattach_request = 0 ;
regcache_cache_only(rt1320->regmap, false );
regcache_sync(rt1320->regmap);
regcache_cache_only(rt1320->mbq_regmap, false );
regcache_sync(rt1320->mbq_regmap);
return 0 ;
}
static const struct dev_pm_ops rt1320_pm = {
SYSTEM_SLEEP_PM_OPS(rt1320_dev_suspend, rt1320_dev_resume)
RUNTIME_PM_OPS(rt1320_dev_suspend, rt1320_dev_resume, NULL)
};
static struct sdw_driver rt1320_sdw_driver = {
.driver = {
.name = "rt1320-sdca" ,
.pm = pm_ptr(&rt1320_pm),
},
.probe = rt1320_sdw_probe,
.remove = rt1320_sdw_remove,
.ops = &rt1320_slave_ops,
.id_table = rt1320_id,
};
module_sdw_driver(rt1320_sdw_driver);
MODULE_DESCRIPTION("ASoC RT1320 driver SDCA SDW" );
MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>" );
MODULE_LICENSE("GPL" );
Messung V0.5 in Prozent C=90 H=95 G=92
¤ Dauer der Verarbeitung: 0.20 Sekunden
(vorverarbeitet am 2026-06-05)
¤
*© Formatika GbR, Deutschland