1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Matt Wu <Matt_Wu@acersoftech.com.cn>
4 * Apr 26, 2001
5 * Routines for control of ALi pci audio M5451
6 *
7 * BUGS:
8 * --
9 *
10 * TODO:
11 * --
12 */
13
14 #include <linux/io.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/init.h>
18 #include <linux/pci.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/dma-mapping.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/info.h>
25 #include <sound/ac97_codec.h>
26 #include <sound/mpu401.h>
27 #include <sound/initval.h>
28
29 MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
30 MODULE_DESCRIPTION("ALI M5451");
31 MODULE_LICENSE("GPL");
32
33 static int index = SNDRV_DEFAULT_IDX1; /* Index */
34 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
35 static int pcm_channels = 32;
36 static bool spdif;
37
38 module_param(index, int, 0444);
39 MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
40 module_param(id, charp, 0444);
41 MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
42 module_param(pcm_channels, int, 0444);
43 MODULE_PARM_DESC(pcm_channels, "PCM Channels");
44 module_param(spdif, bool, 0444);
45 MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
46
47 /* just for backward compatibility */
48 static bool enable;
49 module_param(enable, bool, 0444);
50
51
52 /*
53 * Constants definition
54 */
55
56 #define DEVICE_ID_ALI5451 ((PCI_VENDOR_ID_AL<<16)|PCI_DEVICE_ID_AL_M5451)
57
58
59 #define ALI_CHANNELS 32
60
61 #define ALI_PCM_IN_CHANNEL 31
62 #define ALI_SPDIF_IN_CHANNEL 19
63 #define ALI_SPDIF_OUT_CHANNEL 15
64 #define ALI_CENTER_CHANNEL 24
65 #define ALI_LEF_CHANNEL 23
66 #define ALI_SURR_LEFT_CHANNEL 26
67 #define ALI_SURR_RIGHT_CHANNEL 25
68 #define ALI_MODEM_IN_CHANNEL 21
69 #define ALI_MODEM_OUT_CHANNEL 20
70
71 #define SNDRV_ALI_VOICE_TYPE_PCM 01
72 #define SNDRV_ALI_VOICE_TYPE_OTH 02
73
74 #define ALI_5451_V02 0x02
75
76 /*
77 * Direct Registers
78 */
79
80 #define ALI_LEGACY_DMAR0 0x00 /* ADR0 */
81 #define ALI_LEGACY_DMAR4 0x04 /* CNT0 */
82 #define ALI_LEGACY_DMAR11 0x0b /* MOD */
83 #define ALI_LEGACY_DMAR15 0x0f /* MMR */
84 #define ALI_MPUR0 0x20
85 #define ALI_MPUR1 0x21
86 #define ALI_MPUR2 0x22
87 #define ALI_MPUR3 0x23
88
89 #define ALI_AC97_WRITE 0x40
90 #define ALI_AC97_READ 0x44
91
92 #define ALI_SCTRL 0x48
93 #define ALI_SPDIF_OUT_ENABLE 0x20
94 #define ALI_SCTRL_LINE_IN2 (1 << 9)
95 #define ALI_SCTRL_GPIO_IN2 (1 << 13)
96 #define ALI_SCTRL_LINE_OUT_EN (1 << 20)
97 #define ALI_SCTRL_GPIO_OUT_EN (1 << 23)
98 #define ALI_SCTRL_CODEC1_READY (1 << 24)
99 #define ALI_SCTRL_CODEC2_READY (1 << 25)
100 #define ALI_AC97_GPIO 0x4c
101 #define ALI_AC97_GPIO_ENABLE 0x8000
102 #define ALI_AC97_GPIO_DATA_SHIFT 16
103 #define ALI_SPDIF_CS 0x70
104 #define ALI_SPDIF_CTRL 0x74
105 #define ALI_SPDIF_IN_FUNC_ENABLE 0x02
106 #define ALI_SPDIF_IN_CH_STATUS 0x40
107 #define ALI_SPDIF_OUT_CH_STATUS 0xbf
108 #define ALI_START 0x80
109 #define ALI_STOP 0x84
110 #define ALI_CSPF 0x90
111 #define ALI_AINT 0x98
112 #define ALI_GC_CIR 0xa0
113 #define ENDLP_IE 0x00001000
114 #define MIDLP_IE 0x00002000
115 #define ALI_AINTEN 0xa4
116 #define ALI_VOLUME 0xa8
117 #define ALI_SBDELTA_DELTA_R 0xac
118 #define ALI_MISCINT 0xb0
119 #define ADDRESS_IRQ 0x00000020
120 #define TARGET_REACHED 0x00008000
121 #define MIXER_OVERFLOW 0x00000800
122 #define MIXER_UNDERFLOW 0x00000400
123 #define GPIO_IRQ 0x01000000
124 #define ALI_SBBL_SBCL 0xc0
125 #define ALI_SBCTRL_SBE2R_SBDD 0xc4
126 #define ALI_STIMER 0xc8
127 #define ALI_GLOBAL_CONTROL 0xd4
128 #define ALI_SPDIF_OUT_SEL_PCM 0x00000400 /* bit 10 */
129 #define ALI_SPDIF_IN_SUPPORT 0x00000800 /* bit 11 */
130 #define ALI_SPDIF_OUT_CH_ENABLE 0x00008000 /* bit 15 */
131 #define ALI_SPDIF_IN_CH_ENABLE 0x00080000 /* bit 19 */
132 #define ALI_PCM_IN_ENABLE 0x80000000 /* bit 31 */
133
134 #define ALI_CSO_ALPHA_FMS 0xe0
135 #define ALI_LBA 0xe4
136 #define ALI_ESO_DELTA 0xe8
137 #define ALI_GVSEL_PAN_VOC_CTRL_EC 0xf0
138 #define ALI_EBUF1 0xf4
139 #define ALI_EBUF2 0xf8
140
141 #define ALI_REG(codec, x) ((codec)->port + x)
142
143 #define MAX_CODECS 2
144
145
146 struct snd_ali;
147 struct snd_ali_voice;
148
149 struct snd_ali_channel_control {
150 /* register data */
151 struct REGDATA {
152 unsigned int start;
153 unsigned int stop;
154 unsigned int aint;
155 unsigned int ainten;
156 } data;
157
158 /* register addresses */
159 struct REGS {
160 unsigned int start;
161 unsigned int stop;
162 unsigned int aint;
163 unsigned int ainten;
164 unsigned int ac97read;
165 unsigned int ac97write;
166 } regs;
167
168 };
169
170 struct snd_ali_voice {
171 unsigned int number;
172 unsigned int use :1,
173 pcm :1,
174 midi :1,
175 mode :1,
176 synth :1,
177 running :1;
178
179 /* PCM data */
180 struct snd_ali *codec;
181 struct snd_pcm_substream *substream;
182 struct snd_ali_voice *extra;
183
184 int eso; /* final ESO value for channel */
185 int count; /* runtime->period_size */
186
187 /* --- */
188
189 void *private_data;
190 void (*private_free)(void *private_data);
191 };
192
193
194 struct snd_alidev {
195
196 struct snd_ali_voice voices[ALI_CHANNELS];
197
198 unsigned int chcnt; /* num of opened channels */
199 unsigned int chmap; /* bitmap for opened channels */
200 unsigned int synthcount;
201
202 };
203
204
205 #define ALI_GLOBAL_REGS 56
206 #define ALI_CHANNEL_REGS 8
207 struct snd_ali_image {
208 u32 regs[ALI_GLOBAL_REGS];
209 u32 channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
210 };
211
212
213 struct snd_ali {
214 int irq;
215 unsigned long port;
216 unsigned char revision;
217
218 unsigned int hw_initialized :1;
219 unsigned int spdif_support :1;
220
221 struct pci_dev *pci;
222 struct pci_dev *pci_m1533;
223 struct pci_dev *pci_m7101;
224
225 struct snd_card *card;
226 struct snd_pcm *pcm[MAX_CODECS];
227 struct snd_alidev synth;
228 struct snd_ali_channel_control chregs;
229
230 /* S/PDIF Mask */
231 unsigned int spdif_mask;
232
233 unsigned int spurious_irq_count;
234 unsigned int spurious_irq_max_delta;
235
236 unsigned int num_of_codecs;
237
238 struct snd_ac97_bus *ac97_bus;
239 struct snd_ac97 *ac97[MAX_CODECS];
240 unsigned short ac97_ext_id;
241 unsigned short ac97_ext_status;
242
243 spinlock_t reg_lock;
244 spinlock_t voice_alloc;
245
246 struct snd_ali_image image;
247 };
248
249 static const struct pci_device_id snd_ali_ids[] = {
250 {PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5451), 0, 0, 0},
251 {0, }
252 };
253 MODULE_DEVICE_TABLE(pci, snd_ali_ids);
254
255 static void snd_ali_clear_voices(struct snd_ali *, unsigned int, unsigned int);
256 static unsigned short snd_ali_codec_peek(struct snd_ali *, int, unsigned short);
257 static void snd_ali_codec_poke(struct snd_ali *, int, unsigned short,
258 unsigned short);
259
260 /*
261 * AC97 ACCESS
262 */
263
snd_ali_5451_peek(struct snd_ali * codec,unsigned int port)264 static inline unsigned int snd_ali_5451_peek(struct snd_ali *codec,
265 unsigned int port)
266 {
267 return (unsigned int)inl(ALI_REG(codec, port));
268 }
269
snd_ali_5451_poke(struct snd_ali * codec,unsigned int port,unsigned int val)270 static inline void snd_ali_5451_poke(struct snd_ali *codec,
271 unsigned int port,
272 unsigned int val)
273 {
274 outl((unsigned int)val, ALI_REG(codec, port));
275 }
276
snd_ali_codec_ready(struct snd_ali * codec,unsigned int port)277 static int snd_ali_codec_ready(struct snd_ali *codec,
278 unsigned int port)
279 {
280 unsigned long end_time;
281 unsigned int res;
282
283 end_time = jiffies + msecs_to_jiffies(250);
284
285 for (;;) {
286 res = snd_ali_5451_peek(codec,port);
287 if (!(res & 0x8000))
288 return 0;
289 if (!time_after_eq(end_time, jiffies))
290 break;
291 schedule_timeout_uninterruptible(1);
292 }
293
294 snd_ali_5451_poke(codec, port, res & ~0x8000);
295 dev_dbg(codec->card->dev, "ali_codec_ready: codec is not ready.\n");
296 return -EIO;
297 }
298
snd_ali_stimer_ready(struct snd_ali * codec)299 static int snd_ali_stimer_ready(struct snd_ali *codec)
300 {
301 unsigned long end_time;
302 unsigned long dwChk1,dwChk2;
303
304 dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
305 end_time = jiffies + msecs_to_jiffies(250);
306
307 for (;;) {
308 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
309 if (dwChk2 != dwChk1)
310 return 0;
311 if (!time_after_eq(end_time, jiffies))
312 break;
313 schedule_timeout_uninterruptible(1);
314 }
315
316 dev_err(codec->card->dev, "ali_stimer_read: stimer is not ready.\n");
317 return -EIO;
318 }
319
snd_ali_codec_poke(struct snd_ali * codec,int secondary,unsigned short reg,unsigned short val)320 static void snd_ali_codec_poke(struct snd_ali *codec,int secondary,
321 unsigned short reg,
322 unsigned short val)
323 {
324 unsigned int dwVal;
325 unsigned int port;
326
327 if (reg >= 0x80) {
328 dev_err(codec->card->dev,
329 "ali_codec_poke: reg(%xh) invalid.\n", reg);
330 return;
331 }
332
333 port = codec->chregs.regs.ac97write;
334
335 if (snd_ali_codec_ready(codec, port) < 0)
336 return;
337 if (snd_ali_stimer_ready(codec) < 0)
338 return;
339
340 dwVal = (unsigned int) (reg & 0xff);
341 dwVal |= 0x8000 | (val << 16);
342 if (secondary)
343 dwVal |= 0x0080;
344 if (codec->revision == ALI_5451_V02)
345 dwVal |= 0x0100;
346
347 snd_ali_5451_poke(codec, port, dwVal);
348
349 return ;
350 }
351
snd_ali_codec_peek(struct snd_ali * codec,int secondary,unsigned short reg)352 static unsigned short snd_ali_codec_peek(struct snd_ali *codec,
353 int secondary,
354 unsigned short reg)
355 {
356 unsigned int dwVal;
357 unsigned int port;
358
359 if (reg >= 0x80) {
360 dev_err(codec->card->dev,
361 "ali_codec_peek: reg(%xh) invalid.\n", reg);
362 return ~0;
363 }
364
365 port = codec->chregs.regs.ac97read;
366
367 if (snd_ali_codec_ready(codec, port) < 0)
368 return ~0;
369 if (snd_ali_stimer_ready(codec) < 0)
370 return ~0;
371
372 dwVal = (unsigned int) (reg & 0xff);
373 dwVal |= 0x8000; /* bit 15*/
374 if (secondary)
375 dwVal |= 0x0080;
376
377 snd_ali_5451_poke(codec, port, dwVal);
378
379 if (snd_ali_stimer_ready(codec) < 0)
380 return ~0;
381 if (snd_ali_codec_ready(codec, port) < 0)
382 return ~0;
383
384 return (snd_ali_5451_peek(codec, port) & 0xffff0000) >> 16;
385 }
386
snd_ali_codec_write(struct snd_ac97 * ac97,unsigned short reg,unsigned short val)387 static void snd_ali_codec_write(struct snd_ac97 *ac97,
388 unsigned short reg,
389 unsigned short val )
390 {
391 struct snd_ali *codec = ac97->private_data;
392
393 dev_dbg(codec->card->dev, "codec_write: reg=%xh data=%xh.\n", reg, val);
394 if (reg == AC97_GPIO_STATUS) {
395 outl((val << ALI_AC97_GPIO_DATA_SHIFT) | ALI_AC97_GPIO_ENABLE,
396 ALI_REG(codec, ALI_AC97_GPIO));
397 return;
398 }
399 snd_ali_codec_poke(codec, ac97->num, reg, val);
400 return ;
401 }
402
403
snd_ali_codec_read(struct snd_ac97 * ac97,unsigned short reg)404 static unsigned short snd_ali_codec_read(struct snd_ac97 *ac97,
405 unsigned short reg)
406 {
407 struct snd_ali *codec = ac97->private_data;
408
409 dev_dbg(codec->card->dev, "codec_read reg=%xh.\n", reg);
410 return snd_ali_codec_peek(codec, ac97->num, reg);
411 }
412
413 /*
414 * AC97 Reset
415 */
416
snd_ali_reset_5451(struct snd_ali * codec)417 static int snd_ali_reset_5451(struct snd_ali *codec)
418 {
419 struct pci_dev *pci_dev;
420 unsigned short wCount, wReg;
421 unsigned int dwVal;
422
423 pci_dev = codec->pci_m1533;
424 if (pci_dev) {
425 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
426 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
427 mdelay(5);
428 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
429 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
430 mdelay(5);
431 }
432
433 pci_dev = codec->pci;
434 pci_read_config_dword(pci_dev, 0x44, &dwVal);
435 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
436 udelay(500);
437 pci_read_config_dword(pci_dev, 0x44, &dwVal);
438 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
439 mdelay(5);
440
441 wCount = 200;
442 while(wCount--) {
443 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
444 if ((wReg & 0x000f) == 0x000f)
445 return 0;
446 mdelay(5);
447 }
448
449 /* non-fatal if you have a non PM capable codec */
450 /* dev_warn(codec->card->dev, "ali5451: reset time out\n"); */
451 return 0;
452 }
453
454 /*
455 * ALI 5451 Controller
456 */
457
snd_ali_enable_special_channel(struct snd_ali * codec,unsigned int channel)458 static void snd_ali_enable_special_channel(struct snd_ali *codec,
459 unsigned int channel)
460 {
461 unsigned long dwVal;
462
463 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
464 dwVal |= 1 << (channel & 0x0000001f);
465 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
466 }
467
snd_ali_disable_special_channel(struct snd_ali * codec,unsigned int channel)468 static void snd_ali_disable_special_channel(struct snd_ali *codec,
469 unsigned int channel)
470 {
471 unsigned long dwVal;
472
473 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
474 dwVal &= ~(1 << (channel & 0x0000001f));
475 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
476 }
477
snd_ali_enable_address_interrupt(struct snd_ali * codec)478 static void snd_ali_enable_address_interrupt(struct snd_ali *codec)
479 {
480 unsigned int gc;
481
482 gc = inl(ALI_REG(codec, ALI_GC_CIR));
483 gc |= ENDLP_IE;
484 gc |= MIDLP_IE;
485 outl( gc, ALI_REG(codec, ALI_GC_CIR));
486 }
487
snd_ali_disable_address_interrupt(struct snd_ali * codec)488 static void snd_ali_disable_address_interrupt(struct snd_ali *codec)
489 {
490 unsigned int gc;
491
492 gc = inl(ALI_REG(codec, ALI_GC_CIR));
493 gc &= ~ENDLP_IE;
494 gc &= ~MIDLP_IE;
495 outl(gc, ALI_REG(codec, ALI_GC_CIR));
496 }
497
snd_ali_disable_voice_irq(struct snd_ali * codec,unsigned int channel)498 static void snd_ali_disable_voice_irq(struct snd_ali *codec,
499 unsigned int channel)
500 {
501 unsigned int mask;
502 struct snd_ali_channel_control *pchregs = &(codec->chregs);
503
504 dev_dbg(codec->card->dev, "disable_voice_irq channel=%d\n", channel);
505
506 mask = 1 << (channel & 0x1f);
507 pchregs->data.ainten = inl(ALI_REG(codec, pchregs->regs.ainten));
508 pchregs->data.ainten &= ~mask;
509 outl(pchregs->data.ainten, ALI_REG(codec, pchregs->regs.ainten));
510 }
511
snd_ali_alloc_pcm_channel(struct snd_ali * codec,int channel)512 static int snd_ali_alloc_pcm_channel(struct snd_ali *codec, int channel)
513 {
514 unsigned int idx = channel & 0x1f;
515
516 if (codec->synth.chcnt >= ALI_CHANNELS){
517 dev_err(codec->card->dev,
518 "ali_alloc_pcm_channel: no free channels.\n");
519 return -1;
520 }
521
522 if (!(codec->synth.chmap & (1 << idx))) {
523 codec->synth.chmap |= 1 << idx;
524 codec->synth.chcnt++;
525 dev_dbg(codec->card->dev, "alloc_pcm_channel no. %d.\n", idx);
526 return idx;
527 }
528 return -1;
529 }
530
snd_ali_find_free_channel(struct snd_ali * codec,int rec)531 static int snd_ali_find_free_channel(struct snd_ali * codec, int rec)
532 {
533 int idx;
534 int result = -1;
535
536 dev_dbg(codec->card->dev,
537 "find_free_channel: for %s\n", rec ? "rec" : "pcm");
538
539 /* recording */
540 if (rec) {
541 if (codec->spdif_support &&
542 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
543 ALI_SPDIF_IN_SUPPORT))
544 idx = ALI_SPDIF_IN_CHANNEL;
545 else
546 idx = ALI_PCM_IN_CHANNEL;
547
548 result = snd_ali_alloc_pcm_channel(codec, idx);
549 if (result >= 0)
550 return result;
551 else {
552 dev_err(codec->card->dev,
553 "ali_find_free_channel: record channel is busy now.\n");
554 return -1;
555 }
556 }
557
558 /* playback... */
559 if (codec->spdif_support &&
560 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
561 ALI_SPDIF_OUT_CH_ENABLE)) {
562 idx = ALI_SPDIF_OUT_CHANNEL;
563 result = snd_ali_alloc_pcm_channel(codec, idx);
564 if (result >= 0)
565 return result;
566 else
567 dev_err(codec->card->dev,
568 "ali_find_free_channel: S/PDIF out channel is in busy now.\n");
569 }
570
571 for (idx = 0; idx < ALI_CHANNELS; idx++) {
572 result = snd_ali_alloc_pcm_channel(codec, idx);
573 if (result >= 0)
574 return result;
575 }
576 dev_err(codec->card->dev, "ali_find_free_channel: no free channels.\n");
577 return -1;
578 }
579
snd_ali_free_channel_pcm(struct snd_ali * codec,int channel)580 static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel)
581 {
582 unsigned int idx = channel & 0x0000001f;
583
584 dev_dbg(codec->card->dev, "free_channel_pcm channel=%d\n", channel);
585
586 if (channel < 0 || channel >= ALI_CHANNELS)
587 return;
588
589 if (!(codec->synth.chmap & (1 << idx))) {
590 dev_err(codec->card->dev,
591 "ali_free_channel_pcm: channel %d is not in use.\n",
592 channel);
593 return;
594 } else {
595 codec->synth.chmap &= ~(1 << idx);
596 codec->synth.chcnt--;
597 }
598 }
599
snd_ali_stop_voice(struct snd_ali * codec,unsigned int channel)600 static void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel)
601 {
602 unsigned int mask = 1 << (channel & 0x1f);
603
604 dev_dbg(codec->card->dev, "stop_voice: channel=%d\n", channel);
605 outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
606 }
607
608 /*
609 * S/PDIF Part
610 */
611
snd_ali_delay(struct snd_ali * codec,int interval)612 static void snd_ali_delay(struct snd_ali *codec,int interval)
613 {
614 unsigned long begintimer,currenttimer;
615
616 begintimer = inl(ALI_REG(codec, ALI_STIMER));
617 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
618
619 while (currenttimer < begintimer + interval) {
620 if (snd_ali_stimer_ready(codec) < 0)
621 break;
622 currenttimer = inl(ALI_REG(codec, ALI_STIMER));
623 cpu_relax();
624 }
625 }
626
snd_ali_detect_spdif_rate(struct snd_ali * codec)627 static void snd_ali_detect_spdif_rate(struct snd_ali *codec)
628 {
629 u16 wval;
630 u16 count = 0;
631 u8 bval, R1 = 0, R2;
632
633 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
634 bval |= 0x1F;
635 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1));
636
637 while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) {
638 count ++;
639 snd_ali_delay(codec, 6);
640 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1));
641 R1 = bval & 0x1F;
642 }
643
644 if (count > 50000) {
645 dev_err(codec->card->dev, "ali_detect_spdif_rate: timeout!\n");
646 return;
647 }
648
649 for (count = 0; count <= 50000; count++) {
650 snd_ali_delay(codec, 6);
651 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
652 R2 = bval & 0x1F;
653 if (R2 != R1)
654 R1 = R2;
655 else
656 break;
657 }
658
659 if (count > 50000) {
660 dev_err(codec->card->dev, "ali_detect_spdif_rate: timeout!\n");
661 return;
662 }
663
664 if (R2 >= 0x0b && R2 <= 0x0e) {
665 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
666 wval &= 0xe0f0;
667 wval |= (0x09 << 8) | 0x05;
668 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
669
670 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0;
671 outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3));
672 } else if (R2 == 0x12) {
673 wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2));
674 wval &= 0xe0f0;
675 wval |= (0x0e << 8) | 0x08;
676 outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2));
677
678 bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0;
679 outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3));
680 }
681 }
682
snd_ali_get_spdif_in_rate(struct snd_ali * codec)683 static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec)
684 {
685 u32 dwRate;
686 u8 bval;
687
688 bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
689 bval &= 0x7f;
690 bval |= 0x40;
691 outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL));
692
693 snd_ali_detect_spdif_rate(codec);
694
695 bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3));
696 bval &= 0x0f;
697
698 switch (bval) {
699 case 0: dwRate = 44100; break;
700 case 1: dwRate = 48000; break;
701 case 2: dwRate = 32000; break;
702 default: dwRate = 0; break;
703 }
704
705 return dwRate;
706 }
707
snd_ali_enable_spdif_in(struct snd_ali * codec)708 static void snd_ali_enable_spdif_in(struct snd_ali *codec)
709 {
710 unsigned int dwVal;
711
712 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
713 dwVal |= ALI_SPDIF_IN_SUPPORT;
714 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
715
716 dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
717 dwVal |= 0x02;
718 outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
719
720 snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
721 }
722
snd_ali_disable_spdif_in(struct snd_ali * codec)723 static void snd_ali_disable_spdif_in(struct snd_ali *codec)
724 {
725 unsigned int dwVal;
726
727 dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
728 dwVal &= ~ALI_SPDIF_IN_SUPPORT;
729 outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
730
731 snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
732 }
733
734
snd_ali_set_spdif_out_rate(struct snd_ali * codec,unsigned int rate)735 static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate)
736 {
737 unsigned char bVal;
738 unsigned int dwRate;
739
740 switch (rate) {
741 case 32000: dwRate = 0x300; break;
742 case 48000: dwRate = 0x200; break;
743 default: dwRate = 0; break;
744 }
745
746 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
747 bVal &= (unsigned char)(~(1<<6));
748
749 bVal |= 0x80; /* select right */
750 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
751 outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
752
753 bVal &= ~0x80; /* select left */
754 outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
755 outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
756 }
757
snd_ali_enable_spdif_out(struct snd_ali * codec)758 static void snd_ali_enable_spdif_out(struct snd_ali *codec)
759 {
760 unsigned short wVal;
761 unsigned char bVal;
762 struct pci_dev *pci_dev;
763
764 pci_dev = codec->pci_m1533;
765 if (pci_dev == NULL)
766 return;
767 pci_read_config_byte(pci_dev, 0x61, &bVal);
768 bVal |= 0x40;
769 pci_write_config_byte(pci_dev, 0x61, bVal);
770 pci_read_config_byte(pci_dev, 0x7d, &bVal);
771 bVal |= 0x01;
772 pci_write_config_byte(pci_dev, 0x7d, bVal);
773
774 pci_read_config_byte(pci_dev, 0x7e, &bVal);
775 bVal &= (~0x20);
776 bVal |= 0x10;
777 pci_write_config_byte(pci_dev, 0x7e, bVal);
778
779 bVal = inb(ALI_REG(codec, ALI_SCTRL));
780 outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
781
782 bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
783 outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
784
785 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
786 wVal |= ALI_SPDIF_OUT_SEL_PCM;
787 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
788 snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
789 }
790
snd_ali_enable_spdif_chnout(struct snd_ali * codec)791 static void snd_ali_enable_spdif_chnout(struct snd_ali *codec)
792 {
793 unsigned short wVal;
794
795 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
796 wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
797 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
798 /*
799 wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
800 if (flag & ALI_SPDIF_OUT_NON_PCM)
801 wVal |= 0x0002;
802 else
803 wVal &= (~0x0002);
804 outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
805 */
806 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
807 }
808
snd_ali_disable_spdif_chnout(struct snd_ali * codec)809 static void snd_ali_disable_spdif_chnout(struct snd_ali *codec)
810 {
811 unsigned short wVal;
812
813 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
814 wVal |= ALI_SPDIF_OUT_SEL_PCM;
815 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
816
817 snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
818 }
819
snd_ali_disable_spdif_out(struct snd_ali * codec)820 static void snd_ali_disable_spdif_out(struct snd_ali *codec)
821 {
822 unsigned char bVal;
823
824 bVal = inb(ALI_REG(codec, ALI_SCTRL));
825 outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
826
827 snd_ali_disable_spdif_chnout(codec);
828 }
829
snd_ali_update_ptr(struct snd_ali * codec,int channel)830 static void snd_ali_update_ptr(struct snd_ali *codec, int channel)
831 {
832 struct snd_ali_voice *pvoice;
833 struct snd_ali_channel_control *pchregs;
834 unsigned int old, mask;
835
836 pchregs = &(codec->chregs);
837
838 /* check if interrupt occurred for channel */
839 old = pchregs->data.aint;
840 mask = 1U << (channel & 0x1f);
841
842 if (!(old & mask))
843 return;
844
845 pvoice = &codec->synth.voices[channel];
846
847 udelay(100);
848 spin_lock(&codec->reg_lock);
849
850 if (pvoice->pcm && pvoice->substream) {
851 /* pcm interrupt */
852 if (pvoice->running) {
853 dev_dbg(codec->card->dev,
854 "update_ptr: cso=%4.4x cspf=%d.\n",
855 inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2)),
856 (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask);
857 spin_unlock(&codec->reg_lock);
858 snd_pcm_period_elapsed(pvoice->substream);
859 spin_lock(&codec->reg_lock);
860 } else {
861 snd_ali_stop_voice(codec, channel);
862 snd_ali_disable_voice_irq(codec, channel);
863 }
864 } else if (codec->synth.voices[channel].synth) {
865 /* synth interrupt */
866 } else if (codec->synth.voices[channel].midi) {
867 /* midi interrupt */
868 } else {
869 /* unknown interrupt */
870 snd_ali_stop_voice(codec, channel);
871 snd_ali_disable_voice_irq(codec, channel);
872 }
873 spin_unlock(&codec->reg_lock);
874 outl(mask,ALI_REG(codec,pchregs->regs.aint));
875 pchregs->data.aint = old & (~mask);
876 }
877
snd_ali_card_interrupt(int irq,void * dev_id)878 static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id)
879 {
880 struct snd_ali *codec = dev_id;
881 int channel;
882 unsigned int audio_int;
883 struct snd_ali_channel_control *pchregs;
884
885 if (codec == NULL || !codec->hw_initialized)
886 return IRQ_NONE;
887
888 audio_int = inl(ALI_REG(codec, ALI_MISCINT));
889 if (!audio_int)
890 return IRQ_NONE;
891
892 pchregs = &(codec->chregs);
893 if (audio_int & ADDRESS_IRQ) {
894 /* get interrupt status for all channels */
895 pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint));
896 for (channel = 0; channel < ALI_CHANNELS; channel++)
897 snd_ali_update_ptr(codec, channel);
898 }
899 outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
900 ALI_REG(codec, ALI_MISCINT));
901
902 return IRQ_HANDLED;
903 }
904
905
snd_ali_alloc_voice(struct snd_ali * codec,int type,int rec,int channel)906 static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec,
907 int type, int rec, int channel)
908 {
909 struct snd_ali_voice *pvoice;
910 int idx;
911
912 dev_dbg(codec->card->dev, "alloc_voice: type=%d rec=%d\n", type, rec);
913
914 guard(spinlock_irq)(&codec->voice_alloc);
915 if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
916 idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) :
917 snd_ali_find_free_channel(codec,rec);
918 if (idx < 0) {
919 dev_err(codec->card->dev, "ali_alloc_voice: err.\n");
920 return NULL;
921 }
922 pvoice = &(codec->synth.voices[idx]);
923 pvoice->codec = codec;
924 pvoice->use = 1;
925 pvoice->pcm = 1;
926 pvoice->mode = rec;
927 return pvoice;
928 }
929 return NULL;
930 }
931
932
snd_ali_free_voice(struct snd_ali * codec,struct snd_ali_voice * pvoice)933 static void snd_ali_free_voice(struct snd_ali * codec,
934 struct snd_ali_voice *pvoice)
935 {
936 void (*private_free)(void *);
937 void *private_data;
938
939 dev_dbg(codec->card->dev, "free_voice: channel=%d\n", pvoice->number);
940 if (!pvoice->use)
941 return;
942 snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
943 scoped_guard(spinlock_irq, &codec->voice_alloc) {
944 private_free = pvoice->private_free;
945 private_data = pvoice->private_data;
946 pvoice->private_free = NULL;
947 pvoice->private_data = NULL;
948 if (pvoice->pcm)
949 snd_ali_free_channel_pcm(codec, pvoice->number);
950 pvoice->use = pvoice->pcm = pvoice->synth = 0;
951 pvoice->substream = NULL;
952 }
953 if (private_free)
954 private_free(private_data);
955 }
956
957
snd_ali_clear_voices(struct snd_ali * codec,unsigned int v_min,unsigned int v_max)958 static void snd_ali_clear_voices(struct snd_ali *codec,
959 unsigned int v_min,
960 unsigned int v_max)
961 {
962 unsigned int i;
963
964 for (i = v_min; i <= v_max; i++) {
965 snd_ali_stop_voice(codec, i);
966 snd_ali_disable_voice_irq(codec, i);
967 }
968 }
969
snd_ali_write_voice_regs(struct snd_ali * codec,unsigned int Channel,unsigned int LBA,unsigned int CSO,unsigned int ESO,unsigned int DELTA,unsigned int ALPHA_FMS,unsigned int GVSEL,unsigned int PAN,unsigned int VOL,unsigned int CTRL,unsigned int EC)970 static void snd_ali_write_voice_regs(struct snd_ali *codec,
971 unsigned int Channel,
972 unsigned int LBA,
973 unsigned int CSO,
974 unsigned int ESO,
975 unsigned int DELTA,
976 unsigned int ALPHA_FMS,
977 unsigned int GVSEL,
978 unsigned int PAN,
979 unsigned int VOL,
980 unsigned int CTRL,
981 unsigned int EC)
982 {
983 unsigned int ctlcmds[4];
984
985 outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR));
986
987 ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
988 ctlcmds[1] = LBA;
989 ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff);
990 ctlcmds[3] = (GVSEL << 31) |
991 ((PAN & 0x0000007f) << 24) |
992 ((VOL & 0x000000ff) << 16) |
993 ((CTRL & 0x0000000f) << 12) |
994 (EC & 0x00000fff);
995
996 outb(Channel, ALI_REG(codec, ALI_GC_CIR));
997
998 outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS));
999 outl(ctlcmds[1], ALI_REG(codec, ALI_LBA));
1000 outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA));
1001 outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC));
1002
1003 outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */
1004 outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */
1005 }
1006
snd_ali_convert_rate(unsigned int rate,int rec)1007 static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1008 {
1009 unsigned int delta;
1010
1011 if (rate < 4000)
1012 rate = 4000;
1013 if (rate > 48000)
1014 rate = 48000;
1015
1016 if (rec) {
1017 if (rate == 44100)
1018 delta = 0x116a;
1019 else if (rate == 8000)
1020 delta = 0x6000;
1021 else if (rate == 48000)
1022 delta = 0x1000;
1023 else
1024 delta = ((48000 << 12) / rate) & 0x0000ffff;
1025 } else {
1026 if (rate == 44100)
1027 delta = 0xeb3;
1028 else if (rate == 8000)
1029 delta = 0x2ab;
1030 else if (rate == 48000)
1031 delta = 0x1000;
1032 else
1033 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1034 }
1035
1036 return delta;
1037 }
1038
snd_ali_control_mode(struct snd_pcm_substream * substream)1039 static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream)
1040 {
1041 unsigned int CTRL;
1042 struct snd_pcm_runtime *runtime = substream->runtime;
1043
1044 /* set ctrl mode
1045 CTRL default: 8-bit (unsigned) mono, loop mode enabled
1046 */
1047 CTRL = 0x00000001;
1048 if (snd_pcm_format_width(runtime->format) == 16)
1049 CTRL |= 0x00000008; /* 16-bit data */
1050 if (!snd_pcm_format_unsigned(runtime->format))
1051 CTRL |= 0x00000002; /* signed data */
1052 if (runtime->channels > 1)
1053 CTRL |= 0x00000004; /* stereo data */
1054 return CTRL;
1055 }
1056
1057 /*
1058 * PCM part
1059 */
1060
snd_ali_trigger(struct snd_pcm_substream * substream,int cmd)1061 static int snd_ali_trigger(struct snd_pcm_substream *substream,
1062 int cmd)
1063
1064 {
1065 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1066 struct snd_pcm_substream *s;
1067 unsigned int what, whati;
1068 struct snd_ali_voice *pvoice, *evoice;
1069 unsigned int val;
1070 int do_start;
1071
1072 switch (cmd) {
1073 case SNDRV_PCM_TRIGGER_START:
1074 case SNDRV_PCM_TRIGGER_RESUME:
1075 do_start = 1;
1076 break;
1077 case SNDRV_PCM_TRIGGER_STOP:
1078 case SNDRV_PCM_TRIGGER_SUSPEND:
1079 do_start = 0;
1080 break;
1081 default:
1082 return -EINVAL;
1083 }
1084
1085 what = whati = 0;
1086 snd_pcm_group_for_each_entry(s, substream) {
1087 if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) {
1088 pvoice = s->runtime->private_data;
1089 evoice = pvoice->extra;
1090 what |= 1 << (pvoice->number & 0x1f);
1091 if (evoice == NULL)
1092 whati |= 1 << (pvoice->number & 0x1f);
1093 else {
1094 whati |= 1 << (evoice->number & 0x1f);
1095 what |= 1 << (evoice->number & 0x1f);
1096 }
1097 if (do_start) {
1098 pvoice->running = 1;
1099 if (evoice != NULL)
1100 evoice->running = 1;
1101 } else {
1102 pvoice->running = 0;
1103 if (evoice != NULL)
1104 evoice->running = 0;
1105 }
1106 snd_pcm_trigger_done(s, substream);
1107 }
1108 }
1109 guard(spinlock)(&codec->reg_lock);
1110 if (!do_start)
1111 outl(what, ALI_REG(codec, ALI_STOP));
1112 val = inl(ALI_REG(codec, ALI_AINTEN));
1113 if (do_start)
1114 val |= whati;
1115 else
1116 val &= ~whati;
1117 outl(val, ALI_REG(codec, ALI_AINTEN));
1118 if (do_start)
1119 outl(what, ALI_REG(codec, ALI_START));
1120 dev_dbg(codec->card->dev, "trigger: what=%xh whati=%xh\n", what, whati);
1121
1122 return 0;
1123 }
1124
snd_ali_playback_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)1125 static int snd_ali_playback_hw_params(struct snd_pcm_substream *substream,
1126 struct snd_pcm_hw_params *hw_params)
1127 {
1128 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1129 struct snd_pcm_runtime *runtime = substream->runtime;
1130 struct snd_ali_voice *pvoice = runtime->private_data;
1131 struct snd_ali_voice *evoice = pvoice->extra;
1132
1133 /* voice management */
1134
1135 if (params_buffer_size(hw_params) / 2 !=
1136 params_period_size(hw_params)) {
1137 if (!evoice) {
1138 evoice = snd_ali_alloc_voice(codec,
1139 SNDRV_ALI_VOICE_TYPE_PCM,
1140 0, -1);
1141 if (!evoice)
1142 return -ENOMEM;
1143 pvoice->extra = evoice;
1144 evoice->substream = substream;
1145 }
1146 } else {
1147 if (evoice) {
1148 snd_ali_free_voice(codec, evoice);
1149 pvoice->extra = evoice = NULL;
1150 }
1151 }
1152
1153 return 0;
1154 }
1155
snd_ali_playback_hw_free(struct snd_pcm_substream * substream)1156 static int snd_ali_playback_hw_free(struct snd_pcm_substream *substream)
1157 {
1158 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1159 struct snd_pcm_runtime *runtime = substream->runtime;
1160 struct snd_ali_voice *pvoice = runtime->private_data;
1161 struct snd_ali_voice *evoice = pvoice ? pvoice->extra : NULL;
1162
1163 if (evoice) {
1164 snd_ali_free_voice(codec, evoice);
1165 pvoice->extra = NULL;
1166 }
1167 return 0;
1168 }
1169
snd_ali_playback_prepare(struct snd_pcm_substream * substream)1170 static int snd_ali_playback_prepare(struct snd_pcm_substream *substream)
1171 {
1172 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1173 struct snd_pcm_runtime *runtime = substream->runtime;
1174 struct snd_ali_voice *pvoice = runtime->private_data;
1175 struct snd_ali_voice *evoice = pvoice->extra;
1176
1177 unsigned int LBA;
1178 unsigned int Delta;
1179 unsigned int ESO;
1180 unsigned int CTRL;
1181 unsigned int GVSEL;
1182 unsigned int PAN;
1183 unsigned int VOL;
1184 unsigned int EC;
1185
1186 dev_dbg(codec->card->dev, "playback_prepare ...\n");
1187
1188 guard(spinlock_irq)(&codec->reg_lock);
1189
1190 /* set Delta (rate) value */
1191 Delta = snd_ali_convert_rate(runtime->rate, 0);
1192
1193 if (pvoice->number == ALI_SPDIF_IN_CHANNEL ||
1194 pvoice->number == ALI_PCM_IN_CHANNEL)
1195 snd_ali_disable_special_channel(codec, pvoice->number);
1196 else if (codec->spdif_support &&
1197 (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) &
1198 ALI_SPDIF_OUT_CH_ENABLE)
1199 && pvoice->number == ALI_SPDIF_OUT_CHANNEL) {
1200 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1201 Delta = 0x1000;
1202 }
1203
1204 /* set Loop Back Address */
1205 LBA = runtime->dma_addr;
1206
1207 /* set interrupt count size */
1208 pvoice->count = runtime->period_size;
1209
1210 /* set target ESO for channel */
1211 pvoice->eso = runtime->buffer_size;
1212
1213 dev_dbg(codec->card->dev, "playback_prepare: eso=%xh count=%xh\n",
1214 pvoice->eso, pvoice->count);
1215
1216 /* set ESO to capture first MIDLP interrupt */
1217 ESO = pvoice->eso -1;
1218 /* set ctrl mode */
1219 CTRL = snd_ali_control_mode(substream);
1220
1221 GVSEL = 1;
1222 PAN = 0;
1223 VOL = 0;
1224 EC = 0;
1225 dev_dbg(codec->card->dev, "playback_prepare:\n");
1226 dev_dbg(codec->card->dev,
1227 "ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",
1228 pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1229 snd_ali_write_voice_regs(codec,
1230 pvoice->number,
1231 LBA,
1232 0, /* cso */
1233 ESO,
1234 Delta,
1235 0, /* alpha */
1236 GVSEL,
1237 PAN,
1238 VOL,
1239 CTRL,
1240 EC);
1241 if (evoice) {
1242 evoice->count = pvoice->count;
1243 evoice->eso = pvoice->count << 1;
1244 ESO = evoice->eso - 1;
1245 snd_ali_write_voice_regs(codec,
1246 evoice->number,
1247 LBA,
1248 0, /* cso */
1249 ESO,
1250 Delta,
1251 0, /* alpha */
1252 GVSEL,
1253 0x7f,
1254 0x3ff,
1255 CTRL,
1256 EC);
1257 }
1258 return 0;
1259 }
1260
1261
snd_ali_prepare(struct snd_pcm_substream * substream)1262 static int snd_ali_prepare(struct snd_pcm_substream *substream)
1263 {
1264 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1265 struct snd_pcm_runtime *runtime = substream->runtime;
1266 struct snd_ali_voice *pvoice = runtime->private_data;
1267 unsigned int LBA;
1268 unsigned int Delta;
1269 unsigned int ESO;
1270 unsigned int CTRL;
1271 unsigned int GVSEL;
1272 unsigned int PAN;
1273 unsigned int VOL;
1274 unsigned int EC;
1275 u8 bValue;
1276
1277 spin_lock_irq(&codec->reg_lock);
1278
1279 dev_dbg(codec->card->dev, "ali_prepare...\n");
1280
1281 snd_ali_enable_special_channel(codec,pvoice->number);
1282
1283 Delta = (pvoice->number == ALI_MODEM_IN_CHANNEL ||
1284 pvoice->number == ALI_MODEM_OUT_CHANNEL) ?
1285 0x1000 : snd_ali_convert_rate(runtime->rate, pvoice->mode);
1286
1287 /* Prepare capture intr channel */
1288 if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1289
1290 unsigned int rate;
1291
1292 spin_unlock_irq(&codec->reg_lock);
1293 if (codec->revision != ALI_5451_V02)
1294 return -1;
1295
1296 rate = snd_ali_get_spdif_in_rate(codec);
1297 if (rate == 0) {
1298 dev_warn(codec->card->dev,
1299 "ali_capture_prepare: spdif rate detect err!\n");
1300 rate = 48000;
1301 }
1302 spin_lock_irq(&codec->reg_lock);
1303 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1304 if (bValue & 0x10) {
1305 outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1306 dev_warn(codec->card->dev,
1307 "clear SPDIF parity error flag.\n");
1308 }
1309
1310 if (rate != 48000)
1311 Delta = ((rate << 12) / runtime->rate) & 0x00ffff;
1312 }
1313
1314 /* set target ESO for channel */
1315 pvoice->eso = runtime->buffer_size;
1316
1317 /* set interrupt count size */
1318 pvoice->count = runtime->period_size;
1319
1320 /* set Loop Back Address */
1321 LBA = runtime->dma_addr;
1322
1323 /* set ESO to capture first MIDLP interrupt */
1324 ESO = pvoice->eso - 1;
1325 CTRL = snd_ali_control_mode(substream);
1326 GVSEL = 0;
1327 PAN = 0x00;
1328 VOL = 0x00;
1329 EC = 0;
1330
1331 snd_ali_write_voice_regs( codec,
1332 pvoice->number,
1333 LBA,
1334 0, /* cso */
1335 ESO,
1336 Delta,
1337 0, /* alpha */
1338 GVSEL,
1339 PAN,
1340 VOL,
1341 CTRL,
1342 EC);
1343
1344 spin_unlock_irq(&codec->reg_lock);
1345
1346 return 0;
1347 }
1348
1349
1350 static snd_pcm_uframes_t
snd_ali_playback_pointer(struct snd_pcm_substream * substream)1351 snd_ali_playback_pointer(struct snd_pcm_substream *substream)
1352 {
1353 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1354 struct snd_pcm_runtime *runtime = substream->runtime;
1355 struct snd_ali_voice *pvoice = runtime->private_data;
1356 unsigned int cso;
1357
1358 guard(spinlock)(&codec->reg_lock);
1359 if (!pvoice->running)
1360 return 0;
1361 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1362 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1363 dev_dbg(codec->card->dev, "playback pointer returned cso=%xh.\n", cso);
1364
1365 cso %= runtime->buffer_size;
1366 return cso;
1367 }
1368
1369
snd_ali_pointer(struct snd_pcm_substream * substream)1370 static snd_pcm_uframes_t snd_ali_pointer(struct snd_pcm_substream *substream)
1371 {
1372 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1373 struct snd_pcm_runtime *runtime = substream->runtime;
1374 struct snd_ali_voice *pvoice = runtime->private_data;
1375 unsigned int cso;
1376
1377 guard(spinlock)(&codec->reg_lock);
1378 if (!pvoice->running)
1379 return 0;
1380 outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1381 cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1382
1383 cso %= runtime->buffer_size;
1384 return cso;
1385 }
1386
1387 static const struct snd_pcm_hardware snd_ali_playback =
1388 {
1389 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1390 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1391 SNDRV_PCM_INFO_MMAP_VALID |
1392 SNDRV_PCM_INFO_RESUME |
1393 SNDRV_PCM_INFO_SYNC_START),
1394 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1395 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1396 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1397 .rate_min = 4000,
1398 .rate_max = 48000,
1399 .channels_min = 1,
1400 .channels_max = 2,
1401 .buffer_bytes_max = (256*1024),
1402 .period_bytes_min = 64,
1403 .period_bytes_max = (256*1024),
1404 .periods_min = 1,
1405 .periods_max = 1024,
1406 .fifo_size = 0,
1407 };
1408
1409 /*
1410 * Capture support device description
1411 */
1412
1413 static const struct snd_pcm_hardware snd_ali_capture =
1414 {
1415 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1416 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1417 SNDRV_PCM_INFO_MMAP_VALID |
1418 SNDRV_PCM_INFO_RESUME |
1419 SNDRV_PCM_INFO_SYNC_START),
1420 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1421 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1422 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1423 .rate_min = 4000,
1424 .rate_max = 48000,
1425 .channels_min = 1,
1426 .channels_max = 2,
1427 .buffer_bytes_max = (128*1024),
1428 .period_bytes_min = 64,
1429 .period_bytes_max = (128*1024),
1430 .periods_min = 1,
1431 .periods_max = 1024,
1432 .fifo_size = 0,
1433 };
1434
snd_ali_pcm_free_substream(struct snd_pcm_runtime * runtime)1435 static void snd_ali_pcm_free_substream(struct snd_pcm_runtime *runtime)
1436 {
1437 struct snd_ali_voice *pvoice = runtime->private_data;
1438
1439 if (pvoice)
1440 snd_ali_free_voice(pvoice->codec, pvoice);
1441 }
1442
snd_ali_open(struct snd_pcm_substream * substream,int rec,int channel,const struct snd_pcm_hardware * phw)1443 static int snd_ali_open(struct snd_pcm_substream *substream, int rec,
1444 int channel, const struct snd_pcm_hardware *phw)
1445 {
1446 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1447 struct snd_pcm_runtime *runtime = substream->runtime;
1448 struct snd_ali_voice *pvoice;
1449
1450 pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, rec,
1451 channel);
1452 if (!pvoice)
1453 return -EAGAIN;
1454
1455 pvoice->substream = substream;
1456 runtime->private_data = pvoice;
1457 runtime->private_free = snd_ali_pcm_free_substream;
1458
1459 runtime->hw = *phw;
1460 snd_pcm_set_sync(substream);
1461 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1462 0, 64*1024);
1463 return 0;
1464 }
1465
snd_ali_playback_open(struct snd_pcm_substream * substream)1466 static int snd_ali_playback_open(struct snd_pcm_substream *substream)
1467 {
1468 return snd_ali_open(substream, 0, -1, &snd_ali_playback);
1469 }
1470
snd_ali_capture_open(struct snd_pcm_substream * substream)1471 static int snd_ali_capture_open(struct snd_pcm_substream *substream)
1472 {
1473 return snd_ali_open(substream, 1, -1, &snd_ali_capture);
1474 }
1475
snd_ali_playback_close(struct snd_pcm_substream * substream)1476 static int snd_ali_playback_close(struct snd_pcm_substream *substream)
1477 {
1478 return 0;
1479 }
1480
snd_ali_close(struct snd_pcm_substream * substream)1481 static int snd_ali_close(struct snd_pcm_substream *substream)
1482 {
1483 struct snd_ali *codec = snd_pcm_substream_chip(substream);
1484 struct snd_ali_voice *pvoice = substream->runtime->private_data;
1485
1486 snd_ali_disable_special_channel(codec,pvoice->number);
1487
1488 return 0;
1489 }
1490
1491 static const struct snd_pcm_ops snd_ali_playback_ops = {
1492 .open = snd_ali_playback_open,
1493 .close = snd_ali_playback_close,
1494 .hw_params = snd_ali_playback_hw_params,
1495 .hw_free = snd_ali_playback_hw_free,
1496 .prepare = snd_ali_playback_prepare,
1497 .trigger = snd_ali_trigger,
1498 .pointer = snd_ali_playback_pointer,
1499 };
1500
1501 static const struct snd_pcm_ops snd_ali_capture_ops = {
1502 .open = snd_ali_capture_open,
1503 .close = snd_ali_close,
1504 .prepare = snd_ali_prepare,
1505 .trigger = snd_ali_trigger,
1506 .pointer = snd_ali_pointer,
1507 };
1508
1509 /*
1510 * Modem PCM
1511 */
1512
snd_ali_modem_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)1513 static int snd_ali_modem_hw_params(struct snd_pcm_substream *substream,
1514 struct snd_pcm_hw_params *hw_params)
1515 {
1516 struct snd_ali *chip = snd_pcm_substream_chip(substream);
1517 unsigned int modem_num = chip->num_of_codecs - 1;
1518 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_RATE,
1519 params_rate(hw_params));
1520 snd_ac97_write(chip->ac97[modem_num], AC97_LINE1_LEVEL, 0);
1521 return 0;
1522 }
1523
1524 static const struct snd_pcm_hardware snd_ali_modem =
1525 {
1526 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1527 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1528 SNDRV_PCM_INFO_MMAP_VALID |
1529 SNDRV_PCM_INFO_RESUME |
1530 SNDRV_PCM_INFO_SYNC_START),
1531 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1532 .rates = (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000 |
1533 SNDRV_PCM_RATE_16000),
1534 .rate_min = 8000,
1535 .rate_max = 16000,
1536 .channels_min = 1,
1537 .channels_max = 1,
1538 .buffer_bytes_max = (256*1024),
1539 .period_bytes_min = 64,
1540 .period_bytes_max = (256*1024),
1541 .periods_min = 1,
1542 .periods_max = 1024,
1543 .fifo_size = 0,
1544 };
1545
snd_ali_modem_open(struct snd_pcm_substream * substream,int rec,int channel)1546 static int snd_ali_modem_open(struct snd_pcm_substream *substream, int rec,
1547 int channel)
1548 {
1549 static const unsigned int rates[] = {8000, 9600, 12000, 16000};
1550 static const struct snd_pcm_hw_constraint_list hw_constraint_rates = {
1551 .count = ARRAY_SIZE(rates),
1552 .list = rates,
1553 .mask = 0,
1554 };
1555 int err = snd_ali_open(substream, rec, channel, &snd_ali_modem);
1556
1557 if (err)
1558 return err;
1559 return snd_pcm_hw_constraint_list(substream->runtime, 0,
1560 SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
1561 }
1562
snd_ali_modem_playback_open(struct snd_pcm_substream * substream)1563 static int snd_ali_modem_playback_open(struct snd_pcm_substream *substream)
1564 {
1565 return snd_ali_modem_open(substream, 0, ALI_MODEM_OUT_CHANNEL);
1566 }
1567
snd_ali_modem_capture_open(struct snd_pcm_substream * substream)1568 static int snd_ali_modem_capture_open(struct snd_pcm_substream *substream)
1569 {
1570 return snd_ali_modem_open(substream, 1, ALI_MODEM_IN_CHANNEL);
1571 }
1572
1573 static const struct snd_pcm_ops snd_ali_modem_playback_ops = {
1574 .open = snd_ali_modem_playback_open,
1575 .close = snd_ali_close,
1576 .hw_params = snd_ali_modem_hw_params,
1577 .prepare = snd_ali_prepare,
1578 .trigger = snd_ali_trigger,
1579 .pointer = snd_ali_pointer,
1580 };
1581
1582 static const struct snd_pcm_ops snd_ali_modem_capture_ops = {
1583 .open = snd_ali_modem_capture_open,
1584 .close = snd_ali_close,
1585 .hw_params = snd_ali_modem_hw_params,
1586 .prepare = snd_ali_prepare,
1587 .trigger = snd_ali_trigger,
1588 .pointer = snd_ali_pointer,
1589 };
1590
1591
1592 struct ali_pcm_description {
1593 char *name;
1594 unsigned int playback_num;
1595 unsigned int capture_num;
1596 const struct snd_pcm_ops *playback_ops;
1597 const struct snd_pcm_ops *capture_ops;
1598 unsigned short class;
1599 };
1600
1601
snd_ali_pcm_free(struct snd_pcm * pcm)1602 static void snd_ali_pcm_free(struct snd_pcm *pcm)
1603 {
1604 struct snd_ali *codec = pcm->private_data;
1605 codec->pcm[pcm->device] = NULL;
1606 }
1607
1608
snd_ali_pcm(struct snd_ali * codec,int device,struct ali_pcm_description * desc)1609 static int snd_ali_pcm(struct snd_ali *codec, int device,
1610 struct ali_pcm_description *desc)
1611 {
1612 struct snd_pcm *pcm;
1613 int err;
1614
1615 err = snd_pcm_new(codec->card, desc->name, device,
1616 desc->playback_num, desc->capture_num, &pcm);
1617 if (err < 0) {
1618 dev_err(codec->card->dev,
1619 "snd_ali_pcm: err called snd_pcm_new.\n");
1620 return err;
1621 }
1622 pcm->private_data = codec;
1623 pcm->private_free = snd_ali_pcm_free;
1624 if (desc->playback_ops)
1625 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1626 desc->playback_ops);
1627 if (desc->capture_ops)
1628 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1629 desc->capture_ops);
1630
1631 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1632 &codec->pci->dev, 64*1024, 128*1024);
1633
1634 pcm->info_flags = 0;
1635 pcm->dev_class = desc->class;
1636 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1637 strscpy(pcm->name, desc->name);
1638 codec->pcm[0] = pcm;
1639 return 0;
1640 }
1641
1642 static struct ali_pcm_description ali_pcms[] = {
1643 { .name = "ALI 5451",
1644 .playback_num = ALI_CHANNELS,
1645 .capture_num = 1,
1646 .playback_ops = &snd_ali_playback_ops,
1647 .capture_ops = &snd_ali_capture_ops
1648 },
1649 { .name = "ALI 5451 modem",
1650 .playback_num = 1,
1651 .capture_num = 1,
1652 .playback_ops = &snd_ali_modem_playback_ops,
1653 .capture_ops = &snd_ali_modem_capture_ops,
1654 .class = SNDRV_PCM_CLASS_MODEM
1655 }
1656 };
1657
snd_ali_build_pcms(struct snd_ali * codec)1658 static int snd_ali_build_pcms(struct snd_ali *codec)
1659 {
1660 int i, err;
1661 for (i = 0; i < codec->num_of_codecs && i < ARRAY_SIZE(ali_pcms); i++) {
1662 err = snd_ali_pcm(codec, i, &ali_pcms[i]);
1663 if (err < 0)
1664 return err;
1665 }
1666 return 0;
1667 }
1668
1669
1670 #define ALI5451_SPDIF(xname, xindex, value) \
1671 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1672 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1673 .put = snd_ali5451_spdif_put, .private_value = value}
1674
1675 #define snd_ali5451_spdif_info snd_ctl_boolean_mono_info
1676
snd_ali5451_spdif_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1677 static int snd_ali5451_spdif_get(struct snd_kcontrol *kcontrol,
1678 struct snd_ctl_elem_value *ucontrol)
1679 {
1680 struct snd_ali *codec = snd_kcontrol_chip(kcontrol);
1681 unsigned int spdif_enable;
1682
1683 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
1684
1685 guard(spinlock_irq)(&codec->reg_lock);
1686 switch (kcontrol->private_value) {
1687 case 0:
1688 spdif_enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1689 break;
1690 case 1:
1691 spdif_enable = ((codec->spdif_mask & 0x02) &&
1692 (codec->spdif_mask & 0x04)) ? 1 : 0;
1693 break;
1694 case 2:
1695 spdif_enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1696 break;
1697 default:
1698 break;
1699 }
1700 ucontrol->value.integer.value[0] = spdif_enable;
1701 return 0;
1702 }
1703
snd_ali5451_spdif_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1704 static int snd_ali5451_spdif_put(struct snd_kcontrol *kcontrol,
1705 struct snd_ctl_elem_value *ucontrol)
1706 {
1707 struct snd_ali *codec = snd_kcontrol_chip(kcontrol);
1708 unsigned int change = 0, spdif_enable = 0;
1709
1710 spdif_enable = ucontrol->value.integer.value[0] ? 1 : 0;
1711
1712 guard(spinlock_irq)(&codec->reg_lock);
1713 switch (kcontrol->private_value) {
1714 case 0:
1715 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1716 change = change ^ spdif_enable;
1717 if (change) {
1718 if (spdif_enable) {
1719 codec->spdif_mask |= 0x02;
1720 snd_ali_enable_spdif_out(codec);
1721 } else {
1722 codec->spdif_mask &= ~(0x02);
1723 codec->spdif_mask &= ~(0x04);
1724 snd_ali_disable_spdif_out(codec);
1725 }
1726 }
1727 break;
1728 case 1:
1729 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1730 change = change ^ spdif_enable;
1731 if (change && (codec->spdif_mask & 0x02)) {
1732 if (spdif_enable) {
1733 codec->spdif_mask |= 0x04;
1734 snd_ali_enable_spdif_chnout(codec);
1735 } else {
1736 codec->spdif_mask &= ~(0x04);
1737 snd_ali_disable_spdif_chnout(codec);
1738 }
1739 }
1740 break;
1741 case 2:
1742 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1743 change = change ^ spdif_enable;
1744 if (change) {
1745 if (spdif_enable) {
1746 codec->spdif_mask |= 0x01;
1747 snd_ali_enable_spdif_in(codec);
1748 } else {
1749 codec->spdif_mask &= ~(0x01);
1750 snd_ali_disable_spdif_in(codec);
1751 }
1752 }
1753 break;
1754 default:
1755 break;
1756 }
1757
1758 return change;
1759 }
1760
1761 static const struct snd_kcontrol_new snd_ali5451_mixer_spdif[] = {
1762 /* spdif aplayback switch */
1763 /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1764 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH), 0, 0),
1765 /* spdif out to spdif channel */
1766 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("Channel Output ",NONE,SWITCH), 0, 1),
1767 /* spdif in from spdif channel */
1768 ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1769 };
1770
snd_ali_mixer(struct snd_ali * codec)1771 static int snd_ali_mixer(struct snd_ali *codec)
1772 {
1773 struct snd_ac97_template ac97;
1774 unsigned int idx;
1775 int i, err;
1776 static const struct snd_ac97_bus_ops ops = {
1777 .write = snd_ali_codec_write,
1778 .read = snd_ali_codec_read,
1779 };
1780
1781 err = snd_ac97_bus(codec->card, 0, &ops, codec, &codec->ac97_bus);
1782 if (err < 0)
1783 return err;
1784
1785 memset(&ac97, 0, sizeof(ac97));
1786 ac97.private_data = codec;
1787
1788 for (i = 0; i < codec->num_of_codecs; i++) {
1789 ac97.num = i;
1790 err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97[i]);
1791 if (err < 0) {
1792 dev_err(codec->card->dev,
1793 "ali mixer %d creating error.\n", i);
1794 if (i == 0)
1795 return err;
1796 codec->num_of_codecs = 1;
1797 break;
1798 }
1799 }
1800
1801 if (codec->spdif_support) {
1802 for (idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1803 err = snd_ctl_add(codec->card,
1804 snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1805 if (err < 0)
1806 return err;
1807 }
1808 }
1809 return 0;
1810 }
1811
ali_suspend(struct device * dev)1812 static int ali_suspend(struct device *dev)
1813 {
1814 struct snd_card *card = dev_get_drvdata(dev);
1815 struct snd_ali *chip = card->private_data;
1816 struct snd_ali_image *im = &chip->image;
1817 int i, j;
1818
1819 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1820 for (i = 0; i < chip->num_of_codecs; i++)
1821 snd_ac97_suspend(chip->ac97[i]);
1822
1823 guard(spinlock_irq)(&chip->reg_lock);
1824
1825 im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
1826 /* im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START)); */
1827 im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1828
1829 /* disable all IRQ bits */
1830 outl(0, ALI_REG(chip, ALI_MISCINT));
1831
1832 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1833 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1834 continue;
1835 im->regs[i] = inl(ALI_REG(chip, i*4));
1836 }
1837
1838 for (i = 0; i < ALI_CHANNELS; i++) {
1839 outb(i, ALI_REG(chip, ALI_GC_CIR));
1840 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1841 im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1842 }
1843
1844 /* stop all HW channel */
1845 outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1846
1847 return 0;
1848 }
1849
ali_resume(struct device * dev)1850 static int ali_resume(struct device *dev)
1851 {
1852 struct snd_card *card = dev_get_drvdata(dev);
1853 struct snd_ali *chip = card->private_data;
1854 struct snd_ali_image *im = &chip->image;
1855 int i, j;
1856
1857 scoped_guard(spinlock_irq, &chip->reg_lock) {
1858 for (i = 0; i < ALI_CHANNELS; i++) {
1859 outb(i, ALI_REG(chip, ALI_GC_CIR));
1860 for (j = 0; j < ALI_CHANNEL_REGS; j++)
1861 outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
1862 }
1863
1864 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
1865 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) ||
1866 (i*4 == ALI_START))
1867 continue;
1868 outl(im->regs[i], ALI_REG(chip, i*4));
1869 }
1870
1871 /* start HW channel */
1872 outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
1873 /* restore IRQ enable bits */
1874 outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
1875 }
1876
1877 for (i = 0 ; i < chip->num_of_codecs; i++)
1878 snd_ac97_resume(chip->ac97[i]);
1879
1880 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1881 return 0;
1882 }
1883
1884 static DEFINE_SIMPLE_DEV_PM_OPS(ali_pm, ali_suspend, ali_resume);
1885
snd_ali_free(struct snd_card * card)1886 static void snd_ali_free(struct snd_card *card)
1887 {
1888 struct snd_ali *codec = card->private_data;
1889
1890 if (codec->hw_initialized)
1891 snd_ali_disable_address_interrupt(codec);
1892 pci_dev_put(codec->pci_m1533);
1893 pci_dev_put(codec->pci_m7101);
1894 }
1895
snd_ali_chip_init(struct snd_ali * codec)1896 static int snd_ali_chip_init(struct snd_ali *codec)
1897 {
1898 unsigned int legacy;
1899 unsigned char temp;
1900 struct pci_dev *pci_dev;
1901
1902 dev_dbg(codec->card->dev, "chip initializing ...\n");
1903
1904 if (snd_ali_reset_5451(codec)) {
1905 dev_err(codec->card->dev, "ali_chip_init: reset 5451 error.\n");
1906 return -1;
1907 }
1908
1909 if (codec->revision == ALI_5451_V02) {
1910 pci_dev = codec->pci_m1533;
1911 pci_read_config_byte(pci_dev, 0x59, &temp);
1912 temp |= 0x80;
1913 pci_write_config_byte(pci_dev, 0x59, temp);
1914
1915 pci_dev = codec->pci_m7101;
1916 pci_read_config_byte(pci_dev, 0xb8, &temp);
1917 temp |= 0x20;
1918 pci_write_config_byte(pci_dev, 0xB8, temp);
1919 }
1920
1921 pci_read_config_dword(codec->pci, 0x44, &legacy);
1922 legacy &= 0xff00ff00;
1923 legacy |= 0x000800aa;
1924 pci_write_config_dword(codec->pci, 0x44, legacy);
1925
1926 outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
1927 outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
1928 outl(0xffffffff, ALI_REG(codec, ALI_AINT));
1929 outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
1930 outb(0x10, ALI_REG(codec, ALI_MPUR2));
1931
1932 codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
1933 codec->ac97_ext_status = snd_ali_codec_peek(codec, 0,
1934 AC97_EXTENDED_STATUS);
1935 if (codec->spdif_support) {
1936 snd_ali_enable_spdif_out(codec);
1937 codec->spdif_mask = 0x00000002;
1938 }
1939
1940 codec->num_of_codecs = 1;
1941
1942 /* secondary codec - modem */
1943 if (inl(ALI_REG(codec, ALI_SCTRL)) & ALI_SCTRL_CODEC2_READY) {
1944 codec->num_of_codecs++;
1945 outl(inl(ALI_REG(codec, ALI_SCTRL)) |
1946 (ALI_SCTRL_LINE_IN2 | ALI_SCTRL_GPIO_IN2 |
1947 ALI_SCTRL_LINE_OUT_EN),
1948 ALI_REG(codec, ALI_SCTRL));
1949 }
1950
1951 dev_dbg(codec->card->dev, "chip initialize succeed.\n");
1952 return 0;
1953
1954 }
1955
1956 /* proc for register dump */
snd_ali_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buf)1957 static void snd_ali_proc_read(struct snd_info_entry *entry,
1958 struct snd_info_buffer *buf)
1959 {
1960 struct snd_ali *codec = entry->private_data;
1961 int i;
1962 for (i = 0; i < 256 ; i+= 4)
1963 snd_iprintf(buf, "%02x: %08x\n", i, inl(ALI_REG(codec, i)));
1964 }
1965
snd_ali_proc_init(struct snd_ali * codec)1966 static void snd_ali_proc_init(struct snd_ali *codec)
1967 {
1968 snd_card_ro_proc_new(codec->card, "ali5451", codec, snd_ali_proc_read);
1969 }
1970
snd_ali_resources(struct snd_ali * codec)1971 static int snd_ali_resources(struct snd_ali *codec)
1972 {
1973 int err;
1974
1975 dev_dbg(codec->card->dev, "resources allocation ...\n");
1976 err = pcim_request_all_regions(codec->pci, "ALI 5451");
1977 if (err < 0)
1978 return err;
1979 codec->port = pci_resource_start(codec->pci, 0);
1980
1981 if (devm_request_irq(&codec->pci->dev, codec->pci->irq,
1982 snd_ali_card_interrupt,
1983 IRQF_SHARED, KBUILD_MODNAME, codec)) {
1984 dev_err(codec->card->dev, "Unable to request irq.\n");
1985 return -EBUSY;
1986 }
1987 codec->irq = codec->pci->irq;
1988 codec->card->sync_irq = codec->irq;
1989 dev_dbg(codec->card->dev, "resources allocated.\n");
1990 return 0;
1991 }
1992
snd_ali_create(struct snd_card * card,struct pci_dev * pci,int pcm_streams,int spdif_support)1993 static int snd_ali_create(struct snd_card *card,
1994 struct pci_dev *pci,
1995 int pcm_streams,
1996 int spdif_support)
1997 {
1998 struct snd_ali *codec = card->private_data;
1999 int i, err;
2000 unsigned short cmdw;
2001
2002 dev_dbg(card->dev, "creating ...\n");
2003
2004 /* enable PCI device */
2005 err = pcim_enable_device(pci);
2006 if (err < 0)
2007 return err;
2008 /* check, if we can restrict PCI DMA transfers to 31 bits */
2009 if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(31))) {
2010 dev_err(card->dev,
2011 "architecture does not support 31bit PCI busmaster DMA\n");
2012 return -ENXIO;
2013 }
2014
2015 spin_lock_init(&codec->reg_lock);
2016 spin_lock_init(&codec->voice_alloc);
2017
2018 codec->card = card;
2019 codec->pci = pci;
2020 codec->irq = -1;
2021 codec->revision = pci->revision;
2022 codec->spdif_support = spdif_support;
2023
2024 if (pcm_streams < 1)
2025 pcm_streams = 1;
2026 if (pcm_streams > 32)
2027 pcm_streams = 32;
2028
2029 pci_set_master(pci);
2030 pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2031 if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2032 cmdw |= PCI_COMMAND_IO;
2033 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2034 }
2035
2036 if (snd_ali_resources(codec))
2037 return -EBUSY;
2038 card->private_free = snd_ali_free;
2039
2040 codec->synth.chmap = 0;
2041 codec->synth.chcnt = 0;
2042 codec->spdif_mask = 0;
2043 codec->synth.synthcount = 0;
2044
2045 if (codec->revision == ALI_5451_V02)
2046 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2047 else
2048 codec->chregs.regs.ac97read = ALI_AC97_READ;
2049 codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2050
2051 codec->chregs.regs.start = ALI_START;
2052 codec->chregs.regs.stop = ALI_STOP;
2053 codec->chregs.regs.aint = ALI_AINT;
2054 codec->chregs.regs.ainten = ALI_AINTEN;
2055
2056 codec->chregs.data.start = 0x00;
2057 codec->chregs.data.stop = 0x00;
2058 codec->chregs.data.aint = 0x00;
2059 codec->chregs.data.ainten = 0x00;
2060
2061 /* M1533: southbridge */
2062 codec->pci_m1533 = pci_get_device(0x10b9, 0x1533, NULL);
2063 if (!codec->pci_m1533) {
2064 dev_err(card->dev, "cannot find ALi 1533 chip.\n");
2065 return -ENODEV;
2066 }
2067 /* M7101: power management */
2068 codec->pci_m7101 = pci_get_device(0x10b9, 0x7101, NULL);
2069 if (!codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2070 dev_err(card->dev, "cannot find ALi 7101 chip.\n");
2071 return -ENODEV;
2072 }
2073
2074 /* initialise synth voices*/
2075 for (i = 0; i < ALI_CHANNELS; i++)
2076 codec->synth.voices[i].number = i;
2077
2078 err = snd_ali_chip_init(codec);
2079 if (err < 0) {
2080 dev_err(card->dev, "ali create: chip init error.\n");
2081 return err;
2082 }
2083
2084 snd_ali_enable_address_interrupt(codec);
2085 codec->hw_initialized = 1;
2086 return 0;
2087 }
2088
__snd_ali_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)2089 static int __snd_ali_probe(struct pci_dev *pci,
2090 const struct pci_device_id *pci_id)
2091 {
2092 struct snd_card *card;
2093 struct snd_ali *codec;
2094 int err;
2095
2096 dev_dbg(&pci->dev, "probe ...\n");
2097
2098 err = snd_devm_card_new(&pci->dev, index, id, THIS_MODULE,
2099 sizeof(*codec), &card);
2100 if (err < 0)
2101 return err;
2102 codec = card->private_data;
2103
2104 err = snd_ali_create(card, pci, pcm_channels, spdif);
2105 if (err < 0)
2106 return err;
2107
2108 dev_dbg(&pci->dev, "mixer building ...\n");
2109 err = snd_ali_mixer(codec);
2110 if (err < 0)
2111 return err;
2112
2113 dev_dbg(&pci->dev, "pcm building ...\n");
2114 err = snd_ali_build_pcms(codec);
2115 if (err < 0)
2116 return err;
2117
2118 snd_ali_proc_init(codec);
2119
2120 strscpy(card->driver, "ALI5451");
2121 strscpy(card->shortname, "ALI 5451");
2122
2123 sprintf(card->longname, "%s at 0x%lx, irq %i",
2124 card->shortname, codec->port, codec->irq);
2125
2126 dev_dbg(&pci->dev, "register card.\n");
2127 err = snd_card_register(card);
2128 if (err < 0)
2129 return err;
2130
2131 pci_set_drvdata(pci, card);
2132 return 0;
2133 }
2134
snd_ali_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)2135 static int snd_ali_probe(struct pci_dev *pci,
2136 const struct pci_device_id *pci_id)
2137 {
2138 return snd_card_free_on_error(&pci->dev, __snd_ali_probe(pci, pci_id));
2139 }
2140
2141 static struct pci_driver ali5451_driver = {
2142 .name = KBUILD_MODNAME,
2143 .id_table = snd_ali_ids,
2144 .probe = snd_ali_probe,
2145 .driver = {
2146 .pm = &ali_pm,
2147 },
2148 };
2149
2150 module_pci_driver(ali5451_driver);
2151