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/soc.h> 20 21 struct sdw_mockup_priv { 22 struct sdw_slave *slave; 23 }; 24 25 struct sdw_stream_data { 26 struct sdw_stream_runtime *sdw_stream; 27 }; 28 29 static int sdw_mockup_component_probe(struct snd_soc_component *component) 30 { 31 return 0; 32 } 33 34 static void sdw_mockup_component_remove(struct snd_soc_component *component) 35 { 36 } 37 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, 41 }; 42 43 static int sdw_mockup_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream, 44 int direction) 45 { 46 struct sdw_stream_data *stream; 47 48 if (!sdw_stream) 49 return 0; 50 51 stream = kzalloc(sizeof(*stream), GFP_KERNEL); 52 if (!stream) 53 return -ENOMEM; 54 55 stream->sdw_stream = sdw_stream; 56 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; 60 else 61 dai->capture_dma_data = stream; 62 63 return 0; 64 } 65 66 static void sdw_mockup_shutdown(struct snd_pcm_substream *substream, 67 struct snd_soc_dai *dai) 68 { 69 struct sdw_stream_data *stream; 70 71 stream = snd_soc_dai_get_dma_data(dai, substream); 72 snd_soc_dai_set_dma_data(dai, substream, NULL); 73 kfree(stream); 74 } 75 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) 79 { 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; 86 int num_channels; 87 int port; 88 int ret; 89 90 stream = snd_soc_dai_get_dma_data(dai, substream); 91 if (!stream) 92 return -EINVAL; 93 94 if (!sdw_mockup->slave) 95 return -EINVAL; 96 97 /* SoundWire specific configuration */ 98 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 99 direction = SDW_DATA_DIR_RX; 100 port = 1; 101 } else { 102 direction = SDW_DATA_DIR_TX; 103 port = 8; 104 } 105 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; 110 111 num_channels = params_channels(params); 112 port_config.ch_mask = (1 << num_channels) - 1; 113 port_config.num = port; 114 115 ret = sdw_stream_add_slave(sdw_mockup->slave, &stream_config, 116 &port_config, 1, stream->sdw_stream); 117 if (ret) 118 dev_err(dai->dev, "Unable to configure port\n"); 119 120 return ret; 121 } 122 123 static int sdw_mockup_pcm_hw_free(struct snd_pcm_substream *substream, 124 struct snd_soc_dai *dai) 125 { 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); 130 131 if (!sdw_mockup->slave) 132 return -EINVAL; 133 134 sdw_stream_remove_slave(sdw_mockup->slave, stream->sdw_stream); 135 return 0; 136 } 137 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, 143 }; 144 145 static struct snd_soc_dai_driver sdw_mockup_dai[] = { 146 { 147 .name = "sdw-mockup-aif1", 148 .id = 1, 149 .playback = { 150 .stream_name = "DP1 Playback", 151 .channels_min = 1, 152 .channels_max = 2, 153 }, 154 .capture = { 155 .stream_name = "DP8 Capture", 156 .channels_min = 1, 157 .channels_max = 2, 158 }, 159 .ops = &sdw_mockup_ops, 160 }, 161 }; 162 163 static int sdw_mockup_update_status(struct sdw_slave *slave, 164 enum sdw_slave_status status) 165 { 166 return 0; 167 } 168 169 static int sdw_mockup_read_prop(struct sdw_slave *slave) 170 { 171 struct sdw_slave_prop *prop = &slave->prop; 172 int nval; 173 int i, j; 174 u32 bit; 175 unsigned long addr; 176 struct sdw_dpn_prop *dpn; 177 178 prop->paging_support = false; 179 180 /* 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 184 * DP1 is sink 185 * DP8 is source 186 */ 187 prop->source_ports = BIT(8); 188 prop->sink_ports = BIT(1); 189 190 nval = hweight32(prop->source_ports); 191 prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval, 192 sizeof(*prop->src_dpn_prop), 193 GFP_KERNEL); 194 if (!prop->src_dpn_prop) 195 return -ENOMEM; 196 197 i = 0; 198 dpn = prop->src_dpn_prop; 199 addr = prop->source_ports; 200 for_each_set_bit(bit, &addr, 32) { 201 dpn[i].num = bit; 202 dpn[i].type = SDW_DPN_FULL; 203 dpn[i].simple_ch_prep_sm = true; 204 i++; 205 } 206 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), 211 GFP_KERNEL); 212 if (!prop->sink_dpn_prop) 213 return -ENOMEM; 214 215 j = 0; 216 dpn = prop->sink_dpn_prop; 217 addr = prop->sink_ports; 218 for_each_set_bit(bit, &addr, 32) { 219 dpn[j].num = bit; 220 dpn[j].type = SDW_DPN_FULL; 221 dpn[j].simple_ch_prep_sm = true; 222 j++; 223 } 224 225 prop->simple_clk_stop_capable = true; 226 227 /* wake-up event */ 228 prop->wake_capable = 0; 229 230 return 0; 231 } 232 233 static int sdw_mockup_bus_config(struct sdw_slave *slave, 234 struct sdw_bus_params *params) 235 { 236 return 0; 237 } 238 239 static int sdw_mockup_interrupt_callback(struct sdw_slave *slave, 240 struct sdw_slave_intr_status *status) 241 { 242 return 0; 243 } 244 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, 250 }; 251 252 static int sdw_mockup_sdw_probe(struct sdw_slave *slave, 253 const struct sdw_device_id *id) 254 { 255 struct device *dev = &slave->dev; 256 struct sdw_mockup_priv *sdw_mockup; 257 int ret; 258 259 sdw_mockup = devm_kzalloc(dev, sizeof(*sdw_mockup), GFP_KERNEL); 260 if (!sdw_mockup) 261 return -ENOMEM; 262 263 dev_set_drvdata(dev, sdw_mockup); 264 sdw_mockup->slave = slave; 265 266 slave->is_mockup_device = true; 267 268 ret = devm_snd_soc_register_component(dev, 269 &snd_soc_sdw_mockup_component, 270 sdw_mockup_dai, 271 ARRAY_SIZE(sdw_mockup_dai)); 272 273 return ret; 274 } 275 276 static int sdw_mockup_sdw_remove(struct sdw_slave *slave) 277 { 278 return 0; 279 } 280 281 /* 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 286 * IV feedback 287 * 0x5555: mic codec (mock-up of RT715) - capture-only 288 */ 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), 294 {}, 295 }; 296 MODULE_DEVICE_TABLE(sdw, sdw_mockup_id); 297 298 static struct sdw_driver sdw_mockup_sdw_driver = { 299 .driver = { 300 .name = "sdw-mockup", 301 .owner = THIS_MODULE, 302 }, 303 .probe = sdw_mockup_sdw_probe, 304 .remove = sdw_mockup_sdw_remove, 305 .ops = &sdw_mockup_slave_ops, 306 .id_table = sdw_mockup_id, 307 }; 308 module_sdw_driver(sdw_mockup_sdw_driver); 309 310 MODULE_DESCRIPTION("ASoC SDW mockup codec driver"); 311 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>"); 312 MODULE_LICENSE("GPL"); 313