1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
5 * Soundwire Intel Master Driver
8 #include <linux/acpi.h>
9 #include <linux/debugfs.h>
10 #include <linux/delay.h>
12 #include <sound/pcm_params.h>
13 #include <linux/pm_runtime.h>
14 #include <sound/soc.h>
15 #include <linux/soundwire/sdw_registers.h>
16 #include <linux/soundwire/sdw.h>
17 #include <linux/soundwire/sdw_intel.h>
18 #include "cadence_master.h"
29 #define cdns_to_intel(_cdns) container_of(_cdns, struct sdw_intel, cdns)
32 * Read, write helpers for HW registers
34 static inline int intel_readl(void __iomem *base, int offset)
36 return readl(base + offset);
39 static inline void intel_writel(void __iomem *base, int offset, int value)
41 writel(value, base + offset);
44 static inline u16 intel_readw(void __iomem *base, int offset)
46 return readw(base + offset);
49 static inline void intel_writew(void __iomem *base, int offset, u16 value)
51 writew(value, base + offset);
54 static int intel_wait_bit(void __iomem *base, int offset, u32 mask, u32 target)
60 reg_read = readl(base + offset);
61 if ((reg_read & mask) == target)
65 usleep_range(50, 100);
66 } while (timeout != 0);
71 static int intel_clear_bit(void __iomem *base, int offset, u32 value, u32 mask)
73 writel(value, base + offset);
74 return intel_wait_bit(base, offset, mask, 0);
77 static int intel_set_bit(void __iomem *base, int offset, u32 value, u32 mask)
79 writel(value, base + offset);
80 return intel_wait_bit(base, offset, mask, mask);
86 #ifdef CONFIG_DEBUG_FS
88 #define RD_BUF (2 * PAGE_SIZE)
90 static ssize_t intel_sprintf(void __iomem *mem, bool l,
91 char *buf, size_t pos, unsigned int reg)
96 value = intel_readl(mem, reg);
98 value = intel_readw(mem, reg);
100 return scnprintf(buf + pos, RD_BUF - pos, "%4x\t%4x\n", reg, value);
103 static int intel_reg_show(struct seq_file *s_file, void *data)
105 struct sdw_intel *sdw = s_file->private;
106 void __iomem *s = sdw->link_res->shim;
107 void __iomem *a = sdw->link_res->alh;
111 unsigned int links, reg;
113 buf = kzalloc(RD_BUF, GFP_KERNEL);
117 links = intel_readl(s, SDW_SHIM_LCAP) & SDW_SHIM_LCAP_LCOUNT_MASK;
119 ret = scnprintf(buf, RD_BUF, "Register Value\n");
120 ret += scnprintf(buf + ret, RD_BUF - ret, "\nShim\n");
122 for (i = 0; i < links; i++) {
123 reg = SDW_SHIM_LCAP + i * 4;
124 ret += intel_sprintf(s, true, buf, ret, reg);
127 for (i = 0; i < links; i++) {
128 ret += scnprintf(buf + ret, RD_BUF - ret, "\nLink%d\n", i);
129 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLSCAP(i));
130 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS0CM(i));
131 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS1CM(i));
132 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS2CM(i));
133 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS3CM(i));
134 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PCMSCAP(i));
136 ret += scnprintf(buf + ret, RD_BUF - ret, "\n PCMSyCH registers\n");
139 * the value 10 is the number of PDIs. We will need a
140 * cleanup to remove hard-coded Intel configurations
141 * from cadence_master.c
143 for (j = 0; j < 10; j++) {
144 ret += intel_sprintf(s, false, buf, ret,
145 SDW_SHIM_PCMSYCHM(i, j));
146 ret += intel_sprintf(s, false, buf, ret,
147 SDW_SHIM_PCMSYCHC(i, j));
149 ret += scnprintf(buf + ret, RD_BUF - ret, "\n IOCTL, CTMCTL\n");
151 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_IOCTL(i));
152 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTMCTL(i));
155 ret += scnprintf(buf + ret, RD_BUF - ret, "\nWake registers\n");
156 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKEEN);
157 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKESTS);
159 ret += scnprintf(buf + ret, RD_BUF - ret, "\nALH STRMzCFG\n");
160 for (i = 0; i < SDW_ALH_NUM_STREAMS; i++)
161 ret += intel_sprintf(a, true, buf, ret, SDW_ALH_STRMZCFG(i));
163 seq_printf(s_file, "%s", buf);
168 DEFINE_SHOW_ATTRIBUTE(intel_reg);
170 static int intel_set_m_datamode(void *data, u64 value)
172 struct sdw_intel *sdw = data;
173 struct sdw_bus *bus = &sdw->cdns.bus;
175 if (value > SDW_PORT_DATA_MODE_STATIC_1)
178 /* Userspace changed the hardware state behind the kernel's back */
179 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
181 bus->params.m_data_mode = value;
185 DEFINE_DEBUGFS_ATTRIBUTE(intel_set_m_datamode_fops, NULL,
186 intel_set_m_datamode, "%llu\n");
188 static int intel_set_s_datamode(void *data, u64 value)
190 struct sdw_intel *sdw = data;
191 struct sdw_bus *bus = &sdw->cdns.bus;
193 if (value > SDW_PORT_DATA_MODE_STATIC_1)
196 /* Userspace changed the hardware state behind the kernel's back */
197 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
199 bus->params.s_data_mode = value;
203 DEFINE_DEBUGFS_ATTRIBUTE(intel_set_s_datamode_fops, NULL,
204 intel_set_s_datamode, "%llu\n");
206 static void intel_debugfs_init(struct sdw_intel *sdw)
208 struct dentry *root = sdw->cdns.bus.debugfs;
213 sdw->debugfs = debugfs_create_dir("intel-sdw", root);
215 debugfs_create_file("intel-registers", 0400, sdw->debugfs, sdw,
218 debugfs_create_file("intel-m-datamode", 0200, sdw->debugfs, sdw,
219 &intel_set_m_datamode_fops);
221 debugfs_create_file("intel-s-datamode", 0200, sdw->debugfs, sdw,
222 &intel_set_s_datamode_fops);
224 sdw_cdns_debugfs_init(&sdw->cdns, sdw->debugfs);
227 static void intel_debugfs_exit(struct sdw_intel *sdw)
229 debugfs_remove_recursive(sdw->debugfs);
232 static void intel_debugfs_init(struct sdw_intel *sdw) {}
233 static void intel_debugfs_exit(struct sdw_intel *sdw) {}
234 #endif /* CONFIG_DEBUG_FS */
239 /* this needs to be called with shim_lock */
240 static void intel_shim_glue_to_master_ip(struct sdw_intel *sdw)
242 void __iomem *shim = sdw->link_res->shim;
243 unsigned int link_id = sdw->instance;
246 /* Switch to MIP from Glue logic */
247 ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
249 ioctl &= ~(SDW_SHIM_IOCTL_DOE);
250 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
251 usleep_range(10, 15);
253 ioctl &= ~(SDW_SHIM_IOCTL_DO);
254 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
255 usleep_range(10, 15);
257 ioctl |= (SDW_SHIM_IOCTL_MIF);
258 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
259 usleep_range(10, 15);
261 ioctl &= ~(SDW_SHIM_IOCTL_BKE);
262 ioctl &= ~(SDW_SHIM_IOCTL_COE);
263 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
264 usleep_range(10, 15);
266 /* at this point Master IP has full control of the I/Os */
269 /* this needs to be called with shim_lock */
270 static void intel_shim_master_ip_to_glue(struct sdw_intel *sdw)
272 unsigned int link_id = sdw->instance;
273 void __iomem *shim = sdw->link_res->shim;
277 ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
278 ioctl |= SDW_SHIM_IOCTL_BKE;
279 ioctl |= SDW_SHIM_IOCTL_COE;
280 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
281 usleep_range(10, 15);
283 ioctl &= ~(SDW_SHIM_IOCTL_MIF);
284 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
285 usleep_range(10, 15);
287 /* at this point Integration Glue has full control of the I/Os */
290 /* this needs to be called with shim_lock */
291 static void intel_shim_init(struct sdw_intel *sdw)
293 void __iomem *shim = sdw->link_res->shim;
294 unsigned int link_id = sdw->instance;
295 u16 ioctl = 0, act = 0;
297 /* Initialize Shim */
298 ioctl |= SDW_SHIM_IOCTL_BKE;
299 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
300 usleep_range(10, 15);
302 ioctl |= SDW_SHIM_IOCTL_WPDD;
303 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
304 usleep_range(10, 15);
306 ioctl |= SDW_SHIM_IOCTL_DO;
307 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
308 usleep_range(10, 15);
310 ioctl |= SDW_SHIM_IOCTL_DOE;
311 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
312 usleep_range(10, 15);
314 intel_shim_glue_to_master_ip(sdw);
316 u16p_replace_bits(&act, 0x1, SDW_SHIM_CTMCTL_DOAIS);
317 act |= SDW_SHIM_CTMCTL_DACTQE;
318 act |= SDW_SHIM_CTMCTL_DODS;
319 intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act);
320 usleep_range(10, 15);
323 static int intel_shim_check_wake(struct sdw_intel *sdw)
328 shim = sdw->link_res->shim;
329 wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
331 return wake_sts & BIT(sdw->instance);
334 static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
336 void __iomem *shim = sdw->link_res->shim;
337 unsigned int link_id = sdw->instance;
338 u16 wake_en, wake_sts;
340 mutex_lock(sdw->link_res->shim_lock);
341 wake_en = intel_readw(shim, SDW_SHIM_WAKEEN);
344 /* Enable the wakeup */
345 wake_en |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
346 intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
348 /* Disable the wake up interrupt */
349 wake_en &= ~(SDW_SHIM_WAKEEN_ENABLE << link_id);
350 intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
352 /* Clear wake status */
353 wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
354 wake_sts |= (SDW_SHIM_WAKESTS_STATUS << link_id);
355 intel_writew(shim, SDW_SHIM_WAKESTS, wake_sts);
357 mutex_unlock(sdw->link_res->shim_lock);
360 static int intel_link_power_up(struct sdw_intel *sdw)
362 unsigned int link_id = sdw->instance;
363 void __iomem *shim = sdw->link_res->shim;
364 u32 *shim_mask = sdw->link_res->shim_mask;
365 struct sdw_bus *bus = &sdw->cdns.bus;
366 struct sdw_master_prop *prop = &bus->prop;
367 u32 spa_mask, cpa_mask;
373 mutex_lock(sdw->link_res->shim_lock);
376 * The hardware relies on an internal counter, typically 4kHz,
377 * to generate the SoundWire SSP - which defines a 'safe'
378 * synchronization point between commands and audio transport
379 * and allows for multi link synchronization. The SYNCPRD value
380 * is only dependent on the oscillator clock provided to
381 * the IP, so adjust based on _DSD properties reported in DSDT
382 * tables. The values reported are based on either 24MHz
383 * (CNL/CML) or 38.4 MHz (ICL/TGL+).
385 if (prop->mclk_freq % 6000000)
386 syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_38_4;
388 syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24;
391 dev_dbg(sdw->cdns.dev, "powering up all links\n");
393 /* we first need to program the SyncPRD/CPU registers */
394 dev_dbg(sdw->cdns.dev,
395 "first link up, programming SYNCPRD\n");
397 /* set SyncPRD period */
398 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
399 u32p_replace_bits(&sync_reg, syncprd, SDW_SHIM_SYNC_SYNCPRD);
401 /* Set SyncCPU bit */
402 sync_reg |= SDW_SHIM_SYNC_SYNCCPU;
403 intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
405 /* Link power up sequence */
406 link_control = intel_readl(shim, SDW_SHIM_LCTL);
408 /* only power-up enabled links */
409 spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, sdw->link_res->link_mask);
410 cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
412 link_control |= spa_mask;
414 ret = intel_set_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
416 dev_err(sdw->cdns.dev, "Failed to power up link: %d\n", ret);
420 /* SyncCPU will change once link is active */
421 ret = intel_wait_bit(shim, SDW_SHIM_SYNC,
422 SDW_SHIM_SYNC_SYNCCPU, 0);
424 dev_err(sdw->cdns.dev,
425 "Failed to set SHIM_SYNC: %d\n", ret);
430 *shim_mask |= BIT(link_id);
432 sdw->cdns.link_up = true;
434 intel_shim_init(sdw);
437 mutex_unlock(sdw->link_res->shim_lock);
442 static int intel_link_power_down(struct sdw_intel *sdw)
444 u32 link_control, spa_mask, cpa_mask;
445 unsigned int link_id = sdw->instance;
446 void __iomem *shim = sdw->link_res->shim;
447 u32 *shim_mask = sdw->link_res->shim_mask;
450 mutex_lock(sdw->link_res->shim_lock);
452 if (!(*shim_mask & BIT(link_id)))
453 dev_err(sdw->cdns.dev,
454 "%s: Unbalanced power-up/down calls\n", __func__);
456 sdw->cdns.link_up = false;
458 intel_shim_master_ip_to_glue(sdw);
460 *shim_mask &= ~BIT(link_id);
464 dev_dbg(sdw->cdns.dev, "powering down all links\n");
466 /* Link power down sequence */
467 link_control = intel_readl(shim, SDW_SHIM_LCTL);
469 /* only power-down enabled links */
470 spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, ~sdw->link_res->link_mask);
471 cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
473 link_control &= spa_mask;
475 ret = intel_clear_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
477 dev_err(sdw->cdns.dev, "%s: could not power down link\n", __func__);
480 * we leave the sdw->cdns.link_up flag as false since we've disabled
481 * the link at this point and cannot handle interrupts any longer.
486 mutex_unlock(sdw->link_res->shim_lock);
491 static void intel_shim_sync_arm(struct sdw_intel *sdw)
493 void __iomem *shim = sdw->link_res->shim;
496 mutex_lock(sdw->link_res->shim_lock);
498 /* update SYNC register */
499 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
500 sync_reg |= (SDW_SHIM_SYNC_CMDSYNC << sdw->instance);
501 intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
503 mutex_unlock(sdw->link_res->shim_lock);
506 static int intel_shim_sync_go_unlocked(struct sdw_intel *sdw)
508 void __iomem *shim = sdw->link_res->shim;
512 /* Read SYNC register */
513 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
516 * Set SyncGO bit to synchronously trigger a bank switch for
517 * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all
520 sync_reg |= SDW_SHIM_SYNC_SYNCGO;
522 ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg,
523 SDW_SHIM_SYNC_SYNCGO);
526 dev_err(sdw->cdns.dev, "SyncGO clear failed: %d\n", ret);
531 static int intel_shim_sync_go(struct sdw_intel *sdw)
535 mutex_lock(sdw->link_res->shim_lock);
537 ret = intel_shim_sync_go_unlocked(sdw);
539 mutex_unlock(sdw->link_res->shim_lock);
547 static void intel_pdi_init(struct sdw_intel *sdw,
548 struct sdw_cdns_stream_config *config)
550 void __iomem *shim = sdw->link_res->shim;
551 unsigned int link_id = sdw->instance;
554 /* PCM Stream Capability */
555 pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id));
557 config->pcm_bd = FIELD_GET(SDW_SHIM_PCMSCAP_BSS, pcm_cap);
558 config->pcm_in = FIELD_GET(SDW_SHIM_PCMSCAP_ISS, pcm_cap);
559 config->pcm_out = FIELD_GET(SDW_SHIM_PCMSCAP_OSS, pcm_cap);
561 dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
562 config->pcm_bd, config->pcm_in, config->pcm_out);
566 intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num)
568 void __iomem *shim = sdw->link_res->shim;
569 unsigned int link_id = sdw->instance;
572 count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num));
575 * WORKAROUND: on all existing Intel controllers, pdi
576 * number 2 reports channel count as 1 even though it
577 * supports 8 channels. Performing hardcoding for pdi
583 /* zero based values for channel count in register */
589 static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
590 struct sdw_cdns_pdi *pdi,
591 unsigned int num_pdi,
592 unsigned int *num_ch)
596 for (i = 0; i < num_pdi; i++) {
597 pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num);
598 ch_count += pdi->ch_count;
606 static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
607 struct sdw_cdns_streams *stream)
609 intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
612 intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
615 intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
616 &stream->num_ch_out);
621 static int intel_pdi_ch_update(struct sdw_intel *sdw)
623 intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm);
629 intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
631 void __iomem *shim = sdw->link_res->shim;
632 unsigned int link_id = sdw->instance;
635 /* the Bulk and PCM streams are not contiguous */
636 pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
638 pdi->intel_alh_id += 2;
641 * Program stream parameters to stream SHIM register
642 * This is applicable for PCM stream only.
644 if (pdi->type != SDW_STREAM_PCM)
647 if (pdi->dir == SDW_DATA_DIR_RX)
648 pdi_conf |= SDW_SHIM_PCMSYCM_DIR;
650 pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR);
652 u32p_replace_bits(&pdi_conf, pdi->intel_alh_id, SDW_SHIM_PCMSYCM_STREAM);
653 u32p_replace_bits(&pdi_conf, pdi->l_ch_num, SDW_SHIM_PCMSYCM_LCHN);
654 u32p_replace_bits(&pdi_conf, pdi->h_ch_num, SDW_SHIM_PCMSYCM_HCHN);
656 intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
660 intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
662 void __iomem *alh = sdw->link_res->alh;
663 unsigned int link_id = sdw->instance;
666 /* the Bulk and PCM streams are not contiguous */
667 pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
669 pdi->intel_alh_id += 2;
671 /* Program Stream config ALH register */
672 conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
674 u32p_replace_bits(&conf, SDW_ALH_STRMZCFG_DMAT_VAL, SDW_ALH_STRMZCFG_DMAT);
675 u32p_replace_bits(&conf, pdi->ch_count - 1, SDW_ALH_STRMZCFG_CHN);
677 intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
680 static int intel_params_stream(struct sdw_intel *sdw,
682 struct snd_soc_dai *dai,
683 struct snd_pcm_hw_params *hw_params,
684 int link_id, int alh_stream_id)
686 struct sdw_intel_link_res *res = sdw->link_res;
687 struct sdw_intel_stream_params_data params_data;
689 params_data.stream = stream; /* direction */
690 params_data.dai = dai;
691 params_data.hw_params = hw_params;
692 params_data.link_id = link_id;
693 params_data.alh_stream_id = alh_stream_id;
695 if (res->ops && res->ops->params_stream && res->dev)
696 return res->ops->params_stream(res->dev,
701 static int intel_free_stream(struct sdw_intel *sdw,
703 struct snd_soc_dai *dai,
706 struct sdw_intel_link_res *res = sdw->link_res;
707 struct sdw_intel_stream_free_data free_data;
709 free_data.stream = stream; /* direction */
711 free_data.link_id = link_id;
713 if (res->ops && res->ops->free_stream && res->dev)
714 return res->ops->free_stream(res->dev,
721 * bank switch routines
724 static int intel_pre_bank_switch(struct sdw_intel *sdw)
726 struct sdw_cdns *cdns = &sdw->cdns;
727 struct sdw_bus *bus = &cdns->bus;
729 /* Write to register only for multi-link */
730 if (!bus->multi_link)
733 intel_shim_sync_arm(sdw);
738 static int intel_post_bank_switch(struct sdw_intel *sdw)
740 struct sdw_cdns *cdns = &sdw->cdns;
741 struct sdw_bus *bus = &cdns->bus;
742 void __iomem *shim = sdw->link_res->shim;
745 /* Write to register only for multi-link */
746 if (!bus->multi_link)
749 mutex_lock(sdw->link_res->shim_lock);
751 /* Read SYNC register */
752 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
755 * post_bank_switch() ops is called from the bus in loop for
756 * all the Masters in the steam with the expectation that
757 * we trigger the bankswitch for the only first Master in the list
758 * and do nothing for the other Masters
760 * So, set the SYNCGO bit only if CMDSYNC bit is set for any Master.
762 if (!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK)) {
767 ret = intel_shim_sync_go_unlocked(sdw);
769 mutex_unlock(sdw->link_res->shim_lock);
772 dev_err(sdw->cdns.dev, "Post bank switch failed: %d\n", ret);
781 static int intel_startup(struct snd_pcm_substream *substream,
782 struct snd_soc_dai *dai)
784 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
787 ret = pm_runtime_resume_and_get(cdns->dev);
788 if (ret < 0 && ret != -EACCES) {
789 dev_err_ratelimited(cdns->dev,
790 "pm_runtime_resume_and_get failed in %s, ret %d\n",
797 static int intel_hw_params(struct snd_pcm_substream *substream,
798 struct snd_pcm_hw_params *params,
799 struct snd_soc_dai *dai)
801 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
802 struct sdw_intel *sdw = cdns_to_intel(cdns);
803 struct sdw_cdns_dai_runtime *dai_runtime;
804 struct sdw_cdns_pdi *pdi;
805 struct sdw_stream_config sconfig;
806 struct sdw_port_config *pconfig;
810 dai_runtime = cdns->dai_runtime_array[dai->id];
814 ch = params_channels(params);
815 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
816 dir = SDW_DATA_DIR_RX;
818 dir = SDW_DATA_DIR_TX;
820 pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
827 /* do run-time configurations for SHIM, ALH and PDI/PORT */
828 intel_pdi_shim_configure(sdw, pdi);
829 intel_pdi_alh_configure(sdw, pdi);
830 sdw_cdns_config_stream(cdns, ch, dir, pdi);
832 /* store pdi and hw_params, may be needed in prepare step */
833 dai_runtime->paused = false;
834 dai_runtime->suspended = false;
835 dai_runtime->pdi = pdi;
836 dai_runtime->hw_params = params;
838 /* Inform DSP about PDI stream number */
839 ret = intel_params_stream(sdw, substream->stream, dai, params,
845 sconfig.direction = dir;
846 sconfig.ch_count = ch;
847 sconfig.frame_rate = params_rate(params);
848 sconfig.type = dai_runtime->stream_type;
850 sconfig.bps = snd_pcm_format_width(params_format(params));
852 /* Port configuration */
853 pconfig = kzalloc(sizeof(*pconfig), GFP_KERNEL);
859 pconfig->num = pdi->num;
860 pconfig->ch_mask = (1 << ch) - 1;
862 ret = sdw_stream_add_master(&cdns->bus, &sconfig,
863 pconfig, 1, dai_runtime->stream);
865 dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
872 static int intel_prepare(struct snd_pcm_substream *substream,
873 struct snd_soc_dai *dai)
875 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
876 struct sdw_intel *sdw = cdns_to_intel(cdns);
877 struct sdw_cdns_dai_runtime *dai_runtime;
881 dai_runtime = cdns->dai_runtime_array[dai->id];
883 dev_err(dai->dev, "failed to get dai runtime in %s\n",
888 if (dai_runtime->suspended) {
889 dai_runtime->suspended = false;
892 * .prepare() is called after system resume, where we
893 * need to reinitialize the SHIM/ALH/Cadence IP.
894 * .prepare() is also called to deal with underflows,
895 * but in those cases we cannot touch ALH/SHIM
899 /* configure stream */
900 ch = params_channels(dai_runtime->hw_params);
901 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
902 dir = SDW_DATA_DIR_RX;
904 dir = SDW_DATA_DIR_TX;
906 intel_pdi_shim_configure(sdw, dai_runtime->pdi);
907 intel_pdi_alh_configure(sdw, dai_runtime->pdi);
908 sdw_cdns_config_stream(cdns, ch, dir, dai_runtime->pdi);
910 /* Inform DSP about PDI stream number */
911 ret = intel_params_stream(sdw, substream->stream, dai,
912 dai_runtime->hw_params,
914 dai_runtime->pdi->intel_alh_id);
921 intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
923 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
924 struct sdw_intel *sdw = cdns_to_intel(cdns);
925 struct sdw_cdns_dai_runtime *dai_runtime;
928 dai_runtime = cdns->dai_runtime_array[dai->id];
933 * The sdw stream state will transition to RELEASED when stream->
934 * master_list is empty. So the stream state will transition to
935 * DEPREPARED for the first cpu-dai and to RELEASED for the last
938 ret = sdw_stream_remove_master(&cdns->bus, dai_runtime->stream);
940 dev_err(dai->dev, "remove master from stream %s failed: %d\n",
941 dai_runtime->stream->name, ret);
945 ret = intel_free_stream(sdw, substream->stream, dai, sdw->instance);
947 dev_err(dai->dev, "intel_free_stream: failed %d\n", ret);
951 dai_runtime->hw_params = NULL;
952 dai_runtime->pdi = NULL;
957 static void intel_shutdown(struct snd_pcm_substream *substream,
958 struct snd_soc_dai *dai)
960 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
962 pm_runtime_mark_last_busy(cdns->dev);
963 pm_runtime_put_autosuspend(cdns->dev);
966 static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
967 void *stream, int direction)
969 return cdns_set_sdw_stream(dai, stream, direction);
972 static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
975 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
976 struct sdw_cdns_dai_runtime *dai_runtime;
978 dai_runtime = cdns->dai_runtime_array[dai->id];
980 return ERR_PTR(-EINVAL);
982 return dai_runtime->stream;
985 static int intel_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
987 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
988 struct sdw_intel *sdw = cdns_to_intel(cdns);
989 struct sdw_intel_link_res *res = sdw->link_res;
990 struct sdw_cdns_dai_runtime *dai_runtime;
994 * The .trigger callback is used to send required IPC to audio
995 * firmware. The .free_stream callback will still be called
996 * by intel_free_stream() in the TRIGGER_SUSPEND case.
998 if (res->ops && res->ops->trigger)
999 res->ops->trigger(dai, cmd, substream->stream);
1001 dai_runtime = cdns->dai_runtime_array[dai->id];
1003 dev_err(dai->dev, "failed to get dai runtime in %s\n",
1009 case SNDRV_PCM_TRIGGER_SUSPEND:
1012 * The .prepare callback is used to deal with xruns and resume operations.
1013 * In the case of xruns, the DMAs and SHIM registers cannot be touched,
1014 * but for resume operations the DMAs and SHIM registers need to be initialized.
1015 * the .trigger callback is used to track the suspend case only.
1018 dai_runtime->suspended = true;
1020 ret = intel_free_stream(sdw, substream->stream, dai, sdw->instance);
1023 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1024 dai_runtime->paused = true;
1026 case SNDRV_PCM_TRIGGER_STOP:
1027 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1028 dai_runtime->paused = false;
1037 static int intel_component_probe(struct snd_soc_component *component)
1042 * make sure the device is pm_runtime_active before initiating
1043 * bus transactions during the card registration.
1044 * We use pm_runtime_resume() here, without taking a reference
1045 * and releasing it immediately.
1047 ret = pm_runtime_resume(component->dev);
1048 if (ret < 0 && ret != -EACCES)
1054 static int intel_component_dais_suspend(struct snd_soc_component *component)
1056 struct snd_soc_dai *dai;
1059 * In the corner case where a SUSPEND happens during a PAUSE, the ALSA core
1060 * does not throw the TRIGGER_SUSPEND. This leaves the DAIs in an unbalanced state.
1061 * Since the component suspend is called last, we can trap this corner case
1062 * and force the DAIs to release their resources.
1064 for_each_component_dais(component, dai) {
1065 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1066 struct sdw_intel *sdw = cdns_to_intel(cdns);
1067 struct sdw_cdns_dai_runtime *dai_runtime;
1070 dai_runtime = cdns->dai_runtime_array[dai->id];
1075 if (dai_runtime->suspended)
1078 if (dai_runtime->paused) {
1079 dai_runtime->suspended = true;
1081 ret = intel_free_stream(sdw, dai_runtime->direction, dai, sdw->instance);
1090 static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
1091 .startup = intel_startup,
1092 .hw_params = intel_hw_params,
1093 .prepare = intel_prepare,
1094 .hw_free = intel_hw_free,
1095 .trigger = intel_trigger,
1096 .shutdown = intel_shutdown,
1097 .set_stream = intel_pcm_set_sdw_stream,
1098 .get_stream = intel_get_sdw_stream,
1101 static const struct snd_soc_component_driver dai_component = {
1102 .name = "soundwire",
1103 .probe = intel_component_probe,
1104 .suspend = intel_component_dais_suspend,
1105 .legacy_dai_naming = 1,
1108 static int intel_create_dai(struct sdw_cdns *cdns,
1109 struct snd_soc_dai_driver *dais,
1110 enum intel_pdi_type type,
1111 u32 num, u32 off, u32 max_ch)
1118 /* TODO: Read supported rates/formats from hardware */
1119 for (i = off; i < (off + num); i++) {
1120 dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
1126 if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
1127 dais[i].playback.channels_min = 1;
1128 dais[i].playback.channels_max = max_ch;
1129 dais[i].playback.rates = SNDRV_PCM_RATE_48000;
1130 dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
1133 if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
1134 dais[i].capture.channels_min = 1;
1135 dais[i].capture.channels_max = max_ch;
1136 dais[i].capture.rates = SNDRV_PCM_RATE_48000;
1137 dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
1140 dais[i].ops = &intel_pcm_dai_ops;
1146 static int intel_register_dai(struct sdw_intel *sdw)
1148 struct sdw_cdns_dai_runtime **dai_runtime_array;
1149 struct sdw_cdns_stream_config config;
1150 struct sdw_cdns *cdns = &sdw->cdns;
1151 struct sdw_cdns_streams *stream;
1152 struct snd_soc_dai_driver *dais;
1153 int num_dai, ret, off = 0;
1155 /* Read the PDI config and initialize cadence PDI */
1156 intel_pdi_init(sdw, &config);
1157 ret = sdw_cdns_pdi_init(cdns, config);
1161 intel_pdi_ch_update(sdw);
1163 /* DAIs are created based on total number of PDIs supported */
1164 num_dai = cdns->pcm.num_pdi;
1166 dai_runtime_array = devm_kcalloc(cdns->dev, num_dai,
1167 sizeof(struct sdw_cdns_dai_runtime *),
1169 if (!dai_runtime_array)
1171 cdns->dai_runtime_array = dai_runtime_array;
1173 dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
1177 /* Create PCM DAIs */
1178 stream = &cdns->pcm;
1180 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
1181 off, stream->num_ch_in);
1185 off += cdns->pcm.num_in;
1186 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
1187 off, stream->num_ch_out);
1191 off += cdns->pcm.num_out;
1192 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
1193 off, stream->num_ch_bd);
1197 return devm_snd_soc_register_component(cdns->dev, &dai_component,
1201 static int intel_start_bus(struct sdw_intel *sdw)
1203 struct device *dev = sdw->cdns.dev;
1204 struct sdw_cdns *cdns = &sdw->cdns;
1205 struct sdw_bus *bus = &cdns->bus;
1208 ret = sdw_cdns_enable_interrupt(cdns, true);
1210 dev_err(dev, "%s: cannot enable interrupts: %d\n", __func__, ret);
1215 * follow recommended programming flows to avoid timeouts when
1218 if (bus->multi_link)
1219 intel_shim_sync_arm(sdw);
1221 ret = sdw_cdns_init(cdns);
1223 dev_err(dev, "%s: unable to initialize Cadence IP: %d\n", __func__, ret);
1227 ret = sdw_cdns_exit_reset(cdns);
1229 dev_err(dev, "%s: unable to exit bus reset sequence: %d\n", __func__, ret);
1233 if (bus->multi_link) {
1234 ret = intel_shim_sync_go(sdw);
1236 dev_err(dev, "%s: sync go failed: %d\n", __func__, ret);
1240 sdw_cdns_check_self_clearing_bits(cdns, __func__,
1241 true, INTEL_MASTER_RESET_ITERATIONS);
1246 sdw_cdns_enable_interrupt(cdns, false);
1250 static int intel_start_bus_after_reset(struct sdw_intel *sdw)
1252 struct device *dev = sdw->cdns.dev;
1253 struct sdw_cdns *cdns = &sdw->cdns;
1254 struct sdw_bus *bus = &cdns->bus;
1260 * An exception condition occurs for the CLK_STOP_BUS_RESET
1261 * case if one or more masters remain active. In this condition,
1262 * all the masters are powered on for they are in the same power
1263 * domain. Master can preserve its context for clock stop0, so
1264 * there is no need to clear slave status and reset bus.
1266 clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
1271 * make sure all Slaves are tagged as UNATTACHED and
1272 * provide reason for reinitialization
1275 status = SDW_UNATTACH_REQUEST_MASTER_RESET;
1276 sdw_clear_slave_status(bus, status);
1278 ret = sdw_cdns_enable_interrupt(cdns, true);
1280 dev_err(dev, "cannot enable interrupts during resume\n");
1285 * follow recommended programming flows to avoid
1286 * timeouts when gsync is enabled
1288 if (bus->multi_link)
1289 intel_shim_sync_arm(sdw);
1292 * Re-initialize the IP since it was powered-off
1294 sdw_cdns_init(&sdw->cdns);
1297 ret = sdw_cdns_enable_interrupt(cdns, true);
1299 dev_err(dev, "cannot enable interrupts during resume\n");
1304 ret = sdw_cdns_clock_restart(cdns, !clock_stop0);
1306 dev_err(dev, "unable to restart clock during resume\n");
1311 ret = sdw_cdns_exit_reset(cdns);
1313 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1317 if (bus->multi_link) {
1318 ret = intel_shim_sync_go(sdw);
1320 dev_err(sdw->cdns.dev, "sync go failed during resume\n");
1325 sdw_cdns_check_self_clearing_bits(cdns, __func__, true, INTEL_MASTER_RESET_ITERATIONS);
1330 sdw_cdns_enable_interrupt(cdns, false);
1334 static void intel_check_clock_stop(struct sdw_intel *sdw)
1336 struct device *dev = sdw->cdns.dev;
1339 clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
1341 dev_err(dev, "%s: invalid configuration, clock was not stopped\n", __func__);
1344 static int intel_start_bus_after_clock_stop(struct sdw_intel *sdw)
1346 struct device *dev = sdw->cdns.dev;
1347 struct sdw_cdns *cdns = &sdw->cdns;
1350 ret = sdw_cdns_enable_interrupt(cdns, true);
1352 dev_err(dev, "%s: cannot enable interrupts: %d\n", __func__, ret);
1356 ret = sdw_cdns_clock_restart(cdns, false);
1358 dev_err(dev, "%s: unable to restart clock: %d\n", __func__, ret);
1359 sdw_cdns_enable_interrupt(cdns, false);
1363 sdw_cdns_check_self_clearing_bits(cdns, "intel_resume_runtime no_quirks",
1364 true, INTEL_MASTER_RESET_ITERATIONS);
1369 static int intel_stop_bus(struct sdw_intel *sdw, bool clock_stop)
1371 struct device *dev = sdw->cdns.dev;
1372 struct sdw_cdns *cdns = &sdw->cdns;
1373 bool wake_enable = false;
1377 ret = sdw_cdns_clock_stop(cdns, true);
1379 dev_err(dev, "%s: cannot stop clock: %d\n", __func__, ret);
1384 ret = sdw_cdns_enable_interrupt(cdns, false);
1386 dev_err(dev, "%s: cannot disable interrupts: %d\n", __func__, ret);
1390 ret = intel_link_power_down(sdw);
1392 dev_err(dev, "%s: Link power down failed: %d\n", __func__, ret);
1396 intel_shim_wake(sdw, wake_enable);
1401 const struct sdw_intel_hw_ops sdw_intel_cnl_hw_ops = {
1402 .debugfs_init = intel_debugfs_init,
1403 .debugfs_exit = intel_debugfs_exit,
1405 .register_dai = intel_register_dai,
1407 .check_clock_stop = intel_check_clock_stop,
1408 .start_bus = intel_start_bus,
1409 .start_bus_after_reset = intel_start_bus_after_reset,
1410 .start_bus_after_clock_stop = intel_start_bus_after_clock_stop,
1411 .stop_bus = intel_stop_bus,
1413 .link_power_up = intel_link_power_up,
1414 .link_power_down = intel_link_power_down,
1416 .shim_check_wake = intel_shim_check_wake,
1417 .shim_wake = intel_shim_wake,
1419 .pre_bank_switch = intel_pre_bank_switch,
1420 .post_bank_switch = intel_post_bank_switch,
1422 EXPORT_SYMBOL_NS(sdw_intel_cnl_hw_ops, SOUNDWIRE_INTEL);