linux/drivers/media/pci/saa7134/saa7134-alsa.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 *   SAA713x ALSA support for V4L
   4 */
   5
   6#include "saa7134.h"
   7#include "saa7134-reg.h"
   8
   9#include <linux/init.h>
  10#include <linux/slab.h>
  11#include <linux/time.h>
  12#include <linux/wait.h>
  13#include <linux/module.h>
  14#include <sound/core.h>
  15#include <sound/control.h>
  16#include <sound/pcm.h>
  17#include <sound/pcm_params.h>
  18#include <sound/initval.h>
  19#include <linux/interrupt.h>
  20#include <linux/vmalloc.h>
  21
  22/*
  23 * Configuration macros
  24 */
  25
  26/* defaults */
  27#define MIXER_ADDR_UNSELECTED   -1
  28#define MIXER_ADDR_TVTUNER      0
  29#define MIXER_ADDR_LINE1        1
  30#define MIXER_ADDR_LINE2        2
  31#define MIXER_ADDR_LAST         2
  32
  33
  34static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
  35static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
  36static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
  37
  38module_param_array(index, int, NULL, 0444);
  39module_param_array(enable, int, NULL, 0444);
  40MODULE_PARM_DESC(index, "Index value for SAA7134 capture interface(s).");
  41MODULE_PARM_DESC(enable, "Enable (or not) the SAA7134 capture interface(s).");
  42
  43/*
  44 * Main chip structure
  45 */
  46
  47typedef struct snd_card_saa7134 {
  48        struct snd_card *card;
  49        spinlock_t mixer_lock;
  50        int mixer_volume[MIXER_ADDR_LAST+1][2];
  51        int capture_source_addr;
  52        int capture_source[2];
  53        struct snd_kcontrol *capture_ctl[MIXER_ADDR_LAST+1];
  54        struct pci_dev *pci;
  55        struct saa7134_dev *dev;
  56
  57        unsigned long iobase;
  58        s16 irq;
  59        u16 mute_was_on;
  60
  61        spinlock_t lock;
  62} snd_card_saa7134_t;
  63
  64
  65/*
  66 * PCM structure
  67 */
  68
  69typedef struct snd_card_saa7134_pcm {
  70        struct saa7134_dev *dev;
  71
  72        spinlock_t lock;
  73
  74        struct snd_pcm_substream *substream;
  75} snd_card_saa7134_pcm_t;
  76
  77static struct snd_card *snd_saa7134_cards[SNDRV_CARDS];
  78
  79
  80/*
  81 * saa7134 DMA audio stop
  82 *
  83 *   Called when the capture device is released or the buffer overflows
  84 *
  85 *   - Copied verbatim from saa7134-oss's dsp_dma_stop.
  86 *
  87 */
  88
  89static void saa7134_dma_stop(struct saa7134_dev *dev)
  90{
  91        dev->dmasound.dma_blk     = -1;
  92        dev->dmasound.dma_running = 0;
  93        saa7134_set_dmabits(dev);
  94}
  95
  96/*
  97 * saa7134 DMA audio start
  98 *
  99 *   Called when preparing the capture device for use
 100 *
 101 *   - Copied verbatim from saa7134-oss's dsp_dma_start.
 102 *
 103 */
 104
 105static void saa7134_dma_start(struct saa7134_dev *dev)
 106{
 107        dev->dmasound.dma_blk     = 0;
 108        dev->dmasound.dma_running = 1;
 109        saa7134_set_dmabits(dev);
 110}
 111
 112/*
 113 * saa7134 audio DMA IRQ handler
 114 *
 115 *   Called whenever we get an SAA7134_IRQ_REPORT_DONE_RA3 interrupt
 116 *   Handles shifting between the 2 buffers, manages the read counters,
 117 *  and notifies ALSA when periods elapse
 118 *
 119 *   - Mostly copied from saa7134-oss's saa7134_irq_oss_done.
 120 *
 121 */
 122
 123static void saa7134_irq_alsa_done(struct saa7134_dev *dev,
 124                                  unsigned long status)
 125{
 126        int next_blk, reg = 0;
 127
 128        spin_lock(&dev->slock);
 129        if (UNSET == dev->dmasound.dma_blk) {
 130                pr_debug("irq: recording stopped\n");
 131                goto done;
 132        }
 133        if (0 != (status & 0x0f000000))
 134                pr_debug("irq: lost %ld\n", (status >> 24) & 0x0f);
 135        if (0 == (status & 0x10000000)) {
 136                /* odd */
 137                if (0 == (dev->dmasound.dma_blk & 0x01))
 138                        reg = SAA7134_RS_BA1(6);
 139        } else {
 140                /* even */
 141                if (1 == (dev->dmasound.dma_blk & 0x01))
 142                        reg = SAA7134_RS_BA2(6);
 143        }
 144        if (0 == reg) {
 145                pr_debug("irq: field oops [%s]\n",
 146                        (status & 0x10000000) ? "even" : "odd");
 147                goto done;
 148        }
 149
 150        if (dev->dmasound.read_count >= dev->dmasound.blksize * (dev->dmasound.blocks-2)) {
 151                pr_debug("irq: overrun [full=%d/%d] - Blocks in %d\n",
 152                        dev->dmasound.read_count,
 153                        dev->dmasound.bufsize, dev->dmasound.blocks);
 154                spin_unlock(&dev->slock);
 155                snd_pcm_stop_xrun(dev->dmasound.substream);
 156                return;
 157        }
 158
 159        /* next block addr */
 160        next_blk = (dev->dmasound.dma_blk + 2) % dev->dmasound.blocks;
 161        saa_writel(reg,next_blk * dev->dmasound.blksize);
 162        pr_debug("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
 163                (status & 0x10000000) ? "even" : "odd ", next_blk,
 164                 next_blk * dev->dmasound.blksize, dev->dmasound.blocks,
 165                 dev->dmasound.blksize, dev->dmasound.read_count);
 166
 167        /* update status & wake waiting readers */
 168        dev->dmasound.dma_blk = (dev->dmasound.dma_blk + 1) % dev->dmasound.blocks;
 169        dev->dmasound.read_count += dev->dmasound.blksize;
 170
 171        dev->dmasound.recording_on = reg;
 172
 173        if (dev->dmasound.read_count >= snd_pcm_lib_period_bytes(dev->dmasound.substream)) {
 174                spin_unlock(&dev->slock);
 175                snd_pcm_period_elapsed(dev->dmasound.substream);
 176                spin_lock(&dev->slock);
 177        }
 178
 179 done:
 180        spin_unlock(&dev->slock);
 181
 182}
 183
 184/*
 185 * IRQ request handler
 186 *
 187 *   Runs along with saa7134's IRQ handler, discards anything that isn't
 188 *   DMA sound
 189 *
 190 */
 191
 192static irqreturn_t saa7134_alsa_irq(int irq, void *dev_id)
 193{
 194        struct saa7134_dmasound *dmasound = dev_id;
 195        struct saa7134_dev *dev = dmasound->priv_data;
 196
 197        unsigned long report, status;
 198        int loop, handled = 0;
 199
 200        for (loop = 0; loop < 10; loop++) {
 201                report = saa_readl(SAA7134_IRQ_REPORT);
 202                status = saa_readl(SAA7134_IRQ_STATUS);
 203
 204                if (report & SAA7134_IRQ_REPORT_DONE_RA3) {
 205                        handled = 1;
 206                        saa_writel(SAA7134_IRQ_REPORT,
 207                                   SAA7134_IRQ_REPORT_DONE_RA3);
 208                        saa7134_irq_alsa_done(dev, status);
 209                } else {
 210                        goto out;
 211                }
 212        }
 213
 214        if (loop == 10) {
 215                pr_debug("error! looping IRQ!");
 216        }
 217
 218out:
 219        return IRQ_RETVAL(handled);
 220}
 221
 222/*
 223 * ALSA capture trigger
 224 *
 225 *   - One of the ALSA capture callbacks.
 226 *
 227 *   Called whenever a capture is started or stopped. Must be defined,
 228 *   but there's nothing we want to do here
 229 *
 230 */
 231
 232static int snd_card_saa7134_capture_trigger(struct snd_pcm_substream * substream,
 233                                          int cmd)
 234{
 235        struct snd_pcm_runtime *runtime = substream->runtime;
 236        snd_card_saa7134_pcm_t *pcm = runtime->private_data;
 237        struct saa7134_dev *dev=pcm->dev;
 238        int err = 0;
 239
 240        spin_lock(&dev->slock);
 241        if (cmd == SNDRV_PCM_TRIGGER_START) {
 242                /* start dma */
 243                saa7134_dma_start(dev);
 244        } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
 245                /* stop dma */
 246                saa7134_dma_stop(dev);
 247        } else {
 248                err = -EINVAL;
 249        }
 250        spin_unlock(&dev->slock);
 251
 252        return err;
 253}
 254
 255static int saa7134_alsa_dma_init(struct saa7134_dev *dev, int nr_pages)
 256{
 257        struct saa7134_dmasound *dma = &dev->dmasound;
 258        struct page *pg;
 259        int i;
 260
 261        dma->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
 262        if (NULL == dma->vaddr) {
 263                pr_debug("vmalloc_32(%d pages) failed\n", nr_pages);
 264                return -ENOMEM;
 265        }
 266
 267        pr_debug("vmalloc is at addr %p, size=%d\n",
 268                 dma->vaddr, nr_pages << PAGE_SHIFT);
 269
 270        memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
 271        dma->nr_pages = nr_pages;
 272
 273        dma->sglist = vzalloc(array_size(sizeof(*dma->sglist), dma->nr_pages));
 274        if (NULL == dma->sglist)
 275                goto vzalloc_err;
 276
 277        sg_init_table(dma->sglist, dma->nr_pages);
 278        for (i = 0; i < dma->nr_pages; i++) {
 279                pg = vmalloc_to_page(dma->vaddr + i * PAGE_SIZE);
 280                if (NULL == pg)
 281                        goto vmalloc_to_page_err;
 282                sg_set_page(&dma->sglist[i], pg, PAGE_SIZE, 0);
 283        }
 284        return 0;
 285
 286vmalloc_to_page_err:
 287        vfree(dma->sglist);
 288        dma->sglist = NULL;
 289vzalloc_err:
 290        vfree(dma->vaddr);
 291        dma->vaddr = NULL;
 292        return -ENOMEM;
 293}
 294
 295static int saa7134_alsa_dma_map(struct saa7134_dev *dev)
 296{
 297        struct saa7134_dmasound *dma = &dev->dmasound;
 298
 299        dma->sglen = dma_map_sg(&dev->pci->dev, dma->sglist,
 300                        dma->nr_pages, PCI_DMA_FROMDEVICE);
 301
 302        if (0 == dma->sglen) {
 303                pr_warn("%s: saa7134_alsa_map_sg failed\n", __func__);
 304                return -ENOMEM;
 305        }
 306        return 0;
 307}
 308
 309static int saa7134_alsa_dma_unmap(struct saa7134_dev *dev)
 310{
 311        struct saa7134_dmasound *dma = &dev->dmasound;
 312
 313        if (!dma->sglen)
 314                return 0;
 315
 316        dma_unmap_sg(&dev->pci->dev, dma->sglist, dma->sglen, PCI_DMA_FROMDEVICE);
 317        dma->sglen = 0;
 318        return 0;
 319}
 320
 321static int saa7134_alsa_dma_free(struct saa7134_dmasound *dma)
 322{
 323        vfree(dma->sglist);
 324        dma->sglist = NULL;
 325        vfree(dma->vaddr);
 326        dma->vaddr = NULL;
 327        return 0;
 328}
 329
 330/*
 331 * DMA buffer initialization
 332 *
 333 *   Uses V4L functions to initialize the DMA. Shouldn't be necessary in
 334 *  ALSA, but I was unable to use ALSA's own DMA, and had to force the
 335 *  usage of V4L's
 336 *
 337 *   - Copied verbatim from saa7134-oss.
 338 *
 339 */
 340
 341static int dsp_buffer_init(struct saa7134_dev *dev)
 342{
 343        int err;
 344
 345        BUG_ON(!dev->dmasound.bufsize);
 346
 347        err = saa7134_alsa_dma_init(dev,
 348                               (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
 349        if (0 != err)
 350                return err;
 351        return 0;
 352}
 353
 354/*
 355 * DMA buffer release
 356 *
 357 *   Called after closing the device, during snd_card_saa7134_capture_close
 358 *
 359 */
 360
 361static int dsp_buffer_free(struct saa7134_dev *dev)
 362{
 363        BUG_ON(!dev->dmasound.blksize);
 364
 365        saa7134_alsa_dma_free(&dev->dmasound);
 366
 367        dev->dmasound.blocks  = 0;
 368        dev->dmasound.blksize = 0;
 369        dev->dmasound.bufsize = 0;
 370
 371        return 0;
 372}
 373
 374/*
 375 * Setting the capture source and updating the ALSA controls
 376 */
 377static int snd_saa7134_capsrc_set(struct snd_kcontrol *kcontrol,
 378                                  int left, int right, bool force_notify)
 379{
 380        snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
 381        int change = 0, addr = kcontrol->private_value;
 382        int active, old_addr;
 383        u32 anabar, xbarin;
 384        int analog_io, rate;
 385        struct saa7134_dev *dev;
 386
 387        dev = chip->dev;
 388
 389        spin_lock_irq(&chip->mixer_lock);
 390
 391        active = left != 0 || right != 0;
 392        old_addr = chip->capture_source_addr;
 393
 394        /* The active capture source cannot be deactivated */
 395        if (active) {
 396                change = old_addr != addr ||
 397                         chip->capture_source[0] != left ||
 398                         chip->capture_source[1] != right;
 399
 400                chip->capture_source[0] = left;
 401                chip->capture_source[1] = right;
 402                chip->capture_source_addr = addr;
 403                dev->dmasound.input = addr;
 404        }
 405        spin_unlock_irq(&chip->mixer_lock);
 406
 407        if (change) {
 408                switch (dev->pci->device) {
 409
 410                case PCI_DEVICE_ID_PHILIPS_SAA7134:
 411                        switch (addr) {
 412                        case MIXER_ADDR_TVTUNER:
 413                                saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
 414                                           0xc0, 0xc0);
 415                                saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
 416                                           0x03, 0x00);
 417                                break;
 418                        case MIXER_ADDR_LINE1:
 419                        case MIXER_ADDR_LINE2:
 420                                analog_io = (MIXER_ADDR_LINE1 == addr) ?
 421                                             0x00 : 0x08;
 422                                rate = (32000 == dev->dmasound.rate) ?
 423                                        0x01 : 0x03;
 424                                saa_andorb(SAA7134_ANALOG_IO_SELECT,
 425                                           0x08, analog_io);
 426                                saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
 427                                           0xc0, 0x80);
 428                                saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
 429                                           0x03, rate);
 430                                break;
 431                        }
 432
 433                        break;
 434                case PCI_DEVICE_ID_PHILIPS_SAA7133:
 435                case PCI_DEVICE_ID_PHILIPS_SAA7135:
 436                        xbarin = 0x03; /* adc */
 437                        anabar = 0;
 438                        switch (addr) {
 439                        case MIXER_ADDR_TVTUNER:
 440                                xbarin = 0; /* Demodulator */
 441                                anabar = 2; /* DACs */
 442                                break;
 443                        case MIXER_ADDR_LINE1:
 444                                anabar = 0;  /* aux1, aux1 */
 445                                break;
 446                        case MIXER_ADDR_LINE2:
 447                                anabar = 9;  /* aux2, aux2 */
 448                                break;
 449                        }
 450
 451                        /* output xbar always main channel */
 452                        saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1,
 453                                       0xbbbb10);
 454
 455                        if (left || right) {
 456                                /* We've got data, turn the input on */
 457                                saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
 458                                               xbarin);
 459                                saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
 460                        } else {
 461                                saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
 462                                               0);
 463                                saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
 464                        }
 465                        break;
 466                }
 467        }
 468
 469        if (change) {
 470                if (force_notify)
 471                        snd_ctl_notify(chip->card,
 472                                       SNDRV_CTL_EVENT_MASK_VALUE,
 473                                       &chip->capture_ctl[addr]->id);
 474
 475                if (old_addr != MIXER_ADDR_UNSELECTED && old_addr != addr)
 476                        snd_ctl_notify(chip->card,
 477                                       SNDRV_CTL_EVENT_MASK_VALUE,
 478                                       &chip->capture_ctl[old_addr]->id);
 479        }
 480
 481        return change;
 482}
 483
 484/*
 485 * ALSA PCM preparation
 486 *
 487 *   - One of the ALSA capture callbacks.
 488 *
 489 *   Called right after the capture device is opened, this function configures
 490 *  the buffer using the previously defined functions, allocates the memory,
 491 *  sets up the hardware registers, and then starts the DMA. When this function
 492 *  returns, the audio should be flowing.
 493 *
 494 */
 495
 496static int snd_card_saa7134_capture_prepare(struct snd_pcm_substream * substream)
 497{
 498        struct snd_pcm_runtime *runtime = substream->runtime;
 499        int bswap, sign;
 500        u32 fmt, control;
 501        snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
 502        struct saa7134_dev *dev;
 503        snd_card_saa7134_pcm_t *pcm = runtime->private_data;
 504
 505        pcm->dev->dmasound.substream = substream;
 506
 507        dev = saa7134->dev;
 508
 509        if (snd_pcm_format_width(runtime->format) == 8)
 510                fmt = 0x00;
 511        else
 512                fmt = 0x01;
 513
 514        if (snd_pcm_format_signed(runtime->format))
 515                sign = 1;
 516        else
 517                sign = 0;
 518
 519        if (snd_pcm_format_big_endian(runtime->format))
 520                bswap = 1;
 521        else
 522                bswap = 0;
 523
 524        switch (dev->pci->device) {
 525          case PCI_DEVICE_ID_PHILIPS_SAA7134:
 526                if (1 == runtime->channels)
 527                        fmt |= (1 << 3);
 528                if (2 == runtime->channels)
 529                        fmt |= (3 << 3);
 530                if (sign)
 531                        fmt |= 0x04;
 532
 533                fmt |= (MIXER_ADDR_TVTUNER == dev->dmasound.input) ? 0xc0 : 0x80;
 534                saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
 535                saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >>  8);
 536                saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
 537                saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
 538
 539                break;
 540          case PCI_DEVICE_ID_PHILIPS_SAA7133:
 541          case PCI_DEVICE_ID_PHILIPS_SAA7135:
 542                if (1 == runtime->channels)
 543                        fmt |= (1 << 4);
 544                if (2 == runtime->channels)
 545                        fmt |= (2 << 4);
 546                if (!sign)
 547                        fmt |= 0x04;
 548                saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -1);
 549                saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
 550                break;
 551        }
 552
 553        pr_debug("rec_start: afmt=%d ch=%d  =>  fmt=0x%x swap=%c\n",
 554                runtime->format, runtime->channels, fmt,
 555                bswap ? 'b' : '-');
 556        /* dma: setup channel 6 (= AUDIO) */
 557        control = SAA7134_RS_CONTROL_BURST_16 |
 558                SAA7134_RS_CONTROL_ME |
 559                (dev->dmasound.pt.dma >> 12);
 560        if (bswap)
 561                control |= SAA7134_RS_CONTROL_BSWAP;
 562
 563        saa_writel(SAA7134_RS_BA1(6),0);
 564        saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
 565        saa_writel(SAA7134_RS_PITCH(6),0);
 566        saa_writel(SAA7134_RS_CONTROL(6),control);
 567
 568        dev->dmasound.rate = runtime->rate;
 569
 570        /* Setup and update the card/ALSA controls */
 571        snd_saa7134_capsrc_set(saa7134->capture_ctl[dev->dmasound.input], 1, 1,
 572                               true);
 573
 574        return 0;
 575
 576}
 577
 578/*
 579 * ALSA pointer fetching
 580 *
 581 *   - One of the ALSA capture callbacks.
 582 *
 583 *   Called whenever a period elapses, it must return the current hardware
 584 *  position of the buffer.
 585 *   Also resets the read counter used to prevent overruns
 586 *
 587 */
 588
 589static snd_pcm_uframes_t
 590snd_card_saa7134_capture_pointer(struct snd_pcm_substream * substream)
 591{
 592        struct snd_pcm_runtime *runtime = substream->runtime;
 593        snd_card_saa7134_pcm_t *pcm = runtime->private_data;
 594        struct saa7134_dev *dev=pcm->dev;
 595
 596        if (dev->dmasound.read_count) {
 597                dev->dmasound.read_count  -= snd_pcm_lib_period_bytes(substream);
 598                dev->dmasound.read_offset += snd_pcm_lib_period_bytes(substream);
 599                if (dev->dmasound.read_offset == dev->dmasound.bufsize)
 600                        dev->dmasound.read_offset = 0;
 601        }
 602
 603        return bytes_to_frames(runtime, dev->dmasound.read_offset);
 604}
 605
 606/*
 607 * ALSA hardware capabilities definition
 608 *
 609 *  Report only 32kHz for ALSA:
 610 *
 611 *  - SAA7133/35 uses DDEP (DemDec Easy Programming mode), which works in 32kHz
 612 *    only
 613 *  - SAA7134 for TV mode uses DemDec mode (32kHz)
 614 *  - Radio works in 32kHz only
 615 *  - When recording 48kHz from Line1/Line2, switching of capture source to TV
 616 *    means
 617 *    switching to 32kHz without any frequency translation
 618 */
 619
 620static const struct snd_pcm_hardware snd_card_saa7134_capture =
 621{
 622        .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
 623                                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
 624                                 SNDRV_PCM_INFO_MMAP_VALID),
 625        .formats =              SNDRV_PCM_FMTBIT_S16_LE | \
 626                                SNDRV_PCM_FMTBIT_S16_BE | \
 627                                SNDRV_PCM_FMTBIT_S8 | \
 628                                SNDRV_PCM_FMTBIT_U8 | \
 629                                SNDRV_PCM_FMTBIT_U16_LE | \
 630                                SNDRV_PCM_FMTBIT_U16_BE,
 631        .rates =                SNDRV_PCM_RATE_32000,
 632        .rate_min =             32000,
 633        .rate_max =             32000,
 634        .channels_min =         1,
 635        .channels_max =         2,
 636        .buffer_bytes_max =     (256*1024),
 637        .period_bytes_min =     64,
 638        .period_bytes_max =     (256*1024),
 639        .periods_min =          4,
 640        .periods_max =          1024,
 641};
 642
 643static void snd_card_saa7134_runtime_free(struct snd_pcm_runtime *runtime)
 644{
 645        snd_card_saa7134_pcm_t *pcm = runtime->private_data;
 646
 647        kfree(pcm);
 648}
 649
 650
 651/*
 652 * ALSA hardware params
 653 *
 654 *   - One of the ALSA capture callbacks.
 655 *
 656 *   Called on initialization, right before the PCM preparation
 657 *
 658 */
 659
 660static int snd_card_saa7134_hw_params(struct snd_pcm_substream * substream,
 661                                      struct snd_pcm_hw_params * hw_params)
 662{
 663        snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
 664        struct saa7134_dev *dev;
 665        unsigned int period_size, periods;
 666        int err;
 667
 668        period_size = params_period_bytes(hw_params);
 669        periods = params_periods(hw_params);
 670
 671        if (period_size < 0x100 || period_size > 0x10000)
 672                return -EINVAL;
 673        if (periods < 4)
 674                return -EINVAL;
 675        if (period_size * periods > 1024 * 1024)
 676                return -EINVAL;
 677
 678        dev = saa7134->dev;
 679
 680        if (dev->dmasound.blocks == periods &&
 681            dev->dmasound.blksize == period_size)
 682                return 0;
 683
 684        /* release the old buffer */
 685        if (substream->runtime->dma_area) {
 686                saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
 687                saa7134_alsa_dma_unmap(dev);
 688                dsp_buffer_free(dev);
 689                substream->runtime->dma_area = NULL;
 690        }
 691        dev->dmasound.blocks  = periods;
 692        dev->dmasound.blksize = period_size;
 693        dev->dmasound.bufsize = period_size * periods;
 694
 695        err = dsp_buffer_init(dev);
 696        if (0 != err) {
 697                dev->dmasound.blocks  = 0;
 698                dev->dmasound.blksize = 0;
 699                dev->dmasound.bufsize = 0;
 700                return err;
 701        }
 702
 703        err = saa7134_alsa_dma_map(dev);
 704        if (err) {
 705                dsp_buffer_free(dev);
 706                return err;
 707        }
 708        err = saa7134_pgtable_alloc(dev->pci, &dev->dmasound.pt);
 709        if (err) {
 710                saa7134_alsa_dma_unmap(dev);
 711                dsp_buffer_free(dev);
 712                return err;
 713        }
 714        err = saa7134_pgtable_build(dev->pci, &dev->dmasound.pt,
 715                                dev->dmasound.sglist, dev->dmasound.sglen, 0);
 716        if (err) {
 717                saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
 718                saa7134_alsa_dma_unmap(dev);
 719                dsp_buffer_free(dev);
 720                return err;
 721        }
 722
 723        /* I should be able to use runtime->dma_addr in the control
 724           byte, but it doesn't work. So I allocate the DMA using the
 725           V4L functions, and force ALSA to use that as the DMA area */
 726
 727        substream->runtime->dma_area = dev->dmasound.vaddr;
 728        substream->runtime->dma_bytes = dev->dmasound.bufsize;
 729        substream->runtime->dma_addr = 0;
 730
 731        return 0;
 732
 733}
 734
 735/*
 736 * ALSA hardware release
 737 *
 738 *   - One of the ALSA capture callbacks.
 739 *
 740 *   Called after closing the device, but before snd_card_saa7134_capture_close
 741 *   It stops the DMA audio and releases the buffers.
 742 *
 743 */
 744
 745static int snd_card_saa7134_hw_free(struct snd_pcm_substream * substream)
 746{
 747        snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
 748        struct saa7134_dev *dev;
 749
 750        dev = saa7134->dev;
 751
 752        if (substream->runtime->dma_area) {
 753                saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
 754                saa7134_alsa_dma_unmap(dev);
 755                dsp_buffer_free(dev);
 756                substream->runtime->dma_area = NULL;
 757        }
 758
 759        return 0;
 760}
 761
 762/*
 763 * ALSA capture finish
 764 *
 765 *   - One of the ALSA capture callbacks.
 766 *
 767 *   Called after closing the device.
 768 *
 769 */
 770
 771static int snd_card_saa7134_capture_close(struct snd_pcm_substream * substream)
 772{
 773        snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
 774        struct saa7134_dev *dev = saa7134->dev;
 775
 776        if (saa7134->mute_was_on) {
 777                dev->ctl_mute = 1;
 778                saa7134_tvaudio_setmute(dev);
 779        }
 780        return 0;
 781}
 782
 783/*
 784 * ALSA capture start
 785 *
 786 *   - One of the ALSA capture callbacks.
 787 *
 788 *   Called when opening the device. It creates and populates the PCM
 789 *  structure
 790 *
 791 */
 792
 793static int snd_card_saa7134_capture_open(struct snd_pcm_substream * substream)
 794{
 795        struct snd_pcm_runtime *runtime = substream->runtime;
 796        snd_card_saa7134_pcm_t *pcm;
 797        snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
 798        struct saa7134_dev *dev;
 799        int amux, err;
 800
 801        if (!saa7134) {
 802                pr_err("BUG: saa7134 can't find device struct. Can't proceed with open\n");
 803                return -ENODEV;
 804        }
 805        dev = saa7134->dev;
 806        mutex_lock(&dev->dmasound.lock);
 807
 808        dev->dmasound.read_count  = 0;
 809        dev->dmasound.read_offset = 0;
 810
 811        amux = dev->input->amux;
 812        if ((amux < 1) || (amux > 3))
 813                amux = 1;
 814        dev->dmasound.input  =  amux - 1;
 815
 816        mutex_unlock(&dev->dmasound.lock);
 817
 818        pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
 819        if (pcm == NULL)
 820                return -ENOMEM;
 821
 822        pcm->dev=saa7134->dev;
 823
 824        spin_lock_init(&pcm->lock);
 825
 826        pcm->substream = substream;
 827        runtime->private_data = pcm;
 828        runtime->private_free = snd_card_saa7134_runtime_free;
 829        runtime->hw = snd_card_saa7134_capture;
 830
 831        if (dev->ctl_mute != 0) {
 832                saa7134->mute_was_on = 1;
 833                dev->ctl_mute = 0;
 834                saa7134_tvaudio_setmute(dev);
 835        }
 836
 837        err = snd_pcm_hw_constraint_integer(runtime,
 838                                                SNDRV_PCM_HW_PARAM_PERIODS);
 839        if (err < 0)
 840                return err;
 841
 842        err = snd_pcm_hw_constraint_step(runtime, 0,
 843                                                SNDRV_PCM_HW_PARAM_PERIODS, 2);
 844        if (err < 0)
 845                return err;
 846
 847        return 0;
 848}
 849
 850/*
 851 * page callback (needed for mmap)
 852 */
 853
 854static struct page *snd_card_saa7134_page(struct snd_pcm_substream *substream,
 855                                        unsigned long offset)
 856{
 857        void *pageptr = substream->runtime->dma_area + offset;
 858        return vmalloc_to_page(pageptr);
 859}
 860
 861/*
 862 * ALSA capture callbacks definition
 863 */
 864
 865static const struct snd_pcm_ops snd_card_saa7134_capture_ops = {
 866        .open =                 snd_card_saa7134_capture_open,
 867        .close =                snd_card_saa7134_capture_close,
 868        .hw_params =            snd_card_saa7134_hw_params,
 869        .hw_free =              snd_card_saa7134_hw_free,
 870        .prepare =              snd_card_saa7134_capture_prepare,
 871        .trigger =              snd_card_saa7134_capture_trigger,
 872        .pointer =              snd_card_saa7134_capture_pointer,
 873        .page =                 snd_card_saa7134_page,
 874};
 875
 876/*
 877 * ALSA PCM setup
 878 *
 879 *   Called when initializing the board. Sets up the name and hooks up
 880 *  the callbacks
 881 *
 882 */
 883
 884static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
 885{
 886        struct snd_pcm *pcm;
 887        int err;
 888
 889        if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
 890                return err;
 891        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
 892        pcm->private_data = saa7134;
 893        pcm->info_flags = 0;
 894        strscpy(pcm->name, "SAA7134 PCM", sizeof(pcm->name));
 895        return 0;
 896}
 897
 898#define SAA713x_VOLUME(xname, xindex, addr) \
 899{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
 900  .info = snd_saa7134_volume_info, \
 901  .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
 902  .private_value = addr }
 903
 904static int snd_saa7134_volume_info(struct snd_kcontrol * kcontrol,
 905                                   struct snd_ctl_elem_info * uinfo)
 906{
 907        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 908        uinfo->count = 2;
 909        uinfo->value.integer.min = 0;
 910        uinfo->value.integer.max = 20;
 911        return 0;
 912}
 913
 914static int snd_saa7134_volume_get(struct snd_kcontrol * kcontrol,
 915                                  struct snd_ctl_elem_value * ucontrol)
 916{
 917        snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
 918        int addr = kcontrol->private_value;
 919
 920        ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
 921        ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
 922        return 0;
 923}
 924
 925static int snd_saa7134_volume_put(struct snd_kcontrol * kcontrol,
 926                                  struct snd_ctl_elem_value * ucontrol)
 927{
 928        snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
 929        struct saa7134_dev *dev = chip->dev;
 930
 931        int change, addr = kcontrol->private_value;
 932        int left, right;
 933
 934        left = ucontrol->value.integer.value[0];
 935        if (left < 0)
 936                left = 0;
 937        if (left > 20)
 938                left = 20;
 939        right = ucontrol->value.integer.value[1];
 940        if (right < 0)
 941                right = 0;
 942        if (right > 20)
 943                right = 20;
 944        spin_lock_irq(&chip->mixer_lock);
 945        change = 0;
 946        if (chip->mixer_volume[addr][0] != left) {
 947                change = 1;
 948                right = left;
 949        }
 950        if (chip->mixer_volume[addr][1] != right) {
 951                change = 1;
 952                left = right;
 953        }
 954        if (change) {
 955                switch (dev->pci->device) {
 956                        case PCI_DEVICE_ID_PHILIPS_SAA7134:
 957                                switch (addr) {
 958                                        case MIXER_ADDR_TVTUNER:
 959                                                left = 20;
 960                                                break;
 961                                        case MIXER_ADDR_LINE1:
 962                                                saa_andorb(SAA7134_ANALOG_IO_SELECT,  0x10,
 963                                                           (left > 10) ? 0x00 : 0x10);
 964                                                break;
 965                                        case MIXER_ADDR_LINE2:
 966                                                saa_andorb(SAA7134_ANALOG_IO_SELECT,  0x20,
 967                                                           (left > 10) ? 0x00 : 0x20);
 968                                                break;
 969                                }
 970                                break;
 971                        case PCI_DEVICE_ID_PHILIPS_SAA7133:
 972                        case PCI_DEVICE_ID_PHILIPS_SAA7135:
 973                                switch (addr) {
 974                                        case MIXER_ADDR_TVTUNER:
 975                                                left = 20;
 976                                                break;
 977                                        case MIXER_ADDR_LINE1:
 978                                                saa_andorb(0x0594,  0x10,
 979                                                           (left > 10) ? 0x00 : 0x10);
 980                                                break;
 981                                        case MIXER_ADDR_LINE2:
 982                                                saa_andorb(0x0594,  0x20,
 983                                                           (left > 10) ? 0x00 : 0x20);
 984                                                break;
 985                                }
 986                                break;
 987                }
 988                chip->mixer_volume[addr][0] = left;
 989                chip->mixer_volume[addr][1] = right;
 990        }
 991        spin_unlock_irq(&chip->mixer_lock);
 992        return change;
 993}
 994
 995#define SAA713x_CAPSRC(xname, xindex, addr) \
 996{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
 997  .info = snd_saa7134_capsrc_info, \
 998  .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
 999  .private_value = addr }
1000
1001static int snd_saa7134_capsrc_info(struct snd_kcontrol * kcontrol,
1002                                   struct snd_ctl_elem_info * uinfo)
1003{
1004        uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1005        uinfo->count = 2;
1006        uinfo->value.integer.min = 0;
1007        uinfo->value.integer.max = 1;
1008        return 0;
1009}
1010
1011static int snd_saa7134_capsrc_get(struct snd_kcontrol * kcontrol,
1012                                  struct snd_ctl_elem_value * ucontrol)
1013{
1014        snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
1015        int addr = kcontrol->private_value;
1016
1017        spin_lock_irq(&chip->mixer_lock);
1018        if (chip->capture_source_addr == addr) {
1019                ucontrol->value.integer.value[0] = chip->capture_source[0];
1020                ucontrol->value.integer.value[1] = chip->capture_source[1];
1021        } else {
1022                ucontrol->value.integer.value[0] = 0;
1023                ucontrol->value.integer.value[1] = 0;
1024        }
1025        spin_unlock_irq(&chip->mixer_lock);
1026
1027        return 0;
1028}
1029
1030static int snd_saa7134_capsrc_put(struct snd_kcontrol * kcontrol,
1031                                  struct snd_ctl_elem_value * ucontrol)
1032{
1033        int left, right;
1034        left = ucontrol->value.integer.value[0] & 1;
1035        right = ucontrol->value.integer.value[1] & 1;
1036
1037        return snd_saa7134_capsrc_set(kcontrol, left, right, false);
1038}
1039
1040static struct snd_kcontrol_new snd_saa7134_volume_controls[] = {
1041SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
1042SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
1043SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
1044};
1045
1046static struct snd_kcontrol_new snd_saa7134_capture_controls[] = {
1047SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
1048SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
1049SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
1050};
1051
1052/*
1053 * ALSA mixer setup
1054 *
1055 *   Called when initializing the board. Sets up the name and hooks up
1056 *  the callbacks
1057 *
1058 */
1059
1060static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
1061{
1062        struct snd_card *card = chip->card;
1063        struct snd_kcontrol *kcontrol;
1064        unsigned int idx;
1065        int err, addr;
1066
1067        strscpy(card->mixername, "SAA7134 Mixer", sizeof(card->mixername));
1068
1069        for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_volume_controls); idx++) {
1070                kcontrol = snd_ctl_new1(&snd_saa7134_volume_controls[idx],
1071                                        chip);
1072                err = snd_ctl_add(card, kcontrol);
1073                if (err < 0)
1074                        return err;
1075        }
1076
1077        for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_capture_controls); idx++) {
1078                kcontrol = snd_ctl_new1(&snd_saa7134_capture_controls[idx],
1079                                        chip);
1080                addr = snd_saa7134_capture_controls[idx].private_value;
1081                chip->capture_ctl[addr] = kcontrol;
1082                err = snd_ctl_add(card, kcontrol);
1083                if (err < 0)
1084                        return err;
1085        }
1086
1087        chip->capture_source_addr = MIXER_ADDR_UNSELECTED;
1088        return 0;
1089}
1090
1091static void snd_saa7134_free(struct snd_card * card)
1092{
1093        snd_card_saa7134_t *chip = card->private_data;
1094
1095        if (chip->dev->dmasound.priv_data == NULL)
1096                return;
1097
1098        if (chip->irq >= 0)
1099                free_irq(chip->irq, &chip->dev->dmasound);
1100
1101        chip->dev->dmasound.priv_data = NULL;
1102
1103}
1104
1105/*
1106 * ALSA initialization
1107 *
1108 *   Called by the init routine, once for each saa7134 device present,
1109 *  it creates the basic structures and registers the ALSA devices
1110 *
1111 */
1112
1113static int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
1114{
1115
1116        struct snd_card *card;
1117        snd_card_saa7134_t *chip;
1118        int err;
1119
1120
1121        if (devnum >= SNDRV_CARDS)
1122                return -ENODEV;
1123        if (!enable[devnum])
1124                return -ENODEV;
1125
1126        err = snd_card_new(&dev->pci->dev, index[devnum], id[devnum],
1127                           THIS_MODULE, sizeof(snd_card_saa7134_t), &card);
1128        if (err < 0)
1129                return err;
1130
1131        strscpy(card->driver, "SAA7134", sizeof(card->driver));
1132
1133        /* Card "creation" */
1134
1135        card->private_free = snd_saa7134_free;
1136        chip = card->private_data;
1137
1138        spin_lock_init(&chip->lock);
1139        spin_lock_init(&chip->mixer_lock);
1140
1141        chip->dev = dev;
1142
1143        chip->card = card;
1144
1145        chip->pci = dev->pci;
1146        chip->iobase = pci_resource_start(dev->pci, 0);
1147
1148
1149        err = request_irq(dev->pci->irq, saa7134_alsa_irq,
1150                                IRQF_SHARED, dev->name,
1151                                (void*) &dev->dmasound);
1152
1153        if (err < 0) {
1154                pr_err("%s: can't get IRQ %d for ALSA\n",
1155                        dev->name, dev->pci->irq);
1156                goto __nodev;
1157        }
1158
1159        chip->irq = dev->pci->irq;
1160
1161        mutex_init(&dev->dmasound.lock);
1162
1163        if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
1164                goto __nodev;
1165
1166        if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
1167                goto __nodev;
1168
1169        /* End of "creation" */
1170
1171        strscpy(card->shortname, "SAA7134", sizeof(card->shortname));
1172        sprintf(card->longname, "%s at 0x%lx irq %d",
1173                chip->dev->name, chip->iobase, chip->irq);
1174
1175        pr_info("%s/alsa: %s registered as card %d\n",
1176                dev->name, card->longname, index[devnum]);
1177
1178        if ((err = snd_card_register(card)) == 0) {
1179                snd_saa7134_cards[devnum] = card;
1180                return 0;
1181        }
1182
1183__nodev:
1184        snd_card_free(card);
1185        return err;
1186}
1187
1188
1189static int alsa_device_init(struct saa7134_dev *dev)
1190{
1191        dev->dmasound.priv_data = dev;
1192        alsa_card_saa7134_create(dev,dev->nr);
1193        return 1;
1194}
1195
1196static int alsa_device_exit(struct saa7134_dev *dev)
1197{
1198        if (!snd_saa7134_cards[dev->nr])
1199                return 1;
1200
1201        snd_card_free(snd_saa7134_cards[dev->nr]);
1202        snd_saa7134_cards[dev->nr] = NULL;
1203        return 1;
1204}
1205
1206/*
1207 * Module initializer
1208 *
1209 * Loops through present saa7134 cards, and assigns an ALSA device
1210 * to each one
1211 *
1212 */
1213
1214static int saa7134_alsa_init(void)
1215{
1216        struct saa7134_dev *dev = NULL;
1217        struct list_head *list;
1218
1219        saa7134_dmasound_init = alsa_device_init;
1220        saa7134_dmasound_exit = alsa_device_exit;
1221
1222        pr_info("saa7134 ALSA driver for DMA sound loaded\n");
1223
1224        list_for_each(list,&saa7134_devlist) {
1225                dev = list_entry(list, struct saa7134_dev, devlist);
1226                if (dev->pci->device == PCI_DEVICE_ID_PHILIPS_SAA7130)
1227                        pr_info("%s/alsa: %s doesn't support digital audio\n",
1228                                dev->name, saa7134_boards[dev->board].name);
1229                else
1230                        alsa_device_init(dev);
1231        }
1232
1233        if (dev == NULL)
1234                pr_info("saa7134 ALSA: no saa7134 cards found\n");
1235
1236        return 0;
1237
1238}
1239
1240/*
1241 * Module destructor
1242 */
1243
1244static void saa7134_alsa_exit(void)
1245{
1246        int idx;
1247
1248        for (idx = 0; idx < SNDRV_CARDS; idx++) {
1249                if (snd_saa7134_cards[idx])
1250                        snd_card_free(snd_saa7134_cards[idx]);
1251        }
1252
1253        saa7134_dmasound_init = NULL;
1254        saa7134_dmasound_exit = NULL;
1255        pr_info("saa7134 ALSA driver for DMA sound unloaded\n");
1256
1257        return;
1258}
1259
1260/* We initialize this late, to make sure the sound system is up and running */
1261late_initcall(saa7134_alsa_init);
1262module_exit(saa7134_alsa_exit);
1263MODULE_LICENSE("GPL");
1264MODULE_AUTHOR("Ricardo Cerqueira");
1265