tuner-simple.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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, 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. for (i = 0; i < tun->params[j].count; i++) {
  120. if (freq > tun->params[j].ranges[i].limit)
  121. continue;
  122. break;
  123. }
  124. if (i == tun->params[j].count) {
  125. tuner_dbg("TV frequency out of range (%d > %d)",
  126. freq, tun->params[j].ranges[i - 1].limit);
  127. freq = tun->params[j].ranges[--i].limit;
  128. }
  129. config = tun->params[j].ranges[i].cb;
  130. /* i == 0 -> VHF_LO */
  131. /* i == 1 -> VHF_HI */
  132. /* i == 2 -> UHF */
  133. tuner_dbg("tv: range %d\n",i);
  134. /* tv norm specific stuff for multi-norm tuners */
  135. switch (t->type) {
  136. case TUNER_PHILIPS_SECAM: // FI1216MF
  137. /* 0x01 -> ??? no change ??? */
  138. /* 0x02 -> PAL BDGHI / SECAM L */
  139. /* 0x04 -> ??? PAL others / SECAM others ??? */
  140. config &= ~0x02;
  141. if (t->std & V4L2_STD_SECAM)
  142. config |= 0x02;
  143. break;
  144. case TUNER_TEMIC_4046FM5:
  145. config &= ~0x0f;
  146. if (t->std & V4L2_STD_PAL_BG) {
  147. config |= TEMIC_SET_PAL_BG;
  148. } else if (t->std & V4L2_STD_PAL_I) {
  149. config |= TEMIC_SET_PAL_I;
  150. } else if (t->std & V4L2_STD_PAL_DK) {
  151. config |= TEMIC_SET_PAL_DK;
  152. } else if (t->std & V4L2_STD_SECAM_L) {
  153. config |= TEMIC_SET_PAL_L;
  154. }
  155. break;
  156. case TUNER_PHILIPS_FQ1216ME:
  157. config &= ~0x0f;
  158. if (t->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
  159. config |= PHILIPS_SET_PAL_BGDK;
  160. } else if (t->std & V4L2_STD_PAL_I) {
  161. config |= PHILIPS_SET_PAL_I;
  162. } else if (t->std & V4L2_STD_SECAM_L) {
  163. config |= PHILIPS_SET_PAL_L;
  164. }
  165. break;
  166. case TUNER_PHILIPS_ATSC:
  167. /* 0x00 -> ATSC antenna input 1 */
  168. /* 0x01 -> ATSC antenna input 2 */
  169. /* 0x02 -> NTSC antenna input 1 */
  170. /* 0x03 -> NTSC antenna input 2 */
  171. config &= ~0x03;
  172. if (!(t->std & V4L2_STD_ATSC))
  173. config |= 2;
  174. /* FIXME: input */
  175. break;
  176. case TUNER_MICROTUNE_4042FI5:
  177. /* Set the charge pump for fast tuning */
  178. tun->params[j].config |= TUNER_CHARGE_PUMP;
  179. break;
  180. case TUNER_PHILIPS_TUV1236D:
  181. /* 0x40 -> ATSC antenna input 1 */
  182. /* 0x48 -> ATSC antenna input 2 */
  183. /* 0x00 -> NTSC antenna input 1 */
  184. /* 0x08 -> NTSC antenna input 2 */
  185. buffer[0] = 0x14;
  186. buffer[1] = 0x00;
  187. buffer[2] = 0x17;
  188. buffer[3] = 0x00;
  189. config &= ~0x40;
  190. if (t->std & V4L2_STD_ATSC) {
  191. config |= 0x40;
  192. buffer[1] = 0x04;
  193. }
  194. /* set to the correct mode (analog or digital) */
  195. tuneraddr = c->addr;
  196. c->addr = 0x0a;
  197. if (2 != (rc = i2c_master_send(c,&buffer[0],2)))
  198. tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
  199. if (2 != (rc = i2c_master_send(c,&buffer[2],2)))
  200. tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
  201. c->addr = tuneraddr;
  202. /* FIXME: input */
  203. break;
  204. }
  205. /* IFPCoff = Video Intermediate Frequency - Vif:
  206. 940 =16*58.75 NTSC/J (Japan)
  207. 732 =16*45.75 M/N STD
  208. 704 =16*44 ATSC (at DVB code)
  209. 632 =16*39.50 I U.K.
  210. 622.4=16*38.90 B/G D/K I, L STD
  211. 592 =16*37.00 D China
  212. 590 =16.36.875 B Australia
  213. 543.2=16*33.95 L' STD
  214. 171.2=16*10.70 FM Radio (at set_radio_freq)
  215. */
  216. if (t->std == V4L2_STD_NTSC_M_JP) {
  217. IFPCoff = 940;
  218. } else if ((t->std & V4L2_STD_MN) &&
  219. !(t->std & ~V4L2_STD_MN)) {
  220. IFPCoff = 732;
  221. } else if (t->std == V4L2_STD_SECAM_LC) {
  222. IFPCoff = 543;
  223. } else {
  224. IFPCoff = 623;
  225. }
  226. div=freq + IFPCoff + offset;
  227. tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, Offset=%d.%02d MHz, div=%0d\n",
  228. freq / 16, freq % 16 * 100 / 16,
  229. IFPCoff / 16, IFPCoff % 16 * 100 / 16,
  230. offset / 16, offset % 16 * 100 / 16,
  231. div);
  232. if (tuners[t->type].params->cb_first_if_lower_freq && div < t->last_div) {
  233. buffer[0] = tun->params[j].config;
  234. buffer[1] = config;
  235. buffer[2] = (div>>8) & 0x7f;
  236. buffer[3] = div & 0xff;
  237. } else {
  238. buffer[0] = (div>>8) & 0x7f;
  239. buffer[1] = div & 0xff;
  240. buffer[2] = tun->params[j].config;
  241. buffer[3] = config;
  242. }
  243. t->last_div = div;
  244. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  245. buffer[0],buffer[1],buffer[2],buffer[3]);
  246. if (4 != (rc = i2c_master_send(c,buffer,4)))
  247. tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
  248. if (t->type == TUNER_MICROTUNE_4042FI5) {
  249. // FIXME - this may also work for other tuners
  250. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  251. u8 status_byte = 0;
  252. /* Wait until the PLL locks */
  253. for (;;) {
  254. if (time_after(jiffies,timeout))
  255. return;
  256. if (1 != (rc = i2c_master_recv(c,&status_byte,1))) {
  257. tuner_warn("i2c i/o read error: rc == %d (should be 1)\n",rc);
  258. break;
  259. }
  260. if (status_byte & TUNER_PLL_LOCKED)
  261. break;
  262. udelay(10);
  263. }
  264. /* Set the charge pump for optimized phase noise figure */
  265. tun->params[j].config &= ~TUNER_CHARGE_PUMP;
  266. buffer[0] = (div>>8) & 0x7f;
  267. buffer[1] = div & 0xff;
  268. buffer[2] = tun->params[j].config;
  269. buffer[3] = config;
  270. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  271. buffer[0],buffer[1],buffer[2],buffer[3]);
  272. if (4 != (rc = i2c_master_send(c,buffer,4)))
  273. tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
  274. }
  275. }
  276. static void default_set_radio_freq(struct i2c_client *c, unsigned int freq)
  277. {
  278. struct tunertype *tun;
  279. struct tuner *t = i2c_get_clientdata(c);
  280. u8 buffer[4];
  281. u16 div;
  282. int rc, j;
  283. tun = &tuners[t->type];
  284. j = TUNER_PARAM_ANALOG;
  285. div = (20 * freq / 16000) + (int)(20*10.7); /* IF 10.7 MHz */
  286. buffer[2] = (tun->params[j].config & ~TUNER_RATIO_MASK) | TUNER_RATIO_SELECT_50; /* 50 kHz step */
  287. switch (t->type) {
  288. case TUNER_TENA_9533_DI:
  289. case TUNER_YMEC_TVF_5533MF:
  290. tuner_dbg ("This tuner doesn't have FM. Most cards has a TEA5767 for FM\n");
  291. return;
  292. case TUNER_PHILIPS_FM1216ME_MK3:
  293. case TUNER_PHILIPS_FM1236_MK3:
  294. case TUNER_PHILIPS_FMD1216ME_MK3:
  295. buffer[3] = 0x19;
  296. break;
  297. case TUNER_PHILIPS_FM1256_IH3:
  298. div = (20 * freq) / 16000 + (int)(33.3 * 20); /* IF 33.3 MHz */
  299. buffer[3] = 0x19;
  300. break;
  301. case TUNER_LG_PAL_FM:
  302. buffer[3] = 0xa5;
  303. break;
  304. case TUNER_MICROTUNE_4049FM5:
  305. div = (20 * freq) / 16000 + (int)(33.3 * 20); /* IF 33.3 MHz */
  306. buffer[3] = 0xa4;
  307. break;
  308. default:
  309. buffer[3] = 0xa4;
  310. break;
  311. }
  312. buffer[0] = (div>>8) & 0x7f;
  313. buffer[1] = div & 0xff;
  314. if (tuners[t->type].params->cb_first_if_lower_freq && div < t->last_div) {
  315. buffer[0] = buffer[2];
  316. buffer[1] = buffer[3];
  317. buffer[2] = (div>>8) & 0x7f;
  318. buffer[3] = div & 0xff;
  319. } else {
  320. buffer[0] = (div>>8) & 0x7f;
  321. buffer[1] = div & 0xff;
  322. }
  323. tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
  324. buffer[0],buffer[1],buffer[2],buffer[3]);
  325. t->last_div = div;
  326. if (4 != (rc = i2c_master_send(c,buffer,4)))
  327. tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
  328. }
  329. int default_tuner_init(struct i2c_client *c)
  330. {
  331. struct tuner *t = i2c_get_clientdata(c);
  332. tuner_info("type set to %d (%s)\n",
  333. t->type, tuners[t->type].name);
  334. strlcpy(c->name, tuners[t->type].name, sizeof(c->name));
  335. t->set_tv_freq = default_set_tv_freq;
  336. t->set_radio_freq = default_set_radio_freq;
  337. t->has_signal = tuner_signal;
  338. t->is_stereo = tuner_stereo;
  339. t->standby = NULL;
  340. return 0;
  341. }
  342. /*
  343. * Overrides for Emacs so that we follow Linus's tabbing style.
  344. * ---------------------------------------------------------------------------
  345. * Local variables:
  346. * c-basic-offset: 8
  347. * End:
  348. */