blob: ad476e9e4d252bc74c872ec6a0fde0c151022eb3 [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 Bossartebf878e2020-08-17 23:29:12 +080037
38static int md_flags;
39module_param_named(sdw_md_flags, md_flags, int, 0444);
40MODULE_PARM_DESC(sdw_md_flags, "SoundWire Intel Master device flags (0x0 all off)");
41
Vinod Koul71bb8a12017-12-14 11:19:43 +053042/* Intel SHIM Registers Definition */
43#define SDW_SHIM_LCAP 0x0
44#define SDW_SHIM_LCTL 0x4
45#define SDW_SHIM_IPPTR 0x8
46#define SDW_SHIM_SYNC 0xC
47
Pierre-Louis Bossart7cc6e312019-05-01 10:57:39 -050048#define SDW_SHIM_CTLSCAP(x) (0x010 + 0x60 * (x))
49#define SDW_SHIM_CTLS0CM(x) (0x012 + 0x60 * (x))
50#define SDW_SHIM_CTLS1CM(x) (0x014 + 0x60 * (x))
51#define SDW_SHIM_CTLS2CM(x) (0x016 + 0x60 * (x))
52#define SDW_SHIM_CTLS3CM(x) (0x018 + 0x60 * (x))
53#define SDW_SHIM_PCMSCAP(x) (0x020 + 0x60 * (x))
Vinod Koul71bb8a12017-12-14 11:19:43 +053054
Pierre-Louis Bossart7cc6e312019-05-01 10:57:39 -050055#define SDW_SHIM_PCMSYCHM(x, y) (0x022 + (0x60 * (x)) + (0x2 * (y)))
56#define SDW_SHIM_PCMSYCHC(x, y) (0x042 + (0x60 * (x)) + (0x2 * (y)))
57#define SDW_SHIM_PDMSCAP(x) (0x062 + 0x60 * (x))
58#define SDW_SHIM_IOCTL(x) (0x06C + 0x60 * (x))
59#define SDW_SHIM_CTMCTL(x) (0x06E + 0x60 * (x))
Vinod Koul71bb8a12017-12-14 11:19:43 +053060
61#define SDW_SHIM_WAKEEN 0x190
62#define SDW_SHIM_WAKESTS 0x192
63
64#define SDW_SHIM_LCTL_SPA BIT(0)
65#define SDW_SHIM_LCTL_CPA BIT(8)
66
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +080067#define SDW_SHIM_SYNC_SYNCPRD_VAL_24 (24000 / SDW_CADENCE_GSYNC_KHZ - 1)
68#define SDW_SHIM_SYNC_SYNCPRD_VAL_38_4 (38400 / SDW_CADENCE_GSYNC_KHZ - 1)
Vinod Koul71bb8a12017-12-14 11:19:43 +053069#define SDW_SHIM_SYNC_SYNCPRD GENMASK(14, 0)
70#define SDW_SHIM_SYNC_SYNCCPU BIT(15)
71#define SDW_SHIM_SYNC_CMDSYNC_MASK GENMASK(19, 16)
72#define SDW_SHIM_SYNC_CMDSYNC BIT(16)
73#define SDW_SHIM_SYNC_SYNCGO BIT(24)
74
75#define SDW_SHIM_PCMSCAP_ISS GENMASK(3, 0)
76#define SDW_SHIM_PCMSCAP_OSS GENMASK(7, 4)
77#define SDW_SHIM_PCMSCAP_BSS GENMASK(12, 8)
78
79#define SDW_SHIM_PCMSYCM_LCHN GENMASK(3, 0)
80#define SDW_SHIM_PCMSYCM_HCHN GENMASK(7, 4)
81#define SDW_SHIM_PCMSYCM_STREAM GENMASK(13, 8)
82#define SDW_SHIM_PCMSYCM_DIR BIT(15)
83
84#define SDW_SHIM_PDMSCAP_ISS GENMASK(3, 0)
85#define SDW_SHIM_PDMSCAP_OSS GENMASK(7, 4)
86#define SDW_SHIM_PDMSCAP_BSS GENMASK(12, 8)
87#define SDW_SHIM_PDMSCAP_CPSS GENMASK(15, 13)
88
89#define SDW_SHIM_IOCTL_MIF BIT(0)
90#define SDW_SHIM_IOCTL_CO BIT(1)
91#define SDW_SHIM_IOCTL_COE BIT(2)
92#define SDW_SHIM_IOCTL_DO BIT(3)
93#define SDW_SHIM_IOCTL_DOE BIT(4)
94#define SDW_SHIM_IOCTL_BKE BIT(5)
95#define SDW_SHIM_IOCTL_WPDD BIT(6)
96#define SDW_SHIM_IOCTL_CIBD BIT(8)
97#define SDW_SHIM_IOCTL_DIBD BIT(9)
98
99#define SDW_SHIM_CTMCTL_DACTQE BIT(0)
100#define SDW_SHIM_CTMCTL_DODS BIT(1)
101#define SDW_SHIM_CTMCTL_DOAIS GENMASK(4, 3)
102
103#define SDW_SHIM_WAKEEN_ENABLE BIT(0)
104#define SDW_SHIM_WAKESTS_STATUS BIT(0)
105
106/* Intel ALH Register definitions */
Pierre-Louis Bossart7cc6e312019-05-01 10:57:39 -0500107#define SDW_ALH_STRMZCFG(x) (0x000 + (0x4 * (x)))
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -0500108#define SDW_ALH_NUM_STREAMS 64
Vinod Koul71bb8a12017-12-14 11:19:43 +0530109
110#define SDW_ALH_STRMZCFG_DMAT_VAL 0x3
111#define SDW_ALH_STRMZCFG_DMAT GENMASK(7, 0)
112#define SDW_ALH_STRMZCFG_CHN GENMASK(19, 16)
113
Vinod Koulc46302e2018-04-26 18:39:05 +0530114enum intel_pdi_type {
115 INTEL_PDI_IN = 0,
116 INTEL_PDI_OUT = 1,
117 INTEL_PDI_BD = 2,
118};
119
Vinod Koul71bb8a12017-12-14 11:19:43 +0530120#define cdns_to_intel(_cdns) container_of(_cdns, struct sdw_intel, cdns)
121
122/*
123 * Read, write helpers for HW registers
124 */
125static inline int intel_readl(void __iomem *base, int offset)
126{
127 return readl(base + offset);
128}
129
130static inline void intel_writel(void __iomem *base, int offset, int value)
131{
132 writel(value, base + offset);
133}
134
135static inline u16 intel_readw(void __iomem *base, int offset)
136{
137 return readw(base + offset);
138}
139
140static inline void intel_writew(void __iomem *base, int offset, u16 value)
141{
142 writew(value, base + offset);
143}
144
Pierre-Louis Bossart7d2845d2020-07-16 23:09:39 +0800145static int intel_wait_bit(void __iomem *base, int offset, u32 mask, u32 target)
Vinod Koul71bb8a12017-12-14 11:19:43 +0530146{
147 int timeout = 10;
148 u32 reg_read;
149
Vinod Koul71bb8a12017-12-14 11:19:43 +0530150 do {
151 reg_read = readl(base + offset);
Pierre-Louis Bossart7d2845d2020-07-16 23:09:39 +0800152 if ((reg_read & mask) == target)
Vinod Koul71bb8a12017-12-14 11:19:43 +0530153 return 0;
154
155 timeout--;
Pierre-Louis Bossart7d2845d2020-07-16 23:09:39 +0800156 usleep_range(50, 100);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530157 } while (timeout != 0);
158
159 return -EAGAIN;
160}
161
Pierre-Louis Bossart7d2845d2020-07-16 23:09:39 +0800162static int intel_clear_bit(void __iomem *base, int offset, u32 value, u32 mask)
163{
164 writel(value, base + offset);
165 return intel_wait_bit(base, offset, mask, 0);
166}
167
Vinod Koul71bb8a12017-12-14 11:19:43 +0530168static int intel_set_bit(void __iomem *base, int offset, u32 value, u32 mask)
169{
Vinod Koul71bb8a12017-12-14 11:19:43 +0530170 writel(value, base + offset);
Pierre-Louis Bossart7d2845d2020-07-16 23:09:39 +0800171 return intel_wait_bit(base, offset, mask, mask);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530172}
173
174/*
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -0500175 * debugfs
176 */
177#ifdef CONFIG_DEBUG_FS
178
179#define RD_BUF (2 * PAGE_SIZE)
180
181static ssize_t intel_sprintf(void __iomem *mem, bool l,
182 char *buf, size_t pos, unsigned int reg)
183{
184 int value;
185
186 if (l)
187 value = intel_readl(mem, reg);
188 else
189 value = intel_readw(mem, reg);
190
191 return scnprintf(buf + pos, RD_BUF - pos, "%4x\t%4x\n", reg, value);
192}
193
194static int intel_reg_show(struct seq_file *s_file, void *data)
195{
196 struct sdw_intel *sdw = s_file->private;
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600197 void __iomem *s = sdw->link_res->shim;
198 void __iomem *a = sdw->link_res->alh;
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -0500199 char *buf;
200 ssize_t ret;
201 int i, j;
202 unsigned int links, reg;
203
204 buf = kzalloc(RD_BUF, GFP_KERNEL);
205 if (!buf)
206 return -ENOMEM;
207
208 links = intel_readl(s, SDW_SHIM_LCAP) & GENMASK(2, 0);
209
210 ret = scnprintf(buf, RD_BUF, "Register Value\n");
211 ret += scnprintf(buf + ret, RD_BUF - ret, "\nShim\n");
212
213 for (i = 0; i < links; i++) {
214 reg = SDW_SHIM_LCAP + i * 4;
215 ret += intel_sprintf(s, true, buf, ret, reg);
216 }
217
218 for (i = 0; i < links; i++) {
219 ret += scnprintf(buf + ret, RD_BUF - ret, "\nLink%d\n", i);
220 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLSCAP(i));
221 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS0CM(i));
222 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS1CM(i));
223 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS2CM(i));
224 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS3CM(i));
225 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PCMSCAP(i));
226
227 ret += scnprintf(buf + ret, RD_BUF - ret, "\n PCMSyCH registers\n");
228
229 /*
230 * the value 10 is the number of PDIs. We will need a
231 * cleanup to remove hard-coded Intel configurations
232 * from cadence_master.c
233 */
234 for (j = 0; j < 10; j++) {
235 ret += intel_sprintf(s, false, buf, ret,
236 SDW_SHIM_PCMSYCHM(i, j));
237 ret += intel_sprintf(s, false, buf, ret,
238 SDW_SHIM_PCMSYCHC(i, j));
239 }
240 ret += scnprintf(buf + ret, RD_BUF - ret, "\n PDMSCAP, IOCTL, CTMCTL\n");
241
242 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PDMSCAP(i));
243 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_IOCTL(i));
244 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTMCTL(i));
245 }
246
247 ret += scnprintf(buf + ret, RD_BUF - ret, "\nWake registers\n");
248 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKEEN);
249 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKESTS);
250
251 ret += scnprintf(buf + ret, RD_BUF - ret, "\nALH STRMzCFG\n");
252 for (i = 0; i < SDW_ALH_NUM_STREAMS; i++)
253 ret += intel_sprintf(a, true, buf, ret, SDW_ALH_STRMZCFG(i));
254
255 seq_printf(s_file, "%s", buf);
256 kfree(buf);
257
258 return 0;
259}
260DEFINE_SHOW_ATTRIBUTE(intel_reg);
261
262static void intel_debugfs_init(struct sdw_intel *sdw)
263{
264 struct dentry *root = sdw->cdns.bus.debugfs;
265
266 if (!root)
267 return;
268
269 sdw->debugfs = debugfs_create_dir("intel-sdw", root);
270
271 debugfs_create_file("intel-registers", 0400, sdw->debugfs, sdw,
272 &intel_reg_fops);
273
274 sdw_cdns_debugfs_init(&sdw->cdns, sdw->debugfs);
275}
276
277static void intel_debugfs_exit(struct sdw_intel *sdw)
278{
279 debugfs_remove_recursive(sdw->debugfs);
280}
281#else
282static void intel_debugfs_init(struct sdw_intel *sdw) {}
283static void intel_debugfs_exit(struct sdw_intel *sdw) {}
284#endif /* CONFIG_DEBUG_FS */
285
286/*
Vinod Koul71bb8a12017-12-14 11:19:43 +0530287 * shim ops
288 */
289
290static int intel_link_power_up(struct sdw_intel *sdw)
291{
292 unsigned int link_id = sdw->instance;
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600293 void __iomem *shim = sdw->link_res->shim;
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800294 u32 *shim_mask = sdw->link_res->shim_mask;
295 struct sdw_bus *bus = &sdw->cdns.bus;
296 struct sdw_master_prop *prop = &bus->prop;
Vinod Koul71bb8a12017-12-14 11:19:43 +0530297 int spa_mask, cpa_mask;
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800298 int link_control;
299 int ret = 0;
300 u32 syncprd;
301 u32 sync_reg;
302
303 mutex_lock(sdw->link_res->shim_lock);
304
305 /*
306 * The hardware relies on an internal counter, typically 4kHz,
307 * to generate the SoundWire SSP - which defines a 'safe'
308 * synchronization point between commands and audio transport
309 * and allows for multi link synchronization. The SYNCPRD value
310 * is only dependent on the oscillator clock provided to
311 * the IP, so adjust based on _DSD properties reported in DSDT
312 * tables. The values reported are based on either 24MHz
313 * (CNL/CML) or 38.4 MHz (ICL/TGL+).
314 */
315 if (prop->mclk_freq % 6000000)
316 syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_38_4;
317 else
318 syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24;
319
320 if (!*shim_mask) {
321 /* we first need to program the SyncPRD/CPU registers */
322 dev_dbg(sdw->cdns.dev,
323 "%s: first link up, programming SYNCPRD\n", __func__);
324
325 /* set SyncPRD period */
326 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
327 sync_reg |= (syncprd <<
328 SDW_REG_SHIFT(SDW_SHIM_SYNC_SYNCPRD));
329
330 /* Set SyncCPU bit */
331 sync_reg |= SDW_SHIM_SYNC_SYNCCPU;
332 intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
333 }
Vinod Koul71bb8a12017-12-14 11:19:43 +0530334
335 /* Link power up sequence */
336 link_control = intel_readl(shim, SDW_SHIM_LCTL);
337 spa_mask = (SDW_SHIM_LCTL_SPA << link_id);
338 cpa_mask = (SDW_SHIM_LCTL_CPA << link_id);
339 link_control |= spa_mask;
340
341 ret = intel_set_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800342 if (ret < 0) {
343 dev_err(sdw->cdns.dev, "Failed to power up link: %d\n", ret);
344 goto out;
345 }
346
347 if (!*shim_mask) {
348 /* SyncCPU will change once link is active */
349 ret = intel_wait_bit(shim, SDW_SHIM_SYNC,
350 SDW_SHIM_SYNC_SYNCCPU, 0);
351 if (ret < 0) {
352 dev_err(sdw->cdns.dev,
353 "Failed to set SHIM_SYNC: %d\n", ret);
354 goto out;
355 }
356 }
357
358 *shim_mask |= BIT(link_id);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530359
360 sdw->cdns.link_up = true;
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800361out:
362 mutex_unlock(sdw->link_res->shim_lock);
363
364 return ret;
Vinod Koul71bb8a12017-12-14 11:19:43 +0530365}
366
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800367/* this needs to be called with shim_lock */
368static void intel_shim_glue_to_master_ip(struct sdw_intel *sdw)
Vinod Koul71bb8a12017-12-14 11:19:43 +0530369{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600370 void __iomem *shim = sdw->link_res->shim;
Vinod Koul71bb8a12017-12-14 11:19:43 +0530371 unsigned int link_id = sdw->instance;
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800372 u16 ioctl;
Vinod Koul71bb8a12017-12-14 11:19:43 +0530373
374 /* Switch to MIP from Glue logic */
375 ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
376
377 ioctl &= ~(SDW_SHIM_IOCTL_DOE);
378 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800379 usleep_range(10, 15);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530380
381 ioctl &= ~(SDW_SHIM_IOCTL_DO);
382 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800383 usleep_range(10, 15);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530384
385 ioctl |= (SDW_SHIM_IOCTL_MIF);
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_BKE);
390 ioctl &= ~(SDW_SHIM_IOCTL_COE);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530391 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800392 usleep_range(10, 15);
393
394 /* at this point Master IP has full control of the I/Os */
395}
396
397/* this needs to be called with shim_lock */
398static void intel_shim_master_ip_to_glue(struct sdw_intel *sdw)
399{
400 unsigned int link_id = sdw->instance;
401 void __iomem *shim = sdw->link_res->shim;
402 u16 ioctl;
403
404 /* Glue logic */
405 ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
406 ioctl |= SDW_SHIM_IOCTL_BKE;
407 ioctl |= SDW_SHIM_IOCTL_COE;
408 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
409 usleep_range(10, 15);
410
411 ioctl &= ~(SDW_SHIM_IOCTL_MIF);
412 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
413 usleep_range(10, 15);
414
415 /* at this point Integration Glue has full control of the I/Os */
416}
417
418static int intel_shim_init(struct sdw_intel *sdw, bool clock_stop)
419{
420 void __iomem *shim = sdw->link_res->shim;
421 unsigned int link_id = sdw->instance;
422 int ret = 0;
423 u16 ioctl = 0, act = 0;
424
425 mutex_lock(sdw->link_res->shim_lock);
426
427 /* Initialize Shim */
428 ioctl |= SDW_SHIM_IOCTL_BKE;
429 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
430 usleep_range(10, 15);
431
432 ioctl |= SDW_SHIM_IOCTL_WPDD;
433 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
434 usleep_range(10, 15);
435
436 ioctl |= SDW_SHIM_IOCTL_DO;
437 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
438 usleep_range(10, 15);
439
440 ioctl |= SDW_SHIM_IOCTL_DOE;
441 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
442 usleep_range(10, 15);
443
444 intel_shim_glue_to_master_ip(sdw);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530445
446 act |= 0x1 << SDW_REG_SHIFT(SDW_SHIM_CTMCTL_DOAIS);
447 act |= SDW_SHIM_CTMCTL_DACTQE;
448 act |= SDW_SHIM_CTMCTL_DODS;
449 intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800450 usleep_range(10, 15);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530451
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800452 mutex_unlock(sdw->link_res->shim_lock);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530453
Vinod Koul71bb8a12017-12-14 11:19:43 +0530454 return ret;
455}
456
Rander Wangab2c9132020-07-16 23:09:46 +0800457static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800458{
459 void __iomem *shim = sdw->link_res->shim;
460 unsigned int link_id = sdw->instance;
461 u16 wake_en, wake_sts;
462
463 mutex_lock(sdw->link_res->shim_lock);
464 wake_en = intel_readw(shim, SDW_SHIM_WAKEEN);
465
466 if (wake_enable) {
467 /* Enable the wakeup */
468 wake_en |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
469 intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
470 } else {
471 /* Disable the wake up interrupt */
472 wake_en &= ~(SDW_SHIM_WAKEEN_ENABLE << link_id);
473 intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
474
475 /* Clear wake status */
476 wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
477 wake_sts |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
478 intel_writew(shim, SDW_SHIM_WAKESTS_STATUS, wake_sts);
479 }
480 mutex_unlock(sdw->link_res->shim_lock);
481}
482
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +0800483static int intel_link_power_down(struct sdw_intel *sdw)
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800484{
485 int link_control, spa_mask, cpa_mask;
486 unsigned int link_id = sdw->instance;
487 void __iomem *shim = sdw->link_res->shim;
488 u32 *shim_mask = sdw->link_res->shim_mask;
489 int ret = 0;
490
491 mutex_lock(sdw->link_res->shim_lock);
492
493 intel_shim_master_ip_to_glue(sdw);
494
495 /* Link power down sequence */
496 link_control = intel_readl(shim, SDW_SHIM_LCTL);
497 spa_mask = ~(SDW_SHIM_LCTL_SPA << link_id);
498 cpa_mask = (SDW_SHIM_LCTL_CPA << link_id);
499 link_control &= spa_mask;
500
501 ret = intel_clear_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
502
503 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
509 mutex_unlock(sdw->link_res->shim_lock);
510
Vinod Koul71bb8a12017-12-14 11:19:43 +0530511 if (ret < 0)
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800512 return ret;
513
514 sdw->cdns.link_up = false;
515 return 0;
516}
517
Pierre-Louis Bossart02629e452020-07-16 23:09:41 +0800518static void intel_shim_sync_arm(struct sdw_intel *sdw)
519{
520 void __iomem *shim = sdw->link_res->shim;
521 u32 sync_reg;
522
523 mutex_lock(sdw->link_res->shim_lock);
524
525 /* update SYNC register */
526 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
527 sync_reg |= (SDW_SHIM_SYNC_CMDSYNC << sdw->instance);
528 intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
529
530 mutex_unlock(sdw->link_res->shim_lock);
531}
532
Pierre-Louis Bossart437e3282020-07-16 23:09:42 +0800533static int intel_shim_sync_go_unlocked(struct sdw_intel *sdw)
534{
535 void __iomem *shim = sdw->link_res->shim;
536 u32 sync_reg;
537 int ret;
538
539 /* Read SYNC register */
540 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
541
542 /*
543 * Set SyncGO bit to synchronously trigger a bank switch for
544 * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all
545 * the Masters.
546 */
547 sync_reg |= SDW_SHIM_SYNC_SYNCGO;
548
549 ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg,
550 SDW_SHIM_SYNC_SYNCGO);
551
552 if (ret < 0)
553 dev_err(sdw->cdns.dev, "SyncGO clear failed: %d\n", ret);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530554
555 return ret;
556}
557
Vinod Koul37a2d222018-04-26 18:38:58 +0530558/*
559 * PDI routines
560 */
561static void intel_pdi_init(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500562 struct sdw_cdns_stream_config *config)
Vinod Koul37a2d222018-04-26 18:38:58 +0530563{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600564 void __iomem *shim = sdw->link_res->shim;
Vinod Koul37a2d222018-04-26 18:38:58 +0530565 unsigned int link_id = sdw->instance;
566 int pcm_cap, pdm_cap;
567
568 /* PCM Stream Capability */
569 pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id));
570
571 config->pcm_bd = (pcm_cap & SDW_SHIM_PCMSCAP_BSS) >>
572 SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_BSS);
573 config->pcm_in = (pcm_cap & SDW_SHIM_PCMSCAP_ISS) >>
574 SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_ISS);
575 config->pcm_out = (pcm_cap & SDW_SHIM_PCMSCAP_OSS) >>
576 SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_OSS);
577
Pierre-Louis Bossart121f4362019-05-22 14:47:29 -0500578 dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
579 config->pcm_bd, config->pcm_in, config->pcm_out);
580
Vinod Koul37a2d222018-04-26 18:38:58 +0530581 /* PDM Stream Capability */
582 pdm_cap = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
583
584 config->pdm_bd = (pdm_cap & SDW_SHIM_PDMSCAP_BSS) >>
585 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_BSS);
586 config->pdm_in = (pdm_cap & SDW_SHIM_PDMSCAP_ISS) >>
587 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_ISS);
588 config->pdm_out = (pdm_cap & SDW_SHIM_PDMSCAP_OSS) >>
589 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_OSS);
Pierre-Louis Bossart121f4362019-05-22 14:47:29 -0500590
591 dev_dbg(sdw->cdns.dev, "PDM cap bd:%d in:%d out:%d\n",
592 config->pdm_bd, config->pdm_in, config->pdm_out);
Vinod Koul37a2d222018-04-26 18:38:58 +0530593}
594
595static int
596intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num, bool pcm)
597{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600598 void __iomem *shim = sdw->link_res->shim;
Vinod Koul37a2d222018-04-26 18:38:58 +0530599 unsigned int link_id = sdw->instance;
600 int count;
601
602 if (pcm) {
603 count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num));
Pierre-Louis Bossart18046332019-08-05 19:55:07 -0500604
605 /*
606 * WORKAROUND: on all existing Intel controllers, pdi
607 * number 2 reports channel count as 1 even though it
608 * supports 8 channels. Performing hardcoding for pdi
609 * number 2.
610 */
611 if (pdi_num == 2)
612 count = 7;
613
Vinod Koul37a2d222018-04-26 18:38:58 +0530614 } else {
615 count = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
616 count = ((count & SDW_SHIM_PDMSCAP_CPSS) >>
617 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_CPSS));
618 }
619
620 /* zero based values for channel count in register */
621 count++;
622
623 return count;
624}
625
626static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500627 struct sdw_cdns_pdi *pdi,
628 unsigned int num_pdi,
629 unsigned int *num_ch, bool pcm)
Vinod Koul37a2d222018-04-26 18:38:58 +0530630{
631 int i, ch_count = 0;
632
633 for (i = 0; i < num_pdi; i++) {
634 pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num, pcm);
635 ch_count += pdi->ch_count;
636 pdi++;
637 }
638
639 *num_ch = ch_count;
640 return 0;
641}
642
643static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500644 struct sdw_cdns_streams *stream, bool pcm)
Vinod Koul37a2d222018-04-26 18:38:58 +0530645{
646 intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500647 &stream->num_ch_bd, pcm);
Vinod Koul37a2d222018-04-26 18:38:58 +0530648
649 intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500650 &stream->num_ch_in, pcm);
Vinod Koul37a2d222018-04-26 18:38:58 +0530651
652 intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500653 &stream->num_ch_out, pcm);
Vinod Koul37a2d222018-04-26 18:38:58 +0530654
655 return 0;
656}
657
658static int intel_pdi_ch_update(struct sdw_intel *sdw)
659{
660 /* First update PCM streams followed by PDM streams */
661 intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm, true);
662 intel_pdi_stream_ch_update(sdw, &sdw->cdns.pdm, false);
663
664 return 0;
665}
666
667static void
668intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
669{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600670 void __iomem *shim = sdw->link_res->shim;
Vinod Koul37a2d222018-04-26 18:38:58 +0530671 unsigned int link_id = sdw->instance;
672 int pdi_conf = 0;
673
Pierre-Louis Bossartc134f912019-10-22 18:29:48 -0500674 /* the Bulk and PCM streams are not contiguous */
675 pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
676 if (pdi->num >= 2)
677 pdi->intel_alh_id += 2;
Vinod Koul37a2d222018-04-26 18:38:58 +0530678
679 /*
680 * Program stream parameters to stream SHIM register
681 * This is applicable for PCM stream only.
682 */
683 if (pdi->type != SDW_STREAM_PCM)
684 return;
685
686 if (pdi->dir == SDW_DATA_DIR_RX)
687 pdi_conf |= SDW_SHIM_PCMSYCM_DIR;
688 else
689 pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR);
690
691 pdi_conf |= (pdi->intel_alh_id <<
692 SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_STREAM));
693 pdi_conf |= (pdi->l_ch_num << SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_LCHN));
694 pdi_conf |= (pdi->h_ch_num << SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_HCHN));
695
696 intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
697}
698
699static void
700intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
701{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600702 void __iomem *alh = sdw->link_res->alh;
Vinod Koul37a2d222018-04-26 18:38:58 +0530703 unsigned int link_id = sdw->instance;
704 unsigned int conf;
705
Pierre-Louis Bossartc134f912019-10-22 18:29:48 -0500706 /* the Bulk and PCM streams are not contiguous */
707 pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
708 if (pdi->num >= 2)
709 pdi->intel_alh_id += 2;
Vinod Koul37a2d222018-04-26 18:38:58 +0530710
711 /* Program Stream config ALH register */
712 conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
713
714 conf |= (SDW_ALH_STRMZCFG_DMAT_VAL <<
715 SDW_REG_SHIFT(SDW_ALH_STRMZCFG_DMAT));
716
717 conf |= ((pdi->ch_count - 1) <<
718 SDW_REG_SHIFT(SDW_ALH_STRMZCFG_CHN));
719
720 intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
721}
722
Rander Wang4b206d32019-12-11 19:45:02 -0600723static int intel_params_stream(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500724 struct snd_pcm_substream *substream,
725 struct snd_soc_dai *dai,
Rander Wang4b206d32019-12-11 19:45:02 -0600726 struct snd_pcm_hw_params *hw_params,
727 int link_id, int alh_stream_id)
Vinod Koulc46302e2018-04-26 18:39:05 +0530728{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600729 struct sdw_intel_link_res *res = sdw->link_res;
Rander Wang4b206d32019-12-11 19:45:02 -0600730 struct sdw_intel_stream_params_data params_data;
Pierre-Louis Bossart05c8afe42019-08-05 19:55:06 -0500731
Rander Wang4b206d32019-12-11 19:45:02 -0600732 params_data.substream = substream;
733 params_data.dai = dai;
734 params_data.hw_params = hw_params;
735 params_data.link_id = link_id;
736 params_data.alh_stream_id = alh_stream_id;
Vinod Koulc46302e2018-04-26 18:39:05 +0530737
Rander Wang4b206d32019-12-11 19:45:02 -0600738 if (res->ops && res->ops->params_stream && res->dev)
739 return res->ops->params_stream(res->dev,
740 &params_data);
Vinod Koulc46302e2018-04-26 18:39:05 +0530741 return -EIO;
742}
743
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -0600744static int intel_free_stream(struct sdw_intel *sdw,
745 struct snd_pcm_substream *substream,
746 struct snd_soc_dai *dai,
747 int link_id)
748{
749 struct sdw_intel_link_res *res = sdw->link_res;
750 struct sdw_intel_stream_free_data free_data;
751
752 free_data.substream = substream;
753 free_data.dai = dai;
754 free_data.link_id = link_id;
755
756 if (res->ops && res->ops->free_stream && res->dev)
757 return res->ops->free_stream(res->dev,
758 &free_data);
759
760 return 0;
761}
762
Vinod Koulc46302e2018-04-26 18:39:05 +0530763/*
Shreyas NC30246e22018-07-27 14:44:17 +0530764 * bank switch routines
765 */
766
767static int intel_pre_bank_switch(struct sdw_bus *bus)
768{
769 struct sdw_cdns *cdns = bus_to_cdns(bus);
770 struct sdw_intel *sdw = cdns_to_intel(cdns);
Shreyas NC30246e22018-07-27 14:44:17 +0530771
772 /* Write to register only for multi-link */
773 if (!bus->multi_link)
774 return 0;
775
Pierre-Louis Bossart02629e452020-07-16 23:09:41 +0800776 intel_shim_sync_arm(sdw);
Shreyas NC30246e22018-07-27 14:44:17 +0530777
778 return 0;
779}
780
781static int intel_post_bank_switch(struct sdw_bus *bus)
782{
783 struct sdw_cdns *cdns = bus_to_cdns(bus);
784 struct sdw_intel *sdw = cdns_to_intel(cdns);
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600785 void __iomem *shim = sdw->link_res->shim;
Shreyas NC30246e22018-07-27 14:44:17 +0530786 int sync_reg, ret;
787
788 /* Write to register only for multi-link */
789 if (!bus->multi_link)
790 return 0;
791
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800792 mutex_lock(sdw->link_res->shim_lock);
793
Shreyas NC30246e22018-07-27 14:44:17 +0530794 /* Read SYNC register */
795 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
796
797 /*
798 * post_bank_switch() ops is called from the bus in loop for
799 * all the Masters in the steam with the expectation that
800 * we trigger the bankswitch for the only first Master in the list
801 * and do nothing for the other Masters
802 *
803 * So, set the SYNCGO bit only if CMDSYNC bit is set for any Master.
804 */
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800805 if (!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK)) {
806 ret = 0;
807 goto unlock;
808 }
Shreyas NC30246e22018-07-27 14:44:17 +0530809
Pierre-Louis Bossart437e3282020-07-16 23:09:42 +0800810 ret = intel_shim_sync_go_unlocked(sdw);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +0800811unlock:
812 mutex_unlock(sdw->link_res->shim_lock);
Shreyas NC30246e22018-07-27 14:44:17 +0530813
Shreyas NC30246e22018-07-27 14:44:17 +0530814 if (ret < 0)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500815 dev_err(sdw->cdns.dev, "Post bank switch failed: %d\n", ret);
Shreyas NC30246e22018-07-27 14:44:17 +0530816
817 return ret;
818}
819
820/*
Vinod Koulc46302e2018-04-26 18:39:05 +0530821 * DAI routines
822 */
823
Rander Wang5e7484d2020-02-14 19:47:39 -0600824static int intel_startup(struct snd_pcm_substream *substream,
825 struct snd_soc_dai *dai)
826{
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +0800827 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
828 int ret;
829
830 ret = pm_runtime_get_sync(cdns->dev);
831 if (ret < 0 && ret != -EACCES) {
832 dev_err_ratelimited(cdns->dev,
833 "pm_runtime_get_sync failed in %s, ret %d\n",
834 __func__, ret);
835 pm_runtime_put_noidle(cdns->dev);
836 return ret;
837 }
Pierre-Louis Bossartff16d1e2020-07-01 02:43:54 +0800838 return 0;
Rander Wang5e7484d2020-02-14 19:47:39 -0600839}
840
Vinod Koulc46302e2018-04-26 18:39:05 +0530841static int intel_hw_params(struct snd_pcm_substream *substream,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500842 struct snd_pcm_hw_params *params,
843 struct snd_soc_dai *dai)
Vinod Koulc46302e2018-04-26 18:39:05 +0530844{
845 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
846 struct sdw_intel *sdw = cdns_to_intel(cdns);
847 struct sdw_cdns_dma_data *dma;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500848 struct sdw_cdns_pdi *pdi;
Vinod Koulc46302e2018-04-26 18:39:05 +0530849 struct sdw_stream_config sconfig;
850 struct sdw_port_config *pconfig;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500851 int ch, dir;
852 int ret;
Vinod Koulc46302e2018-04-26 18:39:05 +0530853 bool pcm = true;
854
855 dma = snd_soc_dai_get_dma_data(dai, substream);
856 if (!dma)
857 return -EIO;
858
859 ch = params_channels(params);
860 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
861 dir = SDW_DATA_DIR_RX;
862 else
863 dir = SDW_DATA_DIR_TX;
864
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500865 if (dma->stream_type == SDW_STREAM_PDM)
Vinod Koulc46302e2018-04-26 18:39:05 +0530866 pcm = false;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500867
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500868 if (pcm)
Bard Liao1b53385e2019-09-16 14:23:48 -0500869 pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500870 else
Bard Liao1b53385e2019-09-16 14:23:48 -0500871 pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pdm, ch, dir, dai->id);
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500872
873 if (!pdi) {
874 ret = -EINVAL;
875 goto error;
Vinod Koulc46302e2018-04-26 18:39:05 +0530876 }
877
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500878 /* do run-time configurations for SHIM, ALH and PDI/PORT */
879 intel_pdi_shim_configure(sdw, pdi);
880 intel_pdi_alh_configure(sdw, pdi);
881 sdw_cdns_config_stream(cdns, ch, dir, pdi);
Vinod Koulc46302e2018-04-26 18:39:05 +0530882
Bard Liaoa5a02392020-08-17 23:29:16 +0800883 /* store pdi and hw_params, may be needed in prepare step */
884 dma->suspended = false;
885 dma->pdi = pdi;
886 dma->hw_params = params;
Vinod Koulc46302e2018-04-26 18:39:05 +0530887
888 /* Inform DSP about PDI stream number */
Rander Wang4b206d32019-12-11 19:45:02 -0600889 ret = intel_params_stream(sdw, substream, dai, params,
890 sdw->instance,
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500891 pdi->intel_alh_id);
892 if (ret)
893 goto error;
Vinod Koulc46302e2018-04-26 18:39:05 +0530894
895 sconfig.direction = dir;
896 sconfig.ch_count = ch;
897 sconfig.frame_rate = params_rate(params);
898 sconfig.type = dma->stream_type;
899
900 if (dma->stream_type == SDW_STREAM_PDM) {
901 sconfig.frame_rate *= 50;
902 sconfig.bps = 1;
903 } else {
904 sconfig.bps = snd_pcm_format_width(params_format(params));
905 }
906
907 /* Port configuration */
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500908 pconfig = kcalloc(1, sizeof(*pconfig), GFP_KERNEL);
Vinod Koulc46302e2018-04-26 18:39:05 +0530909 if (!pconfig) {
910 ret = -ENOMEM;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500911 goto error;
Vinod Koulc46302e2018-04-26 18:39:05 +0530912 }
913
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500914 pconfig->num = pdi->num;
915 pconfig->ch_mask = (1 << ch) - 1;
Vinod Koulc46302e2018-04-26 18:39:05 +0530916
917 ret = sdw_stream_add_master(&cdns->bus, &sconfig,
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500918 pconfig, 1, dma->stream);
919 if (ret)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500920 dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
Vinod Koulc46302e2018-04-26 18:39:05 +0530921
922 kfree(pconfig);
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500923error:
Vinod Koulc46302e2018-04-26 18:39:05 +0530924 return ret;
925}
926
Rander Wang27b198f2020-02-14 19:47:37 -0600927static int intel_prepare(struct snd_pcm_substream *substream,
928 struct snd_soc_dai *dai)
929{
Bard Liaoa5a02392020-08-17 23:29:16 +0800930 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
931 struct sdw_intel *sdw = cdns_to_intel(cdns);
Rander Wang27b198f2020-02-14 19:47:37 -0600932 struct sdw_cdns_dma_data *dma;
Bard Liaoa5a02392020-08-17 23:29:16 +0800933 int ch, dir;
934 int ret;
Rander Wang27b198f2020-02-14 19:47:37 -0600935
936 dma = snd_soc_dai_get_dma_data(dai, substream);
937 if (!dma) {
938 dev_err(dai->dev, "failed to get dma data in %s",
939 __func__);
940 return -EIO;
941 }
942
Bard Liaoa5a02392020-08-17 23:29:16 +0800943 if (dma->suspended) {
944 dma->suspended = false;
945
946 /*
947 * .prepare() is called after system resume, where we
948 * need to reinitialize the SHIM/ALH/Cadence IP.
949 * .prepare() is also called to deal with underflows,
950 * but in those cases we cannot touch ALH/SHIM
951 * registers
952 */
953
954 /* configure stream */
955 ch = params_channels(dma->hw_params);
956 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
957 dir = SDW_DATA_DIR_RX;
958 else
959 dir = SDW_DATA_DIR_TX;
960
961 intel_pdi_shim_configure(sdw, dma->pdi);
962 intel_pdi_alh_configure(sdw, dma->pdi);
963 sdw_cdns_config_stream(cdns, ch, dir, dma->pdi);
964
965 /* Inform DSP about PDI stream number */
966 ret = intel_params_stream(sdw, substream, dai,
967 dma->hw_params,
968 sdw->instance,
969 dma->pdi->intel_alh_id);
970 if (ret)
971 goto err;
972 }
973
974 ret = sdw_prepare_stream(dma->stream);
975
976err:
977 return ret;
Rander Wang27b198f2020-02-14 19:47:37 -0600978}
979
Rander Wang973a8422020-02-14 19:47:38 -0600980static int intel_trigger(struct snd_pcm_substream *substream, int cmd,
981 struct snd_soc_dai *dai)
982{
983 struct sdw_cdns_dma_data *dma;
984 int ret;
985
986 dma = snd_soc_dai_get_dma_data(dai, substream);
987 if (!dma) {
988 dev_err(dai->dev, "failed to get dma data in %s", __func__);
989 return -EIO;
990 }
991
992 switch (cmd) {
993 case SNDRV_PCM_TRIGGER_START:
994 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
995 case SNDRV_PCM_TRIGGER_RESUME:
996 ret = sdw_enable_stream(dma->stream);
997 break;
998
999 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1000 case SNDRV_PCM_TRIGGER_SUSPEND:
1001 case SNDRV_PCM_TRIGGER_STOP:
1002 ret = sdw_disable_stream(dma->stream);
1003 break;
1004
1005 default:
1006 ret = -EINVAL;
1007 break;
1008 }
1009
1010 if (ret)
1011 dev_err(dai->dev,
1012 "%s trigger %d failed: %d",
1013 __func__, cmd, ret);
1014 return ret;
1015}
1016
Vinod Koulc46302e2018-04-26 18:39:05 +05301017static int
1018intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
1019{
1020 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001021 struct sdw_intel *sdw = cdns_to_intel(cdns);
Vinod Koulc46302e2018-04-26 18:39:05 +05301022 struct sdw_cdns_dma_data *dma;
1023 int ret;
1024
1025 dma = snd_soc_dai_get_dma_data(dai, substream);
1026 if (!dma)
1027 return -EIO;
1028
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001029 ret = sdw_deprepare_stream(dma->stream);
1030 if (ret) {
1031 dev_err(dai->dev, "sdw_deprepare_stream: failed %d", ret);
1032 return ret;
1033 }
1034
Vinod Koulc46302e2018-04-26 18:39:05 +05301035 ret = sdw_stream_remove_master(&cdns->bus, dma->stream);
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001036 if (ret < 0) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001037 dev_err(dai->dev, "remove master from stream %s failed: %d\n",
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001038 dma->stream->name, ret);
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001039 return ret;
1040 }
Vinod Koulc46302e2018-04-26 18:39:05 +05301041
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001042 ret = intel_free_stream(sdw, substream, dai, sdw->instance);
1043 if (ret < 0) {
1044 dev_err(dai->dev, "intel_free_stream: failed %d", ret);
1045 return ret;
1046 }
1047
Bard Liaoa5a02392020-08-17 23:29:16 +08001048 dma->hw_params = NULL;
1049 dma->pdi = NULL;
1050
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -06001051 return 0;
Vinod Koulc46302e2018-04-26 18:39:05 +05301052}
1053
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -05001054static void intel_shutdown(struct snd_pcm_substream *substream,
1055 struct snd_soc_dai *dai)
1056{
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001057 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -05001058
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001059 pm_runtime_mark_last_busy(cdns->dev);
1060 pm_runtime_put_autosuspend(cdns->dev);
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -05001061}
1062
Bard Liaoa5a02392020-08-17 23:29:16 +08001063static int intel_component_dais_suspend(struct snd_soc_component *component)
1064{
1065 struct sdw_cdns_dma_data *dma;
1066 struct snd_soc_dai *dai;
1067
1068 for_each_component_dais(component, dai) {
1069 /*
1070 * we don't have a .suspend dai_ops, and we don't have access
1071 * to the substream, so let's mark both capture and playback
1072 * DMA contexts as suspended
1073 */
1074 dma = dai->playback_dma_data;
1075 if (dma)
1076 dma->suspended = true;
1077
1078 dma = dai->capture_dma_data;
1079 if (dma)
1080 dma->suspended = true;
1081 }
1082
1083 return 0;
1084}
1085
Vinod Koulc46302e2018-04-26 18:39:05 +05301086static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001087 void *stream, int direction)
Vinod Koulc46302e2018-04-26 18:39:05 +05301088{
1089 return cdns_set_sdw_stream(dai, stream, true, direction);
1090}
1091
1092static int intel_pdm_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001093 void *stream, int direction)
Vinod Koulc46302e2018-04-26 18:39:05 +05301094{
1095 return cdns_set_sdw_stream(dai, stream, false, direction);
1096}
1097
Pierre-Louis Bossart09553142020-07-01 02:43:52 +08001098static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
1099 int direction)
1100{
1101 struct sdw_cdns_dma_data *dma;
1102
1103 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1104 dma = dai->playback_dma_data;
1105 else
1106 dma = dai->capture_dma_data;
1107
1108 if (!dma)
1109 return NULL;
1110
1111 return dma->stream;
1112}
1113
Julia Lawallb1635592018-10-27 15:34:42 +02001114static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
Rander Wang5e7484d2020-02-14 19:47:39 -06001115 .startup = intel_startup,
Vinod Koulc46302e2018-04-26 18:39:05 +05301116 .hw_params = intel_hw_params,
Rander Wang27b198f2020-02-14 19:47:37 -06001117 .prepare = intel_prepare,
Rander Wang973a8422020-02-14 19:47:38 -06001118 .trigger = intel_trigger,
Vinod Koulc46302e2018-04-26 18:39:05 +05301119 .hw_free = intel_hw_free,
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -05001120 .shutdown = intel_shutdown,
Vinod Koulc46302e2018-04-26 18:39:05 +05301121 .set_sdw_stream = intel_pcm_set_sdw_stream,
Pierre-Louis Bossart09553142020-07-01 02:43:52 +08001122 .get_sdw_stream = intel_get_sdw_stream,
Vinod Koulc46302e2018-04-26 18:39:05 +05301123};
1124
Julia Lawallb1635592018-10-27 15:34:42 +02001125static const struct snd_soc_dai_ops intel_pdm_dai_ops = {
Rander Wang5e7484d2020-02-14 19:47:39 -06001126 .startup = intel_startup,
Vinod Koulc46302e2018-04-26 18:39:05 +05301127 .hw_params = intel_hw_params,
Rander Wang27b198f2020-02-14 19:47:37 -06001128 .prepare = intel_prepare,
Rander Wang973a8422020-02-14 19:47:38 -06001129 .trigger = intel_trigger,
Vinod Koulc46302e2018-04-26 18:39:05 +05301130 .hw_free = intel_hw_free,
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -05001131 .shutdown = intel_shutdown,
Vinod Koulc46302e2018-04-26 18:39:05 +05301132 .set_sdw_stream = intel_pdm_set_sdw_stream,
Pierre-Louis Bossart09553142020-07-01 02:43:52 +08001133 .get_sdw_stream = intel_get_sdw_stream,
Vinod Koulc46302e2018-04-26 18:39:05 +05301134};
1135
1136static const struct snd_soc_component_driver dai_component = {
1137 .name = "soundwire",
Bard Liaoa5a02392020-08-17 23:29:16 +08001138 .suspend = intel_component_dais_suspend
Vinod Koulc46302e2018-04-26 18:39:05 +05301139};
1140
1141static int intel_create_dai(struct sdw_cdns *cdns,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001142 struct snd_soc_dai_driver *dais,
1143 enum intel_pdi_type type,
1144 u32 num, u32 off, u32 max_ch, bool pcm)
Vinod Koulc46302e2018-04-26 18:39:05 +05301145{
1146 int i;
1147
1148 if (num == 0)
1149 return 0;
1150
1151 /* TODO: Read supported rates/formats from hardware */
1152 for (i = off; i < (off + num); i++) {
Pierre-Louis Bossartbf6d6e62020-06-17 11:35:36 -05001153 dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
1154 "SDW%d Pin%d",
1155 cdns->instance, i);
Vinod Koulc46302e2018-04-26 18:39:05 +05301156 if (!dais[i].name)
1157 return -ENOMEM;
1158
1159 if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
Vinod Koulc46302e2018-04-26 18:39:05 +05301160 dais[i].playback.channels_min = 1;
1161 dais[i].playback.channels_max = max_ch;
1162 dais[i].playback.rates = SNDRV_PCM_RATE_48000;
1163 dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
1164 }
1165
1166 if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
Srinivas Kandagatla39194122019-06-06 12:23:04 +01001167 dais[i].capture.channels_min = 1;
1168 dais[i].capture.channels_max = max_ch;
Vinod Koulc46302e2018-04-26 18:39:05 +05301169 dais[i].capture.rates = SNDRV_PCM_RATE_48000;
1170 dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
1171 }
1172
Vinod Koulc46302e2018-04-26 18:39:05 +05301173 if (pcm)
1174 dais[i].ops = &intel_pcm_dai_ops;
1175 else
1176 dais[i].ops = &intel_pdm_dai_ops;
1177 }
1178
1179 return 0;
1180}
1181
1182static int intel_register_dai(struct sdw_intel *sdw)
1183{
1184 struct sdw_cdns *cdns = &sdw->cdns;
1185 struct sdw_cdns_streams *stream;
1186 struct snd_soc_dai_driver *dais;
1187 int num_dai, ret, off = 0;
1188
1189 /* DAIs are created based on total number of PDIs supported */
1190 num_dai = cdns->pcm.num_pdi + cdns->pdm.num_pdi;
1191
1192 dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
1193 if (!dais)
1194 return -ENOMEM;
1195
1196 /* Create PCM DAIs */
1197 stream = &cdns->pcm;
1198
Bard Liaocf924962019-09-16 14:23:43 -05001199 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
Vinod Koul1215dae2019-05-02 16:29:25 +05301200 off, stream->num_ch_in, true);
Vinod Koulc46302e2018-04-26 18:39:05 +05301201 if (ret)
1202 return ret;
1203
1204 off += cdns->pcm.num_in;
Vinod Koul1215dae2019-05-02 16:29:25 +05301205 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
1206 off, stream->num_ch_out, true);
Vinod Koulc46302e2018-04-26 18:39:05 +05301207 if (ret)
1208 return ret;
1209
1210 off += cdns->pcm.num_out;
Vinod Koul1215dae2019-05-02 16:29:25 +05301211 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
1212 off, stream->num_ch_bd, true);
Vinod Koulc46302e2018-04-26 18:39:05 +05301213 if (ret)
1214 return ret;
1215
1216 /* Create PDM DAIs */
1217 stream = &cdns->pdm;
1218 off += cdns->pcm.num_bd;
Vinod Koul1215dae2019-05-02 16:29:25 +05301219 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pdm.num_in,
1220 off, stream->num_ch_in, false);
Vinod Koulc46302e2018-04-26 18:39:05 +05301221 if (ret)
1222 return ret;
1223
1224 off += cdns->pdm.num_in;
Vinod Koul1215dae2019-05-02 16:29:25 +05301225 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pdm.num_out,
1226 off, stream->num_ch_out, false);
Vinod Koulc46302e2018-04-26 18:39:05 +05301227 if (ret)
1228 return ret;
1229
Bard Liaocf924962019-09-16 14:23:43 -05001230 off += cdns->pdm.num_out;
Vinod Koul1215dae2019-05-02 16:29:25 +05301231 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pdm.num_bd,
1232 off, stream->num_ch_bd, false);
Vinod Koulc46302e2018-04-26 18:39:05 +05301233 if (ret)
1234 return ret;
1235
1236 return snd_soc_register_component(cdns->dev, &dai_component,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001237 dais, num_dai);
Vinod Koulc46302e2018-04-26 18:39:05 +05301238}
1239
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001240static int sdw_master_read_intel_prop(struct sdw_bus *bus)
1241{
1242 struct sdw_master_prop *prop = &bus->prop;
1243 struct fwnode_handle *link;
1244 char name[32];
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001245 u32 quirk_mask;
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001246
1247 /* Find master handle */
1248 snprintf(name, sizeof(name),
1249 "mipi-sdw-link-%d-subproperties", bus->link_id);
1250
1251 link = device_get_named_child_node(bus->dev, name);
1252 if (!link) {
1253 dev_err(bus->dev, "Master node %s not found\n", name);
1254 return -EIO;
1255 }
1256
1257 fwnode_property_read_u32(link,
1258 "intel-sdw-ip-clock",
1259 &prop->mclk_freq);
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001260
Bard Liaoa19efb52020-01-13 17:11:29 -06001261 /* the values reported by BIOS are the 2x clock, not the bus clock */
1262 prop->mclk_freq /= 2;
1263
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001264 fwnode_property_read_u32(link,
1265 "intel-quirk-mask",
1266 &quirk_mask);
1267
1268 if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
1269 prop->hw_disabled = true;
1270
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001271 return 0;
1272}
1273
Vinod Koul71bb8a12017-12-14 11:19:43 +05301274static int intel_prop_read(struct sdw_bus *bus)
1275{
1276 /* Initialize with default handler to read all DisCo properties */
1277 sdw_master_read_prop(bus);
1278
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001279 /* read Intel-specific properties */
1280 sdw_master_read_intel_prop(bus);
1281
Vinod Koul71bb8a12017-12-14 11:19:43 +05301282 return 0;
1283}
1284
Shreyas NCc91605f2018-04-26 18:38:43 +05301285static struct sdw_master_ops sdw_intel_ops = {
1286 .read_prop = sdw_master_read_prop,
1287 .xfer_msg = cdns_xfer_msg,
1288 .xfer_msg_defer = cdns_xfer_msg_defer,
1289 .reset_page_addr = cdns_reset_page_addr,
Vinod Koul07abeff2018-04-26 18:38:48 +05301290 .set_bus_conf = cdns_bus_conf,
Shreyas NC30246e22018-07-27 14:44:17 +05301291 .pre_bank_switch = intel_pre_bank_switch,
1292 .post_bank_switch = intel_post_bank_switch,
Shreyas NCc91605f2018-04-26 18:38:43 +05301293};
1294
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001295static int intel_init(struct sdw_intel *sdw)
1296{
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +08001297 bool clock_stop;
1298
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001299 /* Initialize shim and controller */
1300 intel_link_power_up(sdw);
Pierre-Louis Bossart4a17c442020-07-16 23:09:40 +08001301
1302 clock_stop = sdw_cdns_is_clock_stop(&sdw->cdns);
1303
1304 intel_shim_init(sdw, clock_stop);
1305
1306 if (clock_stop)
1307 return 0;
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001308
Rander Wang7b174f22020-03-17 11:33:14 -05001309 return sdw_cdns_init(&sdw->cdns);
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001310}
1311
Vinod Koul71bb8a12017-12-14 11:19:43 +05301312/*
1313 * probe and init
1314 */
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001315static int intel_master_probe(struct platform_device *pdev)
Vinod Koul71bb8a12017-12-14 11:19:43 +05301316{
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001317 struct device *dev = &pdev->dev;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301318 struct sdw_intel *sdw;
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001319 struct sdw_cdns *cdns;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001320 struct sdw_bus *bus;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301321 int ret;
1322
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001323 sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301324 if (!sdw)
1325 return -ENOMEM;
1326
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001327 cdns = &sdw->cdns;
1328 bus = &cdns->bus;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301329
Vinod Koul71bb8a12017-12-14 11:19:43 +05301330 sdw->instance = pdev->id;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001331 sdw->link_res = dev_get_platdata(dev);
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001332 cdns->dev = dev;
1333 cdns->registers = sdw->link_res->registers;
1334 cdns->instance = sdw->instance;
1335 cdns->msg_count = 0;
1336
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001337 bus->link_id = pdev->id;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301338
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001339 sdw_cdns_probe(cdns);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301340
1341 /* Set property read ops */
Shreyas NCc91605f2018-04-26 18:38:43 +05301342 sdw_intel_ops.read_prop = intel_prop_read;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001343 bus->ops = &sdw_intel_ops;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301344
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001345 /* set driver data, accessed by snd_soc_dai_get_drvdata() */
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001346 dev_set_drvdata(dev, cdns);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301347
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001348 ret = sdw_bus_master_add(bus, dev, dev->fwnode);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301349 if (ret) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001350 dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001351 return ret;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301352 }
1353
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001354 if (bus->prop.hw_disabled)
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001355 dev_info(dev,
1356 "SoundWire master %d is disabled, will be ignored\n",
1357 bus->link_id);
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001358
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001359 return 0;
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001360}
1361
1362int intel_master_startup(struct platform_device *pdev)
1363{
1364 struct sdw_cdns_stream_config config;
1365 struct device *dev = &pdev->dev;
1366 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1367 struct sdw_intel *sdw = cdns_to_intel(cdns);
1368 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001369 int link_flags;
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001370 int ret;
1371
1372 if (bus->prop.hw_disabled) {
1373 dev_info(dev,
1374 "SoundWire master %d is disabled, ignoring\n",
1375 sdw->instance);
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001376 return 0;
1377 }
1378
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001379 /* Initialize shim, controller and Cadence IP */
1380 ret = intel_init(sdw);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301381 if (ret)
1382 goto err_init;
1383
Vinod Koul37a2d222018-04-26 18:38:58 +05301384 /* Read the PDI config and initialize cadence PDI */
1385 intel_pdi_init(sdw, &config);
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001386 ret = sdw_cdns_pdi_init(cdns, config);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301387 if (ret)
1388 goto err_init;
1389
Vinod Koul37a2d222018-04-26 18:38:58 +05301390 intel_pdi_ch_update(sdw);
1391
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001392 ret = sdw_cdns_enable_interrupt(cdns, true);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301393 if (ret < 0) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001394 dev_err(dev, "cannot enable interrupts\n");
Vinod Koul71bb8a12017-12-14 11:19:43 +05301395 goto err_init;
1396 }
1397
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001398 ret = sdw_cdns_exit_reset(cdns);
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001399 if (ret < 0) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001400 dev_err(dev, "unable to exit bus reset sequence\n");
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001401 goto err_interrupt;
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001402 }
1403
Vinod Koulc46302e2018-04-26 18:39:05 +05301404 /* Register DAIs */
1405 ret = intel_register_dai(sdw);
1406 if (ret) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001407 dev_err(dev, "DAI registration failed: %d\n", ret);
1408 snd_soc_unregister_component(dev);
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001409 goto err_interrupt;
Vinod Koulc46302e2018-04-26 18:39:05 +05301410 }
1411
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -05001412 intel_debugfs_init(sdw);
1413
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001414 /* Enable runtime PM */
1415 link_flags = md_flags >> (bus->link_id * 8);
1416 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
1417 pm_runtime_set_autosuspend_delay(dev,
1418 INTEL_MASTER_SUSPEND_DELAY_MS);
1419 pm_runtime_use_autosuspend(dev);
1420 pm_runtime_mark_last_busy(dev);
1421
1422 pm_runtime_set_active(dev);
1423 pm_runtime_enable(dev);
1424 }
1425
Pierre-Louis Bossarta2d9c162020-08-17 23:29:17 +08001426 /*
1427 * The runtime PM status of Slave devices is "Unsupported"
1428 * until they report as ATTACHED. If they don't, e.g. because
1429 * there are no Slave devices populated or if the power-on is
1430 * delayed or dependent on a power switch, the Master will
1431 * remain active and prevent its parent from suspending.
1432 *
1433 * Conditionally force the pm_runtime core to re-evaluate the
1434 * Master status in the absence of any Slave activity. A quirk
1435 * is provided to e.g. deal with Slaves that may be powered on
1436 * with a delay. A more complete solution would require the
1437 * definition of Master properties.
1438 */
1439 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
1440 pm_runtime_idle(dev);
1441
Vinod Koul71bb8a12017-12-14 11:19:43 +05301442 return 0;
1443
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001444err_interrupt:
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001445 sdw_cdns_enable_interrupt(cdns, false);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301446err_init:
Vinod Koul71bb8a12017-12-14 11:19:43 +05301447 return ret;
1448}
1449
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001450static int intel_master_remove(struct platform_device *pdev)
Vinod Koul71bb8a12017-12-14 11:19:43 +05301451{
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001452 struct device *dev = &pdev->dev;
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001453 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1454 struct sdw_intel *sdw = cdns_to_intel(cdns);
1455 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001456
1457 if (!bus->prop.hw_disabled) {
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001458 intel_debugfs_exit(sdw);
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001459 sdw_cdns_enable_interrupt(cdns, false);
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001460 snd_soc_unregister_component(dev);
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001461 }
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001462 sdw_bus_master_delete(bus);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301463
1464 return 0;
1465}
1466
Rander Wangab2c9132020-07-16 23:09:46 +08001467int intel_master_process_wakeen_event(struct platform_device *pdev)
1468{
1469 struct device *dev = &pdev->dev;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301470 struct sdw_intel *sdw;
Rander Wangab2c9132020-07-16 23:09:46 +08001471 struct sdw_bus *bus;
1472 void __iomem *shim;
1473 u16 wake_sts;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301474
1475 sdw = platform_get_drvdata(pdev);
Rander Wangab2c9132020-07-16 23:09:46 +08001476 bus = &sdw->cdns.bus;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301477
Rander Wangab2c9132020-07-16 23:09:46 +08001478 if (bus->prop.hw_disabled) {
1479 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n", bus->link_id);
1480 return 0;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301481 }
Rander Wangab2c9132020-07-16 23:09:46 +08001482
1483 shim = sdw->link_res->shim;
1484 wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
1485
1486 if (!(wake_sts & BIT(sdw->instance)))
1487 return 0;
1488
1489 /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
1490 intel_shim_wake(sdw, false);
1491
1492 /*
1493 * resume the Master, which will generate a bus reset and result in
1494 * Slaves re-attaching and be re-enumerated. The SoundWire physical
1495 * device which generated the wake will trigger an interrupt, which
1496 * will in turn cause the corresponding Linux Slave device to be
1497 * resumed and the Slave codec driver to check the status.
1498 */
1499 pm_request_resume(dev);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301500
1501 return 0;
1502}
1503
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001504/*
1505 * PM calls
1506 */
1507
1508#ifdef CONFIG_PM
1509
1510static int intel_suspend(struct device *dev)
1511{
1512 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1513 struct sdw_intel *sdw = cdns_to_intel(cdns);
1514 struct sdw_bus *bus = &cdns->bus;
1515 int ret;
1516
1517 if (bus->prop.hw_disabled) {
1518 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n",
1519 bus->link_id);
1520 return 0;
1521 }
1522
Pierre-Louis Bossartb61b8b32020-08-17 23:29:13 +08001523 if (pm_runtime_suspended(dev)) {
1524 dev_dbg(dev, "%s: pm_runtime status: suspended\n", __func__);
1525
1526 return 0;
1527 }
1528
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001529 ret = sdw_cdns_enable_interrupt(cdns, false);
1530 if (ret < 0) {
1531 dev_err(dev, "cannot disable interrupts on suspend\n");
1532 return ret;
1533 }
1534
1535 ret = intel_link_power_down(sdw);
1536 if (ret) {
1537 dev_err(dev, "Link power down failed: %d", ret);
1538 return ret;
1539 }
1540
1541 intel_shim_wake(sdw, false);
1542
1543 return 0;
1544}
1545
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001546static int intel_suspend_runtime(struct device *dev)
1547{
1548 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1549 struct sdw_intel *sdw = cdns_to_intel(cdns);
1550 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001551 u32 clock_stop_quirks;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001552 int ret;
1553
1554 if (bus->prop.hw_disabled) {
1555 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n",
1556 bus->link_id);
1557 return 0;
1558 }
1559
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001560 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1561
1562 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
1563
1564 ret = sdw_cdns_enable_interrupt(cdns, false);
1565 if (ret < 0) {
1566 dev_err(dev, "cannot disable interrupts on suspend\n");
1567 return ret;
1568 }
1569
1570 ret = intel_link_power_down(sdw);
1571 if (ret) {
1572 dev_err(dev, "Link power down failed: %d", ret);
1573 return ret;
1574 }
1575
1576 intel_shim_wake(sdw, false);
1577
Rander Wang6626a612020-08-17 23:29:19 +08001578 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
1579 ret = sdw_cdns_clock_stop(cdns, true);
1580 if (ret < 0) {
1581 dev_err(dev, "cannot enable clock stop on suspend\n");
1582 return ret;
1583 }
1584
1585 ret = sdw_cdns_enable_interrupt(cdns, false);
1586 if (ret < 0) {
1587 dev_err(dev, "cannot disable interrupts on suspend\n");
1588 return ret;
1589 }
1590
1591 ret = intel_link_power_down(sdw);
1592 if (ret) {
1593 dev_err(dev, "Link power down failed: %d", ret);
1594 return ret;
1595 }
1596
1597 intel_shim_wake(sdw, true);
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001598 } else {
1599 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
1600 __func__, clock_stop_quirks);
1601 ret = -EINVAL;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001602 }
1603
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001604 return ret;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001605}
1606
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001607static int intel_resume(struct device *dev)
1608{
1609 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1610 struct sdw_intel *sdw = cdns_to_intel(cdns);
1611 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossarta2d9c162020-08-17 23:29:17 +08001612 int link_flags;
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001613 int ret;
1614
1615 if (bus->prop.hw_disabled) {
1616 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n",
1617 bus->link_id);
1618 return 0;
1619 }
1620
Pierre-Louis Bossartb61b8b32020-08-17 23:29:13 +08001621 if (pm_runtime_suspended(dev)) {
1622 dev_dbg(dev, "%s: pm_runtime status was suspended, forcing active\n", __func__);
1623
1624 /* follow required sequence from runtime_pm.rst */
1625 pm_runtime_disable(dev);
1626 pm_runtime_set_active(dev);
1627 pm_runtime_mark_last_busy(dev);
1628 pm_runtime_enable(dev);
Pierre-Louis Bossarta2d9c162020-08-17 23:29:17 +08001629
1630 link_flags = md_flags >> (bus->link_id * 8);
1631 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
1632 pm_runtime_idle(dev);
Pierre-Louis Bossartb61b8b32020-08-17 23:29:13 +08001633 }
1634
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001635 ret = intel_init(sdw);
1636 if (ret) {
1637 dev_err(dev, "%s failed: %d", __func__, ret);
1638 return ret;
1639 }
1640
Pierre-Louis Bossart99b6a302020-08-17 23:29:15 +08001641 /*
1642 * make sure all Slaves are tagged as UNATTACHED and provide
1643 * reason for reinitialization
1644 */
1645 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
1646
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001647 ret = sdw_cdns_enable_interrupt(cdns, true);
1648 if (ret < 0) {
1649 dev_err(dev, "cannot enable interrupts during resume\n");
1650 return ret;
1651 }
1652
1653 ret = sdw_cdns_exit_reset(cdns);
1654 if (ret < 0) {
1655 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1656 return ret;
1657 }
1658
Pierre-Louis Bossartcb1e6d52020-08-17 23:29:14 +08001659 /*
1660 * after system resume, the pm_runtime suspend() may kick in
1661 * during the enumeration, before any children device force the
1662 * master device to remain active. Using pm_runtime_get()
1663 * routines is not really possible, since it'd prevent the
1664 * master from suspending.
1665 * A reasonable compromise is to update the pm_runtime
1666 * counters and delay the pm_runtime suspend by several
1667 * seconds, by when all enumeration should be complete.
1668 */
1669 pm_runtime_mark_last_busy(dev);
1670
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001671 return ret;
1672}
1673
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001674static int intel_resume_runtime(struct device *dev)
1675{
1676 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1677 struct sdw_intel *sdw = cdns_to_intel(cdns);
1678 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001679 u32 clock_stop_quirks;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001680 int ret;
1681
1682 if (bus->prop.hw_disabled) {
1683 dev_dbg(dev, "SoundWire master %d is disabled, ignoring\n",
1684 bus->link_id);
1685 return 0;
1686 }
1687
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001688 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001689
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001690 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
1691 ret = intel_init(sdw);
1692 if (ret) {
1693 dev_err(dev, "%s failed: %d", __func__, ret);
1694 return ret;
1695 }
Pierre-Louis Bossart99b6a302020-08-17 23:29:15 +08001696
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001697 /*
1698 * make sure all Slaves are tagged as UNATTACHED and provide
1699 * reason for reinitialization
1700 */
1701 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001702
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001703 ret = sdw_cdns_enable_interrupt(cdns, true);
1704 if (ret < 0) {
1705 dev_err(dev, "cannot enable interrupts during resume\n");
1706 return ret;
1707 }
1708
1709 ret = sdw_cdns_exit_reset(cdns);
1710 if (ret < 0) {
1711 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1712 return ret;
1713 }
Rander Wang6626a612020-08-17 23:29:19 +08001714 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
1715 ret = intel_init(sdw);
1716 if (ret) {
1717 dev_err(dev, "%s failed: %d", __func__, ret);
1718 return ret;
1719 }
1720
1721 /*
1722 * make sure all Slaves are tagged as UNATTACHED and
1723 * provide reason for reinitialization
1724 */
1725 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
1726
1727 ret = sdw_cdns_enable_interrupt(cdns, true);
1728 if (ret < 0) {
1729 dev_err(dev, "cannot enable interrupts during resume\n");
1730 return ret;
1731 }
1732
1733 ret = sdw_cdns_clock_restart(cdns, true);
1734 if (ret < 0) {
1735 dev_err(dev, "unable to restart clock during resume\n");
1736 return ret;
1737 }
Pierre-Louis Bossarta320f412020-08-17 23:29:18 +08001738 } else {
1739 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
1740 __func__, clock_stop_quirks);
1741 ret = -EINVAL;
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001742 }
1743
1744 return ret;
1745}
1746
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001747#endif
1748
1749static const struct dev_pm_ops intel_pm = {
1750 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
Pierre-Louis Bossartebf878e2020-08-17 23:29:12 +08001751 SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001752};
1753
Vinod Koul71bb8a12017-12-14 11:19:43 +05301754static struct platform_driver sdw_intel_drv = {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001755 .probe = intel_master_probe,
1756 .remove = intel_master_remove,
Vinod Koul71bb8a12017-12-14 11:19:43 +05301757 .driver = {
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001758 .name = "intel-sdw",
Pierre-Louis Bossart9b3b4b32020-07-22 04:37:11 +08001759 .pm = &intel_pm,
1760 }
Vinod Koul71bb8a12017-12-14 11:19:43 +05301761};
1762
1763module_platform_driver(sdw_intel_drv);
1764
1765MODULE_LICENSE("Dual BSD/GPL");
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001766MODULE_ALIAS("platform:intel-sdw");
Vinod Koul71bb8a12017-12-14 11:19:43 +05301767MODULE_DESCRIPTION("Intel Soundwire Master Driver");