0c020585fffbc02f4cb49865868cb690f4122555
[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  * Copyright (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
12  * This code is placed under the terms of the GNU General Public License
13  * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
14  * Which was based on tda8425.c by Greg Alexander (c) 1998
15  *
16  * OPTIONS:
17  * debug    - set to 1 if you'd like to see debug messages
18  *            set to 2 if you'd like to be inundated with debug messages
19  *
20  * loudness - set between 0 and 15 for varying degrees of loudness effect
21  *
22  * maxvol   - set maximium volume to +20db (1), default is 0db(0)
23  *
24  *
25  *  Revision: 0.7 - maxvol module parm to set maximium volume 0db or +20db
26  *                              store if muted so we can return it
27  *                              change balance only if flaged to
28  *  Revision: 0.6 - added tone controls
29  *  Revision: 0.5 - Fixed odd balance problem
30  *  Revision: 0.4 - added muting
31  *  Revision: 0.3 - Fixed silly reversed volume controls.  :)
32  *  Revision: 0.2 - Cleaned up #defines
33  *                      fixed volume control
34  *          Added I2C_DRIVERID_TDA7432
35  *                      added loudness insmod control
36  *  Revision: 0.1 - initial version
37  */
38
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/kernel.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/videodev2.h>
48 #include <linux/i2c.h>
49
50 #include <media/v4l2-device.h>
51 #include <media/v4l2-ioctl.h>
52 #include <media/i2c-addr.h>
53 #include <media/v4l2-i2c-drv-legacy.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_ADDR_TDA7432 >> 1,
75         I2C_CLIENT_END,
76 };
77
78 I2C_CLIENT_INSMOD;
79
80 /* Structure of address and subaddresses for the tda7432 */
81
82 struct tda7432 {
83         struct v4l2_subdev sd;
84         int addr;
85         int input;
86         int volume;
87         int muted;
88         int bass, treble;
89         int lf, lr, rf, rr;
90         int loud;
91 };
92
93 static inline struct tda7432 *to_state(struct v4l2_subdev *sd)
94 {
95         return container_of(sd, struct tda7432, sd);
96 }
97
98 /* The TDA7432 is made by STS-Thompson
99  * http://www.st.com
100  * http://us.st.com/stonline/books/pdf/docs/4056.pdf
101  *
102  * TDA7432: I2C-bus controlled basic audio processor
103  *
104  * The TDA7432 controls basic audio functions like volume, balance,
105  * and tone control (including loudness).  It also has four channel
106  * output (for front and rear).  Since most vidcap cards probably
107  * don't have 4 channel output, this driver will set front & rear
108  * together (no independent control).
109  */
110
111                 /* Subaddresses for TDA7432 */
112
113 #define TDA7432_IN      0x00 /* Input select                 */
114 #define TDA7432_VL      0x01 /* Volume                       */
115 #define TDA7432_TN      0x02 /* Bass, Treble (Tone)          */
116 #define TDA7432_LF      0x03 /* Attenuation LF (Left Front)  */
117 #define TDA7432_LR      0x04 /* Attenuation LR (Left Rear)   */
118 #define TDA7432_RF      0x05 /* Attenuation RF (Right Front) */
119 #define TDA7432_RR      0x06 /* Attenuation RR (Right Rear)  */
120 #define TDA7432_LD      0x07 /* Loudness                     */
121
122
123                 /* Masks for bits in TDA7432 subaddresses */
124
125 /* Many of these not used - just for documentation */
126
127 /* Subaddress 0x00 - Input selection and bass control */
128
129 /* Bits 0,1,2 control input:
130  * 0x00 - Stereo input
131  * 0x02 - Mono input
132  * 0x03 - Mute  (Using Attenuators Plays better with modules)
133  * Mono probably isn't used - I'm guessing only the stereo
134  * input is connected on most cards, so we'll set it to stereo.
135  *
136  * Bit 3 controls bass cut: 0/1 is non-symmetric/symmetric bass cut
137  * Bit 4 controls bass range: 0/1 is extended/standard bass range
138  *
139  * Highest 3 bits not used
140  */
141
142 #define TDA7432_STEREO_IN       0
143 #define TDA7432_MONO_IN         2       /* Probably won't be used */
144 #define TDA7432_BASS_SYM        1 << 3
145 #define TDA7432_BASS_NORM       1 << 4
146
147 /* Subaddress 0x01 - Volume */
148
149 /* Lower 7 bits control volume from -79dB to +32dB in 1dB steps
150  * Recommended maximum is +20 dB
151  *
152  * +32dB: 0x00
153  * +20dB: 0x0c
154  *   0dB: 0x20
155  * -79dB: 0x6f
156  *
157  * MSB (bit 7) controls loudness: 1/0 is loudness on/off
158  */
159
160 #define TDA7432_VOL_0DB         0x20
161 #define TDA7432_LD_ON           1 << 7
162
163
164 /* Subaddress 0x02 - Tone control */
165
166 /* Bits 0,1,2 control absolute treble gain from 0dB to 14dB
167  * 0x0 is 14dB, 0x7 is 0dB
168  *
169  * Bit 3 controls treble attenuation/gain (sign)
170  * 1 = gain (+)
171  * 0 = attenuation (-)
172  *
173  * Bits 4,5,6 control absolute bass gain from 0dB to 14dB
174  * (This is only true for normal base range, set in 0x00)
175  * 0x0 << 4 is 14dB, 0x7 is 0dB
176  *
177  * Bit 7 controls bass attenuation/gain (sign)
178  * 1 << 7 = gain (+)
179  * 0 << 7 = attenuation (-)
180  *
181  * Example:
182  * 1 1 0 1 0 1 0 1 is +4dB bass, -4dB treble
183  */
184
185 #define TDA7432_TREBLE_0DB              0xf
186 #define TDA7432_TREBLE                  7
187 #define TDA7432_TREBLE_GAIN             1 << 3
188 #define TDA7432_BASS_0DB                0xf
189 #define TDA7432_BASS                    7 << 4
190 #define TDA7432_BASS_GAIN               1 << 7
191
192
193 /* Subaddress 0x03 - Left  Front attenuation */
194 /* Subaddress 0x04 - Left  Rear  attenuation */
195 /* Subaddress 0x05 - Right Front attenuation */
196 /* Subaddress 0x06 - Right Rear  attenuation */
197
198 /* Bits 0,1,2,3,4 control attenuation from 0dB to -37.5dB
199  * in 1.5dB steps.
200  *
201  * 0x00 is     0dB
202  * 0x1f is -37.5dB
203  *
204  * Bit 5 mutes that channel when set (1 = mute, 0 = unmute)
205  * We'll use the mute on the input, though (above)
206  * Bits 6,7 unused
207  */
208
209 #define TDA7432_ATTEN_0DB       0x00
210 #define TDA7432_MUTE        0x1 << 5
211
212
213 /* Subaddress 0x07 - Loudness Control */
214
215 /* Bits 0,1,2,3 control loudness from 0dB to -15dB in 1dB steps
216  * when bit 4 is NOT set
217  *
218  * 0x0 is   0dB
219  * 0xf is -15dB
220  *
221  * If bit 4 is set, then there is a flat attenuation according to
222  * the lower 4 bits, as above.
223  *
224  * Bits 5,6,7 unused
225  */
226
227
228
229 /* Begin code */
230
231 static int tda7432_write(struct v4l2_subdev *sd, int subaddr, int val)
232 {
233         struct i2c_client *client = v4l2_get_subdevdata(sd);
234         unsigned char buffer[2];
235
236         v4l2_dbg(2, debug, sd, "In tda7432_write\n");
237         v4l2_dbg(1, debug, sd, "Writing %d 0x%x\n", subaddr, val);
238         buffer[0] = subaddr;
239         buffer[1] = val;
240         if (2 != i2c_master_send(client, buffer, 2)) {
241                 v4l2_err(sd, "I/O error, trying (write %d 0x%x)\n",
242                        subaddr, val);
243                 return -1;
244         }
245         return 0;
246 }
247
248 static int tda7432_set(struct v4l2_subdev *sd)
249 {
250         struct i2c_client *client = v4l2_get_subdevdata(sd);
251         struct tda7432 *t = to_state(sd);
252         unsigned char buf[16];
253
254         v4l2_dbg(1, debug, sd,
255                 "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
256                 t->input, t->volume, t->bass, t->treble, t->lf, t->lr,
257                 t->rf, t->rr, t->loud);
258         buf[0]  = TDA7432_IN;
259         buf[1]  = t->input;
260         buf[2]  = t->volume;
261         buf[3]  = t->bass;
262         buf[4]  = t->treble;
263         buf[5]  = t->lf;
264         buf[6]  = t->lr;
265         buf[7]  = t->rf;
266         buf[8]  = t->rr;
267         buf[9]  = t->loud;
268         if (10 != i2c_master_send(client, buf, 10)) {
269                 v4l2_err(sd, "I/O error, trying tda7432_set\n");
270                 return -1;
271         }
272
273         return 0;
274 }
275
276 static void do_tda7432_init(struct v4l2_subdev *sd)
277 {
278         struct tda7432 *t = to_state(sd);
279
280         v4l2_dbg(2, debug, sd, "In tda7432_init\n");
281
282         t->input  = TDA7432_STEREO_IN |  /* Main (stereo) input   */
283                     TDA7432_BASS_SYM  |  /* Symmetric bass cut    */
284                     TDA7432_BASS_NORM;   /* Normal bass range     */
285         t->volume =  0x3b ;                              /* -27dB Volume            */
286         if (loudness)                    /* Turn loudness on?     */
287                 t->volume |= TDA7432_LD_ON;
288         t->muted    = 1;
289         t->treble   = TDA7432_TREBLE_0DB; /* 0dB Treble            */
290         t->bass         = TDA7432_BASS_0DB;      /* 0dB Bass              */
291         t->lf     = TDA7432_ATTEN_0DB;   /* 0dB attenuation       */
292         t->lr     = TDA7432_ATTEN_0DB;   /* 0dB attenuation       */
293         t->rf     = TDA7432_ATTEN_0DB;   /* 0dB attenuation       */
294         t->rr     = TDA7432_ATTEN_0DB;   /* 0dB attenuation       */
295         t->loud   = loudness;            /* insmod parameter      */
296
297         tda7432_set(sd);
298 }
299
300 static int tda7432_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
301 {
302         struct tda7432 *t = to_state(sd);
303
304         switch (ctrl->id) {
305         case V4L2_CID_AUDIO_MUTE:
306                 ctrl->value=t->muted;
307                 return 0;
308         case V4L2_CID_AUDIO_VOLUME:
309                 if (!maxvol){  /* max +20db */
310                         ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 630;
311                 } else {       /* max 0db   */
312                         ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 829;
313                 }
314                 return 0;
315         case V4L2_CID_AUDIO_BALANCE:
316         {
317                 if ( (t->lf) < (t->rf) )
318                         /* right is attenuated, balance shifted left */
319                         ctrl->value = (32768 - 1057*(t->rf));
320                 else
321                         /* left is attenuated, balance shifted right */
322                         ctrl->value = (32768 + 1057*(t->lf));
323                 return 0;
324         }
325         case V4L2_CID_AUDIO_BASS:
326         {
327                 /* Bass/treble 4 bits each */
328                 int bass=t->bass;
329                 if(bass >= 0x8)
330                         bass = ~(bass - 0x8) & 0xf;
331                 ctrl->value = (bass << 12)+(bass << 8)+(bass << 4)+(bass);
332                 return 0;
333         }
334         case V4L2_CID_AUDIO_TREBLE:
335         {
336                 int treble=t->treble;
337                 if(treble >= 0x8)
338                         treble = ~(treble - 0x8) & 0xf;
339                 ctrl->value = (treble << 12)+(treble << 8)+(treble << 4)+(treble);
340                 return 0;
341         }
342         }
343         return -EINVAL;
344 }
345
346 static int tda7432_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
347 {
348         struct tda7432 *t = to_state(sd);
349
350         switch (ctrl->id) {
351         case V4L2_CID_AUDIO_MUTE:
352                 t->muted=ctrl->value;
353                 break;
354         case V4L2_CID_AUDIO_VOLUME:
355                 if(!maxvol){ /* max +20db */
356                         t->volume = 0x6f - ((ctrl->value)/630);
357                 } else {    /* max 0db   */
358                         t->volume = 0x6f - ((ctrl->value)/829);
359                 }
360                 if (loudness)           /* Turn on the loudness bit */
361                         t->volume |= TDA7432_LD_ON;
362
363                 tda7432_write(sd, TDA7432_VL, t->volume);
364                 return 0;
365         case V4L2_CID_AUDIO_BALANCE:
366                 if (ctrl->value < 32768) {
367                         /* shifted to left, attenuate right */
368                         t->rr = (32768 - ctrl->value)/1057;
369                         t->rf = t->rr;
370                         t->lr = TDA7432_ATTEN_0DB;
371                         t->lf = TDA7432_ATTEN_0DB;
372                 } else if(ctrl->value > 32769) {
373                         /* shifted to right, attenuate left */
374                         t->lf = (ctrl->value - 32768)/1057;
375                         t->lr = t->lf;
376                         t->rr = TDA7432_ATTEN_0DB;
377                         t->rf = TDA7432_ATTEN_0DB;
378                 } else {
379                         /* centered */
380                         t->rr = TDA7432_ATTEN_0DB;
381                         t->rf = TDA7432_ATTEN_0DB;
382                         t->lf = TDA7432_ATTEN_0DB;
383                         t->lr = TDA7432_ATTEN_0DB;
384                 }
385                 break;
386         case V4L2_CID_AUDIO_BASS:
387                 t->bass = ctrl->value >> 12;
388                 if(t->bass>= 0x8)
389                                 t->bass = (~t->bass & 0xf) + 0x8 ;
390
391                 tda7432_write(sd, TDA7432_TN, 0x10 | (t->bass << 4) | t->treble);
392                 return 0;
393         case V4L2_CID_AUDIO_TREBLE:
394                 t->treble= ctrl->value >> 12;
395                 if(t->treble>= 0x8)
396                                 t->treble = (~t->treble & 0xf) + 0x8 ;
397
398                 tda7432_write(sd, TDA7432_TN, 0x10 | (t->bass << 4) | t->treble);
399                 return 0;
400         default:
401                 return -EINVAL;
402         }
403
404         /* Used for both mute and balance changes */
405         if (t->muted)
406         {
407                 /* Mute & update balance*/
408                 tda7432_write(sd, TDA7432_LF, t->lf | TDA7432_MUTE);
409                 tda7432_write(sd, TDA7432_LR, t->lr | TDA7432_MUTE);
410                 tda7432_write(sd, TDA7432_RF, t->rf | TDA7432_MUTE);
411                 tda7432_write(sd, TDA7432_RR, t->rr | TDA7432_MUTE);
412         } else {
413                 tda7432_write(sd, TDA7432_LF, t->lf);
414                 tda7432_write(sd, TDA7432_LR, t->lr);
415                 tda7432_write(sd, TDA7432_RF, t->rf);
416                 tda7432_write(sd, TDA7432_RR, t->rr);
417         }
418         return 0;
419 }
420
421 static int tda7432_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
422 {
423         switch (qc->id) {
424         case V4L2_CID_AUDIO_MUTE:
425         case V4L2_CID_AUDIO_VOLUME:
426         case V4L2_CID_AUDIO_BALANCE:
427         case V4L2_CID_AUDIO_BASS:
428         case V4L2_CID_AUDIO_TREBLE:
429                 return v4l2_ctrl_query_fill_std(qc);
430         }
431         return -EINVAL;
432 }
433
434 static int tda7432_command(struct i2c_client *client, unsigned cmd, void *arg)
435 {
436         return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
437 }
438
439 /* ----------------------------------------------------------------------- */
440
441 static const struct v4l2_subdev_core_ops tda7432_core_ops = {
442         .queryctrl = tda7432_queryctrl,
443         .g_ctrl = tda7432_g_ctrl,
444         .s_ctrl = tda7432_s_ctrl,
445 };
446
447 static const struct v4l2_subdev_ops tda7432_ops = {
448         .core = &tda7432_core_ops,
449 };
450
451 /* ----------------------------------------------------------------------- */
452
453 /* *********************** *
454  * i2c interface functions *
455  * *********************** */
456
457 static int tda7432_probe(struct i2c_client *client,
458                         const struct i2c_device_id *id)
459 {
460         struct tda7432 *t;
461         struct v4l2_subdev *sd;
462
463         v4l_info(client, "chip found @ 0x%02x (%s)\n",
464                         client->addr << 1, client->adapter->name);
465
466         t = kzalloc(sizeof(*t), GFP_KERNEL);
467         if (!t)
468                 return -ENOMEM;
469         sd = &t->sd;
470         v4l2_i2c_subdev_init(sd, client, &tda7432_ops);
471         if (loudness < 0 || loudness > 15) {
472                 v4l2_warn(sd, "loudness parameter must be between 0 and 15\n");
473                 if (loudness < 0)
474                         loudness = 0;
475                 if (loudness > 15)
476                         loudness = 15;
477         }
478
479         do_tda7432_init(sd);
480         return 0;
481 }
482
483 static int tda7432_remove(struct i2c_client *client)
484 {
485         struct v4l2_subdev *sd = i2c_get_clientdata(client);
486
487         do_tda7432_init(sd);
488         v4l2_device_unregister_subdev(sd);
489         kfree(to_state(sd));
490         return 0;
491 }
492
493 static const struct i2c_device_id tda7432_id[] = {
494         { "tda7432", 0 },
495         { }
496 };
497 MODULE_DEVICE_TABLE(i2c, tda7432_id);
498
499 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
500         .name = "tda7432",
501         .driverid = I2C_DRIVERID_TDA7432,
502         .command = tda7432_command,
503         .probe = tda7432_probe,
504         .remove = tda7432_remove,
505         .id_table = tda7432_id,
506 };