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 * 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
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
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>
43 #include "audiochip.h"
46 MODULE_AUTHOR("Eric Sandeen <eric_sandeen@bigfoot.com>");
47 MODULE_DESCRIPTION("bttv driver for the tda7432 audio processor chip");
49 MODULE_PARM(debug
,"i");
50 MODULE_PARM(loudness
,"i");
51 static int loudness
= 0; /* disable loudness by default */
52 static int debug
= 0; /* insmod parameter */
55 /* Address to scan (I2C address of this chip) */
56 static unsigned short normal_i2c
[] = {
59 static unsigned short normal_i2c_range
[] = {I2C_CLIENT_END
};
60 static unsigned short probe
[2] = { I2C_CLIENT_END
, I2C_CLIENT_END
};
61 static unsigned short probe_range
[2] = { I2C_CLIENT_END
, I2C_CLIENT_END
};
62 static unsigned short ignore
[2] = { I2C_CLIENT_END
, I2C_CLIENT_END
};
63 static unsigned short ignore_range
[2] = { I2C_CLIENT_END
, I2C_CLIENT_END
};
64 static unsigned short force
[2] = { I2C_CLIENT_END
, I2C_CLIENT_END
};
65 static struct i2c_client_address_data addr_data
= {
66 normal_i2c
, normal_i2c_range
,
72 /* Structure of address and subaddresses for the tda7432 */
84 static struct i2c_driver driver
;
85 static struct i2c_client client_template
;
87 #define dprintk if (debug) printk
88 #define d2printk if (debug > 1) printk
90 /* The TDA7432 is made by STS-Thompson
92 * http://us.st.com/stonline/books/pdf/docs/4056.pdf
94 * TDA7432: I2C-bus controlled basic audio processor
96 * The TDA7432 controls basic audio functions like volume, balance,
97 * and tone control (including loudness). It also has four channel
98 * output (for front and rear). Since most vidcap cards probably
99 * don't have 4 channel output, this driver will set front & rear
100 * together (no independent control).
103 /* Subaddresses for TDA7432 */
105 #define TDA7432_IN 0x00 /* Input select */
106 #define TDA7432_VL 0x01 /* Volume */
107 #define TDA7432_TN 0x02 /* Bass, Treble (Tone) */
108 #define TDA7432_LF 0x03 /* Attenuation LF (Left Front) */
109 #define TDA7432_LR 0x04 /* Attenuation LR (Left Rear) */
110 #define TDA7432_RF 0x05 /* Attenuation RF (Right Front) */
111 #define TDA7432_RR 0x06 /* Attenuation RR (Right Rear) */
112 #define TDA7432_LD 0x07 /* Loudness */
115 /* Masks for bits in TDA7432 subaddresses */
117 /* Many of these not used - just for documentation */
119 /* Subaddress 0x00 - Input selection and bass control */
121 /* Bits 0,1,2 control input:
122 * 0x00 - Stereo input
125 * Mono probably isn't used - I'm guessing only the stereo
126 * input is connected on most cards, so we'll set it to stereo.
128 * Bit 3 controls bass cut: 0/1 is non-symmetric/symmetric bass cut
129 * Bit 4 controls bass range: 0/1 is extended/standard bass range
131 * Highest 3 bits not used
134 #define TDA7432_STEREO_IN 0
135 #define TDA7432_MONO_IN 2 /* Probably won't be used */
136 #define TDA7432_MUTE 3 /* 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 << 4
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
205 /* Subaddress 0x07 - Loudness Control */
207 /* Bits 0,1,2,3 control loudness from 0dB to -15dB in 1dB steps
208 * when bit 4 is NOT set
213 * If bit 4 is set, then there is a flat attenuation according to
214 * the lower 4 bits, as above.
223 static int tda7432_write(struct i2c_client
*client
, int subaddr
, int val
)
225 unsigned char buffer
[2];
226 d2printk("tda7432: In tda7432_write\n");
227 dprintk("tda7432: Writing %d 0x%x\n", subaddr
, val
);
230 if (2 != i2c_master_send(client
,buffer
,2)) {
231 printk(KERN_WARNING
"tda7432: I/O error, trying (write %d 0x%x)\n",
238 /* I don't think we ever actually _read_ the chip... */
240 static int tda7432_read(struct i2c_client
*client
)
242 unsigned char buffer
;
243 d2printk("tda7432: In tda7432_read\n");
244 if (1 != i2c_master_recv(client
,&buffer
,1)) {
245 printk(KERN_WARNING
"tda7432: I/O error, trying (read)\n");
248 dprintk("tda7432: Read 0x%02x\n", buffer
);
253 static int tda7432_set(struct i2c_client
*client
)
255 struct tda7432
*t
= client
->data
;
256 unsigned char buf
[16];
257 d2printk("tda7432: In tda7432_set\n");
260 "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
261 t
->input
,t
->volume
,t
->tone
,t
->lf
,t
->lr
,t
->rf
,t
->rr
,t
->loud
);
271 if (9 != i2c_master_send(client
,buf
,9)) {
272 printk(KERN_WARNING
"tda7432: I/O error, trying tda7432_set\n");
279 static void do_tda7432_init(struct i2c_client
*client
)
281 struct tda7432
*t
= client
->data
;
282 d2printk("tda7432: In tda7432_init\n");
284 t
->input
= TDA7432_STEREO_IN
| /* Main (stereo) input */
285 TDA7432_BASS_SYM
| /* Symmetric bass cut */
286 TDA7432_BASS_NORM
; /* Normal bass range */
287 t
->volume
= TDA7432_VOL_0DB
; /* 0dB Volume */
288 if (loudness
) /* Turn loudness on? */
289 t
->volume
|= TDA7432_LD_ON
;
290 t
->tone
= TDA7432_TREBLE_0DB
| /* 0dB Treble */
291 TDA7432_BASS_0DB
; /* 0dB Bass */
292 t
->lf
= TDA7432_ATTEN_0DB
; /* 0dB attenuation */
293 t
->lr
= TDA7432_ATTEN_0DB
; /* 0dB attenuation */
294 t
->rf
= TDA7432_ATTEN_0DB
; /* 0dB attenuation */
295 t
->rr
= TDA7432_ATTEN_0DB
; /* 0dB attenuation */
296 t
->loud
= loudness
; /* insmod parameter */
301 /* *********************** *
302 * i2c interface functions *
303 * *********************** */
305 static int tda7432_attach(struct i2c_adapter
*adap
, int addr
,
306 unsigned short flags
, int kind
)
309 struct i2c_client
*client
;
310 d2printk("tda7432: In tda7432_attach\n");
312 t
= kmalloc(sizeof *t
,GFP_KERNEL
);
315 memset(t
,0,sizeof *t
);
318 memcpy(client
,&client_template
,sizeof(struct i2c_client
));
319 client
->adapter
= adap
;
323 do_tda7432_init(client
);
325 strcpy(client
->name
,"TDA7432");
326 printk(KERN_INFO
"tda7432: init\n");
328 i2c_attach_client(client
);
332 static int tda7432_probe(struct i2c_adapter
*adap
)
334 if (adap
->id
== (I2C_ALGO_BIT
| I2C_HW_B_BT848
))
335 return i2c_probe(adap
, &addr_data
, tda7432_attach
);
339 static int tda7432_detach(struct i2c_client
*client
)
341 struct tda7432
*t
= client
->data
;
343 do_tda7432_init(client
);
344 i2c_detach_client(client
);
351 static int tda7432_command(struct i2c_client
*client
,
352 unsigned int cmd
, void *arg
)
354 struct tda7432
*t
= client
->data
;
355 d2printk("tda7432: In tda7432_command\n");
361 /* --- v4l ioctls --- */
362 /* take care: bttv does userspace copying, we'll get a
363 kernel pointer here... */
365 /* Query card - scale from TDA7432 settings to V4L settings */
368 struct video_audio
*va
= arg
;
369 dprintk("tda7432: VIDIOCGAUDIO\n");
371 va
->flags
|= VIDEO_AUDIO_VOLUME
|
375 /* Master volume control
376 * V4L volume is min 0, max 65535
378 * Min (-79dB) is 0x6f
379 * Max (+20dB) is 0x07
380 * (Mask out bit 7 of vol - it's for the loudness setting)
383 va
->volume
= ( 0x6f - (t
->volume
& 0x7F) ) * 630;
385 /* Balance depends on L,R attenuation
386 * V4L balance is 0 to 65535, middle is 32768
387 * TDA7432 attenuation: min (0dB) is 0, max (-37.5dB) is 0x1f
388 * to scale up to V4L numbers, mult by 1057
389 * attenuation exists for lf, lr, rf, rr
390 * we use only lf and rf (front channels)
393 if ( (t
->lf
) < (t
->rf
) )
394 /* right is attenuated, balance shifted left */
395 va
->balance
= (32768 - 1057*(t
->rf
));
397 /* left is attenuated, balance shifted right */
398 va
->balance
= (32768 + 1057*(t
->lf
));
401 va
->bass
= 32768; /* brain hurts... set to middle for now */
402 va
->treble
= 32768; /* brain hurts... set to middle for now */
404 break; /* VIDIOCGAUDIO case */
407 /* Set card - scale from V4L settings to TDA7432 settings */
410 struct video_audio
*va
= arg
;
411 dprintk("tda7432: VIDEOCSAUDIO\n");
413 t
->volume
= 0x6f - ( (va
->volume
)/630 );
415 if (loudness
) /* Turn on the loudness bit */
416 t
->volume
|= TDA7432_LD_ON
;
418 if (va
->balance
< 32768) {
419 /* shifted to left, attenuate right */
420 t
->rr
= (32768 - va
->balance
)/1057;
424 /* shifted to right, attenuate left */
425 t
->lf
= (va
->balance
- 32768)/1057;
429 /* t->tone = 0xff; */ /* Brain hurts - no tone control for now... */
431 tda7432_write(client
,TDA7432_VL
, t
->volume
);
432 /* tda7432_write(client,TDA7432_TN, t->tone); */
433 tda7432_write(client
,TDA7432_LF
, t
->lf
);
434 tda7432_write(client
,TDA7432_LR
, t
->lr
);
435 tda7432_write(client
,TDA7432_RF
, t
->rf
);
436 tda7432_write(client
,TDA7432_RR
, t
->rr
);
440 } /* end of VIDEOCSAUDIO case */
442 default: /* Not VIDEOCGAUDIO or VIDEOCSAUDIO */
445 d2printk("tda7432: Default\n");
447 } /* end of (cmd) switch */
453 static struct i2c_driver driver
= {
454 "i2c tda7432 driver",
455 I2C_DRIVERID_TDA7432
,
462 static struct i2c_client client_template
=
464 "(unset)", /* name */
473 int init_module(void)
475 int tda7432_init(void)
479 if ( (loudness
< 0) || (loudness
> 15) )
481 printk(KERN_ERR
"tda7432: loudness parameter must be between 0 and 15\n");
486 i2c_add_driver(&driver
);
491 void cleanup_module(void)
493 i2c_del_driver(&driver
);