xref: /linux/sound/pci/lola/lola.c (revision 87c9c16317882dd6dbbc07e349bc3223e14f3244)
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/module.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/delay.h>
13 #include <linux/interrupt.h>
14 #include <linux/slab.h>
15 #include <linux/pci.h>
16 #include <sound/core.h>
17 #include <sound/control.h>
18 #include <sound/pcm.h>
19 #include <sound/initval.h>
20 #include "lola.h"
21 
22 /* Standard options */
23 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
24 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
25 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
26 
27 module_param_array(index, int, NULL, 0444);
28 MODULE_PARM_DESC(index, "Index value for Digigram Lola driver.");
29 module_param_array(id, charp, NULL, 0444);
30 MODULE_PARM_DESC(id, "ID string for Digigram Lola driver.");
31 module_param_array(enable, bool, NULL, 0444);
32 MODULE_PARM_DESC(enable, "Enable Digigram Lola driver.");
33 
34 /* Lola-specific options */
35 
36 /* for instance use always max granularity which is compatible
37  * with all sample rates
38  */
39 static int granularity[SNDRV_CARDS] = {
40 	[0 ... (SNDRV_CARDS - 1)] = LOLA_GRANULARITY_MAX
41 };
42 
43 /* below a sample_rate of 16kHz the analogue audio quality is NOT excellent */
44 static int sample_rate_min[SNDRV_CARDS] = {
45 	[0 ... (SNDRV_CARDS - 1) ] = 16000
46 };
47 
48 module_param_array(granularity, int, NULL, 0444);
49 MODULE_PARM_DESC(granularity, "Granularity value");
50 module_param_array(sample_rate_min, int, NULL, 0444);
51 MODULE_PARM_DESC(sample_rate_min, "Minimal sample rate");
52 
53 /*
54  */
55 
56 MODULE_LICENSE("GPL");
57 MODULE_DESCRIPTION("Digigram Lola driver");
58 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
59 
60 #ifdef CONFIG_SND_DEBUG_VERBOSE
61 static int debug;
62 module_param(debug, int, 0644);
63 #define verbose_debug(fmt, args...)			\
64 	do { if (debug > 1) pr_debug(SFX fmt, ##args); } while (0)
65 #else
66 #define verbose_debug(fmt, args...)
67 #endif
68 
69 /*
70  * pseudo-codec read/write via CORB/RIRB
71  */
72 
73 static int corb_send_verb(struct lola *chip, unsigned int nid,
74 			  unsigned int verb, unsigned int data,
75 			  unsigned int extdata)
76 {
77 	unsigned long flags;
78 	int ret = -EIO;
79 
80 	chip->last_cmd_nid = nid;
81 	chip->last_verb = verb;
82 	chip->last_data = data;
83 	chip->last_extdata = extdata;
84 	data |= (nid << 20) | (verb << 8);
85 
86 	spin_lock_irqsave(&chip->reg_lock, flags);
87 	if (chip->rirb.cmds < LOLA_CORB_ENTRIES - 1) {
88 		unsigned int wp = chip->corb.wp + 1;
89 		wp %= LOLA_CORB_ENTRIES;
90 		chip->corb.wp = wp;
91 		chip->corb.buf[wp * 2] = cpu_to_le32(data);
92 		chip->corb.buf[wp * 2 + 1] = cpu_to_le32(extdata);
93 		lola_writew(chip, BAR0, CORBWP, wp);
94 		chip->rirb.cmds++;
95 		smp_wmb();
96 		ret = 0;
97 	}
98 	spin_unlock_irqrestore(&chip->reg_lock, flags);
99 	return ret;
100 }
101 
102 static void lola_queue_unsol_event(struct lola *chip, unsigned int res,
103 				   unsigned int res_ex)
104 {
105 	lola_update_ext_clock_freq(chip, res);
106 }
107 
108 /* retrieve RIRB entry - called from interrupt handler */
109 static void lola_update_rirb(struct lola *chip)
110 {
111 	unsigned int rp, wp;
112 	u32 res, res_ex;
113 
114 	wp = lola_readw(chip, BAR0, RIRBWP);
115 	if (wp == chip->rirb.wp)
116 		return;
117 	chip->rirb.wp = wp;
118 
119 	while (chip->rirb.rp != wp) {
120 		chip->rirb.rp++;
121 		chip->rirb.rp %= LOLA_CORB_ENTRIES;
122 
123 		rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */
124 		res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]);
125 		res = le32_to_cpu(chip->rirb.buf[rp]);
126 		if (res_ex & LOLA_RIRB_EX_UNSOL_EV)
127 			lola_queue_unsol_event(chip, res, res_ex);
128 		else if (chip->rirb.cmds) {
129 			chip->res = res;
130 			chip->res_ex = res_ex;
131 			smp_wmb();
132 			chip->rirb.cmds--;
133 		}
134 	}
135 }
136 
137 static int rirb_get_response(struct lola *chip, unsigned int *val,
138 			     unsigned int *extval)
139 {
140 	unsigned long timeout;
141 
142  again:
143 	timeout = jiffies + msecs_to_jiffies(1000);
144 	for (;;) {
145 		if (chip->polling_mode) {
146 			spin_lock_irq(&chip->reg_lock);
147 			lola_update_rirb(chip);
148 			spin_unlock_irq(&chip->reg_lock);
149 		}
150 		if (!chip->rirb.cmds) {
151 			*val = chip->res;
152 			if (extval)
153 				*extval = chip->res_ex;
154 			verbose_debug("get_response: %x, %x\n",
155 				      chip->res, chip->res_ex);
156 			if (chip->res_ex & LOLA_RIRB_EX_ERROR) {
157 				dev_warn(chip->card->dev, "RIRB ERROR: "
158 				       "NID=%x, verb=%x, data=%x, ext=%x\n",
159 				       chip->last_cmd_nid,
160 				       chip->last_verb, chip->last_data,
161 				       chip->last_extdata);
162 				return -EIO;
163 			}
164 			return 0;
165 		}
166 		if (time_after(jiffies, timeout))
167 			break;
168 		udelay(20);
169 		cond_resched();
170 	}
171 	dev_warn(chip->card->dev, "RIRB response error\n");
172 	if (!chip->polling_mode) {
173 		dev_warn(chip->card->dev, "switching to polling mode\n");
174 		chip->polling_mode = 1;
175 		goto again;
176 	}
177 	return -EIO;
178 }
179 
180 /* aynchronous write of a codec verb with data */
181 int lola_codec_write(struct lola *chip, unsigned int nid, unsigned int verb,
182 		     unsigned int data, unsigned int extdata)
183 {
184 	verbose_debug("codec_write NID=%x, verb=%x, data=%x, ext=%x\n",
185 		      nid, verb, data, extdata);
186 	return corb_send_verb(chip, nid, verb, data, extdata);
187 }
188 
189 /* write a codec verb with data and read the returned status */
190 int lola_codec_read(struct lola *chip, unsigned int nid, unsigned int verb,
191 		    unsigned int data, unsigned int extdata,
192 		    unsigned int *val, unsigned int *extval)
193 {
194 	int err;
195 
196 	verbose_debug("codec_read NID=%x, verb=%x, data=%x, ext=%x\n",
197 		      nid, verb, data, extdata);
198 	err = corb_send_verb(chip, nid, verb, data, extdata);
199 	if (err < 0)
200 		return err;
201 	err = rirb_get_response(chip, val, extval);
202 	return err;
203 }
204 
205 /* flush all pending codec writes */
206 int lola_codec_flush(struct lola *chip)
207 {
208 	unsigned int tmp;
209 	return rirb_get_response(chip, &tmp, NULL);
210 }
211 
212 /*
213  * interrupt handler
214  */
215 static irqreturn_t lola_interrupt(int irq, void *dev_id)
216 {
217 	struct lola *chip = dev_id;
218 	unsigned int notify_ins, notify_outs, error_ins, error_outs;
219 	int handled = 0;
220 	int i;
221 
222 	notify_ins = notify_outs = error_ins = error_outs = 0;
223 	spin_lock(&chip->reg_lock);
224 	for (;;) {
225 		unsigned int status, in_sts, out_sts;
226 		unsigned int reg;
227 
228 		status = lola_readl(chip, BAR1, DINTSTS);
229 		if (!status || status == -1)
230 			break;
231 
232 		in_sts = lola_readl(chip, BAR1, DIINTSTS);
233 		out_sts = lola_readl(chip, BAR1, DOINTSTS);
234 
235 		/* clear Input Interrupts */
236 		for (i = 0; in_sts && i < chip->pcm[CAPT].num_streams; i++) {
237 			if (!(in_sts & (1 << i)))
238 				continue;
239 			in_sts &= ~(1 << i);
240 			reg = lola_dsd_read(chip, i, STS);
241 			if (reg & LOLA_DSD_STS_DESE) /* error */
242 				error_ins |= (1 << i);
243 			if (reg & LOLA_DSD_STS_BCIS) /* notify */
244 				notify_ins |= (1 << i);
245 			/* clear */
246 			lola_dsd_write(chip, i, STS, reg);
247 		}
248 
249 		/* clear Output Interrupts */
250 		for (i = 0; out_sts && i < chip->pcm[PLAY].num_streams; i++) {
251 			if (!(out_sts & (1 << i)))
252 				continue;
253 			out_sts &= ~(1 << i);
254 			reg = lola_dsd_read(chip, i + MAX_STREAM_IN_COUNT, STS);
255 			if (reg & LOLA_DSD_STS_DESE) /* error */
256 				error_outs |= (1 << i);
257 			if (reg & LOLA_DSD_STS_BCIS) /* notify */
258 				notify_outs |= (1 << i);
259 			lola_dsd_write(chip, i + MAX_STREAM_IN_COUNT, STS, reg);
260 		}
261 
262 		if (status & LOLA_DINT_CTRL) {
263 			unsigned char rbsts; /* ring status is byte access */
264 			rbsts = lola_readb(chip, BAR0, RIRBSTS);
265 			rbsts &= LOLA_RIRB_INT_MASK;
266 			if (rbsts)
267 				lola_writeb(chip, BAR0, RIRBSTS, rbsts);
268 			rbsts = lola_readb(chip, BAR0, CORBSTS);
269 			rbsts &= LOLA_CORB_INT_MASK;
270 			if (rbsts)
271 				lola_writeb(chip, BAR0, CORBSTS, rbsts);
272 
273 			lola_update_rirb(chip);
274 		}
275 
276 		if (status & (LOLA_DINT_FIFOERR | LOLA_DINT_MUERR)) {
277 			/* clear global fifo error interrupt */
278 			lola_writel(chip, BAR1, DINTSTS,
279 				    (status & (LOLA_DINT_FIFOERR | LOLA_DINT_MUERR)));
280 		}
281 		handled = 1;
282 	}
283 	spin_unlock(&chip->reg_lock);
284 
285 	lola_pcm_update(chip, &chip->pcm[CAPT], notify_ins);
286 	lola_pcm_update(chip, &chip->pcm[PLAY], notify_outs);
287 
288 	return IRQ_RETVAL(handled);
289 }
290 
291 
292 /*
293  * controller
294  */
295 static int reset_controller(struct lola *chip)
296 {
297 	unsigned int gctl = lola_readl(chip, BAR0, GCTL);
298 	unsigned long end_time;
299 
300 	if (gctl) {
301 		/* to be sure */
302 		lola_writel(chip, BAR1, BOARD_MODE, 0);
303 		return 0;
304 	}
305 
306 	chip->cold_reset = 1;
307 	lola_writel(chip, BAR0, GCTL, LOLA_GCTL_RESET);
308 	end_time = jiffies + msecs_to_jiffies(200);
309 	do {
310 		msleep(1);
311 		gctl = lola_readl(chip, BAR0, GCTL);
312 		if (gctl)
313 			break;
314 	} while (time_before(jiffies, end_time));
315 	if (!gctl) {
316 		dev_err(chip->card->dev, "cannot reset controller\n");
317 		return -EIO;
318 	}
319 	return 0;
320 }
321 
322 static void lola_irq_enable(struct lola *chip)
323 {
324 	unsigned int val;
325 
326 	/* enalbe all I/O streams */
327 	val = (1 << chip->pcm[PLAY].num_streams) - 1;
328 	lola_writel(chip, BAR1, DOINTCTL, val);
329 	val = (1 << chip->pcm[CAPT].num_streams) - 1;
330 	lola_writel(chip, BAR1, DIINTCTL, val);
331 
332 	/* enable global irqs */
333 	val = LOLA_DINT_GLOBAL | LOLA_DINT_CTRL | LOLA_DINT_FIFOERR |
334 		LOLA_DINT_MUERR;
335 	lola_writel(chip, BAR1, DINTCTL, val);
336 }
337 
338 static void lola_irq_disable(struct lola *chip)
339 {
340 	lola_writel(chip, BAR1, DINTCTL, 0);
341 	lola_writel(chip, BAR1, DIINTCTL, 0);
342 	lola_writel(chip, BAR1, DOINTCTL, 0);
343 }
344 
345 static int setup_corb_rirb(struct lola *chip)
346 {
347 	int err;
348 	unsigned char tmp;
349 	unsigned long end_time;
350 
351 	err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
352 				  &chip->pci->dev,
353 				  PAGE_SIZE, &chip->rb);
354 	if (err < 0)
355 		return err;
356 
357 	chip->corb.addr = chip->rb.addr;
358 	chip->corb.buf = (__le32 *)chip->rb.area;
359 	chip->rirb.addr = chip->rb.addr + 2048;
360 	chip->rirb.buf = (__le32 *)(chip->rb.area + 2048);
361 
362 	/* disable ringbuffer DMAs */
363 	lola_writeb(chip, BAR0, RIRBCTL, 0);
364 	lola_writeb(chip, BAR0, CORBCTL, 0);
365 
366 	end_time = jiffies + msecs_to_jiffies(200);
367 	do {
368 		if (!lola_readb(chip, BAR0, RIRBCTL) &&
369 		    !lola_readb(chip, BAR0, CORBCTL))
370 			break;
371 		msleep(1);
372 	} while (time_before(jiffies, end_time));
373 
374 	/* CORB set up */
375 	lola_writel(chip, BAR0, CORBLBASE, (u32)chip->corb.addr);
376 	lola_writel(chip, BAR0, CORBUBASE, upper_32_bits(chip->corb.addr));
377 	/* set the corb size to 256 entries */
378 	lola_writeb(chip, BAR0, CORBSIZE, 0x02);
379 	/* set the corb write pointer to 0 */
380 	lola_writew(chip, BAR0, CORBWP, 0);
381 	/* reset the corb hw read pointer */
382 	lola_writew(chip, BAR0, CORBRP, LOLA_RBRWP_CLR);
383 	/* enable corb dma */
384 	lola_writeb(chip, BAR0, CORBCTL, LOLA_RBCTL_DMA_EN);
385 	/* clear flags if set */
386 	tmp = lola_readb(chip, BAR0, CORBSTS) & LOLA_CORB_INT_MASK;
387 	if (tmp)
388 		lola_writeb(chip, BAR0, CORBSTS, tmp);
389 	chip->corb.wp = 0;
390 
391 	/* RIRB set up */
392 	lola_writel(chip, BAR0, RIRBLBASE, (u32)chip->rirb.addr);
393 	lola_writel(chip, BAR0, RIRBUBASE, upper_32_bits(chip->rirb.addr));
394 	/* set the rirb size to 256 entries */
395 	lola_writeb(chip, BAR0, RIRBSIZE, 0x02);
396 	/* reset the rirb hw write pointer */
397 	lola_writew(chip, BAR0, RIRBWP, LOLA_RBRWP_CLR);
398 	/* set N=1, get RIRB response interrupt for new entry */
399 	lola_writew(chip, BAR0, RINTCNT, 1);
400 	/* enable rirb dma and response irq */
401 	lola_writeb(chip, BAR0, RIRBCTL, LOLA_RBCTL_DMA_EN | LOLA_RBCTL_IRQ_EN);
402 	/* clear flags if set */
403 	tmp =  lola_readb(chip, BAR0, RIRBSTS) & LOLA_RIRB_INT_MASK;
404 	if (tmp)
405 		lola_writeb(chip, BAR0, RIRBSTS, tmp);
406 	chip->rirb.rp = chip->rirb.cmds = 0;
407 
408 	return 0;
409 }
410 
411 static void stop_corb_rirb(struct lola *chip)
412 {
413 	/* disable ringbuffer DMAs */
414 	lola_writeb(chip, BAR0, RIRBCTL, 0);
415 	lola_writeb(chip, BAR0, CORBCTL, 0);
416 }
417 
418 static void lola_reset_setups(struct lola *chip)
419 {
420 	/* update the granularity */
421 	lola_set_granularity(chip, chip->granularity, true);
422 	/* update the sample clock */
423 	lola_set_clock_index(chip, chip->clock.cur_index);
424 	/* enable unsolicited events of the clock widget */
425 	lola_enable_clock_events(chip);
426 	/* update the analog gains */
427 	lola_setup_all_analog_gains(chip, CAPT, false); /* input, update */
428 	/* update SRC configuration if applicable */
429 	lola_set_src_config(chip, chip->input_src_mask, false);
430 	/* update the analog outputs */
431 	lola_setup_all_analog_gains(chip, PLAY, false); /* output, update */
432 }
433 
434 static int lola_parse_tree(struct lola *chip)
435 {
436 	unsigned int val;
437 	int nid, err;
438 
439 	err = lola_read_param(chip, 0, LOLA_PAR_VENDOR_ID, &val);
440 	if (err < 0) {
441 		dev_err(chip->card->dev, "Can't read VENDOR_ID\n");
442 		return err;
443 	}
444 	val >>= 16;
445 	if (val != 0x1369) {
446 		dev_err(chip->card->dev, "Unknown codec vendor 0x%x\n", val);
447 		return -EINVAL;
448 	}
449 
450 	err = lola_read_param(chip, 1, LOLA_PAR_FUNCTION_TYPE, &val);
451 	if (err < 0) {
452 		dev_err(chip->card->dev, "Can't read FUNCTION_TYPE\n");
453 		return err;
454 	}
455 	if (val != 1) {
456 		dev_err(chip->card->dev, "Unknown function type %d\n", val);
457 		return -EINVAL;
458 	}
459 
460 	err = lola_read_param(chip, 1, LOLA_PAR_SPECIFIC_CAPS, &val);
461 	if (err < 0) {
462 		dev_err(chip->card->dev, "Can't read SPECCAPS\n");
463 		return err;
464 	}
465 	chip->lola_caps = val;
466 	chip->pin[CAPT].num_pins = LOLA_AFG_INPUT_PIN_COUNT(chip->lola_caps);
467 	chip->pin[PLAY].num_pins = LOLA_AFG_OUTPUT_PIN_COUNT(chip->lola_caps);
468 	dev_dbg(chip->card->dev, "speccaps=0x%x, pins in=%d, out=%d\n",
469 		    chip->lola_caps,
470 		    chip->pin[CAPT].num_pins, chip->pin[PLAY].num_pins);
471 
472 	if (chip->pin[CAPT].num_pins > MAX_AUDIO_INOUT_COUNT ||
473 	    chip->pin[PLAY].num_pins > MAX_AUDIO_INOUT_COUNT) {
474 		dev_err(chip->card->dev, "Invalid Lola-spec caps 0x%x\n", val);
475 		return -EINVAL;
476 	}
477 
478 	nid = 0x02;
479 	err = lola_init_pcm(chip, CAPT, &nid);
480 	if (err < 0)
481 		return err;
482 	err = lola_init_pcm(chip, PLAY, &nid);
483 	if (err < 0)
484 		return err;
485 
486 	err = lola_init_pins(chip, CAPT, &nid);
487 	if (err < 0)
488 		return err;
489 	err = lola_init_pins(chip, PLAY, &nid);
490 	if (err < 0)
491 		return err;
492 
493 	if (LOLA_AFG_CLOCK_WIDGET_PRESENT(chip->lola_caps)) {
494 		err = lola_init_clock_widget(chip, nid);
495 		if (err < 0)
496 			return err;
497 		nid++;
498 	}
499 	if (LOLA_AFG_MIXER_WIDGET_PRESENT(chip->lola_caps)) {
500 		err = lola_init_mixer_widget(chip, nid);
501 		if (err < 0)
502 			return err;
503 		nid++;
504 	}
505 
506 	/* enable unsolicited events of the clock widget */
507 	err = lola_enable_clock_events(chip);
508 	if (err < 0)
509 		return err;
510 
511 	/* if last ResetController was not a ColdReset, we don't know
512 	 * the state of the card; initialize here again
513 	 */
514 	if (!chip->cold_reset) {
515 		lola_reset_setups(chip);
516 		chip->cold_reset = 1;
517 	} else {
518 		/* set the granularity if it is not the default */
519 		if (chip->granularity != LOLA_GRANULARITY_MIN)
520 			lola_set_granularity(chip, chip->granularity, true);
521 	}
522 
523 	return 0;
524 }
525 
526 static void lola_stop_hw(struct lola *chip)
527 {
528 	stop_corb_rirb(chip);
529 	lola_irq_disable(chip);
530 }
531 
532 static void lola_free(struct lola *chip)
533 {
534 	if (chip->initialized)
535 		lola_stop_hw(chip);
536 	lola_free_pcm(chip);
537 	lola_free_mixer(chip);
538 	if (chip->irq >= 0)
539 		free_irq(chip->irq, (void *)chip);
540 	iounmap(chip->bar[0].remap_addr);
541 	iounmap(chip->bar[1].remap_addr);
542 	if (chip->rb.area)
543 		snd_dma_free_pages(&chip->rb);
544 	pci_release_regions(chip->pci);
545 	pci_disable_device(chip->pci);
546 	kfree(chip);
547 }
548 
549 static int lola_dev_free(struct snd_device *device)
550 {
551 	lola_free(device->device_data);
552 	return 0;
553 }
554 
555 static int lola_create(struct snd_card *card, struct pci_dev *pci,
556 		       int dev, struct lola **rchip)
557 {
558 	struct lola *chip;
559 	int err;
560 	unsigned int dever;
561 	static const struct snd_device_ops ops = {
562 		.dev_free = lola_dev_free,
563 	};
564 
565 	*rchip = NULL;
566 
567 	err = pci_enable_device(pci);
568 	if (err < 0)
569 		return err;
570 
571 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
572 	if (!chip) {
573 		pci_disable_device(pci);
574 		return -ENOMEM;
575 	}
576 
577 	spin_lock_init(&chip->reg_lock);
578 	mutex_init(&chip->open_mutex);
579 	chip->card = card;
580 	chip->pci = pci;
581 	chip->irq = -1;
582 
583 	chip->granularity = granularity[dev];
584 	switch (chip->granularity) {
585 	case 8:
586 		chip->sample_rate_max = 48000;
587 		break;
588 	case 16:
589 		chip->sample_rate_max = 96000;
590 		break;
591 	case 32:
592 		chip->sample_rate_max = 192000;
593 		break;
594 	default:
595 		dev_warn(chip->card->dev,
596 			   "Invalid granularity %d, reset to %d\n",
597 			   chip->granularity, LOLA_GRANULARITY_MAX);
598 		chip->granularity = LOLA_GRANULARITY_MAX;
599 		chip->sample_rate_max = 192000;
600 		break;
601 	}
602 	chip->sample_rate_min = sample_rate_min[dev];
603 	if (chip->sample_rate_min > chip->sample_rate_max) {
604 		dev_warn(chip->card->dev,
605 			   "Invalid sample_rate_min %d, reset to 16000\n",
606 			   chip->sample_rate_min);
607 		chip->sample_rate_min = 16000;
608 	}
609 
610 	err = pci_request_regions(pci, DRVNAME);
611 	if (err < 0) {
612 		kfree(chip);
613 		pci_disable_device(pci);
614 		return err;
615 	}
616 
617 	chip->bar[0].addr = pci_resource_start(pci, 0);
618 	chip->bar[0].remap_addr = pci_ioremap_bar(pci, 0);
619 	chip->bar[1].addr = pci_resource_start(pci, 2);
620 	chip->bar[1].remap_addr = pci_ioremap_bar(pci, 2);
621 	if (!chip->bar[0].remap_addr || !chip->bar[1].remap_addr) {
622 		dev_err(chip->card->dev, "ioremap error\n");
623 		err = -ENXIO;
624 		goto errout;
625 	}
626 
627 	pci_set_master(pci);
628 
629 	err = reset_controller(chip);
630 	if (err < 0)
631 		goto errout;
632 
633 	if (request_irq(pci->irq, lola_interrupt, IRQF_SHARED,
634 			KBUILD_MODNAME, chip)) {
635 		dev_err(chip->card->dev, "unable to grab IRQ %d\n", pci->irq);
636 		err = -EBUSY;
637 		goto errout;
638 	}
639 	chip->irq = pci->irq;
640 	card->sync_irq = chip->irq;
641 
642 	dever = lola_readl(chip, BAR1, DEVER);
643 	chip->pcm[CAPT].num_streams = (dever >> 0) & 0x3ff;
644 	chip->pcm[PLAY].num_streams = (dever >> 10) & 0x3ff;
645 	chip->version = (dever >> 24) & 0xff;
646 	dev_dbg(chip->card->dev, "streams in=%d, out=%d, version=0x%x\n",
647 		    chip->pcm[CAPT].num_streams, chip->pcm[PLAY].num_streams,
648 		    chip->version);
649 
650 	/* Test LOLA_BAR1_DEVER */
651 	if (chip->pcm[CAPT].num_streams > MAX_STREAM_IN_COUNT ||
652 	    chip->pcm[PLAY].num_streams > MAX_STREAM_OUT_COUNT ||
653 	    (!chip->pcm[CAPT].num_streams &&
654 	     !chip->pcm[PLAY].num_streams)) {
655 		dev_err(chip->card->dev, "invalid DEVER = %x\n", dever);
656 		err = -EINVAL;
657 		goto errout;
658 	}
659 
660 	err = setup_corb_rirb(chip);
661 	if (err < 0)
662 		goto errout;
663 
664 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
665 	if (err < 0) {
666 		dev_err(chip->card->dev, "Error creating device [card]!\n");
667 		goto errout;
668 	}
669 
670 	strcpy(card->driver, "Lola");
671 	strscpy(card->shortname, "Digigram Lola", sizeof(card->shortname));
672 	snprintf(card->longname, sizeof(card->longname),
673 		 "%s at 0x%lx irq %i",
674 		 card->shortname, chip->bar[0].addr, chip->irq);
675 	strcpy(card->mixername, card->shortname);
676 
677 	lola_irq_enable(chip);
678 
679 	chip->initialized = 1;
680 	*rchip = chip;
681 	return 0;
682 
683  errout:
684 	lola_free(chip);
685 	return err;
686 }
687 
688 static int lola_probe(struct pci_dev *pci,
689 		      const struct pci_device_id *pci_id)
690 {
691 	static int dev;
692 	struct snd_card *card;
693 	struct lola *chip;
694 	int err;
695 
696 	if (dev >= SNDRV_CARDS)
697 		return -ENODEV;
698 	if (!enable[dev]) {
699 		dev++;
700 		return -ENOENT;
701 	}
702 
703 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
704 			   0, &card);
705 	if (err < 0) {
706 		dev_err(&pci->dev, "Error creating card!\n");
707 		return err;
708 	}
709 
710 	err = lola_create(card, pci, dev, &chip);
711 	if (err < 0)
712 		goto out_free;
713 	card->private_data = chip;
714 
715 	err = lola_parse_tree(chip);
716 	if (err < 0)
717 		goto out_free;
718 
719 	err = lola_create_pcm(chip);
720 	if (err < 0)
721 		goto out_free;
722 
723 	err = lola_create_mixer(chip);
724 	if (err < 0)
725 		goto out_free;
726 
727 	lola_proc_debug_new(chip);
728 
729 	err = snd_card_register(card);
730 	if (err < 0)
731 		goto out_free;
732 
733 	pci_set_drvdata(pci, card);
734 	dev++;
735 	return err;
736 out_free:
737 	snd_card_free(card);
738 	return err;
739 }
740 
741 static void lola_remove(struct pci_dev *pci)
742 {
743 	snd_card_free(pci_get_drvdata(pci));
744 }
745 
746 /* PCI IDs */
747 static const struct pci_device_id lola_ids[] = {
748 	{ PCI_VDEVICE(DIGIGRAM, 0x0001) },
749 	{ 0, }
750 };
751 MODULE_DEVICE_TABLE(pci, lola_ids);
752 
753 /* pci_driver definition */
754 static struct pci_driver lola_driver = {
755 	.name = KBUILD_MODNAME,
756 	.id_table = lola_ids,
757 	.probe = lola_probe,
758 	.remove = lola_remove,
759 };
760 
761 module_pci_driver(lola_driver);
762