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