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