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 <sound/core.h>
26 #include <sound/pcm.h>
29 #define LOLA_MAX_BDL_ENTRIES 8
30 #define LOLA_MAX_BUF_SIZE (1024*1024*1024)
31 #define LOLA_BDL_ENTRY_SIZE (16 * 16)
33 static struct lola_pcm *lola_get_pcm(struct snd_pcm_substream *substream)
35 struct lola *chip = snd_pcm_substream_chip(substream);
36 return &chip->pcm[substream->stream];
39 static struct lola_stream *lola_get_stream(struct snd_pcm_substream *substream)
41 struct lola_pcm *pcm = lola_get_pcm(substream);
42 unsigned int idx = substream->number;
43 return &pcm->streams[idx];
46 static unsigned int lola_get_lrc(struct lola *chip)
48 return lola_readl(chip, BAR1, LRC);
51 static unsigned int lola_get_tstamp(struct lola *chip, bool quick_no_sync)
53 unsigned int tstamp = lola_get_lrc(chip) >> 8;
54 if (chip->granularity) {
55 unsigned int wait_banks = quick_no_sync ? 0 : 8;
56 tstamp += (wait_banks + 1) * chip->granularity - 1;
57 tstamp -= tstamp % chip->granularity;
62 /* clear any pending interrupt status */
63 static void lola_stream_clear_pending_irq(struct lola *chip,
64 struct lola_stream *str)
66 unsigned int val = lola_dsd_read(chip, str->dsd, STS);
67 val &= LOLA_DSD_STS_DESE | LOLA_DSD_STS_BCIS;
69 lola_dsd_write(chip, str->dsd, STS, val);
72 static void lola_stream_start(struct lola *chip, struct lola_stream *str,
75 lola_stream_clear_pending_irq(chip, str);
76 lola_dsd_write(chip, str->dsd, CTL,
84 static void lola_stream_stop(struct lola *chip, struct lola_stream *str,
87 lola_dsd_write(chip, str->dsd, CTL,
92 lola_stream_clear_pending_irq(chip, str);
95 static void wait_for_srst_clear(struct lola *chip, struct lola_stream *str)
97 unsigned long end_time = jiffies + msecs_to_jiffies(200);
98 while (time_before(jiffies, end_time)) {
100 val = lola_dsd_read(chip, str->dsd, CTL);
101 if (!(val & LOLA_DSD_CTL_SRST))
105 printk(KERN_WARNING SFX "SRST not clear (stream %d)\n", str->dsd);
108 static int lola_stream_wait_for_fifo(struct lola *chip,
109 struct lola_stream *str,
112 unsigned int val = ready ? LOLA_DSD_STS_FIFORDY : 0;
113 unsigned long end_time = jiffies + msecs_to_jiffies(200);
114 while (time_before(jiffies, end_time)) {
115 unsigned int reg = lola_dsd_read(chip, str->dsd, STS);
116 if ((reg & LOLA_DSD_STS_FIFORDY) == val)
120 printk(KERN_WARNING SFX "FIFO not ready (stream %d)\n", str->dsd);
124 static void lola_stream_reset(struct lola *chip, struct lola_stream *str)
129 lola_dsd_write(chip, str->dsd, CTL,
130 LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE);
131 lola_stream_wait_for_fifo(chip, str, false);
132 lola_stream_clear_pending_irq(chip, str);
133 lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRST);
134 lola_dsd_write(chip, str->dsd, LVI, 0);
135 lola_dsd_write(chip, str->dsd, BDPU, 0);
136 lola_dsd_write(chip, str->dsd, BDPL, 0);
137 wait_for_srst_clear(chip, str);
141 static struct snd_pcm_hardware lola_pcm_hw = {
142 .info = (SNDRV_PCM_INFO_MMAP |
143 SNDRV_PCM_INFO_INTERLEAVED |
144 SNDRV_PCM_INFO_BLOCK_TRANSFER |
145 SNDRV_PCM_INFO_MMAP_VALID |
146 SNDRV_PCM_INFO_PAUSE),
147 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
148 SNDRV_PCM_FMTBIT_S24_LE |
149 SNDRV_PCM_FMTBIT_S32_LE |
150 SNDRV_PCM_FMTBIT_FLOAT_LE),
151 .rates = SNDRV_PCM_RATE_8000_192000,
156 .buffer_bytes_max = LOLA_MAX_BUF_SIZE,
157 .period_bytes_min = 128,
158 .period_bytes_max = LOLA_MAX_BUF_SIZE / 2,
160 .periods_max = LOLA_MAX_BDL_ENTRIES,
164 static int lola_pcm_open(struct snd_pcm_substream *substream)
166 struct lola *chip = snd_pcm_substream_chip(substream);
167 struct lola_pcm *pcm = lola_get_pcm(substream);
168 struct lola_stream *str = lola_get_stream(substream);
169 struct snd_pcm_runtime *runtime = substream->runtime;
171 mutex_lock(&chip->open_mutex);
173 mutex_unlock(&chip->open_mutex);
176 str->substream = substream;
179 runtime->hw = lola_pcm_hw;
180 runtime->hw.channels_max = pcm->num_streams - str->index;
181 runtime->hw.rate_min = chip->sample_rate_min;
182 runtime->hw.rate_max = chip->sample_rate_max;
183 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
184 /* period size = multiple of chip->granularity (8, 16 or 32 frames)
185 * use LOLA_GRANULARITY_MAX = 32 for instance
187 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
189 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
191 mutex_unlock(&chip->open_mutex);
195 static void lola_cleanup_slave_streams(struct lola_pcm *pcm,
196 struct lola_stream *str)
199 for (i = str->index + 1; i < pcm->num_streams; i++) {
200 struct lola_stream *s = &pcm->streams[i];
201 if (s->master != str)
208 static int lola_pcm_close(struct snd_pcm_substream *substream)
210 struct lola *chip = snd_pcm_substream_chip(substream);
211 struct lola_stream *str = lola_get_stream(substream);
213 mutex_lock(&chip->open_mutex);
214 if (str->substream == substream) {
215 str->substream = NULL;
218 mutex_unlock(&chip->open_mutex);
222 static int lola_pcm_hw_params(struct snd_pcm_substream *substream,
223 struct snd_pcm_hw_params *hw_params)
225 struct lola_stream *str = lola_get_stream(substream);
228 str->period_bytes = 0;
229 str->format_verb = 0;
230 return snd_pcm_lib_malloc_pages(substream,
231 params_buffer_bytes(hw_params));
234 static int lola_pcm_hw_free(struct snd_pcm_substream *substream)
236 struct lola *chip = snd_pcm_substream_chip(substream);
237 struct lola_pcm *pcm = lola_get_pcm(substream);
238 struct lola_stream *str = lola_get_stream(substream);
240 mutex_lock(&chip->open_mutex);
241 lola_stream_reset(chip, str);
242 lola_cleanup_slave_streams(pcm, str);
243 mutex_unlock(&chip->open_mutex);
244 return snd_pcm_lib_free_pages(substream);
250 static int setup_bdle(struct snd_pcm_substream *substream,
251 struct lola_stream *str, u32 **bdlp,
260 if (str->frags >= LOLA_MAX_BDL_ENTRIES)
263 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
264 /* program the address field of the BDL entry */
265 bdl[0] = cpu_to_le32((u32)addr);
266 bdl[1] = cpu_to_le32(upper_32_bits(addr));
267 /* program the size field of the BDL entry */
268 chunk = snd_pcm_sgbuf_get_chunk_size(substream, ofs, size);
269 bdl[2] = cpu_to_le32(chunk);
270 /* program the IOC to enable interrupt
271 * only when the whole fragment is processed
274 bdl[3] = size ? 0 : cpu_to_le32(0x01);
286 static int lola_setup_periods(struct lola *chip, struct lola_pcm *pcm,
287 struct snd_pcm_substream *substream,
288 struct lola_stream *str)
291 int i, ofs, periods, period_bytes;
293 period_bytes = str->period_bytes;
294 periods = str->bufsize / period_bytes;
296 /* program the initial BDL entries */
297 bdl = (u32 *)(pcm->bdl.area + LOLA_BDL_ENTRY_SIZE * str->index);
300 for (i = 0; i < periods; i++) {
301 ofs = setup_bdle(substream, str, &bdl, ofs, period_bytes);
308 snd_printk(KERN_ERR SFX "Too many BDL entries: buffer=%d, period=%d\n",
309 str->bufsize, period_bytes);
313 static unsigned int lola_get_format_verb(struct snd_pcm_substream *substream)
317 switch (substream->runtime->format) {
318 case SNDRV_PCM_FORMAT_S16_LE:
321 case SNDRV_PCM_FORMAT_S24_LE:
324 case SNDRV_PCM_FORMAT_S32_LE:
327 case SNDRV_PCM_FORMAT_FLOAT_LE:
333 verb |= substream->runtime->channels;
337 static int lola_set_stream_config(struct lola *chip,
338 struct lola_stream *str,
342 unsigned int verb, val;
344 /* set format info for all channels
345 * (with only one command for the first channel)
347 err = lola_codec_read(chip, str->nid, LOLA_VERB_SET_STREAM_FORMAT,
348 str->format_verb, 0, &val, NULL);
350 printk(KERN_ERR SFX "Cannot set stream format 0x%x\n",
355 /* update stream - channel config */
356 for (i = 0; i < channels; i++) {
357 verb = (str->index << 6) | i;
358 err = lola_codec_read(chip, str[i].nid,
359 LOLA_VERB_SET_CHANNEL_STREAMID, 0, verb,
362 printk(KERN_ERR SFX "Cannot set stream channel %d\n", i);
370 * set up the SD for streaming
372 static int lola_setup_controller(struct lola *chip, struct lola_pcm *pcm,
373 struct lola_stream *str)
381 bdl = pcm->bdl.addr + LOLA_BDL_ENTRY_SIZE * str->index;
382 lola_dsd_write(chip, str->dsd, BDPL, (u32)bdl);
383 lola_dsd_write(chip, str->dsd, BDPU, upper_32_bits(bdl));
384 /* program the stream LVI (last valid index) of the BDL */
385 lola_dsd_write(chip, str->dsd, LVI, str->frags - 1);
386 lola_stream_clear_pending_irq(chip, str);
388 lola_dsd_write(chip, str->dsd, CTL,
389 LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE | LOLA_DSD_CTL_SRUN);
393 return lola_stream_wait_for_fifo(chip, str, true);
396 static int lola_pcm_prepare(struct snd_pcm_substream *substream)
398 struct lola *chip = snd_pcm_substream_chip(substream);
399 struct lola_pcm *pcm = lola_get_pcm(substream);
400 struct lola_stream *str = lola_get_stream(substream);
401 struct snd_pcm_runtime *runtime = substream->runtime;
402 unsigned int bufsize, period_bytes, format_verb;
405 mutex_lock(&chip->open_mutex);
406 lola_stream_reset(chip, str);
407 lola_cleanup_slave_streams(pcm, str);
408 if (str->index + runtime->channels >= pcm->num_streams) {
409 mutex_unlock(&chip->open_mutex);
412 for (i = 1; i < runtime->channels; i++) {
416 mutex_unlock(&chip->open_mutex);
418 bufsize = snd_pcm_lib_buffer_bytes(substream);
419 period_bytes = snd_pcm_lib_period_bytes(substream);
420 format_verb = lola_get_format_verb(substream);
422 str->bufsize = bufsize;
423 str->period_bytes = period_bytes;
424 str->format_verb = format_verb;
426 err = lola_setup_periods(chip, pcm, substream, str);
430 err = lola_set_stream_config(chip, str, runtime->channels);
434 err = lola_setup_controller(chip, pcm, str);
436 lola_stream_reset(chip, str);
443 static int lola_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
445 struct lola *chip = snd_pcm_substream_chip(substream);
446 struct lola_stream *str;
447 struct snd_pcm_substream *s;
452 case SNDRV_PCM_TRIGGER_START:
453 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
454 case SNDRV_PCM_TRIGGER_RESUME:
457 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
458 case SNDRV_PCM_TRIGGER_SUSPEND:
459 case SNDRV_PCM_TRIGGER_STOP:
466 tstamp = lola_get_tstamp(chip, false);
467 spin_lock(&chip->reg_lock);
468 snd_pcm_group_for_each_entry(s, substream) {
469 if (s->pcm->card != substream->pcm->card)
471 str = lola_get_stream(s);
473 lola_stream_start(chip, str, tstamp);
475 lola_stream_stop(chip, str, tstamp);
476 str->running = start;
477 snd_pcm_trigger_done(s, substream);
479 spin_unlock(&chip->reg_lock);
483 static snd_pcm_uframes_t lola_pcm_pointer(struct snd_pcm_substream *substream)
485 struct lola *chip = snd_pcm_substream_chip(substream);
486 struct lola_stream *str = lola_get_stream(substream);
487 unsigned int pos = lola_dsd_read(chip, str->dsd, LPIB);
489 if (pos >= str->bufsize)
491 return bytes_to_frames(substream->runtime, pos);
494 void lola_pcm_update(struct lola *chip, struct lola_pcm *pcm, unsigned int bits)
498 for (i = 0; bits && i < pcm->num_streams; i++) {
499 if (bits & (1 << i)) {
500 struct lola_stream *str = &pcm->streams[i];
501 if (str->substream && str->running)
502 snd_pcm_period_elapsed(str->substream);
508 static struct snd_pcm_ops lola_pcm_ops = {
509 .open = lola_pcm_open,
510 .close = lola_pcm_close,
511 .ioctl = snd_pcm_lib_ioctl,
512 .hw_params = lola_pcm_hw_params,
513 .hw_free = lola_pcm_hw_free,
514 .prepare = lola_pcm_prepare,
515 .trigger = lola_pcm_trigger,
516 .pointer = lola_pcm_pointer,
517 .page = snd_pcm_sgbuf_ops_page,
520 int __devinit lola_create_pcm(struct lola *chip)
525 for (i = 0; i < 2; i++) {
526 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
527 snd_dma_pci_data(chip->pci),
528 PAGE_SIZE, &chip->pcm[i].bdl);
533 err = snd_pcm_new(chip->card, "Digigram Lola", 0,
534 chip->pcm[SNDRV_PCM_STREAM_PLAYBACK].num_streams,
535 chip->pcm[SNDRV_PCM_STREAM_CAPTURE].num_streams,
539 strlcpy(pcm->name, "Digigram Lola", sizeof(pcm->name));
540 pcm->private_data = chip;
541 for (i = 0; i < 2; i++) {
542 if (chip->pcm[i].num_streams)
543 snd_pcm_set_ops(pcm, i, &lola_pcm_ops);
545 /* buffer pre-allocation */
546 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
547 snd_dma_pci_data(chip->pci),
548 1024 * 64, 32 * 1024 * 1024);
552 void lola_free_pcm(struct lola *chip)
554 snd_dma_free_pages(&chip->pcm[0].bdl);
555 snd_dma_free_pages(&chip->pcm[1].bdl);
561 static int lola_init_stream(struct lola *chip, struct lola_stream *str,
562 int idx, int nid, int dir)
571 str->dsd += MAX_STREAM_IN_COUNT;
572 err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
574 printk(KERN_ERR SFX "Can't read wcaps for 0x%x\n", nid);
578 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1) */
579 if ((val & 0x00f00dff) != 0x00000010) {
580 printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
585 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1)
586 * (bug : ignore bit8: Conn list = 0/1)
588 if ((val & 0x00f00cff) != 0x00100010) {
589 printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
593 /* test bit9:DIGITAL and bit12:SRC_PRESENT*/
594 if ((val & 0x00001200) == 0x00001200)
595 chip->input_src_caps_mask |= (1 << idx);
598 err = lola_read_param(chip, nid, LOLA_PAR_STREAM_FORMATS, &val);
600 printk(KERN_ERR SFX "Can't read FORMATS 0x%x\n", nid);
605 str->can_float = true;
607 printk(KERN_ERR SFX "Invalid formats 0x%x for 0x%x", val, nid);
613 int __devinit lola_init_pcm(struct lola *chip, int dir, int *nidp)
615 struct lola_pcm *pcm = &chip->pcm[dir];
619 for (i = 0; i < pcm->num_streams; i++, nid++) {
620 err = lola_init_stream(chip, &pcm->streams[i], i, nid, dir);