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/videodev2.h>
48 #include <linux/i2c.h>
50 #include <media/v4l2-device.h>
51 #include <media/v4l2-ioctl.h>
52 #include <media/i2c-addr.h>
53 #include <media/v4l2-i2c-drv-legacy.h>
55 #ifndef VIDEO_AUDIO_BALANCE
56 # define VIDEO_AUDIO_BALANCE 32
59 MODULE_AUTHOR("Eric Sandeen <eric_sandeen@bigfoot.com>");
60 MODULE_DESCRIPTION("bttv driver for the tda7432 audio processor chip");
61 MODULE_LICENSE("GPL");
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_ADDR_TDA7432 >> 1,
80 /* Structure of address and subaddresses for the tda7432 */
83 struct v4l2_subdev sd;
93 static inline struct tda7432 *to_state(struct v4l2_subdev *sd)
95 return container_of(sd, struct tda7432, sd);
98 /* The TDA7432 is made by STS-Thompson
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
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
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)
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)
179 * 0 << 7 = attenuation (-)
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
204 * Bit 5 mutes that channel when set (1 = mute, 0 = unmute)
205 * We'll use the mute on the input, though (above)
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
221 * If bit 4 is set, then there is a flat attenuation according to
222 * the lower 4 bits, as above.
231 static int tda7432_write(struct v4l2_subdev *sd, int subaddr, int val)
233 struct i2c_client *client = v4l2_get_subdevdata(sd);
234 unsigned char buffer[2];
236 v4l2_dbg(2, debug, sd, "In tda7432_write\n");
237 v4l2_dbg(1, debug, sd, "Writing %d 0x%x\n", subaddr, val);
240 if (2 != i2c_master_send(client, buffer, 2)) {
241 v4l2_err(sd, "I/O error, trying (write %d 0x%x)\n",
248 static int tda7432_set(struct v4l2_subdev *sd)
250 struct i2c_client *client = v4l2_get_subdevdata(sd);
251 struct tda7432 *t = to_state(sd);
252 unsigned char buf[16];
254 v4l2_dbg(1, debug, sd,
255 "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
256 t->input, t->volume, t->bass, t->treble, t->lf, t->lr,
257 t->rf, t->rr, t->loud);
268 if (10 != i2c_master_send(client, buf, 10)) {
269 v4l2_err(sd, "I/O error, trying tda7432_set\n");
276 static void do_tda7432_init(struct v4l2_subdev *sd)
278 struct tda7432 *t = to_state(sd);
280 v4l2_dbg(2, debug, sd, "In tda7432_init\n");
282 t->input = TDA7432_STEREO_IN | /* Main (stereo) input */
283 TDA7432_BASS_SYM | /* Symmetric bass cut */
284 TDA7432_BASS_NORM; /* Normal bass range */
285 t->volume = 0x3b ; /* -27dB Volume */
286 if (loudness) /* Turn loudness on? */
287 t->volume |= TDA7432_LD_ON;
289 t->treble = TDA7432_TREBLE_0DB; /* 0dB Treble */
290 t->bass = TDA7432_BASS_0DB; /* 0dB Bass */
291 t->lf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
292 t->lr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
293 t->rf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
294 t->rr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
295 t->loud = loudness; /* insmod parameter */
300 static int tda7432_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
302 struct tda7432 *t = to_state(sd);
305 case V4L2_CID_AUDIO_MUTE:
306 ctrl->value=t->muted;
308 case V4L2_CID_AUDIO_VOLUME:
309 if (!maxvol){ /* max +20db */
310 ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 630;
311 } else { /* max 0db */
312 ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 829;
315 case V4L2_CID_AUDIO_BALANCE:
317 if ( (t->lf) < (t->rf) )
318 /* right is attenuated, balance shifted left */
319 ctrl->value = (32768 - 1057*(t->rf));
321 /* left is attenuated, balance shifted right */
322 ctrl->value = (32768 + 1057*(t->lf));
325 case V4L2_CID_AUDIO_BASS:
327 /* Bass/treble 4 bits each */
330 bass = ~(bass - 0x8) & 0xf;
331 ctrl->value = (bass << 12)+(bass << 8)+(bass << 4)+(bass);
334 case V4L2_CID_AUDIO_TREBLE:
336 int treble=t->treble;
338 treble = ~(treble - 0x8) & 0xf;
339 ctrl->value = (treble << 12)+(treble << 8)+(treble << 4)+(treble);
346 static int tda7432_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
348 struct tda7432 *t = to_state(sd);
351 case V4L2_CID_AUDIO_MUTE:
352 t->muted=ctrl->value;
354 case V4L2_CID_AUDIO_VOLUME:
355 if(!maxvol){ /* max +20db */
356 t->volume = 0x6f - ((ctrl->value)/630);
357 } else { /* max 0db */
358 t->volume = 0x6f - ((ctrl->value)/829);
360 if (loudness) /* Turn on the loudness bit */
361 t->volume |= TDA7432_LD_ON;
363 tda7432_write(sd, TDA7432_VL, t->volume);
365 case V4L2_CID_AUDIO_BALANCE:
366 if (ctrl->value < 32768) {
367 /* shifted to left, attenuate right */
368 t->rr = (32768 - ctrl->value)/1057;
370 t->lr = TDA7432_ATTEN_0DB;
371 t->lf = TDA7432_ATTEN_0DB;
372 } else if(ctrl->value > 32769) {
373 /* shifted to right, attenuate left */
374 t->lf = (ctrl->value - 32768)/1057;
376 t->rr = TDA7432_ATTEN_0DB;
377 t->rf = TDA7432_ATTEN_0DB;
380 t->rr = TDA7432_ATTEN_0DB;
381 t->rf = TDA7432_ATTEN_0DB;
382 t->lf = TDA7432_ATTEN_0DB;
383 t->lr = TDA7432_ATTEN_0DB;
386 case V4L2_CID_AUDIO_BASS:
387 t->bass = ctrl->value >> 12;
389 t->bass = (~t->bass & 0xf) + 0x8 ;
391 tda7432_write(sd, TDA7432_TN, 0x10 | (t->bass << 4) | t->treble);
393 case V4L2_CID_AUDIO_TREBLE:
394 t->treble= ctrl->value >> 12;
396 t->treble = (~t->treble & 0xf) + 0x8 ;
398 tda7432_write(sd, TDA7432_TN, 0x10 | (t->bass << 4) | t->treble);
404 /* Used for both mute and balance changes */
407 /* Mute & update balance*/
408 tda7432_write(sd, TDA7432_LF, t->lf | TDA7432_MUTE);
409 tda7432_write(sd, TDA7432_LR, t->lr | TDA7432_MUTE);
410 tda7432_write(sd, TDA7432_RF, t->rf | TDA7432_MUTE);
411 tda7432_write(sd, TDA7432_RR, t->rr | TDA7432_MUTE);
413 tda7432_write(sd, TDA7432_LF, t->lf);
414 tda7432_write(sd, TDA7432_LR, t->lr);
415 tda7432_write(sd, TDA7432_RF, t->rf);
416 tda7432_write(sd, TDA7432_RR, t->rr);
421 static int tda7432_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
424 case V4L2_CID_AUDIO_MUTE:
425 case V4L2_CID_AUDIO_VOLUME:
426 case V4L2_CID_AUDIO_BALANCE:
427 case V4L2_CID_AUDIO_BASS:
428 case V4L2_CID_AUDIO_TREBLE:
429 return v4l2_ctrl_query_fill_std(qc);
434 static int tda7432_command(struct i2c_client *client, unsigned cmd, void *arg)
436 return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
439 /* ----------------------------------------------------------------------- */
441 static const struct v4l2_subdev_core_ops tda7432_core_ops = {
442 .queryctrl = tda7432_queryctrl,
443 .g_ctrl = tda7432_g_ctrl,
444 .s_ctrl = tda7432_s_ctrl,
447 static const struct v4l2_subdev_ops tda7432_ops = {
448 .core = &tda7432_core_ops,
451 /* ----------------------------------------------------------------------- */
453 /* *********************** *
454 * i2c interface functions *
455 * *********************** */
457 static int tda7432_probe(struct i2c_client *client,
458 const struct i2c_device_id *id)
461 struct v4l2_subdev *sd;
463 v4l_info(client, "chip found @ 0x%02x (%s)\n",
464 client->addr << 1, client->adapter->name);
466 t = kzalloc(sizeof(*t), GFP_KERNEL);
470 v4l2_i2c_subdev_init(sd, client, &tda7432_ops);
471 if (loudness < 0 || loudness > 15) {
472 v4l2_warn(sd, "loudness parameter must be between 0 and 15\n");
483 static int tda7432_remove(struct i2c_client *client)
485 struct v4l2_subdev *sd = i2c_get_clientdata(client);
488 v4l2_device_unregister_subdev(sd);
493 static const struct i2c_device_id tda7432_id[] = {
497 MODULE_DEVICE_TABLE(i2c, tda7432_id);
499 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
501 .driverid = I2C_DRIVERID_TDA7432,
502 .command = tda7432_command,
503 .probe = tda7432_probe,
504 .remove = tda7432_remove,
505 .id_table = tda7432_id,