xref: /freebsd/sys/dev/sound/macio/snapper.c (revision a6deeaa2fb3b28a61ae56a85f2275f67fcf23262)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause AND BSD-3-Clause
3  *
4  * Copyright 2008 by Marco Trillo. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 /*-
28  * Copyright (c) 2002, 2003 Tsubai Masanari.  All rights reserved.
29  *
30  * Redistribution and use in source and binary forms, with or without
31  * modification, are permitted provided that the following conditions
32  * are met:
33  * 1. Redistributions of source code must retain the above copyright
34  *    notice, this list of conditions and the following disclaimer.
35  * 2. Redistributions in binary form must reproduce the above copyright
36  *    notice, this list of conditions and the following disclaimer in the
37  *    documentation and/or other materials provided with the distribution.
38  * 3. The name of the author may not be used to endorse or promote products
39  *    derived from this software without specific prior written permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
42  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
44  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
45  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51  *
52  * 	NetBSD: snapper.c,v 1.28 2008/05/16 03:49:54 macallan Exp
53  *	Id: snapper.c,v 1.11 2002/10/31 17:42:13 tsubai Exp
54  */
55 
56 /*
57  *	Apple Snapper audio.
58  */
59 
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
63 #include <sys/module.h>
64 #include <sys/bus.h>
65 #include <sys/malloc.h>
66 #include <sys/lock.h>
67 #include <sys/mutex.h>
68 #include <sys/taskqueue.h>
69 #include <machine/dbdma.h>
70 #include <machine/intr_machdep.h>
71 #include <machine/resource.h>
72 #include <machine/bus.h>
73 #include <machine/pio.h>
74 #include <sys/rman.h>
75 
76 #include <dev/iicbus/iicbus.h>
77 #include <dev/iicbus/iiconf.h>
78 #include <dev/ofw/ofw_bus.h>
79 
80 #ifdef HAVE_KERNEL_OPTION_HEADERS
81 #include "opt_snd.h"
82 #endif
83 
84 #include <dev/sound/pcm/sound.h>
85 
86 #include "mixer_if.h"
87 
88 extern kobj_class_t i2s_mixer_class;
89 extern device_t	i2s_mixer;
90 
91 struct snapper_softc
92 {
93 	device_t sc_dev;
94 	uint32_t sc_addr;
95 	struct mtx sc_volume_mtx;
96 	struct task sc_volume_task;
97 	u_char sc_volume_reg[6];
98 };
99 
100 static int	snapper_probe(device_t);
101 static int 	snapper_attach(device_t);
102 static int	snapper_init(struct snd_mixer *m);
103 static int	snapper_uninit(struct snd_mixer *m);
104 static int	snapper_reinit(struct snd_mixer *m);
105 static int	snapper_set(struct snd_mixer *m, unsigned dev, unsigned left,
106 		    unsigned right);
107 static uint32_t	snapper_setrecsrc(struct snd_mixer *m, uint32_t src);
108 
109 static device_method_t snapper_methods[] = {
110 	/* Device interface. */
111 	DEVMETHOD(device_probe,		snapper_probe),
112 	DEVMETHOD(device_attach,	snapper_attach),
113 	DEVMETHOD_END
114 };
115 
116 static driver_t snapper_driver = {
117 	"snapper",
118 	snapper_methods,
119 	sizeof(struct snapper_softc)
120 };
121 
122 DRIVER_MODULE(snapper, iicbus, snapper_driver, 0, 0);
123 MODULE_VERSION(snapper, 1);
124 MODULE_DEPEND(snapper, iicbus, 1, 1, 1);
125 
126 static kobj_method_t snapper_mixer_methods[] = {
127 	KOBJMETHOD(mixer_init, 		snapper_init),
128 	KOBJMETHOD(mixer_uninit, 	snapper_uninit),
129 	KOBJMETHOD(mixer_reinit, 	snapper_reinit),
130 	KOBJMETHOD(mixer_set, 		snapper_set),
131 	KOBJMETHOD(mixer_setrecsrc, 	snapper_setrecsrc),
132 	KOBJMETHOD_END
133 };
134 
135 MIXER_DECLARE(snapper_mixer);
136 
137 #define SNAPPER_IICADDR	0x6a	/* Hard-coded I2C slave addr */
138 
139 /* Snapper (Texas Instruments TAS3004) registers. */
140 #define SNAPPER_MCR1	0x01	/* Main control register 1 (1byte) */
141 #define SNAPPER_DRC	0x02	/* Dynamic range compression (6bytes) */
142 #define SNAPPER_VOLUME	0x04	/* Volume (6bytes) */
143 #define SNAPPER_TREBLE	0x05	/* Treble control (1byte) */
144 #define SNAPPER_BASS	0x06	/* Bass control (1byte) */
145 #define SNAPPER_MIXER_L	0x07	/* Mixer left gain (9bytes) */
146 #define SNAPPER_MIXER_R	0x08	/* Mixer right gain (9bytes) */
147 #define SNAPPER_LB0	0x0a	/* Left biquad 0 (15bytes) */
148 #define SNAPPER_LB1	0x0b	/* Left biquad 1 (15bytes) */
149 #define SNAPPER_LB2	0x0c	/* Left biquad 2 (15bytes) */
150 #define SNAPPER_LB3	0x0d	/* Left biquad 3 (15bytes) */
151 #define SNAPPER_LB4	0x0e	/* Left biquad 4 (15bytes) */
152 #define SNAPPER_LB5	0x0f	/* Left biquad 5 (15bytes) */
153 #define SNAPPER_LB6	0x10	/* Left biquad 6 (15bytes) */
154 #define SNAPPER_RB0	0x13	/* Right biquad 0 (15bytes) */
155 #define SNAPPER_RB1	0x14	/* Right biquad 1 (15bytes) */
156 #define SNAPPER_RB2	0x15	/* Right biquad 2 (15bytes) */
157 #define SNAPPER_RB3	0x16	/* Right biquad 3 (15bytes) */
158 #define SNAPPER_RB4	0x17	/* Right biquad 4 (15bytes) */
159 #define SNAPPER_RB5	0x18	/* Right biquad 5 (15bytes) */
160 #define SNAPPER_RB6	0x19	/* Right biquad 6 (15bytes) */
161 #define SNAPPER_LLB	0x21	/* Left loudness biquad (15bytes) */
162 #define SNAPPER_RLB	0x22	/* Right loudness biquad (15bytes) */
163 #define SNAPPER_LLB_GAIN	0x23	/* Left loudness biquad gain (3bytes) */
164 #define SNAPPER_RLB_GAIN	0x24	/* Right loudness biquad gain (3bytes) */
165 #define SNAPPER_ACR		0x40	/* Analog control register (1byte) */
166 #define SNAPPER_MCR2	0x43	/* Main control register 2 (1byte) */
167 #define SNAPPER_MCR1_FL	0x80	/* Fast load */
168 #define SNAPPER_MCR1_SC	0x40	/* SCLK frequency */
169 #define  SNAPPER_MCR1_SC_32	0x00	/*  32fs */
170 #define  SNAPPER_MCR1_SC_64	0x40	/*  64fs */
171 #define SNAPPER_MCR1_SM	0x30	/* Output serial port mode */
172 #define  SNAPPER_MCR1_SM_L	0x00	/*  Left justified */
173 #define  SNAPPER_MCR1_SM_R	0x10	/*  Right justified */
174 #define  SNAPPER_MCR1_SM_I2S 0x20	/*  I2S */
175 #define SNAPPER_MCR1_W	0x03	/* Serial port word length */
176 #define  SNAPPER_MCR1_W_16	0x00	/*  16 bit */
177 #define  SNAPPER_MCR1_W_18	0x01	/*  18 bit */
178 #define  SNAPPER_MCR1_W_20	0x02	/*  20 bit */
179 #define  SNAPPER_MCR1_W_24	0x03	/*  24 bit */
180 #define SNAPPER_MCR2_DL	0x80	/* Download */
181 #define SNAPPER_MCR2_AP	0x02	/* All pass mode */
182 #define SNAPPER_ACR_ADM	0x80	/* ADC output mode */
183 #define SNAPPER_ACR_LRB	0x40	/* Select B input */
184 #define SNAPPER_ACR_DM	0x0c	/* De-emphasis control */
185 #define  SNAPPER_ACR_DM_OFF	0x00	/*  off */
186 #define  SNAPPER_ACR_DM_48	0x04	/*  fs = 48kHz */
187 #define  SNAPPER_ACR_DM_44	0x08	/*  fs = 44.1kHz */
188 #define SNAPPER_ACR_INP	0x02	/* Analog input select */
189 #define  SNAPPER_ACR_INP_A	0x00	/*  A */
190 #define  SNAPPER_ACR_INP_B	0x02	/*  B */
191 #define SNAPPER_ACR_APD	0x01	/* Analog power down */
192 
193 struct snapper_reg {
194 	u_char MCR1[1];
195 	u_char DRC[6];
196 	u_char VOLUME[6];
197 	u_char TREBLE[1];
198 	u_char BASS[1];
199 	u_char MIXER_L[9];
200 	u_char MIXER_R[9];
201 	u_char LB0[15];
202 	u_char LB1[15];
203 	u_char LB2[15];
204 	u_char LB3[15];
205 	u_char LB4[15];
206 	u_char LB5[15];
207 	u_char LB6[15];
208 	u_char RB0[15];
209 	u_char RB1[15];
210 	u_char RB2[15];
211 	u_char RB3[15];
212 	u_char RB4[15];
213 	u_char RB5[15];
214 	u_char RB6[15];
215 	u_char LLB[15];
216 	u_char RLB[15];
217 	u_char LLB_GAIN[3];
218 	u_char RLB_GAIN[3];
219 	u_char ACR[1];
220 	u_char MCR2[1];
221 };
222 
223 static const struct snapper_reg snapper_initdata = {
224 	{ SNAPPER_MCR1_SC_64 | SNAPPER_MCR1_SM_I2S |
225 	  SNAPPER_MCR1_W_16 }, 					/* MCR1 */
226 	{ 1, 0, 0, 0, 0, 0 },					/* DRC */
227 	{ 0, 0, 0, 0, 0, 0 },					/* VOLUME */
228 	{ 0x72 },						/* TREBLE */
229 	{ 0x72 },						/* BASS */
230 	{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },			/* MIXER_L */
231 	{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },			/* MIXER_R */
232 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
233 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
234 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
235 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
236 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
237 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
238 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
239 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
240 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
241 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
242 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
243 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
244 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
245 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
246 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
247 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
248 	{ 0, 0, 0 },						/* LLB_GAIN */
249 	{ 0, 0, 0 },						/* RLB_GAIN */
250 	{ SNAPPER_ACR_ADM | SNAPPER_ACR_LRB | SNAPPER_ACR_INP_B },/* ACR */
251 	{ SNAPPER_MCR2_AP }					 /* MCR2 */
252 };
253 
254 static const char snapper_regsize[] = {
255 	0,					/* 0x00 */
256 	sizeof snapper_initdata.MCR1,		/* 0x01 */
257 	sizeof snapper_initdata.DRC,		/* 0x02 */
258 	0,					/* 0x03 */
259 	sizeof snapper_initdata.VOLUME,		/* 0x04 */
260 	sizeof snapper_initdata.TREBLE,		/* 0x05 */
261 	sizeof snapper_initdata.BASS,		/* 0x06 */
262 	sizeof snapper_initdata.MIXER_L,	/* 0x07 */
263 	sizeof snapper_initdata.MIXER_R,	/* 0x08 */
264 	0,					/* 0x09 */
265 	sizeof snapper_initdata.LB0,		/* 0x0a */
266 	sizeof snapper_initdata.LB1,		/* 0x0b */
267 	sizeof snapper_initdata.LB2,		/* 0x0c */
268 	sizeof snapper_initdata.LB3,		/* 0x0d */
269 	sizeof snapper_initdata.LB4,		/* 0x0e */
270 	sizeof snapper_initdata.LB5,		/* 0x0f */
271 	sizeof snapper_initdata.LB6,		/* 0x10 */
272 	0,					/* 0x11 */
273 	0,					/* 0x12 */
274 	sizeof snapper_initdata.RB0,		/* 0x13 */
275 	sizeof snapper_initdata.RB1,		/* 0x14 */
276 	sizeof snapper_initdata.RB2,		/* 0x15 */
277 	sizeof snapper_initdata.RB3,		/* 0x16 */
278 	sizeof snapper_initdata.RB4,		/* 0x17 */
279 	sizeof snapper_initdata.RB5,		/* 0x18 */
280 	sizeof snapper_initdata.RB6,		/* 0x19 */
281 	0,0,0,0, 0,0,
282 	0,					/* 0x20 */
283 	sizeof snapper_initdata.LLB,		/* 0x21 */
284 	sizeof snapper_initdata.RLB,		/* 0x22 */
285 	sizeof snapper_initdata.LLB_GAIN,	/* 0x23 */
286 	sizeof snapper_initdata.RLB_GAIN,	/* 0x24 */
287 	0,0,0,0, 0,0,0,0, 0,0,0,
288 	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
289 	sizeof snapper_initdata.ACR,		/* 0x40 */
290 	0,					/* 0x41 */
291 	0,					/* 0x42 */
292 	sizeof snapper_initdata.MCR2		/* 0x43 */
293 };
294 
295 /* dB = 20 * log (x) table. */
296 static unsigned int	snapper_volume_table[100] = {
297 	0x00000148,   0x0000015C,   0x00000171,   0x00000186,   // -46.0,	-45.5,	-45.0,	-44.5,
298 	0x0000019E,   0x000001B6,   0x000001D0,   0x000001EB,   // -44.0,	-43.5,	-43.0,	-42.5,
299 	0x00000209,   0x00000227,   0x00000248,   0x0000026B,   // -42.0,	-41.5,	-41.0,	-40.5,
300 	0x0000028F,   0x000002B6,   0x000002DF,   0x0000030B,   // -40.0,	-39.5,	-39.0,	-38.5,
301 	0x00000339,   0x0000036A,   0x0000039E,   0x000003D5,   // -38.0,	-37.5,	-37.0,	-36.5,
302 	0x0000040F,   0x0000044C,   0x0000048D,   0x000004D2,   // -36.0,	-35.5,	-35.0,	-34.5,
303 	0x0000051C,   0x00000569,   0x000005BB,   0x00000612,   // -34.0,	-33.5,	-33.0,	-32.5,
304 	0x0000066E,   0x000006D0,   0x00000737,   0x000007A5,   // -32.0,	-31.5,	-31.0,	-30.5,
305 	0x00000818,   0x00000893,   0x00000915,   0x0000099F,   // -30.0,	-29.5,	-29.0,	-28.5,
306 	0x00000A31,   0x00000ACC,   0x00000B6F,   0x00000C1D,   // -28.0,	-27.5,	-27.0,	-26.5,
307 	0x00000CD5,   0x00000D97,   0x00000E65,   0x00000F40,   // -26.0,	-25.5,	-25.0,	-24.5,
308 	0x00001027,   0x0000111C,   0x00001220,   0x00001333,   // -24.0,	-23.5,	-23.0,	-22.5,
309 	0x00001456,   0x0000158A,   0x000016D1,   0x0000182B,   // -22.0,	-21.5,	-21.0,	-20.5,
310 	0x0000199A,   0x00001B1E,   0x00001CB9,   0x00001E6D,   // -20.0,	-19.5,	-19.0,	-18.5,
311 	0x0000203A,   0x00002223,   0x00002429,   0x0000264E,   // -18.0,	-17.5,	-17.0,	-16.5,
312 	0x00002893,   0x00002AFA,   0x00002D86,   0x00003039,   // -16.0,	-15.5,	-15.0,	-14.5,
313 	0x00003314,   0x0000361B,   0x00003950,   0x00003CB5,   // -14.0,	-13.5,	-13.0,	-12.5,
314 	0x0000404E,   0x0000441D,   0x00004827,   0x00004C6D,   // -12.0,	-11.5,	-11.0,	-10.5,
315 	0x000050F4,   0x000055C0,   0x00005AD5,   0x00006037,   // -10.0,	-9.5,	-9.0,	-8.5,
316 	0x000065EA,   0x00006BF4,   0x0000725A,   0x00007920,   // -8.0,	-7.5,	-7.0,	-6.5,
317 	0x0000804E,   0x000087EF,   0x00008FF6,   0x0000987D,   // -6.0,	-5.5,	-5.0,	-4.5,
318 	0x0000A186,   0x0000AB19,   0x0000B53C,   0x0000BFF9,   // -4.0,	-3.5,	-3.0,	-2.5,
319 	0x0000CB59,   0x0000D766,   0x0000E429,   0x0000F1AE,   // -2.0,	-1.5,	-1.0,	-0.5,
320 	0x00010000,   0x00010F2B,   0x00011F3D,   0x00013042,   // 0.0,   +0.5,	+1.0,	+1.5,
321 	0x00014249,   0x00015562,   0x0001699C,   0x00017F09    // 2.0,   +2.5,	+3.0,	+3.5,
322 };
323 
324 static int
325 snapper_write(struct snapper_softc *sc, uint8_t reg, const void *data)
326 {
327 	unsigned int size;
328 	uint8_t buf[16];
329 
330 	struct iic_msg msg[] = {
331 		{ sc->sc_addr, IIC_M_WR, 0, buf }
332 	};
333 
334 	KASSERT(reg < sizeof(snapper_regsize), ("bad reg"));
335 	size = snapper_regsize[reg];
336 	msg[0].len = size + 1;
337 	buf[0] = reg;
338 	memcpy(&buf[1], data, size);
339 
340 	iicbus_transfer(sc->sc_dev, msg, 1);
341 
342 	return (0);
343 }
344 
345 /*
346  * snapper_write() sleeps in iicbus_transfer(), so snapper_set() cannot
347  * program the volume registers inline. Hand the new values to a task
348  * instead, which runs with no lock held.
349  */
350 static void
351 snapper_volume_task(void *arg, int pending __unused)
352 {
353 	struct snapper_softc *sc = arg;
354 	u_char reg[6];
355 
356 	mtx_lock(&sc->sc_volume_mtx);
357 	memcpy(reg, sc->sc_volume_reg, sizeof(reg));
358 	mtx_unlock(&sc->sc_volume_mtx);
359 
360 	snapper_write(sc, SNAPPER_VOLUME, reg);
361 }
362 
363 static int
364 snapper_probe(device_t dev)
365 {
366 	const char *name, *compat;
367 
368 	name = ofw_bus_get_name(dev);
369 	if (name == NULL)
370 		return (ENXIO);
371 
372 	if (strcmp(name, "deq") == 0) {
373 		if (iicbus_get_addr(dev) != SNAPPER_IICADDR)
374 			return (ENXIO);
375 	} else if (strcmp(name, "codec") == 0) {
376 		compat = ofw_bus_get_compat(dev);
377 		if (compat == NULL || strcmp(compat, "tas3004") != 0)
378 			return (ENXIO);
379 	} else {
380 		return (ENXIO);
381 	}
382 
383 	device_set_desc(dev, "Texas Instruments TAS3004 Audio Codec");
384 	return (0);
385 }
386 
387 static int
388 snapper_attach(device_t dev)
389 {
390 	struct snapper_softc *sc;
391 
392 	sc = device_get_softc(dev);
393 	sc->sc_dev = dev;
394 	sc->sc_addr = iicbus_get_addr(dev);
395 
396 	mtx_init(&sc->sc_volume_mtx, "snapper volume", NULL, MTX_DEF);
397 	TASK_INIT(&sc->sc_volume_task, 0, snapper_volume_task, sc);
398 
399 	i2s_mixer_class = &snapper_mixer_class;
400 	i2s_mixer = dev;
401 
402 	return (0);
403 }
404 
405 static int
406 snapper_init(struct snd_mixer *m)
407 {
408 	struct snapper_softc *sc;
409 	unsigned int		x = 0;
410 
411 	sc = device_get_softc(mix_getdevinfo(m));
412 
413         snapper_write(sc, SNAPPER_LB0, snapper_initdata.LB0);
414 	snapper_write(sc, SNAPPER_LB1, snapper_initdata.LB1);
415 	snapper_write(sc, SNAPPER_LB2, snapper_initdata.LB2);
416 	snapper_write(sc, SNAPPER_LB3, snapper_initdata.LB3);
417 	snapper_write(sc, SNAPPER_LB4, snapper_initdata.LB4);
418 	snapper_write(sc, SNAPPER_LB5, snapper_initdata.LB5);
419 	snapper_write(sc, SNAPPER_LB6, snapper_initdata.LB6);
420 	snapper_write(sc, SNAPPER_RB0, snapper_initdata.RB0);
421 	snapper_write(sc, SNAPPER_RB1, snapper_initdata.RB1);
422 	snapper_write(sc, SNAPPER_RB1, snapper_initdata.RB1);
423 	snapper_write(sc, SNAPPER_RB2, snapper_initdata.RB2);
424 	snapper_write(sc, SNAPPER_RB3, snapper_initdata.RB3);
425 	snapper_write(sc, SNAPPER_RB4, snapper_initdata.RB4);
426 	snapper_write(sc, SNAPPER_RB5, snapper_initdata.RB5);
427 	snapper_write(sc, SNAPPER_RB6, snapper_initdata.RB6);
428 	snapper_write(sc, SNAPPER_MCR1, snapper_initdata.MCR1);
429 	snapper_write(sc, SNAPPER_MCR2, snapper_initdata.MCR2);
430 	snapper_write(sc, SNAPPER_DRC, snapper_initdata.DRC);
431 	snapper_write(sc, SNAPPER_VOLUME, snapper_initdata.VOLUME);
432 	snapper_write(sc, SNAPPER_TREBLE, snapper_initdata.TREBLE);
433 	snapper_write(sc, SNAPPER_BASS, snapper_initdata.BASS);
434 	snapper_write(sc, SNAPPER_MIXER_L, snapper_initdata.MIXER_L);
435 	snapper_write(sc, SNAPPER_MIXER_R, snapper_initdata.MIXER_R);
436 	snapper_write(sc, SNAPPER_LLB, snapper_initdata.LLB);
437 	snapper_write(sc, SNAPPER_RLB, snapper_initdata.RLB);
438 	snapper_write(sc, SNAPPER_LLB_GAIN, snapper_initdata.LLB_GAIN);
439 	snapper_write(sc, SNAPPER_RLB_GAIN, snapper_initdata.RLB_GAIN);
440 	snapper_write(sc, SNAPPER_ACR, snapper_initdata.ACR);
441 
442 	x |= SOUND_MASK_VOLUME;
443 	mix_setdevs(m, x);
444 
445 	return (0);
446 }
447 
448 static int
449 snapper_uninit(struct snd_mixer *m)
450 {
451 	struct snapper_softc *sc;
452 
453 	sc = device_get_softc(mix_getdevinfo(m));
454 
455 	taskqueue_drain(taskqueue_thread, &sc->sc_volume_task);
456 
457 	return (0);
458 }
459 
460 static int
461 snapper_reinit(struct snd_mixer *m)
462 {
463 	return (0);
464 }
465 
466 static int
467 snapper_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
468 {
469 	struct snapper_softc *sc;
470 	unsigned int l, r;
471 	u_char reg[6];
472 
473 	sc = device_get_softc(mix_getdevinfo(m));
474 
475 	if (left > 100 || right > 100)
476 		return (0);
477 
478 	l = (left == 0) ? 0 : snapper_volume_table[left - 1];
479 	r = (right == 0) ? 0 : snapper_volume_table[right - 1];
480 
481 	switch (dev) {
482 	case SOUND_MIXER_VOLUME:
483 		reg[0] = (l & 0xff0000) >> 16;
484 		reg[1] = (l & 0x00ff00) >> 8;
485 		reg[2] = l & 0x0000ff;
486 		reg[3] = (r & 0xff0000) >> 16;
487 		reg[4] = (r & 0x00ff00) >> 8;
488 		reg[5] = r & 0x0000ff;
489 
490 		mtx_lock(&sc->sc_volume_mtx);
491 		memcpy(sc->sc_volume_reg, reg, sizeof(reg));
492 		mtx_unlock(&sc->sc_volume_mtx);
493 
494 		taskqueue_enqueue(taskqueue_thread, &sc->sc_volume_task);
495 
496 		return (left | (right << 8));
497 	}
498 
499 	return (0);
500 }
501 
502 static uint32_t
503 snapper_setrecsrc(struct snd_mixer *m, uint32_t src)
504 {
505 	return (0);
506 }
507