ad1816.c revision 54825
1/*
2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
3 * Copyright Luigi Rizzo, 1997,1998
4 * Copyright by Hannu Savolainen 1994, 1995
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/dev/sound/isa/ad1816.c 54825 1999-12-19 15:07:56Z cg $
29 */
30
31#include <dev/sound/pcm/sound.h>
32#include <dev/sound/isa/ad1816.h>
33
34struct ad1816_info;
35
36struct ad1816_chinfo {
37	struct ad1816_info *parent;
38	pcm_channel *channel;
39	snd_dbuf *buffer;
40	int dir;
41};
42
43struct ad1816_info {
44    struct resource *io_base;	/* primary I/O address for the board */
45    int		     io_rid;
46    struct resource *irq;
47    int		     irq_rid;
48    struct resource *drq1; /* play */
49    int		     drq1_rid;
50    struct resource *drq2; /* rec */
51    int		     drq2_rid;
52    bus_dma_tag_t    parent_dmat;
53
54    struct ad1816_chinfo pch, rch;
55};
56
57static driver_intr_t 	ad1816_intr;
58static int 		ad1816_probe(device_t dev);
59static int 		ad1816_attach(device_t dev);
60
61/* IO primitives */
62static int      	ad1816_wait_init(struct ad1816_info *ad1816, int x);
63static u_short		ad1816_read(struct ad1816_info *ad1816, u_int reg);
64static void     	ad1816_write(struct ad1816_info *ad1816, u_int reg, u_short data);
65
66static int ad1816mix_init(snd_mixer *m);
67static int ad1816mix_set(snd_mixer *m, unsigned dev, unsigned left, unsigned right);
68static int ad1816mix_setrecsrc(snd_mixer *m, u_int32_t src);
69static snd_mixer ad1816_mixer = {
70    "ad1816 mixer",
71    ad1816mix_init,
72    ad1816mix_set,
73    ad1816mix_setrecsrc,
74};
75
76static devclass_t pcm_devclass;
77
78/* channel interface */
79static void *ad1816chan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir);
80static int ad1816chan_setdir(void *data, int dir);
81static int ad1816chan_setformat(void *data, u_int32_t format);
82static int ad1816chan_setspeed(void *data, u_int32_t speed);
83static int ad1816chan_setblocksize(void *data, u_int32_t blocksize);
84static int ad1816chan_trigger(void *data, int go);
85static int ad1816chan_getptr(void *data);
86static pcmchan_caps *ad1816chan_getcaps(void *data);
87
88static pcmchan_caps ad1816_caps = {
89	4000, 55200,
90	AFMT_STEREO | AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW,
91	AFMT_STEREO | AFMT_S16_LE
92};
93
94static pcm_channel ad1816_chantemplate = {
95	ad1816chan_init,
96	ad1816chan_setdir,
97	ad1816chan_setformat,
98	ad1816chan_setspeed,
99	ad1816chan_setblocksize,
100	ad1816chan_trigger,
101	ad1816chan_getptr,
102	ad1816chan_getcaps,
103};
104
105#define AD1816_MUTE 31		/* value for mute */
106
107static int
108port_rd(struct resource *port, int off)
109{
110	if (port)
111		return bus_space_read_1(rman_get_bustag(port),
112					rman_get_bushandle(port),
113					off);
114	else
115		return -1;
116}
117
118static void
119port_wr(struct resource *port, int off, u_int8_t data)
120{
121	if (port)
122		return bus_space_write_1(rman_get_bustag(port),
123					 rman_get_bushandle(port),
124					 off, data);
125}
126
127static int
128io_rd(struct ad1816_info *ad1816, int reg)
129{
130	return port_rd(ad1816->io_base, reg);
131}
132
133static void
134io_wr(struct ad1816_info *ad1816, int reg, u_int8_t data)
135{
136	return port_wr(ad1816->io_base, reg, data);
137}
138
139static void
140ad1816_intr(void *arg)
141{
142    	struct ad1816_info *ad1816 = (struct ad1816_info *)arg;
143    	unsigned char   c, served = 0;
144
145    	/* get interupt status */
146    	c = io_rd(ad1816, AD1816_INT);
147
148    	/* check for stray interupts */
149    	if (c & ~(AD1816_INTRCI | AD1816_INTRPI)) {
150		printf("pcm: stray int (%x)\n", c);
151		c &= AD1816_INTRCI | AD1816_INTRPI;
152    	}
153    	/* check for capture interupt */
154    	if (ad1816->rch.buffer->dl && (c & AD1816_INTRCI)) {
155		chn_intr(ad1816->rch.channel);
156		served |= AD1816_INTRCI;		/* cp served */
157    	}
158    	/* check for playback interupt */
159    	if (ad1816->pch.buffer->dl && (c & AD1816_INTRPI)) {
160		chn_intr(ad1816->pch.channel);
161		served |= AD1816_INTRPI;		/* pb served */
162    	}
163    	if (served == 0) {
164		/* this probably means this is not a (working) ad1816 chip, */
165		/* or an error in dma handling                              */
166		printf("pcm: int without reason (%x)\n", c);
167		c = 0;
168    	} else c &= ~served;
169    	io_wr(ad1816, AD1816_INT, c);
170    	c = io_rd(ad1816, AD1816_INT);
171    	if (c != 0) printf("pcm: int clear failed (%x)\n", c);
172}
173
174static int
175ad1816_wait_init(struct ad1816_info *ad1816, int x)
176{
177    	int             n = 0;	/* to shut up the compiler... */
178
179    	for (; x--;)
180		if ((n = (io_rd(ad1816, AD1816_ALE) & AD1816_BUSY)) == 0) DELAY(10);
181		else return n;
182    	printf("ad1816_wait_init failed 0x%02x.\n", n);
183    	return -1;
184}
185
186static unsigned short
187ad1816_read(struct ad1816_info *ad1816, unsigned int reg)
188{
189    	int             flags;
190    	u_short         x = 0;
191
192    	/* we don't want to be blocked here */
193    	flags = spltty();
194    	if (ad1816_wait_init(ad1816, 100) == -1) return 0;
195    	io_wr(ad1816, AD1816_ALE, 0);
196    	io_wr(ad1816, AD1816_ALE, (reg & AD1816_ALEMASK));
197    	if (ad1816_wait_init(ad1816, 100) == -1) return 0;
198    	x = (io_rd(ad1816, AD1816_HIGH) << 8) | io_rd(ad1816, AD1816_LOW);
199    	splx(flags);
200    	return x;
201}
202
203static void
204ad1816_write(struct ad1816_info *ad1816, unsigned int reg, unsigned short data)
205{
206    	int             flags;
207
208    	flags = spltty();
209    	if (ad1816_wait_init(ad1816, 100) == -1) return;
210    	io_wr(ad1816, AD1816_ALE, (reg & AD1816_ALEMASK));
211    	io_wr(ad1816, AD1816_LOW,  (data & 0x000000ff));
212    	io_wr(ad1816, AD1816_HIGH, (data & 0x0000ff00) >> 8);
213    	splx(flags);
214}
215
216static int
217ad1816mix_init(snd_mixer *m)
218{
219	mix_setdevs(m, AD1816_MIXER_DEVICES);
220	mix_setrecdevs(m, AD1816_REC_DEVICES);
221	return 0;
222}
223
224static int
225ad1816mix_set(snd_mixer *m, unsigned dev, unsigned left, unsigned right)
226{
227	struct ad1816_info *ad1816 = mix_getdevinfo(m);
228    	u_short reg = 0;
229
230    	/* Scale volumes */
231    	left = AD1816_MUTE - (AD1816_MUTE * left) / 100;
232    	right = AD1816_MUTE - (AD1816_MUTE * right) / 100;
233
234    	reg = (left << 8) | right;
235
236    	/* do channel selective muting if volume is zero */
237    	if (left == AD1816_MUTE)	reg |= 0x8000;
238    	if (right == AD1816_MUTE)	reg |= 0x0080;
239
240    	switch (dev) {
241    	case SOUND_MIXER_VOLUME:	/* Register 14 master volume */
242		ad1816_write(ad1816, 14, reg);
243		break;
244
245    	case SOUND_MIXER_CD:	/* Register 15 cd */
246    	case SOUND_MIXER_LINE1:
247		ad1816_write(ad1816, 15, reg);
248		break;
249
250    	case SOUND_MIXER_SYNTH:	/* Register 16 synth */
251		ad1816_write(ad1816, 16, reg);
252		break;
253
254    	case SOUND_MIXER_PCM:	/* Register 4 pcm */
255		ad1816_write(ad1816, 4, reg);
256		break;
257
258    	case SOUND_MIXER_LINE:
259    	case SOUND_MIXER_LINE3:	/* Register 18 line in */
260		ad1816_write(ad1816, 18, reg);
261		break;
262
263    	case SOUND_MIXER_MIC:	/* Register 19 mic volume */
264		ad1816_write(ad1816, 19, reg & ~0xff);	/* mic is mono */
265		break;
266
267    	case SOUND_MIXER_IGAIN:
268		/* and now to something completely different ... */
269		ad1816_write(ad1816, 20, ((ad1816_read(ad1816, 20) & ~0x0f0f)
270	      	| (((AD1816_MUTE - left) / 2) << 8) /* four bits of adc gain */
271	      	| ((AD1816_MUTE - right) / 2)));
272		break;
273
274    	default:
275		printf("ad1816_mixer_set(): unknown device.\n");
276		break;
277    	}
278
279    	left = ((AD1816_MUTE - left) * 100) / AD1816_MUTE;
280    	right = ((AD1816_MUTE - right) * 100) / AD1816_MUTE;
281
282    	return left | (right << 8);
283}
284
285static int
286ad1816mix_setrecsrc(snd_mixer *m, u_int32_t src)
287{
288	struct ad1816_info *ad1816 = mix_getdevinfo(m);
289    	int dev;
290
291    	switch (src) {
292    	case SOUND_MASK_LINE:
293    	case SOUND_MASK_LINE3:
294		dev = 0x00;
295		break;
296
297    	case SOUND_MASK_CD:
298    	case SOUND_MASK_LINE1:
299		dev = 0x20;
300		break;
301
302    	case SOUND_MASK_MIC:
303    	default:
304		dev = 0x50;
305		src = SOUND_MASK_MIC;
306    	}
307
308    	dev |= dev << 8;
309    	ad1816_write(ad1816, 20, (ad1816_read(ad1816, 20) & ~0x7070) | dev);
310    	return src;
311}
312
313/* channel interface */
314static void *
315ad1816chan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir)
316{
317	struct ad1816_info *ad1816 = devinfo;
318	struct ad1816_chinfo *ch = (dir == PCMDIR_PLAY)? &ad1816->pch : &ad1816->rch;
319
320	ch->parent = ad1816;
321	ch->channel = c;
322	ch->buffer = b;
323	ch->buffer->bufsize = DSP_BUFFSIZE;
324	if (chn_allocbuf(ch->buffer, ad1816->parent_dmat) == -1) return NULL;
325	return ch;
326}
327
328static int
329ad1816chan_setdir(void *data, int dir)
330{
331	struct ad1816_chinfo *ch = data;
332  	struct ad1816_info *ad1816 = ch->parent;
333
334	ch->buffer->chan = rman_get_start((dir == PCMDIR_PLAY)?
335		ad1816->drq1 : ad1816->drq2);
336	ch->dir = dir;
337	return 0;
338}
339
340static int
341ad1816chan_setformat(void *data, u_int32_t format)
342{
343	struct ad1816_chinfo *ch = data;
344  	struct ad1816_info *ad1816 = ch->parent;
345
346    	int fmt = AD1816_U8, reg;
347    	if (ch->dir == PCMDIR_PLAY) {
348        	reg = AD1816_PLAY;
349        	ad1816_write(ad1816, 8, 0x0000);	/* reset base and current counter */
350        	ad1816_write(ad1816, 9, 0x0000);	/* for playback and capture */
351    	} else {
352        	reg = AD1816_CAPT;
353        	ad1816_write(ad1816, 10, 0x0000);
354        	ad1816_write(ad1816, 11, 0x0000);
355    	}
356    	switch (format & ~AFMT_STEREO) {
357    	case AFMT_A_LAW:
358        	fmt = AD1816_ALAW;
359		break;
360
361    	case AFMT_MU_LAW:
362		fmt = AD1816_MULAW;
363		break;
364
365    	case AFMT_S16_LE:
366		fmt = AD1816_S16LE;
367		break;
368
369    	case AFMT_S16_BE:
370		fmt = AD1816_S16BE;
371		break;
372
373    	case AFMT_U8:
374		fmt = AD1816_U8;
375		break;
376    	}
377    	if (format & AFMT_STEREO) fmt |= AD1816_STEREO;
378    	io_wr(ad1816, reg, fmt);
379    	return format;
380}
381
382static int
383ad1816chan_setspeed(void *data, u_int32_t speed)
384{
385	struct ad1816_chinfo *ch = data;
386    	struct ad1816_info *ad1816 = ch->parent;
387
388    	RANGE(speed, 4000, 55200);
389    	ad1816_write(ad1816, (ch->dir == PCMDIR_PLAY)? 2 : 3, speed);
390    	return speed;
391}
392
393static int
394ad1816chan_setblocksize(void *data, u_int32_t blocksize)
395{
396	return blocksize;
397}
398
399static int
400ad1816chan_trigger(void *data, int go)
401{
402	struct ad1816_chinfo *ch = data;
403    	struct ad1816_info *ad1816 = ch->parent;
404    	int wr, reg;
405
406	buf_isadma(ch->buffer, go);
407    	wr = (ch->dir == PCMDIR_PLAY);
408    	reg = wr? AD1816_PLAY : AD1816_CAPT;
409    	switch (go) {
410    	case PCMTRIG_START:
411		/* start only if not already running */
412		if (!(io_rd(ad1816, reg) & AD1816_ENABLE)) {
413	    		int cnt = ((ch->buffer->dl) >> 2) - 1;
414	    		ad1816_write(ad1816, wr? 8 : 10, cnt); /* count */
415	    		ad1816_write(ad1816, 1, ad1816_read(ad1816, 1) |
416				     (wr? 0x8000 : 0x4000)); /* enable int */
417	    		/* enable playback */
418	    		io_wr(ad1816, reg, io_rd(ad1816, reg) | AD1816_ENABLE);
419	    		if (!(io_rd(ad1816, reg) & AD1816_ENABLE))
420				printf("ad1816: failed to start %s DMA!\n",
421				       wr? "play" : "rec");
422		}
423		break;
424
425    	case PCMTRIG_STOP:
426    	case PCMTRIG_ABORT:		/* XXX check this... */
427		/* we don't test here if it is running... */
428		if (wr) {
429	    		ad1816_write(ad1816, 1, ad1816_read(ad1816, 1) &
430				     ~(wr? 0x8000 : 0x4000));
431	    		/* disable int */
432	    		io_wr(ad1816, reg, io_rd(ad1816, reg) & ~AD1816_ENABLE);
433	    		/* disable playback */
434	    		if (io_rd(ad1816, reg) & AD1816_ENABLE)
435				printf("ad1816: failed to stop %s DMA!\n",
436				       wr? "play" : "rec");
437	    		ad1816_write(ad1816, wr? 8 : 10, 0); /* reset base cnt */
438	    		ad1816_write(ad1816, wr? 9 : 11, 0); /* reset cur cnt */
439		}
440		break;
441    	}
442    	return 0;
443}
444
445static int
446ad1816chan_getptr(void *data)
447{
448	struct ad1816_chinfo *ch = data;
449	return buf_isadmaptr(ch->buffer);
450}
451
452static pcmchan_caps *
453ad1816chan_getcaps(void *data)
454{
455	return &ad1816_caps;
456}
457
458static void
459ad1816_release_resources(struct ad1816_info *ad1816, device_t dev)
460{
461    	if (ad1816->irq) {
462		bus_release_resource(dev, SYS_RES_IRQ, ad1816->irq_rid,
463				     ad1816->irq);
464		ad1816->irq = 0;
465    	}
466    	if (ad1816->drq1) {
467		bus_release_resource(dev, SYS_RES_DRQ, ad1816->drq1_rid,
468				     ad1816->drq1);
469		ad1816->drq1 = 0;
470    	}
471    	if (ad1816->drq2) {
472		bus_release_resource(dev, SYS_RES_DRQ, ad1816->drq2_rid,
473				     ad1816->drq2);
474		ad1816->drq2 = 0;
475    	}
476    	if (ad1816->io_base) {
477		bus_release_resource(dev, SYS_RES_IOPORT, ad1816->io_rid,
478				     ad1816->io_base);
479		ad1816->io_base = 0;
480    	}
481    	free(ad1816, M_DEVBUF);
482}
483
484static int
485ad1816_alloc_resources(struct ad1816_info *ad1816, device_t dev)
486{
487    	int ok = 1, pdma, rdma;
488	if (!ad1816->io_base)
489    		ad1816->io_base = bus_alloc_resource(dev, SYS_RES_IOPORT, &ad1816->io_rid,
490						  0, ~0, 1, RF_ACTIVE);
491	if (!ad1816->irq)
492    		ad1816->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &ad1816->irq_rid,
493					      0, ~0, 1, RF_ACTIVE);
494	if (!ad1816->drq1)
495    		ad1816->drq1 = bus_alloc_resource(dev, SYS_RES_DRQ, &ad1816->drq1_rid,
496					       0, ~0, 1, RF_ACTIVE);
497    	if (!ad1816->drq2)
498        	ad1816->drq2 = bus_alloc_resource(dev, SYS_RES_DRQ, &ad1816->drq2_rid,
499					       0, ~0, 1, RF_ACTIVE);
500
501    	if (!ad1816->io_base || !ad1816->drq1 || !ad1816->irq) ok = 0;
502
503	if (ok) {
504		pdma = rman_get_start(ad1816->drq1);
505		isa_dma_acquire(pdma);
506		isa_dmainit(pdma, DSP_BUFFSIZE);
507		if (ad1816->drq2) {
508			rdma = rman_get_start(ad1816->drq2);
509			isa_dma_acquire(rdma);
510			isa_dmainit(rdma, DSP_BUFFSIZE);
511		} else rdma = pdma;
512    		if (pdma == rdma)
513			pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
514	}
515    	return ok;
516}
517
518static int
519ad1816_init(struct ad1816_info *ad1816, device_t dev)
520{
521    	ad1816_write(ad1816, 1, 0x2);	/* disable interrupts */
522    	ad1816_write(ad1816, 32, 0x90F0);	/* SoundSys Mode, split fmt */
523
524    	ad1816_write(ad1816, 5, 0x8080);	/* FM volume mute */
525    	ad1816_write(ad1816, 6, 0x8080);	/* I2S1 volume mute */
526    	ad1816_write(ad1816, 7, 0x8080);	/* I2S0 volume mute */
527    	ad1816_write(ad1816, 17, 0x8888);	/* VID Volume mute */
528    	ad1816_write(ad1816, 20, 0x5050);	/* recsrc mic, agc off */
529    	/* adc gain is set to 0 */
530
531	return 0;
532}
533
534static int
535ad1816_probe(device_t dev)
536{
537    	char *s = NULL;
538    	u_int32_t logical_id = isa_get_logicalid(dev);
539
540    	switch (logical_id) {
541    	case 0x80719304: /* ADS7180 */
542 		s = "AD1816";
543 		break;
544    	}
545
546    	if (s) {
547		device_set_desc(dev, s);
548		return 0;
549    	}
550    	return ENXIO;
551}
552
553static int
554ad1816_attach(device_t dev)
555{
556	struct ad1816_info *ad1816;
557    	snddev_info *d = device_get_softc(dev);
558    	void *ih;
559    	char status[SND_STATUSLEN];
560
561	ad1816 = (struct ad1816_info *)malloc(sizeof *ad1816, M_DEVBUF, M_NOWAIT);
562	if (!ad1816) return ENXIO;
563	bzero(ad1816, sizeof *ad1816);
564
565	ad1816->io_rid = 2;
566	ad1816->irq_rid = 0;
567	ad1816->drq1_rid = 0;
568	ad1816->drq2_rid = 1;
569
570    	if (!ad1816_alloc_resources(ad1816, dev)) goto no;
571    	ad1816_init(ad1816, dev);
572    	mixer_init(d, &ad1816_mixer, ad1816);
573	bus_setup_intr(dev, ad1816->irq, INTR_TYPE_TTY, ad1816_intr, ad1816, &ih);
574    	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
575			/*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
576			/*highaddr*/BUS_SPACE_MAXADDR,
577			/*filter*/NULL, /*filterarg*/NULL,
578			/*maxsize*/DSP_BUFFSIZE, /*nsegments*/1,
579			/*maxsegz*/0x3ffff,
580			/*flags*/0, &ad1816->parent_dmat) != 0) {
581		device_printf(dev, "unable to create dma tag\n");
582		goto no;
583    	}
584    	snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld drq %ld",
585    	     	rman_get_start(ad1816->io_base),
586		rman_get_start(ad1816->irq),
587		rman_get_start(ad1816->drq1));
588    	if (ad1816->drq2) snprintf(status + strlen(status),
589        	SND_STATUSLEN - strlen(status), ":%ld",
590		rman_get_start(ad1816->drq2));
591
592    	if (pcm_register(dev, ad1816, 1, 1)) goto no;
593    	pcm_addchan(dev, PCMDIR_REC, &ad1816_chantemplate, ad1816);
594    	pcm_addchan(dev, PCMDIR_PLAY, &ad1816_chantemplate, ad1816);
595    	pcm_setstatus(dev, status);
596
597    	return 0;
598no:
599    	ad1816_release_resources(ad1816, dev);
600    	return ENXIO;
601
602}
603
604static device_method_t ad1816_methods[] = {
605	/* Device interface */
606	DEVMETHOD(device_probe,		ad1816_probe),
607	DEVMETHOD(device_attach,	ad1816_attach),
608
609	{ 0, 0 }
610};
611
612static driver_t ad1816_driver = {
613	"pcm",
614	ad1816_methods,
615	sizeof(snddev_info),
616};
617
618DRIVER_MODULE(ad1816, isa, ad1816_driver, pcm_devclass, 0, 0);
619