tuner-simple.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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. return ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
  121. default:
  122. return status & TUNER_STEREO;
  123. }
  124. }
  125. static inline int tuner_islocked(const int status)
  126. {
  127. return (status & TUNER_FL);
  128. }
  129. static inline int tuner_afcstatus(const int status)
  130. {
  131. return (status & TUNER_AFC) - 2;
  132. }
  133. static int simple_get_status(struct dvb_frontend *fe, u32 *status)
  134. {
  135. struct tuner_simple_priv *priv = fe->tuner_priv;
  136. int tuner_status;
  137. if (priv->i2c_props.adap == NULL)
  138. return -EINVAL;
  139. tuner_status = tuner_read_status(fe);
  140. *status = 0;
  141. if (tuner_islocked(tuner_status))
  142. *status = TUNER_STATUS_LOCKED;
  143. if (tuner_stereo(priv->type, tuner_status))
  144. *status |= TUNER_STATUS_STEREO;
  145. tuner_dbg("AFC Status: %d\n", tuner_afcstatus(tuner_status));
  146. return 0;
  147. }
  148. static int simple_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
  149. {
  150. struct tuner_simple_priv *priv = fe->tuner_priv;
  151. int signal;
  152. if (priv->i2c_props.adap == NULL)
  153. return -EINVAL;
  154. signal = tuner_signal(tuner_read_status(fe));
  155. *strength = signal;
  156. tuner_dbg("Signal strength: %d\n", signal);
  157. return 0;
  158. }
  159. /* ---------------------------------------------------------------------- */
  160. static inline char *tuner_param_name(enum param_type type)
  161. {
  162. char *name;
  163. switch (type) {
  164. case TUNER_PARAM_TYPE_RADIO:
  165. name = "radio";
  166. break;
  167. case TUNER_PARAM_TYPE_PAL:
  168. name = "pal";
  169. break;
  170. case TUNER_PARAM_TYPE_SECAM:
  171. name = "secam";
  172. break;
  173. case TUNER_PARAM_TYPE_NTSC:
  174. name = "ntsc";
  175. break;
  176. case TUNER_PARAM_TYPE_DIGITAL:
  177. name = "digital";
  178. break;
  179. default:
  180. name = "unknown";
  181. break;
  182. }
  183. return name;
  184. }
  185. static struct tuner_params *simple_tuner_params(struct dvb_frontend *fe,
  186. enum param_type desired_type)
  187. {
  188. struct tuner_simple_priv *priv = fe->tuner_priv;
  189. struct tunertype *tun = priv->tun;
  190. int i;
  191. for (i = 0; i < tun->count; i++)
  192. if (desired_type == tun->params[i].type)
  193. break;
  194. /* use default tuner params if desired_type not available */
  195. if (i == tun->count) {
  196. tuner_dbg("desired params (%s) undefined for tuner %d\n",
  197. tuner_param_name(desired_type), priv->type);
  198. i = 0;
  199. }
  200. tuner_dbg("using tuner params #%d (%s)\n", i,
  201. tuner_param_name(tun->params[i].type));
  202. return &tun->params[i];
  203. }
  204. static int simple_config_lookup(struct dvb_frontend *fe,
  205. struct tuner_params *t_params,
  206. unsigned *frequency, u8 *config, u8 *cb)
  207. {
  208. struct tuner_simple_priv *priv = fe->tuner_priv;
  209. int i;
  210. for (i = 0; i < t_params->count; i++) {
  211. if (*frequency > t_params->ranges[i].limit)
  212. continue;
  213. break;
  214. }
  215. if (i == t_params->count) {
  216. tuner_dbg("frequency out of range (%d > %d)\n",
  217. *frequency, t_params->ranges[i - 1].limit);
  218. *frequency = t_params->ranges[--i].limit;
  219. }
  220. *config = t_params->ranges[i].config;
  221. *cb = t_params->ranges[i].cb;
  222. tuner_dbg("freq = %d.%02d (%d), range = %d, "
  223. "config = 0x%02x, cb = 0x%02x\n",
  224. *frequency / 16, *frequency % 16 * 100 / 16, *frequency,
  225. i, *config, *cb);
  226. return i;
  227. }
  228. /* ---------------------------------------------------------------------- */
  229. static void simple_set_rf_input(struct dvb_frontend *fe,
  230. u8 *config, u8 *cb, unsigned int rf)
  231. {
  232. struct tuner_simple_priv *priv = fe->tuner_priv;
  233. switch (priv->type) {
  234. case TUNER_PHILIPS_TUV1236D:
  235. switch (rf) {
  236. case 1:
  237. *cb |= 0x08;
  238. break;
  239. default:
  240. *cb &= ~0x08;
  241. break;
  242. }
  243. break;
  244. case TUNER_PHILIPS_FCV1236D:
  245. switch (rf) {
  246. case 1:
  247. *cb |= 0x01;
  248. break;
  249. default:
  250. *cb &= ~0x01;
  251. break;
  252. }
  253. break;
  254. default:
  255. break;
  256. }
  257. }
  258. static int simple_std_setup(struct dvb_frontend *fe,
  259. struct analog_parameters *params,
  260. u8 *config, u8 *cb)
  261. {
  262. struct tuner_simple_priv *priv = fe->tuner_priv;
  263. u8 tuneraddr;
  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. /* 0x40 -> ATSC antenna input 1 */
  318. /* 0x48 -> ATSC antenna input 2 */
  319. /* 0x00 -> NTSC antenna input 1 */
  320. /* 0x08 -> NTSC antenna input 2 */
  321. u8 buffer[4] = { 0x14, 0x00, 0x17, 0x00};
  322. *cb &= ~0x40;
  323. if (params->std & V4L2_STD_ATSC) {
  324. *cb |= 0x40;
  325. buffer[1] = 0x04;
  326. }
  327. /* set to the correct mode (analog or digital) */
  328. tuneraddr = priv->i2c_props.addr;
  329. priv->i2c_props.addr = 0x0a;
  330. rc = tuner_i2c_xfer_send(&priv->i2c_props, &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(&priv->i2c_props, &buffer[2], 2);
  335. if (2 != rc)
  336. tuner_warn("i2c i/o error: rc == %d "
  337. "(should be 2)\n", rc);
  338. priv->i2c_props.addr = tuneraddr;
  339. break;
  340. }
  341. }
  342. if (atv_input[priv->nr])
  343. simple_set_rf_input(fe, config, cb, atv_input[priv->nr]);
  344. return 0;
  345. }
  346. static int simple_post_tune(struct dvb_frontend *fe, u8 *buffer,
  347. u16 div, u8 config, u8 cb)
  348. {
  349. struct tuner_simple_priv *priv = fe->tuner_priv;
  350. int rc;
  351. switch (priv->type) {
  352. case TUNER_LG_TDVS_H06XF:
  353. /* Set the Auxiliary Byte. */
  354. buffer[0] = buffer[2];
  355. buffer[0] &= ~0x20;
  356. buffer[0] |= 0x18;
  357. buffer[1] = 0x20;
  358. tuner_dbg("tv 0x%02x 0x%02x\n", buffer[0], buffer[1]);
  359. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 2);
  360. if (2 != rc)
  361. tuner_warn("i2c i/o error: rc == %d "
  362. "(should be 2)\n", rc);
  363. break;
  364. case TUNER_MICROTUNE_4042FI5:
  365. {
  366. /* FIXME - this may also work for other tuners */
  367. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  368. u8 status_byte = 0;
  369. /* Wait until the PLL locks */
  370. for (;;) {
  371. if (time_after(jiffies, timeout))
  372. return 0;
  373. rc = tuner_i2c_xfer_recv(&priv->i2c_props,
  374. &status_byte, 1);
  375. if (1 != rc) {
  376. tuner_warn("i2c i/o read error: rc == %d "
  377. "(should be 1)\n", rc);
  378. break;
  379. }
  380. if (status_byte & TUNER_PLL_LOCKED)
  381. break;
  382. udelay(10);
  383. }
  384. /* Set the charge pump for optimized phase noise figure */
  385. config &= ~TUNER_CHARGE_PUMP;
  386. buffer[0] = (div>>8) & 0x7f;
  387. buffer[1] = div & 0xff;
  388. buffer[2] = config;
  389. buffer[3] = cb;
  390. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  391. buffer[0], buffer[1], buffer[2], buffer[3]);
  392. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  393. if (4 != rc)
  394. tuner_warn("i2c i/o error: rc == %d "
  395. "(should be 4)\n", rc);
  396. break;
  397. }
  398. }
  399. return 0;
  400. }
  401. static int simple_radio_bandswitch(struct dvb_frontend *fe, u8 *buffer)
  402. {
  403. struct tuner_simple_priv *priv = fe->tuner_priv;
  404. switch (priv->type) {
  405. case TUNER_TENA_9533_DI:
  406. case TUNER_YMEC_TVF_5533MF:
  407. tuner_dbg("This tuner doesn't have FM. "
  408. "Most cards have a TEA5767 for FM\n");
  409. return 0;
  410. case TUNER_PHILIPS_FM1216ME_MK3:
  411. case TUNER_PHILIPS_FM1236_MK3:
  412. case TUNER_PHILIPS_FMD1216ME_MK3:
  413. case TUNER_LG_NTSC_TAPE:
  414. case TUNER_PHILIPS_FM1256_IH3:
  415. buffer[3] = 0x19;
  416. break;
  417. case TUNER_TNF_5335MF:
  418. buffer[3] = 0x11;
  419. break;
  420. case TUNER_LG_PAL_FM:
  421. buffer[3] = 0xa5;
  422. break;
  423. case TUNER_THOMSON_DTT761X:
  424. buffer[3] = 0x39;
  425. break;
  426. case TUNER_MICROTUNE_4049FM5:
  427. default:
  428. buffer[3] = 0xa4;
  429. break;
  430. }
  431. return 0;
  432. }
  433. /* ---------------------------------------------------------------------- */
  434. static int simple_set_tv_freq(struct dvb_frontend *fe,
  435. struct analog_parameters *params)
  436. {
  437. struct tuner_simple_priv *priv = fe->tuner_priv;
  438. u8 config, cb;
  439. u16 div;
  440. struct tunertype *tun;
  441. u8 buffer[4];
  442. int rc, IFPCoff, i;
  443. enum param_type desired_type;
  444. struct tuner_params *t_params;
  445. tun = priv->tun;
  446. /* IFPCoff = Video Intermediate Frequency - Vif:
  447. 940 =16*58.75 NTSC/J (Japan)
  448. 732 =16*45.75 M/N STD
  449. 704 =16*44 ATSC (at DVB code)
  450. 632 =16*39.50 I U.K.
  451. 622.4=16*38.90 B/G D/K I, L STD
  452. 592 =16*37.00 D China
  453. 590 =16.36.875 B Australia
  454. 543.2=16*33.95 L' STD
  455. 171.2=16*10.70 FM Radio (at set_radio_freq)
  456. */
  457. if (params->std == V4L2_STD_NTSC_M_JP) {
  458. IFPCoff = 940;
  459. desired_type = TUNER_PARAM_TYPE_NTSC;
  460. } else if ((params->std & V4L2_STD_MN) &&
  461. !(params->std & ~V4L2_STD_MN)) {
  462. IFPCoff = 732;
  463. desired_type = TUNER_PARAM_TYPE_NTSC;
  464. } else if (params->std == V4L2_STD_SECAM_LC) {
  465. IFPCoff = 543;
  466. desired_type = TUNER_PARAM_TYPE_SECAM;
  467. } else {
  468. IFPCoff = 623;
  469. desired_type = TUNER_PARAM_TYPE_PAL;
  470. }
  471. t_params = simple_tuner_params(fe, desired_type);
  472. i = simple_config_lookup(fe, t_params, &params->frequency,
  473. &config, &cb);
  474. div = params->frequency + IFPCoff + offset;
  475. tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, "
  476. "Offset=%d.%02d MHz, div=%0d\n",
  477. params->frequency / 16, params->frequency % 16 * 100 / 16,
  478. IFPCoff / 16, IFPCoff % 16 * 100 / 16,
  479. offset / 16, offset % 16 * 100 / 16, div);
  480. /* tv norm specific stuff for multi-norm tuners */
  481. simple_std_setup(fe, params, &config, &cb);
  482. if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
  483. buffer[0] = config;
  484. buffer[1] = cb;
  485. buffer[2] = (div>>8) & 0x7f;
  486. buffer[3] = div & 0xff;
  487. } else {
  488. buffer[0] = (div>>8) & 0x7f;
  489. buffer[1] = div & 0xff;
  490. buffer[2] = config;
  491. buffer[3] = cb;
  492. }
  493. priv->last_div = div;
  494. if (t_params->has_tda9887) {
  495. struct v4l2_priv_tun_config tda9887_cfg;
  496. int tda_config = 0;
  497. int is_secam_l = (params->std & (V4L2_STD_SECAM_L |
  498. V4L2_STD_SECAM_LC)) &&
  499. !(params->std & ~(V4L2_STD_SECAM_L |
  500. V4L2_STD_SECAM_LC));
  501. tda9887_cfg.tuner = TUNER_TDA9887;
  502. tda9887_cfg.priv = &tda_config;
  503. if (params->std == V4L2_STD_SECAM_LC) {
  504. if (t_params->port1_active ^ t_params->port1_invert_for_secam_lc)
  505. tda_config |= TDA9887_PORT1_ACTIVE;
  506. if (t_params->port2_active ^ t_params->port2_invert_for_secam_lc)
  507. tda_config |= TDA9887_PORT2_ACTIVE;
  508. } else {
  509. if (t_params->port1_active)
  510. tda_config |= TDA9887_PORT1_ACTIVE;
  511. if (t_params->port2_active)
  512. tda_config |= TDA9887_PORT2_ACTIVE;
  513. }
  514. if (t_params->intercarrier_mode)
  515. tda_config |= TDA9887_INTERCARRIER;
  516. if (is_secam_l) {
  517. if (i == 0 && t_params->default_top_secam_low)
  518. tda_config |= TDA9887_TOP(t_params->default_top_secam_low);
  519. else if (i == 1 && t_params->default_top_secam_mid)
  520. tda_config |= TDA9887_TOP(t_params->default_top_secam_mid);
  521. else if (t_params->default_top_secam_high)
  522. tda_config |= TDA9887_TOP(t_params->default_top_secam_high);
  523. } else {
  524. if (i == 0 && t_params->default_top_low)
  525. tda_config |= TDA9887_TOP(t_params->default_top_low);
  526. else if (i == 1 && t_params->default_top_mid)
  527. tda_config |= TDA9887_TOP(t_params->default_top_mid);
  528. else if (t_params->default_top_high)
  529. tda_config |= TDA9887_TOP(t_params->default_top_high);
  530. }
  531. if (t_params->default_pll_gating_18)
  532. tda_config |= TDA9887_GATING_18;
  533. i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
  534. &tda9887_cfg);
  535. }
  536. tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
  537. buffer[0], buffer[1], buffer[2], buffer[3]);
  538. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  539. if (4 != rc)
  540. tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
  541. simple_post_tune(fe, &buffer[0], div, config, cb);
  542. return 0;
  543. }
  544. static int simple_set_radio_freq(struct dvb_frontend *fe,
  545. struct analog_parameters *params)
  546. {
  547. struct tunertype *tun;
  548. struct tuner_simple_priv *priv = fe->tuner_priv;
  549. u8 buffer[4];
  550. u16 div;
  551. int rc, j;
  552. struct tuner_params *t_params;
  553. unsigned int freq = params->frequency;
  554. tun = priv->tun;
  555. for (j = tun->count-1; j > 0; j--)
  556. if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
  557. break;
  558. /* default t_params (j=0) will be used if desired type wasn't found */
  559. t_params = &tun->params[j];
  560. /* Select Radio 1st IF used */
  561. switch (t_params->radio_if) {
  562. case 0: /* 10.7 MHz */
  563. freq += (unsigned int)(10.7*16000);
  564. break;
  565. case 1: /* 33.3 MHz */
  566. freq += (unsigned int)(33.3*16000);
  567. break;
  568. case 2: /* 41.3 MHz */
  569. freq += (unsigned int)(41.3*16000);
  570. break;
  571. default:
  572. tuner_warn("Unsupported radio_if value %d\n",
  573. t_params->radio_if);
  574. return 0;
  575. }
  576. /* Bandswitch byte */
  577. simple_radio_bandswitch(fe, &buffer[0]);
  578. buffer[2] = (t_params->ranges[0].config & ~TUNER_RATIO_MASK) |
  579. TUNER_RATIO_SELECT_50; /* 50 kHz step */
  580. /* Convert from 1/16 kHz V4L steps to 1/20 MHz (=50 kHz) PLL steps
  581. freq * (1 Mhz / 16000 V4L steps) * (20 PLL steps / 1 MHz) =
  582. freq * (1/800) */
  583. div = (freq + 400) / 800;
  584. if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
  585. buffer[0] = buffer[2];
  586. buffer[1] = buffer[3];
  587. buffer[2] = (div>>8) & 0x7f;
  588. buffer[3] = div & 0xff;
  589. } else {
  590. buffer[0] = (div>>8) & 0x7f;
  591. buffer[1] = div & 0xff;
  592. }
  593. tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
  594. buffer[0], buffer[1], buffer[2], buffer[3]);
  595. priv->last_div = div;
  596. if (t_params->has_tda9887) {
  597. int config = 0;
  598. struct v4l2_priv_tun_config tda9887_cfg;
  599. tda9887_cfg.tuner = TUNER_TDA9887;
  600. tda9887_cfg.priv = &config;
  601. if (t_params->port1_active &&
  602. !t_params->port1_fm_high_sensitivity)
  603. config |= TDA9887_PORT1_ACTIVE;
  604. if (t_params->port2_active &&
  605. !t_params->port2_fm_high_sensitivity)
  606. config |= TDA9887_PORT2_ACTIVE;
  607. if (t_params->intercarrier_mode)
  608. config |= TDA9887_INTERCARRIER;
  609. /* if (t_params->port1_set_for_fm_mono)
  610. config &= ~TDA9887_PORT1_ACTIVE;*/
  611. if (t_params->fm_gain_normal)
  612. config |= TDA9887_GAIN_NORMAL;
  613. if (t_params->radio_if == 2)
  614. config |= TDA9887_RIF_41_3;
  615. i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
  616. &tda9887_cfg);
  617. }
  618. rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
  619. if (4 != rc)
  620. tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
  621. return 0;
  622. }
  623. static int simple_set_params(struct dvb_frontend *fe,
  624. struct analog_parameters *params)
  625. {
  626. struct tuner_simple_priv *priv = fe->tuner_priv;
  627. int ret = -EINVAL;
  628. if (priv->i2c_props.adap == NULL)
  629. return -EINVAL;
  630. switch (params->mode) {
  631. case V4L2_TUNER_RADIO:
  632. ret = simple_set_radio_freq(fe, params);
  633. priv->frequency = params->frequency * 125 / 2;
  634. break;
  635. case V4L2_TUNER_ANALOG_TV:
  636. case V4L2_TUNER_DIGITAL_TV:
  637. ret = simple_set_tv_freq(fe, params);
  638. priv->frequency = params->frequency * 62500;
  639. break;
  640. }
  641. priv->bandwidth = 0;
  642. return ret;
  643. }
  644. static void simple_set_dvb(struct dvb_frontend *fe, u8 *buf,
  645. const struct dvb_frontend_parameters *params)
  646. {
  647. struct tuner_simple_priv *priv = fe->tuner_priv;
  648. switch (priv->type) {
  649. case TUNER_PHILIPS_FMD1216ME_MK3:
  650. if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ &&
  651. params->frequency >= 158870000)
  652. buf[3] |= 0x08;
  653. break;
  654. case TUNER_PHILIPS_TD1316:
  655. /* determine band */
  656. buf[3] |= (params->frequency < 161000000) ? 1 :
  657. (params->frequency < 444000000) ? 2 : 4;
  658. /* setup PLL filter */
  659. if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
  660. buf[3] |= 1 << 3;
  661. break;
  662. case TUNER_PHILIPS_TUV1236D:
  663. case TUNER_PHILIPS_FCV1236D:
  664. {
  665. unsigned int new_rf;
  666. if (dtv_input[priv->nr])
  667. new_rf = dtv_input[priv->nr];
  668. else
  669. switch (params->u.vsb.modulation) {
  670. case QAM_64:
  671. case QAM_256:
  672. new_rf = 1;
  673. break;
  674. case VSB_8:
  675. default:
  676. new_rf = 0;
  677. break;
  678. }
  679. simple_set_rf_input(fe, &buf[2], &buf[3], new_rf);
  680. break;
  681. }
  682. default:
  683. break;
  684. }
  685. }
  686. static u32 simple_dvb_configure(struct dvb_frontend *fe, u8 *buf,
  687. const struct dvb_frontend_parameters *params)
  688. {
  689. /* This function returns the tuned frequency on success, 0 on error */
  690. struct tuner_simple_priv *priv = fe->tuner_priv;
  691. struct tunertype *tun = priv->tun;
  692. static struct tuner_params *t_params;
  693. u8 config, cb;
  694. u32 div;
  695. int ret;
  696. unsigned frequency = params->frequency / 62500;
  697. t_params = simple_tuner_params(fe, TUNER_PARAM_TYPE_DIGITAL);
  698. ret = simple_config_lookup(fe, t_params, &frequency, &config, &cb);
  699. if (ret < 0)
  700. return 0; /* failure */
  701. div = ((frequency + t_params->iffreq) * 62500 + offset +
  702. tun->stepsize/2) / tun->stepsize;
  703. buf[0] = div >> 8;
  704. buf[1] = div & 0xff;
  705. buf[2] = config;
  706. buf[3] = cb;
  707. simple_set_dvb(fe, buf, params);
  708. tuner_dbg("%s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
  709. tun->name, div, buf[0], buf[1], buf[2], buf[3]);
  710. /* calculate the frequency we set it to */
  711. return (div * tun->stepsize) - t_params->iffreq;
  712. }
  713. static int simple_dvb_calc_regs(struct dvb_frontend *fe,
  714. struct dvb_frontend_parameters *params,
  715. u8 *buf, int buf_len)
  716. {
  717. struct tuner_simple_priv *priv = fe->tuner_priv;
  718. u32 frequency;
  719. if (buf_len < 5)
  720. return -EINVAL;
  721. frequency = simple_dvb_configure(fe, buf+1, params);
  722. if (frequency == 0)
  723. return -EINVAL;
  724. buf[0] = priv->i2c_props.addr;
  725. priv->frequency = frequency;
  726. priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
  727. params->u.ofdm.bandwidth : 0;
  728. return 5;
  729. }
  730. static int simple_dvb_set_params(struct dvb_frontend *fe,
  731. struct dvb_frontend_parameters *params)
  732. {
  733. struct tuner_simple_priv *priv = fe->tuner_priv;
  734. u32 prev_freq, prev_bw;
  735. int ret;
  736. u8 buf[5];
  737. if (priv->i2c_props.adap == NULL)
  738. return -EINVAL;
  739. prev_freq = priv->frequency;
  740. prev_bw = priv->bandwidth;
  741. ret = simple_dvb_calc_regs(fe, params, buf, 5);
  742. if (ret != 5)
  743. goto fail;
  744. /* put analog demod in standby when tuning digital */
  745. if (fe->ops.analog_ops.standby)
  746. fe->ops.analog_ops.standby(fe);
  747. if (fe->ops.i2c_gate_ctrl)
  748. fe->ops.i2c_gate_ctrl(fe, 1);
  749. /* buf[0] contains the i2c address, but *
  750. * we already have it in i2c_props.addr */
  751. ret = tuner_i2c_xfer_send(&priv->i2c_props, buf+1, 4);
  752. if (ret != 4)
  753. goto fail;
  754. return 0;
  755. fail:
  756. /* calc_regs sets frequency and bandwidth. if we failed, unset them */
  757. priv->frequency = prev_freq;
  758. priv->bandwidth = prev_bw;
  759. return ret;
  760. }
  761. static int simple_init(struct dvb_frontend *fe)
  762. {
  763. struct tuner_simple_priv *priv = fe->tuner_priv;
  764. if (priv->i2c_props.adap == NULL)
  765. return -EINVAL;
  766. if (priv->tun->initdata) {
  767. int ret;
  768. if (fe->ops.i2c_gate_ctrl)
  769. fe->ops.i2c_gate_ctrl(fe, 1);
  770. ret = tuner_i2c_xfer_send(&priv->i2c_props,
  771. priv->tun->initdata + 1,
  772. priv->tun->initdata[0]);
  773. if (ret != priv->tun->initdata[0])
  774. return ret;
  775. }
  776. return 0;
  777. }
  778. static int simple_sleep(struct dvb_frontend *fe)
  779. {
  780. struct tuner_simple_priv *priv = fe->tuner_priv;
  781. if (priv->i2c_props.adap == NULL)
  782. return -EINVAL;
  783. if (priv->tun->sleepdata) {
  784. int ret;
  785. if (fe->ops.i2c_gate_ctrl)
  786. fe->ops.i2c_gate_ctrl(fe, 1);
  787. ret = tuner_i2c_xfer_send(&priv->i2c_props,
  788. priv->tun->sleepdata + 1,
  789. priv->tun->sleepdata[0]);
  790. if (ret != priv->tun->sleepdata[0])
  791. return ret;
  792. }
  793. return 0;
  794. }
  795. static int simple_release(struct dvb_frontend *fe)
  796. {
  797. struct tuner_simple_priv *priv = fe->tuner_priv;
  798. mutex_lock(&tuner_simple_list_mutex);
  799. if (priv)
  800. hybrid_tuner_release_state(priv);
  801. mutex_unlock(&tuner_simple_list_mutex);
  802. fe->tuner_priv = NULL;
  803. return 0;
  804. }
  805. static int simple_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  806. {
  807. struct tuner_simple_priv *priv = fe->tuner_priv;
  808. *frequency = priv->frequency;
  809. return 0;
  810. }
  811. static int simple_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  812. {
  813. struct tuner_simple_priv *priv = fe->tuner_priv;
  814. *bandwidth = priv->bandwidth;
  815. return 0;
  816. }
  817. static struct dvb_tuner_ops simple_tuner_ops = {
  818. .init = simple_init,
  819. .sleep = simple_sleep,
  820. .set_analog_params = simple_set_params,
  821. .set_params = simple_dvb_set_params,
  822. .calc_regs = simple_dvb_calc_regs,
  823. .release = simple_release,
  824. .get_frequency = simple_get_frequency,
  825. .get_bandwidth = simple_get_bandwidth,
  826. .get_status = simple_get_status,
  827. .get_rf_strength = simple_get_rf_strength,
  828. };
  829. struct dvb_frontend *simple_tuner_attach(struct dvb_frontend *fe,
  830. struct i2c_adapter *i2c_adap,
  831. u8 i2c_addr,
  832. unsigned int type)
  833. {
  834. struct tuner_simple_priv *priv = NULL;
  835. int instance;
  836. if (type >= tuner_count) {
  837. printk(KERN_WARNING "%s: invalid tuner type: %d (max: %d)\n",
  838. __func__, type, tuner_count-1);
  839. return NULL;
  840. }
  841. /* If i2c_adap is set, check that the tuner is at the correct address.
  842. * Otherwise, if i2c_adap is NULL, the tuner will be programmed directly
  843. * by the digital demod via calc_regs.
  844. */
  845. if (i2c_adap != NULL) {
  846. u8 b[1];
  847. struct i2c_msg msg = {
  848. .addr = i2c_addr, .flags = I2C_M_RD,
  849. .buf = b, .len = 1,
  850. };
  851. if (fe->ops.i2c_gate_ctrl)
  852. fe->ops.i2c_gate_ctrl(fe, 1);
  853. if (1 != i2c_transfer(i2c_adap, &msg, 1))
  854. printk(KERN_WARNING "tuner-simple %d-%04x: "
  855. "unable to probe %s, proceeding anyway.",
  856. i2c_adapter_id(i2c_adap), i2c_addr,
  857. tuners[type].name);
  858. if (fe->ops.i2c_gate_ctrl)
  859. fe->ops.i2c_gate_ctrl(fe, 0);
  860. }
  861. mutex_lock(&tuner_simple_list_mutex);
  862. instance = hybrid_tuner_request_state(struct tuner_simple_priv, priv,
  863. hybrid_tuner_instance_list,
  864. i2c_adap, i2c_addr,
  865. "tuner-simple");
  866. switch (instance) {
  867. case 0:
  868. mutex_unlock(&tuner_simple_list_mutex);
  869. return NULL;
  870. break;
  871. case 1:
  872. fe->tuner_priv = priv;
  873. priv->type = type;
  874. priv->tun = &tuners[type];
  875. priv->nr = simple_devcount++;
  876. break;
  877. default:
  878. fe->tuner_priv = priv;
  879. break;
  880. }
  881. mutex_unlock(&tuner_simple_list_mutex);
  882. memcpy(&fe->ops.tuner_ops, &simple_tuner_ops,
  883. sizeof(struct dvb_tuner_ops));
  884. tuner_info("type set to %d (%s)\n", type, priv->tun->name);
  885. if ((debug) || ((atv_input[priv->nr] > 0) ||
  886. (dtv_input[priv->nr] > 0))) {
  887. if (0 == atv_input[priv->nr])
  888. tuner_info("tuner %d atv rf input will be "
  889. "autoselected\n", priv->nr);
  890. else
  891. tuner_info("tuner %d atv rf input will be "
  892. "set to input %d (insmod option)\n",
  893. priv->nr, atv_input[priv->nr]);
  894. if (0 == dtv_input[priv->nr])
  895. tuner_info("tuner %d dtv rf input will be "
  896. "autoselected\n", priv->nr);
  897. else
  898. tuner_info("tuner %d dtv rf input will be "
  899. "set to input %d (insmod option)\n",
  900. priv->nr, dtv_input[priv->nr]);
  901. }
  902. strlcpy(fe->ops.tuner_ops.info.name, priv->tun->name,
  903. sizeof(fe->ops.tuner_ops.info.name));
  904. return fe;
  905. }
  906. EXPORT_SYMBOL_GPL(simple_tuner_attach);
  907. MODULE_DESCRIPTION("Simple 4-control-bytes style tuner driver");
  908. MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
  909. MODULE_LICENSE("GPL");
  910. /*
  911. * Overrides for Emacs so that we follow Linus's tabbing style.
  912. * ---------------------------------------------------------------------------
  913. * Local variables:
  914. * c-basic-offset: 8
  915. * End:
  916. */