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