blob: 9d528ae00bff1c7bcd63c4da1d512f2e761bac8d [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>
29#include <linux/platform_device.h>
30#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;
Clemens Ladischf7a92752005-12-07 09:13:42 +0100262static struct platform_device *snd_opti9xx_platform_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264#ifdef CONFIG_PNP
265
266static struct pnp_card_device_id snd_opti9xx_pnpids[] = {
267#ifndef OPTi93X
268 /* OPTi 82C924 */
269 { .id = "OPT0924", .devs = { { "OPT0000" }, { "OPT0002" } }, .driver_data = 0x0924 },
270 /* OPTi 82C925 */
271 { .id = "OPT0925", .devs = { { "OPT9250" }, { "OPT0002" } }, .driver_data = 0x0925 },
272#else
273 /* OPTi 82C931/3 */
274 { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } }, .driver_data = 0x0931 },
275#endif /* OPTi93X */
276 { .id = "" }
277};
278
279MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids);
280
281#endif /* CONFIG_PNP */
282
283#ifdef OPTi93X
284#define DRIVER_NAME "snd-card-opti93x"
285#else
286#define DRIVER_NAME "snd-card-opti92x"
287#endif /* OPTi93X */
288
289static char * snd_opti9xx_names[] = {
290 "unkown",
291 "82C928", "82C929",
292 "82C924", "82C925",
293 "82C930", "82C931", "82C933"
294};
295
296
Takashi Iwai99a0b762005-11-17 17:13:59 +0100297static long __init snd_legacy_find_free_ioport(long *port_table, long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 while (*port_table != -1) {
Takashi Iwaib1d57762005-10-10 11:56:31 +0200300 if (request_region(*port_table, size, "ALSA test")) {
301 release_region(*port_table, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return *port_table;
303 }
304 port_table++;
305 }
306 return -1;
307}
308
Takashi Iwai99a0b762005-11-17 17:13:59 +0100309static int __init snd_opti9xx_init(struct snd_opti9xx *chip, 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 Iwai99a0b762005-11-17 17:13:59 +0100454static int __init 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);
504 break;
505#endif /* OPTi93X */
506
507 default:
508 snd_printk("chip %d not supported\n", chip->hardware);
509 return -EINVAL;
510 }
511
512 switch (chip->wss_base) {
513 case 0x530:
514 wss_base_bits = 0x00;
515 break;
516 case 0x604:
517 wss_base_bits = 0x03;
518 break;
519 case 0xe80:
520 wss_base_bits = 0x01;
521 break;
522 case 0xf40:
523 wss_base_bits = 0x02;
524 break;
525 default:
526 snd_printk("WSS port 0x%lx not valid\n", chip->wss_base);
527 goto __skip_base;
528 }
529 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
530
531__skip_base:
532 switch (chip->irq) {
533//#ifdef OPTi93X
534 case 5:
535 irq_bits = 0x05;
536 break;
537//#endif /* OPTi93X */
538 case 7:
539 irq_bits = 0x01;
540 break;
541 case 9:
542 irq_bits = 0x02;
543 break;
544 case 10:
545 irq_bits = 0x03;
546 break;
547 case 11:
548 irq_bits = 0x04;
549 break;
550 default:
551 snd_printk("WSS irq # %d not valid\n", chip->irq);
552 goto __skip_resources;
553 }
554
555 switch (chip->dma1) {
556 case 0:
557 dma_bits = 0x01;
558 break;
559 case 1:
560 dma_bits = 0x02;
561 break;
562 case 3:
563 dma_bits = 0x03;
564 break;
565 default:
566 snd_printk("WSS dma1 # %d not valid\n", chip->dma1);
567 goto __skip_resources;
568 }
569
570#if defined(CS4231) || defined(OPTi93X)
571 if (chip->dma1 == chip->dma2) {
572 snd_printk("don't want to share dmas\n");
573 return -EBUSY;
574 }
575
576 switch (chip->dma2) {
577 case 0:
578 case 1:
579 break;
580 default:
581 snd_printk("WSS dma2 # %d not valid\n", chip->dma2);
582 goto __skip_resources;
583 }
584 dma_bits |= 0x04;
585#endif /* CS4231 || OPTi93X */
586
587#ifndef OPTi93X
588 outb(irq_bits << 3 | dma_bits, chip->wss_base);
589#else /* OPTi93X */
590 snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
591#endif /* OPTi93X */
592
593__skip_resources:
594 if (chip->hardware > OPTi9XX_HW_82C928) {
595 switch (chip->mpu_port) {
596 case 0:
597 case -1:
598 break;
599 case 0x300:
600 mpu_port_bits = 0x03;
601 break;
602 case 0x310:
603 mpu_port_bits = 0x02;
604 break;
605 case 0x320:
606 mpu_port_bits = 0x01;
607 break;
608 case 0x330:
609 mpu_port_bits = 0x00;
610 break;
611 default:
612 snd_printk("MPU-401 port 0x%lx not valid\n",
613 chip->mpu_port);
614 goto __skip_mpu;
615 }
616
617 switch (chip->mpu_irq) {
618 case 5:
619 mpu_irq_bits = 0x02;
620 break;
621 case 7:
622 mpu_irq_bits = 0x03;
623 break;
624 case 9:
625 mpu_irq_bits = 0x00;
626 break;
627 case 10:
628 mpu_irq_bits = 0x01;
629 break;
630 default:
631 snd_printk("MPU-401 irq # %d not valid\n",
632 chip->mpu_irq);
633 goto __skip_mpu;
634 }
635
636 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
637 (chip->mpu_port <= 0) ? 0x00 :
638 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
639 0xf8);
640 }
641__skip_mpu:
642
643 return 0;
644}
645
646#ifdef OPTi93X
647
648static unsigned char snd_opti93x_default_image[32] =
649{
650 0x00, /* 00/00 - l_mixout_outctrl */
651 0x00, /* 01/01 - r_mixout_outctrl */
652 0x88, /* 02/02 - l_cd_inctrl */
653 0x88, /* 03/03 - r_cd_inctrl */
654 0x88, /* 04/04 - l_a1/fm_inctrl */
655 0x88, /* 05/05 - r_a1/fm_inctrl */
656 0x80, /* 06/06 - l_dac_inctrl */
657 0x80, /* 07/07 - r_dac_inctrl */
658 0x00, /* 08/08 - ply_dataform_reg */
659 0x00, /* 09/09 - if_conf */
660 0x00, /* 0a/10 - pin_ctrl */
661 0x00, /* 0b/11 - err_init_reg */
662 0x0a, /* 0c/12 - id_reg */
663 0x00, /* 0d/13 - reserved */
664 0x00, /* 0e/14 - ply_upcount_reg */
665 0x00, /* 0f/15 - ply_lowcount_reg */
666 0x88, /* 10/16 - reserved/l_a1_inctrl */
667 0x88, /* 11/17 - reserved/r_a1_inctrl */
668 0x88, /* 12/18 - l_line_inctrl */
669 0x88, /* 13/19 - r_line_inctrl */
670 0x88, /* 14/20 - l_mic_inctrl */
671 0x88, /* 15/21 - r_mic_inctrl */
672 0x80, /* 16/22 - l_out_outctrl */
673 0x80, /* 17/23 - r_out_outctrl */
674 0x00, /* 18/24 - reserved */
675 0x00, /* 19/25 - reserved */
676 0x00, /* 1a/26 - reserved */
677 0x00, /* 1b/27 - reserved */
678 0x00, /* 1c/28 - cap_dataform_reg */
679 0x00, /* 1d/29 - reserved */
680 0x00, /* 1e/30 - cap_upcount_reg */
681 0x00 /* 1f/31 - cap_lowcount_reg */
682};
683
684
Takashi Iwai346c7a62005-11-17 14:37:56 +0100685static int snd_opti93x_busy_wait(struct snd_opti93x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 int timeout;
688
689 for (timeout = 250; timeout-- > 0; udelay(10))
690 if (!(inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_INIT))
691 return 0;
692
693 snd_printk("chip still busy.\n");
694 return -EBUSY;
695}
696
Takashi Iwai346c7a62005-11-17 14:37:56 +0100697static unsigned char snd_opti93x_in(struct snd_opti93x *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 snd_opti93x_busy_wait(chip);
700 outb(chip->mce_bit | (reg & 0x1f), OPTi93X_PORT(chip, INDEX));
701 return inb(OPTi93X_PORT(chip, DATA));
702}
703
Takashi Iwai346c7a62005-11-17 14:37:56 +0100704static void snd_opti93x_out(struct snd_opti93x *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 unsigned char value)
706{
707 snd_opti93x_busy_wait(chip);
708 outb(chip->mce_bit | (reg & 0x1f), OPTi93X_PORT(chip, INDEX));
709 outb(value, OPTi93X_PORT(chip, DATA));
710}
711
Takashi Iwai346c7a62005-11-17 14:37:56 +0100712static void snd_opti93x_out_image(struct snd_opti93x *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 unsigned char value)
714{
715 snd_opti93x_out(chip, reg, chip->image[reg] = value);
716}
717
Takashi Iwai346c7a62005-11-17 14:37:56 +0100718static void snd_opti93x_out_mask(struct snd_opti93x *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 unsigned char mask, unsigned char value)
720{
721 snd_opti93x_out_image(chip, reg,
722 (chip->image[reg] & ~mask) | (value & mask));
723}
724
725
Takashi Iwai346c7a62005-11-17 14:37:56 +0100726static void snd_opti93x_mce_up(struct snd_opti93x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 snd_opti93x_busy_wait(chip);
729
730 chip->mce_bit = OPTi93X_MCE;
731 if (!(inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_MCE))
732 outb(chip->mce_bit, OPTi93X_PORT(chip, INDEX));
733}
734
Takashi Iwai346c7a62005-11-17 14:37:56 +0100735static void snd_opti93x_mce_down(struct snd_opti93x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 snd_opti93x_busy_wait(chip);
738
739 chip->mce_bit = 0;
740 if (inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_MCE)
741 outb(chip->mce_bit, OPTi93X_PORT(chip, INDEX));
742}
743
744#define snd_opti93x_mute_reg(chip, reg, mute) \
745 snd_opti93x_out(chip, reg, mute ? 0x80 : chip->image[reg]);
746
Takashi Iwai346c7a62005-11-17 14:37:56 +0100747static void snd_opti93x_mute(struct snd_opti93x *chip, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 mute = mute ? 1 : 0;
750 if (chip->mute == mute)
751 return;
752
753 chip->mute = mute;
754
755 snd_opti93x_mute_reg(chip, OPTi93X_CD_LEFT_INPUT, mute);
756 snd_opti93x_mute_reg(chip, OPTi93X_CD_RIGHT_INPUT, mute);
757 switch (chip->hardware) {
758 case OPTi9XX_HW_82C930:
759 snd_opti93x_mute_reg(chip, OPTi930_AUX_LEFT_INPUT, mute);
760 snd_opti93x_mute_reg(chip, OPTi930_AUX_RIGHT_INPUT, mute);
761 break;
762 case OPTi9XX_HW_82C931:
763 case OPTi9XX_HW_82C933:
764 snd_opti93x_mute_reg(chip, OPTi931_FM_LEFT_INPUT, mute);
765 snd_opti93x_mute_reg(chip, OPTi931_FM_RIGHT_INPUT, mute);
766 snd_opti93x_mute_reg(chip, OPTi931_AUX_LEFT_INPUT, mute);
767 snd_opti93x_mute_reg(chip, OPTi931_AUX_RIGHT_INPUT, mute);
768 }
769 snd_opti93x_mute_reg(chip, OPTi93X_DAC_LEFT, mute);
770 snd_opti93x_mute_reg(chip, OPTi93X_DAC_RIGHT, mute);
771 snd_opti93x_mute_reg(chip, OPTi93X_LINE_LEFT_INPUT, mute);
772 snd_opti93x_mute_reg(chip, OPTi93X_LINE_RIGHT_INPUT, mute);
773 snd_opti93x_mute_reg(chip, OPTi93X_MIC_LEFT_INPUT, mute);
774 snd_opti93x_mute_reg(chip, OPTi93X_MIC_RIGHT_INPUT, mute);
775 snd_opti93x_mute_reg(chip, OPTi93X_OUT_LEFT, mute);
776 snd_opti93x_mute_reg(chip, OPTi93X_OUT_RIGHT, mute);
777}
778
779
780static unsigned int snd_opti93x_get_count(unsigned char format,
781 unsigned int size)
782{
783 switch (format & 0xe0) {
784 case OPTi93X_LINEAR_16_LIT:
785 case OPTi93X_LINEAR_16_BIG:
786 size >>= 1;
787 break;
788 case OPTi93X_ADPCM_16:
789 return size >> 2;
790 }
791 return (format & OPTi93X_STEREO) ? (size >> 1) : size;
792}
793
794static unsigned int rates[] = { 5512, 6615, 8000, 9600, 11025, 16000,
795 18900, 22050, 27428, 32000, 33075, 37800,
796 44100, 48000 };
797#define RATES ARRAY_SIZE(rates)
798
Takashi Iwai346c7a62005-11-17 14:37:56 +0100799static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 .count = RATES,
801 .list = rates,
802 .mask = 0,
803};
804
805static unsigned char bits[] = { 0x01, 0x0f, 0x00, 0x0e, 0x03, 0x02,
806 0x05, 0x07, 0x04, 0x06, 0x0d, 0x09,
807 0x0b, 0x0c};
808
809static unsigned char snd_opti93x_get_freq(unsigned int rate)
810{
811 unsigned int i;
812
813 for (i = 0; i < RATES; i++) {
814 if (rate == rates[i])
815 return bits[i];
816 }
817 snd_BUG();
818 return bits[RATES-1];
819}
820
Takashi Iwai346c7a62005-11-17 14:37:56 +0100821static unsigned char snd_opti93x_get_format(struct snd_opti93x *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 unsigned int format, int channels)
823{
824 unsigned char retval = OPTi93X_LINEAR_8;
825
826 switch (format) {
827 case SNDRV_PCM_FORMAT_MU_LAW:
828 retval = OPTi93X_ULAW_8;
829 break;
830 case SNDRV_PCM_FORMAT_A_LAW:
831 retval = OPTi93X_ALAW_8;
832 break;
833 case SNDRV_PCM_FORMAT_S16_LE:
834 retval = OPTi93X_LINEAR_16_LIT;
835 break;
836 case SNDRV_PCM_FORMAT_S16_BE:
837 retval = OPTi93X_LINEAR_16_BIG;
838 break;
839 case SNDRV_PCM_FORMAT_IMA_ADPCM:
840 retval = OPTi93X_ADPCM_16;
841 }
842 return (channels > 1) ? (retval | OPTi93X_STEREO) : retval;
843}
844
845
Takashi Iwai346c7a62005-11-17 14:37:56 +0100846static void snd_opti93x_playback_format(struct snd_opti93x *chip, unsigned char fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 unsigned char mask;
849
850 snd_opti93x_mute(chip, 1);
851
852 snd_opti93x_mce_up(chip);
853 mask = (chip->mode & OPTi93X_MODE_CAPTURE) ? 0xf0 : 0xff;
854 snd_opti93x_out_mask(chip, OPTi93X_PLAY_FORMAT, mask, fmt);
855 snd_opti93x_mce_down(chip);
856
857 snd_opti93x_mute(chip, 0);
858}
859
Takashi Iwai346c7a62005-11-17 14:37:56 +0100860static void snd_opti93x_capture_format(struct snd_opti93x *chip, unsigned char fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 snd_opti93x_mute(chip, 1);
863
864 snd_opti93x_mce_up(chip);
865 if (!(chip->mode & OPTi93X_MODE_PLAY))
866 snd_opti93x_out_mask(chip, OPTi93X_PLAY_FORMAT, 0x0f, fmt);
867 else
868 fmt = chip->image[OPTi93X_PLAY_FORMAT] & 0xf0;
869 snd_opti93x_out_image(chip, OPTi93X_CAPT_FORMAT, fmt);
870 snd_opti93x_mce_down(chip);
871
872 snd_opti93x_mute(chip, 0);
873}
874
875
Takashi Iwai346c7a62005-11-17 14:37:56 +0100876static int snd_opti93x_open(struct snd_opti93x *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 unsigned long flags;
879
880 spin_lock_irqsave(&chip->lock, flags);
881
882 if (chip->mode & mode) {
883 spin_unlock_irqrestore(&chip->lock, flags);
884 return -EAGAIN;
885 }
886
887 if (!(chip->mode & OPTi93X_MODE_OPEN)) {
888 outb(0x00, OPTi93X_PORT(chip, STATUS));
889 snd_opti93x_out_mask(chip, OPTi93X_PIN_CTRL,
890 OPTi93X_IRQ_ENABLE, OPTi93X_IRQ_ENABLE);
891 chip->mode = mode;
892 }
893 else
894 chip->mode |= mode;
895
896 spin_unlock_irqrestore(&chip->lock, flags);
897 return 0;
898}
899
Takashi Iwai346c7a62005-11-17 14:37:56 +0100900static void snd_opti93x_close(struct snd_opti93x *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 unsigned long flags;
903
904 spin_lock_irqsave(&chip->lock, flags);
905
906 chip->mode &= ~mode;
907 if (chip->mode & OPTi93X_MODE_OPEN) {
908 spin_unlock_irqrestore(&chip->lock, flags);
909 return;
910 }
911
912 snd_opti93x_mute(chip, 1);
913
914 outb(0, OPTi93X_PORT(chip, STATUS));
915 snd_opti93x_out_mask(chip, OPTi93X_PIN_CTRL, OPTi93X_IRQ_ENABLE,
916 ~OPTi93X_IRQ_ENABLE);
917
918 snd_opti93x_mce_up(chip);
919 snd_opti93x_out_image(chip, OPTi93X_IFACE_CONF, 0x00);
920 snd_opti93x_mce_down(chip);
921 chip->mode = 0;
922
923 snd_opti93x_mute(chip, 0);
924 spin_unlock_irqrestore(&chip->lock, flags);
925}
926
Takashi Iwai346c7a62005-11-17 14:37:56 +0100927static int snd_opti93x_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 unsigned char what, int cmd)
929{
Takashi Iwai346c7a62005-11-17 14:37:56 +0100930 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 switch (cmd) {
933 case SNDRV_PCM_TRIGGER_START:
934 case SNDRV_PCM_TRIGGER_STOP:
935 {
936 unsigned int what = 0;
937 struct list_head *pos;
Takashi Iwai346c7a62005-11-17 14:37:56 +0100938 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 snd_pcm_group_for_each(pos, substream) {
940 s = snd_pcm_group_substream_entry(pos);
941 if (s == chip->playback_substream) {
942 what |= OPTi93X_PLAYBACK_ENABLE;
943 snd_pcm_trigger_done(s, substream);
944 } else if (s == chip->capture_substream) {
945 what |= OPTi93X_CAPTURE_ENABLE;
946 snd_pcm_trigger_done(s, substream);
947 }
948 }
949 spin_lock(&chip->lock);
950 if (cmd == SNDRV_PCM_TRIGGER_START) {
951 snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF, what, what);
952 if (what & OPTi93X_CAPTURE_ENABLE)
953 udelay(50);
954 } else
955 snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF, what, 0x00);
956 spin_unlock(&chip->lock);
957 break;
958 }
959 default:
960 return -EINVAL;
961 }
962 return 0;
963}
964
Takashi Iwai346c7a62005-11-17 14:37:56 +0100965static int snd_opti93x_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 return snd_opti93x_trigger(substream,
968 OPTi93X_PLAYBACK_ENABLE, cmd);
969}
970
Takashi Iwai346c7a62005-11-17 14:37:56 +0100971static int snd_opti93x_capture_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 return snd_opti93x_trigger(substream,
974 OPTi93X_CAPTURE_ENABLE, cmd);
975}
976
Takashi Iwai346c7a62005-11-17 14:37:56 +0100977static int snd_opti93x_hw_params(struct snd_pcm_substream *substream,
978 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
981}
982
983
Takashi Iwai346c7a62005-11-17 14:37:56 +0100984static int snd_opti93x_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 snd_pcm_lib_free_pages(substream);
987 return 0;
988}
989
990
Takashi Iwai346c7a62005-11-17 14:37:56 +0100991static int snd_opti93x_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Takashi Iwai346c7a62005-11-17 14:37:56 +0100993 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
994 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 unsigned long flags;
996 unsigned char format;
997 unsigned int count = snd_pcm_lib_period_bytes(substream);
998 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
999
1000 spin_lock_irqsave(&chip->lock, flags);
1001
1002 chip->p_dma_size = size;
1003 snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF,
1004 OPTi93X_PLAYBACK_ENABLE | OPTi93X_PLAYBACK_PIO,
1005 ~(OPTi93X_PLAYBACK_ENABLE | OPTi93X_PLAYBACK_PIO));
1006
1007 snd_dma_program(chip->dma1, runtime->dma_addr, size,
1008 DMA_MODE_WRITE | DMA_AUTOINIT);
1009
1010 format = snd_opti93x_get_freq(runtime->rate);
1011 format |= snd_opti93x_get_format(chip, runtime->format,
1012 runtime->channels);
1013 snd_opti93x_playback_format(chip, format);
1014 format = chip->image[OPTi93X_PLAY_FORMAT];
1015
1016 count = snd_opti93x_get_count(format, count) - 1;
1017 snd_opti93x_out_image(chip, OPTi93X_PLAY_LWR_CNT, count);
1018 snd_opti93x_out_image(chip, OPTi93X_PLAY_UPR_CNT, count >> 8);
1019
1020 spin_unlock_irqrestore(&chip->lock, flags);
1021 return 0;
1022}
1023
Takashi Iwai346c7a62005-11-17 14:37:56 +01001024static int snd_opti93x_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001026 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
1027 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 unsigned long flags;
1029 unsigned char format;
1030 unsigned int count = snd_pcm_lib_period_bytes(substream);
1031 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1032
1033 spin_lock_irqsave(&chip->lock, flags);
1034
1035 chip->c_dma_size = size;
1036 snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF,
Clemens Ladischdb673192005-09-05 10:36:27 +02001037 OPTi93X_CAPTURE_ENABLE | OPTi93X_CAPTURE_PIO, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 snd_dma_program(chip->dma2, runtime->dma_addr, size,
1040 DMA_MODE_READ | DMA_AUTOINIT);
1041
1042 format = snd_opti93x_get_freq(runtime->rate);
1043 format |= snd_opti93x_get_format(chip, runtime->format,
1044 runtime->channels);
1045 snd_opti93x_capture_format(chip, format);
1046 format = chip->image[OPTi93X_CAPT_FORMAT];
1047
1048 count = snd_opti93x_get_count(format, count) - 1;
1049 snd_opti93x_out_image(chip, OPTi93X_CAPT_LWR_CNT, count);
1050 snd_opti93x_out_image(chip, OPTi93X_CAPT_UPR_CNT, count >> 8);
1051
1052 spin_unlock_irqrestore(&chip->lock, flags);
1053 return 0;
1054}
1055
Takashi Iwai346c7a62005-11-17 14:37:56 +01001056static snd_pcm_uframes_t snd_opti93x_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001058 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 size_t ptr;
1060
1061 if (!(chip->image[OPTi93X_IFACE_CONF] & OPTi93X_PLAYBACK_ENABLE))
1062 return 0;
1063
1064 ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
1065 return bytes_to_frames(substream->runtime, ptr);
1066}
1067
Takashi Iwai346c7a62005-11-17 14:37:56 +01001068static snd_pcm_uframes_t snd_opti93x_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001070 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 size_t ptr;
1072
1073 if (!(chip->image[OPTi93X_IFACE_CONF] & OPTi93X_CAPTURE_ENABLE))
1074 return 0;
1075
1076 ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
1077 return bytes_to_frames(substream->runtime, ptr);
1078}
1079
1080
Takashi Iwai346c7a62005-11-17 14:37:56 +01001081static void snd_opti93x_overrange(struct snd_opti93x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 unsigned long flags;
1084
1085 spin_lock_irqsave(&chip->lock, flags);
1086
1087 if (snd_opti93x_in(chip, OPTi93X_ERR_INIT) & (0x08 | 0x02))
1088 chip->capture_substream->runtime->overrange++;
1089
1090 spin_unlock_irqrestore(&chip->lock, flags);
1091}
1092
1093static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1094{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001095 struct snd_opti93x *codec = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 unsigned char status;
1097
1098 status = snd_opti9xx_read(codec->chip, OPTi9XX_MC_REG(11));
1099 if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
1100 snd_pcm_period_elapsed(codec->playback_substream);
1101 if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
1102 snd_opti93x_overrange(codec);
1103 snd_pcm_period_elapsed(codec->capture_substream);
1104 }
1105 outb(0x00, OPTi93X_PORT(codec, STATUS));
1106 return IRQ_HANDLED;
1107}
1108
1109
Takashi Iwai346c7a62005-11-17 14:37:56 +01001110static struct snd_pcm_hardware snd_opti93x_playback = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1112 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
1113 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1114 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1115 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1116 .rate_min = 5512,
1117 .rate_max = 48000,
1118 .channels_min = 1,
1119 .channels_max = 2,
1120 .buffer_bytes_max = (128*1024),
1121 .period_bytes_min = 64,
1122 .period_bytes_max = (128*1024),
1123 .periods_min = 1,
1124 .periods_max = 1024,
1125 .fifo_size = 0,
1126};
1127
Takashi Iwai346c7a62005-11-17 14:37:56 +01001128static struct snd_pcm_hardware snd_opti93x_capture = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1130 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
1131 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1132 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1133 .rates = SNDRV_PCM_RATE_8000_48000,
1134 .rate_min = 5512,
1135 .rate_max = 48000,
1136 .channels_min = 1,
1137 .channels_max = 2,
1138 .buffer_bytes_max = (128*1024),
1139 .period_bytes_min = 64,
1140 .period_bytes_max = (128*1024),
1141 .periods_min = 1,
1142 .periods_max = 1024,
1143 .fifo_size = 0,
1144};
1145
Takashi Iwai346c7a62005-11-17 14:37:56 +01001146static int snd_opti93x_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 int error;
Takashi Iwai346c7a62005-11-17 14:37:56 +01001149 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
1150 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 if ((error = snd_opti93x_open(chip, OPTi93X_MODE_PLAY)) < 0)
1153 return error;
1154 snd_pcm_set_sync(substream);
1155 chip->playback_substream = substream;
1156 runtime->hw = snd_opti93x_playback;
1157 snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
1158 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1159 return error;
1160}
1161
Takashi Iwai346c7a62005-11-17 14:37:56 +01001162static int snd_opti93x_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 int error;
Takashi Iwai346c7a62005-11-17 14:37:56 +01001165 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
1166 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 if ((error = snd_opti93x_open(chip, OPTi93X_MODE_CAPTURE)) < 0)
1169 return error;
1170 runtime->hw = snd_opti93x_capture;
1171 snd_pcm_set_sync(substream);
1172 chip->capture_substream = substream;
1173 snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
1174 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1175 return error;
1176}
1177
Takashi Iwai346c7a62005-11-17 14:37:56 +01001178static int snd_opti93x_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001180 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 chip->playback_substream = NULL;
1183 snd_opti93x_close(chip, OPTi93X_MODE_PLAY);
1184 return 0;
1185}
1186
Takashi Iwai346c7a62005-11-17 14:37:56 +01001187static int snd_opti93x_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001189 struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 chip->capture_substream = NULL;
1192 snd_opti93x_close(chip, OPTi93X_MODE_CAPTURE);
1193 return 0;
1194}
1195
1196
Takashi Iwai346c7a62005-11-17 14:37:56 +01001197static void snd_opti93x_init(struct snd_opti93x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 unsigned long flags;
1200 int i;
1201
1202 spin_lock_irqsave(&chip->lock, flags);
1203 snd_opti93x_mce_up(chip);
1204
1205 for (i = 0; i < 32; i++)
1206 snd_opti93x_out_image(chip, i, snd_opti93x_default_image[i]);
1207
1208 snd_opti93x_mce_down(chip);
1209 spin_unlock_irqrestore(&chip->lock, flags);
1210}
1211
Takashi Iwai346c7a62005-11-17 14:37:56 +01001212static int snd_opti93x_probe(struct snd_opti93x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 unsigned long flags;
1215 unsigned char val;
1216
1217 spin_lock_irqsave(&chip->lock, flags);
1218 val = snd_opti93x_in(chip, OPTi93X_ID) & 0x0f;
1219 spin_unlock_irqrestore(&chip->lock, flags);
1220
1221 return (val == 0x0a) ? 0 : -ENODEV;
1222}
1223
Takashi Iwai346c7a62005-11-17 14:37:56 +01001224static int snd_opti93x_free(struct snd_opti93x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
Takashi Iwaib1d57762005-10-10 11:56:31 +02001226 release_and_free_resource(chip->res_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (chip->dma1 >= 0) {
1228 disable_dma(chip->dma1);
1229 free_dma(chip->dma1);
1230 }
1231 if (chip->dma2 >= 0) {
1232 disable_dma(chip->dma2);
1233 free_dma(chip->dma2);
1234 }
1235 if (chip->irq >= 0) {
1236 free_irq(chip->irq, chip);
1237 }
1238 kfree(chip);
1239 return 0;
1240}
1241
Takashi Iwai346c7a62005-11-17 14:37:56 +01001242static int snd_opti93x_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001244 struct snd_opti93x *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return snd_opti93x_free(chip);
1246}
1247
Takashi Iwai346c7a62005-11-17 14:37:56 +01001248static const char *snd_opti93x_chip_id(struct snd_opti93x *codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
1250 switch (codec->hardware) {
1251 case OPTi9XX_HW_82C930: return "82C930";
1252 case OPTi9XX_HW_82C931: return "82C931";
1253 case OPTi9XX_HW_82C933: return "82C933";
1254 default: return "???";
1255 }
1256}
1257
Takashi Iwai346c7a62005-11-17 14:37:56 +01001258static int snd_opti93x_create(struct snd_card *card, struct snd_opti9xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 int dma1, int dma2,
Takashi Iwai346c7a62005-11-17 14:37:56 +01001260 struct snd_opti93x **rcodec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001262 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 .dev_free = snd_opti93x_dev_free,
1264 };
1265 int error;
Takashi Iwai346c7a62005-11-17 14:37:56 +01001266 struct snd_opti93x *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 *rcodec = NULL;
Takashi Iwai9e76a762005-09-09 14:21:17 +02001269 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (codec == NULL)
1271 return -ENOMEM;
1272 codec->irq = -1;
1273 codec->dma1 = -1;
1274 codec->dma2 = -1;
1275
1276 if ((codec->res_port = request_region(chip->wss_base + 4, 4, "OPTI93x CODEC")) == NULL) {
1277 snd_printk(KERN_ERR "opti9xx: can't grab port 0x%lx\n", chip->wss_base + 4);
1278 snd_opti93x_free(codec);
1279 return -EBUSY;
1280 }
1281 if (request_dma(dma1, "OPTI93x - 1")) {
1282 snd_printk(KERN_ERR "opti9xx: can't grab DMA1 %d\n", dma1);
1283 snd_opti93x_free(codec);
1284 return -EBUSY;
1285 }
1286 codec->dma1 = chip->dma1;
1287 if (request_dma(dma2, "OPTI93x - 2")) {
1288 snd_printk(KERN_ERR "opti9xx: can't grab DMA2 %d\n", dma2);
1289 snd_opti93x_free(codec);
1290 return -EBUSY;
1291 }
1292 codec->dma2 = chip->dma2;
1293
Thomas Gleixner65ca68b2006-07-01 19:29:46 -07001294 if (request_irq(chip->irq, snd_opti93x_interrupt, IRQF_DISABLED, DRIVER_NAME" - WSS", codec)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", chip->irq);
1296 snd_opti93x_free(codec);
1297 return -EBUSY;
1298 }
1299
1300 codec->card = card;
1301 codec->port = chip->wss_base + 4;
1302 codec->irq = chip->irq;
1303
1304 spin_lock_init(&codec->lock);
1305 codec->hardware = chip->hardware;
1306 codec->chip = chip;
1307
1308 if ((error = snd_opti93x_probe(codec))) {
1309 snd_opti93x_free(codec);
1310 return error;
1311 }
1312
1313 snd_opti93x_init(codec);
1314
1315 /* Register device */
1316 if ((error = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
1317 snd_opti93x_free(codec);
1318 return error;
1319 }
1320
1321 *rcodec = codec;
1322 return 0;
1323}
1324
Takashi Iwai346c7a62005-11-17 14:37:56 +01001325static struct snd_pcm_ops snd_opti93x_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 .open = snd_opti93x_playback_open,
1327 .close = snd_opti93x_playback_close,
1328 .ioctl = snd_pcm_lib_ioctl,
1329 .hw_params = snd_opti93x_hw_params,
1330 .hw_free = snd_opti93x_hw_free,
1331 .prepare = snd_opti93x_playback_prepare,
1332 .trigger = snd_opti93x_playback_trigger,
1333 .pointer = snd_opti93x_playback_pointer,
1334};
1335
Takashi Iwai346c7a62005-11-17 14:37:56 +01001336static struct snd_pcm_ops snd_opti93x_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 .open = snd_opti93x_capture_open,
1338 .close = snd_opti93x_capture_close,
1339 .ioctl = snd_pcm_lib_ioctl,
1340 .hw_params = snd_opti93x_hw_params,
1341 .hw_free = snd_opti93x_hw_free,
1342 .prepare = snd_opti93x_capture_prepare,
1343 .trigger = snd_opti93x_capture_trigger,
1344 .pointer = snd_opti93x_capture_pointer,
1345};
1346
Takashi Iwai346c7a62005-11-17 14:37:56 +01001347static int snd_opti93x_pcm(struct snd_opti93x *codec, int device, struct snd_pcm **rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 int error;
Takashi Iwai346c7a62005-11-17 14:37:56 +01001350 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Takashi Iwai91134852006-01-23 15:21:05 +01001352 if ((error = snd_pcm_new(codec->card, "OPTi 82C93X", device, 1, 1, &pcm)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return error;
1354
1355 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_opti93x_playback_ops);
1356 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_opti93x_capture_ops);
1357
1358 pcm->private_data = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
1360
1361 strcpy(pcm->name, snd_opti93x_chip_id(codec));
1362
1363 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1364 snd_dma_isa_data(),
1365 64*1024, codec->dma1 > 3 || codec->dma2 > 3 ? 128*1024 : 64*1024);
1366
1367 codec->pcm = pcm;
1368 if (rpcm)
1369 *rpcm = pcm;
1370 return 0;
1371}
1372
1373/*
1374 * MIXER part
1375 */
1376
Takashi Iwai346c7a62005-11-17 14:37:56 +01001377static int snd_opti93x_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 static char *texts[4] = {
1380 "Line1", "Aux", "Mic", "Mix"
1381 };
1382
1383 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1384 uinfo->count = 2;
1385 uinfo->value.enumerated.items = 4;
1386 if (uinfo->value.enumerated.item > 3)
1387 uinfo->value.enumerated.item = 3;
1388 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1389 return 0;
1390}
1391
Takashi Iwai346c7a62005-11-17 14:37:56 +01001392static int snd_opti93x_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001394 struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 unsigned long flags;
1396
1397 spin_lock_irqsave(&chip->lock, flags);
1398 ucontrol->value.enumerated.item[0] = (chip->image[OPTi93X_MIXOUT_LEFT] & OPTi93X_MIXOUT_MIXER) >> 6;
1399 ucontrol->value.enumerated.item[1] = (chip->image[OPTi93X_MIXOUT_RIGHT] & OPTi93X_MIXOUT_MIXER) >> 6;
1400 spin_unlock_irqrestore(&chip->lock, flags);
1401 return 0;
1402}
1403
Takashi Iwai346c7a62005-11-17 14:37:56 +01001404static int snd_opti93x_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001406 struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 unsigned long flags;
1408 unsigned short left, right;
1409 int change;
1410
1411 if (ucontrol->value.enumerated.item[0] > 3 ||
1412 ucontrol->value.enumerated.item[1] > 3)
1413 return -EINVAL;
1414 left = ucontrol->value.enumerated.item[0] << 6;
1415 right = ucontrol->value.enumerated.item[1] << 6;
1416 spin_lock_irqsave(&chip->lock, flags);
1417 left = (chip->image[OPTi93X_MIXOUT_LEFT] & ~OPTi93X_MIXOUT_MIXER) | left;
1418 right = (chip->image[OPTi93X_MIXOUT_RIGHT] & ~OPTi93X_MIXOUT_MIXER) | right;
1419 change = left != chip->image[OPTi93X_MIXOUT_LEFT] ||
1420 right != chip->image[OPTi93X_MIXOUT_RIGHT];
1421 snd_opti93x_out_image(chip, OPTi93X_MIXOUT_LEFT, left);
1422 snd_opti93x_out_image(chip, OPTi93X_MIXOUT_RIGHT, right);
1423 spin_unlock_irqrestore(&chip->lock, flags);
1424 return change;
1425}
1426
1427#if 0
1428
1429#define OPTi93X_SINGLE(xname, xindex, reg, shift, mask, invert) \
1430{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1431 .info = snd_opti93x_info_single, \
1432 .get = snd_opti93x_get_single, .put = snd_opti93x_put_single, \
1433 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1434
Takashi Iwai346c7a62005-11-17 14:37:56 +01001435static int snd_opti93x_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 int mask = (kcontrol->private_value >> 16) & 0xff;
1438
1439 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1440 uinfo->count = 1;
1441 uinfo->value.integer.min = 0;
1442 uinfo->value.integer.max = mask;
1443 return 0;
1444}
1445
Takashi Iwai346c7a62005-11-17 14:37:56 +01001446static int snd_opti93x_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001448 struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 unsigned long flags;
1450 int reg = kcontrol->private_value & 0xff;
1451 int shift = (kcontrol->private_value >> 8) & 0xff;
1452 int mask = (kcontrol->private_value >> 16) & 0xff;
1453 int invert = (kcontrol->private_value >> 24) & 0xff;
1454
1455 spin_lock_irqsave(&chip->lock, flags);
1456 ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
1457 spin_unlock_irqrestore(&chip->lock, flags);
1458 if (invert)
1459 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1460 return 0;
1461}
1462
Takashi Iwai346c7a62005-11-17 14:37:56 +01001463static int snd_opti93x_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001465 struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 unsigned long flags;
1467 int reg = kcontrol->private_value & 0xff;
1468 int shift = (kcontrol->private_value >> 8) & 0xff;
1469 int mask = (kcontrol->private_value >> 16) & 0xff;
1470 int invert = (kcontrol->private_value >> 24) & 0xff;
1471 int change;
1472 unsigned short val;
1473
1474 val = (ucontrol->value.integer.value[0] & mask);
1475 if (invert)
1476 val = mask - val;
1477 val <<= shift;
1478 spin_lock_irqsave(&chip->lock, flags);
1479 val = (chip->image[reg] & ~(mask << shift)) | val;
1480 change = val != chip->image[reg];
1481 snd_opti93x_out(chip, reg, val);
1482 spin_unlock_irqrestore(&chip->lock, flags);
1483 return change;
1484}
1485
1486#endif /* single */
1487
1488#define OPTi93X_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1489{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1490 .info = snd_opti93x_info_double, \
1491 .get = snd_opti93x_get_double, .put = snd_opti93x_put_double, \
1492 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1493
1494#define OPTi93X_DOUBLE_INVERT_INVERT(xctl) \
1495 do { xctl.private_value ^= 22; } while (0)
1496#define OPTi93X_DOUBLE_CHANGE_REGS(xctl, left_reg, right_reg) \
1497 do { xctl.private_value &= ~0x0000ffff; \
1498 xctl.private_value |= left_reg | (right_reg << 8); } while (0)
1499
Takashi Iwai346c7a62005-11-17 14:37:56 +01001500static int snd_opti93x_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 int mask = (kcontrol->private_value >> 24) & 0xff;
1503
1504 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1505 uinfo->count = 2;
1506 uinfo->value.integer.min = 0;
1507 uinfo->value.integer.max = mask;
1508 return 0;
1509}
1510
Takashi Iwai346c7a62005-11-17 14:37:56 +01001511static int snd_opti93x_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001513 struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 unsigned long flags;
1515 int left_reg = kcontrol->private_value & 0xff;
1516 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1517 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1518 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1519 int mask = (kcontrol->private_value >> 24) & 0xff;
1520 int invert = (kcontrol->private_value >> 22) & 1;
1521
1522 spin_lock_irqsave(&chip->lock, flags);
1523 ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
1524 ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
1525 spin_unlock_irqrestore(&chip->lock, flags);
1526 if (invert) {
1527 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1528 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1529 }
1530 return 0;
1531}
1532
Takashi Iwai346c7a62005-11-17 14:37:56 +01001533static int snd_opti93x_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001535 struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 unsigned long flags;
1537 int left_reg = kcontrol->private_value & 0xff;
1538 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1539 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1540 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1541 int mask = (kcontrol->private_value >> 24) & 0xff;
1542 int invert = (kcontrol->private_value >> 22) & 1;
1543 int change;
1544 unsigned short val1, val2;
1545
1546 val1 = ucontrol->value.integer.value[0] & mask;
1547 val2 = ucontrol->value.integer.value[1] & mask;
1548 if (invert) {
1549 val1 = mask - val1;
1550 val2 = mask - val2;
1551 }
1552 val1 <<= shift_left;
1553 val2 <<= shift_right;
1554 spin_lock_irqsave(&chip->lock, flags);
1555 val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
1556 val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
1557 change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
1558 snd_opti93x_out_image(chip, left_reg, val1);
1559 snd_opti93x_out_image(chip, right_reg, val2);
1560 spin_unlock_irqrestore(&chip->lock, flags);
1561 return change;
1562}
1563
Takashi Iwai346c7a62005-11-17 14:37:56 +01001564static struct snd_kcontrol_new snd_opti93x_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565OPTi93X_DOUBLE("Master Playback Switch", 0, OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
1566OPTi93X_DOUBLE("Master Playback Volume", 0, OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1),
1567OPTi93X_DOUBLE("PCM Playback Switch", 0, OPTi93X_DAC_LEFT, OPTi93X_DAC_RIGHT, 7, 7, 1, 1),
1568OPTi93X_DOUBLE("PCM Playback Volume", 0, OPTi93X_DAC_LEFT, OPTi93X_DAC_RIGHT, 0, 0, 31, 1),
1569OPTi93X_DOUBLE("FM Playback Switch", 0, OPTi931_FM_LEFT_INPUT, OPTi931_FM_RIGHT_INPUT, 7, 7, 1, 1),
1570OPTi93X_DOUBLE("FM Playback Volume", 0, OPTi931_FM_LEFT_INPUT, OPTi931_FM_RIGHT_INPUT, 1, 1, 15, 1),
1571OPTi93X_DOUBLE("Line Playback Switch", 0, OPTi93X_LINE_LEFT_INPUT, OPTi93X_LINE_RIGHT_INPUT, 7, 7, 1, 1),
1572OPTi93X_DOUBLE("Line Playback Volume", 0, OPTi93X_LINE_LEFT_INPUT, OPTi93X_LINE_RIGHT_INPUT, 1, 1, 15, 1),
1573OPTi93X_DOUBLE("Mic Playback Switch", 0, OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
1574OPTi93X_DOUBLE("Mic Playback Volume", 0, OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1),
1575OPTi93X_DOUBLE("Mic Boost", 0, OPTi93X_MIXOUT_LEFT, OPTi93X_MIXOUT_RIGHT, 5, 5, 1, 1),
1576OPTi93X_DOUBLE("CD Playback Switch", 0, OPTi93X_CD_LEFT_INPUT, OPTi93X_CD_RIGHT_INPUT, 7, 7, 1, 1),
1577OPTi93X_DOUBLE("CD Playback Volume", 0, OPTi93X_CD_LEFT_INPUT, OPTi93X_CD_RIGHT_INPUT, 1, 1, 15, 1),
1578OPTi93X_DOUBLE("Aux Playback Switch", 0, OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
1579OPTi93X_DOUBLE("Aux Playback Volume", 0, OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1),
1580OPTi93X_DOUBLE("Capture Volume", 0, OPTi93X_MIXOUT_LEFT, OPTi93X_MIXOUT_RIGHT, 0, 0, 15, 0),
1581{
1582 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1583 .name = "Capture Source",
1584 .info = snd_opti93x_info_mux,
1585 .get = snd_opti93x_get_mux,
1586 .put = snd_opti93x_put_mux,
1587}
1588};
1589
Takashi Iwai346c7a62005-11-17 14:37:56 +01001590static int snd_opti93x_mixer(struct snd_opti93x *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Takashi Iwai346c7a62005-11-17 14:37:56 +01001592 struct snd_card *card;
1593 struct snd_kcontrol_new knew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 int err;
1595 unsigned int idx;
1596
1597 snd_assert(chip != NULL && chip->card != NULL, return -EINVAL);
1598
1599 card = chip->card;
1600
1601 strcpy(card->mixername, snd_opti93x_chip_id(chip));
1602
1603 for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
1604 knew = snd_opti93x_controls[idx];
1605 if (chip->hardware == OPTi9XX_HW_82C930) {
1606 if (strstr(knew.name, "FM")) /* skip FM controls */
1607 continue;
1608 else if (strcmp(knew.name, "Mic Playback Volume"))
1609 OPTi93X_DOUBLE_INVERT_INVERT(knew);
1610 else if (strstr(knew.name, "Aux"))
1611 OPTi93X_DOUBLE_CHANGE_REGS(knew, OPTi930_AUX_LEFT_INPUT, OPTi930_AUX_RIGHT_INPUT);
1612 else if (strcmp(knew.name, "PCM Playback Volume"))
1613 OPTi93X_DOUBLE_INVERT_INVERT(knew);
1614 else if (strcmp(knew.name, "Master Playback Volume"))
1615 OPTi93X_DOUBLE_INVERT_INVERT(knew);
1616 }
1617 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_opti93x_controls[idx], chip))) < 0)
1618 return err;
1619 }
1620 return 0;
1621}
1622
1623#endif /* OPTi93X */
1624
Takashi Iwai99a0b762005-11-17 17:13:59 +01001625static int __init snd_card_opti9xx_detect(struct snd_card *card, struct snd_opti9xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 int i, err;
1628
1629#ifndef OPTi93X
1630 for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
1631 unsigned char value;
1632
1633 if ((err = snd_opti9xx_init(chip, i)) < 0)
1634 return err;
1635
1636 if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
1637 continue;
1638
1639 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
1640 if ((value != 0xff) && (value != inb(chip->mc_base + 1)))
1641 if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
1642 return 1;
1643
Takashi Iwaib1d57762005-10-10 11:56:31 +02001644 release_and_free_resource(chip->res_mc_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 chip->res_mc_base = NULL;
1646
1647 }
1648#else /* OPTi93X */
1649 for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
1650 unsigned long flags;
1651 unsigned char value;
1652
1653 if ((err = snd_opti9xx_init(chip, i)) < 0)
1654 return err;
1655
1656 if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
1657 continue;
1658
1659 spin_lock_irqsave(&chip->lock, flags);
1660 outb(chip->password, chip->mc_base + chip->pwd_reg);
1661 outb(((chip->mc_indir_index & (1 << 8)) >> 4) |
1662 ((chip->mc_indir_index & 0xf0) >> 4), chip->mc_base);
1663 spin_unlock_irqrestore(&chip->lock, flags);
1664
1665 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
1666 snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
1667 if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
1668 return 1;
1669
Takashi Iwaib1d57762005-10-10 11:56:31 +02001670 release_and_free_resource(chip->res_mc_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 chip->res_mc_base = NULL;
1672 }
1673#endif /* OPTi93X */
1674
1675 return -ENODEV;
1676}
1677
1678#ifdef CONFIG_PNP
Takashi Iwai99a0b762005-11-17 17:13:59 +01001679static int __init snd_card_opti9xx_pnp(struct snd_opti9xx *chip, struct pnp_card_link *card,
1680 const struct pnp_card_device_id *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 struct pnp_dev *pdev;
1683 struct pnp_resource_table *cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
1684 int err;
1685
1686 chip->dev = pnp_request_card_device(card, pid->devs[0].id, NULL);
1687 if (chip->dev == NULL) {
1688 kfree(cfg);
1689 return -EBUSY;
1690 }
1691 chip->devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
1692
1693 pdev = chip->dev;
1694 pnp_init_resource_table(cfg);
1695
1696#ifdef OPTi93X
1697 if (port != SNDRV_AUTO_PORT)
1698 pnp_resource_change(&cfg->port_resource[0], port + 4, 4);
1699#else
1700 if (pid->driver_data != 0x0924 && port != SNDRV_AUTO_PORT)
1701 pnp_resource_change(&cfg->port_resource[1], port, 4);
1702#endif /* OPTi93X */
1703 if (irq != SNDRV_AUTO_IRQ)
1704 pnp_resource_change(&cfg->irq_resource[0], irq, 1);
1705 if (dma1 != SNDRV_AUTO_DMA)
1706 pnp_resource_change(&cfg->dma_resource[0], dma1, 1);
1707#if defined(CS4231) || defined(OPTi93X)
1708 if (dma2 != SNDRV_AUTO_DMA)
1709 pnp_resource_change(&cfg->dma_resource[1], dma2, 1);
1710#else
1711#ifdef snd_opti9xx_fixup_dma2
1712 snd_opti9xx_fixup_dma2(pdev);
1713#endif
1714#endif /* CS4231 || OPTi93X */
1715#ifdef OPTi93X
1716 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT)
1717 pnp_resource_change(&cfg->port_resource[1], fm_port, 4);
1718#else
1719 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT)
1720 pnp_resource_change(&cfg->port_resource[2], fm_port, 4);
1721#endif
1722 if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
1723 snd_printk(KERN_ERR "AUDIO the requested resources are invalid, using auto config\n");
1724 err = pnp_activate_dev(pdev);
1725 if (err < 0) {
1726 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
1727 kfree(cfg);
1728 return err;
1729 }
1730
1731#ifdef OPTi93X
1732 port = pnp_port_start(pdev, 0) - 4;
1733 fm_port = pnp_port_start(pdev, 1);
1734#else
1735 if (pid->driver_data != 0x0924)
1736 port = pnp_port_start(pdev, 1);
1737 fm_port = pnp_port_start(pdev, 2);
1738#endif /* OPTi93X */
1739 irq = pnp_irq(pdev, 0);
1740 dma1 = pnp_dma(pdev, 0);
1741#if defined(CS4231) || defined(OPTi93X)
1742 dma2 = pnp_dma(pdev, 1);
1743#endif /* CS4231 || OPTi93X */
1744
1745 pdev = chip->devmpu;
1746 if (pdev && mpu_port > 0) {
1747 pnp_init_resource_table(cfg);
1748
1749 if (mpu_port != SNDRV_AUTO_PORT)
1750 pnp_resource_change(&cfg->port_resource[0], mpu_port, 2);
1751 if (mpu_irq != SNDRV_AUTO_IRQ)
1752 pnp_resource_change(&cfg->irq_resource[0], mpu_irq, 1);
1753
1754 if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
1755 snd_printk(KERN_ERR "AUDIO the requested resources are invalid, using auto config\n");
1756 err = pnp_activate_dev(pdev);
1757 if (err < 0) {
1758 snd_printk(KERN_ERR "AUDIO pnp configure failure\n");
1759 mpu_port = -1;
1760 chip->devmpu = NULL;
1761 } else {
1762 mpu_port = pnp_port_start(pdev, 0);
1763 mpu_irq = pnp_irq(pdev, 0);
1764 }
1765 }
1766 kfree(cfg);
1767 return pid->driver_data;
1768}
1769#endif /* CONFIG_PNP */
1770
Takashi Iwai346c7a62005-11-17 14:37:56 +01001771static void snd_card_opti9xx_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Takashi Iwai99a0b762005-11-17 17:13:59 +01001773 struct snd_opti9xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Takashi Iwaib1d57762005-10-10 11:56:31 +02001775 if (chip)
1776 release_and_free_resource(chip->res_mc_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
Takashi Iwai99a0b762005-11-17 17:13:59 +01001779static int __init snd_opti9xx_probe(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
1781 static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 int error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001783 struct snd_opti9xx *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784#if defined(OPTi93X)
Takashi Iwai346c7a62005-11-17 14:37:56 +01001785 struct snd_opti93x *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786#elif defined(CS4231)
Takashi Iwai346c7a62005-11-17 14:37:56 +01001787 struct snd_cs4231 *codec;
1788 struct snd_timer *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789#else
Takashi Iwai346c7a62005-11-17 14:37:56 +01001790 struct snd_ad1848 *codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791#endif
Takashi Iwai346c7a62005-11-17 14:37:56 +01001792 struct snd_pcm *pcm;
1793 struct snd_rawmidi *rmidi;
1794 struct snd_hwdep *synth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 if (! chip->res_mc_base &&
Takashi Iwai99a0b762005-11-17 17:13:59 +01001797 (chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
1798 "OPTi9xx MC")) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 chip->wss_base = port;
1802 chip->fm_port = fm_port;
1803 chip->mpu_port = mpu_port;
1804 chip->irq = irq;
1805 chip->mpu_irq = mpu_irq;
1806 chip->dma1 = dma1;
1807#if defined(CS4231) || defined(OPTi93X)
1808 chip->dma2 = dma2;
1809#endif
1810
1811 if (chip->wss_base == SNDRV_AUTO_PORT) {
1812 if ((chip->wss_base = snd_legacy_find_free_ioport(possible_ports, 4)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 snd_printk("unable to find a free WSS port\n");
1814 return -EBUSY;
1815 }
1816 }
Takashi Iwai99a0b762005-11-17 17:13:59 +01001817 if ((error = snd_opti9xx_configure(chip)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820#if defined(OPTi93X)
Takashi Iwai99a0b762005-11-17 17:13:59 +01001821 if ((error = snd_opti93x_create(card, chip, chip->dma1, chip->dma2, &codec)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001823 if ((error = snd_opti93x_pcm(codec, 0, &pcm)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001825 if ((error = snd_opti93x_mixer(codec)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827#elif defined(CS4231)
1828 if ((error = snd_cs4231_create(card, chip->wss_base + 4, -1,
1829 chip->irq, chip->dma1, chip->dma2,
1830 CS4231_HW_DETECT,
1831 0,
Takashi Iwai99a0b762005-11-17 17:13:59 +01001832 &codec)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001834 if ((error = snd_cs4231_pcm(codec, 0, &pcm)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001836 if ((error = snd_cs4231_mixer(codec)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001838 if ((error = snd_cs4231_timer(codec, 0, &timer)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840#else
1841 if ((error = snd_ad1848_create(card, chip->wss_base + 4,
1842 chip->irq, chip->dma1,
Takashi Iwai99a0b762005-11-17 17:13:59 +01001843 AD1848_HW_DETECT, &codec)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001845 if ((error = snd_ad1848_pcm(codec, 0, &pcm)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001847 if ((error = snd_ad1848_mixer(codec)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849#endif
1850 strcpy(card->driver, chip->name);
1851 sprintf(card->shortname, "OPTi %s", card->driver);
1852#if defined(CS4231) || defined(OPTi93X)
1853 sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
1854 card->shortname, pcm->name, chip->wss_base + 4,
1855 chip->irq, chip->dma1, chip->dma2);
1856#else
1857 sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
1858 card->shortname, pcm->name, chip->wss_base + 4,
1859 chip->irq, chip->dma1);
1860#endif /* CS4231 || OPTi93X */
1861
1862 if (chip->mpu_port <= 0 || chip->mpu_port == SNDRV_AUTO_PORT)
1863 rmidi = NULL;
1864 else
1865 if ((error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
Thomas Gleixner65ca68b2006-07-01 19:29:46 -07001866 chip->mpu_port, 0, chip->mpu_irq, IRQF_DISABLED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 &rmidi)))
Takashi Iwai99a0b762005-11-17 17:13:59 +01001868 snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
1869 chip->mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
Takashi Iwai346c7a62005-11-17 14:37:56 +01001872 struct snd_opl3 *opl3 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873#ifndef OPTi93X
1874 if (chip->hardware == OPTi9XX_HW_82C928 ||
1875 chip->hardware == OPTi9XX_HW_82C929 ||
1876 chip->hardware == OPTi9XX_HW_82C924) {
Takashi Iwai346c7a62005-11-17 14:37:56 +01001877 struct snd_opl4 *opl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 /* assume we have an OPL4 */
1879 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
1880 0x20, 0x20);
1881 if (snd_opl4_create(card,
1882 chip->fm_port,
1883 chip->fm_port - 8,
1884 2, &opl3, &opl4) < 0) {
1885 /* no luck, use OPL3 instead */
1886 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
1887 0x00, 0x20);
1888 }
1889 }
1890#endif /* !OPTi93X */
1891 if (!opl3 && snd_opl3_create(card,
1892 chip->fm_port,
1893 chip->fm_port + 2,
1894 OPL3_HW_AUTO, 0, &opl3) < 0) {
Takashi Iwai99a0b762005-11-17 17:13:59 +01001895 snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 chip->fm_port, chip->fm_port + 4 - 1);
1897 }
1898 if (opl3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899#ifdef CS4231
Takashi Iwai99a0b762005-11-17 17:13:59 +01001900 const int t1dev = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901#else
Takashi Iwai99a0b762005-11-17 17:13:59 +01001902 const int t1dev = 0;
1903#endif
1904 if ((error = snd_opl3_timer_new(opl3, t1dev, t1dev+1)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return error;
Takashi Iwai99a0b762005-11-17 17:13:59 +01001906 if ((error = snd_opl3_hwdep_new(opl3, 0, 1, &synth)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
1909 }
1910
Takashi Iwai99a0b762005-11-17 17:13:59 +01001911 return snd_card_register(card);
1912}
1913
1914static struct snd_card *snd_opti9xx_card_new(void)
1915{
1916 struct snd_card *card;
1917
1918 card = snd_card_new(index, id, THIS_MODULE, sizeof(struct snd_opti9xx));
1919 if (! card)
1920 return NULL;
1921 card->private_free = snd_card_opti9xx_free;
1922 return card;
1923}
1924
1925static int __init snd_opti9xx_nonpnp_probe(struct platform_device *devptr)
1926{
1927 struct snd_card *card;
1928 int error;
1929 static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
1930#ifdef OPTi93X
1931 static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
1932#else
1933 static int possible_irqs[] = {9, 10, 11, 7, -1};
1934#endif /* OPTi93X */
1935 static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
1936 static int possible_dma1s[] = {3, 1, 0, -1};
1937#if defined(CS4231) || defined(OPTi93X)
1938 static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
1939#endif /* CS4231 || OPTi93X */
1940
1941 if (snd_opti9xx_pnp_is_probed)
1942 return -EBUSY;
1943
1944 if (mpu_port == SNDRV_AUTO_PORT) {
1945 if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
1946 snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
1947 return -EBUSY;
1948 }
1949 }
1950 if (irq == SNDRV_AUTO_IRQ) {
1951 if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
1952 snd_printk(KERN_ERR "unable to find a free IRQ\n");
1953 return -EBUSY;
1954 }
1955 }
1956 if (mpu_irq == SNDRV_AUTO_IRQ) {
1957 if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
1958 snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
1959 return -EBUSY;
1960 }
1961 }
1962 if (dma1 == SNDRV_AUTO_DMA) {
1963 if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
1964 snd_printk(KERN_ERR "unable to find a free DMA1\n");
1965 return -EBUSY;
1966 }
1967 }
1968#if defined(CS4231) || defined(OPTi93X)
1969 if (dma2 == SNDRV_AUTO_DMA) {
1970 if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
1971 snd_printk("unable to find a free DMA2\n");
1972 return -EBUSY;
1973 }
1974 }
1975#endif
1976
1977 card = snd_opti9xx_card_new();
1978 if (! card)
1979 return -ENOMEM;
1980
1981 if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 snd_card_free(card);
1983 return error;
1984 }
Takashi Iwai99a0b762005-11-17 17:13:59 +01001985 snd_card_set_dev(card, &devptr->dev);
1986 if ((error = snd_opti9xx_probe(card)) < 0) {
1987 snd_card_free(card);
1988 return error;
1989 }
1990 platform_set_drvdata(devptr, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return 0;
1992}
1993
Takashi Iwai99a0b762005-11-17 17:13:59 +01001994static int __devexit snd_opti9xx_nonpnp_remove(struct platform_device *devptr)
1995{
1996 snd_card_free(platform_get_drvdata(devptr));
1997 platform_set_drvdata(devptr, NULL);
1998 return 0;
1999}
2000
2001static struct platform_driver snd_opti9xx_driver = {
2002 .probe = snd_opti9xx_nonpnp_probe,
2003 .remove = __devexit_p(snd_opti9xx_nonpnp_remove),
2004 /* FIXME: suspend/resume */
2005 .driver = {
2006 .name = DRIVER_NAME
2007 },
2008};
2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010#ifdef CONFIG_PNP
Takashi Iwai99a0b762005-11-17 17:13:59 +01002011static int __init snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
2012 const struct pnp_card_device_id *pid)
2013{
2014 struct snd_card *card;
2015 int error, hw;
2016 struct snd_opti9xx *chip;
2017
2018 if (snd_opti9xx_pnp_is_probed)
2019 return -EBUSY;
2020 if (! isapnp)
2021 return -ENODEV;
2022 card = snd_opti9xx_card_new();
2023 if (! card)
2024 return -ENOMEM;
2025 chip = card->private_data;
2026
2027 hw = snd_card_opti9xx_pnp(chip, pcard, pid);
2028 switch (hw) {
2029 case 0x0924:
2030 hw = OPTi9XX_HW_82C924;
2031 break;
2032 case 0x0925:
2033 hw = OPTi9XX_HW_82C925;
2034 break;
2035 case 0x0931:
2036 hw = OPTi9XX_HW_82C931;
2037 break;
2038 default:
2039 snd_card_free(card);
2040 return -ENODEV;
2041 }
2042
2043 if ((error = snd_opti9xx_init(chip, hw))) {
2044 snd_card_free(card);
2045 return error;
2046 }
2047 if (hw <= OPTi9XX_HW_82C930)
2048 chip->mc_base -= 0x80;
2049 snd_card_set_dev(card, &pcard->card->dev);
2050 if ((error = snd_opti9xx_probe(card)) < 0) {
2051 snd_card_free(card);
2052 return error;
2053 }
2054 pnp_set_card_drvdata(pcard, card);
2055 snd_opti9xx_pnp_is_probed = 1;
2056 return 0;
2057}
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059static void __devexit snd_opti9xx_pnp_remove(struct pnp_card_link * pcard)
2060{
Takashi Iwai99a0b762005-11-17 17:13:59 +01002061 snd_card_free(pnp_get_card_drvdata(pcard));
2062 pnp_set_card_drvdata(pcard, NULL);
2063 snd_opti9xx_pnp_is_probed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
2066static struct pnp_card_driver opti9xx_pnpc_driver = {
2067 .flags = PNP_DRIVER_RES_DISABLE,
2068 .name = "opti9xx",
2069 .id_table = snd_opti9xx_pnpids,
Takashi Iwai99a0b762005-11-17 17:13:59 +01002070 .probe = snd_opti9xx_pnp_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 .remove = __devexit_p(snd_opti9xx_pnp_remove),
2072};
2073#endif
2074
Takashi Iwai99a0b762005-11-17 17:13:59 +01002075#ifdef CONFIG_PNP
2076#define is_isapnp_selected() isapnp
2077#else
2078#define is_isapnp_selected() 0
2079#endif
2080#ifdef OPTi93X
2081#define CHIP_NAME "82C93x"
2082#else
2083#define CHIP_NAME "82C92x"
2084#endif
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086static int __init alsa_card_opti9xx_init(void)
2087{
Takashi Iwai99a0b762005-11-17 17:13:59 +01002088 int error;
2089 struct platform_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01002091#ifdef CONFIG_PNP
Takashi Iwai99a0b762005-11-17 17:13:59 +01002092 pnp_register_card_driver(&opti9xx_pnpc_driver);
2093 if (snd_opti9xx_pnp_is_probed)
2094 return 0;
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01002095#endif
Takashi Iwai99a0b762005-11-17 17:13:59 +01002096 if (! is_isapnp_selected()) {
2097 error = platform_driver_register(&snd_opti9xx_driver);
2098 if (error < 0)
2099 return error;
2100 device = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
Clemens Ladischf7a92752005-12-07 09:13:42 +01002101 if (!IS_ERR(device)) {
Rene Hermandcccdd92006-04-11 14:09:37 +02002102 if (platform_get_drvdata(device)) {
2103 snd_opti9xx_platform_device = device;
2104 return 0;
2105 }
2106 platform_device_unregister(device);
Clemens Ladischf7a92752005-12-07 09:13:42 +01002107 }
Takashi Iwai99a0b762005-11-17 17:13:59 +01002108 platform_driver_unregister(&snd_opti9xx_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01002110#ifdef CONFIG_PNP
Takashi Iwai99a0b762005-11-17 17:13:59 +01002111 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01002112#endif
Takashi Iwai99a0b762005-11-17 17:13:59 +01002113#ifdef MODULE
2114 printk(KERN_ERR "no OPTi " CHIP_NAME " soundcard found\n");
2115#endif
2116 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
2119static void __exit alsa_card_opti9xx_exit(void)
2120{
Clemens Ladischf7a92752005-12-07 09:13:42 +01002121 if (!snd_opti9xx_pnp_is_probed) {
2122 platform_device_unregister(snd_opti9xx_platform_device);
Takashi Iwai99a0b762005-11-17 17:13:59 +01002123 platform_driver_unregister(&snd_opti9xx_driver);
Clemens Ladischf7a92752005-12-07 09:13:42 +01002124 }
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01002125#ifdef CONFIG_PNP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
Takashi Iwai0bbbc4c2006-02-27 17:23:46 +01002127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
2130module_init(alsa_card_opti9xx_init)
2131module_exit(alsa_card_opti9xx_exit)