Home | History | Annotate | Line # | Download | only in dev
snapper.c revision 1.8.2.1
      1  1.8.2.1      yamt /*	$NetBSD: snapper.c,v 1.8.2.1 2006/03/01 09:27:58 yamt Exp $	*/
      2      1.1     grant /*	Id: snapper.c,v 1.11 2002/10/31 17:42:13 tsubai Exp	*/
      3      1.1     grant 
      4      1.1     grant /*-
      5      1.1     grant  * Copyright (c) 2002 Tsubai Masanari.  All rights reserved.
      6      1.1     grant  *
      7      1.1     grant  * Redistribution and use in source and binary forms, with or without
      8      1.1     grant  * modification, are permitted provided that the following conditions
      9      1.1     grant  * are met:
     10      1.1     grant  * 1. Redistributions of source code must retain the above copyright
     11      1.1     grant  *    notice, this list of conditions and the following disclaimer.
     12      1.1     grant  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1     grant  *    notice, this list of conditions and the following disclaimer in the
     14      1.1     grant  *    documentation and/or other materials provided with the distribution.
     15      1.1     grant  * 3. The name of the author may not be used to endorse or promote products
     16      1.1     grant  *    derived from this software without specific prior written permission.
     17      1.1     grant  *
     18      1.1     grant  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19      1.1     grant  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20      1.1     grant  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21      1.1     grant  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22      1.1     grant  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23      1.1     grant  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24      1.1     grant  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25      1.1     grant  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26      1.1     grant  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27      1.1     grant  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28      1.1     grant  */
     29      1.1     grant 
     30      1.1     grant /*
     31      1.1     grant  * Datasheet is available from
     32      1.1     grant  * http://www.ti.com/sc/docs/products/analog/tas3004.html
     33      1.1     grant  */
     34      1.1     grant 
     35      1.1     grant #include <sys/param.h>
     36      1.1     grant #include <sys/audioio.h>
     37      1.1     grant #include <sys/device.h>
     38      1.1     grant #include <sys/systm.h>
     39      1.1     grant 
     40      1.1     grant #include <dev/auconv.h>
     41      1.1     grant #include <dev/audio_if.h>
     42      1.1     grant #include <dev/mulaw.h>
     43      1.1     grant #include <dev/ofw/openfirm.h>
     44      1.1     grant #include <macppc/dev/dbdma.h>
     45      1.1     grant 
     46      1.1     grant #include <uvm/uvm_extern.h>
     47      1.6  macallan #include <dev/i2c/i2cvar.h>
     48      1.1     grant 
     49      1.1     grant #include <machine/autoconf.h>
     50      1.1     grant #include <machine/pio.h>
     51      1.1     grant 
     52      1.6  macallan #include <macppc/dev/deqvar.h>
     53      1.6  macallan 
     54      1.1     grant #ifdef SNAPPER_DEBUG
     55      1.1     grant # define DPRINTF printf
     56      1.1     grant #else
     57      1.1     grant # define DPRINTF while (0) printf
     58      1.1     grant #endif
     59      1.1     grant 
     60      1.1     grant struct snapper_softc {
     61      1.1     grant 	struct device sc_dev;
     62      1.1     grant 	int sc_flags;
     63      1.1     grant 	int sc_node;
     64      1.1     grant 
     65      1.1     grant 	void (*sc_ointr)(void *);	/* dma completion intr handler */
     66      1.1     grant 	void *sc_oarg;			/* arg for sc_ointr() */
     67      1.1     grant 	int sc_opages;			/* # of output pages */
     68      1.1     grant 
     69      1.1     grant 	void (*sc_iintr)(void *);	/* dma completion intr handler */
     70      1.1     grant 	void *sc_iarg;			/* arg for sc_iintr() */
     71      1.1     grant 
     72      1.1     grant 	u_int sc_record_source;		/* recording source mask */
     73      1.1     grant 	u_int sc_output_mask;		/* output source mask */
     74      1.1     grant 
     75      1.1     grant 	u_char *sc_reg;
     76      1.6  macallan 	i2c_addr_t sc_deqaddr;
     77      1.6  macallan 	i2c_tag_t sc_i2c;
     78      1.1     grant 
     79      1.1     grant 	u_int sc_vol_l;
     80      1.1     grant 	u_int sc_vol_r;
     81      1.6  macallan 	u_int sc_treble;
     82      1.6  macallan 	u_int sc_bass;
     83      1.6  macallan 	u_int mixer[6]; /* s1_l, s2_l, an_l, s1_r, s2_r, an_r */
     84      1.1     grant 
     85      1.1     grant 	dbdma_regmap_t *sc_odma;
     86      1.1     grant 	dbdma_regmap_t *sc_idma;
     87      1.5    briggs 	unsigned char	dbdma_cmdspace[sizeof(struct dbdma_command) * 40 + 15];
     88      1.5    briggs 	struct dbdma_command *sc_odmacmd;
     89      1.5    briggs 	struct dbdma_command *sc_idmacmd;
     90      1.1     grant };
     91      1.1     grant 
     92      1.1     grant int snapper_match(struct device *, struct cfdata *, void *);
     93      1.1     grant void snapper_attach(struct device *, struct device *, void *);
     94      1.1     grant void snapper_defer(struct device *);
     95      1.1     grant int snapper_intr(void *);
     96      1.1     grant void snapper_close(void *);
     97      1.1     grant int snapper_query_encoding(void *, struct audio_encoding *);
     98      1.3      kent int snapper_set_params(void *, int, int, audio_params_t *,
     99      1.3      kent     audio_params_t *, stream_filter_list_t *, stream_filter_list_t *);
    100      1.3      kent int snapper_round_blocksize(void *, int, int, const audio_params_t *);
    101      1.1     grant int snapper_halt_output(void *);
    102      1.1     grant int snapper_halt_input(void *);
    103      1.1     grant int snapper_getdev(void *, struct audio_device *);
    104      1.1     grant int snapper_set_port(void *, mixer_ctrl_t *);
    105      1.1     grant int snapper_get_port(void *, mixer_ctrl_t *);
    106      1.1     grant int snapper_query_devinfo(void *, mixer_devinfo_t *);
    107      1.1     grant size_t snapper_round_buffersize(void *, int, size_t);
    108      1.1     grant paddr_t snapper_mappage(void *, void *, off_t, int);
    109      1.1     grant int snapper_get_props(void *);
    110      1.1     grant int snapper_trigger_output(void *, void *, void *, int, void (*)(void *),
    111      1.3      kent     void *, const audio_params_t *);
    112      1.1     grant int snapper_trigger_input(void *, void *, void *, int, void (*)(void *),
    113      1.3      kent     void *, const audio_params_t *);
    114      1.1     grant void snapper_set_volume(struct snapper_softc *, int, int);
    115      1.3      kent int snapper_set_rate(struct snapper_softc *, u_int);
    116      1.6  macallan void snapper_set_treble(struct snapper_softc *, int);
    117      1.6  macallan void snapper_set_bass(struct snapper_softc *, int);
    118      1.6  macallan void snapper_write_mixers(struct snapper_softc *);
    119      1.1     grant 
    120      1.1     grant int tas3004_write(struct snapper_softc *, u_int, const void *);
    121      1.1     grant static int gpio_read(char *);
    122      1.1     grant static void gpio_write(char *, int);
    123      1.1     grant void snapper_mute_speaker(struct snapper_softc *, int);
    124      1.1     grant void snapper_mute_headphone(struct snapper_softc *, int);
    125      1.1     grant int snapper_cint(void *);
    126      1.1     grant int tas3004_init(struct snapper_softc *);
    127      1.1     grant void snapper_init(struct snapper_softc *, int);
    128      1.1     grant 
    129      1.1     grant struct cfattach snapper_ca = {
    130      1.1     grant 	"snapper", {}, sizeof(struct snapper_softc),
    131      1.1     grant 	snapper_match, snapper_attach
    132      1.1     grant };
    133      1.1     grant 
    134      1.2      yamt const struct audio_hw_if snapper_hw_if = {
    135      1.3      kent 	NULL,			/* open */
    136      1.1     grant 	snapper_close,
    137      1.1     grant 	NULL,
    138      1.1     grant 	snapper_query_encoding,
    139      1.1     grant 	snapper_set_params,
    140      1.1     grant 	snapper_round_blocksize,
    141      1.1     grant 	NULL,
    142      1.1     grant 	NULL,
    143      1.1     grant 	NULL,
    144      1.1     grant 	NULL,
    145      1.1     grant 	NULL,
    146      1.1     grant 	snapper_halt_output,
    147      1.1     grant 	snapper_halt_input,
    148      1.1     grant 	NULL,
    149      1.1     grant 	snapper_getdev,
    150      1.1     grant 	NULL,
    151      1.1     grant 	snapper_set_port,
    152      1.1     grant 	snapper_get_port,
    153      1.1     grant 	snapper_query_devinfo,
    154      1.1     grant 	NULL,
    155      1.1     grant 	NULL,
    156      1.1     grant 	snapper_round_buffersize,
    157      1.1     grant 	snapper_mappage,
    158      1.1     grant 	snapper_get_props,
    159      1.1     grant 	snapper_trigger_output,
    160      1.1     grant 	snapper_trigger_input,
    161      1.1     grant 	NULL
    162      1.1     grant };
    163      1.1     grant 
    164      1.1     grant struct audio_device snapper_device = {
    165      1.1     grant 	"SNAPPER",
    166      1.1     grant 	"",
    167      1.1     grant 	"snapper"
    168      1.1     grant };
    169      1.1     grant 
    170      1.6  macallan const uint8_t snapper_basstab[] = {
    171      1.6  macallan 	0x96,	/* -18dB */
    172      1.6  macallan 	0x94,	/* -17dB */
    173      1.6  macallan 	0x92,	/* -16dB */
    174      1.6  macallan 	0x90,	/* -15dB */
    175      1.6  macallan 	0x8e,	/* -14dB */
    176      1.6  macallan 	0x8c,	/* -13dB */
    177      1.6  macallan 	0x8a,	/* -12dB */
    178      1.6  macallan 	0x88,	/* -11dB */
    179      1.6  macallan 	0x86,	/* -10dB */
    180      1.6  macallan 	0x84,	/* -9dB */
    181      1.6  macallan 	0x82,	/* -8dB */
    182      1.6  macallan 	0x80,	/* -7dB */
    183      1.6  macallan 	0x7e,	/* -6dB */
    184      1.6  macallan 	0x7c,	/* -5dB */
    185      1.6  macallan 	0x7a,	/* -4dB */
    186      1.6  macallan 	0x78,	/* -3dB */
    187      1.6  macallan 	0x76,	/* -2dB */
    188      1.6  macallan 	0x74,	/* -1dB */
    189      1.6  macallan 	0x72,	/* 0dB */
    190      1.6  macallan 	0x6f,	/* 1dB */
    191      1.6  macallan 	0x6d,	/* 2dB */
    192      1.6  macallan 	0x6a,	/* 3dB */
    193      1.6  macallan 	0x67,	/* 4dB */
    194      1.6  macallan 	0x65,	/* 5dB */
    195      1.6  macallan 	0x62,	/* 6dB */
    196      1.6  macallan 	0x5f,	/* 7dB */
    197      1.6  macallan 	0x5b,	/* 8dB */
    198      1.6  macallan 	0x55,	/* 9dB */
    199      1.6  macallan 	0x4f,	/* 10dB */
    200      1.6  macallan 	0x49,	/* 11dB */
    201      1.6  macallan 	0x43,	/* 12dB */
    202      1.6  macallan 	0x3b,	/* 13dB */
    203      1.6  macallan 	0x33,	/* 14dB */
    204      1.6  macallan 	0x29,	/* 15dB */
    205      1.6  macallan 	0x1e,	/* 16dB */
    206      1.6  macallan 	0x11,	/* 17dB */
    207      1.6  macallan 	0x01,	/* 18dB */
    208      1.6  macallan };
    209      1.6  macallan 
    210      1.3      kent #define SNAPPER_NFORMATS	1
    211      1.3      kent static const struct audio_format snapper_formats[SNAPPER_NFORMATS] = {
    212      1.3      kent 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_BE, 16, 16,
    213      1.3      kent 	 2, AUFMT_STEREO, 3, {8000, 44100, 48000}},
    214      1.3      kent };
    215      1.3      kent 
    216      1.1     grant static u_char *amp_mute;
    217      1.1     grant static u_char *headphone_mute;
    218      1.1     grant static u_char *audio_hw_reset;
    219      1.1     grant static u_char *headphone_detect;
    220      1.1     grant static int headphone_detect_active;
    221      1.1     grant 
    222      1.1     grant 
    223      1.1     grant /* I2S registers */
    224      1.1     grant #define I2S_INT		0x00
    225      1.1     grant #define I2S_FORMAT	0x10
    226      1.1     grant #define I2S_FRAMECOUNT	0x40
    227      1.1     grant #define I2S_FRAMEMATCH	0x50
    228      1.1     grant #define I2S_WORDSIZE	0x60
    229      1.1     grant 
    230      1.1     grant /* TAS3004 registers */
    231      1.1     grant #define DEQ_MCR1	0x01	/* Main control register 1 (1byte) */
    232      1.1     grant #define DEQ_DRC		0x02	/* Dynamic range compression (6bytes?) */
    233      1.1     grant #define DEQ_VOLUME	0x04	/* Volume (6bytes) */
    234      1.1     grant #define DEQ_TREBLE	0x05	/* Treble control (1byte) */
    235      1.1     grant #define DEQ_BASS	0x06	/* Bass control (1byte) */
    236      1.1     grant #define DEQ_MIXER_L	0x07	/* Mixer left gain (9bytes) */
    237      1.1     grant #define DEQ_MIXER_R	0x08	/* Mixer right gain (9bytes) */
    238      1.1     grant #define DEQ_LB0		0x0a	/* Left biquad 0 (15bytes) */
    239      1.1     grant #define DEQ_LB1		0x0b	/* Left biquad 1 (15bytes) */
    240      1.1     grant #define DEQ_LB2		0x0c	/* Left biquad 2 (15bytes) */
    241      1.1     grant #define DEQ_LB3		0x0d	/* Left biquad 3 (15bytes) */
    242      1.1     grant #define DEQ_LB4		0x0e	/* Left biquad 4 (15bytes) */
    243      1.1     grant #define DEQ_LB5		0x0f	/* Left biquad 5 (15bytes) */
    244      1.1     grant #define DEQ_LB6		0x10	/* Left biquad 6 (15bytes) */
    245      1.1     grant #define DEQ_RB0		0x13	/* Right biquad 0 (15bytes) */
    246      1.1     grant #define DEQ_RB1		0x14	/* Right biquad 1 (15bytes) */
    247      1.1     grant #define DEQ_RB2		0x15	/* Right biquad 2 (15bytes) */
    248      1.1     grant #define DEQ_RB3		0x16	/* Right biquad 3 (15bytes) */
    249      1.1     grant #define DEQ_RB4		0x17	/* Right biquad 4 (15bytes) */
    250      1.1     grant #define DEQ_RB5		0x18	/* Right biquad 5 (15bytes) */
    251      1.1     grant #define DEQ_RB6		0x19	/* Right biquad 6 (15bytes) */
    252      1.1     grant #define DEQ_LLB		0x21	/* Left loudness biquad (15bytes) */
    253      1.1     grant #define DEQ_RLB		0x22	/* Right loudness biquad (15bytes) */
    254      1.1     grant #define DEQ_LLB_GAIN	0x23	/* Left loudness biquad gain (3bytes) */
    255      1.1     grant #define DEQ_RLB_GAIN	0x24	/* Right loudness biquad gain (3bytes) */
    256      1.1     grant #define DEQ_ACR		0x40	/* Analog control register (1byte) */
    257      1.1     grant #define DEQ_MCR2	0x43	/* Main control register 2 (1byte) */
    258      1.1     grant 
    259      1.1     grant #define DEQ_MCR1_FL	0x80	/* Fast load */
    260      1.1     grant #define DEQ_MCR1_SC	0x40	/* SCLK frequency */
    261      1.1     grant #define  DEQ_MCR1_SC_32	0x00	/*  32fs */
    262      1.1     grant #define  DEQ_MCR1_SC_64	0x40	/*  64fs */
    263      1.1     grant #define DEQ_MCR1_SM	0x30	/* Output serial port mode */
    264      1.1     grant #define  DEQ_MCR1_SM_L	0x00	/*  Left justified */
    265      1.1     grant #define  DEQ_MCR1_SM_R	0x10	/*  Right justified */
    266      1.1     grant #define  DEQ_MCR1_SM_I2S 0x20	/*  I2S */
    267      1.1     grant #define DEQ_MCR1_W	0x03	/* Serial port word length */
    268      1.1     grant #define  DEQ_MCR1_W_16	0x00	/*  16 bit */
    269      1.1     grant #define  DEQ_MCR1_W_18	0x01	/*  18 bit */
    270      1.1     grant #define  DEQ_MCR1_W_20	0x02	/*  20 bit */
    271      1.1     grant 
    272      1.1     grant #define DEQ_MCR2_DL	0x80	/* Download */
    273      1.1     grant #define DEQ_MCR2_AP	0x02	/* All pass mode */
    274      1.1     grant 
    275      1.1     grant #define DEQ_ACR_ADM	0x80	/* ADC output mode */
    276      1.1     grant #define DEQ_ACR_LRB	0x40	/* Select B input */
    277      1.1     grant #define DEQ_ACR_DM	0x0c	/* De-emphasis control */
    278      1.1     grant #define  DEQ_ACR_DM_OFF	0x00	/*  off */
    279      1.1     grant #define  DEQ_ACR_DM_48	0x04	/*  fs = 48kHz */
    280      1.1     grant #define  DEQ_ACR_DM_44	0x08	/*  fs = 44.1kHz */
    281      1.1     grant #define DEQ_ACR_INP	0x02	/* Analog input select */
    282      1.1     grant #define  DEQ_ACR_INP_A	0x00	/*  A */
    283      1.1     grant #define  DEQ_ACR_INP_B	0x02	/*  B */
    284      1.1     grant #define DEQ_ACR_APD	0x01	/* Analog power down */
    285      1.1     grant 
    286      1.1     grant struct tas3004_reg {
    287      1.1     grant 	u_char MCR1[1];
    288      1.1     grant 	u_char DRC[6];
    289      1.1     grant 	u_char VOLUME[6];
    290      1.1     grant 	u_char TREBLE[1];
    291      1.1     grant 	u_char BASS[1];
    292      1.1     grant 	u_char MIXER_L[9];
    293      1.1     grant 	u_char MIXER_R[9];
    294      1.1     grant 	u_char LB0[15];
    295      1.1     grant 	u_char LB1[15];
    296      1.1     grant 	u_char LB2[15];
    297      1.1     grant 	u_char LB3[15];
    298      1.1     grant 	u_char LB4[15];
    299      1.1     grant 	u_char LB5[15];
    300      1.1     grant 	u_char LB6[15];
    301      1.1     grant 	u_char RB0[15];
    302      1.1     grant 	u_char RB1[15];
    303      1.1     grant 	u_char RB2[15];
    304      1.1     grant 	u_char RB3[15];
    305      1.1     grant 	u_char RB4[15];
    306      1.1     grant 	u_char RB5[15];
    307      1.1     grant 	u_char RB6[15];
    308      1.1     grant 	u_char LLB[15];
    309      1.1     grant 	u_char RLB[15];
    310      1.1     grant 	u_char LLB_GAIN[3];
    311      1.1     grant 	u_char RLB_GAIN[3];
    312      1.1     grant 	u_char ACR[1];
    313      1.1     grant 	u_char MCR2[1];
    314      1.1     grant };
    315      1.1     grant 
    316      1.1     grant #define GPIO_OUTSEL	0xf0	/* Output select */
    317      1.1     grant 		/*	0x00	GPIO bit0 is output
    318      1.1     grant 			0x10	media-bay power
    319      1.1     grant 			0x20	reserved
    320      1.1     grant 			0x30	MPIC */
    321      1.1     grant 
    322      1.1     grant #define GPIO_ALTOE	0x08	/* Alternate output enable */
    323      1.1     grant 		/*	0x00	Use DDR
    324      1.1     grant 			0x08	Use output select */
    325      1.1     grant 
    326      1.1     grant #define GPIO_DDR	0x04	/* Data direction */
    327      1.1     grant #define GPIO_DDR_OUTPUT	0x04	/* Output */
    328      1.1     grant #define GPIO_DDR_INPUT	0x00	/* Input */
    329      1.1     grant 
    330      1.1     grant #define GPIO_LEVEL	0x02	/* Pin level (RO) */
    331      1.1     grant 
    332      1.1     grant #define	GPIO_DATA	0x01	/* Data */
    333      1.1     grant 
    334      1.1     grant int
    335      1.4      kent snapper_match(struct device *parent, struct cfdata *match, void *aux)
    336      1.1     grant {
    337      1.4      kent 	struct confargs *ca;
    338      1.1     grant 	int soundbus, soundchip;
    339      1.1     grant 	char compat[32];
    340      1.1     grant 
    341      1.4      kent 	ca = aux;
    342      1.1     grant 	if (strcmp(ca->ca_name, "i2s") != 0)
    343      1.1     grant 		return 0;
    344      1.1     grant 
    345      1.1     grant 	if ((soundbus = OF_child(ca->ca_node)) == 0 ||
    346      1.1     grant 	    (soundchip = OF_child(soundbus)) == 0)
    347      1.1     grant 		return 0;
    348      1.1     grant 
    349      1.1     grant 	bzero(compat, sizeof compat);
    350      1.1     grant 	OF_getprop(soundchip, "compatible", compat, sizeof compat);
    351      1.1     grant 
    352      1.1     grant 	if (strcmp(compat, "snapper") != 0)
    353      1.1     grant 		return 0;
    354      1.1     grant 
    355      1.1     grant 	return 1;
    356      1.1     grant }
    357      1.1     grant 
    358      1.1     grant void
    359      1.4      kent snapper_attach(struct device *parent, struct device *self, void *aux)
    360      1.1     grant {
    361      1.4      kent 	struct snapper_softc *sc;
    362      1.4      kent 	struct confargs *ca;
    363      1.5    briggs 	unsigned long v;
    364      1.1     grant 	int cirq, oirq, iirq, cirq_type, oirq_type, iirq_type;
    365      1.1     grant 	int soundbus, intr[6];
    366      1.1     grant 
    367      1.4      kent 	sc = (struct snapper_softc *)self;
    368      1.4      kent 	ca = aux;
    369      1.5    briggs 
    370      1.5    briggs 	v = (((unsigned long) &sc->dbdma_cmdspace[0]) + 0xf) & ~0xf;
    371      1.5    briggs 	sc->sc_odmacmd = (struct dbdma_command *) v;
    372      1.5    briggs 	sc->sc_idmacmd = sc->sc_odmacmd + 20;
    373      1.5    briggs 
    374      1.1     grant #ifdef DIAGNOSTIC
    375      1.1     grant 	if ((vaddr_t)sc->sc_odmacmd & 0x0f) {
    376      1.1     grant 		printf(": bad dbdma alignment\n");
    377      1.1     grant 		return;
    378      1.1     grant 	}
    379      1.1     grant #endif
    380      1.1     grant 
    381      1.1     grant 	ca->ca_reg[0] += ca->ca_baseaddr;
    382      1.1     grant 	ca->ca_reg[2] += ca->ca_baseaddr;
    383      1.1     grant 	ca->ca_reg[4] += ca->ca_baseaddr;
    384      1.1     grant 
    385      1.1     grant 	sc->sc_node = ca->ca_node;
    386      1.1     grant 	sc->sc_reg = (void *)ca->ca_reg[0];
    387      1.1     grant 	sc->sc_odma = (void *)ca->ca_reg[2];
    388      1.1     grant 	sc->sc_idma = (void *)ca->ca_reg[4];
    389      1.1     grant 
    390      1.1     grant 	soundbus = OF_child(ca->ca_node);
    391      1.1     grant 	OF_getprop(soundbus, "interrupts", intr, sizeof intr);
    392      1.1     grant 	cirq = intr[0];
    393      1.1     grant 	oirq = intr[2];
    394      1.1     grant 	iirq = intr[4];
    395      1.1     grant 	cirq_type = intr[1] ? IST_LEVEL : IST_EDGE;
    396      1.1     grant 	oirq_type = intr[3] ? IST_LEVEL : IST_EDGE;
    397      1.1     grant 	iirq_type = intr[5] ? IST_LEVEL : IST_EDGE;
    398      1.1     grant 
    399      1.1     grant 	/* intr_establish(cirq, cirq_type, IPL_AUDIO, snapper_intr, sc); */
    400      1.1     grant 	intr_establish(oirq, oirq_type, IPL_AUDIO, snapper_intr, sc);
    401      1.1     grant 	/* intr_establish(iirq, iirq_type, IPL_AUDIO, snapper_intr, sc); */
    402      1.1     grant 
    403      1.5    briggs 	printf(": irq %d,%d,%d\n", cirq, oirq, iirq);
    404      1.1     grant 
    405      1.1     grant 	config_interrupts(self, snapper_defer);
    406      1.1     grant }
    407      1.1     grant 
    408      1.1     grant void
    409      1.1     grant snapper_defer(struct device *dev)
    410      1.1     grant {
    411      1.4      kent 	struct snapper_softc *sc;
    412      1.1     grant 	struct device *dv;
    413      1.6  macallan 	struct deq_softc *deq;
    414      1.6  macallan 
    415      1.4      kent 	sc = (struct snapper_softc *)dev;
    416      1.6  macallan 	/*
    417      1.1     grant 	for (dv = alldevs.tqh_first; dv; dv=dv->dv_list.tqe_next)
    418      1.1     grant 		if (strncmp(dv->dv_xname, "ki2c", 4) == 0 &&
    419  1.8.2.1      yamt 		    strncmp(device_parent(dv)->dv_xname, "obio", 4) == 0)
    420      1.1     grant 			sc->sc_i2c = dv;
    421      1.6  macallan 	*/
    422      1.6  macallan 	for (dv = alldevs.tqh_first; dv; dv=dv->dv_list.tqe_next)
    423      1.6  macallan 		if (strncmp(dv->dv_xname, "deq", 3) == 0 &&
    424  1.8.2.1      yamt 		    strncmp(device_parent(dv)->dv_xname, "ki2c", 4) == 0) {
    425      1.6  macallan 		    	deq=(struct deq_softc *)dv;
    426      1.6  macallan 			sc->sc_i2c = deq->sc_i2c;
    427      1.6  macallan 			sc->sc_deqaddr=deq->sc_address;
    428      1.6  macallan 		}
    429      1.6  macallan 
    430      1.1     grant 	if (sc->sc_i2c == NULL) {
    431      1.1     grant 		printf("%s: unable to find i2c\n", sc->sc_dev.dv_xname);
    432      1.1     grant 		return;
    433      1.1     grant 	}
    434      1.4      kent 
    435      1.1     grant 	/* XXX If i2c was failed to attach, what should we do? */
    436      1.1     grant 
    437      1.1     grant 	audio_attach_mi(&snapper_hw_if, sc, &sc->sc_dev);
    438      1.1     grant 
    439      1.1     grant 	/* ki2c_setmode(sc->sc_i2c, I2C_STDSUBMODE); */
    440      1.1     grant 	snapper_init(sc, sc->sc_node);
    441      1.1     grant }
    442      1.1     grant 
    443      1.1     grant int
    444      1.4      kent snapper_intr(void *v)
    445      1.1     grant {
    446      1.4      kent 	struct snapper_softc *sc;
    447      1.4      kent 	struct dbdma_command *cmd;
    448      1.4      kent 	int count;
    449      1.1     grant 	int status;
    450      1.1     grant 
    451      1.4      kent 	sc = v;
    452      1.4      kent 	cmd = sc->sc_odmacmd;
    453      1.4      kent 	count = sc->sc_opages;
    454      1.1     grant 	/* Fill used buffer(s). */
    455      1.1     grant 	while (count-- > 0) {
    456      1.1     grant 		if ((dbdma_ld16(&cmd->d_command) & 0x30) == 0x30) {
    457      1.1     grant 			status = dbdma_ld16(&cmd->d_status);
    458      1.1     grant 			cmd->d_status = 0;
    459      1.1     grant 			if (status)	/* status == 0x8400 */
    460      1.1     grant 				if (sc->sc_ointr)
    461      1.1     grant 					(*sc->sc_ointr)(sc->sc_oarg);
    462      1.1     grant 		}
    463      1.1     grant 		cmd++;
    464      1.1     grant 	}
    465      1.1     grant 
    466      1.1     grant 	return 1;
    467      1.1     grant }
    468      1.1     grant 
    469      1.1     grant /*
    470      1.1     grant  * Close function is called at splaudio().
    471      1.1     grant  */
    472      1.1     grant void
    473      1.4      kent snapper_close(void *h)
    474      1.1     grant {
    475      1.4      kent 	struct snapper_softc *sc;
    476      1.1     grant 
    477      1.4      kent 	sc = h;
    478      1.1     grant 	snapper_halt_output(sc);
    479      1.1     grant 	snapper_halt_input(sc);
    480      1.1     grant 
    481      1.1     grant 	sc->sc_ointr = 0;
    482      1.1     grant 	sc->sc_iintr = 0;
    483      1.1     grant }
    484      1.1     grant 
    485      1.1     grant int
    486      1.4      kent snapper_query_encoding(void *h, struct audio_encoding *ae)
    487      1.1     grant {
    488      1.4      kent 
    489      1.1     grant 	ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
    490      1.1     grant 	switch (ae->index) {
    491      1.1     grant 	case 0:
    492      1.1     grant 		strcpy(ae->name, AudioEslinear);
    493      1.1     grant 		ae->encoding = AUDIO_ENCODING_SLINEAR;
    494      1.1     grant 		ae->precision = 16;
    495      1.1     grant 		ae->flags = 0;
    496      1.1     grant 		return 0;
    497      1.1     grant 	case 1:
    498      1.1     grant 		strcpy(ae->name, AudioEslinear_be);
    499      1.1     grant 		ae->encoding = AUDIO_ENCODING_SLINEAR_BE;
    500      1.1     grant 		ae->precision = 16;
    501      1.1     grant 		ae->flags = 0;
    502      1.1     grant 		return 0;
    503      1.1     grant 	case 2:
    504      1.1     grant 		strcpy(ae->name, AudioEslinear_le);
    505      1.1     grant 		ae->encoding = AUDIO_ENCODING_SLINEAR_LE;
    506      1.1     grant 		ae->precision = 16;
    507      1.1     grant 		return 0;
    508      1.1     grant 	case 3:
    509      1.1     grant 		strcpy(ae->name, AudioEulinear_be);
    510      1.1     grant 		ae->encoding = AUDIO_ENCODING_ULINEAR_BE;
    511      1.1     grant 		ae->precision = 16;
    512      1.1     grant 		return 0;
    513      1.1     grant 	case 4:
    514      1.1     grant 		strcpy(ae->name, AudioEulinear_le);
    515      1.1     grant 		ae->encoding = AUDIO_ENCODING_ULINEAR_LE;
    516      1.1     grant 		ae->precision = 16;
    517      1.1     grant 		return 0;
    518      1.1     grant 	case 5:
    519      1.1     grant 		strcpy(ae->name, AudioEmulaw);
    520      1.1     grant 		ae->encoding = AUDIO_ENCODING_ULAW;
    521      1.1     grant 		ae->precision = 8;
    522      1.1     grant 		return 0;
    523      1.1     grant 	case 6:
    524      1.1     grant 		strcpy(ae->name, AudioEalaw);
    525      1.1     grant 		ae->encoding = AUDIO_ENCODING_ALAW;
    526      1.1     grant 		ae->precision = 8;
    527      1.1     grant 		return 0;
    528      1.1     grant 	default:
    529      1.1     grant 		return EINVAL;
    530      1.1     grant 	}
    531      1.1     grant }
    532      1.1     grant 
    533      1.1     grant int
    534      1.4      kent snapper_set_params(void *h, int setmode, int usemode,
    535      1.4      kent 		   audio_params_t *play, audio_params_t *rec,
    536      1.4      kent 		   stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    537      1.1     grant {
    538      1.4      kent 	struct snapper_softc *sc;
    539      1.3      kent 	audio_params_t *p;
    540      1.3      kent 	stream_filter_list_t *fil;
    541      1.3      kent 	int mode;
    542      1.1     grant 
    543      1.4      kent 	sc = h;
    544      1.1     grant 	p = NULL;
    545      1.1     grant 
    546      1.1     grant 	/*
    547      1.1     grant 	 * This device only has one clock, so make the sample rates match.
    548      1.1     grant 	 */
    549      1.1     grant 	if (play->sample_rate != rec->sample_rate &&
    550      1.1     grant 	    usemode == (AUMODE_PLAY | AUMODE_RECORD)) {
    551      1.1     grant 		if (setmode == AUMODE_PLAY) {
    552      1.1     grant 			rec->sample_rate = play->sample_rate;
    553      1.1     grant 			setmode |= AUMODE_RECORD;
    554      1.1     grant 		} else if (setmode == AUMODE_RECORD) {
    555      1.1     grant 			play->sample_rate = rec->sample_rate;
    556      1.1     grant 			setmode |= AUMODE_PLAY;
    557      1.1     grant 		} else
    558      1.1     grant 			return EINVAL;
    559      1.1     grant 	}
    560      1.1     grant 
    561      1.1     grant 	for (mode = AUMODE_RECORD; mode != -1;
    562      1.1     grant 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    563      1.1     grant 		if ((setmode & mode) == 0)
    564      1.1     grant 			continue;
    565      1.1     grant 
    566      1.1     grant 		p = mode == AUMODE_PLAY ? play : rec;
    567      1.3      kent 		if (p->sample_rate < 4000 || p->sample_rate > 50000)
    568      1.1     grant 			return EINVAL;
    569      1.1     grant 
    570      1.3      kent 		fil = mode == AUMODE_PLAY ? pfil : rfil;
    571      1.3      kent 		if (auconv_set_converter(snapper_formats, SNAPPER_NFORMATS,
    572      1.3      kent 					 mode, p, TRUE, fil) < 0)
    573      1.1     grant 			return EINVAL;
    574      1.3      kent 		if (fil->req_size > 0)
    575      1.3      kent 			p = &fil->filters[0].param;
    576      1.1     grant 	}
    577      1.1     grant 
    578      1.3      kent 	/* Set the speed. p points HW encoding. */
    579      1.3      kent 	if (snapper_set_rate(sc, p->sample_rate))
    580      1.1     grant 		return EINVAL;
    581      1.1     grant 
    582      1.1     grant 	return 0;
    583      1.1     grant }
    584      1.1     grant 
    585      1.1     grant int
    586      1.4      kent snapper_round_blocksize(void *h, int size, int mode,
    587      1.4      kent 			const audio_params_t *param)
    588      1.1     grant {
    589      1.4      kent 
    590      1.1     grant 	if (size < NBPG)
    591      1.1     grant 		size = NBPG;
    592      1.1     grant 	return size & ~PGOFSET;
    593      1.1     grant }
    594      1.1     grant 
    595      1.1     grant int
    596      1.4      kent snapper_halt_output(void *h)
    597      1.1     grant {
    598      1.4      kent 	struct snapper_softc *sc;
    599      1.1     grant 
    600      1.4      kent 	sc = h;
    601      1.1     grant 	dbdma_stop(sc->sc_odma);
    602      1.1     grant 	dbdma_reset(sc->sc_odma);
    603      1.1     grant 	return 0;
    604      1.1     grant }
    605      1.1     grant 
    606      1.1     grant int
    607      1.4      kent snapper_halt_input(void *h)
    608      1.1     grant {
    609      1.4      kent 	struct snapper_softc *sc;
    610      1.1     grant 
    611      1.4      kent 	sc = h;
    612      1.1     grant 	dbdma_stop(sc->sc_idma);
    613      1.1     grant 	dbdma_reset(sc->sc_idma);
    614      1.1     grant 	return 0;
    615      1.1     grant }
    616      1.1     grant 
    617      1.1     grant int
    618      1.4      kent snapper_getdev(void *h, struct audio_device *retp)
    619      1.1     grant {
    620      1.4      kent 
    621      1.1     grant 	*retp = snapper_device;
    622      1.1     grant 	return 0;
    623      1.1     grant }
    624      1.1     grant 
    625      1.1     grant enum {
    626      1.1     grant 	SNAPPER_MONITOR_CLASS,
    627      1.1     grant 	SNAPPER_OUTPUT_CLASS,
    628      1.1     grant 	SNAPPER_RECORD_CLASS,
    629      1.1     grant 	SNAPPER_OUTPUT_SELECT,
    630      1.1     grant 	SNAPPER_VOL_OUTPUT,
    631      1.6  macallan 	SNAPPER_DIGI1,
    632      1.6  macallan 	SNAPPER_DIGI2,
    633      1.6  macallan 	SNAPPER_ANALOG,
    634      1.1     grant 	SNAPPER_INPUT_SELECT,
    635      1.1     grant 	SNAPPER_VOL_INPUT,
    636      1.6  macallan 	SNAPPER_TREBLE,
    637      1.6  macallan 	SNAPPER_BASS,
    638      1.1     grant 	SNAPPER_ENUM_LAST
    639      1.1     grant };
    640      1.1     grant 
    641      1.1     grant int
    642      1.4      kent snapper_set_port(void *h, mixer_ctrl_t *mc)
    643      1.1     grant {
    644      1.4      kent 	struct snapper_softc *sc;
    645      1.1     grant 	int l, r;
    646      1.1     grant 
    647      1.1     grant 	DPRINTF("snapper_set_port dev = %d, type = %d\n", mc->dev, mc->type);
    648      1.4      kent 	sc = h;
    649      1.1     grant 	l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    650      1.1     grant 	r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
    651      1.1     grant 
    652      1.1     grant 	switch (mc->dev) {
    653      1.1     grant 	case SNAPPER_OUTPUT_SELECT:
    654      1.1     grant 		/* No change necessary? */
    655      1.1     grant 		if (mc->un.mask == sc->sc_output_mask)
    656      1.1     grant 			return 0;
    657      1.1     grant 
    658      1.1     grant 		snapper_mute_speaker(sc, 1);
    659      1.1     grant 		snapper_mute_headphone(sc, 1);
    660      1.1     grant 		if (mc->un.mask & 1 << 0)
    661      1.1     grant 			snapper_mute_speaker(sc, 0);
    662      1.1     grant 		if (mc->un.mask & 1 << 1)
    663      1.1     grant 			snapper_mute_headphone(sc, 0);
    664      1.1     grant 
    665      1.1     grant 		sc->sc_output_mask = mc->un.mask;
    666      1.1     grant 		return 0;
    667      1.1     grant 
    668      1.1     grant 	case SNAPPER_VOL_OUTPUT:
    669      1.1     grant 		snapper_set_volume(sc, l, r);
    670      1.1     grant 		return 0;
    671      1.1     grant 
    672      1.1     grant 	case SNAPPER_INPUT_SELECT:
    673      1.1     grant 		/* no change necessary? */
    674      1.1     grant 		if (mc->un.mask == sc->sc_record_source)
    675      1.1     grant 			return 0;
    676      1.1     grant 		switch (mc->un.mask) {
    677      1.1     grant 		case 1 << 0: /* CD */
    678      1.1     grant 		case 1 << 1: /* microphone */
    679      1.1     grant 		case 1 << 2: /* line in */
    680      1.1     grant 			/* XXX TO BE DONE */
    681      1.1     grant 			break;
    682      1.1     grant 		default: /* invalid argument */
    683      1.1     grant 			return EINVAL;
    684      1.1     grant 		}
    685      1.1     grant 		sc->sc_record_source = mc->un.mask;
    686      1.1     grant 		return 0;
    687      1.1     grant 
    688      1.1     grant 	case SNAPPER_VOL_INPUT:
    689      1.1     grant 		/* XXX TO BE DONE */
    690      1.1     grant 		return 0;
    691      1.1     grant 
    692      1.6  macallan 	case SNAPPER_BASS:
    693      1.6  macallan 		snapper_set_bass(sc,l);
    694      1.6  macallan 		return 0;
    695      1.6  macallan 	case SNAPPER_TREBLE:
    696      1.6  macallan 		snapper_set_treble(sc,l);
    697      1.6  macallan 		return 0;
    698      1.6  macallan 	case SNAPPER_DIGI1:
    699      1.6  macallan 		sc->mixer[0]=l;
    700      1.6  macallan 		sc->mixer[3]=r;
    701      1.6  macallan 		snapper_write_mixers(sc);
    702      1.6  macallan 		return 0;
    703      1.6  macallan 	case SNAPPER_DIGI2:
    704      1.6  macallan 		sc->mixer[1]=l;
    705      1.6  macallan 		sc->mixer[4]=r;
    706      1.6  macallan 		snapper_write_mixers(sc);
    707      1.6  macallan 		return 0;
    708      1.6  macallan 	case SNAPPER_ANALOG:
    709      1.6  macallan 		sc->mixer[2]=l;
    710      1.6  macallan 		sc->mixer[5]=r;
    711      1.6  macallan 		snapper_write_mixers(sc);
    712      1.6  macallan 		return 0;
    713      1.6  macallan 	}
    714      1.1     grant 	return ENXIO;
    715      1.1     grant }
    716      1.1     grant 
    717      1.1     grant int
    718      1.4      kent snapper_get_port(void *h, mixer_ctrl_t *mc)
    719      1.1     grant {
    720      1.4      kent 	struct snapper_softc *sc;
    721      1.1     grant 
    722      1.1     grant 	DPRINTF("snapper_get_port dev = %d, type = %d\n", mc->dev, mc->type);
    723      1.4      kent 	sc = h;
    724      1.1     grant 	switch (mc->dev) {
    725      1.1     grant 	case SNAPPER_OUTPUT_SELECT:
    726      1.1     grant 		mc->un.mask = sc->sc_output_mask;
    727      1.1     grant 		return 0;
    728      1.1     grant 
    729      1.1     grant 	case SNAPPER_VOL_OUTPUT:
    730      1.1     grant 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->sc_vol_l;
    731      1.1     grant 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->sc_vol_r;
    732      1.1     grant 		return 0;
    733      1.1     grant 
    734      1.1     grant 	case SNAPPER_INPUT_SELECT:
    735      1.1     grant 		mc->un.mask = sc->sc_record_source;
    736      1.1     grant 		return 0;
    737      1.1     grant 
    738      1.1     grant 	case SNAPPER_VOL_INPUT:
    739      1.1     grant 		/* XXX TO BE DONE */
    740      1.1     grant 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = 0;
    741      1.1     grant 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 0;
    742      1.1     grant 		return 0;
    743      1.6  macallan 	case SNAPPER_TREBLE:
    744      1.6  macallan 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO]=sc->sc_treble;
    745      1.6  macallan 		return 0;
    746      1.6  macallan 	case SNAPPER_BASS:
    747      1.6  macallan 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO]=sc->sc_bass;
    748      1.6  macallan 		return 0;
    749      1.6  macallan 	case SNAPPER_DIGI1:
    750      1.6  macallan 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[0];
    751      1.6  macallan 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[3];
    752      1.6  macallan 		return 0;
    753      1.6  macallan 	case SNAPPER_DIGI2:
    754      1.6  macallan 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[1];
    755      1.6  macallan 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[4];
    756      1.6  macallan 		return 0;
    757      1.6  macallan 	case SNAPPER_ANALOG:
    758      1.6  macallan 		mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[2];
    759      1.6  macallan 		mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[5];
    760      1.6  macallan 		return 0;
    761      1.1     grant 	default:
    762      1.1     grant 		return ENXIO;
    763      1.1     grant 	}
    764      1.1     grant 
    765      1.1     grant 	return 0;
    766      1.1     grant }
    767      1.1     grant 
    768      1.1     grant int
    769      1.4      kent snapper_query_devinfo(void *h, mixer_devinfo_t *dip)
    770      1.1     grant {
    771      1.1     grant 	switch (dip->index) {
    772      1.1     grant 
    773      1.1     grant 	case SNAPPER_OUTPUT_SELECT:
    774      1.1     grant 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
    775      1.1     grant 		strcpy(dip->label.name, AudioNoutput);
    776      1.1     grant 		dip->type = AUDIO_MIXER_SET;
    777      1.1     grant 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    778      1.1     grant 		dip->un.s.num_mem = 2;
    779      1.1     grant 		strcpy(dip->un.s.member[0].label.name, AudioNspeaker);
    780      1.1     grant 		dip->un.s.member[0].mask = 1 << 0;
    781      1.1     grant 		strcpy(dip->un.s.member[1].label.name, AudioNheadphone);
    782      1.1     grant 		dip->un.s.member[1].mask = 1 << 1;
    783      1.1     grant 		return 0;
    784      1.1     grant 
    785      1.1     grant 	case SNAPPER_VOL_OUTPUT:
    786      1.1     grant 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
    787      1.1     grant 		strcpy(dip->label.name, AudioNmaster);
    788      1.1     grant 		dip->type = AUDIO_MIXER_VALUE;
    789      1.1     grant 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    790      1.1     grant 		dip->un.v.num_channels = 2;
    791      1.1     grant 		strcpy(dip->un.v.units.name, AudioNvolume);
    792      1.1     grant 		return 0;
    793      1.1     grant 
    794      1.1     grant 	case SNAPPER_INPUT_SELECT:
    795      1.1     grant 		dip->mixer_class = SNAPPER_RECORD_CLASS;
    796      1.1     grant 		strcpy(dip->label.name, AudioNsource);
    797      1.1     grant 		dip->type = AUDIO_MIXER_SET;
    798      1.1     grant 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    799      1.1     grant 		dip->un.s.num_mem = 3;
    800      1.1     grant 		strcpy(dip->un.s.member[0].label.name, AudioNcd);
    801      1.1     grant 		dip->un.s.member[0].mask = 1 << 0;
    802      1.1     grant 		strcpy(dip->un.s.member[1].label.name, AudioNmicrophone);
    803      1.1     grant 		dip->un.s.member[1].mask = 1 << 1;
    804      1.1     grant 		strcpy(dip->un.s.member[2].label.name, AudioNline);
    805      1.1     grant 		dip->un.s.member[2].mask = 1 << 2;
    806      1.1     grant 		return 0;
    807      1.1     grant 
    808      1.1     grant 	case SNAPPER_VOL_INPUT:
    809      1.1     grant 		dip->mixer_class = SNAPPER_RECORD_CLASS;
    810      1.1     grant 		strcpy(dip->label.name, AudioNrecord);
    811      1.1     grant 		dip->type = AUDIO_MIXER_VALUE;
    812      1.1     grant 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    813      1.1     grant 		dip->un.v.num_channels = 2;
    814      1.1     grant 		strcpy(dip->un.v.units.name, AudioNvolume);
    815      1.1     grant 		return 0;
    816      1.1     grant 
    817      1.1     grant 	case SNAPPER_MONITOR_CLASS:
    818      1.1     grant 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
    819      1.1     grant 		strcpy(dip->label.name, AudioCmonitor);
    820      1.1     grant 		dip->type = AUDIO_MIXER_CLASS;
    821      1.1     grant 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    822      1.1     grant 		return 0;
    823      1.1     grant 
    824      1.1     grant 	case SNAPPER_OUTPUT_CLASS:
    825      1.1     grant 		dip->mixer_class = SNAPPER_OUTPUT_CLASS;
    826      1.1     grant 		strcpy(dip->label.name, AudioCoutputs);
    827      1.1     grant 		dip->type = AUDIO_MIXER_CLASS;
    828      1.1     grant 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    829      1.1     grant 		return 0;
    830      1.1     grant 
    831      1.1     grant 	case SNAPPER_RECORD_CLASS:
    832      1.1     grant 		dip->mixer_class = SNAPPER_RECORD_CLASS;
    833      1.1     grant 		strcpy(dip->label.name, AudioCrecord);
    834      1.1     grant 		dip->type = AUDIO_MIXER_CLASS;
    835      1.1     grant 		dip->next = dip->prev = AUDIO_MIXER_LAST;
    836      1.1     grant 		return 0;
    837      1.6  macallan 
    838      1.6  macallan 	case SNAPPER_TREBLE:
    839      1.6  macallan 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
    840      1.6  macallan 		strcpy(dip->label.name, AudioNtreble);
    841      1.6  macallan 		dip->type = AUDIO_MIXER_VALUE;
    842      1.6  macallan 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    843      1.6  macallan 		dip->un.v.num_channels = 1;
    844      1.6  macallan 		return 0;
    845      1.6  macallan 
    846      1.6  macallan 	case SNAPPER_BASS:
    847      1.6  macallan 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
    848      1.6  macallan 		strcpy(dip->label.name, AudioNbass);
    849      1.6  macallan 		dip->type = AUDIO_MIXER_VALUE;
    850      1.6  macallan 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    851      1.6  macallan 		dip->un.v.num_channels = 1;
    852      1.6  macallan 		return 0;
    853      1.6  macallan 
    854      1.6  macallan 	case SNAPPER_DIGI1:
    855      1.6  macallan 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
    856      1.6  macallan 		strcpy(dip->label.name, AudioNdac);
    857      1.6  macallan 		dip->type = AUDIO_MIXER_VALUE;
    858      1.6  macallan 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    859      1.6  macallan 		dip->un.v.num_channels = 2;
    860      1.6  macallan 		return 0;
    861      1.6  macallan 	case SNAPPER_DIGI2:
    862      1.6  macallan 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
    863      1.6  macallan 		strcpy(dip->label.name, "Digi2");
    864      1.6  macallan 		dip->type = AUDIO_MIXER_VALUE;
    865      1.6  macallan 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    866      1.6  macallan 		dip->un.v.num_channels = 2;
    867      1.6  macallan 		return 0;
    868      1.6  macallan 	case SNAPPER_ANALOG:
    869      1.6  macallan 		dip->mixer_class = SNAPPER_MONITOR_CLASS;
    870      1.6  macallan 		strcpy(dip->label.name, "Analog");
    871      1.6  macallan 		dip->type = AUDIO_MIXER_VALUE;
    872      1.6  macallan 		dip->prev = dip->next = AUDIO_MIXER_LAST;
    873      1.6  macallan 		dip->un.v.num_channels = 2;
    874      1.6  macallan 		return 0;
    875      1.1     grant 	}
    876      1.1     grant 
    877      1.1     grant 	return ENXIO;
    878      1.1     grant }
    879      1.1     grant 
    880      1.1     grant size_t
    881      1.4      kent snapper_round_buffersize(void *h, int dir, size_t size)
    882      1.1     grant {
    883      1.4      kent 
    884      1.1     grant 	if (size > 65536)
    885      1.1     grant 		size = 65536;
    886      1.1     grant 	return size;
    887      1.1     grant }
    888      1.1     grant 
    889      1.1     grant paddr_t
    890      1.4      kent snapper_mappage(void *h, void *mem, off_t off, int prot)
    891      1.1     grant {
    892      1.4      kent 
    893      1.1     grant 	if (off < 0)
    894      1.1     grant 		return -1;
    895      1.1     grant 	return -1;	/* XXX */
    896      1.1     grant }
    897      1.1     grant 
    898      1.1     grant int
    899      1.4      kent snapper_get_props(void *h)
    900      1.1     grant {
    901      1.1     grant 	return AUDIO_PROP_FULLDUPLEX /* | AUDIO_PROP_MMAP */;
    902      1.1     grant }
    903      1.1     grant 
    904      1.1     grant int
    905      1.4      kent snapper_trigger_output(void *h, void *start, void *end, int bsize,
    906      1.4      kent 		       void (*intr)(void *), void *arg,
    907      1.4      kent 		       const audio_params_t *param)
    908      1.1     grant {
    909      1.4      kent 	struct snapper_softc *sc;
    910      1.4      kent 	struct dbdma_command *cmd;
    911      1.1     grant 	vaddr_t va;
    912      1.1     grant 	int i, len, intmode;
    913      1.1     grant 
    914      1.1     grant 	DPRINTF("trigger_output %p %p 0x%x\n", start, end, bsize);
    915      1.4      kent 	sc = h;
    916      1.4      kent 	cmd = sc->sc_odmacmd;
    917      1.1     grant 	sc->sc_ointr = intr;
    918      1.1     grant 	sc->sc_oarg = arg;
    919      1.1     grant 	sc->sc_opages = ((char *)end - (char *)start) / NBPG;
    920      1.1     grant 
    921      1.1     grant #ifdef DIAGNOSTIC
    922      1.1     grant 	if (sc->sc_opages > 16)
    923      1.1     grant 		panic("snapper_trigger_output");
    924      1.1     grant #endif
    925      1.1     grant 
    926      1.1     grant 	va = (vaddr_t)start;
    927      1.1     grant 	len = 0;
    928      1.1     grant 	for (i = sc->sc_opages; i > 0; i--) {
    929      1.1     grant 		len += NBPG;
    930      1.1     grant 		if (len < bsize)
    931      1.1     grant 			intmode = 0;
    932      1.1     grant 		else {
    933      1.1     grant 			len = 0;
    934      1.1     grant 			intmode = DBDMA_INT_ALWAYS;
    935      1.1     grant 		}
    936      1.1     grant 
    937      1.4      kent 		DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, NBPG, vtophys(va),
    938      1.4      kent 		    intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    939      1.1     grant 		cmd++;
    940      1.1     grant 		va += NBPG;
    941      1.1     grant 	}
    942      1.1     grant 
    943      1.1     grant 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0,
    944      1.4      kent 	    0/*vtophys((vaddr_t)sc->sc_odmacmd)*/, 0, DBDMA_WAIT_NEVER,
    945      1.4      kent 	    DBDMA_BRANCH_ALWAYS);
    946      1.1     grant 
    947      1.1     grant 	dbdma_st32(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd));
    948      1.1     grant 
    949      1.1     grant 	dbdma_start(sc->sc_odma, sc->sc_odmacmd);
    950      1.1     grant 
    951      1.1     grant 	return 0;
    952      1.1     grant }
    953      1.1     grant 
    954      1.1     grant int
    955      1.4      kent snapper_trigger_input(void *h, void *start, void *end, int bsize,
    956      1.4      kent 		      void (*intr)(void *), void *arg,
    957      1.4      kent 		      const audio_params_t *param)
    958      1.1     grant {
    959      1.4      kent 
    960      1.1     grant 	printf("snapper_trigger_input called\n");
    961      1.1     grant 	return 1;
    962      1.1     grant }
    963      1.1     grant 
    964      1.1     grant void
    965      1.4      kent snapper_set_volume(struct snapper_softc *sc, int left, int right)
    966      1.1     grant {
    967      1.1     grant 	u_char vol[6];
    968      1.1     grant 
    969      1.1     grant 	sc->sc_vol_l = left;
    970      1.1     grant 	sc->sc_vol_r = right;
    971      1.1     grant 
    972      1.6  macallan #if 0
    973      1.1     grant 	left <<= 8;	/* XXX for now */
    974      1.1     grant 	right <<= 8;
    975      1.1     grant 
    976      1.1     grant 	vol[0] = left >> 16;
    977      1.1     grant 	vol[1] = left >> 8;
    978      1.1     grant 	vol[2] = left;
    979      1.1     grant 	vol[3] = right >> 16;
    980      1.1     grant 	vol[4] = right >> 8;
    981      1.1     grant 	vol[5] = right;
    982      1.6  macallan #else
    983      1.6  macallan 	/* 0x07ffff is LOUD, 0x000000 is mute */
    984      1.6  macallan 	vol[0]=0/*(left>>5)&0xff*/;		/* upper 3 bits */
    985      1.6  macallan 	vol[1]=(left/*<<3*/)&0xff;	/* lower 5 bits */
    986      1.6  macallan 	vol[2]=0;
    987      1.6  macallan 	vol[3]=0/*(right>>5)&0xff*/;		/* upper 3 bits */
    988      1.6  macallan 	vol[4]=(right/*<<3*/)&0xff;	/* lower 5 bits */
    989      1.6  macallan 	vol[5]=0;
    990      1.6  macallan #endif
    991      1.6  macallan 	tas3004_write(sc, DEQ_VOLUME, vol);
    992      1.6  macallan }
    993      1.6  macallan 
    994      1.6  macallan void snapper_set_treble(struct snapper_softc *sc, int stuff)
    995      1.6  macallan {
    996      1.6  macallan 	uint8_t reg;
    997      1.6  macallan 	if((stuff>=0) && (stuff<=255) && (sc->sc_treble!=stuff)) {
    998      1.6  macallan 		reg=snapper_basstab[(stuff>>3)+2];
    999      1.6  macallan 		sc->sc_treble=stuff;
   1000      1.6  macallan 		tas3004_write(sc, DEQ_TREBLE,&reg);
   1001      1.6  macallan 	}
   1002      1.6  macallan }
   1003      1.1     grant 
   1004      1.6  macallan void snapper_set_bass(struct snapper_softc *sc, int stuff)
   1005      1.6  macallan {
   1006      1.6  macallan 	uint8_t reg;
   1007      1.6  macallan 	if((stuff>=0) && (stuff<=255) && (stuff!=sc->sc_bass)) {
   1008      1.6  macallan 		reg=snapper_basstab[(stuff>>3)+2];
   1009      1.6  macallan 		sc->sc_bass=stuff;
   1010      1.6  macallan 		tas3004_write(sc, DEQ_BASS,&reg);
   1011      1.6  macallan 	}
   1012      1.6  macallan }
   1013      1.6  macallan 
   1014      1.6  macallan void snapper_write_mixers(struct snapper_softc *sc)
   1015      1.6  macallan {
   1016      1.6  macallan 	uint8_t regs[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
   1017      1.6  macallan 	regs[0] = (sc->mixer[0] >> 4) & 0xff;
   1018      1.6  macallan 	regs[1] = (sc->mixer[0] << 4) & 0xff;
   1019      1.6  macallan 	regs[3] = (sc->mixer[1] >> 4) & 0xff;
   1020      1.6  macallan 	regs[4] = (sc->mixer[1] << 4) & 0xff;
   1021      1.6  macallan 	regs[5] = (sc->mixer[2] >> 4) & 0xff;
   1022      1.6  macallan 	regs[7] = (sc->mixer[2] << 4) & 0xff;
   1023      1.6  macallan 	tas3004_write(sc, DEQ_MIXER_L, regs);
   1024      1.6  macallan 	regs[0] = (sc->mixer[3] >> 4) & 0xff;
   1025      1.6  macallan 	regs[1] = (sc->mixer[3] << 4) & 0xff;
   1026      1.6  macallan 	regs[3] = (sc->mixer[4] >> 4) & 0xff;
   1027      1.6  macallan 	regs[4] = (sc->mixer[4] << 4) & 0xff;
   1028      1.6  macallan 	regs[5] = (sc->mixer[5] >> 4) & 0xff;
   1029      1.6  macallan 	regs[7] = (sc->mixer[5] << 4) & 0xff;
   1030      1.6  macallan 	tas3004_write(sc, DEQ_MIXER_R, regs);
   1031      1.1     grant }
   1032      1.1     grant 
   1033      1.1     grant #define CLKSRC_49MHz	0x80000000	/* Use 49152000Hz Osc. */
   1034      1.1     grant #define CLKSRC_45MHz	0x40000000	/* Use 45158400Hz Osc. */
   1035      1.1     grant #define CLKSRC_18MHz	0x00000000	/* Use 18432000Hz Osc. */
   1036      1.1     grant #define MCLK_DIV	0x1f000000	/* MCLK = SRC / DIV */
   1037      1.1     grant #define  MCLK_DIV1	0x14000000	/*  MCLK = SRC */
   1038      1.1     grant #define  MCLK_DIV3	0x13000000	/*  MCLK = SRC / 3 */
   1039      1.1     grant #define  MCLK_DIV5	0x12000000	/*  MCLK = SRC / 5 */
   1040      1.1     grant #define SCLK_DIV	0x00f00000	/* SCLK = MCLK / DIV */
   1041      1.1     grant #define  SCLK_DIV1	0x00800000
   1042      1.1     grant #define  SCLK_DIV3	0x00900000
   1043      1.1     grant #define SCLK_MASTER	0x00080000	/* Master mode */
   1044      1.1     grant #define SCLK_SLAVE	0x00000000	/* Slave mode */
   1045      1.1     grant #define SERIAL_FORMAT	0x00070000
   1046      1.1     grant #define  SERIAL_SONY	0x00000000
   1047      1.1     grant #define  SERIAL_64x	0x00010000
   1048      1.1     grant #define  SERIAL_32x	0x00020000
   1049      1.1     grant #define  SERIAL_DAV	0x00040000
   1050      1.1     grant #define  SERIAL_SILICON	0x00050000
   1051      1.1     grant 
   1052      1.1     grant // rate = fs = LRCLK
   1053      1.1     grant // SCLK = 64*LRCLK (I2S)
   1054      1.1     grant // MCLK = 256fs (typ. -- changeable)
   1055      1.1     grant 
   1056      1.1     grant // MCLK = clksrc / mdiv
   1057      1.1     grant // SCLK = MCLK / sdiv
   1058      1.1     grant // rate = SCLK / 64    ( = LRCLK = fs)
   1059      1.1     grant 
   1060      1.1     grant int
   1061      1.4      kent snapper_set_rate(struct snapper_softc *sc, u_int rate)
   1062      1.1     grant {
   1063      1.4      kent 	u_int reg;
   1064      1.1     grant 	int MCLK;
   1065      1.1     grant 	int clksrc, mdiv, sdiv;
   1066      1.1     grant 	int mclk_fs;
   1067      1.1     grant 
   1068      1.4      kent 	reg = 0;
   1069      1.1     grant 	switch (rate) {
   1070      1.1     grant 	case 8000:
   1071      1.1     grant 		clksrc = 18432000;		/* 18MHz */
   1072      1.1     grant 		reg = CLKSRC_18MHz;
   1073      1.1     grant 		mclk_fs = 256;
   1074      1.1     grant 		break;
   1075      1.1     grant 
   1076      1.1     grant 	case 44100:
   1077      1.1     grant 		clksrc = 45158400;		/* 45MHz */
   1078      1.1     grant 		reg = CLKSRC_45MHz;
   1079      1.1     grant 		mclk_fs = 256;
   1080      1.1     grant 		break;
   1081      1.1     grant 
   1082      1.1     grant 	case 48000:
   1083      1.1     grant 		clksrc = 49152000;		/* 49MHz */
   1084      1.1     grant 		reg = CLKSRC_49MHz;
   1085      1.1     grant 		mclk_fs = 256;
   1086      1.1     grant 		break;
   1087      1.1     grant 
   1088      1.1     grant 	default:
   1089      1.1     grant 		return EINVAL;
   1090      1.1     grant 	}
   1091      1.1     grant 
   1092      1.1     grant 	MCLK = rate * mclk_fs;
   1093      1.1     grant 	mdiv = clksrc / MCLK;			// 4
   1094      1.1     grant 	sdiv = mclk_fs / 64;			// 4
   1095      1.1     grant 
   1096      1.1     grant 	switch (mdiv) {
   1097      1.1     grant 	case 1:
   1098      1.1     grant 		reg |= MCLK_DIV1;
   1099      1.1     grant 		break;
   1100      1.1     grant 	case 3:
   1101      1.1     grant 		reg |= MCLK_DIV3;
   1102      1.1     grant 		break;
   1103      1.1     grant 	case 5:
   1104      1.1     grant 		reg |= MCLK_DIV5;
   1105      1.1     grant 		break;
   1106      1.1     grant 	default:
   1107      1.1     grant 		reg |= ((mdiv / 2 - 1) << 24) & 0x1f000000;
   1108      1.1     grant 		break;
   1109      1.1     grant 	}
   1110      1.1     grant 
   1111      1.1     grant 	switch (sdiv) {
   1112      1.1     grant 	case 1:
   1113      1.1     grant 		reg |= SCLK_DIV1;
   1114      1.1     grant 		break;
   1115      1.1     grant 	case 3:
   1116      1.1     grant 		reg |= SCLK_DIV3;
   1117      1.1     grant 		break;
   1118      1.1     grant 	default:
   1119      1.1     grant 		reg |= ((sdiv / 2 - 1) << 20) & 0x00f00000;
   1120      1.1     grant 		break;
   1121      1.1     grant 	}
   1122      1.1     grant 
   1123      1.1     grant 	reg |= SCLK_MASTER;	/* XXX master mode */
   1124      1.1     grant 
   1125      1.1     grant 	reg |= SERIAL_64x;
   1126      1.1     grant 
   1127      1.1     grant 	/* stereo input and output */
   1128      1.1     grant 	DPRINTF("I2SSetDataWordSizeReg 0x%08x -> 0x%08x\n",
   1129      1.1     grant 	    in32rb(sc->sc_reg + I2S_WORDSIZE), 0x02000200);
   1130      1.1     grant 	out32rb(sc->sc_reg + I2S_WORDSIZE, 0x02000200);
   1131      1.1     grant 
   1132      1.1     grant 	DPRINTF("I2SSetSerialFormatReg 0x%x -> 0x%x\n",
   1133      1.1     grant 	    in32rb(sc->sc_reg + I2S_FORMAT), reg);
   1134      1.1     grant 	out32rb(sc->sc_reg + I2S_FORMAT, reg);
   1135      1.1     grant 
   1136      1.1     grant 	return 0;
   1137      1.1     grant }
   1138      1.1     grant 
   1139      1.6  macallan /*#define DEQaddr 0x6a*/
   1140      1.1     grant 
   1141      1.1     grant const struct tas3004_reg tas3004_initdata = {
   1142      1.1     grant 	{ DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_20 },	/* MCR1 */
   1143      1.1     grant 	{ 1, 0, 0, 0, 0, 0 },					/* DRC */
   1144      1.1     grant 	{ 0, 0, 0, 0, 0, 0 },					/* VOLUME */
   1145      1.1     grant 	{ 0x72 },						/* TREBLE */
   1146      1.1     grant 	{ 0x72 },						/* BASS */
   1147      1.1     grant 	{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },			/* MIXER_L */
   1148      1.1     grant 	{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },			/* MIXER_R */
   1149      1.1     grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1150      1.1     grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1151      1.1     grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1152      1.1     grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1153      1.1     grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1154      1.1     grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1155      1.1     grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1156      1.1     grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1157      1.1     grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1158      1.1     grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1159      1.1     grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1160      1.1     grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1161      1.1     grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1162      1.1     grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1163      1.1     grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1164      1.1     grant 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
   1165      1.1     grant 	{ 0, 0, 0 },						/* LLB_GAIN */
   1166      1.1     grant 	{ 0, 0, 0 },						/* RLB_GAIN */
   1167      1.6  macallan 	{ 0xc0 },							/* ACR - right channel of input B is the microphone */
   1168      1.6  macallan 	{ 2 }							/* MCR2 - AllPass mode since we don't use the equalizer anyway */
   1169      1.1     grant };
   1170      1.1     grant 
   1171      1.1     grant const char tas3004_regsize[] = {
   1172      1.1     grant 	0,					/* 0x00 */
   1173      1.1     grant 	sizeof tas3004_initdata.MCR1,		/* 0x01 */
   1174      1.1     grant 	sizeof tas3004_initdata.DRC,		/* 0x02 */
   1175      1.1     grant 	0,					/* 0x03 */
   1176      1.1     grant 	sizeof tas3004_initdata.VOLUME,		/* 0x04 */
   1177      1.1     grant 	sizeof tas3004_initdata.TREBLE,		/* 0x05 */
   1178      1.1     grant 	sizeof tas3004_initdata.BASS,		/* 0x06 */
   1179      1.1     grant 	sizeof tas3004_initdata.MIXER_L,	/* 0x07 */
   1180      1.1     grant 	sizeof tas3004_initdata.MIXER_R,	/* 0x08 */
   1181      1.1     grant 	0,					/* 0x09 */
   1182      1.1     grant 	sizeof tas3004_initdata.LB0,		/* 0x0a */
   1183      1.1     grant 	sizeof tas3004_initdata.LB1,		/* 0x0b */
   1184      1.1     grant 	sizeof tas3004_initdata.LB2,		/* 0x0c */
   1185      1.1     grant 	sizeof tas3004_initdata.LB3,		/* 0x0d */
   1186      1.1     grant 	sizeof tas3004_initdata.LB4,		/* 0x0e */
   1187      1.1     grant 	sizeof tas3004_initdata.LB5,		/* 0x0f */
   1188      1.1     grant 	sizeof tas3004_initdata.LB6,		/* 0x10 */
   1189      1.1     grant 	0,					/* 0x11 */
   1190      1.1     grant 	0,					/* 0x12 */
   1191      1.1     grant 	sizeof tas3004_initdata.RB0,		/* 0x13 */
   1192      1.1     grant 	sizeof tas3004_initdata.RB1,		/* 0x14 */
   1193      1.1     grant 	sizeof tas3004_initdata.RB2,		/* 0x15 */
   1194      1.1     grant 	sizeof tas3004_initdata.RB3,		/* 0x16 */
   1195      1.1     grant 	sizeof tas3004_initdata.RB4,		/* 0x17 */
   1196      1.1     grant 	sizeof tas3004_initdata.RB5,		/* 0x18 */
   1197      1.1     grant 	sizeof tas3004_initdata.RB6,		/* 0x19 */
   1198      1.1     grant 	0,0,0,0, 0,0,
   1199      1.1     grant 	0,					/* 0x20 */
   1200      1.1     grant 	sizeof tas3004_initdata.LLB,		/* 0x21 */
   1201      1.1     grant 	sizeof tas3004_initdata.RLB,		/* 0x22 */
   1202      1.1     grant 	sizeof tas3004_initdata.LLB_GAIN,	/* 0x23 */
   1203      1.1     grant 	sizeof tas3004_initdata.RLB_GAIN,	/* 0x24 */
   1204      1.1     grant 	0,0,0,0, 0,0,0,0, 0,0,0,
   1205      1.1     grant 	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
   1206      1.1     grant 	sizeof tas3004_initdata.ACR,		/* 0x40 */
   1207      1.1     grant 	0,					/* 0x41 */
   1208      1.1     grant 	0,					/* 0x42 */
   1209      1.1     grant 	sizeof tas3004_initdata.MCR2		/* 0x43 */
   1210      1.1     grant };
   1211      1.1     grant 
   1212      1.1     grant int
   1213      1.4      kent tas3004_write(struct snapper_softc *sc, u_int reg, const void *data)
   1214      1.1     grant {
   1215      1.1     grant 	int size;
   1216      1.6  macallan 	static char regblock[sizeof(struct tas3004_reg)+1];
   1217      1.6  macallan 
   1218      1.1     grant 	KASSERT(reg < sizeof tas3004_regsize);
   1219      1.1     grant 	size = tas3004_regsize[reg];
   1220      1.1     grant 	KASSERT(size > 0);
   1221      1.1     grant 
   1222      1.6  macallan #ifdef DEBUG_SNAPPER
   1223      1.6  macallan 	printf("reg: %x, %d %d\n",reg,size,((char*)data)[0]);
   1224      1.6  macallan #endif
   1225      1.6  macallan #if 0
   1226      1.6  macallan 	ki2c_setmode(sc->sc_i2c, 8); /* std+sub mode */
   1227      1.6  macallan 
   1228      1.1     grant 	if (ki2c_write(sc->sc_i2c, DEQaddr, reg, data, size))
   1229      1.1     grant 		return -1;
   1230      1.6  macallan #endif
   1231      1.6  macallan 	/* ugly, but for now... */
   1232      1.6  macallan 	regblock[0] = reg;
   1233      1.6  macallan 	memcpy(&regblock[1], data, size);
   1234      1.6  macallan 	iic_acquire_bus(sc->sc_i2c, 0);
   1235      1.6  macallan 	iic_exec(sc->sc_i2c, I2C_OP_WRITE, sc->sc_deqaddr, &regblock, size + 1,
   1236      1.6  macallan 	    NULL, 0, 0);
   1237      1.6  macallan 	iic_release_bus(sc->sc_i2c, 0);
   1238      1.6  macallan 
   1239      1.1     grant 	return 0;
   1240      1.1     grant }
   1241      1.1     grant 
   1242      1.1     grant int
   1243      1.4      kent gpio_read(char *addr)
   1244      1.1     grant {
   1245      1.4      kent 
   1246      1.1     grant 	if (*addr & GPIO_DATA)
   1247      1.1     grant 		return 1;
   1248      1.1     grant 	return 0;
   1249      1.1     grant }
   1250      1.1     grant 
   1251      1.1     grant void
   1252      1.4      kent gpio_write(char *addr, int val)
   1253      1.1     grant {
   1254      1.4      kent 	u_int data;
   1255      1.1     grant 
   1256      1.4      kent 	data = GPIO_DDR_OUTPUT;
   1257      1.1     grant 	if (val)
   1258      1.1     grant 		data |= GPIO_DATA;
   1259      1.1     grant 	*addr = data;
   1260      1.8     perry 	__asm volatile ("eieio");
   1261      1.1     grant }
   1262      1.1     grant 
   1263      1.1     grant #define headphone_active 0	/* XXX OF */
   1264      1.1     grant #define amp_active 0		/* XXX OF */
   1265      1.1     grant 
   1266      1.1     grant void
   1267      1.4      kent snapper_mute_speaker(struct snapper_softc *sc, int mute)
   1268      1.1     grant {
   1269      1.1     grant 	u_int x;
   1270      1.1     grant 
   1271      1.1     grant 	DPRINTF("ampmute %d --> ", gpio_read(amp_mute));
   1272      1.1     grant 
   1273      1.1     grant 	if (mute)
   1274      1.1     grant 		x = amp_active;		/* mute */
   1275      1.1     grant 	else
   1276      1.1     grant 		x = !amp_active;	/* unmute */
   1277      1.1     grant 	if (x != gpio_read(amp_mute))
   1278      1.1     grant 		gpio_write(amp_mute, x);
   1279      1.1     grant 
   1280      1.1     grant 	DPRINTF("%d\n", gpio_read(amp_mute));
   1281      1.1     grant }
   1282      1.1     grant 
   1283      1.1     grant void
   1284      1.4      kent snapper_mute_headphone(struct snapper_softc *sc, int mute)
   1285      1.1     grant {
   1286      1.1     grant 	u_int x;
   1287      1.1     grant 
   1288      1.1     grant 	DPRINTF("headphonemute %d --> ", gpio_read(headphone_mute));
   1289      1.1     grant 
   1290      1.1     grant 	if (mute)
   1291      1.1     grant 		x = headphone_active;	/* mute */
   1292      1.1     grant 	else
   1293      1.1     grant 		x = !headphone_active;	/* unmute */
   1294      1.1     grant 	if (x != gpio_read(headphone_mute))
   1295      1.1     grant 		gpio_write(headphone_mute, x);
   1296      1.1     grant 
   1297      1.1     grant 	DPRINTF("%d\n", gpio_read(headphone_mute));
   1298      1.1     grant }
   1299      1.1     grant 
   1300      1.1     grant int
   1301      1.4      kent snapper_cint(void *v)
   1302      1.1     grant {
   1303      1.4      kent 	struct snapper_softc *sc;
   1304      1.1     grant 	u_int sense;
   1305      1.1     grant 
   1306      1.4      kent 	sc = v;
   1307      1.1     grant 	sense = *headphone_detect;
   1308      1.1     grant 	DPRINTF("headphone detect = 0x%x\n", sense);
   1309      1.1     grant 
   1310      1.1     grant 	if (((sense & 0x02) >> 1) == headphone_detect_active) {
   1311      1.1     grant 		DPRINTF("headphone is inserted\n");
   1312      1.1     grant 		snapper_mute_speaker(sc, 1);
   1313      1.1     grant 		snapper_mute_headphone(sc, 0);
   1314      1.1     grant 		sc->sc_output_mask = 1 << 1;
   1315      1.1     grant 	} else {
   1316      1.1     grant 		DPRINTF("headphone is NOT inserted\n");
   1317      1.1     grant 		snapper_mute_speaker(sc, 0);
   1318      1.1     grant 		snapper_mute_headphone(sc, 1);
   1319      1.1     grant 		sc->sc_output_mask = 1 << 0;
   1320      1.1     grant 	}
   1321      1.1     grant 
   1322      1.1     grant 	return 1;
   1323      1.1     grant }
   1324      1.1     grant 
   1325      1.1     grant #define reset_active 0	/* XXX OF */
   1326      1.1     grant 
   1327      1.1     grant #define DEQ_WRITE(sc, reg, addr) \
   1328      1.1     grant 	if (tas3004_write(sc, reg, addr)) goto err
   1329      1.1     grant 
   1330      1.1     grant int
   1331      1.4      kent tas3004_init(struct snapper_softc *sc)
   1332      1.1     grant {
   1333      1.1     grant 
   1334      1.1     grant 	/* No reset port.  Nothing to do. */
   1335      1.1     grant 	if (audio_hw_reset == NULL)
   1336      1.1     grant 		goto noreset;
   1337      1.1     grant 
   1338      1.1     grant 	/* Reset TAS3004. */
   1339      1.1     grant 	gpio_write(audio_hw_reset, !reset_active);	/* Negate RESET */
   1340      1.1     grant 	delay(100000);				/* XXX Really needed? */
   1341      1.1     grant 
   1342      1.1     grant 	gpio_write(audio_hw_reset, reset_active);	/* Assert RESET */
   1343      1.1     grant 	delay(1);
   1344      1.1     grant 
   1345      1.1     grant 	gpio_write(audio_hw_reset, !reset_active);	/* Negate RESET */
   1346      1.1     grant 	delay(10000);
   1347      1.1     grant 
   1348      1.1     grant noreset:
   1349      1.1     grant 	DEQ_WRITE(sc, DEQ_LB0, tas3004_initdata.LB0);
   1350      1.1     grant 	DEQ_WRITE(sc, DEQ_LB1, tas3004_initdata.LB1);
   1351      1.1     grant 	DEQ_WRITE(sc, DEQ_LB2, tas3004_initdata.LB2);
   1352      1.1     grant 	DEQ_WRITE(sc, DEQ_LB3, tas3004_initdata.LB3);
   1353      1.1     grant 	DEQ_WRITE(sc, DEQ_LB4, tas3004_initdata.LB4);
   1354      1.1     grant 	DEQ_WRITE(sc, DEQ_LB5, tas3004_initdata.LB5);
   1355      1.1     grant 	DEQ_WRITE(sc, DEQ_LB6, tas3004_initdata.LB6);
   1356      1.1     grant 	DEQ_WRITE(sc, DEQ_RB0, tas3004_initdata.RB0);
   1357      1.1     grant 	DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1);
   1358      1.1     grant 	DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1);
   1359      1.1     grant 	DEQ_WRITE(sc, DEQ_RB2, tas3004_initdata.RB2);
   1360      1.1     grant 	DEQ_WRITE(sc, DEQ_RB3, tas3004_initdata.RB3);
   1361      1.1     grant 	DEQ_WRITE(sc, DEQ_RB4, tas3004_initdata.RB4);
   1362      1.1     grant 	DEQ_WRITE(sc, DEQ_RB5, tas3004_initdata.RB5);
   1363      1.1     grant 	DEQ_WRITE(sc, DEQ_MCR1, tas3004_initdata.MCR1);
   1364      1.1     grant 	DEQ_WRITE(sc, DEQ_MCR2, tas3004_initdata.MCR2);
   1365      1.1     grant 	DEQ_WRITE(sc, DEQ_DRC, tas3004_initdata.DRC);
   1366      1.1     grant 	DEQ_WRITE(sc, DEQ_VOLUME, tas3004_initdata.VOLUME);
   1367      1.1     grant 	DEQ_WRITE(sc, DEQ_TREBLE, tas3004_initdata.TREBLE);
   1368      1.1     grant 	DEQ_WRITE(sc, DEQ_BASS, tas3004_initdata.BASS);
   1369      1.1     grant 	DEQ_WRITE(sc, DEQ_MIXER_L, tas3004_initdata.MIXER_L);
   1370      1.1     grant 	DEQ_WRITE(sc, DEQ_MIXER_R, tas3004_initdata.MIXER_R);
   1371      1.1     grant 	DEQ_WRITE(sc, DEQ_LLB, tas3004_initdata.LLB);
   1372      1.1     grant 	DEQ_WRITE(sc, DEQ_RLB, tas3004_initdata.RLB);
   1373      1.1     grant 	DEQ_WRITE(sc, DEQ_LLB_GAIN, tas3004_initdata.LLB_GAIN);
   1374      1.1     grant 	DEQ_WRITE(sc, DEQ_RLB_GAIN, tas3004_initdata.RLB_GAIN);
   1375      1.1     grant 	DEQ_WRITE(sc, DEQ_ACR, tas3004_initdata.ACR);
   1376      1.1     grant 
   1377      1.1     grant 	return 0;
   1378      1.1     grant err:
   1379      1.1     grant 	printf("tas3004_init: error\n");
   1380      1.1     grant 	return -1;
   1381      1.1     grant }
   1382      1.1     grant 
   1383      1.1     grant /* FCR(0x3c) bits */
   1384      1.1     grant #define I2S0CLKEN	0x1000
   1385      1.1     grant #define I2S0EN		0x2000
   1386      1.1     grant #define I2S1CLKEN	0x080000
   1387      1.1     grant #define I2S1EN		0x100000
   1388      1.1     grant 
   1389      1.1     grant #define FCR3C_BITMASK "\020\25I2S1EN\24I2S1CLKEN\16I2S0EN\15I2S0CLKEN"
   1390      1.1     grant 
   1391      1.1     grant void
   1392      1.4      kent snapper_init(struct snapper_softc *sc, int node)
   1393      1.1     grant {
   1394      1.1     grant 	int gpio;
   1395      1.4      kent 	int headphone_detect_intr, headphone_detect_intrtype;
   1396      1.1     grant #ifdef SNAPPER_DEBUG
   1397      1.1     grant 	char fcr[32];
   1398      1.1     grant 
   1399      1.1     grant 	bitmask_snprintf(in32rb(0x8000003c), FCR3C_BITMASK, fcr, sizeof fcr);
   1400      1.1     grant 	printf("FCR(0x3c) 0x%s\n", fcr);
   1401      1.1     grant #endif
   1402      1.4      kent 	headphone_detect_intr = -1;
   1403      1.1     grant 
   1404      1.1     grant 	gpio = getnodebyname(OF_parent(node), "gpio");
   1405      1.1     grant 	DPRINTF(" /gpio 0x%x\n", gpio);
   1406      1.1     grant 	gpio = OF_child(gpio);
   1407      1.1     grant 	while (gpio) {
   1408      1.1     grant 		char name[64], audio_gpio[64];
   1409      1.1     grant 		int intr[2];
   1410      1.1     grant 		char *addr;
   1411      1.1     grant 
   1412      1.1     grant 		bzero(name, sizeof name);
   1413      1.1     grant 		bzero(audio_gpio, sizeof audio_gpio);
   1414      1.1     grant 		addr = 0;
   1415      1.1     grant 		OF_getprop(gpio, "name", name, sizeof name);
   1416      1.1     grant 		OF_getprop(gpio, "audio-gpio", audio_gpio, sizeof audio_gpio);
   1417      1.1     grant 		OF_getprop(gpio, "AAPL,address", &addr, sizeof addr);
   1418      1.1     grant 		/* printf("0x%x %s %s\n", gpio, name, audio_gpio); */
   1419      1.1     grant 
   1420      1.1     grant 		/* gpio5 */
   1421      1.1     grant 		if (strcmp(audio_gpio, "headphone-mute") == 0)
   1422      1.1     grant 			headphone_mute = addr;
   1423      1.1     grant 		/* gpio6 */
   1424      1.1     grant 		if (strcmp(audio_gpio, "amp-mute") == 0)
   1425      1.1     grant 			amp_mute = addr;
   1426      1.1     grant 		/* extint-gpio15 */
   1427      1.1     grant 		if (strcmp(audio_gpio, "headphone-detect") == 0) {
   1428      1.1     grant 			headphone_detect = addr;
   1429      1.1     grant 			OF_getprop(gpio, "audio-gpio-active-state",
   1430      1.1     grant 			    &headphone_detect_active, 4);
   1431      1.1     grant 			OF_getprop(gpio, "interrupts", intr, 8);
   1432      1.1     grant 			headphone_detect_intr = intr[0];
   1433      1.1     grant 			headphone_detect_intrtype = intr[1];
   1434      1.1     grant 		}
   1435      1.1     grant 		/* gpio11 (keywest-11) */
   1436      1.1     grant 		if (strcmp(audio_gpio, "audio-hw-reset") == 0)
   1437      1.1     grant 			audio_hw_reset = addr;
   1438      1.1     grant 		gpio = OF_peer(gpio);
   1439      1.1     grant 	}
   1440      1.1     grant 	DPRINTF(" headphone-mute %p\n", headphone_mute);
   1441      1.1     grant 	DPRINTF(" amp-mute %p\n", amp_mute);
   1442      1.1     grant 	DPRINTF(" headphone-detect %p\n", headphone_detect);
   1443      1.1     grant 	DPRINTF(" headphone-detect active %x\n", headphone_detect_active);
   1444      1.1     grant 	DPRINTF(" headphone-detect intr %x\n", headphone_detect_intr);
   1445      1.1     grant 	DPRINTF(" audio-hw-reset %p\n", audio_hw_reset);
   1446      1.1     grant 
   1447      1.1     grant 	if (headphone_detect_intr != -1)
   1448      1.1     grant 		intr_establish(headphone_detect_intr, IST_EDGE, IPL_AUDIO,
   1449      1.1     grant 		    snapper_cint, sc);
   1450      1.1     grant 
   1451      1.1     grant 	/* "sample-rates" (44100, 48000) */
   1452      1.1     grant 	snapper_set_rate(sc, 44100);
   1453      1.1     grant 
   1454      1.1     grant 	/* Enable headphone interrupt? */
   1455      1.1     grant 	*headphone_detect |= 0x80;
   1456      1.8     perry 	__asm volatile ("eieio");
   1457      1.1     grant 
   1458      1.1     grant 	/* i2c_set_port(port); */
   1459      1.1     grant 
   1460      1.6  macallan #if 0
   1461      1.1     grant 	/* Enable I2C interrupts. */
   1462      1.1     grant #define IER 4
   1463      1.1     grant #define I2C_INT_DATA 0x01
   1464      1.1     grant #define I2C_INT_ADDR 0x02
   1465      1.1     grant #define I2C_INT_STOP 0x04
   1466      1.1     grant 	ki2c_writereg(sc->sc_i2c, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
   1467      1.1     grant #endif
   1468      1.1     grant 
   1469      1.1     grant 	if (tas3004_init(sc))
   1470      1.1     grant 		return;
   1471      1.1     grant 
   1472      1.1     grant 	/* Update headphone status. */
   1473      1.1     grant 	snapper_cint(sc);
   1474      1.1     grant 
   1475      1.1     grant 	snapper_set_volume(sc, 80, 80);
   1476      1.6  macallan 
   1477      1.6  macallan 	sc->sc_bass = 128;
   1478      1.6  macallan 	sc->sc_treble = 128;
   1479      1.6  macallan 
   1480      1.6  macallan 	/* We mute the analog input for now */
   1481      1.6  macallan 	sc->mixer[0] = 80;
   1482      1.6  macallan 	sc->mixer[1] = 80;
   1483      1.6  macallan 	sc->mixer[2] = 0;
   1484      1.6  macallan 	sc->mixer[3] = 80;
   1485      1.6  macallan 	sc->mixer[4] = 80;
   1486      1.6  macallan 	sc->mixer[5] = 0;
   1487      1.6  macallan 	snapper_write_mixers(sc);
   1488      1.1     grant }
   1489