- pre5:
[davej-history.git] / drivers / media / video / tda7432.c
blob889856738bc3d03f3dd7f7ca08b192e64d436cb0
1 /*
2 * For the STS-Thompson TDA7432 audio processor chip
3 *
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
13 * OPTIONS:
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
19 * TODO:
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>
42 #include "bttv.h"
43 #include "audiochip.h"
44 #include "id.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[] = {
57 I2C_TDA7432 >> 1,
58 I2C_CLIENT_END};
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,
67 probe, probe_range,
68 ignore, ignore_range,
69 force
72 /* Structure of address and subaddresses for the tda7432 */
74 struct tda7432 {
75 int addr;
76 int input;
77 int volume;
78 int tone;
79 int lf, lr, rf, rr;
80 int loud;
81 struct i2c_client c;
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
91 * http://www.st.com
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
123 * 0x02 - Mono input
124 * 0x03 - Mute
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
145 * +32dB: 0x00
146 * +20dB: 0x0c
147 * 0dB: 0x20
148 * -79dB: 0x6f
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)
163 * 1 = gain (+)
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)
171 * 1 << 7 = gain (+)
172 * 0 << 7 = attenuation (-)
174 * Example:
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
192 * in 1.5dB steps.
194 * 0x00 is 0dB
195 * 0x1f is -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)
199 * Bits 6,7 unused
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
210 * 0x0 is 0dB
211 * 0xf is -15dB
213 * If bit 4 is set, then there is a flat attenuation according to
214 * the lower 4 bits, as above.
216 * Bits 5,6,7 unused
221 /* Begin code */
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);
228 buffer[0] = subaddr;
229 buffer[1] = val;
230 if (2 != i2c_master_send(client,buffer,2)) {
231 printk(KERN_WARNING "tda7432: I/O error, trying (write %d 0x%x)\n",
232 subaddr, val);
233 return -1;
235 return 0;
238 /* I don't think we ever actually _read_ the chip... */
239 #if 0
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");
246 return -1;
248 dprintk("tda7432: Read 0x%02x\n", buffer);
249 return buffer;
251 #endif
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");
259 dprintk(KERN_INFO
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);
262 buf[0] = TDA7432_IN;
263 buf[1] = t->input;
264 buf[2] = t->volume;
265 buf[3] = t->tone;
266 buf[4] = t->lf;
267 buf[5] = t->lr;
268 buf[6] = t->rf;
269 buf[7] = t->rr;
270 buf[8] = t->loud;
271 if (9 != i2c_master_send(client,buf,9)) {
272 printk(KERN_WARNING "tda7432: I/O error, trying tda7432_set\n");
273 return -1;
276 return 0;
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 */
298 tda7432_set(client);
301 /* *********************** *
302 * i2c interface functions *
303 * *********************** */
305 static int tda7432_attach(struct i2c_adapter *adap, int addr,
306 unsigned short flags, int kind)
308 struct tda7432 *t;
309 struct i2c_client *client;
310 d2printk("tda7432: In tda7432_attach\n");
312 t = kmalloc(sizeof *t,GFP_KERNEL);
313 if (!t)
314 return -ENOMEM;
315 memset(t,0,sizeof *t);
317 client = &t->c;
318 memcpy(client,&client_template,sizeof(struct i2c_client));
319 client->adapter = adap;
320 client->addr = addr;
321 client->data = t;
323 do_tda7432_init(client);
324 MOD_INC_USE_COUNT;
325 strcpy(client->name,"TDA7432");
326 printk(KERN_INFO "tda7432: init\n");
328 i2c_attach_client(client);
329 return 0;
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);
336 return 0;
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);
346 kfree(t);
347 MOD_DEC_USE_COUNT;
348 return 0;
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");
356 #if 0
357 __u16 *sarg = arg;
358 #endif
360 switch (cmd) {
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 */
366 case VIDIOCGAUDIO:
368 struct video_audio *va = arg;
369 dprintk("tda7432: VIDIOCGAUDIO\n");
371 va->flags |= VIDEO_AUDIO_VOLUME |
372 VIDEO_AUDIO_BASS |
373 VIDEO_AUDIO_TREBLE;
375 /* Master volume control
376 * V4L volume is min 0, max 65535
377 * TDA7432 Volume:
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));
396 else
397 /* left is attenuated, balance shifted right */
398 va->balance = (32768 + 1057*(t->lf));
400 /* Bass/treble */
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 */
408 case VIDIOCSAUDIO:
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;
421 t->rf = t->rr;
423 else {
424 /* shifted to right, attenuate left */
425 t->lf = (va->balance - 32768)/1057;
426 t->lr = t->lf;
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);
438 break;
440 } /* end of VIDEOCSAUDIO case */
442 default: /* Not VIDEOCGAUDIO or VIDEOCSAUDIO */
444 /* nothing */
445 d2printk("tda7432: Default\n");
447 } /* end of (cmd) switch */
449 return 0;
453 static struct i2c_driver driver = {
454 "i2c tda7432 driver",
455 I2C_DRIVERID_TDA7432,
456 I2C_DF_NOTIFY,
457 tda7432_probe,
458 tda7432_detach,
459 tda7432_command,
462 static struct i2c_client client_template =
464 "(unset)", /* name */
468 NULL,
469 &driver
472 #ifdef MODULE
473 int init_module(void)
474 #else
475 int tda7432_init(void)
476 #endif
479 if ( (loudness < 0) || (loudness > 15) )
481 printk(KERN_ERR "tda7432: loudness parameter must be between 0 and 15\n");
482 return -EINVAL;
486 i2c_add_driver(&driver);
487 return 0;
490 #ifdef MODULE
491 void cleanup_module(void)
493 i2c_del_driver(&driver);
495 #endif
498 * Local variables:
499 * c-basic-offset: 8
500 * End: