Merge branch 'tip/tracing/core' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / sound / isa / sb / jazz16.c
1
2 /*
3  * jazz16.c - driver for Media Vision Jazz16 based soundcards.
4  * Copyright (C) 2009 Krzysztof Helt <krzysztof.h1@wp.pl>
5  * Based on patches posted by Rask Ingemann Lambertsen and Rene Herman.
6  * Based on OSS Sound Blaster driver.
7  *
8  * This file is subject to the terms and conditions of the GNU General Public
9  * License.  See the file COPYING in the main directory of this archive for
10  * more details.
11  *
12  */
13
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/io.h>
17 #include <asm/dma.h>
18 #include <linux/isa.h>
19 #include <sound/core.h>
20 #include <sound/mpu401.h>
21 #include <sound/opl3.h>
22 #include <sound/sb.h>
23 #define SNDRV_LEGACY_FIND_FREE_IRQ
24 #define SNDRV_LEGACY_FIND_FREE_DMA
25 #include <sound/initval.h>
26
27 #define PFX "jazz16: "
28
29 MODULE_DESCRIPTION("Media Vision Jazz16");
30 MODULE_SUPPORTED_DEVICE("{{Media Vision ??? },"
31                 "{RTL,RTL3000}}");
32
33 MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>");
34 MODULE_LICENSE("GPL");
35
36 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
37 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
38 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;  /* Enable this card */
39 static unsigned long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
40 static unsigned long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
41 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
42 static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
43 static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
44 static int dma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
45
46 module_param_array(index, int, NULL, 0444);
47 MODULE_PARM_DESC(index, "Index value for Media Vision Jazz16 based soundcard.");
48 module_param_array(id, charp, NULL, 0444);
49 MODULE_PARM_DESC(id, "ID string for Media Vision Jazz16 based soundcard.");
50 module_param_array(enable, bool, NULL, 0444);
51 MODULE_PARM_DESC(enable, "Enable Media Vision Jazz16 based soundcard.");
52 module_param_array(port, long, NULL, 0444);
53 MODULE_PARM_DESC(port, "Port # for jazz16 driver.");
54 module_param_array(mpu_port, long, NULL, 0444);
55 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for jazz16 driver.");
56 module_param_array(irq, int, NULL, 0444);
57 MODULE_PARM_DESC(irq, "IRQ # for jazz16 driver.");
58 module_param_array(mpu_irq, int, NULL, 0444);
59 MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for jazz16 driver.");
60 module_param_array(dma8, int, NULL, 0444);
61 MODULE_PARM_DESC(dma8, "DMA8 # for jazz16 driver.");
62 module_param_array(dma16, int, NULL, 0444);
63 MODULE_PARM_DESC(dma16, "DMA16 # for jazz16 driver.");
64
65 #define SB_JAZZ16_WAKEUP        0xaf
66 #define SB_JAZZ16_SET_PORTS     0x50
67 #define SB_DSP_GET_JAZZ_BRD_REV 0xfa
68 #define SB_JAZZ16_SET_DMAINTR   0xfb
69 #define SB_DSP_GET_JAZZ_MODEL   0xfe
70
71 struct snd_card_jazz16 {
72         struct snd_sb *chip;
73 };
74
75 static irqreturn_t jazz16_interrupt(int irq, void *chip)
76 {
77         return snd_sb8dsp_interrupt(chip);
78 }
79
80 static int __devinit jazz16_configure_ports(unsigned long port,
81                                             unsigned long mpu_port, int idx)
82 {
83         unsigned char val;
84
85         if (!request_region(0x201, 1, "jazz16 config")) {
86                 snd_printk(KERN_ERR "config port region is already in use.\n");
87                 return -EBUSY;
88         }
89         outb(SB_JAZZ16_WAKEUP - idx, 0x201);
90         udelay(100);
91         outb(SB_JAZZ16_SET_PORTS + idx, 0x201);
92         udelay(100);
93         val = port & 0x70;
94         val |= (mpu_port & 0x30) >> 4;
95         outb(val, 0x201);
96
97         release_region(0x201, 1);
98         return 0;
99 }
100
101 static int __devinit jazz16_detect_board(unsigned long port,
102                                          unsigned long mpu_port)
103 {
104         int err;
105         int val;
106         struct snd_sb chip;
107
108         if (!request_region(port, 0x10, "jazz16")) {
109                 snd_printk(KERN_ERR "I/O port region is already in use.\n");
110                 return -EBUSY;
111         }
112         /* just to call snd_sbdsp_command/reset/get_byte() */
113         chip.port = port;
114
115         err = snd_sbdsp_reset(&chip);
116         if (err < 0)
117                 for (val = 0; val < 4; val++) {
118                         err = jazz16_configure_ports(port, mpu_port, val);
119                         if (err < 0)
120                                 break;
121
122                         err = snd_sbdsp_reset(&chip);
123                         if (!err)
124                                 break;
125                 }
126         if (err < 0) {
127                 err = -ENODEV;
128                 goto err_unmap;
129         }
130         if (!snd_sbdsp_command(&chip, SB_DSP_GET_JAZZ_BRD_REV)) {
131                 err = -EBUSY;
132                 goto err_unmap;
133         }
134         val = snd_sbdsp_get_byte(&chip);
135         if (val >= 0x30)
136                 snd_sbdsp_get_byte(&chip);
137
138         if ((val & 0xf0) != 0x10) {
139                 err = -ENODEV;
140                 goto err_unmap;
141         }
142         if (!snd_sbdsp_command(&chip, SB_DSP_GET_JAZZ_MODEL)) {
143                 err = -EBUSY;
144                 goto err_unmap;
145         }
146         snd_sbdsp_get_byte(&chip);
147         err = snd_sbdsp_get_byte(&chip);
148         snd_printd("Media Vision Jazz16 board detected: rev 0x%x, model 0x%x\n",
149                    val, err);
150
151         err = 0;
152
153 err_unmap:
154         release_region(port, 0x10);
155         return err;
156 }
157
158 static int __devinit jazz16_configure_board(struct snd_sb *chip, int mpu_irq)
159 {
160         static unsigned char jazz_irq_bits[] = { 0, 0, 2, 3, 0, 1, 0, 4,
161                                                  0, 2, 5, 0, 0, 0, 0, 6 };
162         static unsigned char jazz_dma_bits[] = { 0, 1, 0, 2, 0, 3, 0, 4 };
163
164         if (jazz_dma_bits[chip->dma8] == 0 ||
165             jazz_dma_bits[chip->dma16] == 0 ||
166             jazz_irq_bits[chip->irq] == 0)
167                 return -EINVAL;
168
169         if (!snd_sbdsp_command(chip, SB_JAZZ16_SET_DMAINTR))
170                 return -EBUSY;
171
172         if (!snd_sbdsp_command(chip,
173                                jazz_dma_bits[chip->dma8] |
174                                (jazz_dma_bits[chip->dma16] << 4)))
175                 return -EBUSY;
176
177         if (!snd_sbdsp_command(chip,
178                                jazz_irq_bits[chip->irq] |
179                                (jazz_irq_bits[mpu_irq] << 4)))
180                 return -EBUSY;
181
182         return 0;
183 }
184
185 static int __devinit snd_jazz16_match(struct device *devptr, unsigned int dev)
186 {
187         if (!enable[dev])
188                 return 0;
189         if (port[dev] == SNDRV_AUTO_PORT) {
190                 snd_printk(KERN_ERR "please specify port\n");
191                 return 0;
192         } else if (port[dev] == 0x200 || (port[dev] & ~0x270)) {
193                 snd_printk(KERN_ERR "incorrect port specified\n");
194                 return 0;
195         }
196         if (dma8[dev] != SNDRV_AUTO_DMA &&
197             dma8[dev] != 1 && dma8[dev] != 3) {
198                 snd_printk(KERN_ERR "dma8 must be 1 or 3\n");
199                 return 0;
200         }
201         if (dma16[dev] != SNDRV_AUTO_DMA &&
202             dma16[dev] != 5 && dma16[dev] != 7) {
203                 snd_printk(KERN_ERR "dma16 must be 5 or 7\n");
204                 return 0;
205         }
206         if (mpu_port[dev] != SNDRV_AUTO_PORT &&
207             (mpu_port[dev] & ~0x030) != 0x300) {
208                 snd_printk(KERN_ERR "incorrect mpu_port specified\n");
209                 return 0;
210         }
211         if (mpu_irq[dev] != SNDRV_AUTO_DMA &&
212             mpu_irq[dev] != 2 && mpu_irq[dev] != 3 &&
213             mpu_irq[dev] != 5 && mpu_irq[dev] != 7) {
214                 snd_printk(KERN_ERR "mpu_irq must be 2, 3, 5 or 7\n");
215                 return 0;
216         }
217         return 1;
218 }
219
220 static int __devinit snd_jazz16_probe(struct device *devptr, unsigned int dev)
221 {
222         struct snd_card *card;
223         struct snd_card_jazz16 *jazz16;
224         struct snd_sb *chip;
225         struct snd_opl3 *opl3;
226         static int possible_irqs[] = {2, 3, 5, 7, 9, 10, 15, -1};
227         static int possible_dmas8[] = {1, 3, -1};
228         static int possible_dmas16[] = {5, 7, -1};
229         int err, xirq, xdma8, xdma16, xmpu_port, xmpu_irq;
230
231         err = snd_card_create(index[dev], id[dev], THIS_MODULE,
232                               sizeof(struct snd_card_jazz16), &card);
233         if (err < 0)
234                 return err;
235
236         jazz16 = card->private_data;
237
238         xirq = irq[dev];
239         if (xirq == SNDRV_AUTO_IRQ) {
240                 xirq = snd_legacy_find_free_irq(possible_irqs);
241                 if (xirq < 0) {
242                         snd_printk(KERN_ERR "unable to find a free IRQ\n");
243                         err = -EBUSY;
244                         goto err_free;
245                 }
246         }
247         xdma8 = dma8[dev];
248         if (xdma8 == SNDRV_AUTO_DMA) {
249                 xdma8 = snd_legacy_find_free_dma(possible_dmas8);
250                 if (xdma8 < 0) {
251                         snd_printk(KERN_ERR "unable to find a free DMA8\n");
252                         err = -EBUSY;
253                         goto err_free;
254                 }
255         }
256         xdma16 = dma16[dev];
257         if (xdma16 == SNDRV_AUTO_DMA) {
258                 xdma16 = snd_legacy_find_free_dma(possible_dmas16);
259                 if (xdma16 < 0) {
260                         snd_printk(KERN_ERR "unable to find a free DMA16\n");
261                         err = -EBUSY;
262                         goto err_free;
263                 }
264         }
265
266         xmpu_port = mpu_port[dev];
267         if (xmpu_port == SNDRV_AUTO_PORT)
268                 xmpu_port = 0;
269         err = jazz16_detect_board(port[dev], xmpu_port);
270         if (err < 0) {
271                 printk(KERN_ERR "Media Vision Jazz16 board not detected\n");
272                 goto err_free;
273         }
274         err = snd_sbdsp_create(card, port[dev], irq[dev],
275                                jazz16_interrupt,
276                                dma8[dev], dma16[dev],
277                                SB_HW_JAZZ16,
278                                &chip);
279         if (err < 0)
280                 goto err_free;
281
282         xmpu_irq = mpu_irq[dev];
283         if (xmpu_irq == SNDRV_AUTO_IRQ || mpu_port[dev] == SNDRV_AUTO_PORT)
284                 xmpu_irq = 0;
285         err = jazz16_configure_board(chip, xmpu_irq);
286         if (err < 0) {
287                 printk(KERN_ERR "Media Vision Jazz16 configuration failed\n");
288                 goto err_free;
289         }
290
291         jazz16->chip = chip;
292
293         strcpy(card->driver, "jazz16");
294         strcpy(card->shortname, "Media Vision Jazz16");
295         sprintf(card->longname,
296                 "Media Vision Jazz16 at 0x%lx, irq %d, dma8 %d, dma16 %d",
297                 port[dev], xirq, xdma8, xdma16);
298
299         err = snd_sb8dsp_pcm(chip, 0, NULL);
300         if (err < 0)
301                 goto err_free;
302         err = snd_sbmixer_new(chip);
303         if (err < 0)
304                 goto err_free;
305
306         err = snd_opl3_create(card, chip->port, chip->port + 2,
307                               OPL3_HW_AUTO, 1, &opl3);
308         if (err < 0)
309                 snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
310                            chip->port, chip->port + 2);
311         else {
312                 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
313                 if (err < 0)
314                         goto err_free;
315         }
316         if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
317                 if (mpu_irq[dev] == SNDRV_AUTO_IRQ)
318                         mpu_irq[dev] = -1;
319
320                 if (snd_mpu401_uart_new(card, 0,
321                                         MPU401_HW_MPU401,
322                                         mpu_port[dev], 0,
323                                         mpu_irq[dev],
324                                         mpu_irq[dev] >= 0 ? IRQF_DISABLED : 0,
325                                         NULL) < 0)
326                         snd_printk(KERN_ERR "no MPU-401 device at 0x%lx\n",
327                                         mpu_port[dev]);
328         }
329
330         snd_card_set_dev(card, devptr);
331
332         err = snd_card_register(card);
333         if (err < 0)
334                 goto err_free;
335
336         dev_set_drvdata(devptr, card);
337         return 0;
338
339 err_free:
340         snd_card_free(card);
341         return err;
342 }
343
344 static int __devexit snd_jazz16_remove(struct device *devptr, unsigned int dev)
345 {
346         struct snd_card *card = dev_get_drvdata(devptr);
347
348         dev_set_drvdata(devptr, NULL);
349         snd_card_free(card);
350         return 0;
351 }
352
353 #ifdef CONFIG_PM
354 static int snd_jazz16_suspend(struct device *pdev, unsigned int n,
355                                pm_message_t state)
356 {
357         struct snd_card *card = dev_get_drvdata(pdev);
358         struct snd_card_jazz16 *acard = card->private_data;
359         struct snd_sb *chip = acard->chip;
360
361         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
362         snd_pcm_suspend_all(chip->pcm);
363         snd_sbmixer_suspend(chip);
364         return 0;
365 }
366
367 static int snd_jazz16_resume(struct device *pdev, unsigned int n)
368 {
369         struct snd_card *card = dev_get_drvdata(pdev);
370         struct snd_card_jazz16 *acard = card->private_data;
371         struct snd_sb *chip = acard->chip;
372
373         snd_sbdsp_reset(chip);
374         snd_sbmixer_resume(chip);
375         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
376         return 0;
377 }
378 #endif
379
380 static struct isa_driver snd_jazz16_driver = {
381         .match          = snd_jazz16_match,
382         .probe          = snd_jazz16_probe,
383         .remove         = __devexit_p(snd_jazz16_remove),
384 #ifdef CONFIG_PM
385         .suspend        = snd_jazz16_suspend,
386         .resume         = snd_jazz16_resume,
387 #endif
388         .driver         = {
389                 .name   = "jazz16"
390         },
391 };
392
393 static int __init alsa_card_jazz16_init(void)
394 {
395         return isa_register_driver(&snd_jazz16_driver, SNDRV_CARDS);
396 }
397
398 static void __exit alsa_card_jazz16_exit(void)
399 {
400         isa_unregister_driver(&snd_jazz16_driver);
401 }
402
403 module_init(alsa_card_jazz16_init)
404 module_exit(alsa_card_jazz16_exit)