blob: 4d2d0405bdc7bda05f0eb6a23a58d8d37e6da372 [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;
Randy Dunlap89c0ac72010-03-08 09:32:42 -0800220 else
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100221#endif /* CONFIG_PNP */
Randy Dunlap89c0ac72010-03-08 09:32:42 -0800222 {
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100223 chip->mc_base = 0xf8c;
224 chip->mc_base_size = opti9xx_mc_size[hardware];
225 }
226#else
227 chip->mc_base_size = opti9xx_mc_size[hardware];
228#endif
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 switch (hardware) {
231#ifndef OPTi93X
232 case OPTi9XX_HW_82C928:
233 case OPTi9XX_HW_82C929:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3;
235 chip->pwd_reg = 3;
236 break;
237
238 case OPTi9XX_HW_82C924:
239 case OPTi9XX_HW_82C925:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 chip->password = 0xe5;
241 chip->pwd_reg = 3;
242 break;
243#else /* OPTi93X */
244
245 case OPTi9XX_HW_82C930:
246 case OPTi9XX_HW_82C931:
247 case OPTi9XX_HW_82C933:
248 chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d;
Krzysztof Helte6960e12009-12-04 18:30:18 +0100249 if (!chip->mc_indir_index) {
250 chip->mc_indir_index = 0xe0e;
251 chip->mc_indir_size = 2;
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 chip->password = 0xe4;
254 chip->pwd_reg = 0;
255 break;
256#endif /* OPTi93X */
257
258 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100259 snd_printk(KERN_ERR "chip %d not supported\n", hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return -ENODEV;
261 }
262 return 0;
263}
264
Takashi Iwai346c7a62005-11-17 14:37:56 +0100265static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 unsigned char reg)
267{
268 unsigned long flags;
269 unsigned char retval = 0xff;
270
271 spin_lock_irqsave(&chip->lock, flags);
272 outb(chip->password, chip->mc_base + chip->pwd_reg);
273
274 switch (chip->hardware) {
275#ifndef OPTi93X
276 case OPTi9XX_HW_82C924:
277 case OPTi9XX_HW_82C925:
278 if (reg > 7) {
279 outb(reg, chip->mc_base + 8);
280 outb(chip->password, chip->mc_base + chip->pwd_reg);
281 retval = inb(chip->mc_base + 9);
282 break;
283 }
284
285 case OPTi9XX_HW_82C928:
286 case OPTi9XX_HW_82C929:
287 retval = inb(chip->mc_base + reg);
288 break;
289#else /* OPTi93X */
290
291 case OPTi9XX_HW_82C930:
292 case OPTi9XX_HW_82C931:
293 case OPTi9XX_HW_82C933:
294 outb(reg, chip->mc_indir_index);
295 outb(chip->password, chip->mc_base + chip->pwd_reg);
296 retval = inb(chip->mc_indir_index + 1);
297 break;
298#endif /* OPTi93X */
299
300 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100301 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
304 spin_unlock_irqrestore(&chip->lock, flags);
305 return retval;
306}
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100307
Takashi Iwai346c7a62005-11-17 14:37:56 +0100308static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 unsigned char value)
310{
311 unsigned long flags;
312
313 spin_lock_irqsave(&chip->lock, flags);
314 outb(chip->password, chip->mc_base + chip->pwd_reg);
315
316 switch (chip->hardware) {
317#ifndef OPTi93X
318 case OPTi9XX_HW_82C924:
319 case OPTi9XX_HW_82C925:
320 if (reg > 7) {
321 outb(reg, chip->mc_base + 8);
322 outb(chip->password, chip->mc_base + chip->pwd_reg);
323 outb(value, chip->mc_base + 9);
324 break;
325 }
326
327 case OPTi9XX_HW_82C928:
328 case OPTi9XX_HW_82C929:
329 outb(value, chip->mc_base + reg);
330 break;
331#else /* OPTi93X */
332
333 case OPTi9XX_HW_82C930:
334 case OPTi9XX_HW_82C931:
335 case OPTi9XX_HW_82C933:
336 outb(reg, chip->mc_indir_index);
337 outb(chip->password, chip->mc_base + chip->pwd_reg);
338 outb(value, chip->mc_indir_index + 1);
339 break;
340#endif /* OPTi93X */
341
342 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100343 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
346 spin_unlock_irqrestore(&chip->lock, flags);
347}
348
349
350#define snd_opti9xx_write_mask(chip, reg, value, mask) \
351 snd_opti9xx_write(chip, reg, \
352 (snd_opti9xx_read(chip, reg) & ~(mask)) | ((value) & (mask)))
353
354
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100355static int __devinit snd_opti9xx_configure(struct snd_opti9xx *chip,
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100356 long port,
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100357 int irq, int dma1, int dma2,
358 long mpu_port, int mpu_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 unsigned char wss_base_bits;
361 unsigned char irq_bits;
362 unsigned char dma_bits;
363 unsigned char mpu_port_bits = 0;
364 unsigned char mpu_irq_bits;
365
366 switch (chip->hardware) {
367#ifndef OPTi93X
368 case OPTi9XX_HW_82C924:
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100369 /* opti 929 mode (?), OPL3 clock output, audio enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100371 /* enable wave audio */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
373
374 case OPTi9XX_HW_82C925:
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100375 /* enable WSS mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100377 /* OPL3 FM synthesis */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100379 /* disable Sound Blaster IRQ and DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
381#ifdef CS4231
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100382 /* cs4231/4248 fix enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
384#else
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100385 /* cs4231/4248 fix disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
387#endif /* CS4231 */
388 break;
389
390 case OPTi9XX_HW_82C928:
391 case OPTi9XX_HW_82C929:
392 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
393 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
394 /*
395 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae);
396 */
397 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
398#ifdef CS4231
399 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
400#else
401 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
402#endif /* CS4231 */
403 break;
404
405#else /* OPTi93X */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 case OPTi9XX_HW_82C931:
407 case OPTi9XX_HW_82C933:
Krzysztof Helt3ae5f362008-06-08 07:57:53 +0200408 /*
Krzysztof Heltf81b9532007-10-16 14:54:37 +0200409 * The BTC 1817DW has QS1000 wavetable which is connected
410 * to the serial digital input of the OPTI931.
411 */
412 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff);
413 /*
414 * This bit sets OPTI931 to automaticaly select FM
415 * or digital input signal.
416 */
417 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01);
Krzysztof Helt3ae5f362008-06-08 07:57:53 +0200418 case OPTi9XX_HW_82C930: /* FALL THROUGH */
419 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
420 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
421 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
422 (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
423 0x34);
424 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 break;
426#endif /* OPTi93X */
427
428 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100429 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return -EINVAL;
431 }
432
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100433 /* PnP resource says it decodes only 10 bits of address */
434 switch (port & 0x3ff) {
435 case 0x130:
436 chip->wss_base = 0x530;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 wss_base_bits = 0x00;
438 break;
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100439 case 0x204:
440 chip->wss_base = 0x604;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 wss_base_bits = 0x03;
442 break;
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100443 case 0x280:
444 chip->wss_base = 0xe80;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 wss_base_bits = 0x01;
446 break;
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100447 case 0x340:
448 chip->wss_base = 0xf40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 wss_base_bits = 0x02;
450 break;
451 default:
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100452 snd_printk(KERN_WARNING "WSS port 0x%lx not valid\n", port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 goto __skip_base;
454 }
455 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
456
457__skip_base:
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100458 switch (irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459//#ifdef OPTi93X
460 case 5:
461 irq_bits = 0x05;
462 break;
463//#endif /* OPTi93X */
464 case 7:
465 irq_bits = 0x01;
466 break;
467 case 9:
468 irq_bits = 0x02;
469 break;
470 case 10:
471 irq_bits = 0x03;
472 break;
473 case 11:
474 irq_bits = 0x04;
475 break;
476 default:
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100477 snd_printk(KERN_WARNING "WSS irq # %d not valid\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto __skip_resources;
479 }
480
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100481 switch (dma1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 case 0:
483 dma_bits = 0x01;
484 break;
485 case 1:
486 dma_bits = 0x02;
487 break;
488 case 3:
489 dma_bits = 0x03;
490 break;
491 default:
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100492 snd_printk(KERN_WARNING "WSS dma1 # %d not valid\n", dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 goto __skip_resources;
494 }
495
496#if defined(CS4231) || defined(OPTi93X)
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100497 if (dma1 == dma2) {
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100498 snd_printk(KERN_ERR "don't want to share dmas\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return -EBUSY;
500 }
501
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100502 switch (dma2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 case 0:
504 case 1:
505 break;
506 default:
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100507 snd_printk(KERN_WARNING "WSS dma2 # %d not valid\n", dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto __skip_resources;
509 }
510 dma_bits |= 0x04;
511#endif /* CS4231 || OPTi93X */
512
513#ifndef OPTi93X
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100514 outb(irq_bits << 3 | dma_bits, chip->wss_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515#else /* OPTi93X */
516 snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
517#endif /* OPTi93X */
518
519__skip_resources:
520 if (chip->hardware > OPTi9XX_HW_82C928) {
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100521 switch (mpu_port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 case 0:
523 case -1:
524 break;
525 case 0x300:
526 mpu_port_bits = 0x03;
527 break;
528 case 0x310:
529 mpu_port_bits = 0x02;
530 break;
531 case 0x320:
532 mpu_port_bits = 0x01;
533 break;
534 case 0x330:
535 mpu_port_bits = 0x00;
536 break;
537 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100538 snd_printk(KERN_WARNING
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100539 "MPU-401 port 0x%lx not valid\n", mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 goto __skip_mpu;
541 }
542
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100543 switch (mpu_irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 case 5:
545 mpu_irq_bits = 0x02;
546 break;
547 case 7:
548 mpu_irq_bits = 0x03;
549 break;
550 case 9:
551 mpu_irq_bits = 0x00;
552 break;
553 case 10:
554 mpu_irq_bits = 0x01;
555 break;
556 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100557 snd_printk(KERN_WARNING "MPU-401 irq # %d not valid\n",
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100558 mpu_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto __skip_mpu;
560 }
561
562 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100563 (mpu_port <= 0) ? 0x00 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
565 0xf8);
566 }
567__skip_mpu:
568
569 return 0;
570}
571
572#ifdef OPTi93X
573
Krzysztof Helte9d0a802009-12-12 09:51:03 +0100574static const DECLARE_TLV_DB_SCALE(db_scale_5bit_3db_step, -9300, 300, 0);
575static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
576static const DECLARE_TLV_DB_SCALE(db_scale_4bit_12db_max, -3300, 300, 0);
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100577
578static struct snd_kcontrol_new snd_opti93x_controls[] = {
579WSS_DOUBLE("Master Playback Switch", 0,
580 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
581WSS_DOUBLE_TLV("Master Playback Volume", 0,
582 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1,
Krzysztof Helte9d0a802009-12-12 09:51:03 +0100583 db_scale_5bit_3db_step),
584WSS_DOUBLE_TLV("PCM Playback Volume", 0,
585 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1,
586 db_scale_5bit),
587WSS_DOUBLE_TLV("FM Playback Volume", 0,
588 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1,
589 db_scale_4bit_12db_max),
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100590WSS_DOUBLE("Line Playback Switch", 0,
591 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
Krzysztof Helte9d0a802009-12-12 09:51:03 +0100592WSS_DOUBLE_TLV("Line Playback Volume", 0,
593 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1,
594 db_scale_4bit_12db_max),
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100595WSS_DOUBLE("Mic Playback Switch", 0,
596 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
Krzysztof Helte9d0a802009-12-12 09:51:03 +0100597WSS_DOUBLE_TLV("Mic Playback Volume", 0,
598 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1,
599 db_scale_4bit_12db_max),
600WSS_DOUBLE_TLV("CD Playback Volume", 0,
601 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1,
602 db_scale_4bit_12db_max),
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100603WSS_DOUBLE("Aux Playback Switch", 0,
604 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
Krzysztof Helte9d0a802009-12-12 09:51:03 +0100605WSS_DOUBLE_TLV("Aux Playback Volume", 0,
606 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1,
607 db_scale_4bit_12db_max),
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100608};
609
610static int __devinit snd_opti93x_mixer(struct snd_wss *chip)
611{
612 struct snd_card *card;
613 unsigned int idx;
614 struct snd_ctl_elem_id id1, id2;
615 int err;
616
617 if (snd_BUG_ON(!chip || !chip->pcm))
618 return -EINVAL;
619
620 card = chip->card;
621
622 strcpy(card->mixername, chip->pcm->name);
623
624 memset(&id1, 0, sizeof(id1));
625 memset(&id2, 0, sizeof(id2));
626 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
627 /* reassign AUX0 switch to CD */
628 strcpy(id1.name, "Aux Playback Switch");
629 strcpy(id2.name, "CD Playback Switch");
630 err = snd_ctl_rename_id(card, &id1, &id2);
631 if (err < 0) {
632 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
633 return err;
634 }
635 /* reassign AUX1 switch to FM */
636 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
637 strcpy(id2.name, "FM Playback Switch");
638 err = snd_ctl_rename_id(card, &id1, &id2);
639 if (err < 0) {
640 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
641 return err;
642 }
643 /* remove AUX1 volume */
644 strcpy(id1.name, "Aux Playback Volume"); id1.index = 1;
645 snd_ctl_remove_id(card, &id1);
646
647 /* Replace WSS volume controls with OPTi93x volume controls */
648 id1.index = 0;
649 for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
650 strcpy(id1.name, snd_opti93x_controls[idx].name);
651 snd_ctl_remove_id(card, &id1);
652
653 err = snd_ctl_add(card,
654 snd_ctl_new1(&snd_opti93x_controls[idx], chip));
655 if (err < 0)
656 return err;
657 }
658 return 0;
659}
660
David Howells7d12e782006-10-05 14:55:46 +0100661static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Krzysztof Helt5f60e492009-12-09 20:12:43 +0100663 struct snd_opti9xx *chip = dev_id;
664 struct snd_wss *codec = chip->codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 unsigned char status;
666
Krzysztof Helt5f60e492009-12-09 20:12:43 +0100667 if (!codec)
668 return IRQ_HANDLED;
669
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200670 status = snd_opti9xx_read(chip, OPTi9XX_MC_REG(11));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
672 snd_pcm_period_elapsed(codec->playback_substream);
673 if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200674 snd_wss_overrange(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 snd_pcm_period_elapsed(codec->capture_substream);
676 }
677 outb(0x00, OPTi93X_PORT(codec, STATUS));
678 return IRQ_HANDLED;
679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681#endif /* OPTi93X */
682
Krzysztof Helte6960e12009-12-04 18:30:18 +0100683static int __devinit snd_opti9xx_read_check(struct snd_opti9xx *chip)
684{
685 unsigned char value;
686#ifdef OPTi93X
687 unsigned long flags;
688#endif
689
690 chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
691 "OPTi9xx MC");
692 if (chip->res_mc_base == NULL)
693 return -EBUSY;
694#ifndef OPTi93X
695 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
696 if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
697 if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
698 return 0;
699#else /* OPTi93X */
700 chip->res_mc_indir = request_region(chip->mc_indir_index,
701 chip->mc_indir_size,
702 "OPTi93x MC");
703 if (chip->res_mc_indir == NULL)
704 return -EBUSY;
705
706 spin_lock_irqsave(&chip->lock, flags);
707 outb(chip->password, chip->mc_base + chip->pwd_reg);
708 outb(((chip->mc_indir_index & 0x1f0) >> 4), chip->mc_base);
709 spin_unlock_irqrestore(&chip->lock, flags);
710
711 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
712 snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
713 if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
714 return 0;
715
716 release_and_free_resource(chip->res_mc_indir);
717 chip->res_mc_indir = NULL;
718#endif /* OPTi93X */
719 release_and_free_resource(chip->res_mc_base);
720 chip->res_mc_base = NULL;
721
722 return -ENODEV;
723}
724
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100725static int __devinit snd_card_opti9xx_detect(struct snd_card *card,
726 struct snd_opti9xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 int i, err;
729
730#ifndef OPTi93X
731 for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
Krzysztof Helte6960e12009-12-04 18:30:18 +0100732#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
Krzysztof Helte6960e12009-12-04 18:30:18 +0100734#endif
735 err = snd_opti9xx_init(chip, i);
736 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return err;
738
Krzysztof Helte6960e12009-12-04 18:30:18 +0100739 err = snd_opti9xx_read_check(chip);
740 if (err == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return 1;
Krzysztof Helte6960e12009-12-04 18:30:18 +0100742#ifdef OPTi93X
743 chip->mc_indir_index = 0;
744#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return -ENODEV;
747}
748
749#ifdef CONFIG_PNP
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100750static int __devinit snd_card_opti9xx_pnp(struct snd_opti9xx *chip,
751 struct pnp_card_link *card,
752 const struct pnp_card_device_id *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 struct pnp_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 int err;
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100756 struct pnp_dev *devmpu;
757#ifndef OPTi93X
758 struct pnp_dev *devmc;
759#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100761 pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
762 if (pdev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +0100764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 err = pnp_activate_dev(pdev);
766 if (err < 0) {
767 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return err;
769 }
770
771#ifdef OPTi93X
772 port = pnp_port_start(pdev, 0) - 4;
Takashi Iwai1ea73412007-09-17 16:44:06 +0200773 fm_port = pnp_port_start(pdev, 1) + 8;
Krzysztof Helte6960e12009-12-04 18:30:18 +0100774 chip->mc_indir_index = pnp_port_start(pdev, 3) + 2;
775 chip->mc_indir_size = pnp_port_len(pdev, 3) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776#else
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100777 devmc = pnp_request_card_device(card, pid->devs[2].id, NULL);
778 if (devmc == NULL)
779 return -EBUSY;
780
781 err = pnp_activate_dev(devmc);
782 if (err < 0) {
783 snd_printk(KERN_ERR "MC pnp configure failure: %d\n", err);
784 return err;
785 }
786
787 port = pnp_port_start(pdev, 1);
Takashi Iwai1ea73412007-09-17 16:44:06 +0200788 fm_port = pnp_port_start(pdev, 2) + 8;
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100789 /*
790 * The MC(0) is never accessed and card does not
791 * include it in the PnP resource range. OPTI93x include it.
792 */
793 chip->mc_base = pnp_port_start(devmc, 0) - 1;
794 chip->mc_base_size = pnp_port_len(devmc, 0) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795#endif /* OPTi93X */
796 irq = pnp_irq(pdev, 0);
797 dma1 = pnp_dma(pdev, 0);
798#if defined(CS4231) || defined(OPTi93X)
799 dma2 = pnp_dma(pdev, 1);
800#endif /* CS4231 || OPTi93X */
801
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100802 devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
803
804 if (devmpu && mpu_port > 0) {
805 err = pnp_activate_dev(devmpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (err < 0) {
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100807 snd_printk(KERN_ERR "MPU401 pnp configure failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 mpu_port = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 } else {
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100810 mpu_port = pnp_port_start(devmpu, 0);
811 mpu_irq = pnp_irq(devmpu, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return pid->driver_data;
815}
816#endif /* CONFIG_PNP */
817
Takashi Iwai346c7a62005-11-17 14:37:56 +0100818static void snd_card_opti9xx_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Takashi Iwai99a0b762005-11-17 17:13:59 +0100820 struct snd_opti9xx *chip = card->private_data;
Krzysztof Helte6960e12009-12-04 18:30:18 +0100821
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200822 if (chip) {
823#ifdef OPTi93X
Krzysztof Helt5f60e492009-12-09 20:12:43 +0100824 if (chip->irq > 0) {
825 disable_irq(chip->irq);
826 free_irq(chip->irq, chip);
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200827 }
Krzysztof Helte6960e12009-12-04 18:30:18 +0100828 release_and_free_resource(chip->res_mc_indir);
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200829#endif
Takashi Iwaib1d57762005-10-10 11:56:31 +0200830 release_and_free_resource(chip->res_mc_base);
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100834static int __devinit snd_opti9xx_probe(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 int error;
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100838 int xdma2;
Takashi Iwai99a0b762005-11-17 17:13:59 +0100839 struct snd_opti9xx *chip = card->private_data;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200840 struct snd_wss *codec;
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200841#ifdef CS4231
Takashi Iwai346c7a62005-11-17 14:37:56 +0100842 struct snd_timer *timer;
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200843#endif
Takashi Iwai346c7a62005-11-17 14:37:56 +0100844 struct snd_pcm *pcm;
845 struct snd_rawmidi *rmidi;
846 struct snd_hwdep *synth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848#if defined(CS4231) || defined(OPTi93X)
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100849 xdma2 = dma2;
Rene Hermana0d92742008-08-04 05:26:26 +0200850#else
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100851 xdma2 = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852#endif
853
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100854 if (port == SNDRV_AUTO_PORT) {
855 port = snd_legacy_find_free_ioport(possible_ports, 4);
856 if (port < 0) {
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100857 snd_printk(KERN_ERR "unable to find a free WSS port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -EBUSY;
859 }
860 }
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100861 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
862 mpu_port, mpu_irq);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200863 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100866 error = snd_wss_create(card, chip->wss_base + 4, -1, irq, dma1, xdma2,
Rene Hermana0d92742008-08-04 05:26:26 +0200867#ifdef OPTi93X
Krzysztof Helt7779f752008-07-31 21:03:41 +0200868 WSS_HW_OPTI93X, WSS_HWSHARE_IRQ,
Rene Hermana0d92742008-08-04 05:26:26 +0200869#else
870 WSS_HW_DETECT, 0,
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200871#endif
Krzysztof Helt7779f752008-07-31 21:03:41 +0200872 &codec);
873 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return error;
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200875#ifdef OPTi93X
876 chip->codec = codec;
877#endif
Krzysztof Heltead893c2008-07-31 21:09:32 +0200878 error = snd_wss_pcm(codec, 0, &pcm);
Krzysztof Helt5664daa2008-07-31 21:08:32 +0200879 if (error < 0)
880 return error;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200881 error = snd_wss_mixer(codec);
882 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return error;
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100884#ifdef OPTi93X
885 error = snd_opti93x_mixer(codec);
886 if (error < 0)
887 return error;
888#endif
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200889#ifdef CS4231
Krzysztof Helt7779f752008-07-31 21:03:41 +0200890 error = snd_wss_timer(codec, 0, &timer);
891 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return error;
Krzysztof Helt5664daa2008-07-31 21:08:32 +0200893#endif
894#ifdef OPTi93X
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100895 error = request_irq(irq, snd_opti93x_interrupt,
Krzysztof Helt5f60e492009-12-09 20:12:43 +0100896 IRQF_DISABLED, DEV_NAME" - WSS", chip);
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200897 if (error < 0) {
Krzysztof Helt5f60e492009-12-09 20:12:43 +0100898 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", irq);
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200899 return error;
900 }
901#endif
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100902 chip->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 strcpy(card->driver, chip->name);
904 sprintf(card->shortname, "OPTi %s", card->driver);
905#if defined(CS4231) || defined(OPTi93X)
906 sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100907 card->shortname, pcm->name,
908 chip->wss_base + 4, irq, dma1, xdma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909#else
910 sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100911 card->shortname, pcm->name, chip->wss_base + 4, irq, dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912#endif /* CS4231 || OPTi93X */
913
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100914 if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 rmidi = NULL;
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100916 else {
917 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
918 mpu_port, 0, mpu_irq, IRQF_DISABLED, &rmidi);
919 if (error)
Takashi Iwai99a0b762005-11-17 17:13:59 +0100920 snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100921 mpu_port);
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100924 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
Takashi Iwai346c7a62005-11-17 14:37:56 +0100925 struct snd_opl3 *opl3 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926#ifndef OPTi93X
927 if (chip->hardware == OPTi9XX_HW_82C928 ||
928 chip->hardware == OPTi9XX_HW_82C929 ||
929 chip->hardware == OPTi9XX_HW_82C924) {
Takashi Iwai346c7a62005-11-17 14:37:56 +0100930 struct snd_opl4 *opl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /* assume we have an OPL4 */
932 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
933 0x20, 0x20);
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100934 if (snd_opl4_create(card, fm_port, fm_port - 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 2, &opl3, &opl4) < 0) {
936 /* no luck, use OPL3 instead */
937 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
938 0x00, 0x20);
939 }
940 }
941#endif /* !OPTi93X */
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100942 if (!opl3 && snd_opl3_create(card, fm_port, fm_port + 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 OPL3_HW_AUTO, 0, &opl3) < 0) {
Takashi Iwai99a0b762005-11-17 17:13:59 +0100944 snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100945 fm_port, fm_port + 4 - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947 if (opl3) {
Krzysztof Heltaa9c2932009-01-21 15:08:03 +0100948 error = snd_opl3_hwdep_new(opl3, 0, 1, &synth);
949 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952 }
953
Takashi Iwai99a0b762005-11-17 17:13:59 +0100954 return snd_card_register(card);
955}
956
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +0100957static int snd_opti9xx_card_new(struct snd_card **cardp)
Takashi Iwai99a0b762005-11-17 17:13:59 +0100958{
959 struct snd_card *card;
Takashi Iwaib1a0aac2009-01-14 09:34:06 +0100960 int err;
Takashi Iwai99a0b762005-11-17 17:13:59 +0100961
Takashi Iwaic95eadd2008-12-28 16:43:35 +0100962 err = snd_card_create(index, id, THIS_MODULE,
963 sizeof(struct snd_opti9xx), &card);
964 if (err < 0)
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +0100965 return err;
Takashi Iwai99a0b762005-11-17 17:13:59 +0100966 card->private_free = snd_card_opti9xx_free;
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +0100967 *cardp = card;
968 return 0;
Takashi Iwai99a0b762005-11-17 17:13:59 +0100969}
970
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100971static int __devinit snd_opti9xx_isa_match(struct device *devptr,
972 unsigned int dev)
973{
Takashi Iwai101f6f42007-06-20 12:03:09 +0200974#ifdef CONFIG_PNP
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100975 if (snd_opti9xx_pnp_is_probed)
976 return 0;
977 if (isapnp)
978 return 0;
Takashi Iwai101f6f42007-06-20 12:03:09 +0200979#endif
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100980 return 1;
981}
982
983static int __devinit snd_opti9xx_isa_probe(struct device *devptr,
984 unsigned int dev)
Takashi Iwai99a0b762005-11-17 17:13:59 +0100985{
986 struct snd_card *card;
987 int error;
988 static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
989#ifdef OPTi93X
990 static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
991#else
992 static int possible_irqs[] = {9, 10, 11, 7, -1};
993#endif /* OPTi93X */
994 static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
995 static int possible_dma1s[] = {3, 1, 0, -1};
996#if defined(CS4231) || defined(OPTi93X)
997 static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
998#endif /* CS4231 || OPTi93X */
999
Takashi Iwai99a0b762005-11-17 17:13:59 +01001000 if (mpu_port == SNDRV_AUTO_PORT) {
1001 if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
1002 snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
1003 return -EBUSY;
1004 }
1005 }
1006 if (irq == SNDRV_AUTO_IRQ) {
1007 if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
1008 snd_printk(KERN_ERR "unable to find a free IRQ\n");
1009 return -EBUSY;
1010 }
1011 }
1012 if (mpu_irq == SNDRV_AUTO_IRQ) {
1013 if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
1014 snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
1015 return -EBUSY;
1016 }
1017 }
1018 if (dma1 == SNDRV_AUTO_DMA) {
1019 if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
1020 snd_printk(KERN_ERR "unable to find a free DMA1\n");
1021 return -EBUSY;
1022 }
1023 }
1024#if defined(CS4231) || defined(OPTi93X)
1025 if (dma2 == SNDRV_AUTO_DMA) {
1026 if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
Takashi Iwai4c9f1d32009-02-05 15:47:51 +01001027 snd_printk(KERN_ERR "unable to find a free DMA2\n");
Takashi Iwai99a0b762005-11-17 17:13:59 +01001028 return -EBUSY;
1029 }
1030 }
1031#endif
1032
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01001033 error = snd_opti9xx_card_new(&card);
1034 if (error < 0)
1035 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001036
1037 if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 snd_card_free(card);
1039 return error;
1040 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001041 snd_card_set_dev(card, devptr);
Takashi Iwai99a0b762005-11-17 17:13:59 +01001042 if ((error = snd_opti9xx_probe(card)) < 0) {
1043 snd_card_free(card);
1044 return error;
1045 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001046 dev_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return 0;
1048}
1049
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001050static int __devexit snd_opti9xx_isa_remove(struct device *devptr,
1051 unsigned int dev)
Takashi Iwai99a0b762005-11-17 17:13:59 +01001052{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001053 snd_card_free(dev_get_drvdata(devptr));
1054 dev_set_drvdata(devptr, NULL);
Takashi Iwai99a0b762005-11-17 17:13:59 +01001055 return 0;
1056}
1057
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001058static struct isa_driver snd_opti9xx_driver = {
1059 .match = snd_opti9xx_isa_match,
1060 .probe = snd_opti9xx_isa_probe,
1061 .remove = __devexit_p(snd_opti9xx_isa_remove),
Takashi Iwai99a0b762005-11-17 17:13:59 +01001062 /* FIXME: suspend/resume */
1063 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +01001064 .name = DEV_NAME
Takashi Iwai99a0b762005-11-17 17:13:59 +01001065 },
1066};
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068#ifdef CONFIG_PNP
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001069static int __devinit snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
1070 const struct pnp_card_device_id *pid)
Takashi Iwai99a0b762005-11-17 17:13:59 +01001071{
1072 struct snd_card *card;
1073 int error, hw;
1074 struct snd_opti9xx *chip;
1075
1076 if (snd_opti9xx_pnp_is_probed)
1077 return -EBUSY;
1078 if (! isapnp)
1079 return -ENODEV;
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01001080 error = snd_opti9xx_card_new(&card);
1081 if (error < 0)
1082 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001083 chip = card->private_data;
1084
1085 hw = snd_card_opti9xx_pnp(chip, pcard, pid);
1086 switch (hw) {
1087 case 0x0924:
1088 hw = OPTi9XX_HW_82C924;
1089 break;
1090 case 0x0925:
1091 hw = OPTi9XX_HW_82C925;
1092 break;
1093 case 0x0931:
1094 hw = OPTi9XX_HW_82C931;
1095 break;
1096 default:
1097 snd_card_free(card);
1098 return -ENODEV;
1099 }
1100
1101 if ((error = snd_opti9xx_init(chip, hw))) {
1102 snd_card_free(card);
1103 return error;
1104 }
Krzysztof Helte6960e12009-12-04 18:30:18 +01001105 error = snd_opti9xx_read_check(chip);
1106 if (error) {
1107 snd_printk(KERN_ERR "OPTI chip not found\n");
1108 snd_card_free(card);
1109 return error;
1110 }
Takashi Iwai99a0b762005-11-17 17:13:59 +01001111 snd_card_set_dev(card, &pcard->card->dev);
1112 if ((error = snd_opti9xx_probe(card)) < 0) {
1113 snd_card_free(card);
1114 return error;
1115 }
1116 pnp_set_card_drvdata(pcard, card);
1117 snd_opti9xx_pnp_is_probed = 1;
1118 return 0;
1119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121static void __devexit snd_opti9xx_pnp_remove(struct pnp_card_link * pcard)
1122{
Takashi Iwai99a0b762005-11-17 17:13:59 +01001123 snd_card_free(pnp_get_card_drvdata(pcard));
1124 pnp_set_card_drvdata(pcard, NULL);
1125 snd_opti9xx_pnp_is_probed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
1128static struct pnp_card_driver opti9xx_pnpc_driver = {
1129 .flags = PNP_DRIVER_RES_DISABLE,
1130 .name = "opti9xx",
1131 .id_table = snd_opti9xx_pnpids,
Takashi Iwai99a0b762005-11-17 17:13:59 +01001132 .probe = snd_opti9xx_pnp_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 .remove = __devexit_p(snd_opti9xx_pnp_remove),
1134};
1135#endif
1136
Takashi Iwai99a0b762005-11-17 17:13:59 +01001137#ifdef OPTi93X
1138#define CHIP_NAME "82C93x"
1139#else
1140#define CHIP_NAME "82C92x"
1141#endif
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143static int __init alsa_card_opti9xx_init(void)
1144{
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01001145#ifdef CONFIG_PNP
Takashi Iwai99a0b762005-11-17 17:13:59 +01001146 pnp_register_card_driver(&opti9xx_pnpc_driver);
1147 if (snd_opti9xx_pnp_is_probed)
1148 return 0;
Takashi Iwai101f6f42007-06-20 12:03:09 +02001149 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01001150#endif
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001151 return isa_register_driver(&snd_opti9xx_driver, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
1154static void __exit alsa_card_opti9xx_exit(void)
1155{
Clemens Ladischf7a92752005-12-07 09:13:42 +01001156 if (!snd_opti9xx_pnp_is_probed) {
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001157 isa_unregister_driver(&snd_opti9xx_driver);
1158 return;
Clemens Ladischf7a92752005-12-07 09:13:42 +01001159 }
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01001160#ifdef CONFIG_PNP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01001162#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
1165module_init(alsa_card_opti9xx_init)
1166module_exit(alsa_card_opti9xx_exit)