]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/tuner-core.c
V4L/DVB (6129): tda8290: convert from tuner sub-driver into dvb_frontend module
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / tuner-core.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 *
3 * i2c tv tuner chip device driver
4 * core core, i.e. kernel interfaces, registering and so on
5 */
6
7#include <linux/module.h>
1da177e4 8#include <linux/kernel.h>
1da177e4
LT
9#include <linux/string.h>
10#include <linux/timer.h>
11#include <linux/delay.h>
12#include <linux/errno.h>
13#include <linux/slab.h>
14#include <linux/poll.h>
15#include <linux/i2c.h>
16#include <linux/types.h>
1da177e4 17#include <linux/init.h>
ffbb807c 18#include <linux/videodev.h>
1da177e4 19#include <media/tuner.h>
5e453dc7 20#include <media/v4l2-common.h>
8218b0b2 21#include "tuner-driver.h"
910bb3e3 22#include "tda8290.h"
1da177e4
LT
23
24#define UNSET (-1U)
25
26/* standard i2c insmod options */
27static unsigned short normal_i2c[] = {
9ee476a5 28#ifdef CONFIG_TUNER_TEA5761
8573a9e6
MCC
29 0x10,
30#endif
de48eebc 31 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
f5bec396
MCC
32 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
33 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
1da177e4
LT
34 I2C_CLIENT_END
35};
f7ce3cc6 36
1da177e4
LT
37I2C_CLIENT_INSMOD;
38
39/* insmod options used at init time => read/only */
f7ce3cc6 40static unsigned int addr = 0;
c5287ba1 41static unsigned int no_autodetect = 0;
fd3113e8 42static unsigned int show_i2c = 0;
fd3113e8 43
1da177e4 44/* insmod options used at runtime => read/write */
f9195ded 45int tuner_debug = 0;
1da177e4 46
f7ce3cc6 47static unsigned int tv_range[2] = { 44, 958 };
1da177e4
LT
48static unsigned int radio_range[2] = { 65, 108 };
49
7e578191
MCC
50static char pal[] = "--";
51static char secam[] = "--";
52static char ntsc[] = "-";
53
f9195ded 54
7e578191
MCC
55module_param(addr, int, 0444);
56module_param(no_autodetect, int, 0444);
57module_param(show_i2c, int, 0444);
f9195ded 58module_param_named(debug,tuner_debug, int, 0644);
7e578191
MCC
59module_param_string(pal, pal, sizeof(pal), 0644);
60module_param_string(secam, secam, sizeof(secam), 0644);
61module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
f7ce3cc6 62module_param_array(tv_range, int, NULL, 0644);
1da177e4
LT
63module_param_array(radio_range, int, NULL, 0644);
64
65MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
66MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
67MODULE_LICENSE("GPL");
68
1da177e4
LT
69static struct i2c_driver driver;
70static struct i2c_client client_template;
71
72/* ---------------------------------------------------------------------- */
73
e18f9444
MK
74static void fe_set_freq(struct tuner *t, unsigned int freq)
75{
76 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
77
78 struct analog_parameters params = {
79 .frequency = freq,
80 .mode = t->mode,
81 .audmode = t->audmode,
82 .std = t->std
83 };
84
85 if (NULL == fe_tuner_ops->set_analog_params) {
86 tuner_warn("Tuner frontend module has no way to set freq\n");
87 return;
88 }
89 fe_tuner_ops->set_analog_params(&t->fe, &params);
90}
91
92static void fe_release(struct tuner *t)
93{
94 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
95
96 if (fe_tuner_ops->release)
97 fe_tuner_ops->release(&t->fe);
98}
99
100static void fe_standby(struct tuner *t)
101{
102 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
103
104 if (fe_tuner_ops->sleep)
105 fe_tuner_ops->sleep(&t->fe);
106}
107
56fc08ca 108/* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
1da177e4
LT
109static void set_tv_freq(struct i2c_client *c, unsigned int freq)
110{
111 struct tuner *t = i2c_get_clientdata(c);
112
113 if (t->type == UNSET) {
f7ce3cc6 114 tuner_warn ("tuner type not set\n");
1da177e4
LT
115 return;
116 }
7a91a80a 117 if (NULL == t->ops.set_tv_freq) {
f7ce3cc6 118 tuner_warn ("Tuner has no way to set tv freq\n");
1da177e4
LT
119 return;
120 }
f7ce3cc6
MCC
121 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
122 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
123 freq / 16, freq % 16 * 100 / 16, tv_range[0],
124 tv_range[1]);
27487d44
HV
125 /* V4L2 spec: if the freq is not possible then the closest
126 possible value should be selected */
127 if (freq < tv_range[0] * 16)
128 freq = tv_range[0] * 16;
129 else
130 freq = tv_range[1] * 16;
1da177e4 131 }
db8a6956 132 t->ops.set_tv_freq(t, freq);
1da177e4
LT
133}
134
135static void set_radio_freq(struct i2c_client *c, unsigned int freq)
136{
137 struct tuner *t = i2c_get_clientdata(c);
138
139 if (t->type == UNSET) {
f7ce3cc6 140 tuner_warn ("tuner type not set\n");
1da177e4
LT
141 return;
142 }
7a91a80a 143 if (NULL == t->ops.set_radio_freq) {
f7ce3cc6 144 tuner_warn ("tuner has no way to set radio frequency\n");
1da177e4
LT
145 return;
146 }
27487d44 147 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
f7ce3cc6
MCC
148 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
149 freq / 16000, freq % 16000 * 100 / 16000,
150 radio_range[0], radio_range[1]);
27487d44
HV
151 /* V4L2 spec: if the freq is not possible then the closest
152 possible value should be selected */
153 if (freq < radio_range[0] * 16000)
154 freq = radio_range[0] * 16000;
155 else
156 freq = radio_range[1] * 16000;
1da177e4 157 }
586b0cab 158
db8a6956 159 t->ops.set_radio_freq(t, freq);
1da177e4
LT
160}
161
162static void set_freq(struct i2c_client *c, unsigned long freq)
163{
164 struct tuner *t = i2c_get_clientdata(c);
165
166 switch (t->mode) {
167 case V4L2_TUNER_RADIO:
168 tuner_dbg("radio freq set to %lu.%02lu\n",
f7ce3cc6
MCC
169 freq / 16000, freq % 16000 * 100 / 16000);
170 set_radio_freq(c, freq);
27487d44 171 t->radio_freq = freq;
1da177e4
LT
172 break;
173 case V4L2_TUNER_ANALOG_TV:
174 case V4L2_TUNER_DIGITAL_TV:
175 tuner_dbg("tv freq set to %lu.%02lu\n",
f7ce3cc6 176 freq / 16, freq % 16 * 100 / 16);
1da177e4 177 set_tv_freq(c, freq);
27487d44 178 t->tv_freq = freq;
1da177e4
LT
179 break;
180 }
1da177e4
LT
181}
182
293197cd
MK
183static void tuner_i2c_address_check(struct tuner *t)
184{
185 if ((t->type == UNSET || t->type == TUNER_ABSENT) ||
186 ((t->i2c.addr < 0x64) || (t->i2c.addr > 0x6f)))
187 return;
188
189 tuner_warn("====================== WARNING! ======================\n");
190 tuner_warn("Support for tuners in i2c address range 0x64 thru 0x6f\n");
191 tuner_warn("will soon be dropped. This message indicates that your\n");
192 tuner_warn("hardware has a %s tuner at i2c address 0x%02x.\n",
193 t->i2c.name, t->i2c.addr);
194 tuner_warn("To ensure continued support for your device, please\n");
195 tuner_warn("send a copy of this message, along with full dmesg\n");
196 tuner_warn("output to v4l-dvb-maintainer@linuxtv.org\n");
197 tuner_warn("Please use subject line: \"obsolete tuner i2c address.\"\n");
198 tuner_warn("driver: %s, addr: 0x%02x, type: %d (%s)\n",
199 t->i2c.adapter->name, t->i2c.addr, t->type,
200 tuners[t->type].name);
201 tuner_warn("====================== WARNING! ======================\n");
202}
203
910bb3e3
MK
204static void attach_tda8290(struct tuner *t)
205{
206 struct tda8290_config cfg = {
207 .lna_cfg = &t->config,
208 .tuner_callback = t->tuner_callback
209 };
210 tda8290_attach(&t->fe, t->i2c.adapter, t->i2c.addr, &cfg);
211}
212
f7ce3cc6 213static void set_type(struct i2c_client *c, unsigned int type,
de956c1e 214 unsigned int new_mode_mask, unsigned int new_config,
cfeb8839 215 int (*tuner_callback) (void *dev, int command,int arg))
1da177e4
LT
216{
217 struct tuner *t = i2c_get_clientdata(c);
e18f9444 218 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
586b0cab 219 unsigned char buffer[4];
1da177e4 220
f7ce3cc6
MCC
221 if (type == UNSET || type == TUNER_ABSENT) {
222 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
1da177e4 223 return;
f7ce3cc6
MCC
224 }
225
226 if (type >= tuner_count) {
227 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
1da177e4 228 return;
f7ce3cc6 229 }
1da177e4 230
80f90fba
HH
231 t->type = type;
232 t->config = new_config;
233 if (tuner_callback != NULL) {
234 tuner_dbg("defining GPIO callback\n");
235 t->tuner_callback = tuner_callback;
236 }
237
f7ce3cc6 238 /* This code detects calls by card attach_inform */
1da177e4 239 if (NULL == t->i2c.dev.driver) {
f7ce3cc6
MCC
240 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
241
1da177e4
LT
242 return;
243 }
56fc08ca 244
b2083199 245 /* discard private data, in case set_type() was previously called */
7a91a80a 246 if (t->ops.release)
db8a6956 247 t->ops.release(t);
052c50d9
MK
248 else {
249 kfree(t->priv);
250 t->priv = NULL;
251 }
be2b85a1 252
1da177e4
LT
253 switch (t->type) {
254 case TUNER_MT2032:
db8a6956 255 microtune_init(t);
1da177e4
LT
256 break;
257 case TUNER_PHILIPS_TDA8290:
910bb3e3
MK
258 {
259 attach_tda8290(t);
1da177e4 260 break;
910bb3e3 261 }
586b0cab 262 case TUNER_TEA5767:
db8a6956 263 if (tea5767_tuner_init(t) == EINVAL) {
f7ce3cc6
MCC
264 t->type = TUNER_ABSENT;
265 t->mode_mask = T_UNINITIALIZED;
266 return;
267 }
268 t->mode_mask = T_RADIO;
586b0cab 269 break;
9ee476a5 270#ifdef CONFIG_TUNER_TEA5761
8573a9e6 271 case TUNER_TEA5761:
db8a6956 272 if (tea5761_tuner_init(t) == EINVAL) {
8573a9e6
MCC
273 t->type = TUNER_ABSENT;
274 t->mode_mask = T_UNINITIALIZED;
9ee476a5 275 return;
8573a9e6
MCC
276 }
277 t->mode_mask = T_RADIO;
278 break;
279#endif
586b0cab
MCC
280 case TUNER_PHILIPS_FMD1216ME_MK3:
281 buffer[0] = 0x0b;
282 buffer[1] = 0xdc;
283 buffer[2] = 0x9c;
284 buffer[3] = 0x60;
f7ce3cc6 285 i2c_master_send(c, buffer, 4);
586b0cab
MCC
286 mdelay(1);
287 buffer[2] = 0x86;
288 buffer[3] = 0x54;
f7ce3cc6 289 i2c_master_send(c, buffer, 4);
db8a6956 290 default_tuner_init(t);
586b0cab 291 break;
93df3413
HH
292 case TUNER_PHILIPS_TD1316:
293 buffer[0] = 0x0b;
294 buffer[1] = 0xdc;
295 buffer[2] = 0x86;
296 buffer[3] = 0xa4;
297 i2c_master_send(c,buffer,4);
db8a6956 298 default_tuner_init(t);
ac272ed7 299 break;
15396236 300 case TUNER_TDA9887:
db8a6956 301 tda9887_tuner_init(t);
15396236 302 break;
1da177e4 303 default:
db8a6956 304 default_tuner_init(t);
1da177e4
LT
305 break;
306 }
f7ce3cc6 307
e18f9444
MK
308 if (fe_tuner_ops->set_analog_params) {
309 strlcpy(t->i2c.name, fe_tuner_ops->info.name, sizeof(t->i2c.name));
310
311 t->ops.set_tv_freq = fe_set_freq;
312 t->ops.set_radio_freq = fe_set_freq;
313 t->ops.standby = fe_standby;
314 t->ops.release = fe_release;
315 }
316
317 tuner_info("type set to %s\n", t->i2c.name);
318
f7ce3cc6
MCC
319 if (t->mode_mask == T_UNINITIALIZED)
320 t->mode_mask = new_mode_mask;
321
27487d44 322 set_freq(c, (V4L2_TUNER_RADIO == t->mode) ? t->radio_freq : t->tv_freq);
f7ce3cc6 323 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
604f28e2 324 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
f7ce3cc6 325 t->mode_mask);
293197cd 326 tuner_i2c_address_check(t);
1da177e4
LT
327}
328
f7ce3cc6
MCC
329/*
330 * This function apply tuner config to tuner specified
331 * by tun_setup structure. I addr is unset, then admin status
332 * and tun addr status is more precise then current status,
333 * it's applied. Otherwise status and type are applied only to
334 * tuner with exactly the same addr.
335*/
336
337static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
338{
339 struct tuner *t = i2c_get_clientdata(c);
340
15396236
MCC
341 tuner_dbg("set addr for type %i\n", t->type);
342
de956c1e
HH
343 if ( (t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
344 (t->mode_mask & tun_setup->mode_mask))) ||
345 (tun_setup->addr == c->addr)) {
346 set_type(c, tun_setup->type, tun_setup->mode_mask,
cfeb8839 347 tun_setup->config, tun_setup->tuner_callback);
f7ce3cc6
MCC
348 }
349}
56fc08ca 350
f7ce3cc6 351static inline int check_mode(struct tuner *t, char *cmd)
56fc08ca 352{
793cf9e6
MCC
353 if ((1 << t->mode & t->mode_mask) == 0) {
354 return EINVAL;
355 }
356
357 switch (t->mode) {
358 case V4L2_TUNER_RADIO:
359 tuner_dbg("Cmd %s accepted for radio\n", cmd);
360 break;
361 case V4L2_TUNER_ANALOG_TV:
362 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
363 break;
364 case V4L2_TUNER_DIGITAL_TV:
365 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
366 break;
56fc08ca 367 }
793cf9e6 368 return 0;
56fc08ca 369}
56fc08ca 370
f7ce3cc6 371/* get more precise norm info from insmod option */
1da177e4
LT
372static int tuner_fixup_std(struct tuner *t)
373{
374 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
1da177e4 375 switch (pal[0]) {
e71ced1a
HV
376 case '6':
377 tuner_dbg ("insmod fixup: PAL => PAL-60\n");
378 t->std = V4L2_STD_PAL_60;
379 break;
1da177e4
LT
380 case 'b':
381 case 'B':
382 case 'g':
383 case 'G':
f7ce3cc6 384 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
1da177e4
LT
385 t->std = V4L2_STD_PAL_BG;
386 break;
387 case 'i':
388 case 'I':
f7ce3cc6 389 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
1da177e4
LT
390 t->std = V4L2_STD_PAL_I;
391 break;
392 case 'd':
393 case 'D':
394 case 'k':
395 case 'K':
f7ce3cc6 396 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
1da177e4
LT
397 t->std = V4L2_STD_PAL_DK;
398 break;
f7ce3cc6
MCC
399 case 'M':
400 case 'm':
401 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
402 t->std = V4L2_STD_PAL_M;
403 break;
404 case 'N':
405 case 'n':
7e578191
MCC
406 if (pal[1] == 'c' || pal[1] == 'C') {
407 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
408 t->std = V4L2_STD_PAL_Nc;
409 } else {
410 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
411 t->std = V4L2_STD_PAL_N;
412 }
f7ce3cc6 413 break;
21d4df37
MCC
414 case '-':
415 /* default parameter, do nothing */
416 break;
417 default:
418 tuner_warn ("pal= argument not recognised\n");
419 break;
1da177e4
LT
420 }
421 }
f7ce3cc6
MCC
422 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
423 switch (secam[0]) {
7e578191
MCC
424 case 'b':
425 case 'B':
426 case 'g':
427 case 'G':
428 case 'h':
429 case 'H':
430 tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
431 t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
432 break;
f7ce3cc6
MCC
433 case 'd':
434 case 'D':
435 case 'k':
436 case 'K':
437 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
438 t->std = V4L2_STD_SECAM_DK;
439 break;
440 case 'l':
441 case 'L':
800d3c6f
MCC
442 if ((secam[1]=='C')||(secam[1]=='c')) {
443 tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n");
444 t->std = V4L2_STD_SECAM_LC;
445 } else {
446 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
447 t->std = V4L2_STD_SECAM_L;
448 }
f7ce3cc6 449 break;
21d4df37
MCC
450 case '-':
451 /* default parameter, do nothing */
452 break;
453 default:
454 tuner_warn ("secam= argument not recognised\n");
455 break;
f7ce3cc6
MCC
456 }
457 }
458
7e578191
MCC
459 if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
460 switch (ntsc[0]) {
461 case 'm':
462 case 'M':
463 tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
464 t->std = V4L2_STD_NTSC_M;
465 break;
466 case 'j':
467 case 'J':
468 tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
469 t->std = V4L2_STD_NTSC_M_JP;
470 break;
d97a11e0
HV
471 case 'k':
472 case 'K':
473 tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
474 t->std = V4L2_STD_NTSC_M_KR;
475 break;
7e578191
MCC
476 case '-':
477 /* default parameter, do nothing */
478 break;
479 default:
480 tuner_info("ntsc= argument not recognised\n");
481 break;
482 }
483 }
1da177e4
LT
484 return 0;
485}
486
db8a6956 487static void tuner_status(struct tuner *t)
7e578191 488{
7e578191 489 unsigned long freq, freq_fraction;
e18f9444 490 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
7e578191
MCC
491 const char *p;
492
493 switch (t->mode) {
494 case V4L2_TUNER_RADIO: p = "radio"; break;
495 case V4L2_TUNER_ANALOG_TV: p = "analog TV"; break;
496 case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break;
497 default: p = "undefined"; break;
498 }
499 if (t->mode == V4L2_TUNER_RADIO) {
27487d44
HV
500 freq = t->radio_freq / 16000;
501 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
7e578191 502 } else {
27487d44
HV
503 freq = t->tv_freq / 16;
504 freq_fraction = (t->tv_freq % 16) * 100 / 16;
7e578191
MCC
505 }
506 tuner_info("Tuner mode: %s\n", p);
507 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
4ae5c2e5 508 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
8a4b275f
HV
509 if (t->mode != V4L2_TUNER_RADIO)
510 return;
e18f9444
MK
511 if (fe_tuner_ops->get_status) {
512 u32 tuner_status;
513
514 fe_tuner_ops->get_status(&t->fe, &tuner_status);
515 if (tuner_status & TUNER_STATUS_LOCKED)
516 tuner_info("Tuner is locked.\n");
517 if (tuner_status & TUNER_STATUS_STEREO)
518 tuner_info("Stereo: yes\n");
519 }
7a91a80a 520 if (t->ops.has_signal) {
db8a6956 521 tuner_info("Signal strength: %d\n", t->ops.has_signal(t));
8a4b275f 522 }
7a91a80a 523 if (t->ops.is_stereo) {
db8a6956 524 tuner_info("Stereo: %s\n", t->ops.is_stereo(t) ? "yes" : "no");
7e578191
MCC
525 }
526}
8a4b275f 527
1da177e4
LT
528/* ---------------------------------------------------------------------- */
529
ba8fc399 530/* static vars: used only in tuner_attach and tuner_probe */
f7ce3cc6
MCC
531static unsigned default_mode_mask;
532
533/* During client attach, set_type is called by adapter's attach_inform callback.
534 set_type must then be completed by tuner_attach.
535 */
1da177e4
LT
536static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
537{
538 struct tuner *t;
539
f7ce3cc6
MCC
540 client_template.adapter = adap;
541 client_template.addr = addr;
1da177e4 542
7408187d 543 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
f7ce3cc6
MCC
544 if (NULL == t)
545 return -ENOMEM;
f7ce3cc6 546 memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
1da177e4 547 i2c_set_clientdata(&t->i2c, t);
f7ce3cc6 548 t->type = UNSET;
f7ce3cc6
MCC
549 t->audmode = V4L2_TUNER_MODE_STEREO;
550 t->mode_mask = T_UNINITIALIZED;
7a91a80a 551 t->ops.tuner_status = tuner_status;
f7ce3cc6 552
fd3113e8
MCC
553 if (show_i2c) {
554 unsigned char buffer[16];
555 int i,rc;
556
557 memset(buffer, 0, sizeof(buffer));
558 rc = i2c_master_recv(&t->i2c, buffer, sizeof(buffer));
67678360 559 tuner_info("I2C RECV = ");
fd3113e8
MCC
560 for (i=0;i<rc;i++)
561 printk("%02x ",buffer[i]);
562 printk("\n");
563 }
c28089a6
MH
564 /* HACK: This test were added to avoid tuner to probe tda9840 and tea6415c on the MXB card */
565 if (adap->id == I2C_HW_SAA7146 && addr < 0x4a)
566 return -ENODEV;
567
257c645d 568 /* autodetection code based on the i2c addr */
c5287ba1 569 if (!no_autodetect) {
13dd38d0 570 switch (addr) {
9ee476a5 571#ifdef CONFIG_TUNER_TEA5761
8573a9e6 572 case 0x10:
db8a6956 573 if (tea5761_autodetection(t) != EINVAL) {
8573a9e6
MCC
574 t->type = TUNER_TEA5761;
575 t->mode_mask = T_RADIO;
576 t->mode = T_STANDBY;
577 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
578 default_mode_mask &= ~T_RADIO;
579
580 goto register_client;
581 }
582 break;
583#endif
13dd38d0
MCC
584 case 0x42:
585 case 0x43:
586 case 0x4a:
95736034 587 case 0x4b:
67678360
MCC
588 /* If chip is not tda8290, don't register.
589 since it can be tda9887*/
910bb3e3 590 if (tda8290_probe(t->i2c.adapter, t->i2c.addr) == 0) {
15396236
MCC
591 tuner_dbg("chip at addr %x is a tda8290\n", addr);
592 } else {
593 /* Default is being tda9887 */
594 t->type = TUNER_TDA9887;
595 t->mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
596 t->mode = T_STANDBY;
597 goto register_client;
13dd38d0 598 }
07345f5d
HH
599 break;
600 case 0x60:
db8a6956 601 if (tea5767_autodetection(t) != EINVAL) {
07345f5d
HH
602 t->type = TUNER_TEA5767;
603 t->mode_mask = T_RADIO;
604 t->mode = T_STANDBY;
27487d44 605 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
07345f5d 606 default_mode_mask &= ~T_RADIO;
13dd38d0 607
07345f5d
HH
608 goto register_client;
609 }
610 break;
f7ce3cc6
MCC
611 }
612 }
1da177e4 613
f7ce3cc6
MCC
614 /* Initializes only the first adapter found */
615 if (default_mode_mask != T_UNINITIALIZED) {
616 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
617 t->mode_mask = default_mode_mask;
27487d44
HV
618 t->tv_freq = 400 * 16; /* Sets freq to VHF High */
619 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
f7ce3cc6
MCC
620 default_mode_mask = T_UNINITIALIZED;
621 }
56fc08ca 622
f7ce3cc6 623 /* Should be just before return */
67678360
MCC
624register_client:
625 tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
f7ce3cc6 626 i2c_attach_client (&t->i2c);
cfeb8839 627 set_type (&t->i2c,t->type, t->mode_mask, t->config, t->tuner_callback);
1da177e4
LT
628 return 0;
629}
630
631static int tuner_probe(struct i2c_adapter *adap)
632{
633 if (0 != addr) {
f5bec396
MCC
634 normal_i2c[0] = addr;
635 normal_i2c[1] = I2C_CLIENT_END;
1da177e4 636 }
1da177e4 637
f7ce3cc6 638 default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
391cd727 639
1da177e4
LT
640 if (adap->class & I2C_CLASS_TV_ANALOG)
641 return i2c_probe(adap, &addr_data, tuner_attach);
642 return 0;
643}
644
645static int tuner_detach(struct i2c_client *client)
646{
647 struct tuner *t = i2c_get_clientdata(client);
391cd727
MCC
648 int err;
649
f7ce3cc6 650 err = i2c_detach_client(&t->i2c);
391cd727 651 if (err) {
f7ce3cc6
MCC
652 tuner_warn
653 ("Client deregistration failed, client not detached.\n");
391cd727
MCC
654 return err;
655 }
1da177e4 656
7a91a80a 657 if (t->ops.release)
db8a6956 658 t->ops.release(t);
052c50d9
MK
659 else {
660 kfree(t->priv);
661 }
1da177e4
LT
662 kfree(t);
663 return 0;
664}
665
f7ce3cc6
MCC
666/*
667 * Switch tuner to other mode. If tuner support both tv and radio,
668 * set another frequency to some value (This is needed for some pal
669 * tuners to avoid locking). Otherwise, just put second tuner in
670 * standby mode.
671 */
672
673static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
674{
4ac97914
MCC
675 if (mode == t->mode)
676 return 0;
677
678 t->mode = mode;
679
680 if (check_mode(t, cmd) == EINVAL) {
681 t->mode = T_STANDBY;
7a91a80a 682 if (t->ops.standby)
db8a6956 683 t->ops.standby(t);
4ac97914
MCC
684 return EINVAL;
685 }
686 return 0;
f7ce3cc6
MCC
687}
688
689#define switch_v4l2() if (!t->using_v4l2) \
4ac97914
MCC
690 tuner_dbg("switching to v4l2\n"); \
691 t->using_v4l2 = 1;
f7ce3cc6
MCC
692
693static inline int check_v4l2(struct tuner *t)
694{
3bbe5a83
HV
695 /* bttv still uses both v4l1 and v4l2 calls to the tuner (v4l2 for
696 TV, v4l1 for radio), until that is fixed this code is disabled.
697 Otherwise the radio (v4l1) wouldn't tune after using the TV (v4l2)
698 first. */
f7ce3cc6
MCC
699 return 0;
700}
1da177e4 701
f7ce3cc6 702static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
1da177e4
LT
703{
704 struct tuner *t = i2c_get_clientdata(client);
e18f9444 705 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1da177e4 706
f9195ded 707 if (tuner_debug>1)
5e453dc7
MK
708 v4l_i2c_print_ioctl(&(t->i2c),cmd);
709
f7ce3cc6 710 switch (cmd) {
1da177e4 711 /* --- configuration --- */
56fc08ca 712 case TUNER_SET_TYPE_ADDR:
de956c1e 713 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
f7ce3cc6
MCC
714 ((struct tuner_setup *)arg)->type,
715 ((struct tuner_setup *)arg)->addr,
de956c1e
HH
716 ((struct tuner_setup *)arg)->mode_mask,
717 ((struct tuner_setup *)arg)->config);
f7ce3cc6
MCC
718
719 set_addr(client, (struct tuner_setup *)arg);
391cd727 720 break;
1da177e4 721 case AUDC_SET_RADIO:
27487d44
HV
722 if (set_mode(client, t, V4L2_TUNER_RADIO, "AUDC_SET_RADIO")
723 == EINVAL)
724 return 0;
725 if (t->radio_freq)
726 set_freq(client, t->radio_freq);
1da177e4 727 break;
793cf9e6 728 case TUNER_SET_STANDBY:
27487d44
HV
729 if (check_mode(t, "TUNER_SET_STANDBY") == EINVAL)
730 return 0;
15396236 731 t->mode = T_STANDBY;
7a91a80a 732 if (t->ops.standby)
db8a6956 733 t->ops.standby(t);
27487d44 734 break;
985bc96e 735#ifdef CONFIG_VIDEO_V4L1
fd3113e8
MCC
736 case VIDIOCSAUDIO:
737 if (check_mode(t, "VIDIOCSAUDIO") == EINVAL)
738 return 0;
739 if (check_v4l2(t) == EINVAL)
740 return 0;
741
742 /* Should be implemented, since bttv calls it */
743 tuner_dbg("VIDIOCSAUDIO not implemented.\n");
f7ce3cc6 744 break;
1da177e4 745 case VIDIOCSCHAN:
f7ce3cc6
MCC
746 {
747 static const v4l2_std_id map[] = {
748 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
749 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
750 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
751 [4 /* bttv */ ] = V4L2_STD_PAL_M,
752 [5 /* bttv */ ] = V4L2_STD_PAL_N,
753 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
754 };
755 struct video_channel *vc = arg;
756
757 if (check_v4l2(t) == EINVAL)
758 return 0;
759
760 if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
761 return 0;
762
763 if (vc->norm < ARRAY_SIZE(map))
764 t->std = map[vc->norm];
765 tuner_fixup_std(t);
27487d44
HV
766 if (t->tv_freq)
767 set_tv_freq(client, t->tv_freq);
f7ce3cc6
MCC
768 return 0;
769 }
1da177e4 770 case VIDIOCSFREQ:
f7ce3cc6
MCC
771 {
772 unsigned long *v = arg;
1da177e4 773
f7ce3cc6
MCC
774 if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
775 return 0;
776 if (check_v4l2(t) == EINVAL)
777 return 0;
778
779 set_freq(client, *v);
780 return 0;
781 }
1da177e4 782 case VIDIOCGTUNER:
f7ce3cc6
MCC
783 {
784 struct video_tuner *vt = arg;
785
786 if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
787 return 0;
788 if (check_v4l2(t) == EINVAL)
789 return 0;
790
791 if (V4L2_TUNER_RADIO == t->mode) {
e18f9444
MK
792 if (fe_tuner_ops->get_status) {
793 u32 tuner_status;
794
795 fe_tuner_ops->get_status(&t->fe, &tuner_status);
796 if (tuner_status & TUNER_STATUS_STEREO)
797 vt->flags |= VIDEO_TUNER_STEREO_ON;
798 else
799 vt->flags &= ~VIDEO_TUNER_STEREO_ON;
800 vt->signal = tuner_status & TUNER_STATUS_LOCKED
801 ? 65535 : 0;
802 } else {
803 if (t->ops.is_stereo) {
804 if (t->ops.is_stereo(t))
805 vt->flags |=
806 VIDEO_TUNER_STEREO_ON;
807 else
808 vt->flags &=
809 ~VIDEO_TUNER_STEREO_ON;
810 }
811 if (t->ops.has_signal)
812 vt->signal = t->ops.has_signal(t);
f7ce3cc6
MCC
813 }
814 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
586b0cab 815
f7ce3cc6
MCC
816 vt->rangelow = radio_range[0] * 16000;
817 vt->rangehigh = radio_range[1] * 16000;
586b0cab 818
f7ce3cc6
MCC
819 } else {
820 vt->rangelow = tv_range[0] * 16;
821 vt->rangehigh = tv_range[1] * 16;
822 }
56fc08ca 823
f7ce3cc6
MCC
824 return 0;
825 }
1da177e4 826 case VIDIOCGAUDIO:
f7ce3cc6
MCC
827 {
828 struct video_audio *va = arg;
829
830 if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
831 return 0;
832 if (check_v4l2(t) == EINVAL)
833 return 0;
834
e18f9444
MK
835 if (V4L2_TUNER_RADIO == t->mode) {
836 if (fe_tuner_ops->get_status) {
837 u32 tuner_status;
838
839 fe_tuner_ops->get_status(&t->fe, &tuner_status);
840 va->mode = (tuner_status & TUNER_STATUS_STEREO)
841 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
842 } else if (t->ops.is_stereo)
843 va->mode = t->ops.is_stereo(t)
844 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
845 }
f7ce3cc6
MCC
846 return 0;
847 }
985bc96e
MCC
848#endif
849 case TDA9887_SET_CONFIG:
850 if (t->type == TUNER_TDA9887) {
851 int *i = arg;
1da177e4 852
985bc96e
MCC
853 t->tda9887_config = *i;
854 set_freq(client, t->tv_freq);
855 }
856 break;
857 /* --- v4l ioctls --- */
858 /* take care: bttv does userspace copying, we'll get a
859 kernel pointer here... */
1da177e4 860 case VIDIOC_S_STD:
f7ce3cc6
MCC
861 {
862 v4l2_std_id *id = arg;
1da177e4 863
f7ce3cc6
MCC
864 if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
865 == EINVAL)
866 return 0;
56fc08ca 867
f7ce3cc6
MCC
868 switch_v4l2();
869
870 t->std = *id;
871 tuner_fixup_std(t);
27487d44
HV
872 if (t->tv_freq)
873 set_freq(client, t->tv_freq);
f7ce3cc6
MCC
874 break;
875 }
1da177e4 876 case VIDIOC_S_FREQUENCY:
f7ce3cc6
MCC
877 {
878 struct v4l2_frequency *f = arg;
879
4f725cb3
HV
880 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
881 == EINVAL)
882 return 0;
f7ce3cc6 883 switch_v4l2();
27487d44 884 set_freq(client,f->frequency);
c184ca36 885
f7ce3cc6
MCC
886 break;
887 }
888 case VIDIOC_G_FREQUENCY:
889 {
890 struct v4l2_frequency *f = arg;
891
892 if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
893 return 0;
894 switch_v4l2();
895 f->type = t->mode;
e18f9444
MK
896 if (fe_tuner_ops->get_frequency) {
897 u32 abs_freq;
898
899 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
900 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
901 (abs_freq * 2 + 125/2) / 125 :
902 (abs_freq + 62500/2) / 62500;
903 break;
904 }
27487d44
HV
905 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
906 t->radio_freq : t->tv_freq;
f7ce3cc6
MCC
907 break;
908 }
1da177e4 909 case VIDIOC_G_TUNER:
f7ce3cc6
MCC
910 {
911 struct v4l2_tuner *tuner = arg;
912
913 if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
914 return 0;
915 switch_v4l2();
916
8a4b275f 917 tuner->type = t->mode;
7a91a80a 918 if (t->ops.get_afc)
db8a6956 919 tuner->afc=t->ops.get_afc(t);
ab4cecf9
HV
920 if (t->mode == V4L2_TUNER_ANALOG_TV)
921 tuner->capability |= V4L2_TUNER_CAP_NORM;
8a4b275f 922 if (t->mode != V4L2_TUNER_RADIO) {
f7ce3cc6
MCC
923 tuner->rangelow = tv_range[0] * 16;
924 tuner->rangehigh = tv_range[1] * 16;
8a4b275f
HV
925 break;
926 }
927
928 /* radio mode */
8a4b275f
HV
929 tuner->rxsubchans =
930 V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
e18f9444
MK
931 if (fe_tuner_ops->get_status) {
932 u32 tuner_status;
933
934 fe_tuner_ops->get_status(&t->fe, &tuner_status);
935 tuner->rxsubchans = (tuner_status & TUNER_STATUS_STEREO) ?
8a4b275f 936 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
e18f9444
MK
937 tuner->signal = tuner_status & TUNER_STATUS_LOCKED ? 65535 : 0;
938 } else {
939 if (t->ops.is_stereo) {
940 tuner->rxsubchans = t->ops.is_stereo(t) ?
941 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
942 }
943 if (t->ops.has_signal)
944 tuner->signal = t->ops.has_signal(t);
56fc08ca 945 }
8a4b275f
HV
946 tuner->capability |=
947 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
948 tuner->audmode = t->audmode;
949 tuner->rangelow = radio_range[0] * 16000;
950 tuner->rangehigh = radio_range[1] * 16000;
f7ce3cc6
MCC
951 break;
952 }
953 case VIDIOC_S_TUNER:
954 {
955 struct v4l2_tuner *tuner = arg;
956
957 if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
958 return 0;
959
960 switch_v4l2();
961
8a4b275f
HV
962 /* do nothing unless we're a radio tuner */
963 if (t->mode != V4L2_TUNER_RADIO)
964 break;
965 t->audmode = tuner->audmode;
966 set_radio_freq(client, t->radio_freq);
f7ce3cc6 967 break;
56fc08ca 968 }
cd43c3f6 969 case VIDIOC_LOG_STATUS:
7a91a80a 970 if (t->ops.tuner_status)
db8a6956 971 t->ops.tuner_status(t);
cd43c3f6 972 break;
1da177e4
LT
973 }
974
975 return 0;
976}
977
21b48a70 978static int tuner_suspend(struct i2c_client *c, pm_message_t state)
1da177e4 979{
f7ce3cc6 980 struct tuner *t = i2c_get_clientdata (c);
1da177e4 981
f7ce3cc6 982 tuner_dbg ("suspend\n");
1da177e4
LT
983 /* FIXME: power down ??? */
984 return 0;
985}
986
21b48a70 987static int tuner_resume(struct i2c_client *c)
1da177e4 988{
f7ce3cc6 989 struct tuner *t = i2c_get_clientdata (c);
1da177e4 990
f7ce3cc6 991 tuner_dbg ("resume\n");
27487d44
HV
992 if (V4L2_TUNER_RADIO == t->mode) {
993 if (t->radio_freq)
994 set_freq(c, t->radio_freq);
995 } else {
996 if (t->tv_freq)
997 set_freq(c, t->tv_freq);
998 }
1da177e4
LT
999 return 0;
1000}
1001
1002/* ----------------------------------------------------------------------- */
1003
1004static struct i2c_driver driver = {
f7ce3cc6 1005 .id = I2C_DRIVERID_TUNER,
f7ce3cc6
MCC
1006 .attach_adapter = tuner_probe,
1007 .detach_client = tuner_detach,
1008 .command = tuner_command,
21b48a70
JD
1009 .suspend = tuner_suspend,
1010 .resume = tuner_resume,
1da177e4 1011 .driver = {
cab462f7 1012 .name = "tuner",
cab462f7 1013 },
1da177e4 1014};
f7ce3cc6 1015static struct i2c_client client_template = {
fae91e72 1016 .name = "(tuner unset)",
f7ce3cc6 1017 .driver = &driver,
1da177e4
LT
1018};
1019
1020static int __init tuner_init_module(void)
1021{
1022 return i2c_add_driver(&driver);
1023}
1024
1025static void __exit tuner_cleanup_module(void)
1026{
1027 i2c_del_driver(&driver);
1028}
1029
1030module_init(tuner_init_module);
1031module_exit(tuner_cleanup_module);
1032
1033/*
1034 * Overrides for Emacs so that we follow Linus's tabbing style.
1035 * ---------------------------------------------------------------------------
1036 * Local variables:
1037 * c-basic-offset: 8
1038 * End:
1039 */