blob: 78e043ac9ea0e39fa2195d0b9ddba060db56361b [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
Mauro Carvalho Chehab06804812006-01-09 15:32:46 -020051#include <media/v4l2-common.h>
Mauro Carvalho Chehabfa3fcce2006-03-23 21:45:24 -030052#include <media/i2c-addr.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[] = {
Mauro Carvalho Chehab09df1c12006-03-18 08:31:34 -030073 I2C_ADDR_TDA7432 >> 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* The TDA7432 is made by STS-Thompson
94 * http://www.st.com
95 * http://us.st.com/stonline/books/pdf/docs/4056.pdf
96 *
97 * TDA7432: I2C-bus controlled basic audio processor
98 *
99 * The TDA7432 controls basic audio functions like volume, balance,
100 * and tone control (including loudness). It also has four channel
101 * output (for front and rear). Since most vidcap cards probably
102 * don't have 4 channel output, this driver will set front & rear
103 * together (no independent control).
104 */
105
106 /* Subaddresses for TDA7432 */
107
108#define TDA7432_IN 0x00 /* Input select */
109#define TDA7432_VL 0x01 /* Volume */
110#define TDA7432_TN 0x02 /* Bass, Treble (Tone) */
111#define TDA7432_LF 0x03 /* Attenuation LF (Left Front) */
112#define TDA7432_LR 0x04 /* Attenuation LR (Left Rear) */
113#define TDA7432_RF 0x05 /* Attenuation RF (Right Front) */
114#define TDA7432_RR 0x06 /* Attenuation RR (Right Rear) */
115#define TDA7432_LD 0x07 /* Loudness */
116
117
118 /* Masks for bits in TDA7432 subaddresses */
119
120/* Many of these not used - just for documentation */
121
122/* Subaddress 0x00 - Input selection and bass control */
123
124/* Bits 0,1,2 control input:
125 * 0x00 - Stereo input
126 * 0x02 - Mono input
127 * 0x03 - Mute (Using Attenuators Plays better with modules)
128 * Mono probably isn't used - I'm guessing only the stereo
129 * input is connected on most cards, so we'll set it to stereo.
130 *
131 * Bit 3 controls bass cut: 0/1 is non-symmetric/symmetric bass cut
132 * Bit 4 controls bass range: 0/1 is extended/standard bass range
133 *
134 * Highest 3 bits not used
135 */
136
137#define TDA7432_STEREO_IN 0
138#define TDA7432_MONO_IN 2 /* Probably won't be used */
139#define TDA7432_BASS_SYM 1 << 3
140#define TDA7432_BASS_NORM 1 << 4
141
142/* Subaddress 0x01 - Volume */
143
144/* Lower 7 bits control volume from -79dB to +32dB in 1dB steps
145 * Recommended maximum is +20 dB
146 *
147 * +32dB: 0x00
148 * +20dB: 0x0c
149 * 0dB: 0x20
150 * -79dB: 0x6f
151 *
152 * MSB (bit 7) controls loudness: 1/0 is loudness on/off
153 */
154
155#define TDA7432_VOL_0DB 0x20
156#define TDA7432_LD_ON 1 << 7
157
158
159/* Subaddress 0x02 - Tone control */
160
161/* Bits 0,1,2 control absolute treble gain from 0dB to 14dB
162 * 0x0 is 14dB, 0x7 is 0dB
163 *
164 * Bit 3 controls treble attenuation/gain (sign)
165 * 1 = gain (+)
166 * 0 = attenuation (-)
167 *
168 * Bits 4,5,6 control absolute bass gain from 0dB to 14dB
169 * (This is only true for normal base range, set in 0x00)
170 * 0x0 << 4 is 14dB, 0x7 is 0dB
171 *
172 * Bit 7 controls bass attenuation/gain (sign)
173 * 1 << 7 = gain (+)
174 * 0 << 7 = attenuation (-)
175 *
176 * Example:
177 * 1 1 0 1 0 1 0 1 is +4dB bass, -4dB treble
178 */
179
180#define TDA7432_TREBLE_0DB 0xf
181#define TDA7432_TREBLE 7
182#define TDA7432_TREBLE_GAIN 1 << 3
183#define TDA7432_BASS_0DB 0xf
184#define TDA7432_BASS 7 << 4
185#define TDA7432_BASS_GAIN 1 << 7
186
187
188/* Subaddress 0x03 - Left Front attenuation */
189/* Subaddress 0x04 - Left Rear attenuation */
190/* Subaddress 0x05 - Right Front attenuation */
191/* Subaddress 0x06 - Right Rear attenuation */
192
193/* Bits 0,1,2,3,4 control attenuation from 0dB to -37.5dB
194 * in 1.5dB steps.
195 *
196 * 0x00 is 0dB
197 * 0x1f is -37.5dB
198 *
199 * Bit 5 mutes that channel when set (1 = mute, 0 = unmute)
200 * We'll use the mute on the input, though (above)
201 * Bits 6,7 unused
202 */
203
204#define TDA7432_ATTEN_0DB 0x00
205#define TDA7432_MUTE 0x1 << 5
206
207
208/* Subaddress 0x07 - Loudness Control */
209
210/* Bits 0,1,2,3 control loudness from 0dB to -15dB in 1dB steps
211 * when bit 4 is NOT set
212 *
213 * 0x0 is 0dB
214 * 0xf is -15dB
215 *
216 * If bit 4 is set, then there is a flat attenuation according to
217 * the lower 4 bits, as above.
218 *
219 * Bits 5,6,7 unused
220 */
221
222
223
224/* Begin code */
225
226static int tda7432_write(struct i2c_client *client, int subaddr, int val)
227{
228 unsigned char buffer[2];
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200229 v4l_dbg(2, debug,client,"In tda7432_write\n");
230 v4l_dbg(1, debug,client,"Writing %d 0x%x\n", subaddr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 buffer[0] = subaddr;
232 buffer[1] = val;
233 if (2 != i2c_master_send(client,buffer,2)) {
Mauro Carvalho Chehab06804812006-01-09 15:32:46 -0200234 v4l_err(client,"I/O error, trying (write %d 0x%x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 subaddr, val);
236 return -1;
237 }
238 return 0;
239}
240
241/* I don't think we ever actually _read_ the chip... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243static int tda7432_set(struct i2c_client *client)
244{
245 struct tda7432 *t = i2c_get_clientdata(client);
246 unsigned char buf[16];
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200247 v4l_dbg(2, debug,client,"In tda7432_set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200249 v4l_dbg(1, debug,client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
251 t->input,t->volume,t->bass,t->treble,t->lf,t->lr,t->rf,t->rr,t->loud);
252 buf[0] = TDA7432_IN;
253 buf[1] = t->input;
254 buf[2] = t->volume;
255 buf[3] = t->bass;
256 buf[4] = t->treble;
257 buf[5] = t->lf;
258 buf[6] = t->lr;
259 buf[7] = t->rf;
260 buf[8] = t->rr;
261 buf[9] = t->loud;
262 if (10 != i2c_master_send(client,buf,10)) {
Mauro Carvalho Chehab06804812006-01-09 15:32:46 -0200263 v4l_err(client,"I/O error, trying tda7432_set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return -1;
265 }
266
267 return 0;
268}
269
270static void do_tda7432_init(struct i2c_client *client)
271{
272 struct tda7432 *t = i2c_get_clientdata(client);
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200273 v4l_dbg(2, debug,client,"In tda7432_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
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;
281 t->muted = VIDEO_AUDIO_MUTE;
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 */
289
290 tda7432_set(client);
291}
292
293/* *********************** *
294 * i2c interface functions *
295 * *********************** */
296
297static int tda7432_attach(struct i2c_adapter *adap, int addr, int kind)
298{
299 struct tda7432 *t;
300 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Panagiotis Issaris74081872006-01-11 19:40:56 -0200302 t = kzalloc(sizeof *t,GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (!t)
304 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 client = &t->c;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800307 memcpy(client,&client_template,sizeof(struct i2c_client));
308 client->adapter = adap;
309 client->addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 i2c_set_clientdata(client, t);
311
312 do_tda7432_init(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 i2c_attach_client(client);
Mauro Carvalho Chehab06804812006-01-09 15:32:46 -0200314
315 v4l_info(client, "chip found @ 0x%x (%s)\n", addr << 1, adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317}
318
319static int tda7432_probe(struct i2c_adapter *adap)
320{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (adap->class & I2C_CLASS_TV_ANALOG)
322 return i2c_probe(adap, &addr_data, tda7432_attach);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return 0;
324}
325
326static int tda7432_detach(struct i2c_client *client)
327{
328 struct tda7432 *t = i2c_get_clientdata(client);
329
330 do_tda7432_init(client);
331 i2c_detach_client(client);
332
333 kfree(t);
334 return 0;
335}
336
337static int tda7432_command(struct i2c_client *client,
338 unsigned int cmd, void *arg)
339{
340 struct tda7432 *t = i2c_get_clientdata(client);
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -0200341 v4l_dbg(2, debug,client,"In tda7432_command\n");
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -0200342 if (debug>1)
343 v4l_i2c_print_ioctl(client,cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 switch (cmd) {
346 /* --- v4l ioctls --- */
347 /* take care: bttv does userspace copying, we'll get a
348 kernel pointer here... */
349
350 /* Query card - scale from TDA7432 settings to V4L settings */
351 case VIDIOCGAUDIO:
352 {
353 struct video_audio *va = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 va->flags |= VIDEO_AUDIO_VOLUME |
356 VIDEO_AUDIO_BASS |
357 VIDEO_AUDIO_TREBLE |
358 VIDEO_AUDIO_MUTABLE;
359 if (t->muted)
360 va->flags |= VIDEO_AUDIO_MUTE;
361 va->mode |= VIDEO_SOUND_STEREO;
362 /* Master volume control
363 * V4L volume is min 0, max 65535
364 * TDA7432 Volume:
365 * Min (-79dB) is 0x6f
366 * Max (+20dB) is 0x07 (630)
367 * Max (0dB) is 0x20 (829)
368 * (Mask out bit 7 of vol - it's for the loudness setting)
369 */
370 if (!maxvol){ /* max +20db */
371 va->volume = ( 0x6f - (t->volume & 0x7F) ) * 630;
372 } else { /* max 0db */
373 va->volume = ( 0x6f - (t->volume & 0x7F) ) * 829;
374 }
375
376 /* Balance depends on L,R attenuation
377 * V4L balance is 0 to 65535, middle is 32768
378 * TDA7432 attenuation: min (0dB) is 0, max (-37.5dB) is 0x1f
379 * to scale up to V4L numbers, mult by 1057
380 * attenuation exists for lf, lr, rf, rr
381 * we use only lf and rf (front channels)
382 */
383
384 if ( (t->lf) < (t->rf) )
385 /* right is attenuated, balance shifted left */
386 va->balance = (32768 - 1057*(t->rf));
387 else
388 /* left is attenuated, balance shifted right */
389 va->balance = (32768 + 1057*(t->lf));
390
391 /* Bass/treble 4 bits each */
392 va->bass=t->bass;
393 if(va->bass >= 0x8)
394 va->bass = ~(va->bass - 0x8) & 0xf;
395 va->bass = (va->bass << 12)+(va->bass << 8)+(va->bass << 4)+(va->bass);
396 va->treble=t->treble;
397 if(va->treble >= 0x8)
398 va->treble = ~(va->treble - 0x8) & 0xf;
399 va->treble = (va->treble << 12)+(va->treble << 8)+(va->treble << 4)+(va->treble);
400
401 break; /* VIDIOCGAUDIO case */
402 }
403
404 /* Set card - scale from V4L settings to TDA7432 settings */
405 case VIDIOCSAUDIO:
406 {
407 struct video_audio *va = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 if(va->flags & VIDEO_AUDIO_VOLUME){
410 if(!maxvol){ /* max +20db */
411 t->volume = 0x6f - ((va->volume)/630);
412 } else { /* max 0db */
413 t->volume = 0x6f - ((va->volume)/829);
414 }
415
416 if (loudness) /* Turn on the loudness bit */
417 t->volume |= TDA7432_LD_ON;
418
419 tda7432_write(client,TDA7432_VL, t->volume);
420 }
421
422 if(va->flags & VIDEO_AUDIO_BASS)
423 {
424 t->bass = va->bass >> 12;
425 if(t->bass>= 0x8)
426 t->bass = (~t->bass & 0xf) + 0x8 ;
427 }
428 if(va->flags & VIDEO_AUDIO_TREBLE)
429 {
430 t->treble= va->treble >> 12;
431 if(t->treble>= 0x8)
432 t->treble = (~t->treble & 0xf) + 0x8 ;
433 }
434 if(va->flags & (VIDEO_AUDIO_TREBLE| VIDEO_AUDIO_BASS))
435 tda7432_write(client,TDA7432_TN, 0x10 | (t->bass << 4) | t->treble );
436
437 if(va->flags & VIDEO_AUDIO_BALANCE) {
438 if (va->balance < 32768)
439 {
440 /* shifted to left, attenuate right */
441 t->rr = (32768 - va->balance)/1057;
442 t->rf = t->rr;
443 t->lr = TDA7432_ATTEN_0DB;
444 t->lf = TDA7432_ATTEN_0DB;
445 }
446 else if(va->balance > 32769)
447 {
448 /* shifted to right, attenuate left */
449 t->lf = (va->balance - 32768)/1057;
450 t->lr = t->lf;
451 t->rr = TDA7432_ATTEN_0DB;
452 t->rf = TDA7432_ATTEN_0DB;
453 }
454 else
455 {
456 /* centered */
457 t->rr = TDA7432_ATTEN_0DB;
458 t->rf = TDA7432_ATTEN_0DB;
459 t->lf = TDA7432_ATTEN_0DB;
460 t->lr = TDA7432_ATTEN_0DB;
461 }
462 }
463
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800464 t->muted=(va->flags & VIDEO_AUDIO_MUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (t->muted)
466 {
467 /* Mute & update balance*/
468 tda7432_write(client,TDA7432_LF, t->lf | TDA7432_MUTE);
469 tda7432_write(client,TDA7432_LR, t->lr | TDA7432_MUTE);
470 tda7432_write(client,TDA7432_RF, t->rf | TDA7432_MUTE);
471 tda7432_write(client,TDA7432_RR, t->rr | TDA7432_MUTE);
472 } else {
473 tda7432_write(client,TDA7432_LF, t->lf);
474 tda7432_write(client,TDA7432_LR, t->lr);
475 tda7432_write(client,TDA7432_RF, t->rf);
476 tda7432_write(client,TDA7432_RR, t->rr);
477 }
478
479 break;
480
481 } /* end of VIDEOCSAUDIO case */
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 } /* end of (cmd) switch */
484
485 return 0;
486}
487
488static struct i2c_driver driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100489 .driver = {
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -0200490 .name = "tda7432",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100491 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 .id = I2C_DRIVERID_TDA7432,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 .attach_adapter = tda7432_probe,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800494 .detach_client = tda7432_detach,
495 .command = tda7432_command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496};
497
498static struct i2c_client client_template =
499{
Jean Delvarefae91e72005-08-15 19:57:04 +0200500 .name = "tda7432",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 .driver = &driver,
502};
503
504static int __init tda7432_init(void)
505{
506 if ( (loudness < 0) || (loudness > 15) ) {
Mauro Carvalho Chehab06804812006-01-09 15:32:46 -0200507 printk(KERN_ERR "loudness parameter must be between 0 and 15\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return -EINVAL;
509 }
510
511 return i2c_add_driver(&driver);
512}
513
514static void __exit tda7432_fini(void)
515{
516 i2c_del_driver(&driver);
517}
518
519module_init(tda7432_init);
520module_exit(tda7432_fini);
521
522/*
523 * Local variables:
524 * c-basic-offset: 8
525 * End:
526 */