xref: /openbsd/sys/arch/macppc/dev/tumbler.c (revision 771fbea0)
1 /*	$OpenBSD: tumbler.c,v 1.9 2016/09/19 06:46:43 ratchov Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001,2003 Tsubai Masanari.  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  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Datasheet is available from
31  * http://focus.ti.com/docs/prod/folders/print/tas3001.html
32  */
33 
34 #include <sys/param.h>
35 #include <sys/audioio.h>
36 #include <sys/device.h>
37 #include <sys/systm.h>
38 
39 #include <dev/audio_if.h>
40 #include <dev/ofw/openfirm.h>
41 #include <macppc/dev/dbdma.h>
42 
43 #include <machine/autoconf.h>
44 
45 #include <macppc/dev/i2svar.h>
46 
47 #ifdef TUMBLER_DEBUG
48 # define DPRINTF printf
49 #else
50 # define DPRINTF while (0) printf
51 #endif
52 
53 /* XXX */
54 #define tumbler_softc i2s_softc
55 
56 /* XXX */
57 int kiic_write(struct device *, int, int, const void *, int);
58 int kiic_writereg(struct device *, int, u_int);
59 
60 void tumbler_init(struct tumbler_softc *);
61 int tumbler_match(struct device *, void *, void *);
62 void tumbler_attach(struct device *, struct device *, void *);
63 void tumbler_defer(struct device *);
64 void tumbler_set_volume(struct tumbler_softc *, int, int);
65 void tumbler_set_bass(struct tumbler_softc *, int);
66 void tumbler_set_treble(struct tumbler_softc *, int);
67 
68 int tas3001_write(struct tumbler_softc *, u_int, const void *);
69 int tas3001_init(struct tumbler_softc *);
70 
71 struct cfattach tumbler_ca = {
72 	sizeof(struct tumbler_softc), tumbler_match, tumbler_attach
73 };
74 struct cfdriver tumbler_cd = {
75 	NULL, "tumbler", DV_DULL
76 };
77 
78 struct audio_hw_if tumbler_hw_if = {
79 	i2s_open,
80 	i2s_close,
81 	i2s_set_params,
82 	i2s_round_blocksize,
83 	NULL,
84 	NULL,
85 	NULL,
86 	NULL,
87 	NULL,
88 	i2s_halt_output,
89 	i2s_halt_input,
90 	NULL,
91 	NULL,
92 	i2s_set_port,
93 	i2s_get_port,
94 	i2s_query_devinfo,
95 	i2s_allocm,		/* allocm */
96 	NULL,
97 	i2s_round_buffersize,
98 	i2s_get_props,
99 	i2s_trigger_output,
100 	i2s_trigger_input
101 };
102 
103 const uint8_t tumbler_trebletab[] = {
104 	0x96,	/* -18dB */
105 	0x94,	/* -17dB */
106 	0x92,	/* -16dB */
107 	0x90,	/* -15dB */
108 	0x8e,	/* -14dB */
109 	0x8c,	/* -13dB */
110 	0x8a,	/* -12dB */
111 	0x88,	/* -11dB */
112 	0x86,	/* -10dB */
113 	0x84,	/* -9dB */
114 	0x82,	/* -8dB */
115 	0x80,	/* -7dB */
116 	0x7e,	/* -6dB */
117 	0x7c,	/* -5dB */
118 	0x7a,	/* -4dB */
119 	0x78,	/* -3dB */
120 	0x76,	/* -2dB */
121 	0x74,	/* -1dB */
122 	0x72,	/* 0dB */
123 	0x70,	/* 1dB */
124 	0x6d,	/* 2dB */
125 	0x6b,	/* 3dB */
126 	0x68,	/* 4dB */
127 	0x65,	/* 5dB */
128 	0x62,	/* 6dB */
129 	0x5e,	/* 7dB */
130 	0x59,	/* 8dB */
131 	0x5a,	/* 9dB */
132 	0x4f,	/* 10dB */
133 	0x49,	/* 11dB */
134 	0x42,	/* 12dB */
135 	0x3a,	/* 13dB */
136 	0x32,	/* 14dB */
137 	0x28,	/* 15dB */
138 	0x1c,	/* 16dB */
139 	0x10,	/* 17dB */
140 	0x01,	/* 18dB */
141 };
142 
143 const uint8_t tumbler_basstab[] = {
144 	0x86,	/* -18dB */
145 	0x7f,	/* -17dB */
146 	0x7a,	/* -16dB */
147 	0x76,	/* -15dB */
148 	0x72,	/* -14dB */
149 	0x6e,	/* -13dB */
150 	0x6b,	/* -12dB */
151 	0x66,	/* -11dB */
152 	0x61,	/* -10dB */
153 	0x5d,	/* -9dB */
154 	0x5a,	/* -8dB */
155 	0x58,	/* -7dB */
156 	0x55,	/* -6dB */
157 	0x53,	/* -5dB */
158 	0x4f,	/* -4dB */
159 	0x4b,	/* -3dB */
160 	0x46,	/* -2dB */
161 	0x42,	/* -1dB */
162 	0x3e,	/* 0dB */
163 	0x3b,	/* 1dB */
164 	0x38,	/* 2dB */
165 	0x35,	/* 3dB */
166 	0x31,	/* 4dB */
167 	0x2e,	/* 5dB */
168 	0x2b,	/* 6dB */
169 	0x28,	/* 7dB */
170 	0x25,	/* 8dB */
171 	0x21,	/* 9dB */
172 	0x1c,	/* 10dB */
173 	0x18,	/* 11dB */
174 	0x16,	/* 12dB */
175 	0x13,	/* 13dB */
176 	0x10,	/* 14dB */
177 	0x0d,	/* 15dB */
178 	0x0a,	/* 16dB */
179 	0x06,	/* 17dB */
180 	0x01,	/* 18dB */
181 };
182 
183 /* TAS3001 registers */
184 #define DEQ_MCR		0x01	/* Main Control Register (1byte) */
185 #define DEQ_DRC		0x02	/* Dynamic Range Compression (2bytes) */
186 #define DEQ_VOLUME	0x04	/* Volume (6bytes) */
187 #define DEQ_TREBLE	0x05	/* Treble Control Register (1byte) */
188 #define DEQ_BASS	0x06	/* Bass Control Register (1byte) */
189 #define DEQ_MIXER1	0x07	/* Mixer 1 (3bytes) */
190 #define DEQ_MIXER2	0x08	/* Mixer 2 (3bytes) */
191 #define DEQ_LB0		0x0a	/* Left Biquad 0 (15bytes) */
192 #define DEQ_LB1		0x0b	/* Left Biquad 1 (15bytes) */
193 #define DEQ_LB2		0x0c	/* Left Biquad 2 (15bytes) */
194 #define DEQ_LB3		0x0d	/* Left Biquad 3 (15bytes) */
195 #define DEQ_LB4		0x0e	/* Left Biquad 4 (15bytes) */
196 #define DEQ_LB5		0x0f	/* Left Biquad 5 (15bytes) */
197 #define DEQ_RB0		0x13	/* Right Biquad 0 (15bytes) */
198 #define DEQ_RB1		0x14	/* Right Biquad 1 (15bytes) */
199 #define DEQ_RB2		0x15	/* Right Biquad 2 (15bytes) */
200 #define DEQ_RB3		0x16	/* Right Biquad 3 (15bytes) */
201 #define DEQ_RB4		0x17	/* Right Biquad 4 (15bytes) */
202 #define DEQ_RB5		0x18	/* Right Biquad 5 (15bytes) */
203 
204 #define DEQ_MCR_FL	0x80	/* Fast load */
205 #define DEQ_MCR_SC	0x40	/* SCLK frequency */
206 #define  DEQ_MCR_SC_32	0x00	/*  32fs */
207 #define  DEQ_MCR_SC_64	0x40	/*  64fs */
208 #define DEQ_MCR_OM	0x30	/* Output serial port mode */
209 #define  DEQ_MCR_OM_L	0x00	/*  Left justified */
210 #define  DEQ_MCR_OM_R	0x10	/*  Right justified */
211 #define  DEQ_MCR_OM_I2S	0x20	/*  I2S */
212 #define DEQ_MCR_IM	0x0c	/* Input serial port mode */
213 #define  DEQ_MCR_IM_L	0x00	/*  Left justified */
214 #define  DEQ_MCR_IM_R	0x04	/*  Right justified */
215 #define  DEQ_MCR_IM_I2S	0x08	/*  I2S */
216 #define DEQ_MCR_W	0x03	/* Serial port word length */
217 #define  DEQ_MCR_W_16	0x00	/*  16 bit */
218 #define  DEQ_MCR_W_18	0x01	/*  18 bit */
219 #define  DEQ_MCR_W_20	0x02	/*  20 bit */
220 
221 #define DEQ_DRC_CR	0xc0	/* Compression ratio */
222 #define  DEQ_DRC_CR_31	0xc0	/*  3:1 */
223 #define DEQ_DRC_EN	0x01	/* Enable DRC */
224 
225 #define DEQ_MCR_I2S	(DEQ_MCR_OM_I2S | DEQ_MCR_IM_I2S)
226 
227 struct tas3001_reg {
228 	u_char MCR[1];
229 	u_char DRC[2];
230 	u_char VOLUME[6];
231 	u_char TREBLE[1];
232 	u_char BASS[1];
233 	u_char MIXER1[3];
234 	u_char MIXER2[3];
235 	u_char LB0[15];
236 	u_char LB1[15];
237 	u_char LB2[15];
238 	u_char LB3[15];
239 	u_char LB4[15];
240 	u_char LB5[15];
241 	u_char RB0[15];
242 	u_char RB1[15];
243 	u_char RB2[15];
244 	u_char RB3[15];
245 	u_char RB4[15];
246 	u_char RB5[15];
247 };
248 
249 int
250 tumbler_match(struct device *parent, void *match, void *aux)
251 {
252 	struct confargs *ca = aux;
253 	int soundbus, soundchip;
254 	char compat[32];
255 
256 	if (strcmp(ca->ca_name, "i2s") != 0)
257 		return (0);
258 
259 	if ((soundbus = OF_child(ca->ca_node)) == 0 ||
260 	    (soundchip = OF_child(soundbus)) == 0)
261 		return (0);
262 
263 	bzero(compat, sizeof compat);
264 	OF_getprop(soundchip, "compatible", compat, sizeof compat);
265 
266 	if (strcmp(compat, "tumbler") != 0)
267 		return (0);
268 
269 	return (1);
270 }
271 
272 void
273 tumbler_attach(struct device *parent, struct device *self, void *aux)
274 {
275 	struct tumbler_softc *sc = (struct tumbler_softc *)self;
276 
277 	sc->sc_setvolume = tumbler_set_volume;
278 	sc->sc_setbass = tumbler_set_bass;
279 	sc->sc_settreble = tumbler_set_treble;
280 
281 	i2s_attach(parent, sc, aux);
282 	config_defer(self, tumbler_defer);
283 }
284 
285 void
286 tumbler_defer(struct device *dev)
287 {
288 	struct tumbler_softc *sc = (struct tumbler_softc *)dev;
289 	struct device *dv;
290 
291 	TAILQ_FOREACH(dv, &alldevs, dv_list)
292 		if (strcmp(dv->dv_cfdata->cf_driver->cd_name, "kiic") == 0 &&
293 		    strcmp(dv->dv_parent->dv_cfdata->cf_driver->cd_name, "macobio") == 0)
294 			sc->sc_i2c = dv;
295 	if (sc->sc_i2c == NULL) {
296 		printf("%s: unable to find i2c\n", sc->sc_dev.dv_xname);
297 		return;
298 	}
299 
300 	/* XXX If i2c has failed to attach, what should we do? */
301 
302 	audio_attach_mi(&tumbler_hw_if, sc, &sc->sc_dev);
303 
304 	tumbler_init(sc);
305 }
306 
307 void
308 tumbler_set_volume(struct tumbler_softc *sc, int left, int right)
309 {
310 	u_char vol[6];
311 
312 	sc->sc_vol_l = left;
313 	sc->sc_vol_r = right;
314 
315 	left <<= 6;	/* XXX for now */
316 	right <<= 6;
317 
318 	vol[0] = left >> 16;
319 	vol[1] = left >> 8;
320 	vol[2] = left;
321 	vol[3] = right >> 16;
322 	vol[4] = right >> 8;
323 	vol[5] = right;
324 
325 	tas3001_write(sc, DEQ_VOLUME, vol);
326 }
327 
328 void
329 tumbler_set_treble(struct tumbler_softc *sc, int value)
330 {
331 	uint8_t reg;
332 
333 	if ((value >= 0) && (value <= 255) && (value != sc->sc_treble)) {
334 		reg = tumbler_trebletab[(value >> 3) + 2];
335 		if (tas3001_write(sc, DEQ_TREBLE, &reg) < 0)
336 			return;
337 		sc->sc_treble = value;
338 	}
339 }
340 
341 void
342 tumbler_set_bass(struct tumbler_softc *sc, int value)
343 {
344 	uint8_t reg;
345 
346 	if ((value >= 0) && (value <= 255) && (value != sc->sc_bass)) {
347 		reg = tumbler_basstab[(value >> 3) + 2];
348 		if (tas3001_write(sc, DEQ_BASS, &reg) < 0)
349 			return;
350 		sc->sc_bass = value;
351 	}
352 }
353 
354 const struct tas3001_reg tas3001_initdata = {
355 	{ DEQ_MCR_SC_64 | DEQ_MCR_I2S | DEQ_MCR_W_20 },		/* MCR */
356 	{ DEQ_DRC_CR_31, 0xa0 },				/* DRC */
357 	{ 0, 0, 0, 0, 0, 0 },					/* VOLUME */
358 	{ 0x72 },						/* TREBLE */
359 	{ 0x3e },						/* BASS */
360 	{ 0x10, 0x00, 0x00 },					/* MIXER1 */
361 	{ 0x00, 0x00, 0x00 },					/* MIXER2 */
362 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
363 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
364 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
365 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
366 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
367 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
368 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
369 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
370 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
371 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
372 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
373 	{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* BIQUAD */
374 };
375 
376 const char tas3001_regsize[] = {
377 	0,					/* 0x00 */
378 	sizeof tas3001_initdata.MCR,		/* 0x01 */
379 	sizeof tas3001_initdata.DRC,		/* 0x02 */
380 	0,					/* 0x03 */
381 	sizeof tas3001_initdata.VOLUME,		/* 0x04 */
382 	sizeof tas3001_initdata.TREBLE,		/* 0x05 */
383 	sizeof tas3001_initdata.BASS,		/* 0x06 */
384 	sizeof tas3001_initdata.MIXER1,		/* 0x07 */
385 	sizeof tas3001_initdata.MIXER2,		/* 0x08 */
386 	0,					/* 0x09 */
387 	sizeof tas3001_initdata.LB0,		/* 0x0a */
388 	sizeof tas3001_initdata.LB1,		/* 0x0b */
389 	sizeof tas3001_initdata.LB2,		/* 0x0c */
390 	sizeof tas3001_initdata.LB3,		/* 0x0d */
391 	sizeof tas3001_initdata.LB4,		/* 0x0e */
392 	sizeof tas3001_initdata.LB5,		/* 0x0f */
393 	0,					/* 0x10 */
394 	0,					/* 0x11 */
395 	0,					/* 0x12 */
396 	sizeof tas3001_initdata.RB0,		/* 0x13 */
397 	sizeof tas3001_initdata.RB1,		/* 0x14 */
398 	sizeof tas3001_initdata.RB2,		/* 0x15 */
399 	sizeof tas3001_initdata.RB3,		/* 0x16 */
400 	sizeof tas3001_initdata.RB4,		/* 0x17 */
401 	sizeof tas3001_initdata.RB5		/* 0x18 */
402 };
403 
404 #define DEQaddr 0x68
405 
406 int
407 tas3001_write(struct tumbler_softc *sc, u_int reg, const void *data)
408 {
409 	int size;
410 
411 	KASSERT(reg < sizeof tas3001_regsize);
412 	size = tas3001_regsize[reg];
413 	KASSERT(size > 0);
414 
415 	if (kiic_write(sc->sc_i2c, DEQaddr, reg, data, size))
416 		return (-1);
417 
418 	return (0);
419 }
420 
421 #define DEQ_WRITE(sc, reg, addr) \
422 	if (tas3001_write(sc, reg, addr)) goto err
423 
424 int
425 tas3001_init(struct tumbler_softc *sc)
426 {
427 	deq_reset(sc);
428 
429 	/* Initialize TAS3001 registers. */
430 	DEQ_WRITE(sc, DEQ_LB0, tas3001_initdata.LB0);
431 	DEQ_WRITE(sc, DEQ_LB1, tas3001_initdata.LB1);
432 	DEQ_WRITE(sc, DEQ_LB2, tas3001_initdata.LB2);
433 	DEQ_WRITE(sc, DEQ_LB3, tas3001_initdata.LB3);
434 	DEQ_WRITE(sc, DEQ_LB4, tas3001_initdata.LB4);
435 	DEQ_WRITE(sc, DEQ_LB5, tas3001_initdata.LB5);
436 	DEQ_WRITE(sc, DEQ_RB0, tas3001_initdata.RB0);
437 	DEQ_WRITE(sc, DEQ_RB1, tas3001_initdata.RB1);
438 	DEQ_WRITE(sc, DEQ_RB1, tas3001_initdata.RB1);
439 	DEQ_WRITE(sc, DEQ_RB2, tas3001_initdata.RB2);
440 	DEQ_WRITE(sc, DEQ_RB3, tas3001_initdata.RB3);
441 	DEQ_WRITE(sc, DEQ_RB4, tas3001_initdata.RB4);
442 	DEQ_WRITE(sc, DEQ_MCR, tas3001_initdata.MCR);
443 	DEQ_WRITE(sc, DEQ_DRC, tas3001_initdata.DRC);
444 	DEQ_WRITE(sc, DEQ_VOLUME, tas3001_initdata.VOLUME);
445 	DEQ_WRITE(sc, DEQ_TREBLE, tas3001_initdata.TREBLE);
446 	DEQ_WRITE(sc, DEQ_BASS, tas3001_initdata.BASS);
447 	DEQ_WRITE(sc, DEQ_MIXER1, tas3001_initdata.MIXER1);
448 	DEQ_WRITE(sc, DEQ_MIXER2, tas3001_initdata.MIXER2);
449 
450 	return (0);
451 err:
452 	printf("%s: tas3001_init: error\n", sc->sc_dev.dv_xname);
453 	return (-1);
454 }
455 
456 void
457 tumbler_init(struct tumbler_softc *sc)
458 {
459 
460 	/* "sample-rates" (44100, 48000) */
461 	i2s_set_rate(sc, 44100);
462 
463 #if 1
464 	/* Enable I2C interrupts. */
465 #define IER 4
466 #define I2C_INT_DATA 0x01
467 #define I2C_INT_ADDR 0x02
468 #define I2C_INT_STOP 0x04
469 	kiic_writereg(sc->sc_i2c, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP);
470 #endif
471 
472 	if (tas3001_init(sc))
473 		return;
474 
475 	tumbler_set_volume(sc, 80, 80);
476 }
477