2 * Support for Digigram Lola PCI-e boards
4 * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59
18 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/pci.h>
25 #include <linux/delay.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
30 #define LOLA_MAX_BDL_ENTRIES 8
31 #define LOLA_MAX_BUF_SIZE (1024*1024*1024)
32 #define LOLA_BDL_ENTRY_SIZE (16 * 16)
34 static struct lola_pcm
*lola_get_pcm(struct snd_pcm_substream
*substream
)
36 struct lola
*chip
= snd_pcm_substream_chip(substream
);
37 return &chip
->pcm
[substream
->stream
];
40 static struct lola_stream
*lola_get_stream(struct snd_pcm_substream
*substream
)
42 struct lola_pcm
*pcm
= lola_get_pcm(substream
);
43 unsigned int idx
= substream
->number
;
44 return &pcm
->streams
[idx
];
47 static unsigned int lola_get_lrc(struct lola
*chip
)
49 return lola_readl(chip
, BAR1
, LRC
);
52 static unsigned int lola_get_tstamp(struct lola
*chip
, bool quick_no_sync
)
54 unsigned int tstamp
= lola_get_lrc(chip
) >> 8;
55 if (chip
->granularity
) {
56 unsigned int wait_banks
= quick_no_sync
? 0 : 8;
57 tstamp
+= (wait_banks
+ 1) * chip
->granularity
- 1;
58 tstamp
-= tstamp
% chip
->granularity
;
63 /* clear any pending interrupt status */
64 static void lola_stream_clear_pending_irq(struct lola
*chip
,
65 struct lola_stream
*str
)
67 unsigned int val
= lola_dsd_read(chip
, str
->dsd
, STS
);
68 val
&= LOLA_DSD_STS_DESE
| LOLA_DSD_STS_BCIS
;
70 lola_dsd_write(chip
, str
->dsd
, STS
, val
);
73 static void lola_stream_start(struct lola
*chip
, struct lola_stream
*str
,
76 lola_stream_clear_pending_irq(chip
, str
);
77 lola_dsd_write(chip
, str
->dsd
, CTL
,
85 static void lola_stream_stop(struct lola
*chip
, struct lola_stream
*str
,
88 lola_dsd_write(chip
, str
->dsd
, CTL
,
93 lola_stream_clear_pending_irq(chip
, str
);
96 static void wait_for_srst_clear(struct lola
*chip
, struct lola_stream
*str
)
98 unsigned long end_time
= jiffies
+ msecs_to_jiffies(200);
99 while (time_before(jiffies
, end_time
)) {
101 val
= lola_dsd_read(chip
, str
->dsd
, CTL
);
102 if (!(val
& LOLA_DSD_CTL_SRST
))
106 dev_warn(chip
->card
->dev
, "SRST not clear (stream %d)\n", str
->dsd
);
109 static int lola_stream_wait_for_fifo(struct lola
*chip
,
110 struct lola_stream
*str
,
113 unsigned int val
= ready
? LOLA_DSD_STS_FIFORDY
: 0;
114 unsigned long end_time
= jiffies
+ msecs_to_jiffies(200);
115 while (time_before(jiffies
, end_time
)) {
116 unsigned int reg
= lola_dsd_read(chip
, str
->dsd
, STS
);
117 if ((reg
& LOLA_DSD_STS_FIFORDY
) == val
)
121 dev_warn(chip
->card
->dev
, "FIFO not ready (stream %d)\n", str
->dsd
);
125 /* sync for FIFO ready/empty for all linked streams;
126 * clear paused flag when FIFO gets ready again
128 static int lola_sync_wait_for_fifo(struct lola
*chip
,
129 struct snd_pcm_substream
*substream
,
132 unsigned int val
= ready
? LOLA_DSD_STS_FIFORDY
: 0;
133 unsigned long end_time
= jiffies
+ msecs_to_jiffies(200);
134 struct snd_pcm_substream
*s
;
137 while (time_before(jiffies
, end_time
)) {
139 snd_pcm_group_for_each_entry(s
, substream
) {
140 struct lola_stream
*str
;
141 if (s
->pcm
->card
!= substream
->pcm
->card
)
143 str
= lola_get_stream(s
);
144 if (str
->prepared
&& str
->paused
) {
146 reg
= lola_dsd_read(chip
, str
->dsd
, STS
);
147 if ((reg
& LOLA_DSD_STS_FIFORDY
) != val
) {
148 pending
= str
->dsd
+ 1;
159 dev_warn(chip
->card
->dev
, "FIFO not ready (pending %d)\n", pending
- 1);
163 /* finish pause - prepare for a new resume */
164 static void lola_sync_pause(struct lola
*chip
,
165 struct snd_pcm_substream
*substream
)
167 struct snd_pcm_substream
*s
;
169 lola_sync_wait_for_fifo(chip
, substream
, false);
170 snd_pcm_group_for_each_entry(s
, substream
) {
171 struct lola_stream
*str
;
172 if (s
->pcm
->card
!= substream
->pcm
->card
)
174 str
= lola_get_stream(s
);
175 if (str
->paused
&& str
->prepared
)
176 lola_dsd_write(chip
, str
->dsd
, CTL
, LOLA_DSD_CTL_SRUN
|
177 LOLA_DSD_CTL_IOCE
| LOLA_DSD_CTL_DEIE
);
179 lola_sync_wait_for_fifo(chip
, substream
, true);
182 static void lola_stream_reset(struct lola
*chip
, struct lola_stream
*str
)
186 lola_sync_pause(chip
, str
->substream
);
188 lola_dsd_write(chip
, str
->dsd
, CTL
,
189 LOLA_DSD_CTL_IOCE
| LOLA_DSD_CTL_DEIE
);
190 lola_stream_wait_for_fifo(chip
, str
, false);
191 lola_stream_clear_pending_irq(chip
, str
);
192 lola_dsd_write(chip
, str
->dsd
, CTL
, LOLA_DSD_CTL_SRST
);
193 lola_dsd_write(chip
, str
->dsd
, LVI
, 0);
194 lola_dsd_write(chip
, str
->dsd
, BDPU
, 0);
195 lola_dsd_write(chip
, str
->dsd
, BDPL
, 0);
196 wait_for_srst_clear(chip
, str
);
200 static const struct snd_pcm_hardware lola_pcm_hw
= {
201 .info
= (SNDRV_PCM_INFO_MMAP
|
202 SNDRV_PCM_INFO_INTERLEAVED
|
203 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
204 SNDRV_PCM_INFO_MMAP_VALID
|
205 SNDRV_PCM_INFO_PAUSE
),
206 .formats
= (SNDRV_PCM_FMTBIT_S16_LE
|
207 SNDRV_PCM_FMTBIT_S24_LE
|
208 SNDRV_PCM_FMTBIT_S32_LE
|
209 SNDRV_PCM_FMTBIT_FLOAT_LE
),
210 .rates
= SNDRV_PCM_RATE_8000_192000
,
215 .buffer_bytes_max
= LOLA_MAX_BUF_SIZE
,
216 .period_bytes_min
= 128,
217 .period_bytes_max
= LOLA_MAX_BUF_SIZE
/ 2,
219 .periods_max
= LOLA_MAX_BDL_ENTRIES
,
223 static int lola_pcm_open(struct snd_pcm_substream
*substream
)
225 struct lola
*chip
= snd_pcm_substream_chip(substream
);
226 struct lola_pcm
*pcm
= lola_get_pcm(substream
);
227 struct lola_stream
*str
= lola_get_stream(substream
);
228 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
230 mutex_lock(&chip
->open_mutex
);
232 mutex_unlock(&chip
->open_mutex
);
235 str
->substream
= substream
;
238 runtime
->hw
= lola_pcm_hw
;
239 runtime
->hw
.channels_max
= pcm
->num_streams
- str
->index
;
240 if (chip
->sample_rate
) {
241 /* sample rate is locked */
242 runtime
->hw
.rate_min
= chip
->sample_rate
;
243 runtime
->hw
.rate_max
= chip
->sample_rate
;
245 runtime
->hw
.rate_min
= chip
->sample_rate_min
;
246 runtime
->hw
.rate_max
= chip
->sample_rate_max
;
248 chip
->ref_count_rate
++;
249 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
250 /* period size = multiple of chip->granularity (8, 16 or 32 frames)*/
251 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
253 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
255 mutex_unlock(&chip
->open_mutex
);
259 static void lola_cleanup_slave_streams(struct lola_pcm
*pcm
,
260 struct lola_stream
*str
)
263 for (i
= str
->index
+ 1; i
< pcm
->num_streams
; i
++) {
264 struct lola_stream
*s
= &pcm
->streams
[i
];
265 if (s
->master
!= str
)
272 static int lola_pcm_close(struct snd_pcm_substream
*substream
)
274 struct lola
*chip
= snd_pcm_substream_chip(substream
);
275 struct lola_stream
*str
= lola_get_stream(substream
);
277 mutex_lock(&chip
->open_mutex
);
278 if (str
->substream
== substream
) {
279 str
->substream
= NULL
;
282 if (--chip
->ref_count_rate
== 0) {
283 /* release sample rate */
284 chip
->sample_rate
= 0;
286 mutex_unlock(&chip
->open_mutex
);
290 static int lola_pcm_hw_params(struct snd_pcm_substream
*substream
,
291 struct snd_pcm_hw_params
*hw_params
)
293 struct lola_stream
*str
= lola_get_stream(substream
);
296 str
->period_bytes
= 0;
297 str
->format_verb
= 0;
298 return snd_pcm_lib_malloc_pages(substream
,
299 params_buffer_bytes(hw_params
));
302 static int lola_pcm_hw_free(struct snd_pcm_substream
*substream
)
304 struct lola
*chip
= snd_pcm_substream_chip(substream
);
305 struct lola_pcm
*pcm
= lola_get_pcm(substream
);
306 struct lola_stream
*str
= lola_get_stream(substream
);
308 mutex_lock(&chip
->open_mutex
);
309 lola_stream_reset(chip
, str
);
310 lola_cleanup_slave_streams(pcm
, str
);
311 mutex_unlock(&chip
->open_mutex
);
312 return snd_pcm_lib_free_pages(substream
);
318 static int setup_bdle(struct snd_pcm_substream
*substream
,
319 struct lola_stream
*str
, u32
**bdlp
,
328 if (str
->frags
>= LOLA_MAX_BDL_ENTRIES
)
331 addr
= snd_pcm_sgbuf_get_addr(substream
, ofs
);
332 /* program the address field of the BDL entry */
333 bdl
[0] = cpu_to_le32((u32
)addr
);
334 bdl
[1] = cpu_to_le32(upper_32_bits(addr
));
335 /* program the size field of the BDL entry */
336 chunk
= snd_pcm_sgbuf_get_chunk_size(substream
, ofs
, size
);
337 bdl
[2] = cpu_to_le32(chunk
);
338 /* program the IOC to enable interrupt
339 * only when the whole fragment is processed
342 bdl
[3] = size
? 0 : cpu_to_le32(0x01);
354 static int lola_setup_periods(struct lola
*chip
, struct lola_pcm
*pcm
,
355 struct snd_pcm_substream
*substream
,
356 struct lola_stream
*str
)
359 int i
, ofs
, periods
, period_bytes
;
361 period_bytes
= str
->period_bytes
;
362 periods
= str
->bufsize
/ period_bytes
;
364 /* program the initial BDL entries */
365 bdl
= (u32
*)(pcm
->bdl
.area
+ LOLA_BDL_ENTRY_SIZE
* str
->index
);
368 for (i
= 0; i
< periods
; i
++) {
369 ofs
= setup_bdle(substream
, str
, &bdl
, ofs
, period_bytes
);
376 dev_err(chip
->card
->dev
, "Too many BDL entries: buffer=%d, period=%d\n",
377 str
->bufsize
, period_bytes
);
381 static unsigned int lola_get_format_verb(struct snd_pcm_substream
*substream
)
385 switch (substream
->runtime
->format
) {
386 case SNDRV_PCM_FORMAT_S16_LE
:
389 case SNDRV_PCM_FORMAT_S24_LE
:
392 case SNDRV_PCM_FORMAT_S32_LE
:
395 case SNDRV_PCM_FORMAT_FLOAT_LE
:
401 verb
|= substream
->runtime
->channels
;
405 static int lola_set_stream_config(struct lola
*chip
,
406 struct lola_stream
*str
,
410 unsigned int verb
, val
;
412 /* set format info for all channels
413 * (with only one command for the first channel)
415 err
= lola_codec_read(chip
, str
->nid
, LOLA_VERB_SET_STREAM_FORMAT
,
416 str
->format_verb
, 0, &val
, NULL
);
418 dev_err(chip
->card
->dev
, "Cannot set stream format 0x%x\n",
423 /* update stream - channel config */
424 for (i
= 0; i
< channels
; i
++) {
425 verb
= (str
->index
<< 6) | i
;
426 err
= lola_codec_read(chip
, str
[i
].nid
,
427 LOLA_VERB_SET_CHANNEL_STREAMID
, 0, verb
,
430 dev_err(chip
->card
->dev
,
431 "Cannot set stream channel %d\n", i
);
439 * set up the SD for streaming
441 static int lola_setup_controller(struct lola
*chip
, struct lola_pcm
*pcm
,
442 struct lola_stream
*str
)
450 bdl
= pcm
->bdl
.addr
+ LOLA_BDL_ENTRY_SIZE
* str
->index
;
451 lola_dsd_write(chip
, str
->dsd
, BDPL
, (u32
)bdl
);
452 lola_dsd_write(chip
, str
->dsd
, BDPU
, upper_32_bits(bdl
));
453 /* program the stream LVI (last valid index) of the BDL */
454 lola_dsd_write(chip
, str
->dsd
, LVI
, str
->frags
- 1);
455 lola_stream_clear_pending_irq(chip
, str
);
457 lola_dsd_write(chip
, str
->dsd
, CTL
,
458 LOLA_DSD_CTL_IOCE
| LOLA_DSD_CTL_DEIE
| LOLA_DSD_CTL_SRUN
);
462 return lola_stream_wait_for_fifo(chip
, str
, true);
465 static int lola_pcm_prepare(struct snd_pcm_substream
*substream
)
467 struct lola
*chip
= snd_pcm_substream_chip(substream
);
468 struct lola_pcm
*pcm
= lola_get_pcm(substream
);
469 struct lola_stream
*str
= lola_get_stream(substream
);
470 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
471 unsigned int bufsize
, period_bytes
, format_verb
;
474 mutex_lock(&chip
->open_mutex
);
475 lola_stream_reset(chip
, str
);
476 lola_cleanup_slave_streams(pcm
, str
);
477 if (str
->index
+ runtime
->channels
> pcm
->num_streams
) {
478 mutex_unlock(&chip
->open_mutex
);
481 for (i
= 1; i
< runtime
->channels
; i
++) {
485 mutex_unlock(&chip
->open_mutex
);
487 bufsize
= snd_pcm_lib_buffer_bytes(substream
);
488 period_bytes
= snd_pcm_lib_period_bytes(substream
);
489 format_verb
= lola_get_format_verb(substream
);
491 str
->bufsize
= bufsize
;
492 str
->period_bytes
= period_bytes
;
493 str
->format_verb
= format_verb
;
495 err
= lola_setup_periods(chip
, pcm
, substream
, str
);
499 err
= lola_set_sample_rate(chip
, runtime
->rate
);
502 chip
->sample_rate
= runtime
->rate
; /* sample rate gets locked */
504 err
= lola_set_stream_config(chip
, str
, runtime
->channels
);
508 err
= lola_setup_controller(chip
, pcm
, str
);
510 lola_stream_reset(chip
, str
);
517 static int lola_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
519 struct lola
*chip
= snd_pcm_substream_chip(substream
);
520 struct lola_stream
*str
;
521 struct snd_pcm_substream
*s
;
527 case SNDRV_PCM_TRIGGER_START
:
528 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
529 case SNDRV_PCM_TRIGGER_RESUME
:
532 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
533 case SNDRV_PCM_TRIGGER_SUSPEND
:
534 case SNDRV_PCM_TRIGGER_STOP
:
542 * sample correct synchronization is only needed starting several
543 * streams. On stop or if only one stream do as quick as possible
545 sync_streams
= (start
&& snd_pcm_stream_linked(substream
));
546 tstamp
= lola_get_tstamp(chip
, !sync_streams
);
547 spin_lock(&chip
->reg_lock
);
548 snd_pcm_group_for_each_entry(s
, substream
) {
549 if (s
->pcm
->card
!= substream
->pcm
->card
)
551 str
= lola_get_stream(s
);
553 lola_stream_start(chip
, str
, tstamp
);
555 lola_stream_stop(chip
, str
, tstamp
);
556 str
->running
= start
;
557 str
->paused
= !start
;
558 snd_pcm_trigger_done(s
, substream
);
560 spin_unlock(&chip
->reg_lock
);
564 static snd_pcm_uframes_t
lola_pcm_pointer(struct snd_pcm_substream
*substream
)
566 struct lola
*chip
= snd_pcm_substream_chip(substream
);
567 struct lola_stream
*str
= lola_get_stream(substream
);
568 unsigned int pos
= lola_dsd_read(chip
, str
->dsd
, LPIB
);
570 if (pos
>= str
->bufsize
)
572 return bytes_to_frames(substream
->runtime
, pos
);
575 void lola_pcm_update(struct lola
*chip
, struct lola_pcm
*pcm
, unsigned int bits
)
579 for (i
= 0; bits
&& i
< pcm
->num_streams
; i
++) {
580 if (bits
& (1 << i
)) {
581 struct lola_stream
*str
= &pcm
->streams
[i
];
582 if (str
->substream
&& str
->running
)
583 snd_pcm_period_elapsed(str
->substream
);
589 static const struct snd_pcm_ops lola_pcm_ops
= {
590 .open
= lola_pcm_open
,
591 .close
= lola_pcm_close
,
592 .ioctl
= snd_pcm_lib_ioctl
,
593 .hw_params
= lola_pcm_hw_params
,
594 .hw_free
= lola_pcm_hw_free
,
595 .prepare
= lola_pcm_prepare
,
596 .trigger
= lola_pcm_trigger
,
597 .pointer
= lola_pcm_pointer
,
598 .page
= snd_pcm_sgbuf_ops_page
,
601 int lola_create_pcm(struct lola
*chip
)
606 for (i
= 0; i
< 2; i
++) {
607 err
= snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV
,
608 snd_dma_pci_data(chip
->pci
),
609 PAGE_SIZE
, &chip
->pcm
[i
].bdl
);
614 err
= snd_pcm_new(chip
->card
, "Digigram Lola", 0,
615 chip
->pcm
[SNDRV_PCM_STREAM_PLAYBACK
].num_streams
,
616 chip
->pcm
[SNDRV_PCM_STREAM_CAPTURE
].num_streams
,
620 strlcpy(pcm
->name
, "Digigram Lola", sizeof(pcm
->name
));
621 pcm
->private_data
= chip
;
622 for (i
= 0; i
< 2; i
++) {
623 if (chip
->pcm
[i
].num_streams
)
624 snd_pcm_set_ops(pcm
, i
, &lola_pcm_ops
);
626 /* buffer pre-allocation */
627 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_DEV_SG
,
628 snd_dma_pci_data(chip
->pci
),
629 1024 * 64, 32 * 1024 * 1024);
633 void lola_free_pcm(struct lola
*chip
)
635 snd_dma_free_pages(&chip
->pcm
[0].bdl
);
636 snd_dma_free_pages(&chip
->pcm
[1].bdl
);
642 static int lola_init_stream(struct lola
*chip
, struct lola_stream
*str
,
643 int idx
, int nid
, int dir
)
652 str
->dsd
+= MAX_STREAM_IN_COUNT
;
653 err
= lola_read_param(chip
, nid
, LOLA_PAR_AUDIO_WIDGET_CAP
, &val
);
655 dev_err(chip
->card
->dev
, "Can't read wcaps for 0x%x\n", nid
);
659 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1) */
660 if ((val
& 0x00f00dff) != 0x00000010) {
661 dev_err(chip
->card
->dev
,
662 "Invalid wcaps 0x%x for 0x%x\n",
667 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1)
668 * (bug : ignore bit8: Conn list = 0/1)
670 if ((val
& 0x00f00cff) != 0x00100010) {
671 dev_err(chip
->card
->dev
,
672 "Invalid wcaps 0x%x for 0x%x\n",
676 /* test bit9:DIGITAL and bit12:SRC_PRESENT*/
677 if ((val
& 0x00001200) == 0x00001200)
678 chip
->input_src_caps_mask
|= (1 << idx
);
681 err
= lola_read_param(chip
, nid
, LOLA_PAR_STREAM_FORMATS
, &val
);
683 dev_err(chip
->card
->dev
, "Can't read FORMATS 0x%x\n", nid
);
688 str
->can_float
= true;
690 dev_err(chip
->card
->dev
,
691 "Invalid formats 0x%x for 0x%x", val
, nid
);
697 int lola_init_pcm(struct lola
*chip
, int dir
, int *nidp
)
699 struct lola_pcm
*pcm
= &chip
->pcm
[dir
];
703 for (i
= 0; i
< pcm
->num_streams
; i
++, nid
++) {
704 err
= lola_init_stream(chip
, &pcm
->streams
[i
], i
, nid
, dir
);