~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/pci/ali5451/ali5451.c

Version: ~ [ linux-5.2-rc1 ] ~ [ linux-5.1.2 ] ~ [ linux-5.0.16 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.43 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.119 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.176 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.179 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.139 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.67 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp