sgsmix.c revision 1.4 1 /* $NetBSD: sgsmix.c,v 1.4 2008/05/04 15:26:29 xtraeme Exp $ */
2
3 /*-
4 * Copyright (C) 2005 Michael Lorenz.
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 * a driver for the SGS TDA7433 mixer chip found in Beige PowerMac G3 and
31 * probably others
32 */
33
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: sgsmix.c,v 1.4 2008/05/04 15:26:29 xtraeme Exp $");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
43 #include <sys/sysctl.h>
44
45 #include <uvm/uvm_extern.h>
46
47 #include <dev/i2c/i2cvar.h>
48
49 #include <dev/i2c/sgsmixvar.h>
50 #include "opt_sgsmix.h"
51
52 #ifdef SGSMIX_DEBUG
53 #define DPRINTF printf
54 #else
55 #define DPRINTF while (0) printf
56 #endif
57
58 struct sgsmix_softc {
59 device_t sc_dev;
60 device_t sc_parent;
61 struct i2c_controller *sc_i2c;
62 int sc_node, sc_address;
63 uint8_t sc_regs[7];
64 };
65
66 #define SGSREG_INPUT_SELECT 0
67 #define SGSREG_MASTER_GAIN 1
68 #define SGSREG_BASS_TREBLE 2
69 #define SGSREG_SPEAKER_L 3
70 #define SGSREG_HEADPHONES_L 4
71 #define SGSREG_SPEAKER_R 5
72 #define SGSREG_HEADPHONES_R 6
73
74 static void sgsmix_attach(device_t, device_t, void *);
75 static int sgsmix_match(device_t, cfdata_t, void *);
76 static void sgsmix_setup(struct sgsmix_softc *);
77 static void sgsmix_writereg(struct sgsmix_softc *, int, uint8_t);
78
79 CFATTACH_DECL_NEW(sgsmix, sizeof(struct sgsmix_softc),
80 sgsmix_match, sgsmix_attach, NULL, NULL);
81
82 static int
83 sgsmix_match(device_t parent, cfdata_t cf, void *aux)
84 {
85 struct i2c_attach_args *args = aux;
86 int ret = -1;
87 uint8_t out[2] = {1, 0x20};
88
89 /* see if we can talk to something at address 0x8a */
90 if (args->ia_addr == 0x8a) {
91 iic_acquire_bus(args->ia_tag, 0);
92 ret = iic_exec(args->ia_tag, I2C_OP_WRITE, args->ia_addr,
93 out, 2, NULL, 0, 0);
94 iic_release_bus(args->ia_tag, 0);
95 }
96 return (ret >= 0);
97 }
98
99 static void
100 sgsmix_attach(device_t parent, device_t self, void *aux)
101 {
102 struct sgsmix_softc *sc = device_private(self);
103 struct i2c_attach_args *args = aux;
104
105 sc->sc_dev = self;
106 sc->sc_parent = parent;
107 sc->sc_address = args->ia_addr;
108 aprint_normal(": SGS TDA7433 Basic Audio Processor\n");
109 sc->sc_i2c = (struct i2c_controller *)args->ia_tag;
110 sgsmix_setup(sc);
111 }
112
113 static void
114 sgsmix_setup(struct sgsmix_softc *sc)
115 {
116 int i;
117 uint8_t out[2];
118
119 sc->sc_regs[0] = 9; /* input 1 */
120 sc->sc_regs[1] = 0x20; /* master gain 0dB */
121 sc->sc_regs[2] = 0x77; /* flat bass / treble */
122 sc->sc_regs[3] = 0; /* all speakers full volume */
123 sc->sc_regs[4] = 0;
124 sc->sc_regs[5] = 0;
125 sc->sc_regs[6] = 0;
126
127 iic_acquire_bus(sc->sc_i2c, 0);
128
129 for (i = 0; i < 7; i++) {
130 out[0] = i;
131 out[1] = sc->sc_regs[i];
132 iic_exec(sc->sc_i2c, I2C_OP_WRITE, sc->sc_address, out, 2,
133 NULL, 0, 0);
134 }
135
136 iic_release_bus(sc->sc_i2c, 0);
137 }
138
139 static void
140 sgsmix_writereg(struct sgsmix_softc *sc, int reg, uint8_t val)
141 {
142 uint8_t out[2] = {reg, val};
143
144 if ((reg < 0) || (reg >= 7))
145 return;
146
147 if (sc->sc_regs[reg] == val)
148 return;
149
150 if (iic_exec(sc->sc_i2c, I2C_OP_WRITE, sc->sc_address, out, 2, NULL,
151 0, 0) == 0) {
152 sc->sc_regs[reg] = val;
153 }
154 }
155
156 void
157 sgsmix_set_speaker_vol(void *cookie, int left, int right)
158 {
159 struct sgsmix_softc *sc = cookie;
160
161 DPRINTF("%s: speaker %d %d\n", device_xname(sc->sc_dev), left, right);
162 if (left == 0) {
163 sgsmix_writereg(sc, SGSREG_SPEAKER_L, 0x20);
164 } else {
165 sgsmix_writereg(sc, SGSREG_SPEAKER_L,
166 ((255 - left) >> 3) & 0x1f);
167 }
168
169 if (right == 0) {
170 sgsmix_writereg(sc, SGSREG_SPEAKER_R, 0x20);
171 } else {
172 sgsmix_writereg(sc, SGSREG_SPEAKER_R,
173 ((255 - right) >> 3) & 0x1f);
174 }
175 }
176
177 void
178 sgsmix_set_headphone_vol(void *cookie, int left, int right)
179 {
180 struct sgsmix_softc *sc = cookie;
181
182 DPRINTF("%s: headphones %d %d\n", device_xname(sc->sc_dev), left, right);
183 if (left == 0) {
184 sgsmix_writereg(sc, SGSREG_HEADPHONES_L, 0x20);
185 } else {
186 sgsmix_writereg(sc, SGSREG_HEADPHONES_L,
187 ((255 - left) >> 3) & 0x1f);
188 }
189
190 if (right == 0) {
191 sgsmix_writereg(sc, SGSREG_HEADPHONES_R, 0x20);
192 } else {
193 sgsmix_writereg(sc, SGSREG_HEADPHONES_R,
194 ((255 - right) >> 3) & 0x1f);
195 }
196 }
197
198 void
199 sgsmix_set_bass_treble(void *cookie, int bass, int treble)
200 {
201 struct sgsmix_softc *sc = cookie;
202 uint8_t b, t;
203
204 t = (treble >> 4) & 0xf;
205 if (t & 0x8)
206 t ^= 7;
207 b = bass & 0xf0;
208 if (b & 0x80)
209 b ^= 0x70;
210 DPRINTF("%s: bass/treble %02x %02x\n", device_xname(sc->sc_dev), b, t);
211 sgsmix_writereg(sc, SGSREG_BASS_TREBLE, b | t);
212 }
213