]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/dev/sound/macio/tumbler.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / dev / sound / macio / tumbler.c
1 /*-
2  * Copyright 2008 by Marco Trillo. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
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: tumbler.c,v 1.28 2008/05/16 03:49:54 macallan Exp
53  *      Id: tumbler.c,v 1.11 2002/10/31 17:42:13 tsubai Exp 
54  */
55
56 /*
57  *      Apple I2S audio controller.
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 <machine/dbdma.h>
69 #include <machine/intr_machdep.h>
70 #include <machine/resource.h>
71 #include <machine/bus.h>
72 #include <machine/pio.h>
73 #include <sys/rman.h>
74
75 #include <dev/iicbus/iicbus.h>
76 #include <dev/iicbus/iiconf.h>
77 #include <dev/ofw/ofw_bus.h>
78
79 #ifdef HAVE_KERNEL_OPTION_HEADERS
80 #include "opt_snd.h"
81 #endif
82
83 #include <dev/sound/pcm/sound.h>
84
85 #include "mixer_if.h"
86
87 extern kobj_class_t i2s_mixer_class;
88 extern device_t i2s_mixer;
89
90 struct tumbler_softc
91 {
92         device_t sc_dev;
93         uint32_t sc_addr;
94 };
95
96 static int      tumbler_probe(device_t);
97 static int      tumbler_attach(device_t);
98 static int      tumbler_init(struct snd_mixer *m);
99 static int      tumbler_uninit(struct snd_mixer *m);
100 static int      tumbler_reinit(struct snd_mixer *m);
101 static int      tumbler_set(struct snd_mixer *m, unsigned dev, unsigned left,
102                     unsigned right);
103 static u_int32_t        tumbler_setrecsrc(struct snd_mixer *m, u_int32_t src);
104
105 static device_method_t tumbler_methods[] = {
106         /* Device interface. */
107         DEVMETHOD(device_probe,         tumbler_probe),
108         DEVMETHOD(device_attach,        tumbler_attach),
109
110         { 0, 0 }
111 };
112
113 static driver_t tumbler_driver = {
114         "tumbler",
115         tumbler_methods,
116         sizeof(struct tumbler_softc)
117 };
118 static devclass_t tumbler_devclass;
119
120 DRIVER_MODULE(tumbler, iicbus, tumbler_driver, tumbler_devclass, 0, 0);
121 MODULE_VERSION(tumbler, 1);
122 MODULE_DEPEND(tumbler, iicbus, 1, 1, 1);
123
124 static kobj_method_t tumbler_mixer_methods[] = {
125         KOBJMETHOD(mixer_init,          tumbler_init),
126         KOBJMETHOD(mixer_uninit,        tumbler_uninit),
127         KOBJMETHOD(mixer_reinit,        tumbler_reinit),
128         KOBJMETHOD(mixer_set,           tumbler_set),
129         KOBJMETHOD(mixer_setrecsrc,     tumbler_setrecsrc),
130         KOBJMETHOD_END
131 };
132
133 MIXER_DECLARE(tumbler_mixer);
134
135 #define TUMBLER_IICADDR         0x68    /* Tumbler I2C slave address */
136
137 /* Tumbler (Texas Instruments TAS3001) registers. */
138 #define TUMBLER_MCR             0x01    /* Main control register (1byte) */
139 #define TUMBLER_DRC             0x02    /* Dynamic Range Compression (2bytes) */
140 #define TUMBLER_VOLUME          0x04    /* Volume (6bytes) */
141 #define TUMBLER_TREBLE          0x05    /* Treble control (1byte) */
142 #define TUMBLER_BASS            0x06    /* Bass control (1byte) */
143 #define TUMBLER_MIXER1          0x07    /* Mixer1 (3bytes) */
144 #define TUMBLER_MIXER2          0x08    /* Mixer2 (3bytes) */
145 #define TUMBLER_LB0             0x0a    /* Left biquad 0 (15bytes) */
146 #define TUMBLER_LB1             0x0b    /* Left biquad 1 (15bytes) */
147 #define TUMBLER_LB2             0x0c    /* Left biquad 2 (15bytes) */
148 #define TUMBLER_LB3             0x0d    /* Left biquad 3 (15bytes) */
149 #define TUMBLER_LB4             0x0e    /* Left biquad 4 (15bytes) */
150 #define TUMBLER_LB5             0x0f    /* Left biquad 5 (15bytes) */
151 #define TUMBLER_RB0             0x13    /* Right biquad 0 (15bytes) */
152 #define TUMBLER_RB1             0x14    /* Right biquad 1 (15bytes) */
153 #define TUMBLER_RB2             0x15    /* Right biquad 2 (15bytes) */
154 #define TUMBLER_RB3             0x16    /* Right biquad 3 (15bytes) */
155 #define TUMBLER_RB4             0x17    /* Right biquad 4 (15bytes) */
156 #define TUMBLER_RB5             0x18    /* Right biquad 5 (15bytes) */
157 #define TUMBLER_MCR_FL  0x80    /* Fast load */
158 #define TUMBLER_MCR_SC  0x40    /* SCLK frequency */
159 #define  TUMBLER_MCR_SC_32      0x00    /*  32fs */
160 #define  TUMBLER_MCR_SC_64      0x40    /*  64fs */
161 #define TUMBLER_MCR_SM  0x30    /* Output serial port mode */
162 #define  TUMBLER_MCR_SM_L       0x00    /*  Left justified */
163 #define  TUMBLER_MCR_SM_R       0x10    /*  Right justified */
164 #define  TUMBLER_MCR_SM_I2S 0x20        /*  I2S */
165 #define TUMBLER_MCR_ISM    0x0C    /* Input serial mode */
166 #define  TUMBLER_MCR_ISM_L 0x00
167 #define  TUMBLER_MCR_ISM_R 0x04
168 #define  TUMBLER_MCR_ISM_I2S 0x08
169 #define TUMBLER_MCR_W   0x03    /* Serial port word length */
170 #define  TUMBLER_MCR_W_16       0x00    /*  16 bit */
171 #define  TUMBLER_MCR_W_18       0x01    /*  18 bit */
172 #define  TUMBLER_MCR_W_20       0x02    /*  20 bit */
173 #define TUMBLER_DRC_COMP_31 0xc0    /* 3:1 compression */
174 #define TUMBLER_DRC_ENABLE  0x01    /* enable DRC */
175 #define TUMBLER_DRC_DEFL_TH 0xa0    /* default compression threshold */
176
177 /*
178  * Tumbler codec.
179  */
180
181 struct tumbler_reg {
182         u_char MCR[1];
183         u_char DRC[2];
184         u_char VOLUME[6];
185         u_char TREBLE[1];
186         u_char BASS[1];
187         u_char MIXER1[3];
188         u_char MIXER2[3];
189         u_char LB0[15];
190         u_char LB1[15];
191         u_char LB2[15];
192         u_char LB3[15];
193         u_char LB4[15];
194         u_char LB5[15];
195         u_char RB0[15];
196         u_char RB1[15];
197         u_char RB2[15];
198         u_char RB3[15];
199         u_char RB4[15];
200         u_char RB5[15];
201 };
202
203 const struct tumbler_reg tumbler_initdata = {
204         { TUMBLER_MCR_SC_64 | TUMBLER_MCR_SM_I2S | 
205           TUMBLER_MCR_ISM_I2S | TUMBLER_MCR_W_16 },             /* MCR */
206         { TUMBLER_DRC_COMP_31, TUMBLER_DRC_DEFL_TH },           /* DRC */
207         { 0, 0, 0, 0, 0, 0 },                                   /* VOLUME */
208         { 0x72 },                                               /* TREBLE */
209         { 0x3e },                                               /* BASS */
210         { 0x10, 0x00, 0x00 },                                   /* MIXER1 */
211         { 0x00, 0x00, 0x00 },                                   /* MIXER2 */
212         { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },     /* BIQUAD */
213         { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },     /* BIQUAD */
214         { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },     /* BIQUAD */
215         { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },     /* BIQUAD */
216         { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },     /* BIQUAD */
217         { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },     /* BIQUAD */
218         { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },     /* BIQUAD */
219         { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },     /* BIQUAD */
220         { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },     /* BIQUAD */
221         { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },     /* BIQUAD */
222         { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },     /* BIQUAD */
223         { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }      /* BIQUAD */
224 };
225
226 const char tumbler_regsize[] = {
227         0,                                      /* 0x00 */
228         sizeof tumbler_initdata.MCR,            /* 0x01 */
229         sizeof tumbler_initdata.DRC,            /* 0x02 */
230         0,                                      /* 0x03 */
231         sizeof tumbler_initdata.VOLUME,         /* 0x04 */
232         sizeof tumbler_initdata.TREBLE,         /* 0x05 */
233         sizeof tumbler_initdata.BASS,           /* 0x06 */
234         sizeof tumbler_initdata.MIXER1,         /* 0x07 */
235         sizeof tumbler_initdata.MIXER2,         /* 0x08 */
236         0,                                      /* 0x09 */
237         sizeof tumbler_initdata.LB0,            /* 0x0a */
238         sizeof tumbler_initdata.LB1,            /* 0x0b */
239         sizeof tumbler_initdata.LB2,            /* 0x0c */
240         sizeof tumbler_initdata.LB3,            /* 0x0d */
241         sizeof tumbler_initdata.LB4,            /* 0x0e */
242         sizeof tumbler_initdata.LB5,            /* 0x0f */
243         0,                                      /* 0x10 */
244         0,                                      /* 0x11 */
245         0,                                      /* 0x12 */
246         sizeof tumbler_initdata.RB0,            /* 0x13 */
247         sizeof tumbler_initdata.RB1,            /* 0x14 */
248         sizeof tumbler_initdata.RB2,            /* 0x15 */
249         sizeof tumbler_initdata.RB3,            /* 0x16 */
250         sizeof tumbler_initdata.RB4,            /* 0x17 */
251         sizeof tumbler_initdata.RB5             /* 0x18 */
252 };
253
254 /* dB = 20 * log (x) table. */
255 static u_int    tumbler_volume_table[100] = {           
256         0x00000148,   0x0000015C,   0x00000171,   0x00000186,   // -46.0,       -45.5,  -45.0,  -44.5,
257         0x0000019E,   0x000001B6,   0x000001D0,   0x000001EB,   // -44.0,       -43.5,  -43.0,  -42.5,
258         0x00000209,   0x00000227,   0x00000248,   0x0000026B,   // -42.0,       -41.5,  -41.0,  -40.5,
259         0x0000028F,   0x000002B6,   0x000002DF,   0x0000030B,   // -40.0,       -39.5,  -39.0,  -38.5,
260         0x00000339,   0x0000036A,   0x0000039E,   0x000003D5,   // -38.0,       -37.5,  -37.0,  -36.5,
261         0x0000040F,   0x0000044C,   0x0000048D,   0x000004D2,   // -36.0,       -35.5,  -35.0,  -34.5,
262         0x0000051C,   0x00000569,   0x000005BB,   0x00000612,   // -34.0,       -33.5,  -33.0,  -32.5,
263         0x0000066E,   0x000006D0,   0x00000737,   0x000007A5,   // -32.0,       -31.5,  -31.0,  -30.5,
264         0x00000818,   0x00000893,   0x00000915,   0x0000099F,   // -30.0,       -29.5,  -29.0,  -28.5,
265         0x00000A31,   0x00000ACC,   0x00000B6F,   0x00000C1D,   // -28.0,       -27.5,  -27.0,  -26.5,
266         0x00000CD5,   0x00000D97,   0x00000E65,   0x00000F40,   // -26.0,       -25.5,  -25.0,  -24.5,
267         0x00001027,   0x0000111C,   0x00001220,   0x00001333,   // -24.0,       -23.5,  -23.0,  -22.5,
268         0x00001456,   0x0000158A,   0x000016D1,   0x0000182B,   // -22.0,       -21.5,  -21.0,  -20.5,
269         0x0000199A,   0x00001B1E,   0x00001CB9,   0x00001E6D,   // -20.0,       -19.5,  -19.0,  -18.5,
270         0x0000203A,   0x00002223,   0x00002429,   0x0000264E,   // -18.0,       -17.5,  -17.0,  -16.5,
271         0x00002893,   0x00002AFA,   0x00002D86,   0x00003039,   // -16.0,       -15.5,  -15.0,  -14.5,
272         0x00003314,   0x0000361B,   0x00003950,   0x00003CB5,   // -14.0,       -13.5,  -13.0,  -12.5,
273         0x0000404E,   0x0000441D,   0x00004827,   0x00004C6D,   // -12.0,       -11.5,  -11.0,  -10.5,
274         0x000050F4,   0x000055C0,   0x00005AD5,   0x00006037,   // -10.0,       -9.5,   -9.0,   -8.5,
275         0x000065EA,   0x00006BF4,   0x0000725A,   0x00007920,   // -8.0,        -7.5,   -7.0,   -6.5,
276         0x0000804E,   0x000087EF,   0x00008FF6,   0x0000987D,   // -6.0,        -5.5,   -5.0,   -4.5,
277         0x0000A186,   0x0000AB19,   0x0000B53C,   0x0000BFF9,   // -4.0,        -3.5,   -3.0,   -2.5,
278         0x0000CB59,   0x0000D766,   0x0000E429,   0x0000F1AE,   // -2.0,        -1.5,   -1.0,   -0.5,
279         0x00010000,   0x00010F2B,   0x00011F3D,   0x00013042,   // 0.0,   +0.5, +1.0,   +1.5,
280         0x00014249,   0x00015562,   0x0001699C,   0x00017F09    // 2.0,   +2.5, +3.0,   +3.5,
281 };
282
283 static int
284 tumbler_write(struct tumbler_softc *sc, uint8_t reg, const void *data)
285 {
286         u_int size;
287         uint8_t buf[16];
288
289         struct iic_msg msg[] = {
290                 { sc->sc_addr, IIC_M_WR, 0, buf }
291         };
292                 
293         KASSERT(reg < sizeof(tumbler_regsize), ("bad reg"));
294         size = tumbler_regsize[reg];
295         msg[0].len = size + 1;
296         buf[0] = reg;
297         memcpy(&buf[1], data, size);
298
299         iicbus_transfer(sc->sc_dev, msg, 1);
300
301         return (0);
302 }
303
304 static int
305 tumbler_probe(device_t dev)
306 {
307         const char *name;
308
309         name = ofw_bus_get_name(dev);
310         if (name == NULL)
311                 return (ENXIO);
312
313         if (strcmp(name, "deq") == 0 && iicbus_get_addr(dev) == 
314             TUMBLER_IICADDR) {
315                 device_set_desc(dev, "Texas Instruments TAS3001 Audio Codec");
316                 return (0);
317         }
318
319         return (ENXIO);
320 }
321
322 static int
323 tumbler_attach(device_t dev)
324 {
325         struct tumbler_softc *sc;
326                 
327         sc = device_get_softc(dev);
328         sc->sc_dev = dev;
329         sc->sc_addr = iicbus_get_addr(dev);
330
331         i2s_mixer_class = &tumbler_mixer_class;
332         i2s_mixer = dev;
333
334         return (0);
335 }
336
337 static int
338 tumbler_init(struct snd_mixer *m)
339 {
340         struct tumbler_softc *sc;
341         u_int           x = 0;
342
343         sc = device_get_softc(mix_getdevinfo(m));
344
345         tumbler_write(sc, TUMBLER_LB0, tumbler_initdata.LB0);
346         tumbler_write(sc, TUMBLER_LB1, tumbler_initdata.LB1);
347         tumbler_write(sc, TUMBLER_LB2, tumbler_initdata.LB2);
348         tumbler_write(sc, TUMBLER_LB3, tumbler_initdata.LB3);
349         tumbler_write(sc, TUMBLER_LB4, tumbler_initdata.LB4);
350         tumbler_write(sc, TUMBLER_LB5, tumbler_initdata.LB5);
351         tumbler_write(sc, TUMBLER_RB0, tumbler_initdata.RB0);
352         tumbler_write(sc, TUMBLER_RB1, tumbler_initdata.RB1);
353         tumbler_write(sc, TUMBLER_RB1, tumbler_initdata.RB1);
354         tumbler_write(sc, TUMBLER_RB2, tumbler_initdata.RB2);
355         tumbler_write(sc, TUMBLER_RB3, tumbler_initdata.RB3);
356         tumbler_write(sc, TUMBLER_RB4, tumbler_initdata.RB4);
357         tumbler_write(sc, TUMBLER_RB5, tumbler_initdata.RB5);
358         tumbler_write(sc, TUMBLER_MCR, tumbler_initdata.MCR);
359         tumbler_write(sc, TUMBLER_DRC, tumbler_initdata.DRC);
360         tumbler_write(sc, TUMBLER_VOLUME, tumbler_initdata.VOLUME);
361         tumbler_write(sc, TUMBLER_TREBLE, tumbler_initdata.TREBLE);
362         tumbler_write(sc, TUMBLER_BASS, tumbler_initdata.BASS);
363         tumbler_write(sc, TUMBLER_MIXER1, tumbler_initdata.MIXER1);
364         tumbler_write(sc, TUMBLER_MIXER2, tumbler_initdata.MIXER2);
365
366         x |= SOUND_MASK_VOLUME;
367         mix_setdevs(m, x);
368
369         return (0);
370 }
371
372 static int
373 tumbler_uninit(struct snd_mixer *m)
374 {
375         return (0);
376 }
377
378 static int
379 tumbler_reinit(struct snd_mixer *m)
380 {
381         return (0);
382 }
383
384 static int
385 tumbler_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
386 {
387         struct tumbler_softc *sc;
388         struct mtx *mixer_lock;
389         int locked;
390         u_int l, r;
391         u_char reg[6];
392
393         sc = device_get_softc(mix_getdevinfo(m));
394         mixer_lock = mixer_get_lock(m);
395         locked = mtx_owned(mixer_lock);
396
397         switch (dev) {
398         case SOUND_MIXER_VOLUME:
399                 if (left > 100 || right > 100)
400                         return (0);
401
402                 l = (left == 0 ? 0 : tumbler_volume_table[left - 1]);
403                 r = (right == 0 ? 0 : tumbler_volume_table[right - 1]);
404                 
405                 reg[0] = (l & 0xff0000) >> 16;
406                 reg[1] = (l & 0x00ff00) >> 8;
407                 reg[2] = l & 0x0000ff;
408                 reg[3] = (r & 0xff0000) >> 16;
409                 reg[4] = (r & 0x00ff00) >> 8;
410                 reg[5] = r & 0x0000ff;
411
412                 /*
413                  * We need to unlock the mixer lock because iicbus_transfer()
414                  * may sleep. The mixer lock itself is unnecessary here
415                  * because it is meant to serialize hardware access, which
416                  * is taken care of by the I2C layer, so this is safe.
417                  */
418
419                 if (locked)
420                         mtx_unlock(mixer_lock);
421
422                 tumbler_write(sc, TUMBLER_VOLUME, reg);
423
424                 if (locked)
425                         mtx_lock(mixer_lock);
426
427                 return (left | (right << 8));
428         }
429
430         return (0);
431 }
432
433 static u_int32_t
434 tumbler_setrecsrc(struct snd_mixer *m, u_int32_t src)
435 {
436         return (0);
437 }
438