blob: 36470af7eda70689619388f6b2e774cf850492cd [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
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010028#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * Constants definition
69 */
70
Takashi Iwai8cdfd252005-09-07 14:08:11 +020071#define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73
74#define ALI_CHANNELS 32
75
76#define ALI_PCM_IN_CHANNEL 31
77#define ALI_SPDIF_IN_CHANNEL 19
78#define ALI_SPDIF_OUT_CHANNEL 15
79#define ALI_CENTER_CHANNEL 24
80#define ALI_LEF_CHANNEL 23
81#define ALI_SURR_LEFT_CHANNEL 26
82#define ALI_SURR_RIGHT_CHANNEL 25
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +020083#define ALI_MODEM_IN_CHANNEL 21
84#define ALI_MODEM_OUT_CHANNEL 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#define SNDRV_ALI_VOICE_TYPE_PCM 01
87#define SNDRV_ALI_VOICE_TYPE_OTH 02
88
89#define ALI_5451_V02 0x02
90
91/*
92 * Direct Registers
93 */
94
Takashi Iwaif9ab2b12007-04-03 13:20:49 +020095#define ALI_LEGACY_DMAR0 0x00 /* ADR0 */
96#define ALI_LEGACY_DMAR4 0x04 /* CNT0 */
97#define ALI_LEGACY_DMAR11 0x0b /* MOD */
98#define ALI_LEGACY_DMAR15 0x0f /* MMR */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#define ALI_MPUR0 0x20
100#define ALI_MPUR1 0x21
101#define ALI_MPUR2 0x22
102#define ALI_MPUR3 0x23
103
104#define ALI_AC97_WRITE 0x40
105#define ALI_AC97_READ 0x44
106
107#define ALI_SCTRL 0x48
108#define ALI_SPDIF_OUT_ENABLE 0x20
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200109#define ALI_SCTRL_LINE_IN2 (1 << 9)
110#define ALI_SCTRL_GPIO_IN2 (1 << 13)
111#define ALI_SCTRL_LINE_OUT_EN (1 << 20)
112#define ALI_SCTRL_GPIO_OUT_EN (1 << 23)
113#define ALI_SCTRL_CODEC1_READY (1 << 24)
114#define ALI_SCTRL_CODEC2_READY (1 << 25)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define ALI_AC97_GPIO 0x4c
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200116#define ALI_AC97_GPIO_ENABLE 0x8000
117#define ALI_AC97_GPIO_DATA_SHIFT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#define ALI_SPDIF_CS 0x70
119#define ALI_SPDIF_CTRL 0x74
120#define ALI_SPDIF_IN_FUNC_ENABLE 0x02
121#define ALI_SPDIF_IN_CH_STATUS 0x40
122#define ALI_SPDIF_OUT_CH_STATUS 0xbf
123#define ALI_START 0x80
124#define ALI_STOP 0x84
125#define ALI_CSPF 0x90
126#define ALI_AINT 0x98
127#define ALI_GC_CIR 0xa0
128 #define ENDLP_IE 0x00001000
129 #define MIDLP_IE 0x00002000
130#define ALI_AINTEN 0xa4
131#define ALI_VOLUME 0xa8
132#define ALI_SBDELTA_DELTA_R 0xac
133#define ALI_MISCINT 0xb0
134 #define ADDRESS_IRQ 0x00000020
135 #define TARGET_REACHED 0x00008000
136 #define MIXER_OVERFLOW 0x00000800
137 #define MIXER_UNDERFLOW 0x00000400
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200138 #define GPIO_IRQ 0x01000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define ALI_SBBL_SBCL 0xc0
140#define ALI_SBCTRL_SBE2R_SBDD 0xc4
141#define ALI_STIMER 0xc8
142#define ALI_GLOBAL_CONTROL 0xd4
143#define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
144#define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
145#define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
146#define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
147#define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
148
149#define ALI_CSO_ALPHA_FMS 0xe0
150#define ALI_LBA 0xe4
151#define ALI_ESO_DELTA 0xe8
152#define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
153#define ALI_EBUF1 0xf4
154#define ALI_EBUF2 0xf8
155
156#define ALI_REG(codec, x) ((codec)->port + x)
157
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200158#define MAX_CODECS 2
159
160
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100161struct snd_ali;
162struct snd_ali_voice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100164struct snd_ali_channel_control {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200165 /* register data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct REGDATA {
167 unsigned int start;
168 unsigned int stop;
169 unsigned int aint;
170 unsigned int ainten;
171 } data;
172
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200173 /* register addresses */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 struct REGS {
175 unsigned int start;
176 unsigned int stop;
177 unsigned int aint;
178 unsigned int ainten;
179 unsigned int ac97read;
180 unsigned int ac97write;
181 } regs;
182
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100183};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100185struct snd_ali_voice {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 unsigned int number;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200187 unsigned int use :1,
188 pcm :1,
189 midi :1,
190 mode :1,
191 synth :1,
192 running :1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 /* PCM data */
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100195 struct snd_ali *codec;
196 struct snd_pcm_substream *substream;
197 struct snd_ali_voice *extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 int eso; /* final ESO value for channel */
200 int count; /* runtime->period_size */
201
202 /* --- */
203
204 void *private_data;
205 void (*private_free)(void *private_data);
206};
207
208
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100209struct snd_alidev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100211 struct snd_ali_voice voices[ALI_CHANNELS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 unsigned int chcnt; /* num of opened channels */
214 unsigned int chmap; /* bitmap for opened channels */
215 unsigned int synthcount;
216
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100217};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#define ALI_GLOBAL_REGS 56
221#define ALI_CHANNEL_REGS 8
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100222struct snd_ali_image {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200223 u32 regs[ALI_GLOBAL_REGS];
224 u32 channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100225};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100228struct snd_ali {
Takashi Iwaiba8c3c32007-05-30 12:42:31 +0200229 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 unsigned long port;
231 unsigned char revision;
232
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200233 unsigned int hw_initialized :1;
234 unsigned int spdif_support :1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 struct pci_dev *pci;
237 struct pci_dev *pci_m1533;
238 struct pci_dev *pci_m7101;
239
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100240 struct snd_card *card;
241 struct snd_pcm *pcm[MAX_CODECS];
242 struct snd_alidev synth;
243 struct snd_ali_channel_control chregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 /* S/PDIF Mask */
246 unsigned int spdif_mask;
247
248 unsigned int spurious_irq_count;
249 unsigned int spurious_irq_max_delta;
250
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200251 unsigned int num_of_codecs;
252
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100253 struct snd_ac97_bus *ac97_bus;
254 struct snd_ac97 *ac97[MAX_CODECS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 unsigned short ac97_ext_id;
256 unsigned short ac97_ext_status;
257
258 spinlock_t reg_lock;
259 spinlock_t voice_alloc;
260
Takashi Iwaic7561cd2012-08-14 18:12:04 +0200261#ifdef CONFIG_PM_SLEEP
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100262 struct snd_ali_image *image;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263#endif
264};
265
Benoit Taine9baa3c32014-08-08 15:56:03 +0200266static const struct pci_device_id snd_ali_ids[] = {
Jon Masonec6c8c32006-01-13 13:17:43 +0100267 {PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 {0, }
269};
270MODULE_DEVICE_TABLE(pci, snd_ali_ids);
271
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100272static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
273static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200274static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short,
275 unsigned short);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277/*
278 * AC97 ACCESS
279 */
280
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100281static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200282 unsigned int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 return (unsigned int)inl(ALI_REG(codec, port));
285}
286
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200287static inline void snd_ali_5451_poke(struct snd_ali *codec,
288 unsigned int port,
289 unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 outl((unsigned int)val, ALI_REG(codec, port));
292}
293
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200294static int snd_ali_codec_ready(struct snd_ali *codec,
295 unsigned int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 unsigned long end_time;
298 unsigned int res;
299
Takashi Iwai755e1372005-11-11 21:05:27 +0100300 end_time = jiffies + msecs_to_jiffies(250);
Bartlomiej Zolnierkiewicz70bdbd32009-08-23 15:27:25 +0200301
302 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 res = snd_ali_5451_peek(codec,port);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200304 if (!(res & 0x8000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return 0;
Bartlomiej Zolnierkiewicz70bdbd32009-08-23 15:27:25 +0200306 if (!time_after_eq(end_time, jiffies))
307 break;
Takashi Iwai755e1372005-11-11 21:05:27 +0100308 schedule_timeout_uninterruptible(1);
Bartlomiej Zolnierkiewicz70bdbd32009-08-23 15:27:25 +0200309 }
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 snd_ali_5451_poke(codec, port, res & ~0x8000);
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100312 dev_dbg(codec->card->dev, "ali_codec_ready: codec is not ready.\n ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return -EIO;
314}
315
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100316static int snd_ali_stimer_ready(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 unsigned long end_time;
319 unsigned long dwChk1,dwChk2;
320
321 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
Takashi Iwai755e1372005-11-11 21:05:27 +0100322 end_time = jiffies + msecs_to_jiffies(250);
Bartlomiej Zolnierkiewicz70bdbd32009-08-23 15:27:25 +0200323
324 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
326 if (dwChk2 != dwChk1)
327 return 0;
Bartlomiej Zolnierkiewicz70bdbd32009-08-23 15:27:25 +0200328 if (!time_after_eq(end_time, jiffies))
329 break;
Takashi Iwai755e1372005-11-11 21:05:27 +0100330 schedule_timeout_uninterruptible(1);
Bartlomiej Zolnierkiewicz70bdbd32009-08-23 15:27:25 +0200331 }
332
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100333 dev_err(codec->card->dev, "ali_stimer_read: stimer is not ready.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return -EIO;
335}
336
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100337static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200338 unsigned short reg,
339 unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200341 unsigned int dwVal;
342 unsigned int port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (reg >= 0x80) {
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100345 dev_err(codec->card->dev,
346 "ali_codec_poke: reg(%xh) invalid.\n", reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return;
348 }
349
350 port = codec->chregs.regs.ac97write;
351
Takashi Iwai755e1372005-11-11 21:05:27 +0100352 if (snd_ali_codec_ready(codec, port) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return;
Takashi Iwai755e1372005-11-11 21:05:27 +0100354 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return;
356
357 dwVal = (unsigned int) (reg & 0xff);
358 dwVal |= 0x8000 | (val << 16);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200359 if (secondary)
360 dwVal |= 0x0080;
361 if (codec->revision == ALI_5451_V02)
362 dwVal |= 0x0100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200364 snd_ali_5451_poke(codec, port, dwVal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 return ;
367}
368
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200369static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
370 int secondary,
371 unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200373 unsigned int dwVal;
374 unsigned int port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 if (reg >= 0x80) {
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100377 dev_err(codec->card->dev,
378 "ali_codec_peek: reg(%xh) invalid.\n", reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return ~0;
380 }
381
382 port = codec->chregs.regs.ac97read;
383
Takashi Iwai755e1372005-11-11 21:05:27 +0100384 if (snd_ali_codec_ready(codec, port) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return ~0;
Takashi Iwai755e1372005-11-11 21:05:27 +0100386 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return ~0;
388
389 dwVal = (unsigned int) (reg & 0xff);
390 dwVal |= 0x8000; /* bit 15*/
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200391 if (secondary)
392 dwVal |= 0x0080;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 snd_ali_5451_poke(codec, port, dwVal);
395
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;
Takashi Iwai755e1372005-11-11 21:05:27 +0100398 if (snd_ali_codec_ready(codec, port) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return ~0;
400
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200401 return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100404static void snd_ali_codec_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 unsigned short reg,
406 unsigned short val )
407{
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100408 struct snd_ali *codec = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100410 dev_dbg(codec->card->dev, "codec_write: reg=%xh data=%xh.\n", reg, val);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200411 if (reg == AC97_GPIO_STATUS) {
412 outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
413 ALI_REG(codec, ALI_AC97_GPIO));
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200414 return;
415 }
416 snd_ali_codec_poke(codec, ac97->num, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return ;
418}
419
420
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200421static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
422 unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100424 struct snd_ali *codec = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100426 dev_dbg(codec->card->dev, "codec_read reg=%xh.\n", reg);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200427 return snd_ali_codec_peek(codec, ac97->num, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430/*
431 * AC97 Reset
432 */
433
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100434static int snd_ali_reset_5451(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200436 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 unsigned short wCount, wReg;
438 unsigned int dwVal;
439
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200440 pci_dev = codec->pci_m1533;
441 if (pci_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
443 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
Arnd Bergmann76039352013-04-25 19:28:49 +0200444 mdelay(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
446 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
Arnd Bergmann76039352013-04-25 19:28:49 +0200447 mdelay(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
450 pci_dev = codec->pci;
451 pci_read_config_dword(pci_dev, 0x44, &dwVal);
452 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
453 udelay(500);
454 pci_read_config_dword(pci_dev, 0x44, &dwVal);
455 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
Arnd Bergmann76039352013-04-25 19:28:49 +0200456 mdelay(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 wCount = 200;
459 while(wCount--) {
460 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200461 if ((wReg & 0x000f) == 0x000f)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return 0;
Arnd Bergmann76039352013-04-25 19:28:49 +0200463 mdelay(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
466 /* non-fatal if you have a non PM capable codec */
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100467 /* dev_warn(codec->card->dev, "ali5451: reset time out\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return 0;
469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471/*
472 * ALI 5451 Controller
473 */
474
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200475static void snd_ali_enable_special_channel(struct snd_ali *codec,
476 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200478 unsigned long dwVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200480 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 dwVal |= 1 << (channel & 0x0000001f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200482 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200485static void snd_ali_disable_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_enable_address_interrupt(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 unsigned int gc;
498
499 gc = inl(ALI_REG(codec, ALI_GC_CIR));
500 gc |= ENDLP_IE;
501 gc |= MIDLP_IE;
502 outl( gc, ALI_REG(codec, ALI_GC_CIR));
503}
504
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200505static void snd_ali_disable_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_voice_irq(struct snd_ali *codec,
516 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 unsigned int mask;
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100519 struct snd_ali_channel_control *pchregs = &(codec->chregs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100521 dev_dbg(codec->card->dev, "disable_voice_irq channel=%d\n", channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 mask = 1 << (channel & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200524 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 pchregs->data.ainten &= ~mask;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200526 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100529static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 unsigned int idx = channel & 0x1f;
532
533 if (codec->synth.chcnt >= ALI_CHANNELS){
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100534 dev_err(codec->card->dev,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200535 "ali_alloc_pcm_channel: no free channels.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return -1;
537 }
538
539 if (!(codec->synth.chmap & (1 << idx))) {
540 codec->synth.chmap |= 1 << idx;
541 codec->synth.chcnt++;
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100542 dev_dbg(codec->card->dev, "alloc_pcm_channel no. %d.\n", idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return idx;
544 }
545 return -1;
546}
547
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100548static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 int idx;
551 int result = -1;
552
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100553 dev_dbg(codec->card->dev,
554 "find_free_channel: for %s\n", rec ? "rec" : "pcm");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200556 /* recording */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (rec) {
558 if (codec->spdif_support &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200559 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
560 ALI_SPDIF_IN_SUPPORT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 idx = ALI_SPDIF_IN_CHANNEL;
562 else
563 idx = ALI_PCM_IN_CHANNEL;
564
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200565 result = snd_ali_alloc_pcm_channel(codec, idx);
566 if (result >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return result;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200568 else {
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100569 dev_err(codec->card->dev,
570 "ali_find_free_channel: record channel is busy now.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return -1;
572 }
573 }
574
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200575 /* playback... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (codec->spdif_support &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200577 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
578 ALI_SPDIF_OUT_CH_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 idx = ALI_SPDIF_OUT_CHANNEL;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200580 result = snd_ali_alloc_pcm_channel(codec, idx);
581 if (result >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return result;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200583 else
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100584 dev_err(codec->card->dev,
585 "ali_find_free_channel: S/PDIF out channel is in busy now.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
588 for (idx = 0; idx < ALI_CHANNELS; idx++) {
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;
592 }
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100593 dev_err(codec->card->dev, "ali_find_free_channel: no free channels.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return -1;
595}
596
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100597static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 unsigned int idx = channel & 0x0000001f;
600
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100601 dev_dbg(codec->card->dev, "free_channel_pcm channel=%d\n", channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (channel < 0 || channel >= ALI_CHANNELS)
604 return;
605
606 if (!(codec->synth.chmap & (1 << idx))) {
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100607 dev_err(codec->card->dev,
608 "ali_free_channel_pcm: channel %d is not in use.\n",
609 channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return;
611 } else {
612 codec->synth.chmap &= ~(1 << idx);
613 codec->synth.chcnt--;
614 }
615}
616
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200617static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 unsigned int mask = 1 << (channel & 0x1f);
620
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100621 dev_dbg(codec->card->dev, "stop_voice: channel=%d\n", channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
623}
624
625/*
626 * S/PDIF Part
627 */
628
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100629static void snd_ali_delay(struct snd_ali *codec,int interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 unsigned long begintimer,currenttimer;
632
633 begintimer = inl(ALI_REG(codec, ALI_STIMER));
634 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
635
636 while (currenttimer < begintimer + interval) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200637 if (snd_ali_stimer_ready(codec) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 break;
639 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200640 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642}
643
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100644static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200646 u16 wval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 u16 count = 0;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200648 u8 bval, R1 = 0, R2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200650 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 bval |= 0x1F;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200652 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200654 while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 count ++;
656 snd_ali_delay(codec, 6);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200657 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 R1 = bval & 0x1F;
659 }
660
661 if (count > 50000) {
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100662 dev_err(codec->card->dev, "ali_detect_spdif_rate: timeout!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return;
664 }
665
Andrew Morton1c397322007-06-11 12:23:31 +0200666 for (count = 0; count <= 50000; count++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 snd_ali_delay(codec, 6);
668 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
669 R2 = bval & 0x1F;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200670 if (R2 != R1)
671 R1 = R2;
672 else
673 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675
676 if (count > 50000) {
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100677 dev_err(codec->card->dev, "ali_detect_spdif_rate: timeout!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return;
679 }
680
681 if (R2 >= 0x0b && R2 <= 0x0e) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200682 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
683 wval &= 0xe0f0;
684 wval |= (0x09 << 8) | 0x05;
685 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200687 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
688 outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 } else if (R2 == 0x12) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200690 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
691 wval &= 0xe0f0;
692 wval |= (0x0e << 8) | 0x08;
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 | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698}
699
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100700static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200702 u32 dwRate;
703 u8 bval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200705 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
706 bval &= 0x7f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 bval |= 0x40;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200708 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 snd_ali_detect_spdif_rate(codec);
711
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200712 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
713 bval &= 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200715 switch (bval) {
716 case 0: dwRate = 44100; break;
717 case 1: dwRate = 48000; break;
718 case 2: dwRate = 32000; break;
719 default: dwRate = 0; break;
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 return dwRate;
723}
724
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100725static void snd_ali_enable_spdif_in(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 unsigned int dwVal;
728
729 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
730 dwVal |= ALI_SPDIF_IN_SUPPORT;
731 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
732
733 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
734 dwVal |= 0x02;
735 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
736
737 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
738}
739
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100740static void snd_ali_disable_spdif_in(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 unsigned int dwVal;
743
744 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
745 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
746 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
747
748 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
749}
750
751
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100752static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 unsigned char bVal;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200755 unsigned int dwRate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200757 switch (rate) {
758 case 32000: dwRate = 0x300; break;
759 case 48000: dwRate = 0x200; break;
760 default: dwRate = 0; break;
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
764 bVal &= (unsigned char)(~(1<<6));
765
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200766 bVal |= 0x80; /* select right */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
768 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
769
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200770 bVal &= ~0x80; /* select left */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
772 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
773}
774
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100775static void snd_ali_enable_spdif_out(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 unsigned short wVal;
778 unsigned char bVal;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200779 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 pci_dev = codec->pci_m1533;
782 if (pci_dev == NULL)
783 return;
784 pci_read_config_byte(pci_dev, 0x61, &bVal);
785 bVal |= 0x40;
786 pci_write_config_byte(pci_dev, 0x61, bVal);
787 pci_read_config_byte(pci_dev, 0x7d, &bVal);
788 bVal |= 0x01;
789 pci_write_config_byte(pci_dev, 0x7d, bVal);
790
791 pci_read_config_byte(pci_dev, 0x7e, &bVal);
792 bVal &= (~0x20);
793 bVal |= 0x10;
794 pci_write_config_byte(pci_dev, 0x7e, bVal);
795
796 bVal = inb(ALI_REG(codec, ALI_SCTRL));
797 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
798
799 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
800 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
801
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200802 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
803 wVal |= ALI_SPDIF_OUT_SEL_PCM;
804 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
805 snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100808static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200810 unsigned short wVal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
813 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
814 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
815/*
816 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
817 if (flag & ALI_SPDIF_OUT_NON_PCM)
818 wVal |= 0x0002;
819 else
820 wVal &= (~0x0002);
821 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
822*/
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200823 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100826static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200828 unsigned short wVal;
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
831 wVal |= ALI_SPDIF_OUT_SEL_PCM;
832 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
833
834 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
835}
836
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100837static void snd_ali_disable_spdif_out(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 unsigned char bVal;
840
841 bVal = inb(ALI_REG(codec, ALI_SCTRL));
842 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
843
844 snd_ali_disable_spdif_chnout(codec);
845}
846
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200847static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200849 struct snd_ali_voice *pvoice;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200850 struct snd_ali_channel_control *pchregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 unsigned int old, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 pchregs = &(codec->chregs);
854
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200855 /* check if interrupt occurred for channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 old = pchregs->data.aint;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200857 mask = 1U << (channel & 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 if (!(old & mask))
860 return;
861
862 pvoice = &codec->synth.voices[channel];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 udelay(100);
865 spin_lock(&codec->reg_lock);
866
867 if (pvoice->pcm && pvoice->substream) {
868 /* pcm interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (pvoice->running) {
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100870 dev_dbg(codec->card->dev,
871 "update_ptr: cso=%4.4x cspf=%d.\n",
872 inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2)),
873 (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 spin_unlock(&codec->reg_lock);
875 snd_pcm_period_elapsed(pvoice->substream);
876 spin_lock(&codec->reg_lock);
877 } else {
878 snd_ali_stop_voice(codec, channel);
879 snd_ali_disable_voice_irq(codec, channel);
880 }
881 } else if (codec->synth.voices[channel].synth) {
882 /* synth interrupt */
883 } else if (codec->synth.voices[channel].midi) {
884 /* midi interrupt */
885 } else {
886 /* unknown interrupt */
887 snd_ali_stop_voice(codec, channel);
888 snd_ali_disable_voice_irq(codec, channel);
889 }
890 spin_unlock(&codec->reg_lock);
891 outl(mask,ALI_REG(codec,pchregs->regs.aint));
892 pchregs->data.aint = old & (~mask);
893}
894
David Howells7d12e782006-10-05 14:55:46 +0100895static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Takashi Iwaid1fabd92005-11-17 14:56:03 +0100897 struct snd_ali *codec = dev_id;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200898 int channel;
899 unsigned int audio_int;
900 struct snd_ali_channel_control *pchregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200902 if (codec == NULL || !codec->hw_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return IRQ_NONE;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200904
905 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
906 if (!audio_int)
907 return IRQ_NONE;
908
909 pchregs = &(codec->chregs);
910 if (audio_int & ADDRESS_IRQ) {
911 /* get interrupt status for all channels */
912 pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
913 for (channel = 0; channel < ALI_CHANNELS; channel++)
914 snd_ali_update_ptr(codec, channel);
915 }
916 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
917 ALI_REG(codec, ALI_MISCINT));
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return IRQ_HANDLED;
920}
921
922
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200923static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
924 int type, int rec, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200926 struct snd_ali_voice *pvoice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int idx;
928
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100929 dev_dbg(codec->card->dev, "alloc_voice: type=%d rec=%d\n", type, rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Takashi Iwai755e1372005-11-11 21:05:27 +0100931 spin_lock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +0200933 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
934 snd_ali_find_free_channel(codec,rec);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200935 if (idx < 0) {
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100936 dev_err(codec->card->dev, "ali_alloc_voice: err.\n");
Takashi Iwai755e1372005-11-11 21:05:27 +0100937 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return NULL;
939 }
940 pvoice = &(codec->synth.voices[idx]);
Takashi Iwai755e1372005-11-11 21:05:27 +0100941 pvoice->codec = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 pvoice->use = 1;
943 pvoice->pcm = 1;
944 pvoice->mode = rec;
Takashi Iwai755e1372005-11-11 21:05:27 +0100945 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return pvoice;
947 }
Takashi Iwai755e1372005-11-11 21:05:27 +0100948 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return NULL;
950}
951
952
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200953static void snd_ali_free_voice(struct snd_ali * codec,
954 struct snd_ali_voice *pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 void (*private_free)(void *);
957 void *private_data;
958
Takashi Iwai5f1e6932014-02-25 15:48:50 +0100959 dev_dbg(codec->card->dev, "free_voice: channel=%d\n", pvoice->number);
Julia Lawall4b3be6a2009-10-17 08:33:22 +0200960 if (!pvoice->use)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return;
962 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
Takashi Iwai755e1372005-11-11 21:05:27 +0100963 spin_lock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 private_free = pvoice->private_free;
965 private_data = pvoice->private_data;
966 pvoice->private_free = NULL;
967 pvoice->private_data = NULL;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200968 if (pvoice->pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 snd_ali_free_channel_pcm(codec, pvoice->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 pvoice->use = pvoice->pcm = pvoice->synth = 0;
971 pvoice->substream = NULL;
Takashi Iwai755e1372005-11-11 21:05:27 +0100972 spin_unlock_irq(&codec->voice_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (private_free)
974 private_free(private_data);
975}
976
977
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200978static void snd_ali_clear_voices(struct snd_ali *codec,
979 unsigned int v_min,
980 unsigned int v_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 unsigned int i;
983
984 for (i = v_min; i <= v_max; i++) {
985 snd_ali_stop_voice(codec, i);
986 snd_ali_disable_voice_irq(codec, i);
987 }
988}
989
Takashi Iwaif9ab2b12007-04-03 13:20:49 +0200990static void snd_ali_write_voice_regs(struct snd_ali *codec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 unsigned int Channel,
992 unsigned int LBA,
993 unsigned int CSO,
994 unsigned int ESO,
995 unsigned int DELTA,
996 unsigned int ALPHA_FMS,
997 unsigned int GVSEL,
998 unsigned int PAN,
999 unsigned int VOL,
1000 unsigned int CTRL,
1001 unsigned int EC)
1002{
1003 unsigned int ctlcmds[4];
1004
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001005 outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1008 ctlcmds[1] = LBA;
1009 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
1010 ctlcmds[3] = (GVSEL << 31) |
1011 ((PAN & 0x0000007f) << 24) |
1012 ((VOL & 0x000000ff) << 16) |
1013 ((CTRL & 0x0000000f) << 12) |
1014 (EC & 0x00000fff);
1015
1016 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1017
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001018 outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
1019 outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
1020 outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
1021 outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1024 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1025}
1026
1027static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1028{
1029 unsigned int delta;
1030
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001031 if (rate < 4000)
1032 rate = 4000;
1033 if (rate > 48000)
1034 rate = 48000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 if (rec) {
1037 if (rate == 44100)
1038 delta = 0x116a;
1039 else if (rate == 8000)
1040 delta = 0x6000;
1041 else if (rate == 48000)
1042 delta = 0x1000;
1043 else
1044 delta = ((48000 << 12) / rate) & 0x0000ffff;
1045 } else {
1046 if (rate == 44100)
1047 delta = 0xeb3;
1048 else if (rate == 8000)
1049 delta = 0x2ab;
1050 else if (rate == 48000)
1051 delta = 0x1000;
1052 else
1053 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1054 }
1055
1056 return delta;
1057}
1058
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001059static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 unsigned int CTRL;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001062 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 /* set ctrl mode
1065 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1066 */
1067 CTRL = 0x00000001;
1068 if (snd_pcm_format_width(runtime->format) == 16)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001069 CTRL |= 0x00000008; /* 16-bit data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (!snd_pcm_format_unsigned(runtime->format))
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001071 CTRL |= 0x00000002; /* signed data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (runtime->channels > 1)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001073 CTRL |= 0x00000004; /* stereo data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return CTRL;
1075}
1076
1077/*
1078 * PCM part
1079 */
1080
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001081static int snd_ali_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 int cmd)
1083
1084{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001085 struct snd_ali *codec = snd_pcm_substream_chip(substream);
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001086 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 unsigned int what, whati, capture_flag;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001088 struct snd_ali_voice *pvoice, *evoice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 unsigned int val;
1090 int do_start;
1091
1092 switch (cmd) {
1093 case SNDRV_PCM_TRIGGER_START:
1094 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001095 do_start = 1;
1096 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 case SNDRV_PCM_TRIGGER_STOP:
1098 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001099 do_start = 0;
1100 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 default:
1102 return -EINVAL;
1103 }
1104
1105 what = whati = capture_flag = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +01001106 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001107 if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
1108 pvoice = s->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 evoice = pvoice->extra;
1110 what |= 1 << (pvoice->number & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001111 if (evoice == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 whati |= 1 << (pvoice->number & 0x1f);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001113 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 whati |= 1 << (evoice->number & 0x1f);
1115 what |= 1 << (evoice->number & 0x1f);
1116 }
1117 if (do_start) {
1118 pvoice->running = 1;
1119 if (evoice != NULL)
1120 evoice->running = 1;
1121 } else {
1122 pvoice->running = 0;
1123 if (evoice != NULL)
1124 evoice->running = 0;
1125 }
1126 snd_pcm_trigger_done(s, substream);
1127 if (pvoice->mode)
1128 capture_flag = 1;
1129 }
1130 }
1131 spin_lock(&codec->reg_lock);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001132 if (!do_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 outl(what, ALI_REG(codec, ALI_STOP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 val = inl(ALI_REG(codec, ALI_AINTEN));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001135 if (do_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 val |= whati;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001137 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 val &= ~whati;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 outl(val, ALI_REG(codec, ALI_AINTEN));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001140 if (do_start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 outl(what, ALI_REG(codec, ALI_START));
Takashi Iwai5f1e6932014-02-25 15:48:50 +01001142 dev_dbg(codec->card->dev, "trigger: what=%xh whati=%xh\n", what, whati);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 spin_unlock(&codec->reg_lock);
1144
1145 return 0;
1146}
1147
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001148static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001149 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001151 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1152 struct snd_pcm_runtime *runtime = substream->runtime;
1153 struct snd_ali_voice *pvoice = runtime->private_data;
1154 struct snd_ali_voice *evoice = pvoice->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 int err;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001156
1157 err = snd_pcm_lib_malloc_pages(substream,
1158 params_buffer_bytes(hw_params));
1159 if (err < 0)
1160 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /* voice management */
1163
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001164 if (params_buffer_size(hw_params) / 2 !=
1165 params_period_size(hw_params)) {
1166 if (!evoice) {
1167 evoice = snd_ali_alloc_voice(codec,
1168 SNDRV_ALI_VOICE_TYPE_PCM,
1169 0, -1);
1170 if (!evoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return -ENOMEM;
1172 pvoice->extra = evoice;
1173 evoice->substream = substream;
1174 }
1175 } else {
Takashi Iwai27043642007-05-19 16:30:35 +02001176 if (evoice) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 snd_ali_free_voice(codec, evoice);
1178 pvoice->extra = evoice = NULL;
1179 }
1180 }
1181
1182 return 0;
1183}
1184
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001185static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001187 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1188 struct snd_pcm_runtime *runtime = substream->runtime;
1189 struct snd_ali_voice *pvoice = runtime->private_data;
1190 struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 snd_pcm_lib_free_pages(substream);
Takashi Iwai27043642007-05-19 16:30:35 +02001193 if (evoice) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 snd_ali_free_voice(codec, evoice);
1195 pvoice->extra = NULL;
1196 }
1197 return 0;
1198}
1199
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001200static int snd_ali_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001201 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001203 return snd_pcm_lib_malloc_pages(substream,
1204 params_buffer_bytes(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001207static int snd_ali_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 return snd_pcm_lib_free_pages(substream);
1210}
1211
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001212static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001214 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1215 struct snd_pcm_runtime *runtime = substream->runtime;
1216 struct snd_ali_voice *pvoice = runtime->private_data;
1217 struct snd_ali_voice *evoice = pvoice->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 unsigned int LBA;
1220 unsigned int Delta;
1221 unsigned int ESO;
1222 unsigned int CTRL;
1223 unsigned int GVSEL;
1224 unsigned int PAN;
1225 unsigned int VOL;
1226 unsigned int EC;
1227
Takashi Iwai5f1e6932014-02-25 15:48:50 +01001228 dev_dbg(codec->card->dev, "playback_prepare ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Takashi Iwai755e1372005-11-11 21:05:27 +01001230 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 /* set Delta (rate) value */
1233 Delta = snd_ali_convert_rate(runtime->rate, 0);
1234
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001235 if (pvoice->number == ALI_SPDIF_IN_CHANNEL ||
1236 pvoice->number == ALI_PCM_IN_CHANNEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 snd_ali_disable_special_channel(codec, pvoice->number);
1238 else if (codec->spdif_support &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001239 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
1240 ALI_SPDIF_OUT_CH_ENABLE)
1241 && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1243 Delta = 0x1000;
1244 }
1245
1246 /* set Loop Back Address */
1247 LBA = runtime->dma_addr;
1248
1249 /* set interrupt count size */
1250 pvoice->count = runtime->period_size;
1251
1252 /* set target ESO for channel */
1253 pvoice->eso = runtime->buffer_size;
1254
Takashi Iwai5f1e6932014-02-25 15:48:50 +01001255 dev_dbg(codec->card->dev, "playback_prepare: eso=%xh count=%xh\n",
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001256 pvoice->eso, pvoice->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 /* set ESO to capture first MIDLP interrupt */
1259 ESO = pvoice->eso -1;
1260 /* set ctrl mode */
1261 CTRL = snd_ali_control_mode(substream);
1262
1263 GVSEL = 1;
1264 PAN = 0;
1265 VOL = 0;
1266 EC = 0;
Takashi Iwai5f1e6932014-02-25 15:48:50 +01001267 dev_dbg(codec->card->dev, "playback_prepare:\n");
1268 dev_dbg(codec->card->dev,
1269 "ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001270 pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1271 snd_ali_write_voice_regs(codec,
1272 pvoice->number,
1273 LBA,
1274 0, /* cso */
1275 ESO,
1276 Delta,
1277 0, /* alpha */
1278 GVSEL,
1279 PAN,
1280 VOL,
1281 CTRL,
1282 EC);
Takashi Iwai27043642007-05-19 16:30:35 +02001283 if (evoice) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 evoice->count = pvoice->count;
1285 evoice->eso = pvoice->count << 1;
1286 ESO = evoice->eso - 1;
1287 snd_ali_write_voice_regs(codec,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001288 evoice->number,
1289 LBA,
1290 0, /* cso */
1291 ESO,
1292 Delta,
1293 0, /* alpha */
1294 GVSEL,
1295 0x7f,
1296 0x3ff,
1297 CTRL,
1298 EC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001300 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return 0;
1302}
1303
1304
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001305static int snd_ali_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001307 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1308 struct snd_pcm_runtime *runtime = substream->runtime;
1309 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 unsigned int LBA;
1311 unsigned int Delta;
1312 unsigned int ESO;
1313 unsigned int CTRL;
1314 unsigned int GVSEL;
1315 unsigned int PAN;
1316 unsigned int VOL;
1317 unsigned int EC;
1318 u8 bValue;
1319
Takashi Iwai755e1372005-11-11 21:05:27 +01001320 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Takashi Iwai5f1e6932014-02-25 15:48:50 +01001322 dev_dbg(codec->card->dev, "ali_prepare...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 snd_ali_enable_special_channel(codec,pvoice->number);
1325
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001326 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1327 pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
1328 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001330 /* Prepare capture intr channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1332
1333 unsigned int rate;
1334
Takashi Iwai755e1372005-11-11 21:05:27 +01001335 spin_unlock_irq(&codec->reg_lock);
1336 if (codec->revision != ALI_5451_V02)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return -1;
Takashi Iwai755e1372005-11-11 21:05:27 +01001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 rate = snd_ali_get_spdif_in_rate(codec);
1340 if (rate == 0) {
Takashi Iwai5f1e6932014-02-25 15:48:50 +01001341 dev_warn(codec->card->dev,
1342 "ali_capture_preapre: spdif rate detect err!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 rate = 48000;
1344 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001345 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1347 if (bValue & 0x10) {
1348 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
Takashi Iwai5f1e6932014-02-25 15:48:50 +01001349 dev_warn(codec->card->dev,
1350 "clear SPDIF parity error flag.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352
1353 if (rate != 48000)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001354 Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
1356
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001357 /* set target ESO for channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 pvoice->eso = runtime->buffer_size;
1359
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001360 /* set interrupt count size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 pvoice->count = runtime->period_size;
1362
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001363 /* set Loop Back Address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 LBA = runtime->dma_addr;
1365
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001366 /* set ESO to capture first MIDLP interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 ESO = pvoice->eso - 1;
1368 CTRL = snd_ali_control_mode(substream);
1369 GVSEL = 0;
1370 PAN = 0x00;
1371 VOL = 0x00;
1372 EC = 0;
1373
1374 snd_ali_write_voice_regs( codec,
1375 pvoice->number,
1376 LBA,
1377 0, /* cso */
1378 ESO,
1379 Delta,
1380 0, /* alpha */
1381 GVSEL,
1382 PAN,
1383 VOL,
1384 CTRL,
1385 EC);
1386
Takashi Iwai755e1372005-11-11 21:05:27 +01001387 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 return 0;
1390}
1391
1392
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001393static snd_pcm_uframes_t
1394snd_ali_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001396 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1397 struct snd_pcm_runtime *runtime = substream->runtime;
1398 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 unsigned int cso;
1400
1401 spin_lock(&codec->reg_lock);
1402 if (!pvoice->running) {
1403 spin_unlock(&codec->reg_lock);
1404 return 0;
1405 }
1406 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1407 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1408 spin_unlock(&codec->reg_lock);
Takashi Iwai5f1e6932014-02-25 15:48:50 +01001409 dev_dbg(codec->card->dev, "playback pointer returned cso=%xh.\n", cso);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 return cso;
1412}
1413
1414
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001415static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001417 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1418 struct snd_pcm_runtime *runtime = substream->runtime;
1419 struct snd_ali_voice *pvoice = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 unsigned int cso;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Takashi Iwai755e1372005-11-11 21:05:27 +01001422 spin_lock(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (!pvoice->running) {
Denis Efremovdacae5a2013-02-11 19:49:48 +04001424 spin_unlock(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return 0;
1426 }
1427 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1428 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
Takashi Iwai755e1372005-11-11 21:05:27 +01001429 spin_unlock(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 return cso;
1432}
1433
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001434static struct snd_pcm_hardware snd_ali_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001436 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1437 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1438 SNDRV_PCM_INFO_MMAP_VALID |
1439 SNDRV_PCM_INFO_RESUME |
1440 SNDRV_PCM_INFO_SYNC_START),
1441 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1442 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1443 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 .rate_min = 4000,
1445 .rate_max = 48000,
1446 .channels_min = 1,
1447 .channels_max = 2,
1448 .buffer_bytes_max = (256*1024),
1449 .period_bytes_min = 64,
1450 .period_bytes_max = (256*1024),
1451 .periods_min = 1,
1452 .periods_max = 1024,
1453 .fifo_size = 0,
1454};
1455
1456/*
1457 * Capture support device description
1458 */
1459
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001460static struct snd_pcm_hardware snd_ali_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001462 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1463 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1464 SNDRV_PCM_INFO_MMAP_VALID |
1465 SNDRV_PCM_INFO_RESUME |
1466 SNDRV_PCM_INFO_SYNC_START),
1467 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1468 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1469 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 .rate_min = 4000,
1471 .rate_max = 48000,
1472 .channels_min = 1,
1473 .channels_max = 2,
1474 .buffer_bytes_max = (128*1024),
1475 .period_bytes_min = 64,
1476 .period_bytes_max = (128*1024),
1477 .periods_min = 1,
1478 .periods_max = 1024,
1479 .fifo_size = 0,
1480};
1481
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001482static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001484 struct snd_ali_voice *pvoice = runtime->private_data;
1485 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 if (pvoice) {
1488 codec = pvoice->codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 snd_ali_free_voice(pvoice->codec, pvoice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
1491}
1492
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001493static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
1494 int channel, struct snd_pcm_hardware *phw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001496 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1497 struct snd_pcm_runtime *runtime = substream->runtime;
1498 struct snd_ali_voice *pvoice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001500 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
1501 channel);
1502 if (!pvoice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 pvoice->substream = substream;
1506 runtime->private_data = pvoice;
1507 runtime->private_free = snd_ali_pcm_free_substream;
1508
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001509 runtime->hw = *phw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 snd_pcm_set_sync(substream);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001511 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1512 0, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return 0;
1514}
1515
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001516static int snd_ali_playback_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001517{
1518 return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1519}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001521static int snd_ali_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001523 return snd_ali_open(substream, 1, -1, &snd_ali_capture);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001526static int snd_ali_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
1528 return 0;
1529}
1530
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001531static int snd_ali_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001533 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1534 struct snd_ali_voice *pvoice = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 snd_ali_disable_special_channel(codec,pvoice->number);
1537
1538 return 0;
1539}
1540
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001541static struct snd_pcm_ops snd_ali_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 .open = snd_ali_playback_open,
1543 .close = snd_ali_playback_close,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001544 .ioctl = snd_pcm_lib_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 .hw_params = snd_ali_playback_hw_params,
1546 .hw_free = snd_ali_playback_hw_free,
1547 .prepare = snd_ali_playback_prepare,
1548 .trigger = snd_ali_trigger,
1549 .pointer = snd_ali_playback_pointer,
1550};
1551
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001552static struct snd_pcm_ops snd_ali_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 .open = snd_ali_capture_open,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001554 .close = snd_ali_close,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001555 .ioctl = snd_pcm_lib_ioctl,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001556 .hw_params = snd_ali_hw_params,
1557 .hw_free = snd_ali_hw_free,
1558 .prepare = snd_ali_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 .trigger = snd_ali_trigger,
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001560 .pointer = snd_ali_pointer,
1561};
1562
1563/*
1564 * Modem PCM
1565 */
1566
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001567static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
1568 struct snd_pcm_hw_params *hw_params)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001569{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001570 struct snd_ali *chip = snd_pcm_substream_chip(substream);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001571 unsigned int modem_num = chip->num_of_codecs - 1;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001572 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
1573 params_rate(hw_params));
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001574 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1575 return snd_ali_hw_params(substream, hw_params);
1576}
1577
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001578static struct snd_pcm_hardware snd_ali_modem =
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001579{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001580 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1581 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1582 SNDRV_PCM_INFO_MMAP_VALID |
1583 SNDRV_PCM_INFO_RESUME |
1584 SNDRV_PCM_INFO_SYNC_START),
1585 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1586 .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
1587 SNDRV_PCM_RATE_16000),
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001588 .rate_min = 8000,
1589 .rate_max = 16000,
1590 .channels_min = 1,
1591 .channels_max = 1,
1592 .buffer_bytes_max = (256*1024),
1593 .period_bytes_min = 64,
1594 .period_bytes_max = (256*1024),
1595 .periods_min = 1,
1596 .periods_max = 1024,
1597 .fifo_size = 0,
1598};
1599
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001600static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
1601 int channel)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001602{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001603 static unsigned int rates[] = {8000, 9600, 12000, 16000};
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001604 static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001605 .count = ARRAY_SIZE(rates),
1606 .list = rates,
1607 .mask = 0,
1608 };
1609 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001610
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001611 if (err)
1612 return err;
1613 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1614 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1615}
1616
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001617static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001618{
1619 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1620}
1621
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001622static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001623{
1624 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1625}
1626
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001627static struct snd_pcm_ops snd_ali_modem_playback_ops = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001628 .open = snd_ali_modem_playback_open,
1629 .close = snd_ali_close,
1630 .ioctl = snd_pcm_lib_ioctl,
1631 .hw_params = snd_ali_modem_hw_params,
1632 .hw_free = snd_ali_hw_free,
1633 .prepare = snd_ali_prepare,
1634 .trigger = snd_ali_trigger,
1635 .pointer = snd_ali_pointer,
1636};
1637
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001638static struct snd_pcm_ops snd_ali_modem_capture_ops = {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001639 .open = snd_ali_modem_capture_open,
1640 .close = snd_ali_close,
1641 .ioctl = snd_pcm_lib_ioctl,
1642 .hw_params = snd_ali_modem_hw_params,
1643 .hw_free = snd_ali_hw_free,
1644 .prepare = snd_ali_prepare,
1645 .trigger = snd_ali_trigger,
1646 .pointer = snd_ali_pointer,
1647};
1648
1649
1650struct ali_pcm_description {
1651 char *name;
1652 unsigned int playback_num;
1653 unsigned int capture_num;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001654 struct snd_pcm_ops *playback_ops;
1655 struct snd_pcm_ops *capture_ops;
Sasha Khapyorsky6632d192005-09-29 11:48:17 +02001656 unsigned short class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657};
1658
1659
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001660static void snd_ali_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001662 struct snd_ali *codec = pcm->private_data;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001663 codec->pcm[pcm->device] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001666
Bill Pembertone23e7a12012-12-06 12:35:10 -05001667static int snd_ali_pcm(struct snd_ali *codec, int device,
1668 struct ali_pcm_description *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001670 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 int err;
1672
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001673 err = snd_pcm_new(codec->card, desc->name, device,
1674 desc->playback_num, desc->capture_num, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (err < 0) {
Takashi Iwai5f1e6932014-02-25 15:48:50 +01001676 dev_err(codec->card->dev,
1677 "snd_ali_pcm: err called snd_pcm_new.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return err;
1679 }
1680 pcm->private_data = codec;
1681 pcm->private_free = snd_ali_pcm_free;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001682 if (desc->playback_ops)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001683 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1684 desc->playback_ops);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001685 if (desc->capture_ops)
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001686 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1687 desc->capture_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001690 snd_dma_pci_data(codec->pci),
1691 64*1024, 128*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 pcm->info_flags = 0;
Sasha Khapyorsky6632d192005-09-29 11:48:17 +02001694 pcm->dev_class = desc->class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001696 strcpy(pcm->name, desc->name);
1697 codec->pcm[0] = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return 0;
1699}
1700
Clemens Ladischa53fc182005-08-11 15:59:17 +02001701static struct ali_pcm_description ali_pcms[] = {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001702 { .name = "ALI 5451",
1703 .playback_num = ALI_CHANNELS,
1704 .capture_num = 1,
1705 .playback_ops = &snd_ali_playback_ops,
1706 .capture_ops = &snd_ali_capture_ops
1707 },
1708 { .name = "ALI 5451 modem",
1709 .playback_num = 1,
1710 .capture_num = 1,
1711 .playback_ops = &snd_ali_modem_playback_ops,
1712 .capture_ops = &snd_ali_modem_capture_ops,
1713 .class = SNDRV_PCM_CLASS_MODEM
1714 }
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001715};
1716
Bill Pembertone23e7a12012-12-06 12:35:10 -05001717static int snd_ali_build_pcms(struct snd_ali *codec)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001718{
1719 int i, err;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001720 for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
1721 err = snd_ali_pcm(codec, i, &ali_pcms[i]);
1722 if (err < 0)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001723 return err;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001724 }
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001725 return 0;
1726}
1727
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729#define ALI5451_SPDIF(xname, xindex, value) \
1730{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1731.info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1732.put = snd_ali5451_spdif_put, .private_value = value}
1733
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001734#define snd_ali5451_spdif_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001736static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
1737 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001739 struct snd_ali *codec = kcontrol->private_data;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001740 unsigned int spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001742 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Takashi Iwai755e1372005-11-11 21:05:27 +01001744 spin_lock_irq(&codec->reg_lock);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001745 switch (kcontrol->private_value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 case 0:
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001747 spdif_enable = (codec->spdif_mask & 0x02) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 break;
1749 case 1:
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001750 spdif_enable = ((codec->spdif_mask & 0x02) &&
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001751 (codec->spdif_mask & 0x04)) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 break;
1753 case 2:
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001754 spdif_enable = (codec->spdif_mask & 0x01) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 break;
1756 default:
1757 break;
1758 }
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001759 ucontrol->value.integer.value[0] = spdif_enable;
Takashi Iwai755e1372005-11-11 21:05:27 +01001760 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return 0;
1762}
1763
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001764static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
1765 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001767 struct snd_ali *codec = kcontrol->private_data;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001768 unsigned int change = 0, spdif_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001770 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Takashi Iwai755e1372005-11-11 21:05:27 +01001772 spin_lock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 switch (kcontrol->private_value) {
1774 case 0:
1775 change = (codec->spdif_mask & 0x02) ? 1 : 0;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001776 change = change ^ spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (change) {
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001778 if (spdif_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 codec->spdif_mask |= 0x02;
1780 snd_ali_enable_spdif_out(codec);
1781 } else {
1782 codec->spdif_mask &= ~(0x02);
1783 codec->spdif_mask &= ~(0x04);
1784 snd_ali_disable_spdif_out(codec);
1785 }
1786 }
1787 break;
1788 case 1:
1789 change = (codec->spdif_mask & 0x04) ? 1 : 0;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001790 change = change ^ spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (change && (codec->spdif_mask & 0x02)) {
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001792 if (spdif_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 codec->spdif_mask |= 0x04;
1794 snd_ali_enable_spdif_chnout(codec);
1795 } else {
1796 codec->spdif_mask &= ~(0x04);
1797 snd_ali_disable_spdif_chnout(codec);
1798 }
1799 }
1800 break;
1801 case 2:
1802 change = (codec->spdif_mask & 0x01) ? 1 : 0;
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001803 change = change ^ spdif_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (change) {
Harvey Harrisonc74056d2008-02-28 12:00:48 +01001805 if (spdif_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 codec->spdif_mask |= 0x01;
1807 snd_ali_enable_spdif_in(codec);
1808 } else {
1809 codec->spdif_mask &= ~(0x01);
1810 snd_ali_disable_spdif_in(codec);
1811 }
1812 }
1813 break;
1814 default:
1815 break;
1816 }
Takashi Iwai755e1372005-11-11 21:05:27 +01001817 spin_unlock_irq(&codec->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 return change;
1820}
1821
Bill Pembertone23e7a12012-12-06 12:35:10 -05001822static struct snd_kcontrol_new snd_ali5451_mixer_spdif[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 /* spdif aplayback switch */
1824 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001825 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 /* spdif out to spdif channel */
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001827 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /* spdif in from spdif channel */
1829 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1830};
1831
Bill Pembertone23e7a12012-12-06 12:35:10 -05001832static int snd_ali_mixer(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001834 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 unsigned int idx;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001836 int i, err;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001837 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 .write = snd_ali_codec_write,
1839 .read = snd_ali_codec_read,
1840 };
1841
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001842 err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
1843 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 memset(&ac97, 0, sizeof(ac97));
1847 ac97.private_data = codec;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001848
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001849 for (i = 0; i < codec->num_of_codecs; i++) {
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001850 ac97.num = i;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001851 err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
1852 if (err < 0) {
Takashi Iwai5f1e6932014-02-25 15:48:50 +01001853 dev_err(codec->card->dev,
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001854 "ali mixer %d creating error.\n", i);
1855 if (i == 0)
Takashi Iwai4d060fd2005-10-04 13:50:44 +02001856 return err;
1857 codec->num_of_codecs = 1;
1858 break;
1859 }
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001860 }
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (codec->spdif_support) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001863 for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1864 err = snd_ctl_add(codec->card,
1865 snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1866 if (err < 0)
1867 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869 }
1870 return 0;
1871}
1872
Takashi Iwaic7561cd2012-08-14 18:12:04 +02001873#ifdef CONFIG_PM_SLEEP
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001874static int ali_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001876 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001877 struct snd_ali *chip = card->private_data;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001878 struct snd_ali_image *im;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 int i, j;
1880
1881 im = chip->image;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001882 if (!im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return 0;
1884
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001885 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001886 for (i = 0; i < chip->num_of_codecs; i++) {
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001887 snd_pcm_suspend_all(chip->pcm[i]);
1888 snd_ac97_suspend(chip->ac97[i]);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02001889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 spin_lock_irq(&chip->reg_lock);
1892
1893 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001894 /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1896
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001897 /* disable all IRQ bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 outl(0, ALI_REG(chip, ALI_MISCINT));
1899
1900 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1901 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1902 continue;
1903 im->regs[i] = inl(ALI_REG(chip, i*4));
1904 }
1905
1906 for (i = 0; i < ALI_CHANNELS; i++) {
1907 outb(i, ALI_REG(chip, ALI_GC_CIR));
1908 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1909 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1910 }
1911
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001912 /* stop all HW channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1914
1915 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return 0;
1917}
1918
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001919static int ali_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001921 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001922 struct snd_ali *chip = card->private_data;
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001923 struct snd_ali_image *im;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 int i, j;
1925
1926 im = chip->image;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001927 if (!im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return 0;
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 spin_lock_irq(&chip->reg_lock);
1931
1932 for (i = 0; i < ALI_CHANNELS; i++) {
1933 outb(i, ALI_REG(chip, ALI_GC_CIR));
1934 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1935 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
1936 }
1937
1938 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001939 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
1940 (i*4 == ALI_START))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 continue;
1942 outl(im->regs[i], ALI_REG(chip, i*4));
1943 }
1944
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001945 /* start HW channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001947 /* restore IRQ enable bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
1949
1950 spin_unlock_irq(&chip->reg_lock);
1951
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001952 for (i = 0 ; i < chip->num_of_codecs; i++)
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001953 snd_ac97_resume(chip->ac97[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01001955 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return 0;
1957}
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001958
1959static SIMPLE_DEV_PM_OPS(ali_pm, ali_suspend, ali_resume);
1960#define ALI_PM_OPS &ali_pm
1961#else
1962#define ALI_PM_OPS NULL
Takashi Iwaic7561cd2012-08-14 18:12:04 +02001963#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001965static int snd_ali_free(struct snd_ali * codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 if (codec->hw_initialized)
1968 snd_ali_disable_address_interrupt(codec);
Jeff Garzikf000fd82008-04-22 13:50:34 +02001969 if (codec->irq >= 0)
Takashi Iwai437a5a42006-11-21 12:14:23 +01001970 free_irq(codec->irq, codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (codec->port)
1972 pci_release_regions(codec->pci);
1973 pci_disable_device(codec->pci);
Takashi Iwaic7561cd2012-08-14 18:12:04 +02001974#ifdef CONFIG_PM_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 kfree(codec->image);
1976#endif
Jiri Slaby0dd119f2005-09-07 14:28:33 +02001977 pci_dev_put(codec->pci_m1533);
1978 pci_dev_put(codec->pci_m7101);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 kfree(codec);
1980 return 0;
1981}
1982
Takashi Iwaid1fabd92005-11-17 14:56:03 +01001983static int snd_ali_chip_init(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
1985 unsigned int legacy;
1986 unsigned char temp;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02001987 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Takashi Iwai5f1e6932014-02-25 15:48:50 +01001989 dev_dbg(codec->card->dev, "chip initializing ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 if (snd_ali_reset_5451(codec)) {
Takashi Iwai5f1e6932014-02-25 15:48:50 +01001992 dev_err(codec->card->dev, "ali_chip_init: reset 5451 error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return -1;
1994 }
1995
1996 if (codec->revision == ALI_5451_V02) {
1997 pci_dev = codec->pci_m1533;
1998 pci_read_config_byte(pci_dev, 0x59, &temp);
1999 temp |= 0x80;
2000 pci_write_config_byte(pci_dev, 0x59, temp);
2001
2002 pci_dev = codec->pci_m7101;
2003 pci_read_config_byte(pci_dev, 0xb8, &temp);
2004 temp |= 0x20;
2005 pci_write_config_byte(pci_dev, 0xB8, temp);
2006 }
2007
2008 pci_read_config_dword(codec->pci, 0x44, &legacy);
2009 legacy &= 0xff00ff00;
2010 legacy |= 0x000800aa;
2011 pci_write_config_dword(codec->pci, 0x44, legacy);
2012
2013 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2014 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2015 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2016 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2017 outb(0x10, ALI_REG(codec, ALI_MPUR2));
2018
2019 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002020 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
2021 AC97_EXTENDED_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (codec->spdif_support) {
2023 snd_ali_enable_spdif_out(codec);
2024 codec->spdif_mask = 0x00000002;
2025 }
2026
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002027 codec->num_of_codecs = 1;
2028
2029 /* secondary codec - modem */
2030 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
2031 codec->num_of_codecs++;
2032 outl(inl(ALI_REG(codec, ALI_SCTRL)) |
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002033 (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
2034 ALI_SCTRL_LINE_OUT_EN),
2035 ALI_REG(codec, ALI_SCTRL));
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002036 }
2037
Takashi Iwai5f1e6932014-02-25 15:48:50 +01002038 dev_dbg(codec->card->dev, "chip initialize succeed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 return 0;
2040
2041}
2042
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002043/* proc for register dump */
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002044static void snd_ali_proc_read(struct snd_info_entry *entry,
2045 struct snd_info_buffer *buf)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002046{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002047 struct snd_ali *codec = entry->private_data;
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002048 int i;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002049 for (i = 0; i < 256 ; i+= 4)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002050 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
2051}
2052
Bill Pembertone23e7a12012-12-06 12:35:10 -05002053static void snd_ali_proc_init(struct snd_ali *codec)
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002054{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002055 struct snd_info_entry *entry;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002056 if (!snd_card_proc_new(codec->card, "ali5451", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002057 snd_info_set_text_ops(entry, codec, snd_ali_proc_read);
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002058}
2059
Bill Pembertone23e7a12012-12-06 12:35:10 -05002060static int snd_ali_resources(struct snd_ali *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
2062 int err;
2063
Takashi Iwai5f1e6932014-02-25 15:48:50 +01002064 dev_dbg(codec->card->dev, "resources allocation ...\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002065 err = pci_request_regions(codec->pci, "ALI 5451");
2066 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return err;
2068 codec->port = pci_resource_start(codec->pci, 0);
2069
Takashi Iwai437a5a42006-11-21 12:14:23 +01002070 if (request_irq(codec->pci->irq, snd_ali_card_interrupt,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002071 IRQF_SHARED, KBUILD_MODNAME, codec)) {
Takashi Iwai5f1e6932014-02-25 15:48:50 +01002072 dev_err(codec->card->dev, "Unable to request irq.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return -EBUSY;
2074 }
2075 codec->irq = codec->pci->irq;
Takashi Iwai5f1e6932014-02-25 15:48:50 +01002076 dev_dbg(codec->card->dev, "resources allocated.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return 0;
2078}
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002079static int snd_ali_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002081 struct snd_ali *codec = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 snd_ali_free(codec);
2083 return 0;
2084}
2085
Bill Pembertone23e7a12012-12-06 12:35:10 -05002086static int snd_ali_create(struct snd_card *card,
2087 struct pci_dev *pci,
2088 int pcm_streams,
2089 int spdif_support,
2090 struct snd_ali **r_ali)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002092 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 int i, err;
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002094 unsigned short cmdw;
2095 static struct snd_device_ops ops = {
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002096 .dev_free = snd_ali_dev_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 };
2098
2099 *r_ali = NULL;
2100
Takashi Iwai5f1e6932014-02-25 15:48:50 +01002101 dev_dbg(card->dev, "creating ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 /* enable PCI device */
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002104 err = pci_enable_device(pci);
2105 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 return err;
2107 /* check, if we can restrict PCI DMA transfers to 31 bits */
Quentin Lambert412b9792015-04-15 16:10:17 +02002108 if (dma_set_mask(&pci->dev, DMA_BIT_MASK(31)) < 0 ||
2109 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(31)) < 0) {
Takashi Iwai5f1e6932014-02-25 15:48:50 +01002110 dev_err(card->dev,
2111 "architecture does not support 31bit PCI busmaster DMA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 pci_disable_device(pci);
2113 return -ENXIO;
2114 }
2115
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002116 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
2117 if (!codec) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 pci_disable_device(pci);
2119 return -ENOMEM;
2120 }
2121
2122 spin_lock_init(&codec->reg_lock);
2123 spin_lock_init(&codec->voice_alloc);
2124
2125 codec->card = card;
2126 codec->pci = pci;
2127 codec->irq = -1;
Auke Kok44c10132007-06-08 15:46:36 -07002128 codec->revision = pci->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 codec->spdif_support = spdif_support;
2130
2131 if (pcm_streams < 1)
2132 pcm_streams = 1;
2133 if (pcm_streams > 32)
2134 pcm_streams = 32;
2135
2136 pci_set_master(pci);
2137 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2138 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2139 cmdw |= PCI_COMMAND_IO;
2140 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2141 }
2142 pci_set_master(pci);
2143
2144 if (snd_ali_resources(codec)) {
2145 snd_ali_free(codec);
2146 return -EBUSY;
2147 }
2148
2149 synchronize_irq(pci->irq);
2150
2151 codec->synth.chmap = 0;
2152 codec->synth.chcnt = 0;
2153 codec->spdif_mask = 0;
2154 codec->synth.synthcount = 0;
2155
2156 if (codec->revision == ALI_5451_V02)
2157 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2158 else
2159 codec->chregs.regs.ac97read = ALI_AC97_READ;
2160 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2161
2162 codec->chregs.regs.start = ALI_START;
2163 codec->chregs.regs.stop = ALI_STOP;
2164 codec->chregs.regs.aint = ALI_AINT;
2165 codec->chregs.regs.ainten = ALI_AINTEN;
2166
2167 codec->chregs.data.start = 0x00;
2168 codec->chregs.data.stop = 0x00;
2169 codec->chregs.data.aint = 0x00;
2170 codec->chregs.data.ainten = 0x00;
2171
2172 /* M1533: southbridge */
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002173 codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002174 if (!codec->pci_m1533) {
Takashi Iwai5f1e6932014-02-25 15:48:50 +01002175 dev_err(card->dev, "cannot find ALi 1533 chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 snd_ali_free(codec);
2177 return -ENODEV;
2178 }
2179 /* M7101: power management */
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002180 codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002181 if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
Takashi Iwai5f1e6932014-02-25 15:48:50 +01002182 dev_err(card->dev, "cannot find ALi 7101 chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 snd_ali_free(codec);
2184 return -ENODEV;
2185 }
2186
Takashi Iwai5f1e6932014-02-25 15:48:50 +01002187 dev_dbg(card->dev, "snd_device_new is called.\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002188 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops);
2189 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 snd_ali_free(codec);
2191 return err;
2192 }
2193
2194 /* initialise synth voices*/
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002195 for (i = 0; i < ALI_CHANNELS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 codec->synth.voices[i].number = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002198 err = snd_ali_chip_init(codec);
2199 if (err < 0) {
Takashi Iwai5f1e6932014-02-25 15:48:50 +01002200 dev_err(card->dev, "ali create: chip init error.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return err;
2202 }
2203
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002204#ifdef CONFIG_PM_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002206 if (!codec->image)
Takashi Iwai5f1e6932014-02-25 15:48:50 +01002207 dev_warn(card->dev, "can't allocate apm buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208#endif
2209
2210 snd_ali_enable_address_interrupt(codec);
2211 codec->hw_initialized = 1;
2212
2213 *r_ali = codec;
Takashi Iwai5f1e6932014-02-25 15:48:50 +01002214 dev_dbg(card->dev, "created.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 return 0;
2216}
2217
Bill Pembertone23e7a12012-12-06 12:35:10 -05002218static int snd_ali_probe(struct pci_dev *pci,
2219 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
Takashi Iwaid1fabd92005-11-17 14:56:03 +01002221 struct snd_card *card;
2222 struct snd_ali *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 int err;
2224
Takashi Iwai5f1e6932014-02-25 15:48:50 +01002225 dev_dbg(&pci->dev, "probe ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Takashi Iwai60c57722014-01-29 14:20:19 +01002227 err = snd_card_new(&pci->dev, index, id, THIS_MODULE, 0, &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002228 if (err < 0)
2229 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002231 err = snd_ali_create(card, pci, pcm_channels, spdif, &codec);
2232 if (err < 0)
2233 goto error;
Takashi Iwaibf53c3b2005-11-17 16:10:51 +01002234 card->private_data = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Takashi Iwai5f1e6932014-02-25 15:48:50 +01002236 dev_dbg(&pci->dev, "mixer building ...\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002237 err = snd_ali_mixer(codec);
2238 if (err < 0)
2239 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Takashi Iwai5f1e6932014-02-25 15:48:50 +01002241 dev_dbg(&pci->dev, "pcm building ...\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002242 err = snd_ali_build_pcms(codec);
2243 if (err < 0)
2244 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Sasha Khapyorsky5cbff892005-05-30 08:09:56 +02002246 snd_ali_proc_init(codec);
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 strcpy(card->driver, "ALI5451");
2249 strcpy(card->shortname, "ALI 5451");
2250
Takashi Iwaiba8c3c32007-05-30 12:42:31 +02002251 sprintf(card->longname, "%s at 0x%lx, irq %i",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 card->shortname, codec->port, codec->irq);
2253
Takashi Iwai5f1e6932014-02-25 15:48:50 +01002254 dev_dbg(&pci->dev, "register card.\n");
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002255 err = snd_card_register(card);
2256 if (err < 0)
2257 goto error;
2258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 pci_set_drvdata(pci, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return 0;
Takashi Iwaif9ab2b12007-04-03 13:20:49 +02002261
2262 error:
2263 snd_card_free(card);
2264 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
Bill Pembertone23e7a12012-12-06 12:35:10 -05002267static void snd_ali_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
2269 snd_card_free(pci_get_drvdata(pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002272static struct pci_driver ali5451_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002273 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 .id_table = snd_ali_ids,
2275 .probe = snd_ali_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05002276 .remove = snd_ali_remove,
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002277 .driver = {
2278 .pm = ALI_PM_OPS,
2279 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280};
2281
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002282module_pci_driver(ali5451_driver);