xref: /linux/drivers/soundwire/intel_ace2x.c (revision eacaf5ae747f7dead6cc268de17a7382d79031fc)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 // Copyright(c) 2023 Intel Corporation
3 
4 /*
5  * Soundwire Intel ops for LunarLake
6  */
7 
8 #include <linux/acpi.h>
9 #include <linux/cleanup.h>
10 #include <linux/device.h>
11 #include <linux/soundwire/sdw_registers.h>
12 #include <linux/soundwire/sdw.h>
13 #include <linux/soundwire/sdw_intel.h>
14 #include <linux/string_choices.h>
15 #include <sound/hdaudio.h>
16 #include <sound/hda-mlink.h>
17 #include <sound/hda-sdw-bpt.h>
18 #include <sound/hda_register.h>
19 #include <sound/pcm_params.h>
20 #include "cadence_master.h"
21 #include "bus.h"
22 #include "intel.h"
23 
24 static int sdw_slave_bpt_stream_add(struct sdw_slave *slave, struct sdw_stream_runtime *stream)
25 {
26 	struct sdw_stream_config sconfig = {0};
27 	struct sdw_port_config pconfig = {0};
28 	int ret;
29 
30 	/* arbitrary configuration */
31 	sconfig.frame_rate = 16000;
32 	sconfig.ch_count = 1;
33 	sconfig.bps = 32; /* this is required for BPT/BRA */
34 	sconfig.direction = SDW_DATA_DIR_RX;
35 	sconfig.type = SDW_STREAM_BPT;
36 
37 	pconfig.num = 0;
38 	pconfig.ch_mask = BIT(0);
39 
40 	ret = sdw_stream_add_slave(slave, &sconfig, &pconfig, 1, stream);
41 	if (ret)
42 		dev_err(&slave->dev, "%s: failed: %d\n", __func__, ret);
43 
44 	return ret;
45 }
46 
47 #define READ_PDI1_MIN_SIZE	12
48 
49 static int intel_ace2x_bpt_open_stream(struct sdw_intel *sdw, struct sdw_slave *slave,
50 				       struct sdw_bpt_msg *msg)
51 {
52 	struct sdw_cdns *cdns = &sdw->cdns;
53 	struct sdw_bus *bus = &cdns->bus;
54 	struct sdw_master_prop *prop = &bus->prop;
55 	struct sdw_stream_runtime *stream;
56 	struct sdw_stream_config sconfig;
57 	struct sdw_port_config *pconfig;
58 	unsigned int pdi0_buf_size_pre_frame;
59 	unsigned int pdi1_buf_size_pre_frame;
60 	unsigned int pdi0_buffer_size_;
61 	unsigned int pdi1_buffer_size_;
62 	unsigned int pdi0_buffer_size;
63 	unsigned int tx_dma_bandwidth;
64 	unsigned int pdi1_buffer_size;
65 	unsigned int rx_dma_bandwidth;
66 	unsigned int fake_num_frames;
67 	unsigned int data_per_frame;
68 	unsigned int tx_total_bytes;
69 	struct sdw_cdns_pdi *pdi0;
70 	struct sdw_cdns_pdi *pdi1;
71 	unsigned int rx_alignment;
72 	unsigned int tx_alignment;
73 	unsigned int num_frames_;
74 	unsigned int num_frames;
75 	unsigned int fake_size;
76 	unsigned int tx_pad;
77 	unsigned int rx_pad;
78 	int command;
79 	int ret1;
80 	int ret;
81 	int dir;
82 	int len;
83 	int i;
84 
85 	if (cdns->bus.bpt_stream) {
86 		dev_err(cdns->dev, "%s: BPT stream already exists\n", __func__);
87 		return -EAGAIN;
88 	}
89 
90 	stream = sdw_alloc_stream("BPT", SDW_STREAM_BPT);
91 	if (!stream)
92 		return -ENOMEM;
93 
94 	cdns->bus.bpt_stream = stream;
95 
96 	ret = sdw_slave_bpt_stream_add(slave, stream);
97 	if (ret < 0)
98 		goto release_stream;
99 
100 	/* handle PDI0 first */
101 	dir = SDW_DATA_DIR_TX;
102 
103 	pdi0 = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, 1,  dir, 0);
104 	if (!pdi0) {
105 		dev_err(cdns->dev, "%s: sdw_cdns_alloc_pdi0 failed\n", __func__);
106 		ret = -EINVAL;
107 		goto remove_slave;
108 	}
109 
110 	sdw_cdns_config_stream(cdns, 1, dir, pdi0);
111 
112 	/* handle PDI1  */
113 	dir = SDW_DATA_DIR_RX;
114 
115 	pdi1 = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, 1,  dir, 1);
116 	if (!pdi1) {
117 		dev_err(cdns->dev, "%s: sdw_cdns_alloc_pdi1 failed\n", __func__);
118 		ret = -EINVAL;
119 		goto remove_slave;
120 	}
121 
122 	sdw_cdns_config_stream(cdns, 1, dir, pdi1);
123 
124 	/*
125 	 * the port config direction, number of channels and frame
126 	 * rate is totally arbitrary
127 	 */
128 	sconfig.direction = dir;
129 	sconfig.ch_count = 1;
130 	sconfig.frame_rate = 16000;
131 	sconfig.type = SDW_STREAM_BPT;
132 	sconfig.bps = 32; /* this is required for BPT/BRA */
133 
134 	/* Port configuration */
135 	pconfig = kzalloc_objs(*pconfig, 2);
136 	if (!pconfig) {
137 		ret =  -ENOMEM;
138 		goto remove_slave;
139 	}
140 
141 	for (i = 0; i < 2 /* num_pdi */; i++) {
142 		pconfig[i].num = i;
143 		pconfig[i].ch_mask = 1;
144 	}
145 
146 	ret = sdw_stream_add_master(&cdns->bus, &sconfig, pconfig, 2, stream);
147 	kfree(pconfig);
148 
149 	if (ret < 0) {
150 		dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
151 		goto remove_slave;
152 	}
153 
154 	ret = sdw_prepare_stream(cdns->bus.bpt_stream);
155 	if (ret < 0)
156 		goto remove_master;
157 
158 	command = (msg->flags & SDW_MSG_FLAG_WRITE) ? 0 : 1;
159 
160 	ret = sdw_cdns_bpt_find_bandwidth(command, cdns->bus.params.row,
161 					  cdns->bus.params.col,
162 					  prop->default_frame_rate,
163 					  &tx_dma_bandwidth, &rx_dma_bandwidth);
164 	if (ret < 0)
165 		goto deprepare_stream;
166 
167 	len = 0;
168 	pdi0_buffer_size = 0;
169 	pdi1_buffer_size = 0;
170 	num_frames = 0;
171 	/* Add up pdi buffer size and frame numbers of each BPT sections */
172 	for (i = 0; i < msg->sections; i++) {
173 		ret = sdw_cdns_bpt_find_buffer_sizes(command, cdns->bus.params.row,
174 						     cdns->bus.params.col,
175 						     msg->sec[i].len, SDW_BPT_MSG_MAX_BYTES,
176 						     &data_per_frame, &pdi0_buffer_size_,
177 						     &pdi1_buffer_size_, &num_frames_);
178 		if (ret < 0)
179 			goto deprepare_stream;
180 
181 		len += msg->sec[i].len;
182 		pdi0_buffer_size += pdi0_buffer_size_;
183 		pdi1_buffer_size += pdi1_buffer_size_;
184 		num_frames += num_frames_;
185 	}
186 
187 	sdw->bpt_ctx.pdi0_buffer_size = pdi0_buffer_size;
188 	sdw->bpt_ctx.pdi1_buffer_size = pdi1_buffer_size;
189 	sdw->bpt_ctx.num_frames = num_frames;
190 	sdw->bpt_ctx.data_per_frame = data_per_frame;
191 
192 	rx_alignment = hda_sdw_bpt_get_buf_size_alignment(rx_dma_bandwidth);
193 	tx_alignment = hda_sdw_bpt_get_buf_size_alignment(tx_dma_bandwidth);
194 
195 	if (command) { /* read */
196 		/* Get buffer size of a full frame */
197 		ret = sdw_cdns_bpt_find_buffer_sizes(command, cdns->bus.params.row,
198 						     cdns->bus.params.col,
199 						     data_per_frame, SDW_BPT_MSG_MAX_BYTES,
200 						     &data_per_frame, &pdi0_buf_size_pre_frame,
201 						     &pdi1_buf_size_pre_frame, &fake_num_frames);
202 		if (ret < 0)
203 			goto deprepare_stream;
204 
205 		/* find fake pdi1 buffer size */
206 		rx_pad = rx_alignment - (pdi1_buffer_size % rx_alignment);
207 		while (rx_pad <= READ_PDI1_MIN_SIZE)
208 			rx_pad += rx_alignment;
209 
210 		pdi1_buffer_size += rx_pad;
211 		/* It is fine if we request more than enough byte to read */
212 		fake_num_frames = DIV_ROUND_UP(rx_pad, pdi1_buf_size_pre_frame);
213 		fake_size = fake_num_frames * data_per_frame;
214 
215 		/* find fake pdi0 buffer size */
216 		pdi0_buffer_size += (fake_num_frames * pdi0_buf_size_pre_frame);
217 		tx_pad = tx_alignment - (pdi0_buffer_size % tx_alignment);
218 		pdi0_buffer_size += tx_pad;
219 	} else { /* write */
220 		/*
221 		 * For the write command, the rx data block is 4, and the rx buffer size of a frame
222 		 * is 8. So the rx buffer size (pdi0_buffer_size) is always a multiple of rx
223 		 * alignment.
224 		 */
225 		tx_pad = tx_alignment - (pdi0_buffer_size % tx_alignment);
226 		pdi0_buffer_size += tx_pad;
227 	}
228 
229 	dev_dbg(cdns->dev, "Message len %d transferred in %d frames (%d per frame)\n",
230 		len, num_frames, data_per_frame);
231 	dev_dbg(cdns->dev, "sizes pdi0 %d pdi1 %d tx_bandwidth %d rx_bandwidth %d\n",
232 		pdi0_buffer_size, pdi1_buffer_size, tx_dma_bandwidth, rx_dma_bandwidth);
233 
234 	ret = hda_sdw_bpt_open(cdns->dev->parent, /* PCI device */
235 			       sdw->instance, &sdw->bpt_ctx.bpt_tx_stream,
236 			       &sdw->bpt_ctx.dmab_tx_bdl, pdi0_buffer_size, tx_dma_bandwidth,
237 			       &sdw->bpt_ctx.bpt_rx_stream, &sdw->bpt_ctx.dmab_rx_bdl,
238 			       pdi1_buffer_size, rx_dma_bandwidth);
239 	if (ret < 0) {
240 		dev_err(cdns->dev, "%s: hda_sdw_bpt_open failed %d\n", __func__, ret);
241 		goto deprepare_stream;
242 	}
243 
244 	if (!command) {
245 		ret = sdw_cdns_prepare_write_dma_buffer(msg->dev_num, msg->sec, msg->sections,
246 							data_per_frame,
247 							sdw->bpt_ctx.dmab_tx_bdl.area,
248 							pdi0_buffer_size, &tx_total_bytes);
249 	} else {
250 		ret = sdw_cdns_prepare_read_dma_buffer(msg->dev_num, msg->sec, msg->sections,
251 						       data_per_frame,
252 						       sdw->bpt_ctx.dmab_tx_bdl.area,
253 						       pdi0_buffer_size, &tx_total_bytes,
254 						       fake_size);
255 	}
256 
257 	if (!ret)
258 		return 0;
259 
260 	dev_err(cdns->dev, "%s: sdw_prepare_%s_dma_buffer failed %d\n",
261 		__func__, str_read_write(command), ret);
262 
263 	ret1 = hda_sdw_bpt_close(cdns->dev->parent, /* PCI device */
264 				 sdw->bpt_ctx.bpt_tx_stream, &sdw->bpt_ctx.dmab_tx_bdl,
265 				 sdw->bpt_ctx.bpt_rx_stream, &sdw->bpt_ctx.dmab_rx_bdl);
266 	if (ret1 < 0)
267 		dev_err(cdns->dev, "%s:  hda_sdw_bpt_close failed: ret %d\n",
268 			__func__, ret1);
269 
270 deprepare_stream:
271 	sdw_deprepare_stream(cdns->bus.bpt_stream);
272 
273 remove_master:
274 	ret1 = sdw_stream_remove_master(&cdns->bus, cdns->bus.bpt_stream);
275 	if (ret1 < 0)
276 		dev_err(cdns->dev, "%s: remove master failed: %d\n",
277 			__func__, ret1);
278 
279 remove_slave:
280 	ret1 = sdw_stream_remove_slave(slave, cdns->bus.bpt_stream);
281 	if (ret1 < 0)
282 		dev_err(cdns->dev, "%s: remove slave failed: %d\n",
283 			__func__, ret1);
284 
285 release_stream:
286 	sdw_release_stream(cdns->bus.bpt_stream);
287 	cdns->bus.bpt_stream = NULL;
288 
289 	return ret;
290 }
291 
292 static void intel_ace2x_bpt_close_stream(struct sdw_intel *sdw, struct sdw_slave *slave,
293 					 struct sdw_bpt_msg *msg)
294 {
295 	struct sdw_cdns *cdns = &sdw->cdns;
296 	int ret;
297 
298 	ret = hda_sdw_bpt_close(cdns->dev->parent /* PCI device */, sdw->bpt_ctx.bpt_tx_stream,
299 				&sdw->bpt_ctx.dmab_tx_bdl, sdw->bpt_ctx.bpt_rx_stream,
300 				&sdw->bpt_ctx.dmab_rx_bdl);
301 	if (ret < 0)
302 		dev_err(cdns->dev, "%s:  hda_sdw_bpt_close failed: ret %d\n",
303 			__func__, ret);
304 
305 	ret = sdw_deprepare_stream(cdns->bus.bpt_stream);
306 	if (ret < 0)
307 		dev_err(cdns->dev, "%s: sdw_deprepare_stream failed: ret %d\n",
308 			__func__, ret);
309 
310 	ret = sdw_stream_remove_master(&cdns->bus, cdns->bus.bpt_stream);
311 	if (ret < 0)
312 		dev_err(cdns->dev, "%s: remove master failed: %d\n",
313 			__func__, ret);
314 
315 	ret = sdw_stream_remove_slave(slave, cdns->bus.bpt_stream);
316 	if (ret < 0)
317 		dev_err(cdns->dev, "%s: remove slave failed: %d\n",
318 			__func__, ret);
319 
320 	sdw_release_stream(cdns->bus.bpt_stream);
321 	cdns->bus.bpt_stream = NULL;
322 }
323 
324 #define INTEL_BPT_MSG_BYTE_MIN 16
325 
326 static int intel_ace2x_bpt_send_async(struct sdw_intel *sdw, struct sdw_slave *slave,
327 				      struct sdw_bpt_msg *msg)
328 {
329 	struct sdw_cdns *cdns = &sdw->cdns;
330 	int len = 0;
331 	int ret;
332 	int i;
333 
334 	for (i = 0; i < msg->sections; i++)
335 		len += msg->sec[i].len;
336 
337 	if (len < INTEL_BPT_MSG_BYTE_MIN) {
338 		dev_err(cdns->dev, "BPT message length %d is less than the minimum bytes %d\n",
339 			len, INTEL_BPT_MSG_BYTE_MIN);
340 		return -EINVAL;
341 	}
342 
343 	dev_dbg(cdns->dev, "BPT Transfer start\n");
344 
345 	ret = intel_ace2x_bpt_open_stream(sdw, slave, msg);
346 	if (ret < 0)
347 		return ret;
348 
349 	ret = hda_sdw_bpt_send_async(cdns->dev->parent, /* PCI device */
350 				     sdw->bpt_ctx.bpt_tx_stream, sdw->bpt_ctx.bpt_rx_stream);
351 	if (ret < 0) {
352 		dev_err(cdns->dev, "%s:   hda_sdw_bpt_send_async failed: %d\n",
353 			__func__, ret);
354 
355 		intel_ace2x_bpt_close_stream(sdw, slave, msg);
356 
357 		return ret;
358 	}
359 
360 	ret = sdw_enable_stream(cdns->bus.bpt_stream);
361 	if (ret < 0) {
362 		dev_err(cdns->dev, "%s: sdw_stream_enable failed: %d\n",
363 			__func__, ret);
364 		intel_ace2x_bpt_close_stream(sdw, slave, msg);
365 	}
366 
367 	return ret;
368 }
369 
370 static int intel_ace2x_bpt_wait(struct sdw_intel *sdw, struct sdw_slave *slave,
371 				struct sdw_bpt_msg *msg)
372 {
373 	struct sdw_cdns *cdns = &sdw->cdns;
374 	int ret;
375 
376 	dev_dbg(cdns->dev, "BPT Transfer wait\n");
377 
378 	ret = hda_sdw_bpt_wait(cdns->dev->parent, /* PCI device */
379 			       sdw->bpt_ctx.bpt_tx_stream, sdw->bpt_ctx.bpt_rx_stream);
380 	if (ret < 0)
381 		dev_err(cdns->dev, "%s: hda_sdw_bpt_wait failed: %d\n", __func__, ret);
382 
383 	ret = sdw_disable_stream(cdns->bus.bpt_stream);
384 	if (ret < 0) {
385 		dev_err(cdns->dev, "%s: sdw_stream_enable failed: %d\n",
386 			__func__, ret);
387 		goto err;
388 	}
389 
390 	if (msg->flags & SDW_MSG_FLAG_WRITE) {
391 		ret = sdw_cdns_check_write_response(cdns->dev, sdw->bpt_ctx.dmab_rx_bdl.area,
392 						    sdw->bpt_ctx.pdi1_buffer_size,
393 						    sdw->bpt_ctx.num_frames);
394 		if (ret < 0)
395 			dev_err(cdns->dev, "%s: BPT Write failed %d\n", __func__, ret);
396 	} else {
397 		ret = sdw_cdns_check_read_response(cdns->dev, sdw->bpt_ctx.dmab_rx_bdl.area,
398 						   sdw->bpt_ctx.pdi1_buffer_size,
399 						   msg->sec, msg->sections, sdw->bpt_ctx.num_frames,
400 						   sdw->bpt_ctx.data_per_frame);
401 		if (ret < 0)
402 			dev_err(cdns->dev, "%s: BPT Read failed %d\n", __func__, ret);
403 	}
404 
405 err:
406 	intel_ace2x_bpt_close_stream(sdw, slave, msg);
407 
408 	return ret;
409 }
410 
411 /*
412  * shim vendor-specific (vs) ops
413  */
414 
415 static void intel_shim_vs_init(struct sdw_intel *sdw)
416 {
417 	void __iomem *shim_vs = sdw->link_res->shim_vs;
418 	struct sdw_bus *bus = &sdw->cdns.bus;
419 	struct sdw_intel_prop *intel_prop;
420 	u16 clde;
421 	u16 doaise2;
422 	u16 dodse2;
423 	u16 clds;
424 	u16 clss;
425 	u16 doaise;
426 	u16 doais;
427 	u16 dodse;
428 	u16 dods;
429 	u16 act;
430 
431 	intel_prop = bus->vendor_specific_prop;
432 	clde = intel_prop->clde;
433 	doaise2 = intel_prop->doaise2;
434 	dodse2 = intel_prop->dodse2;
435 	clds = intel_prop->clds;
436 	clss = intel_prop->clss;
437 	doaise = intel_prop->doaise;
438 	doais = intel_prop->doais;
439 	dodse = intel_prop->dodse;
440 	dods = intel_prop->dods;
441 
442 	act = intel_readw(shim_vs, SDW_SHIM2_INTEL_VS_ACTMCTL);
443 	u16p_replace_bits(&act, clde, SDW_SHIM3_INTEL_VS_ACTMCTL_CLDE);
444 	u16p_replace_bits(&act, doaise2, SDW_SHIM3_INTEL_VS_ACTMCTL_DOAISE2);
445 	u16p_replace_bits(&act, dodse2, SDW_SHIM3_INTEL_VS_ACTMCTL_DODSE2);
446 	u16p_replace_bits(&act, clds, SDW_SHIM3_INTEL_VS_ACTMCTL_CLDS);
447 	u16p_replace_bits(&act, clss, SDW_SHIM3_INTEL_VS_ACTMCTL_CLSS);
448 	u16p_replace_bits(&act, doaise, SDW_SHIM2_INTEL_VS_ACTMCTL_DOAISE);
449 	u16p_replace_bits(&act, doais, SDW_SHIM2_INTEL_VS_ACTMCTL_DOAIS);
450 	u16p_replace_bits(&act, dodse, SDW_SHIM2_INTEL_VS_ACTMCTL_DODSE);
451 	u16p_replace_bits(&act, dods, SDW_SHIM2_INTEL_VS_ACTMCTL_DODS);
452 	act |= SDW_SHIM2_INTEL_VS_ACTMCTL_DACTQE;
453 	intel_writew(shim_vs, SDW_SHIM2_INTEL_VS_ACTMCTL, act);
454 	usleep_range(10, 15);
455 }
456 
457 static void intel_shim_vs_set_clock_source(struct sdw_intel *sdw, u32 source)
458 {
459 	void __iomem *shim_vs = sdw->link_res->shim_vs;
460 	u32 val;
461 
462 	val = intel_readl(shim_vs, SDW_SHIM2_INTEL_VS_LVSCTL);
463 
464 	u32p_replace_bits(&val, source, SDW_SHIM2_INTEL_VS_LVSCTL_MLCS);
465 
466 	intel_writel(shim_vs, SDW_SHIM2_INTEL_VS_LVSCTL, val);
467 
468 	dev_dbg(sdw->cdns.dev, "clock source %d LVSCTL %#x\n", source, val);
469 }
470 
471 static int intel_shim_check_wake(struct sdw_intel *sdw)
472 {
473 	/*
474 	 * We follow the HDaudio example and resume unconditionally
475 	 * without checking the WAKESTS bit for that specific link
476 	 */
477 
478 	return 1;
479 }
480 
481 static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
482 {
483 	u16 lsdiid = 0;
484 	u16 wake_en;
485 	u16 wake_sts;
486 	int ret;
487 
488 	mutex_lock(sdw->link_res->shim_lock);
489 
490 	ret = hdac_bus_eml_sdw_get_lsdiid_unlocked(sdw->link_res->hbus, sdw->instance, &lsdiid);
491 	if (ret < 0)
492 		goto unlock;
493 
494 	wake_en = snd_hdac_chip_readw(sdw->link_res->hbus, WAKEEN);
495 
496 	if (wake_enable) {
497 		/* Enable the wakeup */
498 		wake_en |= lsdiid;
499 
500 		snd_hdac_chip_writew(sdw->link_res->hbus, WAKEEN, wake_en);
501 	} else {
502 		/* Disable the wake up interrupt */
503 		wake_en &= ~lsdiid;
504 		snd_hdac_chip_writew(sdw->link_res->hbus, WAKEEN, wake_en);
505 
506 		/* Clear wake status (W1C) */
507 		wake_sts = snd_hdac_chip_readw(sdw->link_res->hbus, STATESTS);
508 		wake_sts |= lsdiid;
509 		snd_hdac_chip_writew(sdw->link_res->hbus, STATESTS, wake_sts);
510 	}
511 unlock:
512 	mutex_unlock(sdw->link_res->shim_lock);
513 }
514 
515 static int intel_link_power_up(struct sdw_intel *sdw)
516 {
517 	struct sdw_bus *bus = &sdw->cdns.bus;
518 	struct sdw_master_prop *prop = &bus->prop;
519 	u32 *shim_mask = sdw->link_res->shim_mask;
520 	unsigned int link_id = sdw->instance;
521 	u32 clock_source;
522 	u32 syncprd;
523 	int ret;
524 
525 	if (prop->mclk_freq % 6000000) {
526 		if (prop->mclk_freq % 2400000) {
527 			syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24_576;
528 			clock_source = SDW_SHIM2_MLCS_CARDINAL_CLK;
529 		} else {
530 			syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_38_4;
531 			clock_source = SDW_SHIM2_MLCS_XTAL_CLK;
532 		}
533 	} else {
534 		syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_96;
535 		clock_source = SDW_SHIM2_MLCS_AUDIO_PLL_CLK;
536 	}
537 
538 	mutex_lock(sdw->link_res->shim_lock);
539 
540 	ret = hdac_bus_eml_sdw_power_up_unlocked(sdw->link_res->hbus, link_id);
541 	if (ret < 0) {
542 		dev_err(sdw->cdns.dev, "%s: hdac_bus_eml_sdw_power_up failed: %d\n",
543 			__func__, ret);
544 		goto out;
545 	}
546 
547 	intel_shim_vs_set_clock_source(sdw, clock_source);
548 
549 	if (!*shim_mask) {
550 		/* we first need to program the SyncPRD/CPU registers */
551 		dev_dbg(sdw->cdns.dev, "first link up, programming SYNCPRD\n");
552 
553 		ret =  hdac_bus_eml_sdw_set_syncprd_unlocked(sdw->link_res->hbus, syncprd);
554 		if (ret < 0) {
555 			dev_err(sdw->cdns.dev, "%s: hdac_bus_eml_sdw_set_syncprd failed: %d\n",
556 				__func__, ret);
557 			goto out;
558 		}
559 
560 		/* SYNCPU will change once link is active */
561 		ret =  hdac_bus_eml_sdw_wait_syncpu_unlocked(sdw->link_res->hbus);
562 		if (ret < 0) {
563 			dev_err(sdw->cdns.dev, "%s: hdac_bus_eml_sdw_wait_syncpu failed: %d\n",
564 				__func__, ret);
565 			goto out;
566 		}
567 
568 		hdac_bus_eml_enable_interrupt_unlocked(sdw->link_res->hbus, true,
569 						       AZX_REG_ML_LEPTR_ID_SDW, true);
570 	}
571 
572 	*shim_mask |= BIT(link_id);
573 
574 	sdw->cdns.link_up = true;
575 
576 	intel_shim_vs_init(sdw);
577 
578 out:
579 	mutex_unlock(sdw->link_res->shim_lock);
580 
581 	return ret;
582 }
583 
584 static int intel_link_power_down(struct sdw_intel *sdw)
585 {
586 	u32 *shim_mask = sdw->link_res->shim_mask;
587 	unsigned int link_id = sdw->instance;
588 	int ret;
589 
590 	mutex_lock(sdw->link_res->shim_lock);
591 
592 	sdw->cdns.link_up = false;
593 
594 	*shim_mask &= ~BIT(link_id);
595 
596 	if (!*shim_mask)
597 		hdac_bus_eml_enable_interrupt_unlocked(sdw->link_res->hbus, true,
598 						       AZX_REG_ML_LEPTR_ID_SDW, false);
599 
600 	ret = hdac_bus_eml_sdw_power_down_unlocked(sdw->link_res->hbus, link_id);
601 	if (ret < 0) {
602 		dev_err(sdw->cdns.dev, "%s: hdac_bus_eml_sdw_power_down failed: %d\n",
603 			__func__, ret);
604 
605 		/*
606 		 * we leave the sdw->cdns.link_up flag as false since we've disabled
607 		 * the link at this point and cannot handle interrupts any longer.
608 		 */
609 	}
610 
611 	mutex_unlock(sdw->link_res->shim_lock);
612 
613 	return ret;
614 }
615 
616 static void intel_sync_arm(struct sdw_intel *sdw)
617 {
618 	unsigned int link_id = sdw->instance;
619 
620 	mutex_lock(sdw->link_res->shim_lock);
621 
622 	hdac_bus_eml_sdw_sync_arm_unlocked(sdw->link_res->hbus, link_id);
623 
624 	mutex_unlock(sdw->link_res->shim_lock);
625 }
626 
627 static int intel_sync_go_unlocked(struct sdw_intel *sdw)
628 {
629 	int ret;
630 
631 	ret = hdac_bus_eml_sdw_sync_go_unlocked(sdw->link_res->hbus);
632 	if (ret < 0)
633 		dev_err(sdw->cdns.dev, "%s: SyncGO clear failed: %d\n", __func__, ret);
634 
635 	return ret;
636 }
637 
638 static int intel_sync_go(struct sdw_intel *sdw)
639 {
640 	int ret;
641 
642 	mutex_lock(sdw->link_res->shim_lock);
643 
644 	ret = intel_sync_go_unlocked(sdw);
645 
646 	mutex_unlock(sdw->link_res->shim_lock);
647 
648 	return ret;
649 }
650 
651 static bool intel_check_cmdsync_unlocked(struct sdw_intel *sdw)
652 {
653 	return hdac_bus_eml_sdw_check_cmdsync_unlocked(sdw->link_res->hbus);
654 }
655 
656 /* DAI callbacks */
657 static int intel_params_stream(struct sdw_intel *sdw,
658 			       struct snd_pcm_substream *substream,
659 			       struct snd_soc_dai *dai,
660 			       struct snd_pcm_hw_params *hw_params,
661 			       int link_id, int alh_stream_id)
662 {
663 	struct sdw_intel_link_res *res = sdw->link_res;
664 	struct sdw_intel_stream_params_data params_data;
665 
666 	params_data.substream = substream;
667 	params_data.dai = dai;
668 	params_data.hw_params = hw_params;
669 	params_data.link_id = link_id;
670 	params_data.alh_stream_id = alh_stream_id;
671 
672 	if (res->ops && res->ops->params_stream && res->dev)
673 		return res->ops->params_stream(res->dev,
674 					       &params_data);
675 	return -EIO;
676 }
677 
678 static int intel_free_stream(struct sdw_intel *sdw,
679 			     struct snd_pcm_substream *substream,
680 			     struct snd_soc_dai *dai,
681 			     int link_id)
682 
683 {
684 	struct sdw_intel_link_res *res = sdw->link_res;
685 	struct sdw_intel_stream_free_data free_data;
686 
687 	free_data.substream = substream;
688 	free_data.dai = dai;
689 	free_data.link_id = link_id;
690 
691 	if (res->ops && res->ops->free_stream && res->dev)
692 		return res->ops->free_stream(res->dev,
693 					     &free_data);
694 
695 	return 0;
696 }
697 
698 /*
699  * DAI operations
700  */
701 static int intel_hw_params(struct snd_pcm_substream *substream,
702 			   struct snd_pcm_hw_params *params,
703 			   struct snd_soc_dai *dai)
704 {
705 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
706 	struct sdw_intel *sdw = cdns_to_intel(cdns);
707 	struct sdw_cdns_dai_runtime *dai_runtime;
708 	struct sdw_cdns_pdi *pdi;
709 	struct sdw_stream_config sconfig;
710 	int ch, dir;
711 	int ret;
712 
713 	dai_runtime = cdns->dai_runtime_array[dai->id];
714 	if (!dai_runtime)
715 		return -EIO;
716 
717 	ch = params_channels(params);
718 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
719 		dir = SDW_DATA_DIR_RX;
720 	else
721 		dir = SDW_DATA_DIR_TX;
722 
723 	pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
724 	if (!pdi)
725 		return -EINVAL;
726 
727 	/* use same definitions for alh_id as previous generations */
728 	pdi->intel_alh_id = (sdw->instance * 16) + pdi->num + 3;
729 	if (pdi->num >= 2)
730 		pdi->intel_alh_id += 2;
731 
732 	/* the SHIM will be configured in the callback functions */
733 
734 	sdw_cdns_config_stream(cdns, ch, dir, pdi);
735 
736 	/* store pdi and state, may be needed in prepare step */
737 	dai_runtime->paused = false;
738 	dai_runtime->suspended = false;
739 	dai_runtime->pdi = pdi;
740 
741 	/* Inform DSP about PDI stream number */
742 	ret = intel_params_stream(sdw, substream, dai, params,
743 				  sdw->instance,
744 				  pdi->intel_alh_id);
745 	if (ret)
746 		return ret;
747 
748 	sconfig.direction = dir;
749 	sconfig.ch_count = ch;
750 	sconfig.frame_rate = params_rate(params);
751 	sconfig.type = dai_runtime->stream_type;
752 
753 	sconfig.bps = snd_pcm_format_width(params_format(params));
754 
755 	/* Port configuration */
756 	struct sdw_port_config *pconfig __free(kfree) = kzalloc_obj(*pconfig);
757 	if (!pconfig)
758 		return -ENOMEM;
759 
760 	pconfig->num = pdi->num;
761 	pconfig->ch_mask = (1 << ch) - 1;
762 
763 	ret = sdw_stream_add_master(&cdns->bus, &sconfig,
764 				    pconfig, 1, dai_runtime->stream);
765 	if (ret)
766 		dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
767 
768 	return ret;
769 }
770 
771 static int intel_prepare(struct snd_pcm_substream *substream,
772 			 struct snd_soc_dai *dai)
773 {
774 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
775 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
776 	struct sdw_intel *sdw = cdns_to_intel(cdns);
777 	struct sdw_cdns_dai_runtime *dai_runtime;
778 	struct snd_pcm_hw_params *hw_params;
779 	int ch, dir;
780 
781 	dai_runtime = cdns->dai_runtime_array[dai->id];
782 	if (!dai_runtime) {
783 		dev_err(dai->dev, "failed to get dai runtime in %s\n",
784 			__func__);
785 		return -EIO;
786 	}
787 
788 	hw_params = &rtd->dpcm[substream->stream].hw_params;
789 	if (dai_runtime->suspended) {
790 		dai_runtime->suspended = false;
791 
792 		/*
793 		 * .prepare() is called after system resume, where we
794 		 * need to reinitialize the SHIM/ALH/Cadence IP.
795 		 * .prepare() is also called to deal with underflows,
796 		 * but in those cases we cannot touch ALH/SHIM
797 		 * registers
798 		 */
799 
800 		/* configure stream */
801 		ch = params_channels(hw_params);
802 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
803 			dir = SDW_DATA_DIR_RX;
804 		else
805 			dir = SDW_DATA_DIR_TX;
806 
807 		/* the SHIM will be configured in the callback functions */
808 
809 		sdw_cdns_config_stream(cdns, ch, dir, dai_runtime->pdi);
810 	}
811 
812 	/* Inform DSP about PDI stream number */
813 	return intel_params_stream(sdw, substream, dai, hw_params, sdw->instance,
814 				   dai_runtime->pdi->intel_alh_id);
815 }
816 
817 static int
818 intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
819 {
820 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
821 	struct sdw_intel *sdw = cdns_to_intel(cdns);
822 	struct sdw_cdns_dai_runtime *dai_runtime;
823 	int ret;
824 
825 	dai_runtime = cdns->dai_runtime_array[dai->id];
826 	if (!dai_runtime)
827 		return -EIO;
828 
829 	/*
830 	 * The sdw stream state will transition to RELEASED when stream->
831 	 * master_list is empty. So the stream state will transition to
832 	 * DEPREPARED for the first cpu-dai and to RELEASED for the last
833 	 * cpu-dai.
834 	 */
835 	ret = sdw_stream_remove_master(&cdns->bus, dai_runtime->stream);
836 	if (ret < 0) {
837 		dev_err(dai->dev, "remove master from stream %s failed: %d\n",
838 			dai_runtime->stream->name, ret);
839 		return ret;
840 	}
841 
842 	ret = intel_free_stream(sdw, substream, dai, sdw->instance);
843 	if (ret < 0) {
844 		dev_err(dai->dev, "intel_free_stream: failed %d\n", ret);
845 		return ret;
846 	}
847 
848 	dai_runtime->pdi = NULL;
849 
850 	return 0;
851 }
852 
853 static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
854 				    void *stream, int direction)
855 {
856 	return cdns_set_sdw_stream(dai, stream, direction);
857 }
858 
859 static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
860 				  int direction)
861 {
862 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
863 	struct sdw_cdns_dai_runtime *dai_runtime;
864 
865 	dai_runtime = cdns->dai_runtime_array[dai->id];
866 	if (!dai_runtime)
867 		return ERR_PTR(-EINVAL);
868 
869 	return dai_runtime->stream;
870 }
871 
872 static int intel_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
873 {
874 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
875 	struct sdw_intel *sdw = cdns_to_intel(cdns);
876 	struct sdw_intel_link_res *res = sdw->link_res;
877 	struct sdw_cdns_dai_runtime *dai_runtime;
878 	int ret = 0;
879 
880 	/*
881 	 * The .trigger callback is used to program HDaudio DMA and send required IPC to audio
882 	 * firmware.
883 	 */
884 	if (res->ops && res->ops->trigger) {
885 		ret = res->ops->trigger(substream, cmd, dai);
886 		if (ret < 0)
887 			return ret;
888 	}
889 
890 	dai_runtime = cdns->dai_runtime_array[dai->id];
891 	if (!dai_runtime) {
892 		dev_err(dai->dev, "failed to get dai runtime in %s\n",
893 			__func__);
894 		return -EIO;
895 	}
896 
897 	switch (cmd) {
898 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
899 		dai_runtime->paused = true;
900 		break;
901 	case SNDRV_PCM_TRIGGER_STOP:
902 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
903 		dai_runtime->paused = false;
904 		break;
905 	default:
906 		break;
907 	}
908 
909 	return ret;
910 }
911 
912 static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
913 	.hw_params = intel_hw_params,
914 	.prepare = intel_prepare,
915 	.hw_free = intel_hw_free,
916 	.trigger = intel_trigger,
917 	.set_stream = intel_pcm_set_sdw_stream,
918 	.get_stream = intel_get_sdw_stream,
919 };
920 
921 static int intel_component_dais_suspend(struct snd_soc_component *component)
922 {
923 	struct snd_soc_dai *dai;
924 
925 	/*
926 	 * Mark all open streams as suspended.
927 	 * Open streams at this point can be in SUSPENDED, PAUSED or STOPPED
928 	 * state and during prepare the DMAs and SHIM registers need to be
929 	 * initialized for them.
930 	 * The STOPPED state is a special corner case which can happen if audio
931 	 * experiences xrun at suspend time.
932 	 */
933 	for_each_component_dais(component, dai) {
934 		struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
935 		struct sdw_cdns_dai_runtime *dai_runtime;
936 
937 		dai_runtime = cdns->dai_runtime_array[dai->id];
938 
939 		if (dai_runtime)
940 			dai_runtime->suspended = true;
941 	}
942 
943 	return 0;
944 }
945 
946 static const struct snd_soc_component_driver dai_component = {
947 	.name			= "soundwire",
948 	.suspend		= intel_component_dais_suspend,
949 };
950 
951 /*
952  * PDI routines
953  */
954 static void intel_pdi_init(struct sdw_intel *sdw,
955 			   struct sdw_cdns_stream_config *config)
956 {
957 	void __iomem *shim = sdw->link_res->shim;
958 	int pcm_cap;
959 
960 	/* PCM Stream Capability */
961 	pcm_cap = intel_readw(shim, SDW_SHIM2_PCMSCAP);
962 
963 	config->pcm_bd = FIELD_GET(SDW_SHIM2_PCMSCAP_BSS, pcm_cap);
964 	config->pcm_in = FIELD_GET(SDW_SHIM2_PCMSCAP_ISS, pcm_cap);
965 	config->pcm_out = FIELD_GET(SDW_SHIM2_PCMSCAP_ISS, pcm_cap);
966 
967 	dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
968 		config->pcm_bd, config->pcm_in, config->pcm_out);
969 }
970 
971 static int
972 intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num)
973 {
974 	void __iomem *shim = sdw->link_res->shim;
975 
976 	/* zero based values for channel count in register */
977 	return intel_readw(shim, SDW_SHIM2_PCMSYCHC(pdi_num)) + 1;
978 }
979 
980 static void intel_pdi_get_ch_update(struct sdw_intel *sdw,
981 				    struct sdw_cdns_pdi *pdi,
982 				    unsigned int num_pdi,
983 				    unsigned int *num_ch)
984 {
985 	int ch_count = 0;
986 	int i;
987 
988 	for (i = 0; i < num_pdi; i++) {
989 		pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num);
990 		ch_count += pdi->ch_count;
991 		pdi++;
992 	}
993 
994 	*num_ch = ch_count;
995 }
996 
997 static void intel_pdi_stream_ch_update(struct sdw_intel *sdw,
998 				       struct sdw_cdns_streams *stream)
999 {
1000 	intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
1001 				&stream->num_ch_bd);
1002 
1003 	intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
1004 				&stream->num_ch_in);
1005 
1006 	intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
1007 				&stream->num_ch_out);
1008 }
1009 
1010 static int intel_create_dai(struct sdw_cdns *cdns,
1011 			    struct snd_soc_dai_driver *dais,
1012 			    enum intel_pdi_type type,
1013 			    u32 num, u32 off, u32 max_ch)
1014 {
1015 	int i;
1016 
1017 	if (!num)
1018 		return 0;
1019 
1020 	for (i = off; i < (off + num); i++) {
1021 		dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
1022 					      "SDW%d Pin%d",
1023 					      cdns->instance, i);
1024 		if (!dais[i].name)
1025 			return -ENOMEM;
1026 
1027 		if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
1028 			dais[i].playback.channels_min = 1;
1029 			dais[i].playback.channels_max = max_ch;
1030 		}
1031 
1032 		if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
1033 			dais[i].capture.channels_min = 1;
1034 			dais[i].capture.channels_max = max_ch;
1035 		}
1036 
1037 		dais[i].ops = &intel_pcm_dai_ops;
1038 	}
1039 
1040 	return 0;
1041 }
1042 
1043 static int intel_register_dai(struct sdw_intel *sdw)
1044 {
1045 	struct sdw_cdns_dai_runtime **dai_runtime_array;
1046 	struct sdw_cdns_stream_config config;
1047 	struct sdw_cdns *cdns = &sdw->cdns;
1048 	struct sdw_cdns_streams *stream;
1049 	struct snd_soc_dai_driver *dais;
1050 	int num_dai;
1051 	int ret;
1052 	int off = 0;
1053 
1054 	/* Read the PDI config and initialize cadence PDI */
1055 	intel_pdi_init(sdw, &config);
1056 	ret = sdw_cdns_pdi_init(cdns, config);
1057 	if (ret)
1058 		return ret;
1059 
1060 	intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm);
1061 
1062 	/* DAIs are created based on total number of PDIs supported */
1063 	num_dai = cdns->pcm.num_pdi;
1064 
1065 	dai_runtime_array = devm_kcalloc(cdns->dev, num_dai,
1066 					 sizeof(struct sdw_cdns_dai_runtime *),
1067 					 GFP_KERNEL);
1068 	if (!dai_runtime_array)
1069 		return -ENOMEM;
1070 	cdns->dai_runtime_array = dai_runtime_array;
1071 
1072 	dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
1073 	if (!dais)
1074 		return -ENOMEM;
1075 
1076 	/* Create PCM DAIs */
1077 	stream = &cdns->pcm;
1078 
1079 	ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
1080 			       off, stream->num_ch_in);
1081 	if (ret)
1082 		return ret;
1083 
1084 	off += cdns->pcm.num_in;
1085 	ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
1086 			       off, stream->num_ch_out);
1087 	if (ret)
1088 		return ret;
1089 
1090 	off += cdns->pcm.num_out;
1091 	ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
1092 			       off, stream->num_ch_bd);
1093 	if (ret)
1094 		return ret;
1095 
1096 	return devm_snd_soc_register_component(cdns->dev, &dai_component,
1097 					       dais, num_dai);
1098 }
1099 
1100 static void intel_program_sdi(struct sdw_intel *sdw, int dev_num)
1101 {
1102 	int ret;
1103 
1104 	ret = hdac_bus_eml_sdw_set_lsdiid(sdw->link_res->hbus, sdw->instance, dev_num);
1105 	if (ret < 0)
1106 		dev_err(sdw->cdns.dev, "%s: could not set lsdiid for link %d %d\n",
1107 			__func__, sdw->instance, dev_num);
1108 }
1109 
1110 static int intel_get_link_count(struct sdw_intel *sdw)
1111 {
1112 	int ret;
1113 
1114 	ret = hdac_bus_eml_get_count(sdw->link_res->hbus, true, AZX_REG_ML_LEPTR_ID_SDW);
1115 	if (!ret) {
1116 		dev_err(sdw->cdns.dev, "%s: could not retrieve link count\n", __func__);
1117 		return -ENODEV;
1118 	}
1119 
1120 	if (ret > SDW_INTEL_MAX_LINKS) {
1121 		dev_err(sdw->cdns.dev, "%s: link count %d exceed max %d\n", __func__, ret, SDW_INTEL_MAX_LINKS);
1122 		return -EINVAL;
1123 	}
1124 
1125 	return ret;
1126 }
1127 
1128 const struct sdw_intel_hw_ops sdw_intel_lnl_hw_ops = {
1129 	.debugfs_init = intel_ace2x_debugfs_init,
1130 	.debugfs_exit = intel_ace2x_debugfs_exit,
1131 
1132 	.get_link_count = intel_get_link_count,
1133 
1134 	.register_dai = intel_register_dai,
1135 
1136 	.check_clock_stop = intel_check_clock_stop,
1137 	.start_bus = intel_start_bus,
1138 	.start_bus_after_reset = intel_start_bus_after_reset,
1139 	.start_bus_after_clock_stop = intel_start_bus_after_clock_stop,
1140 	.stop_bus = intel_stop_bus,
1141 
1142 	.link_power_up = intel_link_power_up,
1143 	.link_power_down = intel_link_power_down,
1144 
1145 	.shim_check_wake = intel_shim_check_wake,
1146 	.shim_wake = intel_shim_wake,
1147 
1148 	.pre_bank_switch = intel_pre_bank_switch,
1149 	.post_bank_switch = intel_post_bank_switch,
1150 
1151 	.sync_arm = intel_sync_arm,
1152 	.sync_go_unlocked = intel_sync_go_unlocked,
1153 	.sync_go = intel_sync_go,
1154 	.sync_check_cmdsync_unlocked = intel_check_cmdsync_unlocked,
1155 
1156 	.program_sdi = intel_program_sdi,
1157 
1158 	.bpt_send_async = intel_ace2x_bpt_send_async,
1159 	.bpt_wait = intel_ace2x_bpt_wait,
1160 };
1161 EXPORT_SYMBOL_NS(sdw_intel_lnl_hw_ops, "SOUNDWIRE_INTEL");
1162 
1163 MODULE_IMPORT_NS("SND_SOC_SOF_HDA_MLINK");
1164 MODULE_IMPORT_NS("SND_SOC_SOF_INTEL_HDA_SDW_BPT");
1165