mt20xx.c 13 KB

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