tuner-simple.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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 *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. {
  265. /* 0x40 -> ATSC antenna input 1 */
  266. /* 0x48 -> ATSC antenna input 2 */
  267. /* 0x00 -> NTSC antenna input 1 */
  268. /* 0x08 -> NTSC antenna input 2 */
  269. u8 buffer[4] = { 0x14, 0x00, 0x17, 0x00};
  270. *cb &= ~0x40;
  271. if (params->std & V4L2_STD_ATSC) {
  272. *cb |= 0x40;
  273. buffer[1] = 0x04;
  274. }
  275. /* set to the correct mode (analog or digital) */
  276. tuneraddr = priv->i2c_props.addr;
  277. priv->i2c_props.addr = 0x0a;
  278. rc = tuner_i2c_xfer_send(&priv->i2c_props, &buffer[0], 2);
  279. if (2 != rc)
  280. tuner_warn("i2c i/o error: rc == %d "
  281. "(should be 2)\n", rc);
  282. rc = tuner_i2c_xfer_send(&priv->i2c_props, &buffer[2], 2);
  283. if (2 != rc)
  284. tuner_warn("i2c i/o error: rc == %d "
  285. "(should be 2)\n", rc);
  286. priv->i2c_props.addr = tuneraddr;
  287. /* FIXME: input */
  288. break;
  289. }
  290. }
  291. return 0;
  292. }
  293. static int simple_post_tune(struct dvb_frontend *fe, u8 *buffer,
  294. u16 div, u8 config, u8 cb)
  295. {
  296. struct tuner_simple_priv *priv = fe->tuner_priv;
  297. int rc;
  298. switch (priv->type) {
  299. case TUNER_LG_TDVS_H06XF:
  300. /* Set the Auxiliary Byte. */
  301. buffer[0] = buffer[2];
  302. buffer[0] &= ~0x20;
  303. buffer[0] |= 0x18;
  304. buffer[1] = 0x20;
  305. tuner_dbg("tv 0x%02x 0x%02x\n", buffer[0], buffer[1]);
  306. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 2);
  307. if (2 != rc)
  308. tuner_warn("i2c i/o error: rc == %d "
  309. "(should be 2)\n", rc);
  310. break;
  311. case TUNER_MICROTUNE_4042FI5:
  312. {
  313. /* FIXME - this may also work for other tuners */
  314. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  315. u8 status_byte = 0;
  316. /* Wait until the PLL locks */
  317. for (;;) {
  318. if (time_after(jiffies, timeout))
  319. return 0;
  320. rc = tuner_i2c_xfer_recv(&priv->i2c_props,
  321. &status_byte, 1);
  322. if (1 != rc) {
  323. tuner_warn("i2c i/o read error: rc == %d "
  324. "(should be 1)\n", rc);
  325. break;
  326. }
  327. if (status_byte & TUNER_PLL_LOCKED)
  328. break;
  329. udelay(10);
  330. }
  331. /* Set the charge pump for optimized phase noise figure */
  332. config &= ~TUNER_CHARGE_PUMP;
  333. buffer[0] = (div>>8) & 0x7f;
  334. buffer[1] = div & 0xff;
  335. buffer[2] = config;
  336. buffer[3] = cb;
  337. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  338. buffer[0], buffer[1], buffer[2], buffer[3]);
  339. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  340. if (4 != rc)
  341. tuner_warn("i2c i/o error: rc == %d "
  342. "(should be 4)\n", rc);
  343. break;
  344. }
  345. }
  346. return 0;
  347. }
  348. static int simple_radio_bandswitch(struct dvb_frontend *fe, u8 *buffer)
  349. {
  350. struct tuner_simple_priv *priv = fe->tuner_priv;
  351. switch (priv->type) {
  352. case TUNER_TENA_9533_DI:
  353. case TUNER_YMEC_TVF_5533MF:
  354. tuner_dbg("This tuner doesn't have FM. "
  355. "Most cards have a TEA5767 for FM\n");
  356. return 0;
  357. case TUNER_PHILIPS_FM1216ME_MK3:
  358. case TUNER_PHILIPS_FM1236_MK3:
  359. case TUNER_PHILIPS_FMD1216ME_MK3:
  360. case TUNER_LG_NTSC_TAPE:
  361. case TUNER_PHILIPS_FM1256_IH3:
  362. buffer[3] = 0x19;
  363. break;
  364. case TUNER_TNF_5335MF:
  365. buffer[3] = 0x11;
  366. break;
  367. case TUNER_LG_PAL_FM:
  368. buffer[3] = 0xa5;
  369. break;
  370. case TUNER_THOMSON_DTT761X:
  371. buffer[3] = 0x39;
  372. break;
  373. case TUNER_MICROTUNE_4049FM5:
  374. default:
  375. buffer[3] = 0xa4;
  376. break;
  377. }
  378. return 0;
  379. }
  380. /* ---------------------------------------------------------------------- */
  381. static int simple_set_tv_freq(struct dvb_frontend *fe,
  382. struct analog_parameters *params)
  383. {
  384. struct tuner_simple_priv *priv = fe->tuner_priv;
  385. u8 config, cb;
  386. u16 div;
  387. struct tunertype *tun;
  388. u8 buffer[4];
  389. int rc, IFPCoff, i;
  390. enum param_type desired_type;
  391. struct tuner_params *t_params;
  392. tun = priv->tun;
  393. /* IFPCoff = Video Intermediate Frequency - Vif:
  394. 940 =16*58.75 NTSC/J (Japan)
  395. 732 =16*45.75 M/N STD
  396. 704 =16*44 ATSC (at DVB code)
  397. 632 =16*39.50 I U.K.
  398. 622.4=16*38.90 B/G D/K I, L STD
  399. 592 =16*37.00 D China
  400. 590 =16.36.875 B Australia
  401. 543.2=16*33.95 L' STD
  402. 171.2=16*10.70 FM Radio (at set_radio_freq)
  403. */
  404. if (params->std == V4L2_STD_NTSC_M_JP) {
  405. IFPCoff = 940;
  406. desired_type = TUNER_PARAM_TYPE_NTSC;
  407. } else if ((params->std & V4L2_STD_MN) &&
  408. !(params->std & ~V4L2_STD_MN)) {
  409. IFPCoff = 732;
  410. desired_type = TUNER_PARAM_TYPE_NTSC;
  411. } else if (params->std == V4L2_STD_SECAM_LC) {
  412. IFPCoff = 543;
  413. desired_type = TUNER_PARAM_TYPE_SECAM;
  414. } else {
  415. IFPCoff = 623;
  416. desired_type = TUNER_PARAM_TYPE_PAL;
  417. }
  418. t_params = simple_tuner_params(fe, desired_type);
  419. i = simple_config_lookup(fe, t_params, &params->frequency,
  420. &config, &cb);
  421. div = params->frequency + IFPCoff + offset;
  422. tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, "
  423. "Offset=%d.%02d MHz, div=%0d\n",
  424. params->frequency / 16, params->frequency % 16 * 100 / 16,
  425. IFPCoff / 16, IFPCoff % 16 * 100 / 16,
  426. offset / 16, offset % 16 * 100 / 16, div);
  427. /* tv norm specific stuff for multi-norm tuners */
  428. simple_std_setup(fe, params, &config, &cb);
  429. if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
  430. buffer[0] = config;
  431. buffer[1] = cb;
  432. buffer[2] = (div>>8) & 0x7f;
  433. buffer[3] = div & 0xff;
  434. } else {
  435. buffer[0] = (div>>8) & 0x7f;
  436. buffer[1] = div & 0xff;
  437. buffer[2] = config;
  438. buffer[3] = cb;
  439. }
  440. priv->last_div = div;
  441. if (t_params->has_tda9887) {
  442. struct v4l2_priv_tun_config tda9887_cfg;
  443. int config = 0;
  444. int is_secam_l = (params->std & (V4L2_STD_SECAM_L |
  445. V4L2_STD_SECAM_LC)) &&
  446. !(params->std & ~(V4L2_STD_SECAM_L |
  447. V4L2_STD_SECAM_LC));
  448. tda9887_cfg.tuner = TUNER_TDA9887;
  449. tda9887_cfg.priv = &config;
  450. if (params->std == V4L2_STD_SECAM_LC) {
  451. if (t_params->port1_active ^ t_params->port1_invert_for_secam_lc)
  452. config |= TDA9887_PORT1_ACTIVE;
  453. if (t_params->port2_active ^ t_params->port2_invert_for_secam_lc)
  454. config |= TDA9887_PORT2_ACTIVE;
  455. } else {
  456. if (t_params->port1_active)
  457. config |= TDA9887_PORT1_ACTIVE;
  458. if (t_params->port2_active)
  459. config |= TDA9887_PORT2_ACTIVE;
  460. }
  461. if (t_params->intercarrier_mode)
  462. config |= TDA9887_INTERCARRIER;
  463. if (is_secam_l) {
  464. if (i == 0 && t_params->default_top_secam_low)
  465. config |= TDA9887_TOP(t_params->default_top_secam_low);
  466. else if (i == 1 && t_params->default_top_secam_mid)
  467. config |= TDA9887_TOP(t_params->default_top_secam_mid);
  468. else if (t_params->default_top_secam_high)
  469. config |= TDA9887_TOP(t_params->default_top_secam_high);
  470. } else {
  471. if (i == 0 && t_params->default_top_low)
  472. config |= TDA9887_TOP(t_params->default_top_low);
  473. else if (i == 1 && t_params->default_top_mid)
  474. config |= TDA9887_TOP(t_params->default_top_mid);
  475. else if (t_params->default_top_high)
  476. config |= TDA9887_TOP(t_params->default_top_high);
  477. }
  478. if (t_params->default_pll_gating_18)
  479. config |= TDA9887_GATING_18;
  480. i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
  481. &tda9887_cfg);
  482. }
  483. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  484. buffer[0], buffer[1], buffer[2], buffer[3]);
  485. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  486. if (4 != rc)
  487. tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
  488. simple_post_tune(fe, &buffer[0], div, config, cb);
  489. return 0;
  490. }
  491. static int simple_set_radio_freq(struct dvb_frontend *fe,
  492. struct analog_parameters *params)
  493. {
  494. struct tunertype *tun;
  495. struct tuner_simple_priv *priv = fe->tuner_priv;
  496. u8 buffer[4];
  497. u16 div;
  498. int rc, j;
  499. struct tuner_params *t_params;
  500. unsigned int freq = params->frequency;
  501. tun = priv->tun;
  502. for (j = tun->count-1; j > 0; j--)
  503. if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
  504. break;
  505. /* default t_params (j=0) will be used if desired type wasn't found */
  506. t_params = &tun->params[j];
  507. /* Select Radio 1st IF used */
  508. switch (t_params->radio_if) {
  509. case 0: /* 10.7 MHz */
  510. freq += (unsigned int)(10.7*16000);
  511. break;
  512. case 1: /* 33.3 MHz */
  513. freq += (unsigned int)(33.3*16000);
  514. break;
  515. case 2: /* 41.3 MHz */
  516. freq += (unsigned int)(41.3*16000);
  517. break;
  518. default:
  519. tuner_warn("Unsupported radio_if value %d\n",
  520. t_params->radio_if);
  521. return 0;
  522. }
  523. /* Bandswitch byte */
  524. simple_radio_bandswitch(fe, &buffer[0]);
  525. buffer[2] = (t_params->ranges[0].config & ~TUNER_RATIO_MASK) |
  526. TUNER_RATIO_SELECT_50; /* 50 kHz step */
  527. /* Convert from 1/16 kHz V4L steps to 1/20 MHz (=50 kHz) PLL steps
  528. freq * (1 Mhz / 16000 V4L steps) * (20 PLL steps / 1 MHz) =
  529. freq * (1/800) */
  530. div = (freq + 400) / 800;
  531. if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
  532. buffer[0] = buffer[2];
  533. buffer[1] = buffer[3];
  534. buffer[2] = (div>>8) & 0x7f;
  535. buffer[3] = div & 0xff;
  536. } else {
  537. buffer[0] = (div>>8) & 0x7f;
  538. buffer[1] = div & 0xff;
  539. }
  540. tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
  541. buffer[0], buffer[1], buffer[2], buffer[3]);
  542. priv->last_div = div;
  543. if (t_params->has_tda9887) {
  544. int config = 0;
  545. struct v4l2_priv_tun_config tda9887_cfg;
  546. tda9887_cfg.tuner = TUNER_TDA9887;
  547. tda9887_cfg.priv = &config;
  548. if (t_params->port1_active &&
  549. !t_params->port1_fm_high_sensitivity)
  550. config |= TDA9887_PORT1_ACTIVE;
  551. if (t_params->port2_active &&
  552. !t_params->port2_fm_high_sensitivity)
  553. config |= TDA9887_PORT2_ACTIVE;
  554. if (t_params->intercarrier_mode)
  555. config |= TDA9887_INTERCARRIER;
  556. /* if (t_params->port1_set_for_fm_mono)
  557. config &= ~TDA9887_PORT1_ACTIVE;*/
  558. if (t_params->fm_gain_normal)
  559. config |= TDA9887_GAIN_NORMAL;
  560. if (t_params->radio_if == 2)
  561. config |= TDA9887_RIF_41_3;
  562. i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
  563. &tda9887_cfg);
  564. }
  565. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  566. if (4 != rc)
  567. tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
  568. return 0;
  569. }
  570. static int simple_set_params(struct dvb_frontend *fe,
  571. struct analog_parameters *params)
  572. {
  573. struct tuner_simple_priv *priv = fe->tuner_priv;
  574. int ret = -EINVAL;
  575. switch (params->mode) {
  576. case V4L2_TUNER_RADIO:
  577. ret = simple_set_radio_freq(fe, params);
  578. priv->frequency = params->frequency * 125 / 2;
  579. break;
  580. case V4L2_TUNER_ANALOG_TV:
  581. case V4L2_TUNER_DIGITAL_TV:
  582. ret = simple_set_tv_freq(fe, params);
  583. priv->frequency = params->frequency * 62500;
  584. break;
  585. }
  586. return ret;
  587. }
  588. static int simple_release(struct dvb_frontend *fe)
  589. {
  590. kfree(fe->tuner_priv);
  591. fe->tuner_priv = NULL;
  592. return 0;
  593. }
  594. static int simple_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  595. {
  596. struct tuner_simple_priv *priv = fe->tuner_priv;
  597. *frequency = priv->frequency;
  598. return 0;
  599. }
  600. static struct dvb_tuner_ops simple_tuner_ops = {
  601. .set_analog_params = simple_set_params,
  602. .release = simple_release,
  603. .get_frequency = simple_get_frequency,
  604. .get_status = simple_get_status,
  605. .get_rf_strength = simple_get_rf_strength,
  606. };
  607. struct dvb_frontend *simple_tuner_attach(struct dvb_frontend *fe,
  608. struct i2c_adapter *i2c_adap,
  609. u8 i2c_addr,
  610. unsigned int type)
  611. {
  612. struct tuner_simple_priv *priv = NULL;
  613. if (type >= tuner_count) {
  614. printk(KERN_WARNING "%s: invalid tuner type: %d (max: %d)\n",
  615. __FUNCTION__, type, tuner_count-1);
  616. return NULL;
  617. }
  618. priv = kzalloc(sizeof(struct tuner_simple_priv), GFP_KERNEL);
  619. if (priv == NULL)
  620. return NULL;
  621. fe->tuner_priv = priv;
  622. priv->i2c_props.addr = i2c_addr;
  623. priv->i2c_props.adap = i2c_adap;
  624. priv->i2c_props.name = "tuner-simple";
  625. priv->type = type;
  626. priv->tun = &tuners[type];
  627. memcpy(&fe->ops.tuner_ops, &simple_tuner_ops,
  628. sizeof(struct dvb_tuner_ops));
  629. tuner_info("type set to %d (%s)\n", priv->type, priv->tun->name);
  630. strlcpy(fe->ops.tuner_ops.info.name, priv->tun->name,
  631. sizeof(fe->ops.tuner_ops.info.name));
  632. return fe;
  633. }
  634. EXPORT_SYMBOL_GPL(simple_tuner_attach);
  635. MODULE_DESCRIPTION("Simple 4-control-bytes style tuner driver");
  636. MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
  637. MODULE_LICENSE("GPL");
  638. /*
  639. * Overrides for Emacs so that we follow Linus's tabbing style.
  640. * ---------------------------------------------------------------------------
  641. * Local variables:
  642. * c-basic-offset: 8
  643. * End:
  644. */