blob: 33d6ee6cde4888369bc5a5eed3e4b2323521bf79 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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 */
30static unsigned short normal_i2c[] = {
31 0x84 >>1,
32 0x86 >>1,
33 0x96 >>1,
34 I2C_CLIENT_END,
35};
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -070036static unsigned short normal_i2c_range[] = {I2C_CLIENT_END,I2C_CLIENT_END};
Linus Torvalds1da177e2005-04-16 15:20:36 -070037I2C_CLIENT_INSMOD;
38
39/* insmod options */
40static unsigned int debug = 0;
41module_param(debug, int, 0644);
42MODULE_LICENSE("GPL");
43
44/* ---------------------------------------------------------------------- */
45
46#define UNSET (-1U)
47#define PREFIX "tda9885/6/7: "
48#define dprintk if (debug) printk
49
50struct tda9887 {
51 struct i2c_client client;
52 v4l2_std_id std;
53 unsigned int radio;
54 unsigned int config;
55 unsigned int pinnacle_id;
56 unsigned int using_v4l2;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -070057 unsigned int radio_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058};
59
60struct tvnorm {
61 v4l2_std_id std;
62 char *name;
63 unsigned char b;
64 unsigned char c;
65 unsigned char e;
66};
67
68static struct i2c_driver driver;
69static 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
141static 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 cDeemphasis50 ),
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
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700217static 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
227static struct tvnorm radio_mono = {
228 .name = "Radio Mono",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 .b = ( cFmRadio |
230 cQSS ),
231 .c = ( cDeemphasisON |
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700232 cDeemphasis50),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 .e = ( cAudioIF_5_5 |
234 cRadioIF_38_90 ),
235};
236
237/* ---------------------------------------------------------------------- */
238
239static 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
267static 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
363static int tda9887_set_tvnorm(struct tda9887 *t, char *buf)
364{
365 struct tvnorm *norm = NULL;
366 int i;
367
368 if (t->radio) {
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700369 if (t->radio_mode == V4L2_TUNER_MODE_MONO)
370 norm = &radio_mono;
371 else
372 norm = &radio_stereo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 } 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 "Oops: no tvnorm entry found\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
393static unsigned int port1 = UNSET;
394static unsigned int port2 = UNSET;
395static unsigned int qss = UNSET;
396static unsigned int adjust = 0x10;
397module_param(port1, int, 0644);
398module_param(port2, int, 0644);
399module_param(qss, int, 0644);
400module_param(adjust, int, 0644);
401
402static 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
429static 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
466static 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
494static char pal[] = "-";
Bert Wesarg975e0462005-04-16 15:25:43 -0700495module_param_string(pal, pal, sizeof(pal), 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496static char secam[] = "-";
Bert Wesarg975e0462005-04-16 15:25:43 -0700497module_param_string(secam, secam, sizeof(secam), 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499static 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 }
524 }
525 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
526 switch (secam[0]) {
527 case 'd':
528 case 'D':
529 case 'k':
530 case 'K':
531 dprintk(PREFIX "insmod fixup: SECAM => SECAM-DK\n");
532 t->std = V4L2_STD_SECAM_DK;
533 break;
534 case 'l':
535 case 'L':
536 dprintk(PREFIX "insmod fixup: SECAM => SECAM-L\n");
537 t->std = V4L2_STD_SECAM_L;
538 break;
539 }
540 }
541 return 0;
542}
543
544static int tda9887_status(struct tda9887 *t)
545{
546 unsigned char buf[1];
547 int rc;
548
549 memset(buf,0,sizeof(buf));
550 if (1 != (rc = i2c_master_recv(&t->client,buf,1)))
551 printk(PREFIX "i2c i/o error: rc == %d (should be 1)\n",rc);
552 dump_read_message(buf);
553 return 0;
554}
555
556static int tda9887_configure(struct tda9887 *t)
557{
558 unsigned char buf[4];
559 int rc;
560
561 memset(buf,0,sizeof(buf));
562 tda9887_set_tvnorm(t,buf);
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 buf[1] |= cOutputPort1Inactive;
565 buf[1] |= cOutputPort2Inactive;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (UNSET != t->pinnacle_id) {
568 tda9887_set_pinnacle(t,buf);
569 }
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 tda9887_set_config(t,buf);
572 tda9887_set_insmod(t,buf);
573
574#if 0
575 /* This as-is breaks some cards, must be fixed in a
576 * card-specific way, probably using TDA9887_SET_CONFIG to
577 * turn on/off port2 */
578 if (t->std & V4L2_STD_SECAM_L) {
579 /* secam fixup (FIXME: move this to tvnorms array?) */
580 buf[1] &= ~cOutputPort2Inactive;
581 }
582#endif
583
584 dprintk(PREFIX "writing: b=0x%02x c=0x%02x e=0x%02x\n",
585 buf[1],buf[2],buf[3]);
586 if (debug > 1)
587 dump_write_message(buf);
588
589 if (4 != (rc = i2c_master_send(&t->client,buf,4)))
590 printk(PREFIX "i2c i/o error: rc == %d (should be 4)\n",rc);
591
592 if (debug > 2) {
593 msleep_interruptible(1000);
594 tda9887_status(t);
595 }
596 return 0;
597}
598
599/* ---------------------------------------------------------------------- */
600
601static int tda9887_attach(struct i2c_adapter *adap, int addr, int kind)
602{
603 struct tda9887 *t;
604
605 client_template.adapter = adap;
606 client_template.addr = addr;
607
608 printk(PREFIX "chip found @ 0x%x\n", addr<<1);
609
610 if (NULL == (t = kmalloc(sizeof(*t), GFP_KERNEL)))
611 return -ENOMEM;
612 memset(t,0,sizeof(*t));
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 t->client = client_template;
615 t->std = 0;
616 t->pinnacle_id = UNSET;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700617 t->radio_mode = V4L2_TUNER_MODE_STEREO;
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 i2c_set_clientdata(&t->client, t);
620 i2c_attach_client(&t->client);
621
622 return 0;
623}
624
625static int tda9887_probe(struct i2c_adapter *adap)
626{
627#ifdef I2C_CLASS_TV_ANALOG
628 if (adap->class & I2C_CLASS_TV_ANALOG)
629 return i2c_probe(adap, &addr_data, tda9887_attach);
630#else
631 switch (adap->id) {
632 case I2C_ALGO_BIT | I2C_HW_B_BT848:
633 case I2C_ALGO_BIT | I2C_HW_B_RIVA:
634 case I2C_ALGO_SAA7134:
635 return i2c_probe(adap, &addr_data, tda9887_attach);
636 break;
637 }
638#endif
639 return 0;
640}
641
642static int tda9887_detach(struct i2c_client *client)
643{
644 struct tda9887 *t = i2c_get_clientdata(client);
645
646 i2c_detach_client(client);
647 kfree(t);
648 return 0;
649}
650
651#define SWITCH_V4L2 if (!t->using_v4l2 && debug) \
652 printk(PREFIX "switching to v4l2\n"); \
653 t->using_v4l2 = 1;
654#define CHECK_V4L2 if (t->using_v4l2) { if (debug) \
655 printk(PREFIX "ignore v4l1 call\n"); \
656 return 0; }
657
658static int
659tda9887_command(struct i2c_client *client, unsigned int cmd, void *arg)
660{
661 struct tda9887 *t = i2c_get_clientdata(client);
662
663 switch (cmd) {
664
665 /* --- configuration --- */
666 case AUDC_SET_RADIO:
667 t->radio = 1;
668 tda9887_configure(t);
669 break;
670
671 case AUDC_CONFIG_PINNACLE:
672 {
673 int *i = arg;
674
675 t->pinnacle_id = *i;
676 tda9887_configure(t);
677 break;
678 }
679 case TDA9887_SET_CONFIG:
680 {
681 int *i = arg;
682
683 t->config = *i;
684 tda9887_configure(t);
685 break;
686 }
687 /* --- v4l ioctls --- */
688 /* take care: bttv does userspace copying, we'll get a
689 kernel pointer here... */
690 case VIDIOCSCHAN:
691 {
692 static const v4l2_std_id map[] = {
693 [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
694 [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
695 [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
696 [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
697 [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
698 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
699 };
700 struct video_channel *vc = arg;
701
702 CHECK_V4L2;
703 t->radio = 0;
704 if (vc->norm < ARRAY_SIZE(map))
705 t->std = map[vc->norm];
706 tda9887_fixup_std(t);
707 tda9887_configure(t);
708 break;
709 }
710 case VIDIOC_S_STD:
711 {
712 v4l2_std_id *id = arg;
713
714 SWITCH_V4L2;
715 t->radio = 0;
716 t->std = *id;
717 tda9887_fixup_std(t);
718 tda9887_configure(t);
719 break;
720 }
721 case VIDIOC_S_FREQUENCY:
722 {
723 struct v4l2_frequency *f = arg;
724
725 SWITCH_V4L2;
726 if (V4L2_TUNER_ANALOG_TV == f->type) {
727 if (t->radio == 0)
728 return 0;
729 t->radio = 0;
730 }
731 if (V4L2_TUNER_RADIO == f->type) {
732 if (t->radio == 1)
733 return 0;
734 t->radio = 1;
735 }
736 tda9887_configure(t);
737 break;
738 }
739 case VIDIOC_G_TUNER:
740 {
741 static int AFC_BITS_2_kHz[] = {
742 -12500, -37500, -62500, -97500,
743 -112500, -137500, -162500, -187500,
744 187500, 162500, 137500, 112500,
745 97500 , 62500, 37500 , 12500
746 };
747 struct v4l2_tuner* tuner = arg;
748
749 if (t->radio) {
750 __u8 reg = 0;
751 tuner->afc=0;
752 if (1 == i2c_master_recv(&t->client,&reg,1))
753 tuner->afc = AFC_BITS_2_kHz[(reg>>1)&0x0f];
754 }
755 break;
756 }
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700757 case VIDIOC_S_TUNER:
758 {
759 struct v4l2_tuner* tuner = arg;
760
761 if (t->radio) {
762 t->radio_mode = tuner->audmode;
763 tda9887_configure (t);
764 }
765 break;
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 default:
768 /* nothing */
769 break;
770 }
771 return 0;
772}
773
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700774static int tda9887_suspend(struct device * dev, u32 state, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 dprintk("tda9887: suspend\n");
777 return 0;
778}
779
780static int tda9887_resume(struct device * dev, u32 level)
781{
782 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
783 struct tda9887 *t = i2c_get_clientdata(c);
784
785 dprintk("tda9887: resume\n");
786 tda9887_configure(t);
787 return 0;
788}
789
790/* ----------------------------------------------------------------------- */
791
792static struct i2c_driver driver = {
793 .owner = THIS_MODULE,
794 .name = "i2c tda9887 driver",
795 .id = -1, /* FIXME */
796 .flags = I2C_DF_NOTIFY,
797 .attach_adapter = tda9887_probe,
798 .detach_client = tda9887_detach,
799 .command = tda9887_command,
800 .driver = {
801 .suspend = tda9887_suspend,
802 .resume = tda9887_resume,
803 },
804};
805static struct i2c_client client_template =
806{
807 I2C_DEVNAME("tda9887"),
808 .flags = I2C_CLIENT_ALLOW_USE,
809 .driver = &driver,
810};
811
812static int __init tda9887_init_module(void)
813{
814 return i2c_add_driver(&driver);
815}
816
817static void __exit tda9887_cleanup_module(void)
818{
819 i2c_del_driver(&driver);
820}
821
822module_init(tda9887_init_module);
823module_exit(tda9887_cleanup_module);
824
825/*
826 * Overrides for Emacs so that we follow Linus's tabbing style.
827 * ---------------------------------------------------------------------------
828 * Local variables:
829 * c-basic-offset: 8
830 * End:
831 */