blob: fe79fff4c6dc303fd3a5babde6320446979a3f14 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/io.h>
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 Gortmaker65a77212011-07-15 13:13:37 -040033#include <linux/module.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010034#include <linux/mutex.h>
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#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
46MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
47MODULE_DESCRIPTION("NeoMagic NM256AV/ZX");
48MODULE_LICENSE("GPL");
49MODULE_SUPPORTED_DEVICE("{{NeoMagic,NM256AV},"
50 "{NeoMagic,NM256ZX}}");
51
52/*
53 * some compile conditions.
54 */
55
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020056static int index = SNDRV_DEFAULT_IDX1; /* Index */
57static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
58static int playback_bufsize = 16;
59static int capture_bufsize = 16;
Rusty Russella67ff6a2011-12-15 13:49:36 +103060static bool force_ac97; /* disabled as default */
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020061static int buffer_top; /* not specified */
Rusty Russella67ff6a2011-12-15 13:49:36 +103062static bool use_cache; /* disabled */
63static bool vaio_hack; /* disabled */
64static bool reset_workaround;
65static bool reset_workaround_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020067module_param(index, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020069module_param(id, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020071module_param(playback_bufsize, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072MODULE_PARM_DESC(playback_bufsize, "DAC frame size in kB for " CARD_NAME " soundcard.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020073module_param(capture_bufsize, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074MODULE_PARM_DESC(capture_bufsize, "ADC frame size in kB for " CARD_NAME " soundcard.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020075module_param(force_ac97, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076MODULE_PARM_DESC(force_ac97, "Force to use AC97 codec for " CARD_NAME " soundcard.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020077module_param(buffer_top, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078MODULE_PARM_DESC(buffer_top, "Set the top address of audio buffer for " CARD_NAME " soundcard.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020079module_param(use_cache, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080MODULE_PARM_DESC(use_cache, "Enable the cache for coefficient table access.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020081module_param(vaio_hack, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082MODULE_PARM_DESC(vaio_hack, "Enable workaround for Sony VAIO notebooks.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020083module_param(reset_workaround, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084MODULE_PARM_DESC(reset_workaround, "Enable AC97 RESET workaround for some laptops.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020085module_param(reset_workaround_2, bool, 0444);
John W. Linville47530cf2005-10-19 16:03:10 +020086MODULE_PARM_DESC(reset_workaround_2, "Enable extended AC97 RESET workaround for some other laptops.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020088/* just for backward compatibility */
Rusty Russella67ff6a2011-12-15 13:49:36 +103089static bool enable;
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020090module_param(enable, bool, 0444);
91
92
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
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 Iwai10754f532005-11-17 15:01:29 +0100187struct nm256_stream {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Takashi Iwai10754f532005-11-17 15:01:29 +0100189 struct nm256 *chip;
190 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int running;
Takashi Iwai1204de32005-08-16 16:54:12 +0200192 int suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
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 Iwai10754f532005-11-17 15:01:29 +0100207struct nm256 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Takashi Iwai10754f532005-11-17 15:01:29 +0100209 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
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. Linville47530cf2005-10-19 16:03:10 +0200229 unsigned int reset_workaround_2: 1; /* Extended workaround for some other laptops to avoid freeze */
Florian Schlichtinga23446c2006-03-15 14:05:19 +0100230 unsigned int in_resume: 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
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 Iwai1204de32005-08-16 16:54:12 +0200237 int irq_acks;
David Howells7d12e782006-10-05 14:55:46 +0100238 irq_handler_t interrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 int badintrcount; /* counter to check bogus interrupts */
Ingo Molnar62932df2006-01-16 16:34:20 +0100240 struct mutex irq_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Takashi Iwai10754f532005-11-17 15:01:29 +0100242 struct nm256_stream streams[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Takashi Iwai10754f532005-11-17 15:01:29 +0100244 struct snd_ac97 *ac97;
Florian Schlichtinga23446c2006-03-15 14:05:19 +0100245 unsigned short *ac97_regs; /* register caches, only for valid regs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Takashi Iwai10754f532005-11-17 15:01:29 +0100247 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
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 */
Alexey Dobriyancebe41d2010-02-06 00:21:03 +0200265static DEFINE_PCI_DEVICE_TABLE(snd_nm256_ids) = {
Joe Perches28d27aa2009-06-24 22:13:35 -0700266 {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 Torvalds1da177e2005-04-16 15:20:36 -0700269 {0,},
270};
271
272MODULE_DEVICE_TABLE(pci, snd_nm256_ids);
273
274
275/*
276 * lowlvel stuffs
277 */
278
Jesper Juhl77933d72005-07-27 11:46:09 -0700279static inline u8
Takashi Iwai10754f532005-11-17 15:01:29 +0100280snd_nm256_readb(struct nm256 *chip, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 return readb(chip->cport + offset);
283}
284
Jesper Juhl77933d72005-07-27 11:46:09 -0700285static inline u16
Takashi Iwai10754f532005-11-17 15:01:29 +0100286snd_nm256_readw(struct nm256 *chip, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 return readw(chip->cport + offset);
289}
290
Jesper Juhl77933d72005-07-27 11:46:09 -0700291static inline u32
Takashi Iwai10754f532005-11-17 15:01:29 +0100292snd_nm256_readl(struct nm256 *chip, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 return readl(chip->cport + offset);
295}
296
Jesper Juhl77933d72005-07-27 11:46:09 -0700297static inline void
Takashi Iwai10754f532005-11-17 15:01:29 +0100298snd_nm256_writeb(struct nm256 *chip, int offset, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 writeb(val, chip->cport + offset);
301}
302
Jesper Juhl77933d72005-07-27 11:46:09 -0700303static inline void
Takashi Iwai10754f532005-11-17 15:01:29 +0100304snd_nm256_writew(struct nm256 *chip, int offset, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 writew(val, chip->cport + offset);
307}
308
Jesper Juhl77933d72005-07-27 11:46:09 -0700309static inline void
Takashi Iwai10754f532005-11-17 15:01:29 +0100310snd_nm256_writel(struct nm256 *chip, int offset, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 writel(val, chip->cport + offset);
313}
314
Jesper Juhl77933d72005-07-27 11:46:09 -0700315static inline void
Takashi Iwai10754f532005-11-17 15:01:29 +0100316snd_nm256_write_buffer(struct nm256 *chip, void *src, int offset, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 offset -= chip->buffer_start;
Takashi Iwai99b359b2005-10-20 18:26:44 +0200319#ifdef CONFIG_SND_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (offset < 0 || offset >= chip->buffer_size) {
Takashi Iwai10754f532005-11-17 15:01:29 +0100321 snd_printk(KERN_ERR "write_buffer invalid offset = %d size = %d\n",
322 offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return;
324 }
325#endif
326 memcpy_toio(chip->buffer + offset, src, size);
327}
328
329/*
330 * coefficient handlers -- what a magic!
331 */
332
333static u16
334snd_nm256_get_start_offset(int which)
335{
336 u16 offset = 0;
337 while (which-- > 0)
338 offset += coefficient_sizes[which];
339 return offset;
340}
341
342static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100343snd_nm256_load_one_coefficient(struct nm256 *chip, int stream, u32 port, int which)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 u32 coeff_buf = chip->coeff_buf[stream];
346 u16 offset = snd_nm256_get_start_offset(which);
347 u16 size = coefficient_sizes[which];
348
349 snd_nm256_write_buffer(chip, coefficients + offset, coeff_buf, size);
350 snd_nm256_writel(chip, port, coeff_buf);
351 /* ??? Record seems to behave differently than playback. */
352 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
353 size--;
354 snd_nm256_writel(chip, port + 4, coeff_buf + size);
355}
356
357static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100358snd_nm256_load_coefficient(struct nm256 *chip, int stream, int number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 /* The enable register for the specified engine. */
Takashi Iwai10754f532005-11-17 15:01:29 +0100361 u32 poffset = (stream == SNDRV_PCM_STREAM_CAPTURE ?
362 NM_RECORD_ENABLE_REG : NM_PLAYBACK_ENABLE_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 u32 addr = NM_COEFF_START_OFFSET;
364
Takashi Iwai10754f532005-11-17 15:01:29 +0100365 addr += (stream == SNDRV_PCM_STREAM_CAPTURE ?
366 NM_RECORD_REG_OFFSET : NM_PLAYBACK_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 if (snd_nm256_readb(chip, poffset) & 1) {
369 snd_printd("NM256: Engine was enabled while loading coefficients!\n");
370 return;
371 }
372
373 /* The recording engine uses coefficient values 8-15. */
374 number &= 7;
375 if (stream == SNDRV_PCM_STREAM_CAPTURE)
376 number += 8;
377
378 if (! chip->use_cache) {
379 snd_nm256_load_one_coefficient(chip, stream, addr, number);
380 return;
381 }
382 if (! chip->coeffs_current) {
383 snd_nm256_write_buffer(chip, coefficients, chip->all_coeff_buf,
384 NM_TOTAL_COEFF_COUNT * 4);
385 chip->coeffs_current = 1;
386 } else {
387 u32 base = chip->all_coeff_buf;
388 u32 offset = snd_nm256_get_start_offset(number);
389 u32 end_offset = offset + coefficient_sizes[number];
390 snd_nm256_writel(chip, addr, base + offset);
391 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
392 end_offset--;
393 snd_nm256_writel(chip, addr + 4, base + end_offset);
394 }
395}
396
397
398/* The actual rates supported by the card. */
399static unsigned int samplerates[8] = {
400 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000,
401};
Takashi Iwai10754f532005-11-17 15:01:29 +0100402static struct snd_pcm_hw_constraint_list constraints_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 .count = ARRAY_SIZE(samplerates),
404 .list = samplerates,
405 .mask = 0,
406};
407
408/*
409 * return the index of the target rate
410 */
411static int
412snd_nm256_fixed_rate(unsigned int rate)
413{
414 unsigned int i;
415 for (i = 0; i < ARRAY_SIZE(samplerates); i++) {
416 if (rate == samplerates[i])
417 return i;
418 }
419 snd_BUG();
420 return 0;
421}
422
423/*
424 * set sample rate and format
425 */
426static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100427snd_nm256_set_format(struct nm256 *chip, struct nm256_stream *s,
428 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Takashi Iwai10754f532005-11-17 15:01:29 +0100430 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 int rate_index = snd_nm256_fixed_rate(runtime->rate);
432 unsigned char ratebits = (rate_index << 4) & NM_RATE_MASK;
433
434 s->shift = 0;
435 if (snd_pcm_format_width(runtime->format) == 16) {
436 ratebits |= NM_RATE_BITS_16;
437 s->shift++;
438 }
439 if (runtime->channels > 1) {
440 ratebits |= NM_RATE_STEREO;
441 s->shift++;
442 }
443
444 runtime->rate = samplerates[rate_index];
445
446 switch (substream->stream) {
447 case SNDRV_PCM_STREAM_PLAYBACK:
448 snd_nm256_load_coefficient(chip, 0, rate_index); /* 0 = playback */
449 snd_nm256_writeb(chip,
450 NM_PLAYBACK_REG_OFFSET + NM_RATE_REG_OFFSET,
451 ratebits);
452 break;
453 case SNDRV_PCM_STREAM_CAPTURE:
454 snd_nm256_load_coefficient(chip, 1, rate_index); /* 1 = record */
455 snd_nm256_writeb(chip,
456 NM_RECORD_REG_OFFSET + NM_RATE_REG_OFFSET,
457 ratebits);
458 break;
459 }
460}
461
Takashi Iwai1204de32005-08-16 16:54:12 +0200462/* acquire interrupt */
Takashi Iwai10754f532005-11-17 15:01:29 +0100463static int snd_nm256_acquire_irq(struct nm256 *chip)
Takashi Iwai1204de32005-08-16 16:54:12 +0200464{
Ingo Molnar62932df2006-01-16 16:34:20 +0100465 mutex_lock(&chip->irq_mutex);
Takashi Iwai1204de32005-08-16 16:54:12 +0200466 if (chip->irq < 0) {
Takashi Iwai437a5a42006-11-21 12:14:23 +0100467 if (request_irq(chip->pci->irq, chip->interrupt, IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +0200468 KBUILD_MODNAME, chip)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200469 snd_printk(KERN_ERR "unable to grab IRQ %d\n", chip->pci->irq);
Ingo Molnar62932df2006-01-16 16:34:20 +0100470 mutex_unlock(&chip->irq_mutex);
Takashi Iwai1204de32005-08-16 16:54:12 +0200471 return -EBUSY;
472 }
473 chip->irq = chip->pci->irq;
474 }
475 chip->irq_acks++;
Ingo Molnar62932df2006-01-16 16:34:20 +0100476 mutex_unlock(&chip->irq_mutex);
Takashi Iwai1204de32005-08-16 16:54:12 +0200477 return 0;
478}
479
480/* release interrupt */
Takashi Iwai10754f532005-11-17 15:01:29 +0100481static void snd_nm256_release_irq(struct nm256 *chip)
Takashi Iwai1204de32005-08-16 16:54:12 +0200482{
Ingo Molnar62932df2006-01-16 16:34:20 +0100483 mutex_lock(&chip->irq_mutex);
Takashi Iwai1204de32005-08-16 16:54:12 +0200484 if (chip->irq_acks > 0)
485 chip->irq_acks--;
486 if (chip->irq_acks == 0 && chip->irq >= 0) {
Takashi Iwai10754f532005-11-17 15:01:29 +0100487 free_irq(chip->irq, chip);
Takashi Iwai1204de32005-08-16 16:54:12 +0200488 chip->irq = -1;
489 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100490 mutex_unlock(&chip->irq_mutex);
Takashi Iwai1204de32005-08-16 16:54:12 +0200491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/*
494 * start / stop
495 */
496
497/* update the watermark (current period) */
Takashi Iwai10754f532005-11-17 15:01:29 +0100498static void snd_nm256_pcm_mark(struct nm256 *chip, struct nm256_stream *s, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 s->cur_period++;
501 s->cur_period %= s->periods;
502 snd_nm256_writel(chip, reg, s->buf + s->cur_period * s->period_size);
503}
504
505#define snd_nm256_playback_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_PBUFFER_WMARK)
506#define snd_nm256_capture_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_RBUFFER_WMARK)
507
508static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100509snd_nm256_playback_start(struct nm256 *chip, struct nm256_stream *s,
510 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 /* program buffer pointers */
513 snd_nm256_writel(chip, NM_PBUFFER_START, s->buf);
514 snd_nm256_writel(chip, NM_PBUFFER_END, s->buf + s->dma_size - (1 << s->shift));
515 snd_nm256_writel(chip, NM_PBUFFER_CURRP, s->buf);
516 snd_nm256_playback_mark(chip, s);
517
518 /* Enable playback engine and interrupts. */
519 snd_nm256_writeb(chip, NM_PLAYBACK_ENABLE_REG,
520 NM_PLAYBACK_ENABLE_FLAG | NM_PLAYBACK_FREERUN);
521 /* Enable both channels. */
522 snd_nm256_writew(chip, NM_AUDIO_MUTE_REG, 0x0);
523}
524
525static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100526snd_nm256_capture_start(struct nm256 *chip, struct nm256_stream *s,
527 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 /* program buffer pointers */
530 snd_nm256_writel(chip, NM_RBUFFER_START, s->buf);
531 snd_nm256_writel(chip, NM_RBUFFER_END, s->buf + s->dma_size);
532 snd_nm256_writel(chip, NM_RBUFFER_CURRP, s->buf);
533 snd_nm256_capture_mark(chip, s);
534
535 /* Enable playback engine and interrupts. */
536 snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG,
537 NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN);
538}
539
540/* Stop the play engine. */
541static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100542snd_nm256_playback_stop(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 /* Shut off sound from both channels. */
545 snd_nm256_writew(chip, NM_AUDIO_MUTE_REG,
546 NM_AUDIO_MUTE_LEFT | NM_AUDIO_MUTE_RIGHT);
547 /* Disable play engine. */
548 snd_nm256_writeb(chip, NM_PLAYBACK_ENABLE_REG, 0);
549}
550
551static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100552snd_nm256_capture_stop(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 /* Disable recording engine. */
555 snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG, 0);
556}
557
558static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100559snd_nm256_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Takashi Iwai10754f532005-11-17 15:01:29 +0100561 struct nm256 *chip = snd_pcm_substream_chip(substream);
562 struct nm256_stream *s = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 int err = 0;
564
Takashi Iwaida3cec32008-08-08 17:12:14 +0200565 if (snd_BUG_ON(!s))
566 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 spin_lock(&chip->reg_lock);
569 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwai1204de32005-08-16 16:54:12 +0200571 s->suspended = 0;
572 /* fallthru */
573 case SNDRV_PCM_TRIGGER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (! s->running) {
575 snd_nm256_playback_start(chip, s, substream);
576 s->running = 1;
577 }
578 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwai1204de32005-08-16 16:54:12 +0200580 s->suspended = 1;
581 /* fallthru */
582 case SNDRV_PCM_TRIGGER_STOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (s->running) {
584 snd_nm256_playback_stop(chip);
585 s->running = 0;
586 }
587 break;
588 default:
589 err = -EINVAL;
590 break;
591 }
592 spin_unlock(&chip->reg_lock);
593 return err;
594}
595
596static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100597snd_nm256_capture_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Takashi Iwai10754f532005-11-17 15:01:29 +0100599 struct nm256 *chip = snd_pcm_substream_chip(substream);
600 struct nm256_stream *s = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 int err = 0;
602
Takashi Iwaida3cec32008-08-08 17:12:14 +0200603 if (snd_BUG_ON(!s))
604 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 spin_lock(&chip->reg_lock);
607 switch (cmd) {
608 case SNDRV_PCM_TRIGGER_START:
609 case SNDRV_PCM_TRIGGER_RESUME:
610 if (! s->running) {
611 snd_nm256_capture_start(chip, s, substream);
612 s->running = 1;
613 }
614 break;
615 case SNDRV_PCM_TRIGGER_STOP:
616 case SNDRV_PCM_TRIGGER_SUSPEND:
617 if (s->running) {
618 snd_nm256_capture_stop(chip);
619 s->running = 0;
620 }
621 break;
622 default:
623 err = -EINVAL;
624 break;
625 }
626 spin_unlock(&chip->reg_lock);
627 return err;
628}
629
630
631/*
632 * prepare playback/capture channel
633 */
Takashi Iwai10754f532005-11-17 15:01:29 +0100634static int snd_nm256_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Takashi Iwai10754f532005-11-17 15:01:29 +0100636 struct nm256 *chip = snd_pcm_substream_chip(substream);
637 struct snd_pcm_runtime *runtime = substream->runtime;
638 struct nm256_stream *s = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Takashi Iwaida3cec32008-08-08 17:12:14 +0200640 if (snd_BUG_ON(!s))
641 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 s->dma_size = frames_to_bytes(runtime, substream->runtime->buffer_size);
643 s->period_size = frames_to_bytes(runtime, substream->runtime->period_size);
644 s->periods = substream->runtime->periods;
645 s->cur_period = 0;
646
647 spin_lock_irq(&chip->reg_lock);
648 s->running = 0;
649 snd_nm256_set_format(chip, s, substream);
650 spin_unlock_irq(&chip->reg_lock);
651
652 return 0;
653}
654
655
656/*
657 * get the current pointer
658 */
659static snd_pcm_uframes_t
Takashi Iwai10754f532005-11-17 15:01:29 +0100660snd_nm256_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Takashi Iwai10754f532005-11-17 15:01:29 +0100662 struct nm256 *chip = snd_pcm_substream_chip(substream);
663 struct nm256_stream *s = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 unsigned long curp;
665
Takashi Iwaida3cec32008-08-08 17:12:14 +0200666 if (snd_BUG_ON(!s))
667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 curp = snd_nm256_readl(chip, NM_PBUFFER_CURRP) - (unsigned long)s->buf;
669 curp %= s->dma_size;
670 return bytes_to_frames(substream->runtime, curp);
671}
672
673static snd_pcm_uframes_t
Takashi Iwai10754f532005-11-17 15:01:29 +0100674snd_nm256_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Takashi Iwai10754f532005-11-17 15:01:29 +0100676 struct nm256 *chip = snd_pcm_substream_chip(substream);
677 struct nm256_stream *s = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 unsigned long curp;
679
Takashi Iwaida3cec32008-08-08 17:12:14 +0200680 if (snd_BUG_ON(!s))
681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 curp = snd_nm256_readl(chip, NM_RBUFFER_CURRP) - (unsigned long)s->buf;
683 curp %= s->dma_size;
684 return bytes_to_frames(substream->runtime, curp);
685}
686
687/* Remapped I/O space can be accessible as pointer on i386 */
688/* This might be changed in the future */
689#ifndef __i386__
690/*
691 * silence / copy for playback
692 */
693static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100694snd_nm256_playback_silence(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 int channel, /* not used (interleaved data) */
696 snd_pcm_uframes_t pos,
697 snd_pcm_uframes_t count)
698{
Takashi Iwai10754f532005-11-17 15:01:29 +0100699 struct snd_pcm_runtime *runtime = substream->runtime;
700 struct nm256_stream *s = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 count = frames_to_bytes(runtime, count);
702 pos = frames_to_bytes(runtime, pos);
703 memset_io(s->bufptr + pos, 0, count);
704 return 0;
705}
706
707static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100708snd_nm256_playback_copy(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 int channel, /* not used (interleaved data) */
710 snd_pcm_uframes_t pos,
711 void __user *src,
712 snd_pcm_uframes_t count)
713{
Takashi Iwai10754f532005-11-17 15:01:29 +0100714 struct snd_pcm_runtime *runtime = substream->runtime;
715 struct nm256_stream *s = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 count = frames_to_bytes(runtime, count);
717 pos = frames_to_bytes(runtime, pos);
718 if (copy_from_user_toio(s->bufptr + pos, src, count))
719 return -EFAULT;
720 return 0;
721}
722
723/*
724 * copy to user
725 */
726static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100727snd_nm256_capture_copy(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 int channel, /* not used (interleaved data) */
729 snd_pcm_uframes_t pos,
730 void __user *dst,
731 snd_pcm_uframes_t count)
732{
Takashi Iwai10754f532005-11-17 15:01:29 +0100733 struct snd_pcm_runtime *runtime = substream->runtime;
734 struct nm256_stream *s = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 count = frames_to_bytes(runtime, count);
736 pos = frames_to_bytes(runtime, pos);
737 if (copy_to_user_fromio(dst, s->bufptr + pos, count))
738 return -EFAULT;
739 return 0;
740}
741
742#endif /* !__i386__ */
743
744
745/*
746 * update playback/capture watermarks
747 */
748
749/* spinlock held! */
750static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100751snd_nm256_playback_update(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Takashi Iwai10754f532005-11-17 15:01:29 +0100753 struct nm256_stream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 s = &chip->streams[SNDRV_PCM_STREAM_PLAYBACK];
756 if (s->running && s->substream) {
757 spin_unlock(&chip->reg_lock);
758 snd_pcm_period_elapsed(s->substream);
759 spin_lock(&chip->reg_lock);
760 snd_nm256_playback_mark(chip, s);
761 }
762}
763
764/* spinlock held! */
765static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100766snd_nm256_capture_update(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Takashi Iwai10754f532005-11-17 15:01:29 +0100768 struct nm256_stream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 s = &chip->streams[SNDRV_PCM_STREAM_CAPTURE];
771 if (s->running && s->substream) {
772 spin_unlock(&chip->reg_lock);
773 snd_pcm_period_elapsed(s->substream);
774 spin_lock(&chip->reg_lock);
775 snd_nm256_capture_mark(chip, s);
776 }
777}
778
779/*
780 * hardware info
781 */
Takashi Iwai10754f532005-11-17 15:01:29 +0100782static struct snd_pcm_hardware snd_nm256_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 .info = SNDRV_PCM_INFO_MMAP_IOMEM |SNDRV_PCM_INFO_MMAP_VALID |
785 SNDRV_PCM_INFO_INTERLEAVED |
786 /*SNDRV_PCM_INFO_PAUSE |*/
787 SNDRV_PCM_INFO_RESUME,
788 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
789 .rates = SNDRV_PCM_RATE_KNOT/*24k*/ | SNDRV_PCM_RATE_8000_48000,
790 .rate_min = 8000,
791 .rate_max = 48000,
792 .channels_min = 1,
793 .channels_max = 2,
794 .periods_min = 2,
795 .periods_max = 1024,
796 .buffer_bytes_max = 128 * 1024,
797 .period_bytes_min = 256,
798 .period_bytes_max = 128 * 1024,
799};
800
Takashi Iwai10754f532005-11-17 15:01:29 +0100801static struct snd_pcm_hardware snd_nm256_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 .info = SNDRV_PCM_INFO_MMAP_IOMEM | SNDRV_PCM_INFO_MMAP_VALID |
804 SNDRV_PCM_INFO_INTERLEAVED |
805 /*SNDRV_PCM_INFO_PAUSE |*/
806 SNDRV_PCM_INFO_RESUME,
807 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
808 .rates = SNDRV_PCM_RATE_KNOT/*24k*/ | SNDRV_PCM_RATE_8000_48000,
809 .rate_min = 8000,
810 .rate_max = 48000,
811 .channels_min = 1,
812 .channels_max = 2,
813 .periods_min = 2,
814 .periods_max = 1024,
815 .buffer_bytes_max = 128 * 1024,
816 .period_bytes_min = 256,
817 .period_bytes_max = 128 * 1024,
818};
819
820
821/* set dma transfer size */
Takashi Iwai10754f532005-11-17 15:01:29 +0100822static int snd_nm256_pcm_hw_params(struct snd_pcm_substream *substream,
823 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 /* area and addr are already set and unchanged */
826 substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
827 return 0;
828}
829
830/*
831 * open
832 */
Takashi Iwai10754f532005-11-17 15:01:29 +0100833static void snd_nm256_setup_stream(struct nm256 *chip, struct nm256_stream *s,
834 struct snd_pcm_substream *substream,
835 struct snd_pcm_hardware *hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Takashi Iwai10754f532005-11-17 15:01:29 +0100837 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 s->running = 0;
840 runtime->hw = *hw_ptr;
841 runtime->hw.buffer_bytes_max = s->bufsize;
842 runtime->hw.period_bytes_max = s->bufsize / 2;
Clemens Ladisch4d233592005-09-05 10:35:20 +0200843 runtime->dma_area = (void __force *) s->bufptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 runtime->dma_addr = s->bufptr_addr;
845 runtime->dma_bytes = s->bufsize;
846 runtime->private_data = s;
847 s->substream = substream;
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
850 &constraints_rates);
851}
852
853static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100854snd_nm256_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Takashi Iwai10754f532005-11-17 15:01:29 +0100856 struct nm256 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Takashi Iwai1204de32005-08-16 16:54:12 +0200858 if (snd_nm256_acquire_irq(chip) < 0)
859 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK],
861 substream, &snd_nm256_playback);
862 return 0;
863}
864
865static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100866snd_nm256_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Takashi Iwai10754f532005-11-17 15:01:29 +0100868 struct nm256 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Takashi Iwai1204de32005-08-16 16:54:12 +0200870 if (snd_nm256_acquire_irq(chip) < 0)
871 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_CAPTURE],
873 substream, &snd_nm256_capture);
874 return 0;
875}
876
877/*
878 * close - we don't have to do special..
879 */
880static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100881snd_nm256_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Takashi Iwai10754f532005-11-17 15:01:29 +0100883 struct nm256 *chip = snd_pcm_substream_chip(substream);
Takashi Iwai1204de32005-08-16 16:54:12 +0200884
885 snd_nm256_release_irq(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return 0;
887}
888
889
890static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100891snd_nm256_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Takashi Iwai10754f532005-11-17 15:01:29 +0100893 struct nm256 *chip = snd_pcm_substream_chip(substream);
Takashi Iwai1204de32005-08-16 16:54:12 +0200894
895 snd_nm256_release_irq(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return 0;
897}
898
899/*
900 * create a pcm instance
901 */
Takashi Iwai10754f532005-11-17 15:01:29 +0100902static struct snd_pcm_ops snd_nm256_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 .open = snd_nm256_playback_open,
904 .close = snd_nm256_playback_close,
905 .ioctl = snd_pcm_lib_ioctl,
906 .hw_params = snd_nm256_pcm_hw_params,
907 .prepare = snd_nm256_pcm_prepare,
908 .trigger = snd_nm256_playback_trigger,
909 .pointer = snd_nm256_playback_pointer,
910#ifndef __i386__
911 .copy = snd_nm256_playback_copy,
912 .silence = snd_nm256_playback_silence,
913#endif
914 .mmap = snd_pcm_lib_mmap_iomem,
915};
916
Takashi Iwai10754f532005-11-17 15:01:29 +0100917static struct snd_pcm_ops snd_nm256_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 .open = snd_nm256_capture_open,
919 .close = snd_nm256_capture_close,
920 .ioctl = snd_pcm_lib_ioctl,
921 .hw_params = snd_nm256_pcm_hw_params,
922 .prepare = snd_nm256_pcm_prepare,
923 .trigger = snd_nm256_capture_trigger,
924 .pointer = snd_nm256_capture_pointer,
925#ifndef __i386__
926 .copy = snd_nm256_capture_copy,
927#endif
928 .mmap = snd_pcm_lib_mmap_iomem,
929};
930
Bill Pembertone23e7a12012-12-06 12:35:10 -0500931static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100932snd_nm256_pcm(struct nm256 *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Takashi Iwai10754f532005-11-17 15:01:29 +0100934 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 int i, err;
936
937 for (i = 0; i < 2; i++) {
Takashi Iwai10754f532005-11-17 15:01:29 +0100938 struct nm256_stream *s = &chip->streams[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 s->bufptr = chip->buffer + (s->buf - chip->buffer_start);
940 s->bufptr_addr = chip->buffer_addr + (s->buf - chip->buffer_start);
941 }
942
943 err = snd_pcm_new(chip->card, chip->card->driver, device,
944 1, 1, &pcm);
945 if (err < 0)
946 return err;
947
948 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_nm256_playback_ops);
949 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_nm256_capture_ops);
950
951 pcm->private_data = chip;
952 pcm->info_flags = 0;
953 chip->pcm = pcm;
954
955 return 0;
956}
957
958
959/*
960 * Initialize the hardware.
961 */
962static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100963snd_nm256_init_chip(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /* Reset everything. */
966 snd_nm256_writeb(chip, 0x0, 0x11);
967 snd_nm256_writew(chip, 0x214, 0);
968 /* stop sounds.. */
969 //snd_nm256_playback_stop(chip);
970 //snd_nm256_capture_stop(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
973
Takashi Iwai1204de32005-08-16 16:54:12 +0200974static irqreturn_t
Takashi Iwai10754f532005-11-17 15:01:29 +0100975snd_nm256_intr_check(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 if (chip->badintrcount++ > 1000) {
978 /*
979 * I'm not sure if the best thing is to stop the card from
980 * playing or just release the interrupt (after all, we're in
981 * a bad situation, so doing fancy stuff may not be such a good
982 * idea).
983 *
984 * I worry about the card engine continuing to play noise
985 * over and over, however--that could become a very
986 * obnoxious problem. And we know that when this usually
987 * happens things are fairly safe, it just means the user's
988 * inserted a PCMCIA card and someone's spamming us with IRQ 9s.
989 */
990 if (chip->streams[SNDRV_PCM_STREAM_PLAYBACK].running)
991 snd_nm256_playback_stop(chip);
992 if (chip->streams[SNDRV_PCM_STREAM_CAPTURE].running)
993 snd_nm256_capture_stop(chip);
994 chip->badintrcount = 0;
Takashi Iwai1204de32005-08-16 16:54:12 +0200995 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
Takashi Iwai1204de32005-08-16 16:54:12 +0200997 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
1000/*
1001 * Handle a potential interrupt for the device referred to by DEV_ID.
1002 *
1003 * I don't like the cut-n-paste job here either between the two routines,
1004 * but there are sufficient differences between the two interrupt handlers
1005 * that parameterizing it isn't all that great either. (Could use a macro,
1006 * I suppose...yucky bleah.)
1007 */
1008
1009static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001010snd_nm256_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Takashi Iwai10754f532005-11-17 15:01:29 +01001012 struct nm256 *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 u16 status;
1014 u8 cbyte;
1015
1016 status = snd_nm256_readw(chip, NM_INT_REG);
1017
1018 /* Not ours. */
Takashi Iwai1204de32005-08-16 16:54:12 +02001019 if (status == 0)
1020 return snd_nm256_intr_check(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 chip->badintrcount = 0;
1023
1024 /* Rather boring; check for individual interrupts and process them. */
1025
1026 spin_lock(&chip->reg_lock);
1027 if (status & NM_PLAYBACK_INT) {
1028 status &= ~NM_PLAYBACK_INT;
1029 NM_ACK_INT(chip, NM_PLAYBACK_INT);
1030 snd_nm256_playback_update(chip);
1031 }
1032
1033 if (status & NM_RECORD_INT) {
1034 status &= ~NM_RECORD_INT;
1035 NM_ACK_INT(chip, NM_RECORD_INT);
1036 snd_nm256_capture_update(chip);
1037 }
1038
1039 if (status & NM_MISC_INT_1) {
1040 status &= ~NM_MISC_INT_1;
1041 NM_ACK_INT(chip, NM_MISC_INT_1);
1042 snd_printd("NM256: Got misc interrupt #1\n");
1043 snd_nm256_writew(chip, NM_INT_REG, 0x8000);
1044 cbyte = snd_nm256_readb(chip, 0x400);
1045 snd_nm256_writeb(chip, 0x400, cbyte | 2);
1046 }
1047
1048 if (status & NM_MISC_INT_2) {
1049 status &= ~NM_MISC_INT_2;
1050 NM_ACK_INT(chip, NM_MISC_INT_2);
1051 snd_printd("NM256: Got misc interrupt #2\n");
1052 cbyte = snd_nm256_readb(chip, 0x400);
1053 snd_nm256_writeb(chip, 0x400, cbyte & ~2);
1054 }
1055
1056 /* Unknown interrupt. */
1057 if (status) {
1058 snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n",
1059 status);
1060 /* Pray. */
1061 NM_ACK_INT(chip, status);
1062 }
1063
1064 spin_unlock(&chip->reg_lock);
1065 return IRQ_HANDLED;
1066}
1067
1068/*
1069 * Handle a potential interrupt for the device referred to by DEV_ID.
1070 * This handler is for the 256ZX, and is very similar to the non-ZX
1071 * routine.
1072 */
1073
1074static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001075snd_nm256_interrupt_zx(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Takashi Iwai10754f532005-11-17 15:01:29 +01001077 struct nm256 *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 u32 status;
1079 u8 cbyte;
1080
1081 status = snd_nm256_readl(chip, NM_INT_REG);
1082
1083 /* Not ours. */
Takashi Iwai1204de32005-08-16 16:54:12 +02001084 if (status == 0)
1085 return snd_nm256_intr_check(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 chip->badintrcount = 0;
1088
1089 /* Rather boring; check for individual interrupts and process them. */
1090
1091 spin_lock(&chip->reg_lock);
1092 if (status & NM2_PLAYBACK_INT) {
1093 status &= ~NM2_PLAYBACK_INT;
1094 NM2_ACK_INT(chip, NM2_PLAYBACK_INT);
1095 snd_nm256_playback_update(chip);
1096 }
1097
1098 if (status & NM2_RECORD_INT) {
1099 status &= ~NM2_RECORD_INT;
1100 NM2_ACK_INT(chip, NM2_RECORD_INT);
1101 snd_nm256_capture_update(chip);
1102 }
1103
1104 if (status & NM2_MISC_INT_1) {
1105 status &= ~NM2_MISC_INT_1;
1106 NM2_ACK_INT(chip, NM2_MISC_INT_1);
1107 snd_printd("NM256: Got misc interrupt #1\n");
1108 cbyte = snd_nm256_readb(chip, 0x400);
1109 snd_nm256_writeb(chip, 0x400, cbyte | 2);
1110 }
1111
1112 if (status & NM2_MISC_INT_2) {
1113 status &= ~NM2_MISC_INT_2;
1114 NM2_ACK_INT(chip, NM2_MISC_INT_2);
1115 snd_printd("NM256: Got misc interrupt #2\n");
1116 cbyte = snd_nm256_readb(chip, 0x400);
1117 snd_nm256_writeb(chip, 0x400, cbyte & ~2);
1118 }
1119
1120 /* Unknown interrupt. */
1121 if (status) {
1122 snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n",
1123 status);
1124 /* Pray. */
1125 NM2_ACK_INT(chip, status);
1126 }
1127
1128 spin_unlock(&chip->reg_lock);
1129 return IRQ_HANDLED;
1130}
1131
1132/*
1133 * AC97 interface
1134 */
1135
1136/*
1137 * Waits for the mixer to become ready to be written; returns a zero value
1138 * if it timed out.
1139 */
1140static int
Takashi Iwai10754f532005-11-17 15:01:29 +01001141snd_nm256_ac97_ready(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 int timeout = 10;
1144 u32 testaddr;
1145 u16 testb;
1146
1147 testaddr = chip->mixer_status_offset;
1148 testb = chip->mixer_status_mask;
1149
1150 /*
1151 * Loop around waiting for the mixer to become ready.
1152 */
1153 while (timeout-- > 0) {
1154 if ((snd_nm256_readw(chip, testaddr) & testb) == 0)
1155 return 1;
1156 udelay(100);
1157 }
1158 return 0;
1159}
1160
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001161/*
1162 * Initial register values to be written to the AC97 mixer.
1163 * While most of these are identical to the reset values, we do this
1164 * so that we have most of the register contents cached--this avoids
1165 * reading from the mixer directly (which seems to be problematic,
1166 * probably due to ignorance).
1167 */
1168
1169struct initialValues {
1170 unsigned short reg;
1171 unsigned short value;
1172};
1173
1174static struct initialValues nm256_ac97_init_val[] =
1175{
1176 { AC97_MASTER, 0x8000 },
1177 { AC97_HEADPHONE, 0x8000 },
1178 { AC97_MASTER_MONO, 0x8000 },
1179 { AC97_PC_BEEP, 0x8000 },
1180 { AC97_PHONE, 0x8008 },
1181 { AC97_MIC, 0x8000 },
1182 { AC97_LINE, 0x8808 },
1183 { AC97_CD, 0x8808 },
1184 { AC97_VIDEO, 0x8808 },
1185 { AC97_AUX, 0x8808 },
1186 { AC97_PCM, 0x8808 },
1187 { AC97_REC_SEL, 0x0000 },
1188 { AC97_REC_GAIN, 0x0B0B },
1189 { AC97_GENERAL_PURPOSE, 0x0000 },
1190 { AC97_3D_CONTROL, 0x8000 },
1191 { AC97_VENDOR_ID1, 0x8384 },
1192 { AC97_VENDOR_ID2, 0x7609 },
1193};
1194
1195static int nm256_ac97_idx(unsigned short reg)
1196{
1197 int i;
1198 for (i = 0; i < ARRAY_SIZE(nm256_ac97_init_val); i++)
1199 if (nm256_ac97_init_val[i].reg == reg)
1200 return i;
1201 return -1;
1202}
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204/*
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001205 * some nm256 easily crash when reading from mixer registers
1206 * thus we're treating it as a write-only mixer and cache the
1207 * written values
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 */
1209static unsigned short
Takashi Iwai10754f532005-11-17 15:01:29 +01001210snd_nm256_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Takashi Iwai10754f532005-11-17 15:01:29 +01001212 struct nm256 *chip = ac97->private_data;
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001213 int idx = nm256_ac97_idx(reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001215 if (idx < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return 0;
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001217 return chip->ac97_regs[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
1220/*
1221 */
1222static void
Takashi Iwai10754f532005-11-17 15:01:29 +01001223snd_nm256_ac97_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 unsigned short reg, unsigned short val)
1225{
Takashi Iwai10754f532005-11-17 15:01:29 +01001226 struct nm256 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 int tries = 2;
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001228 int idx = nm256_ac97_idx(reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 u32 base;
1230
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001231 if (idx < 0)
1232 return;
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 base = chip->mixer_base;
1235
1236 snd_nm256_ac97_ready(chip);
1237
1238 /* Wait for the write to take, too. */
1239 while (tries-- > 0) {
1240 snd_nm256_writew(chip, base + reg, val);
1241 msleep(1); /* a little delay here seems better.. */
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001242 if (snd_nm256_ac97_ready(chip)) {
1243 /* successful write: set cache */
1244 chip->ac97_regs[idx] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return;
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248 snd_printd("nm256: ac97 codec not ready..\n");
1249}
1250
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001251/* static resolution table */
1252static struct snd_ac97_res_table nm256_res_table[] = {
1253 { AC97_MASTER, 0x1f1f },
1254 { AC97_HEADPHONE, 0x1f1f },
1255 { AC97_MASTER_MONO, 0x001f },
1256 { AC97_PC_BEEP, 0x001f },
1257 { AC97_PHONE, 0x001f },
1258 { AC97_MIC, 0x001f },
1259 { AC97_LINE, 0x1f1f },
1260 { AC97_CD, 0x1f1f },
1261 { AC97_VIDEO, 0x1f1f },
1262 { AC97_AUX, 0x1f1f },
1263 { AC97_PCM, 0x1f1f },
1264 { AC97_REC_GAIN, 0x0f0f },
1265 { } /* terminator */
1266};
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268/* initialize the ac97 into a known state */
1269static void
Takashi Iwai10754f532005-11-17 15:01:29 +01001270snd_nm256_ac97_reset(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Takashi Iwai10754f532005-11-17 15:01:29 +01001272 struct nm256 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 /* Reset the mixer. 'Tis magic! */
1275 snd_nm256_writeb(chip, 0x6c0, 1);
1276 if (! chip->reset_workaround) {
1277 /* Dell latitude LS will lock up by this */
1278 snd_nm256_writeb(chip, 0x6cc, 0x87);
1279 }
John W. Linville47530cf2005-10-19 16:03:10 +02001280 if (! chip->reset_workaround_2) {
1281 /* Dell latitude CSx will lock up by this */
1282 snd_nm256_writeb(chip, 0x6cc, 0x80);
1283 snd_nm256_writeb(chip, 0x6cc, 0x0);
1284 }
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001285 if (! chip->in_resume) {
1286 int i;
1287 for (i = 0; i < ARRAY_SIZE(nm256_ac97_init_val); i++) {
1288 /* preload the cache, so as to avoid even a single
1289 * read of the mixer regs
1290 */
1291 snd_nm256_ac97_write(ac97, nm256_ac97_init_val[i].reg,
1292 nm256_ac97_init_val[i].value);
1293 }
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
1297/* create an ac97 mixer interface */
Bill Pembertone23e7a12012-12-06 12:35:10 -05001298static int
Takashi Iwai10754f532005-11-17 15:01:29 +01001299snd_nm256_mixer(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Takashi Iwai10754f532005-11-17 15:01:29 +01001301 struct snd_ac97_bus *pbus;
1302 struct snd_ac97_template ac97;
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001303 int err;
Takashi Iwai10754f532005-11-17 15:01:29 +01001304 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 .reset = snd_nm256_ac97_reset,
1306 .write = snd_nm256_ac97_write,
1307 .read = snd_nm256_ac97_read,
1308 };
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001309
Milton Miller80ca9a72008-07-13 13:58:12 +02001310 chip->ac97_regs = kcalloc(ARRAY_SIZE(nm256_ac97_init_val),
1311 sizeof(short), GFP_KERNEL);
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001312 if (! chip->ac97_regs)
1313 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
1316 return err;
1317
1318 memset(&ac97, 0, sizeof(ac97));
1319 ac97.scaps = AC97_SCAP_AUDIO; /* we support audio! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 ac97.private_data = chip;
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001321 ac97.res_table = nm256_res_table;
Takashi Iwai1204de32005-08-16 16:54:12 +02001322 pbus->no_vra = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 err = snd_ac97_mixer(pbus, &ac97, &chip->ac97);
1324 if (err < 0)
1325 return err;
1326 if (! (chip->ac97->id & (0xf0000000))) {
1327 /* looks like an invalid id */
1328 sprintf(chip->card->mixername, "%s AC97", chip->card->driver);
1329 }
1330 return 0;
1331}
1332
1333/*
1334 * See if the signature left by the NM256 BIOS is intact; if so, we use
1335 * the associated address as the end of our audio buffer in the video
1336 * RAM.
1337 */
1338
Bill Pembertone23e7a12012-12-06 12:35:10 -05001339static int
Takashi Iwai10754f532005-11-17 15:01:29 +01001340snd_nm256_peek_for_sig(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 /* The signature is located 1K below the end of video RAM. */
1343 void __iomem *temp;
1344 /* Default buffer end is 5120 bytes below the top of RAM. */
1345 unsigned long pointer_found = chip->buffer_end - 0x1400;
1346 u32 sig;
1347
1348 temp = ioremap_nocache(chip->buffer_addr + chip->buffer_end - 0x400, 16);
1349 if (temp == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001350 snd_printk(KERN_ERR "Unable to scan for card signature in video RAM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return -EBUSY;
1352 }
1353
1354 sig = readl(temp);
1355 if ((sig & NM_SIG_MASK) == NM_SIGNATURE) {
1356 u32 pointer = readl(temp + 4);
1357
1358 /*
1359 * If it's obviously invalid, don't use it
1360 */
1361 if (pointer == 0xffffffff ||
1362 pointer < chip->buffer_size ||
1363 pointer > chip->buffer_end) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001364 snd_printk(KERN_ERR "invalid signature found: 0x%x\n", pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 iounmap(temp);
1366 return -ENODEV;
1367 } else {
1368 pointer_found = pointer;
Takashi Iwai10754f532005-11-17 15:01:29 +01001369 printk(KERN_INFO "nm256: found card signature in video RAM: 0x%x\n",
1370 pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372 }
1373
1374 iounmap(temp);
1375 chip->buffer_end = pointer_found;
1376
1377 return 0;
1378}
1379
Takashi Iwaic7561cd2012-08-14 18:12:04 +02001380#ifdef CONFIG_PM_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381/*
1382 * APM event handler, so the card is properly reinitialized after a power
1383 * event.
1384 */
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001385static int nm256_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001387 struct pci_dev *pci = to_pci_dev(dev);
1388 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001389 struct nm256 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001391 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 snd_pcm_suspend_all(chip->pcm);
1393 snd_ac97_suspend(chip->ac97);
1394 chip->coeffs_current = 0;
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001395 pci_disable_device(pci);
1396 pci_save_state(pci);
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001397 pci_set_power_state(pci, PCI_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return 0;
1399}
1400
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001401static int nm256_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001403 struct pci_dev *pci = to_pci_dev(dev);
1404 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001405 struct nm256 *chip = card->private_data;
Takashi Iwai1204de32005-08-16 16:54:12 +02001406 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 /* Perform a full reset on the hardware */
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001409 chip->in_resume = 1;
Takashi Iwai30b35392006-10-11 18:52:53 +02001410
1411 pci_set_power_state(pci, PCI_D0);
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001412 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02001413 if (pci_enable_device(pci) < 0) {
1414 printk(KERN_ERR "nm256: pci_enable_device failed, "
1415 "disabling device\n");
1416 snd_card_disconnect(card);
1417 return -EIO;
1418 }
1419 pci_set_master(pci);
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 snd_nm256_init_chip(chip);
1422
1423 /* restore ac97 */
1424 snd_ac97_resume(chip->ac97);
1425
Takashi Iwai1204de32005-08-16 16:54:12 +02001426 for (i = 0; i < 2; i++) {
Takashi Iwai10754f532005-11-17 15:01:29 +01001427 struct nm256_stream *s = &chip->streams[i];
Takashi Iwai1204de32005-08-16 16:54:12 +02001428 if (s->substream && s->suspended) {
1429 spin_lock_irq(&chip->reg_lock);
1430 snd_nm256_set_format(chip, s, s->substream);
1431 spin_unlock_irq(&chip->reg_lock);
1432 }
1433 }
1434
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001435 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001436 chip->in_resume = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return 0;
1438}
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001439
1440static SIMPLE_DEV_PM_OPS(nm256_pm, nm256_suspend, nm256_resume);
1441#define NM256_PM_OPS &nm256_pm
1442#else
1443#define NM256_PM_OPS NULL
Takashi Iwaic7561cd2012-08-14 18:12:04 +02001444#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Takashi Iwai10754f532005-11-17 15:01:29 +01001446static int snd_nm256_free(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 if (chip->streams[SNDRV_PCM_STREAM_PLAYBACK].running)
1449 snd_nm256_playback_stop(chip);
1450 if (chip->streams[SNDRV_PCM_STREAM_CAPTURE].running)
1451 snd_nm256_capture_stop(chip);
1452
1453 if (chip->irq >= 0)
Takashi Iwaiebf029d2008-04-22 17:28:11 +02001454 free_irq(chip->irq, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 if (chip->cport)
1457 iounmap(chip->cport);
1458 if (chip->buffer)
1459 iounmap(chip->buffer);
Takashi Iwaib1d57762005-10-10 11:56:31 +02001460 release_and_free_resource(chip->res_cport);
1461 release_and_free_resource(chip->res_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 pci_disable_device(chip->pci);
Florian Schlichtinga23446c2006-03-15 14:05:19 +01001464 kfree(chip->ac97_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 kfree(chip);
1466 return 0;
1467}
1468
Takashi Iwai10754f532005-11-17 15:01:29 +01001469static int snd_nm256_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Takashi Iwai10754f532005-11-17 15:01:29 +01001471 struct nm256 *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return snd_nm256_free(chip);
1473}
1474
Bill Pembertone23e7a12012-12-06 12:35:10 -05001475static int
Takashi Iwai10754f532005-11-17 15:01:29 +01001476snd_nm256_create(struct snd_card *card, struct pci_dev *pci,
1477 struct nm256 **chip_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Takashi Iwai10754f532005-11-17 15:01:29 +01001479 struct nm256 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 int err, pval;
Takashi Iwai10754f532005-11-17 15:01:29 +01001481 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 .dev_free = snd_nm256_dev_free,
1483 };
1484 u32 addr;
1485
1486 *chip_ret = NULL;
1487
1488 if ((err = pci_enable_device(pci)) < 0)
1489 return err;
1490
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001491 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (chip == NULL) {
1493 pci_disable_device(pci);
1494 return -ENOMEM;
1495 }
1496
1497 chip->card = card;
1498 chip->pci = pci;
Takashi Iwai3f05f862005-11-17 11:12:06 +01001499 chip->use_cache = use_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 spin_lock_init(&chip->reg_lock);
1501 chip->irq = -1;
Ingo Molnar62932df2006-01-16 16:34:20 +01001502 mutex_init(&chip->irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Takashi Iwai3f05f862005-11-17 11:12:06 +01001504 /* store buffer sizes in bytes */
1505 chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize = playback_bufsize * 1024;
1506 chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize = capture_bufsize * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 /*
1509 * The NM256 has two memory ports. The first port is nothing
1510 * more than a chunk of video RAM, which is used as the I/O ring
1511 * buffer. The second port has the actual juicy stuff (like the
1512 * mixer and the playback engine control registers).
1513 */
1514
1515 chip->buffer_addr = pci_resource_start(pci, 0);
1516 chip->cport_addr = pci_resource_start(pci, 1);
1517
1518 /* Init the memory port info. */
1519 /* remap control port (#2) */
1520 chip->res_cport = request_mem_region(chip->cport_addr, NM_PORT2_SIZE,
1521 card->driver);
1522 if (chip->res_cport == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001523 snd_printk(KERN_ERR "memory region 0x%lx (size 0x%x) busy\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 chip->cport_addr, NM_PORT2_SIZE);
1525 err = -EBUSY;
1526 goto __error;
1527 }
1528 chip->cport = ioremap_nocache(chip->cport_addr, NM_PORT2_SIZE);
1529 if (chip->cport == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001530 snd_printk(KERN_ERR "unable to map control port %lx\n", chip->cport_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 err = -ENOMEM;
1532 goto __error;
1533 }
1534
1535 if (!strcmp(card->driver, "NM256AV")) {
1536 /* Ok, try to see if this is a non-AC97 version of the hardware. */
1537 pval = snd_nm256_readw(chip, NM_MIXER_PRESENCE);
1538 if ((pval & NM_PRESENCE_MASK) != NM_PRESENCE_VALUE) {
Takashi Iwai3f05f862005-11-17 11:12:06 +01001539 if (! force_ac97) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 printk(KERN_ERR "nm256: no ac97 is found!\n");
Takashi Iwai10754f532005-11-17 15:01:29 +01001541 printk(KERN_ERR " force the driver to load by "
1542 "passing in the module parameter\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 printk(KERN_ERR " force_ac97=1\n");
Paul Vojta42108612007-07-04 10:35:29 +02001544 printk(KERN_ERR " or try sb16, opl3sa2, or "
1545 "cs423x drivers instead.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 err = -ENXIO;
1547 goto __error;
1548 }
1549 }
1550 chip->buffer_end = 2560 * 1024;
1551 chip->interrupt = snd_nm256_interrupt;
1552 chip->mixer_status_offset = NM_MIXER_STATUS_OFFSET;
1553 chip->mixer_status_mask = NM_MIXER_READY_MASK;
1554 } else {
1555 /* Not sure if there is any relevant detect for the ZX or not. */
1556 if (snd_nm256_readb(chip, 0xa0b) != 0)
1557 chip->buffer_end = 6144 * 1024;
1558 else
1559 chip->buffer_end = 4096 * 1024;
1560
1561 chip->interrupt = snd_nm256_interrupt_zx;
1562 chip->mixer_status_offset = NM2_MIXER_STATUS_OFFSET;
1563 chip->mixer_status_mask = NM2_MIXER_READY_MASK;
1564 }
1565
Takashi Iwai10754f532005-11-17 15:01:29 +01001566 chip->buffer_size = chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize +
1567 chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (chip->use_cache)
1569 chip->buffer_size += NM_TOTAL_COEFF_COUNT * 4;
1570 else
1571 chip->buffer_size += NM_MAX_PLAYBACK_COEF_SIZE + NM_MAX_RECORD_COEF_SIZE;
1572
Takashi Iwai3f05f862005-11-17 11:12:06 +01001573 if (buffer_top >= chip->buffer_size && buffer_top < chip->buffer_end)
1574 chip->buffer_end = buffer_top;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 else {
1576 /* get buffer end pointer from signature */
1577 if ((err = snd_nm256_peek_for_sig(chip)) < 0)
1578 goto __error;
1579 }
1580
1581 chip->buffer_start = chip->buffer_end - chip->buffer_size;
1582 chip->buffer_addr += chip->buffer_start;
1583
1584 printk(KERN_INFO "nm256: Mapping port 1 from 0x%x - 0x%x\n",
1585 chip->buffer_start, chip->buffer_end);
1586
1587 chip->res_buffer = request_mem_region(chip->buffer_addr,
1588 chip->buffer_size,
1589 card->driver);
1590 if (chip->res_buffer == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001591 snd_printk(KERN_ERR "nm256: buffer 0x%lx (size 0x%x) busy\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 chip->buffer_addr, chip->buffer_size);
1593 err = -EBUSY;
1594 goto __error;
1595 }
1596 chip->buffer = ioremap_nocache(chip->buffer_addr, chip->buffer_size);
1597 if (chip->buffer == NULL) {
1598 err = -ENOMEM;
Takashi Iwai99b359b2005-10-20 18:26:44 +02001599 snd_printk(KERN_ERR "unable to map ring buffer at %lx\n", chip->buffer_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 goto __error;
1601 }
1602
1603 /* set offsets */
1604 addr = chip->buffer_start;
1605 chip->streams[SNDRV_PCM_STREAM_PLAYBACK].buf = addr;
1606 addr += chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize;
1607 chip->streams[SNDRV_PCM_STREAM_CAPTURE].buf = addr;
1608 addr += chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize;
1609 if (chip->use_cache) {
1610 chip->all_coeff_buf = addr;
1611 } else {
1612 chip->coeff_buf[SNDRV_PCM_STREAM_PLAYBACK] = addr;
1613 addr += NM_MAX_PLAYBACK_COEF_SIZE;
1614 chip->coeff_buf[SNDRV_PCM_STREAM_CAPTURE] = addr;
1615 }
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 /* Fixed setting. */
1618 chip->mixer_base = NM_MIXER_OFFSET;
1619
1620 chip->coeffs_current = 0;
1621
1622 snd_nm256_init_chip(chip);
1623
1624 // pci_set_master(pci); /* needed? */
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1627 goto __error;
1628
1629 snd_card_set_dev(card, &pci->dev);
1630
1631 *chip_ret = chip;
1632 return 0;
1633
1634__error:
1635 snd_nm256_free(chip);
1636 return err;
1637}
1638
1639
John W. Linville47530cf2005-10-19 16:03:10 +02001640enum { NM_BLACKLISTED, NM_RESET_WORKAROUND, NM_RESET_WORKAROUND_2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Bill Pembertone23e7a12012-12-06 12:35:10 -05001642static struct snd_pci_quirk nm256_quirks[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 /* HP omnibook 4150 has cs4232 codec internally */
Takashi Iwaie2b6d132006-11-24 15:36:13 +01001644 SND_PCI_QUIRK(0x103c, 0x0007, "HP omnibook 4150", NM_BLACKLISTED),
1645 /* Reset workarounds to avoid lock-ups */
1646 SND_PCI_QUIRK(0x104d, 0x8041, "Sony PCG-F305", NM_RESET_WORKAROUND),
1647 SND_PCI_QUIRK(0x1028, 0x0080, "Dell Latitude LS", NM_RESET_WORKAROUND),
1648 SND_PCI_QUIRK(0x1028, 0x0091, "Dell Latitude CSx", NM_RESET_WORKAROUND_2),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 { } /* terminator */
1650};
1651
1652
Bill Pembertone23e7a12012-12-06 12:35:10 -05001653static int snd_nm256_probe(struct pci_dev *pci,
1654 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Takashi Iwai10754f532005-11-17 15:01:29 +01001656 struct snd_card *card;
1657 struct nm256 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 int err;
Takashi Iwaie2b6d132006-11-24 15:36:13 +01001659 const struct snd_pci_quirk *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Takashi Iwaie2b6d132006-11-24 15:36:13 +01001661 q = snd_pci_quirk_lookup(pci, nm256_quirks);
1662 if (q) {
Takashi Iwai86b27232013-01-25 10:54:07 +01001663 snd_printdd(KERN_INFO "nm256: Enabled quirk for %s.\n",
1664 snd_pci_quirk_name(q));
Takashi Iwaie2b6d132006-11-24 15:36:13 +01001665 switch (q->value) {
1666 case NM_BLACKLISTED:
1667 printk(KERN_INFO "nm256: The device is blacklisted. "
1668 "Loading stopped\n");
1669 return -ENODEV;
1670 case NM_RESET_WORKAROUND_2:
1671 reset_workaround_2 = 1;
1672 /* Fall-through */
1673 case NM_RESET_WORKAROUND:
1674 reset_workaround = 1;
1675 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
1677 }
1678
Takashi Iwaie58de7b2008-12-28 16:44:30 +01001679 err = snd_card_create(index, id, THIS_MODULE, 0, &card);
1680 if (err < 0)
1681 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 switch (pci->device) {
1684 case PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO:
1685 strcpy(card->driver, "NM256AV");
1686 break;
1687 case PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO:
1688 strcpy(card->driver, "NM256ZX");
1689 break;
1690 case PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO:
1691 strcpy(card->driver, "NM256XL+");
1692 break;
1693 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001694 snd_printk(KERN_ERR "invalid device id 0x%x\n", pci->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 snd_card_free(card);
1696 return -EINVAL;
1697 }
1698
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +02001699 if (vaio_hack)
Takashi Iwai3f05f862005-11-17 11:12:06 +01001700 buffer_top = 0x25a800; /* this avoids conflicts with XFree86 server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +02001702 if (playback_bufsize < 4)
1703 playback_bufsize = 4;
1704 if (playback_bufsize > 128)
1705 playback_bufsize = 128;
1706 if (capture_bufsize < 4)
1707 capture_bufsize = 4;
1708 if (capture_bufsize > 128)
1709 capture_bufsize = 128;
Takashi Iwai3f05f862005-11-17 11:12:06 +01001710 if ((err = snd_nm256_create(card, pci, &chip)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 snd_card_free(card);
1712 return err;
1713 }
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001714 card->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +02001716 if (reset_workaround) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 snd_printdd(KERN_INFO "nm256: reset_workaround activated\n");
1718 chip->reset_workaround = 1;
1719 }
1720
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +02001721 if (reset_workaround_2) {
John W. Linville47530cf2005-10-19 16:03:10 +02001722 snd_printdd(KERN_INFO "nm256: reset_workaround_2 activated\n");
1723 chip->reset_workaround_2 = 1;
1724 }
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if ((err = snd_nm256_pcm(chip, 0)) < 0 ||
1727 (err = snd_nm256_mixer(chip)) < 0) {
1728 snd_card_free(card);
1729 return err;
1730 }
1731
1732 sprintf(card->shortname, "NeoMagic %s", card->driver);
1733 sprintf(card->longname, "%s at 0x%lx & 0x%lx, irq %d",
1734 card->shortname,
1735 chip->buffer_addr, chip->cport_addr, chip->irq);
1736
1737 if ((err = snd_card_register(card)) < 0) {
1738 snd_card_free(card);
1739 return err;
1740 }
1741
1742 pci_set_drvdata(pci, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return 0;
1744}
1745
Bill Pembertone23e7a12012-12-06 12:35:10 -05001746static void snd_nm256_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
1748 snd_card_free(pci_get_drvdata(pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749}
1750
1751
Takashi Iwaie9f66d92012-04-24 12:25:00 +02001752static struct pci_driver nm256_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02001753 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 .id_table = snd_nm256_ids,
1755 .probe = snd_nm256_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05001756 .remove = snd_nm256_remove,
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001757 .driver = {
1758 .pm = NM256_PM_OPS,
1759 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760};
1761
Takashi Iwaie9f66d92012-04-24 12:25:00 +02001762module_pci_driver(nm256_driver);