blob: 5ea85e8b83ab5018029d92504902775170adb018 [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>
Takashi Iwaif0447852011-05-03 18:21:01 +020025#include <linux/delay.h>
Takashi Iwaid43f30102011-05-03 16:14:46 +020026#include <sound/core.h>
27#include <sound/pcm.h>
28#include "lola.h"
29
Takashi Iwai333ff392011-05-03 16:41:02 +020030#define LOLA_MAX_BDL_ENTRIES 8
Takashi Iwaid43f30102011-05-03 16:14:46 +020031#define LOLA_MAX_BUF_SIZE (1024*1024*1024)
Takashi Iwai333ff392011-05-03 16:41:02 +020032#define LOLA_BDL_ENTRY_SIZE (16 * 16)
33
Takashi Iwaid43f30102011-05-03 16:14:46 +020034static 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
Takashi Iwaid43f30102011-05-03 16:14:46 +020096static void wait_for_srst_clear(struct lola *chip, struct lola_stream *str)
97{
Takashi Iwai333ff392011-05-03 16:41:02 +020098 unsigned long end_time = jiffies + msecs_to_jiffies(200);
Takashi Iwaid43f30102011-05-03 16:14:46 +020099 while (time_before(jiffies, end_time)) {
100 unsigned int val;
101 val = lola_dsd_read(chip, str->dsd, CTL);
102 if (!(val & LOLA_DSD_CTL_SRST))
103 return;
104 msleep(1);
105 }
106 printk(KERN_WARNING SFX "SRST not clear (stream %d)\n", str->dsd);
107}
108
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200109static int lola_stream_wait_for_fifo(struct lola *chip,
110 struct lola_stream *str,
111 bool ready)
Takashi Iwaid43f30102011-05-03 16:14:46 +0200112{
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200113 unsigned int val = ready ? LOLA_DSD_STS_FIFORDY : 0;
114 unsigned long end_time = jiffies + msecs_to_jiffies(200);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200115 while (time_before(jiffies, end_time)) {
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200116 unsigned int reg = lola_dsd_read(chip, str->dsd, STS);
117 if ((reg & LOLA_DSD_STS_FIFORDY) == val)
Takashi Iwaid43f30102011-05-03 16:14:46 +0200118 return 0;
119 msleep(1);
120 }
121 printk(KERN_WARNING SFX "FIFO not ready (stream %d)\n", str->dsd);
122 return -EIO;
123}
124
Takashi Iwaic7aad3c2011-05-03 17:02:35 +0200125/* sync for FIFO ready/empty for all linked streams;
126 * clear paused flag when FIFO gets ready again
127 */
128static int lola_sync_wait_for_fifo(struct lola *chip,
129 struct snd_pcm_substream *substream,
130 bool ready)
131{
132 unsigned int val = ready ? LOLA_DSD_STS_FIFORDY : 0;
133 unsigned long end_time = jiffies + msecs_to_jiffies(200);
134 struct snd_pcm_substream *s;
135 int pending = 0;
136
137 while (time_before(jiffies, end_time)) {
138 pending = 0;
139 snd_pcm_group_for_each_entry(s, substream) {
140 struct lola_stream *str;
141 if (s->pcm->card != substream->pcm->card)
142 continue;
143 str = lola_get_stream(s);
144 if (str->prepared && str->paused) {
145 unsigned int reg;
146 reg = lola_dsd_read(chip, str->dsd, STS);
147 if ((reg & LOLA_DSD_STS_FIFORDY) != val) {
148 pending = str->dsd + 1;
149 break;
150 }
151 if (ready)
152 str->paused = 0;
153 }
154 }
155 if (!pending)
156 return 0;
157 msleep(1);
158 }
159 printk(KERN_WARNING SFX "FIFO not ready (pending %d)\n", pending - 1);
160 return -EIO;
161}
162
163/* finish pause - prepare for a new resume */
164static void lola_sync_pause(struct lola *chip,
165 struct snd_pcm_substream *substream)
166{
167 struct snd_pcm_substream *s;
168
169 lola_sync_wait_for_fifo(chip, substream, false);
170 snd_pcm_group_for_each_entry(s, substream) {
171 struct lola_stream *str;
172 if (s->pcm->card != substream->pcm->card)
173 continue;
174 str = lola_get_stream(s);
175 if (str->paused && str->prepared)
176 lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRUN |
177 LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE);
178 }
179 lola_sync_wait_for_fifo(chip, substream, true);
180}
181
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200182static void lola_stream_reset(struct lola *chip, struct lola_stream *str)
183{
184 if (str->prepared) {
Takashi Iwaic7aad3c2011-05-03 17:02:35 +0200185 if (str->paused)
186 lola_sync_pause(chip, str->substream);
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200187 str->prepared = 0;
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200188 lola_dsd_write(chip, str->dsd, CTL,
189 LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE);
190 lola_stream_wait_for_fifo(chip, str, false);
191 lola_stream_clear_pending_irq(chip, str);
192 lola_dsd_write(chip, str->dsd, CTL, LOLA_DSD_CTL_SRST);
193 lola_dsd_write(chip, str->dsd, LVI, 0);
194 lola_dsd_write(chip, str->dsd, BDPU, 0);
195 lola_dsd_write(chip, str->dsd, BDPL, 0);
196 wait_for_srst_clear(chip, str);
197 }
198}
199
Takashi Iwaid43f30102011-05-03 16:14:46 +0200200static struct snd_pcm_hardware lola_pcm_hw = {
201 .info = (SNDRV_PCM_INFO_MMAP |
202 SNDRV_PCM_INFO_INTERLEAVED |
203 SNDRV_PCM_INFO_BLOCK_TRANSFER |
204 SNDRV_PCM_INFO_MMAP_VALID |
205 SNDRV_PCM_INFO_PAUSE),
206 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
207 SNDRV_PCM_FMTBIT_S24_LE |
208 SNDRV_PCM_FMTBIT_S32_LE |
209 SNDRV_PCM_FMTBIT_FLOAT_LE),
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200210 .rates = SNDRV_PCM_RATE_8000_192000,
211 .rate_min = 8000,
212 .rate_max = 192000,
Takashi Iwaid43f30102011-05-03 16:14:46 +0200213 .channels_min = 1,
214 .channels_max = 2,
215 .buffer_bytes_max = LOLA_MAX_BUF_SIZE,
216 .period_bytes_min = 128,
217 .period_bytes_max = LOLA_MAX_BUF_SIZE / 2,
218 .periods_min = 2,
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200219 .periods_max = LOLA_MAX_BDL_ENTRIES,
Takashi Iwaid43f30102011-05-03 16:14:46 +0200220 .fifo_size = 0,
221};
222
223static int lola_pcm_open(struct snd_pcm_substream *substream)
224{
225 struct lola *chip = snd_pcm_substream_chip(substream);
226 struct lola_pcm *pcm = lola_get_pcm(substream);
227 struct lola_stream *str = lola_get_stream(substream);
228 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200229
230 mutex_lock(&chip->open_mutex);
231 if (str->opened) {
232 mutex_unlock(&chip->open_mutex);
233 return -EBUSY;
234 }
Takashi Iwaid43f30102011-05-03 16:14:46 +0200235 str->substream = substream;
236 str->master = NULL;
237 str->opened = 1;
238 runtime->hw = lola_pcm_hw;
239 runtime->hw.channels_max = pcm->num_streams - str->index;
Takashi Iwai7e79f222011-05-03 16:59:27 +0200240 if (chip->sample_rate) {
241 /* sample rate is locked */
242 runtime->hw.rate_min = chip->sample_rate;
243 runtime->hw.rate_max = chip->sample_rate;
244 } else {
245 runtime->hw.rate_min = chip->sample_rate_min;
246 runtime->hw.rate_max = chip->sample_rate_max;
247 }
248 chip->ref_count_rate++;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200249 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Takashi Iwai7e79f222011-05-03 16:59:27 +0200250 /* period size = multiple of chip->granularity (8, 16 or 32 frames)*/
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200251 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
Takashi Iwai8bd172d2011-05-03 16:51:56 +0200252 chip->granularity);
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200253 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Takashi Iwai8bd172d2011-05-03 16:51:56 +0200254 chip->granularity);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200255 mutex_unlock(&chip->open_mutex);
256 return 0;
257}
258
259static void lola_cleanup_slave_streams(struct lola_pcm *pcm,
260 struct lola_stream *str)
261{
262 int i;
263 for (i = str->index + 1; i < pcm->num_streams; i++) {
264 struct lola_stream *s = &pcm->streams[i];
265 if (s->master != str)
266 break;
267 s->master = NULL;
268 s->opened = 0;
269 }
270}
271
272static int lola_pcm_close(struct snd_pcm_substream *substream)
273{
274 struct lola *chip = snd_pcm_substream_chip(substream);
275 struct lola_stream *str = lola_get_stream(substream);
276
277 mutex_lock(&chip->open_mutex);
278 if (str->substream == substream) {
279 str->substream = NULL;
280 str->opened = 0;
281 }
Takashi Iwai7e79f222011-05-03 16:59:27 +0200282 if (--chip->ref_count_rate == 0) {
283 /* release sample rate */
284 chip->sample_rate = 0;
285 }
Takashi Iwaid43f30102011-05-03 16:14:46 +0200286 mutex_unlock(&chip->open_mutex);
287 return 0;
288}
289
290static int lola_pcm_hw_params(struct snd_pcm_substream *substream,
291 struct snd_pcm_hw_params *hw_params)
292{
293 struct lola_stream *str = lola_get_stream(substream);
294
295 str->bufsize = 0;
296 str->period_bytes = 0;
297 str->format_verb = 0;
298 return snd_pcm_lib_malloc_pages(substream,
299 params_buffer_bytes(hw_params));
300}
301
302static int lola_pcm_hw_free(struct snd_pcm_substream *substream)
303{
304 struct lola *chip = snd_pcm_substream_chip(substream);
305 struct lola_pcm *pcm = lola_get_pcm(substream);
306 struct lola_stream *str = lola_get_stream(substream);
307
308 mutex_lock(&chip->open_mutex);
309 lola_stream_reset(chip, str);
310 lola_cleanup_slave_streams(pcm, str);
311 mutex_unlock(&chip->open_mutex);
312 return snd_pcm_lib_free_pages(substream);
313}
314
315/*
316 * set up a BDL entry
317 */
318static int setup_bdle(struct snd_pcm_substream *substream,
319 struct lola_stream *str, u32 **bdlp,
320 int ofs, int size)
321{
322 u32 *bdl = *bdlp;
323
324 while (size > 0) {
325 dma_addr_t addr;
326 int chunk;
327
328 if (str->frags >= LOLA_MAX_BDL_ENTRIES)
329 return -EINVAL;
330
Takashi Iwai972505c2011-05-03 16:50:51 +0200331 addr = snd_pcm_sgbuf_get_addr(substream, ofs);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200332 /* program the address field of the BDL entry */
333 bdl[0] = cpu_to_le32((u32)addr);
334 bdl[1] = cpu_to_le32(upper_32_bits(addr));
335 /* program the size field of the BDL entry */
Takashi Iwai972505c2011-05-03 16:50:51 +0200336 chunk = snd_pcm_sgbuf_get_chunk_size(substream, ofs, size);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200337 bdl[2] = cpu_to_le32(chunk);
338 /* program the IOC to enable interrupt
339 * only when the whole fragment is processed
340 */
341 size -= chunk;
342 bdl[3] = size ? 0 : cpu_to_le32(0x01);
343 bdl += 4;
344 str->frags++;
345 ofs += chunk;
346 }
347 *bdlp = bdl;
348 return ofs;
349}
350
351/*
352 * set up BDL entries
353 */
Takashi Iwai333ff392011-05-03 16:41:02 +0200354static int lola_setup_periods(struct lola *chip, struct lola_pcm *pcm,
Takashi Iwaid43f30102011-05-03 16:14:46 +0200355 struct snd_pcm_substream *substream,
356 struct lola_stream *str)
357{
358 u32 *bdl;
359 int i, ofs, periods, period_bytes;
360
361 period_bytes = str->period_bytes;
362 periods = str->bufsize / period_bytes;
363
364 /* program the initial BDL entries */
Takashi Iwai333ff392011-05-03 16:41:02 +0200365 bdl = (u32 *)(pcm->bdl.area + LOLA_BDL_ENTRY_SIZE * str->index);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200366 ofs = 0;
367 str->frags = 0;
368 for (i = 0; i < periods; i++) {
369 ofs = setup_bdle(substream, str, &bdl, ofs, period_bytes);
370 if (ofs < 0)
371 goto error;
372 }
373 return 0;
374
375 error:
376 snd_printk(KERN_ERR SFX "Too many BDL entries: buffer=%d, period=%d\n",
377 str->bufsize, period_bytes);
378 return -EINVAL;
379}
380
381static unsigned int lola_get_format_verb(struct snd_pcm_substream *substream)
382{
383 unsigned int verb;
384
385 switch (substream->runtime->format) {
386 case SNDRV_PCM_FORMAT_S16_LE:
387 verb = 0x00000000;
388 break;
389 case SNDRV_PCM_FORMAT_S24_LE:
390 verb = 0x00000200;
391 break;
392 case SNDRV_PCM_FORMAT_S32_LE:
393 verb = 0x00000300;
394 break;
395 case SNDRV_PCM_FORMAT_FLOAT_LE:
396 verb = 0x00001300;
397 break;
398 default:
399 return 0;
400 }
401 verb |= substream->runtime->channels;
402 return verb;
403}
404
405static int lola_set_stream_config(struct lola *chip,
406 struct lola_stream *str,
407 int channels)
408{
409 int i, err;
410 unsigned int verb, val;
411
412 /* set format info for all channels
413 * (with only one command for the first channel)
414 */
415 err = lola_codec_read(chip, str->nid, LOLA_VERB_SET_STREAM_FORMAT,
416 str->format_verb, 0, &val, NULL);
417 if (err < 0) {
418 printk(KERN_ERR SFX "Cannot set stream format 0x%x\n",
419 str->format_verb);
420 return err;
421 }
422
423 /* update stream - channel config */
424 for (i = 0; i < channels; i++) {
425 verb = (str->index << 6) | i;
426 err = lola_codec_read(chip, str[i].nid,
427 LOLA_VERB_SET_CHANNEL_STREAMID, 0, verb,
428 &val, NULL);
429 if (err < 0) {
430 printk(KERN_ERR SFX "Cannot set stream channel %d\n", i);
431 return err;
432 }
433 }
434 return 0;
435}
436
437/*
438 * set up the SD for streaming
439 */
Takashi Iwai333ff392011-05-03 16:41:02 +0200440static int lola_setup_controller(struct lola *chip, struct lola_pcm *pcm,
441 struct lola_stream *str)
Takashi Iwaid43f30102011-05-03 16:14:46 +0200442{
Takashi Iwai333ff392011-05-03 16:41:02 +0200443 dma_addr_t bdl;
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200444
445 if (str->prepared)
446 return -EINVAL;
447
Takashi Iwaid43f30102011-05-03 16:14:46 +0200448 /* set up BDL */
Takashi Iwai333ff392011-05-03 16:41:02 +0200449 bdl = pcm->bdl.addr + LOLA_BDL_ENTRY_SIZE * str->index;
450 lola_dsd_write(chip, str->dsd, BDPL, (u32)bdl);
451 lola_dsd_write(chip, str->dsd, BDPU, upper_32_bits(bdl));
Takashi Iwaid43f30102011-05-03 16:14:46 +0200452 /* program the stream LVI (last valid index) of the BDL */
453 lola_dsd_write(chip, str->dsd, LVI, str->frags - 1);
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200454 lola_stream_clear_pending_irq(chip, str);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200455
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200456 lola_dsd_write(chip, str->dsd, CTL,
457 LOLA_DSD_CTL_IOCE | LOLA_DSD_CTL_DEIE | LOLA_DSD_CTL_SRUN);
458
459 str->prepared = 1;
460
461 return lola_stream_wait_for_fifo(chip, str, true);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200462}
463
464static int lola_pcm_prepare(struct snd_pcm_substream *substream)
465{
466 struct lola *chip = snd_pcm_substream_chip(substream);
467 struct lola_pcm *pcm = lola_get_pcm(substream);
468 struct lola_stream *str = lola_get_stream(substream);
469 struct snd_pcm_runtime *runtime = substream->runtime;
470 unsigned int bufsize, period_bytes, format_verb;
471 int i, err;
472
473 mutex_lock(&chip->open_mutex);
474 lola_stream_reset(chip, str);
475 lola_cleanup_slave_streams(pcm, str);
Takashi Iwai2db30022011-05-03 17:05:08 +0200476 if (str->index + runtime->channels > pcm->num_streams) {
Takashi Iwaid43f30102011-05-03 16:14:46 +0200477 mutex_unlock(&chip->open_mutex);
478 return -EINVAL;
479 }
480 for (i = 1; i < runtime->channels; i++) {
481 str[i].master = str;
482 str[i].opened = 1;
483 }
484 mutex_unlock(&chip->open_mutex);
485
486 bufsize = snd_pcm_lib_buffer_bytes(substream);
487 period_bytes = snd_pcm_lib_period_bytes(substream);
488 format_verb = lola_get_format_verb(substream);
489
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200490 str->bufsize = bufsize;
491 str->period_bytes = period_bytes;
492 str->format_verb = format_verb;
493
494 err = lola_setup_periods(chip, pcm, substream, str);
495 if (err < 0)
496 return err;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200497
Takashi Iwai7e79f222011-05-03 16:59:27 +0200498 err = lola_set_sample_rate(chip, runtime->rate);
499 if (err < 0)
500 return err;
501 chip->sample_rate = runtime->rate; /* sample rate gets locked */
502
Takashi Iwaid43f30102011-05-03 16:14:46 +0200503 err = lola_set_stream_config(chip, str, runtime->channels);
504 if (err < 0)
505 return err;
506
Takashi Iwai0f8f56c2011-05-03 16:47:03 +0200507 err = lola_setup_controller(chip, pcm, str);
508 if (err < 0) {
509 lola_stream_reset(chip, str);
510 return err;
511 }
512
513 return 0;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200514}
515
516static int lola_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
517{
518 struct lola *chip = snd_pcm_substream_chip(substream);
519 struct lola_stream *str;
520 struct snd_pcm_substream *s;
521 unsigned int start;
522 unsigned int tstamp;
Takashi Iwai7e79f222011-05-03 16:59:27 +0200523 bool sync_streams;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200524
525 switch (cmd) {
526 case SNDRV_PCM_TRIGGER_START:
527 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
528 case SNDRV_PCM_TRIGGER_RESUME:
529 start = 1;
530 break;
531 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
532 case SNDRV_PCM_TRIGGER_SUSPEND:
533 case SNDRV_PCM_TRIGGER_STOP:
534 start = 0;
535 break;
536 default:
537 return -EINVAL;
538 }
539
Takashi Iwai7e79f222011-05-03 16:59:27 +0200540 /*
541 * sample correct synchronization is only needed starting several
Takashi Iwai2db30022011-05-03 17:05:08 +0200542 * streams. On stop or if only one stream do as quick as possible
Takashi Iwai7e79f222011-05-03 16:59:27 +0200543 */
544 sync_streams = (start && snd_pcm_stream_linked(substream));
545 tstamp = lola_get_tstamp(chip, !sync_streams);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200546 spin_lock(&chip->reg_lock);
547 snd_pcm_group_for_each_entry(s, substream) {
548 if (s->pcm->card != substream->pcm->card)
549 continue;
550 str = lola_get_stream(s);
551 if (start)
552 lola_stream_start(chip, str, tstamp);
553 else
554 lola_stream_stop(chip, str, tstamp);
555 str->running = start;
Takashi Iwai7e79f222011-05-03 16:59:27 +0200556 str->paused = !start;
Takashi Iwaid43f30102011-05-03 16:14:46 +0200557 snd_pcm_trigger_done(s, substream);
558 }
559 spin_unlock(&chip->reg_lock);
560 return 0;
561}
562
563static snd_pcm_uframes_t lola_pcm_pointer(struct snd_pcm_substream *substream)
564{
565 struct lola *chip = snd_pcm_substream_chip(substream);
566 struct lola_stream *str = lola_get_stream(substream);
567 unsigned int pos = lola_dsd_read(chip, str->dsd, LPIB);
568
569 if (pos >= str->bufsize)
570 pos = 0;
571 return bytes_to_frames(substream->runtime, pos);
572}
573
574void lola_pcm_update(struct lola *chip, struct lola_pcm *pcm, unsigned int bits)
575{
576 int i;
577
578 for (i = 0; bits && i < pcm->num_streams; i++) {
579 if (bits & (1 << i)) {
580 struct lola_stream *str = &pcm->streams[i];
581 if (str->substream && str->running)
582 snd_pcm_period_elapsed(str->substream);
583 bits &= ~(1 << i);
584 }
585 }
586}
587
588static struct snd_pcm_ops lola_pcm_ops = {
589 .open = lola_pcm_open,
590 .close = lola_pcm_close,
591 .ioctl = snd_pcm_lib_ioctl,
592 .hw_params = lola_pcm_hw_params,
593 .hw_free = lola_pcm_hw_free,
594 .prepare = lola_pcm_prepare,
595 .trigger = lola_pcm_trigger,
596 .pointer = lola_pcm_pointer,
Takashi Iwai972505c2011-05-03 16:50:51 +0200597 .page = snd_pcm_sgbuf_ops_page,
Takashi Iwaid43f30102011-05-03 16:14:46 +0200598};
599
Bill Pembertone23e7a12012-12-06 12:35:10 -0500600int lola_create_pcm(struct lola *chip)
Takashi Iwaid43f30102011-05-03 16:14:46 +0200601{
602 struct snd_pcm *pcm;
603 int i, err;
604
Takashi Iwai333ff392011-05-03 16:41:02 +0200605 for (i = 0; i < 2; i++) {
606 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
607 snd_dma_pci_data(chip->pci),
608 PAGE_SIZE, &chip->pcm[i].bdl);
609 if (err < 0)
610 return err;
611 }
612
Takashi Iwaid43f30102011-05-03 16:14:46 +0200613 err = snd_pcm_new(chip->card, "Digigram Lola", 0,
614 chip->pcm[SNDRV_PCM_STREAM_PLAYBACK].num_streams,
615 chip->pcm[SNDRV_PCM_STREAM_CAPTURE].num_streams,
616 &pcm);
617 if (err < 0)
618 return err;
619 strlcpy(pcm->name, "Digigram Lola", sizeof(pcm->name));
620 pcm->private_data = chip;
621 for (i = 0; i < 2; i++) {
622 if (chip->pcm[i].num_streams)
623 snd_pcm_set_ops(pcm, i, &lola_pcm_ops);
624 }
625 /* buffer pre-allocation */
Takashi Iwai972505c2011-05-03 16:50:51 +0200626 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
Takashi Iwaid43f30102011-05-03 16:14:46 +0200627 snd_dma_pci_data(chip->pci),
628 1024 * 64, 32 * 1024 * 1024);
629 return 0;
630}
631
632void lola_free_pcm(struct lola *chip)
633{
Takashi Iwai333ff392011-05-03 16:41:02 +0200634 snd_dma_free_pages(&chip->pcm[0].bdl);
635 snd_dma_free_pages(&chip->pcm[1].bdl);
Takashi Iwaid43f30102011-05-03 16:14:46 +0200636}
637
638/*
639 */
640
641static int lola_init_stream(struct lola *chip, struct lola_stream *str,
642 int idx, int nid, int dir)
643{
644 unsigned int val;
645 int err;
646
647 str->nid = nid;
648 str->index = idx;
649 str->dsd = idx;
650 if (dir == PLAY)
651 str->dsd += MAX_STREAM_IN_COUNT;
652 err = lola_read_param(chip, nid, LOLA_PAR_AUDIO_WIDGET_CAP, &val);
653 if (err < 0) {
654 printk(KERN_ERR SFX "Can't read wcaps for 0x%x\n", nid);
655 return err;
656 }
657 if (dir == PLAY) {
658 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1) */
659 if ((val & 0x00f00dff) != 0x00000010) {
660 printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
661 val, nid);
662 return -EINVAL;
663 }
664 } else {
665 /* test TYPE and bits 0..11 (no test bit9 : Digital = 0/1)
666 * (bug : ignore bit8: Conn list = 0/1)
667 */
668 if ((val & 0x00f00cff) != 0x00100010) {
669 printk(KERN_ERR SFX "Invalid wcaps 0x%x for 0x%x\n",
670 val, nid);
671 return -EINVAL;
672 }
673 /* test bit9:DIGITAL and bit12:SRC_PRESENT*/
674 if ((val & 0x00001200) == 0x00001200)
675 chip->input_src_caps_mask |= (1 << idx);
676 }
677
678 err = lola_read_param(chip, nid, LOLA_PAR_STREAM_FORMATS, &val);
679 if (err < 0) {
680 printk(KERN_ERR SFX "Can't read FORMATS 0x%x\n", nid);
681 return err;
682 }
683 val &= 3;
684 if (val == 3)
685 str->can_float = true;
686 if (!(val & 1)) {
687 printk(KERN_ERR SFX "Invalid formats 0x%x for 0x%x", val, nid);
688 return -EINVAL;
689 }
690 return 0;
691}
692
Bill Pembertone23e7a12012-12-06 12:35:10 -0500693int lola_init_pcm(struct lola *chip, int dir, int *nidp)
Takashi Iwaid43f30102011-05-03 16:14:46 +0200694{
695 struct lola_pcm *pcm = &chip->pcm[dir];
696 int i, nid, err;
697
698 nid = *nidp;
699 for (i = 0; i < pcm->num_streams; i++, nid++) {
700 err = lola_init_stream(chip, &pcm->streams[i], i, nid, dir);
701 if (err < 0)
702 return err;
703 }
704 *nidp = nid;
705 return 0;
706}