1 // SPDX-License-Identifier: GPL-2.0-only
3 // sdw-mockup.c -- a mockup SoundWire codec for tests where only the host
6 // Copyright(c) 2021 Intel Corporation
10 #include <linux/device.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/module.h>
13 #include <linux/soundwire/sdw.h>
14 #include <linux/soundwire/sdw_type.h>
15 #include <linux/soundwire/sdw_registers.h>
16 #include <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
21 struct sdw_mockup_priv {
22 struct sdw_slave *slave;
25 struct sdw_stream_data {
26 struct sdw_stream_runtime *sdw_stream;
29 static int sdw_mockup_component_probe(struct snd_soc_component *component)
34 static void sdw_mockup_component_remove(struct snd_soc_component *component)
38 static const struct snd_soc_component_driver snd_soc_sdw_mockup_component = {
39 .probe = sdw_mockup_component_probe,
40 .remove = sdw_mockup_component_remove,
43 static int sdw_mockup_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
46 struct sdw_stream_data *stream;
51 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
55 stream->sdw_stream = sdw_stream;
57 /* Use tx_mask or rx_mask to configure stream tag and set dma_data */
58 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
59 dai->playback_dma_data = stream;
61 dai->capture_dma_data = stream;
66 static void sdw_mockup_shutdown(struct snd_pcm_substream *substream,
67 struct snd_soc_dai *dai)
69 struct sdw_stream_data *stream;
71 stream = snd_soc_dai_get_dma_data(dai, substream);
72 snd_soc_dai_set_dma_data(dai, substream, NULL);
76 static int sdw_mockup_pcm_hw_params(struct snd_pcm_substream *substream,
77 struct snd_pcm_hw_params *params,
78 struct snd_soc_dai *dai)
80 struct snd_soc_component *component = dai->component;
81 struct sdw_mockup_priv *sdw_mockup = snd_soc_component_get_drvdata(component);
82 struct sdw_stream_config stream_config;
83 struct sdw_port_config port_config;
84 enum sdw_data_direction direction;
85 struct sdw_stream_data *stream;
90 stream = snd_soc_dai_get_dma_data(dai, substream);
94 if (!sdw_mockup->slave)
97 /* SoundWire specific configuration */
98 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
99 direction = SDW_DATA_DIR_RX;
102 direction = SDW_DATA_DIR_TX;
106 stream_config.frame_rate = params_rate(params);
107 stream_config.ch_count = params_channels(params);
108 stream_config.bps = snd_pcm_format_width(params_format(params));
109 stream_config.direction = direction;
111 num_channels = params_channels(params);
112 port_config.ch_mask = (1 << num_channels) - 1;
113 port_config.num = port;
115 ret = sdw_stream_add_slave(sdw_mockup->slave, &stream_config,
116 &port_config, 1, stream->sdw_stream);
118 dev_err(dai->dev, "Unable to configure port\n");
123 static int sdw_mockup_pcm_hw_free(struct snd_pcm_substream *substream,
124 struct snd_soc_dai *dai)
126 struct snd_soc_component *component = dai->component;
127 struct sdw_mockup_priv *sdw_mockup = snd_soc_component_get_drvdata(component);
128 struct sdw_stream_data *stream =
129 snd_soc_dai_get_dma_data(dai, substream);
131 if (!sdw_mockup->slave)
134 sdw_stream_remove_slave(sdw_mockup->slave, stream->sdw_stream);
138 static const struct snd_soc_dai_ops sdw_mockup_ops = {
139 .hw_params = sdw_mockup_pcm_hw_params,
140 .hw_free = sdw_mockup_pcm_hw_free,
141 .set_sdw_stream = sdw_mockup_set_sdw_stream,
142 .shutdown = sdw_mockup_shutdown,
145 static struct snd_soc_dai_driver sdw_mockup_dai[] = {
147 .name = "sdw-mockup-aif1",
150 .stream_name = "DP1 Playback",
155 .stream_name = "DP8 Capture",
159 .ops = &sdw_mockup_ops,
163 static int sdw_mockup_update_status(struct sdw_slave *slave,
164 enum sdw_slave_status status)
169 static int sdw_mockup_read_prop(struct sdw_slave *slave)
171 struct sdw_slave_prop *prop = &slave->prop;
176 struct sdw_dpn_prop *dpn;
178 prop->paging_support = false;
181 * first we need to allocate memory for set bits in port lists
182 * the port allocation is completely arbitrary:
183 * DP0 is not supported
187 prop->source_ports = BIT(8);
188 prop->sink_ports = BIT(1);
190 nval = hweight32(prop->source_ports);
191 prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
192 sizeof(*prop->src_dpn_prop),
194 if (!prop->src_dpn_prop)
198 dpn = prop->src_dpn_prop;
199 addr = prop->source_ports;
200 for_each_set_bit(bit, &addr, 32) {
202 dpn[i].type = SDW_DPN_FULL;
203 dpn[i].simple_ch_prep_sm = true;
207 /* do this again for sink now */
208 nval = hweight32(prop->sink_ports);
209 prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
210 sizeof(*prop->sink_dpn_prop),
212 if (!prop->sink_dpn_prop)
216 dpn = prop->sink_dpn_prop;
217 addr = prop->sink_ports;
218 for_each_set_bit(bit, &addr, 32) {
220 dpn[j].type = SDW_DPN_FULL;
221 dpn[j].simple_ch_prep_sm = true;
225 prop->simple_clk_stop_capable = true;
228 prop->wake_capable = 0;
233 static int sdw_mockup_bus_config(struct sdw_slave *slave,
234 struct sdw_bus_params *params)
239 static int sdw_mockup_interrupt_callback(struct sdw_slave *slave,
240 struct sdw_slave_intr_status *status)
245 static const struct sdw_slave_ops sdw_mockup_slave_ops = {
246 .read_prop = sdw_mockup_read_prop,
247 .interrupt_callback = sdw_mockup_interrupt_callback,
248 .update_status = sdw_mockup_update_status,
249 .bus_config = sdw_mockup_bus_config,
252 static int sdw_mockup_sdw_probe(struct sdw_slave *slave,
253 const struct sdw_device_id *id)
255 struct device *dev = &slave->dev;
256 struct sdw_mockup_priv *sdw_mockup;
259 sdw_mockup = devm_kzalloc(dev, sizeof(*sdw_mockup), GFP_KERNEL);
263 dev_set_drvdata(dev, sdw_mockup);
264 sdw_mockup->slave = slave;
266 slave->is_mockup_device = true;
268 ret = devm_snd_soc_register_component(dev,
269 &snd_soc_sdw_mockup_component,
271 ARRAY_SIZE(sdw_mockup_dai));
276 static int sdw_mockup_sdw_remove(struct sdw_slave *slave)
282 * Intel reserved parts ID with the following mapping expected:
283 * 0xAAAA: generic full-duplex codec
284 * 0xAA55: headset codec (mock-up of RT711/RT5682) - full-duplex
285 * 0x55AA: amplifier (mock-up of RT1308/Maxim 98373) - playback only with
287 * 0x5555: mic codec (mock-up of RT715) - capture-only
289 static const struct sdw_device_id sdw_mockup_id[] = {
290 SDW_SLAVE_ENTRY_EXT(0x0105, 0xAAAA, 0x0, 0, 0),
291 SDW_SLAVE_ENTRY_EXT(0x0105, 0xAA55, 0x0, 0, 0),
292 SDW_SLAVE_ENTRY_EXT(0x0105, 0x55AA, 0x0, 0, 0),
293 SDW_SLAVE_ENTRY_EXT(0x0105, 0x5555, 0x0, 0, 0),
296 MODULE_DEVICE_TABLE(sdw, sdw_mockup_id);
298 static struct sdw_driver sdw_mockup_sdw_driver = {
300 .name = "sdw-mockup",
301 .owner = THIS_MODULE,
303 .probe = sdw_mockup_sdw_probe,
304 .remove = sdw_mockup_sdw_remove,
305 .ops = &sdw_mockup_slave_ops,
306 .id_table = sdw_mockup_id,
308 module_sdw_driver(sdw_mockup_sdw_driver);
310 MODULE_DESCRIPTION("ASoC SDW mockup codec driver");
311 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
312 MODULE_LICENSE("GPL");