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