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

Last change on this file since 777 was 777, checked in by David Azarewicz, 5 months ago

Merge from uniaud32-exp branch

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