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