1 /* 2 * 3 * Support for audio capture 4 * PCI function #1 of the cx2388x. 5 * 6 * (c) 2007 Trent Piepho <xyzzy@speakeasy.org> 7 * (c) 2005,2006 Ricardo Cerqueira <v4l@cerqueira.org> 8 * (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org> 9 * Based on a dummy cx88 module by Gerd Knorr <kraxel@bytesex.org> 10 * Based on dummy.c by Jaroslav Kysela <perex@perex.cz> 11 * 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation; either version 2 of the License, or 15 * (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program; if not, write to the Free Software 24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 25 */ 26 27 #include <linux/module.h> 28 #include <linux/init.h> 29 #include <linux/device.h> 30 #include <linux/interrupt.h> 31 #include <linux/vmalloc.h> 32 #include <linux/dma-mapping.h> 33 #include <linux/pci.h> 34 #include <linux/slab.h> 35 36 #include <asm/delay.h> 37 #include <sound/core.h> 38 #include <sound/pcm.h> 39 #include <sound/pcm_params.h> 40 #include <sound/control.h> 41 #include <sound/initval.h> 42 #include <sound/tlv.h> 43 #include <media/i2c/wm8775.h> 44 45 #include "cx88.h" 46 #include "cx88-reg.h" 47 48 #define dprintk(level, fmt, arg...) do { \ 49 if (debug + 1 > level) \ 50 printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg);\ 51 } while(0) 52 53 #define dprintk_core(level, fmt, arg...) do { \ 54 if (debug + 1 > level) \ 55 printk(KERN_DEBUG "%s/1: " fmt, chip->core->name , ## arg);\ 56 } while(0) 57 58 /**************************************************************************** 59 Data type declarations - Can be moded to a header file later 60 ****************************************************************************/ 61 62 struct cx88_audio_buffer { 63 unsigned int bpl; 64 struct cx88_riscmem risc; 65 void *vaddr; 66 struct scatterlist *sglist; 67 int sglen; 68 int nr_pages; 69 }; 70 71 struct cx88_audio_dev { 72 struct cx88_core *core; 73 struct cx88_dmaqueue q; 74 75 /* pci i/o */ 76 struct pci_dev *pci; 77 78 /* audio controls */ 79 int irq; 80 81 struct snd_card *card; 82 83 spinlock_t reg_lock; 84 atomic_t count; 85 86 unsigned int dma_size; 87 unsigned int period_size; 88 unsigned int num_periods; 89 90 struct cx88_audio_buffer *buf; 91 92 struct snd_pcm_substream *substream; 93 }; 94 typedef struct cx88_audio_dev snd_cx88_card_t; 95 96 97 98 /**************************************************************************** 99 Module global static vars 100 ****************************************************************************/ 101 102 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ 103 static const char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ 104 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; 105 106 module_param_array(enable, bool, NULL, 0444); 107 MODULE_PARM_DESC(enable, "Enable cx88x soundcard. default enabled."); 108 109 module_param_array(index, int, NULL, 0444); 110 MODULE_PARM_DESC(index, "Index value for cx88x capture interface(s)."); 111 112 113 /**************************************************************************** 114 Module macros 115 ****************************************************************************/ 116 117 MODULE_DESCRIPTION("ALSA driver module for cx2388x based TV cards"); 118 MODULE_AUTHOR("Ricardo Cerqueira"); 119 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>"); 120 MODULE_LICENSE("GPL"); 121 MODULE_VERSION(CX88_VERSION); 122 123 MODULE_SUPPORTED_DEVICE("{{Conexant,23881},{{Conexant,23882},{{Conexant,23883}"); 124 static unsigned int debug; 125 module_param(debug,int,0644); 126 MODULE_PARM_DESC(debug,"enable debug messages"); 127 128 /**************************************************************************** 129 Module specific funtions 130 ****************************************************************************/ 131 132 /* 133 * BOARD Specific: Sets audio DMA 134 */ 135 136 static int _cx88_start_audio_dma(snd_cx88_card_t *chip) 137 { 138 struct cx88_audio_buffer *buf = chip->buf; 139 struct cx88_core *core=chip->core; 140 const struct sram_channel *audio_ch = &cx88_sram_channels[SRAM_CH25]; 141 142 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */ 143 cx_clear(MO_AUD_DMACNTRL, 0x11); 144 145 /* setup fifo + format - out channel */ 146 cx88_sram_channel_setup(chip->core, audio_ch, buf->bpl, buf->risc.dma); 147 148 /* sets bpl size */ 149 cx_write(MO_AUDD_LNGTH, buf->bpl); 150 151 /* reset counter */ 152 cx_write(MO_AUDD_GPCNTRL, GP_COUNT_CONTROL_RESET); 153 atomic_set(&chip->count, 0); 154 155 dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d byte buffer\n", 156 buf->bpl, cx_read(audio_ch->cmds_start + 8)>>1, 157 chip->num_periods, buf->bpl * chip->num_periods); 158 159 /* Enables corresponding bits at AUD_INT_STAT */ 160 cx_write(MO_AUD_INTMSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC | 161 AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1); 162 163 /* Clean any pending interrupt bits already set */ 164 cx_write(MO_AUD_INTSTAT, ~0); 165 166 /* enable audio irqs */ 167 cx_set(MO_PCI_INTMSK, chip->core->pci_irqmask | PCI_INT_AUDINT); 168 169 /* start dma */ 170 cx_set(MO_DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */ 171 cx_set(MO_AUD_DMACNTRL, 0x11); /* audio downstream FIFO and RISC enable */ 172 173 if (debug) 174 cx88_sram_channel_dump(chip->core, audio_ch); 175 176 return 0; 177 } 178 179 /* 180 * BOARD Specific: Resets audio DMA 181 */ 182 static int _cx88_stop_audio_dma(snd_cx88_card_t *chip) 183 { 184 struct cx88_core *core=chip->core; 185 dprintk(1, "Stopping audio DMA\n"); 186 187 /* stop dma */ 188 cx_clear(MO_AUD_DMACNTRL, 0x11); 189 190 /* disable irqs */ 191 cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT); 192 cx_clear(MO_AUD_INTMSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC | 193 AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1); 194 195 if (debug) 196 cx88_sram_channel_dump(chip->core, &cx88_sram_channels[SRAM_CH25]); 197 198 return 0; 199 } 200 201 #define MAX_IRQ_LOOP 50 202 203 /* 204 * BOARD Specific: IRQ dma bits 205 */ 206 static const char *cx88_aud_irqs[32] = { 207 "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */ 208 NULL, /* reserved */ 209 "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */ 210 NULL, /* reserved */ 211 "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */ 212 NULL, /* reserved */ 213 "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */ 214 NULL, /* reserved */ 215 "opc_err", "par_err", "rip_err", /* 16-18 */ 216 "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */ 217 }; 218 219 /* 220 * BOARD Specific: Threats IRQ audio specific calls 221 */ 222 static void cx8801_aud_irq(snd_cx88_card_t *chip) 223 { 224 struct cx88_core *core = chip->core; 225 u32 status, mask; 226 227 status = cx_read(MO_AUD_INTSTAT); 228 mask = cx_read(MO_AUD_INTMSK); 229 if (0 == (status & mask)) 230 return; 231 cx_write(MO_AUD_INTSTAT, status); 232 if (debug > 1 || (status & mask & ~0xff)) 233 cx88_print_irqbits(core->name, "irq aud", 234 cx88_aud_irqs, ARRAY_SIZE(cx88_aud_irqs), 235 status, mask); 236 /* risc op code error */ 237 if (status & AUD_INT_OPC_ERR) { 238 printk(KERN_WARNING "%s/1: Audio risc op code error\n",core->name); 239 cx_clear(MO_AUD_DMACNTRL, 0x11); 240 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH25]); 241 } 242 if (status & AUD_INT_DN_SYNC) { 243 dprintk(1, "Downstream sync error\n"); 244 cx_write(MO_AUDD_GPCNTRL, GP_COUNT_CONTROL_RESET); 245 return; 246 } 247 /* risc1 downstream */ 248 if (status & AUD_INT_DN_RISCI1) { 249 atomic_set(&chip->count, cx_read(MO_AUDD_GPCNT)); 250 snd_pcm_period_elapsed(chip->substream); 251 } 252 /* FIXME: Any other status should deserve a special handling? */ 253 } 254 255 /* 256 * BOARD Specific: Handles IRQ calls 257 */ 258 static irqreturn_t cx8801_irq(int irq, void *dev_id) 259 { 260 snd_cx88_card_t *chip = dev_id; 261 struct cx88_core *core = chip->core; 262 u32 status; 263 int loop, handled = 0; 264 265 for (loop = 0; loop < MAX_IRQ_LOOP; loop++) { 266 status = cx_read(MO_PCI_INTSTAT) & 267 (core->pci_irqmask | PCI_INT_AUDINT); 268 if (0 == status) 269 goto out; 270 dprintk(3, "cx8801_irq loop %d/%d, status %x\n", 271 loop, MAX_IRQ_LOOP, status); 272 handled = 1; 273 cx_write(MO_PCI_INTSTAT, status); 274 275 if (status & core->pci_irqmask) 276 cx88_core_irq(core, status); 277 if (status & PCI_INT_AUDINT) 278 cx8801_aud_irq(chip); 279 } 280 281 if (MAX_IRQ_LOOP == loop) { 282 printk(KERN_ERR 283 "%s/1: IRQ loop detected, disabling interrupts\n", 284 core->name); 285 cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT); 286 } 287 288 out: 289 return IRQ_RETVAL(handled); 290 } 291 292 static int cx88_alsa_dma_init(struct cx88_audio_dev *chip, int nr_pages) 293 { 294 struct cx88_audio_buffer *buf = chip->buf; 295 struct page *pg; 296 int i; 297 298 buf->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT); 299 if (NULL == buf->vaddr) { 300 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages); 301 return -ENOMEM; 302 } 303 304 dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n", 305 (unsigned long)buf->vaddr, 306 nr_pages << PAGE_SHIFT); 307 308 memset(buf->vaddr, 0, nr_pages << PAGE_SHIFT); 309 buf->nr_pages = nr_pages; 310 311 buf->sglist = vzalloc(buf->nr_pages * sizeof(*buf->sglist)); 312 if (NULL == buf->sglist) 313 goto vzalloc_err; 314 315 sg_init_table(buf->sglist, buf->nr_pages); 316 for (i = 0; i < buf->nr_pages; i++) { 317 pg = vmalloc_to_page(buf->vaddr + i * PAGE_SIZE); 318 if (NULL == pg) 319 goto vmalloc_to_page_err; 320 sg_set_page(&buf->sglist[i], pg, PAGE_SIZE, 0); 321 } 322 return 0; 323 324 vmalloc_to_page_err: 325 vfree(buf->sglist); 326 buf->sglist = NULL; 327 vzalloc_err: 328 vfree(buf->vaddr); 329 buf->vaddr = NULL; 330 return -ENOMEM; 331 } 332 333 static int cx88_alsa_dma_map(struct cx88_audio_dev *dev) 334 { 335 struct cx88_audio_buffer *buf = dev->buf; 336 337 buf->sglen = dma_map_sg(&dev->pci->dev, buf->sglist, 338 buf->nr_pages, PCI_DMA_FROMDEVICE); 339 340 if (0 == buf->sglen) { 341 pr_warn("%s: cx88_alsa_map_sg failed\n", __func__); 342 return -ENOMEM; 343 } 344 return 0; 345 } 346 347 static int cx88_alsa_dma_unmap(struct cx88_audio_dev *dev) 348 { 349 struct cx88_audio_buffer *buf = dev->buf; 350 351 if (!buf->sglen) 352 return 0; 353 354 dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->sglen, PCI_DMA_FROMDEVICE); 355 buf->sglen = 0; 356 return 0; 357 } 358 359 static int cx88_alsa_dma_free(struct cx88_audio_buffer *buf) 360 { 361 vfree(buf->sglist); 362 buf->sglist = NULL; 363 vfree(buf->vaddr); 364 buf->vaddr = NULL; 365 return 0; 366 } 367 368 369 static int dsp_buffer_free(snd_cx88_card_t *chip) 370 { 371 struct cx88_riscmem *risc = &chip->buf->risc; 372 373 BUG_ON(!chip->dma_size); 374 375 dprintk(2,"Freeing buffer\n"); 376 cx88_alsa_dma_unmap(chip); 377 cx88_alsa_dma_free(chip->buf); 378 if (risc->cpu) 379 pci_free_consistent(chip->pci, risc->size, risc->cpu, risc->dma); 380 kfree(chip->buf); 381 382 chip->buf = NULL; 383 384 return 0; 385 } 386 387 /**************************************************************************** 388 ALSA PCM Interface 389 ****************************************************************************/ 390 391 /* 392 * Digital hardware definition 393 */ 394 #define DEFAULT_FIFO_SIZE 4096 395 static const struct snd_pcm_hardware snd_cx88_digital_hw = { 396 .info = SNDRV_PCM_INFO_MMAP | 397 SNDRV_PCM_INFO_INTERLEAVED | 398 SNDRV_PCM_INFO_BLOCK_TRANSFER | 399 SNDRV_PCM_INFO_MMAP_VALID, 400 .formats = SNDRV_PCM_FMTBIT_S16_LE, 401 402 .rates = SNDRV_PCM_RATE_48000, 403 .rate_min = 48000, 404 .rate_max = 48000, 405 .channels_min = 2, 406 .channels_max = 2, 407 /* Analog audio output will be full of clicks and pops if there 408 are not exactly four lines in the SRAM FIFO buffer. */ 409 .period_bytes_min = DEFAULT_FIFO_SIZE/4, 410 .period_bytes_max = DEFAULT_FIFO_SIZE/4, 411 .periods_min = 1, 412 .periods_max = 1024, 413 .buffer_bytes_max = (1024*1024), 414 }; 415 416 /* 417 * audio pcm capture open callback 418 */ 419 static int snd_cx88_pcm_open(struct snd_pcm_substream *substream) 420 { 421 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream); 422 struct snd_pcm_runtime *runtime = substream->runtime; 423 int err; 424 425 if (!chip) { 426 printk(KERN_ERR "BUG: cx88 can't find device struct. Can't proceed with open\n"); 427 return -ENODEV; 428 } 429 430 err = snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS); 431 if (err < 0) 432 goto _error; 433 434 chip->substream = substream; 435 436 runtime->hw = snd_cx88_digital_hw; 437 438 if (cx88_sram_channels[SRAM_CH25].fifo_size != DEFAULT_FIFO_SIZE) { 439 unsigned int bpl = cx88_sram_channels[SRAM_CH25].fifo_size / 4; 440 bpl &= ~7; /* must be multiple of 8 */ 441 runtime->hw.period_bytes_min = bpl; 442 runtime->hw.period_bytes_max = bpl; 443 } 444 445 return 0; 446 _error: 447 dprintk(1,"Error opening PCM!\n"); 448 return err; 449 } 450 451 /* 452 * audio close callback 453 */ 454 static int snd_cx88_close(struct snd_pcm_substream *substream) 455 { 456 return 0; 457 } 458 459 /* 460 * hw_params callback 461 */ 462 static int snd_cx88_hw_params(struct snd_pcm_substream * substream, 463 struct snd_pcm_hw_params * hw_params) 464 { 465 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream); 466 467 struct cx88_audio_buffer *buf; 468 int ret; 469 470 if (substream->runtime->dma_area) { 471 dsp_buffer_free(chip); 472 substream->runtime->dma_area = NULL; 473 } 474 475 chip->period_size = params_period_bytes(hw_params); 476 chip->num_periods = params_periods(hw_params); 477 chip->dma_size = chip->period_size * params_periods(hw_params); 478 479 BUG_ON(!chip->dma_size); 480 BUG_ON(chip->num_periods & (chip->num_periods-1)); 481 482 buf = kzalloc(sizeof(*buf), GFP_KERNEL); 483 if (NULL == buf) 484 return -ENOMEM; 485 486 chip->buf = buf; 487 buf->bpl = chip->period_size; 488 489 ret = cx88_alsa_dma_init(chip, 490 (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT)); 491 if (ret < 0) 492 goto error; 493 494 ret = cx88_alsa_dma_map(chip); 495 if (ret < 0) 496 goto error; 497 498 ret = cx88_risc_databuffer(chip->pci, &buf->risc, buf->sglist, 499 chip->period_size, chip->num_periods, 1); 500 if (ret < 0) 501 goto error; 502 503 /* Loop back to start of program */ 504 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC); 505 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma); 506 507 substream->runtime->dma_area = chip->buf->vaddr; 508 substream->runtime->dma_bytes = chip->dma_size; 509 substream->runtime->dma_addr = 0; 510 return 0; 511 512 error: 513 kfree(buf); 514 return ret; 515 } 516 517 /* 518 * hw free callback 519 */ 520 static int snd_cx88_hw_free(struct snd_pcm_substream * substream) 521 { 522 523 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream); 524 525 if (substream->runtime->dma_area) { 526 dsp_buffer_free(chip); 527 substream->runtime->dma_area = NULL; 528 } 529 530 return 0; 531 } 532 533 /* 534 * prepare callback 535 */ 536 static int snd_cx88_prepare(struct snd_pcm_substream *substream) 537 { 538 return 0; 539 } 540 541 /* 542 * trigger callback 543 */ 544 static int snd_cx88_card_trigger(struct snd_pcm_substream *substream, int cmd) 545 { 546 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream); 547 int err; 548 549 /* Local interrupts are already disabled by ALSA */ 550 spin_lock(&chip->reg_lock); 551 552 switch (cmd) { 553 case SNDRV_PCM_TRIGGER_START: 554 err=_cx88_start_audio_dma(chip); 555 break; 556 case SNDRV_PCM_TRIGGER_STOP: 557 err=_cx88_stop_audio_dma(chip); 558 break; 559 default: 560 err=-EINVAL; 561 break; 562 } 563 564 spin_unlock(&chip->reg_lock); 565 566 return err; 567 } 568 569 /* 570 * pointer callback 571 */ 572 static snd_pcm_uframes_t snd_cx88_pointer(struct snd_pcm_substream *substream) 573 { 574 snd_cx88_card_t *chip = snd_pcm_substream_chip(substream); 575 struct snd_pcm_runtime *runtime = substream->runtime; 576 u16 count; 577 578 count = atomic_read(&chip->count); 579 580 // dprintk(2, "%s - count %d (+%u), period %d, frame %lu\n", __func__, 581 // count, new, count & (runtime->periods-1), 582 // runtime->period_size * (count & (runtime->periods-1))); 583 return runtime->period_size * (count & (runtime->periods-1)); 584 } 585 586 /* 587 * page callback (needed for mmap) 588 */ 589 static struct page *snd_cx88_page(struct snd_pcm_substream *substream, 590 unsigned long offset) 591 { 592 void *pageptr = substream->runtime->dma_area + offset; 593 return vmalloc_to_page(pageptr); 594 } 595 596 /* 597 * operators 598 */ 599 static const struct snd_pcm_ops snd_cx88_pcm_ops = { 600 .open = snd_cx88_pcm_open, 601 .close = snd_cx88_close, 602 .ioctl = snd_pcm_lib_ioctl, 603 .hw_params = snd_cx88_hw_params, 604 .hw_free = snd_cx88_hw_free, 605 .prepare = snd_cx88_prepare, 606 .trigger = snd_cx88_card_trigger, 607 .pointer = snd_cx88_pointer, 608 .page = snd_cx88_page, 609 }; 610 611 /* 612 * create a PCM device 613 */ 614 static int snd_cx88_pcm(snd_cx88_card_t *chip, int device, const char *name) 615 { 616 int err; 617 struct snd_pcm *pcm; 618 619 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm); 620 if (err < 0) 621 return err; 622 pcm->private_data = chip; 623 strcpy(pcm->name, name); 624 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx88_pcm_ops); 625 626 return 0; 627 } 628 629 /**************************************************************************** 630 CONTROL INTERFACE 631 ****************************************************************************/ 632 static int snd_cx88_volume_info(struct snd_kcontrol *kcontrol, 633 struct snd_ctl_elem_info *info) 634 { 635 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 636 info->count = 2; 637 info->value.integer.min = 0; 638 info->value.integer.max = 0x3f; 639 640 return 0; 641 } 642 643 static int snd_cx88_volume_get(struct snd_kcontrol *kcontrol, 644 struct snd_ctl_elem_value *value) 645 { 646 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol); 647 struct cx88_core *core=chip->core; 648 int vol = 0x3f - (cx_read(AUD_VOL_CTL) & 0x3f), 649 bal = cx_read(AUD_BAL_CTL); 650 651 value->value.integer.value[(bal & 0x40) ? 0 : 1] = vol; 652 vol -= (bal & 0x3f); 653 value->value.integer.value[(bal & 0x40) ? 1 : 0] = vol < 0 ? 0 : vol; 654 655 return 0; 656 } 657 658 static void snd_cx88_wm8775_volume_put(struct snd_kcontrol *kcontrol, 659 struct snd_ctl_elem_value *value) 660 { 661 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol); 662 struct cx88_core *core = chip->core; 663 int left = value->value.integer.value[0]; 664 int right = value->value.integer.value[1]; 665 int v, b; 666 667 /* Pass volume & balance onto any WM8775 */ 668 if (left >= right) { 669 v = left << 10; 670 b = left ? (0x8000 * right) / left : 0x8000; 671 } else { 672 v = right << 10; 673 b = right ? 0xffff - (0x8000 * left) / right : 0x8000; 674 } 675 wm8775_s_ctrl(core, V4L2_CID_AUDIO_VOLUME, v); 676 wm8775_s_ctrl(core, V4L2_CID_AUDIO_BALANCE, b); 677 } 678 679 /* OK - TODO: test it */ 680 static int snd_cx88_volume_put(struct snd_kcontrol *kcontrol, 681 struct snd_ctl_elem_value *value) 682 { 683 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol); 684 struct cx88_core *core=chip->core; 685 int left, right, v, b; 686 int changed = 0; 687 u32 old; 688 689 if (core->sd_wm8775) 690 snd_cx88_wm8775_volume_put(kcontrol, value); 691 692 left = value->value.integer.value[0] & 0x3f; 693 right = value->value.integer.value[1] & 0x3f; 694 b = right - left; 695 if (b < 0) { 696 v = 0x3f - left; 697 b = (-b) | 0x40; 698 } else { 699 v = 0x3f - right; 700 } 701 /* Do we really know this will always be called with IRQs on? */ 702 spin_lock_irq(&chip->reg_lock); 703 old = cx_read(AUD_VOL_CTL); 704 if (v != (old & 0x3f)) { 705 cx_swrite(SHADOW_AUD_VOL_CTL, AUD_VOL_CTL, (old & ~0x3f) | v); 706 changed = 1; 707 } 708 if ((cx_read(AUD_BAL_CTL) & 0x7f) != b) { 709 cx_write(AUD_BAL_CTL, b); 710 changed = 1; 711 } 712 spin_unlock_irq(&chip->reg_lock); 713 714 return changed; 715 } 716 717 static const DECLARE_TLV_DB_SCALE(snd_cx88_db_scale, -6300, 100, 0); 718 719 static const struct snd_kcontrol_new snd_cx88_volume = { 720 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 721 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | 722 SNDRV_CTL_ELEM_ACCESS_TLV_READ, 723 .name = "Analog-TV Volume", 724 .info = snd_cx88_volume_info, 725 .get = snd_cx88_volume_get, 726 .put = snd_cx88_volume_put, 727 .tlv.p = snd_cx88_db_scale, 728 }; 729 730 static int snd_cx88_switch_get(struct snd_kcontrol *kcontrol, 731 struct snd_ctl_elem_value *value) 732 { 733 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol); 734 struct cx88_core *core = chip->core; 735 u32 bit = kcontrol->private_value; 736 737 value->value.integer.value[0] = !(cx_read(AUD_VOL_CTL) & bit); 738 return 0; 739 } 740 741 static int snd_cx88_switch_put(struct snd_kcontrol *kcontrol, 742 struct snd_ctl_elem_value *value) 743 { 744 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol); 745 struct cx88_core *core = chip->core; 746 u32 bit = kcontrol->private_value; 747 int ret = 0; 748 u32 vol; 749 750 spin_lock_irq(&chip->reg_lock); 751 vol = cx_read(AUD_VOL_CTL); 752 if (value->value.integer.value[0] != !(vol & bit)) { 753 vol ^= bit; 754 cx_swrite(SHADOW_AUD_VOL_CTL, AUD_VOL_CTL, vol); 755 /* Pass mute onto any WM8775 */ 756 if (core->sd_wm8775 && ((1<<6) == bit)) 757 wm8775_s_ctrl(core, V4L2_CID_AUDIO_MUTE, 0 != (vol & bit)); 758 ret = 1; 759 } 760 spin_unlock_irq(&chip->reg_lock); 761 return ret; 762 } 763 764 static const struct snd_kcontrol_new snd_cx88_dac_switch = { 765 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 766 .name = "Audio-Out Switch", 767 .info = snd_ctl_boolean_mono_info, 768 .get = snd_cx88_switch_get, 769 .put = snd_cx88_switch_put, 770 .private_value = (1<<8), 771 }; 772 773 static const struct snd_kcontrol_new snd_cx88_source_switch = { 774 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 775 .name = "Analog-TV Switch", 776 .info = snd_ctl_boolean_mono_info, 777 .get = snd_cx88_switch_get, 778 .put = snd_cx88_switch_put, 779 .private_value = (1<<6), 780 }; 781 782 static int snd_cx88_alc_get(struct snd_kcontrol *kcontrol, 783 struct snd_ctl_elem_value *value) 784 { 785 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol); 786 struct cx88_core *core = chip->core; 787 s32 val; 788 789 val = wm8775_g_ctrl(core, V4L2_CID_AUDIO_LOUDNESS); 790 value->value.integer.value[0] = val ? 1 : 0; 791 return 0; 792 } 793 794 static int snd_cx88_alc_put(struct snd_kcontrol *kcontrol, 795 struct snd_ctl_elem_value *value) 796 { 797 snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol); 798 struct cx88_core *core = chip->core; 799 800 wm8775_s_ctrl(core, V4L2_CID_AUDIO_LOUDNESS, 801 value->value.integer.value[0] != 0); 802 return 0; 803 } 804 805 static struct snd_kcontrol_new snd_cx88_alc_switch = { 806 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 807 .name = "Line-In ALC Switch", 808 .info = snd_ctl_boolean_mono_info, 809 .get = snd_cx88_alc_get, 810 .put = snd_cx88_alc_put, 811 }; 812 813 /**************************************************************************** 814 Basic Flow for Sound Devices 815 ****************************************************************************/ 816 817 /* 818 * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio 819 * Only boards with eeprom and byte 1 at eeprom=1 have it 820 */ 821 822 static const struct pci_device_id cx88_audio_pci_tbl[] = { 823 {0x14f1,0x8801,PCI_ANY_ID,PCI_ANY_ID,0,0,0}, 824 {0x14f1,0x8811,PCI_ANY_ID,PCI_ANY_ID,0,0,0}, 825 {0, } 826 }; 827 MODULE_DEVICE_TABLE(pci, cx88_audio_pci_tbl); 828 829 /* 830 * Chip-specific destructor 831 */ 832 833 static int snd_cx88_free(snd_cx88_card_t *chip) 834 { 835 836 if (chip->irq >= 0) 837 free_irq(chip->irq, chip); 838 839 cx88_core_put(chip->core,chip->pci); 840 841 pci_disable_device(chip->pci); 842 return 0; 843 } 844 845 /* 846 * Component Destructor 847 */ 848 static void snd_cx88_dev_free(struct snd_card * card) 849 { 850 snd_cx88_card_t *chip = card->private_data; 851 852 snd_cx88_free(chip); 853 } 854 855 856 /* 857 * Alsa Constructor - Component probe 858 */ 859 860 static int devno; 861 static int snd_cx88_create(struct snd_card *card, struct pci_dev *pci, 862 snd_cx88_card_t **rchip, 863 struct cx88_core **core_ptr) 864 { 865 snd_cx88_card_t *chip; 866 struct cx88_core *core; 867 int err; 868 unsigned char pci_lat; 869 870 *rchip = NULL; 871 872 err = pci_enable_device(pci); 873 if (err < 0) 874 return err; 875 876 pci_set_master(pci); 877 878 chip = card->private_data; 879 880 core = cx88_core_get(pci); 881 if (NULL == core) { 882 err = -EINVAL; 883 return err; 884 } 885 886 err = pci_set_dma_mask(pci,DMA_BIT_MASK(32)); 887 if (err) { 888 dprintk(0, "%s/1: Oops: no 32bit PCI DMA ???\n",core->name); 889 cx88_core_put(core, pci); 890 return err; 891 } 892 893 894 /* pci init */ 895 chip->card = card; 896 chip->pci = pci; 897 chip->irq = -1; 898 spin_lock_init(&chip->reg_lock); 899 900 chip->core = core; 901 902 /* get irq */ 903 err = request_irq(chip->pci->irq, cx8801_irq, 904 IRQF_SHARED, chip->core->name, chip); 905 if (err < 0) { 906 dprintk(0, "%s: can't get IRQ %d\n", 907 chip->core->name, chip->pci->irq); 908 return err; 909 } 910 911 /* print pci info */ 912 pci_read_config_byte(pci, PCI_LATENCY_TIMER, &pci_lat); 913 914 dprintk(1, "ALSA %s/%i: found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n", 915 core->name, devno, 916 pci_name(pci), pci->revision, pci->irq, 917 pci_lat, (unsigned long long)pci_resource_start(pci,0)); 918 919 chip->irq = pci->irq; 920 synchronize_irq(chip->irq); 921 922 *rchip = chip; 923 *core_ptr = core; 924 925 return 0; 926 } 927 928 static int cx88_audio_initdev(struct pci_dev *pci, 929 const struct pci_device_id *pci_id) 930 { 931 struct snd_card *card; 932 snd_cx88_card_t *chip; 933 struct cx88_core *core = NULL; 934 int err; 935 936 if (devno >= SNDRV_CARDS) 937 return (-ENODEV); 938 939 if (!enable[devno]) { 940 ++devno; 941 return (-ENOENT); 942 } 943 944 err = snd_card_new(&pci->dev, index[devno], id[devno], THIS_MODULE, 945 sizeof(snd_cx88_card_t), &card); 946 if (err < 0) 947 return err; 948 949 card->private_free = snd_cx88_dev_free; 950 951 err = snd_cx88_create(card, pci, &chip, &core); 952 if (err < 0) 953 goto error; 954 955 err = snd_cx88_pcm(chip, 0, "CX88 Digital"); 956 if (err < 0) 957 goto error; 958 959 err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_volume, chip)); 960 if (err < 0) 961 goto error; 962 err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_dac_switch, chip)); 963 if (err < 0) 964 goto error; 965 err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_source_switch, chip)); 966 if (err < 0) 967 goto error; 968 969 /* If there's a wm8775 then add a Line-In ALC switch */ 970 if (core->sd_wm8775) 971 snd_ctl_add(card, snd_ctl_new1(&snd_cx88_alc_switch, chip)); 972 973 strcpy (card->driver, "CX88x"); 974 sprintf(card->shortname, "Conexant CX%x", pci->device); 975 sprintf(card->longname, "%s at %#llx", 976 card->shortname,(unsigned long long)pci_resource_start(pci, 0)); 977 strcpy (card->mixername, "CX88"); 978 979 dprintk (0, "%s/%i: ALSA support for cx2388x boards\n", 980 card->driver,devno); 981 982 err = snd_card_register(card); 983 if (err < 0) 984 goto error; 985 pci_set_drvdata(pci,card); 986 987 devno++; 988 return 0; 989 990 error: 991 snd_card_free(card); 992 return err; 993 } 994 /* 995 * ALSA destructor 996 */ 997 static void cx88_audio_finidev(struct pci_dev *pci) 998 { 999 struct snd_card *card = pci_get_drvdata(pci); 1000 1001 snd_card_free(card); 1002 1003 devno--; 1004 } 1005 1006 /* 1007 * PCI driver definition 1008 */ 1009 1010 static struct pci_driver cx88_audio_pci_driver = { 1011 .name = "cx88_audio", 1012 .id_table = cx88_audio_pci_tbl, 1013 .probe = cx88_audio_initdev, 1014 .remove = cx88_audio_finidev, 1015 }; 1016 1017 module_pci_driver(cx88_audio_pci_driver); 1018