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