1187692Snwhitehorn/*-
2187692Snwhitehorn * Copyright 2008 by Marco Trillo. All rights reserved.
3187692Snwhitehorn *
4187692Snwhitehorn * Redistribution and use in source and binary forms, with or without
5187692Snwhitehorn * modification, are permitted provided that the following conditions
6187692Snwhitehorn * are met:
7187692Snwhitehorn * 1. Redistributions of source code must retain the above copyright
8187692Snwhitehorn *    notice, this list of conditions and the following disclaimer.
9187692Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
10187692Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
11187692Snwhitehorn *    documentation and/or other materials provided with the distribution.
12187692Snwhitehorn *
13187692Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14187692Snwhitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15187692Snwhitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16187692Snwhitehorn * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17187692Snwhitehorn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18187692Snwhitehorn * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19187692Snwhitehorn * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20187692Snwhitehorn * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21187692Snwhitehorn * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22187692Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23187692Snwhitehorn * SUCH DAMAGE.
24187692Snwhitehorn *
25187692Snwhitehorn * $FreeBSD: releng/10.2/sys/dev/sound/macio/snapper.c 239245 2012-08-14 05:16:35Z andreast $
26187692Snwhitehorn */
27187692Snwhitehorn/*-
28187692Snwhitehorn * Copyright (c) 2002, 2003 Tsubai Masanari.  All rights reserved.
29187692Snwhitehorn *
30187692Snwhitehorn * Redistribution and use in source and binary forms, with or without
31187692Snwhitehorn * modification, are permitted provided that the following conditions
32187692Snwhitehorn * are met:
33187692Snwhitehorn * 1. Redistributions of source code must retain the above copyright
34187692Snwhitehorn *    notice, this list of conditions and the following disclaimer.
35187692Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
36187692Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
37187692Snwhitehorn *    documentation and/or other materials provided with the distribution.
38187692Snwhitehorn * 3. The name of the author may not be used to endorse or promote products
39187692Snwhitehorn *    derived from this software without specific prior written permission.
40187692Snwhitehorn *
41187692Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
42187692Snwhitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43187692Snwhitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
44187692Snwhitehorn * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
45187692Snwhitehorn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46187692Snwhitehorn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47187692Snwhitehorn * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48187692Snwhitehorn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49187692Snwhitehorn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50187692Snwhitehorn * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51187692Snwhitehorn *
52187692Snwhitehorn * 	NetBSD: snapper.c,v 1.28 2008/05/16 03:49:54 macallan Exp
53187692Snwhitehorn *	Id: snapper.c,v 1.11 2002/10/31 17:42:13 tsubai Exp
54187692Snwhitehorn */
55187692Snwhitehorn
56187692Snwhitehorn/*
57187692Snwhitehorn *	Apple Snapper audio.
58187692Snwhitehorn */
59187692Snwhitehorn
60187692Snwhitehorn#include <sys/param.h>
61187692Snwhitehorn#include <sys/systm.h>
62187692Snwhitehorn#include <sys/kernel.h>
63187692Snwhitehorn#include <sys/module.h>
64187692Snwhitehorn#include <sys/bus.h>
65187692Snwhitehorn#include <sys/malloc.h>
66187692Snwhitehorn#include <sys/lock.h>
67187692Snwhitehorn#include <sys/mutex.h>
68187692Snwhitehorn#include <machine/dbdma.h>
69187692Snwhitehorn#include <machine/intr_machdep.h>
70187692Snwhitehorn#include <machine/resource.h>
71187692Snwhitehorn#include <machine/bus.h>
72187692Snwhitehorn#include <machine/pio.h>
73187692Snwhitehorn#include <sys/rman.h>
74187692Snwhitehorn
75187692Snwhitehorn#include <dev/iicbus/iicbus.h>
76187692Snwhitehorn#include <dev/iicbus/iiconf.h>
77187692Snwhitehorn#include <dev/ofw/ofw_bus.h>
78193640Sariff
79193640Sariff#ifdef HAVE_KERNEL_OPTION_HEADERS
80193640Sariff#include "opt_snd.h"
81193640Sariff#endif
82193640Sariff
83187692Snwhitehorn#include <dev/sound/pcm/sound.h>
84193640Sariff
85187692Snwhitehorn#include "mixer_if.h"
86187692Snwhitehorn
87187692Snwhitehornextern kobj_class_t i2s_mixer_class;
88187692Snwhitehornextern device_t	i2s_mixer;
89187692Snwhitehorn
90187692Snwhitehornstruct snapper_softc
91187692Snwhitehorn{
92187692Snwhitehorn	device_t sc_dev;
93187692Snwhitehorn	uint32_t sc_addr;
94187692Snwhitehorn};
95187692Snwhitehorn
96187692Snwhitehornstatic int	snapper_probe(device_t);
97187692Snwhitehornstatic int 	snapper_attach(device_t);
98187692Snwhitehornstatic int	snapper_init(struct snd_mixer *m);
99193694Sariffstatic int	snapper_uninit(struct snd_mixer *m);
100187692Snwhitehornstatic int	snapper_reinit(struct snd_mixer *m);
101187692Snwhitehornstatic int	snapper_set(struct snd_mixer *m, unsigned dev, unsigned left,
102187692Snwhitehorn		    unsigned right);
103193640Sariffstatic u_int32_t	snapper_setrecsrc(struct snd_mixer *m, u_int32_t src);
104187692Snwhitehorn
105187692Snwhitehornstatic device_method_t snapper_methods[] = {
106187692Snwhitehorn	/* Device interface. */
107187692Snwhitehorn	DEVMETHOD(device_probe,		snapper_probe),
108187692Snwhitehorn	DEVMETHOD(device_attach,	snapper_attach),
109187692Snwhitehorn
110187692Snwhitehorn	{ 0, 0 }
111187692Snwhitehorn};
112187692Snwhitehorn
113187692Snwhitehornstatic driver_t snapper_driver = {
114187692Snwhitehorn	"snapper",
115187692Snwhitehorn	snapper_methods,
116187692Snwhitehorn	sizeof(struct snapper_softc)
117187692Snwhitehorn};
118187692Snwhitehornstatic devclass_t snapper_devclass;
119187692Snwhitehorn
120187692SnwhitehornDRIVER_MODULE(snapper, iicbus, snapper_driver, snapper_devclass, 0, 0);
121187692SnwhitehornMODULE_VERSION(snapper, 1);
122187692SnwhitehornMODULE_DEPEND(snapper, iicbus, 1, 1, 1);
123187692Snwhitehorn
124187692Snwhitehornstatic kobj_method_t snapper_mixer_methods[] = {
125187692Snwhitehorn	KOBJMETHOD(mixer_init, 		snapper_init),
126187692Snwhitehorn	KOBJMETHOD(mixer_uninit, 	snapper_uninit),
127187692Snwhitehorn	KOBJMETHOD(mixer_reinit, 	snapper_reinit),
128187692Snwhitehorn	KOBJMETHOD(mixer_set, 		snapper_set),
129187692Snwhitehorn	KOBJMETHOD(mixer_setrecsrc, 	snapper_setrecsrc),
130193640Sariff	KOBJMETHOD_END
131187692Snwhitehorn};
132187692Snwhitehorn
133187692SnwhitehornMIXER_DECLARE(snapper_mixer);
134187692Snwhitehorn
135187692Snwhitehorn#define SNAPPER_IICADDR	0x6a	/* Hard-coded I2C slave addr */
136187692Snwhitehorn
137187692Snwhitehorn/* Snapper (Texas Instruments TAS3004) registers. */
138187692Snwhitehorn#define SNAPPER_MCR1	0x01	/* Main control register 1 (1byte) */
139187692Snwhitehorn#define SNAPPER_DRC	0x02	/* Dynamic range compression (6bytes) */
140187692Snwhitehorn#define SNAPPER_VOLUME	0x04	/* Volume (6bytes) */
141187692Snwhitehorn#define SNAPPER_TREBLE	0x05	/* Treble control (1byte) */
142187692Snwhitehorn#define SNAPPER_BASS	0x06	/* Bass control (1byte) */
143187692Snwhitehorn#define SNAPPER_MIXER_L	0x07	/* Mixer left gain (9bytes) */
144187692Snwhitehorn#define SNAPPER_MIXER_R	0x08	/* Mixer right gain (9bytes) */
145187692Snwhitehorn#define SNAPPER_LB0	0x0a	/* Left biquad 0 (15bytes) */
146187692Snwhitehorn#define SNAPPER_LB1	0x0b	/* Left biquad 1 (15bytes) */
147187692Snwhitehorn#define SNAPPER_LB2	0x0c	/* Left biquad 2 (15bytes) */
148187692Snwhitehorn#define SNAPPER_LB3	0x0d	/* Left biquad 3 (15bytes) */
149187692Snwhitehorn#define SNAPPER_LB4	0x0e	/* Left biquad 4 (15bytes) */
150187692Snwhitehorn#define SNAPPER_LB5	0x0f	/* Left biquad 5 (15bytes) */
151187692Snwhitehorn#define SNAPPER_LB6	0x10	/* Left biquad 6 (15bytes) */
152187692Snwhitehorn#define SNAPPER_RB0	0x13	/* Right biquad 0 (15bytes) */
153187692Snwhitehorn#define SNAPPER_RB1	0x14	/* Right biquad 1 (15bytes) */
154187692Snwhitehorn#define SNAPPER_RB2	0x15	/* Right biquad 2 (15bytes) */
155187692Snwhitehorn#define SNAPPER_RB3	0x16	/* Right biquad 3 (15bytes) */
156187692Snwhitehorn#define SNAPPER_RB4	0x17	/* Right biquad 4 (15bytes) */
157187692Snwhitehorn#define SNAPPER_RB5	0x18	/* Right biquad 5 (15bytes) */
158187692Snwhitehorn#define SNAPPER_RB6	0x19	/* Right biquad 6 (15bytes) */
159187692Snwhitehorn#define SNAPPER_LLB	0x21	/* Left loudness biquad (15bytes) */
160187692Snwhitehorn#define SNAPPER_RLB	0x22	/* Right loudness biquad (15bytes) */
161187692Snwhitehorn#define SNAPPER_LLB_GAIN	0x23	/* Left loudness biquad gain (3bytes) */
162187692Snwhitehorn#define SNAPPER_RLB_GAIN	0x24	/* Right loudness biquad gain (3bytes) */
163187692Snwhitehorn#define SNAPPER_ACR		0x40	/* Analog control register (1byte) */
164187692Snwhitehorn#define SNAPPER_MCR2	0x43	/* Main control register 2 (1byte) */
165187692Snwhitehorn#define SNAPPER_MCR1_FL	0x80	/* Fast load */
166187692Snwhitehorn#define SNAPPER_MCR1_SC	0x40	/* SCLK frequency */
167187692Snwhitehorn#define  SNAPPER_MCR1_SC_32	0x00	/*  32fs */
168187692Snwhitehorn#define  SNAPPER_MCR1_SC_64	0x40	/*  64fs */
169187692Snwhitehorn#define SNAPPER_MCR1_SM	0x30	/* Output serial port mode */
170187692Snwhitehorn#define  SNAPPER_MCR1_SM_L	0x00	/*  Left justified */
171187692Snwhitehorn#define  SNAPPER_MCR1_SM_R	0x10	/*  Right justified */
172187692Snwhitehorn#define  SNAPPER_MCR1_SM_I2S 0x20	/*  I2S */
173187692Snwhitehorn#define SNAPPER_MCR1_W	0x03	/* Serial port word length */
174187692Snwhitehorn#define  SNAPPER_MCR1_W_16	0x00	/*  16 bit */
175187692Snwhitehorn#define  SNAPPER_MCR1_W_18	0x01	/*  18 bit */
176187692Snwhitehorn#define  SNAPPER_MCR1_W_20	0x02	/*  20 bit */
177239245Sandreast#define  SNAPPER_MCR1_W_24	0x03	/*  24 bit */
178187692Snwhitehorn#define SNAPPER_MCR2_DL	0x80	/* Download */
179187692Snwhitehorn#define SNAPPER_MCR2_AP	0x02	/* All pass mode */
180187692Snwhitehorn#define SNAPPER_ACR_ADM	0x80	/* ADC output mode */
181187692Snwhitehorn#define SNAPPER_ACR_LRB	0x40	/* Select B input */
182187692Snwhitehorn#define SNAPPER_ACR_DM	0x0c	/* De-emphasis control */
183187692Snwhitehorn#define  SNAPPER_ACR_DM_OFF	0x00	/*  off */
184187692Snwhitehorn#define  SNAPPER_ACR_DM_48	0x04	/*  fs = 48kHz */
185187692Snwhitehorn#define  SNAPPER_ACR_DM_44	0x08	/*  fs = 44.1kHz */
186187692Snwhitehorn#define SNAPPER_ACR_INP	0x02	/* Analog input select */
187187692Snwhitehorn#define  SNAPPER_ACR_INP_A	0x00	/*  A */
188187692Snwhitehorn#define  SNAPPER_ACR_INP_B	0x02	/*  B */
189187692Snwhitehorn#define SNAPPER_ACR_APD	0x01	/* Analog power down */
190187692Snwhitehorn
191187692Snwhitehorn
192187692Snwhitehornstruct snapper_reg {
193187692Snwhitehorn	u_char MCR1[1];
194187692Snwhitehorn	u_char DRC[6];
195187692Snwhitehorn	u_char VOLUME[6];
196187692Snwhitehorn	u_char TREBLE[1];
197187692Snwhitehorn	u_char BASS[1];
198187692Snwhitehorn	u_char MIXER_L[9];
199187692Snwhitehorn	u_char MIXER_R[9];
200187692Snwhitehorn	u_char LB0[15];
201187692Snwhitehorn	u_char LB1[15];
202187692Snwhitehorn	u_char LB2[15];
203187692Snwhitehorn	u_char LB3[15];
204187692Snwhitehorn	u_char LB4[15];
205187692Snwhitehorn	u_char LB5[15];
206187692Snwhitehorn	u_char LB6[15];
207187692Snwhitehorn	u_char RB0[15];
208187692Snwhitehorn	u_char RB1[15];
209187692Snwhitehorn	u_char RB2[15];
210187692Snwhitehorn	u_char RB3[15];
211187692Snwhitehorn	u_char RB4[15];
212187692Snwhitehorn	u_char RB5[15];
213187692Snwhitehorn	u_char RB6[15];
214187692Snwhitehorn	u_char LLB[15];
215187692Snwhitehorn	u_char RLB[15];
216187692Snwhitehorn	u_char LLB_GAIN[3];
217187692Snwhitehorn	u_char RLB_GAIN[3];
218187692Snwhitehorn	u_char ACR[1];
219187692Snwhitehorn	u_char MCR2[1];
220187692Snwhitehorn};
221187692Snwhitehorn
222187692Snwhitehornstatic const struct snapper_reg snapper_initdata = {
223187692Snwhitehorn	{ SNAPPER_MCR1_SC_64 | SNAPPER_MCR1_SM_I2S |
224187692Snwhitehorn	  SNAPPER_MCR1_W_16 }, 					/* MCR1 */
225187692Snwhitehorn	{ 1, 0, 0, 0, 0, 0 },					/* DRC */
226187692Snwhitehorn	{ 0, 0, 0, 0, 0, 0 },					/* VOLUME */
227187692Snwhitehorn	{ 0x72 },						/* TREBLE */
228187692Snwhitehorn	{ 0x72 },						/* BASS */
229187692Snwhitehorn	{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },			/* MIXER_L */
230187692Snwhitehorn	{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },			/* MIXER_R */
231187692Snwhitehorn	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
232187692Snwhitehorn	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
233187692Snwhitehorn	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
234187692Snwhitehorn	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
235187692Snwhitehorn	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
236187692Snwhitehorn	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
237187692Snwhitehorn	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
238187692Snwhitehorn	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
239187692Snwhitehorn	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
240187692Snwhitehorn	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
241187692Snwhitehorn	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
242187692Snwhitehorn	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
243187692Snwhitehorn	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
244187692Snwhitehorn	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
245187692Snwhitehorn	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
246187692Snwhitehorn	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
247187692Snwhitehorn	{ 0, 0, 0 },						/* LLB_GAIN */
248187692Snwhitehorn	{ 0, 0, 0 },						/* RLB_GAIN */
249187692Snwhitehorn	{ SNAPPER_ACR_ADM | SNAPPER_ACR_LRB | SNAPPER_ACR_INP_B },/* ACR */
250187692Snwhitehorn	{ SNAPPER_MCR2_AP }					 /* MCR2 */
251187692Snwhitehorn};
252187692Snwhitehorn
253187692Snwhitehornstatic const char snapper_regsize[] = {
254187692Snwhitehorn	0,					/* 0x00 */
255187692Snwhitehorn	sizeof snapper_initdata.MCR1,		/* 0x01 */
256187692Snwhitehorn	sizeof snapper_initdata.DRC,		/* 0x02 */
257187692Snwhitehorn	0,					/* 0x03 */
258187692Snwhitehorn	sizeof snapper_initdata.VOLUME,		/* 0x04 */
259187692Snwhitehorn	sizeof snapper_initdata.TREBLE,		/* 0x05 */
260187692Snwhitehorn	sizeof snapper_initdata.BASS,		/* 0x06 */
261187692Snwhitehorn	sizeof snapper_initdata.MIXER_L,	/* 0x07 */
262187692Snwhitehorn	sizeof snapper_initdata.MIXER_R,	/* 0x08 */
263187692Snwhitehorn	0,					/* 0x09 */
264187692Snwhitehorn	sizeof snapper_initdata.LB0,		/* 0x0a */
265187692Snwhitehorn	sizeof snapper_initdata.LB1,		/* 0x0b */
266187692Snwhitehorn	sizeof snapper_initdata.LB2,		/* 0x0c */
267187692Snwhitehorn	sizeof snapper_initdata.LB3,		/* 0x0d */
268187692Snwhitehorn	sizeof snapper_initdata.LB4,		/* 0x0e */
269187692Snwhitehorn	sizeof snapper_initdata.LB5,		/* 0x0f */
270187692Snwhitehorn	sizeof snapper_initdata.LB6,		/* 0x10 */
271187692Snwhitehorn	0,					/* 0x11 */
272187692Snwhitehorn	0,					/* 0x12 */
273187692Snwhitehorn	sizeof snapper_initdata.RB0,		/* 0x13 */
274187692Snwhitehorn	sizeof snapper_initdata.RB1,		/* 0x14 */
275187692Snwhitehorn	sizeof snapper_initdata.RB2,		/* 0x15 */
276187692Snwhitehorn	sizeof snapper_initdata.RB3,		/* 0x16 */
277187692Snwhitehorn	sizeof snapper_initdata.RB4,		/* 0x17 */
278187692Snwhitehorn	sizeof snapper_initdata.RB5,		/* 0x18 */
279187692Snwhitehorn	sizeof snapper_initdata.RB6,		/* 0x19 */
280187692Snwhitehorn	0,0,0,0, 0,0,
281187692Snwhitehorn	0,					/* 0x20 */
282187692Snwhitehorn	sizeof snapper_initdata.LLB,		/* 0x21 */
283187692Snwhitehorn	sizeof snapper_initdata.RLB,		/* 0x22 */
284187692Snwhitehorn	sizeof snapper_initdata.LLB_GAIN,	/* 0x23 */
285187692Snwhitehorn	sizeof snapper_initdata.RLB_GAIN,	/* 0x24 */
286187692Snwhitehorn	0,0,0,0, 0,0,0,0, 0,0,0,
287187692Snwhitehorn	0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
288187692Snwhitehorn	sizeof snapper_initdata.ACR,		/* 0x40 */
289187692Snwhitehorn	0,					/* 0x41 */
290187692Snwhitehorn	0,					/* 0x42 */
291187692Snwhitehorn	sizeof snapper_initdata.MCR2		/* 0x43 */
292187692Snwhitehorn};
293187692Snwhitehorn
294187692Snwhitehorn/* dB = 20 * log (x) table. */
295187692Snwhitehornstatic u_int	snapper_volume_table[100] = {
296187692Snwhitehorn	0x00000148,   0x0000015C,   0x00000171,   0x00000186,   // -46.0,	-45.5,	-45.0,	-44.5,
297187692Snwhitehorn	0x0000019E,   0x000001B6,   0x000001D0,   0x000001EB,   // -44.0,	-43.5,	-43.0,	-42.5,
298187692Snwhitehorn	0x00000209,   0x00000227,   0x00000248,   0x0000026B,   // -42.0,	-41.5,	-41.0,	-40.5,
299187692Snwhitehorn	0x0000028F,   0x000002B6,   0x000002DF,   0x0000030B,   // -40.0,	-39.5,	-39.0,	-38.5,
300187692Snwhitehorn	0x00000339,   0x0000036A,   0x0000039E,   0x000003D5,   // -38.0,	-37.5,	-37.0,	-36.5,
301187692Snwhitehorn	0x0000040F,   0x0000044C,   0x0000048D,   0x000004D2,   // -36.0,	-35.5,	-35.0,	-34.5,
302187692Snwhitehorn	0x0000051C,   0x00000569,   0x000005BB,   0x00000612,   // -34.0,	-33.5,	-33.0,	-32.5,
303187692Snwhitehorn	0x0000066E,   0x000006D0,   0x00000737,   0x000007A5,   // -32.0,	-31.5,	-31.0,	-30.5,
304187692Snwhitehorn	0x00000818,   0x00000893,   0x00000915,   0x0000099F,   // -30.0,	-29.5,	-29.0,	-28.5,
305187692Snwhitehorn	0x00000A31,   0x00000ACC,   0x00000B6F,   0x00000C1D,   // -28.0,	-27.5,	-27.0,	-26.5,
306187692Snwhitehorn	0x00000CD5,   0x00000D97,   0x00000E65,   0x00000F40,   // -26.0,	-25.5,	-25.0,	-24.5,
307187692Snwhitehorn	0x00001027,   0x0000111C,   0x00001220,   0x00001333,   // -24.0,	-23.5,	-23.0,	-22.5,
308187692Snwhitehorn	0x00001456,   0x0000158A,   0x000016D1,   0x0000182B,   // -22.0,	-21.5,	-21.0,	-20.5,
309187692Snwhitehorn	0x0000199A,   0x00001B1E,   0x00001CB9,   0x00001E6D,   // -20.0,	-19.5,	-19.0,	-18.5,
310187692Snwhitehorn	0x0000203A,   0x00002223,   0x00002429,   0x0000264E,   // -18.0,	-17.5,	-17.0,	-16.5,
311187692Snwhitehorn	0x00002893,   0x00002AFA,   0x00002D86,   0x00003039,   // -16.0,	-15.5,	-15.0,	-14.5,
312187692Snwhitehorn	0x00003314,   0x0000361B,   0x00003950,   0x00003CB5,   // -14.0,	-13.5,	-13.0,	-12.5,
313187692Snwhitehorn	0x0000404E,   0x0000441D,   0x00004827,   0x00004C6D,   // -12.0,	-11.5,	-11.0,	-10.5,
314187692Snwhitehorn	0x000050F4,   0x000055C0,   0x00005AD5,   0x00006037,   // -10.0,	-9.5,	-9.0,	-8.5,
315187692Snwhitehorn	0x000065EA,   0x00006BF4,   0x0000725A,   0x00007920,   // -8.0,	-7.5,	-7.0,	-6.5,
316187692Snwhitehorn	0x0000804E,   0x000087EF,   0x00008FF6,   0x0000987D,   // -6.0,	-5.5,	-5.0,	-4.5,
317187692Snwhitehorn	0x0000A186,   0x0000AB19,   0x0000B53C,   0x0000BFF9,   // -4.0,	-3.5,	-3.0,	-2.5,
318187692Snwhitehorn	0x0000CB59,   0x0000D766,   0x0000E429,   0x0000F1AE,   // -2.0,	-1.5,	-1.0,	-0.5,
319187692Snwhitehorn	0x00010000,   0x00010F2B,   0x00011F3D,   0x00013042,   // 0.0,   +0.5,	+1.0,	+1.5,
320187692Snwhitehorn	0x00014249,   0x00015562,   0x0001699C,   0x00017F09    // 2.0,   +2.5,	+3.0,	+3.5,
321187692Snwhitehorn};
322187692Snwhitehorn
323187692Snwhitehornstatic int
324187692Snwhitehornsnapper_write(struct snapper_softc *sc, uint8_t reg, const void *data)
325187692Snwhitehorn{
326187692Snwhitehorn	u_int size;
327187692Snwhitehorn	uint8_t buf[16];
328187692Snwhitehorn
329187692Snwhitehorn	struct iic_msg msg[] = {
330187692Snwhitehorn		{ sc->sc_addr, IIC_M_WR, 0, buf }
331187692Snwhitehorn	};
332187692Snwhitehorn
333187692Snwhitehorn	KASSERT(reg < sizeof(snapper_regsize), ("bad reg"));
334187692Snwhitehorn	size = snapper_regsize[reg];
335187692Snwhitehorn	msg[0].len = size + 1;
336187692Snwhitehorn	buf[0] = reg;
337187692Snwhitehorn	memcpy(&buf[1], data, size);
338187692Snwhitehorn
339187692Snwhitehorn	iicbus_transfer(sc->sc_dev, msg, 1);
340187692Snwhitehorn
341187692Snwhitehorn	return (0);
342187692Snwhitehorn}
343187692Snwhitehorn
344187692Snwhitehornstatic int
345187692Snwhitehornsnapper_probe(device_t dev)
346187692Snwhitehorn{
347187692Snwhitehorn	const char *name, *compat;
348187692Snwhitehorn
349187692Snwhitehorn	name = ofw_bus_get_name(dev);
350187692Snwhitehorn	if (name == NULL)
351187692Snwhitehorn		return (ENXIO);
352187692Snwhitehorn
353187692Snwhitehorn	if (strcmp(name, "deq") == 0) {
354187692Snwhitehorn		if (iicbus_get_addr(dev) != SNAPPER_IICADDR)
355187692Snwhitehorn			return (ENXIO);
356187692Snwhitehorn	} else if (strcmp(name, "codec") == 0) {
357187692Snwhitehorn		compat = ofw_bus_get_compat(dev);
358187692Snwhitehorn		if (compat == NULL || strcmp(compat, "tas3004") != 0)
359187692Snwhitehorn			return (ENXIO);
360187692Snwhitehorn	} else {
361187692Snwhitehorn		return (ENXIO);
362187692Snwhitehorn	}
363187692Snwhitehorn
364187692Snwhitehorn	device_set_desc(dev, "Texas Instruments TAS3004 Audio Codec");
365187692Snwhitehorn	return (0);
366187692Snwhitehorn}
367187692Snwhitehorn
368187692Snwhitehornstatic int
369187692Snwhitehornsnapper_attach(device_t dev)
370187692Snwhitehorn{
371187692Snwhitehorn	struct snapper_softc *sc;
372187692Snwhitehorn
373187692Snwhitehorn	sc = device_get_softc(dev);
374187692Snwhitehorn	sc->sc_dev = dev;
375187692Snwhitehorn	sc->sc_addr = iicbus_get_addr(dev);
376187692Snwhitehorn
377187692Snwhitehorn	i2s_mixer_class = &snapper_mixer_class;
378187692Snwhitehorn	i2s_mixer = dev;
379187692Snwhitehorn
380187692Snwhitehorn	return (0);
381187692Snwhitehorn}
382187692Snwhitehorn
383187692Snwhitehornstatic int
384187692Snwhitehornsnapper_init(struct snd_mixer *m)
385187692Snwhitehorn{
386187692Snwhitehorn	struct snapper_softc *sc;
387187692Snwhitehorn	u_int		x = 0;
388187692Snwhitehorn
389187692Snwhitehorn	sc = device_get_softc(mix_getdevinfo(m));
390187692Snwhitehorn
391187692Snwhitehorn        snapper_write(sc, SNAPPER_LB0, snapper_initdata.LB0);
392187692Snwhitehorn	snapper_write(sc, SNAPPER_LB1, snapper_initdata.LB1);
393187692Snwhitehorn	snapper_write(sc, SNAPPER_LB2, snapper_initdata.LB2);
394187692Snwhitehorn	snapper_write(sc, SNAPPER_LB3, snapper_initdata.LB3);
395187692Snwhitehorn	snapper_write(sc, SNAPPER_LB4, snapper_initdata.LB4);
396187692Snwhitehorn	snapper_write(sc, SNAPPER_LB5, snapper_initdata.LB5);
397187692Snwhitehorn	snapper_write(sc, SNAPPER_LB6, snapper_initdata.LB6);
398187692Snwhitehorn	snapper_write(sc, SNAPPER_RB0, snapper_initdata.RB0);
399187692Snwhitehorn	snapper_write(sc, SNAPPER_RB1, snapper_initdata.RB1);
400187692Snwhitehorn	snapper_write(sc, SNAPPER_RB1, snapper_initdata.RB1);
401187692Snwhitehorn	snapper_write(sc, SNAPPER_RB2, snapper_initdata.RB2);
402187692Snwhitehorn	snapper_write(sc, SNAPPER_RB3, snapper_initdata.RB3);
403187692Snwhitehorn	snapper_write(sc, SNAPPER_RB4, snapper_initdata.RB4);
404187692Snwhitehorn	snapper_write(sc, SNAPPER_RB5, snapper_initdata.RB5);
405187692Snwhitehorn	snapper_write(sc, SNAPPER_RB6, snapper_initdata.RB6);
406187692Snwhitehorn	snapper_write(sc, SNAPPER_MCR1, snapper_initdata.MCR1);
407187692Snwhitehorn	snapper_write(sc, SNAPPER_MCR2, snapper_initdata.MCR2);
408187692Snwhitehorn	snapper_write(sc, SNAPPER_DRC, snapper_initdata.DRC);
409187692Snwhitehorn	snapper_write(sc, SNAPPER_VOLUME, snapper_initdata.VOLUME);
410187692Snwhitehorn	snapper_write(sc, SNAPPER_TREBLE, snapper_initdata.TREBLE);
411187692Snwhitehorn	snapper_write(sc, SNAPPER_BASS, snapper_initdata.BASS);
412187692Snwhitehorn	snapper_write(sc, SNAPPER_MIXER_L, snapper_initdata.MIXER_L);
413187692Snwhitehorn	snapper_write(sc, SNAPPER_MIXER_R, snapper_initdata.MIXER_R);
414187692Snwhitehorn	snapper_write(sc, SNAPPER_LLB, snapper_initdata.LLB);
415187692Snwhitehorn	snapper_write(sc, SNAPPER_RLB, snapper_initdata.RLB);
416187692Snwhitehorn	snapper_write(sc, SNAPPER_LLB_GAIN, snapper_initdata.LLB_GAIN);
417187692Snwhitehorn	snapper_write(sc, SNAPPER_RLB_GAIN, snapper_initdata.RLB_GAIN);
418187692Snwhitehorn	snapper_write(sc, SNAPPER_ACR, snapper_initdata.ACR);
419187692Snwhitehorn
420187692Snwhitehorn	x |= SOUND_MASK_VOLUME;
421187692Snwhitehorn	mix_setdevs(m, x);
422187692Snwhitehorn
423187692Snwhitehorn	return (0);
424187692Snwhitehorn}
425187692Snwhitehorn
426193694Sariffstatic int
427187692Snwhitehornsnapper_uninit(struct snd_mixer *m)
428187692Snwhitehorn{
429193694Sariff	return (0);
430187692Snwhitehorn}
431187692Snwhitehorn
432187692Snwhitehornstatic int
433187692Snwhitehornsnapper_reinit(struct snd_mixer *m)
434187692Snwhitehorn{
435187692Snwhitehorn	return (0);
436187692Snwhitehorn}
437187692Snwhitehorn
438187692Snwhitehornstatic int
439187692Snwhitehornsnapper_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
440187692Snwhitehorn{
441187692Snwhitehorn	struct snapper_softc *sc;
442188259Snwhitehorn	struct mtx *mixer_lock;
443188259Snwhitehorn	int locked;
444187692Snwhitehorn	u_int l, r;
445187692Snwhitehorn	u_char reg[6];
446187692Snwhitehorn
447187692Snwhitehorn	sc = device_get_softc(mix_getdevinfo(m));
448188259Snwhitehorn	mixer_lock = mixer_get_lock(m);
449188259Snwhitehorn	locked = mtx_owned(mixer_lock);
450187692Snwhitehorn
451187692Snwhitehorn	if (left > 100 || right > 100)
452187692Snwhitehorn		return (0);
453187692Snwhitehorn
454187692Snwhitehorn	l = (left == 0) ? 0 : snapper_volume_table[left - 1];
455187692Snwhitehorn	r = (right == 0) ? 0 : snapper_volume_table[right - 1];
456187692Snwhitehorn
457187692Snwhitehorn	switch (dev) {
458187692Snwhitehorn	case SOUND_MIXER_VOLUME:
459187692Snwhitehorn		reg[0] = (l & 0xff0000) >> 16;
460187692Snwhitehorn		reg[1] = (l & 0x00ff00) >> 8;
461187692Snwhitehorn		reg[2] = l & 0x0000ff;
462187692Snwhitehorn		reg[3] = (r & 0xff0000) >> 16;
463187692Snwhitehorn		reg[4] = (r & 0x00ff00) >> 8;
464187692Snwhitehorn		reg[5] = r & 0x0000ff;
465188259Snwhitehorn
466188259Snwhitehorn		/*
467188259Snwhitehorn		 * We need to unlock the mixer lock because iicbus_transfer()
468188259Snwhitehorn		 * may sleep. The mixer lock itself is unnecessary here
469188259Snwhitehorn		 * because it is meant to serialize hardware access, which
470188259Snwhitehorn		 * is taken care of by the I2C layer, so this is safe.
471188259Snwhitehorn		 */
472188259Snwhitehorn
473188259Snwhitehorn		if (locked)
474188259Snwhitehorn			mtx_unlock(mixer_lock);
475188259Snwhitehorn
476187692Snwhitehorn		snapper_write(sc, SNAPPER_VOLUME, reg);
477187692Snwhitehorn
478188259Snwhitehorn		if (locked)
479188259Snwhitehorn			mtx_lock(mixer_lock);
480188259Snwhitehorn
481187692Snwhitehorn		return (left | (right << 8));
482187692Snwhitehorn	}
483187692Snwhitehorn
484187692Snwhitehorn	return (0);
485187692Snwhitehorn}
486187692Snwhitehorn
487193640Sariffstatic u_int32_t
488187692Snwhitehornsnapper_setrecsrc(struct snd_mixer *m, u_int32_t src)
489187692Snwhitehorn{
490187692Snwhitehorn	return (0);
491187692Snwhitehorn}
492187692Snwhitehorn
493