// SPDX-License-Identifier: GPL-2.0-only
//
// rt1318-sdw.c -- rt1318 SDCA ALSA SoC amplifier audio driver
//
// Copyright(c) 2022 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 "rt1318-sdw.h"
static const struct reg_sequence rt1318_blind_write[] = {
{ 0 xc001, 0 x43 },
{ 0 xc003, 0 xa2 },
{ 0 xc004, 0 x44 },
{ 0 xc005, 0 x44 },
{ 0 xc006, 0 x33 },
{ 0 xc007, 0 x64 },
{ 0 xc320, 0 x20 },
{ 0 xf203, 0 x18 },
{ 0 xf211, 0 x00 },
{ 0 xf212, 0 x26 },
{ 0 xf20d, 0 x17 },
{ 0 xf214, 0 x06 },
{ 0 xf20e, 0 x00 },
{ 0 xf223, 0 x7f },
{ 0 xf224, 0 xdb },
{ 0 xf225, 0 xee },
{ 0 xf226, 0 x3f },
{ 0 xf227, 0 x0f },
{ 0 xf21a, 0 x78 },
{ 0 xf242, 0 x3c },
{ 0 xc321, 0 x0b },
{ 0 xc200, 0 xd8 },
{ 0 xc201, 0 x27 },
{ 0 xc202, 0 x0f },
{ 0 xf800, 0 x20 },
{ 0 xdf00, 0 x10 },
{ 0 xdf5f, 0 x01 },
{ 0 xdf60, 0 xa7 },
{ 0 xc400, 0 x0e },
{ 0 xc401, 0 x43 },
{ 0 xc402, 0 xe0 },
{ 0 xc403, 0 x00 },
{ 0 xc404, 0 x4c },
{ 0 xc407, 0 x02 },
{ 0 xc408, 0 x3f },
{ 0 xc300, 0 x01 },
{ 0 xc206, 0 x78 },
{ 0 xc203, 0 x84 },
{ 0 xc120, 0 xc0 },
{ 0 xc121, 0 x03 },
{ 0 xe000, 0 x88 },
{ 0 xc321, 0 x09 },
{ 0 xc322, 0 x01 },
{ 0 xe706, 0 x0f },
{ 0 xe707, 0 x30 },
{ 0 xe806, 0 x0f },
{ 0 xe807, 0 x30 },
{ 0 xed00, 0 xb0 },
{ 0 xce04, 0 x02 },
{ 0 xce05, 0 x63 },
{ 0 xce06, 0 x68 },
{ 0 xce07, 0 x07 },
{ 0 xcf04, 0 x02 },
{ 0 xcf05, 0 x63 },
{ 0 xcf06, 0 x68 },
{ 0 xcf07, 0 x07 },
{ 0 xce60, 0 xe3 },
{ 0 xc130, 0 x51 },
{ 0 xf102, 0 x00 },
{ 0 xf103, 0 x00 },
{ 0 xf104, 0 xf5 },
{ 0 xf105, 0 x06 },
{ 0 xf109, 0 x9b },
{ 0 xf10a, 0 x0b },
{ 0 xf10b, 0 x4c },
{ 0 xf10b, 0 x5c },
{ 0 xf102, 0 x00 },
{ 0 xf103, 0 x00 },
{ 0 xf104, 0 xf5 },
{ 0 xf105, 0 x0b },
{ 0 xf109, 0 x03 },
{ 0 xf10a, 0 x0b },
{ 0 xf10b, 0 x4c },
{ 0 xf10b, 0 x5c },
{ 0 xf102, 0 x00 },
{ 0 xf103, 0 x00 },
{ 0 xf104, 0 xf5 },
{ 0 xf105, 0 x0c },
{ 0 xf109, 0 x7f },
{ 0 xf10a, 0 x0b },
{ 0 xf10b, 0 x4c },
{ 0 xf10b, 0 x5c },
{ 0 xe604, 0 x00 },
{ 0 xdb00, 0 x0c },
{ 0 xdd00, 0 x0c },
{ 0 xdc19, 0 x00 },
{ 0 xdc1a, 0 xff },
{ 0 xdc1b, 0 xff },
{ 0 xdc1c, 0 xff },
{ 0 xdc1d, 0 x00 },
{ 0 xdc1e, 0 x00 },
{ 0 xdc1f, 0 x00 },
{ 0 xdc20, 0 xff },
{ 0 xde19, 0 x00 },
{ 0 xde1a, 0 xff },
{ 0 xde1b, 0 xff },
{ 0 xde1c, 0 xff },
{ 0 xde1d, 0 x00 },
{ 0 xde1e, 0 x00 },
{ 0 xde1f, 0 x00 },
{ 0 xde20, 0 xff },
{ 0 xdb32, 0 x00 },
{ 0 xdd32, 0 x00 },
{ 0 xdb33, 0 x0a },
{ 0 xdd33, 0 x0a },
{ 0 xdb34, 0 x1a },
{ 0 xdd34, 0 x1a },
{ 0 xdb17, 0 xef },
{ 0 xdd17, 0 xef },
{ 0 xdba7, 0 x00 },
{ 0 xdba8, 0 x64 },
{ 0 xdda7, 0 x00 },
{ 0 xdda8, 0 x64 },
{ 0 xdb19, 0 x40 },
{ 0 xdd19, 0 x40 },
{ 0 xdb00, 0 x4c },
{ 0 xdb01, 0 x79 },
{ 0 xdd01, 0 x79 },
{ 0 xdb04, 0 x05 },
{ 0 xdb05, 0 x03 },
{ 0 xdd04, 0 x05 },
{ 0 xdd05, 0 x03 },
{ 0 xdbbb, 0 x09 },
{ 0 xdbbc, 0 x30 },
{ 0 xdbbd, 0 xf0 },
{ 0 xdbbe, 0 xf1 },
{ 0 xddbb, 0 x09 },
{ 0 xddbc, 0 x30 },
{ 0 xddbd, 0 xf0 },
{ 0 xddbe, 0 xf1 },
{ 0 xdb01, 0 x79 },
{ 0 xdd01, 0 x79 },
{ 0 xdc52, 0 xef },
{ 0 xde52, 0 xef },
{ 0 x2f55, 0 x22 },
};
static const struct reg_default rt1318_reg_defaults[] = {
{ 0 x3000, 0 x00 },
{ 0 x3004, 0 x01 },
{ 0 x3005, 0 x23 },
{ 0 x3202, 0 x00 },
{ 0 x3203, 0 x01 },
{ 0 x3206, 0 x00 },
{ 0 xc000, 0 x00 },
{ 0 xc001, 0 x43 },
{ 0 xc003, 0 x22 },
{ 0 xc004, 0 x44 },
{ 0 xc005, 0 x44 },
{ 0 xc006, 0 x33 },
{ 0 xc007, 0 x64 },
{ 0 xc008, 0 x05 },
{ 0 xc00a, 0 xfc },
{ 0 xc00b, 0 x0f },
{ 0 xc00c, 0 x0e },
{ 0 xc00d, 0 xef },
{ 0 xc00e, 0 xe5 },
{ 0 xc00f, 0 xff },
{ 0 xc120, 0 xc0 },
{ 0 xc121, 0 x00 },
{ 0 xc122, 0 x00 },
{ 0 xc123, 0 x14 },
{ 0 xc125, 0 x00 },
{ 0 xc200, 0 x00 },
{ 0 xc201, 0 x00 },
{ 0 xc202, 0 x00 },
{ 0 xc203, 0 x04 },
{ 0 xc204, 0 x00 },
{ 0 xc205, 0 x00 },
{ 0 xc206, 0 x68 },
{ 0 xc207, 0 x70 },
{ 0 xc208, 0 x00 },
{ 0 xc20a, 0 x00 },
{ 0 xc20b, 0 x01 },
{ 0 xc20c, 0 x7f },
{ 0 xc20d, 0 x01 },
{ 0 xc20e, 0 x7f },
{ 0 xc300, 0 x00 },
{ 0 xc301, 0 x00 },
{ 0 xc303, 0 x80 },
{ 0 xc320, 0 x00 },
{ 0 xc321, 0 x09 },
{ 0 xc322, 0 x02 },
{ 0 xc410, 0 x04 },
{ 0 xc430, 0 x00 },
{ 0 xc431, 0 x00 },
{ 0 xca00, 0 x10 },
{ 0 xca01, 0 x00 },
{ 0 xca02, 0 x0b },
{ 0 xca10, 0 x10 },
{ 0 xca11, 0 x00 },
{ 0 xca12, 0 x0b },
{ 0 xdd93, 0 x00 },
{ 0 xdd94, 0 x64 },
{ 0 xe300, 0 xa0 },
{ 0 xed00, 0 x80 },
{ 0 xed01, 0 x0f },
{ 0 xed02, 0 xff },
{ 0 xed03, 0 x00 },
{ 0 xed04, 0 x00 },
{ 0 xed05, 0 x0f },
{ 0 xed06, 0 xff },
{ 0 xf010, 0 x10 },
{ 0 xf011, 0 xec },
{ 0 xf012, 0 x68 },
{ 0 xf013, 0 x21 },
{ 0 xf800, 0 x00 },
{ 0 xf801, 0 x12 },
{ 0 xf802, 0 xe0 },
{ 0 xf803, 0 x2f },
{ 0 xf804, 0 x00 },
{ 0 xf805, 0 x00 },
{ 0 xf806, 0 x07 },
{ 0 xf807, 0 xff },
{ SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_UDMPU21, RT1318_SDCA_CTL_UDMPU_CLUSTER, 0 ), 0 x00 },
{ SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_FU21, RT1318_SDCA_CTL_FU_MUTE, CH_L), 0 x01 },
{ SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_FU21, RT1318_SDCA_CTL_FU_MUTE, CH_R), 0 x01 },
{ SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_PDE23, RT1318_SDCA_CTL_REQ_POWER_STATE, 0 ), 0 x03 },
{ SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_CS21, RT1318_SDCA_CTL_SAMPLE_FREQ_INDEX, 0 ), 0 x09 },
};
static bool rt1318_readable_register(struct device *dev, unsigned int reg)
{
switch (reg) {
case 0 x2f55:
case 0 x3000:
case 0 x3004 ... 0 x3005:
case 0 x3202 ... 0 x3203:
case 0 x3206:
case 0 xc000 ... 0 xc00f:
case 0 xc120 ... 0 xc125:
case 0 xc200 ... 0 xc20e:
case 0 xc300 ... 0 xc303:
case 0 xc320 ... 0 xc322:
case 0 xc410:
case 0 xc430 ... 0 xc431:
case 0 xca00 ... 0 xca02:
case 0 xca10 ... 0 xca12:
case 0 xcb00 ... 0 xcb0b:
case 0 xcc00 ... 0 xcce5:
case 0 xcd00 ... 0 xcde5:
case 0 xce00 ... 0 xce6a:
case 0 xcf00 ... 0 xcf53:
case 0 xd000 ... 0 xd0cc:
case 0 xd100 ... 0 xd1b9:
case 0 xdb00 ... 0 xdc53:
case 0 xdd00 ... 0 xde53:
case 0 xdf00 ... 0 xdf6b:
case 0 xe300:
case 0 xeb00 ... 0 xebcc:
case 0 xec00 ... 0 xecb9:
case 0 xed00 ... 0 xed06:
case 0 xf010 ... 0 xf014:
case 0 xf800 ... 0 xf807:
case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_UDMPU21, RT1318_SDCA_CTL_UDMPU_CLUSTER, 0 ):
case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_FU21, RT1318_SDCA_CTL_FU_MUTE, CH_L):
case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_FU21, RT1318_SDCA_CTL_FU_MUTE, CH_R):
case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_PDE23, RT1318_SDCA_CTL_REQ_POWER_STATE, 0 ):
case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_CS21, RT1318_SDCA_CTL_SAMPLE_FREQ_INDEX, 0 ):
case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_SAPU, RT1318_SDCA_CTL_SAPU_PROTECTION_MODE, 0 ):
case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_SAPU, RT1318_SDCA_CTL_SAPU_PROTECTION_STATUS, 0 ):
return true ;
default :
return false ;
}
}
static bool rt1318_volatile_register(struct device *dev, unsigned int reg)
{
switch (reg) {
case 0 x2f55:
case 0 x3000 ... 0 x3001:
case 0 xc000:
case 0 xc301:
case 0 xc410:
case 0 xc430 ... 0 xc431:
case 0 xdb06:
case 0 xdb12:
case 0 xdb1d ... 0 xdb1f:
case 0 xdb35:
case 0 xdb37:
case 0 xdb8a ... 0 xdb92:
case 0 xdbc5 ... 0 xdbc8:
case 0 xdc2b ... 0 xdc49:
case 0 xdd0b:
case 0 xdd12:
case 0 xdd1d ... 0 xdd1f:
case 0 xdd35:
case 0 xdd8a ... 0 xdd92:
case 0 xddc5 ... 0 xddc8:
case 0 xde2b ... 0 xde44:
case 0 xdf4a ... 0 xdf55:
case 0 xe224 ... 0 xe23b:
case 0 xea01:
case 0 xebc5:
case 0 xebc8:
case 0 xebcb ... 0 xebcc:
case 0 xed03 ... 0 xed06:
case 0 xf010 ... 0 xf014:
case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_SAPU, RT1318_SDCA_CTL_SAPU_PROTECTION_MODE, 0 ):
case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_SAPU, RT1318_SDCA_CTL_SAPU_PROTECTION_STATUS, 0 ):
return true ;
default :
return false ;
}
}
static const struct regmap_config rt1318_sdw_regmap = {
.reg_bits = 32 ,
.val_bits = 8 ,
.readable_reg = rt1318_readable_register,
.volatile_reg = rt1318_volatile_register,
.max_register = 0 x41081488,
.reg_defaults = rt1318_reg_defaults,
.num_reg_defaults = ARRAY_SIZE(rt1318_reg_defaults),
.cache_type = REGCACHE_MAPLE,
.use_single_read = true ,
.use_single_write = true ,
};
static int rt1318_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 = BIT(2 );
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 = 20 ;
return 0 ;
}
static int rt1318_io_init(struct device *dev, struct sdw_slave *slave)
{
struct rt1318_sdw_priv *rt1318 = dev_get_drvdata(dev);
if (rt1318->hw_init)
return 0 ;
regcache_cache_only(rt1318->regmap, false );
if (rt1318->first_hw_init) {
regcache_cache_bypass(rt1318->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);
/* blind write */
regmap_multi_reg_write(rt1318->regmap, rt1318_blind_write,
ARRAY_SIZE(rt1318_blind_write));
if (rt1318->first_hw_init) {
regcache_cache_bypass(rt1318->regmap, false );
regcache_mark_dirty(rt1318->regmap);
}
/* Mark Slave initialization complete */
rt1318->first_hw_init = true ;
rt1318->hw_init = true ;
pm_runtime_put_autosuspend(&slave->dev);
dev_dbg(&slave->dev, "%s hw_init complete\n" , __func__);
return 0 ;
}
static int rt1318_update_status(struct sdw_slave *slave,
enum sdw_slave_status status)
{
struct rt1318_sdw_priv *rt1318 = dev_get_drvdata(&slave->dev);
if (status == SDW_SLAVE_UNATTACHED)
rt1318->hw_init = false ;
/*
* Perform initialization only if slave status is present and
* hw_init flag is false
*/
if (rt1318->hw_init || status != SDW_SLAVE_ATTACHED)
return 0 ;
/* perform I/O transfers required for Slave initialization */
return rt1318_io_init(&slave->dev, slave);
}
static int rt1318_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 rt1318_sdw_priv *rt1318 = snd_soc_component_get_drvdata(component);
unsigned char ps0 = 0 x0, ps3 = 0 x3;
switch (event) {
case SND_SOC_DAPM_POST_PMU:
regmap_write(rt1318->regmap,
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_PDE23,
RT1318_SDCA_CTL_REQ_POWER_STATE, 0 ),
ps0);
break ;
case SND_SOC_DAPM_PRE_PMD:
regmap_write(rt1318->regmap,
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_PDE23,
RT1318_SDCA_CTL_REQ_POWER_STATE, 0 ),
ps3);
break ;
default :
break ;
}
return 0 ;
}
static const char * const rt1318_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(rt1318_rx_data_ch_enum,
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_UDMPU21, RT1318_SDCA_CTL_UDMPU_CLUSTER, 0 ), 0 ,
rt1318_rx_data_ch_select);
static const struct snd_kcontrol_new rt1318_snd_controls[] = {
/* UDMPU Cluster Selection */
SOC_ENUM("RX Channel Select" , rt1318_rx_data_ch_enum),
};
static const struct snd_kcontrol_new rt1318_sto_dac =
SOC_DAPM_DOUBLE_R("Switch" ,
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_FU21, RT1318_SDCA_CTL_FU_MUTE, CH_L),
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_FU21, RT1318_SDCA_CTL_FU_MUTE, CH_R),
0 , 1 , 1 );
static const struct snd_soc_dapm_widget rt1318_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 , &rt1318_sto_dac),
/* Output */
SND_SOC_DAPM_PGA_E("CLASS D" , SND_SOC_NOPM, 0 , 0 , NULL, 0 ,
rt1318_classd_event, SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
SND_SOC_DAPM_OUTPUT("SPOL" ),
SND_SOC_DAPM_OUTPUT("SPOR" ),
/* Input */
SND_SOC_DAPM_PGA("FB Data" , SND_SOC_NOPM, 0 , 0 , NULL, 0 ),
SND_SOC_DAPM_SIGGEN("FB Gen" ),
};
static const struct snd_soc_dapm_route rt1318_dapm_routes[] = {
{ "DAC" , "Switch" , "DP1RX" },
{ "CLASS D" , NULL, "DAC" },
{ "SPOL" , NULL, "CLASS D" },
{ "SPOR" , NULL, "CLASS D" },
{ "FB Data" , NULL, "FB Gen" },
{ "DP2TX" , NULL, "FB Data" },
};
static int rt1318_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 rt1318_sdw_shutdown(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
snd_soc_dai_set_dma_data(dai, substream, NULL);
}
static int rt1318_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 rt1318_sdw_priv *rt1318 =
snd_soc_component_get_drvdata(component);
struct sdw_stream_config stream_config;
struct sdw_port_config port_config;
enum sdw_data_direction direction;
struct sdw_stream_runtime *sdw_stream;
int retval, port, num_channels, ch_mask;
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 (!rt1318->sdw_slave)
return -EINVAL;
/* SoundWire specific configuration */
/* port 1 for playback */
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
direction = SDW_DATA_DIR_RX;
port = 1 ;
} else {
direction = SDW_DATA_DIR_TX;
port = 2 ;
}
num_channels = params_channels(params);
ch_mask = (1 << num_channels) - 1 ;
stream_config.frame_rate = params_rate(params);
stream_config.ch_count = num_channels;
stream_config.bps = snd_pcm_format_width(params_format(params));
stream_config.direction = direction;
port_config.ch_mask = ch_mask;
port_config.num = port;
retval = sdw_stream_add_slave(rt1318->sdw_slave, &stream_config,
&port_config, 1 , sdw_stream);
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 = RT1318_SDCA_RATE_16000HZ;
break ;
case 32000 :
sampling_rate = RT1318_SDCA_RATE_32000HZ;
break ;
case 44100 :
sampling_rate = RT1318_SDCA_RATE_44100HZ;
break ;
case 48000 :
sampling_rate = RT1318_SDCA_RATE_48000HZ;
break ;
case 96000 :
sampling_rate = RT1318_SDCA_RATE_96000HZ;
break ;
case 192000 :
sampling_rate = RT1318_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 */
regmap_write(rt1318->regmap,
SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_CS21, RT1318_SDCA_CTL_SAMPLE_FREQ_INDEX, 0 ),
sampling_rate);
return 0 ;
}
static int rt1318_sdw_pcm_hw_free(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
struct snd_soc_component *component = dai->component;
struct rt1318_sdw_priv *rt1318 =
snd_soc_component_get_drvdata(component);
struct sdw_stream_runtime *sdw_stream =
snd_soc_dai_get_dma_data(dai, substream);
if (!rt1318->sdw_slave)
return -EINVAL;
sdw_stream_remove_slave(rt1318->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 rt1318_slave_ops = {
.read_prop = rt1318_read_prop,
.update_status = rt1318_update_status,
};
static int rt1318_sdw_component_probe(struct snd_soc_component *component)
{
int ret;
struct rt1318_sdw_priv *rt1318 = snd_soc_component_get_drvdata(component);
rt1318->component = component;
if (!rt1318->first_hw_init)
return 0 ;
ret = pm_runtime_resume(component->dev);
dev_dbg(&rt1318->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_rt1318 = {
.probe = rt1318_sdw_component_probe,
.controls = rt1318_snd_controls,
.num_controls = ARRAY_SIZE(rt1318_snd_controls),
.dapm_widgets = rt1318_dapm_widgets,
.num_dapm_widgets = ARRAY_SIZE(rt1318_dapm_widgets),
.dapm_routes = rt1318_dapm_routes,
.num_dapm_routes = ARRAY_SIZE(rt1318_dapm_routes),
.endianness = 1 ,
};
static const struct snd_soc_dai_ops rt1318_aif_dai_ops = {
.hw_params = rt1318_sdw_hw_params,
.hw_free = rt1318_sdw_pcm_hw_free,
.set_stream = rt1318_set_sdw_stream,
.shutdown = rt1318_sdw_shutdown,
};
#define RT1318_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 RT1318_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | \
SNDRV_PCM_FMTBIT_S32_LE)
static struct snd_soc_dai_driver rt1318_sdw_dai[] = {
{
.name = "rt1318-aif" ,
.playback = {
.stream_name = "DP1 Playback" ,
.channels_min = 1 ,
.channels_max = 2 ,
.rates = RT1318_STEREO_RATES,
.formats = RT1318_FORMATS,
},
.capture = {
.stream_name = "DP2 Capture" ,
.channels_min = 1 ,
.channels_max = 2 ,
.rates = RT1318_STEREO_RATES,
.formats = RT1318_FORMATS,
},
.ops = &rt1318_aif_dai_ops,
},
};
static int rt1318_sdw_init(struct device *dev, struct regmap *regmap,
struct sdw_slave *slave)
{
struct rt1318_sdw_priv *rt1318;
int ret;
rt1318 = devm_kzalloc(dev, sizeof (*rt1318), GFP_KERNEL);
if (!rt1318)
return -ENOMEM;
dev_set_drvdata(dev, rt1318);
rt1318->sdw_slave = slave;
rt1318->regmap = regmap;
regcache_cache_only(rt1318->regmap, true );
/*
* Mark hw_init to false
* HW init will be performed when device reports present
*/
rt1318->hw_init = false ;
rt1318->first_hw_init = false ;
ret = devm_snd_soc_register_component(dev,
&soc_component_sdw_rt1318,
rt1318_sdw_dai,
ARRAY_SIZE(rt1318_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 rt1318_sdw_probe(struct sdw_slave *slave,
const struct sdw_device_id *id)
{
struct regmap *regmap;
/* Regmap Initialization */
regmap = devm_regmap_init_sdw(slave, &rt1318_sdw_regmap);
if (IS_ERR(regmap))
return PTR_ERR(regmap);
return rt1318_sdw_init(&slave->dev, regmap, slave);
}
static int rt1318_sdw_remove(struct sdw_slave *slave)
{
pm_runtime_disable(&slave->dev);
return 0 ;
}
static const struct sdw_device_id rt1318_id[] = {
SDW_SLAVE_ENTRY_EXT(0 x025d, 0 x1318, 0 x3, 0 x1, 0 ),
{},
};
MODULE_DEVICE_TABLE(sdw, rt1318_id);
static int rt1318_dev_suspend(struct device *dev)
{
struct rt1318_sdw_priv *rt1318 = dev_get_drvdata(dev);
if (!rt1318->hw_init)
return 0 ;
regcache_cache_only(rt1318->regmap, true );
return 0 ;
}
#define RT1318_PROBE_TIMEOUT 5000
static int rt1318_dev_resume(struct device *dev)
{
struct sdw_slave *slave = dev_to_sdw_dev(dev);
struct rt1318_sdw_priv *rt1318 = dev_get_drvdata(dev);
unsigned long time;
if (!rt1318->first_hw_init)
return 0 ;
if (!slave->unattach_request)
goto regmap_sync;
time = wait_for_completion_timeout(&slave->initialization_complete,
msecs_to_jiffies(RT1318_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(rt1318->regmap, false );
regcache_sync(rt1318->regmap);
return 0 ;
}
static const struct dev_pm_ops rt1318_pm = {
SYSTEM_SLEEP_PM_OPS(rt1318_dev_suspend, rt1318_dev_resume)
RUNTIME_PM_OPS(rt1318_dev_suspend, rt1318_dev_resume, NULL)
};
static struct sdw_driver rt1318_sdw_driver = {
.driver = {
.name = "rt1318-sdca" ,
.pm = pm_ptr(&rt1318_pm),
},
.probe = rt1318_sdw_probe,
.remove = rt1318_sdw_remove,
.ops = &rt1318_slave_ops,
.id_table = rt1318_id,
};
module_sdw_driver(rt1318_sdw_driver);
MODULE_DESCRIPTION("ASoC RT1318 driver SDCA SDW" );
MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>" );
MODULE_LICENSE("GPL" );
Messung V0.5 in Prozent C=97 H=100 G=98
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet am 2026-06-05)
¤
*© Formatika GbR, Deutschland