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