blob: 9b473709829999e2d200279e11be6933229de8f9 [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>
16#include <sound/soc.h>
Vinod Koul71bb8a12017-12-14 11:19:43 +053017#include <linux/soundwire/sdw_registers.h>
18#include <linux/soundwire/sdw.h>
19#include <linux/soundwire/sdw_intel.h>
20#include "cadence_master.h"
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -050021#include "bus.h"
Vinod Koul71bb8a12017-12-14 11:19:43 +053022#include "intel.h"
23
24/* Intel SHIM Registers Definition */
25#define SDW_SHIM_LCAP 0x0
26#define SDW_SHIM_LCTL 0x4
27#define SDW_SHIM_IPPTR 0x8
28#define SDW_SHIM_SYNC 0xC
29
Pierre-Louis Bossart7cc6e312019-05-01 10:57:39 -050030#define SDW_SHIM_CTLSCAP(x) (0x010 + 0x60 * (x))
31#define SDW_SHIM_CTLS0CM(x) (0x012 + 0x60 * (x))
32#define SDW_SHIM_CTLS1CM(x) (0x014 + 0x60 * (x))
33#define SDW_SHIM_CTLS2CM(x) (0x016 + 0x60 * (x))
34#define SDW_SHIM_CTLS3CM(x) (0x018 + 0x60 * (x))
35#define SDW_SHIM_PCMSCAP(x) (0x020 + 0x60 * (x))
Vinod Koul71bb8a12017-12-14 11:19:43 +053036
Pierre-Louis Bossart7cc6e312019-05-01 10:57:39 -050037#define SDW_SHIM_PCMSYCHM(x, y) (0x022 + (0x60 * (x)) + (0x2 * (y)))
38#define SDW_SHIM_PCMSYCHC(x, y) (0x042 + (0x60 * (x)) + (0x2 * (y)))
39#define SDW_SHIM_PDMSCAP(x) (0x062 + 0x60 * (x))
40#define SDW_SHIM_IOCTL(x) (0x06C + 0x60 * (x))
41#define SDW_SHIM_CTMCTL(x) (0x06E + 0x60 * (x))
Vinod Koul71bb8a12017-12-14 11:19:43 +053042
43#define SDW_SHIM_WAKEEN 0x190
44#define SDW_SHIM_WAKESTS 0x192
45
46#define SDW_SHIM_LCTL_SPA BIT(0)
47#define SDW_SHIM_LCTL_CPA BIT(8)
48
49#define SDW_SHIM_SYNC_SYNCPRD_VAL 0x176F
50#define SDW_SHIM_SYNC_SYNCPRD GENMASK(14, 0)
51#define SDW_SHIM_SYNC_SYNCCPU BIT(15)
52#define SDW_SHIM_SYNC_CMDSYNC_MASK GENMASK(19, 16)
53#define SDW_SHIM_SYNC_CMDSYNC BIT(16)
54#define SDW_SHIM_SYNC_SYNCGO BIT(24)
55
56#define SDW_SHIM_PCMSCAP_ISS GENMASK(3, 0)
57#define SDW_SHIM_PCMSCAP_OSS GENMASK(7, 4)
58#define SDW_SHIM_PCMSCAP_BSS GENMASK(12, 8)
59
60#define SDW_SHIM_PCMSYCM_LCHN GENMASK(3, 0)
61#define SDW_SHIM_PCMSYCM_HCHN GENMASK(7, 4)
62#define SDW_SHIM_PCMSYCM_STREAM GENMASK(13, 8)
63#define SDW_SHIM_PCMSYCM_DIR BIT(15)
64
65#define SDW_SHIM_PDMSCAP_ISS GENMASK(3, 0)
66#define SDW_SHIM_PDMSCAP_OSS GENMASK(7, 4)
67#define SDW_SHIM_PDMSCAP_BSS GENMASK(12, 8)
68#define SDW_SHIM_PDMSCAP_CPSS GENMASK(15, 13)
69
70#define SDW_SHIM_IOCTL_MIF BIT(0)
71#define SDW_SHIM_IOCTL_CO BIT(1)
72#define SDW_SHIM_IOCTL_COE BIT(2)
73#define SDW_SHIM_IOCTL_DO BIT(3)
74#define SDW_SHIM_IOCTL_DOE BIT(4)
75#define SDW_SHIM_IOCTL_BKE BIT(5)
76#define SDW_SHIM_IOCTL_WPDD BIT(6)
77#define SDW_SHIM_IOCTL_CIBD BIT(8)
78#define SDW_SHIM_IOCTL_DIBD BIT(9)
79
80#define SDW_SHIM_CTMCTL_DACTQE BIT(0)
81#define SDW_SHIM_CTMCTL_DODS BIT(1)
82#define SDW_SHIM_CTMCTL_DOAIS GENMASK(4, 3)
83
84#define SDW_SHIM_WAKEEN_ENABLE BIT(0)
85#define SDW_SHIM_WAKESTS_STATUS BIT(0)
86
87/* Intel ALH Register definitions */
Pierre-Louis Bossart7cc6e312019-05-01 10:57:39 -050088#define SDW_ALH_STRMZCFG(x) (0x000 + (0x4 * (x)))
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -050089#define SDW_ALH_NUM_STREAMS 64
Vinod Koul71bb8a12017-12-14 11:19:43 +053090
91#define SDW_ALH_STRMZCFG_DMAT_VAL 0x3
92#define SDW_ALH_STRMZCFG_DMAT GENMASK(7, 0)
93#define SDW_ALH_STRMZCFG_CHN GENMASK(19, 16)
94
Vinod Koulc46302e2018-04-26 18:39:05 +053095enum intel_pdi_type {
96 INTEL_PDI_IN = 0,
97 INTEL_PDI_OUT = 1,
98 INTEL_PDI_BD = 2,
99};
100
Vinod Koul71bb8a12017-12-14 11:19:43 +0530101#define cdns_to_intel(_cdns) container_of(_cdns, struct sdw_intel, cdns)
102
103/*
104 * Read, write helpers for HW registers
105 */
106static inline int intel_readl(void __iomem *base, int offset)
107{
108 return readl(base + offset);
109}
110
111static inline void intel_writel(void __iomem *base, int offset, int value)
112{
113 writel(value, base + offset);
114}
115
116static inline u16 intel_readw(void __iomem *base, int offset)
117{
118 return readw(base + offset);
119}
120
121static inline void intel_writew(void __iomem *base, int offset, u16 value)
122{
123 writew(value, base + offset);
124}
125
126static int intel_clear_bit(void __iomem *base, int offset, u32 value, u32 mask)
127{
128 int timeout = 10;
129 u32 reg_read;
130
131 writel(value, base + offset);
132 do {
133 reg_read = readl(base + offset);
134 if (!(reg_read & mask))
135 return 0;
136
137 timeout--;
138 udelay(50);
139 } while (timeout != 0);
140
141 return -EAGAIN;
142}
143
144static int intel_set_bit(void __iomem *base, int offset, u32 value, u32 mask)
145{
146 int timeout = 10;
147 u32 reg_read;
148
149 writel(value, base + offset);
150 do {
151 reg_read = readl(base + offset);
152 if (reg_read & mask)
153 return 0;
154
155 timeout--;
156 udelay(50);
157 } while (timeout != 0);
158
159 return -EAGAIN;
160}
161
162/*
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -0500163 * debugfs
164 */
165#ifdef CONFIG_DEBUG_FS
166
167#define RD_BUF (2 * PAGE_SIZE)
168
169static ssize_t intel_sprintf(void __iomem *mem, bool l,
170 char *buf, size_t pos, unsigned int reg)
171{
172 int value;
173
174 if (l)
175 value = intel_readl(mem, reg);
176 else
177 value = intel_readw(mem, reg);
178
179 return scnprintf(buf + pos, RD_BUF - pos, "%4x\t%4x\n", reg, value);
180}
181
182static int intel_reg_show(struct seq_file *s_file, void *data)
183{
184 struct sdw_intel *sdw = s_file->private;
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600185 void __iomem *s = sdw->link_res->shim;
186 void __iomem *a = sdw->link_res->alh;
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -0500187 char *buf;
188 ssize_t ret;
189 int i, j;
190 unsigned int links, reg;
191
192 buf = kzalloc(RD_BUF, GFP_KERNEL);
193 if (!buf)
194 return -ENOMEM;
195
196 links = intel_readl(s, SDW_SHIM_LCAP) & GENMASK(2, 0);
197
198 ret = scnprintf(buf, RD_BUF, "Register Value\n");
199 ret += scnprintf(buf + ret, RD_BUF - ret, "\nShim\n");
200
201 for (i = 0; i < links; i++) {
202 reg = SDW_SHIM_LCAP + i * 4;
203 ret += intel_sprintf(s, true, buf, ret, reg);
204 }
205
206 for (i = 0; i < links; i++) {
207 ret += scnprintf(buf + ret, RD_BUF - ret, "\nLink%d\n", i);
208 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLSCAP(i));
209 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS0CM(i));
210 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS1CM(i));
211 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS2CM(i));
212 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS3CM(i));
213 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PCMSCAP(i));
214
215 ret += scnprintf(buf + ret, RD_BUF - ret, "\n PCMSyCH registers\n");
216
217 /*
218 * the value 10 is the number of PDIs. We will need a
219 * cleanup to remove hard-coded Intel configurations
220 * from cadence_master.c
221 */
222 for (j = 0; j < 10; j++) {
223 ret += intel_sprintf(s, false, buf, ret,
224 SDW_SHIM_PCMSYCHM(i, j));
225 ret += intel_sprintf(s, false, buf, ret,
226 SDW_SHIM_PCMSYCHC(i, j));
227 }
228 ret += scnprintf(buf + ret, RD_BUF - ret, "\n PDMSCAP, IOCTL, CTMCTL\n");
229
230 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PDMSCAP(i));
231 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_IOCTL(i));
232 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTMCTL(i));
233 }
234
235 ret += scnprintf(buf + ret, RD_BUF - ret, "\nWake registers\n");
236 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKEEN);
237 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKESTS);
238
239 ret += scnprintf(buf + ret, RD_BUF - ret, "\nALH STRMzCFG\n");
240 for (i = 0; i < SDW_ALH_NUM_STREAMS; i++)
241 ret += intel_sprintf(a, true, buf, ret, SDW_ALH_STRMZCFG(i));
242
243 seq_printf(s_file, "%s", buf);
244 kfree(buf);
245
246 return 0;
247}
248DEFINE_SHOW_ATTRIBUTE(intel_reg);
249
250static void intel_debugfs_init(struct sdw_intel *sdw)
251{
252 struct dentry *root = sdw->cdns.bus.debugfs;
253
254 if (!root)
255 return;
256
257 sdw->debugfs = debugfs_create_dir("intel-sdw", root);
258
259 debugfs_create_file("intel-registers", 0400, sdw->debugfs, sdw,
260 &intel_reg_fops);
261
262 sdw_cdns_debugfs_init(&sdw->cdns, sdw->debugfs);
263}
264
265static void intel_debugfs_exit(struct sdw_intel *sdw)
266{
267 debugfs_remove_recursive(sdw->debugfs);
268}
269#else
270static void intel_debugfs_init(struct sdw_intel *sdw) {}
271static void intel_debugfs_exit(struct sdw_intel *sdw) {}
272#endif /* CONFIG_DEBUG_FS */
273
274/*
Vinod Koul71bb8a12017-12-14 11:19:43 +0530275 * shim ops
276 */
277
278static int intel_link_power_up(struct sdw_intel *sdw)
279{
280 unsigned int link_id = sdw->instance;
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600281 void __iomem *shim = sdw->link_res->shim;
Vinod Koul71bb8a12017-12-14 11:19:43 +0530282 int spa_mask, cpa_mask;
283 int link_control, ret;
284
285 /* Link power up sequence */
286 link_control = intel_readl(shim, SDW_SHIM_LCTL);
287 spa_mask = (SDW_SHIM_LCTL_SPA << link_id);
288 cpa_mask = (SDW_SHIM_LCTL_CPA << link_id);
289 link_control |= spa_mask;
290
291 ret = intel_set_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
292 if (ret < 0)
293 return ret;
294
295 sdw->cdns.link_up = true;
296 return 0;
297}
298
299static int intel_shim_init(struct sdw_intel *sdw)
300{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600301 void __iomem *shim = sdw->link_res->shim;
Vinod Koul71bb8a12017-12-14 11:19:43 +0530302 unsigned int link_id = sdw->instance;
303 int sync_reg, ret;
304 u16 ioctl = 0, act = 0;
305
306 /* Initialize Shim */
307 ioctl |= SDW_SHIM_IOCTL_BKE;
308 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
309
310 ioctl |= SDW_SHIM_IOCTL_WPDD;
311 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
312
313 ioctl |= SDW_SHIM_IOCTL_DO;
314 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
315
316 ioctl |= SDW_SHIM_IOCTL_DOE;
317 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
318
319 /* Switch to MIP from Glue logic */
320 ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
321
322 ioctl &= ~(SDW_SHIM_IOCTL_DOE);
323 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
324
325 ioctl &= ~(SDW_SHIM_IOCTL_DO);
326 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
327
328 ioctl |= (SDW_SHIM_IOCTL_MIF);
329 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
330
331 ioctl &= ~(SDW_SHIM_IOCTL_BKE);
332 ioctl &= ~(SDW_SHIM_IOCTL_COE);
333
334 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
335
336 act |= 0x1 << SDW_REG_SHIFT(SDW_SHIM_CTMCTL_DOAIS);
337 act |= SDW_SHIM_CTMCTL_DACTQE;
338 act |= SDW_SHIM_CTMCTL_DODS;
339 intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act);
340
341 /* Now set SyncPRD period */
342 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
343 sync_reg |= (SDW_SHIM_SYNC_SYNCPRD_VAL <<
344 SDW_REG_SHIFT(SDW_SHIM_SYNC_SYNCPRD));
345
346 /* Set SyncCPU bit */
347 sync_reg |= SDW_SHIM_SYNC_SYNCCPU;
348 ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500349 SDW_SHIM_SYNC_SYNCCPU);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530350 if (ret < 0)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500351 dev_err(sdw->cdns.dev, "Failed to set sync period: %d\n", ret);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530352
353 return ret;
354}
355
Vinod Koul37a2d222018-04-26 18:38:58 +0530356/*
357 * PDI routines
358 */
359static void intel_pdi_init(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500360 struct sdw_cdns_stream_config *config)
Vinod Koul37a2d222018-04-26 18:38:58 +0530361{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600362 void __iomem *shim = sdw->link_res->shim;
Vinod Koul37a2d222018-04-26 18:38:58 +0530363 unsigned int link_id = sdw->instance;
364 int pcm_cap, pdm_cap;
365
366 /* PCM Stream Capability */
367 pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id));
368
369 config->pcm_bd = (pcm_cap & SDW_SHIM_PCMSCAP_BSS) >>
370 SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_BSS);
371 config->pcm_in = (pcm_cap & SDW_SHIM_PCMSCAP_ISS) >>
372 SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_ISS);
373 config->pcm_out = (pcm_cap & SDW_SHIM_PCMSCAP_OSS) >>
374 SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_OSS);
375
Pierre-Louis Bossart121f4362019-05-22 14:47:29 -0500376 dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
377 config->pcm_bd, config->pcm_in, config->pcm_out);
378
Vinod Koul37a2d222018-04-26 18:38:58 +0530379 /* PDM Stream Capability */
380 pdm_cap = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
381
382 config->pdm_bd = (pdm_cap & SDW_SHIM_PDMSCAP_BSS) >>
383 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_BSS);
384 config->pdm_in = (pdm_cap & SDW_SHIM_PDMSCAP_ISS) >>
385 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_ISS);
386 config->pdm_out = (pdm_cap & SDW_SHIM_PDMSCAP_OSS) >>
387 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_OSS);
Pierre-Louis Bossart121f4362019-05-22 14:47:29 -0500388
389 dev_dbg(sdw->cdns.dev, "PDM cap bd:%d in:%d out:%d\n",
390 config->pdm_bd, config->pdm_in, config->pdm_out);
Vinod Koul37a2d222018-04-26 18:38:58 +0530391}
392
393static int
394intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num, bool pcm)
395{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600396 void __iomem *shim = sdw->link_res->shim;
Vinod Koul37a2d222018-04-26 18:38:58 +0530397 unsigned int link_id = sdw->instance;
398 int count;
399
400 if (pcm) {
401 count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num));
Pierre-Louis Bossart18046332019-08-05 19:55:07 -0500402
403 /*
404 * WORKAROUND: on all existing Intel controllers, pdi
405 * number 2 reports channel count as 1 even though it
406 * supports 8 channels. Performing hardcoding for pdi
407 * number 2.
408 */
409 if (pdi_num == 2)
410 count = 7;
411
Vinod Koul37a2d222018-04-26 18:38:58 +0530412 } else {
413 count = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
414 count = ((count & SDW_SHIM_PDMSCAP_CPSS) >>
415 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_CPSS));
416 }
417
418 /* zero based values for channel count in register */
419 count++;
420
421 return count;
422}
423
424static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500425 struct sdw_cdns_pdi *pdi,
426 unsigned int num_pdi,
427 unsigned int *num_ch, bool pcm)
Vinod Koul37a2d222018-04-26 18:38:58 +0530428{
429 int i, ch_count = 0;
430
431 for (i = 0; i < num_pdi; i++) {
432 pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num, pcm);
433 ch_count += pdi->ch_count;
434 pdi++;
435 }
436
437 *num_ch = ch_count;
438 return 0;
439}
440
441static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500442 struct sdw_cdns_streams *stream, bool pcm)
Vinod Koul37a2d222018-04-26 18:38:58 +0530443{
444 intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500445 &stream->num_ch_bd, pcm);
Vinod Koul37a2d222018-04-26 18:38:58 +0530446
447 intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500448 &stream->num_ch_in, pcm);
Vinod Koul37a2d222018-04-26 18:38:58 +0530449
450 intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500451 &stream->num_ch_out, pcm);
Vinod Koul37a2d222018-04-26 18:38:58 +0530452
453 return 0;
454}
455
456static int intel_pdi_ch_update(struct sdw_intel *sdw)
457{
458 /* First update PCM streams followed by PDM streams */
459 intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm, true);
460 intel_pdi_stream_ch_update(sdw, &sdw->cdns.pdm, false);
461
462 return 0;
463}
464
465static void
466intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
467{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600468 void __iomem *shim = sdw->link_res->shim;
Vinod Koul37a2d222018-04-26 18:38:58 +0530469 unsigned int link_id = sdw->instance;
470 int pdi_conf = 0;
471
Pierre-Louis Bossartc134f912019-10-22 18:29:48 -0500472 /* the Bulk and PCM streams are not contiguous */
473 pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
474 if (pdi->num >= 2)
475 pdi->intel_alh_id += 2;
Vinod Koul37a2d222018-04-26 18:38:58 +0530476
477 /*
478 * Program stream parameters to stream SHIM register
479 * This is applicable for PCM stream only.
480 */
481 if (pdi->type != SDW_STREAM_PCM)
482 return;
483
484 if (pdi->dir == SDW_DATA_DIR_RX)
485 pdi_conf |= SDW_SHIM_PCMSYCM_DIR;
486 else
487 pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR);
488
489 pdi_conf |= (pdi->intel_alh_id <<
490 SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_STREAM));
491 pdi_conf |= (pdi->l_ch_num << SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_LCHN));
492 pdi_conf |= (pdi->h_ch_num << SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_HCHN));
493
494 intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
495}
496
497static void
498intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
499{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600500 void __iomem *alh = sdw->link_res->alh;
Vinod Koul37a2d222018-04-26 18:38:58 +0530501 unsigned int link_id = sdw->instance;
502 unsigned int conf;
503
Pierre-Louis Bossartc134f912019-10-22 18:29:48 -0500504 /* the Bulk and PCM streams are not contiguous */
505 pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
506 if (pdi->num >= 2)
507 pdi->intel_alh_id += 2;
Vinod Koul37a2d222018-04-26 18:38:58 +0530508
509 /* Program Stream config ALH register */
510 conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
511
512 conf |= (SDW_ALH_STRMZCFG_DMAT_VAL <<
513 SDW_REG_SHIFT(SDW_ALH_STRMZCFG_DMAT));
514
515 conf |= ((pdi->ch_count - 1) <<
516 SDW_REG_SHIFT(SDW_ALH_STRMZCFG_CHN));
517
518 intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
519}
520
Rander Wang4b206d32019-12-11 19:45:02 -0600521static int intel_params_stream(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500522 struct snd_pcm_substream *substream,
523 struct snd_soc_dai *dai,
Rander Wang4b206d32019-12-11 19:45:02 -0600524 struct snd_pcm_hw_params *hw_params,
525 int link_id, int alh_stream_id)
Vinod Koulc46302e2018-04-26 18:39:05 +0530526{
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600527 struct sdw_intel_link_res *res = sdw->link_res;
Rander Wang4b206d32019-12-11 19:45:02 -0600528 struct sdw_intel_stream_params_data params_data;
Pierre-Louis Bossart05c8afe42019-08-05 19:55:06 -0500529
Rander Wang4b206d32019-12-11 19:45:02 -0600530 params_data.substream = substream;
531 params_data.dai = dai;
532 params_data.hw_params = hw_params;
533 params_data.link_id = link_id;
534 params_data.alh_stream_id = alh_stream_id;
Vinod Koulc46302e2018-04-26 18:39:05 +0530535
Rander Wang4b206d32019-12-11 19:45:02 -0600536 if (res->ops && res->ops->params_stream && res->dev)
537 return res->ops->params_stream(res->dev,
538 &params_data);
Vinod Koulc46302e2018-04-26 18:39:05 +0530539 return -EIO;
540}
541
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -0600542static int intel_free_stream(struct sdw_intel *sdw,
543 struct snd_pcm_substream *substream,
544 struct snd_soc_dai *dai,
545 int link_id)
546{
547 struct sdw_intel_link_res *res = sdw->link_res;
548 struct sdw_intel_stream_free_data free_data;
549
550 free_data.substream = substream;
551 free_data.dai = dai;
552 free_data.link_id = link_id;
553
554 if (res->ops && res->ops->free_stream && res->dev)
555 return res->ops->free_stream(res->dev,
556 &free_data);
557
558 return 0;
559}
560
Vinod Koulc46302e2018-04-26 18:39:05 +0530561/*
Shreyas NC30246e22018-07-27 14:44:17 +0530562 * bank switch routines
563 */
564
565static int intel_pre_bank_switch(struct sdw_bus *bus)
566{
567 struct sdw_cdns *cdns = bus_to_cdns(bus);
568 struct sdw_intel *sdw = cdns_to_intel(cdns);
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600569 void __iomem *shim = sdw->link_res->shim;
Shreyas NC30246e22018-07-27 14:44:17 +0530570 int sync_reg;
571
572 /* Write to register only for multi-link */
573 if (!bus->multi_link)
574 return 0;
575
576 /* Read SYNC register */
577 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
578 sync_reg |= SDW_SHIM_SYNC_CMDSYNC << sdw->instance;
579 intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
580
581 return 0;
582}
583
584static int intel_post_bank_switch(struct sdw_bus *bus)
585{
586 struct sdw_cdns *cdns = bus_to_cdns(bus);
587 struct sdw_intel *sdw = cdns_to_intel(cdns);
Pierre-Louis Bossart25234862020-02-14 19:47:36 -0600588 void __iomem *shim = sdw->link_res->shim;
Shreyas NC30246e22018-07-27 14:44:17 +0530589 int sync_reg, ret;
590
591 /* Write to register only for multi-link */
592 if (!bus->multi_link)
593 return 0;
594
595 /* Read SYNC register */
596 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
597
598 /*
599 * post_bank_switch() ops is called from the bus in loop for
600 * all the Masters in the steam with the expectation that
601 * we trigger the bankswitch for the only first Master in the list
602 * and do nothing for the other Masters
603 *
604 * So, set the SYNCGO bit only if CMDSYNC bit is set for any Master.
605 */
606 if (!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK))
607 return 0;
608
609 /*
610 * Set SyncGO bit to synchronously trigger a bank switch for
611 * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all
612 * the Masters.
613 */
614 sync_reg |= SDW_SHIM_SYNC_SYNCGO;
615
616 ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500617 SDW_SHIM_SYNC_SYNCGO);
Shreyas NC30246e22018-07-27 14:44:17 +0530618 if (ret < 0)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500619 dev_err(sdw->cdns.dev, "Post bank switch failed: %d\n", ret);
Shreyas NC30246e22018-07-27 14:44:17 +0530620
621 return ret;
622}
623
624/*
Vinod Koulc46302e2018-04-26 18:39:05 +0530625 * DAI routines
626 */
627
Rander Wang5e7484d2020-02-14 19:47:39 -0600628static int sdw_stream_setup(struct snd_pcm_substream *substream,
629 struct snd_soc_dai *dai)
630{
631 struct snd_soc_pcm_runtime *rtd = substream->private_data;
632 struct sdw_stream_runtime *sdw_stream = NULL;
633 char *name;
634 int i, ret;
635
636 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
637 name = kasprintf(GFP_KERNEL, "%s-Playback", dai->name);
638 else
639 name = kasprintf(GFP_KERNEL, "%s-Capture", dai->name);
640
641 if (!name)
642 return -ENOMEM;
643
644 sdw_stream = sdw_alloc_stream(name);
645 if (!sdw_stream) {
646 dev_err(dai->dev, "alloc stream failed for DAI %s", dai->name);
647 ret = -ENOMEM;
648 goto error;
649 }
650
651 /* Set stream pointer on CPU DAI */
652 ret = snd_soc_dai_set_sdw_stream(dai, sdw_stream, substream->stream);
653 if (ret < 0) {
654 dev_err(dai->dev, "failed to set stream pointer on cpu dai %s",
655 dai->name);
656 goto release_stream;
657 }
658
659 /* Set stream pointer on all CODEC DAIs */
660 for (i = 0; i < rtd->num_codecs; i++) {
Kuninori Morimoto611905d2020-04-20 16:01:01 +0900661 ret = snd_soc_dai_set_sdw_stream(asoc_rtd_to_codec(rtd, i), sdw_stream,
Rander Wang5e7484d2020-02-14 19:47:39 -0600662 substream->stream);
663 if (ret < 0) {
664 dev_err(dai->dev, "failed to set stream pointer on codec dai %s",
Kuninori Morimoto611905d2020-04-20 16:01:01 +0900665 asoc_rtd_to_codec(rtd, i)->name);
Rander Wang5e7484d2020-02-14 19:47:39 -0600666 goto release_stream;
667 }
668 }
669
670 return 0;
671
672release_stream:
673 sdw_release_stream(sdw_stream);
674error:
675 kfree(name);
676 return ret;
677}
678
679static int intel_startup(struct snd_pcm_substream *substream,
680 struct snd_soc_dai *dai)
681{
682 /*
683 * TODO: add pm_runtime support here, the startup callback
684 * will make sure the IP is 'active'
685 */
686
687 return sdw_stream_setup(substream, dai);
688}
689
Vinod Koulc46302e2018-04-26 18:39:05 +0530690static int intel_hw_params(struct snd_pcm_substream *substream,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500691 struct snd_pcm_hw_params *params,
692 struct snd_soc_dai *dai)
Vinod Koulc46302e2018-04-26 18:39:05 +0530693{
694 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
695 struct sdw_intel *sdw = cdns_to_intel(cdns);
696 struct sdw_cdns_dma_data *dma;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500697 struct sdw_cdns_pdi *pdi;
Vinod Koulc46302e2018-04-26 18:39:05 +0530698 struct sdw_stream_config sconfig;
699 struct sdw_port_config *pconfig;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500700 int ch, dir;
701 int ret;
Vinod Koulc46302e2018-04-26 18:39:05 +0530702 bool pcm = true;
703
704 dma = snd_soc_dai_get_dma_data(dai, substream);
705 if (!dma)
706 return -EIO;
707
708 ch = params_channels(params);
709 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
710 dir = SDW_DATA_DIR_RX;
711 else
712 dir = SDW_DATA_DIR_TX;
713
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500714 if (dma->stream_type == SDW_STREAM_PDM)
Vinod Koulc46302e2018-04-26 18:39:05 +0530715 pcm = false;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500716
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500717 if (pcm)
Bard Liao1b53385e2019-09-16 14:23:48 -0500718 pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500719 else
Bard Liao1b53385e2019-09-16 14:23:48 -0500720 pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pdm, ch, dir, dai->id);
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500721
722 if (!pdi) {
723 ret = -EINVAL;
724 goto error;
Vinod Koulc46302e2018-04-26 18:39:05 +0530725 }
726
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500727 /* do run-time configurations for SHIM, ALH and PDI/PORT */
728 intel_pdi_shim_configure(sdw, pdi);
729 intel_pdi_alh_configure(sdw, pdi);
730 sdw_cdns_config_stream(cdns, ch, dir, pdi);
Vinod Koulc46302e2018-04-26 18:39:05 +0530731
Vinod Koulc46302e2018-04-26 18:39:05 +0530732
733 /* Inform DSP about PDI stream number */
Rander Wang4b206d32019-12-11 19:45:02 -0600734 ret = intel_params_stream(sdw, substream, dai, params,
735 sdw->instance,
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500736 pdi->intel_alh_id);
737 if (ret)
738 goto error;
Vinod Koulc46302e2018-04-26 18:39:05 +0530739
740 sconfig.direction = dir;
741 sconfig.ch_count = ch;
742 sconfig.frame_rate = params_rate(params);
743 sconfig.type = dma->stream_type;
744
745 if (dma->stream_type == SDW_STREAM_PDM) {
746 sconfig.frame_rate *= 50;
747 sconfig.bps = 1;
748 } else {
749 sconfig.bps = snd_pcm_format_width(params_format(params));
750 }
751
752 /* Port configuration */
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500753 pconfig = kcalloc(1, sizeof(*pconfig), GFP_KERNEL);
Vinod Koulc46302e2018-04-26 18:39:05 +0530754 if (!pconfig) {
755 ret = -ENOMEM;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500756 goto error;
Vinod Koulc46302e2018-04-26 18:39:05 +0530757 }
758
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500759 pconfig->num = pdi->num;
760 pconfig->ch_mask = (1 << ch) - 1;
Vinod Koulc46302e2018-04-26 18:39:05 +0530761
762 ret = sdw_stream_add_master(&cdns->bus, &sconfig,
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500763 pconfig, 1, dma->stream);
764 if (ret)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500765 dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
Vinod Koulc46302e2018-04-26 18:39:05 +0530766
767 kfree(pconfig);
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500768error:
Vinod Koulc46302e2018-04-26 18:39:05 +0530769 return ret;
770}
771
Rander Wang27b198f2020-02-14 19:47:37 -0600772static int intel_prepare(struct snd_pcm_substream *substream,
773 struct snd_soc_dai *dai)
774{
775 struct sdw_cdns_dma_data *dma;
776
777 dma = snd_soc_dai_get_dma_data(dai, substream);
778 if (!dma) {
779 dev_err(dai->dev, "failed to get dma data in %s",
780 __func__);
781 return -EIO;
782 }
783
784 return sdw_prepare_stream(dma->stream);
785}
786
Rander Wang973a8422020-02-14 19:47:38 -0600787static int intel_trigger(struct snd_pcm_substream *substream, int cmd,
788 struct snd_soc_dai *dai)
789{
790 struct sdw_cdns_dma_data *dma;
791 int ret;
792
793 dma = snd_soc_dai_get_dma_data(dai, substream);
794 if (!dma) {
795 dev_err(dai->dev, "failed to get dma data in %s", __func__);
796 return -EIO;
797 }
798
799 switch (cmd) {
800 case SNDRV_PCM_TRIGGER_START:
801 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
802 case SNDRV_PCM_TRIGGER_RESUME:
803 ret = sdw_enable_stream(dma->stream);
804 break;
805
806 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
807 case SNDRV_PCM_TRIGGER_SUSPEND:
808 case SNDRV_PCM_TRIGGER_STOP:
809 ret = sdw_disable_stream(dma->stream);
810 break;
811
812 default:
813 ret = -EINVAL;
814 break;
815 }
816
817 if (ret)
818 dev_err(dai->dev,
819 "%s trigger %d failed: %d",
820 __func__, cmd, ret);
821 return ret;
822}
823
Vinod Koulc46302e2018-04-26 18:39:05 +0530824static int
825intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
826{
827 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -0600828 struct sdw_intel *sdw = cdns_to_intel(cdns);
Vinod Koulc46302e2018-04-26 18:39:05 +0530829 struct sdw_cdns_dma_data *dma;
830 int ret;
831
832 dma = snd_soc_dai_get_dma_data(dai, substream);
833 if (!dma)
834 return -EIO;
835
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -0600836 ret = sdw_deprepare_stream(dma->stream);
837 if (ret) {
838 dev_err(dai->dev, "sdw_deprepare_stream: failed %d", ret);
839 return ret;
840 }
841
Vinod Koulc46302e2018-04-26 18:39:05 +0530842 ret = sdw_stream_remove_master(&cdns->bus, dma->stream);
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -0600843 if (ret < 0) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500844 dev_err(dai->dev, "remove master from stream %s failed: %d\n",
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500845 dma->stream->name, ret);
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -0600846 return ret;
847 }
Vinod Koulc46302e2018-04-26 18:39:05 +0530848
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -0600849 ret = intel_free_stream(sdw, substream, dai, sdw->instance);
850 if (ret < 0) {
851 dev_err(dai->dev, "intel_free_stream: failed %d", ret);
852 return ret;
853 }
854
855 kfree(dma->stream->name);
856 sdw_release_stream(dma->stream);
857
858 return 0;
Vinod Koulc46302e2018-04-26 18:39:05 +0530859}
860
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -0500861static void intel_shutdown(struct snd_pcm_substream *substream,
862 struct snd_soc_dai *dai)
863{
864 struct sdw_cdns_dma_data *dma;
865
866 dma = snd_soc_dai_get_dma_data(dai, substream);
867 if (!dma)
868 return;
869
870 snd_soc_dai_set_dma_data(dai, substream, NULL);
871 kfree(dma);
872}
873
Vinod Koulc46302e2018-04-26 18:39:05 +0530874static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500875 void *stream, int direction)
Vinod Koulc46302e2018-04-26 18:39:05 +0530876{
877 return cdns_set_sdw_stream(dai, stream, true, direction);
878}
879
880static int intel_pdm_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500881 void *stream, int direction)
Vinod Koulc46302e2018-04-26 18:39:05 +0530882{
883 return cdns_set_sdw_stream(dai, stream, false, direction);
884}
885
Julia Lawallb1635592018-10-27 15:34:42 +0200886static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
Rander Wang5e7484d2020-02-14 19:47:39 -0600887 .startup = intel_startup,
Vinod Koulc46302e2018-04-26 18:39:05 +0530888 .hw_params = intel_hw_params,
Rander Wang27b198f2020-02-14 19:47:37 -0600889 .prepare = intel_prepare,
Rander Wang973a8422020-02-14 19:47:38 -0600890 .trigger = intel_trigger,
Vinod Koulc46302e2018-04-26 18:39:05 +0530891 .hw_free = intel_hw_free,
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -0500892 .shutdown = intel_shutdown,
Vinod Koulc46302e2018-04-26 18:39:05 +0530893 .set_sdw_stream = intel_pcm_set_sdw_stream,
894};
895
Julia Lawallb1635592018-10-27 15:34:42 +0200896static const struct snd_soc_dai_ops intel_pdm_dai_ops = {
Rander Wang5e7484d2020-02-14 19:47:39 -0600897 .startup = intel_startup,
Vinod Koulc46302e2018-04-26 18:39:05 +0530898 .hw_params = intel_hw_params,
Rander Wang27b198f2020-02-14 19:47:37 -0600899 .prepare = intel_prepare,
Rander Wang973a8422020-02-14 19:47:38 -0600900 .trigger = intel_trigger,
Vinod Koulc46302e2018-04-26 18:39:05 +0530901 .hw_free = intel_hw_free,
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -0500902 .shutdown = intel_shutdown,
Vinod Koulc46302e2018-04-26 18:39:05 +0530903 .set_sdw_stream = intel_pdm_set_sdw_stream,
904};
905
906static const struct snd_soc_component_driver dai_component = {
907 .name = "soundwire",
908};
909
910static int intel_create_dai(struct sdw_cdns *cdns,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500911 struct snd_soc_dai_driver *dais,
912 enum intel_pdi_type type,
913 u32 num, u32 off, u32 max_ch, bool pcm)
Vinod Koulc46302e2018-04-26 18:39:05 +0530914{
915 int i;
916
917 if (num == 0)
918 return 0;
919
920 /* TODO: Read supported rates/formats from hardware */
921 for (i = off; i < (off + num); i++) {
922 dais[i].name = kasprintf(GFP_KERNEL, "SDW%d Pin%d",
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500923 cdns->instance, i);
Vinod Koulc46302e2018-04-26 18:39:05 +0530924 if (!dais[i].name)
925 return -ENOMEM;
926
927 if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
Vinod Koulc46302e2018-04-26 18:39:05 +0530928 dais[i].playback.channels_min = 1;
929 dais[i].playback.channels_max = max_ch;
930 dais[i].playback.rates = SNDRV_PCM_RATE_48000;
931 dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
932 }
933
934 if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
Srinivas Kandagatla39194122019-06-06 12:23:04 +0100935 dais[i].capture.channels_min = 1;
936 dais[i].capture.channels_max = max_ch;
Vinod Koulc46302e2018-04-26 18:39:05 +0530937 dais[i].capture.rates = SNDRV_PCM_RATE_48000;
938 dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
939 }
940
Vinod Koulc46302e2018-04-26 18:39:05 +0530941 if (pcm)
942 dais[i].ops = &intel_pcm_dai_ops;
943 else
944 dais[i].ops = &intel_pdm_dai_ops;
945 }
946
947 return 0;
948}
949
950static int intel_register_dai(struct sdw_intel *sdw)
951{
952 struct sdw_cdns *cdns = &sdw->cdns;
953 struct sdw_cdns_streams *stream;
954 struct snd_soc_dai_driver *dais;
955 int num_dai, ret, off = 0;
956
957 /* DAIs are created based on total number of PDIs supported */
958 num_dai = cdns->pcm.num_pdi + cdns->pdm.num_pdi;
959
960 dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
961 if (!dais)
962 return -ENOMEM;
963
964 /* Create PCM DAIs */
965 stream = &cdns->pcm;
966
Bard Liaocf924962019-09-16 14:23:43 -0500967 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
Vinod Koul1215dae2019-05-02 16:29:25 +0530968 off, stream->num_ch_in, true);
Vinod Koulc46302e2018-04-26 18:39:05 +0530969 if (ret)
970 return ret;
971
972 off += cdns->pcm.num_in;
Vinod Koul1215dae2019-05-02 16:29:25 +0530973 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
974 off, stream->num_ch_out, true);
Vinod Koulc46302e2018-04-26 18:39:05 +0530975 if (ret)
976 return ret;
977
978 off += cdns->pcm.num_out;
Vinod Koul1215dae2019-05-02 16:29:25 +0530979 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
980 off, stream->num_ch_bd, true);
Vinod Koulc46302e2018-04-26 18:39:05 +0530981 if (ret)
982 return ret;
983
984 /* Create PDM DAIs */
985 stream = &cdns->pdm;
986 off += cdns->pcm.num_bd;
Vinod Koul1215dae2019-05-02 16:29:25 +0530987 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pdm.num_in,
988 off, stream->num_ch_in, false);
Vinod Koulc46302e2018-04-26 18:39:05 +0530989 if (ret)
990 return ret;
991
992 off += cdns->pdm.num_in;
Vinod Koul1215dae2019-05-02 16:29:25 +0530993 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pdm.num_out,
994 off, stream->num_ch_out, false);
Vinod Koulc46302e2018-04-26 18:39:05 +0530995 if (ret)
996 return ret;
997
Bard Liaocf924962019-09-16 14:23:43 -0500998 off += cdns->pdm.num_out;
Vinod Koul1215dae2019-05-02 16:29:25 +0530999 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pdm.num_bd,
1000 off, stream->num_ch_bd, false);
Vinod Koulc46302e2018-04-26 18:39:05 +05301001 if (ret)
1002 return ret;
1003
1004 return snd_soc_register_component(cdns->dev, &dai_component,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001005 dais, num_dai);
Vinod Koulc46302e2018-04-26 18:39:05 +05301006}
1007
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001008static int sdw_master_read_intel_prop(struct sdw_bus *bus)
1009{
1010 struct sdw_master_prop *prop = &bus->prop;
1011 struct fwnode_handle *link;
1012 char name[32];
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001013 u32 quirk_mask;
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001014
1015 /* Find master handle */
1016 snprintf(name, sizeof(name),
1017 "mipi-sdw-link-%d-subproperties", bus->link_id);
1018
1019 link = device_get_named_child_node(bus->dev, name);
1020 if (!link) {
1021 dev_err(bus->dev, "Master node %s not found\n", name);
1022 return -EIO;
1023 }
1024
1025 fwnode_property_read_u32(link,
1026 "intel-sdw-ip-clock",
1027 &prop->mclk_freq);
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001028
Bard Liaoa19efb52020-01-13 17:11:29 -06001029 /* the values reported by BIOS are the 2x clock, not the bus clock */
1030 prop->mclk_freq /= 2;
1031
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001032 fwnode_property_read_u32(link,
1033 "intel-quirk-mask",
1034 &quirk_mask);
1035
1036 if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
1037 prop->hw_disabled = true;
1038
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001039 return 0;
1040}
1041
Vinod Koul71bb8a12017-12-14 11:19:43 +05301042static int intel_prop_read(struct sdw_bus *bus)
1043{
1044 /* Initialize with default handler to read all DisCo properties */
1045 sdw_master_read_prop(bus);
1046
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001047 /* read Intel-specific properties */
1048 sdw_master_read_intel_prop(bus);
1049
Vinod Koul71bb8a12017-12-14 11:19:43 +05301050 return 0;
1051}
1052
Shreyas NCc91605f2018-04-26 18:38:43 +05301053static struct sdw_master_ops sdw_intel_ops = {
1054 .read_prop = sdw_master_read_prop,
1055 .xfer_msg = cdns_xfer_msg,
1056 .xfer_msg_defer = cdns_xfer_msg_defer,
1057 .reset_page_addr = cdns_reset_page_addr,
Vinod Koul07abeff2018-04-26 18:38:48 +05301058 .set_bus_conf = cdns_bus_conf,
Shreyas NC30246e22018-07-27 14:44:17 +05301059 .pre_bank_switch = intel_pre_bank_switch,
1060 .post_bank_switch = intel_post_bank_switch,
Shreyas NCc91605f2018-04-26 18:38:43 +05301061};
1062
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001063static int intel_init(struct sdw_intel *sdw)
1064{
1065 /* Initialize shim and controller */
1066 intel_link_power_up(sdw);
1067 intel_shim_init(sdw);
1068
Rander Wang7b174f22020-03-17 11:33:14 -05001069 return sdw_cdns_init(&sdw->cdns);
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001070}
1071
Vinod Koul71bb8a12017-12-14 11:19:43 +05301072/*
1073 * probe and init
1074 */
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001075static int intel_master_probe(struct platform_device *pdev)
Vinod Koul71bb8a12017-12-14 11:19:43 +05301076{
Vinod Koul37a2d222018-04-26 18:38:58 +05301077 struct sdw_cdns_stream_config config;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001078 struct device *dev = &pdev->dev;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301079 struct sdw_intel *sdw;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001080 struct sdw_bus *bus;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301081 int ret;
1082
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001083 sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301084 if (!sdw)
1085 return -ENOMEM;
1086
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001087 bus = &sdw->cdns.bus;
1088
Vinod Koul71bb8a12017-12-14 11:19:43 +05301089 sdw->instance = pdev->id;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001090 sdw->link_res = dev_get_platdata(dev);
1091 sdw->cdns.dev = dev;
Pierre-Louis Bossart25234862020-02-14 19:47:36 -06001092 sdw->cdns.registers = sdw->link_res->registers;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301093 sdw->cdns.instance = sdw->instance;
1094 sdw->cdns.msg_count = 0;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001095 bus->link_id = pdev->id;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301096
1097 sdw_cdns_probe(&sdw->cdns);
1098
1099 /* Set property read ops */
Shreyas NCc91605f2018-04-26 18:38:43 +05301100 sdw_intel_ops.read_prop = intel_prop_read;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001101 bus->ops = &sdw_intel_ops;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301102
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001103 /* set driver data, accessed by snd_soc_dai_get_drvdata() */
Vinod Koul71bb8a12017-12-14 11:19:43 +05301104 platform_set_drvdata(pdev, sdw);
1105
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001106 ret = sdw_bus_master_add(bus, dev, dev->fwnode);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301107 if (ret) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001108 dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001109 return ret;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301110 }
1111
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001112 if (bus->prop.hw_disabled) {
1113 dev_info(dev,
1114 "SoundWire master %d is disabled, will be ignored\n",
1115 bus->link_id);
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001116 return 0;
1117 }
1118
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001119 /* Initialize shim, controller and Cadence IP */
1120 ret = intel_init(sdw);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301121 if (ret)
1122 goto err_init;
1123
Vinod Koul37a2d222018-04-26 18:38:58 +05301124 /* Read the PDI config and initialize cadence PDI */
1125 intel_pdi_init(sdw, &config);
1126 ret = sdw_cdns_pdi_init(&sdw->cdns, config);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301127 if (ret)
1128 goto err_init;
1129
Vinod Koul37a2d222018-04-26 18:38:58 +05301130 intel_pdi_ch_update(sdw);
1131
Vinod Koul71bb8a12017-12-14 11:19:43 +05301132 /* Acquire IRQ */
Pierre-Louis Bossart25234862020-02-14 19:47:36 -06001133 ret = request_threaded_irq(sdw->link_res->irq,
1134 sdw_cdns_irq, sdw_cdns_thread,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -05001135 IRQF_SHARED, KBUILD_MODNAME, &sdw->cdns);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301136 if (ret < 0) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001137 dev_err(dev, "unable to grab IRQ %d, disabling device\n",
Pierre-Louis Bossart25234862020-02-14 19:47:36 -06001138 sdw->link_res->irq);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301139 goto err_init;
1140 }
1141
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001142 ret = sdw_cdns_enable_interrupt(&sdw->cdns, true);
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001143 if (ret < 0) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001144 dev_err(dev, "cannot enable interrupts\n");
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001145 goto err_init;
1146 }
1147
1148 ret = sdw_cdns_exit_reset(&sdw->cdns);
1149 if (ret < 0) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001150 dev_err(dev, "unable to exit bus reset sequence\n");
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001151 goto err_interrupt;
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001152 }
1153
Vinod Koulc46302e2018-04-26 18:39:05 +05301154 /* Register DAIs */
1155 ret = intel_register_dai(sdw);
1156 if (ret) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001157 dev_err(dev, "DAI registration failed: %d\n", ret);
1158 snd_soc_unregister_component(dev);
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001159 goto err_interrupt;
Vinod Koulc46302e2018-04-26 18:39:05 +05301160 }
1161
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -05001162 intel_debugfs_init(sdw);
1163
Vinod Koul71bb8a12017-12-14 11:19:43 +05301164 return 0;
1165
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001166err_interrupt:
1167 sdw_cdns_enable_interrupt(&sdw->cdns, false);
Pierre-Louis Bossart25234862020-02-14 19:47:36 -06001168 free_irq(sdw->link_res->irq, sdw);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301169err_init:
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001170 sdw_bus_master_delete(bus);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301171 return ret;
1172}
1173
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001174static int intel_master_remove(struct platform_device *pdev)
Vinod Koul71bb8a12017-12-14 11:19:43 +05301175{
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001176 struct device *dev = &pdev->dev;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301177 struct sdw_intel *sdw;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001178 struct sdw_bus *bus;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301179
1180 sdw = platform_get_drvdata(pdev);
1181
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001182 bus = &sdw->cdns.bus;
1183
1184 if (!bus->prop.hw_disabled) {
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001185 intel_debugfs_exit(sdw);
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001186 sdw_cdns_enable_interrupt(&sdw->cdns, false);
Pierre-Louis Bossart25234862020-02-14 19:47:36 -06001187 free_irq(sdw->link_res->irq, sdw);
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001188 snd_soc_unregister_component(dev);
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001189 }
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001190 sdw_bus_master_delete(bus);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301191
1192 return 0;
1193}
1194
1195static struct platform_driver sdw_intel_drv = {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001196 .probe = intel_master_probe,
1197 .remove = intel_master_remove,
Vinod Koul71bb8a12017-12-14 11:19:43 +05301198 .driver = {
1199 .name = "int-sdw",
Vinod Koul71bb8a12017-12-14 11:19:43 +05301200 },
1201};
1202
1203module_platform_driver(sdw_intel_drv);
1204
1205MODULE_LICENSE("Dual BSD/GPL");
1206MODULE_ALIAS("platform:int-sdw");
1207MODULE_DESCRIPTION("Intel Soundwire Master Driver");