es137x.c revision 50724
1/*
2 * Support the ENSONIQ AudioPCI board based on the ES1370 and Codec
3 * AK4531.
4 *
5 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
6 * Copyright (c) 1998 by Joachim Kuebart. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in
17 *    the documentation and/or other materials provided with the
18 *    distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 *    software must display the following acknowledgement:
22 *	This product includes software developed by Joachim Kuebart.
23 *
24 * 4. The name of the author may not be used to endorse or promote
25 *    products derived from this software without specific prior
26 *    written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 * DISCLAIMED.	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
32 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
38 * OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 *	$Id: es1370.c,v 1.4 1999/05/09 17:06:45 peter Exp $
41 */
42
43#include "pci.h"
44#include "pcm.h"
45
46#include <dev/pcm/sound.h>
47#include <dev/pcm/pci/es1370.h>
48
49#include <pci/pcireg.h>
50#include <pci/pcivar.h>
51
52#if NPCI != 0
53
54#define MEM_MAP_REG 0x14
55
56/* PCI IDs of supported chips */
57#define ES1370_PCI_ID 0x50001274
58
59/* device private data */
60struct es_info;
61
62struct es_chinfo {
63	struct es_info *parent;
64	pcm_channel *channel;
65	snd_dbuf *buffer;
66	int dir;
67	u_int32_t fmt;
68};
69
70struct es_info {
71	bus_space_tag_t st;
72	bus_space_handle_t sh;
73	bus_dma_tag_t	parent_dmat;
74
75	/* Contents of board's registers */
76	u_long		ctrl;
77	u_long		sctrl;
78	struct es_chinfo pch, rch;
79};
80
81/* -------------------------------------------------------------------- */
82
83/* prototypes */
84static int      es_init(struct es_info *);
85static void     es_intr(void *);
86static int      write_codec(struct es_info *, u_char, u_char);
87
88/* channel interface */
89static void *eschan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir);
90static int eschan_setdir(void *data, int dir);
91static int eschan_setformat(void *data, u_int32_t format);
92static int eschan_setspeed(void *data, u_int32_t speed);
93static int eschan_setblocksize(void *data, u_int32_t blocksize);
94static int eschan_trigger(void *data, int go);
95static int eschan_getptr(void *data);
96static pcmchan_caps *eschan_getcaps(void *data);
97
98static pcmchan_caps es_playcaps = {
99	4000, 48000,
100	AFMT_STEREO | AFMT_U8 | AFMT_S16_LE,
101	AFMT_STEREO | AFMT_S16_LE
102};
103
104static pcmchan_caps es_reccaps = {
105	4000, 48000,
106	AFMT_STEREO | AFMT_U8 | AFMT_S16_LE,
107	AFMT_STEREO | AFMT_S16_LE
108};
109
110static pcm_channel es_chantemplate = {
111	eschan_init,
112	eschan_setdir,
113	eschan_setformat,
114	eschan_setspeed,
115	eschan_setblocksize,
116	eschan_trigger,
117	eschan_getptr,
118	eschan_getcaps,
119};
120
121/* -------------------------------------------------------------------- */
122
123/* The mixer interface */
124
125static int es_mixinit(snd_mixer *m);
126static int es_mixset(snd_mixer *m, unsigned dev, unsigned left, unsigned right);
127static int es_mixsetrecsrc(snd_mixer *m, u_int32_t src);
128
129static snd_mixer es_mixer = {
130	"Ensoniq AudioPCI 1370 mixer",
131	es_mixinit,
132	es_mixset,
133	es_mixsetrecsrc,
134};
135
136static const struct {
137	unsigned        volidx:4;
138	unsigned        left:4;
139	unsigned        right:4;
140	unsigned        stereo:1;
141	unsigned        recmask:13;
142	unsigned        avail:1;
143}       mixtable[SOUND_MIXER_NRDEVICES] = {
144	[SOUND_MIXER_VOLUME]	= { 0, 0x0, 0x1, 1, 0x0000, 1 },
145	[SOUND_MIXER_PCM] 	= { 1, 0x2, 0x3, 1, 0x0400, 1 },
146	[SOUND_MIXER_SYNTH]	= { 2, 0x4, 0x5, 1, 0x0060, 1 },
147	[SOUND_MIXER_CD]	= { 3, 0x6, 0x7, 1, 0x0006, 1 },
148	[SOUND_MIXER_LINE]	= { 4, 0x8, 0x9, 1, 0x0018, 1 },
149	[SOUND_MIXER_LINE1]	= { 5, 0xa, 0xb, 1, 0x1800, 1 },
150	[SOUND_MIXER_LINE2]	= { 6, 0xc, 0x0, 0, 0x0100, 1 },
151	[SOUND_MIXER_LINE3]	= { 7, 0xd, 0x0, 0, 0x0200, 1 },
152	[SOUND_MIXER_MIC]	= { 8, 0xe, 0x0, 0, 0x0001, 1 },
153	[SOUND_MIXER_OGAIN]	= { 9, 0xf, 0x0, 0, 0x0000, 1 } };
154
155static int
156es_mixinit(snd_mixer *m)
157{
158	int i;
159	u_int32_t v;
160
161	v = 0;
162	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
163		if (mixtable[i].avail) v |= (1 << i);
164	mix_setdevs(m, v);
165	v = 0;
166	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
167		if (mixtable[i].recmask) v |= (1 << i);
168	mix_setrecdevs(m, v);
169	return 0;
170}
171
172static int
173es_mixset(snd_mixer *m, unsigned dev, unsigned left, unsigned right)
174{
175	int l, r, rl, rr;
176
177	if (!mixtable[dev].avail) return -1;
178	l = left;
179	r = mixtable[dev].stereo? right : l;
180	if (mixtable[dev].left == 0xf) {
181		rl = (l < 2)? 0x80 : 7 - (l - 2) / 14;
182	} else {
183		rl = (l < 10)? 0x80 : 15 - (l - 10) / 6;
184	}
185	if (mixtable[dev].stereo) {
186		rr = (r < 10)? 0x80 : 15 - (r - 10) / 6;
187		write_codec(mix_getdevinfo(m), mixtable[dev].right, rr);
188	}
189	write_codec(mix_getdevinfo(m), mixtable[dev].left, rl);
190	return l | (r << 8);
191}
192
193static int
194es_mixsetrecsrc(snd_mixer *m, u_int32_t src)
195{
196	int i, j = 0;
197
198	if (src == 0) src = 1 << SOUND_MIXER_MIC;
199	src &= mix_getrecdevs(m);
200	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
201		if ((src & (1 << i)) != 0) j |= mixtable[i].recmask;
202
203	write_codec(mix_getdevinfo(m), CODEC_LIMIX1, j & 0x55);
204	write_codec(mix_getdevinfo(m), CODEC_RIMIX1, j & 0xaa);
205	write_codec(mix_getdevinfo(m), CODEC_LIMIX2, (j >> 8) & 0x17);
206	write_codec(mix_getdevinfo(m), CODEC_RIMIX2, (j >> 8) & 0x0f);
207	write_codec(mix_getdevinfo(m), CODEC_OMIX1, 0x7f);
208	write_codec(mix_getdevinfo(m), CODEC_OMIX2, 0x3f);
209	return src;
210}
211
212static int
213write_codec(struct es_info *es, u_char i, u_char data)
214{
215	int		wait = 100;	/* 100 msec timeout */
216
217	do {
218		if ((bus_space_read_4(es->st, es->sh, ES1370_REG_STATUS) &
219		      STAT_CSTAT) == 0) {
220			bus_space_write_2(es->st, es->sh, ES1370_REG_CODEC,
221				((u_short)i << CODEC_INDEX_SHIFT) | data);
222			return 0;
223		}
224		DELAY(1000);
225	} while (--wait);
226	printf("pcm: write_codec timed out\n");
227	return -1;
228}
229
230/* -------------------------------------------------------------------- */
231
232/* channel interface */
233static void *
234eschan_init(void *devinfo, snd_dbuf *b, pcm_channel *c, int dir)
235{
236	struct es_info *es = devinfo;
237	struct es_chinfo *ch = (dir == PCMDIR_PLAY)? &es->pch : &es->rch;
238
239	ch->parent = es;
240	ch->channel = c;
241	ch->buffer = b;
242	ch->buffer->bufsize = ES_BUFFSIZE;
243	if (chn_allocbuf(ch->buffer, es->parent_dmat) == -1) return NULL;
244	return ch;
245}
246
247static int
248eschan_setdir(void *data, int dir)
249{
250	struct es_chinfo *ch = data;
251	struct es_info *es = ch->parent;
252
253	if (dir == PCMDIR_PLAY) {
254		bus_space_write_1(es->st, es->sh, ES1370_REG_MEMPAGE,
255				  ES1370_REG_DAC2_FRAMEADR >> 8);
256		bus_space_write_4(es->st, es->sh, ES1370_REG_DAC2_FRAMEADR & 0xff,
257				  vtophys(ch->buffer->buf));
258		bus_space_write_4(es->st, es->sh, ES1370_REG_DAC2_FRAMECNT & 0xff,
259				  (ch->buffer->bufsize >> 2) - 1);
260	} else {
261		bus_space_write_1(es->st, es->sh, ES1370_REG_MEMPAGE,
262				  ES1370_REG_ADC_FRAMEADR >> 8);
263		bus_space_write_4(es->st, es->sh, ES1370_REG_ADC_FRAMEADR & 0xff,
264				  vtophys(ch->buffer->buf));
265		bus_space_write_4(es->st, es->sh, ES1370_REG_ADC_FRAMECNT & 0xff,
266				  (ch->buffer->bufsize >> 2) - 1);
267	}
268	ch->dir = dir;
269	return 0;
270}
271
272static int
273eschan_setformat(void *data, u_int32_t format)
274{
275	struct es_chinfo *ch = data;
276	struct es_info *es = ch->parent;
277
278	if (ch->dir == PCMDIR_PLAY) {
279		es->sctrl &= ~SCTRL_P2FMT;
280		if (format & AFMT_S16_LE) es->sctrl |= SCTRL_P2SEB;
281		if (format & AFMT_STEREO) es->sctrl |= SCTRL_P2SMB;
282	} else {
283		es->sctrl &= ~SCTRL_R1FMT;
284		if (format & AFMT_S16_LE) es->sctrl |= SCTRL_R1SEB;
285		if (format & AFMT_STEREO) es->sctrl |= SCTRL_R1SMB;
286	}
287	bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl);
288	ch->fmt = format;
289	return 0;
290}
291
292static int
293eschan_setspeed(void *data, u_int32_t speed)
294{
295	struct es_chinfo *ch = data;
296	struct es_info *es = ch->parent;
297
298	es->ctrl &= ~CTRL_PCLKDIV;
299	es->ctrl |= DAC2_SRTODIV(speed) << CTRL_SH_PCLKDIV;
300	bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl);
301	/* rec/play speeds locked together - should indicate in flags */
302#if 0
303	if (ch->direction == PCMDIR_PLAY) d->rec[0].speed = speed;
304	else d->play[0].speed = speed;
305#endif
306	return speed; /* XXX calc real speed */
307}
308
309static int
310eschan_setblocksize(void *data, u_int32_t blocksize)
311{
312	return blocksize;
313}
314
315static int
316eschan_trigger(void *data, int go)
317{
318	struct es_chinfo *ch = data;
319	struct es_info *es = ch->parent;
320	unsigned cnt = ch->buffer->dl / ch->buffer->sample_size - 1;
321
322	if (ch->dir == PCMDIR_PLAY) {
323		if (go == PCMTRIG_START) {
324			int b = (ch->fmt & AFMT_S16_LE)? 2 : 1;
325			es->ctrl |= CTRL_DAC2_EN;
326			es->sctrl &= ~(SCTRL_P2ENDINC | SCTRL_P2STINC |
327				       SCTRL_P2LOOPSEL | SCTRL_P2PAUSE |
328				       SCTRL_P2DACSEN);
329			es->sctrl |= SCTRL_P2INTEN | (b << SCTRL_SH_P2ENDINC);
330			bus_space_write_4(es->st, es->sh,
331					  ES1370_REG_DAC2_SCOUNT, cnt);
332		} else es->ctrl &= ~CTRL_DAC2_EN;
333	} else {
334		if (go == PCMTRIG_START) {
335			es->ctrl |= CTRL_ADC_EN;
336			es->sctrl &= ~SCTRL_R1LOOPSEL;
337			es->sctrl |= SCTRL_R1INTEN;
338			bus_space_write_4(es->st, es->sh,
339					  ES1370_REG_ADC_SCOUNT, cnt);
340		} else es->ctrl &= ~CTRL_ADC_EN;
341	}
342	bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl);
343	bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl);
344	return 0;
345}
346
347static int
348eschan_getptr(void *data)
349{
350	struct es_chinfo *ch = data;
351	struct es_info *es = ch->parent;
352	if (ch->dir == PCMDIR_PLAY) {
353		bus_space_write_4(es->st, es->sh, ES1370_REG_MEMPAGE,
354				  ES1370_REG_DAC2_FRAMECNT >> 8);
355		return (bus_space_read_4(es->st, es->sh,
356				         ES1370_REG_DAC2_FRAMECNT & 0xff) >> 14) & 0x3fffc;
357	} else {
358		bus_space_write_4(es->st, es->sh, ES1370_REG_MEMPAGE,
359				  ES1370_REG_ADC_FRAMECNT >> 8);
360		return (bus_space_read_4(es->st, es->sh,
361				         ES1370_REG_ADC_FRAMECNT & 0xff) >> 14) & 0x3fffc;
362	}
363}
364
365static pcmchan_caps *
366eschan_getcaps(void *data)
367{
368	struct es_chinfo *ch = data;
369	return (ch->dir == PCMDIR_PLAY)? &es_playcaps : &es_reccaps;
370}
371
372/* The interrupt handler */
373static void
374es_intr (void *p)
375{
376	struct es_info *es = p;
377	unsigned	intsrc, sctrl;
378
379	intsrc = bus_space_read_4(es->st, es->sh, ES1370_REG_STATUS);
380	if ((intsrc & STAT_INTR) == 0) return;
381
382	sctrl = es->sctrl;
383	if (intsrc & STAT_ADC)  sctrl &= ~SCTRL_R1INTEN;
384	if (intsrc & STAT_DAC1)	sctrl &= ~SCTRL_P1INTEN;
385	if (intsrc & STAT_DAC2)	sctrl &= ~SCTRL_P2INTEN;
386
387	bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, sctrl);
388	bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl);
389
390	if (intsrc & STAT_DAC2)	chn_intr(es->pch.channel);
391	if (intsrc & STAT_ADC) chn_intr(es->rch.channel);
392}
393
394/* -------------------------------------------------------------------- */
395
396/*
397 * Probe and attach the card
398 */
399
400static int
401es_init(struct es_info *es)
402{
403	es->ctrl = CTRL_CDC_EN | CTRL_SERR_DIS |
404		(DAC2_SRTODIV(DSP_DEFAULT_SPEED) << CTRL_SH_PCLKDIV);
405	bus_space_write_4(es->st, es->sh, ES1370_REG_CONTROL, es->ctrl);
406
407	es->sctrl = 0;
408	bus_space_write_4(es->st, es->sh, ES1370_REG_SERIAL_CONTROL, es->sctrl);
409
410	write_codec(es, CODEC_RES_PD, 3);/* No RST, PD */
411	write_codec(es, CODEC_CSEL, 0);	/* CODEC ADC and CODEC DAC use
412					 * {LR,B}CLK2 and run off the LRCLK2
413					 * PLL; program DAC_SYNC=0!  */
414	write_codec(es, CODEC_ADSEL, 0);/* Recording source is mixer */
415	write_codec(es, CODEC_MGAIN, 0);/* MIC amp is 0db */
416
417	return 0;
418}
419
420static int
421es_pci_probe(device_t dev)
422{
423	if (pci_get_devid(dev) == ES1370_PCI_ID) {
424		device_set_desc(dev, "AudioPCI ES1370");
425		return 0;
426	}
427	return ENXIO;
428}
429
430static int
431es_pci_attach(device_t dev)
432{
433	snddev_info    *d;
434	u_int32_t	data;
435	struct es_info *es = 0;
436	int		type = 0;
437	int		regid;
438	struct resource *reg = 0;
439	int		mapped;
440	int		irqid;
441	struct resource *irq = 0;
442	void		*ih = 0;
443	char		status[SND_STATUSLEN];
444
445	d = device_get_softc(dev);
446	if ((es = malloc(sizeof *es, M_DEVBUF, M_NOWAIT)) == NULL) {
447		device_printf(dev, "cannot allocate softc\n");
448		return ENXIO;
449	}
450	bzero(es, sizeof *es);
451
452	mapped = 0;
453	data = pci_read_config(dev, PCIR_COMMAND, 2);
454	if (mapped == 0 && (data & PCIM_CMD_MEMEN)) {
455		regid = MEM_MAP_REG;
456		type = SYS_RES_MEMORY;
457		reg = bus_alloc_resource(dev, type, &regid,
458					 0, ~0, 1, RF_ACTIVE);
459		if (reg) {
460			es->st = rman_get_bustag(reg);
461			es->sh = rman_get_bushandle(reg);
462			mapped++;
463		}
464	}
465	if (mapped == 0 && (data & PCIM_CMD_PORTEN)) {
466		regid = PCI_MAP_REG_START;
467		type = SYS_RES_IOPORT;
468		reg = bus_alloc_resource(dev, type, &regid,
469					 0, ~0, 1, RF_ACTIVE);
470		if (reg) {
471			es->st = rman_get_bustag(reg);
472			es->sh = rman_get_bushandle(reg);
473			mapped++;
474		}
475	}
476	if (mapped == 0) {
477		device_printf(dev, "unable to map register space\n");
478		goto bad;
479	}
480
481	if (es_init(es) == -1) {
482		device_printf(dev, "unable to initialize the card\n");
483		goto bad;
484	}
485	mixer_init(d, &es_mixer, es);
486
487	irqid = 0;
488	irq = bus_alloc_resource(dev, SYS_RES_IRQ, &irqid,
489				 0, ~0, 1, RF_ACTIVE);
490	if (!irq
491	    || bus_setup_intr(dev, irq, INTR_TYPE_TTY, es_intr, es, &ih)) {
492		device_printf(dev, "unable to map interrupt\n");
493		goto bad;
494	}
495
496	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
497		/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
498		/*highaddr*/BUS_SPACE_MAXADDR,
499		/*filter*/NULL, /*filterarg*/NULL,
500		/*maxsize*/ES_BUFFSIZE, /*nsegments*/1, /*maxsegz*/0x3ffff,
501		/*flags*/0, &es->parent_dmat) != 0) {
502		device_printf(dev, "unable to create dma tag\n");
503		goto bad;
504	}
505
506	snprintf(status, SND_STATUSLEN, "at %s 0x%lx irq %ld",
507		 (type == SYS_RES_IOPORT)? "io" : "memory",
508		 rman_get_start(reg), rman_get_start(irq));
509
510	if (pcm_register(dev, es, 1, 1)) goto bad;
511	pcm_addchan(dev, PCMDIR_REC, &es_chantemplate, es);
512	pcm_addchan(dev, PCMDIR_PLAY, &es_chantemplate, es);
513	pcm_setstatus(dev, status);
514
515	return 0;
516
517 bad:
518	if (es) free(es, M_DEVBUF);
519	if (reg) bus_release_resource(dev, type, regid, reg);
520	if (ih) bus_teardown_intr(dev, irq, ih);
521	if (irq) bus_release_resource(dev, SYS_RES_IRQ, irqid, irq);
522	return ENXIO;
523}
524
525static device_method_t es_methods[] = {
526	/* Device interface */
527	DEVMETHOD(device_probe,		es_pci_probe),
528	DEVMETHOD(device_attach,	es_pci_attach),
529
530	{ 0, 0 }
531};
532
533static driver_t es_driver = {
534	"pcm",
535	es_methods,
536	sizeof(snddev_info),
537};
538
539static devclass_t pcm_devclass;
540
541DRIVER_MODULE(es, pci, es_driver, pcm_devclass, 0, 0);
542
543#endif /* NPCI != 0 */
544