tuner-simple.c 11 KB

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