blob: 2e1e1088a743a86cf7a1364fe0ea26f38774b850 [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 intel_startup(struct snd_pcm_substream *substream,
629 struct snd_soc_dai *dai)
630{
631 /*
632 * TODO: add pm_runtime support here, the startup callback
633 * will make sure the IP is 'active'
634 */
Pierre-Louis Bossartff16d1e2020-07-01 02:43:54 +0800635 return 0;
Rander Wang5e7484d2020-02-14 19:47:39 -0600636}
637
Vinod Koulc46302e2018-04-26 18:39:05 +0530638static int intel_hw_params(struct snd_pcm_substream *substream,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500639 struct snd_pcm_hw_params *params,
640 struct snd_soc_dai *dai)
Vinod Koulc46302e2018-04-26 18:39:05 +0530641{
642 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
643 struct sdw_intel *sdw = cdns_to_intel(cdns);
644 struct sdw_cdns_dma_data *dma;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500645 struct sdw_cdns_pdi *pdi;
Vinod Koulc46302e2018-04-26 18:39:05 +0530646 struct sdw_stream_config sconfig;
647 struct sdw_port_config *pconfig;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500648 int ch, dir;
649 int ret;
Vinod Koulc46302e2018-04-26 18:39:05 +0530650 bool pcm = true;
651
652 dma = snd_soc_dai_get_dma_data(dai, substream);
653 if (!dma)
654 return -EIO;
655
656 ch = params_channels(params);
657 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
658 dir = SDW_DATA_DIR_RX;
659 else
660 dir = SDW_DATA_DIR_TX;
661
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500662 if (dma->stream_type == SDW_STREAM_PDM)
Vinod Koulc46302e2018-04-26 18:39:05 +0530663 pcm = false;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500664
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500665 if (pcm)
Bard Liao1b53385e2019-09-16 14:23:48 -0500666 pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500667 else
Bard Liao1b53385e2019-09-16 14:23:48 -0500668 pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pdm, ch, dir, dai->id);
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500669
670 if (!pdi) {
671 ret = -EINVAL;
672 goto error;
Vinod Koulc46302e2018-04-26 18:39:05 +0530673 }
674
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500675 /* do run-time configurations for SHIM, ALH and PDI/PORT */
676 intel_pdi_shim_configure(sdw, pdi);
677 intel_pdi_alh_configure(sdw, pdi);
678 sdw_cdns_config_stream(cdns, ch, dir, pdi);
Vinod Koulc46302e2018-04-26 18:39:05 +0530679
Vinod Koulc46302e2018-04-26 18:39:05 +0530680
681 /* Inform DSP about PDI stream number */
Rander Wang4b206d32019-12-11 19:45:02 -0600682 ret = intel_params_stream(sdw, substream, dai, params,
683 sdw->instance,
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500684 pdi->intel_alh_id);
685 if (ret)
686 goto error;
Vinod Koulc46302e2018-04-26 18:39:05 +0530687
688 sconfig.direction = dir;
689 sconfig.ch_count = ch;
690 sconfig.frame_rate = params_rate(params);
691 sconfig.type = dma->stream_type;
692
693 if (dma->stream_type == SDW_STREAM_PDM) {
694 sconfig.frame_rate *= 50;
695 sconfig.bps = 1;
696 } else {
697 sconfig.bps = snd_pcm_format_width(params_format(params));
698 }
699
700 /* Port configuration */
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500701 pconfig = kcalloc(1, sizeof(*pconfig), GFP_KERNEL);
Vinod Koulc46302e2018-04-26 18:39:05 +0530702 if (!pconfig) {
703 ret = -ENOMEM;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500704 goto error;
Vinod Koulc46302e2018-04-26 18:39:05 +0530705 }
706
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500707 pconfig->num = pdi->num;
708 pconfig->ch_mask = (1 << ch) - 1;
Vinod Koulc46302e2018-04-26 18:39:05 +0530709
710 ret = sdw_stream_add_master(&cdns->bus, &sconfig,
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500711 pconfig, 1, dma->stream);
712 if (ret)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500713 dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
Vinod Koulc46302e2018-04-26 18:39:05 +0530714
715 kfree(pconfig);
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500716error:
Vinod Koulc46302e2018-04-26 18:39:05 +0530717 return ret;
718}
719
Rander Wang27b198f2020-02-14 19:47:37 -0600720static int intel_prepare(struct snd_pcm_substream *substream,
721 struct snd_soc_dai *dai)
722{
723 struct sdw_cdns_dma_data *dma;
724
725 dma = snd_soc_dai_get_dma_data(dai, substream);
726 if (!dma) {
727 dev_err(dai->dev, "failed to get dma data in %s",
728 __func__);
729 return -EIO;
730 }
731
732 return sdw_prepare_stream(dma->stream);
733}
734
Rander Wang973a8422020-02-14 19:47:38 -0600735static int intel_trigger(struct snd_pcm_substream *substream, int cmd,
736 struct snd_soc_dai *dai)
737{
738 struct sdw_cdns_dma_data *dma;
739 int ret;
740
741 dma = snd_soc_dai_get_dma_data(dai, substream);
742 if (!dma) {
743 dev_err(dai->dev, "failed to get dma data in %s", __func__);
744 return -EIO;
745 }
746
747 switch (cmd) {
748 case SNDRV_PCM_TRIGGER_START:
749 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
750 case SNDRV_PCM_TRIGGER_RESUME:
751 ret = sdw_enable_stream(dma->stream);
752 break;
753
754 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
755 case SNDRV_PCM_TRIGGER_SUSPEND:
756 case SNDRV_PCM_TRIGGER_STOP:
757 ret = sdw_disable_stream(dma->stream);
758 break;
759
760 default:
761 ret = -EINVAL;
762 break;
763 }
764
765 if (ret)
766 dev_err(dai->dev,
767 "%s trigger %d failed: %d",
768 __func__, cmd, ret);
769 return ret;
770}
771
Vinod Koulc46302e2018-04-26 18:39:05 +0530772static int
773intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
774{
775 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -0600776 struct sdw_intel *sdw = cdns_to_intel(cdns);
Vinod Koulc46302e2018-04-26 18:39:05 +0530777 struct sdw_cdns_dma_data *dma;
778 int ret;
779
780 dma = snd_soc_dai_get_dma_data(dai, substream);
781 if (!dma)
782 return -EIO;
783
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -0600784 ret = sdw_deprepare_stream(dma->stream);
785 if (ret) {
786 dev_err(dai->dev, "sdw_deprepare_stream: failed %d", ret);
787 return ret;
788 }
789
Vinod Koulc46302e2018-04-26 18:39:05 +0530790 ret = sdw_stream_remove_master(&cdns->bus, dma->stream);
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -0600791 if (ret < 0) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500792 dev_err(dai->dev, "remove master from stream %s failed: %d\n",
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500793 dma->stream->name, ret);
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -0600794 return ret;
795 }
Vinod Koulc46302e2018-04-26 18:39:05 +0530796
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -0600797 ret = intel_free_stream(sdw, substream, dai, sdw->instance);
798 if (ret < 0) {
799 dev_err(dai->dev, "intel_free_stream: failed %d", ret);
800 return ret;
801 }
802
Pierre-Louis Bossarteff346f2020-02-14 19:47:40 -0600803 return 0;
Vinod Koulc46302e2018-04-26 18:39:05 +0530804}
805
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -0500806static void intel_shutdown(struct snd_pcm_substream *substream,
807 struct snd_soc_dai *dai)
808{
809 struct sdw_cdns_dma_data *dma;
810
811 dma = snd_soc_dai_get_dma_data(dai, substream);
812 if (!dma)
813 return;
814
815 snd_soc_dai_set_dma_data(dai, substream, NULL);
816 kfree(dma);
817}
818
Vinod Koulc46302e2018-04-26 18:39:05 +0530819static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500820 void *stream, int direction)
Vinod Koulc46302e2018-04-26 18:39:05 +0530821{
822 return cdns_set_sdw_stream(dai, stream, true, direction);
823}
824
825static int intel_pdm_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500826 void *stream, int direction)
Vinod Koulc46302e2018-04-26 18:39:05 +0530827{
828 return cdns_set_sdw_stream(dai, stream, false, direction);
829}
830
Pierre-Louis Bossart09553142020-07-01 02:43:52 +0800831static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
832 int direction)
833{
834 struct sdw_cdns_dma_data *dma;
835
836 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
837 dma = dai->playback_dma_data;
838 else
839 dma = dai->capture_dma_data;
840
841 if (!dma)
842 return NULL;
843
844 return dma->stream;
845}
846
Julia Lawallb1635592018-10-27 15:34:42 +0200847static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
Rander Wang5e7484d2020-02-14 19:47:39 -0600848 .startup = intel_startup,
Vinod Koulc46302e2018-04-26 18:39:05 +0530849 .hw_params = intel_hw_params,
Rander Wang27b198f2020-02-14 19:47:37 -0600850 .prepare = intel_prepare,
Rander Wang973a8422020-02-14 19:47:38 -0600851 .trigger = intel_trigger,
Vinod Koulc46302e2018-04-26 18:39:05 +0530852 .hw_free = intel_hw_free,
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -0500853 .shutdown = intel_shutdown,
Vinod Koulc46302e2018-04-26 18:39:05 +0530854 .set_sdw_stream = intel_pcm_set_sdw_stream,
Pierre-Louis Bossart09553142020-07-01 02:43:52 +0800855 .get_sdw_stream = intel_get_sdw_stream,
Vinod Koulc46302e2018-04-26 18:39:05 +0530856};
857
Julia Lawallb1635592018-10-27 15:34:42 +0200858static const struct snd_soc_dai_ops intel_pdm_dai_ops = {
Rander Wang5e7484d2020-02-14 19:47:39 -0600859 .startup = intel_startup,
Vinod Koulc46302e2018-04-26 18:39:05 +0530860 .hw_params = intel_hw_params,
Rander Wang27b198f2020-02-14 19:47:37 -0600861 .prepare = intel_prepare,
Rander Wang973a8422020-02-14 19:47:38 -0600862 .trigger = intel_trigger,
Vinod Koulc46302e2018-04-26 18:39:05 +0530863 .hw_free = intel_hw_free,
Pierre-Louis Bossart183c7682019-08-05 19:55:22 -0500864 .shutdown = intel_shutdown,
Vinod Koulc46302e2018-04-26 18:39:05 +0530865 .set_sdw_stream = intel_pdm_set_sdw_stream,
Pierre-Louis Bossart09553142020-07-01 02:43:52 +0800866 .get_sdw_stream = intel_get_sdw_stream,
Vinod Koulc46302e2018-04-26 18:39:05 +0530867};
868
869static const struct snd_soc_component_driver dai_component = {
870 .name = "soundwire",
871};
872
873static int intel_create_dai(struct sdw_cdns *cdns,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500874 struct snd_soc_dai_driver *dais,
875 enum intel_pdi_type type,
876 u32 num, u32 off, u32 max_ch, bool pcm)
Vinod Koulc46302e2018-04-26 18:39:05 +0530877{
878 int i;
879
880 if (num == 0)
881 return 0;
882
883 /* TODO: Read supported rates/formats from hardware */
884 for (i = off; i < (off + num); i++) {
885 dais[i].name = kasprintf(GFP_KERNEL, "SDW%d Pin%d",
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500886 cdns->instance, i);
Vinod Koulc46302e2018-04-26 18:39:05 +0530887 if (!dais[i].name)
888 return -ENOMEM;
889
890 if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
Vinod Koulc46302e2018-04-26 18:39:05 +0530891 dais[i].playback.channels_min = 1;
892 dais[i].playback.channels_max = max_ch;
893 dais[i].playback.rates = SNDRV_PCM_RATE_48000;
894 dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
895 }
896
897 if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
Srinivas Kandagatla39194122019-06-06 12:23:04 +0100898 dais[i].capture.channels_min = 1;
899 dais[i].capture.channels_max = max_ch;
Vinod Koulc46302e2018-04-26 18:39:05 +0530900 dais[i].capture.rates = SNDRV_PCM_RATE_48000;
901 dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
902 }
903
Vinod Koulc46302e2018-04-26 18:39:05 +0530904 if (pcm)
905 dais[i].ops = &intel_pcm_dai_ops;
906 else
907 dais[i].ops = &intel_pdm_dai_ops;
908 }
909
910 return 0;
911}
912
913static int intel_register_dai(struct sdw_intel *sdw)
914{
915 struct sdw_cdns *cdns = &sdw->cdns;
916 struct sdw_cdns_streams *stream;
917 struct snd_soc_dai_driver *dais;
918 int num_dai, ret, off = 0;
919
920 /* DAIs are created based on total number of PDIs supported */
921 num_dai = cdns->pcm.num_pdi + cdns->pdm.num_pdi;
922
923 dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
924 if (!dais)
925 return -ENOMEM;
926
927 /* Create PCM DAIs */
928 stream = &cdns->pcm;
929
Bard Liaocf924962019-09-16 14:23:43 -0500930 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
Vinod Koul1215dae2019-05-02 16:29:25 +0530931 off, stream->num_ch_in, true);
Vinod Koulc46302e2018-04-26 18:39:05 +0530932 if (ret)
933 return ret;
934
935 off += cdns->pcm.num_in;
Vinod Koul1215dae2019-05-02 16:29:25 +0530936 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
937 off, stream->num_ch_out, true);
Vinod Koulc46302e2018-04-26 18:39:05 +0530938 if (ret)
939 return ret;
940
941 off += cdns->pcm.num_out;
Vinod Koul1215dae2019-05-02 16:29:25 +0530942 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
943 off, stream->num_ch_bd, true);
Vinod Koulc46302e2018-04-26 18:39:05 +0530944 if (ret)
945 return ret;
946
947 /* Create PDM DAIs */
948 stream = &cdns->pdm;
949 off += cdns->pcm.num_bd;
Vinod Koul1215dae2019-05-02 16:29:25 +0530950 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pdm.num_in,
951 off, stream->num_ch_in, false);
Vinod Koulc46302e2018-04-26 18:39:05 +0530952 if (ret)
953 return ret;
954
955 off += cdns->pdm.num_in;
Vinod Koul1215dae2019-05-02 16:29:25 +0530956 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pdm.num_out,
957 off, stream->num_ch_out, false);
Vinod Koulc46302e2018-04-26 18:39:05 +0530958 if (ret)
959 return ret;
960
Bard Liaocf924962019-09-16 14:23:43 -0500961 off += cdns->pdm.num_out;
Vinod Koul1215dae2019-05-02 16:29:25 +0530962 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pdm.num_bd,
963 off, stream->num_ch_bd, false);
Vinod Koulc46302e2018-04-26 18:39:05 +0530964 if (ret)
965 return ret;
966
967 return snd_soc_register_component(cdns->dev, &dai_component,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500968 dais, num_dai);
Vinod Koulc46302e2018-04-26 18:39:05 +0530969}
970
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -0500971static int sdw_master_read_intel_prop(struct sdw_bus *bus)
972{
973 struct sdw_master_prop *prop = &bus->prop;
974 struct fwnode_handle *link;
975 char name[32];
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -0500976 u32 quirk_mask;
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -0500977
978 /* Find master handle */
979 snprintf(name, sizeof(name),
980 "mipi-sdw-link-%d-subproperties", bus->link_id);
981
982 link = device_get_named_child_node(bus->dev, name);
983 if (!link) {
984 dev_err(bus->dev, "Master node %s not found\n", name);
985 return -EIO;
986 }
987
988 fwnode_property_read_u32(link,
989 "intel-sdw-ip-clock",
990 &prop->mclk_freq);
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -0500991
Bard Liaoa19efb52020-01-13 17:11:29 -0600992 /* the values reported by BIOS are the 2x clock, not the bus clock */
993 prop->mclk_freq /= 2;
994
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -0500995 fwnode_property_read_u32(link,
996 "intel-quirk-mask",
997 &quirk_mask);
998
999 if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
1000 prop->hw_disabled = true;
1001
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001002 return 0;
1003}
1004
Vinod Koul71bb8a12017-12-14 11:19:43 +05301005static int intel_prop_read(struct sdw_bus *bus)
1006{
1007 /* Initialize with default handler to read all DisCo properties */
1008 sdw_master_read_prop(bus);
1009
Pierre-Louis Bossart085f4ac2019-08-05 19:55:16 -05001010 /* read Intel-specific properties */
1011 sdw_master_read_intel_prop(bus);
1012
Vinod Koul71bb8a12017-12-14 11:19:43 +05301013 return 0;
1014}
1015
Shreyas NCc91605f2018-04-26 18:38:43 +05301016static struct sdw_master_ops sdw_intel_ops = {
1017 .read_prop = sdw_master_read_prop,
1018 .xfer_msg = cdns_xfer_msg,
1019 .xfer_msg_defer = cdns_xfer_msg_defer,
1020 .reset_page_addr = cdns_reset_page_addr,
Vinod Koul07abeff2018-04-26 18:38:48 +05301021 .set_bus_conf = cdns_bus_conf,
Shreyas NC30246e22018-07-27 14:44:17 +05301022 .pre_bank_switch = intel_pre_bank_switch,
1023 .post_bank_switch = intel_post_bank_switch,
Shreyas NCc91605f2018-04-26 18:38:43 +05301024};
1025
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001026static int intel_init(struct sdw_intel *sdw)
1027{
1028 /* Initialize shim and controller */
1029 intel_link_power_up(sdw);
1030 intel_shim_init(sdw);
1031
Rander Wang7b174f22020-03-17 11:33:14 -05001032 return sdw_cdns_init(&sdw->cdns);
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001033}
1034
Vinod Koul71bb8a12017-12-14 11:19:43 +05301035/*
1036 * probe and init
1037 */
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001038static int intel_master_probe(struct platform_device *pdev)
Vinod Koul71bb8a12017-12-14 11:19:43 +05301039{
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001040 struct device *dev = &pdev->dev;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301041 struct sdw_intel *sdw;
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001042 struct sdw_cdns *cdns;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001043 struct sdw_bus *bus;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301044 int ret;
1045
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001046 sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301047 if (!sdw)
1048 return -ENOMEM;
1049
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001050 cdns = &sdw->cdns;
1051 bus = &cdns->bus;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001052
Vinod Koul71bb8a12017-12-14 11:19:43 +05301053 sdw->instance = pdev->id;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001054 sdw->link_res = dev_get_platdata(dev);
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001055 cdns->dev = dev;
1056 cdns->registers = sdw->link_res->registers;
1057 cdns->instance = sdw->instance;
1058 cdns->msg_count = 0;
1059
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001060 bus->link_id = pdev->id;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301061
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001062 sdw_cdns_probe(cdns);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301063
1064 /* Set property read ops */
Shreyas NCc91605f2018-04-26 18:38:43 +05301065 sdw_intel_ops.read_prop = intel_prop_read;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001066 bus->ops = &sdw_intel_ops;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301067
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001068 /* set driver data, accessed by snd_soc_dai_get_drvdata() */
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001069 dev_set_drvdata(dev, cdns);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301070
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001071 ret = sdw_bus_master_add(bus, dev, dev->fwnode);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301072 if (ret) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001073 dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001074 return ret;
Vinod Koul71bb8a12017-12-14 11:19:43 +05301075 }
1076
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001077 if (bus->prop.hw_disabled)
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001078 dev_info(dev,
1079 "SoundWire master %d is disabled, will be ignored\n",
1080 bus->link_id);
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001081
1082 /* Acquire IRQ */
1083 ret = request_threaded_irq(sdw->link_res->irq,
1084 sdw_cdns_irq, sdw_cdns_thread,
1085 IRQF_SHARED, KBUILD_MODNAME, cdns);
1086 if (ret < 0) {
1087 dev_err(dev, "unable to grab IRQ %d, disabling device\n",
1088 sdw->link_res->irq);
1089 goto err_init;
1090 }
1091
1092 return 0;
1093
1094err_init:
1095 sdw_bus_master_delete(bus);
1096 return ret;
1097}
1098
1099int intel_master_startup(struct platform_device *pdev)
1100{
1101 struct sdw_cdns_stream_config config;
1102 struct device *dev = &pdev->dev;
1103 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1104 struct sdw_intel *sdw = cdns_to_intel(cdns);
1105 struct sdw_bus *bus = &cdns->bus;
1106 int ret;
1107
1108 if (bus->prop.hw_disabled) {
1109 dev_info(dev,
1110 "SoundWire master %d is disabled, ignoring\n",
1111 sdw->instance);
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001112 return 0;
1113 }
1114
Pierre-Louis Bossartdfbe6422019-10-22 18:54:46 -05001115 /* Initialize shim, controller and Cadence IP */
1116 ret = intel_init(sdw);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301117 if (ret)
1118 goto err_init;
1119
Vinod Koul37a2d222018-04-26 18:38:58 +05301120 /* Read the PDI config and initialize cadence PDI */
1121 intel_pdi_init(sdw, &config);
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001122 ret = sdw_cdns_pdi_init(cdns, config);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301123 if (ret)
1124 goto err_init;
1125
Vinod Koul37a2d222018-04-26 18:38:58 +05301126 intel_pdi_ch_update(sdw);
1127
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001128 ret = sdw_cdns_enable_interrupt(cdns, true);
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001129 if (ret < 0) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001130 dev_err(dev, "cannot enable interrupts\n");
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001131 goto err_init;
1132 }
1133
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001134 ret = sdw_cdns_exit_reset(cdns);
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001135 if (ret < 0) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001136 dev_err(dev, "unable to exit bus reset sequence\n");
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001137 goto err_interrupt;
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001138 }
1139
Vinod Koulc46302e2018-04-26 18:39:05 +05301140 /* Register DAIs */
1141 ret = intel_register_dai(sdw);
1142 if (ret) {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001143 dev_err(dev, "DAI registration failed: %d\n", ret);
1144 snd_soc_unregister_component(dev);
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001145 goto err_interrupt;
Vinod Koulc46302e2018-04-26 18:39:05 +05301146 }
1147
Pierre-Louis Bossart79ee6632019-08-21 13:58:20 -05001148 intel_debugfs_init(sdw);
1149
Vinod Koul71bb8a12017-12-14 11:19:43 +05301150 return 0;
1151
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001152err_interrupt:
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001153 sdw_cdns_enable_interrupt(cdns, false);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301154err_init:
Vinod Koul71bb8a12017-12-14 11:19:43 +05301155 return ret;
1156}
1157
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001158static int intel_master_remove(struct platform_device *pdev)
Vinod Koul71bb8a12017-12-14 11:19:43 +05301159{
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001160 struct device *dev = &pdev->dev;
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001161 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1162 struct sdw_intel *sdw = cdns_to_intel(cdns);
1163 struct sdw_bus *bus = &cdns->bus;
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001164
1165 if (!bus->prop.hw_disabled) {
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001166 intel_debugfs_exit(sdw);
Pierre-Louis Bossart83e129af2020-06-01 02:20:58 +08001167 sdw_cdns_enable_interrupt(cdns, false);
Pierre-Louis Bossart25234862020-02-14 19:47:36 -06001168 free_irq(sdw->link_res->irq, sdw);
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001169 snd_soc_unregister_component(dev);
Pierre-Louis Bossart395713d2019-08-21 13:58:21 -05001170 }
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001171 sdw_bus_master_delete(bus);
Vinod Koul71bb8a12017-12-14 11:19:43 +05301172
1173 return 0;
1174}
1175
1176static struct platform_driver sdw_intel_drv = {
Pierre-Louis Bossartb6109dd2020-06-01 02:20:57 +08001177 .probe = intel_master_probe,
1178 .remove = intel_master_remove,
Vinod Koul71bb8a12017-12-14 11:19:43 +05301179 .driver = {
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001180 .name = "intel-sdw",
Vinod Koul71bb8a12017-12-14 11:19:43 +05301181 },
1182};
1183
1184module_platform_driver(sdw_intel_drv);
1185
1186MODULE_LICENSE("Dual BSD/GPL");
Pierre-Louis Bossart6d2c6662020-06-01 02:21:02 +08001187MODULE_ALIAS("platform:intel-sdw");
Vinod Koul71bb8a12017-12-14 11:19:43 +05301188MODULE_DESCRIPTION("Intel Soundwire Master Driver");