Merge tag 'pidfd-updates-v5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/braun...
[linux-2.6-block.git] / sound / i2c / tea6330t.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * Routines for control of the TEA6330T circuit via i2c bus
4 * Sound fader control circuit for car radios by Philips Semiconductors
c1017a4c 5 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
6 */
7
1da177e4
LT
8#include <linux/init.h>
9#include <linux/slab.h>
da155d5b 10#include <linux/module.h>
1da177e4 11#include <sound/core.h>
c8714ba1 12#include <sound/control.h>
1da177e4
LT
13#include <sound/tea6330t.h>
14
c1017a4c 15MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
1da177e4
LT
16MODULE_DESCRIPTION("Routines for control of the TEA6330T circuit via i2c bus");
17MODULE_LICENSE("GPL");
18
19#define TEA6330T_ADDR (0x80>>1) /* fixed address */
20
21#define TEA6330T_SADDR_VOLUME_LEFT 0x00 /* volume left */
22#define TEA6330T_SADDR_VOLUME_RIGHT 0x01 /* volume right */
23#define TEA6330T_SADDR_BASS 0x02 /* bass control */
24#define TEA6330T_SADDR_TREBLE 0x03 /* treble control */
25#define TEA6330T_SADDR_FADER 0x04 /* fader control */
26#define TEA6330T_MFN 0x20 /* mute control for selected channels */
27#define TEA6330T_FCH 0x10 /* select fader channels - front or rear */
28#define TEA6330T_SADDR_AUDIO_SWITCH 0x05 /* audio switch */
29#define TEA6330T_GMU 0x80 /* mute control, general mute */
30#define TEA6330T_EQN 0x40 /* equalizer switchover (0=equalizer-on) */
31
97f02e05
TI
32
33struct tea6330t {
34 struct snd_i2c_device *device;
35 struct snd_i2c_bus *bus;
c8714ba1
TI
36 int equalizer;
37 int fader;
38 unsigned char regs[8];
39 unsigned char mleft, mright;
40 unsigned char bass, treble;
41 unsigned char max_bass, max_treble;
97f02e05
TI
42};
43
c8714ba1 44
97f02e05 45int snd_tea6330t_detect(struct snd_i2c_bus *bus, int equalizer)
1da177e4
LT
46{
47 int res;
48
49 snd_i2c_lock(bus);
50 res = snd_i2c_probeaddr(bus, TEA6330T_ADDR);
51 snd_i2c_unlock(bus);
52 return res;
53}
54
55#if 0
97f02e05 56static void snd_tea6330t_set(struct tea6330t *tea,
1da177e4
LT
57 unsigned char addr, unsigned char value)
58{
59#if 0
99b359ba 60 printk(KERN_DEBUG "set - 0x%x/0x%x\n", addr, value);
1da177e4
LT
61#endif
62 snd_i2c_write(tea->bus, TEA6330T_ADDR, addr, value, 1);
63}
64#endif
65
66#define TEA6330T_MASTER_VOLUME(xname, xindex) \
67{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
68 .info = snd_tea6330t_info_master_volume, \
69 .get = snd_tea6330t_get_master_volume, .put = snd_tea6330t_put_master_volume }
70
97f02e05
TI
71static int snd_tea6330t_info_master_volume(struct snd_kcontrol *kcontrol,
72 struct snd_ctl_elem_info *uinfo)
1da177e4
LT
73{
74 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
75 uinfo->count = 2;
76 uinfo->value.integer.min = 0;
77 uinfo->value.integer.max = 43;
78 return 0;
79}
80
97f02e05
TI
81static int snd_tea6330t_get_master_volume(struct snd_kcontrol *kcontrol,
82 struct snd_ctl_elem_value *ucontrol)
1da177e4 83{
97f02e05 84 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
1da177e4
LT
85
86 snd_i2c_lock(tea->bus);
87 ucontrol->value.integer.value[0] = tea->mleft - 0x14;
88 ucontrol->value.integer.value[1] = tea->mright - 0x14;
89 snd_i2c_unlock(tea->bus);
90 return 0;
91}
92
97f02e05
TI
93static int snd_tea6330t_put_master_volume(struct snd_kcontrol *kcontrol,
94 struct snd_ctl_elem_value *ucontrol)
1da177e4 95{
97f02e05 96 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
1da177e4
LT
97 int change, count, err;
98 unsigned char bytes[3];
99 unsigned char val1, val2;
100
101 val1 = (ucontrol->value.integer.value[0] % 44) + 0x14;
102 val2 = (ucontrol->value.integer.value[1] % 44) + 0x14;
103 snd_i2c_lock(tea->bus);
104 change = val1 != tea->mleft || val2 != tea->mright;
105 tea->mleft = val1;
106 tea->mright = val2;
107 count = 0;
108 if (tea->regs[TEA6330T_SADDR_VOLUME_LEFT] != 0) {
109 bytes[count++] = TEA6330T_SADDR_VOLUME_LEFT;
110 bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = tea->mleft;
111 }
112 if (tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] != 0) {
113 if (count == 0)
114 bytes[count++] = TEA6330T_SADDR_VOLUME_RIGHT;
115 bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = tea->mright;
116 }
117 if (count > 0) {
118 if ((err = snd_i2c_sendbytes(tea->device, bytes, count)) < 0)
119 change = err;
120 }
121 snd_i2c_unlock(tea->bus);
122 return change;
123}
124
125#define TEA6330T_MASTER_SWITCH(xname, xindex) \
126{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
127 .info = snd_tea6330t_info_master_switch, \
128 .get = snd_tea6330t_get_master_switch, .put = snd_tea6330t_put_master_switch }
129
a5ce8890 130#define snd_tea6330t_info_master_switch snd_ctl_boolean_stereo_info
1da177e4 131
97f02e05
TI
132static int snd_tea6330t_get_master_switch(struct snd_kcontrol *kcontrol,
133 struct snd_ctl_elem_value *ucontrol)
1da177e4 134{
97f02e05 135 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
1da177e4
LT
136
137 snd_i2c_lock(tea->bus);
138 ucontrol->value.integer.value[0] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
139 ucontrol->value.integer.value[1] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
140 snd_i2c_unlock(tea->bus);
141 return 0;
142}
143
97f02e05
TI
144static int snd_tea6330t_put_master_switch(struct snd_kcontrol *kcontrol,
145 struct snd_ctl_elem_value *ucontrol)
1da177e4 146{
97f02e05 147 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
1da177e4
LT
148 int change, err;
149 unsigned char bytes[3];
150 unsigned char oval1, oval2, val1, val2;
151
152 val1 = ucontrol->value.integer.value[0] & 1;
153 val2 = ucontrol->value.integer.value[1] & 1;
154 snd_i2c_lock(tea->bus);
155 oval1 = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
156 oval2 = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
157 change = val1 != oval1 || val2 != oval2;
158 tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = val1 ? tea->mleft : 0;
159 tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = val2 ? tea->mright : 0;
160 bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
161 bytes[1] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT];
162 bytes[2] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT];
163 if ((err = snd_i2c_sendbytes(tea->device, bytes, 3)) < 0)
164 change = err;
165 snd_i2c_unlock(tea->bus);
166 return change;
167}
168
169#define TEA6330T_BASS(xname, xindex) \
170{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
171 .info = snd_tea6330t_info_bass, \
172 .get = snd_tea6330t_get_bass, .put = snd_tea6330t_put_bass }
173
97f02e05
TI
174static int snd_tea6330t_info_bass(struct snd_kcontrol *kcontrol,
175 struct snd_ctl_elem_info *uinfo)
1da177e4 176{
97f02e05 177 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
1da177e4
LT
178
179 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
180 uinfo->count = 1;
181 uinfo->value.integer.min = 0;
182 uinfo->value.integer.max = tea->max_bass;
183 return 0;
184}
185
97f02e05
TI
186static int snd_tea6330t_get_bass(struct snd_kcontrol *kcontrol,
187 struct snd_ctl_elem_value *ucontrol)
1da177e4 188{
97f02e05 189 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
1da177e4
LT
190
191 ucontrol->value.integer.value[0] = tea->bass;
192 return 0;
193}
194
97f02e05
TI
195static int snd_tea6330t_put_bass(struct snd_kcontrol *kcontrol,
196 struct snd_ctl_elem_value *ucontrol)
1da177e4 197{
97f02e05 198 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
1da177e4
LT
199 int change, err;
200 unsigned char bytes[2];
201 unsigned char val1;
202
203 val1 = ucontrol->value.integer.value[0] % (tea->max_bass + 1);
204 snd_i2c_lock(tea->bus);
205 tea->bass = val1;
206 val1 += tea->equalizer ? 7 : 3;
207 change = tea->regs[TEA6330T_SADDR_BASS] != val1;
208 bytes[0] = TEA6330T_SADDR_BASS;
209 bytes[1] = tea->regs[TEA6330T_SADDR_BASS] = val1;
210 if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
211 change = err;
212 snd_i2c_unlock(tea->bus);
213 return change;
214}
215
216#define TEA6330T_TREBLE(xname, xindex) \
217{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
218 .info = snd_tea6330t_info_treble, \
219 .get = snd_tea6330t_get_treble, .put = snd_tea6330t_put_treble }
220
97f02e05
TI
221static int snd_tea6330t_info_treble(struct snd_kcontrol *kcontrol,
222 struct snd_ctl_elem_info *uinfo)
1da177e4 223{
97f02e05 224 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
1da177e4
LT
225
226 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
227 uinfo->count = 1;
228 uinfo->value.integer.min = 0;
229 uinfo->value.integer.max = tea->max_treble;
230 return 0;
231}
232
97f02e05
TI
233static int snd_tea6330t_get_treble(struct snd_kcontrol *kcontrol,
234 struct snd_ctl_elem_value *ucontrol)
1da177e4 235{
97f02e05 236 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
1da177e4
LT
237
238 ucontrol->value.integer.value[0] = tea->treble;
239 return 0;
240}
241
97f02e05
TI
242static int snd_tea6330t_put_treble(struct snd_kcontrol *kcontrol,
243 struct snd_ctl_elem_value *ucontrol)
1da177e4 244{
97f02e05 245 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
1da177e4
LT
246 int change, err;
247 unsigned char bytes[2];
248 unsigned char val1;
249
250 val1 = ucontrol->value.integer.value[0] % (tea->max_treble + 1);
251 snd_i2c_lock(tea->bus);
252 tea->treble = val1;
253 val1 += 3;
254 change = tea->regs[TEA6330T_SADDR_TREBLE] != val1;
255 bytes[0] = TEA6330T_SADDR_TREBLE;
256 bytes[1] = tea->regs[TEA6330T_SADDR_TREBLE] = val1;
257 if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
258 change = err;
259 snd_i2c_unlock(tea->bus);
260 return change;
261}
262
97f02e05 263static struct snd_kcontrol_new snd_tea6330t_controls[] = {
1da177e4
LT
264TEA6330T_MASTER_SWITCH("Master Playback Switch", 0),
265TEA6330T_MASTER_VOLUME("Master Playback Volume", 0),
266TEA6330T_BASS("Tone Control - Bass", 0),
267TEA6330T_TREBLE("Tone Control - Treble", 0)
268};
269
97f02e05 270static void snd_tea6330_free(struct snd_i2c_device *device)
1da177e4 271{
4d572776 272 kfree(device->private_data);
1da177e4
LT
273}
274
97f02e05
TI
275int snd_tea6330t_update_mixer(struct snd_card *card,
276 struct snd_i2c_bus *bus,
1da177e4
LT
277 int equalizer, int fader)
278{
97f02e05
TI
279 struct snd_i2c_device *device;
280 struct tea6330t *tea;
281 struct snd_kcontrol_new *knew;
1da177e4
LT
282 unsigned int idx;
283 int err = -ENOMEM;
284 u8 default_treble, default_bass;
285 unsigned char bytes[7];
286
561b220a 287 tea = kzalloc(sizeof(*tea), GFP_KERNEL);
1da177e4
LT
288 if (tea == NULL)
289 return -ENOMEM;
290 if ((err = snd_i2c_device_create(bus, "TEA6330T", TEA6330T_ADDR, &device)) < 0) {
291 kfree(tea);
292 return err;
293 }
294 tea->device = device;
295 tea->bus = bus;
296 tea->equalizer = equalizer;
297 tea->fader = fader;
298 device->private_data = tea;
299 device->private_free = snd_tea6330_free;
300
301 snd_i2c_lock(bus);
302
303 /* turn fader off and handle equalizer */
304 tea->regs[TEA6330T_SADDR_FADER] = 0x3f;
305 tea->regs[TEA6330T_SADDR_AUDIO_SWITCH] = equalizer ? 0 : TEA6330T_EQN;
306 /* initialize mixer */
307 if (!tea->equalizer) {
308 tea->max_bass = 9;
309 tea->max_treble = 8;
310 default_bass = 3 + 4;
311 tea->bass = 4;
312 default_treble = 3 + 4;
313 tea->treble = 4;
314 } else {
315 tea->max_bass = 5;
316 tea->max_treble = 0;
317 default_bass = 7 + 4;
318 tea->bass = 4;
319 default_treble = 3;
320 tea->treble = 0;
321 }
322 tea->mleft = tea->mright = 0x14;
323 tea->regs[TEA6330T_SADDR_BASS] = default_bass;
324 tea->regs[TEA6330T_SADDR_TREBLE] = default_treble;
325
326 /* compose I2C message and put the hardware to initial state */
327 bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
328 for (idx = 0; idx < 6; idx++)
329 bytes[idx+1] = tea->regs[idx];
330 if ((err = snd_i2c_sendbytes(device, bytes, 7)) < 0)
331 goto __error;
332
333 strcat(card->mixername, ",TEA6330T");
334 if ((err = snd_component_add(card, "TEA6330T")) < 0)
335 goto __error;
336
337 for (idx = 0; idx < ARRAY_SIZE(snd_tea6330t_controls); idx++) {
338 knew = &snd_tea6330t_controls[idx];
339 if (tea->treble == 0 && !strcmp(knew->name, "Tone Control - Treble"))
340 continue;
341 if ((err = snd_ctl_add(card, snd_ctl_new1(knew, tea))) < 0)
342 goto __error;
343 }
344
345 snd_i2c_unlock(bus);
346 return 0;
347
348 __error:
349 snd_i2c_unlock(bus);
350 snd_i2c_device_free(device);
351 return err;
352}
353
354EXPORT_SYMBOL(snd_tea6330t_detect);
355EXPORT_SYMBOL(snd_tea6330t_update_mixer);