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 * Copyright (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
12 * This code is placed under the terms of the GNU General Public License
13 * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
14 * Which was based on tda8425.c by Greg Alexander (c) 1998
17 * debug - set to 1 if you'd like to see debug messages
18 * set to 2 if you'd like to be inundated with debug messages
20 * loudness - set between 0 and 15 for varying degrees of loudness effect
22 * maxvol - set maximium volume to +20db (1), default is 0db(0)
25 * Revision: 0.7 - maxvol module parm to set maximium volume 0db or +20db
26 * store if muted so we can return it
27 * change balance only if flaged to
28 * Revision: 0.6 - added tone controls
29 * Revision: 0.5 - Fixed odd balance problem
30 * Revision: 0.4 - added muting
31 * Revision: 0.3 - Fixed silly reversed volume controls. :)
32 * Revision: 0.2 - Cleaned up #defines
33 * fixed volume control
34 * Added I2C_DRIVERID_TDA7432
35 * added loudness insmod control
36 * Revision: 0.1 - initial version
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/kernel.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>
50 #include <media/v4l2-common.h>
51 #include <media/i2c-addr.h>
53 #ifndef VIDEO_AUDIO_BALANCE
54 # define VIDEO_AUDIO_BALANCE 32
57 MODULE_AUTHOR("Eric Sandeen <eric_sandeen@bigfoot.com>");
58 MODULE_DESCRIPTION("bttv driver for the tda7432 audio processor chip");
59 MODULE_LICENSE("GPL");
62 static int loudness; /* disable loudness by default */
63 static int debug; /* insmod parameter */
64 module_param(debug, int, S_IRUGO | S_IWUSR);
65 module_param(loudness, int, S_IRUGO);
66 MODULE_PARM_DESC(maxvol,"Set maximium volume to +20db (0), default is 0db(1)");
67 module_param(maxvol, int, S_IRUGO | S_IWUSR);
70 /* Address to scan (I2C address of this chip) */
71 static unsigned short normal_i2c[] = {
72 I2C_ADDR_TDA7432 >> 1,
78 /* Structure of address and subaddresses for the tda7432 */
90 static struct i2c_driver driver;
91 static struct i2c_client client_template;
93 /* The TDA7432 is made by STS-Thompson
95 * http://us.st.com/stonline/books/pdf/docs/4056.pdf
97 * TDA7432: I2C-bus controlled basic audio processor
99 * The TDA7432 controls basic audio functions like volume, balance,
100 * and tone control (including loudness). It also has four channel
101 * output (for front and rear). Since most vidcap cards probably
102 * don't have 4 channel output, this driver will set front & rear
103 * together (no independent control).
106 /* Subaddresses for TDA7432 */
108 #define TDA7432_IN 0x00 /* Input select */
109 #define TDA7432_VL 0x01 /* Volume */
110 #define TDA7432_TN 0x02 /* Bass, Treble (Tone) */
111 #define TDA7432_LF 0x03 /* Attenuation LF (Left Front) */
112 #define TDA7432_LR 0x04 /* Attenuation LR (Left Rear) */
113 #define TDA7432_RF 0x05 /* Attenuation RF (Right Front) */
114 #define TDA7432_RR 0x06 /* Attenuation RR (Right Rear) */
115 #define TDA7432_LD 0x07 /* Loudness */
118 /* Masks for bits in TDA7432 subaddresses */
120 /* Many of these not used - just for documentation */
122 /* Subaddress 0x00 - Input selection and bass control */
124 /* Bits 0,1,2 control input:
125 * 0x00 - Stereo input
127 * 0x03 - Mute (Using Attenuators Plays better with modules)
128 * Mono probably isn't used - I'm guessing only the stereo
129 * input is connected on most cards, so we'll set it to stereo.
131 * Bit 3 controls bass cut: 0/1 is non-symmetric/symmetric bass cut
132 * Bit 4 controls bass range: 0/1 is extended/standard bass range
134 * Highest 3 bits not used
137 #define TDA7432_STEREO_IN 0
138 #define TDA7432_MONO_IN 2 /* Probably won't be used */
139 #define TDA7432_BASS_SYM 1 << 3
140 #define TDA7432_BASS_NORM 1 << 4
142 /* Subaddress 0x01 - Volume */
144 /* Lower 7 bits control volume from -79dB to +32dB in 1dB steps
145 * Recommended maximum is +20 dB
152 * MSB (bit 7) controls loudness: 1/0 is loudness on/off
155 #define TDA7432_VOL_0DB 0x20
156 #define TDA7432_LD_ON 1 << 7
159 /* Subaddress 0x02 - Tone control */
161 /* Bits 0,1,2 control absolute treble gain from 0dB to 14dB
162 * 0x0 is 14dB, 0x7 is 0dB
164 * Bit 3 controls treble attenuation/gain (sign)
166 * 0 = attenuation (-)
168 * Bits 4,5,6 control absolute bass gain from 0dB to 14dB
169 * (This is only true for normal base range, set in 0x00)
170 * 0x0 << 4 is 14dB, 0x7 is 0dB
172 * Bit 7 controls bass attenuation/gain (sign)
174 * 0 << 7 = attenuation (-)
177 * 1 1 0 1 0 1 0 1 is +4dB bass, -4dB treble
180 #define TDA7432_TREBLE_0DB 0xf
181 #define TDA7432_TREBLE 7
182 #define TDA7432_TREBLE_GAIN 1 << 3
183 #define TDA7432_BASS_0DB 0xf
184 #define TDA7432_BASS 7 << 4
185 #define TDA7432_BASS_GAIN 1 << 7
188 /* Subaddress 0x03 - Left Front attenuation */
189 /* Subaddress 0x04 - Left Rear attenuation */
190 /* Subaddress 0x05 - Right Front attenuation */
191 /* Subaddress 0x06 - Right Rear attenuation */
193 /* Bits 0,1,2,3,4 control attenuation from 0dB to -37.5dB
199 * Bit 5 mutes that channel when set (1 = mute, 0 = unmute)
200 * We'll use the mute on the input, though (above)
204 #define TDA7432_ATTEN_0DB 0x00
205 #define TDA7432_MUTE 0x1 << 5
208 /* Subaddress 0x07 - Loudness Control */
210 /* Bits 0,1,2,3 control loudness from 0dB to -15dB in 1dB steps
211 * when bit 4 is NOT set
216 * If bit 4 is set, then there is a flat attenuation according to
217 * the lower 4 bits, as above.
226 static int tda7432_write(struct i2c_client *client, int subaddr, int val)
228 unsigned char buffer[2];
229 v4l_dbg(2, debug,client,"In tda7432_write\n");
230 v4l_dbg(1, debug,client,"Writing %d 0x%x\n", subaddr, val);
233 if (2 != i2c_master_send(client,buffer,2)) {
234 v4l_err(client,"I/O error, trying (write %d 0x%x)\n",
241 /* I don't think we ever actually _read_ the chip... */
243 static int tda7432_set(struct i2c_client *client)
245 struct tda7432 *t = i2c_get_clientdata(client);
246 unsigned char buf[16];
247 v4l_dbg(2, debug,client,"In tda7432_set\n");
249 v4l_dbg(1, debug,client,
250 "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
251 t->input,t->volume,t->bass,t->treble,t->lf,t->lr,t->rf,t->rr,t->loud);
262 if (10 != i2c_master_send(client,buf,10)) {
263 v4l_err(client,"I/O error, trying tda7432_set\n");
270 static void do_tda7432_init(struct i2c_client *client)
272 struct tda7432 *t = i2c_get_clientdata(client);
273 v4l_dbg(2, debug,client,"In tda7432_init\n");
275 t->input = TDA7432_STEREO_IN | /* Main (stereo) input */
276 TDA7432_BASS_SYM | /* Symmetric bass cut */
277 TDA7432_BASS_NORM; /* Normal bass range */
278 t->volume = 0x3b ; /* -27dB Volume */
279 if (loudness) /* Turn loudness on? */
280 t->volume |= TDA7432_LD_ON;
282 t->treble = TDA7432_TREBLE_0DB; /* 0dB Treble */
283 t->bass = TDA7432_BASS_0DB; /* 0dB Bass */
284 t->lf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
285 t->lr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
286 t->rf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
287 t->rr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
288 t->loud = loudness; /* insmod parameter */
293 /* *********************** *
294 * i2c interface functions *
295 * *********************** */
297 static int tda7432_attach(struct i2c_adapter *adap, int addr, int kind)
300 struct i2c_client *client;
302 t = kzalloc(sizeof *t,GFP_KERNEL);
307 memcpy(client,&client_template,sizeof(struct i2c_client));
308 client->adapter = adap;
310 i2c_set_clientdata(client, t);
312 do_tda7432_init(client);
313 i2c_attach_client(client);
315 v4l_info(client, "chip found @ 0x%x (%s)\n", addr << 1, adap->name);
319 static int tda7432_probe(struct i2c_adapter *adap)
321 if (adap->class & I2C_CLASS_TV_ANALOG)
322 return i2c_probe(adap, &addr_data, tda7432_attach);
326 static int tda7432_detach(struct i2c_client *client)
328 struct tda7432 *t = i2c_get_clientdata(client);
330 do_tda7432_init(client);
331 i2c_detach_client(client);
337 static int tda7432_get_ctrl(struct i2c_client *client,
338 struct v4l2_control *ctrl)
340 struct tda7432 *t = i2c_get_clientdata(client);
343 case V4L2_CID_AUDIO_MUTE:
344 ctrl->value=t->muted;
346 case V4L2_CID_AUDIO_VOLUME:
347 if (!maxvol){ /* max +20db */
348 ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 630;
349 } else { /* max 0db */
350 ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 829;
353 case V4L2_CID_AUDIO_BALANCE:
355 if ( (t->lf) < (t->rf) )
356 /* right is attenuated, balance shifted left */
357 ctrl->value = (32768 - 1057*(t->rf));
359 /* left is attenuated, balance shifted right */
360 ctrl->value = (32768 + 1057*(t->lf));
363 case V4L2_CID_AUDIO_BASS:
365 /* Bass/treble 4 bits each */
368 bass = ~(bass - 0x8) & 0xf;
369 ctrl->value = (bass << 12)+(bass << 8)+(bass << 4)+(bass);
372 case V4L2_CID_AUDIO_TREBLE:
374 int treble=t->treble;
376 treble = ~(treble - 0x8) & 0xf;
377 ctrl->value = (treble << 12)+(treble << 8)+(treble << 4)+(treble);
384 static int tda7432_set_ctrl(struct i2c_client *client,
385 struct v4l2_control *ctrl)
387 struct tda7432 *t = i2c_get_clientdata(client);
390 case V4L2_CID_AUDIO_MUTE:
391 t->muted=ctrl->value;
393 case V4L2_CID_AUDIO_VOLUME:
394 if(!maxvol){ /* max +20db */
395 t->volume = 0x6f - ((ctrl->value)/630);
396 } else { /* max 0db */
397 t->volume = 0x6f - ((ctrl->value)/829);
399 if (loudness) /* Turn on the loudness bit */
400 t->volume |= TDA7432_LD_ON;
402 tda7432_write(client,TDA7432_VL, t->volume);
404 case V4L2_CID_AUDIO_BALANCE:
405 if (ctrl->value < 32768) {
406 /* shifted to left, attenuate right */
407 t->rr = (32768 - ctrl->value)/1057;
409 t->lr = TDA7432_ATTEN_0DB;
410 t->lf = TDA7432_ATTEN_0DB;
411 } else if(ctrl->value > 32769) {
412 /* shifted to right, attenuate left */
413 t->lf = (ctrl->value - 32768)/1057;
415 t->rr = TDA7432_ATTEN_0DB;
416 t->rf = TDA7432_ATTEN_0DB;
419 t->rr = TDA7432_ATTEN_0DB;
420 t->rf = TDA7432_ATTEN_0DB;
421 t->lf = TDA7432_ATTEN_0DB;
422 t->lr = TDA7432_ATTEN_0DB;
425 case V4L2_CID_AUDIO_BASS:
426 t->bass = ctrl->value >> 12;
428 t->bass = (~t->bass & 0xf) + 0x8 ;
430 tda7432_write(client,TDA7432_TN, 0x10 | (t->bass << 4) | t->treble );
432 case V4L2_CID_AUDIO_TREBLE:
433 t->treble= ctrl->value >> 12;
435 t->treble = (~t->treble & 0xf) + 0x8 ;
437 tda7432_write(client,TDA7432_TN, 0x10 | (t->bass << 4) | t->treble );
443 /* Used for both mute and balance changes */
446 /* Mute & update balance*/
447 tda7432_write(client,TDA7432_LF, t->lf | TDA7432_MUTE);
448 tda7432_write(client,TDA7432_LR, t->lr | TDA7432_MUTE);
449 tda7432_write(client,TDA7432_RF, t->rf | TDA7432_MUTE);
450 tda7432_write(client,TDA7432_RR, t->rr | TDA7432_MUTE);
452 tda7432_write(client,TDA7432_LF, t->lf);
453 tda7432_write(client,TDA7432_LR, t->lr);
454 tda7432_write(client,TDA7432_RF, t->rf);
455 tda7432_write(client,TDA7432_RR, t->rr);
460 static int tda7432_command(struct i2c_client *client,
461 unsigned int cmd, void *arg)
463 v4l_dbg(2, debug,client,"In tda7432_command\n");
465 v4l_i2c_print_ioctl(client,cmd);
468 /* --- v4l ioctls --- */
469 /* take care: bttv does userspace copying, we'll get a
470 kernel pointer here... */
471 case VIDIOC_QUERYCTRL:
473 struct v4l2_queryctrl *qc = arg;
476 case V4L2_CID_AUDIO_MUTE:
477 case V4L2_CID_AUDIO_VOLUME:
478 case V4L2_CID_AUDIO_BALANCE:
479 case V4L2_CID_AUDIO_BASS:
480 case V4L2_CID_AUDIO_TREBLE:
484 return v4l2_ctrl_query_fill_std(qc);
487 return tda7432_set_ctrl(client, arg);
490 return tda7432_get_ctrl(client, arg);
492 } /* end of (cmd) switch */
497 static struct i2c_driver driver = {
501 .id = I2C_DRIVERID_TDA7432,
502 .attach_adapter = tda7432_probe,
503 .detach_client = tda7432_detach,
504 .command = tda7432_command,
507 static struct i2c_client client_template =
513 static int __init tda7432_init(void)
515 if ( (loudness < 0) || (loudness > 15) ) {
516 printk(KERN_ERR "loudness parameter must be between 0 and 15\n");
520 return i2c_add_driver(&driver);
523 static void __exit tda7432_fini(void)
525 i2c_del_driver(&driver);
528 module_init(tda7432_init);
529 module_exit(tda7432_fini);