📄 tuner.c
字号:
dprintk("mt2032 optimize_vco: sel=%d\n",sel); buf[0]=0x0f; buf[1]=sel; i2c_master_send(c,buf,2); lock=mt2032_check_lo_lock(c); return lock;}void mt2032_set_if_freq(struct i2c_client *c,int rfin, int if1, int if2, int from, int to){ unsigned char buf[21]; int lint_try,ret,sel,lock=0; struct tuner *t = (struct tuner*)c->data; dprintk("mt2032_set_if_freq rfin=%d if1=%d if2=%d from=%d to=%d\n",rfin,if1,if2,from,to); buf[0]=0; ret=i2c_master_send(c,buf,1); i2c_master_recv(c,buf,21); buf[0]=0; ret=mt2032_compute_freq(rfin,if1,if2,from,to,&buf[1],&sel,t->xogc); if (ret<0) return; // send only the relevant registers per Rev. 1.2 buf[0]=0; ret=i2c_master_send(c,buf,4); buf[5]=5; ret=i2c_master_send(c,buf+5,4); buf[11]=11; ret=i2c_master_send(c,buf+11,3); if(ret!=3) printk("mt2032_set_if_freq failed with %d\n",ret); // wait for PLLs to lock (per manual), retry LINT if not. for(lint_try=0; lint_try<2; lint_try++) { lock=mt2032_check_lo_lock(c); if(optimize_vco) lock=mt2032_optimize_vco(c,sel,lock); if(lock==6) break; printk("mt2032: re-init PLLs by LINT\n"); buf[0]=7; buf[1]=0x80 +8+t->xogc; // set LINT to re-init PLLs i2c_master_send(c,buf,2); mdelay(10); buf[1]=8+t->xogc; i2c_master_send(c,buf,2); } if (lock!=6) printk("MT2032 Fatal Error: PLLs didn't lock.\n"); buf[0]=2; buf[1]=0x20; // LOGC for optimal phase noise ret=i2c_master_send(c,buf,2); if (ret!=2) printk("mt2032_set_if_freq2 failed with %d\n",ret);}void mt2032_set_tv_freq(struct i2c_client *c, int freq, int norm){ int if2,from,to; // signal bandwidth and picture carrier if(norm==VIDEO_MODE_NTSC) { from=40750*1000; to=46750*1000; if2=45750*1000; } else { // Pal from=32900*1000; to=39900*1000; if2=38900*1000; } mt2032_set_if_freq(c,freq* 1000*1000/16, 1090*1000*1000, if2, from, to);}// Set tuner frequency, freq in Units of 62.5kHz = 1/16MHzstatic void set_tv_freq(struct i2c_client *c, int freq){ u8 config; u16 div; struct tunertype *tun; struct tuner *t = c->data; unsigned char buffer[4]; int rc; if (t->type == -1) { printk("tuner: tuner type not set\n"); return; } if (t->type == TUNER_MT2032) { mt2032_set_tv_freq(c,freq,t->mode); return; } if (freq < tv_range[0]*16 || freq > tv_range[1]*16) { /* FIXME: better do that chip-specific, but right now we don't have that in the config struct and this way is still better than no check at all */ printk("tuner: TV freq (%d.%02d) out of range (%d-%d)\n", freq/16,freq%16*100/16,tv_range[0],tv_range[1]); return; } tun=&tuners[t->type]; if (freq < tun->thresh1) config = tun->VHF_L; else if (freq < tun->thresh2) config = tun->VHF_H; else config = tun->UHF; /* tv norm specific stuff for multi-norm tuners */ switch (t->type) { case TUNER_PHILIPS_SECAM: /* 0x01 -> ??? no change ??? */ /* 0x02 -> PAL BDGHI / SECAM L */ /* 0x04 -> ??? PAL others / SECAM others ??? */ config &= ~0x02; if (t->mode == VIDEO_MODE_SECAM) config |= 0x02; break; case TUNER_TEMIC_4046FM5: config &= ~0x0f; switch (pal[0]) { case 'i': case 'I': config |= TEMIC_SET_PAL_I; break; case 'd': case 'D': config |= TEMIC_SET_PAL_DK; break; case 'l': case 'L': config |= TEMIC_SET_PAL_L; break; case 'b': case 'B': case 'g': case 'G': default: config |= TEMIC_SET_PAL_BG; break; break; } case TUNER_PHILIPS_FQ1216ME: config &= ~0x0f; switch (pal[0]) { case 'i': case 'I': config |= PHILIPS_SET_PAL_I; break; case 'l': case 'L': config |= PHILIPS_SET_PAL_L; break; case 'd': case 'D': case 'b': case 'B': case 'g': case 'G': config |= PHILIPS_SET_PAL_BGDK; break; break; } } /* * Philips FI1216MK2 remark from specification : * for channel selection involving band switching, and to ensure * smooth tuning to the desired channel without causing * unnecessary charge pump action, it is recommended to consider * the difference between wanted channel frequency and the * current channel frequency. Unnecessary charge pump action * will result in very low tuning voltage which may drive the * oscillator to extreme conditions. * * Progfou: specification says to send config data before * frequency in case (wanted frequency < current frequency). */ div=freq + tun->IFPCoff; if (t->type == TUNER_PHILIPS_SECAM && freq < t->freq) { buffer[0] = tun->config; buffer[1] = config; buffer[2] = (div>>8) & 0x7f; buffer[3] = div & 0xff; } else { buffer[0] = (div>>8) & 0x7f; buffer[1] = div & 0xff; buffer[2] = tun->config; buffer[3] = config; } if (4 != (rc = i2c_master_send(c,buffer,4))) printk("tuner: i2c i/o error: rc == %d (should be 4)\n",rc);}static void mt2032_set_radio_freq(struct i2c_client *c,int freq){ int if2; if2=10700*1000; // 10.7MHz FM intermediate frequency mt2032_set_if_freq(c,freq* 1000*1000/16, 1085*1000*1000,if2,if2,if2);}static void set_radio_freq(struct i2c_client *c, int freq){ u8 config; u16 div; struct tunertype *tun; struct tuner *t = (struct tuner*)c->data; unsigned char buffer[4]; int rc; if (freq < radio_range[0]*16 || freq > radio_range[1]*16) { printk("tuner: radio freq (%d.%02d) out of range (%d-%d)\n", freq/16,freq%16*100/16, radio_range[0],radio_range[1]); return; } if (t->type == -1) { printk("tuner: tuner type not set\n"); return; } if (t->type == TUNER_MT2032) { mt2032_set_radio_freq(c,freq); return; } tun=&tuners[t->type]; config = 0xa4 /* 0xa5 */; /* bit 0 is AFC (set) vs. RF-Signal (clear) */ div=freq + (int)(16*10.7); div&=0x7fff; buffer[0] = (div>>8) & 0x7f; buffer[1] = div & 0xff; buffer[2] = tun->config; buffer[3] = config; if (4 != (rc = i2c_master_send(c,buffer,4))) printk("tuner: i2c i/o error: rc == %d (should be 4)\n",rc); if (debug) { current->state = TASK_INTERRUPTIBLE; schedule_timeout(HZ/10); if (tuner_islocked (c)) printk ("tuner: PLL locked\n"); else printk ("tuner: PLL not locked\n"); if (config & 1) { printk ("tuner: AFC: %d\n", tuner_afcstatus(c)); } else { printk ("tuner: Signal: %d\n", tuner_signal(c)); } }}/* ---------------------------------------------------------------------- */static int tuner_attach(struct i2c_adapter *adap, int addr, unsigned short flags, int kind){ struct tuner *t; struct i2c_client *client; if (this_adap > 0) return -1; this_adap++; client_template.adapter = adap; client_template.addr = addr; printk("tuner: chip found @ 0x%x\n", addr<<1); if (NULL == (client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL))) return -ENOMEM; memcpy(client,&client_template,sizeof(struct i2c_client)); client->data = t = kmalloc(sizeof(struct tuner),GFP_KERNEL); if (NULL == t) { kfree(client); return -ENOMEM; } memset(t,0,sizeof(struct tuner)); if (type >= 0 && type < TUNERS) { t->type = type; strncpy(client->name, tuners[t->type].name, sizeof(client->name)); } else { t->type = -1; } i2c_attach_client(client); if (t->type == TUNER_MT2032) mt2032_init(client); MOD_INC_USE_COUNT; return 0;}static int tuner_probe(struct i2c_adapter *adap){ if (0 != addr) { normal_i2c_range[0] = addr; normal_i2c_range[1] = addr; } this_adap = 0; if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848)) return i2c_probe(adap, &addr_data, tuner_attach); return 0;}static int tuner_detach(struct i2c_client *client){ struct tuner *t = (struct tuner*)client->data; i2c_detach_client(client); kfree(t); kfree(client); MOD_DEC_USE_COUNT; return 0;}static inttuner_command(struct i2c_client *client, unsigned int cmd, void *arg){ struct tuner *t = (struct tuner*)client->data; int *iarg = (int*)arg;#if 0 __u16 *sarg = (__u16*)arg;#endif switch (cmd) { /* --- configuration --- */ case TUNER_SET_TYPE: if (t->type != -1) return 0; if (*iarg < 0 || *iarg >= TUNERS) return 0; t->type = *iarg; dprintk("tuner: type set to %d (%s)\n", t->type,tuners[t->type].name); strncpy(client->name, tuners[t->type].name, sizeof(client->name)); if (t->type == TUNER_MT2032) mt2032_init(client); break; case AUDC_SET_RADIO: t->radio = 1; break; /* --- v4l ioctls --- */ /* take care: bttv does userspace copying, we'll get a kernel pointer here... */ case VIDIOCSCHAN: { struct video_channel *vc = arg; t->radio = 0; t->mode = vc->norm; if (t->freq) set_tv_freq(client,t->freq); return 0; } case VIDIOCSFREQ: { unsigned long *v = arg; if (t->radio) { dprintk("tuner: radio freq set to %d.%02d\n", (*iarg)/16,(*iarg)%16*100/16); set_radio_freq(client,*v); } else { dprintk("tuner: tv freq set to %d.%02d\n", (*iarg)/16,(*iarg)%16*100/16); set_tv_freq(client,*v); } t->freq = *v; return 0; } case VIDIOCGTUNER: { struct video_tuner *vt = arg; if (t->radio) vt->signal = tuner_signal(client); return 0; } case VIDIOCGAUDIO: { struct video_audio *va = arg; if (t->radio) va->mode = (tuner_stereo(client) ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO); return 0; } #if 0 /* --- old, obsolete interface --- */ case TUNER_SET_TVFREQ: dprintk("tuner: tv freq set to %d.%02d\n", (*iarg)/16,(*iarg)%16*100/16); set_tv_freq(client,*iarg); t->radio = 0; t->freq = *iarg; break; case TUNER_SET_RADIOFREQ: dprintk("tuner: radio freq set to %d.%02d\n", (*iarg)/16,(*iarg)%16*100/16); set_radio_freq(client,*iarg); t->radio = 1; t->freq = *iarg; break; case TUNER_SET_MODE: if (t->type != TUNER_PHILIPS_SECAM) { dprintk("tuner: trying to change mode for other than TUNER_PHILIPS_SECAM\n"); } else { int mode=(*sarg==VIDEO_MODE_SECAM)?1:0; dprintk("tuner: mode set to %d\n", *sarg); t->mode = mode; set_tv_freq(client,t->freq); } break;#endif default: /* nothing */ break; } return 0;}/* ----------------------------------------------------------------------- */static struct i2c_driver driver = { "i2c TV tuner driver", I2C_DRIVERID_TUNER, I2C_DF_NOTIFY, tuner_probe, tuner_detach, tuner_command,};static struct i2c_client client_template ={ "(unset)", /* name */ -1, 0, 0, NULL, &driver};EXPORT_NO_SYMBOLS;int tuner_init_module(void){ i2c_add_driver(&driver); return 0;}void tuner_cleanup_module(void){ i2c_del_driver(&driver);}module_init(tuner_init_module);module_exit(tuner_cleanup_module);/* * Overrides for Emacs so that we follow Linus's tabbing style. * --------------------------------------------------------------------------- * Local variables: * c-basic-offset: 8 * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -