0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/device.h>
0011 #include <linux/mod_devicetable.h>
0012 #include <linux/module.h>
0013 #include <linux/soundwire/sdw.h>
0014 #include <linux/soundwire/sdw_type.h>
0015 #include <linux/soundwire/sdw_registers.h>
0016 #include <sound/core.h>
0017 #include <sound/pcm.h>
0018 #include <sound/pcm_params.h>
0019 #include <sound/soc.h>
0020
0021 struct sdw_mockup_priv {
0022 struct sdw_slave *slave;
0023 };
0024
0025 struct sdw_stream_data {
0026 struct sdw_stream_runtime *sdw_stream;
0027 };
0028
0029 static int sdw_mockup_component_probe(struct snd_soc_component *component)
0030 {
0031 return 0;
0032 }
0033
0034 static void sdw_mockup_component_remove(struct snd_soc_component *component)
0035 {
0036 }
0037
0038 static const struct snd_soc_component_driver snd_soc_sdw_mockup_component = {
0039 .probe = sdw_mockup_component_probe,
0040 .remove = sdw_mockup_component_remove,
0041 .endianness = 1,
0042 };
0043
0044 static int sdw_mockup_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
0045 int direction)
0046 {
0047 struct sdw_stream_data *stream;
0048
0049 if (!sdw_stream)
0050 return 0;
0051
0052 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
0053 if (!stream)
0054 return -ENOMEM;
0055
0056 stream->sdw_stream = sdw_stream;
0057
0058
0059 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
0060 dai->playback_dma_data = stream;
0061 else
0062 dai->capture_dma_data = stream;
0063
0064 return 0;
0065 }
0066
0067 static void sdw_mockup_shutdown(struct snd_pcm_substream *substream,
0068 struct snd_soc_dai *dai)
0069 {
0070 struct sdw_stream_data *stream;
0071
0072 stream = snd_soc_dai_get_dma_data(dai, substream);
0073 snd_soc_dai_set_dma_data(dai, substream, NULL);
0074 kfree(stream);
0075 }
0076
0077 static int sdw_mockup_pcm_hw_params(struct snd_pcm_substream *substream,
0078 struct snd_pcm_hw_params *params,
0079 struct snd_soc_dai *dai)
0080 {
0081 struct snd_soc_component *component = dai->component;
0082 struct sdw_mockup_priv *sdw_mockup = snd_soc_component_get_drvdata(component);
0083 struct sdw_stream_config stream_config;
0084 struct sdw_port_config port_config;
0085 enum sdw_data_direction direction;
0086 struct sdw_stream_data *stream;
0087 int num_channels;
0088 int port;
0089 int ret;
0090
0091 stream = snd_soc_dai_get_dma_data(dai, substream);
0092 if (!stream)
0093 return -EINVAL;
0094
0095 if (!sdw_mockup->slave)
0096 return -EINVAL;
0097
0098
0099 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
0100 direction = SDW_DATA_DIR_RX;
0101 port = 1;
0102 } else {
0103 direction = SDW_DATA_DIR_TX;
0104 port = 8;
0105 }
0106
0107 stream_config.frame_rate = params_rate(params);
0108 stream_config.ch_count = params_channels(params);
0109 stream_config.bps = snd_pcm_format_width(params_format(params));
0110 stream_config.direction = direction;
0111
0112 num_channels = params_channels(params);
0113 port_config.ch_mask = (1 << num_channels) - 1;
0114 port_config.num = port;
0115
0116 ret = sdw_stream_add_slave(sdw_mockup->slave, &stream_config,
0117 &port_config, 1, stream->sdw_stream);
0118 if (ret)
0119 dev_err(dai->dev, "Unable to configure port\n");
0120
0121 return ret;
0122 }
0123
0124 static int sdw_mockup_pcm_hw_free(struct snd_pcm_substream *substream,
0125 struct snd_soc_dai *dai)
0126 {
0127 struct snd_soc_component *component = dai->component;
0128 struct sdw_mockup_priv *sdw_mockup = snd_soc_component_get_drvdata(component);
0129 struct sdw_stream_data *stream =
0130 snd_soc_dai_get_dma_data(dai, substream);
0131
0132 if (!sdw_mockup->slave)
0133 return -EINVAL;
0134
0135 sdw_stream_remove_slave(sdw_mockup->slave, stream->sdw_stream);
0136 return 0;
0137 }
0138
0139 static const struct snd_soc_dai_ops sdw_mockup_ops = {
0140 .hw_params = sdw_mockup_pcm_hw_params,
0141 .hw_free = sdw_mockup_pcm_hw_free,
0142 .set_stream = sdw_mockup_set_sdw_stream,
0143 .shutdown = sdw_mockup_shutdown,
0144 };
0145
0146 static struct snd_soc_dai_driver sdw_mockup_dai[] = {
0147 {
0148 .name = "sdw-mockup-aif1",
0149 .id = 1,
0150 .playback = {
0151 .stream_name = "DP1 Playback",
0152 .channels_min = 1,
0153 .channels_max = 2,
0154 },
0155 .capture = {
0156 .stream_name = "DP8 Capture",
0157 .channels_min = 1,
0158 .channels_max = 2,
0159 },
0160 .ops = &sdw_mockup_ops,
0161 },
0162 };
0163
0164 static int sdw_mockup_update_status(struct sdw_slave *slave,
0165 enum sdw_slave_status status)
0166 {
0167 return 0;
0168 }
0169
0170 static int sdw_mockup_read_prop(struct sdw_slave *slave)
0171 {
0172 struct sdw_slave_prop *prop = &slave->prop;
0173 int nval;
0174 int i, j;
0175 u32 bit;
0176 unsigned long addr;
0177 struct sdw_dpn_prop *dpn;
0178
0179 prop->paging_support = false;
0180
0181
0182
0183
0184
0185
0186
0187
0188 prop->source_ports = BIT(8);
0189 prop->sink_ports = BIT(1);
0190
0191 nval = hweight32(prop->source_ports);
0192 prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
0193 sizeof(*prop->src_dpn_prop),
0194 GFP_KERNEL);
0195 if (!prop->src_dpn_prop)
0196 return -ENOMEM;
0197
0198 i = 0;
0199 dpn = prop->src_dpn_prop;
0200 addr = prop->source_ports;
0201 for_each_set_bit(bit, &addr, 32) {
0202 dpn[i].num = bit;
0203 dpn[i].type = SDW_DPN_FULL;
0204 dpn[i].simple_ch_prep_sm = true;
0205 i++;
0206 }
0207
0208
0209 nval = hweight32(prop->sink_ports);
0210 prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
0211 sizeof(*prop->sink_dpn_prop),
0212 GFP_KERNEL);
0213 if (!prop->sink_dpn_prop)
0214 return -ENOMEM;
0215
0216 j = 0;
0217 dpn = prop->sink_dpn_prop;
0218 addr = prop->sink_ports;
0219 for_each_set_bit(bit, &addr, 32) {
0220 dpn[j].num = bit;
0221 dpn[j].type = SDW_DPN_FULL;
0222 dpn[j].simple_ch_prep_sm = true;
0223 j++;
0224 }
0225
0226 prop->simple_clk_stop_capable = true;
0227
0228
0229 prop->wake_capable = 0;
0230
0231 return 0;
0232 }
0233
0234 static int sdw_mockup_bus_config(struct sdw_slave *slave,
0235 struct sdw_bus_params *params)
0236 {
0237 return 0;
0238 }
0239
0240 static int sdw_mockup_interrupt_callback(struct sdw_slave *slave,
0241 struct sdw_slave_intr_status *status)
0242 {
0243 return 0;
0244 }
0245
0246 static const struct sdw_slave_ops sdw_mockup_slave_ops = {
0247 .read_prop = sdw_mockup_read_prop,
0248 .interrupt_callback = sdw_mockup_interrupt_callback,
0249 .update_status = sdw_mockup_update_status,
0250 .bus_config = sdw_mockup_bus_config,
0251 };
0252
0253 static int sdw_mockup_sdw_probe(struct sdw_slave *slave,
0254 const struct sdw_device_id *id)
0255 {
0256 struct device *dev = &slave->dev;
0257 struct sdw_mockup_priv *sdw_mockup;
0258 int ret;
0259
0260 sdw_mockup = devm_kzalloc(dev, sizeof(*sdw_mockup), GFP_KERNEL);
0261 if (!sdw_mockup)
0262 return -ENOMEM;
0263
0264 dev_set_drvdata(dev, sdw_mockup);
0265 sdw_mockup->slave = slave;
0266
0267 slave->is_mockup_device = true;
0268
0269 ret = devm_snd_soc_register_component(dev,
0270 &snd_soc_sdw_mockup_component,
0271 sdw_mockup_dai,
0272 ARRAY_SIZE(sdw_mockup_dai));
0273
0274 return ret;
0275 }
0276
0277 static int sdw_mockup_sdw_remove(struct sdw_slave *slave)
0278 {
0279 return 0;
0280 }
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290 static const struct sdw_device_id sdw_mockup_id[] = {
0291 SDW_SLAVE_ENTRY_EXT(0x0105, 0xAAAA, 0x0, 0, 0),
0292 SDW_SLAVE_ENTRY_EXT(0x0105, 0xAA55, 0x0, 0, 0),
0293 SDW_SLAVE_ENTRY_EXT(0x0105, 0x55AA, 0x0, 0, 0),
0294 SDW_SLAVE_ENTRY_EXT(0x0105, 0x5555, 0x0, 0, 0),
0295 {},
0296 };
0297 MODULE_DEVICE_TABLE(sdw, sdw_mockup_id);
0298
0299 static struct sdw_driver sdw_mockup_sdw_driver = {
0300 .driver = {
0301 .name = "sdw-mockup",
0302 .owner = THIS_MODULE,
0303 },
0304 .probe = sdw_mockup_sdw_probe,
0305 .remove = sdw_mockup_sdw_remove,
0306 .ops = &sdw_mockup_slave_ops,
0307 .id_table = sdw_mockup_id,
0308 };
0309 module_sdw_driver(sdw_mockup_sdw_driver);
0310
0311 MODULE_DESCRIPTION("ASoC SDW mockup codec driver");
0312 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
0313 MODULE_LICENSE("GPL");