1/* 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> 8#include <linux/kernel.h> 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> 17#include <linux/init.h> 18#include <linux/videodev2.h> 19#include <media/tuner.h> 20#include <media/tuner-types.h> 21#include <media/v4l2-device.h> 22#include <media/v4l2-ioctl.h> 23#include <media/v4l2-i2c-drv.h> 24#include "mt20xx.h" 25#include "tda8290.h" 26#include "tea5761.h" 27#include "tea5767.h" 28#include "tuner-xc2028.h" 29#include "tuner-simple.h" 30#include "tda9887.h" 31#include "xc5000.h" 32#include "tda18271.h" 33 34#define UNSET (-1U) 35 36#define PREFIX t->i2c->driver->driver.name 37 38/** This macro allows us to probe dynamically, avoiding static links */ 39#ifdef CONFIG_MEDIA_ATTACH 40#define tuner_symbol_probe(FUNCTION, ARGS...) ({ \ 41 int __r = -EINVAL; \ 42 typeof(&FUNCTION) __a = symbol_request(FUNCTION); \ 43 if (__a) { \ 44 __r = (int) __a(ARGS); \ 45 symbol_put(FUNCTION); \ 46 } else { \ 47 printk(KERN_ERR "TUNER: Unable to find " \ 48 "symbol "#FUNCTION"()\n"); \ 49 } \ 50 __r; \ 51}) 52 53static void tuner_detach(struct dvb_frontend *fe) 54{ 55 if (fe->ops.tuner_ops.release) { 56 fe->ops.tuner_ops.release(fe); 57 symbol_put_addr(fe->ops.tuner_ops.release); 58 } 59 if (fe->ops.analog_ops.release) { 60 fe->ops.analog_ops.release(fe); 61 symbol_put_addr(fe->ops.analog_ops.release); 62 } 63} 64#else 65#define tuner_symbol_probe(FUNCTION, ARGS...) ({ \ 66 FUNCTION(ARGS); \ 67}) 68 69static void tuner_detach(struct dvb_frontend *fe) 70{ 71 if (fe->ops.tuner_ops.release) 72 fe->ops.tuner_ops.release(fe); 73 if (fe->ops.analog_ops.release) 74 fe->ops.analog_ops.release(fe); 75} 76#endif 77 78struct tuner { 79 /* device */ 80 struct dvb_frontend fe; 81 struct i2c_client *i2c; 82 struct v4l2_subdev sd; 83 struct list_head list; 84 unsigned int using_v4l2:1; 85 86 /* keep track of the current settings */ 87 v4l2_std_id std; 88 unsigned int tv_freq; 89 unsigned int radio_freq; 90 unsigned int audmode; 91 92 unsigned int mode; 93 unsigned int mode_mask; /* Combination of allowable modes */ 94 95 unsigned int type; /* chip type id */ 96 unsigned int config; 97 const char *name; 98}; 99 100static inline struct tuner *to_tuner(struct v4l2_subdev *sd) 101{ 102 return container_of(sd, struct tuner, sd); 103} 104 105 106/* insmod options used at init time => read/only */ 107static unsigned int addr; 108static unsigned int no_autodetect; 109static unsigned int show_i2c; 110 111/* insmod options used at runtime => read/write */ 112static int tuner_debug; 113 114#define tuner_warn(fmt, arg...) do { \ 115 printk(KERN_WARNING "%s %d-%04x: " fmt, PREFIX, \ 116 i2c_adapter_id(t->i2c->adapter), \ 117 t->i2c->addr, ##arg); \ 118 } while (0) 119 120#define tuner_info(fmt, arg...) do { \ 121 printk(KERN_INFO "%s %d-%04x: " fmt, PREFIX, \ 122 i2c_adapter_id(t->i2c->adapter), \ 123 t->i2c->addr, ##arg); \ 124 } while (0) 125 126#define tuner_err(fmt, arg...) do { \ 127 printk(KERN_ERR "%s %d-%04x: " fmt, PREFIX, \ 128 i2c_adapter_id(t->i2c->adapter), \ 129 t->i2c->addr, ##arg); \ 130 } while (0) 131 132#define tuner_dbg(fmt, arg...) do { \ 133 if (tuner_debug) \ 134 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX, \ 135 i2c_adapter_id(t->i2c->adapter), \ 136 t->i2c->addr, ##arg); \ 137 } while (0) 138 139/* ------------------------------------------------------------------------ */ 140 141static unsigned int tv_range[2] = { 44, 958 }; 142static unsigned int radio_range[2] = { 65, 108 }; 143 144static char pal[] = "--"; 145static char secam[] = "--"; 146static char ntsc[] = "-"; 147 148 149module_param(addr, int, 0444); 150module_param(no_autodetect, int, 0444); 151module_param(show_i2c, int, 0444); 152module_param_named(debug,tuner_debug, int, 0644); 153module_param_string(pal, pal, sizeof(pal), 0644); 154module_param_string(secam, secam, sizeof(secam), 0644); 155module_param_string(ntsc, ntsc, sizeof(ntsc), 0644); 156module_param_array(tv_range, int, NULL, 0644); 157module_param_array(radio_range, int, NULL, 0644); 158 159MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners"); 160MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer"); 161MODULE_LICENSE("GPL"); 162 163/* ---------------------------------------------------------------------- */ 164 165static void fe_set_params(struct dvb_frontend *fe, 166 struct analog_parameters *params) 167{ 168 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops; 169 struct tuner *t = fe->analog_demod_priv; 170 171 if (NULL == fe_tuner_ops->set_analog_params) { 172 tuner_warn("Tuner frontend module has no way to set freq\n"); 173 return; 174 } 175 fe_tuner_ops->set_analog_params(fe, params); 176} 177 178static void fe_standby(struct dvb_frontend *fe) 179{ 180 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops; 181 182 if (fe_tuner_ops->sleep) 183 fe_tuner_ops->sleep(fe); 184} 185 186static int fe_has_signal(struct dvb_frontend *fe) 187{ 188 u16 strength = 0; 189 190 if (fe->ops.tuner_ops.get_rf_strength) 191 fe->ops.tuner_ops.get_rf_strength(fe, &strength); 192 193 return strength; 194} 195 196static int fe_set_config(struct dvb_frontend *fe, void *priv_cfg) 197{ 198 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops; 199 struct tuner *t = fe->analog_demod_priv; 200 201 if (fe_tuner_ops->set_config) 202 return fe_tuner_ops->set_config(fe, priv_cfg); 203 204 tuner_warn("Tuner frontend module has no way to set config\n"); 205 206 return 0; 207} 208 209static void tuner_status(struct dvb_frontend *fe); 210 211static struct analog_demod_ops tuner_analog_ops = { 212 .set_params = fe_set_params, 213 .standby = fe_standby, 214 .has_signal = fe_has_signal, 215 .set_config = fe_set_config, 216 .tuner_status = tuner_status 217}; 218 219/* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */ 220static void set_tv_freq(struct i2c_client *c, unsigned int freq) 221{ 222 struct tuner *t = to_tuner(i2c_get_clientdata(c)); 223 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; 224 225 struct analog_parameters params = { 226 .mode = t->mode, 227 .audmode = t->audmode, 228 .std = t->std 229 }; 230 231 if (t->type == UNSET) { 232 tuner_warn ("tuner type not set\n"); 233 return; 234 } 235 if (NULL == analog_ops->set_params) { 236 tuner_warn ("Tuner has no way to set tv freq\n"); 237 return; 238 } 239 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) { 240 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n", 241 freq / 16, freq % 16 * 100 / 16, tv_range[0], 242 tv_range[1]); 243 /* V4L2 spec: if the freq is not possible then the closest 244 possible value should be selected */ 245 if (freq < tv_range[0] * 16) 246 freq = tv_range[0] * 16; 247 else 248 freq = tv_range[1] * 16; 249 } 250 params.frequency = freq; 251 252 analog_ops->set_params(&t->fe, ¶ms); 253} 254 255static void set_radio_freq(struct i2c_client *c, unsigned int freq) 256{ 257 struct tuner *t = to_tuner(i2c_get_clientdata(c)); 258 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; 259 260 struct analog_parameters params = { 261 .mode = t->mode, 262 .audmode = t->audmode, 263 .std = t->std 264 }; 265 266 if (t->type == UNSET) { 267 tuner_warn ("tuner type not set\n"); 268 return; 269 } 270 if (NULL == analog_ops->set_params) { 271 tuner_warn ("tuner has no way to set radio frequency\n"); 272 return; 273 } 274 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) { 275 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n", 276 freq / 16000, freq % 16000 * 100 / 16000, 277 radio_range[0], radio_range[1]); 278 /* V4L2 spec: if the freq is not possible then the closest 279 possible value should be selected */ 280 if (freq < radio_range[0] * 16000) 281 freq = radio_range[0] * 16000; 282 else 283 freq = radio_range[1] * 16000; 284 } 285 params.frequency = freq; 286 287 analog_ops->set_params(&t->fe, ¶ms); 288} 289 290static void set_freq(struct i2c_client *c, unsigned long freq) 291{ 292 struct tuner *t = to_tuner(i2c_get_clientdata(c)); 293 294 switch (t->mode) { 295 case V4L2_TUNER_RADIO: 296 tuner_dbg("radio freq set to %lu.%02lu\n", 297 freq / 16000, freq % 16000 * 100 / 16000); 298 set_radio_freq(c, freq); 299 t->radio_freq = freq; 300 break; 301 case V4L2_TUNER_ANALOG_TV: 302 case V4L2_TUNER_DIGITAL_TV: 303 tuner_dbg("tv freq set to %lu.%02lu\n", 304 freq / 16, freq % 16 * 100 / 16); 305 set_tv_freq(c, freq); 306 t->tv_freq = freq; 307 break; 308 default: 309 tuner_dbg("freq set: unknown mode: 0x%04x!\n",t->mode); 310 } 311} 312 313static struct xc5000_config xc5000_cfg; 314 315static void set_type(struct i2c_client *c, unsigned int type, 316 unsigned int new_mode_mask, unsigned int new_config, 317 int (*tuner_callback) (void *dev, int component, int cmd, int arg)) 318{ 319 struct tuner *t = to_tuner(i2c_get_clientdata(c)); 320 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops; 321 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; 322 unsigned char buffer[4]; 323 int tune_now = 1; 324 325 if (type == UNSET || type == TUNER_ABSENT) { 326 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr); 327 return; 328 } 329 330 t->type = type; 331 /* prevent invalid config values */ 332 t->config = new_config < 256 ? new_config : 0; 333 if (tuner_callback != NULL) { 334 tuner_dbg("defining GPIO callback\n"); 335 t->fe.callback = tuner_callback; 336 } 337 338 if (t->mode == T_UNINITIALIZED) { 339 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr); 340 341 return; 342 } 343 344 /* discard private data, in case set_type() was previously called */ 345 tuner_detach(&t->fe); 346 t->fe.analog_demod_priv = NULL; 347 348 switch (t->type) { 349 case TUNER_MT2032: 350 if (!dvb_attach(microtune_attach, 351 &t->fe, t->i2c->adapter, t->i2c->addr)) 352 goto attach_failed; 353 break; 354 case TUNER_PHILIPS_TDA8290: 355 { 356 struct tda829x_config cfg = { 357 .lna_cfg = t->config, 358 }; 359 if (!dvb_attach(tda829x_attach, &t->fe, t->i2c->adapter, 360 t->i2c->addr, &cfg)) 361 goto attach_failed; 362 break; 363 } 364 case TUNER_TEA5767: 365 if (!dvb_attach(tea5767_attach, &t->fe, 366 t->i2c->adapter, t->i2c->addr)) 367 goto attach_failed; 368 t->mode_mask = T_RADIO; 369 break; 370 case TUNER_TEA5761: 371 if (!dvb_attach(tea5761_attach, &t->fe, 372 t->i2c->adapter, t->i2c->addr)) 373 goto attach_failed; 374 t->mode_mask = T_RADIO; 375 break; 376 case TUNER_PHILIPS_FMD1216ME_MK3: 377 buffer[0] = 0x0b; 378 buffer[1] = 0xdc; 379 buffer[2] = 0x9c; 380 buffer[3] = 0x60; 381 i2c_master_send(c, buffer, 4); 382 mdelay(1); 383 buffer[2] = 0x86; 384 buffer[3] = 0x54; 385 i2c_master_send(c, buffer, 4); 386 if (!dvb_attach(simple_tuner_attach, &t->fe, 387 t->i2c->adapter, t->i2c->addr, t->type)) 388 goto attach_failed; 389 break; 390 case TUNER_PHILIPS_TD1316: 391 buffer[0] = 0x0b; 392 buffer[1] = 0xdc; 393 buffer[2] = 0x86; 394 buffer[3] = 0xa4; 395 i2c_master_send(c, buffer, 4); 396 if (!dvb_attach(simple_tuner_attach, &t->fe, 397 t->i2c->adapter, t->i2c->addr, t->type)) 398 goto attach_failed; 399 break; 400 case TUNER_XC2028: 401 { 402 struct xc2028_config cfg = { 403 .i2c_adap = t->i2c->adapter, 404 .i2c_addr = t->i2c->addr, 405 }; 406 if (!dvb_attach(xc2028_attach, &t->fe, &cfg)) 407 goto attach_failed; 408 tune_now = 0; 409 break; 410 } 411 case TUNER_TDA9887: 412 if (!dvb_attach(tda9887_attach, 413 &t->fe, t->i2c->adapter, t->i2c->addr)) 414 goto attach_failed; 415 break; 416 case TUNER_XC5000: 417 { 418 xc5000_cfg.i2c_address = t->i2c->addr; 419 /* if_khz will be set when the digital dvb_attach() occurs */ 420 xc5000_cfg.if_khz = 0; 421 if (!dvb_attach(xc5000_attach, 422 &t->fe, t->i2c->adapter, &xc5000_cfg)) 423 goto attach_failed; 424 tune_now = 0; 425 break; 426 } 427 case TUNER_NXP_TDA18271: 428 { 429 struct tda18271_config cfg = { 430 .config = t->config, 431 }; 432 433 if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr, 434 t->i2c->adapter, &cfg)) 435 goto attach_failed; 436 tune_now = 0; 437 break; 438 } 439 default: 440 if (!dvb_attach(simple_tuner_attach, &t->fe, 441 t->i2c->adapter, t->i2c->addr, t->type)) 442 goto attach_failed; 443 444 break; 445 } 446 447 if ((NULL == analog_ops->set_params) && 448 (fe_tuner_ops->set_analog_params)) { 449 450 t->name = fe_tuner_ops->info.name; 451 452 t->fe.analog_demod_priv = t; 453 memcpy(analog_ops, &tuner_analog_ops, 454 sizeof(struct analog_demod_ops)); 455 456 } else { 457 t->name = analog_ops->info.name; 458 } 459 460 tuner_dbg("type set to %s\n", t->name); 461 462 if (t->mode_mask == T_UNINITIALIZED) 463 t->mode_mask = new_mode_mask; 464 465 if (tune_now) 466 set_freq(c, (V4L2_TUNER_RADIO == t->mode) ? 467 t->radio_freq : t->tv_freq); 468 469 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n", 470 c->adapter->name, c->driver->driver.name, c->addr << 1, type, 471 t->mode_mask); 472 return; 473 474attach_failed: 475 tuner_dbg("Tuner attach for type = %d failed.\n", t->type); 476 t->type = TUNER_ABSENT; 477 t->mode_mask = T_UNINITIALIZED; 478 479 return; 480} 481 482/* 483 * This function apply tuner config to tuner specified 484 * by tun_setup structure. I addr is unset, then admin status 485 * and tun addr status is more precise then current status, 486 * it's applied. Otherwise status and type are applied only to 487 * tuner with exactly the same addr. 488*/ 489 490static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup) 491{ 492 struct tuner *t = to_tuner(i2c_get_clientdata(c)); 493 494 if ( (t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) && 495 (t->mode_mask & tun_setup->mode_mask))) || 496 (tun_setup->addr == c->addr)) { 497 set_type(c, tun_setup->type, tun_setup->mode_mask, 498 tun_setup->config, tun_setup->tuner_callback); 499 } else 500 tuner_dbg("set addr discarded for type %i, mask %x. " 501 "Asked to change tuner at addr 0x%02x, with mask %x\n", 502 t->type, t->mode_mask, 503 tun_setup->addr, tun_setup->mode_mask); 504} 505 506static inline int check_mode(struct tuner *t, char *cmd) 507{ 508 if ((1 << t->mode & t->mode_mask) == 0) { 509 return -EINVAL; 510 } 511 512 switch (t->mode) { 513 case V4L2_TUNER_RADIO: 514 tuner_dbg("Cmd %s accepted for radio\n", cmd); 515 break; 516 case V4L2_TUNER_ANALOG_TV: 517 tuner_dbg("Cmd %s accepted for analog TV\n", cmd); 518 break; 519 case V4L2_TUNER_DIGITAL_TV: 520 tuner_dbg("Cmd %s accepted for digital TV\n", cmd); 521 break; 522 } 523 return 0; 524} 525 526/* get more precise norm info from insmod option */ 527static int tuner_fixup_std(struct tuner *t) 528{ 529 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) { 530 switch (pal[0]) { 531 case '6': 532 tuner_dbg ("insmod fixup: PAL => PAL-60\n"); 533 t->std = V4L2_STD_PAL_60; 534 break; 535 case 'b': 536 case 'B': 537 case 'g': 538 case 'G': 539 tuner_dbg ("insmod fixup: PAL => PAL-BG\n"); 540 t->std = V4L2_STD_PAL_BG; 541 break; 542 case 'i': 543 case 'I': 544 tuner_dbg ("insmod fixup: PAL => PAL-I\n"); 545 t->std = V4L2_STD_PAL_I; 546 break; 547 case 'd': 548 case 'D': 549 case 'k': 550 case 'K': 551 tuner_dbg ("insmod fixup: PAL => PAL-DK\n"); 552 t->std = V4L2_STD_PAL_DK; 553 break; 554 case 'M': 555 case 'm': 556 tuner_dbg ("insmod fixup: PAL => PAL-M\n"); 557 t->std = V4L2_STD_PAL_M; 558 break; 559 case 'N': 560 case 'n': 561 if (pal[1] == 'c' || pal[1] == 'C') { 562 tuner_dbg("insmod fixup: PAL => PAL-Nc\n"); 563 t->std = V4L2_STD_PAL_Nc; 564 } else { 565 tuner_dbg ("insmod fixup: PAL => PAL-N\n"); 566 t->std = V4L2_STD_PAL_N; 567 } 568 break; 569 case '-': 570 /* default parameter, do nothing */ 571 break; 572 default: 573 tuner_warn ("pal= argument not recognised\n"); 574 break; 575 } 576 } 577 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) { 578 switch (secam[0]) { 579 case 'b': 580 case 'B': 581 case 'g': 582 case 'G': 583 case 'h': 584 case 'H': 585 tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n"); 586 t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H; 587 break; 588 case 'd': 589 case 'D': 590 case 'k': 591 case 'K': 592 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n"); 593 t->std = V4L2_STD_SECAM_DK; 594 break; 595 case 'l': 596 case 'L': 597 if ((secam[1]=='C')||(secam[1]=='c')) { 598 tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n"); 599 t->std = V4L2_STD_SECAM_LC; 600 } else { 601 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n"); 602 t->std = V4L2_STD_SECAM_L; 603 } 604 break; 605 case '-': 606 /* default parameter, do nothing */ 607 break; 608 default: 609 tuner_warn ("secam= argument not recognised\n"); 610 break; 611 } 612 } 613 614 if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) { 615 switch (ntsc[0]) { 616 case 'm': 617 case 'M': 618 tuner_dbg("insmod fixup: NTSC => NTSC-M\n"); 619 t->std = V4L2_STD_NTSC_M; 620 break; 621 case 'j': 622 case 'J': 623 tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n"); 624 t->std = V4L2_STD_NTSC_M_JP; 625 break; 626 case 'k': 627 case 'K': 628 tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n"); 629 t->std = V4L2_STD_NTSC_M_KR; 630 break; 631 case '-': 632 /* default parameter, do nothing */ 633 break; 634 default: 635 tuner_info("ntsc= argument not recognised\n"); 636 break; 637 } 638 } 639 return 0; 640} 641 642static void tuner_status(struct dvb_frontend *fe) 643{ 644 struct tuner *t = fe->analog_demod_priv; 645 unsigned long freq, freq_fraction; 646 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops; 647 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops; 648 const char *p; 649 650 switch (t->mode) { 651 case V4L2_TUNER_RADIO: p = "radio"; break; 652 case V4L2_TUNER_ANALOG_TV: p = "analog TV"; break; 653 case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break; 654 default: p = "undefined"; break; 655 } 656 if (t->mode == V4L2_TUNER_RADIO) { 657 freq = t->radio_freq / 16000; 658 freq_fraction = (t->radio_freq % 16000) * 100 / 16000; 659 } else { 660 freq = t->tv_freq / 16; 661 freq_fraction = (t->tv_freq % 16) * 100 / 16; 662 } 663 tuner_info("Tuner mode: %s\n", p); 664 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction); 665 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std); 666 if (t->mode != V4L2_TUNER_RADIO) 667 return; 668 if (fe_tuner_ops->get_status) { 669 u32 tuner_status; 670 671 fe_tuner_ops->get_status(&t->fe, &tuner_status); 672 if (tuner_status & TUNER_STATUS_LOCKED) 673 tuner_info("Tuner is locked.\n"); 674 if (tuner_status & TUNER_STATUS_STEREO) 675 tuner_info("Stereo: yes\n"); 676 } 677 if (analog_ops->has_signal) 678 tuner_info("Signal strength: %d\n", 679 analog_ops->has_signal(fe)); 680 if (analog_ops->is_stereo) 681 tuner_info("Stereo: %s\n", 682 analog_ops->is_stereo(fe) ? "yes" : "no"); 683} 684 685/* ---------------------------------------------------------------------- */ 686 687/* 688 * Switch tuner to other mode. If tuner support both tv and radio, 689 * set another frequency to some value (This is needed for some pal 690 * tuners to avoid locking). Otherwise, just put second tuner in 691 * standby mode. 692 */ 693 694static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd) 695{ 696 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; 697 698 if (mode == t->mode) 699 return 0; 700 701 t->mode = mode; 702 703 if (check_mode(t, cmd) == -EINVAL) { 704 tuner_dbg("Tuner doesn't support this mode. " 705 "Putting tuner to sleep\n"); 706 t->mode = T_STANDBY; 707 if (analog_ops->standby) 708 analog_ops->standby(&t->fe); 709 return -EINVAL; 710 } 711 return 0; 712} 713 714#define switch_v4l2() if (!t->using_v4l2) \ 715 tuner_dbg("switching to v4l2\n"); \ 716 t->using_v4l2 = 1; 717 718static inline int check_v4l2(struct tuner *t) 719{ 720 /* bttv still uses both v4l1 and v4l2 calls to the tuner (v4l2 for 721 TV, v4l1 for radio), until that is fixed this code is disabled. 722 Otherwise the radio (v4l1) wouldn't tune after using the TV (v4l2) 723 first. */ 724 return 0; 725} 726 727static int tuner_s_type_addr(struct v4l2_subdev *sd, struct tuner_setup *type) 728{ 729 struct tuner *t = to_tuner(sd); 730 struct i2c_client *client = v4l2_get_subdevdata(sd); 731 732 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n", 733 type->type, 734 type->addr, 735 type->mode_mask, 736 type->config); 737 738 set_addr(client, type); 739 return 0; 740} 741 742static int tuner_s_radio(struct v4l2_subdev *sd) 743{ 744 struct tuner *t = to_tuner(sd); 745 struct i2c_client *client = v4l2_get_subdevdata(sd); 746 747 if (set_mode(client, t, V4L2_TUNER_RADIO, "s_radio") == -EINVAL) 748 return 0; 749 if (t->radio_freq) 750 set_freq(client, t->radio_freq); 751 return 0; 752} 753 754static int tuner_s_power(struct v4l2_subdev *sd, int on) 755{ 756 struct tuner *t = to_tuner(sd); 757 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; 758 759 if (on) 760 return 0; 761 762 tuner_dbg("Putting tuner to sleep\n"); 763 764 if (check_mode(t, "s_power") == -EINVAL) 765 return 0; 766 t->mode = T_STANDBY; 767 if (analog_ops->standby) 768 analog_ops->standby(&t->fe); 769 return 0; 770} 771 772static int tuner_s_config(struct v4l2_subdev *sd, const struct v4l2_priv_tun_config *cfg) 773{ 774 struct tuner *t = to_tuner(sd); 775 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; 776 777 if (t->type != cfg->tuner) 778 return 0; 779 780 if (analog_ops->set_config) { 781 analog_ops->set_config(&t->fe, cfg->priv); 782 return 0; 783 } 784 785 tuner_dbg("Tuner frontend module has no way to set config\n"); 786 return 0; 787} 788 789/* --- v4l ioctls --- */ 790/* take care: bttv does userspace copying, we'll get a 791 kernel pointer here... */ 792static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std) 793{ 794 struct tuner *t = to_tuner(sd); 795 struct i2c_client *client = v4l2_get_subdevdata(sd); 796 797 if (set_mode(client, t, V4L2_TUNER_ANALOG_TV, "s_std") == -EINVAL) 798 return 0; 799 800 switch_v4l2(); 801 802 t->std = std; 803 tuner_fixup_std(t); 804 if (t->tv_freq) 805 set_freq(client, t->tv_freq); 806 return 0; 807} 808 809static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f) 810{ 811 struct tuner *t = to_tuner(sd); 812 struct i2c_client *client = v4l2_get_subdevdata(sd); 813 814 if (set_mode(client, t, f->type, "s_frequency") == -EINVAL) 815 return 0; 816 switch_v4l2(); 817 set_freq(client, f->frequency); 818 819 return 0; 820} 821 822static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f) 823{ 824 struct tuner *t = to_tuner(sd); 825 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops; 826 827 if (check_mode(t, "g_frequency") == -EINVAL) 828 return 0; 829 switch_v4l2(); 830 f->type = t->mode; 831 if (fe_tuner_ops->get_frequency) { 832 u32 abs_freq; 833 834 fe_tuner_ops->get_frequency(&t->fe, &abs_freq); 835 f->frequency = (V4L2_TUNER_RADIO == t->mode) ? 836 DIV_ROUND_CLOSEST(abs_freq * 2, 125) : 837 DIV_ROUND_CLOSEST(abs_freq, 62500); 838 return 0; 839 } 840 f->frequency = (V4L2_TUNER_RADIO == t->mode) ? 841 t->radio_freq : t->tv_freq; 842 return 0; 843} 844 845static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt) 846{ 847 struct tuner *t = to_tuner(sd); 848 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; 849 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops; 850 851 if (check_mode(t, "g_tuner") == -EINVAL) 852 return 0; 853 switch_v4l2(); 854 855 vt->type = t->mode; 856 if (analog_ops->get_afc) 857 vt->afc = analog_ops->get_afc(&t->fe); 858 if (t->mode == V4L2_TUNER_ANALOG_TV) 859 vt->capability |= V4L2_TUNER_CAP_NORM; 860 if (t->mode != V4L2_TUNER_RADIO) { 861 vt->rangelow = tv_range[0] * 16; 862 vt->rangehigh = tv_range[1] * 16; 863 return 0; 864 } 865 866 /* radio mode */ 867 vt->rxsubchans = 868 V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO; 869 if (fe_tuner_ops->get_status) { 870 u32 tuner_status; 871 872 fe_tuner_ops->get_status(&t->fe, &tuner_status); 873 vt->rxsubchans = 874 (tuner_status & TUNER_STATUS_STEREO) ? 875 V4L2_TUNER_SUB_STEREO : 876 V4L2_TUNER_SUB_MONO; 877 } else { 878 if (analog_ops->is_stereo) { 879 vt->rxsubchans = 880 analog_ops->is_stereo(&t->fe) ? 881 V4L2_TUNER_SUB_STEREO : 882 V4L2_TUNER_SUB_MONO; 883 } 884 } 885 if (analog_ops->has_signal) 886 vt->signal = analog_ops->has_signal(&t->fe); 887 vt->capability |= 888 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO; 889 vt->audmode = t->audmode; 890 vt->rangelow = radio_range[0] * 16000; 891 vt->rangehigh = radio_range[1] * 16000; 892 return 0; 893} 894 895static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt) 896{ 897 struct tuner *t = to_tuner(sd); 898 struct i2c_client *client = v4l2_get_subdevdata(sd); 899 900 if (check_mode(t, "s_tuner") == -EINVAL) 901 return 0; 902 903 switch_v4l2(); 904 905 /* do nothing unless we're a radio tuner */ 906 if (t->mode != V4L2_TUNER_RADIO) 907 return 0; 908 t->audmode = vt->audmode; 909 set_radio_freq(client, t->radio_freq); 910 return 0; 911} 912 913static int tuner_log_status(struct v4l2_subdev *sd) 914{ 915 struct tuner *t = to_tuner(sd); 916 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; 917 918 if (analog_ops->tuner_status) 919 analog_ops->tuner_status(&t->fe); 920 return 0; 921} 922 923static int tuner_suspend(struct i2c_client *c, pm_message_t state) 924{ 925 struct tuner *t = to_tuner(i2c_get_clientdata(c)); 926 927 tuner_dbg("suspend\n"); 928 return 0; 929} 930 931static int tuner_resume(struct i2c_client *c) 932{ 933 struct tuner *t = to_tuner(i2c_get_clientdata(c)); 934 935 tuner_dbg("resume\n"); 936 if (V4L2_TUNER_RADIO == t->mode) { 937 if (t->radio_freq) 938 set_freq(c, t->radio_freq); 939 } else { 940 if (t->tv_freq) 941 set_freq(c, t->tv_freq); 942 } 943 return 0; 944} 945 946static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg) 947{ 948 struct v4l2_subdev *sd = i2c_get_clientdata(client); 949 950 /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have 951 to handle it here. 952 There must be a better way of doing this... */ 953 switch (cmd) { 954 case TUNER_SET_CONFIG: 955 return tuner_s_config(sd, arg); 956 } 957 return -ENOIOCTLCMD; 958} 959 960/* ----------------------------------------------------------------------- */ 961 962static const struct v4l2_subdev_core_ops tuner_core_ops = { 963 .log_status = tuner_log_status, 964 .s_std = tuner_s_std, 965 .s_power = tuner_s_power, 966}; 967 968static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = { 969 .s_radio = tuner_s_radio, 970 .g_tuner = tuner_g_tuner, 971 .s_tuner = tuner_s_tuner, 972 .s_frequency = tuner_s_frequency, 973 .g_frequency = tuner_g_frequency, 974 .s_type_addr = tuner_s_type_addr, 975 .s_config = tuner_s_config, 976}; 977 978static const struct v4l2_subdev_ops tuner_ops = { 979 .core = &tuner_core_ops, 980 .tuner = &tuner_tuner_ops, 981}; 982 983/* ---------------------------------------------------------------------- */ 984 985static LIST_HEAD(tuner_list); 986 987/* Search for existing radio and/or TV tuners on the given I2C adapter. 988 Note that when this function is called from tuner_probe you can be 989 certain no other devices will be added/deleted at the same time, I2C 990 core protects against that. */ 991static void tuner_lookup(struct i2c_adapter *adap, 992 struct tuner **radio, struct tuner **tv) 993{ 994 struct tuner *pos; 995 996 *radio = NULL; 997 *tv = NULL; 998 999 list_for_each_entry(pos, &tuner_list, list) { 1000 int mode_mask; 1001 1002 if (pos->i2c->adapter != adap || 1003 strcmp(pos->i2c->driver->driver.name, "tuner")) 1004 continue; 1005 1006 mode_mask = pos->mode_mask & ~T_STANDBY; 1007 if (*radio == NULL && mode_mask == T_RADIO) 1008 *radio = pos; 1009 /* Note: currently TDA9887 is the only demod-only 1010 device. If other devices appear then we need to 1011 make this test more general. */ 1012 else if (*tv == NULL && pos->type != TUNER_TDA9887 && 1013 (pos->mode_mask & (T_ANALOG_TV | T_DIGITAL_TV))) 1014 *tv = pos; 1015 } 1016} 1017 1018/* During client attach, set_type is called by adapter's attach_inform callback. 1019 set_type must then be completed by tuner_probe. 1020 */ 1021static int tuner_probe(struct i2c_client *client, 1022 const struct i2c_device_id *id) 1023{ 1024 struct tuner *t; 1025 struct tuner *radio; 1026 struct tuner *tv; 1027 1028 t = kzalloc(sizeof(struct tuner), GFP_KERNEL); 1029 if (NULL == t) 1030 return -ENOMEM; 1031 v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops); 1032 t->i2c = client; 1033 t->name = "(tuner unset)"; 1034 t->type = UNSET; 1035 t->audmode = V4L2_TUNER_MODE_STEREO; 1036 t->mode_mask = T_UNINITIALIZED; 1037 1038 if (show_i2c) { 1039 unsigned char buffer[16]; 1040 int i, rc; 1041 1042 memset(buffer, 0, sizeof(buffer)); 1043 rc = i2c_master_recv(client, buffer, sizeof(buffer)); 1044 tuner_info("I2C RECV = "); 1045 for (i = 0; i < rc; i++) 1046 printk(KERN_CONT "%02x ", buffer[i]); 1047 printk("\n"); 1048 } 1049 /* HACK: This test was added to avoid tuner to probe tda9840 and 1050 tea6415c on the MXB card */ 1051 if (client->adapter->id == I2C_HW_SAA7146 && client->addr < 0x4a) { 1052 kfree(t); 1053 return -ENODEV; 1054 } 1055 1056 /* autodetection code based on the i2c addr */ 1057 if (!no_autodetect) { 1058 switch (client->addr) { 1059 case 0x10: 1060 if (tuner_symbol_probe(tea5761_autodetection, 1061 t->i2c->adapter, 1062 t->i2c->addr) >= 0) { 1063 t->type = TUNER_TEA5761; 1064 t->mode_mask = T_RADIO; 1065 t->mode = T_STANDBY; 1066 /* Sets freq to FM range */ 1067 t->radio_freq = 87.5 * 16000; 1068 tuner_lookup(t->i2c->adapter, &radio, &tv); 1069 if (tv) 1070 tv->mode_mask &= ~T_RADIO; 1071 1072 goto register_client; 1073 } 1074 kfree(t); 1075 return -ENODEV; 1076 case 0x42: 1077 case 0x43: 1078 case 0x4a: 1079 case 0x4b: 1080 /* If chip is not tda8290, don't register. 1081 since it can be tda9887*/ 1082 if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter, 1083 t->i2c->addr) >= 0) { 1084 tuner_dbg("tda829x detected\n"); 1085 } else { 1086 /* Default is being tda9887 */ 1087 t->type = TUNER_TDA9887; 1088 t->mode_mask = T_RADIO | T_ANALOG_TV | 1089 T_DIGITAL_TV; 1090 t->mode = T_STANDBY; 1091 goto register_client; 1092 } 1093 break; 1094 case 0x60: 1095 if (tuner_symbol_probe(tea5767_autodetection, 1096 t->i2c->adapter, t->i2c->addr) 1097 >= 0) { 1098 t->type = TUNER_TEA5767; 1099 t->mode_mask = T_RADIO; 1100 t->mode = T_STANDBY; 1101 /* Sets freq to FM range */ 1102 t->radio_freq = 87.5 * 16000; 1103 tuner_lookup(t->i2c->adapter, &radio, &tv); 1104 if (tv) 1105 tv->mode_mask &= ~T_RADIO; 1106 1107 goto register_client; 1108 } 1109 break; 1110 } 1111 } 1112 1113 /* Initializes only the first TV tuner on this adapter. Why only the 1114 first? Because there are some devices (notably the ones with TI 1115 tuners) that have more than one i2c address for the *same* device. 1116 Experience shows that, except for just one case, the first 1117 address is the right one. The exception is a Russian tuner 1118 (ACORP_Y878F). So, the desired behavior is just to enable the 1119 first found TV tuner. */ 1120 tuner_lookup(t->i2c->adapter, &radio, &tv); 1121 if (tv == NULL) { 1122 t->mode_mask = T_ANALOG_TV | T_DIGITAL_TV; 1123 if (radio == NULL) 1124 t->mode_mask |= T_RADIO; 1125 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask); 1126 t->tv_freq = 400 * 16; /* Sets freq to VHF High */ 1127 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */ 1128 } 1129 1130 /* Should be just before return */ 1131register_client: 1132 tuner_info("chip found @ 0x%x (%s)\n", client->addr << 1, 1133 client->adapter->name); 1134 1135 /* Sets a default mode */ 1136 if (t->mode_mask & T_ANALOG_TV) { 1137 t->mode = V4L2_TUNER_ANALOG_TV; 1138 } else if (t->mode_mask & T_RADIO) { 1139 t->mode = V4L2_TUNER_RADIO; 1140 } else { 1141 t->mode = V4L2_TUNER_DIGITAL_TV; 1142 } 1143 set_type(client, t->type, t->mode_mask, t->config, t->fe.callback); 1144 list_add_tail(&t->list, &tuner_list); 1145 return 0; 1146} 1147 1148static int tuner_remove(struct i2c_client *client) 1149{ 1150 struct tuner *t = to_tuner(i2c_get_clientdata(client)); 1151 1152 v4l2_device_unregister_subdev(&t->sd); 1153 tuner_detach(&t->fe); 1154 t->fe.analog_demod_priv = NULL; 1155 1156 list_del(&t->list); 1157 kfree(t); 1158 return 0; 1159} 1160 1161/* ----------------------------------------------------------------------- */ 1162 1163/* This driver supports many devices and the idea is to let the driver 1164 detect which device is present. So rather than listing all supported 1165 devices here, we pretend to support a single, fake device type. */ 1166static const struct i2c_device_id tuner_id[] = { 1167 { "tuner", }, /* autodetect */ 1168 { } 1169}; 1170MODULE_DEVICE_TABLE(i2c, tuner_id); 1171 1172static struct v4l2_i2c_driver_data v4l2_i2c_data = { 1173 .name = "tuner", 1174 .probe = tuner_probe, 1175 .remove = tuner_remove, 1176 .command = tuner_command, 1177 .suspend = tuner_suspend, 1178 .resume = tuner_resume, 1179 .id_table = tuner_id, 1180}; 1181 1182/* 1183 * Overrides for Emacs so that we follow Linus's tabbing style. 1184 * --------------------------------------------------------------------------- 1185 * Local variables: 1186 * c-basic-offset: 8 1187 * End: 1188 */ 1189