blob: aa21750202fc6c35ca16b2fc20b15adaf61dfe16 [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
Takashi Iwai333ff392011-05-03 16:41:02 +020029#define LOLA_MAX_BDL_ENTRIES 8
Takashi Iwaid43f30102011-05-03 16:14:46 +020030#define LOLA_MAX_BUF_SIZE (1024*1024*1024)
Takashi Iwai333ff392011-05-03 16:41:02 +020031#define LOLA_BDL_ENTRY_SIZE (16 * 16)
32
Takashi Iwaid43f30102011-05-03 16:14:46 +020033static 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
39static 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
46static unsigned int lola_get_lrc(struct lola *chip)
47{
48 return lola_readl(chip, BAR1, LRC);
49}
50
51static 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 */
63static 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
72static 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
84static 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
Takashi Iwaid43f30102011-05-03 16:14:46 +020095static void wait_for_srst_clear(struct lola *chip, struct lola_stream *str)
96{
Takashi Iwai333ff392011-05-03 16:41:02 +020097 unsigned long end_time = jiffies + msecs_to_jiffies(200);
Takashi Iwaid43f30102011-05-03 16:14:46 +020098 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
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200108static int lola_stream_wait_for_fifo(struct lola *chip,
109 struct lola_stream *str,
110 bool ready)
Takashi Iwaid43f30102011-05-03 16:14:46 +0200111{
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200112 unsigned int val = ready ? LOLA_DSD_STS_FIFORDY : 0;
113 unsigned long end_time = jiffies + msecs_to_jiffies(200);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200114 while (time_before(jiffies, end_time)) {
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200115 unsigned int reg = lola_dsd_read(chip, str->dsd, STS);
116 if ((reg & LOLA_DSD_STS_FIFORDY) == val)
Takashi Iwaid43f30102011-05-03 16:14:46 +0200117 return 0;
118 msleep(1);
119 }
120 printk(KERN_WARNING SFX "FIFO not ready (stream %d)\n", str->dsd);
121 return -EIO;
122}
123
Takashi Iwaic7aad3c2011-05-03 17:02:35 +0200124/* sync for FIFO ready/empty for all linked streams;
125 * clear paused flag when FIFO gets ready again
126 */
127static int lola_sync_wait_for_fifo(struct lola *chip,
128 struct snd_pcm_substream *substream,
129 bool ready)
130{
131 unsigned int val = ready ? LOLA_DSD_STS_FIFORDY : 0;
132 unsigned long end_time = jiffies + msecs_to_jiffies(200);
133 struct snd_pcm_substream *s;
134 int pending = 0;
135
136 while (time_before(jiffies, end_time)) {
137 pending = 0;
138 snd_pcm_group_for_each_entry(s, substream) {
139 struct lola_stream *str;
140 if (s->pcm->card != substream->pcm->card)
141 continue;
142 str = lola_get_stream(s);
143 if (str->prepared && str->paused) {
144 unsigned int reg;
145 reg = lola_dsd_read(chip, str->dsd, STS);
146 if ((reg & LOLA_DSD_STS_FIFORDY) != val) {
147 pending = str->dsd + 1;
148 break;
149 }
150 if (ready)
151 str->paused = 0;
152 }
153 }
154 if (!pending)
155 return 0;
156 msleep(1);
157 }
158 printk(KERN_WARNING SFX "FIFO not ready (pending %d)\n", pending - 1);
159 return -EIO;
160}
161
162/* finish pause - prepare for a new resume */
163static void lola_sync_pause(struct lola *chip,
164 struct snd_pcm_substream *substream)
165{
166 struct snd_pcm_substream *s;
167
168 lola_sync_wait_for_fifo(chip, substream, false);
169 snd_pcm_group_for_each_entry(s, substream) {
170 struct lola_stream *str;
171 if (s->pcm->card != substream->pcm->card)
172 continue;
173 str = lola_get_stream(s);
174 if (str->paused && str->prepared)
175 lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRUN |
176 LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE);
177 }
178 lola_sync_wait_for_fifo(chip, substream, true);
179}
180
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200181static void lola_stream_reset(struct lola *chip, struct lola_stream *str)
182{
183 if (str->prepared) {
Takashi Iwaic7aad3c2011-05-03 17:02:35 +0200184 if (str->paused)
185 lola_sync_pause(chip, str->substream);
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200186 str->prepared = 0;
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200187 lola_dsd_write(chip, str->dsd, CTL,
188 LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE);
189 lola_stream_wait_for_fifo(chip, str, false);
190 lola_stream_clear_pending_irq(chip, str);
191 lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRST);
192 lola_dsd_write(chip, str->dsd, LVI, 0);
193 lola_dsd_write(chip, str->dsd, BDPU, 0);
194 lola_dsd_write(chip, str->dsd, BDPL, 0);
195 wait_for_srst_clear(chip, str);
196 }
197}
198
Takashi Iwaid43f30102011-05-03 16:14:46 +0200199static struct snd_pcm_hardware lola_pcm_hw = {
200 .info = (SNDRV_PCM_INFO_MMAP |
201 SNDRV_PCM_INFO_INTERLEAVED |
202 SNDRV_PCM_INFO_BLOCK_TRANSFER |
203 SNDRV_PCM_INFO_MMAP_VALID |
204 SNDRV_PCM_INFO_PAUSE),
205 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
206 SNDRV_PCM_FMTBIT_S24_LE |
207 SNDRV_PCM_FMTBIT_S32_LE |
208 SNDRV_PCM_FMTBIT_FLOAT_LE),
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200209 .rates = SNDRV_PCM_RATE_8000_192000,
210 .rate_min = 8000,
211 .rate_max = 192000,
Takashi Iwaid43f30102011-05-03 16:14:46 +0200212 .channels_min = 1,
213 .channels_max = 2,
214 .buffer_bytes_max = LOLA_MAX_BUF_SIZE,
215 .period_bytes_min = 128,
216 .period_bytes_max = LOLA_MAX_BUF_SIZE / 2,
217 .periods_min = 2,
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200218 .periods_max = LOLA_MAX_BDL_ENTRIES,
Takashi Iwaid43f30102011-05-03 16:14:46 +0200219 .fifo_size = 0,
220};
221
222static int lola_pcm_open(struct snd_pcm_substream *substream)
223{
224 struct lola *chip = snd_pcm_substream_chip(substream);
225 struct lola_pcm *pcm = lola_get_pcm(substream);
226 struct lola_stream *str = lola_get_stream(substream);
227 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200228
229 mutex_lock(&chip->open_mutex);
230 if (str->opened) {
231 mutex_unlock(&chip->open_mutex);
232 return -EBUSY;
233 }
Takashi Iwaid43f30102011-05-03 16:14:46 +0200234 str->substream = substream;
235 str->master = NULL;
236 str->opened = 1;
237 runtime->hw = lola_pcm_hw;
238 runtime->hw.channels_max = pcm->num_streams - str->index;
Takashi Iwai7e79f222011-05-03 16:59:27 +0200239 if (chip->sample_rate) {
240 /* sample rate is locked */
241 runtime->hw.rate_min = chip->sample_rate;
242 runtime->hw.rate_max = chip->sample_rate;
243 } else {
244 runtime->hw.rate_min = chip->sample_rate_min;
245 runtime->hw.rate_max = chip->sample_rate_max;
246 }
247 chip->ref_count_rate++;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200248 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Takashi Iwai7e79f222011-05-03 16:59:27 +0200249 /* period size = multiple of chip->granularity (8, 16 or 32 frames)*/
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200250 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
Takashi Iwai8bd172d2011-05-03 16:51:56 +0200251 chip->granularity);
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200252 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Takashi Iwai8bd172d2011-05-03 16:51:56 +0200253 chip->granularity);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200254 mutex_unlock(&chip->open_mutex);
255 return 0;
256}
257
258static void lola_cleanup_slave_streams(struct lola_pcm *pcm,
259 struct lola_stream *str)
260{
261 int i;
262 for (i = str->index + 1; i < pcm->num_streams; i++) {
263 struct lola_stream *s = &pcm->streams[i];
264 if (s->master != str)
265 break;
266 s->master = NULL;
267 s->opened = 0;
268 }
269}
270
271static int lola_pcm_close(struct snd_pcm_substream *substream)
272{
273 struct lola *chip = snd_pcm_substream_chip(substream);
274 struct lola_stream *str = lola_get_stream(substream);
275
276 mutex_lock(&chip->open_mutex);
277 if (str->substream == substream) {
278 str->substream = NULL;
279 str->opened = 0;
280 }
Takashi Iwai7e79f222011-05-03 16:59:27 +0200281 if (--chip->ref_count_rate == 0) {
282 /* release sample rate */
283 chip->sample_rate = 0;
284 }
Takashi Iwaid43f30102011-05-03 16:14:46 +0200285 mutex_unlock(&chip->open_mutex);
286 return 0;
287}
288
289static int lola_pcm_hw_params(struct snd_pcm_substream *substream,
290 struct snd_pcm_hw_params *hw_params)
291{
292 struct lola_stream *str = lola_get_stream(substream);
293
294 str->bufsize = 0;
295 str->period_bytes = 0;
296 str->format_verb = 0;
297 return snd_pcm_lib_malloc_pages(substream,
298 params_buffer_bytes(hw_params));
299}
300
301static int lola_pcm_hw_free(struct snd_pcm_substream *substream)
302{
303 struct lola *chip = snd_pcm_substream_chip(substream);
304 struct lola_pcm *pcm = lola_get_pcm(substream);
305 struct lola_stream *str = lola_get_stream(substream);
306
307 mutex_lock(&chip->open_mutex);
308 lola_stream_reset(chip, str);
309 lola_cleanup_slave_streams(pcm, str);
310 mutex_unlock(&chip->open_mutex);
311 return snd_pcm_lib_free_pages(substream);
312}
313
314/*
315 * set up a BDL entry
316 */
317static int setup_bdle(struct snd_pcm_substream *substream,
318 struct lola_stream *str, u32 **bdlp,
319 int ofs, int size)
320{
321 u32 *bdl = *bdlp;
322
323 while (size > 0) {
324 dma_addr_t addr;
325 int chunk;
326
327 if (str->frags >= LOLA_MAX_BDL_ENTRIES)
328 return -EINVAL;
329
Takashi Iwai972505c2011-05-03 16:50:51 +0200330 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200331 /* program the address field of the BDL entry */
332 bdl[0] = cpu_to_le32((u32)addr);
333 bdl[1] = cpu_to_le32(upper_32_bits(addr));
334 /* program the size field of the BDL entry */
Takashi Iwai972505c2011-05-03 16:50:51 +0200335 chunk = snd_pcm_sgbuf_get_chunk_size(substream, ofs, size);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200336 bdl[2] = cpu_to_le32(chunk);
337 /* program the IOC to enable interrupt
338 * only when the whole fragment is processed
339 */
340 size -= chunk;
341 bdl[3] = size ? 0 : cpu_to_le32(0x01);
342 bdl += 4;
343 str->frags++;
344 ofs += chunk;
345 }
346 *bdlp = bdl;
347 return ofs;
348}
349
350/*
351 * set up BDL entries
352 */
Takashi Iwai333ff392011-05-03 16:41:02 +0200353static int lola_setup_periods(struct lola *chip, struct lola_pcm *pcm,
Takashi Iwaid43f30102011-05-03 16:14:46 +0200354 struct snd_pcm_substream *substream,
355 struct lola_stream *str)
356{
357 u32 *bdl;
358 int i, ofs, periods, period_bytes;
359
360 period_bytes = str->period_bytes;
361 periods = str->bufsize / period_bytes;
362
363 /* program the initial BDL entries */
Takashi Iwai333ff392011-05-03 16:41:02 +0200364 bdl = (u32 *)(pcm->bdl.area + LOLA_BDL_ENTRY_SIZE * str->index);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200365 ofs = 0;
366 str->frags = 0;
367 for (i = 0; i < periods; i++) {
368 ofs = setup_bdle(substream, str, &bdl, ofs, period_bytes);
369 if (ofs < 0)
370 goto error;
371 }
372 return 0;
373
374 error:
375 snd_printk(KERN_ERR SFX "Too many BDL entries: buffer=%d, period=%d\n",
376 str->bufsize, period_bytes);
377 return -EINVAL;
378}
379
380static unsigned int lola_get_format_verb(struct snd_pcm_substream *substream)
381{
382 unsigned int verb;
383
384 switch (substream->runtime->format) {
385 case SNDRV_PCM_FORMAT_S16_LE:
386 verb = 0x00000000;
387 break;
388 case SNDRV_PCM_FORMAT_S24_LE:
389 verb = 0x00000200;
390 break;
391 case SNDRV_PCM_FORMAT_S32_LE:
392 verb = 0x00000300;
393 break;
394 case SNDRV_PCM_FORMAT_FLOAT_LE:
395 verb = 0x00001300;
396 break;
397 default:
398 return 0;
399 }
400 verb |= substream->runtime->channels;
401 return verb;
402}
403
404static int lola_set_stream_config(struct lola *chip,
405 struct lola_stream *str,
406 int channels)
407{
408 int i, err;
409 unsigned int verb, val;
410
411 /* set format info for all channels
412 * (with only one command for the first channel)
413 */
414 err = lola_codec_read(chip, str->nid, LOLA_VERB_SET_STREAM_FORMAT,
415 str->format_verb, 0, &val, NULL);
416 if (err < 0) {
417 printk(KERN_ERR SFX "Cannot set stream format 0x%x\n",
418 str->format_verb);
419 return err;
420 }
421
422 /* update stream - channel config */
423 for (i = 0; i < channels; i++) {
424 verb = (str->index << 6) | i;
425 err = lola_codec_read(chip, str[i].nid,
426 LOLA_VERB_SET_CHANNEL_STREAMID, 0, verb,
427 &val, NULL);
428 if (err < 0) {
429 printk(KERN_ERR SFX "Cannot set stream channel %d\n", i);
430 return err;
431 }
432 }
433 return 0;
434}
435
436/*
437 * set up the SD for streaming
438 */
Takashi Iwai333ff392011-05-03 16:41:02 +0200439static int lola_setup_controller(struct lola *chip, struct lola_pcm *pcm,
440 struct lola_stream *str)
Takashi Iwaid43f30102011-05-03 16:14:46 +0200441{
Takashi Iwai333ff392011-05-03 16:41:02 +0200442 dma_addr_t bdl;
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200443
444 if (str->prepared)
445 return -EINVAL;
446
Takashi Iwaid43f30102011-05-03 16:14:46 +0200447 /* set up BDL */
Takashi Iwai333ff392011-05-03 16:41:02 +0200448 bdl = pcm->bdl.addr + LOLA_BDL_ENTRY_SIZE * str->index;
449 lola_dsd_write(chip, str->dsd, BDPL, (u32)bdl);
450 lola_dsd_write(chip, str->dsd, BDPU, upper_32_bits(bdl));
Takashi Iwaid43f30102011-05-03 16:14:46 +0200451 /* program the stream LVI (last valid index) of the BDL */
452 lola_dsd_write(chip, str->dsd, LVI, str->frags - 1);
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200453 lola_stream_clear_pending_irq(chip, str);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200454
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200455 lola_dsd_write(chip, str->dsd, CTL,
456 LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE | LOLA_DSD_CTL_SRUN);
457
458 str->prepared = 1;
459
460 return lola_stream_wait_for_fifo(chip, str, true);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200461}
462
463static int lola_pcm_prepare(struct snd_pcm_substream *substream)
464{
465 struct lola *chip = snd_pcm_substream_chip(substream);
466 struct lola_pcm *pcm = lola_get_pcm(substream);
467 struct lola_stream *str = lola_get_stream(substream);
468 struct snd_pcm_runtime *runtime = substream->runtime;
469 unsigned int bufsize, period_bytes, format_verb;
470 int i, err;
471
472 mutex_lock(&chip->open_mutex);
473 lola_stream_reset(chip, str);
474 lola_cleanup_slave_streams(pcm, str);
Takashi Iwai2db30022011-05-03 17:05:08 +0200475 if (str->index + runtime->channels > pcm->num_streams) {
Takashi Iwaid43f30102011-05-03 16:14:46 +0200476 mutex_unlock(&chip->open_mutex);
477 return -EINVAL;
478 }
479 for (i = 1; i < runtime->channels; i++) {
480 str[i].master = str;
481 str[i].opened = 1;
482 }
483 mutex_unlock(&chip->open_mutex);
484
485 bufsize = snd_pcm_lib_buffer_bytes(substream);
486 period_bytes = snd_pcm_lib_period_bytes(substream);
487 format_verb = lola_get_format_verb(substream);
488
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200489 str->bufsize = bufsize;
490 str->period_bytes = period_bytes;
491 str->format_verb = format_verb;
492
493 err = lola_setup_periods(chip, pcm, substream, str);
494 if (err < 0)
495 return err;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200496
Takashi Iwai7e79f222011-05-03 16:59:27 +0200497 err = lola_set_sample_rate(chip, runtime->rate);
498 if (err < 0)
499 return err;
500 chip->sample_rate = runtime->rate; /* sample rate gets locked */
501
Takashi Iwaid43f30102011-05-03 16:14:46 +0200502 err = lola_set_stream_config(chip, str, runtime->channels);
503 if (err < 0)
504 return err;
505
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200506 err = lola_setup_controller(chip, pcm, str);
507 if (err < 0) {
508 lola_stream_reset(chip, str);
509 return err;
510 }
511
512 return 0;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200513}
514
515static int lola_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
516{
517 struct lola *chip = snd_pcm_substream_chip(substream);
518 struct lola_stream *str;
519 struct snd_pcm_substream *s;
520 unsigned int start;
521 unsigned int tstamp;
Takashi Iwai7e79f222011-05-03 16:59:27 +0200522 bool sync_streams;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200523
524 switch (cmd) {
525 case SNDRV_PCM_TRIGGER_START:
526 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
527 case SNDRV_PCM_TRIGGER_RESUME:
528 start = 1;
529 break;
530 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
531 case SNDRV_PCM_TRIGGER_SUSPEND:
532 case SNDRV_PCM_TRIGGER_STOP:
533 start = 0;
534 break;
535 default:
536 return -EINVAL;
537 }
538
Takashi Iwai7e79f222011-05-03 16:59:27 +0200539 /*
540 * sample correct synchronization is only needed starting several
Takashi Iwai2db30022011-05-03 17:05:08 +0200541 * streams. On stop or if only one stream do as quick as possible
Takashi Iwai7e79f222011-05-03 16:59:27 +0200542 */
543 sync_streams = (start && snd_pcm_stream_linked(substream));
544 tstamp = lola_get_tstamp(chip, !sync_streams);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200545 spin_lock(&chip->reg_lock);
546 snd_pcm_group_for_each_entry(s, substream) {
547 if (s->pcm->card != substream->pcm->card)
548 continue;
549 str = lola_get_stream(s);
550 if (start)
551 lola_stream_start(chip, str, tstamp);
552 else
553 lola_stream_stop(chip, str, tstamp);
554 str->running = start;
Takashi Iwai7e79f222011-05-03 16:59:27 +0200555 str->paused = !start;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200556 snd_pcm_trigger_done(s, substream);
557 }
558 spin_unlock(&chip->reg_lock);
559 return 0;
560}
561
562static snd_pcm_uframes_t lola_pcm_pointer(struct snd_pcm_substream *substream)
563{
564 struct lola *chip = snd_pcm_substream_chip(substream);
565 struct lola_stream *str = lola_get_stream(substream);
566 unsigned int pos = lola_dsd_read(chip, str->dsd, LPIB);
567
568 if (pos >= str->bufsize)
569 pos = 0;
570 return bytes_to_frames(substream->runtime, pos);
571}
572
573void lola_pcm_update(struct lola *chip, struct lola_pcm *pcm, unsigned int bits)
574{
575 int i;
576
577 for (i = 0; bits && i < pcm->num_streams; i++) {
578 if (bits & (1 << i)) {
579 struct lola_stream *str = &pcm->streams[i];
580 if (str->substream && str->running)
581 snd_pcm_period_elapsed(str->substream);
582 bits &= ~(1 << i);
583 }
584 }
585}
586
587static struct snd_pcm_ops lola_pcm_ops = {
588 .open = lola_pcm_open,
589 .close = lola_pcm_close,
590 .ioctl = snd_pcm_lib_ioctl,
591 .hw_params = lola_pcm_hw_params,
592 .hw_free = lola_pcm_hw_free,
593 .prepare = lola_pcm_prepare,
594 .trigger = lola_pcm_trigger,
595 .pointer = lola_pcm_pointer,
Takashi Iwai972505c2011-05-03 16:50:51 +0200596 .page = snd_pcm_sgbuf_ops_page,
Takashi Iwaid43f30102011-05-03 16:14:46 +0200597};
598
599int __devinit lola_create_pcm(struct lola *chip)
600{
601 struct snd_pcm *pcm;
602 int i, err;
603
Takashi Iwai333ff392011-05-03 16:41:02 +0200604 for (i = 0; i < 2; i++) {
605 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
606 snd_dma_pci_data(chip->pci),
607 PAGE_SIZE, &chip->pcm[i].bdl);
608 if (err < 0)
609 return err;
610 }
611
Takashi Iwaid43f30102011-05-03 16:14:46 +0200612 err = snd_pcm_new(chip->card, "Digigram Lola", 0,
613 chip->pcm[SNDRV_PCM_STREAM_PLAYBACK].num_streams,
614 chip->pcm[SNDRV_PCM_STREAM_CAPTURE].num_streams,
615 &pcm);
616 if (err < 0)
617 return err;
618 strlcpy(pcm->name, "Digigram Lola", sizeof(pcm->name));
619 pcm->private_data = chip;
620 for (i = 0; i < 2; i++) {
621 if (chip->pcm[i].num_streams)
622 snd_pcm_set_ops(pcm, i, &lola_pcm_ops);
623 }
624 /* buffer pre-allocation */
Takashi Iwai972505c2011-05-03 16:50:51 +0200625 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwaid43f30102011-05-03 16:14:46 +0200626 snd_dma_pci_data(chip->pci),
627 1024 * 64, 32 * 1024 * 1024);
628 return 0;
629}
630
631void lola_free_pcm(struct lola *chip)
632{
Takashi Iwai333ff392011-05-03 16:41:02 +0200633 snd_dma_free_pages(&chip->pcm[0].bdl);
634 snd_dma_free_pages(&chip->pcm[1].bdl);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200635}
636
637/*
638 */
639
640static int lola_init_stream(struct lola *chip, struct lola_stream *str,
641 int idx, int nid, int dir)
642{
643 unsigned int val;
644 int err;
645
646 str->nid = nid;
647 str->index = idx;
648 str->dsd = idx;
649 if (dir == PLAY)
650 str->dsd += MAX_STREAM_IN_COUNT;
651 err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
652 if (err < 0) {
653 printk(KERN_ERR SFX "Can't read wcaps for 0x%x\n", nid);
654 return err;
655 }
656 if (dir == PLAY) {
657 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1) */
658 if ((val & 0x00f00dff) != 0x00000010) {
659 printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
660 val, nid);
661 return -EINVAL;
662 }
663 } else {
664 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1)
665 * (bug : ignore bit8: Conn list = 0/1)
666 */
667 if ((val & 0x00f00cff) != 0x00100010) {
668 printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
669 val, nid);
670 return -EINVAL;
671 }
672 /* test bit9:DIGITAL and bit12:SRC_PRESENT*/
673 if ((val & 0x00001200) == 0x00001200)
674 chip->input_src_caps_mask |= (1 << idx);
675 }
676
677 err = lola_read_param(chip, nid, LOLA_PAR_STREAM_FORMATS, &val);
678 if (err < 0) {
679 printk(KERN_ERR SFX "Can't read FORMATS 0x%x\n", nid);
680 return err;
681 }
682 val &= 3;
683 if (val == 3)
684 str->can_float = true;
685 if (!(val & 1)) {
686 printk(KERN_ERR SFX "Invalid formats 0x%x for 0x%x", val, nid);
687 return -EINVAL;
688 }
689 return 0;
690}
691
692int __devinit lola_init_pcm(struct lola *chip, int dir, int *nidp)
693{
694 struct lola_pcm *pcm = &chip->pcm[dir];
695 int i, nid, err;
696
697 nid = *nidp;
698 for (i = 0; i < pcm->num_streams; i++, nid++) {
699 err = lola_init_stream(chip, &pcm->streams[i], i, nid, dir);
700 if (err < 0)
701 return err;
702 }
703 *nidp = nid;
704 return 0;
705}