blob: 6a1e862b16c38c901d27a5dddecb89810c12c20b [file] [log] [blame]
Vinod Koul71bb8a12017-12-14 11:19:43 +05301// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2// Copyright(c) 2015-17 Intel Corporation.
3
4/*
5 * Soundwire Intel Master Driver
6 */
7
8#include <linux/acpi.h>
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -05009#include <linux/debugfs.h>
Vinod Koul71bb8a12017-12-14 11:19:43 +053010#include <linux/delay.h>
Paul Gortmaker4abbd782019-04-13 11:12:52 -040011#include <linux/module.h>
Vinod Koul71bb8a12017-12-14 11:19:43 +053012#include <linux/interrupt.h>
Pierre-Louis Bossartdf72b712019-09-16 13:57:38 -050013#include <linux/io.h>
Vinod Koul71bb8a12017-12-14 11:19:43 +053014#include <linux/platform_device.h>
Vinod Koul37a2d222018-04-26 18:38:58 +053015#include <sound/pcm_params.h>
Rander Wangab2c9132020-07-16 23:09:46 +080016#include <linux/pm_runtime.h>
Vinod Koul37a2d222018-04-26 18:38:58 +053017#include <sound/soc.h>
Vinod Koul71bb8a12017-12-14 11:19:43 +053018#include <linux/soundwire/sdw_registers.h>
19#include <linux/soundwire/sdw.h>
20#include <linux/soundwire/sdw_intel.h>
21#include "cadence_master.h"
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -050022#include "bus.h"
Vinod Koul71bb8a12017-12-14 11:19:43 +053023#include "intel.h"
24
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +080025#define INTEL_MASTER_SUSPEND_DELAY_MS 3000
26
27/*
28 * debug/config flags for the Intel SoundWire Master.
29 *
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.
32 */
33
Pierre-Louis Bossarta2d9c162020-08-17 23:29:17 +080034#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)
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +080037#define SDW_INTEL_MASTER_DISABLE_MULTI_LINK BIT(3)
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +080038
39static int md_flags;
40module_param_named(sdw_md_flags, md_flags, int, 0444);
41MODULE_PARM_DESC(sdw_md_flags, "SoundWire Intel Master device flags (0x0 all off)");
42
Vinod Koul71bb8a12017-12-14 11:19:43 +053043/* Intel SHIM Registers Definition */
44#define SDW_SHIM_LCAP 0x0
45#define SDW_SHIM_LCTL 0x4
46#define SDW_SHIM_IPPTR 0x8
47#define SDW_SHIM_SYNC 0xC
48
Pierre-Louis Bossart7cc6e312019-05-01 10:57:39 -050049#define SDW_SHIM_CTLSCAP(x) (0x010 + 0x60 * (x))
50#define SDW_SHIM_CTLS0CM(x) (0x012 + 0x60 * (x))
51#define SDW_SHIM_CTLS1CM(x) (0x014 + 0x60 * (x))
52#define SDW_SHIM_CTLS2CM(x) (0x016 + 0x60 * (x))
53#define SDW_SHIM_CTLS3CM(x) (0x018 + 0x60 * (x))
54#define SDW_SHIM_PCMSCAP(x) (0x020 + 0x60 * (x))
Vinod Koul71bb8a12017-12-14 11:19:43 +053055
Pierre-Louis Bossart7cc6e312019-05-01 10:57:39 -050056#define SDW_SHIM_PCMSYCHM(x, y) (0x022 + (0x60 * (x)) + (0x2 * (y)))
57#define SDW_SHIM_PCMSYCHC(x, y) (0x042 + (0x60 * (x)) + (0x2 * (y)))
58#define SDW_SHIM_PDMSCAP(x) (0x062 + 0x60 * (x))
59#define SDW_SHIM_IOCTL(x) (0x06C + 0x60 * (x))
60#define SDW_SHIM_CTMCTL(x) (0x06E + 0x60 * (x))
Vinod Koul71bb8a12017-12-14 11:19:43 +053061
62#define SDW_SHIM_WAKEEN 0x190
63#define SDW_SHIM_WAKESTS 0x192
64
65#define SDW_SHIM_LCTL_SPA BIT(0)
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +080066#define SDW_SHIM_LCTL_SPA_MASK GENMASK(3, 0)
Vinod Koul71bb8a12017-12-14 11:19:43 +053067#define SDW_SHIM_LCTL_CPA BIT(8)
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +080068#define SDW_SHIM_LCTL_CPA_MASK GENMASK(11, 8)
Vinod Koul71bb8a12017-12-14 11:19:43 +053069
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +080070#define SDW_SHIM_SYNC_SYNCPRD_VAL_24 (24000 / SDW_CADENCE_GSYNC_KHZ - 1)
71#define SDW_SHIM_SYNC_SYNCPRD_VAL_38_4 (38400 / SDW_CADENCE_GSYNC_KHZ - 1)
Vinod Koul71bb8a12017-12-14 11:19:43 +053072#define SDW_SHIM_SYNC_SYNCPRD GENMASK(14, 0)
73#define SDW_SHIM_SYNC_SYNCCPU BIT(15)
74#define SDW_SHIM_SYNC_CMDSYNC_MASK GENMASK(19, 16)
75#define SDW_SHIM_SYNC_CMDSYNC BIT(16)
76#define SDW_SHIM_SYNC_SYNCGO BIT(24)
77
78#define SDW_SHIM_PCMSCAP_ISS GENMASK(3, 0)
79#define SDW_SHIM_PCMSCAP_OSS GENMASK(7, 4)
80#define SDW_SHIM_PCMSCAP_BSS GENMASK(12, 8)
81
82#define SDW_SHIM_PCMSYCM_LCHN GENMASK(3, 0)
83#define SDW_SHIM_PCMSYCM_HCHN GENMASK(7, 4)
84#define SDW_SHIM_PCMSYCM_STREAM GENMASK(13, 8)
85#define SDW_SHIM_PCMSYCM_DIR BIT(15)
86
87#define SDW_SHIM_PDMSCAP_ISS GENMASK(3, 0)
88#define SDW_SHIM_PDMSCAP_OSS GENMASK(7, 4)
89#define SDW_SHIM_PDMSCAP_BSS GENMASK(12, 8)
90#define SDW_SHIM_PDMSCAP_CPSS GENMASK(15, 13)
91
92#define SDW_SHIM_IOCTL_MIF BIT(0)
93#define SDW_SHIM_IOCTL_CO BIT(1)
94#define SDW_SHIM_IOCTL_COE BIT(2)
95#define SDW_SHIM_IOCTL_DO BIT(3)
96#define SDW_SHIM_IOCTL_DOE BIT(4)
97#define SDW_SHIM_IOCTL_BKE BIT(5)
98#define SDW_SHIM_IOCTL_WPDD BIT(6)
99#define SDW_SHIM_IOCTL_CIBD BIT(8)
100#define SDW_SHIM_IOCTL_DIBD BIT(9)
101
102#define SDW_SHIM_CTMCTL_DACTQE BIT(0)
103#define SDW_SHIM_CTMCTL_DODS BIT(1)
104#define SDW_SHIM_CTMCTL_DOAIS GENMASK(4, 3)
105
106#define SDW_SHIM_WAKEEN_ENABLE BIT(0)
107#define SDW_SHIM_WAKESTS_STATUS BIT(0)
108
109/* Intel ALH Register definitions */
Pierre-Louis Bossart7cc6e312019-05-01 10:57:39 -0500110#define SDW_ALH_STRMZCFG(x) (0x000 + (0x4 * (x)))
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -0500111#define SDW_ALH_NUM_STREAMS 64
Vinod Koul71bb8a12017-12-14 11:19:43 +0530112
113#define SDW_ALH_STRMZCFG_DMAT_VAL 0x3
114#define SDW_ALH_STRMZCFG_DMAT GENMASK(7, 0)
115#define SDW_ALH_STRMZCFG_CHN GENMASK(19, 16)
116
Vinod Koulc46302e2018-04-26 18:39:05 +0530117enum intel_pdi_type {
118 INTEL_PDI_IN = 0,
119 INTEL_PDI_OUT = 1,
120 INTEL_PDI_BD = 2,
121};
122
Vinod Koul71bb8a12017-12-14 11:19:43 +0530123#define cdns_to_intel(_cdns) container_of(_cdns, struct sdw_intel, cdns)
124
125/*
126 * Read, write helpers for HW registers
127 */
128static inline int intel_readl(void __iomem *base, int offset)
129{
130 return readl(base + offset);
131}
132
133static inline void intel_writel(void __iomem *base, int offset, int value)
134{
135 writel(value, base + offset);
136}
137
138static inline u16 intel_readw(void __iomem *base, int offset)
139{
140 return readw(base + offset);
141}
142
143static inline void intel_writew(void __iomem *base, int offset, u16 value)
144{
145 writew(value, base + offset);
146}
147
Pierre-Louis Bossart7d2845d2020-07-16 23:09:39 +0800148static int intel_wait_bit(void __iomem *base, int offset, u32 mask, u32 target)
Vinod Koul71bb8a12017-12-14 11:19:43 +0530149{
150 int timeout = 10;
151 u32 reg_read;
152
Vinod Koul71bb8a12017-12-14 11:19:43 +0530153 do {
154 reg_read = readl(base + offset);
Pierre-Louis Bossart7d2845d2020-07-16 23:09:39 +0800155 if ((reg_read & mask) == target)
Vinod Koul71bb8a12017-12-14 11:19:43 +0530156 return 0;
157
158 timeout--;
Pierre-Louis Bossart7d2845d2020-07-16 23:09:39 +0800159 usleep_range(50, 100);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530160 } while (timeout != 0);
161
162 return -EAGAIN;
163}
164
Pierre-Louis Bossart7d2845d2020-07-16 23:09:39 +0800165static int intel_clear_bit(void __iomem *base, int offset, u32 value, u32 mask)
166{
167 writel(value, base + offset);
168 return intel_wait_bit(base, offset, mask, 0);
169}
170
Vinod Koul71bb8a12017-12-14 11:19:43 +0530171static int intel_set_bit(void __iomem *base, int offset, u32 value, u32 mask)
172{
Vinod Koul71bb8a12017-12-14 11:19:43 +0530173 writel(value, base + offset);
Pierre-Louis Bossart7d2845d2020-07-16 23:09:39 +0800174 return intel_wait_bit(base, offset, mask, mask);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530175}
176
177/*
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -0500178 * debugfs
179 */
180#ifdef CONFIG_DEBUG_FS
181
182#define RD_BUF (2 * PAGE_SIZE)
183
184static ssize_t intel_sprintf(void __iomem *mem, bool l,
185 char *buf, size_t pos, unsigned int reg)
186{
187 int value;
188
189 if (l)
190 value = intel_readl(mem, reg);
191 else
192 value = intel_readw(mem, reg);
193
194 return scnprintf(buf + pos, RD_BUF - pos, "%4x\t%4x\n", reg, value);
195}
196
197static int intel_reg_show(struct seq_file *s_file, void *data)
198{
199 struct sdw_intel *sdw = s_file->private;
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600200 void __iomem *s = sdw->link_res->shim;
201 void __iomem *a = sdw->link_res->alh;
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -0500202 char *buf;
203 ssize_t ret;
204 int i, j;
205 unsigned int links, reg;
206
207 buf = kzalloc(RD_BUF, GFP_KERNEL);
208 if (!buf)
209 return -ENOMEM;
210
211 links = intel_readl(s, SDW_SHIM_LCAP) & GENMASK(2, 0);
212
213 ret = scnprintf(buf, RD_BUF, "Register Value\n");
214 ret += scnprintf(buf + ret, RD_BUF - ret, "\nShim\n");
215
216 for (i = 0; i < links; i++) {
217 reg = SDW_SHIM_LCAP + i * 4;
218 ret += intel_sprintf(s, true, buf, ret, reg);
219 }
220
221 for (i = 0; i < links; i++) {
222 ret += scnprintf(buf + ret, RD_BUF - ret, "\nLink%d\n", i);
223 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLSCAP(i));
224 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS0CM(i));
225 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS1CM(i));
226 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS2CM(i));
227 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS3CM(i));
228 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PCMSCAP(i));
229
230 ret += scnprintf(buf + ret, RD_BUF - ret, "\n PCMSyCH registers\n");
231
232 /*
233 * the value 10 is the number of PDIs. We will need a
234 * cleanup to remove hard-coded Intel configurations
235 * from cadence_master.c
236 */
237 for (j = 0; j < 10; j++) {
238 ret += intel_sprintf(s, false, buf, ret,
239 SDW_SHIM_PCMSYCHM(i, j));
240 ret += intel_sprintf(s, false, buf, ret,
241 SDW_SHIM_PCMSYCHC(i, j));
242 }
243 ret += scnprintf(buf + ret, RD_BUF - ret, "\n PDMSCAP, IOCTL, CTMCTL\n");
244
245 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PDMSCAP(i));
246 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_IOCTL(i));
247 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTMCTL(i));
248 }
249
250 ret += scnprintf(buf + ret, RD_BUF - ret, "\nWake registers\n");
251 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKEEN);
252 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKESTS);
253
254 ret += scnprintf(buf + ret, RD_BUF - ret, "\nALH STRMzCFG\n");
255 for (i = 0; i < SDW_ALH_NUM_STREAMS; i++)
256 ret += intel_sprintf(a, true, buf, ret, SDW_ALH_STRMZCFG(i));
257
258 seq_printf(s_file, "%s", buf);
259 kfree(buf);
260
261 return 0;
262}
263DEFINE_SHOW_ATTRIBUTE(intel_reg);
264
Pierre-Louis Bossart0f9138e2020-09-21 03:32:06 +0800265static int intel_set_m_datamode(void *data, u64 value)
266{
267 struct sdw_intel *sdw = data;
268 struct sdw_bus *bus = &sdw->cdns.bus;
269
270 if (value > SDW_PORT_DATA_MODE_STATIC_1)
271 return -EINVAL;
272
273 /* Userspace changed the hardware state behind the kernel's back */
274 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
275
276 bus->params.m_data_mode = value;
277
278 return 0;
279}
280DEFINE_DEBUGFS_ATTRIBUTE(intel_set_m_datamode_fops, NULL,
281 intel_set_m_datamode, "%llu\n");
282
283static int intel_set_s_datamode(void *data, u64 value)
284{
285 struct sdw_intel *sdw = data;
286 struct sdw_bus *bus = &sdw->cdns.bus;
287
288 if (value > SDW_PORT_DATA_MODE_STATIC_1)
289 return -EINVAL;
290
291 /* Userspace changed the hardware state behind the kernel's back */
292 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
293
294 bus->params.s_data_mode = value;
295
296 return 0;
297}
298DEFINE_DEBUGFS_ATTRIBUTE(intel_set_s_datamode_fops, NULL,
299 intel_set_s_datamode, "%llu\n");
300
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -0500301static void intel_debugfs_init(struct sdw_intel *sdw)
302{
303 struct dentry *root = sdw->cdns.bus.debugfs;
304
305 if (!root)
306 return;
307
308 sdw->debugfs = debugfs_create_dir("intel-sdw", root);
309
310 debugfs_create_file("intel-registers", 0400, sdw->debugfs, sdw,
311 &intel_reg_fops);
312
Pierre-Louis Bossart0f9138e2020-09-21 03:32:06 +0800313 debugfs_create_file("intel-m-datamode", 0200, sdw->debugfs, sdw,
314 &intel_set_m_datamode_fops);
315
316 debugfs_create_file("intel-s-datamode", 0200, sdw->debugfs, sdw,
317 &intel_set_s_datamode_fops);
318
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -0500319 sdw_cdns_debugfs_init(&sdw->cdns, sdw->debugfs);
320}
321
322static void intel_debugfs_exit(struct sdw_intel *sdw)
323{
324 debugfs_remove_recursive(sdw->debugfs);
325}
326#else
327static void intel_debugfs_init(struct sdw_intel *sdw) {}
328static void intel_debugfs_exit(struct sdw_intel *sdw) {}
329#endif /* CONFIG_DEBUG_FS */
330
331/*
Vinod Koul71bb8a12017-12-14 11:19:43 +0530332 * shim ops
333 */
334
335static int intel_link_power_up(struct sdw_intel *sdw)
336{
337 unsigned int link_id = sdw->instance;
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600338 void __iomem *shim = sdw->link_res->shim;
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800339 u32 *shim_mask = sdw->link_res->shim_mask;
340 struct sdw_bus *bus = &sdw->cdns.bus;
341 struct sdw_master_prop *prop = &bus->prop;
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800342 u32 spa_mask, cpa_mask;
343 u32 link_control;
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800344 int ret = 0;
345 u32 syncprd;
346 u32 sync_reg;
347
348 mutex_lock(sdw->link_res->shim_lock);
349
350 /*
351 * The hardware relies on an internal counter, typically 4kHz,
352 * to generate the SoundWire SSP - which defines a 'safe'
353 * synchronization point between commands and audio transport
354 * and allows for multi link synchronization. The SYNCPRD value
355 * is only dependent on the oscillator clock provided to
356 * the IP, so adjust based on _DSD properties reported in DSDT
357 * tables. The values reported are based on either 24MHz
358 * (CNL/CML) or 38.4 MHz (ICL/TGL+).
359 */
360 if (prop->mclk_freq % 6000000)
361 syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_38_4;
362 else
363 syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24;
364
365 if (!*shim_mask) {
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800366 dev_dbg(sdw->cdns.dev, "%s: powering up all links\n", __func__);
367
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800368 /* we first need to program the SyncPRD/CPU registers */
369 dev_dbg(sdw->cdns.dev,
370 "%s: first link up, programming SYNCPRD\n", __func__);
371
372 /* set SyncPRD period */
373 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
Vinod Koulf067c922020-09-17 17:31:46 +0530374 u32p_replace_bits(&sync_reg, syncprd, SDW_SHIM_SYNC_SYNCPRD);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800375
376 /* Set SyncCPU bit */
377 sync_reg |= SDW_SHIM_SYNC_SYNCCPU;
378 intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530379
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800380 /* Link power up sequence */
381 link_control = intel_readl(shim, SDW_SHIM_LCTL);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530382
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800383 /* only power-up enabled links */
Vinod Koul3b4979c2020-09-03 17:15:02 +0530384 spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, sdw->link_res->link_mask);
385 cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800386
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800387 link_control |= spa_mask;
388
389 ret = intel_set_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
390 if (ret < 0) {
391 dev_err(sdw->cdns.dev, "Failed to power up link: %d\n", ret);
392 goto out;
393 }
394
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800395 /* SyncCPU will change once link is active */
396 ret = intel_wait_bit(shim, SDW_SHIM_SYNC,
397 SDW_SHIM_SYNC_SYNCCPU, 0);
398 if (ret < 0) {
399 dev_err(sdw->cdns.dev,
400 "Failed to set SHIM_SYNC: %d\n", ret);
401 goto out;
402 }
403 }
404
405 *shim_mask |= BIT(link_id);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530406
407 sdw->cdns.link_up = true;
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800408out:
409 mutex_unlock(sdw->link_res->shim_lock);
410
411 return ret;
Vinod Koul71bb8a12017-12-14 11:19:43 +0530412}
413
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800414/* this needs to be called with shim_lock */
415static void intel_shim_glue_to_master_ip(struct sdw_intel *sdw)
Vinod Koul71bb8a12017-12-14 11:19:43 +0530416{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600417 void __iomem *shim = sdw->link_res->shim;
Vinod Koul71bb8a12017-12-14 11:19:43 +0530418 unsigned int link_id = sdw->instance;
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800419 u16 ioctl;
Vinod Koul71bb8a12017-12-14 11:19:43 +0530420
421 /* Switch to MIP from Glue logic */
422 ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
423
424 ioctl &= ~(SDW_SHIM_IOCTL_DOE);
425 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800426 usleep_range(10, 15);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530427
428 ioctl &= ~(SDW_SHIM_IOCTL_DO);
429 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800430 usleep_range(10, 15);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530431
432 ioctl |= (SDW_SHIM_IOCTL_MIF);
433 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800434 usleep_range(10, 15);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530435
436 ioctl &= ~(SDW_SHIM_IOCTL_BKE);
437 ioctl &= ~(SDW_SHIM_IOCTL_COE);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530438 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800439 usleep_range(10, 15);
440
441 /* at this point Master IP has full control of the I/Os */
442}
443
444/* this needs to be called with shim_lock */
445static void intel_shim_master_ip_to_glue(struct sdw_intel *sdw)
446{
447 unsigned int link_id = sdw->instance;
448 void __iomem *shim = sdw->link_res->shim;
449 u16 ioctl;
450
451 /* Glue logic */
452 ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
453 ioctl |= SDW_SHIM_IOCTL_BKE;
454 ioctl |= SDW_SHIM_IOCTL_COE;
455 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
456 usleep_range(10, 15);
457
458 ioctl &= ~(SDW_SHIM_IOCTL_MIF);
459 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
460 usleep_range(10, 15);
461
462 /* at this point Integration Glue has full control of the I/Os */
463}
464
465static int intel_shim_init(struct sdw_intel *sdw, bool clock_stop)
466{
467 void __iomem *shim = sdw->link_res->shim;
468 unsigned int link_id = sdw->instance;
469 int ret = 0;
470 u16 ioctl = 0, act = 0;
471
472 mutex_lock(sdw->link_res->shim_lock);
473
474 /* Initialize Shim */
475 ioctl |= SDW_SHIM_IOCTL_BKE;
476 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
477 usleep_range(10, 15);
478
479 ioctl |= SDW_SHIM_IOCTL_WPDD;
480 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
481 usleep_range(10, 15);
482
483 ioctl |= SDW_SHIM_IOCTL_DO;
484 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
485 usleep_range(10, 15);
486
487 ioctl |= SDW_SHIM_IOCTL_DOE;
488 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
489 usleep_range(10, 15);
490
491 intel_shim_glue_to_master_ip(sdw);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530492
Vinod Koulf067c922020-09-17 17:31:46 +0530493 u16p_replace_bits(&act, 0x1, SDW_SHIM_CTMCTL_DOAIS);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530494 act |= SDW_SHIM_CTMCTL_DACTQE;
495 act |= SDW_SHIM_CTMCTL_DODS;
496 intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800497 usleep_range(10, 15);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530498
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800499 mutex_unlock(sdw->link_res->shim_lock);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530500
Vinod Koul71bb8a12017-12-14 11:19:43 +0530501 return ret;
502}
503
Rander Wangab2c9132020-07-16 23:09:46 +0800504static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800505{
506 void __iomem *shim = sdw->link_res->shim;
507 unsigned int link_id = sdw->instance;
508 u16 wake_en, wake_sts;
509
510 mutex_lock(sdw->link_res->shim_lock);
511 wake_en = intel_readw(shim, SDW_SHIM_WAKEEN);
512
513 if (wake_enable) {
514 /* Enable the wakeup */
515 wake_en |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
516 intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
517 } else {
518 /* Disable the wake up interrupt */
519 wake_en &= ~(SDW_SHIM_WAKEEN_ENABLE << link_id);
520 intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
521
522 /* Clear wake status */
523 wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
524 wake_sts |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
525 intel_writew(shim, SDW_SHIM_WAKESTS_STATUS, wake_sts);
526 }
527 mutex_unlock(sdw->link_res->shim_lock);
528}
529
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +0800530static int intel_link_power_down(struct sdw_intel *sdw)
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800531{
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800532 u32 link_control, spa_mask, cpa_mask;
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800533 unsigned int link_id = sdw->instance;
534 void __iomem *shim = sdw->link_res->shim;
535 u32 *shim_mask = sdw->link_res->shim_mask;
536 int ret = 0;
537
538 mutex_lock(sdw->link_res->shim_lock);
539
540 intel_shim_master_ip_to_glue(sdw);
541
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800542 if (!(*shim_mask & BIT(link_id)))
543 dev_err(sdw->cdns.dev,
544 "%s: Unbalanced power-up/down calls\n", __func__);
545
546 *shim_mask &= ~BIT(link_id);
547
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800548 if (!*shim_mask) {
549
550 dev_dbg(sdw->cdns.dev, "%s: powering down all links\n", __func__);
551
552 /* Link power down sequence */
553 link_control = intel_readl(shim, SDW_SHIM_LCTL);
554
555 /* only power-down enabled links */
Vinod Koul3b4979c2020-09-03 17:15:02 +0530556 spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, ~sdw->link_res->link_mask);
557 cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800558
559 link_control &= spa_mask;
560
561 ret = intel_clear_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
562 }
563
564 link_control = intel_readl(shim, SDW_SHIM_LCTL);
565
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800566 mutex_unlock(sdw->link_res->shim_lock);
567
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800568 if (ret < 0) {
569 dev_err(sdw->cdns.dev, "%s: could not power down link\n", __func__);
570
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800571 return ret;
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800572 }
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800573
574 sdw->cdns.link_up = false;
575 return 0;
576}
577
Pierre-Louis Bossart02629e42020-07-16 23:09:41 +0800578static void intel_shim_sync_arm(struct sdw_intel *sdw)
579{
580 void __iomem *shim = sdw->link_res->shim;
581 u32 sync_reg;
582
583 mutex_lock(sdw->link_res->shim_lock);
584
585 /* update SYNC register */
586 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
587 sync_reg |= (SDW_SHIM_SYNC_CMDSYNC << sdw->instance);
588 intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
589
590 mutex_unlock(sdw->link_res->shim_lock);
591}
592
Pierre-Louis Bossart437e3282020-07-16 23:09:42 +0800593static int intel_shim_sync_go_unlocked(struct sdw_intel *sdw)
594{
595 void __iomem *shim = sdw->link_res->shim;
596 u32 sync_reg;
597 int ret;
598
599 /* Read SYNC register */
600 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
601
602 /*
603 * Set SyncGO bit to synchronously trigger a bank switch for
604 * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all
605 * the Masters.
606 */
607 sync_reg |= SDW_SHIM_SYNC_SYNCGO;
608
609 ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg,
610 SDW_SHIM_SYNC_SYNCGO);
611
612 if (ret < 0)
613 dev_err(sdw->cdns.dev, "SyncGO clear failed: %d\n", ret);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530614
615 return ret;
616}
617
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +0800618static int intel_shim_sync_go(struct sdw_intel *sdw)
619{
620 int ret;
621
622 mutex_lock(sdw->link_res->shim_lock);
623
624 ret = intel_shim_sync_go_unlocked(sdw);
625
626 mutex_unlock(sdw->link_res->shim_lock);
627
628 return ret;
629}
630
Vinod Koul37a2d222018-04-26 18:38:58 +0530631/*
632 * PDI routines
633 */
634static void intel_pdi_init(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500635 struct sdw_cdns_stream_config *config)
Vinod Koul37a2d222018-04-26 18:38:58 +0530636{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600637 void __iomem *shim = sdw->link_res->shim;
Vinod Koul37a2d222018-04-26 18:38:58 +0530638 unsigned int link_id = sdw->instance;
639 int pcm_cap, pdm_cap;
640
641 /* PCM Stream Capability */
642 pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id));
643
Vinod Koul3b4979c2020-09-03 17:15:02 +0530644 config->pcm_bd = FIELD_GET(SDW_SHIM_PCMSCAP_BSS, pcm_cap);
645 config->pcm_in = FIELD_GET(SDW_SHIM_PCMSCAP_ISS, pcm_cap);
646 config->pcm_out = FIELD_GET(SDW_SHIM_PCMSCAP_OSS, pcm_cap);
Vinod Koul37a2d222018-04-26 18:38:58 +0530647
Pierre-Louis Bossart121f4362019-05-22 14:47:29 -0500648 dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
649 config->pcm_bd, config->pcm_in, config->pcm_out);
650
Vinod Koul37a2d222018-04-26 18:38:58 +0530651 /* PDM Stream Capability */
652 pdm_cap = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
653
Vinod Koul3b4979c2020-09-03 17:15:02 +0530654 config->pdm_bd = FIELD_GET(SDW_SHIM_PDMSCAP_BSS, pdm_cap);
655 config->pdm_in = FIELD_GET(SDW_SHIM_PDMSCAP_ISS, pdm_cap);
656 config->pdm_out = FIELD_GET(SDW_SHIM_PDMSCAP_OSS, pdm_cap);
Pierre-Louis Bossart121f4362019-05-22 14:47:29 -0500657
658 dev_dbg(sdw->cdns.dev, "PDM cap bd:%d in:%d out:%d\n",
659 config->pdm_bd, config->pdm_in, config->pdm_out);
Vinod Koul37a2d222018-04-26 18:38:58 +0530660}
661
662static int
663intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num, bool pcm)
664{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600665 void __iomem *shim = sdw->link_res->shim;
Vinod Koul37a2d222018-04-26 18:38:58 +0530666 unsigned int link_id = sdw->instance;
667 int count;
668
669 if (pcm) {
670 count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num));
Pierre-Louis Bossart18046332019-08-05 19:55:07 -0500671
672 /*
673 * WORKAROUND: on all existing Intel controllers, pdi
674 * number 2 reports channel count as 1 even though it
675 * supports 8 channels. Performing hardcoding for pdi
676 * number 2.
677 */
678 if (pdi_num == 2)
679 count = 7;
680
Vinod Koul37a2d222018-04-26 18:38:58 +0530681 } else {
682 count = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
Vinod Koul3b4979c2020-09-03 17:15:02 +0530683 count = FIELD_GET(SDW_SHIM_PDMSCAP_CPSS, count);
Vinod Koul37a2d222018-04-26 18:38:58 +0530684 }
685
686 /* zero based values for channel count in register */
687 count++;
688
689 return count;
690}
691
692static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500693 struct sdw_cdns_pdi *pdi,
694 unsigned int num_pdi,
695 unsigned int *num_ch, bool pcm)
Vinod Koul37a2d222018-04-26 18:38:58 +0530696{
697 int i, ch_count = 0;
698
699 for (i = 0; i < num_pdi; i++) {
700 pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num, pcm);
701 ch_count += pdi->ch_count;
702 pdi++;
703 }
704
705 *num_ch = ch_count;
706 return 0;
707}
708
709static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500710 struct sdw_cdns_streams *stream, bool pcm)
Vinod Koul37a2d222018-04-26 18:38:58 +0530711{
712 intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500713 &stream->num_ch_bd, pcm);
Vinod Koul37a2d222018-04-26 18:38:58 +0530714
715 intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500716 &stream->num_ch_in, pcm);
Vinod Koul37a2d222018-04-26 18:38:58 +0530717
718 intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500719 &stream->num_ch_out, pcm);
Vinod Koul37a2d222018-04-26 18:38:58 +0530720
721 return 0;
722}
723
724static int intel_pdi_ch_update(struct sdw_intel *sdw)
725{
726 /* First update PCM streams followed by PDM streams */
727 intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm, true);
728 intel_pdi_stream_ch_update(sdw, &sdw->cdns.pdm, false);
729
730 return 0;
731}
732
733static void
734intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
735{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600736 void __iomem *shim = sdw->link_res->shim;
Vinod Koul37a2d222018-04-26 18:38:58 +0530737 unsigned int link_id = sdw->instance;
738 int pdi_conf = 0;
739
Pierre-Louis Bossartc134f912019-10-22 18:29:48 -0500740 /* the Bulk and PCM streams are not contiguous */
741 pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
742 if (pdi->num >= 2)
743 pdi->intel_alh_id += 2;
Vinod Koul37a2d222018-04-26 18:38:58 +0530744
745 /*
746 * Program stream parameters to stream SHIM register
747 * This is applicable for PCM stream only.
748 */
749 if (pdi->type != SDW_STREAM_PCM)
750 return;
751
752 if (pdi->dir == SDW_DATA_DIR_RX)
753 pdi_conf |= SDW_SHIM_PCMSYCM_DIR;
754 else
755 pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR);
756
Vinod Koulf067c922020-09-17 17:31:46 +0530757 u32p_replace_bits(&pdi_conf, pdi->intel_alh_id, SDW_SHIM_PCMSYCM_STREAM);
758 u32p_replace_bits(&pdi_conf, pdi->l_ch_num, SDW_SHIM_PCMSYCM_LCHN);
759 u32p_replace_bits(&pdi_conf, pdi->h_ch_num, SDW_SHIM_PCMSYCM_HCHN);
Vinod Koul37a2d222018-04-26 18:38:58 +0530760
761 intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
762}
763
764static void
765intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
766{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600767 void __iomem *alh = sdw->link_res->alh;
Vinod Koul37a2d222018-04-26 18:38:58 +0530768 unsigned int link_id = sdw->instance;
769 unsigned int conf;
770
Pierre-Louis Bossartc134f912019-10-22 18:29:48 -0500771 /* the Bulk and PCM streams are not contiguous */
772 pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
773 if (pdi->num >= 2)
774 pdi->intel_alh_id += 2;
Vinod Koul37a2d222018-04-26 18:38:58 +0530775
776 /* Program Stream config ALH register */
777 conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
778
Vinod Koulf067c922020-09-17 17:31:46 +0530779 u32p_replace_bits(&conf, SDW_ALH_STRMZCFG_DMAT_VAL, SDW_ALH_STRMZCFG_DMAT);
780 u32p_replace_bits(&conf, pdi->ch_count - 1, SDW_ALH_STRMZCFG_CHN);
Vinod Koul37a2d222018-04-26 18:38:58 +0530781
782 intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
783}
784
Rander Wang4b206d32019-12-11 19:45:02 -0600785static int intel_params_stream(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500786 struct snd_pcm_substream *substream,
787 struct snd_soc_dai *dai,
Rander Wang4b206d32019-12-11 19:45:02 -0600788 struct snd_pcm_hw_params *hw_params,
789 int link_id, int alh_stream_id)
Vinod Koulc46302e2018-04-26 18:39:05 +0530790{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600791 struct sdw_intel_link_res *res = sdw->link_res;
Rander Wang4b206d32019-12-11 19:45:02 -0600792 struct sdw_intel_stream_params_data params_data;
Pierre-Louis Bossart05c8afe42019-08-05 19:55:06 -0500793
Rander Wang4b206d32019-12-11 19:45:02 -0600794 params_data.substream = substream;
795 params_data.dai = dai;
796 params_data.hw_params = hw_params;
797 params_data.link_id = link_id;
798 params_data.alh_stream_id = alh_stream_id;
Vinod Koulc46302e2018-04-26 18:39:05 +0530799
Rander Wang4b206d32019-12-11 19:45:02 -0600800 if (res->ops && res->ops->params_stream && res->dev)
801 return res->ops->params_stream(res->dev,
802 &params_data);
Vinod Koulc46302e2018-04-26 18:39:05 +0530803 return -EIO;
804}
805
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -0600806static int intel_free_stream(struct sdw_intel *sdw,
807 struct snd_pcm_substream *substream,
808 struct snd_soc_dai *dai,
809 int link_id)
810{
811 struct sdw_intel_link_res *res = sdw->link_res;
812 struct sdw_intel_stream_free_data free_data;
813
814 free_data.substream = substream;
815 free_data.dai = dai;
816 free_data.link_id = link_id;
817
818 if (res->ops && res->ops->free_stream && res->dev)
819 return res->ops->free_stream(res->dev,
820 &free_data);
821
822 return 0;
823}
824
Vinod Koulc46302e2018-04-26 18:39:05 +0530825/*
Shreyas NC30246e22018-07-27 14:44:17 +0530826 * bank switch routines
827 */
828
829static int intel_pre_bank_switch(struct sdw_bus *bus)
830{
831 struct sdw_cdns *cdns = bus_to_cdns(bus);
832 struct sdw_intel *sdw = cdns_to_intel(cdns);
Shreyas NC30246e22018-07-27 14:44:17 +0530833
834 /* Write to register only for multi-link */
835 if (!bus->multi_link)
836 return 0;
837
Pierre-Louis Bossart02629e42020-07-16 23:09:41 +0800838 intel_shim_sync_arm(sdw);
Shreyas NC30246e22018-07-27 14:44:17 +0530839
840 return 0;
841}
842
843static int intel_post_bank_switch(struct sdw_bus *bus)
844{
845 struct sdw_cdns *cdns = bus_to_cdns(bus);
846 struct sdw_intel *sdw = cdns_to_intel(cdns);
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600847 void __iomem *shim = sdw->link_res->shim;
Shreyas NC30246e22018-07-27 14:44:17 +0530848 int sync_reg, ret;
849
850 /* Write to register only for multi-link */
851 if (!bus->multi_link)
852 return 0;
853
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800854 mutex_lock(sdw->link_res->shim_lock);
855
Shreyas NC30246e22018-07-27 14:44:17 +0530856 /* Read SYNC register */
857 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
858
859 /*
860 * post_bank_switch() ops is called from the bus in loop for
861 * all the Masters in the steam with the expectation that
862 * we trigger the bankswitch for the only first Master in the list
863 * and do nothing for the other Masters
864 *
865 * So, set the SYNCGO bit only if CMDSYNC bit is set for any Master.
866 */
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800867 if (!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK)) {
868 ret = 0;
869 goto unlock;
870 }
Shreyas NC30246e22018-07-27 14:44:17 +0530871
Pierre-Louis Bossart437e3282020-07-16 23:09:42 +0800872 ret = intel_shim_sync_go_unlocked(sdw);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800873unlock:
874 mutex_unlock(sdw->link_res->shim_lock);
Shreyas NC30246e22018-07-27 14:44:17 +0530875
Shreyas NC30246e22018-07-27 14:44:17 +0530876 if (ret < 0)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500877 dev_err(sdw->cdns.dev, "Post bank switch failed: %d\n", ret);
Shreyas NC30246e22018-07-27 14:44:17 +0530878
879 return ret;
880}
881
882/*
Vinod Koulc46302e2018-04-26 18:39:05 +0530883 * DAI routines
884 */
885
Rander Wang5e7484d2020-02-14 19:47:39 -0600886static int intel_startup(struct snd_pcm_substream *substream,
887 struct snd_soc_dai *dai)
888{
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +0800889 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
890 int ret;
891
892 ret = pm_runtime_get_sync(cdns->dev);
893 if (ret < 0 && ret != -EACCES) {
894 dev_err_ratelimited(cdns->dev,
895 "pm_runtime_get_sync failed in %s, ret %d\n",
896 __func__, ret);
897 pm_runtime_put_noidle(cdns->dev);
898 return ret;
899 }
Pierre-Louis Bossartff16d1e2020-07-01 02:43:54 +0800900 return 0;
Rander Wang5e7484d2020-02-14 19:47:39 -0600901}
902
Vinod Koulc46302e2018-04-26 18:39:05 +0530903static int intel_hw_params(struct snd_pcm_substream *substream,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500904 struct snd_pcm_hw_params *params,
905 struct snd_soc_dai *dai)
Vinod Koulc46302e2018-04-26 18:39:05 +0530906{
907 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
908 struct sdw_intel *sdw = cdns_to_intel(cdns);
909 struct sdw_cdns_dma_data *dma;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500910 struct sdw_cdns_pdi *pdi;
Vinod Koulc46302e2018-04-26 18:39:05 +0530911 struct sdw_stream_config sconfig;
912 struct sdw_port_config *pconfig;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500913 int ch, dir;
914 int ret;
Vinod Koulc46302e2018-04-26 18:39:05 +0530915 bool pcm = true;
916
917 dma = snd_soc_dai_get_dma_data(dai, substream);
918 if (!dma)
919 return -EIO;
920
921 ch = params_channels(params);
922 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
923 dir = SDW_DATA_DIR_RX;
924 else
925 dir = SDW_DATA_DIR_TX;
926
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500927 if (dma->stream_type == SDW_STREAM_PDM)
Vinod Koulc46302e2018-04-26 18:39:05 +0530928 pcm = false;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500929
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500930 if (pcm)
Bard Liao1b533852019-09-16 14:23:48 -0500931 pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500932 else
Bard Liao1b533852019-09-16 14:23:48 -0500933 pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pdm, ch, dir, dai->id);
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500934
935 if (!pdi) {
936 ret = -EINVAL;
937 goto error;
Vinod Koulc46302e2018-04-26 18:39:05 +0530938 }
939
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500940 /* do run-time configurations for SHIM, ALH and PDI/PORT */
941 intel_pdi_shim_configure(sdw, pdi);
942 intel_pdi_alh_configure(sdw, pdi);
943 sdw_cdns_config_stream(cdns, ch, dir, pdi);
Vinod Koulc46302e2018-04-26 18:39:05 +0530944
Bard Liaoa5a02392020-08-17 23:29:16 +0800945 /* store pdi and hw_params, may be needed in prepare step */
946 dma->suspended = false;
947 dma->pdi = pdi;
948 dma->hw_params = params;
Vinod Koulc46302e2018-04-26 18:39:05 +0530949
950 /* Inform DSP about PDI stream number */
Rander Wang4b206d32019-12-11 19:45:02 -0600951 ret = intel_params_stream(sdw, substream, dai, params,
952 sdw->instance,
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500953 pdi->intel_alh_id);
954 if (ret)
955 goto error;
Vinod Koulc46302e2018-04-26 18:39:05 +0530956
957 sconfig.direction = dir;
958 sconfig.ch_count = ch;
959 sconfig.frame_rate = params_rate(params);
960 sconfig.type = dma->stream_type;
961
962 if (dma->stream_type == SDW_STREAM_PDM) {
963 sconfig.frame_rate *= 50;
964 sconfig.bps = 1;
965 } else {
966 sconfig.bps = snd_pcm_format_width(params_format(params));
967 }
968
969 /* Port configuration */
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500970 pconfig = kcalloc(1, sizeof(*pconfig), GFP_KERNEL);
Vinod Koulc46302e2018-04-26 18:39:05 +0530971 if (!pconfig) {
972 ret = -ENOMEM;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500973 goto error;
Vinod Koulc46302e2018-04-26 18:39:05 +0530974 }
975
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500976 pconfig->num = pdi->num;
977 pconfig->ch_mask = (1 << ch) - 1;
Vinod Koulc46302e2018-04-26 18:39:05 +0530978
979 ret = sdw_stream_add_master(&cdns->bus, &sconfig,
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500980 pconfig, 1, dma->stream);
981 if (ret)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500982 dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
Vinod Koulc46302e2018-04-26 18:39:05 +0530983
984 kfree(pconfig);
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500985error:
Vinod Koulc46302e2018-04-26 18:39:05 +0530986 return ret;
987}
988
Rander Wang27b198f2020-02-14 19:47:37 -0600989static int intel_prepare(struct snd_pcm_substream *substream,
990 struct snd_soc_dai *dai)
991{
Bard Liaoa5a02392020-08-17 23:29:16 +0800992 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
993 struct sdw_intel *sdw = cdns_to_intel(cdns);
Rander Wang27b198f2020-02-14 19:47:37 -0600994 struct sdw_cdns_dma_data *dma;
Bard Liaoa5a02392020-08-17 23:29:16 +0800995 int ch, dir;
Pierre-Louis Bossart244eb882020-09-04 04:47:39 +0800996 int ret = 0;
Rander Wang27b198f2020-02-14 19:47:37 -0600997
998 dma = snd_soc_dai_get_dma_data(dai, substream);
999 if (!dma) {
1000 dev_err(dai->dev, "failed to get dma data in %s",
1001 __func__);
1002 return -EIO;
1003 }
1004
Bard Liaoa5a02392020-08-17 23:29:16 +08001005 if (dma->suspended) {
1006 dma->suspended = false;
1007
1008 /*
1009 * .prepare() is called after system resume, where we
1010 * need to reinitialize the SHIM/ALH/Cadence IP.
1011 * .prepare() is also called to deal with underflows,
1012 * but in those cases we cannot touch ALH/SHIM
1013 * registers
1014 */
1015
1016 /* configure stream */
1017 ch = params_channels(dma->hw_params);
1018 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1019 dir = SDW_DATA_DIR_RX;
1020 else
1021 dir = SDW_DATA_DIR_TX;
1022
1023 intel_pdi_shim_configure(sdw, dma->pdi);
1024 intel_pdi_alh_configure(sdw, dma->pdi);
1025 sdw_cdns_config_stream(cdns, ch, dir, dma->pdi);
1026
1027 /* Inform DSP about PDI stream number */
1028 ret = intel_params_stream(sdw, substream, dai,
1029 dma->hw_params,
1030 sdw->instance,
1031 dma->pdi->intel_alh_id);
Bard Liaoa5a02392020-08-17 23:29:16 +08001032 }
1033
Bard Liaoa5a02392020-08-17 23:29:16 +08001034 return ret;
Rander Wang27b198f2020-02-14 19:47:37 -06001035}
1036
Vinod Koulc46302e2018-04-26 18:39:05 +05301037static int
1038intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
1039{
1040 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001041 struct sdw_intel *sdw = cdns_to_intel(cdns);
Vinod Koulc46302e2018-04-26 18:39:05 +05301042 struct sdw_cdns_dma_data *dma;
1043 int ret;
1044
1045 dma = snd_soc_dai_get_dma_data(dai, substream);
1046 if (!dma)
1047 return -EIO;
1048
Pierre-Louis Bossart244eb882020-09-04 04:47:39 +08001049 /*
1050 * The sdw stream state will transition to RELEASED when stream->
1051 * master_list is empty. So the stream state will transition to
1052 * DEPREPARED for the first cpu-dai and to RELEASED for the last
1053 * cpu-dai.
1054 */
Vinod Koulc46302e2018-04-26 18:39:05 +05301055 ret = sdw_stream_remove_master(&cdns->bus, dma->stream);
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001056 if (ret < 0) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001057 dev_err(dai->dev, "remove master from stream %s failed: %d\n",
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001058 dma->stream->name, ret);
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001059 return ret;
1060 }
Vinod Koulc46302e2018-04-26 18:39:05 +05301061
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001062 ret = intel_free_stream(sdw, substream, dai, sdw->instance);
1063 if (ret < 0) {
1064 dev_err(dai->dev, "intel_free_stream: failed %d", ret);
1065 return ret;
1066 }
1067
Bard Liaoa5a02392020-08-17 23:29:16 +08001068 dma->hw_params = NULL;
1069 dma->pdi = NULL;
1070
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001071 return 0;
Vinod Koulc46302e2018-04-26 18:39:05 +05301072}
1073
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -05001074static void intel_shutdown(struct snd_pcm_substream *substream,
1075 struct snd_soc_dai *dai)
1076{
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001077 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -05001078
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001079 pm_runtime_mark_last_busy(cdns->dev);
1080 pm_runtime_put_autosuspend(cdns->dev);
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -05001081}
1082
Bard Liaoa5a02392020-08-17 23:29:16 +08001083static int intel_component_dais_suspend(struct snd_soc_component *component)
1084{
1085 struct sdw_cdns_dma_data *dma;
1086 struct snd_soc_dai *dai;
1087
1088 for_each_component_dais(component, dai) {
1089 /*
1090 * we don't have a .suspend dai_ops, and we don't have access
1091 * to the substream, so let's mark both capture and playback
1092 * DMA contexts as suspended
1093 */
1094 dma = dai->playback_dma_data;
1095 if (dma)
1096 dma->suspended = true;
1097
1098 dma = dai->capture_dma_data;
1099 if (dma)
1100 dma->suspended = true;
1101 }
1102
1103 return 0;
1104}
1105
Vinod Koulc46302e2018-04-26 18:39:05 +05301106static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001107 void *stream, int direction)
Vinod Koulc46302e2018-04-26 18:39:05 +05301108{
1109 return cdns_set_sdw_stream(dai, stream, true, direction);
1110}
1111
1112static int intel_pdm_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001113 void *stream, int direction)
Vinod Koulc46302e2018-04-26 18:39:05 +05301114{
1115 return cdns_set_sdw_stream(dai, stream, false, direction);
1116}
1117
Pierre-Louis Bossart09553142020-07-01 02:43:52 +08001118static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
1119 int direction)
1120{
1121 struct sdw_cdns_dma_data *dma;
1122
1123 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1124 dma = dai->playback_dma_data;
1125 else
1126 dma = dai->capture_dma_data;
1127
1128 if (!dma)
Pierre-Louis Bossart06dcb4e2020-09-04 04:47:37 +08001129 return ERR_PTR(-EINVAL);
Pierre-Louis Bossart09553142020-07-01 02:43:52 +08001130
1131 return dma->stream;
1132}
1133
Julia Lawallb1635592018-10-27 15:34:42 +02001134static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
Rander Wang5e7484d2020-02-14 19:47:39 -06001135 .startup = intel_startup,
Vinod Koulc46302e2018-04-26 18:39:05 +05301136 .hw_params = intel_hw_params,
Rander Wang27b198f2020-02-14 19:47:37 -06001137 .prepare = intel_prepare,
Vinod Koulc46302e2018-04-26 18:39:05 +05301138 .hw_free = intel_hw_free,
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -05001139 .shutdown = intel_shutdown,
Vinod Koulc46302e2018-04-26 18:39:05 +05301140 .set_sdw_stream = intel_pcm_set_sdw_stream,
Pierre-Louis Bossart09553142020-07-01 02:43:52 +08001141 .get_sdw_stream = intel_get_sdw_stream,
Vinod Koulc46302e2018-04-26 18:39:05 +05301142};
1143
Julia Lawallb1635592018-10-27 15:34:42 +02001144static const struct snd_soc_dai_ops intel_pdm_dai_ops = {
Rander Wang5e7484d2020-02-14 19:47:39 -06001145 .startup = intel_startup,
Vinod Koulc46302e2018-04-26 18:39:05 +05301146 .hw_params = intel_hw_params,
Rander Wang27b198f2020-02-14 19:47:37 -06001147 .prepare = intel_prepare,
Vinod Koulc46302e2018-04-26 18:39:05 +05301148 .hw_free = intel_hw_free,
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -05001149 .shutdown = intel_shutdown,
Vinod Koulc46302e2018-04-26 18:39:05 +05301150 .set_sdw_stream = intel_pdm_set_sdw_stream,
Pierre-Louis Bossart09553142020-07-01 02:43:52 +08001151 .get_sdw_stream = intel_get_sdw_stream,
Vinod Koulc46302e2018-04-26 18:39:05 +05301152};
1153
1154static const struct snd_soc_component_driver dai_component = {
1155 .name = "soundwire",
Bard Liaoa5a02392020-08-17 23:29:16 +08001156 .suspend = intel_component_dais_suspend
Vinod Koulc46302e2018-04-26 18:39:05 +05301157};
1158
1159static int intel_create_dai(struct sdw_cdns *cdns,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001160 struct snd_soc_dai_driver *dais,
1161 enum intel_pdi_type type,
1162 u32 num, u32 off, u32 max_ch, bool pcm)
Vinod Koulc46302e2018-04-26 18:39:05 +05301163{
1164 int i;
1165
1166 if (num == 0)
1167 return 0;
1168
1169 /* TODO: Read supported rates/formats from hardware */
1170 for (i = off; i < (off + num); i++) {
Pierre-Louis Bossartbf6d6e62020-06-17 11:35:36 -05001171 dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
1172 "SDW%d Pin%d",
1173 cdns->instance, i);
Vinod Koulc46302e2018-04-26 18:39:05 +05301174 if (!dais[i].name)
1175 return -ENOMEM;
1176
1177 if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
Vinod Koulc46302e2018-04-26 18:39:05 +05301178 dais[i].playback.channels_min = 1;
1179 dais[i].playback.channels_max = max_ch;
1180 dais[i].playback.rates = SNDRV_PCM_RATE_48000;
1181 dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
1182 }
1183
1184 if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
Srinivas Kandagatla39194122019-06-06 12:23:04 +01001185 dais[i].capture.channels_min = 1;
1186 dais[i].capture.channels_max = max_ch;
Vinod Koulc46302e2018-04-26 18:39:05 +05301187 dais[i].capture.rates = SNDRV_PCM_RATE_48000;
1188 dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
1189 }
1190
Vinod Koulc46302e2018-04-26 18:39:05 +05301191 if (pcm)
1192 dais[i].ops = &intel_pcm_dai_ops;
1193 else
1194 dais[i].ops = &intel_pdm_dai_ops;
1195 }
1196
1197 return 0;
1198}
1199
1200static int intel_register_dai(struct sdw_intel *sdw)
1201{
1202 struct sdw_cdns *cdns = &sdw->cdns;
1203 struct sdw_cdns_streams *stream;
1204 struct snd_soc_dai_driver *dais;
1205 int num_dai, ret, off = 0;
1206
1207 /* DAIs are created based on total number of PDIs supported */
1208 num_dai = cdns->pcm.num_pdi + cdns->pdm.num_pdi;
1209
1210 dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
1211 if (!dais)
1212 return -ENOMEM;
1213
1214 /* Create PCM DAIs */
1215 stream = &cdns->pcm;
1216
Bard Liaocf924962019-09-16 14:23:43 -05001217 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
Vinod Koul1215dae2019-05-02 16:29:25 +05301218 off, stream->num_ch_in, true);
Vinod Koulc46302e2018-04-26 18:39:05 +05301219 if (ret)
1220 return ret;
1221
1222 off += cdns->pcm.num_in;
Vinod Koul1215dae2019-05-02 16:29:25 +05301223 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
1224 off, stream->num_ch_out, true);
Vinod Koulc46302e2018-04-26 18:39:05 +05301225 if (ret)
1226 return ret;
1227
1228 off += cdns->pcm.num_out;
Vinod Koul1215dae2019-05-02 16:29:25 +05301229 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
1230 off, stream->num_ch_bd, true);
Vinod Koulc46302e2018-04-26 18:39:05 +05301231 if (ret)
1232 return ret;
1233
1234 /* Create PDM DAIs */
1235 stream = &cdns->pdm;
1236 off += cdns->pcm.num_bd;
Vinod Koul1215dae2019-05-02 16:29:25 +05301237 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pdm.num_in,
1238 off, stream->num_ch_in, false);
Vinod Koulc46302e2018-04-26 18:39:05 +05301239 if (ret)
1240 return ret;
1241
1242 off += cdns->pdm.num_in;
Vinod Koul1215dae2019-05-02 16:29:25 +05301243 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pdm.num_out,
1244 off, stream->num_ch_out, false);
Vinod Koulc46302e2018-04-26 18:39:05 +05301245 if (ret)
1246 return ret;
1247
Bard Liaocf924962019-09-16 14:23:43 -05001248 off += cdns->pdm.num_out;
Vinod Koul1215dae2019-05-02 16:29:25 +05301249 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pdm.num_bd,
1250 off, stream->num_ch_bd, false);
Vinod Koulc46302e2018-04-26 18:39:05 +05301251 if (ret)
1252 return ret;
1253
1254 return snd_soc_register_component(cdns->dev, &dai_component,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001255 dais, num_dai);
Vinod Koulc46302e2018-04-26 18:39:05 +05301256}
1257
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001258static int sdw_master_read_intel_prop(struct sdw_bus *bus)
1259{
1260 struct sdw_master_prop *prop = &bus->prop;
1261 struct fwnode_handle *link;
1262 char name[32];
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001263 u32 quirk_mask;
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001264
1265 /* Find master handle */
1266 snprintf(name, sizeof(name),
1267 "mipi-sdw-link-%d-subproperties", bus->link_id);
1268
1269 link = device_get_named_child_node(bus->dev, name);
1270 if (!link) {
1271 dev_err(bus->dev, "Master node %s not found\n", name);
1272 return -EIO;
1273 }
1274
1275 fwnode_property_read_u32(link,
1276 "intel-sdw-ip-clock",
1277 &prop->mclk_freq);
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001278
Bard Liaoa19efb52020-01-13 17:11:29 -06001279 /* the values reported by BIOS are the 2x clock, not the bus clock */
1280 prop->mclk_freq /= 2;
1281
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001282 fwnode_property_read_u32(link,
1283 "intel-quirk-mask",
1284 &quirk_mask);
1285
1286 if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
1287 prop->hw_disabled = true;
1288
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001289 return 0;
1290}
1291
Vinod Koul71bb8a12017-12-14 11:19:43 +05301292static int intel_prop_read(struct sdw_bus *bus)
1293{
1294 /* Initialize with default handler to read all DisCo properties */
1295 sdw_master_read_prop(bus);
1296
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001297 /* read Intel-specific properties */
1298 sdw_master_read_intel_prop(bus);
1299
Vinod Koul71bb8a12017-12-14 11:19:43 +05301300 return 0;
1301}
1302
Shreyas NCc91605f2018-04-26 18:38:43 +05301303static struct sdw_master_ops sdw_intel_ops = {
1304 .read_prop = sdw_master_read_prop,
1305 .xfer_msg = cdns_xfer_msg,
1306 .xfer_msg_defer = cdns_xfer_msg_defer,
1307 .reset_page_addr = cdns_reset_page_addr,
Vinod Koul07abeff2018-04-26 18:38:48 +05301308 .set_bus_conf = cdns_bus_conf,
Shreyas NC30246e22018-07-27 14:44:17 +05301309 .pre_bank_switch = intel_pre_bank_switch,
1310 .post_bank_switch = intel_post_bank_switch,
Shreyas NCc91605f2018-04-26 18:38:43 +05301311};
1312
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001313static int intel_init(struct sdw_intel *sdw)
1314{
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +08001315 bool clock_stop;
1316
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001317 /* Initialize shim and controller */
1318 intel_link_power_up(sdw);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +08001319
1320 clock_stop = sdw_cdns_is_clock_stop(&sdw->cdns);
1321
1322 intel_shim_init(sdw, clock_stop);
1323
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001324 return 0;
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001325}
1326
Vinod Koul71bb8a12017-12-14 11:19:43 +05301327/*
1328 * probe and init
1329 */
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001330static int intel_master_probe(struct platform_device *pdev)
Vinod Koul71bb8a12017-12-14 11:19:43 +05301331{
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001332 struct device *dev = &pdev->dev;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301333 struct sdw_intel *sdw;
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001334 struct sdw_cdns *cdns;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001335 struct sdw_bus *bus;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301336 int ret;
1337
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001338 sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301339 if (!sdw)
1340 return -ENOMEM;
1341
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001342 cdns = &sdw->cdns;
1343 bus = &cdns->bus;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301344
Vinod Koul71bb8a12017-12-14 11:19:43 +05301345 sdw->instance = pdev->id;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001346 sdw->link_res = dev_get_platdata(dev);
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001347 cdns->dev = dev;
1348 cdns->registers = sdw->link_res->registers;
1349 cdns->instance = sdw->instance;
1350 cdns->msg_count = 0;
1351
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001352 bus->link_id = pdev->id;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301353
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001354 sdw_cdns_probe(cdns);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301355
1356 /* Set property read ops */
Shreyas NCc91605f2018-04-26 18:38:43 +05301357 sdw_intel_ops.read_prop = intel_prop_read;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001358 bus->ops = &sdw_intel_ops;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301359
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001360 /* set driver data, accessed by snd_soc_dai_get_drvdata() */
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001361 dev_set_drvdata(dev, cdns);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301362
Bard Liao90261182020-09-08 21:15:20 +08001363 /* use generic bandwidth allocation algorithm */
1364 sdw->cdns.bus.compute_params = sdw_compute_params;
1365
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001366 ret = sdw_bus_master_add(bus, dev, dev->fwnode);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301367 if (ret) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001368 dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001369 return ret;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301370 }
1371
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001372 if (bus->prop.hw_disabled)
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001373 dev_info(dev,
1374 "SoundWire master %d is disabled, will be ignored\n",
1375 bus->link_id);
Pierre-Louis Bossart0ef29862020-09-01 23:05:48 +08001376 /*
1377 * Ignore BIOS err_threshold, it's a really bad idea when dealing
1378 * with multiple hardware synchronized links
1379 */
1380 bus->prop.err_threshold = 0;
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001381
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001382 return 0;
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001383}
1384
1385int intel_master_startup(struct platform_device *pdev)
1386{
1387 struct sdw_cdns_stream_config config;
1388 struct device *dev = &pdev->dev;
1389 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1390 struct sdw_intel *sdw = cdns_to_intel(cdns);
1391 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001392 int link_flags;
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001393 bool multi_link;
Pierre-Louis Bossartcaf68812020-08-17 23:29:20 +08001394 u32 clock_stop_quirks;
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001395 int ret;
1396
1397 if (bus->prop.hw_disabled) {
1398 dev_info(dev,
1399 "SoundWire master %d is disabled, ignoring\n",
1400 sdw->instance);
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001401 return 0;
1402 }
1403
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001404 link_flags = md_flags >> (bus->link_id * 8);
1405 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1406 if (!multi_link) {
1407 dev_dbg(dev, "Multi-link is disabled\n");
1408 bus->multi_link = false;
1409 } else {
Pierre-Louis Bossart94eed662020-09-01 23:05:52 +08001410 /*
1411 * hardware-based synchronization is required regardless
1412 * of the number of segments used by a stream: SSP-based
1413 * synchronization is gated by gsync when the multi-master
1414 * mode is set.
1415 */
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001416 bus->multi_link = true;
Pierre-Louis Bossart94eed662020-09-01 23:05:52 +08001417 bus->hw_sync_min_links = 1;
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001418 }
1419
1420 /* Initialize shim, controller */
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001421 ret = intel_init(sdw);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301422 if (ret)
1423 goto err_init;
1424
Vinod Koul37a2d222018-04-26 18:38:58 +05301425 /* Read the PDI config and initialize cadence PDI */
1426 intel_pdi_init(sdw, &config);
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001427 ret = sdw_cdns_pdi_init(cdns, config);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301428 if (ret)
1429 goto err_init;
1430
Vinod Koul37a2d222018-04-26 18:38:58 +05301431 intel_pdi_ch_update(sdw);
1432
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001433 ret = sdw_cdns_enable_interrupt(cdns, true);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301434 if (ret < 0) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001435 dev_err(dev, "cannot enable interrupts\n");
Vinod Koul71bb8a12017-12-14 11:19:43 +05301436 goto err_init;
1437 }
1438
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001439 /*
1440 * follow recommended programming flows to avoid timeouts when
1441 * gsync is enabled
1442 */
1443 if (multi_link)
1444 intel_shim_sync_arm(sdw);
1445
1446 ret = sdw_cdns_init(cdns);
1447 if (ret < 0) {
1448 dev_err(dev, "unable to initialize Cadence IP\n");
1449 goto err_interrupt;
1450 }
1451
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001452 ret = sdw_cdns_exit_reset(cdns);
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001453 if (ret < 0) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001454 dev_err(dev, "unable to exit bus reset sequence\n");
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001455 goto err_interrupt;
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001456 }
1457
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001458 if (multi_link) {
1459 ret = intel_shim_sync_go(sdw);
1460 if (ret < 0) {
1461 dev_err(dev, "sync go failed: %d\n", ret);
1462 goto err_interrupt;
1463 }
1464 }
1465
Vinod Koulc46302e2018-04-26 18:39:05 +05301466 /* Register DAIs */
1467 ret = intel_register_dai(sdw);
1468 if (ret) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001469 dev_err(dev, "DAI registration failed: %d\n", ret);
1470 snd_soc_unregister_component(dev);
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001471 goto err_interrupt;
Vinod Koulc46302e2018-04-26 18:39:05 +05301472 }
1473
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -05001474 intel_debugfs_init(sdw);
1475
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001476 /* Enable runtime PM */
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001477 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
1478 pm_runtime_set_autosuspend_delay(dev,
1479 INTEL_MASTER_SUSPEND_DELAY_MS);
1480 pm_runtime_use_autosuspend(dev);
1481 pm_runtime_mark_last_busy(dev);
1482
1483 pm_runtime_set_active(dev);
1484 pm_runtime_enable(dev);
1485 }
1486
Pierre-Louis Bossartcaf68812020-08-17 23:29:20 +08001487 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1488 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
1489 /*
1490 * To keep the clock running we need to prevent
1491 * pm_runtime suspend from happening by increasing the
1492 * reference count.
1493 * This quirk is specified by the parent PCI device in
1494 * case of specific latency requirements. It will have
1495 * no effect if pm_runtime is disabled by the user via
1496 * a module parameter for testing purposes.
1497 */
1498 pm_runtime_get_noresume(dev);
1499 }
1500
Pierre-Louis Bossarta2d9c162020-08-17 23:29:17 +08001501 /*
1502 * The runtime PM status of Slave devices is "Unsupported"
1503 * until they report as ATTACHED. If they don't, e.g. because
1504 * there are no Slave devices populated or if the power-on is
1505 * delayed or dependent on a power switch, the Master will
1506 * remain active and prevent its parent from suspending.
1507 *
1508 * Conditionally force the pm_runtime core to re-evaluate the
1509 * Master status in the absence of any Slave activity. A quirk
1510 * is provided to e.g. deal with Slaves that may be powered on
1511 * with a delay. A more complete solution would require the
1512 * definition of Master properties.
1513 */
1514 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
1515 pm_runtime_idle(dev);
1516
Vinod Koul71bb8a12017-12-14 11:19:43 +05301517 return 0;
1518
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001519err_interrupt:
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001520 sdw_cdns_enable_interrupt(cdns, false);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301521err_init:
Vinod Koul71bb8a12017-12-14 11:19:43 +05301522 return ret;
1523}
1524
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001525static int intel_master_remove(struct platform_device *pdev)
Vinod Koul71bb8a12017-12-14 11:19:43 +05301526{
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001527 struct device *dev = &pdev->dev;
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001528 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1529 struct sdw_intel *sdw = cdns_to_intel(cdns);
1530 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001531
Pierre-Louis Bossartcaf68812020-08-17 23:29:20 +08001532 /*
1533 * Since pm_runtime is already disabled, we don't decrease
1534 * the refcount when the clock_stop_quirk is
1535 * SDW_INTEL_CLK_STOP_NOT_ALLOWED
1536 */
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001537 if (!bus->prop.hw_disabled) {
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001538 intel_debugfs_exit(sdw);
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001539 sdw_cdns_enable_interrupt(cdns, false);
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001540 snd_soc_unregister_component(dev);
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001541 }
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001542 sdw_bus_master_delete(bus);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301543
1544 return 0;
1545}
1546
Rander Wangab2c9132020-07-16 23:09:46 +08001547int intel_master_process_wakeen_event(struct platform_device *pdev)
1548{
1549 struct device *dev = &pdev->dev;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301550 struct sdw_intel *sdw;
Rander Wangab2c9132020-07-16 23:09:46 +08001551 struct sdw_bus *bus;
1552 void __iomem *shim;
1553 u16 wake_sts;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301554
1555 sdw = platform_get_drvdata(pdev);
Rander Wangab2c9132020-07-16 23:09:46 +08001556 bus = &sdw->cdns.bus;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301557
Rander Wangab2c9132020-07-16 23:09:46 +08001558 if (bus->prop.hw_disabled) {
1559 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n", bus->link_id);
1560 return 0;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301561 }
Rander Wangab2c9132020-07-16 23:09:46 +08001562
1563 shim = sdw->link_res->shim;
1564 wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
1565
1566 if (!(wake_sts & BIT(sdw->instance)))
1567 return 0;
1568
1569 /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
1570 intel_shim_wake(sdw, false);
1571
1572 /*
1573 * resume the Master, which will generate a bus reset and result in
1574 * Slaves re-attaching and be re-enumerated. The SoundWire physical
1575 * device which generated the wake will trigger an interrupt, which
1576 * will in turn cause the corresponding Linux Slave device to be
1577 * resumed and the Slave codec driver to check the status.
1578 */
1579 pm_request_resume(dev);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301580
1581 return 0;
1582}
1583
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001584/*
1585 * PM calls
1586 */
1587
1588#ifdef CONFIG_PM
1589
Bard Liaof046b232020-08-24 21:32:34 +08001590static int __maybe_unused intel_suspend(struct device *dev)
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001591{
1592 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1593 struct sdw_intel *sdw = cdns_to_intel(cdns);
1594 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossarte4be9fa2020-09-01 23:05:47 +08001595 u32 clock_stop_quirks;
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001596 int ret;
1597
1598 if (bus->prop.hw_disabled) {
1599 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n",
1600 bus->link_id);
1601 return 0;
1602 }
1603
Pierre-Louis Bossartb61b8b32020-08-17 23:29:13 +08001604 if (pm_runtime_suspended(dev)) {
1605 dev_dbg(dev, "%s: pm_runtime status: suspended\n", __func__);
1606
Pierre-Louis Bossarte4be9fa2020-09-01 23:05:47 +08001607 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1608
1609 if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET ||
1610 !clock_stop_quirks) &&
1611 !pm_runtime_suspended(dev->parent)) {
1612
1613 /*
1614 * if we've enabled clock stop, and the parent
1615 * is still active, disable shim wake. The
1616 * SHIM registers are not accessible if the
1617 * parent is already pm_runtime suspended so
1618 * it's too late to change that configuration
1619 */
1620
1621 intel_shim_wake(sdw, false);
1622 }
1623
Pierre-Louis Bossartb61b8b32020-08-17 23:29:13 +08001624 return 0;
1625 }
1626
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001627 ret = sdw_cdns_enable_interrupt(cdns, false);
1628 if (ret < 0) {
1629 dev_err(dev, "cannot disable interrupts on suspend\n");
1630 return ret;
1631 }
1632
1633 ret = intel_link_power_down(sdw);
1634 if (ret) {
1635 dev_err(dev, "Link power down failed: %d", ret);
1636 return ret;
1637 }
1638
1639 intel_shim_wake(sdw, false);
1640
1641 return 0;
1642}
1643
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001644static int intel_suspend_runtime(struct device *dev)
1645{
1646 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1647 struct sdw_intel *sdw = cdns_to_intel(cdns);
1648 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001649 u32 clock_stop_quirks;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001650 int ret;
1651
1652 if (bus->prop.hw_disabled) {
1653 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n",
1654 bus->link_id);
1655 return 0;
1656 }
1657
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001658 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1659
1660 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
1661
1662 ret = sdw_cdns_enable_interrupt(cdns, false);
1663 if (ret < 0) {
1664 dev_err(dev, "cannot disable interrupts on suspend\n");
1665 return ret;
1666 }
1667
1668 ret = intel_link_power_down(sdw);
1669 if (ret) {
1670 dev_err(dev, "Link power down failed: %d", ret);
1671 return ret;
1672 }
1673
1674 intel_shim_wake(sdw, false);
1675
Pierre-Louis Bossart61fb8302020-08-17 23:29:22 +08001676 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET ||
1677 !clock_stop_quirks) {
Rander Wang6626a612020-08-17 23:29:19 +08001678 ret = sdw_cdns_clock_stop(cdns, true);
1679 if (ret < 0) {
1680 dev_err(dev, "cannot enable clock stop on suspend\n");
1681 return ret;
1682 }
1683
1684 ret = sdw_cdns_enable_interrupt(cdns, false);
1685 if (ret < 0) {
1686 dev_err(dev, "cannot disable interrupts on suspend\n");
1687 return ret;
1688 }
1689
1690 ret = intel_link_power_down(sdw);
1691 if (ret) {
1692 dev_err(dev, "Link power down failed: %d", ret);
1693 return ret;
1694 }
1695
1696 intel_shim_wake(sdw, true);
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001697 } else {
1698 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
1699 __func__, clock_stop_quirks);
1700 ret = -EINVAL;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001701 }
1702
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001703 return ret;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001704}
1705
Bard Liaof046b232020-08-24 21:32:34 +08001706static int __maybe_unused intel_resume(struct device *dev)
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001707{
1708 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1709 struct sdw_intel *sdw = cdns_to_intel(cdns);
1710 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossarta2d9c162020-08-17 23:29:17 +08001711 int link_flags;
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001712 bool multi_link;
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001713 int ret;
1714
1715 if (bus->prop.hw_disabled) {
1716 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n",
1717 bus->link_id);
1718 return 0;
1719 }
1720
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001721 link_flags = md_flags >> (bus->link_id * 8);
1722 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1723
Pierre-Louis Bossartb61b8b32020-08-17 23:29:13 +08001724 if (pm_runtime_suspended(dev)) {
1725 dev_dbg(dev, "%s: pm_runtime status was suspended, forcing active\n", __func__);
1726
1727 /* follow required sequence from runtime_pm.rst */
1728 pm_runtime_disable(dev);
1729 pm_runtime_set_active(dev);
1730 pm_runtime_mark_last_busy(dev);
1731 pm_runtime_enable(dev);
Pierre-Louis Bossarta2d9c162020-08-17 23:29:17 +08001732
1733 link_flags = md_flags >> (bus->link_id * 8);
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001734
Pierre-Louis Bossarta2d9c162020-08-17 23:29:17 +08001735 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
1736 pm_runtime_idle(dev);
Pierre-Louis Bossartb61b8b32020-08-17 23:29:13 +08001737 }
1738
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001739 ret = intel_init(sdw);
1740 if (ret) {
1741 dev_err(dev, "%s failed: %d", __func__, ret);
1742 return ret;
1743 }
1744
Pierre-Louis Bossart99b6a302020-08-17 23:29:15 +08001745 /*
1746 * make sure all Slaves are tagged as UNATTACHED and provide
1747 * reason for reinitialization
1748 */
1749 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
1750
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001751 ret = sdw_cdns_enable_interrupt(cdns, true);
1752 if (ret < 0) {
1753 dev_err(dev, "cannot enable interrupts during resume\n");
1754 return ret;
1755 }
1756
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001757 /*
1758 * follow recommended programming flows to avoid timeouts when
1759 * gsync is enabled
1760 */
1761 if (multi_link)
1762 intel_shim_sync_arm(sdw);
1763
1764 ret = sdw_cdns_init(&sdw->cdns);
1765 if (ret < 0) {
1766 dev_err(dev, "unable to initialize Cadence IP during resume\n");
1767 return ret;
1768 }
1769
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001770 ret = sdw_cdns_exit_reset(cdns);
1771 if (ret < 0) {
1772 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1773 return ret;
1774 }
1775
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001776 if (multi_link) {
1777 ret = intel_shim_sync_go(sdw);
1778 if (ret < 0) {
1779 dev_err(dev, "sync go failed during resume\n");
1780 return ret;
1781 }
1782 }
1783
Pierre-Louis Bossartcb1e6d52020-08-17 23:29:14 +08001784 /*
1785 * after system resume, the pm_runtime suspend() may kick in
1786 * during the enumeration, before any children device force the
1787 * master device to remain active. Using pm_runtime_get()
1788 * routines is not really possible, since it'd prevent the
1789 * master from suspending.
1790 * A reasonable compromise is to update the pm_runtime
1791 * counters and delay the pm_runtime suspend by several
1792 * seconds, by when all enumeration should be complete.
1793 */
1794 pm_runtime_mark_last_busy(dev);
1795
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001796 return ret;
1797}
1798
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001799static int intel_resume_runtime(struct device *dev)
1800{
1801 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1802 struct sdw_intel *sdw = cdns_to_intel(cdns);
1803 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001804 u32 clock_stop_quirks;
Rander Wang08abad92020-08-17 23:29:23 +08001805 bool clock_stop0;
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001806 int link_flags;
1807 bool multi_link;
Rander Wang08abad92020-08-17 23:29:23 +08001808 int status;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001809 int ret;
1810
1811 if (bus->prop.hw_disabled) {
1812 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n",
1813 bus->link_id);
1814 return 0;
1815 }
1816
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001817 link_flags = md_flags >> (bus->link_id * 8);
1818 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1819
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001820 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001821
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001822 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
1823 ret = intel_init(sdw);
1824 if (ret) {
1825 dev_err(dev, "%s failed: %d", __func__, ret);
1826 return ret;
1827 }
Pierre-Louis Bossart99b6a302020-08-17 23:29:15 +08001828
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001829 /*
1830 * make sure all Slaves are tagged as UNATTACHED and provide
1831 * reason for reinitialization
1832 */
1833 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001834
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001835 ret = sdw_cdns_enable_interrupt(cdns, true);
1836 if (ret < 0) {
1837 dev_err(dev, "cannot enable interrupts during resume\n");
1838 return ret;
1839 }
1840
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001841 /*
1842 * follow recommended programming flows to avoid
1843 * timeouts when gsync is enabled
1844 */
1845 if (multi_link)
1846 intel_shim_sync_arm(sdw);
1847
1848 ret = sdw_cdns_init(&sdw->cdns);
1849 if (ret < 0) {
1850 dev_err(dev, "unable to initialize Cadence IP during resume\n");
1851 return ret;
1852 }
1853
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001854 ret = sdw_cdns_exit_reset(cdns);
1855 if (ret < 0) {
1856 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1857 return ret;
1858 }
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001859
1860 if (multi_link) {
1861 ret = intel_shim_sync_go(sdw);
1862 if (ret < 0) {
1863 dev_err(dev, "sync go failed during resume\n");
1864 return ret;
1865 }
1866 }
Rander Wang6626a612020-08-17 23:29:19 +08001867 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
1868 ret = intel_init(sdw);
1869 if (ret) {
1870 dev_err(dev, "%s failed: %d", __func__, ret);
1871 return ret;
1872 }
1873
1874 /*
Rander Wang08abad92020-08-17 23:29:23 +08001875 * An exception condition occurs for the CLK_STOP_BUS_RESET
1876 * case if one or more masters remain active. In this condition,
1877 * all the masters are powered on for they are in the same power
1878 * domain. Master can preserve its context for clock stop0, so
1879 * there is no need to clear slave status and reset bus.
1880 */
1881 clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
1882
Rander Wang08abad92020-08-17 23:29:23 +08001883 if (!clock_stop0) {
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001884
1885 /*
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001886 * make sure all Slaves are tagged as UNATTACHED and
1887 * provide reason for reinitialization
1888 */
1889
Rander Wang08abad92020-08-17 23:29:23 +08001890 status = SDW_UNATTACH_REQUEST_MASTER_RESET;
1891 sdw_clear_slave_status(bus, status);
Rander Wang08abad92020-08-17 23:29:23 +08001892
Pierre-Louis Bossartd78071b2020-09-01 23:05:50 +08001893 ret = sdw_cdns_enable_interrupt(cdns, true);
1894 if (ret < 0) {
1895 dev_err(dev, "cannot enable interrupts during resume\n");
1896 return ret;
1897 }
Rander Wang6626a612020-08-17 23:29:19 +08001898
Pierre-Louis Bossartd78071b2020-09-01 23:05:50 +08001899 /*
1900 * follow recommended programming flows to avoid
1901 * timeouts when gsync is enabled
1902 */
1903 if (multi_link)
1904 intel_shim_sync_arm(sdw);
1905
1906 /*
1907 * Re-initialize the IP since it was powered-off
1908 */
1909 sdw_cdns_init(&sdw->cdns);
1910
1911 } else {
1912 ret = sdw_cdns_enable_interrupt(cdns, true);
1913 if (ret < 0) {
1914 dev_err(dev, "cannot enable interrupts during resume\n");
1915 return ret;
1916 }
Rander Wang6626a612020-08-17 23:29:19 +08001917 }
1918
Rander Wang08abad92020-08-17 23:29:23 +08001919 ret = sdw_cdns_clock_restart(cdns, !clock_stop0);
Rander Wang6626a612020-08-17 23:29:19 +08001920 if (ret < 0) {
1921 dev_err(dev, "unable to restart clock during resume\n");
1922 return ret;
1923 }
Pierre-Louis Bossartd78071b2020-09-01 23:05:50 +08001924
1925 if (!clock_stop0) {
1926 ret = sdw_cdns_exit_reset(cdns);
1927 if (ret < 0) {
1928 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1929 return ret;
1930 }
1931
1932 if (multi_link) {
1933 ret = intel_shim_sync_go(sdw);
1934 if (ret < 0) {
1935 dev_err(sdw->cdns.dev, "sync go failed during resume\n");
1936 return ret;
1937 }
1938 }
1939 }
Pierre-Louis Bossart61fb8302020-08-17 23:29:22 +08001940 } else if (!clock_stop_quirks) {
Pierre-Louis Bossartf748f34e2020-09-01 23:05:54 +08001941
1942 clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
1943 if (!clock_stop0)
1944 dev_err(dev, "%s invalid configuration, clock was not stopped", __func__);
1945
Pierre-Louis Bossart61fb8302020-08-17 23:29:22 +08001946 ret = intel_init(sdw);
1947 if (ret) {
1948 dev_err(dev, "%s failed: %d", __func__, ret);
1949 return ret;
1950 }
1951
1952 ret = sdw_cdns_enable_interrupt(cdns, true);
1953 if (ret < 0) {
1954 dev_err(dev, "cannot enable interrupts during resume\n");
1955 return ret;
1956 }
1957
1958 ret = sdw_cdns_clock_restart(cdns, false);
1959 if (ret < 0) {
1960 dev_err(dev, "unable to resume master during resume\n");
1961 return ret;
1962 }
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001963 } else {
1964 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
1965 __func__, clock_stop_quirks);
1966 ret = -EINVAL;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001967 }
1968
1969 return ret;
1970}
1971
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001972#endif
1973
1974static const struct dev_pm_ops intel_pm = {
1975 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001976 SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001977};
1978
Vinod Koul71bb8a12017-12-14 11:19:43 +05301979static struct platform_driver sdw_intel_drv = {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001980 .probe = intel_master_probe,
1981 .remove = intel_master_remove,
Vinod Koul71bb8a12017-12-14 11:19:43 +05301982 .driver = {
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001983 .name = "intel-sdw",
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001984 .pm = &intel_pm,
1985 }
Vinod Koul71bb8a12017-12-14 11:19:43 +05301986};
1987
1988module_platform_driver(sdw_intel_drv);
1989
1990MODULE_LICENSE("Dual BSD/GPL");
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001991MODULE_ALIAS("platform:intel-sdw");
Vinod Koul71bb8a12017-12-14 11:19:43 +05301992MODULE_DESCRIPTION("Intel Soundwire Master Driver");