080cfba95fb332439c9567e59763548117758891
[linux-flexiantxendom0-3.2.10.git] / drivers / media / video / tda9887.c
1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/i2c.h>
4 #include <linux/types.h>
5 #include <linux/videodev.h>
6 #include <linux/init.h>
7 #include <linux/errno.h>
8 #include <linux/slab.h>
9
10 #include <media/audiochip.h>
11 #include <media/id.h>
12
13 /* Chips:
14    TDA9885 (PAL, NTSC)
15    TDA9886 (PAL, SECAM, NTSC)
16    TDA9887 (PAL, SECAM, NTSC, FM Radio)
17
18    found on:
19    - Pinnacle PCTV (Jul.2002 Version with MT2032, bttv)
20       TDA9887 (world), TDA9885 (USA)
21       Note: OP2 of tda988x must be set to 1, else MT2032 is disabled!
22    - KNC One TV-Station RDS (saa7134)
23 */
24     
25
26 /* Addresses to scan */
27 static unsigned short normal_i2c[] = {I2C_CLIENT_END};
28 static unsigned short normal_i2c_range[] = {0x86>>1,0x86>>1,I2C_CLIENT_END};
29 I2C_CLIENT_INSMOD;
30
31 /* insmod options */
32 static int debug =  0;
33 static char *pal =  "b";
34 static char *secam =  "l";
35 MODULE_PARM(debug,"i");
36 MODULE_PARM(pal,"s");
37 MODULE_PARM(secam,"s");
38 MODULE_LICENSE("GPL");
39
40 /* ---------------------------------------------------------------------- */
41
42 #define dprintk     if (debug) printk
43
44 struct tda9887 {
45         struct i2c_client client;
46         int radio,tvnorm;
47         int pinnacle_id;
48 };
49
50 static struct i2c_driver driver;
51 static struct i2c_client client_template;
52
53 /* ---------------------------------------------------------------------- */
54
55 //
56 // TDA defines
57 //
58
59 //// first reg
60 #define cVideoTrapBypassOFF     0x00    // bit b0
61 #define cVideoTrapBypassON      0x01    // bit b0
62
63 #define cAutoMuteFmInactive     0x00    // bit b1
64 #define cAutoMuteFmActive       0x02    // bit b1
65
66 #define cIntercarrier           0x00    // bit b2
67 #define cQSS                    0x04    // bit b2
68
69 #define cPositiveAmTV           0x00    // bit b3:4
70 #define cFmRadio                0x08    // bit b3:4
71 #define cNegativeFmTV           0x10    // bit b3:4
72
73
74 #define cForcedMuteAudioON      0x20    // bit b5
75 #define cForcedMuteAudioOFF     0x00    // bit b5
76
77 #define cOutputPort1Active      0x00    // bit b6
78 #define cOutputPort1Inactive    0x40    // bit b6
79
80 #define cOutputPort2Active      0x00    // bit b7
81 #define cOutputPort2Inactive    0x80    // bit b7
82
83
84 //// second reg
85 #define cDeemphasisOFF          0x00    // bit c5
86 #define cDeemphasisON           0x20    // bit c5
87
88 #define cDeemphasis75           0x00    // bit c6
89 #define cDeemphasis50           0x40    // bit c6
90
91 #define cAudioGain0             0x00    // bit c7
92 #define cAudioGain6             0x80    // bit c7
93
94
95 //// third reg
96 #define cAudioIF_4_5             0x00    // bit e0:1
97 #define cAudioIF_5_5             0x01    // bit e0:1
98 #define cAudioIF_6_0             0x02    // bit e0:1
99 #define cAudioIF_6_5             0x03    // bit e0:1
100
101
102 #define cVideoIF_58_75           0x00    // bit e2:4
103 #define cVideoIF_45_75           0x04    // bit e2:4
104 #define cVideoIF_38_90           0x08    // bit e2:4
105 #define cVideoIF_38_00           0x0C    // bit e2:4
106 #define cVideoIF_33_90           0x10    // bit e2:4
107 #define cVideoIF_33_40           0x14    // bit e2:4
108 #define cRadioIF_45_75           0x18    // bit e2:4
109 #define cRadioIF_38_90           0x1C    // bit e2:4
110
111
112 #define cTunerGainNormal         0x00    // bit e5
113 #define cTunerGainLow            0x20    // bit e5
114
115 #define cGating_18               0x00    // bit e6
116 #define cGating_36               0x40    // bit e6
117
118 #define cAgcOutON                0x80    // bit e7
119 #define cAgcOutOFF               0x00    // bit e7
120
121 static int tda9887_miro(struct tda9887 *t)
122 {
123         int rc;
124         u8   bData[4]     = { 0 };
125         u8   bVideoIF     = 0;
126         u8   bAudioIF     = 0;
127         u8   bDeEmphasis  = 0;
128         u8   bDeEmphVal   = 0;
129         u8   bModulation  = 0;
130         u8   bCarrierMode = 0;
131         u8   bOutPort1    = cOutputPort1Inactive;
132 #if 0
133         u8   bOutPort2    = cOutputPort2Inactive & mbTADState; // store i2c tuner state
134 #else
135         u8   bOutPort2    = cOutputPort2Inactive;
136 #endif
137         u8   bVideoTrap   = cVideoTrapBypassOFF;
138 #if 1
139         u8   bTopAdjust   = 0x0e /* -2dB */;
140 #else
141         u8   bTopAdjust   = 0;
142 #endif
143
144 #if 0
145         if (mParams.fVideoTrap)
146                 bVideoTrap   = cVideoTrapBypassON;
147 #endif
148
149         if (t->radio) {
150                 bVideoTrap   = cVideoTrapBypassOFF;
151                 bCarrierMode = cQSS;
152                 bModulation  = cFmRadio;
153                 bOutPort1    = cOutputPort1Inactive;
154                 bDeEmphasis  = cDeemphasisON;
155                 if (3 == t->pinnacle_id) {
156                         /* ntsc */
157                         bDeEmphVal   = cDeemphasis75;
158                         bAudioIF     = cAudioIF_4_5;
159                         bVideoIF     = cRadioIF_45_75;
160                 } else {
161                         /* pal */
162                         bAudioIF     = cAudioIF_5_5;
163                         bVideoIF     = cRadioIF_38_90;
164                         bDeEmphVal   = cDeemphasis50;
165                 }
166
167         } else if (t->tvnorm == VIDEO_MODE_PAL) {
168                 bDeEmphasis  = cDeemphasisON;
169                 bDeEmphVal   = cDeemphasis50;
170                 bModulation  = cNegativeFmTV;
171                 bOutPort1    = cOutputPort1Inactive;
172                 if (1 == t->pinnacle_id) {
173                         bCarrierMode = cIntercarrier;
174                 } else {
175                         // stereo boards
176                         bCarrierMode = cQSS;
177                 }
178                 switch (pal[0]) {
179                 case 'b':
180                 case 'g':
181                 case 'h':
182                         bVideoIF     = cVideoIF_38_90;
183                         bAudioIF     = cAudioIF_5_5;
184                         break;
185                 case 'd':
186                         bVideoIF     = cVideoIF_38_00;
187                         bAudioIF     = cAudioIF_6_5;
188                         break;
189                 case 'i':
190                         bVideoIF     = cVideoIF_38_90;
191                         bAudioIF     = cAudioIF_6_0;
192                         break;
193                 case 'm':
194                 case 'n':
195                         bVideoIF     = cVideoIF_45_75;
196                         bAudioIF     = cAudioIF_4_5;
197                         bDeEmphVal   = cDeemphasis75;
198                         if ((5 == t->pinnacle_id) || (6 == t->pinnacle_id)) {
199                                 bCarrierMode = cIntercarrier;
200                         } else {
201                                 bCarrierMode = cQSS;
202                         }
203                         break;
204                 }
205
206         } else if (t->tvnorm == VIDEO_MODE_SECAM) {
207                 bAudioIF     = cAudioIF_6_5;
208                 bDeEmphasis  = cDeemphasisON;
209                 bDeEmphVal   = cDeemphasis50;
210                 bModulation  = cNegativeFmTV;
211                 bCarrierMode = cQSS;
212                 bOutPort1    = cOutputPort1Inactive;                
213                 switch (secam[0]) {
214                 case 'd':
215                         bVideoIF     = cVideoIF_38_00;
216                         break;
217                 case 'k':
218                         bVideoIF     = cVideoIF_38_90;
219                         break;
220                 case 'l':
221                         bVideoIF     = cVideoIF_38_90;
222                         bDeEmphasis  = cDeemphasisOFF;
223                         bDeEmphVal   = cDeemphasis75;
224                         bModulation  = cPositiveAmTV;
225                         break;
226                 case 'L' /* L1 */:
227                         bVideoIF     = cVideoIF_33_90;
228                         bDeEmphasis  = cDeemphasisOFF;
229                         bDeEmphVal   = cDeemphasis75;
230                         bModulation  = cPositiveAmTV;
231                         break;
232                 }
233
234         } else if (t->tvnorm == VIDEO_MODE_NTSC) {
235                 bVideoIF     = cVideoIF_45_75;
236                 bAudioIF     = cAudioIF_4_5;
237                 bDeEmphasis  = cDeemphasisON;
238                 bDeEmphVal   = cDeemphasis75;
239                 bModulation  = cNegativeFmTV;                
240                 bOutPort1    = cOutputPort1Inactive;
241                 if ((5 == t->pinnacle_id) || (6 == t->pinnacle_id)) {
242                         bCarrierMode = cIntercarrier;
243                 } else {
244                         bCarrierMode = cQSS;
245                 }
246         }
247
248         bData[1] = bVideoTrap        |  // B0: video trap bypass
249                 cAutoMuteFmInactive  |  // B1: auto mute
250                 bCarrierMode         |  // B2: InterCarrier for PAL else QSS 
251                 bModulation          |  // B3 - B4: positive AM TV for SECAM only
252                 cForcedMuteAudioOFF  |  // B5: forced Audio Mute (off)
253                 bOutPort1            |  // B6: Out Port 1 
254                 bOutPort2;              // B7: Out Port 2 
255         bData[2] = bTopAdjust |   // C0 - C4: Top Adjust 0 == -16dB  31 == 15dB
256                 bDeEmphasis   |   // C5: De-emphasis on/off
257                 bDeEmphVal    |   // C6: De-emphasis 50/75 microsec
258                 cAudioGain0;      // C7: normal audio gain
259         bData[3] = bAudioIF      |  // E0 - E1: Sound IF
260                 bVideoIF         |  // E2 - E4: Video IF
261                 cTunerGainNormal |  // E5: Tuner gain (normal)
262                 cGating_18       |  // E6: Gating (18%)
263                 cAgcOutOFF;         // E7: VAGC  (off)
264         
265         dprintk("tda9885/6/7: 0x%02x 0x%02x 0x%02x [pinnacle_id=%d]\n",
266                 bData[1],bData[2],bData[3],t->pinnacle_id);
267         if (4 != (rc = i2c_master_send(&t->client,bData,4)))
268                 printk("tda9885/6/7: i2c i/o error: rc == %d (should be 4)\n",rc);
269         return 0;
270 }
271
272 /* ---------------------------------------------------------------------- */
273
274 #if 0
275 /* just for reference: old knc-one saa7134 stuff */
276 static unsigned char buf_pal_bg[]    = { 0x00, 0x16, 0x70, 0x49 };
277 static unsigned char buf_pal_i[]     = { 0x00, 0x16, 0x70, 0x4a };
278 static unsigned char buf_pal_dk[]    = { 0x00, 0x16, 0x70, 0x4b };
279 static unsigned char buf_pal_l[]     = { 0x00, 0x06, 0x50, 0x4b };
280 static unsigned char buf_fm_stereo[] = { 0x00, 0x0e, 0x0d, 0x77 };
281 #endif
282
283 static unsigned char buf_pal_bg[]    = { 0x00, 0x96, 0x70, 0x49 };
284 static unsigned char buf_pal_i[]     = { 0x00, 0x96, 0x70, 0x4a };
285 static unsigned char buf_pal_dk[]    = { 0x00, 0x96, 0x70, 0x4b };
286 static unsigned char buf_pal_l[]     = { 0x00, 0x86, 0x50, 0x4b };
287 static unsigned char buf_fm_stereo[] = { 0x00, 0x8e, 0x0d, 0x77 };
288 static unsigned char buf_ntsc[]      = { 0x00, 0x96, 0x70, 0x44 };
289 static unsigned char buf_ntsc_jp[]   = { 0x00, 0x96, 0x70, 0x40 };
290
291 static int tda9887_configure(struct tda9887 *t)
292 {
293         unsigned char *buf = NULL;
294         int rc;
295
296         if (t->radio) {
297                 dprintk("tda9885/6/7: FM Radio mode\n");
298                 buf = buf_fm_stereo;
299
300         } else if (t->tvnorm == VIDEO_MODE_PAL) {
301                 dprintk("tda9885/6/7: PAL-%c mode\n",pal[0]);
302                 switch (pal[0]) {
303                 case 'b':
304                 case 'g':
305                         buf = buf_pal_bg;
306                         break;
307                 case 'i':
308                         buf = buf_pal_i;
309                         break;
310                 case 'd':
311                 case 'k':
312                         buf = buf_pal_dk;
313                         break;
314                 case 'l':
315                         buf = buf_pal_l;
316                         break;
317                 }
318
319         } else if (t->tvnorm == VIDEO_MODE_NTSC) {
320                 dprintk("tda9885/6/7: NTSC mode\n");
321                 buf = buf_ntsc;
322
323         } else if (t->tvnorm == VIDEO_MODE_SECAM) {
324                 dprintk("tda9885/6/7: SECAM mode\n");
325                 buf = buf_pal_l;
326
327         } else if (t->tvnorm == 6 /* BTTV hack */) {
328                 dprintk("tda9885/6/7: NTSC-Japan mode\n");
329                 buf = buf_ntsc_jp;
330         }
331
332         if (NULL == buf) {
333                 printk("tda9885/6/7 unknown norm=%d\n",t->tvnorm);
334                 return 0;
335         }
336
337         dprintk("tda9885/6/7: 0x%02x 0x%02x 0x%02x\n",
338                 buf[1],buf[2],buf[3]);
339         if (4 != (rc = i2c_master_send(&t->client,buf,4)))
340                 printk("tda9885/6/7: i2c i/o error: rc == %d (should be 4)\n",rc);
341         return 0;
342 }
343
344 /* ---------------------------------------------------------------------- */
345
346 static int tda9887_attach(struct i2c_adapter *adap, int addr, int kind)
347 {
348         struct tda9887 *t;
349
350         client_template.adapter = adap;
351         client_template.addr    = addr;
352
353         printk("tda9887: chip found @ 0x%x\n", addr<<1);
354
355         if (NULL == (t = kmalloc(sizeof(*t), GFP_KERNEL)))
356                 return -ENOMEM;
357         memset(t,0,sizeof(*t));
358         t->client = client_template;
359         t->pinnacle_id = -1;
360         t->tvnorm=VIDEO_MODE_PAL;
361         i2c_set_clientdata(&t->client, t);
362         i2c_attach_client(&t->client);
363         
364         return 0;
365 }
366
367 static int tda9887_probe(struct i2c_adapter *adap)
368 {
369         if (adap->class & I2C_ADAP_CLASS_TV_ANALOG)
370                 return i2c_probe(adap, &addr_data, tda9887_attach);
371         return 0;
372 }
373
374 static int tda9887_detach(struct i2c_client *client)
375 {
376         struct tda9887 *t = i2c_get_clientdata(client);
377
378         i2c_detach_client(client);
379         kfree(t);
380         return 0;
381 }
382
383 static int
384 tda9887_command(struct i2c_client *client, unsigned int cmd, void *arg)
385 {
386         struct tda9887 *t = i2c_get_clientdata(client);
387
388         switch (cmd) {
389
390         /* --- configuration --- */
391         case AUDC_SET_RADIO:
392                 t->radio = 1;
393                 if (-1 != t->pinnacle_id)
394                         tda9887_miro(t);
395                 else
396                         tda9887_configure(t);
397                 break;
398                 
399         case AUDC_CONFIG_PINNACLE:
400         {
401                 int *i = arg;
402
403                 t->pinnacle_id = *i;
404                 tda9887_miro(t);
405                 break;
406         }
407         /* --- v4l ioctls --- */
408         /* take care: bttv does userspace copying, we'll get a
409            kernel pointer here... */
410         case VIDIOCSCHAN:
411         {
412                 struct video_channel *vc = arg;
413
414                 t->radio  = 0;
415                 t->tvnorm = vc->norm;
416                 if (-1 != t->pinnacle_id)
417                         tda9887_miro(t);
418                 else
419                         tda9887_configure(t);
420                 break;
421         }
422         default:
423                 /* nothing */
424                 break;
425         }
426         return 0;
427 }
428
429 /* ----------------------------------------------------------------------- */
430
431 static struct i2c_driver driver = {
432         .owner          = THIS_MODULE,
433         .name           = "i2c tda9887 driver",
434         .id             = -1, /* FIXME */
435         .flags          = I2C_DF_NOTIFY,
436         .attach_adapter = tda9887_probe,
437         .detach_client  = tda9887_detach,
438         .command        = tda9887_command,
439 };
440 static struct i2c_client client_template =
441 {
442         .flags  = I2C_CLIENT_ALLOW_USE,
443         .driver = &driver,
444         .dev    = {
445                 .name   = "tda9887",
446         },
447 };
448
449 static int tda9887_init_module(void)
450 {
451         i2c_add_driver(&driver);
452         return 0;
453 }
454
455 static void tda9887_cleanup_module(void)
456 {
457         i2c_del_driver(&driver);
458 }
459
460 module_init(tda9887_init_module);
461 module_exit(tda9887_cleanup_module);
462
463 /*
464  * Overrides for Emacs so that we follow Linus's tabbing style.
465  * ---------------------------------------------------------------------------
466  * Local variables:
467  * c-basic-offset: 8
468  * End:
469  */