sgsmix.c revision 1.2 1 /* $NetBSD: sgsmix.c,v 1.2 2007/09/02 01:41:29 macallan 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.2 2007/09/02 01:41:29 macallan 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 struct device sc_dev;
60 struct device *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(struct device *, struct device *, void *);
75 static int sgsmix_match(struct device *, struct cfdata *, void *);
76 static void sgsmix_setup(struct sgsmix_softc *);
77 static void sgsmix_writereg(struct sgsmix_softc *, int, uint8_t);
78
79 CFATTACH_DECL(sgsmix, sizeof(struct sgsmix_softc),
80 sgsmix_match, sgsmix_attach, NULL, NULL);
81
82 static int
83 sgsmix_match(struct device *parent, struct cfdata *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(struct device *parent, struct device *self, void *aux)
101 {
102 struct sgsmix_softc *sc = device_private(self);
103 struct i2c_attach_args *args = aux;
104
105 sc->sc_parent = parent;
106 sc->sc_address = args->ia_addr;
107 printf(": SGS TDA7433 Basic Audio Processor\n");
108 sc->sc_i2c = (struct i2c_controller *)args->ia_tag;
109 sgsmix_setup(sc);
110 }
111
112 static void
113 sgsmix_setup(struct sgsmix_softc *sc)
114 {
115 int i;
116 uint8_t out[2];
117
118 sc->sc_regs[0] = 9; /* input 1 */
119 sc->sc_regs[1] = 0x20; /* master gain 0dB */
120 sc->sc_regs[2] = 0x77; /* flat bass / treble */
121 sc->sc_regs[3] = 0; /* all speakers full volume */
122 sc->sc_regs[4] = 0;
123 sc->sc_regs[5] = 0;
124 sc->sc_regs[6] = 0;
125
126 iic_acquire_bus(sc->sc_i2c, 0);
127
128 for (i = 0; i < 7; i++) {
129 out[0] = i;
130 out[1] = sc->sc_regs[i];
131 iic_exec(sc->sc_i2c, I2C_OP_WRITE, sc->sc_address, out, 2,
132 NULL, 0, 0);
133 }
134
135 iic_release_bus(sc->sc_i2c, 0);
136 }
137
138 static void
139 sgsmix_writereg(struct sgsmix_softc *sc, int reg, uint8_t val)
140 {
141 uint8_t out[2] = {reg, val};
142
143 if ((reg < 0) || (reg >= 7))
144 return;
145
146 if (sc->sc_regs[reg] == val)
147 return;
148
149 if (iic_exec(sc->sc_i2c, I2C_OP_WRITE, sc->sc_address, out, 2, NULL,
150 0, 0) == 0) {
151 sc->sc_regs[reg] = val;
152 }
153 }
154
155 void
156 sgsmix_set_speaker_vol(void *cookie, int left, int right)
157 {
158 struct sgsmix_softc *sc = cookie;
159
160 DPRINTF("%s: speaker %d %d\n", sc->sc_dev.dv_xname, left, right);
161 if (left == 0) {
162 sgsmix_writereg(sc, SGSREG_SPEAKER_L, 0x20);
163 } else {
164 sgsmix_writereg(sc, SGSREG_SPEAKER_L,
165 ((255 - left) >> 3) & 0x1f);
166 }
167
168 if (right == 0) {
169 sgsmix_writereg(sc, SGSREG_SPEAKER_R, 0x20);
170 } else {
171 sgsmix_writereg(sc, SGSREG_SPEAKER_R,
172 ((255 - right) >> 3) & 0x1f);
173 }
174 }
175
176 void
177 sgsmix_set_headphone_vol(void *cookie, int left, int right)
178 {
179 struct sgsmix_softc *sc = cookie;
180
181 DPRINTF("%s: headphones %d %d\n", sc->sc_dev.dv_xname, left, right);
182 if (left == 0) {
183 sgsmix_writereg(sc, SGSREG_HEADPHONES_L, 0x20);
184 } else {
185 sgsmix_writereg(sc, SGSREG_HEADPHONES_L,
186 ((255 - left) >> 3) & 0x1f);
187 }
188
189 if (right == 0) {
190 sgsmix_writereg(sc, SGSREG_HEADPHONES_R, 0x20);
191 } else {
192 sgsmix_writereg(sc, SGSREG_HEADPHONES_R,
193 ((255 - right) >> 3) & 0x1f);
194 }
195 }
196
197 void
198 sgsmix_set_bass_treble(void *cookie, int bass, int treble)
199 {
200 struct sgsmix_softc *sc = cookie;
201 uint8_t b, t;
202
203 t = (treble >> 4) & 0xf;
204 if (t & 0x8)
205 t ^= 7;
206 b = bass & 0xf0;
207 if (b & 0x80)
208 b ^= 0x70;
209 DPRINTF("%s: bass/treble %02x %02x\n", sc->sc_dev.dv_xname, b, t);
210 sgsmix_writereg(sc, SGSREG_BASS_TREBLE, b | t);
211 }
212