blob: b36c551da566de2b73386727487c669c72360eda [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/io.h>
29#include <linux/delay.h>
30#include <linux/interrupt.h>
31#include <linux/init.h>
32#include <linux/pci.h>
33#include <linux/slab.h>
34#include <linux/moduleparam.h>
Matthias Gehre910638a2006-03-28 01:56:48 -080035#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <sound/core.h>
37#include <sound/pcm.h>
38#include <sound/info.h>
39#include <sound/ac97_codec.h>
40#include <sound/mpu401.h>
41#include <sound/initval.h>
42
43MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
44MODULE_DESCRIPTION("ALI M5451");
45MODULE_LICENSE("GPL");
46MODULE_SUPPORTED_DEVICE("{{ALI,M5451,pci},{ALI,M5451}}");
47
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020048static int index = SNDRV_DEFAULT_IDX1; /* Index */
49static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
50static int pcm_channels = 32;
Takashi Iwai6581f4e2006-05-17 17:14:51 +020051static int spdif;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020053module_param(index, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020055module_param(id, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020057module_param(pcm_channels, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058MODULE_PARM_DESC(pcm_channels, "PCM Channels");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020059module_param(spdif, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
61
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020062/* just for backward compatibility */
63static int enable;
64module_param(enable, bool, 0444);
65
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
68 * Debug part definitions
69 */
70
Takashi Iwaif9ab2b12007-04-03 13:20:49 +020071/* #define ALI_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73#ifdef ALI_DEBUG
Takashi Iwaif9ab2b12007-04-03 13:20:49 +020074#define snd_ali_printk(format, args...) printk(KERN_DEBUG format, ##args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#else
76#define snd_ali_printk(format, args...)
77#endif
78
79/*
80 * Constants definition
81 */
82
Takashi Iwai8cdfd252005-09-07 14:08:11 +020083#define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85
86#define ALI_CHANNELS 32
87
88#define ALI_PCM_IN_CHANNEL 31
89#define ALI_SPDIF_IN_CHANNEL 19
90#define ALI_SPDIF_OUT_CHANNEL 15
91#define ALI_CENTER_CHANNEL 24
92#define ALI_LEF_CHANNEL 23
93#define ALI_SURR_LEFT_CHANNEL 26
94#define ALI_SURR_RIGHT_CHANNEL 25
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +020095#define ALI_MODEM_IN_CHANNEL 21
96#define ALI_MODEM_OUT_CHANNEL 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98#define SNDRV_ALI_VOICE_TYPE_PCM 01
99#define SNDRV_ALI_VOICE_TYPE_OTH 02
100
101#define ALI_5451_V02 0x02
102
103/*
104 * Direct Registers
105 */
106
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200107#define ALI_LEGACY_DMAR0 0x00 /* ADR0 */
108#define ALI_LEGACY_DMAR4 0x04 /* CNT0 */
109#define ALI_LEGACY_DMAR11 0x0b /* MOD */
110#define ALI_LEGACY_DMAR15 0x0f /* MMR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define ALI_MPUR0 0x20
112#define ALI_MPUR1 0x21
113#define ALI_MPUR2 0x22
114#define ALI_MPUR3 0x23
115
116#define ALI_AC97_WRITE 0x40
117#define ALI_AC97_READ 0x44
118
119#define ALI_SCTRL 0x48
120#define ALI_SPDIF_OUT_ENABLE 0x20
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200121#define ALI_SCTRL_LINE_IN2 (1 << 9)
122#define ALI_SCTRL_GPIO_IN2 (1 << 13)
123#define ALI_SCTRL_LINE_OUT_EN (1 << 20)
124#define ALI_SCTRL_GPIO_OUT_EN (1 << 23)
125#define ALI_SCTRL_CODEC1_READY (1 << 24)
126#define ALI_SCTRL_CODEC2_READY (1 << 25)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define ALI_AC97_GPIO 0x4c
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200128#define ALI_AC97_GPIO_ENABLE 0x8000
129#define ALI_AC97_GPIO_DATA_SHIFT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#define ALI_SPDIF_CS 0x70
131#define ALI_SPDIF_CTRL 0x74
132#define ALI_SPDIF_IN_FUNC_ENABLE 0x02
133#define ALI_SPDIF_IN_CH_STATUS 0x40
134#define ALI_SPDIF_OUT_CH_STATUS 0xbf
135#define ALI_START 0x80
136#define ALI_STOP 0x84
137#define ALI_CSPF 0x90
138#define ALI_AINT 0x98
139#define ALI_GC_CIR 0xa0
140 #define ENDLP_IE 0x00001000
141 #define MIDLP_IE 0x00002000
142#define ALI_AINTEN 0xa4
143#define ALI_VOLUME 0xa8
144#define ALI_SBDELTA_DELTA_R 0xac
145#define ALI_MISCINT 0xb0
146 #define ADDRESS_IRQ 0x00000020
147 #define TARGET_REACHED 0x00008000
148 #define MIXER_OVERFLOW 0x00000800
149 #define MIXER_UNDERFLOW 0x00000400
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200150 #define GPIO_IRQ 0x01000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#define ALI_SBBL_SBCL 0xc0
152#define ALI_SBCTRL_SBE2R_SBDD 0xc4
153#define ALI_STIMER 0xc8
154#define ALI_GLOBAL_CONTROL 0xd4
155#define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
156#define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
157#define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
158#define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
159#define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
160
161#define ALI_CSO_ALPHA_FMS 0xe0
162#define ALI_LBA 0xe4
163#define ALI_ESO_DELTA 0xe8
164#define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
165#define ALI_EBUF1 0xf4
166#define ALI_EBUF2 0xf8
167
168#define ALI_REG(codec, x) ((codec)->port + x)
169
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200170#define MAX_CODECS 2
171
172
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100173struct snd_ali;
174struct snd_ali_voice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100176struct snd_ali_channel_control {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200177 /* register data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct REGDATA {
179 unsigned int start;
180 unsigned int stop;
181 unsigned int aint;
182 unsigned int ainten;
183 } data;
184
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200185 /* register addresses */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct REGS {
187 unsigned int start;
188 unsigned int stop;
189 unsigned int aint;
190 unsigned int ainten;
191 unsigned int ac97read;
192 unsigned int ac97write;
193 } regs;
194
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100195};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100197struct snd_ali_voice {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 unsigned int number;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200199 unsigned int use :1,
200 pcm :1,
201 midi :1,
202 mode :1,
203 synth :1,
204 running :1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /* PCM data */
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100207 struct snd_ali *codec;
208 struct snd_pcm_substream *substream;
209 struct snd_ali_voice *extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 int eso; /* final ESO value for channel */
212 int count; /* runtime->period_size */
213
214 /* --- */
215
216 void *private_data;
217 void (*private_free)(void *private_data);
218};
219
220
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100221struct snd_alidev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100223 struct snd_ali_voice voices[ALI_CHANNELS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 unsigned int chcnt; /* num of opened channels */
226 unsigned int chmap; /* bitmap for opened channels */
227 unsigned int synthcount;
228
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100229};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232#define ALI_GLOBAL_REGS 56
233#define ALI_CHANNEL_REGS 8
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100234struct snd_ali_image {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200235 u32 regs[ALI_GLOBAL_REGS];
236 u32 channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100237};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100240struct snd_ali {
Takashi Iwaiba8c3c32007-05-30 12:42:31 +0200241 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unsigned long port;
243 unsigned char revision;
244
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200245 unsigned int hw_initialized :1;
246 unsigned int spdif_support :1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 struct pci_dev *pci;
249 struct pci_dev *pci_m1533;
250 struct pci_dev *pci_m7101;
251
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100252 struct snd_card *card;
253 struct snd_pcm *pcm[MAX_CODECS];
254 struct snd_alidev synth;
255 struct snd_ali_channel_control chregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* S/PDIF Mask */
258 unsigned int spdif_mask;
259
260 unsigned int spurious_irq_count;
261 unsigned int spurious_irq_max_delta;
262
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200263 unsigned int num_of_codecs;
264
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100265 struct snd_ac97_bus *ac97_bus;
266 struct snd_ac97 *ac97[MAX_CODECS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 unsigned short ac97_ext_id;
268 unsigned short ac97_ext_status;
269
270 spinlock_t reg_lock;
271 spinlock_t voice_alloc;
272
273#ifdef CONFIG_PM
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100274 struct snd_ali_image *image;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#endif
276};
277
Takashi Iwaif40b6892006-07-05 16:51:05 +0200278static struct pci_device_id snd_ali_ids[] = {
Jon Masonec6c8c32006-01-13 13:17:43 +0100279 {PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 {0, }
281};
282MODULE_DEVICE_TABLE(pci, snd_ali_ids);
283
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100284static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
285static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200286static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short,
287 unsigned short);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/*
290 * AC97 ACCESS
291 */
292
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100293static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200294 unsigned int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 return (unsigned int)inl(ALI_REG(codec, port));
297}
298
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200299static inline void snd_ali_5451_poke(struct snd_ali *codec,
300 unsigned int port,
301 unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 outl((unsigned int)val, ALI_REG(codec, port));
304}
305
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200306static int snd_ali_codec_ready(struct snd_ali *codec,
307 unsigned int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 unsigned long end_time;
310 unsigned int res;
311
Takashi Iwai755e1372005-11-11 21:05:27 +0100312 end_time = jiffies + msecs_to_jiffies(250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 do {
314 res = snd_ali_5451_peek(codec,port);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200315 if (!(res & 0x8000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
Takashi Iwai755e1372005-11-11 21:05:27 +0100317 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 } while (time_after_eq(end_time, jiffies));
319 snd_ali_5451_poke(codec, port, res & ~0x8000);
320 snd_printdd("ali_codec_ready: codec is not ready.\n ");
321 return -EIO;
322}
323
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100324static int snd_ali_stimer_ready(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 unsigned long end_time;
327 unsigned long dwChk1,dwChk2;
328
329 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
330 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
331
Takashi Iwai755e1372005-11-11 21:05:27 +0100332 end_time = jiffies + msecs_to_jiffies(250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 do {
334 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
335 if (dwChk2 != dwChk1)
336 return 0;
Takashi Iwai755e1372005-11-11 21:05:27 +0100337 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 } while (time_after_eq(end_time, jiffies));
Takashi Iwai99b359b2005-10-20 18:26:44 +0200339 snd_printk(KERN_ERR "ali_stimer_read: stimer is not ready.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return -EIO;
341}
342
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100343static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200344 unsigned short reg,
345 unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200347 unsigned int dwVal;
348 unsigned int port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 if (reg >= 0x80) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200351 snd_printk(KERN_ERR "ali_codec_poke: reg(%xh) invalid.\n", reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return;
353 }
354
355 port = codec->chregs.regs.ac97write;
356
Takashi Iwai755e1372005-11-11 21:05:27 +0100357 if (snd_ali_codec_ready(codec, port) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return;
Takashi Iwai755e1372005-11-11 21:05:27 +0100359 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return;
361
362 dwVal = (unsigned int) (reg & 0xff);
363 dwVal |= 0x8000 | (val << 16);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200364 if (secondary)
365 dwVal |= 0x0080;
366 if (codec->revision == ALI_5451_V02)
367 dwVal |= 0x0100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200369 snd_ali_5451_poke(codec, port, dwVal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 return ;
372}
373
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200374static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
375 int secondary,
376 unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200378 unsigned int dwVal;
379 unsigned int port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 if (reg >= 0x80) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200382 snd_printk(KERN_ERR "ali_codec_peek: reg(%xh) invalid.\n", reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return ~0;
384 }
385
386 port = codec->chregs.regs.ac97read;
387
Takashi Iwai755e1372005-11-11 21:05:27 +0100388 if (snd_ali_codec_ready(codec, port) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return ~0;
Takashi Iwai755e1372005-11-11 21:05:27 +0100390 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return ~0;
392
393 dwVal = (unsigned int) (reg & 0xff);
394 dwVal |= 0x8000; /* bit 15*/
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200395 if (secondary)
396 dwVal |= 0x0080;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 snd_ali_5451_poke(codec, port, dwVal);
399
Takashi Iwai755e1372005-11-11 21:05:27 +0100400 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return ~0;
Takashi Iwai755e1372005-11-11 21:05:27 +0100402 if (snd_ali_codec_ready(codec, port) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return ~0;
404
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200405 return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100408static void snd_ali_codec_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 unsigned short reg,
410 unsigned short val )
411{
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100412 struct snd_ali *codec = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200415 if (reg == AC97_GPIO_STATUS) {
416 outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
417 ALI_REG(codec, ALI_AC97_GPIO));
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200418 return;
419 }
420 snd_ali_codec_poke(codec, ac97->num, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return ;
422}
423
424
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200425static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
426 unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100428 struct snd_ali *codec = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 snd_ali_printk("codec_read reg=%xh.\n", reg);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200431 return snd_ali_codec_peek(codec, ac97->num, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434/*
435 * AC97 Reset
436 */
437
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100438static int snd_ali_reset_5451(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200440 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 unsigned short wCount, wReg;
442 unsigned int dwVal;
443
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200444 pci_dev = codec->pci_m1533;
445 if (pci_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
447 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
448 udelay(5000);
449 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
450 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
451 udelay(5000);
452 }
453
454 pci_dev = codec->pci;
455 pci_read_config_dword(pci_dev, 0x44, &dwVal);
456 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
457 udelay(500);
458 pci_read_config_dword(pci_dev, 0x44, &dwVal);
459 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
460 udelay(5000);
461
462 wCount = 200;
463 while(wCount--) {
464 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200465 if ((wReg & 0x000f) == 0x000f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return 0;
467 udelay(5000);
468 }
469
470 /* non-fatal if you have a non PM capable codec */
471 /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
472 return 0;
473}
474
475#ifdef CODEC_RESET
476
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100477static int snd_ali_reset_codec(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200479 struct pci_dev *pci_dev;
480 unsigned char bVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 unsigned int dwVal;
482 unsigned short wCount, wReg;
483
484 pci_dev = codec->pci_m1533;
485
486 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
487 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
488 udelay(5000);
489 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
490 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
491 udelay(5000);
492
493 bVal = inb(ALI_REG(codec,ALI_SCTRL));
494 bVal |= 0x02;
495 outb(ALI_REG(codec,ALI_SCTRL),bVal);
496 udelay(5000);
497 bVal = inb(ALI_REG(codec,ALI_SCTRL));
498 bVal &= 0xfd;
499 outb(ALI_REG(codec,ALI_SCTRL),bVal);
500 udelay(15000);
501
502 wCount = 200;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200503 while (wCount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200505 if ((wReg & 0x000f) == 0x000f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return 0;
507 udelay(5000);
508 }
509 return -1;
510}
511
512#endif
513
514/*
515 * ALI 5451 Controller
516 */
517
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200518static void snd_ali_enable_special_channel(struct snd_ali *codec,
519 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200521 unsigned long dwVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200523 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 dwVal |= 1 << (channel & 0x0000001f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200525 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200528static void snd_ali_disable_special_channel(struct snd_ali *codec,
529 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200531 unsigned long dwVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200533 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 dwVal &= ~(1 << (channel & 0x0000001f));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200535 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200538static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 unsigned int gc;
541
542 gc = inl(ALI_REG(codec, ALI_GC_CIR));
543 gc |= ENDLP_IE;
544 gc |= MIDLP_IE;
545 outl( gc, ALI_REG(codec, ALI_GC_CIR));
546}
547
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200548static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 unsigned int gc;
551
552 gc = inl(ALI_REG(codec, ALI_GC_CIR));
553 gc &= ~ENDLP_IE;
554 gc &= ~MIDLP_IE;
555 outl(gc, ALI_REG(codec, ALI_GC_CIR));
556}
557
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200558#if 0 /* not used */
559static void snd_ali_enable_voice_irq(struct snd_ali *codec,
560 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 unsigned int mask;
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100563 struct snd_ali_channel_control *pchregs = &(codec->chregs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 snd_ali_printk("enable_voice_irq channel=%d\n",channel);
566
567 mask = 1 << (channel & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200568 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 pchregs->data.ainten |= mask;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200570 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572#endif
573
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200574static void snd_ali_disable_voice_irq(struct snd_ali *codec,
575 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 unsigned int mask;
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100578 struct snd_ali_channel_control *pchregs = &(codec->chregs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 snd_ali_printk("disable_voice_irq channel=%d\n",channel);
581
582 mask = 1 << (channel & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200583 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 pchregs->data.ainten &= ~mask;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200585 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100588static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 unsigned int idx = channel & 0x1f;
591
592 if (codec->synth.chcnt >= ALI_CHANNELS){
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200593 snd_printk(KERN_ERR
594 "ali_alloc_pcm_channel: no free channels.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return -1;
596 }
597
598 if (!(codec->synth.chmap & (1 << idx))) {
599 codec->synth.chmap |= 1 << idx;
600 codec->synth.chcnt++;
601 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
602 return idx;
603 }
604 return -1;
605}
606
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100607static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 int idx;
610 int result = -1;
611
612 snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
613
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200614 /* recording */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (rec) {
616 if (codec->spdif_support &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200617 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
618 ALI_SPDIF_IN_SUPPORT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 idx = ALI_SPDIF_IN_CHANNEL;
620 else
621 idx = ALI_PCM_IN_CHANNEL;
622
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200623 result = snd_ali_alloc_pcm_channel(codec, idx);
624 if (result >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return result;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200626 else {
627 snd_printk(KERN_ERR "ali_find_free_channel: "
628 "record channel is busy now.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return -1;
630 }
631 }
632
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200633 /* playback... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (codec->spdif_support &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200635 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
636 ALI_SPDIF_OUT_CH_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 idx = ALI_SPDIF_OUT_CHANNEL;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200638 result = snd_ali_alloc_pcm_channel(codec, idx);
639 if (result >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return result;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200641 else
642 snd_printk(KERN_ERR "ali_find_free_channel: "
643 "S/PDIF out channel is in busy now.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
646 for (idx = 0; idx < ALI_CHANNELS; idx++) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200647 result = snd_ali_alloc_pcm_channel(codec, idx);
648 if (result >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return result;
650 }
Takashi Iwai99b359b2005-10-20 18:26:44 +0200651 snd_printk(KERN_ERR "ali_find_free_channel: no free channels.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return -1;
653}
654
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100655static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 unsigned int idx = channel & 0x0000001f;
658
659 snd_ali_printk("free_channel_pcm channel=%d\n",channel);
660
661 if (channel < 0 || channel >= ALI_CHANNELS)
662 return;
663
664 if (!(codec->synth.chmap & (1 << idx))) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200665 snd_printk(KERN_ERR "ali_free_channel_pcm: "
666 "channel %d is not in use.\n", channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return;
668 } else {
669 codec->synth.chmap &= ~(1 << idx);
670 codec->synth.chcnt--;
671 }
672}
673
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200674#if 0 /* not used */
675static void snd_ali_start_voice(struct snd_ali *codec, unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 unsigned int mask = 1 << (channel & 0x1f);
678
679 snd_ali_printk("start_voice: channel=%d\n",channel);
680 outl(mask, ALI_REG(codec,codec->chregs.regs.start));
681}
682#endif
683
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200684static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 unsigned int mask = 1 << (channel & 0x1f);
687
688 snd_ali_printk("stop_voice: channel=%d\n",channel);
689 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
690}
691
692/*
693 * S/PDIF Part
694 */
695
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100696static void snd_ali_delay(struct snd_ali *codec,int interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 unsigned long begintimer,currenttimer;
699
700 begintimer = inl(ALI_REG(codec, ALI_STIMER));
701 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
702
703 while (currenttimer < begintimer + interval) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200704 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 break;
706 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200707 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709}
710
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100711static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200713 u16 wval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 u16 count = 0;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200715 u8 bval, R1 = 0, R2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200717 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 bval |= 0x1F;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200719 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200721 while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 count ++;
723 snd_ali_delay(codec, 6);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200724 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 R1 = bval & 0x1F;
726 }
727
728 if (count > 50000) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200729 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return;
731 }
732
Andrew Morton1c397322007-06-11 12:23:31 +0200733 for (count = 0; count <= 50000; count++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 snd_ali_delay(codec, 6);
735 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
736 R2 = bval & 0x1F;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200737 if (R2 != R1)
738 R1 = R2;
739 else
740 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742
743 if (count > 50000) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200744 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return;
746 }
747
748 if (R2 >= 0x0b && R2 <= 0x0e) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200749 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
750 wval &= 0xe0f0;
751 wval |= (0x09 << 8) | 0x05;
752 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200754 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
755 outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 } else if (R2 == 0x12) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200757 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
758 wval &= 0xe0f0;
759 wval |= (0x0e << 8) | 0x08;
760 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200762 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
763 outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765}
766
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100767static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200769 u32 dwRate;
770 u8 bval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200772 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
773 bval &= 0x7f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 bval |= 0x40;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200775 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 snd_ali_detect_spdif_rate(codec);
778
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200779 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
780 bval &= 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200782 switch (bval) {
783 case 0: dwRate = 44100; break;
784 case 1: dwRate = 48000; break;
785 case 2: dwRate = 32000; break;
786 default: dwRate = 0; break;
787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 return dwRate;
790}
791
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100792static void snd_ali_enable_spdif_in(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 unsigned int dwVal;
795
796 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
797 dwVal |= ALI_SPDIF_IN_SUPPORT;
798 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
799
800 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
801 dwVal |= 0x02;
802 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
803
804 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
805}
806
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100807static void snd_ali_disable_spdif_in(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 unsigned int dwVal;
810
811 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
812 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
813 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
814
815 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
816}
817
818
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100819static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 unsigned char bVal;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200822 unsigned int dwRate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200824 switch (rate) {
825 case 32000: dwRate = 0x300; break;
826 case 48000: dwRate = 0x200; break;
827 default: dwRate = 0; break;
828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
831 bVal &= (unsigned char)(~(1<<6));
832
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200833 bVal |= 0x80; /* select right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
835 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
836
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200837 bVal &= ~0x80; /* select left */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
839 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
840}
841
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100842static void snd_ali_enable_spdif_out(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 unsigned short wVal;
845 unsigned char bVal;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200846 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 pci_dev = codec->pci_m1533;
849 if (pci_dev == NULL)
850 return;
851 pci_read_config_byte(pci_dev, 0x61, &bVal);
852 bVal |= 0x40;
853 pci_write_config_byte(pci_dev, 0x61, bVal);
854 pci_read_config_byte(pci_dev, 0x7d, &bVal);
855 bVal |= 0x01;
856 pci_write_config_byte(pci_dev, 0x7d, bVal);
857
858 pci_read_config_byte(pci_dev, 0x7e, &bVal);
859 bVal &= (~0x20);
860 bVal |= 0x10;
861 pci_write_config_byte(pci_dev, 0x7e, bVal);
862
863 bVal = inb(ALI_REG(codec, ALI_SCTRL));
864 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
865
866 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
867 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
868
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200869 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
870 wVal |= ALI_SPDIF_OUT_SEL_PCM;
871 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
872 snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100875static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200877 unsigned short wVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
880 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
881 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
882/*
883 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
884 if (flag & ALI_SPDIF_OUT_NON_PCM)
885 wVal |= 0x0002;
886 else
887 wVal &= (~0x0002);
888 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
889*/
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200890 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100893static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200895 unsigned short wVal;
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
898 wVal |= ALI_SPDIF_OUT_SEL_PCM;
899 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
900
901 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
902}
903
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100904static void snd_ali_disable_spdif_out(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 unsigned char bVal;
907
908 bVal = inb(ALI_REG(codec, ALI_SCTRL));
909 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
910
911 snd_ali_disable_spdif_chnout(codec);
912}
913
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200914static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200916 struct snd_ali_voice *pvoice;
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100917 struct snd_pcm_runtime *runtime;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200918 struct snd_ali_channel_control *pchregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 unsigned int old, mask;
920#ifdef ALI_DEBUG
921 unsigned int temp, cspf;
922#endif
923
924 pchregs = &(codec->chregs);
925
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200926 /* check if interrupt occurred for channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 old = pchregs->data.aint;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200928 mask = 1U << (channel & 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 if (!(old & mask))
931 return;
932
933 pvoice = &codec->synth.voices[channel];
934 runtime = pvoice->substream->runtime;
935
936 udelay(100);
937 spin_lock(&codec->reg_lock);
938
939 if (pvoice->pcm && pvoice->substream) {
940 /* pcm interrupt */
941#ifdef ALI_DEBUG
942 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
943 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
944 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
945#endif
946 if (pvoice->running) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200947 snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",
948 (u16)temp, cspf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 spin_unlock(&codec->reg_lock);
950 snd_pcm_period_elapsed(pvoice->substream);
951 spin_lock(&codec->reg_lock);
952 } else {
953 snd_ali_stop_voice(codec, channel);
954 snd_ali_disable_voice_irq(codec, channel);
955 }
956 } else if (codec->synth.voices[channel].synth) {
957 /* synth interrupt */
958 } else if (codec->synth.voices[channel].midi) {
959 /* midi interrupt */
960 } else {
961 /* unknown interrupt */
962 snd_ali_stop_voice(codec, channel);
963 snd_ali_disable_voice_irq(codec, channel);
964 }
965 spin_unlock(&codec->reg_lock);
966 outl(mask,ALI_REG(codec,pchregs->regs.aint));
967 pchregs->data.aint = old & (~mask);
968}
969
David Howells7d12e782006-10-05 14:55:46 +0100970static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100972 struct snd_ali *codec = dev_id;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200973 int channel;
974 unsigned int audio_int;
975 struct snd_ali_channel_control *pchregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200977 if (codec == NULL || !codec->hw_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return IRQ_NONE;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200979
980 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
981 if (!audio_int)
982 return IRQ_NONE;
983
984 pchregs = &(codec->chregs);
985 if (audio_int & ADDRESS_IRQ) {
986 /* get interrupt status for all channels */
987 pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
988 for (channel = 0; channel < ALI_CHANNELS; channel++)
989 snd_ali_update_ptr(codec, channel);
990 }
991 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
992 ALI_REG(codec, ALI_MISCINT));
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return IRQ_HANDLED;
995}
996
997
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200998static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
999 int type, int rec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001001 struct snd_ali_voice *pvoice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 int idx;
1003
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001004 snd_ali_printk("alloc_voice: type=%d rec=%d\n", type, rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Takashi Iwai755e1372005-11-11 21:05:27 +01001006 spin_lock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001008 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
1009 snd_ali_find_free_channel(codec,rec);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001010 if (idx < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001011 snd_printk(KERN_ERR "ali_alloc_voice: err.\n");
Takashi Iwai755e1372005-11-11 21:05:27 +01001012 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return NULL;
1014 }
1015 pvoice = &(codec->synth.voices[idx]);
Takashi Iwai755e1372005-11-11 21:05:27 +01001016 pvoice->codec = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 pvoice->use = 1;
1018 pvoice->pcm = 1;
1019 pvoice->mode = rec;
Takashi Iwai755e1372005-11-11 21:05:27 +01001020 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return pvoice;
1022 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001023 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return NULL;
1025}
1026
1027
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001028static void snd_ali_free_voice(struct snd_ali * codec,
1029 struct snd_ali_voice *pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 void (*private_free)(void *);
1032 void *private_data;
1033
1034 snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
1035 if (pvoice == NULL || !pvoice->use)
1036 return;
1037 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
Takashi Iwai755e1372005-11-11 21:05:27 +01001038 spin_lock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 private_free = pvoice->private_free;
1040 private_data = pvoice->private_data;
1041 pvoice->private_free = NULL;
1042 pvoice->private_data = NULL;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001043 if (pvoice->pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 snd_ali_free_channel_pcm(codec, pvoice->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 pvoice->use = pvoice->pcm = pvoice->synth = 0;
1046 pvoice->substream = NULL;
Takashi Iwai755e1372005-11-11 21:05:27 +01001047 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (private_free)
1049 private_free(private_data);
1050}
1051
1052
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001053static void snd_ali_clear_voices(struct snd_ali *codec,
1054 unsigned int v_min,
1055 unsigned int v_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 unsigned int i;
1058
1059 for (i = v_min; i <= v_max; i++) {
1060 snd_ali_stop_voice(codec, i);
1061 snd_ali_disable_voice_irq(codec, i);
1062 }
1063}
1064
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001065static void snd_ali_write_voice_regs(struct snd_ali *codec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 unsigned int Channel,
1067 unsigned int LBA,
1068 unsigned int CSO,
1069 unsigned int ESO,
1070 unsigned int DELTA,
1071 unsigned int ALPHA_FMS,
1072 unsigned int GVSEL,
1073 unsigned int PAN,
1074 unsigned int VOL,
1075 unsigned int CTRL,
1076 unsigned int EC)
1077{
1078 unsigned int ctlcmds[4];
1079
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001080 outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1083 ctlcmds[1] = LBA;
1084 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1085 ctlcmds[3] = (GVSEL << 31) |
1086 ((PAN & 0x0000007f) << 24) |
1087 ((VOL & 0x000000ff) << 16) |
1088 ((CTRL & 0x0000000f) << 12) |
1089 (EC & 0x00000fff);
1090
1091 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1092
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001093 outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
1094 outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
1095 outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
1096 outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1099 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1100}
1101
1102static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1103{
1104 unsigned int delta;
1105
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001106 if (rate < 4000)
1107 rate = 4000;
1108 if (rate > 48000)
1109 rate = 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 if (rec) {
1112 if (rate == 44100)
1113 delta = 0x116a;
1114 else if (rate == 8000)
1115 delta = 0x6000;
1116 else if (rate == 48000)
1117 delta = 0x1000;
1118 else
1119 delta = ((48000 << 12) / rate) & 0x0000ffff;
1120 } else {
1121 if (rate == 44100)
1122 delta = 0xeb3;
1123 else if (rate == 8000)
1124 delta = 0x2ab;
1125 else if (rate == 48000)
1126 delta = 0x1000;
1127 else
1128 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1129 }
1130
1131 return delta;
1132}
1133
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001134static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 unsigned int CTRL;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001137 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 /* set ctrl mode
1140 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1141 */
1142 CTRL = 0x00000001;
1143 if (snd_pcm_format_width(runtime->format) == 16)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001144 CTRL |= 0x00000008; /* 16-bit data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (!snd_pcm_format_unsigned(runtime->format))
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001146 CTRL |= 0x00000002; /* signed data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (runtime->channels > 1)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001148 CTRL |= 0x00000004; /* stereo data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return CTRL;
1150}
1151
1152/*
1153 * PCM part
1154 */
1155
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001156static int snd_ali_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int cmd)
1158
1159{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001160 struct snd_ali *codec = snd_pcm_substream_chip(substream);
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001161 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 unsigned int what, whati, capture_flag;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001163 struct snd_ali_voice *pvoice, *evoice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 unsigned int val;
1165 int do_start;
1166
1167 switch (cmd) {
1168 case SNDRV_PCM_TRIGGER_START:
1169 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001170 do_start = 1;
1171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 case SNDRV_PCM_TRIGGER_STOP:
1173 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001174 do_start = 0;
1175 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 default:
1177 return -EINVAL;
1178 }
1179
1180 what = whati = capture_flag = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +01001181 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001182 if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
1183 pvoice = s->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 evoice = pvoice->extra;
1185 what |= 1 << (pvoice->number & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001186 if (evoice == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 whati |= 1 << (pvoice->number & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001188 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 whati |= 1 << (evoice->number & 0x1f);
1190 what |= 1 << (evoice->number & 0x1f);
1191 }
1192 if (do_start) {
1193 pvoice->running = 1;
1194 if (evoice != NULL)
1195 evoice->running = 1;
1196 } else {
1197 pvoice->running = 0;
1198 if (evoice != NULL)
1199 evoice->running = 0;
1200 }
1201 snd_pcm_trigger_done(s, substream);
1202 if (pvoice->mode)
1203 capture_flag = 1;
1204 }
1205 }
1206 spin_lock(&codec->reg_lock);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001207 if (!do_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 outl(what, ALI_REG(codec, ALI_STOP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 val = inl(ALI_REG(codec, ALI_AINTEN));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001210 if (do_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 val |= whati;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001212 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 val &= ~whati;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 outl(val, ALI_REG(codec, ALI_AINTEN));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001215 if (do_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 outl(what, ALI_REG(codec, ALI_START));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001217 snd_ali_printk("trigger: what=%xh whati=%xh\n", what, whati);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 spin_unlock(&codec->reg_lock);
1219
1220 return 0;
1221}
1222
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001223static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001224 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001226 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1227 struct snd_pcm_runtime *runtime = substream->runtime;
1228 struct snd_ali_voice *pvoice = runtime->private_data;
1229 struct snd_ali_voice *evoice = pvoice->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int err;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001231
1232 err = snd_pcm_lib_malloc_pages(substream,
1233 params_buffer_bytes(hw_params));
1234 if (err < 0)
1235 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 /* voice management */
1238
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001239 if (params_buffer_size(hw_params) / 2 !=
1240 params_period_size(hw_params)) {
1241 if (!evoice) {
1242 evoice = snd_ali_alloc_voice(codec,
1243 SNDRV_ALI_VOICE_TYPE_PCM,
1244 0, -1);
1245 if (!evoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return -ENOMEM;
1247 pvoice->extra = evoice;
1248 evoice->substream = substream;
1249 }
1250 } else {
Takashi Iwai27043642007-05-19 16:30:35 +02001251 if (evoice) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 snd_ali_free_voice(codec, evoice);
1253 pvoice->extra = evoice = NULL;
1254 }
1255 }
1256
1257 return 0;
1258}
1259
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001260static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001262 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1263 struct snd_pcm_runtime *runtime = substream->runtime;
1264 struct snd_ali_voice *pvoice = runtime->private_data;
1265 struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 snd_pcm_lib_free_pages(substream);
Takashi Iwai27043642007-05-19 16:30:35 +02001268 if (evoice) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 snd_ali_free_voice(codec, evoice);
1270 pvoice->extra = NULL;
1271 }
1272 return 0;
1273}
1274
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001275static int snd_ali_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001276 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001278 return snd_pcm_lib_malloc_pages(substream,
1279 params_buffer_bytes(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001282static int snd_ali_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 return snd_pcm_lib_free_pages(substream);
1285}
1286
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001287static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001289 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1290 struct snd_pcm_runtime *runtime = substream->runtime;
1291 struct snd_ali_voice *pvoice = runtime->private_data;
1292 struct snd_ali_voice *evoice = pvoice->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 unsigned int LBA;
1295 unsigned int Delta;
1296 unsigned int ESO;
1297 unsigned int CTRL;
1298 unsigned int GVSEL;
1299 unsigned int PAN;
1300 unsigned int VOL;
1301 unsigned int EC;
1302
1303 snd_ali_printk("playback_prepare ...\n");
1304
Takashi Iwai755e1372005-11-11 21:05:27 +01001305 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 /* set Delta (rate) value */
1308 Delta = snd_ali_convert_rate(runtime->rate, 0);
1309
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001310 if (pvoice->number == ALI_SPDIF_IN_CHANNEL ||
1311 pvoice->number == ALI_PCM_IN_CHANNEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 snd_ali_disable_special_channel(codec, pvoice->number);
1313 else if (codec->spdif_support &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001314 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
1315 ALI_SPDIF_OUT_CH_ENABLE)
1316 && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1318 Delta = 0x1000;
1319 }
1320
1321 /* set Loop Back Address */
1322 LBA = runtime->dma_addr;
1323
1324 /* set interrupt count size */
1325 pvoice->count = runtime->period_size;
1326
1327 /* set target ESO for channel */
1328 pvoice->eso = runtime->buffer_size;
1329
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001330 snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",
1331 pvoice->eso, pvoice->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 /* set ESO to capture first MIDLP interrupt */
1334 ESO = pvoice->eso -1;
1335 /* set ctrl mode */
1336 CTRL = snd_ali_control_mode(substream);
1337
1338 GVSEL = 1;
1339 PAN = 0;
1340 VOL = 0;
1341 EC = 0;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001342 snd_ali_printk("playback_prepare:\n");
1343 snd_ali_printk("ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
1344 pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1345 snd_ali_write_voice_regs(codec,
1346 pvoice->number,
1347 LBA,
1348 0, /* cso */
1349 ESO,
1350 Delta,
1351 0, /* alpha */
1352 GVSEL,
1353 PAN,
1354 VOL,
1355 CTRL,
1356 EC);
Takashi Iwai27043642007-05-19 16:30:35 +02001357 if (evoice) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 evoice->count = pvoice->count;
1359 evoice->eso = pvoice->count << 1;
1360 ESO = evoice->eso - 1;
1361 snd_ali_write_voice_regs(codec,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001362 evoice->number,
1363 LBA,
1364 0, /* cso */
1365 ESO,
1366 Delta,
1367 0, /* alpha */
1368 GVSEL,
1369 0x7f,
1370 0x3ff,
1371 CTRL,
1372 EC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001374 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return 0;
1376}
1377
1378
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001379static int snd_ali_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001381 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1382 struct snd_pcm_runtime *runtime = substream->runtime;
1383 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 unsigned int LBA;
1385 unsigned int Delta;
1386 unsigned int ESO;
1387 unsigned int CTRL;
1388 unsigned int GVSEL;
1389 unsigned int PAN;
1390 unsigned int VOL;
1391 unsigned int EC;
1392 u8 bValue;
1393
Takashi Iwai755e1372005-11-11 21:05:27 +01001394 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001396 snd_ali_printk("ali_prepare...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 snd_ali_enable_special_channel(codec,pvoice->number);
1399
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001400 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1401 pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
1402 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001404 /* Prepare capture intr channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1406
1407 unsigned int rate;
1408
Takashi Iwai755e1372005-11-11 21:05:27 +01001409 spin_unlock_irq(&codec->reg_lock);
1410 if (codec->revision != ALI_5451_V02)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return -1;
Takashi Iwai755e1372005-11-11 21:05:27 +01001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 rate = snd_ali_get_spdif_in_rate(codec);
1414 if (rate == 0) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001415 snd_printk(KERN_WARNING "ali_capture_preapre: "
1416 "spdif rate detect err!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 rate = 48000;
1418 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001419 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1421 if (bValue & 0x10) {
1422 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
Takashi Iwai99b359b2005-10-20 18:26:44 +02001423 printk(KERN_WARNING "clear SPDIF parity error flag.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425
1426 if (rate != 48000)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001427 Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001430 /* set target ESO for channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 pvoice->eso = runtime->buffer_size;
1432
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001433 /* set interrupt count size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 pvoice->count = runtime->period_size;
1435
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001436 /* set Loop Back Address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 LBA = runtime->dma_addr;
1438
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001439 /* set ESO to capture first MIDLP interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 ESO = pvoice->eso - 1;
1441 CTRL = snd_ali_control_mode(substream);
1442 GVSEL = 0;
1443 PAN = 0x00;
1444 VOL = 0x00;
1445 EC = 0;
1446
1447 snd_ali_write_voice_regs( codec,
1448 pvoice->number,
1449 LBA,
1450 0, /* cso */
1451 ESO,
1452 Delta,
1453 0, /* alpha */
1454 GVSEL,
1455 PAN,
1456 VOL,
1457 CTRL,
1458 EC);
1459
Takashi Iwai755e1372005-11-11 21:05:27 +01001460 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 return 0;
1463}
1464
1465
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001466static snd_pcm_uframes_t
1467snd_ali_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001469 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1470 struct snd_pcm_runtime *runtime = substream->runtime;
1471 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 unsigned int cso;
1473
1474 spin_lock(&codec->reg_lock);
1475 if (!pvoice->running) {
1476 spin_unlock(&codec->reg_lock);
1477 return 0;
1478 }
1479 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1480 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1481 spin_unlock(&codec->reg_lock);
1482 snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1483
1484 return cso;
1485}
1486
1487
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001488static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001490 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1491 struct snd_pcm_runtime *runtime = substream->runtime;
1492 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 unsigned int cso;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Takashi Iwai755e1372005-11-11 21:05:27 +01001495 spin_lock(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (!pvoice->running) {
Takashi Iwai755e1372005-11-11 21:05:27 +01001497 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return 0;
1499 }
1500 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1501 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
Takashi Iwai755e1372005-11-11 21:05:27 +01001502 spin_unlock(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 return cso;
1505}
1506
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001507static struct snd_pcm_hardware snd_ali_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001509 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1510 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1511 SNDRV_PCM_INFO_MMAP_VALID |
1512 SNDRV_PCM_INFO_RESUME |
1513 SNDRV_PCM_INFO_SYNC_START),
1514 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1515 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1516 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 .rate_min = 4000,
1518 .rate_max = 48000,
1519 .channels_min = 1,
1520 .channels_max = 2,
1521 .buffer_bytes_max = (256*1024),
1522 .period_bytes_min = 64,
1523 .period_bytes_max = (256*1024),
1524 .periods_min = 1,
1525 .periods_max = 1024,
1526 .fifo_size = 0,
1527};
1528
1529/*
1530 * Capture support device description
1531 */
1532
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001533static struct snd_pcm_hardware snd_ali_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001535 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1536 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1537 SNDRV_PCM_INFO_MMAP_VALID |
1538 SNDRV_PCM_INFO_RESUME |
1539 SNDRV_PCM_INFO_SYNC_START),
1540 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1541 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1542 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 .rate_min = 4000,
1544 .rate_max = 48000,
1545 .channels_min = 1,
1546 .channels_max = 2,
1547 .buffer_bytes_max = (128*1024),
1548 .period_bytes_min = 64,
1549 .period_bytes_max = (128*1024),
1550 .periods_min = 1,
1551 .periods_max = 1024,
1552 .fifo_size = 0,
1553};
1554
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001555static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001557 struct snd_ali_voice *pvoice = runtime->private_data;
1558 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 if (pvoice) {
1561 codec = pvoice->codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 snd_ali_free_voice(pvoice->codec, pvoice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564}
1565
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001566static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
1567 int channel, struct snd_pcm_hardware *phw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001569 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1570 struct snd_pcm_runtime *runtime = substream->runtime;
1571 struct snd_ali_voice *pvoice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001573 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
1574 channel);
1575 if (!pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 pvoice->substream = substream;
1579 runtime->private_data = pvoice;
1580 runtime->private_free = snd_ali_pcm_free_substream;
1581
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001582 runtime->hw = *phw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 snd_pcm_set_sync(substream);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001584 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1585 0, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return 0;
1587}
1588
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001589static int snd_ali_playback_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001590{
1591 return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1592}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001594static int snd_ali_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001596 return snd_ali_open(substream, 1, -1, &snd_ali_capture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001599static int snd_ali_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 return 0;
1602}
1603
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001604static int snd_ali_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001606 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1607 struct snd_ali_voice *pvoice = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 snd_ali_disable_special_channel(codec,pvoice->number);
1610
1611 return 0;
1612}
1613
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001614static struct snd_pcm_ops snd_ali_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 .open = snd_ali_playback_open,
1616 .close = snd_ali_playback_close,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001617 .ioctl = snd_pcm_lib_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 .hw_params = snd_ali_playback_hw_params,
1619 .hw_free = snd_ali_playback_hw_free,
1620 .prepare = snd_ali_playback_prepare,
1621 .trigger = snd_ali_trigger,
1622 .pointer = snd_ali_playback_pointer,
1623};
1624
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001625static struct snd_pcm_ops snd_ali_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 .open = snd_ali_capture_open,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001627 .close = snd_ali_close,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001628 .ioctl = snd_pcm_lib_ioctl,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001629 .hw_params = snd_ali_hw_params,
1630 .hw_free = snd_ali_hw_free,
1631 .prepare = snd_ali_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 .trigger = snd_ali_trigger,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001633 .pointer = snd_ali_pointer,
1634};
1635
1636/*
1637 * Modem PCM
1638 */
1639
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001640static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
1641 struct snd_pcm_hw_params *hw_params)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001642{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001643 struct snd_ali *chip = snd_pcm_substream_chip(substream);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001644 unsigned int modem_num = chip->num_of_codecs - 1;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001645 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
1646 params_rate(hw_params));
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001647 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1648 return snd_ali_hw_params(substream, hw_params);
1649}
1650
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001651static struct snd_pcm_hardware snd_ali_modem =
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001652{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001653 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1654 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1655 SNDRV_PCM_INFO_MMAP_VALID |
1656 SNDRV_PCM_INFO_RESUME |
1657 SNDRV_PCM_INFO_SYNC_START),
1658 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1659 .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
1660 SNDRV_PCM_RATE_16000),
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001661 .rate_min = 8000,
1662 .rate_max = 16000,
1663 .channels_min = 1,
1664 .channels_max = 1,
1665 .buffer_bytes_max = (256*1024),
1666 .period_bytes_min = 64,
1667 .period_bytes_max = (256*1024),
1668 .periods_min = 1,
1669 .periods_max = 1024,
1670 .fifo_size = 0,
1671};
1672
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001673static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
1674 int channel)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001675{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001676 static unsigned int rates[] = {8000, 9600, 12000, 16000};
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001677 static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001678 .count = ARRAY_SIZE(rates),
1679 .list = rates,
1680 .mask = 0,
1681 };
1682 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001683
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001684 if (err)
1685 return err;
1686 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1687 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1688}
1689
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001690static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001691{
1692 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1693}
1694
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001695static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001696{
1697 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1698}
1699
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001700static struct snd_pcm_ops snd_ali_modem_playback_ops = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001701 .open = snd_ali_modem_playback_open,
1702 .close = snd_ali_close,
1703 .ioctl = snd_pcm_lib_ioctl,
1704 .hw_params = snd_ali_modem_hw_params,
1705 .hw_free = snd_ali_hw_free,
1706 .prepare = snd_ali_prepare,
1707 .trigger = snd_ali_trigger,
1708 .pointer = snd_ali_pointer,
1709};
1710
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001711static struct snd_pcm_ops snd_ali_modem_capture_ops = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001712 .open = snd_ali_modem_capture_open,
1713 .close = snd_ali_close,
1714 .ioctl = snd_pcm_lib_ioctl,
1715 .hw_params = snd_ali_modem_hw_params,
1716 .hw_free = snd_ali_hw_free,
1717 .prepare = snd_ali_prepare,
1718 .trigger = snd_ali_trigger,
1719 .pointer = snd_ali_pointer,
1720};
1721
1722
1723struct ali_pcm_description {
1724 char *name;
1725 unsigned int playback_num;
1726 unsigned int capture_num;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001727 struct snd_pcm_ops *playback_ops;
1728 struct snd_pcm_ops *capture_ops;
Sasha Khapyorsky6632d192005-09-29 11:48:17 +02001729 unsigned short class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730};
1731
1732
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001733static void snd_ali_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001735 struct snd_ali *codec = pcm->private_data;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001736 codec->pcm[pcm->device] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001739
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001740static int __devinit snd_ali_pcm(struct snd_ali * codec, int device,
1741 struct ali_pcm_description *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001743 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 int err;
1745
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001746 err = snd_pcm_new(codec->card, desc->name, device,
1747 desc->playback_num, desc->capture_num, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (err < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001749 snd_printk(KERN_ERR "snd_ali_pcm: err called snd_pcm_new.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return err;
1751 }
1752 pcm->private_data = codec;
1753 pcm->private_free = snd_ali_pcm_free;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001754 if (desc->playback_ops)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001755 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1756 desc->playback_ops);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001757 if (desc->capture_ops)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001758 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1759 desc->capture_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001762 snd_dma_pci_data(codec->pci),
1763 64*1024, 128*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 pcm->info_flags = 0;
Sasha Khapyorsky6632d192005-09-29 11:48:17 +02001766 pcm->dev_class = desc->class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001768 strcpy(pcm->name, desc->name);
1769 codec->pcm[0] = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return 0;
1771}
1772
Clemens Ladischa53fc182005-08-11 15:59:17 +02001773static struct ali_pcm_description ali_pcms[] = {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001774 { .name = "ALI 5451",
1775 .playback_num = ALI_CHANNELS,
1776 .capture_num = 1,
1777 .playback_ops = &snd_ali_playback_ops,
1778 .capture_ops = &snd_ali_capture_ops
1779 },
1780 { .name = "ALI 5451 modem",
1781 .playback_num = 1,
1782 .capture_num = 1,
1783 .playback_ops = &snd_ali_modem_playback_ops,
1784 .capture_ops = &snd_ali_modem_capture_ops,
1785 .class = SNDRV_PCM_CLASS_MODEM
1786 }
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001787};
1788
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001789static int __devinit snd_ali_build_pcms(struct snd_ali *codec)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001790{
1791 int i, err;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001792 for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
1793 err = snd_ali_pcm(codec, i, &ali_pcms[i]);
1794 if (err < 0)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001795 return err;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001796 }
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001797 return 0;
1798}
1799
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801#define ALI5451_SPDIF(xname, xindex, value) \
1802{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1803.info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1804.put = snd_ali5451_spdif_put, .private_value = value}
1805
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001806#define snd_ali5451_spdif_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001808static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
1809 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001811 struct snd_ali *codec = kcontrol->private_data;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001812 unsigned int spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001814 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Takashi Iwai755e1372005-11-11 21:05:27 +01001816 spin_lock_irq(&codec->reg_lock);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001817 switch (kcontrol->private_value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 case 0:
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001819 spdif_enable = (codec->spdif_mask & 0x02) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 break;
1821 case 1:
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001822 spdif_enable = ((codec->spdif_mask & 0x02) &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001823 (codec->spdif_mask & 0x04)) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 break;
1825 case 2:
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001826 spdif_enable = (codec->spdif_mask & 0x01) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 break;
1828 default:
1829 break;
1830 }
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001831 ucontrol->value.integer.value[0] = spdif_enable;
Takashi Iwai755e1372005-11-11 21:05:27 +01001832 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return 0;
1834}
1835
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001836static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
1837 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001839 struct snd_ali *codec = kcontrol->private_data;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001840 unsigned int change = 0, spdif_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001842 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Takashi Iwai755e1372005-11-11 21:05:27 +01001844 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 switch (kcontrol->private_value) {
1846 case 0:
1847 change = (codec->spdif_mask & 0x02) ? 1 : 0;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001848 change = change ^ spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (change) {
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001850 if (spdif_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 codec->spdif_mask |= 0x02;
1852 snd_ali_enable_spdif_out(codec);
1853 } else {
1854 codec->spdif_mask &= ~(0x02);
1855 codec->spdif_mask &= ~(0x04);
1856 snd_ali_disable_spdif_out(codec);
1857 }
1858 }
1859 break;
1860 case 1:
1861 change = (codec->spdif_mask & 0x04) ? 1 : 0;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001862 change = change ^ spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (change && (codec->spdif_mask & 0x02)) {
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001864 if (spdif_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 codec->spdif_mask |= 0x04;
1866 snd_ali_enable_spdif_chnout(codec);
1867 } else {
1868 codec->spdif_mask &= ~(0x04);
1869 snd_ali_disable_spdif_chnout(codec);
1870 }
1871 }
1872 break;
1873 case 2:
1874 change = (codec->spdif_mask & 0x01) ? 1 : 0;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001875 change = change ^ spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (change) {
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001877 if (spdif_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 codec->spdif_mask |= 0x01;
1879 snd_ali_enable_spdif_in(codec);
1880 } else {
1881 codec->spdif_mask &= ~(0x01);
1882 snd_ali_disable_spdif_in(codec);
1883 }
1884 }
1885 break;
1886 default:
1887 break;
1888 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001889 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 return change;
1892}
1893
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001894static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 /* spdif aplayback switch */
1896 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001897 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 /* spdif out to spdif channel */
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001899 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 /* spdif in from spdif channel */
1901 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1902};
1903
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001904static int __devinit snd_ali_mixer(struct snd_ali * codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001906 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 unsigned int idx;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001908 int i, err;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001909 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 .write = snd_ali_codec_write,
1911 .read = snd_ali_codec_read,
1912 };
1913
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001914 err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
1915 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 memset(&ac97, 0, sizeof(ac97));
1919 ac97.private_data = codec;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001920
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001921 for (i = 0; i < codec->num_of_codecs; i++) {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001922 ac97.num = i;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001923 err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
1924 if (err < 0) {
1925 snd_printk(KERN_ERR
1926 "ali mixer %d creating error.\n", i);
1927 if (i == 0)
Takashi Iwai4d060fd2005-10-04 13:50:44 +02001928 return err;
1929 codec->num_of_codecs = 1;
1930 break;
1931 }
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001932 }
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (codec->spdif_support) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001935 for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1936 err = snd_ctl_add(codec->card,
1937 snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1938 if (err < 0)
1939 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941 }
1942 return 0;
1943}
1944
1945#ifdef CONFIG_PM
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001946static int ali_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001948 struct snd_card *card = pci_get_drvdata(pci);
1949 struct snd_ali *chip = card->private_data;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001950 struct snd_ali_image *im;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 int i, j;
1952
1953 im = chip->image;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001954 if (!im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return 0;
1956
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001957 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001958 for (i = 0; i < chip->num_of_codecs; i++) {
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001959 snd_pcm_suspend_all(chip->pcm[i]);
1960 snd_ac97_suspend(chip->ac97[i]);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 spin_lock_irq(&chip->reg_lock);
1964
1965 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001966 /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1968
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001969 /* disable all IRQ bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 outl(0, ALI_REG(chip, ALI_MISCINT));
1971
1972 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1973 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1974 continue;
1975 im->regs[i] = inl(ALI_REG(chip, i*4));
1976 }
1977
1978 for (i = 0; i < ALI_CHANNELS; i++) {
1979 outb(i, ALI_REG(chip, ALI_GC_CIR));
1980 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1981 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1982 }
1983
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001984 /* stop all HW channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1986
1987 spin_unlock_irq(&chip->reg_lock);
Takashi Iwai30b35392006-10-11 18:52:53 +02001988
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001989 pci_disable_device(pci);
1990 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02001991 pci_set_power_state(pci, pci_choose_state(pci, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return 0;
1993}
1994
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001995static int ali_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001997 struct snd_card *card = pci_get_drvdata(pci);
1998 struct snd_ali *chip = card->private_data;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001999 struct snd_ali_image *im;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 int i, j;
2001
2002 im = chip->image;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002003 if (!im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return 0;
2005
Takashi Iwai30b35392006-10-11 18:52:53 +02002006 pci_set_power_state(pci, PCI_D0);
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002007 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002008 if (pci_enable_device(pci) < 0) {
2009 printk(KERN_ERR "ali5451: pci_enable_device failed, "
2010 "disabling device\n");
2011 snd_card_disconnect(card);
2012 return -EIO;
2013 }
2014 pci_set_master(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 spin_lock_irq(&chip->reg_lock);
2017
2018 for (i = 0; i < ALI_CHANNELS; i++) {
2019 outb(i, ALI_REG(chip, ALI_GC_CIR));
2020 for (j = 0; j < ALI_CHANNEL_REGS; j++)
2021 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
2022 }
2023
2024 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002025 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
2026 (i*4 == ALI_START))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 continue;
2028 outl(im->regs[i], ALI_REG(chip, i*4));
2029 }
2030
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002031 /* start HW channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002033 /* restore IRQ enable bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
2035
2036 spin_unlock_irq(&chip->reg_lock);
2037
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002038 for (i = 0 ; i < chip->num_of_codecs; i++)
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002039 snd_ac97_resume(chip->ac97[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002041 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return 0;
2043}
2044#endif /* CONFIG_PM */
2045
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002046static int snd_ali_free(struct snd_ali * codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 if (codec->hw_initialized)
2049 snd_ali_disable_address_interrupt(codec);
Jeff Garzikf000fd82008-04-22 13:50:34 +02002050 if (codec->irq >= 0)
Takashi Iwai437a5a42006-11-21 12:14:23 +01002051 free_irq(codec->irq, codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (codec->port)
2053 pci_release_regions(codec->pci);
2054 pci_disable_device(codec->pci);
2055#ifdef CONFIG_PM
2056 kfree(codec->image);
2057#endif
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002058 pci_dev_put(codec->pci_m1533);
2059 pci_dev_put(codec->pci_m7101);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 kfree(codec);
2061 return 0;
2062}
2063
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002064static int snd_ali_chip_init(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 unsigned int legacy;
2067 unsigned char temp;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002068 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 snd_ali_printk("chip initializing ... \n");
2071
2072 if (snd_ali_reset_5451(codec)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002073 snd_printk(KERN_ERR "ali_chip_init: reset 5451 error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return -1;
2075 }
2076
2077 if (codec->revision == ALI_5451_V02) {
2078 pci_dev = codec->pci_m1533;
2079 pci_read_config_byte(pci_dev, 0x59, &temp);
2080 temp |= 0x80;
2081 pci_write_config_byte(pci_dev, 0x59, temp);
2082
2083 pci_dev = codec->pci_m7101;
2084 pci_read_config_byte(pci_dev, 0xb8, &temp);
2085 temp |= 0x20;
2086 pci_write_config_byte(pci_dev, 0xB8, temp);
2087 }
2088
2089 pci_read_config_dword(codec->pci, 0x44, &legacy);
2090 legacy &= 0xff00ff00;
2091 legacy |= 0x000800aa;
2092 pci_write_config_dword(codec->pci, 0x44, legacy);
2093
2094 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2095 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2096 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2097 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2098 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2099
2100 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002101 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
2102 AC97_EXTENDED_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (codec->spdif_support) {
2104 snd_ali_enable_spdif_out(codec);
2105 codec->spdif_mask = 0x00000002;
2106 }
2107
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002108 codec->num_of_codecs = 1;
2109
2110 /* secondary codec - modem */
2111 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
2112 codec->num_of_codecs++;
2113 outl(inl(ALI_REG(codec, ALI_SCTRL)) |
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002114 (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
2115 ALI_SCTRL_LINE_OUT_EN),
2116 ALI_REG(codec, ALI_SCTRL));
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002117 }
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 snd_ali_printk("chip initialize succeed.\n");
2120 return 0;
2121
2122}
2123
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002124/* proc for register dump */
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002125static void snd_ali_proc_read(struct snd_info_entry *entry,
2126 struct snd_info_buffer *buf)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002127{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002128 struct snd_ali *codec = entry->private_data;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002129 int i;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002130 for (i = 0; i < 256 ; i+= 4)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002131 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
2132}
2133
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002134static void __devinit snd_ali_proc_init(struct snd_ali *codec)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002135{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002136 struct snd_info_entry *entry;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002137 if (!snd_card_proc_new(codec->card, "ali5451", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002138 snd_info_set_text_ops(entry, codec, snd_ali_proc_read);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002139}
2140
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002141static int __devinit snd_ali_resources(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 int err;
2144
2145 snd_ali_printk("resouces allocation ...\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002146 err = pci_request_regions(codec->pci, "ALI 5451");
2147 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return err;
2149 codec->port = pci_resource_start(codec->pci, 0);
2150
Takashi Iwai437a5a42006-11-21 12:14:23 +01002151 if (request_irq(codec->pci->irq, snd_ali_card_interrupt,
2152 IRQF_SHARED, "ALI 5451", codec)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002153 snd_printk(KERN_ERR "Unable to request irq.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return -EBUSY;
2155 }
2156 codec->irq = codec->pci->irq;
2157 snd_ali_printk("resouces allocated.\n");
2158 return 0;
2159}
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002160static int snd_ali_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002162 struct snd_ali *codec = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 snd_ali_free(codec);
2164 return 0;
2165}
2166
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002167static int __devinit snd_ali_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 struct pci_dev *pci,
2169 int pcm_streams,
2170 int spdif_support,
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002171 struct snd_ali ** r_ali)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002173 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 int i, err;
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002175 unsigned short cmdw;
2176 static struct snd_device_ops ops = {
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002177 .dev_free = snd_ali_dev_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 };
2179
2180 *r_ali = NULL;
2181
2182 snd_ali_printk("creating ...\n");
2183
2184 /* enable PCI device */
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002185 err = pci_enable_device(pci);
2186 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return err;
2188 /* check, if we can restrict PCI DMA transfers to 31 bits */
Matthias Gehre910638a2006-03-28 01:56:48 -08002189 if (pci_set_dma_mask(pci, DMA_31BIT_MASK) < 0 ||
2190 pci_set_consistent_dma_mask(pci, DMA_31BIT_MASK) < 0) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002191 snd_printk(KERN_ERR "architecture does not support "
2192 "31bit PCI busmaster DMA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 pci_disable_device(pci);
2194 return -ENXIO;
2195 }
2196
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002197 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
2198 if (!codec) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 pci_disable_device(pci);
2200 return -ENOMEM;
2201 }
2202
2203 spin_lock_init(&codec->reg_lock);
2204 spin_lock_init(&codec->voice_alloc);
2205
2206 codec->card = card;
2207 codec->pci = pci;
2208 codec->irq = -1;
Auke Kok44c10132007-06-08 15:46:36 -07002209 codec->revision = pci->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 codec->spdif_support = spdif_support;
2211
2212 if (pcm_streams < 1)
2213 pcm_streams = 1;
2214 if (pcm_streams > 32)
2215 pcm_streams = 32;
2216
2217 pci_set_master(pci);
2218 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2219 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2220 cmdw |= PCI_COMMAND_IO;
2221 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2222 }
2223 pci_set_master(pci);
2224
2225 if (snd_ali_resources(codec)) {
2226 snd_ali_free(codec);
2227 return -EBUSY;
2228 }
2229
2230 synchronize_irq(pci->irq);
2231
2232 codec->synth.chmap = 0;
2233 codec->synth.chcnt = 0;
2234 codec->spdif_mask = 0;
2235 codec->synth.synthcount = 0;
2236
2237 if (codec->revision == ALI_5451_V02)
2238 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2239 else
2240 codec->chregs.regs.ac97read = ALI_AC97_READ;
2241 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2242
2243 codec->chregs.regs.start = ALI_START;
2244 codec->chregs.regs.stop = ALI_STOP;
2245 codec->chregs.regs.aint = ALI_AINT;
2246 codec->chregs.regs.ainten = ALI_AINTEN;
2247
2248 codec->chregs.data.start = 0x00;
2249 codec->chregs.data.stop = 0x00;
2250 codec->chregs.data.aint = 0x00;
2251 codec->chregs.data.ainten = 0x00;
2252
2253 /* M1533: southbridge */
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002254 codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002255 if (!codec->pci_m1533) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2257 snd_ali_free(codec);
2258 return -ENODEV;
2259 }
2260 /* M7101: power management */
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002261 codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002262 if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2264 snd_ali_free(codec);
2265 return -ENODEV;
2266 }
2267
2268 snd_ali_printk("snd_device_new is called.\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002269 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops);
2270 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 snd_ali_free(codec);
2272 return err;
2273 }
2274
Takashi Iwaic187c042007-02-19 15:27:33 +01002275 snd_card_set_dev(card, &pci->dev);
2276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 /* initialise synth voices*/
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002278 for (i = 0; i < ALI_CHANNELS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 codec->synth.voices[i].number = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002281 err = snd_ali_chip_init(codec);
2282 if (err < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002283 snd_printk(KERN_ERR "ali create: chip init error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return err;
2285 }
2286
2287#ifdef CONFIG_PM
2288 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002289 if (!codec->image)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 snd_printk(KERN_WARNING "can't allocate apm buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291#endif
2292
2293 snd_ali_enable_address_interrupt(codec);
2294 codec->hw_initialized = 1;
2295
2296 *r_ali = codec;
2297 snd_ali_printk("created.\n");
2298 return 0;
2299}
2300
2301static int __devinit snd_ali_probe(struct pci_dev *pci,
2302 const struct pci_device_id *pci_id)
2303{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002304 struct snd_card *card;
2305 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 int err;
2307
2308 snd_ali_printk("probe ...\n");
2309
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002310 err = snd_card_create(index, id, THIS_MODULE, 0, &card);
2311 if (err < 0)
2312 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002314 err = snd_ali_create(card, pci, pcm_channels, spdif, &codec);
2315 if (err < 0)
2316 goto error;
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002317 card->private_data = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319 snd_ali_printk("mixer building ...\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002320 err = snd_ali_mixer(codec);
2321 if (err < 0)
2322 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 snd_ali_printk("pcm building ...\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002325 err = snd_ali_build_pcms(codec);
2326 if (err < 0)
2327 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002329 snd_ali_proc_init(codec);
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 strcpy(card->driver, "ALI5451");
2332 strcpy(card->shortname, "ALI 5451");
2333
Takashi Iwaiba8c3c32007-05-30 12:42:31 +02002334 sprintf(card->longname, "%s at 0x%lx, irq %i",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 card->shortname, codec->port, codec->irq);
2336
2337 snd_ali_printk("register card.\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002338 err = snd_card_register(card);
2339 if (err < 0)
2340 goto error;
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 pci_set_drvdata(pci, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return 0;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002344
2345 error:
2346 snd_card_free(card);
2347 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
2350static void __devexit snd_ali_remove(struct pci_dev *pci)
2351{
2352 snd_card_free(pci_get_drvdata(pci));
2353 pci_set_drvdata(pci, NULL);
2354}
2355
2356static struct pci_driver driver = {
2357 .name = "ALI 5451",
2358 .id_table = snd_ali_ids,
2359 .probe = snd_ali_probe,
2360 .remove = __devexit_p(snd_ali_remove),
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002361#ifdef CONFIG_PM
2362 .suspend = ali_suspend,
2363 .resume = ali_resume,
2364#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365};
2366
2367static int __init alsa_card_ali_init(void)
2368{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002369 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370}
2371
2372static void __exit alsa_card_ali_exit(void)
2373{
2374 pci_unregister_driver(&driver);
2375}
2376
2377module_init(alsa_card_ali_init)
2378module_exit(alsa_card_ali_exit)