blob: 79e0bd1aa70f36ca4cf6c4c83c8d9c64a99b9d18 [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};
Linus Torvalds1da177e2005-04-16 15:20:36 -070036I2C_CLIENT_INSMOD;
37
38/* insmod options */
39static unsigned int debug = 0;
40module_param(debug, int, 0644);
41MODULE_LICENSE("GPL");
42
43/* ---------------------------------------------------------------------- */
44
45#define UNSET (-1U)
46#define PREFIX "tda9885/6/7: "
47#define dprintk if (debug) printk
48
49struct tda9887 {
50 struct i2c_client client;
51 v4l2_std_id std;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -070052 enum tuner_mode mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 unsigned int config;
54 unsigned int pinnacle_id;
55 unsigned int using_v4l2;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -070056 unsigned int radio_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
59struct tvnorm {
60 v4l2_std_id std;
61 char *name;
62 unsigned char b;
63 unsigned char c;
64 unsigned char e;
65};
66
67static struct i2c_driver driver;
68static struct i2c_client client_template;
69
70/* ---------------------------------------------------------------------- */
71
72//
73// TDA defines
74//
75
76//// first reg (b)
77#define cVideoTrapBypassOFF 0x00 // bit b0
78#define cVideoTrapBypassON 0x01 // bit b0
79
80#define cAutoMuteFmInactive 0x00 // bit b1
81#define cAutoMuteFmActive 0x02 // bit b1
82
83#define cIntercarrier 0x00 // bit b2
84#define cQSS 0x04 // bit b2
85
86#define cPositiveAmTV 0x00 // bit b3:4
87#define cFmRadio 0x08 // bit b3:4
88#define cNegativeFmTV 0x10 // bit b3:4
89
90
91#define cForcedMuteAudioON 0x20 // bit b5
92#define cForcedMuteAudioOFF 0x00 // bit b5
93
94#define cOutputPort1Active 0x00 // bit b6
95#define cOutputPort1Inactive 0x40 // bit b6
96
97#define cOutputPort2Active 0x00 // bit b7
98#define cOutputPort2Inactive 0x80 // bit b7
99
100
101//// second reg (c)
102#define cDeemphasisOFF 0x00 // bit c5
103#define cDeemphasisON 0x20 // bit c5
104
105#define cDeemphasis75 0x00 // bit c6
106#define cDeemphasis50 0x40 // bit c6
107
108#define cAudioGain0 0x00 // bit c7
109#define cAudioGain6 0x80 // bit c7
110
111
112//// third reg (e)
113#define cAudioIF_4_5 0x00 // bit e0:1
114#define cAudioIF_5_5 0x01 // bit e0:1
115#define cAudioIF_6_0 0x02 // bit e0:1
116#define cAudioIF_6_5 0x03 // bit e0:1
117
118
119#define cVideoIF_58_75 0x00 // bit e2:4
120#define cVideoIF_45_75 0x04 // bit e2:4
121#define cVideoIF_38_90 0x08 // bit e2:4
122#define cVideoIF_38_00 0x0C // bit e2:4
123#define cVideoIF_33_90 0x10 // bit e2:4
124#define cVideoIF_33_40 0x14 // bit e2:4
125#define cRadioIF_45_75 0x18 // bit e2:4
126#define cRadioIF_38_90 0x1C // bit e2:4
127
128
129#define cTunerGainNormal 0x00 // bit e5
130#define cTunerGainLow 0x20 // bit e5
131
132#define cGating_18 0x00 // bit e6
133#define cGating_36 0x40 // bit e6
134
135#define cAgcOutON 0x80 // bit e7
136#define cAgcOutOFF 0x00 // bit e7
137
138/* ---------------------------------------------------------------------- */
139
140static struct tvnorm tvnorms[] = {
141 {
142 .std = V4L2_STD_PAL_BG,
143 .name = "PAL-BG",
144 .b = ( cNegativeFmTV |
145 cQSS ),
146 .c = ( cDeemphasisON |
147 cDeemphasis50 ),
148 .e = ( cAudioIF_5_5 |
149 cVideoIF_38_90 ),
150 },{
151 .std = V4L2_STD_PAL_I,
152 .name = "PAL-I",
153 .b = ( cNegativeFmTV |
154 cQSS ),
155 .c = ( cDeemphasisON |
156 cDeemphasis50 ),
157 .e = ( cAudioIF_6_0 |
158 cVideoIF_38_90 ),
159 },{
160 .std = V4L2_STD_PAL_DK,
161 .name = "PAL-DK",
162 .b = ( cNegativeFmTV |
163 cQSS ),
164 .c = ( cDeemphasisON |
165 cDeemphasis50 ),
166 .e = ( cAudioIF_6_5 |
167 cVideoIF_38_00 ),
168 },{
169 .std = V4L2_STD_PAL_M | V4L2_STD_PAL_N,
170 .name = "PAL-M/N",
171 .b = ( cNegativeFmTV |
172 cQSS ),
173 .c = ( cDeemphasisON |
174 cDeemphasis75 ),
175 .e = ( cAudioIF_4_5 |
176 cVideoIF_45_75 ),
177 },{
178 .std = V4L2_STD_SECAM_L,
179 .name = "SECAM-L",
180 .b = ( cPositiveAmTV |
181 cQSS ),
182 .e = ( cAudioIF_6_5 |
183 cVideoIF_38_90 ),
184 },{
185 .std = V4L2_STD_SECAM_DK,
186 .name = "SECAM-DK",
187 .b = ( cNegativeFmTV |
188 cQSS ),
189 .c = ( cDeemphasisON |
190 cDeemphasis50 ),
191 .e = ( cAudioIF_6_5 |
192 cVideoIF_38_00 ),
193 },{
194 .std = V4L2_STD_NTSC_M,
195 .name = "NTSC-M",
196 .b = ( cNegativeFmTV |
197 cQSS ),
198 .c = ( cDeemphasisON |
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700199 cDeemphasis75 ),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 .e = ( cGating_36 |
201 cAudioIF_4_5 |
202 cVideoIF_45_75 ),
203 },{
204 .std = V4L2_STD_NTSC_M_JP,
205 .name = "NTSC-JP",
206 .b = ( cNegativeFmTV |
207 cQSS ),
208 .c = ( cDeemphasisON |
209 cDeemphasis50 ),
210 .e = ( cGating_36 |
211 cAudioIF_4_5 |
212 cVideoIF_58_75 ),
213 }
214};
215
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700216static struct tvnorm radio_stereo = {
217 .name = "Radio Stereo",
218 .b = ( cFmRadio |
219 cQSS ),
220 .c = ( cDeemphasisOFF |
221 cAudioGain6 ),
222 .e = ( cAudioIF_5_5 |
223 cRadioIF_38_90 ),
224};
225
226static struct tvnorm radio_mono = {
227 .name = "Radio Mono",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 .b = ( cFmRadio |
229 cQSS ),
230 .c = ( cDeemphasisON |
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700231 cDeemphasis50),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 .e = ( cAudioIF_5_5 |
233 cRadioIF_38_90 ),
234};
235
236/* ---------------------------------------------------------------------- */
237
238static void dump_read_message(unsigned char *buf)
239{
240 static char *afc[16] = {
241 "- 12.5 kHz",
242 "- 37.5 kHz",
243 "- 62.5 kHz",
244 "- 87.5 kHz",
245 "-112.5 kHz",
246 "-137.5 kHz",
247 "-162.5 kHz",
248 "-187.5 kHz [min]",
249 "+187.5 kHz [max]",
250 "+162.5 kHz",
251 "+137.5 kHz",
252 "+112.5 kHz",
253 "+ 87.5 kHz",
254 "+ 62.5 kHz",
255 "+ 37.5 kHz",
256 "+ 12.5 kHz",
257 };
258 printk(PREFIX "read: 0x%2x\n", buf[0]);
259 printk(" after power on : %s\n", (buf[0] & 0x01) ? "yes" : "no");
260 printk(" afc : %s\n", afc[(buf[0] >> 1) & 0x0f]);
261 printk(" fmif level : %s\n", (buf[0] & 0x20) ? "high" : "low");
262 printk(" afc window : %s\n", (buf[0] & 0x40) ? "in" : "out");
263 printk(" vfi level : %s\n", (buf[0] & 0x80) ? "high" : "low");
264}
265
266static void dump_write_message(unsigned char *buf)
267{
268 static char *sound[4] = {
269 "AM/TV",
270 "FM/radio",
271 "FM/TV",
272 "FM/radio"
273 };
274 static char *adjust[32] = {
275 "-16", "-15", "-14", "-13", "-12", "-11", "-10", "-9",
276 "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1",
277 "0", "+1", "+2", "+3", "+4", "+5", "+6", "+7",
278 "+8", "+9", "+10", "+11", "+12", "+13", "+14", "+15"
279 };
280 static char *deemph[4] = {
281 "no", "no", "75", "50"
282 };
283 static char *carrier[4] = {
284 "4.5 MHz",
285 "5.5 MHz",
286 "6.0 MHz",
287 "6.5 MHz / AM"
288 };
289 static char *vif[8] = {
290 "58.75 MHz",
291 "45.75 MHz",
292 "38.9 MHz",
293 "38.0 MHz",
294 "33.9 MHz",
295 "33.4 MHz",
296 "45.75 MHz + pin13",
297 "38.9 MHz + pin13",
298 };
299 static char *rif[4] = {
300 "44 MHz",
301 "52 MHz",
302 "52 MHz",
303 "44 MHz",
304 };
305
306 printk(PREFIX "write: byte B 0x%02x\n",buf[1]);
307 printk(" B0 video mode : %s\n",
308 (buf[1] & 0x01) ? "video trap" : "sound trap");
309 printk(" B1 auto mute fm : %s\n",
310 (buf[1] & 0x02) ? "yes" : "no");
311 printk(" B2 carrier mode : %s\n",
312 (buf[1] & 0x04) ? "QSS" : "Intercarrier");
313 printk(" B3-4 tv sound/radio : %s\n",
314 sound[(buf[1] & 0x18) >> 3]);
315 printk(" B5 force mute audio: %s\n",
316 (buf[1] & 0x20) ? "yes" : "no");
317 printk(" B6 output port 1 : %s\n",
318 (buf[1] & 0x40) ? "high (inactive)" : "low (active)");
319 printk(" B7 output port 2 : %s\n",
320 (buf[1] & 0x80) ? "high (inactive)" : "low (active)");
321
322 printk(PREFIX "write: byte C 0x%02x\n",buf[2]);
323 printk(" C0-4 top adjustment : %s dB\n", adjust[buf[2] & 0x1f]);
324 printk(" C5-6 de-emphasis : %s\n", deemph[(buf[2] & 0x60) >> 5]);
325 printk(" C7 audio gain : %s\n",
326 (buf[2] & 0x80) ? "-6" : "0");
327
328 printk(PREFIX "write: byte E 0x%02x\n",buf[3]);
329 printk(" E0-1 sound carrier : %s\n",
330 carrier[(buf[3] & 0x03)]);
331 printk(" E6 l pll ganting : %s\n",
332 (buf[3] & 0x40) ? "36" : "13");
333
334 if (buf[1] & 0x08) {
335 /* radio */
336 printk(" E2-4 video if : %s\n",
337 rif[(buf[3] & 0x0c) >> 2]);
338 printk(" E7 vif agc output : %s\n",
339 (buf[3] & 0x80)
340 ? ((buf[3] & 0x10) ? "fm-agc radio" : "sif-agc radio")
341 : "fm radio carrier afc");
342 } else {
343 /* video */
344 printk(" E2-4 video if : %s\n",
345 vif[(buf[3] & 0x1c) >> 2]);
346 printk(" E5 tuner gain : %s\n",
347 (buf[3] & 0x80)
348 ? ((buf[3] & 0x20) ? "external" : "normal")
349 : ((buf[3] & 0x20) ? "minimum" : "normal"));
350 printk(" E7 vif agc output : %s\n",
351 (buf[3] & 0x80)
352 ? ((buf[3] & 0x20)
353 ? "pin3 port, pin22 vif agc out"
354 : "pin22 port, pin3 vif acg ext in")
355 : "pin3+pin22 port");
356 }
357 printk("--\n");
358}
359
360/* ---------------------------------------------------------------------- */
361
362static int tda9887_set_tvnorm(struct tda9887 *t, char *buf)
363{
364 struct tvnorm *norm = NULL;
365 int i;
366
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700367 if (t->mode == T_RADIO) {
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700368 if (t->radio_mode == V4L2_TUNER_MODE_MONO)
369 norm = &radio_mono;
370 else
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700371 norm = &radio_stereo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 } else {
373 for (i = 0; i < ARRAY_SIZE(tvnorms); i++) {
374 if (tvnorms[i].std & t->std) {
375 norm = tvnorms+i;
376 break;
377 }
378 }
379 }
380 if (NULL == norm) {
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700381 dprintk(PREFIX "Unsupported tvnorm entry - audio muted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return -1;
383 }
384
385 dprintk(PREFIX "configure for: %s\n",norm->name);
386 buf[1] = norm->b;
387 buf[2] = norm->c;
388 buf[3] = norm->e;
389 return 0;
390}
391
392static unsigned int port1 = UNSET;
393static unsigned int port2 = UNSET;
394static unsigned int qss = UNSET;
395static unsigned int adjust = 0x10;
396module_param(port1, int, 0644);
397module_param(port2, int, 0644);
398module_param(qss, int, 0644);
399module_param(adjust, int, 0644);
400
401static int tda9887_set_insmod(struct tda9887 *t, char *buf)
402{
403 if (UNSET != port1) {
404 if (port1)
405 buf[1] |= cOutputPort1Inactive;
406 else
407 buf[1] &= ~cOutputPort1Inactive;
408 }
409 if (UNSET != port2) {
410 if (port2)
411 buf[1] |= cOutputPort2Inactive;
412 else
413 buf[1] &= ~cOutputPort2Inactive;
414 }
415
416 if (UNSET != qss) {
417 if (qss)
418 buf[1] |= cQSS;
419 else
420 buf[1] &= ~cQSS;
421 }
422
423 if (adjust >= 0x00 && adjust < 0x20)
424 buf[2] |= adjust;
425 return 0;
426}
427
428static int tda9887_set_config(struct tda9887 *t, char *buf)
429{
430 if (t->config & TDA9887_PORT1_ACTIVE)
431 buf[1] &= ~cOutputPort1Inactive;
432 if (t->config & TDA9887_PORT1_INACTIVE)
433 buf[1] |= cOutputPort1Inactive;
434 if (t->config & TDA9887_PORT2_ACTIVE)
435 buf[1] &= ~cOutputPort2Inactive;
436 if (t->config & TDA9887_PORT2_INACTIVE)
437 buf[1] |= cOutputPort2Inactive;
438
439 if (t->config & TDA9887_QSS)
440 buf[1] |= cQSS;
441 if (t->config & TDA9887_INTERCARRIER)
442 buf[1] &= ~cQSS;
443
444 if (t->config & TDA9887_AUTOMUTE)
445 buf[1] |= cAutoMuteFmActive;
446 if (t->config & TDA9887_DEEMPHASIS_MASK) {
447 buf[2] &= ~0x60;
448 switch (t->config & TDA9887_DEEMPHASIS_MASK) {
449 case TDA9887_DEEMPHASIS_NONE:
450 buf[2] |= cDeemphasisOFF;
451 break;
452 case TDA9887_DEEMPHASIS_50:
453 buf[2] |= cDeemphasisON | cDeemphasis50;
454 break;
455 case TDA9887_DEEMPHASIS_75:
456 buf[2] |= cDeemphasisON | cDeemphasis75;
457 break;
458 }
459 }
460 return 0;
461}
462
463/* ---------------------------------------------------------------------- */
464
465static int tda9887_set_pinnacle(struct tda9887 *t, char *buf)
466{
467 unsigned int bCarrierMode = UNSET;
468
469 if (t->std & V4L2_STD_625_50) {
470 if ((1 == t->pinnacle_id) || (7 == t->pinnacle_id)) {
471 bCarrierMode = cIntercarrier;
472 } else {
473 bCarrierMode = cQSS;
474 }
475 }
476 if (t->std & V4L2_STD_525_60) {
477 if ((5 == t->pinnacle_id) || (6 == t->pinnacle_id)) {
478 bCarrierMode = cIntercarrier;
479 } else {
480 bCarrierMode = cQSS;
481 }
482 }
483
484 if (bCarrierMode != UNSET) {
485 buf[1] &= ~0x04;
486 buf[1] |= bCarrierMode;
487 }
488 return 0;
489}
490
491/* ---------------------------------------------------------------------- */
492
493static char pal[] = "-";
Bert Wesarg975e0462005-04-16 15:25:43 -0700494module_param_string(pal, pal, sizeof(pal), 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static char secam[] = "-";
Bert Wesarg975e0462005-04-16 15:25:43 -0700496module_param_string(secam, secam, sizeof(secam), 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498static int tda9887_fixup_std(struct tda9887 *t)
499{
500 /* get more precise norm info from insmod option */
501 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
502 switch (pal[0]) {
503 case 'b':
504 case 'B':
505 case 'g':
506 case 'G':
507 dprintk(PREFIX "insmod fixup: PAL => PAL-BG\n");
508 t->std = V4L2_STD_PAL_BG;
509 break;
510 case 'i':
511 case 'I':
512 dprintk(PREFIX "insmod fixup: PAL => PAL-I\n");
513 t->std = V4L2_STD_PAL_I;
514 break;
515 case 'd':
516 case 'D':
517 case 'k':
518 case 'K':
519 dprintk(PREFIX "insmod fixup: PAL => PAL-DK\n");
520 t->std = V4L2_STD_PAL_DK;
521 break;
522 }
523 }
524 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
525 switch (secam[0]) {
526 case 'd':
527 case 'D':
528 case 'k':
529 case 'K':
530 dprintk(PREFIX "insmod fixup: SECAM => SECAM-DK\n");
531 t->std = V4L2_STD_SECAM_DK;
532 break;
533 case 'l':
534 case 'L':
535 dprintk(PREFIX "insmod fixup: SECAM => SECAM-L\n");
536 t->std = V4L2_STD_SECAM_L;
537 break;
538 }
539 }
540 return 0;
541}
542
543static int tda9887_status(struct tda9887 *t)
544{
545 unsigned char buf[1];
546 int rc;
547
548 memset(buf,0,sizeof(buf));
549 if (1 != (rc = i2c_master_recv(&t->client,buf,1)))
550 printk(PREFIX "i2c i/o error: rc == %d (should be 1)\n",rc);
551 dump_read_message(buf);
552 return 0;
553}
554
555static int tda9887_configure(struct tda9887 *t)
556{
557 unsigned char buf[4];
558 int rc;
559
560 memset(buf,0,sizeof(buf));
561 tda9887_set_tvnorm(t,buf);
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 buf[1] |= cOutputPort1Inactive;
564 buf[1] |= cOutputPort2Inactive;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (UNSET != t->pinnacle_id) {
567 tda9887_set_pinnacle(t,buf);
568 }
569 tda9887_set_config(t,buf);
570 tda9887_set_insmod(t,buf);
571
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700572 if (t->mode == T_STANDBY) {
573 buf[1] |= cForcedMuteAudioON;
574 }
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 dprintk(PREFIX "writing: b=0x%02x c=0x%02x e=0x%02x\n",
578 buf[1],buf[2],buf[3]);
579 if (debug > 1)
580 dump_write_message(buf);
581
582 if (4 != (rc = i2c_master_send(&t->client,buf,4)))
583 printk(PREFIX "i2c i/o error: rc == %d (should be 4)\n",rc);
584
585 if (debug > 2) {
586 msleep_interruptible(1000);
587 tda9887_status(t);
588 }
589 return 0;
590}
591
592/* ---------------------------------------------------------------------- */
593
594static int tda9887_attach(struct i2c_adapter *adap, int addr, int kind)
595{
596 struct tda9887 *t;
597
598 client_template.adapter = adap;
599 client_template.addr = addr;
600
601 printk(PREFIX "chip found @ 0x%x\n", addr<<1);
602
603 if (NULL == (t = kmalloc(sizeof(*t), GFP_KERNEL)))
604 return -ENOMEM;
605 memset(t,0,sizeof(*t));
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 t->client = client_template;
608 t->std = 0;
609 t->pinnacle_id = UNSET;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700610 t->radio_mode = V4L2_TUNER_MODE_STEREO;
611
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700612 i2c_set_clientdata(&t->client, t);
613 i2c_attach_client(&t->client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 return 0;
616}
617
618static int tda9887_probe(struct i2c_adapter *adap)
619{
620#ifdef I2C_CLASS_TV_ANALOG
621 if (adap->class & I2C_CLASS_TV_ANALOG)
622 return i2c_probe(adap, &addr_data, tda9887_attach);
623#else
624 switch (adap->id) {
Jean Delvarec7a46532005-08-11 23:41:56 +0200625 case I2C_HW_B_BT848:
626 case I2C_HW_B_RIVA:
Jean Delvare1684a9842005-08-11 23:51:10 +0200627 case I2C_HW_SAA7134:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return i2c_probe(adap, &addr_data, tda9887_attach);
629 break;
630 }
631#endif
632 return 0;
633}
634
635static int tda9887_detach(struct i2c_client *client)
636{
637 struct tda9887 *t = i2c_get_clientdata(client);
638
639 i2c_detach_client(client);
640 kfree(t);
641 return 0;
642}
643
644#define SWITCH_V4L2 if (!t->using_v4l2 && debug) \
645 printk(PREFIX "switching to v4l2\n"); \
646 t->using_v4l2 = 1;
647#define CHECK_V4L2 if (t->using_v4l2) { if (debug) \
648 printk(PREFIX "ignore v4l1 call\n"); \
649 return 0; }
650
651static int
652tda9887_command(struct i2c_client *client, unsigned int cmd, void *arg)
653{
654 struct tda9887 *t = i2c_get_clientdata(client);
655
656 switch (cmd) {
657
658 /* --- configuration --- */
659 case AUDC_SET_RADIO:
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700660 {
661 t->mode = T_RADIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 tda9887_configure(t);
663 break;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700664 }
665 case TUNER_SET_STANDBY:
666 {
667 t->mode = T_STANDBY;
668 tda9887_configure(t);
669 break;
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 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;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700703 t->mode = T_ANALOG_TV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 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;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700715 t->mode = T_ANALOG_TV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 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) {
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700727 if (t->mode == T_ANALOG_TV)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return 0;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700729 t->mode = T_ANALOG_TV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 if (V4L2_TUNER_RADIO == f->type) {
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700732 if (t->mode == T_RADIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 0;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700734 t->mode = T_RADIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
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
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700749 if (t->mode == T_RADIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 __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
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700761 if (t->mode == T_RADIO) {
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700762 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
Pavel Machek829ca9a2005-09-03 15:56:56 -0700774static int tda9887_suspend(struct device * dev, pm_message_t 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{
Jean Delvarefae91e72005-08-15 19:57:04 +0200807 .name = "tda9887",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 .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 */