Fix compile error. DEC guys, nobody of you compiling this stuff or
[linux-2.6/linux-mips.git] / drivers / char / tda7432.c
blob4880913106d2e0a39c2fc9a5a23f8fe3d8715c51
1 /*
2 * For the STS-Thompson TDA7432 audio processor chip
3 *
4 * Handles audio functions: volume, balance, tone, loudness
5 * This driver will not complain if used with any
6 * other i2c device with the same address.
8 * Copyright (c) 2000 Eric Sandeen <eric_sandeen@bigfoot.com>
9 * This code is placed under the terms of the GNU General Public License
10 * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
11 * Which was based on tda8425.c by Greg Alexander (c) 1998
13 * OPTIONS:
14 * debug - set to 1 if you'd like to see debug messages
15 * set to 2 if you'd like to be inundated with debug messages
17 * loudness - set between 0 and 15 for varying degrees of loudness effect
19 * TODO:
20 * Implement tone controls
22 * Revision: 0.3 - Fixed silly reversed volume controls. :)
23 * Revision: 0.2 - Cleaned up #defines
24 * fixed volume control
25 * Added I2C_DRIVERID_TDA7432
26 * added loudness insmod control
27 * Revision: 0.1 - initial version
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/string.h>
34 #include <linux/timer.h>
35 #include <linux/delay.h>
36 #include <linux/errno.h>
37 #include <linux/malloc.h>
38 #include <linux/videodev.h>
39 #include <linux/i2c.h>
40 #include <linux/i2c-algo-bit.h>
42 #include "bttv.h"
43 #include "audiochip.h"
45 /* This driver ID is brand new, so define it if it's not in i2c-id.h yet */
46 #ifndef I2C_DRIVERID_TDA7432
47 #define I2C_DRIVERID_TDA7432 27
48 #endif
51 MODULE_AUTHOR("Eric Sandeen <eric_sandeen@bigfoot.com>");
52 MODULE_DESCRIPTION("bttv driver for the tda7432 audio processor chip");
54 MODULE_PARM(debug,"i");
55 MODULE_PARM(loudness,"i");
56 static int loudness = 0; /* disable loudness by default */
57 static int debug = 0; /* insmod parameter */
60 /* Address to scan (I2C address of this chip) */
61 static unsigned short normal_i2c[] = {
62 I2C_TDA7432 >> 1,
63 I2C_CLIENT_END};
64 static unsigned short normal_i2c_range[] = {I2C_CLIENT_END};
65 static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
66 static unsigned short probe_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
67 static unsigned short ignore[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
68 static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
69 static unsigned short force[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
70 static struct i2c_client_address_data addr_data = {
71 normal_i2c, normal_i2c_range,
72 probe, probe_range,
73 ignore, ignore_range,
74 force
77 /* Structure of address and subaddresses for the tda7432 */
79 struct tda7432 {
80 int addr;
81 int input;
82 int volume;
83 int tone;
84 int lf, lr, rf, rr;
85 int loud;
88 static struct i2c_driver driver;
89 static struct i2c_client client_template;
91 #define dprintk if (debug) printk
92 #define d2printk if (debug > 1) printk
94 /* The TDA7432 is made by STS-Thompson
95 * http://www.st.com
96 * http://us.st.com/stonline/books/pdf/docs/4056.pdf
98 * TDA7432: I2C-bus controlled basic audio processor
100 * The TDA7432 controls basic audio functions like volume, balance,
101 * and tone control (including loudness). It also has four channel
102 * output (for front and rear). Since most vidcap cards probably
103 * don't have 4 channel output, this driver will set front & rear
104 * together (no independent control).
107 /* Subaddresses for TDA7432 */
109 #define TDA7432_IN 0x00 /* Input select */
110 #define TDA7432_VL 0x01 /* Volume */
111 #define TDA7432_TN 0x02 /* Bass, Treble (Tone) */
112 #define TDA7432_LF 0x03 /* Attenuation LF (Left Front) */
113 #define TDA7432_LR 0x04 /* Attenuation LR (Left Rear) */
114 #define TDA7432_RF 0x05 /* Attenuation RF (Right Front) */
115 #define TDA7432_RR 0x06 /* Attenuation RR (Right Rear) */
116 #define TDA7432_LD 0x07 /* Loudness */
119 /* Masks for bits in TDA7432 subaddresses */
121 /* Many of these not used - just for documentation */
123 /* Subaddress 0x00 - Input selection and bass control */
125 /* Bits 0,1,2 control input:
126 * 0x00 - Stereo input
127 * 0x02 - Mono input
128 * 0x03 - Mute
129 * Mono probably isn't used - I'm guessing only the stereo
130 * input is connected on most cards, so we'll set it to stereo.
132 * Bit 3 controls bass cut: 0/1 is non-symmetric/symmetric bass cut
133 * Bit 4 controls bass range: 0/1 is extended/standard bass range
135 * Highest 3 bits not used
138 #define TDA7432_STEREO_IN 0
139 #define TDA7432_MONO_IN 2 /* Probably won't be used */
140 #define TDA7432_MUTE 3 /* Probably won't be used */
141 #define TDA7432_BASS_SYM 1 << 3
142 #define TDA7432_BASS_NORM 1 << 4
144 /* Subaddress 0x01 - Volume */
146 /* Lower 7 bits control volume from -79dB to +32dB in 1dB steps
147 * Recommended maximum is +20 dB
149 * +32dB: 0x00
150 * +20dB: 0x0c
151 * 0dB: 0x20
152 * -79dB: 0x6f
154 * MSB (bit 7) controls loudness: 1/0 is loudness on/off
157 #define TDA7432_VOL_0DB 0x20
158 #define TDA7432_LD_ON 1 << 7
161 /* Subaddress 0x02 - Tone control */
163 /* Bits 0,1,2 control absolute treble gain from 0dB to 14dB
164 * 0x0 is 14dB, 0x7 is 0dB
166 * Bit 3 controls treble attenuation/gain (sign)
167 * 1 = gain (+)
168 * 0 = attenuation (-)
170 * Bits 4,5,6 control absolute bass gain from 0dB to 14dB
171 * (This is only true for normal base range, set in 0x00)
172 * 0x0 << 4 is 14dB, 0x7 is 0dB
174 * Bit 7 controls bass attenuation/gain (sign)
175 * 1 << 7 = gain (+)
176 * 0 << 7 = attenuation (-)
178 * Example:
179 * 1 1 0 1 0 1 0 1 is +4dB bass, -4dB treble
182 #define TDA7432_TREBLE_0DB 0xf
183 #define TDA7432_TREBLE 7
184 #define TDA7432_TREBLE_GAIN 1 << 3
185 #define TDA7432_BASS_0DB 0xf << 4
186 #define TDA7432_BASS 7 << 4
187 #define TDA7432_BASS_GAIN 1 << 7
190 /* Subaddress 0x03 - Left Front attenuation */
191 /* Subaddress 0x04 - Left Rear attenuation */
192 /* Subaddress 0x05 - Right Front attenuation */
193 /* Subaddress 0x06 - Right Rear attenuation */
195 /* Bits 0,1,2,3,4 control attenuation from 0dB to -37.5dB
196 * in 1.5dB steps.
198 * 0x00 is 0dB
199 * 0x1f is -37.5dB
201 * Bit 5 mutes that channel when set (1 = mute, 0 = unmute)
202 * We'll use the mute on the input, though (above)
203 * Bits 6,7 unused
206 #define TDA7432_ATTEN_0DB 0x00
209 /* Subaddress 0x07 - Loudness Control */
211 /* Bits 0,1,2,3 control loudness from 0dB to -15dB in 1dB steps
212 * when bit 4 is NOT set
214 * 0x0 is 0dB
215 * 0xf is -15dB
217 * If bit 4 is set, then there is a flat attenuation according to
218 * the lower 4 bits, as above.
220 * Bits 5,6,7 unused
225 /* Begin code */
227 static int tda7432_write(struct i2c_client *client, int subaddr, int val)
229 unsigned char buffer[2];
230 d2printk("tda7432: In tda7432_write\n");
231 dprintk("tda7432: Writing %d 0x%x\n", subaddr, val);
232 buffer[0] = subaddr;
233 buffer[1] = val;
234 if (2 != i2c_master_send(client,buffer,2)) {
235 printk(KERN_WARNING "tda7432: I/O error, trying (write %d 0x%x)\n",
236 subaddr, val);
237 return -1;
239 return 0;
242 /* I don't think we ever actually _read_ the chip... */
243 #if 0
244 static int tda7432_read(struct i2c_client *client)
246 unsigned char buffer;
247 d2printk("tda7432: In tda7432_read\n");
248 if (1 != i2c_master_recv(client,&buffer,1)) {
249 printk(KERN_WARNING "tda7432: I/O error, trying (read)\n");
250 return -1;
252 dprintk("tda7432: Read 0x%02x\n", buffer);
253 return buffer;
255 #endif
257 static int tda7432_set(struct i2c_client *client)
259 struct tda7432 *t = client->data;
260 unsigned char buf[16];
261 d2printk("tda7432: In tda7432_set\n");
263 dprintk(KERN_INFO
264 "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
265 t->input,t->volume,t->tone,t->lf,t->lr,t->rf,t->rr,t->loud);
266 buf[0] = TDA7432_IN;
267 buf[1] = t->input;
268 buf[2] = t->volume;
269 buf[3] = t->tone;
270 buf[4] = t->lf;
271 buf[5] = t->lr;
272 buf[6] = t->rf;
273 buf[7] = t->rr;
274 buf[8] = t->loud;
275 if (9 != i2c_master_send(client,buf,9)) {
276 printk(KERN_WARNING "tda7432: I/O error, trying tda7432_set\n");
277 return -1;
280 return 0;
283 static void do_tda7432_init(struct i2c_client *client)
285 struct tda7432 *t = client->data;
286 d2printk("tda7432: In tda7432_init\n");
288 t->input = TDA7432_STEREO_IN | /* Main (stereo) input */
289 TDA7432_BASS_SYM | /* Symmetric bass cut */
290 TDA7432_BASS_NORM; /* Normal bass range */
291 t->volume = TDA7432_VOL_0DB; /* 0dB Volume */
292 if (loudness) /* Turn loudness on? */
293 t->volume |= TDA7432_LD_ON;
294 t->tone = TDA7432_TREBLE_0DB | /* 0dB Treble */
295 TDA7432_BASS_0DB; /* 0dB Bass */
296 t->lf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
297 t->lr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
298 t->rf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
299 t->rr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
300 t->loud = loudness; /* insmod parameter */
302 tda7432_set(client);
305 /* *********************** *
306 * i2c interface functions *
307 * *********************** */
309 static int tda7432_attach(struct i2c_adapter *adap, int addr,
310 unsigned short flags, int kind)
312 struct tda7432 *t;
313 struct i2c_client *client;
314 d2printk("tda7432: In tda7432_attach\n");
315 client = kmalloc(sizeof *client,GFP_KERNEL);
316 if (!client)
317 return -ENOMEM;
318 memcpy(client,&client_template,sizeof(struct i2c_client));
319 client->adapter = adap;
320 client->addr = addr;
322 client->data = t = kmalloc(sizeof *t,GFP_KERNEL);
323 if (!t)
324 return -ENOMEM;
325 memset(t,0,sizeof *t);
326 do_tda7432_init(client);
327 MOD_INC_USE_COUNT;
328 strcpy(client->name,"TDA7432");
329 printk(KERN_INFO "tda7432: init\n");
331 i2c_attach_client(client);
332 return 0;
335 static int tda7432_probe(struct i2c_adapter *adap)
337 if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848))
338 return i2c_probe(adap, &addr_data, tda7432_attach);
339 return 0;
342 static int tda7432_detach(struct i2c_client *client)
344 struct tda7432 *t = client->data;
346 do_tda7432_init(client);
347 i2c_detach_client(client);
349 kfree(t);
350 kfree(client);
351 MOD_DEC_USE_COUNT;
352 return 0;
355 static int tda7432_command(struct i2c_client *client,
356 unsigned int cmd, void *arg)
358 struct tda7432 *t = client->data;
359 d2printk("tda7432: In tda7432_command\n");
360 #if 0
361 __u16 *sarg = arg;
362 #endif
364 switch (cmd) {
365 /* --- v4l ioctls --- */
366 /* take care: bttv does userspace copying, we'll get a
367 kernel pointer here... */
369 /* Query card - scale from TDA7432 settings to V4L settings */
370 case VIDIOCGAUDIO:
372 struct video_audio *va = arg;
373 dprintk("tda7432: VIDIOCGAUDIO\n");
375 va->flags |= VIDEO_AUDIO_VOLUME |
376 VIDEO_AUDIO_BASS |
377 VIDEO_AUDIO_TREBLE;
379 /* Master volume control
380 * V4L volume is min 0, max 65535
381 * TDA7432 Volume:
382 * Min (-79dB) is 0x6f
383 * Max (+20dB) is 0x07
384 * (Mask out bit 7 of vol - it's for the loudness setting)
387 va->volume = ( 0x6f - (t->volume & 0x7F) ) * 630;
389 /* Balance depends on L,R attenuation
390 * V4L balance is 0 to 65535, middle is 32768
391 * TDA7432 attenuation: min (0dB) is 0, max (-37.5dB) is 0x1f
392 * to scale up to V4L numbers, mult by 1057
393 * attenuation exists for lf, lr, rf, rr
394 * we use only lf and rf (front channels)
397 if ( (t->lf) < (t->rf) )
398 /* right is attenuated, balance shifted left */
399 va->balance = (32768 - 1057*(t->rf));
400 else
401 /* left is attenuated, balance shifted right */
402 va->balance = (32768 + 1057*(t->lf));
404 /* Bass/treble */
405 va->bass = 32768; /* brain hurts... set to middle for now */
406 va->treble = 32768; /* brain hurts... set to middle for now */
408 break; /* VIDIOCGAUDIO case */
411 /* Set card - scale from V4L settings to TDA7432 settings */
412 case VIDIOCSAUDIO:
414 struct video_audio *va = arg;
415 dprintk("tda7432: VIDEOCSAUDIO\n");
417 t->volume = 0x6f - ( (va->volume)/630 );
419 if (loudness) /* Turn on the loudness bit */
420 t->volume |= TDA7432_LD_ON;
422 if (va->balance < 32768) {
423 /* shifted to left, attenuate right */
424 t->rr = (32768 - va->balance)/1057;
425 t->rf = t->rr;
427 else {
428 /* shifted to right, attenuate left */
429 t->lf = (va->balance - 32768)/1057;
430 t->lr = t->lf;
433 /* t->tone = 0xff; */ /* Brain hurts - no tone control for now... */
435 tda7432_write(client,TDA7432_VL, t->volume);
436 /* tda7432_write(client,TDA7432_TN, t->tone); */
437 tda7432_write(client,TDA7432_LF, t->lf);
438 tda7432_write(client,TDA7432_LR, t->lr);
439 tda7432_write(client,TDA7432_RF, t->rf);
440 tda7432_write(client,TDA7432_RR, t->rr);
442 break;
444 } /* end of VIDEOCSAUDIO case */
446 default: /* Not VIDEOCGAUDIO or VIDEOCSAUDIO */
448 /* nothing */
449 d2printk("tda7432: Default\n");
451 } /* end of (cmd) switch */
453 return 0;
457 static struct i2c_driver driver = {
458 "i2c tda7432 driver",
459 I2C_DRIVERID_TDA7432,
460 I2C_DF_NOTIFY,
461 tda7432_probe,
462 tda7432_detach,
463 tda7432_command,
466 static struct i2c_client client_template =
468 "(unset)", /* name */
472 NULL,
473 &driver
476 #ifdef MODULE
477 int init_module(void)
478 #else
479 int tda7432_init(void)
480 #endif
483 if ( (loudness < 0) || (loudness > 15) )
485 printk(KERN_ERR "tda7432: loudness parameter must be between 0 and 15\n");
486 return -EINVAL;
490 i2c_add_driver(&driver);
491 return 0;
494 #ifdef MODULE
495 void cleanup_module(void)
497 i2c_del_driver(&driver);
499 #endif
502 * Local variables:
503 * c-basic-offset: 8
504 * End: