blob: 9327ab2eccb0c704a951a2a59b51f1c5eb28e2df [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>
Matthias Gehre910638a2006-03-28 01:56:48 -080036#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <sound/core.h>
38#include <sound/pcm.h>
39#include <sound/info.h>
40#include <sound/ac97_codec.h>
41#include <sound/mpu401.h>
42#include <sound/initval.h>
43
44MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
45MODULE_DESCRIPTION("ALI M5451");
46MODULE_LICENSE("GPL");
47MODULE_SUPPORTED_DEVICE("{{ALI,M5451,pci},{ALI,M5451}}");
48
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020049static int index = SNDRV_DEFAULT_IDX1; /* Index */
50static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
51static int pcm_channels = 32;
Takashi Iwai6581f4e2006-05-17 17:14:51 +020052static int spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020054module_param(index, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020056module_param(id, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020058module_param(pcm_channels, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059MODULE_PARM_DESC(pcm_channels, "PCM Channels");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020060module_param(spdif, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
62
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020063/* just for backward compatibility */
64static int enable;
65module_param(enable, bool, 0444);
66
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
69 * Debug part definitions
70 */
71
72//#define ALI_DEBUG
73
74#ifdef ALI_DEBUG
75#define snd_ali_printk(format, args...) printk(format, ##args);
76#else
77#define snd_ali_printk(format, args...)
78#endif
79
80/*
81 * Constants definition
82 */
83
Takashi Iwai8cdfd252005-09-07 14:08:11 +020084#define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86
87#define ALI_CHANNELS 32
88
89#define ALI_PCM_IN_CHANNEL 31
90#define ALI_SPDIF_IN_CHANNEL 19
91#define ALI_SPDIF_OUT_CHANNEL 15
92#define ALI_CENTER_CHANNEL 24
93#define ALI_LEF_CHANNEL 23
94#define ALI_SURR_LEFT_CHANNEL 26
95#define ALI_SURR_RIGHT_CHANNEL 25
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +020096#define ALI_MODEM_IN_CHANNEL 21
97#define ALI_MODEM_OUT_CHANNEL 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99#define SNDRV_ALI_VOICE_TYPE_PCM 01
100#define SNDRV_ALI_VOICE_TYPE_OTH 02
101
102#define ALI_5451_V02 0x02
103
104/*
105 * Direct Registers
106 */
107
108#define ALI_LEGACY_DMAR0 0x00 // ADR0
109#define ALI_LEGACY_DMAR4 0x04 // CNT0
110#define ALI_LEGACY_DMAR11 0x0b // MOD
111#define ALI_LEGACY_DMAR15 0x0f // MMR
112#define ALI_MPUR0 0x20
113#define ALI_MPUR1 0x21
114#define ALI_MPUR2 0x22
115#define ALI_MPUR3 0x23
116
117#define ALI_AC97_WRITE 0x40
118#define ALI_AC97_READ 0x44
119
120#define ALI_SCTRL 0x48
121#define ALI_SPDIF_OUT_ENABLE 0x20
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200122#define ALI_SCTRL_LINE_IN2 (1 << 9)
123#define ALI_SCTRL_GPIO_IN2 (1 << 13)
124#define ALI_SCTRL_LINE_OUT_EN (1 << 20)
125#define ALI_SCTRL_GPIO_OUT_EN (1 << 23)
126#define ALI_SCTRL_CODEC1_READY (1 << 24)
127#define ALI_SCTRL_CODEC2_READY (1 << 25)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define ALI_AC97_GPIO 0x4c
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200129#define ALI_AC97_GPIO_ENABLE 0x8000
130#define ALI_AC97_GPIO_DATA_SHIFT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define ALI_SPDIF_CS 0x70
132#define ALI_SPDIF_CTRL 0x74
133#define ALI_SPDIF_IN_FUNC_ENABLE 0x02
134#define ALI_SPDIF_IN_CH_STATUS 0x40
135#define ALI_SPDIF_OUT_CH_STATUS 0xbf
136#define ALI_START 0x80
137#define ALI_STOP 0x84
138#define ALI_CSPF 0x90
139#define ALI_AINT 0x98
140#define ALI_GC_CIR 0xa0
141 #define ENDLP_IE 0x00001000
142 #define MIDLP_IE 0x00002000
143#define ALI_AINTEN 0xa4
144#define ALI_VOLUME 0xa8
145#define ALI_SBDELTA_DELTA_R 0xac
146#define ALI_MISCINT 0xb0
147 #define ADDRESS_IRQ 0x00000020
148 #define TARGET_REACHED 0x00008000
149 #define MIXER_OVERFLOW 0x00000800
150 #define MIXER_UNDERFLOW 0x00000400
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200151 #define GPIO_IRQ 0x01000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#define ALI_SBBL_SBCL 0xc0
153#define ALI_SBCTRL_SBE2R_SBDD 0xc4
154#define ALI_STIMER 0xc8
155#define ALI_GLOBAL_CONTROL 0xd4
156#define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
157#define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
158#define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
159#define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
160#define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
161
162#define ALI_CSO_ALPHA_FMS 0xe0
163#define ALI_LBA 0xe4
164#define ALI_ESO_DELTA 0xe8
165#define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
166#define ALI_EBUF1 0xf4
167#define ALI_EBUF2 0xf8
168
169#define ALI_REG(codec, x) ((codec)->port + x)
170
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200171#define MAX_CODECS 2
172
173
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100174struct snd_ali;
175struct snd_ali_voice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100177struct snd_ali_channel_control {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 // register data
179 struct REGDATA {
180 unsigned int start;
181 unsigned int stop;
182 unsigned int aint;
183 unsigned int ainten;
184 } data;
185
186 // register addresses
187 struct REGS {
188 unsigned int start;
189 unsigned int stop;
190 unsigned int aint;
191 unsigned int ainten;
192 unsigned int ac97read;
193 unsigned int ac97write;
194 } regs;
195
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100196};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100198struct snd_ali_voice {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 unsigned int number;
200 unsigned int use: 1,
201 pcm: 1,
202 midi: 1,
203 mode: 1,
204 synth: 1;
205
206 /* PCM data */
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100207 struct snd_ali *codec;
208 struct snd_pcm_substream *substream;
209 struct snd_ali_voice *extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 unsigned int running: 1;
212
213 int eso; /* final ESO value for channel */
214 int count; /* runtime->period_size */
215
216 /* --- */
217
218 void *private_data;
219 void (*private_free)(void *private_data);
220};
221
222
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100223struct snd_alidev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100225 struct snd_ali_voice voices[ALI_CHANNELS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 unsigned int chcnt; /* num of opened channels */
228 unsigned int chmap; /* bitmap for opened channels */
229 unsigned int synthcount;
230
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100231};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233
234#ifdef CONFIG_PM
235#define ALI_GLOBAL_REGS 56
236#define ALI_CHANNEL_REGS 8
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100237struct snd_ali_image {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 unsigned long regs[ALI_GLOBAL_REGS];
239 unsigned long channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100240};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241#endif
242
243
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100244struct snd_ali {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 unsigned long irq;
246 unsigned long port;
247 unsigned char revision;
248
249 unsigned int hw_initialized: 1;
250 unsigned int spdif_support: 1;
251
252 struct pci_dev *pci;
253 struct pci_dev *pci_m1533;
254 struct pci_dev *pci_m7101;
255
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100256 struct snd_card *card;
257 struct snd_pcm *pcm[MAX_CODECS];
258 struct snd_alidev synth;
259 struct snd_ali_channel_control chregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /* S/PDIF Mask */
262 unsigned int spdif_mask;
263
264 unsigned int spurious_irq_count;
265 unsigned int spurious_irq_max_delta;
266
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200267 unsigned int num_of_codecs;
268
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100269 struct snd_ac97_bus *ac97_bus;
270 struct snd_ac97 *ac97[MAX_CODECS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 unsigned short ac97_ext_id;
272 unsigned short ac97_ext_status;
273
274 spinlock_t reg_lock;
275 spinlock_t voice_alloc;
276
277#ifdef CONFIG_PM
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100278 struct snd_ali_image *image;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279#endif
280};
281
Takashi Iwaif40b6892006-07-05 16:51:05 +0200282static struct pci_device_id snd_ali_ids[] = {
Jon Masonec6c8c32006-01-13 13:17:43 +0100283 {PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 {0, }
285};
286MODULE_DEVICE_TABLE(pci, snd_ali_ids);
287
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100288static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
289static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
290static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short, unsigned short);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292/*
293 * Debug Part
294 */
295
296#ifdef ALI_DEBUG
297
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100298static void ali_read_regs(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 int i,j;
301 unsigned int dwVal;
302
303 printk("channel %d registers map:\n", channel);
304 outb((unsigned char)(channel & 0x001f), ALI_REG(codec,ALI_GC_CIR));
305
306 printk(" ");
307 for(j=0;j<8;j++)
308 printk("%2.2x ", j*4);
309 printk("\n");
310
311 for (i=0; i<=0xf8/4;i++) {
312 if(i%8 == 0)
313 printk("%2.2x ", (i*4/0x10)*0x10);
314 dwVal = inl(ALI_REG(codec,i*4));
315 printk("%8.8x ", dwVal);
316 if ((i+1)%8 == 0)
317 printk("\n");
318 }
319 printk("\n");
320}
321static void ali_read_cfg(unsigned int vendor, unsigned deviceid)
322{
323 unsigned int dwVal;
Jiri Slaby0dd119f2005-09-07 14:28:33 +0200324 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int i,j;
326
Jiri Slaby0dd119f2005-09-07 14:28:33 +0200327 pci_dev = pci_get_device(vendor, deviceid, NULL);
328 if (pci_dev == NULL)
329 return ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 printk("\nM%x PCI CFG\n", deviceid);
332 printk(" ");
333 for(j=0;j<8;j++)
334 printk("%d ",j);
335 printk("\n");
336
337 for(i=0;i<8;i++) {
338 printk("%d ",i);
339 for(j=0;j<8;j++)
340 {
341 pci_read_config_dword(pci_dev, i*0x20+j*4, &dwVal);
342 printk("%8.8x ", dwVal);
343 }
344 printk("\n");
345 }
Jiri Slaby0dd119f2005-09-07 14:28:33 +0200346 pci_dev_put(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100348static void ali_read_ac97regs(struct snd_ali *codec, int secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 unsigned short i,j;
351 unsigned short wVal;
352
353 printk("\ncodec %d registers map:\n", secondary);
354
355 printk(" ");
356 for(j=0;j<8;j++)
357 printk("%2.2x ",j*2);
358 printk("\n");
359
360 for (i=0; i<64;i++) {
361 if(i%8 == 0)
362 printk("%2.2x ", (i/8)*0x10);
363 wVal = snd_ali_codec_peek(codec, secondary, i*2);
364 printk("%4.4x ", wVal);
365 if ((i+1)%8 == 0)
366 printk("\n");
367 }
368 printk("\n");
369}
370
371#endif
372
373/*
374 * AC97 ACCESS
375 */
376
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100377static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 unsigned int port )
379{
380 return (unsigned int)inl(ALI_REG(codec, port));
381}
382
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100383static inline void snd_ali_5451_poke( struct snd_ali *codec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 unsigned int port,
385 unsigned int val )
386{
387 outl((unsigned int)val, ALI_REG(codec, port));
388}
389
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100390static int snd_ali_codec_ready( struct snd_ali *codec,
Takashi Iwai755e1372005-11-11 21:05:27 +0100391 unsigned int port )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 unsigned long end_time;
394 unsigned int res;
395
Takashi Iwai755e1372005-11-11 21:05:27 +0100396 end_time = jiffies + msecs_to_jiffies(250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 do {
398 res = snd_ali_5451_peek(codec,port);
399 if (! (res & 0x8000))
400 return 0;
Takashi Iwai755e1372005-11-11 21:05:27 +0100401 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 } while (time_after_eq(end_time, jiffies));
403 snd_ali_5451_poke(codec, port, res & ~0x8000);
404 snd_printdd("ali_codec_ready: codec is not ready.\n ");
405 return -EIO;
406}
407
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100408static int snd_ali_stimer_ready(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 unsigned long end_time;
411 unsigned long dwChk1,dwChk2;
412
413 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
414 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
415
Takashi Iwai755e1372005-11-11 21:05:27 +0100416 end_time = jiffies + msecs_to_jiffies(250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 do {
418 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
419 if (dwChk2 != dwChk1)
420 return 0;
Takashi Iwai755e1372005-11-11 21:05:27 +0100421 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 } while (time_after_eq(end_time, jiffies));
Takashi Iwai99b359b2005-10-20 18:26:44 +0200423 snd_printk(KERN_ERR "ali_stimer_read: stimer is not ready.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return -EIO;
425}
426
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100427static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 unsigned short reg,
429 unsigned short val)
430{
431 unsigned int dwVal = 0;
432 unsigned int port = 0;
433
434 if (reg >= 0x80) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200435 snd_printk(KERN_ERR "ali_codec_poke: reg(%xh) invalid.\n", reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return;
437 }
438
439 port = codec->chregs.regs.ac97write;
440
Takashi Iwai755e1372005-11-11 21:05:27 +0100441 if (snd_ali_codec_ready(codec, port) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return;
Takashi Iwai755e1372005-11-11 21:05:27 +0100443 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return;
445
446 dwVal = (unsigned int) (reg & 0xff);
447 dwVal |= 0x8000 | (val << 16);
448 if (secondary) dwVal |= 0x0080;
449 if (codec->revision == ALI_5451_V02) dwVal |= 0x0100;
450
451 snd_ali_5451_poke(codec,port,dwVal);
452
453 return ;
454}
455
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100456static unsigned short snd_ali_codec_peek( struct snd_ali *codec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 int secondary,
458 unsigned short reg)
459{
460 unsigned int dwVal = 0;
461 unsigned int port = 0;
462
463 if (reg >= 0x80) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200464 snd_printk(KERN_ERR "ali_codec_peek: reg(%xh) invalid.\n", reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return ~0;
466 }
467
468 port = codec->chregs.regs.ac97read;
469
Takashi Iwai755e1372005-11-11 21:05:27 +0100470 if (snd_ali_codec_ready(codec, port) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return ~0;
Takashi Iwai755e1372005-11-11 21:05:27 +0100472 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return ~0;
474
475 dwVal = (unsigned int) (reg & 0xff);
476 dwVal |= 0x8000; /* bit 15*/
477 if (secondary) dwVal |= 0x0080;
478
479 snd_ali_5451_poke(codec, port, dwVal);
480
Takashi Iwai755e1372005-11-11 21:05:27 +0100481 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return ~0;
Takashi Iwai755e1372005-11-11 21:05:27 +0100483 if (snd_ali_codec_ready(codec, port) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return ~0;
485
486 return (snd_ali_5451_peek(codec, port) & 0xffff0000)>>16;
487}
488
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100489static void snd_ali_codec_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 unsigned short reg,
491 unsigned short val )
492{
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100493 struct snd_ali *codec = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200496 if(reg == AC97_GPIO_STATUS) {
497 outl((val << ALI_AC97_GPIO_DATA_SHIFT)|ALI_AC97_GPIO_ENABLE,
498 ALI_REG(codec, ALI_AC97_GPIO));
499 return;
500 }
501 snd_ali_codec_poke(codec, ac97->num, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return ;
503}
504
505
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100506static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100508 struct snd_ali *codec = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 snd_ali_printk("codec_read reg=%xh.\n", reg);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200511 return (snd_ali_codec_peek(codec, ac97->num, reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514/*
515 * AC97 Reset
516 */
517
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100518static int snd_ali_reset_5451(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 struct pci_dev *pci_dev = NULL;
521 unsigned short wCount, wReg;
522 unsigned int dwVal;
523
524 if ((pci_dev = codec->pci_m1533) != NULL) {
525 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
526 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
527 udelay(5000);
528 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
529 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
530 udelay(5000);
531 }
532
533 pci_dev = codec->pci;
534 pci_read_config_dword(pci_dev, 0x44, &dwVal);
535 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
536 udelay(500);
537 pci_read_config_dword(pci_dev, 0x44, &dwVal);
538 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
539 udelay(5000);
540
541 wCount = 200;
542 while(wCount--) {
543 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
544 if((wReg & 0x000f) == 0x000f)
545 return 0;
546 udelay(5000);
547 }
548
549 /* non-fatal if you have a non PM capable codec */
550 /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
551 return 0;
552}
553
554#ifdef CODEC_RESET
555
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100556static int snd_ali_reset_codec(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct pci_dev *pci_dev = NULL;
559 unsigned char bVal = 0;
560 unsigned int dwVal;
561 unsigned short wCount, wReg;
562
563 pci_dev = codec->pci_m1533;
564
565 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
566 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
567 udelay(5000);
568 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
569 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
570 udelay(5000);
571
572 bVal = inb(ALI_REG(codec,ALI_SCTRL));
573 bVal |= 0x02;
574 outb(ALI_REG(codec,ALI_SCTRL),bVal);
575 udelay(5000);
576 bVal = inb(ALI_REG(codec,ALI_SCTRL));
577 bVal &= 0xfd;
578 outb(ALI_REG(codec,ALI_SCTRL),bVal);
579 udelay(15000);
580
581 wCount = 200;
582 while(wCount--) {
583 wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
584 if((wReg & 0x000f) == 0x000f)
585 return 0;
586 udelay(5000);
587 }
588 return -1;
589}
590
591#endif
592
593/*
594 * ALI 5451 Controller
595 */
596
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100597static void snd_ali_enable_special_channel(struct snd_ali *codec, unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 unsigned long dwVal = 0;
600
601 dwVal = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
602 dwVal |= 1 << (channel & 0x0000001f);
603 outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
604}
605
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100606static void snd_ali_disable_special_channel(struct snd_ali *codec, unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 unsigned long dwVal = 0;
609
610 dwVal = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
611 dwVal &= ~(1 << (channel & 0x0000001f));
612 outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
613}
614
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100615static void snd_ali_enable_address_interrupt(struct snd_ali * codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 unsigned int gc;
618
619 gc = inl(ALI_REG(codec, ALI_GC_CIR));
620 gc |= ENDLP_IE;
621 gc |= MIDLP_IE;
622 outl( gc, ALI_REG(codec, ALI_GC_CIR));
623}
624
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100625static void snd_ali_disable_address_interrupt(struct snd_ali * codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 unsigned int gc;
628
629 gc = inl(ALI_REG(codec, ALI_GC_CIR));
630 gc &= ~ENDLP_IE;
631 gc &= ~MIDLP_IE;
632 outl(gc, ALI_REG(codec, ALI_GC_CIR));
633}
634
635#if 0 // not used
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100636static void snd_ali_enable_voice_irq(struct snd_ali *codec, unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 unsigned int mask;
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100639 struct snd_ali_channel_control *pchregs = &(codec->chregs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 snd_ali_printk("enable_voice_irq channel=%d\n",channel);
642
643 mask = 1 << (channel & 0x1f);
644 pchregs->data.ainten = inl(ALI_REG(codec,pchregs->regs.ainten));
645 pchregs->data.ainten |= mask;
646 outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
647}
648#endif
649
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100650static void snd_ali_disable_voice_irq(struct snd_ali *codec, unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 unsigned int mask;
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100653 struct snd_ali_channel_control *pchregs = &(codec->chregs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 snd_ali_printk("disable_voice_irq channel=%d\n",channel);
656
657 mask = 1 << (channel & 0x1f);
658 pchregs->data.ainten = inl(ALI_REG(codec,pchregs->regs.ainten));
659 pchregs->data.ainten &= ~mask;
660 outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
661}
662
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100663static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 unsigned int idx = channel & 0x1f;
666
667 if (codec->synth.chcnt >= ALI_CHANNELS){
Takashi Iwai99b359b2005-10-20 18:26:44 +0200668 snd_printk(KERN_ERR "ali_alloc_pcm_channel: no free channels.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return -1;
670 }
671
672 if (!(codec->synth.chmap & (1 << idx))) {
673 codec->synth.chmap |= 1 << idx;
674 codec->synth.chcnt++;
675 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
676 return idx;
677 }
678 return -1;
679}
680
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100681static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 int idx;
684 int result = -1;
685
686 snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
687
688 // recording
689 if (rec) {
690 if (codec->spdif_support &&
691 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_IN_SUPPORT))
692 idx = ALI_SPDIF_IN_CHANNEL;
693 else
694 idx = ALI_PCM_IN_CHANNEL;
695
696 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
697 return result;
698 } else {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200699 snd_printk(KERN_ERR "ali_find_free_channel: record channel is busy now.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return -1;
701 }
702 }
703
704 //playback...
705 if (codec->spdif_support &&
706 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)) {
707 idx = ALI_SPDIF_OUT_CHANNEL;
708 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
709 return result;
710 } else {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200711 snd_printk(KERN_ERR "ali_find_free_channel: S/PDIF out channel is in busy now.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713 }
714
715 for (idx = 0; idx < ALI_CHANNELS; idx++) {
716 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0)
717 return result;
718 }
Takashi Iwai99b359b2005-10-20 18:26:44 +0200719 snd_printk(KERN_ERR "ali_find_free_channel: no free channels.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return -1;
721}
722
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100723static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 unsigned int idx = channel & 0x0000001f;
726
727 snd_ali_printk("free_channel_pcm channel=%d\n",channel);
728
729 if (channel < 0 || channel >= ALI_CHANNELS)
730 return;
731
732 if (!(codec->synth.chmap & (1 << idx))) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200733 snd_printk(KERN_ERR "ali_free_channel_pcm: channel %d is not in use.\n",channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return;
735 } else {
736 codec->synth.chmap &= ~(1 << idx);
737 codec->synth.chcnt--;
738 }
739}
740
741#if 0 // not used
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100742static void snd_ali_start_voice(struct snd_ali * codec, unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 unsigned int mask = 1 << (channel & 0x1f);
745
746 snd_ali_printk("start_voice: channel=%d\n",channel);
747 outl(mask, ALI_REG(codec,codec->chregs.regs.start));
748}
749#endif
750
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100751static void snd_ali_stop_voice(struct snd_ali * codec, unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 unsigned int mask = 1 << (channel & 0x1f);
754
755 snd_ali_printk("stop_voice: channel=%d\n",channel);
756 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
757}
758
759/*
760 * S/PDIF Part
761 */
762
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100763static void snd_ali_delay(struct snd_ali *codec,int interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 unsigned long begintimer,currenttimer;
766
767 begintimer = inl(ALI_REG(codec, ALI_STIMER));
768 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
769
770 while (currenttimer < begintimer + interval) {
Takashi Iwai755e1372005-11-11 21:05:27 +0100771 if(snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 break;
773 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
774 }
775}
776
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100777static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 u16 wval = 0;
780 u16 count = 0;
781 u8 bval = 0, R1 = 0, R2 = 0;
782
783 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
784 bval |= 0x1F;
785 outb(bval,ALI_REG(codec,ALI_SPDIF_CTRL + 1));
786
787 while (((R1 < 0x0B )||(R1 > 0x0E)) && (R1 != 0x12) && count <= 50000) {
788 count ++;
789 snd_ali_delay(codec, 6);
790 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
791 R1 = bval & 0x1F;
792 }
793
794 if (count > 50000) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200795 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return;
797 }
798
799 count = 0;
800 while (count++ <= 50000) {
801 snd_ali_delay(codec, 6);
802 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
803 R2 = bval & 0x1F;
804 if (R2 != R1) R1 = R2; else break;
805 }
806
807 if (count > 50000) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200808 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return;
810 }
811
812 if (R2 >= 0x0b && R2 <= 0x0e) {
813 wval = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
814 wval &= 0xE0F0;
815 wval |= (u16)0x09 << 8 | (u16)0x05;
816 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
817
818 bval = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
819 outb(bval|0x02,ALI_REG(codec,ALI_SPDIF_CS + 3));
820 } else if (R2 == 0x12) {
821 wval = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
822 wval &= 0xE0F0;
823 wval |= (u16)0x0E << 8 | (u16)0x08;
824 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
825
826 bval = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
827 outb(bval|0x03,ALI_REG(codec,ALI_SPDIF_CS + 3));
828 }
829}
830
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100831static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 u32 dwRate = 0;
834 u8 bval = 0;
835
836 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
837 bval &= 0x7F;
838 bval |= 0x40;
839 outb(bval, ALI_REG(codec,ALI_SPDIF_CTRL));
840
841 snd_ali_detect_spdif_rate(codec);
842
843 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3));
844 bval &= 0x0F;
845
846 if (bval == 0) dwRate = 44100;
847 if (bval == 1) dwRate = 48000;
848 if (bval == 2) dwRate = 32000;
849
850 return dwRate;
851}
852
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100853static void snd_ali_enable_spdif_in(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 unsigned int dwVal;
856
857 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
858 dwVal |= ALI_SPDIF_IN_SUPPORT;
859 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
860
861 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
862 dwVal |= 0x02;
863 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
864
865 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
866}
867
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100868static void snd_ali_disable_spdif_in(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 unsigned int dwVal;
871
872 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
873 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
874 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
875
876 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
877}
878
879
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100880static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 unsigned char bVal;
883 unsigned int dwRate = 0;
884
885 if (rate == 32000) dwRate = 0x300;
886 if (rate == 44100) dwRate = 0;
887 if (rate == 48000) dwRate = 0x200;
888
889 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
890 bVal &= (unsigned char)(~(1<<6));
891
892 bVal |= 0x80; //select right
893 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
894 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
895
896 bVal &= (~0x80); //select left
897 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
898 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
899}
900
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100901static void snd_ali_enable_spdif_out(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 unsigned short wVal;
904 unsigned char bVal;
905
906 struct pci_dev *pci_dev = NULL;
907
908 pci_dev = codec->pci_m1533;
909 if (pci_dev == NULL)
910 return;
911 pci_read_config_byte(pci_dev, 0x61, &bVal);
912 bVal |= 0x40;
913 pci_write_config_byte(pci_dev, 0x61, bVal);
914 pci_read_config_byte(pci_dev, 0x7d, &bVal);
915 bVal |= 0x01;
916 pci_write_config_byte(pci_dev, 0x7d, bVal);
917
918 pci_read_config_byte(pci_dev, 0x7e, &bVal);
919 bVal &= (~0x20);
920 bVal |= 0x10;
921 pci_write_config_byte(pci_dev, 0x7e, bVal);
922
923 bVal = inb(ALI_REG(codec, ALI_SCTRL));
924 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
925
926 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
927 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
928
929 {
930 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
931 wVal |= ALI_SPDIF_OUT_SEL_PCM;
932 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
933 snd_ali_disable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
934 }
935}
936
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100937static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 unsigned short wVal = 0;
940
941 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
942 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
943 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
944/*
945 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
946 if (flag & ALI_SPDIF_OUT_NON_PCM)
947 wVal |= 0x0002;
948 else
949 wVal &= (~0x0002);
950 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
951*/
952 snd_ali_enable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
953}
954
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100955static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 unsigned short wVal = 0;
958 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
959 wVal |= ALI_SPDIF_OUT_SEL_PCM;
960 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
961
962 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
963}
964
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100965static void snd_ali_disable_spdif_out(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 unsigned char bVal;
968
969 bVal = inb(ALI_REG(codec, ALI_SCTRL));
970 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
971
972 snd_ali_disable_spdif_chnout(codec);
973}
974
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100975static void snd_ali_update_ptr(struct snd_ali *codec,int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100977 struct snd_ali_voice *pvoice = NULL;
978 struct snd_pcm_runtime *runtime;
979 struct snd_ali_channel_control *pchregs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 unsigned int old, mask;
981#ifdef ALI_DEBUG
982 unsigned int temp, cspf;
983#endif
984
985 pchregs = &(codec->chregs);
986
987 // check if interrupt occurred for channel
988 old = pchregs->data.aint;
989 mask = ((unsigned int) 1L) << (channel & 0x1f);
990
991 if (!(old & mask))
992 return;
993
994 pvoice = &codec->synth.voices[channel];
995 runtime = pvoice->substream->runtime;
996
997 udelay(100);
998 spin_lock(&codec->reg_lock);
999
1000 if (pvoice->pcm && pvoice->substream) {
1001 /* pcm interrupt */
1002#ifdef ALI_DEBUG
1003 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
1004 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1005 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
1006#endif
1007 if (pvoice->running) {
1008 snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",(u16)temp,cspf);
1009 spin_unlock(&codec->reg_lock);
1010 snd_pcm_period_elapsed(pvoice->substream);
1011 spin_lock(&codec->reg_lock);
1012 } else {
1013 snd_ali_stop_voice(codec, channel);
1014 snd_ali_disable_voice_irq(codec, channel);
1015 }
1016 } else if (codec->synth.voices[channel].synth) {
1017 /* synth interrupt */
1018 } else if (codec->synth.voices[channel].midi) {
1019 /* midi interrupt */
1020 } else {
1021 /* unknown interrupt */
1022 snd_ali_stop_voice(codec, channel);
1023 snd_ali_disable_voice_irq(codec, channel);
1024 }
1025 spin_unlock(&codec->reg_lock);
1026 outl(mask,ALI_REG(codec,pchregs->regs.aint));
1027 pchregs->data.aint = old & (~mask);
1028}
1029
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001030static void snd_ali_interrupt(struct snd_ali * codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 int channel;
1033 unsigned int audio_int;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001034 struct snd_ali_channel_control *pchregs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 pchregs = &(codec->chregs);
1036
1037 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
1038 if (audio_int & ADDRESS_IRQ) {
1039 // get interrupt status for all channels
1040 pchregs->data.aint = inl(ALI_REG(codec,pchregs->regs.aint));
1041 for (channel = 0; channel < ALI_CHANNELS; channel++) {
1042 snd_ali_update_ptr(codec, channel);
1043 }
1044 }
1045 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
1046 ALI_REG(codec,ALI_MISCINT));
1047}
1048
1049
David Howells7d12e782006-10-05 14:55:46 +01001050static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001052 struct snd_ali *codec = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (codec == NULL)
1055 return IRQ_NONE;
1056 snd_ali_interrupt(codec);
1057 return IRQ_HANDLED;
1058}
1059
1060
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001061static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec, int type, int rec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001063 struct snd_ali_voice *pvoice = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 int idx;
1065
1066 snd_ali_printk("alloc_voice: type=%d rec=%d\n",type,rec);
1067
Takashi Iwai755e1372005-11-11 21:05:27 +01001068 spin_lock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001070 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
1071 snd_ali_find_free_channel(codec,rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if(idx < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001073 snd_printk(KERN_ERR "ali_alloc_voice: err.\n");
Takashi Iwai755e1372005-11-11 21:05:27 +01001074 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return NULL;
1076 }
1077 pvoice = &(codec->synth.voices[idx]);
Takashi Iwai755e1372005-11-11 21:05:27 +01001078 pvoice->codec = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 pvoice->use = 1;
1080 pvoice->pcm = 1;
1081 pvoice->mode = rec;
Takashi Iwai755e1372005-11-11 21:05:27 +01001082 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return pvoice;
1084 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001085 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return NULL;
1087}
1088
1089
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001090static void snd_ali_free_voice(struct snd_ali * codec, struct snd_ali_voice *pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 void (*private_free)(void *);
1093 void *private_data;
1094
1095 snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
1096 if (pvoice == NULL || !pvoice->use)
1097 return;
1098 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
Takashi Iwai755e1372005-11-11 21:05:27 +01001099 spin_lock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 private_free = pvoice->private_free;
1101 private_data = pvoice->private_data;
1102 pvoice->private_free = NULL;
1103 pvoice->private_data = NULL;
1104 if (pvoice->pcm) {
1105 snd_ali_free_channel_pcm(codec, pvoice->number);
1106 }
1107 pvoice->use = pvoice->pcm = pvoice->synth = 0;
1108 pvoice->substream = NULL;
Takashi Iwai755e1372005-11-11 21:05:27 +01001109 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (private_free)
1111 private_free(private_data);
1112}
1113
1114
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001115static void snd_ali_clear_voices(struct snd_ali * codec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 unsigned int v_min,
1117 unsigned int v_max)
1118{
1119 unsigned int i;
1120
1121 for (i = v_min; i <= v_max; i++) {
1122 snd_ali_stop_voice(codec, i);
1123 snd_ali_disable_voice_irq(codec, i);
1124 }
1125}
1126
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001127static void snd_ali_write_voice_regs(struct snd_ali * codec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 unsigned int Channel,
1129 unsigned int LBA,
1130 unsigned int CSO,
1131 unsigned int ESO,
1132 unsigned int DELTA,
1133 unsigned int ALPHA_FMS,
1134 unsigned int GVSEL,
1135 unsigned int PAN,
1136 unsigned int VOL,
1137 unsigned int CTRL,
1138 unsigned int EC)
1139{
1140 unsigned int ctlcmds[4];
1141
1142 outb((unsigned char)(Channel & 0x001f),ALI_REG(codec,ALI_GC_CIR));
1143
1144 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1145 ctlcmds[1] = LBA;
1146 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1147 ctlcmds[3] = (GVSEL << 31) |
1148 ((PAN & 0x0000007f) << 24) |
1149 ((VOL & 0x000000ff) << 16) |
1150 ((CTRL & 0x0000000f) << 12) |
1151 (EC & 0x00000fff);
1152
1153 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1154
1155 outl(ctlcmds[0], ALI_REG(codec,ALI_CSO_ALPHA_FMS));
1156 outl(ctlcmds[1], ALI_REG(codec,ALI_LBA));
1157 outl(ctlcmds[2], ALI_REG(codec,ALI_ESO_DELTA));
1158 outl(ctlcmds[3], ALI_REG(codec,ALI_GVSEL_PAN_VOC_CTRL_EC));
1159
1160 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1161 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1162}
1163
1164static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1165{
1166 unsigned int delta;
1167
1168 if (rate < 4000) rate = 4000;
1169 if (rate > 48000) rate = 48000;
1170
1171 if (rec) {
1172 if (rate == 44100)
1173 delta = 0x116a;
1174 else if (rate == 8000)
1175 delta = 0x6000;
1176 else if (rate == 48000)
1177 delta = 0x1000;
1178 else
1179 delta = ((48000 << 12) / rate) & 0x0000ffff;
1180 } else {
1181 if (rate == 44100)
1182 delta = 0xeb3;
1183 else if (rate == 8000)
1184 delta = 0x2ab;
1185 else if (rate == 48000)
1186 delta = 0x1000;
1187 else
1188 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1189 }
1190
1191 return delta;
1192}
1193
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001194static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 unsigned int CTRL;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001197 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 /* set ctrl mode
1200 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1201 */
1202 CTRL = 0x00000001;
1203 if (snd_pcm_format_width(runtime->format) == 16)
1204 CTRL |= 0x00000008; // 16-bit data
1205 if (!snd_pcm_format_unsigned(runtime->format))
1206 CTRL |= 0x00000002; // signed data
1207 if (runtime->channels > 1)
1208 CTRL |= 0x00000004; // stereo data
1209 return CTRL;
1210}
1211
1212/*
1213 * PCM part
1214 */
1215
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001216static int snd_ali_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 unsigned int cmd, void *arg)
1218{
1219 return snd_pcm_lib_ioctl(substream, cmd, arg);
1220}
1221
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001222static int snd_ali_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 int cmd)
1224
1225{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001226 struct snd_ali *codec = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 struct list_head *pos;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001228 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 unsigned int what, whati, capture_flag;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001230 struct snd_ali_voice *pvoice = NULL, *evoice = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 unsigned int val;
1232 int do_start;
1233
1234 switch (cmd) {
1235 case SNDRV_PCM_TRIGGER_START:
1236 case SNDRV_PCM_TRIGGER_RESUME:
1237 do_start = 1; break;
1238 case SNDRV_PCM_TRIGGER_STOP:
1239 case SNDRV_PCM_TRIGGER_SUSPEND:
1240 do_start = 0; break;
1241 default:
1242 return -EINVAL;
1243 }
1244
1245 what = whati = capture_flag = 0;
1246 snd_pcm_group_for_each(pos, substream) {
1247 s = snd_pcm_group_substream_entry(pos);
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001248 if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
1249 pvoice = s->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 evoice = pvoice->extra;
1251 what |= 1 << (pvoice->number & 0x1f);
1252 if (evoice == NULL) {
1253 whati |= 1 << (pvoice->number & 0x1f);
1254 } else {
1255 whati |= 1 << (evoice->number & 0x1f);
1256 what |= 1 << (evoice->number & 0x1f);
1257 }
1258 if (do_start) {
1259 pvoice->running = 1;
1260 if (evoice != NULL)
1261 evoice->running = 1;
1262 } else {
1263 pvoice->running = 0;
1264 if (evoice != NULL)
1265 evoice->running = 0;
1266 }
1267 snd_pcm_trigger_done(s, substream);
1268 if (pvoice->mode)
1269 capture_flag = 1;
1270 }
1271 }
1272 spin_lock(&codec->reg_lock);
1273 if (! do_start) {
1274 outl(what, ALI_REG(codec, ALI_STOP));
1275 }
1276 val = inl(ALI_REG(codec, ALI_AINTEN));
1277 if (do_start) {
1278 val |= whati;
1279 } else {
1280 val &= ~whati;
1281 }
1282 outl(val, ALI_REG(codec, ALI_AINTEN));
1283 if (do_start) {
1284 outl(what, ALI_REG(codec, ALI_START));
1285 }
1286 snd_ali_printk("trigger: what=%xh whati=%xh\n",what,whati);
1287 spin_unlock(&codec->reg_lock);
1288
1289 return 0;
1290}
1291
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001292static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
1293 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001295 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1296 struct snd_pcm_runtime *runtime = substream->runtime;
1297 struct snd_ali_voice *pvoice = runtime->private_data;
1298 struct snd_ali_voice *evoice = pvoice->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 int err;
1300 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1301 if (err < 0) return err;
1302
1303 /* voice management */
1304
1305 if (params_buffer_size(hw_params)/2 != params_period_size(hw_params)) {
1306 if (evoice == NULL) {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001307 evoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (evoice == NULL)
1309 return -ENOMEM;
1310 pvoice->extra = evoice;
1311 evoice->substream = substream;
1312 }
1313 } else {
1314 if (evoice != NULL) {
1315 snd_ali_free_voice(codec, evoice);
1316 pvoice->extra = evoice = NULL;
1317 }
1318 }
1319
1320 return 0;
1321}
1322
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001323static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001325 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1326 struct snd_pcm_runtime *runtime = substream->runtime;
1327 struct snd_ali_voice *pvoice = runtime->private_data;
1328 struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 snd_pcm_lib_free_pages(substream);
1331 if (evoice != NULL) {
1332 snd_ali_free_voice(codec, evoice);
1333 pvoice->extra = NULL;
1334 }
1335 return 0;
1336}
1337
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001338static int snd_ali_hw_params(struct snd_pcm_substream *substream,
1339 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1342}
1343
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001344static int snd_ali_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 return snd_pcm_lib_free_pages(substream);
1347}
1348
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001349static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001351 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1352 struct snd_pcm_runtime *runtime = substream->runtime;
1353 struct snd_ali_voice *pvoice = runtime->private_data;
1354 struct snd_ali_voice *evoice = pvoice->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 unsigned int LBA;
1357 unsigned int Delta;
1358 unsigned int ESO;
1359 unsigned int CTRL;
1360 unsigned int GVSEL;
1361 unsigned int PAN;
1362 unsigned int VOL;
1363 unsigned int EC;
1364
1365 snd_ali_printk("playback_prepare ...\n");
1366
Takashi Iwai755e1372005-11-11 21:05:27 +01001367 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 /* set Delta (rate) value */
1370 Delta = snd_ali_convert_rate(runtime->rate, 0);
1371
1372 if ((pvoice->number == ALI_SPDIF_IN_CHANNEL) ||
1373 (pvoice->number == ALI_PCM_IN_CHANNEL))
1374 snd_ali_disable_special_channel(codec, pvoice->number);
1375 else if (codec->spdif_support &&
1376 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)
1377 && (pvoice->number == ALI_SPDIF_OUT_CHANNEL)) {
1378 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1379 Delta = 0x1000;
1380 }
1381
1382 /* set Loop Back Address */
1383 LBA = runtime->dma_addr;
1384
1385 /* set interrupt count size */
1386 pvoice->count = runtime->period_size;
1387
1388 /* set target ESO for channel */
1389 pvoice->eso = runtime->buffer_size;
1390
1391 snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",pvoice->eso,pvoice->count);
1392
1393 /* set ESO to capture first MIDLP interrupt */
1394 ESO = pvoice->eso -1;
1395 /* set ctrl mode */
1396 CTRL = snd_ali_control_mode(substream);
1397
1398 GVSEL = 1;
1399 PAN = 0;
1400 VOL = 0;
1401 EC = 0;
1402 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);
1403 snd_ali_write_voice_regs( codec,
1404 pvoice->number,
1405 LBA,
1406 0, /* cso */
1407 ESO,
1408 Delta,
1409 0, /* alpha */
1410 GVSEL,
1411 PAN,
1412 VOL,
1413 CTRL,
1414 EC);
1415 if (evoice != NULL) {
1416 evoice->count = pvoice->count;
1417 evoice->eso = pvoice->count << 1;
1418 ESO = evoice->eso - 1;
1419 snd_ali_write_voice_regs(codec,
1420 evoice->number,
1421 LBA,
1422 0, /* cso */
1423 ESO,
1424 Delta,
1425 0, /* alpha */
1426 GVSEL,
1427 (unsigned int)0x7f,
1428 (unsigned int)0x3ff,
1429 CTRL,
1430 EC);
1431 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001432 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return 0;
1434}
1435
1436
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001437static int snd_ali_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001439 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1440 struct snd_pcm_runtime *runtime = substream->runtime;
1441 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 unsigned int LBA;
1443 unsigned int Delta;
1444 unsigned int ESO;
1445 unsigned int CTRL;
1446 unsigned int GVSEL;
1447 unsigned int PAN;
1448 unsigned int VOL;
1449 unsigned int EC;
1450 u8 bValue;
1451
Takashi Iwai755e1372005-11-11 21:05:27 +01001452 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001454 snd_ali_printk("ali_prepare...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 snd_ali_enable_special_channel(codec,pvoice->number);
1457
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001458 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1459 pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
1460 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 // Prepare capture intr channel
1463 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1464
1465 unsigned int rate;
1466
Takashi Iwai755e1372005-11-11 21:05:27 +01001467 spin_unlock_irq(&codec->reg_lock);
1468 if (codec->revision != ALI_5451_V02)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return -1;
Takashi Iwai755e1372005-11-11 21:05:27 +01001470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 rate = snd_ali_get_spdif_in_rate(codec);
1472 if (rate == 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001473 snd_printk(KERN_WARNING "ali_capture_preapre: spdif rate detect err!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 rate = 48000;
1475 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001476 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1478 if (bValue & 0x10) {
1479 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
Takashi Iwai99b359b2005-10-20 18:26:44 +02001480 printk(KERN_WARNING "clear SPDIF parity error flag.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482
1483 if (rate != 48000)
1484 Delta = ((rate << 12)/runtime->rate)&0x00ffff;
1485 }
1486
1487 // set target ESO for channel
1488 pvoice->eso = runtime->buffer_size;
1489
1490 // set interrupt count size
1491 pvoice->count = runtime->period_size;
1492
1493 // set Loop Back Address
1494 LBA = runtime->dma_addr;
1495
1496 // set ESO to capture first MIDLP interrupt
1497 ESO = pvoice->eso - 1;
1498 CTRL = snd_ali_control_mode(substream);
1499 GVSEL = 0;
1500 PAN = 0x00;
1501 VOL = 0x00;
1502 EC = 0;
1503
1504 snd_ali_write_voice_regs( codec,
1505 pvoice->number,
1506 LBA,
1507 0, /* cso */
1508 ESO,
1509 Delta,
1510 0, /* alpha */
1511 GVSEL,
1512 PAN,
1513 VOL,
1514 CTRL,
1515 EC);
1516
1517
Takashi Iwai755e1372005-11-11 21:05:27 +01001518 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 return 0;
1521}
1522
1523
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001524static snd_pcm_uframes_t snd_ali_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001526 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1527 struct snd_pcm_runtime *runtime = substream->runtime;
1528 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 unsigned int cso;
1530
1531 spin_lock(&codec->reg_lock);
1532 if (!pvoice->running) {
1533 spin_unlock(&codec->reg_lock);
1534 return 0;
1535 }
1536 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1537 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1538 spin_unlock(&codec->reg_lock);
1539 snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1540
1541 return cso;
1542}
1543
1544
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001545static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001547 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1548 struct snd_pcm_runtime *runtime = substream->runtime;
1549 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 unsigned int cso;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Takashi Iwai755e1372005-11-11 21:05:27 +01001552 spin_lock(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (!pvoice->running) {
Takashi Iwai755e1372005-11-11 21:05:27 +01001554 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return 0;
1556 }
1557 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1558 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
Takashi Iwai755e1372005-11-11 21:05:27 +01001559 spin_unlock(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 return cso;
1562}
1563
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001564static struct snd_pcm_hardware snd_ali_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1567 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1568 SNDRV_PCM_INFO_MMAP_VALID |
1569 SNDRV_PCM_INFO_RESUME |
1570 SNDRV_PCM_INFO_SYNC_START),
1571 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1572 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1573 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1574 .rate_min = 4000,
1575 .rate_max = 48000,
1576 .channels_min = 1,
1577 .channels_max = 2,
1578 .buffer_bytes_max = (256*1024),
1579 .period_bytes_min = 64,
1580 .period_bytes_max = (256*1024),
1581 .periods_min = 1,
1582 .periods_max = 1024,
1583 .fifo_size = 0,
1584};
1585
1586/*
1587 * Capture support device description
1588 */
1589
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001590static struct snd_pcm_hardware snd_ali_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1593 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1594 SNDRV_PCM_INFO_MMAP_VALID |
1595 SNDRV_PCM_INFO_RESUME |
1596 SNDRV_PCM_INFO_SYNC_START),
1597 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1598 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1599 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1600 .rate_min = 4000,
1601 .rate_max = 48000,
1602 .channels_min = 1,
1603 .channels_max = 2,
1604 .buffer_bytes_max = (128*1024),
1605 .period_bytes_min = 64,
1606 .period_bytes_max = (128*1024),
1607 .periods_min = 1,
1608 .periods_max = 1024,
1609 .fifo_size = 0,
1610};
1611
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001612static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001614 struct snd_ali_voice *pvoice = runtime->private_data;
1615 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 if (pvoice) {
1618 codec = pvoice->codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 snd_ali_free_voice(pvoice->codec, pvoice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
1621}
1622
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001623static int snd_ali_open(struct snd_pcm_substream *substream, int rec, int channel,
1624 struct snd_pcm_hardware *phw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001626 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1627 struct snd_pcm_runtime *runtime = substream->runtime;
1628 struct snd_ali_voice *pvoice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001630 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec, channel);
Takashi Iwai755e1372005-11-11 21:05:27 +01001631 if (pvoice == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 pvoice->substream = substream;
1635 runtime->private_data = pvoice;
1636 runtime->private_free = snd_ali_pcm_free_substream;
1637
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001638 runtime->hw = *phw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 snd_pcm_set_sync(substream);
1640 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1641 return 0;
1642}
1643
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001644static int snd_ali_playback_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001645{
1646 return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1647}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001649static int snd_ali_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001651 return snd_ali_open(substream, 1, -1, &snd_ali_capture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001654static int snd_ali_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
1656 return 0;
1657}
1658
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001659static int snd_ali_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001661 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1662 struct snd_ali_voice *pvoice = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 snd_ali_disable_special_channel(codec,pvoice->number);
1665
1666 return 0;
1667}
1668
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001669static struct snd_pcm_ops snd_ali_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 .open = snd_ali_playback_open,
1671 .close = snd_ali_playback_close,
1672 .ioctl = snd_ali_ioctl,
1673 .hw_params = snd_ali_playback_hw_params,
1674 .hw_free = snd_ali_playback_hw_free,
1675 .prepare = snd_ali_playback_prepare,
1676 .trigger = snd_ali_trigger,
1677 .pointer = snd_ali_playback_pointer,
1678};
1679
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001680static struct snd_pcm_ops snd_ali_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 .open = snd_ali_capture_open,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001682 .close = snd_ali_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 .ioctl = snd_ali_ioctl,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001684 .hw_params = snd_ali_hw_params,
1685 .hw_free = snd_ali_hw_free,
1686 .prepare = snd_ali_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 .trigger = snd_ali_trigger,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001688 .pointer = snd_ali_pointer,
1689};
1690
1691/*
1692 * Modem PCM
1693 */
1694
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001695static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
1696 struct snd_pcm_hw_params *hw_params)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001697{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001698 struct snd_ali *chip = snd_pcm_substream_chip(substream);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001699 unsigned int modem_num = chip->num_of_codecs - 1;
1700 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE, params_rate(hw_params));
1701 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1702 return snd_ali_hw_params(substream, hw_params);
1703}
1704
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001705static struct snd_pcm_hardware snd_ali_modem =
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001706{
1707 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1708 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1709 SNDRV_PCM_INFO_MMAP_VALID |
1710 SNDRV_PCM_INFO_RESUME |
1711 SNDRV_PCM_INFO_SYNC_START),
1712 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1713 .rates = SNDRV_PCM_RATE_KNOT|SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000,
1714 .rate_min = 8000,
1715 .rate_max = 16000,
1716 .channels_min = 1,
1717 .channels_max = 1,
1718 .buffer_bytes_max = (256*1024),
1719 .period_bytes_min = 64,
1720 .period_bytes_max = (256*1024),
1721 .periods_min = 1,
1722 .periods_max = 1024,
1723 .fifo_size = 0,
1724};
1725
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001726static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec, int channel)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001727{
1728 static unsigned int rates [] = {8000,9600,12000,16000};
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001729 static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001730 .count = ARRAY_SIZE(rates),
1731 .list = rates,
1732 .mask = 0,
1733 };
1734 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
1735 if (err)
1736 return err;
1737 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1738 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1739}
1740
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001741static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001742{
1743 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1744}
1745
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001746static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001747{
1748 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1749}
1750
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001751static struct snd_pcm_ops snd_ali_modem_playback_ops = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001752 .open = snd_ali_modem_playback_open,
1753 .close = snd_ali_close,
1754 .ioctl = snd_pcm_lib_ioctl,
1755 .hw_params = snd_ali_modem_hw_params,
1756 .hw_free = snd_ali_hw_free,
1757 .prepare = snd_ali_prepare,
1758 .trigger = snd_ali_trigger,
1759 .pointer = snd_ali_pointer,
1760};
1761
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001762static struct snd_pcm_ops snd_ali_modem_capture_ops = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001763 .open = snd_ali_modem_capture_open,
1764 .close = snd_ali_close,
1765 .ioctl = snd_pcm_lib_ioctl,
1766 .hw_params = snd_ali_modem_hw_params,
1767 .hw_free = snd_ali_hw_free,
1768 .prepare = snd_ali_prepare,
1769 .trigger = snd_ali_trigger,
1770 .pointer = snd_ali_pointer,
1771};
1772
1773
1774struct ali_pcm_description {
1775 char *name;
1776 unsigned int playback_num;
1777 unsigned int capture_num;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001778 struct snd_pcm_ops *playback_ops;
1779 struct snd_pcm_ops *capture_ops;
Sasha Khapyorsky6632d192005-09-29 11:48:17 +02001780 unsigned short class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781};
1782
1783
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001784static void snd_ali_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001786 struct snd_ali *codec = pcm->private_data;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001787 codec->pcm[pcm->device] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001790
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001791static int __devinit snd_ali_pcm(struct snd_ali * codec, int device, struct ali_pcm_description *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001793 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 int err;
1795
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001796 err = snd_pcm_new(codec->card, desc->name, device,
1797 desc->playback_num, desc->capture_num, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (err < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001799 snd_printk(KERN_ERR "snd_ali_pcm: err called snd_pcm_new.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return err;
1801 }
1802 pcm->private_data = codec;
1803 pcm->private_free = snd_ali_pcm_free;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001804 if (desc->playback_ops)
1805 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, desc->playback_ops);
1806 if (desc->capture_ops)
1807 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, desc->capture_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1810 snd_dma_pci_data(codec->pci), 64*1024, 128*1024);
1811
1812 pcm->info_flags = 0;
Sasha Khapyorsky6632d192005-09-29 11:48:17 +02001813 pcm->dev_class = desc->class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001815 strcpy(pcm->name, desc->name);
1816 codec->pcm[0] = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return 0;
1818}
1819
Clemens Ladischa53fc182005-08-11 15:59:17 +02001820static struct ali_pcm_description ali_pcms[] = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001821 { "ALI 5451", ALI_CHANNELS, 1, &snd_ali_playback_ops, &snd_ali_capture_ops },
Sasha Khapyorsky6632d192005-09-29 11:48:17 +02001822 { "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 +02001823};
1824
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001825static int __devinit snd_ali_build_pcms(struct snd_ali *codec)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001826{
1827 int i, err;
1828 for(i = 0 ; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms) ; i++)
1829 if((err = snd_ali_pcm(codec, i, &ali_pcms[i])) < 0)
1830 return err;
1831 return 0;
1832}
1833
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835#define ALI5451_SPDIF(xname, xindex, value) \
1836{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1837.info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1838.put = snd_ali5451_spdif_put, .private_value = value}
1839
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001840static int snd_ali5451_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1843 uinfo->count = 1;
1844 uinfo->value.integer.min = 0;
1845 uinfo->value.integer.max = 1;
1846 return 0;
1847}
1848
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001849static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001851 struct snd_ali *codec = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 unsigned int enable;
1853
1854 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1855
Takashi Iwai755e1372005-11-11 21:05:27 +01001856 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 switch(kcontrol->private_value) {
1858 case 0:
1859 enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1860 break;
1861 case 1:
1862 enable = ((codec->spdif_mask & 0x02) && (codec->spdif_mask & 0x04)) ? 1 : 0;
1863 break;
1864 case 2:
1865 enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1866 break;
1867 default:
1868 break;
1869 }
1870 ucontrol->value.integer.value[0] = enable;
Takashi Iwai755e1372005-11-11 21:05:27 +01001871 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 return 0;
1873}
1874
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001875static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001877 struct snd_ali *codec = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 unsigned int change = 0, enable = 0;
1879
1880 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1881
Takashi Iwai755e1372005-11-11 21:05:27 +01001882 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 switch (kcontrol->private_value) {
1884 case 0:
1885 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1886 change = change ^ enable;
1887 if (change) {
1888 if (enable) {
1889 codec->spdif_mask |= 0x02;
1890 snd_ali_enable_spdif_out(codec);
1891 } else {
1892 codec->spdif_mask &= ~(0x02);
1893 codec->spdif_mask &= ~(0x04);
1894 snd_ali_disable_spdif_out(codec);
1895 }
1896 }
1897 break;
1898 case 1:
1899 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1900 change = change ^ enable;
1901 if (change && (codec->spdif_mask & 0x02)) {
1902 if (enable) {
1903 codec->spdif_mask |= 0x04;
1904 snd_ali_enable_spdif_chnout(codec);
1905 } else {
1906 codec->spdif_mask &= ~(0x04);
1907 snd_ali_disable_spdif_chnout(codec);
1908 }
1909 }
1910 break;
1911 case 2:
1912 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1913 change = change ^ enable;
1914 if (change) {
1915 if (enable) {
1916 codec->spdif_mask |= 0x01;
1917 snd_ali_enable_spdif_in(codec);
1918 } else {
1919 codec->spdif_mask &= ~(0x01);
1920 snd_ali_disable_spdif_in(codec);
1921 }
1922 }
1923 break;
1924 default:
1925 break;
1926 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001927 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 return change;
1930}
1931
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001932static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 /* spdif aplayback switch */
1934 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001935 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 /* spdif out to spdif channel */
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001937 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 /* spdif in from spdif channel */
1939 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1940};
1941
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001942static void snd_ali_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001944 struct snd_ali *codec = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 codec->ac97_bus = NULL;
1946}
1947
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001948static void snd_ali_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001950 struct snd_ali *codec = ac97->private_data;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001951 codec->ac97[ac97->num] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001954static int __devinit snd_ali_mixer(struct snd_ali * codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001956 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 unsigned int idx;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001958 int i, err;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001959 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 .write = snd_ali_codec_write,
1961 .read = snd_ali_codec_read,
1962 };
1963
1964 if ((err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus)) < 0)
1965 return err;
1966 codec->ac97_bus->private_free = snd_ali_mixer_free_ac97_bus;
1967
1968 memset(&ac97, 0, sizeof(ac97));
1969 ac97.private_data = codec;
1970 ac97.private_free = snd_ali_mixer_free_ac97;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001971
1972 for ( i = 0 ; i < codec->num_of_codecs ; i++) {
1973 ac97.num = i;
1974 if ((err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i])) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001975 snd_printk(KERN_ERR "ali mixer %d creating error.\n", i);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001976 if(i == 0)
Takashi Iwai4d060fd2005-10-04 13:50:44 +02001977 return err;
1978 codec->num_of_codecs = 1;
1979 break;
1980 }
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001981 }
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (codec->spdif_support) {
1984 for(idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1985 err=snd_ctl_add(codec->card, snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1986 if (err < 0) return err;
1987 }
1988 }
1989 return 0;
1990}
1991
1992#ifdef CONFIG_PM
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001993static int ali_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001995 struct snd_card *card = pci_get_drvdata(pci);
1996 struct snd_ali *chip = card->private_data;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001997 struct snd_ali_image *im;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 int i, j;
1999
2000 im = chip->image;
2001 if (! im)
2002 return 0;
2003
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002004 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002005 for(i = 0 ; i < chip->num_of_codecs ; i++) {
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002006 snd_pcm_suspend_all(chip->pcm[i]);
2007 snd_ac97_suspend(chip->ac97[i]);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 spin_lock_irq(&chip->reg_lock);
2011
2012 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
2013 // im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START));
2014 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
2015
2016 // disable all IRQ bits
2017 outl(0, ALI_REG(chip, ALI_MISCINT));
2018
2019 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
2020 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
2021 continue;
2022 im->regs[i] = inl(ALI_REG(chip, i*4));
2023 }
2024
2025 for (i = 0; i < ALI_CHANNELS; i++) {
2026 outb(i, ALI_REG(chip, ALI_GC_CIR));
2027 for (j = 0; j < ALI_CHANNEL_REGS; j++)
2028 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
2029 }
2030
2031 // stop all HW channel
2032 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
2033
2034 spin_unlock_irq(&chip->reg_lock);
Takashi Iwai30b35392006-10-11 18:52:53 +02002035
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002036 pci_disable_device(pci);
2037 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002038 pci_set_power_state(pci, pci_choose_state(pci, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 return 0;
2040}
2041
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002042static int ali_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002044 struct snd_card *card = pci_get_drvdata(pci);
2045 struct snd_ali *chip = card->private_data;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002046 struct snd_ali_image *im;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 int i, j;
2048
2049 im = chip->image;
2050 if (! im)
2051 return 0;
2052
Takashi Iwai30b35392006-10-11 18:52:53 +02002053 pci_set_power_state(pci, PCI_D0);
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002054 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002055 if (pci_enable_device(pci) < 0) {
2056 printk(KERN_ERR "ali5451: pci_enable_device failed, "
2057 "disabling device\n");
2058 snd_card_disconnect(card);
2059 return -EIO;
2060 }
2061 pci_set_master(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063 spin_lock_irq(&chip->reg_lock);
2064
2065 for (i = 0; i < ALI_CHANNELS; i++) {
2066 outb(i, ALI_REG(chip, ALI_GC_CIR));
2067 for (j = 0; j < ALI_CHANNEL_REGS; j++)
2068 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
2069 }
2070
2071 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
2072 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) || (i*4 == ALI_START))
2073 continue;
2074 outl(im->regs[i], ALI_REG(chip, i*4));
2075 }
2076
2077 // start HW channel
2078 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
2079 // restore IRQ enable bits
2080 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
2081
2082 spin_unlock_irq(&chip->reg_lock);
2083
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002084 for(i = 0 ; i < chip->num_of_codecs ; i++)
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002085 snd_ac97_resume(chip->ac97[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002087 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return 0;
2089}
2090#endif /* CONFIG_PM */
2091
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002092static int snd_ali_free(struct snd_ali * codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
2094 if (codec->hw_initialized)
2095 snd_ali_disable_address_interrupt(codec);
2096 if (codec->irq >= 0) {
2097 synchronize_irq(codec->irq);
Takashi Iwai437a5a42006-11-21 12:14:23 +01002098 free_irq(codec->irq, codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
2100 if (codec->port)
2101 pci_release_regions(codec->pci);
2102 pci_disable_device(codec->pci);
2103#ifdef CONFIG_PM
2104 kfree(codec->image);
2105#endif
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002106 pci_dev_put(codec->pci_m1533);
2107 pci_dev_put(codec->pci_m7101);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 kfree(codec);
2109 return 0;
2110}
2111
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002112static int snd_ali_chip_init(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 unsigned int legacy;
2115 unsigned char temp;
2116 struct pci_dev *pci_dev = NULL;
2117
2118 snd_ali_printk("chip initializing ... \n");
2119
2120 if (snd_ali_reset_5451(codec)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002121 snd_printk(KERN_ERR "ali_chip_init: reset 5451 error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return -1;
2123 }
2124
2125 if (codec->revision == ALI_5451_V02) {
2126 pci_dev = codec->pci_m1533;
2127 pci_read_config_byte(pci_dev, 0x59, &temp);
2128 temp |= 0x80;
2129 pci_write_config_byte(pci_dev, 0x59, temp);
2130
2131 pci_dev = codec->pci_m7101;
2132 pci_read_config_byte(pci_dev, 0xb8, &temp);
2133 temp |= 0x20;
2134 pci_write_config_byte(pci_dev, 0xB8, temp);
2135 }
2136
2137 pci_read_config_dword(codec->pci, 0x44, &legacy);
2138 legacy &= 0xff00ff00;
2139 legacy |= 0x000800aa;
2140 pci_write_config_dword(codec->pci, 0x44, legacy);
2141
2142 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2143 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2144 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2145 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2146 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2147
2148 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
2149 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_STATUS);
2150 if (codec->spdif_support) {
2151 snd_ali_enable_spdif_out(codec);
2152 codec->spdif_mask = 0x00000002;
2153 }
2154
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002155 codec->num_of_codecs = 1;
2156
2157 /* secondary codec - modem */
2158 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
2159 codec->num_of_codecs++;
2160 outl(inl(ALI_REG(codec, ALI_SCTRL)) |
2161 (ALI_SCTRL_LINE_IN2|ALI_SCTRL_GPIO_IN2|ALI_SCTRL_LINE_OUT_EN),
2162 ALI_REG(codec, ALI_SCTRL));
2163 }
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 snd_ali_printk("chip initialize succeed.\n");
2166 return 0;
2167
2168}
2169
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002170/* proc for register dump */
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002171static void snd_ali_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buf)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002172{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002173 struct snd_ali *codec = entry->private_data;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002174 int i;
2175 for(i = 0 ; i < 256 ; i+= 4)
2176 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
2177}
2178
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002179static void __devinit snd_ali_proc_init(struct snd_ali *codec)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002180{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002181 struct snd_info_entry *entry;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002182 if(!snd_card_proc_new(codec->card, "ali5451", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002183 snd_info_set_text_ops(entry, codec, snd_ali_proc_read);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002184}
2185
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002186static int __devinit snd_ali_resources(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 int err;
2189
2190 snd_ali_printk("resouces allocation ...\n");
2191 if ((err = pci_request_regions(codec->pci, "ALI 5451")) < 0)
2192 return err;
2193 codec->port = pci_resource_start(codec->pci, 0);
2194
Takashi Iwai437a5a42006-11-21 12:14:23 +01002195 if (request_irq(codec->pci->irq, snd_ali_card_interrupt,
2196 IRQF_SHARED, "ALI 5451", codec)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002197 snd_printk(KERN_ERR "Unable to request irq.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return -EBUSY;
2199 }
2200 codec->irq = codec->pci->irq;
2201 snd_ali_printk("resouces allocated.\n");
2202 return 0;
2203}
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002204static int snd_ali_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002206 struct snd_ali *codec=device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 snd_ali_free(codec);
2208 return 0;
2209}
2210
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002211static int __devinit snd_ali_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 struct pci_dev *pci,
2213 int pcm_streams,
2214 int spdif_support,
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002215 struct snd_ali ** r_ali)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002217 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 int i, err;
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002219 unsigned short cmdw;
2220 static struct snd_device_ops ops = {
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002221 .dev_free = snd_ali_dev_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 };
2223
2224 *r_ali = NULL;
2225
2226 snd_ali_printk("creating ...\n");
2227
2228 /* enable PCI device */
2229 if ((err = pci_enable_device(pci)) < 0)
2230 return err;
2231 /* check, if we can restrict PCI DMA transfers to 31 bits */
Matthias Gehre910638a2006-03-28 01:56:48 -08002232 if (pci_set_dma_mask(pci, DMA_31BIT_MASK) < 0 ||
2233 pci_set_consistent_dma_mask(pci, DMA_31BIT_MASK) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002234 snd_printk(KERN_ERR "architecture does not support 31bit PCI busmaster DMA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 pci_disable_device(pci);
2236 return -ENXIO;
2237 }
2238
Pekka Enberg8db08ea2005-09-06 15:18:36 -07002239 if ((codec = kzalloc(sizeof(*codec), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 pci_disable_device(pci);
2241 return -ENOMEM;
2242 }
2243
2244 spin_lock_init(&codec->reg_lock);
2245 spin_lock_init(&codec->voice_alloc);
2246
2247 codec->card = card;
2248 codec->pci = pci;
2249 codec->irq = -1;
2250 pci_read_config_byte(pci, PCI_REVISION_ID, &codec->revision);
2251 codec->spdif_support = spdif_support;
2252
2253 if (pcm_streams < 1)
2254 pcm_streams = 1;
2255 if (pcm_streams > 32)
2256 pcm_streams = 32;
2257
2258 pci_set_master(pci);
2259 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2260 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2261 cmdw |= PCI_COMMAND_IO;
2262 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2263 }
2264 pci_set_master(pci);
2265
2266 if (snd_ali_resources(codec)) {
2267 snd_ali_free(codec);
2268 return -EBUSY;
2269 }
2270
2271 synchronize_irq(pci->irq);
2272
2273 codec->synth.chmap = 0;
2274 codec->synth.chcnt = 0;
2275 codec->spdif_mask = 0;
2276 codec->synth.synthcount = 0;
2277
2278 if (codec->revision == ALI_5451_V02)
2279 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2280 else
2281 codec->chregs.regs.ac97read = ALI_AC97_READ;
2282 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2283
2284 codec->chregs.regs.start = ALI_START;
2285 codec->chregs.regs.stop = ALI_STOP;
2286 codec->chregs.regs.aint = ALI_AINT;
2287 codec->chregs.regs.ainten = ALI_AINTEN;
2288
2289 codec->chregs.data.start = 0x00;
2290 codec->chregs.data.stop = 0x00;
2291 codec->chregs.data.aint = 0x00;
2292 codec->chregs.data.ainten = 0x00;
2293
2294 /* M1533: southbridge */
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002295 codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (! codec->pci_m1533) {
2297 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2298 snd_ali_free(codec);
2299 return -ENODEV;
2300 }
2301 /* M7101: power management */
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002302 codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 if (! codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2304 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2305 snd_ali_free(codec);
2306 return -ENODEV;
2307 }
2308
2309 snd_ali_printk("snd_device_new is called.\n");
2310 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
2311 snd_ali_free(codec);
2312 return err;
2313 }
2314
2315 /* initialise synth voices*/
2316 for (i = 0; i < ALI_CHANNELS; i++ ) {
2317 codec->synth.voices[i].number = i;
2318 }
2319
2320 if ((err = snd_ali_chip_init(codec)) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002321 snd_printk(KERN_ERR "ali create: chip init error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return err;
2323 }
2324
2325#ifdef CONFIG_PM
2326 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
2327 if (! codec->image)
2328 snd_printk(KERN_WARNING "can't allocate apm buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329#endif
2330
2331 snd_ali_enable_address_interrupt(codec);
2332 codec->hw_initialized = 1;
2333
2334 *r_ali = codec;
2335 snd_ali_printk("created.\n");
2336 return 0;
2337}
2338
2339static int __devinit snd_ali_probe(struct pci_dev *pci,
2340 const struct pci_device_id *pci_id)
2341{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002342 struct snd_card *card;
2343 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 int err;
2345
2346 snd_ali_printk("probe ...\n");
2347
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +02002348 card = snd_card_new(index, id, THIS_MODULE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (card == NULL)
2350 return -ENOMEM;
2351
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +02002352 if ((err = snd_ali_create(card, pci, pcm_channels, spdif, &codec)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 snd_card_free(card);
2354 return err;
2355 }
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002356 card->private_data = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 snd_ali_printk("mixer building ...\n");
2359 if ((err = snd_ali_mixer(codec)) < 0) {
2360 snd_card_free(card);
2361 return err;
2362 }
2363
2364 snd_ali_printk("pcm building ...\n");
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002365 if ((err = snd_ali_build_pcms(codec)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 snd_card_free(card);
2367 return err;
2368 }
2369
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002370 snd_ali_proc_init(codec);
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 strcpy(card->driver, "ALI5451");
2373 strcpy(card->shortname, "ALI 5451");
2374
2375 sprintf(card->longname, "%s at 0x%lx, irq %li",
2376 card->shortname, codec->port, codec->irq);
2377
2378 snd_ali_printk("register card.\n");
2379 if ((err = snd_card_register(card)) < 0) {
2380 snd_card_free(card);
2381 return err;
2382 }
2383 pci_set_drvdata(pci, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 return 0;
2385}
2386
2387static void __devexit snd_ali_remove(struct pci_dev *pci)
2388{
2389 snd_card_free(pci_get_drvdata(pci));
2390 pci_set_drvdata(pci, NULL);
2391}
2392
2393static struct pci_driver driver = {
2394 .name = "ALI 5451",
2395 .id_table = snd_ali_ids,
2396 .probe = snd_ali_probe,
2397 .remove = __devexit_p(snd_ali_remove),
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002398#ifdef CONFIG_PM
2399 .suspend = ali_suspend,
2400 .resume = ali_resume,
2401#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402};
2403
2404static int __init alsa_card_ali_init(void)
2405{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002406 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
2409static void __exit alsa_card_ali_exit(void)
2410{
2411 pci_unregister_driver(&driver);
2412}
2413
2414module_init(alsa_card_ali_init)
2415module_exit(alsa_card_ali_exit)