tuner-simple.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * i2c tv tuner chip device driver
  3. * controls all those simple 4-control-bytes style tuners.
  4. *
  5. * This "tuner-simple" module was split apart from the original "tuner" module.
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/i2c.h>
  9. #include <linux/videodev.h>
  10. #include <media/tuner.h>
  11. #include <media/v4l2-common.h>
  12. #include <media/tuner-types.h>
  13. #include "tuner-i2c.h"
  14. #include "tuner-simple.h"
  15. static int debug;
  16. module_param(debug, int, 0644);
  17. MODULE_PARM_DESC(debug, "enable verbose debug messages");
  18. static int offset;
  19. module_param(offset, int, 0664);
  20. MODULE_PARM_DESC(offset, "Allows to specify an offset for tuner");
  21. /* ---------------------------------------------------------------------- */
  22. /* tv standard selection for Temic 4046 FM5
  23. this value takes the low bits of control byte 2
  24. from datasheet Rev.01, Feb.00
  25. standard BG I L L2 D
  26. picture IF 38.9 38.9 38.9 33.95 38.9
  27. sound 1 33.4 32.9 32.4 40.45 32.4
  28. sound 2 33.16
  29. NICAM 33.05 32.348 33.05 33.05
  30. */
  31. #define TEMIC_SET_PAL_I 0x05
  32. #define TEMIC_SET_PAL_DK 0x09
  33. #define TEMIC_SET_PAL_L 0x0a /* SECAM ? */
  34. #define TEMIC_SET_PAL_L2 0x0b /* change IF ! */
  35. #define TEMIC_SET_PAL_BG 0x0c
  36. /* tv tuner system standard selection for Philips FQ1216ME
  37. this value takes the low bits of control byte 2
  38. from datasheet "1999 Nov 16" (supersedes "1999 Mar 23")
  39. standard BG DK I L L`
  40. picture carrier 38.90 38.90 38.90 38.90 33.95
  41. colour 34.47 34.47 34.47 34.47 38.38
  42. sound 1 33.40 32.40 32.90 32.40 40.45
  43. sound 2 33.16 - - - -
  44. NICAM 33.05 33.05 32.35 33.05 39.80
  45. */
  46. #define PHILIPS_SET_PAL_I 0x01 /* Bit 2 always zero !*/
  47. #define PHILIPS_SET_PAL_BGDK 0x09
  48. #define PHILIPS_SET_PAL_L2 0x0a
  49. #define PHILIPS_SET_PAL_L 0x0b
  50. /* system switching for Philips FI1216MF MK2
  51. from datasheet "1996 Jul 09",
  52. standard BG L L'
  53. picture carrier 38.90 38.90 33.95
  54. colour 34.47 34.37 38.38
  55. sound 1 33.40 32.40 40.45
  56. sound 2 33.16 - -
  57. NICAM 33.05 33.05 39.80
  58. */
  59. #define PHILIPS_MF_SET_STD_BG 0x01 /* Bit 2 must be zero, Bit 3 is system output */
  60. #define PHILIPS_MF_SET_STD_L 0x03 /* Used on Secam France */
  61. #define PHILIPS_MF_SET_STD_LC 0x02 /* Used on SECAM L' */
  62. /* Control byte */
  63. #define TUNER_RATIO_MASK 0x06 /* Bit cb1:cb2 */
  64. #define TUNER_RATIO_SELECT_50 0x00
  65. #define TUNER_RATIO_SELECT_32 0x02
  66. #define TUNER_RATIO_SELECT_166 0x04
  67. #define TUNER_RATIO_SELECT_62 0x06
  68. #define TUNER_CHARGE_PUMP 0x40 /* Bit cb6 */
  69. /* Status byte */
  70. #define TUNER_POR 0x80
  71. #define TUNER_FL 0x40
  72. #define TUNER_MODE 0x38
  73. #define TUNER_AFC 0x07
  74. #define TUNER_SIGNAL 0x07
  75. #define TUNER_STEREO 0x10
  76. #define TUNER_PLL_LOCKED 0x40
  77. #define TUNER_STEREO_MK3 0x04
  78. struct tuner_simple_priv {
  79. u16 last_div;
  80. struct tuner_i2c_props i2c_props;
  81. unsigned int type;
  82. struct tunertype *tun;
  83. u32 frequency;
  84. };
  85. /* ---------------------------------------------------------------------- */
  86. static int tuner_read_status(struct dvb_frontend *fe)
  87. {
  88. struct tuner_simple_priv *priv = fe->tuner_priv;
  89. unsigned char byte;
  90. if (1 != tuner_i2c_xfer_recv(&priv->i2c_props, &byte, 1))
  91. return 0;
  92. return byte;
  93. }
  94. static inline int tuner_signal(const int status)
  95. {
  96. return (status & TUNER_SIGNAL) << 13;
  97. }
  98. static inline int tuner_stereo(const int type, const int status)
  99. {
  100. switch (type) {
  101. case TUNER_PHILIPS_FM1216ME_MK3:
  102. case TUNER_PHILIPS_FM1236_MK3:
  103. case TUNER_PHILIPS_FM1256_IH3:
  104. case TUNER_LG_NTSC_TAPE:
  105. return ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
  106. default:
  107. return status & TUNER_STEREO;
  108. }
  109. }
  110. static inline int tuner_islocked(const int status)
  111. {
  112. return (status & TUNER_FL);
  113. }
  114. static inline int tuner_afcstatus(const int status)
  115. {
  116. return (status & TUNER_AFC) - 2;
  117. }
  118. static int simple_get_status(struct dvb_frontend *fe, u32 *status)
  119. {
  120. struct tuner_simple_priv *priv = fe->tuner_priv;
  121. int tuner_status = tuner_read_status(fe);
  122. *status = 0;
  123. if (tuner_islocked(tuner_status))
  124. *status = TUNER_STATUS_LOCKED;
  125. if (tuner_stereo(priv->type, tuner_status))
  126. *status |= TUNER_STATUS_STEREO;
  127. tuner_dbg("AFC Status: %d\n", tuner_afcstatus(tuner_status));
  128. return 0;
  129. }
  130. static int simple_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
  131. {
  132. struct tuner_simple_priv *priv = fe->tuner_priv;
  133. int signal = tuner_signal(tuner_read_status(fe));
  134. *strength = signal;
  135. tuner_dbg("Signal strength: %d\n", signal);
  136. return 0;
  137. }
  138. /* ---------------------------------------------------------------------- */
  139. static inline char *tuner_param_name(enum param_type type)
  140. {
  141. char *name;
  142. switch (type) {
  143. case TUNER_PARAM_TYPE_RADIO:
  144. name = "radio";
  145. break;
  146. case TUNER_PARAM_TYPE_PAL:
  147. name = "pal";
  148. break;
  149. case TUNER_PARAM_TYPE_SECAM:
  150. name = "secam";
  151. break;
  152. case TUNER_PARAM_TYPE_NTSC:
  153. name = "ntsc";
  154. break;
  155. default:
  156. name = "unknown";
  157. break;
  158. }
  159. return name;
  160. }
  161. static struct tuner_params *simple_tuner_params(struct dvb_frontend *fe,
  162. enum param_type desired_type)
  163. {
  164. struct tuner_simple_priv *priv = fe->tuner_priv;
  165. struct tunertype *tun = priv->tun;
  166. int i;
  167. for (i = 0; i < tun->count; i++)
  168. if (desired_type == tun->params[i].type)
  169. break;
  170. /* use default tuner params if desired_type not available */
  171. if (i == tun->count) {
  172. tuner_dbg("desired params (%s) undefined for tuner %d\n",
  173. tuner_param_name(desired_type), priv->type);
  174. i = 0;
  175. }
  176. tuner_dbg("using tuner params #%d (%s)\n", i,
  177. tuner_param_name(tun->params[i].type));
  178. return &tun->params[i];
  179. }
  180. static int simple_config_lookup(struct dvb_frontend *fe,
  181. struct tuner_params *t_params,
  182. int *frequency, u8 *config, u8 *cb)
  183. {
  184. struct tuner_simple_priv *priv = fe->tuner_priv;
  185. int i;
  186. for (i = 0; i < t_params->count; i++) {
  187. if (*frequency > t_params->ranges[i].limit)
  188. continue;
  189. break;
  190. }
  191. if (i == t_params->count) {
  192. tuner_dbg("frequency out of range (%d > %d)\n",
  193. *frequency, t_params->ranges[i - 1].limit);
  194. *frequency = t_params->ranges[--i].limit;
  195. }
  196. *config = t_params->ranges[i].config;
  197. *cb = t_params->ranges[i].cb;
  198. tuner_dbg("freq = %d.%02d (%d), range = %d, "
  199. "config = 0x%02x, cb = 0x%02x\n",
  200. *frequency / 16, *frequency % 16 * 100 / 16, *frequency,
  201. i, *config, *cb);
  202. return i;
  203. }
  204. /* ---------------------------------------------------------------------- */
  205. static int simple_std_setup(struct dvb_frontend *fe,
  206. struct analog_parameters *params,
  207. u8 *buffer, u8 *config, u8 *cb)
  208. {
  209. struct tuner_simple_priv *priv = fe->tuner_priv;
  210. u8 tuneraddr;
  211. int rc;
  212. /* tv norm specific stuff for multi-norm tuners */
  213. switch (priv->type) {
  214. case TUNER_PHILIPS_SECAM: /* FI1216MF */
  215. /* 0x01 -> ??? no change ??? */
  216. /* 0x02 -> PAL BDGHI / SECAM L */
  217. /* 0x04 -> ??? PAL others / SECAM others ??? */
  218. *cb &= ~0x03;
  219. if (params->std & V4L2_STD_SECAM_L)
  220. /* also valid for V4L2_STD_SECAM */
  221. *cb |= PHILIPS_MF_SET_STD_L;
  222. else if (params->std & V4L2_STD_SECAM_LC)
  223. *cb |= PHILIPS_MF_SET_STD_LC;
  224. else /* V4L2_STD_B|V4L2_STD_GH */
  225. *cb |= PHILIPS_MF_SET_STD_BG;
  226. break;
  227. case TUNER_TEMIC_4046FM5:
  228. *cb &= ~0x0f;
  229. if (params->std & V4L2_STD_PAL_BG) {
  230. *cb |= TEMIC_SET_PAL_BG;
  231. } else if (params->std & V4L2_STD_PAL_I) {
  232. *cb |= TEMIC_SET_PAL_I;
  233. } else if (params->std & V4L2_STD_PAL_DK) {
  234. *cb |= TEMIC_SET_PAL_DK;
  235. } else if (params->std & V4L2_STD_SECAM_L) {
  236. *cb |= TEMIC_SET_PAL_L;
  237. }
  238. break;
  239. case TUNER_PHILIPS_FQ1216ME:
  240. *cb &= ~0x0f;
  241. if (params->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
  242. *cb |= PHILIPS_SET_PAL_BGDK;
  243. } else if (params->std & V4L2_STD_PAL_I) {
  244. *cb |= PHILIPS_SET_PAL_I;
  245. } else if (params->std & V4L2_STD_SECAM_L) {
  246. *cb |= PHILIPS_SET_PAL_L;
  247. }
  248. break;
  249. case TUNER_PHILIPS_ATSC:
  250. /* 0x00 -> ATSC antenna input 1 */
  251. /* 0x01 -> ATSC antenna input 2 */
  252. /* 0x02 -> NTSC antenna input 1 */
  253. /* 0x03 -> NTSC antenna input 2 */
  254. *cb &= ~0x03;
  255. if (!(params->std & V4L2_STD_ATSC))
  256. *cb |= 2;
  257. /* FIXME: input */
  258. break;
  259. case TUNER_MICROTUNE_4042FI5:
  260. /* Set the charge pump for fast tuning */
  261. *config |= TUNER_CHARGE_PUMP;
  262. break;
  263. case TUNER_PHILIPS_TUV1236D:
  264. /* 0x40 -> ATSC antenna input 1 */
  265. /* 0x48 -> ATSC antenna input 2 */
  266. /* 0x00 -> NTSC antenna input 1 */
  267. /* 0x08 -> NTSC antenna input 2 */
  268. buffer[0] = 0x14;
  269. buffer[1] = 0x00;
  270. buffer[2] = 0x17;
  271. buffer[3] = 0x00;
  272. *cb &= ~0x40;
  273. if (params->std & V4L2_STD_ATSC) {
  274. *cb |= 0x40;
  275. buffer[1] = 0x04;
  276. }
  277. /* set to the correct mode (analog or digital) */
  278. tuneraddr = priv->i2c_props.addr;
  279. priv->i2c_props.addr = 0x0a;
  280. rc = tuner_i2c_xfer_send(&priv->i2c_props, &buffer[0], 2);
  281. if (2 != rc)
  282. tuner_warn("i2c i/o error: rc == %d "
  283. "(should be 2)\n", rc);
  284. rc = tuner_i2c_xfer_send(&priv->i2c_props, &buffer[2], 2);
  285. if (2 != rc)
  286. tuner_warn("i2c i/o error: rc == %d "
  287. "(should be 2)\n", rc);
  288. priv->i2c_props.addr = tuneraddr;
  289. /* FIXME: input */
  290. break;
  291. }
  292. return 0;
  293. }
  294. static int simple_post_tune(struct dvb_frontend *fe, u8 *buffer,
  295. u16 div, u8 config, u8 cb)
  296. {
  297. struct tuner_simple_priv *priv = fe->tuner_priv;
  298. int rc;
  299. switch (priv->type) {
  300. case TUNER_LG_TDVS_H06XF:
  301. /* Set the Auxiliary Byte. */
  302. buffer[0] = buffer[2];
  303. buffer[0] &= ~0x20;
  304. buffer[0] |= 0x18;
  305. buffer[1] = 0x20;
  306. tuner_dbg("tv 0x%02x 0x%02x\n", buffer[0], buffer[1]);
  307. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 2);
  308. if (2 != rc)
  309. tuner_warn("i2c i/o error: rc == %d "
  310. "(should be 2)\n", rc);
  311. break;
  312. case TUNER_MICROTUNE_4042FI5:
  313. {
  314. /* FIXME - this may also work for other tuners */
  315. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  316. u8 status_byte = 0;
  317. /* Wait until the PLL locks */
  318. for (;;) {
  319. if (time_after(jiffies, timeout))
  320. return 0;
  321. rc = tuner_i2c_xfer_recv(&priv->i2c_props,
  322. &status_byte, 1);
  323. if (1 != rc) {
  324. tuner_warn("i2c i/o read error: rc == %d "
  325. "(should be 1)\n", rc);
  326. break;
  327. }
  328. if (status_byte & TUNER_PLL_LOCKED)
  329. break;
  330. udelay(10);
  331. }
  332. /* Set the charge pump for optimized phase noise figure */
  333. config &= ~TUNER_CHARGE_PUMP;
  334. buffer[0] = (div>>8) & 0x7f;
  335. buffer[1] = div & 0xff;
  336. buffer[2] = config;
  337. buffer[3] = cb;
  338. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  339. buffer[0], buffer[1], buffer[2], buffer[3]);
  340. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  341. if (4 != rc)
  342. tuner_warn("i2c i/o error: rc == %d "
  343. "(should be 4)\n", rc);
  344. break;
  345. }
  346. }
  347. return 0;
  348. }
  349. static int simple_radio_bandswitch(struct dvb_frontend *fe, u8 *buffer)
  350. {
  351. struct tuner_simple_priv *priv = fe->tuner_priv;
  352. switch (priv->type) {
  353. case TUNER_TENA_9533_DI:
  354. case TUNER_YMEC_TVF_5533MF:
  355. tuner_dbg("This tuner doesn't have FM. "
  356. "Most cards have a TEA5767 for FM\n");
  357. return 0;
  358. case TUNER_PHILIPS_FM1216ME_MK3:
  359. case TUNER_PHILIPS_FM1236_MK3:
  360. case TUNER_PHILIPS_FMD1216ME_MK3:
  361. case TUNER_LG_NTSC_TAPE:
  362. case TUNER_PHILIPS_FM1256_IH3:
  363. buffer[3] = 0x19;
  364. break;
  365. case TUNER_TNF_5335MF:
  366. buffer[3] = 0x11;
  367. break;
  368. case TUNER_LG_PAL_FM:
  369. buffer[3] = 0xa5;
  370. break;
  371. case TUNER_THOMSON_DTT761X:
  372. buffer[3] = 0x39;
  373. break;
  374. case TUNER_MICROTUNE_4049FM5:
  375. default:
  376. buffer[3] = 0xa4;
  377. break;
  378. }
  379. return 0;
  380. }
  381. /* ---------------------------------------------------------------------- */
  382. static int simple_set_tv_freq(struct dvb_frontend *fe,
  383. struct analog_parameters *params)
  384. {
  385. struct tuner_simple_priv *priv = fe->tuner_priv;
  386. u8 config, cb;
  387. u16 div;
  388. struct tunertype *tun;
  389. u8 buffer[4];
  390. int rc, IFPCoff, i;
  391. enum param_type desired_type;
  392. struct tuner_params *t_params;
  393. tun = priv->tun;
  394. /* IFPCoff = Video Intermediate Frequency - Vif:
  395. 940 =16*58.75 NTSC/J (Japan)
  396. 732 =16*45.75 M/N STD
  397. 704 =16*44 ATSC (at DVB code)
  398. 632 =16*39.50 I U.K.
  399. 622.4=16*38.90 B/G D/K I, L STD
  400. 592 =16*37.00 D China
  401. 590 =16.36.875 B Australia
  402. 543.2=16*33.95 L' STD
  403. 171.2=16*10.70 FM Radio (at set_radio_freq)
  404. */
  405. if (params->std == V4L2_STD_NTSC_M_JP) {
  406. IFPCoff = 940;
  407. desired_type = TUNER_PARAM_TYPE_NTSC;
  408. } else if ((params->std & V4L2_STD_MN) &&
  409. !(params->std & ~V4L2_STD_MN)) {
  410. IFPCoff = 732;
  411. desired_type = TUNER_PARAM_TYPE_NTSC;
  412. } else if (params->std == V4L2_STD_SECAM_LC) {
  413. IFPCoff = 543;
  414. desired_type = TUNER_PARAM_TYPE_SECAM;
  415. } else {
  416. IFPCoff = 623;
  417. desired_type = TUNER_PARAM_TYPE_PAL;
  418. }
  419. t_params = simple_tuner_params(fe, desired_type);
  420. i = simple_config_lookup(fe, t_params, &params->frequency,
  421. &config, &cb);
  422. div = params->frequency + IFPCoff + offset;
  423. tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, "
  424. "Offset=%d.%02d MHz, div=%0d\n",
  425. params->frequency / 16, params->frequency % 16 * 100 / 16,
  426. IFPCoff / 16, IFPCoff % 16 * 100 / 16,
  427. offset / 16, offset % 16 * 100 / 16, div);
  428. /* tv norm specific stuff for multi-norm tuners */
  429. simple_std_setup(fe, params, &buffer[1], &config, &cb);
  430. if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
  431. buffer[0] = config;
  432. buffer[1] = cb;
  433. buffer[2] = (div>>8) & 0x7f;
  434. buffer[3] = div & 0xff;
  435. } else {
  436. buffer[0] = (div>>8) & 0x7f;
  437. buffer[1] = div & 0xff;
  438. buffer[2] = config;
  439. buffer[3] = cb;
  440. }
  441. priv->last_div = div;
  442. if (t_params->has_tda9887) {
  443. struct v4l2_priv_tun_config tda9887_cfg;
  444. int config = 0;
  445. int is_secam_l = (params->std & (V4L2_STD_SECAM_L |
  446. V4L2_STD_SECAM_LC)) &&
  447. !(params->std & ~(V4L2_STD_SECAM_L |
  448. V4L2_STD_SECAM_LC));
  449. tda9887_cfg.tuner = TUNER_TDA9887;
  450. tda9887_cfg.priv = &config;
  451. if (params->std == V4L2_STD_SECAM_LC) {
  452. if (t_params->port1_active ^ t_params->port1_invert_for_secam_lc)
  453. config |= TDA9887_PORT1_ACTIVE;
  454. if (t_params->port2_active ^ t_params->port2_invert_for_secam_lc)
  455. config |= TDA9887_PORT2_ACTIVE;
  456. } else {
  457. if (t_params->port1_active)
  458. config |= TDA9887_PORT1_ACTIVE;
  459. if (t_params->port2_active)
  460. config |= TDA9887_PORT2_ACTIVE;
  461. }
  462. if (t_params->intercarrier_mode)
  463. config |= TDA9887_INTERCARRIER;
  464. if (is_secam_l) {
  465. if (i == 0 && t_params->default_top_secam_low)
  466. config |= TDA9887_TOP(t_params->default_top_secam_low);
  467. else if (i == 1 && t_params->default_top_secam_mid)
  468. config |= TDA9887_TOP(t_params->default_top_secam_mid);
  469. else if (t_params->default_top_secam_high)
  470. config |= TDA9887_TOP(t_params->default_top_secam_high);
  471. } else {
  472. if (i == 0 && t_params->default_top_low)
  473. config |= TDA9887_TOP(t_params->default_top_low);
  474. else if (i == 1 && t_params->default_top_mid)
  475. config |= TDA9887_TOP(t_params->default_top_mid);
  476. else if (t_params->default_top_high)
  477. config |= TDA9887_TOP(t_params->default_top_high);
  478. }
  479. if (t_params->default_pll_gating_18)
  480. config |= TDA9887_GATING_18;
  481. i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
  482. &tda9887_cfg);
  483. }
  484. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  485. buffer[0], buffer[1], buffer[2], buffer[3]);
  486. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  487. if (4 != rc)
  488. tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
  489. simple_post_tune(fe, &buffer[0], div, config, cb);
  490. return 0;
  491. }
  492. static int simple_set_radio_freq(struct dvb_frontend *fe,
  493. struct analog_parameters *params)
  494. {
  495. struct tunertype *tun;
  496. struct tuner_simple_priv *priv = fe->tuner_priv;
  497. u8 buffer[4];
  498. u16 div;
  499. int rc, j;
  500. struct tuner_params *t_params;
  501. unsigned int freq = params->frequency;
  502. tun = priv->tun;
  503. for (j = tun->count-1; j > 0; j--)
  504. if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
  505. break;
  506. /* default t_params (j=0) will be used if desired type wasn't found */
  507. t_params = &tun->params[j];
  508. /* Select Radio 1st IF used */
  509. switch (t_params->radio_if) {
  510. case 0: /* 10.7 MHz */
  511. freq += (unsigned int)(10.7*16000);
  512. break;
  513. case 1: /* 33.3 MHz */
  514. freq += (unsigned int)(33.3*16000);
  515. break;
  516. case 2: /* 41.3 MHz */
  517. freq += (unsigned int)(41.3*16000);
  518. break;
  519. default:
  520. tuner_warn("Unsupported radio_if value %d\n",
  521. t_params->radio_if);
  522. return 0;
  523. }
  524. /* Bandswitch byte */
  525. simple_radio_bandswitch(fe, &buffer[0]);
  526. buffer[2] = (t_params->ranges[0].config & ~TUNER_RATIO_MASK) |
  527. TUNER_RATIO_SELECT_50; /* 50 kHz step */
  528. /* Convert from 1/16 kHz V4L steps to 1/20 MHz (=50 kHz) PLL steps
  529. freq * (1 Mhz / 16000 V4L steps) * (20 PLL steps / 1 MHz) =
  530. freq * (1/800) */
  531. div = (freq + 400) / 800;
  532. if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
  533. buffer[0] = buffer[2];
  534. buffer[1] = buffer[3];
  535. buffer[2] = (div>>8) & 0x7f;
  536. buffer[3] = div & 0xff;
  537. } else {
  538. buffer[0] = (div>>8) & 0x7f;
  539. buffer[1] = div & 0xff;
  540. }
  541. tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
  542. buffer[0], buffer[1], buffer[2], buffer[3]);
  543. priv->last_div = div;
  544. if (t_params->has_tda9887) {
  545. int config = 0;
  546. struct v4l2_priv_tun_config tda9887_cfg;
  547. tda9887_cfg.tuner = TUNER_TDA9887;
  548. tda9887_cfg.priv = &config;
  549. if (t_params->port1_active &&
  550. !t_params->port1_fm_high_sensitivity)
  551. config |= TDA9887_PORT1_ACTIVE;
  552. if (t_params->port2_active &&
  553. !t_params->port2_fm_high_sensitivity)
  554. config |= TDA9887_PORT2_ACTIVE;
  555. if (t_params->intercarrier_mode)
  556. config |= TDA9887_INTERCARRIER;
  557. /* if (t_params->port1_set_for_fm_mono)
  558. config &= ~TDA9887_PORT1_ACTIVE;*/
  559. if (t_params->fm_gain_normal)
  560. config |= TDA9887_GAIN_NORMAL;
  561. if (t_params->radio_if == 2)
  562. config |= TDA9887_RIF_41_3;
  563. i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
  564. &tda9887_cfg);
  565. }
  566. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  567. if (4 != rc)
  568. tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
  569. return 0;
  570. }
  571. static int simple_set_params(struct dvb_frontend *fe,
  572. struct analog_parameters *params)
  573. {
  574. struct tuner_simple_priv *priv = fe->tuner_priv;
  575. int ret = -EINVAL;
  576. switch (params->mode) {
  577. case V4L2_TUNER_RADIO:
  578. ret = simple_set_radio_freq(fe, params);
  579. priv->frequency = params->frequency * 125 / 2;
  580. break;
  581. case V4L2_TUNER_ANALOG_TV:
  582. case V4L2_TUNER_DIGITAL_TV:
  583. ret = simple_set_tv_freq(fe, params);
  584. priv->frequency = params->frequency * 62500;
  585. break;
  586. }
  587. return ret;
  588. }
  589. static int simple_release(struct dvb_frontend *fe)
  590. {
  591. kfree(fe->tuner_priv);
  592. fe->tuner_priv = NULL;
  593. return 0;
  594. }
  595. static int simple_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  596. {
  597. struct tuner_simple_priv *priv = fe->tuner_priv;
  598. *frequency = priv->frequency;
  599. return 0;
  600. }
  601. static struct dvb_tuner_ops simple_tuner_ops = {
  602. .set_analog_params = simple_set_params,
  603. .release = simple_release,
  604. .get_frequency = simple_get_frequency,
  605. .get_status = simple_get_status,
  606. .get_rf_strength = simple_get_rf_strength,
  607. };
  608. struct dvb_frontend *simple_tuner_attach(struct dvb_frontend *fe,
  609. struct i2c_adapter *i2c_adap,
  610. u8 i2c_addr,
  611. unsigned int type)
  612. {
  613. struct tuner_simple_priv *priv = NULL;
  614. if (type >= tuner_count) {
  615. printk(KERN_WARNING "%s: invalid tuner type: %d (max: %d)\n",
  616. __FUNCTION__, type, tuner_count-1);
  617. return NULL;
  618. }
  619. priv = kzalloc(sizeof(struct tuner_simple_priv), GFP_KERNEL);
  620. if (priv == NULL)
  621. return NULL;
  622. fe->tuner_priv = priv;
  623. priv->i2c_props.addr = i2c_addr;
  624. priv->i2c_props.adap = i2c_adap;
  625. priv->i2c_props.name = "tuner-simple";
  626. priv->type = type;
  627. priv->tun = &tuners[type];
  628. memcpy(&fe->ops.tuner_ops, &simple_tuner_ops,
  629. sizeof(struct dvb_tuner_ops));
  630. tuner_info("type set to %d (%s)\n", priv->type, priv->tun->name);
  631. strlcpy(fe->ops.tuner_ops.info.name, priv->tun->name,
  632. sizeof(fe->ops.tuner_ops.info.name));
  633. return fe;
  634. }
  635. EXPORT_SYMBOL_GPL(simple_tuner_attach);
  636. MODULE_DESCRIPTION("Simple 4-control-bytes style tuner driver");
  637. MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
  638. MODULE_LICENSE("GPL");
  639. /*
  640. * Overrides for Emacs so that we follow Linus's tabbing style.
  641. * ---------------------------------------------------------------------------
  642. * Local variables:
  643. * c-basic-offset: 8
  644. * End:
  645. */