mt20xx.c 13 KB

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