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