Linux-2.6.12-rc2
[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 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
46 int snd_tea6330t_detect(snd_i2c_bus_t *bus, int equalizer)
47 {
48         int res;
49
50         snd_i2c_lock(bus);
51         res = snd_i2c_probeaddr(bus, TEA6330T_ADDR);
52         snd_i2c_unlock(bus);
53         return res;
54 }
55
56 #if 0
57 static void snd_tea6330t_set(tea6330t_t *tea,
58                              unsigned char addr, unsigned char value)
59 {
60 #if 0
61         printk("set - 0x%x/0x%x\n", addr, value);
62 #endif
63         snd_i2c_write(tea->bus, TEA6330T_ADDR, addr, value, 1);
64 }
65 #endif
66
67 #define TEA6330T_MASTER_VOLUME(xname, xindex) \
68 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
69   .info = snd_tea6330t_info_master_volume, \
70   .get = snd_tea6330t_get_master_volume, .put = snd_tea6330t_put_master_volume }
71
72 static int snd_tea6330t_info_master_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
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
81 static int snd_tea6330t_get_master_volume(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
82 {
83         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
84         
85         snd_i2c_lock(tea->bus);
86         ucontrol->value.integer.value[0] = tea->mleft - 0x14;
87         ucontrol->value.integer.value[1] = tea->mright - 0x14;
88         snd_i2c_unlock(tea->bus);
89         return 0;
90 }
91
92 static int snd_tea6330t_put_master_volume(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
93 {
94         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
95         int change, count, err;
96         unsigned char bytes[3];
97         unsigned char val1, val2;
98         
99         val1 = (ucontrol->value.integer.value[0] % 44) + 0x14;
100         val2 = (ucontrol->value.integer.value[1] % 44) + 0x14;
101         snd_i2c_lock(tea->bus);
102         change = val1 != tea->mleft || val2 != tea->mright;
103         tea->mleft = val1;
104         tea->mright = val2;
105         count = 0;
106         if (tea->regs[TEA6330T_SADDR_VOLUME_LEFT] != 0) {
107                 bytes[count++] = TEA6330T_SADDR_VOLUME_LEFT;
108                 bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = tea->mleft;
109         }
110         if (tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] != 0) {
111                 if (count == 0)
112                         bytes[count++] = TEA6330T_SADDR_VOLUME_RIGHT;
113                 bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = tea->mright;
114         }
115         if (count > 0) {
116                 if ((err = snd_i2c_sendbytes(tea->device, bytes, count)) < 0)
117                         change = err;
118         }
119         snd_i2c_unlock(tea->bus);
120         return change;
121 }
122
123 #define TEA6330T_MASTER_SWITCH(xname, xindex) \
124 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
125   .info = snd_tea6330t_info_master_switch, \
126   .get = snd_tea6330t_get_master_switch, .put = snd_tea6330t_put_master_switch }
127
128 static int snd_tea6330t_info_master_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
129 {
130         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
131         uinfo->count = 2;
132         uinfo->value.integer.min = 0;
133         uinfo->value.integer.max = 1;
134         return 0;
135 }
136
137 static int snd_tea6330t_get_master_switch(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
138 {
139         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
140         
141         snd_i2c_lock(tea->bus);
142         ucontrol->value.integer.value[0] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
143         ucontrol->value.integer.value[1] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
144         snd_i2c_unlock(tea->bus);
145         return 0;
146 }
147
148 static int snd_tea6330t_put_master_switch(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
149 {
150         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
151         int change, err;
152         unsigned char bytes[3];
153         unsigned char oval1, oval2, val1, val2;
154         
155         val1 = ucontrol->value.integer.value[0] & 1;
156         val2 = ucontrol->value.integer.value[1] & 1;
157         snd_i2c_lock(tea->bus);
158         oval1 = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
159         oval2 = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
160         change = val1 != oval1 || val2 != oval2;
161         tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = val1 ? tea->mleft : 0;
162         tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = val2 ? tea->mright : 0;
163         bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
164         bytes[1] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT];
165         bytes[2] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT];
166         if ((err = snd_i2c_sendbytes(tea->device, bytes, 3)) < 0)
167                 change = err;
168         snd_i2c_unlock(tea->bus);
169         return change;
170 }
171
172 #define TEA6330T_BASS(xname, xindex) \
173 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
174   .info = snd_tea6330t_info_bass, \
175   .get = snd_tea6330t_get_bass, .put = snd_tea6330t_put_bass }
176
177 static int snd_tea6330t_info_bass(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
178 {
179         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
180
181         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
182         uinfo->count = 1;
183         uinfo->value.integer.min = 0;
184         uinfo->value.integer.max = tea->max_bass;
185         return 0;
186 }
187
188 static int snd_tea6330t_get_bass(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
189 {
190         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
191         
192         ucontrol->value.integer.value[0] = tea->bass;
193         return 0;
194 }
195
196 static int snd_tea6330t_put_bass(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
197 {
198         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
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
221 static int snd_tea6330t_info_treble(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
222 {
223         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
224
225         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
226         uinfo->count = 1;
227         uinfo->value.integer.min = 0;
228         uinfo->value.integer.max = tea->max_treble;
229         return 0;
230 }
231
232 static int snd_tea6330t_get_treble(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
233 {
234         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
235         
236         ucontrol->value.integer.value[0] = tea->treble;
237         return 0;
238 }
239
240 static int snd_tea6330t_put_treble(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
241 {
242         tea6330t_t *tea = snd_kcontrol_chip(kcontrol);
243         int change, err;
244         unsigned char bytes[2];
245         unsigned char val1;
246         
247         val1 = ucontrol->value.integer.value[0] % (tea->max_treble + 1);
248         snd_i2c_lock(tea->bus);
249         tea->treble = val1;
250         val1 += 3;
251         change = tea->regs[TEA6330T_SADDR_TREBLE] != val1;
252         bytes[0] = TEA6330T_SADDR_TREBLE;
253         bytes[1] = tea->regs[TEA6330T_SADDR_TREBLE] = val1;
254         if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
255                 change = err;
256         snd_i2c_unlock(tea->bus);
257         return change;
258 }
259
260 static snd_kcontrol_new_t snd_tea6330t_controls[] = {
261 TEA6330T_MASTER_SWITCH("Master Playback Switch", 0),
262 TEA6330T_MASTER_VOLUME("Master Playback Volume", 0),
263 TEA6330T_BASS("Tone Control - Bass", 0),
264 TEA6330T_TREBLE("Tone Control - Treble", 0)
265 };
266
267 static void snd_tea6330_free(snd_i2c_device_t *device)
268 {
269         tea6330t_t *tea = device->private_data;
270         kfree(tea);
271 }
272                                         
273 int snd_tea6330t_update_mixer(snd_card_t * card,
274                               snd_i2c_bus_t *bus,
275                               int equalizer, int fader)
276 {
277         snd_i2c_device_t *device;
278         tea6330t_t *tea;
279         snd_kcontrol_new_t *knew;
280         unsigned int idx;
281         int err = -ENOMEM;
282         u8 default_treble, default_bass;
283         unsigned char bytes[7];
284
285         tea = kcalloc(1, sizeof(*tea), GFP_KERNEL);
286         if (tea == NULL)
287                 return -ENOMEM;
288         if ((err = snd_i2c_device_create(bus, "TEA6330T", TEA6330T_ADDR, &device)) < 0) {
289                 kfree(tea);
290                 return err;
291         }
292         tea->device = device;
293         tea->bus = bus;
294         tea->equalizer = equalizer;
295         tea->fader = fader;
296         device->private_data = tea;
297         device->private_free = snd_tea6330_free;
298
299         snd_i2c_lock(bus);
300
301         /* turn fader off and handle equalizer */
302         tea->regs[TEA6330T_SADDR_FADER] = 0x3f;
303         tea->regs[TEA6330T_SADDR_AUDIO_SWITCH] = equalizer ? 0 : TEA6330T_EQN;
304         /* initialize mixer */
305         if (!tea->equalizer) {
306                 tea->max_bass = 9;
307                 tea->max_treble = 8;
308                 default_bass = 3 + 4;
309                 tea->bass = 4;
310                 default_treble = 3 + 4;
311                 tea->treble = 4;
312         } else {
313                 tea->max_bass = 5;
314                 tea->max_treble = 0;
315                 default_bass = 7 + 4;
316                 tea->bass = 4;
317                 default_treble = 3;
318                 tea->treble = 0;
319         }
320         tea->mleft = tea->mright = 0x14;
321         tea->regs[TEA6330T_SADDR_BASS] = default_bass;
322         tea->regs[TEA6330T_SADDR_TREBLE] = default_treble;
323
324         /* compose I2C message and put the hardware to initial state */
325         bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
326         for (idx = 0; idx < 6; idx++)
327                 bytes[idx+1] = tea->regs[idx];
328         if ((err = snd_i2c_sendbytes(device, bytes, 7)) < 0)
329                 goto __error;
330
331         strcat(card->mixername, ",TEA6330T");
332         if ((err = snd_component_add(card, "TEA6330T")) < 0)
333                 goto __error;
334
335         for (idx = 0; idx < ARRAY_SIZE(snd_tea6330t_controls); idx++) {
336                 knew = &snd_tea6330t_controls[idx];
337                 if (tea->treble == 0 && !strcmp(knew->name, "Tone Control - Treble"))
338                         continue;
339                 if ((err = snd_ctl_add(card, snd_ctl_new1(knew, tea))) < 0)
340                         goto __error;
341         }
342
343         snd_i2c_unlock(bus);
344         return 0;
345         
346       __error:
347         snd_i2c_unlock(bus);
348         snd_i2c_device_free(device);
349         return err;
350 }
351
352 EXPORT_SYMBOL(snd_tea6330t_detect);
353 EXPORT_SYMBOL(snd_tea6330t_update_mixer);
354
355 /*
356  *  INIT part
357  */
358
359 static int __init alsa_tea6330t_init(void)
360 {
361         return 0;
362 }
363
364 static void __exit alsa_tea6330t_exit(void)
365 {
366 }
367
368 module_init(alsa_tea6330t_init)
369 module_exit(alsa_tea6330t_exit)