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/platform_device.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 /* Intel SHIM Registers Definition */
26 #define SDW_SHIM_LCAP 0x0
27 #define SDW_SHIM_LCTL 0x4
28 #define SDW_SHIM_IPPTR 0x8
29 #define SDW_SHIM_SYNC 0xC
31 #define SDW_SHIM_CTLSCAP(x) (0x010 + 0x60 * (x))
32 #define SDW_SHIM_CTLS0CM(x) (0x012 + 0x60 * (x))
33 #define SDW_SHIM_CTLS1CM(x) (0x014 + 0x60 * (x))
34 #define SDW_SHIM_CTLS2CM(x) (0x016 + 0x60 * (x))
35 #define SDW_SHIM_CTLS3CM(x) (0x018 + 0x60 * (x))
36 #define SDW_SHIM_PCMSCAP(x) (0x020 + 0x60 * (x))
38 #define SDW_SHIM_PCMSYCHM(x, y) (0x022 + (0x60 * (x)) + (0x2 * (y)))
39 #define SDW_SHIM_PCMSYCHC(x, y) (0x042 + (0x60 * (x)) + (0x2 * (y)))
40 #define SDW_SHIM_PDMSCAP(x) (0x062 + 0x60 * (x))
41 #define SDW_SHIM_IOCTL(x) (0x06C + 0x60 * (x))
42 #define SDW_SHIM_CTMCTL(x) (0x06E + 0x60 * (x))
44 #define SDW_SHIM_WAKEEN 0x190
45 #define SDW_SHIM_WAKESTS 0x192
47 #define SDW_SHIM_LCTL_SPA BIT(0)
48 #define SDW_SHIM_LCTL_CPA BIT(8)
50 #define SDW_SHIM_SYNC_SYNCPRD_VAL_24 (24000 / SDW_CADENCE_GSYNC_KHZ - 1)
51 #define SDW_SHIM_SYNC_SYNCPRD_VAL_38_4 (38400 / SDW_CADENCE_GSYNC_KHZ - 1)
52 #define SDW_SHIM_SYNC_SYNCPRD GENMASK(14, 0)
53 #define SDW_SHIM_SYNC_SYNCCPU BIT(15)
54 #define SDW_SHIM_SYNC_CMDSYNC_MASK GENMASK(19, 16)
55 #define SDW_SHIM_SYNC_CMDSYNC BIT(16)
56 #define SDW_SHIM_SYNC_SYNCGO BIT(24)
58 #define SDW_SHIM_PCMSCAP_ISS GENMASK(3, 0)
59 #define SDW_SHIM_PCMSCAP_OSS GENMASK(7, 4)
60 #define SDW_SHIM_PCMSCAP_BSS GENMASK(12, 8)
62 #define SDW_SHIM_PCMSYCM_LCHN GENMASK(3, 0)
63 #define SDW_SHIM_PCMSYCM_HCHN GENMASK(7, 4)
64 #define SDW_SHIM_PCMSYCM_STREAM GENMASK(13, 8)
65 #define SDW_SHIM_PCMSYCM_DIR BIT(15)
67 #define SDW_SHIM_PDMSCAP_ISS GENMASK(3, 0)
68 #define SDW_SHIM_PDMSCAP_OSS GENMASK(7, 4)
69 #define SDW_SHIM_PDMSCAP_BSS GENMASK(12, 8)
70 #define SDW_SHIM_PDMSCAP_CPSS GENMASK(15, 13)
72 #define SDW_SHIM_IOCTL_MIF BIT(0)
73 #define SDW_SHIM_IOCTL_CO BIT(1)
74 #define SDW_SHIM_IOCTL_COE BIT(2)
75 #define SDW_SHIM_IOCTL_DO BIT(3)
76 #define SDW_SHIM_IOCTL_DOE BIT(4)
77 #define SDW_SHIM_IOCTL_BKE BIT(5)
78 #define SDW_SHIM_IOCTL_WPDD BIT(6)
79 #define SDW_SHIM_IOCTL_CIBD BIT(8)
80 #define SDW_SHIM_IOCTL_DIBD BIT(9)
82 #define SDW_SHIM_CTMCTL_DACTQE BIT(0)
83 #define SDW_SHIM_CTMCTL_DODS BIT(1)
84 #define SDW_SHIM_CTMCTL_DOAIS GENMASK(4, 3)
86 #define SDW_SHIM_WAKEEN_ENABLE BIT(0)
87 #define SDW_SHIM_WAKESTS_STATUS BIT(0)
89 /* Intel ALH Register definitions */
90 #define SDW_ALH_STRMZCFG(x) (0x000 + (0x4 * (x)))
91 #define SDW_ALH_NUM_STREAMS 64
93 #define SDW_ALH_STRMZCFG_DMAT_VAL 0x3
94 #define SDW_ALH_STRMZCFG_DMAT GENMASK(7, 0)
95 #define SDW_ALH_STRMZCFG_CHN GENMASK(19, 16)
103 #define cdns_to_intel(_cdns) container_of(_cdns, struct sdw_intel, cdns)
106 * Read, write helpers for HW registers
108 static inline int intel_readl(void __iomem
*base
, int offset
)
110 return readl(base
+ offset
);
113 static inline void intel_writel(void __iomem
*base
, int offset
, int value
)
115 writel(value
, base
+ offset
);
118 static inline u16
intel_readw(void __iomem
*base
, int offset
)
120 return readw(base
+ offset
);
123 static inline void intel_writew(void __iomem
*base
, int offset
, u16 value
)
125 writew(value
, base
+ offset
);
128 static int intel_wait_bit(void __iomem
*base
, int offset
, u32 mask
, u32 target
)
134 reg_read
= readl(base
+ offset
);
135 if ((reg_read
& mask
) == target
)
139 usleep_range(50, 100);
140 } while (timeout
!= 0);
145 static int intel_clear_bit(void __iomem
*base
, int offset
, u32 value
, u32 mask
)
147 writel(value
, base
+ offset
);
148 return intel_wait_bit(base
, offset
, mask
, 0);
151 static int intel_set_bit(void __iomem
*base
, int offset
, u32 value
, u32 mask
)
153 writel(value
, base
+ offset
);
154 return intel_wait_bit(base
, offset
, mask
, mask
);
160 #ifdef CONFIG_DEBUG_FS
162 #define RD_BUF (2 * PAGE_SIZE)
164 static ssize_t
intel_sprintf(void __iomem
*mem
, bool l
,
165 char *buf
, size_t pos
, unsigned int reg
)
170 value
= intel_readl(mem
, reg
);
172 value
= intel_readw(mem
, reg
);
174 return scnprintf(buf
+ pos
, RD_BUF
- pos
, "%4x\t%4x\n", reg
, value
);
177 static int intel_reg_show(struct seq_file
*s_file
, void *data
)
179 struct sdw_intel
*sdw
= s_file
->private;
180 void __iomem
*s
= sdw
->link_res
->shim
;
181 void __iomem
*a
= sdw
->link_res
->alh
;
185 unsigned int links
, reg
;
187 buf
= kzalloc(RD_BUF
, GFP_KERNEL
);
191 links
= intel_readl(s
, SDW_SHIM_LCAP
) & GENMASK(2, 0);
193 ret
= scnprintf(buf
, RD_BUF
, "Register Value\n");
194 ret
+= scnprintf(buf
+ ret
, RD_BUF
- ret
, "\nShim\n");
196 for (i
= 0; i
< links
; i
++) {
197 reg
= SDW_SHIM_LCAP
+ i
* 4;
198 ret
+= intel_sprintf(s
, true, buf
, ret
, reg
);
201 for (i
= 0; i
< links
; i
++) {
202 ret
+= scnprintf(buf
+ ret
, RD_BUF
- ret
, "\nLink%d\n", i
);
203 ret
+= intel_sprintf(s
, false, buf
, ret
, SDW_SHIM_CTLSCAP(i
));
204 ret
+= intel_sprintf(s
, false, buf
, ret
, SDW_SHIM_CTLS0CM(i
));
205 ret
+= intel_sprintf(s
, false, buf
, ret
, SDW_SHIM_CTLS1CM(i
));
206 ret
+= intel_sprintf(s
, false, buf
, ret
, SDW_SHIM_CTLS2CM(i
));
207 ret
+= intel_sprintf(s
, false, buf
, ret
, SDW_SHIM_CTLS3CM(i
));
208 ret
+= intel_sprintf(s
, false, buf
, ret
, SDW_SHIM_PCMSCAP(i
));
210 ret
+= scnprintf(buf
+ ret
, RD_BUF
- ret
, "\n PCMSyCH registers\n");
213 * the value 10 is the number of PDIs. We will need a
214 * cleanup to remove hard-coded Intel configurations
215 * from cadence_master.c
217 for (j
= 0; j
< 10; j
++) {
218 ret
+= intel_sprintf(s
, false, buf
, ret
,
219 SDW_SHIM_PCMSYCHM(i
, j
));
220 ret
+= intel_sprintf(s
, false, buf
, ret
,
221 SDW_SHIM_PCMSYCHC(i
, j
));
223 ret
+= scnprintf(buf
+ ret
, RD_BUF
- ret
, "\n PDMSCAP, IOCTL, CTMCTL\n");
225 ret
+= intel_sprintf(s
, false, buf
, ret
, SDW_SHIM_PDMSCAP(i
));
226 ret
+= intel_sprintf(s
, false, buf
, ret
, SDW_SHIM_IOCTL(i
));
227 ret
+= intel_sprintf(s
, false, buf
, ret
, SDW_SHIM_CTMCTL(i
));
230 ret
+= scnprintf(buf
+ ret
, RD_BUF
- ret
, "\nWake registers\n");
231 ret
+= intel_sprintf(s
, false, buf
, ret
, SDW_SHIM_WAKEEN
);
232 ret
+= intel_sprintf(s
, false, buf
, ret
, SDW_SHIM_WAKESTS
);
234 ret
+= scnprintf(buf
+ ret
, RD_BUF
- ret
, "\nALH STRMzCFG\n");
235 for (i
= 0; i
< SDW_ALH_NUM_STREAMS
; i
++)
236 ret
+= intel_sprintf(a
, true, buf
, ret
, SDW_ALH_STRMZCFG(i
));
238 seq_printf(s_file
, "%s", buf
);
243 DEFINE_SHOW_ATTRIBUTE(intel_reg
);
245 static void intel_debugfs_init(struct sdw_intel
*sdw
)
247 struct dentry
*root
= sdw
->cdns
.bus
.debugfs
;
252 sdw
->debugfs
= debugfs_create_dir("intel-sdw", root
);
254 debugfs_create_file("intel-registers", 0400, sdw
->debugfs
, sdw
,
257 sdw_cdns_debugfs_init(&sdw
->cdns
, sdw
->debugfs
);
260 static void intel_debugfs_exit(struct sdw_intel
*sdw
)
262 debugfs_remove_recursive(sdw
->debugfs
);
265 static void intel_debugfs_init(struct sdw_intel
*sdw
) {}
266 static void intel_debugfs_exit(struct sdw_intel
*sdw
) {}
267 #endif /* CONFIG_DEBUG_FS */
273 static int intel_link_power_up(struct sdw_intel
*sdw
)
275 unsigned int link_id
= sdw
->instance
;
276 void __iomem
*shim
= sdw
->link_res
->shim
;
277 u32
*shim_mask
= sdw
->link_res
->shim_mask
;
278 struct sdw_bus
*bus
= &sdw
->cdns
.bus
;
279 struct sdw_master_prop
*prop
= &bus
->prop
;
280 int spa_mask
, cpa_mask
;
286 mutex_lock(sdw
->link_res
->shim_lock
);
289 * The hardware relies on an internal counter, typically 4kHz,
290 * to generate the SoundWire SSP - which defines a 'safe'
291 * synchronization point between commands and audio transport
292 * and allows for multi link synchronization. The SYNCPRD value
293 * is only dependent on the oscillator clock provided to
294 * the IP, so adjust based on _DSD properties reported in DSDT
295 * tables. The values reported are based on either 24MHz
296 * (CNL/CML) or 38.4 MHz (ICL/TGL+).
298 if (prop
->mclk_freq
% 6000000)
299 syncprd
= SDW_SHIM_SYNC_SYNCPRD_VAL_38_4
;
301 syncprd
= SDW_SHIM_SYNC_SYNCPRD_VAL_24
;
304 /* we first need to program the SyncPRD/CPU registers */
305 dev_dbg(sdw
->cdns
.dev
,
306 "%s: first link up, programming SYNCPRD\n", __func__
);
308 /* set SyncPRD period */
309 sync_reg
= intel_readl(shim
, SDW_SHIM_SYNC
);
310 sync_reg
|= (syncprd
<<
311 SDW_REG_SHIFT(SDW_SHIM_SYNC_SYNCPRD
));
313 /* Set SyncCPU bit */
314 sync_reg
|= SDW_SHIM_SYNC_SYNCCPU
;
315 intel_writel(shim
, SDW_SHIM_SYNC
, sync_reg
);
318 /* Link power up sequence */
319 link_control
= intel_readl(shim
, SDW_SHIM_LCTL
);
320 spa_mask
= (SDW_SHIM_LCTL_SPA
<< link_id
);
321 cpa_mask
= (SDW_SHIM_LCTL_CPA
<< link_id
);
322 link_control
|= spa_mask
;
324 ret
= intel_set_bit(shim
, SDW_SHIM_LCTL
, link_control
, cpa_mask
);
326 dev_err(sdw
->cdns
.dev
, "Failed to power up link: %d\n", ret
);
331 /* SyncCPU will change once link is active */
332 ret
= intel_wait_bit(shim
, SDW_SHIM_SYNC
,
333 SDW_SHIM_SYNC_SYNCCPU
, 0);
335 dev_err(sdw
->cdns
.dev
,
336 "Failed to set SHIM_SYNC: %d\n", ret
);
341 *shim_mask
|= BIT(link_id
);
343 sdw
->cdns
.link_up
= true;
345 mutex_unlock(sdw
->link_res
->shim_lock
);
350 /* this needs to be called with shim_lock */
351 static void intel_shim_glue_to_master_ip(struct sdw_intel
*sdw
)
353 void __iomem
*shim
= sdw
->link_res
->shim
;
354 unsigned int link_id
= sdw
->instance
;
357 /* Switch to MIP from Glue logic */
358 ioctl
= intel_readw(shim
, SDW_SHIM_IOCTL(link_id
));
360 ioctl
&= ~(SDW_SHIM_IOCTL_DOE
);
361 intel_writew(shim
, SDW_SHIM_IOCTL(link_id
), ioctl
);
362 usleep_range(10, 15);
364 ioctl
&= ~(SDW_SHIM_IOCTL_DO
);
365 intel_writew(shim
, SDW_SHIM_IOCTL(link_id
), ioctl
);
366 usleep_range(10, 15);
368 ioctl
|= (SDW_SHIM_IOCTL_MIF
);
369 intel_writew(shim
, SDW_SHIM_IOCTL(link_id
), ioctl
);
370 usleep_range(10, 15);
372 ioctl
&= ~(SDW_SHIM_IOCTL_BKE
);
373 ioctl
&= ~(SDW_SHIM_IOCTL_COE
);
374 intel_writew(shim
, SDW_SHIM_IOCTL(link_id
), ioctl
);
375 usleep_range(10, 15);
377 /* at this point Master IP has full control of the I/Os */
380 /* this needs to be called with shim_lock */
381 static void intel_shim_master_ip_to_glue(struct sdw_intel
*sdw
)
383 unsigned int link_id
= sdw
->instance
;
384 void __iomem
*shim
= sdw
->link_res
->shim
;
388 ioctl
= intel_readw(shim
, SDW_SHIM_IOCTL(link_id
));
389 ioctl
|= SDW_SHIM_IOCTL_BKE
;
390 ioctl
|= SDW_SHIM_IOCTL_COE
;
391 intel_writew(shim
, SDW_SHIM_IOCTL(link_id
), ioctl
);
392 usleep_range(10, 15);
394 ioctl
&= ~(SDW_SHIM_IOCTL_MIF
);
395 intel_writew(shim
, SDW_SHIM_IOCTL(link_id
), ioctl
);
396 usleep_range(10, 15);
398 /* at this point Integration Glue has full control of the I/Os */
401 static int intel_shim_init(struct sdw_intel
*sdw
, bool clock_stop
)
403 void __iomem
*shim
= sdw
->link_res
->shim
;
404 unsigned int link_id
= sdw
->instance
;
406 u16 ioctl
= 0, act
= 0;
408 mutex_lock(sdw
->link_res
->shim_lock
);
410 /* Initialize Shim */
411 ioctl
|= SDW_SHIM_IOCTL_BKE
;
412 intel_writew(shim
, SDW_SHIM_IOCTL(link_id
), ioctl
);
413 usleep_range(10, 15);
415 ioctl
|= SDW_SHIM_IOCTL_WPDD
;
416 intel_writew(shim
, SDW_SHIM_IOCTL(link_id
), ioctl
);
417 usleep_range(10, 15);
419 ioctl
|= SDW_SHIM_IOCTL_DO
;
420 intel_writew(shim
, SDW_SHIM_IOCTL(link_id
), ioctl
);
421 usleep_range(10, 15);
423 ioctl
|= SDW_SHIM_IOCTL_DOE
;
424 intel_writew(shim
, SDW_SHIM_IOCTL(link_id
), ioctl
);
425 usleep_range(10, 15);
427 intel_shim_glue_to_master_ip(sdw
);
429 act
|= 0x1 << SDW_REG_SHIFT(SDW_SHIM_CTMCTL_DOAIS
);
430 act
|= SDW_SHIM_CTMCTL_DACTQE
;
431 act
|= SDW_SHIM_CTMCTL_DODS
;
432 intel_writew(shim
, SDW_SHIM_CTMCTL(link_id
), act
);
433 usleep_range(10, 15);
435 mutex_unlock(sdw
->link_res
->shim_lock
);
440 static void intel_shim_wake(struct sdw_intel
*sdw
, bool wake_enable
)
442 void __iomem
*shim
= sdw
->link_res
->shim
;
443 unsigned int link_id
= sdw
->instance
;
444 u16 wake_en
, wake_sts
;
446 mutex_lock(sdw
->link_res
->shim_lock
);
447 wake_en
= intel_readw(shim
, SDW_SHIM_WAKEEN
);
450 /* Enable the wakeup */
451 wake_en
|= (SDW_SHIM_WAKEEN_ENABLE
<< link_id
);
452 intel_writew(shim
, SDW_SHIM_WAKEEN
, wake_en
);
454 /* Disable the wake up interrupt */
455 wake_en
&= ~(SDW_SHIM_WAKEEN_ENABLE
<< link_id
);
456 intel_writew(shim
, SDW_SHIM_WAKEEN
, wake_en
);
458 /* Clear wake status */
459 wake_sts
= intel_readw(shim
, SDW_SHIM_WAKESTS
);
460 wake_sts
|= (SDW_SHIM_WAKEEN_ENABLE
<< link_id
);
461 intel_writew(shim
, SDW_SHIM_WAKESTS_STATUS
, wake_sts
);
463 mutex_unlock(sdw
->link_res
->shim_lock
);
466 static int __maybe_unused
intel_link_power_down(struct sdw_intel
*sdw
)
468 int link_control
, spa_mask
, cpa_mask
;
469 unsigned int link_id
= sdw
->instance
;
470 void __iomem
*shim
= sdw
->link_res
->shim
;
471 u32
*shim_mask
= sdw
->link_res
->shim_mask
;
474 mutex_lock(sdw
->link_res
->shim_lock
);
476 intel_shim_master_ip_to_glue(sdw
);
478 /* Link power down sequence */
479 link_control
= intel_readl(shim
, SDW_SHIM_LCTL
);
480 spa_mask
= ~(SDW_SHIM_LCTL_SPA
<< link_id
);
481 cpa_mask
= (SDW_SHIM_LCTL_CPA
<< link_id
);
482 link_control
&= spa_mask
;
484 ret
= intel_clear_bit(shim
, SDW_SHIM_LCTL
, link_control
, cpa_mask
);
486 if (!(*shim_mask
& BIT(link_id
)))
487 dev_err(sdw
->cdns
.dev
,
488 "%s: Unbalanced power-up/down calls\n", __func__
);
490 *shim_mask
&= ~BIT(link_id
);
492 mutex_unlock(sdw
->link_res
->shim_lock
);
497 sdw
->cdns
.link_up
= false;
501 static void intel_shim_sync_arm(struct sdw_intel
*sdw
)
503 void __iomem
*shim
= sdw
->link_res
->shim
;
506 mutex_lock(sdw
->link_res
->shim_lock
);
508 /* update SYNC register */
509 sync_reg
= intel_readl(shim
, SDW_SHIM_SYNC
);
510 sync_reg
|= (SDW_SHIM_SYNC_CMDSYNC
<< sdw
->instance
);
511 intel_writel(shim
, SDW_SHIM_SYNC
, sync_reg
);
513 mutex_unlock(sdw
->link_res
->shim_lock
);
516 static int intel_shim_sync_go_unlocked(struct sdw_intel
*sdw
)
518 void __iomem
*shim
= sdw
->link_res
->shim
;
522 /* Read SYNC register */
523 sync_reg
= intel_readl(shim
, SDW_SHIM_SYNC
);
526 * Set SyncGO bit to synchronously trigger a bank switch for
527 * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all
530 sync_reg
|= SDW_SHIM_SYNC_SYNCGO
;
532 ret
= intel_clear_bit(shim
, SDW_SHIM_SYNC
, sync_reg
,
533 SDW_SHIM_SYNC_SYNCGO
);
536 dev_err(sdw
->cdns
.dev
, "SyncGO clear failed: %d\n", ret
);
544 static void intel_pdi_init(struct sdw_intel
*sdw
,
545 struct sdw_cdns_stream_config
*config
)
547 void __iomem
*shim
= sdw
->link_res
->shim
;
548 unsigned int link_id
= sdw
->instance
;
549 int pcm_cap
, pdm_cap
;
551 /* PCM Stream Capability */
552 pcm_cap
= intel_readw(shim
, SDW_SHIM_PCMSCAP(link_id
));
554 config
->pcm_bd
= (pcm_cap
& SDW_SHIM_PCMSCAP_BSS
) >>
555 SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_BSS
);
556 config
->pcm_in
= (pcm_cap
& SDW_SHIM_PCMSCAP_ISS
) >>
557 SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_ISS
);
558 config
->pcm_out
= (pcm_cap
& SDW_SHIM_PCMSCAP_OSS
) >>
559 SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_OSS
);
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
);
564 /* PDM Stream Capability */
565 pdm_cap
= intel_readw(shim
, SDW_SHIM_PDMSCAP(link_id
));
567 config
->pdm_bd
= (pdm_cap
& SDW_SHIM_PDMSCAP_BSS
) >>
568 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_BSS
);
569 config
->pdm_in
= (pdm_cap
& SDW_SHIM_PDMSCAP_ISS
) >>
570 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_ISS
);
571 config
->pdm_out
= (pdm_cap
& SDW_SHIM_PDMSCAP_OSS
) >>
572 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_OSS
);
574 dev_dbg(sdw
->cdns
.dev
, "PDM cap bd:%d in:%d out:%d\n",
575 config
->pdm_bd
, config
->pdm_in
, config
->pdm_out
);
579 intel_pdi_get_ch_cap(struct sdw_intel
*sdw
, unsigned int pdi_num
, bool pcm
)
581 void __iomem
*shim
= sdw
->link_res
->shim
;
582 unsigned int link_id
= sdw
->instance
;
586 count
= intel_readw(shim
, SDW_SHIM_PCMSYCHC(link_id
, pdi_num
));
589 * WORKAROUND: on all existing Intel controllers, pdi
590 * number 2 reports channel count as 1 even though it
591 * supports 8 channels. Performing hardcoding for pdi
598 count
= intel_readw(shim
, SDW_SHIM_PDMSCAP(link_id
));
599 count
= ((count
& SDW_SHIM_PDMSCAP_CPSS
) >>
600 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_CPSS
));
603 /* zero based values for channel count in register */
609 static int intel_pdi_get_ch_update(struct sdw_intel
*sdw
,
610 struct sdw_cdns_pdi
*pdi
,
611 unsigned int num_pdi
,
612 unsigned int *num_ch
, bool pcm
)
616 for (i
= 0; i
< num_pdi
; i
++) {
617 pdi
->ch_count
= intel_pdi_get_ch_cap(sdw
, pdi
->num
, pcm
);
618 ch_count
+= pdi
->ch_count
;
626 static int intel_pdi_stream_ch_update(struct sdw_intel
*sdw
,
627 struct sdw_cdns_streams
*stream
, bool pcm
)
629 intel_pdi_get_ch_update(sdw
, stream
->bd
, stream
->num_bd
,
630 &stream
->num_ch_bd
, pcm
);
632 intel_pdi_get_ch_update(sdw
, stream
->in
, stream
->num_in
,
633 &stream
->num_ch_in
, pcm
);
635 intel_pdi_get_ch_update(sdw
, stream
->out
, stream
->num_out
,
636 &stream
->num_ch_out
, pcm
);
641 static int intel_pdi_ch_update(struct sdw_intel
*sdw
)
643 /* First update PCM streams followed by PDM streams */
644 intel_pdi_stream_ch_update(sdw
, &sdw
->cdns
.pcm
, true);
645 intel_pdi_stream_ch_update(sdw
, &sdw
->cdns
.pdm
, false);
651 intel_pdi_shim_configure(struct sdw_intel
*sdw
, struct sdw_cdns_pdi
*pdi
)
653 void __iomem
*shim
= sdw
->link_res
->shim
;
654 unsigned int link_id
= sdw
->instance
;
657 /* the Bulk and PCM streams are not contiguous */
658 pdi
->intel_alh_id
= (link_id
* 16) + pdi
->num
+ 3;
660 pdi
->intel_alh_id
+= 2;
663 * Program stream parameters to stream SHIM register
664 * This is applicable for PCM stream only.
666 if (pdi
->type
!= SDW_STREAM_PCM
)
669 if (pdi
->dir
== SDW_DATA_DIR_RX
)
670 pdi_conf
|= SDW_SHIM_PCMSYCM_DIR
;
672 pdi_conf
&= ~(SDW_SHIM_PCMSYCM_DIR
);
674 pdi_conf
|= (pdi
->intel_alh_id
<<
675 SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_STREAM
));
676 pdi_conf
|= (pdi
->l_ch_num
<< SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_LCHN
));
677 pdi_conf
|= (pdi
->h_ch_num
<< SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_HCHN
));
679 intel_writew(shim
, SDW_SHIM_PCMSYCHM(link_id
, pdi
->num
), pdi_conf
);
683 intel_pdi_alh_configure(struct sdw_intel
*sdw
, struct sdw_cdns_pdi
*pdi
)
685 void __iomem
*alh
= sdw
->link_res
->alh
;
686 unsigned int link_id
= sdw
->instance
;
689 /* the Bulk and PCM streams are not contiguous */
690 pdi
->intel_alh_id
= (link_id
* 16) + pdi
->num
+ 3;
692 pdi
->intel_alh_id
+= 2;
694 /* Program Stream config ALH register */
695 conf
= intel_readl(alh
, SDW_ALH_STRMZCFG(pdi
->intel_alh_id
));
697 conf
|= (SDW_ALH_STRMZCFG_DMAT_VAL
<<
698 SDW_REG_SHIFT(SDW_ALH_STRMZCFG_DMAT
));
700 conf
|= ((pdi
->ch_count
- 1) <<
701 SDW_REG_SHIFT(SDW_ALH_STRMZCFG_CHN
));
703 intel_writel(alh
, SDW_ALH_STRMZCFG(pdi
->intel_alh_id
), conf
);
706 static int intel_params_stream(struct sdw_intel
*sdw
,
707 struct snd_pcm_substream
*substream
,
708 struct snd_soc_dai
*dai
,
709 struct snd_pcm_hw_params
*hw_params
,
710 int link_id
, int alh_stream_id
)
712 struct sdw_intel_link_res
*res
= sdw
->link_res
;
713 struct sdw_intel_stream_params_data params_data
;
715 params_data
.substream
= substream
;
716 params_data
.dai
= dai
;
717 params_data
.hw_params
= hw_params
;
718 params_data
.link_id
= link_id
;
719 params_data
.alh_stream_id
= alh_stream_id
;
721 if (res
->ops
&& res
->ops
->params_stream
&& res
->dev
)
722 return res
->ops
->params_stream(res
->dev
,
727 static int intel_free_stream(struct sdw_intel
*sdw
,
728 struct snd_pcm_substream
*substream
,
729 struct snd_soc_dai
*dai
,
732 struct sdw_intel_link_res
*res
= sdw
->link_res
;
733 struct sdw_intel_stream_free_data free_data
;
735 free_data
.substream
= substream
;
737 free_data
.link_id
= link_id
;
739 if (res
->ops
&& res
->ops
->free_stream
&& res
->dev
)
740 return res
->ops
->free_stream(res
->dev
,
747 * bank switch routines
750 static int intel_pre_bank_switch(struct sdw_bus
*bus
)
752 struct sdw_cdns
*cdns
= bus_to_cdns(bus
);
753 struct sdw_intel
*sdw
= cdns_to_intel(cdns
);
755 /* Write to register only for multi-link */
756 if (!bus
->multi_link
)
759 intel_shim_sync_arm(sdw
);
764 static int intel_post_bank_switch(struct sdw_bus
*bus
)
766 struct sdw_cdns
*cdns
= bus_to_cdns(bus
);
767 struct sdw_intel
*sdw
= cdns_to_intel(cdns
);
768 void __iomem
*shim
= sdw
->link_res
->shim
;
771 /* Write to register only for multi-link */
772 if (!bus
->multi_link
)
775 mutex_lock(sdw
->link_res
->shim_lock
);
777 /* Read SYNC register */
778 sync_reg
= intel_readl(shim
, SDW_SHIM_SYNC
);
781 * post_bank_switch() ops is called from the bus in loop for
782 * all the Masters in the steam with the expectation that
783 * we trigger the bankswitch for the only first Master in the list
784 * and do nothing for the other Masters
786 * So, set the SYNCGO bit only if CMDSYNC bit is set for any Master.
788 if (!(sync_reg
& SDW_SHIM_SYNC_CMDSYNC_MASK
)) {
793 ret
= intel_shim_sync_go_unlocked(sdw
);
795 mutex_unlock(sdw
->link_res
->shim_lock
);
798 dev_err(sdw
->cdns
.dev
, "Post bank switch failed: %d\n", ret
);
807 static int intel_startup(struct snd_pcm_substream
*substream
,
808 struct snd_soc_dai
*dai
)
811 * TODO: add pm_runtime support here, the startup callback
812 * will make sure the IP is 'active'
817 static int intel_hw_params(struct snd_pcm_substream
*substream
,
818 struct snd_pcm_hw_params
*params
,
819 struct snd_soc_dai
*dai
)
821 struct sdw_cdns
*cdns
= snd_soc_dai_get_drvdata(dai
);
822 struct sdw_intel
*sdw
= cdns_to_intel(cdns
);
823 struct sdw_cdns_dma_data
*dma
;
824 struct sdw_cdns_pdi
*pdi
;
825 struct sdw_stream_config sconfig
;
826 struct sdw_port_config
*pconfig
;
831 dma
= snd_soc_dai_get_dma_data(dai
, substream
);
835 ch
= params_channels(params
);
836 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
837 dir
= SDW_DATA_DIR_RX
;
839 dir
= SDW_DATA_DIR_TX
;
841 if (dma
->stream_type
== SDW_STREAM_PDM
)
845 pdi
= sdw_cdns_alloc_pdi(cdns
, &cdns
->pcm
, ch
, dir
, dai
->id
);
847 pdi
= sdw_cdns_alloc_pdi(cdns
, &cdns
->pdm
, ch
, dir
, dai
->id
);
854 /* do run-time configurations for SHIM, ALH and PDI/PORT */
855 intel_pdi_shim_configure(sdw
, pdi
);
856 intel_pdi_alh_configure(sdw
, pdi
);
857 sdw_cdns_config_stream(cdns
, ch
, dir
, pdi
);
860 /* Inform DSP about PDI stream number */
861 ret
= intel_params_stream(sdw
, substream
, dai
, params
,
867 sconfig
.direction
= dir
;
868 sconfig
.ch_count
= ch
;
869 sconfig
.frame_rate
= params_rate(params
);
870 sconfig
.type
= dma
->stream_type
;
872 if (dma
->stream_type
== SDW_STREAM_PDM
) {
873 sconfig
.frame_rate
*= 50;
876 sconfig
.bps
= snd_pcm_format_width(params_format(params
));
879 /* Port configuration */
880 pconfig
= kcalloc(1, sizeof(*pconfig
), GFP_KERNEL
);
886 pconfig
->num
= pdi
->num
;
887 pconfig
->ch_mask
= (1 << ch
) - 1;
889 ret
= sdw_stream_add_master(&cdns
->bus
, &sconfig
,
890 pconfig
, 1, dma
->stream
);
892 dev_err(cdns
->dev
, "add master to stream failed:%d\n", ret
);
899 static int intel_prepare(struct snd_pcm_substream
*substream
,
900 struct snd_soc_dai
*dai
)
902 struct sdw_cdns_dma_data
*dma
;
904 dma
= snd_soc_dai_get_dma_data(dai
, substream
);
906 dev_err(dai
->dev
, "failed to get dma data in %s",
911 return sdw_prepare_stream(dma
->stream
);
914 static int intel_trigger(struct snd_pcm_substream
*substream
, int cmd
,
915 struct snd_soc_dai
*dai
)
917 struct sdw_cdns_dma_data
*dma
;
920 dma
= snd_soc_dai_get_dma_data(dai
, substream
);
922 dev_err(dai
->dev
, "failed to get dma data in %s", __func__
);
927 case SNDRV_PCM_TRIGGER_START
:
928 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
929 case SNDRV_PCM_TRIGGER_RESUME
:
930 ret
= sdw_enable_stream(dma
->stream
);
933 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
934 case SNDRV_PCM_TRIGGER_SUSPEND
:
935 case SNDRV_PCM_TRIGGER_STOP
:
936 ret
= sdw_disable_stream(dma
->stream
);
946 "%s trigger %d failed: %d",
952 intel_hw_free(struct snd_pcm_substream
*substream
, struct snd_soc_dai
*dai
)
954 struct sdw_cdns
*cdns
= snd_soc_dai_get_drvdata(dai
);
955 struct sdw_intel
*sdw
= cdns_to_intel(cdns
);
956 struct sdw_cdns_dma_data
*dma
;
959 dma
= snd_soc_dai_get_dma_data(dai
, substream
);
963 ret
= sdw_deprepare_stream(dma
->stream
);
965 dev_err(dai
->dev
, "sdw_deprepare_stream: failed %d", ret
);
969 ret
= sdw_stream_remove_master(&cdns
->bus
, dma
->stream
);
971 dev_err(dai
->dev
, "remove master from stream %s failed: %d\n",
972 dma
->stream
->name
, ret
);
976 ret
= intel_free_stream(sdw
, substream
, dai
, sdw
->instance
);
978 dev_err(dai
->dev
, "intel_free_stream: failed %d", ret
);
985 static void intel_shutdown(struct snd_pcm_substream
*substream
,
986 struct snd_soc_dai
*dai
)
991 static int intel_pcm_set_sdw_stream(struct snd_soc_dai
*dai
,
992 void *stream
, int direction
)
994 return cdns_set_sdw_stream(dai
, stream
, true, direction
);
997 static int intel_pdm_set_sdw_stream(struct snd_soc_dai
*dai
,
998 void *stream
, int direction
)
1000 return cdns_set_sdw_stream(dai
, stream
, false, direction
);
1003 static void *intel_get_sdw_stream(struct snd_soc_dai
*dai
,
1006 struct sdw_cdns_dma_data
*dma
;
1008 if (direction
== SNDRV_PCM_STREAM_PLAYBACK
)
1009 dma
= dai
->playback_dma_data
;
1011 dma
= dai
->capture_dma_data
;
1019 static const struct snd_soc_dai_ops intel_pcm_dai_ops
= {
1020 .startup
= intel_startup
,
1021 .hw_params
= intel_hw_params
,
1022 .prepare
= intel_prepare
,
1023 .trigger
= intel_trigger
,
1024 .hw_free
= intel_hw_free
,
1025 .shutdown
= intel_shutdown
,
1026 .set_sdw_stream
= intel_pcm_set_sdw_stream
,
1027 .get_sdw_stream
= intel_get_sdw_stream
,
1030 static const struct snd_soc_dai_ops intel_pdm_dai_ops
= {
1031 .startup
= intel_startup
,
1032 .hw_params
= intel_hw_params
,
1033 .prepare
= intel_prepare
,
1034 .trigger
= intel_trigger
,
1035 .hw_free
= intel_hw_free
,
1036 .shutdown
= intel_shutdown
,
1037 .set_sdw_stream
= intel_pdm_set_sdw_stream
,
1038 .get_sdw_stream
= intel_get_sdw_stream
,
1041 static const struct snd_soc_component_driver dai_component
= {
1042 .name
= "soundwire",
1045 static int intel_create_dai(struct sdw_cdns
*cdns
,
1046 struct snd_soc_dai_driver
*dais
,
1047 enum intel_pdi_type type
,
1048 u32 num
, u32 off
, u32 max_ch
, bool pcm
)
1055 /* TODO: Read supported rates/formats from hardware */
1056 for (i
= off
; i
< (off
+ num
); i
++) {
1057 dais
[i
].name
= devm_kasprintf(cdns
->dev
, GFP_KERNEL
,
1063 if (type
== INTEL_PDI_BD
|| type
== INTEL_PDI_OUT
) {
1064 dais
[i
].playback
.channels_min
= 1;
1065 dais
[i
].playback
.channels_max
= max_ch
;
1066 dais
[i
].playback
.rates
= SNDRV_PCM_RATE_48000
;
1067 dais
[i
].playback
.formats
= SNDRV_PCM_FMTBIT_S16_LE
;
1070 if (type
== INTEL_PDI_BD
|| type
== INTEL_PDI_IN
) {
1071 dais
[i
].capture
.channels_min
= 1;
1072 dais
[i
].capture
.channels_max
= max_ch
;
1073 dais
[i
].capture
.rates
= SNDRV_PCM_RATE_48000
;
1074 dais
[i
].capture
.formats
= SNDRV_PCM_FMTBIT_S16_LE
;
1078 dais
[i
].ops
= &intel_pcm_dai_ops
;
1080 dais
[i
].ops
= &intel_pdm_dai_ops
;
1086 static int intel_register_dai(struct sdw_intel
*sdw
)
1088 struct sdw_cdns
*cdns
= &sdw
->cdns
;
1089 struct sdw_cdns_streams
*stream
;
1090 struct snd_soc_dai_driver
*dais
;
1091 int num_dai
, ret
, off
= 0;
1093 /* DAIs are created based on total number of PDIs supported */
1094 num_dai
= cdns
->pcm
.num_pdi
+ cdns
->pdm
.num_pdi
;
1096 dais
= devm_kcalloc(cdns
->dev
, num_dai
, sizeof(*dais
), GFP_KERNEL
);
1100 /* Create PCM DAIs */
1101 stream
= &cdns
->pcm
;
1103 ret
= intel_create_dai(cdns
, dais
, INTEL_PDI_IN
, cdns
->pcm
.num_in
,
1104 off
, stream
->num_ch_in
, true);
1108 off
+= cdns
->pcm
.num_in
;
1109 ret
= intel_create_dai(cdns
, dais
, INTEL_PDI_OUT
, cdns
->pcm
.num_out
,
1110 off
, stream
->num_ch_out
, true);
1114 off
+= cdns
->pcm
.num_out
;
1115 ret
= intel_create_dai(cdns
, dais
, INTEL_PDI_BD
, cdns
->pcm
.num_bd
,
1116 off
, stream
->num_ch_bd
, true);
1120 /* Create PDM DAIs */
1121 stream
= &cdns
->pdm
;
1122 off
+= cdns
->pcm
.num_bd
;
1123 ret
= intel_create_dai(cdns
, dais
, INTEL_PDI_IN
, cdns
->pdm
.num_in
,
1124 off
, stream
->num_ch_in
, false);
1128 off
+= cdns
->pdm
.num_in
;
1129 ret
= intel_create_dai(cdns
, dais
, INTEL_PDI_OUT
, cdns
->pdm
.num_out
,
1130 off
, stream
->num_ch_out
, false);
1134 off
+= cdns
->pdm
.num_out
;
1135 ret
= intel_create_dai(cdns
, dais
, INTEL_PDI_BD
, cdns
->pdm
.num_bd
,
1136 off
, stream
->num_ch_bd
, false);
1140 return snd_soc_register_component(cdns
->dev
, &dai_component
,
1144 static int sdw_master_read_intel_prop(struct sdw_bus
*bus
)
1146 struct sdw_master_prop
*prop
= &bus
->prop
;
1147 struct fwnode_handle
*link
;
1151 /* Find master handle */
1152 snprintf(name
, sizeof(name
),
1153 "mipi-sdw-link-%d-subproperties", bus
->link_id
);
1155 link
= device_get_named_child_node(bus
->dev
, name
);
1157 dev_err(bus
->dev
, "Master node %s not found\n", name
);
1161 fwnode_property_read_u32(link
,
1162 "intel-sdw-ip-clock",
1165 /* the values reported by BIOS are the 2x clock, not the bus clock */
1166 prop
->mclk_freq
/= 2;
1168 fwnode_property_read_u32(link
,
1172 if (quirk_mask
& SDW_INTEL_QUIRK_MASK_BUS_DISABLE
)
1173 prop
->hw_disabled
= true;
1178 static int intel_prop_read(struct sdw_bus
*bus
)
1180 /* Initialize with default handler to read all DisCo properties */
1181 sdw_master_read_prop(bus
);
1183 /* read Intel-specific properties */
1184 sdw_master_read_intel_prop(bus
);
1189 static struct sdw_master_ops sdw_intel_ops
= {
1190 .read_prop
= sdw_master_read_prop
,
1191 .xfer_msg
= cdns_xfer_msg
,
1192 .xfer_msg_defer
= cdns_xfer_msg_defer
,
1193 .reset_page_addr
= cdns_reset_page_addr
,
1194 .set_bus_conf
= cdns_bus_conf
,
1195 .pre_bank_switch
= intel_pre_bank_switch
,
1196 .post_bank_switch
= intel_post_bank_switch
,
1199 static int intel_init(struct sdw_intel
*sdw
)
1203 /* Initialize shim and controller */
1204 intel_link_power_up(sdw
);
1206 clock_stop
= sdw_cdns_is_clock_stop(&sdw
->cdns
);
1208 intel_shim_init(sdw
, clock_stop
);
1213 return sdw_cdns_init(&sdw
->cdns
);
1219 static int intel_master_probe(struct platform_device
*pdev
)
1221 struct device
*dev
= &pdev
->dev
;
1222 struct sdw_intel
*sdw
;
1223 struct sdw_cdns
*cdns
;
1224 struct sdw_bus
*bus
;
1227 sdw
= devm_kzalloc(dev
, sizeof(*sdw
), GFP_KERNEL
);
1234 sdw
->instance
= pdev
->id
;
1235 sdw
->link_res
= dev_get_platdata(dev
);
1237 cdns
->registers
= sdw
->link_res
->registers
;
1238 cdns
->instance
= sdw
->instance
;
1239 cdns
->msg_count
= 0;
1241 bus
->link_id
= pdev
->id
;
1243 sdw_cdns_probe(cdns
);
1245 /* Set property read ops */
1246 sdw_intel_ops
.read_prop
= intel_prop_read
;
1247 bus
->ops
= &sdw_intel_ops
;
1249 /* set driver data, accessed by snd_soc_dai_get_drvdata() */
1250 dev_set_drvdata(dev
, cdns
);
1252 ret
= sdw_bus_master_add(bus
, dev
, dev
->fwnode
);
1254 dev_err(dev
, "sdw_bus_master_add fail: %d\n", ret
);
1258 if (bus
->prop
.hw_disabled
)
1260 "SoundWire master %d is disabled, will be ignored\n",
1266 int intel_master_startup(struct platform_device
*pdev
)
1268 struct sdw_cdns_stream_config config
;
1269 struct device
*dev
= &pdev
->dev
;
1270 struct sdw_cdns
*cdns
= dev_get_drvdata(dev
);
1271 struct sdw_intel
*sdw
= cdns_to_intel(cdns
);
1272 struct sdw_bus
*bus
= &cdns
->bus
;
1275 if (bus
->prop
.hw_disabled
) {
1277 "SoundWire master %d is disabled, ignoring\n",
1282 /* Initialize shim, controller and Cadence IP */
1283 ret
= intel_init(sdw
);
1287 /* Read the PDI config and initialize cadence PDI */
1288 intel_pdi_init(sdw
, &config
);
1289 ret
= sdw_cdns_pdi_init(cdns
, config
);
1293 intel_pdi_ch_update(sdw
);
1295 ret
= sdw_cdns_enable_interrupt(cdns
, true);
1297 dev_err(dev
, "cannot enable interrupts\n");
1301 ret
= sdw_cdns_exit_reset(cdns
);
1303 dev_err(dev
, "unable to exit bus reset sequence\n");
1308 ret
= intel_register_dai(sdw
);
1310 dev_err(dev
, "DAI registration failed: %d\n", ret
);
1311 snd_soc_unregister_component(dev
);
1315 intel_debugfs_init(sdw
);
1320 sdw_cdns_enable_interrupt(cdns
, false);
1325 static int intel_master_remove(struct platform_device
*pdev
)
1327 struct device
*dev
= &pdev
->dev
;
1328 struct sdw_cdns
*cdns
= dev_get_drvdata(dev
);
1329 struct sdw_intel
*sdw
= cdns_to_intel(cdns
);
1330 struct sdw_bus
*bus
= &cdns
->bus
;
1332 if (!bus
->prop
.hw_disabled
) {
1333 intel_debugfs_exit(sdw
);
1334 sdw_cdns_enable_interrupt(cdns
, false);
1335 snd_soc_unregister_component(dev
);
1337 sdw_bus_master_delete(bus
);
1342 int intel_master_process_wakeen_event(struct platform_device
*pdev
)
1344 struct device
*dev
= &pdev
->dev
;
1345 struct sdw_intel
*sdw
;
1346 struct sdw_bus
*bus
;
1350 sdw
= platform_get_drvdata(pdev
);
1351 bus
= &sdw
->cdns
.bus
;
1353 if (bus
->prop
.hw_disabled
) {
1354 dev_dbg(dev
, "SoundWire master %d is disabled, ignoring\n", bus
->link_id
);
1358 shim
= sdw
->link_res
->shim
;
1359 wake_sts
= intel_readw(shim
, SDW_SHIM_WAKESTS
);
1361 if (!(wake_sts
& BIT(sdw
->instance
)))
1364 /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
1365 intel_shim_wake(sdw
, false);
1368 * resume the Master, which will generate a bus reset and result in
1369 * Slaves re-attaching and be re-enumerated. The SoundWire physical
1370 * device which generated the wake will trigger an interrupt, which
1371 * will in turn cause the corresponding Linux Slave device to be
1372 * resumed and the Slave codec driver to check the status.
1374 pm_request_resume(dev
);
1379 static struct platform_driver sdw_intel_drv
= {
1380 .probe
= intel_master_probe
,
1381 .remove
= intel_master_remove
,
1383 .name
= "intel-sdw",
1387 module_platform_driver(sdw_intel_drv
);
1389 MODULE_LICENSE("Dual BSD/GPL");
1390 MODULE_ALIAS("platform:intel-sdw");
1391 MODULE_DESCRIPTION("Intel Soundwire Master Driver");