tuner-simple.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. *
  3. * i2c tv tuner chip device driver
  4. * controls all those simple 4-control-bytes style tuners.
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/i2c.h>
  8. #include <linux/videodev.h>
  9. #include <media/tuner.h>
  10. #include <media/v4l2-common.h>
  11. static int offset = 0;
  12. module_param(offset, int, 0666);
  13. MODULE_PARM_DESC(offset,"Allows to specify an offset for tuner");
  14. /* ---------------------------------------------------------------------- */
  15. /* tv standard selection for Temic 4046 FM5
  16. this value takes the low bits of control byte 2
  17. from datasheet Rev.01, Feb.00
  18. standard BG I L L2 D
  19. picture IF 38.9 38.9 38.9 33.95 38.9
  20. sound 1 33.4 32.9 32.4 40.45 32.4
  21. sound 2 33.16
  22. NICAM 33.05 32.348 33.05 33.05
  23. */
  24. #define TEMIC_SET_PAL_I 0x05
  25. #define TEMIC_SET_PAL_DK 0x09
  26. #define TEMIC_SET_PAL_L 0x0a // SECAM ?
  27. #define TEMIC_SET_PAL_L2 0x0b // change IF !
  28. #define TEMIC_SET_PAL_BG 0x0c
  29. /* tv tuner system standard selection for Philips FQ1216ME
  30. this value takes the low bits of control byte 2
  31. from datasheet "1999 Nov 16" (supersedes "1999 Mar 23")
  32. standard BG DK I L L`
  33. picture carrier 38.90 38.90 38.90 38.90 33.95
  34. colour 34.47 34.47 34.47 34.47 38.38
  35. sound 1 33.40 32.40 32.90 32.40 40.45
  36. sound 2 33.16 - - - -
  37. NICAM 33.05 33.05 32.35 33.05 39.80
  38. */
  39. #define PHILIPS_SET_PAL_I 0x01 /* Bit 2 always zero !*/
  40. #define PHILIPS_SET_PAL_BGDK 0x09
  41. #define PHILIPS_SET_PAL_L2 0x0a
  42. #define PHILIPS_SET_PAL_L 0x0b
  43. /* system switching for Philips FI1216MF MK2
  44. from datasheet "1996 Jul 09",
  45. standard BG L L'
  46. picture carrier 38.90 38.90 33.95
  47. colour 34.47 34.37 38.38
  48. sound 1 33.40 32.40 40.45
  49. sound 2 33.16 - -
  50. NICAM 33.05 33.05 39.80
  51. */
  52. #define PHILIPS_MF_SET_BG 0x01 /* Bit 2 must be zero, Bit 3 is system output */
  53. #define PHILIPS_MF_SET_PAL_L 0x03 // France
  54. #define PHILIPS_MF_SET_PAL_L2 0x02 // L'
  55. /* Control byte */
  56. #define TUNER_RATIO_MASK 0x06 /* Bit cb1:cb2 */
  57. #define TUNER_RATIO_SELECT_50 0x00
  58. #define TUNER_RATIO_SELECT_32 0x02
  59. #define TUNER_RATIO_SELECT_166 0x04
  60. #define TUNER_RATIO_SELECT_62 0x06
  61. #define TUNER_CHARGE_PUMP 0x40 /* Bit cb6 */
  62. /* Status byte */
  63. #define TUNER_POR 0x80
  64. #define TUNER_FL 0x40
  65. #define TUNER_MODE 0x38
  66. #define TUNER_AFC 0x07
  67. #define TUNER_SIGNAL 0x07
  68. #define TUNER_STEREO 0x10
  69. #define TUNER_PLL_LOCKED 0x40
  70. #define TUNER_STEREO_MK3 0x04
  71. /* ---------------------------------------------------------------------- */
  72. static int tuner_getstatus(struct i2c_client *c)
  73. {
  74. unsigned char byte;
  75. if (1 != i2c_master_recv(c,&byte,1))
  76. return 0;
  77. return byte;
  78. }
  79. static int tuner_signal(struct i2c_client *c)
  80. {
  81. return (tuner_getstatus(c) & TUNER_SIGNAL) << 13;
  82. }
  83. static int tuner_stereo(struct i2c_client *c)
  84. {
  85. int stereo, status;
  86. struct tuner *t = i2c_get_clientdata(c);
  87. status = tuner_getstatus (c);
  88. switch (t->type) {
  89. case TUNER_PHILIPS_FM1216ME_MK3:
  90. case TUNER_PHILIPS_FM1236_MK3:
  91. case TUNER_PHILIPS_FM1256_IH3:
  92. stereo = ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
  93. break;
  94. default:
  95. stereo = status & TUNER_STEREO;
  96. }
  97. return stereo;
  98. }
  99. /* ---------------------------------------------------------------------- */
  100. static void default_set_tv_freq(struct i2c_client *c, unsigned int freq)
  101. {
  102. struct tuner *t = i2c_get_clientdata(c);
  103. u8 config, cb, tuneraddr;
  104. u16 div;
  105. struct tunertype *tun;
  106. u8 buffer[4];
  107. int rc, IFPCoff, i, j;
  108. enum param_type desired_type;
  109. struct tuner_params *params;
  110. tun = &tuners[t->type];
  111. /* IFPCoff = Video Intermediate Frequency - Vif:
  112. 940 =16*58.75 NTSC/J (Japan)
  113. 732 =16*45.75 M/N STD
  114. 704 =16*44 ATSC (at DVB code)
  115. 632 =16*39.50 I U.K.
  116. 622.4=16*38.90 B/G D/K I, L STD
  117. 592 =16*37.00 D China
  118. 590 =16.36.875 B Australia
  119. 543.2=16*33.95 L' STD
  120. 171.2=16*10.70 FM Radio (at set_radio_freq)
  121. */
  122. if (t->std == V4L2_STD_NTSC_M_JP) {
  123. IFPCoff = 940;
  124. desired_type = TUNER_PARAM_TYPE_NTSC;
  125. } else if ((t->std & V4L2_STD_MN) &&
  126. !(t->std & ~V4L2_STD_MN)) {
  127. IFPCoff = 732;
  128. desired_type = TUNER_PARAM_TYPE_NTSC;
  129. } else if (t->std == V4L2_STD_SECAM_LC) {
  130. IFPCoff = 543;
  131. desired_type = TUNER_PARAM_TYPE_SECAM;
  132. } else {
  133. IFPCoff = 623;
  134. desired_type = TUNER_PARAM_TYPE_PAL;
  135. }
  136. for (j = 0; j < tun->count-1; j++) {
  137. if (desired_type != tun->params[j].type)
  138. continue;
  139. break;
  140. }
  141. /* use default tuner_params if desired_type not available */
  142. if (desired_type != tun->params[j].type) {
  143. tuner_dbg("IFPCoff = %d: tuner_params undefined for tuner %d\n",
  144. IFPCoff,t->type);
  145. j = 0;
  146. }
  147. params = &tun->params[j];
  148. for (i = 0; i < params->count; i++) {
  149. if (freq > params->ranges[i].limit)
  150. continue;
  151. break;
  152. }
  153. if (i == params->count) {
  154. tuner_dbg("TV frequency out of range (%d > %d)",
  155. freq, params->ranges[i - 1].limit);
  156. freq = params->ranges[--i].limit;
  157. }
  158. config = params->ranges[i].config;
  159. cb = params->ranges[i].cb;
  160. /* i == 0 -> VHF_LO
  161. * i == 1 -> VHF_HI
  162. * i == 2 -> UHF */
  163. tuner_dbg("tv: param %d, range %d\n",j,i);
  164. div=freq + IFPCoff + offset;
  165. tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, Offset=%d.%02d MHz, div=%0d\n",
  166. freq / 16, freq % 16 * 100 / 16,
  167. IFPCoff / 16, IFPCoff % 16 * 100 / 16,
  168. offset / 16, offset % 16 * 100 / 16,
  169. div);
  170. /* tv norm specific stuff for multi-norm tuners */
  171. switch (t->type) {
  172. case TUNER_PHILIPS_SECAM: // FI1216MF
  173. /* 0x01 -> ??? no change ??? */
  174. /* 0x02 -> PAL BDGHI / SECAM L */
  175. /* 0x04 -> ??? PAL others / SECAM others ??? */
  176. cb &= ~0x02;
  177. if (t->std & V4L2_STD_SECAM)
  178. cb |= 0x02;
  179. break;
  180. case TUNER_TEMIC_4046FM5:
  181. cb &= ~0x0f;
  182. if (t->std & V4L2_STD_PAL_BG) {
  183. cb |= TEMIC_SET_PAL_BG;
  184. } else if (t->std & V4L2_STD_PAL_I) {
  185. cb |= TEMIC_SET_PAL_I;
  186. } else if (t->std & V4L2_STD_PAL_DK) {
  187. cb |= TEMIC_SET_PAL_DK;
  188. } else if (t->std & V4L2_STD_SECAM_L) {
  189. cb |= TEMIC_SET_PAL_L;
  190. }
  191. break;
  192. case TUNER_PHILIPS_FQ1216ME:
  193. cb &= ~0x0f;
  194. if (t->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
  195. cb |= PHILIPS_SET_PAL_BGDK;
  196. } else if (t->std & V4L2_STD_PAL_I) {
  197. cb |= PHILIPS_SET_PAL_I;
  198. } else if (t->std & V4L2_STD_SECAM_L) {
  199. cb |= PHILIPS_SET_PAL_L;
  200. }
  201. break;
  202. case TUNER_PHILIPS_ATSC:
  203. /* 0x00 -> ATSC antenna input 1 */
  204. /* 0x01 -> ATSC antenna input 2 */
  205. /* 0x02 -> NTSC antenna input 1 */
  206. /* 0x03 -> NTSC antenna input 2 */
  207. cb &= ~0x03;
  208. if (!(t->std & V4L2_STD_ATSC))
  209. cb |= 2;
  210. /* FIXME: input */
  211. break;
  212. case TUNER_MICROTUNE_4042FI5:
  213. /* Set the charge pump for fast tuning */
  214. config |= TUNER_CHARGE_PUMP;
  215. break;
  216. case TUNER_PHILIPS_TUV1236D:
  217. /* 0x40 -> ATSC antenna input 1 */
  218. /* 0x48 -> ATSC antenna input 2 */
  219. /* 0x00 -> NTSC antenna input 1 */
  220. /* 0x08 -> NTSC antenna input 2 */
  221. buffer[0] = 0x14;
  222. buffer[1] = 0x00;
  223. buffer[2] = 0x17;
  224. buffer[3] = 0x00;
  225. cb &= ~0x40;
  226. if (t->std & V4L2_STD_ATSC) {
  227. cb |= 0x40;
  228. buffer[1] = 0x04;
  229. }
  230. /* set to the correct mode (analog or digital) */
  231. tuneraddr = c->addr;
  232. c->addr = 0x0a;
  233. if (2 != (rc = i2c_master_send(c,&buffer[0],2)))
  234. tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
  235. if (2 != (rc = i2c_master_send(c,&buffer[2],2)))
  236. tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
  237. c->addr = tuneraddr;
  238. /* FIXME: input */
  239. break;
  240. }
  241. if (params->cb_first_if_lower_freq && div < t->last_div) {
  242. buffer[0] = config;
  243. buffer[1] = cb;
  244. buffer[2] = (div>>8) & 0x7f;
  245. buffer[3] = div & 0xff;
  246. } else {
  247. buffer[0] = (div>>8) & 0x7f;
  248. buffer[1] = div & 0xff;
  249. buffer[2] = config;
  250. buffer[3] = cb;
  251. }
  252. t->last_div = div;
  253. if (params->has_tda9887) {
  254. int config = 0;
  255. int is_secam_l = (t->std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC)) &&
  256. !(t->std & ~(V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC));
  257. if (t->std == V4L2_STD_SECAM_LC) {
  258. if (params->port1_active ^ params->port1_invert_for_secam_lc)
  259. config |= TDA9887_PORT1_ACTIVE;
  260. if (params->port2_active ^ params->port2_invert_for_secam_lc)
  261. config |= TDA9887_PORT2_ACTIVE;
  262. }
  263. else {
  264. if (params->port1_active)
  265. config |= TDA9887_PORT1_ACTIVE;
  266. if (params->port2_active)
  267. config |= TDA9887_PORT2_ACTIVE;
  268. }
  269. if (params->intercarrier_mode)
  270. config |= TDA9887_INTERCARRIER;
  271. if (is_secam_l) {
  272. if (i == 0 && params->default_top_secam_low)
  273. config |= TDA9887_TOP(params->default_top_secam_low);
  274. else if (i == 1 && params->default_top_secam_mid)
  275. config |= TDA9887_TOP(params->default_top_secam_mid);
  276. else if (params->default_top_secam_high)
  277. config |= TDA9887_TOP(params->default_top_secam_high);
  278. }
  279. else {
  280. if (i == 0 && params->default_top_low)
  281. config |= TDA9887_TOP(params->default_top_low);
  282. else if (i == 1 && params->default_top_mid)
  283. config |= TDA9887_TOP(params->default_top_mid);
  284. else if (params->default_top_high)
  285. config |= TDA9887_TOP(params->default_top_high);
  286. }
  287. i2c_clients_command(c->adapter, TDA9887_SET_CONFIG, &config);
  288. }
  289. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  290. buffer[0],buffer[1],buffer[2],buffer[3]);
  291. if (4 != (rc = i2c_master_send(c,buffer,4)))
  292. tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
  293. switch (t->type) {
  294. case TUNER_LG_TDVS_H06XF:
  295. /* Set the Auxiliary Byte. */
  296. buffer[0] = buffer[2];
  297. buffer[0] &= ~0x20;
  298. buffer[0] |= 0x18;
  299. buffer[1] = 0x20;
  300. tuner_dbg("tv 0x%02x 0x%02x\n",buffer[0],buffer[1]);
  301. if (2 != (rc = i2c_master_send(c,buffer,2)))
  302. tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
  303. break;
  304. case TUNER_MICROTUNE_4042FI5:
  305. {
  306. // FIXME - this may also work for other tuners
  307. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  308. u8 status_byte = 0;
  309. /* Wait until the PLL locks */
  310. for (;;) {
  311. if (time_after(jiffies,timeout))
  312. return;
  313. if (1 != (rc = i2c_master_recv(c,&status_byte,1))) {
  314. tuner_warn("i2c i/o read error: rc == %d (should be 1)\n",rc);
  315. break;
  316. }
  317. if (status_byte & TUNER_PLL_LOCKED)
  318. break;
  319. udelay(10);
  320. }
  321. /* Set the charge pump for optimized phase noise figure */
  322. config &= ~TUNER_CHARGE_PUMP;
  323. buffer[0] = (div>>8) & 0x7f;
  324. buffer[1] = div & 0xff;
  325. buffer[2] = config;
  326. buffer[3] = cb;
  327. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  328. buffer[0],buffer[1],buffer[2],buffer[3]);
  329. if (4 != (rc = i2c_master_send(c,buffer,4)))
  330. tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
  331. break;
  332. }
  333. }
  334. }
  335. static void default_set_radio_freq(struct i2c_client *c, unsigned int freq)
  336. {
  337. struct tunertype *tun;
  338. struct tuner *t = i2c_get_clientdata(c);
  339. u8 buffer[4];
  340. u16 div;
  341. int rc, j;
  342. enum param_type desired_type = TUNER_PARAM_TYPE_RADIO;
  343. struct tuner_params *params;
  344. tun = &tuners[t->type];
  345. for (j = 0; j < tun->count-1; j++) {
  346. if (desired_type != tun->params[j].type)
  347. continue;
  348. break;
  349. }
  350. /* use default tuner_params if desired_type not available */
  351. if (desired_type != tun->params[j].type)
  352. j = 0;
  353. div = (20 * freq / 16000) + (int)(20*10.7); /* IF 10.7 MHz */
  354. params = &tun->params[j];
  355. buffer[2] = (params->ranges[0].config & ~TUNER_RATIO_MASK) | TUNER_RATIO_SELECT_50; /* 50 kHz step */
  356. switch (t->type) {
  357. case TUNER_TENA_9533_DI:
  358. case TUNER_YMEC_TVF_5533MF:
  359. tuner_dbg ("This tuner doesn't have FM. Most cards has a TEA5767 for FM\n");
  360. return;
  361. case TUNER_PHILIPS_FM1216ME_MK3:
  362. case TUNER_PHILIPS_FM1236_MK3:
  363. case TUNER_PHILIPS_FMD1216ME_MK3:
  364. buffer[3] = 0x19;
  365. break;
  366. case TUNER_TNF_5335MF:
  367. buffer[3] = 0x11;
  368. break;
  369. case TUNER_PHILIPS_FM1256_IH3:
  370. div = (20 * freq) / 16000 + (int)(33.3 * 20); /* IF 33.3 MHz */
  371. buffer[3] = 0x19;
  372. break;
  373. case TUNER_LG_PAL_FM:
  374. buffer[3] = 0xa5;
  375. break;
  376. case TUNER_MICROTUNE_4049FM5:
  377. div = (20 * freq) / 16000 + (int)(33.3 * 20); /* IF 33.3 MHz */
  378. buffer[3] = 0xa4;
  379. break;
  380. default:
  381. buffer[3] = 0xa4;
  382. break;
  383. }
  384. buffer[0] = (div>>8) & 0x7f;
  385. buffer[1] = div & 0xff;
  386. if (params->cb_first_if_lower_freq && div < t->last_div) {
  387. buffer[0] = buffer[2];
  388. buffer[1] = buffer[3];
  389. buffer[2] = (div>>8) & 0x7f;
  390. buffer[3] = div & 0xff;
  391. } else {
  392. buffer[0] = (div>>8) & 0x7f;
  393. buffer[1] = div & 0xff;
  394. }
  395. tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
  396. buffer[0],buffer[1],buffer[2],buffer[3]);
  397. t->last_div = div;
  398. if (params->has_tda9887) {
  399. int config = 0;
  400. if (params->port1_active && !params->port1_fm_high_sensitivity)
  401. config |= TDA9887_PORT1_ACTIVE;
  402. if (params->port2_active && !params->port2_fm_high_sensitivity)
  403. config |= TDA9887_PORT2_ACTIVE;
  404. if (params->intercarrier_mode)
  405. config |= TDA9887_INTERCARRIER;
  406. /* if (params->port1_set_for_fm_mono)
  407. config &= ~TDA9887_PORT1_ACTIVE;*/
  408. i2c_clients_command(c->adapter, TDA9887_SET_CONFIG, &config);
  409. }
  410. if (4 != (rc = i2c_master_send(c,buffer,4)))
  411. tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
  412. }
  413. int default_tuner_init(struct i2c_client *c)
  414. {
  415. struct tuner *t = i2c_get_clientdata(c);
  416. tuner_info("type set to %d (%s)\n",
  417. t->type, tuners[t->type].name);
  418. strlcpy(c->name, tuners[t->type].name, sizeof(c->name));
  419. t->set_tv_freq = default_set_tv_freq;
  420. t->set_radio_freq = default_set_radio_freq;
  421. t->has_signal = tuner_signal;
  422. t->is_stereo = tuner_stereo;
  423. t->standby = NULL;
  424. return 0;
  425. }
  426. /*
  427. * Overrides for Emacs so that we follow Linus's tabbing style.
  428. * ---------------------------------------------------------------------------
  429. * Local variables:
  430. * c-basic-offset: 8
  431. * End:
  432. */