tuner-simple.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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, 0664);
  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. case TUNER_LG_NTSC_TAPE:
  93. stereo = ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
  94. break;
  95. default:
  96. stereo = status & TUNER_STEREO;
  97. }
  98. return stereo;
  99. }
  100. /* ---------------------------------------------------------------------- */
  101. static void default_set_tv_freq(struct i2c_client *c, unsigned int freq)
  102. {
  103. struct tuner *t = i2c_get_clientdata(c);
  104. u8 config, cb, tuneraddr;
  105. u16 div;
  106. struct tunertype *tun;
  107. u8 buffer[4];
  108. int rc, IFPCoff, i, j;
  109. enum param_type desired_type;
  110. struct tuner_params *params;
  111. tun = &tuners[t->type];
  112. /* IFPCoff = Video Intermediate Frequency - Vif:
  113. 940 =16*58.75 NTSC/J (Japan)
  114. 732 =16*45.75 M/N STD
  115. 704 =16*44 ATSC (at DVB code)
  116. 632 =16*39.50 I U.K.
  117. 622.4=16*38.90 B/G D/K I, L STD
  118. 592 =16*37.00 D China
  119. 590 =16.36.875 B Australia
  120. 543.2=16*33.95 L' STD
  121. 171.2=16*10.70 FM Radio (at set_radio_freq)
  122. */
  123. if (t->std == V4L2_STD_NTSC_M_JP) {
  124. IFPCoff = 940;
  125. desired_type = TUNER_PARAM_TYPE_NTSC;
  126. } else if ((t->std & V4L2_STD_MN) &&
  127. !(t->std & ~V4L2_STD_MN)) {
  128. IFPCoff = 732;
  129. desired_type = TUNER_PARAM_TYPE_NTSC;
  130. } else if (t->std == V4L2_STD_SECAM_LC) {
  131. IFPCoff = 543;
  132. desired_type = TUNER_PARAM_TYPE_SECAM;
  133. } else {
  134. IFPCoff = 623;
  135. desired_type = TUNER_PARAM_TYPE_PAL;
  136. }
  137. for (j = 0; j < tun->count-1; j++) {
  138. if (desired_type != tun->params[j].type)
  139. continue;
  140. break;
  141. }
  142. /* use default tuner_params if desired_type not available */
  143. if (desired_type != tun->params[j].type) {
  144. tuner_dbg("IFPCoff = %d: tuner_params undefined for tuner %d\n",
  145. IFPCoff,t->type);
  146. j = 0;
  147. }
  148. params = &tun->params[j];
  149. for (i = 0; i < params->count; i++) {
  150. if (freq > params->ranges[i].limit)
  151. continue;
  152. break;
  153. }
  154. if (i == params->count) {
  155. tuner_dbg("TV frequency out of range (%d > %d)",
  156. freq, params->ranges[i - 1].limit);
  157. freq = params->ranges[--i].limit;
  158. }
  159. config = params->ranges[i].config;
  160. cb = params->ranges[i].cb;
  161. /* i == 0 -> VHF_LO
  162. * i == 1 -> VHF_HI
  163. * i == 2 -> UHF */
  164. tuner_dbg("tv: param %d, range %d\n",j,i);
  165. div=freq + IFPCoff + offset;
  166. tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, Offset=%d.%02d MHz, div=%0d\n",
  167. freq / 16, freq % 16 * 100 / 16,
  168. IFPCoff / 16, IFPCoff % 16 * 100 / 16,
  169. offset / 16, offset % 16 * 100 / 16,
  170. div);
  171. /* tv norm specific stuff for multi-norm tuners */
  172. switch (t->type) {
  173. case TUNER_PHILIPS_SECAM: // FI1216MF
  174. /* 0x01 -> ??? no change ??? */
  175. /* 0x02 -> PAL BDGHI / SECAM L */
  176. /* 0x04 -> ??? PAL others / SECAM others ??? */
  177. cb &= ~0x02;
  178. if (t->std & V4L2_STD_SECAM)
  179. cb |= 0x02;
  180. break;
  181. case TUNER_TEMIC_4046FM5:
  182. cb &= ~0x0f;
  183. if (t->std & V4L2_STD_PAL_BG) {
  184. cb |= TEMIC_SET_PAL_BG;
  185. } else if (t->std & V4L2_STD_PAL_I) {
  186. cb |= TEMIC_SET_PAL_I;
  187. } else if (t->std & V4L2_STD_PAL_DK) {
  188. cb |= TEMIC_SET_PAL_DK;
  189. } else if (t->std & V4L2_STD_SECAM_L) {
  190. cb |= TEMIC_SET_PAL_L;
  191. }
  192. break;
  193. case TUNER_PHILIPS_FQ1216ME:
  194. cb &= ~0x0f;
  195. if (t->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
  196. cb |= PHILIPS_SET_PAL_BGDK;
  197. } else if (t->std & V4L2_STD_PAL_I) {
  198. cb |= PHILIPS_SET_PAL_I;
  199. } else if (t->std & V4L2_STD_SECAM_L) {
  200. cb |= PHILIPS_SET_PAL_L;
  201. }
  202. break;
  203. case TUNER_PHILIPS_ATSC:
  204. /* 0x00 -> ATSC antenna input 1 */
  205. /* 0x01 -> ATSC antenna input 2 */
  206. /* 0x02 -> NTSC antenna input 1 */
  207. /* 0x03 -> NTSC antenna input 2 */
  208. cb &= ~0x03;
  209. if (!(t->std & V4L2_STD_ATSC))
  210. cb |= 2;
  211. /* FIXME: input */
  212. break;
  213. case TUNER_MICROTUNE_4042FI5:
  214. /* Set the charge pump for fast tuning */
  215. config |= TUNER_CHARGE_PUMP;
  216. break;
  217. case TUNER_PHILIPS_TUV1236D:
  218. /* 0x40 -> ATSC antenna input 1 */
  219. /* 0x48 -> ATSC antenna input 2 */
  220. /* 0x00 -> NTSC antenna input 1 */
  221. /* 0x08 -> NTSC antenna input 2 */
  222. buffer[0] = 0x14;
  223. buffer[1] = 0x00;
  224. buffer[2] = 0x17;
  225. buffer[3] = 0x00;
  226. cb &= ~0x40;
  227. if (t->std & V4L2_STD_ATSC) {
  228. cb |= 0x40;
  229. buffer[1] = 0x04;
  230. }
  231. /* set to the correct mode (analog or digital) */
  232. tuneraddr = c->addr;
  233. c->addr = 0x0a;
  234. if (2 != (rc = i2c_master_send(c,&buffer[0],2)))
  235. tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
  236. if (2 != (rc = i2c_master_send(c,&buffer[2],2)))
  237. tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
  238. c->addr = tuneraddr;
  239. /* FIXME: input */
  240. break;
  241. }
  242. if (params->cb_first_if_lower_freq && div < t->last_div) {
  243. buffer[0] = config;
  244. buffer[1] = cb;
  245. buffer[2] = (div>>8) & 0x7f;
  246. buffer[3] = div & 0xff;
  247. } else {
  248. buffer[0] = (div>>8) & 0x7f;
  249. buffer[1] = div & 0xff;
  250. buffer[2] = config;
  251. buffer[3] = cb;
  252. }
  253. t->last_div = div;
  254. if (params->has_tda9887) {
  255. int config = 0;
  256. int is_secam_l = (t->std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC)) &&
  257. !(t->std & ~(V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC));
  258. if (t->std == V4L2_STD_SECAM_LC) {
  259. if (params->port1_active ^ params->port1_invert_for_secam_lc)
  260. config |= TDA9887_PORT1_ACTIVE;
  261. if (params->port2_active ^ params->port2_invert_for_secam_lc)
  262. config |= TDA9887_PORT2_ACTIVE;
  263. }
  264. else {
  265. if (params->port1_active)
  266. config |= TDA9887_PORT1_ACTIVE;
  267. if (params->port2_active)
  268. config |= TDA9887_PORT2_ACTIVE;
  269. }
  270. if (params->intercarrier_mode)
  271. config |= TDA9887_INTERCARRIER;
  272. if (is_secam_l) {
  273. if (i == 0 && params->default_top_secam_low)
  274. config |= TDA9887_TOP(params->default_top_secam_low);
  275. else if (i == 1 && params->default_top_secam_mid)
  276. config |= TDA9887_TOP(params->default_top_secam_mid);
  277. else if (params->default_top_secam_high)
  278. config |= TDA9887_TOP(params->default_top_secam_high);
  279. }
  280. else {
  281. if (i == 0 && params->default_top_low)
  282. config |= TDA9887_TOP(params->default_top_low);
  283. else if (i == 1 && params->default_top_mid)
  284. config |= TDA9887_TOP(params->default_top_mid);
  285. else if (params->default_top_high)
  286. config |= TDA9887_TOP(params->default_top_high);
  287. }
  288. if (params->default_pll_gating_18)
  289. config |= TDA9887_GATING_18;
  290. i2c_clients_command(c->adapter, TDA9887_SET_CONFIG, &config);
  291. }
  292. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  293. buffer[0],buffer[1],buffer[2],buffer[3]);
  294. if (4 != (rc = i2c_master_send(c,buffer,4)))
  295. tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
  296. switch (t->type) {
  297. case TUNER_LG_TDVS_H06XF:
  298. /* Set the Auxiliary Byte. */
  299. buffer[0] = buffer[2];
  300. buffer[0] &= ~0x20;
  301. buffer[0] |= 0x18;
  302. buffer[1] = 0x20;
  303. tuner_dbg("tv 0x%02x 0x%02x\n",buffer[0],buffer[1]);
  304. if (2 != (rc = i2c_master_send(c,buffer,2)))
  305. tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
  306. break;
  307. case TUNER_MICROTUNE_4042FI5:
  308. {
  309. // FIXME - this may also work for other tuners
  310. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  311. u8 status_byte = 0;
  312. /* Wait until the PLL locks */
  313. for (;;) {
  314. if (time_after(jiffies,timeout))
  315. return;
  316. if (1 != (rc = i2c_master_recv(c,&status_byte,1))) {
  317. tuner_warn("i2c i/o read error: rc == %d (should be 1)\n",rc);
  318. break;
  319. }
  320. if (status_byte & TUNER_PLL_LOCKED)
  321. break;
  322. udelay(10);
  323. }
  324. /* Set the charge pump for optimized phase noise figure */
  325. config &= ~TUNER_CHARGE_PUMP;
  326. buffer[0] = (div>>8) & 0x7f;
  327. buffer[1] = div & 0xff;
  328. buffer[2] = config;
  329. buffer[3] = cb;
  330. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  331. buffer[0],buffer[1],buffer[2],buffer[3]);
  332. if (4 != (rc = i2c_master_send(c,buffer,4)))
  333. tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
  334. break;
  335. }
  336. }
  337. }
  338. static void default_set_radio_freq(struct i2c_client *c, unsigned int freq)
  339. {
  340. struct tunertype *tun;
  341. struct tuner *t = i2c_get_clientdata(c);
  342. u8 buffer[4];
  343. u16 div;
  344. int rc, j;
  345. enum param_type desired_type = TUNER_PARAM_TYPE_RADIO;
  346. struct tuner_params *params;
  347. tun = &tuners[t->type];
  348. for (j = 0; j < tun->count-1; j++) {
  349. if (desired_type != tun->params[j].type)
  350. continue;
  351. break;
  352. }
  353. /* use default tuner_params if desired_type not available */
  354. if (desired_type != tun->params[j].type)
  355. j = 0;
  356. div = (20 * freq / 16000) + (int)(20*10.7); /* IF 10.7 MHz */
  357. params = &tun->params[j];
  358. buffer[2] = (params->ranges[0].config & ~TUNER_RATIO_MASK) | TUNER_RATIO_SELECT_50; /* 50 kHz step */
  359. switch (t->type) {
  360. case TUNER_TENA_9533_DI:
  361. case TUNER_YMEC_TVF_5533MF:
  362. tuner_dbg ("This tuner doesn't have FM. Most cards has a TEA5767 for FM\n");
  363. return;
  364. case TUNER_PHILIPS_FM1216ME_MK3:
  365. case TUNER_PHILIPS_FM1236_MK3:
  366. case TUNER_PHILIPS_FMD1216ME_MK3:
  367. case TUNER_LG_NTSC_TAPE:
  368. buffer[3] = 0x19;
  369. break;
  370. case TUNER_TNF_5335MF:
  371. buffer[3] = 0x11;
  372. break;
  373. case TUNER_PHILIPS_FM1256_IH3:
  374. div = (20 * freq) / 16000 + (int)(33.3 * 20); /* IF 33.3 MHz */
  375. buffer[3] = 0x19;
  376. break;
  377. case TUNER_LG_PAL_FM:
  378. buffer[3] = 0xa5;
  379. break;
  380. case TUNER_MICROTUNE_4049FM5:
  381. div = (20 * freq) / 16000 + (int)(33.3 * 20); /* IF 33.3 MHz */
  382. buffer[3] = 0xa4;
  383. break;
  384. default:
  385. buffer[3] = 0xa4;
  386. break;
  387. }
  388. if (params->cb_first_if_lower_freq && div < t->last_div) {
  389. buffer[0] = buffer[2];
  390. buffer[1] = buffer[3];
  391. buffer[2] = (div>>8) & 0x7f;
  392. buffer[3] = div & 0xff;
  393. } else {
  394. buffer[0] = (div>>8) & 0x7f;
  395. buffer[1] = div & 0xff;
  396. }
  397. tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
  398. buffer[0],buffer[1],buffer[2],buffer[3]);
  399. t->last_div = div;
  400. if (params->has_tda9887) {
  401. int config = 0;
  402. if (params->port1_active && !params->port1_fm_high_sensitivity)
  403. config |= TDA9887_PORT1_ACTIVE;
  404. if (params->port2_active && !params->port2_fm_high_sensitivity)
  405. config |= TDA9887_PORT2_ACTIVE;
  406. if (params->intercarrier_mode)
  407. config |= TDA9887_INTERCARRIER;
  408. /* if (params->port1_set_for_fm_mono)
  409. config &= ~TDA9887_PORT1_ACTIVE;*/
  410. if (params->fm_gain_normal)
  411. config |= TDA9887_GAIN_NORMAL;
  412. i2c_clients_command(c->adapter, TDA9887_SET_CONFIG, &config);
  413. }
  414. if (4 != (rc = i2c_master_send(c,buffer,4)))
  415. tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
  416. }
  417. int default_tuner_init(struct i2c_client *c)
  418. {
  419. struct tuner *t = i2c_get_clientdata(c);
  420. tuner_info("type set to %d (%s)\n",
  421. t->type, tuners[t->type].name);
  422. strlcpy(c->name, tuners[t->type].name, sizeof(c->name));
  423. t->set_tv_freq = default_set_tv_freq;
  424. t->set_radio_freq = default_set_radio_freq;
  425. t->has_signal = tuner_signal;
  426. t->is_stereo = tuner_stereo;
  427. t->standby = NULL;
  428. return 0;
  429. }
  430. /*
  431. * Overrides for Emacs so that we follow Linus's tabbing style.
  432. * ---------------------------------------------------------------------------
  433. * Local variables:
  434. * c-basic-offset: 8
  435. * End:
  436. */