blob: 76d76c08339bd0bb4caedf761740c25b1947cb3e [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);
Bartlomiej Zolnierkiewicz70bdbd32009-08-23 15:27:25 +0200313
314 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 res = snd_ali_5451_peek(codec,port);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200316 if (!(res & 0x8000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 0;
Bartlomiej Zolnierkiewicz70bdbd32009-08-23 15:27:25 +0200318 if (!time_after_eq(end_time, jiffies))
319 break;
Takashi Iwai755e1372005-11-11 21:05:27 +0100320 schedule_timeout_uninterruptible(1);
Bartlomiej Zolnierkiewicz70bdbd32009-08-23 15:27:25 +0200321 }
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 snd_ali_5451_poke(codec, port, res & ~0x8000);
324 snd_printdd("ali_codec_ready: codec is not ready.\n ");
325 return -EIO;
326}
327
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100328static int snd_ali_stimer_ready(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 unsigned long end_time;
331 unsigned long dwChk1,dwChk2;
332
333 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
Takashi Iwai755e1372005-11-11 21:05:27 +0100334 end_time = jiffies + msecs_to_jiffies(250);
Bartlomiej Zolnierkiewicz70bdbd32009-08-23 15:27:25 +0200335
336 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
338 if (dwChk2 != dwChk1)
339 return 0;
Bartlomiej Zolnierkiewicz70bdbd32009-08-23 15:27:25 +0200340 if (!time_after_eq(end_time, jiffies))
341 break;
Takashi Iwai755e1372005-11-11 21:05:27 +0100342 schedule_timeout_uninterruptible(1);
Bartlomiej Zolnierkiewicz70bdbd32009-08-23 15:27:25 +0200343 }
344
Takashi Iwai99b359b2005-10-20 18:26:44 +0200345 snd_printk(KERN_ERR "ali_stimer_read: stimer is not ready.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return -EIO;
347}
348
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100349static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200350 unsigned short reg,
351 unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200353 unsigned int dwVal;
354 unsigned int port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 if (reg >= 0x80) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200357 snd_printk(KERN_ERR "ali_codec_poke: reg(%xh) invalid.\n", reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return;
359 }
360
361 port = codec->chregs.regs.ac97write;
362
Takashi Iwai755e1372005-11-11 21:05:27 +0100363 if (snd_ali_codec_ready(codec, port) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return;
Takashi Iwai755e1372005-11-11 21:05:27 +0100365 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return;
367
368 dwVal = (unsigned int) (reg & 0xff);
369 dwVal |= 0x8000 | (val << 16);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200370 if (secondary)
371 dwVal |= 0x0080;
372 if (codec->revision == ALI_5451_V02)
373 dwVal |= 0x0100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200375 snd_ali_5451_poke(codec, port, dwVal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 return ;
378}
379
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200380static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
381 int secondary,
382 unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200384 unsigned int dwVal;
385 unsigned int port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (reg >= 0x80) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200388 snd_printk(KERN_ERR "ali_codec_peek: reg(%xh) invalid.\n", reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return ~0;
390 }
391
392 port = codec->chregs.regs.ac97read;
393
Takashi Iwai755e1372005-11-11 21:05:27 +0100394 if (snd_ali_codec_ready(codec, port) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return ~0;
Takashi Iwai755e1372005-11-11 21:05:27 +0100396 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return ~0;
398
399 dwVal = (unsigned int) (reg & 0xff);
400 dwVal |= 0x8000; /* bit 15*/
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200401 if (secondary)
402 dwVal |= 0x0080;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 snd_ali_5451_poke(codec, port, dwVal);
405
Takashi Iwai755e1372005-11-11 21:05:27 +0100406 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return ~0;
Takashi Iwai755e1372005-11-11 21:05:27 +0100408 if (snd_ali_codec_ready(codec, port) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return ~0;
410
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200411 return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100414static void snd_ali_codec_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 unsigned short reg,
416 unsigned short val )
417{
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100418 struct snd_ali *codec = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200421 if (reg == AC97_GPIO_STATUS) {
422 outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
423 ALI_REG(codec, ALI_AC97_GPIO));
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200424 return;
425 }
426 snd_ali_codec_poke(codec, ac97->num, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return ;
428}
429
430
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200431static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
432 unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100434 struct snd_ali *codec = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 snd_ali_printk("codec_read reg=%xh.\n", reg);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200437 return snd_ali_codec_peek(codec, ac97->num, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440/*
441 * AC97 Reset
442 */
443
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100444static int snd_ali_reset_5451(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200446 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 unsigned short wCount, wReg;
448 unsigned int dwVal;
449
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200450 pci_dev = codec->pci_m1533;
451 if (pci_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
453 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
454 udelay(5000);
455 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
456 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
457 udelay(5000);
458 }
459
460 pci_dev = codec->pci;
461 pci_read_config_dword(pci_dev, 0x44, &dwVal);
462 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
463 udelay(500);
464 pci_read_config_dword(pci_dev, 0x44, &dwVal);
465 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
466 udelay(5000);
467
468 wCount = 200;
469 while(wCount--) {
470 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200471 if ((wReg & 0x000f) == 0x000f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return 0;
473 udelay(5000);
474 }
475
476 /* non-fatal if you have a non PM capable codec */
477 /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
478 return 0;
479}
480
481#ifdef CODEC_RESET
482
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100483static int snd_ali_reset_codec(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200485 struct pci_dev *pci_dev;
486 unsigned char bVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 unsigned int dwVal;
488 unsigned short wCount, wReg;
489
490 pci_dev = codec->pci_m1533;
491
492 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
493 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
494 udelay(5000);
495 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
496 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
497 udelay(5000);
498
499 bVal = inb(ALI_REG(codec,ALI_SCTRL));
500 bVal |= 0x02;
501 outb(ALI_REG(codec,ALI_SCTRL),bVal);
502 udelay(5000);
503 bVal = inb(ALI_REG(codec,ALI_SCTRL));
504 bVal &= 0xfd;
505 outb(ALI_REG(codec,ALI_SCTRL),bVal);
506 udelay(15000);
507
508 wCount = 200;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200509 while (wCount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200511 if ((wReg & 0x000f) == 0x000f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return 0;
513 udelay(5000);
514 }
515 return -1;
516}
517
518#endif
519
520/*
521 * ALI 5451 Controller
522 */
523
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200524static void snd_ali_enable_special_channel(struct snd_ali *codec,
525 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200527 unsigned long dwVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200529 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 dwVal |= 1 << (channel & 0x0000001f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200531 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200534static void snd_ali_disable_special_channel(struct snd_ali *codec,
535 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200537 unsigned long dwVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200539 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 dwVal &= ~(1 << (channel & 0x0000001f));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200541 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200544static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 unsigned int gc;
547
548 gc = inl(ALI_REG(codec, ALI_GC_CIR));
549 gc |= ENDLP_IE;
550 gc |= MIDLP_IE;
551 outl( gc, ALI_REG(codec, ALI_GC_CIR));
552}
553
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200554static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 unsigned int gc;
557
558 gc = inl(ALI_REG(codec, ALI_GC_CIR));
559 gc &= ~ENDLP_IE;
560 gc &= ~MIDLP_IE;
561 outl(gc, ALI_REG(codec, ALI_GC_CIR));
562}
563
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200564#if 0 /* not used */
565static void snd_ali_enable_voice_irq(struct snd_ali *codec,
566 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 unsigned int mask;
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100569 struct snd_ali_channel_control *pchregs = &(codec->chregs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 snd_ali_printk("enable_voice_irq channel=%d\n",channel);
572
573 mask = 1 << (channel & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200574 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 pchregs->data.ainten |= mask;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200576 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578#endif
579
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200580static void snd_ali_disable_voice_irq(struct snd_ali *codec,
581 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 unsigned int mask;
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100584 struct snd_ali_channel_control *pchregs = &(codec->chregs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 snd_ali_printk("disable_voice_irq channel=%d\n",channel);
587
588 mask = 1 << (channel & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200589 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 pchregs->data.ainten &= ~mask;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200591 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100594static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 unsigned int idx = channel & 0x1f;
597
598 if (codec->synth.chcnt >= ALI_CHANNELS){
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200599 snd_printk(KERN_ERR
600 "ali_alloc_pcm_channel: no free channels.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return -1;
602 }
603
604 if (!(codec->synth.chmap & (1 << idx))) {
605 codec->synth.chmap |= 1 << idx;
606 codec->synth.chcnt++;
607 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
608 return idx;
609 }
610 return -1;
611}
612
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100613static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 int idx;
616 int result = -1;
617
618 snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
619
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200620 /* recording */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (rec) {
622 if (codec->spdif_support &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200623 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
624 ALI_SPDIF_IN_SUPPORT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 idx = ALI_SPDIF_IN_CHANNEL;
626 else
627 idx = ALI_PCM_IN_CHANNEL;
628
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200629 result = snd_ali_alloc_pcm_channel(codec, idx);
630 if (result >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return result;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200632 else {
633 snd_printk(KERN_ERR "ali_find_free_channel: "
634 "record channel is busy now.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return -1;
636 }
637 }
638
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200639 /* playback... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (codec->spdif_support &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200641 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
642 ALI_SPDIF_OUT_CH_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 idx = ALI_SPDIF_OUT_CHANNEL;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200644 result = snd_ali_alloc_pcm_channel(codec, idx);
645 if (result >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return result;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200647 else
648 snd_printk(KERN_ERR "ali_find_free_channel: "
649 "S/PDIF out channel is in busy now.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
652 for (idx = 0; idx < ALI_CHANNELS; idx++) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200653 result = snd_ali_alloc_pcm_channel(codec, idx);
654 if (result >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return result;
656 }
Takashi Iwai99b359b2005-10-20 18:26:44 +0200657 snd_printk(KERN_ERR "ali_find_free_channel: no free channels.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -1;
659}
660
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100661static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 unsigned int idx = channel & 0x0000001f;
664
665 snd_ali_printk("free_channel_pcm channel=%d\n",channel);
666
667 if (channel < 0 || channel >= ALI_CHANNELS)
668 return;
669
670 if (!(codec->synth.chmap & (1 << idx))) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200671 snd_printk(KERN_ERR "ali_free_channel_pcm: "
672 "channel %d is not in use.\n", channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return;
674 } else {
675 codec->synth.chmap &= ~(1 << idx);
676 codec->synth.chcnt--;
677 }
678}
679
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200680#if 0 /* not used */
681static void snd_ali_start_voice(struct snd_ali *codec, unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 unsigned int mask = 1 << (channel & 0x1f);
684
685 snd_ali_printk("start_voice: channel=%d\n",channel);
686 outl(mask, ALI_REG(codec,codec->chregs.regs.start));
687}
688#endif
689
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200690static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 unsigned int mask = 1 << (channel & 0x1f);
693
694 snd_ali_printk("stop_voice: channel=%d\n",channel);
695 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
696}
697
698/*
699 * S/PDIF Part
700 */
701
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100702static void snd_ali_delay(struct snd_ali *codec,int interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 unsigned long begintimer,currenttimer;
705
706 begintimer = inl(ALI_REG(codec, ALI_STIMER));
707 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
708
709 while (currenttimer < begintimer + interval) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200710 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 break;
712 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200713 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715}
716
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100717static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200719 u16 wval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 u16 count = 0;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200721 u8 bval, R1 = 0, R2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200723 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 bval |= 0x1F;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200725 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200727 while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 count ++;
729 snd_ali_delay(codec, 6);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200730 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 R1 = bval & 0x1F;
732 }
733
734 if (count > 50000) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200735 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return;
737 }
738
Andrew Morton1c397322007-06-11 12:23:31 +0200739 for (count = 0; count <= 50000; count++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 snd_ali_delay(codec, 6);
741 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
742 R2 = bval & 0x1F;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200743 if (R2 != R1)
744 R1 = R2;
745 else
746 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
749 if (count > 50000) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200750 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return;
752 }
753
754 if (R2 >= 0x0b && R2 <= 0x0e) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200755 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
756 wval &= 0xe0f0;
757 wval |= (0x09 << 8) | 0x05;
758 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200760 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
761 outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 } else if (R2 == 0x12) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200763 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
764 wval &= 0xe0f0;
765 wval |= (0x0e << 8) | 0x08;
766 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200768 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
769 outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771}
772
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100773static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200775 u32 dwRate;
776 u8 bval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200778 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
779 bval &= 0x7f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 bval |= 0x40;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200781 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 snd_ali_detect_spdif_rate(codec);
784
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200785 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
786 bval &= 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200788 switch (bval) {
789 case 0: dwRate = 44100; break;
790 case 1: dwRate = 48000; break;
791 case 2: dwRate = 32000; break;
792 default: dwRate = 0; break;
793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 return dwRate;
796}
797
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100798static void snd_ali_enable_spdif_in(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 unsigned int dwVal;
801
802 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
803 dwVal |= ALI_SPDIF_IN_SUPPORT;
804 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
805
806 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
807 dwVal |= 0x02;
808 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
809
810 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
811}
812
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100813static void snd_ali_disable_spdif_in(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 unsigned int dwVal;
816
817 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
818 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
819 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
820
821 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
822}
823
824
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100825static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 unsigned char bVal;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200828 unsigned int dwRate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200830 switch (rate) {
831 case 32000: dwRate = 0x300; break;
832 case 48000: dwRate = 0x200; break;
833 default: dwRate = 0; break;
834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
837 bVal &= (unsigned char)(~(1<<6));
838
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200839 bVal |= 0x80; /* select right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
841 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
842
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200843 bVal &= ~0x80; /* select left */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
845 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
846}
847
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100848static void snd_ali_enable_spdif_out(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 unsigned short wVal;
851 unsigned char bVal;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200852 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 pci_dev = codec->pci_m1533;
855 if (pci_dev == NULL)
856 return;
857 pci_read_config_byte(pci_dev, 0x61, &bVal);
858 bVal |= 0x40;
859 pci_write_config_byte(pci_dev, 0x61, bVal);
860 pci_read_config_byte(pci_dev, 0x7d, &bVal);
861 bVal |= 0x01;
862 pci_write_config_byte(pci_dev, 0x7d, bVal);
863
864 pci_read_config_byte(pci_dev, 0x7e, &bVal);
865 bVal &= (~0x20);
866 bVal |= 0x10;
867 pci_write_config_byte(pci_dev, 0x7e, bVal);
868
869 bVal = inb(ALI_REG(codec, ALI_SCTRL));
870 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
871
872 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
873 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
874
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200875 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
876 wVal |= ALI_SPDIF_OUT_SEL_PCM;
877 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
878 snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100881static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200883 unsigned short wVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
886 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
887 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
888/*
889 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
890 if (flag & ALI_SPDIF_OUT_NON_PCM)
891 wVal |= 0x0002;
892 else
893 wVal &= (~0x0002);
894 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
895*/
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200896 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100899static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200901 unsigned short wVal;
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
904 wVal |= ALI_SPDIF_OUT_SEL_PCM;
905 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
906
907 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
908}
909
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100910static void snd_ali_disable_spdif_out(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 unsigned char bVal;
913
914 bVal = inb(ALI_REG(codec, ALI_SCTRL));
915 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
916
917 snd_ali_disable_spdif_chnout(codec);
918}
919
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200920static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200922 struct snd_ali_voice *pvoice;
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100923 struct snd_pcm_runtime *runtime;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200924 struct snd_ali_channel_control *pchregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 unsigned int old, mask;
926#ifdef ALI_DEBUG
927 unsigned int temp, cspf;
928#endif
929
930 pchregs = &(codec->chregs);
931
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200932 /* check if interrupt occurred for channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 old = pchregs->data.aint;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200934 mask = 1U << (channel & 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 if (!(old & mask))
937 return;
938
939 pvoice = &codec->synth.voices[channel];
940 runtime = pvoice->substream->runtime;
941
942 udelay(100);
943 spin_lock(&codec->reg_lock);
944
945 if (pvoice->pcm && pvoice->substream) {
946 /* pcm interrupt */
947#ifdef ALI_DEBUG
948 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
949 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
950 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
951#endif
952 if (pvoice->running) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200953 snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",
954 (u16)temp, cspf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 spin_unlock(&codec->reg_lock);
956 snd_pcm_period_elapsed(pvoice->substream);
957 spin_lock(&codec->reg_lock);
958 } else {
959 snd_ali_stop_voice(codec, channel);
960 snd_ali_disable_voice_irq(codec, channel);
961 }
962 } else if (codec->synth.voices[channel].synth) {
963 /* synth interrupt */
964 } else if (codec->synth.voices[channel].midi) {
965 /* midi interrupt */
966 } else {
967 /* unknown interrupt */
968 snd_ali_stop_voice(codec, channel);
969 snd_ali_disable_voice_irq(codec, channel);
970 }
971 spin_unlock(&codec->reg_lock);
972 outl(mask,ALI_REG(codec,pchregs->regs.aint));
973 pchregs->data.aint = old & (~mask);
974}
975
David Howells7d12e782006-10-05 14:55:46 +0100976static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100978 struct snd_ali *codec = dev_id;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200979 int channel;
980 unsigned int audio_int;
981 struct snd_ali_channel_control *pchregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200983 if (codec == NULL || !codec->hw_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return IRQ_NONE;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200985
986 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
987 if (!audio_int)
988 return IRQ_NONE;
989
990 pchregs = &(codec->chregs);
991 if (audio_int & ADDRESS_IRQ) {
992 /* get interrupt status for all channels */
993 pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
994 for (channel = 0; channel < ALI_CHANNELS; channel++)
995 snd_ali_update_ptr(codec, channel);
996 }
997 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
998 ALI_REG(codec, ALI_MISCINT));
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return IRQ_HANDLED;
1001}
1002
1003
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001004static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
1005 int type, int rec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001007 struct snd_ali_voice *pvoice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 int idx;
1009
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001010 snd_ali_printk("alloc_voice: type=%d rec=%d\n", type, rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Takashi Iwai755e1372005-11-11 21:05:27 +01001012 spin_lock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001014 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
1015 snd_ali_find_free_channel(codec,rec);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001016 if (idx < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001017 snd_printk(KERN_ERR "ali_alloc_voice: err.\n");
Takashi Iwai755e1372005-11-11 21:05:27 +01001018 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return NULL;
1020 }
1021 pvoice = &(codec->synth.voices[idx]);
Takashi Iwai755e1372005-11-11 21:05:27 +01001022 pvoice->codec = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 pvoice->use = 1;
1024 pvoice->pcm = 1;
1025 pvoice->mode = rec;
Takashi Iwai755e1372005-11-11 21:05:27 +01001026 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return pvoice;
1028 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001029 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return NULL;
1031}
1032
1033
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001034static void snd_ali_free_voice(struct snd_ali * codec,
1035 struct snd_ali_voice *pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 void (*private_free)(void *);
1038 void *private_data;
1039
1040 snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
1041 if (pvoice == NULL || !pvoice->use)
1042 return;
1043 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
Takashi Iwai755e1372005-11-11 21:05:27 +01001044 spin_lock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 private_free = pvoice->private_free;
1046 private_data = pvoice->private_data;
1047 pvoice->private_free = NULL;
1048 pvoice->private_data = NULL;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001049 if (pvoice->pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 snd_ali_free_channel_pcm(codec, pvoice->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 pvoice->use = pvoice->pcm = pvoice->synth = 0;
1052 pvoice->substream = NULL;
Takashi Iwai755e1372005-11-11 21:05:27 +01001053 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (private_free)
1055 private_free(private_data);
1056}
1057
1058
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001059static void snd_ali_clear_voices(struct snd_ali *codec,
1060 unsigned int v_min,
1061 unsigned int v_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 unsigned int i;
1064
1065 for (i = v_min; i <= v_max; i++) {
1066 snd_ali_stop_voice(codec, i);
1067 snd_ali_disable_voice_irq(codec, i);
1068 }
1069}
1070
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001071static void snd_ali_write_voice_regs(struct snd_ali *codec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 unsigned int Channel,
1073 unsigned int LBA,
1074 unsigned int CSO,
1075 unsigned int ESO,
1076 unsigned int DELTA,
1077 unsigned int ALPHA_FMS,
1078 unsigned int GVSEL,
1079 unsigned int PAN,
1080 unsigned int VOL,
1081 unsigned int CTRL,
1082 unsigned int EC)
1083{
1084 unsigned int ctlcmds[4];
1085
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001086 outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1089 ctlcmds[1] = LBA;
1090 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1091 ctlcmds[3] = (GVSEL << 31) |
1092 ((PAN & 0x0000007f) << 24) |
1093 ((VOL & 0x000000ff) << 16) |
1094 ((CTRL & 0x0000000f) << 12) |
1095 (EC & 0x00000fff);
1096
1097 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1098
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001099 outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
1100 outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
1101 outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
1102 outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1105 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1106}
1107
1108static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1109{
1110 unsigned int delta;
1111
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001112 if (rate < 4000)
1113 rate = 4000;
1114 if (rate > 48000)
1115 rate = 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 if (rec) {
1118 if (rate == 44100)
1119 delta = 0x116a;
1120 else if (rate == 8000)
1121 delta = 0x6000;
1122 else if (rate == 48000)
1123 delta = 0x1000;
1124 else
1125 delta = ((48000 << 12) / rate) & 0x0000ffff;
1126 } else {
1127 if (rate == 44100)
1128 delta = 0xeb3;
1129 else if (rate == 8000)
1130 delta = 0x2ab;
1131 else if (rate == 48000)
1132 delta = 0x1000;
1133 else
1134 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1135 }
1136
1137 return delta;
1138}
1139
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001140static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 unsigned int CTRL;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001143 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 /* set ctrl mode
1146 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1147 */
1148 CTRL = 0x00000001;
1149 if (snd_pcm_format_width(runtime->format) == 16)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001150 CTRL |= 0x00000008; /* 16-bit data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (!snd_pcm_format_unsigned(runtime->format))
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001152 CTRL |= 0x00000002; /* signed data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (runtime->channels > 1)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001154 CTRL |= 0x00000004; /* stereo data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return CTRL;
1156}
1157
1158/*
1159 * PCM part
1160 */
1161
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001162static int snd_ali_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 int cmd)
1164
1165{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001166 struct snd_ali *codec = snd_pcm_substream_chip(substream);
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001167 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 unsigned int what, whati, capture_flag;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001169 struct snd_ali_voice *pvoice, *evoice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 unsigned int val;
1171 int do_start;
1172
1173 switch (cmd) {
1174 case SNDRV_PCM_TRIGGER_START:
1175 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001176 do_start = 1;
1177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 case SNDRV_PCM_TRIGGER_STOP:
1179 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001180 do_start = 0;
1181 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 default:
1183 return -EINVAL;
1184 }
1185
1186 what = whati = capture_flag = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +01001187 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001188 if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
1189 pvoice = s->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 evoice = pvoice->extra;
1191 what |= 1 << (pvoice->number & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001192 if (evoice == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 whati |= 1 << (pvoice->number & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001194 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 whati |= 1 << (evoice->number & 0x1f);
1196 what |= 1 << (evoice->number & 0x1f);
1197 }
1198 if (do_start) {
1199 pvoice->running = 1;
1200 if (evoice != NULL)
1201 evoice->running = 1;
1202 } else {
1203 pvoice->running = 0;
1204 if (evoice != NULL)
1205 evoice->running = 0;
1206 }
1207 snd_pcm_trigger_done(s, substream);
1208 if (pvoice->mode)
1209 capture_flag = 1;
1210 }
1211 }
1212 spin_lock(&codec->reg_lock);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001213 if (!do_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 outl(what, ALI_REG(codec, ALI_STOP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 val = inl(ALI_REG(codec, ALI_AINTEN));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001216 if (do_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 val |= whati;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001218 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 val &= ~whati;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 outl(val, ALI_REG(codec, ALI_AINTEN));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001221 if (do_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 outl(what, ALI_REG(codec, ALI_START));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001223 snd_ali_printk("trigger: what=%xh whati=%xh\n", what, whati);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 spin_unlock(&codec->reg_lock);
1225
1226 return 0;
1227}
1228
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001229static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001230 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001232 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1233 struct snd_pcm_runtime *runtime = substream->runtime;
1234 struct snd_ali_voice *pvoice = runtime->private_data;
1235 struct snd_ali_voice *evoice = pvoice->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 int err;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001237
1238 err = snd_pcm_lib_malloc_pages(substream,
1239 params_buffer_bytes(hw_params));
1240 if (err < 0)
1241 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /* voice management */
1244
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001245 if (params_buffer_size(hw_params) / 2 !=
1246 params_period_size(hw_params)) {
1247 if (!evoice) {
1248 evoice = snd_ali_alloc_voice(codec,
1249 SNDRV_ALI_VOICE_TYPE_PCM,
1250 0, -1);
1251 if (!evoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return -ENOMEM;
1253 pvoice->extra = evoice;
1254 evoice->substream = substream;
1255 }
1256 } else {
Takashi Iwai27043642007-05-19 16:30:35 +02001257 if (evoice) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 snd_ali_free_voice(codec, evoice);
1259 pvoice->extra = evoice = NULL;
1260 }
1261 }
1262
1263 return 0;
1264}
1265
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001266static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001268 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1269 struct snd_pcm_runtime *runtime = substream->runtime;
1270 struct snd_ali_voice *pvoice = runtime->private_data;
1271 struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 snd_pcm_lib_free_pages(substream);
Takashi Iwai27043642007-05-19 16:30:35 +02001274 if (evoice) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 snd_ali_free_voice(codec, evoice);
1276 pvoice->extra = NULL;
1277 }
1278 return 0;
1279}
1280
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001281static int snd_ali_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001282 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001284 return snd_pcm_lib_malloc_pages(substream,
1285 params_buffer_bytes(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001288static int snd_ali_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 return snd_pcm_lib_free_pages(substream);
1291}
1292
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001293static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001295 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1296 struct snd_pcm_runtime *runtime = substream->runtime;
1297 struct snd_ali_voice *pvoice = runtime->private_data;
1298 struct snd_ali_voice *evoice = pvoice->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 unsigned int LBA;
1301 unsigned int Delta;
1302 unsigned int ESO;
1303 unsigned int CTRL;
1304 unsigned int GVSEL;
1305 unsigned int PAN;
1306 unsigned int VOL;
1307 unsigned int EC;
1308
1309 snd_ali_printk("playback_prepare ...\n");
1310
Takashi Iwai755e1372005-11-11 21:05:27 +01001311 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 /* set Delta (rate) value */
1314 Delta = snd_ali_convert_rate(runtime->rate, 0);
1315
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001316 if (pvoice->number == ALI_SPDIF_IN_CHANNEL ||
1317 pvoice->number == ALI_PCM_IN_CHANNEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 snd_ali_disable_special_channel(codec, pvoice->number);
1319 else if (codec->spdif_support &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001320 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
1321 ALI_SPDIF_OUT_CH_ENABLE)
1322 && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1324 Delta = 0x1000;
1325 }
1326
1327 /* set Loop Back Address */
1328 LBA = runtime->dma_addr;
1329
1330 /* set interrupt count size */
1331 pvoice->count = runtime->period_size;
1332
1333 /* set target ESO for channel */
1334 pvoice->eso = runtime->buffer_size;
1335
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001336 snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",
1337 pvoice->eso, pvoice->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 /* set ESO to capture first MIDLP interrupt */
1340 ESO = pvoice->eso -1;
1341 /* set ctrl mode */
1342 CTRL = snd_ali_control_mode(substream);
1343
1344 GVSEL = 1;
1345 PAN = 0;
1346 VOL = 0;
1347 EC = 0;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001348 snd_ali_printk("playback_prepare:\n");
1349 snd_ali_printk("ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
1350 pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1351 snd_ali_write_voice_regs(codec,
1352 pvoice->number,
1353 LBA,
1354 0, /* cso */
1355 ESO,
1356 Delta,
1357 0, /* alpha */
1358 GVSEL,
1359 PAN,
1360 VOL,
1361 CTRL,
1362 EC);
Takashi Iwai27043642007-05-19 16:30:35 +02001363 if (evoice) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 evoice->count = pvoice->count;
1365 evoice->eso = pvoice->count << 1;
1366 ESO = evoice->eso - 1;
1367 snd_ali_write_voice_regs(codec,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001368 evoice->number,
1369 LBA,
1370 0, /* cso */
1371 ESO,
1372 Delta,
1373 0, /* alpha */
1374 GVSEL,
1375 0x7f,
1376 0x3ff,
1377 CTRL,
1378 EC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001380 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return 0;
1382}
1383
1384
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001385static int snd_ali_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001387 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1388 struct snd_pcm_runtime *runtime = substream->runtime;
1389 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 unsigned int LBA;
1391 unsigned int Delta;
1392 unsigned int ESO;
1393 unsigned int CTRL;
1394 unsigned int GVSEL;
1395 unsigned int PAN;
1396 unsigned int VOL;
1397 unsigned int EC;
1398 u8 bValue;
1399
Takashi Iwai755e1372005-11-11 21:05:27 +01001400 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001402 snd_ali_printk("ali_prepare...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 snd_ali_enable_special_channel(codec,pvoice->number);
1405
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001406 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1407 pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
1408 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001410 /* Prepare capture intr channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1412
1413 unsigned int rate;
1414
Takashi Iwai755e1372005-11-11 21:05:27 +01001415 spin_unlock_irq(&codec->reg_lock);
1416 if (codec->revision != ALI_5451_V02)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return -1;
Takashi Iwai755e1372005-11-11 21:05:27 +01001418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 rate = snd_ali_get_spdif_in_rate(codec);
1420 if (rate == 0) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001421 snd_printk(KERN_WARNING "ali_capture_preapre: "
1422 "spdif rate detect err!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 rate = 48000;
1424 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001425 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1427 if (bValue & 0x10) {
1428 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
Takashi Iwai99b359b2005-10-20 18:26:44 +02001429 printk(KERN_WARNING "clear SPDIF parity error flag.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431
1432 if (rate != 48000)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001433 Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001436 /* set target ESO for channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 pvoice->eso = runtime->buffer_size;
1438
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001439 /* set interrupt count size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 pvoice->count = runtime->period_size;
1441
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001442 /* set Loop Back Address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 LBA = runtime->dma_addr;
1444
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001445 /* set ESO to capture first MIDLP interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 ESO = pvoice->eso - 1;
1447 CTRL = snd_ali_control_mode(substream);
1448 GVSEL = 0;
1449 PAN = 0x00;
1450 VOL = 0x00;
1451 EC = 0;
1452
1453 snd_ali_write_voice_regs( codec,
1454 pvoice->number,
1455 LBA,
1456 0, /* cso */
1457 ESO,
1458 Delta,
1459 0, /* alpha */
1460 GVSEL,
1461 PAN,
1462 VOL,
1463 CTRL,
1464 EC);
1465
Takashi Iwai755e1372005-11-11 21:05:27 +01001466 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 return 0;
1469}
1470
1471
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001472static snd_pcm_uframes_t
1473snd_ali_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001475 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1476 struct snd_pcm_runtime *runtime = substream->runtime;
1477 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 unsigned int cso;
1479
1480 spin_lock(&codec->reg_lock);
1481 if (!pvoice->running) {
1482 spin_unlock(&codec->reg_lock);
1483 return 0;
1484 }
1485 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1486 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1487 spin_unlock(&codec->reg_lock);
1488 snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1489
1490 return cso;
1491}
1492
1493
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001494static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001496 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1497 struct snd_pcm_runtime *runtime = substream->runtime;
1498 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 unsigned int cso;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Takashi Iwai755e1372005-11-11 21:05:27 +01001501 spin_lock(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (!pvoice->running) {
Takashi Iwai755e1372005-11-11 21:05:27 +01001503 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return 0;
1505 }
1506 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1507 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
Takashi Iwai755e1372005-11-11 21:05:27 +01001508 spin_unlock(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 return cso;
1511}
1512
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001513static struct snd_pcm_hardware snd_ali_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001515 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1516 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1517 SNDRV_PCM_INFO_MMAP_VALID |
1518 SNDRV_PCM_INFO_RESUME |
1519 SNDRV_PCM_INFO_SYNC_START),
1520 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1521 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1522 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 .rate_min = 4000,
1524 .rate_max = 48000,
1525 .channels_min = 1,
1526 .channels_max = 2,
1527 .buffer_bytes_max = (256*1024),
1528 .period_bytes_min = 64,
1529 .period_bytes_max = (256*1024),
1530 .periods_min = 1,
1531 .periods_max = 1024,
1532 .fifo_size = 0,
1533};
1534
1535/*
1536 * Capture support device description
1537 */
1538
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001539static struct snd_pcm_hardware snd_ali_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001541 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1542 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1543 SNDRV_PCM_INFO_MMAP_VALID |
1544 SNDRV_PCM_INFO_RESUME |
1545 SNDRV_PCM_INFO_SYNC_START),
1546 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1547 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1548 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 .rate_min = 4000,
1550 .rate_max = 48000,
1551 .channels_min = 1,
1552 .channels_max = 2,
1553 .buffer_bytes_max = (128*1024),
1554 .period_bytes_min = 64,
1555 .period_bytes_max = (128*1024),
1556 .periods_min = 1,
1557 .periods_max = 1024,
1558 .fifo_size = 0,
1559};
1560
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001561static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001563 struct snd_ali_voice *pvoice = runtime->private_data;
1564 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 if (pvoice) {
1567 codec = pvoice->codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 snd_ali_free_voice(pvoice->codec, pvoice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
1570}
1571
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001572static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
1573 int channel, struct snd_pcm_hardware *phw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001575 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1576 struct snd_pcm_runtime *runtime = substream->runtime;
1577 struct snd_ali_voice *pvoice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001579 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
1580 channel);
1581 if (!pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 pvoice->substream = substream;
1585 runtime->private_data = pvoice;
1586 runtime->private_free = snd_ali_pcm_free_substream;
1587
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001588 runtime->hw = *phw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 snd_pcm_set_sync(substream);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001590 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1591 0, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return 0;
1593}
1594
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001595static int snd_ali_playback_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001596{
1597 return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1598}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001600static int snd_ali_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001602 return snd_ali_open(substream, 1, -1, &snd_ali_capture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001605static int snd_ali_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607 return 0;
1608}
1609
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001610static int snd_ali_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001612 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1613 struct snd_ali_voice *pvoice = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 snd_ali_disable_special_channel(codec,pvoice->number);
1616
1617 return 0;
1618}
1619
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001620static struct snd_pcm_ops snd_ali_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 .open = snd_ali_playback_open,
1622 .close = snd_ali_playback_close,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001623 .ioctl = snd_pcm_lib_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 .hw_params = snd_ali_playback_hw_params,
1625 .hw_free = snd_ali_playback_hw_free,
1626 .prepare = snd_ali_playback_prepare,
1627 .trigger = snd_ali_trigger,
1628 .pointer = snd_ali_playback_pointer,
1629};
1630
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001631static struct snd_pcm_ops snd_ali_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 .open = snd_ali_capture_open,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001633 .close = snd_ali_close,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001634 .ioctl = snd_pcm_lib_ioctl,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001635 .hw_params = snd_ali_hw_params,
1636 .hw_free = snd_ali_hw_free,
1637 .prepare = snd_ali_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 .trigger = snd_ali_trigger,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001639 .pointer = snd_ali_pointer,
1640};
1641
1642/*
1643 * Modem PCM
1644 */
1645
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001646static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
1647 struct snd_pcm_hw_params *hw_params)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001648{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001649 struct snd_ali *chip = snd_pcm_substream_chip(substream);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001650 unsigned int modem_num = chip->num_of_codecs - 1;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001651 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
1652 params_rate(hw_params));
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001653 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1654 return snd_ali_hw_params(substream, hw_params);
1655}
1656
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001657static struct snd_pcm_hardware snd_ali_modem =
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001658{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001659 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1660 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1661 SNDRV_PCM_INFO_MMAP_VALID |
1662 SNDRV_PCM_INFO_RESUME |
1663 SNDRV_PCM_INFO_SYNC_START),
1664 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1665 .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
1666 SNDRV_PCM_RATE_16000),
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001667 .rate_min = 8000,
1668 .rate_max = 16000,
1669 .channels_min = 1,
1670 .channels_max = 1,
1671 .buffer_bytes_max = (256*1024),
1672 .period_bytes_min = 64,
1673 .period_bytes_max = (256*1024),
1674 .periods_min = 1,
1675 .periods_max = 1024,
1676 .fifo_size = 0,
1677};
1678
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001679static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
1680 int channel)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001681{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001682 static unsigned int rates[] = {8000, 9600, 12000, 16000};
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001683 static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001684 .count = ARRAY_SIZE(rates),
1685 .list = rates,
1686 .mask = 0,
1687 };
1688 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001689
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001690 if (err)
1691 return err;
1692 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1693 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1694}
1695
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001696static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001697{
1698 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1699}
1700
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001701static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001702{
1703 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1704}
1705
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001706static struct snd_pcm_ops snd_ali_modem_playback_ops = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001707 .open = snd_ali_modem_playback_open,
1708 .close = snd_ali_close,
1709 .ioctl = snd_pcm_lib_ioctl,
1710 .hw_params = snd_ali_modem_hw_params,
1711 .hw_free = snd_ali_hw_free,
1712 .prepare = snd_ali_prepare,
1713 .trigger = snd_ali_trigger,
1714 .pointer = snd_ali_pointer,
1715};
1716
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001717static struct snd_pcm_ops snd_ali_modem_capture_ops = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001718 .open = snd_ali_modem_capture_open,
1719 .close = snd_ali_close,
1720 .ioctl = snd_pcm_lib_ioctl,
1721 .hw_params = snd_ali_modem_hw_params,
1722 .hw_free = snd_ali_hw_free,
1723 .prepare = snd_ali_prepare,
1724 .trigger = snd_ali_trigger,
1725 .pointer = snd_ali_pointer,
1726};
1727
1728
1729struct ali_pcm_description {
1730 char *name;
1731 unsigned int playback_num;
1732 unsigned int capture_num;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001733 struct snd_pcm_ops *playback_ops;
1734 struct snd_pcm_ops *capture_ops;
Sasha Khapyorsky6632d192005-09-29 11:48:17 +02001735 unsigned short class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736};
1737
1738
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001739static void snd_ali_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001741 struct snd_ali *codec = pcm->private_data;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001742 codec->pcm[pcm->device] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001745
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001746static int __devinit snd_ali_pcm(struct snd_ali * codec, int device,
1747 struct ali_pcm_description *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001749 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 int err;
1751
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001752 err = snd_pcm_new(codec->card, desc->name, device,
1753 desc->playback_num, desc->capture_num, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (err < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001755 snd_printk(KERN_ERR "snd_ali_pcm: err called snd_pcm_new.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return err;
1757 }
1758 pcm->private_data = codec;
1759 pcm->private_free = snd_ali_pcm_free;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001760 if (desc->playback_ops)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001761 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1762 desc->playback_ops);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001763 if (desc->capture_ops)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001764 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1765 desc->capture_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001768 snd_dma_pci_data(codec->pci),
1769 64*1024, 128*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 pcm->info_flags = 0;
Sasha Khapyorsky6632d192005-09-29 11:48:17 +02001772 pcm->dev_class = desc->class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001774 strcpy(pcm->name, desc->name);
1775 codec->pcm[0] = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return 0;
1777}
1778
Clemens Ladischa53fc182005-08-11 15:59:17 +02001779static struct ali_pcm_description ali_pcms[] = {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001780 { .name = "ALI 5451",
1781 .playback_num = ALI_CHANNELS,
1782 .capture_num = 1,
1783 .playback_ops = &snd_ali_playback_ops,
1784 .capture_ops = &snd_ali_capture_ops
1785 },
1786 { .name = "ALI 5451 modem",
1787 .playback_num = 1,
1788 .capture_num = 1,
1789 .playback_ops = &snd_ali_modem_playback_ops,
1790 .capture_ops = &snd_ali_modem_capture_ops,
1791 .class = SNDRV_PCM_CLASS_MODEM
1792 }
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001793};
1794
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001795static int __devinit snd_ali_build_pcms(struct snd_ali *codec)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001796{
1797 int i, err;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001798 for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
1799 err = snd_ali_pcm(codec, i, &ali_pcms[i]);
1800 if (err < 0)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001801 return err;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001802 }
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001803 return 0;
1804}
1805
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807#define ALI5451_SPDIF(xname, xindex, value) \
1808{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1809.info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1810.put = snd_ali5451_spdif_put, .private_value = value}
1811
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001812#define snd_ali5451_spdif_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001814static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
1815 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001817 struct snd_ali *codec = kcontrol->private_data;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001818 unsigned int spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001820 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Takashi Iwai755e1372005-11-11 21:05:27 +01001822 spin_lock_irq(&codec->reg_lock);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001823 switch (kcontrol->private_value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 case 0:
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001825 spdif_enable = (codec->spdif_mask & 0x02) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 break;
1827 case 1:
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001828 spdif_enable = ((codec->spdif_mask & 0x02) &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001829 (codec->spdif_mask & 0x04)) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 break;
1831 case 2:
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001832 spdif_enable = (codec->spdif_mask & 0x01) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 break;
1834 default:
1835 break;
1836 }
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001837 ucontrol->value.integer.value[0] = spdif_enable;
Takashi Iwai755e1372005-11-11 21:05:27 +01001838 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return 0;
1840}
1841
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001842static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
1843 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001845 struct snd_ali *codec = kcontrol->private_data;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001846 unsigned int change = 0, spdif_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001848 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Takashi Iwai755e1372005-11-11 21:05:27 +01001850 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 switch (kcontrol->private_value) {
1852 case 0:
1853 change = (codec->spdif_mask & 0x02) ? 1 : 0;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001854 change = change ^ spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (change) {
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001856 if (spdif_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 codec->spdif_mask |= 0x02;
1858 snd_ali_enable_spdif_out(codec);
1859 } else {
1860 codec->spdif_mask &= ~(0x02);
1861 codec->spdif_mask &= ~(0x04);
1862 snd_ali_disable_spdif_out(codec);
1863 }
1864 }
1865 break;
1866 case 1:
1867 change = (codec->spdif_mask & 0x04) ? 1 : 0;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001868 change = change ^ spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (change && (codec->spdif_mask & 0x02)) {
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001870 if (spdif_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 codec->spdif_mask |= 0x04;
1872 snd_ali_enable_spdif_chnout(codec);
1873 } else {
1874 codec->spdif_mask &= ~(0x04);
1875 snd_ali_disable_spdif_chnout(codec);
1876 }
1877 }
1878 break;
1879 case 2:
1880 change = (codec->spdif_mask & 0x01) ? 1 : 0;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001881 change = change ^ spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (change) {
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001883 if (spdif_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 codec->spdif_mask |= 0x01;
1885 snd_ali_enable_spdif_in(codec);
1886 } else {
1887 codec->spdif_mask &= ~(0x01);
1888 snd_ali_disable_spdif_in(codec);
1889 }
1890 }
1891 break;
1892 default:
1893 break;
1894 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001895 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 return change;
1898}
1899
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001900static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /* spdif aplayback switch */
1902 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001903 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 /* spdif out to spdif channel */
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001905 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 /* spdif in from spdif channel */
1907 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1908};
1909
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001910static int __devinit snd_ali_mixer(struct snd_ali * codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001912 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 unsigned int idx;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001914 int i, err;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001915 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 .write = snd_ali_codec_write,
1917 .read = snd_ali_codec_read,
1918 };
1919
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001920 err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
1921 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 memset(&ac97, 0, sizeof(ac97));
1925 ac97.private_data = codec;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001926
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001927 for (i = 0; i < codec->num_of_codecs; i++) {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001928 ac97.num = i;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001929 err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
1930 if (err < 0) {
1931 snd_printk(KERN_ERR
1932 "ali mixer %d creating error.\n", i);
1933 if (i == 0)
Takashi Iwai4d060fd2005-10-04 13:50:44 +02001934 return err;
1935 codec->num_of_codecs = 1;
1936 break;
1937 }
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001938 }
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (codec->spdif_support) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001941 for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1942 err = snd_ctl_add(codec->card,
1943 snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1944 if (err < 0)
1945 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947 }
1948 return 0;
1949}
1950
1951#ifdef CONFIG_PM
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001952static int ali_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001954 struct snd_card *card = pci_get_drvdata(pci);
1955 struct snd_ali *chip = card->private_data;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001956 struct snd_ali_image *im;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 int i, j;
1958
1959 im = chip->image;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001960 if (!im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return 0;
1962
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001963 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001964 for (i = 0; i < chip->num_of_codecs; i++) {
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001965 snd_pcm_suspend_all(chip->pcm[i]);
1966 snd_ac97_suspend(chip->ac97[i]);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 spin_lock_irq(&chip->reg_lock);
1970
1971 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001972 /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1974
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001975 /* disable all IRQ bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 outl(0, ALI_REG(chip, ALI_MISCINT));
1977
1978 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1979 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1980 continue;
1981 im->regs[i] = inl(ALI_REG(chip, i*4));
1982 }
1983
1984 for (i = 0; i < ALI_CHANNELS; i++) {
1985 outb(i, ALI_REG(chip, ALI_GC_CIR));
1986 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1987 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1988 }
1989
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001990 /* stop all HW channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1992
1993 spin_unlock_irq(&chip->reg_lock);
Takashi Iwai30b35392006-10-11 18:52:53 +02001994
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001995 pci_disable_device(pci);
1996 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02001997 pci_set_power_state(pci, pci_choose_state(pci, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return 0;
1999}
2000
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002001static int ali_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002003 struct snd_card *card = pci_get_drvdata(pci);
2004 struct snd_ali *chip = card->private_data;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002005 struct snd_ali_image *im;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 int i, j;
2007
2008 im = chip->image;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002009 if (!im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return 0;
2011
Takashi Iwai30b35392006-10-11 18:52:53 +02002012 pci_set_power_state(pci, PCI_D0);
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002013 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002014 if (pci_enable_device(pci) < 0) {
2015 printk(KERN_ERR "ali5451: pci_enable_device failed, "
2016 "disabling device\n");
2017 snd_card_disconnect(card);
2018 return -EIO;
2019 }
2020 pci_set_master(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 spin_lock_irq(&chip->reg_lock);
2023
2024 for (i = 0; i < ALI_CHANNELS; i++) {
2025 outb(i, ALI_REG(chip, ALI_GC_CIR));
2026 for (j = 0; j < ALI_CHANNEL_REGS; j++)
2027 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
2028 }
2029
2030 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002031 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
2032 (i*4 == ALI_START))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 continue;
2034 outl(im->regs[i], ALI_REG(chip, i*4));
2035 }
2036
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002037 /* start HW channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002039 /* restore IRQ enable bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
2041
2042 spin_unlock_irq(&chip->reg_lock);
2043
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002044 for (i = 0 ; i < chip->num_of_codecs; i++)
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002045 snd_ac97_resume(chip->ac97[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002047 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return 0;
2049}
2050#endif /* CONFIG_PM */
2051
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002052static int snd_ali_free(struct snd_ali * codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 if (codec->hw_initialized)
2055 snd_ali_disable_address_interrupt(codec);
Jeff Garzikf000fd82008-04-22 13:50:34 +02002056 if (codec->irq >= 0)
Takashi Iwai437a5a42006-11-21 12:14:23 +01002057 free_irq(codec->irq, codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if (codec->port)
2059 pci_release_regions(codec->pci);
2060 pci_disable_device(codec->pci);
2061#ifdef CONFIG_PM
2062 kfree(codec->image);
2063#endif
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002064 pci_dev_put(codec->pci_m1533);
2065 pci_dev_put(codec->pci_m7101);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 kfree(codec);
2067 return 0;
2068}
2069
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002070static int snd_ali_chip_init(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
2072 unsigned int legacy;
2073 unsigned char temp;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002074 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 snd_ali_printk("chip initializing ... \n");
2077
2078 if (snd_ali_reset_5451(codec)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002079 snd_printk(KERN_ERR "ali_chip_init: reset 5451 error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return -1;
2081 }
2082
2083 if (codec->revision == ALI_5451_V02) {
2084 pci_dev = codec->pci_m1533;
2085 pci_read_config_byte(pci_dev, 0x59, &temp);
2086 temp |= 0x80;
2087 pci_write_config_byte(pci_dev, 0x59, temp);
2088
2089 pci_dev = codec->pci_m7101;
2090 pci_read_config_byte(pci_dev, 0xb8, &temp);
2091 temp |= 0x20;
2092 pci_write_config_byte(pci_dev, 0xB8, temp);
2093 }
2094
2095 pci_read_config_dword(codec->pci, 0x44, &legacy);
2096 legacy &= 0xff00ff00;
2097 legacy |= 0x000800aa;
2098 pci_write_config_dword(codec->pci, 0x44, legacy);
2099
2100 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2101 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2102 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2103 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2104 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2105
2106 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002107 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
2108 AC97_EXTENDED_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (codec->spdif_support) {
2110 snd_ali_enable_spdif_out(codec);
2111 codec->spdif_mask = 0x00000002;
2112 }
2113
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002114 codec->num_of_codecs = 1;
2115
2116 /* secondary codec - modem */
2117 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
2118 codec->num_of_codecs++;
2119 outl(inl(ALI_REG(codec, ALI_SCTRL)) |
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002120 (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
2121 ALI_SCTRL_LINE_OUT_EN),
2122 ALI_REG(codec, ALI_SCTRL));
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002123 }
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 snd_ali_printk("chip initialize succeed.\n");
2126 return 0;
2127
2128}
2129
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002130/* proc for register dump */
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002131static void snd_ali_proc_read(struct snd_info_entry *entry,
2132 struct snd_info_buffer *buf)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002133{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002134 struct snd_ali *codec = entry->private_data;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002135 int i;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002136 for (i = 0; i < 256 ; i+= 4)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002137 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
2138}
2139
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002140static void __devinit snd_ali_proc_init(struct snd_ali *codec)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002141{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002142 struct snd_info_entry *entry;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002143 if (!snd_card_proc_new(codec->card, "ali5451", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002144 snd_info_set_text_ops(entry, codec, snd_ali_proc_read);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002145}
2146
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002147static int __devinit snd_ali_resources(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
2149 int err;
2150
Wolfram Sangc468ac22009-03-20 10:08:11 +01002151 snd_ali_printk("resources allocation ...\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002152 err = pci_request_regions(codec->pci, "ALI 5451");
2153 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return err;
2155 codec->port = pci_resource_start(codec->pci, 0);
2156
Takashi Iwai437a5a42006-11-21 12:14:23 +01002157 if (request_irq(codec->pci->irq, snd_ali_card_interrupt,
2158 IRQF_SHARED, "ALI 5451", codec)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002159 snd_printk(KERN_ERR "Unable to request irq.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 return -EBUSY;
2161 }
2162 codec->irq = codec->pci->irq;
Wolfram Sangc468ac22009-03-20 10:08:11 +01002163 snd_ali_printk("resources allocated.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return 0;
2165}
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002166static int snd_ali_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002168 struct snd_ali *codec = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 snd_ali_free(codec);
2170 return 0;
2171}
2172
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002173static int __devinit snd_ali_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 struct pci_dev *pci,
2175 int pcm_streams,
2176 int spdif_support,
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002177 struct snd_ali ** r_ali)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002179 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 int i, err;
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002181 unsigned short cmdw;
2182 static struct snd_device_ops ops = {
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002183 .dev_free = snd_ali_dev_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 };
2185
2186 *r_ali = NULL;
2187
2188 snd_ali_printk("creating ...\n");
2189
2190 /* enable PCI device */
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002191 err = pci_enable_device(pci);
2192 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return err;
2194 /* check, if we can restrict PCI DMA transfers to 31 bits */
Yang Hongyang929a22a2009-04-06 19:01:16 -07002195 if (pci_set_dma_mask(pci, DMA_BIT_MASK(31)) < 0 ||
2196 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(31)) < 0) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002197 snd_printk(KERN_ERR "architecture does not support "
2198 "31bit PCI busmaster DMA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 pci_disable_device(pci);
2200 return -ENXIO;
2201 }
2202
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002203 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
2204 if (!codec) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 pci_disable_device(pci);
2206 return -ENOMEM;
2207 }
2208
2209 spin_lock_init(&codec->reg_lock);
2210 spin_lock_init(&codec->voice_alloc);
2211
2212 codec->card = card;
2213 codec->pci = pci;
2214 codec->irq = -1;
Auke Kok44c10132007-06-08 15:46:36 -07002215 codec->revision = pci->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 codec->spdif_support = spdif_support;
2217
2218 if (pcm_streams < 1)
2219 pcm_streams = 1;
2220 if (pcm_streams > 32)
2221 pcm_streams = 32;
2222
2223 pci_set_master(pci);
2224 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2225 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2226 cmdw |= PCI_COMMAND_IO;
2227 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2228 }
2229 pci_set_master(pci);
2230
2231 if (snd_ali_resources(codec)) {
2232 snd_ali_free(codec);
2233 return -EBUSY;
2234 }
2235
2236 synchronize_irq(pci->irq);
2237
2238 codec->synth.chmap = 0;
2239 codec->synth.chcnt = 0;
2240 codec->spdif_mask = 0;
2241 codec->synth.synthcount = 0;
2242
2243 if (codec->revision == ALI_5451_V02)
2244 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2245 else
2246 codec->chregs.regs.ac97read = ALI_AC97_READ;
2247 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2248
2249 codec->chregs.regs.start = ALI_START;
2250 codec->chregs.regs.stop = ALI_STOP;
2251 codec->chregs.regs.aint = ALI_AINT;
2252 codec->chregs.regs.ainten = ALI_AINTEN;
2253
2254 codec->chregs.data.start = 0x00;
2255 codec->chregs.data.stop = 0x00;
2256 codec->chregs.data.aint = 0x00;
2257 codec->chregs.data.ainten = 0x00;
2258
2259 /* M1533: southbridge */
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002260 codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002261 if (!codec->pci_m1533) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2263 snd_ali_free(codec);
2264 return -ENODEV;
2265 }
2266 /* M7101: power management */
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002267 codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002268 if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2270 snd_ali_free(codec);
2271 return -ENODEV;
2272 }
2273
2274 snd_ali_printk("snd_device_new is called.\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002275 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops);
2276 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 snd_ali_free(codec);
2278 return err;
2279 }
2280
Takashi Iwaic187c042007-02-19 15:27:33 +01002281 snd_card_set_dev(card, &pci->dev);
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 /* initialise synth voices*/
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002284 for (i = 0; i < ALI_CHANNELS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 codec->synth.voices[i].number = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002287 err = snd_ali_chip_init(codec);
2288 if (err < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002289 snd_printk(KERN_ERR "ali create: chip init error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return err;
2291 }
2292
2293#ifdef CONFIG_PM
2294 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002295 if (!codec->image)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 snd_printk(KERN_WARNING "can't allocate apm buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297#endif
2298
2299 snd_ali_enable_address_interrupt(codec);
2300 codec->hw_initialized = 1;
2301
2302 *r_ali = codec;
2303 snd_ali_printk("created.\n");
2304 return 0;
2305}
2306
2307static int __devinit snd_ali_probe(struct pci_dev *pci,
2308 const struct pci_device_id *pci_id)
2309{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002310 struct snd_card *card;
2311 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 int err;
2313
2314 snd_ali_printk("probe ...\n");
2315
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002316 err = snd_card_create(index, id, THIS_MODULE, 0, &card);
2317 if (err < 0)
2318 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002320 err = snd_ali_create(card, pci, pcm_channels, spdif, &codec);
2321 if (err < 0)
2322 goto error;
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002323 card->private_data = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 snd_ali_printk("mixer building ...\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002326 err = snd_ali_mixer(codec);
2327 if (err < 0)
2328 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 snd_ali_printk("pcm building ...\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002331 err = snd_ali_build_pcms(codec);
2332 if (err < 0)
2333 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002335 snd_ali_proc_init(codec);
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 strcpy(card->driver, "ALI5451");
2338 strcpy(card->shortname, "ALI 5451");
2339
Takashi Iwaiba8c3c32007-05-30 12:42:31 +02002340 sprintf(card->longname, "%s at 0x%lx, irq %i",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 card->shortname, codec->port, codec->irq);
2342
2343 snd_ali_printk("register card.\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002344 err = snd_card_register(card);
2345 if (err < 0)
2346 goto error;
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 pci_set_drvdata(pci, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return 0;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002350
2351 error:
2352 snd_card_free(card);
2353 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354}
2355
2356static void __devexit snd_ali_remove(struct pci_dev *pci)
2357{
2358 snd_card_free(pci_get_drvdata(pci));
2359 pci_set_drvdata(pci, NULL);
2360}
2361
2362static struct pci_driver driver = {
2363 .name = "ALI 5451",
2364 .id_table = snd_ali_ids,
2365 .probe = snd_ali_probe,
2366 .remove = __devexit_p(snd_ali_remove),
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002367#ifdef CONFIG_PM
2368 .suspend = ali_suspend,
2369 .resume = ali_resume,
2370#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371};
2372
2373static int __init alsa_card_ali_init(void)
2374{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002375 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
2377
2378static void __exit alsa_card_ali_exit(void)
2379{
2380 pci_unregister_driver(&driver);
2381}
2382
2383module_init(alsa_card_ali_init)
2384module_exit(alsa_card_ali_exit)