0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/io.h>
0015 #include <linux/delay.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/init.h>
0018 #include <linux/pci.h>
0019 #include <linux/slab.h>
0020 #include <linux/module.h>
0021 #include <linux/dma-mapping.h>
0022 #include <sound/core.h>
0023 #include <sound/pcm.h>
0024 #include <sound/info.h>
0025 #include <sound/ac97_codec.h>
0026 #include <sound/mpu401.h>
0027 #include <sound/initval.h>
0028
0029 MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
0030 MODULE_DESCRIPTION("ALI M5451");
0031 MODULE_LICENSE("GPL");
0032
0033 static int index = SNDRV_DEFAULT_IDX1;
0034 static char *id = SNDRV_DEFAULT_STR1;
0035 static int pcm_channels = 32;
0036 static bool spdif;
0037
0038 module_param(index, int, 0444);
0039 MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
0040 module_param(id, charp, 0444);
0041 MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
0042 module_param(pcm_channels, int, 0444);
0043 MODULE_PARM_DESC(pcm_channels, "PCM Channels");
0044 module_param(spdif, bool, 0444);
0045 MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
0046
0047
0048 static bool enable;
0049 module_param(enable, bool, 0444);
0050
0051
0052
0053
0054
0055
0056 #define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
0057
0058
0059 #define ALI_CHANNELS 32
0060
0061 #define ALI_PCM_IN_CHANNEL 31
0062 #define ALI_SPDIF_IN_CHANNEL 19
0063 #define ALI_SPDIF_OUT_CHANNEL 15
0064 #define ALI_CENTER_CHANNEL 24
0065 #define ALI_LEF_CHANNEL 23
0066 #define ALI_SURR_LEFT_CHANNEL 26
0067 #define ALI_SURR_RIGHT_CHANNEL 25
0068 #define ALI_MODEM_IN_CHANNEL 21
0069 #define ALI_MODEM_OUT_CHANNEL 20
0070
0071 #define SNDRV_ALI_VOICE_TYPE_PCM 01
0072 #define SNDRV_ALI_VOICE_TYPE_OTH 02
0073
0074 #define ALI_5451_V02 0x02
0075
0076
0077
0078
0079
0080 #define ALI_LEGACY_DMAR0 0x00
0081 #define ALI_LEGACY_DMAR4 0x04
0082 #define ALI_LEGACY_DMAR11 0x0b
0083 #define ALI_LEGACY_DMAR15 0x0f
0084 #define ALI_MPUR0 0x20
0085 #define ALI_MPUR1 0x21
0086 #define ALI_MPUR2 0x22
0087 #define ALI_MPUR3 0x23
0088
0089 #define ALI_AC97_WRITE 0x40
0090 #define ALI_AC97_READ 0x44
0091
0092 #define ALI_SCTRL 0x48
0093 #define ALI_SPDIF_OUT_ENABLE 0x20
0094 #define ALI_SCTRL_LINE_IN2 (1 << 9)
0095 #define ALI_SCTRL_GPIO_IN2 (1 << 13)
0096 #define ALI_SCTRL_LINE_OUT_EN (1 << 20)
0097 #define ALI_SCTRL_GPIO_OUT_EN (1 << 23)
0098 #define ALI_SCTRL_CODEC1_READY (1 << 24)
0099 #define ALI_SCTRL_CODEC2_READY (1 << 25)
0100 #define ALI_AC97_GPIO 0x4c
0101 #define ALI_AC97_GPIO_ENABLE 0x8000
0102 #define ALI_AC97_GPIO_DATA_SHIFT 16
0103 #define ALI_SPDIF_CS 0x70
0104 #define ALI_SPDIF_CTRL 0x74
0105 #define ALI_SPDIF_IN_FUNC_ENABLE 0x02
0106 #define ALI_SPDIF_IN_CH_STATUS 0x40
0107 #define ALI_SPDIF_OUT_CH_STATUS 0xbf
0108 #define ALI_START 0x80
0109 #define ALI_STOP 0x84
0110 #define ALI_CSPF 0x90
0111 #define ALI_AINT 0x98
0112 #define ALI_GC_CIR 0xa0
0113 #define ENDLP_IE 0x00001000
0114 #define MIDLP_IE 0x00002000
0115 #define ALI_AINTEN 0xa4
0116 #define ALI_VOLUME 0xa8
0117 #define ALI_SBDELTA_DELTA_R 0xac
0118 #define ALI_MISCINT 0xb0
0119 #define ADDRESS_IRQ 0x00000020
0120 #define TARGET_REACHED 0x00008000
0121 #define MIXER_OVERFLOW 0x00000800
0122 #define MIXER_UNDERFLOW 0x00000400
0123 #define GPIO_IRQ 0x01000000
0124 #define ALI_SBBL_SBCL 0xc0
0125 #define ALI_SBCTRL_SBE2R_SBDD 0xc4
0126 #define ALI_STIMER 0xc8
0127 #define ALI_GLOBAL_CONTROL 0xd4
0128 #define ALI_SPDIF_OUT_SEL_PCM 0x00000400
0129 #define ALI_SPDIF_IN_SUPPORT 0x00000800
0130 #define ALI_SPDIF_OUT_CH_ENABLE 0x00008000
0131 #define ALI_SPDIF_IN_CH_ENABLE 0x00080000
0132 #define ALI_PCM_IN_ENABLE 0x80000000
0133
0134 #define ALI_CSO_ALPHA_FMS 0xe0
0135 #define ALI_LBA 0xe4
0136 #define ALI_ESO_DELTA 0xe8
0137 #define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
0138 #define ALI_EBUF1 0xf4
0139 #define ALI_EBUF2 0xf8
0140
0141 #define ALI_REG(codec, x) ((codec)->port + x)
0142
0143 #define MAX_CODECS 2
0144
0145
0146 struct snd_ali;
0147 struct snd_ali_voice;
0148
0149 struct snd_ali_channel_control {
0150
0151 struct REGDATA {
0152 unsigned int start;
0153 unsigned int stop;
0154 unsigned int aint;
0155 unsigned int ainten;
0156 } data;
0157
0158
0159 struct REGS {
0160 unsigned int start;
0161 unsigned int stop;
0162 unsigned int aint;
0163 unsigned int ainten;
0164 unsigned int ac97read;
0165 unsigned int ac97write;
0166 } regs;
0167
0168 };
0169
0170 struct snd_ali_voice {
0171 unsigned int number;
0172 unsigned int use :1,
0173 pcm :1,
0174 midi :1,
0175 mode :1,
0176 synth :1,
0177 running :1;
0178
0179
0180 struct snd_ali *codec;
0181 struct snd_pcm_substream *substream;
0182 struct snd_ali_voice *extra;
0183
0184 int eso;
0185 int count;
0186
0187
0188
0189 void *private_data;
0190 void (*private_free)(void *private_data);
0191 };
0192
0193
0194 struct snd_alidev {
0195
0196 struct snd_ali_voice voices[ALI_CHANNELS];
0197
0198 unsigned int chcnt;
0199 unsigned int chmap;
0200 unsigned int synthcount;
0201
0202 };
0203
0204
0205 #define ALI_GLOBAL_REGS 56
0206 #define ALI_CHANNEL_REGS 8
0207 struct snd_ali_image {
0208 u32 regs[ALI_GLOBAL_REGS];
0209 u32 channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
0210 };
0211
0212
0213 struct snd_ali {
0214 int irq;
0215 unsigned long port;
0216 unsigned char revision;
0217
0218 unsigned int hw_initialized :1;
0219 unsigned int spdif_support :1;
0220
0221 struct pci_dev *pci;
0222 struct pci_dev *pci_m1533;
0223 struct pci_dev *pci_m7101;
0224
0225 struct snd_card *card;
0226 struct snd_pcm *pcm[MAX_CODECS];
0227 struct snd_alidev synth;
0228 struct snd_ali_channel_control chregs;
0229
0230
0231 unsigned int spdif_mask;
0232
0233 unsigned int spurious_irq_count;
0234 unsigned int spurious_irq_max_delta;
0235
0236 unsigned int num_of_codecs;
0237
0238 struct snd_ac97_bus *ac97_bus;
0239 struct snd_ac97 *ac97[MAX_CODECS];
0240 unsigned short ac97_ext_id;
0241 unsigned short ac97_ext_status;
0242
0243 spinlock_t reg_lock;
0244 spinlock_t voice_alloc;
0245
0246 #ifdef CONFIG_PM_SLEEP
0247 struct snd_ali_image *image;
0248 #endif
0249 };
0250
0251 static const struct pci_device_id snd_ali_ids[] = {
0252 {PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
0253 {0, }
0254 };
0255 MODULE_DEVICE_TABLE(pci, snd_ali_ids);
0256
0257 static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
0258 static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
0259 static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short,
0260 unsigned short);
0261
0262
0263
0264
0265
0266 static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
0267 unsigned int port)
0268 {
0269 return (unsigned int)inl(ALI_REG(codec, port));
0270 }
0271
0272 static inline void snd_ali_5451_poke(struct snd_ali *codec,
0273 unsigned int port,
0274 unsigned int val)
0275 {
0276 outl((unsigned int)val, ALI_REG(codec, port));
0277 }
0278
0279 static int snd_ali_codec_ready(struct snd_ali *codec,
0280 unsigned int port)
0281 {
0282 unsigned long end_time;
0283 unsigned int res;
0284
0285 end_time = jiffies + msecs_to_jiffies(250);
0286
0287 for (;;) {
0288 res = snd_ali_5451_peek(codec,port);
0289 if (!(res & 0x8000))
0290 return 0;
0291 if (!time_after_eq(end_time, jiffies))
0292 break;
0293 schedule_timeout_uninterruptible(1);
0294 }
0295
0296 snd_ali_5451_poke(codec, port, res & ~0x8000);
0297 dev_dbg(codec->card->dev, "ali_codec_ready: codec is not ready.\n ");
0298 return -EIO;
0299 }
0300
0301 static int snd_ali_stimer_ready(struct snd_ali *codec)
0302 {
0303 unsigned long end_time;
0304 unsigned long dwChk1,dwChk2;
0305
0306 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
0307 end_time = jiffies + msecs_to_jiffies(250);
0308
0309 for (;;) {
0310 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
0311 if (dwChk2 != dwChk1)
0312 return 0;
0313 if (!time_after_eq(end_time, jiffies))
0314 break;
0315 schedule_timeout_uninterruptible(1);
0316 }
0317
0318 dev_err(codec->card->dev, "ali_stimer_read: stimer is not ready.\n");
0319 return -EIO;
0320 }
0321
0322 static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
0323 unsigned short reg,
0324 unsigned short val)
0325 {
0326 unsigned int dwVal;
0327 unsigned int port;
0328
0329 if (reg >= 0x80) {
0330 dev_err(codec->card->dev,
0331 "ali_codec_poke: reg(%xh) invalid.\n", reg);
0332 return;
0333 }
0334
0335 port = codec->chregs.regs.ac97write;
0336
0337 if (snd_ali_codec_ready(codec, port) < 0)
0338 return;
0339 if (snd_ali_stimer_ready(codec) < 0)
0340 return;
0341
0342 dwVal = (unsigned int) (reg & 0xff);
0343 dwVal |= 0x8000 | (val << 16);
0344 if (secondary)
0345 dwVal |= 0x0080;
0346 if (codec->revision == ALI_5451_V02)
0347 dwVal |= 0x0100;
0348
0349 snd_ali_5451_poke(codec, port, dwVal);
0350
0351 return ;
0352 }
0353
0354 static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
0355 int secondary,
0356 unsigned short reg)
0357 {
0358 unsigned int dwVal;
0359 unsigned int port;
0360
0361 if (reg >= 0x80) {
0362 dev_err(codec->card->dev,
0363 "ali_codec_peek: reg(%xh) invalid.\n", reg);
0364 return ~0;
0365 }
0366
0367 port = codec->chregs.regs.ac97read;
0368
0369 if (snd_ali_codec_ready(codec, port) < 0)
0370 return ~0;
0371 if (snd_ali_stimer_ready(codec) < 0)
0372 return ~0;
0373
0374 dwVal = (unsigned int) (reg & 0xff);
0375 dwVal |= 0x8000;
0376 if (secondary)
0377 dwVal |= 0x0080;
0378
0379 snd_ali_5451_poke(codec, port, dwVal);
0380
0381 if (snd_ali_stimer_ready(codec) < 0)
0382 return ~0;
0383 if (snd_ali_codec_ready(codec, port) < 0)
0384 return ~0;
0385
0386 return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
0387 }
0388
0389 static void snd_ali_codec_write(struct snd_ac97 *ac97,
0390 unsigned short reg,
0391 unsigned short val )
0392 {
0393 struct snd_ali *codec = ac97->private_data;
0394
0395 dev_dbg(codec->card->dev, "codec_write: reg=%xh data=%xh.\n", reg, val);
0396 if (reg == AC97_GPIO_STATUS) {
0397 outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
0398 ALI_REG(codec, ALI_AC97_GPIO));
0399 return;
0400 }
0401 snd_ali_codec_poke(codec, ac97->num, reg, val);
0402 return ;
0403 }
0404
0405
0406 static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
0407 unsigned short reg)
0408 {
0409 struct snd_ali *codec = ac97->private_data;
0410
0411 dev_dbg(codec->card->dev, "codec_read reg=%xh.\n", reg);
0412 return snd_ali_codec_peek(codec, ac97->num, reg);
0413 }
0414
0415
0416
0417
0418
0419 static int snd_ali_reset_5451(struct snd_ali *codec)
0420 {
0421 struct pci_dev *pci_dev;
0422 unsigned short wCount, wReg;
0423 unsigned int dwVal;
0424
0425 pci_dev = codec->pci_m1533;
0426 if (pci_dev) {
0427 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
0428 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
0429 mdelay(5);
0430 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
0431 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
0432 mdelay(5);
0433 }
0434
0435 pci_dev = codec->pci;
0436 pci_read_config_dword(pci_dev, 0x44, &dwVal);
0437 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
0438 udelay(500);
0439 pci_read_config_dword(pci_dev, 0x44, &dwVal);
0440 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
0441 mdelay(5);
0442
0443 wCount = 200;
0444 while(wCount--) {
0445 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
0446 if ((wReg & 0x000f) == 0x000f)
0447 return 0;
0448 mdelay(5);
0449 }
0450
0451
0452
0453 return 0;
0454 }
0455
0456
0457
0458
0459
0460 static void snd_ali_enable_special_channel(struct snd_ali *codec,
0461 unsigned int channel)
0462 {
0463 unsigned long dwVal;
0464
0465 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
0466 dwVal |= 1 << (channel & 0x0000001f);
0467 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
0468 }
0469
0470 static void snd_ali_disable_special_channel(struct snd_ali *codec,
0471 unsigned int channel)
0472 {
0473 unsigned long dwVal;
0474
0475 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
0476 dwVal &= ~(1 << (channel & 0x0000001f));
0477 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
0478 }
0479
0480 static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
0481 {
0482 unsigned int gc;
0483
0484 gc = inl(ALI_REG(codec, ALI_GC_CIR));
0485 gc |= ENDLP_IE;
0486 gc |= MIDLP_IE;
0487 outl( gc, ALI_REG(codec, ALI_GC_CIR));
0488 }
0489
0490 static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
0491 {
0492 unsigned int gc;
0493
0494 gc = inl(ALI_REG(codec, ALI_GC_CIR));
0495 gc &= ~ENDLP_IE;
0496 gc &= ~MIDLP_IE;
0497 outl(gc, ALI_REG(codec, ALI_GC_CIR));
0498 }
0499
0500 static void snd_ali_disable_voice_irq(struct snd_ali *codec,
0501 unsigned int channel)
0502 {
0503 unsigned int mask;
0504 struct snd_ali_channel_control *pchregs = &(codec->chregs);
0505
0506 dev_dbg(codec->card->dev, "disable_voice_irq channel=%d\n", channel);
0507
0508 mask = 1 << (channel & 0x1f);
0509 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
0510 pchregs->data.ainten &= ~mask;
0511 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
0512 }
0513
0514 static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
0515 {
0516 unsigned int idx = channel & 0x1f;
0517
0518 if (codec->synth.chcnt >= ALI_CHANNELS){
0519 dev_err(codec->card->dev,
0520 "ali_alloc_pcm_channel: no free channels.\n");
0521 return -1;
0522 }
0523
0524 if (!(codec->synth.chmap & (1 << idx))) {
0525 codec->synth.chmap |= 1 << idx;
0526 codec->synth.chcnt++;
0527 dev_dbg(codec->card->dev, "alloc_pcm_channel no. %d.\n", idx);
0528 return idx;
0529 }
0530 return -1;
0531 }
0532
0533 static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
0534 {
0535 int idx;
0536 int result = -1;
0537
0538 dev_dbg(codec->card->dev,
0539 "find_free_channel: for %s\n", rec ? "rec" : "pcm");
0540
0541
0542 if (rec) {
0543 if (codec->spdif_support &&
0544 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
0545 ALI_SPDIF_IN_SUPPORT))
0546 idx = ALI_SPDIF_IN_CHANNEL;
0547 else
0548 idx = ALI_PCM_IN_CHANNEL;
0549
0550 result = snd_ali_alloc_pcm_channel(codec, idx);
0551 if (result >= 0)
0552 return result;
0553 else {
0554 dev_err(codec->card->dev,
0555 "ali_find_free_channel: record channel is busy now.\n");
0556 return -1;
0557 }
0558 }
0559
0560
0561 if (codec->spdif_support &&
0562 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
0563 ALI_SPDIF_OUT_CH_ENABLE)) {
0564 idx = ALI_SPDIF_OUT_CHANNEL;
0565 result = snd_ali_alloc_pcm_channel(codec, idx);
0566 if (result >= 0)
0567 return result;
0568 else
0569 dev_err(codec->card->dev,
0570 "ali_find_free_channel: S/PDIF out channel is in busy now.\n");
0571 }
0572
0573 for (idx = 0; idx < ALI_CHANNELS; idx++) {
0574 result = snd_ali_alloc_pcm_channel(codec, idx);
0575 if (result >= 0)
0576 return result;
0577 }
0578 dev_err(codec->card->dev, "ali_find_free_channel: no free channels.\n");
0579 return -1;
0580 }
0581
0582 static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
0583 {
0584 unsigned int idx = channel & 0x0000001f;
0585
0586 dev_dbg(codec->card->dev, "free_channel_pcm channel=%d\n", channel);
0587
0588 if (channel < 0 || channel >= ALI_CHANNELS)
0589 return;
0590
0591 if (!(codec->synth.chmap & (1 << idx))) {
0592 dev_err(codec->card->dev,
0593 "ali_free_channel_pcm: channel %d is not in use.\n",
0594 channel);
0595 return;
0596 } else {
0597 codec->synth.chmap &= ~(1 << idx);
0598 codec->synth.chcnt--;
0599 }
0600 }
0601
0602 static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
0603 {
0604 unsigned int mask = 1 << (channel & 0x1f);
0605
0606 dev_dbg(codec->card->dev, "stop_voice: channel=%d\n", channel);
0607 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
0608 }
0609
0610
0611
0612
0613
0614 static void snd_ali_delay(struct snd_ali *codec,int interval)
0615 {
0616 unsigned long begintimer,currenttimer;
0617
0618 begintimer = inl(ALI_REG(codec, ALI_STIMER));
0619 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
0620
0621 while (currenttimer < begintimer + interval) {
0622 if (snd_ali_stimer_ready(codec) < 0)
0623 break;
0624 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
0625 cpu_relax();
0626 }
0627 }
0628
0629 static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
0630 {
0631 u16 wval;
0632 u16 count = 0;
0633 u8 bval, R1 = 0, R2;
0634
0635 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
0636 bval |= 0x1F;
0637 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
0638
0639 while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
0640 count ++;
0641 snd_ali_delay(codec, 6);
0642 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
0643 R1 = bval & 0x1F;
0644 }
0645
0646 if (count > 50000) {
0647 dev_err(codec->card->dev, "ali_detect_spdif_rate: timeout!\n");
0648 return;
0649 }
0650
0651 for (count = 0; count <= 50000; count++) {
0652 snd_ali_delay(codec, 6);
0653 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
0654 R2 = bval & 0x1F;
0655 if (R2 != R1)
0656 R1 = R2;
0657 else
0658 break;
0659 }
0660
0661 if (count > 50000) {
0662 dev_err(codec->card->dev, "ali_detect_spdif_rate: timeout!\n");
0663 return;
0664 }
0665
0666 if (R2 >= 0x0b && R2 <= 0x0e) {
0667 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
0668 wval &= 0xe0f0;
0669 wval |= (0x09 << 8) | 0x05;
0670 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
0671
0672 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
0673 outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
0674 } else if (R2 == 0x12) {
0675 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
0676 wval &= 0xe0f0;
0677 wval |= (0x0e << 8) | 0x08;
0678 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
0679
0680 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
0681 outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
0682 }
0683 }
0684
0685 static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
0686 {
0687 u32 dwRate;
0688 u8 bval;
0689
0690 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
0691 bval &= 0x7f;
0692 bval |= 0x40;
0693 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
0694
0695 snd_ali_detect_spdif_rate(codec);
0696
0697 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
0698 bval &= 0x0f;
0699
0700 switch (bval) {
0701 case 0: dwRate = 44100; break;
0702 case 1: dwRate = 48000; break;
0703 case 2: dwRate = 32000; break;
0704 default: dwRate = 0; break;
0705 }
0706
0707 return dwRate;
0708 }
0709
0710 static void snd_ali_enable_spdif_in(struct snd_ali *codec)
0711 {
0712 unsigned int dwVal;
0713
0714 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
0715 dwVal |= ALI_SPDIF_IN_SUPPORT;
0716 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
0717
0718 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
0719 dwVal |= 0x02;
0720 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
0721
0722 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
0723 }
0724
0725 static void snd_ali_disable_spdif_in(struct snd_ali *codec)
0726 {
0727 unsigned int dwVal;
0728
0729 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
0730 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
0731 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
0732
0733 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
0734 }
0735
0736
0737 static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
0738 {
0739 unsigned char bVal;
0740 unsigned int dwRate;
0741
0742 switch (rate) {
0743 case 32000: dwRate = 0x300; break;
0744 case 48000: dwRate = 0x200; break;
0745 default: dwRate = 0; break;
0746 }
0747
0748 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
0749 bVal &= (unsigned char)(~(1<<6));
0750
0751 bVal |= 0x80;
0752 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
0753 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
0754
0755 bVal &= ~0x80;
0756 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
0757 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
0758 }
0759
0760 static void snd_ali_enable_spdif_out(struct snd_ali *codec)
0761 {
0762 unsigned short wVal;
0763 unsigned char bVal;
0764 struct pci_dev *pci_dev;
0765
0766 pci_dev = codec->pci_m1533;
0767 if (pci_dev == NULL)
0768 return;
0769 pci_read_config_byte(pci_dev, 0x61, &bVal);
0770 bVal |= 0x40;
0771 pci_write_config_byte(pci_dev, 0x61, bVal);
0772 pci_read_config_byte(pci_dev, 0x7d, &bVal);
0773 bVal |= 0x01;
0774 pci_write_config_byte(pci_dev, 0x7d, bVal);
0775
0776 pci_read_config_byte(pci_dev, 0x7e, &bVal);
0777 bVal &= (~0x20);
0778 bVal |= 0x10;
0779 pci_write_config_byte(pci_dev, 0x7e, bVal);
0780
0781 bVal = inb(ALI_REG(codec, ALI_SCTRL));
0782 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
0783
0784 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
0785 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
0786
0787 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
0788 wVal |= ALI_SPDIF_OUT_SEL_PCM;
0789 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
0790 snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
0791 }
0792
0793 static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
0794 {
0795 unsigned short wVal;
0796
0797 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
0798 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
0799 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
0800
0801
0802
0803
0804
0805
0806
0807
0808 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
0809 }
0810
0811 static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
0812 {
0813 unsigned short wVal;
0814
0815 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
0816 wVal |= ALI_SPDIF_OUT_SEL_PCM;
0817 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
0818
0819 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
0820 }
0821
0822 static void snd_ali_disable_spdif_out(struct snd_ali *codec)
0823 {
0824 unsigned char bVal;
0825
0826 bVal = inb(ALI_REG(codec, ALI_SCTRL));
0827 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
0828
0829 snd_ali_disable_spdif_chnout(codec);
0830 }
0831
0832 static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
0833 {
0834 struct snd_ali_voice *pvoice;
0835 struct snd_ali_channel_control *pchregs;
0836 unsigned int old, mask;
0837
0838 pchregs = &(codec->chregs);
0839
0840
0841 old = pchregs->data.aint;
0842 mask = 1U << (channel & 0x1f);
0843
0844 if (!(old & mask))
0845 return;
0846
0847 pvoice = &codec->synth.voices[channel];
0848
0849 udelay(100);
0850 spin_lock(&codec->reg_lock);
0851
0852 if (pvoice->pcm && pvoice->substream) {
0853
0854 if (pvoice->running) {
0855 dev_dbg(codec->card->dev,
0856 "update_ptr: cso=%4.4x cspf=%d.\n",
0857 inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2)),
0858 (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask);
0859 spin_unlock(&codec->reg_lock);
0860 snd_pcm_period_elapsed(pvoice->substream);
0861 spin_lock(&codec->reg_lock);
0862 } else {
0863 snd_ali_stop_voice(codec, channel);
0864 snd_ali_disable_voice_irq(codec, channel);
0865 }
0866 } else if (codec->synth.voices[channel].synth) {
0867
0868 } else if (codec->synth.voices[channel].midi) {
0869
0870 } else {
0871
0872 snd_ali_stop_voice(codec, channel);
0873 snd_ali_disable_voice_irq(codec, channel);
0874 }
0875 spin_unlock(&codec->reg_lock);
0876 outl(mask,ALI_REG(codec,pchregs->regs.aint));
0877 pchregs->data.aint = old & (~mask);
0878 }
0879
0880 static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
0881 {
0882 struct snd_ali *codec = dev_id;
0883 int channel;
0884 unsigned int audio_int;
0885 struct snd_ali_channel_control *pchregs;
0886
0887 if (codec == NULL || !codec->hw_initialized)
0888 return IRQ_NONE;
0889
0890 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
0891 if (!audio_int)
0892 return IRQ_NONE;
0893
0894 pchregs = &(codec->chregs);
0895 if (audio_int & ADDRESS_IRQ) {
0896
0897 pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
0898 for (channel = 0; channel < ALI_CHANNELS; channel++)
0899 snd_ali_update_ptr(codec, channel);
0900 }
0901 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
0902 ALI_REG(codec, ALI_MISCINT));
0903
0904 return IRQ_HANDLED;
0905 }
0906
0907
0908 static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
0909 int type, int rec, int channel)
0910 {
0911 struct snd_ali_voice *pvoice;
0912 int idx;
0913
0914 dev_dbg(codec->card->dev, "alloc_voice: type=%d rec=%d\n", type, rec);
0915
0916 spin_lock_irq(&codec->voice_alloc);
0917 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
0918 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
0919 snd_ali_find_free_channel(codec,rec);
0920 if (idx < 0) {
0921 dev_err(codec->card->dev, "ali_alloc_voice: err.\n");
0922 spin_unlock_irq(&codec->voice_alloc);
0923 return NULL;
0924 }
0925 pvoice = &(codec->synth.voices[idx]);
0926 pvoice->codec = codec;
0927 pvoice->use = 1;
0928 pvoice->pcm = 1;
0929 pvoice->mode = rec;
0930 spin_unlock_irq(&codec->voice_alloc);
0931 return pvoice;
0932 }
0933 spin_unlock_irq(&codec->voice_alloc);
0934 return NULL;
0935 }
0936
0937
0938 static void snd_ali_free_voice(struct snd_ali * codec,
0939 struct snd_ali_voice *pvoice)
0940 {
0941 void (*private_free)(void *);
0942 void *private_data;
0943
0944 dev_dbg(codec->card->dev, "free_voice: channel=%d\n", pvoice->number);
0945 if (!pvoice->use)
0946 return;
0947 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
0948 spin_lock_irq(&codec->voice_alloc);
0949 private_free = pvoice->private_free;
0950 private_data = pvoice->private_data;
0951 pvoice->private_free = NULL;
0952 pvoice->private_data = NULL;
0953 if (pvoice->pcm)
0954 snd_ali_free_channel_pcm(codec, pvoice->number);
0955 pvoice->use = pvoice->pcm = pvoice->synth = 0;
0956 pvoice->substream = NULL;
0957 spin_unlock_irq(&codec->voice_alloc);
0958 if (private_free)
0959 private_free(private_data);
0960 }
0961
0962
0963 static void snd_ali_clear_voices(struct snd_ali *codec,
0964 unsigned int v_min,
0965 unsigned int v_max)
0966 {
0967 unsigned int i;
0968
0969 for (i = v_min; i <= v_max; i++) {
0970 snd_ali_stop_voice(codec, i);
0971 snd_ali_disable_voice_irq(codec, i);
0972 }
0973 }
0974
0975 static void snd_ali_write_voice_regs(struct snd_ali *codec,
0976 unsigned int Channel,
0977 unsigned int LBA,
0978 unsigned int CSO,
0979 unsigned int ESO,
0980 unsigned int DELTA,
0981 unsigned int ALPHA_FMS,
0982 unsigned int GVSEL,
0983 unsigned int PAN,
0984 unsigned int VOL,
0985 unsigned int CTRL,
0986 unsigned int EC)
0987 {
0988 unsigned int ctlcmds[4];
0989
0990 outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
0991
0992 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
0993 ctlcmds[1] = LBA;
0994 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
0995 ctlcmds[3] = (GVSEL << 31) |
0996 ((PAN & 0x0000007f) << 24) |
0997 ((VOL & 0x000000ff) << 16) |
0998 ((CTRL & 0x0000000f) << 12) |
0999 (EC & 0x00000fff);
1000
1001 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1002
1003 outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
1004 outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
1005 outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
1006 outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
1007
1008 outl(0x30000000, ALI_REG(codec, ALI_EBUF1));
1009 outl(0x30000000, ALI_REG(codec, ALI_EBUF2));
1010 }
1011
1012 static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1013 {
1014 unsigned int delta;
1015
1016 if (rate < 4000)
1017 rate = 4000;
1018 if (rate > 48000)
1019 rate = 48000;
1020
1021 if (rec) {
1022 if (rate == 44100)
1023 delta = 0x116a;
1024 else if (rate == 8000)
1025 delta = 0x6000;
1026 else if (rate == 48000)
1027 delta = 0x1000;
1028 else
1029 delta = ((48000 << 12) / rate) & 0x0000ffff;
1030 } else {
1031 if (rate == 44100)
1032 delta = 0xeb3;
1033 else if (rate == 8000)
1034 delta = 0x2ab;
1035 else if (rate == 48000)
1036 delta = 0x1000;
1037 else
1038 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1039 }
1040
1041 return delta;
1042 }
1043
1044 static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
1045 {
1046 unsigned int CTRL;
1047 struct snd_pcm_runtime *runtime = substream->runtime;
1048
1049
1050
1051
1052 CTRL = 0x00000001;
1053 if (snd_pcm_format_width(runtime->format) == 16)
1054 CTRL |= 0x00000008;
1055 if (!snd_pcm_format_unsigned(runtime->format))
1056 CTRL |= 0x00000002;
1057 if (runtime->channels > 1)
1058 CTRL |= 0x00000004;
1059 return CTRL;
1060 }
1061
1062
1063
1064
1065
1066 static int snd_ali_trigger(struct snd_pcm_substream *substream,
1067 int cmd)
1068
1069 {
1070 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1071 struct snd_pcm_substream *s;
1072 unsigned int what, whati;
1073 struct snd_ali_voice *pvoice, *evoice;
1074 unsigned int val;
1075 int do_start;
1076
1077 switch (cmd) {
1078 case SNDRV_PCM_TRIGGER_START:
1079 case SNDRV_PCM_TRIGGER_RESUME:
1080 do_start = 1;
1081 break;
1082 case SNDRV_PCM_TRIGGER_STOP:
1083 case SNDRV_PCM_TRIGGER_SUSPEND:
1084 do_start = 0;
1085 break;
1086 default:
1087 return -EINVAL;
1088 }
1089
1090 what = whati = 0;
1091 snd_pcm_group_for_each_entry(s, substream) {
1092 if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
1093 pvoice = s->runtime->private_data;
1094 evoice = pvoice->extra;
1095 what |= 1 << (pvoice->number & 0x1f);
1096 if (evoice == NULL)
1097 whati |= 1 << (pvoice->number & 0x1f);
1098 else {
1099 whati |= 1 << (evoice->number & 0x1f);
1100 what |= 1 << (evoice->number & 0x1f);
1101 }
1102 if (do_start) {
1103 pvoice->running = 1;
1104 if (evoice != NULL)
1105 evoice->running = 1;
1106 } else {
1107 pvoice->running = 0;
1108 if (evoice != NULL)
1109 evoice->running = 0;
1110 }
1111 snd_pcm_trigger_done(s, substream);
1112 }
1113 }
1114 spin_lock(&codec->reg_lock);
1115 if (!do_start)
1116 outl(what, ALI_REG(codec, ALI_STOP));
1117 val = inl(ALI_REG(codec, ALI_AINTEN));
1118 if (do_start)
1119 val |= whati;
1120 else
1121 val &= ~whati;
1122 outl(val, ALI_REG(codec, ALI_AINTEN));
1123 if (do_start)
1124 outl(what, ALI_REG(codec, ALI_START));
1125 dev_dbg(codec->card->dev, "trigger: what=%xh whati=%xh\n", what, whati);
1126 spin_unlock(&codec->reg_lock);
1127
1128 return 0;
1129 }
1130
1131 static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
1132 struct snd_pcm_hw_params *hw_params)
1133 {
1134 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1135 struct snd_pcm_runtime *runtime = substream->runtime;
1136 struct snd_ali_voice *pvoice = runtime->private_data;
1137 struct snd_ali_voice *evoice = pvoice->extra;
1138
1139
1140
1141 if (params_buffer_size(hw_params) / 2 !=
1142 params_period_size(hw_params)) {
1143 if (!evoice) {
1144 evoice = snd_ali_alloc_voice(codec,
1145 SNDRV_ALI_VOICE_TYPE_PCM,
1146 0, -1);
1147 if (!evoice)
1148 return -ENOMEM;
1149 pvoice->extra = evoice;
1150 evoice->substream = substream;
1151 }
1152 } else {
1153 if (evoice) {
1154 snd_ali_free_voice(codec, evoice);
1155 pvoice->extra = evoice = NULL;
1156 }
1157 }
1158
1159 return 0;
1160 }
1161
1162 static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
1163 {
1164 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1165 struct snd_pcm_runtime *runtime = substream->runtime;
1166 struct snd_ali_voice *pvoice = runtime->private_data;
1167 struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
1168
1169 if (evoice) {
1170 snd_ali_free_voice(codec, evoice);
1171 pvoice->extra = NULL;
1172 }
1173 return 0;
1174 }
1175
1176 static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
1177 {
1178 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1179 struct snd_pcm_runtime *runtime = substream->runtime;
1180 struct snd_ali_voice *pvoice = runtime->private_data;
1181 struct snd_ali_voice *evoice = pvoice->extra;
1182
1183 unsigned int LBA;
1184 unsigned int Delta;
1185 unsigned int ESO;
1186 unsigned int CTRL;
1187 unsigned int GVSEL;
1188 unsigned int PAN;
1189 unsigned int VOL;
1190 unsigned int EC;
1191
1192 dev_dbg(codec->card->dev, "playback_prepare ...\n");
1193
1194 spin_lock_irq(&codec->reg_lock);
1195
1196
1197 Delta = snd_ali_convert_rate(runtime->rate, 0);
1198
1199 if (pvoice->number == ALI_SPDIF_IN_CHANNEL ||
1200 pvoice->number == ALI_PCM_IN_CHANNEL)
1201 snd_ali_disable_special_channel(codec, pvoice->number);
1202 else if (codec->spdif_support &&
1203 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
1204 ALI_SPDIF_OUT_CH_ENABLE)
1205 && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
1206 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1207 Delta = 0x1000;
1208 }
1209
1210
1211 LBA = runtime->dma_addr;
1212
1213
1214 pvoice->count = runtime->period_size;
1215
1216
1217 pvoice->eso = runtime->buffer_size;
1218
1219 dev_dbg(codec->card->dev, "playback_prepare: eso=%xh count=%xh\n",
1220 pvoice->eso, pvoice->count);
1221
1222
1223 ESO = pvoice->eso -1;
1224
1225 CTRL = snd_ali_control_mode(substream);
1226
1227 GVSEL = 1;
1228 PAN = 0;
1229 VOL = 0;
1230 EC = 0;
1231 dev_dbg(codec->card->dev, "playback_prepare:\n");
1232 dev_dbg(codec->card->dev,
1233 "ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
1234 pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1235 snd_ali_write_voice_regs(codec,
1236 pvoice->number,
1237 LBA,
1238 0,
1239 ESO,
1240 Delta,
1241 0,
1242 GVSEL,
1243 PAN,
1244 VOL,
1245 CTRL,
1246 EC);
1247 if (evoice) {
1248 evoice->count = pvoice->count;
1249 evoice->eso = pvoice->count << 1;
1250 ESO = evoice->eso - 1;
1251 snd_ali_write_voice_regs(codec,
1252 evoice->number,
1253 LBA,
1254 0,
1255 ESO,
1256 Delta,
1257 0,
1258 GVSEL,
1259 0x7f,
1260 0x3ff,
1261 CTRL,
1262 EC);
1263 }
1264 spin_unlock_irq(&codec->reg_lock);
1265 return 0;
1266 }
1267
1268
1269 static int snd_ali_prepare(struct snd_pcm_substream *substream)
1270 {
1271 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1272 struct snd_pcm_runtime *runtime = substream->runtime;
1273 struct snd_ali_voice *pvoice = runtime->private_data;
1274 unsigned int LBA;
1275 unsigned int Delta;
1276 unsigned int ESO;
1277 unsigned int CTRL;
1278 unsigned int GVSEL;
1279 unsigned int PAN;
1280 unsigned int VOL;
1281 unsigned int EC;
1282 u8 bValue;
1283
1284 spin_lock_irq(&codec->reg_lock);
1285
1286 dev_dbg(codec->card->dev, "ali_prepare...\n");
1287
1288 snd_ali_enable_special_channel(codec,pvoice->number);
1289
1290 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1291 pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
1292 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
1293
1294
1295 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1296
1297 unsigned int rate;
1298
1299 spin_unlock_irq(&codec->reg_lock);
1300 if (codec->revision != ALI_5451_V02)
1301 return -1;
1302
1303 rate = snd_ali_get_spdif_in_rate(codec);
1304 if (rate == 0) {
1305 dev_warn(codec->card->dev,
1306 "ali_capture_prepare: spdif rate detect err!\n");
1307 rate = 48000;
1308 }
1309 spin_lock_irq(&codec->reg_lock);
1310 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1311 if (bValue & 0x10) {
1312 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1313 dev_warn(codec->card->dev,
1314 "clear SPDIF parity error flag.\n");
1315 }
1316
1317 if (rate != 48000)
1318 Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
1319 }
1320
1321
1322 pvoice->eso = runtime->buffer_size;
1323
1324
1325 pvoice->count = runtime->period_size;
1326
1327
1328 LBA = runtime->dma_addr;
1329
1330
1331 ESO = pvoice->eso - 1;
1332 CTRL = snd_ali_control_mode(substream);
1333 GVSEL = 0;
1334 PAN = 0x00;
1335 VOL = 0x00;
1336 EC = 0;
1337
1338 snd_ali_write_voice_regs( codec,
1339 pvoice->number,
1340 LBA,
1341 0,
1342 ESO,
1343 Delta,
1344 0,
1345 GVSEL,
1346 PAN,
1347 VOL,
1348 CTRL,
1349 EC);
1350
1351 spin_unlock_irq(&codec->reg_lock);
1352
1353 return 0;
1354 }
1355
1356
1357 static snd_pcm_uframes_t
1358 snd_ali_playback_pointer(struct snd_pcm_substream *substream)
1359 {
1360 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1361 struct snd_pcm_runtime *runtime = substream->runtime;
1362 struct snd_ali_voice *pvoice = runtime->private_data;
1363 unsigned int cso;
1364
1365 spin_lock(&codec->reg_lock);
1366 if (!pvoice->running) {
1367 spin_unlock(&codec->reg_lock);
1368 return 0;
1369 }
1370 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1371 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1372 spin_unlock(&codec->reg_lock);
1373 dev_dbg(codec->card->dev, "playback pointer returned cso=%xh.\n", cso);
1374
1375 cso %= runtime->buffer_size;
1376 return cso;
1377 }
1378
1379
1380 static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
1381 {
1382 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1383 struct snd_pcm_runtime *runtime = substream->runtime;
1384 struct snd_ali_voice *pvoice = runtime->private_data;
1385 unsigned int cso;
1386
1387 spin_lock(&codec->reg_lock);
1388 if (!pvoice->running) {
1389 spin_unlock(&codec->reg_lock);
1390 return 0;
1391 }
1392 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1393 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1394 spin_unlock(&codec->reg_lock);
1395
1396 cso %= runtime->buffer_size;
1397 return cso;
1398 }
1399
1400 static const struct snd_pcm_hardware snd_ali_playback =
1401 {
1402 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1403 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1404 SNDRV_PCM_INFO_MMAP_VALID |
1405 SNDRV_PCM_INFO_RESUME |
1406 SNDRV_PCM_INFO_SYNC_START),
1407 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1408 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1409 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1410 .rate_min = 4000,
1411 .rate_max = 48000,
1412 .channels_min = 1,
1413 .channels_max = 2,
1414 .buffer_bytes_max = (256*1024),
1415 .period_bytes_min = 64,
1416 .period_bytes_max = (256*1024),
1417 .periods_min = 1,
1418 .periods_max = 1024,
1419 .fifo_size = 0,
1420 };
1421
1422
1423
1424
1425
1426 static const struct snd_pcm_hardware snd_ali_capture =
1427 {
1428 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1429 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1430 SNDRV_PCM_INFO_MMAP_VALID |
1431 SNDRV_PCM_INFO_RESUME |
1432 SNDRV_PCM_INFO_SYNC_START),
1433 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1434 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1435 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1436 .rate_min = 4000,
1437 .rate_max = 48000,
1438 .channels_min = 1,
1439 .channels_max = 2,
1440 .buffer_bytes_max = (128*1024),
1441 .period_bytes_min = 64,
1442 .period_bytes_max = (128*1024),
1443 .periods_min = 1,
1444 .periods_max = 1024,
1445 .fifo_size = 0,
1446 };
1447
1448 static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
1449 {
1450 struct snd_ali_voice *pvoice = runtime->private_data;
1451
1452 if (pvoice)
1453 snd_ali_free_voice(pvoice->codec, pvoice);
1454 }
1455
1456 static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
1457 int channel, const struct snd_pcm_hardware *phw)
1458 {
1459 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1460 struct snd_pcm_runtime *runtime = substream->runtime;
1461 struct snd_ali_voice *pvoice;
1462
1463 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
1464 channel);
1465 if (!pvoice)
1466 return -EAGAIN;
1467
1468 pvoice->substream = substream;
1469 runtime->private_data = pvoice;
1470 runtime->private_free = snd_ali_pcm_free_substream;
1471
1472 runtime->hw = *phw;
1473 snd_pcm_set_sync(substream);
1474 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1475 0, 64*1024);
1476 return 0;
1477 }
1478
1479 static int snd_ali_playback_open(struct snd_pcm_substream *substream)
1480 {
1481 return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1482 }
1483
1484 static int snd_ali_capture_open(struct snd_pcm_substream *substream)
1485 {
1486 return snd_ali_open(substream, 1, -1, &snd_ali_capture);
1487 }
1488
1489 static int snd_ali_playback_close(struct snd_pcm_substream *substream)
1490 {
1491 return 0;
1492 }
1493
1494 static int snd_ali_close(struct snd_pcm_substream *substream)
1495 {
1496 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1497 struct snd_ali_voice *pvoice = substream->runtime->private_data;
1498
1499 snd_ali_disable_special_channel(codec,pvoice->number);
1500
1501 return 0;
1502 }
1503
1504 static const struct snd_pcm_ops snd_ali_playback_ops = {
1505 .open = snd_ali_playback_open,
1506 .close = snd_ali_playback_close,
1507 .hw_params = snd_ali_playback_hw_params,
1508 .hw_free = snd_ali_playback_hw_free,
1509 .prepare = snd_ali_playback_prepare,
1510 .trigger = snd_ali_trigger,
1511 .pointer = snd_ali_playback_pointer,
1512 };
1513
1514 static const struct snd_pcm_ops snd_ali_capture_ops = {
1515 .open = snd_ali_capture_open,
1516 .close = snd_ali_close,
1517 .prepare = snd_ali_prepare,
1518 .trigger = snd_ali_trigger,
1519 .pointer = snd_ali_pointer,
1520 };
1521
1522
1523
1524
1525
1526 static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
1527 struct snd_pcm_hw_params *hw_params)
1528 {
1529 struct snd_ali *chip = snd_pcm_substream_chip(substream);
1530 unsigned int modem_num = chip->num_of_codecs - 1;
1531 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
1532 params_rate(hw_params));
1533 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1534 return 0;
1535 }
1536
1537 static const struct snd_pcm_hardware snd_ali_modem =
1538 {
1539 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1540 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1541 SNDRV_PCM_INFO_MMAP_VALID |
1542 SNDRV_PCM_INFO_RESUME |
1543 SNDRV_PCM_INFO_SYNC_START),
1544 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1545 .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
1546 SNDRV_PCM_RATE_16000),
1547 .rate_min = 8000,
1548 .rate_max = 16000,
1549 .channels_min = 1,
1550 .channels_max = 1,
1551 .buffer_bytes_max = (256*1024),
1552 .period_bytes_min = 64,
1553 .period_bytes_max = (256*1024),
1554 .periods_min = 1,
1555 .periods_max = 1024,
1556 .fifo_size = 0,
1557 };
1558
1559 static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
1560 int channel)
1561 {
1562 static const unsigned int rates[] = {8000, 9600, 12000, 16000};
1563 static const struct snd_pcm_hw_constraint_list hw_constraint_rates = {
1564 .count = ARRAY_SIZE(rates),
1565 .list = rates,
1566 .mask = 0,
1567 };
1568 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
1569
1570 if (err)
1571 return err;
1572 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1573 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1574 }
1575
1576 static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
1577 {
1578 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1579 }
1580
1581 static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
1582 {
1583 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1584 }
1585
1586 static const struct snd_pcm_ops snd_ali_modem_playback_ops = {
1587 .open = snd_ali_modem_playback_open,
1588 .close = snd_ali_close,
1589 .hw_params = snd_ali_modem_hw_params,
1590 .prepare = snd_ali_prepare,
1591 .trigger = snd_ali_trigger,
1592 .pointer = snd_ali_pointer,
1593 };
1594
1595 static const struct snd_pcm_ops snd_ali_modem_capture_ops = {
1596 .open = snd_ali_modem_capture_open,
1597 .close = snd_ali_close,
1598 .hw_params = snd_ali_modem_hw_params,
1599 .prepare = snd_ali_prepare,
1600 .trigger = snd_ali_trigger,
1601 .pointer = snd_ali_pointer,
1602 };
1603
1604
1605 struct ali_pcm_description {
1606 char *name;
1607 unsigned int playback_num;
1608 unsigned int capture_num;
1609 const struct snd_pcm_ops *playback_ops;
1610 const struct snd_pcm_ops *capture_ops;
1611 unsigned short class;
1612 };
1613
1614
1615 static void snd_ali_pcm_free(struct snd_pcm *pcm)
1616 {
1617 struct snd_ali *codec = pcm->private_data;
1618 codec->pcm[pcm->device] = NULL;
1619 }
1620
1621
1622 static int snd_ali_pcm(struct snd_ali *codec, int device,
1623 struct ali_pcm_description *desc)
1624 {
1625 struct snd_pcm *pcm;
1626 int err;
1627
1628 err = snd_pcm_new(codec->card, desc->name, device,
1629 desc->playback_num, desc->capture_num, &pcm);
1630 if (err < 0) {
1631 dev_err(codec->card->dev,
1632 "snd_ali_pcm: err called snd_pcm_new.\n");
1633 return err;
1634 }
1635 pcm->private_data = codec;
1636 pcm->private_free = snd_ali_pcm_free;
1637 if (desc->playback_ops)
1638 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1639 desc->playback_ops);
1640 if (desc->capture_ops)
1641 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1642 desc->capture_ops);
1643
1644 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1645 &codec->pci->dev, 64*1024, 128*1024);
1646
1647 pcm->info_flags = 0;
1648 pcm->dev_class = desc->class;
1649 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1650 strcpy(pcm->name, desc->name);
1651 codec->pcm[0] = pcm;
1652 return 0;
1653 }
1654
1655 static struct ali_pcm_description ali_pcms[] = {
1656 { .name = "ALI 5451",
1657 .playback_num = ALI_CHANNELS,
1658 .capture_num = 1,
1659 .playback_ops = &snd_ali_playback_ops,
1660 .capture_ops = &snd_ali_capture_ops
1661 },
1662 { .name = "ALI 5451 modem",
1663 .playback_num = 1,
1664 .capture_num = 1,
1665 .playback_ops = &snd_ali_modem_playback_ops,
1666 .capture_ops = &snd_ali_modem_capture_ops,
1667 .class = SNDRV_PCM_CLASS_MODEM
1668 }
1669 };
1670
1671 static int snd_ali_build_pcms(struct snd_ali *codec)
1672 {
1673 int i, err;
1674 for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
1675 err = snd_ali_pcm(codec, i, &ali_pcms[i]);
1676 if (err < 0)
1677 return err;
1678 }
1679 return 0;
1680 }
1681
1682
1683 #define ALI5451_SPDIF(xname, xindex, value) \
1684 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1685 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1686 .put = snd_ali5451_spdif_put, .private_value = value}
1687
1688 #define snd_ali5451_spdif_info snd_ctl_boolean_mono_info
1689
1690 static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
1691 struct snd_ctl_elem_value *ucontrol)
1692 {
1693 struct snd_ali *codec = kcontrol->private_data;
1694 unsigned int spdif_enable;
1695
1696 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
1697
1698 spin_lock_irq(&codec->reg_lock);
1699 switch (kcontrol->private_value) {
1700 case 0:
1701 spdif_enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1702 break;
1703 case 1:
1704 spdif_enable = ((codec->spdif_mask & 0x02) &&
1705 (codec->spdif_mask & 0x04)) ? 1 : 0;
1706 break;
1707 case 2:
1708 spdif_enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1709 break;
1710 default:
1711 break;
1712 }
1713 ucontrol->value.integer.value[0] = spdif_enable;
1714 spin_unlock_irq(&codec->reg_lock);
1715 return 0;
1716 }
1717
1718 static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
1719 struct snd_ctl_elem_value *ucontrol)
1720 {
1721 struct snd_ali *codec = kcontrol->private_data;
1722 unsigned int change = 0, spdif_enable = 0;
1723
1724 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
1725
1726 spin_lock_irq(&codec->reg_lock);
1727 switch (kcontrol->private_value) {
1728 case 0:
1729 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1730 change = change ^ spdif_enable;
1731 if (change) {
1732 if (spdif_enable) {
1733 codec->spdif_mask |= 0x02;
1734 snd_ali_enable_spdif_out(codec);
1735 } else {
1736 codec->spdif_mask &= ~(0x02);
1737 codec->spdif_mask &= ~(0x04);
1738 snd_ali_disable_spdif_out(codec);
1739 }
1740 }
1741 break;
1742 case 1:
1743 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1744 change = change ^ spdif_enable;
1745 if (change && (codec->spdif_mask & 0x02)) {
1746 if (spdif_enable) {
1747 codec->spdif_mask |= 0x04;
1748 snd_ali_enable_spdif_chnout(codec);
1749 } else {
1750 codec->spdif_mask &= ~(0x04);
1751 snd_ali_disable_spdif_chnout(codec);
1752 }
1753 }
1754 break;
1755 case 2:
1756 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1757 change = change ^ spdif_enable;
1758 if (change) {
1759 if (spdif_enable) {
1760 codec->spdif_mask |= 0x01;
1761 snd_ali_enable_spdif_in(codec);
1762 } else {
1763 codec->spdif_mask &= ~(0x01);
1764 snd_ali_disable_spdif_in(codec);
1765 }
1766 }
1767 break;
1768 default:
1769 break;
1770 }
1771 spin_unlock_irq(&codec->reg_lock);
1772
1773 return change;
1774 }
1775
1776 static const struct snd_kcontrol_new snd_ali5451_mixer_spdif[] = {
1777
1778
1779 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
1780
1781 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
1782
1783 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1784 };
1785
1786 static int snd_ali_mixer(struct snd_ali *codec)
1787 {
1788 struct snd_ac97_template ac97;
1789 unsigned int idx;
1790 int i, err;
1791 static const struct snd_ac97_bus_ops ops = {
1792 .write = snd_ali_codec_write,
1793 .read = snd_ali_codec_read,
1794 };
1795
1796 err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
1797 if (err < 0)
1798 return err;
1799
1800 memset(&ac97, 0, sizeof(ac97));
1801 ac97.private_data = codec;
1802
1803 for (i = 0; i < codec->num_of_codecs; i++) {
1804 ac97.num = i;
1805 err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
1806 if (err < 0) {
1807 dev_err(codec->card->dev,
1808 "ali mixer %d creating error.\n", i);
1809 if (i == 0)
1810 return err;
1811 codec->num_of_codecs = 1;
1812 break;
1813 }
1814 }
1815
1816 if (codec->spdif_support) {
1817 for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1818 err = snd_ctl_add(codec->card,
1819 snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1820 if (err < 0)
1821 return err;
1822 }
1823 }
1824 return 0;
1825 }
1826
1827 #ifdef CONFIG_PM_SLEEP
1828 static int ali_suspend(struct device *dev)
1829 {
1830 struct snd_card *card = dev_get_drvdata(dev);
1831 struct snd_ali *chip = card->private_data;
1832 struct snd_ali_image *im;
1833 int i, j;
1834
1835 im = chip->image;
1836 if (!im)
1837 return 0;
1838
1839 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1840 for (i = 0; i < chip->num_of_codecs; i++)
1841 snd_ac97_suspend(chip->ac97[i]);
1842
1843 spin_lock_irq(&chip->reg_lock);
1844
1845 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
1846
1847 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1848
1849
1850 outl(0, ALI_REG(chip, ALI_MISCINT));
1851
1852 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1853 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1854 continue;
1855 im->regs[i] = inl(ALI_REG(chip, i*4));
1856 }
1857
1858 for (i = 0; i < ALI_CHANNELS; i++) {
1859 outb(i, ALI_REG(chip, ALI_GC_CIR));
1860 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1861 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1862 }
1863
1864
1865 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1866
1867 spin_unlock_irq(&chip->reg_lock);
1868 return 0;
1869 }
1870
1871 static int ali_resume(struct device *dev)
1872 {
1873 struct snd_card *card = dev_get_drvdata(dev);
1874 struct snd_ali *chip = card->private_data;
1875 struct snd_ali_image *im;
1876 int i, j;
1877
1878 im = chip->image;
1879 if (!im)
1880 return 0;
1881
1882 spin_lock_irq(&chip->reg_lock);
1883
1884 for (i = 0; i < ALI_CHANNELS; i++) {
1885 outb(i, ALI_REG(chip, ALI_GC_CIR));
1886 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1887 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
1888 }
1889
1890 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1891 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
1892 (i*4 == ALI_START))
1893 continue;
1894 outl(im->regs[i], ALI_REG(chip, i*4));
1895 }
1896
1897
1898 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
1899
1900 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
1901
1902 spin_unlock_irq(&chip->reg_lock);
1903
1904 for (i = 0 ; i < chip->num_of_codecs; i++)
1905 snd_ac97_resume(chip->ac97[i]);
1906
1907 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1908 return 0;
1909 }
1910
1911 static SIMPLE_DEV_PM_OPS(ali_pm, ali_suspend, ali_resume);
1912 #define ALI_PM_OPS &ali_pm
1913 #else
1914 #define ALI_PM_OPS NULL
1915 #endif
1916
1917 static void snd_ali_free(struct snd_card *card)
1918 {
1919 struct snd_ali *codec = card->private_data;
1920
1921 if (codec->hw_initialized)
1922 snd_ali_disable_address_interrupt(codec);
1923 pci_dev_put(codec->pci_m1533);
1924 pci_dev_put(codec->pci_m7101);
1925 }
1926
1927 static int snd_ali_chip_init(struct snd_ali *codec)
1928 {
1929 unsigned int legacy;
1930 unsigned char temp;
1931 struct pci_dev *pci_dev;
1932
1933 dev_dbg(codec->card->dev, "chip initializing ...\n");
1934
1935 if (snd_ali_reset_5451(codec)) {
1936 dev_err(codec->card->dev, "ali_chip_init: reset 5451 error.\n");
1937 return -1;
1938 }
1939
1940 if (codec->revision == ALI_5451_V02) {
1941 pci_dev = codec->pci_m1533;
1942 pci_read_config_byte(pci_dev, 0x59, &temp);
1943 temp |= 0x80;
1944 pci_write_config_byte(pci_dev, 0x59, temp);
1945
1946 pci_dev = codec->pci_m7101;
1947 pci_read_config_byte(pci_dev, 0xb8, &temp);
1948 temp |= 0x20;
1949 pci_write_config_byte(pci_dev, 0xB8, temp);
1950 }
1951
1952 pci_read_config_dword(codec->pci, 0x44, &legacy);
1953 legacy &= 0xff00ff00;
1954 legacy |= 0x000800aa;
1955 pci_write_config_dword(codec->pci, 0x44, legacy);
1956
1957 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
1958 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
1959 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
1960 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
1961 outb(0x10, ALI_REG(codec, ALI_MPUR2));
1962
1963 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
1964 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
1965 AC97_EXTENDED_STATUS);
1966 if (codec->spdif_support) {
1967 snd_ali_enable_spdif_out(codec);
1968 codec->spdif_mask = 0x00000002;
1969 }
1970
1971 codec->num_of_codecs = 1;
1972
1973
1974 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
1975 codec->num_of_codecs++;
1976 outl(inl(ALI_REG(codec, ALI_SCTRL)) |
1977 (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
1978 ALI_SCTRL_LINE_OUT_EN),
1979 ALI_REG(codec, ALI_SCTRL));
1980 }
1981
1982 dev_dbg(codec->card->dev, "chip initialize succeed.\n");
1983 return 0;
1984
1985 }
1986
1987
1988 static void snd_ali_proc_read(struct snd_info_entry *entry,
1989 struct snd_info_buffer *buf)
1990 {
1991 struct snd_ali *codec = entry->private_data;
1992 int i;
1993 for (i = 0; i < 256 ; i+= 4)
1994 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
1995 }
1996
1997 static void snd_ali_proc_init(struct snd_ali *codec)
1998 {
1999 snd_card_ro_proc_new(codec->card, "ali5451", codec, snd_ali_proc_read);
2000 }
2001
2002 static int snd_ali_resources(struct snd_ali *codec)
2003 {
2004 int err;
2005
2006 dev_dbg(codec->card->dev, "resources allocation ...\n");
2007 err = pci_request_regions(codec->pci, "ALI 5451");
2008 if (err < 0)
2009 return err;
2010 codec->port = pci_resource_start(codec->pci, 0);
2011
2012 if (devm_request_irq(&codec->pci->dev, codec->pci->irq,
2013 snd_ali_card_interrupt,
2014 IRQF_SHARED, KBUILD_MODNAME, codec)) {
2015 dev_err(codec->card->dev, "Unable to request irq.\n");
2016 return -EBUSY;
2017 }
2018 codec->irq = codec->pci->irq;
2019 codec->card->sync_irq = codec->irq;
2020 dev_dbg(codec->card->dev, "resources allocated.\n");
2021 return 0;
2022 }
2023
2024 static int snd_ali_create(struct snd_card *card,
2025 struct pci_dev *pci,
2026 int pcm_streams,
2027 int spdif_support)
2028 {
2029 struct snd_ali *codec = card->private_data;
2030 int i, err;
2031 unsigned short cmdw;
2032
2033 dev_dbg(card->dev, "creating ...\n");
2034
2035
2036 err = pcim_enable_device(pci);
2037 if (err < 0)
2038 return err;
2039
2040 if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(31))) {
2041 dev_err(card->dev,
2042 "architecture does not support 31bit PCI busmaster DMA\n");
2043 return -ENXIO;
2044 }
2045
2046 spin_lock_init(&codec->reg_lock);
2047 spin_lock_init(&codec->voice_alloc);
2048
2049 codec->card = card;
2050 codec->pci = pci;
2051 codec->irq = -1;
2052 codec->revision = pci->revision;
2053 codec->spdif_support = spdif_support;
2054
2055 if (pcm_streams < 1)
2056 pcm_streams = 1;
2057 if (pcm_streams > 32)
2058 pcm_streams = 32;
2059
2060 pci_set_master(pci);
2061 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2062 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2063 cmdw |= PCI_COMMAND_IO;
2064 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2065 }
2066
2067 if (snd_ali_resources(codec))
2068 return -EBUSY;
2069 card->private_free = snd_ali_free;
2070
2071 codec->synth.chmap = 0;
2072 codec->synth.chcnt = 0;
2073 codec->spdif_mask = 0;
2074 codec->synth.synthcount = 0;
2075
2076 if (codec->revision == ALI_5451_V02)
2077 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2078 else
2079 codec->chregs.regs.ac97read = ALI_AC97_READ;
2080 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2081
2082 codec->chregs.regs.start = ALI_START;
2083 codec->chregs.regs.stop = ALI_STOP;
2084 codec->chregs.regs.aint = ALI_AINT;
2085 codec->chregs.regs.ainten = ALI_AINTEN;
2086
2087 codec->chregs.data.start = 0x00;
2088 codec->chregs.data.stop = 0x00;
2089 codec->chregs.data.aint = 0x00;
2090 codec->chregs.data.ainten = 0x00;
2091
2092
2093 codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
2094 if (!codec->pci_m1533) {
2095 dev_err(card->dev, "cannot find ALi 1533 chip.\n");
2096 return -ENODEV;
2097 }
2098
2099 codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
2100 if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2101 dev_err(card->dev, "cannot find ALi 7101 chip.\n");
2102 return -ENODEV;
2103 }
2104
2105
2106 for (i = 0; i < ALI_CHANNELS; i++)
2107 codec->synth.voices[i].number = i;
2108
2109 err = snd_ali_chip_init(codec);
2110 if (err < 0) {
2111 dev_err(card->dev, "ali create: chip init error.\n");
2112 return err;
2113 }
2114
2115 #ifdef CONFIG_PM_SLEEP
2116 codec->image = devm_kmalloc(&pci->dev, sizeof(*codec->image),
2117 GFP_KERNEL);
2118 if (!codec->image)
2119 dev_warn(card->dev, "can't allocate apm buffer\n");
2120 #endif
2121
2122 snd_ali_enable_address_interrupt(codec);
2123 codec->hw_initialized = 1;
2124 return 0;
2125 }
2126
2127 static int __snd_ali_probe(struct pci_dev *pci,
2128 const struct pci_device_id *pci_id)
2129 {
2130 struct snd_card *card;
2131 struct snd_ali *codec;
2132 int err;
2133
2134 dev_dbg(&pci->dev, "probe ...\n");
2135
2136 err = snd_devm_card_new(&pci->dev, index, id, THIS_MODULE,
2137 sizeof(*codec), &card);
2138 if (err < 0)
2139 return err;
2140 codec = card->private_data;
2141
2142 err = snd_ali_create(card, pci, pcm_channels, spdif);
2143 if (err < 0)
2144 return err;
2145
2146 dev_dbg(&pci->dev, "mixer building ...\n");
2147 err = snd_ali_mixer(codec);
2148 if (err < 0)
2149 return err;
2150
2151 dev_dbg(&pci->dev, "pcm building ...\n");
2152 err = snd_ali_build_pcms(codec);
2153 if (err < 0)
2154 return err;
2155
2156 snd_ali_proc_init(codec);
2157
2158 strcpy(card->driver, "ALI5451");
2159 strcpy(card->shortname, "ALI 5451");
2160
2161 sprintf(card->longname, "%s at 0x%lx, irq %i",
2162 card->shortname, codec->port, codec->irq);
2163
2164 dev_dbg(&pci->dev, "register card.\n");
2165 err = snd_card_register(card);
2166 if (err < 0)
2167 return err;
2168
2169 pci_set_drvdata(pci, card);
2170 return 0;
2171 }
2172
2173 static int snd_ali_probe(struct pci_dev *pci,
2174 const struct pci_device_id *pci_id)
2175 {
2176 return snd_card_free_on_error(&pci->dev, __snd_ali_probe(pci, pci_id));
2177 }
2178
2179 static struct pci_driver ali5451_driver = {
2180 .name = KBUILD_MODNAME,
2181 .id_table = snd_ali_ids,
2182 .probe = snd_ali_probe,
2183 .driver = {
2184 .pm = ALI_PM_OPS,
2185 },
2186 };
2187
2188 module_pci_driver(ali5451_driver);