linux/sound/pci/ad1889.c
<<
>>
Prefs
   1/* Analog Devices 1889 audio driver
   2 *
   3 * This is a driver for the AD1889 PCI audio chipset found
   4 * on the HP PA-RISC [BCJ]-xxx0 workstations.
   5 *
   6 * Copyright (C) 2004-2005, Kyle McMartin <kyle@parisc-linux.org>
   7 * Copyright (C) 2005, Thibaut Varene <varenet@parisc-linux.org>
   8 *   Based on the OSS AD1889 driver by Randolph Chung <tausq@debian.org>
   9 *
  10 * This program is free software; you can redistribute it and/or modify
  11 * it under the terms of the GNU General Public License, version 2, as
  12 * published by the Free Software Foundation.
  13 *
  14 * This program is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17 * GNU General Public License for more details.
  18 *
  19 * You should have received a copy of the GNU General Public License
  20 * along with this program; if not, write to the Free Software
  21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22 *
  23 * TODO:
  24 *      Do we need to take care of CCS register?
  25 *      Maybe we could use finer grained locking (separate locks for pb/cap)?
  26 * Wishlist:
  27 *      Control Interface (mixer) support
  28 *      Better AC97 support (VSR...)?
  29 *      PM support
  30 *      MIDI support
  31 *      Game Port support
  32 *      SG DMA support (this will need *alot* of work)
  33 */
  34
  35#include <linux/init.h>
  36#include <linux/pci.h>
  37#include <linux/dma-mapping.h>
  38#include <linux/slab.h>
  39#include <linux/interrupt.h>
  40#include <linux/compiler.h>
  41#include <linux/delay.h>
  42
  43#include <sound/core.h>
  44#include <sound/pcm.h>
  45#include <sound/initval.h>
  46#include <sound/ac97_codec.h>
  47
  48#include <asm/io.h>
  49
  50#include "ad1889.h"
  51#include "ac97/ac97_id.h"
  52
  53#define AD1889_DRVVER   "Version: 1.7"
  54
  55MODULE_AUTHOR("Kyle McMartin <kyle@parisc-linux.org>, Thibaut Varene <t-bone@parisc-linux.org>");
  56MODULE_DESCRIPTION("Analog Devices AD1889 ALSA sound driver");
  57MODULE_LICENSE("GPL");
  58MODULE_SUPPORTED_DEVICE("{{Analog Devices,AD1889}}");
  59
  60static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  61module_param_array(index, int, NULL, 0444);
  62MODULE_PARM_DESC(index, "Index value for the AD1889 soundcard.");
  63
  64static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  65module_param_array(id, charp, NULL, 0444);
  66MODULE_PARM_DESC(id, "ID string for the AD1889 soundcard.");
  67
  68static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  69module_param_array(enable, bool, NULL, 0444);
  70MODULE_PARM_DESC(enable, "Enable AD1889 soundcard.");
  71
  72static char *ac97_quirk[SNDRV_CARDS];
  73module_param_array(ac97_quirk, charp, NULL, 0444);
  74MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
  75
  76#define DEVNAME "ad1889"
  77#define PFX     DEVNAME ": "
  78
  79/* let's use the global sound debug interfaces */
  80#define ad1889_debug(fmt, arg...) snd_printd(KERN_DEBUG fmt, ## arg)
  81
  82/* keep track of some hw registers */
  83struct ad1889_register_state {
  84        u16 reg;        /* reg setup */
  85        u32 addr;       /* dma base address */
  86        unsigned long size;     /* DMA buffer size */
  87};
  88
  89struct snd_ad1889 {
  90        struct snd_card *card;
  91        struct pci_dev *pci;
  92
  93        int irq;
  94        unsigned long bar;
  95        void __iomem *iobase;
  96
  97        struct snd_ac97 *ac97;
  98        struct snd_ac97_bus *ac97_bus;
  99        struct snd_pcm *pcm;
 100        struct snd_info_entry *proc;
 101
 102        struct snd_pcm_substream *psubs;
 103        struct snd_pcm_substream *csubs;
 104
 105        /* playback register state */
 106        struct ad1889_register_state wave;
 107        struct ad1889_register_state ramc;
 108
 109        spinlock_t lock;
 110};
 111
 112static inline u16
 113ad1889_readw(struct snd_ad1889 *chip, unsigned reg)
 114{
 115        return readw(chip->iobase + reg);
 116}
 117
 118static inline void
 119ad1889_writew(struct snd_ad1889 *chip, unsigned reg, u16 val)
 120{
 121        writew(val, chip->iobase + reg);
 122}
 123
 124static inline u32
 125ad1889_readl(struct snd_ad1889 *chip, unsigned reg)
 126{
 127        return readl(chip->iobase + reg);
 128}
 129
 130static inline void
 131ad1889_writel(struct snd_ad1889 *chip, unsigned reg, u32 val)
 132{
 133        writel(val, chip->iobase + reg);
 134}
 135
 136static inline void
 137ad1889_unmute(struct snd_ad1889 *chip)
 138{
 139        u16 st;
 140        st = ad1889_readw(chip, AD_DS_WADA) & 
 141                ~(AD_DS_WADA_RWAM | AD_DS_WADA_LWAM);
 142        ad1889_writew(chip, AD_DS_WADA, st);
 143        ad1889_readw(chip, AD_DS_WADA);
 144}
 145
 146static inline void
 147ad1889_mute(struct snd_ad1889 *chip)
 148{
 149        u16 st;
 150        st = ad1889_readw(chip, AD_DS_WADA) | AD_DS_WADA_RWAM | AD_DS_WADA_LWAM;
 151        ad1889_writew(chip, AD_DS_WADA, st);
 152        ad1889_readw(chip, AD_DS_WADA);
 153}
 154
 155static inline void
 156ad1889_load_adc_buffer_address(struct snd_ad1889 *chip, u32 address)
 157{
 158        ad1889_writel(chip, AD_DMA_ADCBA, address);
 159        ad1889_writel(chip, AD_DMA_ADCCA, address);
 160}
 161
 162static inline void
 163ad1889_load_adc_buffer_count(struct snd_ad1889 *chip, u32 count)
 164{
 165        ad1889_writel(chip, AD_DMA_ADCBC, count);
 166        ad1889_writel(chip, AD_DMA_ADCCC, count);
 167}
 168
 169static inline void
 170ad1889_load_adc_interrupt_count(struct snd_ad1889 *chip, u32 count)
 171{
 172        ad1889_writel(chip, AD_DMA_ADCIB, count);
 173        ad1889_writel(chip, AD_DMA_ADCIC, count);
 174}
 175
 176static inline void
 177ad1889_load_wave_buffer_address(struct snd_ad1889 *chip, u32 address)
 178{
 179        ad1889_writel(chip, AD_DMA_WAVBA, address);
 180        ad1889_writel(chip, AD_DMA_WAVCA, address);
 181}
 182
 183static inline void
 184ad1889_load_wave_buffer_count(struct snd_ad1889 *chip, u32 count)
 185{
 186        ad1889_writel(chip, AD_DMA_WAVBC, count);
 187        ad1889_writel(chip, AD_DMA_WAVCC, count);
 188}
 189
 190static inline void
 191ad1889_load_wave_interrupt_count(struct snd_ad1889 *chip, u32 count)
 192{
 193        ad1889_writel(chip, AD_DMA_WAVIB, count);
 194        ad1889_writel(chip, AD_DMA_WAVIC, count);
 195}
 196
 197static void
 198ad1889_channel_reset(struct snd_ad1889 *chip, unsigned int channel)
 199{
 200        u16 reg;
 201        
 202        if (channel & AD_CHAN_WAV) {
 203                /* Disable wave channel */
 204                reg = ad1889_readw(chip, AD_DS_WSMC) & ~AD_DS_WSMC_WAEN;
 205                ad1889_writew(chip, AD_DS_WSMC, reg);
 206                chip->wave.reg = reg;
 207                
 208                /* disable IRQs */
 209                reg = ad1889_readw(chip, AD_DMA_WAV);
 210                reg &= AD_DMA_IM_DIS;
 211                reg &= ~AD_DMA_LOOP;
 212                ad1889_writew(chip, AD_DMA_WAV, reg);
 213
 214                /* clear IRQ and address counters and pointers */
 215                ad1889_load_wave_buffer_address(chip, 0x0);
 216                ad1889_load_wave_buffer_count(chip, 0x0);
 217                ad1889_load_wave_interrupt_count(chip, 0x0);
 218
 219                /* flush */
 220                ad1889_readw(chip, AD_DMA_WAV);
 221        }
 222        
 223        if (channel & AD_CHAN_ADC) {
 224                /* Disable ADC channel */
 225                reg = ad1889_readw(chip, AD_DS_RAMC) & ~AD_DS_RAMC_ADEN;
 226                ad1889_writew(chip, AD_DS_RAMC, reg);
 227                chip->ramc.reg = reg;
 228
 229                reg = ad1889_readw(chip, AD_DMA_ADC);
 230                reg &= AD_DMA_IM_DIS;
 231                reg &= ~AD_DMA_LOOP;
 232                ad1889_writew(chip, AD_DMA_ADC, reg);
 233        
 234                ad1889_load_adc_buffer_address(chip, 0x0);
 235                ad1889_load_adc_buffer_count(chip, 0x0);
 236                ad1889_load_adc_interrupt_count(chip, 0x0);
 237
 238                /* flush */
 239                ad1889_readw(chip, AD_DMA_ADC);
 240        }
 241}
 242
 243static u16
 244snd_ad1889_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
 245{
 246        struct snd_ad1889 *chip = ac97->private_data;
 247        return ad1889_readw(chip, AD_AC97_BASE + reg);
 248}
 249
 250static void
 251snd_ad1889_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val)
 252{
 253        struct snd_ad1889 *chip = ac97->private_data;
 254        ad1889_writew(chip, AD_AC97_BASE + reg, val);
 255}
 256
 257static int
 258snd_ad1889_ac97_ready(struct snd_ad1889 *chip)
 259{
 260        int retry = 400; /* average needs 352 msec */
 261        
 262        while (!(ad1889_readw(chip, AD_AC97_ACIC) & AD_AC97_ACIC_ACRDY) 
 263                        && --retry)
 264                mdelay(1);
 265        if (!retry) {
 266                snd_printk(KERN_ERR PFX "[%s] Link is not ready.\n",
 267                       __func__);
 268                return -EIO;
 269        }
 270        ad1889_debug("[%s] ready after %d ms\n", __func__, 400 - retry);
 271
 272        return 0;
 273}
 274
 275static int 
 276snd_ad1889_hw_params(struct snd_pcm_substream *substream,
 277                        struct snd_pcm_hw_params *hw_params)
 278{
 279        return snd_pcm_lib_malloc_pages(substream, 
 280                                        params_buffer_bytes(hw_params));
 281}
 282
 283static int
 284snd_ad1889_hw_free(struct snd_pcm_substream *substream)
 285{
 286        return snd_pcm_lib_free_pages(substream);
 287}
 288
 289static struct snd_pcm_hardware snd_ad1889_playback_hw = {
 290        .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
 291                SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BLOCK_TRANSFER,
 292        .formats = SNDRV_PCM_FMTBIT_S16_LE,
 293        .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
 294        .rate_min = 8000,       /* docs say 7000, but we're lazy */
 295        .rate_max = 48000,
 296        .channels_min = 1,
 297        .channels_max = 2,
 298        .buffer_bytes_max = BUFFER_BYTES_MAX,
 299        .period_bytes_min = PERIOD_BYTES_MIN,
 300        .period_bytes_max = PERIOD_BYTES_MAX,
 301        .periods_min = PERIODS_MIN,
 302        .periods_max = PERIODS_MAX,
 303        /*.fifo_size = 0,*/
 304};
 305
 306static struct snd_pcm_hardware snd_ad1889_capture_hw = {
 307        .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
 308                SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_BLOCK_TRANSFER,
 309        .formats = SNDRV_PCM_FMTBIT_S16_LE,
 310        .rates = SNDRV_PCM_RATE_48000,
 311        .rate_min = 48000,      /* docs say we could to VSR, but we're lazy */
 312        .rate_max = 48000,
 313        .channels_min = 1,
 314        .channels_max = 2,
 315        .buffer_bytes_max = BUFFER_BYTES_MAX,
 316        .period_bytes_min = PERIOD_BYTES_MIN,
 317        .period_bytes_max = PERIOD_BYTES_MAX,
 318        .periods_min = PERIODS_MIN,
 319        .periods_max = PERIODS_MAX,
 320        /*.fifo_size = 0,*/
 321};
 322
 323static int
 324snd_ad1889_playback_open(struct snd_pcm_substream *ss)
 325{
 326        struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
 327        struct snd_pcm_runtime *rt = ss->runtime;
 328
 329        chip->psubs = ss;
 330        rt->hw = snd_ad1889_playback_hw;
 331
 332        return 0;
 333}
 334
 335static int
 336snd_ad1889_capture_open(struct snd_pcm_substream *ss)
 337{
 338        struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
 339        struct snd_pcm_runtime *rt = ss->runtime;
 340
 341        chip->csubs = ss;
 342        rt->hw = snd_ad1889_capture_hw;
 343
 344        return 0;
 345}
 346
 347static int
 348snd_ad1889_playback_close(struct snd_pcm_substream *ss)
 349{
 350        struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
 351        chip->psubs = NULL;
 352        return 0;
 353}
 354
 355static int
 356snd_ad1889_capture_close(struct snd_pcm_substream *ss)
 357{
 358        struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
 359        chip->csubs = NULL;
 360        return 0;
 361}
 362
 363static int
 364snd_ad1889_playback_prepare(struct snd_pcm_substream *ss)
 365{
 366        struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
 367        struct snd_pcm_runtime *rt = ss->runtime;
 368        unsigned int size = snd_pcm_lib_buffer_bytes(ss);
 369        unsigned int count = snd_pcm_lib_period_bytes(ss);
 370        u16 reg;
 371
 372        ad1889_channel_reset(chip, AD_CHAN_WAV);
 373
 374        reg = ad1889_readw(chip, AD_DS_WSMC);
 375        
 376        /* Mask out 16-bit / Stereo */
 377        reg &= ~(AD_DS_WSMC_WA16 | AD_DS_WSMC_WAST);
 378
 379        if (snd_pcm_format_width(rt->format) == 16)
 380                reg |= AD_DS_WSMC_WA16;
 381
 382        if (rt->channels > 1)
 383                reg |= AD_DS_WSMC_WAST;
 384
 385        /* let's make sure we don't clobber ourselves */
 386        spin_lock_irq(&chip->lock);
 387        
 388        chip->wave.size = size;
 389        chip->wave.reg = reg;
 390        chip->wave.addr = rt->dma_addr;
 391
 392        ad1889_writew(chip, AD_DS_WSMC, chip->wave.reg);
 393        
 394        /* Set sample rates on the codec */
 395        ad1889_writew(chip, AD_DS_WAS, rt->rate);
 396
 397        /* Set up DMA */
 398        ad1889_load_wave_buffer_address(chip, chip->wave.addr);
 399        ad1889_load_wave_buffer_count(chip, size);
 400        ad1889_load_wave_interrupt_count(chip, count);
 401
 402        /* writes flush */
 403        ad1889_readw(chip, AD_DS_WSMC);
 404        
 405        spin_unlock_irq(&chip->lock);
 406        
 407        ad1889_debug("prepare playback: addr = 0x%x, count = %u, "
 408                        "size = %u, reg = 0x%x, rate = %u\n", chip->wave.addr,
 409                        count, size, reg, rt->rate);
 410        return 0;
 411}
 412
 413static int
 414snd_ad1889_capture_prepare(struct snd_pcm_substream *ss)
 415{
 416        struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
 417        struct snd_pcm_runtime *rt = ss->runtime;
 418        unsigned int size = snd_pcm_lib_buffer_bytes(ss);
 419        unsigned int count = snd_pcm_lib_period_bytes(ss);
 420        u16 reg;
 421
 422        ad1889_channel_reset(chip, AD_CHAN_ADC);
 423        
 424        reg = ad1889_readw(chip, AD_DS_RAMC);
 425
 426        /* Mask out 16-bit / Stereo */
 427        reg &= ~(AD_DS_RAMC_AD16 | AD_DS_RAMC_ADST);
 428
 429        if (snd_pcm_format_width(rt->format) == 16)
 430                reg |= AD_DS_RAMC_AD16;
 431
 432        if (rt->channels > 1)
 433                reg |= AD_DS_RAMC_ADST;
 434
 435        /* let's make sure we don't clobber ourselves */
 436        spin_lock_irq(&chip->lock);
 437        
 438        chip->ramc.size = size;
 439        chip->ramc.reg = reg;
 440        chip->ramc.addr = rt->dma_addr;
 441
 442        ad1889_writew(chip, AD_DS_RAMC, chip->ramc.reg);
 443
 444        /* Set up DMA */
 445        ad1889_load_adc_buffer_address(chip, chip->ramc.addr);
 446        ad1889_load_adc_buffer_count(chip, size);
 447        ad1889_load_adc_interrupt_count(chip, count);
 448
 449        /* writes flush */
 450        ad1889_readw(chip, AD_DS_RAMC);
 451        
 452        spin_unlock_irq(&chip->lock);
 453        
 454        ad1889_debug("prepare capture: addr = 0x%x, count = %u, "
 455                        "size = %u, reg = 0x%x, rate = %u\n", chip->ramc.addr,
 456                        count, size, reg, rt->rate);
 457        return 0;
 458}
 459
 460/* this is called in atomic context with IRQ disabled.
 461   Must be as fast as possible and not sleep.
 462   DMA should be *triggered* by this call.
 463   The WSMC "WAEN" bit triggers DMA Wave On/Off */
 464static int
 465snd_ad1889_playback_trigger(struct snd_pcm_substream *ss, int cmd)
 466{
 467        u16 wsmc;
 468        struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
 469        
 470        wsmc = ad1889_readw(chip, AD_DS_WSMC);
 471
 472        switch (cmd) {
 473        case SNDRV_PCM_TRIGGER_START:
 474                /* enable DMA loop & interrupts */
 475                ad1889_writew(chip, AD_DMA_WAV, AD_DMA_LOOP | AD_DMA_IM_CNT);
 476                wsmc |= AD_DS_WSMC_WAEN;
 477                /* 1 to clear CHSS bit */
 478                ad1889_writel(chip, AD_DMA_CHSS, AD_DMA_CHSS_WAVS);
 479                ad1889_unmute(chip);
 480                break;
 481        case SNDRV_PCM_TRIGGER_STOP:
 482                ad1889_mute(chip);
 483                wsmc &= ~AD_DS_WSMC_WAEN;
 484                break;
 485        default:
 486                snd_BUG();
 487                return -EINVAL;
 488        }
 489        
 490        chip->wave.reg = wsmc;
 491        ad1889_writew(chip, AD_DS_WSMC, wsmc);  
 492        ad1889_readw(chip, AD_DS_WSMC); /* flush */
 493
 494        /* reset the chip when STOP - will disable IRQs */
 495        if (cmd == SNDRV_PCM_TRIGGER_STOP)
 496                ad1889_channel_reset(chip, AD_CHAN_WAV);
 497
 498        return 0;
 499}
 500
 501/* this is called in atomic context with IRQ disabled.
 502   Must be as fast as possible and not sleep.
 503   DMA should be *triggered* by this call.
 504   The RAMC "ADEN" bit triggers DMA ADC On/Off */
 505static int
 506snd_ad1889_capture_trigger(struct snd_pcm_substream *ss, int cmd)
 507{
 508        u16 ramc;
 509        struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
 510
 511        ramc = ad1889_readw(chip, AD_DS_RAMC);
 512        
 513        switch (cmd) {
 514        case SNDRV_PCM_TRIGGER_START:
 515                /* enable DMA loop & interrupts */
 516                ad1889_writew(chip, AD_DMA_ADC, AD_DMA_LOOP | AD_DMA_IM_CNT);
 517                ramc |= AD_DS_RAMC_ADEN;
 518                /* 1 to clear CHSS bit */
 519                ad1889_writel(chip, AD_DMA_CHSS, AD_DMA_CHSS_ADCS);
 520                break;
 521        case SNDRV_PCM_TRIGGER_STOP:
 522                ramc &= ~AD_DS_RAMC_ADEN;
 523                break;
 524        default:
 525                return -EINVAL;
 526        }
 527        
 528        chip->ramc.reg = ramc;
 529        ad1889_writew(chip, AD_DS_RAMC, ramc);  
 530        ad1889_readw(chip, AD_DS_RAMC); /* flush */
 531        
 532        /* reset the chip when STOP - will disable IRQs */
 533        if (cmd == SNDRV_PCM_TRIGGER_STOP)
 534                ad1889_channel_reset(chip, AD_CHAN_ADC);
 535                
 536        return 0;
 537}
 538
 539/* Called in atomic context with IRQ disabled */
 540static snd_pcm_uframes_t
 541snd_ad1889_playback_pointer(struct snd_pcm_substream *ss)
 542{
 543        size_t ptr = 0;
 544        struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
 545
 546        if (unlikely(!(chip->wave.reg & AD_DS_WSMC_WAEN)))
 547                return 0;
 548
 549        ptr = ad1889_readl(chip, AD_DMA_WAVCA);
 550        ptr -= chip->wave.addr;
 551        
 552        snd_assert((ptr >= 0) && (ptr < chip->wave.size), return 0);
 553        
 554        return bytes_to_frames(ss->runtime, ptr);
 555}
 556
 557/* Called in atomic context with IRQ disabled */
 558static snd_pcm_uframes_t
 559snd_ad1889_capture_pointer(struct snd_pcm_substream *ss)
 560{
 561        size_t ptr = 0;
 562        struct snd_ad1889 *chip = snd_pcm_substream_chip(ss);
 563
 564        if (unlikely(!(chip->ramc.reg & AD_DS_RAMC_ADEN)))
 565                return 0;
 566
 567        ptr = ad1889_readl(chip, AD_DMA_ADCCA);
 568        ptr -= chip->ramc.addr;
 569
 570        snd_assert((ptr >= 0) && (ptr < chip->ramc.size), return 0);
 571        
 572        return bytes_to_frames(ss->runtime, ptr);
 573}
 574
 575static struct snd_pcm_ops snd_ad1889_playback_ops = {
 576        .open = snd_ad1889_playback_open,
 577        .close = snd_ad1889_playback_close,
 578        .ioctl = snd_pcm_lib_ioctl,
 579        .hw_params = snd_ad1889_hw_params,
 580        .hw_free = snd_ad1889_hw_free,
 581        .prepare = snd_ad1889_playback_prepare,
 582        .trigger = snd_ad1889_playback_trigger,
 583        .pointer = snd_ad1889_playback_pointer, 
 584};
 585
 586static struct snd_pcm_ops snd_ad1889_capture_ops = {
 587        .open = snd_ad1889_capture_open,
 588        .close = snd_ad1889_capture_close,
 589        .ioctl = snd_pcm_lib_ioctl,
 590        .hw_params = snd_ad1889_hw_params,
 591        .hw_free = snd_ad1889_hw_free,
 592        .prepare = snd_ad1889_capture_prepare,
 593        .trigger = snd_ad1889_capture_trigger,
 594        .pointer = snd_ad1889_capture_pointer, 
 595};
 596
 597static irqreturn_t
 598snd_ad1889_interrupt(int irq, void *dev_id)
 599{
 600        unsigned long st;
 601        struct snd_ad1889 *chip = dev_id;
 602
 603        st = ad1889_readl(chip, AD_DMA_DISR);
 604
 605        /* clear ISR */
 606        ad1889_writel(chip, AD_DMA_DISR, st);
 607
 608        st &= AD_INTR_MASK;
 609
 610        if (unlikely(!st))
 611                return IRQ_NONE;
 612
 613        if (st & (AD_DMA_DISR_PMAI|AD_DMA_DISR_PTAI))
 614                ad1889_debug("Unexpected master or target abort interrupt!\n");
 615
 616        if ((st & AD_DMA_DISR_WAVI) && chip->psubs)
 617                snd_pcm_period_elapsed(chip->psubs);
 618        if ((st & AD_DMA_DISR_ADCI) && chip->csubs)
 619                snd_pcm_period_elapsed(chip->csubs);
 620
 621        return IRQ_HANDLED;
 622}
 623
 624static int __devinit
 625snd_ad1889_pcm_init(struct snd_ad1889 *chip, int device, struct snd_pcm **rpcm)
 626{
 627        int err;
 628        struct snd_pcm *pcm;
 629
 630        if (rpcm)
 631                *rpcm = NULL;
 632
 633        err = snd_pcm_new(chip->card, chip->card->driver, device, 1, 1, &pcm);
 634        if (err < 0)
 635                return err;
 636
 637        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, 
 638                        &snd_ad1889_playback_ops);
 639        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
 640                        &snd_ad1889_capture_ops);
 641
 642        pcm->private_data = chip;
 643        pcm->info_flags = 0;
 644        strcpy(pcm->name, chip->card->shortname);
 645        
 646        chip->pcm = pcm;
 647        chip->psubs = NULL;
 648        chip->csubs = NULL;
 649
 650        err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
 651                                                snd_dma_pci_data(chip->pci),
 652                                                BUFFER_BYTES_MAX / 2,
 653                                                BUFFER_BYTES_MAX);
 654
 655        if (err < 0) {
 656                snd_printk(KERN_ERR PFX "buffer allocation error: %d\n", err);
 657                return err;
 658        }
 659        
 660        if (rpcm)
 661                *rpcm = pcm;
 662        
 663        return 0;
 664}
 665
 666static void
 667snd_ad1889_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
 668{
 669        struct snd_ad1889 *chip = entry->private_data;
 670        u16 reg;
 671        int tmp;
 672
 673        reg = ad1889_readw(chip, AD_DS_WSMC);
 674        snd_iprintf(buffer, "Wave output: %s\n",
 675                        (reg & AD_DS_WSMC_WAEN) ? "enabled" : "disabled");
 676        snd_iprintf(buffer, "Wave Channels: %s\n",
 677                        (reg & AD_DS_WSMC_WAST) ? "stereo" : "mono");
 678        snd_iprintf(buffer, "Wave Quality: %d-bit linear\n",
 679                        (reg & AD_DS_WSMC_WA16) ? 16 : 8);
 680        
 681        /* WARQ is at offset 12 */
 682        tmp = (reg & AD_DS_WSMC_WARQ) ?
 683                        (((reg & AD_DS_WSMC_WARQ >> 12) & 0x01) ? 12 : 18) : 4;
 684        tmp /= (reg & AD_DS_WSMC_WAST) ? 2 : 1;
 685        
 686        snd_iprintf(buffer, "Wave FIFO: %d %s words\n\n", tmp,
 687                        (reg & AD_DS_WSMC_WAST) ? "stereo" : "mono");
 688                                
 689        
 690        snd_iprintf(buffer, "Synthesis output: %s\n",
 691                        reg & AD_DS_WSMC_SYEN ? "enabled" : "disabled");
 692        
 693        /* SYRQ is at offset 4 */
 694        tmp = (reg & AD_DS_WSMC_SYRQ) ?
 695                        (((reg & AD_DS_WSMC_SYRQ >> 4) & 0x01) ? 12 : 18) : 4;
 696        tmp /= (reg & AD_DS_WSMC_WAST) ? 2 : 1;
 697        
 698        snd_iprintf(buffer, "Synthesis FIFO: %d %s words\n\n", tmp,
 699                        (reg & AD_DS_WSMC_WAST) ? "stereo" : "mono");
 700
 701        reg = ad1889_readw(chip, AD_DS_RAMC);
 702        snd_iprintf(buffer, "ADC input: %s\n",
 703                        (reg & AD_DS_RAMC_ADEN) ? "enabled" : "disabled");
 704        snd_iprintf(buffer, "ADC Channels: %s\n",
 705                        (reg & AD_DS_RAMC_ADST) ? "stereo" : "mono");
 706        snd_iprintf(buffer, "ADC Quality: %d-bit linear\n",
 707                        (reg & AD_DS_RAMC_AD16) ? 16 : 8);
 708        
 709        /* ACRQ is at offset 4 */
 710        tmp = (reg & AD_DS_RAMC_ACRQ) ?
 711                        (((reg & AD_DS_RAMC_ACRQ >> 4) & 0x01) ? 12 : 18) : 4;
 712        tmp /= (reg & AD_DS_RAMC_ADST) ? 2 : 1;
 713        
 714        snd_iprintf(buffer, "ADC FIFO: %d %s words\n\n", tmp,
 715                        (reg & AD_DS_RAMC_ADST) ? "stereo" : "mono");
 716        
 717        snd_iprintf(buffer, "Resampler input: %s\n",
 718                        reg & AD_DS_RAMC_REEN ? "enabled" : "disabled");
 719                        
 720        /* RERQ is at offset 12 */
 721        tmp = (reg & AD_DS_RAMC_RERQ) ?
 722                        (((reg & AD_DS_RAMC_RERQ >> 12) & 0x01) ? 12 : 18) : 4;
 723        tmp /= (reg & AD_DS_RAMC_ADST) ? 2 : 1;
 724        
 725        snd_iprintf(buffer, "Resampler FIFO: %d %s words\n\n", tmp,
 726                        (reg & AD_DS_WSMC_WAST) ? "stereo" : "mono");
 727                                
 728        
 729        /* doc says LSB represents -1.5dB, but the max value (-94.5dB)
 730        suggests that LSB is -3dB, which is more coherent with the logarithmic
 731        nature of the dB scale */
 732        reg = ad1889_readw(chip, AD_DS_WADA);
 733        snd_iprintf(buffer, "Left: %s, -%d dB\n",
 734                        (reg & AD_DS_WADA_LWAM) ? "mute" : "unmute",
 735                        ((reg & AD_DS_WADA_LWAA) >> 8) * 3);
 736        reg = ad1889_readw(chip, AD_DS_WADA);
 737        snd_iprintf(buffer, "Right: %s, -%d dB\n",
 738                        (reg & AD_DS_WADA_RWAM) ? "mute" : "unmute",
 739                        ((reg & AD_DS_WADA_RWAA) >> 8) * 3);
 740        
 741        reg = ad1889_readw(chip, AD_DS_WAS);
 742        snd_iprintf(buffer, "Wave samplerate: %u Hz\n", reg);
 743        reg = ad1889_readw(chip, AD_DS_RES);
 744        snd_iprintf(buffer, "Resampler samplerate: %u Hz\n", reg);
 745}
 746
 747static void __devinit
 748snd_ad1889_proc_init(struct snd_ad1889 *chip)
 749{
 750        struct snd_info_entry *entry;
 751
 752        if (!snd_card_proc_new(chip->card, chip->card->driver, &entry))
 753                snd_info_set_text_ops(entry, chip, snd_ad1889_proc_read);
 754}
 755
 756static struct ac97_quirk ac97_quirks[] = {
 757        {
 758                .subvendor = 0x11d4,    /* AD */
 759                .subdevice = 0x1889,    /* AD1889 */
 760                .codec_id = AC97_ID_AD1819,
 761                .name = "AD1889",
 762                .type = AC97_TUNE_HP_ONLY
 763        },
 764        { } /* terminator */
 765};
 766
 767static void __devinit
 768snd_ad1889_ac97_xinit(struct snd_ad1889 *chip)
 769{
 770        u16 reg;
 771
 772        reg = ad1889_readw(chip, AD_AC97_ACIC);
 773        reg |= AD_AC97_ACIC_ACRD;               /* Reset Disable */
 774        ad1889_writew(chip, AD_AC97_ACIC, reg);
 775        ad1889_readw(chip, AD_AC97_ACIC);       /* flush posted write */
 776        udelay(10);
 777        /* Interface Enable */
 778        reg |= AD_AC97_ACIC_ACIE;
 779        ad1889_writew(chip, AD_AC97_ACIC, reg);
 780        
 781        snd_ad1889_ac97_ready(chip);
 782
 783        /* Audio Stream Output | Variable Sample Rate Mode */
 784        reg = ad1889_readw(chip, AD_AC97_ACIC);
 785        reg |= AD_AC97_ACIC_ASOE | AD_AC97_ACIC_VSRM;
 786        ad1889_writew(chip, AD_AC97_ACIC, reg);
 787        ad1889_readw(chip, AD_AC97_ACIC); /* flush posted write */
 788
 789}
 790
 791static void
 792snd_ad1889_ac97_bus_free(struct snd_ac97_bus *bus)
 793{
 794        struct snd_ad1889 *chip = bus->private_data;
 795        chip->ac97_bus = NULL;
 796}
 797
 798static void
 799snd_ad1889_ac97_free(struct snd_ac97 *ac97)
 800{
 801        struct snd_ad1889 *chip = ac97->private_data;
 802        chip->ac97 = NULL;
 803}
 804
 805static int __devinit
 806snd_ad1889_ac97_init(struct snd_ad1889 *chip, const char *quirk_override)
 807{
 808        int err;
 809        struct snd_ac97_template ac97;
 810        static struct snd_ac97_bus_ops ops = {
 811                .write = snd_ad1889_ac97_write,
 812                .read = snd_ad1889_ac97_read,
 813        };
 814
 815        /* doing that here, it works. */
 816        snd_ad1889_ac97_xinit(chip);
 817
 818        err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus);
 819        if (err < 0)
 820                return err;
 821        
 822        chip->ac97_bus->private_free = snd_ad1889_ac97_bus_free;
 823
 824        memset(&ac97, 0, sizeof(ac97));
 825        ac97.private_data = chip;
 826        ac97.private_free = snd_ad1889_ac97_free;
 827        ac97.pci = chip->pci;
 828
 829        err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97);
 830        if (err < 0)
 831                return err;
 832                
 833        snd_ac97_tune_hardware(chip->ac97, ac97_quirks, quirk_override);
 834        
 835        return 0;
 836}
 837
 838static int
 839snd_ad1889_free(struct snd_ad1889 *chip)
 840{
 841        if (chip->irq < 0)
 842                goto skip_hw;
 843
 844        spin_lock_irq(&chip->lock);
 845
 846        ad1889_mute(chip);
 847
 848        /* Turn off interrupt on count and zero DMA registers */
 849        ad1889_channel_reset(chip, AD_CHAN_WAV | AD_CHAN_ADC);
 850
 851        /* clear DISR. If we don't, we'd better jump off the Eiffel Tower */
 852        ad1889_writel(chip, AD_DMA_DISR, AD_DMA_DISR_PTAI | AD_DMA_DISR_PMAI);
 853        ad1889_readl(chip, AD_DMA_DISR);        /* flush, dammit! */
 854
 855        spin_unlock_irq(&chip->lock);
 856
 857        if (chip->irq >= 0)
 858                free_irq(chip->irq, chip);
 859
 860skip_hw:
 861        if (chip->iobase)
 862                iounmap(chip->iobase);
 863
 864        pci_release_regions(chip->pci);
 865        pci_disable_device(chip->pci);
 866
 867        kfree(chip);
 868        return 0;
 869}
 870
 871static int
 872snd_ad1889_dev_free(struct snd_device *device) 
 873{
 874        struct snd_ad1889 *chip = device->device_data;
 875        return snd_ad1889_free(chip);
 876}
 877
 878static int __devinit
 879snd_ad1889_init(struct snd_ad1889 *chip) 
 880{
 881        ad1889_writew(chip, AD_DS_CCS, AD_DS_CCS_CLKEN); /* turn on clock */
 882        ad1889_readw(chip, AD_DS_CCS);  /* flush posted write */
 883
 884        mdelay(10);
 885
 886        /* enable Master and Target abort interrupts */
 887        ad1889_writel(chip, AD_DMA_DISR, AD_DMA_DISR_PMAE | AD_DMA_DISR_PTAE);
 888
 889        return 0;
 890}
 891
 892static int __devinit
 893snd_ad1889_create(struct snd_card *card,
 894                  struct pci_dev *pci,
 895                  struct snd_ad1889 **rchip)
 896{
 897        int err;
 898
 899        struct snd_ad1889 *chip;
 900        static struct snd_device_ops ops = {
 901                .dev_free = snd_ad1889_dev_free,
 902        };
 903
 904        *rchip = NULL;
 905
 906        if ((err = pci_enable_device(pci)) < 0)
 907                return err;
 908
 909        /* check PCI availability (32bit DMA) */
 910        if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 ||
 911            pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) {
 912                printk(KERN_ERR PFX "error setting 32-bit DMA mask.\n");
 913                pci_disable_device(pci);
 914                return -ENXIO;
 915        }
 916
 917        /* allocate chip specific data with zero-filled memory */
 918        if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
 919                pci_disable_device(pci);
 920                return -ENOMEM;
 921        }
 922
 923        chip->card = card;
 924        card->private_data = chip;
 925        chip->pci = pci;
 926        chip->irq = -1;
 927
 928        /* (1) PCI resource allocation */
 929        if ((err = pci_request_regions(pci, card->driver)) < 0)
 930                goto free_and_ret;
 931
 932        chip->bar = pci_resource_start(pci, 0);
 933        chip->iobase = ioremap_nocache(chip->bar, pci_resource_len(pci, 0));
 934        if (chip->iobase == NULL) {
 935                printk(KERN_ERR PFX "unable to reserve region.\n");
 936                err = -EBUSY;
 937                goto free_and_ret;
 938        }
 939        
 940        pci_set_master(pci);
 941
 942        spin_lock_init(&chip->lock);    /* only now can we call ad1889_free */
 943
 944        if (request_irq(pci->irq, snd_ad1889_interrupt,
 945                        IRQF_SHARED, card->driver, chip)) {
 946                printk(KERN_ERR PFX "cannot obtain IRQ %d\n", pci->irq);
 947                snd_ad1889_free(chip);
 948                return -EBUSY;
 949        }
 950
 951        chip->irq = pci->irq;
 952        synchronize_irq(chip->irq);
 953
 954        /* (2) initialization of the chip hardware */
 955        if ((err = snd_ad1889_init(chip)) < 0) {
 956                snd_ad1889_free(chip);
 957                return err;
 958        }
 959
 960        if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
 961                snd_ad1889_free(chip);
 962                return err;
 963        }
 964
 965        snd_card_set_dev(card, &pci->dev);
 966
 967        *rchip = chip;
 968
 969        return 0;
 970
 971free_and_ret:
 972        kfree(chip);
 973        pci_disable_device(pci);
 974
 975        return err;
 976}
 977
 978static int __devinit
 979snd_ad1889_probe(struct pci_dev *pci,
 980                 const struct pci_device_id *pci_id)
 981{
 982        int err;
 983        static int devno;
 984        struct snd_card *card;
 985        struct snd_ad1889 *chip;
 986
 987        /* (1) */
 988        if (devno >= SNDRV_CARDS)
 989                return -ENODEV;
 990        if (!enable[devno]) {
 991                devno++;
 992                return -ENOENT;
 993        }
 994
 995        /* (2) */
 996        card = snd_card_new(index[devno], id[devno], THIS_MODULE, 0);
 997        /* XXX REVISIT: we can probably allocate chip in this call */
 998        if (card == NULL)
 999                return -ENOMEM;
1000
1001        strcpy(card->driver, "AD1889");
1002        strcpy(card->shortname, "Analog Devices AD1889");
1003
1004        /* (3) */
1005        err = snd_ad1889_create(card, pci, &chip);
1006        if (err < 0)
1007                goto free_and_ret;
1008
1009        /* (4) */
1010        sprintf(card->longname, "%s at 0x%lx irq %i",
1011                card->shortname, chip->bar, chip->irq);
1012
1013        /* (5) */
1014        /* register AC97 mixer */
1015        err = snd_ad1889_ac97_init(chip, ac97_quirk[devno]);
1016        if (err < 0)
1017                goto free_and_ret;
1018        
1019        err = snd_ad1889_pcm_init(chip, 0, NULL);
1020        if (err < 0)
1021                goto free_and_ret;
1022
1023        /* register proc interface */
1024        snd_ad1889_proc_init(chip);
1025
1026        /* (6) */
1027        err = snd_card_register(card);
1028        if (err < 0)
1029                goto free_and_ret;
1030
1031        /* (7) */
1032        pci_set_drvdata(pci, card);
1033
1034        devno++;
1035        return 0;
1036
1037free_and_ret:
1038        snd_card_free(card);
1039        return err;
1040}
1041
1042static void __devexit
1043snd_ad1889_remove(struct pci_dev *pci)
1044{
1045        snd_card_free(pci_get_drvdata(pci));
1046        pci_set_drvdata(pci, NULL);
1047}
1048
1049static struct pci_device_id snd_ad1889_ids[] = {
1050        { PCI_DEVICE(PCI_VENDOR_ID_ANALOG_DEVICES, PCI_DEVICE_ID_AD1889JS) },
1051        { 0, },
1052};
1053MODULE_DEVICE_TABLE(pci, snd_ad1889_ids);
1054
1055static struct pci_driver ad1889_pci_driver = {
1056        .name = "AD1889 Audio",
1057        .id_table = snd_ad1889_ids,
1058        .probe = snd_ad1889_probe,
1059        .remove = __devexit_p(snd_ad1889_remove),
1060};
1061
1062static int __init
1063alsa_ad1889_init(void)
1064{
1065        return pci_register_driver(&ad1889_pci_driver);
1066}
1067
1068static void __exit
1069alsa_ad1889_fini(void)
1070{
1071        pci_unregister_driver(&ad1889_pci_driver);
1072}
1073
1074module_init(alsa_ad1889_init);
1075module_exit(alsa_ad1889_fini);
1076