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

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

initial import

File size: 68.1 KB
Line 
1/*
2 * Matt Wu <Matt_Wu@acersoftech.com.cn>
3 * Apr 26, 2001
4 * Routines for control of ALi pci audio M5451
5 *
6 * BUGS:
7 * --
8 *
9 * TODO:
10 * --
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public Lcodecnse as published by
14 * the Free Software Foundation; either version 2 of the Lcodecnse, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public Lcodecnse for more details.
21 *
22 * You should have received a copy of the GNU General Public Lcodecnse
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28#define __SNDRV_OSS_COMPAT__
29#define SNDRV_MAIN_OBJECT_FILE
30
31#include "sound/driver.h"
32#include "sound/pcm.h"
33#include "sound/info.h"
34#include "sound/ac97_codec.h"
35#include "sound/mpu401.h"
36#define SNDRV_GET_ID
37#include "sound/initval.h"
38
39MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
40MODULE_DESCRIPTION("ALI M5451");
41MODULE_LICENSE("GPL");
42MODULE_CLASSES("{sound}");
43MODULE_DEVICES("{{ALI,M5451,pci},{ALI,M5451}}");
44
45static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
46static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
47static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
48#ifdef TARGET_OS2
49static int pcm_channels[SNDRV_CARDS] = {REPEAT_SNDRV(32)};
50#else
51static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 32};
52#endif
53static int spdif[SNDRV_CARDS] = {REPEAT_SNDRV(0)};
54
55MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
56MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
57MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
58MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
59MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
60MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
61MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
62MODULE_PARM_DESC(enable, "Enable ALI 5451 PCI Audio.");
63MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
64MODULE_PARM(pcm_channels, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
65MODULE_PARM_DESC(pcm_channels, "PCM Channels");
66MODULE_PARM_SYNTAX(pcm_channels, SNDRV_ENABLED ",default:32,allows:{{1,32}}");
67MODULE_PARM(spdif, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
68MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
69MODULE_PARM_SYNTAX(spdif, SNDRV_ENABLED "," SNDRV_BOOLEAN_FALSE_DESC);
70
71/*
72 * Debug part definitions
73 */
74
75//#define ALI_DEBUG
76
77#ifdef TARGET_OS2
78#ifdef ALI_DEBUG
79#define snd_ali_printk printk
80#else
81#define snd_ali_printk 1 ? (void)0 : (void)((int (*)(char *, ...)) NULL)
82#endif
83#else
84#ifdef ALI_DEBUG
85#define snd_ali_printk(a...) printk(a);
86#else
87#define snd_ali_printk(a...)
88#endif
89#endif
90
91/*
92 * Constants definition
93 */
94
95#ifndef PCI_VENDOR_ID_ALI
96#define PCI_VENDOR_ID_ALI 0x10b9
97#endif
98
99#ifndef PCI_DEVICE_ID_ALI_5451
100#define PCI_DEVICE_ID_ALI_5451 0x5451
101#endif
102
103#define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_ALI<<16)|PCI_DEVICE_ID_ALI_5451)
104
105
106#define ALI_CHANNELS 32
107
108#define ALI_PCM_IN_CHANNEL 31
109#define ALI_SPDIF_IN_CHANNEL 19
110#define ALI_SPDIF_OUT_CHANNEL 15
111#define ALI_CENTER_CHANNEL 24
112#define ALI_LEF_CHANNEL 23
113#define ALI_SURR_LEFT_CHANNEL 26
114#define ALI_SURR_RIGHT_CHANNEL 25
115
116#define SNDRV_ALI_VOICE_TYPE_PCM 01
117#define SNDRV_ALI_VOICE_TYPE_OTH 02
118
119#define ALI_5451_V02 0x02
120
121/*
122 * Direct Registers
123 */
124
125#define ALI_LEGACY_DMAR0 0x00 // ADR0
126#define ALI_LEGACY_DMAR4 0x04 // CNT0
127#define ALI_LEGACY_DMAR11 0x0b // MOD
128#define ALI_LEGACY_DMAR15 0x0f // MMR
129#define ALI_MPUR0 0x20
130#define ALI_MPUR1 0x21
131#define ALI_MPUR2 0x22
132#define ALI_MPUR3 0x23
133
134#define ALI_AC97_WRITE 0x40
135#define ALI_AC97_READ 0x44
136
137#define ALI_SCTRL 0x48
138#define ALI_SPDIF_OUT_ENABLE 0x20
139#define ALI_AC97_GPIO 0x4c
140#define ALI_SPDIF_CS 0x70
141#define ALI_SPDIF_CTRL 0x74
142#define ALI_SPDIF_IN_FUNC_ENABLE 0x02
143#define ALI_SPDIF_IN_CH_STATUS 0x40
144#define ALI_SPDIF_OUT_CH_STATUS 0xbf
145#define ALI_START 0x80
146#define ALI_STOP 0x84
147#define ALI_CSPF 0x90
148#define ALI_AINT 0x98
149#define ALI_GC_CIR 0xa0
150#define ENDLP_IE 0x00001000
151#define MIDLP_IE 0x00002000
152#define ALI_AINTEN 0xa4
153#define ALI_VOLUME 0xa8
154#define ALI_SBDELTA_DELTA_R 0xac
155#define ALI_MISCINT 0xb0
156#define ADDRESS_IRQ 0x00000020
157#define TARGET_REACHED 0x00008000
158#define MIXER_OVERFLOW 0x00000800
159#define MIXER_UNDERFLOW 0x00000400
160#define ALI_SBBL_SBCL 0xc0
161#define ALI_SBCTRL_SBE2R_SBDD 0xc4
162#define ALI_STIMER 0xc8
163#define ALI_GLOBAL_CONTROL 0xd4
164#define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
165#define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
166#define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
167#define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
168#define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
169
170#define ALI_CSO_ALPHA_FMS 0xe0
171#define ALI_LBA 0xe4
172#define ALI_ESO_DELTA 0xe8
173#define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
174#define ALI_EBUF1 0xf4
175#define ALI_EBUF2 0xf8
176
177#define ALI_REG(codec, x) ((codec)->port + x)
178
179typedef struct snd_stru_ali ali_t;
180typedef struct snd_ali_stru_voice snd_ali_voice_t;
181
182typedef struct snd_ali_channel_control {
183 // register data
184 struct REGDATA {
185 unsigned int start;
186 unsigned int stop;
187 unsigned int aint;
188 unsigned int ainten;
189 } data;
190
191 // register addresses
192 struct REGS {
193 unsigned int start;
194 unsigned int stop;
195 unsigned int aint;
196 unsigned int ainten;
197 unsigned int ac97read;
198 unsigned int ac97write;
199 } regs;
200
201} snd_ali_channel_control_t;
202
203struct snd_ali_stru_voice {
204 unsigned int number;
205 int use: 1,
206 pcm: 1,
207 midi: 1,
208 mode: 1,
209 synth: 1;
210
211 /* PCM data */
212 ali_t *codec;
213 snd_pcm_substream_t *substream;
214 snd_ali_voice_t *extra;
215
216 int running: 1;
217
218 int eso; /* final ESO value for channel */
219 int count; /* runtime->period_size */
220
221 /* --- */
222
223 void *private_data;
224 void (*private_free)(void *private_data);
225};
226
227
228typedef struct snd_stru_alidev {
229
230 snd_ali_voice_t voices[ALI_CHANNELS];
231
232 unsigned int chcnt; /* num of opened channels */
233 unsigned int chmap; /* bitmap for opened channels */
234 unsigned int synthcount;
235
236} alidev_t;
237
238
239#ifdef CONFIG_PM
240#define ALI_GLOBAL_REGS 56
241#define ALI_CHANNEL_REGS 8
242typedef struct snd_ali_image {
243 unsigned long regs[ALI_GLOBAL_REGS];
244 unsigned long channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
245} ali_image_t;
246#endif
247
248
249struct snd_stru_ali {
250 unsigned long irq;
251 unsigned long port;
252 unsigned char revision;
253
254 unsigned int hw_initialized: 1;
255 unsigned int spdif_support: 1;
256
257 struct pci_dev *pci;
258 struct pci_dev *pci_m1533;
259 struct pci_dev *pci_m7101;
260
261 snd_card_t *card;
262 snd_pcm_t *pcm;
263 alidev_t synth;
264 snd_ali_channel_control_t chregs;
265
266 /* S/PDIF Mask */
267 unsigned int spdif_mask;
268
269 unsigned int spurious_irq_count;
270 unsigned int spurious_irq_max_delta;
271
272 ac97_bus_t *ac97_bus;
273 ac97_t *ac97;
274 unsigned short ac97_ext_id;
275 unsigned short ac97_ext_status;
276
277 spinlock_t reg_lock;
278 spinlock_t voice_alloc;
279
280#ifdef CONFIG_PM
281 ali_image_t *image;
282#endif
283};
284
285static struct pci_device_id snd_ali_ids[] = {
286 {0x10b9, 0x5451, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
287 {0 }
288};
289
290MODULE_DEVICE_TABLE(pci, snd_ali_ids);
291
292static void snd_ali_clear_voices(ali_t *, unsigned int, unsigned int);
293static unsigned short snd_ali_codec_peek(ali_t *, int, unsigned short);
294static void snd_ali_codec_poke(ali_t *, int, unsigned short, unsigned short);
295
296/*
297 * Debug Part
298 */
299
300#ifdef ALI_DEBUG
301
302static void ali_read_regs(ali_t *codec, int channel)
303{
304 int i,j;
305 unsigned int dwVal;
306
307 printk("channel %d registers map:\n", channel);
308 outb((unsigned char)(channel & 0x001f), ALI_REG(codec,ALI_GC_CIR));
309
310 printk(" ");
311 for(j=0;j<8;j++)
312 printk("%2.2x ", j*4);
313 printk("\n");
314
315 for (i=0; i<=0xf8/4;i++) {
316 if(i%8 == 0)
317 printk("%2.2x ", (i*4/0x10)*0x10);
318 dwVal = inl(ALI_REG(codec,i*4));
319 printk("%8.8x ", dwVal);
320 if ((i+1)%8 == 0)
321 printk("\n");
322 }
323 printk("\n");
324}
325static void ali_read_cfg(unsigned int vendor, unsigned deviceid)
326{
327 unsigned int dwVal;
328 struct pci_dev *pci_dev = NULL;
329 int i,j;
330
331
332 pci_dev = pci_find_device(vendor, deviceid, pci_dev);
333 if (pci_dev == NULL)
334 return ;
335
336 printk("\nM%x PCI CFG\n", deviceid);
337 printk(" ");
338 for(j=0;j<8;j++)
339 printk("%d ",j);
340 printk("\n");
341
342 for(i=0;i<8;i++) {
343 printk("%d ",i);
344 for(j=0;j<8;j++)
345 {
346 pci_read_config_dword(pci_dev, i*0x20+j*4, &dwVal);
347 printk("%8.8x ", dwVal);
348 }
349 printk("\n");
350 }
351}
352static void ali_read_ac97regs(ali_t *codec, int secondary)
353{
354 unsigned short i,j;
355 unsigned short wVal;
356
357 printk("\ncodec %d registers map:\n", secondary);
358
359 printk(" ");
360 for(j=0;j<8;j++)
361 printk("%2.2x ",j*2);
362 printk("\n");
363
364 for (i=0; i<64;i++) {
365 if(i%8 == 0)
366 printk("%2.2x ", (i/8)*0x10);
367 wVal = snd_ali_codec_peek(codec, secondary, i*2);
368 printk("%4.4x ", wVal);
369 if ((i+1)%8 == 0)
370 printk("\n");
371 }
372 printk("\n");
373}
374
375#endif
376
377/*
378 * AC97 ACCESS
379 */
380
381static inline unsigned int snd_ali_5451_peek(ali_t *codec,
382 unsigned int port )
383{
384 return (unsigned int)inl(ALI_REG(codec, port));
385}
386
387static inline void snd_ali_5451_poke( ali_t *codec,
388 unsigned int port,
389 unsigned int val )
390{
391 outl((unsigned int)val, ALI_REG(codec, port));
392}
393
394static int snd_ali_codec_ready( ali_t *codec,
395 unsigned int port,
396 int sched )
397{
398 unsigned long end_time;
399 unsigned int res;
400
401 end_time = jiffies + 10 * msecs_to_jiffies(250);
402 do {
403 res = snd_ali_5451_peek(codec,port);
404 if (! (res & 0x8000))
405 return 0;
406 if (sched) {
407 set_current_state(TASK_UNINTERRUPTIBLE);
408 schedule_timeout(1);
409 }
410 } while (time_after_eq(end_time, jiffies));
411 snd_ali_5451_poke(codec, port, res & ~0x8000);
412 snd_printdd("ali_codec_ready: codec is not ready.\n ");
413 return -EIO;
414}
415
416static int snd_ali_stimer_ready(ali_t *codec, int sched)
417{
418 unsigned long end_time;
419 unsigned long dwChk1,dwChk2;
420
421 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
422 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
423
424 end_time = jiffies + 10 * msecs_to_jiffies(250);
425 do {
426 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
427 if (dwChk2 != dwChk1)
428 return 0;
429 if (sched) {
430 set_current_state(TASK_UNINTERRUPTIBLE);
431 schedule_timeout(1);
432 }
433 } while (time_after_eq(end_time, jiffies));
434 snd_printk("ali_stimer_read: stimer is not ready.\n");
435 return -EIO;
436}
437
438static void snd_ali_codec_poke(ali_t *codec,int secondary,
439 unsigned short reg,
440 unsigned short val)
441{
442 unsigned int dwVal = 0;
443 unsigned int port = 0;
444
445 if (reg >= 0x80) {
446 snd_printk("ali_codec_poke: reg(%xh) invalid.\n", reg);
447 return;
448 }
449
450 port = codec->chregs.regs.ac97write;
451
452 if (snd_ali_codec_ready(codec, port, 0) < 0)
453 return;
454 if (snd_ali_stimer_ready(codec, 0) < 0)
455 return;
456
457 dwVal = (unsigned int) (reg & 0xff);
458 dwVal |= 0x8000 | (val << 16);
459 if (secondary) dwVal |= 0x0080;
460 if (codec->revision == ALI_5451_V02) dwVal |= 0x0100;
461
462 snd_ali_5451_poke(codec,port,dwVal);
463
464 return ;
465}
466
467static unsigned short snd_ali_codec_peek( ali_t *codec,
468 int secondary,
469 unsigned short reg)
470{
471 unsigned int dwVal = 0;
472 unsigned int port = 0;
473
474 if (reg >= 0x80) {
475 snd_printk("ali_codec_peek: reg(%xh) invalid.\n", reg);
476 return ~0;
477 }
478
479 port = codec->chregs.regs.ac97read;
480
481 if (snd_ali_codec_ready(codec, port, 0) < 0)
482 return ~0;
483 if (snd_ali_stimer_ready(codec, 0) < 0)
484 return ~0;
485
486 dwVal = (unsigned int) (reg & 0xff);
487 dwVal |= 0x8000; /* bit 15*/
488 if (secondary) dwVal |= 0x0080;
489
490 snd_ali_5451_poke(codec, port, dwVal);
491
492 if (snd_ali_stimer_ready(codec, 0) < 0)
493 return ~0;
494 if (snd_ali_codec_ready(codec, port, 0) < 0)
495 return ~0;
496
497 return (snd_ali_5451_peek(codec, port) & 0xffff0000)>>16;
498}
499
500static void snd_ali_codec_write(ac97_t *ac97,
501 unsigned short reg,
502 unsigned short val )
503{
504 ali_t *codec = ac97->private_data;
505
506 snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
507 snd_ali_codec_poke(codec, 0, reg, val);
508 return ;
509}
510
511
512static unsigned short snd_ali_codec_read(ac97_t *ac97, unsigned short reg)
513{
514 ali_t *codec = ac97->private_data;
515
516 snd_ali_printk("codec_read reg=%xh.\n", reg);
517 return (snd_ali_codec_peek(codec, 0, reg));
518}
519
520/*
521 * AC97 Reset
522 */
523
524static int snd_ali_reset_5451(ali_t *codec)
525{
526 struct pci_dev *pci_dev = NULL;
527 unsigned short wCount, wReg;
528 unsigned int dwVal;
529
530 if ((pci_dev = codec->pci_m1533) != NULL) {
531 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
532 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
533 udelay(5000);
534 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
535 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
536 udelay(5000);
537 }
538
539 pci_dev = codec->pci;
540 pci_read_config_dword(pci_dev, 0x44, &dwVal);
541 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
542 udelay(500);
543 pci_read_config_dword(pci_dev, 0x44, &dwVal);
544 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
545 udelay(5000);
546
547 wCount = 200;
548 while(wCount--) {
549 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
550 if((wReg & 0x000f) == 0x000f)
551 return 0;
552 udelay(5000);
553 }
554
555 /* non-fatal if you have a non PM capable codec */
556 /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
557 return 0;
558}
559
560#ifdef CODEC_RESET
561
562static int snd_ali_reset_codec(ali_t *codec)
563{
564 struct pci_dev *pci_dev = NULL;
565 unsigned char bVal = 0;
566 unsigned int dwVal;
567 unsigned short wCount, wReg;
568
569 pci_dev = codec->pci_m1533;
570
571 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
572 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
573 udelay(5000);
574 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
575 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
576 udelay(5000);
577
578 bVal = inb(ALI_REG(codec,ALI_SCTRL));
579 bVal |= 0x02;
580 outb(ALI_REG(codec,ALI_SCTRL),bVal);
581 udelay(5000);
582 bVal = inb(ALI_REG(codec,ALI_SCTRL));
583 bVal &= 0xfd;
584 outb(ALI_REG(codec,ALI_SCTRL),bVal);
585 udelay(15000);
586
587 wCount = 200;
588 while(wCount--) {
589 wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
590 if((wReg & 0x000f) == 0x000f)
591 return 0;
592 udelay(5000);
593 }
594 return -1;
595}
596
597#endif
598
599/*
600 * ALI 5451 Controller
601 */
602
603static void snd_ali_enable_special_channel(ali_t *codec, unsigned int channel)
604{
605 unsigned long dwVal = 0;
606
607 dwVal = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
608 dwVal |= 1 << (channel & 0x0000001f);
609 outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
610}
611
612static void snd_ali_disable_special_channel(ali_t *codec, unsigned int channel)
613{
614 unsigned long dwVal = 0;
615
616 dwVal = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
617 dwVal &= ~(1 << (channel & 0x0000001f));
618 outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
619}
620
621static void snd_ali_enable_address_interrupt(ali_t * codec)
622{
623 unsigned int gc;
624
625 gc = inl(ALI_REG(codec, ALI_GC_CIR));
626 gc |= ENDLP_IE;
627 gc |= MIDLP_IE;
628 outl( gc, ALI_REG(codec, ALI_GC_CIR));
629}
630
631static void snd_ali_disable_address_interrupt(ali_t * codec)
632{
633 unsigned int gc;
634
635 gc = inl(ALI_REG(codec, ALI_GC_CIR));
636 gc &= ~ENDLP_IE;
637 gc &= ~MIDLP_IE;
638 outl(gc, ALI_REG(codec, ALI_GC_CIR));
639}
640
641#if 0 // not used
642static void snd_ali_enable_voice_irq(ali_t *codec, unsigned int channel)
643{
644 unsigned int mask;
645 snd_ali_channel_control_t *pchregs = &(codec->chregs);
646
647 snd_ali_printk("enable_voice_irq channel=%d\n",channel);
648
649 mask = 1 << (channel & 0x1f);
650 pchregs->data.ainten = inl(ALI_REG(codec,pchregs->regs.ainten));
651 pchregs->data.ainten |= mask;
652 outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
653}
654#endif
655
656static void snd_ali_disable_voice_irq(ali_t *codec, unsigned int channel)
657{
658 unsigned int mask;
659 snd_ali_channel_control_t *pchregs = &(codec->chregs);
660
661 snd_ali_printk("disable_voice_irq channel=%d\n",channel);
662
663 mask = 1 << (channel & 0x1f);
664 pchregs->data.ainten = inl(ALI_REG(codec,pchregs->regs.ainten));
665 pchregs->data.ainten &= ~mask;
666 outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
667}
668
669static int snd_ali_alloc_pcm_channel(ali_t *codec, int channel)
670{
671 unsigned int idx = channel & 0x1f;
672
673 if (codec->synth.chcnt >= ALI_CHANNELS){
674 snd_printk("ali_alloc_pcm_channel: no free channels.\n");
675 return -1;
676 }
677
678 if (!(codec->synth.chmap & (1 << idx))) {
679 codec->synth.chmap |= 1 << idx;
680 codec->synth.chcnt++;
681 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
682 return idx;
683 }
684 return -1;
685}
686
687static int snd_ali_find_free_channel(ali_t * codec, int rec)
688{
689 int idx;
690 int result = -1;
691
692 snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
693
694 // recording
695 if (rec) {
696 if (codec->spdif_support &&
697 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_IN_SUPPORT))
698 idx = ALI_SPDIF_IN_CHANNEL;
699 else
700 idx = ALI_PCM_IN_CHANNEL;
701
702 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
703 return result;
704 } else {
705 snd_printk("ali_find_free_channel: record channel is busy now.\n");
706 return -1;
707 }
708 }
709
710 //playback...
711 if (codec->spdif_support &&
712 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)) {
713 idx = ALI_SPDIF_OUT_CHANNEL;
714 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
715 return result;
716 } else {
717 snd_printk("ali_find_free_channel: S/PDIF out channel is in busy now.\n");
718 }
719 }
720
721 for (idx = 0; idx < ALI_CHANNELS; idx++) {
722 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0)
723 return result;
724 }
725 snd_printk("ali_find_free_channel: no free channels.\n");
726 return -1;
727}
728
729static void snd_ali_free_channel_pcm(ali_t *codec, int channel)
730{
731 unsigned int idx = channel & 0x0000001f;
732
733 snd_ali_printk("free_channel_pcm channel=%d\n",channel);
734
735 if (channel < 0 || channel >= ALI_CHANNELS)
736 return;
737
738 if (!(codec->synth.chmap & (1 << idx))) {
739 snd_printk("ali_free_channel_pcm: channel %d is not in use.\n",channel);
740 return;
741 } else {
742 codec->synth.chmap &= ~(1 << idx);
743 codec->synth.chcnt--;
744 }
745}
746
747#if 0 // not used
748static void snd_ali_start_voice(ali_t * codec, unsigned int channel)
749{
750 unsigned int mask = 1 << (channel & 0x1f);
751
752 snd_ali_printk("start_voice: channel=%d\n",channel);
753 outl(mask, ALI_REG(codec,codec->chregs.regs.start));
754}
755#endif
756
757static void snd_ali_stop_voice(ali_t * codec, unsigned int channel)
758{
759 unsigned int mask = 1 << (channel & 0x1f);
760
761 snd_ali_printk("stop_voice: channel=%d\n",channel);
762 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
763}
764
765/*
766 * S/PDIF Part
767 */
768
769static void snd_ali_delay(ali_t *codec,int interval)
770{
771 unsigned long begintimer,currenttimer;
772
773 begintimer = inl(ALI_REG(codec, ALI_STIMER));
774 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
775
776 while (currenttimer < begintimer + interval) {
777 if(snd_ali_stimer_ready(codec, 1) < 0)
778 break;
779 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
780 }
781}
782
783static void snd_ali_detect_spdif_rate(ali_t *codec)
784{
785 u16 wval = 0;
786 u16 count = 0;
787 u8 bval = 0, R1 = 0, R2 = 0;
788
789 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
790 bval |= 0x1F;
791 outb(bval,ALI_REG(codec,ALI_SPDIF_CTRL + 1));
792
793 while (((R1 < 0x0B )||(R1 > 0x0E)) && (R1 != 0x12) && count <= 50000) {
794 count ++;
795 snd_ali_delay(codec, 6);
796 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
797 R1 = bval & 0x1F;
798 }
799
800 if (count > 50000) {
801 snd_printk("ali_detect_spdif_rate: timeout!\n");
802 return;
803 }
804
805 count = 0;
806 while (count++ <= 50000) {
807 snd_ali_delay(codec, 6);
808 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
809 R2 = bval & 0x1F;
810 if (R2 != R1) R1 = R2; else break;
811 }
812
813 if (count > 50000) {
814 snd_printk("ali_detect_spdif_rate: timeout!\n");
815 return;
816 }
817
818 if (R2 >= 0x0b && R2 <= 0x0e) {
819 wval = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
820 wval &= 0xE0F0;
821 wval |= (u16)0x09 << 8 | (u16)0x05;
822 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
823
824 bval = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
825 outb(bval|0x02,ALI_REG(codec,ALI_SPDIF_CS + 3));
826 } else if (R2 == 0x12) {
827 wval = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
828 wval &= 0xE0F0;
829 wval |= (u16)0x0E << 8 | (u16)0x08;
830 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
831
832 bval = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
833 outb(bval|0x03,ALI_REG(codec,ALI_SPDIF_CS + 3));
834 }
835}
836
837static unsigned int snd_ali_get_spdif_in_rate(ali_t *codec)
838{
839 u32 dwRate = 0;
840 u8 bval = 0;
841
842 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
843 bval &= 0x7F;
844 bval |= 0x40;
845 outb(bval, ALI_REG(codec,ALI_SPDIF_CTRL));
846
847 snd_ali_detect_spdif_rate(codec);
848
849 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3));
850 bval &= 0x0F;
851
852 if (bval == 0) dwRate = 44100;
853 if (bval == 1) dwRate = 48000;
854 if (bval == 2) dwRate = 32000;
855
856 return dwRate;
857}
858
859static void snd_ali_enable_spdif_in(ali_t *codec)
860{
861 unsigned int dwVal;
862
863 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
864 dwVal |= ALI_SPDIF_IN_SUPPORT;
865 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
866
867 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
868 dwVal |= 0x02;
869 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
870
871 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
872}
873
874static void snd_ali_disable_spdif_in(ali_t *codec)
875{
876 unsigned int dwVal;
877
878 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
879 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
880 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
881
882 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
883}
884
885
886static void snd_ali_set_spdif_out_rate(ali_t *codec, unsigned int rate)
887{
888 unsigned char bVal;
889 unsigned int dwRate = 0;
890
891 if (rate == 32000) dwRate = 0x300;
892 if (rate == 44100) dwRate = 0;
893 if (rate == 48000) dwRate = 0x200;
894
895 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
896 bVal &= (unsigned char)(~(1<<6));
897
898 bVal |= 0x80; //select right
899 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
900 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
901
902 bVal &= (~0x80); //select left
903 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
904 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
905}
906
907static void snd_ali_enable_spdif_out(ali_t *codec)
908{
909 unsigned short wVal;
910 unsigned char bVal;
911
912 struct pci_dev *pci_dev = NULL;
913
914 pci_dev = codec->pci_m1533;
915 if (pci_dev == NULL)
916 return;
917 pci_read_config_byte(pci_dev, 0x61, &bVal);
918 bVal |= 0x40;
919 pci_write_config_byte(pci_dev, 0x61, bVal);
920 pci_read_config_byte(pci_dev, 0x7d, &bVal);
921 bVal |= 0x01;
922 pci_write_config_byte(pci_dev, 0x7d, bVal);
923
924 pci_read_config_byte(pci_dev, 0x7e, &bVal);
925 bVal &= (~0x20);
926 bVal |= 0x10;
927 pci_write_config_byte(pci_dev, 0x7e, bVal);
928
929 bVal = inb(ALI_REG(codec, ALI_SCTRL));
930 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
931
932 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
933 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
934
935 {
936 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
937 wVal |= ALI_SPDIF_OUT_SEL_PCM;
938 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
939 snd_ali_disable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
940 }
941}
942
943static void snd_ali_enable_spdif_chnout(ali_t *codec)
944{
945 unsigned short wVal = 0;
946
947 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
948 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
949 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
950 /*
951 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
952 if (flag & ALI_SPDIF_OUT_NON_PCM)
953 wVal |= 0x0002;
954 else
955 wVal &= (~0x0002);
956 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
957 */
958 snd_ali_enable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
959}
960
961static void snd_ali_disable_spdif_chnout(ali_t *codec)
962{
963 unsigned short wVal = 0;
964 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
965 wVal |= ALI_SPDIF_OUT_SEL_PCM;
966 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
967
968 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
969}
970
971static void snd_ali_disable_spdif_out(ali_t *codec)
972{
973 unsigned char bVal;
974
975 bVal = inb(ALI_REG(codec, ALI_SCTRL));
976 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
977
978 snd_ali_disable_spdif_chnout(codec);
979}
980
981static void snd_ali_update_ptr(ali_t *codec,int channel)
982{
983 snd_ali_voice_t *pvoice = NULL;
984 snd_pcm_runtime_t *runtime;
985 snd_ali_channel_control_t *pchregs = NULL;
986 unsigned int old, mask;
987#ifdef ALI_DEBUG
988 unsigned int temp, cspf;
989#endif
990
991 pchregs = &(codec->chregs);
992
993 // check if interrupt occurred for channel
994 old = pchregs->data.aint;
995 mask = ((unsigned int) 1L) << (channel & 0x1f);
996
997 if (!(old & mask))
998 return;
999
1000 pvoice = &codec->synth.voices[channel];
1001 runtime = pvoice->substream->runtime;
1002
1003 udelay(100);
1004 spin_lock(&codec->reg_lock);
1005
1006 if (pvoice->pcm && pvoice->substream) {
1007 /* pcm interrupt */
1008#ifdef ALI_DEBUG
1009 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
1010 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1011 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
1012#endif
1013 if (pvoice->running) {
1014#ifndef TARGET_OS2
1015 snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",(u16)temp,cspf);
1016#endif
1017 spin_unlock(&codec->reg_lock);
1018 snd_pcm_period_elapsed(pvoice->substream);
1019 spin_lock(&codec->reg_lock);
1020 } else {
1021 snd_ali_stop_voice(codec, channel);
1022 snd_ali_disable_voice_irq(codec, channel);
1023 }
1024 } else if (codec->synth.voices[channel].synth) {
1025 /* synth interrupt */
1026 } else if (codec->synth.voices[channel].midi) {
1027 /* midi interrupt */
1028 } else {
1029 /* unknown interrupt */
1030 snd_ali_stop_voice(codec, channel);
1031 snd_ali_disable_voice_irq(codec, channel);
1032 }
1033 spin_unlock(&codec->reg_lock);
1034 outl(mask,ALI_REG(codec,pchregs->regs.aint));
1035 pchregs->data.aint = old & (~mask);
1036}
1037
1038static void snd_ali_interrupt(ali_t * codec)
1039{
1040 int channel;
1041 unsigned int audio_int;
1042 snd_ali_channel_control_t *pchregs = NULL;
1043 pchregs = &(codec->chregs);
1044
1045 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
1046 if (audio_int & ADDRESS_IRQ) {
1047 // get interrupt status for all channels
1048 pchregs->data.aint = inl(ALI_REG(codec,pchregs->regs.aint));
1049 for (channel = 0; channel < ALI_CHANNELS; channel++) {
1050 snd_ali_update_ptr(codec, channel);
1051 }
1052 }
1053 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
1054 ALI_REG(codec,ALI_MISCINT));
1055}
1056
1057
1058static irqreturn_t snd_ali_card_interrupt(int irq,
1059 void *dev_id,
1060 struct pt_regs *regs)
1061{
1062 ali_t *codec = dev_id;
1063#ifdef TARGET_OS2
1064 int fOurIrq = FALSE;
1065#endif
1066
1067 if (codec == NULL)
1068 return IRQ_NONE;
1069#ifdef TARGET_OS2
1070 fOurIrq = TRUE;
1071#endif
1072 snd_ali_interrupt(codec);
1073#ifdef TARGET_OS2
1074 if (fOurIrq) {
1075 eoi_irq(irq);
1076 }
1077#endif //TARGET_OS2
1078
1079 return IRQ_HANDLED;
1080}
1081
1082
1083static snd_ali_voice_t *snd_ali_alloc_voice(ali_t * codec, int type, int rec)
1084{
1085 snd_ali_voice_t *pvoice = NULL;
1086 unsigned long flags;
1087 int idx;
1088
1089 snd_ali_printk("alloc_voice: type=%d rec=%d\n",type,rec);
1090
1091 spin_lock_irqsave(&codec->voice_alloc, flags);
1092 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
1093 idx = snd_ali_find_free_channel(codec,rec);
1094 if(idx < 0) {
1095 snd_printk("ali_alloc_voice: err.\n");
1096 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1097 return NULL;
1098 }
1099 pvoice = &(codec->synth.voices[idx]);
1100 pvoice->use = 1;
1101 pvoice->pcm = 1;
1102 pvoice->mode = rec;
1103 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1104 return pvoice;
1105 }
1106 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1107 return NULL;
1108}
1109
1110
1111static void snd_ali_free_voice(ali_t * codec, snd_ali_voice_t *pvoice)
1112{
1113 unsigned long flags;
1114 void (*private_free)(void *);
1115 void *private_data;
1116
1117 snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
1118 if (pvoice == NULL || !pvoice->use)
1119 return;
1120 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
1121 spin_lock_irqsave(&codec->voice_alloc, flags);
1122 private_free = pvoice->private_free;
1123 private_data = pvoice->private_data;
1124 pvoice->private_free = NULL;
1125 pvoice->private_data = NULL;
1126 if (pvoice->pcm) {
1127 snd_ali_free_channel_pcm(codec, pvoice->number);
1128 }
1129 pvoice->use = pvoice->pcm = pvoice->synth = 0;
1130 pvoice->substream = NULL;
1131 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1132 if (private_free)
1133 private_free(private_data);
1134}
1135
1136
1137static void snd_ali_clear_voices(ali_t * codec,
1138 unsigned int v_min,
1139 unsigned int v_max)
1140{
1141 unsigned int i;
1142
1143 for (i = v_min; i <= v_max; i++) {
1144 snd_ali_stop_voice(codec, i);
1145 snd_ali_disable_voice_irq(codec, i);
1146 }
1147}
1148
1149static void snd_ali_write_voice_regs(ali_t * codec,
1150 unsigned int Channel,
1151 unsigned int LBA,
1152 unsigned int CSO,
1153 unsigned int ESO,
1154 unsigned int DELTA,
1155 unsigned int ALPHA_FMS,
1156 unsigned int GVSEL,
1157 unsigned int PAN,
1158 unsigned int VOL,
1159 unsigned int CTRL,
1160 unsigned int EC)
1161{
1162 unsigned int ctlcmds[4];
1163
1164 outb((unsigned char)(Channel & 0x001f),ALI_REG(codec,ALI_GC_CIR));
1165
1166 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1167 ctlcmds[1] = LBA;
1168 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1169 ctlcmds[3] = (GVSEL << 31) |
1170 ((PAN & 0x0000007f) << 24) |
1171 ((VOL & 0x000000ff) << 16) |
1172 ((CTRL & 0x0000000f) << 12) |
1173 (EC & 0x00000fff);
1174
1175 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1176
1177 outl(ctlcmds[0], ALI_REG(codec,ALI_CSO_ALPHA_FMS));
1178 outl(ctlcmds[1], ALI_REG(codec,ALI_LBA));
1179 outl(ctlcmds[2], ALI_REG(codec,ALI_ESO_DELTA));
1180 outl(ctlcmds[3], ALI_REG(codec,ALI_GVSEL_PAN_VOC_CTRL_EC));
1181
1182 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1183 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1184}
1185
1186static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1187{
1188 unsigned int delta;
1189
1190 if (rate < 4000) rate = 4000;
1191 if (rate > 48000) rate = 48000;
1192
1193 if (rec) {
1194 if (rate == 44100)
1195 delta = 0x116a;
1196 else if (rate == 8000)
1197 delta = 0x6000;
1198 else if (rate == 48000)
1199 delta = 0x1000;
1200 else
1201 delta = ((48000 << 12) / rate) & 0x0000ffff;
1202 } else {
1203 if (rate == 44100)
1204 delta = 0xeb3;
1205 else if (rate == 8000)
1206 delta = 0x2ab;
1207 else if (rate == 48000)
1208 delta = 0x1000;
1209 else
1210 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1211 }
1212
1213 return delta;
1214}
1215
1216static unsigned int snd_ali_control_mode(snd_pcm_substream_t *substream)
1217{
1218 unsigned int CTRL;
1219 snd_pcm_runtime_t *runtime = substream->runtime;
1220
1221 /* set ctrl mode
1222 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1223 */
1224 CTRL = 0x00000001;
1225 if (snd_pcm_format_width(runtime->format) == 16)
1226 CTRL |= 0x00000008; // 16-bit data
1227 if (!snd_pcm_format_unsigned(runtime->format))
1228 CTRL |= 0x00000002; // signed data
1229 if (runtime->channels > 1)
1230 CTRL |= 0x00000004; // stereo data
1231 return CTRL;
1232}
1233
1234/*
1235 * PCM part
1236 */
1237
1238static int snd_ali_ioctl(snd_pcm_substream_t * substream,
1239 unsigned int cmd, void *arg)
1240{
1241 return snd_pcm_lib_ioctl(substream, cmd, arg);
1242}
1243
1244static int snd_ali_trigger(snd_pcm_substream_t *substream,
1245 int cmd)
1246
1247{
1248 ali_t *codec = snd_pcm_substream_chip(substream);
1249 struct list_head *pos;
1250 snd_pcm_substream_t *s;
1251 unsigned int what, whati, capture_flag;
1252 snd_ali_voice_t *pvoice = NULL, *evoice = NULL;
1253 unsigned int val;
1254 int do_start;
1255
1256 switch (cmd) {
1257 case SNDRV_PCM_TRIGGER_START:
1258 case SNDRV_PCM_TRIGGER_RESUME:
1259 do_start = 1; break;
1260 case SNDRV_PCM_TRIGGER_STOP:
1261 case SNDRV_PCM_TRIGGER_SUSPEND:
1262 do_start = 0; break;
1263 default:
1264 return -EINVAL;
1265 }
1266
1267 what = whati = capture_flag = 0;
1268 snd_pcm_group_for_each(pos, substream) {
1269 s = snd_pcm_group_substream_entry(pos);
1270 if ((ali_t *) snd_pcm_substream_chip(s) == codec) {
1271 pvoice = (snd_ali_voice_t *) s->runtime->private_data;
1272 evoice = pvoice->extra;
1273 what |= 1 << (pvoice->number & 0x1f);
1274 if (evoice == NULL) {
1275 whati |= 1 << (pvoice->number & 0x1f);
1276 } else {
1277 whati |= 1 << (evoice->number & 0x1f);
1278 what |= 1 << (evoice->number & 0x1f);
1279 }
1280 if (do_start) {
1281 pvoice->running = 1;
1282 if (evoice != NULL)
1283 evoice->running = 1;
1284 } else {
1285 pvoice->running = 0;
1286 if (evoice != NULL)
1287 evoice->running = 0;
1288 }
1289 snd_pcm_trigger_done(s, substream);
1290 if (pvoice->mode)
1291 capture_flag = 1;
1292 }
1293 }
1294 spin_lock(&codec->reg_lock);
1295 if (! do_start) {
1296 outl(what, ALI_REG(codec, ALI_STOP));
1297 }
1298 val = inl(ALI_REG(codec, ALI_AINTEN));
1299 if (do_start) {
1300 val |= whati;
1301 } else {
1302 val &= ~whati;
1303 }
1304 outl(val, ALI_REG(codec, ALI_AINTEN));
1305 if (do_start) {
1306 outl(what, ALI_REG(codec, ALI_START));
1307 }
1308 snd_ali_printk("trigger: what=%xh whati=%xh\n",what,whati);
1309 spin_unlock(&codec->reg_lock);
1310
1311 return 0;
1312}
1313
1314static int snd_ali_playback_hw_params(snd_pcm_substream_t * substream,
1315 snd_pcm_hw_params_t * hw_params)
1316{
1317 ali_t *codec = snd_pcm_substream_chip(substream);
1318 snd_pcm_runtime_t *runtime = substream->runtime;
1319 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1320 snd_ali_voice_t *evoice = pvoice->extra;
1321 int err;
1322 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1323 if (err < 0) return err;
1324
1325 /* voice management */
1326
1327 if (params_buffer_size(hw_params)/2 != params_period_size(hw_params)) {
1328 if (evoice == NULL) {
1329 evoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0);
1330 if (evoice == NULL)
1331 return -ENOMEM;
1332 pvoice->extra = evoice;
1333 evoice->substream = substream;
1334 }
1335 } else {
1336 if (evoice != NULL) {
1337 snd_ali_free_voice(codec, evoice);
1338 pvoice->extra = evoice = NULL;
1339 }
1340 }
1341
1342 return 0;
1343}
1344
1345static int snd_ali_playback_hw_free(snd_pcm_substream_t * substream)
1346{
1347 ali_t *codec = snd_pcm_substream_chip(substream);
1348 snd_pcm_runtime_t *runtime = substream->runtime;
1349 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1350 snd_ali_voice_t *evoice = pvoice ? pvoice->extra : NULL;
1351
1352 snd_pcm_lib_free_pages(substream);
1353 if (evoice != NULL) {
1354 snd_ali_free_voice(codec, evoice);
1355 pvoice->extra = NULL;
1356 }
1357 return 0;
1358}
1359
1360static int snd_ali_capture_hw_params(snd_pcm_substream_t * substream,
1361 snd_pcm_hw_params_t * hw_params)
1362{
1363 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1364}
1365
1366static int snd_ali_capture_hw_free(snd_pcm_substream_t * substream)
1367{
1368 return snd_pcm_lib_free_pages(substream);
1369}
1370
1371static int snd_ali_playback_prepare(snd_pcm_substream_t * substream)
1372{
1373 ali_t *codec = snd_pcm_substream_chip(substream);
1374 snd_pcm_runtime_t *runtime = substream->runtime;
1375 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1376 snd_ali_voice_t *evoice = pvoice->extra;
1377 unsigned long flags;
1378
1379 unsigned int LBA;
1380 unsigned int Delta;
1381 unsigned int ESO;
1382 unsigned int CTRL;
1383 unsigned int GVSEL;
1384 unsigned int PAN;
1385 unsigned int VOL;
1386 unsigned int EC;
1387
1388 snd_ali_printk("playback_prepare ...\n");
1389
1390 spin_lock_irqsave(&codec->reg_lock, flags);
1391
1392 /* set Delta (rate) value */
1393 Delta = snd_ali_convert_rate(runtime->rate, 0);
1394
1395 if ((pvoice->number == ALI_SPDIF_IN_CHANNEL) ||
1396 (pvoice->number == ALI_PCM_IN_CHANNEL))
1397 snd_ali_disable_special_channel(codec, pvoice->number);
1398 else if (codec->spdif_support &&
1399 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)
1400 && (pvoice->number == ALI_SPDIF_OUT_CHANNEL)) {
1401 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1402 Delta = 0x1000;
1403 }
1404
1405 /* set Loop Back Address */
1406 LBA = runtime->dma_addr;
1407
1408 /* set interrupt count size */
1409 pvoice->count = runtime->period_size;
1410
1411 /* set target ESO for channel */
1412 pvoice->eso = runtime->buffer_size;
1413
1414 snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",pvoice->eso,pvoice->count);
1415
1416 /* set ESO to capture first MIDLP interrupt */
1417 ESO = pvoice->eso -1;
1418 /* set ctrl mode */
1419 CTRL = snd_ali_control_mode(substream);
1420
1421 GVSEL = 1;
1422 PAN = 0;
1423 VOL = 0;
1424 EC = 0;
1425 snd_ali_printk("playback_prepare:\n ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1426 snd_ali_write_voice_regs( codec,
1427 pvoice->number,
1428 LBA,
1429 0, /* cso */
1430 ESO,
1431 Delta,
1432 0, /* alpha */
1433 GVSEL,
1434 PAN,
1435 VOL,
1436 CTRL,
1437 EC);
1438 if (evoice != NULL) {
1439 evoice->count = pvoice->count;
1440 evoice->eso = pvoice->count << 1;
1441 ESO = evoice->eso - 1;
1442 snd_ali_write_voice_regs(codec,
1443 evoice->number,
1444 LBA,
1445 0, /* cso */
1446 ESO,
1447 Delta,
1448 0, /* alpha */
1449 GVSEL,
1450 (unsigned int)0x7f,
1451 (unsigned int)0x3ff,
1452 CTRL,
1453 EC);
1454 }
1455 spin_unlock_irqrestore(&codec->reg_lock, flags);
1456 return 0;
1457}
1458
1459
1460static int snd_ali_capture_prepare(snd_pcm_substream_t * substream)
1461{
1462 ali_t *codec = snd_pcm_substream_chip(substream);
1463 snd_pcm_runtime_t *runtime = substream->runtime;
1464 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1465 unsigned long flags;
1466 unsigned int LBA;
1467 unsigned int Delta;
1468 unsigned int ESO;
1469 unsigned int CTRL;
1470 unsigned int GVSEL;
1471 unsigned int PAN;
1472 unsigned int VOL;
1473 unsigned int EC;
1474 u8 bValue;
1475
1476 spin_lock_irqsave(&codec->reg_lock, flags);
1477
1478 snd_ali_printk("capture_prepare...\n");
1479
1480 snd_ali_enable_special_channel(codec,pvoice->number);
1481
1482 Delta = snd_ali_convert_rate(runtime->rate, 1);
1483
1484 // Prepare capture intr channel
1485 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1486
1487 unsigned int rate;
1488
1489 if (codec->revision != ALI_5451_V02) {
1490 spin_unlock_irqrestore(&codec->reg_lock, flags);
1491 return -1;
1492 }
1493 rate = snd_ali_get_spdif_in_rate(codec);
1494 if (rate == 0) {
1495 snd_printk("ali_capture_preapre: spdif rate detect err!\n");
1496 rate = 48000;
1497 }
1498 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1499 if (bValue & 0x10) {
1500 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1501 printk("clear SPDIF parity error flag.\n");
1502 }
1503
1504 if (rate != 48000)
1505 Delta = ((rate << 12)/runtime->rate)&0x00ffff;
1506 }
1507
1508 // set target ESO for channel
1509 pvoice->eso = runtime->buffer_size;
1510
1511 // set interrupt count size
1512 pvoice->count = runtime->period_size;
1513
1514 // set Loop Back Address
1515 LBA = runtime->dma_addr;
1516
1517 // set ESO to capture first MIDLP interrupt
1518 ESO = pvoice->eso - 1;
1519 CTRL = snd_ali_control_mode(substream);
1520 GVSEL = 0;
1521 PAN = 0x00;
1522 VOL = 0x00;
1523 EC = 0;
1524
1525 snd_ali_write_voice_regs( codec,
1526 pvoice->number,
1527 LBA,
1528 0, /* cso */
1529 ESO,
1530 Delta,
1531 0, /* alpha */
1532 GVSEL,
1533 PAN,
1534 VOL,
1535 CTRL,
1536 EC);
1537
1538
1539 spin_unlock_irqrestore(&codec->reg_lock, flags);
1540
1541 return 0;
1542}
1543
1544
1545static snd_pcm_uframes_t snd_ali_playback_pointer(snd_pcm_substream_t *substream)
1546{
1547 ali_t *codec = snd_pcm_substream_chip(substream);
1548 snd_pcm_runtime_t *runtime = substream->runtime;
1549 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1550 unsigned int cso;
1551
1552 spin_lock(&codec->reg_lock);
1553 if (!pvoice->running) {
1554 spin_unlock(&codec->reg_lock);
1555 return 0;
1556 }
1557 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1558 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1559 spin_unlock(&codec->reg_lock);
1560 snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1561
1562 return cso;
1563}
1564
1565
1566static snd_pcm_uframes_t snd_ali_capture_pointer(snd_pcm_substream_t *substream)
1567{
1568 ali_t *codec = snd_pcm_substream_chip(substream);
1569 snd_pcm_runtime_t *runtime = substream->runtime;
1570 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1571 unsigned int cso;
1572 unsigned long flags;
1573
1574 spin_lock_irqsave(&codec->reg_lock, flags);
1575 if (!pvoice->running) {
1576 spin_unlock_irqrestore(&codec->reg_lock, flags);
1577 return 0;
1578 }
1579 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1580 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1581 spin_unlock_irqrestore(&codec->reg_lock, flags);
1582
1583 return cso;
1584}
1585
1586static snd_pcm_hardware_t snd_ali_playback =
1587{
1588 /* info: */ (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1589 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1590 SNDRV_PCM_INFO_MMAP_VALID |
1591 SNDRV_PCM_INFO_RESUME |
1592 SNDRV_PCM_INFO_SYNC_START),
1593 /* formats: */ (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1594 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1595 /* rates: */ SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1596 /* rate_min: */ 4000,
1597 /* rate_max: */ 48000,
1598 /* channels_min: */ 1,
1599 /* channels_max: */ 2,
1600 /* buffer_bytes_max: */ (256*1024),
1601 /* period_bytes_min: */ 64,
1602 /* period_bytes_max: */ (256*1024),
1603 /* periods_min: */ 1,
1604 /* periods_max: */ 1024,
1605 /* fifo_size: */ 0,
1606};
1607
1608/*
1609 * Capture support device description
1610 */
1611
1612static snd_pcm_hardware_t snd_ali_capture =
1613{
1614 /* info: */ (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1615 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1616 SNDRV_PCM_INFO_MMAP_VALID |
1617 SNDRV_PCM_INFO_RESUME |
1618 SNDRV_PCM_INFO_SYNC_START),
1619 /* formats: */ (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1620 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1621 /* rates: */ SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1622 /* rate_min: */ 4000,
1623 /* rate_max: */ 48000,
1624 /* channels_min: */ 1,
1625 /* channels_max: */ 2,
1626 /* buffer_bytes_max: */ (128*1024),
1627 /* period_bytes_min: */ 64,
1628 /* period_bytes_max: */ (128*1024),
1629 /* periods_min: */ 1,
1630 /* periods_max: */ 1024,
1631 /* fifo_size: */ 0,
1632};
1633
1634static void snd_ali_pcm_free_substream(snd_pcm_runtime_t *runtime)
1635{
1636 unsigned long flags;
1637 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1638 ali_t *codec;
1639
1640 if (pvoice) {
1641 codec = pvoice->codec;
1642 spin_lock_irqsave(&codec->reg_lock, flags);
1643 snd_ali_free_voice(pvoice->codec, pvoice);
1644 spin_unlock_irqrestore(&codec->reg_lock, flags);
1645 }
1646}
1647
1648static int snd_ali_playback_open(snd_pcm_substream_t * substream)
1649{
1650 ali_t *codec = snd_pcm_substream_chip(substream);
1651 snd_pcm_runtime_t *runtime = substream->runtime;
1652 snd_ali_voice_t *pvoice;
1653 unsigned long flags = 0;
1654
1655 spin_lock_irqsave(&codec->reg_lock, flags);
1656 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0);
1657 if (pvoice == NULL) {
1658 spin_unlock_irqrestore(&codec->reg_lock, flags);
1659 return -EAGAIN;
1660 }
1661 pvoice->codec = codec;
1662 spin_unlock_irqrestore(&codec->reg_lock, flags);
1663
1664 pvoice->substream = substream;
1665 runtime->private_data = pvoice;
1666 runtime->private_free = snd_ali_pcm_free_substream;
1667
1668 runtime->hw = snd_ali_playback;
1669 snd_pcm_set_sync(substream);
1670 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1671 return 0;
1672}
1673
1674
1675static int snd_ali_capture_open(snd_pcm_substream_t * substream)
1676{
1677 ali_t *codec = snd_pcm_substream_chip(substream);
1678 snd_pcm_runtime_t *runtime = substream->runtime;
1679 snd_ali_voice_t *pvoice;
1680 unsigned long flags;
1681
1682 spin_lock_irqsave(&codec->reg_lock, flags);
1683 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 1);
1684 if (pvoice == NULL) {
1685 spin_unlock_irqrestore(&codec->reg_lock, flags);
1686 return -EAGAIN;
1687 }
1688 pvoice->codec = codec;
1689 spin_unlock_irqrestore(&codec->reg_lock, flags);
1690
1691 pvoice->substream = substream;
1692 runtime->private_data = pvoice;
1693 runtime->private_free = snd_ali_pcm_free_substream;
1694 runtime->hw = snd_ali_capture;
1695 snd_pcm_set_sync(substream);
1696 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1697 return 0;
1698}
1699
1700
1701static int snd_ali_playback_close(snd_pcm_substream_t * substream)
1702{
1703 return 0;
1704}
1705
1706static int snd_ali_capture_close(snd_pcm_substream_t * substream)
1707{
1708 ali_t *codec = snd_pcm_substream_chip(substream);
1709 snd_pcm_runtime_t *runtime = substream->runtime;
1710 snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1711
1712 snd_ali_disable_special_channel(codec,pvoice->number);
1713
1714 return 0;
1715}
1716
1717#ifdef TARGET_OS2
1718static snd_pcm_ops_t snd_ali_playback_ops = {
1719 snd_ali_playback_open,
1720 snd_ali_playback_close,
1721 snd_ali_ioctl,
1722 snd_ali_playback_hw_params,
1723 snd_ali_playback_hw_free,
1724 snd_ali_playback_prepare,
1725 snd_ali_trigger,
1726 snd_ali_playback_pointer,0,0
1727};
1728
1729static snd_pcm_ops_t snd_ali_capture_ops = {
1730 snd_ali_capture_open,
1731 snd_ali_capture_close,
1732 snd_ali_ioctl,
1733 snd_ali_capture_hw_params,
1734 snd_ali_capture_hw_free,
1735 snd_ali_capture_prepare,
1736 snd_ali_trigger,
1737 snd_ali_capture_pointer,0,0
1738};
1739#else
1740static snd_pcm_ops_t snd_ali_playback_ops = {
1741open: snd_ali_playback_open,
1742 close: snd_ali_playback_close,
1743 ioctl: snd_ali_ioctl,
1744 hw_params: snd_ali_playback_hw_params,
1745 hw_free: snd_ali_playback_hw_free,
1746 prepare: snd_ali_playback_prepare,
1747 trigger: snd_ali_trigger,
1748 pointer: snd_ali_playback_pointer,
1749};
1750
1751static snd_pcm_ops_t snd_ali_capture_ops = {
1752open: snd_ali_capture_open,
1753 close: snd_ali_capture_close,
1754 ioctl: snd_ali_ioctl,
1755 hw_params: snd_ali_capture_hw_params,
1756 hw_free: snd_ali_capture_hw_free,
1757 prepare: snd_ali_capture_prepare,
1758 trigger: snd_ali_trigger,
1759 pointer: snd_ali_capture_pointer,
1760};
1761#endif
1762
1763static void snd_ali_pcm_free(snd_pcm_t *pcm)
1764{
1765 ali_t *codec = pcm->private_data;
1766 codec->pcm = NULL;
1767}
1768
1769static int __devinit snd_ali_pcm(ali_t * codec, int device, snd_pcm_t ** rpcm)
1770{
1771 snd_pcm_t *pcm;
1772 int err;
1773
1774 if (rpcm) *rpcm = NULL;
1775 err = snd_pcm_new(codec->card, "ALI 5451", device, ALI_CHANNELS, 1, &pcm);
1776 if (err < 0) {
1777 snd_printk("snd_ali_pcm: err called snd_pcm_new.\n");
1778 return err;
1779 }
1780 pcm->private_data = codec;
1781 pcm->private_free = snd_ali_pcm_free;
1782 pcm->info_flags = 0;
1783 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ali_playback_ops);
1784 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ali_capture_ops);
1785
1786 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1787 snd_dma_pci_data(codec->pci), 64*1024, 128*1024);
1788
1789 pcm->info_flags = 0;
1790 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1791 strcpy(pcm->name, "ALI 5451");
1792 codec->pcm = pcm;
1793 if (rpcm) *rpcm = pcm;
1794 return 0;
1795}
1796
1797
1798#ifdef TARGET_OS2
1799#define ALI5451_SPDIF(xname, xindex, value) \
1800 { SNDRV_CTL_ELEM_IFACE_MIXER, 0,0, xname, xindex,\
1801 0, 0, snd_ali5451_spdif_info, snd_ali5451_spdif_get, \
1802 snd_ali5451_spdif_put, value}
1803#else
1804#define ALI5451_SPDIF(xname, xindex, value) \
1805 { iface: SNDRV_CTL_ELEM_IFACE_MIXER, name: xname, index: xindex,\
1806 info: snd_ali5451_spdif_info, get: snd_ali5451_spdif_get, \
1807 put: snd_ali5451_spdif_put, private_value: value}
1808#endif
1809
1810static int snd_ali5451_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1811{
1812 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1813 uinfo->count = 1;
1814 uinfo->value.integer.min = 0;
1815 uinfo->value.integer.max = 1;
1816 return 0;
1817}
1818
1819static int snd_ali5451_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1820{
1821 unsigned long flags;
1822 ali_t *codec = kcontrol->private_data;
1823 unsigned int enable;
1824
1825 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1826
1827 spin_lock_irqsave(&codec->reg_lock, flags);
1828 switch(kcontrol->private_value) {
1829 case 0:
1830 enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1831 break;
1832 case 1:
1833 enable = ((codec->spdif_mask & 0x02) && (codec->spdif_mask & 0x04)) ? 1 : 0;
1834 break;
1835 case 2:
1836 enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1837 break;
1838 default:
1839 break;
1840 }
1841 ucontrol->value.integer.value[0] = enable;
1842 spin_unlock_irqrestore(&codec->reg_lock, flags);
1843 return 0;
1844}
1845
1846static int snd_ali5451_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1847{
1848 unsigned long flags;
1849 ali_t *codec = kcontrol->private_data;
1850 unsigned int change = 0, enable = 0;
1851
1852 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1853
1854 spin_lock_irqsave(&codec->reg_lock, flags);
1855 switch (kcontrol->private_value) {
1856 case 0:
1857 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1858 change = change ^ enable;
1859 if (change) {
1860 if (enable) {
1861 codec->spdif_mask |= 0x02;
1862 snd_ali_enable_spdif_out(codec);
1863 } else {
1864 codec->spdif_mask &= ~(0x02);
1865 codec->spdif_mask &= ~(0x04);
1866 snd_ali_disable_spdif_out(codec);
1867 }
1868 }
1869 break;
1870 case 1:
1871 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1872 change = change ^ enable;
1873 if (change && (codec->spdif_mask & 0x02)) {
1874 if (enable) {
1875 codec->spdif_mask |= 0x04;
1876 snd_ali_enable_spdif_chnout(codec);
1877 } else {
1878 codec->spdif_mask &= ~(0x04);
1879 snd_ali_disable_spdif_chnout(codec);
1880 }
1881 }
1882 break;
1883 case 2:
1884 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1885 change = change ^ enable;
1886 if (change) {
1887 if (enable) {
1888 codec->spdif_mask |= 0x01;
1889 snd_ali_enable_spdif_in(codec);
1890 } else {
1891 codec->spdif_mask &= ~(0x01);
1892 snd_ali_disable_spdif_in(codec);
1893 }
1894 }
1895 break;
1896 default:
1897 break;
1898 }
1899 spin_unlock_irqrestore(&codec->reg_lock, flags);
1900
1901 return change;
1902}
1903
1904static snd_kcontrol_new_t snd_ali5451_mixer_spdif[] __devinitdata = {
1905 ALI5451_SPDIF("S/PDIF Out", 0, 0),
1906 ALI5451_SPDIF("S/PDIF Out to S/PDIF Channel",0, 1),
1907 ALI5451_SPDIF("S/PDIF In from S/PDIF Channel",0, 2) };
1908
1909static void snd_ali_mixer_free_ac97_bus(ac97_bus_t *bus)
1910{
1911 ali_t *codec = bus->private_data;
1912 codec->ac97_bus = NULL;
1913}
1914
1915static void snd_ali_mixer_free_ac97(ac97_t *ac97)
1916{
1917 ali_t *codec = ac97->private_data;
1918 codec->ac97 = NULL;
1919}
1920
1921static int __devinit snd_ali_mixer(ali_t * codec)
1922{
1923 ac97_template_t ac97;
1924 unsigned int idx;
1925 int err;
1926 static ac97_bus_ops_t ops = {
1927 0,snd_ali_codec_write,
1928 snd_ali_codec_read,0,0
1929 };
1930
1931 if ((err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus)) < 0)
1932 return err;
1933 codec->ac97_bus->private_free = snd_ali_mixer_free_ac97_bus;
1934
1935 memset(&ac97, 0, sizeof(ac97));
1936 ac97.private_data = codec;
1937 ac97.private_free = snd_ali_mixer_free_ac97;
1938 if ((err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97)) < 0) {
1939 snd_printk("ali mixer creating error.\n");
1940 return err;
1941 }
1942 if (codec->spdif_support) {
1943 for(idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1944 err=snd_ctl_add(codec->card, snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1945 if (err < 0) return err;
1946 }
1947 }
1948 return 0;
1949}
1950
1951#ifdef CONFIG_PM
1952static int ali_suspend(snd_card_t *card, unsigned int state)
1953{
1954 ali_t *chip = card->pm_private_data;
1955 ali_image_t *im;
1956 int i, j;
1957
1958 im = chip->image;
1959 if (! im)
1960 return 0;
1961
1962 snd_pcm_suspend_all(chip->pcm);
1963 snd_ac97_suspend(chip->ac97);
1964
1965 spin_lock_irq(&chip->reg_lock);
1966
1967 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
1968 // im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START));
1969 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1970
1971 // disable all IRQ bits
1972 outl(0, ALI_REG(chip, ALI_MISCINT));
1973
1974 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1975 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1976 continue;
1977 im->regs[i] = inl(ALI_REG(chip, i*4));
1978 }
1979
1980 for (i = 0; i < ALI_CHANNELS; i++) {
1981 outb(i, ALI_REG(chip, ALI_GC_CIR));
1982 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1983 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1984 }
1985
1986 // stop all HW channel
1987 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1988
1989 spin_unlock_irq(&chip->reg_lock);
1990 return 0;
1991}
1992
1993static int ali_resume(snd_card_t *card, unsigned int state)
1994{
1995 ali_t *chip = card->pm_private_data;
1996 ali_image_t *im;
1997 int i, j;
1998
1999 im = chip->image;
2000 if (! im)
2001 return 0;
2002
2003 pci_enable_device(chip->pci);
2004
2005 spin_lock_irq(&chip->reg_lock);
2006
2007 for (i = 0; i < ALI_CHANNELS; i++) {
2008 outb(i, ALI_REG(chip, ALI_GC_CIR));
2009 for (j = 0; j < ALI_CHANNEL_REGS; j++)
2010 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
2011 }
2012
2013 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
2014 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) || (i*4 == ALI_START))
2015 continue;
2016 outl(im->regs[i], ALI_REG(chip, i*4));
2017 }
2018
2019 // start HW channel
2020 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
2021 // restore IRQ enable bits
2022 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
2023
2024 spin_unlock_irq(&chip->reg_lock);
2025
2026 snd_ac97_resume(chip->ac97);
2027 return 0;
2028}
2029#endif /* CONFIG_PM */
2030
2031static int snd_ali_free(ali_t * codec)
2032{
2033 if (codec->hw_initialized)
2034 snd_ali_disable_address_interrupt(codec);
2035 if (codec->irq >= 0) {
2036 synchronize_irq(codec->irq);
2037 free_irq(codec->irq, (void *)codec);
2038 }
2039 if (codec->port)
2040 pci_release_regions(codec->pci);
2041#ifdef CONFIG_PM
2042 if (codec->image)
2043 kfree(codec->image);
2044#endif
2045 kfree(codec);
2046 return 0;
2047}
2048
2049static int snd_ali_chip_init(ali_t *codec)
2050{
2051 unsigned int legacy;
2052 unsigned char temp;
2053 struct pci_dev *pci_dev = NULL;
2054
2055 snd_ali_printk("chip initializing ... \n");
2056
2057 if (snd_ali_reset_5451(codec)) {
2058 snd_printk("ali_chip_init: reset 5451 error.\n");
2059 return -1;
2060 }
2061
2062 if (codec->revision == ALI_5451_V02) {
2063 pci_dev = codec->pci_m1533;
2064 pci_read_config_byte(pci_dev, 0x59, &temp);
2065 temp |= 0x80;
2066 pci_write_config_byte(pci_dev, 0x59, temp);
2067
2068 pci_dev = codec->pci_m7101;
2069 pci_read_config_byte(pci_dev,0xb8,&temp);
2070 temp |= 0x20;
2071 pci_write_config_byte(pci_dev, 0xB8, temp);
2072 }
2073
2074 pci_read_config_dword(codec->pci, 0x44, &legacy);
2075 legacy &= 0xff00ff00;
2076 legacy |= 0x000800aa;
2077 pci_write_config_dword(codec->pci, 0x44, legacy);
2078
2079 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2080 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2081 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2082 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2083 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2084
2085 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
2086 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_STATUS);
2087 if (codec->spdif_support) {
2088 snd_ali_enable_spdif_out(codec);
2089 codec->spdif_mask = 0x00000002;
2090 }
2091
2092 snd_ali_printk("chip initialize succeed.\n");
2093 return 0;
2094
2095}
2096
2097static int __devinit snd_ali_resources(ali_t *codec)
2098{
2099 int err;
2100
2101 snd_ali_printk("resouces allocation ...\n");
2102 if ((err = pci_request_regions(codec->pci, "ALI 5451")) < 0)
2103 return err;
2104 codec->port = pci_resource_start(codec->pci, 0);
2105
2106 if (request_irq(codec->pci->irq, snd_ali_card_interrupt, SA_INTERRUPT|SA_SHIRQ, "ALI 5451", (void *)codec)) {
2107 snd_printk("Unable to request irq.\n");
2108 return -EBUSY;
2109 }
2110 codec->irq = codec->pci->irq;
2111 snd_ali_printk("resouces allocated.\n");
2112 return 0;
2113}
2114static int snd_ali_dev_free(snd_device_t *device)
2115{
2116 ali_t *codec=device->device_data;
2117 snd_ali_free(codec);
2118 return 0;
2119}
2120
2121static int __devinit snd_ali_create(snd_card_t * card,
2122 struct pci_dev *pci,
2123 int pcm_streams,
2124 int spdif_support,
2125 ali_t ** r_ali)
2126{
2127 ali_t *codec;
2128 int i, err;
2129 unsigned short cmdw = 0;
2130 struct pci_dev *pci_dev = NULL;
2131 static snd_device_ops_t ops = {
2132 (snd_dev_free_t *)snd_ali_dev_free,
2133 NULL,
2134 NULL,
2135 NULL
2136 };
2137
2138 *r_ali = NULL;
2139
2140 snd_ali_printk("creating ...\n");
2141
2142 /* enable PCI device */
2143 if ((err = pci_enable_device(pci)) < 0)
2144 return err;
2145 /* check, if we can restrict PCI DMA transfers to 31 bits */
2146 if (!pci_dma_supported(pci, 0x7fffffff)) {
2147 snd_printk("architecture does not support 31bit PCI busmaster DMA\n");
2148 return -ENXIO;
2149 }
2150 pci_set_dma_mask(pci, 0x7fffffff);
2151
2152 if ((codec = kcalloc(1, sizeof(*codec), GFP_KERNEL)) == NULL)
2153 return -ENOMEM;
2154
2155 spin_lock_init(&codec->reg_lock);
2156 spin_lock_init(&codec->voice_alloc);
2157
2158 codec->card = card;
2159 codec->pci = pci;
2160 codec->irq = -1;
2161 pci_read_config_byte(pci, PCI_REVISION_ID, &codec->revision);
2162 codec->spdif_support = spdif_support;
2163
2164 if (pcm_streams < 1)
2165 pcm_streams = 1;
2166 if (pcm_streams > 32)
2167 pcm_streams = 32;
2168
2169 pci_set_master(pci);
2170 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2171 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2172 cmdw |= PCI_COMMAND_IO;
2173 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2174 }
2175 pci_set_master(pci);
2176
2177 if (snd_ali_resources(codec)) {
2178 snd_ali_free(codec);
2179 return -EBUSY;
2180 }
2181
2182 synchronize_irq(pci->irq);
2183
2184 codec->synth.chmap = 0;
2185 codec->synth.chcnt = 0;
2186 codec->spdif_mask = 0;
2187 codec->synth.synthcount = 0;
2188
2189 if (codec->revision == ALI_5451_V02)
2190 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2191 else
2192 codec->chregs.regs.ac97read = ALI_AC97_READ;
2193 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2194
2195 codec->chregs.regs.start = ALI_START;
2196 codec->chregs.regs.stop = ALI_STOP;
2197 codec->chregs.regs.aint = ALI_AINT;
2198 codec->chregs.regs.ainten = ALI_AINTEN;
2199
2200 codec->chregs.data.start = 0x00;
2201 codec->chregs.data.stop = 0x00;
2202 codec->chregs.data.aint = 0x00;
2203 codec->chregs.data.ainten = 0x00;
2204
2205 /* M1533: southbridge */
2206 pci_dev = pci_find_device(0x10b9, 0x1533, NULL);
2207 codec->pci_m1533 = pci_dev;
2208 if (! codec->pci_m1533) {
2209 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2210 snd_ali_free(codec);
2211 return -ENODEV;
2212 }
2213 /* M7101: power management */
2214 pci_dev = pci_find_device(0x10b9, 0x7101, NULL);
2215 codec->pci_m7101 = pci_dev;
2216 if (! codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2217 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2218 snd_ali_free(codec);
2219 return -ENODEV;
2220 }
2221
2222 snd_ali_printk("snd_device_new is called.\n");
2223 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
2224 snd_ali_free(codec);
2225 return err;
2226 }
2227
2228 /* initialise synth voices*/
2229 for (i = 0; i < ALI_CHANNELS; i++ ) {
2230 codec->synth.voices[i].number = i;
2231 }
2232
2233 if ((err = snd_ali_chip_init(codec)) < 0) {
2234 snd_printk("ali create: chip init error.\n");
2235 return err;
2236 }
2237
2238#ifdef CONFIG_PM
2239 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
2240 if (! codec->image)
2241 snd_printk("can't allocate apm buffer\n");
2242 else
2243 snd_card_set_pm_callback(card, ali_suspend, ali_resume, codec);
2244#endif
2245
2246 snd_ali_enable_address_interrupt(codec);
2247 codec->hw_initialized = 1;
2248
2249 *r_ali = codec;
2250 snd_ali_printk("created.\n");
2251 return 0;
2252}
2253
2254static int __devinit snd_ali_probe(struct pci_dev *pci,
2255 const struct pci_device_id *pci_id)
2256{
2257 static int dev;
2258 snd_card_t *card;
2259 ali_t *codec;
2260 int err;
2261
2262 snd_ali_printk("probe ...\n");
2263
2264 if (dev >= SNDRV_CARDS)
2265 return -ENODEV;
2266 if (!enable[dev]) {
2267 dev++;
2268 return -ENOENT;
2269 }
2270
2271 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2272 if (card == NULL)
2273 return -ENOMEM;
2274
2275 if ((err = snd_ali_create(card, pci, pcm_channels[dev], spdif[dev], &codec)) < 0) {
2276 snd_card_free(card);
2277 return err;
2278 }
2279
2280 snd_ali_printk("mixer building ...\n");
2281 if ((err = snd_ali_mixer(codec)) < 0) {
2282 snd_card_free(card);
2283 return err;
2284 }
2285
2286 snd_ali_printk("pcm building ...\n");
2287 if ((err = snd_ali_pcm(codec, 0, NULL)) < 0) {
2288 snd_card_free(card);
2289 return err;
2290 }
2291
2292 strcpy(card->driver, "ALI5451");
2293 strcpy(card->shortname, "ALI 5451");
2294
2295 sprintf(card->longname, "%s at 0x%lx, irq %li",
2296 card->shortname, codec->port, codec->irq);
2297
2298 snd_ali_printk("register card.\n");
2299 if ((err = snd_card_register(card)) < 0) {
2300 snd_card_free(card);
2301 return err;
2302 }
2303 pci_set_drvdata(pci, card);
2304 dev++;
2305 return 0;
2306}
2307
2308static void __devexit snd_ali_remove(struct pci_dev *pci)
2309{
2310 snd_card_free(pci_get_drvdata(pci));
2311 pci_set_drvdata(pci, NULL);
2312}
2313
2314static struct pci_driver driver = {
2315 0,0,0,"ALI 5451",
2316 snd_ali_ids,
2317 snd_ali_probe,
2318 snd_ali_remove,
2319 SND_PCI_PM_CALLBACKS
2320};
2321
2322static int __init alsa_card_ali_init(void)
2323{
2324 int err;
2325
2326 if ((err = pci_module_init(&driver)) < 0) {
2327#ifdef MODULE
2328 // snd_printk("ALi pci audio not found or device busy.\n");
2329#endif
2330 return err;
2331 }
2332 return 0;
2333}
2334
2335static void __exit alsa_card_ali_exit(void)
2336{
2337 pci_unregister_driver(&driver);
2338}
2339
2340module_init(alsa_card_ali_init)
2341module_exit(alsa_card_ali_exit)
2342
2343#ifndef MODULE
2344
2345/* format is: snd-ali5451=enable,index,id,pcm_channels */
2346
2347static int __init alsa_card_ali_setup(char *str)
2348{
2349 static unsigned __initdata nr_dev = 0;
2350
2351 if (nr_dev >= SNDRV_CARDS)
2352 return 0;
2353 (void)(get_option(&str,&enable[nr_dev]) == 2 &&
2354 get_option(&str,&index[nr_dev]) == 2 &&
2355 get_id(&str,&id[nr_dev]) == 2 &&
2356 get_option(&str,&pcm_channels[nr_dev]) == 2);
2357 nr_dev++;
2358 return 1;
2359}
2360
2361__setup("snd-ali5451=", alsa_card_ali_setup);
2362
2363#endif /* ifndef */
Note: See TracBrowser for help on using the repository browser.