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

Last change on this file since 18 was 18, checked in by vladest, 20 years ago

initial import

File size: 88.1 KB
Line 
1/*
2 * Driver for ESS Maestro 1/2/2E Sound Card (started 21.8.99)
3 * Copyright (c) by Matze Braun <MatzeBraun@gmx.de>.
4 * Takashi Iwai <tiwai@suse.de>
5 *
6 * Most of the driver code comes from Zach Brown(zab@redhat.com)
7 * Alan Cox OSS Driver
8 * Rewritted from card-es1938.c source.
9 *
10 * TODO:
11 * Perhaps Synth
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 *
28 * Notes from Zach Brown about the driver code
29 *
30 * Hardware Description
31 *
32 * A working Maestro setup contains the Maestro chip wired to a
33 * codec or 2. In the Maestro we have the APUs, the ASSP, and the
34 * Wavecache. The APUs can be though of as virtual audio routing
35 * channels. They can take data from a number of sources and perform
36 * basic encodings of the data. The wavecache is a storehouse for
37 * PCM data. Typically it deals with PCI and interracts with the
38 * APUs. The ASSP is a wacky DSP like device that ESS is loth
39 * to release docs on. Thankfully it isn't required on the Maestro
40 * until you start doing insane things like FM emulation and surround
41 * encoding. The codecs are almost always AC-97 compliant codecs,
42 * but it appears that early Maestros may have had PT101 (an ESS
43 * part?) wired to them. The only real difference in the Maestro
44 * families is external goop like docking capability, memory for
45 * the ASSP, and initialization differences.
46 *
47 * Driver Operation
48 *
49 * We only drive the APU/Wavecache as typical DACs and drive the
50 * mixers in the codecs. There are 64 APUs. We assign 6 to each
51 * /dev/dsp? device. 2 channels for output, and 4 channels for
52 * input.
53 *
54 * Each APU can do a number of things, but we only really use
55 * 3 basic functions. For playback we use them to convert PCM
56 * data fetched over PCI by the wavecahche into analog data that
57 * is handed to the codec. One APU for mono, and a pair for stereo.
58 * When in stereo, the combination of smarts in the APU and Wavecache
59 * decide which wavecache gets the left or right channel.
60 *
61 * For record we still use the old overly mono system. For each in
62 * coming channel the data comes in from the codec, through a 'input'
63 * APU, through another rate converter APU, and then into memory via
64 * the wavecache and PCI. If its stereo, we mash it back into LRLR in
65 * software. The pass between the 2 APUs is supposedly what requires us
66 * to have a 512 byte buffer sitting around in wavecache/memory.
67 *
68 * The wavecache makes our life even more fun. First off, it can
69 * only address the first 28 bits of PCI address space, making it
70 * useless on quite a few architectures. Secondly, its insane.
71 * It claims to fetch from 4 regions of PCI space, each 4 meg in length.
72 * But that doesn't really work. You can only use 1 region. So all our
73 * allocations have to be in 4meg of each other. Booo. Hiss.
74 * So we have a module parameter, dsps_order, that is the order of
75 * the number of dsps to provide. All their buffer space is allocated
76 * on open time. The sonicvibes OSS routines we inherited really want
77 * power of 2 buffers, so we have all those next to each other, then
78 * 512 byte regions for the recording wavecaches. This ends up
79 * wasting quite a bit of memory. The only fixes I can see would be
80 * getting a kernel allocator that could work in zones, or figuring out
81 * just how to coerce the WP into doing what we want.
82 *
83 * The indirection of the various registers means we have to spinlock
84 * nearly all register accesses. We have the main register indirection
85 * like the wave cache, maestro registers, etc. Then we have beasts
86 * like the APU interface that is indirect registers gotten at through
87 * the main maestro indirection. Ouch. We spinlock around the actual
88 * ports on a per card basis. This means spinlock activity at each IO
89 * operation, but the only IO operation clusters are in non critical
90 * paths and it makes the code far easier to follow. Interrupts are
91 * blocked while holding the locks because the int handler has to
92 * get at some of them :(. The mixer interface doesn't, however.
93 * We also have an OSS state lock that is thrown around in a few
94 * places.
95 */
96
97#define __SND_OSS_COMPAT__
98#include <sound/driver.h>
99#include <sound/pcm.h>
100#include <sound/mpu401.h>
101#include <sound/ac97_codec.h>
102#define SNDRV_GET_ID
103#include <sound/initval.h>
104
105#define CARD_NAME "ESS Maestro1/2"
106#define DRIVER_NAME "ES1968"
107
108EXPORT_NO_SYMBOLS;
109MODULE_DESCRIPTION("ESS Maestro");
110MODULE_CLASSES("{sound}");
111MODULE_LICENSE("GPL");
112MODULE_DEVICES("{{ESS,Maestro 2e},"
113 "{ESS,Maestro 2},"
114 "{ESS,Maestro 1},"
115 "{TerraTec,DMX}}");
116
117static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 1-MAX */
118static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
119static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
120static int total_bufsize[SNDRV_CARDS] = { REPEAT_SNDRV(1024) };
121static int pcm_substreams_p[SNDRV_CARDS] = { REPEAT_SNDRV(4) };
122static int pcm_substreams_c[SNDRV_CARDS] = { REPEAT_SNDRV(1) };
123static int clock[SNDRV_CARDS] = { REPEAT_SNDRV(0) };
124static int use_pm[SNDRV_CARDS] = { REPEAT_SNDRV(2) };
125static int enable_mpu[SNDRV_CARDS] = { REPEAT_SNDRV(1) };
126
127MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
128MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
129MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
130MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
131MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
132MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
133MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
134MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
135MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
136MODULE_PARM(total_bufsize, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
137MODULE_PARM_DESC(total_bufsize, "Total buffer size in kB.");
138MODULE_PARM_SYNTAX(total_bufsize, SNDRV_ENABLED ",allows:{{1,4096}},skill:advanced");
139MODULE_PARM(pcm_substreams_p, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
140MODULE_PARM_DESC(pcm_substreams_p, "PCM Playback substreams for " CARD_NAME " soundcard.");
141MODULE_PARM_SYNTAX(pcm_substreams_p, SNDRV_ENABLED ",allows:{{1,8}}");
142MODULE_PARM(pcm_substreams_c, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
143MODULE_PARM_DESC(pcm_substreams_c, "PCM Capture substreams for " CARD_NAME " soundcard.");
144MODULE_PARM_SYNTAX(pcm_substreams_c, SNDRV_ENABLED ",allows:{{0,8}}");
145MODULE_PARM(clock, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
146MODULE_PARM_DESC(clock, "Clock on " CARD_NAME " soundcard. (0 = auto-detect)");
147MODULE_PARM_SYNTAX(clock, SNDRV_ENABLED);
148MODULE_PARM(use_pm, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
149MODULE_PARM_DESC(use_pm, "Toggle power-management. (0 = off, 1 = on, 2 = auto)");
150MODULE_PARM_SYNTAX(use_pm, SNDRV_ENABLED ",allows:{{0,1,2}},skill:advanced");
151MODULE_PARM(enable_mpu, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
152MODULE_PARM_DESC(enable_mpu, "Enable MPU401. (0 = off, 1 = on, 2 = auto)");
153MODULE_PARM_SYNTAX(enable_mpu, SNDRV_ENABLED "," SNDRV_BOOLEAN_TRUE_DESC);
154
155
156/* PCI Dev ID's */
157
158#ifndef PCI_VENDOR_ID_ESS
159#define PCI_VENDOR_ID_ESS 0x125D
160#endif
161
162#define PCI_VENDOR_ID_ESS_OLD 0x1285 /* Platform Tech, the people the ESS
163was bought form */
164
165#ifndef PCI_DEVICE_ID_ESS_M2E
166#define PCI_DEVICE_ID_ESS_M2E 0x1978
167#endif
168#ifndef PCI_DEVICE_ID_ESS_M2
169#define PCI_DEVICE_ID_ESS_M2 0x1968
170#endif
171#ifndef PCI_DEVICE_ID_ESS_M1
172#define PCI_DEVICE_ID_ESS_M1 0x0100
173#endif
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_ENABLE_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 ISA_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 Adresses**** */
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/* acpi states */
445enum {
446 ACPI_D0=0,
447 ACPI_D1,
448 ACPI_D2,
449 ACPI_D3
450};
451
452/* bits in the acpi masks */
453#define ACPI_12MHZ ( 1 << 15)
454#define ACPI_24MHZ ( 1 << 14)
455#define ACPI_978 ( 1 << 13)
456#define ACPI_SPDIF ( 1 << 12)
457#define ACPI_GLUE ( 1 << 11)
458#define ACPI__10 ( 1 << 10) /* reserved */
459#define ACPI_PCIINT ( 1 << 9)
460#define ACPI_HV ( 1 << 8) /* hardware volume */
461#define ACPI_GPIO ( 1 << 7)
462#define ACPI_ASSP ( 1 << 6)
463#define ACPI_SB ( 1 << 5) /* sb emul */
464#define ACPI_FM ( 1 << 4) /* fm emul */
465#define ACPI_RB ( 1 << 3) /* ringbus / aclink */
466#define ACPI_MIDI ( 1 << 2)
467#define ACPI_GP ( 1 << 1) /* game port */
468#define ACPI_WP ( 1 << 0) /* wave processor */
469
470#define ACPI_ALL (0xffff)
471#define ACPI_SLEEP (~(ACPI_SPDIF|ACPI_ASSP|ACPI_SB|ACPI_FM| \
472 ACPI_MIDI|ACPI_GP|ACPI_WP))
473#define ACPI_NONE (ACPI__10)
474
475/* these masks indicate which units we care about at
476 which states */
477static u16 acpi_state_mask[] = {
478#ifdef TARGET_OS2
479 ACPI_ALL,
480 ACPI_SLEEP,
481 ACPI_SLEEP,
482 ACPI_NONE
483#else
484 [ACPI_D0] = ACPI_ALL,
485 [ACPI_D1] = ACPI_SLEEP,
486 [ACPI_D2] = ACPI_SLEEP,
487 [ACPI_D3] = ACPI_NONE
488#endif
489};
490
491
492typedef struct snd_es1968 es1968_t;
493typedef struct snd_esschan esschan_t;
494typedef struct snd_esm_memory esm_memory_t;
495
496/* APU use in the driver */
497enum snd_enum_apu_type {
498 ESM_APU_PCM_PLAY,
499 ESM_APU_PCM_CAPTURE,
500 ESM_APU_PCM_RATECONV,
501 ESM_APU_FREE
502};
503
504/* chip type */
505enum {
506 TYPE_MAESTRO, TYPE_MAESTRO2, TYPE_MAESTRO2E
507};
508
509/* DMA Hack! */
510struct snd_esm_memory {
511 struct snd_dma_buffer buf;
512 int empty; /* status */
513 struct list_head list;
514};
515
516/* Playback Channel */
517struct snd_esschan {
518 int running;
519
520 u8 apu[4];
521 u8 apu_mode[4];
522
523 /* playback/capture pcm buffer */
524 esm_memory_t *memory;
525 /* capture mixer buffer */
526 esm_memory_t *mixbuf;
527
528 unsigned int hwptr; /* current hw pointer in bytes */
529 unsigned int count; /* sample counter in bytes */
530 unsigned int dma_size; /* total buffer size in bytes */
531 unsigned int frag_size; /* period size in bytes */
532 unsigned int wav_shift;
533 u16 base[4]; /* offset for ptr */
534
535 /* stereo/16bit flag */
536 unsigned char fmt;
537 int mode; /* playback / capture */
538
539 int bob_freq; /* required timer frequency */
540
541 snd_pcm_substream_t *substream;
542
543 /* linked list */
544 struct list_head list;
545
546#ifdef CONFIG_PM
547 u16 wc_map[4];
548#endif
549};
550
551struct snd_es1968 {
552 /* Module Config */
553 int total_bufsize; /* in bytes */
554
555 int playback_streams, capture_streams;
556
557 unsigned int clock; /* clock */
558 /* for clock measurement */
559 unsigned int in_measurement: 1;
560 unsigned int measure_apu;
561 unsigned int measure_lastpos;
562 unsigned int measure_count;
563
564 /* buffer */
565 struct snd_dma_buffer dma;
566
567 /* Resources... */
568 int irq;
569 unsigned long io_port;
570 int type;
571 struct pci_dev *pci;
572 snd_card_t *card;
573 snd_pcm_t *pcm;
574 int do_pm; /* power-management enabled */
575
576 /* DMA memory block */
577 struct list_head buf_list;
578
579 /* ALSA Stuff */
580 ac97_t *ac97;
581 snd_kcontrol_t *master_switch; /* for h/w volume control */
582 snd_kcontrol_t *master_volume;
583
584 snd_rawmidi_t *rmidi;
585
586 spinlock_t reg_lock;
587 spinlock_t ac97_lock;
588 struct tasklet_struct hwvol_tq;
589 unsigned int in_suspend;
590
591 /* Maestro Stuff */
592 u16 maestro_map[32];
593 int bobclient; /* active timer instancs */
594 int bob_freq; /* timer frequency */
595 struct semaphore memory_mutex; /* memory lock */
596
597 /* APU states */
598 unsigned char apu[NR_APUS];
599
600 /* active substreams */
601 struct list_head substream_list;
602 spinlock_t substream_lock;
603
604#ifdef CONFIG_PM
605 u16 apu_map[NR_APUS][NR_APU_REGS];
606#endif
607};
608
609static irqreturn_t snd_es1968_interrupt(int irq, void *dev_id, struct pt_regs *regs);
610
611static struct pci_device_id snd_es1968_ids[] = {
612 /* Maestro 1 */
613 { 0x1285, 0x0100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, TYPE_MAESTRO },
614 /* Maestro 2 */
615 { 0x125d, 0x1968, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, TYPE_MAESTRO2 },
616 /* Maestro 2E */
617 { 0x125d, 0x1978, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, TYPE_MAESTRO2E },
618 { 0, }
619};
620
621MODULE_DEVICE_TABLE(pci, snd_es1968_ids);
622
623/* *********************
624 * Low Level Funcs! *
625 *********************/
626
627/* no spinlock */
628static void __maestro_write(es1968_t *chip, u16 reg, u16 data)
629{
630 outw(reg, chip->io_port + ESM_INDEX);
631 outw(data, chip->io_port + ESM_DATA);
632 chip->maestro_map[reg] = data;
633}
634
635inline static void maestro_write(es1968_t *chip, u16 reg, u16 data)
636{
637 unsigned long flags;
638 spin_lock_irqsave(&chip->reg_lock, flags);
639 __maestro_write(chip, reg, data);
640 spin_unlock_irqrestore(&chip->reg_lock, flags);
641}
642
643/* no spinlock */
644static u16 __maestro_read(es1968_t *chip, u16 reg)
645{
646 if (READABLE_MAP & (1 << reg)) {
647 outw(reg, chip->io_port + ESM_INDEX);
648 chip->maestro_map[reg] = inw(chip->io_port + ESM_DATA);
649 }
650 return chip->maestro_map[reg];
651}
652
653inline static u16 maestro_read(es1968_t *chip, u16 reg)
654{
655 unsigned long flags;
656 u16 result;
657 spin_lock_irqsave(&chip->reg_lock, flags);
658 result = __maestro_read(chip, reg);
659 spin_unlock_irqrestore(&chip->reg_lock, flags);
660 return result;
661}
662
663/* Wait for the codec bus to be free */
664static int snd_es1968_ac97_wait(es1968_t *chip)
665{
666 int timeout = 100000;
667
668 while (timeout-- > 0) {
669 if (!(inb(chip->io_port + ESM_AC97_INDEX) & 1))
670 return 0;
671 cond_resched();
672 }
673 snd_printd("es1968: ac97 timeout\n");
674 return 1; /* timeout */
675}
676
677static void snd_es1968_ac97_write(ac97_t *ac97, unsigned short reg, unsigned short val)
678{
679 es1968_t *chip = ac97->private_data;
680 unsigned long flags;
681
682 snd_es1968_ac97_wait(chip);
683
684 /* Write the bus */
685 spin_lock_irqsave(&chip->ac97_lock, flags);
686 outw(val, chip->io_port + ESM_AC97_DATA);
687 /*msleep(1);*/
688 outb(reg, chip->io_port + ESM_AC97_INDEX);
689 /*msleep(1);*/
690 spin_unlock_irqrestore(&chip->ac97_lock, flags);
691}
692
693static unsigned short snd_es1968_ac97_read(ac97_t *ac97, unsigned short reg)
694{
695 u16 data = 0;
696 es1968_t *chip = ac97->private_data;
697 unsigned long flags;
698
699 snd_es1968_ac97_wait(chip);
700
701 spin_lock_irqsave(&chip->ac97_lock, flags);
702 outb(reg | 0x80, chip->io_port + ESM_AC97_INDEX);
703 /*msleep(1);*/
704
705 if (! snd_es1968_ac97_wait(chip)) {
706 data = inw(chip->io_port + ESM_AC97_DATA);
707 /*msleep(1);*/
708 }
709 spin_unlock_irqrestore(&chip->ac97_lock, flags);
710
711 return data;
712}
713
714/* no spinlock */
715static void apu_index_set(es1968_t *chip, u16 index)
716{
717 int i;
718 __maestro_write(chip, IDR1_CRAM_POINTER, index);
719 for (i = 0; i < 1000; i++)
720 if (__maestro_read(chip, IDR1_CRAM_POINTER) == index)
721 return;
722 snd_printd("es1968: APU register select failed. (Timeout)\n");
723}
724
725/* no spinlock */
726static void apu_data_set(es1968_t *chip, u16 data)
727{
728 int i;
729 for (i = 0; i < 1000; i++) {
730 if (__maestro_read(chip, IDR0_DATA_PORT) == data)
731 return;
732 __maestro_write(chip, IDR0_DATA_PORT, data);
733 }
734 snd_printd("es1968: APU register set probably failed (Timeout)!\n");
735}
736
737/* no spinlock */
738static void __apu_set_register(es1968_t *chip, u16 channel, u8 reg, u16 data)
739{
740 snd_assert(channel < NR_APUS, return);
741#ifdef CONFIG_PM
742 chip->apu_map[channel][reg] = data;
743#endif
744 reg |= (channel << 4);
745 apu_index_set(chip, reg);
746 apu_data_set(chip, data);
747}
748
749inline static void apu_set_register(es1968_t *chip, u16 channel, u8 reg, u16 data)
750{
751 unsigned long flags;
752 spin_lock_irqsave(&chip->reg_lock, flags);
753 __apu_set_register(chip, channel, reg, data);
754 spin_unlock_irqrestore(&chip->reg_lock, flags);
755}
756
757static u16 __apu_get_register(es1968_t *chip, u16 channel, u8 reg)
758{
759 snd_assert(channel < NR_APUS, return 0);
760 reg |= (channel << 4);
761 apu_index_set(chip, reg);
762 return __maestro_read(chip, IDR0_DATA_PORT);
763}
764
765inline static u16 apu_get_register(es1968_t *chip, u16 channel, u8 reg)
766{
767 unsigned long flags;
768 u16 v;
769 spin_lock_irqsave(&chip->reg_lock, flags);
770 v = __apu_get_register(chip, channel, reg);
771 spin_unlock_irqrestore(&chip->reg_lock, flags);
772 return v;
773}
774
775#if 0 /* ASSP is not supported */
776
777static void assp_set_register(es1968_t *chip, u32 reg, u32 value)
778{
779 unsigned long flags;
780
781 spin_lock_irqsave(&chip->reg_lock, flags);
782 outl(reg, chip->io_port + ASSP_INDEX);
783 outl(value, chip->io_port + ASSP_DATA);
784 spin_unlock_irqrestore(&chip->reg_lock, flags);
785}
786
787static u32 assp_get_register(es1968_t *chip, u32 reg)
788{
789 unsigned long flags;
790 u32 value;
791
792 spin_lock_irqsave(&chip->reg_lock, flags);
793 outl(reg, chip->io_port + ASSP_INDEX);
794 value = inl(chip->io_port + ASSP_DATA);
795 spin_unlock_irqrestore(&chip->reg_lock, flags);
796
797 return value;
798}
799
800#endif
801
802static void wave_set_register(es1968_t *chip, u16 reg, u16 value)
803{
804 unsigned long flags;
805
806 spin_lock_irqsave(&chip->reg_lock, flags);
807 outw(reg, chip->io_port + WC_INDEX);
808 outw(value, chip->io_port + WC_DATA);
809 spin_unlock_irqrestore(&chip->reg_lock, flags);
810}
811
812static u16 wave_get_register(es1968_t *chip, u16 reg)
813{
814 unsigned long flags;
815 u16 value;
816
817 spin_lock_irqsave(&chip->reg_lock, flags);
818 outw(reg, chip->io_port + WC_INDEX);
819 value = inw(chip->io_port + WC_DATA);
820 spin_unlock_irqrestore(&chip->reg_lock, flags);
821
822 return value;
823}
824
825/* *******************
826 * Bob the Timer! *
827 *******************/
828
829static void snd_es1968_bob_stop(es1968_t *chip)
830{
831 u16 reg;
832
833 reg = __maestro_read(chip, 0x11);
834 reg &= ~ESM_BOB_ENABLE;
835 __maestro_write(chip, 0x11, reg);
836 reg = __maestro_read(chip, 0x17);
837 reg &= ~ESM_BOB_START;
838 __maestro_write(chip, 0x17, reg);
839}
840
841static void snd_es1968_bob_start(es1968_t *chip)
842{
843 int prescale;
844 int divide;
845
846 /* compute ideal interrupt frequency for buffer size & play rate */
847 /* first, find best prescaler value to match freq */
848 for (prescale = 5; prescale < 12; prescale++)
849 if (chip->bob_freq > (ESS_SYSCLK >> (prescale + 9)))
850 break;
851
852 /* next, back off prescaler whilst getting divider into optimum range */
853 divide = 1;
854 while ((prescale > 5) && (divide < 32)) {
855 prescale--;
856 divide <<= 1;
857 }
858 divide >>= 1;
859
860 /* now fine-tune the divider for best match */
861 for (; divide < 31; divide++)
862 if (chip->bob_freq >
863 ((ESS_SYSCLK >> (prescale + 9)) / (divide + 1))) break;
864
865 /* divide = 0 is illegal, but don't let prescale = 4! */
866 if (divide == 0) {
867 divide++;
868 if (prescale > 5)
869 prescale--;
870 } else if (divide > 1)
871 divide--;
872
873 __maestro_write(chip, 6, 0x9000 | (prescale << 5) | divide); /* set reg */
874
875 /* Now set IDR 11/17 */
876 __maestro_write(chip, 0x11, __maestro_read(chip, 0x11) | 1);
877 __maestro_write(chip, 0x17, __maestro_read(chip, 0x17) | 1);
878}
879
880/* call with substream spinlock */
881static void snd_es1968_bob_inc(es1968_t *chip, int freq)
882{
883 chip->bobclient++;
884 if (chip->bobclient == 1) {
885 chip->bob_freq = freq;
886 snd_es1968_bob_start(chip);
887 } else if (chip->bob_freq < freq) {
888 snd_es1968_bob_stop(chip);
889 chip->bob_freq = freq;
890 snd_es1968_bob_start(chip);
891 }
892}
893
894/* call with substream spinlock */
895static void snd_es1968_bob_dec(es1968_t *chip)
896{
897 chip->bobclient--;
898 if (chip->bobclient <= 0)
899 snd_es1968_bob_stop(chip);
900 else if (chip->bob_freq > ESM_BOB_FREQ) {
901 /* check reduction of timer frequency */
902 struct list_head *p;
903 int max_freq = ESM_BOB_FREQ;
904 list_for_each(p, &chip->substream_list) {
905 esschan_t *es = list_entry(p, esschan_t, list);
906 if (max_freq < es->bob_freq)
907 max_freq = es->bob_freq;
908 }
909 if (max_freq != chip->bob_freq) {
910 snd_es1968_bob_stop(chip);
911 chip->bob_freq = max_freq;
912 snd_es1968_bob_start(chip);
913 }
914 }
915}
916
917static int
918snd_es1968_calc_bob_rate(es1968_t *chip, esschan_t *es,
919 snd_pcm_runtime_t *runtime)
920{
921 /* we acquire 4 interrupts per period for precise control.. */
922 int freq = runtime->rate * 4;
923 if (es->fmt & ESS_FMT_STEREO)
924 freq <<= 1;
925 if (es->fmt & ESS_FMT_16BIT)
926 freq <<= 1;
927 freq /= es->frag_size;
928 if (freq < ESM_BOB_FREQ)
929 freq = ESM_BOB_FREQ;
930 else if (freq > ESM_BOB_FREQ_MAX)
931 freq = ESM_BOB_FREQ_MAX;
932 return freq;
933}
934
935
936/*************
937 * PCM Part *
938 *************/
939
940static u32 snd_es1968_compute_rate(es1968_t *chip, u32 freq)
941{
942 u32 rate = (freq << 16) / chip->clock;
943#if 0 /* XXX: do we need this? */
944 if (rate > 0x10000)
945 rate = 0x10000;
946#endif
947 return rate;
948}
949
950/* get current pointer */
951inline static unsigned int
952snd_es1968_get_dma_ptr(es1968_t *chip, esschan_t *es)
953{
954 unsigned int offset;
955
956 offset = apu_get_register(chip, es->apu[0], 5);
957
958 offset -= es->base[0];
959
960 return (offset & 0xFFFE); /* hardware is in words */
961}
962
963static void snd_es1968_apu_set_freq(es1968_t *chip, int apu, int freq)
964{
965 apu_set_register(chip, apu, 2,
966 (apu_get_register(chip, apu, 2) & 0x00FF) |
967 ((freq & 0xff) << 8) | 0x10);
968 apu_set_register(chip, apu, 3, freq >> 8);
969}
970
971/* spin lock held */
972inline static void snd_es1968_trigger_apu(es1968_t *esm, int apu, int mode)
973{
974 /* set the APU mode */
975 __apu_set_register(esm, apu, 0,
976 (__apu_get_register(esm, apu, 0) & 0xff0f) |
977 (mode << 4));
978}
979
980static void snd_es1968_pcm_start(es1968_t *chip, esschan_t *es)
981{
982 spin_lock(&chip->reg_lock);
983 __apu_set_register(chip, es->apu[0], 5, es->base[0]);
984 snd_es1968_trigger_apu(chip, es->apu[0], es->apu_mode[0]);
985 if (es->mode == ESM_MODE_CAPTURE) {
986 __apu_set_register(chip, es->apu[2], 5, es->base[2]);
987 snd_es1968_trigger_apu(chip, es->apu[2], es->apu_mode[2]);
988 }
989 if (es->fmt & ESS_FMT_STEREO) {
990 __apu_set_register(chip, es->apu[1], 5, es->base[1]);
991 snd_es1968_trigger_apu(chip, es->apu[1], es->apu_mode[1]);
992 if (es->mode == ESM_MODE_CAPTURE) {
993 __apu_set_register(chip, es->apu[3], 5, es->base[3]);
994 snd_es1968_trigger_apu(chip, es->apu[3], es->apu_mode[3]);
995 }
996 }
997 spin_unlock(&chip->reg_lock);
998}
999
1000static void snd_es1968_pcm_stop(es1968_t *chip, esschan_t *es)
1001{
1002 spin_lock(&chip->reg_lock);
1003 snd_es1968_trigger_apu(chip, es->apu[0], 0);
1004 snd_es1968_trigger_apu(chip, es->apu[1], 0);
1005 if (es->mode == ESM_MODE_CAPTURE) {
1006 snd_es1968_trigger_apu(chip, es->apu[2], 0);
1007 snd_es1968_trigger_apu(chip, es->apu[3], 0);
1008 }
1009 spin_unlock(&chip->reg_lock);
1010}
1011
1012/* set the wavecache control reg */
1013static void snd_es1968_program_wavecache(es1968_t *chip, esschan_t *es,
1014 int channel, u32 addr, int capture)
1015{
1016 u32 tmpval = (addr - 0x10) & 0xFFF8;
1017
1018 if (! capture) {
1019 if (!(es->fmt & ESS_FMT_16BIT))
1020 tmpval |= 4; /* 8bit */
1021 if (es->fmt & ESS_FMT_STEREO)
1022 tmpval |= 2; /* stereo */
1023 }
1024
1025 /* set the wavecache control reg */
1026 wave_set_register(chip, es->apu[channel] << 3, tmpval);
1027
1028#ifdef CONFIG_PM
1029 es->wc_map[channel] = tmpval;
1030#endif
1031}
1032
1033
1034static void snd_es1968_playback_setup(es1968_t *chip, esschan_t *es,
1035 snd_pcm_runtime_t *runtime)
1036{
1037 u32 pa;
1038 int high_apu = 0;
1039 int channel, apu;
1040 int i, size;
1041 unsigned long flags;
1042 u32 freq;
1043
1044 size = es->dma_size >> es->wav_shift;
1045
1046 if (es->fmt & ESS_FMT_STEREO)
1047 high_apu++;
1048
1049 for (channel = 0; channel <= high_apu; channel++) {
1050 apu = es->apu[channel];
1051
1052 snd_es1968_program_wavecache(chip, es, channel, es->memory->buf.addr, 0);
1053
1054 /* Offset to PCMBAR */
1055 pa = es->memory->buf.addr;
1056 pa -= chip->dma.addr;
1057 pa >>= 1; /* words */
1058
1059 pa |= 0x00400000; /* System RAM (Bit 22) */
1060
1061 if (es->fmt & ESS_FMT_STEREO) {
1062 /* Enable stereo */
1063 if (channel)
1064 pa |= 0x00800000; /* (Bit 23) */
1065 if (es->fmt & ESS_FMT_16BIT)
1066 pa >>= 1;
1067 }
1068
1069 /* base offset of dma calcs when reading the pointer
1070 on this left one */
1071 es->base[channel] = pa & 0xFFFF;
1072
1073 for (i = 0; i < 16; i++)
1074 apu_set_register(chip, apu, i, 0x0000);
1075
1076 /* Load the buffer into the wave engine */
1077 apu_set_register(chip, apu, 4, ((pa >> 16) & 0xFF) << 8);
1078 apu_set_register(chip, apu, 5, pa & 0xFFFF);
1079 apu_set_register(chip, apu, 6, (pa + size) & 0xFFFF);
1080 /* setting loop == sample len */
1081 apu_set_register(chip, apu, 7, size);
1082
1083 /* clear effects/env.. */
1084 apu_set_register(chip, apu, 8, 0x0000);
1085 /* set amp now to 0xd0 (?), low byte is 'amplitude dest'? */
1086 apu_set_register(chip, apu, 9, 0xD000);
1087
1088 /* clear routing stuff */
1089 apu_set_register(chip, apu, 11, 0x0000);
1090 /* dma on, no envelopes, filter to all 1s) */
1091 apu_set_register(chip, apu, 0, 0x400F);
1092
1093 if (es->fmt & ESS_FMT_16BIT)
1094 es->apu_mode[channel] = ESM_APU_16BITLINEAR;
1095 else
1096 es->apu_mode[channel] = ESM_APU_8BITLINEAR;
1097
1098 if (es->fmt & ESS_FMT_STEREO) {
1099 /* set panning: left or right */
1100 /* Check: different panning. On my Canyon 3D Chipset the
1101 Channels are swapped. I don't know, about the output
1102 to the SPDif Link. Perhaps you have to change this
1103 and not the APU Regs 4-5. */
1104 apu_set_register(chip, apu, 10,
1105 0x8F00 | (channel ? 0 : 0x10));
1106 es->apu_mode[channel] += 1; /* stereo */
1107 } else
1108 apu_set_register(chip, apu, 10, 0x8F08);
1109 }
1110
1111 spin_lock_irqsave(&chip->reg_lock, flags);
1112 /* clear WP interrupts */
1113 outw(1, chip->io_port + 0x04);
1114 /* enable WP ints */
1115 outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ);
1116 spin_unlock_irqrestore(&chip->reg_lock, flags);
1117
1118 freq = runtime->rate;
1119 /* set frequency */
1120 if (freq > 48000)
1121 freq = 48000;
1122 if (freq < 4000)
1123 freq = 4000;
1124
1125 /* hmmm.. */
1126 if (!(es->fmt & ESS_FMT_16BIT) && !(es->fmt & ESS_FMT_STEREO))
1127 freq >>= 1;
1128
1129 freq = snd_es1968_compute_rate(chip, freq);
1130
1131 /* Load the frequency, turn on 6dB */
1132 snd_es1968_apu_set_freq(chip, es->apu[0], freq);
1133 snd_es1968_apu_set_freq(chip, es->apu[1], freq);
1134}
1135
1136
1137static void init_capture_apu(es1968_t *chip, esschan_t *es, int channel,
1138 unsigned int pa, unsigned int bsize,
1139 int mode, int route)
1140{
1141 int i, apu = es->apu[channel];
1142
1143 es->apu_mode[channel] = mode;
1144
1145 /* set the wavecache control reg */
1146 snd_es1968_program_wavecache(chip, es, channel, pa, 1);
1147
1148 /* Offset to PCMBAR */
1149 pa -= chip->dma.addr;
1150 pa >>= 1; /* words */
1151
1152 /* base offset of dma calcs when reading the pointer
1153 on this left one */
1154 es->base[channel] = pa & 0xFFFF;
1155 pa |= 0x00400000; /* bit 22 -> System RAM */
1156
1157 /* Begin loading the APU */
1158 for (i = 0; i < 16; i++)
1159 apu_set_register(chip, apu, i, 0x0000);
1160
1161 /* need to enable subgroups.. and we should probably
1162 have different groups for different /dev/dsps.. */
1163 apu_set_register(chip, apu, 2, 0x8);
1164
1165 /* Load the buffer into the wave engine */
1166 apu_set_register(chip, apu, 4, ((pa >> 16) & 0xFF) << 8);
1167 apu_set_register(chip, apu, 5, pa & 0xFFFF);
1168 apu_set_register(chip, apu, 6, (pa + bsize) & 0xFFFF);
1169 apu_set_register(chip, apu, 7, bsize);
1170 /* clear effects/env.. */
1171 apu_set_register(chip, apu, 8, 0x00F0);
1172 /* amplitude now? sure. why not. */
1173 apu_set_register(chip, apu, 9, 0x0000);
1174 /* set filter tune, radius, polar pan */
1175 apu_set_register(chip, apu, 10, 0x8F08);
1176 /* route input */
1177 apu_set_register(chip, apu, 11, route);
1178 /* dma on, no envelopes, filter to all 1s) */
1179 apu_set_register(chip, apu, 0, 0x400F);
1180}
1181
1182
1183static void snd_es1968_capture_setup(es1968_t *chip, esschan_t *es,
1184 snd_pcm_runtime_t *runtime)
1185{
1186 int size;
1187 u32 freq;
1188 unsigned long flags;
1189
1190 size = es->dma_size >> es->wav_shift;
1191
1192 /* APU assignments:
1193 0 = mono/left SRC
1194 1 = right SRC
1195 2 = mono/left Input Mixer
1196 3 = right Input Mixer
1197 */
1198 /* data seems to flow from the codec, through an apu into
1199 the 'mixbuf' bit of page, then through the SRC apu
1200 and out to the real 'buffer'. ok. sure. */
1201
1202 /* input mixer (left/mono) */
1203 /* parallel in crap, see maestro reg 0xC [8-11] */
1204 init_capture_apu(chip, es, 2,
1205 es->mixbuf->buf.addr, ESM_MIXBUF_SIZE/4, /* in words */
1206 ESM_APU_INPUTMIXER, 0x14);
1207 /* SRC (left/mono); get input from inputing apu */
1208 init_capture_apu(chip, es, 0, es->memory->buf.addr, size,
1209 ESM_APU_SRCONVERTOR, es->apu[2]);
1210 if (es->fmt & ESS_FMT_STEREO) {
1211 /* input mixer (right) */
1212 init_capture_apu(chip, es, 3,
1213 es->mixbuf->buf.addr + ESM_MIXBUF_SIZE/2,
1214 ESM_MIXBUF_SIZE/4, /* in words */
1215 ESM_APU_INPUTMIXER, 0x15);
1216 /* SRC (right) */
1217 init_capture_apu(chip, es, 1,
1218 es->memory->buf.addr + size*2, size,
1219 ESM_APU_SRCONVERTOR, es->apu[3]);
1220 }
1221 freq = runtime->rate;
1222 /* Sample Rate conversion APUs don't like 0x10000 for their rate */
1223 if (freq > 47999)
1224 freq = 47999;
1225 if (freq < 4000)
1226 freq = 4000;
1227
1228 freq = snd_es1968_compute_rate(chip, freq);
1229
1230 /* Load the frequency, turn on 6dB */
1231 snd_es1968_apu_set_freq(chip, es->apu[0], freq);
1232 snd_es1968_apu_set_freq(chip, es->apu[1], freq);
1233
1234 /* fix mixer rate at 48khz. and its _must_ be 0x10000. */
1235 freq = 0x10000;
1236 snd_es1968_apu_set_freq(chip, es->apu[2], freq);
1237 snd_es1968_apu_set_freq(chip, es->apu[3], freq);
1238
1239 spin_lock_irqsave(&chip->reg_lock, flags);
1240 /* clear WP interrupts */
1241 outw(1, chip->io_port + 0x04);
1242 /* enable WP ints */
1243 outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ);
1244 spin_unlock_irqrestore(&chip->reg_lock, flags);
1245}
1246
1247/*******************
1248 * ALSA Interface *
1249 *******************/
1250
1251static int snd_es1968_pcm_prepare(snd_pcm_substream_t *substream)
1252{
1253 es1968_t *chip = snd_pcm_substream_chip(substream);
1254 snd_pcm_runtime_t *runtime = substream->runtime;
1255 esschan_t *es = runtime->private_data;
1256
1257 es->dma_size = snd_pcm_lib_buffer_bytes(substream);
1258 es->frag_size = snd_pcm_lib_period_bytes(substream);
1259
1260 es->wav_shift = 1; /* maestro handles always 16bit */
1261 es->fmt = 0;
1262 if (snd_pcm_format_width(runtime->format) == 16)
1263 es->fmt |= ESS_FMT_16BIT;
1264 if (runtime->channels > 1) {
1265 es->fmt |= ESS_FMT_STEREO;
1266 if (es->fmt & ESS_FMT_16BIT) /* 8bit is already word shifted */
1267 es->wav_shift++;
1268 }
1269 es->bob_freq = snd_es1968_calc_bob_rate(chip, es, runtime);
1270
1271 switch (es->mode) {
1272 case ESM_MODE_PLAY:
1273 snd_es1968_playback_setup(chip, es, runtime);
1274 break;
1275 case ESM_MODE_CAPTURE:
1276 snd_es1968_capture_setup(chip, es, runtime);
1277 break;
1278 }
1279
1280 return 0;
1281}
1282
1283static int snd_es1968_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
1284{
1285 es1968_t *chip = snd_pcm_substream_chip(substream);
1286 esschan_t *es = substream->runtime->private_data;
1287
1288 spin_lock(&chip->substream_lock);
1289 switch (cmd) {
1290 case SNDRV_PCM_TRIGGER_START:
1291 case SNDRV_PCM_TRIGGER_RESUME:
1292 if (es->running)
1293 break;
1294 snd_es1968_bob_inc(chip, es->bob_freq);
1295 es->count = 0;
1296 es->hwptr = 0;
1297 snd_es1968_pcm_start(chip, es);
1298 es->running = 1;
1299 break;
1300 case SNDRV_PCM_TRIGGER_STOP:
1301 case SNDRV_PCM_TRIGGER_SUSPEND:
1302 if (! es->running)
1303 break;
1304 snd_es1968_pcm_stop(chip, es);
1305 es->running = 0;
1306 snd_es1968_bob_dec(chip);
1307 break;
1308 }
1309 spin_unlock(&chip->substream_lock);
1310 return 0;
1311}
1312
1313static snd_pcm_uframes_t snd_es1968_pcm_pointer(snd_pcm_substream_t *substream)
1314{
1315 es1968_t *chip = snd_pcm_substream_chip(substream);
1316 esschan_t *es = substream->runtime->private_data;
1317 unsigned int ptr;
1318
1319 ptr = snd_es1968_get_dma_ptr(chip, es) << es->wav_shift;
1320
1321 return bytes_to_frames(substream->runtime, ptr % es->dma_size);
1322}
1323
1324static snd_pcm_hardware_t snd_es1968_playback = {
1325 /* info: */ (SNDRV_PCM_INFO_MMAP |
1326 SNDRV_PCM_INFO_MMAP_VALID |
1327 SNDRV_PCM_INFO_INTERLEAVED |
1328 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1329 /*SNDRV_PCM_INFO_PAUSE |*/
1330 SNDRV_PCM_INFO_RESUME),
1331 /* formats: */ SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1332 /* rates: */ SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1333 /* rate_min: */ 4000,
1334 /* rate_max: */ 48000,
1335 /* channels_min: */ 1,
1336 /* channels_max: */ 2,
1337 /* buffer_bytes_max: */ 65536,
1338 /* period_bytes_min: */ 256,
1339 /* period_bytes_max: */ 65536,
1340 /* periods_min: */ 1,
1341 /* periods_max: */ 1024,
1342 /* fifo_size: */ 0,
1343};
1344
1345static snd_pcm_hardware_t snd_es1968_capture = {
1346 /* info: */ (SNDRV_PCM_INFO_NONINTERLEAVED |
1347 SNDRV_PCM_INFO_MMAP |
1348 SNDRV_PCM_INFO_MMAP_VALID |
1349 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1350 /*SNDRV_PCM_INFO_PAUSE |*/
1351 SNDRV_PCM_INFO_RESUME),
1352 /* formats: */ /*SNDRV_PCM_FMTBIT_U8 |*/ SNDRV_PCM_FMTBIT_S16_LE,
1353 /* rates: */ SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1354 /* rate_min: */ 4000,
1355 /* rate_max: */ 48000,
1356 /* channels_min: */ 1,
1357 /* channels_max: */ 2,
1358 /* buffer_bytes_max: */ 65536,
1359 /* period_bytes_min: */ 256,
1360 /* period_bytes_max: */ 65536,
1361 /* periods_min: */ 1,
1362 /* periods_max: */ 1024,
1363 /* fifo_size: */ 0,
1364};
1365
1366/* *************************
1367 * DMA memory management *
1368 *************************/
1369
1370/* Because the Maestro can only take addresses relative to the PCM base address
1371 register :( */
1372
1373static int calc_available_memory_size(es1968_t *chip)
1374{
1375 struct list_head *p;
1376 int max_size = 0;
1377
1378 down(&chip->memory_mutex);
1379 list_for_each(p, &chip->buf_list) {
1380 esm_memory_t *buf = list_entry(p, esm_memory_t, list);
1381 if (buf->empty && buf->buf.bytes > max_size)
1382 max_size = buf->buf.bytes;
1383 }
1384 up(&chip->memory_mutex);
1385 if (max_size >= 128*1024)
1386 max_size = 127*1024;
1387 return max_size;
1388}
1389
1390/* allocate a new memory chunk with the specified size */
1391static esm_memory_t *snd_es1968_new_memory(es1968_t *chip, int size)
1392{
1393 esm_memory_t *buf;
1394 struct list_head *p;
1395
1396 size = ((size + ESM_MEM_ALIGN - 1) / ESM_MEM_ALIGN) * ESM_MEM_ALIGN;
1397 down(&chip->memory_mutex);
1398 list_for_each(p, &chip->buf_list) {
1399 buf = list_entry(p, esm_memory_t, list);
1400 if (buf->empty && buf->buf.bytes >= size)
1401 goto __found;
1402 }
1403 up(&chip->memory_mutex);
1404 return NULL;
1405
1406__found:
1407 if (buf->buf.bytes > size) {
1408 esm_memory_t *chunk = kmalloc(sizeof(*chunk), GFP_KERNEL);
1409 if (chunk == NULL) {
1410 up(&chip->memory_mutex);
1411 return NULL;
1412 }
1413 chunk->buf = buf->buf;
1414 chunk->buf.bytes -= size;
1415 chunk->buf.area += size;
1416 chunk->buf.addr += size;
1417 chunk->empty = 1;
1418 buf->buf.bytes = size;
1419 list_add(&chunk->list, &buf->list);
1420 }
1421 buf->empty = 0;
1422 up(&chip->memory_mutex);
1423 return buf;
1424}
1425
1426/* free a memory chunk */
1427static void snd_es1968_free_memory(es1968_t *chip, esm_memory_t *buf)
1428{
1429 esm_memory_t *chunk;
1430
1431 down(&chip->memory_mutex);
1432 buf->empty = 1;
1433 if (buf->list.prev != &chip->buf_list) {
1434 chunk = list_entry(buf->list.prev, esm_memory_t, list);
1435 if (chunk->empty) {
1436 chunk->buf.bytes += buf->buf.bytes;
1437 list_del(&buf->list);
1438 kfree(buf);
1439 buf = chunk;
1440 }
1441 }
1442 if (buf->list.next != &chip->buf_list) {
1443 chunk = list_entry(buf->list.next, esm_memory_t, list);
1444 if (chunk->empty) {
1445 buf->buf.bytes += chunk->buf.bytes;
1446 list_del(&chunk->list);
1447 kfree(chunk);
1448 }
1449 }
1450 up(&chip->memory_mutex);
1451}
1452
1453static void snd_es1968_free_dmabuf(es1968_t *chip)
1454{
1455 struct list_head *p;
1456
1457 if (! chip->dma.area)
1458 return;
1459 snd_dma_reserve_buf(&chip->dma, snd_dma_pci_buf_id(chip->pci));
1460 while ((p = chip->buf_list.next) != &chip->buf_list) {
1461 esm_memory_t *chunk = list_entry(p, esm_memory_t, list);
1462 list_del(p);
1463 kfree(chunk);
1464 }
1465}
1466
1467static int __devinit
1468snd_es1968_init_dmabuf(es1968_t *chip)
1469{
1470 int err;
1471 esm_memory_t *chunk;
1472
1473 chip->dma.dev.type = SNDRV_DMA_TYPE_DEV;
1474 chip->dma.dev.dev = snd_dma_pci_data(chip->pci);
1475 if (! snd_dma_get_reserved_buf(&chip->dma, snd_dma_pci_buf_id(chip->pci))) {
1476 err = snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV,
1477 snd_dma_pci_data(chip->pci),
1478 chip->total_bufsize, &chip->dma);
1479 if (err < 0 || ! chip->dma.area) {
1480 snd_printk("es1968: can't allocate dma pages for size %d\n",
1481 chip->total_bufsize);
1482 return -ENOMEM;
1483 }
1484 if ((chip->dma.addr + chip->dma.bytes - 1) & ~((1 << 28) - 1)) {
1485 snd_dma_free_pages(&chip->dma);
1486 snd_printk("es1968: DMA buffer beyond 256MB.\n");
1487 return -ENOMEM;
1488 }
1489 }
1490
1491 INIT_LIST_HEAD(&chip->buf_list);
1492 /* allocate an empty chunk */
1493 chunk = kmalloc(sizeof(*chunk), GFP_KERNEL);
1494 if (chunk == NULL) {
1495 snd_es1968_free_dmabuf(chip);
1496 return -ENOMEM;
1497 }
1498 memset(chip->dma.area, 0, ESM_MEM_ALIGN);
1499 chunk->buf = chip->dma;
1500 chunk->buf.area += ESM_MEM_ALIGN;
1501 chunk->buf.addr += ESM_MEM_ALIGN;
1502 chunk->buf.bytes -= ESM_MEM_ALIGN;
1503 chunk->empty = 1;
1504 list_add(&chunk->list, &chip->buf_list);
1505
1506 return 0;
1507}
1508
1509/* setup the dma_areas */
1510/* buffer is extracted from the pre-allocated memory chunk */
1511static int snd_es1968_hw_params(snd_pcm_substream_t *substream,
1512 snd_pcm_hw_params_t *hw_params)
1513{
1514 es1968_t *chip = snd_pcm_substream_chip(substream);
1515 snd_pcm_runtime_t *runtime = substream->runtime;
1516 esschan_t *chan = runtime->private_data;
1517 int size = params_buffer_bytes(hw_params);
1518
1519 if (chan->memory) {
1520 if (chan->memory->buf.bytes >= size) {
1521 runtime->dma_bytes = size;
1522 return 0;
1523 }
1524 snd_es1968_free_memory(chip, chan->memory);
1525 }
1526 chan->memory = snd_es1968_new_memory(chip, size);
1527 if (chan->memory == NULL) {
1528 // snd_printd("cannot allocate dma buffer: size = %d\n", size);
1529 return -ENOMEM;
1530 }
1531 snd_pcm_set_runtime_buffer(substream, &chan->memory->buf);
1532 return 1; /* area was changed */
1533}
1534
1535/* remove dma areas if allocated */
1536static int snd_es1968_hw_free(snd_pcm_substream_t * substream)
1537{
1538 es1968_t *chip = snd_pcm_substream_chip(substream);
1539 snd_pcm_runtime_t *runtime = substream->runtime;
1540 esschan_t *chan;
1541
1542 if (runtime->private_data == NULL)
1543 return 0;
1544 chan = runtime->private_data;
1545 if (chan->memory) {
1546 snd_es1968_free_memory(chip, chan->memory);
1547 chan->memory = NULL;
1548 }
1549 return 0;
1550}
1551
1552
1553/*
1554 * allocate APU pair
1555 */
1556static int snd_es1968_alloc_apu_pair(es1968_t *chip, int type)
1557{
1558 int apu;
1559
1560 for (apu = 0; apu < NR_APUS; apu += 2) {
1561 if (chip->apu[apu] == ESM_APU_FREE &&
1562 chip->apu[apu + 1] == ESM_APU_FREE) {
1563 chip->apu[apu] = chip->apu[apu + 1] = type;
1564 return apu;
1565 }
1566 }
1567 return -EBUSY;
1568}
1569
1570/*
1571 * release APU pair
1572 */
1573static void snd_es1968_free_apu_pair(es1968_t *chip, int apu)
1574{
1575 chip->apu[apu] = chip->apu[apu + 1] = ESM_APU_FREE;
1576}
1577
1578
1579/******************
1580 * PCM open/close *
1581 ******************/
1582
1583static int snd_es1968_playback_open(snd_pcm_substream_t *substream)
1584{
1585 es1968_t *chip = snd_pcm_substream_chip(substream);
1586 snd_pcm_runtime_t *runtime = substream->runtime;
1587 esschan_t *es;
1588 int apu1;
1589
1590 /* search 2 APUs */
1591 apu1 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_PLAY);
1592 if (apu1 < 0)
1593 return apu1;
1594
1595 es = kcalloc(1, sizeof(*es), GFP_KERNEL);
1596 if (!es) {
1597 snd_es1968_free_apu_pair(chip, apu1);
1598 return -ENOMEM;
1599 }
1600
1601 es->apu[0] = apu1;
1602 es->apu[1] = apu1 + 1;
1603 es->apu_mode[0] = 0;
1604 es->apu_mode[1] = 0;
1605 es->running = 0;
1606 es->substream = substream;
1607 es->mode = ESM_MODE_PLAY;
1608
1609 runtime->private_data = es;
1610 runtime->hw = snd_es1968_playback;
1611 runtime->hw.buffer_bytes_max = runtime->hw.period_bytes_max =
1612 calc_available_memory_size(chip);
1613#if 0
1614 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1615 1024);
1616#endif
1617 spin_lock_irq(&chip->substream_lock);
1618 list_add(&es->list, &chip->substream_list);
1619 spin_unlock_irq(&chip->substream_lock);
1620
1621 return 0;
1622}
1623
1624static int snd_es1968_capture_open(snd_pcm_substream_t *substream)
1625{
1626 snd_pcm_runtime_t *runtime = substream->runtime;
1627 es1968_t *chip = snd_pcm_substream_chip(substream);
1628 esschan_t *es;
1629 int apu1, apu2;
1630
1631 apu1 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_CAPTURE);
1632 if (apu1 < 0)
1633 return apu1;
1634
1635 apu2 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_RATECONV);
1636 if (apu2 < 0) {
1637 snd_es1968_free_apu_pair(chip, apu1);
1638 return apu2;
1639 }
1640
1641 es = kcalloc(1, sizeof(*es), GFP_KERNEL);
1642 if (!es) {
1643 snd_es1968_free_apu_pair(chip, apu1);
1644 snd_es1968_free_apu_pair(chip, apu2);
1645 return -ENOMEM;
1646 }
1647
1648 es->apu[0] = apu1;
1649 es->apu[1] = apu1 + 1;
1650 es->apu[2] = apu2;
1651 es->apu[3] = apu2 + 1;
1652 es->apu_mode[0] = 0;
1653 es->apu_mode[1] = 0;
1654 es->apu_mode[2] = 0;
1655 es->apu_mode[3] = 0;
1656 es->running = 0;
1657 es->substream = substream;
1658 es->mode = ESM_MODE_CAPTURE;
1659
1660 /* get mixbuffer */
1661 if ((es->mixbuf = snd_es1968_new_memory(chip, ESM_MIXBUF_SIZE)) == NULL) {
1662 snd_es1968_free_apu_pair(chip, apu1);
1663 snd_es1968_free_apu_pair(chip, apu2);
1664 kfree(es);
1665 return -ENOMEM;
1666 }
1667 memset(es->mixbuf->buf.area, 0, ESM_MIXBUF_SIZE);
1668
1669 runtime->private_data = es;
1670 runtime->hw = snd_es1968_capture;
1671 runtime->hw.buffer_bytes_max = runtime->hw.period_bytes_max =
1672 calc_available_memory_size(chip) - 1024; /* keep MIXBUF size */
1673#if 0
1674 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1675 1024);
1676#endif
1677 spin_lock_irq(&chip->substream_lock);
1678 list_add(&es->list, &chip->substream_list);
1679 spin_unlock_irq(&chip->substream_lock);
1680
1681 return 0;
1682}
1683
1684static int snd_es1968_playback_close(snd_pcm_substream_t * substream)
1685{
1686 es1968_t *chip = snd_pcm_substream_chip(substream);
1687 esschan_t *es;
1688
1689 if (substream->runtime->private_data == NULL)
1690 return 0;
1691 es = substream->runtime->private_data;
1692 spin_lock_irq(&chip->substream_lock);
1693 list_del(&es->list);
1694 spin_unlock_irq(&chip->substream_lock);
1695 snd_es1968_free_apu_pair(chip, es->apu[0]);
1696 kfree(es);
1697
1698 return 0;
1699}
1700
1701static int snd_es1968_capture_close(snd_pcm_substream_t * substream)
1702{
1703 es1968_t *chip = snd_pcm_substream_chip(substream);
1704 esschan_t *es;
1705
1706 if (substream->runtime->private_data == NULL)
1707 return 0;
1708 es = substream->runtime->private_data;
1709 spin_lock_irq(&chip->substream_lock);
1710 list_del(&es->list);
1711 spin_unlock_irq(&chip->substream_lock);
1712 snd_es1968_free_memory(chip, es->mixbuf);
1713 snd_es1968_free_apu_pair(chip, es->apu[0]);
1714 snd_es1968_free_apu_pair(chip, es->apu[2]);
1715 kfree(es);
1716
1717 return 0;
1718}
1719static snd_pcm_ops_t snd_es1968_playback_ops = {
1720 /* open: */ snd_es1968_playback_open,
1721 /* close: */ snd_es1968_playback_close,
1722 /* ioctl: */ snd_pcm_lib_ioctl,
1723 /* hw_params:*/ snd_es1968_hw_params,
1724 /* hw_free: */ snd_es1968_hw_free,
1725 /* prepare: */ snd_es1968_pcm_prepare,
1726 /* trigger: */ snd_es1968_pcm_trigger,
1727 /* pointer: */ snd_es1968_pcm_pointer,
1728 0,0
1729};
1730
1731static snd_pcm_ops_t snd_es1968_capture_ops = {
1732 /* open: */ snd_es1968_capture_open,
1733 /* close: */ snd_es1968_capture_close,
1734 /* ioctl: */ snd_pcm_lib_ioctl,
1735 /* hw_params:*/ snd_es1968_hw_params,
1736 /* hw_free: */ snd_es1968_hw_free,
1737 /* prepare: */ snd_es1968_pcm_prepare,
1738 /* trigger: */ snd_es1968_pcm_trigger,
1739 /* pointer: */ snd_es1968_pcm_pointer,
1740 /* copy: */ 0,
1741 0
1742};
1743
1744
1745/*
1746 * measure clock
1747 */
1748#define CLOCK_MEASURE_BUFSIZE 16768 /* enough large for a single shot */
1749
1750static void __devinit es1968_measure_clock(es1968_t *chip)
1751{
1752 int i, apu;
1753 unsigned int pa, offset, t;
1754 esm_memory_t *memory;
1755 struct timeval start_time, stop_time;
1756
1757 if (chip->clock == 0)
1758 chip->clock = 48000; /* default clock value */
1759
1760 /* search 2 APUs (although one apu is enough) */
1761 if ((apu = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_PLAY)) < 0) {
1762 snd_printk("Hmm, cannot find empty APU pair!?\n");
1763 return;
1764 }
1765 if ((memory = snd_es1968_new_memory(chip, CLOCK_MEASURE_BUFSIZE)) == NULL) {
1766 snd_printk("cannot allocate dma buffer - using default clock %d\n", chip->clock);
1767 snd_es1968_free_apu_pair(chip, apu);
1768 return;
1769 }
1770
1771 memset(memory->buf.area, 0, CLOCK_MEASURE_BUFSIZE);
1772
1773 wave_set_register(chip, apu << 3, (memory->buf.addr - 0x10) & 0xfff8);
1774
1775 pa = (unsigned int)((memory->buf.addr - chip->dma.addr) >> 1);
1776 pa |= 0x00400000; /* System RAM (Bit 22) */
1777
1778 /* initialize apu */
1779 for (i = 0; i < 16; i++)
1780 apu_set_register(chip, apu, i, 0x0000);
1781
1782 apu_set_register(chip, apu, 0, 0x400f);
1783 apu_set_register(chip, apu, 4, ((pa >> 16) & 0xff) << 8);
1784 apu_set_register(chip, apu, 5, pa & 0xffff);
1785 apu_set_register(chip, apu, 6, (pa + CLOCK_MEASURE_BUFSIZE/2) & 0xffff);
1786 apu_set_register(chip, apu, 7, CLOCK_MEASURE_BUFSIZE/2);
1787 apu_set_register(chip, apu, 8, 0x0000);
1788 apu_set_register(chip, apu, 9, 0xD000);
1789 apu_set_register(chip, apu, 10, 0x8F08);
1790 apu_set_register(chip, apu, 11, 0x0000);
1791 spin_lock_irq(&chip->reg_lock);
1792 outw(1, chip->io_port + 0x04); /* clear WP interrupts */
1793 outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ); /* enable WP ints */
1794 spin_unlock_irq(&chip->reg_lock);
1795
1796 snd_es1968_apu_set_freq(chip, apu, ((unsigned int)48000 << 16) / chip->clock); /* 48000 Hz */
1797
1798 chip->in_measurement = 1;
1799 chip->measure_apu = apu;
1800 spin_lock_irq(&chip->reg_lock);
1801 snd_es1968_bob_inc(chip, ESM_BOB_FREQ);
1802 __apu_set_register(chip, apu, 5, pa & 0xffff);
1803 snd_es1968_trigger_apu(chip, apu, ESM_APU_16BITLINEAR);
1804 do_gettimeofday(&start_time);
1805 spin_unlock_irq(&chip->reg_lock);
1806 msleep(50);
1807 spin_lock_irq(&chip->reg_lock);
1808 offset = __apu_get_register(chip, apu, 5);
1809 do_gettimeofday(&stop_time);
1810 snd_es1968_trigger_apu(chip, apu, 0); /* stop */
1811 snd_es1968_bob_dec(chip);
1812 chip->in_measurement = 0;
1813 spin_unlock_irq(&chip->reg_lock);
1814
1815 /* check the current position */
1816 offset -= (pa & 0xffff);
1817 offset &= 0xfffe;
1818 offset += chip->measure_count * (CLOCK_MEASURE_BUFSIZE/2);
1819
1820 t = stop_time.tv_sec - start_time.tv_sec;
1821 t *= 1000000;
1822 if (stop_time.tv_usec < start_time.tv_usec)
1823 t -= start_time.tv_usec - stop_time.tv_usec;
1824 else
1825 t += stop_time.tv_usec - start_time.tv_usec;
1826 if (t == 0) {
1827 snd_printk("?? calculation error..\n");
1828 } else {
1829 offset *= 1000;
1830 offset = (offset / t) * 1000 + ((offset % t) * 1000) / t;
1831 if (offset < 47500 || offset > 48500) {
1832 if (offset >= 40000 && offset <= 50000)
1833 chip->clock = (chip->clock * offset) / 48000;
1834 }
1835 printk(KERN_INFO "es1968: clocking to %d\n", chip->clock);
1836 }
1837 snd_es1968_free_memory(chip, memory);
1838 snd_es1968_free_apu_pair(chip, apu);
1839}
1840
1841
1842/*
1843 */
1844static void snd_es1968_pcm_free(snd_pcm_t *pcm)
1845{
1846 es1968_t *esm = pcm->private_data;
1847 snd_es1968_free_dmabuf(esm);
1848 esm->pcm = NULL;
1849}
1850
1851static int __devinit
1852snd_es1968_pcm(es1968_t *chip, int device)
1853{
1854 snd_pcm_t *pcm;
1855 int err;
1856
1857 /* get DMA buffer */
1858 if ((err = snd_es1968_init_dmabuf(chip)) < 0)
1859 return err;
1860
1861 /* set PCMBAR */
1862 wave_set_register(chip, 0x01FC, chip->dma.addr >> 12);
1863 wave_set_register(chip, 0x01FD, chip->dma.addr >> 12);
1864 wave_set_register(chip, 0x01FE, chip->dma.addr >> 12);
1865 wave_set_register(chip, 0x01FF, chip->dma.addr >> 12);
1866
1867 if ((err = snd_pcm_new(chip->card, "ESS Maestro", device,
1868 chip->playback_streams,
1869 chip->capture_streams, &pcm)) < 0)
1870 return err;
1871
1872 pcm->private_data = chip;
1873 pcm->private_free = snd_es1968_pcm_free;
1874
1875 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1968_playback_ops);
1876 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1968_capture_ops);
1877
1878 pcm->info_flags = 0;
1879
1880 strcpy(pcm->name, "ESS Maestro");
1881
1882 chip->pcm = pcm;
1883
1884 return 0;
1885}
1886
1887/*
1888 * update pointer
1889 */
1890static void snd_es1968_update_pcm(es1968_t *chip, esschan_t *es)
1891{
1892 unsigned int hwptr;
1893 unsigned int diff;
1894 snd_pcm_substream_t *subs = es->substream;
1895
1896 if (subs == NULL || !es->running)
1897 return;
1898
1899 hwptr = snd_es1968_get_dma_ptr(chip, es) << es->wav_shift;
1900 hwptr %= es->dma_size;
1901
1902 diff = (es->dma_size + hwptr - es->hwptr) % es->dma_size;
1903
1904 es->hwptr = hwptr;
1905 es->count += diff;
1906
1907 if (es->count > es->frag_size) {
1908 spin_unlock(&chip->substream_lock);
1909 snd_pcm_period_elapsed(subs);
1910 spin_lock(&chip->substream_lock);
1911 es->count %= es->frag_size;
1912 }
1913}
1914
1915/*
1916 */
1917static void es1968_update_hw_volume(unsigned long private_data)
1918{
1919 es1968_t *chip = (es1968_t *) private_data;
1920 int x, val;
1921 unsigned long flags;
1922
1923 /* Figure out which volume control button was pushed,
1924 based on differences from the default register
1925 values. */
1926 x = inb(chip->io_port + 0x1c);
1927 /* Reset the volume control registers. */
1928 outb(0x88, chip->io_port + 0x1c);
1929 outb(0x88, chip->io_port + 0x1d);
1930 outb(0x88, chip->io_port + 0x1e);
1931 outb(0x88, chip->io_port + 0x1f);
1932
1933 if (chip->in_suspend)
1934 return;
1935
1936 if (! chip->master_switch || ! chip->master_volume)
1937 return;
1938
1939 /* FIXME: we can't call snd_ac97_* functions since here is in tasklet. */
1940 spin_lock_irqsave(&chip->ac97_lock, flags);
1941 val = chip->ac97->regs[AC97_MASTER];
1942 if (x & 1) {
1943 /* mute */
1944 val ^= 0x8000;
1945 chip->ac97->regs[AC97_MASTER] = val;
1946 outw(val, chip->io_port + ESM_AC97_DATA);
1947 outb(AC97_MASTER, chip->io_port + ESM_AC97_INDEX);
1948 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1949 &chip->master_switch->id);
1950 } else {
1951 val &= 0x7fff;
1952 if (((x>>1) & 7) > 4) {
1953 /* volume up */
1954 if ((val & 0xff) > 0)
1955 val--;
1956 if ((val & 0xff00) > 0)
1957 val -= 0x0100;
1958 } else {
1959 /* volume down */
1960 if ((val & 0xff) < 0x1f)
1961 val++;
1962 if ((val & 0xff00) < 0x1f00)
1963 val += 0x0100;
1964 }
1965 chip->ac97->regs[AC97_MASTER] = val;
1966 outw(val, chip->io_port + ESM_AC97_DATA);
1967 outb(AC97_MASTER, chip->io_port + ESM_AC97_INDEX);
1968 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1969 &chip->master_volume->id);
1970 }
1971 spin_unlock_irqrestore(&chip->ac97_lock, flags);
1972}
1973
1974/*
1975 * interrupt handler
1976 */
1977static irqreturn_t snd_es1968_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1978{
1979 es1968_t *chip = dev_id;
1980 u32 event;
1981#ifdef TARGET_OS2
1982 int fOurIrq = FALSE;
1983#endif
1984
1985 if (!(event = inb(chip->io_port + 0x1A)))
1986 return IRQ_NONE;
1987
1988#ifdef TARGET_OS2
1989 fOurIrq = TRUE;
1990#endif
1991 outw(inw(chip->io_port + 4) & 1, chip->io_port + 4);
1992
1993 if (event & ESM_HWVOL_IRQ)
1994 tasklet_hi_schedule(&chip->hwvol_tq); /* we'll do this later */
1995
1996 /* else ack 'em all, i imagine */
1997 outb(0xFF, chip->io_port + 0x1A);
1998
1999 if ((event & ESM_MPU401_IRQ) && chip->rmidi) {
2000 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
2001 }
2002
2003 if (event & ESM_SOUND_IRQ) {
2004 struct list_head *p;
2005 spin_lock(&chip->substream_lock);
2006 list_for_each(p, &chip->substream_list) {
2007 esschan_t *es = list_entry(p, esschan_t, list);
2008 if (es->running)
2009 snd_es1968_update_pcm(chip, es);
2010 }
2011 spin_unlock(&chip->substream_lock);
2012 if (chip->in_measurement) {
2013 unsigned int curp = __apu_get_register(chip, chip->measure_apu, 5);
2014 if (curp < chip->measure_lastpos)
2015 chip->measure_count++;
2016 chip->measure_lastpos = curp;
2017 }
2018 }
2019
2020#ifdef TARGET_OS2
2021 if (fOurIrq) {
2022 eoi_irq(irq);
2023 }
2024#endif //TARGET_OS2
2025 return IRQ_HANDLED;
2026}
2027
2028/*
2029 * Mixer stuff
2030 */
2031
2032static int __devinit
2033snd_es1968_mixer(es1968_t *chip)
2034{
2035 ac97_bus_t *pbus;
2036 ac97_template_t ac97;
2037 snd_ctl_elem_id_t id;
2038 int err;
2039 static ac97_bus_ops_t ops = {
2040 0, snd_es1968_ac97_write,
2041 snd_es1968_ac97_read,0,0
2042 };
2043
2044 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
2045 return err;
2046 pbus->no_vra = 1; /* ES1968 doesn't need VRA */
2047
2048 memset(&ac97, 0, sizeof(ac97));
2049 ac97.private_data = chip;
2050 if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97)) < 0)
2051 return err;
2052
2053 /* attach master switch / volumes for h/w volume control */
2054 memset(&id, 0, sizeof(id));
2055 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2056 strcpy(id.name, "Master Playback Switch");
2057 chip->master_switch = snd_ctl_find_id(chip->card, &id);
2058 memset(&id, 0, sizeof(id));
2059 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2060 strcpy(id.name, "Master Playback Volume");
2061 chip->master_volume = snd_ctl_find_id(chip->card, &id);
2062
2063 return 0;
2064}
2065
2066/*
2067 * reset ac97 codec
2068 */
2069
2070static void snd_es1968_ac97_reset(es1968_t *chip)
2071{
2072 unsigned long ioaddr = chip->io_port;
2073
2074 unsigned short save_ringbus_a;
2075 unsigned short save_68;
2076 unsigned short w;
2077 unsigned int vend;
2078
2079 /* save configuration */
2080 save_ringbus_a = inw(ioaddr + 0x36);
2081
2082 //outw(inw(ioaddr + 0x38) & 0xfffc, ioaddr + 0x38); /* clear second codec id? */
2083 /* set command/status address i/o to 1st codec */
2084 outw(inw(ioaddr + 0x3a) & 0xfffc, ioaddr + 0x3a);
2085 outw(inw(ioaddr + 0x3c) & 0xfffc, ioaddr + 0x3c);
2086
2087 /* disable ac link */
2088 outw(0x0000, ioaddr + 0x36);
2089 save_68 = inw(ioaddr + 0x68);
2090 pci_read_config_word(chip->pci, 0x58, &w); /* something magical with gpio and bus arb. */
2091 pci_read_config_dword(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, &vend);
2092 if (w & 1)
2093 save_68 |= 0x10;
2094 outw(0xfffe, ioaddr + 0x64); /* unmask gpio 0 */
2095 outw(0x0001, ioaddr + 0x68); /* gpio write */
2096 outw(0x0000, ioaddr + 0x60); /* write 0 to gpio 0 */
2097 udelay(20);
2098 outw(0x0001, ioaddr + 0x60); /* write 1 to gpio 1 */
2099 msleep(20);
2100
2101 outw(save_68 | 0x1, ioaddr + 0x68); /* now restore .. */
2102 outw((inw(ioaddr + 0x38) & 0xfffc) | 0x1, ioaddr + 0x38);
2103 outw((inw(ioaddr + 0x3a) & 0xfffc) | 0x1, ioaddr + 0x3a);
2104 outw((inw(ioaddr + 0x3c) & 0xfffc) | 0x1, ioaddr + 0x3c);
2105
2106 /* now the second codec */
2107 /* disable ac link */
2108 outw(0x0000, ioaddr + 0x36);
2109 outw(0xfff7, ioaddr + 0x64); /* unmask gpio 3 */
2110 save_68 = inw(ioaddr + 0x68);
2111 outw(0x0009, ioaddr + 0x68); /* gpio write 0 & 3 ?? */
2112 outw(0x0001, ioaddr + 0x60); /* write 1 to gpio */
2113 udelay(20);
2114 outw(0x0009, ioaddr + 0x60); /* write 9 to gpio */
2115 msleep(500);
2116 //outw(inw(ioaddr + 0x38) & 0xfffc, ioaddr + 0x38);
2117 outw(inw(ioaddr + 0x3a) & 0xfffc, ioaddr + 0x3a);
2118 outw(inw(ioaddr + 0x3c) & 0xfffc, ioaddr + 0x3c);
2119
2120#if 0 /* the loop here needs to be much better if we want it.. */
2121 snd_printk("trying software reset\n");
2122 /* try and do a software reset */
2123 outb(0x80 | 0x7c, ioaddr + 0x30);
2124 for (w = 0;; w++) {
2125 if ((inw(ioaddr + 0x30) & 1) == 0) {
2126 if (inb(ioaddr + 0x32) != 0)
2127 break;
2128
2129 outb(0x80 | 0x7d, ioaddr + 0x30);
2130 if (((inw(ioaddr + 0x30) & 1) == 0)
2131 && (inb(ioaddr + 0x32) != 0))
2132 break;
2133 outb(0x80 | 0x7f, ioaddr + 0x30);
2134 if (((inw(ioaddr + 0x30) & 1) == 0)
2135 && (inb(ioaddr + 0x32) != 0))
2136 break;
2137 }
2138
2139 if (w > 10000) {
2140 outb(inb(ioaddr + 0x37) | 0x08, ioaddr + 0x37); /* do a software reset */
2141 msleep(500); /* oh my.. */
2142 outb(inb(ioaddr + 0x37) & ~0x08,
2143 ioaddr + 0x37);
2144 udelay(1);
2145 outw(0x80, ioaddr + 0x30);
2146 for (w = 0; w < 10000; w++) {
2147 if ((inw(ioaddr + 0x30) & 1) == 0)
2148 break;
2149 }
2150 }
2151 }
2152#endif
2153 if (vend == NEC_VERSA_SUBID1 || vend == NEC_VERSA_SUBID2) {
2154 /* turn on external amp? */
2155 outw(0xf9ff, ioaddr + 0x64);
2156 outw(inw(ioaddr + 0x68) | 0x600, ioaddr + 0x68);
2157 outw(0x0209, ioaddr + 0x60);
2158 }
2159
2160 /* restore.. */
2161 outw(save_ringbus_a, ioaddr + 0x36);
2162
2163 /* Turn on the 978 docking chip.
2164 First frob the "master output enable" bit,
2165 then set most of the playback volume control registers to max. */
2166 outb(inb(ioaddr+0xc0)|(1<<5), ioaddr+0xc0);
2167 outb(0xff, ioaddr+0xc3);
2168 outb(0xff, ioaddr+0xc4);
2169 outb(0xff, ioaddr+0xc6);
2170 outb(0xff, ioaddr+0xc8);
2171 outb(0x3f, ioaddr+0xcf);
2172 outb(0x3f, ioaddr+0xd0);
2173}
2174
2175static void snd_es1968_reset(es1968_t *chip)
2176{
2177 /* Reset */
2178 outw(ESM_RESET_MAESTRO | ESM_RESET_DIRECTSOUND,
2179 chip->io_port + ESM_PORT_HOST_IRQ);
2180 udelay(10);
2181 outw(0x0000, chip->io_port + ESM_PORT_HOST_IRQ);
2182 udelay(10);
2183}
2184
2185/*
2186 * power management
2187 */
2188static void snd_es1968_set_acpi(es1968_t *chip, int state)
2189{
2190 u16 active_mask = acpi_state_mask[state];
2191
2192 pci_set_power_state(chip->pci, state);
2193 /* make sure the units we care about are on
2194 XXX we might want to do this before state flipping? */
2195 pci_write_config_word(chip->pci, 0x54, ~ active_mask);
2196 pci_write_config_word(chip->pci, 0x56, ~ active_mask);
2197}
2198
2199
2200/*
2201 * initialize maestro chip
2202 */
2203static void snd_es1968_chip_init(es1968_t *chip)
2204{
2205 struct pci_dev *pci = chip->pci;
2206 int i;
2207 unsigned long iobase = chip->io_port;
2208 u16 w;
2209 u32 n;
2210
2211 /* We used to muck around with pci config space that
2212 * we had no business messing with. We don't know enough
2213 * about the machine to know which DMA mode is appropriate,
2214 * etc. We were guessing wrong on some machines and making
2215 * them unhappy. We now trust in the BIOS to do things right,
2216 * which almost certainly means a new host of problems will
2217 * arise with broken BIOS implementations. screw 'em.
2218 * We're already intolerant of machines that don't assign
2219 * IRQs.
2220 */
2221
2222 /* do config work at full power */
2223 snd_es1968_set_acpi(chip, ACPI_D0);
2224
2225 /* Config Reg A */
2226 pci_read_config_word(pci, ESM_CONFIG_A, &w);
2227
2228 /* Use TDMA for now. TDMA works on all boards, so while its
2229 * not the most efficient its the simplest. */
2230 w &= ~DMA_CLEAR; /* Clear DMA bits */
2231 w |= DMA_TDMA; /* TDMA on */
2232 w &= ~(PIC_SNOOP1 | PIC_SNOOP2); /* Clear Pic Snoop Mode Bits */
2233 w &= ~SAFEGUARD; /* Safeguard off */
2234 w |= POST_WRITE; /* Posted write */
2235 w |= ISA_TIMING; /* ISA timing on */
2236 /* XXX huh? claims to be reserved.. */
2237 w &= ~SWAP_LR; /* swap left/right
2238 seems to only have effect on SB
2239 Emulation */
2240 w &= ~SUBTR_DECODE; /* Subtractive decode off */
2241
2242 pci_write_config_word(pci, ESM_CONFIG_A, w);
2243
2244 /* Config Reg B */
2245
2246 pci_read_config_word(pci, ESM_CONFIG_B, &w);
2247
2248 w &= ~(1 << 15); /* Turn off internal clock multiplier */
2249 /* XXX how do we know which to use? */
2250 w &= ~(1 << 14); /* External clock */
2251
2252 w &= ~SPDIF_CONFB; /* disable S/PDIF output */
2253 w |= HWV_CONFB; /* HWV on */
2254 w |= DEBOUNCE; /* Debounce off: easier to push the HW buttons */
2255 w &= ~GPIO_CONFB; /* GPIO 4:5 */
2256 w |= CHI_CONFB; /* Disconnect from the CHI. Enabling this made a dell 7500 work. */
2257 w &= ~IDMA_CONFB; /* IDMA off (undocumented) */
2258 w &= ~MIDI_FIX; /* MIDI fix off (undoc) */
2259 w &= ~(1 << 1); /* reserved, always write 0 */
2260 w &= ~IRQ_TO_ISA; /* IRQ to ISA off (undoc) */
2261
2262 pci_write_config_word(pci, ESM_CONFIG_B, w);
2263
2264 /* DDMA off */
2265
2266 pci_read_config_word(pci, ESM_DDMA, &w);
2267 w &= ~(1 << 0);
2268 pci_write_config_word(pci, ESM_DDMA, w);
2269
2270 /*
2271 * Legacy mode
2272 */
2273
2274 pci_read_config_word(pci, ESM_LEGACY_AUDIO_CONTROL, &w);
2275
2276 w &= ~ESS_ENABLE_AUDIO; /* Disable Legacy Audio */
2277 w &= ~ESS_ENABLE_SERIAL_IRQ; /* Disable SIRQ */
2278 w &= ~(0x1f); /* disable mpu irq/io, game port, fm, SB */
2279
2280 pci_write_config_word(pci, ESM_LEGACY_AUDIO_CONTROL, w);
2281
2282 /* Set up 978 docking control chip. */
2283 pci_read_config_word(pci, 0x58, &w);
2284 w|=1<<2; /* Enable 978. */
2285 w|=1<<3; /* Turn on 978 hardware volume control. */
2286 w&=~(1<<11); /* Turn on 978 mixer volume control. */
2287 pci_write_config_word(pci, 0x58, w);
2288
2289 /* Sound Reset */
2290
2291 snd_es1968_reset(chip);
2292
2293 /*
2294 * Ring Bus Setup
2295 */
2296
2297 /* setup usual 0x34 stuff.. 0x36 may be chip specific */
2298 outw(0xC090, iobase + ESM_RING_BUS_DEST); /* direct sound, stereo */
2299 udelay(20);
2300 outw(0x3000, iobase + ESM_RING_BUS_CONTR_A); /* enable ringbus/serial */
2301 udelay(20);
2302
2303 /*
2304 * Reset the CODEC
2305 */
2306
2307 snd_es1968_ac97_reset(chip);
2308
2309 /* Ring Bus Control B */
2310
2311 n = inl(iobase + ESM_RING_BUS_CONTR_B);
2312 n &= ~RINGB_EN_SPDIF; /* SPDIF off */
2313 //w |= RINGB_EN_2CODEC; /* enable 2nd codec */
2314 outl(n, iobase + ESM_RING_BUS_CONTR_B);
2315
2316 /* Set hardware volume control registers to midpoints.
2317 We can tell which button was pushed based on how they change. */
2318 outb(0x88, iobase+0x1c);
2319 outb(0x88, iobase+0x1d);
2320 outb(0x88, iobase+0x1e);
2321 outb(0x88, iobase+0x1f);
2322
2323 /* it appears some maestros (dell 7500) only work if these are set,
2324 regardless of wether we use the assp or not. */
2325
2326 outb(0, iobase + ASSP_CONTROL_B);
2327 outb(3, iobase + ASSP_CONTROL_A); /* M: Reserved bits... */
2328 outb(0, iobase + ASSP_CONTROL_C); /* M: Disable ASSP, ASSP IRQ's and FM Port */
2329
2330 /*
2331 * set up wavecache
2332 */
2333 for (i = 0; i < 16; i++) {
2334 /* Write 0 into the buffer area 0x1E0->1EF */
2335 outw(0x01E0 + i, iobase + WC_INDEX);
2336 outw(0x0000, iobase + WC_DATA);
2337
2338 /* The 1.10 test program seem to write 0 into the buffer area
2339 * 0x1D0-0x1DF too.*/
2340 outw(0x01D0 + i, iobase + WC_INDEX);
2341 outw(0x0000, iobase + WC_DATA);
2342 }
2343 wave_set_register(chip, IDR7_WAVE_ROMRAM,
2344 (wave_get_register(chip, IDR7_WAVE_ROMRAM) & 0xFF00));
2345 wave_set_register(chip, IDR7_WAVE_ROMRAM,
2346 wave_get_register(chip, IDR7_WAVE_ROMRAM) | 0x100);
2347 wave_set_register(chip, IDR7_WAVE_ROMRAM,
2348 wave_get_register(chip, IDR7_WAVE_ROMRAM) & ~0x200);
2349 wave_set_register(chip, IDR7_WAVE_ROMRAM,
2350 wave_get_register(chip, IDR7_WAVE_ROMRAM) | ~0x400);
2351
2352
2353 maestro_write(chip, IDR2_CRAM_DATA, 0x0000);
2354 /* Now back to the DirectSound stuff */
2355 /* audio serial configuration.. ? */
2356 maestro_write(chip, 0x08, 0xB004);
2357 maestro_write(chip, 0x09, 0x001B);
2358 maestro_write(chip, 0x0A, 0x8000);
2359 maestro_write(chip, 0x0B, 0x3F37);
2360 maestro_write(chip, 0x0C, 0x0098);
2361
2362 /* parallel in, has something to do with recording :) */
2363 maestro_write(chip, 0x0C,
2364 (maestro_read(chip, 0x0C) & ~0xF000) | 0x8000);
2365 /* parallel out */
2366 maestro_write(chip, 0x0C,
2367 (maestro_read(chip, 0x0C) & ~0x0F00) | 0x0500);
2368
2369 maestro_write(chip, 0x0D, 0x7632);
2370
2371 /* Wave cache control on - test off, sg off,
2372 enable, enable extra chans 1Mb */
2373
2374 w = inw(iobase + WC_CONTROL);
2375
2376 w &= ~0xFA00; /* Seems to be reserved? I don't know */
2377 w |= 0xA000; /* reserved... I don't know */
2378 w &= ~0x0200; /* Channels 56,57,58,59 as Extra Play,Rec Channel enable
2379 Seems to crash the Computer if enabled... */
2380 w |= 0x0100; /* Wave Cache Operation Enabled */
2381 w |= 0x0080; /* Channels 60/61 as Placback/Record enabled */
2382 w &= ~0x0060; /* Clear Wavtable Size */
2383 w |= 0x0020; /* Wavetable Size : 1MB */
2384 /* Bit 4 is reserved */
2385 w &= ~0x000C; /* DMA Stuff? I don't understand what the datasheet means */
2386 /* Bit 1 is reserved */
2387 w &= ~0x0001; /* Test Mode off */
2388
2389 outw(w, iobase + WC_CONTROL);
2390
2391 /* Now clear the APU control ram */
2392 for (i = 0; i < NR_APUS; i++) {
2393 for (w = 0; w < NR_APU_REGS; w++)
2394 apu_set_register(chip, i, w, 0);
2395
2396 }
2397}
2398
2399/* Enable IRQ's */
2400static void snd_es1968_start_irq(es1968_t *chip)
2401{
2402 unsigned short w;
2403 w = ESM_HIRQ_DSIE | ESM_HIRQ_HW_VOLUME;
2404 if (chip->rmidi)
2405 w |= ESM_HIRQ_MPU401;
2406 outw(w, chip->io_port + ESM_PORT_HOST_IRQ);
2407}
2408
2409#ifdef CONFIG_PM
2410/*
2411 * PM support
2412 */
2413static int es1968_suspend(snd_card_t *card, unsigned int state)
2414{
2415 es1968_t *chip = card->pm_private_data;
2416
2417 if (! chip->do_pm)
2418 return 0;
2419
2420 chip->in_suspend = 1;
2421 snd_pcm_suspend_all(chip->pcm);
2422 snd_ac97_suspend(chip->ac97);
2423 snd_es1968_bob_stop(chip);
2424 snd_es1968_set_acpi(chip, ACPI_D3);
2425 return 0;
2426}
2427
2428static int es1968_resume(snd_card_t *card, unsigned int state)
2429{
2430 es1968_t *chip = card->pm_private_data;
2431 struct list_head *p;
2432
2433 if (! chip->do_pm)
2434 return 0;
2435
2436 /* restore all our config */
2437 pci_enable_device(chip->pci);
2438 pci_set_master(chip->pci);
2439 snd_es1968_chip_init(chip);
2440
2441 /* need to restore the base pointers.. */
2442 if (chip->dma.addr) {
2443 /* set PCMBAR */
2444 wave_set_register(chip, 0x01FC, chip->dma.addr >> 12);
2445 // by vladest
2446 // wave_set_register(chip, 0x01FD, chip->dma.addr >> 12);
2447 // wave_set_register(chip, 0x01FE, chip->dma.addr >> 12);
2448 // wave_set_register(chip, 0x01FF, chip->dma.addr >> 12);
2449 // !by vladest
2450
2451 }
2452
2453 snd_es1968_start_irq(chip);
2454
2455 // by vladest 06.10.2003 16:22
2456 // es1968_measure_clock(chip);
2457 //! by vladest
2458
2459 /* restore ac97 state */
2460 snd_ac97_resume(chip->ac97);
2461
2462 list_for_each(p, &chip->substream_list) {
2463 esschan_t *es = list_entry(p, esschan_t, list);
2464 switch (es->mode) {
2465 case ESM_MODE_PLAY:
2466 snd_es1968_playback_setup(chip, es, es->substream->runtime);
2467 break;
2468 case ESM_MODE_CAPTURE:
2469 snd_es1968_capture_setup(chip, es, es->substream->runtime);
2470 break;
2471 }
2472 }
2473 /* start timer again */
2474 if (chip->bobclient)
2475 snd_es1968_bob_start(chip);
2476
2477 chip->in_suspend = 0;
2478 return 0;
2479}
2480
2481#endif /* CONFIG_PM */
2482
2483static int snd_es1968_free(es1968_t *chip)
2484{
2485 if (chip->io_port) {
2486 synchronize_irq(chip->irq);
2487 outw(1, chip->io_port + 0x04); /* clear WP interrupts */
2488 outw(0, chip->io_port + ESM_PORT_HOST_IRQ); /* disable IRQ */
2489 }
2490 if (chip->irq >= 0)
2491 free_irq(chip->irq, (void *)chip);
2492 snd_es1968_set_acpi(chip, ACPI_D3);
2493 chip->master_switch = NULL;
2494 chip->master_volume = NULL;
2495 pci_release_regions(chip->pci);
2496 kfree(chip);
2497 return 0;
2498}
2499
2500static int snd_es1968_dev_free(snd_device_t *device)
2501{
2502 es1968_t *chip = device->device_data;
2503 return snd_es1968_free(chip);
2504}
2505
2506struct ess_device_list {
2507 unsigned short type; /* chip type */
2508 unsigned short vendor; /* subsystem vendor id */
2509};
2510
2511static struct ess_device_list pm_whitelist[] __devinitdata = {
2512 { TYPE_MAESTRO2E, 0x0e11 }, /* Compaq Armada */
2513 { TYPE_MAESTRO2E, 0x1028 },
2514 { TYPE_MAESTRO2E, 0x103c },
2515 { TYPE_MAESTRO2E, 0x1179 },
2516 { TYPE_MAESTRO2E, 0x14c0 }, /* HP omnibook 4150 */
2517 { TYPE_MAESTRO2E, 0x1558 },
2518};
2519
2520static struct ess_device_list mpu_blacklist[] __devinitdata = {
2521 { TYPE_MAESTRO2, 0x125d },
2522};
2523
2524static int __devinit snd_es1968_create(snd_card_t * card,
2525 struct pci_dev *pci,
2526 int total_bufsize,
2527 int play_streams,
2528 int capt_streams,
2529 int chip_type,
2530 int do_pm,
2531 es1968_t **chip_ret)
2532{
2533#ifdef TARGET_OS2
2534 static snd_device_ops_t ops = {
2535 snd_es1968_dev_free,0,0,0
2536 };
2537#else
2538 static snd_device_ops_t ops = {
2539 dev_free: snd_es1968_dev_free,
2540 };
2541#endif
2542 es1968_t *chip;
2543 int i, err;
2544
2545 *chip_ret = NULL;
2546
2547 /* enable PCI device */
2548 if ((err = pci_enable_device(pci)) < 0)
2549 return err;
2550 /* check, if we can restrict PCI DMA transfers to 28 bits */
2551 if (!pci_dma_supported(pci, 0x0fffffff)) {
2552 snd_printk("architecture does not support 28bit PCI busmaster DMA\n");
2553 return -ENXIO;
2554 }
2555 pci_set_consistent_dma_mask(pci, 0x0fffffff);
2556
2557 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
2558 if (! chip)
2559 return -ENOMEM;
2560
2561 /* Set Vars */
2562 chip->type = chip_type;
2563 spin_lock_init(&chip->reg_lock);
2564 spin_lock_init(&chip->substream_lock);
2565 INIT_LIST_HEAD(&chip->buf_list);
2566 INIT_LIST_HEAD(&chip->substream_list);
2567 spin_lock_init(&chip->ac97_lock);
2568 init_MUTEX(&chip->memory_mutex);
2569 tasklet_init(&chip->hwvol_tq, es1968_update_hw_volume, (unsigned long)chip);
2570 chip->card = card;
2571 chip->pci = pci;
2572 chip->irq = -1;
2573 chip->total_bufsize = total_bufsize; /* in bytes */
2574 chip->playback_streams = play_streams;
2575 chip->capture_streams = capt_streams;
2576
2577 if ((err = pci_request_regions(pci, "ESS Maestro")) < 0) {
2578 kfree(chip);
2579 return err;
2580 }
2581 chip->io_port = pci_resource_start(pci, 0);
2582 if (request_irq(pci->irq, snd_es1968_interrupt, SA_INTERRUPT|SA_SHIRQ,
2583 "ESS Maestro", (void*)chip)) {
2584 snd_printk("unable to grab IRQ %d\n", pci->irq);
2585 snd_es1968_free(chip);
2586 return -EBUSY;
2587 }
2588 chip->irq = pci->irq;
2589
2590 /* Clear Maestro_map */
2591 for (i = 0; i < 32; i++)
2592 chip->maestro_map[i] = 0;
2593
2594 /* Clear Apu Map */
2595 for (i = 0; i < NR_APUS; i++)
2596 chip->apu[i] = ESM_APU_FREE;
2597
2598 /* just to be sure */
2599 pci_set_master(pci);
2600
2601 if (do_pm > 1) {
2602 /* disable power-management if not on the whitelist */
2603 unsigned short vend;
2604 pci_read_config_word(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, &vend);
2605 for (i = 0; i < (int)ARRAY_SIZE(pm_whitelist); i++) {
2606 if (chip->type == pm_whitelist[i].type &&
2607 vend == pm_whitelist[i].vendor) {
2608 do_pm = 1;
2609 break;
2610 }
2611 }
2612 if (do_pm > 1) {
2613 /* not matched; disabling pm */
2614 printk(KERN_INFO "es1968: not attempting power management.\n");
2615 do_pm = 0;
2616 }
2617 }
2618 chip->do_pm = do_pm;
2619
2620 snd_es1968_chip_init(chip);
2621
2622 if (chip->do_pm)
2623 snd_card_set_pm_callback(card, es1968_suspend, es1968_resume, chip);
2624
2625 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2626 snd_es1968_free(chip);
2627 return err;
2628 }
2629
2630 *chip_ret = chip;
2631
2632 return 0;
2633}
2634
2635
2636/*
2637 * joystick
2638 */
2639
2640static int snd_es1968_joystick_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
2641{
2642 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2643 uinfo->count = 1;
2644 uinfo->value.integer.min = 0;
2645 uinfo->value.integer.max = 1;
2646 return 0;
2647}
2648
2649static int snd_es1968_joystick_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
2650{
2651 es1968_t *chip = snd_kcontrol_chip(kcontrol);
2652 u16 val;
2653
2654 pci_read_config_word(chip->pci, ESM_LEGACY_AUDIO_CONTROL, &val);
2655 ucontrol->value.integer.value[0] = (val & 0x04) ? 1 : 0;
2656 return 0;
2657}
2658
2659static int snd_es1968_joystick_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
2660{
2661 es1968_t *chip = snd_kcontrol_chip(kcontrol);
2662 u16 val, oval;
2663
2664 pci_read_config_word(chip->pci, ESM_LEGACY_AUDIO_CONTROL, &oval);
2665 val = oval & ~0x04;
2666 if (ucontrol->value.integer.value[0])
2667 val |= 0x04;
2668 if (val != oval) {
2669 pci_write_config_word(chip->pci, ESM_LEGACY_AUDIO_CONTROL, val);
2670 return 1;
2671 }
2672 return 0;
2673}
2674
2675#define num_controls(ary) (sizeof(ary) / sizeof(snd_kcontrol_new_t))
2676
2677static snd_kcontrol_new_t snd_es1968_control_switches[] __devinitdata = {
2678 {
2679 /* .iface = */SNDRV_CTL_ELEM_IFACE_CARD,0,0,
2680 /* .name = */"Joystick",0,0,0,
2681 /* .info = */snd_es1968_joystick_info,
2682 /* .get = */snd_es1968_joystick_get,
2683 /* .put = */snd_es1968_joystick_put,
2684 }
2685};
2686
2687/*
2688 */
2689static int __devinit snd_es1968_probe(struct pci_dev *pci,
2690 const struct pci_device_id *pci_id)
2691{
2692 static int dev;
2693 snd_card_t *card;
2694 es1968_t *chip;
2695 unsigned int i;
2696 int err;
2697
2698 printk("snd_es1968_probe cp1\n");
2699 if (dev >= SNDRV_CARDS)
2700 return -ENODEV;
2701 if (!enable[dev]) {
2702 dev++;
2703 return -ENOENT;
2704 }
2705
2706 printk("snd_es1968_probe cp2\n");
2707 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2708 if (!card)
2709 return -ENOMEM;
2710
2711 printk("snd_es1968_probe cp3\n");
2712
2713 if (total_bufsize[dev] < 128)
2714 total_bufsize[dev] = 128;
2715 if (total_bufsize[dev] > 4096)
2716 total_bufsize[dev] = 4096;
2717 if ((err = snd_es1968_create(card, pci,
2718 total_bufsize[dev] * 1024, /* in bytes */
2719 pcm_substreams_p[dev],
2720 pcm_substreams_c[dev],
2721 pci_id->driver_data,
2722 use_pm[dev],
2723 &chip)) < 0) {
2724 snd_card_free(card);
2725 return err;
2726 }
2727
2728 printk("snd_es1968_probe cp4n");
2729
2730 switch (chip->type) {
2731 case TYPE_MAESTRO2E:
2732 strcpy(card->driver, "ES1978");
2733 strcpy(card->shortname, "ESS ES1978 (Maestro 2E)");
2734 break;
2735 case TYPE_MAESTRO2:
2736 strcpy(card->driver, "ES1968");
2737 strcpy(card->shortname, "ESS ES1968 (Maestro 2)");
2738 break;
2739 case TYPE_MAESTRO:
2740 strcpy(card->driver, "ESM1");
2741 strcpy(card->shortname, "ESS Maestro 1");
2742 break;
2743 }
2744
2745 if ((err = snd_es1968_pcm(chip, 0)) < 0) {
2746 snd_card_free(card);
2747 return err;
2748 }
2749 printk("snd_es1968_probe cp5\n");
2750
2751
2752 if ((err = snd_es1968_mixer(chip)) < 0) {
2753 snd_card_free(card);
2754 return err;
2755 }
2756
2757 printk("snd_es1968_probe cp6\n");
2758
2759 if (enable_mpu[dev] == 2) {
2760 /* check the black list */
2761 unsigned short vend;
2762 pci_read_config_word(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, &vend);
2763 for (i = 0; i < ARRAY_SIZE(mpu_blacklist); i++) {
2764 if (chip->type == mpu_blacklist[i].type &&
2765 vend == mpu_blacklist[i].vendor) {
2766 enable_mpu[dev] = 0;
2767 break;
2768 }
2769 }
2770 }
2771 printk("snd_es1968_probe cp7\n");
2772
2773 if (enable_mpu[dev]) {
2774 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
2775 chip->io_port + ESM_MPU401_PORT, 1,
2776 chip->irq, 0, &chip->rmidi)) < 0) {
2777 printk(KERN_WARNING "es1968: skipping MPU-401 MIDI support..\n");
2778 }
2779 }
2780
2781 printk("snd_es1968_probe cp8\n");
2782
2783 /* card switches */
2784 for (i = 0; i < num_controls(snd_es1968_control_switches); i++) {
2785 err = snd_ctl_add(card, snd_ctl_new1(&snd_es1968_control_switches[i], chip));
2786 if (err < 0) {
2787 snd_card_free(card);
2788 return err;
2789 }
2790 }
2791 printk("snd_es1968_probe cp9\n");
2792
2793
2794 snd_es1968_start_irq(chip);
2795
2796 chip->clock = clock[dev];
2797 if (! chip->clock)
2798 es1968_measure_clock(chip);
2799
2800 printk("snd_es1968_probe cp10\n");
2801
2802 sprintf(card->longname, "%s at 0x%lx, irq %i",
2803 card->shortname, chip->io_port, chip->irq);
2804
2805 if ((err = snd_card_register(card)) < 0) {
2806 snd_card_free(card);
2807 return err;
2808 }
2809 printk("snd_es1968_probe cp11\n");
2810
2811 pci_set_drvdata(pci, card);
2812 printk("snd_es1968_probe cp12\n");
2813
2814 dev++;
2815 return 0;
2816}
2817
2818static void __devexit snd_es1968_remove(struct pci_dev *pci)
2819{
2820 snd_card_free(pci_get_drvdata(pci));
2821 pci_set_drvdata(pci, NULL);
2822}
2823
2824static struct pci_driver driver = {
2825 0, 0, 0,
2826 "ES1968 (ESS Maestro)",
2827 snd_es1968_ids,
2828 snd_es1968_probe,
2829 snd_es1968_remove,
2830 SND_PCI_PM_CALLBACKS
2831};
2832
2833static int __init alsa_card_es1968_init(void)
2834{
2835 int err;
2836
2837 if ((err = pci_module_init(&driver)) < 0) {
2838#ifdef MODULE
2839 // snd_printk("ESS Maestro soundcard not found or device busy\n");
2840#endif
2841 return err;
2842 }
2843 return 0;
2844}
2845
2846static void __exit alsa_card_es1968_exit(void)
2847{
2848#if 0 // do we really need this?
2849 unregister_reboot_notifier(&snd_es1968_nb);
2850#endif
2851 pci_unregister_driver(&driver);
2852}
2853
2854module_init(alsa_card_es1968_init)
2855module_exit(alsa_card_es1968_exit)
2856
2857#ifndef MODULE
2858
2859/* format is: snd-es1968=enable,index,id,
2860 total_bufsize,
2861 pcm_substreams_p,
2862 pcm_substreams_c,
2863 clock,
2864 use_pm,
2865 enable_mpu
2866 */
2867
2868static int __init alsa_card_es1968_setup(char *str)
2869{
2870 static unsigned __initdata nr_dev = 0;
2871
2872 if (nr_dev >= SNDRV_CARDS)
2873 return 0;
2874 (void)(get_option(&str,&enable[nr_dev]) == 2 &&
2875 get_option(&str,&index[nr_dev]) == 2 &&
2876 get_id(&str,&id[nr_dev]) == 2 &&
2877 get_option(&str,&total_bufsize[nr_dev]) == 2 &&
2878 get_option(&str,&pcm_substreams_p[nr_dev]) == 2 &&
2879 get_option(&str,&pcm_substreams_c[nr_dev]) == 2 &&
2880 get_option(&str,&clock[nr_dev]) == 2 &&
2881 get_option(&str,&use_pm[nr_dev]) == 2 &&
2882 get_option(&str,&enable_mpu[nr_dev]) == 2);
2883 nr_dev++;
2884 return 1;
2885}
2886
2887__setup("snd-es1968=", alsa_card_es1968_setup);
2888
2889#endif /* ifndef MODULE */
Note: See TracBrowser for help on using the repository browser.