tuner-simple.c 29 KB

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