xref: /linux/drivers/soundwire/intel_ace2x.c (revision d26aed5eba16bf5a4aa86bc717edf0b5ed192b93)
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 <sound/hdaudio.h>
15 #include <sound/hda-mlink.h>
16 #include <sound/hda_register.h>
17 #include <sound/pcm_params.h>
18 #include "cadence_master.h"
19 #include "bus.h"
20 #include "intel.h"
21 
22 /*
23  * shim vendor-specific (vs) ops
24  */
25 
26 static void intel_shim_vs_init(struct sdw_intel *sdw)
27 {
28 	void __iomem *shim_vs = sdw->link_res->shim_vs;
29 	struct sdw_bus *bus = &sdw->cdns.bus;
30 	struct sdw_intel_prop *intel_prop;
31 	u16 clde;
32 	u16 doaise2;
33 	u16 dodse2;
34 	u16 clds;
35 	u16 clss;
36 	u16 doaise;
37 	u16 doais;
38 	u16 dodse;
39 	u16 dods;
40 	u16 act;
41 
42 	intel_prop = bus->vendor_specific_prop;
43 	clde = intel_prop->clde;
44 	doaise2 = intel_prop->doaise2;
45 	dodse2 = intel_prop->dodse2;
46 	clds = intel_prop->clds;
47 	clss = intel_prop->clss;
48 	doaise = intel_prop->doaise;
49 	doais = intel_prop->doais;
50 	dodse = intel_prop->dodse;
51 	dods = intel_prop->dods;
52 
53 	act = intel_readw(shim_vs, SDW_SHIM2_INTEL_VS_ACTMCTL);
54 	u16p_replace_bits(&act, clde, SDW_SHIM3_INTEL_VS_ACTMCTL_CLDE);
55 	u16p_replace_bits(&act, doaise2, SDW_SHIM3_INTEL_VS_ACTMCTL_DOAISE2);
56 	u16p_replace_bits(&act, dodse2, SDW_SHIM3_INTEL_VS_ACTMCTL_DODSE2);
57 	u16p_replace_bits(&act, clds, SDW_SHIM3_INTEL_VS_ACTMCTL_CLDS);
58 	u16p_replace_bits(&act, clss, SDW_SHIM3_INTEL_VS_ACTMCTL_CLSS);
59 	u16p_replace_bits(&act, doaise, SDW_SHIM2_INTEL_VS_ACTMCTL_DOAISE);
60 	u16p_replace_bits(&act, doais, SDW_SHIM2_INTEL_VS_ACTMCTL_DOAIS);
61 	u16p_replace_bits(&act, dodse, SDW_SHIM2_INTEL_VS_ACTMCTL_DODSE);
62 	u16p_replace_bits(&act, dods, SDW_SHIM2_INTEL_VS_ACTMCTL_DODS);
63 	act |= SDW_SHIM2_INTEL_VS_ACTMCTL_DACTQE;
64 	intel_writew(shim_vs, SDW_SHIM2_INTEL_VS_ACTMCTL, act);
65 	usleep_range(10, 15);
66 }
67 
68 static void intel_shim_vs_set_clock_source(struct sdw_intel *sdw, u32 source)
69 {
70 	void __iomem *shim_vs = sdw->link_res->shim_vs;
71 	u32 val;
72 
73 	val = intel_readl(shim_vs, SDW_SHIM2_INTEL_VS_LVSCTL);
74 
75 	u32p_replace_bits(&val, source, SDW_SHIM2_INTEL_VS_LVSCTL_MLCS);
76 
77 	intel_writel(shim_vs, SDW_SHIM2_INTEL_VS_LVSCTL, val);
78 
79 	dev_dbg(sdw->cdns.dev, "clock source %d LVSCTL %#x\n", source, val);
80 }
81 
82 static int intel_shim_check_wake(struct sdw_intel *sdw)
83 {
84 	/*
85 	 * We follow the HDaudio example and resume unconditionally
86 	 * without checking the WAKESTS bit for that specific link
87 	 */
88 
89 	return 1;
90 }
91 
92 static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
93 {
94 	u16 lsdiid = 0;
95 	u16 wake_en;
96 	u16 wake_sts;
97 	int ret;
98 
99 	mutex_lock(sdw->link_res->shim_lock);
100 
101 	ret = hdac_bus_eml_sdw_get_lsdiid_unlocked(sdw->link_res->hbus, sdw->instance, &lsdiid);
102 	if (ret < 0)
103 		goto unlock;
104 
105 	wake_en = snd_hdac_chip_readw(sdw->link_res->hbus, WAKEEN);
106 
107 	if (wake_enable) {
108 		/* Enable the wakeup */
109 		wake_en |= lsdiid;
110 
111 		snd_hdac_chip_writew(sdw->link_res->hbus, WAKEEN, wake_en);
112 	} else {
113 		/* Disable the wake up interrupt */
114 		wake_en &= ~lsdiid;
115 		snd_hdac_chip_writew(sdw->link_res->hbus, WAKEEN, wake_en);
116 
117 		/* Clear wake status (W1C) */
118 		wake_sts = snd_hdac_chip_readw(sdw->link_res->hbus, STATESTS);
119 		wake_sts |= lsdiid;
120 		snd_hdac_chip_writew(sdw->link_res->hbus, STATESTS, wake_sts);
121 	}
122 unlock:
123 	mutex_unlock(sdw->link_res->shim_lock);
124 }
125 
126 static int intel_link_power_up(struct sdw_intel *sdw)
127 {
128 	struct sdw_bus *bus = &sdw->cdns.bus;
129 	struct sdw_master_prop *prop = &bus->prop;
130 	u32 *shim_mask = sdw->link_res->shim_mask;
131 	unsigned int link_id = sdw->instance;
132 	u32 clock_source;
133 	u32 syncprd;
134 	int ret;
135 
136 	if (prop->mclk_freq % 6000000) {
137 		if (prop->mclk_freq % 2400000) {
138 			syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24_576;
139 			clock_source = SDW_SHIM2_MLCS_CARDINAL_CLK;
140 		} else {
141 			syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_38_4;
142 			clock_source = SDW_SHIM2_MLCS_XTAL_CLK;
143 		}
144 	} else {
145 		syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_96;
146 		clock_source = SDW_SHIM2_MLCS_AUDIO_PLL_CLK;
147 	}
148 
149 	mutex_lock(sdw->link_res->shim_lock);
150 
151 	ret = hdac_bus_eml_sdw_power_up_unlocked(sdw->link_res->hbus, link_id);
152 	if (ret < 0) {
153 		dev_err(sdw->cdns.dev, "%s: hdac_bus_eml_sdw_power_up failed: %d\n",
154 			__func__, ret);
155 		goto out;
156 	}
157 
158 	intel_shim_vs_set_clock_source(sdw, clock_source);
159 
160 	if (!*shim_mask) {
161 		/* we first need to program the SyncPRD/CPU registers */
162 		dev_dbg(sdw->cdns.dev, "first link up, programming SYNCPRD\n");
163 
164 		ret =  hdac_bus_eml_sdw_set_syncprd_unlocked(sdw->link_res->hbus, syncprd);
165 		if (ret < 0) {
166 			dev_err(sdw->cdns.dev, "%s: hdac_bus_eml_sdw_set_syncprd failed: %d\n",
167 				__func__, ret);
168 			goto out;
169 		}
170 
171 		/* SYNCPU will change once link is active */
172 		ret =  hdac_bus_eml_sdw_wait_syncpu_unlocked(sdw->link_res->hbus);
173 		if (ret < 0) {
174 			dev_err(sdw->cdns.dev, "%s: hdac_bus_eml_sdw_wait_syncpu failed: %d\n",
175 				__func__, ret);
176 			goto out;
177 		}
178 
179 		hdac_bus_eml_enable_interrupt_unlocked(sdw->link_res->hbus, true,
180 						       AZX_REG_ML_LEPTR_ID_SDW, true);
181 	}
182 
183 	*shim_mask |= BIT(link_id);
184 
185 	sdw->cdns.link_up = true;
186 
187 	intel_shim_vs_init(sdw);
188 
189 out:
190 	mutex_unlock(sdw->link_res->shim_lock);
191 
192 	return ret;
193 }
194 
195 static int intel_link_power_down(struct sdw_intel *sdw)
196 {
197 	u32 *shim_mask = sdw->link_res->shim_mask;
198 	unsigned int link_id = sdw->instance;
199 	int ret;
200 
201 	mutex_lock(sdw->link_res->shim_lock);
202 
203 	sdw->cdns.link_up = false;
204 
205 	*shim_mask &= ~BIT(link_id);
206 
207 	if (!*shim_mask)
208 		hdac_bus_eml_enable_interrupt_unlocked(sdw->link_res->hbus, true,
209 						       AZX_REG_ML_LEPTR_ID_SDW, false);
210 
211 	ret = hdac_bus_eml_sdw_power_down_unlocked(sdw->link_res->hbus, link_id);
212 	if (ret < 0) {
213 		dev_err(sdw->cdns.dev, "%s: hdac_bus_eml_sdw_power_down failed: %d\n",
214 			__func__, ret);
215 
216 		/*
217 		 * we leave the sdw->cdns.link_up flag as false since we've disabled
218 		 * the link at this point and cannot handle interrupts any longer.
219 		 */
220 	}
221 
222 	mutex_unlock(sdw->link_res->shim_lock);
223 
224 	return ret;
225 }
226 
227 static void intel_sync_arm(struct sdw_intel *sdw)
228 {
229 	unsigned int link_id = sdw->instance;
230 
231 	mutex_lock(sdw->link_res->shim_lock);
232 
233 	hdac_bus_eml_sdw_sync_arm_unlocked(sdw->link_res->hbus, link_id);
234 
235 	mutex_unlock(sdw->link_res->shim_lock);
236 }
237 
238 static int intel_sync_go_unlocked(struct sdw_intel *sdw)
239 {
240 	int ret;
241 
242 	ret = hdac_bus_eml_sdw_sync_go_unlocked(sdw->link_res->hbus);
243 	if (ret < 0)
244 		dev_err(sdw->cdns.dev, "%s: SyncGO clear failed: %d\n", __func__, ret);
245 
246 	return ret;
247 }
248 
249 static int intel_sync_go(struct sdw_intel *sdw)
250 {
251 	int ret;
252 
253 	mutex_lock(sdw->link_res->shim_lock);
254 
255 	ret = intel_sync_go_unlocked(sdw);
256 
257 	mutex_unlock(sdw->link_res->shim_lock);
258 
259 	return ret;
260 }
261 
262 static bool intel_check_cmdsync_unlocked(struct sdw_intel *sdw)
263 {
264 	return hdac_bus_eml_sdw_check_cmdsync_unlocked(sdw->link_res->hbus);
265 }
266 
267 /* DAI callbacks */
268 static int intel_params_stream(struct sdw_intel *sdw,
269 			       struct snd_pcm_substream *substream,
270 			       struct snd_soc_dai *dai,
271 			       struct snd_pcm_hw_params *hw_params,
272 			       int link_id, int alh_stream_id)
273 {
274 	struct sdw_intel_link_res *res = sdw->link_res;
275 	struct sdw_intel_stream_params_data params_data;
276 
277 	params_data.substream = substream;
278 	params_data.dai = dai;
279 	params_data.hw_params = hw_params;
280 	params_data.link_id = link_id;
281 	params_data.alh_stream_id = alh_stream_id;
282 
283 	if (res->ops && res->ops->params_stream && res->dev)
284 		return res->ops->params_stream(res->dev,
285 					       &params_data);
286 	return -EIO;
287 }
288 
289 static int intel_free_stream(struct sdw_intel *sdw,
290 			     struct snd_pcm_substream *substream,
291 			     struct snd_soc_dai *dai,
292 			     int link_id)
293 
294 {
295 	struct sdw_intel_link_res *res = sdw->link_res;
296 	struct sdw_intel_stream_free_data free_data;
297 
298 	free_data.substream = substream;
299 	free_data.dai = dai;
300 	free_data.link_id = link_id;
301 
302 	if (res->ops && res->ops->free_stream && res->dev)
303 		return res->ops->free_stream(res->dev,
304 					     &free_data);
305 
306 	return 0;
307 }
308 
309 /*
310  * DAI operations
311  */
312 static int intel_hw_params(struct snd_pcm_substream *substream,
313 			   struct snd_pcm_hw_params *params,
314 			   struct snd_soc_dai *dai)
315 {
316 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
317 	struct sdw_intel *sdw = cdns_to_intel(cdns);
318 	struct sdw_cdns_dai_runtime *dai_runtime;
319 	struct sdw_cdns_pdi *pdi;
320 	struct sdw_stream_config sconfig;
321 	int ch, dir;
322 	int ret;
323 
324 	dai_runtime = cdns->dai_runtime_array[dai->id];
325 	if (!dai_runtime)
326 		return -EIO;
327 
328 	ch = params_channels(params);
329 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
330 		dir = SDW_DATA_DIR_RX;
331 	else
332 		dir = SDW_DATA_DIR_TX;
333 
334 	pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
335 	if (!pdi)
336 		return -EINVAL;
337 
338 	/* use same definitions for alh_id as previous generations */
339 	pdi->intel_alh_id = (sdw->instance * 16) + pdi->num + 3;
340 	if (pdi->num >= 2)
341 		pdi->intel_alh_id += 2;
342 
343 	/* the SHIM will be configured in the callback functions */
344 
345 	sdw_cdns_config_stream(cdns, ch, dir, pdi);
346 
347 	/* store pdi and state, may be needed in prepare step */
348 	dai_runtime->paused = false;
349 	dai_runtime->suspended = false;
350 	dai_runtime->pdi = pdi;
351 
352 	/* Inform DSP about PDI stream number */
353 	ret = intel_params_stream(sdw, substream, dai, params,
354 				  sdw->instance,
355 				  pdi->intel_alh_id);
356 	if (ret)
357 		return ret;
358 
359 	sconfig.direction = dir;
360 	sconfig.ch_count = ch;
361 	sconfig.frame_rate = params_rate(params);
362 	sconfig.type = dai_runtime->stream_type;
363 
364 	sconfig.bps = snd_pcm_format_width(params_format(params));
365 
366 	/* Port configuration */
367 	struct sdw_port_config *pconfig __free(kfree) = kzalloc(sizeof(*pconfig),
368 								GFP_KERNEL);
369 	if (!pconfig)
370 		return -ENOMEM;
371 
372 	pconfig->num = pdi->num;
373 	pconfig->ch_mask = (1 << ch) - 1;
374 
375 	ret = sdw_stream_add_master(&cdns->bus, &sconfig,
376 				    pconfig, 1, dai_runtime->stream);
377 	if (ret)
378 		dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
379 
380 	return ret;
381 }
382 
383 static int intel_prepare(struct snd_pcm_substream *substream,
384 			 struct snd_soc_dai *dai)
385 {
386 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
387 	struct sdw_intel *sdw = cdns_to_intel(cdns);
388 	struct sdw_cdns_dai_runtime *dai_runtime;
389 	int ch, dir;
390 	int ret = 0;
391 
392 	dai_runtime = cdns->dai_runtime_array[dai->id];
393 	if (!dai_runtime) {
394 		dev_err(dai->dev, "failed to get dai runtime in %s\n",
395 			__func__);
396 		return -EIO;
397 	}
398 
399 	if (dai_runtime->suspended) {
400 		struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
401 		struct snd_pcm_hw_params *hw_params;
402 
403 		hw_params = &rtd->dpcm[substream->stream].hw_params;
404 
405 		dai_runtime->suspended = false;
406 
407 		/*
408 		 * .prepare() is called after system resume, where we
409 		 * need to reinitialize the SHIM/ALH/Cadence IP.
410 		 * .prepare() is also called to deal with underflows,
411 		 * but in those cases we cannot touch ALH/SHIM
412 		 * registers
413 		 */
414 
415 		/* configure stream */
416 		ch = params_channels(hw_params);
417 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
418 			dir = SDW_DATA_DIR_RX;
419 		else
420 			dir = SDW_DATA_DIR_TX;
421 
422 		/* the SHIM will be configured in the callback functions */
423 
424 		sdw_cdns_config_stream(cdns, ch, dir, dai_runtime->pdi);
425 
426 		/* Inform DSP about PDI stream number */
427 		ret = intel_params_stream(sdw, substream, dai,
428 					  hw_params,
429 					  sdw->instance,
430 					  dai_runtime->pdi->intel_alh_id);
431 	}
432 
433 	return ret;
434 }
435 
436 static int
437 intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
438 {
439 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
440 	struct sdw_intel *sdw = cdns_to_intel(cdns);
441 	struct sdw_cdns_dai_runtime *dai_runtime;
442 	int ret;
443 
444 	dai_runtime = cdns->dai_runtime_array[dai->id];
445 	if (!dai_runtime)
446 		return -EIO;
447 
448 	/*
449 	 * The sdw stream state will transition to RELEASED when stream->
450 	 * master_list is empty. So the stream state will transition to
451 	 * DEPREPARED for the first cpu-dai and to RELEASED for the last
452 	 * cpu-dai.
453 	 */
454 	ret = sdw_stream_remove_master(&cdns->bus, dai_runtime->stream);
455 	if (ret < 0) {
456 		dev_err(dai->dev, "remove master from stream %s failed: %d\n",
457 			dai_runtime->stream->name, ret);
458 		return ret;
459 	}
460 
461 	ret = intel_free_stream(sdw, substream, dai, sdw->instance);
462 	if (ret < 0) {
463 		dev_err(dai->dev, "intel_free_stream: failed %d\n", ret);
464 		return ret;
465 	}
466 
467 	dai_runtime->pdi = NULL;
468 
469 	return 0;
470 }
471 
472 static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
473 				    void *stream, int direction)
474 {
475 	return cdns_set_sdw_stream(dai, stream, direction);
476 }
477 
478 static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
479 				  int direction)
480 {
481 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
482 	struct sdw_cdns_dai_runtime *dai_runtime;
483 
484 	dai_runtime = cdns->dai_runtime_array[dai->id];
485 	if (!dai_runtime)
486 		return ERR_PTR(-EINVAL);
487 
488 	return dai_runtime->stream;
489 }
490 
491 static int intel_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
492 {
493 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
494 	struct sdw_intel *sdw = cdns_to_intel(cdns);
495 	struct sdw_intel_link_res *res = sdw->link_res;
496 	struct sdw_cdns_dai_runtime *dai_runtime;
497 	int ret = 0;
498 
499 	/*
500 	 * The .trigger callback is used to program HDaudio DMA and send required IPC to audio
501 	 * firmware.
502 	 */
503 	if (res->ops && res->ops->trigger) {
504 		ret = res->ops->trigger(substream, cmd, dai);
505 		if (ret < 0)
506 			return ret;
507 	}
508 
509 	dai_runtime = cdns->dai_runtime_array[dai->id];
510 	if (!dai_runtime) {
511 		dev_err(dai->dev, "failed to get dai runtime in %s\n",
512 			__func__);
513 		return -EIO;
514 	}
515 
516 	switch (cmd) {
517 	case SNDRV_PCM_TRIGGER_SUSPEND:
518 
519 		/*
520 		 * The .prepare callback is used to deal with xruns and resume operations.
521 		 * In the case of xruns, the DMAs and SHIM registers cannot be touched,
522 		 * but for resume operations the DMAs and SHIM registers need to be initialized.
523 		 * the .trigger callback is used to track the suspend case only.
524 		 */
525 
526 		dai_runtime->suspended = true;
527 
528 		break;
529 
530 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
531 		dai_runtime->paused = true;
532 		break;
533 	case SNDRV_PCM_TRIGGER_STOP:
534 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
535 		dai_runtime->paused = false;
536 		break;
537 	default:
538 		break;
539 	}
540 
541 	return ret;
542 }
543 
544 static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
545 	.hw_params = intel_hw_params,
546 	.prepare = intel_prepare,
547 	.hw_free = intel_hw_free,
548 	.trigger = intel_trigger,
549 	.set_stream = intel_pcm_set_sdw_stream,
550 	.get_stream = intel_get_sdw_stream,
551 };
552 
553 static const struct snd_soc_component_driver dai_component = {
554 	.name			= "soundwire",
555 };
556 
557 /*
558  * PDI routines
559  */
560 static void intel_pdi_init(struct sdw_intel *sdw,
561 			   struct sdw_cdns_stream_config *config)
562 {
563 	void __iomem *shim = sdw->link_res->shim;
564 	int pcm_cap;
565 
566 	/* PCM Stream Capability */
567 	pcm_cap = intel_readw(shim, SDW_SHIM2_PCMSCAP);
568 
569 	config->pcm_bd = FIELD_GET(SDW_SHIM2_PCMSCAP_BSS, pcm_cap);
570 	config->pcm_in = FIELD_GET(SDW_SHIM2_PCMSCAP_ISS, pcm_cap);
571 	config->pcm_out = FIELD_GET(SDW_SHIM2_PCMSCAP_ISS, pcm_cap);
572 
573 	dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
574 		config->pcm_bd, config->pcm_in, config->pcm_out);
575 }
576 
577 static int
578 intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num)
579 {
580 	void __iomem *shim = sdw->link_res->shim;
581 
582 	/* zero based values for channel count in register */
583 	return intel_readw(shim, SDW_SHIM2_PCMSYCHC(pdi_num)) + 1;
584 }
585 
586 static void intel_pdi_get_ch_update(struct sdw_intel *sdw,
587 				    struct sdw_cdns_pdi *pdi,
588 				    unsigned int num_pdi,
589 				    unsigned int *num_ch)
590 {
591 	int ch_count = 0;
592 	int i;
593 
594 	for (i = 0; i < num_pdi; i++) {
595 		pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num);
596 		ch_count += pdi->ch_count;
597 		pdi++;
598 	}
599 
600 	*num_ch = ch_count;
601 }
602 
603 static void intel_pdi_stream_ch_update(struct sdw_intel *sdw,
604 				       struct sdw_cdns_streams *stream)
605 {
606 	intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
607 				&stream->num_ch_bd);
608 
609 	intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
610 				&stream->num_ch_in);
611 
612 	intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
613 				&stream->num_ch_out);
614 }
615 
616 static int intel_create_dai(struct sdw_cdns *cdns,
617 			    struct snd_soc_dai_driver *dais,
618 			    enum intel_pdi_type type,
619 			    u32 num, u32 off, u32 max_ch)
620 {
621 	int i;
622 
623 	if (!num)
624 		return 0;
625 
626 	for (i = off; i < (off + num); i++) {
627 		dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
628 					      "SDW%d Pin%d",
629 					      cdns->instance, i);
630 		if (!dais[i].name)
631 			return -ENOMEM;
632 
633 		if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
634 			dais[i].playback.channels_min = 1;
635 			dais[i].playback.channels_max = max_ch;
636 		}
637 
638 		if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
639 			dais[i].capture.channels_min = 1;
640 			dais[i].capture.channels_max = max_ch;
641 		}
642 
643 		dais[i].ops = &intel_pcm_dai_ops;
644 	}
645 
646 	return 0;
647 }
648 
649 static int intel_register_dai(struct sdw_intel *sdw)
650 {
651 	struct sdw_cdns_dai_runtime **dai_runtime_array;
652 	struct sdw_cdns_stream_config config;
653 	struct sdw_cdns *cdns = &sdw->cdns;
654 	struct sdw_cdns_streams *stream;
655 	struct snd_soc_dai_driver *dais;
656 	int num_dai;
657 	int ret;
658 	int off = 0;
659 
660 	/* Read the PDI config and initialize cadence PDI */
661 	intel_pdi_init(sdw, &config);
662 	ret = sdw_cdns_pdi_init(cdns, config);
663 	if (ret)
664 		return ret;
665 
666 	intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm);
667 
668 	/* DAIs are created based on total number of PDIs supported */
669 	num_dai = cdns->pcm.num_pdi;
670 
671 	dai_runtime_array = devm_kcalloc(cdns->dev, num_dai,
672 					 sizeof(struct sdw_cdns_dai_runtime *),
673 					 GFP_KERNEL);
674 	if (!dai_runtime_array)
675 		return -ENOMEM;
676 	cdns->dai_runtime_array = dai_runtime_array;
677 
678 	dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
679 	if (!dais)
680 		return -ENOMEM;
681 
682 	/* Create PCM DAIs */
683 	stream = &cdns->pcm;
684 
685 	ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
686 			       off, stream->num_ch_in);
687 	if (ret)
688 		return ret;
689 
690 	off += cdns->pcm.num_in;
691 	ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
692 			       off, stream->num_ch_out);
693 	if (ret)
694 		return ret;
695 
696 	off += cdns->pcm.num_out;
697 	ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
698 			       off, stream->num_ch_bd);
699 	if (ret)
700 		return ret;
701 
702 	return devm_snd_soc_register_component(cdns->dev, &dai_component,
703 					       dais, num_dai);
704 }
705 
706 static void intel_program_sdi(struct sdw_intel *sdw, int dev_num)
707 {
708 	int ret;
709 
710 	ret = hdac_bus_eml_sdw_set_lsdiid(sdw->link_res->hbus, sdw->instance, dev_num);
711 	if (ret < 0)
712 		dev_err(sdw->cdns.dev, "%s: could not set lsdiid for link %d %d\n",
713 			__func__, sdw->instance, dev_num);
714 }
715 
716 static int intel_get_link_count(struct sdw_intel *sdw)
717 {
718 	int ret;
719 
720 	ret = hdac_bus_eml_get_count(sdw->link_res->hbus, true, AZX_REG_ML_LEPTR_ID_SDW);
721 	if (!ret) {
722 		dev_err(sdw->cdns.dev, "%s: could not retrieve link count\n", __func__);
723 		return -ENODEV;
724 	}
725 
726 	if (ret > SDW_INTEL_MAX_LINKS) {
727 		dev_err(sdw->cdns.dev, "%s: link count %d exceed max %d\n", __func__, ret, SDW_INTEL_MAX_LINKS);
728 		return -EINVAL;
729 	}
730 
731 	return ret;
732 }
733 
734 const struct sdw_intel_hw_ops sdw_intel_lnl_hw_ops = {
735 	.debugfs_init = intel_ace2x_debugfs_init,
736 	.debugfs_exit = intel_ace2x_debugfs_exit,
737 
738 	.get_link_count = intel_get_link_count,
739 
740 	.register_dai = intel_register_dai,
741 
742 	.check_clock_stop = intel_check_clock_stop,
743 	.start_bus = intel_start_bus,
744 	.start_bus_after_reset = intel_start_bus_after_reset,
745 	.start_bus_after_clock_stop = intel_start_bus_after_clock_stop,
746 	.stop_bus = intel_stop_bus,
747 
748 	.link_power_up = intel_link_power_up,
749 	.link_power_down = intel_link_power_down,
750 
751 	.shim_check_wake = intel_shim_check_wake,
752 	.shim_wake = intel_shim_wake,
753 
754 	.pre_bank_switch = intel_pre_bank_switch,
755 	.post_bank_switch = intel_post_bank_switch,
756 
757 	.sync_arm = intel_sync_arm,
758 	.sync_go_unlocked = intel_sync_go_unlocked,
759 	.sync_go = intel_sync_go,
760 	.sync_check_cmdsync_unlocked = intel_check_cmdsync_unlocked,
761 
762 	.program_sdi = intel_program_sdi,
763 };
764 EXPORT_SYMBOL_NS(sdw_intel_lnl_hw_ops, SOUNDWIRE_INTEL);
765 
766 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_MLINK);
767