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