Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / tda985x.c
blob73fb9bd522011eca5e22e117cd97bf0698e4e280
1 /*
2 * For the TDA9850 and TDA9855 chips
3 * (The TDA9855 is used on the Diamond DTV2000 and the TDA9850 is used
4 * on STB cards. Other cards probably use these chips as well.)
5 * This driver will not complain if used with any
6 * other i2c device with the same address.
8 * Copyright (c) 1999 Gerd Knorr
9 * TDA9850 code and TDA9855.c merger by Eric Sandeen (eric_sandeen@bigfoot.com)
10 * This code is placed under the terms of the GNU General Public License
11 * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
12 * Which was based on tda8425.c by Greg Alexander (c) 1998
14 * OPTIONS:
15 * debug - set to 1 if you'd like to see debug messages
16 * - set to 2 if you'd like to be flooded with debug messages
17 * chip - set to 9850 or 9855 to select your chip (default 9855)
19 * TODO:
20 * Fix channel change bug - sound goes out when changeing channels, mute
21 * and unmote to fix. - Is this still here?
22 * Fine tune sound
23 * Get rest of capabilities into video_audio struct...
25 * Revision 0.5 - cleaned up debugging messages, added debug level=2
26 * Revision: 0.4 - check for correct chip= insmod value
27 * also cleaned up comments a bit
28 * Revision: 0.3 - took out extraneous tda985x_write in tda985x_command
29 * Revision: 0.2 - added insmod option chip=
30 * Revision: 0.1 - original version
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/sched.h>
36 #include <linux/string.h>
37 #include <linux/timer.h>
38 #include <linux/delay.h>
39 #include <linux/errno.h>
40 #include <linux/malloc.h>
41 #include <linux/videodev.h>
42 #include <linux/i2c.h>
43 #include <linux/i2c-algo-bit.h>
45 #include "bttv.h"
46 #include "audiochip.h"
48 MODULE_PARM(debug,"i");
49 MODULE_PARM(chip,"i");
50 MODULE_PARM_DESC(chip, "Type of chip to handle: 9850 or 9855");
52 static int debug = 0; /* insmod parameter */
53 static int chip = 9855; /* insmod parameter */
55 /* Addresses to scan */
56 #define I2C_TDA985x_L 0xb4
57 #define I2C_TDA985x_H 0xb6
58 static unsigned short normal_i2c[] = {I2C_CLIENT_END};
59 static unsigned short normal_i2c_range[] = {
60 I2C_TDA985x_L >> 1,
61 I2C_TDA985x_H >> 1,
62 I2C_CLIENT_END
64 static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
65 static unsigned short probe_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
66 static unsigned short ignore[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
67 static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
68 static unsigned short force[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
69 static struct i2c_client_address_data addr_data = {
70 normal_i2c, normal_i2c_range,
71 probe, probe_range,
72 ignore, ignore_range,
73 force
76 /* This is a superset of the TDA9850 and TDA9855 members */
78 struct tda985x {
79 int addr;
80 int rvol, lvol;
81 int bass, treble, sub;
82 int c4, c5, c6, c7;
83 int a1, a2, a3;
86 static struct i2c_driver driver;
87 static struct i2c_client client_template;
89 #define dprintk if (debug) printk
90 #define d2printk if (debug == 2) printk
92 /* The TDA9850 and TDA9855 are both made by Philips Semiconductor
93 * http://www.semiconductors.philips.com
94 * TDA9850: I2C-bus controlled BTSC stereo/SAP decoder
95 * TDA9855: I2C-bus controlled BTSC stereo/SAP decoder and audio processor
97 * The TDA9850 has more or less a subset of the functions that the TDA9855
98 * has. As a result, we can re-use many of these defines. Anything with
99 * TDA9855 is specific to that chip, anything with TDA9850 is specific
100 * to that chip, and anything with TDA985x is valid for either.
102 * To complicate things further, the TDA9850 uses labels C1 through C4
103 * for subaddresses 0x04 through 0x07, while the TDA9855 uses
104 * C1 through C3 for subadresses 0x05 through 0x07 - quite confusing.
105 * To help keep things straight, I have renamed the various C[1,4] labels
106 * to C[4,7] so that the numerical label matches the hex value of the
107 * subaddress for both chips. At least the A[1,3] labels line up. :)
110 /* subaddresses for TDA9855 */
111 #define TDA9855_VR 0x00 /* Volume, right */
112 #define TDA9855_VL 0x01 /* Volume, left */
113 #define TDA9855_BA 0x02 /* Bass */
114 #define TDA9855_TR 0x03 /* Treble */
115 #define TDA9855_SW 0x04 /* Subwoofer - not connected on DTV2000 */
117 /* subaddresses for TDA9850 */
118 #define TDA9850_C4 0x04 /* Control 1 for TDA9850 */
120 /* subaddesses for both chips */
121 #define TDA985x_C5 0x05 /* Control 2 for TDA9850, Control 1 for TDA9855 */
122 #define TDA985x_C6 0x06 /* Control 3 for TDA9850, Control 2 for TDA9855 */
123 #define TDA985x_C7 0x07 /* Control 4 for TDA9850, Control 3 for TDA9855 */
124 #define TDA985x_A1 0x08 /* Alignment 1 for both chips */
125 #define TDA985x_A2 0x09 /* Alignment 2 for both chips */
126 #define TDA985x_A3 0x0a /* Alignment 3 for both chips */
128 /* Masks for bits in TDA9855 subaddresses */
129 /* 0x00 - VR in TDA9855 */
130 /* 0x01 - VL in TDA9855 */
131 /* lower 7 bits control gain from -71dB (0x28) to 16dB (0x7f)
132 * in 1dB steps - mute is 0x27 */
135 /* 0x02 - BA in TDA9855 */
136 /* lower 5 bits control bass gain from -12dB (0x06) to 16.5dB (0x19)
137 * in .5dB steps - 0 is 0x0E */
140 /* 0x03 - TR in TDA9855 */
141 /* 4 bits << 1 control treble gain from -12dB (0x3) to 12dB (0xb)
142 * in 3dB steps - 0 is 0x7 */
144 /* Masks for bits in both chips' subaddresses */
145 /* 0x04 - SW in TDA9855, C4/Control 1 in TDA9850 */
146 /* Unique to TDA9855: */
147 /* 4 bits << 2 control subwoofer/surround gain from -14db (0x1) to 14db (0xf)
148 * in 3dB steps - mute is 0x0 */
150 /* Unique to TDA9850: */
151 /* lower 4 bits control stereo noise threshold, over which stereo turns off
152 * set to values of 0x00 through 0x0f for Ster1 through Ster16 */
155 /* 0x05 - C5 - Control 1 in TDA9855 , Control 2 in TDA9850*/
156 /* Unique to TDA9855: */
157 #define TDA9855_MUTE 1<<7 /* GMU, Mute at outputs */
158 #define TDA9855_AVL 1<<6 /* AVL, Automatic Volume Level */
159 #define TDA9855_LOUD 1<<5 /* Loudness, 1==off */
160 #define TDA9855_SUR 1<<3 /* Surround / Subwoofer 1==.5(L-R) 0==.5(L+R) */
161 /* Bits 0 to 3 select various combinations
162 * of line in and line out, only the
163 * interesting ones are defined */
164 #define TDA9855_EXT 1<<2 /* Selects inputs LIR and LIL. Pins 41 & 12 */
165 #define TDA9855_INT 0 /* Selects inputs LOR and LOL. (internal) */
167 /* Unique to TDA9850: */
168 /* lower 4 bits contol SAP noise threshold, over which SAP turns off
169 * set to values of 0x00 through 0x0f for SAP1 through SAP16 */
172 /* 0x06 - C6 - Control 2 in TDA9855, Control 3 in TDA9850 */
173 /* Common to TDA9855 and TDA9850: */
174 #define TDA985x_SAP 3<<6 /* Selects SAP output, mute if not received */
175 #define TDA985x_STEREO 1<<6 /* Selects Stereo ouput, mono if not received */
176 #define TDA985x_MONO 0 /* Forces Mono output */
177 #define TDA985x_LMU 1<<3 /* Mute (LOR/LOL for 9855, OUTL/OUTR for 9850) */
179 /* Unique to TDA9855: */
180 #define TDA9855_TZCM 1<<5 /* If set, don't mute till zero crossing */
181 #define TDA9855_VZCM 1<<4 /* If set, don't change volume till zero crossing*/
182 #define TDA9855_LINEAR 0 /* Linear Stereo */
183 #define TDA9855_PSEUDO 1 /* Pseudo Stereo */
184 #define TDA9855_SPAT_30 2 /* Spatial Stereo, 30% anti-phase crosstalk */
185 #define TDA9855_SPAT_50 3 /* Spatial Stereo, 52% anti-phase crosstalk */
186 #define TDA9855_E_MONO 7 /* Forced mono - mono select elseware, so useless*/
189 /* 0x07 - C7 - Control 3 in TDA9855, Control 4 in TDA9850 */
190 /* Common to both TDA9855 and TDA9850: */
191 /* lower 4 bits control input gain from -3.5dB (0x0) to 4dB (0xF)
192 * in .5dB steps - 0dB is 0x7 */
195 /* 0x08, 0x09 - A1 and A2 (read/write) */
196 /* Common to both TDA9855 and TDA9850: */
197 /* lower 5 bites are wideband and spectral expander alignment
198 * from 0x00 to 0x1f - nominal at 0x0f and 0x10 (read/write) */
199 #define TDA985x_STP 1<<5 /* Stereo Pilot/detect (read-only) */
200 #define TDA985x_SAPP 1<<6 /* SAP Pilot/detect (read-only) */
201 #define TDA985x_STS 1<<7 /* Stereo trigger 1= <35mV 0= <30mV (write-only)*/
204 /* 0x0a - A3 */
205 /* Common to both TDA9855 and TDA9850: */
206 /* lower 3 bits control timing current for alignment: -30% (0x0), -20% (0x1),
207 * -10% (0x2), nominal (0x3), +10% (0x6), +20% (0x5), +30% (0x4) */
208 #define TDA985x_ADJ 1<<7 /* Stereo adjust on/off (wideband and spectral */
210 /* Unique to TDA9855: */
211 /* 2 bits << 5 control AVL attack time: 420ohm (0x0), 730ohm (0x2),
212 * 1200ohm (0x1), 2100ohm (0x3) */
215 /* Begin code */
217 static int tda985x_write(struct i2c_client *client, int subaddr, int val)
219 unsigned char buffer[2];
220 d2printk("tda985x: In tda985x_write\n");
221 dprintk("tda985x: Writing %d 0x%x\n", subaddr, val);
222 buffer[0] = subaddr;
223 buffer[1] = val;
224 if (2 != i2c_master_send(client,buffer,2)) {
225 printk(KERN_WARNING "tda985x: I/O error, trying (write %d 0x%x)\n",
226 subaddr, val);
227 return -1;
229 return 0;
232 static int tda985x_read(struct i2c_client *client)
234 unsigned char buffer;
235 d2printk("tda985x: In tda985x_read\n");
236 if (1 != i2c_master_recv(client,&buffer,1)) {
237 printk(KERN_WARNING "tda985x: I/O error, trying (read)\n");
238 return -1;
240 dprintk("tda985x: Read 0x%02x\n", buffer);
241 return buffer;
244 static int tda985x_set(struct i2c_client *client)
246 struct tda985x *t = client->data;
247 unsigned char buf[16];
248 d2printk("tda985x: In tda985x_set\n");
250 if (chip == 9855)
252 dprintk(KERN_INFO
253 "tda985x: tda985x_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
254 t->rvol,t->lvol,t->bass,t->treble,t->sub,
255 t->c5,t->c6,t->c7,t->a1,t->a2,t->a3);
256 buf[0] = TDA9855_VR;
257 buf[1] = t->rvol;
258 buf[2] = t->lvol;
259 buf[3] = t->bass;
260 buf[4] = t->treble;
261 buf[5] = t->sub;
262 buf[6] = t->c5;
263 buf[7] = t->c6;
264 buf[8] = t->c7;
265 buf[9] = t->a1;
266 buf[10] = t->a2;
267 buf[11] = t->a3;
268 if (12 != i2c_master_send(client,buf,12)) {
269 printk(KERN_WARNING "tda985x: I/O error, trying tda985x_set\n");
270 return -1;
274 else if (chip == 9850)
276 dprintk(KERN_INFO
277 "tda986x: tda985x_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
278 t->c4,t->c5,t->c6,t->c7,t->a1,t->a2,t->a3);
279 buf[0] = TDA9850_C4;
280 buf[1] = t->c4;
281 buf[2] = t->c5;
282 buf[3] = t->c6;
283 buf[4] = t->c7;
284 buf[5] = t->a1;
285 buf[6] = t->a2;
286 buf[7] = t->a3;
287 if (8 != i2c_master_send(client,buf,8)) {
288 printk(KERN_WARNING "tda985x: I/O error, trying tda985x_set\n");
289 return -1;
293 return 0;
296 static void do_tda985x_init(struct i2c_client *client)
298 struct tda985x *t = client->data;
299 d2printk("tda985x: In tda985x_init\n");
301 if (chip == 9855)
303 printk("tda985x: Using tda9855 options\n");
304 t->rvol = 0x6f; /* 0dB */
305 t->lvol = 0x6f; /* 0dB */
306 t->bass = 0x0e; /* 0dB */
307 t->treble = (0x07 << 1); /* 0dB */
308 t->sub = 0x8 << 2; /* 0dB */
309 t->c5 = TDA9855_MUTE | TDA9855_AVL |
310 TDA9855_LOUD | TDA9855_INT;
311 /* Set Mute, AVL, Loudness off, Internal sound */
312 t->c6 = TDA985x_STEREO | TDA9855_LINEAR |
313 TDA9855_TZCM | TDA9855_VZCM;
314 /* Stereo linear mode, also wait til zero crossings */
315 t->c7 = 0x07; /* 0dB input gain */
318 else if (chip == 9850)
320 printk("tda985x: Using tda9850 options\n");
321 t->c4 = 0x08; /* Set stereo noise thresh to nominal */
322 t->c5 = 0x08; /* Set SAP noise threshold to nominal */
323 t->c6 = TDA985x_STEREO; /* Select Stereo mode for decoder */
324 t->c7 = 0x07; /* 0dB input gain */
327 /* The following is valid for both chip types */
328 t->a1 = 0x10; /* Select nominal wideband expander */
329 t->a2 = 0x10; /* Select nominal spectral expander and 30mV trigger */
330 t->a3 = 0x3; /* Set: nominal timing current, 420ohm AVL attack */
332 tda985x_set(client);
335 /* *********************** *
336 * i2c interface functions *
337 * *********************** */
339 static int tda985x_attach(struct i2c_adapter *adap, int addr,
340 unsigned short flags, int kind)
342 struct tda985x *t;
343 struct i2c_client *client;
344 d2printk("tda985x: In tda985x_attach\n");
345 client = kmalloc(sizeof *client,GFP_KERNEL);
346 if (!client)
347 return -ENOMEM;
348 memcpy(client,&client_template,sizeof(struct i2c_client));
349 client->adapter = adap;
350 client->addr = addr;
352 client->data = t = kmalloc(sizeof *t,GFP_KERNEL);
353 if (!t)
354 return -ENOMEM;
355 memset(t,0,sizeof *t);
356 do_tda985x_init(client);
357 MOD_INC_USE_COUNT;
358 strcpy(client->name,"TDA985x");
359 printk(KERN_INFO "tda985x: init\n");
361 i2c_attach_client(client);
362 return 0;
365 static int tda985x_probe(struct i2c_adapter *adap)
367 if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848))
368 return i2c_probe(adap, &addr_data, tda985x_attach);
369 return 0;
372 static int tda985x_detach(struct i2c_client *client)
374 struct tda985x *t = client->data;
376 do_tda985x_init(client);
377 i2c_detach_client(client);
379 kfree(t);
380 kfree(client);
381 MOD_DEC_USE_COUNT;
382 return 0;
385 static int tda985x_command(struct i2c_client *client,
386 unsigned int cmd, void *arg)
388 struct tda985x *t = client->data;
389 d2printk("tda985x: In tda985x_command\n");
390 #if 0
391 __u16 *sarg = arg;
392 #endif
394 switch (cmd) {
395 /* --- v4l ioctls --- */
396 /* take care: bttv does userspace copying, we'll get a
397 kernel pointer here... */
398 case VIDIOCGAUDIO:
400 struct video_audio *va = arg;
401 dprintk("tda985x: VIDIOCGAUDIO\n");
402 if (chip == 9855)
404 int left,right;
406 va->flags |= VIDEO_AUDIO_VOLUME |
407 VIDEO_AUDIO_BASS |
408 VIDEO_AUDIO_TREBLE;
410 /* min is 0x27 max is 0x7f, vstep is 2e8 */
411 left = (t->lvol-0x27)*0x2e8;
412 right = (t->rvol-0x27)*0x2e8;
413 va->volume=MAX(left,right);
414 va->balance=(32768*MIN(left,right))/
415 (va->volume ? va->volume : 1);
416 va->balance=(left<right)?
417 (65535-va->balance) : va->balance;
418 va->bass = (t->bass-0x6)*0xccc; /* min 0x6 max 0x19 */
419 va->treble = ((t->treble>>1)-0x3)*0x1c71;
422 /* Valid for both chips: */
424 va->mode = ((TDA985x_STP | TDA985x_SAPP) &
425 tda985x_read(client)) >> 4;
426 /* Add mono mode regardless of SAP and stereo */
427 /* Allows forced mono */
428 va->mode |= VIDEO_SOUND_MONO;
431 break; /* VIDIOCGAUDIO case */
434 case VIDIOCSAUDIO:
436 struct video_audio *va = arg;
437 dprintk("tda985x: VIDEOCSAUDIO\n");
438 if (chip == 9855)
440 int left,right;
442 left = (MIN(65536 - va->balance,32768) *
443 va->volume) / 32768;
444 right = (MIN(va->balance,32768) *
445 va->volume) / 32768;
446 t->lvol = left/0x2e8+0x27;
447 t->rvol = right/0x2e8+0x27;
448 t->bass = va->bass/0xccc+0x6;
449 t->treble = (va->treble/0x1c71+0x3)<<1;
450 tda985x_write(client,TDA9855_VL,t->lvol);
451 tda985x_write(client,TDA9855_VR,t->rvol);
452 tda985x_write(client,TDA9855_BA, t->bass);
453 tda985x_write(client,TDA9855_TR,t->treble);
456 /* The following is valid for both chips */
458 switch (va->mode) {
459 case VIDEO_SOUND_MONO:
460 dprintk("tda985x: VIDEO_SOUND_MONO\n");
461 t->c6= TDA985x_MONO | (t->c6 & 0x3f);
462 tda985x_write(client,TDA985x_C6,t->c6);
463 break;
464 case VIDEO_SOUND_STEREO:
465 dprintk("tda985x: VIDEO_SOUND_STEREO\n");
466 t->c6= TDA985x_STEREO | (t->c6 & 0x3f);
467 tda985x_write(client,TDA985x_C6,t->c6);
468 break;
469 case VIDEO_SOUND_LANG1:
470 dprintk("tda985x: VIDEO_SOUND_LANG1\n");
471 t->c6= TDA985x_SAP | (t->c6 & 0x3f);
472 tda985x_write(client,TDA985x_C6,t->c6);
473 break;
474 } /* End of (va->mode) switch */
476 break;
478 } /* end of VIDEOCSAUDIO case */
480 default: /* Not VIDEOCGAUDIO or VIDEOCSAUDIO */
482 /* nothing */
483 d2printk("tda985x: Default\n");
485 } /* end of (cmd) switch */
487 return 0;
491 static struct i2c_driver driver = {
492 "i2c tda985x driver",
493 I2C_DRIVERID_TDA9855, /* Get new one for TDA985x? */
494 I2C_DF_NOTIFY,
495 tda985x_probe,
496 tda985x_detach,
497 tda985x_command,
500 static struct i2c_client client_template =
502 "(unset)", /* name */
506 NULL,
507 &driver
510 #ifdef MODULE
511 int init_module(void)
512 #else
513 int tda985x_init(void)
514 #endif
516 if ( (chip != 9850) && (chip != 9855) )
518 printk(KERN_ERR "tda985x: chip parameter must be 9850 or 9855\n");
519 return -EINVAL;
521 i2c_add_driver(&driver);
522 return 0;
525 #ifdef MODULE
526 void cleanup_module(void)
528 i2c_del_driver(&driver);
530 #endif
533 * Local variables:
534 * c-basic-offset: 8
535 * End: