101 #include <linux/pci.h>
103 #include <linux/slab.h>
104 #include <linux/gameport.h>
105 #include <linux/module.h>
107 #include <linux/input.h>
115 #ifdef CONFIG_SND_ES1968_RADIO
119 #define CARD_NAME "ESS Maestro1/2"
120 #define DRIVER_NAME "ES1968"
129 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
130 #define SUPPORT_JOYSTICK 1
142 #ifdef SUPPORT_JOYSTICK
162 MODULE_PARM_DESC(use_pm,
"Toggle power-management. (0 = off, 1 = on, 2 = auto)");
165 #ifdef SUPPORT_JOYSTICK
175 #define NR_APU_REGS 16
178 #define NEC_VERSA_SUBID1 0x80581033
179 #define NEC_VERSA_SUBID2 0x803c1033
182 #define ESS_FMT_STEREO 0x01
183 #define ESS_FMT_16BIT 0x02
185 #define DAC_RUNNING 1
186 #define ADC_RUNNING 2
190 #define ESS_DISABLE_AUDIO 0x8000
191 #define ESS_ENABLE_SERIAL_IRQ 0x4000
192 #define IO_ADRESS_ALIAS 0x0020
193 #define MPU401_IRQ_ENABLE 0x0010
194 #define MPU401_IO_ENABLE 0x0008
195 #define GAME_IO_ENABLE 0x0004
196 #define FM_IO_ENABLE 0x0002
197 #define SB_IO_ENABLE 0x0001
201 #define PIC_SNOOP1 0x4000
202 #define PIC_SNOOP2 0x2000
203 #define SAFEGUARD 0x0800
204 #define DMA_CLEAR 0x0700
205 #define DMA_DDMA 0x0000
206 #define DMA_TDMA 0x0100
207 #define DMA_PCPCI 0x0200
208 #define POST_WRITE 0x0080
209 #define PCI_TIMING 0x0040
210 #define SWAP_LR 0x0020
211 #define SUBTR_DECODE 0x0002
215 #define SPDIF_CONFB 0x0100
216 #define HWV_CONFB 0x0080
217 #define DEBOUNCE 0x0040
218 #define GPIO_CONFB 0x0020
219 #define CHI_CONFB 0x0010
220 #define IDMA_CONFB 0x0008
221 #define MIDI_FIX 0x0004
222 #define IRQ_TO_ISA 0x0001
225 #define RINGB_2CODEC_ID_MASK 0x0003
226 #define RINGB_DIS_VALIDATION 0x0008
227 #define RINGB_EN_SPDIF 0x0010
228 #define RINGB_EN_2CODEC 0x0020
229 #define RINGB_SING_BIT_DUAL 0x0040
234 #define ESM_INDEX 0x02
235 #define ESM_DATA 0x00
238 #define ESM_AC97_INDEX 0x30
239 #define ESM_AC97_DATA 0x32
240 #define ESM_RING_BUS_DEST 0x34
241 #define ESM_RING_BUS_CONTR_A 0x36
242 #define ESM_RING_BUS_CONTR_B 0x38
243 #define ESM_RING_BUS_SDO 0x3A
246 #define WC_INDEX 0x10
248 #define WC_CONTROL 0x14
251 #define ASSP_INDEX 0x80
252 #define ASSP_MEMORY 0x82
253 #define ASSP_DATA 0x84
254 #define ASSP_CONTROL_A 0xA2
255 #define ASSP_CONTROL_B 0xA4
256 #define ASSP_CONTROL_C 0xA6
257 #define ASSP_HOSTW_INDEX 0xA8
258 #define ASSP_HOSTW_DATA 0xAA
259 #define ASSP_HOSTW_IRQ 0xAC
261 #define ESM_MPU401_PORT 0x98
263 #define ESM_PORT_HOST_IRQ 0x18
265 #define IDR0_DATA_PORT 0x00
266 #define IDR1_CRAM_POINTER 0x01
267 #define IDR2_CRAM_DATA 0x02
268 #define IDR3_WAVE_DATA 0x03
269 #define IDR4_WAVE_PTR_LOW 0x04
270 #define IDR5_WAVE_PTR_HI 0x05
271 #define IDR6_TIMER_CTRL 0x06
272 #define IDR7_WAVE_ROMRAM 0x07
274 #define WRITEABLE_MAP 0xEFFFFF
275 #define READABLE_MAP 0x64003F
279 #define ESM_LEGACY_AUDIO_CONTROL 0x40
280 #define ESM_ACPI_COMMAND 0x54
281 #define ESM_CONFIG_A 0x50
282 #define ESM_CONFIG_B 0x52
283 #define ESM_DDMA 0x60
286 #define ESM_BOB_ENABLE 0x0001
287 #define ESM_BOB_START 0x0001
290 #define ESM_RESET_MAESTRO 0x8000
291 #define ESM_RESET_DIRECTSOUND 0x4000
292 #define ESM_HIRQ_ClkRun 0x0100
293 #define ESM_HIRQ_HW_VOLUME 0x0040
294 #define ESM_HIRQ_HARPO 0x0030
295 #define ESM_HIRQ_ASSP 0x0010
296 #define ESM_HIRQ_DSIE 0x0004
297 #define ESM_HIRQ_MPU401 0x0002
298 #define ESM_HIRQ_SB 0x0001
301 #define ESM_MPU401_IRQ 0x02
302 #define ESM_SB_IRQ 0x01
303 #define ESM_SOUND_IRQ 0x04
304 #define ESM_ASSP_IRQ 0x10
305 #define ESM_HWVOL_IRQ 0x40
307 #define ESS_SYSCLK 50000000
308 #define ESM_BOB_FREQ 200
309 #define ESM_BOB_FREQ_MAX 800
311 #define ESM_FREQ_ESM1 (49152000L / 1024L)
312 #define ESM_FREQ_ESM2 (50000000L / 1024L)
315 #define ESM_APU_MODE_SHIFT 4
316 #define ESM_APU_MODE_MASK (0xf << 4)
317 #define ESM_APU_OFF 0x00
318 #define ESM_APU_16BITLINEAR 0x01
319 #define ESM_APU_16BITSTEREO 0x02
320 #define ESM_APU_8BITLINEAR 0x03
321 #define ESM_APU_8BITSTEREO 0x04
322 #define ESM_APU_8BITDIFF 0x05
323 #define ESM_APU_DIGITALDELAY 0x06
324 #define ESM_APU_DUALTAP 0x07
325 #define ESM_APU_CORRELATOR 0x08
326 #define ESM_APU_INPUTMIXER 0x09
327 #define ESM_APU_WAVETABLE 0x0A
328 #define ESM_APU_SRCONVERTOR 0x0B
329 #define ESM_APU_16BITPINGPONG 0x0C
330 #define ESM_APU_RESERVED1 0x0D
331 #define ESM_APU_RESERVED2 0x0E
332 #define ESM_APU_RESERVED3 0x0F
335 #define ESM_APU_FILTER_Q_SHIFT 0
336 #define ESM_APU_FILTER_Q_MASK (3 << 0)
338 #define ESM_APU_FILTER_LESSQ 0x00
339 #define ESM_APU_FILTER_MOREQ 0x03
341 #define ESM_APU_FILTER_TYPE_SHIFT 2
342 #define ESM_APU_FILTER_TYPE_MASK (3 << 2)
343 #define ESM_APU_ENV_TYPE_SHIFT 8
344 #define ESM_APU_ENV_TYPE_MASK (3 << 8)
345 #define ESM_APU_ENV_STATE_SHIFT 10
346 #define ESM_APU_ENV_STATE_MASK (3 << 10)
347 #define ESM_APU_END_CURVE (1 << 12)
348 #define ESM_APU_INT_ON_LOOP (1 << 13)
349 #define ESM_APU_DMA_ENABLE (1 << 14)
352 #define ESM_APU_SUBMIX_GROUP_SHIRT 0
353 #define ESM_APU_SUBMIX_GROUP_MASK (7 << 0)
354 #define ESM_APU_SUBMIX_MODE (1 << 3)
355 #define ESM_APU_6dB (1 << 4)
356 #define ESM_APU_DUAL_EFFECT (1 << 5)
357 #define ESM_APU_EFFECT_CHANNELS_SHIFT 6
358 #define ESM_APU_EFFECT_CHANNELS_MASK (3 << 6)
361 #define ESM_APU_STEP_SIZE_MASK 0x0fff
364 #define ESM_APU_PHASE_SHIFT 0
365 #define ESM_APU_PHASE_MASK (0xff << 0)
366 #define ESM_APU_WAVE64K_PAGE_SHIFT 8
367 #define ESM_APU_WAVE64K_PAGE_MASK (0xff << 8)
374 #define ESM_APU_EFFECT_GAIN_SHIFT 0
375 #define ESM_APU_EFFECT_GAIN_MASK (0xff << 0)
376 #define ESM_APU_TREMOLO_DEPTH_SHIFT 8
377 #define ESM_APU_TREMOLO_DEPTH_MASK (0xf << 8)
378 #define ESM_APU_TREMOLO_RATE_SHIFT 12
379 #define ESM_APU_TREMOLO_RATE_MASK (0xf << 12)
383 #define ESM_APU_AMPLITUDE_NOW_SHIFT 8
384 #define ESM_APU_AMPLITUDE_NOW_MASK (0xff << 8)
387 #define ESM_APU_POLAR_PAN_SHIFT 0
388 #define ESM_APU_POLAR_PAN_MASK (0x3f << 0)
390 #define ESM_APU_PAN_CENTER_CIRCLE 0x00
391 #define ESM_APU_PAN_MIDDLE_RADIUS 0x01
392 #define ESM_APU_PAN_OUTSIDE_RADIUS 0x02
394 #define ESM_APU_FILTER_TUNING_SHIFT 8
395 #define ESM_APU_FILTER_TUNING_MASK (0xff << 8)
398 #define ESM_APU_DATA_SRC_A_SHIFT 0
399 #define ESM_APU_DATA_SRC_A_MASK (0x7f << 0)
400 #define ESM_APU_INV_POL_A (1 << 7)
401 #define ESM_APU_DATA_SRC_B_SHIFT 8
402 #define ESM_APU_DATA_SRC_B_MASK (0x7f << 8)
403 #define ESM_APU_INV_POL_B (1 << 15)
405 #define ESM_APU_VIBRATO_RATE_SHIFT 0
406 #define ESM_APU_VIBRATO_RATE_MASK (0xf << 0)
407 #define ESM_APU_VIBRATO_DEPTH_SHIFT 4
408 #define ESM_APU_VIBRATO_DEPTH_MASK (0xf << 4)
409 #define ESM_APU_VIBRATO_PHASE_SHIFT 8
410 #define ESM_APU_VIBRATO_PHASE_MASK (0xff << 8)
413 #define ESM_APU_RADIUS_SELECT (1 << 6)
416 #define ESM_APU_FILTER_2POLE_LOPASS 0x00
417 #define ESM_APU_FILTER_2POLE_BANDPASS 0x01
418 #define ESM_APU_FILTER_2POLE_HIPASS 0x02
419 #define ESM_APU_FILTER_1POLE_LOPASS 0x03
420 #define ESM_APU_FILTER_1POLE_HIPASS 0x04
421 #define ESM_APU_FILTER_OFF 0x05
424 #define ESM_APU_ATFP_AMPLITUDE 0x00
425 #define ESM_APU_ATFP_TREMELO 0x01
426 #define ESM_APU_ATFP_FILTER 0x02
427 #define ESM_APU_ATFP_PAN 0x03
430 #define ESM_APU_ATFP_FLG_OFF 0x00
431 #define ESM_APU_ATFP_FLG_WAIT 0x01
432 #define ESM_APU_ATFP_FLG_DONE 0x02
433 #define ESM_APU_ATFP_FLG_INPROCESS 0x03
437 #define ESM_MEM_ALIGN 0x1000
438 #define ESM_MIXBUF_SIZE 0x400
440 #define ESM_MODE_PLAY 0
441 #define ESM_MODE_CAPTURE 1
494 #ifdef CONFIG_PM_SLEEP
547 #ifdef CONFIG_PM_SLEEP
551 #ifdef SUPPORT_JOYSTICK
555 #ifdef CONFIG_SND_ES1968_INPUT
556 struct input_dev *input_dev;
564 #ifdef CONFIG_SND_ES1968_RADIO
600 __maestro_write(chip, reg, data);
601 spin_unlock_irqrestore(&chip->
reg_lock, flags);
614 static inline u16 maestro_read(
struct es1968 *chip,
u16 reg)
619 result = __maestro_read(chip, reg);
620 spin_unlock_irqrestore(&chip->
reg_lock, flags);
625 static int snd_es1968_ac97_wait(
struct es1968 *chip)
629 while (timeout-- > 0) {
638 static int snd_es1968_ac97_wait_poll(
struct es1968 *chip)
640 int timeout = 100000;
642 while (timeout-- > 0) {
650 static void snd_es1968_ac97_write(
struct snd_ac97 *ac97,
unsigned short reg,
unsigned short val)
654 snd_es1968_ac97_wait(chip);
663 static unsigned short snd_es1968_ac97_read(
struct snd_ac97 *
ac97,
unsigned short reg)
668 snd_es1968_ac97_wait(chip);
673 if (!snd_es1968_ac97_wait_poll(chip)) {
686 for (i = 0; i < 1000; i++)
689 snd_printd(
"es1968: APU register select failed. (Timeout)\n");
693 static void apu_data_set(
struct es1968 *chip,
u16 data)
696 for (i = 0; i < 1000; i++) {
701 snd_printd(
"es1968: APU register set probably failed (Timeout)!\n");
709 #ifdef CONFIG_PM_SLEEP
712 reg |= (channel << 4);
713 apu_index_set(chip, reg);
714 apu_data_set(chip, data);
717 static void apu_set_register(
struct es1968 *chip,
u16 channel,
u8 reg,
u16 data)
721 __apu_set_register(chip, channel, reg, data);
722 spin_unlock_irqrestore(&chip->
reg_lock, flags);
725 static u16 __apu_get_register(
struct es1968 *chip,
u16 channel,
u8 reg)
729 reg |= (channel << 4);
730 apu_index_set(chip, reg);
734 static u16 apu_get_register(
struct es1968 *chip,
u16 channel,
u8 reg)
739 v = __apu_get_register(chip, channel, reg);
740 spin_unlock_irqrestore(&chip->
reg_lock, flags);
753 spin_unlock_irqrestore(&chip->
reg_lock, flags);
756 static u32 assp_get_register(
struct es1968 *chip,
u32 reg)
764 spin_unlock_irqrestore(&chip->
reg_lock, flags);
778 spin_unlock_irqrestore(&chip->
reg_lock, flags);
781 static u16 wave_get_register(
struct es1968 *chip,
u16 reg)
789 spin_unlock_irqrestore(&chip->
reg_lock, flags);
798 static void snd_es1968_bob_stop(
struct es1968 *chip)
802 reg = __maestro_read(chip, 0x11);
804 __maestro_write(chip, 0x11, reg);
805 reg = __maestro_read(chip, 0x17);
807 __maestro_write(chip, 0x17, reg);
810 static void snd_es1968_bob_start(
struct es1968 *chip)
817 for (prescale = 5; prescale < 12; prescale++)
823 while ((prescale > 5) && (divide < 32)) {
830 for (; divide < 31; divide++)
832 ((
ESS_SYSCLK >> (prescale + 9)) / (divide + 1)))
break;
839 }
else if (divide > 1)
842 __maestro_write(chip, 6, 0x9000 | (prescale << 5) | divide);
845 __maestro_write(chip, 0x11, __maestro_read(chip, 0x11) | 1);
846 __maestro_write(chip, 0x17, __maestro_read(chip, 0x17) | 1);
850 static void snd_es1968_bob_inc(
struct es1968 *chip,
int freq)
855 snd_es1968_bob_start(chip);
857 snd_es1968_bob_stop(chip);
859 snd_es1968_bob_start(chip);
864 static void snd_es1968_bob_dec(
struct es1968 *chip)
868 snd_es1968_bob_stop(chip);
878 snd_es1968_bob_stop(chip);
880 snd_es1968_bob_start(chip);
886 snd_es1968_calc_bob_rate(
struct es1968 *chip,
struct esschan *es,
890 int freq = runtime->
rate * 4;
908 static u32 snd_es1968_compute_rate(
struct es1968 *chip,
u32 freq)
919 static inline unsigned int
920 snd_es1968_get_dma_ptr(
struct es1968 *chip,
struct esschan *es)
924 offset = apu_get_register(chip, es->
apu[0], 5);
926 offset -= es->
base[0];
928 return (offset & 0xFFFE);
931 static void snd_es1968_apu_set_freq(
struct es1968 *chip,
int apu,
int freq)
933 apu_set_register(chip, apu, 2,
934 (apu_get_register(chip, apu, 2) & 0x00FF) |
935 ((freq & 0xff) << 8) | 0x10);
936 apu_set_register(chip, apu, 3, freq >> 8);
940 static inline void snd_es1968_trigger_apu(
struct es1968 *esm,
int apu,
int mode)
943 __apu_set_register(esm, apu, 0,
944 (__apu_get_register(esm, apu, 0) & 0xff0f) |
948 static void snd_es1968_pcm_start(
struct es1968 *chip,
struct esschan *es)
951 __apu_set_register(chip, es->
apu[0], 5, es->
base[0]);
952 snd_es1968_trigger_apu(chip, es->
apu[0], es->
apu_mode[0]);
954 __apu_set_register(chip, es->
apu[2], 5, es->
base[2]);
955 snd_es1968_trigger_apu(chip, es->
apu[2], es->
apu_mode[2]);
958 __apu_set_register(chip, es->
apu[1], 5, es->
base[1]);
959 snd_es1968_trigger_apu(chip, es->
apu[1], es->
apu_mode[1]);
961 __apu_set_register(chip, es->
apu[3], 5, es->
base[3]);
962 snd_es1968_trigger_apu(chip, es->
apu[3], es->
apu_mode[3]);
968 static void snd_es1968_pcm_stop(
struct es1968 *chip,
struct esschan *es)
971 snd_es1968_trigger_apu(chip, es->
apu[0], 0);
972 snd_es1968_trigger_apu(chip, es->
apu[1], 0);
974 snd_es1968_trigger_apu(chip, es->
apu[2], 0);
975 snd_es1968_trigger_apu(chip, es->
apu[3], 0);
981 static void snd_es1968_program_wavecache(
struct es1968 *chip,
struct esschan *es,
982 int channel,
u32 addr,
int capture)
984 u32 tmpval = (addr - 0x10) & 0xFFF8;
994 wave_set_register(chip, es->
apu[channel] << 3, tmpval);
996 #ifdef CONFIG_PM_SLEEP
1002 static void snd_es1968_playback_setup(
struct es1968 *chip,
struct esschan *es,
1009 unsigned long flags;
1017 for (channel = 0; channel <= high_apu; channel++) {
1020 snd_es1968_program_wavecache(chip, es, channel, es->
memory->buf.addr, 0);
1023 pa = es->
memory->buf.addr;
1024 pa -= chip->
dma.addr;
1041 for (i = 0; i < 16; i++)
1042 apu_set_register(chip, apu, i, 0x0000);
1045 apu_set_register(chip, apu, 4, ((pa >> 16) & 0xFF) << 8);
1046 apu_set_register(chip, apu, 5, pa & 0xFFFF);
1047 apu_set_register(chip, apu, 6, (pa + size) & 0xFFFF);
1049 apu_set_register(chip, apu, 7, size);
1052 apu_set_register(chip, apu, 8, 0x0000);
1054 apu_set_register(chip, apu, 9, 0xD000);
1057 apu_set_register(chip, apu, 11, 0x0000);
1059 apu_set_register(chip, apu, 0, 0x400F);
1072 apu_set_register(chip, apu, 10,
1073 0x8F00 | (channel ? 0 : 0x10));
1076 apu_set_register(chip, apu, 10, 0x8F08);
1084 spin_unlock_irqrestore(&chip->
reg_lock, flags);
1086 freq = runtime->
rate;
1097 freq = snd_es1968_compute_rate(chip, freq);
1100 snd_es1968_apu_set_freq(chip, es->
apu[0], freq);
1101 snd_es1968_apu_set_freq(chip, es->
apu[1], freq);
1105 static void init_capture_apu(
struct es1968 *chip,
struct esschan *es,
int channel,
1106 unsigned int pa,
unsigned int bsize,
1107 int mode,
int route)
1114 snd_es1968_program_wavecache(chip, es, channel, pa, 1);
1117 pa -= chip->
dma.addr;
1126 for (i = 0; i < 16; i++)
1127 apu_set_register(chip, apu, i, 0x0000);
1131 apu_set_register(chip, apu, 2, 0x8);
1134 apu_set_register(chip, apu, 4, ((pa >> 16) & 0xFF) << 8);
1135 apu_set_register(chip, apu, 5, pa & 0xFFFF);
1136 apu_set_register(chip, apu, 6, (pa + bsize) & 0xFFFF);
1137 apu_set_register(chip, apu, 7, bsize);
1139 apu_set_register(chip, apu, 8, 0x00F0);
1141 apu_set_register(chip, apu, 9, 0x0000);
1143 apu_set_register(chip, apu, 10, 0x8F08);
1145 apu_set_register(chip, apu, 11, route);
1147 apu_set_register(chip, apu, 0, 0x400F);
1150 static void snd_es1968_capture_setup(
struct es1968 *chip,
struct esschan *es,
1155 unsigned long flags;
1171 init_capture_apu(chip, es, 2,
1175 init_capture_apu(chip, es, 0, es->
memory->buf.addr, size,
1179 init_capture_apu(chip, es, 3,
1184 init_capture_apu(chip, es, 1,
1185 es->
memory->buf.addr + size*2, size,
1189 freq = runtime->
rate;
1196 freq = snd_es1968_compute_rate(chip, freq);
1199 snd_es1968_apu_set_freq(chip, es->
apu[0], freq);
1200 snd_es1968_apu_set_freq(chip, es->
apu[1], freq);
1204 snd_es1968_apu_set_freq(chip, es->
apu[2], freq);
1205 snd_es1968_apu_set_freq(chip, es->
apu[3], freq);
1212 spin_unlock_irqrestore(&chip->
reg_lock, flags);
1225 es->
dma_size = snd_pcm_lib_buffer_bytes(substream);
1226 es->
frag_size = snd_pcm_lib_period_bytes(substream);
1237 es->
bob_freq = snd_es1968_calc_bob_rate(chip, es, runtime);
1241 snd_es1968_playback_setup(chip, es, runtime);
1244 snd_es1968_capture_setup(chip, es, runtime);
1262 snd_es1968_bob_inc(chip, es->
bob_freq);
1265 snd_es1968_pcm_start(chip, es);
1272 snd_es1968_pcm_stop(chip, es);
1274 snd_es1968_bob_dec(chip);
1287 ptr = snd_es1968_get_dma_ptr(chip, es) << es->
wav_shift;
1341 static int calc_available_memory_size(
struct es1968 *chip)
1348 if (buf->
empty && buf->
buf.bytes > max_size)
1349 max_size = buf->
buf.bytes;
1352 if (max_size >= 128*1024)
1353 max_size = 127*1024;
1358 static struct esm_memory *snd_es1968_new_memory(
struct es1968 *chip,
int size)
1365 if (buf->
empty && buf->
buf.bytes >= size)
1372 if (buf->
buf.bytes > size) {
1374 if (chunk ==
NULL) {
1384 list_add(&chunk->
list, &buf->
list);
1392 static void snd_es1968_free_memory(
struct es1968 *chip,
struct esm_memory *buf)
1401 chunk->
buf.bytes += buf->
buf.bytes;
1410 buf->
buf.bytes += chunk->
buf.bytes;
1418 static void snd_es1968_free_dmabuf(
struct es1968 *chip)
1422 if (! chip->
dma.area)
1433 snd_es1968_init_dmabuf(
struct es1968 *chip)
1444 if (err < 0 || ! chip->
dma.area) {
1449 if ((chip->
dma.addr + chip->
dma.bytes - 1) & ~((1 << 28) - 1)) {
1459 if (chunk ==
NULL) {
1460 snd_es1968_free_dmabuf(chip);
1485 if (chan->
memory->buf.bytes >= size) {
1489 snd_es1968_free_memory(chip, chan->
memory);
1491 chan->
memory = snd_es1968_new_memory(chip, size);
1496 snd_pcm_set_runtime_buffer(substream, &chan->
memory->buf);
1511 snd_es1968_free_memory(chip, chan->
memory);
1521 static int snd_es1968_alloc_apu_pair(
struct es1968 *chip,
int type)
1525 for (apu = 0; apu <
NR_APUS; apu += 2) {
1538 static void snd_es1968_free_apu_pair(
struct es1968 *chip,
int apu)
1562 snd_es1968_free_apu_pair(chip, apu1);
1567 es->
apu[1] = apu1 + 1;
1575 runtime->
hw = snd_es1968_playback;
1576 runtime->
hw.buffer_bytes_max = runtime->
hw.period_bytes_max =
1577 calc_available_memory_size(chip);
1598 snd_es1968_free_apu_pair(chip, apu1);
1604 snd_es1968_free_apu_pair(chip, apu1);
1605 snd_es1968_free_apu_pair(chip, apu2);
1610 es->
apu[1] = apu1 + 1;
1612 es->
apu[3] = apu2 + 1;
1623 snd_es1968_free_apu_pair(chip, apu1);
1624 snd_es1968_free_apu_pair(chip, apu2);
1631 runtime->
hw = snd_es1968_capture;
1632 runtime->
hw.buffer_bytes_max = runtime->
hw.period_bytes_max =
1633 calc_available_memory_size(chip) - 1024;
1650 es = substream->
runtime->private_data;
1654 snd_es1968_free_apu_pair(chip, es->
apu[0]);
1667 es = substream->
runtime->private_data;
1671 snd_es1968_free_memory(chip, es->
mixbuf);
1672 snd_es1968_free_apu_pair(chip, es->
apu[0]);
1673 snd_es1968_free_apu_pair(chip, es->
apu[2]);
1679 static struct snd_pcm_ops snd_es1968_playback_ops = {
1680 .open = snd_es1968_playback_open,
1681 .close = snd_es1968_playback_close,
1683 .hw_params = snd_es1968_hw_params,
1684 .hw_free = snd_es1968_hw_free,
1685 .prepare = snd_es1968_pcm_prepare,
1686 .trigger = snd_es1968_pcm_trigger,
1687 .pointer = snd_es1968_pcm_pointer,
1690 static struct snd_pcm_ops snd_es1968_capture_ops = {
1691 .open = snd_es1968_capture_open,
1692 .close = snd_es1968_capture_close,
1694 .hw_params = snd_es1968_hw_params,
1695 .hw_free = snd_es1968_hw_free,
1696 .prepare = snd_es1968_pcm_prepare,
1697 .trigger = snd_es1968_pcm_trigger,
1698 .pointer = snd_es1968_pcm_pointer,
1705 #define CLOCK_MEASURE_BUFSIZE 16768
1712 struct timeval start_time, stop_time;
1714 if (chip->
clock == 0)
1715 chip->
clock = 48000;
1724 snd_es1968_free_apu_pair(chip, apu);
1730 wave_set_register(chip, apu << 3, (memory->
buf.addr - 0x10) & 0xfff8);
1732 pa = (
unsigned int)((memory->
buf.addr - chip->
dma.addr) >> 1);
1736 for (i = 0; i < 16; i++)
1737 apu_set_register(chip, apu, i, 0x0000);
1739 apu_set_register(chip, apu, 0, 0x400f);
1740 apu_set_register(chip, apu, 4, ((pa >> 16) & 0xff) << 8);
1741 apu_set_register(chip, apu, 5, pa & 0xffff);
1744 apu_set_register(chip, apu, 8, 0x0000);
1745 apu_set_register(chip, apu, 9, 0xD000);
1746 apu_set_register(chip, apu, 10, 0x8F08);
1747 apu_set_register(chip, apu, 11, 0x0000);
1753 snd_es1968_apu_set_freq(chip, apu, ((
unsigned int)48000 << 16) / chip->
clock);
1759 __apu_set_register(chip, apu, 5, pa & 0xffff);
1765 offset = __apu_get_register(chip, apu, 5);
1767 snd_es1968_trigger_apu(chip, apu, 0);
1768 snd_es1968_bob_dec(chip);
1773 offset -= (pa & 0xffff);
1787 offset = (offset /
t) * 1000 + ((offset % t) * 1000) / t;
1789 if (offset >= 40000 && offset <= 50000)
1794 snd_es1968_free_memory(chip, memory);
1795 snd_es1968_free_apu_pair(chip, apu);
1802 static void snd_es1968_pcm_free(
struct snd_pcm *pcm)
1805 snd_es1968_free_dmabuf(esm);
1816 if ((err = snd_es1968_init_dmabuf(chip)) < 0)
1820 wave_set_register(chip, 0x01FC, chip->
dma.addr >> 12);
1821 wave_set_register(chip, 0x01FD, chip->
dma.addr >> 12);
1822 wave_set_register(chip, 0x01FE, chip->
dma.addr >> 12);
1823 wave_set_register(chip, 0x01FF, chip->
dma.addr >> 12);
1847 static void snd_es1968_suppress_jitter(
struct es1968 *chip,
struct esschan *es)
1853 cp1 = __apu_get_register(chip, 0, 5);
1854 cp2 = __apu_get_register(chip, 1, 5);
1855 diff = (cp1 > cp2 ? cp1 - cp2 : cp2 - cp1);
1864 static void snd_es1968_update_pcm(
struct es1968 *chip,
struct esschan *es)
1873 hwptr = snd_es1968_get_dma_ptr(chip, es) << es->
wav_shift;
1911 #ifndef CONFIG_SND_ES1968_INPUT
1923 if ((val & 0x7f) > 0)
1925 if ((val & 0x7f00) > 0)
1930 if ((val & 0x7f) < 0x1f)
1932 if ((val & 0x7f00) < 0x1f00)
1940 if (!chip->input_dev)
1962 input_report_key(chip->input_dev, val, 1);
1963 input_sync(chip->input_dev);
1964 input_report_key(chip->input_dev, val, 0);
1965 input_sync(chip->input_dev);
1998 snd_es1968_update_pcm(chip, es);
2000 snd_es1968_suppress_jitter(chip, es);
2005 unsigned int curp = __apu_get_register(chip, chip->
measure_apu, 5);
2006 if (curp < chip->measure_lastpos)
2020 snd_es1968_mixer(
struct es1968 *chip)
2024 #ifndef CONFIG_SND_ES1968_INPUT
2029 .write = snd_es1968_ac97_write,
2030 .read = snd_es1968_ac97_read,
2037 memset(&ac97, 0,
sizeof(ac97));
2042 #ifndef CONFIG_SND_ES1968_INPUT
2044 memset(&elem_id, 0,
sizeof(elem_id));
2046 strcpy(elem_id.name,
"Master Playback Switch");
2048 memset(&elem_id, 0,
sizeof(elem_id));
2050 strcpy(elem_id.name,
"Master Playback Volume");
2061 static void snd_es1968_ac97_reset(
struct es1968 *chip)
2063 unsigned long ioaddr = chip->
io_port;
2065 unsigned short save_ringbus_a;
2066 unsigned short save_68;
2071 save_ringbus_a =
inw(ioaddr + 0x36);
2075 outw(
inw(ioaddr + 0x3a) & 0xfffc, ioaddr + 0x3a);
2076 outw(
inw(ioaddr + 0x3c) & 0xfffc, ioaddr + 0x3c);
2079 outw(0x0000, ioaddr + 0x36);
2080 save_68 =
inw(ioaddr + 0x68);
2081 pci_read_config_word(chip->
pci, 0x58, &w);
2085 outw(0xfffe, ioaddr + 0x64);
2086 outw(0x0001, ioaddr + 0x68);
2087 outw(0x0000, ioaddr + 0x60);
2089 outw(0x0001, ioaddr + 0x60);
2092 outw(save_68 | 0x1, ioaddr + 0x68);
2093 outw((
inw(ioaddr + 0x38) & 0xfffc) | 0x1, ioaddr + 0x38);
2094 outw((
inw(ioaddr + 0x3a) & 0xfffc) | 0x1, ioaddr + 0x3a);
2095 outw((
inw(ioaddr + 0x3c) & 0xfffc) | 0x1, ioaddr + 0x3c);
2099 outw(0x0000, ioaddr + 0x36);
2100 outw(0xfff7, ioaddr + 0x64);
2101 save_68 =
inw(ioaddr + 0x68);
2102 outw(0x0009, ioaddr + 0x68);
2103 outw(0x0001, ioaddr + 0x60);
2105 outw(0x0009, ioaddr + 0x60);
2108 outw(
inw(ioaddr + 0x3a) & 0xfffc, ioaddr + 0x3a);
2109 outw(
inw(ioaddr + 0x3c) & 0xfffc, ioaddr + 0x3c);
2114 outb(0x80 | 0x7c, ioaddr + 0x30);
2116 if ((
inw(ioaddr + 0x30) & 1) == 0) {
2117 if (
inb(ioaddr + 0x32) != 0)
2120 outb(0x80 | 0x7d, ioaddr + 0x30);
2121 if (((
inw(ioaddr + 0x30) & 1) == 0)
2122 && (
inb(ioaddr + 0x32) != 0))
2124 outb(0x80 | 0x7f, ioaddr + 0x30);
2125 if (((
inw(ioaddr + 0x30) & 1) == 0)
2126 && (
inb(ioaddr + 0x32) != 0))
2131 outb(
inb(ioaddr + 0x37) | 0x08, ioaddr + 0x37);
2133 outb(
inb(ioaddr + 0x37) & ~0x08,
2136 outw(0x80, ioaddr + 0x30);
2137 for (w = 0; w < 10000; w++) {
2138 if ((
inw(ioaddr + 0x30) & 1) == 0)
2146 outw(0xf9ff, ioaddr + 0x64);
2147 outw(
inw(ioaddr + 0x68) | 0x600, ioaddr + 0x68);
2148 outw(0x0209, ioaddr + 0x60);
2152 outw(save_ringbus_a, ioaddr + 0x36);
2157 outb(
inb(ioaddr+0xc0)|(1<<5), ioaddr+0xc0);
2158 outb(0xff, ioaddr+0xc3);
2159 outb(0xff, ioaddr+0xc4);
2160 outb(0xff, ioaddr+0xc6);
2161 outb(0xff, ioaddr+0xc8);
2162 outb(0x3f, ioaddr+0xcf);
2163 outb(0x3f, ioaddr+0xd0);
2166 static void snd_es1968_reset(
struct es1968 *chip)
2179 static void snd_es1968_chip_init(
struct es1968 *chip)
2236 pci_read_config_word(pci,
ESM_DDMA, &w);
2238 pci_write_config_word(pci,
ESM_DDMA, w);
2253 pci_read_config_word(pci, 0x58, &w);
2257 pci_write_config_word(pci, 0x58, w);
2261 snd_es1968_reset(chip);
2277 snd_es1968_ac97_reset(chip);
2288 outb(0x88, iobase+0x1c);
2289 outb(0x88, iobase+0x1d);
2290 outb(0x88, iobase+0x1e);
2291 outb(0x88, iobase+0x1f);
2303 for (i = 0; i < 16; i++) {
2326 maestro_write(chip, 0x08, 0xB004);
2327 maestro_write(chip, 0x09, 0x001B);
2328 maestro_write(chip, 0x0A, 0x8000);
2329 maestro_write(chip, 0x0B, 0x3F37);
2330 maestro_write(chip, 0x0C, 0x0098);
2333 maestro_write(chip, 0x0C,
2334 (maestro_read(chip, 0x0C) & ~0xF000) | 0x8000);
2336 maestro_write(chip, 0x0C,
2337 (maestro_read(chip, 0x0C) & ~0x0F00) | 0x0500);
2339 maestro_write(chip, 0x0D, 0x7632);
2362 for (i = 0; i <
NR_APUS; i++) {
2364 apu_set_register(chip, i, w, 0);
2370 static void snd_es1968_start_irq(
struct es1968 *chip)
2380 #ifdef CONFIG_PM_SLEEP
2384 static int es1968_suspend(
struct device *
dev)
2396 snd_pcm_suspend_all(chip->
pcm);
2397 snd_ac97_suspend(chip->
ac97);
2398 snd_es1968_bob_stop(chip);
2406 static int es1968_resume(
struct device *dev)
2421 "disabling device\n");
2427 snd_es1968_chip_init(chip);
2430 if (chip->
dma.addr) {
2432 wave_set_register(chip, 0x01FC, chip->
dma.addr >> 12);
2435 snd_es1968_start_irq(chip);
2438 snd_ac97_resume(chip->
ac97);
2443 snd_es1968_playback_setup(chip, es, es->
substream->runtime);
2446 snd_es1968_capture_setup(chip, es, es->
substream->runtime);
2453 snd_es1968_bob_start(chip);
2461 #define ES1968_PM_OPS &es1968_pm
2463 #define ES1968_PM_OPS NULL
2466 #ifdef SUPPORT_JOYSTICK
2467 #define JOYSTICK_ADDR 0x200
2468 static int __devinit snd_es1968_create_gameport(
struct es1968 *chip,
int dev)
2481 chip->gameport = gp = gameport_allocate_port();
2483 printk(
KERN_ERR "es1968: cannot allocate memory for gameport\n");
2491 gameport_set_name(gp,
"ES1968 Gameport");
2493 gameport_set_dev_parent(gp, &chip->
pci->dev);
2494 gp->
io = JOYSTICK_ADDR;
2495 gameport_set_port_data(gp, r);
2497 gameport_register_port(gp);
2502 static void snd_es1968_free_gameport(
struct es1968 *chip)
2504 if (chip->gameport) {
2505 struct resource *
r = gameport_get_port_data(chip->gameport);
2508 chip->gameport =
NULL;
2514 static inline int snd_es1968_create_gameport(
struct es1968 *chip,
int dev) {
return -
ENOSYS; }
2515 static inline void snd_es1968_free_gameport(
struct es1968 *chip) { }
2518 #ifdef CONFIG_SND_ES1968_INPUT
2521 struct input_dev *input_dev;
2524 input_dev = input_allocate_device();
2528 snprintf(chip->phys,
sizeof(chip->phys),
"pci-%s/input0",
2529 pci_name(chip->
pci));
2531 input_dev->name = chip->
card->driver;
2532 input_dev->phys = chip->phys;
2533 input_dev->id.bustype =
BUS_PCI;
2534 input_dev->id.vendor = chip->
pci->vendor;
2535 input_dev->id.product = chip->
pci->device;
2536 input_dev->dev.parent = &chip->
pci->dev;
2543 err = input_register_device(input_dev);
2545 input_free_device(input_dev);
2549 chip->input_dev = input_dev;
2554 #ifdef CONFIG_SND_ES1968_RADIO
2555 #define GPIO_DATA 0x60
2561 #define STR_DATA 0x0040
2562 #define STR_CLK 0x0080
2563 #define STR_WREN 0x0100
2564 #define STR_MOST 0x0200
2579 static u8 snd_es1968_tea575x_get_pins(
struct snd_tea575x *tea)
2594 static void snd_es1968_tea575x_set_direction(
struct snd_tea575x *tea,
bool output)
2598 u16 odir =
inw(io + IO_DIR);
2610 .
set_pins = snd_es1968_tea575x_set_pins,
2611 .get_pins = snd_es1968_tea575x_get_pins,
2612 .set_direction = snd_es1968_tea575x_set_direction,
2616 static int snd_es1968_free(
struct es1968 *chip)
2619 #ifdef CONFIG_SND_ES1968_INPUT
2620 if (chip->input_dev)
2621 input_unregister_device(chip->input_dev);
2631 #ifdef CONFIG_SND_ES1968_RADIO
2638 snd_es1968_free_gameport(chip);
2645 static int snd_es1968_dev_free(
struct snd_device *device)
2648 return snd_es1968_free(chip);
2679 struct es1968 **chip_ret)
2682 .dev_free = snd_es1968_dev_free,
2694 pci_set_consistent_dma_mask(pci,
DMA_BIT_MASK(28)) < 0) {
2728 KBUILD_MODNAME, chip)) {
2730 snd_es1968_free(chip);
2736 for (i = 0; i < 32; i++)
2748 unsigned short vend;
2751 if (chip->
type == pm_whitelist[i].type &&
2752 vend == pm_whitelist[i].vendor) {
2765 snd_es1968_chip_init(chip);
2768 snd_es1968_free(chip);
2774 #ifdef CONFIG_SND_ES1968_RADIO
2777 snd_es1968_free(chip);
2780 chip->tea.v4l2_dev = &chip->v4l2_dev;
2781 chip->tea.private_data =
chip;
2782 chip->tea.radio_nr = radio_nr;
2783 chip->tea.ops = &snd_es1968_tea_ops;
2785 sprintf(chip->tea.bus_info,
"PCI:%s", pci_name(pci));
2818 if (total_bufsize[dev] < 128)
2819 total_bufsize[
dev] = 128;
2820 if (total_bufsize[dev] > 4096)
2821 total_bufsize[
dev] = 4096;
2822 if ((err = snd_es1968_create(card, pci,
2823 total_bufsize[dev] * 1024,
2824 pcm_substreams_p[dev],
2825 pcm_substreams_c[dev],
2835 switch (chip->
type) {
2850 if ((err = snd_es1968_pcm(chip, 0)) < 0) {
2855 if ((err = snd_es1968_mixer(chip)) < 0) {
2860 if (enable_mpu[dev] == 2) {
2862 unsigned short vend;
2864 for (i = 0; i <
ARRAY_SIZE(mpu_blacklist); i++) {
2865 if (chip->
type == mpu_blacklist[i].type &&
2866 vend == mpu_blacklist[i].vendor) {
2867 enable_mpu[
dev] = 0;
2872 if (enable_mpu[dev]) {
2877 -1, &chip->
rmidi)) < 0) {
2882 snd_es1968_create_gameport(chip, dev);
2884 #ifdef CONFIG_SND_ES1968_INPUT
2885 err = snd_es1968_input_register(chip);
2888 "failed with error %i", err);
2891 snd_es1968_start_irq(chip);
2895 es1968_measure_clock(chip);
2904 pci_set_drvdata(pci, card);
2912 pci_set_drvdata(pci,
NULL);
2916 .name = KBUILD_MODNAME,
2917 .id_table = snd_es1968_ids,
2918 .probe = snd_es1968_probe,