Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  Driver for the Conexant CX25821 PCIe bridge
0004  *
0005  *  Copyright (C) 2009 Conexant Systems Inc.
0006  *  Authors  <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
0007  *  Based on SAA713x ALSA driver and CX88 driver
0008  */
0009 
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011 
0012 #include <linux/module.h>
0013 #include <linux/init.h>
0014 #include <linux/device.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/vmalloc.h>
0017 #include <linux/dma-mapping.h>
0018 #include <linux/pci.h>
0019 #include <linux/slab.h>
0020 
0021 #include <linux/delay.h>
0022 #include <sound/core.h>
0023 #include <sound/pcm.h>
0024 #include <sound/pcm_params.h>
0025 #include <sound/control.h>
0026 #include <sound/initval.h>
0027 #include <sound/tlv.h>
0028 
0029 #include "cx25821.h"
0030 #include "cx25821-reg.h"
0031 
0032 #define AUDIO_SRAM_CHANNEL  SRAM_CH08
0033 
0034 #define dprintk(level, fmt, arg...)             \
0035 do {                                \
0036     if (debug >= level)                 \
0037         pr_info("%s/1: " fmt, chip->dev->name, ##arg);  \
0038 } while (0)
0039 #define dprintk_core(level, fmt, arg...)                \
0040 do {                                    \
0041     if (debug >= level)                     \
0042         printk(KERN_DEBUG "%s/1: " fmt, chip->dev->name, ##arg); \
0043 } while (0)
0044 
0045 /****************************************************************************
0046     Data type declarations - Can be moded to a header file later
0047  ****************************************************************************/
0048 
0049 static int devno;
0050 
0051 struct cx25821_audio_buffer {
0052     unsigned int bpl;
0053     struct cx25821_riscmem risc;
0054     void            *vaddr;
0055     struct scatterlist  *sglist;
0056     int         sglen;
0057     unsigned long       nr_pages;
0058 };
0059 
0060 struct cx25821_audio_dev {
0061     struct cx25821_dev *dev;
0062     struct cx25821_dmaqueue q;
0063 
0064     /* pci i/o */
0065     struct pci_dev *pci;
0066 
0067     /* audio controls */
0068     int irq;
0069 
0070     struct snd_card *card;
0071 
0072     unsigned long iobase;
0073     spinlock_t reg_lock;
0074     atomic_t count;
0075 
0076     unsigned int dma_size;
0077     unsigned int period_size;
0078     unsigned int num_periods;
0079 
0080     struct cx25821_audio_buffer *buf;
0081 
0082     struct snd_pcm_substream *substream;
0083 };
0084 
0085 
0086 /****************************************************************************
0087             Module global static vars
0088  ****************************************************************************/
0089 
0090 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;  /* Index 0-MAX */
0091 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;   /* ID for this card */
0092 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
0093 
0094 module_param_array(enable, bool, NULL, 0444);
0095 MODULE_PARM_DESC(enable, "Enable cx25821 soundcard. default enabled.");
0096 
0097 module_param_array(index, int, NULL, 0444);
0098 MODULE_PARM_DESC(index, "Index value for cx25821 capture interface(s).");
0099 
0100 /****************************************************************************
0101                 Module macros
0102  ****************************************************************************/
0103 
0104 MODULE_DESCRIPTION("ALSA driver module for cx25821 based capture cards");
0105 MODULE_AUTHOR("Hiep Huynh");
0106 MODULE_LICENSE("GPL");
0107 
0108 static unsigned int debug;
0109 module_param(debug, int, 0644);
0110 MODULE_PARM_DESC(debug, "enable debug messages");
0111 
0112 /****************************************************************************
0113             Module specific functions
0114  ****************************************************************************/
0115 /* Constants taken from cx88-reg.h */
0116 #define AUD_INT_DN_RISCI1       (1 <<  0)
0117 #define AUD_INT_UP_RISCI1       (1 <<  1)
0118 #define AUD_INT_RDS_DN_RISCI1   (1 <<  2)
0119 #define AUD_INT_DN_RISCI2       (1 <<  4)   /* yes, 3 is skipped */
0120 #define AUD_INT_UP_RISCI2       (1 <<  5)
0121 #define AUD_INT_RDS_DN_RISCI2   (1 <<  6)
0122 #define AUD_INT_DN_SYNC         (1 << 12)
0123 #define AUD_INT_UP_SYNC         (1 << 13)
0124 #define AUD_INT_RDS_DN_SYNC     (1 << 14)
0125 #define AUD_INT_OPC_ERR         (1 << 16)
0126 #define AUD_INT_BER_IRQ         (1 << 20)
0127 #define AUD_INT_MCHG_IRQ        (1 << 21)
0128 #define GP_COUNT_CONTROL_RESET  0x3
0129 
0130 #define PCI_MSK_AUD_EXT   (1 <<  4)
0131 #define PCI_MSK_AUD_INT   (1 <<  3)
0132 
0133 static int cx25821_alsa_dma_init(struct cx25821_audio_dev *chip,
0134                  unsigned long nr_pages)
0135 {
0136     struct cx25821_audio_buffer *buf = chip->buf;
0137     struct page *pg;
0138     int i;
0139 
0140     buf->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
0141     if (NULL == buf->vaddr) {
0142         dprintk(1, "vmalloc_32(%lu pages) failed\n", nr_pages);
0143         return -ENOMEM;
0144     }
0145 
0146     dprintk(1, "vmalloc is at addr 0x%p, size=%lu\n",
0147                 buf->vaddr,
0148                 nr_pages << PAGE_SHIFT);
0149 
0150     memset(buf->vaddr, 0, nr_pages << PAGE_SHIFT);
0151     buf->nr_pages = nr_pages;
0152 
0153     buf->sglist = vzalloc(array_size(sizeof(*buf->sglist), buf->nr_pages));
0154     if (NULL == buf->sglist)
0155         goto vzalloc_err;
0156 
0157     sg_init_table(buf->sglist, buf->nr_pages);
0158     for (i = 0; i < buf->nr_pages; i++) {
0159         pg = vmalloc_to_page(buf->vaddr + i * PAGE_SIZE);
0160         if (NULL == pg)
0161             goto vmalloc_to_page_err;
0162         sg_set_page(&buf->sglist[i], pg, PAGE_SIZE, 0);
0163     }
0164     return 0;
0165 
0166 vmalloc_to_page_err:
0167     vfree(buf->sglist);
0168     buf->sglist = NULL;
0169 vzalloc_err:
0170     vfree(buf->vaddr);
0171     buf->vaddr = NULL;
0172     return -ENOMEM;
0173 }
0174 
0175 static int cx25821_alsa_dma_map(struct cx25821_audio_dev *dev)
0176 {
0177     struct cx25821_audio_buffer *buf = dev->buf;
0178 
0179     buf->sglen = dma_map_sg(&dev->pci->dev, buf->sglist,
0180             buf->nr_pages, DMA_FROM_DEVICE);
0181 
0182     if (0 == buf->sglen) {
0183         pr_warn("%s: cx25821_alsa_map_sg failed\n", __func__);
0184         return -ENOMEM;
0185     }
0186     return 0;
0187 }
0188 
0189 static int cx25821_alsa_dma_unmap(struct cx25821_audio_dev *dev)
0190 {
0191     struct cx25821_audio_buffer *buf = dev->buf;
0192 
0193     if (!buf->sglen)
0194         return 0;
0195 
0196     dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->nr_pages, DMA_FROM_DEVICE);
0197     buf->sglen = 0;
0198     return 0;
0199 }
0200 
0201 static int cx25821_alsa_dma_free(struct cx25821_audio_buffer *buf)
0202 {
0203     vfree(buf->sglist);
0204     buf->sglist = NULL;
0205     vfree(buf->vaddr);
0206     buf->vaddr = NULL;
0207     return 0;
0208 }
0209 
0210 /*
0211  * BOARD Specific: Sets audio DMA
0212  */
0213 
0214 static int _cx25821_start_audio_dma(struct cx25821_audio_dev *chip)
0215 {
0216     struct cx25821_audio_buffer *buf = chip->buf;
0217     struct cx25821_dev *dev = chip->dev;
0218     const struct sram_channel *audio_ch =
0219         &cx25821_sram_channels[AUDIO_SRAM_CHANNEL];
0220     u32 tmp = 0;
0221 
0222     /* enable output on the GPIO 0 for the MCLK ADC (Audio) */
0223     cx25821_set_gpiopin_direction(chip->dev, 0, 0);
0224 
0225     /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
0226     cx_clear(AUD_INT_DMA_CTL,
0227          FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
0228 
0229     /* setup fifo + format - out channel */
0230     cx25821_sram_channel_setup_audio(chip->dev, audio_ch, buf->bpl,
0231                      buf->risc.dma);
0232 
0233     /* sets bpl size */
0234     cx_write(AUD_A_LNGTH, buf->bpl);
0235 
0236     /* reset counter */
0237     /* GP_COUNT_CONTROL_RESET = 0x3 */
0238     cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
0239     atomic_set(&chip->count, 0);
0240 
0241     /* Set the input mode to 16-bit */
0242     tmp = cx_read(AUD_A_CFG);
0243     cx_write(AUD_A_CFG, tmp | FLD_AUD_DST_PK_MODE | FLD_AUD_DST_ENABLE |
0244          FLD_AUD_CLK_ENABLE);
0245 
0246     /*
0247     pr_info("DEBUG: Start audio DMA, %d B/line, cmds_start(0x%x)= %d lines/FIFO, %d periods, %d byte buffer\n",
0248         buf->bpl, audio_ch->cmds_start,
0249         cx_read(audio_ch->cmds_start + 12)>>1,
0250         chip->num_periods, buf->bpl * chip->num_periods);
0251     */
0252 
0253     /* Enables corresponding bits at AUD_INT_STAT */
0254     cx_write(AUD_A_INT_MSK, FLD_AUD_DST_RISCI1 | FLD_AUD_DST_OF |
0255          FLD_AUD_DST_SYNC | FLD_AUD_DST_OPC_ERR);
0256 
0257     /* Clean any pending interrupt bits already set */
0258     cx_write(AUD_A_INT_STAT, ~0);
0259 
0260     /* enable audio irqs */
0261     cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
0262 
0263     /* Turn on audio downstream fifo and risc enable 0x101 */
0264     tmp = cx_read(AUD_INT_DMA_CTL);
0265     cx_set(AUD_INT_DMA_CTL, tmp |
0266            (FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN));
0267 
0268     mdelay(100);
0269     return 0;
0270 }
0271 
0272 /*
0273  * BOARD Specific: Resets audio DMA
0274  */
0275 static int _cx25821_stop_audio_dma(struct cx25821_audio_dev *chip)
0276 {
0277     struct cx25821_dev *dev = chip->dev;
0278 
0279     /* stop dma */
0280     cx_clear(AUD_INT_DMA_CTL,
0281          FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
0282 
0283     /* disable irqs */
0284     cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
0285     cx_clear(AUD_A_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
0286          AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
0287 
0288     return 0;
0289 }
0290 
0291 #define MAX_IRQ_LOOP 50
0292 
0293 /*
0294  * BOARD Specific: IRQ dma bits
0295  */
0296 static char *cx25821_aud_irqs[32] = {
0297     "dn_risci1", "up_risci1", "rds_dn_risc1",   /* 0-2 */
0298     NULL,                       /* reserved */
0299     "dn_risci2", "up_risci2", "rds_dn_risc2",   /* 4-6 */
0300     NULL,                       /* reserved */
0301     "dnf_of", "upf_uf", "rds_dnf_uf",       /* 8-10 */
0302     NULL,                       /* reserved */
0303     "dn_sync", "up_sync", "rds_dn_sync",        /* 12-14 */
0304     NULL,                       /* reserved */
0305     "opc_err", "par_err", "rip_err",        /* 16-18 */
0306     "pci_abort", "ber_irq", "mchg_irq"      /* 19-21 */
0307 };
0308 
0309 /*
0310  * BOARD Specific: Threats IRQ audio specific calls
0311  */
0312 static void cx25821_aud_irq(struct cx25821_audio_dev *chip, u32 status,
0313                 u32 mask)
0314 {
0315     struct cx25821_dev *dev = chip->dev;
0316 
0317     if (0 == (status & mask))
0318         return;
0319 
0320     cx_write(AUD_A_INT_STAT, status);
0321     if (debug > 1 || (status & mask & ~0xff))
0322         cx25821_print_irqbits(dev->name, "irq aud", cx25821_aud_irqs,
0323                 ARRAY_SIZE(cx25821_aud_irqs), status, mask);
0324 
0325     /* risc op code error */
0326     if (status & AUD_INT_OPC_ERR) {
0327         pr_warn("WARNING %s/1: Audio risc op code error\n", dev->name);
0328 
0329         cx_clear(AUD_INT_DMA_CTL,
0330              FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
0331         cx25821_sram_channel_dump_audio(dev,
0332                 &cx25821_sram_channels[AUDIO_SRAM_CHANNEL]);
0333     }
0334     if (status & AUD_INT_DN_SYNC) {
0335         pr_warn("WARNING %s: Downstream sync error!\n", dev->name);
0336         cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
0337         return;
0338     }
0339 
0340     /* risc1 downstream */
0341     if (status & AUD_INT_DN_RISCI1) {
0342         atomic_set(&chip->count, cx_read(AUD_A_GPCNT));
0343         snd_pcm_period_elapsed(chip->substream);
0344     }
0345 }
0346 
0347 /*
0348  * BOARD Specific: Handles IRQ calls
0349  */
0350 static irqreturn_t cx25821_irq(int irq, void *dev_id)
0351 {
0352     struct cx25821_audio_dev *chip = dev_id;
0353     struct cx25821_dev *dev = chip->dev;
0354     u32 status, pci_status;
0355     u32 audint_status, audint_mask;
0356     int loop, handled = 0;
0357 
0358     audint_status = cx_read(AUD_A_INT_STAT);
0359     audint_mask = cx_read(AUD_A_INT_MSK);
0360     status = cx_read(PCI_INT_STAT);
0361 
0362     for (loop = 0; loop < 1; loop++) {
0363         status = cx_read(PCI_INT_STAT);
0364         if (0 == status) {
0365             status = cx_read(PCI_INT_STAT);
0366             audint_status = cx_read(AUD_A_INT_STAT);
0367             audint_mask = cx_read(AUD_A_INT_MSK);
0368 
0369             if (status) {
0370                 handled = 1;
0371                 cx_write(PCI_INT_STAT, status);
0372 
0373                 cx25821_aud_irq(chip, audint_status,
0374                         audint_mask);
0375                 break;
0376             } else {
0377                 goto out;
0378             }
0379         }
0380 
0381         handled = 1;
0382         cx_write(PCI_INT_STAT, status);
0383 
0384         cx25821_aud_irq(chip, audint_status, audint_mask);
0385     }
0386 
0387     pci_status = cx_read(PCI_INT_STAT);
0388 
0389     if (handled)
0390         cx_write(PCI_INT_STAT, pci_status);
0391 
0392 out:
0393     return IRQ_RETVAL(handled);
0394 }
0395 
0396 static int dsp_buffer_free(struct cx25821_audio_dev *chip)
0397 {
0398     struct cx25821_riscmem *risc = &chip->buf->risc;
0399 
0400     BUG_ON(!chip->dma_size);
0401 
0402     dprintk(2, "Freeing buffer\n");
0403     cx25821_alsa_dma_unmap(chip);
0404     cx25821_alsa_dma_free(chip->buf);
0405     dma_free_coherent(&chip->pci->dev, risc->size, risc->cpu, risc->dma);
0406     kfree(chip->buf);
0407 
0408     chip->buf = NULL;
0409     chip->dma_size = 0;
0410 
0411     return 0;
0412 }
0413 
0414 /****************************************************************************
0415                 ALSA PCM Interface
0416  ****************************************************************************/
0417 
0418 /*
0419  * Digital hardware definition
0420  */
0421 #define DEFAULT_FIFO_SIZE   384
0422 static const struct snd_pcm_hardware snd_cx25821_digital_hw = {
0423     .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
0424         SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID,
0425     .formats = SNDRV_PCM_FMTBIT_S16_LE,
0426 
0427     .rates = SNDRV_PCM_RATE_48000,
0428     .rate_min = 48000,
0429     .rate_max = 48000,
0430     .channels_min = 2,
0431     .channels_max = 2,
0432     /* Analog audio output will be full of clicks and pops if there
0433        are not exactly four lines in the SRAM FIFO buffer.  */
0434     .period_bytes_min = DEFAULT_FIFO_SIZE / 3,
0435     .period_bytes_max = DEFAULT_FIFO_SIZE / 3,
0436     .periods_min = 1,
0437     .periods_max = AUDIO_LINE_SIZE,
0438     /* 128 * 128 = 16384 = 1024 * 16 */
0439     .buffer_bytes_max = (AUDIO_LINE_SIZE * AUDIO_LINE_SIZE),
0440 };
0441 
0442 /*
0443  * audio pcm capture open callback
0444  */
0445 static int snd_cx25821_pcm_open(struct snd_pcm_substream *substream)
0446 {
0447     struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
0448     struct snd_pcm_runtime *runtime = substream->runtime;
0449     int err;
0450     unsigned int bpl = 0;
0451 
0452     if (!chip) {
0453         pr_err("DEBUG: cx25821 can't find device struct. Can't proceed with open\n");
0454         return -ENODEV;
0455     }
0456 
0457     err = snd_pcm_hw_constraint_pow2(runtime, 0,
0458                      SNDRV_PCM_HW_PARAM_PERIODS);
0459     if (err < 0)
0460         goto _error;
0461 
0462     chip->substream = substream;
0463 
0464     runtime->hw = snd_cx25821_digital_hw;
0465 
0466     if (cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
0467         DEFAULT_FIFO_SIZE) {
0468         /* since there are 3 audio Clusters */
0469         bpl = cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 3;
0470         bpl &= ~7;  /* must be multiple of 8 */
0471 
0472         if (bpl > AUDIO_LINE_SIZE)
0473             bpl = AUDIO_LINE_SIZE;
0474 
0475         runtime->hw.period_bytes_min = bpl;
0476         runtime->hw.period_bytes_max = bpl;
0477     }
0478 
0479     return 0;
0480 _error:
0481     dprintk(1, "Error opening PCM!\n");
0482     return err;
0483 }
0484 
0485 /*
0486  * audio close callback
0487  */
0488 static int snd_cx25821_close(struct snd_pcm_substream *substream)
0489 {
0490     return 0;
0491 }
0492 
0493 /*
0494  * hw_params callback
0495  */
0496 static int snd_cx25821_hw_params(struct snd_pcm_substream *substream,
0497                  struct snd_pcm_hw_params *hw_params)
0498 {
0499     struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
0500     struct cx25821_audio_buffer *buf;
0501     int ret;
0502 
0503     if (substream->runtime->dma_area) {
0504         dsp_buffer_free(chip);
0505         substream->runtime->dma_area = NULL;
0506     }
0507 
0508     chip->period_size = params_period_bytes(hw_params);
0509     chip->num_periods = params_periods(hw_params);
0510     chip->dma_size = chip->period_size * params_periods(hw_params);
0511 
0512     BUG_ON(!chip->dma_size);
0513     BUG_ON(chip->num_periods & (chip->num_periods - 1));
0514 
0515     buf = kzalloc(sizeof(*buf), GFP_KERNEL);
0516     if (NULL == buf)
0517         return -ENOMEM;
0518 
0519     if (chip->period_size > AUDIO_LINE_SIZE)
0520         chip->period_size = AUDIO_LINE_SIZE;
0521 
0522     buf->bpl = chip->period_size;
0523     chip->buf = buf;
0524 
0525     ret = cx25821_alsa_dma_init(chip,
0526             (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
0527     if (ret < 0)
0528         goto error;
0529 
0530     ret = cx25821_alsa_dma_map(chip);
0531     if (ret < 0)
0532         goto error;
0533 
0534     ret = cx25821_risc_databuffer_audio(chip->pci, &buf->risc, buf->sglist,
0535             chip->period_size, chip->num_periods, 1);
0536     if (ret < 0) {
0537         pr_info("DEBUG: ERROR after cx25821_risc_databuffer_audio()\n");
0538         goto error;
0539     }
0540 
0541     /* Loop back to start of program */
0542     buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
0543     buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
0544     buf->risc.jmp[2] = cpu_to_le32(0);  /* bits 63-32 */
0545 
0546     substream->runtime->dma_area = chip->buf->vaddr;
0547     substream->runtime->dma_bytes = chip->dma_size;
0548     substream->runtime->dma_addr = 0;
0549 
0550     return 0;
0551 
0552 error:
0553     chip->buf = NULL;
0554     kfree(buf);
0555     return ret;
0556 }
0557 
0558 /*
0559  * hw free callback
0560  */
0561 static int snd_cx25821_hw_free(struct snd_pcm_substream *substream)
0562 {
0563     struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
0564 
0565     if (substream->runtime->dma_area) {
0566         dsp_buffer_free(chip);
0567         substream->runtime->dma_area = NULL;
0568     }
0569 
0570     return 0;
0571 }
0572 
0573 /*
0574  * prepare callback
0575  */
0576 static int snd_cx25821_prepare(struct snd_pcm_substream *substream)
0577 {
0578     return 0;
0579 }
0580 
0581 /*
0582  * trigger callback
0583  */
0584 static int snd_cx25821_card_trigger(struct snd_pcm_substream *substream,
0585                     int cmd)
0586 {
0587     struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
0588     int err = 0;
0589 
0590     /* Local interrupts are already disabled by ALSA */
0591     spin_lock(&chip->reg_lock);
0592 
0593     switch (cmd) {
0594     case SNDRV_PCM_TRIGGER_START:
0595         err = _cx25821_start_audio_dma(chip);
0596         break;
0597     case SNDRV_PCM_TRIGGER_STOP:
0598         err = _cx25821_stop_audio_dma(chip);
0599         break;
0600     default:
0601         err = -EINVAL;
0602         break;
0603     }
0604 
0605     spin_unlock(&chip->reg_lock);
0606 
0607     return err;
0608 }
0609 
0610 /*
0611  * pointer callback
0612  */
0613 static snd_pcm_uframes_t snd_cx25821_pointer(struct snd_pcm_substream
0614                          *substream)
0615 {
0616     struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
0617     struct snd_pcm_runtime *runtime = substream->runtime;
0618     u16 count;
0619 
0620     count = atomic_read(&chip->count);
0621 
0622     return runtime->period_size * (count & (runtime->periods - 1));
0623 }
0624 
0625 /*
0626  * page callback (needed for mmap)
0627  */
0628 static struct page *snd_cx25821_page(struct snd_pcm_substream *substream,
0629                      unsigned long offset)
0630 {
0631     void *pageptr = substream->runtime->dma_area + offset;
0632 
0633     return vmalloc_to_page(pageptr);
0634 }
0635 
0636 /*
0637  * operators
0638  */
0639 static const struct snd_pcm_ops snd_cx25821_pcm_ops = {
0640     .open = snd_cx25821_pcm_open,
0641     .close = snd_cx25821_close,
0642     .hw_params = snd_cx25821_hw_params,
0643     .hw_free = snd_cx25821_hw_free,
0644     .prepare = snd_cx25821_prepare,
0645     .trigger = snd_cx25821_card_trigger,
0646     .pointer = snd_cx25821_pointer,
0647     .page = snd_cx25821_page,
0648 };
0649 
0650 /*
0651  * ALSA create a PCM device:  Called when initializing the board.
0652  * Sets up the name and hooks up the callbacks
0653  */
0654 static int snd_cx25821_pcm(struct cx25821_audio_dev *chip, int device,
0655                char *name)
0656 {
0657     struct snd_pcm *pcm;
0658     int err;
0659 
0660     err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
0661     if (err < 0) {
0662         pr_info("ERROR: FAILED snd_pcm_new() in %s\n", __func__);
0663         return err;
0664     }
0665     pcm->private_data = chip;
0666     pcm->info_flags = 0;
0667     strscpy(pcm->name, name, sizeof(pcm->name));
0668     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx25821_pcm_ops);
0669 
0670     return 0;
0671 }
0672 
0673 /****************************************************************************
0674             Basic Flow for Sound Devices
0675  ****************************************************************************/
0676 
0677 /*
0678  * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
0679  * Only boards with eeprom and byte 1 at eeprom=1 have it
0680  */
0681 
0682 static const struct pci_device_id __maybe_unused cx25821_audio_pci_tbl[] = {
0683     {0x14f1, 0x0920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
0684     {0,}
0685 };
0686 
0687 MODULE_DEVICE_TABLE(pci, cx25821_audio_pci_tbl);
0688 
0689 /*
0690  * Alsa Constructor - Component probe
0691  */
0692 static int cx25821_audio_initdev(struct cx25821_dev *dev)
0693 {
0694     struct snd_card *card;
0695     struct cx25821_audio_dev *chip;
0696     int err;
0697 
0698     if (devno >= SNDRV_CARDS) {
0699         pr_info("DEBUG ERROR: devno >= SNDRV_CARDS %s\n", __func__);
0700         return -ENODEV;
0701     }
0702 
0703     if (!enable[devno]) {
0704         ++devno;
0705         pr_info("DEBUG ERROR: !enable[devno] %s\n", __func__);
0706         return -ENOENT;
0707     }
0708 
0709     err = snd_card_new(&dev->pci->dev, index[devno], id[devno],
0710                THIS_MODULE,
0711                sizeof(struct cx25821_audio_dev), &card);
0712     if (err < 0) {
0713         pr_info("DEBUG ERROR: cannot create snd_card_new in %s\n",
0714             __func__);
0715         return err;
0716     }
0717 
0718     strscpy(card->driver, "cx25821", sizeof(card->driver));
0719 
0720     /* Card "creation" */
0721     chip = card->private_data;
0722     spin_lock_init(&chip->reg_lock);
0723 
0724     chip->dev = dev;
0725     chip->card = card;
0726     chip->pci = dev->pci;
0727     chip->iobase = pci_resource_start(dev->pci, 0);
0728 
0729     chip->irq = dev->pci->irq;
0730 
0731     err = devm_request_irq(&dev->pci->dev, dev->pci->irq, cx25821_irq,
0732                    IRQF_SHARED, chip->dev->name, chip);
0733 
0734     if (err < 0) {
0735         pr_err("ERROR %s: can't get IRQ %d for ALSA\n", chip->dev->name,
0736             dev->pci->irq);
0737         goto error;
0738     }
0739 
0740     err = snd_cx25821_pcm(chip, 0, "cx25821 Digital");
0741     if (err < 0) {
0742         pr_info("DEBUG ERROR: cannot create snd_cx25821_pcm %s\n",
0743             __func__);
0744         goto error;
0745     }
0746 
0747     strscpy(card->shortname, "cx25821", sizeof(card->shortname));
0748     sprintf(card->longname, "%s at 0x%lx irq %d", chip->dev->name,
0749         chip->iobase, chip->irq);
0750     strscpy(card->mixername, "CX25821", sizeof(card->mixername));
0751 
0752     pr_info("%s/%i: ALSA support for cx25821 boards\n", card->driver,
0753         devno);
0754 
0755     err = snd_card_register(card);
0756     if (err < 0) {
0757         pr_info("DEBUG ERROR: cannot register sound card %s\n",
0758             __func__);
0759         goto error;
0760     }
0761 
0762     dev->card = card;
0763     devno++;
0764     return 0;
0765 
0766 error:
0767     snd_card_free(card);
0768     return err;
0769 }
0770 
0771 /****************************************************************************
0772                 LINUX MODULE INIT
0773  ****************************************************************************/
0774 
0775 static int cx25821_alsa_exit_callback(struct device *dev, void *data)
0776 {
0777     struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
0778     struct cx25821_dev *cxdev = get_cx25821(v4l2_dev);
0779 
0780     snd_card_free(cxdev->card);
0781     return 0;
0782 }
0783 
0784 static void cx25821_audio_fini(void)
0785 {
0786     struct device_driver *drv = driver_find("cx25821", &pci_bus_type);
0787     int ret;
0788 
0789     ret = driver_for_each_device(drv, NULL, NULL, cx25821_alsa_exit_callback);
0790     if (ret)
0791         pr_err("%s failed to find a cx25821 driver.\n", __func__);
0792 }
0793 
0794 static int cx25821_alsa_init_callback(struct device *dev, void *data)
0795 {
0796     struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
0797     struct cx25821_dev *cxdev = get_cx25821(v4l2_dev);
0798 
0799     cx25821_audio_initdev(cxdev);
0800     return 0;
0801 }
0802 
0803 /*
0804  * Module initializer
0805  *
0806  * Loops through present saa7134 cards, and assigns an ALSA device
0807  * to each one
0808  *
0809  */
0810 static int cx25821_alsa_init(void)
0811 {
0812     struct device_driver *drv = driver_find("cx25821", &pci_bus_type);
0813 
0814     return driver_for_each_device(drv, NULL, NULL, cx25821_alsa_init_callback);
0815 
0816 }
0817 
0818 late_initcall(cx25821_alsa_init);
0819 module_exit(cx25821_audio_fini);