1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Matt Wu <Matt_Wu@acersoftech.com.cn>
5 * Routines for control of ALi pci audio M5451
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/init.h>
18 #include <linux/pci.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/dma-mapping.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/info.h>
25 #include <sound/ac97_codec.h>
26 #include <sound/mpu401.h>
27 #include <sound/initval.h>
29 MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
30 MODULE_DESCRIPTION("ALI M5451");
31 MODULE_LICENSE("GPL");
32 MODULE_SUPPORTED_DEVICE("{{ALI,M5451,pci},{ALI,M5451}}");
34 static int index
= SNDRV_DEFAULT_IDX1
; /* Index */
35 static char *id
= SNDRV_DEFAULT_STR1
; /* ID for this card */
36 static int pcm_channels
= 32;
39 module_param(index
, int, 0444);
40 MODULE_PARM_DESC(index
, "Index value for ALI M5451 PCI Audio.");
41 module_param(id
, charp
, 0444);
42 MODULE_PARM_DESC(id
, "ID string for ALI M5451 PCI Audio.");
43 module_param(pcm_channels
, int, 0444);
44 MODULE_PARM_DESC(pcm_channels
, "PCM Channels");
45 module_param(spdif
, bool, 0444);
46 MODULE_PARM_DESC(spdif
, "Support SPDIF I/O");
48 /* just for backward compatibility */
50 module_param(enable
, bool, 0444);
54 * Constants definition
57 #define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
60 #define ALI_CHANNELS 32
62 #define ALI_PCM_IN_CHANNEL 31
63 #define ALI_SPDIF_IN_CHANNEL 19
64 #define ALI_SPDIF_OUT_CHANNEL 15
65 #define ALI_CENTER_CHANNEL 24
66 #define ALI_LEF_CHANNEL 23
67 #define ALI_SURR_LEFT_CHANNEL 26
68 #define ALI_SURR_RIGHT_CHANNEL 25
69 #define ALI_MODEM_IN_CHANNEL 21
70 #define ALI_MODEM_OUT_CHANNEL 20
72 #define SNDRV_ALI_VOICE_TYPE_PCM 01
73 #define SNDRV_ALI_VOICE_TYPE_OTH 02
75 #define ALI_5451_V02 0x02
81 #define ALI_LEGACY_DMAR0 0x00 /* ADR0 */
82 #define ALI_LEGACY_DMAR4 0x04 /* CNT0 */
83 #define ALI_LEGACY_DMAR11 0x0b /* MOD */
84 #define ALI_LEGACY_DMAR15 0x0f /* MMR */
85 #define ALI_MPUR0 0x20
86 #define ALI_MPUR1 0x21
87 #define ALI_MPUR2 0x22
88 #define ALI_MPUR3 0x23
90 #define ALI_AC97_WRITE 0x40
91 #define ALI_AC97_READ 0x44
93 #define ALI_SCTRL 0x48
94 #define ALI_SPDIF_OUT_ENABLE 0x20
95 #define ALI_SCTRL_LINE_IN2 (1 << 9)
96 #define ALI_SCTRL_GPIO_IN2 (1 << 13)
97 #define ALI_SCTRL_LINE_OUT_EN (1 << 20)
98 #define ALI_SCTRL_GPIO_OUT_EN (1 << 23)
99 #define ALI_SCTRL_CODEC1_READY (1 << 24)
100 #define ALI_SCTRL_CODEC2_READY (1 << 25)
101 #define ALI_AC97_GPIO 0x4c
102 #define ALI_AC97_GPIO_ENABLE 0x8000
103 #define ALI_AC97_GPIO_DATA_SHIFT 16
104 #define ALI_SPDIF_CS 0x70
105 #define ALI_SPDIF_CTRL 0x74
106 #define ALI_SPDIF_IN_FUNC_ENABLE 0x02
107 #define ALI_SPDIF_IN_CH_STATUS 0x40
108 #define ALI_SPDIF_OUT_CH_STATUS 0xbf
109 #define ALI_START 0x80
110 #define ALI_STOP 0x84
111 #define ALI_CSPF 0x90
112 #define ALI_AINT 0x98
113 #define ALI_GC_CIR 0xa0
114 #define ENDLP_IE 0x00001000
115 #define MIDLP_IE 0x00002000
116 #define ALI_AINTEN 0xa4
117 #define ALI_VOLUME 0xa8
118 #define ALI_SBDELTA_DELTA_R 0xac
119 #define ALI_MISCINT 0xb0
120 #define ADDRESS_IRQ 0x00000020
121 #define TARGET_REACHED 0x00008000
122 #define MIXER_OVERFLOW 0x00000800
123 #define MIXER_UNDERFLOW 0x00000400
124 #define GPIO_IRQ 0x01000000
125 #define ALI_SBBL_SBCL 0xc0
126 #define ALI_SBCTRL_SBE2R_SBDD 0xc4
127 #define ALI_STIMER 0xc8
128 #define ALI_GLOBAL_CONTROL 0xd4
129 #define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
130 #define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
131 #define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
132 #define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
133 #define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
135 #define ALI_CSO_ALPHA_FMS 0xe0
137 #define ALI_ESO_DELTA 0xe8
138 #define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
139 #define ALI_EBUF1 0xf4
140 #define ALI_EBUF2 0xf8
142 #define ALI_REG(codec, x) ((codec)->port + x)
148 struct snd_ali_voice
;
150 struct snd_ali_channel_control
{
159 /* register addresses */
165 unsigned int ac97read
;
166 unsigned int ac97write
;
171 struct snd_ali_voice
{
181 struct snd_ali
*codec
;
182 struct snd_pcm_substream
*substream
;
183 struct snd_ali_voice
*extra
;
185 int eso
; /* final ESO value for channel */
186 int count
; /* runtime->period_size */
191 void (*private_free
)(void *private_data
);
197 struct snd_ali_voice voices
[ALI_CHANNELS
];
199 unsigned int chcnt
; /* num of opened channels */
200 unsigned int chmap
; /* bitmap for opened channels */
201 unsigned int synthcount
;
206 #define ALI_GLOBAL_REGS 56
207 #define ALI_CHANNEL_REGS 8
208 struct snd_ali_image
{
209 u32 regs
[ALI_GLOBAL_REGS
];
210 u32 channel_regs
[ALI_CHANNELS
][ALI_CHANNEL_REGS
];
217 unsigned char revision
;
219 unsigned int hw_initialized
:1;
220 unsigned int spdif_support
:1;
223 struct pci_dev
*pci_m1533
;
224 struct pci_dev
*pci_m7101
;
226 struct snd_card
*card
;
227 struct snd_pcm
*pcm
[MAX_CODECS
];
228 struct snd_alidev synth
;
229 struct snd_ali_channel_control chregs
;
232 unsigned int spdif_mask
;
234 unsigned int spurious_irq_count
;
235 unsigned int spurious_irq_max_delta
;
237 unsigned int num_of_codecs
;
239 struct snd_ac97_bus
*ac97_bus
;
240 struct snd_ac97
*ac97
[MAX_CODECS
];
241 unsigned short ac97_ext_id
;
242 unsigned short ac97_ext_status
;
245 spinlock_t voice_alloc
;
247 #ifdef CONFIG_PM_SLEEP
248 struct snd_ali_image
*image
;
252 static const struct pci_device_id snd_ali_ids
[] = {
253 {PCI_DEVICE(PCI_VENDOR_ID_AL
, PCI_DEVICE_ID_AL_M5451
), 0, 0, 0},
256 MODULE_DEVICE_TABLE(pci
, snd_ali_ids
);
258 static void snd_ali_clear_voices(struct snd_ali
*, unsigned int, unsigned int);
259 static unsigned short snd_ali_codec_peek(struct snd_ali
*, int, unsigned short);
260 static void snd_ali_codec_poke(struct snd_ali
*, int, unsigned short,
267 static inline unsigned int snd_ali_5451_peek(struct snd_ali
*codec
,
270 return (unsigned int)inl(ALI_REG(codec
, port
));
273 static inline void snd_ali_5451_poke(struct snd_ali
*codec
,
277 outl((unsigned int)val
, ALI_REG(codec
, port
));
280 static int snd_ali_codec_ready(struct snd_ali
*codec
,
283 unsigned long end_time
;
286 end_time
= jiffies
+ msecs_to_jiffies(250);
289 res
= snd_ali_5451_peek(codec
,port
);
292 if (!time_after_eq(end_time
, jiffies
))
294 schedule_timeout_uninterruptible(1);
297 snd_ali_5451_poke(codec
, port
, res
& ~0x8000);
298 dev_dbg(codec
->card
->dev
, "ali_codec_ready: codec is not ready.\n ");
302 static int snd_ali_stimer_ready(struct snd_ali
*codec
)
304 unsigned long end_time
;
305 unsigned long dwChk1
,dwChk2
;
307 dwChk1
= snd_ali_5451_peek(codec
, ALI_STIMER
);
308 end_time
= jiffies
+ msecs_to_jiffies(250);
311 dwChk2
= snd_ali_5451_peek(codec
, ALI_STIMER
);
312 if (dwChk2
!= dwChk1
)
314 if (!time_after_eq(end_time
, jiffies
))
316 schedule_timeout_uninterruptible(1);
319 dev_err(codec
->card
->dev
, "ali_stimer_read: stimer is not ready.\n");
323 static void snd_ali_codec_poke(struct snd_ali
*codec
,int secondary
,
331 dev_err(codec
->card
->dev
,
332 "ali_codec_poke: reg(%xh) invalid.\n", reg
);
336 port
= codec
->chregs
.regs
.ac97write
;
338 if (snd_ali_codec_ready(codec
, port
) < 0)
340 if (snd_ali_stimer_ready(codec
) < 0)
343 dwVal
= (unsigned int) (reg
& 0xff);
344 dwVal
|= 0x8000 | (val
<< 16);
347 if (codec
->revision
== ALI_5451_V02
)
350 snd_ali_5451_poke(codec
, port
, dwVal
);
355 static unsigned short snd_ali_codec_peek(struct snd_ali
*codec
,
363 dev_err(codec
->card
->dev
,
364 "ali_codec_peek: reg(%xh) invalid.\n", reg
);
368 port
= codec
->chregs
.regs
.ac97read
;
370 if (snd_ali_codec_ready(codec
, port
) < 0)
372 if (snd_ali_stimer_ready(codec
) < 0)
375 dwVal
= (unsigned int) (reg
& 0xff);
376 dwVal
|= 0x8000; /* bit 15*/
380 snd_ali_5451_poke(codec
, port
, dwVal
);
382 if (snd_ali_stimer_ready(codec
) < 0)
384 if (snd_ali_codec_ready(codec
, port
) < 0)
387 return (snd_ali_5451_peek(codec
, port
) & 0xffff0000) >> 16;
390 static void snd_ali_codec_write(struct snd_ac97
*ac97
,
394 struct snd_ali
*codec
= ac97
->private_data
;
396 dev_dbg(codec
->card
->dev
, "codec_write: reg=%xh data=%xh.\n", reg
, val
);
397 if (reg
== AC97_GPIO_STATUS
) {
398 outl((val
<< ALI_AC97_GPIO_DATA_SHIFT
) | ALI_AC97_GPIO_ENABLE
,
399 ALI_REG(codec
, ALI_AC97_GPIO
));
402 snd_ali_codec_poke(codec
, ac97
->num
, reg
, val
);
407 static unsigned short snd_ali_codec_read(struct snd_ac97
*ac97
,
410 struct snd_ali
*codec
= ac97
->private_data
;
412 dev_dbg(codec
->card
->dev
, "codec_read reg=%xh.\n", reg
);
413 return snd_ali_codec_peek(codec
, ac97
->num
, reg
);
420 static int snd_ali_reset_5451(struct snd_ali
*codec
)
422 struct pci_dev
*pci_dev
;
423 unsigned short wCount
, wReg
;
426 pci_dev
= codec
->pci_m1533
;
428 pci_read_config_dword(pci_dev
, 0x7c, &dwVal
);
429 pci_write_config_dword(pci_dev
, 0x7c, dwVal
| 0x08000000);
431 pci_read_config_dword(pci_dev
, 0x7c, &dwVal
);
432 pci_write_config_dword(pci_dev
, 0x7c, dwVal
& 0xf7ffffff);
436 pci_dev
= codec
->pci
;
437 pci_read_config_dword(pci_dev
, 0x44, &dwVal
);
438 pci_write_config_dword(pci_dev
, 0x44, dwVal
| 0x000c0000);
440 pci_read_config_dword(pci_dev
, 0x44, &dwVal
);
441 pci_write_config_dword(pci_dev
, 0x44, dwVal
& 0xfffbffff);
446 wReg
= snd_ali_codec_peek(codec
, 0, AC97_POWERDOWN
);
447 if ((wReg
& 0x000f) == 0x000f)
452 /* non-fatal if you have a non PM capable codec */
453 /* dev_warn(codec->card->dev, "ali5451: reset time out\n"); */
458 * ALI 5451 Controller
461 static void snd_ali_enable_special_channel(struct snd_ali
*codec
,
462 unsigned int channel
)
466 dwVal
= inl(ALI_REG(codec
, ALI_GLOBAL_CONTROL
));
467 dwVal
|= 1 << (channel
& 0x0000001f);
468 outl(dwVal
, ALI_REG(codec
, ALI_GLOBAL_CONTROL
));
471 static void snd_ali_disable_special_channel(struct snd_ali
*codec
,
472 unsigned int channel
)
476 dwVal
= inl(ALI_REG(codec
, ALI_GLOBAL_CONTROL
));
477 dwVal
&= ~(1 << (channel
& 0x0000001f));
478 outl(dwVal
, ALI_REG(codec
, ALI_GLOBAL_CONTROL
));
481 static void snd_ali_enable_address_interrupt(struct snd_ali
*codec
)
485 gc
= inl(ALI_REG(codec
, ALI_GC_CIR
));
488 outl( gc
, ALI_REG(codec
, ALI_GC_CIR
));
491 static void snd_ali_disable_address_interrupt(struct snd_ali
*codec
)
495 gc
= inl(ALI_REG(codec
, ALI_GC_CIR
));
498 outl(gc
, ALI_REG(codec
, ALI_GC_CIR
));
501 static void snd_ali_disable_voice_irq(struct snd_ali
*codec
,
502 unsigned int channel
)
505 struct snd_ali_channel_control
*pchregs
= &(codec
->chregs
);
507 dev_dbg(codec
->card
->dev
, "disable_voice_irq channel=%d\n", channel
);
509 mask
= 1 << (channel
& 0x1f);
510 pchregs
->data
.ainten
= inl(ALI_REG(codec
, pchregs
->regs
.ainten
));
511 pchregs
->data
.ainten
&= ~mask
;
512 outl(pchregs
->data
.ainten
, ALI_REG(codec
, pchregs
->regs
.ainten
));
515 static int snd_ali_alloc_pcm_channel(struct snd_ali
*codec
, int channel
)
517 unsigned int idx
= channel
& 0x1f;
519 if (codec
->synth
.chcnt
>= ALI_CHANNELS
){
520 dev_err(codec
->card
->dev
,
521 "ali_alloc_pcm_channel: no free channels.\n");
525 if (!(codec
->synth
.chmap
& (1 << idx
))) {
526 codec
->synth
.chmap
|= 1 << idx
;
527 codec
->synth
.chcnt
++;
528 dev_dbg(codec
->card
->dev
, "alloc_pcm_channel no. %d.\n", idx
);
534 static int snd_ali_find_free_channel(struct snd_ali
* codec
, int rec
)
539 dev_dbg(codec
->card
->dev
,
540 "find_free_channel: for %s\n", rec
? "rec" : "pcm");
544 if (codec
->spdif_support
&&
545 (inl(ALI_REG(codec
, ALI_GLOBAL_CONTROL
)) &
546 ALI_SPDIF_IN_SUPPORT
))
547 idx
= ALI_SPDIF_IN_CHANNEL
;
549 idx
= ALI_PCM_IN_CHANNEL
;
551 result
= snd_ali_alloc_pcm_channel(codec
, idx
);
555 dev_err(codec
->card
->dev
,
556 "ali_find_free_channel: record channel is busy now.\n");
562 if (codec
->spdif_support
&&
563 (inl(ALI_REG(codec
, ALI_GLOBAL_CONTROL
)) &
564 ALI_SPDIF_OUT_CH_ENABLE
)) {
565 idx
= ALI_SPDIF_OUT_CHANNEL
;
566 result
= snd_ali_alloc_pcm_channel(codec
, idx
);
570 dev_err(codec
->card
->dev
,
571 "ali_find_free_channel: S/PDIF out channel is in busy now.\n");
574 for (idx
= 0; idx
< ALI_CHANNELS
; idx
++) {
575 result
= snd_ali_alloc_pcm_channel(codec
, idx
);
579 dev_err(codec
->card
->dev
, "ali_find_free_channel: no free channels.\n");
583 static void snd_ali_free_channel_pcm(struct snd_ali
*codec
, int channel
)
585 unsigned int idx
= channel
& 0x0000001f;
587 dev_dbg(codec
->card
->dev
, "free_channel_pcm channel=%d\n", channel
);
589 if (channel
< 0 || channel
>= ALI_CHANNELS
)
592 if (!(codec
->synth
.chmap
& (1 << idx
))) {
593 dev_err(codec
->card
->dev
,
594 "ali_free_channel_pcm: channel %d is not in use.\n",
598 codec
->synth
.chmap
&= ~(1 << idx
);
599 codec
->synth
.chcnt
--;
603 static void snd_ali_stop_voice(struct snd_ali
*codec
, unsigned int channel
)
605 unsigned int mask
= 1 << (channel
& 0x1f);
607 dev_dbg(codec
->card
->dev
, "stop_voice: channel=%d\n", channel
);
608 outl(mask
, ALI_REG(codec
, codec
->chregs
.regs
.stop
));
615 static void snd_ali_delay(struct snd_ali
*codec
,int interval
)
617 unsigned long begintimer
,currenttimer
;
619 begintimer
= inl(ALI_REG(codec
, ALI_STIMER
));
620 currenttimer
= inl(ALI_REG(codec
, ALI_STIMER
));
622 while (currenttimer
< begintimer
+ interval
) {
623 if (snd_ali_stimer_ready(codec
) < 0)
625 currenttimer
= inl(ALI_REG(codec
, ALI_STIMER
));
630 static void snd_ali_detect_spdif_rate(struct snd_ali
*codec
)
636 bval
= inb(ALI_REG(codec
, ALI_SPDIF_CTRL
+ 1));
638 outb(bval
, ALI_REG(codec
, ALI_SPDIF_CTRL
+ 1));
640 while ((R1
< 0x0b || R1
> 0x0e) && R1
!= 0x12 && count
<= 50000) {
642 snd_ali_delay(codec
, 6);
643 bval
= inb(ALI_REG(codec
, ALI_SPDIF_CTRL
+ 1));
648 dev_err(codec
->card
->dev
, "ali_detect_spdif_rate: timeout!\n");
652 for (count
= 0; count
<= 50000; count
++) {
653 snd_ali_delay(codec
, 6);
654 bval
= inb(ALI_REG(codec
,ALI_SPDIF_CTRL
+ 1));
663 dev_err(codec
->card
->dev
, "ali_detect_spdif_rate: timeout!\n");
667 if (R2
>= 0x0b && R2
<= 0x0e) {
668 wval
= inw(ALI_REG(codec
, ALI_SPDIF_CTRL
+ 2));
670 wval
|= (0x09 << 8) | 0x05;
671 outw(wval
, ALI_REG(codec
, ALI_SPDIF_CTRL
+ 2));
673 bval
= inb(ALI_REG(codec
, ALI_SPDIF_CS
+ 3)) & 0xf0;
674 outb(bval
| 0x02, ALI_REG(codec
, ALI_SPDIF_CS
+ 3));
675 } else if (R2
== 0x12) {
676 wval
= inw(ALI_REG(codec
, ALI_SPDIF_CTRL
+ 2));
678 wval
|= (0x0e << 8) | 0x08;
679 outw(wval
, ALI_REG(codec
, ALI_SPDIF_CTRL
+ 2));
681 bval
= inb(ALI_REG(codec
,ALI_SPDIF_CS
+ 3)) & 0xf0;
682 outb(bval
| 0x03, ALI_REG(codec
, ALI_SPDIF_CS
+ 3));
686 static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali
*codec
)
691 bval
= inb(ALI_REG(codec
, ALI_SPDIF_CTRL
));
694 outb(bval
, ALI_REG(codec
, ALI_SPDIF_CTRL
));
696 snd_ali_detect_spdif_rate(codec
);
698 bval
= inb(ALI_REG(codec
, ALI_SPDIF_CS
+ 3));
702 case 0: dwRate
= 44100; break;
703 case 1: dwRate
= 48000; break;
704 case 2: dwRate
= 32000; break;
705 default: dwRate
= 0; break;
711 static void snd_ali_enable_spdif_in(struct snd_ali
*codec
)
715 dwVal
= inl(ALI_REG(codec
, ALI_GLOBAL_CONTROL
));
716 dwVal
|= ALI_SPDIF_IN_SUPPORT
;
717 outl(dwVal
, ALI_REG(codec
, ALI_GLOBAL_CONTROL
));
719 dwVal
= inb(ALI_REG(codec
, ALI_SPDIF_CTRL
));
721 outb(dwVal
, ALI_REG(codec
, ALI_SPDIF_CTRL
));
723 snd_ali_enable_special_channel(codec
, ALI_SPDIF_IN_CHANNEL
);
726 static void snd_ali_disable_spdif_in(struct snd_ali
*codec
)
730 dwVal
= inl(ALI_REG(codec
, ALI_GLOBAL_CONTROL
));
731 dwVal
&= ~ALI_SPDIF_IN_SUPPORT
;
732 outl(dwVal
, ALI_REG(codec
, ALI_GLOBAL_CONTROL
));
734 snd_ali_disable_special_channel(codec
, ALI_SPDIF_IN_CHANNEL
);
738 static void snd_ali_set_spdif_out_rate(struct snd_ali
*codec
, unsigned int rate
)
744 case 32000: dwRate
= 0x300; break;
745 case 48000: dwRate
= 0x200; break;
746 default: dwRate
= 0; break;
749 bVal
= inb(ALI_REG(codec
, ALI_SPDIF_CTRL
));
750 bVal
&= (unsigned char)(~(1<<6));
752 bVal
|= 0x80; /* select right */
753 outb(bVal
, ALI_REG(codec
, ALI_SPDIF_CTRL
));
754 outb(dwRate
| 0x20, ALI_REG(codec
, ALI_SPDIF_CS
+ 2));
756 bVal
&= ~0x80; /* select left */
757 outb(bVal
, ALI_REG(codec
, ALI_SPDIF_CTRL
));
758 outw(rate
| 0x10, ALI_REG(codec
, ALI_SPDIF_CS
+ 2));
761 static void snd_ali_enable_spdif_out(struct snd_ali
*codec
)
765 struct pci_dev
*pci_dev
;
767 pci_dev
= codec
->pci_m1533
;
770 pci_read_config_byte(pci_dev
, 0x61, &bVal
);
772 pci_write_config_byte(pci_dev
, 0x61, bVal
);
773 pci_read_config_byte(pci_dev
, 0x7d, &bVal
);
775 pci_write_config_byte(pci_dev
, 0x7d, bVal
);
777 pci_read_config_byte(pci_dev
, 0x7e, &bVal
);
780 pci_write_config_byte(pci_dev
, 0x7e, bVal
);
782 bVal
= inb(ALI_REG(codec
, ALI_SCTRL
));
783 outb(bVal
| ALI_SPDIF_OUT_ENABLE
, ALI_REG(codec
, ALI_SCTRL
));
785 bVal
= inb(ALI_REG(codec
, ALI_SPDIF_CTRL
));
786 outb(bVal
& ALI_SPDIF_OUT_CH_STATUS
, ALI_REG(codec
, ALI_SPDIF_CTRL
));
788 wVal
= inw(ALI_REG(codec
, ALI_GLOBAL_CONTROL
));
789 wVal
|= ALI_SPDIF_OUT_SEL_PCM
;
790 outw(wVal
, ALI_REG(codec
, ALI_GLOBAL_CONTROL
));
791 snd_ali_disable_special_channel(codec
, ALI_SPDIF_OUT_CHANNEL
);
794 static void snd_ali_enable_spdif_chnout(struct snd_ali
*codec
)
798 wVal
= inw(ALI_REG(codec
, ALI_GLOBAL_CONTROL
));
799 wVal
&= ~ALI_SPDIF_OUT_SEL_PCM
;
800 outw(wVal
, ALI_REG(codec
, ALI_GLOBAL_CONTROL
));
802 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
803 if (flag & ALI_SPDIF_OUT_NON_PCM)
807 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
809 snd_ali_enable_special_channel(codec
, ALI_SPDIF_OUT_CHANNEL
);
812 static void snd_ali_disable_spdif_chnout(struct snd_ali
*codec
)
816 wVal
= inw(ALI_REG(codec
, ALI_GLOBAL_CONTROL
));
817 wVal
|= ALI_SPDIF_OUT_SEL_PCM
;
818 outw(wVal
, ALI_REG(codec
, ALI_GLOBAL_CONTROL
));
820 snd_ali_enable_special_channel(codec
, ALI_SPDIF_OUT_CHANNEL
);
823 static void snd_ali_disable_spdif_out(struct snd_ali
*codec
)
827 bVal
= inb(ALI_REG(codec
, ALI_SCTRL
));
828 outb(bVal
& ~ALI_SPDIF_OUT_ENABLE
, ALI_REG(codec
, ALI_SCTRL
));
830 snd_ali_disable_spdif_chnout(codec
);
833 static void snd_ali_update_ptr(struct snd_ali
*codec
, int channel
)
835 struct snd_ali_voice
*pvoice
;
836 struct snd_ali_channel_control
*pchregs
;
837 unsigned int old
, mask
;
839 pchregs
= &(codec
->chregs
);
841 /* check if interrupt occurred for channel */
842 old
= pchregs
->data
.aint
;
843 mask
= 1U << (channel
& 0x1f);
848 pvoice
= &codec
->synth
.voices
[channel
];
851 spin_lock(&codec
->reg_lock
);
853 if (pvoice
->pcm
&& pvoice
->substream
) {
855 if (pvoice
->running
) {
856 dev_dbg(codec
->card
->dev
,
857 "update_ptr: cso=%4.4x cspf=%d.\n",
858 inw(ALI_REG(codec
, ALI_CSO_ALPHA_FMS
+ 2)),
859 (inl(ALI_REG(codec
, ALI_CSPF
)) & mask
) == mask
);
860 spin_unlock(&codec
->reg_lock
);
861 snd_pcm_period_elapsed(pvoice
->substream
);
862 spin_lock(&codec
->reg_lock
);
864 snd_ali_stop_voice(codec
, channel
);
865 snd_ali_disable_voice_irq(codec
, channel
);
867 } else if (codec
->synth
.voices
[channel
].synth
) {
868 /* synth interrupt */
869 } else if (codec
->synth
.voices
[channel
].midi
) {
872 /* unknown interrupt */
873 snd_ali_stop_voice(codec
, channel
);
874 snd_ali_disable_voice_irq(codec
, channel
);
876 spin_unlock(&codec
->reg_lock
);
877 outl(mask
,ALI_REG(codec
,pchregs
->regs
.aint
));
878 pchregs
->data
.aint
= old
& (~mask
);
881 static irqreturn_t
snd_ali_card_interrupt(int irq
, void *dev_id
)
883 struct snd_ali
*codec
= dev_id
;
885 unsigned int audio_int
;
886 struct snd_ali_channel_control
*pchregs
;
888 if (codec
== NULL
|| !codec
->hw_initialized
)
891 audio_int
= inl(ALI_REG(codec
, ALI_MISCINT
));
895 pchregs
= &(codec
->chregs
);
896 if (audio_int
& ADDRESS_IRQ
) {
897 /* get interrupt status for all channels */
898 pchregs
->data
.aint
= inl(ALI_REG(codec
, pchregs
->regs
.aint
));
899 for (channel
= 0; channel
< ALI_CHANNELS
; channel
++)
900 snd_ali_update_ptr(codec
, channel
);
902 outl((TARGET_REACHED
| MIXER_OVERFLOW
| MIXER_UNDERFLOW
),
903 ALI_REG(codec
, ALI_MISCINT
));
909 static struct snd_ali_voice
*snd_ali_alloc_voice(struct snd_ali
* codec
,
910 int type
, int rec
, int channel
)
912 struct snd_ali_voice
*pvoice
;
915 dev_dbg(codec
->card
->dev
, "alloc_voice: type=%d rec=%d\n", type
, rec
);
917 spin_lock_irq(&codec
->voice_alloc
);
918 if (type
== SNDRV_ALI_VOICE_TYPE_PCM
) {
919 idx
= channel
> 0 ? snd_ali_alloc_pcm_channel(codec
, channel
) :
920 snd_ali_find_free_channel(codec
,rec
);
922 dev_err(codec
->card
->dev
, "ali_alloc_voice: err.\n");
923 spin_unlock_irq(&codec
->voice_alloc
);
926 pvoice
= &(codec
->synth
.voices
[idx
]);
927 pvoice
->codec
= codec
;
931 spin_unlock_irq(&codec
->voice_alloc
);
934 spin_unlock_irq(&codec
->voice_alloc
);
939 static void snd_ali_free_voice(struct snd_ali
* codec
,
940 struct snd_ali_voice
*pvoice
)
942 void (*private_free
)(void *);
945 dev_dbg(codec
->card
->dev
, "free_voice: channel=%d\n", pvoice
->number
);
948 snd_ali_clear_voices(codec
, pvoice
->number
, pvoice
->number
);
949 spin_lock_irq(&codec
->voice_alloc
);
950 private_free
= pvoice
->private_free
;
951 private_data
= pvoice
->private_data
;
952 pvoice
->private_free
= NULL
;
953 pvoice
->private_data
= NULL
;
955 snd_ali_free_channel_pcm(codec
, pvoice
->number
);
956 pvoice
->use
= pvoice
->pcm
= pvoice
->synth
= 0;
957 pvoice
->substream
= NULL
;
958 spin_unlock_irq(&codec
->voice_alloc
);
960 private_free(private_data
);
964 static void snd_ali_clear_voices(struct snd_ali
*codec
,
970 for (i
= v_min
; i
<= v_max
; i
++) {
971 snd_ali_stop_voice(codec
, i
);
972 snd_ali_disable_voice_irq(codec
, i
);
976 static void snd_ali_write_voice_regs(struct snd_ali
*codec
,
977 unsigned int Channel
,
982 unsigned int ALPHA_FMS
,
989 unsigned int ctlcmds
[4];
991 outb((unsigned char)(Channel
& 0x001f), ALI_REG(codec
, ALI_GC_CIR
));
993 ctlcmds
[0] = (CSO
<< 16) | (ALPHA_FMS
& 0x0000ffff);
995 ctlcmds
[2] = (ESO
<< 16) | (DELTA
& 0x0ffff);
996 ctlcmds
[3] = (GVSEL
<< 31) |
997 ((PAN
& 0x0000007f) << 24) |
998 ((VOL
& 0x000000ff) << 16) |
999 ((CTRL
& 0x0000000f) << 12) |
1002 outb(Channel
, ALI_REG(codec
, ALI_GC_CIR
));
1004 outl(ctlcmds
[0], ALI_REG(codec
, ALI_CSO_ALPHA_FMS
));
1005 outl(ctlcmds
[1], ALI_REG(codec
, ALI_LBA
));
1006 outl(ctlcmds
[2], ALI_REG(codec
, ALI_ESO_DELTA
));
1007 outl(ctlcmds
[3], ALI_REG(codec
, ALI_GVSEL_PAN_VOC_CTRL_EC
));
1009 outl(0x30000000, ALI_REG(codec
, ALI_EBUF1
)); /* Still Mode */
1010 outl(0x30000000, ALI_REG(codec
, ALI_EBUF2
)); /* Still Mode */
1013 static unsigned int snd_ali_convert_rate(unsigned int rate
, int rec
)
1025 else if (rate
== 8000)
1027 else if (rate
== 48000)
1030 delta
= ((48000 << 12) / rate
) & 0x0000ffff;
1034 else if (rate
== 8000)
1036 else if (rate
== 48000)
1039 delta
= (((rate
<< 12) + rate
) / 48000) & 0x0000ffff;
1045 static unsigned int snd_ali_control_mode(struct snd_pcm_substream
*substream
)
1048 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1051 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1054 if (snd_pcm_format_width(runtime
->format
) == 16)
1055 CTRL
|= 0x00000008; /* 16-bit data */
1056 if (!snd_pcm_format_unsigned(runtime
->format
))
1057 CTRL
|= 0x00000002; /* signed data */
1058 if (runtime
->channels
> 1)
1059 CTRL
|= 0x00000004; /* stereo data */
1067 static int snd_ali_trigger(struct snd_pcm_substream
*substream
,
1071 struct snd_ali
*codec
= snd_pcm_substream_chip(substream
);
1072 struct snd_pcm_substream
*s
;
1073 unsigned int what
, whati
;
1074 struct snd_ali_voice
*pvoice
, *evoice
;
1079 case SNDRV_PCM_TRIGGER_START
:
1080 case SNDRV_PCM_TRIGGER_RESUME
:
1083 case SNDRV_PCM_TRIGGER_STOP
:
1084 case SNDRV_PCM_TRIGGER_SUSPEND
:
1092 snd_pcm_group_for_each_entry(s
, substream
) {
1093 if ((struct snd_ali
*) snd_pcm_substream_chip(s
) == codec
) {
1094 pvoice
= s
->runtime
->private_data
;
1095 evoice
= pvoice
->extra
;
1096 what
|= 1 << (pvoice
->number
& 0x1f);
1098 whati
|= 1 << (pvoice
->number
& 0x1f);
1100 whati
|= 1 << (evoice
->number
& 0x1f);
1101 what
|= 1 << (evoice
->number
& 0x1f);
1104 pvoice
->running
= 1;
1106 evoice
->running
= 1;
1108 pvoice
->running
= 0;
1110 evoice
->running
= 0;
1112 snd_pcm_trigger_done(s
, substream
);
1115 spin_lock(&codec
->reg_lock
);
1117 outl(what
, ALI_REG(codec
, ALI_STOP
));
1118 val
= inl(ALI_REG(codec
, ALI_AINTEN
));
1123 outl(val
, ALI_REG(codec
, ALI_AINTEN
));
1125 outl(what
, ALI_REG(codec
, ALI_START
));
1126 dev_dbg(codec
->card
->dev
, "trigger: what=%xh whati=%xh\n", what
, whati
);
1127 spin_unlock(&codec
->reg_lock
);
1132 static int snd_ali_playback_hw_params(struct snd_pcm_substream
*substream
,
1133 struct snd_pcm_hw_params
*hw_params
)
1135 struct snd_ali
*codec
= snd_pcm_substream_chip(substream
);
1136 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1137 struct snd_ali_voice
*pvoice
= runtime
->private_data
;
1138 struct snd_ali_voice
*evoice
= pvoice
->extra
;
1140 /* voice management */
1142 if (params_buffer_size(hw_params
) / 2 !=
1143 params_period_size(hw_params
)) {
1145 evoice
= snd_ali_alloc_voice(codec
,
1146 SNDRV_ALI_VOICE_TYPE_PCM
,
1150 pvoice
->extra
= evoice
;
1151 evoice
->substream
= substream
;
1155 snd_ali_free_voice(codec
, evoice
);
1156 pvoice
->extra
= evoice
= NULL
;
1163 static int snd_ali_playback_hw_free(struct snd_pcm_substream
*substream
)
1165 struct snd_ali
*codec
= snd_pcm_substream_chip(substream
);
1166 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1167 struct snd_ali_voice
*pvoice
= runtime
->private_data
;
1168 struct snd_ali_voice
*evoice
= pvoice
? pvoice
->extra
: NULL
;
1171 snd_ali_free_voice(codec
, evoice
);
1172 pvoice
->extra
= NULL
;
1177 static int snd_ali_playback_prepare(struct snd_pcm_substream
*substream
)
1179 struct snd_ali
*codec
= snd_pcm_substream_chip(substream
);
1180 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1181 struct snd_ali_voice
*pvoice
= runtime
->private_data
;
1182 struct snd_ali_voice
*evoice
= pvoice
->extra
;
1193 dev_dbg(codec
->card
->dev
, "playback_prepare ...\n");
1195 spin_lock_irq(&codec
->reg_lock
);
1197 /* set Delta (rate) value */
1198 Delta
= snd_ali_convert_rate(runtime
->rate
, 0);
1200 if (pvoice
->number
== ALI_SPDIF_IN_CHANNEL
||
1201 pvoice
->number
== ALI_PCM_IN_CHANNEL
)
1202 snd_ali_disable_special_channel(codec
, pvoice
->number
);
1203 else if (codec
->spdif_support
&&
1204 (inl(ALI_REG(codec
, ALI_GLOBAL_CONTROL
)) &
1205 ALI_SPDIF_OUT_CH_ENABLE
)
1206 && pvoice
->number
== ALI_SPDIF_OUT_CHANNEL
) {
1207 snd_ali_set_spdif_out_rate(codec
, runtime
->rate
);
1211 /* set Loop Back Address */
1212 LBA
= runtime
->dma_addr
;
1214 /* set interrupt count size */
1215 pvoice
->count
= runtime
->period_size
;
1217 /* set target ESO for channel */
1218 pvoice
->eso
= runtime
->buffer_size
;
1220 dev_dbg(codec
->card
->dev
, "playback_prepare: eso=%xh count=%xh\n",
1221 pvoice
->eso
, pvoice
->count
);
1223 /* set ESO to capture first MIDLP interrupt */
1224 ESO
= pvoice
->eso
-1;
1226 CTRL
= snd_ali_control_mode(substream
);
1232 dev_dbg(codec
->card
->dev
, "playback_prepare:\n");
1233 dev_dbg(codec
->card
->dev
,
1234 "ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
1235 pvoice
->number
,runtime
->rate
,Delta
,GVSEL
,PAN
,CTRL
);
1236 snd_ali_write_voice_regs(codec
,
1249 evoice
->count
= pvoice
->count
;
1250 evoice
->eso
= pvoice
->count
<< 1;
1251 ESO
= evoice
->eso
- 1;
1252 snd_ali_write_voice_regs(codec
,
1265 spin_unlock_irq(&codec
->reg_lock
);
1270 static int snd_ali_prepare(struct snd_pcm_substream
*substream
)
1272 struct snd_ali
*codec
= snd_pcm_substream_chip(substream
);
1273 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1274 struct snd_ali_voice
*pvoice
= runtime
->private_data
;
1285 spin_lock_irq(&codec
->reg_lock
);
1287 dev_dbg(codec
->card
->dev
, "ali_prepare...\n");
1289 snd_ali_enable_special_channel(codec
,pvoice
->number
);
1291 Delta
= (pvoice
->number
== ALI_MODEM_IN_CHANNEL
||
1292 pvoice
->number
== ALI_MODEM_OUT_CHANNEL
) ?
1293 0x1000 : snd_ali_convert_rate(runtime
->rate
, pvoice
->mode
);
1295 /* Prepare capture intr channel */
1296 if (pvoice
->number
== ALI_SPDIF_IN_CHANNEL
) {
1300 spin_unlock_irq(&codec
->reg_lock
);
1301 if (codec
->revision
!= ALI_5451_V02
)
1304 rate
= snd_ali_get_spdif_in_rate(codec
);
1306 dev_warn(codec
->card
->dev
,
1307 "ali_capture_prepare: spdif rate detect err!\n");
1310 spin_lock_irq(&codec
->reg_lock
);
1311 bValue
= inb(ALI_REG(codec
,ALI_SPDIF_CTRL
));
1312 if (bValue
& 0x10) {
1313 outb(bValue
,ALI_REG(codec
,ALI_SPDIF_CTRL
));
1314 dev_warn(codec
->card
->dev
,
1315 "clear SPDIF parity error flag.\n");
1319 Delta
= ((rate
<< 12) / runtime
->rate
) & 0x00ffff;
1322 /* set target ESO for channel */
1323 pvoice
->eso
= runtime
->buffer_size
;
1325 /* set interrupt count size */
1326 pvoice
->count
= runtime
->period_size
;
1328 /* set Loop Back Address */
1329 LBA
= runtime
->dma_addr
;
1331 /* set ESO to capture first MIDLP interrupt */
1332 ESO
= pvoice
->eso
- 1;
1333 CTRL
= snd_ali_control_mode(substream
);
1339 snd_ali_write_voice_regs( codec
,
1352 spin_unlock_irq(&codec
->reg_lock
);
1358 static snd_pcm_uframes_t
1359 snd_ali_playback_pointer(struct snd_pcm_substream
*substream
)
1361 struct snd_ali
*codec
= snd_pcm_substream_chip(substream
);
1362 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1363 struct snd_ali_voice
*pvoice
= runtime
->private_data
;
1366 spin_lock(&codec
->reg_lock
);
1367 if (!pvoice
->running
) {
1368 spin_unlock(&codec
->reg_lock
);
1371 outb(pvoice
->number
, ALI_REG(codec
, ALI_GC_CIR
));
1372 cso
= inw(ALI_REG(codec
, ALI_CSO_ALPHA_FMS
+ 2));
1373 spin_unlock(&codec
->reg_lock
);
1374 dev_dbg(codec
->card
->dev
, "playback pointer returned cso=%xh.\n", cso
);
1376 cso
%= runtime
->buffer_size
;
1381 static snd_pcm_uframes_t
snd_ali_pointer(struct snd_pcm_substream
*substream
)
1383 struct snd_ali
*codec
= snd_pcm_substream_chip(substream
);
1384 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1385 struct snd_ali_voice
*pvoice
= runtime
->private_data
;
1388 spin_lock(&codec
->reg_lock
);
1389 if (!pvoice
->running
) {
1390 spin_unlock(&codec
->reg_lock
);
1393 outb(pvoice
->number
, ALI_REG(codec
, ALI_GC_CIR
));
1394 cso
= inw(ALI_REG(codec
, ALI_CSO_ALPHA_FMS
+ 2));
1395 spin_unlock(&codec
->reg_lock
);
1397 cso
%= runtime
->buffer_size
;
1401 static const struct snd_pcm_hardware snd_ali_playback
=
1403 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
1404 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
1405 SNDRV_PCM_INFO_MMAP_VALID
|
1406 SNDRV_PCM_INFO_RESUME
|
1407 SNDRV_PCM_INFO_SYNC_START
),
1408 .formats
= (SNDRV_PCM_FMTBIT_U8
| SNDRV_PCM_FMTBIT_S16_LE
|
1409 SNDRV_PCM_FMTBIT_S8
| SNDRV_PCM_FMTBIT_U16_LE
),
1410 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
1415 .buffer_bytes_max
= (256*1024),
1416 .period_bytes_min
= 64,
1417 .period_bytes_max
= (256*1024),
1419 .periods_max
= 1024,
1424 * Capture support device description
1427 static const struct snd_pcm_hardware snd_ali_capture
=
1429 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
1430 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
1431 SNDRV_PCM_INFO_MMAP_VALID
|
1432 SNDRV_PCM_INFO_RESUME
|
1433 SNDRV_PCM_INFO_SYNC_START
),
1434 .formats
= (SNDRV_PCM_FMTBIT_U8
| SNDRV_PCM_FMTBIT_S16_LE
|
1435 SNDRV_PCM_FMTBIT_S8
| SNDRV_PCM_FMTBIT_U16_LE
),
1436 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
1441 .buffer_bytes_max
= (128*1024),
1442 .period_bytes_min
= 64,
1443 .period_bytes_max
= (128*1024),
1445 .periods_max
= 1024,
1449 static void snd_ali_pcm_free_substream(struct snd_pcm_runtime
*runtime
)
1451 struct snd_ali_voice
*pvoice
= runtime
->private_data
;
1454 snd_ali_free_voice(pvoice
->codec
, pvoice
);
1457 static int snd_ali_open(struct snd_pcm_substream
*substream
, int rec
,
1458 int channel
, const struct snd_pcm_hardware
*phw
)
1460 struct snd_ali
*codec
= snd_pcm_substream_chip(substream
);
1461 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1462 struct snd_ali_voice
*pvoice
;
1464 pvoice
= snd_ali_alloc_voice(codec
, SNDRV_ALI_VOICE_TYPE_PCM
, rec
,
1469 pvoice
->substream
= substream
;
1470 runtime
->private_data
= pvoice
;
1471 runtime
->private_free
= snd_ali_pcm_free_substream
;
1474 snd_pcm_set_sync(substream
);
1475 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1480 static int snd_ali_playback_open(struct snd_pcm_substream
*substream
)
1482 return snd_ali_open(substream
, 0, -1, &snd_ali_playback
);
1485 static int snd_ali_capture_open(struct snd_pcm_substream
*substream
)
1487 return snd_ali_open(substream
, 1, -1, &snd_ali_capture
);
1490 static int snd_ali_playback_close(struct snd_pcm_substream
*substream
)
1495 static int snd_ali_close(struct snd_pcm_substream
*substream
)
1497 struct snd_ali
*codec
= snd_pcm_substream_chip(substream
);
1498 struct snd_ali_voice
*pvoice
= substream
->runtime
->private_data
;
1500 snd_ali_disable_special_channel(codec
,pvoice
->number
);
1505 static const struct snd_pcm_ops snd_ali_playback_ops
= {
1506 .open
= snd_ali_playback_open
,
1507 .close
= snd_ali_playback_close
,
1508 .hw_params
= snd_ali_playback_hw_params
,
1509 .hw_free
= snd_ali_playback_hw_free
,
1510 .prepare
= snd_ali_playback_prepare
,
1511 .trigger
= snd_ali_trigger
,
1512 .pointer
= snd_ali_playback_pointer
,
1515 static const struct snd_pcm_ops snd_ali_capture_ops
= {
1516 .open
= snd_ali_capture_open
,
1517 .close
= snd_ali_close
,
1518 .prepare
= snd_ali_prepare
,
1519 .trigger
= snd_ali_trigger
,
1520 .pointer
= snd_ali_pointer
,
1527 static int snd_ali_modem_hw_params(struct snd_pcm_substream
*substream
,
1528 struct snd_pcm_hw_params
*hw_params
)
1530 struct snd_ali
*chip
= snd_pcm_substream_chip(substream
);
1531 unsigned int modem_num
= chip
->num_of_codecs
- 1;
1532 snd_ac97_write(chip
->ac97
[modem_num
], AC97_LINE1_RATE
,
1533 params_rate(hw_params
));
1534 snd_ac97_write(chip
->ac97
[modem_num
], AC97_LINE1_LEVEL
, 0);
1538 static const struct snd_pcm_hardware snd_ali_modem
=
1540 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
1541 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
1542 SNDRV_PCM_INFO_MMAP_VALID
|
1543 SNDRV_PCM_INFO_RESUME
|
1544 SNDRV_PCM_INFO_SYNC_START
),
1545 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
1546 .rates
= (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_8000
|
1547 SNDRV_PCM_RATE_16000
),
1552 .buffer_bytes_max
= (256*1024),
1553 .period_bytes_min
= 64,
1554 .period_bytes_max
= (256*1024),
1556 .periods_max
= 1024,
1560 static int snd_ali_modem_open(struct snd_pcm_substream
*substream
, int rec
,
1563 static const unsigned int rates
[] = {8000, 9600, 12000, 16000};
1564 static const struct snd_pcm_hw_constraint_list hw_constraint_rates
= {
1565 .count
= ARRAY_SIZE(rates
),
1569 int err
= snd_ali_open(substream
, rec
, channel
, &snd_ali_modem
);
1573 return snd_pcm_hw_constraint_list(substream
->runtime
, 0,
1574 SNDRV_PCM_HW_PARAM_RATE
, &hw_constraint_rates
);
1577 static int snd_ali_modem_playback_open(struct snd_pcm_substream
*substream
)
1579 return snd_ali_modem_open(substream
, 0, ALI_MODEM_OUT_CHANNEL
);
1582 static int snd_ali_modem_capture_open(struct snd_pcm_substream
*substream
)
1584 return snd_ali_modem_open(substream
, 1, ALI_MODEM_IN_CHANNEL
);
1587 static const struct snd_pcm_ops snd_ali_modem_playback_ops
= {
1588 .open
= snd_ali_modem_playback_open
,
1589 .close
= snd_ali_close
,
1590 .hw_params
= snd_ali_modem_hw_params
,
1591 .prepare
= snd_ali_prepare
,
1592 .trigger
= snd_ali_trigger
,
1593 .pointer
= snd_ali_pointer
,
1596 static const struct snd_pcm_ops snd_ali_modem_capture_ops
= {
1597 .open
= snd_ali_modem_capture_open
,
1598 .close
= snd_ali_close
,
1599 .hw_params
= snd_ali_modem_hw_params
,
1600 .prepare
= snd_ali_prepare
,
1601 .trigger
= snd_ali_trigger
,
1602 .pointer
= snd_ali_pointer
,
1606 struct ali_pcm_description
{
1608 unsigned int playback_num
;
1609 unsigned int capture_num
;
1610 const struct snd_pcm_ops
*playback_ops
;
1611 const struct snd_pcm_ops
*capture_ops
;
1612 unsigned short class;
1616 static void snd_ali_pcm_free(struct snd_pcm
*pcm
)
1618 struct snd_ali
*codec
= pcm
->private_data
;
1619 codec
->pcm
[pcm
->device
] = NULL
;
1623 static int snd_ali_pcm(struct snd_ali
*codec
, int device
,
1624 struct ali_pcm_description
*desc
)
1626 struct snd_pcm
*pcm
;
1629 err
= snd_pcm_new(codec
->card
, desc
->name
, device
,
1630 desc
->playback_num
, desc
->capture_num
, &pcm
);
1632 dev_err(codec
->card
->dev
,
1633 "snd_ali_pcm: err called snd_pcm_new.\n");
1636 pcm
->private_data
= codec
;
1637 pcm
->private_free
= snd_ali_pcm_free
;
1638 if (desc
->playback_ops
)
1639 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
1640 desc
->playback_ops
);
1641 if (desc
->capture_ops
)
1642 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
1645 snd_pcm_set_managed_buffer_all(pcm
, SNDRV_DMA_TYPE_DEV
,
1646 &codec
->pci
->dev
, 64*1024, 128*1024);
1648 pcm
->info_flags
= 0;
1649 pcm
->dev_class
= desc
->class;
1650 pcm
->dev_subclass
= SNDRV_PCM_SUBCLASS_GENERIC_MIX
;
1651 strcpy(pcm
->name
, desc
->name
);
1652 codec
->pcm
[0] = pcm
;
1656 static struct ali_pcm_description ali_pcms
[] = {
1657 { .name
= "ALI 5451",
1658 .playback_num
= ALI_CHANNELS
,
1660 .playback_ops
= &snd_ali_playback_ops
,
1661 .capture_ops
= &snd_ali_capture_ops
1663 { .name
= "ALI 5451 modem",
1666 .playback_ops
= &snd_ali_modem_playback_ops
,
1667 .capture_ops
= &snd_ali_modem_capture_ops
,
1668 .class = SNDRV_PCM_CLASS_MODEM
1672 static int snd_ali_build_pcms(struct snd_ali
*codec
)
1675 for (i
= 0; i
< codec
->num_of_codecs
&& i
< ARRAY_SIZE(ali_pcms
); i
++) {
1676 err
= snd_ali_pcm(codec
, i
, &ali_pcms
[i
]);
1684 #define ALI5451_SPDIF(xname, xindex, value) \
1685 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1686 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1687 .put = snd_ali5451_spdif_put, .private_value = value}
1689 #define snd_ali5451_spdif_info snd_ctl_boolean_mono_info
1691 static int snd_ali5451_spdif_get(struct snd_kcontrol
*kcontrol
,
1692 struct snd_ctl_elem_value
*ucontrol
)
1694 struct snd_ali
*codec
= kcontrol
->private_data
;
1695 unsigned int spdif_enable
;
1697 spdif_enable
= ucontrol
->value
.integer
.value
[0] ? 1 : 0;
1699 spin_lock_irq(&codec
->reg_lock
);
1700 switch (kcontrol
->private_value
) {
1702 spdif_enable
= (codec
->spdif_mask
& 0x02) ? 1 : 0;
1705 spdif_enable
= ((codec
->spdif_mask
& 0x02) &&
1706 (codec
->spdif_mask
& 0x04)) ? 1 : 0;
1709 spdif_enable
= (codec
->spdif_mask
& 0x01) ? 1 : 0;
1714 ucontrol
->value
.integer
.value
[0] = spdif_enable
;
1715 spin_unlock_irq(&codec
->reg_lock
);
1719 static int snd_ali5451_spdif_put(struct snd_kcontrol
*kcontrol
,
1720 struct snd_ctl_elem_value
*ucontrol
)
1722 struct snd_ali
*codec
= kcontrol
->private_data
;
1723 unsigned int change
= 0, spdif_enable
= 0;
1725 spdif_enable
= ucontrol
->value
.integer
.value
[0] ? 1 : 0;
1727 spin_lock_irq(&codec
->reg_lock
);
1728 switch (kcontrol
->private_value
) {
1730 change
= (codec
->spdif_mask
& 0x02) ? 1 : 0;
1731 change
= change
^ spdif_enable
;
1734 codec
->spdif_mask
|= 0x02;
1735 snd_ali_enable_spdif_out(codec
);
1737 codec
->spdif_mask
&= ~(0x02);
1738 codec
->spdif_mask
&= ~(0x04);
1739 snd_ali_disable_spdif_out(codec
);
1744 change
= (codec
->spdif_mask
& 0x04) ? 1 : 0;
1745 change
= change
^ spdif_enable
;
1746 if (change
&& (codec
->spdif_mask
& 0x02)) {
1748 codec
->spdif_mask
|= 0x04;
1749 snd_ali_enable_spdif_chnout(codec
);
1751 codec
->spdif_mask
&= ~(0x04);
1752 snd_ali_disable_spdif_chnout(codec
);
1757 change
= (codec
->spdif_mask
& 0x01) ? 1 : 0;
1758 change
= change
^ spdif_enable
;
1761 codec
->spdif_mask
|= 0x01;
1762 snd_ali_enable_spdif_in(codec
);
1764 codec
->spdif_mask
&= ~(0x01);
1765 snd_ali_disable_spdif_in(codec
);
1772 spin_unlock_irq(&codec
->reg_lock
);
1777 static const struct snd_kcontrol_new snd_ali5451_mixer_spdif
[] = {
1778 /* spdif aplayback switch */
1779 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1780 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE
,SWITCH
), 0, 0),
1781 /* spdif out to spdif channel */
1782 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE
,SWITCH
), 0, 1),
1783 /* spdif in from spdif channel */
1784 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE
,SWITCH
), 0, 2)
1787 static int snd_ali_mixer(struct snd_ali
*codec
)
1789 struct snd_ac97_template ac97
;
1792 static const struct snd_ac97_bus_ops ops
= {
1793 .write
= snd_ali_codec_write
,
1794 .read
= snd_ali_codec_read
,
1797 err
= snd_ac97_bus(codec
->card
, 0, &ops
, codec
, &codec
->ac97_bus
);
1801 memset(&ac97
, 0, sizeof(ac97
));
1802 ac97
.private_data
= codec
;
1804 for (i
= 0; i
< codec
->num_of_codecs
; i
++) {
1806 err
= snd_ac97_mixer(codec
->ac97_bus
, &ac97
, &codec
->ac97
[i
]);
1808 dev_err(codec
->card
->dev
,
1809 "ali mixer %d creating error.\n", i
);
1812 codec
->num_of_codecs
= 1;
1817 if (codec
->spdif_support
) {
1818 for (idx
= 0; idx
< ARRAY_SIZE(snd_ali5451_mixer_spdif
); idx
++) {
1819 err
= snd_ctl_add(codec
->card
,
1820 snd_ctl_new1(&snd_ali5451_mixer_spdif
[idx
], codec
));
1828 #ifdef CONFIG_PM_SLEEP
1829 static int ali_suspend(struct device
*dev
)
1831 struct snd_card
*card
= dev_get_drvdata(dev
);
1832 struct snd_ali
*chip
= card
->private_data
;
1833 struct snd_ali_image
*im
;
1840 snd_power_change_state(card
, SNDRV_CTL_POWER_D3hot
);
1841 for (i
= 0; i
< chip
->num_of_codecs
; i
++)
1842 snd_ac97_suspend(chip
->ac97
[i
]);
1844 spin_lock_irq(&chip
->reg_lock
);
1846 im
->regs
[ALI_MISCINT
>> 2] = inl(ALI_REG(chip
, ALI_MISCINT
));
1847 /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
1848 im
->regs
[ALI_STOP
>> 2] = inl(ALI_REG(chip
, ALI_STOP
));
1850 /* disable all IRQ bits */
1851 outl(0, ALI_REG(chip
, ALI_MISCINT
));
1853 for (i
= 0; i
< ALI_GLOBAL_REGS
; i
++) {
1854 if ((i
*4 == ALI_MISCINT
) || (i
*4 == ALI_STOP
))
1856 im
->regs
[i
] = inl(ALI_REG(chip
, i
*4));
1859 for (i
= 0; i
< ALI_CHANNELS
; i
++) {
1860 outb(i
, ALI_REG(chip
, ALI_GC_CIR
));
1861 for (j
= 0; j
< ALI_CHANNEL_REGS
; j
++)
1862 im
->channel_regs
[i
][j
] = inl(ALI_REG(chip
, j
*4 + 0xe0));
1865 /* stop all HW channel */
1866 outl(0xffffffff, ALI_REG(chip
, ALI_STOP
));
1868 spin_unlock_irq(&chip
->reg_lock
);
1872 static int ali_resume(struct device
*dev
)
1874 struct snd_card
*card
= dev_get_drvdata(dev
);
1875 struct snd_ali
*chip
= card
->private_data
;
1876 struct snd_ali_image
*im
;
1883 spin_lock_irq(&chip
->reg_lock
);
1885 for (i
= 0; i
< ALI_CHANNELS
; i
++) {
1886 outb(i
, ALI_REG(chip
, ALI_GC_CIR
));
1887 for (j
= 0; j
< ALI_CHANNEL_REGS
; j
++)
1888 outl(im
->channel_regs
[i
][j
], ALI_REG(chip
, j
*4 + 0xe0));
1891 for (i
= 0; i
< ALI_GLOBAL_REGS
; i
++) {
1892 if ((i
*4 == ALI_MISCINT
) || (i
*4 == ALI_STOP
) ||
1895 outl(im
->regs
[i
], ALI_REG(chip
, i
*4));
1898 /* start HW channel */
1899 outl(im
->regs
[ALI_START
>> 2], ALI_REG(chip
, ALI_START
));
1900 /* restore IRQ enable bits */
1901 outl(im
->regs
[ALI_MISCINT
>> 2], ALI_REG(chip
, ALI_MISCINT
));
1903 spin_unlock_irq(&chip
->reg_lock
);
1905 for (i
= 0 ; i
< chip
->num_of_codecs
; i
++)
1906 snd_ac97_resume(chip
->ac97
[i
]);
1908 snd_power_change_state(card
, SNDRV_CTL_POWER_D0
);
1912 static SIMPLE_DEV_PM_OPS(ali_pm
, ali_suspend
, ali_resume
);
1913 #define ALI_PM_OPS &ali_pm
1915 #define ALI_PM_OPS NULL
1916 #endif /* CONFIG_PM_SLEEP */
1918 static int snd_ali_free(struct snd_ali
* codec
)
1920 if (codec
->hw_initialized
)
1921 snd_ali_disable_address_interrupt(codec
);
1922 if (codec
->irq
>= 0)
1923 free_irq(codec
->irq
, codec
);
1925 pci_release_regions(codec
->pci
);
1926 pci_disable_device(codec
->pci
);
1927 #ifdef CONFIG_PM_SLEEP
1928 kfree(codec
->image
);
1930 pci_dev_put(codec
->pci_m1533
);
1931 pci_dev_put(codec
->pci_m7101
);
1936 static int snd_ali_chip_init(struct snd_ali
*codec
)
1938 unsigned int legacy
;
1940 struct pci_dev
*pci_dev
;
1942 dev_dbg(codec
->card
->dev
, "chip initializing ...\n");
1944 if (snd_ali_reset_5451(codec
)) {
1945 dev_err(codec
->card
->dev
, "ali_chip_init: reset 5451 error.\n");
1949 if (codec
->revision
== ALI_5451_V02
) {
1950 pci_dev
= codec
->pci_m1533
;
1951 pci_read_config_byte(pci_dev
, 0x59, &temp
);
1953 pci_write_config_byte(pci_dev
, 0x59, temp
);
1955 pci_dev
= codec
->pci_m7101
;
1956 pci_read_config_byte(pci_dev
, 0xb8, &temp
);
1958 pci_write_config_byte(pci_dev
, 0xB8, temp
);
1961 pci_read_config_dword(codec
->pci
, 0x44, &legacy
);
1962 legacy
&= 0xff00ff00;
1963 legacy
|= 0x000800aa;
1964 pci_write_config_dword(codec
->pci
, 0x44, legacy
);
1966 outl(0x80000001, ALI_REG(codec
, ALI_GLOBAL_CONTROL
));
1967 outl(0x00000000, ALI_REG(codec
, ALI_AINTEN
));
1968 outl(0xffffffff, ALI_REG(codec
, ALI_AINT
));
1969 outl(0x00000000, ALI_REG(codec
, ALI_VOLUME
));
1970 outb(0x10, ALI_REG(codec
, ALI_MPUR2
));
1972 codec
->ac97_ext_id
= snd_ali_codec_peek(codec
, 0, AC97_EXTENDED_ID
);
1973 codec
->ac97_ext_status
= snd_ali_codec_peek(codec
, 0,
1974 AC97_EXTENDED_STATUS
);
1975 if (codec
->spdif_support
) {
1976 snd_ali_enable_spdif_out(codec
);
1977 codec
->spdif_mask
= 0x00000002;
1980 codec
->num_of_codecs
= 1;
1982 /* secondary codec - modem */
1983 if (inl(ALI_REG(codec
, ALI_SCTRL
)) & ALI_SCTRL_CODEC2_READY
) {
1984 codec
->num_of_codecs
++;
1985 outl(inl(ALI_REG(codec
, ALI_SCTRL
)) |
1986 (ALI_SCTRL_LINE_IN2
| ALI_SCTRL_GPIO_IN2
|
1987 ALI_SCTRL_LINE_OUT_EN
),
1988 ALI_REG(codec
, ALI_SCTRL
));
1991 dev_dbg(codec
->card
->dev
, "chip initialize succeed.\n");
1996 /* proc for register dump */
1997 static void snd_ali_proc_read(struct snd_info_entry
*entry
,
1998 struct snd_info_buffer
*buf
)
2000 struct snd_ali
*codec
= entry
->private_data
;
2002 for (i
= 0; i
< 256 ; i
+= 4)
2003 snd_iprintf(buf
, "%02x: %08x\n", i
, inl(ALI_REG(codec
, i
)));
2006 static void snd_ali_proc_init(struct snd_ali
*codec
)
2008 snd_card_ro_proc_new(codec
->card
, "ali5451", codec
, snd_ali_proc_read
);
2011 static int snd_ali_resources(struct snd_ali
*codec
)
2015 dev_dbg(codec
->card
->dev
, "resources allocation ...\n");
2016 err
= pci_request_regions(codec
->pci
, "ALI 5451");
2019 codec
->port
= pci_resource_start(codec
->pci
, 0);
2021 if (request_irq(codec
->pci
->irq
, snd_ali_card_interrupt
,
2022 IRQF_SHARED
, KBUILD_MODNAME
, codec
)) {
2023 dev_err(codec
->card
->dev
, "Unable to request irq.\n");
2026 codec
->irq
= codec
->pci
->irq
;
2027 codec
->card
->sync_irq
= codec
->irq
;
2028 dev_dbg(codec
->card
->dev
, "resources allocated.\n");
2031 static int snd_ali_dev_free(struct snd_device
*device
)
2033 struct snd_ali
*codec
= device
->device_data
;
2034 snd_ali_free(codec
);
2038 static int snd_ali_create(struct snd_card
*card
,
2039 struct pci_dev
*pci
,
2042 struct snd_ali
**r_ali
)
2044 struct snd_ali
*codec
;
2046 unsigned short cmdw
;
2047 static const struct snd_device_ops ops
= {
2048 .dev_free
= snd_ali_dev_free
,
2053 dev_dbg(card
->dev
, "creating ...\n");
2055 /* enable PCI device */
2056 err
= pci_enable_device(pci
);
2059 /* check, if we can restrict PCI DMA transfers to 31 bits */
2060 if (dma_set_mask(&pci
->dev
, DMA_BIT_MASK(31)) < 0 ||
2061 dma_set_coherent_mask(&pci
->dev
, DMA_BIT_MASK(31)) < 0) {
2063 "architecture does not support 31bit PCI busmaster DMA\n");
2064 pci_disable_device(pci
);
2068 codec
= kzalloc(sizeof(*codec
), GFP_KERNEL
);
2070 pci_disable_device(pci
);
2074 spin_lock_init(&codec
->reg_lock
);
2075 spin_lock_init(&codec
->voice_alloc
);
2080 codec
->revision
= pci
->revision
;
2081 codec
->spdif_support
= spdif_support
;
2083 if (pcm_streams
< 1)
2085 if (pcm_streams
> 32)
2088 pci_set_master(pci
);
2089 pci_read_config_word(pci
, PCI_COMMAND
, &cmdw
);
2090 if ((cmdw
& PCI_COMMAND_IO
) != PCI_COMMAND_IO
) {
2091 cmdw
|= PCI_COMMAND_IO
;
2092 pci_write_config_word(pci
, PCI_COMMAND
, cmdw
);
2094 pci_set_master(pci
);
2096 if (snd_ali_resources(codec
)) {
2097 snd_ali_free(codec
);
2101 codec
->synth
.chmap
= 0;
2102 codec
->synth
.chcnt
= 0;
2103 codec
->spdif_mask
= 0;
2104 codec
->synth
.synthcount
= 0;
2106 if (codec
->revision
== ALI_5451_V02
)
2107 codec
->chregs
.regs
.ac97read
= ALI_AC97_WRITE
;
2109 codec
->chregs
.regs
.ac97read
= ALI_AC97_READ
;
2110 codec
->chregs
.regs
.ac97write
= ALI_AC97_WRITE
;
2112 codec
->chregs
.regs
.start
= ALI_START
;
2113 codec
->chregs
.regs
.stop
= ALI_STOP
;
2114 codec
->chregs
.regs
.aint
= ALI_AINT
;
2115 codec
->chregs
.regs
.ainten
= ALI_AINTEN
;
2117 codec
->chregs
.data
.start
= 0x00;
2118 codec
->chregs
.data
.stop
= 0x00;
2119 codec
->chregs
.data
.aint
= 0x00;
2120 codec
->chregs
.data
.ainten
= 0x00;
2122 /* M1533: southbridge */
2123 codec
->pci_m1533
= pci_get_device(0x10b9, 0x1533, NULL
);
2124 if (!codec
->pci_m1533
) {
2125 dev_err(card
->dev
, "cannot find ALi 1533 chip.\n");
2126 snd_ali_free(codec
);
2129 /* M7101: power management */
2130 codec
->pci_m7101
= pci_get_device(0x10b9, 0x7101, NULL
);
2131 if (!codec
->pci_m7101
&& codec
->revision
== ALI_5451_V02
) {
2132 dev_err(card
->dev
, "cannot find ALi 7101 chip.\n");
2133 snd_ali_free(codec
);
2137 dev_dbg(card
->dev
, "snd_device_new is called.\n");
2138 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, codec
, &ops
);
2140 snd_ali_free(codec
);
2144 /* initialise synth voices*/
2145 for (i
= 0; i
< ALI_CHANNELS
; i
++)
2146 codec
->synth
.voices
[i
].number
= i
;
2148 err
= snd_ali_chip_init(codec
);
2150 dev_err(card
->dev
, "ali create: chip init error.\n");
2154 #ifdef CONFIG_PM_SLEEP
2155 codec
->image
= kmalloc(sizeof(*codec
->image
), GFP_KERNEL
);
2157 dev_warn(card
->dev
, "can't allocate apm buffer\n");
2160 snd_ali_enable_address_interrupt(codec
);
2161 codec
->hw_initialized
= 1;
2164 dev_dbg(card
->dev
, "created.\n");
2168 static int snd_ali_probe(struct pci_dev
*pci
,
2169 const struct pci_device_id
*pci_id
)
2171 struct snd_card
*card
;
2172 struct snd_ali
*codec
;
2175 dev_dbg(&pci
->dev
, "probe ...\n");
2177 err
= snd_card_new(&pci
->dev
, index
, id
, THIS_MODULE
, 0, &card
);
2181 err
= snd_ali_create(card
, pci
, pcm_channels
, spdif
, &codec
);
2184 card
->private_data
= codec
;
2186 dev_dbg(&pci
->dev
, "mixer building ...\n");
2187 err
= snd_ali_mixer(codec
);
2191 dev_dbg(&pci
->dev
, "pcm building ...\n");
2192 err
= snd_ali_build_pcms(codec
);
2196 snd_ali_proc_init(codec
);
2198 strcpy(card
->driver
, "ALI5451");
2199 strcpy(card
->shortname
, "ALI 5451");
2201 sprintf(card
->longname
, "%s at 0x%lx, irq %i",
2202 card
->shortname
, codec
->port
, codec
->irq
);
2204 dev_dbg(&pci
->dev
, "register card.\n");
2205 err
= snd_card_register(card
);
2209 pci_set_drvdata(pci
, card
);
2213 snd_card_free(card
);
2217 static void snd_ali_remove(struct pci_dev
*pci
)
2219 snd_card_free(pci_get_drvdata(pci
));
2222 static struct pci_driver ali5451_driver
= {
2223 .name
= KBUILD_MODNAME
,
2224 .id_table
= snd_ali_ids
,
2225 .probe
= snd_ali_probe
,
2226 .remove
= snd_ali_remove
,
2232 module_pci_driver(ali5451_driver
);