tuner-simple.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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. int rc;
  265. /* tv norm specific stuff for multi-norm tuners */
  266. switch (priv->type) {
  267. case TUNER_PHILIPS_SECAM: /* FI1216MF */
  268. /* 0x01 -> ??? no change ??? */
  269. /* 0x02 -> PAL BDGHI / SECAM L */
  270. /* 0x04 -> ??? PAL others / SECAM others ??? */
  271. *cb &= ~0x03;
  272. if (params->std & V4L2_STD_SECAM_L)
  273. /* also valid for V4L2_STD_SECAM */
  274. *cb |= PHILIPS_MF_SET_STD_L;
  275. else if (params->std & V4L2_STD_SECAM_LC)
  276. *cb |= PHILIPS_MF_SET_STD_LC;
  277. else /* V4L2_STD_B|V4L2_STD_GH */
  278. *cb |= PHILIPS_MF_SET_STD_BG;
  279. break;
  280. case TUNER_TEMIC_4046FM5:
  281. *cb &= ~0x0f;
  282. if (params->std & V4L2_STD_PAL_BG) {
  283. *cb |= TEMIC_SET_PAL_BG;
  284. } else if (params->std & V4L2_STD_PAL_I) {
  285. *cb |= TEMIC_SET_PAL_I;
  286. } else if (params->std & V4L2_STD_PAL_DK) {
  287. *cb |= TEMIC_SET_PAL_DK;
  288. } else if (params->std & V4L2_STD_SECAM_L) {
  289. *cb |= TEMIC_SET_PAL_L;
  290. }
  291. break;
  292. case TUNER_PHILIPS_FQ1216ME:
  293. *cb &= ~0x0f;
  294. if (params->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
  295. *cb |= PHILIPS_SET_PAL_BGDK;
  296. } else if (params->std & V4L2_STD_PAL_I) {
  297. *cb |= PHILIPS_SET_PAL_I;
  298. } else if (params->std & V4L2_STD_SECAM_L) {
  299. *cb |= PHILIPS_SET_PAL_L;
  300. }
  301. break;
  302. case TUNER_PHILIPS_FCV1236D:
  303. /* 0x00 -> ATSC antenna input 1 */
  304. /* 0x01 -> ATSC antenna input 2 */
  305. /* 0x02 -> NTSC antenna input 1 */
  306. /* 0x03 -> NTSC antenna input 2 */
  307. *cb &= ~0x03;
  308. if (!(params->std & V4L2_STD_ATSC))
  309. *cb |= 2;
  310. break;
  311. case TUNER_MICROTUNE_4042FI5:
  312. /* Set the charge pump for fast tuning */
  313. *config |= TUNER_CHARGE_PUMP;
  314. break;
  315. case TUNER_PHILIPS_TUV1236D:
  316. {
  317. struct tuner_i2c_props i2c = priv->i2c_props;
  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. i2c.addr = 0x0a;
  330. rc = tuner_i2c_xfer_send(&i2c, &buffer[0], 2);
  331. if (2 != rc)
  332. tuner_warn("i2c i/o error: rc == %d "
  333. "(should be 2)\n", rc);
  334. rc = tuner_i2c_xfer_send(&i2c, &buffer[2], 2);
  335. if (2 != rc)
  336. tuner_warn("i2c i/o error: rc == %d "
  337. "(should be 2)\n", rc);
  338. break;
  339. }
  340. }
  341. if (atv_input[priv->nr])
  342. simple_set_rf_input(fe, config, cb, atv_input[priv->nr]);
  343. return 0;
  344. }
  345. static int simple_post_tune(struct dvb_frontend *fe, u8 *buffer,
  346. u16 div, u8 config, u8 cb)
  347. {
  348. struct tuner_simple_priv *priv = fe->tuner_priv;
  349. int rc;
  350. switch (priv->type) {
  351. case TUNER_LG_TDVS_H06XF:
  352. /* Set the Auxiliary Byte. */
  353. buffer[0] = buffer[2];
  354. buffer[0] &= ~0x20;
  355. buffer[0] |= 0x18;
  356. buffer[1] = 0x20;
  357. tuner_dbg("tv 0x%02x 0x%02x\n", buffer[0], buffer[1]);
  358. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 2);
  359. if (2 != rc)
  360. tuner_warn("i2c i/o error: rc == %d "
  361. "(should be 2)\n", rc);
  362. break;
  363. case TUNER_MICROTUNE_4042FI5:
  364. {
  365. /* FIXME - this may also work for other tuners */
  366. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  367. u8 status_byte = 0;
  368. /* Wait until the PLL locks */
  369. for (;;) {
  370. if (time_after(jiffies, timeout))
  371. return 0;
  372. rc = tuner_i2c_xfer_recv(&priv->i2c_props,
  373. &status_byte, 1);
  374. if (1 != rc) {
  375. tuner_warn("i2c i/o read error: rc == %d "
  376. "(should be 1)\n", rc);
  377. break;
  378. }
  379. if (status_byte & TUNER_PLL_LOCKED)
  380. break;
  381. udelay(10);
  382. }
  383. /* Set the charge pump for optimized phase noise figure */
  384. config &= ~TUNER_CHARGE_PUMP;
  385. buffer[0] = (div>>8) & 0x7f;
  386. buffer[1] = div & 0xff;
  387. buffer[2] = config;
  388. buffer[3] = cb;
  389. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  390. buffer[0], buffer[1], buffer[2], buffer[3]);
  391. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  392. if (4 != rc)
  393. tuner_warn("i2c i/o error: rc == %d "
  394. "(should be 4)\n", rc);
  395. break;
  396. }
  397. }
  398. return 0;
  399. }
  400. static int simple_radio_bandswitch(struct dvb_frontend *fe, u8 *buffer)
  401. {
  402. struct tuner_simple_priv *priv = fe->tuner_priv;
  403. switch (priv->type) {
  404. case TUNER_TENA_9533_DI:
  405. case TUNER_YMEC_TVF_5533MF:
  406. tuner_dbg("This tuner doesn't have FM. "
  407. "Most cards have a TEA5767 for FM\n");
  408. return 0;
  409. case TUNER_PHILIPS_FM1216ME_MK3:
  410. case TUNER_PHILIPS_FM1236_MK3:
  411. case TUNER_PHILIPS_FMD1216ME_MK3:
  412. case TUNER_PHILIPS_FMD1216MEX_MK3:
  413. case TUNER_LG_NTSC_TAPE:
  414. case TUNER_PHILIPS_FM1256_IH3:
  415. case TUNER_TCL_MF02GIP_5N:
  416. buffer[3] = 0x19;
  417. break;
  418. case TUNER_TNF_5335MF:
  419. buffer[3] = 0x11;
  420. break;
  421. case TUNER_LG_PAL_FM:
  422. buffer[3] = 0xa5;
  423. break;
  424. case TUNER_THOMSON_DTT761X:
  425. buffer[3] = 0x39;
  426. break;
  427. case TUNER_MICROTUNE_4049FM5:
  428. default:
  429. buffer[3] = 0xa4;
  430. break;
  431. }
  432. return 0;
  433. }
  434. /* ---------------------------------------------------------------------- */
  435. static int simple_set_tv_freq(struct dvb_frontend *fe,
  436. struct analog_parameters *params)
  437. {
  438. struct tuner_simple_priv *priv = fe->tuner_priv;
  439. u8 config, cb;
  440. u16 div;
  441. struct tunertype *tun;
  442. u8 buffer[4];
  443. int rc, IFPCoff, i;
  444. enum param_type desired_type;
  445. struct tuner_params *t_params;
  446. tun = priv->tun;
  447. /* IFPCoff = Video Intermediate Frequency - Vif:
  448. 940 =16*58.75 NTSC/J (Japan)
  449. 732 =16*45.75 M/N STD
  450. 704 =16*44 ATSC (at DVB code)
  451. 632 =16*39.50 I U.K.
  452. 622.4=16*38.90 B/G D/K I, L STD
  453. 592 =16*37.00 D China
  454. 590 =16.36.875 B Australia
  455. 543.2=16*33.95 L' STD
  456. 171.2=16*10.70 FM Radio (at set_radio_freq)
  457. */
  458. if (params->std == V4L2_STD_NTSC_M_JP) {
  459. IFPCoff = 940;
  460. desired_type = TUNER_PARAM_TYPE_NTSC;
  461. } else if ((params->std & V4L2_STD_MN) &&
  462. !(params->std & ~V4L2_STD_MN)) {
  463. IFPCoff = 732;
  464. desired_type = TUNER_PARAM_TYPE_NTSC;
  465. } else if (params->std == V4L2_STD_SECAM_LC) {
  466. IFPCoff = 543;
  467. desired_type = TUNER_PARAM_TYPE_SECAM;
  468. } else {
  469. IFPCoff = 623;
  470. desired_type = TUNER_PARAM_TYPE_PAL;
  471. }
  472. t_params = simple_tuner_params(fe, desired_type);
  473. i = simple_config_lookup(fe, t_params, &params->frequency,
  474. &config, &cb);
  475. div = params->frequency + IFPCoff + offset;
  476. tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, "
  477. "Offset=%d.%02d MHz, div=%0d\n",
  478. params->frequency / 16, params->frequency % 16 * 100 / 16,
  479. IFPCoff / 16, IFPCoff % 16 * 100 / 16,
  480. offset / 16, offset % 16 * 100 / 16, div);
  481. /* tv norm specific stuff for multi-norm tuners */
  482. simple_std_setup(fe, params, &config, &cb);
  483. if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
  484. buffer[0] = config;
  485. buffer[1] = cb;
  486. buffer[2] = (div>>8) & 0x7f;
  487. buffer[3] = div & 0xff;
  488. } else {
  489. buffer[0] = (div>>8) & 0x7f;
  490. buffer[1] = div & 0xff;
  491. buffer[2] = config;
  492. buffer[3] = cb;
  493. }
  494. priv->last_div = div;
  495. if (t_params->has_tda9887) {
  496. struct v4l2_priv_tun_config tda9887_cfg;
  497. int tda_config = 0;
  498. int is_secam_l = (params->std & (V4L2_STD_SECAM_L |
  499. V4L2_STD_SECAM_LC)) &&
  500. !(params->std & ~(V4L2_STD_SECAM_L |
  501. V4L2_STD_SECAM_LC));
  502. tda9887_cfg.tuner = TUNER_TDA9887;
  503. tda9887_cfg.priv = &tda_config;
  504. if (params->std == V4L2_STD_SECAM_LC) {
  505. if (t_params->port1_active ^ t_params->port1_invert_for_secam_lc)
  506. tda_config |= TDA9887_PORT1_ACTIVE;
  507. if (t_params->port2_active ^ t_params->port2_invert_for_secam_lc)
  508. tda_config |= TDA9887_PORT2_ACTIVE;
  509. } else {
  510. if (t_params->port1_active)
  511. tda_config |= TDA9887_PORT1_ACTIVE;
  512. if (t_params->port2_active)
  513. tda_config |= TDA9887_PORT2_ACTIVE;
  514. }
  515. if (t_params->intercarrier_mode)
  516. tda_config |= TDA9887_INTERCARRIER;
  517. if (is_secam_l) {
  518. if (i == 0 && t_params->default_top_secam_low)
  519. tda_config |= TDA9887_TOP(t_params->default_top_secam_low);
  520. else if (i == 1 && t_params->default_top_secam_mid)
  521. tda_config |= TDA9887_TOP(t_params->default_top_secam_mid);
  522. else if (t_params->default_top_secam_high)
  523. tda_config |= TDA9887_TOP(t_params->default_top_secam_high);
  524. } else {
  525. if (i == 0 && t_params->default_top_low)
  526. tda_config |= TDA9887_TOP(t_params->default_top_low);
  527. else if (i == 1 && t_params->default_top_mid)
  528. tda_config |= TDA9887_TOP(t_params->default_top_mid);
  529. else if (t_params->default_top_high)
  530. tda_config |= TDA9887_TOP(t_params->default_top_high);
  531. }
  532. if (t_params->default_pll_gating_18)
  533. tda_config |= TDA9887_GATING_18;
  534. i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
  535. &tda9887_cfg);
  536. }
  537. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  538. buffer[0], buffer[1], buffer[2], buffer[3]);
  539. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  540. if (4 != rc)
  541. tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
  542. simple_post_tune(fe, &buffer[0], div, config, cb);
  543. return 0;
  544. }
  545. static int simple_set_radio_freq(struct dvb_frontend *fe,
  546. struct analog_parameters *params)
  547. {
  548. struct tunertype *tun;
  549. struct tuner_simple_priv *priv = fe->tuner_priv;
  550. u8 buffer[4];
  551. u16 div;
  552. int rc, j;
  553. struct tuner_params *t_params;
  554. unsigned int freq = params->frequency;
  555. tun = priv->tun;
  556. for (j = tun->count-1; j > 0; j--)
  557. if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
  558. break;
  559. /* default t_params (j=0) will be used if desired type wasn't found */
  560. t_params = &tun->params[j];
  561. /* Select Radio 1st IF used */
  562. switch (t_params->radio_if) {
  563. case 0: /* 10.7 MHz */
  564. freq += (unsigned int)(10.7*16000);
  565. break;
  566. case 1: /* 33.3 MHz */
  567. freq += (unsigned int)(33.3*16000);
  568. break;
  569. case 2: /* 41.3 MHz */
  570. freq += (unsigned int)(41.3*16000);
  571. break;
  572. default:
  573. tuner_warn("Unsupported radio_if value %d\n",
  574. t_params->radio_if);
  575. return 0;
  576. }
  577. /* Bandswitch byte */
  578. simple_radio_bandswitch(fe, &buffer[0]);
  579. buffer[2] = (t_params->ranges[0].config & ~TUNER_RATIO_MASK) |
  580. TUNER_RATIO_SELECT_50; /* 50 kHz step */
  581. /* Convert from 1/16 kHz V4L steps to 1/20 MHz (=50 kHz) PLL steps
  582. freq * (1 Mhz / 16000 V4L steps) * (20 PLL steps / 1 MHz) =
  583. freq * (1/800) */
  584. div = (freq + 400) / 800;
  585. if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
  586. buffer[0] = buffer[2];
  587. buffer[1] = buffer[3];
  588. buffer[2] = (div>>8) & 0x7f;
  589. buffer[3] = div & 0xff;
  590. } else {
  591. buffer[0] = (div>>8) & 0x7f;
  592. buffer[1] = div & 0xff;
  593. }
  594. tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
  595. buffer[0], buffer[1], buffer[2], buffer[3]);
  596. priv->last_div = div;
  597. if (t_params->has_tda9887) {
  598. int config = 0;
  599. struct v4l2_priv_tun_config tda9887_cfg;
  600. tda9887_cfg.tuner = TUNER_TDA9887;
  601. tda9887_cfg.priv = &config;
  602. if (t_params->port1_active &&
  603. !t_params->port1_fm_high_sensitivity)
  604. config |= TDA9887_PORT1_ACTIVE;
  605. if (t_params->port2_active &&
  606. !t_params->port2_fm_high_sensitivity)
  607. config |= TDA9887_PORT2_ACTIVE;
  608. if (t_params->intercarrier_mode)
  609. config |= TDA9887_INTERCARRIER;
  610. /* if (t_params->port1_set_for_fm_mono)
  611. config &= ~TDA9887_PORT1_ACTIVE;*/
  612. if (t_params->fm_gain_normal)
  613. config |= TDA9887_GAIN_NORMAL;
  614. if (t_params->radio_if == 2)
  615. config |= TDA9887_RIF_41_3;
  616. i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
  617. &tda9887_cfg);
  618. }
  619. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  620. if (4 != rc)
  621. tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
  622. return 0;
  623. }
  624. static int simple_set_params(struct dvb_frontend *fe,
  625. struct analog_parameters *params)
  626. {
  627. struct tuner_simple_priv *priv = fe->tuner_priv;
  628. int ret = -EINVAL;
  629. if (priv->i2c_props.adap == NULL)
  630. return -EINVAL;
  631. switch (params->mode) {
  632. case V4L2_TUNER_RADIO:
  633. ret = simple_set_radio_freq(fe, params);
  634. priv->frequency = params->frequency * 125 / 2;
  635. break;
  636. case V4L2_TUNER_ANALOG_TV:
  637. case V4L2_TUNER_DIGITAL_TV:
  638. ret = simple_set_tv_freq(fe, params);
  639. priv->frequency = params->frequency * 62500;
  640. break;
  641. }
  642. priv->bandwidth = 0;
  643. return ret;
  644. }
  645. static void simple_set_dvb(struct dvb_frontend *fe, u8 *buf,
  646. const struct dvb_frontend_parameters *params)
  647. {
  648. struct tuner_simple_priv *priv = fe->tuner_priv;
  649. switch (priv->type) {
  650. case TUNER_PHILIPS_FMD1216ME_MK3:
  651. case TUNER_PHILIPS_FMD1216MEX_MK3:
  652. if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ &&
  653. params->frequency >= 158870000)
  654. buf[3] |= 0x08;
  655. break;
  656. case TUNER_PHILIPS_TD1316:
  657. /* determine band */
  658. buf[3] |= (params->frequency < 161000000) ? 1 :
  659. (params->frequency < 444000000) ? 2 : 4;
  660. /* setup PLL filter */
  661. if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
  662. buf[3] |= 1 << 3;
  663. break;
  664. case TUNER_PHILIPS_TUV1236D:
  665. case TUNER_PHILIPS_FCV1236D:
  666. {
  667. unsigned int new_rf;
  668. if (dtv_input[priv->nr])
  669. new_rf = dtv_input[priv->nr];
  670. else
  671. switch (params->u.vsb.modulation) {
  672. case QAM_64:
  673. case QAM_256:
  674. new_rf = 1;
  675. break;
  676. case VSB_8:
  677. default:
  678. new_rf = 0;
  679. break;
  680. }
  681. simple_set_rf_input(fe, &buf[2], &buf[3], new_rf);
  682. break;
  683. }
  684. default:
  685. break;
  686. }
  687. }
  688. static u32 simple_dvb_configure(struct dvb_frontend *fe, u8 *buf,
  689. const struct dvb_frontend_parameters *params)
  690. {
  691. /* This function returns the tuned frequency on success, 0 on error */
  692. struct tuner_simple_priv *priv = fe->tuner_priv;
  693. struct tunertype *tun = priv->tun;
  694. static struct tuner_params *t_params;
  695. u8 config, cb;
  696. u32 div;
  697. int ret;
  698. unsigned frequency = params->frequency / 62500;
  699. if (!tun->stepsize) {
  700. /* tuner-core was loaded before the digital tuner was
  701. * configured and somehow picked the wrong tuner type */
  702. tuner_err("attempt to treat tuner %d (%s) as digital tuner "
  703. "without stepsize defined.\n",
  704. priv->type, priv->tun->name);
  705. return 0; /* failure */
  706. }
  707. t_params = simple_tuner_params(fe, TUNER_PARAM_TYPE_DIGITAL);
  708. ret = simple_config_lookup(fe, t_params, &frequency, &config, &cb);
  709. if (ret < 0)
  710. return 0; /* failure */
  711. div = ((frequency + t_params->iffreq) * 62500 + offset +
  712. tun->stepsize/2) / tun->stepsize;
  713. buf[0] = div >> 8;
  714. buf[1] = div & 0xff;
  715. buf[2] = config;
  716. buf[3] = cb;
  717. simple_set_dvb(fe, buf, params);
  718. tuner_dbg("%s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
  719. tun->name, div, buf[0], buf[1], buf[2], buf[3]);
  720. /* calculate the frequency we set it to */
  721. return (div * tun->stepsize) - t_params->iffreq;
  722. }
  723. static int simple_dvb_calc_regs(struct dvb_frontend *fe,
  724. struct dvb_frontend_parameters *params,
  725. u8 *buf, int buf_len)
  726. {
  727. struct tuner_simple_priv *priv = fe->tuner_priv;
  728. u32 frequency;
  729. if (buf_len < 5)
  730. return -EINVAL;
  731. frequency = simple_dvb_configure(fe, buf+1, params);
  732. if (frequency == 0)
  733. return -EINVAL;
  734. buf[0] = priv->i2c_props.addr;
  735. priv->frequency = frequency;
  736. priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
  737. params->u.ofdm.bandwidth : 0;
  738. return 5;
  739. }
  740. static int simple_dvb_set_params(struct dvb_frontend *fe,
  741. struct dvb_frontend_parameters *params)
  742. {
  743. struct tuner_simple_priv *priv = fe->tuner_priv;
  744. u32 prev_freq, prev_bw;
  745. int ret;
  746. u8 buf[5];
  747. if (priv->i2c_props.adap == NULL)
  748. return -EINVAL;
  749. prev_freq = priv->frequency;
  750. prev_bw = priv->bandwidth;
  751. ret = simple_dvb_calc_regs(fe, params, buf, 5);
  752. if (ret != 5)
  753. goto fail;
  754. /* put analog demod in standby when tuning digital */
  755. if (fe->ops.analog_ops.standby)
  756. fe->ops.analog_ops.standby(fe);
  757. if (fe->ops.i2c_gate_ctrl)
  758. fe->ops.i2c_gate_ctrl(fe, 1);
  759. /* buf[0] contains the i2c address, but *
  760. * we already have it in i2c_props.addr */
  761. ret = tuner_i2c_xfer_send(&priv->i2c_props, buf+1, 4);
  762. if (ret != 4)
  763. goto fail;
  764. return 0;
  765. fail:
  766. /* calc_regs sets frequency and bandwidth. if we failed, unset them */
  767. priv->frequency = prev_freq;
  768. priv->bandwidth = prev_bw;
  769. return ret;
  770. }
  771. static int simple_init(struct dvb_frontend *fe)
  772. {
  773. struct tuner_simple_priv *priv = fe->tuner_priv;
  774. if (priv->i2c_props.adap == NULL)
  775. return -EINVAL;
  776. if (priv->tun->initdata) {
  777. int ret;
  778. if (fe->ops.i2c_gate_ctrl)
  779. fe->ops.i2c_gate_ctrl(fe, 1);
  780. ret = tuner_i2c_xfer_send(&priv->i2c_props,
  781. priv->tun->initdata + 1,
  782. priv->tun->initdata[0]);
  783. if (ret != priv->tun->initdata[0])
  784. return ret;
  785. }
  786. return 0;
  787. }
  788. static int simple_sleep(struct dvb_frontend *fe)
  789. {
  790. struct tuner_simple_priv *priv = fe->tuner_priv;
  791. if (priv->i2c_props.adap == NULL)
  792. return -EINVAL;
  793. if (priv->tun->sleepdata) {
  794. int ret;
  795. if (fe->ops.i2c_gate_ctrl)
  796. fe->ops.i2c_gate_ctrl(fe, 1);
  797. ret = tuner_i2c_xfer_send(&priv->i2c_props,
  798. priv->tun->sleepdata + 1,
  799. priv->tun->sleepdata[0]);
  800. if (ret != priv->tun->sleepdata[0])
  801. return ret;
  802. }
  803. return 0;
  804. }
  805. static int simple_release(struct dvb_frontend *fe)
  806. {
  807. struct tuner_simple_priv *priv = fe->tuner_priv;
  808. mutex_lock(&tuner_simple_list_mutex);
  809. if (priv)
  810. hybrid_tuner_release_state(priv);
  811. mutex_unlock(&tuner_simple_list_mutex);
  812. fe->tuner_priv = NULL;
  813. return 0;
  814. }
  815. static int simple_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  816. {
  817. struct tuner_simple_priv *priv = fe->tuner_priv;
  818. *frequency = priv->frequency;
  819. return 0;
  820. }
  821. static int simple_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  822. {
  823. struct tuner_simple_priv *priv = fe->tuner_priv;
  824. *bandwidth = priv->bandwidth;
  825. return 0;
  826. }
  827. static struct dvb_tuner_ops simple_tuner_ops = {
  828. .init = simple_init,
  829. .sleep = simple_sleep,
  830. .set_analog_params = simple_set_params,
  831. .set_params = simple_dvb_set_params,
  832. .calc_regs = simple_dvb_calc_regs,
  833. .release = simple_release,
  834. .get_frequency = simple_get_frequency,
  835. .get_bandwidth = simple_get_bandwidth,
  836. .get_status = simple_get_status,
  837. .get_rf_strength = simple_get_rf_strength,
  838. };
  839. struct dvb_frontend *simple_tuner_attach(struct dvb_frontend *fe,
  840. struct i2c_adapter *i2c_adap,
  841. u8 i2c_addr,
  842. unsigned int type)
  843. {
  844. struct tuner_simple_priv *priv = NULL;
  845. int instance;
  846. if (type >= tuner_count) {
  847. printk(KERN_WARNING "%s: invalid tuner type: %d (max: %d)\n",
  848. __func__, type, tuner_count-1);
  849. return NULL;
  850. }
  851. /* If i2c_adap is set, check that the tuner is at the correct address.
  852. * Otherwise, if i2c_adap is NULL, the tuner will be programmed directly
  853. * by the digital demod via calc_regs.
  854. */
  855. if (i2c_adap != NULL) {
  856. u8 b[1];
  857. struct i2c_msg msg = {
  858. .addr = i2c_addr, .flags = I2C_M_RD,
  859. .buf = b, .len = 1,
  860. };
  861. if (fe->ops.i2c_gate_ctrl)
  862. fe->ops.i2c_gate_ctrl(fe, 1);
  863. if (1 != i2c_transfer(i2c_adap, &msg, 1))
  864. printk(KERN_WARNING "tuner-simple %d-%04x: "
  865. "unable to probe %s, proceeding anyway.",
  866. i2c_adapter_id(i2c_adap), i2c_addr,
  867. tuners[type].name);
  868. if (fe->ops.i2c_gate_ctrl)
  869. fe->ops.i2c_gate_ctrl(fe, 0);
  870. }
  871. mutex_lock(&tuner_simple_list_mutex);
  872. instance = hybrid_tuner_request_state(struct tuner_simple_priv, priv,
  873. hybrid_tuner_instance_list,
  874. i2c_adap, i2c_addr,
  875. "tuner-simple");
  876. switch (instance) {
  877. case 0:
  878. mutex_unlock(&tuner_simple_list_mutex);
  879. return NULL;
  880. case 1:
  881. fe->tuner_priv = priv;
  882. priv->type = type;
  883. priv->tun = &tuners[type];
  884. priv->nr = simple_devcount++;
  885. break;
  886. default:
  887. fe->tuner_priv = priv;
  888. break;
  889. }
  890. mutex_unlock(&tuner_simple_list_mutex);
  891. memcpy(&fe->ops.tuner_ops, &simple_tuner_ops,
  892. sizeof(struct dvb_tuner_ops));
  893. if (type != priv->type)
  894. tuner_warn("couldn't set type to %d. Using %d (%s) instead\n",
  895. type, priv->type, priv->tun->name);
  896. else
  897. tuner_info("type set to %d (%s)\n",
  898. priv->type, priv->tun->name);
  899. if ((debug) || ((atv_input[priv->nr] > 0) ||
  900. (dtv_input[priv->nr] > 0))) {
  901. if (0 == atv_input[priv->nr])
  902. tuner_info("tuner %d atv rf input will be "
  903. "autoselected\n", priv->nr);
  904. else
  905. tuner_info("tuner %d atv rf input will be "
  906. "set to input %d (insmod option)\n",
  907. priv->nr, atv_input[priv->nr]);
  908. if (0 == dtv_input[priv->nr])
  909. tuner_info("tuner %d dtv rf input will be "
  910. "autoselected\n", priv->nr);
  911. else
  912. tuner_info("tuner %d dtv rf input will be "
  913. "set to input %d (insmod option)\n",
  914. priv->nr, dtv_input[priv->nr]);
  915. }
  916. strlcpy(fe->ops.tuner_ops.info.name, priv->tun->name,
  917. sizeof(fe->ops.tuner_ops.info.name));
  918. return fe;
  919. }
  920. EXPORT_SYMBOL_GPL(simple_tuner_attach);
  921. MODULE_DESCRIPTION("Simple 4-control-bytes style tuner driver");
  922. MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
  923. MODULE_LICENSE("GPL");
  924. /*
  925. * Overrides for Emacs so that we follow Linus's tabbing style.
  926. * ---------------------------------------------------------------------------
  927. * Local variables:
  928. * c-basic-offset: 8
  929. * End:
  930. */