]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - sound/pci/lola/lola_pcm.c
4bb5b5bd6371f78fda1adb900d9e4e764643d0b3
[karo-tx-linux.git] / sound / pci / lola / lola_pcm.c
1 /*
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 LOLA_MAX_BDL_ENTRIES    8
30 #define LOLA_MAX_BUF_SIZE       (1024*1024*1024)
31 #define LOLA_BDL_ENTRY_SIZE     (16 * 16)
32
33 static struct lola_pcm *lola_get_pcm(struct snd_pcm_substream *substream)
34 {
35         struct lola *chip = snd_pcm_substream_chip(substream);
36         return &chip->pcm[substream->stream];
37 }
38
39 static struct lola_stream *lola_get_stream(struct snd_pcm_substream *substream)
40 {
41         struct lola_pcm *pcm = lola_get_pcm(substream);
42         unsigned int idx = substream->number;
43         return &pcm->streams[idx];
44 }
45
46 static unsigned int lola_get_lrc(struct lola *chip)
47 {
48         return lola_readl(chip, BAR1, LRC);
49 }
50
51 static unsigned int lola_get_tstamp(struct lola *chip, bool quick_no_sync)
52 {
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;
58         }
59         return tstamp << 8;
60 }
61
62 /* clear any pending interrupt status */
63 static void lola_stream_clear_pending_irq(struct lola *chip,
64                                           struct lola_stream *str)
65 {
66         unsigned int val = lola_dsd_read(chip, str->dsd, STS);
67         val &= LOLA_DSD_STS_DESE | LOLA_DSD_STS_BCIS;
68         if (val)
69                 lola_dsd_write(chip, str->dsd, STS, val);
70 }
71
72 static void lola_stream_start(struct lola *chip, struct lola_stream *str,
73                               unsigned int tstamp)
74 {
75         lola_stream_clear_pending_irq(chip, str);
76         lola_dsd_write(chip, str->dsd, CTL,
77                        LOLA_DSD_CTL_SRUN |
78                        LOLA_DSD_CTL_IOCE |
79                        LOLA_DSD_CTL_DEIE |
80                        LOLA_DSD_CTL_VLRCV |
81                        tstamp);
82 }
83
84 static void lola_stream_stop(struct lola *chip, struct lola_stream *str,
85                              unsigned int tstamp)
86 {
87         lola_dsd_write(chip, str->dsd, CTL,
88                        LOLA_DSD_CTL_IOCE |
89                        LOLA_DSD_CTL_DEIE |
90                        LOLA_DSD_CTL_VLRCV |
91                        tstamp);
92         lola_stream_clear_pending_irq(chip, str);
93 }
94
95 static void wait_for_srst_clear(struct lola *chip, struct lola_stream *str)
96 {
97         unsigned long end_time = jiffies + msecs_to_jiffies(200);
98         while (time_before(jiffies, end_time)) {
99                 unsigned int val;
100                 val = lola_dsd_read(chip, str->dsd, CTL);
101                 if (!(val & LOLA_DSD_CTL_SRST))
102                         return;
103                 msleep(1);
104         }
105         printk(KERN_WARNING SFX "SRST not clear (stream %d)\n", str->dsd);
106 }
107
108 static int lola_stream_wait_for_fifo(struct lola *chip,
109                                      struct lola_stream *str,
110                                      bool ready)
111 {
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)
117                         return 0;
118                 msleep(1);
119         }
120         printk(KERN_WARNING SFX "FIFO not ready (stream %d)\n", str->dsd);
121         return -EIO;
122 }
123
124 static void lola_stream_reset(struct lola *chip, struct lola_stream *str)
125 {
126         if (str->prepared) {
127                 str->prepared = 0;
128
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);
138         }
139 }
140
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,
152         .rate_min =             8000,
153         .rate_max =             192000,
154         .channels_min =         1,
155         .channels_max =         2,
156         .buffer_bytes_max =     LOLA_MAX_BUF_SIZE,
157         .period_bytes_min =     128,
158         .period_bytes_max =     LOLA_MAX_BUF_SIZE / 2,
159         .periods_min =          2,
160         .periods_max =          LOLA_MAX_BDL_ENTRIES,
161         .fifo_size =            0,
162 };
163
164 static int lola_pcm_open(struct snd_pcm_substream *substream)
165 {
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;
170
171         mutex_lock(&chip->open_mutex);
172         if (str->opened) {
173                 mutex_unlock(&chip->open_mutex);
174                 return -EBUSY;
175         }
176         str->substream = substream;
177         str->master = NULL;
178         str->opened = 1;
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
186          */
187         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
188                                    chip->granularity);
189         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
190                                    chip->granularity);
191         mutex_unlock(&chip->open_mutex);
192         return 0;
193 }
194
195 static void lola_cleanup_slave_streams(struct lola_pcm *pcm,
196                                        struct lola_stream *str)
197 {
198         int i;
199         for (i = str->index + 1; i < pcm->num_streams; i++) {
200                 struct lola_stream *s = &pcm->streams[i];
201                 if (s->master != str)
202                         break;
203                 s->master = NULL;
204                 s->opened = 0;
205         }
206 }
207
208 static int lola_pcm_close(struct snd_pcm_substream *substream)
209 {
210         struct lola *chip = snd_pcm_substream_chip(substream);
211         struct lola_stream *str = lola_get_stream(substream);
212
213         mutex_lock(&chip->open_mutex);
214         if (str->substream == substream) {
215                 str->substream = NULL;
216                 str->opened = 0;
217         }
218         mutex_unlock(&chip->open_mutex);
219         return 0;
220 }
221
222 static int lola_pcm_hw_params(struct snd_pcm_substream *substream,
223                               struct snd_pcm_hw_params *hw_params)
224 {
225         struct lola_stream *str = lola_get_stream(substream);
226
227         str->bufsize = 0;
228         str->period_bytes = 0;
229         str->format_verb = 0;
230         return snd_pcm_lib_malloc_pages(substream,
231                                         params_buffer_bytes(hw_params));
232 }
233
234 static int lola_pcm_hw_free(struct snd_pcm_substream *substream)
235 {
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);
239
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);
245 }
246
247 /*
248  * set up a BDL entry
249  */
250 static int setup_bdle(struct snd_pcm_substream *substream,
251                       struct lola_stream *str, u32 **bdlp,
252                       int ofs, int size)
253 {
254         u32 *bdl = *bdlp;
255
256         while (size > 0) {
257                 dma_addr_t addr;
258                 int chunk;
259
260                 if (str->frags >= LOLA_MAX_BDL_ENTRIES)
261                         return -EINVAL;
262
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
272                  */
273                 size -= chunk;
274                 bdl[3] = size ? 0 : cpu_to_le32(0x01);
275                 bdl += 4;
276                 str->frags++;
277                 ofs += chunk;
278         }
279         *bdlp = bdl;
280         return ofs;
281 }
282
283 /*
284  * set up BDL entries
285  */
286 static int lola_setup_periods(struct lola *chip, struct lola_pcm *pcm,
287                               struct snd_pcm_substream *substream,
288                               struct lola_stream *str)
289 {
290         u32 *bdl;
291         int i, ofs, periods, period_bytes;
292
293         period_bytes = str->period_bytes;
294         periods = str->bufsize / period_bytes;
295
296         /* program the initial BDL entries */
297         bdl = (u32 *)(pcm->bdl.area + LOLA_BDL_ENTRY_SIZE * str->index);
298         ofs = 0;
299         str->frags = 0;
300         for (i = 0; i < periods; i++) {
301                 ofs = setup_bdle(substream, str, &bdl, ofs, period_bytes);
302                 if (ofs < 0)
303                         goto error;
304         }
305         return 0;
306
307  error:
308         snd_printk(KERN_ERR SFX "Too many BDL entries: buffer=%d, period=%d\n",
309                    str->bufsize, period_bytes);
310         return -EINVAL;
311 }
312
313 static unsigned int lola_get_format_verb(struct snd_pcm_substream *substream)
314 {
315         unsigned int verb;
316
317         switch (substream->runtime->format) {
318         case SNDRV_PCM_FORMAT_S16_LE:
319                 verb = 0x00000000;
320                 break;
321         case SNDRV_PCM_FORMAT_S24_LE:
322                 verb = 0x00000200;
323                 break;
324         case SNDRV_PCM_FORMAT_S32_LE:
325                 verb = 0x00000300;
326                 break;
327         case SNDRV_PCM_FORMAT_FLOAT_LE:
328                 verb = 0x00001300;
329                 break;
330         default:
331                 return 0;
332         }
333         verb |= substream->runtime->channels;
334         return verb;
335 }
336
337 static int lola_set_stream_config(struct lola *chip,
338                                   struct lola_stream *str,
339                                   int channels)
340 {
341         int i, err;
342         unsigned int verb, val;
343
344         /* set format info for all channels
345          * (with only one command for the first channel)
346          */
347         err = lola_codec_read(chip, str->nid, LOLA_VERB_SET_STREAM_FORMAT,
348                               str->format_verb, 0, &val, NULL);
349         if (err < 0) {
350                 printk(KERN_ERR SFX "Cannot set stream format 0x%x\n",
351                        str->format_verb);
352                 return err;
353         }
354
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,
360                                       &val, NULL);
361                 if (err < 0) {
362                         printk(KERN_ERR SFX "Cannot set stream channel %d\n", i);
363                         return err;
364                 }
365         }
366         return 0;
367 }
368
369 /*
370  * set up the SD for streaming
371  */
372 static int lola_setup_controller(struct lola *chip, struct lola_pcm *pcm,
373                                  struct lola_stream *str)
374 {
375         dma_addr_t bdl;
376
377         if (str->prepared)
378                 return -EINVAL;
379
380         /* set up BDL */
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);
387
388         lola_dsd_write(chip, str->dsd, CTL,
389                        LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE | LOLA_DSD_CTL_SRUN);
390
391         str->prepared = 1;
392
393         return lola_stream_wait_for_fifo(chip, str, true);
394 }
395
396 static int lola_pcm_prepare(struct snd_pcm_substream *substream)
397 {
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;
403         int i, err;
404
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);
410                 return -EINVAL;
411         }
412         for (i = 1; i < runtime->channels; i++) {
413                 str[i].master = str;
414                 str[i].opened = 1;
415         }
416         mutex_unlock(&chip->open_mutex);
417
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);
421
422         str->bufsize = bufsize;
423         str->period_bytes = period_bytes;
424         str->format_verb = format_verb;
425
426         err = lola_setup_periods(chip, pcm, substream, str);
427         if (err < 0)
428                 return err;
429
430         err = lola_set_stream_config(chip, str, runtime->channels);
431         if (err < 0)
432                 return err;
433
434         err = lola_setup_controller(chip, pcm, str);
435         if (err < 0) {
436                 lola_stream_reset(chip, str);
437                 return err;
438         }
439
440         return 0;
441 }
442
443 static int lola_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
444 {
445         struct lola *chip = snd_pcm_substream_chip(substream);
446         struct lola_stream *str;
447         struct snd_pcm_substream *s;
448         unsigned int start;
449         unsigned int tstamp;
450
451         switch (cmd) {
452         case SNDRV_PCM_TRIGGER_START:
453         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
454         case SNDRV_PCM_TRIGGER_RESUME:
455                 start = 1;
456                 break;
457         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
458         case SNDRV_PCM_TRIGGER_SUSPEND:
459         case SNDRV_PCM_TRIGGER_STOP:
460                 start = 0;
461                 break;
462         default:
463                 return -EINVAL;
464         }
465
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)
470                         continue;
471                 str = lola_get_stream(s);
472                 if (start)
473                         lola_stream_start(chip, str, tstamp);
474                 else
475                         lola_stream_stop(chip, str, tstamp);
476                 str->running = start;
477                 snd_pcm_trigger_done(s, substream);
478         }
479         spin_unlock(&chip->reg_lock);
480         return 0;
481 }
482
483 static snd_pcm_uframes_t lola_pcm_pointer(struct snd_pcm_substream *substream)
484 {
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);
488
489         if (pos >= str->bufsize)
490                 pos = 0;
491         return bytes_to_frames(substream->runtime, pos);
492 }
493
494 void lola_pcm_update(struct lola *chip, struct lola_pcm *pcm, unsigned int bits)
495 {
496         int i;
497
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);
503                         bits &= ~(1 << i);
504                 }
505         }
506 }
507
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,
518 };
519
520 int __devinit lola_create_pcm(struct lola *chip)
521 {
522         struct snd_pcm *pcm;
523         int i, err;
524
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);
529                 if (err < 0)
530                         return err;
531         }
532
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,
536                           &pcm);
537         if (err < 0)
538                 return err;
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);
544         }
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);
549         return 0;
550 }
551
552 void lola_free_pcm(struct lola *chip)
553 {
554         snd_dma_free_pages(&chip->pcm[0].bdl);
555         snd_dma_free_pages(&chip->pcm[1].bdl);
556 }
557
558 /*
559  */
560
561 static int lola_init_stream(struct lola *chip, struct lola_stream *str,
562                             int idx, int nid, int dir)
563 {
564         unsigned int val;
565         int err;
566
567         str->nid = nid;
568         str->index = idx;
569         str->dsd = idx;
570         if (dir == PLAY)
571                 str->dsd += MAX_STREAM_IN_COUNT;
572         err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
573         if (err < 0) {
574                 printk(KERN_ERR SFX "Can't read wcaps for 0x%x\n", nid);
575                 return err;
576         }
577         if (dir == PLAY) {
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",
581                                val, nid);
582                         return -EINVAL;
583                 }
584         } else {
585                 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1)
586                  * (bug : ignore bit8: Conn list = 0/1)
587                  */
588                 if ((val & 0x00f00cff) != 0x00100010) {
589                         printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
590                                val, nid);
591                         return -EINVAL;
592                 }
593                 /* test bit9:DIGITAL and bit12:SRC_PRESENT*/
594                 if ((val & 0x00001200) == 0x00001200)
595                         chip->input_src_caps_mask |= (1 << idx);
596         }
597
598         err = lola_read_param(chip, nid, LOLA_PAR_STREAM_FORMATS, &val);
599         if (err < 0) {
600                 printk(KERN_ERR SFX "Can't read FORMATS 0x%x\n", nid);
601                 return err;
602         }
603         val &= 3;
604         if (val == 3)
605                 str->can_float = true;
606         if (!(val & 1)) {
607                 printk(KERN_ERR SFX "Invalid formats 0x%x for 0x%x", val, nid);
608                 return -EINVAL;
609         }
610         return 0;
611 }
612
613 int __devinit lola_init_pcm(struct lola *chip, int dir, int *nidp)
614 {
615         struct lola_pcm *pcm = &chip->pcm[dir];
616         int i, nid, err;
617
618         nid = *nidp;
619         for (i = 0; i < pcm->num_streams; i++, nid++) {
620                 err = lola_init_stream(chip, &pcm->streams[i], i, nid, dir);
621                 if (err < 0)
622                         return err;
623         }
624         *nidp = nid;
625         return 0;
626 }