blob: 6be6b7e8f5676a1a37867ed4c8aa7920f6b0f0e4 [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 Iwai0f8f56c2011-05-03 16:47:03 +0200124static void lola_stream_reset(struct lola *chip, struct lola_stream *str)
125{
126 if (str->prepared) {
127 str->prepared = 0;
128
Takashi Iwai7e79f222011-05-03 16:59:27 +0200129 if (str->paused) {
130 /* finish pause - prepare for a new resume
131 * move this code later to trigger function,
132 * as this is also needed when resuming from pause
133 */
134 str->paused = 0;
135 /* implement later loop for all streams */
136 lola_stream_wait_for_fifo(chip, str, false);
137 lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRUN |
138 LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE);
139 /* end loop */
140 /* implement later once more loop for all streams */
141 lola_stream_wait_for_fifo(chip, str, true);
142 /* end loop */
143 /* end finish pause */
144 }
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200145 lola_dsd_write(chip, str->dsd, CTL,
146 LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE);
147 lola_stream_wait_for_fifo(chip, str, false);
148 lola_stream_clear_pending_irq(chip, str);
149 lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRST);
150 lola_dsd_write(chip, str->dsd, LVI, 0);
151 lola_dsd_write(chip, str->dsd, BDPU, 0);
152 lola_dsd_write(chip, str->dsd, BDPL, 0);
153 wait_for_srst_clear(chip, str);
154 }
155}
156
Takashi Iwaid43f30102011-05-03 16:14:46 +0200157static struct snd_pcm_hardware lola_pcm_hw = {
158 .info = (SNDRV_PCM_INFO_MMAP |
159 SNDRV_PCM_INFO_INTERLEAVED |
160 SNDRV_PCM_INFO_BLOCK_TRANSFER |
161 SNDRV_PCM_INFO_MMAP_VALID |
162 SNDRV_PCM_INFO_PAUSE),
163 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
164 SNDRV_PCM_FMTBIT_S24_LE |
165 SNDRV_PCM_FMTBIT_S32_LE |
166 SNDRV_PCM_FMTBIT_FLOAT_LE),
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200167 .rates = SNDRV_PCM_RATE_8000_192000,
168 .rate_min = 8000,
169 .rate_max = 192000,
Takashi Iwaid43f30102011-05-03 16:14:46 +0200170 .channels_min = 1,
171 .channels_max = 2,
172 .buffer_bytes_max = LOLA_MAX_BUF_SIZE,
173 .period_bytes_min = 128,
174 .period_bytes_max = LOLA_MAX_BUF_SIZE / 2,
175 .periods_min = 2,
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200176 .periods_max = LOLA_MAX_BDL_ENTRIES,
Takashi Iwaid43f30102011-05-03 16:14:46 +0200177 .fifo_size = 0,
178};
179
180static int lola_pcm_open(struct snd_pcm_substream *substream)
181{
182 struct lola *chip = snd_pcm_substream_chip(substream);
183 struct lola_pcm *pcm = lola_get_pcm(substream);
184 struct lola_stream *str = lola_get_stream(substream);
185 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200186
187 mutex_lock(&chip->open_mutex);
188 if (str->opened) {
189 mutex_unlock(&chip->open_mutex);
190 return -EBUSY;
191 }
Takashi Iwaid43f30102011-05-03 16:14:46 +0200192 str->substream = substream;
193 str->master = NULL;
194 str->opened = 1;
195 runtime->hw = lola_pcm_hw;
196 runtime->hw.channels_max = pcm->num_streams - str->index;
Takashi Iwai7e79f222011-05-03 16:59:27 +0200197 if (chip->sample_rate) {
198 /* sample rate is locked */
199 runtime->hw.rate_min = chip->sample_rate;
200 runtime->hw.rate_max = chip->sample_rate;
201 } else {
202 runtime->hw.rate_min = chip->sample_rate_min;
203 runtime->hw.rate_max = chip->sample_rate_max;
204 }
205 chip->ref_count_rate++;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200206 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Takashi Iwai7e79f222011-05-03 16:59:27 +0200207 /* period size = multiple of chip->granularity (8, 16 or 32 frames)*/
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200208 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
Takashi Iwai8bd172d2011-05-03 16:51:56 +0200209 chip->granularity);
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200210 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Takashi Iwai8bd172d2011-05-03 16:51:56 +0200211 chip->granularity);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200212 mutex_unlock(&chip->open_mutex);
213 return 0;
214}
215
216static void lola_cleanup_slave_streams(struct lola_pcm *pcm,
217 struct lola_stream *str)
218{
219 int i;
220 for (i = str->index + 1; i < pcm->num_streams; i++) {
221 struct lola_stream *s = &pcm->streams[i];
222 if (s->master != str)
223 break;
224 s->master = NULL;
225 s->opened = 0;
226 }
227}
228
229static int lola_pcm_close(struct snd_pcm_substream *substream)
230{
231 struct lola *chip = snd_pcm_substream_chip(substream);
232 struct lola_stream *str = lola_get_stream(substream);
233
234 mutex_lock(&chip->open_mutex);
235 if (str->substream == substream) {
236 str->substream = NULL;
237 str->opened = 0;
238 }
Takashi Iwai7e79f222011-05-03 16:59:27 +0200239 if (--chip->ref_count_rate == 0) {
240 /* release sample rate */
241 chip->sample_rate = 0;
242 }
Takashi Iwaid43f30102011-05-03 16:14:46 +0200243 mutex_unlock(&chip->open_mutex);
244 return 0;
245}
246
247static int lola_pcm_hw_params(struct snd_pcm_substream *substream,
248 struct snd_pcm_hw_params *hw_params)
249{
250 struct lola_stream *str = lola_get_stream(substream);
251
252 str->bufsize = 0;
253 str->period_bytes = 0;
254 str->format_verb = 0;
255 return snd_pcm_lib_malloc_pages(substream,
256 params_buffer_bytes(hw_params));
257}
258
259static int lola_pcm_hw_free(struct snd_pcm_substream *substream)
260{
261 struct lola *chip = snd_pcm_substream_chip(substream);
262 struct lola_pcm *pcm = lola_get_pcm(substream);
263 struct lola_stream *str = lola_get_stream(substream);
264
265 mutex_lock(&chip->open_mutex);
266 lola_stream_reset(chip, str);
267 lola_cleanup_slave_streams(pcm, str);
268 mutex_unlock(&chip->open_mutex);
269 return snd_pcm_lib_free_pages(substream);
270}
271
272/*
273 * set up a BDL entry
274 */
275static int setup_bdle(struct snd_pcm_substream *substream,
276 struct lola_stream *str, u32 **bdlp,
277 int ofs, int size)
278{
279 u32 *bdl = *bdlp;
280
281 while (size > 0) {
282 dma_addr_t addr;
283 int chunk;
284
285 if (str->frags >= LOLA_MAX_BDL_ENTRIES)
286 return -EINVAL;
287
Takashi Iwai972505c2011-05-03 16:50:51 +0200288 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200289 /* program the address field of the BDL entry */
290 bdl[0] = cpu_to_le32((u32)addr);
291 bdl[1] = cpu_to_le32(upper_32_bits(addr));
292 /* program the size field of the BDL entry */
Takashi Iwai972505c2011-05-03 16:50:51 +0200293 chunk = snd_pcm_sgbuf_get_chunk_size(substream, ofs, size);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200294 bdl[2] = cpu_to_le32(chunk);
295 /* program the IOC to enable interrupt
296 * only when the whole fragment is processed
297 */
298 size -= chunk;
299 bdl[3] = size ? 0 : cpu_to_le32(0x01);
300 bdl += 4;
301 str->frags++;
302 ofs += chunk;
303 }
304 *bdlp = bdl;
305 return ofs;
306}
307
308/*
309 * set up BDL entries
310 */
Takashi Iwai333ff392011-05-03 16:41:02 +0200311static int lola_setup_periods(struct lola *chip, struct lola_pcm *pcm,
Takashi Iwaid43f30102011-05-03 16:14:46 +0200312 struct snd_pcm_substream *substream,
313 struct lola_stream *str)
314{
315 u32 *bdl;
316 int i, ofs, periods, period_bytes;
317
318 period_bytes = str->period_bytes;
319 periods = str->bufsize / period_bytes;
320
321 /* program the initial BDL entries */
Takashi Iwai333ff392011-05-03 16:41:02 +0200322 bdl = (u32 *)(pcm->bdl.area + LOLA_BDL_ENTRY_SIZE * str->index);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200323 ofs = 0;
324 str->frags = 0;
325 for (i = 0; i < periods; i++) {
326 ofs = setup_bdle(substream, str, &bdl, ofs, period_bytes);
327 if (ofs < 0)
328 goto error;
329 }
330 return 0;
331
332 error:
333 snd_printk(KERN_ERR SFX "Too many BDL entries: buffer=%d, period=%d\n",
334 str->bufsize, period_bytes);
335 return -EINVAL;
336}
337
338static unsigned int lola_get_format_verb(struct snd_pcm_substream *substream)
339{
340 unsigned int verb;
341
342 switch (substream->runtime->format) {
343 case SNDRV_PCM_FORMAT_S16_LE:
344 verb = 0x00000000;
345 break;
346 case SNDRV_PCM_FORMAT_S24_LE:
347 verb = 0x00000200;
348 break;
349 case SNDRV_PCM_FORMAT_S32_LE:
350 verb = 0x00000300;
351 break;
352 case SNDRV_PCM_FORMAT_FLOAT_LE:
353 verb = 0x00001300;
354 break;
355 default:
356 return 0;
357 }
358 verb |= substream->runtime->channels;
359 return verb;
360}
361
362static int lola_set_stream_config(struct lola *chip,
363 struct lola_stream *str,
364 int channels)
365{
366 int i, err;
367 unsigned int verb, val;
368
369 /* set format info for all channels
370 * (with only one command for the first channel)
371 */
372 err = lola_codec_read(chip, str->nid, LOLA_VERB_SET_STREAM_FORMAT,
373 str->format_verb, 0, &val, NULL);
374 if (err < 0) {
375 printk(KERN_ERR SFX "Cannot set stream format 0x%x\n",
376 str->format_verb);
377 return err;
378 }
379
380 /* update stream - channel config */
381 for (i = 0; i < channels; i++) {
382 verb = (str->index << 6) | i;
383 err = lola_codec_read(chip, str[i].nid,
384 LOLA_VERB_SET_CHANNEL_STREAMID, 0, verb,
385 &val, NULL);
386 if (err < 0) {
387 printk(KERN_ERR SFX "Cannot set stream channel %d\n", i);
388 return err;
389 }
390 }
391 return 0;
392}
393
394/*
395 * set up the SD for streaming
396 */
Takashi Iwai333ff392011-05-03 16:41:02 +0200397static int lola_setup_controller(struct lola *chip, struct lola_pcm *pcm,
398 struct lola_stream *str)
Takashi Iwaid43f30102011-05-03 16:14:46 +0200399{
Takashi Iwai333ff392011-05-03 16:41:02 +0200400 dma_addr_t bdl;
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200401
402 if (str->prepared)
403 return -EINVAL;
404
Takashi Iwaid43f30102011-05-03 16:14:46 +0200405 /* set up BDL */
Takashi Iwai333ff392011-05-03 16:41:02 +0200406 bdl = pcm->bdl.addr + LOLA_BDL_ENTRY_SIZE * str->index;
407 lola_dsd_write(chip, str->dsd, BDPL, (u32)bdl);
408 lola_dsd_write(chip, str->dsd, BDPU, upper_32_bits(bdl));
Takashi Iwaid43f30102011-05-03 16:14:46 +0200409 /* program the stream LVI (last valid index) of the BDL */
410 lola_dsd_write(chip, str->dsd, LVI, str->frags - 1);
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200411 lola_stream_clear_pending_irq(chip, str);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200412
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200413 lola_dsd_write(chip, str->dsd, CTL,
414 LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE | LOLA_DSD_CTL_SRUN);
415
416 str->prepared = 1;
417
418 return lola_stream_wait_for_fifo(chip, str, true);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200419}
420
421static int lola_pcm_prepare(struct snd_pcm_substream *substream)
422{
423 struct lola *chip = snd_pcm_substream_chip(substream);
424 struct lola_pcm *pcm = lola_get_pcm(substream);
425 struct lola_stream *str = lola_get_stream(substream);
426 struct snd_pcm_runtime *runtime = substream->runtime;
427 unsigned int bufsize, period_bytes, format_verb;
428 int i, err;
429
430 mutex_lock(&chip->open_mutex);
431 lola_stream_reset(chip, str);
432 lola_cleanup_slave_streams(pcm, str);
433 if (str->index + runtime->channels >= pcm->num_streams) {
434 mutex_unlock(&chip->open_mutex);
435 return -EINVAL;
436 }
437 for (i = 1; i < runtime->channels; i++) {
438 str[i].master = str;
439 str[i].opened = 1;
440 }
441 mutex_unlock(&chip->open_mutex);
442
443 bufsize = snd_pcm_lib_buffer_bytes(substream);
444 period_bytes = snd_pcm_lib_period_bytes(substream);
445 format_verb = lola_get_format_verb(substream);
446
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200447 str->bufsize = bufsize;
448 str->period_bytes = period_bytes;
449 str->format_verb = format_verb;
450
451 err = lola_setup_periods(chip, pcm, substream, str);
452 if (err < 0)
453 return err;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200454
Takashi Iwai7e79f222011-05-03 16:59:27 +0200455 err = lola_set_sample_rate(chip, runtime->rate);
456 if (err < 0)
457 return err;
458 chip->sample_rate = runtime->rate; /* sample rate gets locked */
459
Takashi Iwaid43f30102011-05-03 16:14:46 +0200460 err = lola_set_stream_config(chip, str, runtime->channels);
461 if (err < 0)
462 return err;
463
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200464 err = lola_setup_controller(chip, pcm, str);
465 if (err < 0) {
466 lola_stream_reset(chip, str);
467 return err;
468 }
469
470 return 0;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200471}
472
473static int lola_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
474{
475 struct lola *chip = snd_pcm_substream_chip(substream);
476 struct lola_stream *str;
477 struct snd_pcm_substream *s;
478 unsigned int start;
479 unsigned int tstamp;
Takashi Iwai7e79f222011-05-03 16:59:27 +0200480 bool sync_streams;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200481
482 switch (cmd) {
483 case SNDRV_PCM_TRIGGER_START:
484 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
485 case SNDRV_PCM_TRIGGER_RESUME:
486 start = 1;
487 break;
488 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
489 case SNDRV_PCM_TRIGGER_SUSPEND:
490 case SNDRV_PCM_TRIGGER_STOP:
491 start = 0;
492 break;
493 default:
494 return -EINVAL;
495 }
496
Takashi Iwai7e79f222011-05-03 16:59:27 +0200497 /*
498 * sample correct synchronization is only needed starting several
499 * streams on stop or if only one stream do as quick as possible
500 */
501 sync_streams = (start && snd_pcm_stream_linked(substream));
502 tstamp = lola_get_tstamp(chip, !sync_streams);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200503 spin_lock(&chip->reg_lock);
504 snd_pcm_group_for_each_entry(s, substream) {
505 if (s->pcm->card != substream->pcm->card)
506 continue;
507 str = lola_get_stream(s);
508 if (start)
509 lola_stream_start(chip, str, tstamp);
510 else
511 lola_stream_stop(chip, str, tstamp);
512 str->running = start;
Takashi Iwai7e79f222011-05-03 16:59:27 +0200513 str->paused = !start;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200514 snd_pcm_trigger_done(s, substream);
515 }
516 spin_unlock(&chip->reg_lock);
517 return 0;
518}
519
520static snd_pcm_uframes_t lola_pcm_pointer(struct snd_pcm_substream *substream)
521{
522 struct lola *chip = snd_pcm_substream_chip(substream);
523 struct lola_stream *str = lola_get_stream(substream);
524 unsigned int pos = lola_dsd_read(chip, str->dsd, LPIB);
525
526 if (pos >= str->bufsize)
527 pos = 0;
528 return bytes_to_frames(substream->runtime, pos);
529}
530
531void lola_pcm_update(struct lola *chip, struct lola_pcm *pcm, unsigned int bits)
532{
533 int i;
534
535 for (i = 0; bits && i < pcm->num_streams; i++) {
536 if (bits & (1 << i)) {
537 struct lola_stream *str = &pcm->streams[i];
538 if (str->substream && str->running)
539 snd_pcm_period_elapsed(str->substream);
540 bits &= ~(1 << i);
541 }
542 }
543}
544
545static struct snd_pcm_ops lola_pcm_ops = {
546 .open = lola_pcm_open,
547 .close = lola_pcm_close,
548 .ioctl = snd_pcm_lib_ioctl,
549 .hw_params = lola_pcm_hw_params,
550 .hw_free = lola_pcm_hw_free,
551 .prepare = lola_pcm_prepare,
552 .trigger = lola_pcm_trigger,
553 .pointer = lola_pcm_pointer,
Takashi Iwai972505c2011-05-03 16:50:51 +0200554 .page = snd_pcm_sgbuf_ops_page,
Takashi Iwaid43f30102011-05-03 16:14:46 +0200555};
556
557int __devinit lola_create_pcm(struct lola *chip)
558{
559 struct snd_pcm *pcm;
560 int i, err;
561
Takashi Iwai333ff392011-05-03 16:41:02 +0200562 for (i = 0; i < 2; i++) {
563 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
564 snd_dma_pci_data(chip->pci),
565 PAGE_SIZE, &chip->pcm[i].bdl);
566 if (err < 0)
567 return err;
568 }
569
Takashi Iwaid43f30102011-05-03 16:14:46 +0200570 err = snd_pcm_new(chip->card, "Digigram Lola", 0,
571 chip->pcm[SNDRV_PCM_STREAM_PLAYBACK].num_streams,
572 chip->pcm[SNDRV_PCM_STREAM_CAPTURE].num_streams,
573 &pcm);
574 if (err < 0)
575 return err;
576 strlcpy(pcm->name, "Digigram Lola", sizeof(pcm->name));
577 pcm->private_data = chip;
578 for (i = 0; i < 2; i++) {
579 if (chip->pcm[i].num_streams)
580 snd_pcm_set_ops(pcm, i, &lola_pcm_ops);
581 }
582 /* buffer pre-allocation */
Takashi Iwai972505c2011-05-03 16:50:51 +0200583 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwaid43f30102011-05-03 16:14:46 +0200584 snd_dma_pci_data(chip->pci),
585 1024 * 64, 32 * 1024 * 1024);
586 return 0;
587}
588
589void lola_free_pcm(struct lola *chip)
590{
Takashi Iwai333ff392011-05-03 16:41:02 +0200591 snd_dma_free_pages(&chip->pcm[0].bdl);
592 snd_dma_free_pages(&chip->pcm[1].bdl);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200593}
594
595/*
596 */
597
598static int lola_init_stream(struct lola *chip, struct lola_stream *str,
599 int idx, int nid, int dir)
600{
601 unsigned int val;
602 int err;
603
604 str->nid = nid;
605 str->index = idx;
606 str->dsd = idx;
607 if (dir == PLAY)
608 str->dsd += MAX_STREAM_IN_COUNT;
609 err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
610 if (err < 0) {
611 printk(KERN_ERR SFX "Can't read wcaps for 0x%x\n", nid);
612 return err;
613 }
614 if (dir == PLAY) {
615 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1) */
616 if ((val & 0x00f00dff) != 0x00000010) {
617 printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
618 val, nid);
619 return -EINVAL;
620 }
621 } else {
622 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1)
623 * (bug : ignore bit8: Conn list = 0/1)
624 */
625 if ((val & 0x00f00cff) != 0x00100010) {
626 printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
627 val, nid);
628 return -EINVAL;
629 }
630 /* test bit9:DIGITAL and bit12:SRC_PRESENT*/
631 if ((val & 0x00001200) == 0x00001200)
632 chip->input_src_caps_mask |= (1 << idx);
633 }
634
635 err = lola_read_param(chip, nid, LOLA_PAR_STREAM_FORMATS, &val);
636 if (err < 0) {
637 printk(KERN_ERR SFX "Can't read FORMATS 0x%x\n", nid);
638 return err;
639 }
640 val &= 3;
641 if (val == 3)
642 str->can_float = true;
643 if (!(val & 1)) {
644 printk(KERN_ERR SFX "Invalid formats 0x%x for 0x%x", val, nid);
645 return -EINVAL;
646 }
647 return 0;
648}
649
650int __devinit lola_init_pcm(struct lola *chip, int dir, int *nidp)
651{
652 struct lola_pcm *pcm = &chip->pcm[dir];
653 int i, nid, err;
654
655 nid = *nidp;
656 for (i = 0; i < pcm->num_streams; i++, nid++) {
657 err = lola_init_stream(chip, &pcm->streams[i], i, nid, dir);
658 if (err < 0)
659 return err;
660 }
661 *nidp = nid;
662 return 0;
663}