source: GPL/trunk/alsa-kernel/pci/es1968.c@ 679

Last change on this file since 679 was 679, checked in by David Azarewicz, 4 years ago

Merge changes from Paul's uniaud32next branch.

File size: 81.7 KB
Line 
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Driver for ESS Maestro 1/2/2E Sound Card (started 21.8.99)
4 * Copyright (c) by Matze Braun <MatzeBraun@gmx.de>.
5 * Takashi Iwai <tiwai@suse.de>
6 *
7 * Most of the driver code comes from Zach Brown(zab@redhat.com)
8 * Alan Cox OSS Driver
9 * Rewritted from card-es1938.c source.
10 *
11 * TODO:
12 * Perhaps Synth
13 *
14 * Notes from Zach Brown about the driver code
15 *
16 * Hardware Description
17 *
18 * A working Maestro setup contains the Maestro chip wired to a
19 * codec or 2. In the Maestro we have the APUs, the ASSP, and the
20 * Wavecache. The APUs can be though of as virtual audio routing
21 * channels. They can take data from a number of sources and perform
22 * basic encodings of the data. The wavecache is a storehouse for
23 * PCM data. Typically it deals with PCI and interracts with the
24 * APUs. The ASSP is a wacky DSP like device that ESS is loth
25 * to release docs on. Thankfully it isn't required on the Maestro
26 * until you start doing insane things like FM emulation and surround
27 * encoding. The codecs are almost always AC-97 compliant codecs,
28 * but it appears that early Maestros may have had PT101 (an ESS
29 * part?) wired to them. The only real difference in the Maestro
30 * families is external goop like docking capability, memory for
31 * the ASSP, and initialization differences.
32 *
33 * Driver Operation
34 *
35 * We only drive the APU/Wavecache as typical DACs and drive the
36 * mixers in the codecs. There are 64 APUs. We assign 6 to each
37 * /dev/dsp? device. 2 channels for output, and 4 channels for
38 * input.
39 *
40 * Each APU can do a number of things, but we only really use
41 * 3 basic functions. For playback we use them to convert PCM
42 * data fetched over PCI by the wavecahche into analog data that
43 * is handed to the codec. One APU for mono, and a pair for stereo.
44 * When in stereo, the combination of smarts in the APU and Wavecache
45 * decide which wavecache gets the left or right channel.
46 *
47 * For record we still use the old overly mono system. For each in
48 * coming channel the data comes in from the codec, through a 'input'
49 * APU, through another rate converter APU, and then into memory via
50 * the wavecache and PCI. If its stereo, we mash it back into LRLR in
51 * software. The pass between the 2 APUs is supposedly what requires us
52 * to have a 512 byte buffer sitting around in wavecache/memory.
53 *
54 * The wavecache makes our life even more fun. First off, it can
55 * only address the first 28 bits of PCI address space, making it
56 * useless on quite a few architectures. Secondly, its insane.
57 * It claims to fetch from 4 regions of PCI space, each 4 meg in length.
58 * But that doesn't really work. You can only use 1 region. So all our
59 * allocations have to be in 4meg of each other. Booo. Hiss.
60 * So we have a module parameter, dsps_order, that is the order of
61 * the number of dsps to provide. All their buffer space is allocated
62 * on open time. The sonicvibes OSS routines we inherited really want
63 * power of 2 buffers, so we have all those next to each other, then
64 * 512 byte regions for the recording wavecaches. This ends up
65 * wasting quite a bit of memory. The only fixes I can see would be
66 * getting a kernel allocator that could work in zones, or figuring out
67 * just how to coerce the WP into doing what we want.
68 *
69 * The indirection of the various registers means we have to spinlock
70 * nearly all register accesses. We have the main register indirection
71 * like the wave cache, maestro registers, etc. Then we have beasts
72 * like the APU interface that is indirect registers gotten at through
73 * the main maestro indirection. Ouch. We spinlock around the actual
74 * ports on a per card basis. This means spinlock activity at each IO
75 * operation, but the only IO operation clusters are in non critical
76 * paths and it makes the code far easier to follow. Interrupts are
77 * blocked while holding the locks because the int handler has to
78 * get at some of them :(. The mixer interface doesn't, however.
79 * We also have an OSS state lock that is thrown around in a few
80 * places.
81 */
82
83#include <linux/io.h>
84#include <linux/delay.h>
85#include <linux/interrupt.h>
86#include <linux/init.h>
87#include <linux/pci.h>
88#include <linux/dma-mapping.h>
89#include <linux/slab.h>
90#include <linux/gameport.h>
91#include <linux/module.h>
92#include <linux/mutex.h>
93#include <linux/input.h>
94
95#include <sound/core.h>
96#include <sound/pcm.h>
97#include <sound/mpu401.h>
98#include <sound/ac97_codec.h>
99#include <sound/initval.h>
100
101#ifdef CONFIG_SND_ES1968_RADIO
102#include <media/drv-intf/tea575x.h>
103#endif
104
105#define CARD_NAME "ESS Maestro1/2"
106#define DRIVER_NAME "ES1968"
107
108#ifdef TARGET_OS2
109#define KBUILD_MODNAME "es1968"
110#endif
111MODULE_DESCRIPTION("ESS Maestro");
112MODULE_LICENSE("GPL");
113MODULE_SUPPORTED_DEVICE("{{ESS,Maestro 2e},"
114 "{ESS,Maestro 2},"
115 "{ESS,Maestro 1},"
116 "{TerraTec,DMX}}");
117
118#if IS_REACHABLE(CONFIG_GAMEPORT)
119#define SUPPORT_JOYSTICK 1
120#endif
121
122static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 1-MAX */
123static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
124static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
125#ifndef TARGET_OS2
126static int total_bufsize[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1024 };
127static int pcm_substreams_p[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4 };
128static int pcm_substreams_c[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1 };
129static int clock[SNDRV_CARDS];
130static int use_pm[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
131static int enable_mpu[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
132#else
133static int total_bufsize[SNDRV_CARDS] = { REPEAT_SNDRV(1024) };
134static int pcm_substreams_p[SNDRV_CARDS] = { REPEAT_SNDRV(4) };
135static int pcm_substreams_c[SNDRV_CARDS] = { REPEAT_SNDRV(1) };
136static int clock[SNDRV_CARDS];
137static int use_pm[SNDRV_CARDS] = { REPEAT_SNDRV(2) };
138static int enable_mpu[SNDRV_CARDS] = { REPEAT_SNDRV(1) };
139#endif
140#ifdef SUPPORT_JOYSTICK
141static bool joystick[SNDRV_CARDS];
142#endif
143#ifndef TARGET_OS2
144static int radio_nr[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
145#else
146static int radio_nr[SNDRV_CARDS] = { REPEAT_SNDRV(1) };
147#endif
148module_param_array(index, int, NULL, 0444);
149MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
150module_param_array(id, charp, NULL, 0444);
151MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
152module_param_array(enable, bool, NULL, 0444);
153MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
154module_param_array(total_bufsize, int, NULL, 0444);
155MODULE_PARM_DESC(total_bufsize, "Total buffer size in kB.");
156module_param_array(pcm_substreams_p, int, NULL, 0444);
157MODULE_PARM_DESC(pcm_substreams_p, "PCM Playback substreams for " CARD_NAME " soundcard.");
158module_param_array(pcm_substreams_c, int, NULL, 0444);
159MODULE_PARM_DESC(pcm_substreams_c, "PCM Capture substreams for " CARD_NAME " soundcard.");
160module_param_array(clock, int, NULL, 0444);
161MODULE_PARM_DESC(clock, "Clock on " CARD_NAME " soundcard. (0 = auto-detect)");
162module_param_array(use_pm, int, NULL, 0444);
163MODULE_PARM_DESC(use_pm, "Toggle power-management. (0 = off, 1 = on, 2 = auto)");
164module_param_array(enable_mpu, int, NULL, 0444);
165MODULE_PARM_DESC(enable_mpu, "Enable MPU401. (0 = off, 1 = on, 2 = auto)");
166#ifdef SUPPORT_JOYSTICK
167module_param_array(joystick, bool, NULL, 0444);
168MODULE_PARM_DESC(joystick, "Enable joystick.");
169#endif
170module_param_array(radio_nr, int, NULL, 0444);
171MODULE_PARM_DESC(radio_nr, "Radio device numbers");
172
173
174
175#define NR_APUS 64
176#define NR_APU_REGS 16
177
178/* NEC Versas ? */
179#define NEC_VERSA_SUBID1 0x80581033
180#define NEC_VERSA_SUBID2 0x803c1033
181
182/* Mode Flags */
183#define ESS_FMT_STEREO 0x01
184#define ESS_FMT_16BIT 0x02
185
186#define DAC_RUNNING 1
187#define ADC_RUNNING 2
188
189/* Values for the ESM_LEGACY_AUDIO_CONTROL */
190
191#define ESS_DISABLE_AUDIO 0x8000
192#define ESS_ENABLE_SERIAL_IRQ 0x4000
193#define IO_ADRESS_ALIAS 0x0020
194#define MPU401_IRQ_ENABLE 0x0010
195#define MPU401_IO_ENABLE 0x0008
196#define GAME_IO_ENABLE 0x0004
197#define FM_IO_ENABLE 0x0002
198#define SB_IO_ENABLE 0x0001
199
200/* Values for the ESM_CONFIG_A */
201
202#define PIC_SNOOP1 0x4000
203#define PIC_SNOOP2 0x2000
204#define SAFEGUARD 0x0800
205#define DMA_CLEAR 0x0700
206#define DMA_DDMA 0x0000
207#define DMA_TDMA 0x0100
208#define DMA_PCPCI 0x0200
209#define POST_WRITE 0x0080
210#define PCI_TIMING 0x0040
211#define SWAP_LR 0x0020
212#define SUBTR_DECODE 0x0002
213
214/* Values for the ESM_CONFIG_B */
215
216#define SPDIF_CONFB 0x0100
217#define HWV_CONFB 0x0080
218#define DEBOUNCE 0x0040
219#define GPIO_CONFB 0x0020
220#define CHI_CONFB 0x0010
221#define IDMA_CONFB 0x0008 /*undoc */
222#define MIDI_FIX 0x0004 /*undoc */
223#define IRQ_TO_ISA 0x0001 /*undoc */
224
225/* Values for Ring Bus Control B */
226#define RINGB_2CODEC_ID_MASK 0x0003
227#define RINGB_DIS_VALIDATION 0x0008
228#define RINGB_EN_SPDIF 0x0010
229#define RINGB_EN_2CODEC 0x0020
230#define RINGB_SING_BIT_DUAL 0x0040
231
232/* ****Port Addresses**** */
233
234/* Write & Read */
235#define ESM_INDEX 0x02
236#define ESM_DATA 0x00
237
238/* AC97 + RingBus */
239#define ESM_AC97_INDEX 0x30
240#define ESM_AC97_DATA 0x32
241#define ESM_RING_BUS_DEST 0x34
242#define ESM_RING_BUS_CONTR_A 0x36
243#define ESM_RING_BUS_CONTR_B 0x38
244#define ESM_RING_BUS_SDO 0x3A
245
246/* WaveCache*/
247#define WC_INDEX 0x10
248#define WC_DATA 0x12
249#define WC_CONTROL 0x14
250
251/* ASSP*/
252#define ASSP_INDEX 0x80
253#define ASSP_MEMORY 0x82
254#define ASSP_DATA 0x84
255#define ASSP_CONTROL_A 0xA2
256#define ASSP_CONTROL_B 0xA4
257#define ASSP_CONTROL_C 0xA6
258#define ASSP_HOSTW_INDEX 0xA8
259#define ASSP_HOSTW_DATA 0xAA
260#define ASSP_HOSTW_IRQ 0xAC
261/* Midi */
262#define ESM_MPU401_PORT 0x98
263/* Others */
264#define ESM_PORT_HOST_IRQ 0x18
265
266#define IDR0_DATA_PORT 0x00
267#define IDR1_CRAM_POINTER 0x01
268#define IDR2_CRAM_DATA 0x02
269#define IDR3_WAVE_DATA 0x03
270#define IDR4_WAVE_PTR_LOW 0x04
271#define IDR5_WAVE_PTR_HI 0x05
272#define IDR6_TIMER_CTRL 0x06
273#define IDR7_WAVE_ROMRAM 0x07
274
275#define WRITEABLE_MAP 0xEFFFFF
276#define READABLE_MAP 0x64003F
277
278/* PCI Register */
279
280#define ESM_LEGACY_AUDIO_CONTROL 0x40
281#define ESM_ACPI_COMMAND 0x54
282#define ESM_CONFIG_A 0x50
283#define ESM_CONFIG_B 0x52
284#define ESM_DDMA 0x60
285
286/* Bob Bits */
287#define ESM_BOB_ENABLE 0x0001
288#define ESM_BOB_START 0x0001
289
290/* Host IRQ Control Bits */
291#define ESM_RESET_MAESTRO 0x8000
292#define ESM_RESET_DIRECTSOUND 0x4000
293#define ESM_HIRQ_ClkRun 0x0100
294#define ESM_HIRQ_HW_VOLUME 0x0040
295#define ESM_HIRQ_HARPO 0x0030 /* What's that? */
296#define ESM_HIRQ_ASSP 0x0010
297#define ESM_HIRQ_DSIE 0x0004
298#define ESM_HIRQ_MPU401 0x0002
299#define ESM_HIRQ_SB 0x0001
300
301/* Host IRQ Status Bits */
302#define ESM_MPU401_IRQ 0x02
303#define ESM_SB_IRQ 0x01
304#define ESM_SOUND_IRQ 0x04
305#define ESM_ASSP_IRQ 0x10
306#define ESM_HWVOL_IRQ 0x40
307
308#define ESS_SYSCLK 50000000
309#define ESM_BOB_FREQ 200
310#define ESM_BOB_FREQ_MAX 800
311
312#define ESM_FREQ_ESM1 (49152000L / 1024L) /* default rate 48000 */
313#define ESM_FREQ_ESM2 (50000000L / 1024L)
314
315/* APU Modes: reg 0x00, bit 4-7 */
316#define ESM_APU_MODE_SHIFT 4
317#define ESM_APU_MODE_MASK (0xf << 4)
318#define ESM_APU_OFF 0x00
319#define ESM_APU_16BITLINEAR 0x01 /* 16-Bit Linear Sample Player */
320#define ESM_APU_16BITSTEREO 0x02 /* 16-Bit Stereo Sample Player */
321#define ESM_APU_8BITLINEAR 0x03 /* 8-Bit Linear Sample Player */
322#define ESM_APU_8BITSTEREO 0x04 /* 8-Bit Stereo Sample Player */
323#define ESM_APU_8BITDIFF 0x05 /* 8-Bit Differential Sample Playrer */
324#define ESM_APU_DIGITALDELAY 0x06 /* Digital Delay Line */
325#define ESM_APU_DUALTAP 0x07 /* Dual Tap Reader */
326#define ESM_APU_CORRELATOR 0x08 /* Correlator */
327#define ESM_APU_INPUTMIXER 0x09 /* Input Mixer */
328#define ESM_APU_WAVETABLE 0x0A /* Wave Table Mode */
329#define ESM_APU_SRCONVERTOR 0x0B /* Sample Rate Convertor */
330#define ESM_APU_16BITPINGPONG 0x0C /* 16-Bit Ping-Pong Sample Player */
331#define ESM_APU_RESERVED1 0x0D /* Reserved 1 */
332#define ESM_APU_RESERVED2 0x0E /* Reserved 2 */
333#define ESM_APU_RESERVED3 0x0F /* Reserved 3 */
334
335/* reg 0x00 */
336#define ESM_APU_FILTER_Q_SHIFT 0
337#define ESM_APU_FILTER_Q_MASK (3 << 0)
338/* APU Filtey Q Control */
339#define ESM_APU_FILTER_LESSQ 0x00
340#define ESM_APU_FILTER_MOREQ 0x03
341
342#define ESM_APU_FILTER_TYPE_SHIFT 2
343#define ESM_APU_FILTER_TYPE_MASK (3 << 2)
344#define ESM_APU_ENV_TYPE_SHIFT 8
345#define ESM_APU_ENV_TYPE_MASK (3 << 8)
346#define ESM_APU_ENV_STATE_SHIFT 10
347#define ESM_APU_ENV_STATE_MASK (3 << 10)
348#define ESM_APU_END_CURVE (1 << 12)
349#define ESM_APU_INT_ON_LOOP (1 << 13)
350#define ESM_APU_DMA_ENABLE (1 << 14)
351
352/* reg 0x02 */
353#define ESM_APU_SUBMIX_GROUP_SHIRT 0
354#define ESM_APU_SUBMIX_GROUP_MASK (7 << 0)
355#define ESM_APU_SUBMIX_MODE (1 << 3)
356#define ESM_APU_6dB (1 << 4)
357#define ESM_APU_DUAL_EFFECT (1 << 5)
358#define ESM_APU_EFFECT_CHANNELS_SHIFT 6
359#define ESM_APU_EFFECT_CHANNELS_MASK (3 << 6)
360
361/* reg 0x03 */
362#define ESM_APU_STEP_SIZE_MASK 0x0fff
363
364/* reg 0x04 */
365#define ESM_APU_PHASE_SHIFT 0
366#define ESM_APU_PHASE_MASK (0xff << 0)
367#define ESM_APU_WAVE64K_PAGE_SHIFT 8 /* most 8bit of wave start offset */
368#define ESM_APU_WAVE64K_PAGE_MASK (0xff << 8)
369
370/* reg 0x05 - wave start offset */
371/* reg 0x06 - wave end offset */
372/* reg 0x07 - wave loop length */
373
374/* reg 0x08 */
375#define ESM_APU_EFFECT_GAIN_SHIFT 0
376#define ESM_APU_EFFECT_GAIN_MASK (0xff << 0)
377#define ESM_APU_TREMOLO_DEPTH_SHIFT 8
378#define ESM_APU_TREMOLO_DEPTH_MASK (0xf << 8)
379#define ESM_APU_TREMOLO_RATE_SHIFT 12
380#define ESM_APU_TREMOLO_RATE_MASK (0xf << 12)
381
382/* reg 0x09 */
383/* bit 0-7 amplitude dest? */
384#define ESM_APU_AMPLITUDE_NOW_SHIFT 8
385#define ESM_APU_AMPLITUDE_NOW_MASK (0xff << 8)
386
387/* reg 0x0a */
388#define ESM_APU_POLAR_PAN_SHIFT 0
389#define ESM_APU_POLAR_PAN_MASK (0x3f << 0)
390/* Polar Pan Control */
391#define ESM_APU_PAN_CENTER_CIRCLE 0x00
392#define ESM_APU_PAN_MIDDLE_RADIUS 0x01
393#define ESM_APU_PAN_OUTSIDE_RADIUS 0x02
394
395#define ESM_APU_FILTER_TUNING_SHIFT 8
396#define ESM_APU_FILTER_TUNING_MASK (0xff << 8)
397
398/* reg 0x0b */
399#define ESM_APU_DATA_SRC_A_SHIFT 0
400#define ESM_APU_DATA_SRC_A_MASK (0x7f << 0)
401#define ESM_APU_INV_POL_A (1 << 7)
402#define ESM_APU_DATA_SRC_B_SHIFT 8
403#define ESM_APU_DATA_SRC_B_MASK (0x7f << 8)
404#define ESM_APU_INV_POL_B (1 << 15)
405
406#define ESM_APU_VIBRATO_RATE_SHIFT 0
407#define ESM_APU_VIBRATO_RATE_MASK (0xf << 0)
408#define ESM_APU_VIBRATO_DEPTH_SHIFT 4
409#define ESM_APU_VIBRATO_DEPTH_MASK (0xf << 4)
410#define ESM_APU_VIBRATO_PHASE_SHIFT 8
411#define ESM_APU_VIBRATO_PHASE_MASK (0xff << 8)
412
413/* reg 0x0c */
414#define ESM_APU_RADIUS_SELECT (1 << 6)
415
416/* APU Filter Control */
417#define ESM_APU_FILTER_2POLE_LOPASS 0x00
418#define ESM_APU_FILTER_2POLE_BANDPASS 0x01
419#define ESM_APU_FILTER_2POLE_HIPASS 0x02
420#define ESM_APU_FILTER_1POLE_LOPASS 0x03
421#define ESM_APU_FILTER_1POLE_HIPASS 0x04
422#define ESM_APU_FILTER_OFF 0x05
423
424/* APU ATFP Type */
425#define ESM_APU_ATFP_AMPLITUDE 0x00
426#define ESM_APU_ATFP_TREMELO 0x01
427#define ESM_APU_ATFP_FILTER 0x02
428#define ESM_APU_ATFP_PAN 0x03
429
430/* APU ATFP Flags */
431#define ESM_APU_ATFP_FLG_OFF 0x00
432#define ESM_APU_ATFP_FLG_WAIT 0x01
433#define ESM_APU_ATFP_FLG_DONE 0x02
434#define ESM_APU_ATFP_FLG_INPROCESS 0x03
435
436
437/* capture mixing buffer size */
438#define ESM_MEM_ALIGN 0x1000
439#define ESM_MIXBUF_SIZE 0x400
440
441#define ESM_MODE_PLAY 0
442#define ESM_MODE_CAPTURE 1
443
444
445/* APU use in the driver */
446enum snd_enum_apu_type {
447 ESM_APU_PCM_PLAY,
448 ESM_APU_PCM_CAPTURE,
449 ESM_APU_PCM_RATECONV,
450 ESM_APU_FREE
451};
452
453/* chip type */
454enum {
455 TYPE_MAESTRO, TYPE_MAESTRO2, TYPE_MAESTRO2E
456};
457
458/* DMA Hack! */
459struct esm_memory {
460 struct snd_dma_buffer buf;
461 int empty; /* status */
462 struct list_head list;
463};
464
465/* Playback Channel */
466struct esschan {
467 int running;
468
469 u8 apu[4];
470 u8 apu_mode[4];
471
472 /* playback/capture pcm buffer */
473 struct esm_memory *memory;
474 /* capture mixer buffer */
475 struct esm_memory *mixbuf;
476
477 unsigned int hwptr; /* current hw pointer in bytes */
478 unsigned int count; /* sample counter in bytes */
479 unsigned int dma_size; /* total buffer size in bytes */
480 unsigned int frag_size; /* period size in bytes */
481 unsigned int wav_shift;
482 u16 base[4]; /* offset for ptr */
483
484 /* stereo/16bit flag */
485 unsigned char fmt;
486 int mode; /* playback / capture */
487
488 int bob_freq; /* required timer frequency */
489
490 struct snd_pcm_substream *substream;
491
492 /* linked list */
493 struct list_head list;
494
495#ifdef CONFIG_PM_SLEEP
496 u16 wc_map[4];
497#endif
498};
499
500struct es1968 {
501 /* Module Config */
502 int total_bufsize; /* in bytes */
503
504 int playback_streams, capture_streams;
505
506 unsigned int clock; /* clock */
507 /* for clock measurement */
508 unsigned int in_measurement: 1;
509 unsigned int measure_apu;
510 unsigned int measure_lastpos;
511 unsigned int measure_count;
512
513 /* buffer */
514 struct snd_dma_buffer dma;
515
516 /* Resources... */
517 int irq;
518 unsigned long io_port;
519 int type;
520 struct pci_dev *pci;
521 struct snd_card *card;
522 struct snd_pcm *pcm;
523 int do_pm; /* power-management enabled */
524
525 /* DMA memory block */
526 struct list_head buf_list;
527
528 /* ALSA Stuff */
529 struct snd_ac97 *ac97;
530 struct snd_rawmidi *rmidi;
531
532 spinlock_t reg_lock;
533 unsigned int in_suspend;
534
535 /* Maestro Stuff */
536 u16 maestro_map[32];
537 int bobclient; /* active timer instancs */
538 int bob_freq; /* timer frequency */
539 struct mutex memory_mutex; /* memory lock */
540
541 /* APU states */
542 unsigned char apu[NR_APUS];
543
544 /* active substreams */
545 struct list_head substream_list;
546 spinlock_t substream_lock;
547
548#ifdef CONFIG_PM_SLEEP
549 u16 apu_map[NR_APUS][NR_APU_REGS];
550#endif
551
552#ifdef SUPPORT_JOYSTICK
553 struct gameport *gameport;
554#endif
555
556#ifdef CONFIG_SND_ES1968_INPUT
557 struct input_dev *input_dev;
558 char phys[64]; /* physical device path */
559#else
560 struct snd_kcontrol *master_switch; /* for h/w volume control */
561 struct snd_kcontrol *master_volume;
562#endif
563 struct work_struct hwvol_work;
564
565#ifdef CONFIG_SND_ES1968_RADIO
566 struct v4l2_device v4l2_dev;
567 struct snd_tea575x tea;
568 unsigned int tea575x_tuner;
569#endif
570};
571
572static irqreturn_t snd_es1968_interrupt(int irq, void *dev_id);
573
574static const struct pci_device_id snd_es1968_ids[] = {
575 /* Maestro 1 */
576 { 0x1285, 0x0100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, TYPE_MAESTRO },
577 /* Maestro 2 */
578 { 0x125d, 0x1968, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, TYPE_MAESTRO2 },
579 /* Maestro 2E */
580 { 0x125d, 0x1978, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, TYPE_MAESTRO2E },
581 { 0, }
582};
583
584MODULE_DEVICE_TABLE(pci, snd_es1968_ids);
585
586/* *********************
587 * Low Level Funcs! *
588 *********************/
589
590/* no spinlock */
591static void __maestro_write(struct es1968 *chip, u16 reg, u16 data)
592{
593 outw(reg, chip->io_port + ESM_INDEX);
594 outw(data, chip->io_port + ESM_DATA);
595 chip->maestro_map[reg] = data;
596}
597
598static inline void maestro_write(struct es1968 *chip, u16 reg, u16 data)
599{
600 unsigned long flags;
601 spin_lock_irqsave(&chip->reg_lock, flags);
602 __maestro_write(chip, reg, data);
603 spin_unlock_irqrestore(&chip->reg_lock, flags);
604}
605
606/* no spinlock */
607static u16 __maestro_read(struct es1968 *chip, u16 reg)
608{
609 if (READABLE_MAP & (1 << reg)) {
610 outw(reg, chip->io_port + ESM_INDEX);
611 chip->maestro_map[reg] = inw(chip->io_port + ESM_DATA);
612 }
613 return chip->maestro_map[reg];
614}
615
616static inline u16 maestro_read(struct es1968 *chip, u16 reg)
617{
618 unsigned long flags;
619 u16 result;
620 spin_lock_irqsave(&chip->reg_lock, flags);
621 result = __maestro_read(chip, reg);
622 spin_unlock_irqrestore(&chip->reg_lock, flags);
623 return result;
624}
625
626/* Wait for the codec bus to be free */
627static int snd_es1968_ac97_wait(struct es1968 *chip)
628{
629 int timeout = 100000;
630
631 while (timeout-- > 0) {
632 if (!(inb(chip->io_port + ESM_AC97_INDEX) & 1))
633 return 0;
634 cond_resched();
635 }
636 dev_dbg(chip->card->dev, "ac97 timeout\n");
637 return 1; /* timeout */
638}
639
640static int snd_es1968_ac97_wait_poll(struct es1968 *chip)
641{
642 int timeout = 100000;
643
644 while (timeout-- > 0) {
645 if (!(inb(chip->io_port + ESM_AC97_INDEX) & 1))
646 return 0;
647 }
648 dev_dbg(chip->card->dev, "ac97 timeout\n");
649 return 1; /* timeout */
650}
651
652static void snd_es1968_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val)
653{
654 struct es1968 *chip = ac97->private_data;
655
656 snd_es1968_ac97_wait(chip);
657
658 /* Write the bus */
659 outw(val, chip->io_port + ESM_AC97_DATA);
660 /*msleep(1);*/
661 outb(reg, chip->io_port + ESM_AC97_INDEX);
662 /*msleep(1);*/
663}
664
665static unsigned short snd_es1968_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
666{
667 u16 data = 0;
668 struct es1968 *chip = ac97->private_data;
669
670 snd_es1968_ac97_wait(chip);
671
672 outb(reg | 0x80, chip->io_port + ESM_AC97_INDEX);
673 /*msleep(1);*/
674
675 if (!snd_es1968_ac97_wait_poll(chip)) {
676 data = inw(chip->io_port + ESM_AC97_DATA);
677 /*msleep(1);*/
678 }
679
680 return data;
681}
682
683/* no spinlock */
684static void apu_index_set(struct es1968 *chip, u16 index)
685{
686 int i;
687 __maestro_write(chip, IDR1_CRAM_POINTER, index);
688 for (i = 0; i < 1000; i++)
689 if (__maestro_read(chip, IDR1_CRAM_POINTER) == index)
690 return;
691 dev_dbg(chip->card->dev, "APU register select failed. (Timeout)\n");
692}
693
694/* no spinlock */
695static void apu_data_set(struct es1968 *chip, u16 data)
696{
697 int i;
698 for (i = 0; i < 1000; i++) {
699 if (__maestro_read(chip, IDR0_DATA_PORT) == data)
700 return;
701 __maestro_write(chip, IDR0_DATA_PORT, data);
702 }
703 dev_dbg(chip->card->dev, "APU register set probably failed (Timeout)!\n");
704}
705
706/* no spinlock */
707static void __apu_set_register(struct es1968 *chip, u16 channel, u8 reg, u16 data)
708{
709 if (snd_BUG_ON(channel >= NR_APUS))
710 return;
711#ifdef CONFIG_PM_SLEEP
712 chip->apu_map[channel][reg] = data;
713#endif
714 reg |= (channel << 4);
715 apu_index_set(chip, reg);
716 apu_data_set(chip, data);
717}
718
719static void apu_set_register(struct es1968 *chip, u16 channel, u8 reg, u16 data)
720{
721 unsigned long flags;
722 spin_lock_irqsave(&chip->reg_lock, flags);
723 __apu_set_register(chip, channel, reg, data);
724 spin_unlock_irqrestore(&chip->reg_lock, flags);
725}
726
727static u16 __apu_get_register(struct es1968 *chip, u16 channel, u8 reg)
728{
729 if (snd_BUG_ON(channel >= NR_APUS))
730 return 0;
731 reg |= (channel << 4);
732 apu_index_set(chip, reg);
733 return __maestro_read(chip, IDR0_DATA_PORT);
734}
735
736static u16 apu_get_register(struct es1968 *chip, u16 channel, u8 reg)
737{
738 unsigned long flags;
739 u16 v;
740 spin_lock_irqsave(&chip->reg_lock, flags);
741 v = __apu_get_register(chip, channel, reg);
742 spin_unlock_irqrestore(&chip->reg_lock, flags);
743 return v;
744}
745
746#if 0 /* ASSP is not supported */
747
748static void assp_set_register(struct es1968 *chip, u32 reg, u32 value)
749{
750 unsigned long flags;
751
752 spin_lock_irqsave(&chip->reg_lock, flags);
753 outl(reg, chip->io_port + ASSP_INDEX);
754 outl(value, chip->io_port + ASSP_DATA);
755 spin_unlock_irqrestore(&chip->reg_lock, flags);
756}
757
758static u32 assp_get_register(struct es1968 *chip, u32 reg)
759{
760 unsigned long flags;
761 u32 value;
762
763 spin_lock_irqsave(&chip->reg_lock, flags);
764 outl(reg, chip->io_port + ASSP_INDEX);
765 value = inl(chip->io_port + ASSP_DATA);
766 spin_unlock_irqrestore(&chip->reg_lock, flags);
767
768 return value;
769}
770
771#endif
772
773static void wave_set_register(struct es1968 *chip, u16 reg, u16 value)
774{
775 unsigned long flags;
776
777 spin_lock_irqsave(&chip->reg_lock, flags);
778 outw(reg, chip->io_port + WC_INDEX);
779 outw(value, chip->io_port + WC_DATA);
780 spin_unlock_irqrestore(&chip->reg_lock, flags);
781}
782
783static u16 wave_get_register(struct es1968 *chip, u16 reg)
784{
785 unsigned long flags;
786 u16 value;
787
788 spin_lock_irqsave(&chip->reg_lock, flags);
789 outw(reg, chip->io_port + WC_INDEX);
790 value = inw(chip->io_port + WC_DATA);
791 spin_unlock_irqrestore(&chip->reg_lock, flags);
792
793 return value;
794}
795
796/* *******************
797 * Bob the Timer! *
798 *******************/
799
800static void snd_es1968_bob_stop(struct es1968 *chip)
801{
802 u16 reg;
803
804 reg = __maestro_read(chip, 0x11);
805 reg &= ~ESM_BOB_ENABLE;
806 __maestro_write(chip, 0x11, reg);
807 reg = __maestro_read(chip, 0x17);
808 reg &= ~ESM_BOB_START;
809 __maestro_write(chip, 0x17, reg);
810}
811
812static void snd_es1968_bob_start(struct es1968 *chip)
813{
814 int prescale;
815 int divide;
816
817 /* compute ideal interrupt frequency for buffer size & play rate */
818 /* first, find best prescaler value to match freq */
819 for (prescale = 5; prescale < 12; prescale++)
820 if (chip->bob_freq > (ESS_SYSCLK >> (prescale + 9)))
821 break;
822
823 /* next, back off prescaler whilst getting divider into optimum range */
824 divide = 1;
825 while ((prescale > 5) && (divide < 32)) {
826 prescale--;
827 divide <<= 1;
828 }
829 divide >>= 1;
830
831 /* now fine-tune the divider for best match */
832 for (; divide < 31; divide++)
833 if (chip->bob_freq >
834 ((ESS_SYSCLK >> (prescale + 9)) / (divide + 1))) break;
835
836 /* divide = 0 is illegal, but don't let prescale = 4! */
837 if (divide == 0) {
838 divide++;
839 if (prescale > 5)
840 prescale--;
841 } else if (divide > 1)
842 divide--;
843
844 __maestro_write(chip, 6, 0x9000 | (prescale << 5) | divide); /* set reg */
845
846 /* Now set IDR 11/17 */
847 __maestro_write(chip, 0x11, __maestro_read(chip, 0x11) | 1);
848 __maestro_write(chip, 0x17, __maestro_read(chip, 0x17) | 1);
849}
850
851/* call with substream spinlock */
852static void snd_es1968_bob_inc(struct es1968 *chip, int freq)
853{
854 chip->bobclient++;
855 if (chip->bobclient == 1) {
856 chip->bob_freq = freq;
857 snd_es1968_bob_start(chip);
858 } else if (chip->bob_freq < freq) {
859 snd_es1968_bob_stop(chip);
860 chip->bob_freq = freq;
861 snd_es1968_bob_start(chip);
862 }
863}
864
865/* call with substream spinlock */
866static void snd_es1968_bob_dec(struct es1968 *chip)
867{
868 chip->bobclient--;
869 if (chip->bobclient <= 0)
870 snd_es1968_bob_stop(chip);
871 else if (chip->bob_freq > ESM_BOB_FREQ) {
872 /* check reduction of timer frequency */
873 int max_freq = ESM_BOB_FREQ;
874 struct esschan *es;
875 list_for_each_entry(es, &chip->substream_list, list, struct esschan) {
876 if (max_freq < es->bob_freq)
877 max_freq = es->bob_freq;
878 }
879 if (max_freq != chip->bob_freq) {
880 snd_es1968_bob_stop(chip);
881 chip->bob_freq = max_freq;
882 snd_es1968_bob_start(chip);
883 }
884 }
885}
886
887static int
888snd_es1968_calc_bob_rate(struct es1968 *chip, struct esschan *es,
889 struct snd_pcm_runtime *runtime)
890{
891 /* we acquire 4 interrupts per period for precise control.. */
892 int freq = runtime->rate * 4;
893 if (es->fmt & ESS_FMT_STEREO)
894 freq <<= 1;
895 if (es->fmt & ESS_FMT_16BIT)
896 freq <<= 1;
897 freq /= es->frag_size;
898 if (freq < ESM_BOB_FREQ)
899 freq = ESM_BOB_FREQ;
900 else if (freq > ESM_BOB_FREQ_MAX)
901 freq = ESM_BOB_FREQ_MAX;
902 return freq;
903}
904
905
906/*************
907 * PCM Part *
908 *************/
909
910static u32 snd_es1968_compute_rate(struct es1968 *chip, u32 freq)
911{
912 u32 rate = (freq << 16) / chip->clock;
913#if 0 /* XXX: do we need this? */
914 if (rate > 0x10000)
915 rate = 0x10000;
916#endif
917 return rate;
918}
919
920/* get current pointer */
921static inline unsigned int
922snd_es1968_get_dma_ptr(struct es1968 *chip, struct esschan *es)
923{
924 unsigned int offset;
925
926 offset = apu_get_register(chip, es->apu[0], 5);
927
928 offset -= es->base[0];
929
930 return (offset & 0xFFFE); /* hardware is in words */
931}
932
933static void snd_es1968_apu_set_freq(struct es1968 *chip, int apu, int freq)
934{
935 apu_set_register(chip, apu, 2,
936 (apu_get_register(chip, apu, 2) & 0x00FF) |
937 ((freq & 0xff) << 8) | 0x10);
938 apu_set_register(chip, apu, 3, freq >> 8);
939}
940
941/* spin lock held */
942static inline void snd_es1968_trigger_apu(struct es1968 *esm, int apu, int mode)
943{
944 /* set the APU mode */
945 __apu_set_register(esm, apu, 0,
946 (__apu_get_register(esm, apu, 0) & 0xff0f) |
947 (mode << 4));
948}
949
950static void snd_es1968_pcm_start(struct es1968 *chip, struct esschan *es)
951{
952 spin_lock(&chip->reg_lock);
953 __apu_set_register(chip, es->apu[0], 5, es->base[0]);
954 snd_es1968_trigger_apu(chip, es->apu[0], es->apu_mode[0]);
955 if (es->mode == ESM_MODE_CAPTURE) {
956 __apu_set_register(chip, es->apu[2], 5, es->base[2]);
957 snd_es1968_trigger_apu(chip, es->apu[2], es->apu_mode[2]);
958 }
959 if (es->fmt & ESS_FMT_STEREO) {
960 __apu_set_register(chip, es->apu[1], 5, es->base[1]);
961 snd_es1968_trigger_apu(chip, es->apu[1], es->apu_mode[1]);
962 if (es->mode == ESM_MODE_CAPTURE) {
963 __apu_set_register(chip, es->apu[3], 5, es->base[3]);
964 snd_es1968_trigger_apu(chip, es->apu[3], es->apu_mode[3]);
965 }
966 }
967 spin_unlock(&chip->reg_lock);
968}
969
970static void snd_es1968_pcm_stop(struct es1968 *chip, struct esschan *es)
971{
972 spin_lock(&chip->reg_lock);
973 snd_es1968_trigger_apu(chip, es->apu[0], 0);
974 snd_es1968_trigger_apu(chip, es->apu[1], 0);
975 if (es->mode == ESM_MODE_CAPTURE) {
976 snd_es1968_trigger_apu(chip, es->apu[2], 0);
977 snd_es1968_trigger_apu(chip, es->apu[3], 0);
978 }
979 spin_unlock(&chip->reg_lock);
980}
981
982/* set the wavecache control reg */
983static void snd_es1968_program_wavecache(struct es1968 *chip, struct esschan *es,
984 int channel, u32 addr, int capture)
985{
986 u32 tmpval = (addr - 0x10) & 0xFFF8;
987
988 if (! capture) {
989 if (!(es->fmt & ESS_FMT_16BIT))
990 tmpval |= 4; /* 8bit */
991 if (es->fmt & ESS_FMT_STEREO)
992 tmpval |= 2; /* stereo */
993 }
994
995 /* set the wavecache control reg */
996 wave_set_register(chip, es->apu[channel] << 3, tmpval);
997
998#ifdef CONFIG_PM_SLEEP
999 es->wc_map[channel] = tmpval;
1000#endif
1001}
1002
1003
1004static void snd_es1968_playback_setup(struct es1968 *chip, struct esschan *es,
1005 struct snd_pcm_runtime *runtime)
1006{
1007 u32 pa;
1008 int high_apu = 0;
1009 int channel, apu;
1010 int i, size;
1011 unsigned long flags;
1012 u32 freq;
1013
1014 size = es->dma_size >> es->wav_shift;
1015
1016 if (es->fmt & ESS_FMT_STEREO)
1017 high_apu++;
1018
1019 for (channel = 0; channel <= high_apu; channel++) {
1020 apu = es->apu[channel];
1021
1022 snd_es1968_program_wavecache(chip, es, channel, es->memory->buf.addr, 0);
1023
1024 /* Offset to PCMBAR */
1025 pa = es->memory->buf.addr;
1026 pa -= chip->dma.addr;
1027 pa >>= 1; /* words */
1028
1029 pa |= 0x00400000; /* System RAM (Bit 22) */
1030
1031 if (es->fmt & ESS_FMT_STEREO) {
1032 /* Enable stereo */
1033 if (channel)
1034 pa |= 0x00800000; /* (Bit 23) */
1035 if (es->fmt & ESS_FMT_16BIT)
1036 pa >>= 1;
1037 }
1038
1039 /* base offset of dma calcs when reading the pointer
1040 on this left one */
1041 es->base[channel] = pa & 0xFFFF;
1042
1043 for (i = 0; i < 16; i++)
1044 apu_set_register(chip, apu, i, 0x0000);
1045
1046 /* Load the buffer into the wave engine */
1047 apu_set_register(chip, apu, 4, ((pa >> 16) & 0xFF) << 8);
1048 apu_set_register(chip, apu, 5, pa & 0xFFFF);
1049 apu_set_register(chip, apu, 6, (pa + size) & 0xFFFF);
1050 /* setting loop == sample len */
1051 apu_set_register(chip, apu, 7, size);
1052
1053 /* clear effects/env.. */
1054 apu_set_register(chip, apu, 8, 0x0000);
1055 /* set amp now to 0xd0 (?), low byte is 'amplitude dest'? */
1056 apu_set_register(chip, apu, 9, 0xD000);
1057
1058 /* clear routing stuff */
1059 apu_set_register(chip, apu, 11, 0x0000);
1060 /* dma on, no envelopes, filter to all 1s) */
1061 apu_set_register(chip, apu, 0, 0x400F);
1062
1063 if (es->fmt & ESS_FMT_16BIT)
1064 es->apu_mode[channel] = ESM_APU_16BITLINEAR;
1065 else
1066 es->apu_mode[channel] = ESM_APU_8BITLINEAR;
1067
1068 if (es->fmt & ESS_FMT_STEREO) {
1069 /* set panning: left or right */
1070 /* Check: different panning. On my Canyon 3D Chipset the
1071 Channels are swapped. I don't know, about the output
1072 to the SPDif Link. Perhaps you have to change this
1073 and not the APU Regs 4-5. */
1074 apu_set_register(chip, apu, 10,
1075 0x8F00 | (channel ? 0 : 0x10));
1076 es->apu_mode[channel] += 1; /* stereo */
1077 } else
1078 apu_set_register(chip, apu, 10, 0x8F08);
1079 }
1080
1081 spin_lock_irqsave(&chip->reg_lock, flags);
1082 /* clear WP interrupts */
1083 outw(1, chip->io_port + 0x04);
1084 /* enable WP ints */
1085 outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ);
1086 spin_unlock_irqrestore(&chip->reg_lock, flags);
1087
1088 freq = runtime->rate;
1089 /* set frequency */
1090 if (freq > 48000)
1091 freq = 48000;
1092 if (freq < 4000)
1093 freq = 4000;
1094
1095 /* hmmm.. */
1096 if (!(es->fmt & ESS_FMT_16BIT) && !(es->fmt & ESS_FMT_STEREO))
1097 freq >>= 1;
1098
1099 freq = snd_es1968_compute_rate(chip, freq);
1100
1101 /* Load the frequency, turn on 6dB */
1102 snd_es1968_apu_set_freq(chip, es->apu[0], freq);
1103 snd_es1968_apu_set_freq(chip, es->apu[1], freq);
1104}
1105
1106
1107static void init_capture_apu(struct es1968 *chip, struct esschan *es, int channel,
1108 unsigned int pa, unsigned int bsize,
1109 int mode, int route)
1110{
1111 int i, apu = es->apu[channel];
1112
1113 es->apu_mode[channel] = mode;
1114
1115 /* set the wavecache control reg */
1116 snd_es1968_program_wavecache(chip, es, channel, pa, 1);
1117
1118 /* Offset to PCMBAR */
1119 pa -= chip->dma.addr;
1120 pa >>= 1; /* words */
1121
1122 /* base offset of dma calcs when reading the pointer
1123 on this left one */
1124 es->base[channel] = pa & 0xFFFF;
1125 pa |= 0x00400000; /* bit 22 -> System RAM */
1126
1127 /* Begin loading the APU */
1128 for (i = 0; i < 16; i++)
1129 apu_set_register(chip, apu, i, 0x0000);
1130
1131 /* need to enable subgroups.. and we should probably
1132 have different groups for different /dev/dsps.. */
1133 apu_set_register(chip, apu, 2, 0x8);
1134
1135 /* Load the buffer into the wave engine */
1136 apu_set_register(chip, apu, 4, ((pa >> 16) & 0xFF) << 8);
1137 apu_set_register(chip, apu, 5, pa & 0xFFFF);
1138 apu_set_register(chip, apu, 6, (pa + bsize) & 0xFFFF);
1139 apu_set_register(chip, apu, 7, bsize);
1140 /* clear effects/env.. */
1141 apu_set_register(chip, apu, 8, 0x00F0);
1142 /* amplitude now? sure. why not. */
1143 apu_set_register(chip, apu, 9, 0x0000);
1144 /* set filter tune, radius, polar pan */
1145 apu_set_register(chip, apu, 10, 0x8F08);
1146 /* route input */
1147 apu_set_register(chip, apu, 11, route);
1148 /* dma on, no envelopes, filter to all 1s) */
1149 apu_set_register(chip, apu, 0, 0x400F);
1150}
1151
1152static void snd_es1968_capture_setup(struct es1968 *chip, struct esschan *es,
1153 struct snd_pcm_runtime *runtime)
1154{
1155 int size;
1156 u32 freq;
1157 unsigned long flags;
1158
1159 size = es->dma_size >> es->wav_shift;
1160
1161 /* APU assignments:
1162 0 = mono/left SRC
1163 1 = right SRC
1164 2 = mono/left Input Mixer
1165 3 = right Input Mixer
1166 */
1167 /* data seems to flow from the codec, through an apu into
1168 the 'mixbuf' bit of page, then through the SRC apu
1169 and out to the real 'buffer'. ok. sure. */
1170
1171 /* input mixer (left/mono) */
1172 /* parallel in crap, see maestro reg 0xC [8-11] */
1173 init_capture_apu(chip, es, 2,
1174 es->mixbuf->buf.addr, ESM_MIXBUF_SIZE/4, /* in words */
1175 ESM_APU_INPUTMIXER, 0x14);
1176 /* SRC (left/mono); get input from inputing apu */
1177 init_capture_apu(chip, es, 0, es->memory->buf.addr, size,
1178 ESM_APU_SRCONVERTOR, es->apu[2]);
1179 if (es->fmt & ESS_FMT_STEREO) {
1180 /* input mixer (right) */
1181 init_capture_apu(chip, es, 3,
1182 es->mixbuf->buf.addr + ESM_MIXBUF_SIZE/2,
1183 ESM_MIXBUF_SIZE/4, /* in words */
1184 ESM_APU_INPUTMIXER, 0x15);
1185 /* SRC (right) */
1186 init_capture_apu(chip, es, 1,
1187 es->memory->buf.addr + size*2, size,
1188 ESM_APU_SRCONVERTOR, es->apu[3]);
1189 }
1190
1191 freq = runtime->rate;
1192 /* Sample Rate conversion APUs don't like 0x10000 for their rate */
1193 if (freq > 47999)
1194 freq = 47999;
1195 if (freq < 4000)
1196 freq = 4000;
1197
1198 freq = snd_es1968_compute_rate(chip, freq);
1199
1200 /* Load the frequency, turn on 6dB */
1201 snd_es1968_apu_set_freq(chip, es->apu[0], freq);
1202 snd_es1968_apu_set_freq(chip, es->apu[1], freq);
1203
1204 /* fix mixer rate at 48khz. and its _must_ be 0x10000. */
1205 freq = 0x10000;
1206 snd_es1968_apu_set_freq(chip, es->apu[2], freq);
1207 snd_es1968_apu_set_freq(chip, es->apu[3], freq);
1208
1209 spin_lock_irqsave(&chip->reg_lock, flags);
1210 /* clear WP interrupts */
1211 outw(1, chip->io_port + 0x04);
1212 /* enable WP ints */
1213 outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ);
1214 spin_unlock_irqrestore(&chip->reg_lock, flags);
1215}
1216
1217/*******************
1218 * ALSA Interface *
1219 *******************/
1220
1221static int snd_es1968_pcm_prepare(struct snd_pcm_substream *substream)
1222{
1223 struct es1968 *chip = snd_pcm_substream_chip(substream);
1224 struct snd_pcm_runtime *runtime = substream->runtime;
1225 struct esschan *es = runtime->private_data;
1226
1227 es->dma_size = snd_pcm_lib_buffer_bytes(substream);
1228 es->frag_size = snd_pcm_lib_period_bytes(substream);
1229
1230 es->wav_shift = 1; /* maestro handles always 16bit */
1231 es->fmt = 0;
1232 if (snd_pcm_format_width(runtime->format) == 16)
1233 es->fmt |= ESS_FMT_16BIT;
1234 if (runtime->channels > 1) {
1235 es->fmt |= ESS_FMT_STEREO;
1236 if (es->fmt & ESS_FMT_16BIT) /* 8bit is already word shifted */
1237 es->wav_shift++;
1238 }
1239 es->bob_freq = snd_es1968_calc_bob_rate(chip, es, runtime);
1240
1241 switch (es->mode) {
1242 case ESM_MODE_PLAY:
1243 snd_es1968_playback_setup(chip, es, runtime);
1244 break;
1245 case ESM_MODE_CAPTURE:
1246 snd_es1968_capture_setup(chip, es, runtime);
1247 break;
1248 }
1249
1250 return 0;
1251}
1252
1253static int snd_es1968_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1254{
1255 struct es1968 *chip = snd_pcm_substream_chip(substream);
1256 struct esschan *es = substream->runtime->private_data;
1257
1258 spin_lock(&chip->substream_lock);
1259 switch (cmd) {
1260 case SNDRV_PCM_TRIGGER_START:
1261 case SNDRV_PCM_TRIGGER_RESUME:
1262 if (es->running)
1263 break;
1264 snd_es1968_bob_inc(chip, es->bob_freq);
1265 es->count = 0;
1266 es->hwptr = 0;
1267 snd_es1968_pcm_start(chip, es);
1268 es->running = 1;
1269 break;
1270 case SNDRV_PCM_TRIGGER_STOP:
1271 case SNDRV_PCM_TRIGGER_SUSPEND:
1272 if (! es->running)
1273 break;
1274 snd_es1968_pcm_stop(chip, es);
1275 es->running = 0;
1276 snd_es1968_bob_dec(chip);
1277 break;
1278 }
1279 spin_unlock(&chip->substream_lock);
1280 return 0;
1281}
1282
1283static snd_pcm_uframes_t snd_es1968_pcm_pointer(struct snd_pcm_substream *substream)
1284{
1285 struct es1968 *chip = snd_pcm_substream_chip(substream);
1286 struct esschan *es = substream->runtime->private_data;
1287 unsigned int ptr;
1288
1289 ptr = snd_es1968_get_dma_ptr(chip, es) << es->wav_shift;
1290
1291 return bytes_to_frames(substream->runtime, ptr % es->dma_size);
1292}
1293
1294static const struct snd_pcm_hardware snd_es1968_playback = {
1295 .info = (SNDRV_PCM_INFO_MMAP |
1296 SNDRV_PCM_INFO_MMAP_VALID |
1297 SNDRV_PCM_INFO_INTERLEAVED |
1298 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1299 /*SNDRV_PCM_INFO_PAUSE |*/
1300 SNDRV_PCM_INFO_RESUME),
1301 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1302 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1303 .rate_min = 4000,
1304 .rate_max = 48000,
1305 .channels_min = 1,
1306 .channels_max = 2,
1307 .buffer_bytes_max = 65536,
1308 .period_bytes_min = 256,
1309 .period_bytes_max = 65536,
1310 .periods_min = 1,
1311 .periods_max = 1024,
1312 .fifo_size = 0,
1313};
1314
1315static const struct snd_pcm_hardware snd_es1968_capture = {
1316 .info = (SNDRV_PCM_INFO_NONINTERLEAVED |
1317 SNDRV_PCM_INFO_MMAP |
1318 SNDRV_PCM_INFO_MMAP_VALID |
1319 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1320 /*SNDRV_PCM_INFO_PAUSE |*/
1321 SNDRV_PCM_INFO_RESUME),
1322 .formats = /*SNDRV_PCM_FMTBIT_U8 |*/ SNDRV_PCM_FMTBIT_S16_LE,
1323 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1324 .rate_min = 4000,
1325 .rate_max = 48000,
1326 .channels_min = 1,
1327 .channels_max = 2,
1328 .buffer_bytes_max = 65536,
1329 .period_bytes_min = 256,
1330 .period_bytes_max = 65536,
1331 .periods_min = 1,
1332 .periods_max = 1024,
1333 .fifo_size = 0,
1334};
1335
1336/* *************************
1337 * DMA memory management *
1338 *************************/
1339
1340/* Because the Maestro can only take addresses relative to the PCM base address
1341 register :( */
1342
1343static int calc_available_memory_size(struct es1968 *chip)
1344{
1345 int max_size = 0;
1346 struct esm_memory *buf;
1347
1348 mutex_lock(&chip->memory_mutex);
1349 list_for_each_entry(buf, &chip->buf_list, list, struct esm_memory) {
1350 if (buf->empty && buf->buf.bytes > max_size)
1351 max_size = buf->buf.bytes;
1352 }
1353 mutex_unlock(&chip->memory_mutex);
1354 if (max_size >= 128*1024)
1355 max_size = 127*1024;
1356 return max_size;
1357}
1358
1359/* allocate a new memory chunk with the specified size */
1360static struct esm_memory *snd_es1968_new_memory(struct es1968 *chip, int size)
1361{
1362 struct esm_memory *buf;
1363
1364 size = ALIGN(size, ESM_MEM_ALIGN);
1365 mutex_lock(&chip->memory_mutex);
1366 list_for_each_entry(buf, &chip->buf_list, list, struct esm_memory) {
1367 if (buf->empty && buf->buf.bytes >= size)
1368 goto __found;
1369 }
1370 mutex_unlock(&chip->memory_mutex);
1371 return NULL;
1372
1373__found:
1374 if (buf->buf.bytes > size) {
1375 struct esm_memory *chunk = kmalloc(sizeof(*chunk), GFP_KERNEL);
1376 if (chunk == NULL) {
1377 mutex_unlock(&chip->memory_mutex);
1378 return NULL;
1379 }
1380 chunk->buf = buf->buf;
1381 chunk->buf.bytes -= size;
1382 chunk->buf.area += size;
1383 chunk->buf.addr += size;
1384 chunk->empty = 1;
1385 buf->buf.bytes = size;
1386 list_add(&chunk->list, &buf->list);
1387 }
1388 buf->empty = 0;
1389 mutex_unlock(&chip->memory_mutex);
1390 return buf;
1391}
1392
1393/* free a memory chunk */
1394static void snd_es1968_free_memory(struct es1968 *chip, struct esm_memory *buf)
1395{
1396 struct esm_memory *chunk;
1397
1398 mutex_lock(&chip->memory_mutex);
1399 buf->empty = 1;
1400 if (buf->list.prev != &chip->buf_list) {
1401 chunk = list_entry(buf->list.prev, struct esm_memory, list);
1402 if (chunk->empty) {
1403 chunk->buf.bytes += buf->buf.bytes;
1404 list_del(&buf->list);
1405 kfree(buf);
1406 buf = chunk;
1407 }
1408 }
1409 if (buf->list.next != &chip->buf_list) {
1410 chunk = list_entry(buf->list.next, struct esm_memory, list);
1411 if (chunk->empty) {
1412 buf->buf.bytes += chunk->buf.bytes;
1413 list_del(&chunk->list);
1414 kfree(chunk);
1415 }
1416 }
1417 mutex_unlock(&chip->memory_mutex);
1418}
1419
1420static void snd_es1968_free_dmabuf(struct es1968 *chip)
1421{
1422 struct list_head *p;
1423
1424 if (! chip->dma.area)
1425 return;
1426 snd_dma_free_pages(&chip->dma);
1427 while ((p = chip->buf_list.next) != &chip->buf_list) {
1428 struct esm_memory *chunk = list_entry(p, struct esm_memory, list);
1429 list_del(p);
1430 kfree(chunk);
1431 }
1432}
1433
1434static int
1435snd_es1968_init_dmabuf(struct es1968 *chip)
1436{
1437 int err;
1438 struct esm_memory *chunk;
1439
1440 err = snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV,
1441 &chip->pci->dev,
1442 chip->total_bufsize, &chip->dma);
1443 if (err < 0 || ! chip->dma.area) {
1444 dev_err(chip->card->dev,
1445 "can't allocate dma pages for size %d\n",
1446 chip->total_bufsize);
1447 return -ENOMEM;
1448 }
1449 if ((chip->dma.addr + chip->dma.bytes - 1) & ~((1 << 28) - 1)) {
1450 snd_dma_free_pages(&chip->dma);
1451 dev_err(chip->card->dev, "DMA buffer beyond 256MB.\n");
1452 return -ENOMEM;
1453 }
1454
1455 INIT_LIST_HEAD(&chip->buf_list);
1456 /* allocate an empty chunk */
1457 chunk = kmalloc(sizeof(*chunk), GFP_KERNEL);
1458 if (chunk == NULL) {
1459 snd_es1968_free_dmabuf(chip);
1460 return -ENOMEM;
1461 }
1462 memset(chip->dma.area, 0, ESM_MEM_ALIGN);
1463 chunk->buf = chip->dma;
1464 chunk->buf.area += ESM_MEM_ALIGN;
1465 chunk->buf.addr += ESM_MEM_ALIGN;
1466 chunk->buf.bytes -= ESM_MEM_ALIGN;
1467 chunk->empty = 1;
1468 list_add(&chunk->list, &chip->buf_list);
1469
1470 return 0;
1471}
1472
1473/* setup the dma_areas */
1474/* buffer is extracted from the pre-allocated memory chunk */
1475static int snd_es1968_hw_params(struct snd_pcm_substream *substream,
1476 struct snd_pcm_hw_params *hw_params)
1477{
1478 struct es1968 *chip = snd_pcm_substream_chip(substream);
1479 struct snd_pcm_runtime *runtime = substream->runtime;
1480 struct esschan *chan = runtime->private_data;
1481 int size = params_buffer_bytes(hw_params);
1482
1483 if (chan->memory) {
1484 if (chan->memory->buf.bytes >= size) {
1485 runtime->dma_bytes = size;
1486 return 0;
1487 }
1488 snd_es1968_free_memory(chip, chan->memory);
1489 }
1490 chan->memory = snd_es1968_new_memory(chip, size);
1491 if (chan->memory == NULL) {
1492 dev_dbg(chip->card->dev,
1493 "cannot allocate dma buffer: size = %d\n", size);
1494 return -ENOMEM;
1495 }
1496 snd_pcm_set_runtime_buffer(substream, &chan->memory->buf);
1497 return 1; /* area was changed */
1498}
1499
1500/* remove dma areas if allocated */
1501static int snd_es1968_hw_free(struct snd_pcm_substream *substream)
1502{
1503 struct es1968 *chip = snd_pcm_substream_chip(substream);
1504 struct snd_pcm_runtime *runtime = substream->runtime;
1505 struct esschan *chan;
1506
1507 if (runtime->private_data == NULL)
1508 return 0;
1509 chan = runtime->private_data;
1510 if (chan->memory) {
1511 snd_es1968_free_memory(chip, chan->memory);
1512 chan->memory = NULL;
1513 }
1514 return 0;
1515}
1516
1517
1518/*
1519 * allocate APU pair
1520 */
1521static int snd_es1968_alloc_apu_pair(struct es1968 *chip, int type)
1522{
1523 int apu;
1524
1525 for (apu = 0; apu < NR_APUS; apu += 2) {
1526 if (chip->apu[apu] == ESM_APU_FREE &&
1527 chip->apu[apu + 1] == ESM_APU_FREE) {
1528 chip->apu[apu] = chip->apu[apu + 1] = type;
1529 return apu;
1530 }
1531 }
1532 return -EBUSY;
1533}
1534
1535/*
1536 * release APU pair
1537 */
1538static void snd_es1968_free_apu_pair(struct es1968 *chip, int apu)
1539{
1540 chip->apu[apu] = chip->apu[apu + 1] = ESM_APU_FREE;
1541}
1542
1543
1544/******************
1545 * PCM open/close *
1546 ******************/
1547
1548static int snd_es1968_playback_open(struct snd_pcm_substream *substream)
1549{
1550 struct es1968 *chip = snd_pcm_substream_chip(substream);
1551 struct snd_pcm_runtime *runtime = substream->runtime;
1552 struct esschan *es;
1553 int apu1;
1554
1555 /* search 2 APUs */
1556 apu1 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_PLAY);
1557 if (apu1 < 0)
1558 return apu1;
1559
1560 es = kzalloc(sizeof(*es), GFP_KERNEL);
1561 if (!es) {
1562 snd_es1968_free_apu_pair(chip, apu1);
1563 return -ENOMEM;
1564 }
1565
1566 es->apu[0] = apu1;
1567 es->apu[1] = apu1 + 1;
1568 es->apu_mode[0] = 0;
1569 es->apu_mode[1] = 0;
1570 es->running = 0;
1571 es->substream = substream;
1572 es->mode = ESM_MODE_PLAY;
1573
1574 runtime->private_data = es;
1575 runtime->hw = snd_es1968_playback;
1576 runtime->hw.buffer_bytes_max = runtime->hw.period_bytes_max =
1577 calc_available_memory_size(chip);
1578
1579 spin_lock_irq(&chip->substream_lock);
1580 list_add(&es->list, &chip->substream_list);
1581 spin_unlock_irq(&chip->substream_lock);
1582
1583 return 0;
1584}
1585
1586static int snd_es1968_capture_open(struct snd_pcm_substream *substream)
1587{
1588 struct snd_pcm_runtime *runtime = substream->runtime;
1589 struct es1968 *chip = snd_pcm_substream_chip(substream);
1590 struct esschan *es;
1591 int apu1, apu2;
1592
1593 apu1 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_CAPTURE);
1594 if (apu1 < 0)
1595 return apu1;
1596 apu2 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_RATECONV);
1597 if (apu2 < 0) {
1598 snd_es1968_free_apu_pair(chip, apu1);
1599 return apu2;
1600 }
1601
1602 es = kzalloc(sizeof(*es), GFP_KERNEL);
1603 if (!es) {
1604 snd_es1968_free_apu_pair(chip, apu1);
1605 snd_es1968_free_apu_pair(chip, apu2);
1606 return -ENOMEM;
1607 }
1608
1609 es->apu[0] = apu1;
1610 es->apu[1] = apu1 + 1;
1611 es->apu[2] = apu2;
1612 es->apu[3] = apu2 + 1;
1613 es->apu_mode[0] = 0;
1614 es->apu_mode[1] = 0;
1615 es->apu_mode[2] = 0;
1616 es->apu_mode[3] = 0;
1617 es->running = 0;
1618 es->substream = substream;
1619 es->mode = ESM_MODE_CAPTURE;
1620
1621 /* get mixbuffer */
1622 if ((es->mixbuf = snd_es1968_new_memory(chip, ESM_MIXBUF_SIZE)) == NULL) {
1623 snd_es1968_free_apu_pair(chip, apu1);
1624 snd_es1968_free_apu_pair(chip, apu2);
1625 kfree(es);
1626 return -ENOMEM;
1627 }
1628 memset(es->mixbuf->buf.area, 0, ESM_MIXBUF_SIZE);
1629
1630 runtime->private_data = es;
1631 runtime->hw = snd_es1968_capture;
1632 runtime->hw.buffer_bytes_max = runtime->hw.period_bytes_max =
1633 calc_available_memory_size(chip) - 1024; /* keep MIXBUF size */
1634 snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1635
1636 spin_lock_irq(&chip->substream_lock);
1637 list_add(&es->list, &chip->substream_list);
1638 spin_unlock_irq(&chip->substream_lock);
1639
1640 return 0;
1641}
1642
1643static int snd_es1968_playback_close(struct snd_pcm_substream *substream)
1644{
1645 struct es1968 *chip = snd_pcm_substream_chip(substream);
1646 struct esschan *es;
1647
1648 if (substream->runtime->private_data == NULL)
1649 return 0;
1650 es = substream->runtime->private_data;
1651 spin_lock_irq(&chip->substream_lock);
1652 list_del(&es->list);
1653 spin_unlock_irq(&chip->substream_lock);
1654 snd_es1968_free_apu_pair(chip, es->apu[0]);
1655 kfree(es);
1656
1657 return 0;
1658}
1659
1660static int snd_es1968_capture_close(struct snd_pcm_substream *substream)
1661{
1662 struct es1968 *chip = snd_pcm_substream_chip(substream);
1663 struct esschan *es;
1664
1665 if (substream->runtime->private_data == NULL)
1666 return 0;
1667 es = substream->runtime->private_data;
1668 spin_lock_irq(&chip->substream_lock);
1669 list_del(&es->list);
1670 spin_unlock_irq(&chip->substream_lock);
1671 snd_es1968_free_memory(chip, es->mixbuf);
1672 snd_es1968_free_apu_pair(chip, es->apu[0]);
1673 snd_es1968_free_apu_pair(chip, es->apu[2]);
1674 kfree(es);
1675
1676 return 0;
1677}
1678
1679static const struct snd_pcm_ops snd_es1968_playback_ops = {
1680 .open = snd_es1968_playback_open,
1681 .close = snd_es1968_playback_close,
1682 .hw_params = snd_es1968_hw_params,
1683 .hw_free = snd_es1968_hw_free,
1684 .prepare = snd_es1968_pcm_prepare,
1685 .trigger = snd_es1968_pcm_trigger,
1686 .pointer = snd_es1968_pcm_pointer,
1687};
1688
1689static const struct snd_pcm_ops snd_es1968_capture_ops = {
1690 .open = snd_es1968_capture_open,
1691 .close = snd_es1968_capture_close,
1692 .hw_params = snd_es1968_hw_params,
1693 .hw_free = snd_es1968_hw_free,
1694 .prepare = snd_es1968_pcm_prepare,
1695 .trigger = snd_es1968_pcm_trigger,
1696 .pointer = snd_es1968_pcm_pointer,
1697};
1698
1699
1700/*
1701 * measure clock
1702 */
1703#define CLOCK_MEASURE_BUFSIZE 16768 /* enough large for a single shot */
1704
1705static void es1968_measure_clock(struct es1968 *chip)
1706{
1707 int i, apu;
1708 unsigned int pa, offset, t;
1709 struct esm_memory *memory;
1710#ifndef TARGET_OS2
1711 ktime_t start_time, stop_time;
1712 ktime_t diff;
1713#else
1714 struct timeval start_time, stop_time;
1715#endif
1716
1717 if (chip->clock == 0)
1718 chip->clock = 48000; /* default clock value */
1719
1720 /* search 2 APUs (although one apu is enough) */
1721 if ((apu = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_PLAY)) < 0) {
1722 dev_err(chip->card->dev, "Hmm, cannot find empty APU pair!?\n");
1723 return;
1724 }
1725 if ((memory = snd_es1968_new_memory(chip, CLOCK_MEASURE_BUFSIZE)) == NULL) {
1726 dev_warn(chip->card->dev,
1727 "cannot allocate dma buffer - using default clock %d\n",
1728 chip->clock);
1729 snd_es1968_free_apu_pair(chip, apu);
1730 return;
1731 }
1732
1733 memset(memory->buf.area, 0, CLOCK_MEASURE_BUFSIZE);
1734
1735 wave_set_register(chip, apu << 3, (memory->buf.addr - 0x10) & 0xfff8);
1736
1737 pa = (unsigned int)((memory->buf.addr - chip->dma.addr) >> 1);
1738 pa |= 0x00400000; /* System RAM (Bit 22) */
1739
1740 /* initialize apu */
1741 for (i = 0; i < 16; i++)
1742 apu_set_register(chip, apu, i, 0x0000);
1743
1744 apu_set_register(chip, apu, 0, 0x400f);
1745 apu_set_register(chip, apu, 4, ((pa >> 16) & 0xff) << 8);
1746 apu_set_register(chip, apu, 5, pa & 0xffff);
1747 apu_set_register(chip, apu, 6, (pa + CLOCK_MEASURE_BUFSIZE/2) & 0xffff);
1748 apu_set_register(chip, apu, 7, CLOCK_MEASURE_BUFSIZE/2);
1749 apu_set_register(chip, apu, 8, 0x0000);
1750 apu_set_register(chip, apu, 9, 0xD000);
1751 apu_set_register(chip, apu, 10, 0x8F08);
1752 apu_set_register(chip, apu, 11, 0x0000);
1753 spin_lock_irq(&chip->reg_lock);
1754 outw(1, chip->io_port + 0x04); /* clear WP interrupts */
1755 outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ); /* enable WP ints */
1756 spin_unlock_irq(&chip->reg_lock);
1757
1758 snd_es1968_apu_set_freq(chip, apu, ((unsigned int)48000 << 16) / chip->clock); /* 48000 Hz */
1759
1760 chip->in_measurement = 1;
1761 chip->measure_apu = apu;
1762 spin_lock_irq(&chip->reg_lock);
1763 snd_es1968_bob_inc(chip, ESM_BOB_FREQ);
1764 __apu_set_register(chip, apu, 5, pa & 0xffff);
1765 snd_es1968_trigger_apu(chip, apu, ESM_APU_16BITLINEAR);
1766#ifndef TARGET_OS2
1767 start_time = ktime_get();
1768#else
1769 do_gettimeofday(&start_time);
1770#endif
1771 spin_unlock_irq(&chip->reg_lock);
1772 msleep(50);
1773 spin_lock_irq(&chip->reg_lock);
1774 offset = __apu_get_register(chip, apu, 5);
1775#ifndef TARGET_OS2
1776 stop_time = ktime_get();
1777#else
1778 do_gettimeofday(&stop_time);
1779#endif
1780 snd_es1968_trigger_apu(chip, apu, 0); /* stop */
1781 snd_es1968_bob_dec(chip);
1782 chip->in_measurement = 0;
1783 spin_unlock_irq(&chip->reg_lock);
1784
1785 /* check the current position */
1786 offset -= (pa & 0xffff);
1787 offset &= 0xfffe;
1788 offset += chip->measure_count * (CLOCK_MEASURE_BUFSIZE/2);
1789
1790#ifndef TARGET_OS2
1791 diff = ktime_sub(stop_time, start_time);
1792 t = ktime_to_us(diff);
1793#else
1794 t = stop_time.tv_sec - start_time.tv_sec;
1795 t *= 1000000;
1796 if (stop_time.tv_usec < start_time.tv_usec)
1797 t -= start_time.tv_usec - stop_time.tv_usec;
1798 else
1799 t += stop_time.tv_usec - start_time.tv_usec;
1800#endif
1801 if (t == 0) {
1802 dev_err(chip->card->dev, "?? calculation error..\n");
1803 } else {
1804 offset *= 1000;
1805 offset = (offset / t) * 1000 + ((offset % t) * 1000) / t;
1806 if (offset < 47500 || offset > 48500) {
1807 if (offset >= 40000 && offset <= 50000)
1808 chip->clock = (chip->clock * offset) / 48000;
1809 }
1810 dev_info(chip->card->dev, "clocking to %d\n", chip->clock);
1811 }
1812 snd_es1968_free_memory(chip, memory);
1813 snd_es1968_free_apu_pair(chip, apu);
1814}
1815
1816
1817/*
1818 */
1819
1820static void snd_es1968_pcm_free(struct snd_pcm *pcm)
1821{
1822 struct es1968 *esm = pcm->private_data;
1823 snd_es1968_free_dmabuf(esm);
1824 esm->pcm = NULL;
1825}
1826
1827static int
1828snd_es1968_pcm(struct es1968 *chip, int device)
1829{
1830 struct snd_pcm *pcm;
1831 int err;
1832
1833 /* get DMA buffer */
1834 if ((err = snd_es1968_init_dmabuf(chip)) < 0)
1835 return err;
1836
1837 /* set PCMBAR */
1838 wave_set_register(chip, 0x01FC, chip->dma.addr >> 12);
1839 wave_set_register(chip, 0x01FD, chip->dma.addr >> 12);
1840 wave_set_register(chip, 0x01FE, chip->dma.addr >> 12);
1841 wave_set_register(chip, 0x01FF, chip->dma.addr >> 12);
1842
1843 if ((err = snd_pcm_new(chip->card, "ESS Maestro", device,
1844 chip->playback_streams,
1845 chip->capture_streams, &pcm)) < 0)
1846 return err;
1847
1848 pcm->private_data = chip;
1849 pcm->private_free = snd_es1968_pcm_free;
1850
1851 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1968_playback_ops);
1852 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1968_capture_ops);
1853
1854 pcm->info_flags = 0;
1855
1856 strcpy(pcm->name, "ESS Maestro");
1857
1858 chip->pcm = pcm;
1859
1860 return 0;
1861}
1862/*
1863 * suppress jitter on some maestros when playing stereo
1864 */
1865static void snd_es1968_suppress_jitter(struct es1968 *chip, struct esschan *es)
1866{
1867 unsigned int cp1;
1868 unsigned int cp2;
1869 unsigned int diff;
1870
1871 cp1 = __apu_get_register(chip, 0, 5);
1872 cp2 = __apu_get_register(chip, 1, 5);
1873 diff = (cp1 > cp2 ? cp1 - cp2 : cp2 - cp1);
1874
1875 if (diff > 1)
1876 __maestro_write(chip, IDR0_DATA_PORT, cp1);
1877}
1878
1879/*
1880 * update pointer
1881 */
1882static void snd_es1968_update_pcm(struct es1968 *chip, struct esschan *es)
1883{
1884 unsigned int hwptr;
1885 unsigned int diff;
1886 struct snd_pcm_substream *subs = es->substream;
1887
1888 if (subs == NULL || !es->running)
1889 return;
1890
1891 hwptr = snd_es1968_get_dma_ptr(chip, es) << es->wav_shift;
1892 hwptr %= es->dma_size;
1893
1894 diff = (es->dma_size + hwptr - es->hwptr) % es->dma_size;
1895
1896 es->hwptr = hwptr;
1897 es->count += diff;
1898
1899 if (es->count > es->frag_size) {
1900 spin_unlock(&chip->substream_lock);
1901 snd_pcm_period_elapsed(subs);
1902 spin_lock(&chip->substream_lock);
1903 es->count %= es->frag_size;
1904 }
1905}
1906
1907/* The hardware volume works by incrementing / decrementing 2 counters
1908 (without wrap around) in response to volume button presses and then
1909 generating an interrupt. The pair of counters is stored in bits 1-3 and 5-7
1910 of a byte wide register. The meaning of bits 0 and 4 is unknown. */
1911static void es1968_update_hw_volume(struct work_struct *work)
1912{
1913 struct es1968 *chip = container_of(work, struct es1968, hwvol_work);
1914 int x, val;
1915
1916 /* Figure out which volume control button was pushed,
1917 based on differences from the default register
1918 values. */
1919 x = inb(chip->io_port + 0x1c) & 0xee;
1920 /* Reset the volume control registers. */
1921 outb(0x88, chip->io_port + 0x1c);
1922 outb(0x88, chip->io_port + 0x1d);
1923 outb(0x88, chip->io_port + 0x1e);
1924 outb(0x88, chip->io_port + 0x1f);
1925
1926 if (chip->in_suspend)
1927 return;
1928
1929#ifndef CONFIG_SND_ES1968_INPUT
1930 if (! chip->master_switch || ! chip->master_volume)
1931 return;
1932
1933 val = snd_ac97_read(chip->ac97, AC97_MASTER);
1934 switch (x) {
1935 case 0x88:
1936 /* mute */
1937 val ^= 0x8000;
1938 break;
1939 case 0xaa:
1940 /* volume up */
1941 if ((val & 0x7f) > 0)
1942 val--;
1943 if ((val & 0x7f00) > 0)
1944 val -= 0x0100;
1945 break;
1946 case 0x66:
1947 /* volume down */
1948 if ((val & 0x7f) < 0x1f)
1949 val++;
1950 if ((val & 0x7f00) < 0x1f00)
1951 val += 0x0100;
1952 break;
1953 }
1954 if (snd_ac97_update(chip->ac97, AC97_MASTER, val))
1955 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1956 &chip->master_volume->id);
1957#else
1958 if (!chip->input_dev)
1959 return;
1960
1961 val = 0;
1962 switch (x) {
1963 case 0x88:
1964 /* The counters have not changed, yet we've received a HV
1965 interrupt. According to tests run by various people this
1966 happens when pressing the mute button. */
1967 val = KEY_MUTE;
1968 break;
1969 case 0xaa:
1970 /* counters increased by 1 -> volume up */
1971 val = KEY_VOLUMEUP;
1972 break;
1973 case 0x66:
1974 /* counters decreased by 1 -> volume down */
1975 val = KEY_VOLUMEDOWN;
1976 break;
1977 }
1978
1979 if (val) {
1980 input_report_key(chip->input_dev, val, 1);
1981 input_sync(chip->input_dev);
1982 input_report_key(chip->input_dev, val, 0);
1983 input_sync(chip->input_dev);
1984 }
1985#endif
1986}
1987
1988/*
1989 * interrupt handler
1990 */
1991static irqreturn_t snd_es1968_interrupt(int irq, void *dev_id)
1992{
1993 struct es1968 *chip = dev_id;
1994 u32 event;
1995
1996 if (!(event = inb(chip->io_port + 0x1A)))
1997 return IRQ_NONE;
1998
1999 outw(inw(chip->io_port + 4) & 1, chip->io_port + 4);
2000
2001 if (event & ESM_HWVOL_IRQ)
2002 schedule_work(&chip->hwvol_work);
2003
2004 /* else ack 'em all, i imagine */
2005 outb(0xFF, chip->io_port + 0x1A);
2006
2007 if ((event & ESM_MPU401_IRQ) && chip->rmidi) {
2008 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
2009 }
2010
2011 if (event & ESM_SOUND_IRQ) {
2012 struct esschan *es;
2013 spin_lock(&chip->substream_lock);
2014 list_for_each_entry(es, &chip->substream_list, list, struct esschan) {
2015 if (es->running) {
2016 snd_es1968_update_pcm(chip, es);
2017 if (es->fmt & ESS_FMT_STEREO)
2018 snd_es1968_suppress_jitter(chip, es);
2019 }
2020 }
2021 spin_unlock(&chip->substream_lock);
2022 if (chip->in_measurement) {
2023 unsigned int curp = __apu_get_register(chip, chip->measure_apu, 5);
2024 if (curp < chip->measure_lastpos)
2025 chip->measure_count++;
2026 chip->measure_lastpos = curp;
2027 }
2028 }
2029
2030 return IRQ_HANDLED;
2031}
2032
2033/*
2034 * Mixer stuff
2035 */
2036
2037static int
2038snd_es1968_mixer(struct es1968 *chip)
2039{
2040 struct snd_ac97_bus *pbus;
2041 struct snd_ac97_template ac97;
2042#ifndef CONFIG_SND_ES1968_INPUT
2043 struct snd_ctl_elem_id elem_id;
2044#endif
2045 int err;
2046 static const struct snd_ac97_bus_ops ops = {
2047 .write = snd_es1968_ac97_write,
2048 .read = snd_es1968_ac97_read,
2049 };
2050
2051 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
2052 return err;
2053 pbus->no_vra = 1; /* ES1968 doesn't need VRA */
2054
2055 memset(&ac97, 0, sizeof(ac97));
2056 ac97.private_data = chip;
2057 if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97)) < 0)
2058 return err;
2059
2060#ifndef CONFIG_SND_ES1968_INPUT
2061 /* attach master switch / volumes for h/w volume control */
2062 memset(&elem_id, 0, sizeof(elem_id));
2063 elem_id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2064 strcpy(elem_id.name, "Master Playback Switch");
2065 chip->master_switch = snd_ctl_find_id(chip->card, &elem_id);
2066 memset(&elem_id, 0, sizeof(elem_id));
2067 elem_id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2068 strcpy(elem_id.name, "Master Playback Volume");
2069 chip->master_volume = snd_ctl_find_id(chip->card, &elem_id);
2070#endif
2071
2072 return 0;
2073}
2074
2075/*
2076 * reset ac97 codec
2077 */
2078
2079static void snd_es1968_ac97_reset(struct es1968 *chip)
2080{
2081 unsigned long ioaddr = chip->io_port;
2082
2083 unsigned short save_ringbus_a;
2084 unsigned short save_68;
2085 unsigned short w;
2086 unsigned int vend;
2087
2088 /* save configuration */
2089 save_ringbus_a = inw(ioaddr + 0x36);
2090
2091 //outw(inw(ioaddr + 0x38) & 0xfffc, ioaddr + 0x38); /* clear second codec id? */
2092 /* set command/status address i/o to 1st codec */
2093 outw(inw(ioaddr + 0x3a) & 0xfffc, ioaddr + 0x3a);
2094 outw(inw(ioaddr + 0x3c) & 0xfffc, ioaddr + 0x3c);
2095
2096 /* disable ac link */
2097 outw(0x0000, ioaddr + 0x36);
2098 save_68 = inw(ioaddr + 0x68);
2099 pci_read_config_word(chip->pci, 0x58, &w); /* something magical with gpio and bus arb. */
2100 pci_read_config_dword(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, &vend);
2101 if (w & 1)
2102 save_68 |= 0x10;
2103 outw(0xfffe, ioaddr + 0x64); /* unmask gpio 0 */
2104 outw(0x0001, ioaddr + 0x68); /* gpio write */
2105 outw(0x0000, ioaddr + 0x60); /* write 0 to gpio 0 */
2106 udelay(20);
2107 outw(0x0001, ioaddr + 0x60); /* write 1 to gpio 1 */
2108 msleep(20);
2109
2110 outw(save_68 | 0x1, ioaddr + 0x68); /* now restore .. */
2111 outw((inw(ioaddr + 0x38) & 0xfffc) | 0x1, ioaddr + 0x38);
2112 outw((inw(ioaddr + 0x3a) & 0xfffc) | 0x1, ioaddr + 0x3a);
2113 outw((inw(ioaddr + 0x3c) & 0xfffc) | 0x1, ioaddr + 0x3c);
2114
2115 /* now the second codec */
2116 /* disable ac link */
2117 outw(0x0000, ioaddr + 0x36);
2118 outw(0xfff7, ioaddr + 0x64); /* unmask gpio 3 */
2119 save_68 = inw(ioaddr + 0x68);
2120 outw(0x0009, ioaddr + 0x68); /* gpio write 0 & 3 ?? */
2121 outw(0x0001, ioaddr + 0x60); /* write 1 to gpio */
2122 udelay(20);
2123 outw(0x0009, ioaddr + 0x60); /* write 9 to gpio */
2124 msleep(500);
2125 //outw(inw(ioaddr + 0x38) & 0xfffc, ioaddr + 0x38);
2126 outw(inw(ioaddr + 0x3a) & 0xfffc, ioaddr + 0x3a);
2127 outw(inw(ioaddr + 0x3c) & 0xfffc, ioaddr + 0x3c);
2128
2129#if 0 /* the loop here needs to be much better if we want it.. */
2130 dev_info(chip->card->dev, "trying software reset\n");
2131 /* try and do a software reset */
2132 outb(0x80 | 0x7c, ioaddr + 0x30);
2133 for (w = 0;; w++) {
2134 if ((inw(ioaddr + 0x30) & 1) == 0) {
2135 if (inb(ioaddr + 0x32) != 0)
2136 break;
2137
2138 outb(0x80 | 0x7d, ioaddr + 0x30);
2139 if (((inw(ioaddr + 0x30) & 1) == 0)
2140 && (inb(ioaddr + 0x32) != 0))
2141 break;
2142 outb(0x80 | 0x7f, ioaddr + 0x30);
2143 if (((inw(ioaddr + 0x30) & 1) == 0)
2144 && (inb(ioaddr + 0x32) != 0))
2145 break;
2146 }
2147
2148 if (w > 10000) {
2149 outb(inb(ioaddr + 0x37) | 0x08, ioaddr + 0x37); /* do a software reset */
2150 msleep(500); /* oh my.. */
2151 outb(inb(ioaddr + 0x37) & ~0x08,
2152 ioaddr + 0x37);
2153 udelay(1);
2154 outw(0x80, ioaddr + 0x30);
2155 for (w = 0; w < 10000; w++) {
2156 if ((inw(ioaddr + 0x30) & 1) == 0)
2157 break;
2158 }
2159 }
2160 }
2161#endif
2162 if (vend == NEC_VERSA_SUBID1 || vend == NEC_VERSA_SUBID2) {
2163 /* turn on external amp? */
2164 outw(0xf9ff, ioaddr + 0x64);
2165 outw(inw(ioaddr + 0x68) | 0x600, ioaddr + 0x68);
2166 outw(0x0209, ioaddr + 0x60);
2167 }
2168
2169 /* restore.. */
2170 outw(save_ringbus_a, ioaddr + 0x36);
2171
2172 /* Turn on the 978 docking chip.
2173 First frob the "master output enable" bit,
2174 then set most of the playback volume control registers to max. */
2175 outb(inb(ioaddr+0xc0)|(1<<5), ioaddr+0xc0);
2176 outb(0xff, ioaddr+0xc3);
2177 outb(0xff, ioaddr+0xc4);
2178 outb(0xff, ioaddr+0xc6);
2179 outb(0xff, ioaddr+0xc8);
2180 outb(0x3f, ioaddr+0xcf);
2181 outb(0x3f, ioaddr+0xd0);
2182}
2183
2184static void snd_es1968_reset(struct es1968 *chip)
2185{
2186 /* Reset */
2187 outw(ESM_RESET_MAESTRO | ESM_RESET_DIRECTSOUND,
2188 chip->io_port + ESM_PORT_HOST_IRQ);
2189 udelay(10);
2190 outw(0x0000, chip->io_port + ESM_PORT_HOST_IRQ);
2191 udelay(10);
2192}
2193
2194/*
2195 * initialize maestro chip
2196 */
2197static void snd_es1968_chip_init(struct es1968 *chip)
2198{
2199 struct pci_dev *pci = chip->pci;
2200 int i;
2201 unsigned long iobase = chip->io_port;
2202 u16 w;
2203 u32 n;
2204
2205 /* We used to muck around with pci config space that
2206 * we had no business messing with. We don't know enough
2207 * about the machine to know which DMA mode is appropriate,
2208 * etc. We were guessing wrong on some machines and making
2209 * them unhappy. We now trust in the BIOS to do things right,
2210 * which almost certainly means a new host of problems will
2211 * arise with broken BIOS implementations. screw 'em.
2212 * We're already intolerant of machines that don't assign
2213 * IRQs.
2214 */
2215
2216 /* Config Reg A */
2217 pci_read_config_word(pci, ESM_CONFIG_A, &w);
2218
2219 w &= ~DMA_CLEAR; /* Clear DMA bits */
2220 w &= ~(PIC_SNOOP1 | PIC_SNOOP2); /* Clear Pic Snoop Mode Bits */
2221 w &= ~SAFEGUARD; /* Safeguard off */
2222 w |= POST_WRITE; /* Posted write */
2223 w |= PCI_TIMING; /* PCI timing on */
2224 /* XXX huh? claims to be reserved.. */
2225 w &= ~SWAP_LR; /* swap left/right
2226 seems to only have effect on SB
2227 Emulation */
2228 w &= ~SUBTR_DECODE; /* Subtractive decode off */
2229
2230 pci_write_config_word(pci, ESM_CONFIG_A, w);
2231
2232 /* Config Reg B */
2233
2234 pci_read_config_word(pci, ESM_CONFIG_B, &w);
2235
2236 w &= ~(1 << 15); /* Turn off internal clock multiplier */
2237 /* XXX how do we know which to use? */
2238 w &= ~(1 << 14); /* External clock */
2239
2240 w &= ~SPDIF_CONFB; /* disable S/PDIF output */
2241 w |= HWV_CONFB; /* HWV on */
2242 w |= DEBOUNCE; /* Debounce off: easier to push the HW buttons */
2243 w &= ~GPIO_CONFB; /* GPIO 4:5 */
2244 w |= CHI_CONFB; /* Disconnect from the CHI. Enabling this made a dell 7500 work. */
2245 w &= ~IDMA_CONFB; /* IDMA off (undocumented) */
2246 w &= ~MIDI_FIX; /* MIDI fix off (undoc) */
2247 w &= ~(1 << 1); /* reserved, always write 0 */
2248 w &= ~IRQ_TO_ISA; /* IRQ to ISA off (undoc) */
2249
2250 pci_write_config_word(pci, ESM_CONFIG_B, w);
2251
2252 /* DDMA off */
2253
2254 pci_read_config_word(pci, ESM_DDMA, &w);
2255 w &= ~(1 << 0);
2256 pci_write_config_word(pci, ESM_DDMA, w);
2257
2258 /*
2259 * Legacy mode
2260 */
2261
2262 pci_read_config_word(pci, ESM_LEGACY_AUDIO_CONTROL, &w);
2263
2264 w |= ESS_DISABLE_AUDIO; /* Disable Legacy Audio */
2265 w &= ~ESS_ENABLE_SERIAL_IRQ; /* Disable SIRQ */
2266 w &= ~(0x1f); /* disable mpu irq/io, game port, fm, SB */
2267
2268 pci_write_config_word(pci, ESM_LEGACY_AUDIO_CONTROL, w);
2269
2270 /* Set up 978 docking control chip. */
2271 pci_read_config_word(pci, 0x58, &w);
2272 w|=1<<2; /* Enable 978. */
2273 w|=1<<3; /* Turn on 978 hardware volume control. */
2274 w&=~(1<<11); /* Turn on 978 mixer volume control. */
2275 pci_write_config_word(pci, 0x58, w);
2276
2277 /* Sound Reset */
2278
2279 snd_es1968_reset(chip);
2280
2281 /*
2282 * Ring Bus Setup
2283 */
2284
2285 /* setup usual 0x34 stuff.. 0x36 may be chip specific */
2286 outw(0xC090, iobase + ESM_RING_BUS_DEST); /* direct sound, stereo */
2287 udelay(20);
2288 outw(0x3000, iobase + ESM_RING_BUS_CONTR_A); /* enable ringbus/serial */
2289 udelay(20);
2290
2291 /*
2292 * Reset the CODEC
2293 */
2294
2295 snd_es1968_ac97_reset(chip);
2296
2297 /* Ring Bus Control B */
2298
2299 n = inl(iobase + ESM_RING_BUS_CONTR_B);
2300 n &= ~RINGB_EN_SPDIF; /* SPDIF off */
2301 //w |= RINGB_EN_2CODEC; /* enable 2nd codec */
2302 outl(n, iobase + ESM_RING_BUS_CONTR_B);
2303
2304 /* Set hardware volume control registers to midpoints.
2305 We can tell which button was pushed based on how they change. */
2306 outb(0x88, iobase+0x1c);
2307 outb(0x88, iobase+0x1d);
2308 outb(0x88, iobase+0x1e);
2309 outb(0x88, iobase+0x1f);
2310
2311 /* it appears some maestros (dell 7500) only work if these are set,
2312 regardless of whether we use the assp or not. */
2313
2314 outb(0, iobase + ASSP_CONTROL_B);
2315 outb(3, iobase + ASSP_CONTROL_A); /* M: Reserved bits... */
2316 outb(0, iobase + ASSP_CONTROL_C); /* M: Disable ASSP, ASSP IRQ's and FM Port */
2317
2318 /*
2319 * set up wavecache
2320 */
2321 for (i = 0; i < 16; i++) {
2322 /* Write 0 into the buffer area 0x1E0->1EF */
2323 outw(0x01E0 + i, iobase + WC_INDEX);
2324 outw(0x0000, iobase + WC_DATA);
2325
2326 /* The 1.10 test program seem to write 0 into the buffer area
2327 * 0x1D0-0x1DF too.*/
2328 outw(0x01D0 + i, iobase + WC_INDEX);
2329 outw(0x0000, iobase + WC_DATA);
2330 }
2331 wave_set_register(chip, IDR7_WAVE_ROMRAM,
2332 (wave_get_register(chip, IDR7_WAVE_ROMRAM) & 0xFF00));
2333 wave_set_register(chip, IDR7_WAVE_ROMRAM,
2334 wave_get_register(chip, IDR7_WAVE_ROMRAM) | 0x100);
2335 wave_set_register(chip, IDR7_WAVE_ROMRAM,
2336 wave_get_register(chip, IDR7_WAVE_ROMRAM) & ~0x200);
2337 wave_set_register(chip, IDR7_WAVE_ROMRAM,
2338 wave_get_register(chip, IDR7_WAVE_ROMRAM) | ~0x400);
2339
2340
2341 maestro_write(chip, IDR2_CRAM_DATA, 0x0000);
2342 /* Now back to the DirectSound stuff */
2343 /* audio serial configuration.. ? */
2344 maestro_write(chip, 0x08, 0xB004);
2345 maestro_write(chip, 0x09, 0x001B);
2346 maestro_write(chip, 0x0A, 0x8000);
2347 maestro_write(chip, 0x0B, 0x3F37);
2348 maestro_write(chip, 0x0C, 0x0098);
2349
2350 /* parallel in, has something to do with recording :) */
2351 maestro_write(chip, 0x0C,
2352 (maestro_read(chip, 0x0C) & ~0xF000) | 0x8000);
2353 /* parallel out */
2354 maestro_write(chip, 0x0C,
2355 (maestro_read(chip, 0x0C) & ~0x0F00) | 0x0500);
2356
2357 maestro_write(chip, 0x0D, 0x7632);
2358
2359 /* Wave cache control on - test off, sg off,
2360 enable, enable extra chans 1Mb */
2361
2362 w = inw(iobase + WC_CONTROL);
2363
2364 w &= ~0xFA00; /* Seems to be reserved? I don't know */
2365 w |= 0xA000; /* reserved... I don't know */
2366 w &= ~0x0200; /* Channels 56,57,58,59 as Extra Play,Rec Channel enable
2367 Seems to crash the Computer if enabled... */
2368 w |= 0x0100; /* Wave Cache Operation Enabled */
2369 w |= 0x0080; /* Channels 60/61 as Placback/Record enabled */
2370 w &= ~0x0060; /* Clear Wavtable Size */
2371 w |= 0x0020; /* Wavetable Size : 1MB */
2372 /* Bit 4 is reserved */
2373 w &= ~0x000C; /* DMA Stuff? I don't understand what the datasheet means */
2374 /* Bit 1 is reserved */
2375 w &= ~0x0001; /* Test Mode off */
2376
2377 outw(w, iobase + WC_CONTROL);
2378
2379 /* Now clear the APU control ram */
2380 for (i = 0; i < NR_APUS; i++) {
2381 for (w = 0; w < NR_APU_REGS; w++)
2382 apu_set_register(chip, i, w, 0);
2383
2384 }
2385}
2386
2387/* Enable IRQ's */
2388static void snd_es1968_start_irq(struct es1968 *chip)
2389{
2390 unsigned short w;
2391 w = ESM_HIRQ_DSIE | ESM_HIRQ_HW_VOLUME;
2392 if (chip->rmidi)
2393 w |= ESM_HIRQ_MPU401;
2394 outb(w, chip->io_port + 0x1A);
2395 outw(w, chip->io_port + ESM_PORT_HOST_IRQ);
2396}
2397
2398#ifdef CONFIG_PM_SLEEP
2399/*
2400 * PM support
2401 */
2402static int es1968_suspend(struct device *dev)
2403{
2404 struct snd_card *card = dev_get_drvdata(dev);
2405 struct es1968 *chip = card->private_data;
2406
2407 if (! chip->do_pm)
2408 return 0;
2409
2410 chip->in_suspend = 1;
2411 cancel_work_sync(&chip->hwvol_work);
2412 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2413 snd_ac97_suspend(chip->ac97);
2414 snd_es1968_bob_stop(chip);
2415 return 0;
2416}
2417
2418static int es1968_resume(struct device *dev)
2419{
2420 struct snd_card *card = dev_get_drvdata(dev);
2421 struct es1968 *chip = card->private_data;
2422 struct esschan *es;
2423
2424 if (! chip->do_pm)
2425 return 0;
2426
2427 snd_es1968_chip_init(chip);
2428
2429 /* need to restore the base pointers.. */
2430 if (chip->dma.addr) {
2431 /* set PCMBAR */
2432 wave_set_register(chip, 0x01FC, chip->dma.addr >> 12);
2433 }
2434
2435 snd_es1968_start_irq(chip);
2436
2437 /* restore ac97 state */
2438 snd_ac97_resume(chip->ac97);
2439
2440 list_for_each_entry(es, &chip->substream_list, list, struct esschan) {
2441 switch (es->mode) {
2442 case ESM_MODE_PLAY:
2443 snd_es1968_playback_setup(chip, es, es->substream->runtime);
2444 break;
2445 case ESM_MODE_CAPTURE:
2446 snd_es1968_capture_setup(chip, es, es->substream->runtime);
2447 break;
2448 }
2449 }
2450
2451 /* start timer again */
2452 if (chip->bobclient)
2453 snd_es1968_bob_start(chip);
2454
2455 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2456 chip->in_suspend = 0;
2457 return 0;
2458}
2459
2460static SIMPLE_DEV_PM_OPS(es1968_pm, es1968_suspend, es1968_resume);
2461#define ES1968_PM_OPS &es1968_pm
2462#else
2463#define ES1968_PM_OPS NULL
2464#endif /* CONFIG_PM_SLEEP */
2465
2466#ifdef SUPPORT_JOYSTICK
2467#define JOYSTICK_ADDR 0x200
2468static int snd_es1968_create_gameport(struct es1968 *chip, int dev)
2469{
2470 struct gameport *gp;
2471 struct resource *r;
2472 u16 val;
2473
2474 if (!joystick[dev])
2475 return -ENODEV;
2476
2477 r = request_region(JOYSTICK_ADDR, 8, "ES1968 gameport");
2478 if (!r)
2479 return -EBUSY;
2480
2481 chip->gameport = gp = gameport_allocate_port();
2482 if (!gp) {
2483 dev_err(chip->card->dev,
2484 "cannot allocate memory for gameport\n");
2485 release_and_free_resource(r);
2486 return -ENOMEM;
2487 }
2488
2489 pci_read_config_word(chip->pci, ESM_LEGACY_AUDIO_CONTROL, &val);
2490 pci_write_config_word(chip->pci, ESM_LEGACY_AUDIO_CONTROL, val | 0x04);
2491
2492 gameport_set_name(gp, "ES1968 Gameport");
2493 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
2494 gameport_set_dev_parent(gp, &chip->pci->dev);
2495 gp->io = JOYSTICK_ADDR;
2496 gameport_set_port_data(gp, r);
2497
2498 gameport_register_port(gp);
2499
2500 return 0;
2501}
2502
2503static void snd_es1968_free_gameport(struct es1968 *chip)
2504{
2505 if (chip->gameport) {
2506 struct resource *r = gameport_get_port_data(chip->gameport);
2507
2508 gameport_unregister_port(chip->gameport);
2509 chip->gameport = NULL;
2510
2511 release_and_free_resource(r);
2512 }
2513}
2514#else
2515static inline int snd_es1968_create_gameport(struct es1968 *chip, int dev) { return -ENOSYS; }
2516static inline void snd_es1968_free_gameport(struct es1968 *chip) { }
2517#endif
2518
2519#ifdef CONFIG_SND_ES1968_INPUT
2520static int snd_es1968_input_register(struct es1968 *chip)
2521{
2522 struct input_dev *input_dev;
2523 int err;
2524
2525 input_dev = input_allocate_device();
2526 if (!input_dev)
2527 return -ENOMEM;
2528
2529 snprintf(chip->phys, sizeof(chip->phys), "pci-%s/input0",
2530 pci_name(chip->pci));
2531
2532 input_dev->name = chip->card->driver;
2533 input_dev->phys = chip->phys;
2534 input_dev->id.bustype = BUS_PCI;
2535 input_dev->id.vendor = chip->pci->vendor;
2536 input_dev->id.product = chip->pci->device;
2537 input_dev->dev.parent = &chip->pci->dev;
2538
2539 __set_bit(EV_KEY, input_dev->evbit);
2540 __set_bit(KEY_MUTE, input_dev->keybit);
2541 __set_bit(KEY_VOLUMEDOWN, input_dev->keybit);
2542 __set_bit(KEY_VOLUMEUP, input_dev->keybit);
2543
2544 err = input_register_device(input_dev);
2545 if (err) {
2546 input_free_device(input_dev);
2547 return err;
2548 }
2549
2550 chip->input_dev = input_dev;
2551 return 0;
2552}
2553#endif /* CONFIG_SND_ES1968_INPUT */
2554
2555#ifdef CONFIG_SND_ES1968_RADIO
2556#define GPIO_DATA 0x60
2557#define IO_MASK 4 /* mask register offset from GPIO_DATA
2558 bits 1=unmask write to given bit */
2559#define IO_DIR 8 /* direction register offset from GPIO_DATA
2560 bits 0/1=read/write direction */
2561
2562/* GPIO to TEA575x maps */
2563struct snd_es1968_tea575x_gpio {
2564 u8 data, clk, wren, most;
2565 char *name;
2566};
2567
2568static const struct snd_es1968_tea575x_gpio snd_es1968_tea575x_gpios[] = {
2569 { .data = 6, .clk = 7, .wren = 8, .most = 9, .name = "SF64-PCE2" },
2570 { .data = 7, .clk = 8, .wren = 6, .most = 10, .name = "M56VAP" },
2571};
2572
2573#define get_tea575x_gpio(chip) \
2574 (&snd_es1968_tea575x_gpios[(chip)->tea575x_tuner])
2575
2576
2577static void snd_es1968_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
2578{
2579 struct es1968 *chip = tea->private_data;
2580 struct snd_es1968_tea575x_gpio gpio = *get_tea575x_gpio(chip);
2581 u16 val = 0;
2582
2583 val |= (pins & TEA575X_DATA) ? (1 << gpio.data) : 0;
2584 val |= (pins & TEA575X_CLK) ? (1 << gpio.clk) : 0;
2585 val |= (pins & TEA575X_WREN) ? (1 << gpio.wren) : 0;
2586
2587 outw(val, chip->io_port + GPIO_DATA);
2588}
2589
2590static u8 snd_es1968_tea575x_get_pins(struct snd_tea575x *tea)
2591{
2592 struct es1968 *chip = tea->private_data;
2593 struct snd_es1968_tea575x_gpio gpio = *get_tea575x_gpio(chip);
2594 u16 val = inw(chip->io_port + GPIO_DATA);
2595 u8 ret = 0;
2596
2597 if (val & (1 << gpio.data))
2598 ret |= TEA575X_DATA;
2599 if (val & (1 << gpio.most))
2600 ret |= TEA575X_MOST;
2601
2602 return ret;
2603}
2604
2605static void snd_es1968_tea575x_set_direction(struct snd_tea575x *tea, bool output)
2606{
2607 struct es1968 *chip = tea->private_data;
2608 unsigned long io = chip->io_port + GPIO_DATA;
2609 u16 odir = inw(io + IO_DIR);
2610 struct snd_es1968_tea575x_gpio gpio = *get_tea575x_gpio(chip);
2611
2612 if (output) {
2613 outw(~((1 << gpio.data) | (1 << gpio.clk) | (1 << gpio.wren)),
2614 io + IO_MASK);
2615 outw(odir | (1 << gpio.data) | (1 << gpio.clk) | (1 << gpio.wren),
2616 io + IO_DIR);
2617 } else {
2618 outw(~((1 << gpio.clk) | (1 << gpio.wren) | (1 << gpio.data) | (1 << gpio.most)),
2619 io + IO_MASK);
2620 outw((odir & ~((1 << gpio.data) | (1 << gpio.most)))
2621 | (1 << gpio.clk) | (1 << gpio.wren), io + IO_DIR);
2622 }
2623}
2624
2625static const struct snd_tea575x_ops snd_es1968_tea_ops = {
2626 .set_pins = snd_es1968_tea575x_set_pins,
2627 .get_pins = snd_es1968_tea575x_get_pins,
2628 .set_direction = snd_es1968_tea575x_set_direction,
2629};
2630#endif
2631
2632static int snd_es1968_free(struct es1968 *chip)
2633{
2634 cancel_work_sync(&chip->hwvol_work);
2635#ifdef CONFIG_SND_ES1968_INPUT
2636 if (chip->input_dev)
2637 input_unregister_device(chip->input_dev);
2638#endif
2639
2640 if (chip->io_port) {
2641 outw(1, chip->io_port + 0x04); /* clear WP interrupts */
2642 outw(0, chip->io_port + ESM_PORT_HOST_IRQ); /* disable IRQ */
2643 }
2644
2645#ifdef CONFIG_SND_ES1968_RADIO
2646 snd_tea575x_exit(&chip->tea);
2647 v4l2_device_unregister(&chip->v4l2_dev);
2648#endif
2649
2650 if (chip->irq >= 0)
2651 free_irq(chip->irq, chip);
2652 snd_es1968_free_gameport(chip);
2653 pci_release_regions(chip->pci);
2654 pci_disable_device(chip->pci);
2655 kfree(chip);
2656 return 0;
2657}
2658
2659static int snd_es1968_dev_free(struct snd_device *device)
2660{
2661 struct es1968 *chip = device->device_data;
2662 return snd_es1968_free(chip);
2663}
2664
2665struct ess_device_list {
2666 unsigned short type; /* chip type */
2667 unsigned short vendor; /* subsystem vendor id */
2668};
2669
2670static const struct ess_device_list pm_allowlist[] = {
2671 { TYPE_MAESTRO2E, 0x0e11 }, /* Compaq Armada */
2672 { TYPE_MAESTRO2E, 0x1028 },
2673 { TYPE_MAESTRO2E, 0x103c },
2674 { TYPE_MAESTRO2E, 0x1179 },
2675 { TYPE_MAESTRO2E, 0x14c0 }, /* HP omnibook 4150 */
2676 { TYPE_MAESTRO2E, 0x1558 },
2677 { TYPE_MAESTRO2E, 0x125d }, /* a PCI card, e.g. Terratec DMX */
2678 { TYPE_MAESTRO2, 0x125d }, /* a PCI card, e.g. SF64-PCE2 */
2679};
2680
2681static const struct ess_device_list mpu_denylist[] = {
2682 { TYPE_MAESTRO2, 0x125d },
2683};
2684
2685static int snd_es1968_create(struct snd_card *card,
2686 struct pci_dev *pci,
2687 int total_bufsize,
2688 int play_streams,
2689 int capt_streams,
2690 int chip_type,
2691 int do_pm,
2692 int radio_nr,
2693 struct es1968 **chip_ret)
2694{
2695 static const struct snd_device_ops ops = {
2696 .dev_free = snd_es1968_dev_free,
2697 };
2698 struct es1968 *chip;
2699 int i, err;
2700
2701 *chip_ret = NULL;
2702
2703 /* enable PCI device */
2704 if ((err = pci_enable_device(pci)) < 0)
2705 return err;
2706 /* check, if we can restrict PCI DMA transfers to 28 bits */
2707 if (dma_set_mask(&pci->dev, DMA_BIT_MASK(28)) < 0 ||
2708 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(28)) < 0) {
2709 dev_err(card->dev,
2710 "architecture does not support 28bit PCI busmaster DMA\n");
2711 pci_disable_device(pci);
2712 return -ENXIO;
2713 }
2714
2715 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
2716 if (! chip) {
2717 pci_disable_device(pci);
2718 return -ENOMEM;
2719 }
2720
2721 /* Set Vars */
2722 chip->type = chip_type;
2723 spin_lock_init(&chip->reg_lock);
2724 spin_lock_init(&chip->substream_lock);
2725 INIT_LIST_HEAD(&chip->buf_list);
2726 INIT_LIST_HEAD(&chip->substream_list);
2727 mutex_init(&chip->memory_mutex);
2728 INIT_WORK(&chip->hwvol_work, es1968_update_hw_volume);
2729 chip->card = card;
2730 chip->pci = pci;
2731 chip->irq = -1;
2732 chip->total_bufsize = total_bufsize; /* in bytes */
2733 chip->playback_streams = play_streams;
2734 chip->capture_streams = capt_streams;
2735
2736 if ((err = pci_request_regions(pci, "ESS Maestro")) < 0) {
2737 kfree(chip);
2738 pci_disable_device(pci);
2739 return err;
2740 }
2741 chip->io_port = pci_resource_start(pci, 0);
2742 if (request_irq(pci->irq, snd_es1968_interrupt, IRQF_SHARED,
2743 KBUILD_MODNAME, chip)) {
2744 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
2745 snd_es1968_free(chip);
2746 return -EBUSY;
2747 }
2748 chip->irq = pci->irq;
2749 card->sync_irq = chip->irq;
2750
2751 /* Clear Maestro_map */
2752 for (i = 0; i < 32; i++)
2753 chip->maestro_map[i] = 0;
2754
2755 /* Clear Apu Map */
2756 for (i = 0; i < NR_APUS; i++)
2757 chip->apu[i] = ESM_APU_FREE;
2758
2759 /* just to be sure */
2760 pci_set_master(pci);
2761
2762 if (do_pm > 1) {
2763 /* disable power-management if not on the allowlist */
2764 unsigned short vend;
2765 pci_read_config_word(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, &vend);
2766 for (i = 0; i < (int)ARRAY_SIZE(pm_allowlist); i++) {
2767 if (chip->type == pm_allowlist[i].type &&
2768 vend == pm_allowlist[i].vendor) {
2769 do_pm = 1;
2770 break;
2771 }
2772 }
2773 if (do_pm > 1) {
2774 /* not matched; disabling pm */
2775 dev_info(card->dev, "not attempting power management.\n");
2776 do_pm = 0;
2777 }
2778 }
2779 chip->do_pm = do_pm;
2780
2781 snd_es1968_chip_init(chip);
2782
2783 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2784 snd_es1968_free(chip);
2785 return err;
2786 }
2787
2788#ifdef CONFIG_SND_ES1968_RADIO
2789 /* don't play with GPIOs on laptops */
2790 if (chip->pci->subsystem_vendor != 0x125d)
2791 goto no_radio;
2792 err = v4l2_device_register(&pci->dev, &chip->v4l2_dev);
2793 if (err < 0) {
2794 snd_es1968_free(chip);
2795 return err;
2796 }
2797 chip->tea.v4l2_dev = &chip->v4l2_dev;
2798 chip->tea.private_data = chip;
2799 chip->tea.radio_nr = radio_nr;
2800 chip->tea.ops = &snd_es1968_tea_ops;
2801 sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
2802 for (i = 0; i < ARRAY_SIZE(snd_es1968_tea575x_gpios); i++) {
2803 chip->tea575x_tuner = i;
2804 if (!snd_tea575x_init(&chip->tea, THIS_MODULE)) {
2805 dev_info(card->dev, "detected TEA575x radio type %s\n",
2806 get_tea575x_gpio(chip)->name);
2807 strlcpy(chip->tea.card, get_tea575x_gpio(chip)->name,
2808 sizeof(chip->tea.card));
2809 break;
2810 }
2811 }
2812no_radio:
2813#endif
2814
2815 *chip_ret = chip;
2816
2817 return 0;
2818}
2819
2820
2821/*
2822 */
2823static int snd_es1968_probe(struct pci_dev *pci,
2824 const struct pci_device_id *pci_id)
2825{
2826 static int dev;
2827 struct snd_card *card;
2828 struct es1968 *chip;
2829 unsigned int i;
2830 int err;
2831
2832 if (dev >= SNDRV_CARDS)
2833 return -ENODEV;
2834 if (!enable[dev]) {
2835 dev++;
2836 return -ENOENT;
2837 }
2838
2839 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2840 0, &card);
2841 if (err < 0)
2842 return err;
2843
2844 if (total_bufsize[dev] < 128)
2845 total_bufsize[dev] = 128;
2846 if (total_bufsize[dev] > 4096)
2847 total_bufsize[dev] = 4096;
2848 if ((err = snd_es1968_create(card, pci,
2849 total_bufsize[dev] * 1024, /* in bytes */
2850 pcm_substreams_p[dev],
2851 pcm_substreams_c[dev],
2852 pci_id->driver_data,
2853 use_pm[dev],
2854 radio_nr[dev],
2855 &chip)) < 0) {
2856 snd_card_free(card);
2857 return err;
2858 }
2859 card->private_data = chip;
2860
2861 switch (chip->type) {
2862 case TYPE_MAESTRO2E:
2863 strcpy(card->driver, "ES1978");
2864 strcpy(card->shortname, "ESS ES1978 (Maestro 2E)");
2865 break;
2866 case TYPE_MAESTRO2:
2867 strcpy(card->driver, "ES1968");
2868 strcpy(card->shortname, "ESS ES1968 (Maestro 2)");
2869 break;
2870 case TYPE_MAESTRO:
2871 strcpy(card->driver, "ESM1");
2872 strcpy(card->shortname, "ESS Maestro 1");
2873 break;
2874 }
2875
2876 if ((err = snd_es1968_pcm(chip, 0)) < 0) {
2877 snd_card_free(card);
2878 return err;
2879 }
2880
2881 if ((err = snd_es1968_mixer(chip)) < 0) {
2882 snd_card_free(card);
2883 return err;
2884 }
2885
2886 if (enable_mpu[dev] == 2) {
2887 /* check the deny list */
2888 unsigned short vend;
2889 pci_read_config_word(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, &vend);
2890 for (i = 0; i < ARRAY_SIZE(mpu_denylist); i++) {
2891 if (chip->type == mpu_denylist[i].type &&
2892 vend == mpu_denylist[i].vendor) {
2893 enable_mpu[dev] = 0;
2894 break;
2895 }
2896 }
2897 }
2898 if (enable_mpu[dev]) {
2899 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
2900 chip->io_port + ESM_MPU401_PORT,
2901 MPU401_INFO_INTEGRATED |
2902 MPU401_INFO_IRQ_HOOK,
2903 -1, &chip->rmidi)) < 0) {
2904 dev_warn(card->dev, "skipping MPU-401 MIDI support..\n");
2905 }
2906 }
2907
2908 snd_es1968_create_gameport(chip, dev);
2909
2910#ifdef CONFIG_SND_ES1968_INPUT
2911 err = snd_es1968_input_register(chip);
2912 if (err)
2913 dev_warn(card->dev,
2914 "Input device registration failed with error %i", err);
2915#endif
2916
2917 snd_es1968_start_irq(chip);
2918
2919 chip->clock = clock[dev];
2920 if (! chip->clock)
2921 es1968_measure_clock(chip);
2922
2923 sprintf(card->longname, "%s at 0x%lx, irq %i",
2924 card->shortname, chip->io_port, chip->irq);
2925
2926 if ((err = snd_card_register(card)) < 0) {
2927 snd_card_free(card);
2928 return err;
2929 }
2930 pci_set_drvdata(pci, card);
2931 dev++;
2932 return 0;
2933}
2934
2935static void snd_es1968_remove(struct pci_dev *pci)
2936{
2937 snd_card_free(pci_get_drvdata(pci));
2938}
2939
2940static struct pci_driver es1968_driver = {
2941 .name = KBUILD_MODNAME,
2942 .id_table = snd_es1968_ids,
2943 .probe = snd_es1968_probe,
2944 .remove = snd_es1968_remove,
2945 .driver = {
2946 .pm = ES1968_PM_OPS,
2947 },
2948};
2949
2950module_pci_driver(es1968_driver);
Note: See TracBrowser for help on using the repository browser.