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