1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Support for Digigram Lola PCI-e boards
5 * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/pci.h>
12 #include <linux/delay.h>
13 #include <sound/core.h>
14 #include <sound/pcm.h>
17 #define LOLA_MAX_BDL_ENTRIES 8
18 #define LOLA_MAX_BUF_SIZE (1024*1024*1024)
19 #define LOLA_BDL_ENTRY_SIZE (16 * 16)
21 static struct lola_pcm
*lola_get_pcm(struct snd_pcm_substream
*substream
)
23 struct lola
*chip
= snd_pcm_substream_chip(substream
);
24 return &chip
->pcm
[substream
->stream
];
27 static struct lola_stream
*lola_get_stream(struct snd_pcm_substream
*substream
)
29 struct lola_pcm
*pcm
= lola_get_pcm(substream
);
30 unsigned int idx
= substream
->number
;
31 return &pcm
->streams
[idx
];
34 static unsigned int lola_get_lrc(struct lola
*chip
)
36 return lola_readl(chip
, BAR1
, LRC
);
39 static unsigned int lola_get_tstamp(struct lola
*chip
, bool quick_no_sync
)
41 unsigned int tstamp
= lola_get_lrc(chip
) >> 8;
42 if (chip
->granularity
) {
43 unsigned int wait_banks
= quick_no_sync
? 0 : 8;
44 tstamp
+= (wait_banks
+ 1) * chip
->granularity
- 1;
45 tstamp
-= tstamp
% chip
->granularity
;
50 /* clear any pending interrupt status */
51 static void lola_stream_clear_pending_irq(struct lola
*chip
,
52 struct lola_stream
*str
)
54 unsigned int val
= lola_dsd_read(chip
, str
->dsd
, STS
);
55 val
&= LOLA_DSD_STS_DESE
| LOLA_DSD_STS_BCIS
;
57 lola_dsd_write(chip
, str
->dsd
, STS
, val
);
60 static void lola_stream_start(struct lola
*chip
, struct lola_stream
*str
,
63 lola_stream_clear_pending_irq(chip
, str
);
64 lola_dsd_write(chip
, str
->dsd
, CTL
,
72 static void lola_stream_stop(struct lola
*chip
, struct lola_stream
*str
,
75 lola_dsd_write(chip
, str
->dsd
, CTL
,
80 lola_stream_clear_pending_irq(chip
, str
);
83 static void wait_for_srst_clear(struct lola
*chip
, struct lola_stream
*str
)
85 unsigned long end_time
= jiffies
+ msecs_to_jiffies(200);
86 while (time_before(jiffies
, end_time
)) {
88 val
= lola_dsd_read(chip
, str
->dsd
, CTL
);
89 if (!(val
& LOLA_DSD_CTL_SRST
))
93 dev_warn(chip
->card
->dev
, "SRST not clear (stream %d)\n", str
->dsd
);
96 static int lola_stream_wait_for_fifo(struct lola
*chip
,
97 struct lola_stream
*str
,
100 unsigned int val
= ready
? LOLA_DSD_STS_FIFORDY
: 0;
101 unsigned long end_time
= jiffies
+ msecs_to_jiffies(200);
102 while (time_before(jiffies
, end_time
)) {
103 unsigned int reg
= lola_dsd_read(chip
, str
->dsd
, STS
);
104 if ((reg
& LOLA_DSD_STS_FIFORDY
) == val
)
108 dev_warn(chip
->card
->dev
, "FIFO not ready (stream %d)\n", str
->dsd
);
112 /* sync for FIFO ready/empty for all linked streams;
113 * clear paused flag when FIFO gets ready again
115 static int lola_sync_wait_for_fifo(struct lola
*chip
,
116 struct snd_pcm_substream
*substream
,
119 unsigned int val
= ready
? LOLA_DSD_STS_FIFORDY
: 0;
120 unsigned long end_time
= jiffies
+ msecs_to_jiffies(200);
121 struct snd_pcm_substream
*s
;
124 while (time_before(jiffies
, end_time
)) {
126 snd_pcm_group_for_each_entry(s
, substream
) {
127 struct lola_stream
*str
;
128 if (s
->pcm
->card
!= substream
->pcm
->card
)
130 str
= lola_get_stream(s
);
131 if (str
->prepared
&& str
->paused
) {
133 reg
= lola_dsd_read(chip
, str
->dsd
, STS
);
134 if ((reg
& LOLA_DSD_STS_FIFORDY
) != val
) {
135 pending
= str
->dsd
+ 1;
146 dev_warn(chip
->card
->dev
, "FIFO not ready (pending %d)\n", pending
- 1);
150 /* finish pause - prepare for a new resume */
151 static void lola_sync_pause(struct lola
*chip
,
152 struct snd_pcm_substream
*substream
)
154 struct snd_pcm_substream
*s
;
156 lola_sync_wait_for_fifo(chip
, substream
, false);
157 snd_pcm_group_for_each_entry(s
, substream
) {
158 struct lola_stream
*str
;
159 if (s
->pcm
->card
!= substream
->pcm
->card
)
161 str
= lola_get_stream(s
);
162 if (str
->paused
&& str
->prepared
)
163 lola_dsd_write(chip
, str
->dsd
, CTL
, LOLA_DSD_CTL_SRUN
|
164 LOLA_DSD_CTL_IOCE
| LOLA_DSD_CTL_DEIE
);
166 lola_sync_wait_for_fifo(chip
, substream
, true);
169 static void lola_stream_reset(struct lola
*chip
, struct lola_stream
*str
)
173 lola_sync_pause(chip
, str
->substream
);
175 lola_dsd_write(chip
, str
->dsd
, CTL
,
176 LOLA_DSD_CTL_IOCE
| LOLA_DSD_CTL_DEIE
);
177 lola_stream_wait_for_fifo(chip
, str
, false);
178 lola_stream_clear_pending_irq(chip
, str
);
179 lola_dsd_write(chip
, str
->dsd
, CTL
, LOLA_DSD_CTL_SRST
);
180 lola_dsd_write(chip
, str
->dsd
, LVI
, 0);
181 lola_dsd_write(chip
, str
->dsd
, BDPU
, 0);
182 lola_dsd_write(chip
, str
->dsd
, BDPL
, 0);
183 wait_for_srst_clear(chip
, str
);
187 static const struct snd_pcm_hardware lola_pcm_hw
= {
188 .info
= (SNDRV_PCM_INFO_MMAP
|
189 SNDRV_PCM_INFO_INTERLEAVED
|
190 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
191 SNDRV_PCM_INFO_MMAP_VALID
|
192 SNDRV_PCM_INFO_PAUSE
),
193 .formats
= (SNDRV_PCM_FMTBIT_S16_LE
|
194 SNDRV_PCM_FMTBIT_S24_LE
|
195 SNDRV_PCM_FMTBIT_S32_LE
|
196 SNDRV_PCM_FMTBIT_FLOAT_LE
),
197 .rates
= SNDRV_PCM_RATE_8000_192000
,
202 .buffer_bytes_max
= LOLA_MAX_BUF_SIZE
,
203 .period_bytes_min
= 128,
204 .period_bytes_max
= LOLA_MAX_BUF_SIZE
/ 2,
206 .periods_max
= LOLA_MAX_BDL_ENTRIES
,
210 static int lola_pcm_open(struct snd_pcm_substream
*substream
)
212 struct lola
*chip
= snd_pcm_substream_chip(substream
);
213 struct lola_pcm
*pcm
= lola_get_pcm(substream
);
214 struct lola_stream
*str
= lola_get_stream(substream
);
215 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
217 mutex_lock(&chip
->open_mutex
);
219 mutex_unlock(&chip
->open_mutex
);
222 str
->substream
= substream
;
225 runtime
->hw
= lola_pcm_hw
;
226 runtime
->hw
.channels_max
= pcm
->num_streams
- str
->index
;
227 if (chip
->sample_rate
) {
228 /* sample rate is locked */
229 runtime
->hw
.rate_min
= chip
->sample_rate
;
230 runtime
->hw
.rate_max
= chip
->sample_rate
;
232 runtime
->hw
.rate_min
= chip
->sample_rate_min
;
233 runtime
->hw
.rate_max
= chip
->sample_rate_max
;
235 chip
->ref_count_rate
++;
236 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
237 /* period size = multiple of chip->granularity (8, 16 or 32 frames)*/
238 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
240 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
242 mutex_unlock(&chip
->open_mutex
);
246 static void lola_cleanup_slave_streams(struct lola_pcm
*pcm
,
247 struct lola_stream
*str
)
250 for (i
= str
->index
+ 1; i
< pcm
->num_streams
; i
++) {
251 struct lola_stream
*s
= &pcm
->streams
[i
];
252 if (s
->master
!= str
)
259 static int lola_pcm_close(struct snd_pcm_substream
*substream
)
261 struct lola
*chip
= snd_pcm_substream_chip(substream
);
262 struct lola_stream
*str
= lola_get_stream(substream
);
264 mutex_lock(&chip
->open_mutex
);
265 if (str
->substream
== substream
) {
266 str
->substream
= NULL
;
269 if (--chip
->ref_count_rate
== 0) {
270 /* release sample rate */
271 chip
->sample_rate
= 0;
273 mutex_unlock(&chip
->open_mutex
);
277 static int lola_pcm_hw_params(struct snd_pcm_substream
*substream
,
278 struct snd_pcm_hw_params
*hw_params
)
280 struct lola_stream
*str
= lola_get_stream(substream
);
283 str
->period_bytes
= 0;
284 str
->format_verb
= 0;
288 static int lola_pcm_hw_free(struct snd_pcm_substream
*substream
)
290 struct lola
*chip
= snd_pcm_substream_chip(substream
);
291 struct lola_pcm
*pcm
= lola_get_pcm(substream
);
292 struct lola_stream
*str
= lola_get_stream(substream
);
294 mutex_lock(&chip
->open_mutex
);
295 lola_stream_reset(chip
, str
);
296 lola_cleanup_slave_streams(pcm
, str
);
297 mutex_unlock(&chip
->open_mutex
);
304 static int setup_bdle(struct snd_pcm_substream
*substream
,
305 struct lola_stream
*str
, __le32
**bdlp
,
314 if (str
->frags
>= LOLA_MAX_BDL_ENTRIES
)
317 addr
= snd_pcm_sgbuf_get_addr(substream
, ofs
);
318 /* program the address field of the BDL entry */
319 bdl
[0] = cpu_to_le32((u32
)addr
);
320 bdl
[1] = cpu_to_le32(upper_32_bits(addr
));
321 /* program the size field of the BDL entry */
322 chunk
= snd_pcm_sgbuf_get_chunk_size(substream
, ofs
, size
);
323 bdl
[2] = cpu_to_le32(chunk
);
324 /* program the IOC to enable interrupt
325 * only when the whole fragment is processed
328 bdl
[3] = size
? 0 : cpu_to_le32(0x01);
340 static int lola_setup_periods(struct lola
*chip
, struct lola_pcm
*pcm
,
341 struct snd_pcm_substream
*substream
,
342 struct lola_stream
*str
)
345 int i
, ofs
, periods
, period_bytes
;
347 period_bytes
= str
->period_bytes
;
348 periods
= str
->bufsize
/ period_bytes
;
350 /* program the initial BDL entries */
351 bdl
= (__le32
*)(pcm
->bdl
->area
+ LOLA_BDL_ENTRY_SIZE
* str
->index
);
354 for (i
= 0; i
< periods
; i
++) {
355 ofs
= setup_bdle(substream
, str
, &bdl
, ofs
, period_bytes
);
362 dev_err(chip
->card
->dev
, "Too many BDL entries: buffer=%d, period=%d\n",
363 str
->bufsize
, period_bytes
);
367 static unsigned int lola_get_format_verb(struct snd_pcm_substream
*substream
)
371 switch (substream
->runtime
->format
) {
372 case SNDRV_PCM_FORMAT_S16_LE
:
375 case SNDRV_PCM_FORMAT_S24_LE
:
378 case SNDRV_PCM_FORMAT_S32_LE
:
381 case SNDRV_PCM_FORMAT_FLOAT_LE
:
387 verb
|= substream
->runtime
->channels
;
391 static int lola_set_stream_config(struct lola
*chip
,
392 struct lola_stream
*str
,
396 unsigned int verb
, val
;
398 /* set format info for all channels
399 * (with only one command for the first channel)
401 err
= lola_codec_read(chip
, str
->nid
, LOLA_VERB_SET_STREAM_FORMAT
,
402 str
->format_verb
, 0, &val
, NULL
);
404 dev_err(chip
->card
->dev
, "Cannot set stream format 0x%x\n",
409 /* update stream - channel config */
410 for (i
= 0; i
< channels
; i
++) {
411 verb
= (str
->index
<< 6) | i
;
412 err
= lola_codec_read(chip
, str
[i
].nid
,
413 LOLA_VERB_SET_CHANNEL_STREAMID
, 0, verb
,
416 dev_err(chip
->card
->dev
,
417 "Cannot set stream channel %d\n", i
);
425 * set up the SD for streaming
427 static int lola_setup_controller(struct lola
*chip
, struct lola_pcm
*pcm
,
428 struct lola_stream
*str
)
436 bdl
= pcm
->bdl
->addr
+ LOLA_BDL_ENTRY_SIZE
* str
->index
;
437 lola_dsd_write(chip
, str
->dsd
, BDPL
, (u32
)bdl
);
438 lola_dsd_write(chip
, str
->dsd
, BDPU
, upper_32_bits(bdl
));
439 /* program the stream LVI (last valid index) of the BDL */
440 lola_dsd_write(chip
, str
->dsd
, LVI
, str
->frags
- 1);
441 lola_stream_clear_pending_irq(chip
, str
);
443 lola_dsd_write(chip
, str
->dsd
, CTL
,
444 LOLA_DSD_CTL_IOCE
| LOLA_DSD_CTL_DEIE
| LOLA_DSD_CTL_SRUN
);
448 return lola_stream_wait_for_fifo(chip
, str
, true);
451 static int lola_pcm_prepare(struct snd_pcm_substream
*substream
)
453 struct lola
*chip
= snd_pcm_substream_chip(substream
);
454 struct lola_pcm
*pcm
= lola_get_pcm(substream
);
455 struct lola_stream
*str
= lola_get_stream(substream
);
456 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
457 unsigned int bufsize
, period_bytes
, format_verb
;
460 mutex_lock(&chip
->open_mutex
);
461 lola_stream_reset(chip
, str
);
462 lola_cleanup_slave_streams(pcm
, str
);
463 if (str
->index
+ runtime
->channels
> pcm
->num_streams
) {
464 mutex_unlock(&chip
->open_mutex
);
467 for (i
= 1; i
< runtime
->channels
; i
++) {
471 mutex_unlock(&chip
->open_mutex
);
473 bufsize
= snd_pcm_lib_buffer_bytes(substream
);
474 period_bytes
= snd_pcm_lib_period_bytes(substream
);
475 format_verb
= lola_get_format_verb(substream
);
477 str
->bufsize
= bufsize
;
478 str
->period_bytes
= period_bytes
;
479 str
->format_verb
= format_verb
;
481 err
= lola_setup_periods(chip
, pcm
, substream
, str
);
485 err
= lola_set_sample_rate(chip
, runtime
->rate
);
488 chip
->sample_rate
= runtime
->rate
; /* sample rate gets locked */
490 err
= lola_set_stream_config(chip
, str
, runtime
->channels
);
494 err
= lola_setup_controller(chip
, pcm
, str
);
496 lola_stream_reset(chip
, str
);
503 static int lola_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
505 struct lola
*chip
= snd_pcm_substream_chip(substream
);
506 struct lola_stream
*str
;
507 struct snd_pcm_substream
*s
;
513 case SNDRV_PCM_TRIGGER_START
:
514 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
515 case SNDRV_PCM_TRIGGER_RESUME
:
518 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
519 case SNDRV_PCM_TRIGGER_SUSPEND
:
520 case SNDRV_PCM_TRIGGER_STOP
:
528 * sample correct synchronization is only needed starting several
529 * streams. On stop or if only one stream do as quick as possible
531 sync_streams
= (start
&& snd_pcm_stream_linked(substream
));
532 tstamp
= lola_get_tstamp(chip
, !sync_streams
);
533 spin_lock(&chip
->reg_lock
);
534 snd_pcm_group_for_each_entry(s
, substream
) {
535 if (s
->pcm
->card
!= substream
->pcm
->card
)
537 str
= lola_get_stream(s
);
539 lola_stream_start(chip
, str
, tstamp
);
541 lola_stream_stop(chip
, str
, tstamp
);
542 str
->running
= start
;
543 str
->paused
= !start
;
544 snd_pcm_trigger_done(s
, substream
);
546 spin_unlock(&chip
->reg_lock
);
550 static snd_pcm_uframes_t
lola_pcm_pointer(struct snd_pcm_substream
*substream
)
552 struct lola
*chip
= snd_pcm_substream_chip(substream
);
553 struct lola_stream
*str
= lola_get_stream(substream
);
554 unsigned int pos
= lola_dsd_read(chip
, str
->dsd
, LPIB
);
556 if (pos
>= str
->bufsize
)
558 return bytes_to_frames(substream
->runtime
, pos
);
561 void lola_pcm_update(struct lola
*chip
, struct lola_pcm
*pcm
, unsigned int bits
)
564 u8 num_streams
= min_t(u8
, pcm
->num_streams
, ARRAY_SIZE(pcm
->streams
));
566 for (i
= 0; bits
&& i
< num_streams
; i
++) {
567 if (bits
& (1 << i
)) {
568 struct lola_stream
*str
= &pcm
->streams
[i
];
569 if (str
->substream
&& str
->running
)
570 snd_pcm_period_elapsed(str
->substream
);
576 static const struct snd_pcm_ops lola_pcm_ops
= {
577 .open
= lola_pcm_open
,
578 .close
= lola_pcm_close
,
579 .hw_params
= lola_pcm_hw_params
,
580 .hw_free
= lola_pcm_hw_free
,
581 .prepare
= lola_pcm_prepare
,
582 .trigger
= lola_pcm_trigger
,
583 .pointer
= lola_pcm_pointer
,
586 int lola_create_pcm(struct lola
*chip
)
591 for (i
= 0; i
< 2; i
++) {
593 snd_devm_alloc_pages(&chip
->pci
->dev
, SNDRV_DMA_TYPE_DEV
,
595 if (!chip
->pcm
[i
].bdl
)
599 err
= snd_pcm_new(chip
->card
, "Digigram Lola", 0,
600 chip
->pcm
[SNDRV_PCM_STREAM_PLAYBACK
].num_streams
,
601 chip
->pcm
[SNDRV_PCM_STREAM_CAPTURE
].num_streams
,
605 strscpy(pcm
->name
, "Digigram Lola", sizeof(pcm
->name
));
606 pcm
->private_data
= chip
;
607 for (i
= 0; i
< 2; i
++) {
608 if (chip
->pcm
[i
].num_streams
)
609 snd_pcm_set_ops(pcm
, i
, &lola_pcm_ops
);
611 /* buffer pre-allocation */
612 snd_pcm_set_managed_buffer_all(pcm
, SNDRV_DMA_TYPE_DEV_SG
,
614 1024 * 64, 32 * 1024 * 1024);
621 static int lola_init_stream(struct lola
*chip
, struct lola_stream
*str
,
622 int idx
, int nid
, int dir
)
631 str
->dsd
+= MAX_STREAM_IN_COUNT
;
632 err
= lola_read_param(chip
, nid
, LOLA_PAR_AUDIO_WIDGET_CAP
, &val
);
634 dev_err(chip
->card
->dev
, "Can't read wcaps for 0x%x\n", nid
);
638 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1) */
639 if ((val
& 0x00f00dff) != 0x00000010) {
640 dev_err(chip
->card
->dev
,
641 "Invalid wcaps 0x%x for 0x%x\n",
646 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1)
647 * (bug : ignore bit8: Conn list = 0/1)
649 if ((val
& 0x00f00cff) != 0x00100010) {
650 dev_err(chip
->card
->dev
,
651 "Invalid wcaps 0x%x for 0x%x\n",
655 /* test bit9:DIGITAL and bit12:SRC_PRESENT*/
656 if ((val
& 0x00001200) == 0x00001200)
657 chip
->input_src_caps_mask
|= (1 << idx
);
660 err
= lola_read_param(chip
, nid
, LOLA_PAR_STREAM_FORMATS
, &val
);
662 dev_err(chip
->card
->dev
, "Can't read FORMATS 0x%x\n", nid
);
667 str
->can_float
= true;
669 dev_err(chip
->card
->dev
,
670 "Invalid formats 0x%x for 0x%x", val
, nid
);
676 int lola_init_pcm(struct lola
*chip
, int dir
, int *nidp
)
678 struct lola_pcm
*pcm
= &chip
->pcm
[dir
];
682 for (i
= 0; i
< pcm
->num_streams
; i
++, nid
++) {
683 err
= lola_init_stream(chip
, &pcm
->streams
[i
], i
, nid
, dir
);