blob: bf17f921d45ff45b7975c9f809c5ced51381978f [file] [log] [blame]
Takashi Iwaid43f30102011-05-03 16:14:46 +02001/*
2 * Support for Digigram Lola PCI-e boards
3 *
4 * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
5 *
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)
9 * any later version.
10 *
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
14 * more details.
15 *
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.
19 */
20
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>
27#include "lola.h"
28
29#define BDL_SIZE 4096
30#define LOLA_MAX_BDL_ENTRIES (BDL_SIZE / 16)
31#define LOLA_MAX_FRAG 32
32#define LOLA_MAX_BUF_SIZE (1024*1024*1024)
33
34static struct lola_pcm *lola_get_pcm(struct snd_pcm_substream *substream)
35{
36 struct lola *chip = snd_pcm_substream_chip(substream);
37 return &chip->pcm[substream->stream];
38}
39
40static struct lola_stream *lola_get_stream(struct snd_pcm_substream *substream)
41{
42 struct lola_pcm *pcm = lola_get_pcm(substream);
43 unsigned int idx = substream->number;
44 return &pcm->streams[idx];
45}
46
47static unsigned int lola_get_lrc(struct lola *chip)
48{
49 return lola_readl(chip, BAR1, LRC);
50}
51
52static unsigned int lola_get_tstamp(struct lola *chip, bool quick_no_sync)
53{
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;
59 }
60 return tstamp << 8;
61}
62
63/* clear any pending interrupt status */
64static void lola_stream_clear_pending_irq(struct lola *chip,
65 struct lola_stream *str)
66{
67 unsigned int val = lola_dsd_read(chip, str->dsd, STS);
68 val &= LOLA_DSD_STS_DESE | LOLA_DSD_STS_BCIS;
69 if (val)
70 lola_dsd_write(chip, str->dsd, STS, val);
71}
72
73static void lola_stream_start(struct lola *chip, struct lola_stream *str,
74 unsigned int tstamp)
75{
76 lola_stream_clear_pending_irq(chip, str);
77 lola_dsd_write(chip, str->dsd, CTL,
78 LOLA_DSD_CTL_SRUN |
79 LOLA_DSD_CTL_IOCE |
80 LOLA_DSD_CTL_DEIE |
81 LOLA_DSD_CTL_VLRCV |
82 tstamp);
83}
84
85static void lola_stream_stop(struct lola *chip, struct lola_stream *str,
86 unsigned int tstamp)
87{
88 lola_dsd_write(chip, str->dsd, CTL,
89 LOLA_DSD_CTL_IOCE |
90 LOLA_DSD_CTL_DEIE |
91 LOLA_DSD_CTL_VLRCV |
92 tstamp);
93 lola_stream_clear_pending_irq(chip, str);
94}
95
96static void lola_stream_clear(struct lola *chip, struct lola_stream *str)
97{
98 lola_dsd_write(chip, str->dsd, CTL, 0);
99 lola_stream_clear_pending_irq(chip, str);
100}
101
102static void wait_for_srst_clear(struct lola *chip, struct lola_stream *str)
103{
104 unsigned long end_time = jiffies + msecs_to_jiffies(50);
105 while (time_before(jiffies, end_time)) {
106 unsigned int val;
107 val = lola_dsd_read(chip, str->dsd, CTL);
108 if (!(val & LOLA_DSD_CTL_SRST))
109 return;
110 msleep(1);
111 }
112 printk(KERN_WARNING SFX "SRST not clear (stream %d)\n", str->dsd);
113}
114
115static void lola_stream_reset(struct lola *chip, struct lola_stream *str)
116{
117 lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRST);
118 lola_dsd_write(chip, str->dsd, LVI, 0);
119 lola_dsd_write(chip, str->dsd, BDPU, 0);
120 lola_dsd_write(chip, str->dsd, BDPL, 0);
121 wait_for_srst_clear(chip, str);
122}
123
124static int lola_stream_wait_for_fifo_ready(struct lola *chip,
125 struct lola_stream *str)
126{
127 unsigned long end_time = jiffies + msecs_to_jiffies(50);
128 while (time_before(jiffies, end_time)) {
129 unsigned int val = lola_dsd_read(chip, str->dsd, STS);
130 if (val & LOLA_DSD_STS_FIFORDY)
131 return 0;
132 msleep(1);
133 }
134 printk(KERN_WARNING SFX "FIFO not ready (stream %d)\n", str->dsd);
135 return -EIO;
136}
137
138static struct snd_pcm_hardware lola_pcm_hw = {
139 .info = (SNDRV_PCM_INFO_MMAP |
140 SNDRV_PCM_INFO_INTERLEAVED |
141 SNDRV_PCM_INFO_BLOCK_TRANSFER |
142 SNDRV_PCM_INFO_MMAP_VALID |
143 SNDRV_PCM_INFO_PAUSE),
144 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
145 SNDRV_PCM_FMTBIT_S24_LE |
146 SNDRV_PCM_FMTBIT_S32_LE |
147 SNDRV_PCM_FMTBIT_FLOAT_LE),
148 .rates = SNDRV_PCM_RATE_48000,
149 .rate_min = 48000,
150 .rate_max = 48000,
151 .channels_min = 1,
152 .channels_max = 2,
153 .buffer_bytes_max = LOLA_MAX_BUF_SIZE,
154 .period_bytes_min = 128,
155 .period_bytes_max = LOLA_MAX_BUF_SIZE / 2,
156 .periods_min = 2,
157 .periods_max = LOLA_MAX_FRAG,
158 .fifo_size = 0,
159};
160
161static int lola_pcm_open(struct snd_pcm_substream *substream)
162{
163 struct lola *chip = snd_pcm_substream_chip(substream);
164 struct lola_pcm *pcm = lola_get_pcm(substream);
165 struct lola_stream *str = lola_get_stream(substream);
166 struct snd_pcm_runtime *runtime = substream->runtime;
167 int err;
168
169 mutex_lock(&chip->open_mutex);
170 if (str->opened) {
171 mutex_unlock(&chip->open_mutex);
172 return -EBUSY;
173 }
174 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
175 snd_dma_pci_data(chip->pci),
176 PAGE_SIZE, &str->bdl);
177 if (err < 0) {
178 mutex_unlock(&chip->open_mutex);
179 printk(KERN_ERR SFX "Can't allocate BDL\n");
180 return err;
181 }
182 str->substream = substream;
183 str->master = NULL;
184 str->opened = 1;
185 runtime->hw = lola_pcm_hw;
186 runtime->hw.channels_max = pcm->num_streams - str->index;
187 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
188 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
189 128);
190 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
191 128);
192 mutex_unlock(&chip->open_mutex);
193 return 0;
194}
195
196static void lola_cleanup_slave_streams(struct lola_pcm *pcm,
197 struct lola_stream *str)
198{
199 int i;
200 for (i = str->index + 1; i < pcm->num_streams; i++) {
201 struct lola_stream *s = &pcm->streams[i];
202 if (s->master != str)
203 break;
204 s->master = NULL;
205 s->opened = 0;
206 }
207}
208
209static int lola_pcm_close(struct snd_pcm_substream *substream)
210{
211 struct lola *chip = snd_pcm_substream_chip(substream);
212 struct lola_stream *str = lola_get_stream(substream);
213
214 mutex_lock(&chip->open_mutex);
215 if (str->substream == substream) {
216 str->substream = NULL;
217 str->opened = 0;
218 }
219 snd_dma_free_pages(&str->bdl);
220 mutex_unlock(&chip->open_mutex);
221 return 0;
222}
223
224static int lola_pcm_hw_params(struct snd_pcm_substream *substream,
225 struct snd_pcm_hw_params *hw_params)
226{
227 struct lola_stream *str = lola_get_stream(substream);
228
229 str->bufsize = 0;
230 str->period_bytes = 0;
231 str->format_verb = 0;
232 return snd_pcm_lib_malloc_pages(substream,
233 params_buffer_bytes(hw_params));
234}
235
236static int lola_pcm_hw_free(struct snd_pcm_substream *substream)
237{
238 struct lola *chip = snd_pcm_substream_chip(substream);
239 struct lola_pcm *pcm = lola_get_pcm(substream);
240 struct lola_stream *str = lola_get_stream(substream);
241
242 mutex_lock(&chip->open_mutex);
243 lola_stream_reset(chip, str);
244 lola_cleanup_slave_streams(pcm, str);
245 mutex_unlock(&chip->open_mutex);
246 return snd_pcm_lib_free_pages(substream);
247}
248
249/*
250 * set up a BDL entry
251 */
252static int setup_bdle(struct snd_pcm_substream *substream,
253 struct lola_stream *str, u32 **bdlp,
254 int ofs, int size)
255{
256 u32 *bdl = *bdlp;
257
258 while (size > 0) {
259 dma_addr_t addr;
260 int chunk;
261
262 if (str->frags >= LOLA_MAX_BDL_ENTRIES)
263 return -EINVAL;
264
265 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
266 /* program the address field of the BDL entry */
267 bdl[0] = cpu_to_le32((u32)addr);
268 bdl[1] = cpu_to_le32(upper_32_bits(addr));
269 /* program the size field of the BDL entry */
270 chunk = snd_pcm_sgbuf_get_chunk_size(substream, ofs, size);
271 bdl[2] = cpu_to_le32(chunk);
272 /* program the IOC to enable interrupt
273 * only when the whole fragment is processed
274 */
275 size -= chunk;
276 bdl[3] = size ? 0 : cpu_to_le32(0x01);
277 bdl += 4;
278 str->frags++;
279 ofs += chunk;
280 }
281 *bdlp = bdl;
282 return ofs;
283}
284
285/*
286 * set up BDL entries
287 */
288static int lola_setup_periods(struct lola *chip,
289 struct snd_pcm_substream *substream,
290 struct lola_stream *str)
291{
292 u32 *bdl;
293 int i, ofs, periods, period_bytes;
294
295 period_bytes = str->period_bytes;
296 periods = str->bufsize / period_bytes;
297
298 /* program the initial BDL entries */
299 bdl = (u32 *)str->bdl.area;
300 ofs = 0;
301 str->frags = 0;
302 for (i = 0; i < periods; i++) {
303 ofs = setup_bdle(substream, str, &bdl, ofs, period_bytes);
304 if (ofs < 0)
305 goto error;
306 }
307 return 0;
308
309 error:
310 snd_printk(KERN_ERR SFX "Too many BDL entries: buffer=%d, period=%d\n",
311 str->bufsize, period_bytes);
312 return -EINVAL;
313}
314
315static unsigned int lola_get_format_verb(struct snd_pcm_substream *substream)
316{
317 unsigned int verb;
318
319 switch (substream->runtime->format) {
320 case SNDRV_PCM_FORMAT_S16_LE:
321 verb = 0x00000000;
322 break;
323 case SNDRV_PCM_FORMAT_S24_LE:
324 verb = 0x00000200;
325 break;
326 case SNDRV_PCM_FORMAT_S32_LE:
327 verb = 0x00000300;
328 break;
329 case SNDRV_PCM_FORMAT_FLOAT_LE:
330 verb = 0x00001300;
331 break;
332 default:
333 return 0;
334 }
335 verb |= substream->runtime->channels;
336 return verb;
337}
338
339static int lola_set_stream_config(struct lola *chip,
340 struct lola_stream *str,
341 int channels)
342{
343 int i, err;
344 unsigned int verb, val;
345
346 /* set format info for all channels
347 * (with only one command for the first channel)
348 */
349 err = lola_codec_read(chip, str->nid, LOLA_VERB_SET_STREAM_FORMAT,
350 str->format_verb, 0, &val, NULL);
351 if (err < 0) {
352 printk(KERN_ERR SFX "Cannot set stream format 0x%x\n",
353 str->format_verb);
354 return err;
355 }
356
357 /* update stream - channel config */
358 for (i = 0; i < channels; i++) {
359 verb = (str->index << 6) | i;
360 err = lola_codec_read(chip, str[i].nid,
361 LOLA_VERB_SET_CHANNEL_STREAMID, 0, verb,
362 &val, NULL);
363 if (err < 0) {
364 printk(KERN_ERR SFX "Cannot set stream channel %d\n", i);
365 return err;
366 }
367 }
368 return 0;
369}
370
371/*
372 * set up the SD for streaming
373 */
374static int lola_setup_controller(struct lola *chip, struct lola_stream *str)
375{
376 /* make sure the run bit is zero for SD */
377 lola_stream_clear(chip, str);
378 /* set up BDL */
379 lola_dsd_write(chip, str->dsd, BDPL, (u32)str->bdl.addr);
380 lola_dsd_write(chip, str->dsd, BDPU, upper_32_bits(str->bdl.addr));
381 /* program the stream LVI (last valid index) of the BDL */
382 lola_dsd_write(chip, str->dsd, LVI, str->frags - 1);
383 lola_stream_stop(chip, str, lola_get_tstamp(chip, false));
384 lola_stream_wait_for_fifo_ready(chip, str);
385
386 return 0;
387}
388
389static int lola_pcm_prepare(struct snd_pcm_substream *substream)
390{
391 struct lola *chip = snd_pcm_substream_chip(substream);
392 struct lola_pcm *pcm = lola_get_pcm(substream);
393 struct lola_stream *str = lola_get_stream(substream);
394 struct snd_pcm_runtime *runtime = substream->runtime;
395 unsigned int bufsize, period_bytes, format_verb;
396 int i, err;
397
398 mutex_lock(&chip->open_mutex);
399 lola_stream_reset(chip, str);
400 lola_cleanup_slave_streams(pcm, str);
401 if (str->index + runtime->channels >= pcm->num_streams) {
402 mutex_unlock(&chip->open_mutex);
403 return -EINVAL;
404 }
405 for (i = 1; i < runtime->channels; i++) {
406 str[i].master = str;
407 str[i].opened = 1;
408 }
409 mutex_unlock(&chip->open_mutex);
410
411 bufsize = snd_pcm_lib_buffer_bytes(substream);
412 period_bytes = snd_pcm_lib_period_bytes(substream);
413 format_verb = lola_get_format_verb(substream);
414
415 if (bufsize != str->bufsize ||
416 period_bytes != str->period_bytes ||
417 format_verb != str->format_verb) {
418 str->bufsize = bufsize;
419 str->period_bytes = period_bytes;
420 str->format_verb = format_verb;
421 err = lola_setup_periods(chip, substream, str);
422 if (err < 0)
423 return err;
424 }
425
426 err = lola_set_stream_config(chip, str, runtime->channels);
427 if (err < 0)
428 return err;
429
430 return lola_setup_controller(chip, str);
431}
432
433static int lola_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
434{
435 struct lola *chip = snd_pcm_substream_chip(substream);
436 struct lola_stream *str;
437 struct snd_pcm_substream *s;
438 unsigned int start;
439 unsigned int tstamp;
440
441 switch (cmd) {
442 case SNDRV_PCM_TRIGGER_START:
443 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
444 case SNDRV_PCM_TRIGGER_RESUME:
445 start = 1;
446 break;
447 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
448 case SNDRV_PCM_TRIGGER_SUSPEND:
449 case SNDRV_PCM_TRIGGER_STOP:
450 start = 0;
451 break;
452 default:
453 return -EINVAL;
454 }
455
456 tstamp = lola_get_tstamp(chip, false);
457 spin_lock(&chip->reg_lock);
458 snd_pcm_group_for_each_entry(s, substream) {
459 if (s->pcm->card != substream->pcm->card)
460 continue;
461 str = lola_get_stream(s);
462 if (start)
463 lola_stream_start(chip, str, tstamp);
464 else
465 lola_stream_stop(chip, str, tstamp);
466 str->running = start;
467 snd_pcm_trigger_done(s, substream);
468 }
469 spin_unlock(&chip->reg_lock);
470 return 0;
471}
472
473static snd_pcm_uframes_t lola_pcm_pointer(struct snd_pcm_substream *substream)
474{
475 struct lola *chip = snd_pcm_substream_chip(substream);
476 struct lola_stream *str = lola_get_stream(substream);
477 unsigned int pos = lola_dsd_read(chip, str->dsd, LPIB);
478
479 if (pos >= str->bufsize)
480 pos = 0;
481 return bytes_to_frames(substream->runtime, pos);
482}
483
484void lola_pcm_update(struct lola *chip, struct lola_pcm *pcm, unsigned int bits)
485{
486 int i;
487
488 for (i = 0; bits && i < pcm->num_streams; i++) {
489 if (bits & (1 << i)) {
490 struct lola_stream *str = &pcm->streams[i];
491 if (str->substream && str->running)
492 snd_pcm_period_elapsed(str->substream);
493 bits &= ~(1 << i);
494 }
495 }
496}
497
498static struct snd_pcm_ops lola_pcm_ops = {
499 .open = lola_pcm_open,
500 .close = lola_pcm_close,
501 .ioctl = snd_pcm_lib_ioctl,
502 .hw_params = lola_pcm_hw_params,
503 .hw_free = lola_pcm_hw_free,
504 .prepare = lola_pcm_prepare,
505 .trigger = lola_pcm_trigger,
506 .pointer = lola_pcm_pointer,
507 .page = snd_pcm_sgbuf_ops_page,
508};
509
510int __devinit lola_create_pcm(struct lola *chip)
511{
512 struct snd_pcm *pcm;
513 int i, err;
514
515 err = snd_pcm_new(chip->card, "Digigram Lola", 0,
516 chip->pcm[SNDRV_PCM_STREAM_PLAYBACK].num_streams,
517 chip->pcm[SNDRV_PCM_STREAM_CAPTURE].num_streams,
518 &pcm);
519 if (err < 0)
520 return err;
521 strlcpy(pcm->name, "Digigram Lola", sizeof(pcm->name));
522 pcm->private_data = chip;
523 for (i = 0; i < 2; i++) {
524 if (chip->pcm[i].num_streams)
525 snd_pcm_set_ops(pcm, i, &lola_pcm_ops);
526 }
527 /* buffer pre-allocation */
528 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
529 snd_dma_pci_data(chip->pci),
530 1024 * 64, 32 * 1024 * 1024);
531 return 0;
532}
533
534void lola_free_pcm(struct lola *chip)
535{
536 /* nothing to do */
537}
538
539/*
540 */
541
542static int lola_init_stream(struct lola *chip, struct lola_stream *str,
543 int idx, int nid, int dir)
544{
545 unsigned int val;
546 int err;
547
548 str->nid = nid;
549 str->index = idx;
550 str->dsd = idx;
551 if (dir == PLAY)
552 str->dsd += MAX_STREAM_IN_COUNT;
553 err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
554 if (err < 0) {
555 printk(KERN_ERR SFX "Can't read wcaps for 0x%x\n", nid);
556 return err;
557 }
558 if (dir == PLAY) {
559 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1) */
560 if ((val & 0x00f00dff) != 0x00000010) {
561 printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
562 val, nid);
563 return -EINVAL;
564 }
565 } else {
566 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1)
567 * (bug : ignore bit8: Conn list = 0/1)
568 */
569 if ((val & 0x00f00cff) != 0x00100010) {
570 printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
571 val, nid);
572 return -EINVAL;
573 }
574 /* test bit9:DIGITAL and bit12:SRC_PRESENT*/
575 if ((val & 0x00001200) == 0x00001200)
576 chip->input_src_caps_mask |= (1 << idx);
577 }
578
579 err = lola_read_param(chip, nid, LOLA_PAR_STREAM_FORMATS, &val);
580 if (err < 0) {
581 printk(KERN_ERR SFX "Can't read FORMATS 0x%x\n", nid);
582 return err;
583 }
584 val &= 3;
585 if (val == 3)
586 str->can_float = true;
587 if (!(val & 1)) {
588 printk(KERN_ERR SFX "Invalid formats 0x%x for 0x%x", val, nid);
589 return -EINVAL;
590 }
591 return 0;
592}
593
594int __devinit lola_init_pcm(struct lola *chip, int dir, int *nidp)
595{
596 struct lola_pcm *pcm = &chip->pcm[dir];
597 int i, nid, err;
598
599 nid = *nidp;
600 for (i = 0; i < pcm->num_streams; i++, nid++) {
601 err = lola_init_stream(chip, &pcm->streams[i], i, nid, dir);
602 if (err < 0)
603 return err;
604 }
605 *nidp = nid;
606 return 0;
607}