mt20xx.c 13 KB

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