Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
| 3 | * i2c tv tuner chip device driver |
| 4 | * controls microtune tuners, mt2032 + mt2050 at the moment. |
| 5 | */ |
| 6 | #include <linux/delay.h> |
| 7 | #include <linux/i2c.h> |
| 8 | #include <linux/videodev.h> |
| 9 | #include <linux/moduleparam.h> |
| 10 | #include <media/tuner.h> |
| 11 | |
| 12 | /* ---------------------------------------------------------------------- */ |
| 13 | |
| 14 | static unsigned int optimize_vco = 1; |
| 15 | module_param(optimize_vco, int, 0644); |
| 16 | |
| 17 | static unsigned int tv_antenna = 1; |
| 18 | module_param(tv_antenna, int, 0644); |
| 19 | |
| 20 | static unsigned int radio_antenna = 0; |
| 21 | module_param(radio_antenna, int, 0644); |
| 22 | |
Hans Verkuil | fac9e89 | 2006-01-09 15:32:40 -0200 | [diff] [blame] | 23 | /* from tuner-core.c */ |
Hans Verkuil | f9195de | 2006-01-11 19:01:01 -0200 | [diff] [blame] | 24 | extern int tuner_debug; |
Hans Verkuil | fac9e89 | 2006-01-09 15:32:40 -0200 | [diff] [blame] | 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | /* ---------------------------------------------------------------------- */ |
| 27 | |
| 28 | #define MT2032 0x04 |
| 29 | #define MT2030 0x06 |
| 30 | #define MT2040 0x07 |
| 31 | #define MT2050 0x42 |
| 32 | |
| 33 | static char *microtune_part[] = { |
| 34 | [ MT2030 ] = "MT2030", |
| 35 | [ MT2032 ] = "MT2032", |
| 36 | [ MT2040 ] = "MT2040", |
| 37 | [ MT2050 ] = "MT2050", |
| 38 | }; |
| 39 | |
Michael Krufky | b208319 | 2007-05-29 22:54:06 -0300 | [diff] [blame] | 40 | struct microtune_priv { |
| 41 | unsigned int xogc; |
| 42 | unsigned int radio_if2; |
| 43 | }; |
| 44 | |
Michael Krufky | c22bcb0 | 2007-06-06 16:14:18 -0300 | [diff] [blame^] | 45 | static void microtune_release(struct i2c_client *c) |
| 46 | { |
| 47 | struct tuner *t = i2c_get_clientdata(c); |
| 48 | |
| 49 | kfree(t->priv); |
| 50 | t->priv = NULL; |
| 51 | } |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | // IsSpurInBand()? |
| 54 | static int mt2032_spurcheck(struct i2c_client *c, |
| 55 | int f1, int f2, int spectrum_from,int spectrum_to) |
| 56 | { |
| 57 | struct tuner *t = i2c_get_clientdata(c); |
| 58 | int n1=1,n2,f; |
| 59 | |
| 60 | f1=f1/1000; //scale to kHz to avoid 32bit overflows |
| 61 | f2=f2/1000; |
| 62 | spectrum_from/=1000; |
| 63 | spectrum_to/=1000; |
| 64 | |
| 65 | tuner_dbg("spurcheck f1=%d f2=%d from=%d to=%d\n", |
| 66 | f1,f2,spectrum_from,spectrum_to); |
| 67 | |
| 68 | do { |
| 69 | n2=-n1; |
| 70 | f=n1*(f1-f2); |
| 71 | do { |
| 72 | n2--; |
| 73 | f=f-f2; |
| 74 | tuner_dbg("spurtest n1=%d n2=%d ftest=%d\n",n1,n2,f); |
| 75 | |
| 76 | if( (f>spectrum_from) && (f<spectrum_to)) |
| 77 | tuner_dbg("mt2032 spurcheck triggered: %d\n",n1); |
| 78 | } while ( (f>(f2-spectrum_to)) || (n2>-5)); |
| 79 | n1++; |
| 80 | } while (n1<5); |
| 81 | |
| 82 | return 1; |
| 83 | } |
| 84 | |
| 85 | static int mt2032_compute_freq(struct i2c_client *c, |
| 86 | unsigned int rfin, |
| 87 | unsigned int if1, unsigned int if2, |
| 88 | unsigned int spectrum_from, |
| 89 | unsigned int spectrum_to, |
| 90 | unsigned char *buf, |
| 91 | int *ret_sel, |
| 92 | unsigned int xogc) //all in Hz |
| 93 | { |
| 94 | struct tuner *t = i2c_get_clientdata(c); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 95 | unsigned int fref,lo1,lo1n,lo1a,s,sel,lo1freq, desired_lo1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | desired_lo2,lo2,lo2n,lo2a,lo2num,lo2freq; |
| 97 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 98 | fref= 5250 *1000; //5.25MHz |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | desired_lo1=rfin+if1; |
| 100 | |
| 101 | lo1=(2*(desired_lo1/1000)+(fref/1000)) / (2*fref/1000); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 102 | lo1n=lo1/8; |
| 103 | lo1a=lo1-(lo1n*8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 105 | s=rfin/1000/1000+1090; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | if(optimize_vco) { |
| 108 | if(s>1890) sel=0; |
| 109 | else if(s>1720) sel=1; |
| 110 | else if(s>1530) sel=2; |
| 111 | else if(s>1370) sel=3; |
| 112 | else sel=4; // >1090 |
| 113 | } |
| 114 | else { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 115 | if(s>1790) sel=0; // <1958 |
| 116 | else if(s>1617) sel=1; |
| 117 | else if(s>1449) sel=2; |
| 118 | else if(s>1291) sel=3; |
| 119 | else sel=4; // >1090 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | *ret_sel=sel; |
| 122 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 123 | lo1freq=(lo1a+8*lo1n)*fref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | tuner_dbg("mt2032: rfin=%d lo1=%d lo1n=%d lo1a=%d sel=%d, lo1freq=%d\n", |
| 126 | rfin,lo1,lo1n,lo1a,sel,lo1freq); |
| 127 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 128 | desired_lo2=lo1freq-rfin-if2; |
| 129 | lo2=(desired_lo2)/fref; |
| 130 | lo2n=lo2/8; |
| 131 | lo2a=lo2-(lo2n*8); |
| 132 | lo2num=((desired_lo2/1000)%(fref/1000))* 3780/(fref/1000); //scale to fit in 32bit arith |
| 133 | lo2freq=(lo2a+8*lo2n)*fref + lo2num*(fref/1000)/3780*1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | tuner_dbg("mt2032: rfin=%d lo2=%d lo2n=%d lo2a=%d num=%d lo2freq=%d\n", |
| 136 | rfin,lo2,lo2n,lo2a,lo2num,lo2freq); |
| 137 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 138 | if(lo1a<0 || lo1a>7 || lo1n<17 ||lo1n>48 || lo2a<0 ||lo2a >7 ||lo2n<17 || lo2n>30) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | tuner_info("mt2032: frequency parameters out of range: %d %d %d %d\n", |
| 140 | lo1a, lo1n, lo2a,lo2n); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 141 | return(-1); |
| 142 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
| 144 | mt2032_spurcheck(c, lo1freq, desired_lo2, spectrum_from, spectrum_to); |
| 145 | // should recalculate lo1 (one step up/down) |
| 146 | |
| 147 | // set up MT2032 register map for transfer over i2c |
| 148 | buf[0]=lo1n-1; |
| 149 | buf[1]=lo1a | (sel<<4); |
| 150 | buf[2]=0x86; // LOGC |
| 151 | buf[3]=0x0f; //reserved |
| 152 | buf[4]=0x1f; |
| 153 | buf[5]=(lo2n-1) | (lo2a<<5); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 154 | if(rfin >400*1000*1000) |
| 155 | buf[6]=0xe4; |
| 156 | else |
| 157 | buf[6]=0xf4; // set PKEN per rev 1.2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | buf[7]=8+xogc; |
| 159 | buf[8]=0xc3; //reserved |
| 160 | buf[9]=0x4e; //reserved |
| 161 | buf[10]=0xec; //reserved |
| 162 | buf[11]=(lo2num&0xff); |
| 163 | buf[12]=(lo2num>>8) |0x80; // Lo2RST |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static int mt2032_check_lo_lock(struct i2c_client *c) |
| 169 | { |
| 170 | struct tuner *t = i2c_get_clientdata(c); |
| 171 | int try,lock=0; |
| 172 | unsigned char buf[2]; |
| 173 | |
| 174 | for(try=0;try<10;try++) { |
| 175 | buf[0]=0x0e; |
| 176 | i2c_master_send(c,buf,1); |
| 177 | i2c_master_recv(c,buf,1); |
| 178 | tuner_dbg("mt2032 Reg.E=0x%02x\n",buf[0]); |
| 179 | lock=buf[0] &0x06; |
| 180 | |
| 181 | if (lock==6) |
| 182 | break; |
| 183 | |
| 184 | tuner_dbg("mt2032: pll wait 1ms for lock (0x%2x)\n",buf[0]); |
| 185 | udelay(1000); |
| 186 | } |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 187 | return lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static int mt2032_optimize_vco(struct i2c_client *c,int sel,int lock) |
| 191 | { |
| 192 | struct tuner *t = i2c_get_clientdata(c); |
| 193 | unsigned char buf[2]; |
| 194 | int tad1; |
| 195 | |
| 196 | buf[0]=0x0f; |
| 197 | i2c_master_send(c,buf,1); |
| 198 | i2c_master_recv(c,buf,1); |
| 199 | tuner_dbg("mt2032 Reg.F=0x%02x\n",buf[0]); |
| 200 | tad1=buf[0]&0x07; |
| 201 | |
| 202 | if(tad1 ==0) return lock; |
| 203 | if(tad1 ==1) return lock; |
| 204 | |
| 205 | if(tad1==2) { |
| 206 | if(sel==0) |
| 207 | return lock; |
| 208 | else sel--; |
| 209 | } |
| 210 | else { |
| 211 | if(sel<4) |
| 212 | sel++; |
| 213 | else |
| 214 | return lock; |
| 215 | } |
| 216 | |
| 217 | tuner_dbg("mt2032 optimize_vco: sel=%d\n",sel); |
| 218 | |
| 219 | buf[0]=0x0f; |
| 220 | buf[1]=sel; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 221 | i2c_master_send(c,buf,2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | lock=mt2032_check_lo_lock(c); |
| 223 | return lock; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | static void mt2032_set_if_freq(struct i2c_client *c, unsigned int rfin, |
| 228 | unsigned int if1, unsigned int if2, |
| 229 | unsigned int from, unsigned int to) |
| 230 | { |
| 231 | unsigned char buf[21]; |
| 232 | int lint_try,ret,sel,lock=0; |
| 233 | struct tuner *t = i2c_get_clientdata(c); |
Michael Krufky | b208319 | 2007-05-29 22:54:06 -0300 | [diff] [blame] | 234 | struct microtune_priv *priv = t->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
| 236 | tuner_dbg("mt2032_set_if_freq rfin=%d if1=%d if2=%d from=%d to=%d\n", |
| 237 | rfin,if1,if2,from,to); |
| 238 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 239 | buf[0]=0; |
| 240 | ret=i2c_master_send(c,buf,1); |
| 241 | i2c_master_recv(c,buf,21); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | buf[0]=0; |
Michael Krufky | b208319 | 2007-05-29 22:54:06 -0300 | [diff] [blame] | 244 | ret=mt2032_compute_freq(c,rfin,if1,if2,from,to,&buf[1],&sel,priv->xogc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | if (ret<0) |
| 246 | return; |
| 247 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 248 | // send only the relevant registers per Rev. 1.2 |
| 249 | buf[0]=0; |
| 250 | ret=i2c_master_send(c,buf,4); |
| 251 | buf[5]=5; |
| 252 | ret=i2c_master_send(c,buf+5,4); |
| 253 | buf[11]=11; |
| 254 | ret=i2c_master_send(c,buf+11,3); |
| 255 | if(ret!=3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | tuner_warn("i2c i/o error: rc == %d (should be 3)\n",ret); |
| 257 | |
| 258 | // wait for PLLs to lock (per manual), retry LINT if not. |
| 259 | for(lint_try=0; lint_try<2; lint_try++) { |
| 260 | lock=mt2032_check_lo_lock(c); |
| 261 | |
| 262 | if(optimize_vco) |
| 263 | lock=mt2032_optimize_vco(c,sel,lock); |
| 264 | if(lock==6) break; |
| 265 | |
| 266 | tuner_dbg("mt2032: re-init PLLs by LINT\n"); |
| 267 | buf[0]=7; |
Michael Krufky | b208319 | 2007-05-29 22:54:06 -0300 | [diff] [blame] | 268 | buf[1]=0x80 +8+priv->xogc; // set LINT to re-init PLLs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | i2c_master_send(c,buf,2); |
| 270 | mdelay(10); |
Michael Krufky | b208319 | 2007-05-29 22:54:06 -0300 | [diff] [blame] | 271 | buf[1]=8+priv->xogc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | i2c_master_send(c,buf,2); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | if (lock!=6) |
| 276 | tuner_warn("MT2032 Fatal Error: PLLs didn't lock.\n"); |
| 277 | |
| 278 | buf[0]=2; |
| 279 | buf[1]=0x20; // LOGC for optimal phase noise |
| 280 | ret=i2c_master_send(c,buf,2); |
| 281 | if (ret!=2) |
| 282 | tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | static void mt2032_set_tv_freq(struct i2c_client *c, unsigned int freq) |
| 287 | { |
| 288 | struct tuner *t = i2c_get_clientdata(c); |
| 289 | int if2,from,to; |
| 290 | |
| 291 | // signal bandwidth and picture carrier |
| 292 | if (t->std & V4L2_STD_525_60) { |
| 293 | // NTSC |
| 294 | from = 40750*1000; |
| 295 | to = 46750*1000; |
| 296 | if2 = 45750*1000; |
| 297 | } else { |
| 298 | // PAL |
| 299 | from = 32900*1000; |
| 300 | to = 39900*1000; |
| 301 | if2 = 38900*1000; |
| 302 | } |
| 303 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 304 | mt2032_set_if_freq(c, freq*62500 /* freq*1000*1000/16 */, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | 1090*1000*1000, if2, from, to); |
| 306 | } |
| 307 | |
| 308 | static void mt2032_set_radio_freq(struct i2c_client *c, unsigned int freq) |
| 309 | { |
| 310 | struct tuner *t = i2c_get_clientdata(c); |
Michael Krufky | b208319 | 2007-05-29 22:54:06 -0300 | [diff] [blame] | 311 | struct microtune_priv *priv = t->priv; |
| 312 | int if2 = priv->radio_if2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | // per Manual for FM tuning: first if center freq. 1085 MHz |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 315 | mt2032_set_if_freq(c, freq * 1000 / 16, |
Mauro Carvalho Chehab | 586b0ca | 2005-06-28 20:45:21 -0700 | [diff] [blame] | 316 | 1085*1000*1000,if2,if2,if2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Michael Krufky | c22bcb0 | 2007-06-06 16:14:18 -0300 | [diff] [blame^] | 319 | static struct tuner_operations mt2032_tuner_ops = { |
| 320 | .set_tv_freq = mt2032_set_tv_freq, |
| 321 | .set_radio_freq = mt2032_set_radio_freq, |
| 322 | .release = microtune_release, |
| 323 | }; |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | // Initalization as described in "MT203x Programming Procedures", Rev 1.2, Feb.2001 |
| 326 | static int mt2032_init(struct i2c_client *c) |
| 327 | { |
| 328 | struct tuner *t = i2c_get_clientdata(c); |
Michael Krufky | b208319 | 2007-05-29 22:54:06 -0300 | [diff] [blame] | 329 | struct microtune_priv *priv = t->priv; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 330 | unsigned char buf[21]; |
| 331 | int ret,xogc,xok=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | // Initialize Registers per spec. |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 334 | buf[1]=2; // Index to register 2 |
| 335 | buf[2]=0xff; |
| 336 | buf[3]=0x0f; |
| 337 | buf[4]=0x1f; |
| 338 | ret=i2c_master_send(c,buf+1,4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 340 | buf[5]=6; // Index register 6 |
| 341 | buf[6]=0xe4; |
| 342 | buf[7]=0x8f; |
| 343 | buf[8]=0xc3; |
| 344 | buf[9]=0x4e; |
| 345 | buf[10]=0xec; |
| 346 | ret=i2c_master_send(c,buf+5,6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 348 | buf[12]=13; // Index register 13 |
| 349 | buf[13]=0x32; |
| 350 | ret=i2c_master_send(c,buf+12,2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 352 | // Adjust XOGC (register 7), wait for XOK |
| 353 | xogc=7; |
| 354 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | tuner_dbg("mt2032: xogc = 0x%02x\n",xogc&0x07); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 356 | mdelay(10); |
| 357 | buf[0]=0x0e; |
| 358 | i2c_master_send(c,buf,1); |
| 359 | i2c_master_recv(c,buf,1); |
| 360 | xok=buf[0]&0x01; |
| 361 | tuner_dbg("mt2032: xok = 0x%02x\n",xok); |
| 362 | if (xok == 1) break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 364 | xogc--; |
| 365 | tuner_dbg("mt2032: xogc = 0x%02x\n",xogc&0x07); |
| 366 | if (xogc == 3) { |
| 367 | xogc=4; // min. 4 per spec |
| 368 | break; |
| 369 | } |
| 370 | buf[0]=0x07; |
| 371 | buf[1]=0x88 + xogc; |
| 372 | ret=i2c_master_send(c,buf,2); |
| 373 | if (ret!=2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 375 | } while (xok != 1 ); |
Michael Krufky | b208319 | 2007-05-29 22:54:06 -0300 | [diff] [blame] | 376 | priv->xogc=xogc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Michael Krufky | c22bcb0 | 2007-06-06 16:14:18 -0300 | [diff] [blame^] | 378 | memcpy(&t->ops, &mt2032_tuner_ops, sizeof(struct tuner_operations)); |
| 379 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 380 | return(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | static void mt2050_set_antenna(struct i2c_client *c, unsigned char antenna) |
| 384 | { |
| 385 | struct tuner *t = i2c_get_clientdata(c); |
Michael Krufky | 56584c9 | 2007-05-29 15:36:37 -0300 | [diff] [blame] | 386 | unsigned char buf[2]; |
| 387 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
Michael Krufky | 56584c9 | 2007-05-29 15:36:37 -0300 | [diff] [blame] | 389 | buf[0] = 6; |
| 390 | buf[1] = antenna ? 0x11 : 0x10; |
| 391 | ret=i2c_master_send(c,buf,2); |
| 392 | tuner_dbg("mt2050: enabled antenna connector %d\n", antenna); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static void mt2050_set_if_freq(struct i2c_client *c,unsigned int freq, unsigned int if2) |
| 396 | { |
| 397 | struct tuner *t = i2c_get_clientdata(c); |
| 398 | unsigned int if1=1218*1000*1000; |
| 399 | unsigned int f_lo1,f_lo2,lo1,lo2,f_lo1_modulo,f_lo2_modulo,num1,num2,div1a,div1b,div2a,div2b; |
| 400 | int ret; |
| 401 | unsigned char buf[6]; |
| 402 | |
| 403 | tuner_dbg("mt2050_set_if_freq freq=%d if1=%d if2=%d\n", |
| 404 | freq,if1,if2); |
| 405 | |
| 406 | f_lo1=freq+if1; |
| 407 | f_lo1=(f_lo1/1000000)*1000000; |
| 408 | |
| 409 | f_lo2=f_lo1-freq-if2; |
| 410 | f_lo2=(f_lo2/50000)*50000; |
| 411 | |
| 412 | lo1=f_lo1/4000000; |
| 413 | lo2=f_lo2/4000000; |
| 414 | |
| 415 | f_lo1_modulo= f_lo1-(lo1*4000000); |
| 416 | f_lo2_modulo= f_lo2-(lo2*4000000); |
| 417 | |
| 418 | num1=4*f_lo1_modulo/4000000; |
| 419 | num2=4096*(f_lo2_modulo/1000)/4000; |
| 420 | |
| 421 | // todo spurchecks |
| 422 | |
| 423 | div1a=(lo1/12)-1; |
| 424 | div1b=lo1-(div1a+1)*12; |
| 425 | |
| 426 | div2a=(lo2/8)-1; |
| 427 | div2b=lo2-(div2a+1)*8; |
| 428 | |
Hans Verkuil | f9195de | 2006-01-11 19:01:01 -0200 | [diff] [blame] | 429 | if (tuner_debug > 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | tuner_dbg("lo1 lo2 = %d %d\n", lo1, lo2); |
| 431 | tuner_dbg("num1 num2 div1a div1b div2a div2b= %x %x %x %x %x %x\n", |
| 432 | num1,num2,div1a,div1b,div2a,div2b); |
| 433 | } |
| 434 | |
| 435 | buf[0]=1; |
| 436 | buf[1]= 4*div1b + num1; |
| 437 | if(freq<275*1000*1000) buf[1] = buf[1]|0x80; |
| 438 | |
| 439 | buf[2]=div1a; |
| 440 | buf[3]=32*div2b + num2/256; |
| 441 | buf[4]=num2-(num2/256)*256; |
| 442 | buf[5]=div2a; |
| 443 | if(num2!=0) buf[5]=buf[5]|0x40; |
| 444 | |
Hans Verkuil | f9195de | 2006-01-11 19:01:01 -0200 | [diff] [blame] | 445 | if (tuner_debug > 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | int i; |
| 447 | tuner_dbg("bufs is: "); |
| 448 | for(i=0;i<6;i++) |
| 449 | printk("%x ",buf[i]); |
| 450 | printk("\n"); |
| 451 | } |
| 452 | |
| 453 | ret=i2c_master_send(c,buf,6); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 454 | if (ret!=6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | tuner_warn("i2c i/o error: rc == %d (should be 6)\n",ret); |
| 456 | } |
| 457 | |
| 458 | static void mt2050_set_tv_freq(struct i2c_client *c, unsigned int freq) |
| 459 | { |
| 460 | struct tuner *t = i2c_get_clientdata(c); |
| 461 | unsigned int if2; |
| 462 | |
| 463 | if (t->std & V4L2_STD_525_60) { |
| 464 | // NTSC |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 465 | if2 = 45750*1000; |
| 466 | } else { |
| 467 | // PAL |
| 468 | if2 = 38900*1000; |
| 469 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | if (V4L2_TUNER_DIGITAL_TV == t->mode) { |
| 471 | // DVB (pinnacle 300i) |
| 472 | if2 = 36150*1000; |
| 473 | } |
| 474 | mt2050_set_if_freq(c, freq*62500, if2); |
| 475 | mt2050_set_antenna(c, tv_antenna); |
| 476 | } |
| 477 | |
| 478 | static void mt2050_set_radio_freq(struct i2c_client *c, unsigned int freq) |
| 479 | { |
| 480 | struct tuner *t = i2c_get_clientdata(c); |
Michael Krufky | b208319 | 2007-05-29 22:54:06 -0300 | [diff] [blame] | 481 | struct microtune_priv *priv = t->priv; |
| 482 | int if2 = priv->radio_if2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
Nickolay V. Shmyrev | e9d096d | 2005-11-08 21:38:17 -0800 | [diff] [blame] | 484 | mt2050_set_if_freq(c, freq * 1000 / 16, if2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | mt2050_set_antenna(c, radio_antenna); |
| 486 | } |
| 487 | |
Michael Krufky | c22bcb0 | 2007-06-06 16:14:18 -0300 | [diff] [blame^] | 488 | static struct tuner_operations mt2050_tuner_ops = { |
| 489 | .set_tv_freq = mt2050_set_tv_freq, |
| 490 | .set_radio_freq = mt2050_set_radio_freq, |
| 491 | .release = microtune_release, |
| 492 | }; |
| 493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | static int mt2050_init(struct i2c_client *c) |
| 495 | { |
| 496 | struct tuner *t = i2c_get_clientdata(c); |
| 497 | unsigned char buf[2]; |
| 498 | int ret; |
| 499 | |
| 500 | buf[0]=6; |
| 501 | buf[1]=0x10; |
| 502 | ret=i2c_master_send(c,buf,2); // power |
| 503 | |
| 504 | buf[0]=0x0f; |
| 505 | buf[1]=0x0f; |
| 506 | ret=i2c_master_send(c,buf,2); // m1lo |
| 507 | |
| 508 | buf[0]=0x0d; |
| 509 | ret=i2c_master_send(c,buf,1); |
| 510 | i2c_master_recv(c,buf,1); |
| 511 | |
| 512 | tuner_dbg("mt2050: sro is %x\n",buf[0]); |
Michael Krufky | c22bcb0 | 2007-06-06 16:14:18 -0300 | [diff] [blame^] | 513 | |
| 514 | memcpy(&t->ops, &mt2050_tuner_ops, sizeof(struct tuner_operations)); |
| 515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | int microtune_init(struct i2c_client *c) |
| 520 | { |
Michael Krufky | b208319 | 2007-05-29 22:54:06 -0300 | [diff] [blame] | 521 | struct microtune_priv *priv = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | struct tuner *t = i2c_get_clientdata(c); |
| 523 | char *name; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 524 | unsigned char buf[21]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | int company_code; |
| 526 | |
Michael Krufky | b208319 | 2007-05-29 22:54:06 -0300 | [diff] [blame] | 527 | priv = kzalloc(sizeof(struct microtune_priv), GFP_KERNEL); |
| 528 | if (priv == NULL) |
| 529 | return -ENOMEM; |
| 530 | t->priv = priv; |
| 531 | |
| 532 | priv->radio_if2 = 10700 * 1000; /* 10.7MHz - FM radio */ |
| 533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | memset(buf,0,sizeof(buf)); |
Michael Krufky | c22bcb0 | 2007-06-06 16:14:18 -0300 | [diff] [blame^] | 535 | |
Hans Verkuil | 39e8f40 | 2006-01-09 15:25:16 -0200 | [diff] [blame] | 536 | if (t->std & V4L2_STD_525_60) { |
| 537 | tuner_dbg("pinnacle ntsc\n"); |
Michael Krufky | b208319 | 2007-05-29 22:54:06 -0300 | [diff] [blame] | 538 | priv->radio_if2 = 41300 * 1000; |
Hans Verkuil | 39e8f40 | 2006-01-09 15:25:16 -0200 | [diff] [blame] | 539 | } else { |
| 540 | tuner_dbg("pinnacle pal\n"); |
Michael Krufky | b208319 | 2007-05-29 22:54:06 -0300 | [diff] [blame] | 541 | priv->radio_if2 = 33300 * 1000; |
Hans Verkuil | 39e8f40 | 2006-01-09 15:25:16 -0200 | [diff] [blame] | 542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | name = "unknown"; |
| 544 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 545 | i2c_master_send(c,buf,1); |
| 546 | i2c_master_recv(c,buf,21); |
Hans Verkuil | f9195de | 2006-01-11 19:01:01 -0200 | [diff] [blame] | 547 | if (tuner_debug) { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 548 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | tuner_dbg("MT20xx hexdump:"); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 550 | for(i=0;i<21;i++) { |
| 551 | printk(" %02x",buf[i]); |
| 552 | if(((i+1)%8)==0) printk(" "); |
| 553 | } |
| 554 | printk("\n"); |
| 555 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | company_code = buf[0x11] << 8 | buf[0x12]; |
| 557 | tuner_info("microtune: companycode=%04x part=%02x rev=%02x\n", |
| 558 | company_code,buf[0x13],buf[0x14]); |
| 559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
| 561 | if (buf[0x13] < ARRAY_SIZE(microtune_part) && |
| 562 | NULL != microtune_part[buf[0x13]]) |
| 563 | name = microtune_part[buf[0x13]]; |
| 564 | switch (buf[0x13]) { |
| 565 | case MT2032: |
| 566 | mt2032_init(c); |
| 567 | break; |
| 568 | case MT2050: |
| 569 | mt2050_init(c); |
| 570 | break; |
| 571 | default: |
| 572 | tuner_info("microtune %s found, not (yet?) supported, sorry :-/\n", |
| 573 | name); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 574 | return 0; |
| 575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
| 577 | strlcpy(c->name, name, sizeof(c->name)); |
| 578 | tuner_info("microtune %s found, OK\n",name); |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 584 | * --------------------------------------------------------------------------- |
| 585 | * Local variables: |
| 586 | * c-basic-offset: 8 |
| 587 | * End: |
| 588 | */ |