blob: 97e3205d95a2f2bdda69e954a62716991177dfd6 [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>
9#include <linux/delay.h>
Paul Gortmaker4abbd782019-04-13 11:12:52 -040010#include <linux/module.h>
Vinod Koul71bb8a12017-12-14 11:19:43 +053011#include <linux/interrupt.h>
12#include <linux/platform_device.h>
Vinod Koul37a2d222018-04-26 18:38:58 +053013#include <sound/pcm_params.h>
14#include <sound/soc.h>
Vinod Koul71bb8a12017-12-14 11:19:43 +053015#include <linux/soundwire/sdw_registers.h>
16#include <linux/soundwire/sdw.h>
17#include <linux/soundwire/sdw_intel.h>
18#include "cadence_master.h"
19#include "intel.h"
20
21/* Intel SHIM Registers Definition */
22#define SDW_SHIM_LCAP 0x0
23#define SDW_SHIM_LCTL 0x4
24#define SDW_SHIM_IPPTR 0x8
25#define SDW_SHIM_SYNC 0xC
26
Pierre-Louis Bossart7cc6e312019-05-01 10:57:39 -050027#define SDW_SHIM_CTLSCAP(x) (0x010 + 0x60 * (x))
28#define SDW_SHIM_CTLS0CM(x) (0x012 + 0x60 * (x))
29#define SDW_SHIM_CTLS1CM(x) (0x014 + 0x60 * (x))
30#define SDW_SHIM_CTLS2CM(x) (0x016 + 0x60 * (x))
31#define SDW_SHIM_CTLS3CM(x) (0x018 + 0x60 * (x))
32#define SDW_SHIM_PCMSCAP(x) (0x020 + 0x60 * (x))
Vinod Koul71bb8a12017-12-14 11:19:43 +053033
Pierre-Louis Bossart7cc6e312019-05-01 10:57:39 -050034#define SDW_SHIM_PCMSYCHM(x, y) (0x022 + (0x60 * (x)) + (0x2 * (y)))
35#define SDW_SHIM_PCMSYCHC(x, y) (0x042 + (0x60 * (x)) + (0x2 * (y)))
36#define SDW_SHIM_PDMSCAP(x) (0x062 + 0x60 * (x))
37#define SDW_SHIM_IOCTL(x) (0x06C + 0x60 * (x))
38#define SDW_SHIM_CTMCTL(x) (0x06E + 0x60 * (x))
Vinod Koul71bb8a12017-12-14 11:19:43 +053039
40#define SDW_SHIM_WAKEEN 0x190
41#define SDW_SHIM_WAKESTS 0x192
42
43#define SDW_SHIM_LCTL_SPA BIT(0)
44#define SDW_SHIM_LCTL_CPA BIT(8)
45
46#define SDW_SHIM_SYNC_SYNCPRD_VAL 0x176F
47#define SDW_SHIM_SYNC_SYNCPRD GENMASK(14, 0)
48#define SDW_SHIM_SYNC_SYNCCPU BIT(15)
49#define SDW_SHIM_SYNC_CMDSYNC_MASK GENMASK(19, 16)
50#define SDW_SHIM_SYNC_CMDSYNC BIT(16)
51#define SDW_SHIM_SYNC_SYNCGO BIT(24)
52
53#define SDW_SHIM_PCMSCAP_ISS GENMASK(3, 0)
54#define SDW_SHIM_PCMSCAP_OSS GENMASK(7, 4)
55#define SDW_SHIM_PCMSCAP_BSS GENMASK(12, 8)
56
57#define SDW_SHIM_PCMSYCM_LCHN GENMASK(3, 0)
58#define SDW_SHIM_PCMSYCM_HCHN GENMASK(7, 4)
59#define SDW_SHIM_PCMSYCM_STREAM GENMASK(13, 8)
60#define SDW_SHIM_PCMSYCM_DIR BIT(15)
61
62#define SDW_SHIM_PDMSCAP_ISS GENMASK(3, 0)
63#define SDW_SHIM_PDMSCAP_OSS GENMASK(7, 4)
64#define SDW_SHIM_PDMSCAP_BSS GENMASK(12, 8)
65#define SDW_SHIM_PDMSCAP_CPSS GENMASK(15, 13)
66
67#define SDW_SHIM_IOCTL_MIF BIT(0)
68#define SDW_SHIM_IOCTL_CO BIT(1)
69#define SDW_SHIM_IOCTL_COE BIT(2)
70#define SDW_SHIM_IOCTL_DO BIT(3)
71#define SDW_SHIM_IOCTL_DOE BIT(4)
72#define SDW_SHIM_IOCTL_BKE BIT(5)
73#define SDW_SHIM_IOCTL_WPDD BIT(6)
74#define SDW_SHIM_IOCTL_CIBD BIT(8)
75#define SDW_SHIM_IOCTL_DIBD BIT(9)
76
77#define SDW_SHIM_CTMCTL_DACTQE BIT(0)
78#define SDW_SHIM_CTMCTL_DODS BIT(1)
79#define SDW_SHIM_CTMCTL_DOAIS GENMASK(4, 3)
80
81#define SDW_SHIM_WAKEEN_ENABLE BIT(0)
82#define SDW_SHIM_WAKESTS_STATUS BIT(0)
83
84/* Intel ALH Register definitions */
Pierre-Louis Bossart7cc6e312019-05-01 10:57:39 -050085#define SDW_ALH_STRMZCFG(x) (0x000 + (0x4 * (x)))
Vinod Koul71bb8a12017-12-14 11:19:43 +053086
87#define SDW_ALH_STRMZCFG_DMAT_VAL 0x3
88#define SDW_ALH_STRMZCFG_DMAT GENMASK(7, 0)
89#define SDW_ALH_STRMZCFG_CHN GENMASK(19, 16)
90
Vinod Koulc46302e2018-04-26 18:39:05 +053091enum intel_pdi_type {
92 INTEL_PDI_IN = 0,
93 INTEL_PDI_OUT = 1,
94 INTEL_PDI_BD = 2,
95};
96
Vinod Koul71bb8a12017-12-14 11:19:43 +053097struct sdw_intel {
98 struct sdw_cdns cdns;
99 int instance;
100 struct sdw_intel_link_res *res;
101};
102
103#define cdns_to_intel(_cdns) container_of(_cdns, struct sdw_intel, cdns)
104
105/*
106 * Read, write helpers for HW registers
107 */
108static inline int intel_readl(void __iomem *base, int offset)
109{
110 return readl(base + offset);
111}
112
113static inline void intel_writel(void __iomem *base, int offset, int value)
114{
115 writel(value, base + offset);
116}
117
118static inline u16 intel_readw(void __iomem *base, int offset)
119{
120 return readw(base + offset);
121}
122
123static inline void intel_writew(void __iomem *base, int offset, u16 value)
124{
125 writew(value, base + offset);
126}
127
128static int intel_clear_bit(void __iomem *base, int offset, u32 value, u32 mask)
129{
130 int timeout = 10;
131 u32 reg_read;
132
133 writel(value, base + offset);
134 do {
135 reg_read = readl(base + offset);
136 if (!(reg_read & mask))
137 return 0;
138
139 timeout--;
140 udelay(50);
141 } while (timeout != 0);
142
143 return -EAGAIN;
144}
145
146static int intel_set_bit(void __iomem *base, int offset, u32 value, u32 mask)
147{
148 int timeout = 10;
149 u32 reg_read;
150
151 writel(value, base + offset);
152 do {
153 reg_read = readl(base + offset);
154 if (reg_read & mask)
155 return 0;
156
157 timeout--;
158 udelay(50);
159 } while (timeout != 0);
160
161 return -EAGAIN;
162}
163
164/*
165 * shim ops
166 */
167
168static int intel_link_power_up(struct sdw_intel *sdw)
169{
170 unsigned int link_id = sdw->instance;
171 void __iomem *shim = sdw->res->shim;
172 int spa_mask, cpa_mask;
173 int link_control, ret;
174
175 /* Link power up sequence */
176 link_control = intel_readl(shim, SDW_SHIM_LCTL);
177 spa_mask = (SDW_SHIM_LCTL_SPA << link_id);
178 cpa_mask = (SDW_SHIM_LCTL_CPA << link_id);
179 link_control |= spa_mask;
180
181 ret = intel_set_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
182 if (ret < 0)
183 return ret;
184
185 sdw->cdns.link_up = true;
186 return 0;
187}
188
189static int intel_shim_init(struct sdw_intel *sdw)
190{
191 void __iomem *shim = sdw->res->shim;
192 unsigned int link_id = sdw->instance;
193 int sync_reg, ret;
194 u16 ioctl = 0, act = 0;
195
196 /* Initialize Shim */
197 ioctl |= SDW_SHIM_IOCTL_BKE;
198 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
199
200 ioctl |= SDW_SHIM_IOCTL_WPDD;
201 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
202
203 ioctl |= SDW_SHIM_IOCTL_DO;
204 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
205
206 ioctl |= SDW_SHIM_IOCTL_DOE;
207 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
208
209 /* Switch to MIP from Glue logic */
210 ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
211
212 ioctl &= ~(SDW_SHIM_IOCTL_DOE);
213 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
214
215 ioctl &= ~(SDW_SHIM_IOCTL_DO);
216 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
217
218 ioctl |= (SDW_SHIM_IOCTL_MIF);
219 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
220
221 ioctl &= ~(SDW_SHIM_IOCTL_BKE);
222 ioctl &= ~(SDW_SHIM_IOCTL_COE);
223
224 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
225
226 act |= 0x1 << SDW_REG_SHIFT(SDW_SHIM_CTMCTL_DOAIS);
227 act |= SDW_SHIM_CTMCTL_DACTQE;
228 act |= SDW_SHIM_CTMCTL_DODS;
229 intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act);
230
231 /* Now set SyncPRD period */
232 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
233 sync_reg |= (SDW_SHIM_SYNC_SYNCPRD_VAL <<
234 SDW_REG_SHIFT(SDW_SHIM_SYNC_SYNCPRD));
235
236 /* Set SyncCPU bit */
237 sync_reg |= SDW_SHIM_SYNC_SYNCCPU;
238 ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500239 SDW_SHIM_SYNC_SYNCCPU);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530240 if (ret < 0)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500241 dev_err(sdw->cdns.dev, "Failed to set sync period: %d\n", ret);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530242
243 return ret;
244}
245
Vinod Koul37a2d222018-04-26 18:38:58 +0530246/*
247 * PDI routines
248 */
249static void intel_pdi_init(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500250 struct sdw_cdns_stream_config *config)
Vinod Koul37a2d222018-04-26 18:38:58 +0530251{
252 void __iomem *shim = sdw->res->shim;
253 unsigned int link_id = sdw->instance;
254 int pcm_cap, pdm_cap;
255
256 /* PCM Stream Capability */
257 pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id));
258
259 config->pcm_bd = (pcm_cap & SDW_SHIM_PCMSCAP_BSS) >>
260 SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_BSS);
261 config->pcm_in = (pcm_cap & SDW_SHIM_PCMSCAP_ISS) >>
262 SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_ISS);
263 config->pcm_out = (pcm_cap & SDW_SHIM_PCMSCAP_OSS) >>
264 SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_OSS);
265
Pierre-Louis Bossart121f4362019-05-22 14:47:29 -0500266 dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
267 config->pcm_bd, config->pcm_in, config->pcm_out);
268
Vinod Koul37a2d222018-04-26 18:38:58 +0530269 /* PDM Stream Capability */
270 pdm_cap = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
271
272 config->pdm_bd = (pdm_cap & SDW_SHIM_PDMSCAP_BSS) >>
273 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_BSS);
274 config->pdm_in = (pdm_cap & SDW_SHIM_PDMSCAP_ISS) >>
275 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_ISS);
276 config->pdm_out = (pdm_cap & SDW_SHIM_PDMSCAP_OSS) >>
277 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_OSS);
Pierre-Louis Bossart121f4362019-05-22 14:47:29 -0500278
279 dev_dbg(sdw->cdns.dev, "PDM cap bd:%d in:%d out:%d\n",
280 config->pdm_bd, config->pdm_in, config->pdm_out);
Vinod Koul37a2d222018-04-26 18:38:58 +0530281}
282
283static int
284intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num, bool pcm)
285{
286 void __iomem *shim = sdw->res->shim;
287 unsigned int link_id = sdw->instance;
288 int count;
289
290 if (pcm) {
291 count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num));
292 } else {
293 count = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
294 count = ((count & SDW_SHIM_PDMSCAP_CPSS) >>
295 SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_CPSS));
296 }
297
298 /* zero based values for channel count in register */
299 count++;
300
301 return count;
302}
303
304static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500305 struct sdw_cdns_pdi *pdi,
306 unsigned int num_pdi,
307 unsigned int *num_ch, bool pcm)
Vinod Koul37a2d222018-04-26 18:38:58 +0530308{
309 int i, ch_count = 0;
310
311 for (i = 0; i < num_pdi; i++) {
312 pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num, pcm);
313 ch_count += pdi->ch_count;
314 pdi++;
315 }
316
317 *num_ch = ch_count;
318 return 0;
319}
320
321static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500322 struct sdw_cdns_streams *stream, bool pcm)
Vinod Koul37a2d222018-04-26 18:38:58 +0530323{
324 intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500325 &stream->num_ch_bd, pcm);
Vinod Koul37a2d222018-04-26 18:38:58 +0530326
327 intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500328 &stream->num_ch_in, pcm);
Vinod Koul37a2d222018-04-26 18:38:58 +0530329
330 intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500331 &stream->num_ch_out, pcm);
Vinod Koul37a2d222018-04-26 18:38:58 +0530332
333 return 0;
334}
335
336static int intel_pdi_ch_update(struct sdw_intel *sdw)
337{
338 /* First update PCM streams followed by PDM streams */
339 intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm, true);
340 intel_pdi_stream_ch_update(sdw, &sdw->cdns.pdm, false);
341
342 return 0;
343}
344
345static void
346intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
347{
348 void __iomem *shim = sdw->res->shim;
349 unsigned int link_id = sdw->instance;
350 int pdi_conf = 0;
351
352 pdi->intel_alh_id = (link_id * 16) + pdi->num + 5;
353
354 /*
355 * Program stream parameters to stream SHIM register
356 * This is applicable for PCM stream only.
357 */
358 if (pdi->type != SDW_STREAM_PCM)
359 return;
360
361 if (pdi->dir == SDW_DATA_DIR_RX)
362 pdi_conf |= SDW_SHIM_PCMSYCM_DIR;
363 else
364 pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR);
365
366 pdi_conf |= (pdi->intel_alh_id <<
367 SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_STREAM));
368 pdi_conf |= (pdi->l_ch_num << SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_LCHN));
369 pdi_conf |= (pdi->h_ch_num << SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_HCHN));
370
371 intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
372}
373
374static void
375intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
376{
377 void __iomem *alh = sdw->res->alh;
378 unsigned int link_id = sdw->instance;
379 unsigned int conf;
380
381 pdi->intel_alh_id = (link_id * 16) + pdi->num + 5;
382
383 /* Program Stream config ALH register */
384 conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
385
386 conf |= (SDW_ALH_STRMZCFG_DMAT_VAL <<
387 SDW_REG_SHIFT(SDW_ALH_STRMZCFG_DMAT));
388
389 conf |= ((pdi->ch_count - 1) <<
390 SDW_REG_SHIFT(SDW_ALH_STRMZCFG_CHN));
391
392 intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
393}
394
Vinod Koulc46302e2018-04-26 18:39:05 +0530395static int intel_config_stream(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500396 struct snd_pcm_substream *substream,
397 struct snd_soc_dai *dai,
398 struct snd_pcm_hw_params *hw_params, int link_id)
Vinod Koulc46302e2018-04-26 18:39:05 +0530399{
Pierre-Louis Bossart05c8afe42019-08-05 19:55:06 -0500400 struct sdw_intel_link_res *res = sdw->res;
401
402 if (res->ops && res->ops->config_stream && res->arg)
403 return res->ops->config_stream(res->arg,
Vinod Koulc46302e2018-04-26 18:39:05 +0530404 substream, dai, hw_params, link_id);
405
406 return -EIO;
407}
408
409/*
Shreyas NC30246e22018-07-27 14:44:17 +0530410 * bank switch routines
411 */
412
413static int intel_pre_bank_switch(struct sdw_bus *bus)
414{
415 struct sdw_cdns *cdns = bus_to_cdns(bus);
416 struct sdw_intel *sdw = cdns_to_intel(cdns);
417 void __iomem *shim = sdw->res->shim;
418 int sync_reg;
419
420 /* Write to register only for multi-link */
421 if (!bus->multi_link)
422 return 0;
423
424 /* Read SYNC register */
425 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
426 sync_reg |= SDW_SHIM_SYNC_CMDSYNC << sdw->instance;
427 intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
428
429 return 0;
430}
431
432static int intel_post_bank_switch(struct sdw_bus *bus)
433{
434 struct sdw_cdns *cdns = bus_to_cdns(bus);
435 struct sdw_intel *sdw = cdns_to_intel(cdns);
436 void __iomem *shim = sdw->res->shim;
437 int sync_reg, ret;
438
439 /* Write to register only for multi-link */
440 if (!bus->multi_link)
441 return 0;
442
443 /* Read SYNC register */
444 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
445
446 /*
447 * post_bank_switch() ops is called from the bus in loop for
448 * all the Masters in the steam with the expectation that
449 * we trigger the bankswitch for the only first Master in the list
450 * and do nothing for the other Masters
451 *
452 * So, set the SYNCGO bit only if CMDSYNC bit is set for any Master.
453 */
454 if (!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK))
455 return 0;
456
457 /*
458 * Set SyncGO bit to synchronously trigger a bank switch for
459 * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all
460 * the Masters.
461 */
462 sync_reg |= SDW_SHIM_SYNC_SYNCGO;
463
464 ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500465 SDW_SHIM_SYNC_SYNCGO);
Shreyas NC30246e22018-07-27 14:44:17 +0530466 if (ret < 0)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500467 dev_err(sdw->cdns.dev, "Post bank switch failed: %d\n", ret);
Shreyas NC30246e22018-07-27 14:44:17 +0530468
469 return ret;
470}
471
472/*
Vinod Koulc46302e2018-04-26 18:39:05 +0530473 * DAI routines
474 */
475
476static struct sdw_cdns_port *intel_alloc_port(struct sdw_intel *sdw,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500477 u32 ch, u32 dir, bool pcm)
Vinod Koulc46302e2018-04-26 18:39:05 +0530478{
479 struct sdw_cdns *cdns = &sdw->cdns;
480 struct sdw_cdns_port *port = NULL;
481 int i, ret = 0;
482
483 for (i = 0; i < cdns->num_ports; i++) {
Pierre-Louis Bossart2890a632019-05-01 10:57:40 -0500484 if (cdns->ports[i].assigned)
Vinod Koulc46302e2018-04-26 18:39:05 +0530485 continue;
486
487 port = &cdns->ports[i];
488 port->assigned = true;
489 port->direction = dir;
490 port->ch = ch;
491 break;
492 }
493
494 if (!port) {
495 dev_err(cdns->dev, "Unable to find a free port\n");
496 return NULL;
497 }
498
499 if (pcm) {
500 ret = sdw_cdns_alloc_stream(cdns, &cdns->pcm, port, ch, dir);
501 if (ret)
502 goto out;
503
504 intel_pdi_shim_configure(sdw, port->pdi);
505 sdw_cdns_config_stream(cdns, port, ch, dir, port->pdi);
506
507 intel_pdi_alh_configure(sdw, port->pdi);
508
509 } else {
510 ret = sdw_cdns_alloc_stream(cdns, &cdns->pdm, port, ch, dir);
511 }
512
513out:
514 if (ret) {
515 port->assigned = false;
516 port = NULL;
517 }
518
519 return port;
520}
521
522static void intel_port_cleanup(struct sdw_cdns_dma_data *dma)
523{
524 int i;
525
526 for (i = 0; i < dma->nr_ports; i++) {
527 if (dma->port[i]) {
528 dma->port[i]->pdi->assigned = false;
529 dma->port[i]->pdi = NULL;
530 dma->port[i]->assigned = false;
531 dma->port[i] = NULL;
532 }
533 }
534}
535
536static int intel_hw_params(struct snd_pcm_substream *substream,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500537 struct snd_pcm_hw_params *params,
538 struct snd_soc_dai *dai)
Vinod Koulc46302e2018-04-26 18:39:05 +0530539{
540 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
541 struct sdw_intel *sdw = cdns_to_intel(cdns);
542 struct sdw_cdns_dma_data *dma;
543 struct sdw_stream_config sconfig;
544 struct sdw_port_config *pconfig;
545 int ret, i, ch, dir;
546 bool pcm = true;
547
548 dma = snd_soc_dai_get_dma_data(dai, substream);
549 if (!dma)
550 return -EIO;
551
552 ch = params_channels(params);
553 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
554 dir = SDW_DATA_DIR_RX;
555 else
556 dir = SDW_DATA_DIR_TX;
557
558 if (dma->stream_type == SDW_STREAM_PDM) {
559 /* TODO: Check whether PDM decimator is already in use */
560 dma->nr_ports = sdw_cdns_get_stream(cdns, &cdns->pdm, ch, dir);
561 pcm = false;
562 } else {
563 dma->nr_ports = sdw_cdns_get_stream(cdns, &cdns->pcm, ch, dir);
564 }
565
566 if (!dma->nr_ports) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500567 dev_err(dai->dev, "ports/resources not available\n");
Vinod Koulc46302e2018-04-26 18:39:05 +0530568 return -EINVAL;
569 }
570
571 dma->port = kcalloc(dma->nr_ports, sizeof(*dma->port), GFP_KERNEL);
572 if (!dma->port)
573 return -ENOMEM;
574
575 for (i = 0; i < dma->nr_ports; i++) {
576 dma->port[i] = intel_alloc_port(sdw, ch, dir, pcm);
577 if (!dma->port[i]) {
578 ret = -EINVAL;
579 goto port_error;
580 }
581 }
582
583 /* Inform DSP about PDI stream number */
584 for (i = 0; i < dma->nr_ports; i++) {
585 ret = intel_config_stream(sdw, substream, dai, params,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500586 dma->port[i]->pdi->intel_alh_id);
Vinod Koulc46302e2018-04-26 18:39:05 +0530587 if (ret)
588 goto port_error;
589 }
590
591 sconfig.direction = dir;
592 sconfig.ch_count = ch;
593 sconfig.frame_rate = params_rate(params);
594 sconfig.type = dma->stream_type;
595
596 if (dma->stream_type == SDW_STREAM_PDM) {
597 sconfig.frame_rate *= 50;
598 sconfig.bps = 1;
599 } else {
600 sconfig.bps = snd_pcm_format_width(params_format(params));
601 }
602
603 /* Port configuration */
604 pconfig = kcalloc(dma->nr_ports, sizeof(*pconfig), GFP_KERNEL);
605 if (!pconfig) {
606 ret = -ENOMEM;
607 goto port_error;
608 }
609
610 for (i = 0; i < dma->nr_ports; i++) {
611 pconfig[i].num = dma->port[i]->num;
612 pconfig[i].ch_mask = (1 << ch) - 1;
613 }
614
615 ret = sdw_stream_add_master(&cdns->bus, &sconfig,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500616 pconfig, dma->nr_ports, dma->stream);
Vinod Koulc46302e2018-04-26 18:39:05 +0530617 if (ret) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500618 dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
Vinod Koulc46302e2018-04-26 18:39:05 +0530619 goto stream_error;
620 }
621
622 kfree(pconfig);
623 return ret;
624
625stream_error:
626 kfree(pconfig);
627port_error:
628 intel_port_cleanup(dma);
629 kfree(dma->port);
630 return ret;
631}
632
633static int
634intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
635{
636 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
637 struct sdw_cdns_dma_data *dma;
638 int ret;
639
640 dma = snd_soc_dai_get_dma_data(dai, substream);
641 if (!dma)
642 return -EIO;
643
644 ret = sdw_stream_remove_master(&cdns->bus, dma->stream);
645 if (ret < 0)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500646 dev_err(dai->dev, "remove master from stream %s failed: %d\n",
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500647 dma->stream->name, ret);
Vinod Koulc46302e2018-04-26 18:39:05 +0530648
649 intel_port_cleanup(dma);
650 kfree(dma->port);
651 return ret;
652}
653
654static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500655 void *stream, int direction)
Vinod Koulc46302e2018-04-26 18:39:05 +0530656{
657 return cdns_set_sdw_stream(dai, stream, true, direction);
658}
659
660static int intel_pdm_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500661 void *stream, int direction)
Vinod Koulc46302e2018-04-26 18:39:05 +0530662{
663 return cdns_set_sdw_stream(dai, stream, false, direction);
664}
665
Julia Lawallb1635592018-10-27 15:34:42 +0200666static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
Vinod Koulc46302e2018-04-26 18:39:05 +0530667 .hw_params = intel_hw_params,
668 .hw_free = intel_hw_free,
669 .shutdown = sdw_cdns_shutdown,
670 .set_sdw_stream = intel_pcm_set_sdw_stream,
671};
672
Julia Lawallb1635592018-10-27 15:34:42 +0200673static const struct snd_soc_dai_ops intel_pdm_dai_ops = {
Vinod Koulc46302e2018-04-26 18:39:05 +0530674 .hw_params = intel_hw_params,
675 .hw_free = intel_hw_free,
676 .shutdown = sdw_cdns_shutdown,
677 .set_sdw_stream = intel_pdm_set_sdw_stream,
678};
679
680static const struct snd_soc_component_driver dai_component = {
681 .name = "soundwire",
682};
683
684static int intel_create_dai(struct sdw_cdns *cdns,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500685 struct snd_soc_dai_driver *dais,
686 enum intel_pdi_type type,
687 u32 num, u32 off, u32 max_ch, bool pcm)
Vinod Koulc46302e2018-04-26 18:39:05 +0530688{
689 int i;
690
691 if (num == 0)
692 return 0;
693
694 /* TODO: Read supported rates/formats from hardware */
695 for (i = off; i < (off + num); i++) {
696 dais[i].name = kasprintf(GFP_KERNEL, "SDW%d Pin%d",
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500697 cdns->instance, i);
Vinod Koulc46302e2018-04-26 18:39:05 +0530698 if (!dais[i].name)
699 return -ENOMEM;
700
701 if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
Vinod Koul1215dae2019-05-02 16:29:25 +0530702 dais[i].playback.stream_name =
703 kasprintf(GFP_KERNEL, "SDW%d Tx%d",
704 cdns->instance, i);
Vinod Koulc46302e2018-04-26 18:39:05 +0530705 if (!dais[i].playback.stream_name) {
706 kfree(dais[i].name);
707 return -ENOMEM;
708 }
709
710 dais[i].playback.channels_min = 1;
711 dais[i].playback.channels_max = max_ch;
712 dais[i].playback.rates = SNDRV_PCM_RATE_48000;
713 dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
714 }
715
716 if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
Vinod Koul1215dae2019-05-02 16:29:25 +0530717 dais[i].capture.stream_name =
718 kasprintf(GFP_KERNEL, "SDW%d Rx%d",
719 cdns->instance, i);
Vinod Koulc46302e2018-04-26 18:39:05 +0530720 if (!dais[i].capture.stream_name) {
721 kfree(dais[i].name);
722 kfree(dais[i].playback.stream_name);
723 return -ENOMEM;
724 }
725
Srinivas Kandagatla39194122019-06-06 12:23:04 +0100726 dais[i].capture.channels_min = 1;
727 dais[i].capture.channels_max = max_ch;
Vinod Koulc46302e2018-04-26 18:39:05 +0530728 dais[i].capture.rates = SNDRV_PCM_RATE_48000;
729 dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
730 }
731
732 dais[i].id = SDW_DAI_ID_RANGE_START + i;
733
734 if (pcm)
735 dais[i].ops = &intel_pcm_dai_ops;
736 else
737 dais[i].ops = &intel_pdm_dai_ops;
738 }
739
740 return 0;
741}
742
743static int intel_register_dai(struct sdw_intel *sdw)
744{
745 struct sdw_cdns *cdns = &sdw->cdns;
746 struct sdw_cdns_streams *stream;
747 struct snd_soc_dai_driver *dais;
748 int num_dai, ret, off = 0;
749
750 /* DAIs are created based on total number of PDIs supported */
751 num_dai = cdns->pcm.num_pdi + cdns->pdm.num_pdi;
752
753 dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
754 if (!dais)
755 return -ENOMEM;
756
757 /* Create PCM DAIs */
758 stream = &cdns->pcm;
759
Vinod Koul1215dae2019-05-02 16:29:25 +0530760 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, stream->num_in,
761 off, stream->num_ch_in, true);
Vinod Koulc46302e2018-04-26 18:39:05 +0530762 if (ret)
763 return ret;
764
765 off += cdns->pcm.num_in;
Vinod Koul1215dae2019-05-02 16:29:25 +0530766 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
767 off, stream->num_ch_out, true);
Vinod Koulc46302e2018-04-26 18:39:05 +0530768 if (ret)
769 return ret;
770
771 off += cdns->pcm.num_out;
Vinod Koul1215dae2019-05-02 16:29:25 +0530772 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
773 off, stream->num_ch_bd, true);
Vinod Koulc46302e2018-04-26 18:39:05 +0530774 if (ret)
775 return ret;
776
777 /* Create PDM DAIs */
778 stream = &cdns->pdm;
779 off += cdns->pcm.num_bd;
Vinod Koul1215dae2019-05-02 16:29:25 +0530780 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pdm.num_in,
781 off, stream->num_ch_in, false);
Vinod Koulc46302e2018-04-26 18:39:05 +0530782 if (ret)
783 return ret;
784
785 off += cdns->pdm.num_in;
Vinod Koul1215dae2019-05-02 16:29:25 +0530786 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pdm.num_out,
787 off, stream->num_ch_out, false);
Vinod Koulc46302e2018-04-26 18:39:05 +0530788 if (ret)
789 return ret;
790
791 off += cdns->pdm.num_bd;
Vinod Koul1215dae2019-05-02 16:29:25 +0530792 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pdm.num_bd,
793 off, stream->num_ch_bd, false);
Vinod Koulc46302e2018-04-26 18:39:05 +0530794 if (ret)
795 return ret;
796
797 return snd_soc_register_component(cdns->dev, &dai_component,
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500798 dais, num_dai);
Vinod Koulc46302e2018-04-26 18:39:05 +0530799}
800
Vinod Koul71bb8a12017-12-14 11:19:43 +0530801static int intel_prop_read(struct sdw_bus *bus)
802{
803 /* Initialize with default handler to read all DisCo properties */
804 sdw_master_read_prop(bus);
805
Vinod Koul71bb8a12017-12-14 11:19:43 +0530806 return 0;
807}
808
Shreyas NCc91605f2018-04-26 18:38:43 +0530809static struct sdw_master_ops sdw_intel_ops = {
810 .read_prop = sdw_master_read_prop,
811 .xfer_msg = cdns_xfer_msg,
812 .xfer_msg_defer = cdns_xfer_msg_defer,
813 .reset_page_addr = cdns_reset_page_addr,
Vinod Koul07abeff2018-04-26 18:38:48 +0530814 .set_bus_conf = cdns_bus_conf,
Shreyas NC30246e22018-07-27 14:44:17 +0530815 .pre_bank_switch = intel_pre_bank_switch,
816 .post_bank_switch = intel_post_bank_switch,
Shreyas NCc91605f2018-04-26 18:38:43 +0530817};
818
Vinod Koul71bb8a12017-12-14 11:19:43 +0530819/*
820 * probe and init
821 */
822static int intel_probe(struct platform_device *pdev)
823{
Vinod Koul37a2d222018-04-26 18:38:58 +0530824 struct sdw_cdns_stream_config config;
Vinod Koul71bb8a12017-12-14 11:19:43 +0530825 struct sdw_intel *sdw;
826 int ret;
827
828 sdw = devm_kzalloc(&pdev->dev, sizeof(*sdw), GFP_KERNEL);
829 if (!sdw)
830 return -ENOMEM;
831
832 sdw->instance = pdev->id;
833 sdw->res = dev_get_platdata(&pdev->dev);
834 sdw->cdns.dev = &pdev->dev;
835 sdw->cdns.registers = sdw->res->registers;
836 sdw->cdns.instance = sdw->instance;
837 sdw->cdns.msg_count = 0;
838 sdw->cdns.bus.dev = &pdev->dev;
839 sdw->cdns.bus.link_id = pdev->id;
840
841 sdw_cdns_probe(&sdw->cdns);
842
843 /* Set property read ops */
Shreyas NCc91605f2018-04-26 18:38:43 +0530844 sdw_intel_ops.read_prop = intel_prop_read;
845 sdw->cdns.bus.ops = &sdw_intel_ops;
Vinod Koul71bb8a12017-12-14 11:19:43 +0530846
847 platform_set_drvdata(pdev, sdw);
848
849 ret = sdw_add_bus_master(&sdw->cdns.bus);
850 if (ret) {
851 dev_err(&pdev->dev, "sdw_add_bus_master fail: %d\n", ret);
852 goto err_master_reg;
853 }
854
855 /* Initialize shim and controller */
856 intel_link_power_up(sdw);
857 intel_shim_init(sdw);
858
859 ret = sdw_cdns_init(&sdw->cdns);
860 if (ret)
861 goto err_init;
862
Colin Ian King7094dc22018-01-08 22:22:43 +0530863 ret = sdw_cdns_enable_interrupt(&sdw->cdns);
Vinod Koul37a2d222018-04-26 18:38:58 +0530864
865 /* Read the PDI config and initialize cadence PDI */
866 intel_pdi_init(sdw, &config);
867 ret = sdw_cdns_pdi_init(&sdw->cdns, config);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530868 if (ret)
869 goto err_init;
870
Vinod Koul37a2d222018-04-26 18:38:58 +0530871 intel_pdi_ch_update(sdw);
872
Vinod Koul71bb8a12017-12-14 11:19:43 +0530873 /* Acquire IRQ */
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500874 ret = request_threaded_irq(sdw->res->irq, sdw_cdns_irq, sdw_cdns_thread,
875 IRQF_SHARED, KBUILD_MODNAME, &sdw->cdns);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530876 if (ret < 0) {
877 dev_err(sdw->cdns.dev, "unable to grab IRQ %d, disabling device\n",
Pierre-Louis Bossartd542bc92019-05-01 10:57:38 -0500878 sdw->res->irq);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530879 goto err_init;
880 }
881
Vinod Koulc46302e2018-04-26 18:39:05 +0530882 /* Register DAIs */
883 ret = intel_register_dai(sdw);
884 if (ret) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500885 dev_err(sdw->cdns.dev, "DAI registration failed: %d\n", ret);
Vinod Koulc46302e2018-04-26 18:39:05 +0530886 snd_soc_unregister_component(sdw->cdns.dev);
887 goto err_dai;
888 }
889
Vinod Koul71bb8a12017-12-14 11:19:43 +0530890 return 0;
891
Vinod Koulc46302e2018-04-26 18:39:05 +0530892err_dai:
893 free_irq(sdw->res->irq, sdw);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530894err_init:
895 sdw_delete_bus_master(&sdw->cdns.bus);
896err_master_reg:
897 return ret;
898}
899
900static int intel_remove(struct platform_device *pdev)
901{
902 struct sdw_intel *sdw;
903
904 sdw = platform_get_drvdata(pdev);
905
906 free_irq(sdw->res->irq, sdw);
Vinod Koulc46302e2018-04-26 18:39:05 +0530907 snd_soc_unregister_component(sdw->cdns.dev);
Vinod Koul71bb8a12017-12-14 11:19:43 +0530908 sdw_delete_bus_master(&sdw->cdns.bus);
909
910 return 0;
911}
912
913static struct platform_driver sdw_intel_drv = {
914 .probe = intel_probe,
915 .remove = intel_remove,
916 .driver = {
917 .name = "int-sdw",
918
919 },
920};
921
922module_platform_driver(sdw_intel_drv);
923
924MODULE_LICENSE("Dual BSD/GPL");
925MODULE_ALIAS("platform:int-sdw");
926MODULE_DESCRIPTION("Intel Soundwire Master Driver");