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