tuner-simple.c 19 KB

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