Pull bugzilla-7887 into release branch
[deliverable/linux.git] / drivers / media / video / tda7432.c
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.
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/string.h>
42 #include <linux/timer.h>
43 #include <linux/delay.h>
44 #include <linux/errno.h>
45 #include <linux/slab.h>
46 #include <linux/videodev.h>
47 #include <linux/i2c.h>
48 #include <linux/i2c-algo-bit.h>
49
50 #include <media/v4l2-common.h>
51 #include <media/i2c-addr.h>
52
53 #ifndef VIDEO_AUDIO_BALANCE
54 # define VIDEO_AUDIO_BALANCE 32
55 #endif
56
57 MODULE_AUTHOR("Eric Sandeen <eric_sandeen@bigfoot.com>");
58 MODULE_DESCRIPTION("bttv driver for the tda7432 audio processor chip");
59 MODULE_LICENSE("GPL");
60
61 static int maxvol;
62 static int loudness; /* disable loudness by default */
63 static int debug; /* insmod parameter */
64 module_param(debug, int, S_IRUGO | S_IWUSR);
65 module_param(loudness, int, S_IRUGO);
66 MODULE_PARM_DESC(maxvol,"Set maximium volume to +20db (0), default is 0db(1)");
67 module_param(maxvol, int, S_IRUGO | S_IWUSR);
68
69
70 /* Address to scan (I2C address of this chip) */
71 static unsigned short normal_i2c[] = {
72 I2C_ADDR_TDA7432 >> 1,
73 I2C_CLIENT_END,
74 };
75 I2C_CLIENT_INSMOD;
76
77 /* Structure of address and subaddresses for the tda7432 */
78
79 struct tda7432 {
80 int addr;
81 int input;
82 int volume;
83 int muted;
84 int bass, treble;
85 int lf, lr, rf, rr;
86 int loud;
87 struct i2c_client c;
88 };
89 static struct i2c_driver driver;
90 static struct i2c_client client_template;
91
92 /* The TDA7432 is made by STS-Thompson
93 * http://www.st.com
94 * http://us.st.com/stonline/books/pdf/docs/4056.pdf
95 *
96 * TDA7432: I2C-bus controlled basic audio processor
97 *
98 * The TDA7432 controls basic audio functions like volume, balance,
99 * and tone control (including loudness). It also has four channel
100 * output (for front and rear). Since most vidcap cards probably
101 * don't have 4 channel output, this driver will set front & rear
102 * together (no independent control).
103 */
104
105 /* Subaddresses for TDA7432 */
106
107 #define TDA7432_IN 0x00 /* Input select */
108 #define TDA7432_VL 0x01 /* Volume */
109 #define TDA7432_TN 0x02 /* Bass, Treble (Tone) */
110 #define TDA7432_LF 0x03 /* Attenuation LF (Left Front) */
111 #define TDA7432_LR 0x04 /* Attenuation LR (Left Rear) */
112 #define TDA7432_RF 0x05 /* Attenuation RF (Right Front) */
113 #define TDA7432_RR 0x06 /* Attenuation RR (Right Rear) */
114 #define TDA7432_LD 0x07 /* Loudness */
115
116
117 /* Masks for bits in TDA7432 subaddresses */
118
119 /* Many of these not used - just for documentation */
120
121 /* Subaddress 0x00 - Input selection and bass control */
122
123 /* Bits 0,1,2 control input:
124 * 0x00 - Stereo input
125 * 0x02 - Mono input
126 * 0x03 - Mute (Using Attenuators Plays better with modules)
127 * Mono probably isn't used - I'm guessing only the stereo
128 * input is connected on most cards, so we'll set it to stereo.
129 *
130 * Bit 3 controls bass cut: 0/1 is non-symmetric/symmetric bass cut
131 * Bit 4 controls bass range: 0/1 is extended/standard bass range
132 *
133 * Highest 3 bits not used
134 */
135
136 #define TDA7432_STEREO_IN 0
137 #define TDA7432_MONO_IN 2 /* Probably won't be used */
138 #define TDA7432_BASS_SYM 1 << 3
139 #define TDA7432_BASS_NORM 1 << 4
140
141 /* Subaddress 0x01 - Volume */
142
143 /* Lower 7 bits control volume from -79dB to +32dB in 1dB steps
144 * Recommended maximum is +20 dB
145 *
146 * +32dB: 0x00
147 * +20dB: 0x0c
148 * 0dB: 0x20
149 * -79dB: 0x6f
150 *
151 * MSB (bit 7) controls loudness: 1/0 is loudness on/off
152 */
153
154 #define TDA7432_VOL_0DB 0x20
155 #define TDA7432_LD_ON 1 << 7
156
157
158 /* Subaddress 0x02 - Tone control */
159
160 /* Bits 0,1,2 control absolute treble gain from 0dB to 14dB
161 * 0x0 is 14dB, 0x7 is 0dB
162 *
163 * Bit 3 controls treble attenuation/gain (sign)
164 * 1 = gain (+)
165 * 0 = attenuation (-)
166 *
167 * Bits 4,5,6 control absolute bass gain from 0dB to 14dB
168 * (This is only true for normal base range, set in 0x00)
169 * 0x0 << 4 is 14dB, 0x7 is 0dB
170 *
171 * Bit 7 controls bass attenuation/gain (sign)
172 * 1 << 7 = gain (+)
173 * 0 << 7 = attenuation (-)
174 *
175 * Example:
176 * 1 1 0 1 0 1 0 1 is +4dB bass, -4dB treble
177 */
178
179 #define TDA7432_TREBLE_0DB 0xf
180 #define TDA7432_TREBLE 7
181 #define TDA7432_TREBLE_GAIN 1 << 3
182 #define TDA7432_BASS_0DB 0xf
183 #define TDA7432_BASS 7 << 4
184 #define TDA7432_BASS_GAIN 1 << 7
185
186
187 /* Subaddress 0x03 - Left Front attenuation */
188 /* Subaddress 0x04 - Left Rear attenuation */
189 /* Subaddress 0x05 - Right Front attenuation */
190 /* Subaddress 0x06 - Right Rear attenuation */
191
192 /* Bits 0,1,2,3,4 control attenuation from 0dB to -37.5dB
193 * in 1.5dB steps.
194 *
195 * 0x00 is 0dB
196 * 0x1f is -37.5dB
197 *
198 * Bit 5 mutes that channel when set (1 = mute, 0 = unmute)
199 * We'll use the mute on the input, though (above)
200 * Bits 6,7 unused
201 */
202
203 #define TDA7432_ATTEN_0DB 0x00
204 #define TDA7432_MUTE 0x1 << 5
205
206
207 /* Subaddress 0x07 - Loudness Control */
208
209 /* Bits 0,1,2,3 control loudness from 0dB to -15dB in 1dB steps
210 * when bit 4 is NOT set
211 *
212 * 0x0 is 0dB
213 * 0xf is -15dB
214 *
215 * If bit 4 is set, then there is a flat attenuation according to
216 * the lower 4 bits, as above.
217 *
218 * Bits 5,6,7 unused
219 */
220
221
222
223 /* Begin code */
224
225 static int tda7432_write(struct i2c_client *client, int subaddr, int val)
226 {
227 unsigned char buffer[2];
228 v4l_dbg(2, debug,client,"In tda7432_write\n");
229 v4l_dbg(1, debug,client,"Writing %d 0x%x\n", subaddr, val);
230 buffer[0] = subaddr;
231 buffer[1] = val;
232 if (2 != i2c_master_send(client,buffer,2)) {
233 v4l_err(client,"I/O error, trying (write %d 0x%x)\n",
234 subaddr, val);
235 return -1;
236 }
237 return 0;
238 }
239
240 /* I don't think we ever actually _read_ the chip... */
241
242 static int tda7432_set(struct i2c_client *client)
243 {
244 struct tda7432 *t = i2c_get_clientdata(client);
245 unsigned char buf[16];
246 v4l_dbg(2, debug,client,"In tda7432_set\n");
247
248 v4l_dbg(1, debug,client,
249 "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
250 t->input,t->volume,t->bass,t->treble,t->lf,t->lr,t->rf,t->rr,t->loud);
251 buf[0] = TDA7432_IN;
252 buf[1] = t->input;
253 buf[2] = t->volume;
254 buf[3] = t->bass;
255 buf[4] = t->treble;
256 buf[5] = t->lf;
257 buf[6] = t->lr;
258 buf[7] = t->rf;
259 buf[8] = t->rr;
260 buf[9] = t->loud;
261 if (10 != i2c_master_send(client,buf,10)) {
262 v4l_err(client,"I/O error, trying tda7432_set\n");
263 return -1;
264 }
265
266 return 0;
267 }
268
269 static void do_tda7432_init(struct i2c_client *client)
270 {
271 struct tda7432 *t = i2c_get_clientdata(client);
272 v4l_dbg(2, debug,client,"In tda7432_init\n");
273
274 t->input = TDA7432_STEREO_IN | /* Main (stereo) input */
275 TDA7432_BASS_SYM | /* Symmetric bass cut */
276 TDA7432_BASS_NORM; /* Normal bass range */
277 t->volume = 0x3b ; /* -27dB Volume */
278 if (loudness) /* Turn loudness on? */
279 t->volume |= TDA7432_LD_ON;
280 t->muted = VIDEO_AUDIO_MUTE;
281 t->treble = TDA7432_TREBLE_0DB; /* 0dB Treble */
282 t->bass = TDA7432_BASS_0DB; /* 0dB Bass */
283 t->lf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
284 t->lr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
285 t->rf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
286 t->rr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
287 t->loud = loudness; /* insmod parameter */
288
289 tda7432_set(client);
290 }
291
292 /* *********************** *
293 * i2c interface functions *
294 * *********************** */
295
296 static int tda7432_attach(struct i2c_adapter *adap, int addr, int kind)
297 {
298 struct tda7432 *t;
299 struct i2c_client *client;
300
301 t = kzalloc(sizeof *t,GFP_KERNEL);
302 if (!t)
303 return -ENOMEM;
304
305 client = &t->c;
306 memcpy(client,&client_template,sizeof(struct i2c_client));
307 client->adapter = adap;
308 client->addr = addr;
309 i2c_set_clientdata(client, t);
310
311 do_tda7432_init(client);
312 i2c_attach_client(client);
313
314 v4l_info(client, "chip found @ 0x%x (%s)\n", addr << 1, adap->name);
315 return 0;
316 }
317
318 static int tda7432_probe(struct i2c_adapter *adap)
319 {
320 if (adap->class & I2C_CLASS_TV_ANALOG)
321 return i2c_probe(adap, &addr_data, tda7432_attach);
322 return 0;
323 }
324
325 static int tda7432_detach(struct i2c_client *client)
326 {
327 struct tda7432 *t = i2c_get_clientdata(client);
328
329 do_tda7432_init(client);
330 i2c_detach_client(client);
331
332 kfree(t);
333 return 0;
334 }
335
336 static int tda7432_command(struct i2c_client *client,
337 unsigned int cmd, void *arg)
338 {
339 struct tda7432 *t = i2c_get_clientdata(client);
340 v4l_dbg(2, debug,client,"In tda7432_command\n");
341 if (debug>1)
342 v4l_i2c_print_ioctl(client,cmd);
343
344 switch (cmd) {
345 /* --- v4l ioctls --- */
346 /* take care: bttv does userspace copying, we'll get a
347 kernel pointer here... */
348
349 /* Query card - scale from TDA7432 settings to V4L settings */
350 case VIDIOCGAUDIO:
351 {
352 struct video_audio *va = arg;
353
354 va->flags |= VIDEO_AUDIO_VOLUME |
355 VIDEO_AUDIO_BASS |
356 VIDEO_AUDIO_TREBLE |
357 VIDEO_AUDIO_MUTABLE;
358 if (t->muted)
359 va->flags |= VIDEO_AUDIO_MUTE;
360 va->mode |= VIDEO_SOUND_STEREO;
361 /* Master volume control
362 * V4L volume is min 0, max 65535
363 * TDA7432 Volume:
364 * Min (-79dB) is 0x6f
365 * Max (+20dB) is 0x07 (630)
366 * Max (0dB) is 0x20 (829)
367 * (Mask out bit 7 of vol - it's for the loudness setting)
368 */
369 if (!maxvol){ /* max +20db */
370 va->volume = ( 0x6f - (t->volume & 0x7F) ) * 630;
371 } else { /* max 0db */
372 va->volume = ( 0x6f - (t->volume & 0x7F) ) * 829;
373 }
374
375 /* Balance depends on L,R attenuation
376 * V4L balance is 0 to 65535, middle is 32768
377 * TDA7432 attenuation: min (0dB) is 0, max (-37.5dB) is 0x1f
378 * to scale up to V4L numbers, mult by 1057
379 * attenuation exists for lf, lr, rf, rr
380 * we use only lf and rf (front channels)
381 */
382
383 if ( (t->lf) < (t->rf) )
384 /* right is attenuated, balance shifted left */
385 va->balance = (32768 - 1057*(t->rf));
386 else
387 /* left is attenuated, balance shifted right */
388 va->balance = (32768 + 1057*(t->lf));
389
390 /* Bass/treble 4 bits each */
391 va->bass=t->bass;
392 if(va->bass >= 0x8)
393 va->bass = ~(va->bass - 0x8) & 0xf;
394 va->bass = (va->bass << 12)+(va->bass << 8)+(va->bass << 4)+(va->bass);
395 va->treble=t->treble;
396 if(va->treble >= 0x8)
397 va->treble = ~(va->treble - 0x8) & 0xf;
398 va->treble = (va->treble << 12)+(va->treble << 8)+(va->treble << 4)+(va->treble);
399
400 break; /* VIDIOCGAUDIO case */
401 }
402
403 /* Set card - scale from V4L settings to TDA7432 settings */
404 case VIDIOCSAUDIO:
405 {
406 struct video_audio *va = arg;
407
408 if(va->flags & VIDEO_AUDIO_VOLUME){
409 if(!maxvol){ /* max +20db */
410 t->volume = 0x6f - ((va->volume)/630);
411 } else { /* max 0db */
412 t->volume = 0x6f - ((va->volume)/829);
413 }
414
415 if (loudness) /* Turn on the loudness bit */
416 t->volume |= TDA7432_LD_ON;
417
418 tda7432_write(client,TDA7432_VL, t->volume);
419 }
420
421 if(va->flags & VIDEO_AUDIO_BASS)
422 {
423 t->bass = va->bass >> 12;
424 if(t->bass>= 0x8)
425 t->bass = (~t->bass & 0xf) + 0x8 ;
426 }
427 if(va->flags & VIDEO_AUDIO_TREBLE)
428 {
429 t->treble= va->treble >> 12;
430 if(t->treble>= 0x8)
431 t->treble = (~t->treble & 0xf) + 0x8 ;
432 }
433 if(va->flags & (VIDEO_AUDIO_TREBLE| VIDEO_AUDIO_BASS))
434 tda7432_write(client,TDA7432_TN, 0x10 | (t->bass << 4) | t->treble );
435
436 if(va->flags & VIDEO_AUDIO_BALANCE) {
437 if (va->balance < 32768)
438 {
439 /* shifted to left, attenuate right */
440 t->rr = (32768 - va->balance)/1057;
441 t->rf = t->rr;
442 t->lr = TDA7432_ATTEN_0DB;
443 t->lf = TDA7432_ATTEN_0DB;
444 }
445 else if(va->balance > 32769)
446 {
447 /* shifted to right, attenuate left */
448 t->lf = (va->balance - 32768)/1057;
449 t->lr = t->lf;
450 t->rr = TDA7432_ATTEN_0DB;
451 t->rf = TDA7432_ATTEN_0DB;
452 }
453 else
454 {
455 /* centered */
456 t->rr = TDA7432_ATTEN_0DB;
457 t->rf = TDA7432_ATTEN_0DB;
458 t->lf = TDA7432_ATTEN_0DB;
459 t->lr = TDA7432_ATTEN_0DB;
460 }
461 }
462
463 t->muted=(va->flags & VIDEO_AUDIO_MUTE);
464 if (t->muted)
465 {
466 /* Mute & update balance*/
467 tda7432_write(client,TDA7432_LF, t->lf | TDA7432_MUTE);
468 tda7432_write(client,TDA7432_LR, t->lr | TDA7432_MUTE);
469 tda7432_write(client,TDA7432_RF, t->rf | TDA7432_MUTE);
470 tda7432_write(client,TDA7432_RR, t->rr | TDA7432_MUTE);
471 } else {
472 tda7432_write(client,TDA7432_LF, t->lf);
473 tda7432_write(client,TDA7432_LR, t->lr);
474 tda7432_write(client,TDA7432_RF, t->rf);
475 tda7432_write(client,TDA7432_RR, t->rr);
476 }
477
478 break;
479
480 } /* end of VIDEOCSAUDIO case */
481
482 } /* end of (cmd) switch */
483
484 return 0;
485 }
486
487 static struct i2c_driver driver = {
488 .driver = {
489 .name = "tda7432",
490 },
491 .id = I2C_DRIVERID_TDA7432,
492 .attach_adapter = tda7432_probe,
493 .detach_client = tda7432_detach,
494 .command = tda7432_command,
495 };
496
497 static struct i2c_client client_template =
498 {
499 .name = "tda7432",
500 .driver = &driver,
501 };
502
503 static int __init tda7432_init(void)
504 {
505 if ( (loudness < 0) || (loudness > 15) ) {
506 printk(KERN_ERR "loudness parameter must be between 0 and 15\n");
507 return -EINVAL;
508 }
509
510 return i2c_add_driver(&driver);
511 }
512
513 static void __exit tda7432_fini(void)
514 {
515 i2c_del_driver(&driver);
516 }
517
518 module_init(tda7432_init);
519 module_exit(tda7432_fini);
520
521 /*
522 * Local variables:
523 * c-basic-offset: 8
524 * End:
525 */
This page took 0.074923 seconds and 6 git commands to generate.