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