tuner-simple.c 15 KB

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