blob: 4ef3caaf4354b5cf62c16b36f9f009d9096b5d0e [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 card-opti92x-ad1848.c - driver for OPTi 82c92x based soundcards.
3 Copyright (C) 1998-2000 by Massimo Piccioni <dafastidio@libero.it>
4
5 Part of this code was developed at the Italian Ministry of Air Defence,
6 Sixth Division (oh, che pace ...), Rome.
7
8 Thanks to Maria Grazia Pollarini, Salvatore Vassallo.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23*/
24
25
26#include <linux/init.h>
27#include <linux/err.h>
28#include <linux/isa.h>
29#include <linux/delay.h>
30#include <linux/pnp.h>
31#include <linux/module.h>
32#include <linux/io.h>
33#include <asm/dma.h>
34#include <sound/core.h>
35#include <sound/tlv.h>
36#include <sound/wss.h>
37#include <sound/mpu401.h>
38#include <sound/opl3.h>
39#ifndef OPTi93X
40#include <sound/opl4.h>
41#endif
42#define SNDRV_LEGACY_FIND_FREE_IOPORT
43#define SNDRV_LEGACY_FIND_FREE_IRQ
44#define SNDRV_LEGACY_FIND_FREE_DMA
45#include <sound/initval.h>
46
47MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
48MODULE_LICENSE("GPL");
49#ifdef OPTi93X
50MODULE_DESCRIPTION("OPTi93X");
51MODULE_SUPPORTED_DEVICE("{{OPTi,82C931/3}}");
52#else /* OPTi93X */
53#ifdef CS4231
54MODULE_DESCRIPTION("OPTi92X - CS4231");
55MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (CS4231)},"
56 "{OPTi,82C925 (CS4231)}}");
57#else /* CS4231 */
58MODULE_DESCRIPTION("OPTi92X - AD1848");
59MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (AD1848)},"
60 "{OPTi,82C925 (AD1848)},"
61 "{OAK,Mozart}}");
62#endif /* CS4231 */
63#endif /* OPTi93X */
64
65static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
66static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
67//static bool enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */
68#ifdef CONFIG_PNP
69static bool isapnp = true; /* Enable ISA PnP detection */
70#endif
71static long port = SNDRV_DEFAULT_PORT1; /* 0x530,0xe80,0xf40,0x604 */
72static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */
73static long fm_port = SNDRV_DEFAULT_PORT1; /* 0x388 */
74static int irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10,11 */
75static int mpu_irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10 */
76static int dma1 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
77#if defined(CS4231) || defined(OPTi93X)
78static int dma2 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
79#endif /* CS4231 || OPTi93X */
80
81module_param(index, int, 0444);
82MODULE_PARM_DESC(index, "Index value for opti9xx based soundcard.");
83module_param(id, charp, 0444);
84MODULE_PARM_DESC(id, "ID string for opti9xx based soundcard.");
85//module_param(enable, bool, 0444);
86//MODULE_PARM_DESC(enable, "Enable opti9xx soundcard.");
87#ifdef CONFIG_PNP
88module_param(isapnp, bool, 0444);
89MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
90#endif
91module_param_hw(port, long, ioport, 0444);
92MODULE_PARM_DESC(port, "WSS port # for opti9xx driver.");
93module_param_hw(mpu_port, long, ioport, 0444);
94MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver.");
95module_param_hw(fm_port, long, ioport, 0444);
96MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver.");
97module_param_hw(irq, int, irq, 0444);
98MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver.");
99module_param_hw(mpu_irq, int, irq, 0444);
100MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver.");
101module_param_hw(dma1, int, dma, 0444);
102MODULE_PARM_DESC(dma1, "1st dma # for opti9xx driver.");
103#if defined(CS4231) || defined(OPTi93X)
104module_param_hw(dma2, int, dma, 0444);
105MODULE_PARM_DESC(dma2, "2nd dma # for opti9xx driver.");
106#endif /* CS4231 || OPTi93X */
107
108#define OPTi9XX_HW_82C928 1
109#define OPTi9XX_HW_82C929 2
110#define OPTi9XX_HW_82C924 3
111#define OPTi9XX_HW_82C925 4
112#define OPTi9XX_HW_82C930 5
113#define OPTi9XX_HW_82C931 6
114#define OPTi9XX_HW_82C933 7
115#define OPTi9XX_HW_LAST OPTi9XX_HW_82C933
116
117#define OPTi9XX_MC_REG(n) n
118
119#ifdef OPTi93X
120
121#define OPTi93X_STATUS 0x02
122#define OPTi93X_PORT(chip, r) ((chip)->port + OPTi93X_##r)
123
124#define OPTi93X_IRQ_PLAYBACK 0x04
125#define OPTi93X_IRQ_CAPTURE 0x08
126
127#endif /* OPTi93X */
128
129struct snd_opti9xx {
130 unsigned short hardware;
131 unsigned char password;
132 char name[7];
133
134 unsigned long mc_base;
135 struct resource *res_mc_base;
136 unsigned long mc_base_size;
137#ifdef OPTi93X
138 unsigned long mc_indir_index;
139 struct resource *res_mc_indir;
140#endif /* OPTi93X */
141 struct snd_wss *codec;
142 unsigned long pwd_reg;
143
144 spinlock_t lock;
145
146 long wss_base;
147 int irq;
148};
149
150static int snd_opti9xx_pnp_is_probed;
151
152#ifdef CONFIG_PNP
153
154static const struct pnp_card_device_id snd_opti9xx_pnpids[] = {
155#ifndef OPTi93X
156 /* OPTi 82C924 */
157 { .id = "OPT0924",
158 .devs = { { "OPT0000" }, { "OPT0002" }, { "OPT0005" } },
159 .driver_data = 0x0924 },
160 /* OPTi 82C925 */
161 { .id = "OPT0925",
162 .devs = { { "OPT9250" }, { "OPT0002" }, { "OPT0005" } },
163 .driver_data = 0x0925 },
164#else
165 /* OPTi 82C931/3 */
166 { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } },
167 .driver_data = 0x0931 },
168#endif /* OPTi93X */
169 { .id = "" }
170};
171
172MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids);
173
174#endif /* CONFIG_PNP */
175
176#define DEV_NAME KBUILD_MODNAME
177
178static char * snd_opti9xx_names[] = {
179 "unknown",
180 "82C928", "82C929",
181 "82C924", "82C925",
182 "82C930", "82C931", "82C933"
183};
184
185static int snd_opti9xx_init(struct snd_opti9xx *chip,
186 unsigned short hardware)
187{
188 static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
189
190 chip->hardware = hardware;
191 strcpy(chip->name, snd_opti9xx_names[hardware]);
192
193 spin_lock_init(&chip->lock);
194
195 chip->irq = -1;
196
197#ifndef OPTi93X
198#ifdef CONFIG_PNP
199 if (isapnp && chip->mc_base)
200 /* PnP resource gives the least 10 bits */
201 chip->mc_base |= 0xc00;
202 else
203#endif /* CONFIG_PNP */
204 {
205 chip->mc_base = 0xf8c;
206 chip->mc_base_size = opti9xx_mc_size[hardware];
207 }
208#else
209 chip->mc_base_size = opti9xx_mc_size[hardware];
210#endif
211
212 switch (hardware) {
213#ifndef OPTi93X
214 case OPTi9XX_HW_82C928:
215 case OPTi9XX_HW_82C929:
216 chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3;
217 chip->pwd_reg = 3;
218 break;
219
220 case OPTi9XX_HW_82C924:
221 case OPTi9XX_HW_82C925:
222 chip->password = 0xe5;
223 chip->pwd_reg = 3;
224 break;
225#else /* OPTi93X */
226
227 case OPTi9XX_HW_82C930:
228 case OPTi9XX_HW_82C931:
229 case OPTi9XX_HW_82C933:
230 chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d;
231 if (!chip->mc_indir_index)
232 chip->mc_indir_index = 0xe0e;
233 chip->password = 0xe4;
234 chip->pwd_reg = 0;
235 break;
236#endif /* OPTi93X */
237
238 default:
239 snd_printk(KERN_ERR "chip %d not supported\n", hardware);
240 return -ENODEV;
241 }
242 return 0;
243}
244
245static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip,
246 unsigned char reg)
247{
248 unsigned long flags;
249 unsigned char retval = 0xff;
250
251 spin_lock_irqsave(&chip->lock, flags);
252 outb(chip->password, chip->mc_base + chip->pwd_reg);
253
254 switch (chip->hardware) {
255#ifndef OPTi93X
256 case OPTi9XX_HW_82C924:
257 case OPTi9XX_HW_82C925:
258 if (reg > 7) {
259 outb(reg, chip->mc_base + 8);
260 outb(chip->password, chip->mc_base + chip->pwd_reg);
261 retval = inb(chip->mc_base + 9);
262 break;
263 }
264
265 case OPTi9XX_HW_82C928:
266 case OPTi9XX_HW_82C929:
267 retval = inb(chip->mc_base + reg);
268 break;
269#else /* OPTi93X */
270
271 case OPTi9XX_HW_82C930:
272 case OPTi9XX_HW_82C931:
273 case OPTi9XX_HW_82C933:
274 outb(reg, chip->mc_indir_index);
275 outb(chip->password, chip->mc_base + chip->pwd_reg);
276 retval = inb(chip->mc_indir_index + 1);
277 break;
278#endif /* OPTi93X */
279
280 default:
281 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
282 }
283
284 spin_unlock_irqrestore(&chip->lock, flags);
285 return retval;
286}
287
288static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg,
289 unsigned char value)
290{
291 unsigned long flags;
292
293 spin_lock_irqsave(&chip->lock, flags);
294 outb(chip->password, chip->mc_base + chip->pwd_reg);
295
296 switch (chip->hardware) {
297#ifndef OPTi93X
298 case OPTi9XX_HW_82C924:
299 case OPTi9XX_HW_82C925:
300 if (reg > 7) {
301 outb(reg, chip->mc_base + 8);
302 outb(chip->password, chip->mc_base + chip->pwd_reg);
303 outb(value, chip->mc_base + 9);
304 break;
305 }
306
307 case OPTi9XX_HW_82C928:
308 case OPTi9XX_HW_82C929:
309 outb(value, chip->mc_base + reg);
310 break;
311#else /* OPTi93X */
312
313 case OPTi9XX_HW_82C930:
314 case OPTi9XX_HW_82C931:
315 case OPTi9XX_HW_82C933:
316 outb(reg, chip->mc_indir_index);
317 outb(chip->password, chip->mc_base + chip->pwd_reg);
318 outb(value, chip->mc_indir_index + 1);
319 break;
320#endif /* OPTi93X */
321
322 default:
323 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
324 }
325
326 spin_unlock_irqrestore(&chip->lock, flags);
327}
328
329
330static inline void snd_opti9xx_write_mask(struct snd_opti9xx *chip,
331 unsigned char reg, unsigned char value, unsigned char mask)
332{
333 unsigned char oldval = snd_opti9xx_read(chip, reg);
334
335 snd_opti9xx_write(chip, reg, (oldval & ~mask) | (value & mask));
336}
337
338static int snd_opti9xx_configure(struct snd_opti9xx *chip,
339 long port,
340 int irq, int dma1, int dma2,
341 long mpu_port, int mpu_irq)
342{
343 unsigned char wss_base_bits;
344 unsigned char irq_bits;
345 unsigned char dma_bits;
346 unsigned char mpu_port_bits = 0;
347 unsigned char mpu_irq_bits;
348
349 switch (chip->hardware) {
350#ifndef OPTi93X
351 case OPTi9XX_HW_82C924:
352 /* opti 929 mode (?), OPL3 clock output, audio enable */
353 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
354 /* enable wave audio */
355 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
356
357 case OPTi9XX_HW_82C925:
358 /* enable WSS mode */
359 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
360 /* OPL3 FM synthesis */
361 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
362 /* disable Sound Blaster IRQ and DMA */
363 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
364#ifdef CS4231
365 /* cs4231/4248 fix enabled */
366 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
367#else
368 /* cs4231/4248 fix disabled */
369 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
370#endif /* CS4231 */
371 break;
372
373 case OPTi9XX_HW_82C928:
374 case OPTi9XX_HW_82C929:
375 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
376 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
377 /*
378 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae);
379 */
380 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
381#ifdef CS4231
382 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
383#else
384 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
385#endif /* CS4231 */
386 break;
387
388#else /* OPTi93X */
389 case OPTi9XX_HW_82C931:
390 /* disable 3D sound (set GPIO1 as output, low) */
391 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(20), 0x04, 0x0c);
392 case OPTi9XX_HW_82C933: /* FALL THROUGH */
393 /*
394 * The BTC 1817DW has QS1000 wavetable which is connected
395 * to the serial digital input of the OPTI931.
396 */
397 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff);
398 /*
399 * This bit sets OPTI931 to automaticaly select FM
400 * or digital input signal.
401 */
402 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01);
403 case OPTi9XX_HW_82C930: /* FALL THROUGH */
404 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
405 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
406 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
407 (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
408 0x34);
409 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
410 break;
411#endif /* OPTi93X */
412
413 default:
414 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
415 return -EINVAL;
416 }
417
418 /* PnP resource says it decodes only 10 bits of address */
419 switch (port & 0x3ff) {
420 case 0x130:
421 chip->wss_base = 0x530;
422 wss_base_bits = 0x00;
423 break;
424 case 0x204:
425 chip->wss_base = 0x604;
426 wss_base_bits = 0x03;
427 break;
428 case 0x280:
429 chip->wss_base = 0xe80;
430 wss_base_bits = 0x01;
431 break;
432 case 0x340:
433 chip->wss_base = 0xf40;
434 wss_base_bits = 0x02;
435 break;
436 default:
437 snd_printk(KERN_WARNING "WSS port 0x%lx not valid\n", port);
438 goto __skip_base;
439 }
440 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
441
442__skip_base:
443 switch (irq) {
444//#ifdef OPTi93X
445 case 5:
446 irq_bits = 0x05;
447 break;
448//#endif /* OPTi93X */
449 case 7:
450 irq_bits = 0x01;
451 break;
452 case 9:
453 irq_bits = 0x02;
454 break;
455 case 10:
456 irq_bits = 0x03;
457 break;
458 case 11:
459 irq_bits = 0x04;
460 break;
461 default:
462 snd_printk(KERN_WARNING "WSS irq # %d not valid\n", irq);
463 goto __skip_resources;
464 }
465
466 switch (dma1) {
467 case 0:
468 dma_bits = 0x01;
469 break;
470 case 1:
471 dma_bits = 0x02;
472 break;
473 case 3:
474 dma_bits = 0x03;
475 break;
476 default:
477 snd_printk(KERN_WARNING "WSS dma1 # %d not valid\n", dma1);
478 goto __skip_resources;
479 }
480
481#if defined(CS4231) || defined(OPTi93X)
482 if (dma1 == dma2) {
483 snd_printk(KERN_ERR "don't want to share dmas\n");
484 return -EBUSY;
485 }
486
487 switch (dma2) {
488 case 0:
489 case 1:
490 break;
491 default:
492 snd_printk(KERN_WARNING "WSS dma2 # %d not valid\n", dma2);
493 goto __skip_resources;
494 }
495 dma_bits |= 0x04;
496#endif /* CS4231 || OPTi93X */
497
498#ifndef OPTi93X
499 outb(irq_bits << 3 | dma_bits, chip->wss_base);
500#else /* OPTi93X */
501 snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
502#endif /* OPTi93X */
503
504__skip_resources:
505 if (chip->hardware > OPTi9XX_HW_82C928) {
506 switch (mpu_port) {
507 case 0:
508 case -1:
509 break;
510 case 0x300:
511 mpu_port_bits = 0x03;
512 break;
513 case 0x310:
514 mpu_port_bits = 0x02;
515 break;
516 case 0x320:
517 mpu_port_bits = 0x01;
518 break;
519 case 0x330:
520 mpu_port_bits = 0x00;
521 break;
522 default:
523 snd_printk(KERN_WARNING
524 "MPU-401 port 0x%lx not valid\n", mpu_port);
525 goto __skip_mpu;
526 }
527
528 switch (mpu_irq) {
529 case 5:
530 mpu_irq_bits = 0x02;
531 break;
532 case 7:
533 mpu_irq_bits = 0x03;
534 break;
535 case 9:
536 mpu_irq_bits = 0x00;
537 break;
538 case 10:
539 mpu_irq_bits = 0x01;
540 break;
541 default:
542 snd_printk(KERN_WARNING "MPU-401 irq # %d not valid\n",
543 mpu_irq);
544 goto __skip_mpu;
545 }
546
547 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
548 (mpu_port <= 0) ? 0x00 :
549 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
550 0xf8);
551 }
552__skip_mpu:
553
554 return 0;
555}
556
557#ifdef OPTi93X
558
559static const DECLARE_TLV_DB_SCALE(db_scale_5bit_3db_step, -9300, 300, 0);
560static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
561static const DECLARE_TLV_DB_SCALE(db_scale_4bit_12db_max, -3300, 300, 0);
562
563static struct snd_kcontrol_new snd_opti93x_controls[] = {
564WSS_DOUBLE("Master Playback Switch", 0,
565 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
566WSS_DOUBLE_TLV("Master Playback Volume", 0,
567 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1,
568 db_scale_5bit_3db_step),
569WSS_DOUBLE_TLV("PCM Playback Volume", 0,
570 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1,
571 db_scale_5bit),
572WSS_DOUBLE_TLV("FM Playback Volume", 0,
573 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1,
574 db_scale_4bit_12db_max),
575WSS_DOUBLE("Line Playback Switch", 0,
576 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
577WSS_DOUBLE_TLV("Line Playback Volume", 0,
578 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1,
579 db_scale_4bit_12db_max),
580WSS_DOUBLE("Mic Playback Switch", 0,
581 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
582WSS_DOUBLE_TLV("Mic Playback Volume", 0,
583 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1,
584 db_scale_4bit_12db_max),
585WSS_DOUBLE_TLV("CD Playback Volume", 0,
586 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1,
587 db_scale_4bit_12db_max),
588WSS_DOUBLE("Aux Playback Switch", 0,
589 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
590WSS_DOUBLE_TLV("Aux Playback Volume", 0,
591 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1,
592 db_scale_4bit_12db_max),
593};
594
595static int snd_opti93x_mixer(struct snd_wss *chip)
596{
597 struct snd_card *card;
598 unsigned int idx;
599 struct snd_ctl_elem_id id1, id2;
600 int err;
601
602 if (snd_BUG_ON(!chip || !chip->pcm))
603 return -EINVAL;
604
605 card = chip->card;
606
607 strcpy(card->mixername, chip->pcm->name);
608
609 memset(&id1, 0, sizeof(id1));
610 memset(&id2, 0, sizeof(id2));
611 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
612 /* reassign AUX0 switch to CD */
613 strcpy(id1.name, "Aux Playback Switch");
614 strcpy(id2.name, "CD Playback Switch");
615 err = snd_ctl_rename_id(card, &id1, &id2);
616 if (err < 0) {
617 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
618 return err;
619 }
620 /* reassign AUX1 switch to FM */
621 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
622 strcpy(id2.name, "FM Playback Switch");
623 err = snd_ctl_rename_id(card, &id1, &id2);
624 if (err < 0) {
625 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
626 return err;
627 }
628 /* remove AUX1 volume */
629 strcpy(id1.name, "Aux Playback Volume"); id1.index = 1;
630 snd_ctl_remove_id(card, &id1);
631
632 /* Replace WSS volume controls with OPTi93x volume controls */
633 id1.index = 0;
634 for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
635 strcpy(id1.name, snd_opti93x_controls[idx].name);
636 snd_ctl_remove_id(card, &id1);
637
638 err = snd_ctl_add(card,
639 snd_ctl_new1(&snd_opti93x_controls[idx], chip));
640 if (err < 0)
641 return err;
642 }
643 return 0;
644}
645
646static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id)
647{
648 struct snd_opti9xx *chip = dev_id;
649 struct snd_wss *codec = chip->codec;
650 unsigned char status;
651
652 if (!codec)
653 return IRQ_HANDLED;
654
655 status = snd_opti9xx_read(chip, OPTi9XX_MC_REG(11));
656 if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
657 snd_pcm_period_elapsed(codec->playback_substream);
658 if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
659 snd_wss_overrange(codec);
660 snd_pcm_period_elapsed(codec->capture_substream);
661 }
662 outb(0x00, OPTi93X_PORT(codec, STATUS));
663 return IRQ_HANDLED;
664}
665
666#endif /* OPTi93X */
667
668static int snd_opti9xx_read_check(struct snd_opti9xx *chip)
669{
670 unsigned char value;
671#ifdef OPTi93X
672 unsigned long flags;
673#endif
674
675 chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
676 "OPTi9xx MC");
677 if (chip->res_mc_base == NULL)
678 return -EBUSY;
679#ifndef OPTi93X
680 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
681 if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
682 if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
683 return 0;
684#else /* OPTi93X */
685 chip->res_mc_indir = request_region(chip->mc_indir_index, 2,
686 "OPTi93x MC");
687 if (chip->res_mc_indir == NULL)
688 return -EBUSY;
689
690 spin_lock_irqsave(&chip->lock, flags);
691 outb(chip->password, chip->mc_base + chip->pwd_reg);
692 outb(((chip->mc_indir_index & 0x1f0) >> 4), chip->mc_base);
693 spin_unlock_irqrestore(&chip->lock, flags);
694
695 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
696 snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
697 if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
698 return 0;
699
700 release_and_free_resource(chip->res_mc_indir);
701 chip->res_mc_indir = NULL;
702#endif /* OPTi93X */
703 release_and_free_resource(chip->res_mc_base);
704 chip->res_mc_base = NULL;
705
706 return -ENODEV;
707}
708
709static int snd_card_opti9xx_detect(struct snd_card *card,
710 struct snd_opti9xx *chip)
711{
712 int i, err;
713
714#ifndef OPTi93X
715 for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
716#else
717 for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
718#endif
719 err = snd_opti9xx_init(chip, i);
720 if (err < 0)
721 return err;
722
723 err = snd_opti9xx_read_check(chip);
724 if (err == 0)
725 return 1;
726#ifdef OPTi93X
727 chip->mc_indir_index = 0;
728#endif
729 }
730 return -ENODEV;
731}
732
733#ifdef CONFIG_PNP
734static int snd_card_opti9xx_pnp(struct snd_opti9xx *chip,
735 struct pnp_card_link *card,
736 const struct pnp_card_device_id *pid)
737{
738 struct pnp_dev *pdev;
739 int err;
740 struct pnp_dev *devmpu;
741#ifndef OPTi93X
742 struct pnp_dev *devmc;
743#endif
744
745 pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
746 if (pdev == NULL)
747 return -EBUSY;
748
749 err = pnp_activate_dev(pdev);
750 if (err < 0) {
751 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
752 return err;
753 }
754
755#ifdef OPTi93X
756 port = pnp_port_start(pdev, 0) - 4;
757 fm_port = pnp_port_start(pdev, 1) + 8;
758 /* adjust mc_indir_index - some cards report it at 0xe?d,
759 other at 0xe?c but it really is always at 0xe?e */
760 chip->mc_indir_index = (pnp_port_start(pdev, 3) & ~0xf) | 0xe;
761#else
762 devmc = pnp_request_card_device(card, pid->devs[2].id, NULL);
763 if (devmc == NULL)
764 return -EBUSY;
765
766 err = pnp_activate_dev(devmc);
767 if (err < 0) {
768 snd_printk(KERN_ERR "MC pnp configure failure: %d\n", err);
769 return err;
770 }
771
772 port = pnp_port_start(pdev, 1);
773 fm_port = pnp_port_start(pdev, 2) + 8;
774 /*
775 * The MC(0) is never accessed and card does not
776 * include it in the PnP resource range. OPTI93x include it.
777 */
778 chip->mc_base = pnp_port_start(devmc, 0) - 1;
779 chip->mc_base_size = pnp_port_len(devmc, 0) + 1;
780#endif /* OPTi93X */
781 irq = pnp_irq(pdev, 0);
782 dma1 = pnp_dma(pdev, 0);
783#if defined(CS4231) || defined(OPTi93X)
784 dma2 = pnp_dma(pdev, 1);
785#endif /* CS4231 || OPTi93X */
786
787 devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
788
789 if (devmpu && mpu_port > 0) {
790 err = pnp_activate_dev(devmpu);
791 if (err < 0) {
792 snd_printk(KERN_ERR "MPU401 pnp configure failure\n");
793 mpu_port = -1;
794 } else {
795 mpu_port = pnp_port_start(devmpu, 0);
796 mpu_irq = pnp_irq(devmpu, 0);
797 }
798 }
799 return pid->driver_data;
800}
801#endif /* CONFIG_PNP */
802
803static void snd_card_opti9xx_free(struct snd_card *card)
804{
805 struct snd_opti9xx *chip = card->private_data;
806
807 if (chip) {
808#ifdef OPTi93X
809 if (chip->irq > 0) {
810 disable_irq(chip->irq);
811 free_irq(chip->irq, chip);
812 }
813 release_and_free_resource(chip->res_mc_indir);
814#endif
815 release_and_free_resource(chip->res_mc_base);
816 }
817}
818
819static int snd_opti9xx_probe(struct snd_card *card)
820{
821 static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
822 int error;
823 int xdma2;
824 struct snd_opti9xx *chip = card->private_data;
825 struct snd_wss *codec;
826 struct snd_rawmidi *rmidi;
827 struct snd_hwdep *synth;
828
829#if defined(CS4231) || defined(OPTi93X)
830 xdma2 = dma2;
831#else
832 xdma2 = -1;
833#endif
834
835 if (port == SNDRV_AUTO_PORT) {
836 port = snd_legacy_find_free_ioport(possible_ports, 4);
837 if (port < 0) {
838 snd_printk(KERN_ERR "unable to find a free WSS port\n");
839 return -EBUSY;
840 }
841 }
842 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
843 mpu_port, mpu_irq);
844 if (error)
845 return error;
846
847 error = snd_wss_create(card, chip->wss_base + 4, -1, irq, dma1, xdma2,
848#ifdef OPTi93X
849 WSS_HW_OPTI93X, WSS_HWSHARE_IRQ,
850#else
851 WSS_HW_DETECT, 0,
852#endif
853 &codec);
854 if (error < 0)
855 return error;
856 chip->codec = codec;
857 error = snd_wss_pcm(codec, 0);
858 if (error < 0)
859 return error;
860 error = snd_wss_mixer(codec);
861 if (error < 0)
862 return error;
863#ifdef OPTi93X
864 error = snd_opti93x_mixer(codec);
865 if (error < 0)
866 return error;
867#endif
868#ifdef CS4231
869 error = snd_wss_timer(codec, 0);
870 if (error < 0)
871 return error;
872#endif
873#ifdef OPTi93X
874 error = request_irq(irq, snd_opti93x_interrupt,
875 0, DEV_NAME" - WSS", chip);
876 if (error < 0) {
877 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", irq);
878 return error;
879 }
880#endif
881 chip->irq = irq;
882 strcpy(card->driver, chip->name);
883 sprintf(card->shortname, "OPTi %s", card->driver);
884#if defined(CS4231) || defined(OPTi93X)
885 snprintf(card->longname, sizeof(card->longname),
886 "%s, %s at 0x%lx, irq %d, dma %d&%d",
887 card->shortname, codec->pcm->name,
888 chip->wss_base + 4, irq, dma1, xdma2);
889#else
890 snprintf(card->longname, sizeof(card->longname),
891 "%s, %s at 0x%lx, irq %d, dma %d",
892 card->shortname, codec->pcm->name, chip->wss_base + 4, irq,
893 dma1);
894#endif /* CS4231 || OPTi93X */
895
896 if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
897 rmidi = NULL;
898 else {
899 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
900 mpu_port, 0, mpu_irq, &rmidi);
901 if (error)
902 snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
903 mpu_port);
904 }
905
906 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
907 struct snd_opl3 *opl3 = NULL;
908#ifndef OPTi93X
909 if (chip->hardware == OPTi9XX_HW_82C928 ||
910 chip->hardware == OPTi9XX_HW_82C929 ||
911 chip->hardware == OPTi9XX_HW_82C924) {
912 struct snd_opl4 *opl4;
913 /* assume we have an OPL4 */
914 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
915 0x20, 0x20);
916 if (snd_opl4_create(card, fm_port, fm_port - 8,
917 2, &opl3, &opl4) < 0) {
918 /* no luck, use OPL3 instead */
919 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
920 0x00, 0x20);
921 }
922 }
923#endif /* !OPTi93X */
924 if (!opl3 && snd_opl3_create(card, fm_port, fm_port + 2,
925 OPL3_HW_AUTO, 0, &opl3) < 0) {
926 snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
927 fm_port, fm_port + 4 - 1);
928 }
929 if (opl3) {
930 error = snd_opl3_hwdep_new(opl3, 0, 1, &synth);
931 if (error < 0)
932 return error;
933 }
934 }
935
936 return snd_card_register(card);
937}
938
939static int snd_opti9xx_card_new(struct device *pdev, struct snd_card **cardp)
940{
941 struct snd_card *card;
942 int err;
943
944 err = snd_card_new(pdev, index, id, THIS_MODULE,
945 sizeof(struct snd_opti9xx), &card);
946 if (err < 0)
947 return err;
948 card->private_free = snd_card_opti9xx_free;
949 *cardp = card;
950 return 0;
951}
952
953static int snd_opti9xx_isa_match(struct device *devptr,
954 unsigned int dev)
955{
956#ifdef CONFIG_PNP
957 if (snd_opti9xx_pnp_is_probed)
958 return 0;
959 if (isapnp)
960 return 0;
961#endif
962 return 1;
963}
964
965static int snd_opti9xx_isa_probe(struct device *devptr,
966 unsigned int dev)
967{
968 struct snd_card *card;
969 int error;
970 static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
971#ifdef OPTi93X
972 static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
973#else
974 static int possible_irqs[] = {9, 10, 11, 7, -1};
975#endif /* OPTi93X */
976 static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
977 static int possible_dma1s[] = {3, 1, 0, -1};
978#if defined(CS4231) || defined(OPTi93X)
979 static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
980#endif /* CS4231 || OPTi93X */
981
982 if (mpu_port == SNDRV_AUTO_PORT) {
983 if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
984 snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
985 return -EBUSY;
986 }
987 }
988 if (irq == SNDRV_AUTO_IRQ) {
989 if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
990 snd_printk(KERN_ERR "unable to find a free IRQ\n");
991 return -EBUSY;
992 }
993 }
994 if (mpu_irq == SNDRV_AUTO_IRQ) {
995 if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
996 snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
997 return -EBUSY;
998 }
999 }
1000 if (dma1 == SNDRV_AUTO_DMA) {
1001 if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
1002 snd_printk(KERN_ERR "unable to find a free DMA1\n");
1003 return -EBUSY;
1004 }
1005 }
1006#if defined(CS4231) || defined(OPTi93X)
1007 if (dma2 == SNDRV_AUTO_DMA) {
1008 if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
1009 snd_printk(KERN_ERR "unable to find a free DMA2\n");
1010 return -EBUSY;
1011 }
1012 }
1013#endif
1014
1015 error = snd_opti9xx_card_new(devptr, &card);
1016 if (error < 0)
1017 return error;
1018
1019 if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
1020 snd_card_free(card);
1021 return error;
1022 }
1023 if ((error = snd_opti9xx_probe(card)) < 0) {
1024 snd_card_free(card);
1025 return error;
1026 }
1027 dev_set_drvdata(devptr, card);
1028 return 0;
1029}
1030
1031static int snd_opti9xx_isa_remove(struct device *devptr,
1032 unsigned int dev)
1033{
1034 snd_card_free(dev_get_drvdata(devptr));
1035 return 0;
1036}
1037
1038#ifdef CONFIG_PM
1039static int snd_opti9xx_suspend(struct snd_card *card)
1040{
1041 struct snd_opti9xx *chip = card->private_data;
1042
1043 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1044 chip->codec->suspend(chip->codec);
1045 return 0;
1046}
1047
1048static int snd_opti9xx_resume(struct snd_card *card)
1049{
1050 struct snd_opti9xx *chip = card->private_data;
1051 int error, xdma2;
1052#if defined(CS4231) || defined(OPTi93X)
1053 xdma2 = dma2;
1054#else
1055 xdma2 = -1;
1056#endif
1057
1058 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
1059 mpu_port, mpu_irq);
1060 if (error)
1061 return error;
1062 chip->codec->resume(chip->codec);
1063 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1064 return 0;
1065}
1066
1067static int snd_opti9xx_isa_suspend(struct device *dev, unsigned int n,
1068 pm_message_t state)
1069{
1070 return snd_opti9xx_suspend(dev_get_drvdata(dev));
1071}
1072
1073static int snd_opti9xx_isa_resume(struct device *dev, unsigned int n)
1074{
1075 return snd_opti9xx_resume(dev_get_drvdata(dev));
1076}
1077#endif
1078
1079static struct isa_driver snd_opti9xx_driver = {
1080 .match = snd_opti9xx_isa_match,
1081 .probe = snd_opti9xx_isa_probe,
1082 .remove = snd_opti9xx_isa_remove,
1083#ifdef CONFIG_PM
1084 .suspend = snd_opti9xx_isa_suspend,
1085 .resume = snd_opti9xx_isa_resume,
1086#endif
1087 .driver = {
1088 .name = DEV_NAME
1089 },
1090};
1091
1092#ifdef CONFIG_PNP
1093static int snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
1094 const struct pnp_card_device_id *pid)
1095{
1096 struct snd_card *card;
1097 int error, hw;
1098 struct snd_opti9xx *chip;
1099
1100 if (snd_opti9xx_pnp_is_probed)
1101 return -EBUSY;
1102 if (! isapnp)
1103 return -ENODEV;
1104 error = snd_opti9xx_card_new(&pcard->card->dev, &card);
1105 if (error < 0)
1106 return error;
1107 chip = card->private_data;
1108
1109 hw = snd_card_opti9xx_pnp(chip, pcard, pid);
1110 switch (hw) {
1111 case 0x0924:
1112 hw = OPTi9XX_HW_82C924;
1113 break;
1114 case 0x0925:
1115 hw = OPTi9XX_HW_82C925;
1116 break;
1117 case 0x0931:
1118 hw = OPTi9XX_HW_82C931;
1119 break;
1120 default:
1121 snd_card_free(card);
1122 return -ENODEV;
1123 }
1124
1125 if ((error = snd_opti9xx_init(chip, hw))) {
1126 snd_card_free(card);
1127 return error;
1128 }
1129 error = snd_opti9xx_read_check(chip);
1130 if (error) {
1131 snd_printk(KERN_ERR "OPTI chip not found\n");
1132 snd_card_free(card);
1133 return error;
1134 }
1135 if ((error = snd_opti9xx_probe(card)) < 0) {
1136 snd_card_free(card);
1137 return error;
1138 }
1139 pnp_set_card_drvdata(pcard, card);
1140 snd_opti9xx_pnp_is_probed = 1;
1141 return 0;
1142}
1143
1144static void snd_opti9xx_pnp_remove(struct pnp_card_link *pcard)
1145{
1146 snd_card_free(pnp_get_card_drvdata(pcard));
1147 pnp_set_card_drvdata(pcard, NULL);
1148 snd_opti9xx_pnp_is_probed = 0;
1149}
1150
1151#ifdef CONFIG_PM
1152static int snd_opti9xx_pnp_suspend(struct pnp_card_link *pcard,
1153 pm_message_t state)
1154{
1155 return snd_opti9xx_suspend(pnp_get_card_drvdata(pcard));
1156}
1157
1158static int snd_opti9xx_pnp_resume(struct pnp_card_link *pcard)
1159{
1160 return snd_opti9xx_resume(pnp_get_card_drvdata(pcard));
1161}
1162#endif
1163
1164static struct pnp_card_driver opti9xx_pnpc_driver = {
1165 .flags = PNP_DRIVER_RES_DISABLE,
1166 .name = DEV_NAME,
1167 .id_table = snd_opti9xx_pnpids,
1168 .probe = snd_opti9xx_pnp_probe,
1169 .remove = snd_opti9xx_pnp_remove,
1170#ifdef CONFIG_PM
1171 .suspend = snd_opti9xx_pnp_suspend,
1172 .resume = snd_opti9xx_pnp_resume,
1173#endif
1174};
1175#endif
1176
1177#ifdef OPTi93X
1178#define CHIP_NAME "82C93x"
1179#else
1180#define CHIP_NAME "82C92x"
1181#endif
1182
1183static int __init alsa_card_opti9xx_init(void)
1184{
1185#ifdef CONFIG_PNP
1186 pnp_register_card_driver(&opti9xx_pnpc_driver);
1187 if (snd_opti9xx_pnp_is_probed)
1188 return 0;
1189 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
1190#endif
1191 return isa_register_driver(&snd_opti9xx_driver, 1);
1192}
1193
1194static void __exit alsa_card_opti9xx_exit(void)
1195{
1196 if (!snd_opti9xx_pnp_is_probed) {
1197 isa_unregister_driver(&snd_opti9xx_driver);
1198 return;
1199 }
1200#ifdef CONFIG_PNP
1201 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
1202#endif
1203}
1204
1205module_init(alsa_card_opti9xx_init)
1206module_exit(alsa_card_opti9xx_exit)