tuner-simple.c 14 KB

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