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