linux/drivers/media/pci/cx25821/cx25821-alsa.c
<<
>>
Prefs
   1/*
   2 *  Driver for the Conexant CX25821 PCIe bridge
   3 *
   4 *  Copyright (C) 2009 Conexant Systems Inc.
   5 *  Authors  <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
   6 *      Based on SAA713x ALSA driver and CX88 driver
   7 *
   8 *   This program is free software; you can redistribute it and/or modify
   9 *   it under the terms of the GNU General Public License as published by
  10 *   the Free Software Foundation, version 2
  11 *
  12 *   This program is distributed in the hope that it will be useful,
  13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 *   GNU General Public License for more details.
  16 *
  17 *   You should have received a copy of the GNU General Public License
  18 *   along with this program; if not, write to the Free Software
  19 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  20 *
  21 */
  22
  23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24
  25#include <linux/module.h>
  26#include <linux/init.h>
  27#include <linux/device.h>
  28#include <linux/interrupt.h>
  29#include <linux/vmalloc.h>
  30#include <linux/dma-mapping.h>
  31#include <linux/pci.h>
  32#include <linux/slab.h>
  33
  34#include <linux/delay.h>
  35#include <sound/core.h>
  36#include <sound/pcm.h>
  37#include <sound/pcm_params.h>
  38#include <sound/control.h>
  39#include <sound/initval.h>
  40#include <sound/tlv.h>
  41
  42#include "cx25821.h"
  43#include "cx25821-reg.h"
  44
  45#define AUDIO_SRAM_CHANNEL      SRAM_CH08
  46
  47#define dprintk(level, fmt, arg...)                             \
  48do {                                                            \
  49        if (debug >= level)                                     \
  50                pr_info("%s/1: " fmt, chip->dev->name, ##arg);  \
  51} while (0)
  52#define dprintk_core(level, fmt, arg...)                                \
  53do {                                                                    \
  54        if (debug >= level)                                             \
  55                printk(KERN_DEBUG "%s/1: " fmt, chip->dev->name, ##arg); \
  56} while (0)
  57
  58/****************************************************************************
  59        Data type declarations - Can be moded to a header file later
  60 ****************************************************************************/
  61
  62static int devno;
  63
  64struct cx25821_audio_buffer {
  65        unsigned int bpl;
  66        struct cx25821_riscmem risc;
  67        void                    *vaddr;
  68        struct scatterlist      *sglist;
  69        int                     sglen;
  70        int                     nr_pages;
  71};
  72
  73struct cx25821_audio_dev {
  74        struct cx25821_dev *dev;
  75        struct cx25821_dmaqueue q;
  76
  77        /* pci i/o */
  78        struct pci_dev *pci;
  79
  80        /* audio controls */
  81        int irq;
  82
  83        struct snd_card *card;
  84
  85        unsigned long iobase;
  86        spinlock_t reg_lock;
  87        atomic_t count;
  88
  89        unsigned int dma_size;
  90        unsigned int period_size;
  91        unsigned int num_periods;
  92
  93        struct cx25821_audio_buffer *buf;
  94
  95        struct snd_pcm_substream *substream;
  96};
  97
  98
  99/****************************************************************************
 100                        Module global static vars
 101 ****************************************************************************/
 102
 103static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
 104static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
 105static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
 106
 107module_param_array(enable, bool, NULL, 0444);
 108MODULE_PARM_DESC(enable, "Enable cx25821 soundcard. default enabled.");
 109
 110module_param_array(index, int, NULL, 0444);
 111MODULE_PARM_DESC(index, "Index value for cx25821 capture interface(s).");
 112
 113/****************************************************************************
 114                                Module macros
 115 ****************************************************************************/
 116
 117MODULE_DESCRIPTION("ALSA driver module for cx25821 based capture cards");
 118MODULE_AUTHOR("Hiep Huynh");
 119MODULE_LICENSE("GPL");
 120MODULE_SUPPORTED_DEVICE("{{Conexant,25821}");   /* "{{Conexant,23881}," */
 121
 122static unsigned int debug;
 123module_param(debug, int, 0644);
 124MODULE_PARM_DESC(debug, "enable debug messages");
 125
 126/****************************************************************************
 127                        Module specific funtions
 128 ****************************************************************************/
 129/* Constants taken from cx88-reg.h */
 130#define AUD_INT_DN_RISCI1       (1 <<  0)
 131#define AUD_INT_UP_RISCI1       (1 <<  1)
 132#define AUD_INT_RDS_DN_RISCI1   (1 <<  2)
 133#define AUD_INT_DN_RISCI2       (1 <<  4)       /* yes, 3 is skipped */
 134#define AUD_INT_UP_RISCI2       (1 <<  5)
 135#define AUD_INT_RDS_DN_RISCI2   (1 <<  6)
 136#define AUD_INT_DN_SYNC         (1 << 12)
 137#define AUD_INT_UP_SYNC         (1 << 13)
 138#define AUD_INT_RDS_DN_SYNC     (1 << 14)
 139#define AUD_INT_OPC_ERR         (1 << 16)
 140#define AUD_INT_BER_IRQ         (1 << 20)
 141#define AUD_INT_MCHG_IRQ        (1 << 21)
 142#define GP_COUNT_CONTROL_RESET  0x3
 143
 144#define PCI_MSK_AUD_EXT   (1 <<  4)
 145#define PCI_MSK_AUD_INT   (1 <<  3)
 146
 147static int cx25821_alsa_dma_init(struct cx25821_audio_dev *chip, int nr_pages)
 148{
 149        struct cx25821_audio_buffer *buf = chip->buf;
 150        struct page *pg;
 151        int i;
 152
 153        buf->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
 154        if (NULL == buf->vaddr) {
 155                dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
 156                return -ENOMEM;
 157        }
 158
 159        dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n",
 160                                (unsigned long)buf->vaddr,
 161                                nr_pages << PAGE_SHIFT);
 162
 163        memset(buf->vaddr, 0, nr_pages << PAGE_SHIFT);
 164        buf->nr_pages = nr_pages;
 165
 166        buf->sglist = vzalloc(buf->nr_pages * sizeof(*buf->sglist));
 167        if (NULL == buf->sglist)
 168                goto vzalloc_err;
 169
 170        sg_init_table(buf->sglist, buf->nr_pages);
 171        for (i = 0; i < buf->nr_pages; i++) {
 172                pg = vmalloc_to_page(buf->vaddr + i * PAGE_SIZE);
 173                if (NULL == pg)
 174                        goto vmalloc_to_page_err;
 175                sg_set_page(&buf->sglist[i], pg, PAGE_SIZE, 0);
 176        }
 177        return 0;
 178
 179vmalloc_to_page_err:
 180        vfree(buf->sglist);
 181        buf->sglist = NULL;
 182vzalloc_err:
 183        vfree(buf->vaddr);
 184        buf->vaddr = NULL;
 185        return -ENOMEM;
 186}
 187
 188static int cx25821_alsa_dma_map(struct cx25821_audio_dev *dev)
 189{
 190        struct cx25821_audio_buffer *buf = dev->buf;
 191
 192        buf->sglen = dma_map_sg(&dev->pci->dev, buf->sglist,
 193                        buf->nr_pages, PCI_DMA_FROMDEVICE);
 194
 195        if (0 == buf->sglen) {
 196                pr_warn("%s: cx25821_alsa_map_sg failed\n", __func__);
 197                return -ENOMEM;
 198        }
 199        return 0;
 200}
 201
 202static int cx25821_alsa_dma_unmap(struct cx25821_audio_dev *dev)
 203{
 204        struct cx25821_audio_buffer *buf = dev->buf;
 205
 206        if (!buf->sglen)
 207                return 0;
 208
 209        dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->sglen, PCI_DMA_FROMDEVICE);
 210        buf->sglen = 0;
 211        return 0;
 212}
 213
 214static int cx25821_alsa_dma_free(struct cx25821_audio_buffer *buf)
 215{
 216        vfree(buf->sglist);
 217        buf->sglist = NULL;
 218        vfree(buf->vaddr);
 219        buf->vaddr = NULL;
 220        return 0;
 221}
 222
 223/*
 224 * BOARD Specific: Sets audio DMA
 225 */
 226
 227static int _cx25821_start_audio_dma(struct cx25821_audio_dev *chip)
 228{
 229        struct cx25821_audio_buffer *buf = chip->buf;
 230        struct cx25821_dev *dev = chip->dev;
 231        const struct sram_channel *audio_ch =
 232            &cx25821_sram_channels[AUDIO_SRAM_CHANNEL];
 233        u32 tmp = 0;
 234
 235        /* enable output on the GPIO 0 for the MCLK ADC (Audio) */
 236        cx25821_set_gpiopin_direction(chip->dev, 0, 0);
 237
 238        /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
 239        cx_clear(AUD_INT_DMA_CTL,
 240                 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
 241
 242        /* setup fifo + format - out channel */
 243        cx25821_sram_channel_setup_audio(chip->dev, audio_ch, buf->bpl,
 244                                         buf->risc.dma);
 245
 246        /* sets bpl size */
 247        cx_write(AUD_A_LNGTH, buf->bpl);
 248
 249        /* reset counter */
 250        /* GP_COUNT_CONTROL_RESET = 0x3 */
 251        cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
 252        atomic_set(&chip->count, 0);
 253
 254        /* Set the input mode to 16-bit */
 255        tmp = cx_read(AUD_A_CFG);
 256        cx_write(AUD_A_CFG, tmp | FLD_AUD_DST_PK_MODE | FLD_AUD_DST_ENABLE |
 257                 FLD_AUD_CLK_ENABLE);
 258
 259        /*
 260        pr_info("DEBUG: Start audio DMA, %d B/line, cmds_start(0x%x)= %d lines/FIFO, %d periods, %d byte buffer\n",
 261                buf->bpl, audio_ch->cmds_start,
 262                cx_read(audio_ch->cmds_start + 12)>>1,
 263                chip->num_periods, buf->bpl * chip->num_periods);
 264        */
 265
 266        /* Enables corresponding bits at AUD_INT_STAT */
 267        cx_write(AUD_A_INT_MSK, FLD_AUD_DST_RISCI1 | FLD_AUD_DST_OF |
 268                 FLD_AUD_DST_SYNC | FLD_AUD_DST_OPC_ERR);
 269
 270        /* Clean any pending interrupt bits already set */
 271        cx_write(AUD_A_INT_STAT, ~0);
 272
 273        /* enable audio irqs */
 274        cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
 275
 276        /* Turn on audio downstream fifo and risc enable 0x101 */
 277        tmp = cx_read(AUD_INT_DMA_CTL);
 278        cx_set(AUD_INT_DMA_CTL, tmp |
 279               (FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN));
 280
 281        mdelay(100);
 282        return 0;
 283}
 284
 285/*
 286 * BOARD Specific: Resets audio DMA
 287 */
 288static int _cx25821_stop_audio_dma(struct cx25821_audio_dev *chip)
 289{
 290        struct cx25821_dev *dev = chip->dev;
 291
 292        /* stop dma */
 293        cx_clear(AUD_INT_DMA_CTL,
 294                 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
 295
 296        /* disable irqs */
 297        cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
 298        cx_clear(AUD_A_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
 299                 AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
 300
 301        return 0;
 302}
 303
 304#define MAX_IRQ_LOOP 50
 305
 306/*
 307 * BOARD Specific: IRQ dma bits
 308 */
 309static char *cx25821_aud_irqs[32] = {
 310        "dn_risci1", "up_risci1", "rds_dn_risc1",       /* 0-2 */
 311        NULL,                                           /* reserved */
 312        "dn_risci2", "up_risci2", "rds_dn_risc2",       /* 4-6 */
 313        NULL,                                           /* reserved */
 314        "dnf_of", "upf_uf", "rds_dnf_uf",               /* 8-10 */
 315        NULL,                                           /* reserved */
 316        "dn_sync", "up_sync", "rds_dn_sync",            /* 12-14 */
 317        NULL,                                           /* reserved */
 318        "opc_err", "par_err", "rip_err",                /* 16-18 */
 319        "pci_abort", "ber_irq", "mchg_irq"              /* 19-21 */
 320};
 321
 322/*
 323 * BOARD Specific: Threats IRQ audio specific calls
 324 */
 325static void cx25821_aud_irq(struct cx25821_audio_dev *chip, u32 status,
 326                            u32 mask)
 327{
 328        struct cx25821_dev *dev = chip->dev;
 329
 330        if (0 == (status & mask))
 331                return;
 332
 333        cx_write(AUD_A_INT_STAT, status);
 334        if (debug > 1 || (status & mask & ~0xff))
 335                cx25821_print_irqbits(dev->name, "irq aud", cx25821_aud_irqs,
 336                                ARRAY_SIZE(cx25821_aud_irqs), status, mask);
 337
 338        /* risc op code error */
 339        if (status & AUD_INT_OPC_ERR) {
 340                pr_warn("WARNING %s/1: Audio risc op code error\n", dev->name);
 341
 342                cx_clear(AUD_INT_DMA_CTL,
 343                         FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
 344                cx25821_sram_channel_dump_audio(dev,
 345                                &cx25821_sram_channels[AUDIO_SRAM_CHANNEL]);
 346        }
 347        if (status & AUD_INT_DN_SYNC) {
 348                pr_warn("WARNING %s: Downstream sync error!\n", dev->name);
 349                cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
 350                return;
 351        }
 352
 353        /* risc1 downstream */
 354        if (status & AUD_INT_DN_RISCI1) {
 355                atomic_set(&chip->count, cx_read(AUD_A_GPCNT));
 356                snd_pcm_period_elapsed(chip->substream);
 357        }
 358}
 359
 360/*
 361 * BOARD Specific: Handles IRQ calls
 362 */
 363static irqreturn_t cx25821_irq(int irq, void *dev_id)
 364{
 365        struct cx25821_audio_dev *chip = dev_id;
 366        struct cx25821_dev *dev = chip->dev;
 367        u32 status, pci_status;
 368        u32 audint_status, audint_mask;
 369        int loop, handled = 0;
 370
 371        audint_status = cx_read(AUD_A_INT_STAT);
 372        audint_mask = cx_read(AUD_A_INT_MSK);
 373        status = cx_read(PCI_INT_STAT);
 374
 375        for (loop = 0; loop < 1; loop++) {
 376                status = cx_read(PCI_INT_STAT);
 377                if (0 == status) {
 378                        status = cx_read(PCI_INT_STAT);
 379                        audint_status = cx_read(AUD_A_INT_STAT);
 380                        audint_mask = cx_read(AUD_A_INT_MSK);
 381
 382                        if (status) {
 383                                handled = 1;
 384                                cx_write(PCI_INT_STAT, status);
 385
 386                                cx25821_aud_irq(chip, audint_status,
 387                                                audint_mask);
 388                                break;
 389                        } else {
 390                                goto out;
 391                        }
 392                }
 393
 394                handled = 1;
 395                cx_write(PCI_INT_STAT, status);
 396
 397                cx25821_aud_irq(chip, audint_status, audint_mask);
 398        }
 399
 400        pci_status = cx_read(PCI_INT_STAT);
 401
 402        if (handled)
 403                cx_write(PCI_INT_STAT, pci_status);
 404
 405out:
 406        return IRQ_RETVAL(handled);
 407}
 408
 409static int dsp_buffer_free(struct cx25821_audio_dev *chip)
 410{
 411        struct cx25821_riscmem *risc = &chip->buf->risc;
 412
 413        BUG_ON(!chip->dma_size);
 414
 415        dprintk(2, "Freeing buffer\n");
 416        cx25821_alsa_dma_unmap(chip);
 417        cx25821_alsa_dma_free(chip->buf);
 418        pci_free_consistent(chip->pci, risc->size, risc->cpu, risc->dma);
 419        kfree(chip->buf);
 420
 421        chip->buf = NULL;
 422        chip->dma_size = 0;
 423
 424        return 0;
 425}
 426
 427/****************************************************************************
 428                                ALSA PCM Interface
 429 ****************************************************************************/
 430
 431/*
 432 * Digital hardware definition
 433 */
 434#define DEFAULT_FIFO_SIZE       384
 435static struct snd_pcm_hardware snd_cx25821_digital_hw = {
 436        .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
 437                SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID,
 438        .formats = SNDRV_PCM_FMTBIT_S16_LE,
 439
 440        .rates = SNDRV_PCM_RATE_48000,
 441        .rate_min = 48000,
 442        .rate_max = 48000,
 443        .channels_min = 2,
 444        .channels_max = 2,
 445        /* Analog audio output will be full of clicks and pops if there
 446           are not exactly four lines in the SRAM FIFO buffer.  */
 447        .period_bytes_min = DEFAULT_FIFO_SIZE / 3,
 448        .period_bytes_max = DEFAULT_FIFO_SIZE / 3,
 449        .periods_min = 1,
 450        .periods_max = AUDIO_LINE_SIZE,
 451        /* 128 * 128 = 16384 = 1024 * 16 */
 452        .buffer_bytes_max = (AUDIO_LINE_SIZE * AUDIO_LINE_SIZE),
 453};
 454
 455/*
 456 * audio pcm capture open callback
 457 */
 458static int snd_cx25821_pcm_open(struct snd_pcm_substream *substream)
 459{
 460        struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
 461        struct snd_pcm_runtime *runtime = substream->runtime;
 462        int err;
 463        unsigned int bpl = 0;
 464
 465        if (!chip) {
 466                pr_err("DEBUG: cx25821 can't find device struct. Can't proceed with open\n");
 467                return -ENODEV;
 468        }
 469
 470        err = snd_pcm_hw_constraint_pow2(runtime, 0,
 471                                         SNDRV_PCM_HW_PARAM_PERIODS);
 472        if (err < 0)
 473                goto _error;
 474
 475        chip->substream = substream;
 476
 477        runtime->hw = snd_cx25821_digital_hw;
 478
 479        if (cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
 480            DEFAULT_FIFO_SIZE) {
 481                /* since there are 3 audio Clusters */
 482                bpl = cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 3;
 483                bpl &= ~7;      /* must be multiple of 8 */
 484
 485                if (bpl > AUDIO_LINE_SIZE)
 486                        bpl = AUDIO_LINE_SIZE;
 487
 488                runtime->hw.period_bytes_min = bpl;
 489                runtime->hw.period_bytes_max = bpl;
 490        }
 491
 492        return 0;
 493_error:
 494        dprintk(1, "Error opening PCM!\n");
 495        return err;
 496}
 497
 498/*
 499 * audio close callback
 500 */
 501static int snd_cx25821_close(struct snd_pcm_substream *substream)
 502{
 503        return 0;
 504}
 505
 506/*
 507 * hw_params callback
 508 */
 509static int snd_cx25821_hw_params(struct snd_pcm_substream *substream,
 510                                 struct snd_pcm_hw_params *hw_params)
 511{
 512        struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
 513        struct cx25821_audio_buffer *buf;
 514        int ret;
 515
 516        if (substream->runtime->dma_area) {
 517                dsp_buffer_free(chip);
 518                substream->runtime->dma_area = NULL;
 519        }
 520
 521        chip->period_size = params_period_bytes(hw_params);
 522        chip->num_periods = params_periods(hw_params);
 523        chip->dma_size = chip->period_size * params_periods(hw_params);
 524
 525        BUG_ON(!chip->dma_size);
 526        BUG_ON(chip->num_periods & (chip->num_periods - 1));
 527
 528        buf = kzalloc(sizeof(*buf), GFP_KERNEL);
 529        if (NULL == buf)
 530                return -ENOMEM;
 531
 532        if (chip->period_size > AUDIO_LINE_SIZE)
 533                chip->period_size = AUDIO_LINE_SIZE;
 534
 535        buf->bpl = chip->period_size;
 536        chip->buf = buf;
 537
 538        ret = cx25821_alsa_dma_init(chip,
 539                        (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
 540        if (ret < 0)
 541                goto error;
 542
 543        ret = cx25821_alsa_dma_map(chip);
 544        if (ret < 0)
 545                goto error;
 546
 547        ret = cx25821_risc_databuffer_audio(chip->pci, &buf->risc, buf->sglist,
 548                        chip->period_size, chip->num_periods, 1);
 549        if (ret < 0) {
 550                pr_info("DEBUG: ERROR after cx25821_risc_databuffer_audio()\n");
 551                goto error;
 552        }
 553
 554        /* Loop back to start of program */
 555        buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
 556        buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
 557        buf->risc.jmp[2] = cpu_to_le32(0);      /* bits 63-32 */
 558
 559        substream->runtime->dma_area = chip->buf->vaddr;
 560        substream->runtime->dma_bytes = chip->dma_size;
 561        substream->runtime->dma_addr = 0;
 562
 563        return 0;
 564
 565error:
 566        chip->buf = NULL;
 567        kfree(buf);
 568        return ret;
 569}
 570
 571/*
 572 * hw free callback
 573 */
 574static int snd_cx25821_hw_free(struct snd_pcm_substream *substream)
 575{
 576        struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
 577
 578        if (substream->runtime->dma_area) {
 579                dsp_buffer_free(chip);
 580                substream->runtime->dma_area = NULL;
 581        }
 582
 583        return 0;
 584}
 585
 586/*
 587 * prepare callback
 588 */
 589static int snd_cx25821_prepare(struct snd_pcm_substream *substream)
 590{
 591        return 0;
 592}
 593
 594/*
 595 * trigger callback
 596 */
 597static int snd_cx25821_card_trigger(struct snd_pcm_substream *substream,
 598                                    int cmd)
 599{
 600        struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
 601        int err = 0;
 602
 603        /* Local interrupts are already disabled by ALSA */
 604        spin_lock(&chip->reg_lock);
 605
 606        switch (cmd) {
 607        case SNDRV_PCM_TRIGGER_START:
 608                err = _cx25821_start_audio_dma(chip);
 609                break;
 610        case SNDRV_PCM_TRIGGER_STOP:
 611                err = _cx25821_stop_audio_dma(chip);
 612                break;
 613        default:
 614                err = -EINVAL;
 615                break;
 616        }
 617
 618        spin_unlock(&chip->reg_lock);
 619
 620        return err;
 621}
 622
 623/*
 624 * pointer callback
 625 */
 626static snd_pcm_uframes_t snd_cx25821_pointer(struct snd_pcm_substream
 627                                             *substream)
 628{
 629        struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
 630        struct snd_pcm_runtime *runtime = substream->runtime;
 631        u16 count;
 632
 633        count = atomic_read(&chip->count);
 634
 635        return runtime->period_size * (count & (runtime->periods - 1));
 636}
 637
 638/*
 639 * page callback (needed for mmap)
 640 */
 641static struct page *snd_cx25821_page(struct snd_pcm_substream *substream,
 642                                     unsigned long offset)
 643{
 644        void *pageptr = substream->runtime->dma_area + offset;
 645
 646        return vmalloc_to_page(pageptr);
 647}
 648
 649/*
 650 * operators
 651 */
 652static struct snd_pcm_ops snd_cx25821_pcm_ops = {
 653        .open = snd_cx25821_pcm_open,
 654        .close = snd_cx25821_close,
 655        .ioctl = snd_pcm_lib_ioctl,
 656        .hw_params = snd_cx25821_hw_params,
 657        .hw_free = snd_cx25821_hw_free,
 658        .prepare = snd_cx25821_prepare,
 659        .trigger = snd_cx25821_card_trigger,
 660        .pointer = snd_cx25821_pointer,
 661        .page = snd_cx25821_page,
 662};
 663
 664/*
 665 * ALSA create a PCM device:  Called when initializing the board.
 666 * Sets up the name and hooks up the callbacks
 667 */
 668static int snd_cx25821_pcm(struct cx25821_audio_dev *chip, int device,
 669                           char *name)
 670{
 671        struct snd_pcm *pcm;
 672        int err;
 673
 674        err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
 675        if (err < 0) {
 676                pr_info("ERROR: FAILED snd_pcm_new() in %s\n", __func__);
 677                return err;
 678        }
 679        pcm->private_data = chip;
 680        pcm->info_flags = 0;
 681        strcpy(pcm->name, name);
 682        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx25821_pcm_ops);
 683
 684        return 0;
 685}
 686
 687/****************************************************************************
 688                        Basic Flow for Sound Devices
 689 ****************************************************************************/
 690
 691/*
 692 * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
 693 * Only boards with eeprom and byte 1 at eeprom=1 have it
 694 */
 695
 696static const struct pci_device_id __maybe_unused cx25821_audio_pci_tbl[] = {
 697        {0x14f1, 0x0920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
 698        {0,}
 699};
 700
 701MODULE_DEVICE_TABLE(pci, cx25821_audio_pci_tbl);
 702
 703/*
 704 * Alsa Constructor - Component probe
 705 */
 706static int cx25821_audio_initdev(struct cx25821_dev *dev)
 707{
 708        struct snd_card *card;
 709        struct cx25821_audio_dev *chip;
 710        int err;
 711
 712        if (devno >= SNDRV_CARDS) {
 713                pr_info("DEBUG ERROR: devno >= SNDRV_CARDS %s\n", __func__);
 714                return -ENODEV;
 715        }
 716
 717        if (!enable[devno]) {
 718                ++devno;
 719                pr_info("DEBUG ERROR: !enable[devno] %s\n", __func__);
 720                return -ENOENT;
 721        }
 722
 723        err = snd_card_new(&dev->pci->dev, index[devno], id[devno],
 724                           THIS_MODULE,
 725                           sizeof(struct cx25821_audio_dev), &card);
 726        if (err < 0) {
 727                pr_info("DEBUG ERROR: cannot create snd_card_new in %s\n",
 728                        __func__);
 729                return err;
 730        }
 731
 732        strcpy(card->driver, "cx25821");
 733
 734        /* Card "creation" */
 735        chip = card->private_data;
 736        spin_lock_init(&chip->reg_lock);
 737
 738        chip->dev = dev;
 739        chip->card = card;
 740        chip->pci = dev->pci;
 741        chip->iobase = pci_resource_start(dev->pci, 0);
 742
 743        chip->irq = dev->pci->irq;
 744
 745        err = request_irq(dev->pci->irq, cx25821_irq,
 746                          IRQF_SHARED, chip->dev->name, chip);
 747
 748        if (err < 0) {
 749                pr_err("ERROR %s: can't get IRQ %d for ALSA\n", chip->dev->name,
 750                        dev->pci->irq);
 751                goto error;
 752        }
 753
 754        err = snd_cx25821_pcm(chip, 0, "cx25821 Digital");
 755        if (err < 0) {
 756                pr_info("DEBUG ERROR: cannot create snd_cx25821_pcm %s\n",
 757                        __func__);
 758                goto error;
 759        }
 760
 761        strcpy(card->shortname, "cx25821");
 762        sprintf(card->longname, "%s at 0x%lx irq %d", chip->dev->name,
 763                chip->iobase, chip->irq);
 764        strcpy(card->mixername, "CX25821");
 765
 766        pr_info("%s/%i: ALSA support for cx25821 boards\n", card->driver,
 767                devno);
 768
 769        err = snd_card_register(card);
 770        if (err < 0) {
 771                pr_info("DEBUG ERROR: cannot register sound card %s\n",
 772                        __func__);
 773                goto error;
 774        }
 775
 776        dev->card = card;
 777        devno++;
 778        return 0;
 779
 780error:
 781        snd_card_free(card);
 782        return err;
 783}
 784
 785/****************************************************************************
 786                                LINUX MODULE INIT
 787 ****************************************************************************/
 788
 789static int cx25821_alsa_exit_callback(struct device *dev, void *data)
 790{
 791        struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
 792        struct cx25821_dev *cxdev = get_cx25821(v4l2_dev);
 793
 794        snd_card_free(cxdev->card);
 795        return 0;
 796}
 797
 798static void cx25821_audio_fini(void)
 799{
 800        struct device_driver *drv = driver_find("cx25821", &pci_bus_type);
 801        int ret;
 802
 803        ret = driver_for_each_device(drv, NULL, NULL, cx25821_alsa_exit_callback);
 804        if (ret)
 805                pr_err("%s failed to find a cx25821 driver.\n", __func__);
 806}
 807
 808static int cx25821_alsa_init_callback(struct device *dev, void *data)
 809{
 810        struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
 811        struct cx25821_dev *cxdev = get_cx25821(v4l2_dev);
 812
 813        cx25821_audio_initdev(cxdev);
 814        return 0;
 815}
 816
 817/*
 818 * Module initializer
 819 *
 820 * Loops through present saa7134 cards, and assigns an ALSA device
 821 * to each one
 822 *
 823 */
 824static int cx25821_alsa_init(void)
 825{
 826        struct device_driver *drv = driver_find("cx25821", &pci_bus_type);
 827
 828        return driver_for_each_device(drv, NULL, NULL, cx25821_alsa_init_callback);
 829
 830}
 831
 832late_initcall(cx25821_alsa_init);
 833module_exit(cx25821_audio_fini);
 834