mt20xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  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. static unsigned int optimize_vco = 1;
  13. module_param(optimize_vco, int, 0644);
  14. static unsigned int tv_antenna = 1;
  15. module_param(tv_antenna, int, 0644);
  16. static unsigned int radio_antenna = 0;
  17. module_param(radio_antenna, int, 0644);
  18. /* ---------------------------------------------------------------------- */
  19. #define MT2032 0x04
  20. #define MT2030 0x06
  21. #define MT2040 0x07
  22. #define MT2050 0x42
  23. static char *microtune_part[] = {
  24. [ MT2030 ] = "MT2030",
  25. [ MT2032 ] = "MT2032",
  26. [ MT2040 ] = "MT2040",
  27. [ MT2050 ] = "MT2050",
  28. };
  29. // IsSpurInBand()?
  30. static int mt2032_spurcheck(struct i2c_client *c,
  31. int f1, int f2, int spectrum_from,int spectrum_to)
  32. {
  33. struct tuner *t = i2c_get_clientdata(c);
  34. int n1=1,n2,f;
  35. f1=f1/1000; //scale to kHz to avoid 32bit overflows
  36. f2=f2/1000;
  37. spectrum_from/=1000;
  38. spectrum_to/=1000;
  39. tuner_dbg("spurcheck f1=%d f2=%d from=%d to=%d\n",
  40. f1,f2,spectrum_from,spectrum_to);
  41. do {
  42. n2=-n1;
  43. f=n1*(f1-f2);
  44. do {
  45. n2--;
  46. f=f-f2;
  47. tuner_dbg("spurtest n1=%d n2=%d ftest=%d\n",n1,n2,f);
  48. if( (f>spectrum_from) && (f<spectrum_to))
  49. tuner_dbg("mt2032 spurcheck triggered: %d\n",n1);
  50. } while ( (f>(f2-spectrum_to)) || (n2>-5));
  51. n1++;
  52. } while (n1<5);
  53. return 1;
  54. }
  55. static int mt2032_compute_freq(struct i2c_client *c,
  56. unsigned int rfin,
  57. unsigned int if1, unsigned int if2,
  58. unsigned int spectrum_from,
  59. unsigned int spectrum_to,
  60. unsigned char *buf,
  61. int *ret_sel,
  62. unsigned int xogc) //all in Hz
  63. {
  64. struct tuner *t = i2c_get_clientdata(c);
  65. unsigned int fref,lo1,lo1n,lo1a,s,sel,lo1freq, desired_lo1,
  66. desired_lo2,lo2,lo2n,lo2a,lo2num,lo2freq;
  67. fref= 5250 *1000; //5.25MHz
  68. desired_lo1=rfin+if1;
  69. lo1=(2*(desired_lo1/1000)+(fref/1000)) / (2*fref/1000);
  70. lo1n=lo1/8;
  71. lo1a=lo1-(lo1n*8);
  72. s=rfin/1000/1000+1090;
  73. if(optimize_vco) {
  74. if(s>1890) sel=0;
  75. else if(s>1720) sel=1;
  76. else if(s>1530) sel=2;
  77. else if(s>1370) sel=3;
  78. else sel=4; // >1090
  79. }
  80. else {
  81. if(s>1790) sel=0; // <1958
  82. else if(s>1617) sel=1;
  83. else if(s>1449) sel=2;
  84. else if(s>1291) sel=3;
  85. else sel=4; // >1090
  86. }
  87. *ret_sel=sel;
  88. lo1freq=(lo1a+8*lo1n)*fref;
  89. tuner_dbg("mt2032: rfin=%d lo1=%d lo1n=%d lo1a=%d sel=%d, lo1freq=%d\n",
  90. rfin,lo1,lo1n,lo1a,sel,lo1freq);
  91. desired_lo2=lo1freq-rfin-if2;
  92. lo2=(desired_lo2)/fref;
  93. lo2n=lo2/8;
  94. lo2a=lo2-(lo2n*8);
  95. lo2num=((desired_lo2/1000)%(fref/1000))* 3780/(fref/1000); //scale to fit in 32bit arith
  96. lo2freq=(lo2a+8*lo2n)*fref + lo2num*(fref/1000)/3780*1000;
  97. tuner_dbg("mt2032: rfin=%d lo2=%d lo2n=%d lo2a=%d num=%d lo2freq=%d\n",
  98. rfin,lo2,lo2n,lo2a,lo2num,lo2freq);
  99. if(lo1a<0 || lo1a>7 || lo1n<17 ||lo1n>48 || lo2a<0 ||lo2a >7 ||lo2n<17 || lo2n>30) {
  100. tuner_info("mt2032: frequency parameters out of range: %d %d %d %d\n",
  101. lo1a, lo1n, lo2a,lo2n);
  102. return(-1);
  103. }
  104. mt2032_spurcheck(c, lo1freq, desired_lo2, spectrum_from, spectrum_to);
  105. // should recalculate lo1 (one step up/down)
  106. // set up MT2032 register map for transfer over i2c
  107. buf[0]=lo1n-1;
  108. buf[1]=lo1a | (sel<<4);
  109. buf[2]=0x86; // LOGC
  110. buf[3]=0x0f; //reserved
  111. buf[4]=0x1f;
  112. buf[5]=(lo2n-1) | (lo2a<<5);
  113. if(rfin >400*1000*1000)
  114. buf[6]=0xe4;
  115. else
  116. buf[6]=0xf4; // set PKEN per rev 1.2
  117. buf[7]=8+xogc;
  118. buf[8]=0xc3; //reserved
  119. buf[9]=0x4e; //reserved
  120. buf[10]=0xec; //reserved
  121. buf[11]=(lo2num&0xff);
  122. buf[12]=(lo2num>>8) |0x80; // Lo2RST
  123. return 0;
  124. }
  125. static int mt2032_check_lo_lock(struct i2c_client *c)
  126. {
  127. struct tuner *t = i2c_get_clientdata(c);
  128. int try,lock=0;
  129. unsigned char buf[2];
  130. for(try=0;try<10;try++) {
  131. buf[0]=0x0e;
  132. i2c_master_send(c,buf,1);
  133. i2c_master_recv(c,buf,1);
  134. tuner_dbg("mt2032 Reg.E=0x%02x\n",buf[0]);
  135. lock=buf[0] &0x06;
  136. if (lock==6)
  137. break;
  138. tuner_dbg("mt2032: pll wait 1ms for lock (0x%2x)\n",buf[0]);
  139. udelay(1000);
  140. }
  141. return lock;
  142. }
  143. static int mt2032_optimize_vco(struct i2c_client *c,int sel,int lock)
  144. {
  145. struct tuner *t = i2c_get_clientdata(c);
  146. unsigned char buf[2];
  147. int tad1;
  148. buf[0]=0x0f;
  149. i2c_master_send(c,buf,1);
  150. i2c_master_recv(c,buf,1);
  151. tuner_dbg("mt2032 Reg.F=0x%02x\n",buf[0]);
  152. tad1=buf[0]&0x07;
  153. if(tad1 ==0) return lock;
  154. if(tad1 ==1) return lock;
  155. if(tad1==2) {
  156. if(sel==0)
  157. return lock;
  158. else sel--;
  159. }
  160. else {
  161. if(sel<4)
  162. sel++;
  163. else
  164. return lock;
  165. }
  166. tuner_dbg("mt2032 optimize_vco: sel=%d\n",sel);
  167. buf[0]=0x0f;
  168. buf[1]=sel;
  169. i2c_master_send(c,buf,2);
  170. lock=mt2032_check_lo_lock(c);
  171. return lock;
  172. }
  173. static void mt2032_set_if_freq(struct i2c_client *c, unsigned int rfin,
  174. unsigned int if1, unsigned int if2,
  175. unsigned int from, unsigned int to)
  176. {
  177. unsigned char buf[21];
  178. int lint_try,ret,sel,lock=0;
  179. struct tuner *t = i2c_get_clientdata(c);
  180. tuner_dbg("mt2032_set_if_freq rfin=%d if1=%d if2=%d from=%d to=%d\n",
  181. rfin,if1,if2,from,to);
  182. buf[0]=0;
  183. ret=i2c_master_send(c,buf,1);
  184. i2c_master_recv(c,buf,21);
  185. buf[0]=0;
  186. ret=mt2032_compute_freq(c,rfin,if1,if2,from,to,&buf[1],&sel,t->xogc);
  187. if (ret<0)
  188. return;
  189. // send only the relevant registers per Rev. 1.2
  190. buf[0]=0;
  191. ret=i2c_master_send(c,buf,4);
  192. buf[5]=5;
  193. ret=i2c_master_send(c,buf+5,4);
  194. buf[11]=11;
  195. ret=i2c_master_send(c,buf+11,3);
  196. if(ret!=3)
  197. tuner_warn("i2c i/o error: rc == %d (should be 3)\n",ret);
  198. // wait for PLLs to lock (per manual), retry LINT if not.
  199. for(lint_try=0; lint_try<2; lint_try++) {
  200. lock=mt2032_check_lo_lock(c);
  201. if(optimize_vco)
  202. lock=mt2032_optimize_vco(c,sel,lock);
  203. if(lock==6) break;
  204. tuner_dbg("mt2032: re-init PLLs by LINT\n");
  205. buf[0]=7;
  206. buf[1]=0x80 +8+t->xogc; // set LINT to re-init PLLs
  207. i2c_master_send(c,buf,2);
  208. mdelay(10);
  209. buf[1]=8+t->xogc;
  210. i2c_master_send(c,buf,2);
  211. }
  212. if (lock!=6)
  213. tuner_warn("MT2032 Fatal Error: PLLs didn't lock.\n");
  214. buf[0]=2;
  215. buf[1]=0x20; // LOGC for optimal phase noise
  216. ret=i2c_master_send(c,buf,2);
  217. if (ret!=2)
  218. tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret);
  219. }
  220. static void mt2032_set_tv_freq(struct i2c_client *c, unsigned int freq)
  221. {
  222. struct tuner *t = i2c_get_clientdata(c);
  223. int if2,from,to;
  224. // signal bandwidth and picture carrier
  225. if (t->std & V4L2_STD_525_60) {
  226. // NTSC
  227. from = 40750*1000;
  228. to = 46750*1000;
  229. if2 = 45750*1000;
  230. } else {
  231. // PAL
  232. from = 32900*1000;
  233. to = 39900*1000;
  234. if2 = 38900*1000;
  235. }
  236. mt2032_set_if_freq(c, freq*62500 /* freq*1000*1000/16 */,
  237. 1090*1000*1000, if2, from, to);
  238. }
  239. static void mt2032_set_radio_freq(struct i2c_client *c, unsigned int freq)
  240. {
  241. struct tuner *t = i2c_get_clientdata(c);
  242. int if2 = t->radio_if2;
  243. // per Manual for FM tuning: first if center freq. 1085 MHz
  244. mt2032_set_if_freq(c, freq * 1000 / 16,
  245. 1085*1000*1000,if2,if2,if2);
  246. }
  247. // Initalization as described in "MT203x Programming Procedures", Rev 1.2, Feb.2001
  248. static int mt2032_init(struct i2c_client *c)
  249. {
  250. struct tuner *t = i2c_get_clientdata(c);
  251. unsigned char buf[21];
  252. int ret,xogc,xok=0;
  253. // Initialize Registers per spec.
  254. buf[1]=2; // Index to register 2
  255. buf[2]=0xff;
  256. buf[3]=0x0f;
  257. buf[4]=0x1f;
  258. ret=i2c_master_send(c,buf+1,4);
  259. buf[5]=6; // Index register 6
  260. buf[6]=0xe4;
  261. buf[7]=0x8f;
  262. buf[8]=0xc3;
  263. buf[9]=0x4e;
  264. buf[10]=0xec;
  265. ret=i2c_master_send(c,buf+5,6);
  266. buf[12]=13; // Index register 13
  267. buf[13]=0x32;
  268. ret=i2c_master_send(c,buf+12,2);
  269. // Adjust XOGC (register 7), wait for XOK
  270. xogc=7;
  271. do {
  272. tuner_dbg("mt2032: xogc = 0x%02x\n",xogc&0x07);
  273. mdelay(10);
  274. buf[0]=0x0e;
  275. i2c_master_send(c,buf,1);
  276. i2c_master_recv(c,buf,1);
  277. xok=buf[0]&0x01;
  278. tuner_dbg("mt2032: xok = 0x%02x\n",xok);
  279. if (xok == 1) break;
  280. xogc--;
  281. tuner_dbg("mt2032: xogc = 0x%02x\n",xogc&0x07);
  282. if (xogc == 3) {
  283. xogc=4; // min. 4 per spec
  284. break;
  285. }
  286. buf[0]=0x07;
  287. buf[1]=0x88 + xogc;
  288. ret=i2c_master_send(c,buf,2);
  289. if (ret!=2)
  290. tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret);
  291. } while (xok != 1 );
  292. t->xogc=xogc;
  293. t->tv_freq = mt2032_set_tv_freq;
  294. t->radio_freq = mt2032_set_radio_freq;
  295. return(1);
  296. }
  297. static void mt2050_set_antenna(struct i2c_client *c, unsigned char antenna)
  298. {
  299. struct tuner *t = i2c_get_clientdata(c);
  300. unsigned char buf[2];
  301. int ret;
  302. buf[0] = 6;
  303. buf[1] = antenna ? 0x11 : 0x10;
  304. ret=i2c_master_send(c,buf,2);
  305. tuner_dbg("mt2050: enabled antenna connector %d\n", antenna);
  306. }
  307. static void mt2050_set_if_freq(struct i2c_client *c,unsigned int freq, unsigned int if2)
  308. {
  309. struct tuner *t = i2c_get_clientdata(c);
  310. unsigned int if1=1218*1000*1000;
  311. unsigned int f_lo1,f_lo2,lo1,lo2,f_lo1_modulo,f_lo2_modulo,num1,num2,div1a,div1b,div2a,div2b;
  312. int ret;
  313. unsigned char buf[6];
  314. tuner_dbg("mt2050_set_if_freq freq=%d if1=%d if2=%d\n",
  315. freq,if1,if2);
  316. f_lo1=freq+if1;
  317. f_lo1=(f_lo1/1000000)*1000000;
  318. f_lo2=f_lo1-freq-if2;
  319. f_lo2=(f_lo2/50000)*50000;
  320. lo1=f_lo1/4000000;
  321. lo2=f_lo2/4000000;
  322. f_lo1_modulo= f_lo1-(lo1*4000000);
  323. f_lo2_modulo= f_lo2-(lo2*4000000);
  324. num1=4*f_lo1_modulo/4000000;
  325. num2=4096*(f_lo2_modulo/1000)/4000;
  326. // todo spurchecks
  327. div1a=(lo1/12)-1;
  328. div1b=lo1-(div1a+1)*12;
  329. div2a=(lo2/8)-1;
  330. div2b=lo2-(div2a+1)*8;
  331. if (tuner_debug > 1) {
  332. tuner_dbg("lo1 lo2 = %d %d\n", lo1, lo2);
  333. tuner_dbg("num1 num2 div1a div1b div2a div2b= %x %x %x %x %x %x\n",
  334. num1,num2,div1a,div1b,div2a,div2b);
  335. }
  336. buf[0]=1;
  337. buf[1]= 4*div1b + num1;
  338. if(freq<275*1000*1000) buf[1] = buf[1]|0x80;
  339. buf[2]=div1a;
  340. buf[3]=32*div2b + num2/256;
  341. buf[4]=num2-(num2/256)*256;
  342. buf[5]=div2a;
  343. if(num2!=0) buf[5]=buf[5]|0x40;
  344. if (tuner_debug > 1) {
  345. int i;
  346. tuner_dbg("bufs is: ");
  347. for(i=0;i<6;i++)
  348. printk("%x ",buf[i]);
  349. printk("\n");
  350. }
  351. ret=i2c_master_send(c,buf,6);
  352. if (ret!=6)
  353. tuner_warn("i2c i/o error: rc == %d (should be 6)\n",ret);
  354. }
  355. static void mt2050_set_tv_freq(struct i2c_client *c, unsigned int freq)
  356. {
  357. struct tuner *t = i2c_get_clientdata(c);
  358. unsigned int if2;
  359. if (t->std & V4L2_STD_525_60) {
  360. // NTSC
  361. if2 = 45750*1000;
  362. } else {
  363. // PAL
  364. if2 = 38900*1000;
  365. }
  366. if (V4L2_TUNER_DIGITAL_TV == t->mode) {
  367. // DVB (pinnacle 300i)
  368. if2 = 36150*1000;
  369. }
  370. mt2050_set_if_freq(c, freq*62500, if2);
  371. mt2050_set_antenna(c, tv_antenna);
  372. }
  373. static void mt2050_set_radio_freq(struct i2c_client *c, unsigned int freq)
  374. {
  375. struct tuner *t = i2c_get_clientdata(c);
  376. int if2 = t->radio_if2;
  377. mt2050_set_if_freq(c, freq*62500, if2);
  378. mt2050_set_antenna(c, radio_antenna);
  379. }
  380. static int mt2050_init(struct i2c_client *c)
  381. {
  382. struct tuner *t = i2c_get_clientdata(c);
  383. unsigned char buf[2];
  384. int ret;
  385. buf[0]=6;
  386. buf[1]=0x10;
  387. ret=i2c_master_send(c,buf,2); // power
  388. buf[0]=0x0f;
  389. buf[1]=0x0f;
  390. ret=i2c_master_send(c,buf,2); // m1lo
  391. buf[0]=0x0d;
  392. ret=i2c_master_send(c,buf,1);
  393. i2c_master_recv(c,buf,1);
  394. tuner_dbg("mt2050: sro is %x\n",buf[0]);
  395. t->tv_freq = mt2050_set_tv_freq;
  396. t->radio_freq = mt2050_set_radio_freq;
  397. return 0;
  398. }
  399. int microtune_init(struct i2c_client *c)
  400. {
  401. struct tuner *t = i2c_get_clientdata(c);
  402. char *name;
  403. unsigned char buf[21];
  404. int company_code;
  405. memset(buf,0,sizeof(buf));
  406. t->tv_freq = NULL;
  407. t->radio_freq = NULL;
  408. t->standby = NULL;
  409. name = "unknown";
  410. i2c_master_send(c,buf,1);
  411. i2c_master_recv(c,buf,21);
  412. if (tuner_debug) {
  413. int i;
  414. tuner_dbg("MT20xx hexdump:");
  415. for(i=0;i<21;i++) {
  416. printk(" %02x",buf[i]);
  417. if(((i+1)%8)==0) printk(" ");
  418. }
  419. printk("\n");
  420. }
  421. company_code = buf[0x11] << 8 | buf[0x12];
  422. tuner_info("microtune: companycode=%04x part=%02x rev=%02x\n",
  423. company_code,buf[0x13],buf[0x14]);
  424. if (buf[0x13] < ARRAY_SIZE(microtune_part) &&
  425. NULL != microtune_part[buf[0x13]])
  426. name = microtune_part[buf[0x13]];
  427. switch (buf[0x13]) {
  428. case MT2032:
  429. mt2032_init(c);
  430. break;
  431. case MT2050:
  432. mt2050_init(c);
  433. break;
  434. default:
  435. tuner_info("microtune %s found, not (yet?) supported, sorry :-/\n",
  436. name);
  437. return 0;
  438. }
  439. strlcpy(c->name, name, sizeof(c->name));
  440. tuner_info("microtune %s found, OK\n",name);
  441. return 0;
  442. }
  443. /*
  444. * Overrides for Emacs so that we follow Linus's tabbing style.
  445. * ---------------------------------------------------------------------------
  446. * Local variables:
  447. * c-basic-offset: 8
  448. * End:
  449. */