blob: 0e3a9f2c5297658dd001782734dad639e9daf811 [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
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/slab.h>
25#include <sound/core.h>
Takashi Iwaic8714ba2005-11-17 10:22:32 +010026#include <sound/control.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/tea6330t.h>
28
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020029MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070030MODULE_DESCRIPTION("Routines for control of the TEA6330T circuit via i2c bus");
31MODULE_LICENSE("GPL");
32
33#define TEA6330T_ADDR (0x80>>1) /* fixed address */
34
35#define TEA6330T_SADDR_VOLUME_LEFT 0x00 /* volume left */
36#define TEA6330T_SADDR_VOLUME_RIGHT 0x01 /* volume right */
37#define TEA6330T_SADDR_BASS 0x02 /* bass control */
38#define TEA6330T_SADDR_TREBLE 0x03 /* treble control */
39#define TEA6330T_SADDR_FADER 0x04 /* fader control */
40#define TEA6330T_MFN 0x20 /* mute control for selected channels */
41#define TEA6330T_FCH 0x10 /* select fader channels - front or rear */
42#define TEA6330T_SADDR_AUDIO_SWITCH 0x05 /* audio switch */
43#define TEA6330T_GMU 0x80 /* mute control, general mute */
44#define TEA6330T_EQN 0x40 /* equalizer switchover (0=equalizer-on) */
45
Takashi Iwai97f02e02005-11-17 14:17:19 +010046
47struct tea6330t {
48 struct snd_i2c_device *device;
49 struct snd_i2c_bus *bus;
Takashi Iwaic8714ba2005-11-17 10:22:32 +010050 int equalizer;
51 int fader;
52 unsigned char regs[8];
53 unsigned char mleft, mright;
54 unsigned char bass, treble;
55 unsigned char max_bass, max_treble;
Takashi Iwai97f02e02005-11-17 14:17:19 +010056};
Takashi Iwaic8714ba2005-11-17 10:22:32 +010057
Takashi Iwai97f02e02005-11-17 14:17:19 +010058
59int snd_tea6330t_detect(struct snd_i2c_bus *bus, int equalizer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 int res;
62
63 snd_i2c_lock(bus);
64 res = snd_i2c_probeaddr(bus, TEA6330T_ADDR);
65 snd_i2c_unlock(bus);
66 return res;
67}
68
69#if 0
Takashi Iwai97f02e02005-11-17 14:17:19 +010070static void snd_tea6330t_set(struct tea6330t *tea,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 unsigned char addr, unsigned char value)
72{
73#if 0
Takashi Iwai99b359b2005-10-20 18:26:44 +020074 printk(KERN_DEBUG "set - 0x%x/0x%x\n", addr, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#endif
76 snd_i2c_write(tea->bus, TEA6330T_ADDR, addr, value, 1);
77}
78#endif
79
80#define TEA6330T_MASTER_VOLUME(xname, xindex) \
81{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
82 .info = snd_tea6330t_info_master_volume, \
83 .get = snd_tea6330t_get_master_volume, .put = snd_tea6330t_put_master_volume }
84
Takashi Iwai97f02e02005-11-17 14:17:19 +010085static int snd_tea6330t_info_master_volume(struct snd_kcontrol *kcontrol,
86 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
89 uinfo->count = 2;
90 uinfo->value.integer.min = 0;
91 uinfo->value.integer.max = 43;
92 return 0;
93}
94
Takashi Iwai97f02e02005-11-17 14:17:19 +010095static int snd_tea6330t_get_master_volume(struct snd_kcontrol *kcontrol,
96 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Takashi Iwai97f02e02005-11-17 14:17:19 +010098 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 snd_i2c_lock(tea->bus);
101 ucontrol->value.integer.value[0] = tea->mleft - 0x14;
102 ucontrol->value.integer.value[1] = tea->mright - 0x14;
103 snd_i2c_unlock(tea->bus);
104 return 0;
105}
106
Takashi Iwai97f02e02005-11-17 14:17:19 +0100107static int snd_tea6330t_put_master_volume(struct snd_kcontrol *kcontrol,
108 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100110 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 int change, count, err;
112 unsigned char bytes[3];
113 unsigned char val1, val2;
114
115 val1 = (ucontrol->value.integer.value[0] % 44) + 0x14;
116 val2 = (ucontrol->value.integer.value[1] % 44) + 0x14;
117 snd_i2c_lock(tea->bus);
118 change = val1 != tea->mleft || val2 != tea->mright;
119 tea->mleft = val1;
120 tea->mright = val2;
121 count = 0;
122 if (tea->regs[TEA6330T_SADDR_VOLUME_LEFT] != 0) {
123 bytes[count++] = TEA6330T_SADDR_VOLUME_LEFT;
124 bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = tea->mleft;
125 }
126 if (tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] != 0) {
127 if (count == 0)
128 bytes[count++] = TEA6330T_SADDR_VOLUME_RIGHT;
129 bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = tea->mright;
130 }
131 if (count > 0) {
132 if ((err = snd_i2c_sendbytes(tea->device, bytes, count)) < 0)
133 change = err;
134 }
135 snd_i2c_unlock(tea->bus);
136 return change;
137}
138
139#define TEA6330T_MASTER_SWITCH(xname, xindex) \
140{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
141 .info = snd_tea6330t_info_master_switch, \
142 .get = snd_tea6330t_get_master_switch, .put = snd_tea6330t_put_master_switch }
143
Takashi Iwaia5ce8892007-07-23 15:42:26 +0200144#define snd_tea6330t_info_master_switch snd_ctl_boolean_stereo_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Takashi Iwai97f02e02005-11-17 14:17:19 +0100146static int snd_tea6330t_get_master_switch(struct snd_kcontrol *kcontrol,
147 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100149 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 snd_i2c_lock(tea->bus);
152 ucontrol->value.integer.value[0] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
153 ucontrol->value.integer.value[1] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
154 snd_i2c_unlock(tea->bus);
155 return 0;
156}
157
Takashi Iwai97f02e02005-11-17 14:17:19 +0100158static int snd_tea6330t_put_master_switch(struct snd_kcontrol *kcontrol,
159 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100161 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 int change, err;
163 unsigned char bytes[3];
164 unsigned char oval1, oval2, val1, val2;
165
166 val1 = ucontrol->value.integer.value[0] & 1;
167 val2 = ucontrol->value.integer.value[1] & 1;
168 snd_i2c_lock(tea->bus);
169 oval1 = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
170 oval2 = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
171 change = val1 != oval1 || val2 != oval2;
172 tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = val1 ? tea->mleft : 0;
173 tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = val2 ? tea->mright : 0;
174 bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
175 bytes[1] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT];
176 bytes[2] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT];
177 if ((err = snd_i2c_sendbytes(tea->device, bytes, 3)) < 0)
178 change = err;
179 snd_i2c_unlock(tea->bus);
180 return change;
181}
182
183#define TEA6330T_BASS(xname, xindex) \
184{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
185 .info = snd_tea6330t_info_bass, \
186 .get = snd_tea6330t_get_bass, .put = snd_tea6330t_put_bass }
187
Takashi Iwai97f02e02005-11-17 14:17:19 +0100188static int snd_tea6330t_info_bass(struct snd_kcontrol *kcontrol,
189 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100191 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
194 uinfo->count = 1;
195 uinfo->value.integer.min = 0;
196 uinfo->value.integer.max = tea->max_bass;
197 return 0;
198}
199
Takashi Iwai97f02e02005-11-17 14:17:19 +0100200static int snd_tea6330t_get_bass(struct snd_kcontrol *kcontrol,
201 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100203 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 ucontrol->value.integer.value[0] = tea->bass;
206 return 0;
207}
208
Takashi Iwai97f02e02005-11-17 14:17:19 +0100209static int snd_tea6330t_put_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 int change, err;
214 unsigned char bytes[2];
215 unsigned char val1;
216
217 val1 = ucontrol->value.integer.value[0] % (tea->max_bass + 1);
218 snd_i2c_lock(tea->bus);
219 tea->bass = val1;
220 val1 += tea->equalizer ? 7 : 3;
221 change = tea->regs[TEA6330T_SADDR_BASS] != val1;
222 bytes[0] = TEA6330T_SADDR_BASS;
223 bytes[1] = tea->regs[TEA6330T_SADDR_BASS] = val1;
224 if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
225 change = err;
226 snd_i2c_unlock(tea->bus);
227 return change;
228}
229
230#define TEA6330T_TREBLE(xname, xindex) \
231{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
232 .info = snd_tea6330t_info_treble, \
233 .get = snd_tea6330t_get_treble, .put = snd_tea6330t_put_treble }
234
Takashi Iwai97f02e02005-11-17 14:17:19 +0100235static int snd_tea6330t_info_treble(struct snd_kcontrol *kcontrol,
236 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100238 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
241 uinfo->count = 1;
242 uinfo->value.integer.min = 0;
243 uinfo->value.integer.max = tea->max_treble;
244 return 0;
245}
246
Takashi Iwai97f02e02005-11-17 14:17:19 +0100247static int snd_tea6330t_get_treble(struct snd_kcontrol *kcontrol,
248 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100250 struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 ucontrol->value.integer.value[0] = tea->treble;
253 return 0;
254}
255
Takashi Iwai97f02e02005-11-17 14:17:19 +0100256static int snd_tea6330t_put_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 int change, err;
261 unsigned char bytes[2];
262 unsigned char val1;
263
264 val1 = ucontrol->value.integer.value[0] % (tea->max_treble + 1);
265 snd_i2c_lock(tea->bus);
266 tea->treble = val1;
267 val1 += 3;
268 change = tea->regs[TEA6330T_SADDR_TREBLE] != val1;
269 bytes[0] = TEA6330T_SADDR_TREBLE;
270 bytes[1] = tea->regs[TEA6330T_SADDR_TREBLE] = val1;
271 if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
272 change = err;
273 snd_i2c_unlock(tea->bus);
274 return change;
275}
276
Takashi Iwai97f02e02005-11-17 14:17:19 +0100277static struct snd_kcontrol_new snd_tea6330t_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278TEA6330T_MASTER_SWITCH("Master Playback Switch", 0),
279TEA6330T_MASTER_VOLUME("Master Playback Volume", 0),
280TEA6330T_BASS("Tone Control - Bass", 0),
281TEA6330T_TREBLE("Tone Control - Treble", 0)
282};
283
Takashi Iwai97f02e02005-11-17 14:17:19 +0100284static void snd_tea6330_free(struct snd_i2c_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Jesper Juhl4d572772005-05-30 17:30:32 +0200286 kfree(device->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Takashi Iwai97f02e02005-11-17 14:17:19 +0100289int snd_tea6330t_update_mixer(struct snd_card *card,
290 struct snd_i2c_bus *bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 int equalizer, int fader)
292{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100293 struct snd_i2c_device *device;
294 struct tea6330t *tea;
295 struct snd_kcontrol_new *knew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 unsigned int idx;
297 int err = -ENOMEM;
298 u8 default_treble, default_bass;
299 unsigned char bytes[7];
300
Takashi Iwai561b2202005-09-09 14:22:34 +0200301 tea = kzalloc(sizeof(*tea), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (tea == NULL)
303 return -ENOMEM;
304 if ((err = snd_i2c_device_create(bus, "TEA6330T", TEA6330T_ADDR, &device)) < 0) {
305 kfree(tea);
306 return err;
307 }
308 tea->device = device;
309 tea->bus = bus;
310 tea->equalizer = equalizer;
311 tea->fader = fader;
312 device->private_data = tea;
313 device->private_free = snd_tea6330_free;
314
315 snd_i2c_lock(bus);
316
317 /* turn fader off and handle equalizer */
318 tea->regs[TEA6330T_SADDR_FADER] = 0x3f;
319 tea->regs[TEA6330T_SADDR_AUDIO_SWITCH] = equalizer ? 0 : TEA6330T_EQN;
320 /* initialize mixer */
321 if (!tea->equalizer) {
322 tea->max_bass = 9;
323 tea->max_treble = 8;
324 default_bass = 3 + 4;
325 tea->bass = 4;
326 default_treble = 3 + 4;
327 tea->treble = 4;
328 } else {
329 tea->max_bass = 5;
330 tea->max_treble = 0;
331 default_bass = 7 + 4;
332 tea->bass = 4;
333 default_treble = 3;
334 tea->treble = 0;
335 }
336 tea->mleft = tea->mright = 0x14;
337 tea->regs[TEA6330T_SADDR_BASS] = default_bass;
338 tea->regs[TEA6330T_SADDR_TREBLE] = default_treble;
339
340 /* compose I2C message and put the hardware to initial state */
341 bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
342 for (idx = 0; idx < 6; idx++)
343 bytes[idx+1] = tea->regs[idx];
344 if ((err = snd_i2c_sendbytes(device, bytes, 7)) < 0)
345 goto __error;
346
347 strcat(card->mixername, ",TEA6330T");
348 if ((err = snd_component_add(card, "TEA6330T")) < 0)
349 goto __error;
350
351 for (idx = 0; idx < ARRAY_SIZE(snd_tea6330t_controls); idx++) {
352 knew = &snd_tea6330t_controls[idx];
353 if (tea->treble == 0 && !strcmp(knew->name, "Tone Control - Treble"))
354 continue;
355 if ((err = snd_ctl_add(card, snd_ctl_new1(knew, tea))) < 0)
356 goto __error;
357 }
358
359 snd_i2c_unlock(bus);
360 return 0;
361
362 __error:
363 snd_i2c_unlock(bus);
364 snd_i2c_device_free(device);
365 return err;
366}
367
368EXPORT_SYMBOL(snd_tea6330t_detect);
369EXPORT_SYMBOL(snd_tea6330t_update_mixer);
370
371/*
372 * INIT part
373 */
374
375static int __init alsa_tea6330t_init(void)
376{
377 return 0;
378}
379
380static void __exit alsa_tea6330t_exit(void)
381{
382}
383
384module_init(alsa_tea6330t_init)
385module_exit(alsa_tea6330t_exit)