blob: 115b1120319ab15cd74d2f6888584f604229bbd2 [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>
Paul Gortmaker65a77212011-07-15 13:13:37 -040034#include <linux/module.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;
Rusty Russella67ff6a2011-12-15 13:49:36 +103051static bool 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 */
Rusty Russella67ff6a2011-12-15 13:49:36 +103063static bool enable;
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020064module_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
Takashi Iwaic7561cd2012-08-14 18:12:04 +0200273#ifdef CONFIG_PM_SLEEP
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100274 struct snd_ali_image *image;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#endif
276};
277
Alexey Dobriyancebe41d2010-02-06 00:21:03 +0200278static DEFINE_PCI_DEVICE_TABLE(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);
Arnd Bergmann76039352013-04-25 19:28:49 +0200454 mdelay(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
456 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
Arnd Bergmann76039352013-04-25 19:28:49 +0200457 mdelay(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
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);
Arnd Bergmann76039352013-04-25 19:28:49 +0200466 mdelay(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
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;
Arnd Bergmann76039352013-04-25 19:28:49 +0200473 mdelay(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/*
482 * ALI 5451 Controller
483 */
484
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200485static void snd_ali_enable_special_channel(struct snd_ali *codec,
486 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200488 unsigned long dwVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200490 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 dwVal |= 1 << (channel & 0x0000001f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200492 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200495static void snd_ali_disable_special_channel(struct snd_ali *codec,
496 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200498 unsigned long dwVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200500 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 dwVal &= ~(1 << (channel & 0x0000001f));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200502 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200505static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 unsigned int gc;
508
509 gc = inl(ALI_REG(codec, ALI_GC_CIR));
510 gc |= ENDLP_IE;
511 gc |= MIDLP_IE;
512 outl( gc, ALI_REG(codec, ALI_GC_CIR));
513}
514
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200515static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 unsigned int gc;
518
519 gc = inl(ALI_REG(codec, ALI_GC_CIR));
520 gc &= ~ENDLP_IE;
521 gc &= ~MIDLP_IE;
522 outl(gc, ALI_REG(codec, ALI_GC_CIR));
523}
524
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200525static void snd_ali_disable_voice_irq(struct snd_ali *codec,
526 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 unsigned int mask;
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100529 struct snd_ali_channel_control *pchregs = &(codec->chregs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 snd_ali_printk("disable_voice_irq channel=%d\n",channel);
532
533 mask = 1 << (channel & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200534 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 pchregs->data.ainten &= ~mask;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200536 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100539static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 unsigned int idx = channel & 0x1f;
542
543 if (codec->synth.chcnt >= ALI_CHANNELS){
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200544 snd_printk(KERN_ERR
545 "ali_alloc_pcm_channel: no free channels.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return -1;
547 }
548
549 if (!(codec->synth.chmap & (1 << idx))) {
550 codec->synth.chmap |= 1 << idx;
551 codec->synth.chcnt++;
552 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
553 return idx;
554 }
555 return -1;
556}
557
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100558static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 int idx;
561 int result = -1;
562
563 snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
564
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200565 /* recording */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (rec) {
567 if (codec->spdif_support &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200568 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
569 ALI_SPDIF_IN_SUPPORT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 idx = ALI_SPDIF_IN_CHANNEL;
571 else
572 idx = ALI_PCM_IN_CHANNEL;
573
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200574 result = snd_ali_alloc_pcm_channel(codec, idx);
575 if (result >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return result;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200577 else {
578 snd_printk(KERN_ERR "ali_find_free_channel: "
579 "record channel is busy now.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return -1;
581 }
582 }
583
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200584 /* playback... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (codec->spdif_support &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200586 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
587 ALI_SPDIF_OUT_CH_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 idx = ALI_SPDIF_OUT_CHANNEL;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200589 result = snd_ali_alloc_pcm_channel(codec, idx);
590 if (result >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return result;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200592 else
593 snd_printk(KERN_ERR "ali_find_free_channel: "
594 "S/PDIF out channel is in busy now.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
597 for (idx = 0; idx < ALI_CHANNELS; idx++) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200598 result = snd_ali_alloc_pcm_channel(codec, idx);
599 if (result >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return result;
601 }
Takashi Iwai99b359b2005-10-20 18:26:44 +0200602 snd_printk(KERN_ERR "ali_find_free_channel: no free channels.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return -1;
604}
605
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100606static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 unsigned int idx = channel & 0x0000001f;
609
610 snd_ali_printk("free_channel_pcm channel=%d\n",channel);
611
612 if (channel < 0 || channel >= ALI_CHANNELS)
613 return;
614
615 if (!(codec->synth.chmap & (1 << idx))) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200616 snd_printk(KERN_ERR "ali_free_channel_pcm: "
617 "channel %d is not in use.\n", channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return;
619 } else {
620 codec->synth.chmap &= ~(1 << idx);
621 codec->synth.chcnt--;
622 }
623}
624
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200625static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 unsigned int mask = 1 << (channel & 0x1f);
628
629 snd_ali_printk("stop_voice: channel=%d\n",channel);
630 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
631}
632
633/*
634 * S/PDIF Part
635 */
636
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100637static void snd_ali_delay(struct snd_ali *codec,int interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 unsigned long begintimer,currenttimer;
640
641 begintimer = inl(ALI_REG(codec, ALI_STIMER));
642 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
643
644 while (currenttimer < begintimer + interval) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200645 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 break;
647 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200648 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650}
651
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100652static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200654 u16 wval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 u16 count = 0;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200656 u8 bval, R1 = 0, R2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200658 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 bval |= 0x1F;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200660 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200662 while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 count ++;
664 snd_ali_delay(codec, 6);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200665 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 R1 = bval & 0x1F;
667 }
668
669 if (count > 50000) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200670 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return;
672 }
673
Andrew Morton1c397322007-06-11 12:23:31 +0200674 for (count = 0; count <= 50000; count++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 snd_ali_delay(codec, 6);
676 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
677 R2 = bval & 0x1F;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200678 if (R2 != R1)
679 R1 = R2;
680 else
681 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683
684 if (count > 50000) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200685 snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return;
687 }
688
689 if (R2 >= 0x0b && R2 <= 0x0e) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200690 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
691 wval &= 0xe0f0;
692 wval |= (0x09 << 8) | 0x05;
693 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200695 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
696 outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 } else if (R2 == 0x12) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200698 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
699 wval &= 0xe0f0;
700 wval |= (0x0e << 8) | 0x08;
701 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200703 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
704 outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706}
707
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100708static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200710 u32 dwRate;
711 u8 bval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200713 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
714 bval &= 0x7f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 bval |= 0x40;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200716 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 snd_ali_detect_spdif_rate(codec);
719
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200720 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
721 bval &= 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200723 switch (bval) {
724 case 0: dwRate = 44100; break;
725 case 1: dwRate = 48000; break;
726 case 2: dwRate = 32000; break;
727 default: dwRate = 0; break;
728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 return dwRate;
731}
732
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100733static void snd_ali_enable_spdif_in(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 unsigned int dwVal;
736
737 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
738 dwVal |= ALI_SPDIF_IN_SUPPORT;
739 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
740
741 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
742 dwVal |= 0x02;
743 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
744
745 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
746}
747
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100748static void snd_ali_disable_spdif_in(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 unsigned int dwVal;
751
752 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
753 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
754 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
755
756 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
757}
758
759
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100760static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 unsigned char bVal;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200763 unsigned int dwRate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200765 switch (rate) {
766 case 32000: dwRate = 0x300; break;
767 case 48000: dwRate = 0x200; break;
768 default: dwRate = 0; break;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
772 bVal &= (unsigned char)(~(1<<6));
773
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200774 bVal |= 0x80; /* select right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
776 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
777
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200778 bVal &= ~0x80; /* select left */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
780 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
781}
782
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100783static void snd_ali_enable_spdif_out(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 unsigned short wVal;
786 unsigned char bVal;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200787 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 pci_dev = codec->pci_m1533;
790 if (pci_dev == NULL)
791 return;
792 pci_read_config_byte(pci_dev, 0x61, &bVal);
793 bVal |= 0x40;
794 pci_write_config_byte(pci_dev, 0x61, bVal);
795 pci_read_config_byte(pci_dev, 0x7d, &bVal);
796 bVal |= 0x01;
797 pci_write_config_byte(pci_dev, 0x7d, bVal);
798
799 pci_read_config_byte(pci_dev, 0x7e, &bVal);
800 bVal &= (~0x20);
801 bVal |= 0x10;
802 pci_write_config_byte(pci_dev, 0x7e, bVal);
803
804 bVal = inb(ALI_REG(codec, ALI_SCTRL));
805 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
806
807 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
808 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
809
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200810 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
811 wVal |= ALI_SPDIF_OUT_SEL_PCM;
812 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
813 snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100816static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200818 unsigned short wVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
821 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
822 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
823/*
824 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
825 if (flag & ALI_SPDIF_OUT_NON_PCM)
826 wVal |= 0x0002;
827 else
828 wVal &= (~0x0002);
829 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
830*/
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200831 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100834static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200836 unsigned short wVal;
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
839 wVal |= ALI_SPDIF_OUT_SEL_PCM;
840 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
841
842 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
843}
844
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100845static void snd_ali_disable_spdif_out(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 unsigned char bVal;
848
849 bVal = inb(ALI_REG(codec, ALI_SCTRL));
850 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
851
852 snd_ali_disable_spdif_chnout(codec);
853}
854
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200855static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200857 struct snd_ali_voice *pvoice;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200858 struct snd_ali_channel_control *pchregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 unsigned int old, mask;
860#ifdef ALI_DEBUG
861 unsigned int temp, cspf;
862#endif
863
864 pchregs = &(codec->chregs);
865
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200866 /* check if interrupt occurred for channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 old = pchregs->data.aint;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200868 mask = 1U << (channel & 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 if (!(old & mask))
871 return;
872
873 pvoice = &codec->synth.voices[channel];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 udelay(100);
876 spin_lock(&codec->reg_lock);
877
878 if (pvoice->pcm && pvoice->substream) {
879 /* pcm interrupt */
880#ifdef ALI_DEBUG
881 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
882 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
883 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
884#endif
885 if (pvoice->running) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200886 snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",
887 (u16)temp, cspf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 spin_unlock(&codec->reg_lock);
889 snd_pcm_period_elapsed(pvoice->substream);
890 spin_lock(&codec->reg_lock);
891 } else {
892 snd_ali_stop_voice(codec, channel);
893 snd_ali_disable_voice_irq(codec, channel);
894 }
895 } else if (codec->synth.voices[channel].synth) {
896 /* synth interrupt */
897 } else if (codec->synth.voices[channel].midi) {
898 /* midi interrupt */
899 } else {
900 /* unknown interrupt */
901 snd_ali_stop_voice(codec, channel);
902 snd_ali_disable_voice_irq(codec, channel);
903 }
904 spin_unlock(&codec->reg_lock);
905 outl(mask,ALI_REG(codec,pchregs->regs.aint));
906 pchregs->data.aint = old & (~mask);
907}
908
David Howells7d12e782006-10-05 14:55:46 +0100909static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100911 struct snd_ali *codec = dev_id;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200912 int channel;
913 unsigned int audio_int;
914 struct snd_ali_channel_control *pchregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200916 if (codec == NULL || !codec->hw_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return IRQ_NONE;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200918
919 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
920 if (!audio_int)
921 return IRQ_NONE;
922
923 pchregs = &(codec->chregs);
924 if (audio_int & ADDRESS_IRQ) {
925 /* get interrupt status for all channels */
926 pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
927 for (channel = 0; channel < ALI_CHANNELS; channel++)
928 snd_ali_update_ptr(codec, channel);
929 }
930 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
931 ALI_REG(codec, ALI_MISCINT));
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return IRQ_HANDLED;
934}
935
936
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200937static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
938 int type, int rec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200940 struct snd_ali_voice *pvoice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 int idx;
942
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200943 snd_ali_printk("alloc_voice: type=%d rec=%d\n", type, rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Takashi Iwai755e1372005-11-11 21:05:27 +0100945 spin_lock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200947 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
948 snd_ali_find_free_channel(codec,rec);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200949 if (idx < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200950 snd_printk(KERN_ERR "ali_alloc_voice: err.\n");
Takashi Iwai755e1372005-11-11 21:05:27 +0100951 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return NULL;
953 }
954 pvoice = &(codec->synth.voices[idx]);
Takashi Iwai755e1372005-11-11 21:05:27 +0100955 pvoice->codec = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 pvoice->use = 1;
957 pvoice->pcm = 1;
958 pvoice->mode = rec;
Takashi Iwai755e1372005-11-11 21:05:27 +0100959 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return pvoice;
961 }
Takashi Iwai755e1372005-11-11 21:05:27 +0100962 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return NULL;
964}
965
966
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200967static void snd_ali_free_voice(struct snd_ali * codec,
968 struct snd_ali_voice *pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 void (*private_free)(void *);
971 void *private_data;
972
973 snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
Julia Lawall4b3be6a2009-10-17 08:33:22 +0200974 if (!pvoice->use)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return;
976 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
Takashi Iwai755e1372005-11-11 21:05:27 +0100977 spin_lock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 private_free = pvoice->private_free;
979 private_data = pvoice->private_data;
980 pvoice->private_free = NULL;
981 pvoice->private_data = NULL;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200982 if (pvoice->pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 snd_ali_free_channel_pcm(codec, pvoice->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 pvoice->use = pvoice->pcm = pvoice->synth = 0;
985 pvoice->substream = NULL;
Takashi Iwai755e1372005-11-11 21:05:27 +0100986 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (private_free)
988 private_free(private_data);
989}
990
991
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200992static void snd_ali_clear_voices(struct snd_ali *codec,
993 unsigned int v_min,
994 unsigned int v_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 unsigned int i;
997
998 for (i = v_min; i <= v_max; i++) {
999 snd_ali_stop_voice(codec, i);
1000 snd_ali_disable_voice_irq(codec, i);
1001 }
1002}
1003
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001004static void snd_ali_write_voice_regs(struct snd_ali *codec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 unsigned int Channel,
1006 unsigned int LBA,
1007 unsigned int CSO,
1008 unsigned int ESO,
1009 unsigned int DELTA,
1010 unsigned int ALPHA_FMS,
1011 unsigned int GVSEL,
1012 unsigned int PAN,
1013 unsigned int VOL,
1014 unsigned int CTRL,
1015 unsigned int EC)
1016{
1017 unsigned int ctlcmds[4];
1018
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001019 outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1022 ctlcmds[1] = LBA;
1023 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1024 ctlcmds[3] = (GVSEL << 31) |
1025 ((PAN & 0x0000007f) << 24) |
1026 ((VOL & 0x000000ff) << 16) |
1027 ((CTRL & 0x0000000f) << 12) |
1028 (EC & 0x00000fff);
1029
1030 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1031
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001032 outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
1033 outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
1034 outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
1035 outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1038 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1039}
1040
1041static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1042{
1043 unsigned int delta;
1044
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001045 if (rate < 4000)
1046 rate = 4000;
1047 if (rate > 48000)
1048 rate = 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 if (rec) {
1051 if (rate == 44100)
1052 delta = 0x116a;
1053 else if (rate == 8000)
1054 delta = 0x6000;
1055 else if (rate == 48000)
1056 delta = 0x1000;
1057 else
1058 delta = ((48000 << 12) / rate) & 0x0000ffff;
1059 } else {
1060 if (rate == 44100)
1061 delta = 0xeb3;
1062 else if (rate == 8000)
1063 delta = 0x2ab;
1064 else if (rate == 48000)
1065 delta = 0x1000;
1066 else
1067 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1068 }
1069
1070 return delta;
1071}
1072
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001073static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 unsigned int CTRL;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001076 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 /* set ctrl mode
1079 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1080 */
1081 CTRL = 0x00000001;
1082 if (snd_pcm_format_width(runtime->format) == 16)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001083 CTRL |= 0x00000008; /* 16-bit data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (!snd_pcm_format_unsigned(runtime->format))
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001085 CTRL |= 0x00000002; /* signed data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (runtime->channels > 1)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001087 CTRL |= 0x00000004; /* stereo data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return CTRL;
1089}
1090
1091/*
1092 * PCM part
1093 */
1094
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001095static int snd_ali_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 int cmd)
1097
1098{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001099 struct snd_ali *codec = snd_pcm_substream_chip(substream);
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001100 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 unsigned int what, whati, capture_flag;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001102 struct snd_ali_voice *pvoice, *evoice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 unsigned int val;
1104 int do_start;
1105
1106 switch (cmd) {
1107 case SNDRV_PCM_TRIGGER_START:
1108 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001109 do_start = 1;
1110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 case SNDRV_PCM_TRIGGER_STOP:
1112 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001113 do_start = 0;
1114 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 default:
1116 return -EINVAL;
1117 }
1118
1119 what = whati = capture_flag = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +01001120 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001121 if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
1122 pvoice = s->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 evoice = pvoice->extra;
1124 what |= 1 << (pvoice->number & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001125 if (evoice == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 whati |= 1 << (pvoice->number & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001127 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 whati |= 1 << (evoice->number & 0x1f);
1129 what |= 1 << (evoice->number & 0x1f);
1130 }
1131 if (do_start) {
1132 pvoice->running = 1;
1133 if (evoice != NULL)
1134 evoice->running = 1;
1135 } else {
1136 pvoice->running = 0;
1137 if (evoice != NULL)
1138 evoice->running = 0;
1139 }
1140 snd_pcm_trigger_done(s, substream);
1141 if (pvoice->mode)
1142 capture_flag = 1;
1143 }
1144 }
1145 spin_lock(&codec->reg_lock);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001146 if (!do_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 outl(what, ALI_REG(codec, ALI_STOP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 val = inl(ALI_REG(codec, ALI_AINTEN));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001149 if (do_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 val |= whati;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001151 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 val &= ~whati;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 outl(val, ALI_REG(codec, ALI_AINTEN));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001154 if (do_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 outl(what, ALI_REG(codec, ALI_START));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001156 snd_ali_printk("trigger: what=%xh whati=%xh\n", what, whati);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 spin_unlock(&codec->reg_lock);
1158
1159 return 0;
1160}
1161
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001162static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001163 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001165 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1166 struct snd_pcm_runtime *runtime = substream->runtime;
1167 struct snd_ali_voice *pvoice = runtime->private_data;
1168 struct snd_ali_voice *evoice = pvoice->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 int err;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001170
1171 err = snd_pcm_lib_malloc_pages(substream,
1172 params_buffer_bytes(hw_params));
1173 if (err < 0)
1174 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /* voice management */
1177
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001178 if (params_buffer_size(hw_params) / 2 !=
1179 params_period_size(hw_params)) {
1180 if (!evoice) {
1181 evoice = snd_ali_alloc_voice(codec,
1182 SNDRV_ALI_VOICE_TYPE_PCM,
1183 0, -1);
1184 if (!evoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return -ENOMEM;
1186 pvoice->extra = evoice;
1187 evoice->substream = substream;
1188 }
1189 } else {
Takashi Iwai27043642007-05-19 16:30:35 +02001190 if (evoice) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 snd_ali_free_voice(codec, evoice);
1192 pvoice->extra = evoice = NULL;
1193 }
1194 }
1195
1196 return 0;
1197}
1198
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001199static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001201 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1202 struct snd_pcm_runtime *runtime = substream->runtime;
1203 struct snd_ali_voice *pvoice = runtime->private_data;
1204 struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 snd_pcm_lib_free_pages(substream);
Takashi Iwai27043642007-05-19 16:30:35 +02001207 if (evoice) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 snd_ali_free_voice(codec, evoice);
1209 pvoice->extra = NULL;
1210 }
1211 return 0;
1212}
1213
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001214static int snd_ali_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001215 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001217 return snd_pcm_lib_malloc_pages(substream,
1218 params_buffer_bytes(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001221static int snd_ali_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
1223 return snd_pcm_lib_free_pages(substream);
1224}
1225
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001226static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001228 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1229 struct snd_pcm_runtime *runtime = substream->runtime;
1230 struct snd_ali_voice *pvoice = runtime->private_data;
1231 struct snd_ali_voice *evoice = pvoice->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 unsigned int LBA;
1234 unsigned int Delta;
1235 unsigned int ESO;
1236 unsigned int CTRL;
1237 unsigned int GVSEL;
1238 unsigned int PAN;
1239 unsigned int VOL;
1240 unsigned int EC;
1241
1242 snd_ali_printk("playback_prepare ...\n");
1243
Takashi Iwai755e1372005-11-11 21:05:27 +01001244 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 /* set Delta (rate) value */
1247 Delta = snd_ali_convert_rate(runtime->rate, 0);
1248
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001249 if (pvoice->number == ALI_SPDIF_IN_CHANNEL ||
1250 pvoice->number == ALI_PCM_IN_CHANNEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 snd_ali_disable_special_channel(codec, pvoice->number);
1252 else if (codec->spdif_support &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001253 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
1254 ALI_SPDIF_OUT_CH_ENABLE)
1255 && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1257 Delta = 0x1000;
1258 }
1259
1260 /* set Loop Back Address */
1261 LBA = runtime->dma_addr;
1262
1263 /* set interrupt count size */
1264 pvoice->count = runtime->period_size;
1265
1266 /* set target ESO for channel */
1267 pvoice->eso = runtime->buffer_size;
1268
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001269 snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",
1270 pvoice->eso, pvoice->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 /* set ESO to capture first MIDLP interrupt */
1273 ESO = pvoice->eso -1;
1274 /* set ctrl mode */
1275 CTRL = snd_ali_control_mode(substream);
1276
1277 GVSEL = 1;
1278 PAN = 0;
1279 VOL = 0;
1280 EC = 0;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001281 snd_ali_printk("playback_prepare:\n");
1282 snd_ali_printk("ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
1283 pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1284 snd_ali_write_voice_regs(codec,
1285 pvoice->number,
1286 LBA,
1287 0, /* cso */
1288 ESO,
1289 Delta,
1290 0, /* alpha */
1291 GVSEL,
1292 PAN,
1293 VOL,
1294 CTRL,
1295 EC);
Takashi Iwai27043642007-05-19 16:30:35 +02001296 if (evoice) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 evoice->count = pvoice->count;
1298 evoice->eso = pvoice->count << 1;
1299 ESO = evoice->eso - 1;
1300 snd_ali_write_voice_regs(codec,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001301 evoice->number,
1302 LBA,
1303 0, /* cso */
1304 ESO,
1305 Delta,
1306 0, /* alpha */
1307 GVSEL,
1308 0x7f,
1309 0x3ff,
1310 CTRL,
1311 EC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001313 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return 0;
1315}
1316
1317
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001318static int snd_ali_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001320 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1321 struct snd_pcm_runtime *runtime = substream->runtime;
1322 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 unsigned int LBA;
1324 unsigned int Delta;
1325 unsigned int ESO;
1326 unsigned int CTRL;
1327 unsigned int GVSEL;
1328 unsigned int PAN;
1329 unsigned int VOL;
1330 unsigned int EC;
1331 u8 bValue;
1332
Takashi Iwai755e1372005-11-11 21:05:27 +01001333 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001335 snd_ali_printk("ali_prepare...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 snd_ali_enable_special_channel(codec,pvoice->number);
1338
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001339 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1340 pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
1341 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001343 /* Prepare capture intr channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1345
1346 unsigned int rate;
1347
Takashi Iwai755e1372005-11-11 21:05:27 +01001348 spin_unlock_irq(&codec->reg_lock);
1349 if (codec->revision != ALI_5451_V02)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return -1;
Takashi Iwai755e1372005-11-11 21:05:27 +01001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 rate = snd_ali_get_spdif_in_rate(codec);
1353 if (rate == 0) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001354 snd_printk(KERN_WARNING "ali_capture_preapre: "
1355 "spdif rate detect err!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 rate = 48000;
1357 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001358 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1360 if (bValue & 0x10) {
1361 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
Takashi Iwai99b359b2005-10-20 18:26:44 +02001362 printk(KERN_WARNING "clear SPDIF parity error flag.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364
1365 if (rate != 48000)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001366 Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001369 /* set target ESO for channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 pvoice->eso = runtime->buffer_size;
1371
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001372 /* set interrupt count size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 pvoice->count = runtime->period_size;
1374
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001375 /* set Loop Back Address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 LBA = runtime->dma_addr;
1377
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001378 /* set ESO to capture first MIDLP interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 ESO = pvoice->eso - 1;
1380 CTRL = snd_ali_control_mode(substream);
1381 GVSEL = 0;
1382 PAN = 0x00;
1383 VOL = 0x00;
1384 EC = 0;
1385
1386 snd_ali_write_voice_regs( codec,
1387 pvoice->number,
1388 LBA,
1389 0, /* cso */
1390 ESO,
1391 Delta,
1392 0, /* alpha */
1393 GVSEL,
1394 PAN,
1395 VOL,
1396 CTRL,
1397 EC);
1398
Takashi Iwai755e1372005-11-11 21:05:27 +01001399 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 return 0;
1402}
1403
1404
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001405static snd_pcm_uframes_t
1406snd_ali_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001408 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1409 struct snd_pcm_runtime *runtime = substream->runtime;
1410 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 unsigned int cso;
1412
1413 spin_lock(&codec->reg_lock);
1414 if (!pvoice->running) {
1415 spin_unlock(&codec->reg_lock);
1416 return 0;
1417 }
1418 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1419 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1420 spin_unlock(&codec->reg_lock);
1421 snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1422
1423 return cso;
1424}
1425
1426
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001427static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001429 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1430 struct snd_pcm_runtime *runtime = substream->runtime;
1431 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 unsigned int cso;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Takashi Iwai755e1372005-11-11 21:05:27 +01001434 spin_lock(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (!pvoice->running) {
Denis Efremovdacae5a2013-02-11 19:49:48 +04001436 spin_unlock(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return 0;
1438 }
1439 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1440 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
Takashi Iwai755e1372005-11-11 21:05:27 +01001441 spin_unlock(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 return cso;
1444}
1445
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001446static struct snd_pcm_hardware snd_ali_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001448 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1449 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1450 SNDRV_PCM_INFO_MMAP_VALID |
1451 SNDRV_PCM_INFO_RESUME |
1452 SNDRV_PCM_INFO_SYNC_START),
1453 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1454 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1455 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 .rate_min = 4000,
1457 .rate_max = 48000,
1458 .channels_min = 1,
1459 .channels_max = 2,
1460 .buffer_bytes_max = (256*1024),
1461 .period_bytes_min = 64,
1462 .period_bytes_max = (256*1024),
1463 .periods_min = 1,
1464 .periods_max = 1024,
1465 .fifo_size = 0,
1466};
1467
1468/*
1469 * Capture support device description
1470 */
1471
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001472static struct snd_pcm_hardware snd_ali_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001474 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1475 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1476 SNDRV_PCM_INFO_MMAP_VALID |
1477 SNDRV_PCM_INFO_RESUME |
1478 SNDRV_PCM_INFO_SYNC_START),
1479 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1480 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1481 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 .rate_min = 4000,
1483 .rate_max = 48000,
1484 .channels_min = 1,
1485 .channels_max = 2,
1486 .buffer_bytes_max = (128*1024),
1487 .period_bytes_min = 64,
1488 .period_bytes_max = (128*1024),
1489 .periods_min = 1,
1490 .periods_max = 1024,
1491 .fifo_size = 0,
1492};
1493
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001494static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001496 struct snd_ali_voice *pvoice = runtime->private_data;
1497 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 if (pvoice) {
1500 codec = pvoice->codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 snd_ali_free_voice(pvoice->codec, pvoice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503}
1504
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001505static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
1506 int channel, struct snd_pcm_hardware *phw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001508 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1509 struct snd_pcm_runtime *runtime = substream->runtime;
1510 struct snd_ali_voice *pvoice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001512 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
1513 channel);
1514 if (!pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 pvoice->substream = substream;
1518 runtime->private_data = pvoice;
1519 runtime->private_free = snd_ali_pcm_free_substream;
1520
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001521 runtime->hw = *phw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 snd_pcm_set_sync(substream);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001523 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1524 0, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return 0;
1526}
1527
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001528static int snd_ali_playback_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001529{
1530 return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1531}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001533static int snd_ali_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001535 return snd_ali_open(substream, 1, -1, &snd_ali_capture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001538static int snd_ali_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 return 0;
1541}
1542
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001543static int snd_ali_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001545 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1546 struct snd_ali_voice *pvoice = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 snd_ali_disable_special_channel(codec,pvoice->number);
1549
1550 return 0;
1551}
1552
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001553static struct snd_pcm_ops snd_ali_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 .open = snd_ali_playback_open,
1555 .close = snd_ali_playback_close,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001556 .ioctl = snd_pcm_lib_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 .hw_params = snd_ali_playback_hw_params,
1558 .hw_free = snd_ali_playback_hw_free,
1559 .prepare = snd_ali_playback_prepare,
1560 .trigger = snd_ali_trigger,
1561 .pointer = snd_ali_playback_pointer,
1562};
1563
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001564static struct snd_pcm_ops snd_ali_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 .open = snd_ali_capture_open,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001566 .close = snd_ali_close,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001567 .ioctl = snd_pcm_lib_ioctl,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001568 .hw_params = snd_ali_hw_params,
1569 .hw_free = snd_ali_hw_free,
1570 .prepare = snd_ali_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 .trigger = snd_ali_trigger,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001572 .pointer = snd_ali_pointer,
1573};
1574
1575/*
1576 * Modem PCM
1577 */
1578
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001579static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
1580 struct snd_pcm_hw_params *hw_params)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001581{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001582 struct snd_ali *chip = snd_pcm_substream_chip(substream);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001583 unsigned int modem_num = chip->num_of_codecs - 1;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001584 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
1585 params_rate(hw_params));
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001586 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1587 return snd_ali_hw_params(substream, hw_params);
1588}
1589
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001590static struct snd_pcm_hardware snd_ali_modem =
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001591{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001592 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1593 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1594 SNDRV_PCM_INFO_MMAP_VALID |
1595 SNDRV_PCM_INFO_RESUME |
1596 SNDRV_PCM_INFO_SYNC_START),
1597 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1598 .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
1599 SNDRV_PCM_RATE_16000),
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001600 .rate_min = 8000,
1601 .rate_max = 16000,
1602 .channels_min = 1,
1603 .channels_max = 1,
1604 .buffer_bytes_max = (256*1024),
1605 .period_bytes_min = 64,
1606 .period_bytes_max = (256*1024),
1607 .periods_min = 1,
1608 .periods_max = 1024,
1609 .fifo_size = 0,
1610};
1611
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001612static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
1613 int channel)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001614{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001615 static unsigned int rates[] = {8000, 9600, 12000, 16000};
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001616 static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001617 .count = ARRAY_SIZE(rates),
1618 .list = rates,
1619 .mask = 0,
1620 };
1621 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001622
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001623 if (err)
1624 return err;
1625 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1626 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1627}
1628
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001629static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001630{
1631 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1632}
1633
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001634static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001635{
1636 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1637}
1638
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001639static struct snd_pcm_ops snd_ali_modem_playback_ops = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001640 .open = snd_ali_modem_playback_open,
1641 .close = snd_ali_close,
1642 .ioctl = snd_pcm_lib_ioctl,
1643 .hw_params = snd_ali_modem_hw_params,
1644 .hw_free = snd_ali_hw_free,
1645 .prepare = snd_ali_prepare,
1646 .trigger = snd_ali_trigger,
1647 .pointer = snd_ali_pointer,
1648};
1649
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001650static struct snd_pcm_ops snd_ali_modem_capture_ops = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001651 .open = snd_ali_modem_capture_open,
1652 .close = snd_ali_close,
1653 .ioctl = snd_pcm_lib_ioctl,
1654 .hw_params = snd_ali_modem_hw_params,
1655 .hw_free = snd_ali_hw_free,
1656 .prepare = snd_ali_prepare,
1657 .trigger = snd_ali_trigger,
1658 .pointer = snd_ali_pointer,
1659};
1660
1661
1662struct ali_pcm_description {
1663 char *name;
1664 unsigned int playback_num;
1665 unsigned int capture_num;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001666 struct snd_pcm_ops *playback_ops;
1667 struct snd_pcm_ops *capture_ops;
Sasha Khapyorsky6632d192005-09-29 11:48:17 +02001668 unsigned short class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669};
1670
1671
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001672static void snd_ali_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001674 struct snd_ali *codec = pcm->private_data;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001675 codec->pcm[pcm->device] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001678
Bill Pembertone23e7a12012-12-06 12:35:10 -05001679static int snd_ali_pcm(struct snd_ali *codec, int device,
1680 struct ali_pcm_description *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001682 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 int err;
1684
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001685 err = snd_pcm_new(codec->card, desc->name, device,
1686 desc->playback_num, desc->capture_num, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (err < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001688 snd_printk(KERN_ERR "snd_ali_pcm: err called snd_pcm_new.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return err;
1690 }
1691 pcm->private_data = codec;
1692 pcm->private_free = snd_ali_pcm_free;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001693 if (desc->playback_ops)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001694 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1695 desc->playback_ops);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001696 if (desc->capture_ops)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001697 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1698 desc->capture_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001701 snd_dma_pci_data(codec->pci),
1702 64*1024, 128*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 pcm->info_flags = 0;
Sasha Khapyorsky6632d192005-09-29 11:48:17 +02001705 pcm->dev_class = desc->class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001707 strcpy(pcm->name, desc->name);
1708 codec->pcm[0] = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return 0;
1710}
1711
Clemens Ladischa53fc182005-08-11 15:59:17 +02001712static struct ali_pcm_description ali_pcms[] = {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001713 { .name = "ALI 5451",
1714 .playback_num = ALI_CHANNELS,
1715 .capture_num = 1,
1716 .playback_ops = &snd_ali_playback_ops,
1717 .capture_ops = &snd_ali_capture_ops
1718 },
1719 { .name = "ALI 5451 modem",
1720 .playback_num = 1,
1721 .capture_num = 1,
1722 .playback_ops = &snd_ali_modem_playback_ops,
1723 .capture_ops = &snd_ali_modem_capture_ops,
1724 .class = SNDRV_PCM_CLASS_MODEM
1725 }
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001726};
1727
Bill Pembertone23e7a12012-12-06 12:35:10 -05001728static int snd_ali_build_pcms(struct snd_ali *codec)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001729{
1730 int i, err;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001731 for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
1732 err = snd_ali_pcm(codec, i, &ali_pcms[i]);
1733 if (err < 0)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001734 return err;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001735 }
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001736 return 0;
1737}
1738
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740#define ALI5451_SPDIF(xname, xindex, value) \
1741{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1742.info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1743.put = snd_ali5451_spdif_put, .private_value = value}
1744
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001745#define snd_ali5451_spdif_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001747static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
1748 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001750 struct snd_ali *codec = kcontrol->private_data;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001751 unsigned int spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001753 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Takashi Iwai755e1372005-11-11 21:05:27 +01001755 spin_lock_irq(&codec->reg_lock);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001756 switch (kcontrol->private_value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 case 0:
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001758 spdif_enable = (codec->spdif_mask & 0x02) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 break;
1760 case 1:
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001761 spdif_enable = ((codec->spdif_mask & 0x02) &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001762 (codec->spdif_mask & 0x04)) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 break;
1764 case 2:
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001765 spdif_enable = (codec->spdif_mask & 0x01) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 break;
1767 default:
1768 break;
1769 }
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001770 ucontrol->value.integer.value[0] = spdif_enable;
Takashi Iwai755e1372005-11-11 21:05:27 +01001771 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return 0;
1773}
1774
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001775static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
1776 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001778 struct snd_ali *codec = kcontrol->private_data;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001779 unsigned int change = 0, spdif_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001781 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Takashi Iwai755e1372005-11-11 21:05:27 +01001783 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 switch (kcontrol->private_value) {
1785 case 0:
1786 change = (codec->spdif_mask & 0x02) ? 1 : 0;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001787 change = change ^ spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (change) {
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001789 if (spdif_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 codec->spdif_mask |= 0x02;
1791 snd_ali_enable_spdif_out(codec);
1792 } else {
1793 codec->spdif_mask &= ~(0x02);
1794 codec->spdif_mask &= ~(0x04);
1795 snd_ali_disable_spdif_out(codec);
1796 }
1797 }
1798 break;
1799 case 1:
1800 change = (codec->spdif_mask & 0x04) ? 1 : 0;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001801 change = change ^ spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (change && (codec->spdif_mask & 0x02)) {
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001803 if (spdif_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 codec->spdif_mask |= 0x04;
1805 snd_ali_enable_spdif_chnout(codec);
1806 } else {
1807 codec->spdif_mask &= ~(0x04);
1808 snd_ali_disable_spdif_chnout(codec);
1809 }
1810 }
1811 break;
1812 case 2:
1813 change = (codec->spdif_mask & 0x01) ? 1 : 0;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001814 change = change ^ spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (change) {
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001816 if (spdif_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 codec->spdif_mask |= 0x01;
1818 snd_ali_enable_spdif_in(codec);
1819 } else {
1820 codec->spdif_mask &= ~(0x01);
1821 snd_ali_disable_spdif_in(codec);
1822 }
1823 }
1824 break;
1825 default:
1826 break;
1827 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001828 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 return change;
1831}
1832
Bill Pembertone23e7a12012-12-06 12:35:10 -05001833static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 /* spdif aplayback switch */
1835 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001836 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 /* spdif out to spdif channel */
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001838 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 /* spdif in from spdif channel */
1840 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1841};
1842
Bill Pembertone23e7a12012-12-06 12:35:10 -05001843static int snd_ali_mixer(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001845 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 unsigned int idx;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001847 int i, err;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001848 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 .write = snd_ali_codec_write,
1850 .read = snd_ali_codec_read,
1851 };
1852
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001853 err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
1854 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 memset(&ac97, 0, sizeof(ac97));
1858 ac97.private_data = codec;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001859
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001860 for (i = 0; i < codec->num_of_codecs; i++) {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001861 ac97.num = i;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001862 err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
1863 if (err < 0) {
1864 snd_printk(KERN_ERR
1865 "ali mixer %d creating error.\n", i);
1866 if (i == 0)
Takashi Iwai4d060fd2005-10-04 13:50:44 +02001867 return err;
1868 codec->num_of_codecs = 1;
1869 break;
1870 }
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001871 }
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (codec->spdif_support) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001874 for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1875 err = snd_ctl_add(codec->card,
1876 snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1877 if (err < 0)
1878 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
1880 }
1881 return 0;
1882}
1883
Takashi Iwaic7561cd2012-08-14 18:12:04 +02001884#ifdef CONFIG_PM_SLEEP
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001885static int ali_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001887 struct pci_dev *pci = to_pci_dev(dev);
1888 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001889 struct snd_ali *chip = card->private_data;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001890 struct snd_ali_image *im;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 int i, j;
1892
1893 im = chip->image;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001894 if (!im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 return 0;
1896
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001897 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001898 for (i = 0; i < chip->num_of_codecs; i++) {
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001899 snd_pcm_suspend_all(chip->pcm[i]);
1900 snd_ac97_suspend(chip->ac97[i]);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 spin_lock_irq(&chip->reg_lock);
1904
1905 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001906 /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1908
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001909 /* disable all IRQ bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 outl(0, ALI_REG(chip, ALI_MISCINT));
1911
1912 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1913 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1914 continue;
1915 im->regs[i] = inl(ALI_REG(chip, i*4));
1916 }
1917
1918 for (i = 0; i < ALI_CHANNELS; i++) {
1919 outb(i, ALI_REG(chip, ALI_GC_CIR));
1920 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1921 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1922 }
1923
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001924 /* stop all HW channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1926
1927 spin_unlock_irq(&chip->reg_lock);
Takashi Iwai30b35392006-10-11 18:52:53 +02001928
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001929 pci_disable_device(pci);
1930 pci_save_state(pci);
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001931 pci_set_power_state(pci, PCI_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return 0;
1933}
1934
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001935static int ali_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001937 struct pci_dev *pci = to_pci_dev(dev);
1938 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001939 struct snd_ali *chip = card->private_data;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001940 struct snd_ali_image *im;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 int i, j;
1942
1943 im = chip->image;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001944 if (!im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 return 0;
1946
Takashi Iwai30b35392006-10-11 18:52:53 +02001947 pci_set_power_state(pci, PCI_D0);
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001948 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02001949 if (pci_enable_device(pci) < 0) {
1950 printk(KERN_ERR "ali5451: pci_enable_device failed, "
1951 "disabling device\n");
1952 snd_card_disconnect(card);
1953 return -EIO;
1954 }
1955 pci_set_master(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 spin_lock_irq(&chip->reg_lock);
1958
1959 for (i = 0; i < ALI_CHANNELS; i++) {
1960 outb(i, ALI_REG(chip, ALI_GC_CIR));
1961 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1962 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
1963 }
1964
1965 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001966 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
1967 (i*4 == ALI_START))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 continue;
1969 outl(im->regs[i], ALI_REG(chip, i*4));
1970 }
1971
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001972 /* start HW channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001974 /* restore IRQ enable bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
1976
1977 spin_unlock_irq(&chip->reg_lock);
1978
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001979 for (i = 0 ; i < chip->num_of_codecs; i++)
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001980 snd_ac97_resume(chip->ac97[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001982 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return 0;
1984}
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001985
1986static SIMPLE_DEV_PM_OPS(ali_pm, ali_suspend, ali_resume);
1987#define ALI_PM_OPS &ali_pm
1988#else
1989#define ALI_PM_OPS NULL
Takashi Iwaic7561cd2012-08-14 18:12:04 +02001990#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001992static int snd_ali_free(struct snd_ali * codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 if (codec->hw_initialized)
1995 snd_ali_disable_address_interrupt(codec);
Jeff Garzikf000fd82008-04-22 13:50:34 +02001996 if (codec->irq >= 0)
Takashi Iwai437a5a42006-11-21 12:14:23 +01001997 free_irq(codec->irq, codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 if (codec->port)
1999 pci_release_regions(codec->pci);
2000 pci_disable_device(codec->pci);
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002001#ifdef CONFIG_PM_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 kfree(codec->image);
2003#endif
Jiri Slaby0dd119f2005-09-07 14:28:33 +02002004 pci_dev_put(codec->pci_m1533);
2005 pci_dev_put(codec->pci_m7101);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 kfree(codec);
2007 return 0;
2008}
2009
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002010static int snd_ali_chip_init(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
2012 unsigned int legacy;
2013 unsigned char temp;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002014 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 snd_ali_printk("chip initializing ... \n");
2017
2018 if (snd_ali_reset_5451(codec)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002019 snd_printk(KERN_ERR "ali_chip_init: reset 5451 error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return -1;
2021 }
2022
2023 if (codec->revision == ALI_5451_V02) {
2024 pci_dev = codec->pci_m1533;
2025 pci_read_config_byte(pci_dev, 0x59, &temp);
2026 temp |= 0x80;
2027 pci_write_config_byte(pci_dev, 0x59, temp);
2028
2029 pci_dev = codec->pci_m7101;
2030 pci_read_config_byte(pci_dev, 0xb8, &temp);
2031 temp |= 0x20;
2032 pci_write_config_byte(pci_dev, 0xB8, temp);
2033 }
2034
2035 pci_read_config_dword(codec->pci, 0x44, &legacy);
2036 legacy &= 0xff00ff00;
2037 legacy |= 0x000800aa;
2038 pci_write_config_dword(codec->pci, 0x44, legacy);
2039
2040 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2041 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2042 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2043 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2044 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2045
2046 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002047 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
2048 AC97_EXTENDED_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (codec->spdif_support) {
2050 snd_ali_enable_spdif_out(codec);
2051 codec->spdif_mask = 0x00000002;
2052 }
2053
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002054 codec->num_of_codecs = 1;
2055
2056 /* secondary codec - modem */
2057 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
2058 codec->num_of_codecs++;
2059 outl(inl(ALI_REG(codec, ALI_SCTRL)) |
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002060 (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
2061 ALI_SCTRL_LINE_OUT_EN),
2062 ALI_REG(codec, ALI_SCTRL));
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002063 }
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 snd_ali_printk("chip initialize succeed.\n");
2066 return 0;
2067
2068}
2069
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002070/* proc for register dump */
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002071static void snd_ali_proc_read(struct snd_info_entry *entry,
2072 struct snd_info_buffer *buf)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002073{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002074 struct snd_ali *codec = entry->private_data;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002075 int i;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002076 for (i = 0; i < 256 ; i+= 4)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002077 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
2078}
2079
Bill Pembertone23e7a12012-12-06 12:35:10 -05002080static void snd_ali_proc_init(struct snd_ali *codec)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002081{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002082 struct snd_info_entry *entry;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002083 if (!snd_card_proc_new(codec->card, "ali5451", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002084 snd_info_set_text_ops(entry, codec, snd_ali_proc_read);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002085}
2086
Bill Pembertone23e7a12012-12-06 12:35:10 -05002087static int snd_ali_resources(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
2089 int err;
2090
Wolfram Sangc468ac22009-03-20 10:08:11 +01002091 snd_ali_printk("resources allocation ...\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002092 err = pci_request_regions(codec->pci, "ALI 5451");
2093 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return err;
2095 codec->port = pci_resource_start(codec->pci, 0);
2096
Takashi Iwai437a5a42006-11-21 12:14:23 +01002097 if (request_irq(codec->pci->irq, snd_ali_card_interrupt,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002098 IRQF_SHARED, KBUILD_MODNAME, codec)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002099 snd_printk(KERN_ERR "Unable to request irq.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return -EBUSY;
2101 }
2102 codec->irq = codec->pci->irq;
Wolfram Sangc468ac22009-03-20 10:08:11 +01002103 snd_ali_printk("resources allocated.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return 0;
2105}
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002106static int snd_ali_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002108 struct snd_ali *codec = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 snd_ali_free(codec);
2110 return 0;
2111}
2112
Bill Pembertone23e7a12012-12-06 12:35:10 -05002113static int snd_ali_create(struct snd_card *card,
2114 struct pci_dev *pci,
2115 int pcm_streams,
2116 int spdif_support,
2117 struct snd_ali **r_ali)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002119 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 int i, err;
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002121 unsigned short cmdw;
2122 static struct snd_device_ops ops = {
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002123 .dev_free = snd_ali_dev_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 };
2125
2126 *r_ali = NULL;
2127
2128 snd_ali_printk("creating ...\n");
2129
2130 /* enable PCI device */
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002131 err = pci_enable_device(pci);
2132 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return err;
2134 /* check, if we can restrict PCI DMA transfers to 31 bits */
Yang Hongyang929a22a2009-04-06 19:01:16 -07002135 if (pci_set_dma_mask(pci, DMA_BIT_MASK(31)) < 0 ||
2136 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(31)) < 0) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002137 snd_printk(KERN_ERR "architecture does not support "
2138 "31bit PCI busmaster DMA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 pci_disable_device(pci);
2140 return -ENXIO;
2141 }
2142
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002143 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
2144 if (!codec) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 pci_disable_device(pci);
2146 return -ENOMEM;
2147 }
2148
2149 spin_lock_init(&codec->reg_lock);
2150 spin_lock_init(&codec->voice_alloc);
2151
2152 codec->card = card;
2153 codec->pci = pci;
2154 codec->irq = -1;
Auke Kok44c10132007-06-08 15:46:36 -07002155 codec->revision = pci->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 codec->spdif_support = spdif_support;
2157
2158 if (pcm_streams < 1)
2159 pcm_streams = 1;
2160 if (pcm_streams > 32)
2161 pcm_streams = 32;
2162
2163 pci_set_master(pci);
2164 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2165 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2166 cmdw |= PCI_COMMAND_IO;
2167 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2168 }
2169 pci_set_master(pci);
2170
2171 if (snd_ali_resources(codec)) {
2172 snd_ali_free(codec);
2173 return -EBUSY;
2174 }
2175
2176 synchronize_irq(pci->irq);
2177
2178 codec->synth.chmap = 0;
2179 codec->synth.chcnt = 0;
2180 codec->spdif_mask = 0;
2181 codec->synth.synthcount = 0;
2182
2183 if (codec->revision == ALI_5451_V02)
2184 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2185 else
2186 codec->chregs.regs.ac97read = ALI_AC97_READ;
2187 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2188
2189 codec->chregs.regs.start = ALI_START;
2190 codec->chregs.regs.stop = ALI_STOP;
2191 codec->chregs.regs.aint = ALI_AINT;
2192 codec->chregs.regs.ainten = ALI_AINTEN;
2193
2194 codec->chregs.data.start = 0x00;
2195 codec->chregs.data.stop = 0x00;
2196 codec->chregs.data.aint = 0x00;
2197 codec->chregs.data.ainten = 0x00;
2198
2199 /* M1533: southbridge */
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002200 codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002201 if (!codec->pci_m1533) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2203 snd_ali_free(codec);
2204 return -ENODEV;
2205 }
2206 /* M7101: power management */
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002207 codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002208 if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2210 snd_ali_free(codec);
2211 return -ENODEV;
2212 }
2213
2214 snd_ali_printk("snd_device_new is called.\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002215 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops);
2216 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 snd_ali_free(codec);
2218 return err;
2219 }
2220
2221 /* initialise synth voices*/
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002222 for (i = 0; i < ALI_CHANNELS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 codec->synth.voices[i].number = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002225 err = snd_ali_chip_init(codec);
2226 if (err < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002227 snd_printk(KERN_ERR "ali create: chip init error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return err;
2229 }
2230
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002231#ifdef CONFIG_PM_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002233 if (!codec->image)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 snd_printk(KERN_WARNING "can't allocate apm buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235#endif
2236
2237 snd_ali_enable_address_interrupt(codec);
2238 codec->hw_initialized = 1;
2239
2240 *r_ali = codec;
2241 snd_ali_printk("created.\n");
2242 return 0;
2243}
2244
Bill Pembertone23e7a12012-12-06 12:35:10 -05002245static int snd_ali_probe(struct pci_dev *pci,
2246 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002248 struct snd_card *card;
2249 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 int err;
2251
2252 snd_ali_printk("probe ...\n");
2253
Takashi Iwai60c57722014-01-29 14:20:19 +01002254 err = snd_card_new(&pci->dev, index, id, THIS_MODULE, 0, &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002255 if (err < 0)
2256 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002258 err = snd_ali_create(card, pci, pcm_channels, spdif, &codec);
2259 if (err < 0)
2260 goto error;
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002261 card->private_data = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263 snd_ali_printk("mixer building ...\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002264 err = snd_ali_mixer(codec);
2265 if (err < 0)
2266 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
2268 snd_ali_printk("pcm building ...\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002269 err = snd_ali_build_pcms(codec);
2270 if (err < 0)
2271 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002273 snd_ali_proc_init(codec);
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 strcpy(card->driver, "ALI5451");
2276 strcpy(card->shortname, "ALI 5451");
2277
Takashi Iwaiba8c3c32007-05-30 12:42:31 +02002278 sprintf(card->longname, "%s at 0x%lx, irq %i",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 card->shortname, codec->port, codec->irq);
2280
2281 snd_ali_printk("register card.\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002282 err = snd_card_register(card);
2283 if (err < 0)
2284 goto error;
2285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 pci_set_drvdata(pci, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return 0;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002288
2289 error:
2290 snd_card_free(card);
2291 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
Bill Pembertone23e7a12012-12-06 12:35:10 -05002294static void snd_ali_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
2296 snd_card_free(pci_get_drvdata(pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
2298
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002299static struct pci_driver ali5451_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002300 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 .id_table = snd_ali_ids,
2302 .probe = snd_ali_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05002303 .remove = snd_ali_remove,
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002304 .driver = {
2305 .pm = ALI_PM_OPS,
2306 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307};
2308
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002309module_pci_driver(ali5451_driver);