tuner-simple.c 11 KB

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