ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / i2c / tea6330t.c
1 /*
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>
27 #include <sound/tea6330t.h>
28
29 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
30 MODULE_DESCRIPTION("Routines for control of the TEA6330T circuit via i2c bus");
31 MODULE_LICENSE("GPL");
32
33 #define chip_t tea6330t_t
34
35 #define TEA6330T_ADDR                   (0x80>>1) /* fixed address */
36
37 #define TEA6330T_SADDR_VOLUME_LEFT      0x00    /* volume left */
38 #define TEA6330T_SADDR_VOLUME_RIGHT     0x01    /* volume right */
39 #define TEA6330T_SADDR_BASS             0x02    /* bass control */
40 #define TEA6330T_SADDR_TREBLE           0x03    /* treble control */
41 #define TEA6330T_SADDR_FADER            0x04    /* fader control */
42 #define   TEA6330T_MFN                  0x20    /* mute control for selected channels */
43 #define   TEA6330T_FCH                  0x10    /* select fader channels - front or rear */
44 #define TEA6330T_SADDR_AUDIO_SWITCH     0x05    /* audio switch */
45 #define   TEA6330T_GMU                  0x80    /* mute control, general mute */
46 #define   TEA6330T_EQN                  0x40    /* equalizer switchover (0=equalizer-on) */
47
48 int snd_tea6330t_detect(snd_i2c_bus_t *bus, int equalizer)
49 {
50         int res;
51
52         snd_i2c_lock(bus);
53         res = snd_i2c_probeaddr(bus, TEA6330T_ADDR);
54         snd_i2c_unlock(bus);
55         return res;
56 }
57
58 #if 0
59 static void snd_tea6330t_set(tea6330t_t *tea,
60                              unsigned char addr, unsigned char value)
61 {
62 #if 0
63         printk("set - 0x%x/0x%x\n", addr, value);
64 #endif
65         snd_i2c_write(tea->bus, TEA6330T_ADDR, addr, value, 1);
66 }
67 #endif
68
69 #define TEA6330T_MASTER_VOLUME(xname, xindex) \
70 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
71   .info = snd_tea6330t_info_master_volume, \
72   .get = snd_tea6330t_get_master_volume, .put = snd_tea6330t_put_master_volume }
73
74 static int snd_tea6330t_info_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
75 {
76         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
77         uinfo->count = 2;
78         uinfo->value.integer.min = 0;
79         uinfo->value.integer.max = 43;
80         return 0;
81 }
82
83 static int snd_tea6330t_get_master_volume(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
84 {
85         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
86         
87         snd_i2c_lock(tea->bus);
88         ucontrol->value.integer.value[0] = tea->mleft - 0x14;
89         ucontrol->value.integer.value[1] = tea->mright - 0x14;
90         snd_i2c_unlock(tea->bus);
91         return 0;
92 }
93
94 static int snd_tea6330t_put_master_volume(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
95 {
96         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
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
130 static int snd_tea6330t_info_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
131 {
132         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
133         uinfo->count = 2;
134         uinfo->value.integer.min = 0;
135         uinfo->value.integer.max = 1;
136         return 0;
137 }
138
139 static int snd_tea6330t_get_master_switch(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
140 {
141         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
142         
143         snd_i2c_lock(tea->bus);
144         ucontrol->value.integer.value[0] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
145         ucontrol->value.integer.value[1] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
146         snd_i2c_unlock(tea->bus);
147         return 0;
148 }
149
150 static int snd_tea6330t_put_master_switch(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
151 {
152         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
153         int change, err;
154         unsigned char bytes[3];
155         unsigned char oval1, oval2, val1, val2;
156         
157         val1 = ucontrol->value.integer.value[0] & 1;
158         val2 = ucontrol->value.integer.value[1] & 1;
159         snd_i2c_lock(tea->bus);
160         oval1 = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
161         oval2 = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
162         change = val1 != oval1 || val2 != oval2;
163         tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = val1 ? tea->mleft : 0;
164         tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = val2 ? tea->mright : 0;
165         bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
166         bytes[1] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT];
167         bytes[2] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT];
168         if ((err = snd_i2c_sendbytes(tea->device, bytes, 3)) < 0)
169                 change = err;
170         snd_i2c_unlock(tea->bus);
171         return change;
172 }
173
174 #define TEA6330T_BASS(xname, xindex) \
175 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
176   .info = snd_tea6330t_info_bass, \
177   .get = snd_tea6330t_get_bass, .put = snd_tea6330t_put_bass }
178
179 static int snd_tea6330t_info_bass(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
180 {
181         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
182
183         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
184         uinfo->count = 1;
185         uinfo->value.integer.min = 0;
186         uinfo->value.integer.max = tea->max_bass;
187         return 0;
188 }
189
190 static int snd_tea6330t_get_bass(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
191 {
192         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
193         
194         ucontrol->value.integer.value[0] = tea->bass;
195         return 0;
196 }
197
198 static int snd_tea6330t_put_bass(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
199 {
200         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
201         int change, err;
202         unsigned char bytes[2];
203         unsigned char val1;
204         
205         val1 = ucontrol->value.integer.value[0] % (tea->max_bass + 1);
206         snd_i2c_lock(tea->bus);
207         tea->bass = val1;
208         val1 += tea->equalizer ? 7 : 3;
209         change = tea->regs[TEA6330T_SADDR_BASS] != val1;
210         bytes[0] = TEA6330T_SADDR_BASS;
211         bytes[1] = tea->regs[TEA6330T_SADDR_BASS] = val1;
212         if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
213                 change = err;
214         snd_i2c_unlock(tea->bus);
215         return change;
216 }
217
218 #define TEA6330T_TREBLE(xname, xindex) \
219 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
220   .info = snd_tea6330t_info_treble, \
221   .get = snd_tea6330t_get_treble, .put = snd_tea6330t_put_treble }
222
223 static int snd_tea6330t_info_treble(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
224 {
225         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
226
227         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
228         uinfo->count = 1;
229         uinfo->value.integer.min = 0;
230         uinfo->value.integer.max = tea->max_treble;
231         return 0;
232 }
233
234 static int snd_tea6330t_get_treble(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
235 {
236         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
237         
238         ucontrol->value.integer.value[0] = tea->treble;
239         return 0;
240 }
241
242 static int snd_tea6330t_put_treble(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
243 {
244         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
245         int change, err;
246         unsigned char bytes[2];
247         unsigned char val1;
248         
249         val1 = ucontrol->value.integer.value[0] % (tea->max_treble + 1);
250         snd_i2c_lock(tea->bus);
251         tea->treble = val1;
252         val1 += 3;
253         change = tea->regs[TEA6330T_SADDR_TREBLE] != val1;
254         bytes[0] = TEA6330T_SADDR_TREBLE;
255         bytes[1] = tea->regs[TEA6330T_SADDR_TREBLE] = val1;
256         if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
257                 change = err;
258         snd_i2c_unlock(tea->bus);
259         return change;
260 }
261
262 #define TEA6330T_CONTROLS (sizeof(snd_tea6330t_controls)/sizeof(snd_kcontrol_new_t))
263
264 static snd_kcontrol_new_t snd_tea6330t_controls[] = {
265 TEA6330T_MASTER_SWITCH("Master Playback Switch", 0),
266 TEA6330T_MASTER_VOLUME("Master Playback Volume", 0),
267 TEA6330T_BASS("Tone Control - Bass", 0),
268 TEA6330T_TREBLE("Tone Control - Treble", 0)
269 };
270
271 static void snd_tea6330_free(snd_i2c_device_t *device)
272 {
273         tea6330t_t *tea = snd_magic_cast(tea6330t_t, device->private_data, return);
274         snd_magic_kfree(tea);
275 }
276                                         
277 int snd_tea6330t_update_mixer(snd_card_t * card,
278                               snd_i2c_bus_t *bus,
279                               int equalizer, int fader)
280 {
281         snd_i2c_device_t *device;
282         tea6330t_t *tea;
283         snd_kcontrol_new_t *knew;
284         unsigned int idx;
285         int err = -ENOMEM;
286         u8 default_treble, default_bass;
287         unsigned char bytes[7];
288
289         tea = snd_magic_kcalloc(tea6330t_t, 0, GFP_KERNEL);
290         if (tea == NULL)
291                 return -ENOMEM;
292         if ((err = snd_i2c_device_create(bus, "TEA6330T", TEA6330T_ADDR, &device)) < 0) {
293                 snd_magic_kfree(tea);
294                 return err;
295         }
296         tea->device = device;
297         tea->bus = bus;
298         tea->equalizer = equalizer;
299         tea->fader = fader;
300         device->private_data = tea;
301         device->private_free = snd_tea6330_free;
302
303         snd_i2c_lock(bus);
304
305         /* turn fader off and handle equalizer */
306         tea->regs[TEA6330T_SADDR_FADER] = 0x3f;
307         tea->regs[TEA6330T_SADDR_AUDIO_SWITCH] = equalizer ? 0 : TEA6330T_EQN;
308         /* initialize mixer */
309         if (!tea->equalizer) {
310                 tea->max_bass = 9;
311                 tea->max_treble = 8;
312                 default_bass = 3 + 4;
313                 tea->bass = 4;
314                 default_treble = 3 + 4;
315                 tea->treble = 4;
316         } else {
317                 tea->max_bass = 5;
318                 tea->max_treble = 0;
319                 default_bass = 7 + 4;
320                 tea->bass = 4;
321                 default_treble = 3;
322                 tea->treble = 0;
323         }
324         tea->mleft = tea->mright = 0x14;
325         tea->regs[TEA6330T_SADDR_BASS] = default_bass;
326         tea->regs[TEA6330T_SADDR_TREBLE] = default_treble;
327
328         /* compose I2C message and put the hardware to initial state */
329         bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
330         for (idx = 0; idx < 6; idx++)
331                 bytes[idx+1] = tea->regs[idx];
332         if ((err = snd_i2c_sendbytes(device, bytes, 7)) < 0)
333                 goto __error;
334
335         strcat(card->mixername, ",TEA6330T");
336         if ((err = snd_component_add(card, "TEA6330T")) < 0)
337                 goto __error;
338
339         for (idx = 0; idx < TEA6330T_CONTROLS; idx++) {
340                 knew = &snd_tea6330t_controls[idx];
341                 if (tea->treble == 0 && !strcmp(knew->name, "Tone Control - Treble"))
342                         continue;
343                 if ((err = snd_ctl_add(card, snd_ctl_new1(knew, tea))) < 0)
344                         goto __error;
345         }
346
347         snd_i2c_unlock(bus);
348         return 0;
349         
350       __error:
351         snd_i2c_unlock(bus);
352         snd_i2c_device_free(device);
353         return err;
354 }
355
356 EXPORT_SYMBOL(snd_tea6330t_detect);
357 EXPORT_SYMBOL(snd_tea6330t_update_mixer);
358
359 /*
360  *  INIT part
361  */
362
363 static int __init alsa_tea6330t_init(void)
364 {
365         return 0;
366 }
367
368 static void __exit alsa_tea6330t_exit(void)
369 {
370 }
371
372 module_init(alsa_tea6330t_init)
373 module_exit(alsa_tea6330t_exit)