Merge kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6.git
[powerpc.git] / drivers / media / video / tda9887.c
1 #include <linux/module.h>
2 #include <linux/moduleparam.h>
3 #include <linux/kernel.h>
4 #include <linux/i2c.h>
5 #include <linux/types.h>
6 #include <linux/videodev.h>
7 #include <linux/init.h>
8 #include <linux/errno.h>
9 #include <linux/slab.h>
10 #include <linux/delay.h>
11
12 #include <media/audiochip.h>
13 #include <media/tuner.h>
14 #include <media/id.h>
15
16 /* Chips:
17    TDA9885 (PAL, NTSC)
18    TDA9886 (PAL, SECAM, NTSC)
19    TDA9887 (PAL, SECAM, NTSC, FM Radio)
20
21    found on:
22    - Pinnacle PCTV (Jul.2002 Version with MT2032, bttv)
23       TDA9887 (world), TDA9885 (USA)
24       Note: OP2 of tda988x must be set to 1, else MT2032 is disabled!
25    - KNC One TV-Station RDS (saa7134)
26 */
27
28
29 /* Addresses to scan */
30 static unsigned short normal_i2c[] = {
31         0x84 >>1,
32         0x86 >>1,
33         0x96 >>1,
34         I2C_CLIENT_END,
35 };
36 I2C_CLIENT_INSMOD;
37
38 /* insmod options */
39 static unsigned int debug = 0;
40 module_param(debug, int, 0644);
41 MODULE_LICENSE("GPL");
42
43 /* ---------------------------------------------------------------------- */
44
45 #define UNSET       (-1U)
46 #define PREFIX      "tda9885/6/7: "
47 #define dprintk     if (debug) printk
48
49 struct tda9887 {
50         struct i2c_client  client;
51         v4l2_std_id        std;
52         unsigned int       radio;
53         unsigned int       config;
54         unsigned int       pinnacle_id;
55         unsigned int       using_v4l2;
56 };
57
58 struct tvnorm {
59         v4l2_std_id       std;
60         char              *name;
61         unsigned char     b;
62         unsigned char     c;
63         unsigned char     e;
64 };
65
66 static struct i2c_driver driver;
67 static struct i2c_client client_template;
68
69 /* ---------------------------------------------------------------------- */
70
71 //
72 // TDA defines
73 //
74
75 //// first reg (b)
76 #define cVideoTrapBypassOFF     0x00    // bit b0
77 #define cVideoTrapBypassON      0x01    // bit b0
78
79 #define cAutoMuteFmInactive     0x00    // bit b1
80 #define cAutoMuteFmActive       0x02    // bit b1
81
82 #define cIntercarrier           0x00    // bit b2
83 #define cQSS                    0x04    // bit b2
84
85 #define cPositiveAmTV           0x00    // bit b3:4
86 #define cFmRadio                0x08    // bit b3:4
87 #define cNegativeFmTV           0x10    // bit b3:4
88
89
90 #define cForcedMuteAudioON      0x20    // bit b5
91 #define cForcedMuteAudioOFF     0x00    // bit b5
92
93 #define cOutputPort1Active      0x00    // bit b6
94 #define cOutputPort1Inactive    0x40    // bit b6
95
96 #define cOutputPort2Active      0x00    // bit b7
97 #define cOutputPort2Inactive    0x80    // bit b7
98
99
100 //// second reg (c)
101 #define cDeemphasisOFF          0x00    // bit c5
102 #define cDeemphasisON           0x20    // bit c5
103
104 #define cDeemphasis75           0x00    // bit c6
105 #define cDeemphasis50           0x40    // bit c6
106
107 #define cAudioGain0             0x00    // bit c7
108 #define cAudioGain6             0x80    // bit c7
109
110
111 //// third reg (e)
112 #define cAudioIF_4_5             0x00    // bit e0:1
113 #define cAudioIF_5_5             0x01    // bit e0:1
114 #define cAudioIF_6_0             0x02    // bit e0:1
115 #define cAudioIF_6_5             0x03    // bit e0:1
116
117
118 #define cVideoIF_58_75           0x00    // bit e2:4
119 #define cVideoIF_45_75           0x04    // bit e2:4
120 #define cVideoIF_38_90           0x08    // bit e2:4
121 #define cVideoIF_38_00           0x0C    // bit e2:4
122 #define cVideoIF_33_90           0x10    // bit e2:4
123 #define cVideoIF_33_40           0x14    // bit e2:4
124 #define cRadioIF_45_75           0x18    // bit e2:4
125 #define cRadioIF_38_90           0x1C    // bit e2:4
126
127
128 #define cTunerGainNormal         0x00    // bit e5
129 #define cTunerGainLow            0x20    // bit e5
130
131 #define cGating_18               0x00    // bit e6
132 #define cGating_36               0x40    // bit e6
133
134 #define cAgcOutON                0x80    // bit e7
135 #define cAgcOutOFF               0x00    // bit e7
136
137 /* ---------------------------------------------------------------------- */
138
139 static struct tvnorm tvnorms[] = {
140         {
141                 .std   = V4L2_STD_PAL_BG,
142                 .name  = "PAL-BG",
143                 .b     = ( cNegativeFmTV  |
144                            cQSS           ),
145                 .c     = ( cDeemphasisON  |
146                            cDeemphasis50  ),
147                 .e     = ( cAudioIF_5_5   |
148                            cVideoIF_38_90 ),
149         },{
150                 .std   = V4L2_STD_PAL_I,
151                 .name  = "PAL-I",
152                 .b     = ( cNegativeFmTV  |
153                            cQSS           ),
154                 .c     = ( cDeemphasisON  |
155                            cDeemphasis50  ),
156                 .e     = ( cAudioIF_6_0   |
157                            cVideoIF_38_90 ),
158         },{
159                 .std   = V4L2_STD_PAL_DK,
160                 .name  = "PAL-DK",
161                 .b     = ( cNegativeFmTV  |
162                            cQSS           ),
163                 .c     = ( cDeemphasisON  |
164                            cDeemphasis50  ),
165                 .e     = ( cAudioIF_6_5   |
166                            cVideoIF_38_00 ),
167         },{
168                 .std   = V4L2_STD_PAL_M | V4L2_STD_PAL_N,
169                 .name  = "PAL-M/N",
170                 .b     = ( cNegativeFmTV  |
171                            cQSS           ),
172                 .c     = ( cDeemphasisON  |
173                            cDeemphasis75  ),
174                 .e     = ( cAudioIF_4_5   |
175                            cVideoIF_45_75 ),
176         },{
177                 .std   = V4L2_STD_SECAM_L,
178                 .name  = "SECAM-L",
179                 .b     = ( cPositiveAmTV  |
180                            cQSS           ),
181                 .e     = ( cAudioIF_6_5   |
182                            cVideoIF_38_90 ),
183         },{
184                 .std   = V4L2_STD_SECAM_DK,
185                 .name  = "SECAM-DK",
186                 .b     = ( cNegativeFmTV  |
187                            cQSS           ),
188                 .c     = ( cDeemphasisON  |
189                            cDeemphasis50  ),
190                 .e     = ( cAudioIF_6_5   |
191                            cVideoIF_38_00 ),
192         },{
193                 .std   = V4L2_STD_NTSC_M,
194                 .name  = "NTSC-M",
195                 .b     = ( cNegativeFmTV  |
196                            cQSS           ),
197                 .c     = ( cDeemphasisON  |
198                            cDeemphasis50  ),
199                 .e     = ( cGating_36     |
200                            cAudioIF_4_5   |
201                            cVideoIF_45_75 ),
202         },{
203                 .std   = V4L2_STD_NTSC_M_JP,
204                 .name  = "NTSC-JP",
205                 .b     = ( cNegativeFmTV  |
206                            cQSS           ),
207                 .c     = ( cDeemphasisON  |
208                            cDeemphasis50  ),
209                 .e     = ( cGating_36     |
210                            cAudioIF_4_5   |
211                            cVideoIF_58_75 ),
212         }
213 };
214
215 static struct tvnorm radio = {
216         .name = "radio",
217         .b    = ( cFmRadio       |
218                   cQSS           ),
219         .c    = ( cDeemphasisON  |
220                   cDeemphasis50  ),
221         .e    = ( cAudioIF_5_5   |
222                   cRadioIF_38_90 ),
223 };
224
225 /* ---------------------------------------------------------------------- */
226
227 static void dump_read_message(unsigned char *buf)
228 {
229         static char *afc[16] = {
230                 "- 12.5 kHz",
231                 "- 37.5 kHz",
232                 "- 62.5 kHz",
233                 "- 87.5 kHz",
234                 "-112.5 kHz",
235                 "-137.5 kHz",
236                 "-162.5 kHz",
237                 "-187.5 kHz [min]",
238                 "+187.5 kHz [max]",
239                 "+162.5 kHz",
240                 "+137.5 kHz",
241                 "+112.5 kHz",
242                 "+ 87.5 kHz",
243                 "+ 62.5 kHz",
244                 "+ 37.5 kHz",
245                 "+ 12.5 kHz",
246         };
247         printk(PREFIX "read: 0x%2x\n", buf[0]);
248         printk("  after power on : %s\n", (buf[0] & 0x01) ? "yes" : "no");
249         printk("  afc            : %s\n", afc[(buf[0] >> 1) & 0x0f]);
250         printk("  fmif level     : %s\n", (buf[0] & 0x20) ? "high" : "low");
251         printk("  afc window     : %s\n", (buf[0] & 0x40) ? "in" : "out");
252         printk("  vfi level      : %s\n", (buf[0] & 0x80) ? "high" : "low");
253 }
254
255 static void dump_write_message(unsigned char *buf)
256 {
257         static char *sound[4] = {
258                 "AM/TV",
259                 "FM/radio",
260                 "FM/TV",
261                 "FM/radio"
262         };
263         static char *adjust[32] = {
264                 "-16", "-15", "-14", "-13", "-12", "-11", "-10", "-9",
265                 "-8",  "-7",  "-6",  "-5",  "-4",  "-3",  "-2",  "-1",
266                 "0",   "+1",  "+2",  "+3",  "+4",  "+5",  "+6",  "+7",
267                 "+8",  "+9",  "+10", "+11", "+12", "+13", "+14", "+15"
268         };
269         static char *deemph[4] = {
270                 "no", "no", "75", "50"
271         };
272         static char *carrier[4] = {
273                 "4.5 MHz",
274                 "5.5 MHz",
275                 "6.0 MHz",
276                 "6.5 MHz / AM"
277         };
278         static char *vif[8] = {
279                 "58.75 MHz",
280                 "45.75 MHz",
281                 "38.9 MHz",
282                 "38.0 MHz",
283                 "33.9 MHz",
284                 "33.4 MHz",
285                 "45.75 MHz + pin13",
286                 "38.9 MHz + pin13",
287         };
288         static char *rif[4] = {
289                 "44 MHz",
290                 "52 MHz",
291                 "52 MHz",
292                 "44 MHz",
293         };
294
295         printk(PREFIX "write: byte B 0x%02x\n",buf[1]);
296         printk("  B0   video mode      : %s\n",
297                (buf[1] & 0x01) ? "video trap" : "sound trap");
298         printk("  B1   auto mute fm    : %s\n",
299                (buf[1] & 0x02) ? "yes" : "no");
300         printk("  B2   carrier mode    : %s\n",
301                (buf[1] & 0x04) ? "QSS" : "Intercarrier");
302         printk("  B3-4 tv sound/radio  : %s\n",
303                sound[(buf[1] & 0x18) >> 3]);
304         printk("  B5   force mute audio: %s\n",
305                (buf[1] & 0x20) ? "yes" : "no");
306         printk("  B6   output port 1   : %s\n",
307                (buf[1] & 0x40) ? "high (inactive)" : "low (active)");
308         printk("  B7   output port 2   : %s\n",
309                (buf[1] & 0x80) ? "high (inactive)" : "low (active)");
310
311         printk(PREFIX "write: byte C 0x%02x\n",buf[2]);
312         printk("  C0-4 top adjustment  : %s dB\n", adjust[buf[2] & 0x1f]);
313         printk("  C5-6 de-emphasis     : %s\n", deemph[(buf[2] & 0x60) >> 5]);
314         printk("  C7   audio gain      : %s\n",
315                (buf[2] & 0x80) ? "-6" : "0");
316
317         printk(PREFIX "write: byte E 0x%02x\n",buf[3]);
318         printk("  E0-1 sound carrier   : %s\n",
319                carrier[(buf[3] & 0x03)]);
320         printk("  E6   l pll ganting   : %s\n",
321                (buf[3] & 0x40) ? "36" : "13");
322
323         if (buf[1] & 0x08) {
324                 /* radio */
325                 printk("  E2-4 video if        : %s\n",
326                        rif[(buf[3] & 0x0c) >> 2]);
327                 printk("  E7   vif agc output  : %s\n",
328                        (buf[3] & 0x80)
329                        ? ((buf[3] & 0x10) ? "fm-agc radio" : "sif-agc radio")
330                        : "fm radio carrier afc");
331         } else {
332                 /* video */
333                 printk("  E2-4 video if        : %s\n",
334                        vif[(buf[3] & 0x1c) >> 2]);
335                 printk("  E5   tuner gain      : %s\n",
336                        (buf[3] & 0x80)
337                        ? ((buf[3] & 0x20) ? "external" : "normal")
338                        : ((buf[3] & 0x20) ? "minimum"  : "normal"));
339                 printk("  E7   vif agc output  : %s\n",
340                        (buf[3] & 0x80)
341                        ? ((buf[3] & 0x20)
342                           ? "pin3 port, pin22 vif agc out"
343                           : "pin22 port, pin3 vif acg ext in")
344                        : "pin3+pin22 port");
345         }
346         printk("--\n");
347 }
348
349 /* ---------------------------------------------------------------------- */
350
351 static int tda9887_set_tvnorm(struct tda9887 *t, char *buf)
352 {
353         struct tvnorm *norm = NULL;
354         int i;
355
356         if (t->radio) {
357                 norm = &radio;
358         } else {
359                 for (i = 0; i < ARRAY_SIZE(tvnorms); i++) {
360                         if (tvnorms[i].std & t->std) {
361                                 norm = tvnorms+i;
362                                 break;
363                         }
364                 }
365         }
366         if (NULL == norm) {
367                 dprintk(PREFIX "Oops: no tvnorm entry found\n");
368                 return -1;
369         }
370
371         dprintk(PREFIX "configure for: %s\n",norm->name);
372         buf[1] = norm->b;
373         buf[2] = norm->c;
374         buf[3] = norm->e;
375         return 0;
376 }
377
378 static unsigned int port1  = UNSET;
379 static unsigned int port2  = UNSET;
380 static unsigned int qss    = UNSET;
381 static unsigned int adjust = 0x10;
382 module_param(port1, int, 0644);
383 module_param(port2, int, 0644);
384 module_param(qss, int, 0644);
385 module_param(adjust, int, 0644);
386
387 static int tda9887_set_insmod(struct tda9887 *t, char *buf)
388 {
389         if (UNSET != port1) {
390                 if (port1)
391                         buf[1] |= cOutputPort1Inactive;
392                 else
393                         buf[1] &= ~cOutputPort1Inactive;
394         }
395         if (UNSET != port2) {
396                 if (port2)
397                         buf[1] |= cOutputPort2Inactive;
398                 else
399                         buf[1] &= ~cOutputPort2Inactive;
400         }
401
402         if (UNSET != qss) {
403                 if (qss)
404                         buf[1] |= cQSS;
405                 else
406                         buf[1] &= ~cQSS;
407         }
408
409         if (adjust >= 0x00 && adjust < 0x20)
410                 buf[2] |= adjust;
411         return 0;
412 }
413
414 static int tda9887_set_config(struct tda9887 *t, char *buf)
415 {
416         if (t->config & TDA9887_PORT1_ACTIVE)
417                 buf[1] &= ~cOutputPort1Inactive;
418         if (t->config & TDA9887_PORT1_INACTIVE)
419                 buf[1] |= cOutputPort1Inactive;
420         if (t->config & TDA9887_PORT2_ACTIVE)
421                 buf[1] &= ~cOutputPort2Inactive;
422         if (t->config & TDA9887_PORT2_INACTIVE)
423                 buf[1] |= cOutputPort2Inactive;
424
425         if (t->config & TDA9887_QSS)
426                 buf[1] |= cQSS;
427         if (t->config & TDA9887_INTERCARRIER)
428                 buf[1] &= ~cQSS;
429
430         if (t->config & TDA9887_AUTOMUTE)
431                 buf[1] |= cAutoMuteFmActive;
432         if (t->config & TDA9887_DEEMPHASIS_MASK) {
433                 buf[2] &= ~0x60;
434                 switch (t->config & TDA9887_DEEMPHASIS_MASK) {
435                 case TDA9887_DEEMPHASIS_NONE:
436                         buf[2] |= cDeemphasisOFF;
437                         break;
438                 case TDA9887_DEEMPHASIS_50:
439                         buf[2] |= cDeemphasisON | cDeemphasis50;
440                         break;
441                 case TDA9887_DEEMPHASIS_75:
442                         buf[2] |= cDeemphasisON | cDeemphasis75;
443                         break;
444                 }
445         }
446         return 0;
447 }
448
449 /* ---------------------------------------------------------------------- */
450
451 static int tda9887_set_pinnacle(struct tda9887 *t, char *buf)
452 {
453         unsigned int bCarrierMode = UNSET;
454
455         if (t->std & V4L2_STD_625_50) {
456                 if ((1 == t->pinnacle_id) || (7 == t->pinnacle_id)) {
457                         bCarrierMode = cIntercarrier;
458                 } else {
459                         bCarrierMode = cQSS;
460                 }
461         }
462         if (t->std & V4L2_STD_525_60) {
463                 if ((5 == t->pinnacle_id) || (6 == t->pinnacle_id)) {
464                         bCarrierMode = cIntercarrier;
465                 } else {
466                         bCarrierMode = cQSS;
467                 }
468         }
469
470         if (bCarrierMode != UNSET) {
471                 buf[1] &= ~0x04;
472                 buf[1] |= bCarrierMode;
473         }
474         return 0;
475 }
476
477 /* ---------------------------------------------------------------------- */
478
479 static char pal[] = "-";
480 module_param_string(pal, pal, sizeof(pal), 0644);
481 static char secam[] = "-";
482 module_param_string(secam, secam, sizeof(secam), 0644);
483
484 static int tda9887_fixup_std(struct tda9887 *t)
485 {
486         /* get more precise norm info from insmod option */
487         if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
488                 switch (pal[0]) {
489                 case 'b':
490                 case 'B':
491                 case 'g':
492                 case 'G':
493                         dprintk(PREFIX "insmod fixup: PAL => PAL-BG\n");
494                         t->std = V4L2_STD_PAL_BG;
495                         break;
496                 case 'i':
497                 case 'I':
498                         dprintk(PREFIX "insmod fixup: PAL => PAL-I\n");
499                         t->std = V4L2_STD_PAL_I;
500                         break;
501                 case 'd':
502                 case 'D':
503                 case 'k':
504                 case 'K':
505                         dprintk(PREFIX "insmod fixup: PAL => PAL-DK\n");
506                         t->std = V4L2_STD_PAL_DK;
507                         break;
508                 }
509         }
510         if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
511                 switch (secam[0]) {
512                 case 'd':
513                 case 'D':
514                 case 'k':
515                 case 'K':
516                         dprintk(PREFIX "insmod fixup: SECAM => SECAM-DK\n");
517                         t->std = V4L2_STD_SECAM_DK;
518                         break;
519                 case 'l':
520                 case 'L':
521                         dprintk(PREFIX "insmod fixup: SECAM => SECAM-L\n");
522                         t->std = V4L2_STD_SECAM_L;
523                         break;
524                 }
525         }
526         return 0;
527 }
528
529 static int tda9887_status(struct tda9887 *t)
530 {
531         unsigned char buf[1];
532         int rc;
533
534         memset(buf,0,sizeof(buf));
535         if (1 != (rc = i2c_master_recv(&t->client,buf,1)))
536                 printk(PREFIX "i2c i/o error: rc == %d (should be 1)\n",rc);
537         dump_read_message(buf);
538         return 0;
539 }
540
541 static int tda9887_configure(struct tda9887 *t)
542 {
543         unsigned char buf[4];
544         int rc;
545
546         memset(buf,0,sizeof(buf));
547         tda9887_set_tvnorm(t,buf);
548         buf[1] |= cOutputPort1Inactive;
549         buf[1] |= cOutputPort2Inactive;
550         if (UNSET != t->pinnacle_id) {
551                 tda9887_set_pinnacle(t,buf);
552         }
553         tda9887_set_config(t,buf);
554         tda9887_set_insmod(t,buf);
555
556 #if 0
557         /* This as-is breaks some cards, must be fixed in a
558          * card-specific way, probably using TDA9887_SET_CONFIG to
559          * turn on/off port2 */
560         if (t->std & V4L2_STD_SECAM_L) {
561                 /* secam fixup (FIXME: move this to tvnorms array?) */
562                 buf[1] &= ~cOutputPort2Inactive;
563         }
564 #endif
565
566         dprintk(PREFIX "writing: b=0x%02x c=0x%02x e=0x%02x\n",
567                 buf[1],buf[2],buf[3]);
568         if (debug > 1)
569                 dump_write_message(buf);
570
571         if (4 != (rc = i2c_master_send(&t->client,buf,4)))
572                 printk(PREFIX "i2c i/o error: rc == %d (should be 4)\n",rc);
573
574         if (debug > 2) {
575                 msleep_interruptible(1000);
576                 tda9887_status(t);
577         }
578         return 0;
579 }
580
581 /* ---------------------------------------------------------------------- */
582
583 static int tda9887_attach(struct i2c_adapter *adap, int addr, int kind)
584 {
585         struct tda9887 *t;
586
587         client_template.adapter = adap;
588         client_template.addr    = addr;
589
590         printk(PREFIX "chip found @ 0x%x\n", addr<<1);
591
592         if (NULL == (t = kmalloc(sizeof(*t), GFP_KERNEL)))
593                 return -ENOMEM;
594         memset(t,0,sizeof(*t));
595         t->client      = client_template;
596         t->std         = 0;
597         t->pinnacle_id = UNSET;
598         i2c_set_clientdata(&t->client, t);
599         i2c_attach_client(&t->client);
600
601         return 0;
602 }
603
604 static int tda9887_probe(struct i2c_adapter *adap)
605 {
606 #ifdef I2C_CLASS_TV_ANALOG
607         if (adap->class & I2C_CLASS_TV_ANALOG)
608                 return i2c_probe(adap, &addr_data, tda9887_attach);
609 #else
610         switch (adap->id) {
611         case I2C_ALGO_BIT | I2C_HW_B_BT848:
612         case I2C_ALGO_BIT | I2C_HW_B_RIVA:
613         case I2C_ALGO_SAA7134:
614                 return i2c_probe(adap, &addr_data, tda9887_attach);
615                 break;
616         }
617 #endif
618         return 0;
619 }
620
621 static int tda9887_detach(struct i2c_client *client)
622 {
623         struct tda9887 *t = i2c_get_clientdata(client);
624
625         i2c_detach_client(client);
626         kfree(t);
627         return 0;
628 }
629
630 #define SWITCH_V4L2     if (!t->using_v4l2 && debug) \
631                           printk(PREFIX "switching to v4l2\n"); \
632                           t->using_v4l2 = 1;
633 #define CHECK_V4L2      if (t->using_v4l2) { if (debug) \
634                           printk(PREFIX "ignore v4l1 call\n"); \
635                           return 0; }
636
637 static int
638 tda9887_command(struct i2c_client *client, unsigned int cmd, void *arg)
639 {
640         struct tda9887 *t = i2c_get_clientdata(client);
641
642         switch (cmd) {
643
644         /* --- configuration --- */
645         case AUDC_SET_RADIO:
646                 t->radio = 1;
647                 tda9887_configure(t);
648                 break;
649
650         case AUDC_CONFIG_PINNACLE:
651         {
652                 int *i = arg;
653
654                 t->pinnacle_id = *i;
655                 tda9887_configure(t);
656                 break;
657         }
658         case TDA9887_SET_CONFIG:
659         {
660                 int *i = arg;
661
662                 t->config = *i;
663                 tda9887_configure(t);
664                 break;
665         }
666         /* --- v4l ioctls --- */
667         /* take care: bttv does userspace copying, we'll get a
668            kernel pointer here... */
669         case VIDIOCSCHAN:
670         {
671                 static const v4l2_std_id map[] = {
672                         [ VIDEO_MODE_PAL   ] = V4L2_STD_PAL,
673                         [ VIDEO_MODE_NTSC  ] = V4L2_STD_NTSC_M,
674                         [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
675                         [ 4 /* bttv */     ] = V4L2_STD_PAL_M,
676                         [ 5 /* bttv */     ] = V4L2_STD_PAL_N,
677                         [ 6 /* bttv */     ] = V4L2_STD_NTSC_M_JP,
678                 };
679                 struct video_channel *vc = arg;
680
681                 CHECK_V4L2;
682                 t->radio = 0;
683                 if (vc->norm < ARRAY_SIZE(map))
684                         t->std = map[vc->norm];
685                 tda9887_fixup_std(t);
686                 tda9887_configure(t);
687                 break;
688         }
689         case VIDIOC_S_STD:
690         {
691                 v4l2_std_id *id = arg;
692
693                 SWITCH_V4L2;
694                 t->radio = 0;
695                 t->std   = *id;
696                 tda9887_fixup_std(t);
697                 tda9887_configure(t);
698                 break;
699         }
700         case VIDIOC_S_FREQUENCY:
701         {
702                 struct v4l2_frequency *f = arg;
703
704                 SWITCH_V4L2;
705                 if (V4L2_TUNER_ANALOG_TV == f->type) {
706                         if (t->radio == 0)
707                                 return 0;
708                         t->radio = 0;
709                 }
710                 if (V4L2_TUNER_RADIO == f->type) {
711                         if (t->radio == 1)
712                                 return 0;
713                         t->radio = 1;
714                 }
715                 tda9887_configure(t);
716                 break;
717         }
718         case VIDIOC_G_TUNER:
719         {
720                 static int AFC_BITS_2_kHz[] = {
721                         -12500,  -37500,  -62500,  -97500,
722                         -112500, -137500, -162500, -187500,
723                         187500,  162500,  137500,  112500,
724                         97500 ,  62500,   37500 ,  12500
725                 };
726                 struct v4l2_tuner* tuner = arg;
727
728                 if (t->radio) {
729                         __u8 reg = 0;
730                         tuner->afc=0;
731                         if (1 == i2c_master_recv(&t->client,&reg,1))
732                                 tuner->afc = AFC_BITS_2_kHz[(reg>>1)&0x0f];
733                 }
734                 break;
735         }
736         default:
737                 /* nothing */
738                 break;
739         }
740         return 0;
741 }
742
743 static int tda9887_suspend(struct device * dev, pm_message_t state, u32 level)
744 {
745         dprintk("tda9887: suspend\n");
746         return 0;
747 }
748
749 static int tda9887_resume(struct device * dev, u32 level)
750 {
751         struct i2c_client *c = container_of(dev, struct i2c_client, dev);
752         struct tda9887 *t = i2c_get_clientdata(c);
753
754         dprintk("tda9887: resume\n");
755         tda9887_configure(t);
756         return 0;
757 }
758
759 /* ----------------------------------------------------------------------- */
760
761 static struct i2c_driver driver = {
762         .owner          = THIS_MODULE,
763         .name           = "i2c tda9887 driver",
764         .id             = -1, /* FIXME */
765         .flags          = I2C_DF_NOTIFY,
766         .attach_adapter = tda9887_probe,
767         .detach_client  = tda9887_detach,
768         .command        = tda9887_command,
769         .driver = {
770                 .suspend = tda9887_suspend,
771                 .resume  = tda9887_resume,
772         },
773 };
774 static struct i2c_client client_template =
775 {
776         I2C_DEVNAME("tda9887"),
777         .flags     = I2C_CLIENT_ALLOW_USE,
778         .driver    = &driver,
779 };
780
781 static int __init tda9887_init_module(void)
782 {
783         return i2c_add_driver(&driver);
784 }
785
786 static void __exit tda9887_cleanup_module(void)
787 {
788         i2c_del_driver(&driver);
789 }
790
791 module_init(tda9887_init_module);
792 module_exit(tda9887_cleanup_module);
793
794 /*
795  * Overrides for Emacs so that we follow Linus's tabbing style.
796  * ---------------------------------------------------------------------------
797  * Local variables:
798  * c-basic-offset: 8
799  * End:
800  */