Linux Audio

Check our new training course

Yocto distribution development and maintenance

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