tuner-simple.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. };
  76. /* ---------------------------------------------------------------------- */
  77. static int tuner_getstatus(struct i2c_client *c)
  78. {
  79. unsigned char byte;
  80. if (1 != i2c_master_recv(c,&byte,1))
  81. return 0;
  82. return byte;
  83. }
  84. static int tuner_signal(struct i2c_client *c)
  85. {
  86. return (tuner_getstatus(c) & TUNER_SIGNAL) << 13;
  87. }
  88. static int tuner_stereo(struct i2c_client *c)
  89. {
  90. int stereo, status;
  91. struct tuner *t = i2c_get_clientdata(c);
  92. status = tuner_getstatus (c);
  93. switch (t->type) {
  94. case TUNER_PHILIPS_FM1216ME_MK3:
  95. case TUNER_PHILIPS_FM1236_MK3:
  96. case TUNER_PHILIPS_FM1256_IH3:
  97. case TUNER_LG_NTSC_TAPE:
  98. stereo = ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
  99. break;
  100. default:
  101. stereo = status & TUNER_STEREO;
  102. }
  103. return stereo;
  104. }
  105. /* ---------------------------------------------------------------------- */
  106. static void default_set_tv_freq(struct i2c_client *c, unsigned int freq)
  107. {
  108. struct tuner *t = i2c_get_clientdata(c);
  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 = c->addr;
  243. c->addr = 0x0a;
  244. if (2 != (rc = i2c_master_send(c,&buffer[0],2)))
  245. tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
  246. if (2 != (rc = i2c_master_send(c,&buffer[2],2)))
  247. tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
  248. c->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(c->adapter, 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 = i2c_master_send(c,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 = i2c_master_send(c,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 = i2c_master_recv(c,&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 = i2c_master_send(c,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 i2c_client *c, unsigned int freq)
  349. {
  350. struct tunertype *tun;
  351. struct tuner *t = i2c_get_clientdata(c);
  352. struct tuner_simple_priv *priv = t->priv;
  353. u8 buffer[4];
  354. u16 div;
  355. int rc, j;
  356. struct tuner_params *params;
  357. tun = &tuners[t->type];
  358. for (j = tun->count-1; j > 0; j--)
  359. if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
  360. break;
  361. /* default params (j=0) will be used if desired type wasn't found */
  362. params = &tun->params[j];
  363. /* Select Radio 1st IF used */
  364. switch (params->radio_if) {
  365. case 0: /* 10.7 MHz */
  366. freq += (unsigned int)(10.7*16000);
  367. break;
  368. case 1: /* 33.3 MHz */
  369. freq += (unsigned int)(33.3*16000);
  370. break;
  371. case 2: /* 41.3 MHz */
  372. freq += (unsigned int)(41.3*16000);
  373. break;
  374. default:
  375. tuner_warn("Unsupported radio_if value %d\n", params->radio_if);
  376. return;
  377. }
  378. /* Bandswitch byte */
  379. switch (t->type) {
  380. case TUNER_TENA_9533_DI:
  381. case TUNER_YMEC_TVF_5533MF:
  382. tuner_dbg ("This tuner doesn't have FM. Most cards have a TEA5767 for FM\n");
  383. return;
  384. case TUNER_PHILIPS_FM1216ME_MK3:
  385. case TUNER_PHILIPS_FM1236_MK3:
  386. case TUNER_PHILIPS_FMD1216ME_MK3:
  387. case TUNER_LG_NTSC_TAPE:
  388. case TUNER_PHILIPS_FM1256_IH3:
  389. buffer[3] = 0x19;
  390. break;
  391. case TUNER_TNF_5335MF:
  392. buffer[3] = 0x11;
  393. break;
  394. case TUNER_LG_PAL_FM:
  395. buffer[3] = 0xa5;
  396. break;
  397. case TUNER_THOMSON_DTT761X:
  398. buffer[3] = 0x39;
  399. break;
  400. case TUNER_MICROTUNE_4049FM5:
  401. default:
  402. buffer[3] = 0xa4;
  403. break;
  404. }
  405. buffer[2] = (params->ranges[0].config & ~TUNER_RATIO_MASK) |
  406. TUNER_RATIO_SELECT_50; /* 50 kHz step */
  407. /* Convert from 1/16 kHz V4L steps to 1/20 MHz (=50 kHz) PLL steps
  408. freq * (1 Mhz / 16000 V4L steps) * (20 PLL steps / 1 MHz) =
  409. freq * (1/800) */
  410. div = (freq + 400) / 800;
  411. if (params->cb_first_if_lower_freq && div < priv->last_div) {
  412. buffer[0] = buffer[2];
  413. buffer[1] = buffer[3];
  414. buffer[2] = (div>>8) & 0x7f;
  415. buffer[3] = div & 0xff;
  416. } else {
  417. buffer[0] = (div>>8) & 0x7f;
  418. buffer[1] = div & 0xff;
  419. }
  420. tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
  421. buffer[0],buffer[1],buffer[2],buffer[3]);
  422. priv->last_div = div;
  423. if (params->has_tda9887) {
  424. int config = 0;
  425. if (params->port1_active && !params->port1_fm_high_sensitivity)
  426. config |= TDA9887_PORT1_ACTIVE;
  427. if (params->port2_active && !params->port2_fm_high_sensitivity)
  428. config |= TDA9887_PORT2_ACTIVE;
  429. if (params->intercarrier_mode)
  430. config |= TDA9887_INTERCARRIER;
  431. /* if (params->port1_set_for_fm_mono)
  432. config &= ~TDA9887_PORT1_ACTIVE;*/
  433. if (params->fm_gain_normal)
  434. config |= TDA9887_GAIN_NORMAL;
  435. if (params->radio_if == 2)
  436. config |= TDA9887_RIF_41_3;
  437. i2c_clients_command(c->adapter, TDA9887_SET_CONFIG, &config);
  438. }
  439. if (4 != (rc = i2c_master_send(c,buffer,4)))
  440. tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
  441. }
  442. static void tuner_release(struct i2c_client *c)
  443. {
  444. struct tuner *t = i2c_get_clientdata(c);
  445. kfree(t->priv);
  446. t->priv = NULL;
  447. }
  448. static struct tuner_operations simple_tuner_ops = {
  449. .set_tv_freq = default_set_tv_freq,
  450. .set_radio_freq = default_set_radio_freq,
  451. .has_signal = tuner_signal,
  452. .is_stereo = tuner_stereo,
  453. .release = tuner_release,
  454. };
  455. int default_tuner_init(struct i2c_client *c)
  456. {
  457. struct tuner *t = i2c_get_clientdata(c);
  458. struct tuner_simple_priv *priv = NULL;
  459. priv = kzalloc(sizeof(struct tuner_simple_priv), GFP_KERNEL);
  460. if (priv == NULL)
  461. return -ENOMEM;
  462. t->priv = priv;
  463. tuner_info("type set to %d (%s)\n",
  464. t->type, tuners[t->type].name);
  465. strlcpy(c->name, tuners[t->type].name, sizeof(c->name));
  466. memcpy(&t->ops, &simple_tuner_ops, sizeof(struct tuner_operations));
  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. */