blob: 9300cf371ee84c6242522fb7ac04a3a14f92cba7 [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
26#include <sound/driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
Takashi Iwai99a0b762005-11-17 17:13:59 +010028#include <linux/err.h>
Takashi Iwai5e24c1c2007-02-22 12:50:54 +010029#include <linux/isa.h>
Takashi Iwai99a0b762005-11-17 17:13:59 +010030#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/slab.h>
32#include <linux/pnp.h>
33#include <linux/moduleparam.h>
Takashi Iwai99a0b762005-11-17 17:13:59 +010034#include <asm/io.h>
35#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <sound/core.h>
37#ifdef CS4231
38#include <sound/cs4231.h>
39#else
40#ifndef OPTi93X
41#include <sound/ad1848.h>
42#else
43#include <sound/control.h>
44#include <sound/pcm.h>
45#endif /* OPTi93X */
46#endif /* CS4231 */
47#include <sound/mpu401.h>
48#include <sound/opl3.h>
49#ifndef OPTi93X
50#include <sound/opl4.h>
51#endif
52#define SNDRV_LEGACY_FIND_FREE_IRQ
53#define SNDRV_LEGACY_FIND_FREE_DMA
54#include <sound/initval.h>
55
56MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
57MODULE_LICENSE("GPL");
58#ifdef OPTi93X
59MODULE_DESCRIPTION("OPTi93X");
60MODULE_SUPPORTED_DEVICE("{{OPTi,82C931/3}}");
61#else /* OPTi93X */
62#ifdef CS4231
63MODULE_DESCRIPTION("OPTi92X - CS4231");
64MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (CS4231)},"
65 "{OPTi,82C925 (CS4231)}}");
66#else /* CS4231 */
67MODULE_DESCRIPTION("OPTi92X - AD1848");
68MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (AD1848)},"
69 "{OPTi,82C925 (AD1848)},"
70 "{OAK,Mozart}}");
71#endif /* CS4231 */
72#endif /* OPTi93X */
73
74static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
75static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
76//static int enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */
77static int isapnp = 1; /* Enable ISA PnP detection */
78static long port = SNDRV_DEFAULT_PORT1; /* 0x530,0xe80,0xf40,0x604 */
79static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */
80static long fm_port = SNDRV_DEFAULT_PORT1; /* 0x388 */
81static int irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10,11 */
82static int mpu_irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10 */
83static int dma1 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
84#if defined(CS4231) || defined(OPTi93X)
85static int dma2 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
86#endif /* CS4231 || OPTi93X */
87
88module_param(index, int, 0444);
89MODULE_PARM_DESC(index, "Index value for opti9xx based soundcard.");
90module_param(id, charp, 0444);
91MODULE_PARM_DESC(id, "ID string for opti9xx based soundcard.");
92//module_param(enable, bool, 0444);
93//MODULE_PARM_DESC(enable, "Enable opti9xx soundcard.");
94module_param(isapnp, bool, 0444);
95MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
96module_param(port, long, 0444);
97MODULE_PARM_DESC(port, "WSS port # for opti9xx driver.");
98module_param(mpu_port, long, 0444);
99MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver.");
100module_param(fm_port, long, 0444);
101MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver.");
102module_param(irq, int, 0444);
103MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver.");
104module_param(mpu_irq, int, 0444);
105MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver.");
106module_param(dma1, int, 0444);
107MODULE_PARM_DESC(dma1, "1st dma # for opti9xx driver.");
108#if defined(CS4231) || defined(OPTi93X)
109module_param(dma2, int, 0444);
110MODULE_PARM_DESC(dma2, "2nd dma # for opti9xx driver.");
111#endif /* CS4231 || OPTi93X */
112
113#define OPTi9XX_HW_DETECT 0
114#define OPTi9XX_HW_82C928 1
115#define OPTi9XX_HW_82C929 2
116#define OPTi9XX_HW_82C924 3
117#define OPTi9XX_HW_82C925 4
118#define OPTi9XX_HW_82C930 5
119#define OPTi9XX_HW_82C931 6
120#define OPTi9XX_HW_82C933 7
121#define OPTi9XX_HW_LAST OPTi9XX_HW_82C933
122
123#define OPTi9XX_MC_REG(n) n
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#ifdef OPTi93X
126
127#define OPTi93X_INDEX 0x00
128#define OPTi93X_DATA 0x01
129#define OPTi93X_STATUS 0x02
130#define OPTi93X_DDATA 0x03
131#define OPTi93X_PORT(chip, r) ((chip)->port + OPTi93X_##r)
132
133#define OPTi93X_MIXOUT_LEFT 0x00
134#define OPTi93X_MIXOUT_RIGHT 0x01
135#define OPTi93X_CD_LEFT_INPUT 0x02
136#define OPTi93X_CD_RIGHT_INPUT 0x03
137#define OPTi930_AUX_LEFT_INPUT 0x04
138#define OPTi930_AUX_RIGHT_INPUT 0x05
139#define OPTi931_FM_LEFT_INPUT 0x04
140#define OPTi931_FM_RIGHT_INPUT 0x05
141#define OPTi93X_DAC_LEFT 0x06
142#define OPTi93X_DAC_RIGHT 0x07
143#define OPTi93X_PLAY_FORMAT 0x08
144#define OPTi93X_IFACE_CONF 0x09
145#define OPTi93X_PIN_CTRL 0x0a
146#define OPTi93X_ERR_INIT 0x0b
147#define OPTi93X_ID 0x0c
148#define OPTi93X_PLAY_UPR_CNT 0x0e
149#define OPTi93X_PLAY_LWR_CNT 0x0f
150#define OPTi931_AUX_LEFT_INPUT 0x10
151#define OPTi931_AUX_RIGHT_INPUT 0x11
152#define OPTi93X_LINE_LEFT_INPUT 0x12
153#define OPTi93X_LINE_RIGHT_INPUT 0x13
154#define OPTi93X_MIC_LEFT_INPUT 0x14
155#define OPTi93X_MIC_RIGHT_INPUT 0x15
156#define OPTi93X_OUT_LEFT 0x16
157#define OPTi93X_OUT_RIGHT 0x17
158#define OPTi93X_CAPT_FORMAT 0x1c
159#define OPTi93X_CAPT_UPR_CNT 0x1e
160#define OPTi93X_CAPT_LWR_CNT 0x1f
161
162#define OPTi93X_TRD 0x20
163#define OPTi93X_MCE 0x40
164#define OPTi93X_INIT 0x80
165
166#define OPTi93X_MIXOUT_MIC_GAIN 0x20
167#define OPTi93X_MIXOUT_LINE 0x00
168#define OPTi93X_MIXOUT_CD 0x40
169#define OPTi93X_MIXOUT_MIC 0x80
170#define OPTi93X_MIXOUT_MIXER 0xc0
171
172#define OPTi93X_STEREO 0x10
173#define OPTi93X_LINEAR_8 0x00
174#define OPTi93X_ULAW_8 0x20
175#define OPTi93X_LINEAR_16_LIT 0x40
176#define OPTi93X_ALAW_8 0x60
177#define OPTi93X_ADPCM_16 0xa0
178#define OPTi93X_LINEAR_16_BIG 0xc0
179
180#define OPTi93X_CAPTURE_PIO 0x80
181#define OPTi93X_PLAYBACK_PIO 0x40
182#define OPTi93X_AUTOCALIB 0x08
183#define OPTi93X_SINGLE_DMA 0x04
184#define OPTi93X_CAPTURE_ENABLE 0x02
185#define OPTi93X_PLAYBACK_ENABLE 0x01
186
187#define OPTi93X_IRQ_ENABLE 0x02
188
189#define OPTi93X_DMA_REQUEST 0x10
190#define OPTi93X_CALIB_IN_PROGRESS 0x20
191
192#define OPTi93X_IRQ_PLAYBACK 0x04
193#define OPTi93X_IRQ_CAPTURE 0x08
194
195
Takashi Iwai346c7a62005-11-17 14:37:56 +0100196struct snd_opti93x {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 unsigned long port;
198 struct resource *res_port;
199 int irq;
200 int dma1;
201 int dma2;
202
Takashi Iwai346c7a62005-11-17 14:37:56 +0100203 struct snd_opti9xx *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 unsigned short hardware;
205 unsigned char image[32];
206
207 unsigned char mce_bit;
208 unsigned short mode;
209 int mute;
210
211 spinlock_t lock;
212
Takashi Iwai346c7a62005-11-17 14:37:56 +0100213 struct snd_card *card;
214 struct snd_pcm *pcm;
215 struct snd_pcm_substream *playback_substream;
216 struct snd_pcm_substream *capture_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 unsigned int p_dma_size;
218 unsigned int c_dma_size;
219};
220
221#define OPTi93X_MODE_NONE 0x00
222#define OPTi93X_MODE_PLAY 0x01
223#define OPTi93X_MODE_CAPTURE 0x02
224#define OPTi93X_MODE_OPEN (OPTi93X_MODE_PLAY | OPTi93X_MODE_CAPTURE)
225
226#endif /* OPTi93X */
227
Takashi Iwai346c7a62005-11-17 14:37:56 +0100228struct snd_opti9xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 unsigned short hardware;
230 unsigned char password;
231 char name[7];
232
233 unsigned long mc_base;
234 struct resource *res_mc_base;
235 unsigned long mc_base_size;
236#ifdef OPTi93X
237 unsigned long mc_indir_index;
238#endif /* OPTi93X */
239 unsigned long pwd_reg;
240
241 spinlock_t lock;
242
243 long wss_base;
244 int irq;
245 int dma1;
246#if defined(CS4231) || defined(OPTi93X)
247 int dma2;
248#endif /* CS4231 || OPTi93X */
249
250 long fm_port;
251
252 long mpu_port;
253 int mpu_irq;
254
255#ifdef CONFIG_PNP
256 struct pnp_dev *dev;
257 struct pnp_dev *devmpu;
258#endif /* CONFIG_PNP */
259};
260
Takashi Iwai99a0b762005-11-17 17:13:59 +0100261static int snd_opti9xx_pnp_is_probed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263#ifdef CONFIG_PNP
264
265static struct pnp_card_device_id snd_opti9xx_pnpids[] = {
266#ifndef OPTi93X
267 /* OPTi 82C924 */
268 { .id = "OPT0924", .devs = { { "OPT0000" }, { "OPT0002" } }, .driver_data = 0x0924 },
269 /* OPTi 82C925 */
270 { .id = "OPT0925", .devs = { { "OPT9250" }, { "OPT0002" } }, .driver_data = 0x0925 },
271#else
272 /* OPTi 82C931/3 */
273 { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } }, .driver_data = 0x0931 },
274#endif /* OPTi93X */
275 { .id = "" }
276};
277
278MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids);
279
280#endif /* CONFIG_PNP */
281
282#ifdef OPTi93X
Rene Herman83c51c02007-03-20 11:33:46 +0100283#define DEV_NAME "opti93x"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284#else
Rene Herman83c51c02007-03-20 11:33:46 +0100285#define DEV_NAME "opti92x"
286#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288static char * snd_opti9xx_names[] = {
289 "unkown",
290 "82C928", "82C929",
291 "82C924", "82C925",
292 "82C930", "82C931", "82C933"
293};
294
295
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100296static long __devinit snd_legacy_find_free_ioport(long *port_table, long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 while (*port_table != -1) {
Takashi Iwaib1d57762005-10-10 11:56:31 +0200299 if (request_region(*port_table, size, "ALSA test")) {
300 release_region(*port_table, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return *port_table;
302 }
303 port_table++;
304 }
305 return -1;
306}
307
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100308static int __devinit snd_opti9xx_init(struct snd_opti9xx *chip,
309 unsigned short hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
312
313 chip->hardware = hardware;
314 strcpy(chip->name, snd_opti9xx_names[hardware]);
315
316 chip->mc_base_size = opti9xx_mc_size[hardware];
317
318 spin_lock_init(&chip->lock);
319
320 chip->wss_base = -1;
321 chip->irq = -1;
322 chip->dma1 = -1;
323#if defined(CS4231) || defined (OPTi93X)
324 chip->dma2 = -1;
325#endif /* CS4231 || OPTi93X */
326 chip->fm_port = -1;
327 chip->mpu_port = -1;
328 chip->mpu_irq = -1;
329
330 switch (hardware) {
331#ifndef OPTi93X
332 case OPTi9XX_HW_82C928:
333 case OPTi9XX_HW_82C929:
334 chip->mc_base = 0xf8c;
335 chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3;
336 chip->pwd_reg = 3;
337 break;
338
339 case OPTi9XX_HW_82C924:
340 case OPTi9XX_HW_82C925:
341 chip->mc_base = 0xf8c;
342 chip->password = 0xe5;
343 chip->pwd_reg = 3;
344 break;
345#else /* OPTi93X */
346
347 case OPTi9XX_HW_82C930:
348 case OPTi9XX_HW_82C931:
349 case OPTi9XX_HW_82C933:
350 chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d;
351 chip->mc_indir_index = 0xe0e;
352 chip->password = 0xe4;
353 chip->pwd_reg = 0;
354 break;
355#endif /* OPTi93X */
356
357 default:
358 snd_printk("chip %d not supported\n", hardware);
359 return -ENODEV;
360 }
361 return 0;
362}
363
Takashi Iwai346c7a62005-11-17 14:37:56 +0100364static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 unsigned char reg)
366{
367 unsigned long flags;
368 unsigned char retval = 0xff;
369
370 spin_lock_irqsave(&chip->lock, flags);
371 outb(chip->password, chip->mc_base + chip->pwd_reg);
372
373 switch (chip->hardware) {
374#ifndef OPTi93X
375 case OPTi9XX_HW_82C924:
376 case OPTi9XX_HW_82C925:
377 if (reg > 7) {
378 outb(reg, chip->mc_base + 8);
379 outb(chip->password, chip->mc_base + chip->pwd_reg);
380 retval = inb(chip->mc_base + 9);
381 break;
382 }
383
384 case OPTi9XX_HW_82C928:
385 case OPTi9XX_HW_82C929:
386 retval = inb(chip->mc_base + reg);
387 break;
388#else /* OPTi93X */
389
390 case OPTi9XX_HW_82C930:
391 case OPTi9XX_HW_82C931:
392 case OPTi9XX_HW_82C933:
393 outb(reg, chip->mc_indir_index);
394 outb(chip->password, chip->mc_base + chip->pwd_reg);
395 retval = inb(chip->mc_indir_index + 1);
396 break;
397#endif /* OPTi93X */
398
399 default:
400 snd_printk("chip %d not supported\n", chip->hardware);
401 }
402
403 spin_unlock_irqrestore(&chip->lock, flags);
404 return retval;
405}
406
Takashi Iwai346c7a62005-11-17 14:37:56 +0100407static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 unsigned char value)
409{
410 unsigned long flags;
411
412 spin_lock_irqsave(&chip->lock, flags);
413 outb(chip->password, chip->mc_base + chip->pwd_reg);
414
415 switch (chip->hardware) {
416#ifndef OPTi93X
417 case OPTi9XX_HW_82C924:
418 case OPTi9XX_HW_82C925:
419 if (reg > 7) {
420 outb(reg, chip->mc_base + 8);
421 outb(chip->password, chip->mc_base + chip->pwd_reg);
422 outb(value, chip->mc_base + 9);
423 break;
424 }
425
426 case OPTi9XX_HW_82C928:
427 case OPTi9XX_HW_82C929:
428 outb(value, chip->mc_base + reg);
429 break;
430#else /* OPTi93X */
431
432 case OPTi9XX_HW_82C930:
433 case OPTi9XX_HW_82C931:
434 case OPTi9XX_HW_82C933:
435 outb(reg, chip->mc_indir_index);
436 outb(chip->password, chip->mc_base + chip->pwd_reg);
437 outb(value, chip->mc_indir_index + 1);
438 break;
439#endif /* OPTi93X */
440
441 default:
442 snd_printk("chip %d not supported\n", chip->hardware);
443 }
444
445 spin_unlock_irqrestore(&chip->lock, flags);
446}
447
448
449#define snd_opti9xx_write_mask(chip, reg, value, mask) \
450 snd_opti9xx_write(chip, reg, \
451 (snd_opti9xx_read(chip, reg) & ~(mask)) | ((value) & (mask)))
452
453
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100454static int __devinit snd_opti9xx_configure(struct snd_opti9xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 unsigned char wss_base_bits;
457 unsigned char irq_bits;
458 unsigned char dma_bits;
459 unsigned char mpu_port_bits = 0;
460 unsigned char mpu_irq_bits;
461
462 switch (chip->hardware) {
463#ifndef OPTi93X
464 case OPTi9XX_HW_82C924:
465 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
466 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
467
468 case OPTi9XX_HW_82C925:
469 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
470 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
471 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
472#ifdef CS4231
473 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
474#else
475 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
476#endif /* CS4231 */
477 break;
478
479 case OPTi9XX_HW_82C928:
480 case OPTi9XX_HW_82C929:
481 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
482 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
483 /*
484 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae);
485 */
486 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
487#ifdef CS4231
488 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
489#else
490 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
491#endif /* CS4231 */
492 break;
493
494#else /* OPTi93X */
495 case OPTi9XX_HW_82C930:
496 case OPTi9XX_HW_82C931:
497 case OPTi9XX_HW_82C933:
498 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
499 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
500 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
501 (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
502 0x34);
503 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
Krzysztof Heltf81b9532007-10-16 14:54:37 +0200504 /*
505 * The BTC 1817DW has QS1000 wavetable which is connected
506 * to the serial digital input of the OPTI931.
507 */
508 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff);
509 /*
510 * This bit sets OPTI931 to automaticaly select FM
511 * or digital input signal.
512 */
513 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 break;
515#endif /* OPTi93X */
516
517 default:
518 snd_printk("chip %d not supported\n", chip->hardware);
519 return -EINVAL;
520 }
521
522 switch (chip->wss_base) {
523 case 0x530:
524 wss_base_bits = 0x00;
525 break;
526 case 0x604:
527 wss_base_bits = 0x03;
528 break;
529 case 0xe80:
530 wss_base_bits = 0x01;
531 break;
532 case 0xf40:
533 wss_base_bits = 0x02;
534 break;
535 default:
536 snd_printk("WSS port 0x%lx not valid\n", chip->wss_base);
537 goto __skip_base;
538 }
539 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
540
541__skip_base:
542 switch (chip->irq) {
543//#ifdef OPTi93X
544 case 5:
545 irq_bits = 0x05;
546 break;
547//#endif /* OPTi93X */
548 case 7:
549 irq_bits = 0x01;
550 break;
551 case 9:
552 irq_bits = 0x02;
553 break;
554 case 10:
555 irq_bits = 0x03;
556 break;
557 case 11:
558 irq_bits = 0x04;
559 break;
560 default:
561 snd_printk("WSS irq # %d not valid\n", chip->irq);
562 goto __skip_resources;
563 }
564
565 switch (chip->dma1) {
566 case 0:
567 dma_bits = 0x01;
568 break;
569 case 1:
570 dma_bits = 0x02;
571 break;
572 case 3:
573 dma_bits = 0x03;
574 break;
575 default:
576 snd_printk("WSS dma1 # %d not valid\n", chip->dma1);
577 goto __skip_resources;
578 }
579
580#if defined(CS4231) || defined(OPTi93X)
581 if (chip->dma1 == chip->dma2) {
582 snd_printk("don't want to share dmas\n");
583 return -EBUSY;
584 }
585
586 switch (chip->dma2) {
587 case 0:
588 case 1:
589 break;
590 default:
591 snd_printk("WSS dma2 # %d not valid\n", chip->dma2);
592 goto __skip_resources;
593 }
594 dma_bits |= 0x04;
595#endif /* CS4231 || OPTi93X */
596
597#ifndef OPTi93X
598 outb(irq_bits << 3 | dma_bits, chip->wss_base);
599#else /* OPTi93X */
600 snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
601#endif /* OPTi93X */
602
603__skip_resources:
604 if (chip->hardware > OPTi9XX_HW_82C928) {
605 switch (chip->mpu_port) {
606 case 0:
607 case -1:
608 break;
609 case 0x300:
610 mpu_port_bits = 0x03;
611 break;
612 case 0x310:
613 mpu_port_bits = 0x02;
614 break;
615 case 0x320:
616 mpu_port_bits = 0x01;
617 break;
618 case 0x330:
619 mpu_port_bits = 0x00;
620 break;
621 default:
622 snd_printk("MPU-401 port 0x%lx not valid\n",
623 chip->mpu_port);
624 goto __skip_mpu;
625 }
626
627 switch (chip->mpu_irq) {
628 case 5:
629 mpu_irq_bits = 0x02;
630 break;
631 case 7:
632 mpu_irq_bits = 0x03;
633 break;
634 case 9:
635 mpu_irq_bits = 0x00;
636 break;
637 case 10:
638 mpu_irq_bits = 0x01;
639 break;
640 default:
641 snd_printk("MPU-401 irq # %d not valid\n",
642 chip->mpu_irq);
643 goto __skip_mpu;
644 }
645
646 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
647 (chip->mpu_port <= 0) ? 0x00 :
648 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
649 0xf8);
650 }
651__skip_mpu:
652
653 return 0;
654}
655
656#ifdef OPTi93X
657
658static unsigned char snd_opti93x_default_image[32] =
659{
660 0x00, /* 00/00 - l_mixout_outctrl */
661 0x00, /* 01/01 - r_mixout_outctrl */
662 0x88, /* 02/02 - l_cd_inctrl */
663 0x88, /* 03/03 - r_cd_inctrl */
664 0x88, /* 04/04 - l_a1/fm_inctrl */
665 0x88, /* 05/05 - r_a1/fm_inctrl */
666 0x80, /* 06/06 - l_dac_inctrl */
667 0x80, /* 07/07 - r_dac_inctrl */
668 0x00, /* 08/08 - ply_dataform_reg */
669 0x00, /* 09/09 - if_conf */
670 0x00, /* 0a/10 - pin_ctrl */
671 0x00, /* 0b/11 - err_init_reg */
672 0x0a, /* 0c/12 - id_reg */
673 0x00, /* 0d/13 - reserved */
674 0x00, /* 0e/14 - ply_upcount_reg */
675 0x00, /* 0f/15 - ply_lowcount_reg */
676 0x88, /* 10/16 - reserved/l_a1_inctrl */
677 0x88, /* 11/17 - reserved/r_a1_inctrl */
678 0x88, /* 12/18 - l_line_inctrl */
679 0x88, /* 13/19 - r_line_inctrl */
680 0x88, /* 14/20 - l_mic_inctrl */
681 0x88, /* 15/21 - r_mic_inctrl */
682 0x80, /* 16/22 - l_out_outctrl */
683 0x80, /* 17/23 - r_out_outctrl */
684 0x00, /* 18/24 - reserved */
685 0x00, /* 19/25 - reserved */
686 0x00, /* 1a/26 - reserved */
687 0x00, /* 1b/27 - reserved */
688 0x00, /* 1c/28 - cap_dataform_reg */
689 0x00, /* 1d/29 - reserved */
690 0x00, /* 1e/30 - cap_upcount_reg */
691 0x00 /* 1f/31 - cap_lowcount_reg */
692};
693
694
Takashi Iwai346c7a62005-11-17 14:37:56 +0100695static int snd_opti93x_busy_wait(struct snd_opti93x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 int timeout;
698
699 for (timeout = 250; timeout-- > 0; udelay(10))
700 if (!(inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_INIT))
701 return 0;
702
703 snd_printk("chip still busy.\n");
704 return -EBUSY;
705}
706
Takashi Iwai346c7a62005-11-17 14:37:56 +0100707static unsigned char snd_opti93x_in(struct snd_opti93x *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 snd_opti93x_busy_wait(chip);
710 outb(chip->mce_bit | (reg & 0x1f), OPTi93X_PORT(chip, INDEX));
711 return inb(OPTi93X_PORT(chip, DATA));
712}
713
Takashi Iwai346c7a62005-11-17 14:37:56 +0100714static void snd_opti93x_out(struct snd_opti93x *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 unsigned char value)
716{
717 snd_opti93x_busy_wait(chip);
718 outb(chip->mce_bit | (reg & 0x1f), OPTi93X_PORT(chip, INDEX));
719 outb(value, OPTi93X_PORT(chip, DATA));
720}
721
Takashi Iwai346c7a62005-11-17 14:37:56 +0100722static void snd_opti93x_out_image(struct snd_opti93x *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 unsigned char value)
724{
725 snd_opti93x_out(chip, reg, chip->image[reg] = value);
726}
727
Takashi Iwai346c7a62005-11-17 14:37:56 +0100728static void snd_opti93x_out_mask(struct snd_opti93x *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 unsigned char mask, unsigned char value)
730{
731 snd_opti93x_out_image(chip, reg,
732 (chip->image[reg] & ~mask) | (value & mask));
733}
734
735
Takashi Iwai346c7a62005-11-17 14:37:56 +0100736static void snd_opti93x_mce_up(struct snd_opti93x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 snd_opti93x_busy_wait(chip);
739
740 chip->mce_bit = OPTi93X_MCE;
741 if (!(inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_MCE))
742 outb(chip->mce_bit, OPTi93X_PORT(chip, INDEX));
743}
744
Takashi Iwai346c7a62005-11-17 14:37:56 +0100745static void snd_opti93x_mce_down(struct snd_opti93x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 snd_opti93x_busy_wait(chip);
748
749 chip->mce_bit = 0;
750 if (inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_MCE)
751 outb(chip->mce_bit, OPTi93X_PORT(chip, INDEX));
752}
753
754#define snd_opti93x_mute_reg(chip, reg, mute) \
755 snd_opti93x_out(chip, reg, mute ? 0x80 : chip->image[reg]);
756
Takashi Iwai346c7a62005-11-17 14:37:56 +0100757static void snd_opti93x_mute(struct snd_opti93x *chip, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 mute = mute ? 1 : 0;
760 if (chip->mute == mute)
761 return;
762
763 chip->mute = mute;
764
765 snd_opti93x_mute_reg(chip, OPTi93X_CD_LEFT_INPUT, mute);
766 snd_opti93x_mute_reg(chip, OPTi93X_CD_RIGHT_INPUT, mute);
767 switch (chip->hardware) {
768 case OPTi9XX_HW_82C930:
769 snd_opti93x_mute_reg(chip, OPTi930_AUX_LEFT_INPUT, mute);
770 snd_opti93x_mute_reg(chip, OPTi930_AUX_RIGHT_INPUT, mute);
771 break;
772 case OPTi9XX_HW_82C931:
773 case OPTi9XX_HW_82C933:
774 snd_opti93x_mute_reg(chip, OPTi931_FM_LEFT_INPUT, mute);
775 snd_opti93x_mute_reg(chip, OPTi931_FM_RIGHT_INPUT, mute);
776 snd_opti93x_mute_reg(chip, OPTi931_AUX_LEFT_INPUT, mute);
777 snd_opti93x_mute_reg(chip, OPTi931_AUX_RIGHT_INPUT, mute);
778 }
779 snd_opti93x_mute_reg(chip, OPTi93X_DAC_LEFT, mute);
780 snd_opti93x_mute_reg(chip, OPTi93X_DAC_RIGHT, mute);
781 snd_opti93x_mute_reg(chip, OPTi93X_LINE_LEFT_INPUT, mute);
782 snd_opti93x_mute_reg(chip, OPTi93X_LINE_RIGHT_INPUT, mute);
783 snd_opti93x_mute_reg(chip, OPTi93X_MIC_LEFT_INPUT, mute);
784 snd_opti93x_mute_reg(chip, OPTi93X_MIC_RIGHT_INPUT, mute);
785 snd_opti93x_mute_reg(chip, OPTi93X_OUT_LEFT, mute);
786 snd_opti93x_mute_reg(chip, OPTi93X_OUT_RIGHT, mute);
787}
788
789
790static unsigned int snd_opti93x_get_count(unsigned char format,
791 unsigned int size)
792{
793 switch (format & 0xe0) {
794 case OPTi93X_LINEAR_16_LIT:
795 case OPTi93X_LINEAR_16_BIG:
796 size >>= 1;
797 break;
798 case OPTi93X_ADPCM_16:
799 return size >> 2;
800 }
801 return (format & OPTi93X_STEREO) ? (size >> 1) : size;
802}
803
804static unsigned int rates[] = { 5512, 6615, 8000, 9600, 11025, 16000,
805 18900, 22050, 27428, 32000, 33075, 37800,
806 44100, 48000 };
807#define RATES ARRAY_SIZE(rates)
808
Takashi Iwai346c7a62005-11-17 14:37:56 +0100809static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 .count = RATES,
811 .list = rates,
812 .mask = 0,
813};
814
815static unsigned char bits[] = { 0x01, 0x0f, 0x00, 0x0e, 0x03, 0x02,
816 0x05, 0x07, 0x04, 0x06, 0x0d, 0x09,
817 0x0b, 0x0c};
818
819static unsigned char snd_opti93x_get_freq(unsigned int rate)
820{
821 unsigned int i;
822
823 for (i = 0; i < RATES; i++) {
824 if (rate == rates[i])
825 return bits[i];
826 }
827 snd_BUG();
828 return bits[RATES-1];
829}
830
Takashi Iwai346c7a62005-11-17 14:37:56 +0100831static unsigned char snd_opti93x_get_format(struct snd_opti93x *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 unsigned int format, int channels)
833{
834 unsigned char retval = OPTi93X_LINEAR_8;
835
836 switch (format) {
837 case SNDRV_PCM_FORMAT_MU_LAW:
838 retval = OPTi93X_ULAW_8;
839 break;
840 case SNDRV_PCM_FORMAT_A_LAW:
841 retval = OPTi93X_ALAW_8;
842 break;
843 case SNDRV_PCM_FORMAT_S16_LE:
844 retval = OPTi93X_LINEAR_16_LIT;
845 break;
846 case SNDRV_PCM_FORMAT_S16_BE:
847 retval = OPTi93X_LINEAR_16_BIG;
848 break;
849 case SNDRV_PCM_FORMAT_IMA_ADPCM:
850 retval = OPTi93X_ADPCM_16;
851 }
852 return (channels > 1) ? (retval | OPTi93X_STEREO) : retval;
853}
854
855
Takashi Iwai346c7a62005-11-17 14:37:56 +0100856static void snd_opti93x_playback_format(struct snd_opti93x *chip, unsigned char fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 unsigned char mask;
859
860 snd_opti93x_mute(chip, 1);
861
862 snd_opti93x_mce_up(chip);
863 mask = (chip->mode & OPTi93X_MODE_CAPTURE) ? 0xf0 : 0xff;
864 snd_opti93x_out_mask(chip, OPTi93X_PLAY_FORMAT, mask, fmt);
865 snd_opti93x_mce_down(chip);
866
867 snd_opti93x_mute(chip, 0);
868}
869
Takashi Iwai346c7a62005-11-17 14:37:56 +0100870static void snd_opti93x_capture_format(struct snd_opti93x *chip, unsigned char fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 snd_opti93x_mute(chip, 1);
873
874 snd_opti93x_mce_up(chip);
875 if (!(chip->mode & OPTi93X_MODE_PLAY))
876 snd_opti93x_out_mask(chip, OPTi93X_PLAY_FORMAT, 0x0f, fmt);
877 else
878 fmt = chip->image[OPTi93X_PLAY_FORMAT] & 0xf0;
879 snd_opti93x_out_image(chip, OPTi93X_CAPT_FORMAT, fmt);
880 snd_opti93x_mce_down(chip);
881
882 snd_opti93x_mute(chip, 0);
883}
884
885
Takashi Iwai346c7a62005-11-17 14:37:56 +0100886static int snd_opti93x_open(struct snd_opti93x *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 unsigned long flags;
889
890 spin_lock_irqsave(&chip->lock, flags);
891
892 if (chip->mode & mode) {
893 spin_unlock_irqrestore(&chip->lock, flags);
894 return -EAGAIN;
895 }
896
897 if (!(chip->mode & OPTi93X_MODE_OPEN)) {
898 outb(0x00, OPTi93X_PORT(chip, STATUS));
899 snd_opti93x_out_mask(chip, OPTi93X_PIN_CTRL,
900 OPTi93X_IRQ_ENABLE, OPTi93X_IRQ_ENABLE);
901 chip->mode = mode;
902 }
903 else
904 chip->mode |= mode;
905
906 spin_unlock_irqrestore(&chip->lock, flags);
907 return 0;
908}
909
Takashi Iwai346c7a62005-11-17 14:37:56 +0100910static void snd_opti93x_close(struct snd_opti93x *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 unsigned long flags;
913
914 spin_lock_irqsave(&chip->lock, flags);
915
916 chip->mode &= ~mode;
917 if (chip->mode & OPTi93X_MODE_OPEN) {
918 spin_unlock_irqrestore(&chip->lock, flags);
919 return;
920 }
921
922 snd_opti93x_mute(chip, 1);
923
924 outb(0, OPTi93X_PORT(chip, STATUS));
925 snd_opti93x_out_mask(chip, OPTi93X_PIN_CTRL, OPTi93X_IRQ_ENABLE,
926 ~OPTi93X_IRQ_ENABLE);
927
928 snd_opti93x_mce_up(chip);
929 snd_opti93x_out_image(chip, OPTi93X_IFACE_CONF, 0x00);
930 snd_opti93x_mce_down(chip);
931 chip->mode = 0;
932
933 snd_opti93x_mute(chip, 0);
934 spin_unlock_irqrestore(&chip->lock, flags);
935}
936
Takashi Iwai346c7a62005-11-17 14:37:56 +0100937static int snd_opti93x_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 unsigned char what, int cmd)
939{
Takashi Iwai346c7a62005-11-17 14:37:56 +0100940 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 switch (cmd) {
943 case SNDRV_PCM_TRIGGER_START:
944 case SNDRV_PCM_TRIGGER_STOP:
945 {
946 unsigned int what = 0;
Takashi Iwai346c7a62005-11-17 14:37:56 +0100947 struct snd_pcm_substream *s;
Takashi Iwaief991b92007-02-22 12:52:53 +0100948 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (s == chip->playback_substream) {
950 what |= OPTi93X_PLAYBACK_ENABLE;
951 snd_pcm_trigger_done(s, substream);
952 } else if (s == chip->capture_substream) {
953 what |= OPTi93X_CAPTURE_ENABLE;
954 snd_pcm_trigger_done(s, substream);
955 }
956 }
957 spin_lock(&chip->lock);
958 if (cmd == SNDRV_PCM_TRIGGER_START) {
959 snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF, what, what);
960 if (what & OPTi93X_CAPTURE_ENABLE)
961 udelay(50);
962 } else
963 snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF, what, 0x00);
964 spin_unlock(&chip->lock);
965 break;
966 }
967 default:
968 return -EINVAL;
969 }
970 return 0;
971}
972
Takashi Iwai346c7a62005-11-17 14:37:56 +0100973static int snd_opti93x_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 return snd_opti93x_trigger(substream,
976 OPTi93X_PLAYBACK_ENABLE, cmd);
977}
978
Takashi Iwai346c7a62005-11-17 14:37:56 +0100979static int snd_opti93x_capture_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 return snd_opti93x_trigger(substream,
982 OPTi93X_CAPTURE_ENABLE, cmd);
983}
984
Takashi Iwai346c7a62005-11-17 14:37:56 +0100985static int snd_opti93x_hw_params(struct snd_pcm_substream *substream,
986 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
989}
990
991
Takashi Iwai346c7a62005-11-17 14:37:56 +0100992static int snd_opti93x_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994 snd_pcm_lib_free_pages(substream);
995 return 0;
996}
997
998
Takashi Iwai346c7a62005-11-17 14:37:56 +0100999static int snd_opti93x_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001001 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
1002 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 unsigned long flags;
1004 unsigned char format;
1005 unsigned int count = snd_pcm_lib_period_bytes(substream);
1006 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1007
1008 spin_lock_irqsave(&chip->lock, flags);
1009
1010 chip->p_dma_size = size;
1011 snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF,
1012 OPTi93X_PLAYBACK_ENABLE | OPTi93X_PLAYBACK_PIO,
1013 ~(OPTi93X_PLAYBACK_ENABLE | OPTi93X_PLAYBACK_PIO));
1014
1015 snd_dma_program(chip->dma1, runtime->dma_addr, size,
1016 DMA_MODE_WRITE | DMA_AUTOINIT);
1017
1018 format = snd_opti93x_get_freq(runtime->rate);
1019 format |= snd_opti93x_get_format(chip, runtime->format,
1020 runtime->channels);
1021 snd_opti93x_playback_format(chip, format);
1022 format = chip->image[OPTi93X_PLAY_FORMAT];
1023
1024 count = snd_opti93x_get_count(format, count) - 1;
1025 snd_opti93x_out_image(chip, OPTi93X_PLAY_LWR_CNT, count);
1026 snd_opti93x_out_image(chip, OPTi93X_PLAY_UPR_CNT, count >> 8);
1027
1028 spin_unlock_irqrestore(&chip->lock, flags);
1029 return 0;
1030}
1031
Takashi Iwai346c7a62005-11-17 14:37:56 +01001032static int snd_opti93x_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001034 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
1035 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 unsigned long flags;
1037 unsigned char format;
1038 unsigned int count = snd_pcm_lib_period_bytes(substream);
1039 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1040
1041 spin_lock_irqsave(&chip->lock, flags);
1042
1043 chip->c_dma_size = size;
1044 snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF,
Clemens Ladischdb673192005-09-05 10:36:27 +02001045 OPTi93X_CAPTURE_ENABLE | OPTi93X_CAPTURE_PIO, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 snd_dma_program(chip->dma2, runtime->dma_addr, size,
1048 DMA_MODE_READ | DMA_AUTOINIT);
1049
1050 format = snd_opti93x_get_freq(runtime->rate);
1051 format |= snd_opti93x_get_format(chip, runtime->format,
1052 runtime->channels);
1053 snd_opti93x_capture_format(chip, format);
1054 format = chip->image[OPTi93X_CAPT_FORMAT];
1055
1056 count = snd_opti93x_get_count(format, count) - 1;
1057 snd_opti93x_out_image(chip, OPTi93X_CAPT_LWR_CNT, count);
1058 snd_opti93x_out_image(chip, OPTi93X_CAPT_UPR_CNT, count >> 8);
1059
1060 spin_unlock_irqrestore(&chip->lock, flags);
1061 return 0;
1062}
1063
Takashi Iwai346c7a62005-11-17 14:37:56 +01001064static snd_pcm_uframes_t snd_opti93x_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001066 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 size_t ptr;
1068
1069 if (!(chip->image[OPTi93X_IFACE_CONF] & OPTi93X_PLAYBACK_ENABLE))
1070 return 0;
1071
1072 ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
1073 return bytes_to_frames(substream->runtime, ptr);
1074}
1075
Takashi Iwai346c7a62005-11-17 14:37:56 +01001076static snd_pcm_uframes_t snd_opti93x_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001078 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 size_t ptr;
1080
1081 if (!(chip->image[OPTi93X_IFACE_CONF] & OPTi93X_CAPTURE_ENABLE))
1082 return 0;
1083
1084 ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
1085 return bytes_to_frames(substream->runtime, ptr);
1086}
1087
1088
Takashi Iwai346c7a62005-11-17 14:37:56 +01001089static void snd_opti93x_overrange(struct snd_opti93x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 unsigned long flags;
1092
1093 spin_lock_irqsave(&chip->lock, flags);
1094
1095 if (snd_opti93x_in(chip, OPTi93X_ERR_INIT) & (0x08 | 0x02))
1096 chip->capture_substream->runtime->overrange++;
1097
1098 spin_unlock_irqrestore(&chip->lock, flags);
1099}
1100
David Howells7d12e782006-10-05 14:55:46 +01001101static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001103 struct snd_opti93x *codec = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 unsigned char status;
1105
1106 status = snd_opti9xx_read(codec->chip, OPTi9XX_MC_REG(11));
1107 if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
1108 snd_pcm_period_elapsed(codec->playback_substream);
1109 if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
1110 snd_opti93x_overrange(codec);
1111 snd_pcm_period_elapsed(codec->capture_substream);
1112 }
1113 outb(0x00, OPTi93X_PORT(codec, STATUS));
1114 return IRQ_HANDLED;
1115}
1116
1117
Takashi Iwai346c7a62005-11-17 14:37:56 +01001118static struct snd_pcm_hardware snd_opti93x_playback = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1120 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
1121 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1122 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1123 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1124 .rate_min = 5512,
1125 .rate_max = 48000,
1126 .channels_min = 1,
1127 .channels_max = 2,
1128 .buffer_bytes_max = (128*1024),
1129 .period_bytes_min = 64,
1130 .period_bytes_max = (128*1024),
1131 .periods_min = 1,
1132 .periods_max = 1024,
1133 .fifo_size = 0,
1134};
1135
Takashi Iwai346c7a62005-11-17 14:37:56 +01001136static struct snd_pcm_hardware snd_opti93x_capture = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1138 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
1139 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1140 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1141 .rates = SNDRV_PCM_RATE_8000_48000,
1142 .rate_min = 5512,
1143 .rate_max = 48000,
1144 .channels_min = 1,
1145 .channels_max = 2,
1146 .buffer_bytes_max = (128*1024),
1147 .period_bytes_min = 64,
1148 .period_bytes_max = (128*1024),
1149 .periods_min = 1,
1150 .periods_max = 1024,
1151 .fifo_size = 0,
1152};
1153
Takashi Iwai346c7a62005-11-17 14:37:56 +01001154static int snd_opti93x_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
1156 int error;
Takashi Iwai346c7a62005-11-17 14:37:56 +01001157 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
1158 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 if ((error = snd_opti93x_open(chip, OPTi93X_MODE_PLAY)) < 0)
1161 return error;
1162 snd_pcm_set_sync(substream);
1163 chip->playback_substream = substream;
1164 runtime->hw = snd_opti93x_playback;
1165 snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
1166 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1167 return error;
1168}
1169
Takashi Iwai346c7a62005-11-17 14:37:56 +01001170static int snd_opti93x_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 int error;
Takashi Iwai346c7a62005-11-17 14:37:56 +01001173 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
1174 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 if ((error = snd_opti93x_open(chip, OPTi93X_MODE_CAPTURE)) < 0)
1177 return error;
1178 runtime->hw = snd_opti93x_capture;
1179 snd_pcm_set_sync(substream);
1180 chip->capture_substream = substream;
1181 snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
1182 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1183 return error;
1184}
1185
Takashi Iwai346c7a62005-11-17 14:37:56 +01001186static int snd_opti93x_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001188 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 chip->playback_substream = NULL;
1191 snd_opti93x_close(chip, OPTi93X_MODE_PLAY);
1192 return 0;
1193}
1194
Takashi Iwai346c7a62005-11-17 14:37:56 +01001195static int snd_opti93x_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001197 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 chip->capture_substream = NULL;
1200 snd_opti93x_close(chip, OPTi93X_MODE_CAPTURE);
1201 return 0;
1202}
1203
1204
Takashi Iwai346c7a62005-11-17 14:37:56 +01001205static void snd_opti93x_init(struct snd_opti93x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 unsigned long flags;
1208 int i;
1209
1210 spin_lock_irqsave(&chip->lock, flags);
1211 snd_opti93x_mce_up(chip);
1212
1213 for (i = 0; i < 32; i++)
1214 snd_opti93x_out_image(chip, i, snd_opti93x_default_image[i]);
1215
1216 snd_opti93x_mce_down(chip);
1217 spin_unlock_irqrestore(&chip->lock, flags);
1218}
1219
Takashi Iwai346c7a62005-11-17 14:37:56 +01001220static int snd_opti93x_probe(struct snd_opti93x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 unsigned long flags;
1223 unsigned char val;
1224
1225 spin_lock_irqsave(&chip->lock, flags);
1226 val = snd_opti93x_in(chip, OPTi93X_ID) & 0x0f;
1227 spin_unlock_irqrestore(&chip->lock, flags);
1228
1229 return (val == 0x0a) ? 0 : -ENODEV;
1230}
1231
Takashi Iwai346c7a62005-11-17 14:37:56 +01001232static int snd_opti93x_free(struct snd_opti93x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Takashi Iwaib1d57762005-10-10 11:56:31 +02001234 release_and_free_resource(chip->res_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (chip->dma1 >= 0) {
1236 disable_dma(chip->dma1);
1237 free_dma(chip->dma1);
1238 }
1239 if (chip->dma2 >= 0) {
1240 disable_dma(chip->dma2);
1241 free_dma(chip->dma2);
1242 }
1243 if (chip->irq >= 0) {
1244 free_irq(chip->irq, chip);
1245 }
1246 kfree(chip);
1247 return 0;
1248}
1249
Takashi Iwai346c7a62005-11-17 14:37:56 +01001250static int snd_opti93x_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001252 struct snd_opti93x *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return snd_opti93x_free(chip);
1254}
1255
Takashi Iwai346c7a62005-11-17 14:37:56 +01001256static const char *snd_opti93x_chip_id(struct snd_opti93x *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
1258 switch (codec->hardware) {
1259 case OPTi9XX_HW_82C930: return "82C930";
1260 case OPTi9XX_HW_82C931: return "82C931";
1261 case OPTi9XX_HW_82C933: return "82C933";
1262 default: return "???";
1263 }
1264}
1265
Takashi Iwai346c7a62005-11-17 14:37:56 +01001266static int snd_opti93x_create(struct snd_card *card, struct snd_opti9xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 int dma1, int dma2,
Takashi Iwai346c7a62005-11-17 14:37:56 +01001268 struct snd_opti93x **rcodec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001270 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 .dev_free = snd_opti93x_dev_free,
1272 };
1273 int error;
Takashi Iwai346c7a62005-11-17 14:37:56 +01001274 struct snd_opti93x *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 *rcodec = NULL;
Takashi Iwai9e76a762005-09-09 14:21:17 +02001277 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (codec == NULL)
1279 return -ENOMEM;
1280 codec->irq = -1;
1281 codec->dma1 = -1;
1282 codec->dma2 = -1;
1283
1284 if ((codec->res_port = request_region(chip->wss_base + 4, 4, "OPTI93x CODEC")) == NULL) {
1285 snd_printk(KERN_ERR "opti9xx: can't grab port 0x%lx\n", chip->wss_base + 4);
1286 snd_opti93x_free(codec);
1287 return -EBUSY;
1288 }
1289 if (request_dma(dma1, "OPTI93x - 1")) {
1290 snd_printk(KERN_ERR "opti9xx: can't grab DMA1 %d\n", dma1);
1291 snd_opti93x_free(codec);
1292 return -EBUSY;
1293 }
1294 codec->dma1 = chip->dma1;
1295 if (request_dma(dma2, "OPTI93x - 2")) {
1296 snd_printk(KERN_ERR "opti9xx: can't grab DMA2 %d\n", dma2);
1297 snd_opti93x_free(codec);
1298 return -EBUSY;
1299 }
1300 codec->dma2 = chip->dma2;
1301
Rene Herman83c51c02007-03-20 11:33:46 +01001302 if (request_irq(chip->irq, snd_opti93x_interrupt, IRQF_DISABLED, DEV_NAME" - WSS", codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", chip->irq);
1304 snd_opti93x_free(codec);
1305 return -EBUSY;
1306 }
1307
1308 codec->card = card;
1309 codec->port = chip->wss_base + 4;
1310 codec->irq = chip->irq;
1311
1312 spin_lock_init(&codec->lock);
1313 codec->hardware = chip->hardware;
1314 codec->chip = chip;
1315
1316 if ((error = snd_opti93x_probe(codec))) {
1317 snd_opti93x_free(codec);
1318 return error;
1319 }
1320
1321 snd_opti93x_init(codec);
1322
1323 /* Register device */
1324 if ((error = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
1325 snd_opti93x_free(codec);
1326 return error;
1327 }
1328
1329 *rcodec = codec;
1330 return 0;
1331}
1332
Takashi Iwai346c7a62005-11-17 14:37:56 +01001333static struct snd_pcm_ops snd_opti93x_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 .open = snd_opti93x_playback_open,
1335 .close = snd_opti93x_playback_close,
1336 .ioctl = snd_pcm_lib_ioctl,
1337 .hw_params = snd_opti93x_hw_params,
1338 .hw_free = snd_opti93x_hw_free,
1339 .prepare = snd_opti93x_playback_prepare,
1340 .trigger = snd_opti93x_playback_trigger,
1341 .pointer = snd_opti93x_playback_pointer,
1342};
1343
Takashi Iwai346c7a62005-11-17 14:37:56 +01001344static struct snd_pcm_ops snd_opti93x_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 .open = snd_opti93x_capture_open,
1346 .close = snd_opti93x_capture_close,
1347 .ioctl = snd_pcm_lib_ioctl,
1348 .hw_params = snd_opti93x_hw_params,
1349 .hw_free = snd_opti93x_hw_free,
1350 .prepare = snd_opti93x_capture_prepare,
1351 .trigger = snd_opti93x_capture_trigger,
1352 .pointer = snd_opti93x_capture_pointer,
1353};
1354
Takashi Iwai346c7a62005-11-17 14:37:56 +01001355static int snd_opti93x_pcm(struct snd_opti93x *codec, int device, struct snd_pcm **rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 int error;
Takashi Iwai346c7a62005-11-17 14:37:56 +01001358 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Takashi Iwai91134852006-01-23 15:21:05 +01001360 if ((error = snd_pcm_new(codec->card, "OPTi 82C93X", device, 1, 1, &pcm)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return error;
1362
1363 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_opti93x_playback_ops);
1364 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_opti93x_capture_ops);
1365
1366 pcm->private_data = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
1368
1369 strcpy(pcm->name, snd_opti93x_chip_id(codec));
1370
1371 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1372 snd_dma_isa_data(),
1373 64*1024, codec->dma1 > 3 || codec->dma2 > 3 ? 128*1024 : 64*1024);
1374
1375 codec->pcm = pcm;
1376 if (rpcm)
1377 *rpcm = pcm;
1378 return 0;
1379}
1380
1381/*
1382 * MIXER part
1383 */
1384
Takashi Iwai346c7a62005-11-17 14:37:56 +01001385static int snd_opti93x_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
1387 static char *texts[4] = {
1388 "Line1", "Aux", "Mic", "Mix"
1389 };
1390
1391 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1392 uinfo->count = 2;
1393 uinfo->value.enumerated.items = 4;
1394 if (uinfo->value.enumerated.item > 3)
1395 uinfo->value.enumerated.item = 3;
1396 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1397 return 0;
1398}
1399
Takashi Iwai346c7a62005-11-17 14:37:56 +01001400static int snd_opti93x_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001402 struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 unsigned long flags;
1404
1405 spin_lock_irqsave(&chip->lock, flags);
1406 ucontrol->value.enumerated.item[0] = (chip->image[OPTi93X_MIXOUT_LEFT] & OPTi93X_MIXOUT_MIXER) >> 6;
1407 ucontrol->value.enumerated.item[1] = (chip->image[OPTi93X_MIXOUT_RIGHT] & OPTi93X_MIXOUT_MIXER) >> 6;
1408 spin_unlock_irqrestore(&chip->lock, flags);
1409 return 0;
1410}
1411
Takashi Iwai346c7a62005-11-17 14:37:56 +01001412static int snd_opti93x_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001414 struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 unsigned long flags;
1416 unsigned short left, right;
1417 int change;
1418
1419 if (ucontrol->value.enumerated.item[0] > 3 ||
1420 ucontrol->value.enumerated.item[1] > 3)
1421 return -EINVAL;
1422 left = ucontrol->value.enumerated.item[0] << 6;
1423 right = ucontrol->value.enumerated.item[1] << 6;
1424 spin_lock_irqsave(&chip->lock, flags);
1425 left = (chip->image[OPTi93X_MIXOUT_LEFT] & ~OPTi93X_MIXOUT_MIXER) | left;
1426 right = (chip->image[OPTi93X_MIXOUT_RIGHT] & ~OPTi93X_MIXOUT_MIXER) | right;
1427 change = left != chip->image[OPTi93X_MIXOUT_LEFT] ||
1428 right != chip->image[OPTi93X_MIXOUT_RIGHT];
1429 snd_opti93x_out_image(chip, OPTi93X_MIXOUT_LEFT, left);
1430 snd_opti93x_out_image(chip, OPTi93X_MIXOUT_RIGHT, right);
1431 spin_unlock_irqrestore(&chip->lock, flags);
1432 return change;
1433}
1434
1435#if 0
1436
1437#define OPTi93X_SINGLE(xname, xindex, reg, shift, mask, invert) \
1438{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1439 .info = snd_opti93x_info_single, \
1440 .get = snd_opti93x_get_single, .put = snd_opti93x_put_single, \
1441 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1442
Takashi Iwai346c7a62005-11-17 14:37:56 +01001443static int snd_opti93x_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 int mask = (kcontrol->private_value >> 16) & 0xff;
1446
1447 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1448 uinfo->count = 1;
1449 uinfo->value.integer.min = 0;
1450 uinfo->value.integer.max = mask;
1451 return 0;
1452}
1453
Takashi Iwai346c7a62005-11-17 14:37:56 +01001454static int snd_opti93x_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001456 struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 unsigned long flags;
1458 int reg = kcontrol->private_value & 0xff;
1459 int shift = (kcontrol->private_value >> 8) & 0xff;
1460 int mask = (kcontrol->private_value >> 16) & 0xff;
1461 int invert = (kcontrol->private_value >> 24) & 0xff;
1462
1463 spin_lock_irqsave(&chip->lock, flags);
1464 ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
1465 spin_unlock_irqrestore(&chip->lock, flags);
1466 if (invert)
1467 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1468 return 0;
1469}
1470
Takashi Iwai346c7a62005-11-17 14:37:56 +01001471static int snd_opti93x_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001473 struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 unsigned long flags;
1475 int reg = kcontrol->private_value & 0xff;
1476 int shift = (kcontrol->private_value >> 8) & 0xff;
1477 int mask = (kcontrol->private_value >> 16) & 0xff;
1478 int invert = (kcontrol->private_value >> 24) & 0xff;
1479 int change;
1480 unsigned short val;
1481
1482 val = (ucontrol->value.integer.value[0] & mask);
1483 if (invert)
1484 val = mask - val;
1485 val <<= shift;
1486 spin_lock_irqsave(&chip->lock, flags);
1487 val = (chip->image[reg] & ~(mask << shift)) | val;
1488 change = val != chip->image[reg];
1489 snd_opti93x_out(chip, reg, val);
1490 spin_unlock_irqrestore(&chip->lock, flags);
1491 return change;
1492}
1493
1494#endif /* single */
1495
1496#define OPTi93X_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1497{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1498 .info = snd_opti93x_info_double, \
1499 .get = snd_opti93x_get_double, .put = snd_opti93x_put_double, \
1500 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1501
1502#define OPTi93X_DOUBLE_INVERT_INVERT(xctl) \
1503 do { xctl.private_value ^= 22; } while (0)
1504#define OPTi93X_DOUBLE_CHANGE_REGS(xctl, left_reg, right_reg) \
1505 do { xctl.private_value &= ~0x0000ffff; \
1506 xctl.private_value |= left_reg | (right_reg << 8); } while (0)
1507
Takashi Iwai346c7a62005-11-17 14:37:56 +01001508static int snd_opti93x_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 int mask = (kcontrol->private_value >> 24) & 0xff;
1511
1512 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1513 uinfo->count = 2;
1514 uinfo->value.integer.min = 0;
1515 uinfo->value.integer.max = mask;
1516 return 0;
1517}
1518
Takashi Iwai346c7a62005-11-17 14:37:56 +01001519static int snd_opti93x_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001521 struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 unsigned long flags;
1523 int left_reg = kcontrol->private_value & 0xff;
1524 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1525 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1526 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1527 int mask = (kcontrol->private_value >> 24) & 0xff;
1528 int invert = (kcontrol->private_value >> 22) & 1;
1529
1530 spin_lock_irqsave(&chip->lock, flags);
1531 ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
1532 ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
1533 spin_unlock_irqrestore(&chip->lock, flags);
1534 if (invert) {
1535 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1536 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1537 }
1538 return 0;
1539}
1540
Takashi Iwai346c7a62005-11-17 14:37:56 +01001541static int snd_opti93x_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001543 struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 unsigned long flags;
1545 int left_reg = kcontrol->private_value & 0xff;
1546 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1547 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1548 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1549 int mask = (kcontrol->private_value >> 24) & 0xff;
1550 int invert = (kcontrol->private_value >> 22) & 1;
1551 int change;
1552 unsigned short val1, val2;
1553
1554 val1 = ucontrol->value.integer.value[0] & mask;
1555 val2 = ucontrol->value.integer.value[1] & mask;
1556 if (invert) {
1557 val1 = mask - val1;
1558 val2 = mask - val2;
1559 }
1560 val1 <<= shift_left;
1561 val2 <<= shift_right;
1562 spin_lock_irqsave(&chip->lock, flags);
1563 val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
1564 val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
1565 change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
1566 snd_opti93x_out_image(chip, left_reg, val1);
1567 snd_opti93x_out_image(chip, right_reg, val2);
1568 spin_unlock_irqrestore(&chip->lock, flags);
1569 return change;
1570}
1571
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001572static struct snd_kcontrol_new snd_opti93x_controls[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573OPTi93X_DOUBLE("Master Playback Switch", 0, OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
1574OPTi93X_DOUBLE("Master Playback Volume", 0, OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1),
1575OPTi93X_DOUBLE("PCM Playback Switch", 0, OPTi93X_DAC_LEFT, OPTi93X_DAC_RIGHT, 7, 7, 1, 1),
1576OPTi93X_DOUBLE("PCM Playback Volume", 0, OPTi93X_DAC_LEFT, OPTi93X_DAC_RIGHT, 0, 0, 31, 1),
1577OPTi93X_DOUBLE("FM Playback Switch", 0, OPTi931_FM_LEFT_INPUT, OPTi931_FM_RIGHT_INPUT, 7, 7, 1, 1),
1578OPTi93X_DOUBLE("FM Playback Volume", 0, OPTi931_FM_LEFT_INPUT, OPTi931_FM_RIGHT_INPUT, 1, 1, 15, 1),
1579OPTi93X_DOUBLE("Line Playback Switch", 0, OPTi93X_LINE_LEFT_INPUT, OPTi93X_LINE_RIGHT_INPUT, 7, 7, 1, 1),
1580OPTi93X_DOUBLE("Line Playback Volume", 0, OPTi93X_LINE_LEFT_INPUT, OPTi93X_LINE_RIGHT_INPUT, 1, 1, 15, 1),
1581OPTi93X_DOUBLE("Mic Playback Switch", 0, OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
1582OPTi93X_DOUBLE("Mic Playback Volume", 0, OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1),
1583OPTi93X_DOUBLE("Mic Boost", 0, OPTi93X_MIXOUT_LEFT, OPTi93X_MIXOUT_RIGHT, 5, 5, 1, 1),
1584OPTi93X_DOUBLE("CD Playback Switch", 0, OPTi93X_CD_LEFT_INPUT, OPTi93X_CD_RIGHT_INPUT, 7, 7, 1, 1),
1585OPTi93X_DOUBLE("CD Playback Volume", 0, OPTi93X_CD_LEFT_INPUT, OPTi93X_CD_RIGHT_INPUT, 1, 1, 15, 1),
1586OPTi93X_DOUBLE("Aux Playback Switch", 0, OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
1587OPTi93X_DOUBLE("Aux Playback Volume", 0, OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1),
1588OPTi93X_DOUBLE("Capture Volume", 0, OPTi93X_MIXOUT_LEFT, OPTi93X_MIXOUT_RIGHT, 0, 0, 15, 0),
1589{
1590 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1591 .name = "Capture Source",
1592 .info = snd_opti93x_info_mux,
1593 .get = snd_opti93x_get_mux,
1594 .put = snd_opti93x_put_mux,
1595}
1596};
1597
Takashi Iwai346c7a62005-11-17 14:37:56 +01001598static int snd_opti93x_mixer(struct snd_opti93x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001600 struct snd_card *card;
1601 struct snd_kcontrol_new knew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 int err;
1603 unsigned int idx;
1604
1605 snd_assert(chip != NULL && chip->card != NULL, return -EINVAL);
1606
1607 card = chip->card;
1608
1609 strcpy(card->mixername, snd_opti93x_chip_id(chip));
1610
1611 for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
1612 knew = snd_opti93x_controls[idx];
1613 if (chip->hardware == OPTi9XX_HW_82C930) {
1614 if (strstr(knew.name, "FM")) /* skip FM controls */
1615 continue;
1616 else if (strcmp(knew.name, "Mic Playback Volume"))
1617 OPTi93X_DOUBLE_INVERT_INVERT(knew);
1618 else if (strstr(knew.name, "Aux"))
1619 OPTi93X_DOUBLE_CHANGE_REGS(knew, OPTi930_AUX_LEFT_INPUT, OPTi930_AUX_RIGHT_INPUT);
1620 else if (strcmp(knew.name, "PCM Playback Volume"))
1621 OPTi93X_DOUBLE_INVERT_INVERT(knew);
1622 else if (strcmp(knew.name, "Master Playback Volume"))
1623 OPTi93X_DOUBLE_INVERT_INVERT(knew);
1624 }
1625 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_opti93x_controls[idx], chip))) < 0)
1626 return err;
1627 }
1628 return 0;
1629}
1630
1631#endif /* OPTi93X */
1632
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001633static int __devinit snd_card_opti9xx_detect(struct snd_card *card,
1634 struct snd_opti9xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 int i, err;
1637
1638#ifndef OPTi93X
1639 for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
1640 unsigned char value;
1641
1642 if ((err = snd_opti9xx_init(chip, i)) < 0)
1643 return err;
1644
1645 if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
1646 continue;
1647
1648 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
1649 if ((value != 0xff) && (value != inb(chip->mc_base + 1)))
1650 if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
1651 return 1;
1652
Takashi Iwaib1d57762005-10-10 11:56:31 +02001653 release_and_free_resource(chip->res_mc_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 chip->res_mc_base = NULL;
1655
1656 }
1657#else /* OPTi93X */
1658 for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
1659 unsigned long flags;
1660 unsigned char value;
1661
1662 if ((err = snd_opti9xx_init(chip, i)) < 0)
1663 return err;
1664
1665 if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
1666 continue;
1667
1668 spin_lock_irqsave(&chip->lock, flags);
1669 outb(chip->password, chip->mc_base + chip->pwd_reg);
1670 outb(((chip->mc_indir_index & (1 << 8)) >> 4) |
1671 ((chip->mc_indir_index & 0xf0) >> 4), chip->mc_base);
1672 spin_unlock_irqrestore(&chip->lock, flags);
1673
1674 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
1675 snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
1676 if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
1677 return 1;
1678
Takashi Iwaib1d57762005-10-10 11:56:31 +02001679 release_and_free_resource(chip->res_mc_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 chip->res_mc_base = NULL;
1681 }
1682#endif /* OPTi93X */
1683
1684 return -ENODEV;
1685}
1686
1687#ifdef CONFIG_PNP
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001688static int __devinit snd_card_opti9xx_pnp(struct snd_opti9xx *chip,
1689 struct pnp_card_link *card,
1690 const struct pnp_card_device_id *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
1692 struct pnp_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 int err;
1694
1695 chip->dev = pnp_request_card_device(card, pid->devs[0].id, NULL);
Rene Herman109c53f842007-11-30 17:59:25 +01001696 if (chip->dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return -EBUSY;
Rene Herman109c53f842007-11-30 17:59:25 +01001698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 chip->devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
1700
1701 pdev = chip->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 err = pnp_activate_dev(pdev);
1704 if (err < 0) {
1705 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return err;
1707 }
1708
1709#ifdef OPTi93X
1710 port = pnp_port_start(pdev, 0) - 4;
Takashi Iwai1ea73412007-09-17 16:44:06 +02001711 fm_port = pnp_port_start(pdev, 1) + 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712#else
1713 if (pid->driver_data != 0x0924)
1714 port = pnp_port_start(pdev, 1);
Takashi Iwai1ea73412007-09-17 16:44:06 +02001715 fm_port = pnp_port_start(pdev, 2) + 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716#endif /* OPTi93X */
1717 irq = pnp_irq(pdev, 0);
1718 dma1 = pnp_dma(pdev, 0);
1719#if defined(CS4231) || defined(OPTi93X)
1720 dma2 = pnp_dma(pdev, 1);
1721#endif /* CS4231 || OPTi93X */
1722
1723 pdev = chip->devmpu;
1724 if (pdev && mpu_port > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 err = pnp_activate_dev(pdev);
1726 if (err < 0) {
1727 snd_printk(KERN_ERR "AUDIO pnp configure failure\n");
1728 mpu_port = -1;
1729 chip->devmpu = NULL;
1730 } else {
1731 mpu_port = pnp_port_start(pdev, 0);
1732 mpu_irq = pnp_irq(pdev, 0);
1733 }
1734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return pid->driver_data;
1736}
1737#endif /* CONFIG_PNP */
1738
Takashi Iwai346c7a62005-11-17 14:37:56 +01001739static void snd_card_opti9xx_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
Takashi Iwai99a0b762005-11-17 17:13:59 +01001741 struct snd_opti9xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Takashi Iwaib1d57762005-10-10 11:56:31 +02001743 if (chip)
1744 release_and_free_resource(chip->res_mc_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001747static int __devinit snd_opti9xx_probe(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
1749 static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 int error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001751 struct snd_opti9xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752#if defined(OPTi93X)
Takashi Iwai346c7a62005-11-17 14:37:56 +01001753 struct snd_opti93x *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754#elif defined(CS4231)
Takashi Iwai346c7a62005-11-17 14:37:56 +01001755 struct snd_cs4231 *codec;
1756 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757#else
Takashi Iwai346c7a62005-11-17 14:37:56 +01001758 struct snd_ad1848 *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759#endif
Takashi Iwai346c7a62005-11-17 14:37:56 +01001760 struct snd_pcm *pcm;
1761 struct snd_rawmidi *rmidi;
1762 struct snd_hwdep *synth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 if (! chip->res_mc_base &&
Takashi Iwai99a0b762005-11-17 17:13:59 +01001765 (chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
1766 "OPTi9xx MC")) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 chip->wss_base = port;
1770 chip->fm_port = fm_port;
1771 chip->mpu_port = mpu_port;
1772 chip->irq = irq;
1773 chip->mpu_irq = mpu_irq;
1774 chip->dma1 = dma1;
1775#if defined(CS4231) || defined(OPTi93X)
1776 chip->dma2 = dma2;
1777#endif
1778
1779 if (chip->wss_base == SNDRV_AUTO_PORT) {
1780 if ((chip->wss_base = snd_legacy_find_free_ioport(possible_ports, 4)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 snd_printk("unable to find a free WSS port\n");
1782 return -EBUSY;
1783 }
1784 }
Takashi Iwai99a0b762005-11-17 17:13:59 +01001785 if ((error = snd_opti9xx_configure(chip)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788#if defined(OPTi93X)
Takashi Iwai99a0b762005-11-17 17:13:59 +01001789 if ((error = snd_opti93x_create(card, chip, chip->dma1, chip->dma2, &codec)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001791 if ((error = snd_opti93x_pcm(codec, 0, &pcm)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001793 if ((error = snd_opti93x_mixer(codec)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795#elif defined(CS4231)
1796 if ((error = snd_cs4231_create(card, chip->wss_base + 4, -1,
1797 chip->irq, chip->dma1, chip->dma2,
1798 CS4231_HW_DETECT,
1799 0,
Takashi Iwai99a0b762005-11-17 17:13:59 +01001800 &codec)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001802 if ((error = snd_cs4231_pcm(codec, 0, &pcm)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001804 if ((error = snd_cs4231_mixer(codec)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001806 if ((error = snd_cs4231_timer(codec, 0, &timer)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808#else
1809 if ((error = snd_ad1848_create(card, chip->wss_base + 4,
1810 chip->irq, chip->dma1,
Takashi Iwai99a0b762005-11-17 17:13:59 +01001811 AD1848_HW_DETECT, &codec)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001813 if ((error = snd_ad1848_pcm(codec, 0, &pcm)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001815 if ((error = snd_ad1848_mixer(codec)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817#endif
1818 strcpy(card->driver, chip->name);
1819 sprintf(card->shortname, "OPTi %s", card->driver);
1820#if defined(CS4231) || defined(OPTi93X)
1821 sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
1822 card->shortname, pcm->name, chip->wss_base + 4,
1823 chip->irq, chip->dma1, chip->dma2);
1824#else
1825 sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
1826 card->shortname, pcm->name, chip->wss_base + 4,
1827 chip->irq, chip->dma1);
1828#endif /* CS4231 || OPTi93X */
1829
1830 if (chip->mpu_port <= 0 || chip->mpu_port == SNDRV_AUTO_PORT)
1831 rmidi = NULL;
1832 else
1833 if ((error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
Thomas Gleixner65ca68b2006-07-01 19:29:46 -07001834 chip->mpu_port, 0, chip->mpu_irq, IRQF_DISABLED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 &rmidi)))
Takashi Iwai99a0b762005-11-17 17:13:59 +01001836 snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
1837 chip->mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
Takashi Iwai346c7a62005-11-17 14:37:56 +01001840 struct snd_opl3 *opl3 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841#ifndef OPTi93X
1842 if (chip->hardware == OPTi9XX_HW_82C928 ||
1843 chip->hardware == OPTi9XX_HW_82C929 ||
1844 chip->hardware == OPTi9XX_HW_82C924) {
Takashi Iwai346c7a62005-11-17 14:37:56 +01001845 struct snd_opl4 *opl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 /* assume we have an OPL4 */
1847 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
1848 0x20, 0x20);
1849 if (snd_opl4_create(card,
1850 chip->fm_port,
1851 chip->fm_port - 8,
1852 2, &opl3, &opl4) < 0) {
1853 /* no luck, use OPL3 instead */
1854 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
1855 0x00, 0x20);
1856 }
1857 }
1858#endif /* !OPTi93X */
1859 if (!opl3 && snd_opl3_create(card,
1860 chip->fm_port,
1861 chip->fm_port + 2,
1862 OPL3_HW_AUTO, 0, &opl3) < 0) {
Takashi Iwai99a0b762005-11-17 17:13:59 +01001863 snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 chip->fm_port, chip->fm_port + 4 - 1);
1865 }
1866 if (opl3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867#ifdef CS4231
Takashi Iwai99a0b762005-11-17 17:13:59 +01001868 const int t1dev = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869#else
Takashi Iwai99a0b762005-11-17 17:13:59 +01001870 const int t1dev = 0;
1871#endif
1872 if ((error = snd_opl3_timer_new(opl3, t1dev, t1dev+1)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001874 if ((error = snd_opl3_hwdep_new(opl3, 0, 1, &synth)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
1877 }
1878
Takashi Iwai99a0b762005-11-17 17:13:59 +01001879 return snd_card_register(card);
1880}
1881
1882static struct snd_card *snd_opti9xx_card_new(void)
1883{
1884 struct snd_card *card;
1885
1886 card = snd_card_new(index, id, THIS_MODULE, sizeof(struct snd_opti9xx));
1887 if (! card)
1888 return NULL;
1889 card->private_free = snd_card_opti9xx_free;
1890 return card;
1891}
1892
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001893static int __devinit snd_opti9xx_isa_match(struct device *devptr,
1894 unsigned int dev)
1895{
Takashi Iwai101f6f42007-06-20 12:03:09 +02001896#ifdef CONFIG_PNP
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001897 if (snd_opti9xx_pnp_is_probed)
1898 return 0;
1899 if (isapnp)
1900 return 0;
Takashi Iwai101f6f42007-06-20 12:03:09 +02001901#endif
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001902 return 1;
1903}
1904
1905static int __devinit snd_opti9xx_isa_probe(struct device *devptr,
1906 unsigned int dev)
Takashi Iwai99a0b762005-11-17 17:13:59 +01001907{
1908 struct snd_card *card;
1909 int error;
1910 static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
1911#ifdef OPTi93X
1912 static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
1913#else
1914 static int possible_irqs[] = {9, 10, 11, 7, -1};
1915#endif /* OPTi93X */
1916 static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
1917 static int possible_dma1s[] = {3, 1, 0, -1};
1918#if defined(CS4231) || defined(OPTi93X)
1919 static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
1920#endif /* CS4231 || OPTi93X */
1921
Takashi Iwai99a0b762005-11-17 17:13:59 +01001922 if (mpu_port == SNDRV_AUTO_PORT) {
1923 if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
1924 snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
1925 return -EBUSY;
1926 }
1927 }
1928 if (irq == SNDRV_AUTO_IRQ) {
1929 if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
1930 snd_printk(KERN_ERR "unable to find a free IRQ\n");
1931 return -EBUSY;
1932 }
1933 }
1934 if (mpu_irq == SNDRV_AUTO_IRQ) {
1935 if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
1936 snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
1937 return -EBUSY;
1938 }
1939 }
1940 if (dma1 == SNDRV_AUTO_DMA) {
1941 if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
1942 snd_printk(KERN_ERR "unable to find a free DMA1\n");
1943 return -EBUSY;
1944 }
1945 }
1946#if defined(CS4231) || defined(OPTi93X)
1947 if (dma2 == SNDRV_AUTO_DMA) {
1948 if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
1949 snd_printk("unable to find a free DMA2\n");
1950 return -EBUSY;
1951 }
1952 }
1953#endif
1954
1955 card = snd_opti9xx_card_new();
1956 if (! card)
1957 return -ENOMEM;
1958
1959 if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 snd_card_free(card);
1961 return error;
1962 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001963 snd_card_set_dev(card, devptr);
Takashi Iwai99a0b762005-11-17 17:13:59 +01001964 if ((error = snd_opti9xx_probe(card)) < 0) {
1965 snd_card_free(card);
1966 return error;
1967 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001968 dev_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 return 0;
1970}
1971
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001972static int __devexit snd_opti9xx_isa_remove(struct device *devptr,
1973 unsigned int dev)
Takashi Iwai99a0b762005-11-17 17:13:59 +01001974{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001975 snd_card_free(dev_get_drvdata(devptr));
1976 dev_set_drvdata(devptr, NULL);
Takashi Iwai99a0b762005-11-17 17:13:59 +01001977 return 0;
1978}
1979
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001980static struct isa_driver snd_opti9xx_driver = {
1981 .match = snd_opti9xx_isa_match,
1982 .probe = snd_opti9xx_isa_probe,
1983 .remove = __devexit_p(snd_opti9xx_isa_remove),
Takashi Iwai99a0b762005-11-17 17:13:59 +01001984 /* FIXME: suspend/resume */
1985 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +01001986 .name = DEV_NAME
Takashi Iwai99a0b762005-11-17 17:13:59 +01001987 },
1988};
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990#ifdef CONFIG_PNP
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001991static int __devinit snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
1992 const struct pnp_card_device_id *pid)
Takashi Iwai99a0b762005-11-17 17:13:59 +01001993{
1994 struct snd_card *card;
1995 int error, hw;
1996 struct snd_opti9xx *chip;
1997
1998 if (snd_opti9xx_pnp_is_probed)
1999 return -EBUSY;
2000 if (! isapnp)
2001 return -ENODEV;
2002 card = snd_opti9xx_card_new();
2003 if (! card)
2004 return -ENOMEM;
2005 chip = card->private_data;
2006
2007 hw = snd_card_opti9xx_pnp(chip, pcard, pid);
2008 switch (hw) {
2009 case 0x0924:
2010 hw = OPTi9XX_HW_82C924;
2011 break;
2012 case 0x0925:
2013 hw = OPTi9XX_HW_82C925;
2014 break;
2015 case 0x0931:
2016 hw = OPTi9XX_HW_82C931;
2017 break;
2018 default:
2019 snd_card_free(card);
2020 return -ENODEV;
2021 }
2022
2023 if ((error = snd_opti9xx_init(chip, hw))) {
2024 snd_card_free(card);
2025 return error;
2026 }
2027 if (hw <= OPTi9XX_HW_82C930)
2028 chip->mc_base -= 0x80;
2029 snd_card_set_dev(card, &pcard->card->dev);
2030 if ((error = snd_opti9xx_probe(card)) < 0) {
2031 snd_card_free(card);
2032 return error;
2033 }
2034 pnp_set_card_drvdata(pcard, card);
2035 snd_opti9xx_pnp_is_probed = 1;
2036 return 0;
2037}
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039static void __devexit snd_opti9xx_pnp_remove(struct pnp_card_link * pcard)
2040{
Takashi Iwai99a0b762005-11-17 17:13:59 +01002041 snd_card_free(pnp_get_card_drvdata(pcard));
2042 pnp_set_card_drvdata(pcard, NULL);
2043 snd_opti9xx_pnp_is_probed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
2046static struct pnp_card_driver opti9xx_pnpc_driver = {
2047 .flags = PNP_DRIVER_RES_DISABLE,
2048 .name = "opti9xx",
2049 .id_table = snd_opti9xx_pnpids,
Takashi Iwai99a0b762005-11-17 17:13:59 +01002050 .probe = snd_opti9xx_pnp_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 .remove = __devexit_p(snd_opti9xx_pnp_remove),
2052};
2053#endif
2054
Takashi Iwai99a0b762005-11-17 17:13:59 +01002055#ifdef OPTi93X
2056#define CHIP_NAME "82C93x"
2057#else
2058#define CHIP_NAME "82C92x"
2059#endif
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061static int __init alsa_card_opti9xx_init(void)
2062{
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01002063#ifdef CONFIG_PNP
Takashi Iwai99a0b762005-11-17 17:13:59 +01002064 pnp_register_card_driver(&opti9xx_pnpc_driver);
2065 if (snd_opti9xx_pnp_is_probed)
2066 return 0;
Takashi Iwai101f6f42007-06-20 12:03:09 +02002067 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01002068#endif
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002069 return isa_register_driver(&snd_opti9xx_driver, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
2072static void __exit alsa_card_opti9xx_exit(void)
2073{
Clemens Ladischf7a92752005-12-07 09:13:42 +01002074 if (!snd_opti9xx_pnp_is_probed) {
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01002075 isa_unregister_driver(&snd_opti9xx_driver);
2076 return;
Clemens Ladischf7a92752005-12-07 09:13:42 +01002077 }
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01002078#ifdef CONFIG_PNP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01002080#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
2083module_init(alsa_card_opti9xx_init)
2084module_exit(alsa_card_opti9xx_exit)