import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / media / video / tda9875.c
1 /*
2  * For the TDA9875 chip
3  * (The TDA9875 is used on the Diamond DTV2000 french version 
4  * 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.
7  *
8  * Copyright (c) 2000 Guillaume Delvit based on Gerd Knorr source and
9  * Eric Sandeen 
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
13  *
14  * OPTIONS:
15  * debug   - set to 1 if you'd like to see debug messages
16  * 
17  *  Revision: 0.1 - original version
18  */
19
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/string.h>
24 #include <linux/timer.h>
25 #include <linux/delay.h>
26 #include <linux/errno.h>
27 #include <linux/slab.h>
28 #include <linux/videodev.h>
29 #include <linux/i2c.h>
30 #include <linux/i2c-algo-bit.h>
31 #include <linux/init.h>
32
33 #include "bttv.h"
34 #include "audiochip.h"
35 #include "id.h"
36
37 MODULE_PARM(debug,"i");
38 MODULE_LICENSE("GPL");
39
40 static int debug = 0;   /* insmod parameter */
41
42 /* Addresses to scan */
43 static unsigned short normal_i2c[] =  {
44     I2C_TDA9875 >> 1,
45     I2C_CLIENT_END
46 };
47 static unsigned short normal_i2c_range[] = {I2C_CLIENT_END};
48 I2C_CLIENT_INSMOD;
49
50 /* This is a superset of the TDA9875 */
51 struct tda9875 {
52         int mode;
53         int rvol, lvol;
54         int bass, treble;
55         struct i2c_client c;
56 };
57
58 static struct i2c_driver driver;
59 static struct i2c_client client_template;
60
61 #define dprintk  if (debug) printk
62
63 /* The TDA9875 is made by Philips Semiconductor
64  * http://www.semiconductors.philips.com
65  * TDA9875: I2C-bus controlled DSP audio processor, FM demodulator
66  *
67  */ 
68
69                 /* subaddresses for TDA9875 */
70 #define TDA9875_MUT         0x12  /*General mute  (value --> 0b11001100*/
71 #define TDA9875_CFG         0x01  /* Config register (value --> 0b00000000 */
72 #define TDA9875_DACOS       0x13  /*DAC i/o select (ADC) 0b0000100*/
73 #define TDA9875_LOSR        0x16  /*Line output select regirter 0b0100 0001*/
74
75 #define TDA9875_CH1V        0x0c  /*Channel 1 volume (mute)*/
76 #define TDA9875_CH2V        0x0d  /*Channel 2 volume (mute)*/
77 #define TDA9875_SC1         0x14  /*SCART 1 in (mono)*/
78 #define TDA9875_SC2         0x15  /*SCART 2 in (mono)*/
79
80 #define TDA9875_ADCIS       0x17  /*ADC input select (mono) 0b0110 000*/
81 #define TDA9875_AER         0x19  /*Audio effect (AVL+Pseudo) 0b0000 0110*/
82 #define TDA9875_MCS         0x18  /*Main channel select (DAC) 0b0000100*/
83 #define TDA9875_MVL         0x1a  /* Main volume gauche */
84 #define TDA9875_MVR         0x1b  /* Main volume droite */
85 #define TDA9875_MBA         0x1d  /* Main Basse */
86 #define TDA9875_MTR         0x1e  /* Main treble */
87 #define TDA9875_ACS         0x1f  /* Auxilary channel select (FM) 0b0000000*/
88 #define TDA9875_AVL         0x20  /* Auxilary volume gauche */
89 #define TDA9875_AVR         0x21  /* Auxilary volume droite */
90 #define TDA9875_ABA         0x22  /* Auxilary Basse */
91 #define TDA9875_ATR         0x23  /* Auxilary treble */
92
93 #define TDA9875_MSR         0x02  /* Monitor select register */
94 #define TDA9875_C1MSB       0x03  /* Carrier 1 (FM) frequency register MSB */
95 #define TDA9875_C1MIB       0x04  /* Carrier 1 (FM) frequency register (16-8]b */
96 #define TDA9875_C1LSB       0x05  /* Carrier 1 (FM) frequency register LSB */
97 #define TDA9875_C2MSB       0x06  /* Carrier 2 (nicam) frequency register MSB */
98 #define TDA9875_C2MIB       0x07  /* Carrier 2 (nicam) frequency register (16-8]b */
99 #define TDA9875_C2LSB       0x08  /* Carrier 2 (nicam) frequency register LSB */
100 #define TDA9875_DCR         0x09  /* Demodulateur configuration regirter*/
101 #define TDA9875_DEEM        0x0a  /* FM de-emphasis regirter*/
102 #define TDA9875_FMAT        0x0b  /* FM Matrix regirter*/
103
104 /* values */
105 #define TDA9875_MUTE_ON     0xff /* general mute */
106 #define TDA9875_MUTE_OFF    0xcc /* general no mute */
107
108
109
110 /* Begin code */
111
112 static int tda9875_write(struct i2c_client *client, int subaddr, unsigned char val)
113 {
114         unsigned char buffer[2];
115         dprintk("In tda9875_write\n");
116         dprintk("Writing %d 0x%x\n", subaddr, val);
117         buffer[0] = subaddr;
118         buffer[1] = val;
119         if (2 != i2c_master_send(client,buffer,2)) {
120                 printk(KERN_WARNING "tda9875: I/O error, trying (write %d 0x%x)\n",
121                        subaddr, val);
122                 return -1;
123         }
124         return 0;
125 }
126
127 #if 0
128 static int tda9875_read(struct i2c_client *client)
129 {
130         unsigned char buffer;
131         dprintk("In tda9875_read\n");
132         if (1 != i2c_master_recv(client,&buffer,1)) {
133                 printk(KERN_WARNING "tda9875: I/O error, trying (read)\n");
134                 return -1;
135         }
136         dprintk("Read 0x%02x\n", buffer); 
137         return buffer;
138 }
139 #endif
140
141 static int i2c_read_register(struct i2c_adapter *adap, int addr, int reg)
142 {
143         unsigned char write[1];
144         unsigned char read[1];
145         struct i2c_msg msgs[2] = {
146                 { addr, 0,        1, write },
147                 { addr, I2C_M_RD, 1, read  }
148         };
149         write[0] = reg;
150
151         if (2 != i2c_transfer(adap,msgs,2)) {
152                 printk(KERN_WARNING "tda9875: I/O error (read2)\n");
153                 return -1;
154         }
155         dprintk("tda9875: chip_read2: reg%d=0x%x\n",reg,read[0]);
156         return read[0];
157 }
158
159 static void tda9875_set(struct i2c_client *client)
160 {
161         struct tda9875 *tda = client->data;
162         unsigned char a;
163
164         dprintk(KERN_DEBUG "tda9875_set(%04x,%04x,%04x,%04x)\n",tda->lvol,tda->rvol,tda->bass,tda->treble);
165
166
167         a = tda->lvol & 0xff;
168         tda9875_write(client, TDA9875_MVL, a);
169         a =tda->rvol & 0xff;
170         tda9875_write(client, TDA9875_MVR, a);
171         a =tda->bass & 0xff;
172         tda9875_write(client, TDA9875_MBA, a);
173         a =tda->treble  & 0xff;
174         tda9875_write(client, TDA9875_MTR, a);
175 }
176
177 static void do_tda9875_init(struct i2c_client *client)
178 {
179         struct tda9875 *t = client->data;
180         dprintk("In tda9875_init\n"); 
181         tda9875_write(client, TDA9875_CFG, 0xd0 ); /*reg de config 0 (reset)*/
182         tda9875_write(client, TDA9875_MSR, 0x03 );    /* Monitor 0b00000XXX*/
183         tda9875_write(client, TDA9875_C1MSB, 0x00 );  /*Car1(FM) MSB XMHz*/
184         tda9875_write(client, TDA9875_C1MIB, 0x00 );  /*Car1(FM) MIB XMHz*/
185         tda9875_write(client, TDA9875_C1LSB, 0x00 );  /*Car1(FM) LSB XMHz*/
186         tda9875_write(client, TDA9875_C2MSB, 0x00 );  /*Car2(NICAM) MSB XMHz*/
187         tda9875_write(client, TDA9875_C2MIB, 0x00 );  /*Car2(NICAM) MIB XMHz*/
188         tda9875_write(client, TDA9875_C2LSB, 0x00 );  /*Car2(NICAM) LSB XMHz*/
189         tda9875_write(client, TDA9875_DCR, 0x00 );    /*Demod config 0x00*/
190         tda9875_write(client, TDA9875_DEEM, 0x44 );   /*DE-Emph 0b0100 0100*/
191         tda9875_write(client, TDA9875_FMAT, 0x00 );   /*FM Matrix reg 0x00*/
192         tda9875_write(client, TDA9875_SC1, 0x00 );    /* SCART 1 (SC1)*/
193         tda9875_write(client, TDA9875_SC2, 0x01 );    /* SCART 2 (sc2)*/
194         
195         tda9875_write(client, TDA9875_CH1V, 0x10 );  /* Channel volume 1 mute*/
196         tda9875_write(client, TDA9875_CH2V, 0x10 );  /* Channel volume 2 mute */
197         tda9875_write(client, TDA9875_DACOS, 0x02 ); /* sig DAC i/o(in:nicam)*/
198         tda9875_write(client, TDA9875_ADCIS, 0x6f ); /* sig ADC input(in:mono)*/
199         tda9875_write(client, TDA9875_LOSR, 0x00 );  /* line out (in:mono)*/
200         tda9875_write(client, TDA9875_AER, 0x00 );   /*06 Effect (AVL+PSEUDO) */
201         tda9875_write(client, TDA9875_MCS, 0x44 );   /* Main ch select (DAC) */
202         tda9875_write(client, TDA9875_MVL, 0x03 );   /* Vol Main left 10dB */
203         tda9875_write(client, TDA9875_MVR, 0x03 );   /* Vol Main right 10dB*/
204         tda9875_write(client, TDA9875_MBA, 0x00 );   /* Main Bass Main 0dB*/
205         tda9875_write(client, TDA9875_MTR, 0x00 );   /* Main Treble Main 0dB*/
206         tda9875_write(client, TDA9875_ACS, 0x44 );   /* Aux chan select (dac)*/ 
207         tda9875_write(client, TDA9875_AVL, 0x00 );   /* Vol Aux left 0dB*/
208         tda9875_write(client, TDA9875_AVR, 0x00 );   /* Vol Aux right 0dB*/
209         tda9875_write(client, TDA9875_ABA, 0x00 );   /* Aux Bass Main 0dB*/
210         tda9875_write(client, TDA9875_ATR, 0x00 );   /* Aux Aigus Main 0dB*/
211         
212         tda9875_write(client, TDA9875_MUT, 0xcc );   /* General mute  */
213         
214         t->mode=AUDIO_UNMUTE;
215         t->lvol=t->rvol =0;     /* 0dB */
216         t->bass=0;                      /* 0dB */
217         t->treble=0;            /* 0dB */
218         tda9875_set(client);
219
220 }
221
222
223 /* *********************** *
224  * i2c interface functions *
225  * *********************** */
226
227 static int tda9875_checkit(struct i2c_adapter *adap, int addr)
228 {
229         int dic,rev;
230
231         dic=i2c_read_register(adap,addr,254);
232         rev=i2c_read_register(adap,addr,255);
233
234         if(dic==0 || dic==2) { // tda9875 and tda9875A
235                 printk("tda9875: TDA9875%s Rev.%d detected at 0x%x\n",
236                 dic==0?"":"A", rev,addr<<1);
237                 return 1;
238         }
239         printk("tda9875: no such chip at 0x%x (dic=0x%x rev=0x%x)\n",addr<<1,dic,rev);
240         return(0);
241 }
242
243 static int tda9875_attach(struct i2c_adapter *adap, int addr,
244                           unsigned short flags, int kind)
245 {
246         struct tda9875 *t;
247         struct i2c_client *client;
248         dprintk("In tda9875_attach\n");
249
250         t = kmalloc(sizeof *t,GFP_KERNEL);
251         if (!t)
252                 return -ENOMEM;
253         memset(t,0,sizeof *t);
254
255         client = &t->c;
256         memcpy(client,&client_template,sizeof(struct i2c_client));
257         client->adapter = adap;
258         client->addr = addr;
259         client->data = t;
260
261         if(!tda9875_checkit(adap,addr)) {
262                 kfree(t);
263                 return 1;
264         }
265         
266         do_tda9875_init(client);
267         MOD_INC_USE_COUNT;
268         strcpy(client->name,"TDA9875");
269         printk(KERN_INFO "tda9875: init\n");
270
271         i2c_attach_client(client);
272         return 0;
273 }
274
275 static int tda9875_probe(struct i2c_adapter *adap)
276 {
277         if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848))
278                 return i2c_probe(adap, &addr_data, tda9875_attach);
279         return 0;
280 }
281
282 static int tda9875_detach(struct i2c_client *client)
283 {
284         struct tda9875 *t  = client->data;
285
286         do_tda9875_init(client);
287         i2c_detach_client(client);
288         
289         kfree(t);
290         MOD_DEC_USE_COUNT;
291         return 0;
292 }
293
294 static int tda9875_command(struct i2c_client *client,
295                                 unsigned int cmd, void *arg)
296 {
297         struct tda9875 *t = client->data;
298
299         dprintk("In tda9875_command...\n"); 
300
301         switch (cmd) {  
302         /* --- v4l ioctls --- */
303         /* take care: bttv does userspace copying, we'll get a
304            kernel pointer here... */
305         case VIDIOCGAUDIO:
306         {
307                 struct video_audio *va = arg;
308                 int left,right;
309
310                 dprintk("VIDIOCGAUDIO\n");
311
312                 va->flags |= VIDEO_AUDIO_VOLUME |
313                         VIDEO_AUDIO_BASS |
314                         VIDEO_AUDIO_TREBLE;
315
316                 /* min is -84 max is 24 */
317                 left = (t->lvol+84)*606;
318                 right = (t->rvol+84)*606;
319                 va->volume=MAX(left,right);
320                 va->balance=(32768*MIN(left,right))/
321                         (va->volume ? va->volume : 1);
322                 va->balance=(left<right)?
323                         (65535-va->balance) : va->balance;
324                 va->bass = (t->bass+12)*2427;    /* min -12 max +15 */
325                 va->treble = (t->treble+12)*2730;/* min -12 max +12 */
326
327                 va->mode |= VIDEO_SOUND_MONO;
328
329
330                 break; /* VIDIOCGAUDIO case */
331         }
332
333         case VIDIOCSAUDIO:
334         {
335                 struct video_audio *va = arg;
336                 int left,right;
337
338                 dprintk("VIDEOCSAUDIO...\n"); 
339                 left = (MIN(65536 - va->balance,32768) *
340                         va->volume) / 32768;
341                 right = (MIN(va->balance,32768) *
342                          va->volume) / 32768;
343                 t->lvol = ((left/606)-84) & 0xff;
344                 if (t->lvol > 24)
345                  t->lvol = 24;
346                 if (t->lvol < -84)
347                  t->lvol = -84 & 0xff;
348
349                 t->rvol = ((right/606)-84) & 0xff;
350                 if (t->rvol > 24)
351                  t->rvol = 24;
352                 if (t->rvol < -84)
353                  t->rvol = -84 & 0xff;
354
355                 t->bass = ((va->bass/2400)-12) & 0xff;
356                 if (t->bass > 15)
357                  t->bass = 15;
358                 if (t->bass < -12)
359                  t->bass = -12 & 0xff;
360                 
361                 t->treble = ((va->treble/2700)-12) & 0xff;
362                 if (t->treble > 12)
363                  t->treble = 12;
364                 if (t->treble < -12)
365                  t->treble = -12 & 0xff;
366
367
368
369 //printk("tda9875 bal:%04x vol:%04x bass:%04x treble:%04x\n",va->balance,va->volume,va->bass,va->treble);
370                 
371
372                 tda9875_set(client);
373
374                 break;
375
376         } /* end of VIDEOCSAUDIO case */
377
378         default: /* Not VIDEOCGAUDIO or VIDEOCSAUDIO */
379
380                 /* nothing */
381                 dprintk("Default\n");
382
383         } /* end of (cmd) switch */
384
385         return 0;
386 }
387
388
389 static struct i2c_driver driver = {
390         .name           = "i2c tda9875 driver",
391         .id             = I2C_DRIVERID_TDA9875,
392         .flags          = I2C_DF_NOTIFY,
393         .attach_adapter = tda9875_probe,
394         .detach_client  = tda9875_detach,
395         .command        = tda9875_command,
396 };
397
398 static struct i2c_client client_template =
399 {
400         .name    = "tda9875",
401         .id      = -1,
402         .driver  = &driver,
403 };
404
405 static int tda9875_init(void)
406 {
407         i2c_add_driver(&driver);
408         return 0;
409 }
410
411 static void tda9875_fini(void)
412 {
413         i2c_del_driver(&driver);
414 }
415
416 module_init(tda9875_init);
417 module_exit(tda9875_fini);
418
419 /*
420  * Local variables:
421  * c-basic-offset: 8
422  * End:
423  */
424