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>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
14 #include <linux/auxiliary_bus.h>
15 #include <sound/pcm_params.h>
16 #include <linux/pm_runtime.h>
17 #include <sound/soc.h>
18 #include <linux/soundwire/sdw_registers.h>
19 #include <linux/soundwire/sdw.h>
20 #include <linux/soundwire/sdw_intel.h>
21 #include "cadence_master.h"
25 #define INTEL_MASTER_SUSPEND_DELAY_MS 3000
28 * debug/config flags for the Intel SoundWire Master.
30 * Since we may have multiple masters active, we can have up to 8
31 * flags reused in each byte, with master0 using the ls-byte, etc.
34 #define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME BIT(0)
35 #define SDW_INTEL_MASTER_DISABLE_CLOCK_STOP BIT(1)
36 #define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE BIT(2)
37 #define SDW_INTEL_MASTER_DISABLE_MULTI_LINK BIT(3)
40 module_param_named(sdw_md_flags, md_flags, int, 0444);
41 MODULE_PARM_DESC(sdw_md_flags, "SoundWire Intel Master device flags (0x0 all off)");
49 #define cdns_to_intel(_cdns) container_of(_cdns, struct sdw_intel, cdns)
52 * Read, write helpers for HW registers
54 static inline int intel_readl(void __iomem *base, int offset)
56 return readl(base + offset);
59 static inline void intel_writel(void __iomem *base, int offset, int value)
61 writel(value, base + offset);
64 static inline u16 intel_readw(void __iomem *base, int offset)
66 return readw(base + offset);
69 static inline void intel_writew(void __iomem *base, int offset, u16 value)
71 writew(value, base + offset);
74 static int intel_wait_bit(void __iomem *base, int offset, u32 mask, u32 target)
80 reg_read = readl(base + offset);
81 if ((reg_read & mask) == target)
85 usleep_range(50, 100);
86 } while (timeout != 0);
91 static int intel_clear_bit(void __iomem *base, int offset, u32 value, u32 mask)
93 writel(value, base + offset);
94 return intel_wait_bit(base, offset, mask, 0);
97 static int intel_set_bit(void __iomem *base, int offset, u32 value, u32 mask)
99 writel(value, base + offset);
100 return intel_wait_bit(base, offset, mask, mask);
106 #ifdef CONFIG_DEBUG_FS
108 #define RD_BUF (2 * PAGE_SIZE)
110 static ssize_t intel_sprintf(void __iomem *mem, bool l,
111 char *buf, size_t pos, unsigned int reg)
116 value = intel_readl(mem, reg);
118 value = intel_readw(mem, reg);
120 return scnprintf(buf + pos, RD_BUF - pos, "%4x\t%4x\n", reg, value);
123 static int intel_reg_show(struct seq_file *s_file, void *data)
125 struct sdw_intel *sdw = s_file->private;
126 void __iomem *s = sdw->link_res->shim;
127 void __iomem *a = sdw->link_res->alh;
131 unsigned int links, reg;
133 buf = kzalloc(RD_BUF, GFP_KERNEL);
137 links = intel_readl(s, SDW_SHIM_LCAP) & GENMASK(2, 0);
139 ret = scnprintf(buf, RD_BUF, "Register Value\n");
140 ret += scnprintf(buf + ret, RD_BUF - ret, "\nShim\n");
142 for (i = 0; i < links; i++) {
143 reg = SDW_SHIM_LCAP + i * 4;
144 ret += intel_sprintf(s, true, buf, ret, reg);
147 for (i = 0; i < links; i++) {
148 ret += scnprintf(buf + ret, RD_BUF - ret, "\nLink%d\n", i);
149 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLSCAP(i));
150 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS0CM(i));
151 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS1CM(i));
152 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS2CM(i));
153 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS3CM(i));
154 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PCMSCAP(i));
156 ret += scnprintf(buf + ret, RD_BUF - ret, "\n PCMSyCH registers\n");
159 * the value 10 is the number of PDIs. We will need a
160 * cleanup to remove hard-coded Intel configurations
161 * from cadence_master.c
163 for (j = 0; j < 10; j++) {
164 ret += intel_sprintf(s, false, buf, ret,
165 SDW_SHIM_PCMSYCHM(i, j));
166 ret += intel_sprintf(s, false, buf, ret,
167 SDW_SHIM_PCMSYCHC(i, j));
169 ret += scnprintf(buf + ret, RD_BUF - ret, "\n PDMSCAP, IOCTL, CTMCTL\n");
171 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PDMSCAP(i));
172 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_IOCTL(i));
173 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTMCTL(i));
176 ret += scnprintf(buf + ret, RD_BUF - ret, "\nWake registers\n");
177 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKEEN);
178 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKESTS);
180 ret += scnprintf(buf + ret, RD_BUF - ret, "\nALH STRMzCFG\n");
181 for (i = 0; i < SDW_ALH_NUM_STREAMS; i++)
182 ret += intel_sprintf(a, true, buf, ret, SDW_ALH_STRMZCFG(i));
184 seq_printf(s_file, "%s", buf);
189 DEFINE_SHOW_ATTRIBUTE(intel_reg);
191 static int intel_set_m_datamode(void *data, u64 value)
193 struct sdw_intel *sdw = data;
194 struct sdw_bus *bus = &sdw->cdns.bus;
196 if (value > SDW_PORT_DATA_MODE_STATIC_1)
199 /* Userspace changed the hardware state behind the kernel's back */
200 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
202 bus->params.m_data_mode = value;
206 DEFINE_DEBUGFS_ATTRIBUTE(intel_set_m_datamode_fops, NULL,
207 intel_set_m_datamode, "%llu\n");
209 static int intel_set_s_datamode(void *data, u64 value)
211 struct sdw_intel *sdw = data;
212 struct sdw_bus *bus = &sdw->cdns.bus;
214 if (value > SDW_PORT_DATA_MODE_STATIC_1)
217 /* Userspace changed the hardware state behind the kernel's back */
218 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
220 bus->params.s_data_mode = value;
224 DEFINE_DEBUGFS_ATTRIBUTE(intel_set_s_datamode_fops, NULL,
225 intel_set_s_datamode, "%llu\n");
227 static void intel_debugfs_init(struct sdw_intel *sdw)
229 struct dentry *root = sdw->cdns.bus.debugfs;
234 sdw->debugfs = debugfs_create_dir("intel-sdw", root);
236 debugfs_create_file("intel-registers", 0400, sdw->debugfs, sdw,
239 debugfs_create_file("intel-m-datamode", 0200, sdw->debugfs, sdw,
240 &intel_set_m_datamode_fops);
242 debugfs_create_file("intel-s-datamode", 0200, sdw->debugfs, sdw,
243 &intel_set_s_datamode_fops);
245 sdw_cdns_debugfs_init(&sdw->cdns, sdw->debugfs);
248 static void intel_debugfs_exit(struct sdw_intel *sdw)
250 debugfs_remove_recursive(sdw->debugfs);
253 static void intel_debugfs_init(struct sdw_intel *sdw) {}
254 static void intel_debugfs_exit(struct sdw_intel *sdw) {}
255 #endif /* CONFIG_DEBUG_FS */
261 static int intel_link_power_up(struct sdw_intel *sdw)
263 unsigned int link_id = sdw->instance;
264 void __iomem *shim = sdw->link_res->shim;
265 u32 *shim_mask = sdw->link_res->shim_mask;
266 struct sdw_bus *bus = &sdw->cdns.bus;
267 struct sdw_master_prop *prop = &bus->prop;
268 u32 spa_mask, cpa_mask;
274 mutex_lock(sdw->link_res->shim_lock);
277 * The hardware relies on an internal counter, typically 4kHz,
278 * to generate the SoundWire SSP - which defines a 'safe'
279 * synchronization point between commands and audio transport
280 * and allows for multi link synchronization. The SYNCPRD value
281 * is only dependent on the oscillator clock provided to
282 * the IP, so adjust based on _DSD properties reported in DSDT
283 * tables. The values reported are based on either 24MHz
284 * (CNL/CML) or 38.4 MHz (ICL/TGL+).
286 if (prop->mclk_freq % 6000000)
287 syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_38_4;
289 syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24;
292 dev_dbg(sdw->cdns.dev, "%s: powering up all links\n", __func__);
294 /* we first need to program the SyncPRD/CPU registers */
295 dev_dbg(sdw->cdns.dev,
296 "%s: first link up, programming SYNCPRD\n", __func__);
298 /* set SyncPRD period */
299 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
300 u32p_replace_bits(&sync_reg, syncprd, SDW_SHIM_SYNC_SYNCPRD);
302 /* Set SyncCPU bit */
303 sync_reg |= SDW_SHIM_SYNC_SYNCCPU;
304 intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
306 /* Link power up sequence */
307 link_control = intel_readl(shim, SDW_SHIM_LCTL);
309 /* only power-up enabled links */
310 spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, sdw->link_res->link_mask);
311 cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
313 link_control |= spa_mask;
315 ret = intel_set_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
317 dev_err(sdw->cdns.dev, "Failed to power up link: %d\n", ret);
321 /* SyncCPU will change once link is active */
322 ret = intel_wait_bit(shim, SDW_SHIM_SYNC,
323 SDW_SHIM_SYNC_SYNCCPU, 0);
325 dev_err(sdw->cdns.dev,
326 "Failed to set SHIM_SYNC: %d\n", ret);
331 *shim_mask |= BIT(link_id);
333 sdw->cdns.link_up = true;
335 mutex_unlock(sdw->link_res->shim_lock);
340 /* this needs to be called with shim_lock */
341 static void intel_shim_glue_to_master_ip(struct sdw_intel *sdw)
343 void __iomem *shim = sdw->link_res->shim;
344 unsigned int link_id = sdw->instance;
347 /* Switch to MIP from Glue logic */
348 ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
350 ioctl &= ~(SDW_SHIM_IOCTL_DOE);
351 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
352 usleep_range(10, 15);
354 ioctl &= ~(SDW_SHIM_IOCTL_DO);
355 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
356 usleep_range(10, 15);
358 ioctl |= (SDW_SHIM_IOCTL_MIF);
359 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
360 usleep_range(10, 15);
362 ioctl &= ~(SDW_SHIM_IOCTL_BKE);
363 ioctl &= ~(SDW_SHIM_IOCTL_COE);
364 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
365 usleep_range(10, 15);
367 /* at this point Master IP has full control of the I/Os */
370 /* this needs to be called with shim_lock */
371 static void intel_shim_master_ip_to_glue(struct sdw_intel *sdw)
373 unsigned int link_id = sdw->instance;
374 void __iomem *shim = sdw->link_res->shim;
378 ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
379 ioctl |= SDW_SHIM_IOCTL_BKE;
380 ioctl |= SDW_SHIM_IOCTL_COE;
381 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
382 usleep_range(10, 15);
384 ioctl &= ~(SDW_SHIM_IOCTL_MIF);
385 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
386 usleep_range(10, 15);
388 /* at this point Integration Glue has full control of the I/Os */
391 static int intel_shim_init(struct sdw_intel *sdw, bool clock_stop)
393 void __iomem *shim = sdw->link_res->shim;
394 unsigned int link_id = sdw->instance;
396 u16 ioctl = 0, act = 0;
398 mutex_lock(sdw->link_res->shim_lock);
400 /* Initialize Shim */
401 ioctl |= SDW_SHIM_IOCTL_BKE;
402 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
403 usleep_range(10, 15);
405 ioctl |= SDW_SHIM_IOCTL_WPDD;
406 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
407 usleep_range(10, 15);
409 ioctl |= SDW_SHIM_IOCTL_DO;
410 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
411 usleep_range(10, 15);
413 ioctl |= SDW_SHIM_IOCTL_DOE;
414 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
415 usleep_range(10, 15);
417 intel_shim_glue_to_master_ip(sdw);
419 u16p_replace_bits(&act, 0x1, SDW_SHIM_CTMCTL_DOAIS);
420 act |= SDW_SHIM_CTMCTL_DACTQE;
421 act |= SDW_SHIM_CTMCTL_DODS;
422 intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act);
423 usleep_range(10, 15);
425 mutex_unlock(sdw->link_res->shim_lock);
430 static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
432 void __iomem *shim = sdw->link_res->shim;
433 unsigned int link_id = sdw->instance;
434 u16 wake_en, wake_sts;
436 mutex_lock(sdw->link_res->shim_lock);
437 wake_en = intel_readw(shim, SDW_SHIM_WAKEEN);
440 /* Enable the wakeup */
441 wake_en |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
442 intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
444 /* Disable the wake up interrupt */
445 wake_en &= ~(SDW_SHIM_WAKEEN_ENABLE << link_id);
446 intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
448 /* Clear wake status */
449 wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
450 wake_sts |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
451 intel_writew(shim, SDW_SHIM_WAKESTS_STATUS, wake_sts);
453 mutex_unlock(sdw->link_res->shim_lock);
456 static int intel_link_power_down(struct sdw_intel *sdw)
458 u32 link_control, spa_mask, cpa_mask;
459 unsigned int link_id = sdw->instance;
460 void __iomem *shim = sdw->link_res->shim;
461 u32 *shim_mask = sdw->link_res->shim_mask;
464 mutex_lock(sdw->link_res->shim_lock);
466 intel_shim_master_ip_to_glue(sdw);
468 if (!(*shim_mask & BIT(link_id)))
469 dev_err(sdw->cdns.dev,
470 "%s: Unbalanced power-up/down calls\n", __func__);
472 *shim_mask &= ~BIT(link_id);
476 dev_dbg(sdw->cdns.dev, "%s: powering down all links\n", __func__);
478 /* Link power down sequence */
479 link_control = intel_readl(shim, SDW_SHIM_LCTL);
481 /* only power-down enabled links */
482 spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, ~sdw->link_res->link_mask);
483 cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
485 link_control &= spa_mask;
487 ret = intel_clear_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
490 mutex_unlock(sdw->link_res->shim_lock);
493 dev_err(sdw->cdns.dev, "%s: could not power down link\n", __func__);
498 sdw->cdns.link_up = false;
502 static void intel_shim_sync_arm(struct sdw_intel *sdw)
504 void __iomem *shim = sdw->link_res->shim;
507 mutex_lock(sdw->link_res->shim_lock);
509 /* update SYNC register */
510 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
511 sync_reg |= (SDW_SHIM_SYNC_CMDSYNC << sdw->instance);
512 intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
514 mutex_unlock(sdw->link_res->shim_lock);
517 static int intel_shim_sync_go_unlocked(struct sdw_intel *sdw)
519 void __iomem *shim = sdw->link_res->shim;
523 /* Read SYNC register */
524 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
527 * Set SyncGO bit to synchronously trigger a bank switch for
528 * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all
531 sync_reg |= SDW_SHIM_SYNC_SYNCGO;
533 ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg,
534 SDW_SHIM_SYNC_SYNCGO);
537 dev_err(sdw->cdns.dev, "SyncGO clear failed: %d\n", ret);
542 static int intel_shim_sync_go(struct sdw_intel *sdw)
546 mutex_lock(sdw->link_res->shim_lock);
548 ret = intel_shim_sync_go_unlocked(sdw);
550 mutex_unlock(sdw->link_res->shim_lock);
558 static void intel_pdi_init(struct sdw_intel *sdw,
559 struct sdw_cdns_stream_config *config)
561 void __iomem *shim = sdw->link_res->shim;
562 unsigned int link_id = sdw->instance;
563 int pcm_cap, pdm_cap;
565 /* PCM Stream Capability */
566 pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id));
568 config->pcm_bd = FIELD_GET(SDW_SHIM_PCMSCAP_BSS, pcm_cap);
569 config->pcm_in = FIELD_GET(SDW_SHIM_PCMSCAP_ISS, pcm_cap);
570 config->pcm_out = FIELD_GET(SDW_SHIM_PCMSCAP_OSS, pcm_cap);
572 dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
573 config->pcm_bd, config->pcm_in, config->pcm_out);
575 /* PDM Stream Capability */
576 pdm_cap = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
578 config->pdm_bd = FIELD_GET(SDW_SHIM_PDMSCAP_BSS, pdm_cap);
579 config->pdm_in = FIELD_GET(SDW_SHIM_PDMSCAP_ISS, pdm_cap);
580 config->pdm_out = FIELD_GET(SDW_SHIM_PDMSCAP_OSS, pdm_cap);
582 dev_dbg(sdw->cdns.dev, "PDM cap bd:%d in:%d out:%d\n",
583 config->pdm_bd, config->pdm_in, config->pdm_out);
587 intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num, bool pcm)
589 void __iomem *shim = sdw->link_res->shim;
590 unsigned int link_id = sdw->instance;
594 count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num));
597 * WORKAROUND: on all existing Intel controllers, pdi
598 * number 2 reports channel count as 1 even though it
599 * supports 8 channels. Performing hardcoding for pdi
606 count = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
607 count = FIELD_GET(SDW_SHIM_PDMSCAP_CPSS, count);
610 /* zero based values for channel count in register */
616 static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
617 struct sdw_cdns_pdi *pdi,
618 unsigned int num_pdi,
619 unsigned int *num_ch, bool pcm)
623 for (i = 0; i < num_pdi; i++) {
624 pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num, pcm);
625 ch_count += pdi->ch_count;
633 static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
634 struct sdw_cdns_streams *stream, bool pcm)
636 intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
637 &stream->num_ch_bd, pcm);
639 intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
640 &stream->num_ch_in, pcm);
642 intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
643 &stream->num_ch_out, pcm);
648 static int intel_pdi_ch_update(struct sdw_intel *sdw)
650 /* First update PCM streams followed by PDM streams */
651 intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm, true);
652 intel_pdi_stream_ch_update(sdw, &sdw->cdns.pdm, false);
658 intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
660 void __iomem *shim = sdw->link_res->shim;
661 unsigned int link_id = sdw->instance;
664 /* the Bulk and PCM streams are not contiguous */
665 pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
667 pdi->intel_alh_id += 2;
670 * Program stream parameters to stream SHIM register
671 * This is applicable for PCM stream only.
673 if (pdi->type != SDW_STREAM_PCM)
676 if (pdi->dir == SDW_DATA_DIR_RX)
677 pdi_conf |= SDW_SHIM_PCMSYCM_DIR;
679 pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR);
681 u32p_replace_bits(&pdi_conf, pdi->intel_alh_id, SDW_SHIM_PCMSYCM_STREAM);
682 u32p_replace_bits(&pdi_conf, pdi->l_ch_num, SDW_SHIM_PCMSYCM_LCHN);
683 u32p_replace_bits(&pdi_conf, pdi->h_ch_num, SDW_SHIM_PCMSYCM_HCHN);
685 intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
689 intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
691 void __iomem *alh = sdw->link_res->alh;
692 unsigned int link_id = sdw->instance;
695 /* the Bulk and PCM streams are not contiguous */
696 pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
698 pdi->intel_alh_id += 2;
700 /* Program Stream config ALH register */
701 conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
703 u32p_replace_bits(&conf, SDW_ALH_STRMZCFG_DMAT_VAL, SDW_ALH_STRMZCFG_DMAT);
704 u32p_replace_bits(&conf, pdi->ch_count - 1, SDW_ALH_STRMZCFG_CHN);
706 intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
709 static int intel_params_stream(struct sdw_intel *sdw,
710 struct snd_pcm_substream *substream,
711 struct snd_soc_dai *dai,
712 struct snd_pcm_hw_params *hw_params,
713 int link_id, int alh_stream_id)
715 struct sdw_intel_link_res *res = sdw->link_res;
716 struct sdw_intel_stream_params_data params_data;
718 params_data.substream = substream;
719 params_data.dai = dai;
720 params_data.hw_params = hw_params;
721 params_data.link_id = link_id;
722 params_data.alh_stream_id = alh_stream_id;
724 if (res->ops && res->ops->params_stream && res->dev)
725 return res->ops->params_stream(res->dev,
730 static int intel_free_stream(struct sdw_intel *sdw,
731 struct snd_pcm_substream *substream,
732 struct snd_soc_dai *dai,
735 struct sdw_intel_link_res *res = sdw->link_res;
736 struct sdw_intel_stream_free_data free_data;
738 free_data.substream = substream;
740 free_data.link_id = link_id;
742 if (res->ops && res->ops->free_stream && res->dev)
743 return res->ops->free_stream(res->dev,
750 * bank switch routines
753 static int intel_pre_bank_switch(struct sdw_bus *bus)
755 struct sdw_cdns *cdns = bus_to_cdns(bus);
756 struct sdw_intel *sdw = cdns_to_intel(cdns);
758 /* Write to register only for multi-link */
759 if (!bus->multi_link)
762 intel_shim_sync_arm(sdw);
767 static int intel_post_bank_switch(struct sdw_bus *bus)
769 struct sdw_cdns *cdns = bus_to_cdns(bus);
770 struct sdw_intel *sdw = cdns_to_intel(cdns);
771 void __iomem *shim = sdw->link_res->shim;
774 /* Write to register only for multi-link */
775 if (!bus->multi_link)
778 mutex_lock(sdw->link_res->shim_lock);
780 /* Read SYNC register */
781 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
784 * post_bank_switch() ops is called from the bus in loop for
785 * all the Masters in the steam with the expectation that
786 * we trigger the bankswitch for the only first Master in the list
787 * and do nothing for the other Masters
789 * So, set the SYNCGO bit only if CMDSYNC bit is set for any Master.
791 if (!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK)) {
796 ret = intel_shim_sync_go_unlocked(sdw);
798 mutex_unlock(sdw->link_res->shim_lock);
801 dev_err(sdw->cdns.dev, "Post bank switch failed: %d\n", ret);
810 static int intel_startup(struct snd_pcm_substream *substream,
811 struct snd_soc_dai *dai)
813 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
816 ret = pm_runtime_get_sync(cdns->dev);
817 if (ret < 0 && ret != -EACCES) {
818 dev_err_ratelimited(cdns->dev,
819 "pm_runtime_get_sync failed in %s, ret %d\n",
821 pm_runtime_put_noidle(cdns->dev);
827 static int intel_hw_params(struct snd_pcm_substream *substream,
828 struct snd_pcm_hw_params *params,
829 struct snd_soc_dai *dai)
831 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
832 struct sdw_intel *sdw = cdns_to_intel(cdns);
833 struct sdw_cdns_dma_data *dma;
834 struct sdw_cdns_pdi *pdi;
835 struct sdw_stream_config sconfig;
836 struct sdw_port_config *pconfig;
841 dma = snd_soc_dai_get_dma_data(dai, substream);
845 ch = params_channels(params);
846 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
847 dir = SDW_DATA_DIR_RX;
849 dir = SDW_DATA_DIR_TX;
851 if (dma->stream_type == SDW_STREAM_PDM)
855 pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
857 pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pdm, ch, dir, dai->id);
864 /* do run-time configurations for SHIM, ALH and PDI/PORT */
865 intel_pdi_shim_configure(sdw, pdi);
866 intel_pdi_alh_configure(sdw, pdi);
867 sdw_cdns_config_stream(cdns, ch, dir, pdi);
869 /* store pdi and hw_params, may be needed in prepare step */
870 dma->suspended = false;
872 dma->hw_params = params;
874 /* Inform DSP about PDI stream number */
875 ret = intel_params_stream(sdw, substream, dai, params,
881 sconfig.direction = dir;
882 sconfig.ch_count = ch;
883 sconfig.frame_rate = params_rate(params);
884 sconfig.type = dma->stream_type;
886 if (dma->stream_type == SDW_STREAM_PDM) {
887 sconfig.frame_rate *= 50;
890 sconfig.bps = snd_pcm_format_width(params_format(params));
893 /* Port configuration */
894 pconfig = kzalloc(sizeof(*pconfig), GFP_KERNEL);
900 pconfig->num = pdi->num;
901 pconfig->ch_mask = (1 << ch) - 1;
903 ret = sdw_stream_add_master(&cdns->bus, &sconfig,
904 pconfig, 1, dma->stream);
906 dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
913 static int intel_prepare(struct snd_pcm_substream *substream,
914 struct snd_soc_dai *dai)
916 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
917 struct sdw_intel *sdw = cdns_to_intel(cdns);
918 struct sdw_cdns_dma_data *dma;
922 dma = snd_soc_dai_get_dma_data(dai, substream);
924 dev_err(dai->dev, "failed to get dma data in %s\n",
929 if (dma->suspended) {
930 dma->suspended = false;
933 * .prepare() is called after system resume, where we
934 * need to reinitialize the SHIM/ALH/Cadence IP.
935 * .prepare() is also called to deal with underflows,
936 * but in those cases we cannot touch ALH/SHIM
940 /* configure stream */
941 ch = params_channels(dma->hw_params);
942 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
943 dir = SDW_DATA_DIR_RX;
945 dir = SDW_DATA_DIR_TX;
947 intel_pdi_shim_configure(sdw, dma->pdi);
948 intel_pdi_alh_configure(sdw, dma->pdi);
949 sdw_cdns_config_stream(cdns, ch, dir, dma->pdi);
951 /* Inform DSP about PDI stream number */
952 ret = intel_params_stream(sdw, substream, dai,
955 dma->pdi->intel_alh_id);
962 intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
964 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
965 struct sdw_intel *sdw = cdns_to_intel(cdns);
966 struct sdw_cdns_dma_data *dma;
969 dma = snd_soc_dai_get_dma_data(dai, substream);
974 * The sdw stream state will transition to RELEASED when stream->
975 * master_list is empty. So the stream state will transition to
976 * DEPREPARED for the first cpu-dai and to RELEASED for the last
979 ret = sdw_stream_remove_master(&cdns->bus, dma->stream);
981 dev_err(dai->dev, "remove master from stream %s failed: %d\n",
982 dma->stream->name, ret);
986 ret = intel_free_stream(sdw, substream, dai, sdw->instance);
988 dev_err(dai->dev, "intel_free_stream: failed %d\n", ret);
992 dma->hw_params = NULL;
998 static void intel_shutdown(struct snd_pcm_substream *substream,
999 struct snd_soc_dai *dai)
1001 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1003 pm_runtime_mark_last_busy(cdns->dev);
1004 pm_runtime_put_autosuspend(cdns->dev);
1007 static int intel_component_dais_suspend(struct snd_soc_component *component)
1009 struct sdw_cdns_dma_data *dma;
1010 struct snd_soc_dai *dai;
1012 for_each_component_dais(component, dai) {
1014 * we don't have a .suspend dai_ops, and we don't have access
1015 * to the substream, so let's mark both capture and playback
1016 * DMA contexts as suspended
1018 dma = dai->playback_dma_data;
1020 dma->suspended = true;
1022 dma = dai->capture_dma_data;
1024 dma->suspended = true;
1030 static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
1031 void *stream, int direction)
1033 return cdns_set_sdw_stream(dai, stream, true, direction);
1036 static int intel_pdm_set_sdw_stream(struct snd_soc_dai *dai,
1037 void *stream, int direction)
1039 return cdns_set_sdw_stream(dai, stream, false, direction);
1042 static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
1045 struct sdw_cdns_dma_data *dma;
1047 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1048 dma = dai->playback_dma_data;
1050 dma = dai->capture_dma_data;
1053 return ERR_PTR(-EINVAL);
1058 static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
1059 .startup = intel_startup,
1060 .hw_params = intel_hw_params,
1061 .prepare = intel_prepare,
1062 .hw_free = intel_hw_free,
1063 .shutdown = intel_shutdown,
1064 .set_sdw_stream = intel_pcm_set_sdw_stream,
1065 .get_sdw_stream = intel_get_sdw_stream,
1068 static const struct snd_soc_dai_ops intel_pdm_dai_ops = {
1069 .startup = intel_startup,
1070 .hw_params = intel_hw_params,
1071 .prepare = intel_prepare,
1072 .hw_free = intel_hw_free,
1073 .shutdown = intel_shutdown,
1074 .set_sdw_stream = intel_pdm_set_sdw_stream,
1075 .get_sdw_stream = intel_get_sdw_stream,
1078 static const struct snd_soc_component_driver dai_component = {
1079 .name = "soundwire",
1080 .suspend = intel_component_dais_suspend
1083 static int intel_create_dai(struct sdw_cdns *cdns,
1084 struct snd_soc_dai_driver *dais,
1085 enum intel_pdi_type type,
1086 u32 num, u32 off, u32 max_ch, bool pcm)
1093 /* TODO: Read supported rates/formats from hardware */
1094 for (i = off; i < (off + num); i++) {
1095 dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
1101 if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
1102 dais[i].playback.channels_min = 1;
1103 dais[i].playback.channels_max = max_ch;
1104 dais[i].playback.rates = SNDRV_PCM_RATE_48000;
1105 dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
1108 if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
1109 dais[i].capture.channels_min = 1;
1110 dais[i].capture.channels_max = max_ch;
1111 dais[i].capture.rates = SNDRV_PCM_RATE_48000;
1112 dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
1116 dais[i].ops = &intel_pcm_dai_ops;
1118 dais[i].ops = &intel_pdm_dai_ops;
1124 static int intel_register_dai(struct sdw_intel *sdw)
1126 struct sdw_cdns *cdns = &sdw->cdns;
1127 struct sdw_cdns_streams *stream;
1128 struct snd_soc_dai_driver *dais;
1129 int num_dai, ret, off = 0;
1131 /* DAIs are created based on total number of PDIs supported */
1132 num_dai = cdns->pcm.num_pdi + cdns->pdm.num_pdi;
1134 dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
1138 /* Create PCM DAIs */
1139 stream = &cdns->pcm;
1141 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
1142 off, stream->num_ch_in, true);
1146 off += cdns->pcm.num_in;
1147 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
1148 off, stream->num_ch_out, true);
1152 off += cdns->pcm.num_out;
1153 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
1154 off, stream->num_ch_bd, true);
1158 /* Create PDM DAIs */
1159 stream = &cdns->pdm;
1160 off += cdns->pcm.num_bd;
1161 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pdm.num_in,
1162 off, stream->num_ch_in, false);
1166 off += cdns->pdm.num_in;
1167 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pdm.num_out,
1168 off, stream->num_ch_out, false);
1172 off += cdns->pdm.num_out;
1173 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pdm.num_bd,
1174 off, stream->num_ch_bd, false);
1178 return snd_soc_register_component(cdns->dev, &dai_component,
1182 static int sdw_master_read_intel_prop(struct sdw_bus *bus)
1184 struct sdw_master_prop *prop = &bus->prop;
1185 struct fwnode_handle *link;
1189 /* Find master handle */
1190 snprintf(name, sizeof(name),
1191 "mipi-sdw-link-%d-subproperties", bus->link_id);
1193 link = device_get_named_child_node(bus->dev, name);
1195 dev_err(bus->dev, "Master node %s not found\n", name);
1199 fwnode_property_read_u32(link,
1200 "intel-sdw-ip-clock",
1203 /* the values reported by BIOS are the 2x clock, not the bus clock */
1204 prop->mclk_freq /= 2;
1206 fwnode_property_read_u32(link,
1210 if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
1211 prop->hw_disabled = true;
1213 prop->quirks = SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH |
1214 SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY;
1219 static int intel_prop_read(struct sdw_bus *bus)
1221 /* Initialize with default handler to read all DisCo properties */
1222 sdw_master_read_prop(bus);
1224 /* read Intel-specific properties */
1225 sdw_master_read_intel_prop(bus);
1230 static struct sdw_master_ops sdw_intel_ops = {
1231 .read_prop = sdw_master_read_prop,
1232 .override_adr = sdw_dmi_override_adr,
1233 .xfer_msg = cdns_xfer_msg,
1234 .xfer_msg_defer = cdns_xfer_msg_defer,
1235 .reset_page_addr = cdns_reset_page_addr,
1236 .set_bus_conf = cdns_bus_conf,
1237 .pre_bank_switch = intel_pre_bank_switch,
1238 .post_bank_switch = intel_post_bank_switch,
1241 static int intel_init(struct sdw_intel *sdw)
1245 /* Initialize shim and controller */
1246 intel_link_power_up(sdw);
1248 clock_stop = sdw_cdns_is_clock_stop(&sdw->cdns);
1250 intel_shim_init(sdw, clock_stop);
1256 * probe and init (aux_dev_id argument is required by function prototype but not used)
1258 static int intel_link_probe(struct auxiliary_device *auxdev,
1259 const struct auxiliary_device_id *aux_dev_id)
1262 struct device *dev = &auxdev->dev;
1263 struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
1264 struct sdw_intel *sdw;
1265 struct sdw_cdns *cdns;
1266 struct sdw_bus *bus;
1269 sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
1276 sdw->instance = auxdev->id;
1277 sdw->link_res = &ldev->link_res;
1279 cdns->registers = sdw->link_res->registers;
1280 cdns->instance = sdw->instance;
1281 cdns->msg_count = 0;
1283 bus->link_id = auxdev->id;
1285 sdw_cdns_probe(cdns);
1287 /* Set property read ops */
1288 sdw_intel_ops.read_prop = intel_prop_read;
1289 bus->ops = &sdw_intel_ops;
1291 /* set driver data, accessed by snd_soc_dai_get_drvdata() */
1292 dev_set_drvdata(dev, cdns);
1294 /* use generic bandwidth allocation algorithm */
1295 sdw->cdns.bus.compute_params = sdw_compute_params;
1297 ret = sdw_bus_master_add(bus, dev, dev->fwnode);
1299 dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
1303 if (bus->prop.hw_disabled)
1305 "SoundWire master %d is disabled, will be ignored\n",
1308 * Ignore BIOS err_threshold, it's a really bad idea when dealing
1309 * with multiple hardware synchronized links
1311 bus->prop.err_threshold = 0;
1316 int intel_link_startup(struct auxiliary_device *auxdev)
1318 struct sdw_cdns_stream_config config;
1319 struct device *dev = &auxdev->dev;
1320 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1321 struct sdw_intel *sdw = cdns_to_intel(cdns);
1322 struct sdw_bus *bus = &cdns->bus;
1325 u32 clock_stop_quirks;
1328 if (bus->prop.hw_disabled) {
1330 "SoundWire master %d is disabled, ignoring\n",
1335 link_flags = md_flags >> (bus->link_id * 8);
1336 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1338 dev_dbg(dev, "Multi-link is disabled\n");
1339 bus->multi_link = false;
1342 * hardware-based synchronization is required regardless
1343 * of the number of segments used by a stream: SSP-based
1344 * synchronization is gated by gsync when the multi-master
1347 bus->multi_link = true;
1348 bus->hw_sync_min_links = 1;
1351 /* Initialize shim, controller */
1352 ret = intel_init(sdw);
1356 /* Read the PDI config and initialize cadence PDI */
1357 intel_pdi_init(sdw, &config);
1358 ret = sdw_cdns_pdi_init(cdns, config);
1362 intel_pdi_ch_update(sdw);
1364 ret = sdw_cdns_enable_interrupt(cdns, true);
1366 dev_err(dev, "cannot enable interrupts\n");
1371 * follow recommended programming flows to avoid timeouts when
1375 intel_shim_sync_arm(sdw);
1377 ret = sdw_cdns_init(cdns);
1379 dev_err(dev, "unable to initialize Cadence IP\n");
1383 ret = sdw_cdns_exit_reset(cdns);
1385 dev_err(dev, "unable to exit bus reset sequence\n");
1390 ret = intel_shim_sync_go(sdw);
1392 dev_err(dev, "sync go failed: %d\n", ret);
1398 ret = intel_register_dai(sdw);
1400 dev_err(dev, "DAI registration failed: %d\n", ret);
1401 snd_soc_unregister_component(dev);
1405 intel_debugfs_init(sdw);
1407 /* Enable runtime PM */
1408 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
1409 pm_runtime_set_autosuspend_delay(dev,
1410 INTEL_MASTER_SUSPEND_DELAY_MS);
1411 pm_runtime_use_autosuspend(dev);
1412 pm_runtime_mark_last_busy(dev);
1414 pm_runtime_set_active(dev);
1415 pm_runtime_enable(dev);
1418 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1419 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
1421 * To keep the clock running we need to prevent
1422 * pm_runtime suspend from happening by increasing the
1424 * This quirk is specified by the parent PCI device in
1425 * case of specific latency requirements. It will have
1426 * no effect if pm_runtime is disabled by the user via
1427 * a module parameter for testing purposes.
1429 pm_runtime_get_noresume(dev);
1433 * The runtime PM status of Slave devices is "Unsupported"
1434 * until they report as ATTACHED. If they don't, e.g. because
1435 * there are no Slave devices populated or if the power-on is
1436 * delayed or dependent on a power switch, the Master will
1437 * remain active and prevent its parent from suspending.
1439 * Conditionally force the pm_runtime core to re-evaluate the
1440 * Master status in the absence of any Slave activity. A quirk
1441 * is provided to e.g. deal with Slaves that may be powered on
1442 * with a delay. A more complete solution would require the
1443 * definition of Master properties.
1445 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
1446 pm_runtime_idle(dev);
1451 sdw_cdns_enable_interrupt(cdns, false);
1456 static void intel_link_remove(struct auxiliary_device *auxdev)
1458 struct device *dev = &auxdev->dev;
1459 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1460 struct sdw_intel *sdw = cdns_to_intel(cdns);
1461 struct sdw_bus *bus = &cdns->bus;
1464 * Since pm_runtime is already disabled, we don't decrease
1465 * the refcount when the clock_stop_quirk is
1466 * SDW_INTEL_CLK_STOP_NOT_ALLOWED
1468 if (!bus->prop.hw_disabled) {
1469 intel_debugfs_exit(sdw);
1470 sdw_cdns_enable_interrupt(cdns, false);
1471 snd_soc_unregister_component(dev);
1473 sdw_bus_master_delete(bus);
1476 int intel_link_process_wakeen_event(struct auxiliary_device *auxdev)
1478 struct device *dev = &auxdev->dev;
1479 struct sdw_intel *sdw;
1480 struct sdw_bus *bus;
1484 sdw = dev_get_drvdata(dev);
1485 bus = &sdw->cdns.bus;
1487 if (bus->prop.hw_disabled) {
1488 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n", bus->link_id);
1492 shim = sdw->link_res->shim;
1493 wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
1495 if (!(wake_sts & BIT(sdw->instance)))
1498 /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
1499 intel_shim_wake(sdw, false);
1502 * resume the Master, which will generate a bus reset and result in
1503 * Slaves re-attaching and be re-enumerated. The SoundWire physical
1504 * device which generated the wake will trigger an interrupt, which
1505 * will in turn cause the corresponding Linux Slave device to be
1506 * resumed and the Slave codec driver to check the status.
1508 pm_request_resume(dev);
1517 static int __maybe_unused intel_suspend(struct device *dev)
1519 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1520 struct sdw_intel *sdw = cdns_to_intel(cdns);
1521 struct sdw_bus *bus = &cdns->bus;
1522 u32 clock_stop_quirks;
1525 if (bus->prop.hw_disabled) {
1526 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n",
1531 if (pm_runtime_suspended(dev)) {
1532 dev_dbg(dev, "%s: pm_runtime status: suspended\n", __func__);
1534 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1536 if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET ||
1537 !clock_stop_quirks) &&
1538 !pm_runtime_suspended(dev->parent)) {
1541 * if we've enabled clock stop, and the parent
1542 * is still active, disable shim wake. The
1543 * SHIM registers are not accessible if the
1544 * parent is already pm_runtime suspended so
1545 * it's too late to change that configuration
1548 intel_shim_wake(sdw, false);
1554 ret = sdw_cdns_enable_interrupt(cdns, false);
1556 dev_err(dev, "cannot disable interrupts on suspend\n");
1560 ret = intel_link_power_down(sdw);
1562 dev_err(dev, "Link power down failed: %d\n", ret);
1566 intel_shim_wake(sdw, false);
1571 static int __maybe_unused intel_suspend_runtime(struct device *dev)
1573 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1574 struct sdw_intel *sdw = cdns_to_intel(cdns);
1575 struct sdw_bus *bus = &cdns->bus;
1576 u32 clock_stop_quirks;
1579 if (bus->prop.hw_disabled) {
1580 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n",
1585 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1587 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
1589 ret = sdw_cdns_enable_interrupt(cdns, false);
1591 dev_err(dev, "cannot disable interrupts on suspend\n");
1595 ret = intel_link_power_down(sdw);
1597 dev_err(dev, "Link power down failed: %d\n", ret);
1601 intel_shim_wake(sdw, false);
1603 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET ||
1604 !clock_stop_quirks) {
1605 bool wake_enable = true;
1607 ret = sdw_cdns_clock_stop(cdns, true);
1609 dev_err(dev, "cannot enable clock stop on suspend\n");
1610 wake_enable = false;
1613 ret = sdw_cdns_enable_interrupt(cdns, false);
1615 dev_err(dev, "cannot disable interrupts on suspend\n");
1619 ret = intel_link_power_down(sdw);
1621 dev_err(dev, "Link power down failed: %d\n", ret);
1625 intel_shim_wake(sdw, wake_enable);
1627 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
1628 __func__, clock_stop_quirks);
1635 static int __maybe_unused intel_resume(struct device *dev)
1637 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1638 struct sdw_intel *sdw = cdns_to_intel(cdns);
1639 struct sdw_bus *bus = &cdns->bus;
1644 if (bus->prop.hw_disabled) {
1645 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n",
1650 link_flags = md_flags >> (bus->link_id * 8);
1651 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1653 if (pm_runtime_suspended(dev)) {
1654 dev_dbg(dev, "%s: pm_runtime status was suspended, forcing active\n", __func__);
1656 /* follow required sequence from runtime_pm.rst */
1657 pm_runtime_disable(dev);
1658 pm_runtime_set_active(dev);
1659 pm_runtime_mark_last_busy(dev);
1660 pm_runtime_enable(dev);
1662 link_flags = md_flags >> (bus->link_id * 8);
1664 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
1665 pm_runtime_idle(dev);
1668 ret = intel_init(sdw);
1670 dev_err(dev, "%s failed: %d\n", __func__, ret);
1675 * make sure all Slaves are tagged as UNATTACHED and provide
1676 * reason for reinitialization
1678 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
1680 ret = sdw_cdns_enable_interrupt(cdns, true);
1682 dev_err(dev, "cannot enable interrupts during resume\n");
1687 * follow recommended programming flows to avoid timeouts when
1691 intel_shim_sync_arm(sdw);
1693 ret = sdw_cdns_init(&sdw->cdns);
1695 dev_err(dev, "unable to initialize Cadence IP during resume\n");
1699 ret = sdw_cdns_exit_reset(cdns);
1701 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1706 ret = intel_shim_sync_go(sdw);
1708 dev_err(dev, "sync go failed during resume\n");
1714 * after system resume, the pm_runtime suspend() may kick in
1715 * during the enumeration, before any children device force the
1716 * master device to remain active. Using pm_runtime_get()
1717 * routines is not really possible, since it'd prevent the
1718 * master from suspending.
1719 * A reasonable compromise is to update the pm_runtime
1720 * counters and delay the pm_runtime suspend by several
1721 * seconds, by when all enumeration should be complete.
1723 pm_runtime_mark_last_busy(dev);
1728 static int __maybe_unused intel_resume_runtime(struct device *dev)
1730 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1731 struct sdw_intel *sdw = cdns_to_intel(cdns);
1732 struct sdw_bus *bus = &cdns->bus;
1733 u32 clock_stop_quirks;
1740 if (bus->prop.hw_disabled) {
1741 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n",
1746 link_flags = md_flags >> (bus->link_id * 8);
1747 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1749 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1751 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
1752 ret = intel_init(sdw);
1754 dev_err(dev, "%s failed: %d\n", __func__, ret);
1759 * make sure all Slaves are tagged as UNATTACHED and provide
1760 * reason for reinitialization
1762 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
1764 ret = sdw_cdns_enable_interrupt(cdns, true);
1766 dev_err(dev, "cannot enable interrupts during resume\n");
1771 * follow recommended programming flows to avoid
1772 * timeouts when gsync is enabled
1775 intel_shim_sync_arm(sdw);
1777 ret = sdw_cdns_init(&sdw->cdns);
1779 dev_err(dev, "unable to initialize Cadence IP during resume\n");
1783 ret = sdw_cdns_exit_reset(cdns);
1785 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1790 ret = intel_shim_sync_go(sdw);
1792 dev_err(dev, "sync go failed during resume\n");
1796 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
1797 ret = intel_init(sdw);
1799 dev_err(dev, "%s failed: %d\n", __func__, ret);
1804 * An exception condition occurs for the CLK_STOP_BUS_RESET
1805 * case if one or more masters remain active. In this condition,
1806 * all the masters are powered on for they are in the same power
1807 * domain. Master can preserve its context for clock stop0, so
1808 * there is no need to clear slave status and reset bus.
1810 clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
1815 * make sure all Slaves are tagged as UNATTACHED and
1816 * provide reason for reinitialization
1819 status = SDW_UNATTACH_REQUEST_MASTER_RESET;
1820 sdw_clear_slave_status(bus, status);
1822 ret = sdw_cdns_enable_interrupt(cdns, true);
1824 dev_err(dev, "cannot enable interrupts during resume\n");
1829 * follow recommended programming flows to avoid
1830 * timeouts when gsync is enabled
1833 intel_shim_sync_arm(sdw);
1836 * Re-initialize the IP since it was powered-off
1838 sdw_cdns_init(&sdw->cdns);
1841 ret = sdw_cdns_enable_interrupt(cdns, true);
1843 dev_err(dev, "cannot enable interrupts during resume\n");
1848 ret = sdw_cdns_clock_restart(cdns, !clock_stop0);
1850 dev_err(dev, "unable to restart clock during resume\n");
1855 ret = sdw_cdns_exit_reset(cdns);
1857 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1862 ret = intel_shim_sync_go(sdw);
1864 dev_err(sdw->cdns.dev, "sync go failed during resume\n");
1869 } else if (!clock_stop_quirks) {
1871 clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
1873 dev_err(dev, "%s invalid configuration, clock was not stopped", __func__);
1875 ret = intel_init(sdw);
1877 dev_err(dev, "%s failed: %d\n", __func__, ret);
1881 ret = sdw_cdns_enable_interrupt(cdns, true);
1883 dev_err(dev, "cannot enable interrupts during resume\n");
1887 ret = sdw_cdns_clock_restart(cdns, false);
1889 dev_err(dev, "unable to resume master during resume\n");
1893 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
1894 __func__, clock_stop_quirks);
1901 static const struct dev_pm_ops intel_pm = {
1902 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
1903 SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
1906 static const struct auxiliary_device_id intel_link_id_table[] = {
1907 { .name = "soundwire_intel.link" },
1910 MODULE_DEVICE_TABLE(auxiliary, intel_link_id_table);
1912 static struct auxiliary_driver sdw_intel_drv = {
1913 .probe = intel_link_probe,
1914 .remove = intel_link_remove,
1916 /* auxiliary_driver_register() sets .name to be the modname */
1919 .id_table = intel_link_id_table
1921 module_auxiliary_driver(sdw_intel_drv);
1923 MODULE_LICENSE("Dual BSD/GPL");
1924 MODULE_DESCRIPTION("Intel Soundwire Link Driver");