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