blob: d32737dd21427d8bfed38d1206fc11b5dd272622 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
7 *
8 * Muting and tone control by Jonathan Isom <jisom@ematic.com>
9 *
10 * Copyright (c) 2000 Eric Sandeen <eric_sandeen@bigfoot.com>
11 * This code is placed under the terms of the GNU General Public License
12 * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
13 * Which was based on tda8425.c by Greg Alexander (c) 1998
14 *
15 * OPTIONS:
16 * debug - set to 1 if you'd like to see debug messages
17 * set to 2 if you'd like to be inundated with debug messages
18 *
19 * loudness - set between 0 and 15 for varying degrees of loudness effect
20 *
21 * maxvol - set maximium volume to +20db (1), default is 0db(0)
22 *
23 *
24 * Revision: 0.7 - maxvol module parm to set maximium volume 0db or +20db
25 * store if muted so we can return it
26 * change balance only if flaged to
27 * Revision: 0.6 - added tone controls
28 * Revision: 0.5 - Fixed odd balance problem
29 * Revision: 0.4 - added muting
30 * Revision: 0.3 - Fixed silly reversed volume controls. :)
31 * Revision: 0.2 - Cleaned up #defines
32 * fixed volume control
33 * Added I2C_DRIVERID_TDA7432
34 * added loudness insmod control
35 * Revision: 0.1 - initial version
36 */
37
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/kernel.h>
41#include <linux/sched.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/videodev.h>
48#include <linux/i2c.h>
49#include <linux/i2c-algo-bit.h>
50
51#include "bttv.h"
52#include <media/audiochip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#ifndef VIDEO_AUDIO_BALANCE
55# define VIDEO_AUDIO_BALANCE 32
56#endif
57
58MODULE_AUTHOR("Eric Sandeen <eric_sandeen@bigfoot.com>");
59MODULE_DESCRIPTION("bttv driver for the tda7432 audio processor chip");
60MODULE_LICENSE("GPL");
61
62static int maxvol;
63static int loudness; /* disable loudness by default */
64static int debug; /* insmod parameter */
65module_param(debug, int, S_IRUGO | S_IWUSR);
66module_param(loudness, int, S_IRUGO);
67MODULE_PARM_DESC(maxvol,"Set maximium volume to +20db (0), default is 0db(1)");
68module_param(maxvol, int, S_IRUGO | S_IWUSR);
69
70
71/* Address to scan (I2C address of this chip) */
72static unsigned short normal_i2c[] = {
73 I2C_TDA7432 >> 1,
74 I2C_CLIENT_END,
75};
Linus Torvalds1da177e2005-04-16 15:20:36 -070076I2C_CLIENT_INSMOD;
77
78/* Structure of address and subaddresses for the tda7432 */
79
80struct tda7432 {
81 int addr;
82 int input;
83 int volume;
84 int muted;
85 int bass, treble;
86 int lf, lr, rf, rr;
87 int loud;
88 struct i2c_client c;
89};
90static struct i2c_driver driver;
91static struct i2c_client client_template;
92
93#define dprintk if (debug) printk
94#define d2printk if (debug > 1) printk
95
96/* The TDA7432 is made by STS-Thompson
97 * http://www.st.com
98 * http://us.st.com/stonline/books/pdf/docs/4056.pdf
99 *
100 * TDA7432: I2C-bus controlled basic audio processor
101 *
102 * The TDA7432 controls basic audio functions like volume, balance,
103 * and tone control (including loudness). It also has four channel
104 * output (for front and rear). Since most vidcap cards probably
105 * don't have 4 channel output, this driver will set front & rear
106 * together (no independent control).
107 */
108
109 /* Subaddresses for TDA7432 */
110
111#define TDA7432_IN 0x00 /* Input select */
112#define TDA7432_VL 0x01 /* Volume */
113#define TDA7432_TN 0x02 /* Bass, Treble (Tone) */
114#define TDA7432_LF 0x03 /* Attenuation LF (Left Front) */
115#define TDA7432_LR 0x04 /* Attenuation LR (Left Rear) */
116#define TDA7432_RF 0x05 /* Attenuation RF (Right Front) */
117#define TDA7432_RR 0x06 /* Attenuation RR (Right Rear) */
118#define TDA7432_LD 0x07 /* Loudness */
119
120
121 /* Masks for bits in TDA7432 subaddresses */
122
123/* Many of these not used - just for documentation */
124
125/* Subaddress 0x00 - Input selection and bass control */
126
127/* Bits 0,1,2 control input:
128 * 0x00 - Stereo input
129 * 0x02 - Mono input
130 * 0x03 - Mute (Using Attenuators Plays better with modules)
131 * Mono probably isn't used - I'm guessing only the stereo
132 * input is connected on most cards, so we'll set it to stereo.
133 *
134 * Bit 3 controls bass cut: 0/1 is non-symmetric/symmetric bass cut
135 * Bit 4 controls bass range: 0/1 is extended/standard bass range
136 *
137 * Highest 3 bits not used
138 */
139
140#define TDA7432_STEREO_IN 0
141#define TDA7432_MONO_IN 2 /* Probably won't be used */
142#define TDA7432_BASS_SYM 1 << 3
143#define TDA7432_BASS_NORM 1 << 4
144
145/* Subaddress 0x01 - Volume */
146
147/* Lower 7 bits control volume from -79dB to +32dB in 1dB steps
148 * Recommended maximum is +20 dB
149 *
150 * +32dB: 0x00
151 * +20dB: 0x0c
152 * 0dB: 0x20
153 * -79dB: 0x6f
154 *
155 * MSB (bit 7) controls loudness: 1/0 is loudness on/off
156 */
157
158#define TDA7432_VOL_0DB 0x20
159#define TDA7432_LD_ON 1 << 7
160
161
162/* Subaddress 0x02 - Tone control */
163
164/* Bits 0,1,2 control absolute treble gain from 0dB to 14dB
165 * 0x0 is 14dB, 0x7 is 0dB
166 *
167 * Bit 3 controls treble attenuation/gain (sign)
168 * 1 = gain (+)
169 * 0 = attenuation (-)
170 *
171 * Bits 4,5,6 control absolute bass gain from 0dB to 14dB
172 * (This is only true for normal base range, set in 0x00)
173 * 0x0 << 4 is 14dB, 0x7 is 0dB
174 *
175 * Bit 7 controls bass attenuation/gain (sign)
176 * 1 << 7 = gain (+)
177 * 0 << 7 = attenuation (-)
178 *
179 * Example:
180 * 1 1 0 1 0 1 0 1 is +4dB bass, -4dB treble
181 */
182
183#define TDA7432_TREBLE_0DB 0xf
184#define TDA7432_TREBLE 7
185#define TDA7432_TREBLE_GAIN 1 << 3
186#define TDA7432_BASS_0DB 0xf
187#define TDA7432_BASS 7 << 4
188#define TDA7432_BASS_GAIN 1 << 7
189
190
191/* Subaddress 0x03 - Left Front attenuation */
192/* Subaddress 0x04 - Left Rear attenuation */
193/* Subaddress 0x05 - Right Front attenuation */
194/* Subaddress 0x06 - Right Rear attenuation */
195
196/* Bits 0,1,2,3,4 control attenuation from 0dB to -37.5dB
197 * in 1.5dB steps.
198 *
199 * 0x00 is 0dB
200 * 0x1f is -37.5dB
201 *
202 * Bit 5 mutes that channel when set (1 = mute, 0 = unmute)
203 * We'll use the mute on the input, though (above)
204 * Bits 6,7 unused
205 */
206
207#define TDA7432_ATTEN_0DB 0x00
208#define TDA7432_MUTE 0x1 << 5
209
210
211/* Subaddress 0x07 - Loudness Control */
212
213/* Bits 0,1,2,3 control loudness from 0dB to -15dB in 1dB steps
214 * when bit 4 is NOT set
215 *
216 * 0x0 is 0dB
217 * 0xf is -15dB
218 *
219 * If bit 4 is set, then there is a flat attenuation according to
220 * the lower 4 bits, as above.
221 *
222 * Bits 5,6,7 unused
223 */
224
225
226
227/* Begin code */
228
229static int tda7432_write(struct i2c_client *client, int subaddr, int val)
230{
231 unsigned char buffer[2];
232 d2printk("tda7432: In tda7432_write\n");
233 dprintk("tda7432: Writing %d 0x%x\n", subaddr, val);
234 buffer[0] = subaddr;
235 buffer[1] = val;
236 if (2 != i2c_master_send(client,buffer,2)) {
237 printk(KERN_WARNING "tda7432: I/O error, trying (write %d 0x%x)\n",
238 subaddr, val);
239 return -1;
240 }
241 return 0;
242}
243
244/* I don't think we ever actually _read_ the chip... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246static int tda7432_set(struct i2c_client *client)
247{
248 struct tda7432 *t = i2c_get_clientdata(client);
249 unsigned char buf[16];
250 d2printk("tda7432: In tda7432_set\n");
251
252 dprintk(KERN_INFO
253 "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
254 t->input,t->volume,t->bass,t->treble,t->lf,t->lr,t->rf,t->rr,t->loud);
255 buf[0] = TDA7432_IN;
256 buf[1] = t->input;
257 buf[2] = t->volume;
258 buf[3] = t->bass;
259 buf[4] = t->treble;
260 buf[5] = t->lf;
261 buf[6] = t->lr;
262 buf[7] = t->rf;
263 buf[8] = t->rr;
264 buf[9] = t->loud;
265 if (10 != i2c_master_send(client,buf,10)) {
266 printk(KERN_WARNING "tda7432: I/O error, trying tda7432_set\n");
267 return -1;
268 }
269
270 return 0;
271}
272
273static void do_tda7432_init(struct i2c_client *client)
274{
275 struct tda7432 *t = i2c_get_clientdata(client);
276 d2printk("tda7432: In tda7432_init\n");
277
278 t->input = TDA7432_STEREO_IN | /* Main (stereo) input */
279 TDA7432_BASS_SYM | /* Symmetric bass cut */
280 TDA7432_BASS_NORM; /* Normal bass range */
281 t->volume = 0x3b ; /* -27dB Volume */
282 if (loudness) /* Turn loudness on? */
283 t->volume |= TDA7432_LD_ON;
284 t->muted = VIDEO_AUDIO_MUTE;
285 t->treble = TDA7432_TREBLE_0DB; /* 0dB Treble */
286 t->bass = TDA7432_BASS_0DB; /* 0dB Bass */
287 t->lf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
288 t->lr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
289 t->rf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
290 t->rr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
291 t->loud = loudness; /* insmod parameter */
292
293 tda7432_set(client);
294}
295
296/* *********************** *
297 * i2c interface functions *
298 * *********************** */
299
300static int tda7432_attach(struct i2c_adapter *adap, int addr, int kind)
301{
302 struct tda7432 *t;
303 struct i2c_client *client;
304 d2printk("tda7432: In tda7432_attach\n");
305
306 t = kmalloc(sizeof *t,GFP_KERNEL);
307 if (!t)
308 return -ENOMEM;
309 memset(t,0,sizeof *t);
310
311 client = &t->c;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800312 memcpy(client,&client_template,sizeof(struct i2c_client));
313 client->adapter = adap;
314 client->addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 i2c_set_clientdata(client, t);
316
317 do_tda7432_init(client);
318 printk(KERN_INFO "tda7432: init\n");
319
320 i2c_attach_client(client);
321 return 0;
322}
323
324static int tda7432_probe(struct i2c_adapter *adap)
325{
326#ifdef I2C_CLASS_TV_ANALOG
327 if (adap->class & I2C_CLASS_TV_ANALOG)
328 return i2c_probe(adap, &addr_data, tda7432_attach);
329#else
Jean Delvarec7a46532005-08-11 23:41:56 +0200330 if (adap->id == I2C_HW_B_BT848)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return i2c_probe(adap, &addr_data, tda7432_attach);
332#endif
333 return 0;
334}
335
336static int tda7432_detach(struct i2c_client *client)
337{
338 struct tda7432 *t = i2c_get_clientdata(client);
339
340 do_tda7432_init(client);
341 i2c_detach_client(client);
342
343 kfree(t);
344 return 0;
345}
346
347static int tda7432_command(struct i2c_client *client,
348 unsigned int cmd, void *arg)
349{
350 struct tda7432 *t = i2c_get_clientdata(client);
351 d2printk("tda7432: In tda7432_command\n");
352
353 switch (cmd) {
354 /* --- v4l ioctls --- */
355 /* take care: bttv does userspace copying, we'll get a
356 kernel pointer here... */
357
358 /* Query card - scale from TDA7432 settings to V4L settings */
359 case VIDIOCGAUDIO:
360 {
361 struct video_audio *va = arg;
362 dprintk("tda7432: VIDIOCGAUDIO\n");
363
364 va->flags |= VIDEO_AUDIO_VOLUME |
365 VIDEO_AUDIO_BASS |
366 VIDEO_AUDIO_TREBLE |
367 VIDEO_AUDIO_MUTABLE;
368 if (t->muted)
369 va->flags |= VIDEO_AUDIO_MUTE;
370 va->mode |= VIDEO_SOUND_STEREO;
371 /* Master volume control
372 * V4L volume is min 0, max 65535
373 * TDA7432 Volume:
374 * Min (-79dB) is 0x6f
375 * Max (+20dB) is 0x07 (630)
376 * Max (0dB) is 0x20 (829)
377 * (Mask out bit 7 of vol - it's for the loudness setting)
378 */
379 if (!maxvol){ /* max +20db */
380 va->volume = ( 0x6f - (t->volume & 0x7F) ) * 630;
381 } else { /* max 0db */
382 va->volume = ( 0x6f - (t->volume & 0x7F) ) * 829;
383 }
384
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)
391 */
392
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));
399
400 /* Bass/treble 4 bits each */
401 va->bass=t->bass;
402 if(va->bass >= 0x8)
403 va->bass = ~(va->bass - 0x8) & 0xf;
404 va->bass = (va->bass << 12)+(va->bass << 8)+(va->bass << 4)+(va->bass);
405 va->treble=t->treble;
406 if(va->treble >= 0x8)
407 va->treble = ~(va->treble - 0x8) & 0xf;
408 va->treble = (va->treble << 12)+(va->treble << 8)+(va->treble << 4)+(va->treble);
409
410 break; /* VIDIOCGAUDIO case */
411 }
412
413 /* Set card - scale from V4L settings to TDA7432 settings */
414 case VIDIOCSAUDIO:
415 {
416 struct video_audio *va = arg;
417 dprintk("tda7432: VIDEOCSAUDIO\n");
418
419 if(va->flags & VIDEO_AUDIO_VOLUME){
420 if(!maxvol){ /* max +20db */
421 t->volume = 0x6f - ((va->volume)/630);
422 } else { /* max 0db */
423 t->volume = 0x6f - ((va->volume)/829);
424 }
425
426 if (loudness) /* Turn on the loudness bit */
427 t->volume |= TDA7432_LD_ON;
428
429 tda7432_write(client,TDA7432_VL, t->volume);
430 }
431
432 if(va->flags & VIDEO_AUDIO_BASS)
433 {
434 t->bass = va->bass >> 12;
435 if(t->bass>= 0x8)
436 t->bass = (~t->bass & 0xf) + 0x8 ;
437 }
438 if(va->flags & VIDEO_AUDIO_TREBLE)
439 {
440 t->treble= va->treble >> 12;
441 if(t->treble>= 0x8)
442 t->treble = (~t->treble & 0xf) + 0x8 ;
443 }
444 if(va->flags & (VIDEO_AUDIO_TREBLE| VIDEO_AUDIO_BASS))
445 tda7432_write(client,TDA7432_TN, 0x10 | (t->bass << 4) | t->treble );
446
447 if(va->flags & VIDEO_AUDIO_BALANCE) {
448 if (va->balance < 32768)
449 {
450 /* shifted to left, attenuate right */
451 t->rr = (32768 - va->balance)/1057;
452 t->rf = t->rr;
453 t->lr = TDA7432_ATTEN_0DB;
454 t->lf = TDA7432_ATTEN_0DB;
455 }
456 else if(va->balance > 32769)
457 {
458 /* shifted to right, attenuate left */
459 t->lf = (va->balance - 32768)/1057;
460 t->lr = t->lf;
461 t->rr = TDA7432_ATTEN_0DB;
462 t->rf = TDA7432_ATTEN_0DB;
463 }
464 else
465 {
466 /* centered */
467 t->rr = TDA7432_ATTEN_0DB;
468 t->rf = TDA7432_ATTEN_0DB;
469 t->lf = TDA7432_ATTEN_0DB;
470 t->lr = TDA7432_ATTEN_0DB;
471 }
472 }
473
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800474 t->muted=(va->flags & VIDEO_AUDIO_MUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (t->muted)
476 {
477 /* Mute & update balance*/
478 tda7432_write(client,TDA7432_LF, t->lf | TDA7432_MUTE);
479 tda7432_write(client,TDA7432_LR, t->lr | TDA7432_MUTE);
480 tda7432_write(client,TDA7432_RF, t->rf | TDA7432_MUTE);
481 tda7432_write(client,TDA7432_RR, t->rr | TDA7432_MUTE);
482 } else {
483 tda7432_write(client,TDA7432_LF, t->lf);
484 tda7432_write(client,TDA7432_LR, t->lr);
485 tda7432_write(client,TDA7432_RF, t->rf);
486 tda7432_write(client,TDA7432_RR, t->rr);
487 }
488
489 break;
490
491 } /* end of VIDEOCSAUDIO case */
492
493 default: /* Not VIDEOCGAUDIO or VIDEOCSAUDIO */
494
495 /* nothing */
496 d2printk("tda7432: Default\n");
497
498 } /* end of (cmd) switch */
499
500 return 0;
501}
502
503static struct i2c_driver driver = {
504 .owner = THIS_MODULE,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800505 .name = "i2c tda7432 driver",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 .id = I2C_DRIVERID_TDA7432,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800507 .flags = I2C_DF_NOTIFY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 .attach_adapter = tda7432_probe,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800509 .detach_client = tda7432_detach,
510 .command = tda7432_command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511};
512
513static struct i2c_client client_template =
514{
Jean Delvarefae91e72005-08-15 19:57:04 +0200515 .name = "tda7432",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 .driver = &driver,
517};
518
519static int __init tda7432_init(void)
520{
521 if ( (loudness < 0) || (loudness > 15) ) {
522 printk(KERN_ERR "tda7432: loudness parameter must be between 0 and 15\n");
523 return -EINVAL;
524 }
525
526 return i2c_add_driver(&driver);
527}
528
529static void __exit tda7432_fini(void)
530{
531 i2c_del_driver(&driver);
532}
533
534module_init(tda7432_init);
535module_exit(tda7432_fini);
536
537/*
538 * Local variables:
539 * c-basic-offset: 8
540 * End:
541 */