blob: 4c2bd7adf6748215588add8a700e919b9df90a79 [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
Takashi Iwaif9ab2b12007-04-03 13:20:49 +020072/* #define ALI_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#ifdef ALI_DEBUG
Takashi Iwaif9ab2b12007-04-03 13:20:49 +020075#define snd_ali_printk(format, args...) printk(KERN_DEBUG format, ##args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#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
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200108#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#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 {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200178 /* register data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 struct REGDATA {
180 unsigned int start;
181 unsigned int stop;
182 unsigned int aint;
183 unsigned int ainten;
184 } data;
185
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200186 /* register addresses */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 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;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200200 unsigned int use :1,
201 pcm :1,
202 midi :1,
203 mode :1,
204 synth :1,
205 running :1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 /* PCM data */
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100208 struct snd_ali *codec;
209 struct snd_pcm_substream *substream;
210 struct snd_ali_voice *extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 int eso; /* final ESO value for channel */
213 int count; /* runtime->period_size */
214
215 /* --- */
216
217 void *private_data;
218 void (*private_free)(void *private_data);
219};
220
221
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100222struct snd_alidev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100224 struct snd_ali_voice voices[ALI_CHANNELS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 unsigned int chcnt; /* num of opened channels */
227 unsigned int chmap; /* bitmap for opened channels */
228 unsigned int synthcount;
229
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100230};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#define ALI_GLOBAL_REGS 56
234#define ALI_CHANNEL_REGS 8
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100235struct snd_ali_image {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200236 u32 regs[ALI_GLOBAL_REGS];
237 u32 channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100238};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100241struct snd_ali {
Takashi Iwaiba8c3c32007-05-30 12:42:31 +0200242 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned long port;
244 unsigned char revision;
245
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200246 unsigned int hw_initialized :1;
247 unsigned int spdif_support :1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 struct pci_dev *pci;
250 struct pci_dev *pci_m1533;
251 struct pci_dev *pci_m7101;
252
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100253 struct snd_card *card;
254 struct snd_pcm *pcm[MAX_CODECS];
255 struct snd_alidev synth;
256 struct snd_ali_channel_control chregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /* S/PDIF Mask */
259 unsigned int spdif_mask;
260
261 unsigned int spurious_irq_count;
262 unsigned int spurious_irq_max_delta;
263
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200264 unsigned int num_of_codecs;
265
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100266 struct snd_ac97_bus *ac97_bus;
267 struct snd_ac97 *ac97[MAX_CODECS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 unsigned short ac97_ext_id;
269 unsigned short ac97_ext_status;
270
271 spinlock_t reg_lock;
272 spinlock_t voice_alloc;
273
274#ifdef CONFIG_PM
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100275 struct snd_ali_image *image;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276#endif
277};
278
Takashi Iwaif40b6892006-07-05 16:51:05 +0200279static struct pci_device_id snd_ali_ids[] = {
Jon Masonec6c8c32006-01-13 13:17:43 +0100280 {PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 {0, }
282};
283MODULE_DEVICE_TABLE(pci, snd_ali_ids);
284
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100285static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
286static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200287static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short,
288 unsigned short);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290/*
291 * AC97 ACCESS
292 */
293
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100294static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200295 unsigned int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 return (unsigned int)inl(ALI_REG(codec, port));
298}
299
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200300static inline void snd_ali_5451_poke(struct snd_ali *codec,
301 unsigned int port,
302 unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 outl((unsigned int)val, ALI_REG(codec, port));
305}
306
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200307static int snd_ali_codec_ready(struct snd_ali *codec,
308 unsigned int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 unsigned long end_time;
311 unsigned int res;
312
Takashi Iwai755e1372005-11-11 21:05:27 +0100313 end_time = jiffies + msecs_to_jiffies(250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 do {
315 res = snd_ali_5451_peek(codec,port);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200316 if (!(res & 0x8000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 0;
Takashi Iwai755e1372005-11-11 21:05:27 +0100318 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 } while (time_after_eq(end_time, jiffies));
320 snd_ali_5451_poke(codec, port, res & ~0x8000);
321 snd_printdd("ali_codec_ready: codec is not ready.\n ");
322 return -EIO;
323}
324
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100325static int snd_ali_stimer_ready(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 unsigned long end_time;
328 unsigned long dwChk1,dwChk2;
329
330 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
331 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
332
Takashi Iwai755e1372005-11-11 21:05:27 +0100333 end_time = jiffies + msecs_to_jiffies(250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 do {
335 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
336 if (dwChk2 != dwChk1)
337 return 0;
Takashi Iwai755e1372005-11-11 21:05:27 +0100338 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 } while (time_after_eq(end_time, jiffies));
Takashi Iwai99b359b2005-10-20 18:26:44 +0200340 snd_printk(KERN_ERR "ali_stimer_read: stimer is not ready.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -EIO;
342}
343
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100344static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200345 unsigned short reg,
346 unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200348 unsigned int dwVal;
349 unsigned int port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 if (reg >= 0x80) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200352 snd_printk(KERN_ERR "ali_codec_poke: reg(%xh) invalid.\n", reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return;
354 }
355
356 port = codec->chregs.regs.ac97write;
357
Takashi Iwai755e1372005-11-11 21:05:27 +0100358 if (snd_ali_codec_ready(codec, port) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return;
Takashi Iwai755e1372005-11-11 21:05:27 +0100360 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return;
362
363 dwVal = (unsigned int) (reg & 0xff);
364 dwVal |= 0x8000 | (val << 16);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200365 if (secondary)
366 dwVal |= 0x0080;
367 if (codec->revision == ALI_5451_V02)
368 dwVal |= 0x0100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200370 snd_ali_5451_poke(codec, port, dwVal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 return ;
373}
374
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200375static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
376 int secondary,
377 unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200379 unsigned int dwVal;
380 unsigned int port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 if (reg >= 0x80) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200383 snd_printk(KERN_ERR "ali_codec_peek: reg(%xh) invalid.\n", reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return ~0;
385 }
386
387 port = codec->chregs.regs.ac97read;
388
Takashi Iwai755e1372005-11-11 21:05:27 +0100389 if (snd_ali_codec_ready(codec, port) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return ~0;
Takashi Iwai755e1372005-11-11 21:05:27 +0100391 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return ~0;
393
394 dwVal = (unsigned int) (reg & 0xff);
395 dwVal |= 0x8000; /* bit 15*/
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200396 if (secondary)
397 dwVal |= 0x0080;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 snd_ali_5451_poke(codec, port, dwVal);
400
Takashi Iwai755e1372005-11-11 21:05:27 +0100401 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return ~0;
Takashi Iwai755e1372005-11-11 21:05:27 +0100403 if (snd_ali_codec_ready(codec, port) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return ~0;
405
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200406 return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100409static void snd_ali_codec_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 unsigned short reg,
411 unsigned short val )
412{
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100413 struct snd_ali *codec = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200416 if (reg == AC97_GPIO_STATUS) {
417 outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
418 ALI_REG(codec, ALI_AC97_GPIO));
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200419 return;
420 }
421 snd_ali_codec_poke(codec, ac97->num, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return ;
423}
424
425
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200426static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
427 unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100429 struct snd_ali *codec = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 snd_ali_printk("codec_read reg=%xh.\n", reg);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200432 return snd_ali_codec_peek(codec, ac97->num, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435/*
436 * AC97 Reset
437 */
438
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100439static int snd_ali_reset_5451(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200441 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 unsigned short wCount, wReg;
443 unsigned int dwVal;
444
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200445 pci_dev = codec->pci_m1533;
446 if (pci_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
448 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
449 udelay(5000);
450 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
451 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
452 udelay(5000);
453 }
454
455 pci_dev = codec->pci;
456 pci_read_config_dword(pci_dev, 0x44, &dwVal);
457 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
458 udelay(500);
459 pci_read_config_dword(pci_dev, 0x44, &dwVal);
460 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
461 udelay(5000);
462
463 wCount = 200;
464 while(wCount--) {
465 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200466 if ((wReg & 0x000f) == 0x000f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return 0;
468 udelay(5000);
469 }
470
471 /* non-fatal if you have a non PM capable codec */
472 /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
473 return 0;
474}
475
476#ifdef CODEC_RESET
477
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100478static int snd_ali_reset_codec(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200480 struct pci_dev *pci_dev;
481 unsigned char bVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 unsigned int dwVal;
483 unsigned short wCount, wReg;
484
485 pci_dev = codec->pci_m1533;
486
487 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
488 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
489 udelay(5000);
490 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
491 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
492 udelay(5000);
493
494 bVal = inb(ALI_REG(codec,ALI_SCTRL));
495 bVal |= 0x02;
496 outb(ALI_REG(codec,ALI_SCTRL),bVal);
497 udelay(5000);
498 bVal = inb(ALI_REG(codec,ALI_SCTRL));
499 bVal &= 0xfd;
500 outb(ALI_REG(codec,ALI_SCTRL),bVal);
501 udelay(15000);
502
503 wCount = 200;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200504 while (wCount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200506 if ((wReg & 0x000f) == 0x000f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return 0;
508 udelay(5000);
509 }
510 return -1;
511}
512
513#endif
514
515/*
516 * ALI 5451 Controller
517 */
518
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200519static void snd_ali_enable_special_channel(struct snd_ali *codec,
520 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200522 unsigned long dwVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200524 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 dwVal |= 1 << (channel & 0x0000001f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200526 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200529static void snd_ali_disable_special_channel(struct snd_ali *codec,
530 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200532 unsigned long dwVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200534 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 dwVal &= ~(1 << (channel & 0x0000001f));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200536 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200539static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 unsigned int gc;
542
543 gc = inl(ALI_REG(codec, ALI_GC_CIR));
544 gc |= ENDLP_IE;
545 gc |= MIDLP_IE;
546 outl( gc, ALI_REG(codec, ALI_GC_CIR));
547}
548
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200549static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 unsigned int gc;
552
553 gc = inl(ALI_REG(codec, ALI_GC_CIR));
554 gc &= ~ENDLP_IE;
555 gc &= ~MIDLP_IE;
556 outl(gc, ALI_REG(codec, ALI_GC_CIR));
557}
558
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200559#if 0 /* not used */
560static void snd_ali_enable_voice_irq(struct snd_ali *codec,
561 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 unsigned int mask;
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100564 struct snd_ali_channel_control *pchregs = &(codec->chregs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 snd_ali_printk("enable_voice_irq channel=%d\n",channel);
567
568 mask = 1 << (channel & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200569 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 pchregs->data.ainten |= mask;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200571 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573#endif
574
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200575static void snd_ali_disable_voice_irq(struct snd_ali *codec,
576 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 unsigned int mask;
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100579 struct snd_ali_channel_control *pchregs = &(codec->chregs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 snd_ali_printk("disable_voice_irq channel=%d\n",channel);
582
583 mask = 1 << (channel & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200584 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 pchregs->data.ainten &= ~mask;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200586 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100589static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 unsigned int idx = channel & 0x1f;
592
593 if (codec->synth.chcnt >= ALI_CHANNELS){
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200594 snd_printk(KERN_ERR
595 "ali_alloc_pcm_channel: no free channels.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return -1;
597 }
598
599 if (!(codec->synth.chmap & (1 << idx))) {
600 codec->synth.chmap |= 1 << idx;
601 codec->synth.chcnt++;
602 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
603 return idx;
604 }
605 return -1;
606}
607
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100608static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 int idx;
611 int result = -1;
612
613 snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
614
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200615 /* recording */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (rec) {
617 if (codec->spdif_support &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200618 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
619 ALI_SPDIF_IN_SUPPORT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 idx = ALI_SPDIF_IN_CHANNEL;
621 else
622 idx = ALI_PCM_IN_CHANNEL;
623
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200624 result = snd_ali_alloc_pcm_channel(codec, idx);
625 if (result >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return result;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200627 else {
628 snd_printk(KERN_ERR "ali_find_free_channel: "
629 "record channel is busy now.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return -1;
631 }
632 }
633
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200634 /* playback... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (codec->spdif_support &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200636 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
637 ALI_SPDIF_OUT_CH_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 idx = ALI_SPDIF_OUT_CHANNEL;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200639 result = snd_ali_alloc_pcm_channel(codec, idx);
640 if (result >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return result;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200642 else
643 snd_printk(KERN_ERR "ali_find_free_channel: "
644 "S/PDIF out channel is in busy now.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
647 for (idx = 0; idx < ALI_CHANNELS; idx++) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200648 result = snd_ali_alloc_pcm_channel(codec, idx);
649 if (result >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return result;
651 }
Takashi Iwai99b359b2005-10-20 18:26:44 +0200652 snd_printk(KERN_ERR "ali_find_free_channel: no free channels.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return -1;
654}
655
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100656static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 unsigned int idx = channel & 0x0000001f;
659
660 snd_ali_printk("free_channel_pcm channel=%d\n",channel);
661
662 if (channel < 0 || channel >= ALI_CHANNELS)
663 return;
664
665 if (!(codec->synth.chmap & (1 << idx))) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200666 snd_printk(KERN_ERR "ali_free_channel_pcm: "
667 "channel %d is not in use.\n", channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return;
669 } else {
670 codec->synth.chmap &= ~(1 << idx);
671 codec->synth.chcnt--;
672 }
673}
674
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200675#if 0 /* not used */
676static void snd_ali_start_voice(struct snd_ali *codec, unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 unsigned int mask = 1 << (channel & 0x1f);
679
680 snd_ali_printk("start_voice: channel=%d\n",channel);
681 outl(mask, ALI_REG(codec,codec->chregs.regs.start));
682}
683#endif
684
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200685static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 unsigned int mask = 1 << (channel & 0x1f);
688
689 snd_ali_printk("stop_voice: channel=%d\n",channel);
690 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
691}
692
693/*
694 * S/PDIF Part
695 */
696
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100697static void snd_ali_delay(struct snd_ali *codec,int interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 unsigned long begintimer,currenttimer;
700
701 begintimer = inl(ALI_REG(codec, ALI_STIMER));
702 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
703
704 while (currenttimer < begintimer + interval) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200705 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 break;
707 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200708 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710}
711
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100712static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200714 u16 wval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 u16 count = 0;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200716 u8 bval, R1 = 0, R2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200718 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 bval |= 0x1F;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200720 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200722 while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 count ++;
724 snd_ali_delay(codec, 6);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200725 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 R1 = bval & 0x1F;
727 }
728
729 if (count > 50000) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200730 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return;
732 }
733
Andrew Morton1c397322007-06-11 12:23:31 +0200734 for (count = 0; count <= 50000; count++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 snd_ali_delay(codec, 6);
736 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
737 R2 = bval & 0x1F;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200738 if (R2 != R1)
739 R1 = R2;
740 else
741 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
744 if (count > 50000) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200745 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return;
747 }
748
749 if (R2 >= 0x0b && R2 <= 0x0e) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200750 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
751 wval &= 0xe0f0;
752 wval |= (0x09 << 8) | 0x05;
753 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200755 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
756 outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 } else if (R2 == 0x12) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200758 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
759 wval &= 0xe0f0;
760 wval |= (0x0e << 8) | 0x08;
761 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200763 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
764 outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766}
767
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100768static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200770 u32 dwRate;
771 u8 bval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200773 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
774 bval &= 0x7f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 bval |= 0x40;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200776 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 snd_ali_detect_spdif_rate(codec);
779
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200780 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
781 bval &= 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200783 switch (bval) {
784 case 0: dwRate = 44100; break;
785 case 1: dwRate = 48000; break;
786 case 2: dwRate = 32000; break;
787 default: dwRate = 0; break;
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 return dwRate;
791}
792
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100793static void snd_ali_enable_spdif_in(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 unsigned int dwVal;
796
797 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
798 dwVal |= ALI_SPDIF_IN_SUPPORT;
799 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
800
801 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
802 dwVal |= 0x02;
803 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
804
805 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
806}
807
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100808static void snd_ali_disable_spdif_in(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 unsigned int dwVal;
811
812 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
813 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
814 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
815
816 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
817}
818
819
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100820static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 unsigned char bVal;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200823 unsigned int dwRate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200825 switch (rate) {
826 case 32000: dwRate = 0x300; break;
827 case 48000: dwRate = 0x200; break;
828 default: dwRate = 0; break;
829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
832 bVal &= (unsigned char)(~(1<<6));
833
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200834 bVal |= 0x80; /* select right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
836 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
837
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200838 bVal &= ~0x80; /* select left */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
840 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
841}
842
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100843static void snd_ali_enable_spdif_out(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 unsigned short wVal;
846 unsigned char bVal;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200847 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 pci_dev = codec->pci_m1533;
850 if (pci_dev == NULL)
851 return;
852 pci_read_config_byte(pci_dev, 0x61, &bVal);
853 bVal |= 0x40;
854 pci_write_config_byte(pci_dev, 0x61, bVal);
855 pci_read_config_byte(pci_dev, 0x7d, &bVal);
856 bVal |= 0x01;
857 pci_write_config_byte(pci_dev, 0x7d, bVal);
858
859 pci_read_config_byte(pci_dev, 0x7e, &bVal);
860 bVal &= (~0x20);
861 bVal |= 0x10;
862 pci_write_config_byte(pci_dev, 0x7e, bVal);
863
864 bVal = inb(ALI_REG(codec, ALI_SCTRL));
865 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
866
867 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
868 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
869
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200870 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
871 wVal |= ALI_SPDIF_OUT_SEL_PCM;
872 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
873 snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100876static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200878 unsigned short wVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
881 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
882 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
883/*
884 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
885 if (flag & ALI_SPDIF_OUT_NON_PCM)
886 wVal |= 0x0002;
887 else
888 wVal &= (~0x0002);
889 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
890*/
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200891 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100894static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200896 unsigned short wVal;
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
899 wVal |= ALI_SPDIF_OUT_SEL_PCM;
900 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
901
902 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
903}
904
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100905static void snd_ali_disable_spdif_out(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 unsigned char bVal;
908
909 bVal = inb(ALI_REG(codec, ALI_SCTRL));
910 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
911
912 snd_ali_disable_spdif_chnout(codec);
913}
914
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200915static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200917 struct snd_ali_voice *pvoice;
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100918 struct snd_pcm_runtime *runtime;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200919 struct snd_ali_channel_control *pchregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 unsigned int old, mask;
921#ifdef ALI_DEBUG
922 unsigned int temp, cspf;
923#endif
924
925 pchregs = &(codec->chregs);
926
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200927 /* check if interrupt occurred for channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 old = pchregs->data.aint;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200929 mask = 1U << (channel & 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 if (!(old & mask))
932 return;
933
934 pvoice = &codec->synth.voices[channel];
935 runtime = pvoice->substream->runtime;
936
937 udelay(100);
938 spin_lock(&codec->reg_lock);
939
940 if (pvoice->pcm && pvoice->substream) {
941 /* pcm interrupt */
942#ifdef ALI_DEBUG
943 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
944 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
945 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
946#endif
947 if (pvoice->running) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200948 snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",
949 (u16)temp, cspf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 spin_unlock(&codec->reg_lock);
951 snd_pcm_period_elapsed(pvoice->substream);
952 spin_lock(&codec->reg_lock);
953 } else {
954 snd_ali_stop_voice(codec, channel);
955 snd_ali_disable_voice_irq(codec, channel);
956 }
957 } else if (codec->synth.voices[channel].synth) {
958 /* synth interrupt */
959 } else if (codec->synth.voices[channel].midi) {
960 /* midi interrupt */
961 } else {
962 /* unknown interrupt */
963 snd_ali_stop_voice(codec, channel);
964 snd_ali_disable_voice_irq(codec, channel);
965 }
966 spin_unlock(&codec->reg_lock);
967 outl(mask,ALI_REG(codec,pchregs->regs.aint));
968 pchregs->data.aint = old & (~mask);
969}
970
David Howells7d12e782006-10-05 14:55:46 +0100971static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100973 struct snd_ali *codec = dev_id;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200974 int channel;
975 unsigned int audio_int;
976 struct snd_ali_channel_control *pchregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200978 if (codec == NULL || !codec->hw_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return IRQ_NONE;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200980
981 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
982 if (!audio_int)
983 return IRQ_NONE;
984
985 pchregs = &(codec->chregs);
986 if (audio_int & ADDRESS_IRQ) {
987 /* get interrupt status for all channels */
988 pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
989 for (channel = 0; channel < ALI_CHANNELS; channel++)
990 snd_ali_update_ptr(codec, channel);
991 }
992 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
993 ALI_REG(codec, ALI_MISCINT));
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return IRQ_HANDLED;
996}
997
998
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200999static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
1000 int type, int rec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001002 struct snd_ali_voice *pvoice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 int idx;
1004
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001005 snd_ali_printk("alloc_voice: type=%d rec=%d\n", type, rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Takashi Iwai755e1372005-11-11 21:05:27 +01001007 spin_lock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001009 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
1010 snd_ali_find_free_channel(codec,rec);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001011 if (idx < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001012 snd_printk(KERN_ERR "ali_alloc_voice: err.\n");
Takashi Iwai755e1372005-11-11 21:05:27 +01001013 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return NULL;
1015 }
1016 pvoice = &(codec->synth.voices[idx]);
Takashi Iwai755e1372005-11-11 21:05:27 +01001017 pvoice->codec = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 pvoice->use = 1;
1019 pvoice->pcm = 1;
1020 pvoice->mode = rec;
Takashi Iwai755e1372005-11-11 21:05:27 +01001021 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return pvoice;
1023 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001024 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return NULL;
1026}
1027
1028
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001029static void snd_ali_free_voice(struct snd_ali * codec,
1030 struct snd_ali_voice *pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 void (*private_free)(void *);
1033 void *private_data;
1034
1035 snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
1036 if (pvoice == NULL || !pvoice->use)
1037 return;
1038 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
Takashi Iwai755e1372005-11-11 21:05:27 +01001039 spin_lock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 private_free = pvoice->private_free;
1041 private_data = pvoice->private_data;
1042 pvoice->private_free = NULL;
1043 pvoice->private_data = NULL;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001044 if (pvoice->pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 snd_ali_free_channel_pcm(codec, pvoice->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 pvoice->use = pvoice->pcm = pvoice->synth = 0;
1047 pvoice->substream = NULL;
Takashi Iwai755e1372005-11-11 21:05:27 +01001048 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (private_free)
1050 private_free(private_data);
1051}
1052
1053
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001054static void snd_ali_clear_voices(struct snd_ali *codec,
1055 unsigned int v_min,
1056 unsigned int v_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 unsigned int i;
1059
1060 for (i = v_min; i <= v_max; i++) {
1061 snd_ali_stop_voice(codec, i);
1062 snd_ali_disable_voice_irq(codec, i);
1063 }
1064}
1065
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001066static void snd_ali_write_voice_regs(struct snd_ali *codec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 unsigned int Channel,
1068 unsigned int LBA,
1069 unsigned int CSO,
1070 unsigned int ESO,
1071 unsigned int DELTA,
1072 unsigned int ALPHA_FMS,
1073 unsigned int GVSEL,
1074 unsigned int PAN,
1075 unsigned int VOL,
1076 unsigned int CTRL,
1077 unsigned int EC)
1078{
1079 unsigned int ctlcmds[4];
1080
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001081 outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1084 ctlcmds[1] = LBA;
1085 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1086 ctlcmds[3] = (GVSEL << 31) |
1087 ((PAN & 0x0000007f) << 24) |
1088 ((VOL & 0x000000ff) << 16) |
1089 ((CTRL & 0x0000000f) << 12) |
1090 (EC & 0x00000fff);
1091
1092 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1093
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001094 outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
1095 outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
1096 outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
1097 outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1100 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1101}
1102
1103static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1104{
1105 unsigned int delta;
1106
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001107 if (rate < 4000)
1108 rate = 4000;
1109 if (rate > 48000)
1110 rate = 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 if (rec) {
1113 if (rate == 44100)
1114 delta = 0x116a;
1115 else if (rate == 8000)
1116 delta = 0x6000;
1117 else if (rate == 48000)
1118 delta = 0x1000;
1119 else
1120 delta = ((48000 << 12) / rate) & 0x0000ffff;
1121 } else {
1122 if (rate == 44100)
1123 delta = 0xeb3;
1124 else if (rate == 8000)
1125 delta = 0x2ab;
1126 else if (rate == 48000)
1127 delta = 0x1000;
1128 else
1129 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1130 }
1131
1132 return delta;
1133}
1134
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001135static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 unsigned int CTRL;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001138 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 /* set ctrl mode
1141 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1142 */
1143 CTRL = 0x00000001;
1144 if (snd_pcm_format_width(runtime->format) == 16)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001145 CTRL |= 0x00000008; /* 16-bit data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (!snd_pcm_format_unsigned(runtime->format))
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001147 CTRL |= 0x00000002; /* signed data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (runtime->channels > 1)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001149 CTRL |= 0x00000004; /* stereo data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return CTRL;
1151}
1152
1153/*
1154 * PCM part
1155 */
1156
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001157static int snd_ali_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 int cmd)
1159
1160{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001161 struct snd_ali *codec = snd_pcm_substream_chip(substream);
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001162 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 unsigned int what, whati, capture_flag;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001164 struct snd_ali_voice *pvoice, *evoice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 unsigned int val;
1166 int do_start;
1167
1168 switch (cmd) {
1169 case SNDRV_PCM_TRIGGER_START:
1170 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001171 do_start = 1;
1172 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 case SNDRV_PCM_TRIGGER_STOP:
1174 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001175 do_start = 0;
1176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 default:
1178 return -EINVAL;
1179 }
1180
1181 what = whati = capture_flag = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +01001182 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001183 if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
1184 pvoice = s->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 evoice = pvoice->extra;
1186 what |= 1 << (pvoice->number & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001187 if (evoice == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 whati |= 1 << (pvoice->number & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001189 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 whati |= 1 << (evoice->number & 0x1f);
1191 what |= 1 << (evoice->number & 0x1f);
1192 }
1193 if (do_start) {
1194 pvoice->running = 1;
1195 if (evoice != NULL)
1196 evoice->running = 1;
1197 } else {
1198 pvoice->running = 0;
1199 if (evoice != NULL)
1200 evoice->running = 0;
1201 }
1202 snd_pcm_trigger_done(s, substream);
1203 if (pvoice->mode)
1204 capture_flag = 1;
1205 }
1206 }
1207 spin_lock(&codec->reg_lock);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001208 if (!do_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 outl(what, ALI_REG(codec, ALI_STOP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 val = inl(ALI_REG(codec, ALI_AINTEN));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001211 if (do_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 val |= whati;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001213 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 val &= ~whati;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 outl(val, ALI_REG(codec, ALI_AINTEN));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001216 if (do_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 outl(what, ALI_REG(codec, ALI_START));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001218 snd_ali_printk("trigger: what=%xh whati=%xh\n", what, whati);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 spin_unlock(&codec->reg_lock);
1220
1221 return 0;
1222}
1223
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001224static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001225 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001227 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1228 struct snd_pcm_runtime *runtime = substream->runtime;
1229 struct snd_ali_voice *pvoice = runtime->private_data;
1230 struct snd_ali_voice *evoice = pvoice->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 int err;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001232
1233 err = snd_pcm_lib_malloc_pages(substream,
1234 params_buffer_bytes(hw_params));
1235 if (err < 0)
1236 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 /* voice management */
1239
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001240 if (params_buffer_size(hw_params) / 2 !=
1241 params_period_size(hw_params)) {
1242 if (!evoice) {
1243 evoice = snd_ali_alloc_voice(codec,
1244 SNDRV_ALI_VOICE_TYPE_PCM,
1245 0, -1);
1246 if (!evoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return -ENOMEM;
1248 pvoice->extra = evoice;
1249 evoice->substream = substream;
1250 }
1251 } else {
Takashi Iwai27043642007-05-19 16:30:35 +02001252 if (evoice) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 snd_ali_free_voice(codec, evoice);
1254 pvoice->extra = evoice = NULL;
1255 }
1256 }
1257
1258 return 0;
1259}
1260
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001261static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001263 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1264 struct snd_pcm_runtime *runtime = substream->runtime;
1265 struct snd_ali_voice *pvoice = runtime->private_data;
1266 struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 snd_pcm_lib_free_pages(substream);
Takashi Iwai27043642007-05-19 16:30:35 +02001269 if (evoice) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 snd_ali_free_voice(codec, evoice);
1271 pvoice->extra = NULL;
1272 }
1273 return 0;
1274}
1275
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001276static int snd_ali_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001277 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001279 return snd_pcm_lib_malloc_pages(substream,
1280 params_buffer_bytes(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001283static int snd_ali_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 return snd_pcm_lib_free_pages(substream);
1286}
1287
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001288static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001290 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1291 struct snd_pcm_runtime *runtime = substream->runtime;
1292 struct snd_ali_voice *pvoice = runtime->private_data;
1293 struct snd_ali_voice *evoice = pvoice->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 unsigned int LBA;
1296 unsigned int Delta;
1297 unsigned int ESO;
1298 unsigned int CTRL;
1299 unsigned int GVSEL;
1300 unsigned int PAN;
1301 unsigned int VOL;
1302 unsigned int EC;
1303
1304 snd_ali_printk("playback_prepare ...\n");
1305
Takashi Iwai755e1372005-11-11 21:05:27 +01001306 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /* set Delta (rate) value */
1309 Delta = snd_ali_convert_rate(runtime->rate, 0);
1310
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001311 if (pvoice->number == ALI_SPDIF_IN_CHANNEL ||
1312 pvoice->number == ALI_PCM_IN_CHANNEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 snd_ali_disable_special_channel(codec, pvoice->number);
1314 else if (codec->spdif_support &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001315 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
1316 ALI_SPDIF_OUT_CH_ENABLE)
1317 && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1319 Delta = 0x1000;
1320 }
1321
1322 /* set Loop Back Address */
1323 LBA = runtime->dma_addr;
1324
1325 /* set interrupt count size */
1326 pvoice->count = runtime->period_size;
1327
1328 /* set target ESO for channel */
1329 pvoice->eso = runtime->buffer_size;
1330
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001331 snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",
1332 pvoice->eso, pvoice->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 /* set ESO to capture first MIDLP interrupt */
1335 ESO = pvoice->eso -1;
1336 /* set ctrl mode */
1337 CTRL = snd_ali_control_mode(substream);
1338
1339 GVSEL = 1;
1340 PAN = 0;
1341 VOL = 0;
1342 EC = 0;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001343 snd_ali_printk("playback_prepare:\n");
1344 snd_ali_printk("ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
1345 pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1346 snd_ali_write_voice_regs(codec,
1347 pvoice->number,
1348 LBA,
1349 0, /* cso */
1350 ESO,
1351 Delta,
1352 0, /* alpha */
1353 GVSEL,
1354 PAN,
1355 VOL,
1356 CTRL,
1357 EC);
Takashi Iwai27043642007-05-19 16:30:35 +02001358 if (evoice) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 evoice->count = pvoice->count;
1360 evoice->eso = pvoice->count << 1;
1361 ESO = evoice->eso - 1;
1362 snd_ali_write_voice_regs(codec,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001363 evoice->number,
1364 LBA,
1365 0, /* cso */
1366 ESO,
1367 Delta,
1368 0, /* alpha */
1369 GVSEL,
1370 0x7f,
1371 0x3ff,
1372 CTRL,
1373 EC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001375 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return 0;
1377}
1378
1379
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001380static int snd_ali_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001382 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1383 struct snd_pcm_runtime *runtime = substream->runtime;
1384 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 unsigned int LBA;
1386 unsigned int Delta;
1387 unsigned int ESO;
1388 unsigned int CTRL;
1389 unsigned int GVSEL;
1390 unsigned int PAN;
1391 unsigned int VOL;
1392 unsigned int EC;
1393 u8 bValue;
1394
Takashi Iwai755e1372005-11-11 21:05:27 +01001395 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001397 snd_ali_printk("ali_prepare...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 snd_ali_enable_special_channel(codec,pvoice->number);
1400
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001401 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1402 pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
1403 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001405 /* Prepare capture intr channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1407
1408 unsigned int rate;
1409
Takashi Iwai755e1372005-11-11 21:05:27 +01001410 spin_unlock_irq(&codec->reg_lock);
1411 if (codec->revision != ALI_5451_V02)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return -1;
Takashi Iwai755e1372005-11-11 21:05:27 +01001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 rate = snd_ali_get_spdif_in_rate(codec);
1415 if (rate == 0) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001416 snd_printk(KERN_WARNING "ali_capture_preapre: "
1417 "spdif rate detect err!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 rate = 48000;
1419 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001420 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1422 if (bValue & 0x10) {
1423 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
Takashi Iwai99b359b2005-10-20 18:26:44 +02001424 printk(KERN_WARNING "clear SPDIF parity error flag.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426
1427 if (rate != 48000)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001428 Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001431 /* set target ESO for channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 pvoice->eso = runtime->buffer_size;
1433
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001434 /* set interrupt count size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 pvoice->count = runtime->period_size;
1436
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001437 /* set Loop Back Address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 LBA = runtime->dma_addr;
1439
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001440 /* set ESO to capture first MIDLP interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 ESO = pvoice->eso - 1;
1442 CTRL = snd_ali_control_mode(substream);
1443 GVSEL = 0;
1444 PAN = 0x00;
1445 VOL = 0x00;
1446 EC = 0;
1447
1448 snd_ali_write_voice_regs( codec,
1449 pvoice->number,
1450 LBA,
1451 0, /* cso */
1452 ESO,
1453 Delta,
1454 0, /* alpha */
1455 GVSEL,
1456 PAN,
1457 VOL,
1458 CTRL,
1459 EC);
1460
Takashi Iwai755e1372005-11-11 21:05:27 +01001461 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 return 0;
1464}
1465
1466
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001467static snd_pcm_uframes_t
1468snd_ali_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001470 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1471 struct snd_pcm_runtime *runtime = substream->runtime;
1472 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 unsigned int cso;
1474
1475 spin_lock(&codec->reg_lock);
1476 if (!pvoice->running) {
1477 spin_unlock(&codec->reg_lock);
1478 return 0;
1479 }
1480 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1481 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1482 spin_unlock(&codec->reg_lock);
1483 snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1484
1485 return cso;
1486}
1487
1488
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001489static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001491 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1492 struct snd_pcm_runtime *runtime = substream->runtime;
1493 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 unsigned int cso;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Takashi Iwai755e1372005-11-11 21:05:27 +01001496 spin_lock(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (!pvoice->running) {
Takashi Iwai755e1372005-11-11 21:05:27 +01001498 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return 0;
1500 }
1501 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1502 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
Takashi Iwai755e1372005-11-11 21:05:27 +01001503 spin_unlock(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 return cso;
1506}
1507
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001508static struct snd_pcm_hardware snd_ali_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001510 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1511 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1512 SNDRV_PCM_INFO_MMAP_VALID |
1513 SNDRV_PCM_INFO_RESUME |
1514 SNDRV_PCM_INFO_SYNC_START),
1515 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1516 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1517 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 .rate_min = 4000,
1519 .rate_max = 48000,
1520 .channels_min = 1,
1521 .channels_max = 2,
1522 .buffer_bytes_max = (256*1024),
1523 .period_bytes_min = 64,
1524 .period_bytes_max = (256*1024),
1525 .periods_min = 1,
1526 .periods_max = 1024,
1527 .fifo_size = 0,
1528};
1529
1530/*
1531 * Capture support device description
1532 */
1533
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001534static struct snd_pcm_hardware snd_ali_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001536 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1537 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1538 SNDRV_PCM_INFO_MMAP_VALID |
1539 SNDRV_PCM_INFO_RESUME |
1540 SNDRV_PCM_INFO_SYNC_START),
1541 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1542 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1543 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 .rate_min = 4000,
1545 .rate_max = 48000,
1546 .channels_min = 1,
1547 .channels_max = 2,
1548 .buffer_bytes_max = (128*1024),
1549 .period_bytes_min = 64,
1550 .period_bytes_max = (128*1024),
1551 .periods_min = 1,
1552 .periods_max = 1024,
1553 .fifo_size = 0,
1554};
1555
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001556static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001558 struct snd_ali_voice *pvoice = runtime->private_data;
1559 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 if (pvoice) {
1562 codec = pvoice->codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 snd_ali_free_voice(pvoice->codec, pvoice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
1565}
1566
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001567static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
1568 int channel, struct snd_pcm_hardware *phw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001570 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1571 struct snd_pcm_runtime *runtime = substream->runtime;
1572 struct snd_ali_voice *pvoice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001574 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
1575 channel);
1576 if (!pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 pvoice->substream = substream;
1580 runtime->private_data = pvoice;
1581 runtime->private_free = snd_ali_pcm_free_substream;
1582
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001583 runtime->hw = *phw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 snd_pcm_set_sync(substream);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001585 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1586 0, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return 0;
1588}
1589
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001590static int snd_ali_playback_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001591{
1592 return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1593}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001595static int snd_ali_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001597 return snd_ali_open(substream, 1, -1, &snd_ali_capture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001600static int snd_ali_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 return 0;
1603}
1604
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001605static int snd_ali_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001607 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1608 struct snd_ali_voice *pvoice = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 snd_ali_disable_special_channel(codec,pvoice->number);
1611
1612 return 0;
1613}
1614
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001615static struct snd_pcm_ops snd_ali_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 .open = snd_ali_playback_open,
1617 .close = snd_ali_playback_close,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001618 .ioctl = snd_pcm_lib_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 .hw_params = snd_ali_playback_hw_params,
1620 .hw_free = snd_ali_playback_hw_free,
1621 .prepare = snd_ali_playback_prepare,
1622 .trigger = snd_ali_trigger,
1623 .pointer = snd_ali_playback_pointer,
1624};
1625
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001626static struct snd_pcm_ops snd_ali_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 .open = snd_ali_capture_open,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001628 .close = snd_ali_close,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001629 .ioctl = snd_pcm_lib_ioctl,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001630 .hw_params = snd_ali_hw_params,
1631 .hw_free = snd_ali_hw_free,
1632 .prepare = snd_ali_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 .trigger = snd_ali_trigger,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001634 .pointer = snd_ali_pointer,
1635};
1636
1637/*
1638 * Modem PCM
1639 */
1640
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001641static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
1642 struct snd_pcm_hw_params *hw_params)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001643{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001644 struct snd_ali *chip = snd_pcm_substream_chip(substream);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001645 unsigned int modem_num = chip->num_of_codecs - 1;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001646 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
1647 params_rate(hw_params));
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001648 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1649 return snd_ali_hw_params(substream, hw_params);
1650}
1651
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001652static struct snd_pcm_hardware snd_ali_modem =
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001653{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001654 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1655 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1656 SNDRV_PCM_INFO_MMAP_VALID |
1657 SNDRV_PCM_INFO_RESUME |
1658 SNDRV_PCM_INFO_SYNC_START),
1659 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1660 .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
1661 SNDRV_PCM_RATE_16000),
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001662 .rate_min = 8000,
1663 .rate_max = 16000,
1664 .channels_min = 1,
1665 .channels_max = 1,
1666 .buffer_bytes_max = (256*1024),
1667 .period_bytes_min = 64,
1668 .period_bytes_max = (256*1024),
1669 .periods_min = 1,
1670 .periods_max = 1024,
1671 .fifo_size = 0,
1672};
1673
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001674static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
1675 int channel)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001676{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001677 static unsigned int rates[] = {8000, 9600, 12000, 16000};
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001678 static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001679 .count = ARRAY_SIZE(rates),
1680 .list = rates,
1681 .mask = 0,
1682 };
1683 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001684
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001685 if (err)
1686 return err;
1687 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1688 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1689}
1690
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001691static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001692{
1693 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1694}
1695
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001696static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001697{
1698 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1699}
1700
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001701static struct snd_pcm_ops snd_ali_modem_playback_ops = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001702 .open = snd_ali_modem_playback_open,
1703 .close = snd_ali_close,
1704 .ioctl = snd_pcm_lib_ioctl,
1705 .hw_params = snd_ali_modem_hw_params,
1706 .hw_free = snd_ali_hw_free,
1707 .prepare = snd_ali_prepare,
1708 .trigger = snd_ali_trigger,
1709 .pointer = snd_ali_pointer,
1710};
1711
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001712static struct snd_pcm_ops snd_ali_modem_capture_ops = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001713 .open = snd_ali_modem_capture_open,
1714 .close = snd_ali_close,
1715 .ioctl = snd_pcm_lib_ioctl,
1716 .hw_params = snd_ali_modem_hw_params,
1717 .hw_free = snd_ali_hw_free,
1718 .prepare = snd_ali_prepare,
1719 .trigger = snd_ali_trigger,
1720 .pointer = snd_ali_pointer,
1721};
1722
1723
1724struct ali_pcm_description {
1725 char *name;
1726 unsigned int playback_num;
1727 unsigned int capture_num;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001728 struct snd_pcm_ops *playback_ops;
1729 struct snd_pcm_ops *capture_ops;
Sasha Khapyorsky6632d192005-09-29 11:48:17 +02001730 unsigned short class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731};
1732
1733
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001734static void snd_ali_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001736 struct snd_ali *codec = pcm->private_data;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001737 codec->pcm[pcm->device] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001740
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001741static int __devinit snd_ali_pcm(struct snd_ali * codec, int device,
1742 struct ali_pcm_description *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001744 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 int err;
1746
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001747 err = snd_pcm_new(codec->card, desc->name, device,
1748 desc->playback_num, desc->capture_num, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (err < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001750 snd_printk(KERN_ERR "snd_ali_pcm: err called snd_pcm_new.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return err;
1752 }
1753 pcm->private_data = codec;
1754 pcm->private_free = snd_ali_pcm_free;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001755 if (desc->playback_ops)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001756 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1757 desc->playback_ops);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001758 if (desc->capture_ops)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001759 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1760 desc->capture_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001763 snd_dma_pci_data(codec->pci),
1764 64*1024, 128*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 pcm->info_flags = 0;
Sasha Khapyorsky6632d192005-09-29 11:48:17 +02001767 pcm->dev_class = desc->class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001769 strcpy(pcm->name, desc->name);
1770 codec->pcm[0] = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return 0;
1772}
1773
Clemens Ladischa53fc182005-08-11 15:59:17 +02001774static struct ali_pcm_description ali_pcms[] = {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001775 { .name = "ALI 5451",
1776 .playback_num = ALI_CHANNELS,
1777 .capture_num = 1,
1778 .playback_ops = &snd_ali_playback_ops,
1779 .capture_ops = &snd_ali_capture_ops
1780 },
1781 { .name = "ALI 5451 modem",
1782 .playback_num = 1,
1783 .capture_num = 1,
1784 .playback_ops = &snd_ali_modem_playback_ops,
1785 .capture_ops = &snd_ali_modem_capture_ops,
1786 .class = SNDRV_PCM_CLASS_MODEM
1787 }
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001788};
1789
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001790static int __devinit snd_ali_build_pcms(struct snd_ali *codec)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001791{
1792 int i, err;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001793 for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
1794 err = snd_ali_pcm(codec, i, &ali_pcms[i]);
1795 if (err < 0)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001796 return err;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001797 }
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001798 return 0;
1799}
1800
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802#define ALI5451_SPDIF(xname, xindex, value) \
1803{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1804.info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1805.put = snd_ali5451_spdif_put, .private_value = value}
1806
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001807#define snd_ali5451_spdif_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001809static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
1810 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001812 struct snd_ali *codec = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 unsigned int enable;
1814
1815 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1816
Takashi Iwai755e1372005-11-11 21:05:27 +01001817 spin_lock_irq(&codec->reg_lock);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001818 switch (kcontrol->private_value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 case 0:
1820 enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1821 break;
1822 case 1:
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001823 enable = ((codec->spdif_mask & 0x02) &&
1824 (codec->spdif_mask & 0x04)) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 break;
1826 case 2:
1827 enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1828 break;
1829 default:
1830 break;
1831 }
1832 ucontrol->value.integer.value[0] = enable;
Takashi Iwai755e1372005-11-11 21:05:27 +01001833 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return 0;
1835}
1836
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001837static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
1838 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001840 struct snd_ali *codec = kcontrol->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 unsigned int change = 0, enable = 0;
1842
1843 enable = ucontrol->value.integer.value[0] ? 1 : 0;
1844
Takashi Iwai755e1372005-11-11 21:05:27 +01001845 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 switch (kcontrol->private_value) {
1847 case 0:
1848 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1849 change = change ^ enable;
1850 if (change) {
1851 if (enable) {
1852 codec->spdif_mask |= 0x02;
1853 snd_ali_enable_spdif_out(codec);
1854 } else {
1855 codec->spdif_mask &= ~(0x02);
1856 codec->spdif_mask &= ~(0x04);
1857 snd_ali_disable_spdif_out(codec);
1858 }
1859 }
1860 break;
1861 case 1:
1862 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1863 change = change ^ enable;
1864 if (change && (codec->spdif_mask & 0x02)) {
1865 if (enable) {
1866 codec->spdif_mask |= 0x04;
1867 snd_ali_enable_spdif_chnout(codec);
1868 } else {
1869 codec->spdif_mask &= ~(0x04);
1870 snd_ali_disable_spdif_chnout(codec);
1871 }
1872 }
1873 break;
1874 case 2:
1875 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1876 change = change ^ enable;
1877 if (change) {
1878 if (enable) {
1879 codec->spdif_mask |= 0x01;
1880 snd_ali_enable_spdif_in(codec);
1881 } else {
1882 codec->spdif_mask &= ~(0x01);
1883 snd_ali_disable_spdif_in(codec);
1884 }
1885 }
1886 break;
1887 default:
1888 break;
1889 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001890 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 return change;
1893}
1894
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001895static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 /* spdif aplayback switch */
1897 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001898 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 /* spdif out to spdif channel */
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001900 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /* spdif in from spdif channel */
1902 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1903};
1904
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001905static int __devinit snd_ali_mixer(struct snd_ali * codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001907 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 unsigned int idx;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001909 int i, err;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001910 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 .write = snd_ali_codec_write,
1912 .read = snd_ali_codec_read,
1913 };
1914
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001915 err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
1916 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 memset(&ac97, 0, sizeof(ac97));
1920 ac97.private_data = codec;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001921
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001922 for (i = 0; i < codec->num_of_codecs; i++) {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001923 ac97.num = i;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001924 err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
1925 if (err < 0) {
1926 snd_printk(KERN_ERR
1927 "ali mixer %d creating error.\n", i);
1928 if (i == 0)
Takashi Iwai4d060fd2005-10-04 13:50:44 +02001929 return err;
1930 codec->num_of_codecs = 1;
1931 break;
1932 }
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001933 }
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (codec->spdif_support) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001936 for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1937 err = snd_ctl_add(codec->card,
1938 snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1939 if (err < 0)
1940 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942 }
1943 return 0;
1944}
1945
1946#ifdef CONFIG_PM
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001947static int ali_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001949 struct snd_card *card = pci_get_drvdata(pci);
1950 struct snd_ali *chip = card->private_data;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001951 struct snd_ali_image *im;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 int i, j;
1953
1954 im = chip->image;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001955 if (!im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return 0;
1957
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001958 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001959 for (i = 0; i < chip->num_of_codecs; i++) {
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001960 snd_pcm_suspend_all(chip->pcm[i]);
1961 snd_ac97_suspend(chip->ac97[i]);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 spin_lock_irq(&chip->reg_lock);
1965
1966 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001967 /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1969
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001970 /* disable all IRQ bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 outl(0, ALI_REG(chip, ALI_MISCINT));
1972
1973 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1974 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1975 continue;
1976 im->regs[i] = inl(ALI_REG(chip, i*4));
1977 }
1978
1979 for (i = 0; i < ALI_CHANNELS; i++) {
1980 outb(i, ALI_REG(chip, ALI_GC_CIR));
1981 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1982 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1983 }
1984
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001985 /* stop all HW channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1987
1988 spin_unlock_irq(&chip->reg_lock);
Takashi Iwai30b35392006-10-11 18:52:53 +02001989
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001990 pci_disable_device(pci);
1991 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02001992 pci_set_power_state(pci, pci_choose_state(pci, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return 0;
1994}
1995
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001996static int ali_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001998 struct snd_card *card = pci_get_drvdata(pci);
1999 struct snd_ali *chip = card->private_data;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002000 struct snd_ali_image *im;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 int i, j;
2002
2003 im = chip->image;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002004 if (!im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return 0;
2006
Takashi Iwai30b35392006-10-11 18:52:53 +02002007 pci_set_power_state(pci, PCI_D0);
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002008 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002009 if (pci_enable_device(pci) < 0) {
2010 printk(KERN_ERR "ali5451: pci_enable_device failed, "
2011 "disabling device\n");
2012 snd_card_disconnect(card);
2013 return -EIO;
2014 }
2015 pci_set_master(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 spin_lock_irq(&chip->reg_lock);
2018
2019 for (i = 0; i < ALI_CHANNELS; i++) {
2020 outb(i, ALI_REG(chip, ALI_GC_CIR));
2021 for (j = 0; j < ALI_CHANNEL_REGS; j++)
2022 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
2023 }
2024
2025 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002026 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
2027 (i*4 == ALI_START))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 continue;
2029 outl(im->regs[i], ALI_REG(chip, i*4));
2030 }
2031
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002032 /* start HW channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002034 /* restore IRQ enable bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
2036
2037 spin_unlock_irq(&chip->reg_lock);
2038
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002039 for (i = 0 ; i < chip->num_of_codecs; i++)
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002040 snd_ac97_resume(chip->ac97[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002042 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return 0;
2044}
2045#endif /* CONFIG_PM */
2046
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002047static int snd_ali_free(struct snd_ali * codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 if (codec->hw_initialized)
2050 snd_ali_disable_address_interrupt(codec);
2051 if (codec->irq >= 0) {
2052 synchronize_irq(codec->irq);
Takashi Iwai437a5a42006-11-21 12:14:23 +01002053 free_irq(codec->irq, codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 }
2055 if (codec->port)
2056 pci_release_regions(codec->pci);
2057 pci_disable_device(codec->pci);
2058#ifdef CONFIG_PM
2059 kfree(codec->image);
2060#endif
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002061 pci_dev_put(codec->pci_m1533);
2062 pci_dev_put(codec->pci_m7101);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 kfree(codec);
2064 return 0;
2065}
2066
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002067static int snd_ali_chip_init(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
2069 unsigned int legacy;
2070 unsigned char temp;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002071 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 snd_ali_printk("chip initializing ... \n");
2074
2075 if (snd_ali_reset_5451(codec)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002076 snd_printk(KERN_ERR "ali_chip_init: reset 5451 error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return -1;
2078 }
2079
2080 if (codec->revision == ALI_5451_V02) {
2081 pci_dev = codec->pci_m1533;
2082 pci_read_config_byte(pci_dev, 0x59, &temp);
2083 temp |= 0x80;
2084 pci_write_config_byte(pci_dev, 0x59, temp);
2085
2086 pci_dev = codec->pci_m7101;
2087 pci_read_config_byte(pci_dev, 0xb8, &temp);
2088 temp |= 0x20;
2089 pci_write_config_byte(pci_dev, 0xB8, temp);
2090 }
2091
2092 pci_read_config_dword(codec->pci, 0x44, &legacy);
2093 legacy &= 0xff00ff00;
2094 legacy |= 0x000800aa;
2095 pci_write_config_dword(codec->pci, 0x44, legacy);
2096
2097 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2098 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2099 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2100 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2101 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2102
2103 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002104 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
2105 AC97_EXTENDED_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (codec->spdif_support) {
2107 snd_ali_enable_spdif_out(codec);
2108 codec->spdif_mask = 0x00000002;
2109 }
2110
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002111 codec->num_of_codecs = 1;
2112
2113 /* secondary codec - modem */
2114 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
2115 codec->num_of_codecs++;
2116 outl(inl(ALI_REG(codec, ALI_SCTRL)) |
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002117 (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
2118 ALI_SCTRL_LINE_OUT_EN),
2119 ALI_REG(codec, ALI_SCTRL));
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002120 }
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 snd_ali_printk("chip initialize succeed.\n");
2123 return 0;
2124
2125}
2126
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002127/* proc for register dump */
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002128static void snd_ali_proc_read(struct snd_info_entry *entry,
2129 struct snd_info_buffer *buf)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002130{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002131 struct snd_ali *codec = entry->private_data;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002132 int i;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002133 for (i = 0; i < 256 ; i+= 4)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002134 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
2135}
2136
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002137static void __devinit snd_ali_proc_init(struct snd_ali *codec)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002138{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002139 struct snd_info_entry *entry;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002140 if (!snd_card_proc_new(codec->card, "ali5451", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002141 snd_info_set_text_ops(entry, codec, snd_ali_proc_read);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002142}
2143
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002144static int __devinit snd_ali_resources(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
2146 int err;
2147
2148 snd_ali_printk("resouces allocation ...\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002149 err = pci_request_regions(codec->pci, "ALI 5451");
2150 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return err;
2152 codec->port = pci_resource_start(codec->pci, 0);
2153
Takashi Iwai437a5a42006-11-21 12:14:23 +01002154 if (request_irq(codec->pci->irq, snd_ali_card_interrupt,
2155 IRQF_SHARED, "ALI 5451", codec)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002156 snd_printk(KERN_ERR "Unable to request irq.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return -EBUSY;
2158 }
2159 codec->irq = codec->pci->irq;
2160 snd_ali_printk("resouces allocated.\n");
2161 return 0;
2162}
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002163static int snd_ali_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002165 struct snd_ali *codec = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 snd_ali_free(codec);
2167 return 0;
2168}
2169
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002170static int __devinit snd_ali_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 struct pci_dev *pci,
2172 int pcm_streams,
2173 int spdif_support,
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002174 struct snd_ali ** r_ali)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002176 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 int i, err;
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002178 unsigned short cmdw;
2179 static struct snd_device_ops ops = {
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002180 .dev_free = snd_ali_dev_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 };
2182
2183 *r_ali = NULL;
2184
2185 snd_ali_printk("creating ...\n");
2186
2187 /* enable PCI device */
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002188 err = pci_enable_device(pci);
2189 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return err;
2191 /* check, if we can restrict PCI DMA transfers to 31 bits */
Matthias Gehre910638a2006-03-28 01:56:48 -08002192 if (pci_set_dma_mask(pci, DMA_31BIT_MASK) < 0 ||
2193 pci_set_consistent_dma_mask(pci, DMA_31BIT_MASK) < 0) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002194 snd_printk(KERN_ERR "architecture does not support "
2195 "31bit PCI busmaster DMA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 pci_disable_device(pci);
2197 return -ENXIO;
2198 }
2199
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002200 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
2201 if (!codec) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 pci_disable_device(pci);
2203 return -ENOMEM;
2204 }
2205
2206 spin_lock_init(&codec->reg_lock);
2207 spin_lock_init(&codec->voice_alloc);
2208
2209 codec->card = card;
2210 codec->pci = pci;
2211 codec->irq = -1;
Auke Kok44c10132007-06-08 15:46:36 -07002212 codec->revision = pci->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 codec->spdif_support = spdif_support;
2214
2215 if (pcm_streams < 1)
2216 pcm_streams = 1;
2217 if (pcm_streams > 32)
2218 pcm_streams = 32;
2219
2220 pci_set_master(pci);
2221 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2222 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2223 cmdw |= PCI_COMMAND_IO;
2224 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2225 }
2226 pci_set_master(pci);
2227
2228 if (snd_ali_resources(codec)) {
2229 snd_ali_free(codec);
2230 return -EBUSY;
2231 }
2232
2233 synchronize_irq(pci->irq);
2234
2235 codec->synth.chmap = 0;
2236 codec->synth.chcnt = 0;
2237 codec->spdif_mask = 0;
2238 codec->synth.synthcount = 0;
2239
2240 if (codec->revision == ALI_5451_V02)
2241 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2242 else
2243 codec->chregs.regs.ac97read = ALI_AC97_READ;
2244 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2245
2246 codec->chregs.regs.start = ALI_START;
2247 codec->chregs.regs.stop = ALI_STOP;
2248 codec->chregs.regs.aint = ALI_AINT;
2249 codec->chregs.regs.ainten = ALI_AINTEN;
2250
2251 codec->chregs.data.start = 0x00;
2252 codec->chregs.data.stop = 0x00;
2253 codec->chregs.data.aint = 0x00;
2254 codec->chregs.data.ainten = 0x00;
2255
2256 /* M1533: southbridge */
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002257 codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002258 if (!codec->pci_m1533) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2260 snd_ali_free(codec);
2261 return -ENODEV;
2262 }
2263 /* M7101: power management */
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002264 codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002265 if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2267 snd_ali_free(codec);
2268 return -ENODEV;
2269 }
2270
2271 snd_ali_printk("snd_device_new is called.\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002272 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops);
2273 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 snd_ali_free(codec);
2275 return err;
2276 }
2277
Takashi Iwaic187c042007-02-19 15:27:33 +01002278 snd_card_set_dev(card, &pci->dev);
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 /* initialise synth voices*/
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002281 for (i = 0; i < ALI_CHANNELS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 codec->synth.voices[i].number = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002284 err = snd_ali_chip_init(codec);
2285 if (err < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002286 snd_printk(KERN_ERR "ali create: chip init error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return err;
2288 }
2289
2290#ifdef CONFIG_PM
2291 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002292 if (!codec->image)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 snd_printk(KERN_WARNING "can't allocate apm buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294#endif
2295
2296 snd_ali_enable_address_interrupt(codec);
2297 codec->hw_initialized = 1;
2298
2299 *r_ali = codec;
2300 snd_ali_printk("created.\n");
2301 return 0;
2302}
2303
2304static int __devinit snd_ali_probe(struct pci_dev *pci,
2305 const struct pci_device_id *pci_id)
2306{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002307 struct snd_card *card;
2308 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 int err;
2310
2311 snd_ali_printk("probe ...\n");
2312
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +02002313 card = snd_card_new(index, id, THIS_MODULE, 0);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002314 if (!card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return -ENOMEM;
2316
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002317 err = snd_ali_create(card, pci, pcm_channels, spdif, &codec);
2318 if (err < 0)
2319 goto error;
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002320 card->private_data = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 snd_ali_printk("mixer building ...\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002323 err = snd_ali_mixer(codec);
2324 if (err < 0)
2325 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 snd_ali_printk("pcm building ...\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002328 err = snd_ali_build_pcms(codec);
2329 if (err < 0)
2330 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002332 snd_ali_proc_init(codec);
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 strcpy(card->driver, "ALI5451");
2335 strcpy(card->shortname, "ALI 5451");
2336
Takashi Iwaiba8c3c32007-05-30 12:42:31 +02002337 sprintf(card->longname, "%s at 0x%lx, irq %i",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 card->shortname, codec->port, codec->irq);
2339
2340 snd_ali_printk("register card.\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002341 err = snd_card_register(card);
2342 if (err < 0)
2343 goto error;
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 pci_set_drvdata(pci, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return 0;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002347
2348 error:
2349 snd_card_free(card);
2350 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351}
2352
2353static void __devexit snd_ali_remove(struct pci_dev *pci)
2354{
2355 snd_card_free(pci_get_drvdata(pci));
2356 pci_set_drvdata(pci, NULL);
2357}
2358
2359static struct pci_driver driver = {
2360 .name = "ALI 5451",
2361 .id_table = snd_ali_ids,
2362 .probe = snd_ali_probe,
2363 .remove = __devexit_p(snd_ali_remove),
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002364#ifdef CONFIG_PM
2365 .suspend = ali_suspend,
2366 .resume = ali_resume,
2367#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368};
2369
2370static int __init alsa_card_ali_init(void)
2371{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002372 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374
2375static void __exit alsa_card_ali_exit(void)
2376{
2377 pci_unregister_driver(&driver);
2378}
2379
2380module_init(alsa_card_ali_init)
2381module_exit(alsa_card_ali_exit)