blob: 93bf10436b82504e14e4e8a70d27ae93e7122ee9 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -080015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016/* 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)
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -070026 - Hauppauge PVR-150/500 (possibly more)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027*/
28
29
30/* Addresses to scan */
31static unsigned short normal_i2c[] = {
32 0x84 >>1,
33 0x86 >>1,
34 0x96 >>1,
35 I2C_CLIENT_END,
36};
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)
Hans Verkuil4eb0c142005-11-08 21:37:18 -080047#define tda9887_info(fmt, arg...) do {\
48 printk(KERN_INFO "%s %d-%04x: " fmt, t->client.name, \
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080049 i2c_adapter_id(t->client.adapter), t->client.addr , ##arg); } while (0)
Hans Verkuil4eb0c142005-11-08 21:37:18 -080050#define tda9887_dbg(fmt, arg...) do {\
51 if (debug) \
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080052 printk(KERN_INFO "%s %d-%04x: " fmt, t->client.name, \
53 i2c_adapter_id(t->client.adapter), t->client.addr , ##arg); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55struct tda9887 {
56 struct i2c_client client;
57 v4l2_std_id std;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -070058 enum tuner_mode mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned int config;
60 unsigned int pinnacle_id;
61 unsigned int using_v4l2;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -070062 unsigned int radio_mode;
Hans Verkuil299392b2005-11-08 21:37:42 -080063 unsigned char data[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
66struct tvnorm {
67 v4l2_std_id std;
68 char *name;
69 unsigned char b;
70 unsigned char c;
71 unsigned char e;
72};
73
74static struct i2c_driver driver;
75static struct i2c_client client_template;
76
77/* ---------------------------------------------------------------------- */
78
79//
80// TDA defines
81//
82
83//// first reg (b)
84#define cVideoTrapBypassOFF 0x00 // bit b0
85#define cVideoTrapBypassON 0x01 // bit b0
86
87#define cAutoMuteFmInactive 0x00 // bit b1
88#define cAutoMuteFmActive 0x02 // bit b1
89
90#define cIntercarrier 0x00 // bit b2
91#define cQSS 0x04 // bit b2
92
93#define cPositiveAmTV 0x00 // bit b3:4
94#define cFmRadio 0x08 // bit b3:4
95#define cNegativeFmTV 0x10 // bit b3:4
96
97
98#define cForcedMuteAudioON 0x20 // bit b5
99#define cForcedMuteAudioOFF 0x00 // bit b5
100
101#define cOutputPort1Active 0x00 // bit b6
102#define cOutputPort1Inactive 0x40 // bit b6
103
104#define cOutputPort2Active 0x00 // bit b7
105#define cOutputPort2Inactive 0x80 // bit b7
106
107
108//// second reg (c)
109#define cDeemphasisOFF 0x00 // bit c5
110#define cDeemphasisON 0x20 // bit c5
111
112#define cDeemphasis75 0x00 // bit c6
113#define cDeemphasis50 0x40 // bit c6
114
115#define cAudioGain0 0x00 // bit c7
116#define cAudioGain6 0x80 // bit c7
117
118
119//// third reg (e)
120#define cAudioIF_4_5 0x00 // bit e0:1
121#define cAudioIF_5_5 0x01 // bit e0:1
122#define cAudioIF_6_0 0x02 // bit e0:1
123#define cAudioIF_6_5 0x03 // bit e0:1
124
125
126#define cVideoIF_58_75 0x00 // bit e2:4
127#define cVideoIF_45_75 0x04 // bit e2:4
128#define cVideoIF_38_90 0x08 // bit e2:4
129#define cVideoIF_38_00 0x0C // bit e2:4
130#define cVideoIF_33_90 0x10 // bit e2:4
131#define cVideoIF_33_40 0x14 // bit e2:4
132#define cRadioIF_45_75 0x18 // bit e2:4
133#define cRadioIF_38_90 0x1C // bit e2:4
134
135
136#define cTunerGainNormal 0x00 // bit e5
137#define cTunerGainLow 0x20 // bit e5
138
139#define cGating_18 0x00 // bit e6
140#define cGating_36 0x40 // bit e6
141
142#define cAgcOutON 0x80 // bit e7
143#define cAgcOutOFF 0x00 // bit e7
144
145/* ---------------------------------------------------------------------- */
146
147static struct tvnorm tvnorms[] = {
148 {
149 .std = V4L2_STD_PAL_BG,
150 .name = "PAL-BG",
151 .b = ( cNegativeFmTV |
152 cQSS ),
153 .c = ( cDeemphasisON |
154 cDeemphasis50 ),
155 .e = ( cAudioIF_5_5 |
156 cVideoIF_38_90 ),
157 },{
158 .std = V4L2_STD_PAL_I,
159 .name = "PAL-I",
160 .b = ( cNegativeFmTV |
161 cQSS ),
162 .c = ( cDeemphasisON |
163 cDeemphasis50 ),
164 .e = ( cAudioIF_6_0 |
165 cVideoIF_38_90 ),
166 },{
167 .std = V4L2_STD_PAL_DK,
168 .name = "PAL-DK",
169 .b = ( cNegativeFmTV |
170 cQSS ),
171 .c = ( cDeemphasisON |
172 cDeemphasis50 ),
173 .e = ( cAudioIF_6_5 |
174 cVideoIF_38_00 ),
175 },{
176 .std = V4L2_STD_PAL_M | V4L2_STD_PAL_N,
177 .name = "PAL-M/N",
178 .b = ( cNegativeFmTV |
179 cQSS ),
180 .c = ( cDeemphasisON |
181 cDeemphasis75 ),
182 .e = ( cAudioIF_4_5 |
183 cVideoIF_45_75 ),
184 },{
185 .std = V4L2_STD_SECAM_L,
186 .name = "SECAM-L",
187 .b = ( cPositiveAmTV |
188 cQSS ),
Nickolay V. Shmyrev3375c392005-11-08 21:37:05 -0800189 .e = ( cGating_36 |
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800190 cAudioIF_6_5 |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 cVideoIF_38_90 ),
192 },{
Mauro Carvalho Chehabf3c59872006-01-09 15:25:00 -0200193 .std = V4L2_STD_SECAM_LC,
194 .name = "SECAM-L'",
195 .b = ( cOutputPort2Inactive |
196 cPositiveAmTV |
197 cQSS ),
198 .e = ( cGating_36 |
199 cAudioIF_6_5 |
200 cVideoIF_33_90 ),
201 },{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 .std = V4L2_STD_SECAM_DK,
203 .name = "SECAM-DK",
204 .b = ( cNegativeFmTV |
205 cQSS ),
206 .c = ( cDeemphasisON |
207 cDeemphasis50 ),
208 .e = ( cAudioIF_6_5 |
209 cVideoIF_38_00 ),
210 },{
211 .std = V4L2_STD_NTSC_M,
212 .name = "NTSC-M",
213 .b = ( cNegativeFmTV |
214 cQSS ),
215 .c = ( cDeemphasisON |
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700216 cDeemphasis75 ),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 .e = ( cGating_36 |
218 cAudioIF_4_5 |
219 cVideoIF_45_75 ),
220 },{
221 .std = V4L2_STD_NTSC_M_JP,
222 .name = "NTSC-JP",
223 .b = ( cNegativeFmTV |
224 cQSS ),
225 .c = ( cDeemphasisON |
226 cDeemphasis50 ),
227 .e = ( cGating_36 |
228 cAudioIF_4_5 |
229 cVideoIF_58_75 ),
230 }
231};
232
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700233static struct tvnorm radio_stereo = {
234 .name = "Radio Stereo",
235 .b = ( cFmRadio |
236 cQSS ),
237 .c = ( cDeemphasisOFF |
238 cAudioGain6 ),
239 .e = ( cAudioIF_5_5 |
240 cRadioIF_38_90 ),
241};
242
243static struct tvnorm radio_mono = {
244 .name = "Radio Mono",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .b = ( cFmRadio |
246 cQSS ),
247 .c = ( cDeemphasisON |
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700248 cDeemphasis50),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .e = ( cAudioIF_5_5 |
250 cRadioIF_38_90 ),
251};
252
253/* ---------------------------------------------------------------------- */
254
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800255static void dump_read_message(struct tda9887 *t, unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 static char *afc[16] = {
258 "- 12.5 kHz",
259 "- 37.5 kHz",
260 "- 62.5 kHz",
261 "- 87.5 kHz",
262 "-112.5 kHz",
263 "-137.5 kHz",
264 "-162.5 kHz",
265 "-187.5 kHz [min]",
266 "+187.5 kHz [max]",
267 "+162.5 kHz",
268 "+137.5 kHz",
269 "+112.5 kHz",
270 "+ 87.5 kHz",
271 "+ 62.5 kHz",
272 "+ 37.5 kHz",
273 "+ 12.5 kHz",
274 };
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800275 tda9887_info("read: 0x%2x\n", buf[0]);
276 tda9887_info(" after power on : %s\n", (buf[0] & 0x01) ? "yes" : "no");
277 tda9887_info(" afc : %s\n", afc[(buf[0] >> 1) & 0x0f]);
278 tda9887_info(" fmif level : %s\n", (buf[0] & 0x20) ? "high" : "low");
279 tda9887_info(" afc window : %s\n", (buf[0] & 0x40) ? "in" : "out");
280 tda9887_info(" vfi level : %s\n", (buf[0] & 0x80) ? "high" : "low");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800283static void dump_write_message(struct tda9887 *t, unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 static char *sound[4] = {
286 "AM/TV",
287 "FM/radio",
288 "FM/TV",
289 "FM/radio"
290 };
291 static char *adjust[32] = {
292 "-16", "-15", "-14", "-13", "-12", "-11", "-10", "-9",
293 "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1",
294 "0", "+1", "+2", "+3", "+4", "+5", "+6", "+7",
295 "+8", "+9", "+10", "+11", "+12", "+13", "+14", "+15"
296 };
297 static char *deemph[4] = {
298 "no", "no", "75", "50"
299 };
300 static char *carrier[4] = {
301 "4.5 MHz",
302 "5.5 MHz",
303 "6.0 MHz",
304 "6.5 MHz / AM"
305 };
306 static char *vif[8] = {
307 "58.75 MHz",
308 "45.75 MHz",
309 "38.9 MHz",
310 "38.0 MHz",
311 "33.9 MHz",
312 "33.4 MHz",
313 "45.75 MHz + pin13",
314 "38.9 MHz + pin13",
315 };
316 static char *rif[4] = {
317 "44 MHz",
318 "52 MHz",
319 "52 MHz",
320 "44 MHz",
321 };
322
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800323 tda9887_info("write: byte B 0x%02x\n",buf[1]);
324 tda9887_info(" B0 video mode : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 (buf[1] & 0x01) ? "video trap" : "sound trap");
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800326 tda9887_info(" B1 auto mute fm : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 (buf[1] & 0x02) ? "yes" : "no");
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800328 tda9887_info(" B2 carrier mode : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 (buf[1] & 0x04) ? "QSS" : "Intercarrier");
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800330 tda9887_info(" B3-4 tv sound/radio : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 sound[(buf[1] & 0x18) >> 3]);
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800332 tda9887_info(" B5 force mute audio: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 (buf[1] & 0x20) ? "yes" : "no");
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800334 tda9887_info(" B6 output port 1 : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 (buf[1] & 0x40) ? "high (inactive)" : "low (active)");
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800336 tda9887_info(" B7 output port 2 : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 (buf[1] & 0x80) ? "high (inactive)" : "low (active)");
338
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800339 tda9887_info("write: byte C 0x%02x\n",buf[2]);
340 tda9887_info(" C0-4 top adjustment : %s dB\n", adjust[buf[2] & 0x1f]);
341 tda9887_info(" C5-6 de-emphasis : %s\n", deemph[(buf[2] & 0x60) >> 5]);
342 tda9887_info(" C7 audio gain : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 (buf[2] & 0x80) ? "-6" : "0");
344
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800345 tda9887_info("write: byte E 0x%02x\n",buf[3]);
346 tda9887_info(" E0-1 sound carrier : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 carrier[(buf[3] & 0x03)]);
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800348 tda9887_info(" E6 l pll gating : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 (buf[3] & 0x40) ? "36" : "13");
350
351 if (buf[1] & 0x08) {
352 /* radio */
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800353 tda9887_info(" E2-4 video if : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 rif[(buf[3] & 0x0c) >> 2]);
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800355 tda9887_info(" E7 vif agc output : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 (buf[3] & 0x80)
357 ? ((buf[3] & 0x10) ? "fm-agc radio" : "sif-agc radio")
358 : "fm radio carrier afc");
359 } else {
360 /* video */
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800361 tda9887_info(" E2-4 video if : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 vif[(buf[3] & 0x1c) >> 2]);
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800363 tda9887_info(" E5 tuner gain : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 (buf[3] & 0x80)
365 ? ((buf[3] & 0x20) ? "external" : "normal")
366 : ((buf[3] & 0x20) ? "minimum" : "normal"));
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800367 tda9887_info(" E7 vif agc output : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 (buf[3] & 0x80)
369 ? ((buf[3] & 0x20)
370 ? "pin3 port, pin22 vif agc out"
371 : "pin22 port, pin3 vif acg ext in")
372 : "pin3+pin22 port");
373 }
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800374 tda9887_info("--\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377/* ---------------------------------------------------------------------- */
378
379static int tda9887_set_tvnorm(struct tda9887 *t, char *buf)
380{
381 struct tvnorm *norm = NULL;
382 int i;
383
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700384 if (t->mode == T_RADIO) {
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700385 if (t->radio_mode == V4L2_TUNER_MODE_MONO)
386 norm = &radio_mono;
387 else
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700388 norm = &radio_stereo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 } else {
390 for (i = 0; i < ARRAY_SIZE(tvnorms); i++) {
391 if (tvnorms[i].std & t->std) {
392 norm = tvnorms+i;
393 break;
394 }
395 }
396 }
397 if (NULL == norm) {
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800398 tda9887_dbg("Unsupported tvnorm entry - audio muted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return -1;
400 }
401
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800402 tda9887_dbg("configure for: %s\n",norm->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 buf[1] = norm->b;
404 buf[2] = norm->c;
405 buf[3] = norm->e;
406 return 0;
407}
408
409static unsigned int port1 = UNSET;
410static unsigned int port2 = UNSET;
411static unsigned int qss = UNSET;
412static unsigned int adjust = 0x10;
413module_param(port1, int, 0644);
414module_param(port2, int, 0644);
415module_param(qss, int, 0644);
416module_param(adjust, int, 0644);
417
418static int tda9887_set_insmod(struct tda9887 *t, char *buf)
419{
420 if (UNSET != port1) {
421 if (port1)
422 buf[1] |= cOutputPort1Inactive;
423 else
424 buf[1] &= ~cOutputPort1Inactive;
425 }
426 if (UNSET != port2) {
427 if (port2)
428 buf[1] |= cOutputPort2Inactive;
429 else
430 buf[1] &= ~cOutputPort2Inactive;
431 }
432
433 if (UNSET != qss) {
434 if (qss)
435 buf[1] |= cQSS;
436 else
437 buf[1] &= ~cQSS;
438 }
439
440 if (adjust >= 0x00 && adjust < 0x20)
441 buf[2] |= adjust;
442 return 0;
443}
444
445static int tda9887_set_config(struct tda9887 *t, char *buf)
446{
447 if (t->config & TDA9887_PORT1_ACTIVE)
448 buf[1] &= ~cOutputPort1Inactive;
449 if (t->config & TDA9887_PORT1_INACTIVE)
450 buf[1] |= cOutputPort1Inactive;
451 if (t->config & TDA9887_PORT2_ACTIVE)
452 buf[1] &= ~cOutputPort2Inactive;
453 if (t->config & TDA9887_PORT2_INACTIVE)
454 buf[1] |= cOutputPort2Inactive;
455
456 if (t->config & TDA9887_QSS)
457 buf[1] |= cQSS;
458 if (t->config & TDA9887_INTERCARRIER)
459 buf[1] &= ~cQSS;
460
461 if (t->config & TDA9887_AUTOMUTE)
462 buf[1] |= cAutoMuteFmActive;
463 if (t->config & TDA9887_DEEMPHASIS_MASK) {
464 buf[2] &= ~0x60;
465 switch (t->config & TDA9887_DEEMPHASIS_MASK) {
466 case TDA9887_DEEMPHASIS_NONE:
467 buf[2] |= cDeemphasisOFF;
468 break;
469 case TDA9887_DEEMPHASIS_50:
470 buf[2] |= cDeemphasisON | cDeemphasis50;
471 break;
472 case TDA9887_DEEMPHASIS_75:
473 buf[2] |= cDeemphasisON | cDeemphasis75;
474 break;
475 }
476 }
Nickolay V. Shmyrev3ae1adc2005-11-08 21:37:39 -0800477 if ((t->config & TDA9887_INTERCARRIER_NTSC) && (t->std & V4L2_STD_NTSC))
478 buf[1] &= ~cQSS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0;
480}
481
482/* ---------------------------------------------------------------------- */
483
484static int tda9887_set_pinnacle(struct tda9887 *t, char *buf)
485{
486 unsigned int bCarrierMode = UNSET;
487
488 if (t->std & V4L2_STD_625_50) {
489 if ((1 == t->pinnacle_id) || (7 == t->pinnacle_id)) {
490 bCarrierMode = cIntercarrier;
491 } else {
492 bCarrierMode = cQSS;
493 }
494 }
495 if (t->std & V4L2_STD_525_60) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800496 if ((5 == t->pinnacle_id) || (6 == t->pinnacle_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 bCarrierMode = cIntercarrier;
498 } else {
499 bCarrierMode = cQSS;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502
503 if (bCarrierMode != UNSET) {
504 buf[1] &= ~0x04;
505 buf[1] |= bCarrierMode;
506 }
507 return 0;
508}
509
510/* ---------------------------------------------------------------------- */
511
512static char pal[] = "-";
Bert Wesarg975e0462005-04-16 15:25:43 -0700513module_param_string(pal, pal, sizeof(pal), 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514static char secam[] = "-";
Bert Wesarg975e0462005-04-16 15:25:43 -0700515module_param_string(secam, secam, sizeof(secam), 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517static int tda9887_fixup_std(struct tda9887 *t)
518{
519 /* get more precise norm info from insmod option */
520 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
521 switch (pal[0]) {
522 case 'b':
523 case 'B':
524 case 'g':
525 case 'G':
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800526 tda9887_dbg("insmod fixup: PAL => PAL-BG\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 t->std = V4L2_STD_PAL_BG;
528 break;
529 case 'i':
530 case 'I':
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800531 tda9887_dbg("insmod fixup: PAL => PAL-I\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 t->std = V4L2_STD_PAL_I;
533 break;
534 case 'd':
535 case 'D':
536 case 'k':
537 case 'K':
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800538 tda9887_dbg("insmod fixup: PAL => PAL-DK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 t->std = V4L2_STD_PAL_DK;
540 break;
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700541 case '-':
542 /* default parameter, do nothing */
543 break;
544 default:
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800545 tda9887_info("pal= argument not recognised\n");
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700546 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 }
549 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
550 switch (secam[0]) {
551 case 'd':
552 case 'D':
553 case 'k':
554 case 'K':
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800555 tda9887_dbg("insmod fixup: SECAM => SECAM-DK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 t->std = V4L2_STD_SECAM_DK;
557 break;
558 case 'l':
559 case 'L':
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800560 tda9887_dbg("insmod fixup: SECAM => SECAM-L\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 t->std = V4L2_STD_SECAM_L;
562 break;
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700563 case '-':
564 /* default parameter, do nothing */
565 break;
566 default:
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800567 tda9887_info("secam= argument not recognised\n");
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700568 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570 }
571 return 0;
572}
573
574static int tda9887_status(struct tda9887 *t)
575{
576 unsigned char buf[1];
577 int rc;
578
579 memset(buf,0,sizeof(buf));
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800580 if (1 != (rc = i2c_master_recv(&t->client,buf,1)))
581 tda9887_info("i2c i/o error: rc == %d (should be 1)\n",rc);
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800582 dump_read_message(t, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return 0;
584}
585
586static int tda9887_configure(struct tda9887 *t)
587{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 int rc;
589
Hans Verkuil299392b2005-11-08 21:37:42 -0800590 memset(t->data,0,sizeof(t->data));
591 tda9887_set_tvnorm(t,t->data);
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700592
Hans Verkuil299392b2005-11-08 21:37:42 -0800593 t->data[1] |= cOutputPort1Inactive;
594 t->data[1] |= cOutputPort2Inactive;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (UNSET != t->pinnacle_id) {
Hans Verkuil299392b2005-11-08 21:37:42 -0800597 tda9887_set_pinnacle(t,t->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Hans Verkuil299392b2005-11-08 21:37:42 -0800599 tda9887_set_config(t,t->data);
600 tda9887_set_insmod(t,t->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700602 if (t->mode == T_STANDBY) {
Hans Verkuil299392b2005-11-08 21:37:42 -0800603 t->data[1] |= cForcedMuteAudioON;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700604 }
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800607 tda9887_dbg("writing: b=0x%02x c=0x%02x e=0x%02x\n",
Hans Verkuil299392b2005-11-08 21:37:42 -0800608 t->data[1],t->data[2],t->data[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (debug > 1)
Hans Verkuil299392b2005-11-08 21:37:42 -0800610 dump_write_message(t, t->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800612 if (4 != (rc = i2c_master_send(&t->client,t->data,4)))
613 tda9887_info("i2c i/o error: rc == %d (should be 4)\n",rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 if (debug > 2) {
616 msleep_interruptible(1000);
617 tda9887_status(t);
618 }
619 return 0;
620}
621
622/* ---------------------------------------------------------------------- */
623
624static int tda9887_attach(struct i2c_adapter *adap, int addr, int kind)
625{
626 struct tda9887 *t;
627
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800628 client_template.adapter = adap;
629 client_template.addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800631 if (NULL == (t = kmalloc(sizeof(*t), GFP_KERNEL)))
632 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 memset(t,0,sizeof(*t));
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 t->client = client_template;
636 t->std = 0;
637 t->pinnacle_id = UNSET;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700638 t->radio_mode = V4L2_TUNER_MODE_STEREO;
639
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800640 tda9887_info("chip found @ 0x%x (%s)\n", addr<<1, adap->name);
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800641
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700642 i2c_set_clientdata(&t->client, t);
643 i2c_attach_client(&t->client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 return 0;
646}
647
648static int tda9887_probe(struct i2c_adapter *adap)
649{
650#ifdef I2C_CLASS_TV_ANALOG
651 if (adap->class & I2C_CLASS_TV_ANALOG)
652 return i2c_probe(adap, &addr_data, tda9887_attach);
653#else
654 switch (adap->id) {
Jean Delvarec7a46532005-08-11 23:41:56 +0200655 case I2C_HW_B_BT848:
656 case I2C_HW_B_RIVA:
Jean Delvare1684a9842005-08-11 23:51:10 +0200657 case I2C_HW_SAA7134:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return i2c_probe(adap, &addr_data, tda9887_attach);
659 break;
660 }
661#endif
662 return 0;
663}
664
665static int tda9887_detach(struct i2c_client *client)
666{
667 struct tda9887 *t = i2c_get_clientdata(client);
668
669 i2c_detach_client(client);
670 kfree(t);
671 return 0;
672}
673
674#define SWITCH_V4L2 if (!t->using_v4l2 && debug) \
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800675 tda9887_info("switching to v4l2\n"); \
676 t->using_v4l2 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677#define CHECK_V4L2 if (t->using_v4l2) { if (debug) \
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800678 tda9887_info("ignore v4l1 call\n"); \
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800679 return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681static int
682tda9887_command(struct i2c_client *client, unsigned int cmd, void *arg)
683{
684 struct tda9887 *t = i2c_get_clientdata(client);
685
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800686 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 /* --- configuration --- */
689 case AUDC_SET_RADIO:
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700690 {
691 t->mode = T_RADIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 tda9887_configure(t);
693 break;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700694 }
695 case TUNER_SET_STANDBY:
696 {
697 t->mode = T_STANDBY;
698 tda9887_configure(t);
699 break;
700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 case AUDC_CONFIG_PINNACLE:
702 {
703 int *i = arg;
704
705 t->pinnacle_id = *i;
706 tda9887_configure(t);
707 break;
708 }
709 case TDA9887_SET_CONFIG:
710 {
711 int *i = arg;
712
713 t->config = *i;
714 tda9887_configure(t);
715 break;
716 }
717 /* --- v4l ioctls --- */
718 /* take care: bttv does userspace copying, we'll get a
719 kernel pointer here... */
720 case VIDIOCSCHAN:
721 {
722 static const v4l2_std_id map[] = {
723 [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
724 [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
725 [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
726 [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
727 [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
728 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
729 };
730 struct video_channel *vc = arg;
731
732 CHECK_V4L2;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700733 t->mode = T_ANALOG_TV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (vc->norm < ARRAY_SIZE(map))
735 t->std = map[vc->norm];
736 tda9887_fixup_std(t);
737 tda9887_configure(t);
738 break;
739 }
740 case VIDIOC_S_STD:
741 {
742 v4l2_std_id *id = arg;
743
744 SWITCH_V4L2;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700745 t->mode = T_ANALOG_TV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 t->std = *id;
747 tda9887_fixup_std(t);
748 tda9887_configure(t);
749 break;
750 }
751 case VIDIOC_S_FREQUENCY:
752 {
753 struct v4l2_frequency *f = arg;
754
755 SWITCH_V4L2;
756 if (V4L2_TUNER_ANALOG_TV == f->type) {
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700757 if (t->mode == T_ANALOG_TV)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return 0;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700759 t->mode = T_ANALOG_TV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761 if (V4L2_TUNER_RADIO == f->type) {
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700762 if (t->mode == T_RADIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return 0;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700764 t->mode = T_RADIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766 tda9887_configure(t);
767 break;
768 }
769 case VIDIOC_G_TUNER:
770 {
771 static int AFC_BITS_2_kHz[] = {
772 -12500, -37500, -62500, -97500,
773 -112500, -137500, -162500, -187500,
774 187500, 162500, 137500, 112500,
775 97500 , 62500, 37500 , 12500
776 };
777 struct v4l2_tuner* tuner = arg;
778
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700779 if (t->mode == T_RADIO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 __u8 reg = 0;
781 tuner->afc=0;
782 if (1 == i2c_master_recv(&t->client,&reg,1))
783 tuner->afc = AFC_BITS_2_kHz[(reg>>1)&0x0f];
784 }
785 break;
786 }
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700787 case VIDIOC_S_TUNER:
788 {
789 struct v4l2_tuner* tuner = arg;
790
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700791 if (t->mode == T_RADIO) {
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700792 t->radio_mode = tuner->audmode;
793 tda9887_configure (t);
794 }
795 break;
796 }
Hans Verkuil299392b2005-11-08 21:37:42 -0800797 case VIDIOC_LOG_STATUS:
798 {
799 tda9887_info("Data bytes: b=%02x c=%02x e=%02x\n", t->data[1], t->data[2], t->data[3]);
800 break;
801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 default:
803 /* nothing */
804 break;
805 }
806 return 0;
807}
808
Russell King9480e302005-10-28 09:52:56 -0700809static int tda9887_suspend(struct device * dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800811 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
812 struct tda9887 *t = i2c_get_clientdata(c);
813
814 tda9887_dbg("suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return 0;
816}
817
Russell King9480e302005-10-28 09:52:56 -0700818static int tda9887_resume(struct device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
821 struct tda9887 *t = i2c_get_clientdata(c);
822
Hans Verkuil4eb0c142005-11-08 21:37:18 -0800823 tda9887_dbg("resume\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 tda9887_configure(t);
825 return 0;
826}
827
828/* ----------------------------------------------------------------------- */
829
830static struct i2c_driver driver = {
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800831 .id = -1, /* FIXME */
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800832 .attach_adapter = tda9887_probe,
833 .detach_client = tda9887_detach,
834 .command = tda9887_command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100836 .name = "i2c tda9887 driver",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 .suspend = tda9887_suspend,
838 .resume = tda9887_resume,
839 },
840};
841static struct i2c_client client_template =
842{
Jean Delvarefae91e72005-08-15 19:57:04 +0200843 .name = "tda9887",
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800844 .driver = &driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845};
846
847static int __init tda9887_init_module(void)
848{
849 return i2c_add_driver(&driver);
850}
851
852static void __exit tda9887_cleanup_module(void)
853{
854 i2c_del_driver(&driver);
855}
856
857module_init(tda9887_init_module);
858module_exit(tda9887_cleanup_module);
859
860/*
861 * Overrides for Emacs so that we follow Linus's tabbing style.
862 * ---------------------------------------------------------------------------
863 * Local variables:
864 * c-basic-offset: 8
865 * End:
866 */