blob: b41ed8661b237abd41676cf2bed810686028e046 [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/pnp.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040031#include <linux/module.h>
Takashi Iwai99a0b762005-11-17 17:13:59 +010032#include <asm/io.h>
33#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <sound/core.h>
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +010035#include <sound/tlv.h>
Krzysztof Helt61ef19d2008-07-31 21:02:42 +020036#include <sound/wss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <sound/mpu401.h>
38#include <sound/opl3.h>
39#ifndef OPTi93X
40#include <sound/opl4.h>
41#endif
Ondrej Zary988aec32012-08-01 16:05:39 +020042#define SNDRV_LEGACY_FIND_FREE_IOPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#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 */
Rusty Russella67ff6a2011-12-15 13:49:36 +103067//static bool enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */
Rene Herman51f6baa2008-07-18 11:15:12 +020068#ifdef CONFIG_PNP
Randy Dunlapc578ae02012-03-28 18:26:17 -070069static bool isapnp = true; /* 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 struct resource *res_mc_indir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#endif /* OPTi93X */
Ondrej Zary5dd25072012-07-17 09:16:44 +0200141 struct snd_wss *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 unsigned long pwd_reg;
143
144 spinlock_t lock;
145
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100146 long wss_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Takashi Iwai99a0b762005-11-17 17:13:59 +0100150static int snd_opti9xx_pnp_is_probed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152#ifdef CONFIG_PNP
153
154static struct pnp_card_device_id snd_opti9xx_pnpids[] = {
155#ifndef OPTi93X
156 /* OPTi 82C924 */
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100157 { .id = "OPT0924",
158 .devs = { { "OPT0000" }, { "OPT0002" }, { "OPT0005" } },
159 .driver_data = 0x0924 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* OPTi 82C925 */
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100161 { .id = "OPT0925",
162 .devs = { { "OPT9250" }, { "OPT0002" }, { "OPT0005" } },
163 .driver_data = 0x0925 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#else
165 /* OPTi 82C931/3 */
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100166 { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } },
167 .driver_data = 0x0931 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#endif /* OPTi93X */
169 { .id = "" }
170};
171
172MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids);
173
174#endif /* CONFIG_PNP */
175
176#ifdef OPTi93X
Rene Herman83c51c02007-03-20 11:33:46 +0100177#define DEV_NAME "opti93x"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#else
Rene Herman83c51c02007-03-20 11:33:46 +0100179#define DEV_NAME "opti92x"
180#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182static char * snd_opti9xx_names[] = {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200183 "unknown",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 "82C928", "82C929",
185 "82C924", "82C925",
186 "82C930", "82C931", "82C933"
187};
188
Bill Pemberton1bff2922012-12-06 12:35:21 -0500189static int snd_opti9xx_init(struct snd_opti9xx *chip,
190 unsigned short hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
193
194 chip->hardware = hardware;
195 strcpy(chip->name, snd_opti9xx_names[hardware]);
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 spin_lock_init(&chip->lock);
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 chip->irq = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100201#ifndef OPTi93X
202#ifdef CONFIG_PNP
203 if (isapnp && chip->mc_base)
204 /* PnP resource gives the least 10 bits */
205 chip->mc_base |= 0xc00;
Randy Dunlap89c0ac72010-03-08 09:32:42 -0800206 else
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100207#endif /* CONFIG_PNP */
Randy Dunlap89c0ac72010-03-08 09:32:42 -0800208 {
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100209 chip->mc_base = 0xf8c;
210 chip->mc_base_size = opti9xx_mc_size[hardware];
211 }
212#else
213 chip->mc_base_size = opti9xx_mc_size[hardware];
214#endif
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 switch (hardware) {
217#ifndef OPTi93X
218 case OPTi9XX_HW_82C928:
219 case OPTi9XX_HW_82C929:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3;
221 chip->pwd_reg = 3;
222 break;
223
224 case OPTi9XX_HW_82C924:
225 case OPTi9XX_HW_82C925:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 chip->password = 0xe5;
227 chip->pwd_reg = 3;
228 break;
229#else /* OPTi93X */
230
231 case OPTi9XX_HW_82C930:
232 case OPTi9XX_HW_82C931:
233 case OPTi9XX_HW_82C933:
234 chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d;
Ondrej Zary10a30612012-06-15 20:55:54 +0200235 if (!chip->mc_indir_index)
Krzysztof Helte6960e12009-12-04 18:30:18 +0100236 chip->mc_indir_index = 0xe0e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 chip->password = 0xe4;
238 chip->pwd_reg = 0;
239 break;
240#endif /* OPTi93X */
241
242 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100243 snd_printk(KERN_ERR "chip %d not supported\n", hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return -ENODEV;
245 }
246 return 0;
247}
248
Takashi Iwai346c7a62005-11-17 14:37:56 +0100249static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 unsigned char reg)
251{
252 unsigned long flags;
253 unsigned char retval = 0xff;
254
255 spin_lock_irqsave(&chip->lock, flags);
256 outb(chip->password, chip->mc_base + chip->pwd_reg);
257
258 switch (chip->hardware) {
259#ifndef OPTi93X
260 case OPTi9XX_HW_82C924:
261 case OPTi9XX_HW_82C925:
262 if (reg > 7) {
263 outb(reg, chip->mc_base + 8);
264 outb(chip->password, chip->mc_base + chip->pwd_reg);
265 retval = inb(chip->mc_base + 9);
266 break;
267 }
268
269 case OPTi9XX_HW_82C928:
270 case OPTi9XX_HW_82C929:
271 retval = inb(chip->mc_base + reg);
272 break;
273#else /* OPTi93X */
274
275 case OPTi9XX_HW_82C930:
276 case OPTi9XX_HW_82C931:
277 case OPTi9XX_HW_82C933:
278 outb(reg, chip->mc_indir_index);
279 outb(chip->password, chip->mc_base + chip->pwd_reg);
280 retval = inb(chip->mc_indir_index + 1);
281 break;
282#endif /* OPTi93X */
283
284 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100285 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287
288 spin_unlock_irqrestore(&chip->lock, flags);
289 return retval;
290}
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100291
Takashi Iwai346c7a62005-11-17 14:37:56 +0100292static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 unsigned char value)
294{
295 unsigned long flags;
296
297 spin_lock_irqsave(&chip->lock, flags);
298 outb(chip->password, chip->mc_base + chip->pwd_reg);
299
300 switch (chip->hardware) {
301#ifndef OPTi93X
302 case OPTi9XX_HW_82C924:
303 case OPTi9XX_HW_82C925:
304 if (reg > 7) {
305 outb(reg, chip->mc_base + 8);
306 outb(chip->password, chip->mc_base + chip->pwd_reg);
307 outb(value, chip->mc_base + 9);
308 break;
309 }
310
311 case OPTi9XX_HW_82C928:
312 case OPTi9XX_HW_82C929:
313 outb(value, chip->mc_base + reg);
314 break;
315#else /* OPTi93X */
316
317 case OPTi9XX_HW_82C930:
318 case OPTi9XX_HW_82C931:
319 case OPTi9XX_HW_82C933:
320 outb(reg, chip->mc_indir_index);
321 outb(chip->password, chip->mc_base + chip->pwd_reg);
322 outb(value, chip->mc_indir_index + 1);
323 break;
324#endif /* OPTi93X */
325
326 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100327 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
330 spin_unlock_irqrestore(&chip->lock, flags);
331}
332
333
334#define snd_opti9xx_write_mask(chip, reg, value, mask) \
335 snd_opti9xx_write(chip, reg, \
336 (snd_opti9xx_read(chip, reg) & ~(mask)) | ((value) & (mask)))
337
338
Takashi Iwai59b1f082012-07-17 12:14:17 +0200339static int snd_opti9xx_configure(struct snd_opti9xx *chip,
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100340 long port,
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100341 int irq, int dma1, int dma2,
342 long mpu_port, int mpu_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 unsigned char wss_base_bits;
345 unsigned char irq_bits;
346 unsigned char dma_bits;
347 unsigned char mpu_port_bits = 0;
348 unsigned char mpu_irq_bits;
349
350 switch (chip->hardware) {
351#ifndef OPTi93X
352 case OPTi9XX_HW_82C924:
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100353 /* opti 929 mode (?), OPL3 clock output, audio enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100355 /* enable wave audio */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
357
358 case OPTi9XX_HW_82C925:
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100359 /* enable WSS mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100361 /* OPL3 FM synthesis */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100363 /* disable Sound Blaster IRQ and DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
365#ifdef CS4231
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100366 /* cs4231/4248 fix enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
368#else
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100369 /* cs4231/4248 fix disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
371#endif /* CS4231 */
372 break;
373
374 case OPTi9XX_HW_82C928:
375 case OPTi9XX_HW_82C929:
376 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
377 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
378 /*
379 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae);
380 */
381 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
382#ifdef CS4231
383 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
384#else
385 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
386#endif /* CS4231 */
387 break;
388
389#else /* OPTi93X */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 case OPTi9XX_HW_82C931:
Ondrej Zary10a30612012-06-15 20:55:54 +0200391 /* disable 3D sound (set GPIO1 as output, low) */
392 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(20), 0x04, 0x0c);
393 case OPTi9XX_HW_82C933: /* FALL THROUGH */
Krzysztof Helt3ae5f362008-06-08 07:57:53 +0200394 /*
Krzysztof Heltf81b9532007-10-16 14:54:37 +0200395 * The BTC 1817DW has QS1000 wavetable which is connected
396 * to the serial digital input of the OPTI931.
397 */
398 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff);
399 /*
400 * This bit sets OPTI931 to automaticaly select FM
401 * or digital input signal.
402 */
403 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01);
Krzysztof Helt3ae5f362008-06-08 07:57:53 +0200404 case OPTi9XX_HW_82C930: /* FALL THROUGH */
405 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
406 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
407 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
408 (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
409 0x34);
410 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 break;
412#endif /* OPTi93X */
413
414 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100415 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return -EINVAL;
417 }
418
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100419 /* PnP resource says it decodes only 10 bits of address */
420 switch (port & 0x3ff) {
421 case 0x130:
422 chip->wss_base = 0x530;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 wss_base_bits = 0x00;
424 break;
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100425 case 0x204:
426 chip->wss_base = 0x604;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 wss_base_bits = 0x03;
428 break;
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100429 case 0x280:
430 chip->wss_base = 0xe80;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 wss_base_bits = 0x01;
432 break;
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100433 case 0x340:
434 chip->wss_base = 0xf40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 wss_base_bits = 0x02;
436 break;
437 default:
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100438 snd_printk(KERN_WARNING "WSS port 0x%lx not valid\n", port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 goto __skip_base;
440 }
441 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
442
443__skip_base:
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100444 switch (irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445//#ifdef OPTi93X
446 case 5:
447 irq_bits = 0x05;
448 break;
449//#endif /* OPTi93X */
450 case 7:
451 irq_bits = 0x01;
452 break;
453 case 9:
454 irq_bits = 0x02;
455 break;
456 case 10:
457 irq_bits = 0x03;
458 break;
459 case 11:
460 irq_bits = 0x04;
461 break;
462 default:
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100463 snd_printk(KERN_WARNING "WSS irq # %d not valid\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 goto __skip_resources;
465 }
466
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100467 switch (dma1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 case 0:
469 dma_bits = 0x01;
470 break;
471 case 1:
472 dma_bits = 0x02;
473 break;
474 case 3:
475 dma_bits = 0x03;
476 break;
477 default:
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100478 snd_printk(KERN_WARNING "WSS dma1 # %d not valid\n", dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 goto __skip_resources;
480 }
481
482#if defined(CS4231) || defined(OPTi93X)
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100483 if (dma1 == dma2) {
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100484 snd_printk(KERN_ERR "don't want to share dmas\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return -EBUSY;
486 }
487
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100488 switch (dma2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 case 0:
490 case 1:
491 break;
492 default:
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100493 snd_printk(KERN_WARNING "WSS dma2 # %d not valid\n", dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto __skip_resources;
495 }
496 dma_bits |= 0x04;
497#endif /* CS4231 || OPTi93X */
498
499#ifndef OPTi93X
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100500 outb(irq_bits << 3 | dma_bits, chip->wss_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501#else /* OPTi93X */
502 snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
503#endif /* OPTi93X */
504
505__skip_resources:
506 if (chip->hardware > OPTi9XX_HW_82C928) {
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100507 switch (mpu_port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 case 0:
509 case -1:
510 break;
511 case 0x300:
512 mpu_port_bits = 0x03;
513 break;
514 case 0x310:
515 mpu_port_bits = 0x02;
516 break;
517 case 0x320:
518 mpu_port_bits = 0x01;
519 break;
520 case 0x330:
521 mpu_port_bits = 0x00;
522 break;
523 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100524 snd_printk(KERN_WARNING
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100525 "MPU-401 port 0x%lx not valid\n", mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 goto __skip_mpu;
527 }
528
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100529 switch (mpu_irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 case 5:
531 mpu_irq_bits = 0x02;
532 break;
533 case 7:
534 mpu_irq_bits = 0x03;
535 break;
536 case 9:
537 mpu_irq_bits = 0x00;
538 break;
539 case 10:
540 mpu_irq_bits = 0x01;
541 break;
542 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100543 snd_printk(KERN_WARNING "MPU-401 irq # %d not valid\n",
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100544 mpu_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 goto __skip_mpu;
546 }
547
548 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100549 (mpu_port <= 0) ? 0x00 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
551 0xf8);
552 }
553__skip_mpu:
554
555 return 0;
556}
557
558#ifdef OPTi93X
559
Krzysztof Helte9d0a802009-12-12 09:51:03 +0100560static const DECLARE_TLV_DB_SCALE(db_scale_5bit_3db_step, -9300, 300, 0);
561static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
562static const DECLARE_TLV_DB_SCALE(db_scale_4bit_12db_max, -3300, 300, 0);
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100563
564static struct snd_kcontrol_new snd_opti93x_controls[] = {
565WSS_DOUBLE("Master Playback Switch", 0,
566 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
567WSS_DOUBLE_TLV("Master Playback Volume", 0,
568 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1,
Krzysztof Helte9d0a802009-12-12 09:51:03 +0100569 db_scale_5bit_3db_step),
570WSS_DOUBLE_TLV("PCM Playback Volume", 0,
571 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1,
572 db_scale_5bit),
573WSS_DOUBLE_TLV("FM Playback Volume", 0,
574 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1,
575 db_scale_4bit_12db_max),
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100576WSS_DOUBLE("Line Playback Switch", 0,
577 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
Krzysztof Helte9d0a802009-12-12 09:51:03 +0100578WSS_DOUBLE_TLV("Line Playback Volume", 0,
579 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1,
580 db_scale_4bit_12db_max),
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100581WSS_DOUBLE("Mic Playback Switch", 0,
582 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
Krzysztof Helte9d0a802009-12-12 09:51:03 +0100583WSS_DOUBLE_TLV("Mic Playback Volume", 0,
584 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1,
585 db_scale_4bit_12db_max),
586WSS_DOUBLE_TLV("CD Playback Volume", 0,
587 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1,
588 db_scale_4bit_12db_max),
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100589WSS_DOUBLE("Aux Playback Switch", 0,
590 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
Krzysztof Helte9d0a802009-12-12 09:51:03 +0100591WSS_DOUBLE_TLV("Aux Playback Volume", 0,
592 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1,
593 db_scale_4bit_12db_max),
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100594};
595
Bill Pemberton1bff2922012-12-06 12:35:21 -0500596static int snd_opti93x_mixer(struct snd_wss *chip)
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100597{
598 struct snd_card *card;
599 unsigned int idx;
600 struct snd_ctl_elem_id id1, id2;
601 int err;
602
603 if (snd_BUG_ON(!chip || !chip->pcm))
604 return -EINVAL;
605
606 card = chip->card;
607
608 strcpy(card->mixername, chip->pcm->name);
609
610 memset(&id1, 0, sizeof(id1));
611 memset(&id2, 0, sizeof(id2));
612 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
613 /* reassign AUX0 switch to CD */
614 strcpy(id1.name, "Aux Playback Switch");
615 strcpy(id2.name, "CD Playback Switch");
616 err = snd_ctl_rename_id(card, &id1, &id2);
617 if (err < 0) {
618 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
619 return err;
620 }
621 /* reassign AUX1 switch to FM */
622 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
623 strcpy(id2.name, "FM Playback Switch");
624 err = snd_ctl_rename_id(card, &id1, &id2);
625 if (err < 0) {
626 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
627 return err;
628 }
629 /* remove AUX1 volume */
630 strcpy(id1.name, "Aux Playback Volume"); id1.index = 1;
631 snd_ctl_remove_id(card, &id1);
632
633 /* Replace WSS volume controls with OPTi93x volume controls */
634 id1.index = 0;
635 for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
636 strcpy(id1.name, snd_opti93x_controls[idx].name);
637 snd_ctl_remove_id(card, &id1);
638
639 err = snd_ctl_add(card,
640 snd_ctl_new1(&snd_opti93x_controls[idx], chip));
641 if (err < 0)
642 return err;
643 }
644 return 0;
645}
646
David Howells7d12e782006-10-05 14:55:46 +0100647static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Krzysztof Helt5f60e492009-12-09 20:12:43 +0100649 struct snd_opti9xx *chip = dev_id;
650 struct snd_wss *codec = chip->codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 unsigned char status;
652
Krzysztof Helt5f60e492009-12-09 20:12:43 +0100653 if (!codec)
654 return IRQ_HANDLED;
655
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200656 status = snd_opti9xx_read(chip, OPTi9XX_MC_REG(11));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
658 snd_pcm_period_elapsed(codec->playback_substream);
659 if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200660 snd_wss_overrange(codec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 snd_pcm_period_elapsed(codec->capture_substream);
662 }
663 outb(0x00, OPTi93X_PORT(codec, STATUS));
664 return IRQ_HANDLED;
665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667#endif /* OPTi93X */
668
Bill Pemberton1bff2922012-12-06 12:35:21 -0500669static int snd_opti9xx_read_check(struct snd_opti9xx *chip)
Krzysztof Helte6960e12009-12-04 18:30:18 +0100670{
671 unsigned char value;
672#ifdef OPTi93X
673 unsigned long flags;
674#endif
675
676 chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
677 "OPTi9xx MC");
678 if (chip->res_mc_base == NULL)
679 return -EBUSY;
680#ifndef OPTi93X
681 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
682 if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
683 if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
684 return 0;
685#else /* OPTi93X */
Ondrej Zary10a30612012-06-15 20:55:54 +0200686 chip->res_mc_indir = request_region(chip->mc_indir_index, 2,
Krzysztof Helte6960e12009-12-04 18:30:18 +0100687 "OPTi93x MC");
688 if (chip->res_mc_indir == NULL)
689 return -EBUSY;
690
691 spin_lock_irqsave(&chip->lock, flags);
692 outb(chip->password, chip->mc_base + chip->pwd_reg);
693 outb(((chip->mc_indir_index & 0x1f0) >> 4), chip->mc_base);
694 spin_unlock_irqrestore(&chip->lock, flags);
695
696 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
697 snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
698 if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
699 return 0;
700
701 release_and_free_resource(chip->res_mc_indir);
702 chip->res_mc_indir = NULL;
703#endif /* OPTi93X */
704 release_and_free_resource(chip->res_mc_base);
705 chip->res_mc_base = NULL;
706
707 return -ENODEV;
708}
709
Bill Pemberton1bff2922012-12-06 12:35:21 -0500710static int snd_card_opti9xx_detect(struct snd_card *card,
711 struct snd_opti9xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 int i, err;
714
715#ifndef OPTi93X
716 for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
Krzysztof Helte6960e12009-12-04 18:30:18 +0100717#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
Krzysztof Helte6960e12009-12-04 18:30:18 +0100719#endif
720 err = snd_opti9xx_init(chip, i);
721 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return err;
723
Krzysztof Helte6960e12009-12-04 18:30:18 +0100724 err = snd_opti9xx_read_check(chip);
725 if (err == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return 1;
Krzysztof Helte6960e12009-12-04 18:30:18 +0100727#ifdef OPTi93X
728 chip->mc_indir_index = 0;
729#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return -ENODEV;
732}
733
734#ifdef CONFIG_PNP
Bill Pemberton1bff2922012-12-06 12:35:21 -0500735static int snd_card_opti9xx_pnp(struct snd_opti9xx *chip,
736 struct pnp_card_link *card,
737 const struct pnp_card_device_id *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 struct pnp_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 int err;
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100741 struct pnp_dev *devmpu;
742#ifndef OPTi93X
743 struct pnp_dev *devmc;
744#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100746 pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
747 if (pdev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +0100749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 err = pnp_activate_dev(pdev);
751 if (err < 0) {
752 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return err;
754 }
755
756#ifdef OPTi93X
757 port = pnp_port_start(pdev, 0) - 4;
Takashi Iwai1ea73412007-09-17 16:44:06 +0200758 fm_port = pnp_port_start(pdev, 1) + 8;
Ondrej Zary10a30612012-06-15 20:55:54 +0200759 /* adjust mc_indir_index - some cards report it at 0xe?d,
760 other at 0xe?c but it really is always at 0xe?e */
761 chip->mc_indir_index = (pnp_port_start(pdev, 3) & ~0xf) | 0xe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762#else
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100763 devmc = pnp_request_card_device(card, pid->devs[2].id, NULL);
764 if (devmc == NULL)
765 return -EBUSY;
766
767 err = pnp_activate_dev(devmc);
768 if (err < 0) {
769 snd_printk(KERN_ERR "MC pnp configure failure: %d\n", err);
770 return err;
771 }
772
773 port = pnp_port_start(pdev, 1);
Takashi Iwai1ea73412007-09-17 16:44:06 +0200774 fm_port = pnp_port_start(pdev, 2) + 8;
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100775 /*
776 * The MC(0) is never accessed and card does not
777 * include it in the PnP resource range. OPTI93x include it.
778 */
779 chip->mc_base = pnp_port_start(devmc, 0) - 1;
780 chip->mc_base_size = pnp_port_len(devmc, 0) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781#endif /* OPTi93X */
782 irq = pnp_irq(pdev, 0);
783 dma1 = pnp_dma(pdev, 0);
784#if defined(CS4231) || defined(OPTi93X)
785 dma2 = pnp_dma(pdev, 1);
786#endif /* CS4231 || OPTi93X */
787
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100788 devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
789
790 if (devmpu && mpu_port > 0) {
791 err = pnp_activate_dev(devmpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (err < 0) {
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100793 snd_printk(KERN_ERR "MPU401 pnp configure failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 mpu_port = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 } else {
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100796 mpu_port = pnp_port_start(devmpu, 0);
797 mpu_irq = pnp_irq(devmpu, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return pid->driver_data;
801}
802#endif /* CONFIG_PNP */
803
Takashi Iwai346c7a62005-11-17 14:37:56 +0100804static void snd_card_opti9xx_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Takashi Iwai99a0b762005-11-17 17:13:59 +0100806 struct snd_opti9xx *chip = card->private_data;
Krzysztof Helte6960e12009-12-04 18:30:18 +0100807
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200808 if (chip) {
809#ifdef OPTi93X
Krzysztof Helt5f60e492009-12-09 20:12:43 +0100810 if (chip->irq > 0) {
811 disable_irq(chip->irq);
812 free_irq(chip->irq, chip);
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200813 }
Krzysztof Helte6960e12009-12-04 18:30:18 +0100814 release_and_free_resource(chip->res_mc_indir);
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200815#endif
Takashi Iwaib1d57762005-10-10 11:56:31 +0200816 release_and_free_resource(chip->res_mc_base);
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Bill Pemberton1bff2922012-12-06 12:35:21 -0500820static int snd_opti9xx_probe(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 int error;
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100824 int xdma2;
Takashi Iwai99a0b762005-11-17 17:13:59 +0100825 struct snd_opti9xx *chip = card->private_data;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200826 struct snd_wss *codec;
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200827#ifdef CS4231
Takashi Iwai346c7a62005-11-17 14:37:56 +0100828 struct snd_timer *timer;
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200829#endif
Takashi Iwai346c7a62005-11-17 14:37:56 +0100830 struct snd_pcm *pcm;
831 struct snd_rawmidi *rmidi;
832 struct snd_hwdep *synth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834#if defined(CS4231) || defined(OPTi93X)
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100835 xdma2 = dma2;
Rene Hermana0d92742008-08-04 05:26:26 +0200836#else
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100837 xdma2 = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838#endif
839
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100840 if (port == SNDRV_AUTO_PORT) {
841 port = snd_legacy_find_free_ioport(possible_ports, 4);
842 if (port < 0) {
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100843 snd_printk(KERN_ERR "unable to find a free WSS port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -EBUSY;
845 }
846 }
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100847 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
848 mpu_port, mpu_irq);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200849 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100852 error = snd_wss_create(card, chip->wss_base + 4, -1, irq, dma1, xdma2,
Rene Hermana0d92742008-08-04 05:26:26 +0200853#ifdef OPTi93X
Krzysztof Helt7779f752008-07-31 21:03:41 +0200854 WSS_HW_OPTI93X, WSS_HWSHARE_IRQ,
Rene Hermana0d92742008-08-04 05:26:26 +0200855#else
856 WSS_HW_DETECT, 0,
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200857#endif
Krzysztof Helt7779f752008-07-31 21:03:41 +0200858 &codec);
859 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return error;
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200861 chip->codec = codec;
Krzysztof Heltead893c2008-07-31 21:09:32 +0200862 error = snd_wss_pcm(codec, 0, &pcm);
Krzysztof Helt5664daa2008-07-31 21:08:32 +0200863 if (error < 0)
864 return error;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200865 error = snd_wss_mixer(codec);
866 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return error;
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +0100868#ifdef OPTi93X
869 error = snd_opti93x_mixer(codec);
870 if (error < 0)
871 return error;
872#endif
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200873#ifdef CS4231
Krzysztof Helt7779f752008-07-31 21:03:41 +0200874 error = snd_wss_timer(codec, 0, &timer);
875 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return error;
Krzysztof Helt5664daa2008-07-31 21:08:32 +0200877#endif
878#ifdef OPTi93X
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100879 error = request_irq(irq, snd_opti93x_interrupt,
Yong Zhang88e24c32011-09-22 16:59:20 +0800880 0, DEV_NAME" - WSS", chip);
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200881 if (error < 0) {
Krzysztof Helt5f60e492009-12-09 20:12:43 +0100882 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", irq);
Krzysztof Helt9f240a52008-06-11 12:26:32 +0200883 return error;
884 }
885#endif
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100886 chip->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 strcpy(card->driver, chip->name);
888 sprintf(card->shortname, "OPTi %s", card->driver);
889#if defined(CS4231) || defined(OPTi93X)
890 sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100891 card->shortname, pcm->name,
892 chip->wss_base + 4, irq, dma1, xdma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893#else
894 sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
Krzysztof Heltfd8d4732010-03-03 19:41:44 +0100895 card->shortname, pcm->name, chip->wss_base + 4, irq, dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896#endif /* CS4231 || OPTi93X */
897
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100898 if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 rmidi = NULL;
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100900 else {
901 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
Clemens Ladischdba8b462011-09-13 11:24:41 +0200902 mpu_port, 0, mpu_irq, &rmidi);
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100903 if (error)
Takashi Iwai99a0b762005-11-17 17:13:59 +0100904 snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100905 mpu_port);
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100908 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
Takashi Iwai346c7a62005-11-17 14:37:56 +0100909 struct snd_opl3 *opl3 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910#ifndef OPTi93X
911 if (chip->hardware == OPTi9XX_HW_82C928 ||
912 chip->hardware == OPTi9XX_HW_82C929 ||
913 chip->hardware == OPTi9XX_HW_82C924) {
Takashi Iwai346c7a62005-11-17 14:37:56 +0100914 struct snd_opl4 *opl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /* assume we have an OPL4 */
916 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
917 0x20, 0x20);
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100918 if (snd_opl4_create(card, fm_port, fm_port - 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 2, &opl3, &opl4) < 0) {
920 /* no luck, use OPL3 instead */
921 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
922 0x00, 0x20);
923 }
924 }
925#endif /* !OPTi93X */
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100926 if (!opl3 && snd_opl3_create(card, fm_port, fm_port + 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 OPL3_HW_AUTO, 0, &opl3) < 0) {
Takashi Iwai99a0b762005-11-17 17:13:59 +0100928 snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
Krzysztof Heltd8ea2392009-12-02 23:27:12 +0100929 fm_port, fm_port + 4 - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931 if (opl3) {
Krzysztof Heltaa9c2932009-01-21 15:08:03 +0100932 error = snd_opl3_hwdep_new(opl3, 0, 1, &synth);
933 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936 }
937
Takashi Iwai99a0b762005-11-17 17:13:59 +0100938 return snd_card_register(card);
939}
940
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +0100941static int snd_opti9xx_card_new(struct snd_card **cardp)
Takashi Iwai99a0b762005-11-17 17:13:59 +0100942{
943 struct snd_card *card;
Takashi Iwaib1a0aac2009-01-14 09:34:06 +0100944 int err;
Takashi Iwai99a0b762005-11-17 17:13:59 +0100945
Takashi Iwaic95eadd2008-12-28 16:43:35 +0100946 err = snd_card_create(index, id, THIS_MODULE,
947 sizeof(struct snd_opti9xx), &card);
948 if (err < 0)
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +0100949 return err;
Takashi Iwai99a0b762005-11-17 17:13:59 +0100950 card->private_free = snd_card_opti9xx_free;
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +0100951 *cardp = card;
952 return 0;
Takashi Iwai99a0b762005-11-17 17:13:59 +0100953}
954
Bill Pemberton1bff2922012-12-06 12:35:21 -0500955static int snd_opti9xx_isa_match(struct device *devptr,
956 unsigned int dev)
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100957{
Takashi Iwai101f6f42007-06-20 12:03:09 +0200958#ifdef CONFIG_PNP
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100959 if (snd_opti9xx_pnp_is_probed)
960 return 0;
961 if (isapnp)
962 return 0;
Takashi Iwai101f6f42007-06-20 12:03:09 +0200963#endif
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100964 return 1;
965}
966
Bill Pemberton1bff2922012-12-06 12:35:21 -0500967static int snd_opti9xx_isa_probe(struct device *devptr,
968 unsigned int dev)
Takashi Iwai99a0b762005-11-17 17:13:59 +0100969{
970 struct snd_card *card;
971 int error;
972 static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
973#ifdef OPTi93X
974 static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
975#else
976 static int possible_irqs[] = {9, 10, 11, 7, -1};
977#endif /* OPTi93X */
978 static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
979 static int possible_dma1s[] = {3, 1, 0, -1};
980#if defined(CS4231) || defined(OPTi93X)
981 static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
982#endif /* CS4231 || OPTi93X */
983
Takashi Iwai99a0b762005-11-17 17:13:59 +0100984 if (mpu_port == SNDRV_AUTO_PORT) {
985 if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
986 snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
987 return -EBUSY;
988 }
989 }
990 if (irq == SNDRV_AUTO_IRQ) {
991 if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
992 snd_printk(KERN_ERR "unable to find a free IRQ\n");
993 return -EBUSY;
994 }
995 }
996 if (mpu_irq == SNDRV_AUTO_IRQ) {
997 if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
998 snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
999 return -EBUSY;
1000 }
1001 }
1002 if (dma1 == SNDRV_AUTO_DMA) {
1003 if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
1004 snd_printk(KERN_ERR "unable to find a free DMA1\n");
1005 return -EBUSY;
1006 }
1007 }
1008#if defined(CS4231) || defined(OPTi93X)
1009 if (dma2 == SNDRV_AUTO_DMA) {
1010 if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
Takashi Iwai4c9f1d32009-02-05 15:47:51 +01001011 snd_printk(KERN_ERR "unable to find a free DMA2\n");
Takashi Iwai99a0b762005-11-17 17:13:59 +01001012 return -EBUSY;
1013 }
1014 }
1015#endif
1016
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01001017 error = snd_opti9xx_card_new(&card);
1018 if (error < 0)
1019 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001020
1021 if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 snd_card_free(card);
1023 return error;
1024 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001025 snd_card_set_dev(card, devptr);
Takashi Iwai99a0b762005-11-17 17:13:59 +01001026 if ((error = snd_opti9xx_probe(card)) < 0) {
1027 snd_card_free(card);
1028 return error;
1029 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001030 dev_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return 0;
1032}
1033
Bill Pemberton1bff2922012-12-06 12:35:21 -05001034static int snd_opti9xx_isa_remove(struct device *devptr,
1035 unsigned int dev)
Takashi Iwai99a0b762005-11-17 17:13:59 +01001036{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001037 snd_card_free(dev_get_drvdata(devptr));
1038 dev_set_drvdata(devptr, NULL);
Takashi Iwai99a0b762005-11-17 17:13:59 +01001039 return 0;
1040}
1041
Ondrej Zary5dd25072012-07-17 09:16:44 +02001042#ifdef CONFIG_PM
1043static int snd_opti9xx_suspend(struct snd_card *card)
1044{
1045 struct snd_opti9xx *chip = card->private_data;
1046
1047 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1048 chip->codec->suspend(chip->codec);
1049 return 0;
1050}
1051
1052static int snd_opti9xx_resume(struct snd_card *card)
1053{
1054 struct snd_opti9xx *chip = card->private_data;
1055 int error, xdma2;
1056#if defined(CS4231) || defined(OPTi93X)
1057 xdma2 = dma2;
1058#else
1059 xdma2 = -1;
1060#endif
1061
1062 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
1063 mpu_port, mpu_irq);
1064 if (error)
1065 return error;
1066 chip->codec->resume(chip->codec);
1067 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1068 return 0;
1069}
1070
1071static int snd_opti9xx_isa_suspend(struct device *dev, unsigned int n,
1072 pm_message_t state)
1073{
1074 return snd_opti9xx_suspend(dev_get_drvdata(dev));
1075}
1076
1077static int snd_opti9xx_isa_resume(struct device *dev, unsigned int n)
1078{
1079 return snd_opti9xx_resume(dev_get_drvdata(dev));
1080}
1081#endif
1082
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001083static struct isa_driver snd_opti9xx_driver = {
1084 .match = snd_opti9xx_isa_match,
1085 .probe = snd_opti9xx_isa_probe,
Bill Pemberton1bff2922012-12-06 12:35:21 -05001086 .remove = snd_opti9xx_isa_remove,
Ondrej Zary5dd25072012-07-17 09:16:44 +02001087#ifdef CONFIG_PM
1088 .suspend = snd_opti9xx_isa_suspend,
1089 .resume = snd_opti9xx_isa_resume,
1090#endif
Takashi Iwai99a0b762005-11-17 17:13:59 +01001091 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +01001092 .name = DEV_NAME
Takashi Iwai99a0b762005-11-17 17:13:59 +01001093 },
1094};
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096#ifdef CONFIG_PNP
Bill Pemberton1bff2922012-12-06 12:35:21 -05001097static int snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
1098 const struct pnp_card_device_id *pid)
Takashi Iwai99a0b762005-11-17 17:13:59 +01001099{
1100 struct snd_card *card;
1101 int error, hw;
1102 struct snd_opti9xx *chip;
1103
1104 if (snd_opti9xx_pnp_is_probed)
1105 return -EBUSY;
1106 if (! isapnp)
1107 return -ENODEV;
Takashi Iwai3e7fb9f2008-12-28 16:47:30 +01001108 error = snd_opti9xx_card_new(&card);
1109 if (error < 0)
1110 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001111 chip = card->private_data;
1112
1113 hw = snd_card_opti9xx_pnp(chip, pcard, pid);
1114 switch (hw) {
1115 case 0x0924:
1116 hw = OPTi9XX_HW_82C924;
1117 break;
1118 case 0x0925:
1119 hw = OPTi9XX_HW_82C925;
1120 break;
1121 case 0x0931:
1122 hw = OPTi9XX_HW_82C931;
1123 break;
1124 default:
1125 snd_card_free(card);
1126 return -ENODEV;
1127 }
1128
1129 if ((error = snd_opti9xx_init(chip, hw))) {
1130 snd_card_free(card);
1131 return error;
1132 }
Krzysztof Helte6960e12009-12-04 18:30:18 +01001133 error = snd_opti9xx_read_check(chip);
1134 if (error) {
1135 snd_printk(KERN_ERR "OPTI chip not found\n");
1136 snd_card_free(card);
1137 return error;
1138 }
Takashi Iwai99a0b762005-11-17 17:13:59 +01001139 snd_card_set_dev(card, &pcard->card->dev);
1140 if ((error = snd_opti9xx_probe(card)) < 0) {
1141 snd_card_free(card);
1142 return error;
1143 }
1144 pnp_set_card_drvdata(pcard, card);
1145 snd_opti9xx_pnp_is_probed = 1;
1146 return 0;
1147}
1148
Bill Pemberton1bff2922012-12-06 12:35:21 -05001149static void snd_opti9xx_pnp_remove(struct pnp_card_link *pcard)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Takashi Iwai99a0b762005-11-17 17:13:59 +01001151 snd_card_free(pnp_get_card_drvdata(pcard));
1152 pnp_set_card_drvdata(pcard, NULL);
1153 snd_opti9xx_pnp_is_probed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Ondrej Zary5dd25072012-07-17 09:16:44 +02001156#ifdef CONFIG_PM
1157static int snd_opti9xx_pnp_suspend(struct pnp_card_link *pcard,
1158 pm_message_t state)
1159{
1160 return snd_opti9xx_suspend(pnp_get_card_drvdata(pcard));
1161}
1162
1163static int snd_opti9xx_pnp_resume(struct pnp_card_link *pcard)
1164{
1165 return snd_opti9xx_resume(pnp_get_card_drvdata(pcard));
1166}
1167#endif
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169static struct pnp_card_driver opti9xx_pnpc_driver = {
1170 .flags = PNP_DRIVER_RES_DISABLE,
1171 .name = "opti9xx",
1172 .id_table = snd_opti9xx_pnpids,
Takashi Iwai99a0b762005-11-17 17:13:59 +01001173 .probe = snd_opti9xx_pnp_probe,
Bill Pemberton1bff2922012-12-06 12:35:21 -05001174 .remove = snd_opti9xx_pnp_remove,
Ondrej Zary5dd25072012-07-17 09:16:44 +02001175#ifdef CONFIG_PM
1176 .suspend = snd_opti9xx_pnp_suspend,
1177 .resume = snd_opti9xx_pnp_resume,
1178#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179};
1180#endif
1181
Takashi Iwai99a0b762005-11-17 17:13:59 +01001182#ifdef OPTi93X
1183#define CHIP_NAME "82C93x"
1184#else
1185#define CHIP_NAME "82C92x"
1186#endif
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188static int __init alsa_card_opti9xx_init(void)
1189{
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01001190#ifdef CONFIG_PNP
Takashi Iwai99a0b762005-11-17 17:13:59 +01001191 pnp_register_card_driver(&opti9xx_pnpc_driver);
1192 if (snd_opti9xx_pnp_is_probed)
1193 return 0;
Takashi Iwai101f6f42007-06-20 12:03:09 +02001194 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01001195#endif
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001196 return isa_register_driver(&snd_opti9xx_driver, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199static void __exit alsa_card_opti9xx_exit(void)
1200{
Clemens Ladischf7a92752005-12-07 09:13:42 +01001201 if (!snd_opti9xx_pnp_is_probed) {
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001202 isa_unregister_driver(&snd_opti9xx_driver);
1203 return;
Clemens Ladischf7a92752005-12-07 09:13:42 +01001204 }
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01001205#ifdef CONFIG_PNP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01001207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210module_init(alsa_card_opti9xx_init)
1211module_exit(alsa_card_opti9xx_exit)