tuner-simple.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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/videodev2.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 TUNER_SIMPLE_MAX 64
  19. static unsigned int simple_devcount;
  20. static int offset;
  21. module_param(offset, int, 0664);
  22. MODULE_PARM_DESC(offset, "Allows to specify an offset for tuner");
  23. static unsigned int atv_input[TUNER_SIMPLE_MAX] = \
  24. { [0 ... (TUNER_SIMPLE_MAX-1)] = 0 };
  25. static unsigned int dtv_input[TUNER_SIMPLE_MAX] = \
  26. { [0 ... (TUNER_SIMPLE_MAX-1)] = 0 };
  27. module_param_array(atv_input, int, NULL, 0644);
  28. module_param_array(dtv_input, int, NULL, 0644);
  29. MODULE_PARM_DESC(atv_input, "specify atv rf input, 0 for autoselect");
  30. MODULE_PARM_DESC(dtv_input, "specify dtv rf input, 0 for autoselect");
  31. /* ---------------------------------------------------------------------- */
  32. /* tv standard selection for Temic 4046 FM5
  33. this value takes the low bits of control byte 2
  34. from datasheet Rev.01, Feb.00
  35. standard BG I L L2 D
  36. picture IF 38.9 38.9 38.9 33.95 38.9
  37. sound 1 33.4 32.9 32.4 40.45 32.4
  38. sound 2 33.16
  39. NICAM 33.05 32.348 33.05 33.05
  40. */
  41. #define TEMIC_SET_PAL_I 0x05
  42. #define TEMIC_SET_PAL_DK 0x09
  43. #define TEMIC_SET_PAL_L 0x0a /* SECAM ? */
  44. #define TEMIC_SET_PAL_L2 0x0b /* change IF ! */
  45. #define TEMIC_SET_PAL_BG 0x0c
  46. /* tv tuner system standard selection for Philips FQ1216ME
  47. this value takes the low bits of control byte 2
  48. from datasheet "1999 Nov 16" (supersedes "1999 Mar 23")
  49. standard BG DK I L L`
  50. picture carrier 38.90 38.90 38.90 38.90 33.95
  51. colour 34.47 34.47 34.47 34.47 38.38
  52. sound 1 33.40 32.40 32.90 32.40 40.45
  53. sound 2 33.16 - - - -
  54. NICAM 33.05 33.05 32.35 33.05 39.80
  55. */
  56. #define PHILIPS_SET_PAL_I 0x01 /* Bit 2 always zero !*/
  57. #define PHILIPS_SET_PAL_BGDK 0x09
  58. #define PHILIPS_SET_PAL_L2 0x0a
  59. #define PHILIPS_SET_PAL_L 0x0b
  60. /* system switching for Philips FI1216MF MK2
  61. from datasheet "1996 Jul 09",
  62. standard BG L L'
  63. picture carrier 38.90 38.90 33.95
  64. colour 34.47 34.37 38.38
  65. sound 1 33.40 32.40 40.45
  66. sound 2 33.16 - -
  67. NICAM 33.05 33.05 39.80
  68. */
  69. #define PHILIPS_MF_SET_STD_BG 0x01 /* Bit 2 must be zero, Bit 3 is system output */
  70. #define PHILIPS_MF_SET_STD_L 0x03 /* Used on Secam France */
  71. #define PHILIPS_MF_SET_STD_LC 0x02 /* Used on SECAM L' */
  72. /* Control byte */
  73. #define TUNER_RATIO_MASK 0x06 /* Bit cb1:cb2 */
  74. #define TUNER_RATIO_SELECT_50 0x00
  75. #define TUNER_RATIO_SELECT_32 0x02
  76. #define TUNER_RATIO_SELECT_166 0x04
  77. #define TUNER_RATIO_SELECT_62 0x06
  78. #define TUNER_CHARGE_PUMP 0x40 /* Bit cb6 */
  79. /* Status byte */
  80. #define TUNER_POR 0x80
  81. #define TUNER_FL 0x40
  82. #define TUNER_MODE 0x38
  83. #define TUNER_AFC 0x07
  84. #define TUNER_SIGNAL 0x07
  85. #define TUNER_STEREO 0x10
  86. #define TUNER_PLL_LOCKED 0x40
  87. #define TUNER_STEREO_MK3 0x04
  88. static DEFINE_MUTEX(tuner_simple_list_mutex);
  89. static LIST_HEAD(hybrid_tuner_instance_list);
  90. struct tuner_simple_priv {
  91. unsigned int nr;
  92. u16 last_div;
  93. struct tuner_i2c_props i2c_props;
  94. struct list_head hybrid_tuner_instance_list;
  95. unsigned int type;
  96. struct tunertype *tun;
  97. u32 frequency;
  98. u32 bandwidth;
  99. };
  100. /* ---------------------------------------------------------------------- */
  101. static int tuner_read_status(struct dvb_frontend *fe)
  102. {
  103. struct tuner_simple_priv *priv = fe->tuner_priv;
  104. unsigned char byte;
  105. if (1 != tuner_i2c_xfer_recv(&priv->i2c_props, &byte, 1))
  106. return 0;
  107. return byte;
  108. }
  109. static inline int tuner_signal(const int status)
  110. {
  111. return (status & TUNER_SIGNAL) << 13;
  112. }
  113. static inline int tuner_stereo(const int type, const int status)
  114. {
  115. switch (type) {
  116. case TUNER_PHILIPS_FM1216ME_MK3:
  117. case TUNER_PHILIPS_FM1236_MK3:
  118. case TUNER_PHILIPS_FM1256_IH3:
  119. case TUNER_LG_NTSC_TAPE:
  120. case TUNER_TCL_MF02GIP_5N:
  121. return ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
  122. default:
  123. return status & TUNER_STEREO;
  124. }
  125. }
  126. static inline int tuner_islocked(const int status)
  127. {
  128. return (status & TUNER_FL);
  129. }
  130. static inline int tuner_afcstatus(const int status)
  131. {
  132. return (status & TUNER_AFC) - 2;
  133. }
  134. static int simple_get_status(struct dvb_frontend *fe, u32 *status)
  135. {
  136. struct tuner_simple_priv *priv = fe->tuner_priv;
  137. int tuner_status;
  138. if (priv->i2c_props.adap == NULL)
  139. return -EINVAL;
  140. tuner_status = tuner_read_status(fe);
  141. *status = 0;
  142. if (tuner_islocked(tuner_status))
  143. *status = TUNER_STATUS_LOCKED;
  144. if (tuner_stereo(priv->type, tuner_status))
  145. *status |= TUNER_STATUS_STEREO;
  146. tuner_dbg("AFC Status: %d\n", tuner_afcstatus(tuner_status));
  147. return 0;
  148. }
  149. static int simple_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
  150. {
  151. struct tuner_simple_priv *priv = fe->tuner_priv;
  152. int signal;
  153. if (priv->i2c_props.adap == NULL)
  154. return -EINVAL;
  155. signal = tuner_signal(tuner_read_status(fe));
  156. *strength = signal;
  157. tuner_dbg("Signal strength: %d\n", signal);
  158. return 0;
  159. }
  160. /* ---------------------------------------------------------------------- */
  161. static inline char *tuner_param_name(enum param_type type)
  162. {
  163. char *name;
  164. switch (type) {
  165. case TUNER_PARAM_TYPE_RADIO:
  166. name = "radio";
  167. break;
  168. case TUNER_PARAM_TYPE_PAL:
  169. name = "pal";
  170. break;
  171. case TUNER_PARAM_TYPE_SECAM:
  172. name = "secam";
  173. break;
  174. case TUNER_PARAM_TYPE_NTSC:
  175. name = "ntsc";
  176. break;
  177. case TUNER_PARAM_TYPE_DIGITAL:
  178. name = "digital";
  179. break;
  180. default:
  181. name = "unknown";
  182. break;
  183. }
  184. return name;
  185. }
  186. static struct tuner_params *simple_tuner_params(struct dvb_frontend *fe,
  187. enum param_type desired_type)
  188. {
  189. struct tuner_simple_priv *priv = fe->tuner_priv;
  190. struct tunertype *tun = priv->tun;
  191. int i;
  192. for (i = 0; i < tun->count; i++)
  193. if (desired_type == tun->params[i].type)
  194. break;
  195. /* use default tuner params if desired_type not available */
  196. if (i == tun->count) {
  197. tuner_dbg("desired params (%s) undefined for tuner %d\n",
  198. tuner_param_name(desired_type), priv->type);
  199. i = 0;
  200. }
  201. tuner_dbg("using tuner params #%d (%s)\n", i,
  202. tuner_param_name(tun->params[i].type));
  203. return &tun->params[i];
  204. }
  205. static int simple_config_lookup(struct dvb_frontend *fe,
  206. struct tuner_params *t_params,
  207. unsigned *frequency, u8 *config, u8 *cb)
  208. {
  209. struct tuner_simple_priv *priv = fe->tuner_priv;
  210. int i;
  211. for (i = 0; i < t_params->count; i++) {
  212. if (*frequency > t_params->ranges[i].limit)
  213. continue;
  214. break;
  215. }
  216. if (i == t_params->count) {
  217. tuner_dbg("frequency out of range (%d > %d)\n",
  218. *frequency, t_params->ranges[i - 1].limit);
  219. *frequency = t_params->ranges[--i].limit;
  220. }
  221. *config = t_params->ranges[i].config;
  222. *cb = t_params->ranges[i].cb;
  223. tuner_dbg("freq = %d.%02d (%d), range = %d, "
  224. "config = 0x%02x, cb = 0x%02x\n",
  225. *frequency / 16, *frequency % 16 * 100 / 16, *frequency,
  226. i, *config, *cb);
  227. return i;
  228. }
  229. /* ---------------------------------------------------------------------- */
  230. static void simple_set_rf_input(struct dvb_frontend *fe,
  231. u8 *config, u8 *cb, unsigned int rf)
  232. {
  233. struct tuner_simple_priv *priv = fe->tuner_priv;
  234. switch (priv->type) {
  235. case TUNER_PHILIPS_TUV1236D:
  236. switch (rf) {
  237. case 1:
  238. *cb |= 0x08;
  239. break;
  240. default:
  241. *cb &= ~0x08;
  242. break;
  243. }
  244. break;
  245. case TUNER_PHILIPS_FCV1236D:
  246. switch (rf) {
  247. case 1:
  248. *cb |= 0x01;
  249. break;
  250. default:
  251. *cb &= ~0x01;
  252. break;
  253. }
  254. break;
  255. default:
  256. break;
  257. }
  258. }
  259. static int simple_std_setup(struct dvb_frontend *fe,
  260. struct analog_parameters *params,
  261. u8 *config, u8 *cb)
  262. {
  263. struct tuner_simple_priv *priv = fe->tuner_priv;
  264. u8 tuneraddr;
  265. int rc;
  266. /* tv norm specific stuff for multi-norm tuners */
  267. switch (priv->type) {
  268. case TUNER_PHILIPS_SECAM: /* FI1216MF */
  269. /* 0x01 -> ??? no change ??? */
  270. /* 0x02 -> PAL BDGHI / SECAM L */
  271. /* 0x04 -> ??? PAL others / SECAM others ??? */
  272. *cb &= ~0x03;
  273. if (params->std & V4L2_STD_SECAM_L)
  274. /* also valid for V4L2_STD_SECAM */
  275. *cb |= PHILIPS_MF_SET_STD_L;
  276. else if (params->std & V4L2_STD_SECAM_LC)
  277. *cb |= PHILIPS_MF_SET_STD_LC;
  278. else /* V4L2_STD_B|V4L2_STD_GH */
  279. *cb |= PHILIPS_MF_SET_STD_BG;
  280. break;
  281. case TUNER_TEMIC_4046FM5:
  282. *cb &= ~0x0f;
  283. if (params->std & V4L2_STD_PAL_BG) {
  284. *cb |= TEMIC_SET_PAL_BG;
  285. } else if (params->std & V4L2_STD_PAL_I) {
  286. *cb |= TEMIC_SET_PAL_I;
  287. } else if (params->std & V4L2_STD_PAL_DK) {
  288. *cb |= TEMIC_SET_PAL_DK;
  289. } else if (params->std & V4L2_STD_SECAM_L) {
  290. *cb |= TEMIC_SET_PAL_L;
  291. }
  292. break;
  293. case TUNER_PHILIPS_FQ1216ME:
  294. *cb &= ~0x0f;
  295. if (params->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
  296. *cb |= PHILIPS_SET_PAL_BGDK;
  297. } else if (params->std & V4L2_STD_PAL_I) {
  298. *cb |= PHILIPS_SET_PAL_I;
  299. } else if (params->std & V4L2_STD_SECAM_L) {
  300. *cb |= PHILIPS_SET_PAL_L;
  301. }
  302. break;
  303. case TUNER_PHILIPS_FCV1236D:
  304. /* 0x00 -> ATSC antenna input 1 */
  305. /* 0x01 -> ATSC antenna input 2 */
  306. /* 0x02 -> NTSC antenna input 1 */
  307. /* 0x03 -> NTSC antenna input 2 */
  308. *cb &= ~0x03;
  309. if (!(params->std & V4L2_STD_ATSC))
  310. *cb |= 2;
  311. break;
  312. case TUNER_MICROTUNE_4042FI5:
  313. /* Set the charge pump for fast tuning */
  314. *config |= TUNER_CHARGE_PUMP;
  315. break;
  316. case TUNER_PHILIPS_TUV1236D:
  317. {
  318. /* 0x40 -> ATSC antenna input 1 */
  319. /* 0x48 -> ATSC antenna input 2 */
  320. /* 0x00 -> NTSC antenna input 1 */
  321. /* 0x08 -> NTSC antenna input 2 */
  322. u8 buffer[4] = { 0x14, 0x00, 0x17, 0x00};
  323. *cb &= ~0x40;
  324. if (params->std & V4L2_STD_ATSC) {
  325. *cb |= 0x40;
  326. buffer[1] = 0x04;
  327. }
  328. /* set to the correct mode (analog or digital) */
  329. tuneraddr = priv->i2c_props.addr;
  330. priv->i2c_props.addr = 0x0a;
  331. rc = tuner_i2c_xfer_send(&priv->i2c_props, &buffer[0], 2);
  332. if (2 != rc)
  333. tuner_warn("i2c i/o error: rc == %d "
  334. "(should be 2)\n", rc);
  335. rc = tuner_i2c_xfer_send(&priv->i2c_props, &buffer[2], 2);
  336. if (2 != rc)
  337. tuner_warn("i2c i/o error: rc == %d "
  338. "(should be 2)\n", rc);
  339. priv->i2c_props.addr = tuneraddr;
  340. break;
  341. }
  342. }
  343. if (atv_input[priv->nr])
  344. simple_set_rf_input(fe, config, cb, atv_input[priv->nr]);
  345. return 0;
  346. }
  347. static int simple_post_tune(struct dvb_frontend *fe, u8 *buffer,
  348. u16 div, u8 config, u8 cb)
  349. {
  350. struct tuner_simple_priv *priv = fe->tuner_priv;
  351. int rc;
  352. switch (priv->type) {
  353. case TUNER_LG_TDVS_H06XF:
  354. /* Set the Auxiliary Byte. */
  355. buffer[0] = buffer[2];
  356. buffer[0] &= ~0x20;
  357. buffer[0] |= 0x18;
  358. buffer[1] = 0x20;
  359. tuner_dbg("tv 0x%02x 0x%02x\n", buffer[0], buffer[1]);
  360. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 2);
  361. if (2 != rc)
  362. tuner_warn("i2c i/o error: rc == %d "
  363. "(should be 2)\n", rc);
  364. break;
  365. case TUNER_MICROTUNE_4042FI5:
  366. {
  367. /* FIXME - this may also work for other tuners */
  368. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  369. u8 status_byte = 0;
  370. /* Wait until the PLL locks */
  371. for (;;) {
  372. if (time_after(jiffies, timeout))
  373. return 0;
  374. rc = tuner_i2c_xfer_recv(&priv->i2c_props,
  375. &status_byte, 1);
  376. if (1 != rc) {
  377. tuner_warn("i2c i/o read error: rc == %d "
  378. "(should be 1)\n", rc);
  379. break;
  380. }
  381. if (status_byte & TUNER_PLL_LOCKED)
  382. break;
  383. udelay(10);
  384. }
  385. /* Set the charge pump for optimized phase noise figure */
  386. config &= ~TUNER_CHARGE_PUMP;
  387. buffer[0] = (div>>8) & 0x7f;
  388. buffer[1] = div & 0xff;
  389. buffer[2] = config;
  390. buffer[3] = cb;
  391. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  392. buffer[0], buffer[1], buffer[2], buffer[3]);
  393. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  394. if (4 != rc)
  395. tuner_warn("i2c i/o error: rc == %d "
  396. "(should be 4)\n", rc);
  397. break;
  398. }
  399. }
  400. return 0;
  401. }
  402. static int simple_radio_bandswitch(struct dvb_frontend *fe, u8 *buffer)
  403. {
  404. struct tuner_simple_priv *priv = fe->tuner_priv;
  405. switch (priv->type) {
  406. case TUNER_TENA_9533_DI:
  407. case TUNER_YMEC_TVF_5533MF:
  408. tuner_dbg("This tuner doesn't have FM. "
  409. "Most cards have a TEA5767 for FM\n");
  410. return 0;
  411. case TUNER_PHILIPS_FM1216ME_MK3:
  412. case TUNER_PHILIPS_FM1236_MK3:
  413. case TUNER_PHILIPS_FMD1216ME_MK3:
  414. case TUNER_PHILIPS_FMD1216MEX_MK3:
  415. case TUNER_LG_NTSC_TAPE:
  416. case TUNER_PHILIPS_FM1256_IH3:
  417. case TUNER_TCL_MF02GIP_5N:
  418. buffer[3] = 0x19;
  419. break;
  420. case TUNER_TNF_5335MF:
  421. buffer[3] = 0x11;
  422. break;
  423. case TUNER_LG_PAL_FM:
  424. buffer[3] = 0xa5;
  425. break;
  426. case TUNER_THOMSON_DTT761X:
  427. buffer[3] = 0x39;
  428. break;
  429. case TUNER_MICROTUNE_4049FM5:
  430. default:
  431. buffer[3] = 0xa4;
  432. break;
  433. }
  434. return 0;
  435. }
  436. /* ---------------------------------------------------------------------- */
  437. static int simple_set_tv_freq(struct dvb_frontend *fe,
  438. struct analog_parameters *params)
  439. {
  440. struct tuner_simple_priv *priv = fe->tuner_priv;
  441. u8 config, cb;
  442. u16 div;
  443. struct tunertype *tun;
  444. u8 buffer[4];
  445. int rc, IFPCoff, i;
  446. enum param_type desired_type;
  447. struct tuner_params *t_params;
  448. tun = priv->tun;
  449. /* IFPCoff = Video Intermediate Frequency - Vif:
  450. 940 =16*58.75 NTSC/J (Japan)
  451. 732 =16*45.75 M/N STD
  452. 704 =16*44 ATSC (at DVB code)
  453. 632 =16*39.50 I U.K.
  454. 622.4=16*38.90 B/G D/K I, L STD
  455. 592 =16*37.00 D China
  456. 590 =16.36.875 B Australia
  457. 543.2=16*33.95 L' STD
  458. 171.2=16*10.70 FM Radio (at set_radio_freq)
  459. */
  460. if (params->std == V4L2_STD_NTSC_M_JP) {
  461. IFPCoff = 940;
  462. desired_type = TUNER_PARAM_TYPE_NTSC;
  463. } else if ((params->std & V4L2_STD_MN) &&
  464. !(params->std & ~V4L2_STD_MN)) {
  465. IFPCoff = 732;
  466. desired_type = TUNER_PARAM_TYPE_NTSC;
  467. } else if (params->std == V4L2_STD_SECAM_LC) {
  468. IFPCoff = 543;
  469. desired_type = TUNER_PARAM_TYPE_SECAM;
  470. } else {
  471. IFPCoff = 623;
  472. desired_type = TUNER_PARAM_TYPE_PAL;
  473. }
  474. t_params = simple_tuner_params(fe, desired_type);
  475. i = simple_config_lookup(fe, t_params, &params->frequency,
  476. &config, &cb);
  477. div = params->frequency + IFPCoff + offset;
  478. tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, "
  479. "Offset=%d.%02d MHz, div=%0d\n",
  480. params->frequency / 16, params->frequency % 16 * 100 / 16,
  481. IFPCoff / 16, IFPCoff % 16 * 100 / 16,
  482. offset / 16, offset % 16 * 100 / 16, div);
  483. /* tv norm specific stuff for multi-norm tuners */
  484. simple_std_setup(fe, params, &config, &cb);
  485. if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
  486. buffer[0] = config;
  487. buffer[1] = cb;
  488. buffer[2] = (div>>8) & 0x7f;
  489. buffer[3] = div & 0xff;
  490. } else {
  491. buffer[0] = (div>>8) & 0x7f;
  492. buffer[1] = div & 0xff;
  493. buffer[2] = config;
  494. buffer[3] = cb;
  495. }
  496. priv->last_div = div;
  497. if (t_params->has_tda9887) {
  498. struct v4l2_priv_tun_config tda9887_cfg;
  499. int tda_config = 0;
  500. int is_secam_l = (params->std & (V4L2_STD_SECAM_L |
  501. V4L2_STD_SECAM_LC)) &&
  502. !(params->std & ~(V4L2_STD_SECAM_L |
  503. V4L2_STD_SECAM_LC));
  504. tda9887_cfg.tuner = TUNER_TDA9887;
  505. tda9887_cfg.priv = &tda_config;
  506. if (params->std == V4L2_STD_SECAM_LC) {
  507. if (t_params->port1_active ^ t_params->port1_invert_for_secam_lc)
  508. tda_config |= TDA9887_PORT1_ACTIVE;
  509. if (t_params->port2_active ^ t_params->port2_invert_for_secam_lc)
  510. tda_config |= TDA9887_PORT2_ACTIVE;
  511. } else {
  512. if (t_params->port1_active)
  513. tda_config |= TDA9887_PORT1_ACTIVE;
  514. if (t_params->port2_active)
  515. tda_config |= TDA9887_PORT2_ACTIVE;
  516. }
  517. if (t_params->intercarrier_mode)
  518. tda_config |= TDA9887_INTERCARRIER;
  519. if (is_secam_l) {
  520. if (i == 0 && t_params->default_top_secam_low)
  521. tda_config |= TDA9887_TOP(t_params->default_top_secam_low);
  522. else if (i == 1 && t_params->default_top_secam_mid)
  523. tda_config |= TDA9887_TOP(t_params->default_top_secam_mid);
  524. else if (t_params->default_top_secam_high)
  525. tda_config |= TDA9887_TOP(t_params->default_top_secam_high);
  526. } else {
  527. if (i == 0 && t_params->default_top_low)
  528. tda_config |= TDA9887_TOP(t_params->default_top_low);
  529. else if (i == 1 && t_params->default_top_mid)
  530. tda_config |= TDA9887_TOP(t_params->default_top_mid);
  531. else if (t_params->default_top_high)
  532. tda_config |= TDA9887_TOP(t_params->default_top_high);
  533. }
  534. if (t_params->default_pll_gating_18)
  535. tda_config |= TDA9887_GATING_18;
  536. i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
  537. &tda9887_cfg);
  538. }
  539. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  540. buffer[0], buffer[1], buffer[2], buffer[3]);
  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. simple_post_tune(fe, &buffer[0], div, config, cb);
  545. return 0;
  546. }
  547. static int simple_set_radio_freq(struct dvb_frontend *fe,
  548. struct analog_parameters *params)
  549. {
  550. struct tunertype *tun;
  551. struct tuner_simple_priv *priv = fe->tuner_priv;
  552. u8 buffer[4];
  553. u16 div;
  554. int rc, j;
  555. struct tuner_params *t_params;
  556. unsigned int freq = params->frequency;
  557. tun = priv->tun;
  558. for (j = tun->count-1; j > 0; j--)
  559. if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
  560. break;
  561. /* default t_params (j=0) will be used if desired type wasn't found */
  562. t_params = &tun->params[j];
  563. /* Select Radio 1st IF used */
  564. switch (t_params->radio_if) {
  565. case 0: /* 10.7 MHz */
  566. freq += (unsigned int)(10.7*16000);
  567. break;
  568. case 1: /* 33.3 MHz */
  569. freq += (unsigned int)(33.3*16000);
  570. break;
  571. case 2: /* 41.3 MHz */
  572. freq += (unsigned int)(41.3*16000);
  573. break;
  574. default:
  575. tuner_warn("Unsupported radio_if value %d\n",
  576. t_params->radio_if);
  577. return 0;
  578. }
  579. /* Bandswitch byte */
  580. simple_radio_bandswitch(fe, &buffer[0]);
  581. buffer[2] = (t_params->ranges[0].config & ~TUNER_RATIO_MASK) |
  582. TUNER_RATIO_SELECT_50; /* 50 kHz step */
  583. /* Convert from 1/16 kHz V4L steps to 1/20 MHz (=50 kHz) PLL steps
  584. freq * (1 Mhz / 16000 V4L steps) * (20 PLL steps / 1 MHz) =
  585. freq * (1/800) */
  586. div = (freq + 400) / 800;
  587. if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
  588. buffer[0] = buffer[2];
  589. buffer[1] = buffer[3];
  590. buffer[2] = (div>>8) & 0x7f;
  591. buffer[3] = div & 0xff;
  592. } else {
  593. buffer[0] = (div>>8) & 0x7f;
  594. buffer[1] = div & 0xff;
  595. }
  596. tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
  597. buffer[0], buffer[1], buffer[2], buffer[3]);
  598. priv->last_div = div;
  599. if (t_params->has_tda9887) {
  600. int config = 0;
  601. struct v4l2_priv_tun_config tda9887_cfg;
  602. tda9887_cfg.tuner = TUNER_TDA9887;
  603. tda9887_cfg.priv = &config;
  604. if (t_params->port1_active &&
  605. !t_params->port1_fm_high_sensitivity)
  606. config |= TDA9887_PORT1_ACTIVE;
  607. if (t_params->port2_active &&
  608. !t_params->port2_fm_high_sensitivity)
  609. config |= TDA9887_PORT2_ACTIVE;
  610. if (t_params->intercarrier_mode)
  611. config |= TDA9887_INTERCARRIER;
  612. /* if (t_params->port1_set_for_fm_mono)
  613. config &= ~TDA9887_PORT1_ACTIVE;*/
  614. if (t_params->fm_gain_normal)
  615. config |= TDA9887_GAIN_NORMAL;
  616. if (t_params->radio_if == 2)
  617. config |= TDA9887_RIF_41_3;
  618. i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
  619. &tda9887_cfg);
  620. }
  621. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  622. if (4 != rc)
  623. tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
  624. return 0;
  625. }
  626. static int simple_set_params(struct dvb_frontend *fe,
  627. struct analog_parameters *params)
  628. {
  629. struct tuner_simple_priv *priv = fe->tuner_priv;
  630. int ret = -EINVAL;
  631. if (priv->i2c_props.adap == NULL)
  632. return -EINVAL;
  633. switch (params->mode) {
  634. case V4L2_TUNER_RADIO:
  635. ret = simple_set_radio_freq(fe, params);
  636. priv->frequency = params->frequency * 125 / 2;
  637. break;
  638. case V4L2_TUNER_ANALOG_TV:
  639. case V4L2_TUNER_DIGITAL_TV:
  640. ret = simple_set_tv_freq(fe, params);
  641. priv->frequency = params->frequency * 62500;
  642. break;
  643. }
  644. priv->bandwidth = 0;
  645. return ret;
  646. }
  647. static void simple_set_dvb(struct dvb_frontend *fe, u8 *buf,
  648. const struct dvb_frontend_parameters *params)
  649. {
  650. struct tuner_simple_priv *priv = fe->tuner_priv;
  651. switch (priv->type) {
  652. case TUNER_PHILIPS_FMD1216ME_MK3:
  653. case TUNER_PHILIPS_FMD1216MEX_MK3:
  654. if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ &&
  655. params->frequency >= 158870000)
  656. buf[3] |= 0x08;
  657. break;
  658. case TUNER_PHILIPS_TD1316:
  659. /* determine band */
  660. buf[3] |= (params->frequency < 161000000) ? 1 :
  661. (params->frequency < 444000000) ? 2 : 4;
  662. /* setup PLL filter */
  663. if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
  664. buf[3] |= 1 << 3;
  665. break;
  666. case TUNER_PHILIPS_TUV1236D:
  667. case TUNER_PHILIPS_FCV1236D:
  668. {
  669. unsigned int new_rf;
  670. if (dtv_input[priv->nr])
  671. new_rf = dtv_input[priv->nr];
  672. else
  673. switch (params->u.vsb.modulation) {
  674. case QAM_64:
  675. case QAM_256:
  676. new_rf = 1;
  677. break;
  678. case VSB_8:
  679. default:
  680. new_rf = 0;
  681. break;
  682. }
  683. simple_set_rf_input(fe, &buf[2], &buf[3], new_rf);
  684. break;
  685. }
  686. default:
  687. break;
  688. }
  689. }
  690. static u32 simple_dvb_configure(struct dvb_frontend *fe, u8 *buf,
  691. const struct dvb_frontend_parameters *params)
  692. {
  693. /* This function returns the tuned frequency on success, 0 on error */
  694. struct tuner_simple_priv *priv = fe->tuner_priv;
  695. struct tunertype *tun = priv->tun;
  696. static struct tuner_params *t_params;
  697. u8 config, cb;
  698. u32 div;
  699. int ret;
  700. unsigned frequency = params->frequency / 62500;
  701. if (!tun->stepsize) {
  702. /* tuner-core was loaded before the digital tuner was
  703. * configured and somehow picked the wrong tuner type */
  704. tuner_err("attempt to treat tuner %d (%s) as digital tuner "
  705. "without stepsize defined.\n",
  706. priv->type, priv->tun->name);
  707. return 0; /* failure */
  708. }
  709. t_params = simple_tuner_params(fe, TUNER_PARAM_TYPE_DIGITAL);
  710. ret = simple_config_lookup(fe, t_params, &frequency, &config, &cb);
  711. if (ret < 0)
  712. return 0; /* failure */
  713. div = ((frequency + t_params->iffreq) * 62500 + offset +
  714. tun->stepsize/2) / tun->stepsize;
  715. buf[0] = div >> 8;
  716. buf[1] = div & 0xff;
  717. buf[2] = config;
  718. buf[3] = cb;
  719. simple_set_dvb(fe, buf, params);
  720. tuner_dbg("%s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
  721. tun->name, div, buf[0], buf[1], buf[2], buf[3]);
  722. /* calculate the frequency we set it to */
  723. return (div * tun->stepsize) - t_params->iffreq;
  724. }
  725. static int simple_dvb_calc_regs(struct dvb_frontend *fe,
  726. struct dvb_frontend_parameters *params,
  727. u8 *buf, int buf_len)
  728. {
  729. struct tuner_simple_priv *priv = fe->tuner_priv;
  730. u32 frequency;
  731. if (buf_len < 5)
  732. return -EINVAL;
  733. frequency = simple_dvb_configure(fe, buf+1, params);
  734. if (frequency == 0)
  735. return -EINVAL;
  736. buf[0] = priv->i2c_props.addr;
  737. priv->frequency = frequency;
  738. priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
  739. params->u.ofdm.bandwidth : 0;
  740. return 5;
  741. }
  742. static int simple_dvb_set_params(struct dvb_frontend *fe,
  743. struct dvb_frontend_parameters *params)
  744. {
  745. struct tuner_simple_priv *priv = fe->tuner_priv;
  746. u32 prev_freq, prev_bw;
  747. int ret;
  748. u8 buf[5];
  749. if (priv->i2c_props.adap == NULL)
  750. return -EINVAL;
  751. prev_freq = priv->frequency;
  752. prev_bw = priv->bandwidth;
  753. ret = simple_dvb_calc_regs(fe, params, buf, 5);
  754. if (ret != 5)
  755. goto fail;
  756. /* put analog demod in standby when tuning digital */
  757. if (fe->ops.analog_ops.standby)
  758. fe->ops.analog_ops.standby(fe);
  759. if (fe->ops.i2c_gate_ctrl)
  760. fe->ops.i2c_gate_ctrl(fe, 1);
  761. /* buf[0] contains the i2c address, but *
  762. * we already have it in i2c_props.addr */
  763. ret = tuner_i2c_xfer_send(&priv->i2c_props, buf+1, 4);
  764. if (ret != 4)
  765. goto fail;
  766. return 0;
  767. fail:
  768. /* calc_regs sets frequency and bandwidth. if we failed, unset them */
  769. priv->frequency = prev_freq;
  770. priv->bandwidth = prev_bw;
  771. return ret;
  772. }
  773. static int simple_init(struct dvb_frontend *fe)
  774. {
  775. struct tuner_simple_priv *priv = fe->tuner_priv;
  776. if (priv->i2c_props.adap == NULL)
  777. return -EINVAL;
  778. if (priv->tun->initdata) {
  779. int ret;
  780. if (fe->ops.i2c_gate_ctrl)
  781. fe->ops.i2c_gate_ctrl(fe, 1);
  782. ret = tuner_i2c_xfer_send(&priv->i2c_props,
  783. priv->tun->initdata + 1,
  784. priv->tun->initdata[0]);
  785. if (ret != priv->tun->initdata[0])
  786. return ret;
  787. }
  788. return 0;
  789. }
  790. static int simple_sleep(struct dvb_frontend *fe)
  791. {
  792. struct tuner_simple_priv *priv = fe->tuner_priv;
  793. if (priv->i2c_props.adap == NULL)
  794. return -EINVAL;
  795. if (priv->tun->sleepdata) {
  796. int ret;
  797. if (fe->ops.i2c_gate_ctrl)
  798. fe->ops.i2c_gate_ctrl(fe, 1);
  799. ret = tuner_i2c_xfer_send(&priv->i2c_props,
  800. priv->tun->sleepdata + 1,
  801. priv->tun->sleepdata[0]);
  802. if (ret != priv->tun->sleepdata[0])
  803. return ret;
  804. }
  805. return 0;
  806. }
  807. static int simple_release(struct dvb_frontend *fe)
  808. {
  809. struct tuner_simple_priv *priv = fe->tuner_priv;
  810. mutex_lock(&tuner_simple_list_mutex);
  811. if (priv)
  812. hybrid_tuner_release_state(priv);
  813. mutex_unlock(&tuner_simple_list_mutex);
  814. fe->tuner_priv = NULL;
  815. return 0;
  816. }
  817. static int simple_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  818. {
  819. struct tuner_simple_priv *priv = fe->tuner_priv;
  820. *frequency = priv->frequency;
  821. return 0;
  822. }
  823. static int simple_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  824. {
  825. struct tuner_simple_priv *priv = fe->tuner_priv;
  826. *bandwidth = priv->bandwidth;
  827. return 0;
  828. }
  829. static struct dvb_tuner_ops simple_tuner_ops = {
  830. .init = simple_init,
  831. .sleep = simple_sleep,
  832. .set_analog_params = simple_set_params,
  833. .set_params = simple_dvb_set_params,
  834. .calc_regs = simple_dvb_calc_regs,
  835. .release = simple_release,
  836. .get_frequency = simple_get_frequency,
  837. .get_bandwidth = simple_get_bandwidth,
  838. .get_status = simple_get_status,
  839. .get_rf_strength = simple_get_rf_strength,
  840. };
  841. struct dvb_frontend *simple_tuner_attach(struct dvb_frontend *fe,
  842. struct i2c_adapter *i2c_adap,
  843. u8 i2c_addr,
  844. unsigned int type)
  845. {
  846. struct tuner_simple_priv *priv = NULL;
  847. int instance;
  848. if (type >= tuner_count) {
  849. printk(KERN_WARNING "%s: invalid tuner type: %d (max: %d)\n",
  850. __func__, type, tuner_count-1);
  851. return NULL;
  852. }
  853. /* If i2c_adap is set, check that the tuner is at the correct address.
  854. * Otherwise, if i2c_adap is NULL, the tuner will be programmed directly
  855. * by the digital demod via calc_regs.
  856. */
  857. if (i2c_adap != NULL) {
  858. u8 b[1];
  859. struct i2c_msg msg = {
  860. .addr = i2c_addr, .flags = I2C_M_RD,
  861. .buf = b, .len = 1,
  862. };
  863. if (fe->ops.i2c_gate_ctrl)
  864. fe->ops.i2c_gate_ctrl(fe, 1);
  865. if (1 != i2c_transfer(i2c_adap, &msg, 1))
  866. printk(KERN_WARNING "tuner-simple %d-%04x: "
  867. "unable to probe %s, proceeding anyway.",
  868. i2c_adapter_id(i2c_adap), i2c_addr,
  869. tuners[type].name);
  870. if (fe->ops.i2c_gate_ctrl)
  871. fe->ops.i2c_gate_ctrl(fe, 0);
  872. }
  873. mutex_lock(&tuner_simple_list_mutex);
  874. instance = hybrid_tuner_request_state(struct tuner_simple_priv, priv,
  875. hybrid_tuner_instance_list,
  876. i2c_adap, i2c_addr,
  877. "tuner-simple");
  878. switch (instance) {
  879. case 0:
  880. mutex_unlock(&tuner_simple_list_mutex);
  881. return NULL;
  882. case 1:
  883. fe->tuner_priv = priv;
  884. priv->type = type;
  885. priv->tun = &tuners[type];
  886. priv->nr = simple_devcount++;
  887. break;
  888. default:
  889. fe->tuner_priv = priv;
  890. break;
  891. }
  892. mutex_unlock(&tuner_simple_list_mutex);
  893. memcpy(&fe->ops.tuner_ops, &simple_tuner_ops,
  894. sizeof(struct dvb_tuner_ops));
  895. if (type != priv->type)
  896. tuner_warn("couldn't set type to %d. Using %d (%s) instead\n",
  897. type, priv->type, priv->tun->name);
  898. else
  899. tuner_info("type set to %d (%s)\n",
  900. priv->type, priv->tun->name);
  901. if ((debug) || ((atv_input[priv->nr] > 0) ||
  902. (dtv_input[priv->nr] > 0))) {
  903. if (0 == atv_input[priv->nr])
  904. tuner_info("tuner %d atv rf input will be "
  905. "autoselected\n", priv->nr);
  906. else
  907. tuner_info("tuner %d atv rf input will be "
  908. "set to input %d (insmod option)\n",
  909. priv->nr, atv_input[priv->nr]);
  910. if (0 == dtv_input[priv->nr])
  911. tuner_info("tuner %d dtv rf input will be "
  912. "autoselected\n", priv->nr);
  913. else
  914. tuner_info("tuner %d dtv rf input will be "
  915. "set to input %d (insmod option)\n",
  916. priv->nr, dtv_input[priv->nr]);
  917. }
  918. strlcpy(fe->ops.tuner_ops.info.name, priv->tun->name,
  919. sizeof(fe->ops.tuner_ops.info.name));
  920. return fe;
  921. }
  922. EXPORT_SYMBOL_GPL(simple_tuner_attach);
  923. MODULE_DESCRIPTION("Simple 4-control-bytes style tuner driver");
  924. MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
  925. MODULE_LICENSE("GPL");
  926. /*
  927. * Overrides for Emacs so that we follow Linus's tabbing style.
  928. * ---------------------------------------------------------------------------
  929. * Local variables:
  930. * c-basic-offset: 8
  931. * End:
  932. */