1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for NeoMagic 256AV and 256ZX chipsets.
4 * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>
6 * Based on nm256_audio.c OSS driver in linux kernel.
7 * The original author of OSS nm256 driver wishes to remain anonymous,
8 * so I just put my acknoledgment to him/her here.
9 * The original author's web page is found at
10 * http://www.uglx.org/sony.html
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/init.h>
17 #include <linux/pci.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
22 #include <sound/core.h>
23 #include <sound/info.h>
24 #include <sound/control.h>
25 #include <sound/pcm.h>
26 #include <sound/ac97_codec.h>
27 #include <sound/initval.h>
29 #define CARD_NAME "NeoMagic 256AV/ZX"
30 #define DRIVER_NAME "NM256"
32 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
33 MODULE_DESCRIPTION("NeoMagic NM256AV/ZX");
34 MODULE_LICENSE("GPL");
35 MODULE_SUPPORTED_DEVICE("{{NeoMagic,NM256AV},"
36 "{NeoMagic,NM256ZX}}");
39 * some compile conditions.
42 static int index
= SNDRV_DEFAULT_IDX1
; /* Index */
43 static char *id
= SNDRV_DEFAULT_STR1
; /* ID for this card */
44 static int playback_bufsize
= 16;
45 static int capture_bufsize
= 16;
46 static bool force_ac97
; /* disabled as default */
47 static int buffer_top
; /* not specified */
48 static bool use_cache
; /* disabled */
49 static bool vaio_hack
; /* disabled */
50 static bool reset_workaround
;
51 static bool reset_workaround_2
;
53 module_param(index
, int, 0444);
54 MODULE_PARM_DESC(index
, "Index value for " CARD_NAME
" soundcard.");
55 module_param(id
, charp
, 0444);
56 MODULE_PARM_DESC(id
, "ID string for " CARD_NAME
" soundcard.");
57 module_param(playback_bufsize
, int, 0444);
58 MODULE_PARM_DESC(playback_bufsize
, "DAC frame size in kB for " CARD_NAME
" soundcard.");
59 module_param(capture_bufsize
, int, 0444);
60 MODULE_PARM_DESC(capture_bufsize
, "ADC frame size in kB for " CARD_NAME
" soundcard.");
61 module_param(force_ac97
, bool, 0444);
62 MODULE_PARM_DESC(force_ac97
, "Force to use AC97 codec for " CARD_NAME
" soundcard.");
63 module_param(buffer_top
, int, 0444);
64 MODULE_PARM_DESC(buffer_top
, "Set the top address of audio buffer for " CARD_NAME
" soundcard.");
65 module_param(use_cache
, bool, 0444);
66 MODULE_PARM_DESC(use_cache
, "Enable the cache for coefficient table access.");
67 module_param(vaio_hack
, bool, 0444);
68 MODULE_PARM_DESC(vaio_hack
, "Enable workaround for Sony VAIO notebooks.");
69 module_param(reset_workaround
, bool, 0444);
70 MODULE_PARM_DESC(reset_workaround
, "Enable AC97 RESET workaround for some laptops.");
71 module_param(reset_workaround_2
, bool, 0444);
72 MODULE_PARM_DESC(reset_workaround_2
, "Enable extended AC97 RESET workaround for some other laptops.");
74 /* just for backward compatibility */
76 module_param(enable
, bool, 0444);
84 /* The BIOS signature. */
85 #define NM_SIGNATURE 0x4e4d0000
87 #define NM_SIG_MASK 0xffff0000
89 /* Size of the second memory area. */
90 #define NM_PORT2_SIZE 4096
92 /* The base offset of the mixer in the second memory area. */
93 #define NM_MIXER_OFFSET 0x600
95 /* The maximum size of a coefficient entry. */
96 #define NM_MAX_PLAYBACK_COEF_SIZE 0x5000
97 #define NM_MAX_RECORD_COEF_SIZE 0x1260
99 /* The interrupt register. */
100 #define NM_INT_REG 0xa04
102 #define NM_PLAYBACK_INT 0x40
103 #define NM_RECORD_INT 0x100
104 #define NM_MISC_INT_1 0x4000
105 #define NM_MISC_INT_2 0x1
106 #define NM_ACK_INT(chip, X) snd_nm256_writew(chip, NM_INT_REG, (X) << 1)
108 /* The AV's "mixer ready" status bit and location. */
109 #define NM_MIXER_STATUS_OFFSET 0xa04
110 #define NM_MIXER_READY_MASK 0x0800
111 #define NM_MIXER_PRESENCE 0xa06
112 #define NM_PRESENCE_MASK 0x0050
113 #define NM_PRESENCE_VALUE 0x0040
116 * For the ZX. It uses the same interrupt register, but it holds 32
117 * bits instead of 16.
119 #define NM2_PLAYBACK_INT 0x10000
120 #define NM2_RECORD_INT 0x80000
121 #define NM2_MISC_INT_1 0x8
122 #define NM2_MISC_INT_2 0x2
123 #define NM2_ACK_INT(chip, X) snd_nm256_writel(chip, NM_INT_REG, (X))
125 /* The ZX's "mixer ready" status bit and location. */
126 #define NM2_MIXER_STATUS_OFFSET 0xa06
127 #define NM2_MIXER_READY_MASK 0x0800
129 /* The playback registers start from here. */
130 #define NM_PLAYBACK_REG_OFFSET 0x0
131 /* The record registers start from here. */
132 #define NM_RECORD_REG_OFFSET 0x200
134 /* The rate register is located 2 bytes from the start of the register area. */
135 #define NM_RATE_REG_OFFSET 2
137 /* Mono/stereo flag, number of bits on playback, and rate mask. */
138 #define NM_RATE_STEREO 1
139 #define NM_RATE_BITS_16 2
140 #define NM_RATE_MASK 0xf0
142 /* Playback enable register. */
143 #define NM_PLAYBACK_ENABLE_REG (NM_PLAYBACK_REG_OFFSET + 0x1)
144 #define NM_PLAYBACK_ENABLE_FLAG 1
145 #define NM_PLAYBACK_ONESHOT 2
146 #define NM_PLAYBACK_FREERUN 4
148 /* Mutes the audio output. */
149 #define NM_AUDIO_MUTE_REG (NM_PLAYBACK_REG_OFFSET + 0x18)
150 #define NM_AUDIO_MUTE_LEFT 0x8000
151 #define NM_AUDIO_MUTE_RIGHT 0x0080
153 /* Recording enable register. */
154 #define NM_RECORD_ENABLE_REG (NM_RECORD_REG_OFFSET + 0)
155 #define NM_RECORD_ENABLE_FLAG 1
156 #define NM_RECORD_FREERUN 2
158 /* coefficient buffer pointer */
159 #define NM_COEFF_START_OFFSET 0x1c
160 #define NM_COEFF_END_OFFSET 0x20
162 /* DMA buffer offsets */
163 #define NM_RBUFFER_START (NM_RECORD_REG_OFFSET + 0x4)
164 #define NM_RBUFFER_END (NM_RECORD_REG_OFFSET + 0x10)
165 #define NM_RBUFFER_WMARK (NM_RECORD_REG_OFFSET + 0xc)
166 #define NM_RBUFFER_CURRP (NM_RECORD_REG_OFFSET + 0x8)
168 #define NM_PBUFFER_START (NM_PLAYBACK_REG_OFFSET + 0x4)
169 #define NM_PBUFFER_END (NM_PLAYBACK_REG_OFFSET + 0x14)
170 #define NM_PBUFFER_WMARK (NM_PLAYBACK_REG_OFFSET + 0xc)
171 #define NM_PBUFFER_CURRP (NM_PLAYBACK_REG_OFFSET + 0x8)
173 struct nm256_stream
{
176 struct snd_pcm_substream
*substream
;
180 u32 buf
; /* offset from chip->buffer */
181 int bufsize
; /* buffer size in bytes */
182 void __iomem
*bufptr
; /* mapped pointer */
183 unsigned long bufptr_addr
; /* physical address of the mapped pointer */
185 int dma_size
; /* buffer size of the substream in bytes */
186 int period_size
; /* period size in bytes */
187 int periods
; /* # of periods */
188 int shift
; /* bit shifts */
189 int cur_period
; /* current period # */
195 struct snd_card
*card
;
197 void __iomem
*cport
; /* control port */
198 struct resource
*res_cport
; /* its resource */
199 unsigned long cport_addr
; /* physical address */
201 void __iomem
*buffer
; /* buffer */
202 struct resource
*res_buffer
; /* its resource */
203 unsigned long buffer_addr
; /* buffer phyiscal address */
205 u32 buffer_start
; /* start offset from pci resource 0 */
206 u32 buffer_end
; /* end offset */
207 u32 buffer_size
; /* total buffer size */
209 u32 all_coeff_buf
; /* coefficient buffer */
210 u32 coeff_buf
[2]; /* coefficient buffer for each stream */
212 unsigned int coeffs_current
: 1; /* coeff. table is loaded? */
213 unsigned int use_cache
: 1; /* use one big coef. table */
214 unsigned int reset_workaround
: 1; /* Workaround for some laptops to avoid freeze */
215 unsigned int reset_workaround_2
: 1; /* Extended workaround for some other laptops to avoid freeze */
216 unsigned int in_resume
: 1;
218 int mixer_base
; /* register offset of ac97 mixer */
219 int mixer_status_offset
; /* offset of mixer status reg. */
220 int mixer_status_mask
; /* bit mask to test the mixer status */
224 irq_handler_t interrupt
;
225 int badintrcount
; /* counter to check bogus interrupts */
226 struct mutex irq_mutex
;
228 struct nm256_stream streams
[2];
230 struct snd_ac97
*ac97
;
231 unsigned short *ac97_regs
; /* register caches, only for valid regs */
243 * include coefficient table
245 #include "nm256_coef.c"
251 static const struct pci_device_id snd_nm256_ids
[] = {
252 {PCI_VDEVICE(NEOMAGIC
, PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO
), 0},
253 {PCI_VDEVICE(NEOMAGIC
, PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO
), 0},
254 {PCI_VDEVICE(NEOMAGIC
, PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO
), 0},
258 MODULE_DEVICE_TABLE(pci
, snd_nm256_ids
);
266 snd_nm256_readb(struct nm256
*chip
, int offset
)
268 return readb(chip
->cport
+ offset
);
272 snd_nm256_readw(struct nm256
*chip
, int offset
)
274 return readw(chip
->cport
+ offset
);
278 snd_nm256_readl(struct nm256
*chip
, int offset
)
280 return readl(chip
->cport
+ offset
);
284 snd_nm256_writeb(struct nm256
*chip
, int offset
, u8 val
)
286 writeb(val
, chip
->cport
+ offset
);
290 snd_nm256_writew(struct nm256
*chip
, int offset
, u16 val
)
292 writew(val
, chip
->cport
+ offset
);
296 snd_nm256_writel(struct nm256
*chip
, int offset
, u32 val
)
298 writel(val
, chip
->cport
+ offset
);
302 snd_nm256_write_buffer(struct nm256
*chip
, void *src
, int offset
, int size
)
304 offset
-= chip
->buffer_start
;
305 #ifdef CONFIG_SND_DEBUG
306 if (offset
< 0 || offset
>= chip
->buffer_size
) {
307 dev_err(chip
->card
->dev
,
308 "write_buffer invalid offset = %d size = %d\n",
313 memcpy_toio(chip
->buffer
+ offset
, src
, size
);
317 * coefficient handlers -- what a magic!
321 snd_nm256_get_start_offset(int which
)
325 offset
+= coefficient_sizes
[which
];
330 snd_nm256_load_one_coefficient(struct nm256
*chip
, int stream
, u32 port
, int which
)
332 u32 coeff_buf
= chip
->coeff_buf
[stream
];
333 u16 offset
= snd_nm256_get_start_offset(which
);
334 u16 size
= coefficient_sizes
[which
];
336 snd_nm256_write_buffer(chip
, coefficients
+ offset
, coeff_buf
, size
);
337 snd_nm256_writel(chip
, port
, coeff_buf
);
338 /* ??? Record seems to behave differently than playback. */
339 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
341 snd_nm256_writel(chip
, port
+ 4, coeff_buf
+ size
);
345 snd_nm256_load_coefficient(struct nm256
*chip
, int stream
, int number
)
347 /* The enable register for the specified engine. */
348 u32 poffset
= (stream
== SNDRV_PCM_STREAM_CAPTURE
?
349 NM_RECORD_ENABLE_REG
: NM_PLAYBACK_ENABLE_REG
);
350 u32 addr
= NM_COEFF_START_OFFSET
;
352 addr
+= (stream
== SNDRV_PCM_STREAM_CAPTURE
?
353 NM_RECORD_REG_OFFSET
: NM_PLAYBACK_REG_OFFSET
);
355 if (snd_nm256_readb(chip
, poffset
) & 1) {
356 dev_dbg(chip
->card
->dev
,
357 "NM256: Engine was enabled while loading coefficients!\n");
361 /* The recording engine uses coefficient values 8-15. */
363 if (stream
== SNDRV_PCM_STREAM_CAPTURE
)
366 if (! chip
->use_cache
) {
367 snd_nm256_load_one_coefficient(chip
, stream
, addr
, number
);
370 if (! chip
->coeffs_current
) {
371 snd_nm256_write_buffer(chip
, coefficients
, chip
->all_coeff_buf
,
372 NM_TOTAL_COEFF_COUNT
* 4);
373 chip
->coeffs_current
= 1;
375 u32 base
= chip
->all_coeff_buf
;
376 u32 offset
= snd_nm256_get_start_offset(number
);
377 u32 end_offset
= offset
+ coefficient_sizes
[number
];
378 snd_nm256_writel(chip
, addr
, base
+ offset
);
379 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
381 snd_nm256_writel(chip
, addr
+ 4, base
+ end_offset
);
386 /* The actual rates supported by the card. */
387 static const unsigned int samplerates
[8] = {
388 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000,
390 static const struct snd_pcm_hw_constraint_list constraints_rates
= {
391 .count
= ARRAY_SIZE(samplerates
),
397 * return the index of the target rate
400 snd_nm256_fixed_rate(unsigned int rate
)
403 for (i
= 0; i
< ARRAY_SIZE(samplerates
); i
++) {
404 if (rate
== samplerates
[i
])
412 * set sample rate and format
415 snd_nm256_set_format(struct nm256
*chip
, struct nm256_stream
*s
,
416 struct snd_pcm_substream
*substream
)
418 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
419 int rate_index
= snd_nm256_fixed_rate(runtime
->rate
);
420 unsigned char ratebits
= (rate_index
<< 4) & NM_RATE_MASK
;
423 if (snd_pcm_format_width(runtime
->format
) == 16) {
424 ratebits
|= NM_RATE_BITS_16
;
427 if (runtime
->channels
> 1) {
428 ratebits
|= NM_RATE_STEREO
;
432 runtime
->rate
= samplerates
[rate_index
];
434 switch (substream
->stream
) {
435 case SNDRV_PCM_STREAM_PLAYBACK
:
436 snd_nm256_load_coefficient(chip
, 0, rate_index
); /* 0 = playback */
437 snd_nm256_writeb(chip
,
438 NM_PLAYBACK_REG_OFFSET
+ NM_RATE_REG_OFFSET
,
441 case SNDRV_PCM_STREAM_CAPTURE
:
442 snd_nm256_load_coefficient(chip
, 1, rate_index
); /* 1 = record */
443 snd_nm256_writeb(chip
,
444 NM_RECORD_REG_OFFSET
+ NM_RATE_REG_OFFSET
,
450 /* acquire interrupt */
451 static int snd_nm256_acquire_irq(struct nm256
*chip
)
453 mutex_lock(&chip
->irq_mutex
);
455 if (request_irq(chip
->pci
->irq
, chip
->interrupt
, IRQF_SHARED
,
456 KBUILD_MODNAME
, chip
)) {
457 dev_err(chip
->card
->dev
,
458 "unable to grab IRQ %d\n", chip
->pci
->irq
);
459 mutex_unlock(&chip
->irq_mutex
);
462 chip
->irq
= chip
->pci
->irq
;
465 mutex_unlock(&chip
->irq_mutex
);
469 /* release interrupt */
470 static void snd_nm256_release_irq(struct nm256
*chip
)
472 mutex_lock(&chip
->irq_mutex
);
473 if (chip
->irq_acks
> 0)
475 if (chip
->irq_acks
== 0 && chip
->irq
>= 0) {
476 free_irq(chip
->irq
, chip
);
479 mutex_unlock(&chip
->irq_mutex
);
486 /* update the watermark (current period) */
487 static void snd_nm256_pcm_mark(struct nm256
*chip
, struct nm256_stream
*s
, int reg
)
490 s
->cur_period
%= s
->periods
;
491 snd_nm256_writel(chip
, reg
, s
->buf
+ s
->cur_period
* s
->period_size
);
494 #define snd_nm256_playback_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_PBUFFER_WMARK)
495 #define snd_nm256_capture_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_RBUFFER_WMARK)
498 snd_nm256_playback_start(struct nm256
*chip
, struct nm256_stream
*s
,
499 struct snd_pcm_substream
*substream
)
501 /* program buffer pointers */
502 snd_nm256_writel(chip
, NM_PBUFFER_START
, s
->buf
);
503 snd_nm256_writel(chip
, NM_PBUFFER_END
, s
->buf
+ s
->dma_size
- (1 << s
->shift
));
504 snd_nm256_writel(chip
, NM_PBUFFER_CURRP
, s
->buf
);
505 snd_nm256_playback_mark(chip
, s
);
507 /* Enable playback engine and interrupts. */
508 snd_nm256_writeb(chip
, NM_PLAYBACK_ENABLE_REG
,
509 NM_PLAYBACK_ENABLE_FLAG
| NM_PLAYBACK_FREERUN
);
510 /* Enable both channels. */
511 snd_nm256_writew(chip
, NM_AUDIO_MUTE_REG
, 0x0);
515 snd_nm256_capture_start(struct nm256
*chip
, struct nm256_stream
*s
,
516 struct snd_pcm_substream
*substream
)
518 /* program buffer pointers */
519 snd_nm256_writel(chip
, NM_RBUFFER_START
, s
->buf
);
520 snd_nm256_writel(chip
, NM_RBUFFER_END
, s
->buf
+ s
->dma_size
);
521 snd_nm256_writel(chip
, NM_RBUFFER_CURRP
, s
->buf
);
522 snd_nm256_capture_mark(chip
, s
);
524 /* Enable playback engine and interrupts. */
525 snd_nm256_writeb(chip
, NM_RECORD_ENABLE_REG
,
526 NM_RECORD_ENABLE_FLAG
| NM_RECORD_FREERUN
);
529 /* Stop the play engine. */
531 snd_nm256_playback_stop(struct nm256
*chip
)
533 /* Shut off sound from both channels. */
534 snd_nm256_writew(chip
, NM_AUDIO_MUTE_REG
,
535 NM_AUDIO_MUTE_LEFT
| NM_AUDIO_MUTE_RIGHT
);
536 /* Disable play engine. */
537 snd_nm256_writeb(chip
, NM_PLAYBACK_ENABLE_REG
, 0);
541 snd_nm256_capture_stop(struct nm256
*chip
)
543 /* Disable recording engine. */
544 snd_nm256_writeb(chip
, NM_RECORD_ENABLE_REG
, 0);
548 snd_nm256_playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
550 struct nm256
*chip
= snd_pcm_substream_chip(substream
);
551 struct nm256_stream
*s
= substream
->runtime
->private_data
;
557 spin_lock(&chip
->reg_lock
);
559 case SNDRV_PCM_TRIGGER_RESUME
:
562 case SNDRV_PCM_TRIGGER_START
:
564 snd_nm256_playback_start(chip
, s
, substream
);
568 case SNDRV_PCM_TRIGGER_SUSPEND
:
571 case SNDRV_PCM_TRIGGER_STOP
:
573 snd_nm256_playback_stop(chip
);
581 spin_unlock(&chip
->reg_lock
);
586 snd_nm256_capture_trigger(struct snd_pcm_substream
*substream
, int cmd
)
588 struct nm256
*chip
= snd_pcm_substream_chip(substream
);
589 struct nm256_stream
*s
= substream
->runtime
->private_data
;
595 spin_lock(&chip
->reg_lock
);
597 case SNDRV_PCM_TRIGGER_START
:
598 case SNDRV_PCM_TRIGGER_RESUME
:
600 snd_nm256_capture_start(chip
, s
, substream
);
604 case SNDRV_PCM_TRIGGER_STOP
:
605 case SNDRV_PCM_TRIGGER_SUSPEND
:
607 snd_nm256_capture_stop(chip
);
615 spin_unlock(&chip
->reg_lock
);
621 * prepare playback/capture channel
623 static int snd_nm256_pcm_prepare(struct snd_pcm_substream
*substream
)
625 struct nm256
*chip
= snd_pcm_substream_chip(substream
);
626 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
627 struct nm256_stream
*s
= runtime
->private_data
;
631 s
->dma_size
= frames_to_bytes(runtime
, substream
->runtime
->buffer_size
);
632 s
->period_size
= frames_to_bytes(runtime
, substream
->runtime
->period_size
);
633 s
->periods
= substream
->runtime
->periods
;
636 spin_lock_irq(&chip
->reg_lock
);
638 snd_nm256_set_format(chip
, s
, substream
);
639 spin_unlock_irq(&chip
->reg_lock
);
646 * get the current pointer
648 static snd_pcm_uframes_t
649 snd_nm256_playback_pointer(struct snd_pcm_substream
*substream
)
651 struct nm256
*chip
= snd_pcm_substream_chip(substream
);
652 struct nm256_stream
*s
= substream
->runtime
->private_data
;
657 curp
= snd_nm256_readl(chip
, NM_PBUFFER_CURRP
) - (unsigned long)s
->buf
;
659 return bytes_to_frames(substream
->runtime
, curp
);
662 static snd_pcm_uframes_t
663 snd_nm256_capture_pointer(struct snd_pcm_substream
*substream
)
665 struct nm256
*chip
= snd_pcm_substream_chip(substream
);
666 struct nm256_stream
*s
= substream
->runtime
->private_data
;
671 curp
= snd_nm256_readl(chip
, NM_RBUFFER_CURRP
) - (unsigned long)s
->buf
;
673 return bytes_to_frames(substream
->runtime
, curp
);
676 /* Remapped I/O space can be accessible as pointer on i386 */
677 /* This might be changed in the future */
680 * silence / copy for playback
683 snd_nm256_playback_silence(struct snd_pcm_substream
*substream
,
684 int channel
, unsigned long pos
, unsigned long count
)
686 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
687 struct nm256_stream
*s
= runtime
->private_data
;
689 memset_io(s
->bufptr
+ pos
, 0, count
);
694 snd_nm256_playback_copy(struct snd_pcm_substream
*substream
,
695 int channel
, unsigned long pos
,
696 void __user
*src
, unsigned long count
)
698 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
699 struct nm256_stream
*s
= runtime
->private_data
;
701 if (copy_from_user_toio(s
->bufptr
+ pos
, src
, count
))
707 snd_nm256_playback_copy_kernel(struct snd_pcm_substream
*substream
,
708 int channel
, unsigned long pos
,
709 void *src
, unsigned long count
)
711 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
712 struct nm256_stream
*s
= runtime
->private_data
;
714 memcpy_toio(s
->bufptr
+ pos
, src
, count
);
722 snd_nm256_capture_copy(struct snd_pcm_substream
*substream
,
723 int channel
, unsigned long pos
,
724 void __user
*dst
, unsigned long count
)
726 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
727 struct nm256_stream
*s
= runtime
->private_data
;
729 if (copy_to_user_fromio(dst
, s
->bufptr
+ pos
, count
))
735 snd_nm256_capture_copy_kernel(struct snd_pcm_substream
*substream
,
736 int channel
, unsigned long pos
,
737 void *dst
, unsigned long count
)
739 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
740 struct nm256_stream
*s
= runtime
->private_data
;
742 memcpy_fromio(dst
, s
->bufptr
+ pos
, count
);
746 #endif /* !__i386__ */
750 * update playback/capture watermarks
755 snd_nm256_playback_update(struct nm256
*chip
)
757 struct nm256_stream
*s
;
759 s
= &chip
->streams
[SNDRV_PCM_STREAM_PLAYBACK
];
760 if (s
->running
&& s
->substream
) {
761 spin_unlock(&chip
->reg_lock
);
762 snd_pcm_period_elapsed(s
->substream
);
763 spin_lock(&chip
->reg_lock
);
764 snd_nm256_playback_mark(chip
, s
);
770 snd_nm256_capture_update(struct nm256
*chip
)
772 struct nm256_stream
*s
;
774 s
= &chip
->streams
[SNDRV_PCM_STREAM_CAPTURE
];
775 if (s
->running
&& s
->substream
) {
776 spin_unlock(&chip
->reg_lock
);
777 snd_pcm_period_elapsed(s
->substream
);
778 spin_lock(&chip
->reg_lock
);
779 snd_nm256_capture_mark(chip
, s
);
786 static struct snd_pcm_hardware snd_nm256_playback
=
788 .info
= SNDRV_PCM_INFO_MMAP_IOMEM
|SNDRV_PCM_INFO_MMAP_VALID
|
789 SNDRV_PCM_INFO_INTERLEAVED
|
790 /*SNDRV_PCM_INFO_PAUSE |*/
791 SNDRV_PCM_INFO_RESUME
,
792 .formats
= SNDRV_PCM_FMTBIT_U8
| SNDRV_PCM_FMTBIT_S16_LE
,
793 .rates
= SNDRV_PCM_RATE_KNOT
/*24k*/ | SNDRV_PCM_RATE_8000_48000
,
800 .buffer_bytes_max
= 128 * 1024,
801 .period_bytes_min
= 256,
802 .period_bytes_max
= 128 * 1024,
805 static struct snd_pcm_hardware snd_nm256_capture
=
807 .info
= SNDRV_PCM_INFO_MMAP_IOMEM
| SNDRV_PCM_INFO_MMAP_VALID
|
808 SNDRV_PCM_INFO_INTERLEAVED
|
809 /*SNDRV_PCM_INFO_PAUSE |*/
810 SNDRV_PCM_INFO_RESUME
,
811 .formats
= SNDRV_PCM_FMTBIT_U8
| SNDRV_PCM_FMTBIT_S16_LE
,
812 .rates
= SNDRV_PCM_RATE_KNOT
/*24k*/ | SNDRV_PCM_RATE_8000_48000
,
819 .buffer_bytes_max
= 128 * 1024,
820 .period_bytes_min
= 256,
821 .period_bytes_max
= 128 * 1024,
825 /* set dma transfer size */
826 static int snd_nm256_pcm_hw_params(struct snd_pcm_substream
*substream
,
827 struct snd_pcm_hw_params
*hw_params
)
829 /* area and addr are already set and unchanged */
830 substream
->runtime
->dma_bytes
= params_buffer_bytes(hw_params
);
837 static void snd_nm256_setup_stream(struct nm256
*chip
, struct nm256_stream
*s
,
838 struct snd_pcm_substream
*substream
,
839 struct snd_pcm_hardware
*hw_ptr
)
841 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
844 runtime
->hw
= *hw_ptr
;
845 runtime
->hw
.buffer_bytes_max
= s
->bufsize
;
846 runtime
->hw
.period_bytes_max
= s
->bufsize
/ 2;
847 runtime
->dma_area
= (void __force
*) s
->bufptr
;
848 runtime
->dma_addr
= s
->bufptr_addr
;
849 runtime
->dma_bytes
= s
->bufsize
;
850 runtime
->private_data
= s
;
851 s
->substream
= substream
;
853 snd_pcm_hw_constraint_list(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
858 snd_nm256_playback_open(struct snd_pcm_substream
*substream
)
860 struct nm256
*chip
= snd_pcm_substream_chip(substream
);
862 if (snd_nm256_acquire_irq(chip
) < 0)
864 snd_nm256_setup_stream(chip
, &chip
->streams
[SNDRV_PCM_STREAM_PLAYBACK
],
865 substream
, &snd_nm256_playback
);
870 snd_nm256_capture_open(struct snd_pcm_substream
*substream
)
872 struct nm256
*chip
= snd_pcm_substream_chip(substream
);
874 if (snd_nm256_acquire_irq(chip
) < 0)
876 snd_nm256_setup_stream(chip
, &chip
->streams
[SNDRV_PCM_STREAM_CAPTURE
],
877 substream
, &snd_nm256_capture
);
882 * close - we don't have to do special..
885 snd_nm256_playback_close(struct snd_pcm_substream
*substream
)
887 struct nm256
*chip
= snd_pcm_substream_chip(substream
);
889 snd_nm256_release_irq(chip
);
895 snd_nm256_capture_close(struct snd_pcm_substream
*substream
)
897 struct nm256
*chip
= snd_pcm_substream_chip(substream
);
899 snd_nm256_release_irq(chip
);
904 * create a pcm instance
906 static const struct snd_pcm_ops snd_nm256_playback_ops
= {
907 .open
= snd_nm256_playback_open
,
908 .close
= snd_nm256_playback_close
,
909 .ioctl
= snd_pcm_lib_ioctl
,
910 .hw_params
= snd_nm256_pcm_hw_params
,
911 .prepare
= snd_nm256_pcm_prepare
,
912 .trigger
= snd_nm256_playback_trigger
,
913 .pointer
= snd_nm256_playback_pointer
,
915 .copy_user
= snd_nm256_playback_copy
,
916 .copy_kernel
= snd_nm256_playback_copy_kernel
,
917 .fill_silence
= snd_nm256_playback_silence
,
919 .mmap
= snd_pcm_lib_mmap_iomem
,
922 static const struct snd_pcm_ops snd_nm256_capture_ops
= {
923 .open
= snd_nm256_capture_open
,
924 .close
= snd_nm256_capture_close
,
925 .ioctl
= snd_pcm_lib_ioctl
,
926 .hw_params
= snd_nm256_pcm_hw_params
,
927 .prepare
= snd_nm256_pcm_prepare
,
928 .trigger
= snd_nm256_capture_trigger
,
929 .pointer
= snd_nm256_capture_pointer
,
931 .copy_user
= snd_nm256_capture_copy
,
932 .copy_kernel
= snd_nm256_capture_copy_kernel
,
934 .mmap
= snd_pcm_lib_mmap_iomem
,
938 snd_nm256_pcm(struct nm256
*chip
, int device
)
943 for (i
= 0; i
< 2; i
++) {
944 struct nm256_stream
*s
= &chip
->streams
[i
];
945 s
->bufptr
= chip
->buffer
+ (s
->buf
- chip
->buffer_start
);
946 s
->bufptr_addr
= chip
->buffer_addr
+ (s
->buf
- chip
->buffer_start
);
949 err
= snd_pcm_new(chip
->card
, chip
->card
->driver
, device
,
954 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_nm256_playback_ops
);
955 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_nm256_capture_ops
);
957 pcm
->private_data
= chip
;
966 * Initialize the hardware.
969 snd_nm256_init_chip(struct nm256
*chip
)
971 /* Reset everything. */
972 snd_nm256_writeb(chip
, 0x0, 0x11);
973 snd_nm256_writew(chip
, 0x214, 0);
975 //snd_nm256_playback_stop(chip);
976 //snd_nm256_capture_stop(chip);
981 snd_nm256_intr_check(struct nm256
*chip
)
983 if (chip
->badintrcount
++ > 1000) {
985 * I'm not sure if the best thing is to stop the card from
986 * playing or just release the interrupt (after all, we're in
987 * a bad situation, so doing fancy stuff may not be such a good
990 * I worry about the card engine continuing to play noise
991 * over and over, however--that could become a very
992 * obnoxious problem. And we know that when this usually
993 * happens things are fairly safe, it just means the user's
994 * inserted a PCMCIA card and someone's spamming us with IRQ 9s.
996 if (chip
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].running
)
997 snd_nm256_playback_stop(chip
);
998 if (chip
->streams
[SNDRV_PCM_STREAM_CAPTURE
].running
)
999 snd_nm256_capture_stop(chip
);
1000 chip
->badintrcount
= 0;
1007 * Handle a potential interrupt for the device referred to by DEV_ID.
1009 * I don't like the cut-n-paste job here either between the two routines,
1010 * but there are sufficient differences between the two interrupt handlers
1011 * that parameterizing it isn't all that great either. (Could use a macro,
1012 * I suppose...yucky bleah.)
1016 snd_nm256_interrupt(int irq
, void *dev_id
)
1018 struct nm256
*chip
= dev_id
;
1022 status
= snd_nm256_readw(chip
, NM_INT_REG
);
1026 return snd_nm256_intr_check(chip
);
1028 chip
->badintrcount
= 0;
1030 /* Rather boring; check for individual interrupts and process them. */
1032 spin_lock(&chip
->reg_lock
);
1033 if (status
& NM_PLAYBACK_INT
) {
1034 status
&= ~NM_PLAYBACK_INT
;
1035 NM_ACK_INT(chip
, NM_PLAYBACK_INT
);
1036 snd_nm256_playback_update(chip
);
1039 if (status
& NM_RECORD_INT
) {
1040 status
&= ~NM_RECORD_INT
;
1041 NM_ACK_INT(chip
, NM_RECORD_INT
);
1042 snd_nm256_capture_update(chip
);
1045 if (status
& NM_MISC_INT_1
) {
1046 status
&= ~NM_MISC_INT_1
;
1047 NM_ACK_INT(chip
, NM_MISC_INT_1
);
1048 dev_dbg(chip
->card
->dev
, "NM256: Got misc interrupt #1\n");
1049 snd_nm256_writew(chip
, NM_INT_REG
, 0x8000);
1050 cbyte
= snd_nm256_readb(chip
, 0x400);
1051 snd_nm256_writeb(chip
, 0x400, cbyte
| 2);
1054 if (status
& NM_MISC_INT_2
) {
1055 status
&= ~NM_MISC_INT_2
;
1056 NM_ACK_INT(chip
, NM_MISC_INT_2
);
1057 dev_dbg(chip
->card
->dev
, "NM256: Got misc interrupt #2\n");
1058 cbyte
= snd_nm256_readb(chip
, 0x400);
1059 snd_nm256_writeb(chip
, 0x400, cbyte
& ~2);
1062 /* Unknown interrupt. */
1064 dev_dbg(chip
->card
->dev
,
1065 "NM256: Fire in the hole! Unknown status 0x%x\n",
1068 NM_ACK_INT(chip
, status
);
1071 spin_unlock(&chip
->reg_lock
);
1076 * Handle a potential interrupt for the device referred to by DEV_ID.
1077 * This handler is for the 256ZX, and is very similar to the non-ZX
1082 snd_nm256_interrupt_zx(int irq
, void *dev_id
)
1084 struct nm256
*chip
= dev_id
;
1088 status
= snd_nm256_readl(chip
, NM_INT_REG
);
1092 return snd_nm256_intr_check(chip
);
1094 chip
->badintrcount
= 0;
1096 /* Rather boring; check for individual interrupts and process them. */
1098 spin_lock(&chip
->reg_lock
);
1099 if (status
& NM2_PLAYBACK_INT
) {
1100 status
&= ~NM2_PLAYBACK_INT
;
1101 NM2_ACK_INT(chip
, NM2_PLAYBACK_INT
);
1102 snd_nm256_playback_update(chip
);
1105 if (status
& NM2_RECORD_INT
) {
1106 status
&= ~NM2_RECORD_INT
;
1107 NM2_ACK_INT(chip
, NM2_RECORD_INT
);
1108 snd_nm256_capture_update(chip
);
1111 if (status
& NM2_MISC_INT_1
) {
1112 status
&= ~NM2_MISC_INT_1
;
1113 NM2_ACK_INT(chip
, NM2_MISC_INT_1
);
1114 dev_dbg(chip
->card
->dev
, "NM256: Got misc interrupt #1\n");
1115 cbyte
= snd_nm256_readb(chip
, 0x400);
1116 snd_nm256_writeb(chip
, 0x400, cbyte
| 2);
1119 if (status
& NM2_MISC_INT_2
) {
1120 status
&= ~NM2_MISC_INT_2
;
1121 NM2_ACK_INT(chip
, NM2_MISC_INT_2
);
1122 dev_dbg(chip
->card
->dev
, "NM256: Got misc interrupt #2\n");
1123 cbyte
= snd_nm256_readb(chip
, 0x400);
1124 snd_nm256_writeb(chip
, 0x400, cbyte
& ~2);
1127 /* Unknown interrupt. */
1129 dev_dbg(chip
->card
->dev
,
1130 "NM256: Fire in the hole! Unknown status 0x%x\n",
1133 NM2_ACK_INT(chip
, status
);
1136 spin_unlock(&chip
->reg_lock
);
1145 * Waits for the mixer to become ready to be written; returns a zero value
1149 snd_nm256_ac97_ready(struct nm256
*chip
)
1155 testaddr
= chip
->mixer_status_offset
;
1156 testb
= chip
->mixer_status_mask
;
1159 * Loop around waiting for the mixer to become ready.
1161 while (timeout
-- > 0) {
1162 if ((snd_nm256_readw(chip
, testaddr
) & testb
) == 0)
1170 * Initial register values to be written to the AC97 mixer.
1171 * While most of these are identical to the reset values, we do this
1172 * so that we have most of the register contents cached--this avoids
1173 * reading from the mixer directly (which seems to be problematic,
1174 * probably due to ignorance).
1177 struct initialValues
{
1179 unsigned short value
;
1182 static struct initialValues nm256_ac97_init_val
[] =
1184 { AC97_MASTER
, 0x8000 },
1185 { AC97_HEADPHONE
, 0x8000 },
1186 { AC97_MASTER_MONO
, 0x8000 },
1187 { AC97_PC_BEEP
, 0x8000 },
1188 { AC97_PHONE
, 0x8008 },
1189 { AC97_MIC
, 0x8000 },
1190 { AC97_LINE
, 0x8808 },
1191 { AC97_CD
, 0x8808 },
1192 { AC97_VIDEO
, 0x8808 },
1193 { AC97_AUX
, 0x8808 },
1194 { AC97_PCM
, 0x8808 },
1195 { AC97_REC_SEL
, 0x0000 },
1196 { AC97_REC_GAIN
, 0x0B0B },
1197 { AC97_GENERAL_PURPOSE
, 0x0000 },
1198 { AC97_3D_CONTROL
, 0x8000 },
1199 { AC97_VENDOR_ID1
, 0x8384 },
1200 { AC97_VENDOR_ID2
, 0x7609 },
1203 static int nm256_ac97_idx(unsigned short reg
)
1206 for (i
= 0; i
< ARRAY_SIZE(nm256_ac97_init_val
); i
++)
1207 if (nm256_ac97_init_val
[i
].reg
== reg
)
1213 * some nm256 easily crash when reading from mixer registers
1214 * thus we're treating it as a write-only mixer and cache the
1217 static unsigned short
1218 snd_nm256_ac97_read(struct snd_ac97
*ac97
, unsigned short reg
)
1220 struct nm256
*chip
= ac97
->private_data
;
1221 int idx
= nm256_ac97_idx(reg
);
1225 return chip
->ac97_regs
[idx
];
1231 snd_nm256_ac97_write(struct snd_ac97
*ac97
,
1232 unsigned short reg
, unsigned short val
)
1234 struct nm256
*chip
= ac97
->private_data
;
1236 int idx
= nm256_ac97_idx(reg
);
1242 base
= chip
->mixer_base
;
1244 snd_nm256_ac97_ready(chip
);
1246 /* Wait for the write to take, too. */
1247 while (tries
-- > 0) {
1248 snd_nm256_writew(chip
, base
+ reg
, val
);
1249 msleep(1); /* a little delay here seems better.. */
1250 if (snd_nm256_ac97_ready(chip
)) {
1251 /* successful write: set cache */
1252 chip
->ac97_regs
[idx
] = val
;
1256 dev_dbg(chip
->card
->dev
, "nm256: ac97 codec not ready..\n");
1259 /* static resolution table */
1260 static const struct snd_ac97_res_table nm256_res_table
[] = {
1261 { AC97_MASTER
, 0x1f1f },
1262 { AC97_HEADPHONE
, 0x1f1f },
1263 { AC97_MASTER_MONO
, 0x001f },
1264 { AC97_PC_BEEP
, 0x001f },
1265 { AC97_PHONE
, 0x001f },
1266 { AC97_MIC
, 0x001f },
1267 { AC97_LINE
, 0x1f1f },
1268 { AC97_CD
, 0x1f1f },
1269 { AC97_VIDEO
, 0x1f1f },
1270 { AC97_AUX
, 0x1f1f },
1271 { AC97_PCM
, 0x1f1f },
1272 { AC97_REC_GAIN
, 0x0f0f },
1273 { } /* terminator */
1276 /* initialize the ac97 into a known state */
1278 snd_nm256_ac97_reset(struct snd_ac97
*ac97
)
1280 struct nm256
*chip
= ac97
->private_data
;
1282 /* Reset the mixer. 'Tis magic! */
1283 snd_nm256_writeb(chip
, 0x6c0, 1);
1284 if (! chip
->reset_workaround
) {
1285 /* Dell latitude LS will lock up by this */
1286 snd_nm256_writeb(chip
, 0x6cc, 0x87);
1288 if (! chip
->reset_workaround_2
) {
1289 /* Dell latitude CSx will lock up by this */
1290 snd_nm256_writeb(chip
, 0x6cc, 0x80);
1291 snd_nm256_writeb(chip
, 0x6cc, 0x0);
1293 if (! chip
->in_resume
) {
1295 for (i
= 0; i
< ARRAY_SIZE(nm256_ac97_init_val
); i
++) {
1296 /* preload the cache, so as to avoid even a single
1297 * read of the mixer regs
1299 snd_nm256_ac97_write(ac97
, nm256_ac97_init_val
[i
].reg
,
1300 nm256_ac97_init_val
[i
].value
);
1305 /* create an ac97 mixer interface */
1307 snd_nm256_mixer(struct nm256
*chip
)
1309 struct snd_ac97_bus
*pbus
;
1310 struct snd_ac97_template ac97
;
1312 static struct snd_ac97_bus_ops ops
= {
1313 .reset
= snd_nm256_ac97_reset
,
1314 .write
= snd_nm256_ac97_write
,
1315 .read
= snd_nm256_ac97_read
,
1318 chip
->ac97_regs
= kcalloc(ARRAY_SIZE(nm256_ac97_init_val
),
1319 sizeof(short), GFP_KERNEL
);
1320 if (! chip
->ac97_regs
)
1323 if ((err
= snd_ac97_bus(chip
->card
, 0, &ops
, NULL
, &pbus
)) < 0)
1326 memset(&ac97
, 0, sizeof(ac97
));
1327 ac97
.scaps
= AC97_SCAP_AUDIO
; /* we support audio! */
1328 ac97
.private_data
= chip
;
1329 ac97
.res_table
= nm256_res_table
;
1331 err
= snd_ac97_mixer(pbus
, &ac97
, &chip
->ac97
);
1334 if (! (chip
->ac97
->id
& (0xf0000000))) {
1335 /* looks like an invalid id */
1336 sprintf(chip
->card
->mixername
, "%s AC97", chip
->card
->driver
);
1342 * See if the signature left by the NM256 BIOS is intact; if so, we use
1343 * the associated address as the end of our audio buffer in the video
1348 snd_nm256_peek_for_sig(struct nm256
*chip
)
1350 /* The signature is located 1K below the end of video RAM. */
1352 /* Default buffer end is 5120 bytes below the top of RAM. */
1353 unsigned long pointer_found
= chip
->buffer_end
- 0x1400;
1356 temp
= ioremap_nocache(chip
->buffer_addr
+ chip
->buffer_end
- 0x400, 16);
1358 dev_err(chip
->card
->dev
,
1359 "Unable to scan for card signature in video RAM\n");
1364 if ((sig
& NM_SIG_MASK
) == NM_SIGNATURE
) {
1365 u32 pointer
= readl(temp
+ 4);
1368 * If it's obviously invalid, don't use it
1370 if (pointer
== 0xffffffff ||
1371 pointer
< chip
->buffer_size
||
1372 pointer
> chip
->buffer_end
) {
1373 dev_err(chip
->card
->dev
,
1374 "invalid signature found: 0x%x\n", pointer
);
1378 pointer_found
= pointer
;
1379 dev_info(chip
->card
->dev
,
1380 "found card signature in video RAM: 0x%x\n",
1386 chip
->buffer_end
= pointer_found
;
1391 #ifdef CONFIG_PM_SLEEP
1393 * APM event handler, so the card is properly reinitialized after a power
1396 static int nm256_suspend(struct device
*dev
)
1398 struct snd_card
*card
= dev_get_drvdata(dev
);
1399 struct nm256
*chip
= card
->private_data
;
1401 snd_power_change_state(card
, SNDRV_CTL_POWER_D3hot
);
1402 snd_ac97_suspend(chip
->ac97
);
1403 chip
->coeffs_current
= 0;
1407 static int nm256_resume(struct device
*dev
)
1409 struct snd_card
*card
= dev_get_drvdata(dev
);
1410 struct nm256
*chip
= card
->private_data
;
1413 /* Perform a full reset on the hardware */
1414 chip
->in_resume
= 1;
1416 snd_nm256_init_chip(chip
);
1419 snd_ac97_resume(chip
->ac97
);
1421 for (i
= 0; i
< 2; i
++) {
1422 struct nm256_stream
*s
= &chip
->streams
[i
];
1423 if (s
->substream
&& s
->suspended
) {
1424 spin_lock_irq(&chip
->reg_lock
);
1425 snd_nm256_set_format(chip
, s
, s
->substream
);
1426 spin_unlock_irq(&chip
->reg_lock
);
1430 snd_power_change_state(card
, SNDRV_CTL_POWER_D0
);
1431 chip
->in_resume
= 0;
1435 static SIMPLE_DEV_PM_OPS(nm256_pm
, nm256_suspend
, nm256_resume
);
1436 #define NM256_PM_OPS &nm256_pm
1438 #define NM256_PM_OPS NULL
1439 #endif /* CONFIG_PM_SLEEP */
1441 static int snd_nm256_free(struct nm256
*chip
)
1443 if (chip
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].running
)
1444 snd_nm256_playback_stop(chip
);
1445 if (chip
->streams
[SNDRV_PCM_STREAM_CAPTURE
].running
)
1446 snd_nm256_capture_stop(chip
);
1449 free_irq(chip
->irq
, chip
);
1451 iounmap(chip
->cport
);
1452 iounmap(chip
->buffer
);
1453 release_and_free_resource(chip
->res_cport
);
1454 release_and_free_resource(chip
->res_buffer
);
1456 pci_disable_device(chip
->pci
);
1457 kfree(chip
->ac97_regs
);
1462 static int snd_nm256_dev_free(struct snd_device
*device
)
1464 struct nm256
*chip
= device
->device_data
;
1465 return snd_nm256_free(chip
);
1469 snd_nm256_create(struct snd_card
*card
, struct pci_dev
*pci
,
1470 struct nm256
**chip_ret
)
1474 static struct snd_device_ops ops
= {
1475 .dev_free
= snd_nm256_dev_free
,
1481 if ((err
= pci_enable_device(pci
)) < 0)
1484 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
1486 pci_disable_device(pci
);
1492 chip
->use_cache
= use_cache
;
1493 spin_lock_init(&chip
->reg_lock
);
1495 mutex_init(&chip
->irq_mutex
);
1497 /* store buffer sizes in bytes */
1498 chip
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].bufsize
= playback_bufsize
* 1024;
1499 chip
->streams
[SNDRV_PCM_STREAM_CAPTURE
].bufsize
= capture_bufsize
* 1024;
1502 * The NM256 has two memory ports. The first port is nothing
1503 * more than a chunk of video RAM, which is used as the I/O ring
1504 * buffer. The second port has the actual juicy stuff (like the
1505 * mixer and the playback engine control registers).
1508 chip
->buffer_addr
= pci_resource_start(pci
, 0);
1509 chip
->cport_addr
= pci_resource_start(pci
, 1);
1511 /* Init the memory port info. */
1512 /* remap control port (#2) */
1513 chip
->res_cport
= request_mem_region(chip
->cport_addr
, NM_PORT2_SIZE
,
1515 if (chip
->res_cport
== NULL
) {
1516 dev_err(card
->dev
, "memory region 0x%lx (size 0x%x) busy\n",
1517 chip
->cport_addr
, NM_PORT2_SIZE
);
1521 chip
->cport
= ioremap_nocache(chip
->cport_addr
, NM_PORT2_SIZE
);
1522 if (chip
->cport
== NULL
) {
1523 dev_err(card
->dev
, "unable to map control port %lx\n",
1529 if (!strcmp(card
->driver
, "NM256AV")) {
1530 /* Ok, try to see if this is a non-AC97 version of the hardware. */
1531 pval
= snd_nm256_readw(chip
, NM_MIXER_PRESENCE
);
1532 if ((pval
& NM_PRESENCE_MASK
) != NM_PRESENCE_VALUE
) {
1535 "no ac97 is found!\n");
1537 "force the driver to load by passing in the module parameter\n");
1541 "or try sb16, opl3sa2, or cs423x drivers instead.\n");
1546 chip
->buffer_end
= 2560 * 1024;
1547 chip
->interrupt
= snd_nm256_interrupt
;
1548 chip
->mixer_status_offset
= NM_MIXER_STATUS_OFFSET
;
1549 chip
->mixer_status_mask
= NM_MIXER_READY_MASK
;
1551 /* Not sure if there is any relevant detect for the ZX or not. */
1552 if (snd_nm256_readb(chip
, 0xa0b) != 0)
1553 chip
->buffer_end
= 6144 * 1024;
1555 chip
->buffer_end
= 4096 * 1024;
1557 chip
->interrupt
= snd_nm256_interrupt_zx
;
1558 chip
->mixer_status_offset
= NM2_MIXER_STATUS_OFFSET
;
1559 chip
->mixer_status_mask
= NM2_MIXER_READY_MASK
;
1562 chip
->buffer_size
= chip
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].bufsize
+
1563 chip
->streams
[SNDRV_PCM_STREAM_CAPTURE
].bufsize
;
1564 if (chip
->use_cache
)
1565 chip
->buffer_size
+= NM_TOTAL_COEFF_COUNT
* 4;
1567 chip
->buffer_size
+= NM_MAX_PLAYBACK_COEF_SIZE
+ NM_MAX_RECORD_COEF_SIZE
;
1569 if (buffer_top
>= chip
->buffer_size
&& buffer_top
< chip
->buffer_end
)
1570 chip
->buffer_end
= buffer_top
;
1572 /* get buffer end pointer from signature */
1573 if ((err
= snd_nm256_peek_for_sig(chip
)) < 0)
1577 chip
->buffer_start
= chip
->buffer_end
- chip
->buffer_size
;
1578 chip
->buffer_addr
+= chip
->buffer_start
;
1580 dev_info(card
->dev
, "Mapping port 1 from 0x%x - 0x%x\n",
1581 chip
->buffer_start
, chip
->buffer_end
);
1583 chip
->res_buffer
= request_mem_region(chip
->buffer_addr
,
1586 if (chip
->res_buffer
== NULL
) {
1587 dev_err(card
->dev
, "buffer 0x%lx (size 0x%x) busy\n",
1588 chip
->buffer_addr
, chip
->buffer_size
);
1592 chip
->buffer
= ioremap_nocache(chip
->buffer_addr
, chip
->buffer_size
);
1593 if (chip
->buffer
== NULL
) {
1595 dev_err(card
->dev
, "unable to map ring buffer at %lx\n",
1601 addr
= chip
->buffer_start
;
1602 chip
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].buf
= addr
;
1603 addr
+= chip
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].bufsize
;
1604 chip
->streams
[SNDRV_PCM_STREAM_CAPTURE
].buf
= addr
;
1605 addr
+= chip
->streams
[SNDRV_PCM_STREAM_CAPTURE
].bufsize
;
1606 if (chip
->use_cache
) {
1607 chip
->all_coeff_buf
= addr
;
1609 chip
->coeff_buf
[SNDRV_PCM_STREAM_PLAYBACK
] = addr
;
1610 addr
+= NM_MAX_PLAYBACK_COEF_SIZE
;
1611 chip
->coeff_buf
[SNDRV_PCM_STREAM_CAPTURE
] = addr
;
1614 /* Fixed setting. */
1615 chip
->mixer_base
= NM_MIXER_OFFSET
;
1617 chip
->coeffs_current
= 0;
1619 snd_nm256_init_chip(chip
);
1621 // pci_set_master(pci); /* needed? */
1623 if ((err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, chip
, &ops
)) < 0)
1630 snd_nm256_free(chip
);
1635 enum { NM_BLACKLISTED
, NM_RESET_WORKAROUND
, NM_RESET_WORKAROUND_2
};
1637 static struct snd_pci_quirk nm256_quirks
[] = {
1638 /* HP omnibook 4150 has cs4232 codec internally */
1639 SND_PCI_QUIRK(0x103c, 0x0007, "HP omnibook 4150", NM_BLACKLISTED
),
1640 /* Reset workarounds to avoid lock-ups */
1641 SND_PCI_QUIRK(0x104d, 0x8041, "Sony PCG-F305", NM_RESET_WORKAROUND
),
1642 SND_PCI_QUIRK(0x1028, 0x0080, "Dell Latitude LS", NM_RESET_WORKAROUND
),
1643 SND_PCI_QUIRK(0x1028, 0x0091, "Dell Latitude CSx", NM_RESET_WORKAROUND_2
),
1644 { } /* terminator */
1648 static int snd_nm256_probe(struct pci_dev
*pci
,
1649 const struct pci_device_id
*pci_id
)
1651 struct snd_card
*card
;
1654 const struct snd_pci_quirk
*q
;
1656 q
= snd_pci_quirk_lookup(pci
, nm256_quirks
);
1658 dev_dbg(&pci
->dev
, "Enabled quirk for %s.\n",
1659 snd_pci_quirk_name(q
));
1661 case NM_BLACKLISTED
:
1663 "The device is blacklisted. Loading stopped\n");
1665 case NM_RESET_WORKAROUND_2
:
1666 reset_workaround_2
= 1;
1668 case NM_RESET_WORKAROUND
:
1669 reset_workaround
= 1;
1674 err
= snd_card_new(&pci
->dev
, index
, id
, THIS_MODULE
, 0, &card
);
1678 switch (pci
->device
) {
1679 case PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO
:
1680 strcpy(card
->driver
, "NM256AV");
1682 case PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO
:
1683 strcpy(card
->driver
, "NM256ZX");
1685 case PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO
:
1686 strcpy(card
->driver
, "NM256XL+");
1689 dev_err(&pci
->dev
, "invalid device id 0x%x\n", pci
->device
);
1690 snd_card_free(card
);
1695 buffer_top
= 0x25a800; /* this avoids conflicts with XFree86 server */
1697 if (playback_bufsize
< 4)
1698 playback_bufsize
= 4;
1699 if (playback_bufsize
> 128)
1700 playback_bufsize
= 128;
1701 if (capture_bufsize
< 4)
1702 capture_bufsize
= 4;
1703 if (capture_bufsize
> 128)
1704 capture_bufsize
= 128;
1705 if ((err
= snd_nm256_create(card
, pci
, &chip
)) < 0) {
1706 snd_card_free(card
);
1709 card
->private_data
= chip
;
1711 if (reset_workaround
) {
1712 dev_dbg(&pci
->dev
, "reset_workaround activated\n");
1713 chip
->reset_workaround
= 1;
1716 if (reset_workaround_2
) {
1717 dev_dbg(&pci
->dev
, "reset_workaround_2 activated\n");
1718 chip
->reset_workaround_2
= 1;
1721 if ((err
= snd_nm256_pcm(chip
, 0)) < 0 ||
1722 (err
= snd_nm256_mixer(chip
)) < 0) {
1723 snd_card_free(card
);
1727 sprintf(card
->shortname
, "NeoMagic %s", card
->driver
);
1728 sprintf(card
->longname
, "%s at 0x%lx & 0x%lx, irq %d",
1730 chip
->buffer_addr
, chip
->cport_addr
, chip
->irq
);
1732 if ((err
= snd_card_register(card
)) < 0) {
1733 snd_card_free(card
);
1737 pci_set_drvdata(pci
, card
);
1741 static void snd_nm256_remove(struct pci_dev
*pci
)
1743 snd_card_free(pci_get_drvdata(pci
));
1747 static struct pci_driver nm256_driver
= {
1748 .name
= KBUILD_MODNAME
,
1749 .id_table
= snd_nm256_ids
,
1750 .probe
= snd_nm256_probe
,
1751 .remove
= snd_nm256_remove
,
1757 module_pci_driver(nm256_driver
);