Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / tda7432.c
blob376a4a439e9b52eaf729951e33b96cc09f2a53c9
1 /*
2 * For the STS-Thompson TDA7432 audio processor chip
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 * Muting and tone control by Jonathan Isom <jisom@ematic.com>
10 * Copyright (c) 2000 Eric Sandeen <eric_sandeen@bigfoot.com>
11 * This code is placed under the terms of the GNU General Public License
12 * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
13 * Which was based on tda8425.c by Greg Alexander (c) 1998
15 * OPTIONS:
16 * debug - set to 1 if you'd like to see debug messages
17 * set to 2 if you'd like to be inundated with debug messages
19 * loudness - set between 0 and 15 for varying degrees of loudness effect
21 * maxvol - set maximium volume to +20db (1), default is 0db(0)
24 * Revision: 0.7 - maxvol module parm to set maximium volume 0db or +20db
25 * store if muted so we can return it
26 * change balance only if flaged to
27 * Revision: 0.6 - added tone controls
28 * Revision: 0.5 - Fixed odd balance problem
29 * Revision: 0.4 - added muting
30 * Revision: 0.3 - Fixed silly reversed volume controls. :)
31 * Revision: 0.2 - Cleaned up #defines
32 * fixed volume control
33 * Added I2C_DRIVERID_TDA7432
34 * added loudness insmod control
35 * Revision: 0.1 - initial version
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/kernel.h>
41 #include <linux/sched.h>
42 #include <linux/string.h>
43 #include <linux/timer.h>
44 #include <linux/delay.h>
45 #include <linux/errno.h>
46 #include <linux/slab.h>
47 #include <linux/videodev.h>
48 #include <linux/i2c.h>
49 #include <linux/i2c-algo-bit.h>
51 #include "bttv.h"
52 #include <media/audiochip.h>
53 #include <media/id.h>
55 #ifndef VIDEO_AUDIO_BALANCE
56 # define VIDEO_AUDIO_BALANCE 32
57 #endif
59 MODULE_AUTHOR("Eric Sandeen <eric_sandeen@bigfoot.com>");
60 MODULE_DESCRIPTION("bttv driver for the tda7432 audio processor chip");
61 MODULE_LICENSE("GPL");
63 static int maxvol;
64 static int loudness; /* disable loudness by default */
65 static int debug; /* insmod parameter */
66 module_param(debug, int, S_IRUGO | S_IWUSR);
67 module_param(loudness, int, S_IRUGO);
68 MODULE_PARM_DESC(maxvol,"Set maximium volume to +20db (0), default is 0db(1)");
69 module_param(maxvol, int, S_IRUGO | S_IWUSR);
72 /* Address to scan (I2C address of this chip) */
73 static unsigned short normal_i2c[] = {
74 I2C_TDA7432 >> 1,
75 I2C_CLIENT_END,
77 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END, I2C_CLIENT_END };
78 I2C_CLIENT_INSMOD;
80 /* Structure of address and subaddresses for the tda7432 */
82 struct tda7432 {
83 int addr;
84 int input;
85 int volume;
86 int muted;
87 int bass, treble;
88 int lf, lr, rf, rr;
89 int loud;
90 struct i2c_client c;
92 static struct i2c_driver driver;
93 static struct i2c_client client_template;
95 #define dprintk if (debug) printk
96 #define d2printk if (debug > 1) printk
98 /* The TDA7432 is made by STS-Thompson
99 * http://www.st.com
100 * http://us.st.com/stonline/books/pdf/docs/4056.pdf
102 * TDA7432: I2C-bus controlled basic audio processor
104 * The TDA7432 controls basic audio functions like volume, balance,
105 * and tone control (including loudness). It also has four channel
106 * output (for front and rear). Since most vidcap cards probably
107 * don't have 4 channel output, this driver will set front & rear
108 * together (no independent control).
111 /* Subaddresses for TDA7432 */
113 #define TDA7432_IN 0x00 /* Input select */
114 #define TDA7432_VL 0x01 /* Volume */
115 #define TDA7432_TN 0x02 /* Bass, Treble (Tone) */
116 #define TDA7432_LF 0x03 /* Attenuation LF (Left Front) */
117 #define TDA7432_LR 0x04 /* Attenuation LR (Left Rear) */
118 #define TDA7432_RF 0x05 /* Attenuation RF (Right Front) */
119 #define TDA7432_RR 0x06 /* Attenuation RR (Right Rear) */
120 #define TDA7432_LD 0x07 /* Loudness */
123 /* Masks for bits in TDA7432 subaddresses */
125 /* Many of these not used - just for documentation */
127 /* Subaddress 0x00 - Input selection and bass control */
129 /* Bits 0,1,2 control input:
130 * 0x00 - Stereo input
131 * 0x02 - Mono input
132 * 0x03 - Mute (Using Attenuators Plays better with modules)
133 * Mono probably isn't used - I'm guessing only the stereo
134 * input is connected on most cards, so we'll set it to stereo.
136 * Bit 3 controls bass cut: 0/1 is non-symmetric/symmetric bass cut
137 * Bit 4 controls bass range: 0/1 is extended/standard bass range
139 * Highest 3 bits not used
142 #define TDA7432_STEREO_IN 0
143 #define TDA7432_MONO_IN 2 /* Probably won't be used */
144 #define TDA7432_BASS_SYM 1 << 3
145 #define TDA7432_BASS_NORM 1 << 4
147 /* Subaddress 0x01 - Volume */
149 /* Lower 7 bits control volume from -79dB to +32dB in 1dB steps
150 * Recommended maximum is +20 dB
152 * +32dB: 0x00
153 * +20dB: 0x0c
154 * 0dB: 0x20
155 * -79dB: 0x6f
157 * MSB (bit 7) controls loudness: 1/0 is loudness on/off
160 #define TDA7432_VOL_0DB 0x20
161 #define TDA7432_LD_ON 1 << 7
164 /* Subaddress 0x02 - Tone control */
166 /* Bits 0,1,2 control absolute treble gain from 0dB to 14dB
167 * 0x0 is 14dB, 0x7 is 0dB
169 * Bit 3 controls treble attenuation/gain (sign)
170 * 1 = gain (+)
171 * 0 = attenuation (-)
173 * Bits 4,5,6 control absolute bass gain from 0dB to 14dB
174 * (This is only true for normal base range, set in 0x00)
175 * 0x0 << 4 is 14dB, 0x7 is 0dB
177 * Bit 7 controls bass attenuation/gain (sign)
178 * 1 << 7 = gain (+)
179 * 0 << 7 = attenuation (-)
181 * Example:
182 * 1 1 0 1 0 1 0 1 is +4dB bass, -4dB treble
185 #define TDA7432_TREBLE_0DB 0xf
186 #define TDA7432_TREBLE 7
187 #define TDA7432_TREBLE_GAIN 1 << 3
188 #define TDA7432_BASS_0DB 0xf
189 #define TDA7432_BASS 7 << 4
190 #define TDA7432_BASS_GAIN 1 << 7
193 /* Subaddress 0x03 - Left Front attenuation */
194 /* Subaddress 0x04 - Left Rear attenuation */
195 /* Subaddress 0x05 - Right Front attenuation */
196 /* Subaddress 0x06 - Right Rear attenuation */
198 /* Bits 0,1,2,3,4 control attenuation from 0dB to -37.5dB
199 * in 1.5dB steps.
201 * 0x00 is 0dB
202 * 0x1f is -37.5dB
204 * Bit 5 mutes that channel when set (1 = mute, 0 = unmute)
205 * We'll use the mute on the input, though (above)
206 * Bits 6,7 unused
209 #define TDA7432_ATTEN_0DB 0x00
210 #define TDA7432_MUTE 0x1 << 5
213 /* Subaddress 0x07 - Loudness Control */
215 /* Bits 0,1,2,3 control loudness from 0dB to -15dB in 1dB steps
216 * when bit 4 is NOT set
218 * 0x0 is 0dB
219 * 0xf is -15dB
221 * If bit 4 is set, then there is a flat attenuation according to
222 * the lower 4 bits, as above.
224 * Bits 5,6,7 unused
229 /* Begin code */
231 static int tda7432_write(struct i2c_client *client, int subaddr, int val)
233 unsigned char buffer[2];
234 d2printk("tda7432: In tda7432_write\n");
235 dprintk("tda7432: Writing %d 0x%x\n", subaddr, val);
236 buffer[0] = subaddr;
237 buffer[1] = val;
238 if (2 != i2c_master_send(client,buffer,2)) {
239 printk(KERN_WARNING "tda7432: I/O error, trying (write %d 0x%x)\n",
240 subaddr, val);
241 return -1;
243 return 0;
246 /* I don't think we ever actually _read_ the chip... */
247 #if 0
248 static int tda7432_read(struct i2c_client *client)
250 unsigned char buffer;
251 d2printk("tda7432: In tda7432_read\n");
252 if (1 != i2c_master_recv(client,&buffer,1)) {
253 printk(KERN_WARNING "tda7432: I/O error, trying (read)\n");
254 return -1;
256 dprintk("tda7432: Read 0x%02x\n", buffer);
257 return buffer;
259 #endif
261 static int tda7432_set(struct i2c_client *client)
263 struct tda7432 *t = i2c_get_clientdata(client);
264 unsigned char buf[16];
265 d2printk("tda7432: In tda7432_set\n");
267 dprintk(KERN_INFO
268 "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
269 t->input,t->volume,t->bass,t->treble,t->lf,t->lr,t->rf,t->rr,t->loud);
270 buf[0] = TDA7432_IN;
271 buf[1] = t->input;
272 buf[2] = t->volume;
273 buf[3] = t->bass;
274 buf[4] = t->treble;
275 buf[5] = t->lf;
276 buf[6] = t->lr;
277 buf[7] = t->rf;
278 buf[8] = t->rr;
279 buf[9] = t->loud;
280 if (10 != i2c_master_send(client,buf,10)) {
281 printk(KERN_WARNING "tda7432: I/O error, trying tda7432_set\n");
282 return -1;
285 return 0;
288 static void do_tda7432_init(struct i2c_client *client)
290 struct tda7432 *t = i2c_get_clientdata(client);
291 d2printk("tda7432: In tda7432_init\n");
293 t->input = TDA7432_STEREO_IN | /* Main (stereo) input */
294 TDA7432_BASS_SYM | /* Symmetric bass cut */
295 TDA7432_BASS_NORM; /* Normal bass range */
296 t->volume = 0x3b ; /* -27dB Volume */
297 if (loudness) /* Turn loudness on? */
298 t->volume |= TDA7432_LD_ON;
299 t->muted = VIDEO_AUDIO_MUTE;
300 t->treble = TDA7432_TREBLE_0DB; /* 0dB Treble */
301 t->bass = TDA7432_BASS_0DB; /* 0dB Bass */
302 t->lf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
303 t->lr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
304 t->rf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
305 t->rr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
306 t->loud = loudness; /* insmod parameter */
308 tda7432_set(client);
311 /* *********************** *
312 * i2c interface functions *
313 * *********************** */
315 static int tda7432_attach(struct i2c_adapter *adap, int addr, int kind)
317 struct tda7432 *t;
318 struct i2c_client *client;
319 d2printk("tda7432: In tda7432_attach\n");
321 t = kmalloc(sizeof *t,GFP_KERNEL);
322 if (!t)
323 return -ENOMEM;
324 memset(t,0,sizeof *t);
326 client = &t->c;
327 memcpy(client,&client_template,sizeof(struct i2c_client));
328 client->adapter = adap;
329 client->addr = addr;
330 i2c_set_clientdata(client, t);
332 do_tda7432_init(client);
333 printk(KERN_INFO "tda7432: init\n");
335 i2c_attach_client(client);
336 return 0;
339 static int tda7432_probe(struct i2c_adapter *adap)
341 #ifdef I2C_CLASS_TV_ANALOG
342 if (adap->class & I2C_CLASS_TV_ANALOG)
343 return i2c_probe(adap, &addr_data, tda7432_attach);
344 #else
345 if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848))
346 return i2c_probe(adap, &addr_data, tda7432_attach);
347 #endif
348 return 0;
351 static int tda7432_detach(struct i2c_client *client)
353 struct tda7432 *t = i2c_get_clientdata(client);
355 do_tda7432_init(client);
356 i2c_detach_client(client);
358 kfree(t);
359 return 0;
362 static int tda7432_command(struct i2c_client *client,
363 unsigned int cmd, void *arg)
365 struct tda7432 *t = i2c_get_clientdata(client);
366 d2printk("tda7432: In tda7432_command\n");
368 switch (cmd) {
369 /* --- v4l ioctls --- */
370 /* take care: bttv does userspace copying, we'll get a
371 kernel pointer here... */
373 /* Query card - scale from TDA7432 settings to V4L settings */
374 case VIDIOCGAUDIO:
376 struct video_audio *va = arg;
377 dprintk("tda7432: VIDIOCGAUDIO\n");
379 va->flags |= VIDEO_AUDIO_VOLUME |
380 VIDEO_AUDIO_BASS |
381 VIDEO_AUDIO_TREBLE |
382 VIDEO_AUDIO_MUTABLE;
383 if (t->muted)
384 va->flags |= VIDEO_AUDIO_MUTE;
385 va->mode |= VIDEO_SOUND_STEREO;
386 /* Master volume control
387 * V4L volume is min 0, max 65535
388 * TDA7432 Volume:
389 * Min (-79dB) is 0x6f
390 * Max (+20dB) is 0x07 (630)
391 * Max (0dB) is 0x20 (829)
392 * (Mask out bit 7 of vol - it's for the loudness setting)
394 if (!maxvol){ /* max +20db */
395 va->volume = ( 0x6f - (t->volume & 0x7F) ) * 630;
396 } else { /* max 0db */
397 va->volume = ( 0x6f - (t->volume & 0x7F) ) * 829;
400 /* Balance depends on L,R attenuation
401 * V4L balance is 0 to 65535, middle is 32768
402 * TDA7432 attenuation: min (0dB) is 0, max (-37.5dB) is 0x1f
403 * to scale up to V4L numbers, mult by 1057
404 * attenuation exists for lf, lr, rf, rr
405 * we use only lf and rf (front channels)
408 if ( (t->lf) < (t->rf) )
409 /* right is attenuated, balance shifted left */
410 va->balance = (32768 - 1057*(t->rf));
411 else
412 /* left is attenuated, balance shifted right */
413 va->balance = (32768 + 1057*(t->lf));
415 /* Bass/treble 4 bits each */
416 va->bass=t->bass;
417 if(va->bass >= 0x8)
418 va->bass = ~(va->bass - 0x8) & 0xf;
419 va->bass = (va->bass << 12)+(va->bass << 8)+(va->bass << 4)+(va->bass);
420 va->treble=t->treble;
421 if(va->treble >= 0x8)
422 va->treble = ~(va->treble - 0x8) & 0xf;
423 va->treble = (va->treble << 12)+(va->treble << 8)+(va->treble << 4)+(va->treble);
425 break; /* VIDIOCGAUDIO case */
428 /* Set card - scale from V4L settings to TDA7432 settings */
429 case VIDIOCSAUDIO:
431 struct video_audio *va = arg;
432 dprintk("tda7432: VIDEOCSAUDIO\n");
434 if(va->flags & VIDEO_AUDIO_VOLUME){
435 if(!maxvol){ /* max +20db */
436 t->volume = 0x6f - ((va->volume)/630);
437 } else { /* max 0db */
438 t->volume = 0x6f - ((va->volume)/829);
441 if (loudness) /* Turn on the loudness bit */
442 t->volume |= TDA7432_LD_ON;
444 tda7432_write(client,TDA7432_VL, t->volume);
447 if(va->flags & VIDEO_AUDIO_BASS)
449 t->bass = va->bass >> 12;
450 if(t->bass>= 0x8)
451 t->bass = (~t->bass & 0xf) + 0x8 ;
453 if(va->flags & VIDEO_AUDIO_TREBLE)
455 t->treble= va->treble >> 12;
456 if(t->treble>= 0x8)
457 t->treble = (~t->treble & 0xf) + 0x8 ;
459 if(va->flags & (VIDEO_AUDIO_TREBLE| VIDEO_AUDIO_BASS))
460 tda7432_write(client,TDA7432_TN, 0x10 | (t->bass << 4) | t->treble );
462 if(va->flags & VIDEO_AUDIO_BALANCE) {
463 if (va->balance < 32768)
465 /* shifted to left, attenuate right */
466 t->rr = (32768 - va->balance)/1057;
467 t->rf = t->rr;
468 t->lr = TDA7432_ATTEN_0DB;
469 t->lf = TDA7432_ATTEN_0DB;
471 else if(va->balance > 32769)
473 /* shifted to right, attenuate left */
474 t->lf = (va->balance - 32768)/1057;
475 t->lr = t->lf;
476 t->rr = TDA7432_ATTEN_0DB;
477 t->rf = TDA7432_ATTEN_0DB;
479 else
481 /* centered */
482 t->rr = TDA7432_ATTEN_0DB;
483 t->rf = TDA7432_ATTEN_0DB;
484 t->lf = TDA7432_ATTEN_0DB;
485 t->lr = TDA7432_ATTEN_0DB;
489 t->muted=(va->flags & VIDEO_AUDIO_MUTE);
490 if (t->muted)
492 /* Mute & update balance*/
493 tda7432_write(client,TDA7432_LF, t->lf | TDA7432_MUTE);
494 tda7432_write(client,TDA7432_LR, t->lr | TDA7432_MUTE);
495 tda7432_write(client,TDA7432_RF, t->rf | TDA7432_MUTE);
496 tda7432_write(client,TDA7432_RR, t->rr | TDA7432_MUTE);
497 } else {
498 tda7432_write(client,TDA7432_LF, t->lf);
499 tda7432_write(client,TDA7432_LR, t->lr);
500 tda7432_write(client,TDA7432_RF, t->rf);
501 tda7432_write(client,TDA7432_RR, t->rr);
504 break;
506 } /* end of VIDEOCSAUDIO case */
508 default: /* Not VIDEOCGAUDIO or VIDEOCSAUDIO */
510 /* nothing */
511 d2printk("tda7432: Default\n");
513 } /* end of (cmd) switch */
515 return 0;
518 static struct i2c_driver driver = {
519 .owner = THIS_MODULE,
520 .name = "i2c tda7432 driver",
521 .id = I2C_DRIVERID_TDA7432,
522 .flags = I2C_DF_NOTIFY,
523 .attach_adapter = tda7432_probe,
524 .detach_client = tda7432_detach,
525 .command = tda7432_command,
528 static struct i2c_client client_template =
530 I2C_DEVNAME("tda7432"),
531 .driver = &driver,
534 static int __init tda7432_init(void)
536 if ( (loudness < 0) || (loudness > 15) ) {
537 printk(KERN_ERR "tda7432: loudness parameter must be between 0 and 15\n");
538 return -EINVAL;
541 return i2c_add_driver(&driver);
544 static void __exit tda7432_fini(void)
546 i2c_del_driver(&driver);
549 module_init(tda7432_init);
550 module_exit(tda7432_fini);
553 * Local variables:
554 * c-basic-offset: 8
555 * End: