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