1 /* 2 * 3 * Support for CX23885 analog audio capture 4 * 5 * (c) 2008 Mijhail Moreyra <mijhail.moreyra@gmail.com> 6 * Adapted from cx88-alsa.c 7 * (c) 2009 Steven Toth <stoth@kernellabs.com> 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 */ 19 20 #include "cx23885.h" 21 #include "cx23885-reg.h" 22 23 #include <linux/module.h> 24 #include <linux/init.h> 25 #include <linux/device.h> 26 #include <linux/interrupt.h> 27 #include <linux/vmalloc.h> 28 #include <linux/dma-mapping.h> 29 #include <linux/pci.h> 30 31 #include <asm/delay.h> 32 33 #include <sound/core.h> 34 #include <sound/pcm.h> 35 #include <sound/pcm_params.h> 36 #include <sound/control.h> 37 #include <sound/initval.h> 38 39 #include <sound/tlv.h> 40 41 #define AUDIO_SRAM_CHANNEL SRAM_CH07 42 43 #define dprintk(level, fmt, arg...) do { \ 44 if (audio_debug + 1 > level) \ 45 printk(KERN_DEBUG pr_fmt("%s: alsa: " fmt), \ 46 chip->dev->name, ##arg); \ 47 } while(0) 48 49 /**************************************************************************** 50 Module global static vars 51 ****************************************************************************/ 52 53 static unsigned int disable_analog_audio; 54 module_param(disable_analog_audio, int, 0644); 55 MODULE_PARM_DESC(disable_analog_audio, "disable analog audio ALSA driver"); 56 57 static unsigned int audio_debug; 58 module_param(audio_debug, int, 0644); 59 MODULE_PARM_DESC(audio_debug, "enable debug messages [analog audio]"); 60 61 /**************************************************************************** 62 Board specific funtions 63 ****************************************************************************/ 64 65 /* Constants taken from cx88-reg.h */ 66 #define AUD_INT_DN_RISCI1 (1 << 0) 67 #define AUD_INT_UP_RISCI1 (1 << 1) 68 #define AUD_INT_RDS_DN_RISCI1 (1 << 2) 69 #define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */ 70 #define AUD_INT_UP_RISCI2 (1 << 5) 71 #define AUD_INT_RDS_DN_RISCI2 (1 << 6) 72 #define AUD_INT_DN_SYNC (1 << 12) 73 #define AUD_INT_UP_SYNC (1 << 13) 74 #define AUD_INT_RDS_DN_SYNC (1 << 14) 75 #define AUD_INT_OPC_ERR (1 << 16) 76 #define AUD_INT_BER_IRQ (1 << 20) 77 #define AUD_INT_MCHG_IRQ (1 << 21) 78 #define GP_COUNT_CONTROL_RESET 0x3 79 80 static int cx23885_alsa_dma_init(struct cx23885_audio_dev *chip, int nr_pages) 81 { 82 struct cx23885_audio_buffer *buf = chip->buf; 83 struct page *pg; 84 int i; 85 86 buf->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT); 87 if (NULL == buf->vaddr) { 88 dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages); 89 return -ENOMEM; 90 } 91 92 dprintk(1, "vmalloc is at addr %p, size=%d\n", 93 buf->vaddr, nr_pages << PAGE_SHIFT); 94 95 memset(buf->vaddr, 0, nr_pages << PAGE_SHIFT); 96 buf->nr_pages = nr_pages; 97 98 buf->sglist = vzalloc(array_size(sizeof(*buf->sglist), buf->nr_pages)); 99 if (NULL == buf->sglist) 100 goto vzalloc_err; 101 102 sg_init_table(buf->sglist, buf->nr_pages); 103 for (i = 0; i < buf->nr_pages; i++) { 104 pg = vmalloc_to_page(buf->vaddr + i * PAGE_SIZE); 105 if (NULL == pg) 106 goto vmalloc_to_page_err; 107 sg_set_page(&buf->sglist[i], pg, PAGE_SIZE, 0); 108 } 109 return 0; 110 111 vmalloc_to_page_err: 112 vfree(buf->sglist); 113 buf->sglist = NULL; 114 vzalloc_err: 115 vfree(buf->vaddr); 116 buf->vaddr = NULL; 117 return -ENOMEM; 118 } 119 120 static int cx23885_alsa_dma_map(struct cx23885_audio_dev *dev) 121 { 122 struct cx23885_audio_buffer *buf = dev->buf; 123 124 buf->sglen = dma_map_sg(&dev->pci->dev, buf->sglist, 125 buf->nr_pages, PCI_DMA_FROMDEVICE); 126 127 if (0 == buf->sglen) { 128 pr_warn("%s: cx23885_alsa_map_sg failed\n", __func__); 129 return -ENOMEM; 130 } 131 return 0; 132 } 133 134 static int cx23885_alsa_dma_unmap(struct cx23885_audio_dev *dev) 135 { 136 struct cx23885_audio_buffer *buf = dev->buf; 137 138 if (!buf->sglen) 139 return 0; 140 141 dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->sglen, PCI_DMA_FROMDEVICE); 142 buf->sglen = 0; 143 return 0; 144 } 145 146 static int cx23885_alsa_dma_free(struct cx23885_audio_buffer *buf) 147 { 148 vfree(buf->sglist); 149 buf->sglist = NULL; 150 vfree(buf->vaddr); 151 buf->vaddr = NULL; 152 return 0; 153 } 154 155 /* 156 * BOARD Specific: Sets audio DMA 157 */ 158 159 static int cx23885_start_audio_dma(struct cx23885_audio_dev *chip) 160 { 161 struct cx23885_audio_buffer *buf = chip->buf; 162 struct cx23885_dev *dev = chip->dev; 163 struct sram_channel *audio_ch = 164 &dev->sram_channels[AUDIO_SRAM_CHANNEL]; 165 166 dprintk(1, "%s()\n", __func__); 167 168 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */ 169 cx_clear(AUD_INT_DMA_CTL, 0x11); 170 171 /* setup fifo + format - out channel */ 172 cx23885_sram_channel_setup(chip->dev, audio_ch, buf->bpl, 173 buf->risc.dma); 174 175 /* sets bpl size */ 176 cx_write(AUD_INT_A_LNGTH, buf->bpl); 177 178 /* This is required to get good audio (1 seems to be ok) */ 179 cx_write(AUD_INT_A_MODE, 1); 180 181 /* reset counter */ 182 cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET); 183 atomic_set(&chip->count, 0); 184 185 dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d byte buffer\n", 186 buf->bpl, cx_read(audio_ch->cmds_start+12)>>1, 187 chip->num_periods, buf->bpl * chip->num_periods); 188 189 /* Enables corresponding bits at AUD_INT_STAT */ 190 cx_write(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC | 191 AUD_INT_DN_RISCI1); 192 193 /* Clean any pending interrupt bits already set */ 194 cx_write(AUDIO_INT_INT_STAT, ~0); 195 196 /* enable audio irqs */ 197 cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT); 198 199 /* start dma */ 200 cx_set(DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */ 201 cx_set(AUD_INT_DMA_CTL, 0x11); /* audio downstream FIFO and 202 RISC enable */ 203 if (audio_debug) 204 cx23885_sram_channel_dump(chip->dev, audio_ch); 205 206 return 0; 207 } 208 209 /* 210 * BOARD Specific: Resets audio DMA 211 */ 212 static int cx23885_stop_audio_dma(struct cx23885_audio_dev *chip) 213 { 214 struct cx23885_dev *dev = chip->dev; 215 dprintk(1, "Stopping audio DMA\n"); 216 217 /* stop dma */ 218 cx_clear(AUD_INT_DMA_CTL, 0x11); 219 220 /* disable irqs */ 221 cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT); 222 cx_clear(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC | 223 AUD_INT_DN_RISCI1); 224 225 if (audio_debug) 226 cx23885_sram_channel_dump(chip->dev, 227 &dev->sram_channels[AUDIO_SRAM_CHANNEL]); 228 229 return 0; 230 } 231 232 /* 233 * BOARD Specific: Handles audio IRQ 234 */ 235 int cx23885_audio_irq(struct cx23885_dev *dev, u32 status, u32 mask) 236 { 237 struct cx23885_audio_dev *chip = dev->audio_dev; 238 239 if (0 == (status & mask)) 240 return 0; 241 242 cx_write(AUDIO_INT_INT_STAT, status); 243 244 /* risc op code error */ 245 if (status & AUD_INT_OPC_ERR) { 246 pr_warn("%s/1: Audio risc op code error\n", 247 dev->name); 248 cx_clear(AUD_INT_DMA_CTL, 0x11); 249 cx23885_sram_channel_dump(dev, 250 &dev->sram_channels[AUDIO_SRAM_CHANNEL]); 251 } 252 if (status & AUD_INT_DN_SYNC) { 253 dprintk(1, "Downstream sync error\n"); 254 cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET); 255 return 1; 256 } 257 /* risc1 downstream */ 258 if (status & AUD_INT_DN_RISCI1) { 259 atomic_set(&chip->count, cx_read(AUD_INT_A_GPCNT)); 260 snd_pcm_period_elapsed(chip->substream); 261 } 262 /* FIXME: Any other status should deserve a special handling? */ 263 264 return 1; 265 } 266 267 static int dsp_buffer_free(struct cx23885_audio_dev *chip) 268 { 269 struct cx23885_riscmem *risc; 270 271 BUG_ON(!chip->dma_size); 272 273 dprintk(2, "Freeing buffer\n"); 274 cx23885_alsa_dma_unmap(chip); 275 cx23885_alsa_dma_free(chip->buf); 276 risc = &chip->buf->risc; 277 pci_free_consistent(chip->pci, risc->size, risc->cpu, risc->dma); 278 kfree(chip->buf); 279 280 chip->buf = NULL; 281 chip->dma_size = 0; 282 283 return 0; 284 } 285 286 /**************************************************************************** 287 ALSA PCM Interface 288 ****************************************************************************/ 289 290 /* 291 * Digital hardware definition 292 */ 293 #define DEFAULT_FIFO_SIZE 4096 294 295 static const struct snd_pcm_hardware snd_cx23885_digital_hw = { 296 .info = SNDRV_PCM_INFO_MMAP | 297 SNDRV_PCM_INFO_INTERLEAVED | 298 SNDRV_PCM_INFO_BLOCK_TRANSFER | 299 SNDRV_PCM_INFO_MMAP_VALID, 300 .formats = SNDRV_PCM_FMTBIT_S16_LE, 301 302 .rates = SNDRV_PCM_RATE_48000, 303 .rate_min = 48000, 304 .rate_max = 48000, 305 .channels_min = 2, 306 .channels_max = 2, 307 /* Analog audio output will be full of clicks and pops if there 308 are not exactly four lines in the SRAM FIFO buffer. */ 309 .period_bytes_min = DEFAULT_FIFO_SIZE/4, 310 .period_bytes_max = DEFAULT_FIFO_SIZE/4, 311 .periods_min = 1, 312 .periods_max = 1024, 313 .buffer_bytes_max = (1024*1024), 314 }; 315 316 /* 317 * audio pcm capture open callback 318 */ 319 static int snd_cx23885_pcm_open(struct snd_pcm_substream *substream) 320 { 321 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream); 322 struct snd_pcm_runtime *runtime = substream->runtime; 323 int err; 324 325 if (!chip) { 326 pr_err("BUG: cx23885 can't find device struct. Can't proceed with open\n"); 327 return -ENODEV; 328 } 329 330 err = snd_pcm_hw_constraint_pow2(runtime, 0, 331 SNDRV_PCM_HW_PARAM_PERIODS); 332 if (err < 0) 333 goto _error; 334 335 chip->substream = substream; 336 337 runtime->hw = snd_cx23885_digital_hw; 338 339 if (chip->dev->sram_channels[AUDIO_SRAM_CHANNEL].fifo_size != 340 DEFAULT_FIFO_SIZE) { 341 unsigned int bpl = chip->dev-> 342 sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 4; 343 bpl &= ~7; /* must be multiple of 8 */ 344 runtime->hw.period_bytes_min = bpl; 345 runtime->hw.period_bytes_max = bpl; 346 } 347 348 return 0; 349 _error: 350 dprintk(1, "Error opening PCM!\n"); 351 return err; 352 } 353 354 /* 355 * audio close callback 356 */ 357 static int snd_cx23885_close(struct snd_pcm_substream *substream) 358 { 359 return 0; 360 } 361 362 363 /* 364 * hw_params callback 365 */ 366 static int snd_cx23885_hw_params(struct snd_pcm_substream *substream, 367 struct snd_pcm_hw_params *hw_params) 368 { 369 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream); 370 struct cx23885_audio_buffer *buf; 371 int ret; 372 373 if (substream->runtime->dma_area) { 374 dsp_buffer_free(chip); 375 substream->runtime->dma_area = NULL; 376 } 377 378 chip->period_size = params_period_bytes(hw_params); 379 chip->num_periods = params_periods(hw_params); 380 chip->dma_size = chip->period_size * params_periods(hw_params); 381 382 BUG_ON(!chip->dma_size); 383 BUG_ON(chip->num_periods & (chip->num_periods-1)); 384 385 buf = kzalloc(sizeof(*buf), GFP_KERNEL); 386 if (NULL == buf) 387 return -ENOMEM; 388 389 buf->bpl = chip->period_size; 390 chip->buf = buf; 391 392 ret = cx23885_alsa_dma_init(chip, 393 (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT)); 394 if (ret < 0) 395 goto error; 396 397 ret = cx23885_alsa_dma_map(chip); 398 if (ret < 0) 399 goto error; 400 401 ret = cx23885_risc_databuffer(chip->pci, &buf->risc, buf->sglist, 402 chip->period_size, chip->num_periods, 1); 403 if (ret < 0) 404 goto error; 405 406 /* Loop back to start of program */ 407 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC); 408 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma); 409 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */ 410 411 substream->runtime->dma_area = chip->buf->vaddr; 412 substream->runtime->dma_bytes = chip->dma_size; 413 substream->runtime->dma_addr = 0; 414 415 return 0; 416 417 error: 418 kfree(buf); 419 chip->buf = NULL; 420 return ret; 421 } 422 423 /* 424 * hw free callback 425 */ 426 static int snd_cx23885_hw_free(struct snd_pcm_substream *substream) 427 { 428 429 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream); 430 431 if (substream->runtime->dma_area) { 432 dsp_buffer_free(chip); 433 substream->runtime->dma_area = NULL; 434 } 435 436 return 0; 437 } 438 439 /* 440 * prepare callback 441 */ 442 static int snd_cx23885_prepare(struct snd_pcm_substream *substream) 443 { 444 return 0; 445 } 446 447 /* 448 * trigger callback 449 */ 450 static int snd_cx23885_card_trigger(struct snd_pcm_substream *substream, 451 int cmd) 452 { 453 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream); 454 int err; 455 456 /* Local interrupts are already disabled by ALSA */ 457 spin_lock(&chip->lock); 458 459 switch (cmd) { 460 case SNDRV_PCM_TRIGGER_START: 461 err = cx23885_start_audio_dma(chip); 462 break; 463 case SNDRV_PCM_TRIGGER_STOP: 464 err = cx23885_stop_audio_dma(chip); 465 break; 466 default: 467 err = -EINVAL; 468 break; 469 } 470 471 spin_unlock(&chip->lock); 472 473 return err; 474 } 475 476 /* 477 * pointer callback 478 */ 479 static snd_pcm_uframes_t snd_cx23885_pointer( 480 struct snd_pcm_substream *substream) 481 { 482 struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream); 483 struct snd_pcm_runtime *runtime = substream->runtime; 484 u16 count; 485 486 count = atomic_read(&chip->count); 487 488 return runtime->period_size * (count & (runtime->periods-1)); 489 } 490 491 /* 492 * page callback (needed for mmap) 493 */ 494 static struct page *snd_cx23885_page(struct snd_pcm_substream *substream, 495 unsigned long offset) 496 { 497 void *pageptr = substream->runtime->dma_area + offset; 498 return vmalloc_to_page(pageptr); 499 } 500 501 /* 502 * operators 503 */ 504 static const struct snd_pcm_ops snd_cx23885_pcm_ops = { 505 .open = snd_cx23885_pcm_open, 506 .close = snd_cx23885_close, 507 .ioctl = snd_pcm_lib_ioctl, 508 .hw_params = snd_cx23885_hw_params, 509 .hw_free = snd_cx23885_hw_free, 510 .prepare = snd_cx23885_prepare, 511 .trigger = snd_cx23885_card_trigger, 512 .pointer = snd_cx23885_pointer, 513 .page = snd_cx23885_page, 514 }; 515 516 /* 517 * create a PCM device 518 */ 519 static int snd_cx23885_pcm(struct cx23885_audio_dev *chip, int device, 520 char *name) 521 { 522 int err; 523 struct snd_pcm *pcm; 524 525 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm); 526 if (err < 0) 527 return err; 528 pcm->private_data = chip; 529 strcpy(pcm->name, name); 530 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx23885_pcm_ops); 531 532 return 0; 533 } 534 535 /**************************************************************************** 536 Basic Flow for Sound Devices 537 ****************************************************************************/ 538 539 /* 540 * Alsa Constructor - Component probe 541 */ 542 543 struct cx23885_audio_dev *cx23885_audio_register(struct cx23885_dev *dev) 544 { 545 struct snd_card *card; 546 struct cx23885_audio_dev *chip; 547 int err; 548 549 if (disable_analog_audio) 550 return NULL; 551 552 if (dev->sram_channels[AUDIO_SRAM_CHANNEL].cmds_start == 0) { 553 pr_warn("%s(): Missing SRAM channel configuration for analog TV Audio\n", 554 __func__); 555 return NULL; 556 } 557 558 err = snd_card_new(&dev->pci->dev, 559 SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, 560 THIS_MODULE, sizeof(struct cx23885_audio_dev), &card); 561 if (err < 0) 562 goto error; 563 564 chip = (struct cx23885_audio_dev *) card->private_data; 565 chip->dev = dev; 566 chip->pci = dev->pci; 567 chip->card = card; 568 spin_lock_init(&chip->lock); 569 570 err = snd_cx23885_pcm(chip, 0, "CX23885 Digital"); 571 if (err < 0) 572 goto error; 573 574 strcpy(card->driver, "CX23885"); 575 sprintf(card->shortname, "Conexant CX23885"); 576 sprintf(card->longname, "%s at %s", card->shortname, dev->name); 577 578 err = snd_card_register(card); 579 if (err < 0) 580 goto error; 581 582 dprintk(0, "registered ALSA audio device\n"); 583 584 return chip; 585 586 error: 587 snd_card_free(card); 588 pr_err("%s(): Failed to register analog audio adapter\n", 589 __func__); 590 591 return NULL; 592 } 593 594 /* 595 * ALSA destructor 596 */ 597 void cx23885_audio_unregister(struct cx23885_dev *dev) 598 { 599 struct cx23885_audio_dev *chip = dev->audio_dev; 600 601 snd_card_free(chip->card); 602 } 603