1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // sdw-mockup.c -- a mockup SoundWire codec for tests where only the host 4 // drives the bus. 5 // 6 // Copyright(c) 2021 Intel Corporation 7 // 8 // 9 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/sdw.h> 20 #include <sound/soc.h> 21 22 struct sdw_mockup_priv { 23 struct sdw_slave *slave; 24 }; 25 26 struct sdw_stream_data { 27 struct sdw_stream_runtime *sdw_stream; 28 }; 29 30 static int sdw_mockup_component_probe(struct snd_soc_component *component) 31 { 32 return 0; 33 } 34 35 static void sdw_mockup_component_remove(struct snd_soc_component *component) 36 { 37 } 38 39 static const struct snd_soc_component_driver snd_soc_sdw_mockup_component = { 40 .probe = sdw_mockup_component_probe, 41 .remove = sdw_mockup_component_remove, 42 .endianness = 1, 43 }; 44 45 static int sdw_mockup_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream, 46 int direction) 47 { 48 struct sdw_stream_data *stream; 49 50 if (!sdw_stream) 51 return 0; 52 53 stream = kzalloc(sizeof(*stream), GFP_KERNEL); 54 if (!stream) 55 return -ENOMEM; 56 57 stream->sdw_stream = sdw_stream; 58 59 /* Use tx_mask or rx_mask to configure stream tag and set dma_data */ 60 if (direction == SNDRV_PCM_STREAM_PLAYBACK) 61 dai->playback_dma_data = stream; 62 else 63 dai->capture_dma_data = stream; 64 65 return 0; 66 } 67 68 static void sdw_mockup_shutdown(struct snd_pcm_substream *substream, 69 struct snd_soc_dai *dai) 70 { 71 struct sdw_stream_data *stream; 72 73 stream = snd_soc_dai_get_dma_data(dai, substream); 74 snd_soc_dai_set_dma_data(dai, substream, NULL); 75 kfree(stream); 76 } 77 78 static int sdw_mockup_pcm_hw_params(struct snd_pcm_substream *substream, 79 struct snd_pcm_hw_params *params, 80 struct snd_soc_dai *dai) 81 { 82 struct snd_soc_component *component = dai->component; 83 struct sdw_mockup_priv *sdw_mockup = snd_soc_component_get_drvdata(component); 84 struct sdw_stream_config stream_config = {0}; 85 struct sdw_port_config port_config = {0}; 86 struct sdw_stream_data *stream; 87 int ret; 88 89 stream = snd_soc_dai_get_dma_data(dai, substream); 90 if (!stream) 91 return -EINVAL; 92 93 if (!sdw_mockup->slave) 94 return -EINVAL; 95 96 /* SoundWire specific configuration */ 97 snd_sdw_params_to_config(substream, params, &stream_config, &port_config); 98 99 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 100 port_config.num = 1; 101 else 102 port_config.num = 8; 103 104 ret = sdw_stream_add_slave(sdw_mockup->slave, &stream_config, 105 &port_config, 1, stream->sdw_stream); 106 if (ret) 107 dev_err(dai->dev, "Unable to configure port\n"); 108 109 return ret; 110 } 111 112 static int sdw_mockup_pcm_hw_free(struct snd_pcm_substream *substream, 113 struct snd_soc_dai *dai) 114 { 115 struct snd_soc_component *component = dai->component; 116 struct sdw_mockup_priv *sdw_mockup = snd_soc_component_get_drvdata(component); 117 struct sdw_stream_data *stream = 118 snd_soc_dai_get_dma_data(dai, substream); 119 120 if (!sdw_mockup->slave) 121 return -EINVAL; 122 123 sdw_stream_remove_slave(sdw_mockup->slave, stream->sdw_stream); 124 return 0; 125 } 126 127 static const struct snd_soc_dai_ops sdw_mockup_ops = { 128 .hw_params = sdw_mockup_pcm_hw_params, 129 .hw_free = sdw_mockup_pcm_hw_free, 130 .set_stream = sdw_mockup_set_sdw_stream, 131 .shutdown = sdw_mockup_shutdown, 132 }; 133 134 static struct snd_soc_dai_driver sdw_mockup_dai[] = { 135 { 136 .name = "sdw-mockup-aif1", 137 .id = 1, 138 .playback = { 139 .stream_name = "DP1 Playback", 140 .channels_min = 1, 141 .channels_max = 2, 142 }, 143 .capture = { 144 .stream_name = "DP8 Capture", 145 .channels_min = 1, 146 .channels_max = 2, 147 }, 148 .ops = &sdw_mockup_ops, 149 }, 150 }; 151 152 static int sdw_mockup_update_status(struct sdw_slave *slave, 153 enum sdw_slave_status status) 154 { 155 return 0; 156 } 157 158 static int sdw_mockup_read_prop(struct sdw_slave *slave) 159 { 160 struct sdw_slave_prop *prop = &slave->prop; 161 int nval; 162 int i, j; 163 u32 bit; 164 unsigned long addr; 165 struct sdw_dpn_prop *dpn; 166 167 prop->paging_support = false; 168 169 /* 170 * first we need to allocate memory for set bits in port lists 171 * the port allocation is completely arbitrary: 172 * DP0 is not supported 173 * DP1 is sink 174 * DP8 is source 175 */ 176 prop->source_ports = BIT(8); 177 prop->sink_ports = BIT(1); 178 179 nval = hweight32(prop->source_ports); 180 prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval, 181 sizeof(*prop->src_dpn_prop), 182 GFP_KERNEL); 183 if (!prop->src_dpn_prop) 184 return -ENOMEM; 185 186 i = 0; 187 dpn = prop->src_dpn_prop; 188 addr = prop->source_ports; 189 for_each_set_bit(bit, &addr, 32) { 190 dpn[i].num = bit; 191 dpn[i].type = SDW_DPN_FULL; 192 dpn[i].simple_ch_prep_sm = true; 193 i++; 194 } 195 196 /* do this again for sink now */ 197 nval = hweight32(prop->sink_ports); 198 prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval, 199 sizeof(*prop->sink_dpn_prop), 200 GFP_KERNEL); 201 if (!prop->sink_dpn_prop) 202 return -ENOMEM; 203 204 j = 0; 205 dpn = prop->sink_dpn_prop; 206 addr = prop->sink_ports; 207 for_each_set_bit(bit, &addr, 32) { 208 dpn[j].num = bit; 209 dpn[j].type = SDW_DPN_FULL; 210 dpn[j].simple_ch_prep_sm = true; 211 j++; 212 } 213 214 prop->simple_clk_stop_capable = true; 215 216 /* wake-up event */ 217 prop->wake_capable = 0; 218 219 return 0; 220 } 221 222 static int sdw_mockup_bus_config(struct sdw_slave *slave, 223 struct sdw_bus_params *params) 224 { 225 return 0; 226 } 227 228 static int sdw_mockup_interrupt_callback(struct sdw_slave *slave, 229 struct sdw_slave_intr_status *status) 230 { 231 return 0; 232 } 233 234 static const struct sdw_slave_ops sdw_mockup_slave_ops = { 235 .read_prop = sdw_mockup_read_prop, 236 .interrupt_callback = sdw_mockup_interrupt_callback, 237 .update_status = sdw_mockup_update_status, 238 .bus_config = sdw_mockup_bus_config, 239 }; 240 241 static int sdw_mockup_sdw_probe(struct sdw_slave *slave, 242 const struct sdw_device_id *id) 243 { 244 struct device *dev = &slave->dev; 245 struct sdw_mockup_priv *sdw_mockup; 246 int ret; 247 248 sdw_mockup = devm_kzalloc(dev, sizeof(*sdw_mockup), GFP_KERNEL); 249 if (!sdw_mockup) 250 return -ENOMEM; 251 252 dev_set_drvdata(dev, sdw_mockup); 253 sdw_mockup->slave = slave; 254 255 slave->is_mockup_device = true; 256 257 ret = devm_snd_soc_register_component(dev, 258 &snd_soc_sdw_mockup_component, 259 sdw_mockup_dai, 260 ARRAY_SIZE(sdw_mockup_dai)); 261 262 return ret; 263 } 264 265 static int sdw_mockup_sdw_remove(struct sdw_slave *slave) 266 { 267 return 0; 268 } 269 270 /* 271 * Intel reserved parts ID with the following mapping expected: 272 * 0xAAAA: generic full-duplex codec 273 * 0xAA55: headset codec (mock-up of RT711/RT5682) - full-duplex 274 * 0x55AA: amplifier (mock-up of RT1308/Maxim 98373) - playback only with 275 * IV feedback 276 * 0x5555: mic codec (mock-up of RT715) - capture-only 277 */ 278 static const struct sdw_device_id sdw_mockup_id[] = { 279 SDW_SLAVE_ENTRY_EXT(0x0105, 0xAAAA, 0x0, 0, 0), 280 SDW_SLAVE_ENTRY_EXT(0x0105, 0xAA55, 0x0, 0, 0), 281 SDW_SLAVE_ENTRY_EXT(0x0105, 0x55AA, 0x0, 0, 0), 282 SDW_SLAVE_ENTRY_EXT(0x0105, 0x5555, 0x0, 0, 0), 283 {}, 284 }; 285 MODULE_DEVICE_TABLE(sdw, sdw_mockup_id); 286 287 static struct sdw_driver sdw_mockup_sdw_driver = { 288 .driver = { 289 .name = "sdw-mockup", 290 .owner = THIS_MODULE, 291 }, 292 .probe = sdw_mockup_sdw_probe, 293 .remove = sdw_mockup_sdw_remove, 294 .ops = &sdw_mockup_slave_ops, 295 .id_table = sdw_mockup_id, 296 }; 297 module_sdw_driver(sdw_mockup_sdw_driver); 298 299 MODULE_DESCRIPTION("ASoC SDW mockup codec driver"); 300 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>"); 301 MODULE_LICENSE("GPL"); 302