xref: /netbsd-src/sys/dev/i2c/sgsmix.c (revision 4ea2e44bb61ff86f86e3be96ca8b9926e20b512f)
1 /*	$NetBSD: sgsmix.c,v 1.11 2024/05/18 00:02:04 thorpej 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*
28  * a driver for the SGS TDA7433 mixer chip found in Beige PowerMac G3 and
29  * probably others
30  */
31 
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: sgsmix.c,v 1.11 2024/05/18 00:02:04 thorpej Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
40 #include <sys/sysctl.h>
41 
42 #include <dev/i2c/i2cvar.h>
43 
44 #include <dev/i2c/sgsmixvar.h>
45 #include "opt_sgsmix.h"
46 
47 #ifdef SGSMIX_DEBUG
48 #define DPRINTF printf
49 #else
50 #define DPRINTF while (0) printf
51 #endif
52 
53 struct sgsmix_softc {
54 	device_t sc_dev;
55 	device_t sc_parent;
56 	i2c_tag_t sc_i2c;
57 	int sc_node, sc_address;
58 	uint8_t sc_regs[7];
59 };
60 
61 #define SGSREG_INPUT_SELECT	0
62 #define SGSREG_MASTER_GAIN	1
63 #define SGSREG_BASS_TREBLE	2
64 #define SGSREG_SPEAKER_L	3
65 #define SGSREG_HEADPHONES_L	4
66 #define SGSREG_SPEAKER_R	5
67 #define SGSREG_HEADPHONES_R	6
68 
69 static void sgsmix_attach(device_t, device_t, void *);
70 static int sgsmix_match(device_t, cfdata_t, void *);
71 static void sgsmix_setup(struct sgsmix_softc *);
72 static void sgsmix_writereg(struct sgsmix_softc *, int, uint8_t);
73 
74 CFATTACH_DECL_NEW(sgsmix, sizeof(struct sgsmix_softc),
75     sgsmix_match, sgsmix_attach, NULL, NULL);
76 
77 static const struct device_compatible_entry compat_data[] = {
78 	{ .compat = "st,tda7433" },
79 	DEVICE_COMPAT_EOL
80 };
81 
82 static int
sgsmix_match(device_t parent,cfdata_t cf,void * aux)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 	int match_result;
89 
90 	if (iic_use_direct_match(args, cf, compat_data, &match_result))
91 		return match_result;
92 
93 	/* see if we can talk to something at address 0x8a */
94 	if (args->ia_addr != 0x8a)
95 		return 0;
96 
97 	iic_acquire_bus(args->ia_tag, 0);
98 	ret = iic_exec(args->ia_tag, I2C_OP_WRITE, args->ia_addr,
99 	    out, 2, NULL, 0, 0);
100 	iic_release_bus(args->ia_tag, 0);
101 
102 	return (ret >= 0) ? I2C_MATCH_ADDRESS_AND_PROBE : 0;
103 }
104 
105 static void
sgsmix_attach(device_t parent,device_t self,void * aux)106 sgsmix_attach(device_t parent, device_t self, void *aux)
107 {
108 	struct sgsmix_softc *sc = device_private(self);
109 	struct i2c_attach_args *args = aux;
110 
111 	sc->sc_dev = self;
112 	sc->sc_parent = parent;
113 	sc->sc_address = args->ia_addr;
114 	aprint_normal(": SGS TDA7433 Basic Audio Processor\n");
115 	sc->sc_i2c = args->ia_tag;
116 	sgsmix_setup(sc);
117 }
118 
119 static void
sgsmix_setup(struct sgsmix_softc * sc)120 sgsmix_setup(struct sgsmix_softc *sc)
121 {
122 	int i;
123 	uint8_t out[2];
124 
125 	sc->sc_regs[0] = 9;	/* input 1 */
126 	sc->sc_regs[1] = 0x20;	/* master gain 0dB */
127 	sc->sc_regs[2] = 0x77;	/* flat bass / treble */
128 	sc->sc_regs[3] = 0;	/* all speakers full volume */
129 	sc->sc_regs[4] = 0;
130 	sc->sc_regs[5] = 0;
131 	sc->sc_regs[6] = 0;
132 
133 	iic_acquire_bus(sc->sc_i2c, 0);
134 
135 	for (i = 0; i < 7; i++) {
136 		out[0] = i;
137 		out[1] = sc->sc_regs[i];
138 		iic_exec(sc->sc_i2c, I2C_OP_WRITE, sc->sc_address, out, 2,
139 		    NULL, 0, 0);
140 	}
141 
142 	iic_release_bus(sc->sc_i2c, 0);
143 }
144 
145 static void
sgsmix_writereg(struct sgsmix_softc * sc,int reg,uint8_t val)146 sgsmix_writereg(struct sgsmix_softc *sc, int reg, uint8_t val)
147 {
148 	uint8_t out[2] = {reg, val};
149 
150 	if ((reg < 0) || (reg >= 7))
151 		return;
152 
153 	if (sc->sc_regs[reg] == val)
154 		return;
155 
156 	if (iic_exec(sc->sc_i2c, I2C_OP_WRITE, sc->sc_address, out, 2, NULL,
157 	    0, 0) == 0) {
158 	    	sc->sc_regs[reg] = val;
159 	}
160 }
161 
162 void
sgsmix_set_speaker_vol(void * cookie,int left,int right)163 sgsmix_set_speaker_vol(void *cookie, int left, int right)
164 {
165 	struct sgsmix_softc *sc = device_private((device_t)cookie);
166 
167 	DPRINTF("%s: speaker %d %d\n", device_xname(sc->sc_dev), left, right);
168 	if (left == 0) {
169 		sgsmix_writereg(sc, SGSREG_SPEAKER_L, 0x20);
170 	} else {
171 		sgsmix_writereg(sc, SGSREG_SPEAKER_L,
172 		    ((255 - left) >> 3) & 0x1f);
173 	}
174 
175 	if (right == 0) {
176 		sgsmix_writereg(sc, SGSREG_SPEAKER_R, 0x20);
177 	} else {
178 		sgsmix_writereg(sc, SGSREG_SPEAKER_R,
179 		    ((255 - right) >> 3) & 0x1f);
180 	}
181 }
182 
183 void
sgsmix_set_headphone_vol(void * cookie,int left,int right)184 sgsmix_set_headphone_vol(void *cookie, int left, int right)
185 {
186 	struct sgsmix_softc *sc = device_private((device_t)cookie);
187 
188 	DPRINTF("%s: headphones %d %d\n", device_xname(sc->sc_dev), left, right);
189 	if (left == 0) {
190 		sgsmix_writereg(sc, SGSREG_HEADPHONES_L, 0x20);
191 	} else {
192 		sgsmix_writereg(sc, SGSREG_HEADPHONES_L,
193 		    ((255 - left) >> 3) & 0x1f);
194 	}
195 
196 	if (right == 0) {
197 		sgsmix_writereg(sc, SGSREG_HEADPHONES_R, 0x20);
198 	} else {
199 		sgsmix_writereg(sc, SGSREG_HEADPHONES_R,
200 		    ((255 - right) >> 3) & 0x1f);
201 	}
202 }
203 
204 void
sgsmix_set_bass_treble(void * cookie,int bass,int treble)205 sgsmix_set_bass_treble(void *cookie, int bass, int treble)
206 {
207 	struct sgsmix_softc *sc = device_private((device_t)cookie);
208 	uint8_t b, t;
209 
210 	t = (treble >> 4) & 0xf;
211 	if (t & 0x8)
212 		t ^= 7;
213 	b = bass & 0xf0;
214 	if (b & 0x80)
215 		b ^= 0x70;
216 	DPRINTF("%s: bass/treble %02x %02x\n", device_xname(sc->sc_dev), b, t);
217 	sgsmix_writereg(sc, SGSREG_BASS_TREBLE, b | t);
218 }
219