blob: ae5b1e3a68cee3c155e9f63defa3bfce6db68c07 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines for control of the TEA6330T circuit via i2c bus
3 * Sound fader control circuit for car radios by Philips Semiconductors
4 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <sound/driver.h>
24#include <linux/init.h>
25#include <linux/slab.h>
26#include <sound/core.h>
Takashi Iwaic8714ba2005-11-17 10:22:32 +010027#include <sound/control.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/tea6330t.h>
29
30MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
31MODULE_DESCRIPTION("Routines for control of the TEA6330T circuit via i2c bus");
32MODULE_LICENSE("GPL");
33
34#define TEA6330T_ADDR (0x80>>1) /* fixed address */
35
36#define TEA6330T_SADDR_VOLUME_LEFT 0x00 /* volume left */
37#define TEA6330T_SADDR_VOLUME_RIGHT 0x01 /* volume right */
38#define TEA6330T_SADDR_BASS 0x02 /* bass control */
39#define TEA6330T_SADDR_TREBLE 0x03 /* treble control */
40#define TEA6330T_SADDR_FADER 0x04 /* fader control */
41#define TEA6330T_MFN 0x20 /* mute control for selected channels */
42#define TEA6330T_FCH 0x10 /* select fader channels - front or rear */
43#define TEA6330T_SADDR_AUDIO_SWITCH 0x05 /* audio switch */
44#define TEA6330T_GMU 0x80 /* mute control, general mute */
45#define TEA6330T_EQN 0x40 /* equalizer switchover (0=equalizer-on) */
46
Takashi Iwai97f02e02005-11-17 14:17:19 +010047
48struct tea6330t {
49 struct snd_i2c_device *device;
50 struct snd_i2c_bus *bus;
Takashi Iwaic8714ba2005-11-17 10:22:32 +010051 int equalizer;
52 int fader;
53 unsigned char regs[8];
54 unsigned char mleft, mright;
55 unsigned char bass, treble;
56 unsigned char max_bass, max_treble;
Takashi Iwai97f02e02005-11-17 14:17:19 +010057};
Takashi Iwaic8714ba2005-11-17 10:22:32 +010058
Takashi Iwai97f02e02005-11-17 14:17:19 +010059
60int snd_tea6330t_detect(struct snd_i2c_bus *bus, int equalizer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 int res;
63
64 snd_i2c_lock(bus);
65 res = snd_i2c_probeaddr(bus, TEA6330T_ADDR);
66 snd_i2c_unlock(bus);
67 return res;
68}
69
70#if 0
Takashi Iwai97f02e02005-11-17 14:17:19 +010071static void snd_tea6330t_set(struct tea6330t *tea,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 unsigned char addr, unsigned char value)
73{
74#if 0
Takashi Iwai99b359b2005-10-20 18:26:44 +020075 printk(KERN_DEBUG "set - 0x%x/0x%x\n", addr, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#endif
77 snd_i2c_write(tea->bus, TEA6330T_ADDR, addr, value, 1);
78}
79#endif
80
81#define TEA6330T_MASTER_VOLUME(xname, xindex) \
82{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
83 .info = snd_tea6330t_info_master_volume, \
84 .get = snd_tea6330t_get_master_volume, .put = snd_tea6330t_put_master_volume }
85
Takashi Iwai97f02e02005-11-17 14:17:19 +010086static int snd_tea6330t_info_master_volume(struct snd_kcontrol *kcontrol,
87 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
90 uinfo->count = 2;
91 uinfo->value.integer.min = 0;
92 uinfo->value.integer.max = 43;
93 return 0;
94}
95
Takashi Iwai97f02e02005-11-17 14:17:19 +010096static int snd_tea6330t_get_master_volume(struct snd_kcontrol *kcontrol,
97 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Takashi Iwai97f02e02005-11-17 14:17:19 +010099 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 snd_i2c_lock(tea->bus);
102 ucontrol->value.integer.value[0] = tea->mleft - 0x14;
103 ucontrol->value.integer.value[1] = tea->mright - 0x14;
104 snd_i2c_unlock(tea->bus);
105 return 0;
106}
107
Takashi Iwai97f02e02005-11-17 14:17:19 +0100108static int snd_tea6330t_put_master_volume(struct snd_kcontrol *kcontrol,
109 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100111 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int change, count, err;
113 unsigned char bytes[3];
114 unsigned char val1, val2;
115
116 val1 = (ucontrol->value.integer.value[0] % 44) + 0x14;
117 val2 = (ucontrol->value.integer.value[1] % 44) + 0x14;
118 snd_i2c_lock(tea->bus);
119 change = val1 != tea->mleft || val2 != tea->mright;
120 tea->mleft = val1;
121 tea->mright = val2;
122 count = 0;
123 if (tea->regs[TEA6330T_SADDR_VOLUME_LEFT] != 0) {
124 bytes[count++] = TEA6330T_SADDR_VOLUME_LEFT;
125 bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = tea->mleft;
126 }
127 if (tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] != 0) {
128 if (count == 0)
129 bytes[count++] = TEA6330T_SADDR_VOLUME_RIGHT;
130 bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = tea->mright;
131 }
132 if (count > 0) {
133 if ((err = snd_i2c_sendbytes(tea->device, bytes, count)) < 0)
134 change = err;
135 }
136 snd_i2c_unlock(tea->bus);
137 return change;
138}
139
140#define TEA6330T_MASTER_SWITCH(xname, xindex) \
141{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
142 .info = snd_tea6330t_info_master_switch, \
143 .get = snd_tea6330t_get_master_switch, .put = snd_tea6330t_put_master_switch }
144
Takashi Iwai97f02e02005-11-17 14:17:19 +0100145static int snd_tea6330t_info_master_switch(struct snd_kcontrol *kcontrol,
146 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
149 uinfo->count = 2;
150 uinfo->value.integer.min = 0;
151 uinfo->value.integer.max = 1;
152 return 0;
153}
154
Takashi Iwai97f02e02005-11-17 14:17:19 +0100155static int snd_tea6330t_get_master_switch(struct snd_kcontrol *kcontrol,
156 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100158 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 snd_i2c_lock(tea->bus);
161 ucontrol->value.integer.value[0] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
162 ucontrol->value.integer.value[1] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
163 snd_i2c_unlock(tea->bus);
164 return 0;
165}
166
Takashi Iwai97f02e02005-11-17 14:17:19 +0100167static int snd_tea6330t_put_master_switch(struct snd_kcontrol *kcontrol,
168 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100170 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 int change, err;
172 unsigned char bytes[3];
173 unsigned char oval1, oval2, val1, val2;
174
175 val1 = ucontrol->value.integer.value[0] & 1;
176 val2 = ucontrol->value.integer.value[1] & 1;
177 snd_i2c_lock(tea->bus);
178 oval1 = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
179 oval2 = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
180 change = val1 != oval1 || val2 != oval2;
181 tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = val1 ? tea->mleft : 0;
182 tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = val2 ? tea->mright : 0;
183 bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
184 bytes[1] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT];
185 bytes[2] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT];
186 if ((err = snd_i2c_sendbytes(tea->device, bytes, 3)) < 0)
187 change = err;
188 snd_i2c_unlock(tea->bus);
189 return change;
190}
191
192#define TEA6330T_BASS(xname, xindex) \
193{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
194 .info = snd_tea6330t_info_bass, \
195 .get = snd_tea6330t_get_bass, .put = snd_tea6330t_put_bass }
196
Takashi Iwai97f02e02005-11-17 14:17:19 +0100197static int snd_tea6330t_info_bass(struct snd_kcontrol *kcontrol,
198 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100200 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
203 uinfo->count = 1;
204 uinfo->value.integer.min = 0;
205 uinfo->value.integer.max = tea->max_bass;
206 return 0;
207}
208
Takashi Iwai97f02e02005-11-17 14:17:19 +0100209static int snd_tea6330t_get_bass(struct snd_kcontrol *kcontrol,
210 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100212 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 ucontrol->value.integer.value[0] = tea->bass;
215 return 0;
216}
217
Takashi Iwai97f02e02005-11-17 14:17:19 +0100218static int snd_tea6330t_put_bass(struct snd_kcontrol *kcontrol,
219 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100221 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int change, err;
223 unsigned char bytes[2];
224 unsigned char val1;
225
226 val1 = ucontrol->value.integer.value[0] % (tea->max_bass + 1);
227 snd_i2c_lock(tea->bus);
228 tea->bass = val1;
229 val1 += tea->equalizer ? 7 : 3;
230 change = tea->regs[TEA6330T_SADDR_BASS] != val1;
231 bytes[0] = TEA6330T_SADDR_BASS;
232 bytes[1] = tea->regs[TEA6330T_SADDR_BASS] = val1;
233 if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
234 change = err;
235 snd_i2c_unlock(tea->bus);
236 return change;
237}
238
239#define TEA6330T_TREBLE(xname, xindex) \
240{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
241 .info = snd_tea6330t_info_treble, \
242 .get = snd_tea6330t_get_treble, .put = snd_tea6330t_put_treble }
243
Takashi Iwai97f02e02005-11-17 14:17:19 +0100244static int snd_tea6330t_info_treble(struct snd_kcontrol *kcontrol,
245 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100247 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
250 uinfo->count = 1;
251 uinfo->value.integer.min = 0;
252 uinfo->value.integer.max = tea->max_treble;
253 return 0;
254}
255
Takashi Iwai97f02e02005-11-17 14:17:19 +0100256static int snd_tea6330t_get_treble(struct snd_kcontrol *kcontrol,
257 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100259 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 ucontrol->value.integer.value[0] = tea->treble;
262 return 0;
263}
264
Takashi Iwai97f02e02005-11-17 14:17:19 +0100265static int snd_tea6330t_put_treble(struct snd_kcontrol *kcontrol,
266 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100268 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 int change, err;
270 unsigned char bytes[2];
271 unsigned char val1;
272
273 val1 = ucontrol->value.integer.value[0] % (tea->max_treble + 1);
274 snd_i2c_lock(tea->bus);
275 tea->treble = val1;
276 val1 += 3;
277 change = tea->regs[TEA6330T_SADDR_TREBLE] != val1;
278 bytes[0] = TEA6330T_SADDR_TREBLE;
279 bytes[1] = tea->regs[TEA6330T_SADDR_TREBLE] = val1;
280 if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
281 change = err;
282 snd_i2c_unlock(tea->bus);
283 return change;
284}
285
Takashi Iwai97f02e02005-11-17 14:17:19 +0100286static struct snd_kcontrol_new snd_tea6330t_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287TEA6330T_MASTER_SWITCH("Master Playback Switch", 0),
288TEA6330T_MASTER_VOLUME("Master Playback Volume", 0),
289TEA6330T_BASS("Tone Control - Bass", 0),
290TEA6330T_TREBLE("Tone Control - Treble", 0)
291};
292
Takashi Iwai97f02e02005-11-17 14:17:19 +0100293static void snd_tea6330_free(struct snd_i2c_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Jesper Juhl4d572772005-05-30 17:30:32 +0200295 kfree(device->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Takashi Iwai97f02e02005-11-17 14:17:19 +0100298int snd_tea6330t_update_mixer(struct snd_card *card,
299 struct snd_i2c_bus *bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 int equalizer, int fader)
301{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100302 struct snd_i2c_device *device;
303 struct tea6330t *tea;
304 struct snd_kcontrol_new *knew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 unsigned int idx;
306 int err = -ENOMEM;
307 u8 default_treble, default_bass;
308 unsigned char bytes[7];
309
Takashi Iwai561b2202005-09-09 14:22:34 +0200310 tea = kzalloc(sizeof(*tea), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (tea == NULL)
312 return -ENOMEM;
313 if ((err = snd_i2c_device_create(bus, "TEA6330T", TEA6330T_ADDR, &device)) < 0) {
314 kfree(tea);
315 return err;
316 }
317 tea->device = device;
318 tea->bus = bus;
319 tea->equalizer = equalizer;
320 tea->fader = fader;
321 device->private_data = tea;
322 device->private_free = snd_tea6330_free;
323
324 snd_i2c_lock(bus);
325
326 /* turn fader off and handle equalizer */
327 tea->regs[TEA6330T_SADDR_FADER] = 0x3f;
328 tea->regs[TEA6330T_SADDR_AUDIO_SWITCH] = equalizer ? 0 : TEA6330T_EQN;
329 /* initialize mixer */
330 if (!tea->equalizer) {
331 tea->max_bass = 9;
332 tea->max_treble = 8;
333 default_bass = 3 + 4;
334 tea->bass = 4;
335 default_treble = 3 + 4;
336 tea->treble = 4;
337 } else {
338 tea->max_bass = 5;
339 tea->max_treble = 0;
340 default_bass = 7 + 4;
341 tea->bass = 4;
342 default_treble = 3;
343 tea->treble = 0;
344 }
345 tea->mleft = tea->mright = 0x14;
346 tea->regs[TEA6330T_SADDR_BASS] = default_bass;
347 tea->regs[TEA6330T_SADDR_TREBLE] = default_treble;
348
349 /* compose I2C message and put the hardware to initial state */
350 bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
351 for (idx = 0; idx < 6; idx++)
352 bytes[idx+1] = tea->regs[idx];
353 if ((err = snd_i2c_sendbytes(device, bytes, 7)) < 0)
354 goto __error;
355
356 strcat(card->mixername, ",TEA6330T");
357 if ((err = snd_component_add(card, "TEA6330T")) < 0)
358 goto __error;
359
360 for (idx = 0; idx < ARRAY_SIZE(snd_tea6330t_controls); idx++) {
361 knew = &snd_tea6330t_controls[idx];
362 if (tea->treble == 0 && !strcmp(knew->name, "Tone Control - Treble"))
363 continue;
364 if ((err = snd_ctl_add(card, snd_ctl_new1(knew, tea))) < 0)
365 goto __error;
366 }
367
368 snd_i2c_unlock(bus);
369 return 0;
370
371 __error:
372 snd_i2c_unlock(bus);
373 snd_i2c_device_free(device);
374 return err;
375}
376
377EXPORT_SYMBOL(snd_tea6330t_detect);
378EXPORT_SYMBOL(snd_tea6330t_update_mixer);
379
380/*
381 * INIT part
382 */
383
384static int __init alsa_tea6330t_init(void)
385{
386 return 0;
387}
388
389static void __exit alsa_tea6330t_exit(void)
390{
391}
392
393module_init(alsa_tea6330t_init)
394module_exit(alsa_tea6330t_exit)