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

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

Merge changes from next branch.

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