xref: /netbsd/sys/arch/prep/isa/paud_isa.c (revision 6550d01e)
1 /*	$NetBSD: paud_isa.c,v 1.12 2008/04/28 20:23:33 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: paud_isa.c,v 1.12 2008/04/28 20:23:33 martin Exp $");
34 
35 #include "audio.h"
36 #if NAUDIO > 0
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/errno.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
43 
44 #include <machine/bus.h>
45 #include <machine/intr.h>
46 
47 #include <sys/audioio.h>
48 #include <dev/audio_if.h>
49 
50 #include <dev/isa/isavar.h>
51 #include <dev/isa/isadmavar.h>
52 
53 #include <dev/ic/ad1848reg.h>
54 #include <dev/ic/cs4231reg.h>
55 
56 #include <dev/isa/ad1848var.h>
57 #include <dev/isa/cs4231var.h>
58 
59 #define	PAUD_MIC_IN_LVL		0
60 #define	PAUD_LINE_IN_LVL	1
61 #define	PAUD_DAC_LVL		2
62 #define	PAUD_REC_LVL		3
63 #define	PAUD_MONITOR_LVL	4
64 #define	PAUD_MIC_IN_MUTE	5
65 #define	PAUD_LINE_IN_MUTE	6
66 #define	PAUD_DAC_MUTE		7
67 #define	PAUD_MONITOR_MUTE	8
68 #define	PAUD_RECORD_SOURCE	9
69 #define	PAUD_INPUT_CLASS	10
70 #define	PAUD_RECORD_CLASS	11
71 #define	PAUD_MONITOR_CLASS	12
72 
73 
74 /* autoconfiguration driver */
75 static void paud_attach_isa(struct device *, struct device *, void *);
76 static int  paud_match_isa(struct device *, struct cfdata *, void *);
77 
78 CFATTACH_DECL(paud_isa, sizeof(struct ad1848_isa_softc),
79     paud_match_isa, paud_attach_isa, NULL, NULL);
80 
81 /*
82  * Define our interface to the higher level audio driver.
83  */
84 static struct audio_device paud_device = {
85 	"paud,cs4232",
86 	"",
87 	""
88 };
89 
90 static int paud_getdev(void *, struct audio_device *);
91 static int paud_mixer_set_port(void *, mixer_ctrl_t *);
92 static int paud_mixer_get_port(void *, mixer_ctrl_t *);
93 static int paud_query_devinfo(void *, mixer_devinfo_t *);
94 
95 static const struct audio_hw_if paud_hw_if = {
96 	ad1848_isa_open,
97 	ad1848_isa_close,
98 	NULL,
99 	ad1848_query_encoding,
100 	ad1848_set_params,
101 	ad1848_round_blocksize,
102 	ad1848_commit_settings,
103 	NULL,
104 	NULL,
105 	NULL,
106 	NULL,
107 	ad1848_isa_halt_output,
108 	ad1848_isa_halt_input,
109 	NULL,
110 	paud_getdev,
111 	NULL,
112 	paud_mixer_set_port,
113 	paud_mixer_get_port,
114 	paud_query_devinfo,
115 	ad1848_isa_malloc,
116 	ad1848_isa_free,
117 	ad1848_isa_round_buffersize,
118 	ad1848_isa_mappage,
119 	ad1848_isa_get_props,
120 	ad1848_isa_trigger_output,
121 	ad1848_isa_trigger_input,
122 	NULL,
123 };
124 
125 /* autoconfig routines */
126 
127 static int
128 paud_match_isa(struct device *parent, struct cfdata *cf, void *aux)
129 {
130 	struct ad1848_isa_softc probesc, *sc = &probesc;
131 	struct isa_attach_args *ia = aux;
132 
133 	if (ia->ia_io[0].ir_addr != 0x830)
134 		return 0;
135 
136 	sc->sc_ad1848.sc_iot = ia->ia_iot;
137 	sc->sc_ic = ia->ia_ic;
138 	if (ad1848_isa_mapprobe(sc, ia->ia_io[0].ir_addr)) {
139 		ia->ia_io[0].ir_size = AD1848_NPORT;
140 		ad1848_isa_unmap(sc);
141 		return 1;
142 	}
143 	return 0;
144 }
145 
146 /*
147  * Audio chip found.
148  */
149 static void
150 paud_attach_isa(struct device *parent, struct device *self, void *aux)
151 {
152 	struct ad1848_isa_softc *sc;
153 	struct isa_attach_args *ia;
154 
155 	sc = (struct ad1848_isa_softc *)self;
156 	ia = aux;
157 	sc->sc_ad1848.sc_iot = ia->ia_iot;
158 	sc->sc_ic = ia->ia_ic;
159 
160 	if (ad1848_isa_mapprobe(sc, ia->ia_io[0].ir_addr) == 0) {
161 		aprint_error(": attach failed\n");
162 		return;
163 	}
164 	sc->sc_playdrq = ia->ia_drq[0].ir_drq;
165 	sc->sc_recdrq = ia->ia_drq[1].ir_drq;
166 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
167 	    IST_EDGE, IPL_AUDIO, ad1848_isa_intr, sc);
168 	ad1848_isa_attach(sc);
169 	aprint_normal("\n");
170 	audio_attach_mi(&paud_hw_if, &sc->sc_ad1848, &sc->sc_ad1848.sc_dev);
171 
172 }
173 
174 static int
175 paud_getdev(void *addr, struct audio_device *retp)
176 {
177 
178 	*retp = paud_device;
179 	return 0;
180 }
181 
182 static ad1848_devmap_t mappings[] = {
183 	{ PAUD_MIC_IN_LVL, AD1848_KIND_LVL, AD1848_AUX2_CHANNEL },
184 	{ PAUD_LINE_IN_LVL, AD1848_KIND_LVL, AD1848_AUX1_CHANNEL },
185 	{ PAUD_DAC_LVL, AD1848_KIND_LVL, AD1848_DAC_CHANNEL },
186 	{ PAUD_MONITOR_LVL, AD1848_KIND_LVL, AD1848_MONO_CHANNEL },
187 	{ PAUD_MIC_IN_MUTE, AD1848_KIND_MUTE, AD1848_AUX2_CHANNEL },
188 	{ PAUD_LINE_IN_MUTE, AD1848_KIND_MUTE, AD1848_AUX1_CHANNEL },
189 	{ PAUD_DAC_MUTE, AD1848_KIND_MUTE, AD1848_DAC_CHANNEL },
190 	{ PAUD_MONITOR_MUTE, AD1848_KIND_MUTE, AD1848_MONO_CHANNEL },
191 	{ PAUD_REC_LVL, AD1848_KIND_RECORDGAIN, -1 },
192 	{ PAUD_RECORD_SOURCE, AD1848_KIND_RECORDSOURCE, -1}
193 };
194 
195 static const int nummap = sizeof(mappings) / sizeof(mappings[0]);
196 
197 static int
198 paud_mixer_set_port(void *addr, mixer_ctrl_t *cp)
199 {
200 	struct ad1848_softc *ac;
201 
202 	ac = addr;
203 	return ad1848_mixer_set_port(ac, mappings, nummap, cp);
204 }
205 
206 static int
207 paud_mixer_get_port(void *addr, mixer_ctrl_t *cp)
208 {
209 	struct ad1848_softc *ac;
210 
211 	ac = addr;
212 	return ad1848_mixer_get_port(ac, mappings, nummap, cp);
213 }
214 
215 static int
216 paud_query_devinfo(void *addr, mixer_devinfo_t *dip)
217 {
218 
219 	switch(dip->index) {
220 	case PAUD_MIC_IN_LVL:	/* Microphone */
221 		dip->type = AUDIO_MIXER_VALUE;
222 		dip->mixer_class = PAUD_INPUT_CLASS;
223 		dip->prev = AUDIO_MIXER_LAST;
224 		dip->next = PAUD_MIC_IN_MUTE;
225 		strcpy(dip->label.name, AudioNmicrophone);
226 		dip->un.v.num_channels = 2;
227 		strcpy(dip->un.v.units.name, AudioNvolume);
228 		break;
229 
230 	case PAUD_LINE_IN_LVL:	/* line/CD */
231 		dip->type = AUDIO_MIXER_VALUE;
232 		dip->mixer_class = PAUD_INPUT_CLASS;
233 		dip->prev = AUDIO_MIXER_LAST;
234 		dip->next = PAUD_LINE_IN_MUTE;
235 		strcpy(dip->label.name, AudioNcd);
236 		dip->un.v.num_channels = 2;
237 		strcpy(dip->un.v.units.name, AudioNvolume);
238 		break;
239 
240 	case PAUD_DAC_LVL:		/*  dacout */
241 		dip->type = AUDIO_MIXER_VALUE;
242 		dip->mixer_class = PAUD_INPUT_CLASS;
243 		dip->prev = AUDIO_MIXER_LAST;
244 		dip->next = PAUD_DAC_MUTE;
245 		strcpy(dip->label.name, AudioNdac);
246 		dip->un.v.num_channels = 2;
247 		strcpy(dip->un.v.units.name, AudioNvolume);
248 		break;
249 
250 	case PAUD_REC_LVL:	/* record level */
251 		dip->type = AUDIO_MIXER_VALUE;
252 		dip->mixer_class = PAUD_RECORD_CLASS;
253 		dip->prev = AUDIO_MIXER_LAST;
254 		dip->next = PAUD_RECORD_SOURCE;
255 		strcpy(dip->label.name, AudioNrecord);
256 		dip->un.v.num_channels = 2;
257 		strcpy(dip->un.v.units.name, AudioNvolume);
258 		break;
259 
260 	case PAUD_MONITOR_LVL:	/* monitor level */
261 		dip->type = AUDIO_MIXER_VALUE;
262 		dip->mixer_class = PAUD_MONITOR_CLASS;
263 		dip->prev = AUDIO_MIXER_LAST;
264 		dip->next = PAUD_MONITOR_MUTE;
265 		strcpy(dip->label.name, AudioNmonitor);
266 		dip->un.v.num_channels = 1;
267 		strcpy(dip->un.v.units.name, AudioNvolume);
268 		break;
269 
270 	case PAUD_INPUT_CLASS:			/* input class descriptor */
271 		dip->type = AUDIO_MIXER_CLASS;
272 		dip->mixer_class = PAUD_INPUT_CLASS;
273 		dip->next = dip->prev = AUDIO_MIXER_LAST;
274 		strcpy(dip->label.name, AudioCinputs);
275 		break;
276 
277 	case PAUD_MONITOR_CLASS:			/* monitor class descriptor */
278 		dip->type = AUDIO_MIXER_CLASS;
279 		dip->mixer_class = PAUD_MONITOR_CLASS;
280 		dip->next = dip->prev = AUDIO_MIXER_LAST;
281 		strcpy(dip->label.name, AudioCmonitor);
282 		break;
283 
284 	case PAUD_RECORD_CLASS:			/* record source class */
285 		dip->type = AUDIO_MIXER_CLASS;
286 		dip->mixer_class = PAUD_RECORD_CLASS;
287 		dip->next = dip->prev = AUDIO_MIXER_LAST;
288 		strcpy(dip->label.name, AudioCrecord);
289 		break;
290 
291 	case PAUD_MIC_IN_MUTE:
292 		dip->mixer_class = PAUD_INPUT_CLASS;
293 		dip->type = AUDIO_MIXER_ENUM;
294 		dip->prev = PAUD_MIC_IN_LVL;
295 		dip->next = AUDIO_MIXER_LAST;
296 		goto mute;
297 
298 	case PAUD_LINE_IN_MUTE:
299 		dip->mixer_class = PAUD_INPUT_CLASS;
300 		dip->type = AUDIO_MIXER_ENUM;
301 		dip->prev = PAUD_LINE_IN_LVL;
302 		dip->next = AUDIO_MIXER_LAST;
303 		goto mute;
304 
305 	case PAUD_DAC_MUTE:
306 		dip->mixer_class = PAUD_INPUT_CLASS;
307 		dip->type = AUDIO_MIXER_ENUM;
308 		dip->prev = PAUD_DAC_LVL;
309 		dip->next = AUDIO_MIXER_LAST;
310 		goto mute;
311 
312 	case PAUD_MONITOR_MUTE:
313 		dip->mixer_class = PAUD_MONITOR_CLASS;
314 		dip->type = AUDIO_MIXER_ENUM;
315 		dip->prev = PAUD_MONITOR_LVL;
316 		dip->next = AUDIO_MIXER_LAST;
317 	mute:
318 		strcpy(dip->label.name, AudioNmute);
319 		dip->un.e.num_mem = 2;
320 		strcpy(dip->un.e.member[0].label.name, AudioNoff);
321 		dip->un.e.member[0].ord = 0;
322 		strcpy(dip->un.e.member[1].label.name, AudioNon);
323 		dip->un.e.member[1].ord = 1;
324 		break;
325 
326 	case PAUD_RECORD_SOURCE:
327 		dip->mixer_class = PAUD_RECORD_CLASS;
328 		dip->type = AUDIO_MIXER_ENUM;
329 		dip->prev = PAUD_REC_LVL;
330 		dip->next = AUDIO_MIXER_LAST;
331 		strcpy(dip->label.name, AudioNsource);
332 		dip->un.e.num_mem = 3;
333 		strcpy(dip->un.e.member[0].label.name, AudioNmicrophone);
334 		dip->un.e.member[0].ord = PAUD_MIC_IN_LVL;
335 		strcpy(dip->un.e.member[1].label.name, AudioNcd);
336 		dip->un.e.member[1].ord = PAUD_LINE_IN_LVL;
337 		strcpy(dip->un.e.member[2].label.name, AudioNdac);
338 		dip->un.e.member[2].ord = PAUD_DAC_LVL;
339 		break;
340 
341 	default:
342 		return ENXIO;
343 		/*NOTREACHED*/
344 	}
345 
346 	return 0;
347 }
348 
349 #endif
350