Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for NeoMagic 256AV and 256ZX chipsets. |
| 3 | * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de> |
| 4 | * |
| 5 | * Based on nm256_audio.c OSS driver in linux kernel. |
| 6 | * The original author of OSS nm256 driver wishes to remain anonymous, |
| 7 | * so I just put my acknoledgment to him/her here. |
| 8 | * The original author's web page is found at |
| 9 | * http://www.uglx.org/sony.html |
| 10 | * |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <sound/driver.h> |
| 28 | #include <asm/io.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/pci.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/moduleparam.h> |
| 35 | #include <sound/core.h> |
| 36 | #include <sound/info.h> |
| 37 | #include <sound/control.h> |
| 38 | #include <sound/pcm.h> |
| 39 | #include <sound/ac97_codec.h> |
| 40 | #include <sound/initval.h> |
| 41 | |
| 42 | #define CARD_NAME "NeoMagic 256AV/ZX" |
| 43 | #define DRIVER_NAME "NM256" |
| 44 | |
| 45 | MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>"); |
| 46 | MODULE_DESCRIPTION("NeoMagic NM256AV/ZX"); |
| 47 | MODULE_LICENSE("GPL"); |
| 48 | MODULE_SUPPORTED_DEVICE("{{NeoMagic,NM256AV}," |
| 49 | "{NeoMagic,NM256ZX}}"); |
| 50 | |
| 51 | /* |
| 52 | * some compile conditions. |
| 53 | */ |
| 54 | |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 55 | static int index = SNDRV_DEFAULT_IDX1; /* Index */ |
| 56 | static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */ |
| 57 | static int playback_bufsize = 16; |
| 58 | static int capture_bufsize = 16; |
| 59 | static int force_ac97; /* disabled as default */ |
| 60 | static int buffer_top; /* not specified */ |
| 61 | static int use_cache; /* disabled */ |
| 62 | static int vaio_hack; /* disabled */ |
| 63 | static int reset_workaround; |
| 64 | static int reset_workaround_2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 66 | module_param(index, int, 0444); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard."); |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 68 | module_param(id, charp, 0444); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard."); |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 70 | module_param(playback_bufsize, int, 0444); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | MODULE_PARM_DESC(playback_bufsize, "DAC frame size in kB for " CARD_NAME " soundcard."); |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 72 | module_param(capture_bufsize, int, 0444); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | MODULE_PARM_DESC(capture_bufsize, "ADC frame size in kB for " CARD_NAME " soundcard."); |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 74 | module_param(force_ac97, bool, 0444); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | MODULE_PARM_DESC(force_ac97, "Force to use AC97 codec for " CARD_NAME " soundcard."); |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 76 | module_param(buffer_top, int, 0444); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | MODULE_PARM_DESC(buffer_top, "Set the top address of audio buffer for " CARD_NAME " soundcard."); |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 78 | module_param(use_cache, bool, 0444); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | MODULE_PARM_DESC(use_cache, "Enable the cache for coefficient table access."); |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 80 | module_param(vaio_hack, bool, 0444); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | MODULE_PARM_DESC(vaio_hack, "Enable workaround for Sony VAIO notebooks."); |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 82 | module_param(reset_workaround, bool, 0444); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | MODULE_PARM_DESC(reset_workaround, "Enable AC97 RESET workaround for some laptops."); |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 84 | module_param(reset_workaround_2, bool, 0444); |
John W. Linville | 47530cf | 2005-10-19 16:03:10 +0200 | [diff] [blame] | 85 | MODULE_PARM_DESC(reset_workaround_2, "Enable extended AC97 RESET workaround for some other laptops."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 87 | /* just for backward compatibility */ |
| 88 | static int enable; |
| 89 | module_param(enable, bool, 0444); |
| 90 | |
| 91 | |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | /* |
| 94 | * hw definitions |
| 95 | */ |
| 96 | |
| 97 | /* The BIOS signature. */ |
| 98 | #define NM_SIGNATURE 0x4e4d0000 |
| 99 | /* Signature mask. */ |
| 100 | #define NM_SIG_MASK 0xffff0000 |
| 101 | |
| 102 | /* Size of the second memory area. */ |
| 103 | #define NM_PORT2_SIZE 4096 |
| 104 | |
| 105 | /* The base offset of the mixer in the second memory area. */ |
| 106 | #define NM_MIXER_OFFSET 0x600 |
| 107 | |
| 108 | /* The maximum size of a coefficient entry. */ |
| 109 | #define NM_MAX_PLAYBACK_COEF_SIZE 0x5000 |
| 110 | #define NM_MAX_RECORD_COEF_SIZE 0x1260 |
| 111 | |
| 112 | /* The interrupt register. */ |
| 113 | #define NM_INT_REG 0xa04 |
| 114 | /* And its bits. */ |
| 115 | #define NM_PLAYBACK_INT 0x40 |
| 116 | #define NM_RECORD_INT 0x100 |
| 117 | #define NM_MISC_INT_1 0x4000 |
| 118 | #define NM_MISC_INT_2 0x1 |
| 119 | #define NM_ACK_INT(chip, X) snd_nm256_writew(chip, NM_INT_REG, (X) << 1) |
| 120 | |
| 121 | /* The AV's "mixer ready" status bit and location. */ |
| 122 | #define NM_MIXER_STATUS_OFFSET 0xa04 |
| 123 | #define NM_MIXER_READY_MASK 0x0800 |
| 124 | #define NM_MIXER_PRESENCE 0xa06 |
| 125 | #define NM_PRESENCE_MASK 0x0050 |
| 126 | #define NM_PRESENCE_VALUE 0x0040 |
| 127 | |
| 128 | /* |
| 129 | * For the ZX. It uses the same interrupt register, but it holds 32 |
| 130 | * bits instead of 16. |
| 131 | */ |
| 132 | #define NM2_PLAYBACK_INT 0x10000 |
| 133 | #define NM2_RECORD_INT 0x80000 |
| 134 | #define NM2_MISC_INT_1 0x8 |
| 135 | #define NM2_MISC_INT_2 0x2 |
| 136 | #define NM2_ACK_INT(chip, X) snd_nm256_writel(chip, NM_INT_REG, (X)) |
| 137 | |
| 138 | /* The ZX's "mixer ready" status bit and location. */ |
| 139 | #define NM2_MIXER_STATUS_OFFSET 0xa06 |
| 140 | #define NM2_MIXER_READY_MASK 0x0800 |
| 141 | |
| 142 | /* The playback registers start from here. */ |
| 143 | #define NM_PLAYBACK_REG_OFFSET 0x0 |
| 144 | /* The record registers start from here. */ |
| 145 | #define NM_RECORD_REG_OFFSET 0x200 |
| 146 | |
| 147 | /* The rate register is located 2 bytes from the start of the register area. */ |
| 148 | #define NM_RATE_REG_OFFSET 2 |
| 149 | |
| 150 | /* Mono/stereo flag, number of bits on playback, and rate mask. */ |
| 151 | #define NM_RATE_STEREO 1 |
| 152 | #define NM_RATE_BITS_16 2 |
| 153 | #define NM_RATE_MASK 0xf0 |
| 154 | |
| 155 | /* Playback enable register. */ |
| 156 | #define NM_PLAYBACK_ENABLE_REG (NM_PLAYBACK_REG_OFFSET + 0x1) |
| 157 | #define NM_PLAYBACK_ENABLE_FLAG 1 |
| 158 | #define NM_PLAYBACK_ONESHOT 2 |
| 159 | #define NM_PLAYBACK_FREERUN 4 |
| 160 | |
| 161 | /* Mutes the audio output. */ |
| 162 | #define NM_AUDIO_MUTE_REG (NM_PLAYBACK_REG_OFFSET + 0x18) |
| 163 | #define NM_AUDIO_MUTE_LEFT 0x8000 |
| 164 | #define NM_AUDIO_MUTE_RIGHT 0x0080 |
| 165 | |
| 166 | /* Recording enable register. */ |
| 167 | #define NM_RECORD_ENABLE_REG (NM_RECORD_REG_OFFSET + 0) |
| 168 | #define NM_RECORD_ENABLE_FLAG 1 |
| 169 | #define NM_RECORD_FREERUN 2 |
| 170 | |
| 171 | /* coefficient buffer pointer */ |
| 172 | #define NM_COEFF_START_OFFSET 0x1c |
| 173 | #define NM_COEFF_END_OFFSET 0x20 |
| 174 | |
| 175 | /* DMA buffer offsets */ |
| 176 | #define NM_RBUFFER_START (NM_RECORD_REG_OFFSET + 0x4) |
| 177 | #define NM_RBUFFER_END (NM_RECORD_REG_OFFSET + 0x10) |
| 178 | #define NM_RBUFFER_WMARK (NM_RECORD_REG_OFFSET + 0xc) |
| 179 | #define NM_RBUFFER_CURRP (NM_RECORD_REG_OFFSET + 0x8) |
| 180 | |
| 181 | #define NM_PBUFFER_START (NM_PLAYBACK_REG_OFFSET + 0x4) |
| 182 | #define NM_PBUFFER_END (NM_PLAYBACK_REG_OFFSET + 0x14) |
| 183 | #define NM_PBUFFER_WMARK (NM_PLAYBACK_REG_OFFSET + 0xc) |
| 184 | #define NM_PBUFFER_CURRP (NM_PLAYBACK_REG_OFFSET + 0x8) |
| 185 | |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 186 | struct nm256_stream { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 188 | struct nm256 *chip; |
| 189 | struct snd_pcm_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | int running; |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 191 | int suspended; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
| 193 | u32 buf; /* offset from chip->buffer */ |
| 194 | int bufsize; /* buffer size in bytes */ |
| 195 | void __iomem *bufptr; /* mapped pointer */ |
| 196 | unsigned long bufptr_addr; /* physical address of the mapped pointer */ |
| 197 | |
| 198 | int dma_size; /* buffer size of the substream in bytes */ |
| 199 | int period_size; /* period size in bytes */ |
| 200 | int periods; /* # of periods */ |
| 201 | int shift; /* bit shifts */ |
| 202 | int cur_period; /* current period # */ |
| 203 | |
| 204 | }; |
| 205 | |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 206 | struct nm256 { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 208 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | void __iomem *cport; /* control port */ |
| 211 | struct resource *res_cport; /* its resource */ |
| 212 | unsigned long cport_addr; /* physical address */ |
| 213 | |
| 214 | void __iomem *buffer; /* buffer */ |
| 215 | struct resource *res_buffer; /* its resource */ |
| 216 | unsigned long buffer_addr; /* buffer phyiscal address */ |
| 217 | |
| 218 | u32 buffer_start; /* start offset from pci resource 0 */ |
| 219 | u32 buffer_end; /* end offset */ |
| 220 | u32 buffer_size; /* total buffer size */ |
| 221 | |
| 222 | u32 all_coeff_buf; /* coefficient buffer */ |
| 223 | u32 coeff_buf[2]; /* coefficient buffer for each stream */ |
| 224 | |
| 225 | unsigned int coeffs_current: 1; /* coeff. table is loaded? */ |
| 226 | unsigned int use_cache: 1; /* use one big coef. table */ |
| 227 | unsigned int reset_workaround: 1; /* Workaround for some laptops to avoid freeze */ |
John W. Linville | 47530cf | 2005-10-19 16:03:10 +0200 | [diff] [blame] | 228 | unsigned int reset_workaround_2: 1; /* Extended workaround for some other laptops to avoid freeze */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | int mixer_base; /* register offset of ac97 mixer */ |
| 231 | int mixer_status_offset; /* offset of mixer status reg. */ |
| 232 | int mixer_status_mask; /* bit mask to test the mixer status */ |
| 233 | |
| 234 | int irq; |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 235 | int irq_acks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | irqreturn_t (*interrupt)(int, void *, struct pt_regs *); |
| 237 | int badintrcount; /* counter to check bogus interrupts */ |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 238 | struct semaphore irq_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 240 | struct nm256_stream streams[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 242 | struct snd_ac97 *ac97; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 244 | struct snd_pcm *pcm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| 246 | struct pci_dev *pci; |
| 247 | |
| 248 | spinlock_t reg_lock; |
| 249 | |
| 250 | }; |
| 251 | |
| 252 | |
| 253 | /* |
| 254 | * include coefficient table |
| 255 | */ |
| 256 | #include "nm256_coef.c" |
| 257 | |
| 258 | |
| 259 | /* |
| 260 | * PCI ids |
| 261 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | static struct pci_device_id snd_nm256_ids[] = { |
| 263 | {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, |
| 264 | {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, |
| 265 | {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, |
| 266 | {0,}, |
| 267 | }; |
| 268 | |
| 269 | MODULE_DEVICE_TABLE(pci, snd_nm256_ids); |
| 270 | |
| 271 | |
| 272 | /* |
| 273 | * lowlvel stuffs |
| 274 | */ |
| 275 | |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 276 | static inline u8 |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 277 | snd_nm256_readb(struct nm256 *chip, int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { |
| 279 | return readb(chip->cport + offset); |
| 280 | } |
| 281 | |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 282 | static inline u16 |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 283 | snd_nm256_readw(struct nm256 *chip, int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | { |
| 285 | return readw(chip->cport + offset); |
| 286 | } |
| 287 | |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 288 | static inline u32 |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 289 | snd_nm256_readl(struct nm256 *chip, int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
| 291 | return readl(chip->cport + offset); |
| 292 | } |
| 293 | |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 294 | static inline void |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 295 | snd_nm256_writeb(struct nm256 *chip, int offset, u8 val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { |
| 297 | writeb(val, chip->cport + offset); |
| 298 | } |
| 299 | |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 300 | static inline void |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 301 | snd_nm256_writew(struct nm256 *chip, int offset, u16 val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
| 303 | writew(val, chip->cport + offset); |
| 304 | } |
| 305 | |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 306 | static inline void |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 307 | snd_nm256_writel(struct nm256 *chip, int offset, u32 val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
| 309 | writel(val, chip->cport + offset); |
| 310 | } |
| 311 | |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 312 | static inline void |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 313 | snd_nm256_write_buffer(struct nm256 *chip, void *src, int offset, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
| 315 | offset -= chip->buffer_start; |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 316 | #ifdef CONFIG_SND_DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | if (offset < 0 || offset >= chip->buffer_size) { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 318 | snd_printk(KERN_ERR "write_buffer invalid offset = %d size = %d\n", |
| 319 | offset, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | return; |
| 321 | } |
| 322 | #endif |
| 323 | memcpy_toio(chip->buffer + offset, src, size); |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * coefficient handlers -- what a magic! |
| 328 | */ |
| 329 | |
| 330 | static u16 |
| 331 | snd_nm256_get_start_offset(int which) |
| 332 | { |
| 333 | u16 offset = 0; |
| 334 | while (which-- > 0) |
| 335 | offset += coefficient_sizes[which]; |
| 336 | return offset; |
| 337 | } |
| 338 | |
| 339 | static void |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 340 | snd_nm256_load_one_coefficient(struct nm256 *chip, int stream, u32 port, int which) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
| 342 | u32 coeff_buf = chip->coeff_buf[stream]; |
| 343 | u16 offset = snd_nm256_get_start_offset(which); |
| 344 | u16 size = coefficient_sizes[which]; |
| 345 | |
| 346 | snd_nm256_write_buffer(chip, coefficients + offset, coeff_buf, size); |
| 347 | snd_nm256_writel(chip, port, coeff_buf); |
| 348 | /* ??? Record seems to behave differently than playback. */ |
| 349 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 350 | size--; |
| 351 | snd_nm256_writel(chip, port + 4, coeff_buf + size); |
| 352 | } |
| 353 | |
| 354 | static void |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 355 | snd_nm256_load_coefficient(struct nm256 *chip, int stream, int number) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
| 357 | /* The enable register for the specified engine. */ |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 358 | u32 poffset = (stream == SNDRV_PCM_STREAM_CAPTURE ? |
| 359 | NM_RECORD_ENABLE_REG : NM_PLAYBACK_ENABLE_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | u32 addr = NM_COEFF_START_OFFSET; |
| 361 | |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 362 | addr += (stream == SNDRV_PCM_STREAM_CAPTURE ? |
| 363 | NM_RECORD_REG_OFFSET : NM_PLAYBACK_REG_OFFSET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
| 365 | if (snd_nm256_readb(chip, poffset) & 1) { |
| 366 | snd_printd("NM256: Engine was enabled while loading coefficients!\n"); |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | /* The recording engine uses coefficient values 8-15. */ |
| 371 | number &= 7; |
| 372 | if (stream == SNDRV_PCM_STREAM_CAPTURE) |
| 373 | number += 8; |
| 374 | |
| 375 | if (! chip->use_cache) { |
| 376 | snd_nm256_load_one_coefficient(chip, stream, addr, number); |
| 377 | return; |
| 378 | } |
| 379 | if (! chip->coeffs_current) { |
| 380 | snd_nm256_write_buffer(chip, coefficients, chip->all_coeff_buf, |
| 381 | NM_TOTAL_COEFF_COUNT * 4); |
| 382 | chip->coeffs_current = 1; |
| 383 | } else { |
| 384 | u32 base = chip->all_coeff_buf; |
| 385 | u32 offset = snd_nm256_get_start_offset(number); |
| 386 | u32 end_offset = offset + coefficient_sizes[number]; |
| 387 | snd_nm256_writel(chip, addr, base + offset); |
| 388 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 389 | end_offset--; |
| 390 | snd_nm256_writel(chip, addr + 4, base + end_offset); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | |
| 395 | /* The actual rates supported by the card. */ |
| 396 | static unsigned int samplerates[8] = { |
| 397 | 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, |
| 398 | }; |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 399 | static struct snd_pcm_hw_constraint_list constraints_rates = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | .count = ARRAY_SIZE(samplerates), |
| 401 | .list = samplerates, |
| 402 | .mask = 0, |
| 403 | }; |
| 404 | |
| 405 | /* |
| 406 | * return the index of the target rate |
| 407 | */ |
| 408 | static int |
| 409 | snd_nm256_fixed_rate(unsigned int rate) |
| 410 | { |
| 411 | unsigned int i; |
| 412 | for (i = 0; i < ARRAY_SIZE(samplerates); i++) { |
| 413 | if (rate == samplerates[i]) |
| 414 | return i; |
| 415 | } |
| 416 | snd_BUG(); |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * set sample rate and format |
| 422 | */ |
| 423 | static void |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 424 | snd_nm256_set_format(struct nm256 *chip, struct nm256_stream *s, |
| 425 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 427 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | int rate_index = snd_nm256_fixed_rate(runtime->rate); |
| 429 | unsigned char ratebits = (rate_index << 4) & NM_RATE_MASK; |
| 430 | |
| 431 | s->shift = 0; |
| 432 | if (snd_pcm_format_width(runtime->format) == 16) { |
| 433 | ratebits |= NM_RATE_BITS_16; |
| 434 | s->shift++; |
| 435 | } |
| 436 | if (runtime->channels > 1) { |
| 437 | ratebits |= NM_RATE_STEREO; |
| 438 | s->shift++; |
| 439 | } |
| 440 | |
| 441 | runtime->rate = samplerates[rate_index]; |
| 442 | |
| 443 | switch (substream->stream) { |
| 444 | case SNDRV_PCM_STREAM_PLAYBACK: |
| 445 | snd_nm256_load_coefficient(chip, 0, rate_index); /* 0 = playback */ |
| 446 | snd_nm256_writeb(chip, |
| 447 | NM_PLAYBACK_REG_OFFSET + NM_RATE_REG_OFFSET, |
| 448 | ratebits); |
| 449 | break; |
| 450 | case SNDRV_PCM_STREAM_CAPTURE: |
| 451 | snd_nm256_load_coefficient(chip, 1, rate_index); /* 1 = record */ |
| 452 | snd_nm256_writeb(chip, |
| 453 | NM_RECORD_REG_OFFSET + NM_RATE_REG_OFFSET, |
| 454 | ratebits); |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 459 | /* acquire interrupt */ |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 460 | static int snd_nm256_acquire_irq(struct nm256 *chip) |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 461 | { |
| 462 | down(&chip->irq_mutex); |
| 463 | if (chip->irq < 0) { |
| 464 | if (request_irq(chip->pci->irq, chip->interrupt, SA_INTERRUPT|SA_SHIRQ, |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 465 | chip->card->driver, chip)) { |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 466 | snd_printk(KERN_ERR "unable to grab IRQ %d\n", chip->pci->irq); |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 467 | up(&chip->irq_mutex); |
| 468 | return -EBUSY; |
| 469 | } |
| 470 | chip->irq = chip->pci->irq; |
| 471 | } |
| 472 | chip->irq_acks++; |
| 473 | up(&chip->irq_mutex); |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | /* release interrupt */ |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 478 | static void snd_nm256_release_irq(struct nm256 *chip) |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 479 | { |
| 480 | down(&chip->irq_mutex); |
| 481 | if (chip->irq_acks > 0) |
| 482 | chip->irq_acks--; |
| 483 | if (chip->irq_acks == 0 && chip->irq >= 0) { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 484 | free_irq(chip->irq, chip); |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 485 | chip->irq = -1; |
| 486 | } |
| 487 | up(&chip->irq_mutex); |
| 488 | } |
| 489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | /* |
| 491 | * start / stop |
| 492 | */ |
| 493 | |
| 494 | /* update the watermark (current period) */ |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 495 | static void snd_nm256_pcm_mark(struct nm256 *chip, struct nm256_stream *s, int reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | { |
| 497 | s->cur_period++; |
| 498 | s->cur_period %= s->periods; |
| 499 | snd_nm256_writel(chip, reg, s->buf + s->cur_period * s->period_size); |
| 500 | } |
| 501 | |
| 502 | #define snd_nm256_playback_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_PBUFFER_WMARK) |
| 503 | #define snd_nm256_capture_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_RBUFFER_WMARK) |
| 504 | |
| 505 | static void |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 506 | snd_nm256_playback_start(struct nm256 *chip, struct nm256_stream *s, |
| 507 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | { |
| 509 | /* program buffer pointers */ |
| 510 | snd_nm256_writel(chip, NM_PBUFFER_START, s->buf); |
| 511 | snd_nm256_writel(chip, NM_PBUFFER_END, s->buf + s->dma_size - (1 << s->shift)); |
| 512 | snd_nm256_writel(chip, NM_PBUFFER_CURRP, s->buf); |
| 513 | snd_nm256_playback_mark(chip, s); |
| 514 | |
| 515 | /* Enable playback engine and interrupts. */ |
| 516 | snd_nm256_writeb(chip, NM_PLAYBACK_ENABLE_REG, |
| 517 | NM_PLAYBACK_ENABLE_FLAG | NM_PLAYBACK_FREERUN); |
| 518 | /* Enable both channels. */ |
| 519 | snd_nm256_writew(chip, NM_AUDIO_MUTE_REG, 0x0); |
| 520 | } |
| 521 | |
| 522 | static void |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 523 | snd_nm256_capture_start(struct nm256 *chip, struct nm256_stream *s, |
| 524 | struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | { |
| 526 | /* program buffer pointers */ |
| 527 | snd_nm256_writel(chip, NM_RBUFFER_START, s->buf); |
| 528 | snd_nm256_writel(chip, NM_RBUFFER_END, s->buf + s->dma_size); |
| 529 | snd_nm256_writel(chip, NM_RBUFFER_CURRP, s->buf); |
| 530 | snd_nm256_capture_mark(chip, s); |
| 531 | |
| 532 | /* Enable playback engine and interrupts. */ |
| 533 | snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG, |
| 534 | NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN); |
| 535 | } |
| 536 | |
| 537 | /* Stop the play engine. */ |
| 538 | static void |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 539 | snd_nm256_playback_stop(struct nm256 *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | { |
| 541 | /* Shut off sound from both channels. */ |
| 542 | snd_nm256_writew(chip, NM_AUDIO_MUTE_REG, |
| 543 | NM_AUDIO_MUTE_LEFT | NM_AUDIO_MUTE_RIGHT); |
| 544 | /* Disable play engine. */ |
| 545 | snd_nm256_writeb(chip, NM_PLAYBACK_ENABLE_REG, 0); |
| 546 | } |
| 547 | |
| 548 | static void |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 549 | snd_nm256_capture_stop(struct nm256 *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | { |
| 551 | /* Disable recording engine. */ |
| 552 | snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG, 0); |
| 553 | } |
| 554 | |
| 555 | static int |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 556 | snd_nm256_playback_trigger(struct snd_pcm_substream *substream, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 558 | struct nm256 *chip = snd_pcm_substream_chip(substream); |
| 559 | struct nm256_stream *s = substream->runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | int err = 0; |
| 561 | |
| 562 | snd_assert(s != NULL, return -ENXIO); |
| 563 | |
| 564 | spin_lock(&chip->reg_lock); |
| 565 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | case SNDRV_PCM_TRIGGER_RESUME: |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 567 | s->suspended = 0; |
| 568 | /* fallthru */ |
| 569 | case SNDRV_PCM_TRIGGER_START: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | if (! s->running) { |
| 571 | snd_nm256_playback_start(chip, s, substream); |
| 572 | s->running = 1; |
| 573 | } |
| 574 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | case SNDRV_PCM_TRIGGER_SUSPEND: |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 576 | s->suspended = 1; |
| 577 | /* fallthru */ |
| 578 | case SNDRV_PCM_TRIGGER_STOP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | if (s->running) { |
| 580 | snd_nm256_playback_stop(chip); |
| 581 | s->running = 0; |
| 582 | } |
| 583 | break; |
| 584 | default: |
| 585 | err = -EINVAL; |
| 586 | break; |
| 587 | } |
| 588 | spin_unlock(&chip->reg_lock); |
| 589 | return err; |
| 590 | } |
| 591 | |
| 592 | static int |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 593 | snd_nm256_capture_trigger(struct snd_pcm_substream *substream, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 595 | struct nm256 *chip = snd_pcm_substream_chip(substream); |
| 596 | struct nm256_stream *s = substream->runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | int err = 0; |
| 598 | |
| 599 | snd_assert(s != NULL, return -ENXIO); |
| 600 | |
| 601 | spin_lock(&chip->reg_lock); |
| 602 | switch (cmd) { |
| 603 | case SNDRV_PCM_TRIGGER_START: |
| 604 | case SNDRV_PCM_TRIGGER_RESUME: |
| 605 | if (! s->running) { |
| 606 | snd_nm256_capture_start(chip, s, substream); |
| 607 | s->running = 1; |
| 608 | } |
| 609 | break; |
| 610 | case SNDRV_PCM_TRIGGER_STOP: |
| 611 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 612 | if (s->running) { |
| 613 | snd_nm256_capture_stop(chip); |
| 614 | s->running = 0; |
| 615 | } |
| 616 | break; |
| 617 | default: |
| 618 | err = -EINVAL; |
| 619 | break; |
| 620 | } |
| 621 | spin_unlock(&chip->reg_lock); |
| 622 | return err; |
| 623 | } |
| 624 | |
| 625 | |
| 626 | /* |
| 627 | * prepare playback/capture channel |
| 628 | */ |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 629 | static int snd_nm256_pcm_prepare(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 631 | struct nm256 *chip = snd_pcm_substream_chip(substream); |
| 632 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 633 | struct nm256_stream *s = runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
| 635 | snd_assert(s, return -ENXIO); |
| 636 | s->dma_size = frames_to_bytes(runtime, substream->runtime->buffer_size); |
| 637 | s->period_size = frames_to_bytes(runtime, substream->runtime->period_size); |
| 638 | s->periods = substream->runtime->periods; |
| 639 | s->cur_period = 0; |
| 640 | |
| 641 | spin_lock_irq(&chip->reg_lock); |
| 642 | s->running = 0; |
| 643 | snd_nm256_set_format(chip, s, substream); |
| 644 | spin_unlock_irq(&chip->reg_lock); |
| 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | |
| 650 | /* |
| 651 | * get the current pointer |
| 652 | */ |
| 653 | static snd_pcm_uframes_t |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 654 | snd_nm256_playback_pointer(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 656 | struct nm256 *chip = snd_pcm_substream_chip(substream); |
| 657 | struct nm256_stream *s = substream->runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | unsigned long curp; |
| 659 | |
| 660 | snd_assert(s, return 0); |
| 661 | curp = snd_nm256_readl(chip, NM_PBUFFER_CURRP) - (unsigned long)s->buf; |
| 662 | curp %= s->dma_size; |
| 663 | return bytes_to_frames(substream->runtime, curp); |
| 664 | } |
| 665 | |
| 666 | static snd_pcm_uframes_t |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 667 | snd_nm256_capture_pointer(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 669 | struct nm256 *chip = snd_pcm_substream_chip(substream); |
| 670 | struct nm256_stream *s = substream->runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | unsigned long curp; |
| 672 | |
| 673 | snd_assert(s != NULL, return 0); |
| 674 | curp = snd_nm256_readl(chip, NM_RBUFFER_CURRP) - (unsigned long)s->buf; |
| 675 | curp %= s->dma_size; |
| 676 | return bytes_to_frames(substream->runtime, curp); |
| 677 | } |
| 678 | |
| 679 | /* Remapped I/O space can be accessible as pointer on i386 */ |
| 680 | /* This might be changed in the future */ |
| 681 | #ifndef __i386__ |
| 682 | /* |
| 683 | * silence / copy for playback |
| 684 | */ |
| 685 | static int |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 686 | snd_nm256_playback_silence(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | int channel, /* not used (interleaved data) */ |
| 688 | snd_pcm_uframes_t pos, |
| 689 | snd_pcm_uframes_t count) |
| 690 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 691 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 692 | struct nm256_stream *s = runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | count = frames_to_bytes(runtime, count); |
| 694 | pos = frames_to_bytes(runtime, pos); |
| 695 | memset_io(s->bufptr + pos, 0, count); |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | static int |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 700 | snd_nm256_playback_copy(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | int channel, /* not used (interleaved data) */ |
| 702 | snd_pcm_uframes_t pos, |
| 703 | void __user *src, |
| 704 | snd_pcm_uframes_t count) |
| 705 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 706 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 707 | struct nm256_stream *s = runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | count = frames_to_bytes(runtime, count); |
| 709 | pos = frames_to_bytes(runtime, pos); |
| 710 | if (copy_from_user_toio(s->bufptr + pos, src, count)) |
| 711 | return -EFAULT; |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | * copy to user |
| 717 | */ |
| 718 | static int |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 719 | snd_nm256_capture_copy(struct snd_pcm_substream *substream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | int channel, /* not used (interleaved data) */ |
| 721 | snd_pcm_uframes_t pos, |
| 722 | void __user *dst, |
| 723 | snd_pcm_uframes_t count) |
| 724 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 725 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 726 | struct nm256_stream *s = runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | count = frames_to_bytes(runtime, count); |
| 728 | pos = frames_to_bytes(runtime, pos); |
| 729 | if (copy_to_user_fromio(dst, s->bufptr + pos, count)) |
| 730 | return -EFAULT; |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | #endif /* !__i386__ */ |
| 735 | |
| 736 | |
| 737 | /* |
| 738 | * update playback/capture watermarks |
| 739 | */ |
| 740 | |
| 741 | /* spinlock held! */ |
| 742 | static void |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 743 | snd_nm256_playback_update(struct nm256 *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 745 | struct nm256_stream *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
| 747 | s = &chip->streams[SNDRV_PCM_STREAM_PLAYBACK]; |
| 748 | if (s->running && s->substream) { |
| 749 | spin_unlock(&chip->reg_lock); |
| 750 | snd_pcm_period_elapsed(s->substream); |
| 751 | spin_lock(&chip->reg_lock); |
| 752 | snd_nm256_playback_mark(chip, s); |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | /* spinlock held! */ |
| 757 | static void |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 758 | snd_nm256_capture_update(struct nm256 *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 760 | struct nm256_stream *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | |
| 762 | s = &chip->streams[SNDRV_PCM_STREAM_CAPTURE]; |
| 763 | if (s->running && s->substream) { |
| 764 | spin_unlock(&chip->reg_lock); |
| 765 | snd_pcm_period_elapsed(s->substream); |
| 766 | spin_lock(&chip->reg_lock); |
| 767 | snd_nm256_capture_mark(chip, s); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | /* |
| 772 | * hardware info |
| 773 | */ |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 774 | static struct snd_pcm_hardware snd_nm256_playback = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | { |
| 776 | .info = SNDRV_PCM_INFO_MMAP_IOMEM |SNDRV_PCM_INFO_MMAP_VALID | |
| 777 | SNDRV_PCM_INFO_INTERLEAVED | |
| 778 | /*SNDRV_PCM_INFO_PAUSE |*/ |
| 779 | SNDRV_PCM_INFO_RESUME, |
| 780 | .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE, |
| 781 | .rates = SNDRV_PCM_RATE_KNOT/*24k*/ | SNDRV_PCM_RATE_8000_48000, |
| 782 | .rate_min = 8000, |
| 783 | .rate_max = 48000, |
| 784 | .channels_min = 1, |
| 785 | .channels_max = 2, |
| 786 | .periods_min = 2, |
| 787 | .periods_max = 1024, |
| 788 | .buffer_bytes_max = 128 * 1024, |
| 789 | .period_bytes_min = 256, |
| 790 | .period_bytes_max = 128 * 1024, |
| 791 | }; |
| 792 | |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 793 | static struct snd_pcm_hardware snd_nm256_capture = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | { |
| 795 | .info = SNDRV_PCM_INFO_MMAP_IOMEM | SNDRV_PCM_INFO_MMAP_VALID | |
| 796 | SNDRV_PCM_INFO_INTERLEAVED | |
| 797 | /*SNDRV_PCM_INFO_PAUSE |*/ |
| 798 | SNDRV_PCM_INFO_RESUME, |
| 799 | .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE, |
| 800 | .rates = SNDRV_PCM_RATE_KNOT/*24k*/ | SNDRV_PCM_RATE_8000_48000, |
| 801 | .rate_min = 8000, |
| 802 | .rate_max = 48000, |
| 803 | .channels_min = 1, |
| 804 | .channels_max = 2, |
| 805 | .periods_min = 2, |
| 806 | .periods_max = 1024, |
| 807 | .buffer_bytes_max = 128 * 1024, |
| 808 | .period_bytes_min = 256, |
| 809 | .period_bytes_max = 128 * 1024, |
| 810 | }; |
| 811 | |
| 812 | |
| 813 | /* set dma transfer size */ |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 814 | static int snd_nm256_pcm_hw_params(struct snd_pcm_substream *substream, |
| 815 | struct snd_pcm_hw_params *hw_params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | { |
| 817 | /* area and addr are already set and unchanged */ |
| 818 | substream->runtime->dma_bytes = params_buffer_bytes(hw_params); |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | /* |
| 823 | * open |
| 824 | */ |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 825 | static void snd_nm256_setup_stream(struct nm256 *chip, struct nm256_stream *s, |
| 826 | struct snd_pcm_substream *substream, |
| 827 | struct snd_pcm_hardware *hw_ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 829 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
| 831 | s->running = 0; |
| 832 | runtime->hw = *hw_ptr; |
| 833 | runtime->hw.buffer_bytes_max = s->bufsize; |
| 834 | runtime->hw.period_bytes_max = s->bufsize / 2; |
Clemens Ladisch | 4d23359 | 2005-09-05 10:35:20 +0200 | [diff] [blame] | 835 | runtime->dma_area = (void __force *) s->bufptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | runtime->dma_addr = s->bufptr_addr; |
| 837 | runtime->dma_bytes = s->bufsize; |
| 838 | runtime->private_data = s; |
| 839 | s->substream = substream; |
| 840 | |
| 841 | snd_pcm_set_sync(substream); |
| 842 | snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
| 843 | &constraints_rates); |
| 844 | } |
| 845 | |
| 846 | static int |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 847 | snd_nm256_playback_open(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 849 | struct nm256 *chip = snd_pcm_substream_chip(substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 851 | if (snd_nm256_acquire_irq(chip) < 0) |
| 852 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK], |
| 854 | substream, &snd_nm256_playback); |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | static int |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 859 | snd_nm256_capture_open(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 861 | struct nm256 *chip = snd_pcm_substream_chip(substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 863 | if (snd_nm256_acquire_irq(chip) < 0) |
| 864 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_CAPTURE], |
| 866 | substream, &snd_nm256_capture); |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | /* |
| 871 | * close - we don't have to do special.. |
| 872 | */ |
| 873 | static int |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 874 | snd_nm256_playback_close(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 876 | struct nm256 *chip = snd_pcm_substream_chip(substream); |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 877 | |
| 878 | snd_nm256_release_irq(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | |
| 883 | static int |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 884 | snd_nm256_capture_close(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 886 | struct nm256 *chip = snd_pcm_substream_chip(substream); |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 887 | |
| 888 | snd_nm256_release_irq(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | /* |
| 893 | * create a pcm instance |
| 894 | */ |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 895 | static struct snd_pcm_ops snd_nm256_playback_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | .open = snd_nm256_playback_open, |
| 897 | .close = snd_nm256_playback_close, |
| 898 | .ioctl = snd_pcm_lib_ioctl, |
| 899 | .hw_params = snd_nm256_pcm_hw_params, |
| 900 | .prepare = snd_nm256_pcm_prepare, |
| 901 | .trigger = snd_nm256_playback_trigger, |
| 902 | .pointer = snd_nm256_playback_pointer, |
| 903 | #ifndef __i386__ |
| 904 | .copy = snd_nm256_playback_copy, |
| 905 | .silence = snd_nm256_playback_silence, |
| 906 | #endif |
| 907 | .mmap = snd_pcm_lib_mmap_iomem, |
| 908 | }; |
| 909 | |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 910 | static struct snd_pcm_ops snd_nm256_capture_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | .open = snd_nm256_capture_open, |
| 912 | .close = snd_nm256_capture_close, |
| 913 | .ioctl = snd_pcm_lib_ioctl, |
| 914 | .hw_params = snd_nm256_pcm_hw_params, |
| 915 | .prepare = snd_nm256_pcm_prepare, |
| 916 | .trigger = snd_nm256_capture_trigger, |
| 917 | .pointer = snd_nm256_capture_pointer, |
| 918 | #ifndef __i386__ |
| 919 | .copy = snd_nm256_capture_copy, |
| 920 | #endif |
| 921 | .mmap = snd_pcm_lib_mmap_iomem, |
| 922 | }; |
| 923 | |
| 924 | static int __devinit |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 925 | snd_nm256_pcm(struct nm256 *chip, int device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 927 | struct snd_pcm *pcm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | int i, err; |
| 929 | |
| 930 | for (i = 0; i < 2; i++) { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 931 | struct nm256_stream *s = &chip->streams[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | s->bufptr = chip->buffer + (s->buf - chip->buffer_start); |
| 933 | s->bufptr_addr = chip->buffer_addr + (s->buf - chip->buffer_start); |
| 934 | } |
| 935 | |
| 936 | err = snd_pcm_new(chip->card, chip->card->driver, device, |
| 937 | 1, 1, &pcm); |
| 938 | if (err < 0) |
| 939 | return err; |
| 940 | |
| 941 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_nm256_playback_ops); |
| 942 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_nm256_capture_ops); |
| 943 | |
| 944 | pcm->private_data = chip; |
| 945 | pcm->info_flags = 0; |
| 946 | chip->pcm = pcm; |
| 947 | |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | |
| 952 | /* |
| 953 | * Initialize the hardware. |
| 954 | */ |
| 955 | static void |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 956 | snd_nm256_init_chip(struct nm256 *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | /* Reset everything. */ |
| 959 | snd_nm256_writeb(chip, 0x0, 0x11); |
| 960 | snd_nm256_writew(chip, 0x214, 0); |
| 961 | /* stop sounds.. */ |
| 962 | //snd_nm256_playback_stop(chip); |
| 963 | //snd_nm256_capture_stop(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 967 | static irqreturn_t |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 968 | snd_nm256_intr_check(struct nm256 *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | { |
| 970 | if (chip->badintrcount++ > 1000) { |
| 971 | /* |
| 972 | * I'm not sure if the best thing is to stop the card from |
| 973 | * playing or just release the interrupt (after all, we're in |
| 974 | * a bad situation, so doing fancy stuff may not be such a good |
| 975 | * idea). |
| 976 | * |
| 977 | * I worry about the card engine continuing to play noise |
| 978 | * over and over, however--that could become a very |
| 979 | * obnoxious problem. And we know that when this usually |
| 980 | * happens things are fairly safe, it just means the user's |
| 981 | * inserted a PCMCIA card and someone's spamming us with IRQ 9s. |
| 982 | */ |
| 983 | if (chip->streams[SNDRV_PCM_STREAM_PLAYBACK].running) |
| 984 | snd_nm256_playback_stop(chip); |
| 985 | if (chip->streams[SNDRV_PCM_STREAM_CAPTURE].running) |
| 986 | snd_nm256_capture_stop(chip); |
| 987 | chip->badintrcount = 0; |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 988 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | } |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 990 | return IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /* |
| 994 | * Handle a potential interrupt for the device referred to by DEV_ID. |
| 995 | * |
| 996 | * I don't like the cut-n-paste job here either between the two routines, |
| 997 | * but there are sufficient differences between the two interrupt handlers |
| 998 | * that parameterizing it isn't all that great either. (Could use a macro, |
| 999 | * I suppose...yucky bleah.) |
| 1000 | */ |
| 1001 | |
| 1002 | static irqreturn_t |
| 1003 | snd_nm256_interrupt(int irq, void *dev_id, struct pt_regs *dummy) |
| 1004 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1005 | struct nm256 *chip = dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | u16 status; |
| 1007 | u8 cbyte; |
| 1008 | |
| 1009 | status = snd_nm256_readw(chip, NM_INT_REG); |
| 1010 | |
| 1011 | /* Not ours. */ |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 1012 | if (status == 0) |
| 1013 | return snd_nm256_intr_check(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | |
| 1015 | chip->badintrcount = 0; |
| 1016 | |
| 1017 | /* Rather boring; check for individual interrupts and process them. */ |
| 1018 | |
| 1019 | spin_lock(&chip->reg_lock); |
| 1020 | if (status & NM_PLAYBACK_INT) { |
| 1021 | status &= ~NM_PLAYBACK_INT; |
| 1022 | NM_ACK_INT(chip, NM_PLAYBACK_INT); |
| 1023 | snd_nm256_playback_update(chip); |
| 1024 | } |
| 1025 | |
| 1026 | if (status & NM_RECORD_INT) { |
| 1027 | status &= ~NM_RECORD_INT; |
| 1028 | NM_ACK_INT(chip, NM_RECORD_INT); |
| 1029 | snd_nm256_capture_update(chip); |
| 1030 | } |
| 1031 | |
| 1032 | if (status & NM_MISC_INT_1) { |
| 1033 | status &= ~NM_MISC_INT_1; |
| 1034 | NM_ACK_INT(chip, NM_MISC_INT_1); |
| 1035 | snd_printd("NM256: Got misc interrupt #1\n"); |
| 1036 | snd_nm256_writew(chip, NM_INT_REG, 0x8000); |
| 1037 | cbyte = snd_nm256_readb(chip, 0x400); |
| 1038 | snd_nm256_writeb(chip, 0x400, cbyte | 2); |
| 1039 | } |
| 1040 | |
| 1041 | if (status & NM_MISC_INT_2) { |
| 1042 | status &= ~NM_MISC_INT_2; |
| 1043 | NM_ACK_INT(chip, NM_MISC_INT_2); |
| 1044 | snd_printd("NM256: Got misc interrupt #2\n"); |
| 1045 | cbyte = snd_nm256_readb(chip, 0x400); |
| 1046 | snd_nm256_writeb(chip, 0x400, cbyte & ~2); |
| 1047 | } |
| 1048 | |
| 1049 | /* Unknown interrupt. */ |
| 1050 | if (status) { |
| 1051 | snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n", |
| 1052 | status); |
| 1053 | /* Pray. */ |
| 1054 | NM_ACK_INT(chip, status); |
| 1055 | } |
| 1056 | |
| 1057 | spin_unlock(&chip->reg_lock); |
| 1058 | return IRQ_HANDLED; |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | * Handle a potential interrupt for the device referred to by DEV_ID. |
| 1063 | * This handler is for the 256ZX, and is very similar to the non-ZX |
| 1064 | * routine. |
| 1065 | */ |
| 1066 | |
| 1067 | static irqreturn_t |
| 1068 | snd_nm256_interrupt_zx(int irq, void *dev_id, struct pt_regs *dummy) |
| 1069 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1070 | struct nm256 *chip = dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | u32 status; |
| 1072 | u8 cbyte; |
| 1073 | |
| 1074 | status = snd_nm256_readl(chip, NM_INT_REG); |
| 1075 | |
| 1076 | /* Not ours. */ |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 1077 | if (status == 0) |
| 1078 | return snd_nm256_intr_check(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
| 1080 | chip->badintrcount = 0; |
| 1081 | |
| 1082 | /* Rather boring; check for individual interrupts and process them. */ |
| 1083 | |
| 1084 | spin_lock(&chip->reg_lock); |
| 1085 | if (status & NM2_PLAYBACK_INT) { |
| 1086 | status &= ~NM2_PLAYBACK_INT; |
| 1087 | NM2_ACK_INT(chip, NM2_PLAYBACK_INT); |
| 1088 | snd_nm256_playback_update(chip); |
| 1089 | } |
| 1090 | |
| 1091 | if (status & NM2_RECORD_INT) { |
| 1092 | status &= ~NM2_RECORD_INT; |
| 1093 | NM2_ACK_INT(chip, NM2_RECORD_INT); |
| 1094 | snd_nm256_capture_update(chip); |
| 1095 | } |
| 1096 | |
| 1097 | if (status & NM2_MISC_INT_1) { |
| 1098 | status &= ~NM2_MISC_INT_1; |
| 1099 | NM2_ACK_INT(chip, NM2_MISC_INT_1); |
| 1100 | snd_printd("NM256: Got misc interrupt #1\n"); |
| 1101 | cbyte = snd_nm256_readb(chip, 0x400); |
| 1102 | snd_nm256_writeb(chip, 0x400, cbyte | 2); |
| 1103 | } |
| 1104 | |
| 1105 | if (status & NM2_MISC_INT_2) { |
| 1106 | status &= ~NM2_MISC_INT_2; |
| 1107 | NM2_ACK_INT(chip, NM2_MISC_INT_2); |
| 1108 | snd_printd("NM256: Got misc interrupt #2\n"); |
| 1109 | cbyte = snd_nm256_readb(chip, 0x400); |
| 1110 | snd_nm256_writeb(chip, 0x400, cbyte & ~2); |
| 1111 | } |
| 1112 | |
| 1113 | /* Unknown interrupt. */ |
| 1114 | if (status) { |
| 1115 | snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n", |
| 1116 | status); |
| 1117 | /* Pray. */ |
| 1118 | NM2_ACK_INT(chip, status); |
| 1119 | } |
| 1120 | |
| 1121 | spin_unlock(&chip->reg_lock); |
| 1122 | return IRQ_HANDLED; |
| 1123 | } |
| 1124 | |
| 1125 | /* |
| 1126 | * AC97 interface |
| 1127 | */ |
| 1128 | |
| 1129 | /* |
| 1130 | * Waits for the mixer to become ready to be written; returns a zero value |
| 1131 | * if it timed out. |
| 1132 | */ |
| 1133 | static int |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1134 | snd_nm256_ac97_ready(struct nm256 *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | { |
| 1136 | int timeout = 10; |
| 1137 | u32 testaddr; |
| 1138 | u16 testb; |
| 1139 | |
| 1140 | testaddr = chip->mixer_status_offset; |
| 1141 | testb = chip->mixer_status_mask; |
| 1142 | |
| 1143 | /* |
| 1144 | * Loop around waiting for the mixer to become ready. |
| 1145 | */ |
| 1146 | while (timeout-- > 0) { |
| 1147 | if ((snd_nm256_readw(chip, testaddr) & testb) == 0) |
| 1148 | return 1; |
| 1149 | udelay(100); |
| 1150 | } |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | /* |
| 1155 | */ |
| 1156 | static unsigned short |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1157 | snd_nm256_ac97_read(struct snd_ac97 *ac97, unsigned short reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1159 | struct nm256 *chip = ac97->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | int res; |
| 1161 | |
| 1162 | if (reg >= 128) |
| 1163 | return 0; |
| 1164 | |
| 1165 | if (! snd_nm256_ac97_ready(chip)) |
| 1166 | return 0; |
| 1167 | res = snd_nm256_readw(chip, chip->mixer_base + reg); |
| 1168 | /* Magic delay. Bleah yucky. */ |
| 1169 | msleep(1); |
| 1170 | return res; |
| 1171 | } |
| 1172 | |
| 1173 | /* |
| 1174 | */ |
| 1175 | static void |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1176 | snd_nm256_ac97_write(struct snd_ac97 *ac97, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | unsigned short reg, unsigned short val) |
| 1178 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1179 | struct nm256 *chip = ac97->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | int tries = 2; |
| 1181 | u32 base; |
| 1182 | |
| 1183 | base = chip->mixer_base; |
| 1184 | |
| 1185 | snd_nm256_ac97_ready(chip); |
| 1186 | |
| 1187 | /* Wait for the write to take, too. */ |
| 1188 | while (tries-- > 0) { |
| 1189 | snd_nm256_writew(chip, base + reg, val); |
| 1190 | msleep(1); /* a little delay here seems better.. */ |
| 1191 | if (snd_nm256_ac97_ready(chip)) |
| 1192 | return; |
| 1193 | } |
| 1194 | snd_printd("nm256: ac97 codec not ready..\n"); |
| 1195 | } |
| 1196 | |
| 1197 | /* initialize the ac97 into a known state */ |
| 1198 | static void |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1199 | snd_nm256_ac97_reset(struct snd_ac97 *ac97) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1201 | struct nm256 *chip = ac97->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | |
| 1203 | /* Reset the mixer. 'Tis magic! */ |
| 1204 | snd_nm256_writeb(chip, 0x6c0, 1); |
| 1205 | if (! chip->reset_workaround) { |
| 1206 | /* Dell latitude LS will lock up by this */ |
| 1207 | snd_nm256_writeb(chip, 0x6cc, 0x87); |
| 1208 | } |
John W. Linville | 47530cf | 2005-10-19 16:03:10 +0200 | [diff] [blame] | 1209 | if (! chip->reset_workaround_2) { |
| 1210 | /* Dell latitude CSx will lock up by this */ |
| 1211 | snd_nm256_writeb(chip, 0x6cc, 0x80); |
| 1212 | snd_nm256_writeb(chip, 0x6cc, 0x0); |
| 1213 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | /* create an ac97 mixer interface */ |
| 1217 | static int __devinit |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1218 | snd_nm256_mixer(struct nm256 *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1220 | struct snd_ac97_bus *pbus; |
| 1221 | struct snd_ac97_template ac97; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | int i, err; |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1223 | static struct snd_ac97_bus_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | .reset = snd_nm256_ac97_reset, |
| 1225 | .write = snd_nm256_ac97_write, |
| 1226 | .read = snd_nm256_ac97_read, |
| 1227 | }; |
| 1228 | /* looks like nm256 hangs up when unexpected registers are touched... */ |
| 1229 | static int mixer_regs[] = { |
| 1230 | AC97_MASTER, AC97_HEADPHONE, AC97_MASTER_MONO, |
| 1231 | AC97_PC_BEEP, AC97_PHONE, AC97_MIC, AC97_LINE, AC97_CD, |
| 1232 | AC97_VIDEO, AC97_AUX, AC97_PCM, AC97_REC_SEL, |
| 1233 | AC97_REC_GAIN, AC97_GENERAL_PURPOSE, AC97_3D_CONTROL, |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 1234 | /*AC97_EXTENDED_ID,*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | AC97_VENDOR_ID1, AC97_VENDOR_ID2, |
| 1236 | -1 |
| 1237 | }; |
| 1238 | |
| 1239 | if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0) |
| 1240 | return err; |
| 1241 | |
| 1242 | memset(&ac97, 0, sizeof(ac97)); |
| 1243 | ac97.scaps = AC97_SCAP_AUDIO; /* we support audio! */ |
| 1244 | ac97.limited_regs = 1; |
| 1245 | for (i = 0; mixer_regs[i] >= 0; i++) |
| 1246 | set_bit(mixer_regs[i], ac97.reg_accessed); |
| 1247 | ac97.private_data = chip; |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 1248 | pbus->no_vra = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | err = snd_ac97_mixer(pbus, &ac97, &chip->ac97); |
| 1250 | if (err < 0) |
| 1251 | return err; |
| 1252 | if (! (chip->ac97->id & (0xf0000000))) { |
| 1253 | /* looks like an invalid id */ |
| 1254 | sprintf(chip->card->mixername, "%s AC97", chip->card->driver); |
| 1255 | } |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
| 1259 | /* |
| 1260 | * See if the signature left by the NM256 BIOS is intact; if so, we use |
| 1261 | * the associated address as the end of our audio buffer in the video |
| 1262 | * RAM. |
| 1263 | */ |
| 1264 | |
| 1265 | static int __devinit |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1266 | snd_nm256_peek_for_sig(struct nm256 *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | { |
| 1268 | /* The signature is located 1K below the end of video RAM. */ |
| 1269 | void __iomem *temp; |
| 1270 | /* Default buffer end is 5120 bytes below the top of RAM. */ |
| 1271 | unsigned long pointer_found = chip->buffer_end - 0x1400; |
| 1272 | u32 sig; |
| 1273 | |
| 1274 | temp = ioremap_nocache(chip->buffer_addr + chip->buffer_end - 0x400, 16); |
| 1275 | if (temp == NULL) { |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 1276 | snd_printk(KERN_ERR "Unable to scan for card signature in video RAM\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | return -EBUSY; |
| 1278 | } |
| 1279 | |
| 1280 | sig = readl(temp); |
| 1281 | if ((sig & NM_SIG_MASK) == NM_SIGNATURE) { |
| 1282 | u32 pointer = readl(temp + 4); |
| 1283 | |
| 1284 | /* |
| 1285 | * If it's obviously invalid, don't use it |
| 1286 | */ |
| 1287 | if (pointer == 0xffffffff || |
| 1288 | pointer < chip->buffer_size || |
| 1289 | pointer > chip->buffer_end) { |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 1290 | snd_printk(KERN_ERR "invalid signature found: 0x%x\n", pointer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | iounmap(temp); |
| 1292 | return -ENODEV; |
| 1293 | } else { |
| 1294 | pointer_found = pointer; |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1295 | printk(KERN_INFO "nm256: found card signature in video RAM: 0x%x\n", |
| 1296 | pointer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | iounmap(temp); |
| 1301 | chip->buffer_end = pointer_found; |
| 1302 | |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
| 1306 | #ifdef CONFIG_PM |
| 1307 | /* |
| 1308 | * APM event handler, so the card is properly reinitialized after a power |
| 1309 | * event. |
| 1310 | */ |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1311 | static int nm256_suspend(struct snd_card *card, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1313 | struct nm256 *chip = card->pm_private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | |
| 1315 | snd_pcm_suspend_all(chip->pcm); |
| 1316 | snd_ac97_suspend(chip->ac97); |
| 1317 | chip->coeffs_current = 0; |
| 1318 | pci_disable_device(chip->pci); |
| 1319 | return 0; |
| 1320 | } |
| 1321 | |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1322 | static int nm256_resume(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1324 | struct nm256 *chip = card->pm_private_data; |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 1325 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | |
| 1327 | /* Perform a full reset on the hardware */ |
| 1328 | pci_enable_device(chip->pci); |
| 1329 | snd_nm256_init_chip(chip); |
| 1330 | |
| 1331 | /* restore ac97 */ |
| 1332 | snd_ac97_resume(chip->ac97); |
| 1333 | |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 1334 | for (i = 0; i < 2; i++) { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1335 | struct nm256_stream *s = &chip->streams[i]; |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 1336 | if (s->substream && s->suspended) { |
| 1337 | spin_lock_irq(&chip->reg_lock); |
| 1338 | snd_nm256_set_format(chip, s, s->substream); |
| 1339 | spin_unlock_irq(&chip->reg_lock); |
| 1340 | } |
| 1341 | } |
| 1342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | return 0; |
| 1344 | } |
| 1345 | #endif /* CONFIG_PM */ |
| 1346 | |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1347 | static int snd_nm256_free(struct nm256 *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | { |
| 1349 | if (chip->streams[SNDRV_PCM_STREAM_PLAYBACK].running) |
| 1350 | snd_nm256_playback_stop(chip); |
| 1351 | if (chip->streams[SNDRV_PCM_STREAM_CAPTURE].running) |
| 1352 | snd_nm256_capture_stop(chip); |
| 1353 | |
| 1354 | if (chip->irq >= 0) |
| 1355 | synchronize_irq(chip->irq); |
| 1356 | |
| 1357 | if (chip->cport) |
| 1358 | iounmap(chip->cport); |
| 1359 | if (chip->buffer) |
| 1360 | iounmap(chip->buffer); |
Takashi Iwai | b1d5776 | 2005-10-10 11:56:31 +0200 | [diff] [blame] | 1361 | release_and_free_resource(chip->res_cport); |
| 1362 | release_and_free_resource(chip->res_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | if (chip->irq >= 0) |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1364 | free_irq(chip->irq, chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
| 1366 | pci_disable_device(chip->pci); |
| 1367 | kfree(chip); |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1371 | static int snd_nm256_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1373 | struct nm256 *chip = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | return snd_nm256_free(chip); |
| 1375 | } |
| 1376 | |
| 1377 | static int __devinit |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1378 | snd_nm256_create(struct snd_card *card, struct pci_dev *pci, |
| 1379 | struct nm256 **chip_ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1381 | struct nm256 *chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | int err, pval; |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1383 | static struct snd_device_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | .dev_free = snd_nm256_dev_free, |
| 1385 | }; |
| 1386 | u32 addr; |
| 1387 | |
| 1388 | *chip_ret = NULL; |
| 1389 | |
| 1390 | if ((err = pci_enable_device(pci)) < 0) |
| 1391 | return err; |
| 1392 | |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 1393 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | if (chip == NULL) { |
| 1395 | pci_disable_device(pci); |
| 1396 | return -ENOMEM; |
| 1397 | } |
| 1398 | |
| 1399 | chip->card = card; |
| 1400 | chip->pci = pci; |
Takashi Iwai | 3f05f86 | 2005-11-17 11:12:06 +0100 | [diff] [blame] | 1401 | chip->use_cache = use_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | spin_lock_init(&chip->reg_lock); |
| 1403 | chip->irq = -1; |
Takashi Iwai | 1204de3 | 2005-08-16 16:54:12 +0200 | [diff] [blame] | 1404 | init_MUTEX(&chip->irq_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | |
Takashi Iwai | 3f05f86 | 2005-11-17 11:12:06 +0100 | [diff] [blame] | 1406 | /* store buffer sizes in bytes */ |
| 1407 | chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize = playback_bufsize * 1024; |
| 1408 | chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize = capture_bufsize * 1024; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | |
| 1410 | /* |
| 1411 | * The NM256 has two memory ports. The first port is nothing |
| 1412 | * more than a chunk of video RAM, which is used as the I/O ring |
| 1413 | * buffer. The second port has the actual juicy stuff (like the |
| 1414 | * mixer and the playback engine control registers). |
| 1415 | */ |
| 1416 | |
| 1417 | chip->buffer_addr = pci_resource_start(pci, 0); |
| 1418 | chip->cport_addr = pci_resource_start(pci, 1); |
| 1419 | |
| 1420 | /* Init the memory port info. */ |
| 1421 | /* remap control port (#2) */ |
| 1422 | chip->res_cport = request_mem_region(chip->cport_addr, NM_PORT2_SIZE, |
| 1423 | card->driver); |
| 1424 | if (chip->res_cport == NULL) { |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 1425 | snd_printk(KERN_ERR "memory region 0x%lx (size 0x%x) busy\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | chip->cport_addr, NM_PORT2_SIZE); |
| 1427 | err = -EBUSY; |
| 1428 | goto __error; |
| 1429 | } |
| 1430 | chip->cport = ioremap_nocache(chip->cport_addr, NM_PORT2_SIZE); |
| 1431 | if (chip->cport == NULL) { |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 1432 | snd_printk(KERN_ERR "unable to map control port %lx\n", chip->cport_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | err = -ENOMEM; |
| 1434 | goto __error; |
| 1435 | } |
| 1436 | |
| 1437 | if (!strcmp(card->driver, "NM256AV")) { |
| 1438 | /* Ok, try to see if this is a non-AC97 version of the hardware. */ |
| 1439 | pval = snd_nm256_readw(chip, NM_MIXER_PRESENCE); |
| 1440 | if ((pval & NM_PRESENCE_MASK) != NM_PRESENCE_VALUE) { |
Takashi Iwai | 3f05f86 | 2005-11-17 11:12:06 +0100 | [diff] [blame] | 1441 | if (! force_ac97) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | printk(KERN_ERR "nm256: no ac97 is found!\n"); |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1443 | printk(KERN_ERR " force the driver to load by " |
| 1444 | "passing in the module parameter\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | printk(KERN_ERR " force_ac97=1\n"); |
| 1446 | printk(KERN_ERR " or try sb16 or cs423x drivers instead.\n"); |
| 1447 | err = -ENXIO; |
| 1448 | goto __error; |
| 1449 | } |
| 1450 | } |
| 1451 | chip->buffer_end = 2560 * 1024; |
| 1452 | chip->interrupt = snd_nm256_interrupt; |
| 1453 | chip->mixer_status_offset = NM_MIXER_STATUS_OFFSET; |
| 1454 | chip->mixer_status_mask = NM_MIXER_READY_MASK; |
| 1455 | } else { |
| 1456 | /* Not sure if there is any relevant detect for the ZX or not. */ |
| 1457 | if (snd_nm256_readb(chip, 0xa0b) != 0) |
| 1458 | chip->buffer_end = 6144 * 1024; |
| 1459 | else |
| 1460 | chip->buffer_end = 4096 * 1024; |
| 1461 | |
| 1462 | chip->interrupt = snd_nm256_interrupt_zx; |
| 1463 | chip->mixer_status_offset = NM2_MIXER_STATUS_OFFSET; |
| 1464 | chip->mixer_status_mask = NM2_MIXER_READY_MASK; |
| 1465 | } |
| 1466 | |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1467 | chip->buffer_size = chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize + |
| 1468 | chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | if (chip->use_cache) |
| 1470 | chip->buffer_size += NM_TOTAL_COEFF_COUNT * 4; |
| 1471 | else |
| 1472 | chip->buffer_size += NM_MAX_PLAYBACK_COEF_SIZE + NM_MAX_RECORD_COEF_SIZE; |
| 1473 | |
Takashi Iwai | 3f05f86 | 2005-11-17 11:12:06 +0100 | [diff] [blame] | 1474 | if (buffer_top >= chip->buffer_size && buffer_top < chip->buffer_end) |
| 1475 | chip->buffer_end = buffer_top; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | else { |
| 1477 | /* get buffer end pointer from signature */ |
| 1478 | if ((err = snd_nm256_peek_for_sig(chip)) < 0) |
| 1479 | goto __error; |
| 1480 | } |
| 1481 | |
| 1482 | chip->buffer_start = chip->buffer_end - chip->buffer_size; |
| 1483 | chip->buffer_addr += chip->buffer_start; |
| 1484 | |
| 1485 | printk(KERN_INFO "nm256: Mapping port 1 from 0x%x - 0x%x\n", |
| 1486 | chip->buffer_start, chip->buffer_end); |
| 1487 | |
| 1488 | chip->res_buffer = request_mem_region(chip->buffer_addr, |
| 1489 | chip->buffer_size, |
| 1490 | card->driver); |
| 1491 | if (chip->res_buffer == NULL) { |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 1492 | snd_printk(KERN_ERR "nm256: buffer 0x%lx (size 0x%x) busy\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | chip->buffer_addr, chip->buffer_size); |
| 1494 | err = -EBUSY; |
| 1495 | goto __error; |
| 1496 | } |
| 1497 | chip->buffer = ioremap_nocache(chip->buffer_addr, chip->buffer_size); |
| 1498 | if (chip->buffer == NULL) { |
| 1499 | err = -ENOMEM; |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 1500 | snd_printk(KERN_ERR "unable to map ring buffer at %lx\n", chip->buffer_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | goto __error; |
| 1502 | } |
| 1503 | |
| 1504 | /* set offsets */ |
| 1505 | addr = chip->buffer_start; |
| 1506 | chip->streams[SNDRV_PCM_STREAM_PLAYBACK].buf = addr; |
| 1507 | addr += chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize; |
| 1508 | chip->streams[SNDRV_PCM_STREAM_CAPTURE].buf = addr; |
| 1509 | addr += chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize; |
| 1510 | if (chip->use_cache) { |
| 1511 | chip->all_coeff_buf = addr; |
| 1512 | } else { |
| 1513 | chip->coeff_buf[SNDRV_PCM_STREAM_PLAYBACK] = addr; |
| 1514 | addr += NM_MAX_PLAYBACK_COEF_SIZE; |
| 1515 | chip->coeff_buf[SNDRV_PCM_STREAM_CAPTURE] = addr; |
| 1516 | } |
| 1517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | /* Fixed setting. */ |
| 1519 | chip->mixer_base = NM_MIXER_OFFSET; |
| 1520 | |
| 1521 | chip->coeffs_current = 0; |
| 1522 | |
| 1523 | snd_nm256_init_chip(chip); |
| 1524 | |
| 1525 | // pci_set_master(pci); /* needed? */ |
| 1526 | |
| 1527 | snd_card_set_pm_callback(card, nm256_suspend, nm256_resume, chip); |
| 1528 | |
| 1529 | if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) |
| 1530 | goto __error; |
| 1531 | |
| 1532 | snd_card_set_dev(card, &pci->dev); |
| 1533 | |
| 1534 | *chip_ret = chip; |
| 1535 | return 0; |
| 1536 | |
| 1537 | __error: |
| 1538 | snd_nm256_free(chip); |
| 1539 | return err; |
| 1540 | } |
| 1541 | |
| 1542 | |
| 1543 | struct nm256_quirk { |
| 1544 | unsigned short vendor; |
| 1545 | unsigned short device; |
| 1546 | int type; |
| 1547 | }; |
| 1548 | |
John W. Linville | 47530cf | 2005-10-19 16:03:10 +0200 | [diff] [blame] | 1549 | enum { NM_BLACKLISTED, NM_RESET_WORKAROUND, NM_RESET_WORKAROUND_2 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | |
| 1551 | static struct nm256_quirk nm256_quirks[] __devinitdata = { |
| 1552 | /* HP omnibook 4150 has cs4232 codec internally */ |
| 1553 | { .vendor = 0x103c, .device = 0x0007, .type = NM_BLACKLISTED }, |
| 1554 | /* Sony PCG-F305 */ |
| 1555 | { .vendor = 0x104d, .device = 0x8041, .type = NM_RESET_WORKAROUND }, |
| 1556 | /* Dell Latitude LS */ |
| 1557 | { .vendor = 0x1028, .device = 0x0080, .type = NM_RESET_WORKAROUND }, |
John W. Linville | 47530cf | 2005-10-19 16:03:10 +0200 | [diff] [blame] | 1558 | /* Dell Latitude CSx */ |
| 1559 | { .vendor = 0x1028, .device = 0x0091, .type = NM_RESET_WORKAROUND_2 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | { } /* terminator */ |
| 1561 | }; |
| 1562 | |
| 1563 | |
| 1564 | static int __devinit snd_nm256_probe(struct pci_dev *pci, |
| 1565 | const struct pci_device_id *pci_id) |
| 1566 | { |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1567 | struct snd_card *card; |
| 1568 | struct nm256 *chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | struct nm256_quirk *q; |
| 1571 | u16 subsystem_vendor, subsystem_device; |
| 1572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor); |
| 1574 | pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device); |
| 1575 | |
| 1576 | for (q = nm256_quirks; q->vendor; q++) { |
| 1577 | if (q->vendor == subsystem_vendor && q->device == subsystem_device) { |
| 1578 | switch (q->type) { |
| 1579 | case NM_BLACKLISTED: |
Takashi Iwai | 10754f53 | 2005-11-17 15:01:29 +0100 | [diff] [blame^] | 1580 | printk(KERN_INFO "nm256: The device is blacklisted. " |
| 1581 | "Loading stopped\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | return -ENODEV; |
John W. Linville | 47530cf | 2005-10-19 16:03:10 +0200 | [diff] [blame] | 1583 | case NM_RESET_WORKAROUND_2: |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 1584 | reset_workaround_2 = 1; |
John W. Linville | 47530cf | 2005-10-19 16:03:10 +0200 | [diff] [blame] | 1585 | /* Fall-through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | case NM_RESET_WORKAROUND: |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 1587 | reset_workaround = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | break; |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 1593 | card = snd_card_new(index, id, THIS_MODULE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | if (card == NULL) |
| 1595 | return -ENOMEM; |
| 1596 | |
| 1597 | switch (pci->device) { |
| 1598 | case PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO: |
| 1599 | strcpy(card->driver, "NM256AV"); |
| 1600 | break; |
| 1601 | case PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO: |
| 1602 | strcpy(card->driver, "NM256ZX"); |
| 1603 | break; |
| 1604 | case PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO: |
| 1605 | strcpy(card->driver, "NM256XL+"); |
| 1606 | break; |
| 1607 | default: |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 1608 | snd_printk(KERN_ERR "invalid device id 0x%x\n", pci->device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | snd_card_free(card); |
| 1610 | return -EINVAL; |
| 1611 | } |
| 1612 | |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 1613 | if (vaio_hack) |
Takashi Iwai | 3f05f86 | 2005-11-17 11:12:06 +0100 | [diff] [blame] | 1614 | buffer_top = 0x25a800; /* this avoids conflicts with XFree86 server */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 1616 | if (playback_bufsize < 4) |
| 1617 | playback_bufsize = 4; |
| 1618 | if (playback_bufsize > 128) |
| 1619 | playback_bufsize = 128; |
| 1620 | if (capture_bufsize < 4) |
| 1621 | capture_bufsize = 4; |
| 1622 | if (capture_bufsize > 128) |
| 1623 | capture_bufsize = 128; |
Takashi Iwai | 3f05f86 | 2005-11-17 11:12:06 +0100 | [diff] [blame] | 1624 | if ((err = snd_nm256_create(card, pci, &chip)) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | snd_card_free(card); |
| 1626 | return err; |
| 1627 | } |
| 1628 | |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 1629 | if (reset_workaround) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | snd_printdd(KERN_INFO "nm256: reset_workaround activated\n"); |
| 1631 | chip->reset_workaround = 1; |
| 1632 | } |
| 1633 | |
Takashi Iwai | 8a3fb4d | 2005-10-20 17:10:49 +0200 | [diff] [blame] | 1634 | if (reset_workaround_2) { |
John W. Linville | 47530cf | 2005-10-19 16:03:10 +0200 | [diff] [blame] | 1635 | snd_printdd(KERN_INFO "nm256: reset_workaround_2 activated\n"); |
| 1636 | chip->reset_workaround_2 = 1; |
| 1637 | } |
| 1638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | if ((err = snd_nm256_pcm(chip, 0)) < 0 || |
| 1640 | (err = snd_nm256_mixer(chip)) < 0) { |
| 1641 | snd_card_free(card); |
| 1642 | return err; |
| 1643 | } |
| 1644 | |
| 1645 | sprintf(card->shortname, "NeoMagic %s", card->driver); |
| 1646 | sprintf(card->longname, "%s at 0x%lx & 0x%lx, irq %d", |
| 1647 | card->shortname, |
| 1648 | chip->buffer_addr, chip->cport_addr, chip->irq); |
| 1649 | |
| 1650 | if ((err = snd_card_register(card)) < 0) { |
| 1651 | snd_card_free(card); |
| 1652 | return err; |
| 1653 | } |
| 1654 | |
| 1655 | pci_set_drvdata(pci, card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | return 0; |
| 1657 | } |
| 1658 | |
| 1659 | static void __devexit snd_nm256_remove(struct pci_dev *pci) |
| 1660 | { |
| 1661 | snd_card_free(pci_get_drvdata(pci)); |
| 1662 | pci_set_drvdata(pci, NULL); |
| 1663 | } |
| 1664 | |
| 1665 | |
| 1666 | static struct pci_driver driver = { |
| 1667 | .name = "NeoMagic 256", |
| 1668 | .id_table = snd_nm256_ids, |
| 1669 | .probe = snd_nm256_probe, |
| 1670 | .remove = __devexit_p(snd_nm256_remove), |
| 1671 | SND_PCI_PM_CALLBACKS |
| 1672 | }; |
| 1673 | |
| 1674 | |
| 1675 | static int __init alsa_card_nm256_init(void) |
| 1676 | { |
Takashi Iwai | 01d25d4 | 2005-04-11 16:58:24 +0200 | [diff] [blame] | 1677 | return pci_register_driver(&driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | static void __exit alsa_card_nm256_exit(void) |
| 1681 | { |
| 1682 | pci_unregister_driver(&driver); |
| 1683 | } |
| 1684 | |
| 1685 | module_init(alsa_card_nm256_init) |
| 1686 | module_exit(alsa_card_nm256_exit) |