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