// SPDX-License-Identifier: GPL-2.0-only
//
// rt1316-sdw.c -- rt1316 SDCA ALSA SoC amplifier audio driver
//
// Copyright(c) 2021 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 <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/sdw.h>
#include <sound/soc-dapm.h>
#include <sound/initval.h>
#include "rt1316-sdw.h"
static const struct reg_default rt1316_reg_defaults[] = {
{ 0 x3004, 0 x00 },
{ 0 x3005, 0 x00 },
{ 0 x3206, 0 x00 },
{ 0 xc001, 0 x00 },
{ 0 xc002, 0 x00 },
{ 0 xc003, 0 x00 },
{ 0 xc004, 0 x00 },
{ 0 xc005, 0 x00 },
{ 0 xc006, 0 x00 },
{ 0 xc007, 0 x00 },
{ 0 xc008, 0 x00 },
{ 0 xc009, 0 x00 },
{ 0 xc00a, 0 x00 },
{ 0 xc00b, 0 x00 },
{ 0 xc00c, 0 x00 },
{ 0 xc00d, 0 x00 },
{ 0 xc00e, 0 x00 },
{ 0 xc00f, 0 x00 },
{ 0 xc010, 0 xa5 },
{ 0 xc011, 0 x00 },
{ 0 xc012, 0 xff },
{ 0 xc013, 0 xff },
{ 0 xc014, 0 x40 },
{ 0 xc015, 0 x00 },
{ 0 xc016, 0 x00 },
{ 0 xc017, 0 x00 },
{ 0 xc605, 0 x30 },
{ 0 xc700, 0 x0a },
{ 0 xc701, 0 xaa },
{ 0 xc702, 0 x1a },
{ 0 xc703, 0 x0a },
{ 0 xc710, 0 x80 },
{ 0 xc711, 0 x00 },
{ 0 xc712, 0 x3e },
{ 0 xc713, 0 x80 },
{ 0 xc714, 0 x80 },
{ 0 xc715, 0 x06 },
{ 0 xd101, 0 x00 },
{ 0 xd102, 0 x30 },
{ 0 xd103, 0 x00 },
{ SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_UDMPU21, RT1316_SDCA_CTL_UDMPU_CLUSTER, 0 ), 0 x00 },
{ SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_FU21, RT1316_SDCA_CTL_FU_MUTE, CH_L), 0 x01 },
{ SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_FU21, RT1316_SDCA_CTL_FU_MUTE, CH_R), 0 x01 },
{ SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_XU24, RT1316_SDCA_CTL_BYPASS, 0 ), 0 x01 },
{ SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_PDE23, RT1316_SDCA_CTL_REQ_POWER_STATE, 0 ), 0 x03 },
{ SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_PDE22, RT1316_SDCA_CTL_REQ_POWER_STATE, 0 ), 0 x03 },
{ SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_PDE24, RT1316_SDCA_CTL_REQ_POWER_STATE, 0 ), 0 x03 },
};
static const struct reg_sequence rt1316_blind_write[] = {
{ 0 xc710, 0 x17 },
{ 0 xc711, 0 x80 },
{ 0 xc712, 0 x26 },
{ 0 xc713, 0 x06 },
{ 0 xc714, 0 x80 },
{ 0 xc715, 0 x06 },
{ 0 xc702, 0 x0a },
{ 0 xc703, 0 x0a },
{ 0 xc001, 0 x45 },
{ 0 xc003, 0 x00 },
{ 0 xc004, 0 x11 },
{ 0 xc005, 0 x00 },
{ 0 xc006, 0 x00 },
{ 0 xc106, 0 x00 },
{ 0 xc007, 0 x11 },
{ 0 xc008, 0 x11 },
{ 0 xc009, 0 x00 },
{ 0 x2f0a, 0 x00 },
{ 0 xd101, 0 xf0 },
{ 0 xd103, 0 x9b },
{ 0 x2f36, 0 x8e },
{ 0 x3206, 0 x80 },
{ 0 x3211, 0 x0b },
{ 0 x3216, 0 x06 },
{ 0 xc614, 0 x20 },
{ 0 xc615, 0 x0a },
{ 0 xc616, 0 x02 },
{ 0 xc617, 0 x00 },
{ 0 xc60b, 0 x10 },
{ 0 xc60e, 0 x05 },
{ 0 xc102, 0 x00 },
{ 0 xc090, 0 xb0 },
{ 0 xc00f, 0 x01 },
{ 0 xc09c, 0 x7b },
{ 0 xc602, 0 x07 },
{ 0 xc603, 0 x07 },
{ 0 xc0a3, 0 x71 },
{ 0 xc00b, 0 x30 },
{ 0 xc093, 0 x80 },
{ 0 xc09d, 0 x80 },
{ 0 xc0b0, 0 x77 },
{ 0 xc010, 0 xa5 },
{ 0 xc050, 0 x83 },
{ 0 x2f55, 0 x03 },
{ 0 x3217, 0 xb5 },
{ 0 x3202, 0 x02 },
{ SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_XU24, RT1316_SDCA_CTL_BYPASS, 0 ), 0 x00 },
/* for IV sense */
{ 0 x2232, 0 x80 },
{ 0 xc0b0, 0 x77 },
{ 0 xc011, 0 x00 },
{ 0 xc020, 0 x00 },
{ 0 xc023, 0 x00 },
{ 0 x3101, 0 x00 },
{ 0 x3004, 0 xa0 },
{ 0 x3005, 0 xb1 },
{ 0 xc007, 0 x11 },
{ 0 xc008, 0 x11 },
{ 0 xc009, 0 x00 },
{ 0 xc022, 0 xd6 },
{ 0 xc025, 0 xd6 },
{ 0 xd001, 0 x03 },
{ 0 xd002, 0 xbf },
{ 0 xd003, 0 x03 },
{ 0 xd004, 0 xbf },
};
static bool rt1316_readable_register(struct device *dev, unsigned int reg)
{
switch (reg) {
case 0 x2f0a:
case 0 x2f36:
case 0 x3203 ... 0 x320e:
case 0 xc000 ... 0 xc7b4:
case 0 xcf00 ... 0 xcf03:
case 0 xd101 ... 0 xd103:
case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_UDMPU21, RT1316_SDCA_CTL_UDMPU_CLUSTER, 0 ):
case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_FU21, RT1316_SDCA_CTL_FU_MUTE, CH_L):
case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_FU21, RT1316_SDCA_CTL_FU_MUTE, CH_R):
case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_PDE23, RT1316_SDCA_CTL_REQ_POWER_STATE, 0 ):
case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_PDE27, RT1316_SDCA_CTL_REQ_POWER_STATE, 0 ):
case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_PDE22, RT1316_SDCA_CTL_REQ_POWER_STATE, 0 ):
case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_PDE24, RT1316_SDCA_CTL_REQ_POWER_STATE, 0 ):
return true ;
default :
return false ;
}
}
static bool rt1316_volatile_register(struct device *dev, unsigned int reg)
{
switch (reg) {
case 0 xc000:
case 0 xc093:
case 0 xc09d:
case 0 xc0a3:
case 0 xc201:
case 0 xc427 ... 0 xc428:
case 0 xd102:
return true ;
default :
return false ;
}
}
static const struct regmap_config rt1316_sdw_regmap = {
.reg_bits = 32 ,
.val_bits = 8 ,
.readable_reg = rt1316_readable_register,
.volatile_reg = rt1316_volatile_register,
.max_register = 0 x4108ffff,
.reg_defaults = rt1316_reg_defaults,
.num_reg_defaults = ARRAY_SIZE(rt1316_reg_defaults),
.cache_type = REGCACHE_MAPLE,
.use_single_read = true ,
.use_single_write = true ,
};
static int rt1316_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;
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 ;
/* first we need to allocate memory for set bits in port lists */
prop->source_ports = 0 x04; /* BITMAP: 00000100 */
prop->sink_ports = 0 x2; /* BITMAP: 00000010 */
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 = 20 ;
dev_dbg(&slave->dev, "%s\n" , __func__);
return 0 ;
}
static void rt1316_apply_bq_params(struct rt1316_sdw_priv *rt1316)
{
unsigned int i, reg, data;
for (i = 0 ; i < rt1316->bq_params_cnt; i += 3 ) {
reg = rt1316->bq_params[i] | (rt1316->bq_params[i + 1 ] << 8 );
data = rt1316->bq_params[i + 2 ];
regmap_write(rt1316->regmap, reg, data);
}
}
static int rt1316_io_init(struct device *dev, struct sdw_slave *slave)
{
struct rt1316_sdw_priv *rt1316 = dev_get_drvdata(dev);
if (rt1316->hw_init)
return 0 ;
regcache_cache_only(rt1316->regmap, false );
if (rt1316->first_hw_init) {
regcache_cache_bypass(rt1316->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);
/* sw reset */
regmap_write(rt1316->regmap, 0 xc000, 0 x02);
/* initial settings - blind write */
regmap_multi_reg_write(rt1316->regmap, rt1316_blind_write,
ARRAY_SIZE(rt1316_blind_write));
if (rt1316->first_hw_init) {
regcache_cache_bypass(rt1316->regmap, false );
regcache_mark_dirty(rt1316->regmap);
} else
rt1316->first_hw_init = true ;
/* Mark Slave initialization complete */
rt1316->hw_init = true ;
pm_runtime_put_autosuspend(&slave->dev);
dev_dbg(&slave->dev, "%s hw_init complete\n" , __func__);
return 0 ;
}
static int rt1316_update_status(struct sdw_slave *slave,
enum sdw_slave_status status)
{
struct rt1316_sdw_priv *rt1316 = dev_get_drvdata(&slave->dev);
if (status == SDW_SLAVE_UNATTACHED)
rt1316->hw_init = false ;
/*
* Perform initialization only if slave status is present and
* hw_init flag is false
*/
if (rt1316->hw_init || status != SDW_SLAVE_ATTACHED)
return 0 ;
/* perform I/O transfers required for Slave initialization */
return rt1316_io_init(&slave->dev, slave);
}
static int rt1316_classd_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 rt1316_sdw_priv *rt1316 = snd_soc_component_get_drvdata(component);
unsigned char ps0 = 0 x0, ps3 = 0 x3;
switch (event) {
case SND_SOC_DAPM_POST_PMU:
regmap_write(rt1316->regmap,
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_PDE23,
RT1316_SDCA_CTL_REQ_POWER_STATE, 0 ),
ps0);
regmap_write(rt1316->regmap,
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_PDE27,
RT1316_SDCA_CTL_REQ_POWER_STATE, 0 ),
ps0);
regmap_write(rt1316->regmap,
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_PDE22,
RT1316_SDCA_CTL_REQ_POWER_STATE, 0 ),
ps0);
break ;
case SND_SOC_DAPM_PRE_PMD:
regmap_write(rt1316->regmap,
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_PDE23,
RT1316_SDCA_CTL_REQ_POWER_STATE, 0 ),
ps3);
regmap_write(rt1316->regmap,
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_PDE27,
RT1316_SDCA_CTL_REQ_POWER_STATE, 0 ),
ps3);
regmap_write(rt1316->regmap,
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_PDE22,
RT1316_SDCA_CTL_REQ_POWER_STATE, 0 ),
ps3);
break ;
default :
break ;
}
return 0 ;
}
static int rt1316_pde24_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 rt1316_sdw_priv *rt1316 = snd_soc_component_get_drvdata(component);
unsigned char ps0 = 0 x0, ps3 = 0 x3;
switch (event) {
case SND_SOC_DAPM_POST_PMU:
regmap_write(rt1316->regmap,
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_PDE24,
RT1316_SDCA_CTL_REQ_POWER_STATE, 0 ),
ps0);
break ;
case SND_SOC_DAPM_PRE_PMD:
regmap_write(rt1316->regmap,
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_PDE24,
RT1316_SDCA_CTL_REQ_POWER_STATE, 0 ),
ps3);
break ;
}
return 0 ;
}
static const char * const rt1316_rx_data_ch_select[] = {
"L,R" ,
"L,L" ,
"L,R" ,
"L,L+R" ,
"R,L" ,
"R,R" ,
"R,L+R" ,
"L+R,L" ,
"L+R,R" ,
"L+R,L+R" ,
};
static SOC_ENUM_SINGLE_DECL(rt1316_rx_data_ch_enum,
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_UDMPU21, RT1316_SDCA_CTL_UDMPU_CLUSTER, 0 ), 0 ,
rt1316_rx_data_ch_select);
static const char * const rt1316_dac_output_vol_select[] = {
"immediately" ,
"zero crossing" ,
"zero crossing with soft ramp" ,
};
static SOC_ENUM_SINGLE_DECL(rt1316_dac_vol_ctl_enum,
0 xc010, 6 , rt1316_dac_output_vol_select);
static const struct snd_kcontrol_new rt1316_snd_controls[] = {
/* I2S Data Channel Selection */
SOC_ENUM("RX Channel Select" , rt1316_rx_data_ch_enum),
/* XU24 Bypass Control */
SOC_SINGLE("XU24 Bypass Switch" ,
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_XU24, RT1316_SDCA_CTL_BYPASS, 0 ), 0 , 1 , 0 ),
/* Left/Right IV tag */
SOC_SINGLE("Left V Tag Select" , 0 x3004, 0 , 7 , 0 ),
SOC_SINGLE("Left I Tag Select" , 0 x3004, 4 , 7 , 0 ),
SOC_SINGLE("Right V Tag Select" , 0 x3005, 0 , 7 , 0 ),
SOC_SINGLE("Right I Tag Select" , 0 x3005, 4 , 7 , 0 ),
/* IV mixer Control */
SOC_DOUBLE("Isense Mixer Switch" , 0 xc605, 2 , 0 , 1 , 1 ),
SOC_DOUBLE("Vsense Mixer Switch" , 0 xc605, 3 , 1 , 1 , 1 ),
/* DAC Output Volume Control */
SOC_ENUM("DAC Output Vol Control" , rt1316_dac_vol_ctl_enum),
};
static const struct snd_kcontrol_new rt1316_sto_dac =
SOC_DAPM_DOUBLE_R("Switch" ,
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_FU21, RT1316_SDCA_CTL_FU_MUTE, CH_L),
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1316_SDCA_ENT_FU21, RT1316_SDCA_CTL_FU_MUTE, CH_R),
0 , 1 , 1 );
static const struct snd_soc_dapm_widget rt1316_dapm_widgets[] = {
/* Audio Interface */
SND_SOC_DAPM_AIF_IN("DP1RX" , "DP1 Playback" , 0 , SND_SOC_NOPM, 0 , 0 ),
SND_SOC_DAPM_AIF_OUT("DP2TX" , "DP2 Capture" , 0 , SND_SOC_NOPM, 0 , 0 ),
/* Digital Interface */
SND_SOC_DAPM_SWITCH("DAC" , SND_SOC_NOPM, 0 , 0 , &rt1316_sto_dac),
/* Output Lines */
SND_SOC_DAPM_PGA_E("CLASS D" , SND_SOC_NOPM, 0 , 0 , NULL, 0 ,
rt1316_classd_event,
SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
SND_SOC_DAPM_OUTPUT("SPOL" ),
SND_SOC_DAPM_OUTPUT("SPOR" ),
SND_SOC_DAPM_SUPPLY("PDE 24" , SND_SOC_NOPM, 0 , 0 ,
rt1316_pde24_event,
SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
SND_SOC_DAPM_PGA("I Sense" , SND_SOC_NOPM, 0 , 0 , NULL, 0 ),
SND_SOC_DAPM_PGA("V Sense" , SND_SOC_NOPM, 0 , 0 , NULL, 0 ),
SND_SOC_DAPM_SIGGEN("I Gen" ),
SND_SOC_DAPM_SIGGEN("V Gen" ),
};
static const struct snd_soc_dapm_route rt1316_dapm_routes[] = {
{ "DAC" , "Switch" , "DP1RX" },
{ "CLASS D" , NULL, "DAC" },
{ "SPOL" , NULL, "CLASS D" },
{ "SPOR" , NULL, "CLASS D" },
{ "I Sense" , NULL, "I Gen" },
{ "V Sense" , NULL, "V Gen" },
{ "I Sense" , NULL, "PDE 24" },
{ "V Sense" , NULL, "PDE 24" },
{ "DP2TX" , NULL, "I Sense" },
{ "DP2TX" , NULL, "V Sense" },
};
static int rt1316_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 rt1316_sdw_shutdown(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
snd_soc_dai_set_dma_data(dai, substream, NULL);
}
static int rt1316_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 rt1316_sdw_priv *rt1316 =
snd_soc_component_get_drvdata(component);
struct sdw_stream_config stream_config = {0 };
struct sdw_port_config port_config = {0 };
struct sdw_stream_runtime *sdw_stream;
int retval;
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 (!rt1316->sdw_slave)
return -EINVAL;
/* SoundWire specific configuration */
snd_sdw_params_to_config(substream, params, &stream_config, &port_config);
/* port 1 for playback */
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
port_config.num = 1 ;
else
port_config.num = 2 ;
retval = sdw_stream_add_slave(rt1316->sdw_slave, &stream_config,
&port_config, 1 , sdw_stream);
if (retval) {
dev_err(dai->dev, "%s: Unable to configure port\n" , __func__);
return retval;
}
return 0 ;
}
static int rt1316_sdw_pcm_hw_free(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
struct snd_soc_component *component = dai->component;
struct rt1316_sdw_priv *rt1316 =
snd_soc_component_get_drvdata(component);
struct sdw_stream_runtime *sdw_stream =
snd_soc_dai_get_dma_data(dai, substream);
if (!rt1316->sdw_slave)
return -EINVAL;
sdw_stream_remove_slave(rt1316->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 rt1316_slave_ops = {
.read_prop = rt1316_read_prop,
.update_status = rt1316_update_status,
};
static int rt1316_sdw_parse_dt(struct rt1316_sdw_priv *rt1316, struct device *dev)
{
int ret = 0 ;
device_property_read_u32(dev, "realtek,bq-params-cnt" , &rt1316->bq_params_cnt);
if (rt1316->bq_params_cnt) {
rt1316->bq_params = devm_kzalloc(dev, rt1316->bq_params_cnt, GFP_KERNEL);
if (!rt1316->bq_params) {
dev_err(dev, "%s: Could not allocate bq_params memory\n" , __func__);
ret = -ENOMEM;
} else {
ret = device_property_read_u8_array(dev, "realtek,bq-params" , rt1316->bq_params, rt1316->bq_params_cnt);
if (ret < 0 )
dev_err(dev, "%s: Could not read list of realtek,bq-params\n" , __func__);
}
}
dev_dbg(dev, "bq_params_cnt=%d\n" , rt1316->bq_params_cnt);
return ret;
}
static int rt1316_sdw_component_probe(struct snd_soc_component *component)
{
struct rt1316_sdw_priv *rt1316 = snd_soc_component_get_drvdata(component);
int ret;
rt1316->component = component;
rt1316_sdw_parse_dt(rt1316, &rt1316->sdw_slave->dev);
if (!rt1316->first_hw_init)
return 0 ;
ret = pm_runtime_resume(component->dev);
if (ret < 0 && ret != -EACCES)
return ret;
/* apply BQ params */
rt1316_apply_bq_params(rt1316);
return 0 ;
}
static const struct snd_soc_component_driver soc_component_sdw_rt1316 = {
.probe = rt1316_sdw_component_probe,
.controls = rt1316_snd_controls,
.num_controls = ARRAY_SIZE(rt1316_snd_controls),
.dapm_widgets = rt1316_dapm_widgets,
.num_dapm_widgets = ARRAY_SIZE(rt1316_dapm_widgets),
.dapm_routes = rt1316_dapm_routes,
.num_dapm_routes = ARRAY_SIZE(rt1316_dapm_routes),
.endianness = 1 ,
};
static const struct snd_soc_dai_ops rt1316_aif_dai_ops = {
.hw_params = rt1316_sdw_hw_params,
.hw_free = rt1316_sdw_pcm_hw_free,
.set_stream = rt1316_set_sdw_stream,
.shutdown = rt1316_sdw_shutdown,
};
#define RT1316_STEREO_RATES SNDRV_PCM_RATE_48000
#define RT1316_FORMATS (SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S16_LE | \
SNDRV_PCM_FMTBIT_S24_LE)
static struct snd_soc_dai_driver rt1316_sdw_dai[] = {
{
.name = "rt1316-aif" ,
.playback = {
.stream_name = "DP1 Playback" ,
.channels_min = 1 ,
.channels_max = 2 ,
.rates = RT1316_STEREO_RATES,
.formats = RT1316_FORMATS,
},
.capture = {
.stream_name = "DP2 Capture" ,
.channels_min = 1 ,
.channels_max = 2 ,
.rates = RT1316_STEREO_RATES,
.formats = RT1316_FORMATS,
},
.ops = &rt1316_aif_dai_ops,
},
};
static int rt1316_sdw_init(struct device *dev, struct regmap *regmap,
struct sdw_slave *slave)
{
struct rt1316_sdw_priv *rt1316;
int ret;
rt1316 = devm_kzalloc(dev, sizeof (*rt1316), GFP_KERNEL);
if (!rt1316)
return -ENOMEM;
dev_set_drvdata(dev, rt1316);
rt1316->sdw_slave = slave;
rt1316->regmap = regmap;
regcache_cache_only(rt1316->regmap, true );
/*
* Mark hw_init to false
* HW init will be performed when device reports present
*/
rt1316->hw_init = false ;
rt1316->first_hw_init = false ;
ret = devm_snd_soc_register_component(dev,
&soc_component_sdw_rt1316,
rt1316_sdw_dai,
ARRAY_SIZE(rt1316_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 0 ;
}
static int rt1316_sdw_probe(struct sdw_slave *slave,
const struct sdw_device_id *id)
{
struct regmap *regmap;
/* Regmap Initialization */
regmap = devm_regmap_init_sdw(slave, &rt1316_sdw_regmap);
if (IS_ERR(regmap))
return PTR_ERR(regmap);
return rt1316_sdw_init(&slave->dev, regmap, slave);
}
static int rt1316_sdw_remove(struct sdw_slave *slave)
{
pm_runtime_disable(&slave->dev);
return 0 ;
}
static const struct sdw_device_id rt1316_id[] = {
SDW_SLAVE_ENTRY_EXT(0 x025d, 0 x1316, 0 x3, 0 x1, 0 ),
{},
};
MODULE_DEVICE_TABLE(sdw, rt1316_id);
static int rt1316_dev_suspend(struct device *dev)
{
struct rt1316_sdw_priv *rt1316 = dev_get_drvdata(dev);
if (!rt1316->hw_init)
return 0 ;
regcache_cache_only(rt1316->regmap, true );
return 0 ;
}
#define RT1316_PROBE_TIMEOUT 5000
static int rt1316_dev_resume(struct device *dev)
{
struct sdw_slave *slave = dev_to_sdw_dev(dev);
struct rt1316_sdw_priv *rt1316 = dev_get_drvdata(dev);
unsigned long time;
if (!rt1316->first_hw_init)
return 0 ;
if (!slave->unattach_request)
goto regmap_sync;
time = wait_for_completion_timeout(&slave->initialization_complete,
msecs_to_jiffies(RT1316_PROBE_TIMEOUT));
if (!time) {
dev_err(&slave->dev, "%s: Initialization not complete, timed out\n" , __func__);
sdw_show_ping_status(slave->bus, true );
return -ETIMEDOUT;
}
regmap_sync:
slave->unattach_request = 0 ;
regcache_cache_only(rt1316->regmap, false );
regcache_sync(rt1316->regmap);
return 0 ;
}
static const struct dev_pm_ops rt1316_pm = {
SYSTEM_SLEEP_PM_OPS(rt1316_dev_suspend, rt1316_dev_resume)
RUNTIME_PM_OPS(rt1316_dev_suspend, rt1316_dev_resume, NULL)
};
static struct sdw_driver rt1316_sdw_driver = {
.driver = {
.name = "rt1316-sdca" ,
.pm = pm_ptr(&rt1316_pm),
},
.probe = rt1316_sdw_probe,
.remove = rt1316_sdw_remove,
.ops = &rt1316_slave_ops,
.id_table = rt1316_id,
};
module_sdw_driver(rt1316_sdw_driver);
MODULE_DESCRIPTION("ASoC RT1316 driver SDCA SDW" );
MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>" );
MODULE_LICENSE("GPL" );
Messung V0.5 in Prozent C=95 H=98 G=96
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-06-05)
¤
*© Formatika GbR, Deutschland