blob: becd90d7536df11cbbd312b8ccd774bf75406646 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
Takashi Iwai99a0b762005-11-17 17:13:59 +010027#include <linux/err.h>
Takashi Iwai5e24c1c2007-02-22 12:50:54 +010028#include <linux/isa.h>
Takashi Iwai99a0b762005-11-17 17:13:59 +010029#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/slab.h>
31#include <linux/pnp.h>
32#include <linux/moduleparam.h>
Takashi Iwai99a0b762005-11-17 17:13:59 +010033#include <asm/io.h>
34#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <sound/core.h>
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +010036#include <sound/tlv.h>
Krzysztof Helt61ef19d2008-07-31 21:02:42 +020037#include <sound/wss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <sound/mpu401.h>
39#include <sound/opl3.h>
40#ifndef OPTi93X
41#include <sound/opl4.h>
42#endif
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 int enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */
Rene Herman51f6baa2008-07-18 11:15:12 +020068#ifdef CONFIG_PNP
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int isapnp = 1; /* Enable ISA PnP detection */
Rene Herman51f6baa2008-07-18 11:15:12 +020070#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static 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.");
Rene Herman51f6baa2008-07-18 11:15:12 +020087#ifdef CONFIG_PNP
Linus Torvalds1da177e2005-04-16 15:20:36 -070088module_param(isapnp, bool, 0444);
89MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
Rene Herman51f6baa2008-07-18 11:15:12 +020090#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070091module_param(port, long, 0444);
92MODULE_PARM_DESC(port, "WSS port # for opti9xx driver.");
93module_param(mpu_port, long, 0444);
94MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver.");
95module_param(fm_port, long, 0444);
96MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver.");
97module_param(irq, int, 0444);
98MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver.");
99module_param(mpu_irq, int, 0444);
100MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver.");
101module_param(dma1, int, 0444);
102MODULE_PARM_DESC(dma1, "1st dma # for opti9xx driver.");
103#if defined(CS4231) || defined(OPTi93X)
104module_param(dma2, int, 0444);
105MODULE_PARM_DESC(dma2, "2nd dma # for opti9xx driver.");
106#endif /* CS4231 || OPTi93X */
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#ifdef OPTi93X
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#define OPTi93X_STATUS 0x02
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#define OPTi93X_PORT(chip, r) ((chip)->port + OPTi93X_##r)
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define OPTi93X_IRQ_PLAYBACK 0x04
125#define OPTi93X_IRQ_CAPTURE 0x08
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#endif /* OPTi93X */
128
Takashi Iwai346c7a62005-11-17 14:37:56 +0100129struct snd_opti9xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 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;
Krzysztof Helte6960e12009-12-04 18:30:18 +0100139 unsigned long mc_indir_size;
140 struct resource *res_mc_indir;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200141 struct snd_wss *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#endif /* OPTi93X */
143 unsigned long pwd_reg;
144
145 spinlock_t lock;
146
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100147 long wss_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
Takashi Iwai99a0b762005-11-17 17:13:59 +0100151static int snd_opti9xx_pnp_is_probed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153#ifdef CONFIG_PNP
154
155static struct pnp_card_device_id snd_opti9xx_pnpids[] = {
156#ifndef OPTi93X
157 /* OPTi 82C924 */
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100158 { .id = "OPT0924",
159 .devs = { { "OPT0000" }, { "OPT0002" }, { "OPT0005" } },
160 .driver_data = 0x0924 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /* OPTi 82C925 */
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100162 { .id = "OPT0925",
163 .devs = { { "OPT9250" }, { "OPT0002" }, { "OPT0005" } },
164 .driver_data = 0x0925 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#else
166 /* OPTi 82C931/3 */
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100167 { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } },
168 .driver_data = 0x0931 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#endif /* OPTi93X */
170 { .id = "" }
171};
172
173MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids);
174
175#endif /* CONFIG_PNP */
176
177#ifdef OPTi93X
Rene Herman83c51c02007-03-20 11:33:46 +0100178#define DEV_NAME "opti93x"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179#else
Rene Herman83c51c02007-03-20 11:33:46 +0100180#define DEV_NAME "opti92x"
181#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183static char * snd_opti9xx_names[] = {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200184 "unknown",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 "82C928", "82C929",
186 "82C924", "82C925",
187 "82C930", "82C931", "82C933"
188};
189
190
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100191static long __devinit snd_legacy_find_free_ioport(long *port_table, long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 while (*port_table != -1) {
Takashi Iwaib1d57762005-10-10 11:56:31 +0200194 if (request_region(*port_table, size, "ALSA test")) {
195 release_region(*port_table, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return *port_table;
197 }
198 port_table++;
199 }
200 return -1;
201}
202
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100203static int __devinit snd_opti9xx_init(struct snd_opti9xx *chip,
204 unsigned short hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
207
208 chip->hardware = hardware;
209 strcpy(chip->name, snd_opti9xx_names[hardware]);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 spin_lock_init(&chip->lock);
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 chip->irq = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100215#ifndef OPTi93X
216#ifdef CONFIG_PNP
217 if (isapnp && chip->mc_base)
218 /* PnP resource gives the least 10 bits */
219 chip->mc_base |= 0xc00;
220#endif /* CONFIG_PNP */
221 else {
222 chip->mc_base = 0xf8c;
223 chip->mc_base_size = opti9xx_mc_size[hardware];
224 }
225#else
226 chip->mc_base_size = opti9xx_mc_size[hardware];
227#endif
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 switch (hardware) {
230#ifndef OPTi93X
231 case OPTi9XX_HW_82C928:
232 case OPTi9XX_HW_82C929:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3;
234 chip->pwd_reg = 3;
235 break;
236
237 case OPTi9XX_HW_82C924:
238 case OPTi9XX_HW_82C925:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 chip->password = 0xe5;
240 chip->pwd_reg = 3;
241 break;
242#else /* OPTi93X */
243
244 case OPTi9XX_HW_82C930:
245 case OPTi9XX_HW_82C931:
246 case OPTi9XX_HW_82C933:
247 chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d;
Krzysztof Helte6960e12009-12-04 18:30:18 +0100248 if (!chip->mc_indir_index) {
249 chip->mc_indir_index = 0xe0e;
250 chip->mc_indir_size = 2;
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 chip->password = 0xe4;
253 chip->pwd_reg = 0;
254 break;
255#endif /* OPTi93X */
256
257 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100258 snd_printk(KERN_ERR "chip %d not supported\n", hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return -ENODEV;
260 }
261 return 0;
262}
263
Takashi Iwai346c7a62005-11-17 14:37:56 +0100264static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 unsigned char reg)
266{
267 unsigned long flags;
268 unsigned char retval = 0xff;
269
270 spin_lock_irqsave(&chip->lock, flags);
271 outb(chip->password, chip->mc_base + chip->pwd_reg);
272
273 switch (chip->hardware) {
274#ifndef OPTi93X
275 case OPTi9XX_HW_82C924:
276 case OPTi9XX_HW_82C925:
277 if (reg > 7) {
278 outb(reg, chip->mc_base + 8);
279 outb(chip->password, chip->mc_base + chip->pwd_reg);
280 retval = inb(chip->mc_base + 9);
281 break;
282 }
283
284 case OPTi9XX_HW_82C928:
285 case OPTi9XX_HW_82C929:
286 retval = inb(chip->mc_base + reg);
287 break;
288#else /* OPTi93X */
289
290 case OPTi9XX_HW_82C930:
291 case OPTi9XX_HW_82C931:
292 case OPTi9XX_HW_82C933:
293 outb(reg, chip->mc_indir_index);
294 outb(chip->password, chip->mc_base + chip->pwd_reg);
295 retval = inb(chip->mc_indir_index + 1);
296 break;
297#endif /* OPTi93X */
298
299 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100300 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
303 spin_unlock_irqrestore(&chip->lock, flags);
304 return retval;
305}
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100306
Takashi Iwai346c7a62005-11-17 14:37:56 +0100307static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 unsigned char value)
309{
310 unsigned long flags;
311
312 spin_lock_irqsave(&chip->lock, flags);
313 outb(chip->password, chip->mc_base + chip->pwd_reg);
314
315 switch (chip->hardware) {
316#ifndef OPTi93X
317 case OPTi9XX_HW_82C924:
318 case OPTi9XX_HW_82C925:
319 if (reg > 7) {
320 outb(reg, chip->mc_base + 8);
321 outb(chip->password, chip->mc_base + chip->pwd_reg);
322 outb(value, chip->mc_base + 9);
323 break;
324 }
325
326 case OPTi9XX_HW_82C928:
327 case OPTi9XX_HW_82C929:
328 outb(value, chip->mc_base + reg);
329 break;
330#else /* OPTi93X */
331
332 case OPTi9XX_HW_82C930:
333 case OPTi9XX_HW_82C931:
334 case OPTi9XX_HW_82C933:
335 outb(reg, chip->mc_indir_index);
336 outb(chip->password, chip->mc_base + chip->pwd_reg);
337 outb(value, chip->mc_indir_index + 1);
338 break;
339#endif /* OPTi93X */
340
341 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100342 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345 spin_unlock_irqrestore(&chip->lock, flags);
346}
347
348
349#define snd_opti9xx_write_mask(chip, reg, value, mask) \
350 snd_opti9xx_write(chip, reg, \
351 (snd_opti9xx_read(chip, reg) & ~(mask)) | ((value) & (mask)))
352
353
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100354static int __devinit snd_opti9xx_configure(struct snd_opti9xx *chip,
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100355 long port,
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100356 int irq, int dma1, int dma2,
357 long mpu_port, int mpu_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 unsigned char wss_base_bits;
360 unsigned char irq_bits;
361 unsigned char dma_bits;
362 unsigned char mpu_port_bits = 0;
363 unsigned char mpu_irq_bits;
364
365 switch (chip->hardware) {
366#ifndef OPTi93X
367 case OPTi9XX_HW_82C924:
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100368 /* opti 929 mode (?), OPL3 clock output, audio enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100370 /* enable wave audio */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
372
373 case OPTi9XX_HW_82C925:
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100374 /* enable WSS mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100376 /* OPL3 FM synthesis */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100378 /* disable Sound Blaster IRQ and DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
380#ifdef CS4231
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100381 /* cs4231/4248 fix enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
383#else
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100384 /* cs4231/4248 fix disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
386#endif /* CS4231 */
387 break;
388
389 case OPTi9XX_HW_82C928:
390 case OPTi9XX_HW_82C929:
391 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
392 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
393 /*
394 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae);
395 */
396 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
397#ifdef CS4231
398 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
399#else
400 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
401#endif /* CS4231 */
402 break;
403
404#else /* OPTi93X */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 case OPTi9XX_HW_82C931:
406 case OPTi9XX_HW_82C933:
Krzysztof Helt3ae5f362008-06-08 07:57:53 +0200407 /*
Krzysztof Heltf81b9532007-10-16 14:54:37 +0200408 * The BTC 1817DW has QS1000 wavetable which is connected
409 * to the serial digital input of the OPTI931.
410 */
411 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff);
412 /*
413 * This bit sets OPTI931 to automaticaly select FM
414 * or digital input signal.
415 */
416 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01);
Krzysztof Helt3ae5f362008-06-08 07:57:53 +0200417 case OPTi9XX_HW_82C930: /* FALL THROUGH */
418 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
419 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
420 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
421 (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
422 0x34);
423 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 break;
425#endif /* OPTi93X */
426
427 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100428 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return -EINVAL;
430 }
431
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100432 /* PnP resource says it decodes only 10 bits of address */
433 switch (port & 0x3ff) {
434 case 0x130:
435 chip->wss_base = 0x530;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 wss_base_bits = 0x00;
437 break;
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100438 case 0x204:
439 chip->wss_base = 0x604;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 wss_base_bits = 0x03;
441 break;
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100442 case 0x280:
443 chip->wss_base = 0xe80;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 wss_base_bits = 0x01;
445 break;
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100446 case 0x340:
447 chip->wss_base = 0xf40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 wss_base_bits = 0x02;
449 break;
450 default:
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100451 snd_printk(KERN_WARNING "WSS port 0x%lx not valid\n", port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 goto __skip_base;
453 }
454 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
455
456__skip_base:
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100457 switch (irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458//#ifdef OPTi93X
459 case 5:
460 irq_bits = 0x05;
461 break;
462//#endif /* OPTi93X */
463 case 7:
464 irq_bits = 0x01;
465 break;
466 case 9:
467 irq_bits = 0x02;
468 break;
469 case 10:
470 irq_bits = 0x03;
471 break;
472 case 11:
473 irq_bits = 0x04;
474 break;
475 default:
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100476 snd_printk(KERN_WARNING "WSS irq # %d not valid\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 goto __skip_resources;
478 }
479
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100480 switch (dma1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 case 0:
482 dma_bits = 0x01;
483 break;
484 case 1:
485 dma_bits = 0x02;
486 break;
487 case 3:
488 dma_bits = 0x03;
489 break;
490 default:
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100491 snd_printk(KERN_WARNING "WSS dma1 # %d not valid\n", dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 goto __skip_resources;
493 }
494
495#if defined(CS4231) || defined(OPTi93X)
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100496 if (dma1 == dma2) {
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100497 snd_printk(KERN_ERR "don't want to share dmas\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return -EBUSY;
499 }
500
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100501 switch (dma2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 case 0:
503 case 1:
504 break;
505 default:
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100506 snd_printk(KERN_WARNING "WSS dma2 # %d not valid\n", dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 goto __skip_resources;
508 }
509 dma_bits |= 0x04;
510#endif /* CS4231 || OPTi93X */
511
512#ifndef OPTi93X
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100513 outb(irq_bits << 3 | dma_bits, chip->wss_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514#else /* OPTi93X */
515 snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
516#endif /* OPTi93X */
517
518__skip_resources:
519 if (chip->hardware > OPTi9XX_HW_82C928) {
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100520 switch (mpu_port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 case 0:
522 case -1:
523 break;
524 case 0x300:
525 mpu_port_bits = 0x03;
526 break;
527 case 0x310:
528 mpu_port_bits = 0x02;
529 break;
530 case 0x320:
531 mpu_port_bits = 0x01;
532 break;
533 case 0x330:
534 mpu_port_bits = 0x00;
535 break;
536 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100537 snd_printk(KERN_WARNING
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100538 "MPU-401 port 0x%lx not valid\n", mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto __skip_mpu;
540 }
541
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100542 switch (mpu_irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 case 5:
544 mpu_irq_bits = 0x02;
545 break;
546 case 7:
547 mpu_irq_bits = 0x03;
548 break;
549 case 9:
550 mpu_irq_bits = 0x00;
551 break;
552 case 10:
553 mpu_irq_bits = 0x01;
554 break;
555 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100556 snd_printk(KERN_WARNING "MPU-401 irq # %d not valid\n",
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100557 mpu_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto __skip_mpu;
559 }
560
561 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100562 (mpu_port <= 0) ? 0x00 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
564 0xf8);
565 }
566__skip_mpu:
567
568 return 0;
569}
570
571#ifdef OPTi93X
572
Krzysztof Helte9d0a802009-12-12 09:51:03 +0100573static const DECLARE_TLV_DB_SCALE(db_scale_5bit_3db_step, -9300, 300, 0);
574static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
575static const DECLARE_TLV_DB_SCALE(db_scale_4bit_12db_max, -3300, 300, 0);
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100576
577static struct snd_kcontrol_new snd_opti93x_controls[] = {
578WSS_DOUBLE("Master Playback Switch", 0,
579 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
580WSS_DOUBLE_TLV("Master Playback Volume", 0,
581 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1,
Krzysztof Helte9d0a802009-12-12 09:51:03 +0100582 db_scale_5bit_3db_step),
583WSS_DOUBLE_TLV("PCM Playback Volume", 0,
584 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1,
585 db_scale_5bit),
586WSS_DOUBLE_TLV("FM Playback Volume", 0,
587 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1,
588 db_scale_4bit_12db_max),
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100589WSS_DOUBLE("Line Playback Switch", 0,
590 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
Krzysztof Helte9d0a802009-12-12 09:51:03 +0100591WSS_DOUBLE_TLV("Line Playback Volume", 0,
592 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1,
593 db_scale_4bit_12db_max),
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100594WSS_DOUBLE("Mic Playback Switch", 0,
595 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
Krzysztof Helte9d0a802009-12-12 09:51:03 +0100596WSS_DOUBLE_TLV("Mic Playback Volume", 0,
597 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1,
598 db_scale_4bit_12db_max),
599WSS_DOUBLE_TLV("CD Playback Volume", 0,
600 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1,
601 db_scale_4bit_12db_max),
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100602WSS_DOUBLE("Aux Playback Switch", 0,
603 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
Krzysztof Helte9d0a802009-12-12 09:51:03 +0100604WSS_DOUBLE_TLV("Aux Playback Volume", 0,
605 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1,
606 db_scale_4bit_12db_max),
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100607};
608
609static int __devinit snd_opti93x_mixer(struct snd_wss *chip)
610{
611 struct snd_card *card;
612 unsigned int idx;
613 struct snd_ctl_elem_id id1, id2;
614 int err;
615
616 if (snd_BUG_ON(!chip || !chip->pcm))
617 return -EINVAL;
618
619 card = chip->card;
620
621 strcpy(card->mixername, chip->pcm->name);
622
623 memset(&id1, 0, sizeof(id1));
624 memset(&id2, 0, sizeof(id2));
625 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
626 /* reassign AUX0 switch to CD */
627 strcpy(id1.name, "Aux Playback Switch");
628 strcpy(id2.name, "CD Playback Switch");
629 err = snd_ctl_rename_id(card, &id1, &id2);
630 if (err < 0) {
631 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
632 return err;
633 }
634 /* reassign AUX1 switch to FM */
635 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
636 strcpy(id2.name, "FM Playback Switch");
637 err = snd_ctl_rename_id(card, &id1, &id2);
638 if (err < 0) {
639 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
640 return err;
641 }
642 /* remove AUX1 volume */
643 strcpy(id1.name, "Aux Playback Volume"); id1.index = 1;
644 snd_ctl_remove_id(card, &id1);
645
646 /* Replace WSS volume controls with OPTi93x volume controls */
647 id1.index = 0;
648 for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
649 strcpy(id1.name, snd_opti93x_controls[idx].name);
650 snd_ctl_remove_id(card, &id1);
651
652 err = snd_ctl_add(card,
653 snd_ctl_new1(&snd_opti93x_controls[idx], chip));
654 if (err < 0)
655 return err;
656 }
657 return 0;
658}
659
David Howells7d12e782006-10-05 14:55:46 +0100660static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Krzysztof Helt5f60e492009-12-09 20:12:43 +0100662 struct snd_opti9xx *chip = dev_id;
663 struct snd_wss *codec = chip->codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 unsigned char status;
665
Krzysztof Helt5f60e492009-12-09 20:12:43 +0100666 if (!codec)
667 return IRQ_HANDLED;
668
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200669 status = snd_opti9xx_read(chip, OPTi9XX_MC_REG(11));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
671 snd_pcm_period_elapsed(codec->playback_substream);
672 if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200673 snd_wss_overrange(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 snd_pcm_period_elapsed(codec->capture_substream);
675 }
676 outb(0x00, OPTi93X_PORT(codec, STATUS));
677 return IRQ_HANDLED;
678}
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680#endif /* OPTi93X */
681
Krzysztof Helte6960e12009-12-04 18:30:18 +0100682static int __devinit snd_opti9xx_read_check(struct snd_opti9xx *chip)
683{
684 unsigned char value;
685#ifdef OPTi93X
686 unsigned long flags;
687#endif
688
689 chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
690 "OPTi9xx MC");
691 if (chip->res_mc_base == NULL)
692 return -EBUSY;
693#ifndef OPTi93X
694 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
695 if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
696 if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
697 return 0;
698#else /* OPTi93X */
699 chip->res_mc_indir = request_region(chip->mc_indir_index,
700 chip->mc_indir_size,
701 "OPTi93x MC");
702 if (chip->res_mc_indir == NULL)
703 return -EBUSY;
704
705 spin_lock_irqsave(&chip->lock, flags);
706 outb(chip->password, chip->mc_base + chip->pwd_reg);
707 outb(((chip->mc_indir_index & 0x1f0) >> 4), chip->mc_base);
708 spin_unlock_irqrestore(&chip->lock, flags);
709
710 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
711 snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
712 if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
713 return 0;
714
715 release_and_free_resource(chip->res_mc_indir);
716 chip->res_mc_indir = NULL;
717#endif /* OPTi93X */
718 release_and_free_resource(chip->res_mc_base);
719 chip->res_mc_base = NULL;
720
721 return -ENODEV;
722}
723
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100724static int __devinit snd_card_opti9xx_detect(struct snd_card *card,
725 struct snd_opti9xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 int i, err;
728
729#ifndef OPTi93X
730 for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
Krzysztof Helte6960e12009-12-04 18:30:18 +0100731#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
Krzysztof Helte6960e12009-12-04 18:30:18 +0100733#endif
734 err = snd_opti9xx_init(chip, i);
735 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return err;
737
Krzysztof Helte6960e12009-12-04 18:30:18 +0100738 err = snd_opti9xx_read_check(chip);
739 if (err == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return 1;
Krzysztof Helte6960e12009-12-04 18:30:18 +0100741#ifdef OPTi93X
742 chip->mc_indir_index = 0;
743#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return -ENODEV;
746}
747
748#ifdef CONFIG_PNP
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100749static int __devinit snd_card_opti9xx_pnp(struct snd_opti9xx *chip,
750 struct pnp_card_link *card,
751 const struct pnp_card_device_id *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct pnp_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 int err;
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100755 struct pnp_dev *devmpu;
756#ifndef OPTi93X
757 struct pnp_dev *devmc;
758#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100760 pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
761 if (pdev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +0100763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 err = pnp_activate_dev(pdev);
765 if (err < 0) {
766 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return err;
768 }
769
770#ifdef OPTi93X
771 port = pnp_port_start(pdev, 0) - 4;
Takashi Iwai1ea73412007-09-17 16:44:06 +0200772 fm_port = pnp_port_start(pdev, 1) + 8;
Krzysztof Helte6960e12009-12-04 18:30:18 +0100773 chip->mc_indir_index = pnp_port_start(pdev, 3) + 2;
774 chip->mc_indir_size = pnp_port_len(pdev, 3) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775#else
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100776 devmc = pnp_request_card_device(card, pid->devs[2].id, NULL);
777 if (devmc == NULL)
778 return -EBUSY;
779
780 err = pnp_activate_dev(devmc);
781 if (err < 0) {
782 snd_printk(KERN_ERR "MC pnp configure failure: %d\n", err);
783 return err;
784 }
785
786 port = pnp_port_start(pdev, 1);
Takashi Iwai1ea73412007-09-17 16:44:06 +0200787 fm_port = pnp_port_start(pdev, 2) + 8;
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100788 /*
789 * The MC(0) is never accessed and card does not
790 * include it in the PnP resource range. OPTI93x include it.
791 */
792 chip->mc_base = pnp_port_start(devmc, 0) - 1;
793 chip->mc_base_size = pnp_port_len(devmc, 0) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794#endif /* OPTi93X */
795 irq = pnp_irq(pdev, 0);
796 dma1 = pnp_dma(pdev, 0);
797#if defined(CS4231) || defined(OPTi93X)
798 dma2 = pnp_dma(pdev, 1);
799#endif /* CS4231 || OPTi93X */
800
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100801 devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
802
803 if (devmpu && mpu_port > 0) {
804 err = pnp_activate_dev(devmpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (err < 0) {
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100806 snd_printk(KERN_ERR "MPU401 pnp configure failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 mpu_port = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 } else {
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100809 mpu_port = pnp_port_start(devmpu, 0);
810 mpu_irq = pnp_irq(devmpu, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return pid->driver_data;
814}
815#endif /* CONFIG_PNP */
816
Takashi Iwai346c7a62005-11-17 14:37:56 +0100817static void snd_card_opti9xx_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Takashi Iwai99a0b762005-11-17 17:13:59 +0100819 struct snd_opti9xx *chip = card->private_data;
Krzysztof Helte6960e12009-12-04 18:30:18 +0100820
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200821 if (chip) {
822#ifdef OPTi93X
Krzysztof Helt5f60e492009-12-09 20:12:43 +0100823 if (chip->irq > 0) {
824 disable_irq(chip->irq);
825 free_irq(chip->irq, chip);
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200826 }
Krzysztof Helte6960e12009-12-04 18:30:18 +0100827 release_and_free_resource(chip->res_mc_indir);
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200828#endif
Takashi Iwaib1d57762005-10-10 11:56:31 +0200829 release_and_free_resource(chip->res_mc_base);
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100833static int __devinit snd_opti9xx_probe(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 int error;
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100837 int xdma2;
Takashi Iwai99a0b762005-11-17 17:13:59 +0100838 struct snd_opti9xx *chip = card->private_data;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200839 struct snd_wss *codec;
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200840#ifdef CS4231
Takashi Iwai346c7a62005-11-17 14:37:56 +0100841 struct snd_timer *timer;
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200842#endif
Takashi Iwai346c7a62005-11-17 14:37:56 +0100843 struct snd_pcm *pcm;
844 struct snd_rawmidi *rmidi;
845 struct snd_hwdep *synth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847#if defined(CS4231) || defined(OPTi93X)
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100848 xdma2 = dma2;
Rene Hermana0d92742008-08-04 05:26:26 +0200849#else
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100850 xdma2 = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851#endif
852
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100853 if (port == SNDRV_AUTO_PORT) {
854 port = snd_legacy_find_free_ioport(possible_ports, 4);
855 if (port < 0) {
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100856 snd_printk(KERN_ERR "unable to find a free WSS port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return -EBUSY;
858 }
859 }
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100860 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
861 mpu_port, mpu_irq);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200862 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100865 error = snd_wss_create(card, chip->wss_base + 4, -1, irq, dma1, xdma2,
Rene Hermana0d92742008-08-04 05:26:26 +0200866#ifdef OPTi93X
Krzysztof Helt7779f752008-07-31 21:03:41 +0200867 WSS_HW_OPTI93X, WSS_HWSHARE_IRQ,
Rene Hermana0d92742008-08-04 05:26:26 +0200868#else
869 WSS_HW_DETECT, 0,
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200870#endif
Krzysztof Helt7779f752008-07-31 21:03:41 +0200871 &codec);
872 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return error;
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200874#ifdef OPTi93X
875 chip->codec = codec;
876#endif
Krzysztof Heltead893c2008-07-31 21:09:32 +0200877 error = snd_wss_pcm(codec, 0, &pcm);
Krzysztof Helt5664daa2008-07-31 21:08:32 +0200878 if (error < 0)
879 return error;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200880 error = snd_wss_mixer(codec);
881 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return error;
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100883#ifdef OPTi93X
884 error = snd_opti93x_mixer(codec);
885 if (error < 0)
886 return error;
887#endif
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200888#ifdef CS4231
Krzysztof Helt7779f752008-07-31 21:03:41 +0200889 error = snd_wss_timer(codec, 0, &timer);
890 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return error;
Krzysztof Helt5664daa2008-07-31 21:08:32 +0200892#endif
893#ifdef OPTi93X
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100894 error = request_irq(irq, snd_opti93x_interrupt,
Krzysztof Helt5f60e492009-12-09 20:12:43 +0100895 IRQF_DISABLED, DEV_NAME" - WSS", chip);
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200896 if (error < 0) {
Krzysztof Helt5f60e492009-12-09 20:12:43 +0100897 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", irq);
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200898 return error;
899 }
900#endif
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100901 chip->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 strcpy(card->driver, chip->name);
903 sprintf(card->shortname, "OPTi %s", card->driver);
904#if defined(CS4231) || defined(OPTi93X)
905 sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100906 card->shortname, pcm->name,
907 chip->wss_base + 4, irq, dma1, xdma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908#else
909 sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100910 card->shortname, pcm->name, chip->wss_base + 4, irq, dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911#endif /* CS4231 || OPTi93X */
912
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100913 if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 rmidi = NULL;
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100915 else {
916 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
917 mpu_port, 0, mpu_irq, IRQF_DISABLED, &rmidi);
918 if (error)
Takashi Iwai99a0b762005-11-17 17:13:59 +0100919 snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100920 mpu_port);
921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100923 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
Takashi Iwai346c7a62005-11-17 14:37:56 +0100924 struct snd_opl3 *opl3 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925#ifndef OPTi93X
926 if (chip->hardware == OPTi9XX_HW_82C928 ||
927 chip->hardware == OPTi9XX_HW_82C929 ||
928 chip->hardware == OPTi9XX_HW_82C924) {
Takashi Iwai346c7a62005-11-17 14:37:56 +0100929 struct snd_opl4 *opl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /* assume we have an OPL4 */
931 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
932 0x20, 0x20);
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100933 if (snd_opl4_create(card, fm_port, fm_port - 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 2, &opl3, &opl4) < 0) {
935 /* no luck, use OPL3 instead */
936 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
937 0x00, 0x20);
938 }
939 }
940#endif /* !OPTi93X */
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100941 if (!opl3 && snd_opl3_create(card, fm_port, fm_port + 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 OPL3_HW_AUTO, 0, &opl3) < 0) {
Takashi Iwai99a0b762005-11-17 17:13:59 +0100943 snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100944 fm_port, fm_port + 4 - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 if (opl3) {
Krzysztof Heltaa9c2932009-01-21 15:08:03 +0100947 error = snd_opl3_hwdep_new(opl3, 0, 1, &synth);
948 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951 }
952
Takashi Iwai99a0b762005-11-17 17:13:59 +0100953 return snd_card_register(card);
954}
955
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +0100956static int snd_opti9xx_card_new(struct snd_card **cardp)
Takashi Iwai99a0b762005-11-17 17:13:59 +0100957{
958 struct snd_card *card;
Takashi Iwaib1a0aac2009-01-14 09:34:06 +0100959 int err;
Takashi Iwai99a0b762005-11-17 17:13:59 +0100960
Takashi Iwaic95eadd2008-12-28 16:43:35 +0100961 err = snd_card_create(index, id, THIS_MODULE,
962 sizeof(struct snd_opti9xx), &card);
963 if (err < 0)
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +0100964 return err;
Takashi Iwai99a0b762005-11-17 17:13:59 +0100965 card->private_free = snd_card_opti9xx_free;
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +0100966 *cardp = card;
967 return 0;
Takashi Iwai99a0b762005-11-17 17:13:59 +0100968}
969
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100970static int __devinit snd_opti9xx_isa_match(struct device *devptr,
971 unsigned int dev)
972{
Takashi Iwai101f6f42007-06-20 12:03:09 +0200973#ifdef CONFIG_PNP
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100974 if (snd_opti9xx_pnp_is_probed)
975 return 0;
976 if (isapnp)
977 return 0;
Takashi Iwai101f6f42007-06-20 12:03:09 +0200978#endif
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100979 return 1;
980}
981
982static int __devinit snd_opti9xx_isa_probe(struct device *devptr,
983 unsigned int dev)
Takashi Iwai99a0b762005-11-17 17:13:59 +0100984{
985 struct snd_card *card;
986 int error;
987 static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
988#ifdef OPTi93X
989 static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
990#else
991 static int possible_irqs[] = {9, 10, 11, 7, -1};
992#endif /* OPTi93X */
993 static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
994 static int possible_dma1s[] = {3, 1, 0, -1};
995#if defined(CS4231) || defined(OPTi93X)
996 static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
997#endif /* CS4231 || OPTi93X */
998
Takashi Iwai99a0b762005-11-17 17:13:59 +0100999 if (mpu_port == SNDRV_AUTO_PORT) {
1000 if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
1001 snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
1002 return -EBUSY;
1003 }
1004 }
1005 if (irq == SNDRV_AUTO_IRQ) {
1006 if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
1007 snd_printk(KERN_ERR "unable to find a free IRQ\n");
1008 return -EBUSY;
1009 }
1010 }
1011 if (mpu_irq == SNDRV_AUTO_IRQ) {
1012 if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
1013 snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
1014 return -EBUSY;
1015 }
1016 }
1017 if (dma1 == SNDRV_AUTO_DMA) {
1018 if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
1019 snd_printk(KERN_ERR "unable to find a free DMA1\n");
1020 return -EBUSY;
1021 }
1022 }
1023#if defined(CS4231) || defined(OPTi93X)
1024 if (dma2 == SNDRV_AUTO_DMA) {
1025 if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
Takashi Iwai4c9f1d32009-02-05 15:47:51 +01001026 snd_printk(KERN_ERR "unable to find a free DMA2\n");
Takashi Iwai99a0b762005-11-17 17:13:59 +01001027 return -EBUSY;
1028 }
1029 }
1030#endif
1031
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01001032 error = snd_opti9xx_card_new(&card);
1033 if (error < 0)
1034 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001035
1036 if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 snd_card_free(card);
1038 return error;
1039 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001040 snd_card_set_dev(card, devptr);
Takashi Iwai99a0b762005-11-17 17:13:59 +01001041 if ((error = snd_opti9xx_probe(card)) < 0) {
1042 snd_card_free(card);
1043 return error;
1044 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001045 dev_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return 0;
1047}
1048
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001049static int __devexit snd_opti9xx_isa_remove(struct device *devptr,
1050 unsigned int dev)
Takashi Iwai99a0b762005-11-17 17:13:59 +01001051{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001052 snd_card_free(dev_get_drvdata(devptr));
1053 dev_set_drvdata(devptr, NULL);
Takashi Iwai99a0b762005-11-17 17:13:59 +01001054 return 0;
1055}
1056
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001057static struct isa_driver snd_opti9xx_driver = {
1058 .match = snd_opti9xx_isa_match,
1059 .probe = snd_opti9xx_isa_probe,
1060 .remove = __devexit_p(snd_opti9xx_isa_remove),
Takashi Iwai99a0b762005-11-17 17:13:59 +01001061 /* FIXME: suspend/resume */
1062 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +01001063 .name = DEV_NAME
Takashi Iwai99a0b762005-11-17 17:13:59 +01001064 },
1065};
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067#ifdef CONFIG_PNP
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001068static int __devinit snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
1069 const struct pnp_card_device_id *pid)
Takashi Iwai99a0b762005-11-17 17:13:59 +01001070{
1071 struct snd_card *card;
1072 int error, hw;
1073 struct snd_opti9xx *chip;
1074
1075 if (snd_opti9xx_pnp_is_probed)
1076 return -EBUSY;
1077 if (! isapnp)
1078 return -ENODEV;
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01001079 error = snd_opti9xx_card_new(&card);
1080 if (error < 0)
1081 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001082 chip = card->private_data;
1083
1084 hw = snd_card_opti9xx_pnp(chip, pcard, pid);
1085 switch (hw) {
1086 case 0x0924:
1087 hw = OPTi9XX_HW_82C924;
1088 break;
1089 case 0x0925:
1090 hw = OPTi9XX_HW_82C925;
1091 break;
1092 case 0x0931:
1093 hw = OPTi9XX_HW_82C931;
1094 break;
1095 default:
1096 snd_card_free(card);
1097 return -ENODEV;
1098 }
1099
1100 if ((error = snd_opti9xx_init(chip, hw))) {
1101 snd_card_free(card);
1102 return error;
1103 }
Krzysztof Helte6960e12009-12-04 18:30:18 +01001104 error = snd_opti9xx_read_check(chip);
1105 if (error) {
1106 snd_printk(KERN_ERR "OPTI chip not found\n");
1107 snd_card_free(card);
1108 return error;
1109 }
Takashi Iwai99a0b762005-11-17 17:13:59 +01001110 snd_card_set_dev(card, &pcard->card->dev);
1111 if ((error = snd_opti9xx_probe(card)) < 0) {
1112 snd_card_free(card);
1113 return error;
1114 }
1115 pnp_set_card_drvdata(pcard, card);
1116 snd_opti9xx_pnp_is_probed = 1;
1117 return 0;
1118}
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120static void __devexit snd_opti9xx_pnp_remove(struct pnp_card_link * pcard)
1121{
Takashi Iwai99a0b762005-11-17 17:13:59 +01001122 snd_card_free(pnp_get_card_drvdata(pcard));
1123 pnp_set_card_drvdata(pcard, NULL);
1124 snd_opti9xx_pnp_is_probed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
1127static struct pnp_card_driver opti9xx_pnpc_driver = {
1128 .flags = PNP_DRIVER_RES_DISABLE,
1129 .name = "opti9xx",
1130 .id_table = snd_opti9xx_pnpids,
Takashi Iwai99a0b762005-11-17 17:13:59 +01001131 .probe = snd_opti9xx_pnp_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 .remove = __devexit_p(snd_opti9xx_pnp_remove),
1133};
1134#endif
1135
Takashi Iwai99a0b762005-11-17 17:13:59 +01001136#ifdef OPTi93X
1137#define CHIP_NAME "82C93x"
1138#else
1139#define CHIP_NAME "82C92x"
1140#endif
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142static int __init alsa_card_opti9xx_init(void)
1143{
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01001144#ifdef CONFIG_PNP
Takashi Iwai99a0b762005-11-17 17:13:59 +01001145 pnp_register_card_driver(&opti9xx_pnpc_driver);
1146 if (snd_opti9xx_pnp_is_probed)
1147 return 0;
Takashi Iwai101f6f42007-06-20 12:03:09 +02001148 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01001149#endif
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001150 return isa_register_driver(&snd_opti9xx_driver, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152
1153static void __exit alsa_card_opti9xx_exit(void)
1154{
Clemens Ladischf7a92752005-12-07 09:13:42 +01001155 if (!snd_opti9xx_pnp_is_probed) {
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001156 isa_unregister_driver(&snd_opti9xx_driver);
1157 return;
Clemens Ladischf7a92752005-12-07 09:13:42 +01001158 }
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01001159#ifdef CONFIG_PNP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01001161#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
1164module_init(alsa_card_opti9xx_init)
1165module_exit(alsa_card_opti9xx_exit)