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.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);
73 /* Structure of address and subaddresses for the tda7432 */
76 struct v4l2_subdev sd;
86 static inline struct tda7432 *to_state(struct v4l2_subdev *sd)
88 return container_of(sd, struct tda7432, sd);
91 /* The TDA7432 is made by STS-Thompson
93 * http://us.st.com/stonline/books/pdf/docs/4056.pdf
95 * TDA7432: I2C-bus controlled basic audio processor
97 * The TDA7432 controls basic audio functions like volume, balance,
98 * and tone control (including loudness). It also has four channel
99 * output (for front and rear). Since most vidcap cards probably
100 * don't have 4 channel output, this driver will set front & rear
101 * together (no independent control).
104 /* Subaddresses for TDA7432 */
106 #define TDA7432_IN 0x00 /* Input select */
107 #define TDA7432_VL 0x01 /* Volume */
108 #define TDA7432_TN 0x02 /* Bass, Treble (Tone) */
109 #define TDA7432_LF 0x03 /* Attenuation LF (Left Front) */
110 #define TDA7432_LR 0x04 /* Attenuation LR (Left Rear) */
111 #define TDA7432_RF 0x05 /* Attenuation RF (Right Front) */
112 #define TDA7432_RR 0x06 /* Attenuation RR (Right Rear) */
113 #define TDA7432_LD 0x07 /* Loudness */
116 /* Masks for bits in TDA7432 subaddresses */
118 /* Many of these not used - just for documentation */
120 /* Subaddress 0x00 - Input selection and bass control */
122 /* Bits 0,1,2 control input:
123 * 0x00 - Stereo input
125 * 0x03 - Mute (Using Attenuators Plays better with modules)
126 * Mono probably isn't used - I'm guessing only the stereo
127 * input is connected on most cards, so we'll set it to stereo.
129 * Bit 3 controls bass cut: 0/1 is non-symmetric/symmetric bass cut
130 * Bit 4 controls bass range: 0/1 is extended/standard bass range
132 * Highest 3 bits not used
135 #define TDA7432_STEREO_IN 0
136 #define TDA7432_MONO_IN 2 /* Probably won't be used */
137 #define TDA7432_BASS_SYM 1 << 3
138 #define TDA7432_BASS_NORM 1 << 4
140 /* Subaddress 0x01 - Volume */
142 /* Lower 7 bits control volume from -79dB to +32dB in 1dB steps
143 * Recommended maximum is +20 dB
150 * MSB (bit 7) controls loudness: 1/0 is loudness on/off
153 #define TDA7432_VOL_0DB 0x20
154 #define TDA7432_LD_ON 1 << 7
157 /* Subaddress 0x02 - Tone control */
159 /* Bits 0,1,2 control absolute treble gain from 0dB to 14dB
160 * 0x0 is 14dB, 0x7 is 0dB
162 * Bit 3 controls treble attenuation/gain (sign)
164 * 0 = attenuation (-)
166 * Bits 4,5,6 control absolute bass gain from 0dB to 14dB
167 * (This is only true for normal base range, set in 0x00)
168 * 0x0 << 4 is 14dB, 0x7 is 0dB
170 * Bit 7 controls bass attenuation/gain (sign)
172 * 0 << 7 = attenuation (-)
175 * 1 1 0 1 0 1 0 1 is +4dB bass, -4dB treble
178 #define TDA7432_TREBLE_0DB 0xf
179 #define TDA7432_TREBLE 7
180 #define TDA7432_TREBLE_GAIN 1 << 3
181 #define TDA7432_BASS_0DB 0xf
182 #define TDA7432_BASS 7 << 4
183 #define TDA7432_BASS_GAIN 1 << 7
186 /* Subaddress 0x03 - Left Front attenuation */
187 /* Subaddress 0x04 - Left Rear attenuation */
188 /* Subaddress 0x05 - Right Front attenuation */
189 /* Subaddress 0x06 - Right Rear attenuation */
191 /* Bits 0,1,2,3,4 control attenuation from 0dB to -37.5dB
197 * Bit 5 mutes that channel when set (1 = mute, 0 = unmute)
198 * We'll use the mute on the input, though (above)
202 #define TDA7432_ATTEN_0DB 0x00
203 #define TDA7432_MUTE 0x1 << 5
206 /* Subaddress 0x07 - Loudness Control */
208 /* Bits 0,1,2,3 control loudness from 0dB to -15dB in 1dB steps
209 * when bit 4 is NOT set
214 * If bit 4 is set, then there is a flat attenuation according to
215 * the lower 4 bits, as above.
224 static int tda7432_write(struct v4l2_subdev *sd, int subaddr, int val)
226 struct i2c_client *client = v4l2_get_subdevdata(sd);
227 unsigned char buffer[2];
229 v4l2_dbg(2, debug, sd, "In tda7432_write\n");
230 v4l2_dbg(1, debug, sd, "Writing %d 0x%x\n", subaddr, val);
233 if (2 != i2c_master_send(client, buffer, 2)) {
234 v4l2_err(sd, "I/O error, trying (write %d 0x%x)\n",
241 static int tda7432_set(struct v4l2_subdev *sd)
243 struct i2c_client *client = v4l2_get_subdevdata(sd);
244 struct tda7432 *t = to_state(sd);
245 unsigned char buf[16];
247 v4l2_dbg(1, debug, sd,
248 "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
249 t->input, t->volume, t->bass, t->treble, t->lf, t->lr,
250 t->rf, t->rr, t->loud);
261 if (10 != i2c_master_send(client, buf, 10)) {
262 v4l2_err(sd, "I/O error, trying tda7432_set\n");
269 static void do_tda7432_init(struct v4l2_subdev *sd)
271 struct tda7432 *t = to_state(sd);
273 v4l2_dbg(2, debug, sd, "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 static int tda7432_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
295 struct tda7432 *t = to_state(sd);
298 case V4L2_CID_AUDIO_MUTE:
299 ctrl->value=t->muted;
301 case V4L2_CID_AUDIO_VOLUME:
302 if (!maxvol){ /* max +20db */
303 ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 630;
304 } else { /* max 0db */
305 ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 829;
308 case V4L2_CID_AUDIO_BALANCE:
310 if ( (t->lf) < (t->rf) )
311 /* right is attenuated, balance shifted left */
312 ctrl->value = (32768 - 1057*(t->rf));
314 /* left is attenuated, balance shifted right */
315 ctrl->value = (32768 + 1057*(t->lf));
318 case V4L2_CID_AUDIO_BASS:
320 /* Bass/treble 4 bits each */
323 bass = ~(bass - 0x8) & 0xf;
324 ctrl->value = (bass << 12)+(bass << 8)+(bass << 4)+(bass);
327 case V4L2_CID_AUDIO_TREBLE:
329 int treble=t->treble;
331 treble = ~(treble - 0x8) & 0xf;
332 ctrl->value = (treble << 12)+(treble << 8)+(treble << 4)+(treble);
339 static int tda7432_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
341 struct tda7432 *t = to_state(sd);
344 case V4L2_CID_AUDIO_MUTE:
345 t->muted=ctrl->value;
347 case V4L2_CID_AUDIO_VOLUME:
348 if(!maxvol){ /* max +20db */
349 t->volume = 0x6f - ((ctrl->value)/630);
350 } else { /* max 0db */
351 t->volume = 0x6f - ((ctrl->value)/829);
353 if (loudness) /* Turn on the loudness bit */
354 t->volume |= TDA7432_LD_ON;
356 tda7432_write(sd, TDA7432_VL, t->volume);
358 case V4L2_CID_AUDIO_BALANCE:
359 if (ctrl->value < 32768) {
360 /* shifted to left, attenuate right */
361 t->rr = (32768 - ctrl->value)/1057;
363 t->lr = TDA7432_ATTEN_0DB;
364 t->lf = TDA7432_ATTEN_0DB;
365 } else if(ctrl->value > 32769) {
366 /* shifted to right, attenuate left */
367 t->lf = (ctrl->value - 32768)/1057;
369 t->rr = TDA7432_ATTEN_0DB;
370 t->rf = TDA7432_ATTEN_0DB;
373 t->rr = TDA7432_ATTEN_0DB;
374 t->rf = TDA7432_ATTEN_0DB;
375 t->lf = TDA7432_ATTEN_0DB;
376 t->lr = TDA7432_ATTEN_0DB;
379 case V4L2_CID_AUDIO_BASS:
380 t->bass = ctrl->value >> 12;
382 t->bass = (~t->bass & 0xf) + 0x8 ;
384 tda7432_write(sd, TDA7432_TN, 0x10 | (t->bass << 4) | t->treble);
386 case V4L2_CID_AUDIO_TREBLE:
387 t->treble= ctrl->value >> 12;
389 t->treble = (~t->treble & 0xf) + 0x8 ;
391 tda7432_write(sd, TDA7432_TN, 0x10 | (t->bass << 4) | t->treble);
397 /* Used for both mute and balance changes */
400 /* Mute & update balance*/
401 tda7432_write(sd, TDA7432_LF, t->lf | TDA7432_MUTE);
402 tda7432_write(sd, TDA7432_LR, t->lr | TDA7432_MUTE);
403 tda7432_write(sd, TDA7432_RF, t->rf | TDA7432_MUTE);
404 tda7432_write(sd, TDA7432_RR, t->rr | TDA7432_MUTE);
406 tda7432_write(sd, TDA7432_LF, t->lf);
407 tda7432_write(sd, TDA7432_LR, t->lr);
408 tda7432_write(sd, TDA7432_RF, t->rf);
409 tda7432_write(sd, TDA7432_RR, t->rr);
414 static int tda7432_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
417 case V4L2_CID_AUDIO_VOLUME:
418 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 58880);
419 case V4L2_CID_AUDIO_MUTE:
420 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
421 case V4L2_CID_AUDIO_BALANCE:
422 case V4L2_CID_AUDIO_BASS:
423 case V4L2_CID_AUDIO_TREBLE:
424 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
429 /* ----------------------------------------------------------------------- */
431 static const struct v4l2_subdev_core_ops tda7432_core_ops = {
432 .queryctrl = tda7432_queryctrl,
433 .g_ctrl = tda7432_g_ctrl,
434 .s_ctrl = tda7432_s_ctrl,
437 static const struct v4l2_subdev_ops tda7432_ops = {
438 .core = &tda7432_core_ops,
441 /* ----------------------------------------------------------------------- */
443 /* *********************** *
444 * i2c interface functions *
445 * *********************** */
447 static int tda7432_probe(struct i2c_client *client,
448 const struct i2c_device_id *id)
451 struct v4l2_subdev *sd;
453 v4l_info(client, "chip found @ 0x%02x (%s)\n",
454 client->addr << 1, client->adapter->name);
456 t = kzalloc(sizeof(*t), GFP_KERNEL);
460 v4l2_i2c_subdev_init(sd, client, &tda7432_ops);
461 if (loudness < 0 || loudness > 15) {
462 v4l2_warn(sd, "loudness parameter must be between 0 and 15\n");
473 static int tda7432_remove(struct i2c_client *client)
475 struct v4l2_subdev *sd = i2c_get_clientdata(client);
478 v4l2_device_unregister_subdev(sd);
483 static const struct i2c_device_id tda7432_id[] = {
487 MODULE_DEVICE_TABLE(i2c, tda7432_id);
489 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
491 .probe = tda7432_probe,
492 .remove = tda7432_remove,
493 .id_table = tda7432_id,