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