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