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"
45 /* This driver ID is brand new, so define it if it's not in i2c-id.h yet */
46 #ifndef I2C_DRIVERID_TDA7432
47 #define I2C_DRIVERID_TDA7432 27
51 MODULE_AUTHOR("Eric Sandeen <eric_sandeen@bigfoot.com>");
52 MODULE_DESCRIPTION("bttv driver for the tda7432 audio processor chip");
54 MODULE_PARM(debug
,"i");
55 MODULE_PARM(loudness
,"i");
56 static int loudness
= 0; /* disable loudness by default */
57 static int debug
= 0; /* insmod parameter */
60 /* Address to scan (I2C address of this chip) */
61 static unsigned short normal_i2c
[] = {
64 static unsigned short normal_i2c_range
[] = {I2C_CLIENT_END
};
65 static unsigned short probe
[2] = { I2C_CLIENT_END
, I2C_CLIENT_END
};
66 static unsigned short probe_range
[2] = { I2C_CLIENT_END
, I2C_CLIENT_END
};
67 static unsigned short ignore
[2] = { I2C_CLIENT_END
, I2C_CLIENT_END
};
68 static unsigned short ignore_range
[2] = { I2C_CLIENT_END
, I2C_CLIENT_END
};
69 static unsigned short force
[2] = { I2C_CLIENT_END
, I2C_CLIENT_END
};
70 static struct i2c_client_address_data addr_data
= {
71 normal_i2c
, normal_i2c_range
,
77 /* Structure of address and subaddresses for the tda7432 */
88 static struct i2c_driver driver
;
89 static struct i2c_client client_template
;
91 #define dprintk if (debug) printk
92 #define d2printk if (debug > 1) printk
94 /* The TDA7432 is made by STS-Thompson
96 * http://us.st.com/stonline/books/pdf/docs/4056.pdf
98 * TDA7432: I2C-bus controlled basic audio processor
100 * The TDA7432 controls basic audio functions like volume, balance,
101 * and tone control (including loudness). It also has four channel
102 * output (for front and rear). Since most vidcap cards probably
103 * don't have 4 channel output, this driver will set front & rear
104 * together (no independent control).
107 /* Subaddresses for TDA7432 */
109 #define TDA7432_IN 0x00 /* Input select */
110 #define TDA7432_VL 0x01 /* Volume */
111 #define TDA7432_TN 0x02 /* Bass, Treble (Tone) */
112 #define TDA7432_LF 0x03 /* Attenuation LF (Left Front) */
113 #define TDA7432_LR 0x04 /* Attenuation LR (Left Rear) */
114 #define TDA7432_RF 0x05 /* Attenuation RF (Right Front) */
115 #define TDA7432_RR 0x06 /* Attenuation RR (Right Rear) */
116 #define TDA7432_LD 0x07 /* Loudness */
119 /* Masks for bits in TDA7432 subaddresses */
121 /* Many of these not used - just for documentation */
123 /* Subaddress 0x00 - Input selection and bass control */
125 /* Bits 0,1,2 control input:
126 * 0x00 - Stereo input
129 * Mono probably isn't used - I'm guessing only the stereo
130 * input is connected on most cards, so we'll set it to stereo.
132 * Bit 3 controls bass cut: 0/1 is non-symmetric/symmetric bass cut
133 * Bit 4 controls bass range: 0/1 is extended/standard bass range
135 * Highest 3 bits not used
138 #define TDA7432_STEREO_IN 0
139 #define TDA7432_MONO_IN 2 /* Probably won't be used */
140 #define TDA7432_MUTE 3 /* Probably won't be used */
141 #define TDA7432_BASS_SYM 1 << 3
142 #define TDA7432_BASS_NORM 1 << 4
144 /* Subaddress 0x01 - Volume */
146 /* Lower 7 bits control volume from -79dB to +32dB in 1dB steps
147 * Recommended maximum is +20 dB
154 * MSB (bit 7) controls loudness: 1/0 is loudness on/off
157 #define TDA7432_VOL_0DB 0x20
158 #define TDA7432_LD_ON 1 << 7
161 /* Subaddress 0x02 - Tone control */
163 /* Bits 0,1,2 control absolute treble gain from 0dB to 14dB
164 * 0x0 is 14dB, 0x7 is 0dB
166 * Bit 3 controls treble attenuation/gain (sign)
168 * 0 = attenuation (-)
170 * Bits 4,5,6 control absolute bass gain from 0dB to 14dB
171 * (This is only true for normal base range, set in 0x00)
172 * 0x0 << 4 is 14dB, 0x7 is 0dB
174 * Bit 7 controls bass attenuation/gain (sign)
176 * 0 << 7 = attenuation (-)
179 * 1 1 0 1 0 1 0 1 is +4dB bass, -4dB treble
182 #define TDA7432_TREBLE_0DB 0xf
183 #define TDA7432_TREBLE 7
184 #define TDA7432_TREBLE_GAIN 1 << 3
185 #define TDA7432_BASS_0DB 0xf << 4
186 #define TDA7432_BASS 7 << 4
187 #define TDA7432_BASS_GAIN 1 << 7
190 /* Subaddress 0x03 - Left Front attenuation */
191 /* Subaddress 0x04 - Left Rear attenuation */
192 /* Subaddress 0x05 - Right Front attenuation */
193 /* Subaddress 0x06 - Right Rear attenuation */
195 /* Bits 0,1,2,3,4 control attenuation from 0dB to -37.5dB
201 * Bit 5 mutes that channel when set (1 = mute, 0 = unmute)
202 * We'll use the mute on the input, though (above)
206 #define TDA7432_ATTEN_0DB 0x00
209 /* Subaddress 0x07 - Loudness Control */
211 /* Bits 0,1,2,3 control loudness from 0dB to -15dB in 1dB steps
212 * when bit 4 is NOT set
217 * If bit 4 is set, then there is a flat attenuation according to
218 * the lower 4 bits, as above.
227 static int tda7432_write(struct i2c_client
*client
, int subaddr
, int val
)
229 unsigned char buffer
[2];
230 d2printk("tda7432: In tda7432_write\n");
231 dprintk("tda7432: Writing %d 0x%x\n", subaddr
, val
);
234 if (2 != i2c_master_send(client
,buffer
,2)) {
235 printk(KERN_WARNING
"tda7432: I/O error, trying (write %d 0x%x)\n",
242 /* I don't think we ever actually _read_ the chip... */
244 static int tda7432_read(struct i2c_client
*client
)
246 unsigned char buffer
;
247 d2printk("tda7432: In tda7432_read\n");
248 if (1 != i2c_master_recv(client
,&buffer
,1)) {
249 printk(KERN_WARNING
"tda7432: I/O error, trying (read)\n");
252 dprintk("tda7432: Read 0x%02x\n", buffer
);
257 static int tda7432_set(struct i2c_client
*client
)
259 struct tda7432
*t
= client
->data
;
260 unsigned char buf
[16];
261 d2printk("tda7432: In tda7432_set\n");
264 "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
265 t
->input
,t
->volume
,t
->tone
,t
->lf
,t
->lr
,t
->rf
,t
->rr
,t
->loud
);
275 if (9 != i2c_master_send(client
,buf
,9)) {
276 printk(KERN_WARNING
"tda7432: I/O error, trying tda7432_set\n");
283 static void do_tda7432_init(struct i2c_client
*client
)
285 struct tda7432
*t
= client
->data
;
286 d2printk("tda7432: In tda7432_init\n");
288 t
->input
= TDA7432_STEREO_IN
| /* Main (stereo) input */
289 TDA7432_BASS_SYM
| /* Symmetric bass cut */
290 TDA7432_BASS_NORM
; /* Normal bass range */
291 t
->volume
= TDA7432_VOL_0DB
; /* 0dB Volume */
292 if (loudness
) /* Turn loudness on? */
293 t
->volume
|= TDA7432_LD_ON
;
294 t
->tone
= TDA7432_TREBLE_0DB
| /* 0dB Treble */
295 TDA7432_BASS_0DB
; /* 0dB Bass */
296 t
->lf
= TDA7432_ATTEN_0DB
; /* 0dB attenuation */
297 t
->lr
= TDA7432_ATTEN_0DB
; /* 0dB attenuation */
298 t
->rf
= TDA7432_ATTEN_0DB
; /* 0dB attenuation */
299 t
->rr
= TDA7432_ATTEN_0DB
; /* 0dB attenuation */
300 t
->loud
= loudness
; /* insmod parameter */
305 /* *********************** *
306 * i2c interface functions *
307 * *********************** */
309 static int tda7432_attach(struct i2c_adapter
*adap
, int addr
,
310 unsigned short flags
, int kind
)
313 struct i2c_client
*client
;
314 d2printk("tda7432: In tda7432_attach\n");
315 client
= kmalloc(sizeof *client
,GFP_KERNEL
);
318 memcpy(client
,&client_template
,sizeof(struct i2c_client
));
319 client
->adapter
= adap
;
322 client
->data
= t
= kmalloc(sizeof *t
,GFP_KERNEL
);
325 memset(t
,0,sizeof *t
);
326 do_tda7432_init(client
);
328 strcpy(client
->name
,"TDA7432");
329 printk(KERN_INFO
"tda7432: init\n");
331 i2c_attach_client(client
);
335 static int tda7432_probe(struct i2c_adapter
*adap
)
337 if (adap
->id
== (I2C_ALGO_BIT
| I2C_HW_B_BT848
))
338 return i2c_probe(adap
, &addr_data
, tda7432_attach
);
342 static int tda7432_detach(struct i2c_client
*client
)
344 struct tda7432
*t
= client
->data
;
346 do_tda7432_init(client
);
347 i2c_detach_client(client
);
355 static int tda7432_command(struct i2c_client
*client
,
356 unsigned int cmd
, void *arg
)
358 struct tda7432
*t
= client
->data
;
359 d2printk("tda7432: In tda7432_command\n");
365 /* --- v4l ioctls --- */
366 /* take care: bttv does userspace copying, we'll get a
367 kernel pointer here... */
369 /* Query card - scale from TDA7432 settings to V4L settings */
372 struct video_audio
*va
= arg
;
373 dprintk("tda7432: VIDIOCGAUDIO\n");
375 va
->flags
|= VIDEO_AUDIO_VOLUME
|
379 /* Master volume control
380 * V4L volume is min 0, max 65535
382 * Min (-79dB) is 0x6f
383 * Max (+20dB) is 0x07
384 * (Mask out bit 7 of vol - it's for the loudness setting)
387 va
->volume
= ( 0x6f - (t
->volume
& 0x7F) ) * 630;
389 /* Balance depends on L,R attenuation
390 * V4L balance is 0 to 65535, middle is 32768
391 * TDA7432 attenuation: min (0dB) is 0, max (-37.5dB) is 0x1f
392 * to scale up to V4L numbers, mult by 1057
393 * attenuation exists for lf, lr, rf, rr
394 * we use only lf and rf (front channels)
397 if ( (t
->lf
) < (t
->rf
) )
398 /* right is attenuated, balance shifted left */
399 va
->balance
= (32768 - 1057*(t
->rf
));
401 /* left is attenuated, balance shifted right */
402 va
->balance
= (32768 + 1057*(t
->lf
));
405 va
->bass
= 32768; /* brain hurts... set to middle for now */
406 va
->treble
= 32768; /* brain hurts... set to middle for now */
408 break; /* VIDIOCGAUDIO case */
411 /* Set card - scale from V4L settings to TDA7432 settings */
414 struct video_audio
*va
= arg
;
415 dprintk("tda7432: VIDEOCSAUDIO\n");
417 t
->volume
= 0x6f - ( (va
->volume
)/630 );
419 if (loudness
) /* Turn on the loudness bit */
420 t
->volume
|= TDA7432_LD_ON
;
422 if (va
->balance
< 32768) {
423 /* shifted to left, attenuate right */
424 t
->rr
= (32768 - va
->balance
)/1057;
428 /* shifted to right, attenuate left */
429 t
->lf
= (va
->balance
- 32768)/1057;
433 /* t->tone = 0xff; */ /* Brain hurts - no tone control for now... */
435 tda7432_write(client
,TDA7432_VL
, t
->volume
);
436 /* tda7432_write(client,TDA7432_TN, t->tone); */
437 tda7432_write(client
,TDA7432_LF
, t
->lf
);
438 tda7432_write(client
,TDA7432_LR
, t
->lr
);
439 tda7432_write(client
,TDA7432_RF
, t
->rf
);
440 tda7432_write(client
,TDA7432_RR
, t
->rr
);
444 } /* end of VIDEOCSAUDIO case */
446 default: /* Not VIDEOCGAUDIO or VIDEOCSAUDIO */
449 d2printk("tda7432: Default\n");
451 } /* end of (cmd) switch */
457 static struct i2c_driver driver
= {
458 "i2c tda7432 driver",
459 I2C_DRIVERID_TDA7432
,
466 static struct i2c_client client_template
=
468 "(unset)", /* name */
477 int init_module(void)
479 int tda7432_init(void)
483 if ( (loudness
< 0) || (loudness
> 15) )
485 printk(KERN_ERR
"tda7432: loudness parameter must be between 0 and 15\n");
490 i2c_add_driver(&driver
);
495 void cleanup_module(void)
497 i2c_del_driver(&driver
);