blob: 710f5eba936bbbdb1583c91b583f8056edea0e61 [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
265static void intel_debugfs_init(struct sdw_intel *sdw)
266{
267 struct dentry *root = sdw->cdns.bus.debugfs;
268
269 if (!root)
270 return;
271
272 sdw->debugfs = debugfs_create_dir("intel-sdw", root);
273
274 debugfs_create_file("intel-registers", 0400, sdw->debugfs, sdw,
275 &intel_reg_fops);
276
277 sdw_cdns_debugfs_init(&sdw->cdns, sdw->debugfs);
278}
279
280static void intel_debugfs_exit(struct sdw_intel *sdw)
281{
282 debugfs_remove_recursive(sdw->debugfs);
283}
284#else
285static void intel_debugfs_init(struct sdw_intel *sdw) {}
286static void intel_debugfs_exit(struct sdw_intel *sdw) {}
287#endif /* CONFIG_DEBUG_FS */
288
289/*
Vinod Koul71bb8a12017-12-14 11:19:43 +0530290 * shim ops
291 */
292
293static int intel_link_power_up(struct sdw_intel *sdw)
294{
295 unsigned int link_id = sdw->instance;
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600296 void __iomem *shim = sdw->link_res->shim;
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800297 u32 *shim_mask = sdw->link_res->shim_mask;
298 struct sdw_bus *bus = &sdw->cdns.bus;
299 struct sdw_master_prop *prop = &bus->prop;
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800300 u32 spa_mask, cpa_mask;
301 u32 link_control;
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800302 int ret = 0;
303 u32 syncprd;
304 u32 sync_reg;
305
306 mutex_lock(sdw->link_res->shim_lock);
307
308 /*
309 * The hardware relies on an internal counter, typically 4kHz,
310 * to generate the SoundWire SSP - which defines a 'safe'
311 * synchronization point between commands and audio transport
312 * and allows for multi link synchronization. The SYNCPRD value
313 * is only dependent on the oscillator clock provided to
314 * the IP, so adjust based on _DSD properties reported in DSDT
315 * tables. The values reported are based on either 24MHz
316 * (CNL/CML) or 38.4 MHz (ICL/TGL+).
317 */
318 if (prop->mclk_freq % 6000000)
319 syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_38_4;
320 else
321 syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24;
322
323 if (!*shim_mask) {
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800324 dev_dbg(sdw->cdns.dev, "%s: powering up all links\n", __func__);
325
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800326 /* we first need to program the SyncPRD/CPU registers */
327 dev_dbg(sdw->cdns.dev,
328 "%s: first link up, programming SYNCPRD\n", __func__);
329
330 /* set SyncPRD period */
331 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
332 sync_reg |= (syncprd <<
333 SDW_REG_SHIFT(SDW_SHIM_SYNC_SYNCPRD));
334
335 /* Set SyncCPU bit */
336 sync_reg |= SDW_SHIM_SYNC_SYNCCPU;
337 intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530338
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800339 /* Link power up sequence */
340 link_control = intel_readl(shim, SDW_SHIM_LCTL);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530341
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800342 /* only power-up enabled links */
343 spa_mask = sdw->link_res->link_mask <<
344 SDW_REG_SHIFT(SDW_SHIM_LCTL_SPA_MASK);
345 cpa_mask = sdw->link_res->link_mask <<
346 SDW_REG_SHIFT(SDW_SHIM_LCTL_CPA_MASK);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800347
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800348 link_control |= spa_mask;
349
350 ret = intel_set_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
351 if (ret < 0) {
352 dev_err(sdw->cdns.dev, "Failed to power up link: %d\n", ret);
353 goto out;
354 }
355
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800356 /* SyncCPU will change once link is active */
357 ret = intel_wait_bit(shim, SDW_SHIM_SYNC,
358 SDW_SHIM_SYNC_SYNCCPU, 0);
359 if (ret < 0) {
360 dev_err(sdw->cdns.dev,
361 "Failed to set SHIM_SYNC: %d\n", ret);
362 goto out;
363 }
364 }
365
366 *shim_mask |= BIT(link_id);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530367
368 sdw->cdns.link_up = true;
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800369out:
370 mutex_unlock(sdw->link_res->shim_lock);
371
372 return ret;
Vinod Koul71bb8a12017-12-14 11:19:43 +0530373}
374
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800375/* this needs to be called with shim_lock */
376static void intel_shim_glue_to_master_ip(struct sdw_intel *sdw)
Vinod Koul71bb8a12017-12-14 11:19:43 +0530377{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600378 void __iomem *shim = sdw->link_res->shim;
Vinod Koul71bb8a12017-12-14 11:19:43 +0530379 unsigned int link_id = sdw->instance;
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800380 u16 ioctl;
Vinod Koul71bb8a12017-12-14 11:19:43 +0530381
382 /* Switch to MIP from Glue logic */
383 ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
384
385 ioctl &= ~(SDW_SHIM_IOCTL_DOE);
386 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800387 usleep_range(10, 15);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530388
389 ioctl &= ~(SDW_SHIM_IOCTL_DO);
390 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800391 usleep_range(10, 15);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530392
393 ioctl |= (SDW_SHIM_IOCTL_MIF);
394 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800395 usleep_range(10, 15);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530396
397 ioctl &= ~(SDW_SHIM_IOCTL_BKE);
398 ioctl &= ~(SDW_SHIM_IOCTL_COE);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530399 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800400 usleep_range(10, 15);
401
402 /* at this point Master IP has full control of the I/Os */
403}
404
405/* this needs to be called with shim_lock */
406static void intel_shim_master_ip_to_glue(struct sdw_intel *sdw)
407{
408 unsigned int link_id = sdw->instance;
409 void __iomem *shim = sdw->link_res->shim;
410 u16 ioctl;
411
412 /* Glue logic */
413 ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
414 ioctl |= SDW_SHIM_IOCTL_BKE;
415 ioctl |= SDW_SHIM_IOCTL_COE;
416 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
417 usleep_range(10, 15);
418
419 ioctl &= ~(SDW_SHIM_IOCTL_MIF);
420 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
421 usleep_range(10, 15);
422
423 /* at this point Integration Glue has full control of the I/Os */
424}
425
426static int intel_shim_init(struct sdw_intel *sdw, bool clock_stop)
427{
428 void __iomem *shim = sdw->link_res->shim;
429 unsigned int link_id = sdw->instance;
430 int ret = 0;
431 u16 ioctl = 0, act = 0;
432
433 mutex_lock(sdw->link_res->shim_lock);
434
435 /* Initialize Shim */
436 ioctl |= SDW_SHIM_IOCTL_BKE;
437 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
438 usleep_range(10, 15);
439
440 ioctl |= SDW_SHIM_IOCTL_WPDD;
441 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
442 usleep_range(10, 15);
443
444 ioctl |= SDW_SHIM_IOCTL_DO;
445 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
446 usleep_range(10, 15);
447
448 ioctl |= SDW_SHIM_IOCTL_DOE;
449 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
450 usleep_range(10, 15);
451
452 intel_shim_glue_to_master_ip(sdw);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530453
454 act |= 0x1 << SDW_REG_SHIFT(SDW_SHIM_CTMCTL_DOAIS);
455 act |= SDW_SHIM_CTMCTL_DACTQE;
456 act |= SDW_SHIM_CTMCTL_DODS;
457 intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800458 usleep_range(10, 15);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530459
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800460 mutex_unlock(sdw->link_res->shim_lock);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530461
Vinod Koul71bb8a12017-12-14 11:19:43 +0530462 return ret;
463}
464
Rander Wangab2c9132020-07-16 23:09:46 +0800465static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800466{
467 void __iomem *shim = sdw->link_res->shim;
468 unsigned int link_id = sdw->instance;
469 u16 wake_en, wake_sts;
470
471 mutex_lock(sdw->link_res->shim_lock);
472 wake_en = intel_readw(shim, SDW_SHIM_WAKEEN);
473
474 if (wake_enable) {
475 /* Enable the wakeup */
476 wake_en |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
477 intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
478 } else {
479 /* Disable the wake up interrupt */
480 wake_en &= ~(SDW_SHIM_WAKEEN_ENABLE << link_id);
481 intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
482
483 /* Clear wake status */
484 wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
485 wake_sts |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
486 intel_writew(shim, SDW_SHIM_WAKESTS_STATUS, wake_sts);
487 }
488 mutex_unlock(sdw->link_res->shim_lock);
489}
490
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +0800491static int intel_link_power_down(struct sdw_intel *sdw)
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800492{
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800493 u32 link_control, spa_mask, cpa_mask;
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800494 unsigned int link_id = sdw->instance;
495 void __iomem *shim = sdw->link_res->shim;
496 u32 *shim_mask = sdw->link_res->shim_mask;
497 int ret = 0;
498
499 mutex_lock(sdw->link_res->shim_lock);
500
501 intel_shim_master_ip_to_glue(sdw);
502
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800503 if (!(*shim_mask & BIT(link_id)))
504 dev_err(sdw->cdns.dev,
505 "%s: Unbalanced power-up/down calls\n", __func__);
506
507 *shim_mask &= ~BIT(link_id);
508
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800509 if (!*shim_mask) {
510
511 dev_dbg(sdw->cdns.dev, "%s: powering down all links\n", __func__);
512
513 /* Link power down sequence */
514 link_control = intel_readl(shim, SDW_SHIM_LCTL);
515
516 /* only power-down enabled links */
517 spa_mask = (~sdw->link_res->link_mask) <<
518 SDW_REG_SHIFT(SDW_SHIM_LCTL_SPA_MASK);
519 cpa_mask = sdw->link_res->link_mask <<
520 SDW_REG_SHIFT(SDW_SHIM_LCTL_CPA_MASK);
521
522 link_control &= spa_mask;
523
524 ret = intel_clear_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
525 }
526
527 link_control = intel_readl(shim, SDW_SHIM_LCTL);
528
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800529 mutex_unlock(sdw->link_res->shim_lock);
530
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800531 if (ret < 0) {
532 dev_err(sdw->cdns.dev, "%s: could not power down link\n", __func__);
533
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800534 return ret;
Pierre-Louis Bossart5ee74eb2020-09-01 23:05:56 +0800535 }
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800536
537 sdw->cdns.link_up = false;
538 return 0;
539}
540
Pierre-Louis Bossart02629e42020-07-16 23:09:41 +0800541static void intel_shim_sync_arm(struct sdw_intel *sdw)
542{
543 void __iomem *shim = sdw->link_res->shim;
544 u32 sync_reg;
545
546 mutex_lock(sdw->link_res->shim_lock);
547
548 /* update SYNC register */
549 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
550 sync_reg |= (SDW_SHIM_SYNC_CMDSYNC << sdw->instance);
551 intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
552
553 mutex_unlock(sdw->link_res->shim_lock);
554}
555
Pierre-Louis Bossart437e3282020-07-16 23:09:42 +0800556static int intel_shim_sync_go_unlocked(struct sdw_intel *sdw)
557{
558 void __iomem *shim = sdw->link_res->shim;
559 u32 sync_reg;
560 int ret;
561
562 /* Read SYNC register */
563 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
564
565 /*
566 * Set SyncGO bit to synchronously trigger a bank switch for
567 * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all
568 * the Masters.
569 */
570 sync_reg |= SDW_SHIM_SYNC_SYNCGO;
571
572 ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg,
573 SDW_SHIM_SYNC_SYNCGO);
574
575 if (ret < 0)
576 dev_err(sdw->cdns.dev, "SyncGO clear failed: %d\n", ret);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530577
578 return ret;
579}
580
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +0800581static int intel_shim_sync_go(struct sdw_intel *sdw)
582{
583 int ret;
584
585 mutex_lock(sdw->link_res->shim_lock);
586
587 ret = intel_shim_sync_go_unlocked(sdw);
588
589 mutex_unlock(sdw->link_res->shim_lock);
590
591 return ret;
592}
593
Vinod Koul37a2d222018-04-26 18:38:58 +0530594/*
595 * PDI routines
596 */
597static void intel_pdi_init(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500598 struct sdw_cdns_stream_config *config)
Vinod Koul37a2d222018-04-26 18:38:58 +0530599{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600600 void __iomem *shim = sdw->link_res->shim;
Vinod Koul37a2d222018-04-26 18:38:58 +0530601 unsigned int link_id = sdw->instance;
602 int pcm_cap, pdm_cap;
603
604 /* PCM Stream Capability */
605 pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id));
606
607 config->pcm_bd = (pcm_cap & SDW_SHIM_PCMSCAP_BSS) >>
608 SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_BSS);
609 config->pcm_in = (pcm_cap & SDW_SHIM_PCMSCAP_ISS) >>
610 SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_ISS);
611 config->pcm_out = (pcm_cap & SDW_SHIM_PCMSCAP_OSS) >>
612 SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_OSS);
613
Pierre-Louis Bossart121f4362019-05-22 14:47:29 -0500614 dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
615 config->pcm_bd, config->pcm_in, config->pcm_out);
616
Vinod Koul37a2d222018-04-26 18:38:58 +0530617 /* PDM Stream Capability */
618 pdm_cap = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
619
620 config->pdm_bd = (pdm_cap & SDW_SHIM_PDMSCAP_BSS) >>
621 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_BSS);
622 config->pdm_in = (pdm_cap & SDW_SHIM_PDMSCAP_ISS) >>
623 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_ISS);
624 config->pdm_out = (pdm_cap & SDW_SHIM_PDMSCAP_OSS) >>
625 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_OSS);
Pierre-Louis Bossart121f4362019-05-22 14:47:29 -0500626
627 dev_dbg(sdw->cdns.dev, "PDM cap bd:%d in:%d out:%d\n",
628 config->pdm_bd, config->pdm_in, config->pdm_out);
Vinod Koul37a2d222018-04-26 18:38:58 +0530629}
630
631static int
632intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num, bool pcm)
633{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600634 void __iomem *shim = sdw->link_res->shim;
Vinod Koul37a2d222018-04-26 18:38:58 +0530635 unsigned int link_id = sdw->instance;
636 int count;
637
638 if (pcm) {
639 count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num));
Pierre-Louis Bossart18046332019-08-05 19:55:07 -0500640
641 /*
642 * WORKAROUND: on all existing Intel controllers, pdi
643 * number 2 reports channel count as 1 even though it
644 * supports 8 channels. Performing hardcoding for pdi
645 * number 2.
646 */
647 if (pdi_num == 2)
648 count = 7;
649
Vinod Koul37a2d222018-04-26 18:38:58 +0530650 } else {
651 count = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
652 count = ((count & SDW_SHIM_PDMSCAP_CPSS) >>
653 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_CPSS));
654 }
655
656 /* zero based values for channel count in register */
657 count++;
658
659 return count;
660}
661
662static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500663 struct sdw_cdns_pdi *pdi,
664 unsigned int num_pdi,
665 unsigned int *num_ch, bool pcm)
Vinod Koul37a2d222018-04-26 18:38:58 +0530666{
667 int i, ch_count = 0;
668
669 for (i = 0; i < num_pdi; i++) {
670 pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num, pcm);
671 ch_count += pdi->ch_count;
672 pdi++;
673 }
674
675 *num_ch = ch_count;
676 return 0;
677}
678
679static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500680 struct sdw_cdns_streams *stream, bool pcm)
Vinod Koul37a2d222018-04-26 18:38:58 +0530681{
682 intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500683 &stream->num_ch_bd, pcm);
Vinod Koul37a2d222018-04-26 18:38:58 +0530684
685 intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500686 &stream->num_ch_in, pcm);
Vinod Koul37a2d222018-04-26 18:38:58 +0530687
688 intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500689 &stream->num_ch_out, pcm);
Vinod Koul37a2d222018-04-26 18:38:58 +0530690
691 return 0;
692}
693
694static int intel_pdi_ch_update(struct sdw_intel *sdw)
695{
696 /* First update PCM streams followed by PDM streams */
697 intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm, true);
698 intel_pdi_stream_ch_update(sdw, &sdw->cdns.pdm, false);
699
700 return 0;
701}
702
703static void
704intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
705{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600706 void __iomem *shim = sdw->link_res->shim;
Vinod Koul37a2d222018-04-26 18:38:58 +0530707 unsigned int link_id = sdw->instance;
708 int pdi_conf = 0;
709
Pierre-Louis Bossartc134f912019-10-22 18:29:48 -0500710 /* the Bulk and PCM streams are not contiguous */
711 pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
712 if (pdi->num >= 2)
713 pdi->intel_alh_id += 2;
Vinod Koul37a2d222018-04-26 18:38:58 +0530714
715 /*
716 * Program stream parameters to stream SHIM register
717 * This is applicable for PCM stream only.
718 */
719 if (pdi->type != SDW_STREAM_PCM)
720 return;
721
722 if (pdi->dir == SDW_DATA_DIR_RX)
723 pdi_conf |= SDW_SHIM_PCMSYCM_DIR;
724 else
725 pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR);
726
727 pdi_conf |= (pdi->intel_alh_id <<
728 SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_STREAM));
729 pdi_conf |= (pdi->l_ch_num << SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_LCHN));
730 pdi_conf |= (pdi->h_ch_num << SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_HCHN));
731
732 intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
733}
734
735static void
736intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
737{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600738 void __iomem *alh = sdw->link_res->alh;
Vinod Koul37a2d222018-04-26 18:38:58 +0530739 unsigned int link_id = sdw->instance;
740 unsigned int conf;
741
Pierre-Louis Bossartc134f912019-10-22 18:29:48 -0500742 /* the Bulk and PCM streams are not contiguous */
743 pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
744 if (pdi->num >= 2)
745 pdi->intel_alh_id += 2;
Vinod Koul37a2d222018-04-26 18:38:58 +0530746
747 /* Program Stream config ALH register */
748 conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
749
750 conf |= (SDW_ALH_STRMZCFG_DMAT_VAL <<
751 SDW_REG_SHIFT(SDW_ALH_STRMZCFG_DMAT));
752
753 conf |= ((pdi->ch_count - 1) <<
754 SDW_REG_SHIFT(SDW_ALH_STRMZCFG_CHN));
755
756 intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
757}
758
Rander Wang4b206d32019-12-11 19:45:02 -0600759static int intel_params_stream(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500760 struct snd_pcm_substream *substream,
761 struct snd_soc_dai *dai,
Rander Wang4b206d32019-12-11 19:45:02 -0600762 struct snd_pcm_hw_params *hw_params,
763 int link_id, int alh_stream_id)
Vinod Koulc46302e2018-04-26 18:39:05 +0530764{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600765 struct sdw_intel_link_res *res = sdw->link_res;
Rander Wang4b206d32019-12-11 19:45:02 -0600766 struct sdw_intel_stream_params_data params_data;
Pierre-Louis Bossart05c8afe42019-08-05 19:55:06 -0500767
Rander Wang4b206d32019-12-11 19:45:02 -0600768 params_data.substream = substream;
769 params_data.dai = dai;
770 params_data.hw_params = hw_params;
771 params_data.link_id = link_id;
772 params_data.alh_stream_id = alh_stream_id;
Vinod Koulc46302e2018-04-26 18:39:05 +0530773
Rander Wang4b206d32019-12-11 19:45:02 -0600774 if (res->ops && res->ops->params_stream && res->dev)
775 return res->ops->params_stream(res->dev,
776 &params_data);
Vinod Koulc46302e2018-04-26 18:39:05 +0530777 return -EIO;
778}
779
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -0600780static int intel_free_stream(struct sdw_intel *sdw,
781 struct snd_pcm_substream *substream,
782 struct snd_soc_dai *dai,
783 int link_id)
784{
785 struct sdw_intel_link_res *res = sdw->link_res;
786 struct sdw_intel_stream_free_data free_data;
787
788 free_data.substream = substream;
789 free_data.dai = dai;
790 free_data.link_id = link_id;
791
792 if (res->ops && res->ops->free_stream && res->dev)
793 return res->ops->free_stream(res->dev,
794 &free_data);
795
796 return 0;
797}
798
Vinod Koulc46302e2018-04-26 18:39:05 +0530799/*
Shreyas NC30246e22018-07-27 14:44:17 +0530800 * bank switch routines
801 */
802
803static int intel_pre_bank_switch(struct sdw_bus *bus)
804{
805 struct sdw_cdns *cdns = bus_to_cdns(bus);
806 struct sdw_intel *sdw = cdns_to_intel(cdns);
Shreyas NC30246e22018-07-27 14:44:17 +0530807
808 /* Write to register only for multi-link */
809 if (!bus->multi_link)
810 return 0;
811
Pierre-Louis Bossart02629e42020-07-16 23:09:41 +0800812 intel_shim_sync_arm(sdw);
Shreyas NC30246e22018-07-27 14:44:17 +0530813
814 return 0;
815}
816
817static int intel_post_bank_switch(struct sdw_bus *bus)
818{
819 struct sdw_cdns *cdns = bus_to_cdns(bus);
820 struct sdw_intel *sdw = cdns_to_intel(cdns);
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600821 void __iomem *shim = sdw->link_res->shim;
Shreyas NC30246e22018-07-27 14:44:17 +0530822 int sync_reg, ret;
823
824 /* Write to register only for multi-link */
825 if (!bus->multi_link)
826 return 0;
827
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800828 mutex_lock(sdw->link_res->shim_lock);
829
Shreyas NC30246e22018-07-27 14:44:17 +0530830 /* Read SYNC register */
831 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
832
833 /*
834 * post_bank_switch() ops is called from the bus in loop for
835 * all the Masters in the steam with the expectation that
836 * we trigger the bankswitch for the only first Master in the list
837 * and do nothing for the other Masters
838 *
839 * So, set the SYNCGO bit only if CMDSYNC bit is set for any Master.
840 */
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800841 if (!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK)) {
842 ret = 0;
843 goto unlock;
844 }
Shreyas NC30246e22018-07-27 14:44:17 +0530845
Pierre-Louis Bossart437e3282020-07-16 23:09:42 +0800846 ret = intel_shim_sync_go_unlocked(sdw);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800847unlock:
848 mutex_unlock(sdw->link_res->shim_lock);
Shreyas NC30246e22018-07-27 14:44:17 +0530849
Shreyas NC30246e22018-07-27 14:44:17 +0530850 if (ret < 0)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500851 dev_err(sdw->cdns.dev, "Post bank switch failed: %d\n", ret);
Shreyas NC30246e22018-07-27 14:44:17 +0530852
853 return ret;
854}
855
856/*
Vinod Koulc46302e2018-04-26 18:39:05 +0530857 * DAI routines
858 */
859
Rander Wang5e7484d2020-02-14 19:47:39 -0600860static int intel_startup(struct snd_pcm_substream *substream,
861 struct snd_soc_dai *dai)
862{
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +0800863 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
864 int ret;
865
866 ret = pm_runtime_get_sync(cdns->dev);
867 if (ret < 0 && ret != -EACCES) {
868 dev_err_ratelimited(cdns->dev,
869 "pm_runtime_get_sync failed in %s, ret %d\n",
870 __func__, ret);
871 pm_runtime_put_noidle(cdns->dev);
872 return ret;
873 }
Pierre-Louis Bossartff16d1e2020-07-01 02:43:54 +0800874 return 0;
Rander Wang5e7484d2020-02-14 19:47:39 -0600875}
876
Vinod Koulc46302e2018-04-26 18:39:05 +0530877static int intel_hw_params(struct snd_pcm_substream *substream,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500878 struct snd_pcm_hw_params *params,
879 struct snd_soc_dai *dai)
Vinod Koulc46302e2018-04-26 18:39:05 +0530880{
881 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
882 struct sdw_intel *sdw = cdns_to_intel(cdns);
883 struct sdw_cdns_dma_data *dma;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500884 struct sdw_cdns_pdi *pdi;
Vinod Koulc46302e2018-04-26 18:39:05 +0530885 struct sdw_stream_config sconfig;
886 struct sdw_port_config *pconfig;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500887 int ch, dir;
888 int ret;
Vinod Koulc46302e2018-04-26 18:39:05 +0530889 bool pcm = true;
890
891 dma = snd_soc_dai_get_dma_data(dai, substream);
892 if (!dma)
893 return -EIO;
894
895 ch = params_channels(params);
896 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
897 dir = SDW_DATA_DIR_RX;
898 else
899 dir = SDW_DATA_DIR_TX;
900
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500901 if (dma->stream_type == SDW_STREAM_PDM)
Vinod Koulc46302e2018-04-26 18:39:05 +0530902 pcm = false;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500903
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500904 if (pcm)
Bard Liao1b533852019-09-16 14:23:48 -0500905 pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500906 else
Bard Liao1b533852019-09-16 14:23:48 -0500907 pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pdm, ch, dir, dai->id);
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500908
909 if (!pdi) {
910 ret = -EINVAL;
911 goto error;
Vinod Koulc46302e2018-04-26 18:39:05 +0530912 }
913
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500914 /* do run-time configurations for SHIM, ALH and PDI/PORT */
915 intel_pdi_shim_configure(sdw, pdi);
916 intel_pdi_alh_configure(sdw, pdi);
917 sdw_cdns_config_stream(cdns, ch, dir, pdi);
Vinod Koulc46302e2018-04-26 18:39:05 +0530918
Bard Liaoa5a02392020-08-17 23:29:16 +0800919 /* store pdi and hw_params, may be needed in prepare step */
920 dma->suspended = false;
921 dma->pdi = pdi;
922 dma->hw_params = params;
Vinod Koulc46302e2018-04-26 18:39:05 +0530923
924 /* Inform DSP about PDI stream number */
Rander Wang4b206d32019-12-11 19:45:02 -0600925 ret = intel_params_stream(sdw, substream, dai, params,
926 sdw->instance,
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500927 pdi->intel_alh_id);
928 if (ret)
929 goto error;
Vinod Koulc46302e2018-04-26 18:39:05 +0530930
931 sconfig.direction = dir;
932 sconfig.ch_count = ch;
933 sconfig.frame_rate = params_rate(params);
934 sconfig.type = dma->stream_type;
935
936 if (dma->stream_type == SDW_STREAM_PDM) {
937 sconfig.frame_rate *= 50;
938 sconfig.bps = 1;
939 } else {
940 sconfig.bps = snd_pcm_format_width(params_format(params));
941 }
942
943 /* Port configuration */
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500944 pconfig = kcalloc(1, sizeof(*pconfig), GFP_KERNEL);
Vinod Koulc46302e2018-04-26 18:39:05 +0530945 if (!pconfig) {
946 ret = -ENOMEM;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500947 goto error;
Vinod Koulc46302e2018-04-26 18:39:05 +0530948 }
949
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500950 pconfig->num = pdi->num;
951 pconfig->ch_mask = (1 << ch) - 1;
Vinod Koulc46302e2018-04-26 18:39:05 +0530952
953 ret = sdw_stream_add_master(&cdns->bus, &sconfig,
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500954 pconfig, 1, dma->stream);
955 if (ret)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500956 dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
Vinod Koulc46302e2018-04-26 18:39:05 +0530957
958 kfree(pconfig);
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500959error:
Vinod Koulc46302e2018-04-26 18:39:05 +0530960 return ret;
961}
962
Rander Wang27b198f2020-02-14 19:47:37 -0600963static int intel_prepare(struct snd_pcm_substream *substream,
964 struct snd_soc_dai *dai)
965{
Bard Liaoa5a02392020-08-17 23:29:16 +0800966 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
967 struct sdw_intel *sdw = cdns_to_intel(cdns);
Rander Wang27b198f2020-02-14 19:47:37 -0600968 struct sdw_cdns_dma_data *dma;
Bard Liaoa5a02392020-08-17 23:29:16 +0800969 int ch, dir;
970 int ret;
Rander Wang27b198f2020-02-14 19:47:37 -0600971
972 dma = snd_soc_dai_get_dma_data(dai, substream);
973 if (!dma) {
974 dev_err(dai->dev, "failed to get dma data in %s",
975 __func__);
976 return -EIO;
977 }
978
Bard Liaoa5a02392020-08-17 23:29:16 +0800979 if (dma->suspended) {
980 dma->suspended = false;
981
982 /*
983 * .prepare() is called after system resume, where we
984 * need to reinitialize the SHIM/ALH/Cadence IP.
985 * .prepare() is also called to deal with underflows,
986 * but in those cases we cannot touch ALH/SHIM
987 * registers
988 */
989
990 /* configure stream */
991 ch = params_channels(dma->hw_params);
992 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
993 dir = SDW_DATA_DIR_RX;
994 else
995 dir = SDW_DATA_DIR_TX;
996
997 intel_pdi_shim_configure(sdw, dma->pdi);
998 intel_pdi_alh_configure(sdw, dma->pdi);
999 sdw_cdns_config_stream(cdns, ch, dir, dma->pdi);
1000
1001 /* Inform DSP about PDI stream number */
1002 ret = intel_params_stream(sdw, substream, dai,
1003 dma->hw_params,
1004 sdw->instance,
1005 dma->pdi->intel_alh_id);
1006 if (ret)
1007 goto err;
1008 }
1009
1010 ret = sdw_prepare_stream(dma->stream);
1011
1012err:
1013 return ret;
Rander Wang27b198f2020-02-14 19:47:37 -06001014}
1015
Rander Wang973a8422020-02-14 19:47:38 -06001016static int intel_trigger(struct snd_pcm_substream *substream, int cmd,
1017 struct snd_soc_dai *dai)
1018{
1019 struct sdw_cdns_dma_data *dma;
1020 int ret;
1021
1022 dma = snd_soc_dai_get_dma_data(dai, substream);
1023 if (!dma) {
1024 dev_err(dai->dev, "failed to get dma data in %s", __func__);
1025 return -EIO;
1026 }
1027
1028 switch (cmd) {
1029 case SNDRV_PCM_TRIGGER_START:
1030 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1031 case SNDRV_PCM_TRIGGER_RESUME:
1032 ret = sdw_enable_stream(dma->stream);
1033 break;
1034
1035 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1036 case SNDRV_PCM_TRIGGER_SUSPEND:
1037 case SNDRV_PCM_TRIGGER_STOP:
1038 ret = sdw_disable_stream(dma->stream);
1039 break;
1040
1041 default:
1042 ret = -EINVAL;
1043 break;
1044 }
1045
1046 if (ret)
1047 dev_err(dai->dev,
1048 "%s trigger %d failed: %d",
1049 __func__, cmd, ret);
1050 return ret;
1051}
1052
Vinod Koulc46302e2018-04-26 18:39:05 +05301053static int
1054intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
1055{
1056 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001057 struct sdw_intel *sdw = cdns_to_intel(cdns);
Vinod Koulc46302e2018-04-26 18:39:05 +05301058 struct sdw_cdns_dma_data *dma;
1059 int ret;
1060
1061 dma = snd_soc_dai_get_dma_data(dai, substream);
1062 if (!dma)
1063 return -EIO;
1064
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001065 ret = sdw_deprepare_stream(dma->stream);
1066 if (ret) {
1067 dev_err(dai->dev, "sdw_deprepare_stream: failed %d", ret);
1068 return ret;
1069 }
1070
Vinod Koulc46302e2018-04-26 18:39:05 +05301071 ret = sdw_stream_remove_master(&cdns->bus, dma->stream);
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001072 if (ret < 0) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001073 dev_err(dai->dev, "remove master from stream %s failed: %d\n",
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001074 dma->stream->name, ret);
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001075 return ret;
1076 }
Vinod Koulc46302e2018-04-26 18:39:05 +05301077
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001078 ret = intel_free_stream(sdw, substream, dai, sdw->instance);
1079 if (ret < 0) {
1080 dev_err(dai->dev, "intel_free_stream: failed %d", ret);
1081 return ret;
1082 }
1083
Bard Liaoa5a02392020-08-17 23:29:16 +08001084 dma->hw_params = NULL;
1085 dma->pdi = NULL;
1086
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001087 return 0;
Vinod Koulc46302e2018-04-26 18:39:05 +05301088}
1089
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -05001090static void intel_shutdown(struct snd_pcm_substream *substream,
1091 struct snd_soc_dai *dai)
1092{
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001093 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -05001094
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001095 pm_runtime_mark_last_busy(cdns->dev);
1096 pm_runtime_put_autosuspend(cdns->dev);
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -05001097}
1098
Bard Liaoa5a02392020-08-17 23:29:16 +08001099static int intel_component_dais_suspend(struct snd_soc_component *component)
1100{
1101 struct sdw_cdns_dma_data *dma;
1102 struct snd_soc_dai *dai;
1103
1104 for_each_component_dais(component, dai) {
1105 /*
1106 * we don't have a .suspend dai_ops, and we don't have access
1107 * to the substream, so let's mark both capture and playback
1108 * DMA contexts as suspended
1109 */
1110 dma = dai->playback_dma_data;
1111 if (dma)
1112 dma->suspended = true;
1113
1114 dma = dai->capture_dma_data;
1115 if (dma)
1116 dma->suspended = true;
1117 }
1118
1119 return 0;
1120}
1121
Vinod Koulc46302e2018-04-26 18:39:05 +05301122static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001123 void *stream, int direction)
Vinod Koulc46302e2018-04-26 18:39:05 +05301124{
1125 return cdns_set_sdw_stream(dai, stream, true, direction);
1126}
1127
1128static int intel_pdm_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001129 void *stream, int direction)
Vinod Koulc46302e2018-04-26 18:39:05 +05301130{
1131 return cdns_set_sdw_stream(dai, stream, false, direction);
1132}
1133
Pierre-Louis Bossart09553142020-07-01 02:43:52 +08001134static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
1135 int direction)
1136{
1137 struct sdw_cdns_dma_data *dma;
1138
1139 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1140 dma = dai->playback_dma_data;
1141 else
1142 dma = dai->capture_dma_data;
1143
1144 if (!dma)
1145 return NULL;
1146
1147 return dma->stream;
1148}
1149
Julia Lawallb1635592018-10-27 15:34:42 +02001150static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
Rander Wang5e7484d2020-02-14 19:47:39 -06001151 .startup = intel_startup,
Vinod Koulc46302e2018-04-26 18:39:05 +05301152 .hw_params = intel_hw_params,
Rander Wang27b198f2020-02-14 19:47:37 -06001153 .prepare = intel_prepare,
Rander Wang973a8422020-02-14 19:47:38 -06001154 .trigger = intel_trigger,
Vinod Koulc46302e2018-04-26 18:39:05 +05301155 .hw_free = intel_hw_free,
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -05001156 .shutdown = intel_shutdown,
Vinod Koulc46302e2018-04-26 18:39:05 +05301157 .set_sdw_stream = intel_pcm_set_sdw_stream,
Pierre-Louis Bossart09553142020-07-01 02:43:52 +08001158 .get_sdw_stream = intel_get_sdw_stream,
Vinod Koulc46302e2018-04-26 18:39:05 +05301159};
1160
Julia Lawallb1635592018-10-27 15:34:42 +02001161static const struct snd_soc_dai_ops intel_pdm_dai_ops = {
Rander Wang5e7484d2020-02-14 19:47:39 -06001162 .startup = intel_startup,
Vinod Koulc46302e2018-04-26 18:39:05 +05301163 .hw_params = intel_hw_params,
Rander Wang27b198f2020-02-14 19:47:37 -06001164 .prepare = intel_prepare,
Rander Wang973a8422020-02-14 19:47:38 -06001165 .trigger = intel_trigger,
Vinod Koulc46302e2018-04-26 18:39:05 +05301166 .hw_free = intel_hw_free,
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -05001167 .shutdown = intel_shutdown,
Vinod Koulc46302e2018-04-26 18:39:05 +05301168 .set_sdw_stream = intel_pdm_set_sdw_stream,
Pierre-Louis Bossart09553142020-07-01 02:43:52 +08001169 .get_sdw_stream = intel_get_sdw_stream,
Vinod Koulc46302e2018-04-26 18:39:05 +05301170};
1171
1172static const struct snd_soc_component_driver dai_component = {
1173 .name = "soundwire",
Bard Liaoa5a02392020-08-17 23:29:16 +08001174 .suspend = intel_component_dais_suspend
Vinod Koulc46302e2018-04-26 18:39:05 +05301175};
1176
1177static int intel_create_dai(struct sdw_cdns *cdns,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001178 struct snd_soc_dai_driver *dais,
1179 enum intel_pdi_type type,
1180 u32 num, u32 off, u32 max_ch, bool pcm)
Vinod Koulc46302e2018-04-26 18:39:05 +05301181{
1182 int i;
1183
1184 if (num == 0)
1185 return 0;
1186
1187 /* TODO: Read supported rates/formats from hardware */
1188 for (i = off; i < (off + num); i++) {
Pierre-Louis Bossartbf6d6e62020-06-17 11:35:36 -05001189 dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
1190 "SDW%d Pin%d",
1191 cdns->instance, i);
Vinod Koulc46302e2018-04-26 18:39:05 +05301192 if (!dais[i].name)
1193 return -ENOMEM;
1194
1195 if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
Vinod Koulc46302e2018-04-26 18:39:05 +05301196 dais[i].playback.channels_min = 1;
1197 dais[i].playback.channels_max = max_ch;
1198 dais[i].playback.rates = SNDRV_PCM_RATE_48000;
1199 dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
1200 }
1201
1202 if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
Srinivas Kandagatla39194122019-06-06 12:23:04 +01001203 dais[i].capture.channels_min = 1;
1204 dais[i].capture.channels_max = max_ch;
Vinod Koulc46302e2018-04-26 18:39:05 +05301205 dais[i].capture.rates = SNDRV_PCM_RATE_48000;
1206 dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
1207 }
1208
Vinod Koulc46302e2018-04-26 18:39:05 +05301209 if (pcm)
1210 dais[i].ops = &intel_pcm_dai_ops;
1211 else
1212 dais[i].ops = &intel_pdm_dai_ops;
1213 }
1214
1215 return 0;
1216}
1217
1218static int intel_register_dai(struct sdw_intel *sdw)
1219{
1220 struct sdw_cdns *cdns = &sdw->cdns;
1221 struct sdw_cdns_streams *stream;
1222 struct snd_soc_dai_driver *dais;
1223 int num_dai, ret, off = 0;
1224
1225 /* DAIs are created based on total number of PDIs supported */
1226 num_dai = cdns->pcm.num_pdi + cdns->pdm.num_pdi;
1227
1228 dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
1229 if (!dais)
1230 return -ENOMEM;
1231
1232 /* Create PCM DAIs */
1233 stream = &cdns->pcm;
1234
Bard Liaocf924962019-09-16 14:23:43 -05001235 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
Vinod Koul1215dae2019-05-02 16:29:25 +05301236 off, stream->num_ch_in, true);
Vinod Koulc46302e2018-04-26 18:39:05 +05301237 if (ret)
1238 return ret;
1239
1240 off += cdns->pcm.num_in;
Vinod Koul1215dae2019-05-02 16:29:25 +05301241 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
1242 off, stream->num_ch_out, true);
Vinod Koulc46302e2018-04-26 18:39:05 +05301243 if (ret)
1244 return ret;
1245
1246 off += cdns->pcm.num_out;
Vinod Koul1215dae2019-05-02 16:29:25 +05301247 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
1248 off, stream->num_ch_bd, true);
Vinod Koulc46302e2018-04-26 18:39:05 +05301249 if (ret)
1250 return ret;
1251
1252 /* Create PDM DAIs */
1253 stream = &cdns->pdm;
1254 off += cdns->pcm.num_bd;
Vinod Koul1215dae2019-05-02 16:29:25 +05301255 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pdm.num_in,
1256 off, stream->num_ch_in, false);
Vinod Koulc46302e2018-04-26 18:39:05 +05301257 if (ret)
1258 return ret;
1259
1260 off += cdns->pdm.num_in;
Vinod Koul1215dae2019-05-02 16:29:25 +05301261 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pdm.num_out,
1262 off, stream->num_ch_out, false);
Vinod Koulc46302e2018-04-26 18:39:05 +05301263 if (ret)
1264 return ret;
1265
Bard Liaocf924962019-09-16 14:23:43 -05001266 off += cdns->pdm.num_out;
Vinod Koul1215dae2019-05-02 16:29:25 +05301267 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pdm.num_bd,
1268 off, stream->num_ch_bd, false);
Vinod Koulc46302e2018-04-26 18:39:05 +05301269 if (ret)
1270 return ret;
1271
1272 return snd_soc_register_component(cdns->dev, &dai_component,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001273 dais, num_dai);
Vinod Koulc46302e2018-04-26 18:39:05 +05301274}
1275
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001276static int sdw_master_read_intel_prop(struct sdw_bus *bus)
1277{
1278 struct sdw_master_prop *prop = &bus->prop;
1279 struct fwnode_handle *link;
1280 char name[32];
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001281 u32 quirk_mask;
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001282
1283 /* Find master handle */
1284 snprintf(name, sizeof(name),
1285 "mipi-sdw-link-%d-subproperties", bus->link_id);
1286
1287 link = device_get_named_child_node(bus->dev, name);
1288 if (!link) {
1289 dev_err(bus->dev, "Master node %s not found\n", name);
1290 return -EIO;
1291 }
1292
1293 fwnode_property_read_u32(link,
1294 "intel-sdw-ip-clock",
1295 &prop->mclk_freq);
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001296
Bard Liaoa19efb52020-01-13 17:11:29 -06001297 /* the values reported by BIOS are the 2x clock, not the bus clock */
1298 prop->mclk_freq /= 2;
1299
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001300 fwnode_property_read_u32(link,
1301 "intel-quirk-mask",
1302 &quirk_mask);
1303
1304 if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
1305 prop->hw_disabled = true;
1306
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001307 return 0;
1308}
1309
Vinod Koul71bb8a12017-12-14 11:19:43 +05301310static int intel_prop_read(struct sdw_bus *bus)
1311{
1312 /* Initialize with default handler to read all DisCo properties */
1313 sdw_master_read_prop(bus);
1314
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001315 /* read Intel-specific properties */
1316 sdw_master_read_intel_prop(bus);
1317
Vinod Koul71bb8a12017-12-14 11:19:43 +05301318 return 0;
1319}
1320
Shreyas NCc91605f2018-04-26 18:38:43 +05301321static struct sdw_master_ops sdw_intel_ops = {
1322 .read_prop = sdw_master_read_prop,
1323 .xfer_msg = cdns_xfer_msg,
1324 .xfer_msg_defer = cdns_xfer_msg_defer,
1325 .reset_page_addr = cdns_reset_page_addr,
Vinod Koul07abeff2018-04-26 18:38:48 +05301326 .set_bus_conf = cdns_bus_conf,
Shreyas NC30246e22018-07-27 14:44:17 +05301327 .pre_bank_switch = intel_pre_bank_switch,
1328 .post_bank_switch = intel_post_bank_switch,
Shreyas NCc91605f2018-04-26 18:38:43 +05301329};
1330
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001331static int intel_init(struct sdw_intel *sdw)
1332{
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +08001333 bool clock_stop;
1334
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001335 /* Initialize shim and controller */
1336 intel_link_power_up(sdw);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +08001337
1338 clock_stop = sdw_cdns_is_clock_stop(&sdw->cdns);
1339
1340 intel_shim_init(sdw, clock_stop);
1341
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001342 return 0;
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001343}
1344
Vinod Koul71bb8a12017-12-14 11:19:43 +05301345/*
1346 * probe and init
1347 */
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001348static int intel_master_probe(struct platform_device *pdev)
Vinod Koul71bb8a12017-12-14 11:19:43 +05301349{
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001350 struct device *dev = &pdev->dev;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301351 struct sdw_intel *sdw;
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001352 struct sdw_cdns *cdns;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001353 struct sdw_bus *bus;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301354 int ret;
1355
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001356 sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301357 if (!sdw)
1358 return -ENOMEM;
1359
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001360 cdns = &sdw->cdns;
1361 bus = &cdns->bus;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301362
Vinod Koul71bb8a12017-12-14 11:19:43 +05301363 sdw->instance = pdev->id;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001364 sdw->link_res = dev_get_platdata(dev);
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001365 cdns->dev = dev;
1366 cdns->registers = sdw->link_res->registers;
1367 cdns->instance = sdw->instance;
1368 cdns->msg_count = 0;
1369
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001370 bus->link_id = pdev->id;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301371
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001372 sdw_cdns_probe(cdns);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301373
1374 /* Set property read ops */
Shreyas NCc91605f2018-04-26 18:38:43 +05301375 sdw_intel_ops.read_prop = intel_prop_read;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001376 bus->ops = &sdw_intel_ops;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301377
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001378 /* set driver data, accessed by snd_soc_dai_get_drvdata() */
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001379 dev_set_drvdata(dev, cdns);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301380
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001381 ret = sdw_bus_master_add(bus, dev, dev->fwnode);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301382 if (ret) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001383 dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001384 return ret;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301385 }
1386
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001387 if (bus->prop.hw_disabled)
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001388 dev_info(dev,
1389 "SoundWire master %d is disabled, will be ignored\n",
1390 bus->link_id);
Pierre-Louis Bossart0ef29862020-09-01 23:05:48 +08001391 /*
1392 * Ignore BIOS err_threshold, it's a really bad idea when dealing
1393 * with multiple hardware synchronized links
1394 */
1395 bus->prop.err_threshold = 0;
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001396
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001397 return 0;
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001398}
1399
1400int intel_master_startup(struct platform_device *pdev)
1401{
1402 struct sdw_cdns_stream_config config;
1403 struct device *dev = &pdev->dev;
1404 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1405 struct sdw_intel *sdw = cdns_to_intel(cdns);
1406 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001407 int link_flags;
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001408 bool multi_link;
Pierre-Louis Bossartcaf68812020-08-17 23:29:20 +08001409 u32 clock_stop_quirks;
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001410 int ret;
1411
1412 if (bus->prop.hw_disabled) {
1413 dev_info(dev,
1414 "SoundWire master %d is disabled, ignoring\n",
1415 sdw->instance);
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001416 return 0;
1417 }
1418
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001419 link_flags = md_flags >> (bus->link_id * 8);
1420 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1421 if (!multi_link) {
1422 dev_dbg(dev, "Multi-link is disabled\n");
1423 bus->multi_link = false;
1424 } else {
Pierre-Louis Bossart94eed662020-09-01 23:05:52 +08001425 /*
1426 * hardware-based synchronization is required regardless
1427 * of the number of segments used by a stream: SSP-based
1428 * synchronization is gated by gsync when the multi-master
1429 * mode is set.
1430 */
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001431 bus->multi_link = true;
Pierre-Louis Bossart94eed662020-09-01 23:05:52 +08001432 bus->hw_sync_min_links = 1;
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001433 }
1434
1435 /* Initialize shim, controller */
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001436 ret = intel_init(sdw);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301437 if (ret)
1438 goto err_init;
1439
Vinod Koul37a2d222018-04-26 18:38:58 +05301440 /* Read the PDI config and initialize cadence PDI */
1441 intel_pdi_init(sdw, &config);
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001442 ret = sdw_cdns_pdi_init(cdns, config);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301443 if (ret)
1444 goto err_init;
1445
Vinod Koul37a2d222018-04-26 18:38:58 +05301446 intel_pdi_ch_update(sdw);
1447
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001448 ret = sdw_cdns_enable_interrupt(cdns, true);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301449 if (ret < 0) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001450 dev_err(dev, "cannot enable interrupts\n");
Vinod Koul71bb8a12017-12-14 11:19:43 +05301451 goto err_init;
1452 }
1453
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001454 /*
1455 * follow recommended programming flows to avoid timeouts when
1456 * gsync is enabled
1457 */
1458 if (multi_link)
1459 intel_shim_sync_arm(sdw);
1460
1461 ret = sdw_cdns_init(cdns);
1462 if (ret < 0) {
1463 dev_err(dev, "unable to initialize Cadence IP\n");
1464 goto err_interrupt;
1465 }
1466
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001467 ret = sdw_cdns_exit_reset(cdns);
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001468 if (ret < 0) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001469 dev_err(dev, "unable to exit bus reset sequence\n");
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001470 goto err_interrupt;
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001471 }
1472
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001473 if (multi_link) {
1474 ret = intel_shim_sync_go(sdw);
1475 if (ret < 0) {
1476 dev_err(dev, "sync go failed: %d\n", ret);
1477 goto err_interrupt;
1478 }
1479 }
1480
Vinod Koulc46302e2018-04-26 18:39:05 +05301481 /* Register DAIs */
1482 ret = intel_register_dai(sdw);
1483 if (ret) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001484 dev_err(dev, "DAI registration failed: %d\n", ret);
1485 snd_soc_unregister_component(dev);
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001486 goto err_interrupt;
Vinod Koulc46302e2018-04-26 18:39:05 +05301487 }
1488
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -05001489 intel_debugfs_init(sdw);
1490
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001491 /* Enable runtime PM */
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001492 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
1493 pm_runtime_set_autosuspend_delay(dev,
1494 INTEL_MASTER_SUSPEND_DELAY_MS);
1495 pm_runtime_use_autosuspend(dev);
1496 pm_runtime_mark_last_busy(dev);
1497
1498 pm_runtime_set_active(dev);
1499 pm_runtime_enable(dev);
1500 }
1501
Pierre-Louis Bossartcaf68812020-08-17 23:29:20 +08001502 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1503 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
1504 /*
1505 * To keep the clock running we need to prevent
1506 * pm_runtime suspend from happening by increasing the
1507 * reference count.
1508 * This quirk is specified by the parent PCI device in
1509 * case of specific latency requirements. It will have
1510 * no effect if pm_runtime is disabled by the user via
1511 * a module parameter for testing purposes.
1512 */
1513 pm_runtime_get_noresume(dev);
1514 }
1515
Pierre-Louis Bossarta2d9c162020-08-17 23:29:17 +08001516 /*
1517 * The runtime PM status of Slave devices is "Unsupported"
1518 * until they report as ATTACHED. If they don't, e.g. because
1519 * there are no Slave devices populated or if the power-on is
1520 * delayed or dependent on a power switch, the Master will
1521 * remain active and prevent its parent from suspending.
1522 *
1523 * Conditionally force the pm_runtime core to re-evaluate the
1524 * Master status in the absence of any Slave activity. A quirk
1525 * is provided to e.g. deal with Slaves that may be powered on
1526 * with a delay. A more complete solution would require the
1527 * definition of Master properties.
1528 */
1529 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
1530 pm_runtime_idle(dev);
1531
Vinod Koul71bb8a12017-12-14 11:19:43 +05301532 return 0;
1533
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001534err_interrupt:
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001535 sdw_cdns_enable_interrupt(cdns, false);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301536err_init:
Vinod Koul71bb8a12017-12-14 11:19:43 +05301537 return ret;
1538}
1539
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001540static int intel_master_remove(struct platform_device *pdev)
Vinod Koul71bb8a12017-12-14 11:19:43 +05301541{
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001542 struct device *dev = &pdev->dev;
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001543 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1544 struct sdw_intel *sdw = cdns_to_intel(cdns);
1545 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001546
Pierre-Louis Bossartcaf68812020-08-17 23:29:20 +08001547 /*
1548 * Since pm_runtime is already disabled, we don't decrease
1549 * the refcount when the clock_stop_quirk is
1550 * SDW_INTEL_CLK_STOP_NOT_ALLOWED
1551 */
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001552 if (!bus->prop.hw_disabled) {
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001553 intel_debugfs_exit(sdw);
Pierre-Louis Bossart83e129a2020-06-01 02:20:58 +08001554 sdw_cdns_enable_interrupt(cdns, false);
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001555 snd_soc_unregister_component(dev);
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001556 }
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001557 sdw_bus_master_delete(bus);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301558
1559 return 0;
1560}
1561
Rander Wangab2c9132020-07-16 23:09:46 +08001562int intel_master_process_wakeen_event(struct platform_device *pdev)
1563{
1564 struct device *dev = &pdev->dev;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301565 struct sdw_intel *sdw;
Rander Wangab2c9132020-07-16 23:09:46 +08001566 struct sdw_bus *bus;
1567 void __iomem *shim;
1568 u16 wake_sts;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301569
1570 sdw = platform_get_drvdata(pdev);
Rander Wangab2c9132020-07-16 23:09:46 +08001571 bus = &sdw->cdns.bus;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301572
Rander Wangab2c9132020-07-16 23:09:46 +08001573 if (bus->prop.hw_disabled) {
1574 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n", bus->link_id);
1575 return 0;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301576 }
Rander Wangab2c9132020-07-16 23:09:46 +08001577
1578 shim = sdw->link_res->shim;
1579 wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
1580
1581 if (!(wake_sts & BIT(sdw->instance)))
1582 return 0;
1583
1584 /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
1585 intel_shim_wake(sdw, false);
1586
1587 /*
1588 * resume the Master, which will generate a bus reset and result in
1589 * Slaves re-attaching and be re-enumerated. The SoundWire physical
1590 * device which generated the wake will trigger an interrupt, which
1591 * will in turn cause the corresponding Linux Slave device to be
1592 * resumed and the Slave codec driver to check the status.
1593 */
1594 pm_request_resume(dev);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301595
1596 return 0;
1597}
1598
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001599/*
1600 * PM calls
1601 */
1602
1603#ifdef CONFIG_PM
1604
Bard Liaof046b232020-08-24 21:32:34 +08001605static int __maybe_unused intel_suspend(struct device *dev)
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001606{
1607 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1608 struct sdw_intel *sdw = cdns_to_intel(cdns);
1609 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossarte4be9fa2020-09-01 23:05:47 +08001610 u32 clock_stop_quirks;
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001611 int ret;
1612
1613 if (bus->prop.hw_disabled) {
1614 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n",
1615 bus->link_id);
1616 return 0;
1617 }
1618
Pierre-Louis Bossartb61b8b32020-08-17 23:29:13 +08001619 if (pm_runtime_suspended(dev)) {
1620 dev_dbg(dev, "%s: pm_runtime status: suspended\n", __func__);
1621
Pierre-Louis Bossarte4be9fa2020-09-01 23:05:47 +08001622 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1623
1624 if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET ||
1625 !clock_stop_quirks) &&
1626 !pm_runtime_suspended(dev->parent)) {
1627
1628 /*
1629 * if we've enabled clock stop, and the parent
1630 * is still active, disable shim wake. The
1631 * SHIM registers are not accessible if the
1632 * parent is already pm_runtime suspended so
1633 * it's too late to change that configuration
1634 */
1635
1636 intel_shim_wake(sdw, false);
1637 }
1638
Pierre-Louis Bossartb61b8b32020-08-17 23:29:13 +08001639 return 0;
1640 }
1641
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001642 ret = sdw_cdns_enable_interrupt(cdns, false);
1643 if (ret < 0) {
1644 dev_err(dev, "cannot disable interrupts on suspend\n");
1645 return ret;
1646 }
1647
1648 ret = intel_link_power_down(sdw);
1649 if (ret) {
1650 dev_err(dev, "Link power down failed: %d", ret);
1651 return ret;
1652 }
1653
1654 intel_shim_wake(sdw, false);
1655
1656 return 0;
1657}
1658
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001659static int intel_suspend_runtime(struct device *dev)
1660{
1661 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1662 struct sdw_intel *sdw = cdns_to_intel(cdns);
1663 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001664 u32 clock_stop_quirks;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001665 int ret;
1666
1667 if (bus->prop.hw_disabled) {
1668 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n",
1669 bus->link_id);
1670 return 0;
1671 }
1672
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001673 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1674
1675 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
1676
1677 ret = sdw_cdns_enable_interrupt(cdns, false);
1678 if (ret < 0) {
1679 dev_err(dev, "cannot disable interrupts on suspend\n");
1680 return ret;
1681 }
1682
1683 ret = intel_link_power_down(sdw);
1684 if (ret) {
1685 dev_err(dev, "Link power down failed: %d", ret);
1686 return ret;
1687 }
1688
1689 intel_shim_wake(sdw, false);
1690
Pierre-Louis Bossart61fb8302020-08-17 23:29:22 +08001691 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET ||
1692 !clock_stop_quirks) {
Rander Wang6626a612020-08-17 23:29:19 +08001693 ret = sdw_cdns_clock_stop(cdns, true);
1694 if (ret < 0) {
1695 dev_err(dev, "cannot enable clock stop on suspend\n");
1696 return ret;
1697 }
1698
1699 ret = sdw_cdns_enable_interrupt(cdns, false);
1700 if (ret < 0) {
1701 dev_err(dev, "cannot disable interrupts on suspend\n");
1702 return ret;
1703 }
1704
1705 ret = intel_link_power_down(sdw);
1706 if (ret) {
1707 dev_err(dev, "Link power down failed: %d", ret);
1708 return ret;
1709 }
1710
1711 intel_shim_wake(sdw, true);
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001712 } else {
1713 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
1714 __func__, clock_stop_quirks);
1715 ret = -EINVAL;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001716 }
1717
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001718 return ret;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001719}
1720
Bard Liaof046b232020-08-24 21:32:34 +08001721static int __maybe_unused intel_resume(struct device *dev)
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001722{
1723 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1724 struct sdw_intel *sdw = cdns_to_intel(cdns);
1725 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossarta2d9c162020-08-17 23:29:17 +08001726 int link_flags;
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001727 bool multi_link;
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001728 int ret;
1729
1730 if (bus->prop.hw_disabled) {
1731 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n",
1732 bus->link_id);
1733 return 0;
1734 }
1735
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001736 link_flags = md_flags >> (bus->link_id * 8);
1737 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1738
Pierre-Louis Bossartb61b8b32020-08-17 23:29:13 +08001739 if (pm_runtime_suspended(dev)) {
1740 dev_dbg(dev, "%s: pm_runtime status was suspended, forcing active\n", __func__);
1741
1742 /* follow required sequence from runtime_pm.rst */
1743 pm_runtime_disable(dev);
1744 pm_runtime_set_active(dev);
1745 pm_runtime_mark_last_busy(dev);
1746 pm_runtime_enable(dev);
Pierre-Louis Bossarta2d9c162020-08-17 23:29:17 +08001747
1748 link_flags = md_flags >> (bus->link_id * 8);
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001749
Pierre-Louis Bossarta2d9c162020-08-17 23:29:17 +08001750 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
1751 pm_runtime_idle(dev);
Pierre-Louis Bossartb61b8b32020-08-17 23:29:13 +08001752 }
1753
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001754 ret = intel_init(sdw);
1755 if (ret) {
1756 dev_err(dev, "%s failed: %d", __func__, ret);
1757 return ret;
1758 }
1759
Pierre-Louis Bossart99b6a302020-08-17 23:29:15 +08001760 /*
1761 * make sure all Slaves are tagged as UNATTACHED and provide
1762 * reason for reinitialization
1763 */
1764 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
1765
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001766 ret = sdw_cdns_enable_interrupt(cdns, true);
1767 if (ret < 0) {
1768 dev_err(dev, "cannot enable interrupts during resume\n");
1769 return ret;
1770 }
1771
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001772 /*
1773 * follow recommended programming flows to avoid timeouts when
1774 * gsync is enabled
1775 */
1776 if (multi_link)
1777 intel_shim_sync_arm(sdw);
1778
1779 ret = sdw_cdns_init(&sdw->cdns);
1780 if (ret < 0) {
1781 dev_err(dev, "unable to initialize Cadence IP during resume\n");
1782 return ret;
1783 }
1784
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001785 ret = sdw_cdns_exit_reset(cdns);
1786 if (ret < 0) {
1787 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1788 return ret;
1789 }
1790
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001791 if (multi_link) {
1792 ret = intel_shim_sync_go(sdw);
1793 if (ret < 0) {
1794 dev_err(dev, "sync go failed during resume\n");
1795 return ret;
1796 }
1797 }
1798
Pierre-Louis Bossartcb1e6d52020-08-17 23:29:14 +08001799 /*
1800 * after system resume, the pm_runtime suspend() may kick in
1801 * during the enumeration, before any children device force the
1802 * master device to remain active. Using pm_runtime_get()
1803 * routines is not really possible, since it'd prevent the
1804 * master from suspending.
1805 * A reasonable compromise is to update the pm_runtime
1806 * counters and delay the pm_runtime suspend by several
1807 * seconds, by when all enumeration should be complete.
1808 */
1809 pm_runtime_mark_last_busy(dev);
1810
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001811 return ret;
1812}
1813
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001814static int intel_resume_runtime(struct device *dev)
1815{
1816 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1817 struct sdw_intel *sdw = cdns_to_intel(cdns);
1818 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001819 u32 clock_stop_quirks;
Rander Wang08abad92020-08-17 23:29:23 +08001820 bool clock_stop0;
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001821 int link_flags;
1822 bool multi_link;
Rander Wang08abad92020-08-17 23:29:23 +08001823 int status;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001824 int ret;
1825
1826 if (bus->prop.hw_disabled) {
1827 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n",
1828 bus->link_id);
1829 return 0;
1830 }
1831
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001832 link_flags = md_flags >> (bus->link_id * 8);
1833 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1834
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001835 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001836
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001837 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
1838 ret = intel_init(sdw);
1839 if (ret) {
1840 dev_err(dev, "%s failed: %d", __func__, ret);
1841 return ret;
1842 }
Pierre-Louis Bossart99b6a302020-08-17 23:29:15 +08001843
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001844 /*
1845 * make sure all Slaves are tagged as UNATTACHED and provide
1846 * reason for reinitialization
1847 */
1848 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001849
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001850 ret = sdw_cdns_enable_interrupt(cdns, true);
1851 if (ret < 0) {
1852 dev_err(dev, "cannot enable interrupts during resume\n");
1853 return ret;
1854 }
1855
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001856 /*
1857 * follow recommended programming flows to avoid
1858 * timeouts when gsync is enabled
1859 */
1860 if (multi_link)
1861 intel_shim_sync_arm(sdw);
1862
1863 ret = sdw_cdns_init(&sdw->cdns);
1864 if (ret < 0) {
1865 dev_err(dev, "unable to initialize Cadence IP during resume\n");
1866 return ret;
1867 }
1868
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001869 ret = sdw_cdns_exit_reset(cdns);
1870 if (ret < 0) {
1871 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1872 return ret;
1873 }
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001874
1875 if (multi_link) {
1876 ret = intel_shim_sync_go(sdw);
1877 if (ret < 0) {
1878 dev_err(dev, "sync go failed during resume\n");
1879 return ret;
1880 }
1881 }
Rander Wang6626a612020-08-17 23:29:19 +08001882 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
1883 ret = intel_init(sdw);
1884 if (ret) {
1885 dev_err(dev, "%s failed: %d", __func__, ret);
1886 return ret;
1887 }
1888
1889 /*
Rander Wang08abad92020-08-17 23:29:23 +08001890 * An exception condition occurs for the CLK_STOP_BUS_RESET
1891 * case if one or more masters remain active. In this condition,
1892 * all the masters are powered on for they are in the same power
1893 * domain. Master can preserve its context for clock stop0, so
1894 * there is no need to clear slave status and reset bus.
1895 */
1896 clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
1897
Rander Wang08abad92020-08-17 23:29:23 +08001898 if (!clock_stop0) {
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001899
1900 /*
Pierre-Louis Bossart857a7c42020-09-01 23:05:49 +08001901 * make sure all Slaves are tagged as UNATTACHED and
1902 * provide reason for reinitialization
1903 */
1904
Rander Wang08abad92020-08-17 23:29:23 +08001905 status = SDW_UNATTACH_REQUEST_MASTER_RESET;
1906 sdw_clear_slave_status(bus, status);
Rander Wang08abad92020-08-17 23:29:23 +08001907
Pierre-Louis Bossartd78071b2020-09-01 23:05:50 +08001908 ret = sdw_cdns_enable_interrupt(cdns, true);
1909 if (ret < 0) {
1910 dev_err(dev, "cannot enable interrupts during resume\n");
1911 return ret;
1912 }
Rander Wang6626a612020-08-17 23:29:19 +08001913
Pierre-Louis Bossartd78071b2020-09-01 23:05:50 +08001914 /*
1915 * follow recommended programming flows to avoid
1916 * timeouts when gsync is enabled
1917 */
1918 if (multi_link)
1919 intel_shim_sync_arm(sdw);
1920
1921 /*
1922 * Re-initialize the IP since it was powered-off
1923 */
1924 sdw_cdns_init(&sdw->cdns);
1925
1926 } else {
1927 ret = sdw_cdns_enable_interrupt(cdns, true);
1928 if (ret < 0) {
1929 dev_err(dev, "cannot enable interrupts during resume\n");
1930 return ret;
1931 }
Rander Wang6626a612020-08-17 23:29:19 +08001932 }
1933
Rander Wang08abad92020-08-17 23:29:23 +08001934 ret = sdw_cdns_clock_restart(cdns, !clock_stop0);
Rander Wang6626a612020-08-17 23:29:19 +08001935 if (ret < 0) {
1936 dev_err(dev, "unable to restart clock during resume\n");
1937 return ret;
1938 }
Pierre-Louis Bossartd78071b2020-09-01 23:05:50 +08001939
1940 if (!clock_stop0) {
1941 ret = sdw_cdns_exit_reset(cdns);
1942 if (ret < 0) {
1943 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1944 return ret;
1945 }
1946
1947 if (multi_link) {
1948 ret = intel_shim_sync_go(sdw);
1949 if (ret < 0) {
1950 dev_err(sdw->cdns.dev, "sync go failed during resume\n");
1951 return ret;
1952 }
1953 }
1954 }
Pierre-Louis Bossart61fb8302020-08-17 23:29:22 +08001955 } else if (!clock_stop_quirks) {
Pierre-Louis Bossartf748f34e2020-09-01 23:05:54 +08001956
1957 clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
1958 if (!clock_stop0)
1959 dev_err(dev, "%s invalid configuration, clock was not stopped", __func__);
1960
Pierre-Louis Bossart61fb8302020-08-17 23:29:22 +08001961 ret = intel_init(sdw);
1962 if (ret) {
1963 dev_err(dev, "%s failed: %d", __func__, ret);
1964 return ret;
1965 }
1966
1967 ret = sdw_cdns_enable_interrupt(cdns, true);
1968 if (ret < 0) {
1969 dev_err(dev, "cannot enable interrupts during resume\n");
1970 return ret;
1971 }
1972
1973 ret = sdw_cdns_clock_restart(cdns, false);
1974 if (ret < 0) {
1975 dev_err(dev, "unable to resume master during resume\n");
1976 return ret;
1977 }
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001978 } else {
1979 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
1980 __func__, clock_stop_quirks);
1981 ret = -EINVAL;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001982 }
1983
1984 return ret;
1985}
1986
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001987#endif
1988
1989static const struct dev_pm_ops intel_pm = {
1990 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001991 SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001992};
1993
Vinod Koul71bb8a12017-12-14 11:19:43 +05301994static struct platform_driver sdw_intel_drv = {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001995 .probe = intel_master_probe,
1996 .remove = intel_master_remove,
Vinod Koul71bb8a12017-12-14 11:19:43 +05301997 .driver = {
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001998 .name = "intel-sdw",
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001999 .pm = &intel_pm,
2000 }
Vinod Koul71bb8a12017-12-14 11:19:43 +05302001};
2002
2003module_platform_driver(sdw_intel_drv);
2004
2005MODULE_LICENSE("Dual BSD/GPL");
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08002006MODULE_ALIAS("platform:intel-sdw");
Vinod Koul71bb8a12017-12-14 11:19:43 +05302007MODULE_DESCRIPTION("Intel Soundwire Master Driver");